erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 28 | #include "v8.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 29 | #include "hydrogen.h" |
| 30 | |
| 31 | #include "codegen.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 32 | #include "full-codegen.h" |
| 33 | #include "hashmap.h" |
| 34 | #include "lithium-allocator.h" |
| 35 | #include "parser.h" |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 36 | #include "scopeinfo.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 37 | #include "scopes.h" |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 38 | #include "stub-cache.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 39 | |
| 40 | #if V8_TARGET_ARCH_IA32 |
| 41 | #include "ia32/lithium-codegen-ia32.h" |
| 42 | #elif V8_TARGET_ARCH_X64 |
| 43 | #include "x64/lithium-codegen-x64.h" |
| 44 | #elif V8_TARGET_ARCH_ARM |
| 45 | #include "arm/lithium-codegen-arm.h" |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 46 | #elif V8_TARGET_ARCH_MIPS |
| 47 | #include "mips/lithium-codegen-mips.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 48 | #else |
| 49 | #error Unsupported target architecture. |
| 50 | #endif |
| 51 | |
| 52 | namespace v8 { |
| 53 | namespace internal { |
| 54 | |
| 55 | HBasicBlock::HBasicBlock(HGraph* graph) |
| 56 | : block_id_(graph->GetNextBlockID()), |
| 57 | graph_(graph), |
| 58 | phis_(4), |
| 59 | first_(NULL), |
| 60 | last_(NULL), |
| 61 | end_(NULL), |
| 62 | loop_information_(NULL), |
| 63 | predecessors_(2), |
| 64 | dominator_(NULL), |
| 65 | dominated_blocks_(4), |
| 66 | last_environment_(NULL), |
| 67 | argument_count_(-1), |
| 68 | first_instruction_index_(-1), |
| 69 | last_instruction_index_(-1), |
| 70 | deleted_phis_(4), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 71 | parent_loop_header_(NULL), |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 72 | is_inline_return_target_(false), |
| 73 | is_deoptimizing_(false) { } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 74 | |
| 75 | |
| 76 | void HBasicBlock::AttachLoopInformation() { |
| 77 | ASSERT(!IsLoopHeader()); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 78 | loop_information_ = new(zone()) HLoopInformation(this); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | |
| 82 | void HBasicBlock::DetachLoopInformation() { |
| 83 | ASSERT(IsLoopHeader()); |
| 84 | loop_information_ = NULL; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | void HBasicBlock::AddPhi(HPhi* phi) { |
| 89 | ASSERT(!IsStartBlock()); |
| 90 | phis_.Add(phi); |
| 91 | phi->SetBlock(this); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | void HBasicBlock::RemovePhi(HPhi* phi) { |
| 96 | ASSERT(phi->block() == this); |
| 97 | ASSERT(phis_.Contains(phi)); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 98 | ASSERT(phi->HasNoUses() || !phi->is_live()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 99 | phi->ClearOperands(); |
| 100 | phis_.RemoveElement(phi); |
| 101 | phi->SetBlock(NULL); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | void HBasicBlock::AddInstruction(HInstruction* instr) { |
| 106 | ASSERT(!IsStartBlock() || !IsFinished()); |
| 107 | ASSERT(!instr->IsLinked()); |
| 108 | ASSERT(!IsFinished()); |
| 109 | if (first_ == NULL) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 110 | HBlockEntry* entry = new(zone()) HBlockEntry(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 111 | entry->InitializeAsFirst(this); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 112 | first_ = last_ = entry; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 113 | } |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 114 | instr->InsertAfter(last_); |
| 115 | last_ = instr; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 119 | HDeoptimize* HBasicBlock::CreateDeoptimize( |
| 120 | HDeoptimize::UseEnvironment has_uses) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 121 | ASSERT(HasEnvironment()); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 122 | if (has_uses == HDeoptimize::kNoUses) return new(zone()) HDeoptimize(0); |
| 123 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 124 | HEnvironment* environment = last_environment(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 125 | HDeoptimize* instr = new(zone()) HDeoptimize(environment->length()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 126 | for (int i = 0; i < environment->length(); i++) { |
| 127 | HValue* val = environment->values()->at(i); |
| 128 | instr->AddEnvironmentValue(val); |
| 129 | } |
| 130 | |
| 131 | return instr; |
| 132 | } |
| 133 | |
| 134 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 135 | HSimulate* HBasicBlock::CreateSimulate(int ast_id) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 136 | ASSERT(HasEnvironment()); |
| 137 | HEnvironment* environment = last_environment(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 138 | ASSERT(ast_id == AstNode::kNoNumber || |
| 139 | environment->closure()->shared()->VerifyBailoutId(ast_id)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 140 | |
| 141 | int push_count = environment->push_count(); |
| 142 | int pop_count = environment->pop_count(); |
| 143 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 144 | HSimulate* instr = new(zone()) HSimulate(ast_id, pop_count); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 145 | for (int i = push_count - 1; i >= 0; --i) { |
| 146 | instr->AddPushedValue(environment->ExpressionStackAt(i)); |
| 147 | } |
| 148 | for (int i = 0; i < environment->assigned_variables()->length(); ++i) { |
| 149 | int index = environment->assigned_variables()->at(i); |
| 150 | instr->AddAssignedValue(index, environment->Lookup(index)); |
| 151 | } |
| 152 | environment->ClearHistory(); |
| 153 | return instr; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | void HBasicBlock::Finish(HControlInstruction* end) { |
| 158 | ASSERT(!IsFinished()); |
| 159 | AddInstruction(end); |
| 160 | end_ = end; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 161 | for (HSuccessorIterator it(end); !it.Done(); it.Advance()) { |
| 162 | it.Current()->RegisterPredecessor(this); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 167 | void HBasicBlock::Goto(HBasicBlock* block, bool drop_extra) { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 168 | if (block->IsInlineReturnTarget()) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 169 | AddInstruction(new(zone()) HLeaveInlined); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 170 | last_environment_ = last_environment()->DiscardInlined(drop_extra); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 171 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 172 | AddSimulate(AstNode::kNoNumber); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 173 | HGoto* instr = new(zone()) HGoto(block); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 174 | Finish(instr); |
| 175 | } |
| 176 | |
| 177 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 178 | void HBasicBlock::AddLeaveInlined(HValue* return_value, |
| 179 | HBasicBlock* target, |
| 180 | bool drop_extra) { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 181 | ASSERT(target->IsInlineReturnTarget()); |
| 182 | ASSERT(return_value != NULL); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 183 | AddInstruction(new(zone()) HLeaveInlined); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 184 | last_environment_ = last_environment()->DiscardInlined(drop_extra); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 185 | last_environment()->Push(return_value); |
| 186 | AddSimulate(AstNode::kNoNumber); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 187 | HGoto* instr = new(zone()) HGoto(target); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 188 | Finish(instr); |
| 189 | } |
| 190 | |
| 191 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 192 | void HBasicBlock::SetInitialEnvironment(HEnvironment* env) { |
| 193 | ASSERT(!HasEnvironment()); |
| 194 | ASSERT(first() == NULL); |
| 195 | UpdateEnvironment(env); |
| 196 | } |
| 197 | |
| 198 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 199 | void HBasicBlock::SetJoinId(int ast_id) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 200 | int length = predecessors_.length(); |
| 201 | ASSERT(length > 0); |
| 202 | for (int i = 0; i < length; i++) { |
| 203 | HBasicBlock* predecessor = predecessors_[i]; |
| 204 | ASSERT(predecessor->end()->IsGoto()); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 205 | HSimulate* simulate = HSimulate::cast(predecessor->end()->previous()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 206 | // We only need to verify the ID once. |
| 207 | ASSERT(i != 0 || |
| 208 | predecessor->last_environment()->closure()->shared() |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 209 | ->VerifyBailoutId(ast_id)); |
| 210 | simulate->set_ast_id(ast_id); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | |
| 215 | bool HBasicBlock::Dominates(HBasicBlock* other) const { |
| 216 | HBasicBlock* current = other->dominator(); |
| 217 | while (current != NULL) { |
| 218 | if (current == this) return true; |
| 219 | current = current->dominator(); |
| 220 | } |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 225 | int HBasicBlock::LoopNestingDepth() const { |
| 226 | const HBasicBlock* current = this; |
| 227 | int result = (current->IsLoopHeader()) ? 1 : 0; |
| 228 | while (current->parent_loop_header() != NULL) { |
| 229 | current = current->parent_loop_header(); |
| 230 | result++; |
| 231 | } |
| 232 | return result; |
| 233 | } |
| 234 | |
| 235 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 236 | void HBasicBlock::PostProcessLoopHeader(IterationStatement* stmt) { |
| 237 | ASSERT(IsLoopHeader()); |
| 238 | |
| 239 | SetJoinId(stmt->EntryId()); |
| 240 | if (predecessors()->length() == 1) { |
| 241 | // This is a degenerated loop. |
| 242 | DetachLoopInformation(); |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | // Only the first entry into the loop is from outside the loop. All other |
| 247 | // entries must be back edges. |
| 248 | for (int i = 1; i < predecessors()->length(); ++i) { |
| 249 | loop_information()->RegisterBackEdge(predecessors()->at(i)); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | |
| 254 | void HBasicBlock::RegisterPredecessor(HBasicBlock* pred) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 255 | if (HasPredecessor()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 256 | // Only loop header blocks can have a predecessor added after |
| 257 | // instructions have been added to the block (they have phis for all |
| 258 | // values in the environment, these phis may be eliminated later). |
| 259 | ASSERT(IsLoopHeader() || first_ == NULL); |
| 260 | HEnvironment* incoming_env = pred->last_environment(); |
| 261 | if (IsLoopHeader()) { |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 262 | ASSERT(phis()->length() == incoming_env->length()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 263 | for (int i = 0; i < phis_.length(); ++i) { |
| 264 | phis_[i]->AddInput(incoming_env->values()->at(i)); |
| 265 | } |
| 266 | } else { |
| 267 | last_environment()->AddIncomingEdge(this, pred->last_environment()); |
| 268 | } |
| 269 | } else if (!HasEnvironment() && !IsFinished()) { |
| 270 | ASSERT(!IsLoopHeader()); |
| 271 | SetInitialEnvironment(pred->last_environment()->Copy()); |
| 272 | } |
| 273 | |
| 274 | predecessors_.Add(pred); |
| 275 | } |
| 276 | |
| 277 | |
| 278 | void HBasicBlock::AddDominatedBlock(HBasicBlock* block) { |
| 279 | ASSERT(!dominated_blocks_.Contains(block)); |
| 280 | // Keep the list of dominated blocks sorted such that if there is two |
| 281 | // succeeding block in this list, the predecessor is before the successor. |
| 282 | int index = 0; |
| 283 | while (index < dominated_blocks_.length() && |
| 284 | dominated_blocks_[index]->block_id() < block->block_id()) { |
| 285 | ++index; |
| 286 | } |
| 287 | dominated_blocks_.InsertAt(index, block); |
| 288 | } |
| 289 | |
| 290 | |
| 291 | void HBasicBlock::AssignCommonDominator(HBasicBlock* other) { |
| 292 | if (dominator_ == NULL) { |
| 293 | dominator_ = other; |
| 294 | other->AddDominatedBlock(this); |
| 295 | } else if (other->dominator() != NULL) { |
| 296 | HBasicBlock* first = dominator_; |
| 297 | HBasicBlock* second = other; |
| 298 | |
| 299 | while (first != second) { |
| 300 | if (first->block_id() > second->block_id()) { |
| 301 | first = first->dominator(); |
| 302 | } else { |
| 303 | second = second->dominator(); |
| 304 | } |
| 305 | ASSERT(first != NULL && second != NULL); |
| 306 | } |
| 307 | |
| 308 | if (dominator_ != first) { |
| 309 | ASSERT(dominator_->dominated_blocks_.Contains(this)); |
| 310 | dominator_->dominated_blocks_.RemoveElement(this); |
| 311 | dominator_ = first; |
| 312 | first->AddDominatedBlock(this); |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | |
| 318 | int HBasicBlock::PredecessorIndexOf(HBasicBlock* predecessor) const { |
| 319 | for (int i = 0; i < predecessors_.length(); ++i) { |
| 320 | if (predecessors_[i] == predecessor) return i; |
| 321 | } |
| 322 | UNREACHABLE(); |
| 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | #ifdef DEBUG |
| 328 | void HBasicBlock::Verify() { |
| 329 | // Check that every block is finished. |
| 330 | ASSERT(IsFinished()); |
| 331 | ASSERT(block_id() >= 0); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 332 | |
| 333 | // Check that the incoming edges are in edge split form. |
| 334 | if (predecessors_.length() > 1) { |
| 335 | for (int i = 0; i < predecessors_.length(); ++i) { |
| 336 | ASSERT(predecessors_[i]->end()->SecondSuccessor() == NULL); |
| 337 | } |
| 338 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 339 | } |
| 340 | #endif |
| 341 | |
| 342 | |
| 343 | void HLoopInformation::RegisterBackEdge(HBasicBlock* block) { |
| 344 | this->back_edges_.Add(block); |
| 345 | AddBlock(block); |
| 346 | } |
| 347 | |
| 348 | |
| 349 | HBasicBlock* HLoopInformation::GetLastBackEdge() const { |
| 350 | int max_id = -1; |
| 351 | HBasicBlock* result = NULL; |
| 352 | for (int i = 0; i < back_edges_.length(); ++i) { |
| 353 | HBasicBlock* cur = back_edges_[i]; |
| 354 | if (cur->block_id() > max_id) { |
| 355 | max_id = cur->block_id(); |
| 356 | result = cur; |
| 357 | } |
| 358 | } |
| 359 | return result; |
| 360 | } |
| 361 | |
| 362 | |
| 363 | void HLoopInformation::AddBlock(HBasicBlock* block) { |
| 364 | if (block == loop_header()) return; |
| 365 | if (block->parent_loop_header() == loop_header()) return; |
| 366 | if (block->parent_loop_header() != NULL) { |
| 367 | AddBlock(block->parent_loop_header()); |
| 368 | } else { |
| 369 | block->set_parent_loop_header(loop_header()); |
| 370 | blocks_.Add(block); |
| 371 | for (int i = 0; i < block->predecessors()->length(); ++i) { |
| 372 | AddBlock(block->predecessors()->at(i)); |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | |
| 378 | #ifdef DEBUG |
| 379 | |
| 380 | // Checks reachability of the blocks in this graph and stores a bit in |
| 381 | // the BitVector "reachable()" for every block that can be reached |
| 382 | // from the start block of the graph. If "dont_visit" is non-null, the given |
| 383 | // block is treated as if it would not be part of the graph. "visited_count()" |
| 384 | // returns the number of reachable blocks. |
| 385 | class ReachabilityAnalyzer BASE_EMBEDDED { |
| 386 | public: |
| 387 | ReachabilityAnalyzer(HBasicBlock* entry_block, |
| 388 | int block_count, |
| 389 | HBasicBlock* dont_visit) |
| 390 | : visited_count_(0), |
| 391 | stack_(16), |
| 392 | reachable_(block_count), |
| 393 | dont_visit_(dont_visit) { |
| 394 | PushBlock(entry_block); |
| 395 | Analyze(); |
| 396 | } |
| 397 | |
| 398 | int visited_count() const { return visited_count_; } |
| 399 | const BitVector* reachable() const { return &reachable_; } |
| 400 | |
| 401 | private: |
| 402 | void PushBlock(HBasicBlock* block) { |
| 403 | if (block != NULL && block != dont_visit_ && |
| 404 | !reachable_.Contains(block->block_id())) { |
| 405 | reachable_.Add(block->block_id()); |
| 406 | stack_.Add(block); |
| 407 | visited_count_++; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | void Analyze() { |
| 412 | while (!stack_.is_empty()) { |
| 413 | HControlInstruction* end = stack_.RemoveLast()->end(); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 414 | for (HSuccessorIterator it(end); !it.Done(); it.Advance()) { |
| 415 | PushBlock(it.Current()); |
| 416 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
| 420 | int visited_count_; |
| 421 | ZoneList<HBasicBlock*> stack_; |
| 422 | BitVector reachable_; |
| 423 | HBasicBlock* dont_visit_; |
| 424 | }; |
| 425 | |
| 426 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 427 | void HGraph::Verify(bool do_full_verify) const { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 428 | for (int i = 0; i < blocks_.length(); i++) { |
| 429 | HBasicBlock* block = blocks_.at(i); |
| 430 | |
| 431 | block->Verify(); |
| 432 | |
| 433 | // Check that every block contains at least one node and that only the last |
| 434 | // node is a control instruction. |
| 435 | HInstruction* current = block->first(); |
| 436 | ASSERT(current != NULL && current->IsBlockEntry()); |
| 437 | while (current != NULL) { |
| 438 | ASSERT((current->next() == NULL) == current->IsControlInstruction()); |
| 439 | ASSERT(current->block() == block); |
| 440 | current->Verify(); |
| 441 | current = current->next(); |
| 442 | } |
| 443 | |
| 444 | // Check that successors are correctly set. |
| 445 | HBasicBlock* first = block->end()->FirstSuccessor(); |
| 446 | HBasicBlock* second = block->end()->SecondSuccessor(); |
| 447 | ASSERT(second == NULL || first != NULL); |
| 448 | |
| 449 | // Check that the predecessor array is correct. |
| 450 | if (first != NULL) { |
| 451 | ASSERT(first->predecessors()->Contains(block)); |
| 452 | if (second != NULL) { |
| 453 | ASSERT(second->predecessors()->Contains(block)); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | // Check that phis have correct arguments. |
| 458 | for (int j = 0; j < block->phis()->length(); j++) { |
| 459 | HPhi* phi = block->phis()->at(j); |
| 460 | phi->Verify(); |
| 461 | } |
| 462 | |
| 463 | // Check that all join blocks have predecessors that end with an |
| 464 | // unconditional goto and agree on their environment node id. |
| 465 | if (block->predecessors()->length() >= 2) { |
| 466 | int id = block->predecessors()->first()->last_environment()->ast_id(); |
| 467 | for (int k = 0; k < block->predecessors()->length(); k++) { |
| 468 | HBasicBlock* predecessor = block->predecessors()->at(k); |
| 469 | ASSERT(predecessor->end()->IsGoto()); |
| 470 | ASSERT(predecessor->last_environment()->ast_id() == id); |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | // Check special property of first block to have no predecessors. |
| 476 | ASSERT(blocks_.at(0)->predecessors()->is_empty()); |
| 477 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 478 | if (do_full_verify) { |
| 479 | // Check that the graph is fully connected. |
| 480 | ReachabilityAnalyzer analyzer(entry_block_, blocks_.length(), NULL); |
| 481 | ASSERT(analyzer.visited_count() == blocks_.length()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 482 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 483 | // Check that entry block dominator is NULL. |
| 484 | ASSERT(entry_block_->dominator() == NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 485 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 486 | // Check dominators. |
| 487 | for (int i = 0; i < blocks_.length(); ++i) { |
| 488 | HBasicBlock* block = blocks_.at(i); |
| 489 | if (block->dominator() == NULL) { |
| 490 | // Only start block may have no dominator assigned to. |
| 491 | ASSERT(i == 0); |
| 492 | } else { |
| 493 | // Assert that block is unreachable if dominator must not be visited. |
| 494 | ReachabilityAnalyzer dominator_analyzer(entry_block_, |
| 495 | blocks_.length(), |
| 496 | block->dominator()); |
| 497 | ASSERT(!dominator_analyzer.reachable()->Contains(block->block_id())); |
| 498 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | #endif |
| 504 | |
| 505 | |
| 506 | HConstant* HGraph::GetConstant(SetOncePointer<HConstant>* pointer, |
| 507 | Object* value) { |
| 508 | if (!pointer->is_set()) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 509 | HConstant* constant = new(zone()) HConstant(Handle<Object>(value), |
| 510 | Representation::Tagged()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 511 | constant->InsertAfter(GetConstantUndefined()); |
| 512 | pointer->set(constant); |
| 513 | } |
| 514 | return pointer->get(); |
| 515 | } |
| 516 | |
| 517 | |
| 518 | HConstant* HGraph::GetConstant1() { |
| 519 | return GetConstant(&constant_1_, Smi::FromInt(1)); |
| 520 | } |
| 521 | |
| 522 | |
| 523 | HConstant* HGraph::GetConstantMinus1() { |
| 524 | return GetConstant(&constant_minus1_, Smi::FromInt(-1)); |
| 525 | } |
| 526 | |
| 527 | |
| 528 | HConstant* HGraph::GetConstantTrue() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 529 | return GetConstant(&constant_true_, isolate()->heap()->true_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | |
| 533 | HConstant* HGraph::GetConstantFalse() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 534 | return GetConstant(&constant_false_, isolate()->heap()->false_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 535 | } |
| 536 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 537 | |
| 538 | HConstant* HGraph::GetConstantHole() { |
| 539 | return GetConstant(&constant_hole_, isolate()->heap()->the_hole_value()); |
| 540 | } |
| 541 | |
| 542 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 543 | HGraphBuilder::HGraphBuilder(CompilationInfo* info, |
| 544 | TypeFeedbackOracle* oracle) |
| 545 | : function_state_(NULL), |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 546 | initial_function_state_(this, info, oracle, false), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 547 | ast_context_(NULL), |
| 548 | break_scope_(NULL), |
| 549 | graph_(NULL), |
| 550 | current_block_(NULL), |
| 551 | inlined_count_(0), |
| 552 | zone_(info->isolate()->zone()), |
| 553 | inline_bailout_(false) { |
| 554 | // This is not initialized in the initializer list because the |
| 555 | // constructor for the initial state relies on function_state_ == NULL |
| 556 | // to know it's the initial state. |
| 557 | function_state_= &initial_function_state_; |
| 558 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 559 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 560 | HBasicBlock* HGraphBuilder::CreateJoin(HBasicBlock* first, |
| 561 | HBasicBlock* second, |
| 562 | int join_id) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 563 | if (first == NULL) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 564 | return second; |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 565 | } else if (second == NULL) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 566 | return first; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 567 | } else { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 568 | HBasicBlock* join_block = graph_->CreateBasicBlock(); |
| 569 | first->Goto(join_block); |
| 570 | second->Goto(join_block); |
| 571 | join_block->SetJoinId(join_id); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 572 | return join_block; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
| 576 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 577 | HBasicBlock* HGraphBuilder::JoinContinue(IterationStatement* statement, |
| 578 | HBasicBlock* exit_block, |
| 579 | HBasicBlock* continue_block) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 580 | if (continue_block != NULL) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 581 | if (exit_block != NULL) exit_block->Goto(continue_block); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 582 | continue_block->SetJoinId(statement->ContinueId()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 583 | return continue_block; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 584 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 585 | return exit_block; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 589 | HBasicBlock* HGraphBuilder::CreateLoop(IterationStatement* statement, |
| 590 | HBasicBlock* loop_entry, |
| 591 | HBasicBlock* body_exit, |
| 592 | HBasicBlock* loop_successor, |
| 593 | HBasicBlock* break_block) { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 594 | if (body_exit != NULL) body_exit->Goto(loop_entry); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 595 | loop_entry->PostProcessLoopHeader(statement); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 596 | if (break_block != NULL) { |
| 597 | if (loop_successor != NULL) loop_successor->Goto(break_block); |
| 598 | break_block->SetJoinId(statement->ExitId()); |
| 599 | return break_block; |
| 600 | } |
| 601 | return loop_successor; |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 605 | void HBasicBlock::FinishExit(HControlInstruction* instruction) { |
| 606 | Finish(instruction); |
| 607 | ClearEnvironment(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 611 | HGraph::HGraph(CompilationInfo* info) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 612 | : isolate_(info->isolate()), |
| 613 | next_block_id_(0), |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 614 | entry_block_(NULL), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 615 | blocks_(8), |
| 616 | values_(16), |
| 617 | phi_list_(NULL) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 618 | start_environment_ = |
| 619 | new(zone()) HEnvironment(NULL, info->scope(), info->closure()); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 620 | start_environment_->set_ast_id(AstNode::kFunctionEntryId); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 621 | entry_block_ = CreateBasicBlock(); |
| 622 | entry_block_->SetInitialEnvironment(start_environment_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 626 | Handle<Code> HGraph::Compile(CompilationInfo* info) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 627 | int values = GetMaximumValueID(); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame^] | 628 | if (values > LUnallocated::kMaxVirtualRegisters) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 629 | if (FLAG_trace_bailout) { |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame^] | 630 | PrintF("Not enough virtual registers for (values).\n"); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 631 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 632 | return Handle<Code>::null(); |
| 633 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 634 | LAllocator allocator(values, this); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 635 | LChunkBuilder builder(info, this, &allocator); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 636 | LChunk* chunk = builder.Build(); |
| 637 | if (chunk == NULL) return Handle<Code>::null(); |
| 638 | |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame^] | 639 | if (!allocator.Allocate(chunk)) { |
| 640 | if (FLAG_trace_bailout) { |
| 641 | PrintF("Not enough virtual registers (regalloc).\n"); |
| 642 | } |
| 643 | return Handle<Code>::null(); |
| 644 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 645 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 646 | MacroAssembler assembler(info->isolate(), NULL, 0); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 647 | LCodeGen generator(chunk, &assembler, info); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 648 | |
| 649 | if (FLAG_eliminate_empty_blocks) { |
| 650 | chunk->MarkEmptyBlocks(); |
| 651 | } |
| 652 | |
| 653 | if (generator.GenerateCode()) { |
| 654 | if (FLAG_trace_codegen) { |
| 655 | PrintF("Crankshaft Compiler - "); |
| 656 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 657 | CodeGenerator::MakeCodePrologue(info); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 658 | Code::Flags flags = Code::ComputeFlags(Code::OPTIMIZED_FUNCTION); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 659 | Handle<Code> code = |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 660 | CodeGenerator::MakeCodeEpilogue(&assembler, flags, info); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 661 | generator.FinishCode(code); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 662 | CodeGenerator::PrintCode(code, info); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 663 | return code; |
| 664 | } |
| 665 | return Handle<Code>::null(); |
| 666 | } |
| 667 | |
| 668 | |
| 669 | HBasicBlock* HGraph::CreateBasicBlock() { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 670 | HBasicBlock* result = new(zone()) HBasicBlock(this); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 671 | blocks_.Add(result); |
| 672 | return result; |
| 673 | } |
| 674 | |
| 675 | |
| 676 | void HGraph::Canonicalize() { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 677 | if (!FLAG_use_canonicalizing) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 678 | HPhase phase("Canonicalize", this); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 679 | for (int i = 0; i < blocks()->length(); ++i) { |
| 680 | HInstruction* instr = blocks()->at(i)->first(); |
| 681 | while (instr != NULL) { |
| 682 | HValue* value = instr->Canonicalize(); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 683 | if (value != instr) instr->DeleteAndReplaceWith(value); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 684 | instr = instr->next(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | |
| 690 | void HGraph::OrderBlocks() { |
| 691 | HPhase phase("Block ordering"); |
| 692 | BitVector visited(blocks_.length()); |
| 693 | |
| 694 | ZoneList<HBasicBlock*> reverse_result(8); |
| 695 | HBasicBlock* start = blocks_[0]; |
| 696 | Postorder(start, &visited, &reverse_result, NULL); |
| 697 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 698 | blocks_.Rewind(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 699 | int index = 0; |
| 700 | for (int i = reverse_result.length() - 1; i >= 0; --i) { |
| 701 | HBasicBlock* b = reverse_result[i]; |
| 702 | blocks_.Add(b); |
| 703 | b->set_block_id(index++); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | |
| 708 | void HGraph::PostorderLoopBlocks(HLoopInformation* loop, |
| 709 | BitVector* visited, |
| 710 | ZoneList<HBasicBlock*>* order, |
| 711 | HBasicBlock* loop_header) { |
| 712 | for (int i = 0; i < loop->blocks()->length(); ++i) { |
| 713 | HBasicBlock* b = loop->blocks()->at(i); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 714 | for (HSuccessorIterator it(b->end()); !it.Done(); it.Advance()) { |
| 715 | Postorder(it.Current(), visited, order, loop_header); |
| 716 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 717 | if (b->IsLoopHeader() && b != loop->loop_header()) { |
| 718 | PostorderLoopBlocks(b->loop_information(), visited, order, loop_header); |
| 719 | } |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | |
| 724 | void HGraph::Postorder(HBasicBlock* block, |
| 725 | BitVector* visited, |
| 726 | ZoneList<HBasicBlock*>* order, |
| 727 | HBasicBlock* loop_header) { |
| 728 | if (block == NULL || visited->Contains(block->block_id())) return; |
| 729 | if (block->parent_loop_header() != loop_header) return; |
| 730 | visited->Add(block->block_id()); |
| 731 | if (block->IsLoopHeader()) { |
| 732 | PostorderLoopBlocks(block->loop_information(), visited, order, loop_header); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 733 | for (HSuccessorIterator it(block->end()); !it.Done(); it.Advance()) { |
| 734 | Postorder(it.Current(), visited, order, block); |
| 735 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 736 | } else { |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 737 | ASSERT(block->IsFinished()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 738 | for (HSuccessorIterator it(block->end()); !it.Done(); it.Advance()) { |
| 739 | Postorder(it.Current(), visited, order, loop_header); |
| 740 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 741 | } |
| 742 | ASSERT(block->end()->FirstSuccessor() == NULL || |
| 743 | order->Contains(block->end()->FirstSuccessor()) || |
| 744 | block->end()->FirstSuccessor()->IsLoopHeader()); |
| 745 | ASSERT(block->end()->SecondSuccessor() == NULL || |
| 746 | order->Contains(block->end()->SecondSuccessor()) || |
| 747 | block->end()->SecondSuccessor()->IsLoopHeader()); |
| 748 | order->Add(block); |
| 749 | } |
| 750 | |
| 751 | |
| 752 | void HGraph::AssignDominators() { |
| 753 | HPhase phase("Assign dominators", this); |
| 754 | for (int i = 0; i < blocks_.length(); ++i) { |
| 755 | if (blocks_[i]->IsLoopHeader()) { |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 756 | // Only the first predecessor of a loop header is from outside the loop. |
| 757 | // All others are back edges, and thus cannot dominate the loop header. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 758 | blocks_[i]->AssignCommonDominator(blocks_[i]->predecessors()->first()); |
| 759 | } else { |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 760 | for (int j = blocks_[i]->predecessors()->length() - 1; j >= 0; --j) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 761 | blocks_[i]->AssignCommonDominator(blocks_[i]->predecessors()->at(j)); |
| 762 | } |
| 763 | } |
| 764 | } |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 765 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 766 | |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 767 | // Mark all blocks that are dominated by an unconditional soft deoptimize to |
| 768 | // prevent code motion across those blocks. |
| 769 | void HGraph::PropagateDeoptimizingMark() { |
| 770 | HPhase phase("Propagate deoptimizing mark", this); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 771 | MarkAsDeoptimizingRecursively(entry_block()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 772 | } |
| 773 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 774 | void HGraph::MarkAsDeoptimizingRecursively(HBasicBlock* block) { |
| 775 | for (int i = 0; i < block->dominated_blocks()->length(); ++i) { |
| 776 | HBasicBlock* dominated = block->dominated_blocks()->at(i); |
| 777 | if (block->IsDeoptimizing()) dominated->MarkAsDeoptimizing(); |
| 778 | MarkAsDeoptimizingRecursively(dominated); |
| 779 | } |
| 780 | } |
| 781 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 782 | void HGraph::EliminateRedundantPhis() { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 783 | HPhase phase("Redundant phi elimination", this); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 784 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 785 | // Worklist of phis that can potentially be eliminated. Initialized with |
| 786 | // all phi nodes. When elimination of a phi node modifies another phi node |
| 787 | // the modified phi node is added to the worklist. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 788 | ZoneList<HPhi*> worklist(blocks_.length()); |
| 789 | for (int i = 0; i < blocks_.length(); ++i) { |
| 790 | worklist.AddAll(*blocks_[i]->phis()); |
| 791 | } |
| 792 | |
| 793 | while (!worklist.is_empty()) { |
| 794 | HPhi* phi = worklist.RemoveLast(); |
| 795 | HBasicBlock* block = phi->block(); |
| 796 | |
| 797 | // Skip phi node if it was already replaced. |
| 798 | if (block == NULL) continue; |
| 799 | |
| 800 | // Get replacement value if phi is redundant. |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 801 | HValue* replacement = phi->GetRedundantReplacement(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 802 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 803 | if (replacement != NULL) { |
| 804 | // Iterate through the uses and replace them all. |
| 805 | for (HUseIterator it(phi->uses()); !it.Done(); it.Advance()) { |
| 806 | HValue* value = it.value(); |
| 807 | value->SetOperandAt(it.index(), replacement); |
| 808 | if (value->IsPhi()) worklist.Add(HPhi::cast(value)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 809 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 810 | block->RemovePhi(phi); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | |
| 816 | void HGraph::EliminateUnreachablePhis() { |
| 817 | HPhase phase("Unreachable phi elimination", this); |
| 818 | |
| 819 | // Initialize worklist. |
| 820 | ZoneList<HPhi*> phi_list(blocks_.length()); |
| 821 | ZoneList<HPhi*> worklist(blocks_.length()); |
| 822 | for (int i = 0; i < blocks_.length(); ++i) { |
| 823 | for (int j = 0; j < blocks_[i]->phis()->length(); j++) { |
| 824 | HPhi* phi = blocks_[i]->phis()->at(j); |
| 825 | phi_list.Add(phi); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 826 | // We can't eliminate phis in the receiver position in the environment |
| 827 | // because in case of throwing an error we need this value to |
| 828 | // construct a stack trace. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 829 | if (phi->HasRealUses() || phi->IsReceiver()) { |
| 830 | phi->set_is_live(true); |
| 831 | worklist.Add(phi); |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | // Iteratively mark live phis. |
| 837 | while (!worklist.is_empty()) { |
| 838 | HPhi* phi = worklist.RemoveLast(); |
| 839 | for (int i = 0; i < phi->OperandCount(); i++) { |
| 840 | HValue* operand = phi->OperandAt(i); |
| 841 | if (operand->IsPhi() && !HPhi::cast(operand)->is_live()) { |
| 842 | HPhi::cast(operand)->set_is_live(true); |
| 843 | worklist.Add(HPhi::cast(operand)); |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | // Remove unreachable phis. |
| 849 | for (int i = 0; i < phi_list.length(); i++) { |
| 850 | HPhi* phi = phi_list[i]; |
| 851 | if (!phi->is_live()) { |
| 852 | HBasicBlock* block = phi->block(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 853 | block->RemovePhi(phi); |
| 854 | block->RecordDeletedPhi(phi->merged_index()); |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 860 | bool HGraph::CheckArgumentsPhiUses() { |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 861 | int block_count = blocks_.length(); |
| 862 | for (int i = 0; i < block_count; ++i) { |
| 863 | for (int j = 0; j < blocks_[i]->phis()->length(); ++j) { |
| 864 | HPhi* phi = blocks_[i]->phis()->at(j); |
| 865 | // We don't support phi uses of arguments for now. |
| 866 | if (phi->CheckFlag(HValue::kIsArguments)) return false; |
| 867 | } |
| 868 | } |
| 869 | return true; |
| 870 | } |
| 871 | |
| 872 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 873 | bool HGraph::CheckConstPhiUses() { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 874 | int block_count = blocks_.length(); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 875 | for (int i = 0; i < block_count; ++i) { |
| 876 | for (int j = 0; j < blocks_[i]->phis()->length(); ++j) { |
| 877 | HPhi* phi = blocks_[i]->phis()->at(j); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 878 | // Check for the hole value (from an uninitialized const). |
| 879 | for (int k = 0; k < phi->OperandCount(); k++) { |
| 880 | if (phi->OperandAt(k) == GetConstantHole()) return false; |
| 881 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | return true; |
| 885 | } |
| 886 | |
| 887 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 888 | void HGraph::CollectPhis() { |
| 889 | int block_count = blocks_.length(); |
| 890 | phi_list_ = new ZoneList<HPhi*>(block_count); |
| 891 | for (int i = 0; i < block_count; ++i) { |
| 892 | for (int j = 0; j < blocks_[i]->phis()->length(); ++j) { |
| 893 | HPhi* phi = blocks_[i]->phis()->at(j); |
| 894 | phi_list_->Add(phi); |
| 895 | } |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 900 | void HGraph::InferTypes(ZoneList<HValue*>* worklist) { |
| 901 | BitVector in_worklist(GetMaximumValueID()); |
| 902 | for (int i = 0; i < worklist->length(); ++i) { |
| 903 | ASSERT(!in_worklist.Contains(worklist->at(i)->id())); |
| 904 | in_worklist.Add(worklist->at(i)->id()); |
| 905 | } |
| 906 | |
| 907 | while (!worklist->is_empty()) { |
| 908 | HValue* current = worklist->RemoveLast(); |
| 909 | in_worklist.Remove(current->id()); |
| 910 | if (current->UpdateInferredType()) { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 911 | for (HUseIterator it(current->uses()); !it.Done(); it.Advance()) { |
| 912 | HValue* use = it.value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 913 | if (!in_worklist.Contains(use->id())) { |
| 914 | in_worklist.Add(use->id()); |
| 915 | worklist->Add(use); |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | |
| 923 | class HRangeAnalysis BASE_EMBEDDED { |
| 924 | public: |
| 925 | explicit HRangeAnalysis(HGraph* graph) : graph_(graph), changed_ranges_(16) {} |
| 926 | |
| 927 | void Analyze(); |
| 928 | |
| 929 | private: |
| 930 | void TraceRange(const char* msg, ...); |
| 931 | void Analyze(HBasicBlock* block); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 932 | void InferControlFlowRange(HCompareIDAndBranch* test, HBasicBlock* dest); |
| 933 | void UpdateControlFlowRange(Token::Value op, HValue* value, HValue* other); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 934 | void InferRange(HValue* value); |
| 935 | void RollBackTo(int index); |
| 936 | void AddRange(HValue* value, Range* range); |
| 937 | |
| 938 | HGraph* graph_; |
| 939 | ZoneList<HValue*> changed_ranges_; |
| 940 | }; |
| 941 | |
| 942 | |
| 943 | void HRangeAnalysis::TraceRange(const char* msg, ...) { |
| 944 | if (FLAG_trace_range) { |
| 945 | va_list arguments; |
| 946 | va_start(arguments, msg); |
| 947 | OS::VPrint(msg, arguments); |
| 948 | va_end(arguments); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | |
| 953 | void HRangeAnalysis::Analyze() { |
| 954 | HPhase phase("Range analysis", graph_); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 955 | Analyze(graph_->entry_block()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | |
| 959 | void HRangeAnalysis::Analyze(HBasicBlock* block) { |
| 960 | TraceRange("Analyzing block B%d\n", block->block_id()); |
| 961 | |
| 962 | int last_changed_range = changed_ranges_.length() - 1; |
| 963 | |
| 964 | // Infer range based on control flow. |
| 965 | if (block->predecessors()->length() == 1) { |
| 966 | HBasicBlock* pred = block->predecessors()->first(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 967 | if (pred->end()->IsCompareIDAndBranch()) { |
| 968 | InferControlFlowRange(HCompareIDAndBranch::cast(pred->end()), block); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 969 | } |
| 970 | } |
| 971 | |
| 972 | // Process phi instructions. |
| 973 | for (int i = 0; i < block->phis()->length(); ++i) { |
| 974 | HPhi* phi = block->phis()->at(i); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 975 | InferRange(phi); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | // Go through all instructions of the current block. |
| 979 | HInstruction* instr = block->first(); |
| 980 | while (instr != block->end()) { |
| 981 | InferRange(instr); |
| 982 | instr = instr->next(); |
| 983 | } |
| 984 | |
| 985 | // Continue analysis in all dominated blocks. |
| 986 | for (int i = 0; i < block->dominated_blocks()->length(); ++i) { |
| 987 | Analyze(block->dominated_blocks()->at(i)); |
| 988 | } |
| 989 | |
| 990 | RollBackTo(last_changed_range); |
| 991 | } |
| 992 | |
| 993 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 994 | void HRangeAnalysis::InferControlFlowRange(HCompareIDAndBranch* test, |
| 995 | HBasicBlock* dest) { |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 996 | ASSERT((test->FirstSuccessor() == dest) == (test->SecondSuccessor() != dest)); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 997 | if (test->GetInputRepresentation().IsInteger32()) { |
| 998 | Token::Value op = test->token(); |
| 999 | if (test->SecondSuccessor() == dest) { |
| 1000 | op = Token::NegateCompareOp(op); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1001 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1002 | Token::Value inverted_op = Token::InvertCompareOp(op); |
| 1003 | UpdateControlFlowRange(op, test->left(), test->right()); |
| 1004 | UpdateControlFlowRange(inverted_op, test->right(), test->left()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | // We know that value [op] other. Use this information to update the range on |
| 1010 | // value. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1011 | void HRangeAnalysis::UpdateControlFlowRange(Token::Value op, |
| 1012 | HValue* value, |
| 1013 | HValue* other) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1014 | Range temp_range; |
| 1015 | Range* range = other->range() != NULL ? other->range() : &temp_range; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1016 | Range* new_range = NULL; |
| 1017 | |
| 1018 | TraceRange("Control flow range infer %d %s %d\n", |
| 1019 | value->id(), |
| 1020 | Token::Name(op), |
| 1021 | other->id()); |
| 1022 | |
| 1023 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
| 1024 | // The same range has to apply for value. |
| 1025 | new_range = range->Copy(); |
| 1026 | } else if (op == Token::LT || op == Token::LTE) { |
| 1027 | new_range = range->CopyClearLower(); |
| 1028 | if (op == Token::LT) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1029 | new_range->AddConstant(-1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1030 | } |
| 1031 | } else if (op == Token::GT || op == Token::GTE) { |
| 1032 | new_range = range->CopyClearUpper(); |
| 1033 | if (op == Token::GT) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1034 | new_range->AddConstant(1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | if (new_range != NULL && !new_range->IsMostGeneric()) { |
| 1039 | AddRange(value, new_range); |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1044 | void HRangeAnalysis::InferRange(HValue* value) { |
| 1045 | ASSERT(!value->HasRange()); |
| 1046 | if (!value->representation().IsNone()) { |
| 1047 | value->ComputeInitialRange(); |
| 1048 | Range* range = value->range(); |
| 1049 | TraceRange("Initial inferred range of %d (%s) set to [%d,%d]\n", |
| 1050 | value->id(), |
| 1051 | value->Mnemonic(), |
| 1052 | range->lower(), |
| 1053 | range->upper()); |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | |
| 1058 | void HRangeAnalysis::RollBackTo(int index) { |
| 1059 | for (int i = index + 1; i < changed_ranges_.length(); ++i) { |
| 1060 | changed_ranges_[i]->RemoveLastAddedRange(); |
| 1061 | } |
| 1062 | changed_ranges_.Rewind(index + 1); |
| 1063 | } |
| 1064 | |
| 1065 | |
| 1066 | void HRangeAnalysis::AddRange(HValue* value, Range* range) { |
| 1067 | Range* original_range = value->range(); |
| 1068 | value->AddNewRange(range); |
| 1069 | changed_ranges_.Add(value); |
| 1070 | Range* new_range = value->range(); |
| 1071 | TraceRange("Updated range of %d set to [%d,%d]\n", |
| 1072 | value->id(), |
| 1073 | new_range->lower(), |
| 1074 | new_range->upper()); |
| 1075 | if (original_range != NULL) { |
| 1076 | TraceRange("Original range was [%d,%d]\n", |
| 1077 | original_range->lower(), |
| 1078 | original_range->upper()); |
| 1079 | } |
| 1080 | TraceRange("New information was [%d,%d]\n", |
| 1081 | range->lower(), |
| 1082 | range->upper()); |
| 1083 | } |
| 1084 | |
| 1085 | |
| 1086 | void TraceGVN(const char* msg, ...) { |
| 1087 | if (FLAG_trace_gvn) { |
| 1088 | va_list arguments; |
| 1089 | va_start(arguments, msg); |
| 1090 | OS::VPrint(msg, arguments); |
| 1091 | va_end(arguments); |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1096 | HValueMap::HValueMap(Zone* zone, const HValueMap* other) |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1097 | : array_size_(other->array_size_), |
| 1098 | lists_size_(other->lists_size_), |
| 1099 | count_(other->count_), |
| 1100 | present_flags_(other->present_flags_), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1101 | array_(zone->NewArray<HValueMapListElement>(other->array_size_)), |
| 1102 | lists_(zone->NewArray<HValueMapListElement>(other->lists_size_)), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1103 | free_list_head_(other->free_list_head_) { |
| 1104 | memcpy(array_, other->array_, array_size_ * sizeof(HValueMapListElement)); |
| 1105 | memcpy(lists_, other->lists_, lists_size_ * sizeof(HValueMapListElement)); |
| 1106 | } |
| 1107 | |
| 1108 | |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1109 | void HValueMap::Kill(GVNFlagSet flags) { |
| 1110 | GVNFlagSet depends_flags = HValue::ConvertChangesToDependsFlags(flags); |
| 1111 | if (!present_flags_.ContainsAnyOf(depends_flags)) return; |
| 1112 | present_flags_.RemoveAll(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1113 | for (int i = 0; i < array_size_; ++i) { |
| 1114 | HValue* value = array_[i].value; |
| 1115 | if (value != NULL) { |
| 1116 | // Clear list of collisions first, so we know if it becomes empty. |
| 1117 | int kept = kNil; // List of kept elements. |
| 1118 | int next; |
| 1119 | for (int current = array_[i].next; current != kNil; current = next) { |
| 1120 | next = lists_[current].next; |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1121 | HValue* value = lists_[current].value; |
| 1122 | if (value->gvn_flags().ContainsAnyOf(depends_flags)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1123 | // Drop it. |
| 1124 | count_--; |
| 1125 | lists_[current].next = free_list_head_; |
| 1126 | free_list_head_ = current; |
| 1127 | } else { |
| 1128 | // Keep it. |
| 1129 | lists_[current].next = kept; |
| 1130 | kept = current; |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1131 | present_flags_.Add(value->gvn_flags()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | array_[i].next = kept; |
| 1135 | |
| 1136 | // Now possibly drop directly indexed element. |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1137 | value = array_[i].value; |
| 1138 | if (value->gvn_flags().ContainsAnyOf(depends_flags)) { // Drop it. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1139 | count_--; |
| 1140 | int head = array_[i].next; |
| 1141 | if (head == kNil) { |
| 1142 | array_[i].value = NULL; |
| 1143 | } else { |
| 1144 | array_[i].value = lists_[head].value; |
| 1145 | array_[i].next = lists_[head].next; |
| 1146 | lists_[head].next = free_list_head_; |
| 1147 | free_list_head_ = head; |
| 1148 | } |
| 1149 | } else { |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1150 | present_flags_.Add(value->gvn_flags()); // Keep it. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | |
| 1157 | HValue* HValueMap::Lookup(HValue* value) const { |
| 1158 | uint32_t hash = static_cast<uint32_t>(value->Hashcode()); |
| 1159 | uint32_t pos = Bound(hash); |
| 1160 | if (array_[pos].value != NULL) { |
| 1161 | if (array_[pos].value->Equals(value)) return array_[pos].value; |
| 1162 | int next = array_[pos].next; |
| 1163 | while (next != kNil) { |
| 1164 | if (lists_[next].value->Equals(value)) return lists_[next].value; |
| 1165 | next = lists_[next].next; |
| 1166 | } |
| 1167 | } |
| 1168 | return NULL; |
| 1169 | } |
| 1170 | |
| 1171 | |
| 1172 | void HValueMap::Resize(int new_size) { |
| 1173 | ASSERT(new_size > count_); |
| 1174 | // Hashing the values into the new array has no more collisions than in the |
| 1175 | // old hash map, so we can use the existing lists_ array, if we are careful. |
| 1176 | |
| 1177 | // Make sure we have at least one free element. |
| 1178 | if (free_list_head_ == kNil) { |
| 1179 | ResizeLists(lists_size_ << 1); |
| 1180 | } |
| 1181 | |
| 1182 | HValueMapListElement* new_array = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1183 | ZONE->NewArray<HValueMapListElement>(new_size); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1184 | memset(new_array, 0, sizeof(HValueMapListElement) * new_size); |
| 1185 | |
| 1186 | HValueMapListElement* old_array = array_; |
| 1187 | int old_size = array_size_; |
| 1188 | |
| 1189 | int old_count = count_; |
| 1190 | count_ = 0; |
| 1191 | // Do not modify present_flags_. It is currently correct. |
| 1192 | array_size_ = new_size; |
| 1193 | array_ = new_array; |
| 1194 | |
| 1195 | if (old_array != NULL) { |
| 1196 | // Iterate over all the elements in lists, rehashing them. |
| 1197 | for (int i = 0; i < old_size; ++i) { |
| 1198 | if (old_array[i].value != NULL) { |
| 1199 | int current = old_array[i].next; |
| 1200 | while (current != kNil) { |
| 1201 | Insert(lists_[current].value); |
| 1202 | int next = lists_[current].next; |
| 1203 | lists_[current].next = free_list_head_; |
| 1204 | free_list_head_ = current; |
| 1205 | current = next; |
| 1206 | } |
| 1207 | // Rehash the directly stored value. |
| 1208 | Insert(old_array[i].value); |
| 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | USE(old_count); |
| 1213 | ASSERT(count_ == old_count); |
| 1214 | } |
| 1215 | |
| 1216 | |
| 1217 | void HValueMap::ResizeLists(int new_size) { |
| 1218 | ASSERT(new_size > lists_size_); |
| 1219 | |
| 1220 | HValueMapListElement* new_lists = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1221 | ZONE->NewArray<HValueMapListElement>(new_size); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1222 | memset(new_lists, 0, sizeof(HValueMapListElement) * new_size); |
| 1223 | |
| 1224 | HValueMapListElement* old_lists = lists_; |
| 1225 | int old_size = lists_size_; |
| 1226 | |
| 1227 | lists_size_ = new_size; |
| 1228 | lists_ = new_lists; |
| 1229 | |
| 1230 | if (old_lists != NULL) { |
| 1231 | memcpy(lists_, old_lists, old_size * sizeof(HValueMapListElement)); |
| 1232 | } |
| 1233 | for (int i = old_size; i < lists_size_; ++i) { |
| 1234 | lists_[i].next = free_list_head_; |
| 1235 | free_list_head_ = i; |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | |
| 1240 | void HValueMap::Insert(HValue* value) { |
| 1241 | ASSERT(value != NULL); |
| 1242 | // Resizing when half of the hashtable is filled up. |
| 1243 | if (count_ >= array_size_ >> 1) Resize(array_size_ << 1); |
| 1244 | ASSERT(count_ < array_size_); |
| 1245 | count_++; |
| 1246 | uint32_t pos = Bound(static_cast<uint32_t>(value->Hashcode())); |
| 1247 | if (array_[pos].value == NULL) { |
| 1248 | array_[pos].value = value; |
| 1249 | array_[pos].next = kNil; |
| 1250 | } else { |
| 1251 | if (free_list_head_ == kNil) { |
| 1252 | ResizeLists(lists_size_ << 1); |
| 1253 | } |
| 1254 | int new_element_pos = free_list_head_; |
| 1255 | ASSERT(new_element_pos != kNil); |
| 1256 | free_list_head_ = lists_[free_list_head_].next; |
| 1257 | lists_[new_element_pos].value = value; |
| 1258 | lists_[new_element_pos].next = array_[pos].next; |
| 1259 | ASSERT(array_[pos].next == kNil || lists_[array_[pos].next].value != NULL); |
| 1260 | array_[pos].next = new_element_pos; |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | |
| 1265 | class HStackCheckEliminator BASE_EMBEDDED { |
| 1266 | public: |
| 1267 | explicit HStackCheckEliminator(HGraph* graph) : graph_(graph) { } |
| 1268 | |
| 1269 | void Process(); |
| 1270 | |
| 1271 | private: |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1272 | HGraph* graph_; |
| 1273 | }; |
| 1274 | |
| 1275 | |
| 1276 | void HStackCheckEliminator::Process() { |
| 1277 | // For each loop block walk the dominator tree from the backwards branch to |
| 1278 | // the loop header. If a call instruction is encountered the backwards branch |
| 1279 | // is dominated by a call and the stack check in the backwards branch can be |
| 1280 | // removed. |
| 1281 | for (int i = 0; i < graph_->blocks()->length(); i++) { |
| 1282 | HBasicBlock* block = graph_->blocks()->at(i); |
| 1283 | if (block->IsLoopHeader()) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1284 | HBasicBlock* back_edge = block->loop_information()->GetLastBackEdge(); |
| 1285 | HBasicBlock* dominator = back_edge; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1286 | while (true) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1287 | HInstruction* instr = dominator->first(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1288 | while (instr != NULL) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1289 | if (instr->IsCall()) { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1290 | block->loop_information()->stack_check()->Eliminate(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1291 | break; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1292 | } |
| 1293 | instr = instr->next(); |
| 1294 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1295 | |
| 1296 | // Done when the loop header is processed. |
| 1297 | if (dominator == block) break; |
| 1298 | |
| 1299 | // Move up the dominator tree. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1300 | dominator = dominator->dominator(); |
| 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1307 | // Simple sparse set with O(1) add, contains, and clear. |
| 1308 | class SparseSet { |
| 1309 | public: |
| 1310 | SparseSet(Zone* zone, int capacity) |
| 1311 | : capacity_(capacity), |
| 1312 | length_(0), |
| 1313 | dense_(zone->NewArray<int>(capacity)), |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1314 | sparse_(zone->NewArray<int>(capacity)) { |
| 1315 | #ifndef NVALGRIND |
| 1316 | // Initialize the sparse array to make valgrind happy. |
| 1317 | memset(sparse_, 0, sizeof(sparse_[0]) * capacity); |
| 1318 | #endif |
| 1319 | } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1320 | |
| 1321 | bool Contains(int n) const { |
| 1322 | ASSERT(0 <= n && n < capacity_); |
| 1323 | int d = sparse_[n]; |
| 1324 | return 0 <= d && d < length_ && dense_[d] == n; |
| 1325 | } |
| 1326 | |
| 1327 | bool Add(int n) { |
| 1328 | if (Contains(n)) return false; |
| 1329 | dense_[length_] = n; |
| 1330 | sparse_[n] = length_; |
| 1331 | ++length_; |
| 1332 | return true; |
| 1333 | } |
| 1334 | |
| 1335 | void Clear() { length_ = 0; } |
| 1336 | |
| 1337 | private: |
| 1338 | int capacity_; |
| 1339 | int length_; |
| 1340 | int* dense_; |
| 1341 | int* sparse_; |
| 1342 | |
| 1343 | DISALLOW_COPY_AND_ASSIGN(SparseSet); |
| 1344 | }; |
| 1345 | |
| 1346 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1347 | class HGlobalValueNumberer BASE_EMBEDDED { |
| 1348 | public: |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1349 | explicit HGlobalValueNumberer(HGraph* graph, CompilationInfo* info) |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1350 | : graph_(graph), |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1351 | info_(info), |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1352 | removed_side_effects_(false), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1353 | block_side_effects_(graph->blocks()->length()), |
| 1354 | loop_side_effects_(graph->blocks()->length()), |
| 1355 | visited_on_paths_(graph->zone(), graph->blocks()->length()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1356 | ASSERT(info->isolate()->heap()->allow_allocation(false)); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1357 | block_side_effects_.AddBlock(GVNFlagSet(), graph_->blocks()->length()); |
| 1358 | loop_side_effects_.AddBlock(GVNFlagSet(), graph_->blocks()->length()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1359 | } |
| 1360 | ~HGlobalValueNumberer() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1361 | ASSERT(!info_->isolate()->heap()->allow_allocation(true)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1364 | // Returns true if values with side effects are removed. |
| 1365 | bool Analyze(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1366 | |
| 1367 | private: |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1368 | GVNFlagSet CollectSideEffectsOnPathsToDominatedBlock( |
| 1369 | HBasicBlock* dominator, |
| 1370 | HBasicBlock* dominated); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1371 | void AnalyzeBlock(HBasicBlock* block, HValueMap* map); |
| 1372 | void ComputeBlockSideEffects(); |
| 1373 | void LoopInvariantCodeMotion(); |
| 1374 | void ProcessLoopBlock(HBasicBlock* block, |
| 1375 | HBasicBlock* before_loop, |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1376 | GVNFlagSet loop_kills); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1377 | bool AllowCodeMotion(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1378 | bool ShouldMove(HInstruction* instr, HBasicBlock* loop_header); |
| 1379 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1380 | HGraph* graph() { return graph_; } |
| 1381 | CompilationInfo* info() { return info_; } |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1382 | Zone* zone() { return graph_->zone(); } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1383 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1384 | HGraph* graph_; |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1385 | CompilationInfo* info_; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1386 | bool removed_side_effects_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1387 | |
| 1388 | // A map of block IDs to their side effects. |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1389 | ZoneList<GVNFlagSet> block_side_effects_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1390 | |
| 1391 | // A map of loop header block IDs to their loop's side effects. |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1392 | ZoneList<GVNFlagSet> loop_side_effects_; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1393 | |
| 1394 | // Used when collecting side effects on paths from dominator to |
| 1395 | // dominated. |
| 1396 | SparseSet visited_on_paths_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1397 | }; |
| 1398 | |
| 1399 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1400 | bool HGlobalValueNumberer::Analyze() { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1401 | ComputeBlockSideEffects(); |
| 1402 | if (FLAG_loop_invariant_code_motion) { |
| 1403 | LoopInvariantCodeMotion(); |
| 1404 | } |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1405 | HValueMap* map = new(zone()) HValueMap(); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1406 | AnalyzeBlock(graph_->entry_block(), map); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1407 | return removed_side_effects_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | |
| 1411 | void HGlobalValueNumberer::ComputeBlockSideEffects() { |
| 1412 | for (int i = graph_->blocks()->length() - 1; i >= 0; --i) { |
| 1413 | // Compute side effects for the block. |
| 1414 | HBasicBlock* block = graph_->blocks()->at(i); |
| 1415 | HInstruction* instr = block->first(); |
| 1416 | int id = block->block_id(); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1417 | GVNFlagSet side_effects; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1418 | while (instr != NULL) { |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1419 | side_effects.Add(instr->ChangesFlags()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1420 | instr = instr->next(); |
| 1421 | } |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1422 | block_side_effects_[id].Add(side_effects); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1423 | |
| 1424 | // Loop headers are part of their loop. |
| 1425 | if (block->IsLoopHeader()) { |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1426 | loop_side_effects_[id].Add(side_effects); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | // Propagate loop side effects upwards. |
| 1430 | if (block->HasParentLoopHeader()) { |
| 1431 | int header_id = block->parent_loop_header()->block_id(); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1432 | loop_side_effects_[header_id].Add(block->IsLoopHeader() |
| 1433 | ? loop_side_effects_[id] |
| 1434 | : side_effects); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1435 | } |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | |
| 1440 | void HGlobalValueNumberer::LoopInvariantCodeMotion() { |
| 1441 | for (int i = graph_->blocks()->length() - 1; i >= 0; --i) { |
| 1442 | HBasicBlock* block = graph_->blocks()->at(i); |
| 1443 | if (block->IsLoopHeader()) { |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1444 | GVNFlagSet side_effects = loop_side_effects_[block->block_id()]; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1445 | TraceGVN("Try loop invariant motion for block B%d effects=0x%x\n", |
| 1446 | block->block_id(), |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1447 | side_effects.ToIntegral()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1448 | |
| 1449 | HBasicBlock* last = block->loop_information()->GetLastBackEdge(); |
| 1450 | for (int j = block->block_id(); j <= last->block_id(); ++j) { |
| 1451 | ProcessLoopBlock(graph_->blocks()->at(j), block, side_effects); |
| 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | |
| 1458 | void HGlobalValueNumberer::ProcessLoopBlock(HBasicBlock* block, |
| 1459 | HBasicBlock* loop_header, |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1460 | GVNFlagSet loop_kills) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1461 | HBasicBlock* pre_header = loop_header->predecessors()->at(0); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1462 | GVNFlagSet depends_flags = HValue::ConvertChangesToDependsFlags(loop_kills); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1463 | TraceGVN("Loop invariant motion for B%d depends_flags=0x%x\n", |
| 1464 | block->block_id(), |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1465 | depends_flags.ToIntegral()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1466 | HInstruction* instr = block->first(); |
| 1467 | while (instr != NULL) { |
| 1468 | HInstruction* next = instr->next(); |
| 1469 | if (instr->CheckFlag(HValue::kUseGVN) && |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1470 | !instr->gvn_flags().ContainsAnyOf(depends_flags)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1471 | TraceGVN("Checking instruction %d (%s)\n", |
| 1472 | instr->id(), |
| 1473 | instr->Mnemonic()); |
| 1474 | bool inputs_loop_invariant = true; |
| 1475 | for (int i = 0; i < instr->OperandCount(); ++i) { |
| 1476 | if (instr->OperandAt(i)->IsDefinedAfter(pre_header)) { |
| 1477 | inputs_loop_invariant = false; |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | if (inputs_loop_invariant && ShouldMove(instr, loop_header)) { |
| 1482 | TraceGVN("Found loop invariant instruction %d\n", instr->id()); |
| 1483 | // Move the instruction out of the loop. |
| 1484 | instr->Unlink(); |
| 1485 | instr->InsertBefore(pre_header->end()); |
| 1486 | } |
| 1487 | } |
| 1488 | instr = next; |
| 1489 | } |
| 1490 | } |
| 1491 | |
kmillikin@chromium.org | c0cfb56 | 2011-01-26 10:44:48 +0000 | [diff] [blame] | 1492 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1493 | bool HGlobalValueNumberer::AllowCodeMotion() { |
| 1494 | return info()->shared_info()->opt_count() + 1 < Compiler::kDefaultMaxOptCount; |
| 1495 | } |
| 1496 | |
| 1497 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1498 | bool HGlobalValueNumberer::ShouldMove(HInstruction* instr, |
| 1499 | HBasicBlock* loop_header) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1500 | // If we've disabled code motion or we're in a block that unconditionally |
| 1501 | // deoptimizes, don't move any instructions. |
| 1502 | return AllowCodeMotion() && !instr->block()->IsDeoptimizing(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1506 | GVNFlagSet HGlobalValueNumberer::CollectSideEffectsOnPathsToDominatedBlock( |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1507 | HBasicBlock* dominator, HBasicBlock* dominated) { |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1508 | GVNFlagSet side_effects; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1509 | for (int i = 0; i < dominated->predecessors()->length(); ++i) { |
| 1510 | HBasicBlock* block = dominated->predecessors()->at(i); |
| 1511 | if (dominator->block_id() < block->block_id() && |
| 1512 | block->block_id() < dominated->block_id() && |
| 1513 | visited_on_paths_.Add(block->block_id())) { |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1514 | side_effects.Add(block_side_effects_[block->block_id()]); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1515 | if (block->IsLoopHeader()) { |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1516 | side_effects.Add(loop_side_effects_[block->block_id()]); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1517 | } |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1518 | side_effects.Add(CollectSideEffectsOnPathsToDominatedBlock( |
| 1519 | dominator, block)); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1520 | } |
| 1521 | } |
| 1522 | return side_effects; |
| 1523 | } |
| 1524 | |
| 1525 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1526 | void HGlobalValueNumberer::AnalyzeBlock(HBasicBlock* block, HValueMap* map) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1527 | TraceGVN("Analyzing block B%d%s\n", |
| 1528 | block->block_id(), |
| 1529 | block->IsLoopHeader() ? " (loop header)" : ""); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1530 | |
| 1531 | // If this is a loop header kill everything killed by the loop. |
| 1532 | if (block->IsLoopHeader()) { |
| 1533 | map->Kill(loop_side_effects_[block->block_id()]); |
| 1534 | } |
| 1535 | |
| 1536 | // Go through all instructions of the current block. |
| 1537 | HInstruction* instr = block->first(); |
| 1538 | while (instr != NULL) { |
| 1539 | HInstruction* next = instr->next(); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1540 | GVNFlagSet flags = instr->ChangesFlags(); |
| 1541 | if (!flags.IsEmpty()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1542 | // Clear all instructions in the map that are affected by side effects. |
| 1543 | map->Kill(flags); |
| 1544 | TraceGVN("Instruction %d kills\n", instr->id()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1545 | } |
| 1546 | if (instr->CheckFlag(HValue::kUseGVN)) { |
| 1547 | ASSERT(!instr->HasObservableSideEffects()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1548 | HValue* other = map->Lookup(instr); |
| 1549 | if (other != NULL) { |
| 1550 | ASSERT(instr->Equals(other) && other->Equals(instr)); |
| 1551 | TraceGVN("Replacing value %d (%s) with value %d (%s)\n", |
| 1552 | instr->id(), |
| 1553 | instr->Mnemonic(), |
| 1554 | other->id(), |
| 1555 | other->Mnemonic()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1556 | if (instr->HasSideEffects()) removed_side_effects_ = true; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1557 | instr->DeleteAndReplaceWith(other); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1558 | } else { |
| 1559 | map->Add(instr); |
| 1560 | } |
| 1561 | } |
| 1562 | instr = next; |
| 1563 | } |
| 1564 | |
| 1565 | // Recursively continue analysis for all immediately dominated blocks. |
| 1566 | int length = block->dominated_blocks()->length(); |
| 1567 | for (int i = 0; i < length; ++i) { |
| 1568 | HBasicBlock* dominated = block->dominated_blocks()->at(i); |
| 1569 | // No need to copy the map for the last child in the dominator tree. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1570 | HValueMap* successor_map = (i == length - 1) ? map : map->Copy(zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1571 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1572 | // Kill everything killed on any path between this block and the |
| 1573 | // dominated block. |
| 1574 | // We don't have to traverse these paths if the value map is |
| 1575 | // already empty. |
| 1576 | // If the range of block ids (block_id, dominated_id) is empty |
| 1577 | // there are no such paths. |
| 1578 | if (!successor_map->IsEmpty() && |
| 1579 | block->block_id() + 1 < dominated->block_id()) { |
| 1580 | visited_on_paths_.Clear(); |
| 1581 | successor_map->Kill(CollectSideEffectsOnPathsToDominatedBlock(block, |
| 1582 | dominated)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1583 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1584 | AnalyzeBlock(dominated, successor_map); |
| 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | |
| 1589 | class HInferRepresentation BASE_EMBEDDED { |
| 1590 | public: |
| 1591 | explicit HInferRepresentation(HGraph* graph) |
| 1592 | : graph_(graph), worklist_(8), in_worklist_(graph->GetMaximumValueID()) {} |
| 1593 | |
| 1594 | void Analyze(); |
| 1595 | |
| 1596 | private: |
| 1597 | Representation TryChange(HValue* current); |
| 1598 | void AddToWorklist(HValue* current); |
| 1599 | void InferBasedOnInputs(HValue* current); |
| 1600 | void AddDependantsToWorklist(HValue* current); |
| 1601 | void InferBasedOnUses(HValue* current); |
| 1602 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1603 | Zone* zone() { return graph_->zone(); } |
| 1604 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1605 | HGraph* graph_; |
| 1606 | ZoneList<HValue*> worklist_; |
| 1607 | BitVector in_worklist_; |
| 1608 | }; |
| 1609 | |
| 1610 | |
| 1611 | void HInferRepresentation::AddToWorklist(HValue* current) { |
| 1612 | if (current->representation().IsSpecialization()) return; |
| 1613 | if (!current->CheckFlag(HValue::kFlexibleRepresentation)) return; |
| 1614 | if (in_worklist_.Contains(current->id())) return; |
| 1615 | worklist_.Add(current); |
| 1616 | in_worklist_.Add(current->id()); |
| 1617 | } |
| 1618 | |
| 1619 | |
| 1620 | // This method tries to specialize the representation type of the value |
| 1621 | // given as a parameter. The value is asked to infer its representation type |
| 1622 | // based on its inputs. If the inferred type is more specialized, then this |
| 1623 | // becomes the new representation type of the node. |
| 1624 | void HInferRepresentation::InferBasedOnInputs(HValue* current) { |
| 1625 | Representation r = current->representation(); |
| 1626 | if (r.IsSpecialization()) return; |
| 1627 | ASSERT(current->CheckFlag(HValue::kFlexibleRepresentation)); |
| 1628 | Representation inferred = current->InferredRepresentation(); |
| 1629 | if (inferred.IsSpecialization()) { |
| 1630 | current->ChangeRepresentation(inferred); |
| 1631 | AddDependantsToWorklist(current); |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1636 | void HInferRepresentation::AddDependantsToWorklist(HValue* value) { |
| 1637 | for (HUseIterator it(value->uses()); !it.Done(); it.Advance()) { |
| 1638 | AddToWorklist(it.value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1639 | } |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1640 | for (int i = 0; i < value->OperandCount(); ++i) { |
| 1641 | AddToWorklist(value->OperandAt(i)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | |
| 1646 | // This method calculates whether specializing the representation of the value |
| 1647 | // given as the parameter has a benefit in terms of less necessary type |
| 1648 | // conversions. If there is a benefit, then the representation of the value is |
| 1649 | // specialized. |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1650 | void HInferRepresentation::InferBasedOnUses(HValue* value) { |
| 1651 | Representation r = value->representation(); |
| 1652 | if (r.IsSpecialization() || value->HasNoUses()) return; |
| 1653 | ASSERT(value->CheckFlag(HValue::kFlexibleRepresentation)); |
| 1654 | Representation new_rep = TryChange(value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1655 | if (!new_rep.IsNone()) { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1656 | if (!value->representation().Equals(new_rep)) { |
| 1657 | value->ChangeRepresentation(new_rep); |
| 1658 | AddDependantsToWorklist(value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1659 | } |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1664 | Representation HInferRepresentation::TryChange(HValue* value) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1665 | // Array of use counts for each representation. |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1666 | int use_count[Representation::kNumRepresentations] = { 0 }; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1667 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1668 | for (HUseIterator it(value->uses()); !it.Done(); it.Advance()) { |
| 1669 | HValue* use = it.value(); |
| 1670 | Representation rep = use->RequiredInputRepresentation(it.index()); |
| 1671 | if (rep.IsNone()) continue; |
| 1672 | if (use->IsPhi()) HPhi::cast(use)->AddIndirectUsesTo(&use_count[0]); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 1673 | use_count[rep.kind()] += use->LoopWeight(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1674 | } |
| 1675 | int tagged_count = use_count[Representation::kTagged]; |
| 1676 | int double_count = use_count[Representation::kDouble]; |
| 1677 | int int32_count = use_count[Representation::kInteger32]; |
| 1678 | int non_tagged_count = double_count + int32_count; |
| 1679 | |
| 1680 | // If a non-loop phi has tagged uses, don't convert it to untagged. |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1681 | if (value->IsPhi() && !value->block()->IsLoopHeader() && tagged_count > 0) { |
| 1682 | return Representation::None(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1685 | // Prefer unboxing over boxing, the latter is more expensive. |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 1686 | if (tagged_count > non_tagged_count) return Representation::None(); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1687 | |
| 1688 | // Prefer Integer32 over Double, if possible. |
| 1689 | if (int32_count > 0 && value->IsConvertibleToInteger()) { |
| 1690 | return Representation::Integer32(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1691 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1692 | |
| 1693 | if (double_count > 0) return Representation::Double(); |
| 1694 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1695 | return Representation::None(); |
| 1696 | } |
| 1697 | |
| 1698 | |
| 1699 | void HInferRepresentation::Analyze() { |
| 1700 | HPhase phase("Infer representations", graph_); |
| 1701 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1702 | // (1) Initialize bit vectors and count real uses. Each phi gets a |
| 1703 | // bit-vector of length <number of phis>. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1704 | const ZoneList<HPhi*>* phi_list = graph_->phi_list(); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1705 | int phi_count = phi_list->length(); |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 1706 | ZoneList<BitVector*> connected_phis(phi_count); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1707 | for (int i = 0; i < phi_count; ++i) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1708 | phi_list->at(i)->InitRealUses(i); |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 1709 | BitVector* connected_set = new(zone()) BitVector(phi_count); |
| 1710 | connected_set->Add(i); |
| 1711 | connected_phis.Add(connected_set); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1714 | // (2) Do a fixed point iteration to find the set of connected phis. A |
| 1715 | // phi is connected to another phi if its value is used either directly or |
| 1716 | // indirectly through a transitive closure of the def-use relation. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1717 | bool change = true; |
| 1718 | while (change) { |
| 1719 | change = false; |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1720 | // We normally have far more "forward edges" than "backward edges", |
| 1721 | // so we terminate faster when we walk backwards. |
| 1722 | for (int i = phi_count - 1; i >= 0; --i) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1723 | HPhi* phi = phi_list->at(i); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1724 | for (HUseIterator it(phi->uses()); !it.Done(); it.Advance()) { |
| 1725 | HValue* use = it.value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1726 | if (use->IsPhi()) { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1727 | int id = HPhi::cast(use)->phi_id(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1728 | if (connected_phis[i]->UnionIsChanged(*connected_phis[id])) |
| 1729 | change = true; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1730 | } |
| 1731 | } |
| 1732 | } |
| 1733 | } |
| 1734 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1735 | // (3) Use the phi reachability information from step 2 to |
| 1736 | // (a) sum up the non-phi use counts of all connected phis. |
| 1737 | // (b) push information about values which can't be converted to integer |
| 1738 | // without deoptimization through the phi use-def chains, avoiding |
| 1739 | // unnecessary deoptimizations later. |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1740 | for (int i = 0; i < phi_count; ++i) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1741 | HPhi* phi = phi_list->at(i); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1742 | bool cti = phi->AllOperandsConvertibleToInteger(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1743 | for (BitVector::Iterator it(connected_phis.at(i)); |
| 1744 | !it.Done(); |
| 1745 | it.Advance()) { |
| 1746 | int index = it.Current(); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1747 | HPhi* it_use = phi_list->at(it.Current()); |
| 1748 | if (index != i) phi->AddNonPhiUsesFrom(it_use); // Don't count twice! |
| 1749 | if (!cti) it_use->set_is_convertible_to_integer(false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1750 | } |
| 1751 | } |
| 1752 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1753 | // Initialize work list |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1754 | for (int i = 0; i < graph_->blocks()->length(); ++i) { |
| 1755 | HBasicBlock* block = graph_->blocks()->at(i); |
| 1756 | const ZoneList<HPhi*>* phis = block->phis(); |
| 1757 | for (int j = 0; j < phis->length(); ++j) { |
| 1758 | AddToWorklist(phis->at(j)); |
| 1759 | } |
| 1760 | |
| 1761 | HInstruction* current = block->first(); |
| 1762 | while (current != NULL) { |
| 1763 | AddToWorklist(current); |
| 1764 | current = current->next(); |
| 1765 | } |
| 1766 | } |
| 1767 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1768 | // Do a fixed point iteration, trying to improve representations |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1769 | while (!worklist_.is_empty()) { |
| 1770 | HValue* current = worklist_.RemoveLast(); |
| 1771 | in_worklist_.Remove(current->id()); |
| 1772 | InferBasedOnInputs(current); |
| 1773 | InferBasedOnUses(current); |
| 1774 | } |
| 1775 | } |
| 1776 | |
| 1777 | |
| 1778 | void HGraph::InitializeInferredTypes() { |
| 1779 | HPhase phase("Inferring types", this); |
| 1780 | InitializeInferredTypes(0, this->blocks_.length() - 1); |
| 1781 | } |
| 1782 | |
| 1783 | |
| 1784 | void HGraph::InitializeInferredTypes(int from_inclusive, int to_inclusive) { |
| 1785 | for (int i = from_inclusive; i <= to_inclusive; ++i) { |
| 1786 | HBasicBlock* block = blocks_[i]; |
| 1787 | |
| 1788 | const ZoneList<HPhi*>* phis = block->phis(); |
| 1789 | for (int j = 0; j < phis->length(); j++) { |
| 1790 | phis->at(j)->UpdateInferredType(); |
| 1791 | } |
| 1792 | |
| 1793 | HInstruction* current = block->first(); |
| 1794 | while (current != NULL) { |
| 1795 | current->UpdateInferredType(); |
| 1796 | current = current->next(); |
| 1797 | } |
| 1798 | |
| 1799 | if (block->IsLoopHeader()) { |
| 1800 | HBasicBlock* last_back_edge = |
| 1801 | block->loop_information()->GetLastBackEdge(); |
| 1802 | InitializeInferredTypes(i + 1, last_back_edge->block_id()); |
| 1803 | // Skip all blocks already processed by the recursive call. |
| 1804 | i = last_back_edge->block_id(); |
| 1805 | // Update phis of the loop header now after the whole loop body is |
| 1806 | // guaranteed to be processed. |
| 1807 | ZoneList<HValue*> worklist(block->phis()->length()); |
| 1808 | for (int j = 0; j < block->phis()->length(); ++j) { |
| 1809 | worklist.Add(block->phis()->at(j)); |
| 1810 | } |
| 1811 | InferTypes(&worklist); |
| 1812 | } |
| 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | |
| 1817 | void HGraph::PropagateMinusZeroChecks(HValue* value, BitVector* visited) { |
| 1818 | HValue* current = value; |
| 1819 | while (current != NULL) { |
| 1820 | if (visited->Contains(current->id())) return; |
| 1821 | |
| 1822 | // For phis, we must propagate the check to all of its inputs. |
| 1823 | if (current->IsPhi()) { |
| 1824 | visited->Add(current->id()); |
| 1825 | HPhi* phi = HPhi::cast(current); |
| 1826 | for (int i = 0; i < phi->OperandCount(); ++i) { |
| 1827 | PropagateMinusZeroChecks(phi->OperandAt(i), visited); |
| 1828 | } |
| 1829 | break; |
| 1830 | } |
| 1831 | |
| 1832 | // For multiplication and division, we must propagate to the left and |
| 1833 | // the right side. |
| 1834 | if (current->IsMul()) { |
| 1835 | HMul* mul = HMul::cast(current); |
| 1836 | mul->EnsureAndPropagateNotMinusZero(visited); |
| 1837 | PropagateMinusZeroChecks(mul->left(), visited); |
| 1838 | PropagateMinusZeroChecks(mul->right(), visited); |
| 1839 | } else if (current->IsDiv()) { |
| 1840 | HDiv* div = HDiv::cast(current); |
| 1841 | div->EnsureAndPropagateNotMinusZero(visited); |
| 1842 | PropagateMinusZeroChecks(div->left(), visited); |
| 1843 | PropagateMinusZeroChecks(div->right(), visited); |
| 1844 | } |
| 1845 | |
| 1846 | current = current->EnsureAndPropagateNotMinusZero(visited); |
| 1847 | } |
| 1848 | } |
| 1849 | |
| 1850 | |
| 1851 | void HGraph::InsertRepresentationChangeForUse(HValue* value, |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1852 | HValue* use_value, |
| 1853 | int use_index, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1854 | Representation to) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1855 | // Insert the representation change right before its use. For phi-uses we |
| 1856 | // insert at the end of the corresponding predecessor. |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1857 | HInstruction* next = NULL; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1858 | if (use_value->IsPhi()) { |
| 1859 | next = use_value->block()->predecessors()->at(use_index)->end(); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1860 | } else { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1861 | next = HInstruction::cast(use_value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1864 | // For constants we try to make the representation change at compile |
| 1865 | // time. When a representation change is not possible without loss of |
| 1866 | // information we treat constants like normal instructions and insert the |
| 1867 | // change instructions for them. |
| 1868 | HInstruction* new_value = NULL; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1869 | bool is_truncating = use_value->CheckFlag(HValue::kTruncatingToInt32); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1870 | bool deoptimize_on_undefined = |
| 1871 | use_value->CheckFlag(HValue::kDeoptimizeOnUndefined); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1872 | if (value->IsConstant()) { |
| 1873 | HConstant* constant = HConstant::cast(value); |
| 1874 | // Try to create a new copy of the constant with the new representation. |
| 1875 | new_value = is_truncating |
| 1876 | ? constant->CopyToTruncatedInt32() |
| 1877 | : constant->CopyToRepresentation(to); |
| 1878 | } |
| 1879 | |
| 1880 | if (new_value == NULL) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1881 | new_value = new(zone()) HChange(value, to, |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1882 | is_truncating, deoptimize_on_undefined); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | new_value->InsertBefore(next); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1886 | use_value->SetOperandAt(use_index, new_value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1890 | void HGraph::InsertRepresentationChangesForValue(HValue* value) { |
| 1891 | Representation r = value->representation(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1892 | if (r.IsNone()) return; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1893 | if (value->HasNoUses()) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1894 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1895 | for (HUseIterator it(value->uses()); !it.Done(); it.Advance()) { |
| 1896 | HValue* use_value = it.value(); |
| 1897 | int use_index = it.index(); |
| 1898 | Representation req = use_value->RequiredInputRepresentation(use_index); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1899 | if (req.IsNone() || req.Equals(r)) continue; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1900 | InsertRepresentationChangeForUse(value, use_value, use_index, req); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1901 | } |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1902 | if (value->HasNoUses()) { |
| 1903 | ASSERT(value->IsConstant()); |
| 1904 | value->DeleteAndReplaceWith(NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1905 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 1906 | |
| 1907 | // The only purpose of a HForceRepresentation is to represent the value |
| 1908 | // after the (possible) HChange instruction. We make it disappear. |
| 1909 | if (value->IsForceRepresentation()) { |
| 1910 | value->DeleteAndReplaceWith(HForceRepresentation::cast(value)->value()); |
| 1911 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | |
| 1915 | void HGraph::InsertRepresentationChanges() { |
| 1916 | HPhase phase("Insert representation changes", this); |
| 1917 | |
| 1918 | |
| 1919 | // Compute truncation flag for phis: Initially assume that all |
| 1920 | // int32-phis allow truncation and iteratively remove the ones that |
| 1921 | // are used in an operation that does not allow a truncating |
| 1922 | // conversion. |
| 1923 | // TODO(fschneider): Replace this with a worklist-based iteration. |
| 1924 | for (int i = 0; i < phi_list()->length(); i++) { |
| 1925 | HPhi* phi = phi_list()->at(i); |
| 1926 | if (phi->representation().IsInteger32()) { |
| 1927 | phi->SetFlag(HValue::kTruncatingToInt32); |
| 1928 | } |
| 1929 | } |
| 1930 | bool change = true; |
| 1931 | while (change) { |
| 1932 | change = false; |
| 1933 | for (int i = 0; i < phi_list()->length(); i++) { |
| 1934 | HPhi* phi = phi_list()->at(i); |
| 1935 | if (!phi->CheckFlag(HValue::kTruncatingToInt32)) continue; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1936 | for (HUseIterator it(phi->uses()); !it.Done(); it.Advance()) { |
| 1937 | HValue* use = it.value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1938 | if (!use->CheckFlag(HValue::kTruncatingToInt32)) { |
| 1939 | phi->ClearFlag(HValue::kTruncatingToInt32); |
| 1940 | change = true; |
| 1941 | break; |
| 1942 | } |
| 1943 | } |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | for (int i = 0; i < blocks_.length(); ++i) { |
| 1948 | // Process phi instructions first. |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1949 | const ZoneList<HPhi*>* phis = blocks_[i]->phis(); |
| 1950 | for (int j = 0; j < phis->length(); j++) { |
| 1951 | InsertRepresentationChangesForValue(phis->at(j)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | // Process normal instructions. |
| 1955 | HInstruction* current = blocks_[i]->first(); |
| 1956 | while (current != NULL) { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1957 | InsertRepresentationChangesForValue(current); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1958 | current = current->next(); |
| 1959 | } |
| 1960 | } |
| 1961 | } |
| 1962 | |
| 1963 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1964 | void HGraph::RecursivelyMarkPhiDeoptimizeOnUndefined(HPhi* phi) { |
| 1965 | if (phi->CheckFlag(HValue::kDeoptimizeOnUndefined)) return; |
| 1966 | phi->SetFlag(HValue::kDeoptimizeOnUndefined); |
| 1967 | for (int i = 0; i < phi->OperandCount(); ++i) { |
| 1968 | HValue* input = phi->OperandAt(i); |
| 1969 | if (input->IsPhi()) { |
| 1970 | RecursivelyMarkPhiDeoptimizeOnUndefined(HPhi::cast(input)); |
| 1971 | } |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | |
| 1976 | void HGraph::MarkDeoptimizeOnUndefined() { |
| 1977 | HPhase phase("MarkDeoptimizeOnUndefined", this); |
| 1978 | // Compute DeoptimizeOnUndefined flag for phis. |
| 1979 | // Any phi that can reach a use with DeoptimizeOnUndefined set must |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1980 | // have DeoptimizeOnUndefined set. Currently only HCompareIDAndBranch, with |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1981 | // double input representation, has this flag set. |
| 1982 | // The flag is used by HChange tagged->double, which must deoptimize |
| 1983 | // if one of its uses has this flag set. |
| 1984 | for (int i = 0; i < phi_list()->length(); i++) { |
| 1985 | HPhi* phi = phi_list()->at(i); |
| 1986 | if (phi->representation().IsDouble()) { |
| 1987 | for (HUseIterator it(phi->uses()); !it.Done(); it.Advance()) { |
| 1988 | if (it.value()->CheckFlag(HValue::kDeoptimizeOnUndefined)) { |
| 1989 | RecursivelyMarkPhiDeoptimizeOnUndefined(phi); |
| 1990 | break; |
| 1991 | } |
| 1992 | } |
| 1993 | } |
| 1994 | } |
| 1995 | } |
| 1996 | |
| 1997 | |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 1998 | void HGraph::ComputeMinusZeroChecks() { |
| 1999 | BitVector visited(GetMaximumValueID()); |
| 2000 | for (int i = 0; i < blocks_.length(); ++i) { |
| 2001 | for (HInstruction* current = blocks_[i]->first(); |
| 2002 | current != NULL; |
| 2003 | current = current->next()) { |
| 2004 | if (current->IsChange()) { |
| 2005 | HChange* change = HChange::cast(current); |
| 2006 | // Propagate flags for negative zero checks upwards from conversions |
| 2007 | // int32-to-tagged and int32-to-double. |
| 2008 | Representation from = change->value()->representation(); |
| 2009 | ASSERT(from.Equals(change->from())); |
| 2010 | if (from.IsInteger32()) { |
| 2011 | ASSERT(change->to().IsTagged() || change->to().IsDouble()); |
| 2012 | ASSERT(visited.IsEmpty()); |
| 2013 | PropagateMinusZeroChecks(change->value(), &visited); |
| 2014 | visited.Clear(); |
| 2015 | } |
| 2016 | } |
| 2017 | } |
| 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2022 | // Implementation of utility class to encapsulate the translation state for |
| 2023 | // a (possibly inlined) function. |
| 2024 | FunctionState::FunctionState(HGraphBuilder* owner, |
| 2025 | CompilationInfo* info, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2026 | TypeFeedbackOracle* oracle, |
| 2027 | bool drop_extra) |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2028 | : owner_(owner), |
| 2029 | compilation_info_(info), |
| 2030 | oracle_(oracle), |
| 2031 | call_context_(NULL), |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2032 | drop_extra_(drop_extra), |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2033 | function_return_(NULL), |
| 2034 | test_context_(NULL), |
| 2035 | outer_(owner->function_state()) { |
| 2036 | if (outer_ != NULL) { |
| 2037 | // State for an inline function. |
| 2038 | if (owner->ast_context()->IsTest()) { |
| 2039 | HBasicBlock* if_true = owner->graph()->CreateBasicBlock(); |
| 2040 | HBasicBlock* if_false = owner->graph()->CreateBasicBlock(); |
| 2041 | if_true->MarkAsInlineReturnTarget(); |
| 2042 | if_false->MarkAsInlineReturnTarget(); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2043 | Expression* cond = TestContext::cast(owner->ast_context())->condition(); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2044 | // The AstContext constructor pushed on the context stack. This newed |
| 2045 | // instance is the reason that AstContext can't be BASE_EMBEDDED. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2046 | test_context_ = new TestContext(owner, cond, if_true, if_false); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2047 | } else { |
| 2048 | function_return_ = owner->graph()->CreateBasicBlock(); |
| 2049 | function_return()->MarkAsInlineReturnTarget(); |
| 2050 | } |
| 2051 | // Set this after possibly allocating a new TestContext above. |
| 2052 | call_context_ = owner->ast_context(); |
| 2053 | } |
| 2054 | |
| 2055 | // Push on the state stack. |
| 2056 | owner->set_function_state(this); |
| 2057 | } |
| 2058 | |
| 2059 | |
| 2060 | FunctionState::~FunctionState() { |
| 2061 | delete test_context_; |
| 2062 | owner_->set_function_state(outer_); |
| 2063 | } |
| 2064 | |
| 2065 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2066 | // Implementation of utility classes to represent an expression's context in |
| 2067 | // the AST. |
| 2068 | AstContext::AstContext(HGraphBuilder* owner, Expression::Context kind) |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2069 | : owner_(owner), |
| 2070 | kind_(kind), |
| 2071 | outer_(owner->ast_context()), |
| 2072 | for_typeof_(false) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2073 | owner->set_ast_context(this); // Push. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2074 | #ifdef DEBUG |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 2075 | ASSERT(!owner->environment()->is_arguments_adaptor()); |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 2076 | original_length_ = owner->environment()->length(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2077 | #endif |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | |
| 2081 | AstContext::~AstContext() { |
| 2082 | owner_->set_ast_context(outer_); // Pop. |
| 2083 | } |
| 2084 | |
| 2085 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2086 | EffectContext::~EffectContext() { |
| 2087 | ASSERT(owner()->HasStackOverflow() || |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2088 | owner()->current_block() == NULL || |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 2089 | (owner()->environment()->length() == original_length_ && |
| 2090 | !owner()->environment()->is_arguments_adaptor())); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | |
| 2094 | ValueContext::~ValueContext() { |
| 2095 | ASSERT(owner()->HasStackOverflow() || |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2096 | owner()->current_block() == NULL || |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 2097 | (owner()->environment()->length() == original_length_ + 1 && |
| 2098 | !owner()->environment()->is_arguments_adaptor())); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | |
| 2102 | void EffectContext::ReturnValue(HValue* value) { |
| 2103 | // The value is simply ignored. |
| 2104 | } |
| 2105 | |
| 2106 | |
| 2107 | void ValueContext::ReturnValue(HValue* value) { |
| 2108 | // The value is tracked in the bailout environment, and communicated |
| 2109 | // through the environment as the result of the expression. |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 2110 | if (!arguments_allowed() && value->CheckFlag(HValue::kIsArguments)) { |
| 2111 | owner()->Bailout("bad value context for arguments value"); |
| 2112 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2113 | owner()->Push(value); |
| 2114 | } |
| 2115 | |
| 2116 | |
| 2117 | void TestContext::ReturnValue(HValue* value) { |
| 2118 | BuildBranch(value); |
| 2119 | } |
| 2120 | |
| 2121 | |
| 2122 | void EffectContext::ReturnInstruction(HInstruction* instr, int ast_id) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2123 | ASSERT(!instr->IsControlInstruction()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2124 | owner()->AddInstruction(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2125 | if (instr->HasObservableSideEffects()) owner()->AddSimulate(ast_id); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2129 | void EffectContext::ReturnControl(HControlInstruction* instr, int ast_id) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2130 | ASSERT(!instr->HasObservableSideEffects()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2131 | HBasicBlock* empty_true = owner()->graph()->CreateBasicBlock(); |
| 2132 | HBasicBlock* empty_false = owner()->graph()->CreateBasicBlock(); |
| 2133 | instr->SetSuccessorAt(0, empty_true); |
| 2134 | instr->SetSuccessorAt(1, empty_false); |
| 2135 | owner()->current_block()->Finish(instr); |
| 2136 | HBasicBlock* join = owner()->CreateJoin(empty_true, empty_false, ast_id); |
| 2137 | owner()->set_current_block(join); |
| 2138 | } |
| 2139 | |
| 2140 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2141 | void ValueContext::ReturnInstruction(HInstruction* instr, int ast_id) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2142 | ASSERT(!instr->IsControlInstruction()); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 2143 | if (!arguments_allowed() && instr->CheckFlag(HValue::kIsArguments)) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2144 | return owner()->Bailout("bad value context for arguments object value"); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 2145 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2146 | owner()->AddInstruction(instr); |
| 2147 | owner()->Push(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2148 | if (instr->HasObservableSideEffects()) owner()->AddSimulate(ast_id); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2152 | void ValueContext::ReturnControl(HControlInstruction* instr, int ast_id) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2153 | ASSERT(!instr->HasObservableSideEffects()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2154 | if (!arguments_allowed() && instr->CheckFlag(HValue::kIsArguments)) { |
| 2155 | return owner()->Bailout("bad value context for arguments object value"); |
| 2156 | } |
| 2157 | HBasicBlock* materialize_false = owner()->graph()->CreateBasicBlock(); |
| 2158 | HBasicBlock* materialize_true = owner()->graph()->CreateBasicBlock(); |
| 2159 | instr->SetSuccessorAt(0, materialize_true); |
| 2160 | instr->SetSuccessorAt(1, materialize_false); |
| 2161 | owner()->current_block()->Finish(instr); |
| 2162 | owner()->set_current_block(materialize_true); |
| 2163 | owner()->Push(owner()->graph()->GetConstantTrue()); |
| 2164 | owner()->set_current_block(materialize_false); |
| 2165 | owner()->Push(owner()->graph()->GetConstantFalse()); |
| 2166 | HBasicBlock* join = |
| 2167 | owner()->CreateJoin(materialize_true, materialize_false, ast_id); |
| 2168 | owner()->set_current_block(join); |
| 2169 | } |
| 2170 | |
| 2171 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2172 | void TestContext::ReturnInstruction(HInstruction* instr, int ast_id) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2173 | ASSERT(!instr->IsControlInstruction()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2174 | HGraphBuilder* builder = owner(); |
| 2175 | builder->AddInstruction(instr); |
| 2176 | // We expect a simulate after every expression with side effects, though |
| 2177 | // this one isn't actually needed (and wouldn't work if it were targeted). |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2178 | if (instr->HasObservableSideEffects()) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2179 | builder->Push(instr); |
| 2180 | builder->AddSimulate(ast_id); |
| 2181 | builder->Pop(); |
| 2182 | } |
| 2183 | BuildBranch(instr); |
| 2184 | } |
| 2185 | |
| 2186 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2187 | void TestContext::ReturnControl(HControlInstruction* instr, int ast_id) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2188 | ASSERT(!instr->HasObservableSideEffects()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2189 | HBasicBlock* empty_true = owner()->graph()->CreateBasicBlock(); |
| 2190 | HBasicBlock* empty_false = owner()->graph()->CreateBasicBlock(); |
| 2191 | instr->SetSuccessorAt(0, empty_true); |
| 2192 | instr->SetSuccessorAt(1, empty_false); |
| 2193 | owner()->current_block()->Finish(instr); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2194 | empty_true->Goto(if_true(), owner()->function_state()->drop_extra()); |
| 2195 | empty_false->Goto(if_false(), owner()->function_state()->drop_extra()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2196 | owner()->set_current_block(NULL); |
| 2197 | } |
| 2198 | |
| 2199 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2200 | void TestContext::BuildBranch(HValue* value) { |
| 2201 | // We expect the graph to be in edge-split form: there is no edge that |
| 2202 | // connects a branch node to a join node. We conservatively ensure that |
| 2203 | // property by always adding an empty block on the outgoing edges of this |
| 2204 | // branch. |
| 2205 | HGraphBuilder* builder = owner(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2206 | if (value != NULL && value->CheckFlag(HValue::kIsArguments)) { |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 2207 | builder->Bailout("arguments object value in a test context"); |
| 2208 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2209 | HBasicBlock* empty_true = builder->graph()->CreateBasicBlock(); |
| 2210 | HBasicBlock* empty_false = builder->graph()->CreateBasicBlock(); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 2211 | unsigned test_id = condition()->test_id(); |
| 2212 | ToBooleanStub::Types expected(builder->oracle()->ToBooleanTypes(test_id)); |
| 2213 | HBranch* test = new(zone()) HBranch(value, empty_true, empty_false, expected); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2214 | builder->current_block()->Finish(test); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2215 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2216 | empty_true->Goto(if_true(), owner()->function_state()->drop_extra()); |
| 2217 | empty_false->Goto(if_false(), owner()->function_state()->drop_extra()); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2218 | builder->set_current_block(NULL); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2221 | |
| 2222 | // HGraphBuilder infrastructure for bailing out and checking bailouts. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2223 | #define CHECK_BAILOUT(call) \ |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2224 | do { \ |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2225 | call; \ |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2226 | if (HasStackOverflow()) return; \ |
| 2227 | } while (false) |
| 2228 | |
| 2229 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2230 | #define CHECK_ALIVE(call) \ |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2231 | do { \ |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2232 | call; \ |
| 2233 | if (HasStackOverflow() || current_block() == NULL) return; \ |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2234 | } while (false) |
| 2235 | |
| 2236 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2237 | void HGraphBuilder::Bailout(const char* reason) { |
| 2238 | if (FLAG_trace_bailout) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 2239 | SmartArrayPointer<char> name( |
| 2240 | info()->shared_info()->DebugName()->ToCString()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2241 | PrintF("Bailout in HGraphBuilder: @\"%s\": %s\n", *name, reason); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2242 | } |
| 2243 | SetStackOverflow(); |
| 2244 | } |
| 2245 | |
| 2246 | |
| 2247 | void HGraphBuilder::VisitForEffect(Expression* expr) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2248 | EffectContext for_effect(this); |
| 2249 | Visit(expr); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 2253 | void HGraphBuilder::VisitForValue(Expression* expr, ArgumentsAllowedFlag flag) { |
| 2254 | ValueContext for_value(this, flag); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2255 | Visit(expr); |
| 2256 | } |
| 2257 | |
| 2258 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2259 | void HGraphBuilder::VisitForTypeOf(Expression* expr) { |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 2260 | ValueContext for_value(this, ARGUMENTS_NOT_ALLOWED); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2261 | for_value.set_for_typeof(true); |
| 2262 | Visit(expr); |
| 2263 | } |
| 2264 | |
| 2265 | |
| 2266 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2267 | void HGraphBuilder::VisitForControl(Expression* expr, |
| 2268 | HBasicBlock* true_block, |
| 2269 | HBasicBlock* false_block) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2270 | TestContext for_test(this, expr, true_block, false_block); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2271 | Visit(expr); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 2275 | HValue* HGraphBuilder::VisitArgument(Expression* expr) { |
| 2276 | VisitForValue(expr); |
| 2277 | if (HasStackOverflow() || current_block() == NULL) return NULL; |
| 2278 | HValue* value = Pop(); |
| 2279 | Push(AddInstruction(new(zone()) HPushArgument(value))); |
| 2280 | return value; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | |
| 2284 | void HGraphBuilder::VisitArgumentList(ZoneList<Expression*>* arguments) { |
| 2285 | for (int i = 0; i < arguments->length(); i++) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2286 | CHECK_ALIVE(VisitArgument(arguments->at(i))); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2287 | } |
| 2288 | } |
| 2289 | |
| 2290 | |
| 2291 | void HGraphBuilder::VisitExpressions(ZoneList<Expression*>* exprs) { |
| 2292 | for (int i = 0; i < exprs->length(); ++i) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2293 | CHECK_ALIVE(VisitForValue(exprs->at(i))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2298 | HGraph* HGraphBuilder::CreateGraph() { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 2299 | graph_ = new(zone()) HGraph(info()); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2300 | if (FLAG_hydrogen_stats) HStatistics::Instance()->Initialize(info()); |
| 2301 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2302 | { |
| 2303 | HPhase phase("Block building"); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2304 | current_block_ = graph()->entry_block(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2305 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2306 | Scope* scope = info()->scope(); |
| 2307 | if (scope->HasIllegalRedeclaration()) { |
| 2308 | Bailout("function with illegal redeclaration"); |
| 2309 | return NULL; |
| 2310 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2311 | SetUpScope(scope); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2312 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2313 | // Add an edge to the body entry. This is warty: the graph's start |
| 2314 | // environment will be used by the Lithium translation as the initial |
| 2315 | // environment on graph entry, but it has now been mutated by the |
| 2316 | // Hydrogen translation of the instructions in the start block. This |
| 2317 | // environment uses values which have not been defined yet. These |
| 2318 | // Hydrogen instructions will then be replayed by the Lithium |
| 2319 | // translation, so they cannot have an environment effect. The edge to |
| 2320 | // the body's entry block (along with some special logic for the start |
| 2321 | // block in HInstruction::InsertAfter) seals the start block from |
| 2322 | // getting unwanted instructions inserted. |
| 2323 | // |
| 2324 | // TODO(kmillikin): Fix this. Stop mutating the initial environment. |
| 2325 | // Make the Hydrogen instructions in the initial block into Hydrogen |
| 2326 | // values (but not instructions), present in the initial environment and |
| 2327 | // not replayed by the Lithium translation. |
| 2328 | HEnvironment* initial_env = environment()->CopyWithoutHistory(); |
| 2329 | HBasicBlock* body_entry = CreateBasicBlock(initial_env); |
| 2330 | current_block()->Goto(body_entry); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 2331 | body_entry->SetJoinId(AstNode::kFunctionEntryId); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2332 | set_current_block(body_entry); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 2333 | |
| 2334 | // Handle implicit declaration of the function name in named function |
| 2335 | // expressions before other declarations. |
| 2336 | if (scope->is_function_scope() && scope->function() != NULL) { |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 2337 | HandleDeclaration(scope->function(), CONST, NULL); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 2338 | } |
| 2339 | VisitDeclarations(scope->declarations()); |
| 2340 | AddSimulate(AstNode::kDeclarationsId); |
| 2341 | |
| 2342 | HValue* context = environment()->LookupContext(); |
| 2343 | AddInstruction( |
| 2344 | new(zone()) HStackCheck(context, HStackCheck::kFunctionEntry)); |
| 2345 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2346 | VisitStatements(info()->function()->body()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2347 | if (HasStackOverflow()) return NULL; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2348 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2349 | if (current_block() != NULL) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 2350 | HReturn* instr = new(zone()) HReturn(graph()->GetConstantUndefined()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2351 | current_block()->FinishExit(instr); |
| 2352 | set_current_block(NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2353 | } |
| 2354 | } |
| 2355 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2356 | graph()->OrderBlocks(); |
| 2357 | graph()->AssignDominators(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2358 | |
| 2359 | #ifdef DEBUG |
| 2360 | // Do a full verify after building the graph and computing dominators. |
| 2361 | graph()->Verify(true); |
| 2362 | #endif |
| 2363 | |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 2364 | graph()->PropagateDeoptimizingMark(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2365 | if (!graph()->CheckConstPhiUses()) { |
| 2366 | Bailout("Unsupported phi use of const variable"); |
| 2367 | return NULL; |
| 2368 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2369 | graph()->EliminateRedundantPhis(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2370 | if (!graph()->CheckArgumentsPhiUses()) { |
| 2371 | Bailout("Unsupported phi use of arguments"); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 2372 | return NULL; |
| 2373 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2374 | if (FLAG_eliminate_dead_phis) graph()->EliminateUnreachablePhis(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2375 | graph()->CollectPhis(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2376 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2377 | HInferRepresentation rep(graph()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2378 | rep.Analyze(); |
| 2379 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 2380 | graph()->MarkDeoptimizeOnUndefined(); |
| 2381 | graph()->InsertRepresentationChanges(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2382 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2383 | graph()->InitializeInferredTypes(); |
| 2384 | graph()->Canonicalize(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2385 | |
| 2386 | // Perform common subexpression elimination and loop-invariant code motion. |
| 2387 | if (FLAG_use_gvn) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2388 | HPhase phase("Global value numbering", graph()); |
| 2389 | HGlobalValueNumberer gvn(graph(), info()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2390 | bool removed_side_effects = gvn.Analyze(); |
| 2391 | // Trigger a second analysis pass to further eliminate duplicate values that |
| 2392 | // could only be discovered by removing side-effect-generating instructions |
| 2393 | // during the first pass. |
| 2394 | if (FLAG_smi_only_arrays && removed_side_effects) { |
| 2395 | gvn.Analyze(); |
| 2396 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 2399 | if (FLAG_use_range) { |
| 2400 | HRangeAnalysis rangeAnalysis(graph()); |
| 2401 | rangeAnalysis.Analyze(); |
| 2402 | } |
| 2403 | graph()->ComputeMinusZeroChecks(); |
| 2404 | |
| 2405 | // Eliminate redundant stack checks on backwards branches. |
| 2406 | HStackCheckEliminator sce(graph()); |
| 2407 | sce.Process(); |
| 2408 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2409 | // Replace the results of check instructions with the original value, if the |
| 2410 | // result is used. This is safe now, since we don't do code motion after this |
| 2411 | // point. It enables better register allocation since the value produced by |
| 2412 | // check instructions is really a copy of the original value. |
| 2413 | graph()->ReplaceCheckedValues(); |
| 2414 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2415 | return graph(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2419 | void HGraph::ReplaceCheckedValues() { |
| 2420 | HPhase phase("Replace checked values", this); |
| 2421 | for (int i = 0; i < blocks()->length(); ++i) { |
| 2422 | HInstruction* instr = blocks()->at(i)->first(); |
| 2423 | while (instr != NULL) { |
| 2424 | if (instr->IsBoundsCheck()) { |
| 2425 | // Replace all uses of the checked value with the original input. |
| 2426 | ASSERT(instr->UseCount() > 0); |
| 2427 | instr->ReplaceAllUsesWith(HBoundsCheck::cast(instr)->index()); |
| 2428 | } |
| 2429 | instr = instr->next(); |
| 2430 | } |
| 2431 | } |
| 2432 | } |
| 2433 | |
| 2434 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2435 | HInstruction* HGraphBuilder::AddInstruction(HInstruction* instr) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2436 | ASSERT(current_block() != NULL); |
| 2437 | current_block()->AddInstruction(instr); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2438 | return instr; |
| 2439 | } |
| 2440 | |
| 2441 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2442 | void HGraphBuilder::AddSimulate(int ast_id) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2443 | ASSERT(current_block() != NULL); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2444 | current_block()->AddSimulate(ast_id); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
| 2447 | |
| 2448 | void HGraphBuilder::AddPhi(HPhi* instr) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2449 | ASSERT(current_block() != NULL); |
| 2450 | current_block()->AddPhi(instr); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | |
| 2454 | void HGraphBuilder::PushAndAdd(HInstruction* instr) { |
| 2455 | Push(instr); |
| 2456 | AddInstruction(instr); |
| 2457 | } |
| 2458 | |
| 2459 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2460 | template <int V> |
| 2461 | HInstruction* HGraphBuilder::PreProcessCall(HCall<V>* call) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2462 | int count = call->argument_count(); |
| 2463 | ZoneList<HValue*> arguments(count); |
| 2464 | for (int i = 0; i < count; ++i) { |
| 2465 | arguments.Add(Pop()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2466 | } |
| 2467 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2468 | while (!arguments.is_empty()) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 2469 | AddInstruction(new(zone()) HPushArgument(arguments.RemoveLast())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2470 | } |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2471 | return call; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2475 | void HGraphBuilder::SetUpScope(Scope* scope) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 2476 | HConstant* undefined_constant = new(zone()) HConstant( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2477 | isolate()->factory()->undefined_value(), Representation::Tagged()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2478 | AddInstruction(undefined_constant); |
| 2479 | graph_->set_undefined_constant(undefined_constant); |
| 2480 | |
| 2481 | // Set the initial values of parameters including "this". "This" has |
| 2482 | // parameter index 0. |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 2483 | ASSERT_EQ(scope->num_parameters() + 1, environment()->parameter_count()); |
| 2484 | |
| 2485 | for (int i = 0; i < environment()->parameter_count(); ++i) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 2486 | HInstruction* parameter = AddInstruction(new(zone()) HParameter(i)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2487 | environment()->Bind(i, parameter); |
| 2488 | } |
| 2489 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 2490 | // First special is HContext. |
| 2491 | HInstruction* context = AddInstruction(new(zone()) HContext); |
| 2492 | environment()->BindContext(context); |
| 2493 | |
| 2494 | // Initialize specials and locals to undefined. |
| 2495 | for (int i = environment()->parameter_count() + 1; |
| 2496 | i < environment()->length(); |
| 2497 | ++i) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2498 | environment()->Bind(i, undefined_constant); |
| 2499 | } |
| 2500 | |
| 2501 | // Handle the arguments and arguments shadow variables specially (they do |
| 2502 | // not have declarations). |
| 2503 | if (scope->arguments() != NULL) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 2504 | if (!scope->arguments()->IsStackAllocated()) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2505 | return Bailout("context-allocated arguments"); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2506 | } |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 2507 | HArgumentsObject* object = new(zone()) HArgumentsObject; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2508 | AddInstruction(object); |
| 2509 | graph()->SetArgumentsObject(object); |
| 2510 | environment()->Bind(scope->arguments(), object); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2511 | } |
| 2512 | } |
| 2513 | |
| 2514 | |
| 2515 | void HGraphBuilder::VisitStatements(ZoneList<Statement*>* statements) { |
| 2516 | for (int i = 0; i < statements->length(); i++) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2517 | CHECK_ALIVE(Visit(statements->at(i))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2518 | } |
| 2519 | } |
| 2520 | |
| 2521 | |
| 2522 | HBasicBlock* HGraphBuilder::CreateBasicBlock(HEnvironment* env) { |
| 2523 | HBasicBlock* b = graph()->CreateBasicBlock(); |
| 2524 | b->SetInitialEnvironment(env); |
| 2525 | return b; |
| 2526 | } |
| 2527 | |
| 2528 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2529 | HBasicBlock* HGraphBuilder::CreateLoopHeaderBlock() { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2530 | HBasicBlock* header = graph()->CreateBasicBlock(); |
| 2531 | HEnvironment* entry_env = environment()->CopyAsLoopHeader(header); |
| 2532 | header->SetInitialEnvironment(entry_env); |
| 2533 | header->AttachLoopInformation(); |
| 2534 | return header; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
| 2537 | |
| 2538 | void HGraphBuilder::VisitBlock(Block* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2539 | ASSERT(!HasStackOverflow()); |
| 2540 | ASSERT(current_block() != NULL); |
| 2541 | ASSERT(current_block()->HasPredecessor()); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 2542 | if (stmt->block_scope() != NULL) { |
| 2543 | return Bailout("ScopedBlock"); |
| 2544 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2545 | BreakAndContinueInfo break_info(stmt); |
| 2546 | { BreakAndContinueScope push(&break_info, this); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2547 | CHECK_BAILOUT(VisitStatements(stmt->statements())); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2548 | } |
| 2549 | HBasicBlock* break_block = break_info.break_block(); |
| 2550 | if (break_block != NULL) { |
| 2551 | if (current_block() != NULL) current_block()->Goto(break_block); |
| 2552 | break_block->SetJoinId(stmt->ExitId()); |
| 2553 | set_current_block(break_block); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2554 | } |
| 2555 | } |
| 2556 | |
| 2557 | |
| 2558 | void HGraphBuilder::VisitExpressionStatement(ExpressionStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2559 | ASSERT(!HasStackOverflow()); |
| 2560 | ASSERT(current_block() != NULL); |
| 2561 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2562 | VisitForEffect(stmt->expression()); |
| 2563 | } |
| 2564 | |
| 2565 | |
| 2566 | void HGraphBuilder::VisitEmptyStatement(EmptyStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2567 | ASSERT(!HasStackOverflow()); |
| 2568 | ASSERT(current_block() != NULL); |
| 2569 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
| 2572 | |
| 2573 | void HGraphBuilder::VisitIfStatement(IfStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2574 | ASSERT(!HasStackOverflow()); |
| 2575 | ASSERT(current_block() != NULL); |
| 2576 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2577 | if (stmt->condition()->ToBooleanIsTrue()) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2578 | AddSimulate(stmt->ThenId()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2579 | Visit(stmt->then_statement()); |
| 2580 | } else if (stmt->condition()->ToBooleanIsFalse()) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2581 | AddSimulate(stmt->ElseId()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2582 | Visit(stmt->else_statement()); |
| 2583 | } else { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2584 | HBasicBlock* cond_true = graph()->CreateBasicBlock(); |
| 2585 | HBasicBlock* cond_false = graph()->CreateBasicBlock(); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2586 | CHECK_BAILOUT(VisitForControl(stmt->condition(), cond_true, cond_false)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2587 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2588 | if (cond_true->HasPredecessor()) { |
| 2589 | cond_true->SetJoinId(stmt->ThenId()); |
| 2590 | set_current_block(cond_true); |
| 2591 | CHECK_BAILOUT(Visit(stmt->then_statement())); |
| 2592 | cond_true = current_block(); |
| 2593 | } else { |
| 2594 | cond_true = NULL; |
| 2595 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2596 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2597 | if (cond_false->HasPredecessor()) { |
| 2598 | cond_false->SetJoinId(stmt->ElseId()); |
| 2599 | set_current_block(cond_false); |
| 2600 | CHECK_BAILOUT(Visit(stmt->else_statement())); |
| 2601 | cond_false = current_block(); |
| 2602 | } else { |
| 2603 | cond_false = NULL; |
| 2604 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2605 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 2606 | HBasicBlock* join = CreateJoin(cond_true, cond_false, stmt->IfId()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2607 | set_current_block(join); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2608 | } |
| 2609 | } |
| 2610 | |
| 2611 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2612 | HBasicBlock* HGraphBuilder::BreakAndContinueScope::Get( |
| 2613 | BreakableStatement* stmt, |
| 2614 | BreakType type) { |
| 2615 | BreakAndContinueScope* current = this; |
| 2616 | while (current != NULL && current->info()->target() != stmt) { |
| 2617 | current = current->next(); |
| 2618 | } |
| 2619 | ASSERT(current != NULL); // Always found (unless stack is malformed). |
| 2620 | HBasicBlock* block = NULL; |
| 2621 | switch (type) { |
| 2622 | case BREAK: |
| 2623 | block = current->info()->break_block(); |
| 2624 | if (block == NULL) { |
| 2625 | block = current->owner()->graph()->CreateBasicBlock(); |
| 2626 | current->info()->set_break_block(block); |
| 2627 | } |
| 2628 | break; |
| 2629 | |
| 2630 | case CONTINUE: |
| 2631 | block = current->info()->continue_block(); |
| 2632 | if (block == NULL) { |
| 2633 | block = current->owner()->graph()->CreateBasicBlock(); |
| 2634 | current->info()->set_continue_block(block); |
| 2635 | } |
| 2636 | break; |
| 2637 | } |
| 2638 | |
| 2639 | return block; |
| 2640 | } |
| 2641 | |
| 2642 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2643 | void HGraphBuilder::VisitContinueStatement(ContinueStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2644 | ASSERT(!HasStackOverflow()); |
| 2645 | ASSERT(current_block() != NULL); |
| 2646 | ASSERT(current_block()->HasPredecessor()); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2647 | HBasicBlock* continue_block = break_scope()->Get(stmt->target(), CONTINUE); |
| 2648 | current_block()->Goto(continue_block); |
| 2649 | set_current_block(NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | |
| 2653 | void HGraphBuilder::VisitBreakStatement(BreakStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2654 | ASSERT(!HasStackOverflow()); |
| 2655 | ASSERT(current_block() != NULL); |
| 2656 | ASSERT(current_block()->HasPredecessor()); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2657 | HBasicBlock* break_block = break_scope()->Get(stmt->target(), BREAK); |
| 2658 | current_block()->Goto(break_block); |
| 2659 | set_current_block(NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2660 | } |
| 2661 | |
| 2662 | |
| 2663 | void HGraphBuilder::VisitReturnStatement(ReturnStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2664 | ASSERT(!HasStackOverflow()); |
| 2665 | ASSERT(current_block() != NULL); |
| 2666 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2667 | AstContext* context = call_context(); |
| 2668 | if (context == NULL) { |
| 2669 | // Not an inlined return, so an actual one. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2670 | CHECK_ALIVE(VisitForValue(stmt->expression())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2671 | HValue* result = environment()->Pop(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 2672 | current_block()->FinishExit(new(zone()) HReturn(result)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 2673 | set_current_block(NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2674 | } else { |
| 2675 | // Return from an inlined function, visit the subexpression in the |
| 2676 | // expression context of the call. |
| 2677 | if (context->IsTest()) { |
| 2678 | TestContext* test = TestContext::cast(context); |
| 2679 | VisitForControl(stmt->expression(), |
| 2680 | test->if_true(), |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2681 | test->if_false()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2682 | } else if (context->IsEffect()) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2683 | CHECK_ALIVE(VisitForEffect(stmt->expression())); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2684 | current_block()->Goto(function_return(), function_state()->drop_extra()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2685 | } else { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2686 | ASSERT(context->IsValue()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2687 | CHECK_ALIVE(VisitForValue(stmt->expression())); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2688 | HValue* return_value = environment()->Pop(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2689 | current_block()->AddLeaveInlined(return_value, |
| 2690 | function_return(), |
| 2691 | function_state()->drop_extra()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2692 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2693 | set_current_block(NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2694 | } |
| 2695 | } |
| 2696 | |
| 2697 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 2698 | void HGraphBuilder::VisitWithStatement(WithStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2699 | ASSERT(!HasStackOverflow()); |
| 2700 | ASSERT(current_block() != NULL); |
| 2701 | ASSERT(current_block()->HasPredecessor()); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 2702 | return Bailout("WithStatement"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
| 2705 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2706 | void HGraphBuilder::VisitSwitchStatement(SwitchStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2707 | ASSERT(!HasStackOverflow()); |
| 2708 | ASSERT(current_block() != NULL); |
| 2709 | ASSERT(current_block()->HasPredecessor()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2710 | // We only optimize switch statements with smi-literal smi comparisons, |
| 2711 | // with a bounded number of clauses. |
| 2712 | const int kCaseClauseLimit = 128; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2713 | ZoneList<CaseClause*>* clauses = stmt->cases(); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2714 | int clause_count = clauses->length(); |
| 2715 | if (clause_count > kCaseClauseLimit) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2716 | return Bailout("SwitchStatement: too many clauses"); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2717 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2718 | |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 2719 | HValue* context = environment()->LookupContext(); |
| 2720 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2721 | CHECK_ALIVE(VisitForValue(stmt->tag())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2722 | AddSimulate(stmt->EntryId()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2723 | HValue* tag_value = Pop(); |
| 2724 | HBasicBlock* first_test_block = current_block(); |
| 2725 | |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 2726 | SwitchType switch_type = UNKNOWN_SWITCH; |
| 2727 | |
| 2728 | // 1. Extract clause type |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2729 | for (int i = 0; i < clause_count; ++i) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2730 | CaseClause* clause = clauses->at(i); |
| 2731 | if (clause->is_default()) continue; |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 2732 | |
| 2733 | if (switch_type == UNKNOWN_SWITCH) { |
| 2734 | if (clause->label()->IsSmiLiteral()) { |
| 2735 | switch_type = SMI_SWITCH; |
| 2736 | } else if (clause->label()->IsStringLiteral()) { |
| 2737 | switch_type = STRING_SWITCH; |
| 2738 | } else { |
| 2739 | return Bailout("SwitchStatement: non-literal switch label"); |
| 2740 | } |
| 2741 | } else if ((switch_type == STRING_SWITCH && |
| 2742 | !clause->label()->IsStringLiteral()) || |
| 2743 | (switch_type == SMI_SWITCH && |
| 2744 | !clause->label()->IsSmiLiteral())) { |
| 2745 | return Bailout("SwitchStatemnt: mixed label types are not supported"); |
| 2746 | } |
| 2747 | } |
| 2748 | |
| 2749 | HUnaryControlInstruction* string_check = NULL; |
| 2750 | HBasicBlock* not_string_block = NULL; |
| 2751 | |
| 2752 | // Test switch's tag value if all clauses are string literals |
| 2753 | if (switch_type == STRING_SWITCH) { |
| 2754 | string_check = new(zone()) HIsStringAndBranch(tag_value); |
| 2755 | first_test_block = graph()->CreateBasicBlock(); |
| 2756 | not_string_block = graph()->CreateBasicBlock(); |
| 2757 | |
| 2758 | string_check->SetSuccessorAt(0, first_test_block); |
| 2759 | string_check->SetSuccessorAt(1, not_string_block); |
| 2760 | current_block()->Finish(string_check); |
| 2761 | |
| 2762 | set_current_block(first_test_block); |
| 2763 | } |
| 2764 | |
| 2765 | // 2. Build all the tests, with dangling true branches |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2766 | int default_id = AstNode::kNoNumber; |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 2767 | for (int i = 0; i < clause_count; ++i) { |
| 2768 | CaseClause* clause = clauses->at(i); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2769 | if (clause->is_default()) { |
| 2770 | default_id = clause->EntryId(); |
| 2771 | continue; |
| 2772 | } |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 2773 | if (switch_type == SMI_SWITCH) { |
| 2774 | clause->RecordTypeFeedback(oracle()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2775 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2776 | |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 2777 | // Generate a compare and branch. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2778 | CHECK_ALIVE(VisitForValue(clause->label())); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2779 | HValue* label_value = Pop(); |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 2780 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2781 | HBasicBlock* next_test_block = graph()->CreateBasicBlock(); |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 2782 | HBasicBlock* body_block = graph()->CreateBasicBlock(); |
| 2783 | |
| 2784 | HControlInstruction* compare; |
| 2785 | |
| 2786 | if (switch_type == SMI_SWITCH) { |
| 2787 | if (!clause->IsSmiCompare()) { |
| 2788 | // Finish with deoptimize and add uses of enviroment values to |
| 2789 | // account for invisible uses. |
| 2790 | current_block()->FinishExitWithDeoptimization(HDeoptimize::kUseAll); |
| 2791 | set_current_block(NULL); |
| 2792 | break; |
| 2793 | } |
| 2794 | |
| 2795 | HCompareIDAndBranch* compare_ = |
| 2796 | new(zone()) HCompareIDAndBranch(tag_value, |
| 2797 | label_value, |
| 2798 | Token::EQ_STRICT); |
| 2799 | compare_->SetInputRepresentation(Representation::Integer32()); |
| 2800 | compare = compare_; |
| 2801 | } else { |
| 2802 | compare = new(zone()) HStringCompareAndBranch(context, tag_value, |
| 2803 | label_value, |
| 2804 | Token::EQ_STRICT); |
| 2805 | } |
| 2806 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2807 | compare->SetSuccessorAt(0, body_block); |
| 2808 | compare->SetSuccessorAt(1, next_test_block); |
| 2809 | current_block()->Finish(compare); |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 2810 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2811 | set_current_block(next_test_block); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2812 | } |
| 2813 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2814 | // Save the current block to use for the default or to join with the |
| 2815 | // exit. This block is NULL if we deoptimized. |
| 2816 | HBasicBlock* last_block = current_block(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2817 | |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 2818 | if (not_string_block != NULL) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2819 | int join_id = (default_id != AstNode::kNoNumber) |
| 2820 | ? default_id |
| 2821 | : stmt->ExitId(); |
| 2822 | last_block = CreateJoin(last_block, not_string_block, join_id); |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
| 2825 | // 3. Loop over the clauses and the linked list of tests in lockstep, |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2826 | // translating the clause bodies. |
| 2827 | HBasicBlock* curr_test_block = first_test_block; |
| 2828 | HBasicBlock* fall_through_block = NULL; |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 2829 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2830 | BreakAndContinueInfo break_info(stmt); |
| 2831 | { BreakAndContinueScope push(&break_info, this); |
| 2832 | for (int i = 0; i < clause_count; ++i) { |
| 2833 | CaseClause* clause = clauses->at(i); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2834 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2835 | // Identify the block where normal (non-fall-through) control flow |
| 2836 | // goes to. |
| 2837 | HBasicBlock* normal_block = NULL; |
fschneider@chromium.org | 13da64d | 2011-05-18 12:07:24 +0000 | [diff] [blame] | 2838 | if (clause->is_default()) { |
| 2839 | if (last_block != NULL) { |
| 2840 | normal_block = last_block; |
| 2841 | last_block = NULL; // Cleared to indicate we've handled it. |
| 2842 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2843 | } else if (!curr_test_block->end()->IsDeoptimize()) { |
| 2844 | normal_block = curr_test_block->end()->FirstSuccessor(); |
| 2845 | curr_test_block = curr_test_block->end()->SecondSuccessor(); |
| 2846 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2847 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2848 | // Identify a block to emit the body into. |
| 2849 | if (normal_block == NULL) { |
| 2850 | if (fall_through_block == NULL) { |
| 2851 | // (a) Unreachable. |
| 2852 | if (clause->is_default()) { |
| 2853 | continue; // Might still be reachable clause bodies. |
| 2854 | } else { |
| 2855 | break; |
| 2856 | } |
| 2857 | } else { |
| 2858 | // (b) Reachable only as fall through. |
| 2859 | set_current_block(fall_through_block); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2860 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2861 | } else if (fall_through_block == NULL) { |
| 2862 | // (c) Reachable only normally. |
| 2863 | set_current_block(normal_block); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2864 | } else { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2865 | // (d) Reachable both ways. |
| 2866 | HBasicBlock* join = CreateJoin(fall_through_block, |
| 2867 | normal_block, |
| 2868 | clause->EntryId()); |
| 2869 | set_current_block(join); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2870 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2871 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2872 | CHECK_BAILOUT(VisitStatements(clause->statements())); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2873 | fall_through_block = current_block(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2874 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2877 | // Create an up-to-3-way join. Use the break block if it exists since |
| 2878 | // it's already a join block. |
| 2879 | HBasicBlock* break_block = break_info.break_block(); |
| 2880 | if (break_block == NULL) { |
| 2881 | set_current_block(CreateJoin(fall_through_block, |
| 2882 | last_block, |
| 2883 | stmt->ExitId())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2884 | } else { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2885 | if (fall_through_block != NULL) fall_through_block->Goto(break_block); |
| 2886 | if (last_block != NULL) last_block->Goto(break_block); |
| 2887 | break_block->SetJoinId(stmt->ExitId()); |
| 2888 | set_current_block(break_block); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2889 | } |
| 2890 | } |
| 2891 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 2892 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2893 | bool HGraphBuilder::HasOsrEntryAt(IterationStatement* statement) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2894 | return statement->OsrEntryId() == info()->osr_ast_id(); |
| 2895 | } |
| 2896 | |
| 2897 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 2898 | void HGraphBuilder::PreProcessOsrEntry(IterationStatement* statement) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2899 | if (!HasOsrEntryAt(statement)) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2900 | |
| 2901 | HBasicBlock* non_osr_entry = graph()->CreateBasicBlock(); |
| 2902 | HBasicBlock* osr_entry = graph()->CreateBasicBlock(); |
| 2903 | HValue* true_value = graph()->GetConstantTrue(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2904 | HBranch* test = new(zone()) HBranch(true_value, non_osr_entry, osr_entry); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 2905 | current_block()->Finish(test); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2906 | |
| 2907 | HBasicBlock* loop_predecessor = graph()->CreateBasicBlock(); |
| 2908 | non_osr_entry->Goto(loop_predecessor); |
| 2909 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 2910 | set_current_block(osr_entry); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2911 | int osr_entry_id = statement->OsrEntryId(); |
| 2912 | // We want the correct environment at the OsrEntry instruction. Build |
| 2913 | // it explicitly. The expression stack should be empty. |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 2914 | ASSERT(environment()->ExpressionStackIsEmpty()); |
| 2915 | for (int i = 0; i < environment()->length(); ++i) { |
| 2916 | HUnknownOSRValue* osr_value = new(zone()) HUnknownOSRValue; |
| 2917 | AddInstruction(osr_value); |
| 2918 | environment()->Bind(i, osr_value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 2921 | AddSimulate(osr_entry_id); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 2922 | AddInstruction(new(zone()) HOsrEntry(osr_entry_id)); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 2923 | HContext* context = new(zone()) HContext; |
| 2924 | AddInstruction(context); |
| 2925 | environment()->BindContext(context); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 2926 | current_block()->Goto(loop_predecessor); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2927 | loop_predecessor->SetJoinId(statement->EntryId()); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 2928 | set_current_block(loop_predecessor); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 2932 | void HGraphBuilder::VisitLoopBody(IterationStatement* stmt, |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2933 | HBasicBlock* loop_entry, |
| 2934 | BreakAndContinueInfo* break_info) { |
| 2935 | BreakAndContinueScope push(break_info, this); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 2936 | AddSimulate(stmt->StackCheckId()); |
| 2937 | HValue* context = environment()->LookupContext(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2938 | HStackCheck* stack_check = |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 2939 | new(zone()) HStackCheck(context, HStackCheck::kBackwardsBranch); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2940 | AddInstruction(stack_check); |
| 2941 | ASSERT(loop_entry->IsLoopHeader()); |
| 2942 | loop_entry->loop_information()->set_stack_check(stack_check); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 2943 | CHECK_BAILOUT(Visit(stmt->body())); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2944 | } |
| 2945 | |
| 2946 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2947 | void HGraphBuilder::VisitDoWhileStatement(DoWhileStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2948 | ASSERT(!HasStackOverflow()); |
| 2949 | ASSERT(current_block() != NULL); |
| 2950 | ASSERT(current_block()->HasPredecessor()); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2951 | ASSERT(current_block() != NULL); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 2952 | PreProcessOsrEntry(stmt); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2953 | HBasicBlock* loop_entry = CreateLoopHeaderBlock(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2954 | current_block()->Goto(loop_entry); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2955 | set_current_block(loop_entry); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2956 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2957 | BreakAndContinueInfo break_info(stmt); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 2958 | CHECK_BAILOUT(VisitLoopBody(stmt, loop_entry, &break_info)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2959 | HBasicBlock* body_exit = |
| 2960 | JoinContinue(stmt, current_block(), break_info.continue_block()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2961 | HBasicBlock* loop_successor = NULL; |
| 2962 | if (body_exit != NULL && !stmt->cond()->ToBooleanIsTrue()) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2963 | set_current_block(body_exit); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2964 | // The block for a true condition, the actual predecessor block of the |
| 2965 | // back edge. |
| 2966 | body_exit = graph()->CreateBasicBlock(); |
| 2967 | loop_successor = graph()->CreateBasicBlock(); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2968 | CHECK_BAILOUT(VisitForControl(stmt->cond(), body_exit, loop_successor)); |
| 2969 | if (body_exit->HasPredecessor()) { |
| 2970 | body_exit->SetJoinId(stmt->BackEdgeId()); |
| 2971 | } else { |
| 2972 | body_exit = NULL; |
| 2973 | } |
| 2974 | if (loop_successor->HasPredecessor()) { |
| 2975 | loop_successor->SetJoinId(stmt->ExitId()); |
| 2976 | } else { |
| 2977 | loop_successor = NULL; |
| 2978 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2979 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2980 | HBasicBlock* loop_exit = CreateLoop(stmt, |
| 2981 | loop_entry, |
| 2982 | body_exit, |
| 2983 | loop_successor, |
| 2984 | break_info.break_block()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2985 | set_current_block(loop_exit); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2986 | } |
| 2987 | |
| 2988 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2989 | void HGraphBuilder::VisitWhileStatement(WhileStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2990 | ASSERT(!HasStackOverflow()); |
| 2991 | ASSERT(current_block() != NULL); |
| 2992 | ASSERT(current_block()->HasPredecessor()); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2993 | ASSERT(current_block() != NULL); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 2994 | PreProcessOsrEntry(stmt); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2995 | HBasicBlock* loop_entry = CreateLoopHeaderBlock(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2996 | current_block()->Goto(loop_entry); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2997 | set_current_block(loop_entry); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2998 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2999 | // If the condition is constant true, do not generate a branch. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3000 | HBasicBlock* loop_successor = NULL; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3001 | if (!stmt->cond()->ToBooleanIsTrue()) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3002 | HBasicBlock* body_entry = graph()->CreateBasicBlock(); |
| 3003 | loop_successor = graph()->CreateBasicBlock(); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3004 | CHECK_BAILOUT(VisitForControl(stmt->cond(), body_entry, loop_successor)); |
| 3005 | if (body_entry->HasPredecessor()) { |
| 3006 | body_entry->SetJoinId(stmt->BodyId()); |
| 3007 | set_current_block(body_entry); |
| 3008 | } |
| 3009 | if (loop_successor->HasPredecessor()) { |
| 3010 | loop_successor->SetJoinId(stmt->ExitId()); |
| 3011 | } else { |
| 3012 | loop_successor = NULL; |
| 3013 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3014 | } |
| 3015 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3016 | BreakAndContinueInfo break_info(stmt); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3017 | if (current_block() != NULL) { |
| 3018 | BreakAndContinueScope push(&break_info, this); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 3019 | CHECK_BAILOUT(VisitLoopBody(stmt, loop_entry, &break_info)); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3020 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3021 | HBasicBlock* body_exit = |
| 3022 | JoinContinue(stmt, current_block(), break_info.continue_block()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3023 | HBasicBlock* loop_exit = CreateLoop(stmt, |
| 3024 | loop_entry, |
| 3025 | body_exit, |
| 3026 | loop_successor, |
| 3027 | break_info.break_block()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3028 | set_current_block(loop_exit); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
| 3031 | |
| 3032 | void HGraphBuilder::VisitForStatement(ForStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3033 | ASSERT(!HasStackOverflow()); |
| 3034 | ASSERT(current_block() != NULL); |
| 3035 | ASSERT(current_block()->HasPredecessor()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3036 | if (stmt->init() != NULL) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3037 | CHECK_ALIVE(Visit(stmt->init())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3038 | } |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3039 | ASSERT(current_block() != NULL); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 3040 | PreProcessOsrEntry(stmt); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3041 | HBasicBlock* loop_entry = CreateLoopHeaderBlock(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3042 | current_block()->Goto(loop_entry); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3043 | set_current_block(loop_entry); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3044 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3045 | HBasicBlock* loop_successor = NULL; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3046 | if (stmt->cond() != NULL) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3047 | HBasicBlock* body_entry = graph()->CreateBasicBlock(); |
| 3048 | loop_successor = graph()->CreateBasicBlock(); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3049 | CHECK_BAILOUT(VisitForControl(stmt->cond(), body_entry, loop_successor)); |
| 3050 | if (body_entry->HasPredecessor()) { |
| 3051 | body_entry->SetJoinId(stmt->BodyId()); |
| 3052 | set_current_block(body_entry); |
| 3053 | } |
| 3054 | if (loop_successor->HasPredecessor()) { |
| 3055 | loop_successor->SetJoinId(stmt->ExitId()); |
| 3056 | } else { |
| 3057 | loop_successor = NULL; |
| 3058 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3059 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3060 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3061 | BreakAndContinueInfo break_info(stmt); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3062 | if (current_block() != NULL) { |
| 3063 | BreakAndContinueScope push(&break_info, this); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 3064 | CHECK_BAILOUT(VisitLoopBody(stmt, loop_entry, &break_info)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3065 | } |
| 3066 | HBasicBlock* body_exit = |
| 3067 | JoinContinue(stmt, current_block(), break_info.continue_block()); |
| 3068 | |
| 3069 | if (stmt->next() != NULL && body_exit != NULL) { |
| 3070 | set_current_block(body_exit); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3071 | CHECK_BAILOUT(Visit(stmt->next())); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3072 | body_exit = current_block(); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3073 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3074 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3075 | HBasicBlock* loop_exit = CreateLoop(stmt, |
| 3076 | loop_entry, |
| 3077 | body_exit, |
| 3078 | loop_successor, |
| 3079 | break_info.break_block()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3080 | set_current_block(loop_exit); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
| 3083 | |
| 3084 | void HGraphBuilder::VisitForInStatement(ForInStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3085 | ASSERT(!HasStackOverflow()); |
| 3086 | ASSERT(current_block() != NULL); |
| 3087 | ASSERT(current_block()->HasPredecessor()); |
| 3088 | return Bailout("ForInStatement"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
| 3091 | |
| 3092 | void HGraphBuilder::VisitTryCatchStatement(TryCatchStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3093 | ASSERT(!HasStackOverflow()); |
| 3094 | ASSERT(current_block() != NULL); |
| 3095 | ASSERT(current_block()->HasPredecessor()); |
| 3096 | return Bailout("TryCatchStatement"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | |
| 3100 | void HGraphBuilder::VisitTryFinallyStatement(TryFinallyStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3101 | ASSERT(!HasStackOverflow()); |
| 3102 | ASSERT(current_block() != NULL); |
| 3103 | ASSERT(current_block()->HasPredecessor()); |
| 3104 | return Bailout("TryFinallyStatement"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3105 | } |
| 3106 | |
| 3107 | |
| 3108 | void HGraphBuilder::VisitDebuggerStatement(DebuggerStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3109 | ASSERT(!HasStackOverflow()); |
| 3110 | ASSERT(current_block() != NULL); |
| 3111 | ASSERT(current_block()->HasPredecessor()); |
| 3112 | return Bailout("DebuggerStatement"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3113 | } |
| 3114 | |
| 3115 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3116 | static Handle<SharedFunctionInfo> SearchSharedFunctionInfo( |
| 3117 | Code* unoptimized_code, FunctionLiteral* expr) { |
| 3118 | int start_position = expr->start_position(); |
| 3119 | RelocIterator it(unoptimized_code); |
| 3120 | for (;!it.done(); it.next()) { |
| 3121 | RelocInfo* rinfo = it.rinfo(); |
| 3122 | if (rinfo->rmode() != RelocInfo::EMBEDDED_OBJECT) continue; |
| 3123 | Object* obj = rinfo->target_object(); |
| 3124 | if (obj->IsSharedFunctionInfo()) { |
| 3125 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(obj); |
| 3126 | if (shared->start_position() == start_position) { |
| 3127 | return Handle<SharedFunctionInfo>(shared); |
| 3128 | } |
| 3129 | } |
| 3130 | } |
| 3131 | |
| 3132 | return Handle<SharedFunctionInfo>(); |
| 3133 | } |
| 3134 | |
| 3135 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3136 | void HGraphBuilder::VisitFunctionLiteral(FunctionLiteral* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3137 | ASSERT(!HasStackOverflow()); |
| 3138 | ASSERT(current_block() != NULL); |
| 3139 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3140 | Handle<SharedFunctionInfo> shared_info = |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3141 | SearchSharedFunctionInfo(info()->shared_info()->code(), |
| 3142 | expr); |
| 3143 | if (shared_info.is_null()) { |
| 3144 | shared_info = Compiler::BuildFunctionInfo(expr, info()->script()); |
| 3145 | } |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3146 | // We also have a stack overflow if the recursive compilation did. |
| 3147 | if (HasStackOverflow()) return; |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 3148 | HValue* context = environment()->LookupContext(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3149 | HFunctionLiteral* instr = |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 3150 | new(zone()) HFunctionLiteral(context, shared_info, expr->pretenure()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 3151 | return ast_context()->ReturnInstruction(instr, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | |
| 3155 | void HGraphBuilder::VisitSharedFunctionInfoLiteral( |
| 3156 | SharedFunctionInfoLiteral* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3157 | ASSERT(!HasStackOverflow()); |
| 3158 | ASSERT(current_block() != NULL); |
| 3159 | ASSERT(current_block()->HasPredecessor()); |
| 3160 | return Bailout("SharedFunctionInfoLiteral"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3161 | } |
| 3162 | |
| 3163 | |
| 3164 | void HGraphBuilder::VisitConditional(Conditional* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3165 | ASSERT(!HasStackOverflow()); |
| 3166 | ASSERT(current_block() != NULL); |
| 3167 | ASSERT(current_block()->HasPredecessor()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3168 | HBasicBlock* cond_true = graph()->CreateBasicBlock(); |
| 3169 | HBasicBlock* cond_false = graph()->CreateBasicBlock(); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3170 | CHECK_BAILOUT(VisitForControl(expr->condition(), cond_true, cond_false)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3171 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3172 | // Visit the true and false subexpressions in the same AST context as the |
| 3173 | // whole expression. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3174 | if (cond_true->HasPredecessor()) { |
| 3175 | cond_true->SetJoinId(expr->ThenId()); |
| 3176 | set_current_block(cond_true); |
| 3177 | CHECK_BAILOUT(Visit(expr->then_expression())); |
| 3178 | cond_true = current_block(); |
| 3179 | } else { |
| 3180 | cond_true = NULL; |
| 3181 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3182 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3183 | if (cond_false->HasPredecessor()) { |
| 3184 | cond_false->SetJoinId(expr->ElseId()); |
| 3185 | set_current_block(cond_false); |
| 3186 | CHECK_BAILOUT(Visit(expr->else_expression())); |
| 3187 | cond_false = current_block(); |
| 3188 | } else { |
| 3189 | cond_false = NULL; |
| 3190 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3191 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3192 | if (!ast_context()->IsTest()) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3193 | HBasicBlock* join = CreateJoin(cond_true, cond_false, expr->id()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3194 | set_current_block(join); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3195 | if (join != NULL && !ast_context()->IsEffect()) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 3196 | return ast_context()->ReturnValue(Pop()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3197 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3198 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
| 3201 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3202 | HGraphBuilder::GlobalPropertyAccess HGraphBuilder::LookupGlobalProperty( |
| 3203 | Variable* var, LookupResult* lookup, bool is_store) { |
| 3204 | if (var->is_this() || !info()->has_global_object()) { |
| 3205 | return kUseGeneric; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3206 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3207 | Handle<GlobalObject> global(info()->global_object()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3208 | global->Lookup(*var->name(), lookup); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 3209 | if (!lookup->IsFound() || |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3210 | lookup->type() != NORMAL || |
| 3211 | (is_store && lookup->IsReadOnly()) || |
| 3212 | lookup->holder() != *global) { |
| 3213 | return kUseGeneric; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3214 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3215 | |
| 3216 | return kUseCell; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
| 3219 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3220 | HValue* HGraphBuilder::BuildContextChainWalk(Variable* var) { |
| 3221 | ASSERT(var->IsContextSlot()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 3222 | HValue* context = environment()->LookupContext(); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3223 | int length = info()->scope()->ContextChainLength(var->scope()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3224 | while (length-- > 0) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 3225 | HInstruction* context_instruction = new(zone()) HOuterContext(context); |
| 3226 | AddInstruction(context_instruction); |
| 3227 | context = context_instruction; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3228 | } |
| 3229 | return context; |
| 3230 | } |
| 3231 | |
| 3232 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3233 | void HGraphBuilder::VisitVariableProxy(VariableProxy* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3234 | ASSERT(!HasStackOverflow()); |
| 3235 | ASSERT(current_block() != NULL); |
| 3236 | ASSERT(current_block()->HasPredecessor()); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3237 | Variable* variable = expr->var(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3238 | switch (variable->location()) { |
| 3239 | case Variable::UNALLOCATED: { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3240 | if (variable->mode() == LET || variable->mode() == CONST_HARMONY) { |
| 3241 | return Bailout("reference to global harmony declared variable"); |
| 3242 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3243 | // Handle known global constants like 'undefined' specially to avoid a |
| 3244 | // load from a global cell for them. |
| 3245 | Handle<Object> constant_value = |
| 3246 | isolate()->factory()->GlobalConstantFor(variable->name()); |
| 3247 | if (!constant_value.is_null()) { |
| 3248 | HConstant* instr = |
| 3249 | new(zone()) HConstant(constant_value, Representation::Tagged()); |
| 3250 | return ast_context()->ReturnInstruction(instr, expr->id()); |
| 3251 | } |
| 3252 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3253 | LookupResult lookup(isolate()); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3254 | GlobalPropertyAccess type = |
| 3255 | LookupGlobalProperty(variable, &lookup, false); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3256 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3257 | if (type == kUseCell && |
| 3258 | info()->global_object()->IsAccessCheckNeeded()) { |
| 3259 | type = kUseGeneric; |
| 3260 | } |
| 3261 | |
| 3262 | if (type == kUseCell) { |
| 3263 | Handle<GlobalObject> global(info()->global_object()); |
| 3264 | Handle<JSGlobalPropertyCell> cell(global->GetPropertyCell(&lookup)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3265 | HLoadGlobalCell* instr = |
| 3266 | new(zone()) HLoadGlobalCell(cell, lookup.GetPropertyDetails()); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3267 | return ast_context()->ReturnInstruction(instr, expr->id()); |
| 3268 | } else { |
| 3269 | HValue* context = environment()->LookupContext(); |
| 3270 | HGlobalObject* global_object = new(zone()) HGlobalObject(context); |
| 3271 | AddInstruction(global_object); |
| 3272 | HLoadGlobalGeneric* instr = |
| 3273 | new(zone()) HLoadGlobalGeneric(context, |
| 3274 | global_object, |
| 3275 | variable->name(), |
| 3276 | ast_context()->is_for_typeof()); |
| 3277 | instr->set_position(expr->position()); |
| 3278 | return ast_context()->ReturnInstruction(instr, expr->id()); |
| 3279 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3280 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3281 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3282 | case Variable::PARAMETER: |
| 3283 | case Variable::LOCAL: { |
| 3284 | HValue* value = environment()->Lookup(variable); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3285 | if (value == graph()->GetConstantHole()) { |
| 3286 | ASSERT(variable->mode() == CONST || |
| 3287 | variable->mode() == CONST_HARMONY || |
| 3288 | variable->mode() == LET); |
| 3289 | return Bailout("reference to uninitialized variable"); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3290 | } |
| 3291 | return ast_context()->ReturnValue(value); |
| 3292 | } |
| 3293 | |
| 3294 | case Variable::CONTEXT: { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3295 | HValue* context = BuildContextChainWalk(variable); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3296 | HLoadContextSlot* instr = new(zone()) HLoadContextSlot(context, variable); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 3297 | return ast_context()->ReturnInstruction(instr, expr->id()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3298 | } |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3299 | |
| 3300 | case Variable::LOOKUP: |
| 3301 | return Bailout("reference to a variable which requires dynamic lookup"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | |
| 3306 | void HGraphBuilder::VisitLiteral(Literal* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3307 | ASSERT(!HasStackOverflow()); |
| 3308 | ASSERT(current_block() != NULL); |
| 3309 | ASSERT(current_block()->HasPredecessor()); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 3310 | HConstant* instr = |
| 3311 | new(zone()) HConstant(expr->handle(), Representation::Tagged()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 3312 | return ast_context()->ReturnInstruction(instr, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3313 | } |
| 3314 | |
| 3315 | |
| 3316 | void HGraphBuilder::VisitRegExpLiteral(RegExpLiteral* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3317 | ASSERT(!HasStackOverflow()); |
| 3318 | ASSERT(current_block() != NULL); |
| 3319 | ASSERT(current_block()->HasPredecessor()); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 3320 | HValue* context = environment()->LookupContext(); |
| 3321 | |
| 3322 | HRegExpLiteral* instr = new(zone()) HRegExpLiteral(context, |
| 3323 | expr->pattern(), |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 3324 | expr->flags(), |
| 3325 | expr->literal_index()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 3326 | return ast_context()->ReturnInstruction(instr, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
| 3329 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3330 | // Determines whether the given object literal boilerplate satisfies all |
| 3331 | // limits to be considered for fast deep-copying and computes the total |
| 3332 | // size of all objects that are part of the graph. |
| 3333 | static bool IsFastObjectLiteral(Handle<JSObject> boilerplate, |
| 3334 | int max_depth, |
| 3335 | int* max_properties, |
| 3336 | int* total_size) { |
| 3337 | if (max_depth <= 0) return false; |
| 3338 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3339 | Handle<FixedArrayBase> elements(boilerplate->elements()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3340 | if (elements->length() > 0 && |
| 3341 | elements->map() != HEAP->fixed_cow_array_map()) { |
| 3342 | return false; |
| 3343 | } |
| 3344 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3345 | Handle<FixedArray> properties(boilerplate->properties()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3346 | if (properties->length() > 0) { |
| 3347 | return false; |
| 3348 | } else { |
| 3349 | int nof = boilerplate->map()->inobject_properties(); |
| 3350 | for (int i = 0; i < nof; i++) { |
| 3351 | if ((*max_properties)-- <= 0) return false; |
| 3352 | Handle<Object> value(boilerplate->InObjectPropertyAt(i)); |
| 3353 | if (value->IsJSObject()) { |
| 3354 | Handle<JSObject> value_object = Handle<JSObject>::cast(value); |
| 3355 | if (!IsFastObjectLiteral(value_object, |
| 3356 | max_depth - 1, |
| 3357 | max_properties, |
| 3358 | total_size)) { |
| 3359 | return false; |
| 3360 | } |
| 3361 | } |
| 3362 | } |
| 3363 | } |
| 3364 | |
| 3365 | *total_size += boilerplate->map()->instance_size(); |
| 3366 | return true; |
| 3367 | } |
| 3368 | |
| 3369 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3370 | void HGraphBuilder::VisitObjectLiteral(ObjectLiteral* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3371 | ASSERT(!HasStackOverflow()); |
| 3372 | ASSERT(current_block() != NULL); |
| 3373 | ASSERT(current_block()->HasPredecessor()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3374 | Handle<JSFunction> closure = function_state()->compilation_info()->closure(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 3375 | HValue* context = environment()->LookupContext(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3376 | HInstruction* literal; |
| 3377 | |
| 3378 | // Check whether to use fast or slow deep-copying for boilerplate. |
| 3379 | int total_size = 0; |
| 3380 | int max_properties = HObjectLiteralFast::kMaxObjectLiteralProperties; |
| 3381 | Handle<Object> boilerplate(closure->literals()->get(expr->literal_index())); |
| 3382 | if (boilerplate->IsJSObject() && |
| 3383 | IsFastObjectLiteral(Handle<JSObject>::cast(boilerplate), |
| 3384 | HObjectLiteralFast::kMaxObjectLiteralDepth, |
| 3385 | &max_properties, |
| 3386 | &total_size)) { |
| 3387 | Handle<JSObject> boilerplate_object = Handle<JSObject>::cast(boilerplate); |
| 3388 | literal = new(zone()) HObjectLiteralFast(context, |
| 3389 | boilerplate_object, |
| 3390 | total_size, |
| 3391 | expr->literal_index(), |
| 3392 | expr->depth()); |
| 3393 | } else { |
| 3394 | literal = new(zone()) HObjectLiteralGeneric(context, |
| 3395 | expr->constant_properties(), |
| 3396 | expr->fast_elements(), |
| 3397 | expr->literal_index(), |
| 3398 | expr->depth(), |
| 3399 | expr->has_function()); |
| 3400 | } |
| 3401 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3402 | // The object is expected in the bailout environment during computation |
| 3403 | // of the property values and is the value of the entire expression. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3404 | PushAndAdd(literal); |
| 3405 | |
| 3406 | expr->CalculateEmitStore(); |
| 3407 | |
| 3408 | for (int i = 0; i < expr->properties()->length(); i++) { |
| 3409 | ObjectLiteral::Property* property = expr->properties()->at(i); |
| 3410 | if (property->IsCompileTimeValue()) continue; |
| 3411 | |
| 3412 | Literal* key = property->key(); |
| 3413 | Expression* value = property->value(); |
| 3414 | |
| 3415 | switch (property->kind()) { |
| 3416 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 3417 | ASSERT(!CompileTimeValue::IsCompileTimeValue(value)); |
| 3418 | // Fall through. |
| 3419 | case ObjectLiteral::Property::COMPUTED: |
| 3420 | if (key->handle()->IsSymbol()) { |
| 3421 | if (property->emit_store()) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3422 | CHECK_ALIVE(VisitForValue(value)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3423 | HValue* value = Pop(); |
| 3424 | Handle<String> name = Handle<String>::cast(key->handle()); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3425 | HStoreNamedGeneric* store = |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 3426 | new(zone()) HStoreNamedGeneric( |
| 3427 | context, |
| 3428 | literal, |
| 3429 | name, |
| 3430 | value, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3431 | function_strict_mode_flag()); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3432 | AddInstruction(store); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3433 | AddSimulate(key->id()); |
| 3434 | } else { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3435 | CHECK_ALIVE(VisitForEffect(value)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3436 | } |
| 3437 | break; |
| 3438 | } |
| 3439 | // Fall through. |
| 3440 | case ObjectLiteral::Property::PROTOTYPE: |
| 3441 | case ObjectLiteral::Property::SETTER: |
| 3442 | case ObjectLiteral::Property::GETTER: |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3443 | return Bailout("Object literal with complex property"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3444 | default: UNREACHABLE(); |
| 3445 | } |
| 3446 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3447 | |
| 3448 | if (expr->has_function()) { |
| 3449 | // Return the result of the transformation to fast properties |
| 3450 | // instead of the original since this operation changes the map |
| 3451 | // of the object. This makes sure that the original object won't |
| 3452 | // be used by other optimized code before it is transformed |
| 3453 | // (e.g. because of code motion). |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 3454 | HToFastProperties* result = new(zone()) HToFastProperties(Pop()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3455 | AddInstruction(result); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 3456 | return ast_context()->ReturnValue(result); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3457 | } else { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 3458 | return ast_context()->ReturnValue(Pop()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3459 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3460 | } |
| 3461 | |
| 3462 | |
| 3463 | void HGraphBuilder::VisitArrayLiteral(ArrayLiteral* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3464 | ASSERT(!HasStackOverflow()); |
| 3465 | ASSERT(current_block() != NULL); |
| 3466 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3467 | ZoneList<Expression*>* subexprs = expr->values(); |
| 3468 | int length = subexprs->length(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 3469 | HValue* context = environment()->LookupContext(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3470 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3471 | Handle<FixedArray> literals(environment()->closure()->literals()); |
| 3472 | Handle<Object> raw_boilerplate(literals->get(expr->literal_index())); |
| 3473 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3474 | if (raw_boilerplate->IsUndefined()) { |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 3475 | raw_boilerplate = Runtime::CreateArrayLiteralBoilerplate( |
| 3476 | isolate(), literals, expr->constant_elements()); |
| 3477 | if (raw_boilerplate.is_null()) { |
| 3478 | return Bailout("array boilerplate creation failed"); |
| 3479 | } |
| 3480 | literals->set(expr->literal_index(), *raw_boilerplate); |
| 3481 | if (JSObject::cast(*raw_boilerplate)->elements()->map() == |
| 3482 | isolate()->heap()->fixed_cow_array_map()) { |
| 3483 | isolate()->counters()->cow_arrays_created_runtime()->Increment(); |
| 3484 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3485 | } |
| 3486 | |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 3487 | Handle<JSObject> boilerplate = Handle<JSObject>::cast(raw_boilerplate); |
| 3488 | ElementsKind boilerplate_elements_kind = |
| 3489 | Handle<JSObject>::cast(boilerplate)->GetElementsKind(); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3490 | |
| 3491 | HArrayLiteral* literal = new(zone()) HArrayLiteral( |
| 3492 | context, |
| 3493 | boilerplate, |
| 3494 | length, |
| 3495 | expr->literal_index(), |
| 3496 | expr->depth()); |
| 3497 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3498 | // The array is expected in the bailout environment during computation |
| 3499 | // of the property values and is the value of the entire expression. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3500 | PushAndAdd(literal); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3501 | |
| 3502 | HLoadElements* elements = NULL; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3503 | |
| 3504 | for (int i = 0; i < length; i++) { |
| 3505 | Expression* subexpr = subexprs->at(i); |
| 3506 | // If the subexpression is a literal or a simple materialized literal it |
| 3507 | // is already set in the cloned array. |
| 3508 | if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue; |
| 3509 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3510 | CHECK_ALIVE(VisitForValue(subexpr)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3511 | HValue* value = Pop(); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3512 | if (!Smi::IsValid(i)) return Bailout("Non-smi key in array literal"); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3513 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3514 | elements = new(zone()) HLoadElements(literal); |
| 3515 | AddInstruction(elements); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3516 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 3517 | HValue* key = AddInstruction( |
| 3518 | new(zone()) HConstant(Handle<Object>(Smi::FromInt(i)), |
| 3519 | Representation::Integer32())); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3520 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3521 | switch (boilerplate_elements_kind) { |
| 3522 | case FAST_SMI_ONLY_ELEMENTS: |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 3523 | // Smi-only arrays need a smi check. |
| 3524 | AddInstruction(new(zone()) HCheckSmi(value)); |
| 3525 | // Fall through. |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3526 | case FAST_ELEMENTS: |
| 3527 | AddInstruction(new(zone()) HStoreKeyedFastElement( |
| 3528 | elements, |
| 3529 | key, |
| 3530 | value, |
| 3531 | boilerplate_elements_kind)); |
| 3532 | break; |
| 3533 | case FAST_DOUBLE_ELEMENTS: |
| 3534 | AddInstruction(new(zone()) HStoreKeyedFastDoubleElement(elements, |
| 3535 | key, |
| 3536 | value)); |
| 3537 | break; |
| 3538 | default: |
| 3539 | UNREACHABLE(); |
| 3540 | break; |
| 3541 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3542 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3543 | AddSimulate(expr->GetIdForElement(i)); |
| 3544 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 3545 | return ast_context()->ReturnValue(Pop()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3546 | } |
| 3547 | |
| 3548 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3549 | // Sets the lookup result and returns true if the store can be inlined. |
| 3550 | static bool ComputeStoredField(Handle<Map> type, |
| 3551 | Handle<String> name, |
| 3552 | LookupResult* lookup) { |
| 3553 | type->LookupInDescriptors(NULL, *name, lookup); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 3554 | if (!lookup->IsFound()) return false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3555 | if (lookup->type() == FIELD) return true; |
| 3556 | return (lookup->type() == MAP_TRANSITION) && |
| 3557 | (type->unused_property_fields() > 0); |
| 3558 | } |
| 3559 | |
| 3560 | |
| 3561 | static int ComputeStoredFieldIndex(Handle<Map> type, |
| 3562 | Handle<String> name, |
| 3563 | LookupResult* lookup) { |
| 3564 | ASSERT(lookup->type() == FIELD || lookup->type() == MAP_TRANSITION); |
| 3565 | if (lookup->type() == FIELD) { |
| 3566 | return lookup->GetLocalFieldIndexFromMap(*type); |
| 3567 | } else { |
| 3568 | Map* transition = lookup->GetTransitionMapFromMap(*type); |
| 3569 | return transition->PropertyIndexFor(*name) - type->inobject_properties(); |
| 3570 | } |
| 3571 | } |
| 3572 | |
| 3573 | |
| 3574 | HInstruction* HGraphBuilder::BuildStoreNamedField(HValue* object, |
| 3575 | Handle<String> name, |
| 3576 | HValue* value, |
| 3577 | Handle<Map> type, |
| 3578 | LookupResult* lookup, |
| 3579 | bool smi_and_map_check) { |
| 3580 | if (smi_and_map_check) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 3581 | AddInstruction(new(zone()) HCheckNonSmi(object)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 3582 | AddInstruction(new(zone()) HCheckMap(object, type, NULL, |
| 3583 | ALLOW_ELEMENT_TRANSITION_MAPS)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | int index = ComputeStoredFieldIndex(type, name, lookup); |
| 3587 | bool is_in_object = index < 0; |
| 3588 | int offset = index * kPointerSize; |
| 3589 | if (index < 0) { |
| 3590 | // Negative property indices are in-object properties, indexed |
| 3591 | // from the end of the fixed part of the object. |
| 3592 | offset += type->instance_size(); |
| 3593 | } else { |
| 3594 | offset += FixedArray::kHeaderSize; |
| 3595 | } |
| 3596 | HStoreNamedField* instr = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 3597 | new(zone()) HStoreNamedField(object, name, value, is_in_object, offset); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3598 | if (lookup->type() == MAP_TRANSITION) { |
| 3599 | Handle<Map> transition(lookup->GetTransitionMapFromMap(*type)); |
| 3600 | instr->set_transition(transition); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 3601 | // TODO(fschneider): Record the new map type of the object in the IR to |
| 3602 | // enable elimination of redundant checks after the transition store. |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 3603 | instr->SetGVNFlag(kChangesMaps); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3604 | } |
| 3605 | return instr; |
| 3606 | } |
| 3607 | |
| 3608 | |
| 3609 | HInstruction* HGraphBuilder::BuildStoreNamedGeneric(HValue* object, |
| 3610 | Handle<String> name, |
| 3611 | HValue* value) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 3612 | HValue* context = environment()->LookupContext(); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 3613 | return new(zone()) HStoreNamedGeneric( |
| 3614 | context, |
| 3615 | object, |
| 3616 | name, |
| 3617 | value, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3618 | function_strict_mode_flag()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3619 | } |
| 3620 | |
| 3621 | |
| 3622 | HInstruction* HGraphBuilder::BuildStoreNamed(HValue* object, |
| 3623 | HValue* value, |
| 3624 | Expression* expr) { |
| 3625 | Property* prop = (expr->AsProperty() != NULL) |
| 3626 | ? expr->AsProperty() |
| 3627 | : expr->AsAssignment()->target()->AsProperty(); |
| 3628 | Literal* key = prop->key()->AsLiteral(); |
| 3629 | Handle<String> name = Handle<String>::cast(key->handle()); |
| 3630 | ASSERT(!name.is_null()); |
| 3631 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3632 | LookupResult lookup(isolate()); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3633 | SmallMapList* types = expr->GetReceiverTypes(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3634 | bool is_monomorphic = expr->IsMonomorphic() && |
| 3635 | ComputeStoredField(types->first(), name, &lookup); |
| 3636 | |
| 3637 | return is_monomorphic |
| 3638 | ? BuildStoreNamedField(object, name, value, types->first(), &lookup, |
| 3639 | true) // Needs smi and map check. |
| 3640 | : BuildStoreNamedGeneric(object, name, value); |
| 3641 | } |
| 3642 | |
| 3643 | |
| 3644 | void HGraphBuilder::HandlePolymorphicStoreNamedField(Assignment* expr, |
| 3645 | HValue* object, |
| 3646 | HValue* value, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3647 | SmallMapList* types, |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3648 | Handle<String> name) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3649 | // TODO(ager): We should recognize when the prototype chains for different |
| 3650 | // maps are identical. In that case we can avoid repeatedly generating the |
| 3651 | // same prototype map checks. |
| 3652 | int count = 0; |
| 3653 | HBasicBlock* join = NULL; |
| 3654 | for (int i = 0; i < types->length() && count < kMaxStorePolymorphism; ++i) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3655 | Handle<Map> map = types->at(i); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3656 | LookupResult lookup(isolate()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3657 | if (ComputeStoredField(map, name, &lookup)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3658 | if (count == 0) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 3659 | AddInstruction(new(zone()) HCheckNonSmi(object)); // Only needed once. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3660 | join = graph()->CreateBasicBlock(); |
| 3661 | } |
| 3662 | ++count; |
| 3663 | HBasicBlock* if_true = graph()->CreateBasicBlock(); |
| 3664 | HBasicBlock* if_false = graph()->CreateBasicBlock(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 3665 | HCompareMap* compare = |
| 3666 | new(zone()) HCompareMap(object, map, if_true, if_false); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3667 | current_block()->Finish(compare); |
| 3668 | |
| 3669 | set_current_block(if_true); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3670 | HInstruction* instr = |
| 3671 | BuildStoreNamedField(object, name, value, map, &lookup, false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3672 | instr->set_position(expr->position()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3673 | // Goto will add the HSimulate for the store. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3674 | AddInstruction(instr); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3675 | if (!ast_context()->IsEffect()) Push(value); |
| 3676 | current_block()->Goto(join); |
| 3677 | |
| 3678 | set_current_block(if_false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3679 | } |
| 3680 | } |
| 3681 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3682 | // Finish up. Unconditionally deoptimize if we've handled all the maps we |
| 3683 | // know about and do not want to handle ones we've never seen. Otherwise |
| 3684 | // use a generic IC. |
| 3685 | if (count == types->length() && FLAG_deoptimize_uncommon_cases) { |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 3686 | current_block()->FinishExitWithDeoptimization(HDeoptimize::kNoUses); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3687 | } else { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3688 | HInstruction* instr = BuildStoreNamedGeneric(object, name, value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3689 | instr->set_position(expr->position()); |
| 3690 | AddInstruction(instr); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3691 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3692 | if (join != NULL) { |
| 3693 | if (!ast_context()->IsEffect()) Push(value); |
| 3694 | current_block()->Goto(join); |
| 3695 | } else { |
| 3696 | // The HSimulate for the store should not see the stored value in |
| 3697 | // effect contexts (it is not materialized at expr->id() in the |
| 3698 | // unoptimized code). |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 3699 | if (instr->HasObservableSideEffects()) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3700 | if (ast_context()->IsEffect()) { |
| 3701 | AddSimulate(expr->id()); |
| 3702 | } else { |
| 3703 | Push(value); |
| 3704 | AddSimulate(expr->id()); |
| 3705 | Drop(1); |
| 3706 | } |
| 3707 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 3708 | return ast_context()->ReturnValue(value); |
kmillikin@chromium.org | c278c96 | 2011-01-06 08:52:11 +0000 | [diff] [blame] | 3709 | } |
lrn@chromium.org | 8541d77 | 2010-12-15 12:05:09 +0000 | [diff] [blame] | 3710 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3711 | |
| 3712 | ASSERT(join != NULL); |
| 3713 | join->SetJoinId(expr->id()); |
| 3714 | set_current_block(join); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 3715 | if (!ast_context()->IsEffect()) return ast_context()->ReturnValue(Pop()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
| 3718 | |
| 3719 | void HGraphBuilder::HandlePropertyAssignment(Assignment* expr) { |
| 3720 | Property* prop = expr->target()->AsProperty(); |
| 3721 | ASSERT(prop != NULL); |
| 3722 | expr->RecordTypeFeedback(oracle()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3723 | CHECK_ALIVE(VisitForValue(prop->obj())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3724 | |
| 3725 | HValue* value = NULL; |
| 3726 | HInstruction* instr = NULL; |
| 3727 | |
| 3728 | if (prop->key()->IsPropertyName()) { |
| 3729 | // Named store. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3730 | CHECK_ALIVE(VisitForValue(expr->value())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3731 | value = Pop(); |
| 3732 | HValue* object = Pop(); |
| 3733 | |
| 3734 | Literal* key = prop->key()->AsLiteral(); |
| 3735 | Handle<String> name = Handle<String>::cast(key->handle()); |
| 3736 | ASSERT(!name.is_null()); |
| 3737 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3738 | SmallMapList* types = expr->GetReceiverTypes(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3739 | LookupResult lookup(isolate()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3740 | |
| 3741 | if (expr->IsMonomorphic()) { |
| 3742 | instr = BuildStoreNamed(object, value, expr); |
| 3743 | |
| 3744 | } else if (types != NULL && types->length() > 1) { |
| 3745 | HandlePolymorphicStoreNamedField(expr, object, value, types, name); |
| 3746 | return; |
| 3747 | |
| 3748 | } else { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3749 | instr = BuildStoreNamedGeneric(object, name, value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3750 | } |
| 3751 | |
| 3752 | } else { |
| 3753 | // Keyed store. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3754 | CHECK_ALIVE(VisitForValue(prop->key())); |
| 3755 | CHECK_ALIVE(VisitForValue(expr->value())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3756 | value = Pop(); |
| 3757 | HValue* key = Pop(); |
| 3758 | HValue* object = Pop(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 3759 | bool has_side_effects = false; |
| 3760 | HandleKeyedElementAccess(object, key, value, expr, expr->AssignmentId(), |
| 3761 | expr->position(), |
| 3762 | true, // is_store |
| 3763 | &has_side_effects); |
| 3764 | Push(value); |
| 3765 | ASSERT(has_side_effects); // Stores always have side effects. |
| 3766 | AddSimulate(expr->AssignmentId()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 3767 | return ast_context()->ReturnValue(Pop()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3768 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3769 | Push(value); |
| 3770 | instr->set_position(expr->position()); |
| 3771 | AddInstruction(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 3772 | if (instr->HasObservableSideEffects()) AddSimulate(expr->AssignmentId()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 3773 | return ast_context()->ReturnValue(Pop()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3774 | } |
| 3775 | |
| 3776 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3777 | // Because not every expression has a position and there is not common |
| 3778 | // superclass of Assignment and CountOperation, we cannot just pass the |
| 3779 | // owning expression instead of position and ast_id separately. |
| 3780 | void HGraphBuilder::HandleGlobalVariableAssignment(Variable* var, |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3781 | HValue* value, |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3782 | int position, |
| 3783 | int ast_id) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3784 | LookupResult lookup(isolate()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3785 | GlobalPropertyAccess type = LookupGlobalProperty(var, &lookup, true); |
| 3786 | if (type == kUseCell) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3787 | Handle<GlobalObject> global(info()->global_object()); |
| 3788 | Handle<JSGlobalPropertyCell> cell(global->GetPropertyCell(&lookup)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3789 | HInstruction* instr = |
| 3790 | new(zone()) HStoreGlobalCell(value, cell, lookup.GetPropertyDetails()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3791 | instr->set_position(position); |
| 3792 | AddInstruction(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 3793 | if (instr->HasObservableSideEffects()) AddSimulate(ast_id); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3794 | } else { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 3795 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 3796 | HGlobalObject* global_object = new(zone()) HGlobalObject(context); |
| 3797 | AddInstruction(global_object); |
| 3798 | HStoreGlobalGeneric* instr = |
| 3799 | new(zone()) HStoreGlobalGeneric(context, |
| 3800 | global_object, |
| 3801 | var->name(), |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 3802 | value, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3803 | function_strict_mode_flag()); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 3804 | instr->set_position(position); |
| 3805 | AddInstruction(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 3806 | ASSERT(instr->HasObservableSideEffects()); |
| 3807 | if (instr->HasObservableSideEffects()) AddSimulate(ast_id); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3808 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3809 | } |
| 3810 | |
| 3811 | |
| 3812 | void HGraphBuilder::HandleCompoundAssignment(Assignment* expr) { |
| 3813 | Expression* target = expr->target(); |
| 3814 | VariableProxy* proxy = target->AsVariableProxy(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3815 | Property* prop = target->AsProperty(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3816 | ASSERT(proxy == NULL || prop == NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3817 | |
| 3818 | // We have a second position recorded in the FullCodeGenerator to have |
| 3819 | // type feedback for the binary operation. |
| 3820 | BinaryOperation* operation = expr->binary_operation(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3821 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3822 | if (proxy != NULL) { |
| 3823 | Variable* var = proxy->var(); |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 3824 | if (var->mode() == LET) { |
| 3825 | return Bailout("unsupported let compound assignment"); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3826 | } |
| 3827 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3828 | CHECK_ALIVE(VisitForValue(operation)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3829 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3830 | switch (var->location()) { |
| 3831 | case Variable::UNALLOCATED: |
| 3832 | HandleGlobalVariableAssignment(var, |
| 3833 | Top(), |
| 3834 | expr->position(), |
| 3835 | expr->AssignmentId()); |
| 3836 | break; |
| 3837 | |
| 3838 | case Variable::PARAMETER: |
| 3839 | case Variable::LOCAL: |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 3840 | if (var->mode() == CONST) { |
| 3841 | return Bailout("unsupported const compound assignment"); |
| 3842 | } |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3843 | Bind(var, Top()); |
| 3844 | break; |
| 3845 | |
| 3846 | case Variable::CONTEXT: { |
| 3847 | // Bail out if we try to mutate a parameter value in a function |
| 3848 | // using the arguments object. We do not (yet) correctly handle the |
| 3849 | // arguments property of the function. |
| 3850 | if (info()->scope()->arguments() != NULL) { |
| 3851 | // Parameters will be allocated to context slots. We have no |
| 3852 | // direct way to detect that the variable is a parameter so we do |
| 3853 | // a linear search of the parameter variables. |
| 3854 | int count = info()->scope()->num_parameters(); |
| 3855 | for (int i = 0; i < count; ++i) { |
| 3856 | if (var == info()->scope()->parameter(i)) { |
| 3857 | Bailout( |
| 3858 | "assignment to parameter, function uses arguments object"); |
| 3859 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 3860 | } |
| 3861 | } |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3862 | |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 3863 | HStoreContextSlot::Mode mode; |
| 3864 | |
| 3865 | switch (var->mode()) { |
| 3866 | case LET: |
| 3867 | mode = HStoreContextSlot::kCheckDeoptimize; |
| 3868 | break; |
| 3869 | case CONST: |
| 3870 | return ast_context()->ReturnValue(Pop()); |
| 3871 | case CONST_HARMONY: |
| 3872 | // This case is checked statically so no need to |
| 3873 | // perform checks here |
| 3874 | UNREACHABLE(); |
| 3875 | default: |
| 3876 | mode = HStoreContextSlot::kNoCheck; |
| 3877 | } |
| 3878 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3879 | HValue* context = BuildContextChainWalk(var); |
| 3880 | HStoreContextSlot* instr = |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3881 | new(zone()) HStoreContextSlot(context, var->index(), mode, Top()); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3882 | AddInstruction(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 3883 | if (instr->HasObservableSideEffects()) { |
| 3884 | AddSimulate(expr->AssignmentId()); |
| 3885 | } |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3886 | break; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 3887 | } |
| 3888 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3889 | case Variable::LOOKUP: |
| 3890 | return Bailout("compound assignment to lookup slot"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3891 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 3892 | return ast_context()->ReturnValue(Pop()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3893 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3894 | } else if (prop != NULL) { |
| 3895 | prop->RecordTypeFeedback(oracle()); |
| 3896 | |
| 3897 | if (prop->key()->IsPropertyName()) { |
| 3898 | // Named property. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3899 | CHECK_ALIVE(VisitForValue(prop->obj())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3900 | HValue* obj = Top(); |
| 3901 | |
| 3902 | HInstruction* load = NULL; |
| 3903 | if (prop->IsMonomorphic()) { |
| 3904 | Handle<String> name = prop->key()->AsLiteral()->AsPropertyName(); |
| 3905 | Handle<Map> map = prop->GetReceiverTypes()->first(); |
| 3906 | load = BuildLoadNamed(obj, prop, map, name); |
| 3907 | } else { |
| 3908 | load = BuildLoadNamedGeneric(obj, prop); |
| 3909 | } |
| 3910 | PushAndAdd(load); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 3911 | if (load->HasObservableSideEffects()) AddSimulate(expr->CompoundLoadId()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3912 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3913 | CHECK_ALIVE(VisitForValue(expr->value())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3914 | HValue* right = Pop(); |
| 3915 | HValue* left = Pop(); |
| 3916 | |
| 3917 | HInstruction* instr = BuildBinaryOperation(operation, left, right); |
| 3918 | PushAndAdd(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 3919 | if (instr->HasObservableSideEffects()) AddSimulate(operation->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3920 | |
| 3921 | HInstruction* store = BuildStoreNamed(obj, instr, prop); |
| 3922 | AddInstruction(store); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3923 | // Drop the simulated receiver and value. Return the value. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3924 | Drop(2); |
| 3925 | Push(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 3926 | if (store->HasObservableSideEffects()) AddSimulate(expr->AssignmentId()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 3927 | return ast_context()->ReturnValue(Pop()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3928 | |
| 3929 | } else { |
| 3930 | // Keyed property. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3931 | CHECK_ALIVE(VisitForValue(prop->obj())); |
| 3932 | CHECK_ALIVE(VisitForValue(prop->key())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3933 | HValue* obj = environment()->ExpressionStackAt(1); |
| 3934 | HValue* key = environment()->ExpressionStackAt(0); |
| 3935 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 3936 | bool has_side_effects = false; |
| 3937 | HValue* load = HandleKeyedElementAccess( |
| 3938 | obj, key, NULL, prop, expr->CompoundLoadId(), RelocInfo::kNoPosition, |
| 3939 | false, // is_store |
| 3940 | &has_side_effects); |
| 3941 | Push(load); |
| 3942 | if (has_side_effects) AddSimulate(expr->CompoundLoadId()); |
| 3943 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3944 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3945 | CHECK_ALIVE(VisitForValue(expr->value())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3946 | HValue* right = Pop(); |
| 3947 | HValue* left = Pop(); |
| 3948 | |
| 3949 | HInstruction* instr = BuildBinaryOperation(operation, left, right); |
| 3950 | PushAndAdd(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 3951 | if (instr->HasObservableSideEffects()) AddSimulate(operation->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3952 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 3953 | expr->RecordTypeFeedback(oracle()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 3954 | HandleKeyedElementAccess(obj, key, instr, expr, expr->AssignmentId(), |
| 3955 | RelocInfo::kNoPosition, |
| 3956 | true, // is_store |
| 3957 | &has_side_effects); |
| 3958 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3959 | // Drop the simulated receiver, key, and value. Return the value. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3960 | Drop(3); |
| 3961 | Push(instr); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 3962 | ASSERT(has_side_effects); // Stores always have side effects. |
| 3963 | AddSimulate(expr->AssignmentId()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 3964 | return ast_context()->ReturnValue(Pop()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3965 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3966 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3967 | } else { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3968 | return Bailout("invalid lhs in compound assignment"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3969 | } |
| 3970 | } |
| 3971 | |
| 3972 | |
| 3973 | void HGraphBuilder::VisitAssignment(Assignment* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3974 | ASSERT(!HasStackOverflow()); |
| 3975 | ASSERT(current_block() != NULL); |
| 3976 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3977 | VariableProxy* proxy = expr->target()->AsVariableProxy(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3978 | Property* prop = expr->target()->AsProperty(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3979 | ASSERT(proxy == NULL || prop == NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3980 | |
| 3981 | if (expr->is_compound()) { |
| 3982 | HandleCompoundAssignment(expr); |
| 3983 | return; |
| 3984 | } |
| 3985 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 3986 | if (prop != NULL) { |
| 3987 | HandlePropertyAssignment(expr); |
| 3988 | } else if (proxy != NULL) { |
| 3989 | Variable* var = proxy->var(); |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 3990 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 3991 | if (var->mode() == CONST) { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3992 | if (expr->op() != Token::INIT_CONST) { |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 3993 | CHECK_ALIVE(VisitForValue(expr->value())); |
| 3994 | return ast_context()->ReturnValue(Pop()); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3995 | } |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 3996 | |
| 3997 | if (var->IsStackAllocated()) { |
| 3998 | // We insert a use of the old value to detect unsupported uses of const |
| 3999 | // variables (e.g. initialization inside a loop). |
| 4000 | HValue* old_value = environment()->Lookup(var); |
| 4001 | AddInstruction(new HUseConst(old_value)); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 4002 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4003 | } else if (var->mode() == CONST_HARMONY) { |
| 4004 | if (expr->op() != Token::INIT_CONST_HARMONY) { |
| 4005 | return Bailout("non-initializer assignment to const"); |
| 4006 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4007 | } |
| 4008 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4009 | if (proxy->IsArguments()) return Bailout("assignment to arguments"); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4010 | |
| 4011 | // Handle the assignment. |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 4012 | switch (var->location()) { |
| 4013 | case Variable::UNALLOCATED: |
| 4014 | CHECK_ALIVE(VisitForValue(expr->value())); |
| 4015 | HandleGlobalVariableAssignment(var, |
| 4016 | Top(), |
| 4017 | expr->position(), |
| 4018 | expr->AssignmentId()); |
| 4019 | return ast_context()->ReturnValue(Pop()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4020 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 4021 | case Variable::PARAMETER: |
| 4022 | case Variable::LOCAL: { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4023 | // Perform an initialization check for let declared variables |
| 4024 | // or parameters. |
| 4025 | if (var->mode() == LET && expr->op() == Token::ASSIGN) { |
| 4026 | HValue* env_value = environment()->Lookup(var); |
| 4027 | if (env_value == graph()->GetConstantHole()) { |
| 4028 | return Bailout("assignment to let variable before initialization"); |
| 4029 | } |
| 4030 | } |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 4031 | // We do not allow the arguments object to occur in a context where it |
| 4032 | // may escape, but assignments to stack-allocated locals are |
| 4033 | // permitted. |
| 4034 | CHECK_ALIVE(VisitForValue(expr->value(), ARGUMENTS_ALLOWED)); |
| 4035 | HValue* value = Pop(); |
| 4036 | Bind(var, value); |
| 4037 | return ast_context()->ReturnValue(value); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4038 | } |
| 4039 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 4040 | case Variable::CONTEXT: { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 4041 | // Bail out if we try to mutate a parameter value in a function using |
| 4042 | // the arguments object. We do not (yet) correctly handle the |
| 4043 | // arguments property of the function. |
| 4044 | if (info()->scope()->arguments() != NULL) { |
| 4045 | // Parameters will rewrite to context slots. We have no direct way |
| 4046 | // to detect that the variable is a parameter. |
| 4047 | int count = info()->scope()->num_parameters(); |
| 4048 | for (int i = 0; i < count; ++i) { |
| 4049 | if (var == info()->scope()->parameter(i)) { |
| 4050 | return Bailout("assignment to parameter in arguments object"); |
| 4051 | } |
| 4052 | } |
| 4053 | } |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4054 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 4055 | CHECK_ALIVE(VisitForValue(expr->value())); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4056 | HStoreContextSlot::Mode mode; |
| 4057 | if (expr->op() == Token::ASSIGN) { |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 4058 | switch (var->mode()) { |
| 4059 | case LET: |
| 4060 | mode = HStoreContextSlot::kCheckDeoptimize; |
| 4061 | break; |
| 4062 | case CONST: |
| 4063 | return ast_context()->ReturnValue(Pop()); |
| 4064 | case CONST_HARMONY: |
| 4065 | // This case is checked statically so no need to |
| 4066 | // perform checks here |
| 4067 | UNREACHABLE(); |
| 4068 | default: |
| 4069 | mode = HStoreContextSlot::kNoCheck; |
| 4070 | } |
| 4071 | } else if (expr->op() == Token::INIT_VAR || |
| 4072 | expr->op() == Token::INIT_LET || |
| 4073 | expr->op() == Token::INIT_CONST_HARMONY) { |
| 4074 | mode = HStoreContextSlot::kNoCheck; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4075 | } else { |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 4076 | ASSERT(expr->op() == Token::INIT_CONST); |
| 4077 | |
| 4078 | mode = HStoreContextSlot::kCheckIgnoreAssignment; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4079 | } |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 4080 | |
| 4081 | HValue* context = BuildContextChainWalk(var); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4082 | HStoreContextSlot* instr = new(zone()) HStoreContextSlot( |
| 4083 | context, var->index(), mode, Top()); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 4084 | AddInstruction(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 4085 | if (instr->HasObservableSideEffects()) { |
| 4086 | AddSimulate(expr->AssignmentId()); |
| 4087 | } |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 4088 | return ast_context()->ReturnValue(Pop()); |
| 4089 | } |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4090 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 4091 | case Variable::LOOKUP: |
| 4092 | return Bailout("assignment to LOOKUP variable"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4093 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4094 | } else { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4095 | return Bailout("invalid left-hand side in assignment"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4096 | } |
| 4097 | } |
| 4098 | |
| 4099 | |
| 4100 | void HGraphBuilder::VisitThrow(Throw* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4101 | ASSERT(!HasStackOverflow()); |
| 4102 | ASSERT(current_block() != NULL); |
| 4103 | ASSERT(current_block()->HasPredecessor()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4104 | // We don't optimize functions with invalid left-hand sides in |
| 4105 | // assignments, count operations, or for-in. Consequently throw can |
| 4106 | // currently only occur in an effect context. |
| 4107 | ASSERT(ast_context()->IsEffect()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4108 | CHECK_ALIVE(VisitForValue(expr->exception())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4109 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 4110 | HValue* context = environment()->LookupContext(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4111 | HValue* value = environment()->Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 4112 | HThrow* instr = new(zone()) HThrow(context, value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4113 | instr->set_position(expr->position()); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 4114 | AddInstruction(instr); |
| 4115 | AddSimulate(expr->id()); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4116 | current_block()->FinishExit(new(zone()) HAbnormalExit); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 4117 | set_current_block(NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 4121 | HLoadNamedField* HGraphBuilder::BuildLoadNamedField(HValue* object, |
| 4122 | Property* expr, |
| 4123 | Handle<Map> type, |
| 4124 | LookupResult* lookup, |
| 4125 | bool smi_and_map_check) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4126 | if (smi_and_map_check) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4127 | AddInstruction(new(zone()) HCheckNonSmi(object)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4128 | AddInstruction(new(zone()) HCheckMap(object, type, NULL, |
| 4129 | ALLOW_ELEMENT_TRANSITION_MAPS)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4130 | } |
| 4131 | |
| 4132 | int index = lookup->GetLocalFieldIndexFromMap(*type); |
| 4133 | if (index < 0) { |
| 4134 | // Negative property indices are in-object properties, indexed |
| 4135 | // from the end of the fixed part of the object. |
| 4136 | int offset = (index * kPointerSize) + type->instance_size(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4137 | return new(zone()) HLoadNamedField(object, true, offset); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4138 | } else { |
| 4139 | // Non-negative property indices are in the properties array. |
| 4140 | int offset = (index * kPointerSize) + FixedArray::kHeaderSize; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4141 | return new(zone()) HLoadNamedField(object, false, offset); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4142 | } |
| 4143 | } |
| 4144 | |
| 4145 | |
| 4146 | HInstruction* HGraphBuilder::BuildLoadNamedGeneric(HValue* obj, |
| 4147 | Property* expr) { |
| 4148 | ASSERT(expr->key()->IsPropertyName()); |
| 4149 | Handle<Object> name = expr->key()->AsLiteral()->handle(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 4150 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4151 | return new(zone()) HLoadNamedGeneric(context, obj, name); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | |
| 4155 | HInstruction* HGraphBuilder::BuildLoadNamed(HValue* obj, |
| 4156 | Property* expr, |
| 4157 | Handle<Map> map, |
| 4158 | Handle<String> name) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4159 | LookupResult lookup(isolate()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4160 | map->LookupInDescriptors(NULL, *name, &lookup); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 4161 | if (lookup.IsFound() && lookup.type() == FIELD) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4162 | return BuildLoadNamedField(obj, |
| 4163 | expr, |
| 4164 | map, |
| 4165 | &lookup, |
| 4166 | true); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 4167 | } else if (lookup.IsFound() && lookup.type() == CONSTANT_FUNCTION) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4168 | AddInstruction(new(zone()) HCheckNonSmi(obj)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4169 | AddInstruction(new(zone()) HCheckMap(obj, map, NULL, |
| 4170 | ALLOW_ELEMENT_TRANSITION_MAPS)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4171 | Handle<JSFunction> function(lookup.GetConstantFunctionFromMap(*map)); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4172 | return new(zone()) HConstant(function, Representation::Tagged()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4173 | } else { |
| 4174 | return BuildLoadNamedGeneric(obj, expr); |
| 4175 | } |
| 4176 | } |
| 4177 | |
| 4178 | |
| 4179 | HInstruction* HGraphBuilder::BuildLoadKeyedGeneric(HValue* object, |
| 4180 | HValue* key) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 4181 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4182 | return new(zone()) HLoadKeyedGeneric(context, object, key); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4183 | } |
| 4184 | |
| 4185 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4186 | HInstruction* HGraphBuilder::BuildExternalArrayElementAccess( |
| 4187 | HValue* external_elements, |
| 4188 | HValue* checked_key, |
| 4189 | HValue* val, |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 4190 | ElementsKind elements_kind, |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4191 | bool is_store) { |
| 4192 | if (is_store) { |
| 4193 | ASSERT(val != NULL); |
| 4194 | switch (elements_kind) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 4195 | case EXTERNAL_PIXEL_ELEMENTS: { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4196 | HClampToUint8* clamp = new(zone()) HClampToUint8(val); |
| 4197 | AddInstruction(clamp); |
| 4198 | val = clamp; |
| 4199 | break; |
| 4200 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 4201 | case EXTERNAL_BYTE_ELEMENTS: |
| 4202 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 4203 | case EXTERNAL_SHORT_ELEMENTS: |
| 4204 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 4205 | case EXTERNAL_INT_ELEMENTS: |
| 4206 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4207 | HToInt32* floor_val = new(zone()) HToInt32(val); |
| 4208 | AddInstruction(floor_val); |
| 4209 | val = floor_val; |
| 4210 | break; |
| 4211 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 4212 | case EXTERNAL_FLOAT_ELEMENTS: |
| 4213 | case EXTERNAL_DOUBLE_ELEMENTS: |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4214 | break; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4215 | case FAST_SMI_ONLY_ELEMENTS: |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 4216 | case FAST_ELEMENTS: |
| 4217 | case FAST_DOUBLE_ELEMENTS: |
| 4218 | case DICTIONARY_ELEMENTS: |
| 4219 | case NON_STRICT_ARGUMENTS_ELEMENTS: |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4220 | UNREACHABLE(); |
| 4221 | break; |
| 4222 | } |
| 4223 | return new(zone()) HStoreKeyedSpecializedArrayElement( |
| 4224 | external_elements, checked_key, val, elements_kind); |
| 4225 | } else { |
| 4226 | return new(zone()) HLoadKeyedSpecializedArrayElement( |
| 4227 | external_elements, checked_key, elements_kind); |
| 4228 | } |
| 4229 | } |
| 4230 | |
| 4231 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4232 | HInstruction* HGraphBuilder::BuildFastElementAccess(HValue* elements, |
| 4233 | HValue* checked_key, |
| 4234 | HValue* val, |
| 4235 | ElementsKind elements_kind, |
| 4236 | bool is_store) { |
| 4237 | if (is_store) { |
| 4238 | ASSERT(val != NULL); |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 4239 | switch (elements_kind) { |
| 4240 | case FAST_DOUBLE_ELEMENTS: |
| 4241 | return new(zone()) HStoreKeyedFastDoubleElement( |
| 4242 | elements, checked_key, val); |
| 4243 | case FAST_SMI_ONLY_ELEMENTS: |
| 4244 | // Smi-only arrays need a smi check. |
| 4245 | AddInstruction(new(zone()) HCheckSmi(val)); |
| 4246 | // Fall through. |
| 4247 | case FAST_ELEMENTS: |
| 4248 | return new(zone()) HStoreKeyedFastElement( |
| 4249 | elements, checked_key, val, elements_kind); |
| 4250 | default: |
| 4251 | UNREACHABLE(); |
| 4252 | return NULL; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4253 | } |
| 4254 | } |
| 4255 | // It's an element load (!is_store). |
| 4256 | if (elements_kind == FAST_DOUBLE_ELEMENTS) { |
| 4257 | return new(zone()) HLoadKeyedFastDoubleElement(elements, checked_key); |
| 4258 | } else { // FAST_ELEMENTS or FAST_SMI_ONLY_ELEMENTS. |
| 4259 | return new(zone()) HLoadKeyedFastElement(elements, checked_key); |
| 4260 | } |
| 4261 | } |
| 4262 | |
| 4263 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4264 | HInstruction* HGraphBuilder::BuildMonomorphicElementAccess(HValue* object, |
| 4265 | HValue* key, |
| 4266 | HValue* val, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4267 | Handle<Map> map, |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4268 | bool is_store) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4269 | HInstruction* mapcheck = AddInstruction(new(zone()) HCheckMap(object, map)); |
| 4270 | bool fast_smi_only_elements = map->has_fast_smi_only_elements(); |
| 4271 | bool fast_elements = map->has_fast_elements(); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 4272 | HInstruction* elements = AddInstruction(new(zone()) HLoadElements(object)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4273 | if (is_store && (fast_elements || fast_smi_only_elements)) { |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 4274 | AddInstruction(new(zone()) HCheckMap( |
| 4275 | elements, isolate()->factory()->fixed_array_map())); |
| 4276 | } |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 4277 | HInstruction* length = NULL; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4278 | HInstruction* checked_key = NULL; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4279 | if (map->has_external_array_elements()) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 4280 | length = AddInstruction(new(zone()) HFixedArrayBaseLength(elements)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4281 | checked_key = AddInstruction(new(zone()) HBoundsCheck(key, length)); |
| 4282 | HLoadExternalArrayPointer* external_elements = |
| 4283 | new(zone()) HLoadExternalArrayPointer(elements); |
| 4284 | AddInstruction(external_elements); |
| 4285 | return BuildExternalArrayElementAccess(external_elements, checked_key, |
| 4286 | val, map->elements_kind(), is_store); |
| 4287 | } |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 4288 | ASSERT(fast_smi_only_elements || |
| 4289 | fast_elements || |
| 4290 | map->has_fast_double_elements()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4291 | if (map->instance_type() == JS_ARRAY_TYPE) { |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 4292 | length = AddInstruction(new(zone()) HJSArrayLength(object, mapcheck)); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 4293 | } else { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 4294 | length = AddInstruction(new(zone()) HFixedArrayBaseLength(elements)); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 4295 | } |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 4296 | checked_key = AddInstruction(new(zone()) HBoundsCheck(key, length)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4297 | return BuildFastElementAccess(elements, checked_key, val, |
| 4298 | map->elements_kind(), is_store); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4299 | } |
| 4300 | |
| 4301 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4302 | HValue* HGraphBuilder::HandlePolymorphicElementAccess(HValue* object, |
| 4303 | HValue* key, |
| 4304 | HValue* val, |
| 4305 | Expression* prop, |
| 4306 | int ast_id, |
| 4307 | int position, |
| 4308 | bool is_store, |
| 4309 | bool* has_side_effects) { |
| 4310 | *has_side_effects = false; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4311 | AddInstruction(new(zone()) HCheckNonSmi(object)); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 4312 | SmallMapList* maps = prop->GetReceiverTypes(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4313 | bool todo_external_array = false; |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4314 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 4315 | static const int kNumElementTypes = kElementsKindCount; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4316 | bool type_todo[kNumElementTypes]; |
| 4317 | for (int i = 0; i < kNumElementTypes; ++i) { |
| 4318 | type_todo[i] = false; |
| 4319 | } |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4320 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4321 | // Elements_kind transition support. |
| 4322 | MapHandleList transition_target(maps->length()); |
| 4323 | // Collect possible transition targets. |
| 4324 | MapHandleList possible_transitioned_maps(maps->length()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4325 | for (int i = 0; i < maps->length(); ++i) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4326 | Handle<Map> map = maps->at(i); |
| 4327 | ElementsKind elements_kind = map->elements_kind(); |
| 4328 | if (elements_kind == FAST_DOUBLE_ELEMENTS || |
| 4329 | elements_kind == FAST_ELEMENTS) { |
| 4330 | possible_transitioned_maps.Add(map); |
| 4331 | } |
| 4332 | } |
| 4333 | // Get transition target for each map (NULL == no transition). |
| 4334 | for (int i = 0; i < maps->length(); ++i) { |
| 4335 | Handle<Map> map = maps->at(i); |
| 4336 | Handle<Map> transitioned_map = |
| 4337 | map->FindTransitionedMap(&possible_transitioned_maps); |
| 4338 | transition_target.Add(transitioned_map); |
| 4339 | } |
| 4340 | |
| 4341 | int num_untransitionable_maps = 0; |
| 4342 | Handle<Map> untransitionable_map; |
| 4343 | for (int i = 0; i < maps->length(); ++i) { |
| 4344 | Handle<Map> map = maps->at(i); |
| 4345 | ASSERT(map->IsMap()); |
| 4346 | if (!transition_target.at(i).is_null()) { |
| 4347 | object = AddInstruction(new(zone()) HTransitionElementsKind( |
| 4348 | object, map, transition_target.at(i))); |
| 4349 | } else { |
| 4350 | type_todo[map->elements_kind()] = true; |
| 4351 | if (map->elements_kind() >= FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND) { |
| 4352 | todo_external_array = true; |
| 4353 | } |
| 4354 | num_untransitionable_maps++; |
| 4355 | untransitionable_map = map; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4356 | } |
| 4357 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4358 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4359 | // If only one map is left after transitioning, handle this case |
| 4360 | // monomorphically. |
| 4361 | if (num_untransitionable_maps == 1) { |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 4362 | HInstruction* instr = NULL; |
| 4363 | if (untransitionable_map->has_slow_elements_kind()) { |
| 4364 | instr = AddInstruction(is_store ? BuildStoreKeyedGeneric(object, key, val) |
| 4365 | : BuildLoadKeyedGeneric(object, key)); |
| 4366 | } else { |
| 4367 | instr = AddInstruction(BuildMonomorphicElementAccess( |
| 4368 | object, key, val, untransitionable_map, is_store)); |
| 4369 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 4370 | *has_side_effects |= instr->HasObservableSideEffects(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4371 | instr->set_position(position); |
| 4372 | return is_store ? NULL : instr; |
| 4373 | } |
| 4374 | |
| 4375 | AddInstruction(HCheckInstanceType::NewIsSpecObject(object)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4376 | HBasicBlock* join = graph()->CreateBasicBlock(); |
| 4377 | |
| 4378 | HInstruction* elements_kind_instr = |
| 4379 | AddInstruction(new(zone()) HElementsKind(object)); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 4380 | HCompareConstantEqAndBranch* elements_kind_branch = NULL; |
| 4381 | HInstruction* elements = AddInstruction(new(zone()) HLoadElements(object)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4382 | HLoadExternalArrayPointer* external_elements = NULL; |
| 4383 | HInstruction* checked_key = NULL; |
| 4384 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4385 | // Generated code assumes that FAST_SMI_ONLY_ELEMENTS, FAST_ELEMENTS, |
| 4386 | // FAST_DOUBLE_ELEMENTS and DICTIONARY_ELEMENTS are handled before external |
| 4387 | // arrays. |
| 4388 | STATIC_ASSERT(FAST_SMI_ONLY_ELEMENTS < FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND); |
| 4389 | STATIC_ASSERT(FAST_ELEMENTS < FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND); |
| 4390 | STATIC_ASSERT(FAST_DOUBLE_ELEMENTS < FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND); |
| 4391 | STATIC_ASSERT(DICTIONARY_ELEMENTS < FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4392 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4393 | for (ElementsKind elements_kind = FIRST_ELEMENTS_KIND; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 4394 | elements_kind <= LAST_ELEMENTS_KIND; |
| 4395 | elements_kind = ElementsKind(elements_kind + 1)) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4396 | // After having handled FAST_ELEMENTS, FAST_SMI_ONLY_ELEMENTS, |
| 4397 | // FAST_DOUBLE_ELEMENTS and DICTIONARY_ELEMENTS, we need to add some code |
| 4398 | // that's executed for all external array cases. |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 4399 | STATIC_ASSERT(LAST_EXTERNAL_ARRAY_ELEMENTS_KIND == |
| 4400 | LAST_ELEMENTS_KIND); |
| 4401 | if (elements_kind == FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4402 | && todo_external_array) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4403 | HInstruction* length = |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 4404 | AddInstruction(new(zone()) HFixedArrayBaseLength(elements)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4405 | checked_key = AddInstruction(new(zone()) HBoundsCheck(key, length)); |
| 4406 | external_elements = new(zone()) HLoadExternalArrayPointer(elements); |
| 4407 | AddInstruction(external_elements); |
| 4408 | } |
| 4409 | if (type_todo[elements_kind]) { |
| 4410 | HBasicBlock* if_true = graph()->CreateBasicBlock(); |
| 4411 | HBasicBlock* if_false = graph()->CreateBasicBlock(); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 4412 | elements_kind_branch = new(zone()) HCompareConstantEqAndBranch( |
| 4413 | elements_kind_instr, elements_kind, Token::EQ_STRICT); |
| 4414 | elements_kind_branch->SetSuccessorAt(0, if_true); |
| 4415 | elements_kind_branch->SetSuccessorAt(1, if_false); |
| 4416 | current_block()->Finish(elements_kind_branch); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4417 | |
| 4418 | set_current_block(if_true); |
| 4419 | HInstruction* access; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4420 | if (elements_kind == FAST_SMI_ONLY_ELEMENTS || |
| 4421 | elements_kind == FAST_ELEMENTS || |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 4422 | elements_kind == FAST_DOUBLE_ELEMENTS) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4423 | if (is_store && elements_kind != FAST_DOUBLE_ELEMENTS) { |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 4424 | AddInstruction(new(zone()) HCheckMap( |
| 4425 | elements, isolate()->factory()->fixed_array_map(), |
| 4426 | elements_kind_branch)); |
| 4427 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4428 | // TODO(jkummerow): The need for these two blocks could be avoided |
| 4429 | // in one of two ways: |
| 4430 | // (1) Introduce ElementsKinds for JSArrays that are distinct from |
| 4431 | // those for fast objects. |
| 4432 | // (2) Put the common instructions into a third "join" block. This |
| 4433 | // requires additional AST IDs that we can deopt to from inside |
| 4434 | // that join block. They must be added to the Property class (when |
| 4435 | // it's a keyed property) and registered in the full codegen. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4436 | HBasicBlock* if_jsarray = graph()->CreateBasicBlock(); |
| 4437 | HBasicBlock* if_fastobject = graph()->CreateBasicBlock(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4438 | HHasInstanceTypeAndBranch* typecheck = |
| 4439 | new(zone()) HHasInstanceTypeAndBranch(object, JS_ARRAY_TYPE); |
| 4440 | typecheck->SetSuccessorAt(0, if_jsarray); |
| 4441 | typecheck->SetSuccessorAt(1, if_fastobject); |
| 4442 | current_block()->Finish(typecheck); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4443 | |
| 4444 | set_current_block(if_jsarray); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4445 | HInstruction* length; |
| 4446 | length = AddInstruction(new(zone()) HJSArrayLength(object, typecheck)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4447 | checked_key = AddInstruction(new(zone()) HBoundsCheck(key, length)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4448 | access = AddInstruction(BuildFastElementAccess( |
| 4449 | elements, checked_key, val, elements_kind, is_store)); |
| 4450 | if (!is_store) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4451 | Push(access); |
| 4452 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4453 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 4454 | *has_side_effects |= access->HasObservableSideEffects(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4455 | if (position != -1) { |
| 4456 | access->set_position(position); |
| 4457 | } |
| 4458 | if_jsarray->Goto(join); |
| 4459 | |
| 4460 | set_current_block(if_fastobject); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 4461 | length = AddInstruction(new(zone()) HFixedArrayBaseLength(elements)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4462 | checked_key = AddInstruction(new(zone()) HBoundsCheck(key, length)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4463 | access = AddInstruction(BuildFastElementAccess( |
| 4464 | elements, checked_key, val, elements_kind, is_store)); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 4465 | } else if (elements_kind == DICTIONARY_ELEMENTS) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4466 | if (is_store) { |
| 4467 | access = AddInstruction(BuildStoreKeyedGeneric(object, key, val)); |
| 4468 | } else { |
| 4469 | access = AddInstruction(BuildLoadKeyedGeneric(object, key)); |
| 4470 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4471 | } else { // External array elements. |
| 4472 | access = AddInstruction(BuildExternalArrayElementAccess( |
| 4473 | external_elements, checked_key, val, elements_kind, is_store)); |
| 4474 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 4475 | *has_side_effects |= access->HasObservableSideEffects(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4476 | access->set_position(position); |
| 4477 | if (!is_store) { |
| 4478 | Push(access); |
| 4479 | } |
| 4480 | current_block()->Goto(join); |
| 4481 | set_current_block(if_false); |
| 4482 | } |
| 4483 | } |
| 4484 | |
| 4485 | // Deopt if none of the cases matched. |
| 4486 | current_block()->FinishExitWithDeoptimization(HDeoptimize::kNoUses); |
| 4487 | join->SetJoinId(ast_id); |
| 4488 | set_current_block(join); |
| 4489 | return is_store ? NULL : Pop(); |
| 4490 | } |
| 4491 | |
| 4492 | |
| 4493 | HValue* HGraphBuilder::HandleKeyedElementAccess(HValue* obj, |
| 4494 | HValue* key, |
| 4495 | HValue* val, |
| 4496 | Expression* expr, |
| 4497 | int ast_id, |
| 4498 | int position, |
| 4499 | bool is_store, |
| 4500 | bool* has_side_effects) { |
| 4501 | ASSERT(!expr->IsPropertyName()); |
| 4502 | HInstruction* instr = NULL; |
| 4503 | if (expr->IsMonomorphic()) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4504 | Handle<Map> map = expr->GetMonomorphicReceiverType(); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 4505 | if (map->has_slow_elements_kind()) { |
| 4506 | instr = is_store ? BuildStoreKeyedGeneric(obj, key, val) |
| 4507 | : BuildLoadKeyedGeneric(obj, key); |
| 4508 | } else { |
| 4509 | AddInstruction(new(zone()) HCheckNonSmi(obj)); |
| 4510 | instr = BuildMonomorphicElementAccess(obj, key, val, map, is_store); |
| 4511 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4512 | } else if (expr->GetReceiverTypes() != NULL && |
| 4513 | !expr->GetReceiverTypes()->is_empty()) { |
| 4514 | return HandlePolymorphicElementAccess( |
| 4515 | obj, key, val, expr, ast_id, position, is_store, has_side_effects); |
| 4516 | } else { |
| 4517 | if (is_store) { |
| 4518 | instr = BuildStoreKeyedGeneric(obj, key, val); |
| 4519 | } else { |
| 4520 | instr = BuildLoadKeyedGeneric(obj, key); |
| 4521 | } |
| 4522 | } |
| 4523 | instr->set_position(position); |
| 4524 | AddInstruction(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 4525 | *has_side_effects = instr->HasObservableSideEffects(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4526 | return instr; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4527 | } |
| 4528 | |
| 4529 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4530 | HInstruction* HGraphBuilder::BuildStoreKeyedGeneric(HValue* object, |
| 4531 | HValue* key, |
| 4532 | HValue* value) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 4533 | HValue* context = environment()->LookupContext(); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4534 | return new(zone()) HStoreKeyedGeneric( |
| 4535 | context, |
| 4536 | object, |
| 4537 | key, |
| 4538 | value, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4539 | function_strict_mode_flag()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4540 | } |
| 4541 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4542 | bool HGraphBuilder::TryArgumentsAccess(Property* expr) { |
| 4543 | VariableProxy* proxy = expr->obj()->AsVariableProxy(); |
| 4544 | if (proxy == NULL) return false; |
| 4545 | if (!proxy->var()->IsStackAllocated()) return false; |
| 4546 | if (!environment()->Lookup(proxy->var())->CheckFlag(HValue::kIsArguments)) { |
| 4547 | return false; |
| 4548 | } |
| 4549 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 4550 | // Our implementation of arguments (based on this stack frame or an |
| 4551 | // adapter below it) does not work for inlined functions. |
| 4552 | if (function_state()->outer() != NULL) { |
| 4553 | Bailout("arguments access in inlined function"); |
| 4554 | return true; |
| 4555 | } |
| 4556 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4557 | HInstruction* result = NULL; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4558 | if (expr->key()->IsPropertyName()) { |
| 4559 | Handle<String> name = expr->key()->AsLiteral()->AsPropertyName(); |
| 4560 | if (!name->IsEqualTo(CStrVector("length"))) return false; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4561 | HInstruction* elements = AddInstruction(new(zone()) HArgumentsElements); |
| 4562 | result = new(zone()) HArgumentsLength(elements); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4563 | } else { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4564 | Push(graph()->GetArgumentsObject()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4565 | VisitForValue(expr->key()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4566 | if (HasStackOverflow() || current_block() == NULL) return true; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4567 | HValue* key = Pop(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4568 | Drop(1); // Arguments object. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4569 | HInstruction* elements = AddInstruction(new(zone()) HArgumentsElements); |
| 4570 | HInstruction* length = AddInstruction( |
| 4571 | new(zone()) HArgumentsLength(elements)); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4572 | HInstruction* checked_key = |
| 4573 | AddInstruction(new(zone()) HBoundsCheck(key, length)); |
| 4574 | result = new(zone()) HAccessArgumentsAt(elements, length, checked_key); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4575 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4576 | ast_context()->ReturnInstruction(result, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4577 | return true; |
| 4578 | } |
| 4579 | |
| 4580 | |
| 4581 | void HGraphBuilder::VisitProperty(Property* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4582 | ASSERT(!HasStackOverflow()); |
| 4583 | ASSERT(current_block() != NULL); |
| 4584 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4585 | expr->RecordTypeFeedback(oracle()); |
| 4586 | |
| 4587 | if (TryArgumentsAccess(expr)) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4588 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4589 | CHECK_ALIVE(VisitForValue(expr->obj())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4590 | |
| 4591 | HInstruction* instr = NULL; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 4592 | if (expr->AsProperty()->IsArrayLength()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4593 | HValue* array = Pop(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4594 | AddInstruction(new(zone()) HCheckNonSmi(array)); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 4595 | HInstruction* mapcheck = |
| 4596 | AddInstruction(HCheckInstanceType::NewIsJSArray(array)); |
| 4597 | instr = new(zone()) HJSArrayLength(array, mapcheck); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 4598 | |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4599 | } else if (expr->IsStringLength()) { |
| 4600 | HValue* string = Pop(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4601 | AddInstruction(new(zone()) HCheckNonSmi(string)); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 4602 | AddInstruction(HCheckInstanceType::NewIsString(string)); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4603 | instr = new(zone()) HStringLength(string); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 4604 | } else if (expr->IsStringAccess()) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4605 | CHECK_ALIVE(VisitForValue(expr->key())); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 4606 | HValue* index = Pop(); |
| 4607 | HValue* string = Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 4608 | HValue* context = environment()->LookupContext(); |
| 4609 | HStringCharCodeAt* char_code = |
| 4610 | BuildStringCharCodeAt(context, string, index); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 4611 | AddInstruction(char_code); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 4612 | instr = new(zone()) HStringCharFromCode(context, char_code); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4613 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 4614 | } else if (expr->IsFunctionPrototype()) { |
| 4615 | HValue* function = Pop(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4616 | AddInstruction(new(zone()) HCheckNonSmi(function)); |
| 4617 | instr = new(zone()) HLoadFunctionPrototype(function); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4618 | |
| 4619 | } else if (expr->key()->IsPropertyName()) { |
| 4620 | Handle<String> name = expr->key()->AsLiteral()->AsPropertyName(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 4621 | SmallMapList* types = expr->GetReceiverTypes(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4622 | |
| 4623 | HValue* obj = Pop(); |
| 4624 | if (expr->IsMonomorphic()) { |
| 4625 | instr = BuildLoadNamed(obj, expr, types->first(), name); |
| 4626 | } else if (types != NULL && types->length() > 1) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4627 | AddInstruction(new(zone()) HCheckNonSmi(obj)); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 4628 | HValue* context = environment()->LookupContext(); |
| 4629 | instr = new(zone()) HLoadNamedFieldPolymorphic(context, obj, types, name); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4630 | } else { |
| 4631 | instr = BuildLoadNamedGeneric(obj, expr); |
| 4632 | } |
| 4633 | |
| 4634 | } else { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4635 | CHECK_ALIVE(VisitForValue(expr->key())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4636 | |
| 4637 | HValue* key = Pop(); |
| 4638 | HValue* obj = Pop(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4639 | |
| 4640 | bool has_side_effects = false; |
| 4641 | HValue* load = HandleKeyedElementAccess( |
| 4642 | obj, key, NULL, expr, expr->id(), expr->position(), |
| 4643 | false, // is_store |
| 4644 | &has_side_effects); |
| 4645 | if (has_side_effects) { |
| 4646 | if (ast_context()->IsEffect()) { |
| 4647 | AddSimulate(expr->id()); |
| 4648 | } else { |
| 4649 | Push(load); |
| 4650 | AddSimulate(expr->id()); |
| 4651 | Drop(1); |
| 4652 | } |
| 4653 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4654 | return ast_context()->ReturnValue(load); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4655 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4656 | instr->set_position(expr->position()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4657 | return ast_context()->ReturnInstruction(instr, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | |
| 4661 | void HGraphBuilder::AddCheckConstantFunction(Call* expr, |
| 4662 | HValue* receiver, |
| 4663 | Handle<Map> receiver_map, |
| 4664 | bool smi_and_map_check) { |
| 4665 | // Constant functions have the nice property that the map will change if they |
| 4666 | // are overwritten. Therefore it is enough to check the map of the holder and |
| 4667 | // its prototypes. |
| 4668 | if (smi_and_map_check) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4669 | AddInstruction(new(zone()) HCheckNonSmi(receiver)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4670 | AddInstruction(new(zone()) HCheckMap(receiver, receiver_map, NULL, |
| 4671 | ALLOW_ELEMENT_TRANSITION_MAPS)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4672 | } |
| 4673 | if (!expr->holder().is_null()) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4674 | AddInstruction(new(zone()) HCheckPrototypeMaps( |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 4675 | Handle<JSObject>(JSObject::cast(receiver_map->prototype())), |
| 4676 | expr->holder())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4677 | } |
| 4678 | } |
| 4679 | |
| 4680 | |
| 4681 | void HGraphBuilder::HandlePolymorphicCallNamed(Call* expr, |
| 4682 | HValue* receiver, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 4683 | SmallMapList* types, |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4684 | Handle<String> name) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4685 | // TODO(ager): We should recognize when the prototype chains for different |
| 4686 | // maps are identical. In that case we can avoid repeatedly generating the |
| 4687 | // same prototype map checks. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4688 | int argument_count = expr->arguments()->length() + 1; // Includes receiver. |
| 4689 | int count = 0; |
| 4690 | HBasicBlock* join = NULL; |
| 4691 | for (int i = 0; i < types->length() && count < kMaxCallPolymorphism; ++i) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4692 | Handle<Map> map = types->at(i); |
| 4693 | if (expr->ComputeTarget(map, name)) { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4694 | if (count == 0) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4695 | // Only needed once. |
| 4696 | AddInstruction(new(zone()) HCheckNonSmi(receiver)); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4697 | join = graph()->CreateBasicBlock(); |
| 4698 | } |
| 4699 | ++count; |
| 4700 | HBasicBlock* if_true = graph()->CreateBasicBlock(); |
| 4701 | HBasicBlock* if_false = graph()->CreateBasicBlock(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4702 | HCompareMap* compare = |
| 4703 | new(zone()) HCompareMap(receiver, map, if_true, if_false); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4704 | current_block()->Finish(compare); |
| 4705 | |
| 4706 | set_current_block(if_true); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4707 | AddCheckConstantFunction(expr, receiver, map, false); |
| 4708 | if (FLAG_trace_inlining && FLAG_polymorphic_inlining) { |
| 4709 | PrintF("Trying to inline the polymorphic call to %s\n", |
| 4710 | *name->ToCString()); |
| 4711 | } |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4712 | if (FLAG_polymorphic_inlining && TryInline(expr)) { |
| 4713 | // Trying to inline will signal that we should bailout from the |
| 4714 | // entire compilation by setting stack overflow on the visitor. |
| 4715 | if (HasStackOverflow()) return; |
| 4716 | } else { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 4717 | HCallConstantFunction* call = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4718 | new(zone()) HCallConstantFunction(expr->target(), argument_count); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4719 | call->set_position(expr->position()); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4720 | PreProcessCall(call); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4721 | AddInstruction(call); |
| 4722 | if (!ast_context()->IsEffect()) Push(call); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4723 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4724 | |
| 4725 | if (current_block() != NULL) current_block()->Goto(join); |
| 4726 | set_current_block(if_false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4727 | } |
| 4728 | } |
| 4729 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4730 | // Finish up. Unconditionally deoptimize if we've handled all the maps we |
| 4731 | // know about and do not want to handle ones we've never seen. Otherwise |
| 4732 | // use a generic IC. |
| 4733 | if (count == types->length() && FLAG_deoptimize_uncommon_cases) { |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4734 | current_block()->FinishExitWithDeoptimization(HDeoptimize::kNoUses); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4735 | } else { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 4736 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4737 | HCallNamed* call = new(zone()) HCallNamed(context, name, argument_count); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4738 | call->set_position(expr->position()); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4739 | PreProcessCall(call); |
lrn@chromium.org | 8541d77 | 2010-12-15 12:05:09 +0000 | [diff] [blame] | 4740 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4741 | if (join != NULL) { |
| 4742 | AddInstruction(call); |
| 4743 | if (!ast_context()->IsEffect()) Push(call); |
| 4744 | current_block()->Goto(join); |
| 4745 | } else { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4746 | return ast_context()->ReturnInstruction(call, expr->id()); |
kmillikin@chromium.org | c278c96 | 2011-01-06 08:52:11 +0000 | [diff] [blame] | 4747 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4748 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4749 | |
| 4750 | // We assume that control flow is always live after an expression. So |
| 4751 | // even without predecessors to the join block, we set it as the exit |
| 4752 | // block and continue by adding instructions there. |
| 4753 | ASSERT(join != NULL); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4754 | if (join->HasPredecessor()) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4755 | set_current_block(join); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4756 | join->SetJoinId(expr->id()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4757 | if (!ast_context()->IsEffect()) return ast_context()->ReturnValue(Pop()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4758 | } else { |
| 4759 | set_current_block(NULL); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4760 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4761 | } |
| 4762 | |
| 4763 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 4764 | void HGraphBuilder::TraceInline(Handle<JSFunction> target, |
| 4765 | Handle<JSFunction> caller, |
| 4766 | const char* reason) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4767 | if (FLAG_trace_inlining) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 4768 | SmartArrayPointer<char> target_name = |
| 4769 | target->shared()->DebugName()->ToCString(); |
| 4770 | SmartArrayPointer<char> caller_name = |
| 4771 | caller->shared()->DebugName()->ToCString(); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4772 | if (reason == NULL) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 4773 | PrintF("Inlined %s called from %s.\n", *target_name, *caller_name); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4774 | } else { |
| 4775 | PrintF("Did not inline %s called from %s (%s).\n", |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 4776 | *target_name, *caller_name, reason); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4777 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4778 | } |
| 4779 | } |
| 4780 | |
| 4781 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4782 | bool HGraphBuilder::TryInline(Call* expr, bool drop_extra) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4783 | if (!FLAG_use_inlining) return false; |
| 4784 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 4785 | // The function call we are inlining is a method call if the call |
| 4786 | // is a property call. |
| 4787 | CallKind call_kind = (expr->expression()->AsProperty() == NULL) |
| 4788 | ? CALL_AS_FUNCTION |
| 4789 | : CALL_AS_METHOD; |
| 4790 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4791 | // Precondition: call is monomorphic and we have found a target with the |
| 4792 | // appropriate arity. |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 4793 | Handle<JSFunction> caller = info()->closure(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4794 | Handle<JSFunction> target = expr->target(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 4795 | Handle<SharedFunctionInfo> target_shared(target->shared()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4796 | |
| 4797 | // Do a quick check on source code length to avoid parsing large |
| 4798 | // inlining candidates. |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 4799 | if ((FLAG_limit_inlining && target->shared()->SourceSize() > kMaxSourceSize) |
| 4800 | || target->shared()->SourceSize() > kUnlimitedMaxSourceSize) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 4801 | TraceInline(target, caller, "target text too big"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4802 | return false; |
| 4803 | } |
| 4804 | |
| 4805 | // Target must be inlineable. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4806 | if (!target->IsInlineable()) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 4807 | TraceInline(target, caller, "target not inlineable"); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4808 | return false; |
| 4809 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4810 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4811 | #if !defined(V8_TARGET_ARCH_IA32) |
| 4812 | // Target must be able to use caller's context. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4813 | CompilationInfo* outer_info = info(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4814 | if (target->context() != outer_info->closure()->context() || |
| 4815 | outer_info->scope()->contains_with() || |
| 4816 | outer_info->scope()->num_heap_slots() > 0) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 4817 | TraceInline(target, caller, "target requires context change"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4818 | return false; |
| 4819 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4820 | #endif |
| 4821 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4822 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4823 | // Don't inline deeper than kMaxInliningLevels calls. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4824 | HEnvironment* env = environment(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4825 | int current_level = 1; |
| 4826 | while (env->outer() != NULL) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4827 | if (current_level == Compiler::kMaxInliningLevels) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 4828 | TraceInline(target, caller, "inline depth limit reached"); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4829 | return false; |
| 4830 | } |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 4831 | if (!env->outer()->is_arguments_adaptor()) { |
| 4832 | current_level++; |
| 4833 | } |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4834 | env = env->outer(); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4835 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4836 | |
| 4837 | // Don't inline recursive functions. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4838 | for (FunctionState* state = function_state(); |
| 4839 | state != NULL; |
| 4840 | state = state->outer()) { |
| 4841 | if (state->compilation_info()->closure()->shared() == *target_shared) { |
| 4842 | TraceInline(target, caller, "target is recursive"); |
| 4843 | return false; |
| 4844 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4845 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4846 | |
| 4847 | // We don't want to add more than a certain number of nodes from inlining. |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 4848 | if ((FLAG_limit_inlining && inlined_count_ > kMaxInlinedNodes) || |
| 4849 | inlined_count_ > kUnlimitedMaxInlinedNodes) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 4850 | TraceInline(target, caller, "cumulative AST node limit reached"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4851 | return false; |
| 4852 | } |
| 4853 | |
| 4854 | int count_before = AstNode::Count(); |
| 4855 | |
| 4856 | // Parse and allocate variables. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4857 | CompilationInfo target_info(target); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4858 | if (!ParserApi::Parse(&target_info, kNoParsingFlags) || |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4859 | !Scope::Analyze(&target_info)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4860 | if (target_info.isolate()->has_pending_exception()) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4861 | // Parse or scope error, never optimize this function. |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4862 | SetStackOverflow(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 4863 | target_shared->DisableOptimization(*target); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4864 | } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 4865 | TraceInline(target, caller, "parse failure"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4866 | return false; |
| 4867 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4868 | |
| 4869 | if (target_info.scope()->num_heap_slots() > 0) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 4870 | TraceInline(target, caller, "target has context-allocated variables"); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4871 | return false; |
| 4872 | } |
| 4873 | FunctionLiteral* function = target_info.function(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4874 | |
| 4875 | // Count the number of AST nodes added by inlining this call. |
| 4876 | int nodes_added = AstNode::Count() - count_before; |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 4877 | if ((FLAG_limit_inlining && nodes_added > kMaxInlinedSize) || |
| 4878 | nodes_added > kUnlimitedMaxInlinedSize) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 4879 | TraceInline(target, caller, "target AST is too large"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4880 | return false; |
| 4881 | } |
| 4882 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 4883 | // Don't inline functions that uses the arguments object. |
| 4884 | if (function->scope()->arguments() != NULL) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 4885 | TraceInline(target, caller, "target requires special argument handling"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4886 | return false; |
| 4887 | } |
| 4888 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 4889 | // All declarations must be inlineable. |
| 4890 | ZoneList<Declaration*>* decls = target_info.scope()->declarations(); |
| 4891 | int decl_count = decls->length(); |
| 4892 | for (int i = 0; i < decl_count; ++i) { |
| 4893 | if (!decls->at(i)->IsInlineable()) { |
| 4894 | TraceInline(target, caller, "target has non-trivial declaration"); |
| 4895 | return false; |
| 4896 | } |
| 4897 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4898 | // All statements in the body must be inlineable. |
| 4899 | for (int i = 0, count = function->body()->length(); i < count; ++i) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4900 | if (!function->body()->at(i)->IsInlineable()) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 4901 | TraceInline(target, caller, "target contains unsupported syntax"); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4902 | return false; |
| 4903 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
| 4906 | // Generate the deoptimization data for the unoptimized version of |
| 4907 | // the target function if we don't already have it. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4908 | if (!target_shared->has_deoptimization_support()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4909 | // Note that we compile here using the same AST that we will use for |
| 4910 | // generating the optimized inline code. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4911 | target_info.EnableDeoptimizationSupport(); |
| 4912 | if (!FullCodeGenerator::MakeCode(&target_info)) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 4913 | TraceInline(target, caller, "could not generate deoptimization info"); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4914 | return false; |
| 4915 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 4916 | if (target_shared->scope_info() == ScopeInfo::Empty()) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4917 | // The scope info might not have been set if a lazily compiled |
| 4918 | // function is inlined before being called for the first time. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 4919 | Handle<ScopeInfo> target_scope_info = |
| 4920 | ScopeInfo::Create(target_info.scope()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4921 | target_shared->set_scope_info(*target_scope_info); |
| 4922 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4923 | target_shared->EnableDeoptimizationSupport(*target_info.code()); |
| 4924 | Compiler::RecordFunctionCompilation(Logger::FUNCTION_TAG, |
| 4925 | &target_info, |
| 4926 | target_shared); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4929 | // ---------------------------------------------------------------- |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 4930 | // After this point, we've made a decision to inline this function (so |
| 4931 | // TryInline should always return true). |
| 4932 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4933 | // Save the pending call context and type feedback oracle. Set up new ones |
| 4934 | // for the inlined function. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4935 | ASSERT(target_shared->has_deoptimization_support()); |
| 4936 | TypeFeedbackOracle target_oracle( |
| 4937 | Handle<Code>(target_shared->code()), |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 4938 | Handle<Context>(target->context()->global_context()), |
| 4939 | isolate()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4940 | // The function state is new-allocated because we need to delete it |
| 4941 | // in two different places. |
| 4942 | FunctionState* target_state = |
| 4943 | new FunctionState(this, &target_info, &target_oracle, drop_extra); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4944 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4945 | HConstant* undefined = graph()->GetConstantUndefined(); |
| 4946 | HEnvironment* inner_env = |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 4947 | environment()->CopyForInlining(target, |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 4948 | expr->arguments()->length(), |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 4949 | function, |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 4950 | undefined, |
| 4951 | call_kind); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4952 | #ifdef V8_TARGET_ARCH_IA32 |
| 4953 | // IA32 only, overwrite the caller's context in the deoptimization |
| 4954 | // environment with the correct one. |
| 4955 | // |
| 4956 | // TODO(kmillikin): implement the same inlining on other platforms so we |
| 4957 | // can remove the unsightly ifdefs in this function. |
| 4958 | HConstant* context = new HConstant(Handle<Context>(target->context()), |
| 4959 | Representation::Tagged()); |
| 4960 | AddInstruction(context); |
| 4961 | inner_env->BindContext(context); |
| 4962 | #endif |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4963 | HBasicBlock* body_entry = CreateBasicBlock(inner_env); |
| 4964 | current_block()->Goto(body_entry); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4965 | body_entry->SetJoinId(expr->ReturnId()); |
| 4966 | set_current_block(body_entry); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 4967 | AddInstruction(new(zone()) HEnterInlined(target, |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 4968 | expr->arguments()->length(), |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 4969 | function, |
| 4970 | call_kind)); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 4971 | VisitDeclarations(target_info.scope()->declarations()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4972 | VisitStatements(function->body()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4973 | if (HasStackOverflow()) { |
| 4974 | // Bail out if the inline function did, as we cannot residualize a call |
| 4975 | // instead. |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 4976 | TraceInline(target, caller, "inline graph construction failed"); |
| 4977 | target_shared->DisableOptimization(*target); |
| 4978 | inline_bailout_ = true; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4979 | delete target_state; |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4980 | return true; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4981 | } |
| 4982 | |
| 4983 | // Update inlined nodes count. |
| 4984 | inlined_count_ += nodes_added; |
| 4985 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 4986 | TraceInline(target, caller, NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4987 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4988 | if (current_block() != NULL) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4989 | // Add a return of undefined if control can fall off the body. In a |
| 4990 | // test context, undefined is false. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4991 | if (inlined_test_context() == NULL) { |
| 4992 | ASSERT(function_return() != NULL); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4993 | ASSERT(call_context()->IsEffect() || call_context()->IsValue()); |
| 4994 | if (call_context()->IsEffect()) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4995 | current_block()->Goto(function_return(), drop_extra); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4996 | } else { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4997 | current_block()->AddLeaveInlined(undefined, |
| 4998 | function_return(), |
| 4999 | drop_extra); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 5000 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5001 | } else { |
| 5002 | // The graph builder assumes control can reach both branches of a |
| 5003 | // test, so we materialize the undefined value and test it rather than |
| 5004 | // simply jumping to the false target. |
| 5005 | // |
| 5006 | // TODO(3168478): refactor to avoid this. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5007 | ASSERT(call_context()->IsTest()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5008 | HBasicBlock* empty_true = graph()->CreateBasicBlock(); |
| 5009 | HBasicBlock* empty_false = graph()->CreateBasicBlock(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5010 | HBranch* test = new(zone()) HBranch(undefined, empty_true, empty_false); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 5011 | current_block()->Finish(test); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5012 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5013 | empty_true->Goto(inlined_test_context()->if_true(), drop_extra); |
| 5014 | empty_false->Goto(inlined_test_context()->if_false(), drop_extra); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5015 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5016 | } |
| 5017 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5018 | // Fix up the function exits. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 5019 | if (inlined_test_context() != NULL) { |
| 5020 | HBasicBlock* if_true = inlined_test_context()->if_true(); |
| 5021 | HBasicBlock* if_false = inlined_test_context()->if_false(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5022 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 5023 | // Pop the return test context from the expression context stack. |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5024 | ASSERT(ast_context() == inlined_test_context()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 5025 | ClearInlinedTestContext(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5026 | delete target_state; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5027 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5028 | // Forward to the real test context. |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5029 | if (if_true->HasPredecessor()) { |
| 5030 | if_true->SetJoinId(expr->id()); |
| 5031 | HBasicBlock* true_target = TestContext::cast(ast_context())->if_true(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5032 | if_true->Goto(true_target, function_state()->drop_extra()); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5033 | } |
| 5034 | if (if_false->HasPredecessor()) { |
| 5035 | if_false->SetJoinId(expr->id()); |
| 5036 | HBasicBlock* false_target = TestContext::cast(ast_context())->if_false(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5037 | if_false->Goto(false_target, function_state()->drop_extra()); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5038 | } |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 5039 | set_current_block(NULL); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5040 | return true; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5041 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5042 | } else if (function_return()->HasPredecessor()) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 5043 | function_return()->SetJoinId(expr->id()); |
| 5044 | set_current_block(function_return()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5045 | } else { |
| 5046 | set_current_block(NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5047 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5048 | delete target_state; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5049 | return true; |
| 5050 | } |
| 5051 | |
| 5052 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5053 | bool HGraphBuilder::TryInlineBuiltinFunction(Call* expr, |
| 5054 | HValue* receiver, |
| 5055 | Handle<Map> receiver_map, |
| 5056 | CheckType check_type) { |
| 5057 | ASSERT(check_type != RECEIVER_MAP_CHECK || !receiver_map.is_null()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5058 | // Try to inline calls like Math.* as operations in the calling function. |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5059 | if (!expr->target()->shared()->HasBuiltinFunctionId()) return false; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5060 | BuiltinFunctionId id = expr->target()->shared()->builtin_function_id(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5061 | int argument_count = expr->arguments()->length() + 1; // Plus receiver. |
| 5062 | switch (id) { |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5063 | case kStringCharCodeAt: |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 5064 | case kStringCharAt: |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5065 | if (argument_count == 2 && check_type == STRING_CHECK) { |
| 5066 | HValue* index = Pop(); |
| 5067 | HValue* string = Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5068 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5069 | ASSERT(!expr->holder().is_null()); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5070 | AddInstruction(new(zone()) HCheckPrototypeMaps( |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5071 | oracle()->GetPrototypeForPrimitiveCheck(STRING_CHECK), |
| 5072 | expr->holder())); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5073 | HStringCharCodeAt* char_code = |
| 5074 | BuildStringCharCodeAt(context, string, index); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 5075 | if (id == kStringCharCodeAt) { |
| 5076 | ast_context()->ReturnInstruction(char_code, expr->id()); |
| 5077 | return true; |
| 5078 | } |
| 5079 | AddInstruction(char_code); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5080 | HStringCharFromCode* result = |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5081 | new(zone()) HStringCharFromCode(context, char_code); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5082 | ast_context()->ReturnInstruction(result, expr->id()); |
| 5083 | return true; |
| 5084 | } |
| 5085 | break; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5086 | case kMathRound: |
| 5087 | case kMathFloor: |
| 5088 | case kMathAbs: |
| 5089 | case kMathSqrt: |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5090 | case kMathLog: |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 5091 | case kMathSin: |
| 5092 | case kMathCos: |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5093 | if (argument_count == 2 && check_type == RECEIVER_MAP_CHECK) { |
| 5094 | AddCheckConstantFunction(expr, receiver, receiver_map, true); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5095 | HValue* argument = Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5096 | HValue* context = environment()->LookupContext(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5097 | Drop(1); // Receiver. |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5098 | HUnaryMathOperation* op = |
| 5099 | new(zone()) HUnaryMathOperation(context, argument, id); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5100 | op->set_position(expr->position()); |
| 5101 | ast_context()->ReturnInstruction(op, expr->id()); |
| 5102 | return true; |
| 5103 | } |
| 5104 | break; |
| 5105 | case kMathPow: |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5106 | if (argument_count == 3 && check_type == RECEIVER_MAP_CHECK) { |
| 5107 | AddCheckConstantFunction(expr, receiver, receiver_map, true); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5108 | HValue* right = Pop(); |
| 5109 | HValue* left = Pop(); |
| 5110 | Pop(); // Pop receiver. |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5111 | HValue* context = environment()->LookupContext(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5112 | HInstruction* result = NULL; |
| 5113 | // Use sqrt() if exponent is 0.5 or -0.5. |
| 5114 | if (right->IsConstant() && HConstant::cast(right)->HasDoubleValue()) { |
| 5115 | double exponent = HConstant::cast(right)->DoubleValue(); |
| 5116 | if (exponent == 0.5) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5117 | result = |
| 5118 | new(zone()) HUnaryMathOperation(context, left, kMathPowHalf); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5119 | } else if (exponent == -0.5) { |
| 5120 | HConstant* double_one = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5121 | new(zone()) HConstant(Handle<Object>(Smi::FromInt(1)), |
| 5122 | Representation::Double()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5123 | AddInstruction(double_one); |
| 5124 | HUnaryMathOperation* square_root = |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5125 | new(zone()) HUnaryMathOperation(context, left, kMathPowHalf); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5126 | AddInstruction(square_root); |
| 5127 | // MathPowHalf doesn't have side effects so there's no need for |
| 5128 | // an environment simulation here. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5129 | ASSERT(!square_root->HasObservableSideEffects()); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5130 | result = new(zone()) HDiv(context, double_one, square_root); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5131 | } else if (exponent == 2.0) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5132 | result = new(zone()) HMul(context, left, left); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5133 | } |
| 5134 | } else if (right->IsConstant() && |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5135 | HConstant::cast(right)->HasInteger32Value() && |
| 5136 | HConstant::cast(right)->Integer32Value() == 2) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5137 | result = new(zone()) HMul(context, left, left); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5138 | } |
| 5139 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5140 | if (result == NULL) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5141 | result = new(zone()) HPower(left, right); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5142 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5143 | ast_context()->ReturnInstruction(result, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5144 | return true; |
| 5145 | } |
| 5146 | break; |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5147 | case kMathRandom: |
| 5148 | if (argument_count == 1 && check_type == RECEIVER_MAP_CHECK) { |
| 5149 | AddCheckConstantFunction(expr, receiver, receiver_map, true); |
| 5150 | Drop(1); |
| 5151 | HValue* context = environment()->LookupContext(); |
| 5152 | HGlobalObject* global_object = new(zone()) HGlobalObject(context); |
| 5153 | AddInstruction(global_object); |
| 5154 | HRandom* result = new(zone()) HRandom(global_object); |
| 5155 | ast_context()->ReturnInstruction(result, expr->id()); |
| 5156 | return true; |
| 5157 | } |
| 5158 | break; |
| 5159 | case kMathMax: |
| 5160 | case kMathMin: |
| 5161 | if (argument_count == 3 && check_type == RECEIVER_MAP_CHECK) { |
| 5162 | AddCheckConstantFunction(expr, receiver, receiver_map, true); |
| 5163 | HValue* right = Pop(); |
| 5164 | HValue* left = Pop(); |
| 5165 | // Do not inline if the return representation is not certain. |
| 5166 | if (!left->representation().Equals(right->representation())) { |
| 5167 | Push(left); |
| 5168 | Push(right); |
| 5169 | return false; |
| 5170 | } |
| 5171 | |
| 5172 | Pop(); // Pop receiver. |
| 5173 | Token::Value op = (id == kMathMin) ? Token::LT : Token::GT; |
| 5174 | HCompareIDAndBranch* compare = NULL; |
| 5175 | |
| 5176 | if (left->representation().IsTagged()) { |
| 5177 | HChange* left_cvt = |
| 5178 | new(zone()) HChange(left, Representation::Double(), false, true); |
| 5179 | left_cvt->SetFlag(HValue::kBailoutOnMinusZero); |
| 5180 | AddInstruction(left_cvt); |
| 5181 | HChange* right_cvt = |
| 5182 | new(zone()) HChange(right, Representation::Double(), false, true); |
| 5183 | right_cvt->SetFlag(HValue::kBailoutOnMinusZero); |
| 5184 | AddInstruction(right_cvt); |
| 5185 | compare = new(zone()) HCompareIDAndBranch(left_cvt, right_cvt, op); |
| 5186 | compare->SetInputRepresentation(Representation::Double()); |
| 5187 | } else { |
| 5188 | compare = new(zone()) HCompareIDAndBranch(left, right, op); |
| 5189 | compare->SetInputRepresentation(left->representation()); |
| 5190 | } |
| 5191 | |
| 5192 | HBasicBlock* return_left = graph()->CreateBasicBlock(); |
| 5193 | HBasicBlock* return_right = graph()->CreateBasicBlock(); |
| 5194 | |
| 5195 | compare->SetSuccessorAt(0, return_left); |
| 5196 | compare->SetSuccessorAt(1, return_right); |
| 5197 | current_block()->Finish(compare); |
| 5198 | |
| 5199 | set_current_block(return_left); |
| 5200 | Push(left); |
| 5201 | set_current_block(return_right); |
| 5202 | Push(right); |
| 5203 | |
| 5204 | HBasicBlock* join = CreateJoin(return_left, return_right, expr->id()); |
| 5205 | set_current_block(join); |
| 5206 | ast_context()->ReturnValue(Pop()); |
| 5207 | return true; |
| 5208 | } |
| 5209 | break; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5210 | default: |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5211 | // Not yet supported for inlining. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5212 | break; |
| 5213 | } |
| 5214 | return false; |
| 5215 | } |
| 5216 | |
| 5217 | |
| 5218 | bool HGraphBuilder::TryCallApply(Call* expr) { |
| 5219 | Expression* callee = expr->expression(); |
| 5220 | Property* prop = callee->AsProperty(); |
| 5221 | ASSERT(prop != NULL); |
| 5222 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 5223 | if (!expr->IsMonomorphic() || expr->check_type() != RECEIVER_MAP_CHECK) { |
| 5224 | return false; |
| 5225 | } |
| 5226 | Handle<Map> function_map = expr->GetReceiverTypes()->first(); |
| 5227 | if (function_map->instance_type() != JS_FUNCTION_TYPE || |
| 5228 | !expr->target()->shared()->HasBuiltinFunctionId() || |
| 5229 | expr->target()->shared()->builtin_function_id() != kFunctionApply) { |
| 5230 | return false; |
| 5231 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5232 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 5233 | if (info()->scope()->arguments() == NULL) return false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5234 | |
| 5235 | ZoneList<Expression*>* args = expr->arguments(); |
| 5236 | if (args->length() != 2) return false; |
| 5237 | |
| 5238 | VariableProxy* arg_two = args->at(1)->AsVariableProxy(); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 5239 | if (arg_two == NULL || !arg_two->var()->IsStackAllocated()) return false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5240 | HValue* arg_two_value = environment()->Lookup(arg_two->var()); |
| 5241 | if (!arg_two_value->CheckFlag(HValue::kIsArguments)) return false; |
| 5242 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5243 | // Our implementation of arguments (based on this stack frame or an |
| 5244 | // adapter below it) does not work for inlined functions. |
| 5245 | if (function_state()->outer() != NULL) { |
| 5246 | Bailout("Function.prototype.apply optimization in inlined function"); |
| 5247 | return true; |
| 5248 | } |
| 5249 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5250 | // Found pattern f.apply(receiver, arguments). |
| 5251 | VisitForValue(prop->obj()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5252 | if (HasStackOverflow() || current_block() == NULL) return true; |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 5253 | HValue* function = Top(); |
| 5254 | AddCheckConstantFunction(expr, function, function_map, true); |
| 5255 | Drop(1); |
| 5256 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5257 | VisitForValue(args->at(0)); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5258 | if (HasStackOverflow() || current_block() == NULL) return true; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5259 | HValue* receiver = Pop(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5260 | HInstruction* elements = AddInstruction(new(zone()) HArgumentsElements); |
| 5261 | HInstruction* length = AddInstruction(new(zone()) HArgumentsLength(elements)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5262 | HInstruction* result = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5263 | new(zone()) HApplyArguments(function, receiver, length, elements); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5264 | result->set_position(expr->position()); |
| 5265 | ast_context()->ReturnInstruction(result, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5266 | return true; |
| 5267 | } |
| 5268 | |
| 5269 | |
| 5270 | void HGraphBuilder::VisitCall(Call* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5271 | ASSERT(!HasStackOverflow()); |
| 5272 | ASSERT(current_block() != NULL); |
| 5273 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5274 | Expression* callee = expr->expression(); |
| 5275 | int argument_count = expr->arguments()->length() + 1; // Plus receiver. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 5276 | HInstruction* call = NULL; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5277 | |
| 5278 | Property* prop = callee->AsProperty(); |
| 5279 | if (prop != NULL) { |
| 5280 | if (!prop->key()->IsPropertyName()) { |
| 5281 | // Keyed function call. |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 5282 | CHECK_ALIVE(VisitArgument(prop->obj())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5283 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5284 | CHECK_ALIVE(VisitForValue(prop->key())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5285 | // Push receiver and key like the non-optimized code generator expects it. |
| 5286 | HValue* key = Pop(); |
| 5287 | HValue* receiver = Pop(); |
| 5288 | Push(key); |
| 5289 | Push(receiver); |
| 5290 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 5291 | CHECK_ALIVE(VisitArgumentList(expr->arguments())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5292 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5293 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 5294 | call = new(zone()) HCallKeyed(context, key, argument_count); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5295 | call->set_position(expr->position()); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 5296 | Drop(argument_count + 1); // 1 is the key. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5297 | return ast_context()->ReturnInstruction(call, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5298 | } |
| 5299 | |
| 5300 | // Named function call. |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 5301 | expr->RecordTypeFeedback(oracle(), CALL_AS_METHOD); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5302 | |
| 5303 | if (TryCallApply(expr)) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5304 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5305 | CHECK_ALIVE(VisitForValue(prop->obj())); |
| 5306 | CHECK_ALIVE(VisitExpressions(expr->arguments())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5307 | |
| 5308 | Handle<String> name = prop->key()->AsLiteral()->AsPropertyName(); |
| 5309 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5310 | SmallMapList* types = expr->GetReceiverTypes(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5311 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 5312 | HValue* receiver = |
| 5313 | environment()->ExpressionStackAt(expr->arguments()->length()); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5314 | if (expr->IsMonomorphic()) { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5315 | Handle<Map> receiver_map = (types == NULL || types->is_empty()) |
| 5316 | ? Handle<Map>::null() |
| 5317 | : types->first(); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5318 | if (TryInlineBuiltinFunction(expr, |
| 5319 | receiver, |
| 5320 | receiver_map, |
| 5321 | expr->check_type())) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5322 | return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5323 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5324 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5325 | if (CallStubCompiler::HasCustomCallGenerator(expr->target()) || |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5326 | expr->check_type() != RECEIVER_MAP_CHECK) { |
| 5327 | // When the target has a custom call IC generator, use the IC, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5328 | // because it is likely to generate better code. Also use the IC |
| 5329 | // when a primitive receiver check is required. |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5330 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5331 | call = PreProcessCall( |
| 5332 | new(zone()) HCallNamed(context, name, argument_count)); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5333 | } else { |
| 5334 | AddCheckConstantFunction(expr, receiver, receiver_map, true); |
| 5335 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5336 | if (TryInline(expr)) return; |
| 5337 | call = PreProcessCall( |
| 5338 | new(zone()) HCallConstantFunction(expr->target(), |
| 5339 | argument_count)); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5340 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5341 | } else if (types != NULL && types->length() > 1) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5342 | ASSERT(expr->check_type() == RECEIVER_MAP_CHECK); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5343 | HandlePolymorphicCallNamed(expr, receiver, types, name); |
| 5344 | return; |
| 5345 | |
| 5346 | } else { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5347 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5348 | call = PreProcessCall( |
| 5349 | new(zone()) HCallNamed(context, name, argument_count)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5350 | } |
| 5351 | |
| 5352 | } else { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5353 | expr->RecordTypeFeedback(oracle(), CALL_AS_FUNCTION); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5354 | VariableProxy* proxy = expr->expression()->AsVariableProxy(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5355 | bool global_call = proxy != NULL && proxy->var()->IsUnallocated(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5356 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5357 | if (global_call) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5358 | Variable* var = proxy->var(); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 5359 | bool known_global_function = false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5360 | // If there is a global property cell for the name at compile time and |
| 5361 | // access check is not enabled we assume that the function will not change |
| 5362 | // and generate optimized code for calling the function. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5363 | LookupResult lookup(isolate()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5364 | GlobalPropertyAccess type = LookupGlobalProperty(var, &lookup, false); |
| 5365 | if (type == kUseCell && |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 5366 | !info()->global_object()->IsAccessCheckNeeded()) { |
| 5367 | Handle<GlobalObject> global(info()->global_object()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5368 | known_global_function = expr->ComputeGlobalTarget(global, &lookup); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 5369 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5370 | if (known_global_function) { |
| 5371 | // Push the global object instead of the global receiver because |
| 5372 | // code generated by the full code generator expects it. |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5373 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5374 | HGlobalObject* global_object = new(zone()) HGlobalObject(context); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 5375 | PushAndAdd(global_object); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5376 | CHECK_ALIVE(VisitExpressions(expr->arguments())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5377 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5378 | CHECK_ALIVE(VisitForValue(expr->expression())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5379 | HValue* function = Pop(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5380 | AddInstruction(new(zone()) HCheckFunction(function, expr->target())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5381 | |
| 5382 | // Replace the global object with the global receiver. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5383 | HGlobalReceiver* global_receiver = |
| 5384 | new(zone()) HGlobalReceiver(global_object); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5385 | // Index of the receiver from the top of the expression stack. |
| 5386 | const int receiver_index = argument_count - 1; |
| 5387 | AddInstruction(global_receiver); |
| 5388 | ASSERT(environment()->ExpressionStackAt(receiver_index)-> |
| 5389 | IsGlobalObject()); |
| 5390 | environment()->SetExpressionStackAt(receiver_index, global_receiver); |
| 5391 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5392 | if (TryInline(expr)) return; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5393 | call = PreProcessCall(new(zone()) HCallKnownGlobal(expr->target(), |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5394 | argument_count)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5395 | } else { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5396 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 5397 | HGlobalObject* receiver = new(zone()) HGlobalObject(context); |
| 5398 | AddInstruction(receiver); |
| 5399 | PushAndAdd(new(zone()) HPushArgument(receiver)); |
| 5400 | CHECK_ALIVE(VisitArgumentList(expr->arguments())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5401 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 5402 | call = new(zone()) HCallGlobal(context, var->name(), argument_count); |
| 5403 | Drop(argument_count); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5404 | } |
| 5405 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5406 | } else if (expr->IsMonomorphic()) { |
| 5407 | // The function is on the stack in the unoptimized code during |
| 5408 | // evaluation of the arguments. |
| 5409 | CHECK_ALIVE(VisitForValue(expr->expression())); |
| 5410 | HValue* function = Top(); |
| 5411 | HValue* context = environment()->LookupContext(); |
| 5412 | HGlobalObject* global = new(zone()) HGlobalObject(context); |
| 5413 | HGlobalReceiver* receiver = new(zone()) HGlobalReceiver(global); |
| 5414 | AddInstruction(global); |
| 5415 | PushAndAdd(receiver); |
| 5416 | CHECK_ALIVE(VisitExpressions(expr->arguments())); |
| 5417 | AddInstruction(new(zone()) HCheckFunction(function, expr->target())); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5418 | if (TryInline(expr, true)) { // Drop function from environment. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5419 | return; |
| 5420 | } else { |
| 5421 | call = PreProcessCall(new(zone()) HInvokeFunction(context, |
| 5422 | function, |
| 5423 | argument_count)); |
| 5424 | Drop(1); // The function. |
| 5425 | } |
| 5426 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5427 | } else { |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 5428 | CHECK_ALIVE(VisitForValue(expr->expression())); |
| 5429 | HValue* function = Top(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5430 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5431 | HGlobalObject* global_object = new(zone()) HGlobalObject(context); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 5432 | HGlobalReceiver* receiver = new(zone()) HGlobalReceiver(global_object); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 5433 | AddInstruction(global_object); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 5434 | AddInstruction(receiver); |
| 5435 | PushAndAdd(new(zone()) HPushArgument(receiver)); |
| 5436 | CHECK_ALIVE(VisitArgumentList(expr->arguments())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5437 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 5438 | call = new(zone()) HCallFunction(context, function, argument_count); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 5439 | Drop(argument_count + 1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5440 | } |
| 5441 | } |
| 5442 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5443 | call->set_position(expr->position()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5444 | return ast_context()->ReturnInstruction(call, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5445 | } |
| 5446 | |
| 5447 | |
| 5448 | void HGraphBuilder::VisitCallNew(CallNew* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5449 | ASSERT(!HasStackOverflow()); |
| 5450 | ASSERT(current_block() != NULL); |
| 5451 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5452 | // The constructor function is also used as the receiver argument to the |
| 5453 | // JS construct call builtin. |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 5454 | HValue* constructor = NULL; |
| 5455 | CHECK_ALIVE(constructor = VisitArgument(expr->expression())); |
| 5456 | CHECK_ALIVE(VisitArgumentList(expr->arguments())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5457 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5458 | HValue* context = environment()->LookupContext(); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 5459 | |
| 5460 | // The constructor is both an operand to the instruction and an argument |
| 5461 | // to the construct call. |
| 5462 | int arg_count = expr->arguments()->length() + 1; // Plus constructor. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5463 | HCallNew* call = new(zone()) HCallNew(context, constructor, arg_count); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5464 | call->set_position(expr->position()); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 5465 | Drop(arg_count); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5466 | return ast_context()->ReturnInstruction(call, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5467 | } |
| 5468 | |
| 5469 | |
| 5470 | // Support for generating inlined runtime functions. |
| 5471 | |
| 5472 | // Lookup table for generators for runtime calls that are generated inline. |
| 5473 | // Elements of the table are member pointers to functions of HGraphBuilder. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5474 | #define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \ |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5475 | &HGraphBuilder::Generate##Name, |
| 5476 | |
| 5477 | const HGraphBuilder::InlineFunctionGenerator |
| 5478 | HGraphBuilder::kInlineFunctionGenerators[] = { |
| 5479 | INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS) |
| 5480 | INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS) |
| 5481 | }; |
| 5482 | #undef INLINE_FUNCTION_GENERATOR_ADDRESS |
| 5483 | |
| 5484 | |
| 5485 | void HGraphBuilder::VisitCallRuntime(CallRuntime* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5486 | ASSERT(!HasStackOverflow()); |
| 5487 | ASSERT(current_block() != NULL); |
| 5488 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5489 | if (expr->is_jsruntime()) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5490 | return Bailout("call to a JavaScript runtime function"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5491 | } |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 5492 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5493 | const Runtime::Function* function = expr->function(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5494 | ASSERT(function != NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5495 | if (function->intrinsic_type == Runtime::INLINE) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 5496 | ASSERT(expr->name()->length() > 0); |
| 5497 | ASSERT(expr->name()->Get(0) == '_'); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5498 | // Call to an inline function. |
| 5499 | int lookup_index = static_cast<int>(function->function_id) - |
| 5500 | static_cast<int>(Runtime::kFirstInlineFunction); |
| 5501 | ASSERT(lookup_index >= 0); |
| 5502 | ASSERT(static_cast<size_t>(lookup_index) < |
| 5503 | ARRAY_SIZE(kInlineFunctionGenerators)); |
| 5504 | InlineFunctionGenerator generator = kInlineFunctionGenerators[lookup_index]; |
| 5505 | |
| 5506 | // Call the inline code generator using the pointer-to-member. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 5507 | (this->*generator)(expr); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5508 | } else { |
| 5509 | ASSERT(function->intrinsic_type == Runtime::RUNTIME); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5510 | CHECK_ALIVE(VisitArgumentList(expr->arguments())); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 5511 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5512 | HValue* context = environment()->LookupContext(); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 5513 | Handle<String> name = expr->name(); |
| 5514 | int argument_count = expr->arguments()->length(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5515 | HCallRuntime* call = |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5516 | new(zone()) HCallRuntime(context, name, function, argument_count); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5517 | call->set_position(RelocInfo::kNoPosition); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 5518 | Drop(argument_count); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5519 | return ast_context()->ReturnInstruction(call, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5520 | } |
| 5521 | } |
| 5522 | |
| 5523 | |
| 5524 | void HGraphBuilder::VisitUnaryOperation(UnaryOperation* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5525 | ASSERT(!HasStackOverflow()); |
| 5526 | ASSERT(current_block() != NULL); |
| 5527 | ASSERT(current_block()->HasPredecessor()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5528 | switch (expr->op()) { |
| 5529 | case Token::DELETE: return VisitDelete(expr); |
| 5530 | case Token::VOID: return VisitVoid(expr); |
| 5531 | case Token::TYPEOF: return VisitTypeof(expr); |
| 5532 | case Token::ADD: return VisitAdd(expr); |
| 5533 | case Token::SUB: return VisitSub(expr); |
| 5534 | case Token::BIT_NOT: return VisitBitNot(expr); |
| 5535 | case Token::NOT: return VisitNot(expr); |
| 5536 | default: UNREACHABLE(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5537 | } |
| 5538 | } |
| 5539 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5540 | void HGraphBuilder::VisitDelete(UnaryOperation* expr) { |
| 5541 | Property* prop = expr->expression()->AsProperty(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5542 | VariableProxy* proxy = expr->expression()->AsVariableProxy(); |
| 5543 | if (prop != NULL) { |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 5544 | CHECK_ALIVE(VisitForValue(prop->obj())); |
| 5545 | CHECK_ALIVE(VisitForValue(prop->key())); |
| 5546 | HValue* key = Pop(); |
| 5547 | HValue* obj = Pop(); |
| 5548 | HValue* context = environment()->LookupContext(); |
| 5549 | HDeleteProperty* instr = new(zone()) HDeleteProperty(context, obj, key); |
| 5550 | return ast_context()->ReturnInstruction(instr, expr->id()); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5551 | } else if (proxy != NULL) { |
| 5552 | Variable* var = proxy->var(); |
| 5553 | if (var->IsUnallocated()) { |
| 5554 | Bailout("delete with global variable"); |
| 5555 | } else if (var->IsStackAllocated() || var->IsContextSlot()) { |
| 5556 | // Result of deleting non-global variables is false. 'this' is not |
| 5557 | // really a variable, though we implement it as one. The |
| 5558 | // subexpression does not have side effects. |
| 5559 | HValue* value = var->is_this() |
| 5560 | ? graph()->GetConstantTrue() |
| 5561 | : graph()->GetConstantFalse(); |
| 5562 | return ast_context()->ReturnValue(value); |
| 5563 | } else { |
| 5564 | Bailout("delete with non-global variable"); |
| 5565 | } |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5566 | } else { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5567 | // Result of deleting non-property, non-variable reference is true. |
| 5568 | // Evaluate the subexpression for side effects. |
| 5569 | CHECK_ALIVE(VisitForEffect(expr->expression())); |
| 5570 | return ast_context()->ReturnValue(graph()->GetConstantTrue()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5571 | } |
| 5572 | } |
| 5573 | |
| 5574 | |
| 5575 | void HGraphBuilder::VisitVoid(UnaryOperation* expr) { |
| 5576 | CHECK_ALIVE(VisitForEffect(expr->expression())); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5577 | return ast_context()->ReturnValue(graph()->GetConstantUndefined()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5578 | } |
| 5579 | |
| 5580 | |
| 5581 | void HGraphBuilder::VisitTypeof(UnaryOperation* expr) { |
| 5582 | CHECK_ALIVE(VisitForTypeOf(expr->expression())); |
| 5583 | HValue* value = Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5584 | HValue* context = environment()->LookupContext(); |
| 5585 | HInstruction* instr = new(zone()) HTypeof(context, value); |
| 5586 | return ast_context()->ReturnInstruction(instr, expr->id()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5587 | } |
| 5588 | |
| 5589 | |
| 5590 | void HGraphBuilder::VisitAdd(UnaryOperation* expr) { |
| 5591 | CHECK_ALIVE(VisitForValue(expr->expression())); |
| 5592 | HValue* value = Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5593 | HValue* context = environment()->LookupContext(); |
| 5594 | HInstruction* instr = |
| 5595 | new(zone()) HMul(context, value, graph_->GetConstant1()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5596 | return ast_context()->ReturnInstruction(instr, expr->id()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5597 | } |
| 5598 | |
| 5599 | |
| 5600 | void HGraphBuilder::VisitSub(UnaryOperation* expr) { |
| 5601 | CHECK_ALIVE(VisitForValue(expr->expression())); |
| 5602 | HValue* value = Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5603 | HValue* context = environment()->LookupContext(); |
| 5604 | HInstruction* instr = |
| 5605 | new(zone()) HMul(context, value, graph_->GetConstantMinus1()); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 5606 | TypeInfo info = oracle()->UnaryType(expr); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5607 | if (info.IsUninitialized()) { |
| 5608 | AddInstruction(new(zone()) HSoftDeoptimize); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5609 | current_block()->MarkAsDeoptimizing(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5610 | info = TypeInfo::Unknown(); |
| 5611 | } |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 5612 | Representation rep = ToRepresentation(info); |
| 5613 | TraceRepresentation(expr->op(), info, instr, rep); |
| 5614 | instr->AssumeRepresentation(rep); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5615 | return ast_context()->ReturnInstruction(instr, expr->id()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5616 | } |
| 5617 | |
| 5618 | |
| 5619 | void HGraphBuilder::VisitBitNot(UnaryOperation* expr) { |
| 5620 | CHECK_ALIVE(VisitForValue(expr->expression())); |
| 5621 | HValue* value = Pop(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5622 | TypeInfo info = oracle()->UnaryType(expr); |
| 5623 | if (info.IsUninitialized()) { |
| 5624 | AddInstruction(new(zone()) HSoftDeoptimize); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5625 | current_block()->MarkAsDeoptimizing(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5626 | } |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5627 | HInstruction* instr = new(zone()) HBitNot(value); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5628 | return ast_context()->ReturnInstruction(instr, expr->id()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5629 | } |
| 5630 | |
| 5631 | |
| 5632 | void HGraphBuilder::VisitNot(UnaryOperation* expr) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5633 | if (ast_context()->IsTest()) { |
| 5634 | TestContext* context = TestContext::cast(ast_context()); |
| 5635 | VisitForControl(expr->expression(), |
| 5636 | context->if_false(), |
| 5637 | context->if_true()); |
| 5638 | return; |
| 5639 | } |
| 5640 | |
| 5641 | if (ast_context()->IsEffect()) { |
| 5642 | VisitForEffect(expr->expression()); |
| 5643 | return; |
| 5644 | } |
| 5645 | |
| 5646 | ASSERT(ast_context()->IsValue()); |
| 5647 | HBasicBlock* materialize_false = graph()->CreateBasicBlock(); |
| 5648 | HBasicBlock* materialize_true = graph()->CreateBasicBlock(); |
| 5649 | CHECK_BAILOUT(VisitForControl(expr->expression(), |
| 5650 | materialize_false, |
| 5651 | materialize_true)); |
| 5652 | |
| 5653 | if (materialize_false->HasPredecessor()) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5654 | materialize_false->SetJoinId(expr->MaterializeFalseId()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5655 | set_current_block(materialize_false); |
| 5656 | Push(graph()->GetConstantFalse()); |
| 5657 | } else { |
| 5658 | materialize_false = NULL; |
| 5659 | } |
| 5660 | |
| 5661 | if (materialize_true->HasPredecessor()) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5662 | materialize_true->SetJoinId(expr->MaterializeTrueId()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5663 | set_current_block(materialize_true); |
| 5664 | Push(graph()->GetConstantTrue()); |
| 5665 | } else { |
| 5666 | materialize_true = NULL; |
| 5667 | } |
| 5668 | |
| 5669 | HBasicBlock* join = |
| 5670 | CreateJoin(materialize_false, materialize_true, expr->id()); |
| 5671 | set_current_block(join); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5672 | if (join != NULL) return ast_context()->ReturnValue(Pop()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5673 | } |
| 5674 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5675 | |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 5676 | HInstruction* HGraphBuilder::BuildIncrement(bool returns_original_input, |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 5677 | CountOperation* expr) { |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 5678 | // The input to the count operation is on top of the expression stack. |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 5679 | TypeInfo info = oracle()->IncrementType(expr); |
| 5680 | Representation rep = ToRepresentation(info); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 5681 | if (rep.IsTagged()) { |
| 5682 | rep = Representation::Integer32(); |
| 5683 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 5684 | |
| 5685 | if (returns_original_input) { |
| 5686 | // We need an explicit HValue representing ToNumber(input). The |
| 5687 | // actual HChange instruction we need is (sometimes) added in a later |
| 5688 | // phase, so it is not available now to be used as an input to HAdd and |
| 5689 | // as the return value. |
| 5690 | HInstruction* number_input = new(zone()) HForceRepresentation(Pop(), rep); |
| 5691 | AddInstruction(number_input); |
| 5692 | Push(number_input); |
| 5693 | } |
| 5694 | |
| 5695 | // The addition has no side effects, so we do not need |
| 5696 | // to simulate the expression stack after this instruction. |
| 5697 | // Any later failures deopt to the load of the input or earlier. |
| 5698 | HConstant* delta = (expr->op() == Token::INC) |
| 5699 | ? graph_->GetConstant1() |
| 5700 | : graph_->GetConstantMinus1(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5701 | HValue* context = environment()->LookupContext(); |
| 5702 | HInstruction* instr = new(zone()) HAdd(context, Top(), delta); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 5703 | TraceRepresentation(expr->op(), info, instr, rep); |
| 5704 | instr->AssumeRepresentation(rep); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 5705 | AddInstruction(instr); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5706 | return instr; |
| 5707 | } |
| 5708 | |
| 5709 | |
| 5710 | void HGraphBuilder::VisitCountOperation(CountOperation* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5711 | ASSERT(!HasStackOverflow()); |
| 5712 | ASSERT(current_block() != NULL); |
| 5713 | ASSERT(current_block()->HasPredecessor()); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 5714 | Expression* target = expr->expression(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5715 | VariableProxy* proxy = target->AsVariableProxy(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5716 | Property* prop = target->AsProperty(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5717 | if (proxy == NULL && prop == NULL) { |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 5718 | return Bailout("invalid lhs in count operation"); |
| 5719 | } |
| 5720 | |
| 5721 | // Match the full code generator stack by simulating an extra stack |
| 5722 | // element for postfix operations in a non-effect context. The return |
| 5723 | // value is ToNumber(input). |
| 5724 | bool returns_original_input = |
| 5725 | expr->is_postfix() && !ast_context()->IsEffect(); |
| 5726 | HValue* input = NULL; // ToNumber(original_input). |
| 5727 | HValue* after = NULL; // The result after incrementing or decrementing. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5728 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5729 | if (proxy != NULL) { |
| 5730 | Variable* var = proxy->var(); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 5731 | if (var->mode() == CONST) { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 5732 | return Bailout("unsupported count operation with const"); |
| 5733 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 5734 | // Argument of the count operation is a variable, not a property. |
| 5735 | ASSERT(prop == NULL); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5736 | CHECK_ALIVE(VisitForValue(target)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5737 | |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 5738 | after = BuildIncrement(returns_original_input, expr); |
| 5739 | input = returns_original_input ? Top() : Pop(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5740 | Push(after); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5741 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5742 | switch (var->location()) { |
| 5743 | case Variable::UNALLOCATED: |
| 5744 | HandleGlobalVariableAssignment(var, |
| 5745 | after, |
| 5746 | expr->position(), |
| 5747 | expr->AssignmentId()); |
| 5748 | break; |
| 5749 | |
| 5750 | case Variable::PARAMETER: |
| 5751 | case Variable::LOCAL: |
| 5752 | Bind(var, after); |
| 5753 | break; |
| 5754 | |
| 5755 | case Variable::CONTEXT: { |
| 5756 | // Bail out if we try to mutate a parameter value in a function |
| 5757 | // using the arguments object. We do not (yet) correctly handle the |
| 5758 | // arguments property of the function. |
| 5759 | if (info()->scope()->arguments() != NULL) { |
| 5760 | // Parameters will rewrite to context slots. We have no direct |
| 5761 | // way to detect that the variable is a parameter so we use a |
| 5762 | // linear search of the parameter list. |
| 5763 | int count = info()->scope()->num_parameters(); |
| 5764 | for (int i = 0; i < count; ++i) { |
| 5765 | if (var == info()->scope()->parameter(i)) { |
| 5766 | return Bailout("assignment to parameter in arguments object"); |
| 5767 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5768 | } |
| 5769 | } |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5770 | |
| 5771 | HValue* context = BuildContextChainWalk(var); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 5772 | HStoreContextSlot::Mode mode = |
| 5773 | (var->mode() == LET || var->mode() == CONST_HARMONY) |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 5774 | ? HStoreContextSlot::kCheckDeoptimize : HStoreContextSlot::kNoCheck; |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5775 | HStoreContextSlot* instr = |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 5776 | new(zone()) HStoreContextSlot(context, var->index(), mode, after); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5777 | AddInstruction(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5778 | if (instr->HasObservableSideEffects()) { |
| 5779 | AddSimulate(expr->AssignmentId()); |
| 5780 | } |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5781 | break; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5782 | } |
| 5783 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5784 | case Variable::LOOKUP: |
| 5785 | return Bailout("lookup variable in count operation"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5786 | } |
| 5787 | |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 5788 | } else { |
| 5789 | // Argument of the count operation is a property. |
| 5790 | ASSERT(prop != NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5791 | prop->RecordTypeFeedback(oracle()); |
| 5792 | |
| 5793 | if (prop->key()->IsPropertyName()) { |
| 5794 | // Named property. |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 5795 | if (returns_original_input) Push(graph_->GetConstantUndefined()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5796 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5797 | CHECK_ALIVE(VisitForValue(prop->obj())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5798 | HValue* obj = Top(); |
| 5799 | |
| 5800 | HInstruction* load = NULL; |
| 5801 | if (prop->IsMonomorphic()) { |
| 5802 | Handle<String> name = prop->key()->AsLiteral()->AsPropertyName(); |
| 5803 | Handle<Map> map = prop->GetReceiverTypes()->first(); |
| 5804 | load = BuildLoadNamed(obj, prop, map, name); |
| 5805 | } else { |
| 5806 | load = BuildLoadNamedGeneric(obj, prop); |
| 5807 | } |
| 5808 | PushAndAdd(load); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5809 | if (load->HasObservableSideEffects()) AddSimulate(expr->CountId()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5810 | |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 5811 | after = BuildIncrement(returns_original_input, expr); |
| 5812 | input = Pop(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5813 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5814 | HInstruction* store = BuildStoreNamed(obj, after, prop); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5815 | AddInstruction(store); |
| 5816 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5817 | // Overwrite the receiver in the bailout environment with the result |
| 5818 | // of the operation, and the placeholder with the original value if |
| 5819 | // necessary. |
| 5820 | environment()->SetExpressionStackAt(0, after); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 5821 | if (returns_original_input) environment()->SetExpressionStackAt(1, input); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5822 | if (store->HasObservableSideEffects()) AddSimulate(expr->AssignmentId()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5823 | |
| 5824 | } else { |
| 5825 | // Keyed property. |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 5826 | if (returns_original_input) Push(graph_->GetConstantUndefined()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5827 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5828 | CHECK_ALIVE(VisitForValue(prop->obj())); |
| 5829 | CHECK_ALIVE(VisitForValue(prop->key())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5830 | HValue* obj = environment()->ExpressionStackAt(1); |
| 5831 | HValue* key = environment()->ExpressionStackAt(0); |
| 5832 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5833 | bool has_side_effects = false; |
| 5834 | HValue* load = HandleKeyedElementAccess( |
| 5835 | obj, key, NULL, prop, expr->CountId(), RelocInfo::kNoPosition, |
| 5836 | false, // is_store |
| 5837 | &has_side_effects); |
| 5838 | Push(load); |
| 5839 | if (has_side_effects) AddSimulate(expr->CountId()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5840 | |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 5841 | after = BuildIncrement(returns_original_input, expr); |
| 5842 | input = Pop(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5843 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 5844 | expr->RecordTypeFeedback(oracle()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5845 | HandleKeyedElementAccess(obj, key, after, expr, expr->AssignmentId(), |
| 5846 | RelocInfo::kNoPosition, |
| 5847 | true, // is_store |
| 5848 | &has_side_effects); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5849 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5850 | // Drop the key from the bailout environment. Overwrite the receiver |
| 5851 | // with the result of the operation, and the placeholder with the |
| 5852 | // original value if necessary. |
| 5853 | Drop(1); |
| 5854 | environment()->SetExpressionStackAt(0, after); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 5855 | if (returns_original_input) environment()->SetExpressionStackAt(1, input); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5856 | ASSERT(has_side_effects); // Stores always have side effects. |
| 5857 | AddSimulate(expr->AssignmentId()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5858 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5859 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 5860 | |
| 5861 | Drop(returns_original_input ? 2 : 1); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5862 | return ast_context()->ReturnValue(expr->is_postfix() ? input : after); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5863 | } |
| 5864 | |
| 5865 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5866 | HStringCharCodeAt* HGraphBuilder::BuildStringCharCodeAt(HValue* context, |
| 5867 | HValue* string, |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5868 | HValue* index) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5869 | AddInstruction(new(zone()) HCheckNonSmi(string)); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5870 | AddInstruction(HCheckInstanceType::NewIsString(string)); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5871 | HStringLength* length = new(zone()) HStringLength(string); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5872 | AddInstruction(length); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5873 | HInstruction* checked_index = |
| 5874 | AddInstruction(new(zone()) HBoundsCheck(index, length)); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5875 | return new(zone()) HStringCharCodeAt(context, string, checked_index); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5876 | } |
| 5877 | |
| 5878 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5879 | HInstruction* HGraphBuilder::BuildBinaryOperation(BinaryOperation* expr, |
| 5880 | HValue* left, |
| 5881 | HValue* right) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5882 | HValue* context = environment()->LookupContext(); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5883 | TypeInfo info = oracle()->BinaryType(expr); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5884 | if (info.IsUninitialized()) { |
| 5885 | AddInstruction(new(zone()) HSoftDeoptimize); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5886 | current_block()->MarkAsDeoptimizing(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5887 | info = TypeInfo::Unknown(); |
| 5888 | } |
| 5889 | HInstruction* instr = NULL; |
| 5890 | switch (expr->op()) { |
| 5891 | case Token::ADD: |
| 5892 | if (info.IsString()) { |
| 5893 | AddInstruction(new(zone()) HCheckNonSmi(left)); |
| 5894 | AddInstruction(HCheckInstanceType::NewIsString(left)); |
| 5895 | AddInstruction(new(zone()) HCheckNonSmi(right)); |
| 5896 | AddInstruction(HCheckInstanceType::NewIsString(right)); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5897 | instr = new(zone()) HStringAdd(context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5898 | } else { |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 5899 | instr = HAdd::NewHAdd(zone(), context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5900 | } |
| 5901 | break; |
| 5902 | case Token::SUB: |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 5903 | instr = HSub::NewHSub(zone(), context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5904 | break; |
| 5905 | case Token::MUL: |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 5906 | instr = HMul::NewHMul(zone(), context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5907 | break; |
| 5908 | case Token::MOD: |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 5909 | instr = HMod::NewHMod(zone(), context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5910 | break; |
| 5911 | case Token::DIV: |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 5912 | instr = HDiv::NewHDiv(zone(), context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5913 | break; |
| 5914 | case Token::BIT_XOR: |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5915 | case Token::BIT_AND: |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5916 | case Token::BIT_OR: |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 5917 | instr = HBitwise::NewHBitwise(zone(), expr->op(), context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5918 | break; |
| 5919 | case Token::SAR: |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 5920 | instr = HSar::NewHSar(zone(), context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5921 | break; |
| 5922 | case Token::SHR: |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 5923 | instr = HShr::NewHShr(zone(), context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5924 | break; |
| 5925 | case Token::SHL: |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 5926 | instr = HShl::NewHShl(zone(), context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 5927 | break; |
| 5928 | default: |
| 5929 | UNREACHABLE(); |
| 5930 | } |
| 5931 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5932 | // If we hit an uninitialized binary op stub we will get type info |
| 5933 | // for a smi operation. If one of the operands is a constant string |
| 5934 | // do not generate code assuming it is a smi operation. |
| 5935 | if (info.IsSmi() && |
| 5936 | ((left->IsConstant() && HConstant::cast(left)->HasStringValue()) || |
| 5937 | (right->IsConstant() && HConstant::cast(right)->HasStringValue()))) { |
| 5938 | return instr; |
| 5939 | } |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 5940 | Representation rep = ToRepresentation(info); |
| 5941 | // We only generate either int32 or generic tagged bitwise operations. |
| 5942 | if (instr->IsBitwiseBinaryOperation() && rep.IsDouble()) { |
| 5943 | rep = Representation::Integer32(); |
| 5944 | } |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 5945 | TraceRepresentation(expr->op(), info, instr, rep); |
| 5946 | instr->AssumeRepresentation(rep); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5947 | return instr; |
| 5948 | } |
| 5949 | |
| 5950 | |
| 5951 | // Check for the form (%_ClassOf(foo) === 'BarClass'). |
| 5952 | static bool IsClassOfTest(CompareOperation* expr) { |
| 5953 | if (expr->op() != Token::EQ_STRICT) return false; |
| 5954 | CallRuntime* call = expr->left()->AsCallRuntime(); |
| 5955 | if (call == NULL) return false; |
| 5956 | Literal* literal = expr->right()->AsLiteral(); |
| 5957 | if (literal == NULL) return false; |
| 5958 | if (!literal->handle()->IsString()) return false; |
| 5959 | if (!call->name()->IsEqualTo(CStrVector("_ClassOf"))) return false; |
| 5960 | ASSERT(call->arguments()->length() == 1); |
| 5961 | return true; |
| 5962 | } |
| 5963 | |
| 5964 | |
| 5965 | void HGraphBuilder::VisitBinaryOperation(BinaryOperation* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5966 | ASSERT(!HasStackOverflow()); |
| 5967 | ASSERT(current_block() != NULL); |
| 5968 | ASSERT(current_block()->HasPredecessor()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5969 | switch (expr->op()) { |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 5970 | case Token::COMMA: |
| 5971 | return VisitComma(expr); |
| 5972 | case Token::OR: |
| 5973 | case Token::AND: |
| 5974 | return VisitLogicalExpression(expr); |
| 5975 | default: |
| 5976 | return VisitArithmeticExpression(expr); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5977 | } |
| 5978 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5979 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5980 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5981 | void HGraphBuilder::VisitComma(BinaryOperation* expr) { |
| 5982 | CHECK_ALIVE(VisitForEffect(expr->left())); |
| 5983 | // Visit the right subexpression in the same AST context as the entire |
| 5984 | // expression. |
| 5985 | Visit(expr->right()); |
| 5986 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5987 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5988 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 5989 | void HGraphBuilder::VisitLogicalExpression(BinaryOperation* expr) { |
| 5990 | bool is_logical_and = expr->op() == Token::AND; |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5991 | if (ast_context()->IsTest()) { |
| 5992 | TestContext* context = TestContext::cast(ast_context()); |
| 5993 | // Translate left subexpression. |
| 5994 | HBasicBlock* eval_right = graph()->CreateBasicBlock(); |
| 5995 | if (is_logical_and) { |
| 5996 | CHECK_BAILOUT(VisitForControl(expr->left(), |
| 5997 | eval_right, |
| 5998 | context->if_false())); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 5999 | } else { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6000 | CHECK_BAILOUT(VisitForControl(expr->left(), |
| 6001 | context->if_true(), |
| 6002 | eval_right)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6003 | } |
| 6004 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6005 | // Translate right subexpression by visiting it in the same AST |
| 6006 | // context as the entire expression. |
| 6007 | if (eval_right->HasPredecessor()) { |
| 6008 | eval_right->SetJoinId(expr->RightId()); |
| 6009 | set_current_block(eval_right); |
| 6010 | Visit(expr->right()); |
| 6011 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6012 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6013 | } else if (ast_context()->IsValue()) { |
| 6014 | CHECK_ALIVE(VisitForValue(expr->left())); |
| 6015 | ASSERT(current_block() != NULL); |
| 6016 | |
| 6017 | // We need an extra block to maintain edge-split form. |
| 6018 | HBasicBlock* empty_block = graph()->CreateBasicBlock(); |
| 6019 | HBasicBlock* eval_right = graph()->CreateBasicBlock(); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 6020 | unsigned test_id = expr->left()->test_id(); |
| 6021 | ToBooleanStub::Types expected(oracle()->ToBooleanTypes(test_id)); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6022 | HBranch* test = is_logical_and |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 6023 | ? new(zone()) HBranch(Top(), eval_right, empty_block, expected) |
| 6024 | : new(zone()) HBranch(Top(), empty_block, eval_right, expected); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6025 | current_block()->Finish(test); |
| 6026 | |
| 6027 | set_current_block(eval_right); |
| 6028 | Drop(1); // Value of the left subexpression. |
| 6029 | CHECK_BAILOUT(VisitForValue(expr->right())); |
| 6030 | |
| 6031 | HBasicBlock* join_block = |
| 6032 | CreateJoin(empty_block, current_block(), expr->id()); |
| 6033 | set_current_block(join_block); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6034 | return ast_context()->ReturnValue(Pop()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6035 | |
| 6036 | } else { |
| 6037 | ASSERT(ast_context()->IsEffect()); |
| 6038 | // In an effect context, we don't need the value of the left subexpression, |
| 6039 | // only its control flow and side effects. We need an extra block to |
| 6040 | // maintain edge-split form. |
| 6041 | HBasicBlock* empty_block = graph()->CreateBasicBlock(); |
| 6042 | HBasicBlock* right_block = graph()->CreateBasicBlock(); |
| 6043 | if (is_logical_and) { |
| 6044 | CHECK_BAILOUT(VisitForControl(expr->left(), right_block, empty_block)); |
| 6045 | } else { |
| 6046 | CHECK_BAILOUT(VisitForControl(expr->left(), empty_block, right_block)); |
| 6047 | } |
| 6048 | |
| 6049 | // TODO(kmillikin): Find a way to fix this. It's ugly that there are |
| 6050 | // actually two empty blocks (one here and one inserted by |
| 6051 | // TestContext::BuildBranch, and that they both have an HSimulate though the |
| 6052 | // second one is not a merge node, and that we really have no good AST ID to |
| 6053 | // put on that first HSimulate. |
| 6054 | |
| 6055 | if (empty_block->HasPredecessor()) { |
| 6056 | empty_block->SetJoinId(expr->id()); |
| 6057 | } else { |
| 6058 | empty_block = NULL; |
| 6059 | } |
| 6060 | |
| 6061 | if (right_block->HasPredecessor()) { |
| 6062 | right_block->SetJoinId(expr->RightId()); |
| 6063 | set_current_block(right_block); |
| 6064 | CHECK_BAILOUT(VisitForEffect(expr->right())); |
| 6065 | right_block = current_block(); |
| 6066 | } else { |
| 6067 | right_block = NULL; |
| 6068 | } |
| 6069 | |
| 6070 | HBasicBlock* join_block = |
| 6071 | CreateJoin(empty_block, right_block, expr->id()); |
| 6072 | set_current_block(join_block); |
| 6073 | // We did not materialize any value in the predecessor environments, |
| 6074 | // so there is no need to handle it here. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6075 | } |
| 6076 | } |
| 6077 | |
| 6078 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 6079 | void HGraphBuilder::VisitArithmeticExpression(BinaryOperation* expr) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6080 | CHECK_ALIVE(VisitForValue(expr->left())); |
| 6081 | CHECK_ALIVE(VisitForValue(expr->right())); |
| 6082 | HValue* right = Pop(); |
| 6083 | HValue* left = Pop(); |
| 6084 | HInstruction* instr = BuildBinaryOperation(expr, left, right); |
| 6085 | instr->set_position(expr->position()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6086 | return ast_context()->ReturnInstruction(instr, expr->id()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6087 | } |
| 6088 | |
| 6089 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 6090 | void HGraphBuilder::TraceRepresentation(Token::Value op, |
| 6091 | TypeInfo info, |
| 6092 | HValue* value, |
| 6093 | Representation rep) { |
| 6094 | if (!FLAG_trace_representation) return; |
| 6095 | // TODO(svenpanne) Under which circumstances are we actually not flexible? |
| 6096 | // At first glance, this looks a bit weird... |
| 6097 | bool flexible = value->CheckFlag(HValue::kFlexibleRepresentation); |
| 6098 | PrintF("Operation %s has type info %s, %schange representation assumption " |
| 6099 | "for %s (ID %d) from %s to %s\n", |
| 6100 | Token::Name(op), |
| 6101 | info.ToString(), |
| 6102 | flexible ? "" : " DO NOT ", |
| 6103 | value->Mnemonic(), |
| 6104 | graph_->GetMaximumValueID(), |
| 6105 | value->representation().Mnemonic(), |
| 6106 | rep.Mnemonic()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6107 | } |
| 6108 | |
| 6109 | |
| 6110 | Representation HGraphBuilder::ToRepresentation(TypeInfo info) { |
| 6111 | if (info.IsSmi()) return Representation::Integer32(); |
| 6112 | if (info.IsInteger32()) return Representation::Integer32(); |
| 6113 | if (info.IsDouble()) return Representation::Double(); |
| 6114 | if (info.IsNumber()) return Representation::Double(); |
| 6115 | return Representation::Tagged(); |
| 6116 | } |
| 6117 | |
| 6118 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6119 | void HGraphBuilder::HandleLiteralCompareTypeof(CompareOperation* expr, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6120 | HTypeof* typeof_expr, |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 6121 | Handle<String> check) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6122 | // Note: The HTypeof itself is removed during canonicalization, if possible. |
| 6123 | HValue* value = typeof_expr->value(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6124 | HTypeofIsAndBranch* instr = new(zone()) HTypeofIsAndBranch(value, check); |
| 6125 | instr->set_position(expr->position()); |
| 6126 | return ast_context()->ReturnControl(instr, expr->id()); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 6127 | } |
| 6128 | |
| 6129 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6130 | static bool MatchLiteralCompareNil(HValue* left, |
| 6131 | Token::Value op, |
| 6132 | HValue* right, |
| 6133 | Handle<Object> nil, |
| 6134 | HValue** expr) { |
| 6135 | if (left->IsConstant() && |
| 6136 | HConstant::cast(left)->handle().is_identical_to(nil) && |
| 6137 | Token::IsEqualityOp(op)) { |
| 6138 | *expr = right; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6139 | return true; |
| 6140 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6141 | return false; |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 6142 | } |
| 6143 | |
| 6144 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6145 | static bool MatchLiteralCompareTypeof(HValue* left, |
| 6146 | Token::Value op, |
| 6147 | HValue* right, |
| 6148 | HTypeof** typeof_expr, |
| 6149 | Handle<String>* check) { |
| 6150 | if (left->IsTypeof() && |
| 6151 | Token::IsEqualityOp(op) && |
| 6152 | right->IsConstant() && |
| 6153 | HConstant::cast(right)->HasStringValue()) { |
| 6154 | *typeof_expr = HTypeof::cast(left); |
| 6155 | *check = Handle<String>::cast(HConstant::cast(right)->handle()); |
| 6156 | return true; |
| 6157 | } |
| 6158 | return false; |
| 6159 | } |
| 6160 | |
| 6161 | |
| 6162 | static bool IsLiteralCompareTypeof(HValue* left, |
| 6163 | Token::Value op, |
| 6164 | HValue* right, |
| 6165 | HTypeof** typeof_expr, |
| 6166 | Handle<String>* check) { |
| 6167 | return MatchLiteralCompareTypeof(left, op, right, typeof_expr, check) || |
| 6168 | MatchLiteralCompareTypeof(right, op, left, typeof_expr, check); |
| 6169 | } |
| 6170 | |
| 6171 | |
| 6172 | static bool IsLiteralCompareNil(HValue* left, |
| 6173 | Token::Value op, |
| 6174 | HValue* right, |
| 6175 | Handle<Object> nil, |
| 6176 | HValue** expr) { |
| 6177 | return MatchLiteralCompareNil(left, op, right, nil, expr) || |
| 6178 | MatchLiteralCompareNil(right, op, left, nil, expr); |
| 6179 | } |
| 6180 | |
| 6181 | |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 6182 | static bool IsLiteralCompareBool(HValue* left, |
| 6183 | Token::Value op, |
| 6184 | HValue* right) { |
| 6185 | return op == Token::EQ_STRICT && |
| 6186 | ((left->IsConstant() && HConstant::cast(left)->handle()->IsBoolean()) || |
| 6187 | (right->IsConstant() && HConstant::cast(right)->handle()->IsBoolean())); |
| 6188 | } |
| 6189 | |
| 6190 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6191 | void HGraphBuilder::VisitCompareOperation(CompareOperation* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6192 | ASSERT(!HasStackOverflow()); |
| 6193 | ASSERT(current_block() != NULL); |
| 6194 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6195 | if (IsClassOfTest(expr)) { |
| 6196 | CallRuntime* call = expr->left()->AsCallRuntime(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6197 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6198 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6199 | HValue* value = Pop(); |
| 6200 | Literal* literal = expr->right()->AsLiteral(); |
| 6201 | Handle<String> rhs = Handle<String>::cast(literal->handle()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6202 | HClassOfTestAndBranch* instr = |
| 6203 | new(zone()) HClassOfTestAndBranch(value, rhs); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6204 | instr->set_position(expr->position()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6205 | return ast_context()->ReturnControl(instr, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6206 | } |
| 6207 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 6208 | TypeInfo type_info = oracle()->CompareType(expr); |
| 6209 | // Check if this expression was ever executed according to type feedback. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6210 | // Note that for the special typeof/null/undefined cases we get unknown here. |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 6211 | if (type_info.IsUninitialized()) { |
| 6212 | AddInstruction(new(zone()) HSoftDeoptimize); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6213 | current_block()->MarkAsDeoptimizing(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 6214 | type_info = TypeInfo::Unknown(); |
| 6215 | } |
| 6216 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6217 | CHECK_ALIVE(VisitForValue(expr->left())); |
| 6218 | CHECK_ALIVE(VisitForValue(expr->right())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6219 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6220 | HValue* context = environment()->LookupContext(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6221 | HValue* right = Pop(); |
| 6222 | HValue* left = Pop(); |
| 6223 | Token::Value op = expr->op(); |
| 6224 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6225 | HTypeof* typeof_expr = NULL; |
| 6226 | Handle<String> check; |
| 6227 | if (IsLiteralCompareTypeof(left, op, right, &typeof_expr, &check)) { |
| 6228 | return HandleLiteralCompareTypeof(expr, typeof_expr, check); |
| 6229 | } |
| 6230 | HValue* sub_expr = NULL; |
| 6231 | Factory* f = graph()->isolate()->factory(); |
| 6232 | if (IsLiteralCompareNil(left, op, right, f->undefined_value(), &sub_expr)) { |
| 6233 | return HandleLiteralCompareNil(expr, sub_expr, kUndefinedValue); |
| 6234 | } |
| 6235 | if (IsLiteralCompareNil(left, op, right, f->null_value(), &sub_expr)) { |
| 6236 | return HandleLiteralCompareNil(expr, sub_expr, kNullValue); |
| 6237 | } |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 6238 | if (IsLiteralCompareBool(left, op, right)) { |
| 6239 | HCompareObjectEqAndBranch* result = |
| 6240 | new(zone()) HCompareObjectEqAndBranch(left, right); |
| 6241 | result->set_position(expr->position()); |
| 6242 | return ast_context()->ReturnControl(result, expr->id()); |
| 6243 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6244 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6245 | if (op == Token::INSTANCEOF) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 6246 | // Check to see if the rhs of the instanceof is a global function not |
| 6247 | // residing in new space. If it is we assume that the function will stay the |
| 6248 | // same. |
| 6249 | Handle<JSFunction> target = Handle<JSFunction>::null(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 6250 | VariableProxy* proxy = expr->right()->AsVariableProxy(); |
| 6251 | bool global_function = (proxy != NULL) && proxy->var()->IsUnallocated(); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 6252 | if (global_function && |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6253 | info()->has_global_object() && |
| 6254 | !info()->global_object()->IsAccessCheckNeeded()) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 6255 | Handle<String> name = proxy->name(); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6256 | Handle<GlobalObject> global(info()->global_object()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6257 | LookupResult lookup(isolate()); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 6258 | global->Lookup(*name, &lookup); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 6259 | if (lookup.IsFound() && |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 6260 | lookup.type() == NORMAL && |
| 6261 | lookup.GetValue()->IsJSFunction()) { |
| 6262 | Handle<JSFunction> candidate(JSFunction::cast(lookup.GetValue())); |
| 6263 | // If the function is in new space we assume it's more likely to |
| 6264 | // change and thus prefer the general IC code. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6265 | if (!isolate()->heap()->InNewSpace(*candidate)) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 6266 | target = candidate; |
| 6267 | } |
| 6268 | } |
| 6269 | } |
| 6270 | |
| 6271 | // If the target is not null we have found a known global function that is |
| 6272 | // assumed to stay the same for this instanceof. |
| 6273 | if (target.is_null()) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6274 | HInstanceOf* result = new(zone()) HInstanceOf(context, left, right); |
| 6275 | result->set_position(expr->position()); |
| 6276 | return ast_context()->ReturnInstruction(result, expr->id()); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 6277 | } else { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6278 | AddInstruction(new(zone()) HCheckFunction(right, target)); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6279 | HInstanceOfKnownGlobal* result = |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6280 | new(zone()) HInstanceOfKnownGlobal(context, left, target); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6281 | result->set_position(expr->position()); |
| 6282 | return ast_context()->ReturnInstruction(result, expr->id()); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 6283 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6284 | } else if (op == Token::IN) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6285 | HIn* result = new(zone()) HIn(context, left, right); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6286 | result->set_position(expr->position()); |
| 6287 | return ast_context()->ReturnInstruction(result, expr->id()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6288 | } else if (type_info.IsNonPrimitive()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6289 | switch (op) { |
| 6290 | case Token::EQ: |
| 6291 | case Token::EQ_STRICT: { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 6292 | // Can we get away with map check and not instance type check? |
| 6293 | Handle<Map> map = oracle()->GetCompareMap(expr); |
| 6294 | if (!map.is_null()) { |
| 6295 | AddInstruction(new(zone()) HCheckNonSmi(left)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6296 | AddInstruction(new(zone()) HCheckMap(left, map, NULL, |
| 6297 | ALLOW_ELEMENT_TRANSITION_MAPS)); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 6298 | AddInstruction(new(zone()) HCheckNonSmi(right)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6299 | AddInstruction(new(zone()) HCheckMap(right, map, NULL, |
| 6300 | ALLOW_ELEMENT_TRANSITION_MAPS)); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 6301 | HCompareObjectEqAndBranch* result = |
| 6302 | new(zone()) HCompareObjectEqAndBranch(left, right); |
| 6303 | result->set_position(expr->position()); |
| 6304 | return ast_context()->ReturnControl(result, expr->id()); |
| 6305 | } else { |
| 6306 | AddInstruction(new(zone()) HCheckNonSmi(left)); |
| 6307 | AddInstruction(HCheckInstanceType::NewIsSpecObject(left)); |
| 6308 | AddInstruction(new(zone()) HCheckNonSmi(right)); |
| 6309 | AddInstruction(HCheckInstanceType::NewIsSpecObject(right)); |
| 6310 | HCompareObjectEqAndBranch* result = |
| 6311 | new(zone()) HCompareObjectEqAndBranch(left, right); |
| 6312 | result->set_position(expr->position()); |
| 6313 | return ast_context()->ReturnControl(result, expr->id()); |
| 6314 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6315 | } |
| 6316 | default: |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6317 | return Bailout("Unsupported non-primitive compare"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6318 | } |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 6319 | } else if (type_info.IsString() && oracle()->IsSymbolCompare(expr) && |
| 6320 | (op == Token::EQ || op == Token::EQ_STRICT)) { |
lrn@chromium.org | ac2828d | 2011-06-23 06:29:21 +0000 | [diff] [blame] | 6321 | AddInstruction(new(zone()) HCheckNonSmi(left)); |
| 6322 | AddInstruction(HCheckInstanceType::NewIsSymbol(left)); |
| 6323 | AddInstruction(new(zone()) HCheckNonSmi(right)); |
| 6324 | AddInstruction(HCheckInstanceType::NewIsSymbol(right)); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6325 | HCompareObjectEqAndBranch* result = |
| 6326 | new(zone()) HCompareObjectEqAndBranch(left, right); |
| 6327 | result->set_position(expr->position()); |
| 6328 | return ast_context()->ReturnControl(result, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6329 | } else { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6330 | Representation r = ToRepresentation(type_info); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6331 | if (r.IsTagged()) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6332 | HCompareGeneric* result = |
| 6333 | new(zone()) HCompareGeneric(context, left, right, op); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6334 | result->set_position(expr->position()); |
| 6335 | return ast_context()->ReturnInstruction(result, expr->id()); |
| 6336 | } else { |
| 6337 | HCompareIDAndBranch* result = |
| 6338 | new(zone()) HCompareIDAndBranch(left, right, op); |
| 6339 | result->set_position(expr->position()); |
| 6340 | result->SetInputRepresentation(r); |
| 6341 | return ast_context()->ReturnControl(result, expr->id()); |
| 6342 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6343 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6344 | } |
| 6345 | |
| 6346 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6347 | void HGraphBuilder::HandleLiteralCompareNil(CompareOperation* expr, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6348 | HValue* value, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6349 | NilValue nil) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6350 | ASSERT(!HasStackOverflow()); |
| 6351 | ASSERT(current_block() != NULL); |
| 6352 | ASSERT(current_block()->HasPredecessor()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6353 | EqualityKind kind = |
| 6354 | expr->op() == Token::EQ_STRICT ? kStrictEquality : kNonStrictEquality; |
| 6355 | HIsNilAndBranch* instr = new(zone()) HIsNilAndBranch(value, kind, nil); |
| 6356 | instr->set_position(expr->position()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6357 | return ast_context()->ReturnControl(instr, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6358 | } |
| 6359 | |
| 6360 | |
| 6361 | void HGraphBuilder::VisitThisFunction(ThisFunction* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6362 | ASSERT(!HasStackOverflow()); |
| 6363 | ASSERT(current_block() != NULL); |
| 6364 | ASSERT(current_block()->HasPredecessor()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6365 | HThisFunction* self = new(zone()) HThisFunction( |
| 6366 | function_state()->compilation_info()->closure()); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 6367 | return ast_context()->ReturnInstruction(self, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6368 | } |
| 6369 | |
| 6370 | |
| 6371 | void HGraphBuilder::VisitDeclaration(Declaration* decl) { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 6372 | HandleDeclaration(decl->proxy(), decl->mode(), decl->fun()); |
| 6373 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 6374 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 6375 | |
| 6376 | void HGraphBuilder::HandleDeclaration(VariableProxy* proxy, |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 6377 | VariableMode mode, |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 6378 | FunctionLiteral* function) { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 6379 | Variable* var = proxy->var(); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 6380 | bool binding_needs_init = |
| 6381 | (mode == CONST || mode == CONST_HARMONY || mode == LET); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 6382 | switch (var->location()) { |
| 6383 | case Variable::UNALLOCATED: |
| 6384 | return Bailout("unsupported global declaration"); |
| 6385 | case Variable::PARAMETER: |
| 6386 | case Variable::LOCAL: |
| 6387 | case Variable::CONTEXT: |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 6388 | if (binding_needs_init || function != NULL) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 6389 | HValue* value = NULL; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 6390 | if (function != NULL) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 6391 | VisitForValue(function); |
| 6392 | value = Pop(); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 6393 | } else { |
| 6394 | value = graph()->GetConstantHole(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 6395 | } |
| 6396 | if (var->IsContextSlot()) { |
| 6397 | HValue* context = environment()->LookupContext(); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 6398 | HStoreContextSlot* store = new HStoreContextSlot( |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 6399 | context, var->index(), HStoreContextSlot::kNoCheck, value); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 6400 | AddInstruction(store); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 6401 | if (store->HasObservableSideEffects()) AddSimulate(proxy->id()); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 6402 | } else { |
| 6403 | environment()->Bind(var, value); |
| 6404 | } |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 6405 | } |
| 6406 | break; |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 6407 | case Variable::LOOKUP: |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 6408 | return Bailout("unsupported lookup slot in declaration"); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 6409 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6410 | } |
| 6411 | |
| 6412 | |
| 6413 | // Generators for inline runtime functions. |
| 6414 | // Support for types. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6415 | void HGraphBuilder::GenerateIsSmi(CallRuntime* call) { |
| 6416 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6417 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6418 | HValue* value = Pop(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6419 | HIsSmiAndBranch* result = new(zone()) HIsSmiAndBranch(value); |
| 6420 | return ast_context()->ReturnControl(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6421 | } |
| 6422 | |
| 6423 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6424 | void HGraphBuilder::GenerateIsSpecObject(CallRuntime* call) { |
| 6425 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6426 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6427 | HValue* value = Pop(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6428 | HHasInstanceTypeAndBranch* result = |
| 6429 | new(zone()) HHasInstanceTypeAndBranch(value, |
| 6430 | FIRST_SPEC_OBJECT_TYPE, |
| 6431 | LAST_SPEC_OBJECT_TYPE); |
| 6432 | return ast_context()->ReturnControl(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6433 | } |
| 6434 | |
| 6435 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6436 | void HGraphBuilder::GenerateIsFunction(CallRuntime* call) { |
| 6437 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6438 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6439 | HValue* value = Pop(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6440 | HHasInstanceTypeAndBranch* result = |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6441 | new(zone()) HHasInstanceTypeAndBranch(value, JS_FUNCTION_TYPE); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6442 | return ast_context()->ReturnControl(result, call->id()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6443 | } |
| 6444 | |
| 6445 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6446 | void HGraphBuilder::GenerateHasCachedArrayIndex(CallRuntime* call) { |
| 6447 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6448 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6449 | HValue* value = Pop(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6450 | HHasCachedArrayIndexAndBranch* result = |
| 6451 | new(zone()) HHasCachedArrayIndexAndBranch(value); |
| 6452 | return ast_context()->ReturnControl(result, call->id()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6453 | } |
| 6454 | |
| 6455 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6456 | void HGraphBuilder::GenerateIsArray(CallRuntime* call) { |
| 6457 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6458 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6459 | HValue* value = Pop(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6460 | HHasInstanceTypeAndBranch* result = |
| 6461 | new(zone()) HHasInstanceTypeAndBranch(value, JS_ARRAY_TYPE); |
| 6462 | return ast_context()->ReturnControl(result, call->id()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6463 | } |
| 6464 | |
| 6465 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6466 | void HGraphBuilder::GenerateIsRegExp(CallRuntime* call) { |
| 6467 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6468 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6469 | HValue* value = Pop(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6470 | HHasInstanceTypeAndBranch* result = |
| 6471 | new(zone()) HHasInstanceTypeAndBranch(value, JS_REGEXP_TYPE); |
| 6472 | return ast_context()->ReturnControl(result, call->id()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6473 | } |
| 6474 | |
| 6475 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6476 | void HGraphBuilder::GenerateIsObject(CallRuntime* call) { |
| 6477 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6478 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6479 | HValue* value = Pop(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6480 | HIsObjectAndBranch* result = new(zone()) HIsObjectAndBranch(value); |
| 6481 | return ast_context()->ReturnControl(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6482 | } |
| 6483 | |
| 6484 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6485 | void HGraphBuilder::GenerateIsNonNegativeSmi(CallRuntime* call) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6486 | return Bailout("inlined runtime function: IsNonNegativeSmi"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6487 | } |
| 6488 | |
| 6489 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6490 | void HGraphBuilder::GenerateIsUndetectableObject(CallRuntime* call) { |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 6491 | ASSERT(call->arguments()->length() == 1); |
| 6492 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
| 6493 | HValue* value = Pop(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6494 | HIsUndetectableAndBranch* result = |
| 6495 | new(zone()) HIsUndetectableAndBranch(value); |
| 6496 | return ast_context()->ReturnControl(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6497 | } |
| 6498 | |
| 6499 | |
| 6500 | void HGraphBuilder::GenerateIsStringWrapperSafeForDefaultValueOf( |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6501 | CallRuntime* call) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6502 | return Bailout( |
| 6503 | "inlined runtime function: IsStringWrapperSafeForDefaultValueOf"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6504 | } |
| 6505 | |
| 6506 | |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 6507 | // Support for construct call checks. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6508 | void HGraphBuilder::GenerateIsConstructCall(CallRuntime* call) { |
| 6509 | ASSERT(call->arguments()->length() == 0); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6510 | if (function_state()->outer() != NULL) { |
| 6511 | // We are generating graph for inlined function. Currently |
| 6512 | // constructor inlining is not supported and we can just return |
| 6513 | // false from %_IsConstructCall(). |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6514 | return ast_context()->ReturnValue(graph()->GetConstantFalse()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6515 | } else { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6516 | return ast_context()->ReturnControl(new(zone()) HIsConstructCallAndBranch, |
| 6517 | call->id()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6518 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6519 | } |
| 6520 | |
| 6521 | |
| 6522 | // Support for arguments.length and arguments[?]. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6523 | void HGraphBuilder::GenerateArgumentsLength(CallRuntime* call) { |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 6524 | // Our implementation of arguments (based on this stack frame or an |
| 6525 | // adapter below it) does not work for inlined functions. This runtime |
| 6526 | // function is blacklisted by AstNode::IsInlineable. |
| 6527 | ASSERT(function_state()->outer() == NULL); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6528 | ASSERT(call->arguments()->length() == 0); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6529 | HInstruction* elements = AddInstruction(new(zone()) HArgumentsElements); |
| 6530 | HArgumentsLength* result = new(zone()) HArgumentsLength(elements); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6531 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6532 | } |
| 6533 | |
| 6534 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6535 | void HGraphBuilder::GenerateArguments(CallRuntime* call) { |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 6536 | // Our implementation of arguments (based on this stack frame or an |
| 6537 | // adapter below it) does not work for inlined functions. This runtime |
| 6538 | // function is blacklisted by AstNode::IsInlineable. |
| 6539 | ASSERT(function_state()->outer() == NULL); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6540 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6541 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6542 | HValue* index = Pop(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6543 | HInstruction* elements = AddInstruction(new(zone()) HArgumentsElements); |
| 6544 | HInstruction* length = AddInstruction(new(zone()) HArgumentsLength(elements)); |
| 6545 | HAccessArgumentsAt* result = |
| 6546 | new(zone()) HAccessArgumentsAt(elements, length, index); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6547 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6548 | } |
| 6549 | |
| 6550 | |
| 6551 | // Support for accessing the class and value fields of an object. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6552 | void HGraphBuilder::GenerateClassOf(CallRuntime* call) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6553 | // The special form detected by IsClassOfTest is detected before we get here |
| 6554 | // and does not cause a bailout. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6555 | return Bailout("inlined runtime function: ClassOf"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6556 | } |
| 6557 | |
| 6558 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6559 | void HGraphBuilder::GenerateValueOf(CallRuntime* call) { |
| 6560 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6561 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6562 | HValue* value = Pop(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6563 | HValueOf* result = new(zone()) HValueOf(value); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6564 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6565 | } |
| 6566 | |
| 6567 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6568 | void HGraphBuilder::GenerateSetValueOf(CallRuntime* call) { |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 6569 | ASSERT(call->arguments()->length() == 2); |
| 6570 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
| 6571 | CHECK_ALIVE(VisitForValue(call->arguments()->at(1))); |
| 6572 | HValue* value = Pop(); |
| 6573 | HValue* object = Pop(); |
| 6574 | // Check if object is a not a smi. |
| 6575 | HIsSmiAndBranch* smicheck = new(zone()) HIsSmiAndBranch(object); |
| 6576 | HBasicBlock* if_smi = graph()->CreateBasicBlock(); |
| 6577 | HBasicBlock* if_heap_object = graph()->CreateBasicBlock(); |
| 6578 | HBasicBlock* join = graph()->CreateBasicBlock(); |
| 6579 | smicheck->SetSuccessorAt(0, if_smi); |
| 6580 | smicheck->SetSuccessorAt(1, if_heap_object); |
| 6581 | current_block()->Finish(smicheck); |
| 6582 | if_smi->Goto(join); |
| 6583 | |
| 6584 | // Check if object is a JSValue. |
| 6585 | set_current_block(if_heap_object); |
| 6586 | HHasInstanceTypeAndBranch* typecheck = |
| 6587 | new(zone()) HHasInstanceTypeAndBranch(object, JS_VALUE_TYPE); |
| 6588 | HBasicBlock* if_js_value = graph()->CreateBasicBlock(); |
| 6589 | HBasicBlock* not_js_value = graph()->CreateBasicBlock(); |
| 6590 | typecheck->SetSuccessorAt(0, if_js_value); |
| 6591 | typecheck->SetSuccessorAt(1, not_js_value); |
| 6592 | current_block()->Finish(typecheck); |
| 6593 | not_js_value->Goto(join); |
| 6594 | |
| 6595 | // Create in-object property store to kValueOffset. |
| 6596 | set_current_block(if_js_value); |
| 6597 | Handle<String> name = isolate()->factory()->undefined_symbol(); |
| 6598 | AddInstruction(new HStoreNamedField(object, |
| 6599 | name, |
| 6600 | value, |
| 6601 | true, // in-object store. |
| 6602 | JSValue::kValueOffset)); |
| 6603 | if_js_value->Goto(join); |
| 6604 | join->SetJoinId(call->id()); |
| 6605 | set_current_block(join); |
| 6606 | return ast_context()->ReturnValue(value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6607 | } |
| 6608 | |
| 6609 | |
| 6610 | // Fast support for charCodeAt(n). |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6611 | void HGraphBuilder::GenerateStringCharCodeAt(CallRuntime* call) { |
| 6612 | ASSERT(call->arguments()->length() == 2); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6613 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
| 6614 | CHECK_ALIVE(VisitForValue(call->arguments()->at(1))); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 6615 | HValue* index = Pop(); |
| 6616 | HValue* string = Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6617 | HValue* context = environment()->LookupContext(); |
| 6618 | HStringCharCodeAt* result = BuildStringCharCodeAt(context, string, index); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6619 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6620 | } |
| 6621 | |
| 6622 | |
| 6623 | // Fast support for string.charAt(n) and string[n]. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6624 | void HGraphBuilder::GenerateStringCharFromCode(CallRuntime* call) { |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 6625 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6626 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 6627 | HValue* char_code = Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6628 | HValue* context = environment()->LookupContext(); |
| 6629 | HStringCharFromCode* result = |
| 6630 | new(zone()) HStringCharFromCode(context, char_code); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6631 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6632 | } |
| 6633 | |
| 6634 | |
| 6635 | // Fast support for string.charAt(n) and string[n]. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6636 | void HGraphBuilder::GenerateStringCharAt(CallRuntime* call) { |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 6637 | ASSERT(call->arguments()->length() == 2); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6638 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
| 6639 | CHECK_ALIVE(VisitForValue(call->arguments()->at(1))); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 6640 | HValue* index = Pop(); |
| 6641 | HValue* string = Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6642 | HValue* context = environment()->LookupContext(); |
| 6643 | HStringCharCodeAt* char_code = BuildStringCharCodeAt(context, string, index); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 6644 | AddInstruction(char_code); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6645 | HStringCharFromCode* result = |
| 6646 | new(zone()) HStringCharFromCode(context, char_code); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6647 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6648 | } |
| 6649 | |
| 6650 | |
| 6651 | // Fast support for object equality testing. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6652 | void HGraphBuilder::GenerateObjectEquals(CallRuntime* call) { |
| 6653 | ASSERT(call->arguments()->length() == 2); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6654 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
| 6655 | CHECK_ALIVE(VisitForValue(call->arguments()->at(1))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6656 | HValue* right = Pop(); |
| 6657 | HValue* left = Pop(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6658 | HCompareObjectEqAndBranch* result = |
| 6659 | new(zone()) HCompareObjectEqAndBranch(left, right); |
| 6660 | return ast_context()->ReturnControl(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6661 | } |
| 6662 | |
| 6663 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6664 | void HGraphBuilder::GenerateLog(CallRuntime* call) { |
| 6665 | // %_Log is ignored in optimized code. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6666 | return ast_context()->ReturnValue(graph()->GetConstantUndefined()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6667 | } |
| 6668 | |
| 6669 | |
| 6670 | // Fast support for Math.random(). |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6671 | void HGraphBuilder::GenerateRandomHeapNumber(CallRuntime* call) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6672 | HValue* context = environment()->LookupContext(); |
| 6673 | HGlobalObject* global_object = new(zone()) HGlobalObject(context); |
| 6674 | AddInstruction(global_object); |
| 6675 | HRandom* result = new(zone()) HRandom(global_object); |
| 6676 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6677 | } |
| 6678 | |
| 6679 | |
| 6680 | // Fast support for StringAdd. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6681 | void HGraphBuilder::GenerateStringAdd(CallRuntime* call) { |
| 6682 | ASSERT_EQ(2, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6683 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6684 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6685 | HCallStub* result = new(zone()) HCallStub(context, CodeStub::StringAdd, 2); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6686 | Drop(2); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6687 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6688 | } |
| 6689 | |
| 6690 | |
| 6691 | // Fast support for SubString. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6692 | void HGraphBuilder::GenerateSubString(CallRuntime* call) { |
| 6693 | ASSERT_EQ(3, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6694 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6695 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6696 | HCallStub* result = new(zone()) HCallStub(context, CodeStub::SubString, 3); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6697 | Drop(3); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6698 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6699 | } |
| 6700 | |
| 6701 | |
| 6702 | // Fast support for StringCompare. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6703 | void HGraphBuilder::GenerateStringCompare(CallRuntime* call) { |
| 6704 | ASSERT_EQ(2, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6705 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6706 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6707 | HCallStub* result = |
| 6708 | new(zone()) HCallStub(context, CodeStub::StringCompare, 2); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6709 | Drop(2); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6710 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6711 | } |
| 6712 | |
| 6713 | |
| 6714 | // Support for direct calls from JavaScript to native RegExp code. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6715 | void HGraphBuilder::GenerateRegExpExec(CallRuntime* call) { |
| 6716 | ASSERT_EQ(4, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6717 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6718 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6719 | HCallStub* result = new(zone()) HCallStub(context, CodeStub::RegExpExec, 4); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6720 | Drop(4); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6721 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6722 | } |
| 6723 | |
| 6724 | |
| 6725 | // Construct a RegExp exec result with two in-object properties. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6726 | void HGraphBuilder::GenerateRegExpConstructResult(CallRuntime* call) { |
| 6727 | ASSERT_EQ(3, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6728 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6729 | HValue* context = environment()->LookupContext(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6730 | HCallStub* result = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6731 | new(zone()) HCallStub(context, CodeStub::RegExpConstructResult, 3); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6732 | Drop(3); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6733 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6734 | } |
| 6735 | |
| 6736 | |
| 6737 | // Support for fast native caches. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6738 | void HGraphBuilder::GenerateGetFromCache(CallRuntime* call) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6739 | return Bailout("inlined runtime function: GetFromCache"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6740 | } |
| 6741 | |
| 6742 | |
| 6743 | // Fast support for number to string. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6744 | void HGraphBuilder::GenerateNumberToString(CallRuntime* call) { |
| 6745 | ASSERT_EQ(1, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6746 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6747 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6748 | HCallStub* result = |
| 6749 | new(zone()) HCallStub(context, CodeStub::NumberToString, 1); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6750 | Drop(1); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6751 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6752 | } |
| 6753 | |
| 6754 | |
| 6755 | // Fast swapping of elements. Takes three expressions, the object and two |
| 6756 | // indices. This should only be used if the indices are known to be |
| 6757 | // non-negative and within bounds of the elements array at the call site. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6758 | void HGraphBuilder::GenerateSwapElements(CallRuntime* call) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6759 | return Bailout("inlined runtime function: SwapElements"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6760 | } |
| 6761 | |
| 6762 | |
| 6763 | // Fast call for custom callbacks. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6764 | void HGraphBuilder::GenerateCallFunction(CallRuntime* call) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6765 | // 1 ~ The function to call is not itself an argument to the call. |
| 6766 | int arg_count = call->arguments()->length() - 1; |
| 6767 | ASSERT(arg_count >= 1); // There's always at least a receiver. |
| 6768 | |
| 6769 | for (int i = 0; i < arg_count; ++i) { |
| 6770 | CHECK_ALIVE(VisitArgument(call->arguments()->at(i))); |
| 6771 | } |
| 6772 | CHECK_ALIVE(VisitForValue(call->arguments()->last())); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 6773 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6774 | HValue* function = Pop(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6775 | HValue* context = environment()->LookupContext(); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 6776 | |
| 6777 | // Branch for function proxies, or other non-functions. |
| 6778 | HHasInstanceTypeAndBranch* typecheck = |
| 6779 | new(zone()) HHasInstanceTypeAndBranch(function, JS_FUNCTION_TYPE); |
| 6780 | HBasicBlock* if_jsfunction = graph()->CreateBasicBlock(); |
| 6781 | HBasicBlock* if_nonfunction = graph()->CreateBasicBlock(); |
| 6782 | HBasicBlock* join = graph()->CreateBasicBlock(); |
| 6783 | typecheck->SetSuccessorAt(0, if_jsfunction); |
| 6784 | typecheck->SetSuccessorAt(1, if_nonfunction); |
| 6785 | current_block()->Finish(typecheck); |
| 6786 | |
| 6787 | set_current_block(if_jsfunction); |
| 6788 | HInstruction* invoke_result = AddInstruction( |
| 6789 | new(zone()) HInvokeFunction(context, function, arg_count)); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6790 | Drop(arg_count); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 6791 | Push(invoke_result); |
| 6792 | if_jsfunction->Goto(join); |
| 6793 | |
| 6794 | set_current_block(if_nonfunction); |
| 6795 | HInstruction* call_result = AddInstruction( |
| 6796 | new(zone()) HCallFunction(context, function, arg_count)); |
| 6797 | Drop(arg_count); |
| 6798 | Push(call_result); |
| 6799 | if_nonfunction->Goto(join); |
| 6800 | |
| 6801 | set_current_block(join); |
| 6802 | join->SetJoinId(call->id()); |
| 6803 | return ast_context()->ReturnValue(Pop()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6804 | } |
| 6805 | |
| 6806 | |
| 6807 | // Fast call to math functions. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6808 | void HGraphBuilder::GenerateMathPow(CallRuntime* call) { |
| 6809 | ASSERT_EQ(2, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6810 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
| 6811 | CHECK_ALIVE(VisitForValue(call->arguments()->at(1))); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6812 | HValue* right = Pop(); |
| 6813 | HValue* left = Pop(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6814 | HPower* result = new(zone()) HPower(left, right); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6815 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6816 | } |
| 6817 | |
| 6818 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6819 | void HGraphBuilder::GenerateMathSin(CallRuntime* call) { |
| 6820 | ASSERT_EQ(1, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6821 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6822 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6823 | HCallStub* result = |
| 6824 | new(zone()) HCallStub(context, CodeStub::TranscendentalCache, 1); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6825 | result->set_transcendental_type(TranscendentalCache::SIN); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6826 | Drop(1); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6827 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6828 | } |
| 6829 | |
| 6830 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6831 | void HGraphBuilder::GenerateMathCos(CallRuntime* call) { |
| 6832 | ASSERT_EQ(1, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6833 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6834 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6835 | HCallStub* result = |
| 6836 | new(zone()) HCallStub(context, CodeStub::TranscendentalCache, 1); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6837 | result->set_transcendental_type(TranscendentalCache::COS); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6838 | Drop(1); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6839 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6840 | } |
| 6841 | |
| 6842 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 6843 | void HGraphBuilder::GenerateMathTan(CallRuntime* call) { |
| 6844 | ASSERT_EQ(1, call->arguments()->length()); |
| 6845 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
| 6846 | HValue* context = environment()->LookupContext(); |
| 6847 | HCallStub* result = |
| 6848 | new(zone()) HCallStub(context, CodeStub::TranscendentalCache, 1); |
| 6849 | result->set_transcendental_type(TranscendentalCache::TAN); |
| 6850 | Drop(1); |
| 6851 | return ast_context()->ReturnInstruction(result, call->id()); |
| 6852 | } |
| 6853 | |
| 6854 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6855 | void HGraphBuilder::GenerateMathLog(CallRuntime* call) { |
| 6856 | ASSERT_EQ(1, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6857 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6858 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6859 | HCallStub* result = |
| 6860 | new(zone()) HCallStub(context, CodeStub::TranscendentalCache, 1); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6861 | result->set_transcendental_type(TranscendentalCache::LOG); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6862 | Drop(1); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6863 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6864 | } |
| 6865 | |
| 6866 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6867 | void HGraphBuilder::GenerateMathSqrt(CallRuntime* call) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6868 | return Bailout("inlined runtime function: MathSqrt"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6869 | } |
| 6870 | |
| 6871 | |
| 6872 | // Check whether two RegExps are equivalent |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6873 | void HGraphBuilder::GenerateIsRegExpEquivalent(CallRuntime* call) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6874 | return Bailout("inlined runtime function: IsRegExpEquivalent"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6875 | } |
| 6876 | |
| 6877 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6878 | void HGraphBuilder::GenerateGetCachedArrayIndex(CallRuntime* call) { |
| 6879 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6880 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6881 | HValue* value = Pop(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6882 | HGetCachedArrayIndex* result = new(zone()) HGetCachedArrayIndex(value); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6883 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6884 | } |
| 6885 | |
| 6886 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6887 | void HGraphBuilder::GenerateFastAsciiArrayJoin(CallRuntime* call) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6888 | return Bailout("inlined runtime function: FastAsciiArrayJoin"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6889 | } |
| 6890 | |
| 6891 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6892 | #undef CHECK_BAILOUT |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6893 | #undef CHECK_ALIVE |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6894 | |
| 6895 | |
| 6896 | HEnvironment::HEnvironment(HEnvironment* outer, |
| 6897 | Scope* scope, |
| 6898 | Handle<JSFunction> closure) |
| 6899 | : closure_(closure), |
| 6900 | values_(0), |
| 6901 | assigned_variables_(4), |
| 6902 | parameter_count_(0), |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6903 | specials_count_(1), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6904 | local_count_(0), |
| 6905 | outer_(outer), |
| 6906 | pop_count_(0), |
| 6907 | push_count_(0), |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 6908 | ast_id_(AstNode::kNoNumber), |
| 6909 | arguments_adaptor_(false) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6910 | Initialize(scope->num_parameters() + 1, scope->num_stack_slots(), 0); |
| 6911 | } |
| 6912 | |
| 6913 | |
| 6914 | HEnvironment::HEnvironment(const HEnvironment* other) |
| 6915 | : values_(0), |
| 6916 | assigned_variables_(0), |
| 6917 | parameter_count_(0), |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6918 | specials_count_(1), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6919 | local_count_(0), |
| 6920 | outer_(NULL), |
| 6921 | pop_count_(0), |
| 6922 | push_count_(0), |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 6923 | ast_id_(other->ast_id()), |
| 6924 | arguments_adaptor_(false) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6925 | Initialize(other); |
| 6926 | } |
| 6927 | |
| 6928 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 6929 | HEnvironment::HEnvironment(HEnvironment* outer, |
| 6930 | Handle<JSFunction> closure, |
| 6931 | int arguments) |
| 6932 | : closure_(closure), |
| 6933 | values_(arguments), |
| 6934 | assigned_variables_(0), |
| 6935 | parameter_count_(arguments), |
| 6936 | local_count_(0), |
| 6937 | outer_(outer), |
| 6938 | pop_count_(0), |
| 6939 | push_count_(0), |
| 6940 | ast_id_(AstNode::kNoNumber), |
| 6941 | arguments_adaptor_(true) { |
| 6942 | } |
| 6943 | |
| 6944 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6945 | void HEnvironment::Initialize(int parameter_count, |
| 6946 | int local_count, |
| 6947 | int stack_height) { |
| 6948 | parameter_count_ = parameter_count; |
| 6949 | local_count_ = local_count; |
| 6950 | |
| 6951 | // Avoid reallocating the temporaries' backing store on the first Push. |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6952 | int total = parameter_count + specials_count_ + local_count + stack_height; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6953 | values_.Initialize(total + 4); |
| 6954 | for (int i = 0; i < total; ++i) values_.Add(NULL); |
| 6955 | } |
| 6956 | |
| 6957 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 6958 | void HEnvironment::Initialize(const HEnvironment* other) { |
| 6959 | closure_ = other->closure(); |
| 6960 | values_.AddAll(other->values_); |
| 6961 | assigned_variables_.AddAll(other->assigned_variables_); |
| 6962 | parameter_count_ = other->parameter_count_; |
| 6963 | local_count_ = other->local_count_; |
| 6964 | if (other->outer_ != NULL) outer_ = other->outer_->Copy(); // Deep copy. |
| 6965 | pop_count_ = other->pop_count_; |
| 6966 | push_count_ = other->push_count_; |
| 6967 | ast_id_ = other->ast_id_; |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 6968 | arguments_adaptor_ = other->arguments_adaptor_; |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 6969 | } |
| 6970 | |
| 6971 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6972 | void HEnvironment::AddIncomingEdge(HBasicBlock* block, HEnvironment* other) { |
| 6973 | ASSERT(!block->IsLoopHeader()); |
| 6974 | ASSERT(values_.length() == other->values_.length()); |
| 6975 | |
| 6976 | int length = values_.length(); |
| 6977 | for (int i = 0; i < length; ++i) { |
| 6978 | HValue* value = values_[i]; |
| 6979 | if (value != NULL && value->IsPhi() && value->block() == block) { |
| 6980 | // There is already a phi for the i'th value. |
| 6981 | HPhi* phi = HPhi::cast(value); |
| 6982 | // Assert index is correct and that we haven't missed an incoming edge. |
| 6983 | ASSERT(phi->merged_index() == i); |
| 6984 | ASSERT(phi->OperandCount() == block->predecessors()->length()); |
| 6985 | phi->AddInput(other->values_[i]); |
| 6986 | } else if (values_[i] != other->values_[i]) { |
| 6987 | // There is a fresh value on the incoming edge, a phi is needed. |
| 6988 | ASSERT(values_[i] != NULL && other->values_[i] != NULL); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6989 | HPhi* phi = new(block->zone()) HPhi(i); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6990 | HValue* old_value = values_[i]; |
| 6991 | for (int j = 0; j < block->predecessors()->length(); j++) { |
| 6992 | phi->AddInput(old_value); |
| 6993 | } |
| 6994 | phi->AddInput(other->values_[i]); |
| 6995 | this->values_[i] = phi; |
| 6996 | block->AddPhi(phi); |
| 6997 | } |
| 6998 | } |
| 6999 | } |
| 7000 | |
| 7001 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 7002 | void HEnvironment::Bind(int index, HValue* value) { |
| 7003 | ASSERT(value != NULL); |
| 7004 | if (!assigned_variables_.Contains(index)) { |
| 7005 | assigned_variables_.Add(index); |
| 7006 | } |
| 7007 | values_[index] = value; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7008 | } |
| 7009 | |
| 7010 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 7011 | bool HEnvironment::HasExpressionAt(int index) const { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7012 | return index >= parameter_count_ + specials_count_ + local_count_; |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 7013 | } |
| 7014 | |
| 7015 | |
| 7016 | bool HEnvironment::ExpressionStackIsEmpty() const { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7017 | int first_expression = parameter_count() + specials_count() + local_count(); |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 7018 | ASSERT(length() >= first_expression); |
| 7019 | return length() == first_expression; |
| 7020 | } |
| 7021 | |
| 7022 | |
| 7023 | void HEnvironment::SetExpressionStackAt(int index_from_top, HValue* value) { |
| 7024 | int count = index_from_top + 1; |
| 7025 | int index = values_.length() - count; |
| 7026 | ASSERT(HasExpressionAt(index)); |
| 7027 | // The push count must include at least the element in question or else |
| 7028 | // the new value will not be included in this environment's history. |
| 7029 | if (push_count_ < count) { |
| 7030 | // This is the same effect as popping then re-pushing 'count' elements. |
| 7031 | pop_count_ += (count - push_count_); |
| 7032 | push_count_ = count; |
| 7033 | } |
| 7034 | values_[index] = value; |
| 7035 | } |
| 7036 | |
| 7037 | |
| 7038 | void HEnvironment::Drop(int count) { |
| 7039 | for (int i = 0; i < count; ++i) { |
| 7040 | Pop(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7041 | } |
| 7042 | } |
| 7043 | |
| 7044 | |
| 7045 | HEnvironment* HEnvironment::Copy() const { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7046 | return new(closure()->GetIsolate()->zone()) HEnvironment(this); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7047 | } |
| 7048 | |
| 7049 | |
| 7050 | HEnvironment* HEnvironment::CopyWithoutHistory() const { |
| 7051 | HEnvironment* result = Copy(); |
| 7052 | result->ClearHistory(); |
| 7053 | return result; |
| 7054 | } |
| 7055 | |
| 7056 | |
| 7057 | HEnvironment* HEnvironment::CopyAsLoopHeader(HBasicBlock* loop_header) const { |
| 7058 | HEnvironment* new_env = Copy(); |
| 7059 | for (int i = 0; i < values_.length(); ++i) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7060 | HPhi* phi = new(loop_header->zone()) HPhi(i); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7061 | phi->AddInput(values_[i]); |
| 7062 | new_env->values_[i] = phi; |
| 7063 | loop_header->AddPhi(phi); |
| 7064 | } |
| 7065 | new_env->ClearHistory(); |
| 7066 | return new_env; |
| 7067 | } |
| 7068 | |
| 7069 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 7070 | HEnvironment* HEnvironment::CopyForInlining( |
| 7071 | Handle<JSFunction> target, |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 7072 | int arguments, |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 7073 | FunctionLiteral* function, |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 7074 | HConstant* undefined, |
| 7075 | CallKind call_kind) const { |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 7076 | ASSERT(!is_arguments_adaptor()); |
| 7077 | |
| 7078 | Zone* zone = closure()->GetIsolate()->zone(); |
| 7079 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7080 | // Outer environment is a copy of this one without the arguments. |
| 7081 | int arity = function->scope()->num_parameters(); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 7082 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7083 | HEnvironment* outer = Copy(); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 7084 | outer->Drop(arguments + 1); // Including receiver. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7085 | outer->ClearHistory(); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 7086 | |
| 7087 | if (arity != arguments) { |
| 7088 | // Create artificial arguments adaptation environment. |
| 7089 | outer = new(zone) HEnvironment(outer, target, arguments + 1); |
| 7090 | for (int i = 0; i <= arguments; ++i) { // Include receiver. |
| 7091 | outer->Push(ExpressionStackAt(arguments - i)); |
| 7092 | } |
| 7093 | outer->ClearHistory(); |
| 7094 | } |
| 7095 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7096 | HEnvironment* inner = |
| 7097 | new(zone) HEnvironment(outer, function->scope(), target); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7098 | // Get the argument values from the original environment. |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7099 | for (int i = 0; i <= arity; ++i) { // Include receiver. |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 7100 | HValue* push = (i <= arguments) ? |
| 7101 | ExpressionStackAt(arguments - i) : undefined; |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7102 | inner->SetValueAt(i, push); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7103 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 7104 | // If the function we are inlining is a strict mode function or a |
| 7105 | // builtin function, pass undefined as the receiver for function |
| 7106 | // calls (instead of the global receiver). |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 7107 | if ((target->shared()->native() || !function->is_classic_mode()) && |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 7108 | call_kind == CALL_AS_FUNCTION) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 7109 | inner->SetValueAt(0, undefined); |
| 7110 | } |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 7111 | inner->SetValueAt(arity + 1, LookupContext()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7112 | for (int i = arity + 2; i < inner->length(); ++i) { |
| 7113 | inner->SetValueAt(i, undefined); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7114 | } |
| 7115 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 7116 | inner->set_ast_id(AstNode::kFunctionEntryId); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7117 | return inner; |
| 7118 | } |
| 7119 | |
| 7120 | |
| 7121 | void HEnvironment::PrintTo(StringStream* stream) { |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 7122 | for (int i = 0; i < length(); i++) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7123 | if (i == 0) stream->Add("parameters\n"); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7124 | if (i == parameter_count()) stream->Add("specials\n"); |
| 7125 | if (i == parameter_count() + specials_count()) stream->Add("locals\n"); |
| 7126 | if (i == parameter_count() + specials_count() + local_count()) { |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 7127 | stream->Add("expressions\n"); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7128 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7129 | HValue* val = values_.at(i); |
| 7130 | stream->Add("%d: ", i); |
| 7131 | if (val != NULL) { |
| 7132 | val->PrintNameTo(stream); |
| 7133 | } else { |
| 7134 | stream->Add("NULL"); |
| 7135 | } |
| 7136 | stream->Add("\n"); |
| 7137 | } |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 7138 | PrintF("\n"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7139 | } |
| 7140 | |
| 7141 | |
| 7142 | void HEnvironment::PrintToStd() { |
| 7143 | HeapStringAllocator string_allocator; |
| 7144 | StringStream trace(&string_allocator); |
| 7145 | PrintTo(&trace); |
| 7146 | PrintF("%s", *trace.ToCString()); |
| 7147 | } |
| 7148 | |
| 7149 | |
| 7150 | void HTracer::TraceCompilation(FunctionLiteral* function) { |
| 7151 | Tag tag(this, "compilation"); |
| 7152 | Handle<String> name = function->debug_name(); |
| 7153 | PrintStringProperty("name", *name->ToCString()); |
| 7154 | PrintStringProperty("method", *name->ToCString()); |
| 7155 | PrintLongProperty("date", static_cast<int64_t>(OS::TimeCurrentMillis())); |
| 7156 | } |
| 7157 | |
| 7158 | |
| 7159 | void HTracer::TraceLithium(const char* name, LChunk* chunk) { |
| 7160 | Trace(name, chunk->graph(), chunk); |
| 7161 | } |
| 7162 | |
| 7163 | |
| 7164 | void HTracer::TraceHydrogen(const char* name, HGraph* graph) { |
| 7165 | Trace(name, graph, NULL); |
| 7166 | } |
| 7167 | |
| 7168 | |
| 7169 | void HTracer::Trace(const char* name, HGraph* graph, LChunk* chunk) { |
| 7170 | Tag tag(this, "cfg"); |
| 7171 | PrintStringProperty("name", name); |
| 7172 | const ZoneList<HBasicBlock*>* blocks = graph->blocks(); |
| 7173 | for (int i = 0; i < blocks->length(); i++) { |
| 7174 | HBasicBlock* current = blocks->at(i); |
| 7175 | Tag block_tag(this, "block"); |
| 7176 | PrintBlockProperty("name", current->block_id()); |
| 7177 | PrintIntProperty("from_bci", -1); |
| 7178 | PrintIntProperty("to_bci", -1); |
| 7179 | |
| 7180 | if (!current->predecessors()->is_empty()) { |
| 7181 | PrintIndent(); |
| 7182 | trace_.Add("predecessors"); |
| 7183 | for (int j = 0; j < current->predecessors()->length(); ++j) { |
| 7184 | trace_.Add(" \"B%d\"", current->predecessors()->at(j)->block_id()); |
| 7185 | } |
| 7186 | trace_.Add("\n"); |
| 7187 | } else { |
| 7188 | PrintEmptyProperty("predecessors"); |
| 7189 | } |
| 7190 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7191 | if (current->end()->SuccessorCount() == 0) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7192 | PrintEmptyProperty("successors"); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7193 | } else { |
| 7194 | PrintIndent(); |
| 7195 | trace_.Add("successors"); |
| 7196 | for (HSuccessorIterator it(current->end()); !it.Done(); it.Advance()) { |
| 7197 | trace_.Add(" \"B%d\"", it.Current()->block_id()); |
| 7198 | } |
| 7199 | trace_.Add("\n"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7200 | } |
| 7201 | |
| 7202 | PrintEmptyProperty("xhandlers"); |
| 7203 | PrintEmptyProperty("flags"); |
| 7204 | |
| 7205 | if (current->dominator() != NULL) { |
| 7206 | PrintBlockProperty("dominator", current->dominator()->block_id()); |
| 7207 | } |
| 7208 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 7209 | PrintIntProperty("loop_depth", current->LoopNestingDepth()); |
| 7210 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7211 | if (chunk != NULL) { |
| 7212 | int first_index = current->first_instruction_index(); |
| 7213 | int last_index = current->last_instruction_index(); |
| 7214 | PrintIntProperty( |
| 7215 | "first_lir_id", |
| 7216 | LifetimePosition::FromInstructionIndex(first_index).Value()); |
| 7217 | PrintIntProperty( |
| 7218 | "last_lir_id", |
| 7219 | LifetimePosition::FromInstructionIndex(last_index).Value()); |
| 7220 | } |
| 7221 | |
| 7222 | { |
| 7223 | Tag states_tag(this, "states"); |
| 7224 | Tag locals_tag(this, "locals"); |
| 7225 | int total = current->phis()->length(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7226 | PrintIntProperty("size", current->phis()->length()); |
| 7227 | PrintStringProperty("method", "None"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7228 | for (int j = 0; j < total; ++j) { |
| 7229 | HPhi* phi = current->phis()->at(j); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7230 | PrintIndent(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7231 | trace_.Add("%d ", phi->merged_index()); |
| 7232 | phi->PrintNameTo(&trace_); |
| 7233 | trace_.Add(" "); |
| 7234 | phi->PrintTo(&trace_); |
| 7235 | trace_.Add("\n"); |
| 7236 | } |
| 7237 | } |
| 7238 | |
| 7239 | { |
| 7240 | Tag HIR_tag(this, "HIR"); |
| 7241 | HInstruction* instruction = current->first(); |
| 7242 | while (instruction != NULL) { |
| 7243 | int bci = 0; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 7244 | int uses = instruction->UseCount(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7245 | PrintIndent(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7246 | trace_.Add("%d %d ", bci, uses); |
| 7247 | instruction->PrintNameTo(&trace_); |
| 7248 | trace_.Add(" "); |
| 7249 | instruction->PrintTo(&trace_); |
| 7250 | trace_.Add(" <|@\n"); |
| 7251 | instruction = instruction->next(); |
| 7252 | } |
| 7253 | } |
| 7254 | |
| 7255 | |
| 7256 | if (chunk != NULL) { |
| 7257 | Tag LIR_tag(this, "LIR"); |
| 7258 | int first_index = current->first_instruction_index(); |
| 7259 | int last_index = current->last_instruction_index(); |
| 7260 | if (first_index != -1 && last_index != -1) { |
| 7261 | const ZoneList<LInstruction*>* instructions = chunk->instructions(); |
| 7262 | for (int i = first_index; i <= last_index; ++i) { |
| 7263 | LInstruction* linstr = instructions->at(i); |
| 7264 | if (linstr != NULL) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7265 | PrintIndent(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7266 | trace_.Add("%d ", |
| 7267 | LifetimePosition::FromInstructionIndex(i).Value()); |
| 7268 | linstr->PrintTo(&trace_); |
| 7269 | trace_.Add(" <|@\n"); |
| 7270 | } |
| 7271 | } |
| 7272 | } |
| 7273 | } |
| 7274 | } |
| 7275 | } |
| 7276 | |
| 7277 | |
| 7278 | void HTracer::TraceLiveRanges(const char* name, LAllocator* allocator) { |
| 7279 | Tag tag(this, "intervals"); |
| 7280 | PrintStringProperty("name", name); |
| 7281 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 7282 | const Vector<LiveRange*>* fixed_d = allocator->fixed_double_live_ranges(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7283 | for (int i = 0; i < fixed_d->length(); ++i) { |
| 7284 | TraceLiveRange(fixed_d->at(i), "fixed"); |
| 7285 | } |
| 7286 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 7287 | const Vector<LiveRange*>* fixed = allocator->fixed_live_ranges(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7288 | for (int i = 0; i < fixed->length(); ++i) { |
| 7289 | TraceLiveRange(fixed->at(i), "fixed"); |
| 7290 | } |
| 7291 | |
| 7292 | const ZoneList<LiveRange*>* live_ranges = allocator->live_ranges(); |
| 7293 | for (int i = 0; i < live_ranges->length(); ++i) { |
| 7294 | TraceLiveRange(live_ranges->at(i), "object"); |
| 7295 | } |
| 7296 | } |
| 7297 | |
| 7298 | |
| 7299 | void HTracer::TraceLiveRange(LiveRange* range, const char* type) { |
| 7300 | if (range != NULL && !range->IsEmpty()) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7301 | PrintIndent(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7302 | trace_.Add("%d %s", range->id(), type); |
| 7303 | if (range->HasRegisterAssigned()) { |
| 7304 | LOperand* op = range->CreateAssignedOperand(); |
| 7305 | int assigned_reg = op->index(); |
| 7306 | if (op->IsDoubleRegister()) { |
| 7307 | trace_.Add(" \"%s\"", |
| 7308 | DoubleRegister::AllocationIndexToString(assigned_reg)); |
| 7309 | } else { |
| 7310 | ASSERT(op->IsRegister()); |
| 7311 | trace_.Add(" \"%s\"", Register::AllocationIndexToString(assigned_reg)); |
| 7312 | } |
| 7313 | } else if (range->IsSpilled()) { |
| 7314 | LOperand* op = range->TopLevel()->GetSpillOperand(); |
| 7315 | if (op->IsDoubleStackSlot()) { |
| 7316 | trace_.Add(" \"double_stack:%d\"", op->index()); |
| 7317 | } else { |
| 7318 | ASSERT(op->IsStackSlot()); |
| 7319 | trace_.Add(" \"stack:%d\"", op->index()); |
| 7320 | } |
| 7321 | } |
| 7322 | int parent_index = -1; |
| 7323 | if (range->IsChild()) { |
| 7324 | parent_index = range->parent()->id(); |
| 7325 | } else { |
| 7326 | parent_index = range->id(); |
| 7327 | } |
| 7328 | LOperand* op = range->FirstHint(); |
| 7329 | int hint_index = -1; |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 7330 | if (op != NULL && op->IsUnallocated()) { |
| 7331 | hint_index = LUnallocated::cast(op)->virtual_register(); |
| 7332 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7333 | trace_.Add(" %d %d", parent_index, hint_index); |
| 7334 | UseInterval* cur_interval = range->first_interval(); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 7335 | while (cur_interval != NULL && range->Covers(cur_interval->start())) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7336 | trace_.Add(" [%d, %d[", |
| 7337 | cur_interval->start().Value(), |
| 7338 | cur_interval->end().Value()); |
| 7339 | cur_interval = cur_interval->next(); |
| 7340 | } |
| 7341 | |
| 7342 | UsePosition* current_pos = range->first_pos(); |
| 7343 | while (current_pos != NULL) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 7344 | if (current_pos->RegisterIsBeneficial() || FLAG_trace_all_uses) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7345 | trace_.Add(" %d M", current_pos->pos().Value()); |
| 7346 | } |
| 7347 | current_pos = current_pos->next(); |
| 7348 | } |
| 7349 | |
| 7350 | trace_.Add(" \"\"\n"); |
| 7351 | } |
| 7352 | } |
| 7353 | |
| 7354 | |
| 7355 | void HTracer::FlushToFile() { |
| 7356 | AppendChars(filename_, *trace_.ToCString(), trace_.length(), false); |
| 7357 | trace_.Reset(); |
| 7358 | } |
| 7359 | |
| 7360 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 7361 | void HStatistics::Initialize(CompilationInfo* info) { |
| 7362 | source_size_ += info->shared_info()->SourceSize(); |
| 7363 | } |
| 7364 | |
| 7365 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7366 | void HStatistics::Print() { |
| 7367 | PrintF("Timing results:\n"); |
| 7368 | int64_t sum = 0; |
| 7369 | for (int i = 0; i < timing_.length(); ++i) { |
| 7370 | sum += timing_[i]; |
| 7371 | } |
| 7372 | |
| 7373 | for (int i = 0; i < names_.length(); ++i) { |
| 7374 | PrintF("%30s", names_[i]); |
| 7375 | double ms = static_cast<double>(timing_[i]) / 1000; |
| 7376 | double percent = static_cast<double>(timing_[i]) * 100 / sum; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 7377 | PrintF(" - %7.3f ms / %4.1f %% ", ms, percent); |
| 7378 | |
| 7379 | unsigned size = sizes_[i]; |
| 7380 | double size_percent = static_cast<double>(size) * 100 / total_size_; |
| 7381 | PrintF(" %8u bytes / %4.1f %%\n", size, size_percent); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7382 | } |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 7383 | double source_size_in_kb = static_cast<double>(source_size_) / 1024; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7384 | double normalized_time = source_size_in_kb > 0 |
| 7385 | ? (static_cast<double>(sum) / 1000) / source_size_in_kb |
| 7386 | : 0; |
| 7387 | double normalized_bytes = source_size_in_kb > 0 |
| 7388 | ? total_size_ / source_size_in_kb |
| 7389 | : 0; |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 7390 | PrintF("%30s - %7.3f ms %7.3f bytes\n", "Sum", |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7391 | normalized_time, normalized_bytes); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7392 | PrintF("---------------------------------------------------------------\n"); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 7393 | PrintF("%30s - %7.3f ms (%.1f times slower than full code gen)\n", |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7394 | "Total", |
| 7395 | static_cast<double>(total_) / 1000, |
| 7396 | static_cast<double>(total_) / full_code_gen_); |
| 7397 | } |
| 7398 | |
| 7399 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 7400 | void HStatistics::SaveTiming(const char* name, int64_t ticks, unsigned size) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7401 | if (name == HPhase::kFullCodeGen) { |
| 7402 | full_code_gen_ += ticks; |
| 7403 | } else if (name == HPhase::kTotal) { |
| 7404 | total_ += ticks; |
| 7405 | } else { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 7406 | total_size_ += size; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7407 | for (int i = 0; i < names_.length(); ++i) { |
| 7408 | if (names_[i] == name) { |
| 7409 | timing_[i] += ticks; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 7410 | sizes_[i] += size; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7411 | return; |
| 7412 | } |
| 7413 | } |
| 7414 | names_.Add(name); |
| 7415 | timing_.Add(ticks); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 7416 | sizes_.Add(size); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7417 | } |
| 7418 | } |
| 7419 | |
| 7420 | |
| 7421 | const char* const HPhase::kFullCodeGen = "Full code generator"; |
| 7422 | const char* const HPhase::kTotal = "Total"; |
| 7423 | |
| 7424 | |
| 7425 | void HPhase::Begin(const char* name, |
| 7426 | HGraph* graph, |
| 7427 | LChunk* chunk, |
| 7428 | LAllocator* allocator) { |
| 7429 | name_ = name; |
| 7430 | graph_ = graph; |
| 7431 | chunk_ = chunk; |
| 7432 | allocator_ = allocator; |
| 7433 | if (allocator != NULL && chunk_ == NULL) { |
| 7434 | chunk_ = allocator->chunk(); |
| 7435 | } |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 7436 | if (FLAG_hydrogen_stats) start_ = OS::Ticks(); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 7437 | start_allocation_size_ = Zone::allocation_size_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7438 | } |
| 7439 | |
| 7440 | |
| 7441 | void HPhase::End() const { |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 7442 | if (FLAG_hydrogen_stats) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7443 | int64_t end = OS::Ticks(); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 7444 | unsigned size = Zone::allocation_size_ - start_allocation_size_; |
| 7445 | HStatistics::Instance()->SaveTiming(name_, end - start_, size); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7446 | } |
| 7447 | |
| 7448 | if (FLAG_trace_hydrogen) { |
| 7449 | if (graph_ != NULL) HTracer::Instance()->TraceHydrogen(name_, graph_); |
| 7450 | if (chunk_ != NULL) HTracer::Instance()->TraceLithium(name_, chunk_); |
| 7451 | if (allocator_ != NULL) { |
| 7452 | HTracer::Instance()->TraceLiveRanges(name_, allocator_); |
| 7453 | } |
| 7454 | } |
| 7455 | |
| 7456 | #ifdef DEBUG |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 7457 | if (graph_ != NULL) graph_->Verify(false); // No full verify. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7458 | if (allocator_ != NULL) allocator_->Verify(); |
| 7459 | #endif |
| 7460 | } |
| 7461 | |
| 7462 | } } // namespace v8::internal |