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), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 58 | phis_(4, graph->zone()), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 59 | first_(NULL), |
| 60 | last_(NULL), |
| 61 | end_(NULL), |
| 62 | loop_information_(NULL), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 63 | predecessors_(2, graph->zone()), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 64 | dominator_(NULL), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 65 | dominated_blocks_(4, graph->zone()), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 66 | last_environment_(NULL), |
| 67 | argument_count_(-1), |
| 68 | first_instruction_index_(-1), |
| 69 | last_instruction_index_(-1), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 70 | deleted_phis_(4, graph->zone()), |
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), |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 73 | is_deoptimizing_(false), |
| 74 | dominates_loop_successors_(false) { } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 75 | |
| 76 | |
| 77 | void HBasicBlock::AttachLoopInformation() { |
| 78 | ASSERT(!IsLoopHeader()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 79 | loop_information_ = new(zone()) HLoopInformation(this, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | |
| 83 | void HBasicBlock::DetachLoopInformation() { |
| 84 | ASSERT(IsLoopHeader()); |
| 85 | loop_information_ = NULL; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | void HBasicBlock::AddPhi(HPhi* phi) { |
| 90 | ASSERT(!IsStartBlock()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 91 | phis_.Add(phi, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 92 | phi->SetBlock(this); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | void HBasicBlock::RemovePhi(HPhi* phi) { |
| 97 | ASSERT(phi->block() == this); |
| 98 | ASSERT(phis_.Contains(phi)); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 99 | ASSERT(phi->HasNoUses() || !phi->is_live()); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 100 | phi->Kill(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 101 | phis_.RemoveElement(phi); |
| 102 | phi->SetBlock(NULL); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | void HBasicBlock::AddInstruction(HInstruction* instr) { |
| 107 | ASSERT(!IsStartBlock() || !IsFinished()); |
| 108 | ASSERT(!instr->IsLinked()); |
| 109 | ASSERT(!IsFinished()); |
| 110 | if (first_ == NULL) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 111 | HBlockEntry* entry = new(zone()) HBlockEntry(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 112 | entry->InitializeAsFirst(this); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 113 | first_ = last_ = entry; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 114 | } |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 115 | instr->InsertAfter(last_); |
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()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 122 | if (has_uses == HDeoptimize::kNoUses) |
| 123 | return new(zone()) HDeoptimize(0, zone()); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 124 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 125 | HEnvironment* environment = last_environment(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 126 | HDeoptimize* instr = new(zone()) HDeoptimize(environment->length(), zone()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 127 | for (int i = 0; i < environment->length(); i++) { |
| 128 | HValue* val = environment->values()->at(i); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 129 | instr->AddEnvironmentValue(val, zone()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | return instr; |
| 133 | } |
| 134 | |
| 135 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 136 | HSimulate* HBasicBlock::CreateSimulate(int ast_id) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 137 | ASSERT(HasEnvironment()); |
| 138 | HEnvironment* environment = last_environment(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 139 | ASSERT(ast_id == AstNode::kNoNumber || |
| 140 | environment->closure()->shared()->VerifyBailoutId(ast_id)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 141 | |
| 142 | int push_count = environment->push_count(); |
| 143 | int pop_count = environment->pop_count(); |
| 144 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 145 | HSimulate* instr = new(zone()) HSimulate(ast_id, pop_count, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 146 | for (int i = push_count - 1; i >= 0; --i) { |
| 147 | instr->AddPushedValue(environment->ExpressionStackAt(i)); |
| 148 | } |
| 149 | for (int i = 0; i < environment->assigned_variables()->length(); ++i) { |
| 150 | int index = environment->assigned_variables()->at(i); |
| 151 | instr->AddAssignedValue(index, environment->Lookup(index)); |
| 152 | } |
| 153 | environment->ClearHistory(); |
| 154 | return instr; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | void HBasicBlock::Finish(HControlInstruction* end) { |
| 159 | ASSERT(!IsFinished()); |
| 160 | AddInstruction(end); |
| 161 | end_ = end; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 162 | for (HSuccessorIterator it(end); !it.Done(); it.Advance()) { |
| 163 | it.Current()->RegisterPredecessor(this); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 168 | void HBasicBlock::Goto(HBasicBlock* block, FunctionState* state) { |
| 169 | bool drop_extra = state != NULL && state->drop_extra(); |
| 170 | bool arguments_pushed = state != NULL && state->arguments_pushed(); |
| 171 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 172 | if (block->IsInlineReturnTarget()) { |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 173 | AddInstruction(new(zone()) HLeaveInlined(arguments_pushed)); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 174 | last_environment_ = last_environment()->DiscardInlined(drop_extra); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 175 | } |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 176 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 177 | AddSimulate(AstNode::kNoNumber); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 178 | HGoto* instr = new(zone()) HGoto(block); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 179 | Finish(instr); |
| 180 | } |
| 181 | |
| 182 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 183 | void HBasicBlock::AddLeaveInlined(HValue* return_value, |
| 184 | HBasicBlock* target, |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 185 | FunctionState* state) { |
| 186 | bool drop_extra = state != NULL && state->drop_extra(); |
| 187 | bool arguments_pushed = state != NULL && state->arguments_pushed(); |
| 188 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 189 | ASSERT(target->IsInlineReturnTarget()); |
| 190 | ASSERT(return_value != NULL); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 191 | AddInstruction(new(zone()) HLeaveInlined(arguments_pushed)); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 192 | last_environment_ = last_environment()->DiscardInlined(drop_extra); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 193 | last_environment()->Push(return_value); |
| 194 | AddSimulate(AstNode::kNoNumber); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 195 | HGoto* instr = new(zone()) HGoto(target); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 196 | Finish(instr); |
| 197 | } |
| 198 | |
| 199 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 200 | void HBasicBlock::SetInitialEnvironment(HEnvironment* env) { |
| 201 | ASSERT(!HasEnvironment()); |
| 202 | ASSERT(first() == NULL); |
| 203 | UpdateEnvironment(env); |
| 204 | } |
| 205 | |
| 206 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 207 | void HBasicBlock::SetJoinId(int ast_id) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 208 | int length = predecessors_.length(); |
| 209 | ASSERT(length > 0); |
| 210 | for (int i = 0; i < length; i++) { |
| 211 | HBasicBlock* predecessor = predecessors_[i]; |
| 212 | ASSERT(predecessor->end()->IsGoto()); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 213 | HSimulate* simulate = HSimulate::cast(predecessor->end()->previous()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 214 | // We only need to verify the ID once. |
| 215 | ASSERT(i != 0 || |
| 216 | predecessor->last_environment()->closure()->shared() |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 217 | ->VerifyBailoutId(ast_id)); |
| 218 | simulate->set_ast_id(ast_id); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | |
| 223 | bool HBasicBlock::Dominates(HBasicBlock* other) const { |
| 224 | HBasicBlock* current = other->dominator(); |
| 225 | while (current != NULL) { |
| 226 | if (current == this) return true; |
| 227 | current = current->dominator(); |
| 228 | } |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 233 | int HBasicBlock::LoopNestingDepth() const { |
| 234 | const HBasicBlock* current = this; |
| 235 | int result = (current->IsLoopHeader()) ? 1 : 0; |
| 236 | while (current->parent_loop_header() != NULL) { |
| 237 | current = current->parent_loop_header(); |
| 238 | result++; |
| 239 | } |
| 240 | return result; |
| 241 | } |
| 242 | |
| 243 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 244 | void HBasicBlock::PostProcessLoopHeader(IterationStatement* stmt) { |
| 245 | ASSERT(IsLoopHeader()); |
| 246 | |
| 247 | SetJoinId(stmt->EntryId()); |
| 248 | if (predecessors()->length() == 1) { |
| 249 | // This is a degenerated loop. |
| 250 | DetachLoopInformation(); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | // Only the first entry into the loop is from outside the loop. All other |
| 255 | // entries must be back edges. |
| 256 | for (int i = 1; i < predecessors()->length(); ++i) { |
| 257 | loop_information()->RegisterBackEdge(predecessors()->at(i)); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | |
| 262 | void HBasicBlock::RegisterPredecessor(HBasicBlock* pred) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 263 | if (HasPredecessor()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 264 | // Only loop header blocks can have a predecessor added after |
| 265 | // instructions have been added to the block (they have phis for all |
| 266 | // values in the environment, these phis may be eliminated later). |
| 267 | ASSERT(IsLoopHeader() || first_ == NULL); |
| 268 | HEnvironment* incoming_env = pred->last_environment(); |
| 269 | if (IsLoopHeader()) { |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 270 | ASSERT(phis()->length() == incoming_env->length()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 271 | for (int i = 0; i < phis_.length(); ++i) { |
| 272 | phis_[i]->AddInput(incoming_env->values()->at(i)); |
| 273 | } |
| 274 | } else { |
| 275 | last_environment()->AddIncomingEdge(this, pred->last_environment()); |
| 276 | } |
| 277 | } else if (!HasEnvironment() && !IsFinished()) { |
| 278 | ASSERT(!IsLoopHeader()); |
| 279 | SetInitialEnvironment(pred->last_environment()->Copy()); |
| 280 | } |
| 281 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 282 | predecessors_.Add(pred, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | |
| 286 | void HBasicBlock::AddDominatedBlock(HBasicBlock* block) { |
| 287 | ASSERT(!dominated_blocks_.Contains(block)); |
| 288 | // Keep the list of dominated blocks sorted such that if there is two |
| 289 | // succeeding block in this list, the predecessor is before the successor. |
| 290 | int index = 0; |
| 291 | while (index < dominated_blocks_.length() && |
| 292 | dominated_blocks_[index]->block_id() < block->block_id()) { |
| 293 | ++index; |
| 294 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 295 | dominated_blocks_.InsertAt(index, block, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | |
| 299 | void HBasicBlock::AssignCommonDominator(HBasicBlock* other) { |
| 300 | if (dominator_ == NULL) { |
| 301 | dominator_ = other; |
| 302 | other->AddDominatedBlock(this); |
| 303 | } else if (other->dominator() != NULL) { |
| 304 | HBasicBlock* first = dominator_; |
| 305 | HBasicBlock* second = other; |
| 306 | |
| 307 | while (first != second) { |
| 308 | if (first->block_id() > second->block_id()) { |
| 309 | first = first->dominator(); |
| 310 | } else { |
| 311 | second = second->dominator(); |
| 312 | } |
| 313 | ASSERT(first != NULL && second != NULL); |
| 314 | } |
| 315 | |
| 316 | if (dominator_ != first) { |
| 317 | ASSERT(dominator_->dominated_blocks_.Contains(this)); |
| 318 | dominator_->dominated_blocks_.RemoveElement(this); |
| 319 | dominator_ = first; |
| 320 | first->AddDominatedBlock(this); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 326 | void HBasicBlock::AssignLoopSuccessorDominators() { |
| 327 | // Mark blocks that dominate all subsequent reachable blocks inside their |
| 328 | // loop. Exploit the fact that blocks are sorted in reverse post order. When |
| 329 | // the loop is visited in increasing block id order, if the number of |
| 330 | // non-loop-exiting successor edges at the dominator_candidate block doesn't |
| 331 | // exceed the number of previously encountered predecessor edges, there is no |
| 332 | // path from the loop header to any block with higher id that doesn't go |
| 333 | // through the dominator_candidate block. In this case, the |
| 334 | // dominator_candidate block is guaranteed to dominate all blocks reachable |
| 335 | // from it with higher ids. |
| 336 | HBasicBlock* last = loop_information()->GetLastBackEdge(); |
| 337 | int outstanding_successors = 1; // one edge from the pre-header |
| 338 | // Header always dominates everything. |
| 339 | MarkAsLoopSuccessorDominator(); |
| 340 | for (int j = block_id(); j <= last->block_id(); ++j) { |
| 341 | HBasicBlock* dominator_candidate = graph_->blocks()->at(j); |
| 342 | for (HPredecessorIterator it(dominator_candidate); !it.Done(); |
| 343 | it.Advance()) { |
| 344 | HBasicBlock* predecessor = it.Current(); |
| 345 | // Don't count back edges. |
| 346 | if (predecessor->block_id() < dominator_candidate->block_id()) { |
| 347 | outstanding_successors--; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | // If more successors than predecessors have been seen in the loop up to |
| 352 | // now, it's not possible to guarantee that the current block dominates |
| 353 | // all of the blocks with higher IDs. In this case, assume conservatively |
| 354 | // that those paths through loop that don't go through the current block |
| 355 | // contain all of the loop's dependencies. Also be careful to record |
| 356 | // dominator information about the current loop that's being processed, |
| 357 | // and not nested loops, which will be processed when |
| 358 | // AssignLoopSuccessorDominators gets called on their header. |
| 359 | ASSERT(outstanding_successors >= 0); |
| 360 | HBasicBlock* parent_loop_header = dominator_candidate->parent_loop_header(); |
| 361 | if (outstanding_successors == 0 && |
| 362 | (parent_loop_header == this && !dominator_candidate->IsLoopHeader())) { |
| 363 | dominator_candidate->MarkAsLoopSuccessorDominator(); |
| 364 | } |
| 365 | HControlInstruction* end = dominator_candidate->end(); |
| 366 | for (HSuccessorIterator it(end); !it.Done(); it.Advance()) { |
| 367 | HBasicBlock* successor = it.Current(); |
| 368 | // Only count successors that remain inside the loop and don't loop back |
| 369 | // to a loop header. |
| 370 | if (successor->block_id() > dominator_candidate->block_id() && |
| 371 | successor->block_id() <= last->block_id()) { |
| 372 | // Backwards edges must land on loop headers. |
| 373 | ASSERT(successor->block_id() > dominator_candidate->block_id() || |
| 374 | successor->IsLoopHeader()); |
| 375 | outstanding_successors++; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 382 | int HBasicBlock::PredecessorIndexOf(HBasicBlock* predecessor) const { |
| 383 | for (int i = 0; i < predecessors_.length(); ++i) { |
| 384 | if (predecessors_[i] == predecessor) return i; |
| 385 | } |
| 386 | UNREACHABLE(); |
| 387 | return -1; |
| 388 | } |
| 389 | |
| 390 | |
| 391 | #ifdef DEBUG |
| 392 | void HBasicBlock::Verify() { |
| 393 | // Check that every block is finished. |
| 394 | ASSERT(IsFinished()); |
| 395 | ASSERT(block_id() >= 0); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 396 | |
| 397 | // Check that the incoming edges are in edge split form. |
| 398 | if (predecessors_.length() > 1) { |
| 399 | for (int i = 0; i < predecessors_.length(); ++i) { |
| 400 | ASSERT(predecessors_[i]->end()->SecondSuccessor() == NULL); |
| 401 | } |
| 402 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 403 | } |
| 404 | #endif |
| 405 | |
| 406 | |
| 407 | void HLoopInformation::RegisterBackEdge(HBasicBlock* block) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 408 | this->back_edges_.Add(block, block->zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 409 | AddBlock(block); |
| 410 | } |
| 411 | |
| 412 | |
| 413 | HBasicBlock* HLoopInformation::GetLastBackEdge() const { |
| 414 | int max_id = -1; |
| 415 | HBasicBlock* result = NULL; |
| 416 | for (int i = 0; i < back_edges_.length(); ++i) { |
| 417 | HBasicBlock* cur = back_edges_[i]; |
| 418 | if (cur->block_id() > max_id) { |
| 419 | max_id = cur->block_id(); |
| 420 | result = cur; |
| 421 | } |
| 422 | } |
| 423 | return result; |
| 424 | } |
| 425 | |
| 426 | |
| 427 | void HLoopInformation::AddBlock(HBasicBlock* block) { |
| 428 | if (block == loop_header()) return; |
| 429 | if (block->parent_loop_header() == loop_header()) return; |
| 430 | if (block->parent_loop_header() != NULL) { |
| 431 | AddBlock(block->parent_loop_header()); |
| 432 | } else { |
| 433 | block->set_parent_loop_header(loop_header()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 434 | blocks_.Add(block, block->zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 435 | for (int i = 0; i < block->predecessors()->length(); ++i) { |
| 436 | AddBlock(block->predecessors()->at(i)); |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | |
| 442 | #ifdef DEBUG |
| 443 | |
| 444 | // Checks reachability of the blocks in this graph and stores a bit in |
| 445 | // the BitVector "reachable()" for every block that can be reached |
| 446 | // from the start block of the graph. If "dont_visit" is non-null, the given |
| 447 | // block is treated as if it would not be part of the graph. "visited_count()" |
| 448 | // returns the number of reachable blocks. |
| 449 | class ReachabilityAnalyzer BASE_EMBEDDED { |
| 450 | public: |
| 451 | ReachabilityAnalyzer(HBasicBlock* entry_block, |
| 452 | int block_count, |
| 453 | HBasicBlock* dont_visit) |
| 454 | : visited_count_(0), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 455 | stack_(16, entry_block->zone()), |
| 456 | reachable_(block_count, entry_block->zone()), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 457 | dont_visit_(dont_visit) { |
| 458 | PushBlock(entry_block); |
| 459 | Analyze(); |
| 460 | } |
| 461 | |
| 462 | int visited_count() const { return visited_count_; } |
| 463 | const BitVector* reachable() const { return &reachable_; } |
| 464 | |
| 465 | private: |
| 466 | void PushBlock(HBasicBlock* block) { |
| 467 | if (block != NULL && block != dont_visit_ && |
| 468 | !reachable_.Contains(block->block_id())) { |
| 469 | reachable_.Add(block->block_id()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 470 | stack_.Add(block, block->zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 471 | visited_count_++; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | void Analyze() { |
| 476 | while (!stack_.is_empty()) { |
| 477 | HControlInstruction* end = stack_.RemoveLast()->end(); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 478 | for (HSuccessorIterator it(end); !it.Done(); it.Advance()) { |
| 479 | PushBlock(it.Current()); |
| 480 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
| 484 | int visited_count_; |
| 485 | ZoneList<HBasicBlock*> stack_; |
| 486 | BitVector reachable_; |
| 487 | HBasicBlock* dont_visit_; |
| 488 | }; |
| 489 | |
| 490 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 491 | void HGraph::Verify(bool do_full_verify) const { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 492 | for (int i = 0; i < blocks_.length(); i++) { |
| 493 | HBasicBlock* block = blocks_.at(i); |
| 494 | |
| 495 | block->Verify(); |
| 496 | |
| 497 | // Check that every block contains at least one node and that only the last |
| 498 | // node is a control instruction. |
| 499 | HInstruction* current = block->first(); |
| 500 | ASSERT(current != NULL && current->IsBlockEntry()); |
| 501 | while (current != NULL) { |
| 502 | ASSERT((current->next() == NULL) == current->IsControlInstruction()); |
| 503 | ASSERT(current->block() == block); |
| 504 | current->Verify(); |
| 505 | current = current->next(); |
| 506 | } |
| 507 | |
| 508 | // Check that successors are correctly set. |
| 509 | HBasicBlock* first = block->end()->FirstSuccessor(); |
| 510 | HBasicBlock* second = block->end()->SecondSuccessor(); |
| 511 | ASSERT(second == NULL || first != NULL); |
| 512 | |
| 513 | // Check that the predecessor array is correct. |
| 514 | if (first != NULL) { |
| 515 | ASSERT(first->predecessors()->Contains(block)); |
| 516 | if (second != NULL) { |
| 517 | ASSERT(second->predecessors()->Contains(block)); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | // Check that phis have correct arguments. |
| 522 | for (int j = 0; j < block->phis()->length(); j++) { |
| 523 | HPhi* phi = block->phis()->at(j); |
| 524 | phi->Verify(); |
| 525 | } |
| 526 | |
| 527 | // Check that all join blocks have predecessors that end with an |
| 528 | // unconditional goto and agree on their environment node id. |
| 529 | if (block->predecessors()->length() >= 2) { |
| 530 | int id = block->predecessors()->first()->last_environment()->ast_id(); |
| 531 | for (int k = 0; k < block->predecessors()->length(); k++) { |
| 532 | HBasicBlock* predecessor = block->predecessors()->at(k); |
| 533 | ASSERT(predecessor->end()->IsGoto()); |
| 534 | ASSERT(predecessor->last_environment()->ast_id() == id); |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | // Check special property of first block to have no predecessors. |
| 540 | ASSERT(blocks_.at(0)->predecessors()->is_empty()); |
| 541 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 542 | if (do_full_verify) { |
| 543 | // Check that the graph is fully connected. |
| 544 | ReachabilityAnalyzer analyzer(entry_block_, blocks_.length(), NULL); |
| 545 | ASSERT(analyzer.visited_count() == blocks_.length()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 546 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 547 | // Check that entry block dominator is NULL. |
| 548 | ASSERT(entry_block_->dominator() == NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 549 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 550 | // Check dominators. |
| 551 | for (int i = 0; i < blocks_.length(); ++i) { |
| 552 | HBasicBlock* block = blocks_.at(i); |
| 553 | if (block->dominator() == NULL) { |
| 554 | // Only start block may have no dominator assigned to. |
| 555 | ASSERT(i == 0); |
| 556 | } else { |
| 557 | // Assert that block is unreachable if dominator must not be visited. |
| 558 | ReachabilityAnalyzer dominator_analyzer(entry_block_, |
| 559 | blocks_.length(), |
| 560 | block->dominator()); |
| 561 | ASSERT(!dominator_analyzer.reachable()->Contains(block->block_id())); |
| 562 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | #endif |
| 568 | |
| 569 | |
| 570 | HConstant* HGraph::GetConstant(SetOncePointer<HConstant>* pointer, |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame] | 571 | Handle<Object> value) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 572 | if (!pointer->is_set()) { |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame] | 573 | HConstant* constant = new(zone()) HConstant(value, |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 574 | Representation::Tagged()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 575 | constant->InsertAfter(GetConstantUndefined()); |
| 576 | pointer->set(constant); |
| 577 | } |
| 578 | return pointer->get(); |
| 579 | } |
| 580 | |
| 581 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 582 | HConstant* HGraph::GetConstantInt32(SetOncePointer<HConstant>* pointer, |
| 583 | int32_t value) { |
| 584 | if (!pointer->is_set()) { |
| 585 | HConstant* constant = |
| 586 | new(zone()) HConstant(value, Representation::Integer32()); |
| 587 | constant->InsertAfter(GetConstantUndefined()); |
| 588 | pointer->set(constant); |
| 589 | } |
| 590 | return pointer->get(); |
| 591 | } |
| 592 | |
| 593 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 594 | HConstant* HGraph::GetConstant1() { |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 595 | return GetConstantInt32(&constant_1_, 1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | |
| 599 | HConstant* HGraph::GetConstantMinus1() { |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 600 | return GetConstantInt32(&constant_minus1_, -1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | |
| 604 | HConstant* HGraph::GetConstantTrue() { |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame] | 605 | return GetConstant(&constant_true_, isolate()->factory()->true_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | |
| 609 | HConstant* HGraph::GetConstantFalse() { |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame] | 610 | return GetConstant(&constant_false_, isolate()->factory()->false_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 611 | } |
| 612 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 613 | |
| 614 | HConstant* HGraph::GetConstantHole() { |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame] | 615 | return GetConstant(&constant_hole_, isolate()->factory()->the_hole_value()); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 619 | HGraphBuilder::HGraphBuilder(CompilationInfo* info, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 620 | TypeFeedbackOracle* oracle) |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 621 | : function_state_(NULL), |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 622 | initial_function_state_(this, info, oracle, NORMAL_RETURN), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 623 | ast_context_(NULL), |
| 624 | break_scope_(NULL), |
| 625 | graph_(NULL), |
| 626 | current_block_(NULL), |
| 627 | inlined_count_(0), |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 628 | globals_(10, info->zone()), |
| 629 | zone_(info->zone()), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 630 | inline_bailout_(false) { |
| 631 | // This is not initialized in the initializer list because the |
| 632 | // constructor for the initial state relies on function_state_ == NULL |
| 633 | // to know it's the initial state. |
| 634 | function_state_= &initial_function_state_; |
| 635 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 636 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 637 | HBasicBlock* HGraphBuilder::CreateJoin(HBasicBlock* first, |
| 638 | HBasicBlock* second, |
| 639 | int join_id) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 640 | if (first == NULL) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 641 | return second; |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 642 | } else if (second == NULL) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 643 | return first; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 644 | } else { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 645 | HBasicBlock* join_block = graph_->CreateBasicBlock(); |
| 646 | first->Goto(join_block); |
| 647 | second->Goto(join_block); |
| 648 | join_block->SetJoinId(join_id); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 649 | return join_block; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
| 653 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 654 | HBasicBlock* HGraphBuilder::JoinContinue(IterationStatement* statement, |
| 655 | HBasicBlock* exit_block, |
| 656 | HBasicBlock* continue_block) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 657 | if (continue_block != NULL) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 658 | if (exit_block != NULL) exit_block->Goto(continue_block); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 659 | continue_block->SetJoinId(statement->ContinueId()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 660 | return continue_block; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 661 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 662 | return exit_block; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 666 | HBasicBlock* HGraphBuilder::CreateLoop(IterationStatement* statement, |
| 667 | HBasicBlock* loop_entry, |
| 668 | HBasicBlock* body_exit, |
| 669 | HBasicBlock* loop_successor, |
| 670 | HBasicBlock* break_block) { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 671 | if (body_exit != NULL) body_exit->Goto(loop_entry); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 672 | loop_entry->PostProcessLoopHeader(statement); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 673 | if (break_block != NULL) { |
| 674 | if (loop_successor != NULL) loop_successor->Goto(break_block); |
| 675 | break_block->SetJoinId(statement->ExitId()); |
| 676 | return break_block; |
| 677 | } |
| 678 | return loop_successor; |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 682 | void HBasicBlock::FinishExit(HControlInstruction* instruction) { |
| 683 | Finish(instruction); |
| 684 | ClearEnvironment(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 688 | HGraph::HGraph(CompilationInfo* info) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 689 | : isolate_(info->isolate()), |
| 690 | next_block_id_(0), |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 691 | entry_block_(NULL), |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 692 | blocks_(8, info->zone()), |
| 693 | values_(16, info->zone()), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 694 | phi_list_(NULL), |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 695 | info_(info), |
| 696 | zone_(info->zone()), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 697 | is_recursive_(false) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 698 | start_environment_ = |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 699 | new(zone_) HEnvironment(NULL, info->scope(), info->closure(), zone_); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 700 | start_environment_->set_ast_id(AstNode::kFunctionEntryId); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 701 | entry_block_ = CreateBasicBlock(); |
| 702 | entry_block_->SetInitialEnvironment(start_environment_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 706 | HBasicBlock* HGraph::CreateBasicBlock() { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 707 | HBasicBlock* result = new(zone()) HBasicBlock(this); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 708 | blocks_.Add(result, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 709 | return result; |
| 710 | } |
| 711 | |
| 712 | |
| 713 | void HGraph::Canonicalize() { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 714 | if (!FLAG_use_canonicalizing) return; |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 715 | HPhase phase("H_Canonicalize", this); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 716 | for (int i = 0; i < blocks()->length(); ++i) { |
| 717 | HInstruction* instr = blocks()->at(i)->first(); |
| 718 | while (instr != NULL) { |
| 719 | HValue* value = instr->Canonicalize(); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 720 | if (value != instr) instr->DeleteAndReplaceWith(value); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 721 | instr = instr->next(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 726 | // Block ordering was implemented with two mutually recursive methods, |
| 727 | // HGraph::Postorder and HGraph::PostorderLoopBlocks. |
| 728 | // The recursion could lead to stack overflow so the algorithm has been |
| 729 | // implemented iteratively. |
| 730 | // At a high level the algorithm looks like this: |
| 731 | // |
| 732 | // Postorder(block, loop_header) : { |
| 733 | // if (block has already been visited or is of another loop) return; |
| 734 | // mark block as visited; |
| 735 | // if (block is a loop header) { |
| 736 | // VisitLoopMembers(block, loop_header); |
| 737 | // VisitSuccessorsOfLoopHeader(block); |
| 738 | // } else { |
| 739 | // VisitSuccessors(block) |
| 740 | // } |
| 741 | // put block in result list; |
| 742 | // } |
| 743 | // |
| 744 | // VisitLoopMembers(block, outer_loop_header) { |
| 745 | // foreach (block b in block loop members) { |
| 746 | // VisitSuccessorsOfLoopMember(b, outer_loop_header); |
| 747 | // if (b is loop header) VisitLoopMembers(b); |
| 748 | // } |
| 749 | // } |
| 750 | // |
| 751 | // VisitSuccessorsOfLoopMember(block, outer_loop_header) { |
| 752 | // foreach (block b in block successors) Postorder(b, outer_loop_header) |
| 753 | // } |
| 754 | // |
| 755 | // VisitSuccessorsOfLoopHeader(block) { |
| 756 | // foreach (block b in block successors) Postorder(b, block) |
| 757 | // } |
| 758 | // |
| 759 | // VisitSuccessors(block, loop_header) { |
| 760 | // foreach (block b in block successors) Postorder(b, loop_header) |
| 761 | // } |
| 762 | // |
| 763 | // The ordering is started calling Postorder(entry, NULL). |
| 764 | // |
| 765 | // Each instance of PostorderProcessor represents the "stack frame" of the |
| 766 | // recursion, and particularly keeps the state of the loop (iteration) of the |
| 767 | // "Visit..." function it represents. |
| 768 | // To recycle memory we keep all the frames in a double linked list but |
| 769 | // this means that we cannot use constructors to initialize the frames. |
| 770 | // |
| 771 | class PostorderProcessor : public ZoneObject { |
| 772 | public: |
| 773 | // Back link (towards the stack bottom). |
| 774 | PostorderProcessor* parent() {return father_; } |
| 775 | // Forward link (towards the stack top). |
| 776 | PostorderProcessor* child() {return child_; } |
| 777 | HBasicBlock* block() { return block_; } |
| 778 | HLoopInformation* loop() { return loop_; } |
| 779 | HBasicBlock* loop_header() { return loop_header_; } |
| 780 | |
| 781 | static PostorderProcessor* CreateEntryProcessor(Zone* zone, |
| 782 | HBasicBlock* block, |
| 783 | BitVector* visited) { |
| 784 | PostorderProcessor* result = new(zone) PostorderProcessor(NULL); |
| 785 | return result->SetupSuccessors(zone, block, NULL, visited); |
| 786 | } |
| 787 | |
| 788 | PostorderProcessor* PerformStep(Zone* zone, |
| 789 | BitVector* visited, |
| 790 | ZoneList<HBasicBlock*>* order) { |
| 791 | PostorderProcessor* next = |
| 792 | PerformNonBacktrackingStep(zone, visited, order); |
| 793 | if (next != NULL) { |
| 794 | return next; |
| 795 | } else { |
| 796 | return Backtrack(zone, visited, order); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | private: |
| 801 | explicit PostorderProcessor(PostorderProcessor* father) |
| 802 | : father_(father), child_(NULL), successor_iterator(NULL) { } |
| 803 | |
| 804 | // Each enum value states the cycle whose state is kept by this instance. |
| 805 | enum LoopKind { |
| 806 | NONE, |
| 807 | SUCCESSORS, |
| 808 | SUCCESSORS_OF_LOOP_HEADER, |
| 809 | LOOP_MEMBERS, |
| 810 | SUCCESSORS_OF_LOOP_MEMBER |
| 811 | }; |
| 812 | |
| 813 | // Each "Setup..." method is like a constructor for a cycle state. |
| 814 | PostorderProcessor* SetupSuccessors(Zone* zone, |
| 815 | HBasicBlock* block, |
| 816 | HBasicBlock* loop_header, |
| 817 | BitVector* visited) { |
| 818 | if (block == NULL || visited->Contains(block->block_id()) || |
| 819 | block->parent_loop_header() != loop_header) { |
| 820 | kind_ = NONE; |
| 821 | block_ = NULL; |
| 822 | loop_ = NULL; |
| 823 | loop_header_ = NULL; |
| 824 | return this; |
| 825 | } else { |
| 826 | block_ = block; |
| 827 | loop_ = NULL; |
| 828 | visited->Add(block->block_id()); |
| 829 | |
| 830 | if (block->IsLoopHeader()) { |
| 831 | kind_ = SUCCESSORS_OF_LOOP_HEADER; |
| 832 | loop_header_ = block; |
| 833 | InitializeSuccessors(); |
| 834 | PostorderProcessor* result = Push(zone); |
| 835 | return result->SetupLoopMembers(zone, block, block->loop_information(), |
| 836 | loop_header); |
| 837 | } else { |
| 838 | ASSERT(block->IsFinished()); |
| 839 | kind_ = SUCCESSORS; |
| 840 | loop_header_ = loop_header; |
| 841 | InitializeSuccessors(); |
| 842 | return this; |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | PostorderProcessor* SetupLoopMembers(Zone* zone, |
| 848 | HBasicBlock* block, |
| 849 | HLoopInformation* loop, |
| 850 | HBasicBlock* loop_header) { |
| 851 | kind_ = LOOP_MEMBERS; |
| 852 | block_ = block; |
| 853 | loop_ = loop; |
| 854 | loop_header_ = loop_header; |
| 855 | InitializeLoopMembers(); |
| 856 | return this; |
| 857 | } |
| 858 | |
| 859 | PostorderProcessor* SetupSuccessorsOfLoopMember( |
| 860 | HBasicBlock* block, |
| 861 | HLoopInformation* loop, |
| 862 | HBasicBlock* loop_header) { |
| 863 | kind_ = SUCCESSORS_OF_LOOP_MEMBER; |
| 864 | block_ = block; |
| 865 | loop_ = loop; |
| 866 | loop_header_ = loop_header; |
| 867 | InitializeSuccessors(); |
| 868 | return this; |
| 869 | } |
| 870 | |
| 871 | // This method "allocates" a new stack frame. |
| 872 | PostorderProcessor* Push(Zone* zone) { |
| 873 | if (child_ == NULL) { |
| 874 | child_ = new(zone) PostorderProcessor(this); |
| 875 | } |
| 876 | return child_; |
| 877 | } |
| 878 | |
| 879 | void ClosePostorder(ZoneList<HBasicBlock*>* order, Zone* zone) { |
| 880 | ASSERT(block_->end()->FirstSuccessor() == NULL || |
| 881 | order->Contains(block_->end()->FirstSuccessor()) || |
| 882 | block_->end()->FirstSuccessor()->IsLoopHeader()); |
| 883 | ASSERT(block_->end()->SecondSuccessor() == NULL || |
| 884 | order->Contains(block_->end()->SecondSuccessor()) || |
| 885 | block_->end()->SecondSuccessor()->IsLoopHeader()); |
| 886 | order->Add(block_, zone); |
| 887 | } |
| 888 | |
| 889 | // This method is the basic block to walk up the stack. |
| 890 | PostorderProcessor* Pop(Zone* zone, |
| 891 | BitVector* visited, |
| 892 | ZoneList<HBasicBlock*>* order) { |
| 893 | switch (kind_) { |
| 894 | case SUCCESSORS: |
| 895 | case SUCCESSORS_OF_LOOP_HEADER: |
| 896 | ClosePostorder(order, zone); |
| 897 | return father_; |
| 898 | case LOOP_MEMBERS: |
| 899 | return father_; |
| 900 | case SUCCESSORS_OF_LOOP_MEMBER: |
| 901 | if (block()->IsLoopHeader() && block() != loop_->loop_header()) { |
| 902 | // In this case we need to perform a LOOP_MEMBERS cycle so we |
| 903 | // initialize it and return this instead of father. |
| 904 | return SetupLoopMembers(zone, block(), |
| 905 | block()->loop_information(), loop_header_); |
| 906 | } else { |
| 907 | return father_; |
| 908 | } |
| 909 | case NONE: |
| 910 | return father_; |
| 911 | } |
| 912 | UNREACHABLE(); |
| 913 | return NULL; |
| 914 | } |
| 915 | |
| 916 | // Walks up the stack. |
| 917 | PostorderProcessor* Backtrack(Zone* zone, |
| 918 | BitVector* visited, |
| 919 | ZoneList<HBasicBlock*>* order) { |
| 920 | PostorderProcessor* parent = Pop(zone, visited, order); |
| 921 | while (parent != NULL) { |
| 922 | PostorderProcessor* next = |
| 923 | parent->PerformNonBacktrackingStep(zone, visited, order); |
| 924 | if (next != NULL) { |
| 925 | return next; |
| 926 | } else { |
| 927 | parent = parent->Pop(zone, visited, order); |
| 928 | } |
| 929 | } |
| 930 | return NULL; |
| 931 | } |
| 932 | |
| 933 | PostorderProcessor* PerformNonBacktrackingStep( |
| 934 | Zone* zone, |
| 935 | BitVector* visited, |
| 936 | ZoneList<HBasicBlock*>* order) { |
| 937 | HBasicBlock* next_block; |
| 938 | switch (kind_) { |
| 939 | case SUCCESSORS: |
| 940 | next_block = AdvanceSuccessors(); |
| 941 | if (next_block != NULL) { |
| 942 | PostorderProcessor* result = Push(zone); |
| 943 | return result->SetupSuccessors(zone, next_block, |
| 944 | loop_header_, visited); |
| 945 | } |
| 946 | break; |
| 947 | case SUCCESSORS_OF_LOOP_HEADER: |
| 948 | next_block = AdvanceSuccessors(); |
| 949 | if (next_block != NULL) { |
| 950 | PostorderProcessor* result = Push(zone); |
| 951 | return result->SetupSuccessors(zone, next_block, |
| 952 | block(), visited); |
| 953 | } |
| 954 | break; |
| 955 | case LOOP_MEMBERS: |
| 956 | next_block = AdvanceLoopMembers(); |
| 957 | if (next_block != NULL) { |
| 958 | PostorderProcessor* result = Push(zone); |
| 959 | return result->SetupSuccessorsOfLoopMember(next_block, |
| 960 | loop_, loop_header_); |
| 961 | } |
| 962 | break; |
| 963 | case SUCCESSORS_OF_LOOP_MEMBER: |
| 964 | next_block = AdvanceSuccessors(); |
| 965 | if (next_block != NULL) { |
| 966 | PostorderProcessor* result = Push(zone); |
| 967 | return result->SetupSuccessors(zone, next_block, |
| 968 | loop_header_, visited); |
| 969 | } |
| 970 | break; |
| 971 | case NONE: |
| 972 | return NULL; |
| 973 | } |
| 974 | return NULL; |
| 975 | } |
| 976 | |
| 977 | // The following two methods implement a "foreach b in successors" cycle. |
| 978 | void InitializeSuccessors() { |
| 979 | loop_index = 0; |
| 980 | loop_length = 0; |
| 981 | successor_iterator = HSuccessorIterator(block_->end()); |
| 982 | } |
| 983 | |
| 984 | HBasicBlock* AdvanceSuccessors() { |
| 985 | if (!successor_iterator.Done()) { |
| 986 | HBasicBlock* result = successor_iterator.Current(); |
| 987 | successor_iterator.Advance(); |
| 988 | return result; |
| 989 | } |
| 990 | return NULL; |
| 991 | } |
| 992 | |
| 993 | // The following two methods implement a "foreach b in loop members" cycle. |
| 994 | void InitializeLoopMembers() { |
| 995 | loop_index = 0; |
| 996 | loop_length = loop_->blocks()->length(); |
| 997 | } |
| 998 | |
| 999 | HBasicBlock* AdvanceLoopMembers() { |
| 1000 | if (loop_index < loop_length) { |
| 1001 | HBasicBlock* result = loop_->blocks()->at(loop_index); |
| 1002 | loop_index++; |
| 1003 | return result; |
| 1004 | } else { |
| 1005 | return NULL; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | LoopKind kind_; |
| 1010 | PostorderProcessor* father_; |
| 1011 | PostorderProcessor* child_; |
| 1012 | HLoopInformation* loop_; |
| 1013 | HBasicBlock* block_; |
| 1014 | HBasicBlock* loop_header_; |
| 1015 | int loop_index; |
| 1016 | int loop_length; |
| 1017 | HSuccessorIterator successor_iterator; |
| 1018 | }; |
| 1019 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1020 | |
| 1021 | void HGraph::OrderBlocks() { |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 1022 | HPhase phase("H_Block ordering"); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 1023 | BitVector visited(blocks_.length(), zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1024 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1025 | ZoneList<HBasicBlock*> reverse_result(8, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1026 | HBasicBlock* start = blocks_[0]; |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1027 | PostorderProcessor* postorder = |
| 1028 | PostorderProcessor::CreateEntryProcessor(zone(), start, &visited); |
| 1029 | while (postorder != NULL) { |
| 1030 | postorder = postorder->PerformStep(zone(), &visited, &reverse_result); |
| 1031 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1032 | blocks_.Rewind(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1033 | int index = 0; |
| 1034 | for (int i = reverse_result.length() - 1; i >= 0; --i) { |
| 1035 | HBasicBlock* b = reverse_result[i]; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1036 | blocks_.Add(b, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1037 | b->set_block_id(index++); |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1042 | void HGraph::AssignDominators() { |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 1043 | HPhase phase("H_Assign dominators", this); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1044 | for (int i = 0; i < blocks_.length(); ++i) { |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1045 | HBasicBlock* block = blocks_[i]; |
| 1046 | if (block->IsLoopHeader()) { |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 1047 | // Only the first predecessor of a loop header is from outside the loop. |
| 1048 | // All others are back edges, and thus cannot dominate the loop header. |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1049 | block->AssignCommonDominator(block->predecessors()->first()); |
| 1050 | block->AssignLoopSuccessorDominators(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1051 | } else { |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1052 | for (int j = blocks_[i]->predecessors()->length() - 1; j >= 0; --j) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1053 | blocks_[i]->AssignCommonDominator(blocks_[i]->predecessors()->at(j)); |
| 1054 | } |
| 1055 | } |
| 1056 | } |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 1057 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1058 | |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 1059 | // Mark all blocks that are dominated by an unconditional soft deoptimize to |
| 1060 | // prevent code motion across those blocks. |
| 1061 | void HGraph::PropagateDeoptimizingMark() { |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 1062 | HPhase phase("H_Propagate deoptimizing mark", this); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1063 | MarkAsDeoptimizingRecursively(entry_block()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1066 | void HGraph::MarkAsDeoptimizingRecursively(HBasicBlock* block) { |
| 1067 | for (int i = 0; i < block->dominated_blocks()->length(); ++i) { |
| 1068 | HBasicBlock* dominated = block->dominated_blocks()->at(i); |
| 1069 | if (block->IsDeoptimizing()) dominated->MarkAsDeoptimizing(); |
| 1070 | MarkAsDeoptimizingRecursively(dominated); |
| 1071 | } |
| 1072 | } |
| 1073 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1074 | void HGraph::EliminateRedundantPhis() { |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 1075 | HPhase phase("H_Redundant phi elimination", this); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1076 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1077 | // Worklist of phis that can potentially be eliminated. Initialized with |
| 1078 | // all phi nodes. When elimination of a phi node modifies another phi node |
| 1079 | // the modified phi node is added to the worklist. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1080 | ZoneList<HPhi*> worklist(blocks_.length(), zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1081 | for (int i = 0; i < blocks_.length(); ++i) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1082 | worklist.AddAll(*blocks_[i]->phis(), zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | while (!worklist.is_empty()) { |
| 1086 | HPhi* phi = worklist.RemoveLast(); |
| 1087 | HBasicBlock* block = phi->block(); |
| 1088 | |
| 1089 | // Skip phi node if it was already replaced. |
| 1090 | if (block == NULL) continue; |
| 1091 | |
| 1092 | // Get replacement value if phi is redundant. |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1093 | HValue* replacement = phi->GetRedundantReplacement(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1094 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1095 | if (replacement != NULL) { |
| 1096 | // Iterate through the uses and replace them all. |
| 1097 | for (HUseIterator it(phi->uses()); !it.Done(); it.Advance()) { |
| 1098 | HValue* value = it.value(); |
| 1099 | value->SetOperandAt(it.index(), replacement); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1100 | if (value->IsPhi()) worklist.Add(HPhi::cast(value), zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1101 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1102 | block->RemovePhi(phi); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1103 | } |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | |
| 1108 | void HGraph::EliminateUnreachablePhis() { |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 1109 | HPhase phase("H_Unreachable phi elimination", this); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1110 | |
| 1111 | // Initialize worklist. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1112 | ZoneList<HPhi*> phi_list(blocks_.length(), zone()); |
| 1113 | ZoneList<HPhi*> worklist(blocks_.length(), zone()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1114 | for (int i = 0; i < blocks_.length(); ++i) { |
| 1115 | for (int j = 0; j < blocks_[i]->phis()->length(); j++) { |
| 1116 | HPhi* phi = blocks_[i]->phis()->at(j); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1117 | phi_list.Add(phi, zone()); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 1118 | // We can't eliminate phis in the receiver position in the environment |
| 1119 | // because in case of throwing an error we need this value to |
| 1120 | // construct a stack trace. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1121 | if (phi->HasRealUses() || phi->IsReceiver()) { |
| 1122 | phi->set_is_live(true); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1123 | worklist.Add(phi, zone()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | // Iteratively mark live phis. |
| 1129 | while (!worklist.is_empty()) { |
| 1130 | HPhi* phi = worklist.RemoveLast(); |
| 1131 | for (int i = 0; i < phi->OperandCount(); i++) { |
| 1132 | HValue* operand = phi->OperandAt(i); |
| 1133 | if (operand->IsPhi() && !HPhi::cast(operand)->is_live()) { |
| 1134 | HPhi::cast(operand)->set_is_live(true); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1135 | worklist.Add(HPhi::cast(operand), zone()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | // Remove unreachable phis. |
| 1141 | for (int i = 0; i < phi_list.length(); i++) { |
| 1142 | HPhi* phi = phi_list[i]; |
| 1143 | if (!phi->is_live()) { |
| 1144 | HBasicBlock* block = phi->block(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1145 | block->RemovePhi(phi); |
| 1146 | block->RecordDeletedPhi(phi->merged_index()); |
| 1147 | } |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1152 | bool HGraph::CheckArgumentsPhiUses() { |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 1153 | int block_count = blocks_.length(); |
| 1154 | for (int i = 0; i < block_count; ++i) { |
| 1155 | for (int j = 0; j < blocks_[i]->phis()->length(); ++j) { |
| 1156 | HPhi* phi = blocks_[i]->phis()->at(j); |
| 1157 | // We don't support phi uses of arguments for now. |
| 1158 | if (phi->CheckFlag(HValue::kIsArguments)) return false; |
| 1159 | } |
| 1160 | } |
| 1161 | return true; |
| 1162 | } |
| 1163 | |
| 1164 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1165 | bool HGraph::CheckConstPhiUses() { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1166 | int block_count = blocks_.length(); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1167 | for (int i = 0; i < block_count; ++i) { |
| 1168 | for (int j = 0; j < blocks_[i]->phis()->length(); ++j) { |
| 1169 | HPhi* phi = blocks_[i]->phis()->at(j); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1170 | // Check for the hole value (from an uninitialized const). |
| 1171 | for (int k = 0; k < phi->OperandCount(); k++) { |
| 1172 | if (phi->OperandAt(k) == GetConstantHole()) return false; |
| 1173 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | return true; |
| 1177 | } |
| 1178 | |
| 1179 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1180 | void HGraph::CollectPhis() { |
| 1181 | int block_count = blocks_.length(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1182 | phi_list_ = new(zone()) ZoneList<HPhi*>(block_count, zone()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1183 | for (int i = 0; i < block_count; ++i) { |
| 1184 | for (int j = 0; j < blocks_[i]->phis()->length(); ++j) { |
| 1185 | HPhi* phi = blocks_[i]->phis()->at(j); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1186 | phi_list_->Add(phi, zone()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1192 | void HGraph::InferTypes(ZoneList<HValue*>* worklist) { |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 1193 | BitVector in_worklist(GetMaximumValueID(), zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1194 | for (int i = 0; i < worklist->length(); ++i) { |
| 1195 | ASSERT(!in_worklist.Contains(worklist->at(i)->id())); |
| 1196 | in_worklist.Add(worklist->at(i)->id()); |
| 1197 | } |
| 1198 | |
| 1199 | while (!worklist->is_empty()) { |
| 1200 | HValue* current = worklist->RemoveLast(); |
| 1201 | in_worklist.Remove(current->id()); |
| 1202 | if (current->UpdateInferredType()) { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1203 | for (HUseIterator it(current->uses()); !it.Done(); it.Advance()) { |
| 1204 | HValue* use = it.value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1205 | if (!in_worklist.Contains(use->id())) { |
| 1206 | in_worklist.Add(use->id()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1207 | worklist->Add(use, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1208 | } |
| 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | |
| 1215 | class HRangeAnalysis BASE_EMBEDDED { |
| 1216 | public: |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1217 | explicit HRangeAnalysis(HGraph* graph) : |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1218 | graph_(graph), zone_(graph->zone()), changed_ranges_(16, zone_) { } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1219 | |
| 1220 | void Analyze(); |
| 1221 | |
| 1222 | private: |
| 1223 | void TraceRange(const char* msg, ...); |
| 1224 | void Analyze(HBasicBlock* block); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1225 | void InferControlFlowRange(HCompareIDAndBranch* test, HBasicBlock* dest); |
| 1226 | void UpdateControlFlowRange(Token::Value op, HValue* value, HValue* other); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1227 | void InferRange(HValue* value); |
| 1228 | void RollBackTo(int index); |
| 1229 | void AddRange(HValue* value, Range* range); |
| 1230 | |
| 1231 | HGraph* graph_; |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1232 | Zone* zone_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1233 | ZoneList<HValue*> changed_ranges_; |
| 1234 | }; |
| 1235 | |
| 1236 | |
| 1237 | void HRangeAnalysis::TraceRange(const char* msg, ...) { |
| 1238 | if (FLAG_trace_range) { |
| 1239 | va_list arguments; |
| 1240 | va_start(arguments, msg); |
| 1241 | OS::VPrint(msg, arguments); |
| 1242 | va_end(arguments); |
| 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | |
| 1247 | void HRangeAnalysis::Analyze() { |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 1248 | HPhase phase("H_Range analysis", graph_); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1249 | Analyze(graph_->entry_block()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | |
| 1253 | void HRangeAnalysis::Analyze(HBasicBlock* block) { |
| 1254 | TraceRange("Analyzing block B%d\n", block->block_id()); |
| 1255 | |
| 1256 | int last_changed_range = changed_ranges_.length() - 1; |
| 1257 | |
| 1258 | // Infer range based on control flow. |
| 1259 | if (block->predecessors()->length() == 1) { |
| 1260 | HBasicBlock* pred = block->predecessors()->first(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1261 | if (pred->end()->IsCompareIDAndBranch()) { |
| 1262 | InferControlFlowRange(HCompareIDAndBranch::cast(pred->end()), block); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | // Process phi instructions. |
| 1267 | for (int i = 0; i < block->phis()->length(); ++i) { |
| 1268 | HPhi* phi = block->phis()->at(i); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1269 | InferRange(phi); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | // Go through all instructions of the current block. |
| 1273 | HInstruction* instr = block->first(); |
| 1274 | while (instr != block->end()) { |
| 1275 | InferRange(instr); |
| 1276 | instr = instr->next(); |
| 1277 | } |
| 1278 | |
| 1279 | // Continue analysis in all dominated blocks. |
| 1280 | for (int i = 0; i < block->dominated_blocks()->length(); ++i) { |
| 1281 | Analyze(block->dominated_blocks()->at(i)); |
| 1282 | } |
| 1283 | |
| 1284 | RollBackTo(last_changed_range); |
| 1285 | } |
| 1286 | |
| 1287 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1288 | void HRangeAnalysis::InferControlFlowRange(HCompareIDAndBranch* test, |
| 1289 | HBasicBlock* dest) { |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1290 | ASSERT((test->FirstSuccessor() == dest) == (test->SecondSuccessor() != dest)); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1291 | if (test->GetInputRepresentation().IsInteger32()) { |
| 1292 | Token::Value op = test->token(); |
| 1293 | if (test->SecondSuccessor() == dest) { |
| 1294 | op = Token::NegateCompareOp(op); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1295 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1296 | Token::Value inverted_op = Token::InvertCompareOp(op); |
| 1297 | UpdateControlFlowRange(op, test->left(), test->right()); |
| 1298 | UpdateControlFlowRange(inverted_op, test->right(), test->left()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | |
| 1303 | // We know that value [op] other. Use this information to update the range on |
| 1304 | // value. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1305 | void HRangeAnalysis::UpdateControlFlowRange(Token::Value op, |
| 1306 | HValue* value, |
| 1307 | HValue* other) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1308 | Range temp_range; |
| 1309 | Range* range = other->range() != NULL ? other->range() : &temp_range; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1310 | Range* new_range = NULL; |
| 1311 | |
| 1312 | TraceRange("Control flow range infer %d %s %d\n", |
| 1313 | value->id(), |
| 1314 | Token::Name(op), |
| 1315 | other->id()); |
| 1316 | |
| 1317 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
| 1318 | // The same range has to apply for value. |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1319 | new_range = range->Copy(zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1320 | } else if (op == Token::LT || op == Token::LTE) { |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1321 | new_range = range->CopyClearLower(zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1322 | if (op == Token::LT) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1323 | new_range->AddConstant(-1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1324 | } |
| 1325 | } else if (op == Token::GT || op == Token::GTE) { |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1326 | new_range = range->CopyClearUpper(zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1327 | if (op == Token::GT) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1328 | new_range->AddConstant(1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | if (new_range != NULL && !new_range->IsMostGeneric()) { |
| 1333 | AddRange(value, new_range); |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1338 | void HRangeAnalysis::InferRange(HValue* value) { |
| 1339 | ASSERT(!value->HasRange()); |
| 1340 | if (!value->representation().IsNone()) { |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1341 | value->ComputeInitialRange(zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1342 | Range* range = value->range(); |
| 1343 | TraceRange("Initial inferred range of %d (%s) set to [%d,%d]\n", |
| 1344 | value->id(), |
| 1345 | value->Mnemonic(), |
| 1346 | range->lower(), |
| 1347 | range->upper()); |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | |
| 1352 | void HRangeAnalysis::RollBackTo(int index) { |
| 1353 | for (int i = index + 1; i < changed_ranges_.length(); ++i) { |
| 1354 | changed_ranges_[i]->RemoveLastAddedRange(); |
| 1355 | } |
| 1356 | changed_ranges_.Rewind(index + 1); |
| 1357 | } |
| 1358 | |
| 1359 | |
| 1360 | void HRangeAnalysis::AddRange(HValue* value, Range* range) { |
| 1361 | Range* original_range = value->range(); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1362 | value->AddNewRange(range, zone_); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1363 | changed_ranges_.Add(value, zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1364 | Range* new_range = value->range(); |
| 1365 | TraceRange("Updated range of %d set to [%d,%d]\n", |
| 1366 | value->id(), |
| 1367 | new_range->lower(), |
| 1368 | new_range->upper()); |
| 1369 | if (original_range != NULL) { |
| 1370 | TraceRange("Original range was [%d,%d]\n", |
| 1371 | original_range->lower(), |
| 1372 | original_range->upper()); |
| 1373 | } |
| 1374 | TraceRange("New information was [%d,%d]\n", |
| 1375 | range->lower(), |
| 1376 | range->upper()); |
| 1377 | } |
| 1378 | |
| 1379 | |
| 1380 | void TraceGVN(const char* msg, ...) { |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 1381 | va_list arguments; |
| 1382 | va_start(arguments, msg); |
| 1383 | OS::VPrint(msg, arguments); |
| 1384 | va_end(arguments); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 1387 | // Wrap TraceGVN in macros to avoid the expense of evaluating its arguments when |
| 1388 | // --trace-gvn is off. |
| 1389 | #define TRACE_GVN_1(msg, a1) \ |
| 1390 | if (FLAG_trace_gvn) { \ |
| 1391 | TraceGVN(msg, a1); \ |
| 1392 | } |
| 1393 | |
| 1394 | #define TRACE_GVN_2(msg, a1, a2) \ |
| 1395 | if (FLAG_trace_gvn) { \ |
| 1396 | TraceGVN(msg, a1, a2); \ |
| 1397 | } |
| 1398 | |
| 1399 | #define TRACE_GVN_3(msg, a1, a2, a3) \ |
| 1400 | if (FLAG_trace_gvn) { \ |
| 1401 | TraceGVN(msg, a1, a2, a3); \ |
| 1402 | } |
| 1403 | |
| 1404 | #define TRACE_GVN_4(msg, a1, a2, a3, a4) \ |
| 1405 | if (FLAG_trace_gvn) { \ |
| 1406 | TraceGVN(msg, a1, a2, a3, a4); \ |
| 1407 | } |
| 1408 | |
| 1409 | #define TRACE_GVN_5(msg, a1, a2, a3, a4, a5) \ |
| 1410 | if (FLAG_trace_gvn) { \ |
| 1411 | TraceGVN(msg, a1, a2, a3, a4, a5); \ |
| 1412 | } |
| 1413 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1414 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1415 | HValueMap::HValueMap(Zone* zone, const HValueMap* other) |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1416 | : array_size_(other->array_size_), |
| 1417 | lists_size_(other->lists_size_), |
| 1418 | count_(other->count_), |
| 1419 | present_flags_(other->present_flags_), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1420 | array_(zone->NewArray<HValueMapListElement>(other->array_size_)), |
| 1421 | lists_(zone->NewArray<HValueMapListElement>(other->lists_size_)), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1422 | free_list_head_(other->free_list_head_) { |
| 1423 | memcpy(array_, other->array_, array_size_ * sizeof(HValueMapListElement)); |
| 1424 | memcpy(lists_, other->lists_, lists_size_ * sizeof(HValueMapListElement)); |
| 1425 | } |
| 1426 | |
| 1427 | |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1428 | void HValueMap::Kill(GVNFlagSet flags) { |
| 1429 | GVNFlagSet depends_flags = HValue::ConvertChangesToDependsFlags(flags); |
| 1430 | if (!present_flags_.ContainsAnyOf(depends_flags)) return; |
| 1431 | present_flags_.RemoveAll(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1432 | for (int i = 0; i < array_size_; ++i) { |
| 1433 | HValue* value = array_[i].value; |
| 1434 | if (value != NULL) { |
| 1435 | // Clear list of collisions first, so we know if it becomes empty. |
| 1436 | int kept = kNil; // List of kept elements. |
| 1437 | int next; |
| 1438 | for (int current = array_[i].next; current != kNil; current = next) { |
| 1439 | next = lists_[current].next; |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1440 | HValue* value = lists_[current].value; |
| 1441 | if (value->gvn_flags().ContainsAnyOf(depends_flags)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1442 | // Drop it. |
| 1443 | count_--; |
| 1444 | lists_[current].next = free_list_head_; |
| 1445 | free_list_head_ = current; |
| 1446 | } else { |
| 1447 | // Keep it. |
| 1448 | lists_[current].next = kept; |
| 1449 | kept = current; |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1450 | present_flags_.Add(value->gvn_flags()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1451 | } |
| 1452 | } |
| 1453 | array_[i].next = kept; |
| 1454 | |
| 1455 | // Now possibly drop directly indexed element. |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1456 | value = array_[i].value; |
| 1457 | if (value->gvn_flags().ContainsAnyOf(depends_flags)) { // Drop it. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1458 | count_--; |
| 1459 | int head = array_[i].next; |
| 1460 | if (head == kNil) { |
| 1461 | array_[i].value = NULL; |
| 1462 | } else { |
| 1463 | array_[i].value = lists_[head].value; |
| 1464 | array_[i].next = lists_[head].next; |
| 1465 | lists_[head].next = free_list_head_; |
| 1466 | free_list_head_ = head; |
| 1467 | } |
| 1468 | } else { |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1469 | present_flags_.Add(value->gvn_flags()); // Keep it. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1470 | } |
| 1471 | } |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | |
| 1476 | HValue* HValueMap::Lookup(HValue* value) const { |
| 1477 | uint32_t hash = static_cast<uint32_t>(value->Hashcode()); |
| 1478 | uint32_t pos = Bound(hash); |
| 1479 | if (array_[pos].value != NULL) { |
| 1480 | if (array_[pos].value->Equals(value)) return array_[pos].value; |
| 1481 | int next = array_[pos].next; |
| 1482 | while (next != kNil) { |
| 1483 | if (lists_[next].value->Equals(value)) return lists_[next].value; |
| 1484 | next = lists_[next].next; |
| 1485 | } |
| 1486 | } |
| 1487 | return NULL; |
| 1488 | } |
| 1489 | |
| 1490 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1491 | void HValueMap::Resize(int new_size, Zone* zone) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1492 | ASSERT(new_size > count_); |
| 1493 | // Hashing the values into the new array has no more collisions than in the |
| 1494 | // old hash map, so we can use the existing lists_ array, if we are careful. |
| 1495 | |
| 1496 | // Make sure we have at least one free element. |
| 1497 | if (free_list_head_ == kNil) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1498 | ResizeLists(lists_size_ << 1, zone); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | HValueMapListElement* new_array = |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1502 | zone->NewArray<HValueMapListElement>(new_size); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1503 | memset(new_array, 0, sizeof(HValueMapListElement) * new_size); |
| 1504 | |
| 1505 | HValueMapListElement* old_array = array_; |
| 1506 | int old_size = array_size_; |
| 1507 | |
| 1508 | int old_count = count_; |
| 1509 | count_ = 0; |
| 1510 | // Do not modify present_flags_. It is currently correct. |
| 1511 | array_size_ = new_size; |
| 1512 | array_ = new_array; |
| 1513 | |
| 1514 | if (old_array != NULL) { |
| 1515 | // Iterate over all the elements in lists, rehashing them. |
| 1516 | for (int i = 0; i < old_size; ++i) { |
| 1517 | if (old_array[i].value != NULL) { |
| 1518 | int current = old_array[i].next; |
| 1519 | while (current != kNil) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1520 | Insert(lists_[current].value, zone); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1521 | int next = lists_[current].next; |
| 1522 | lists_[current].next = free_list_head_; |
| 1523 | free_list_head_ = current; |
| 1524 | current = next; |
| 1525 | } |
| 1526 | // Rehash the directly stored value. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1527 | Insert(old_array[i].value, zone); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1528 | } |
| 1529 | } |
| 1530 | } |
| 1531 | USE(old_count); |
| 1532 | ASSERT(count_ == old_count); |
| 1533 | } |
| 1534 | |
| 1535 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1536 | void HValueMap::ResizeLists(int new_size, Zone* zone) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1537 | ASSERT(new_size > lists_size_); |
| 1538 | |
| 1539 | HValueMapListElement* new_lists = |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1540 | zone->NewArray<HValueMapListElement>(new_size); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1541 | memset(new_lists, 0, sizeof(HValueMapListElement) * new_size); |
| 1542 | |
| 1543 | HValueMapListElement* old_lists = lists_; |
| 1544 | int old_size = lists_size_; |
| 1545 | |
| 1546 | lists_size_ = new_size; |
| 1547 | lists_ = new_lists; |
| 1548 | |
| 1549 | if (old_lists != NULL) { |
| 1550 | memcpy(lists_, old_lists, old_size * sizeof(HValueMapListElement)); |
| 1551 | } |
| 1552 | for (int i = old_size; i < lists_size_; ++i) { |
| 1553 | lists_[i].next = free_list_head_; |
| 1554 | free_list_head_ = i; |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1559 | void HValueMap::Insert(HValue* value, Zone* zone) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1560 | ASSERT(value != NULL); |
| 1561 | // Resizing when half of the hashtable is filled up. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1562 | if (count_ >= array_size_ >> 1) Resize(array_size_ << 1, zone); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1563 | ASSERT(count_ < array_size_); |
| 1564 | count_++; |
| 1565 | uint32_t pos = Bound(static_cast<uint32_t>(value->Hashcode())); |
| 1566 | if (array_[pos].value == NULL) { |
| 1567 | array_[pos].value = value; |
| 1568 | array_[pos].next = kNil; |
| 1569 | } else { |
| 1570 | if (free_list_head_ == kNil) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1571 | ResizeLists(lists_size_ << 1, zone); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1572 | } |
| 1573 | int new_element_pos = free_list_head_; |
| 1574 | ASSERT(new_element_pos != kNil); |
| 1575 | free_list_head_ = lists_[free_list_head_].next; |
| 1576 | lists_[new_element_pos].value = value; |
| 1577 | lists_[new_element_pos].next = array_[pos].next; |
| 1578 | ASSERT(array_[pos].next == kNil || lists_[array_[pos].next].value != NULL); |
| 1579 | array_[pos].next = new_element_pos; |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1584 | HSideEffectMap::HSideEffectMap() : count_(0) { |
| 1585 | memset(data_, 0, kNumberOfTrackedSideEffects * kPointerSize); |
| 1586 | } |
| 1587 | |
| 1588 | |
| 1589 | HSideEffectMap::HSideEffectMap(HSideEffectMap* other) : count_(other->count_) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1590 | *this = *other; // Calls operator=. |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1594 | HSideEffectMap& HSideEffectMap::operator= (const HSideEffectMap& other) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1595 | if (this != &other) { |
| 1596 | memcpy(data_, other.data_, kNumberOfTrackedSideEffects * kPointerSize); |
| 1597 | } |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1598 | return *this; |
| 1599 | } |
| 1600 | |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1601 | void HSideEffectMap::Kill(GVNFlagSet flags) { |
| 1602 | for (int i = 0; i < kNumberOfTrackedSideEffects; i++) { |
| 1603 | GVNFlag changes_flag = HValue::ChangesFlagFromInt(i); |
| 1604 | if (flags.Contains(changes_flag)) { |
| 1605 | if (data_[i] != NULL) count_--; |
| 1606 | data_[i] = NULL; |
| 1607 | } |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | |
| 1612 | void HSideEffectMap::Store(GVNFlagSet flags, HInstruction* instr) { |
| 1613 | for (int i = 0; i < kNumberOfTrackedSideEffects; i++) { |
| 1614 | GVNFlag changes_flag = HValue::ChangesFlagFromInt(i); |
| 1615 | if (flags.Contains(changes_flag)) { |
| 1616 | if (data_[i] == NULL) count_++; |
| 1617 | data_[i] = instr; |
| 1618 | } |
| 1619 | } |
| 1620 | } |
| 1621 | |
| 1622 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1623 | class HStackCheckEliminator BASE_EMBEDDED { |
| 1624 | public: |
| 1625 | explicit HStackCheckEliminator(HGraph* graph) : graph_(graph) { } |
| 1626 | |
| 1627 | void Process(); |
| 1628 | |
| 1629 | private: |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1630 | HGraph* graph_; |
| 1631 | }; |
| 1632 | |
| 1633 | |
| 1634 | void HStackCheckEliminator::Process() { |
| 1635 | // For each loop block walk the dominator tree from the backwards branch to |
| 1636 | // the loop header. If a call instruction is encountered the backwards branch |
| 1637 | // is dominated by a call and the stack check in the backwards branch can be |
| 1638 | // removed. |
| 1639 | for (int i = 0; i < graph_->blocks()->length(); i++) { |
| 1640 | HBasicBlock* block = graph_->blocks()->at(i); |
| 1641 | if (block->IsLoopHeader()) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1642 | HBasicBlock* back_edge = block->loop_information()->GetLastBackEdge(); |
| 1643 | HBasicBlock* dominator = back_edge; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1644 | while (true) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1645 | HInstruction* instr = dominator->first(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1646 | while (instr != NULL) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1647 | if (instr->IsCall()) { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1648 | block->loop_information()->stack_check()->Eliminate(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1649 | break; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1650 | } |
| 1651 | instr = instr->next(); |
| 1652 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1653 | |
| 1654 | // Done when the loop header is processed. |
| 1655 | if (dominator == block) break; |
| 1656 | |
| 1657 | // Move up the dominator tree. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1658 | dominator = dominator->dominator(); |
| 1659 | } |
| 1660 | } |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1665 | // Simple sparse set with O(1) add, contains, and clear. |
| 1666 | class SparseSet { |
| 1667 | public: |
| 1668 | SparseSet(Zone* zone, int capacity) |
| 1669 | : capacity_(capacity), |
| 1670 | length_(0), |
| 1671 | dense_(zone->NewArray<int>(capacity)), |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1672 | sparse_(zone->NewArray<int>(capacity)) { |
| 1673 | #ifndef NVALGRIND |
| 1674 | // Initialize the sparse array to make valgrind happy. |
| 1675 | memset(sparse_, 0, sizeof(sparse_[0]) * capacity); |
| 1676 | #endif |
| 1677 | } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1678 | |
| 1679 | bool Contains(int n) const { |
| 1680 | ASSERT(0 <= n && n < capacity_); |
| 1681 | int d = sparse_[n]; |
| 1682 | return 0 <= d && d < length_ && dense_[d] == n; |
| 1683 | } |
| 1684 | |
| 1685 | bool Add(int n) { |
| 1686 | if (Contains(n)) return false; |
| 1687 | dense_[length_] = n; |
| 1688 | sparse_[n] = length_; |
| 1689 | ++length_; |
| 1690 | return true; |
| 1691 | } |
| 1692 | |
| 1693 | void Clear() { length_ = 0; } |
| 1694 | |
| 1695 | private: |
| 1696 | int capacity_; |
| 1697 | int length_; |
| 1698 | int* dense_; |
| 1699 | int* sparse_; |
| 1700 | |
| 1701 | DISALLOW_COPY_AND_ASSIGN(SparseSet); |
| 1702 | }; |
| 1703 | |
| 1704 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1705 | class HGlobalValueNumberer BASE_EMBEDDED { |
| 1706 | public: |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1707 | explicit HGlobalValueNumberer(HGraph* graph, CompilationInfo* info) |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1708 | : graph_(graph), |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1709 | info_(info), |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1710 | removed_side_effects_(false), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1711 | block_side_effects_(graph->blocks()->length(), graph->zone()), |
| 1712 | loop_side_effects_(graph->blocks()->length(), graph->zone()), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1713 | visited_on_paths_(graph->zone(), graph->blocks()->length()) { |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 1714 | ASSERT(!info->isolate()->heap()->IsAllocationAllowed()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1715 | block_side_effects_.AddBlock(GVNFlagSet(), graph_->blocks()->length(), |
| 1716 | graph_->zone()); |
| 1717 | loop_side_effects_.AddBlock(GVNFlagSet(), graph_->blocks()->length(), |
| 1718 | graph_->zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1719 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1720 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1721 | // Returns true if values with side effects are removed. |
| 1722 | bool Analyze(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1723 | |
| 1724 | private: |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1725 | GVNFlagSet CollectSideEffectsOnPathsToDominatedBlock( |
| 1726 | HBasicBlock* dominator, |
| 1727 | HBasicBlock* dominated); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1728 | void AnalyzeGraph(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1729 | void ComputeBlockSideEffects(); |
| 1730 | void LoopInvariantCodeMotion(); |
| 1731 | void ProcessLoopBlock(HBasicBlock* block, |
| 1732 | HBasicBlock* before_loop, |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1733 | GVNFlagSet loop_kills, |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1734 | GVNFlagSet* accumulated_first_time_depends, |
| 1735 | GVNFlagSet* accumulated_first_time_changes); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1736 | bool AllowCodeMotion(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1737 | bool ShouldMove(HInstruction* instr, HBasicBlock* loop_header); |
| 1738 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1739 | HGraph* graph() { return graph_; } |
| 1740 | CompilationInfo* info() { return info_; } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1741 | Zone* zone() const { return graph_->zone(); } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1742 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1743 | HGraph* graph_; |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1744 | CompilationInfo* info_; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1745 | bool removed_side_effects_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1746 | |
| 1747 | // A map of block IDs to their side effects. |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1748 | ZoneList<GVNFlagSet> block_side_effects_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1749 | |
| 1750 | // 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] | 1751 | ZoneList<GVNFlagSet> loop_side_effects_; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1752 | |
| 1753 | // Used when collecting side effects on paths from dominator to |
| 1754 | // dominated. |
| 1755 | SparseSet visited_on_paths_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1756 | }; |
| 1757 | |
| 1758 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1759 | bool HGlobalValueNumberer::Analyze() { |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1760 | removed_side_effects_ = false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1761 | ComputeBlockSideEffects(); |
| 1762 | if (FLAG_loop_invariant_code_motion) { |
| 1763 | LoopInvariantCodeMotion(); |
| 1764 | } |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1765 | AnalyzeGraph(); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1766 | return removed_side_effects_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | |
| 1770 | void HGlobalValueNumberer::ComputeBlockSideEffects() { |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1771 | // The Analyze phase of GVN can be called multiple times. Clear loop side |
| 1772 | // effects before computing them to erase the contents from previous Analyze |
| 1773 | // passes. |
| 1774 | for (int i = 0; i < loop_side_effects_.length(); ++i) { |
| 1775 | loop_side_effects_[i].RemoveAll(); |
| 1776 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1777 | for (int i = graph_->blocks()->length() - 1; i >= 0; --i) { |
| 1778 | // Compute side effects for the block. |
| 1779 | HBasicBlock* block = graph_->blocks()->at(i); |
| 1780 | HInstruction* instr = block->first(); |
| 1781 | int id = block->block_id(); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1782 | GVNFlagSet side_effects; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1783 | while (instr != NULL) { |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1784 | side_effects.Add(instr->ChangesFlags()); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1785 | if (instr->IsSoftDeoptimize()) { |
| 1786 | block_side_effects_[id].RemoveAll(); |
| 1787 | side_effects.RemoveAll(); |
| 1788 | break; |
| 1789 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1790 | instr = instr->next(); |
| 1791 | } |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1792 | block_side_effects_[id].Add(side_effects); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1793 | |
| 1794 | // Loop headers are part of their loop. |
| 1795 | if (block->IsLoopHeader()) { |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1796 | loop_side_effects_[id].Add(side_effects); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | // Propagate loop side effects upwards. |
| 1800 | if (block->HasParentLoopHeader()) { |
| 1801 | int header_id = block->parent_loop_header()->block_id(); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1802 | loop_side_effects_[header_id].Add(block->IsLoopHeader() |
| 1803 | ? loop_side_effects_[id] |
| 1804 | : side_effects); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1805 | } |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 1810 | SmartArrayPointer<char> GetGVNFlagsString(GVNFlagSet flags) { |
| 1811 | char underlying_buffer[kLastFlag * 128]; |
| 1812 | Vector<char> buffer(underlying_buffer, sizeof(underlying_buffer)); |
| 1813 | #if DEBUG |
| 1814 | int offset = 0; |
| 1815 | const char* separator = ""; |
| 1816 | const char* comma = ", "; |
| 1817 | buffer[0] = 0; |
| 1818 | uint32_t set_depends_on = 0; |
| 1819 | uint32_t set_changes = 0; |
| 1820 | for (int bit = 0; bit < kLastFlag; ++bit) { |
| 1821 | if ((flags.ToIntegral() & (1 << bit)) != 0) { |
| 1822 | if (bit % 2 == 0) { |
| 1823 | set_changes++; |
| 1824 | } else { |
| 1825 | set_depends_on++; |
| 1826 | } |
| 1827 | } |
| 1828 | } |
| 1829 | bool positive_changes = set_changes < (kLastFlag / 2); |
| 1830 | bool positive_depends_on = set_depends_on < (kLastFlag / 2); |
| 1831 | if (set_changes > 0) { |
| 1832 | if (positive_changes) { |
| 1833 | offset += OS::SNPrintF(buffer + offset, "changes ["); |
| 1834 | } else { |
| 1835 | offset += OS::SNPrintF(buffer + offset, "changes all except ["); |
| 1836 | } |
| 1837 | for (int bit = 0; bit < kLastFlag; ++bit) { |
| 1838 | if (((flags.ToIntegral() & (1 << bit)) != 0) == positive_changes) { |
| 1839 | switch (static_cast<GVNFlag>(bit)) { |
| 1840 | #define DECLARE_FLAG(type) \ |
| 1841 | case kChanges##type: \ |
| 1842 | offset += OS::SNPrintF(buffer + offset, separator); \ |
| 1843 | offset += OS::SNPrintF(buffer + offset, #type); \ |
| 1844 | separator = comma; \ |
| 1845 | break; |
| 1846 | GVN_TRACKED_FLAG_LIST(DECLARE_FLAG) |
| 1847 | GVN_UNTRACKED_FLAG_LIST(DECLARE_FLAG) |
| 1848 | #undef DECLARE_FLAG |
| 1849 | default: |
| 1850 | break; |
| 1851 | } |
| 1852 | } |
| 1853 | } |
| 1854 | offset += OS::SNPrintF(buffer + offset, "]"); |
| 1855 | } |
| 1856 | if (set_depends_on > 0) { |
| 1857 | separator = ""; |
| 1858 | if (set_changes > 0) { |
| 1859 | offset += OS::SNPrintF(buffer + offset, ", "); |
| 1860 | } |
| 1861 | if (positive_depends_on) { |
| 1862 | offset += OS::SNPrintF(buffer + offset, "depends on ["); |
| 1863 | } else { |
| 1864 | offset += OS::SNPrintF(buffer + offset, "depends on all except ["); |
| 1865 | } |
| 1866 | for (int bit = 0; bit < kLastFlag; ++bit) { |
| 1867 | if (((flags.ToIntegral() & (1 << bit)) != 0) == positive_depends_on) { |
| 1868 | switch (static_cast<GVNFlag>(bit)) { |
| 1869 | #define DECLARE_FLAG(type) \ |
| 1870 | case kDependsOn##type: \ |
| 1871 | offset += OS::SNPrintF(buffer + offset, separator); \ |
| 1872 | offset += OS::SNPrintF(buffer + offset, #type); \ |
| 1873 | separator = comma; \ |
| 1874 | break; |
| 1875 | GVN_TRACKED_FLAG_LIST(DECLARE_FLAG) |
| 1876 | GVN_UNTRACKED_FLAG_LIST(DECLARE_FLAG) |
| 1877 | #undef DECLARE_FLAG |
| 1878 | default: |
| 1879 | break; |
| 1880 | } |
| 1881 | } |
| 1882 | } |
| 1883 | offset += OS::SNPrintF(buffer + offset, "]"); |
| 1884 | } |
| 1885 | #else |
| 1886 | OS::SNPrintF(buffer, "0x%08X", flags.ToIntegral()); |
| 1887 | #endif |
| 1888 | size_t string_len = strlen(underlying_buffer) + 1; |
| 1889 | ASSERT(string_len <= sizeof(underlying_buffer)); |
| 1890 | char* result = new char[strlen(underlying_buffer) + 1]; |
| 1891 | memcpy(result, underlying_buffer, string_len); |
| 1892 | return SmartArrayPointer<char>(result); |
| 1893 | } |
| 1894 | |
| 1895 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1896 | void HGlobalValueNumberer::LoopInvariantCodeMotion() { |
| 1897 | for (int i = graph_->blocks()->length() - 1; i >= 0; --i) { |
| 1898 | HBasicBlock* block = graph_->blocks()->at(i); |
| 1899 | if (block->IsLoopHeader()) { |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1900 | GVNFlagSet side_effects = loop_side_effects_[block->block_id()]; |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 1901 | TRACE_GVN_2("Try loop invariant motion for block B%d %s\n", |
| 1902 | block->block_id(), |
| 1903 | *GetGVNFlagsString(side_effects)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1904 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1905 | GVNFlagSet accumulated_first_time_depends; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1906 | GVNFlagSet accumulated_first_time_changes; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1907 | HBasicBlock* last = block->loop_information()->GetLastBackEdge(); |
| 1908 | for (int j = block->block_id(); j <= last->block_id(); ++j) { |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1909 | ProcessLoopBlock(graph_->blocks()->at(j), block, side_effects, |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1910 | &accumulated_first_time_depends, |
| 1911 | &accumulated_first_time_changes); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1912 | } |
| 1913 | } |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1918 | void HGlobalValueNumberer::ProcessLoopBlock( |
| 1919 | HBasicBlock* block, |
| 1920 | HBasicBlock* loop_header, |
| 1921 | GVNFlagSet loop_kills, |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1922 | GVNFlagSet* first_time_depends, |
| 1923 | GVNFlagSet* first_time_changes) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1924 | HBasicBlock* pre_header = loop_header->predecessors()->at(0); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1925 | GVNFlagSet depends_flags = HValue::ConvertChangesToDependsFlags(loop_kills); |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 1926 | TRACE_GVN_2("Loop invariant motion for B%d %s\n", |
| 1927 | block->block_id(), |
| 1928 | *GetGVNFlagsString(depends_flags)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1929 | HInstruction* instr = block->first(); |
| 1930 | while (instr != NULL) { |
| 1931 | HInstruction* next = instr->next(); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1932 | bool hoisted = false; |
| 1933 | if (instr->CheckFlag(HValue::kUseGVN)) { |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 1934 | TRACE_GVN_4("Checking instruction %d (%s) %s. Loop %s\n", |
| 1935 | instr->id(), |
| 1936 | instr->Mnemonic(), |
| 1937 | *GetGVNFlagsString(instr->gvn_flags()), |
| 1938 | *GetGVNFlagsString(loop_kills)); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1939 | bool can_hoist = !instr->gvn_flags().ContainsAnyOf(depends_flags); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1940 | if (instr->IsTransitionElementsKind()) { |
| 1941 | // It's possible to hoist transitions out of a loop as long as the |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 1942 | // hoisting wouldn't move the transition past an instruction that has a |
| 1943 | // DependsOn flag for anything it changes. |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1944 | GVNFlagSet hoist_depends_blockers = |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 1945 | HValue::ConvertChangesToDependsFlags(instr->ChangesFlags()); |
| 1946 | |
| 1947 | // In addition, the transition must not be hoisted above elements kind |
| 1948 | // changes, or if the transition is destructive to the elements buffer, |
| 1949 | // changes to array pointer or array contents. |
| 1950 | GVNFlagSet hoist_change_blockers; |
| 1951 | hoist_change_blockers.Add(kChangesElementsKind); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1952 | HTransitionElementsKind* trans = HTransitionElementsKind::cast(instr); |
| 1953 | if (trans->original_map()->has_fast_double_elements()) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 1954 | hoist_change_blockers.Add(kChangesElementsPointer); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1955 | hoist_change_blockers.Add(kChangesDoubleArrayElements); |
| 1956 | } |
| 1957 | if (trans->transitioned_map()->has_fast_double_elements()) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 1958 | hoist_change_blockers.Add(kChangesElementsPointer); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1959 | hoist_change_blockers.Add(kChangesArrayElements); |
| 1960 | } |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 1961 | if (FLAG_trace_gvn) { |
| 1962 | GVNFlagSet hoist_blockers = hoist_depends_blockers; |
| 1963 | hoist_blockers.Add(hoist_change_blockers); |
| 1964 | GVNFlagSet first_time = *first_time_changes; |
| 1965 | first_time.Add(*first_time_depends); |
| 1966 | TRACE_GVN_4("Checking dependencies on HTransitionElementsKind " |
| 1967 | "%d (%s) hoist blockers: %s; " |
| 1968 | "first-time accumulated: %s\n", |
| 1969 | instr->id(), |
| 1970 | instr->Mnemonic(), |
| 1971 | *GetGVNFlagsString(hoist_blockers), |
| 1972 | *GetGVNFlagsString(first_time)); |
| 1973 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1974 | // It's possible to hoist transition from the current loop loop only if |
| 1975 | // they dominate all of the successor blocks in the same loop and there |
| 1976 | // are not any instructions that have Changes/DependsOn that intervene |
| 1977 | // between it and the beginning of the loop header. |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1978 | bool in_nested_loop = block != loop_header && |
| 1979 | ((block->parent_loop_header() != loop_header) || |
| 1980 | block->IsLoopHeader()); |
| 1981 | can_hoist = !in_nested_loop && |
| 1982 | block->IsLoopSuccessorDominator() && |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1983 | !first_time_depends->ContainsAnyOf(hoist_depends_blockers) && |
| 1984 | !first_time_changes->ContainsAnyOf(hoist_change_blockers); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1987 | if (can_hoist) { |
| 1988 | bool inputs_loop_invariant = true; |
| 1989 | for (int i = 0; i < instr->OperandCount(); ++i) { |
| 1990 | if (instr->OperandAt(i)->IsDefinedAfter(pre_header)) { |
| 1991 | inputs_loop_invariant = false; |
| 1992 | } |
| 1993 | } |
| 1994 | |
| 1995 | if (inputs_loop_invariant && ShouldMove(instr, loop_header)) { |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 1996 | TRACE_GVN_1("Hoisting loop invariant instruction %d\n", instr->id()); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1997 | // Move the instruction out of the loop. |
| 1998 | instr->Unlink(); |
| 1999 | instr->InsertBefore(pre_header->end()); |
| 2000 | if (instr->HasSideEffects()) removed_side_effects_ = true; |
| 2001 | hoisted = true; |
| 2002 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2003 | } |
| 2004 | } |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 2005 | if (!hoisted) { |
| 2006 | // If an instruction is not hoisted, we have to account for its side |
| 2007 | // effects when hoisting later HTransitionElementsKind instructions. |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 2008 | GVNFlagSet previous_depends = *first_time_depends; |
| 2009 | GVNFlagSet previous_changes = *first_time_changes; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 2010 | first_time_depends->Add(instr->DependsOnFlags()); |
| 2011 | first_time_changes->Add(instr->ChangesFlags()); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 2012 | if (!(previous_depends == *first_time_depends)) { |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 2013 | TRACE_GVN_1("Updated first-time accumulated %s\n", |
| 2014 | *GetGVNFlagsString(*first_time_depends)); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 2015 | } |
| 2016 | if (!(previous_changes == *first_time_changes)) { |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 2017 | TRACE_GVN_1("Updated first-time accumulated %s\n", |
| 2018 | *GetGVNFlagsString(*first_time_changes)); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 2019 | } |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 2020 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2021 | instr = next; |
| 2022 | } |
| 2023 | } |
| 2024 | |
kmillikin@chromium.org | c0cfb56 | 2011-01-26 10:44:48 +0000 | [diff] [blame] | 2025 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2026 | bool HGlobalValueNumberer::AllowCodeMotion() { |
| 2027 | return info()->shared_info()->opt_count() + 1 < Compiler::kDefaultMaxOptCount; |
| 2028 | } |
| 2029 | |
| 2030 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2031 | bool HGlobalValueNumberer::ShouldMove(HInstruction* instr, |
| 2032 | HBasicBlock* loop_header) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2033 | // If we've disabled code motion or we're in a block that unconditionally |
| 2034 | // deoptimizes, don't move any instructions. |
| 2035 | return AllowCodeMotion() && !instr->block()->IsDeoptimizing(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 2039 | GVNFlagSet HGlobalValueNumberer::CollectSideEffectsOnPathsToDominatedBlock( |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 2040 | HBasicBlock* dominator, HBasicBlock* dominated) { |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 2041 | GVNFlagSet side_effects; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 2042 | for (int i = 0; i < dominated->predecessors()->length(); ++i) { |
| 2043 | HBasicBlock* block = dominated->predecessors()->at(i); |
| 2044 | if (dominator->block_id() < block->block_id() && |
| 2045 | block->block_id() < dominated->block_id() && |
| 2046 | visited_on_paths_.Add(block->block_id())) { |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 2047 | side_effects.Add(block_side_effects_[block->block_id()]); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2048 | if (block->IsLoopHeader()) { |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 2049 | side_effects.Add(loop_side_effects_[block->block_id()]); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2050 | } |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 2051 | side_effects.Add(CollectSideEffectsOnPathsToDominatedBlock( |
| 2052 | dominator, block)); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 2053 | } |
| 2054 | } |
| 2055 | return side_effects; |
| 2056 | } |
| 2057 | |
| 2058 | |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 2059 | // Each instance of this class is like a "stack frame" for the recursive |
| 2060 | // traversal of the dominator tree done during GVN (the stack is handled |
| 2061 | // as a double linked list). |
| 2062 | // We reuse frames when possible so the list length is limited by the depth |
| 2063 | // of the dominator tree but this forces us to initialize each frame calling |
| 2064 | // an explicit "Initialize" method instead of a using constructor. |
| 2065 | class GvnBasicBlockState: public ZoneObject { |
| 2066 | public: |
| 2067 | static GvnBasicBlockState* CreateEntry(Zone* zone, |
| 2068 | HBasicBlock* entry_block, |
| 2069 | HValueMap* entry_map) { |
| 2070 | return new(zone) |
| 2071 | GvnBasicBlockState(NULL, entry_block, entry_map, NULL, zone); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 2074 | HBasicBlock* block() { return block_; } |
| 2075 | HValueMap* map() { return map_; } |
| 2076 | HSideEffectMap* dominators() { return &dominators_; } |
| 2077 | |
| 2078 | GvnBasicBlockState* next_in_dominator_tree_traversal( |
| 2079 | Zone* zone, |
| 2080 | HBasicBlock** dominator) { |
| 2081 | // This assignment needs to happen before calling next_dominated() because |
| 2082 | // that call can reuse "this" if we are at the last dominated block. |
| 2083 | *dominator = block(); |
| 2084 | GvnBasicBlockState* result = next_dominated(zone); |
| 2085 | if (result == NULL) { |
| 2086 | GvnBasicBlockState* dominator_state = pop(); |
| 2087 | if (dominator_state != NULL) { |
| 2088 | // This branch is guaranteed not to return NULL because pop() never |
| 2089 | // returns a state where "is_done() == true". |
| 2090 | *dominator = dominator_state->block(); |
| 2091 | result = dominator_state->next_dominated(zone); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2092 | } else { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 2093 | // Unnecessary (we are returning NULL) but done for cleanness. |
| 2094 | *dominator = NULL; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2095 | } |
| 2096 | } |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 2097 | return result; |
| 2098 | } |
| 2099 | |
| 2100 | private: |
| 2101 | void Initialize(HBasicBlock* block, |
| 2102 | HValueMap* map, |
| 2103 | HSideEffectMap* dominators, |
| 2104 | bool copy_map, |
| 2105 | Zone* zone) { |
| 2106 | block_ = block; |
| 2107 | map_ = copy_map ? map->Copy(zone) : map; |
| 2108 | dominated_index_ = -1; |
| 2109 | length_ = block->dominated_blocks()->length(); |
| 2110 | if (dominators != NULL) { |
| 2111 | dominators_ = *dominators; |
| 2112 | } |
| 2113 | } |
| 2114 | bool is_done() { return dominated_index_ >= length_; } |
| 2115 | |
| 2116 | GvnBasicBlockState(GvnBasicBlockState* previous, |
| 2117 | HBasicBlock* block, |
| 2118 | HValueMap* map, |
| 2119 | HSideEffectMap* dominators, |
| 2120 | Zone* zone) |
| 2121 | : previous_(previous), next_(NULL) { |
| 2122 | Initialize(block, map, dominators, true, zone); |
| 2123 | } |
| 2124 | |
| 2125 | GvnBasicBlockState* next_dominated(Zone* zone) { |
| 2126 | dominated_index_++; |
| 2127 | if (dominated_index_ == length_ - 1) { |
| 2128 | // No need to copy the map for the last child in the dominator tree. |
| 2129 | Initialize(block_->dominated_blocks()->at(dominated_index_), |
| 2130 | map(), |
| 2131 | dominators(), |
| 2132 | false, |
| 2133 | zone); |
| 2134 | return this; |
| 2135 | } else if (dominated_index_ < length_) { |
| 2136 | return push(zone, |
| 2137 | block_->dominated_blocks()->at(dominated_index_), |
| 2138 | dominators()); |
| 2139 | } else { |
| 2140 | return NULL; |
| 2141 | } |
| 2142 | } |
| 2143 | |
| 2144 | GvnBasicBlockState* push(Zone* zone, |
| 2145 | HBasicBlock* block, |
| 2146 | HSideEffectMap* dominators) { |
| 2147 | if (next_ == NULL) { |
| 2148 | next_ = |
| 2149 | new(zone) GvnBasicBlockState(this, block, map(), dominators, zone); |
| 2150 | } else { |
| 2151 | next_->Initialize(block, map(), dominators, true, zone); |
| 2152 | } |
| 2153 | return next_; |
| 2154 | } |
| 2155 | GvnBasicBlockState* pop() { |
| 2156 | GvnBasicBlockState* result = previous_; |
| 2157 | while (result != NULL && result->is_done()) { |
| 2158 | TRACE_GVN_2("Backtracking from block B%d to block b%d\n", |
| 2159 | block()->block_id(), |
| 2160 | previous_->block()->block_id()) |
| 2161 | result = result->previous_; |
| 2162 | } |
| 2163 | return result; |
| 2164 | } |
| 2165 | |
| 2166 | GvnBasicBlockState* previous_; |
| 2167 | GvnBasicBlockState* next_; |
| 2168 | HBasicBlock* block_; |
| 2169 | HValueMap* map_; |
| 2170 | HSideEffectMap dominators_; |
| 2171 | int dominated_index_; |
| 2172 | int length_; |
| 2173 | }; |
| 2174 | |
| 2175 | // This is a recursive traversal of the dominator tree but it has been turned |
| 2176 | // into a loop to avoid stack overflows. |
| 2177 | // The logical "stack frames" of the recursion are kept in a list of |
| 2178 | // GvnBasicBlockState instances. |
| 2179 | void HGlobalValueNumberer::AnalyzeGraph() { |
| 2180 | HBasicBlock* entry_block = graph_->entry_block(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2181 | HValueMap* entry_map = new(zone()) HValueMap(zone()); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 2182 | GvnBasicBlockState* current = |
| 2183 | GvnBasicBlockState::CreateEntry(zone(), entry_block, entry_map); |
| 2184 | |
| 2185 | while (current != NULL) { |
| 2186 | HBasicBlock* block = current->block(); |
| 2187 | HValueMap* map = current->map(); |
| 2188 | HSideEffectMap* dominators = current->dominators(); |
| 2189 | |
| 2190 | TRACE_GVN_2("Analyzing block B%d%s\n", |
| 2191 | block->block_id(), |
| 2192 | block->IsLoopHeader() ? " (loop header)" : ""); |
| 2193 | |
| 2194 | // If this is a loop header kill everything killed by the loop. |
| 2195 | if (block->IsLoopHeader()) { |
| 2196 | map->Kill(loop_side_effects_[block->block_id()]); |
| 2197 | } |
| 2198 | |
| 2199 | // Go through all instructions of the current block. |
| 2200 | HInstruction* instr = block->first(); |
| 2201 | while (instr != NULL) { |
| 2202 | HInstruction* next = instr->next(); |
| 2203 | GVNFlagSet flags = instr->ChangesFlags(); |
| 2204 | if (!flags.IsEmpty()) { |
| 2205 | // Clear all instructions in the map that are affected by side effects. |
| 2206 | // Store instruction as the dominating one for tracked side effects. |
| 2207 | map->Kill(flags); |
| 2208 | dominators->Store(flags, instr); |
| 2209 | TRACE_GVN_2("Instruction %d %s\n", instr->id(), |
| 2210 | *GetGVNFlagsString(flags)); |
| 2211 | } |
| 2212 | if (instr->CheckFlag(HValue::kUseGVN)) { |
| 2213 | ASSERT(!instr->HasObservableSideEffects()); |
| 2214 | HValue* other = map->Lookup(instr); |
| 2215 | if (other != NULL) { |
| 2216 | ASSERT(instr->Equals(other) && other->Equals(instr)); |
| 2217 | TRACE_GVN_4("Replacing value %d (%s) with value %d (%s)\n", |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 2218 | instr->id(), |
| 2219 | instr->Mnemonic(), |
| 2220 | other->id(), |
| 2221 | other->Mnemonic()); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 2222 | if (instr->HasSideEffects()) removed_side_effects_ = true; |
| 2223 | instr->DeleteAndReplaceWith(other); |
| 2224 | } else { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2225 | map->Add(instr, zone()); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 2226 | } |
| 2227 | } |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 2228 | if (instr->CheckFlag(HValue::kTrackSideEffectDominators)) { |
| 2229 | for (int i = 0; i < kNumberOfTrackedSideEffects; i++) { |
| 2230 | HValue* other = dominators->at(i); |
| 2231 | GVNFlag changes_flag = HValue::ChangesFlagFromInt(i); |
| 2232 | GVNFlag depends_on_flag = HValue::DependsOnFlagFromInt(i); |
| 2233 | if (instr->DependsOnFlags().Contains(depends_on_flag) && |
| 2234 | (other != NULL)) { |
| 2235 | TRACE_GVN_5("Side-effect #%d in %d (%s) is dominated by %d (%s)\n", |
| 2236 | i, |
| 2237 | instr->id(), |
| 2238 | instr->Mnemonic(), |
| 2239 | other->id(), |
| 2240 | other->Mnemonic()); |
| 2241 | instr->SetSideEffectDominator(changes_flag, other); |
| 2242 | } |
| 2243 | } |
| 2244 | } |
| 2245 | instr = next; |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 2246 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2247 | |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 2248 | HBasicBlock* dominator_block; |
| 2249 | GvnBasicBlockState* next = |
| 2250 | current->next_in_dominator_tree_traversal(zone(), &dominator_block); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2251 | |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 2252 | if (next != NULL) { |
| 2253 | HBasicBlock* dominated = next->block(); |
| 2254 | HValueMap* successor_map = next->map(); |
| 2255 | HSideEffectMap* successor_dominators = next->dominators(); |
| 2256 | |
| 2257 | // Kill everything killed on any path between this block and the |
| 2258 | // dominated block. We don't have to traverse these paths if the |
| 2259 | // value map and the dominators list is already empty. If the range |
| 2260 | // of block ids (block_id, dominated_id) is empty there are no such |
| 2261 | // paths. |
| 2262 | if ((!successor_map->IsEmpty() || !successor_dominators->IsEmpty()) && |
| 2263 | dominator_block->block_id() + 1 < dominated->block_id()) { |
| 2264 | visited_on_paths_.Clear(); |
| 2265 | GVNFlagSet side_effects_on_all_paths = |
| 2266 | CollectSideEffectsOnPathsToDominatedBlock(dominator_block, |
| 2267 | dominated); |
| 2268 | successor_map->Kill(side_effects_on_all_paths); |
| 2269 | successor_dominators->Kill(side_effects_on_all_paths); |
| 2270 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2271 | } |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 2272 | current = next; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2273 | } |
| 2274 | } |
| 2275 | |
| 2276 | |
| 2277 | class HInferRepresentation BASE_EMBEDDED { |
| 2278 | public: |
| 2279 | explicit HInferRepresentation(HGraph* graph) |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 2280 | : graph_(graph), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2281 | worklist_(8, graph->zone()), |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 2282 | in_worklist_(graph->GetMaximumValueID(), graph->zone()) { } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2283 | |
| 2284 | void Analyze(); |
| 2285 | |
| 2286 | private: |
| 2287 | Representation TryChange(HValue* current); |
| 2288 | void AddToWorklist(HValue* current); |
| 2289 | void InferBasedOnInputs(HValue* current); |
| 2290 | void AddDependantsToWorklist(HValue* current); |
| 2291 | void InferBasedOnUses(HValue* current); |
| 2292 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2293 | Zone* zone() const { return graph_->zone(); } |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 2294 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2295 | HGraph* graph_; |
| 2296 | ZoneList<HValue*> worklist_; |
| 2297 | BitVector in_worklist_; |
| 2298 | }; |
| 2299 | |
| 2300 | |
| 2301 | void HInferRepresentation::AddToWorklist(HValue* current) { |
| 2302 | if (current->representation().IsSpecialization()) return; |
| 2303 | if (!current->CheckFlag(HValue::kFlexibleRepresentation)) return; |
| 2304 | if (in_worklist_.Contains(current->id())) return; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2305 | worklist_.Add(current, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2306 | in_worklist_.Add(current->id()); |
| 2307 | } |
| 2308 | |
| 2309 | |
| 2310 | // This method tries to specialize the representation type of the value |
| 2311 | // given as a parameter. The value is asked to infer its representation type |
| 2312 | // based on its inputs. If the inferred type is more specialized, then this |
| 2313 | // becomes the new representation type of the node. |
| 2314 | void HInferRepresentation::InferBasedOnInputs(HValue* current) { |
| 2315 | Representation r = current->representation(); |
| 2316 | if (r.IsSpecialization()) return; |
| 2317 | ASSERT(current->CheckFlag(HValue::kFlexibleRepresentation)); |
| 2318 | Representation inferred = current->InferredRepresentation(); |
| 2319 | if (inferred.IsSpecialization()) { |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 2320 | if (FLAG_trace_representation) { |
| 2321 | PrintF("Changing #%d representation %s -> %s based on inputs\n", |
| 2322 | current->id(), |
| 2323 | r.Mnemonic(), |
| 2324 | inferred.Mnemonic()); |
| 2325 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2326 | current->ChangeRepresentation(inferred); |
| 2327 | AddDependantsToWorklist(current); |
| 2328 | } |
| 2329 | } |
| 2330 | |
| 2331 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2332 | void HInferRepresentation::AddDependantsToWorklist(HValue* value) { |
| 2333 | for (HUseIterator it(value->uses()); !it.Done(); it.Advance()) { |
| 2334 | AddToWorklist(it.value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2335 | } |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2336 | for (int i = 0; i < value->OperandCount(); ++i) { |
| 2337 | AddToWorklist(value->OperandAt(i)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2338 | } |
| 2339 | } |
| 2340 | |
| 2341 | |
| 2342 | // This method calculates whether specializing the representation of the value |
| 2343 | // given as the parameter has a benefit in terms of less necessary type |
| 2344 | // conversions. If there is a benefit, then the representation of the value is |
| 2345 | // specialized. |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2346 | void HInferRepresentation::InferBasedOnUses(HValue* value) { |
| 2347 | Representation r = value->representation(); |
| 2348 | if (r.IsSpecialization() || value->HasNoUses()) return; |
| 2349 | ASSERT(value->CheckFlag(HValue::kFlexibleRepresentation)); |
| 2350 | Representation new_rep = TryChange(value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2351 | if (!new_rep.IsNone()) { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2352 | if (!value->representation().Equals(new_rep)) { |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 2353 | if (FLAG_trace_representation) { |
| 2354 | PrintF("Changing #%d representation %s -> %s based on uses\n", |
| 2355 | value->id(), |
| 2356 | r.Mnemonic(), |
| 2357 | new_rep.Mnemonic()); |
| 2358 | } |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2359 | value->ChangeRepresentation(new_rep); |
| 2360 | AddDependantsToWorklist(value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2361 | } |
| 2362 | } |
| 2363 | } |
| 2364 | |
| 2365 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2366 | Representation HInferRepresentation::TryChange(HValue* value) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2367 | // Array of use counts for each representation. |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2368 | int use_count[Representation::kNumRepresentations] = { 0 }; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2369 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2370 | for (HUseIterator it(value->uses()); !it.Done(); it.Advance()) { |
| 2371 | HValue* use = it.value(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2372 | Representation rep = use->ObservedInputRepresentation(it.index()); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2373 | if (rep.IsNone()) continue; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2374 | if (FLAG_trace_representation) { |
| 2375 | PrintF("%d %s is used by %d %s as %s\n", |
| 2376 | value->id(), |
| 2377 | value->Mnemonic(), |
| 2378 | use->id(), |
| 2379 | use->Mnemonic(), |
| 2380 | rep.Mnemonic()); |
| 2381 | } |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2382 | if (use->IsPhi()) HPhi::cast(use)->AddIndirectUsesTo(&use_count[0]); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 2383 | use_count[rep.kind()] += use->LoopWeight(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2384 | } |
| 2385 | int tagged_count = use_count[Representation::kTagged]; |
| 2386 | int double_count = use_count[Representation::kDouble]; |
| 2387 | int int32_count = use_count[Representation::kInteger32]; |
| 2388 | int non_tagged_count = double_count + int32_count; |
| 2389 | |
| 2390 | // 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] | 2391 | if (value->IsPhi() && !value->block()->IsLoopHeader() && tagged_count > 0) { |
| 2392 | return Representation::None(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 2395 | // Prefer unboxing over boxing, the latter is more expensive. |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 2396 | if (tagged_count > non_tagged_count) return Representation::None(); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 2397 | |
| 2398 | // Prefer Integer32 over Double, if possible. |
| 2399 | if (int32_count > 0 && value->IsConvertibleToInteger()) { |
| 2400 | return Representation::Integer32(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2401 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 2402 | |
| 2403 | if (double_count > 0) return Representation::Double(); |
| 2404 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2405 | return Representation::None(); |
| 2406 | } |
| 2407 | |
| 2408 | |
| 2409 | void HInferRepresentation::Analyze() { |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 2410 | HPhase phase("H_Infer representations", graph_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2411 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2412 | // (1) Initialize bit vectors and count real uses. Each phi gets a |
| 2413 | // bit-vector of length <number of phis>. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2414 | const ZoneList<HPhi*>* phi_list = graph_->phi_list(); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2415 | int phi_count = phi_list->length(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2416 | ZoneList<BitVector*> connected_phis(phi_count, graph_->zone()); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2417 | for (int i = 0; i < phi_count; ++i) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2418 | phi_list->at(i)->InitRealUses(i); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 2419 | BitVector* connected_set = new(zone()) BitVector(phi_count, graph_->zone()); |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 2420 | connected_set->Add(i); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2421 | connected_phis.Add(connected_set, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2424 | // (2) Do a fixed point iteration to find the set of connected phis. A |
| 2425 | // phi is connected to another phi if its value is used either directly or |
| 2426 | // indirectly through a transitive closure of the def-use relation. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2427 | bool change = true; |
| 2428 | while (change) { |
| 2429 | change = false; |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2430 | // We normally have far more "forward edges" than "backward edges", |
| 2431 | // so we terminate faster when we walk backwards. |
| 2432 | for (int i = phi_count - 1; i >= 0; --i) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2433 | HPhi* phi = phi_list->at(i); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2434 | for (HUseIterator it(phi->uses()); !it.Done(); it.Advance()) { |
| 2435 | HValue* use = it.value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2436 | if (use->IsPhi()) { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2437 | int id = HPhi::cast(use)->phi_id(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2438 | if (connected_phis[i]->UnionIsChanged(*connected_phis[id])) |
| 2439 | change = true; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2440 | } |
| 2441 | } |
| 2442 | } |
| 2443 | } |
| 2444 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2445 | // (3a) Use the phi reachability information from step 2 to |
| 2446 | // push information about values which can't be converted to integer |
| 2447 | // without deoptimization through the phi use-def chains, avoiding |
| 2448 | // unnecessary deoptimizations later. |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2449 | for (int i = 0; i < phi_count; ++i) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2450 | HPhi* phi = phi_list->at(i); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 2451 | bool cti = phi->AllOperandsConvertibleToInteger(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2452 | if (cti) continue; |
| 2453 | |
| 2454 | for (BitVector::Iterator it(connected_phis.at(i)); |
| 2455 | !it.Done(); |
| 2456 | it.Advance()) { |
| 2457 | HPhi* phi = phi_list->at(it.Current()); |
| 2458 | phi->set_is_convertible_to_integer(false); |
| 2459 | phi->ResetInteger32Uses(); |
| 2460 | } |
| 2461 | } |
| 2462 | |
| 2463 | // (3b) Use the phi reachability information from step 2 to |
| 2464 | // sum up the non-phi use counts of all connected phis. |
| 2465 | for (int i = 0; i < phi_count; ++i) { |
| 2466 | HPhi* phi = phi_list->at(i); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2467 | for (BitVector::Iterator it(connected_phis.at(i)); |
| 2468 | !it.Done(); |
| 2469 | it.Advance()) { |
| 2470 | int index = it.Current(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2471 | HPhi* it_use = phi_list->at(index); |
| 2472 | if (index != i) phi->AddNonPhiUsesFrom(it_use); // Don't count twice. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2473 | } |
| 2474 | } |
| 2475 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 2476 | // Initialize work list |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2477 | for (int i = 0; i < graph_->blocks()->length(); ++i) { |
| 2478 | HBasicBlock* block = graph_->blocks()->at(i); |
| 2479 | const ZoneList<HPhi*>* phis = block->phis(); |
| 2480 | for (int j = 0; j < phis->length(); ++j) { |
| 2481 | AddToWorklist(phis->at(j)); |
| 2482 | } |
| 2483 | |
| 2484 | HInstruction* current = block->first(); |
| 2485 | while (current != NULL) { |
| 2486 | AddToWorklist(current); |
| 2487 | current = current->next(); |
| 2488 | } |
| 2489 | } |
| 2490 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 2491 | // Do a fixed point iteration, trying to improve representations |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2492 | while (!worklist_.is_empty()) { |
| 2493 | HValue* current = worklist_.RemoveLast(); |
| 2494 | in_worklist_.Remove(current->id()); |
| 2495 | InferBasedOnInputs(current); |
| 2496 | InferBasedOnUses(current); |
| 2497 | } |
| 2498 | } |
| 2499 | |
| 2500 | |
| 2501 | void HGraph::InitializeInferredTypes() { |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 2502 | HPhase phase("H_Inferring types", this); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2503 | InitializeInferredTypes(0, this->blocks_.length() - 1); |
| 2504 | } |
| 2505 | |
| 2506 | |
| 2507 | void HGraph::InitializeInferredTypes(int from_inclusive, int to_inclusive) { |
| 2508 | for (int i = from_inclusive; i <= to_inclusive; ++i) { |
| 2509 | HBasicBlock* block = blocks_[i]; |
| 2510 | |
| 2511 | const ZoneList<HPhi*>* phis = block->phis(); |
| 2512 | for (int j = 0; j < phis->length(); j++) { |
| 2513 | phis->at(j)->UpdateInferredType(); |
| 2514 | } |
| 2515 | |
| 2516 | HInstruction* current = block->first(); |
| 2517 | while (current != NULL) { |
| 2518 | current->UpdateInferredType(); |
| 2519 | current = current->next(); |
| 2520 | } |
| 2521 | |
| 2522 | if (block->IsLoopHeader()) { |
| 2523 | HBasicBlock* last_back_edge = |
| 2524 | block->loop_information()->GetLastBackEdge(); |
| 2525 | InitializeInferredTypes(i + 1, last_back_edge->block_id()); |
| 2526 | // Skip all blocks already processed by the recursive call. |
| 2527 | i = last_back_edge->block_id(); |
| 2528 | // Update phis of the loop header now after the whole loop body is |
| 2529 | // guaranteed to be processed. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2530 | ZoneList<HValue*> worklist(block->phis()->length(), zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2531 | for (int j = 0; j < block->phis()->length(); ++j) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2532 | worklist.Add(block->phis()->at(j), zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2533 | } |
| 2534 | InferTypes(&worklist); |
| 2535 | } |
| 2536 | } |
| 2537 | } |
| 2538 | |
| 2539 | |
| 2540 | void HGraph::PropagateMinusZeroChecks(HValue* value, BitVector* visited) { |
| 2541 | HValue* current = value; |
| 2542 | while (current != NULL) { |
| 2543 | if (visited->Contains(current->id())) return; |
| 2544 | |
| 2545 | // For phis, we must propagate the check to all of its inputs. |
| 2546 | if (current->IsPhi()) { |
| 2547 | visited->Add(current->id()); |
| 2548 | HPhi* phi = HPhi::cast(current); |
| 2549 | for (int i = 0; i < phi->OperandCount(); ++i) { |
| 2550 | PropagateMinusZeroChecks(phi->OperandAt(i), visited); |
| 2551 | } |
| 2552 | break; |
| 2553 | } |
| 2554 | |
| 2555 | // For multiplication and division, we must propagate to the left and |
| 2556 | // the right side. |
| 2557 | if (current->IsMul()) { |
| 2558 | HMul* mul = HMul::cast(current); |
| 2559 | mul->EnsureAndPropagateNotMinusZero(visited); |
| 2560 | PropagateMinusZeroChecks(mul->left(), visited); |
| 2561 | PropagateMinusZeroChecks(mul->right(), visited); |
| 2562 | } else if (current->IsDiv()) { |
| 2563 | HDiv* div = HDiv::cast(current); |
| 2564 | div->EnsureAndPropagateNotMinusZero(visited); |
| 2565 | PropagateMinusZeroChecks(div->left(), visited); |
| 2566 | PropagateMinusZeroChecks(div->right(), visited); |
| 2567 | } |
| 2568 | |
| 2569 | current = current->EnsureAndPropagateNotMinusZero(visited); |
| 2570 | } |
| 2571 | } |
| 2572 | |
| 2573 | |
| 2574 | void HGraph::InsertRepresentationChangeForUse(HValue* value, |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2575 | HValue* use_value, |
| 2576 | int use_index, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2577 | Representation to) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2578 | // Insert the representation change right before its use. For phi-uses we |
| 2579 | // insert at the end of the corresponding predecessor. |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2580 | HInstruction* next = NULL; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2581 | if (use_value->IsPhi()) { |
| 2582 | next = use_value->block()->predecessors()->at(use_index)->end(); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2583 | } else { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2584 | next = HInstruction::cast(use_value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2587 | // For constants we try to make the representation change at compile |
| 2588 | // time. When a representation change is not possible without loss of |
| 2589 | // information we treat constants like normal instructions and insert the |
| 2590 | // change instructions for them. |
| 2591 | HInstruction* new_value = NULL; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2592 | bool is_truncating = use_value->CheckFlag(HValue::kTruncatingToInt32); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2593 | bool deoptimize_on_undefined = |
| 2594 | use_value->CheckFlag(HValue::kDeoptimizeOnUndefined); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2595 | if (value->IsConstant()) { |
| 2596 | HConstant* constant = HConstant::cast(value); |
| 2597 | // Try to create a new copy of the constant with the new representation. |
| 2598 | new_value = is_truncating |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2599 | ? constant->CopyToTruncatedInt32(zone()) |
| 2600 | : constant->CopyToRepresentation(to, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | if (new_value == NULL) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2604 | new_value = new(zone()) HChange(value, to, |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2605 | is_truncating, deoptimize_on_undefined); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
| 2608 | new_value->InsertBefore(next); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2609 | use_value->SetOperandAt(use_index, new_value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
| 2612 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2613 | void HGraph::InsertRepresentationChangesForValue(HValue* value) { |
| 2614 | Representation r = value->representation(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2615 | if (r.IsNone()) return; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2616 | if (value->HasNoUses()) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2617 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2618 | for (HUseIterator it(value->uses()); !it.Done(); it.Advance()) { |
| 2619 | HValue* use_value = it.value(); |
| 2620 | int use_index = it.index(); |
| 2621 | Representation req = use_value->RequiredInputRepresentation(use_index); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2622 | if (req.IsNone() || req.Equals(r)) continue; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2623 | InsertRepresentationChangeForUse(value, use_value, use_index, req); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2624 | } |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2625 | if (value->HasNoUses()) { |
| 2626 | ASSERT(value->IsConstant()); |
| 2627 | value->DeleteAndReplaceWith(NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2628 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 2629 | |
| 2630 | // The only purpose of a HForceRepresentation is to represent the value |
| 2631 | // after the (possible) HChange instruction. We make it disappear. |
| 2632 | if (value->IsForceRepresentation()) { |
| 2633 | value->DeleteAndReplaceWith(HForceRepresentation::cast(value)->value()); |
| 2634 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2635 | } |
| 2636 | |
| 2637 | |
| 2638 | void HGraph::InsertRepresentationChanges() { |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 2639 | HPhase phase("H_Representation changes", this); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2640 | |
| 2641 | // Compute truncation flag for phis: Initially assume that all |
| 2642 | // int32-phis allow truncation and iteratively remove the ones that |
| 2643 | // are used in an operation that does not allow a truncating |
| 2644 | // conversion. |
| 2645 | // TODO(fschneider): Replace this with a worklist-based iteration. |
| 2646 | for (int i = 0; i < phi_list()->length(); i++) { |
| 2647 | HPhi* phi = phi_list()->at(i); |
| 2648 | if (phi->representation().IsInteger32()) { |
| 2649 | phi->SetFlag(HValue::kTruncatingToInt32); |
| 2650 | } |
| 2651 | } |
| 2652 | bool change = true; |
| 2653 | while (change) { |
| 2654 | change = false; |
| 2655 | for (int i = 0; i < phi_list()->length(); i++) { |
| 2656 | HPhi* phi = phi_list()->at(i); |
| 2657 | if (!phi->CheckFlag(HValue::kTruncatingToInt32)) continue; |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 2658 | if (!phi->CheckUsesForFlag(HValue::kTruncatingToInt32)) { |
| 2659 | phi->ClearFlag(HValue::kTruncatingToInt32); |
| 2660 | change = true; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2661 | } |
| 2662 | } |
| 2663 | } |
| 2664 | |
| 2665 | for (int i = 0; i < blocks_.length(); ++i) { |
| 2666 | // Process phi instructions first. |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2667 | const ZoneList<HPhi*>* phis = blocks_[i]->phis(); |
| 2668 | for (int j = 0; j < phis->length(); j++) { |
| 2669 | InsertRepresentationChangesForValue(phis->at(j)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
| 2672 | // Process normal instructions. |
| 2673 | HInstruction* current = blocks_[i]->first(); |
| 2674 | while (current != NULL) { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2675 | InsertRepresentationChangesForValue(current); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2676 | current = current->next(); |
| 2677 | } |
| 2678 | } |
| 2679 | } |
| 2680 | |
| 2681 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2682 | void HGraph::RecursivelyMarkPhiDeoptimizeOnUndefined(HPhi* phi) { |
| 2683 | if (phi->CheckFlag(HValue::kDeoptimizeOnUndefined)) return; |
| 2684 | phi->SetFlag(HValue::kDeoptimizeOnUndefined); |
| 2685 | for (int i = 0; i < phi->OperandCount(); ++i) { |
| 2686 | HValue* input = phi->OperandAt(i); |
| 2687 | if (input->IsPhi()) { |
| 2688 | RecursivelyMarkPhiDeoptimizeOnUndefined(HPhi::cast(input)); |
| 2689 | } |
| 2690 | } |
| 2691 | } |
| 2692 | |
| 2693 | |
| 2694 | void HGraph::MarkDeoptimizeOnUndefined() { |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 2695 | HPhase phase("H_MarkDeoptimizeOnUndefined", this); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2696 | // Compute DeoptimizeOnUndefined flag for phis. |
| 2697 | // Any phi that can reach a use with DeoptimizeOnUndefined set must |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2698 | // have DeoptimizeOnUndefined set. Currently only HCompareIDAndBranch, with |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2699 | // double input representation, has this flag set. |
| 2700 | // The flag is used by HChange tagged->double, which must deoptimize |
| 2701 | // if one of its uses has this flag set. |
| 2702 | for (int i = 0; i < phi_list()->length(); i++) { |
| 2703 | HPhi* phi = phi_list()->at(i); |
| 2704 | if (phi->representation().IsDouble()) { |
| 2705 | for (HUseIterator it(phi->uses()); !it.Done(); it.Advance()) { |
| 2706 | if (it.value()->CheckFlag(HValue::kDeoptimizeOnUndefined)) { |
| 2707 | RecursivelyMarkPhiDeoptimizeOnUndefined(phi); |
| 2708 | break; |
| 2709 | } |
| 2710 | } |
| 2711 | } |
| 2712 | } |
| 2713 | } |
| 2714 | |
| 2715 | |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 2716 | void HGraph::ComputeMinusZeroChecks() { |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 2717 | BitVector visited(GetMaximumValueID(), zone()); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 2718 | for (int i = 0; i < blocks_.length(); ++i) { |
| 2719 | for (HInstruction* current = blocks_[i]->first(); |
| 2720 | current != NULL; |
| 2721 | current = current->next()) { |
| 2722 | if (current->IsChange()) { |
| 2723 | HChange* change = HChange::cast(current); |
| 2724 | // Propagate flags for negative zero checks upwards from conversions |
| 2725 | // int32-to-tagged and int32-to-double. |
| 2726 | Representation from = change->value()->representation(); |
| 2727 | ASSERT(from.Equals(change->from())); |
| 2728 | if (from.IsInteger32()) { |
| 2729 | ASSERT(change->to().IsTagged() || change->to().IsDouble()); |
| 2730 | ASSERT(visited.IsEmpty()); |
| 2731 | PropagateMinusZeroChecks(change->value(), &visited); |
| 2732 | visited.Clear(); |
| 2733 | } |
| 2734 | } |
| 2735 | } |
| 2736 | } |
| 2737 | } |
| 2738 | |
| 2739 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2740 | // Implementation of utility class to encapsulate the translation state for |
| 2741 | // a (possibly inlined) function. |
| 2742 | FunctionState::FunctionState(HGraphBuilder* owner, |
| 2743 | CompilationInfo* info, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2744 | TypeFeedbackOracle* oracle, |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 2745 | ReturnHandlingFlag return_handling) |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2746 | : owner_(owner), |
| 2747 | compilation_info_(info), |
| 2748 | oracle_(oracle), |
| 2749 | call_context_(NULL), |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 2750 | return_handling_(return_handling), |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2751 | function_return_(NULL), |
| 2752 | test_context_(NULL), |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 2753 | entry_(NULL), |
| 2754 | arguments_elements_(NULL), |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2755 | outer_(owner->function_state()) { |
| 2756 | if (outer_ != NULL) { |
| 2757 | // State for an inline function. |
| 2758 | if (owner->ast_context()->IsTest()) { |
| 2759 | HBasicBlock* if_true = owner->graph()->CreateBasicBlock(); |
| 2760 | HBasicBlock* if_false = owner->graph()->CreateBasicBlock(); |
| 2761 | if_true->MarkAsInlineReturnTarget(); |
| 2762 | if_false->MarkAsInlineReturnTarget(); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2763 | Expression* cond = TestContext::cast(owner->ast_context())->condition(); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2764 | // The AstContext constructor pushed on the context stack. This newed |
| 2765 | // instance is the reason that AstContext can't be BASE_EMBEDDED. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2766 | test_context_ = new TestContext(owner, cond, if_true, if_false); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2767 | } else { |
| 2768 | function_return_ = owner->graph()->CreateBasicBlock(); |
| 2769 | function_return()->MarkAsInlineReturnTarget(); |
| 2770 | } |
| 2771 | // Set this after possibly allocating a new TestContext above. |
| 2772 | call_context_ = owner->ast_context(); |
| 2773 | } |
| 2774 | |
| 2775 | // Push on the state stack. |
| 2776 | owner->set_function_state(this); |
| 2777 | } |
| 2778 | |
| 2779 | |
| 2780 | FunctionState::~FunctionState() { |
| 2781 | delete test_context_; |
| 2782 | owner_->set_function_state(outer_); |
| 2783 | } |
| 2784 | |
| 2785 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2786 | // Implementation of utility classes to represent an expression's context in |
| 2787 | // the AST. |
| 2788 | AstContext::AstContext(HGraphBuilder* owner, Expression::Context kind) |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2789 | : owner_(owner), |
| 2790 | kind_(kind), |
| 2791 | outer_(owner->ast_context()), |
| 2792 | for_typeof_(false) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2793 | owner->set_ast_context(this); // Push. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2794 | #ifdef DEBUG |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 2795 | ASSERT(owner->environment()->frame_type() == JS_FUNCTION); |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 2796 | original_length_ = owner->environment()->length(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2797 | #endif |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | |
| 2801 | AstContext::~AstContext() { |
| 2802 | owner_->set_ast_context(outer_); // Pop. |
| 2803 | } |
| 2804 | |
| 2805 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2806 | EffectContext::~EffectContext() { |
| 2807 | ASSERT(owner()->HasStackOverflow() || |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2808 | owner()->current_block() == NULL || |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 2809 | (owner()->environment()->length() == original_length_ && |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 2810 | owner()->environment()->frame_type() == JS_FUNCTION)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
| 2813 | |
| 2814 | ValueContext::~ValueContext() { |
| 2815 | ASSERT(owner()->HasStackOverflow() || |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2816 | owner()->current_block() == NULL || |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 2817 | (owner()->environment()->length() == original_length_ + 1 && |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 2818 | owner()->environment()->frame_type() == JS_FUNCTION)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
| 2821 | |
| 2822 | void EffectContext::ReturnValue(HValue* value) { |
| 2823 | // The value is simply ignored. |
| 2824 | } |
| 2825 | |
| 2826 | |
| 2827 | void ValueContext::ReturnValue(HValue* value) { |
| 2828 | // The value is tracked in the bailout environment, and communicated |
| 2829 | // through the environment as the result of the expression. |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 2830 | if (!arguments_allowed() && value->CheckFlag(HValue::kIsArguments)) { |
| 2831 | owner()->Bailout("bad value context for arguments value"); |
| 2832 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2833 | owner()->Push(value); |
| 2834 | } |
| 2835 | |
| 2836 | |
| 2837 | void TestContext::ReturnValue(HValue* value) { |
| 2838 | BuildBranch(value); |
| 2839 | } |
| 2840 | |
| 2841 | |
| 2842 | void EffectContext::ReturnInstruction(HInstruction* instr, int ast_id) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2843 | ASSERT(!instr->IsControlInstruction()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2844 | owner()->AddInstruction(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2845 | if (instr->HasObservableSideEffects()) owner()->AddSimulate(ast_id); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2849 | void EffectContext::ReturnControl(HControlInstruction* instr, int ast_id) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2850 | ASSERT(!instr->HasObservableSideEffects()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2851 | HBasicBlock* empty_true = owner()->graph()->CreateBasicBlock(); |
| 2852 | HBasicBlock* empty_false = owner()->graph()->CreateBasicBlock(); |
| 2853 | instr->SetSuccessorAt(0, empty_true); |
| 2854 | instr->SetSuccessorAt(1, empty_false); |
| 2855 | owner()->current_block()->Finish(instr); |
| 2856 | HBasicBlock* join = owner()->CreateJoin(empty_true, empty_false, ast_id); |
| 2857 | owner()->set_current_block(join); |
| 2858 | } |
| 2859 | |
| 2860 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2861 | void ValueContext::ReturnInstruction(HInstruction* instr, int ast_id) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2862 | ASSERT(!instr->IsControlInstruction()); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 2863 | if (!arguments_allowed() && instr->CheckFlag(HValue::kIsArguments)) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2864 | return owner()->Bailout("bad value context for arguments object value"); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 2865 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2866 | owner()->AddInstruction(instr); |
| 2867 | owner()->Push(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2868 | if (instr->HasObservableSideEffects()) owner()->AddSimulate(ast_id); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2869 | } |
| 2870 | |
| 2871 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2872 | void ValueContext::ReturnControl(HControlInstruction* instr, int ast_id) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2873 | ASSERT(!instr->HasObservableSideEffects()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2874 | if (!arguments_allowed() && instr->CheckFlag(HValue::kIsArguments)) { |
| 2875 | return owner()->Bailout("bad value context for arguments object value"); |
| 2876 | } |
| 2877 | HBasicBlock* materialize_false = owner()->graph()->CreateBasicBlock(); |
| 2878 | HBasicBlock* materialize_true = owner()->graph()->CreateBasicBlock(); |
| 2879 | instr->SetSuccessorAt(0, materialize_true); |
| 2880 | instr->SetSuccessorAt(1, materialize_false); |
| 2881 | owner()->current_block()->Finish(instr); |
| 2882 | owner()->set_current_block(materialize_true); |
| 2883 | owner()->Push(owner()->graph()->GetConstantTrue()); |
| 2884 | owner()->set_current_block(materialize_false); |
| 2885 | owner()->Push(owner()->graph()->GetConstantFalse()); |
| 2886 | HBasicBlock* join = |
| 2887 | owner()->CreateJoin(materialize_true, materialize_false, ast_id); |
| 2888 | owner()->set_current_block(join); |
| 2889 | } |
| 2890 | |
| 2891 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2892 | void TestContext::ReturnInstruction(HInstruction* instr, int ast_id) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2893 | ASSERT(!instr->IsControlInstruction()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2894 | HGraphBuilder* builder = owner(); |
| 2895 | builder->AddInstruction(instr); |
| 2896 | // We expect a simulate after every expression with side effects, though |
| 2897 | // 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] | 2898 | if (instr->HasObservableSideEffects()) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2899 | builder->Push(instr); |
| 2900 | builder->AddSimulate(ast_id); |
| 2901 | builder->Pop(); |
| 2902 | } |
| 2903 | BuildBranch(instr); |
| 2904 | } |
| 2905 | |
| 2906 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2907 | void TestContext::ReturnControl(HControlInstruction* instr, int ast_id) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2908 | ASSERT(!instr->HasObservableSideEffects()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2909 | HBasicBlock* empty_true = owner()->graph()->CreateBasicBlock(); |
| 2910 | HBasicBlock* empty_false = owner()->graph()->CreateBasicBlock(); |
| 2911 | instr->SetSuccessorAt(0, empty_true); |
| 2912 | instr->SetSuccessorAt(1, empty_false); |
| 2913 | owner()->current_block()->Finish(instr); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 2914 | empty_true->Goto(if_true(), owner()->function_state()); |
| 2915 | empty_false->Goto(if_false(), owner()->function_state()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2916 | owner()->set_current_block(NULL); |
| 2917 | } |
| 2918 | |
| 2919 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2920 | void TestContext::BuildBranch(HValue* value) { |
| 2921 | // We expect the graph to be in edge-split form: there is no edge that |
| 2922 | // connects a branch node to a join node. We conservatively ensure that |
| 2923 | // property by always adding an empty block on the outgoing edges of this |
| 2924 | // branch. |
| 2925 | HGraphBuilder* builder = owner(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2926 | if (value != NULL && value->CheckFlag(HValue::kIsArguments)) { |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 2927 | builder->Bailout("arguments object value in a test context"); |
| 2928 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2929 | HBasicBlock* empty_true = builder->graph()->CreateBasicBlock(); |
| 2930 | HBasicBlock* empty_false = builder->graph()->CreateBasicBlock(); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 2931 | unsigned test_id = condition()->test_id(); |
| 2932 | ToBooleanStub::Types expected(builder->oracle()->ToBooleanTypes(test_id)); |
| 2933 | HBranch* test = new(zone()) HBranch(value, empty_true, empty_false, expected); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2934 | builder->current_block()->Finish(test); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2935 | |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 2936 | empty_true->Goto(if_true(), owner()->function_state()); |
| 2937 | empty_false->Goto(if_false(), owner()->function_state()); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2938 | builder->set_current_block(NULL); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2941 | |
| 2942 | // HGraphBuilder infrastructure for bailing out and checking bailouts. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2943 | #define CHECK_BAILOUT(call) \ |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2944 | do { \ |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2945 | call; \ |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2946 | if (HasStackOverflow()) return; \ |
| 2947 | } while (false) |
| 2948 | |
| 2949 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2950 | #define CHECK_ALIVE(call) \ |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2951 | do { \ |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2952 | call; \ |
| 2953 | if (HasStackOverflow() || current_block() == NULL) return; \ |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2954 | } while (false) |
| 2955 | |
| 2956 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2957 | void HGraphBuilder::Bailout(const char* reason) { |
| 2958 | if (FLAG_trace_bailout) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 2959 | SmartArrayPointer<char> name( |
| 2960 | info()->shared_info()->DebugName()->ToCString()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2961 | PrintF("Bailout in HGraphBuilder: @\"%s\": %s\n", *name, reason); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2962 | } |
| 2963 | SetStackOverflow(); |
| 2964 | } |
| 2965 | |
| 2966 | |
| 2967 | void HGraphBuilder::VisitForEffect(Expression* expr) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2968 | EffectContext for_effect(this); |
| 2969 | Visit(expr); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
| 2972 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 2973 | void HGraphBuilder::VisitForValue(Expression* expr, ArgumentsAllowedFlag flag) { |
| 2974 | ValueContext for_value(this, flag); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2975 | Visit(expr); |
| 2976 | } |
| 2977 | |
| 2978 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2979 | void HGraphBuilder::VisitForTypeOf(Expression* expr) { |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 2980 | ValueContext for_value(this, ARGUMENTS_NOT_ALLOWED); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2981 | for_value.set_for_typeof(true); |
| 2982 | Visit(expr); |
| 2983 | } |
| 2984 | |
| 2985 | |
| 2986 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2987 | void HGraphBuilder::VisitForControl(Expression* expr, |
| 2988 | HBasicBlock* true_block, |
| 2989 | HBasicBlock* false_block) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2990 | TestContext for_test(this, expr, true_block, false_block); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2991 | Visit(expr); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
| 2994 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame^] | 2995 | void HGraphBuilder::VisitArgument(Expression* expr) { |
| 2996 | CHECK_ALIVE(VisitForValue(expr)); |
| 2997 | Push(AddInstruction(new(zone()) HPushArgument(Pop()))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | |
| 3001 | void HGraphBuilder::VisitArgumentList(ZoneList<Expression*>* arguments) { |
| 3002 | for (int i = 0; i < arguments->length(); i++) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3003 | CHECK_ALIVE(VisitArgument(arguments->at(i))); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3004 | } |
| 3005 | } |
| 3006 | |
| 3007 | |
| 3008 | void HGraphBuilder::VisitExpressions(ZoneList<Expression*>* exprs) { |
| 3009 | for (int i = 0; i < exprs->length(); ++i) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3010 | CHECK_ALIVE(VisitForValue(exprs->at(i))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3011 | } |
| 3012 | } |
| 3013 | |
| 3014 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3015 | HGraph* HGraphBuilder::CreateGraph() { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 3016 | graph_ = new(zone()) HGraph(info()); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 3017 | if (FLAG_hydrogen_stats) HStatistics::Instance()->Initialize(info()); |
| 3018 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3019 | { |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 3020 | HPhase phase("H_Block building"); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3021 | CompilationHandleScope handle_scope(info()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3022 | current_block_ = graph()->entry_block(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3023 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3024 | Scope* scope = info()->scope(); |
| 3025 | if (scope->HasIllegalRedeclaration()) { |
| 3026 | Bailout("function with illegal redeclaration"); |
| 3027 | return NULL; |
| 3028 | } |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 3029 | if (scope->calls_eval()) { |
| 3030 | Bailout("function calls eval"); |
| 3031 | return NULL; |
| 3032 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 3033 | SetUpScope(scope); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3034 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3035 | // Add an edge to the body entry. This is warty: the graph's start |
| 3036 | // environment will be used by the Lithium translation as the initial |
| 3037 | // environment on graph entry, but it has now been mutated by the |
| 3038 | // Hydrogen translation of the instructions in the start block. This |
| 3039 | // environment uses values which have not been defined yet. These |
| 3040 | // Hydrogen instructions will then be replayed by the Lithium |
| 3041 | // translation, so they cannot have an environment effect. The edge to |
| 3042 | // the body's entry block (along with some special logic for the start |
| 3043 | // block in HInstruction::InsertAfter) seals the start block from |
| 3044 | // getting unwanted instructions inserted. |
| 3045 | // |
| 3046 | // TODO(kmillikin): Fix this. Stop mutating the initial environment. |
| 3047 | // Make the Hydrogen instructions in the initial block into Hydrogen |
| 3048 | // values (but not instructions), present in the initial environment and |
| 3049 | // not replayed by the Lithium translation. |
| 3050 | HEnvironment* initial_env = environment()->CopyWithoutHistory(); |
| 3051 | HBasicBlock* body_entry = CreateBasicBlock(initial_env); |
| 3052 | current_block()->Goto(body_entry); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 3053 | body_entry->SetJoinId(AstNode::kFunctionEntryId); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3054 | set_current_block(body_entry); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 3055 | |
| 3056 | // Handle implicit declaration of the function name in named function |
| 3057 | // expressions before other declarations. |
| 3058 | if (scope->is_function_scope() && scope->function() != NULL) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 3059 | VisitVariableDeclaration(scope->function()); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 3060 | } |
| 3061 | VisitDeclarations(scope->declarations()); |
| 3062 | AddSimulate(AstNode::kDeclarationsId); |
| 3063 | |
| 3064 | HValue* context = environment()->LookupContext(); |
| 3065 | AddInstruction( |
| 3066 | new(zone()) HStackCheck(context, HStackCheck::kFunctionEntry)); |
| 3067 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3068 | VisitStatements(info()->function()->body()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3069 | if (HasStackOverflow()) return NULL; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3070 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3071 | if (current_block() != NULL) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 3072 | HReturn* instr = new(zone()) HReturn(graph()->GetConstantUndefined()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3073 | current_block()->FinishExit(instr); |
| 3074 | set_current_block(NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3075 | } |
| 3076 | } |
| 3077 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 3078 | return graph(); |
| 3079 | } |
| 3080 | |
| 3081 | bool HGraph::Optimize(SmartArrayPointer<char>* bailout_reason) { |
| 3082 | NoHandleAllocation no_handles; |
| 3083 | AssertNoAllocation no_gc; |
| 3084 | |
| 3085 | *bailout_reason = SmartArrayPointer<char>(); |
| 3086 | OrderBlocks(); |
| 3087 | AssignDominators(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3088 | |
| 3089 | #ifdef DEBUG |
| 3090 | // Do a full verify after building the graph and computing dominators. |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 3091 | Verify(true); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3092 | #endif |
| 3093 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 3094 | PropagateDeoptimizingMark(); |
| 3095 | if (!CheckConstPhiUses()) { |
| 3096 | *bailout_reason = SmartArrayPointer<char>(StrDup( |
| 3097 | "Unsupported phi use of const variable")); |
| 3098 | return false; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3099 | } |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 3100 | EliminateRedundantPhis(); |
| 3101 | if (!CheckArgumentsPhiUses()) { |
| 3102 | *bailout_reason = SmartArrayPointer<char>(StrDup( |
| 3103 | "Unsupported phi use of arguments")); |
| 3104 | return false; |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 3105 | } |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 3106 | if (FLAG_eliminate_dead_phis) EliminateUnreachablePhis(); |
| 3107 | CollectPhis(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3108 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 3109 | if (has_osr_loop_entry()) { |
| 3110 | const ZoneList<HPhi*>* phis = osr_loop_entry()->phis(); |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 3111 | for (int j = 0; j < phis->length(); j++) { |
| 3112 | HPhi* phi = phis->at(j); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 3113 | osr_values()->at(phi->merged_index())->set_incoming_value(phi); |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 3114 | } |
| 3115 | } |
| 3116 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 3117 | HInferRepresentation rep(this); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3118 | rep.Analyze(); |
| 3119 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 3120 | MarkDeoptimizeOnUndefined(); |
| 3121 | InsertRepresentationChanges(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3122 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 3123 | InitializeInferredTypes(); |
| 3124 | Canonicalize(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3125 | |
| 3126 | // Perform common subexpression elimination and loop-invariant code motion. |
| 3127 | if (FLAG_use_gvn) { |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 3128 | HPhase phase("H_Global value numbering", this); |
| 3129 | HGlobalValueNumberer gvn(this, info()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 3130 | bool removed_side_effects = gvn.Analyze(); |
| 3131 | // Trigger a second analysis pass to further eliminate duplicate values that |
| 3132 | // could only be discovered by removing side-effect-generating instructions |
| 3133 | // during the first pass. |
| 3134 | if (FLAG_smi_only_arrays && removed_side_effects) { |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 3135 | removed_side_effects = gvn.Analyze(); |
| 3136 | ASSERT(!removed_side_effects); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 3137 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3138 | } |
| 3139 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3140 | if (FLAG_use_range) { |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 3141 | HRangeAnalysis rangeAnalysis(this); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3142 | rangeAnalysis.Analyze(); |
| 3143 | } |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 3144 | ComputeMinusZeroChecks(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3145 | |
| 3146 | // Eliminate redundant stack checks on backwards branches. |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 3147 | HStackCheckEliminator sce(this); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3148 | sce.Process(); |
| 3149 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 3150 | EliminateRedundantBoundsChecks(); |
| 3151 | DehoistSimpleArrayIndexComputations(); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3152 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 3153 | return true; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3157 | // We try to "factor up" HBoundsCheck instructions towards the root of the |
| 3158 | // dominator tree. |
| 3159 | // For now we handle checks where the index is like "exp + int32value". |
| 3160 | // If in the dominator tree we check "exp + v1" and later (dominated) |
| 3161 | // "exp + v2", if v2 <= v1 we can safely remove the second check, and if |
| 3162 | // v2 > v1 we can use v2 in the 1st check and again remove the second. |
| 3163 | // To do so we keep a dictionary of all checks where the key if the pair |
| 3164 | // "exp, length". |
| 3165 | // The class BoundsCheckKey represents this key. |
| 3166 | class BoundsCheckKey : public ZoneObject { |
| 3167 | public: |
| 3168 | HValue* IndexBase() const { return index_base_; } |
| 3169 | HValue* Length() const { return length_; } |
| 3170 | |
| 3171 | uint32_t Hash() { |
| 3172 | return static_cast<uint32_t>(index_base_->Hashcode() ^ length_->Hashcode()); |
| 3173 | } |
| 3174 | |
| 3175 | static BoundsCheckKey* Create(Zone* zone, |
| 3176 | HBoundsCheck* check, |
| 3177 | int32_t* offset) { |
| 3178 | HValue* index_base = NULL; |
| 3179 | HConstant* constant = NULL; |
| 3180 | bool is_sub = false; |
| 3181 | |
| 3182 | if (check->index()->IsAdd()) { |
| 3183 | HAdd* index = HAdd::cast(check->index()); |
| 3184 | if (index->left()->IsConstant()) { |
| 3185 | constant = HConstant::cast(index->left()); |
| 3186 | index_base = index->right(); |
| 3187 | } else if (index->right()->IsConstant()) { |
| 3188 | constant = HConstant::cast(index->right()); |
| 3189 | index_base = index->left(); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3190 | } |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3191 | } else if (check->index()->IsSub()) { |
| 3192 | HSub* index = HSub::cast(check->index()); |
| 3193 | is_sub = true; |
| 3194 | if (index->left()->IsConstant()) { |
| 3195 | constant = HConstant::cast(index->left()); |
| 3196 | index_base = index->right(); |
| 3197 | } else if (index->right()->IsConstant()) { |
| 3198 | constant = HConstant::cast(index->right()); |
| 3199 | index_base = index->left(); |
| 3200 | } |
| 3201 | } |
| 3202 | |
| 3203 | if (constant != NULL && constant->HasInteger32Value()) { |
| 3204 | *offset = is_sub ? - constant->Integer32Value() |
| 3205 | : constant->Integer32Value(); |
| 3206 | } else { |
| 3207 | *offset = 0; |
| 3208 | index_base = check->index(); |
| 3209 | } |
| 3210 | |
| 3211 | return new(zone) BoundsCheckKey(index_base, check->length()); |
| 3212 | } |
| 3213 | |
| 3214 | private: |
| 3215 | BoundsCheckKey(HValue* index_base, HValue* length) |
| 3216 | : index_base_(index_base), |
| 3217 | length_(length) { } |
| 3218 | |
| 3219 | HValue* index_base_; |
| 3220 | HValue* length_; |
| 3221 | }; |
| 3222 | |
| 3223 | |
| 3224 | // Data about each HBoundsCheck that can be eliminated or moved. |
| 3225 | // It is the "value" in the dictionary indexed by "base-index, length" |
| 3226 | // (the key is BoundsCheckKey). |
| 3227 | // We scan the code with a dominator tree traversal. |
| 3228 | // Traversing the dominator tree we keep a stack (implemented as a singly |
| 3229 | // linked list) of "data" for each basic block that contains a relevant check |
| 3230 | // with the same key (the dictionary holds the head of the list). |
| 3231 | // We also keep all the "data" created for a given basic block in a list, and |
| 3232 | // use it to "clean up" the dictionary when backtracking in the dominator tree |
| 3233 | // traversal. |
| 3234 | // Doing this each dictionary entry always directly points to the check that |
| 3235 | // is dominating the code being examined now. |
| 3236 | // We also track the current "offset" of the index expression and use it to |
| 3237 | // decide if any check is already "covered" (so it can be removed) or not. |
| 3238 | class BoundsCheckBbData: public ZoneObject { |
| 3239 | public: |
| 3240 | BoundsCheckKey* Key() const { return key_; } |
| 3241 | int32_t LowerOffset() const { return lower_offset_; } |
| 3242 | int32_t UpperOffset() const { return upper_offset_; } |
| 3243 | HBasicBlock* BasicBlock() const { return basic_block_; } |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 3244 | HBoundsCheck* LowerCheck() const { return lower_check_; } |
| 3245 | HBoundsCheck* UpperCheck() const { return upper_check_; } |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3246 | BoundsCheckBbData* NextInBasicBlock() const { return next_in_bb_; } |
| 3247 | BoundsCheckBbData* FatherInDominatorTree() const { return father_in_dt_; } |
| 3248 | |
| 3249 | bool OffsetIsCovered(int32_t offset) const { |
| 3250 | return offset >= LowerOffset() && offset <= UpperOffset(); |
| 3251 | } |
| 3252 | |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 3253 | bool HasSingleCheck() { return lower_check_ == upper_check_; } |
| 3254 | |
| 3255 | // The goal of this method is to modify either upper_offset_ or |
| 3256 | // lower_offset_ so that also new_offset is covered (the covered |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3257 | // range grows). |
| 3258 | // |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 3259 | // The precondition is that new_check follows UpperCheck() and |
| 3260 | // LowerCheck() in the same basic block, and that new_offset is not |
| 3261 | // covered (otherwise we could simply remove new_check). |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3262 | // |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 3263 | // If HasSingleCheck() is true then new_check is added as "second check" |
| 3264 | // (either upper or lower; note that HasSingleCheck() becomes false). |
| 3265 | // Otherwise one of the current checks is modified so that it also covers |
| 3266 | // new_offset, and new_check is removed. |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3267 | void CoverCheck(HBoundsCheck* new_check, |
| 3268 | int32_t new_offset) { |
| 3269 | ASSERT(new_check->index()->representation().IsInteger32()); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 3270 | bool keep_new_check = false; |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3271 | |
| 3272 | if (new_offset > upper_offset_) { |
| 3273 | upper_offset_ = new_offset; |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 3274 | if (HasSingleCheck()) { |
| 3275 | keep_new_check = true; |
| 3276 | upper_check_ = new_check; |
| 3277 | } else { |
| 3278 | BuildOffsetAdd(upper_check_, |
| 3279 | &added_upper_index_, |
| 3280 | &added_upper_offset_, |
| 3281 | Key()->IndexBase(), |
| 3282 | new_check->index()->representation(), |
| 3283 | new_offset); |
| 3284 | upper_check_->SetOperandAt(0, added_upper_index_); |
| 3285 | } |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3286 | } else if (new_offset < lower_offset_) { |
| 3287 | lower_offset_ = new_offset; |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 3288 | if (HasSingleCheck()) { |
| 3289 | keep_new_check = true; |
| 3290 | lower_check_ = new_check; |
| 3291 | } else { |
| 3292 | BuildOffsetAdd(lower_check_, |
| 3293 | &added_lower_index_, |
| 3294 | &added_lower_offset_, |
| 3295 | Key()->IndexBase(), |
| 3296 | new_check->index()->representation(), |
| 3297 | new_offset); |
| 3298 | lower_check_->SetOperandAt(0, added_lower_index_); |
| 3299 | } |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3300 | } else { |
| 3301 | ASSERT(false); |
| 3302 | } |
| 3303 | |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 3304 | if (!keep_new_check) { |
| 3305 | new_check->DeleteAndReplaceWith(NULL); |
| 3306 | } |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | void RemoveZeroOperations() { |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 3310 | RemoveZeroAdd(&added_lower_index_, &added_lower_offset_); |
| 3311 | RemoveZeroAdd(&added_upper_index_, &added_upper_offset_); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | BoundsCheckBbData(BoundsCheckKey* key, |
| 3315 | int32_t lower_offset, |
| 3316 | int32_t upper_offset, |
| 3317 | HBasicBlock* bb, |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 3318 | HBoundsCheck* lower_check, |
| 3319 | HBoundsCheck* upper_check, |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3320 | BoundsCheckBbData* next_in_bb, |
| 3321 | BoundsCheckBbData* father_in_dt) |
| 3322 | : key_(key), |
| 3323 | lower_offset_(lower_offset), |
| 3324 | upper_offset_(upper_offset), |
| 3325 | basic_block_(bb), |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 3326 | lower_check_(lower_check), |
| 3327 | upper_check_(upper_check), |
| 3328 | added_lower_index_(NULL), |
| 3329 | added_lower_offset_(NULL), |
| 3330 | added_upper_index_(NULL), |
| 3331 | added_upper_offset_(NULL), |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3332 | next_in_bb_(next_in_bb), |
| 3333 | father_in_dt_(father_in_dt) { } |
| 3334 | |
| 3335 | private: |
| 3336 | BoundsCheckKey* key_; |
| 3337 | int32_t lower_offset_; |
| 3338 | int32_t upper_offset_; |
| 3339 | HBasicBlock* basic_block_; |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 3340 | HBoundsCheck* lower_check_; |
| 3341 | HBoundsCheck* upper_check_; |
| 3342 | HAdd* added_lower_index_; |
| 3343 | HConstant* added_lower_offset_; |
| 3344 | HAdd* added_upper_index_; |
| 3345 | HConstant* added_upper_offset_; |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3346 | BoundsCheckBbData* next_in_bb_; |
| 3347 | BoundsCheckBbData* father_in_dt_; |
| 3348 | |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 3349 | void BuildOffsetAdd(HBoundsCheck* check, |
| 3350 | HAdd** add, |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3351 | HConstant** constant, |
| 3352 | HValue* original_value, |
| 3353 | Representation representation, |
| 3354 | int32_t new_offset) { |
| 3355 | HConstant* new_constant = new(BasicBlock()->zone()) |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 3356 | HConstant(new_offset, Representation::Integer32()); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3357 | if (*add == NULL) { |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 3358 | new_constant->InsertBefore(check); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3359 | *add = new(BasicBlock()->zone()) HAdd(NULL, |
| 3360 | original_value, |
| 3361 | new_constant); |
| 3362 | (*add)->AssumeRepresentation(representation); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 3363 | (*add)->InsertBefore(check); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3364 | } else { |
| 3365 | new_constant->InsertBefore(*add); |
| 3366 | (*constant)->DeleteAndReplaceWith(new_constant); |
| 3367 | } |
| 3368 | *constant = new_constant; |
| 3369 | } |
| 3370 | |
| 3371 | void RemoveZeroAdd(HAdd** add, HConstant** constant) { |
| 3372 | if (*add != NULL && (*constant)->Integer32Value() == 0) { |
| 3373 | (*add)->DeleteAndReplaceWith((*add)->left()); |
| 3374 | (*constant)->DeleteAndReplaceWith(NULL); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3375 | } |
| 3376 | } |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3377 | }; |
| 3378 | |
| 3379 | |
| 3380 | static bool BoundsCheckKeyMatch(void* key1, void* key2) { |
| 3381 | BoundsCheckKey* k1 = static_cast<BoundsCheckKey*>(key1); |
| 3382 | BoundsCheckKey* k2 = static_cast<BoundsCheckKey*>(key2); |
| 3383 | return k1->IndexBase() == k2->IndexBase() && k1->Length() == k2->Length(); |
| 3384 | } |
| 3385 | |
| 3386 | |
| 3387 | class BoundsCheckTable : private ZoneHashMap { |
| 3388 | public: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3389 | BoundsCheckBbData** LookupOrInsert(BoundsCheckKey* key, Zone* zone) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3390 | return reinterpret_cast<BoundsCheckBbData**>( |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3391 | &(Lookup(key, key->Hash(), true, ZoneAllocationPolicy(zone))->value)); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3392 | } |
| 3393 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3394 | void Insert(BoundsCheckKey* key, BoundsCheckBbData* data, Zone* zone) { |
| 3395 | Lookup(key, key->Hash(), true, ZoneAllocationPolicy(zone))->value = data; |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
| 3398 | void Delete(BoundsCheckKey* key) { |
| 3399 | Remove(key, key->Hash()); |
| 3400 | } |
| 3401 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3402 | explicit BoundsCheckTable(Zone* zone) |
| 3403 | : ZoneHashMap(BoundsCheckKeyMatch, ZoneHashMap::kDefaultHashMapCapacity, |
| 3404 | ZoneAllocationPolicy(zone)) { } |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3405 | }; |
| 3406 | |
| 3407 | |
| 3408 | // Eliminates checks in bb and recursively in the dominated blocks. |
| 3409 | // Also replace the results of check instructions with the original value, if |
| 3410 | // the result is used. This is safe now, since we don't do code motion after |
| 3411 | // this point. It enables better register allocation since the value produced |
| 3412 | // by check instructions is really a copy of the original value. |
| 3413 | void HGraph::EliminateRedundantBoundsChecks(HBasicBlock* bb, |
| 3414 | BoundsCheckTable* table) { |
| 3415 | BoundsCheckBbData* bb_data_list = NULL; |
| 3416 | |
| 3417 | for (HInstruction* i = bb->first(); i != NULL; i = i->next()) { |
| 3418 | if (!i->IsBoundsCheck()) continue; |
| 3419 | |
| 3420 | HBoundsCheck* check = HBoundsCheck::cast(i); |
| 3421 | check->ReplaceAllUsesWith(check->index()); |
| 3422 | |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3423 | if (!FLAG_array_bounds_checks_elimination) continue; |
| 3424 | |
| 3425 | int32_t offset; |
| 3426 | BoundsCheckKey* key = |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3427 | BoundsCheckKey::Create(zone(), check, &offset); |
| 3428 | BoundsCheckBbData** data_p = table->LookupOrInsert(key, zone()); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3429 | BoundsCheckBbData* data = *data_p; |
| 3430 | if (data == NULL) { |
| 3431 | bb_data_list = new(zone()) BoundsCheckBbData(key, |
| 3432 | offset, |
| 3433 | offset, |
| 3434 | bb, |
| 3435 | check, |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 3436 | check, |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3437 | bb_data_list, |
| 3438 | NULL); |
| 3439 | *data_p = bb_data_list; |
| 3440 | } else if (data->OffsetIsCovered(offset)) { |
| 3441 | check->DeleteAndReplaceWith(NULL); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3442 | } else if (data->BasicBlock() == bb) { |
| 3443 | data->CoverCheck(check, offset); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3444 | } else { |
| 3445 | int32_t new_lower_offset = offset < data->LowerOffset() |
| 3446 | ? offset |
| 3447 | : data->LowerOffset(); |
| 3448 | int32_t new_upper_offset = offset > data->UpperOffset() |
| 3449 | ? offset |
| 3450 | : data->UpperOffset(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3451 | bb_data_list = new(zone()) BoundsCheckBbData(key, |
| 3452 | new_lower_offset, |
| 3453 | new_upper_offset, |
| 3454 | bb, |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 3455 | data->LowerCheck(), |
| 3456 | data->UpperCheck(), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3457 | bb_data_list, |
| 3458 | data); |
| 3459 | table->Insert(key, bb_data_list, zone()); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3460 | } |
| 3461 | } |
| 3462 | |
| 3463 | for (int i = 0; i < bb->dominated_blocks()->length(); ++i) { |
| 3464 | EliminateRedundantBoundsChecks(bb->dominated_blocks()->at(i), table); |
| 3465 | } |
| 3466 | |
| 3467 | for (BoundsCheckBbData* data = bb_data_list; |
| 3468 | data != NULL; |
| 3469 | data = data->NextInBasicBlock()) { |
| 3470 | data->RemoveZeroOperations(); |
| 3471 | if (data->FatherInDominatorTree()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3472 | table->Insert(data->Key(), data->FatherInDominatorTree(), zone()); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3473 | } else { |
| 3474 | table->Delete(data->Key()); |
| 3475 | } |
| 3476 | } |
| 3477 | } |
| 3478 | |
| 3479 | |
| 3480 | void HGraph::EliminateRedundantBoundsChecks() { |
| 3481 | HPhase phase("H_Eliminate bounds checks", this); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3482 | BoundsCheckTable checks_table(zone()); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 3483 | EliminateRedundantBoundsChecks(entry_block(), &checks_table); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3484 | } |
| 3485 | |
| 3486 | |
ulan@chromium.org | 0e3f88b | 2012-05-22 09:16:05 +0000 | [diff] [blame] | 3487 | static void DehoistArrayIndex(ArrayInstructionInterface* array_operation) { |
| 3488 | HValue* index = array_operation->GetKey(); |
| 3489 | |
| 3490 | HConstant* constant; |
| 3491 | HValue* subexpression; |
| 3492 | int32_t sign; |
| 3493 | if (index->IsAdd()) { |
| 3494 | sign = 1; |
| 3495 | HAdd* add = HAdd::cast(index); |
| 3496 | if (add->left()->IsConstant()) { |
| 3497 | subexpression = add->right(); |
| 3498 | constant = HConstant::cast(add->left()); |
| 3499 | } else if (add->right()->IsConstant()) { |
| 3500 | subexpression = add->left(); |
| 3501 | constant = HConstant::cast(add->right()); |
| 3502 | } else { |
| 3503 | return; |
| 3504 | } |
| 3505 | } else if (index->IsSub()) { |
| 3506 | sign = -1; |
| 3507 | HSub* sub = HSub::cast(index); |
| 3508 | if (sub->left()->IsConstant()) { |
| 3509 | subexpression = sub->right(); |
| 3510 | constant = HConstant::cast(sub->left()); |
| 3511 | } else if (sub->right()->IsConstant()) { |
| 3512 | subexpression = sub->left(); |
| 3513 | constant = HConstant::cast(sub->right()); |
| 3514 | } return; |
| 3515 | } else { |
| 3516 | return; |
| 3517 | } |
| 3518 | |
| 3519 | if (!constant->HasInteger32Value()) return; |
| 3520 | int32_t value = constant->Integer32Value() * sign; |
| 3521 | // We limit offset values to 30 bits because we want to avoid the risk of |
| 3522 | // overflows when the offset is added to the object header size. |
| 3523 | if (value >= 1 << 30 || value < 0) return; |
| 3524 | array_operation->SetKey(subexpression); |
| 3525 | if (index->HasNoUses()) { |
| 3526 | index->DeleteAndReplaceWith(NULL); |
| 3527 | } |
| 3528 | ASSERT(value >= 0); |
| 3529 | array_operation->SetIndexOffset(static_cast<uint32_t>(value)); |
| 3530 | array_operation->SetDehoisted(true); |
| 3531 | } |
| 3532 | |
| 3533 | |
| 3534 | void HGraph::DehoistSimpleArrayIndexComputations() { |
| 3535 | if (!FLAG_array_index_dehoisting) return; |
| 3536 | |
| 3537 | HPhase phase("H_Dehoist index computations", this); |
| 3538 | for (int i = 0; i < blocks()->length(); ++i) { |
| 3539 | for (HInstruction* instr = blocks()->at(i)->first(); |
| 3540 | instr != NULL; |
| 3541 | instr = instr->next()) { |
| 3542 | ArrayInstructionInterface* array_instruction = NULL; |
| 3543 | if (instr->IsLoadKeyedFastElement()) { |
| 3544 | HLoadKeyedFastElement* op = HLoadKeyedFastElement::cast(instr); |
| 3545 | array_instruction = static_cast<ArrayInstructionInterface*>(op); |
| 3546 | } else if (instr->IsLoadKeyedFastDoubleElement()) { |
| 3547 | HLoadKeyedFastDoubleElement* op = |
| 3548 | HLoadKeyedFastDoubleElement::cast(instr); |
| 3549 | array_instruction = static_cast<ArrayInstructionInterface*>(op); |
| 3550 | } else if (instr->IsLoadKeyedSpecializedArrayElement()) { |
| 3551 | HLoadKeyedSpecializedArrayElement* op = |
| 3552 | HLoadKeyedSpecializedArrayElement::cast(instr); |
| 3553 | array_instruction = static_cast<ArrayInstructionInterface*>(op); |
| 3554 | } else if (instr->IsStoreKeyedFastElement()) { |
| 3555 | HStoreKeyedFastElement* op = HStoreKeyedFastElement::cast(instr); |
| 3556 | array_instruction = static_cast<ArrayInstructionInterface*>(op); |
| 3557 | } else if (instr->IsStoreKeyedFastDoubleElement()) { |
| 3558 | HStoreKeyedFastDoubleElement* op = |
| 3559 | HStoreKeyedFastDoubleElement::cast(instr); |
| 3560 | array_instruction = static_cast<ArrayInstructionInterface*>(op); |
| 3561 | } else if (instr->IsStoreKeyedSpecializedArrayElement()) { |
| 3562 | HStoreKeyedSpecializedArrayElement* op = |
| 3563 | HStoreKeyedSpecializedArrayElement::cast(instr); |
| 3564 | array_instruction = static_cast<ArrayInstructionInterface*>(op); |
| 3565 | } else { |
| 3566 | continue; |
| 3567 | } |
| 3568 | DehoistArrayIndex(array_instruction); |
| 3569 | } |
| 3570 | } |
| 3571 | } |
| 3572 | |
| 3573 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3574 | HInstruction* HGraphBuilder::AddInstruction(HInstruction* instr) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3575 | ASSERT(current_block() != NULL); |
| 3576 | current_block()->AddInstruction(instr); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3577 | return instr; |
| 3578 | } |
| 3579 | |
| 3580 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3581 | void HGraphBuilder::AddSimulate(int ast_id) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3582 | ASSERT(current_block() != NULL); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3583 | current_block()->AddSimulate(ast_id); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | |
| 3587 | void HGraphBuilder::AddPhi(HPhi* instr) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3588 | ASSERT(current_block() != NULL); |
| 3589 | current_block()->AddPhi(instr); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3590 | } |
| 3591 | |
| 3592 | |
| 3593 | void HGraphBuilder::PushAndAdd(HInstruction* instr) { |
| 3594 | Push(instr); |
| 3595 | AddInstruction(instr); |
| 3596 | } |
| 3597 | |
| 3598 | |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 3599 | template <class Instruction> |
| 3600 | HInstruction* HGraphBuilder::PreProcessCall(Instruction* call) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3601 | int count = call->argument_count(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3602 | ZoneList<HValue*> arguments(count, zone()); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3603 | for (int i = 0; i < count; ++i) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3604 | arguments.Add(Pop(), zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3605 | } |
| 3606 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3607 | while (!arguments.is_empty()) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 3608 | AddInstruction(new(zone()) HPushArgument(arguments.RemoveLast())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3609 | } |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3610 | return call; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 3614 | void HGraphBuilder::SetUpScope(Scope* scope) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 3615 | HConstant* undefined_constant = new(zone()) HConstant( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3616 | isolate()->factory()->undefined_value(), Representation::Tagged()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3617 | AddInstruction(undefined_constant); |
| 3618 | graph_->set_undefined_constant(undefined_constant); |
| 3619 | |
mstarzinger@chromium.org | 3233d2f | 2012-03-14 11:16:03 +0000 | [diff] [blame] | 3620 | HArgumentsObject* object = new(zone()) HArgumentsObject; |
| 3621 | AddInstruction(object); |
| 3622 | graph()->SetArgumentsObject(object); |
| 3623 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3624 | // Set the initial values of parameters including "this". "This" has |
| 3625 | // parameter index 0. |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 3626 | ASSERT_EQ(scope->num_parameters() + 1, environment()->parameter_count()); |
| 3627 | |
| 3628 | for (int i = 0; i < environment()->parameter_count(); ++i) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 3629 | HInstruction* parameter = AddInstruction(new(zone()) HParameter(i)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3630 | environment()->Bind(i, parameter); |
| 3631 | } |
| 3632 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 3633 | // First special is HContext. |
| 3634 | HInstruction* context = AddInstruction(new(zone()) HContext); |
| 3635 | environment()->BindContext(context); |
| 3636 | |
| 3637 | // Initialize specials and locals to undefined. |
| 3638 | for (int i = environment()->parameter_count() + 1; |
| 3639 | i < environment()->length(); |
| 3640 | ++i) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3641 | environment()->Bind(i, undefined_constant); |
| 3642 | } |
| 3643 | |
| 3644 | // Handle the arguments and arguments shadow variables specially (they do |
| 3645 | // not have declarations). |
| 3646 | if (scope->arguments() != NULL) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 3647 | if (!scope->arguments()->IsStackAllocated()) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3648 | return Bailout("context-allocated arguments"); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3649 | } |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 3650 | |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 3651 | environment()->Bind(scope->arguments(), |
| 3652 | graph()->GetArgumentsObject()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3653 | } |
| 3654 | } |
| 3655 | |
| 3656 | |
| 3657 | void HGraphBuilder::VisitStatements(ZoneList<Statement*>* statements) { |
| 3658 | for (int i = 0; i < statements->length(); i++) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3659 | CHECK_ALIVE(Visit(statements->at(i))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3660 | } |
| 3661 | } |
| 3662 | |
| 3663 | |
| 3664 | HBasicBlock* HGraphBuilder::CreateBasicBlock(HEnvironment* env) { |
| 3665 | HBasicBlock* b = graph()->CreateBasicBlock(); |
| 3666 | b->SetInitialEnvironment(env); |
| 3667 | return b; |
| 3668 | } |
| 3669 | |
| 3670 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3671 | HBasicBlock* HGraphBuilder::CreateLoopHeaderBlock() { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3672 | HBasicBlock* header = graph()->CreateBasicBlock(); |
| 3673 | HEnvironment* entry_env = environment()->CopyAsLoopHeader(header); |
| 3674 | header->SetInitialEnvironment(entry_env); |
| 3675 | header->AttachLoopInformation(); |
| 3676 | return header; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3677 | } |
| 3678 | |
| 3679 | |
| 3680 | void HGraphBuilder::VisitBlock(Block* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3681 | ASSERT(!HasStackOverflow()); |
| 3682 | ASSERT(current_block() != NULL); |
| 3683 | ASSERT(current_block()->HasPredecessor()); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 3684 | if (stmt->scope() != NULL) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 3685 | return Bailout("ScopedBlock"); |
| 3686 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3687 | BreakAndContinueInfo break_info(stmt); |
| 3688 | { BreakAndContinueScope push(&break_info, this); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3689 | CHECK_BAILOUT(VisitStatements(stmt->statements())); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3690 | } |
| 3691 | HBasicBlock* break_block = break_info.break_block(); |
| 3692 | if (break_block != NULL) { |
| 3693 | if (current_block() != NULL) current_block()->Goto(break_block); |
| 3694 | break_block->SetJoinId(stmt->ExitId()); |
| 3695 | set_current_block(break_block); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3696 | } |
| 3697 | } |
| 3698 | |
| 3699 | |
| 3700 | void HGraphBuilder::VisitExpressionStatement(ExpressionStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3701 | ASSERT(!HasStackOverflow()); |
| 3702 | ASSERT(current_block() != NULL); |
| 3703 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3704 | VisitForEffect(stmt->expression()); |
| 3705 | } |
| 3706 | |
| 3707 | |
| 3708 | void HGraphBuilder::VisitEmptyStatement(EmptyStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3709 | ASSERT(!HasStackOverflow()); |
| 3710 | ASSERT(current_block() != NULL); |
| 3711 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3712 | } |
| 3713 | |
| 3714 | |
| 3715 | void HGraphBuilder::VisitIfStatement(IfStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3716 | ASSERT(!HasStackOverflow()); |
| 3717 | ASSERT(current_block() != NULL); |
| 3718 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3719 | if (stmt->condition()->ToBooleanIsTrue()) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3720 | AddSimulate(stmt->ThenId()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3721 | Visit(stmt->then_statement()); |
| 3722 | } else if (stmt->condition()->ToBooleanIsFalse()) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3723 | AddSimulate(stmt->ElseId()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3724 | Visit(stmt->else_statement()); |
| 3725 | } else { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3726 | HBasicBlock* cond_true = graph()->CreateBasicBlock(); |
| 3727 | HBasicBlock* cond_false = graph()->CreateBasicBlock(); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3728 | CHECK_BAILOUT(VisitForControl(stmt->condition(), cond_true, cond_false)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3729 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3730 | if (cond_true->HasPredecessor()) { |
| 3731 | cond_true->SetJoinId(stmt->ThenId()); |
| 3732 | set_current_block(cond_true); |
| 3733 | CHECK_BAILOUT(Visit(stmt->then_statement())); |
| 3734 | cond_true = current_block(); |
| 3735 | } else { |
| 3736 | cond_true = NULL; |
| 3737 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3738 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3739 | if (cond_false->HasPredecessor()) { |
| 3740 | cond_false->SetJoinId(stmt->ElseId()); |
| 3741 | set_current_block(cond_false); |
| 3742 | CHECK_BAILOUT(Visit(stmt->else_statement())); |
| 3743 | cond_false = current_block(); |
| 3744 | } else { |
| 3745 | cond_false = NULL; |
| 3746 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3747 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3748 | HBasicBlock* join = CreateJoin(cond_true, cond_false, stmt->IfId()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3749 | set_current_block(join); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3750 | } |
| 3751 | } |
| 3752 | |
| 3753 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3754 | HBasicBlock* HGraphBuilder::BreakAndContinueScope::Get( |
| 3755 | BreakableStatement* stmt, |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 3756 | BreakType type, |
| 3757 | int* drop_extra) { |
| 3758 | *drop_extra = 0; |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3759 | BreakAndContinueScope* current = this; |
| 3760 | while (current != NULL && current->info()->target() != stmt) { |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 3761 | *drop_extra += current->info()->drop_extra(); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3762 | current = current->next(); |
| 3763 | } |
| 3764 | ASSERT(current != NULL); // Always found (unless stack is malformed). |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 3765 | |
| 3766 | if (type == BREAK) { |
| 3767 | *drop_extra += current->info()->drop_extra(); |
| 3768 | } |
| 3769 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3770 | HBasicBlock* block = NULL; |
| 3771 | switch (type) { |
| 3772 | case BREAK: |
| 3773 | block = current->info()->break_block(); |
| 3774 | if (block == NULL) { |
| 3775 | block = current->owner()->graph()->CreateBasicBlock(); |
| 3776 | current->info()->set_break_block(block); |
| 3777 | } |
| 3778 | break; |
| 3779 | |
| 3780 | case CONTINUE: |
| 3781 | block = current->info()->continue_block(); |
| 3782 | if (block == NULL) { |
| 3783 | block = current->owner()->graph()->CreateBasicBlock(); |
| 3784 | current->info()->set_continue_block(block); |
| 3785 | } |
| 3786 | break; |
| 3787 | } |
| 3788 | |
| 3789 | return block; |
| 3790 | } |
| 3791 | |
| 3792 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3793 | void HGraphBuilder::VisitContinueStatement(ContinueStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3794 | ASSERT(!HasStackOverflow()); |
| 3795 | ASSERT(current_block() != NULL); |
| 3796 | ASSERT(current_block()->HasPredecessor()); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 3797 | int drop_extra = 0; |
| 3798 | HBasicBlock* continue_block = break_scope()->Get(stmt->target(), |
| 3799 | CONTINUE, |
| 3800 | &drop_extra); |
| 3801 | Drop(drop_extra); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3802 | current_block()->Goto(continue_block); |
| 3803 | set_current_block(NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3804 | } |
| 3805 | |
| 3806 | |
| 3807 | void HGraphBuilder::VisitBreakStatement(BreakStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3808 | ASSERT(!HasStackOverflow()); |
| 3809 | ASSERT(current_block() != NULL); |
| 3810 | ASSERT(current_block()->HasPredecessor()); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 3811 | int drop_extra = 0; |
| 3812 | HBasicBlock* break_block = break_scope()->Get(stmt->target(), |
| 3813 | BREAK, |
| 3814 | &drop_extra); |
| 3815 | Drop(drop_extra); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3816 | current_block()->Goto(break_block); |
| 3817 | set_current_block(NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3818 | } |
| 3819 | |
| 3820 | |
| 3821 | void HGraphBuilder::VisitReturnStatement(ReturnStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3822 | ASSERT(!HasStackOverflow()); |
| 3823 | ASSERT(current_block() != NULL); |
| 3824 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3825 | AstContext* context = call_context(); |
| 3826 | if (context == NULL) { |
| 3827 | // Not an inlined return, so an actual one. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3828 | CHECK_ALIVE(VisitForValue(stmt->expression())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3829 | HValue* result = environment()->Pop(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 3830 | current_block()->FinishExit(new(zone()) HReturn(result)); |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 3831 | } else if (function_state()->is_construct()) { |
| 3832 | // Return from an inlined construct call. In a test context the return |
| 3833 | // value will always evaluate to true, in a value context the return value |
| 3834 | // needs to be a JSObject. |
| 3835 | if (context->IsTest()) { |
| 3836 | TestContext* test = TestContext::cast(context); |
| 3837 | CHECK_ALIVE(VisitForEffect(stmt->expression())); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 3838 | current_block()->Goto(test->if_true(), function_state()); |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 3839 | } else if (context->IsEffect()) { |
| 3840 | CHECK_ALIVE(VisitForEffect(stmt->expression())); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 3841 | current_block()->Goto(function_return(), function_state()); |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 3842 | } else { |
| 3843 | ASSERT(context->IsValue()); |
| 3844 | CHECK_ALIVE(VisitForValue(stmt->expression())); |
| 3845 | HValue* return_value = Pop(); |
| 3846 | HValue* receiver = environment()->Lookup(0); |
| 3847 | HHasInstanceTypeAndBranch* typecheck = |
| 3848 | new(zone()) HHasInstanceTypeAndBranch(return_value, |
| 3849 | FIRST_SPEC_OBJECT_TYPE, |
| 3850 | LAST_SPEC_OBJECT_TYPE); |
| 3851 | HBasicBlock* if_spec_object = graph()->CreateBasicBlock(); |
| 3852 | HBasicBlock* not_spec_object = graph()->CreateBasicBlock(); |
| 3853 | typecheck->SetSuccessorAt(0, if_spec_object); |
| 3854 | typecheck->SetSuccessorAt(1, not_spec_object); |
| 3855 | current_block()->Finish(typecheck); |
| 3856 | if_spec_object->AddLeaveInlined(return_value, |
| 3857 | function_return(), |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 3858 | function_state()); |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 3859 | not_spec_object->AddLeaveInlined(receiver, |
| 3860 | function_return(), |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 3861 | function_state()); |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 3862 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3863 | } else { |
| 3864 | // Return from an inlined function, visit the subexpression in the |
| 3865 | // expression context of the call. |
| 3866 | if (context->IsTest()) { |
| 3867 | TestContext* test = TestContext::cast(context); |
| 3868 | VisitForControl(stmt->expression(), |
| 3869 | test->if_true(), |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3870 | test->if_false()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3871 | } else if (context->IsEffect()) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3872 | CHECK_ALIVE(VisitForEffect(stmt->expression())); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 3873 | current_block()->Goto(function_return(), function_state()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3874 | } else { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3875 | ASSERT(context->IsValue()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3876 | CHECK_ALIVE(VisitForValue(stmt->expression())); |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 3877 | HValue* return_value = Pop(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3878 | current_block()->AddLeaveInlined(return_value, |
| 3879 | function_return(), |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 3880 | function_state()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3881 | } |
| 3882 | } |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 3883 | set_current_block(NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
| 3886 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 3887 | void HGraphBuilder::VisitWithStatement(WithStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3888 | ASSERT(!HasStackOverflow()); |
| 3889 | ASSERT(current_block() != NULL); |
| 3890 | ASSERT(current_block()->HasPredecessor()); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 3891 | return Bailout("WithStatement"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
| 3894 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3895 | void HGraphBuilder::VisitSwitchStatement(SwitchStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3896 | ASSERT(!HasStackOverflow()); |
| 3897 | ASSERT(current_block() != NULL); |
| 3898 | ASSERT(current_block()->HasPredecessor()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3899 | // We only optimize switch statements with smi-literal smi comparisons, |
| 3900 | // with a bounded number of clauses. |
| 3901 | const int kCaseClauseLimit = 128; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3902 | ZoneList<CaseClause*>* clauses = stmt->cases(); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3903 | int clause_count = clauses->length(); |
| 3904 | if (clause_count > kCaseClauseLimit) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3905 | return Bailout("SwitchStatement: too many clauses"); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3906 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3907 | |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 3908 | HValue* context = environment()->LookupContext(); |
| 3909 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3910 | CHECK_ALIVE(VisitForValue(stmt->tag())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3911 | AddSimulate(stmt->EntryId()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3912 | HValue* tag_value = Pop(); |
| 3913 | HBasicBlock* first_test_block = current_block(); |
| 3914 | |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 3915 | SwitchType switch_type = UNKNOWN_SWITCH; |
| 3916 | |
| 3917 | // 1. Extract clause type |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3918 | for (int i = 0; i < clause_count; ++i) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3919 | CaseClause* clause = clauses->at(i); |
| 3920 | if (clause->is_default()) continue; |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 3921 | |
| 3922 | if (switch_type == UNKNOWN_SWITCH) { |
| 3923 | if (clause->label()->IsSmiLiteral()) { |
| 3924 | switch_type = SMI_SWITCH; |
| 3925 | } else if (clause->label()->IsStringLiteral()) { |
| 3926 | switch_type = STRING_SWITCH; |
| 3927 | } else { |
| 3928 | return Bailout("SwitchStatement: non-literal switch label"); |
| 3929 | } |
| 3930 | } else if ((switch_type == STRING_SWITCH && |
| 3931 | !clause->label()->IsStringLiteral()) || |
| 3932 | (switch_type == SMI_SWITCH && |
| 3933 | !clause->label()->IsSmiLiteral())) { |
| 3934 | return Bailout("SwitchStatemnt: mixed label types are not supported"); |
| 3935 | } |
| 3936 | } |
| 3937 | |
| 3938 | HUnaryControlInstruction* string_check = NULL; |
| 3939 | HBasicBlock* not_string_block = NULL; |
| 3940 | |
| 3941 | // Test switch's tag value if all clauses are string literals |
| 3942 | if (switch_type == STRING_SWITCH) { |
| 3943 | string_check = new(zone()) HIsStringAndBranch(tag_value); |
| 3944 | first_test_block = graph()->CreateBasicBlock(); |
| 3945 | not_string_block = graph()->CreateBasicBlock(); |
| 3946 | |
| 3947 | string_check->SetSuccessorAt(0, first_test_block); |
| 3948 | string_check->SetSuccessorAt(1, not_string_block); |
| 3949 | current_block()->Finish(string_check); |
| 3950 | |
| 3951 | set_current_block(first_test_block); |
| 3952 | } |
| 3953 | |
| 3954 | // 2. Build all the tests, with dangling true branches |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3955 | int default_id = AstNode::kNoNumber; |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 3956 | for (int i = 0; i < clause_count; ++i) { |
| 3957 | CaseClause* clause = clauses->at(i); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3958 | if (clause->is_default()) { |
| 3959 | default_id = clause->EntryId(); |
| 3960 | continue; |
| 3961 | } |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 3962 | if (switch_type == SMI_SWITCH) { |
| 3963 | clause->RecordTypeFeedback(oracle()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3964 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3965 | |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 3966 | // Generate a compare and branch. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 3967 | CHECK_ALIVE(VisitForValue(clause->label())); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3968 | HValue* label_value = Pop(); |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 3969 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3970 | HBasicBlock* next_test_block = graph()->CreateBasicBlock(); |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 3971 | HBasicBlock* body_block = graph()->CreateBasicBlock(); |
| 3972 | |
| 3973 | HControlInstruction* compare; |
| 3974 | |
| 3975 | if (switch_type == SMI_SWITCH) { |
| 3976 | if (!clause->IsSmiCompare()) { |
| 3977 | // Finish with deoptimize and add uses of enviroment values to |
| 3978 | // account for invisible uses. |
| 3979 | current_block()->FinishExitWithDeoptimization(HDeoptimize::kUseAll); |
| 3980 | set_current_block(NULL); |
| 3981 | break; |
| 3982 | } |
| 3983 | |
| 3984 | HCompareIDAndBranch* compare_ = |
| 3985 | new(zone()) HCompareIDAndBranch(tag_value, |
| 3986 | label_value, |
| 3987 | Token::EQ_STRICT); |
| 3988 | compare_->SetInputRepresentation(Representation::Integer32()); |
| 3989 | compare = compare_; |
| 3990 | } else { |
| 3991 | compare = new(zone()) HStringCompareAndBranch(context, tag_value, |
| 3992 | label_value, |
| 3993 | Token::EQ_STRICT); |
| 3994 | } |
| 3995 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 3996 | compare->SetSuccessorAt(0, body_block); |
| 3997 | compare->SetSuccessorAt(1, next_test_block); |
| 3998 | current_block()->Finish(compare); |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 3999 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4000 | set_current_block(next_test_block); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4003 | // Save the current block to use for the default or to join with the |
| 4004 | // exit. This block is NULL if we deoptimized. |
| 4005 | HBasicBlock* last_block = current_block(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4006 | |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 4007 | if (not_string_block != NULL) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4008 | int join_id = (default_id != AstNode::kNoNumber) |
| 4009 | ? default_id |
| 4010 | : stmt->ExitId(); |
| 4011 | last_block = CreateJoin(last_block, not_string_block, join_id); |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 4012 | } |
| 4013 | |
| 4014 | // 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] | 4015 | // translating the clause bodies. |
| 4016 | HBasicBlock* curr_test_block = first_test_block; |
| 4017 | HBasicBlock* fall_through_block = NULL; |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 4018 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4019 | BreakAndContinueInfo break_info(stmt); |
| 4020 | { BreakAndContinueScope push(&break_info, this); |
| 4021 | for (int i = 0; i < clause_count; ++i) { |
| 4022 | CaseClause* clause = clauses->at(i); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4023 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4024 | // Identify the block where normal (non-fall-through) control flow |
| 4025 | // goes to. |
| 4026 | HBasicBlock* normal_block = NULL; |
fschneider@chromium.org | 13da64d | 2011-05-18 12:07:24 +0000 | [diff] [blame] | 4027 | if (clause->is_default()) { |
| 4028 | if (last_block != NULL) { |
| 4029 | normal_block = last_block; |
| 4030 | last_block = NULL; // Cleared to indicate we've handled it. |
| 4031 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4032 | } else if (!curr_test_block->end()->IsDeoptimize()) { |
| 4033 | normal_block = curr_test_block->end()->FirstSuccessor(); |
| 4034 | curr_test_block = curr_test_block->end()->SecondSuccessor(); |
| 4035 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4036 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4037 | // Identify a block to emit the body into. |
| 4038 | if (normal_block == NULL) { |
| 4039 | if (fall_through_block == NULL) { |
| 4040 | // (a) Unreachable. |
| 4041 | if (clause->is_default()) { |
| 4042 | continue; // Might still be reachable clause bodies. |
| 4043 | } else { |
| 4044 | break; |
| 4045 | } |
| 4046 | } else { |
| 4047 | // (b) Reachable only as fall through. |
| 4048 | set_current_block(fall_through_block); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4049 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4050 | } else if (fall_through_block == NULL) { |
| 4051 | // (c) Reachable only normally. |
| 4052 | set_current_block(normal_block); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4053 | } else { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4054 | // (d) Reachable both ways. |
| 4055 | HBasicBlock* join = CreateJoin(fall_through_block, |
| 4056 | normal_block, |
| 4057 | clause->EntryId()); |
| 4058 | set_current_block(join); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4059 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4060 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4061 | CHECK_BAILOUT(VisitStatements(clause->statements())); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4062 | fall_through_block = current_block(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4063 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4064 | } |
| 4065 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4066 | // Create an up-to-3-way join. Use the break block if it exists since |
| 4067 | // it's already a join block. |
| 4068 | HBasicBlock* break_block = break_info.break_block(); |
| 4069 | if (break_block == NULL) { |
| 4070 | set_current_block(CreateJoin(fall_through_block, |
| 4071 | last_block, |
| 4072 | stmt->ExitId())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4073 | } else { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4074 | if (fall_through_block != NULL) fall_through_block->Goto(break_block); |
| 4075 | if (last_block != NULL) last_block->Goto(break_block); |
| 4076 | break_block->SetJoinId(stmt->ExitId()); |
| 4077 | set_current_block(break_block); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4078 | } |
| 4079 | } |
| 4080 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4081 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4082 | bool HGraphBuilder::HasOsrEntryAt(IterationStatement* statement) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4083 | return statement->OsrEntryId() == info()->osr_ast_id(); |
| 4084 | } |
| 4085 | |
| 4086 | |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 4087 | bool HGraphBuilder::PreProcessOsrEntry(IterationStatement* statement) { |
| 4088 | if (!HasOsrEntryAt(statement)) return false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4089 | |
| 4090 | HBasicBlock* non_osr_entry = graph()->CreateBasicBlock(); |
| 4091 | HBasicBlock* osr_entry = graph()->CreateBasicBlock(); |
| 4092 | HValue* true_value = graph()->GetConstantTrue(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4093 | 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] | 4094 | current_block()->Finish(test); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4095 | |
| 4096 | HBasicBlock* loop_predecessor = graph()->CreateBasicBlock(); |
| 4097 | non_osr_entry->Goto(loop_predecessor); |
| 4098 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 4099 | set_current_block(osr_entry); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4100 | int osr_entry_id = statement->OsrEntryId(); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 4101 | int first_expression_index = environment()->first_expression_index(); |
| 4102 | int length = environment()->length(); |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 4103 | ZoneList<HUnknownOSRValue*>* osr_values = |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4104 | new(zone()) ZoneList<HUnknownOSRValue*>(length, zone()); |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 4105 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 4106 | for (int i = 0; i < first_expression_index; ++i) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 4107 | HUnknownOSRValue* osr_value = new(zone()) HUnknownOSRValue; |
| 4108 | AddInstruction(osr_value); |
| 4109 | environment()->Bind(i, osr_value); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4110 | osr_values->Add(osr_value, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 4113 | if (first_expression_index != length) { |
| 4114 | environment()->Drop(length - first_expression_index); |
| 4115 | for (int i = first_expression_index; i < length; ++i) { |
| 4116 | HUnknownOSRValue* osr_value = new(zone()) HUnknownOSRValue; |
| 4117 | AddInstruction(osr_value); |
| 4118 | environment()->Push(osr_value); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4119 | osr_values->Add(osr_value, zone()); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 4120 | } |
| 4121 | } |
| 4122 | |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 4123 | graph()->set_osr_values(osr_values); |
| 4124 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 4125 | AddSimulate(osr_entry_id); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4126 | AddInstruction(new(zone()) HOsrEntry(osr_entry_id)); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 4127 | HContext* context = new(zone()) HContext; |
| 4128 | AddInstruction(context); |
| 4129 | environment()->BindContext(context); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 4130 | current_block()->Goto(loop_predecessor); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4131 | loop_predecessor->SetJoinId(statement->EntryId()); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 4132 | set_current_block(loop_predecessor); |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 4133 | return true; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 4137 | void HGraphBuilder::VisitLoopBody(IterationStatement* stmt, |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4138 | HBasicBlock* loop_entry, |
| 4139 | BreakAndContinueInfo* break_info) { |
| 4140 | BreakAndContinueScope push(break_info, this); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 4141 | AddSimulate(stmt->StackCheckId()); |
| 4142 | HValue* context = environment()->LookupContext(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4143 | HStackCheck* stack_check = |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 4144 | new(zone()) HStackCheck(context, HStackCheck::kBackwardsBranch); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4145 | AddInstruction(stack_check); |
| 4146 | ASSERT(loop_entry->IsLoopHeader()); |
| 4147 | loop_entry->loop_information()->set_stack_check(stack_check); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 4148 | CHECK_BAILOUT(Visit(stmt->body())); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
| 4151 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4152 | void HGraphBuilder::VisitDoWhileStatement(DoWhileStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4153 | ASSERT(!HasStackOverflow()); |
| 4154 | ASSERT(current_block() != NULL); |
| 4155 | ASSERT(current_block()->HasPredecessor()); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 4156 | ASSERT(current_block() != NULL); |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 4157 | bool osr_entry = PreProcessOsrEntry(stmt); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4158 | HBasicBlock* loop_entry = CreateLoopHeaderBlock(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4159 | current_block()->Goto(loop_entry); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4160 | set_current_block(loop_entry); |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 4161 | if (osr_entry) graph()->set_osr_loop_entry(loop_entry); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4162 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 4163 | BreakAndContinueInfo break_info(stmt); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 4164 | CHECK_BAILOUT(VisitLoopBody(stmt, loop_entry, &break_info)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4165 | HBasicBlock* body_exit = |
| 4166 | JoinContinue(stmt, current_block(), break_info.continue_block()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4167 | HBasicBlock* loop_successor = NULL; |
| 4168 | if (body_exit != NULL && !stmt->cond()->ToBooleanIsTrue()) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4169 | set_current_block(body_exit); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4170 | // The block for a true condition, the actual predecessor block of the |
| 4171 | // back edge. |
| 4172 | body_exit = graph()->CreateBasicBlock(); |
| 4173 | loop_successor = graph()->CreateBasicBlock(); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4174 | CHECK_BAILOUT(VisitForControl(stmt->cond(), body_exit, loop_successor)); |
| 4175 | if (body_exit->HasPredecessor()) { |
| 4176 | body_exit->SetJoinId(stmt->BackEdgeId()); |
| 4177 | } else { |
| 4178 | body_exit = NULL; |
| 4179 | } |
| 4180 | if (loop_successor->HasPredecessor()) { |
| 4181 | loop_successor->SetJoinId(stmt->ExitId()); |
| 4182 | } else { |
| 4183 | loop_successor = NULL; |
| 4184 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4185 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4186 | HBasicBlock* loop_exit = CreateLoop(stmt, |
| 4187 | loop_entry, |
| 4188 | body_exit, |
| 4189 | loop_successor, |
| 4190 | break_info.break_block()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4191 | set_current_block(loop_exit); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
| 4194 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4195 | void HGraphBuilder::VisitWhileStatement(WhileStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4196 | ASSERT(!HasStackOverflow()); |
| 4197 | ASSERT(current_block() != NULL); |
| 4198 | ASSERT(current_block()->HasPredecessor()); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 4199 | ASSERT(current_block() != NULL); |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 4200 | bool osr_entry = PreProcessOsrEntry(stmt); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4201 | HBasicBlock* loop_entry = CreateLoopHeaderBlock(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4202 | current_block()->Goto(loop_entry); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4203 | set_current_block(loop_entry); |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 4204 | if (osr_entry) graph()->set_osr_loop_entry(loop_entry); |
| 4205 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4206 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4207 | // If the condition is constant true, do not generate a branch. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4208 | HBasicBlock* loop_successor = NULL; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4209 | if (!stmt->cond()->ToBooleanIsTrue()) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4210 | HBasicBlock* body_entry = graph()->CreateBasicBlock(); |
| 4211 | loop_successor = graph()->CreateBasicBlock(); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4212 | CHECK_BAILOUT(VisitForControl(stmt->cond(), body_entry, loop_successor)); |
| 4213 | if (body_entry->HasPredecessor()) { |
| 4214 | body_entry->SetJoinId(stmt->BodyId()); |
| 4215 | set_current_block(body_entry); |
| 4216 | } |
| 4217 | if (loop_successor->HasPredecessor()) { |
| 4218 | loop_successor->SetJoinId(stmt->ExitId()); |
| 4219 | } else { |
| 4220 | loop_successor = NULL; |
| 4221 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4222 | } |
| 4223 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 4224 | BreakAndContinueInfo break_info(stmt); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4225 | if (current_block() != NULL) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 4226 | CHECK_BAILOUT(VisitLoopBody(stmt, loop_entry, &break_info)); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 4227 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4228 | HBasicBlock* body_exit = |
| 4229 | JoinContinue(stmt, current_block(), break_info.continue_block()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4230 | HBasicBlock* loop_exit = CreateLoop(stmt, |
| 4231 | loop_entry, |
| 4232 | body_exit, |
| 4233 | loop_successor, |
| 4234 | break_info.break_block()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4235 | set_current_block(loop_exit); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4236 | } |
| 4237 | |
| 4238 | |
| 4239 | void HGraphBuilder::VisitForStatement(ForStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4240 | ASSERT(!HasStackOverflow()); |
| 4241 | ASSERT(current_block() != NULL); |
| 4242 | ASSERT(current_block()->HasPredecessor()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4243 | if (stmt->init() != NULL) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4244 | CHECK_ALIVE(Visit(stmt->init())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4245 | } |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 4246 | ASSERT(current_block() != NULL); |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 4247 | bool osr_entry = PreProcessOsrEntry(stmt); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4248 | HBasicBlock* loop_entry = CreateLoopHeaderBlock(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4249 | current_block()->Goto(loop_entry); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4250 | set_current_block(loop_entry); |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 4251 | if (osr_entry) graph()->set_osr_loop_entry(loop_entry); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4252 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4253 | HBasicBlock* loop_successor = NULL; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4254 | if (stmt->cond() != NULL) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4255 | HBasicBlock* body_entry = graph()->CreateBasicBlock(); |
| 4256 | loop_successor = graph()->CreateBasicBlock(); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4257 | CHECK_BAILOUT(VisitForControl(stmt->cond(), body_entry, loop_successor)); |
| 4258 | if (body_entry->HasPredecessor()) { |
| 4259 | body_entry->SetJoinId(stmt->BodyId()); |
| 4260 | set_current_block(body_entry); |
| 4261 | } |
| 4262 | if (loop_successor->HasPredecessor()) { |
| 4263 | loop_successor->SetJoinId(stmt->ExitId()); |
| 4264 | } else { |
| 4265 | loop_successor = NULL; |
| 4266 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4267 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4268 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 4269 | BreakAndContinueInfo break_info(stmt); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4270 | if (current_block() != NULL) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 4271 | CHECK_BAILOUT(VisitLoopBody(stmt, loop_entry, &break_info)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4272 | } |
| 4273 | HBasicBlock* body_exit = |
| 4274 | JoinContinue(stmt, current_block(), break_info.continue_block()); |
| 4275 | |
| 4276 | if (stmt->next() != NULL && body_exit != NULL) { |
| 4277 | set_current_block(body_exit); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4278 | CHECK_BAILOUT(Visit(stmt->next())); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4279 | body_exit = current_block(); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 4280 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4281 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4282 | HBasicBlock* loop_exit = CreateLoop(stmt, |
| 4283 | loop_entry, |
| 4284 | body_exit, |
| 4285 | loop_successor, |
| 4286 | break_info.break_block()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4287 | set_current_block(loop_exit); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | |
| 4291 | void HGraphBuilder::VisitForInStatement(ForInStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4292 | ASSERT(!HasStackOverflow()); |
| 4293 | ASSERT(current_block() != NULL); |
| 4294 | ASSERT(current_block()->HasPredecessor()); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 4295 | |
yangguo@chromium.org | a7d3df9 | 2012-02-27 11:46:55 +0000 | [diff] [blame] | 4296 | if (!FLAG_optimize_for_in) { |
| 4297 | return Bailout("ForInStatement optimization is disabled"); |
| 4298 | } |
| 4299 | |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 4300 | if (!oracle()->IsForInFastCase(stmt)) { |
| 4301 | return Bailout("ForInStatement is not fast case"); |
| 4302 | } |
| 4303 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 4304 | if (!stmt->each()->IsVariableProxy() || |
| 4305 | !stmt->each()->AsVariableProxy()->var()->IsStackLocal()) { |
| 4306 | return Bailout("ForInStatement with non-local each variable"); |
| 4307 | } |
| 4308 | |
| 4309 | Variable* each_var = stmt->each()->AsVariableProxy()->var(); |
| 4310 | |
| 4311 | CHECK_ALIVE(VisitForValue(stmt->enumerable())); |
| 4312 | HValue* enumerable = Top(); // Leave enumerable at the top. |
| 4313 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 4314 | HInstruction* map = AddInstruction(new(zone()) HForInPrepareMap( |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 4315 | environment()->LookupContext(), enumerable)); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 4316 | AddSimulate(stmt->PrepareId()); |
| 4317 | |
| 4318 | HInstruction* array = AddInstruction( |
| 4319 | new(zone()) HForInCacheArray( |
| 4320 | enumerable, |
| 4321 | map, |
| 4322 | DescriptorArray::kEnumCacheBridgeCacheIndex)); |
| 4323 | |
| 4324 | HInstruction* array_length = AddInstruction( |
| 4325 | new(zone()) HFixedArrayBaseLength(array)); |
| 4326 | |
| 4327 | HInstruction* start_index = AddInstruction(new(zone()) HConstant( |
| 4328 | Handle<Object>(Smi::FromInt(0)), Representation::Integer32())); |
| 4329 | |
| 4330 | Push(map); |
| 4331 | Push(array); |
| 4332 | Push(array_length); |
| 4333 | Push(start_index); |
| 4334 | |
| 4335 | HInstruction* index_cache = AddInstruction( |
| 4336 | new(zone()) HForInCacheArray( |
| 4337 | enumerable, |
| 4338 | map, |
| 4339 | DescriptorArray::kEnumCacheBridgeIndicesCacheIndex)); |
| 4340 | HForInCacheArray::cast(array)->set_index_cache( |
| 4341 | HForInCacheArray::cast(index_cache)); |
| 4342 | |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 4343 | bool osr_entry = PreProcessOsrEntry(stmt); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 4344 | HBasicBlock* loop_entry = CreateLoopHeaderBlock(); |
| 4345 | current_block()->Goto(loop_entry); |
| 4346 | set_current_block(loop_entry); |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 4347 | if (osr_entry) graph()->set_osr_loop_entry(loop_entry); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 4348 | |
| 4349 | HValue* index = environment()->ExpressionStackAt(0); |
| 4350 | HValue* limit = environment()->ExpressionStackAt(1); |
| 4351 | |
| 4352 | // Check that we still have more keys. |
| 4353 | HCompareIDAndBranch* compare_index = |
| 4354 | new(zone()) HCompareIDAndBranch(index, limit, Token::LT); |
| 4355 | compare_index->SetInputRepresentation(Representation::Integer32()); |
| 4356 | |
| 4357 | HBasicBlock* loop_body = graph()->CreateBasicBlock(); |
| 4358 | HBasicBlock* loop_successor = graph()->CreateBasicBlock(); |
| 4359 | |
| 4360 | compare_index->SetSuccessorAt(0, loop_body); |
| 4361 | compare_index->SetSuccessorAt(1, loop_successor); |
| 4362 | current_block()->Finish(compare_index); |
| 4363 | |
| 4364 | set_current_block(loop_successor); |
| 4365 | Drop(5); |
| 4366 | |
| 4367 | set_current_block(loop_body); |
| 4368 | |
| 4369 | HValue* key = AddInstruction( |
| 4370 | new(zone()) HLoadKeyedFastElement( |
| 4371 | environment()->ExpressionStackAt(2), // Enum cache. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4372 | environment()->ExpressionStackAt(0))); // Iteration index. |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 4373 | |
| 4374 | // Check if the expected map still matches that of the enumerable. |
| 4375 | // If not just deoptimize. |
| 4376 | AddInstruction(new(zone()) HCheckMapValue( |
| 4377 | environment()->ExpressionStackAt(4), |
| 4378 | environment()->ExpressionStackAt(3))); |
| 4379 | |
| 4380 | Bind(each_var, key); |
| 4381 | |
| 4382 | BreakAndContinueInfo break_info(stmt, 5); |
| 4383 | CHECK_BAILOUT(VisitLoopBody(stmt, loop_entry, &break_info)); |
| 4384 | |
| 4385 | HBasicBlock* body_exit = |
| 4386 | JoinContinue(stmt, current_block(), break_info.continue_block()); |
| 4387 | |
| 4388 | if (body_exit != NULL) { |
| 4389 | set_current_block(body_exit); |
| 4390 | |
| 4391 | HValue* current_index = Pop(); |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 4392 | HInstruction* new_index = new(zone()) HAdd(environment()->LookupContext(), |
| 4393 | current_index, |
| 4394 | graph()->GetConstant1()); |
| 4395 | new_index->AssumeRepresentation(Representation::Integer32()); |
| 4396 | PushAndAdd(new_index); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 4397 | body_exit = current_block(); |
| 4398 | } |
| 4399 | |
| 4400 | HBasicBlock* loop_exit = CreateLoop(stmt, |
| 4401 | loop_entry, |
| 4402 | body_exit, |
| 4403 | loop_successor, |
| 4404 | break_info.break_block()); |
| 4405 | |
| 4406 | set_current_block(loop_exit); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4407 | } |
| 4408 | |
| 4409 | |
| 4410 | void HGraphBuilder::VisitTryCatchStatement(TryCatchStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4411 | ASSERT(!HasStackOverflow()); |
| 4412 | ASSERT(current_block() != NULL); |
| 4413 | ASSERT(current_block()->HasPredecessor()); |
| 4414 | return Bailout("TryCatchStatement"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4415 | } |
| 4416 | |
| 4417 | |
| 4418 | void HGraphBuilder::VisitTryFinallyStatement(TryFinallyStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4419 | ASSERT(!HasStackOverflow()); |
| 4420 | ASSERT(current_block() != NULL); |
| 4421 | ASSERT(current_block()->HasPredecessor()); |
| 4422 | return Bailout("TryFinallyStatement"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4423 | } |
| 4424 | |
| 4425 | |
| 4426 | void HGraphBuilder::VisitDebuggerStatement(DebuggerStatement* stmt) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4427 | ASSERT(!HasStackOverflow()); |
| 4428 | ASSERT(current_block() != NULL); |
| 4429 | ASSERT(current_block()->HasPredecessor()); |
| 4430 | return Bailout("DebuggerStatement"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4431 | } |
| 4432 | |
| 4433 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4434 | static Handle<SharedFunctionInfo> SearchSharedFunctionInfo( |
| 4435 | Code* unoptimized_code, FunctionLiteral* expr) { |
| 4436 | int start_position = expr->start_position(); |
| 4437 | RelocIterator it(unoptimized_code); |
| 4438 | for (;!it.done(); it.next()) { |
| 4439 | RelocInfo* rinfo = it.rinfo(); |
| 4440 | if (rinfo->rmode() != RelocInfo::EMBEDDED_OBJECT) continue; |
| 4441 | Object* obj = rinfo->target_object(); |
| 4442 | if (obj->IsSharedFunctionInfo()) { |
| 4443 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(obj); |
| 4444 | if (shared->start_position() == start_position) { |
| 4445 | return Handle<SharedFunctionInfo>(shared); |
| 4446 | } |
| 4447 | } |
| 4448 | } |
| 4449 | |
| 4450 | return Handle<SharedFunctionInfo>(); |
| 4451 | } |
| 4452 | |
| 4453 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4454 | void HGraphBuilder::VisitFunctionLiteral(FunctionLiteral* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4455 | ASSERT(!HasStackOverflow()); |
| 4456 | ASSERT(current_block() != NULL); |
| 4457 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4458 | Handle<SharedFunctionInfo> shared_info = |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4459 | SearchSharedFunctionInfo(info()->shared_info()->code(), |
| 4460 | expr); |
| 4461 | if (shared_info.is_null()) { |
| 4462 | shared_info = Compiler::BuildFunctionInfo(expr, info()->script()); |
| 4463 | } |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4464 | // We also have a stack overflow if the recursive compilation did. |
| 4465 | if (HasStackOverflow()) return; |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 4466 | HValue* context = environment()->LookupContext(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4467 | HFunctionLiteral* instr = |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 4468 | new(zone()) HFunctionLiteral(context, shared_info, expr->pretenure()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4469 | return ast_context()->ReturnInstruction(instr, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4470 | } |
| 4471 | |
| 4472 | |
| 4473 | void HGraphBuilder::VisitSharedFunctionInfoLiteral( |
| 4474 | SharedFunctionInfoLiteral* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4475 | ASSERT(!HasStackOverflow()); |
| 4476 | ASSERT(current_block() != NULL); |
| 4477 | ASSERT(current_block()->HasPredecessor()); |
| 4478 | return Bailout("SharedFunctionInfoLiteral"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
| 4481 | |
| 4482 | void HGraphBuilder::VisitConditional(Conditional* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4483 | ASSERT(!HasStackOverflow()); |
| 4484 | ASSERT(current_block() != NULL); |
| 4485 | ASSERT(current_block()->HasPredecessor()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4486 | HBasicBlock* cond_true = graph()->CreateBasicBlock(); |
| 4487 | HBasicBlock* cond_false = graph()->CreateBasicBlock(); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4488 | CHECK_BAILOUT(VisitForControl(expr->condition(), cond_true, cond_false)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4489 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4490 | // Visit the true and false subexpressions in the same AST context as the |
| 4491 | // whole expression. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4492 | if (cond_true->HasPredecessor()) { |
| 4493 | cond_true->SetJoinId(expr->ThenId()); |
| 4494 | set_current_block(cond_true); |
| 4495 | CHECK_BAILOUT(Visit(expr->then_expression())); |
| 4496 | cond_true = current_block(); |
| 4497 | } else { |
| 4498 | cond_true = NULL; |
| 4499 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4500 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4501 | if (cond_false->HasPredecessor()) { |
| 4502 | cond_false->SetJoinId(expr->ElseId()); |
| 4503 | set_current_block(cond_false); |
| 4504 | CHECK_BAILOUT(Visit(expr->else_expression())); |
| 4505 | cond_false = current_block(); |
| 4506 | } else { |
| 4507 | cond_false = NULL; |
| 4508 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4509 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4510 | if (!ast_context()->IsTest()) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4511 | HBasicBlock* join = CreateJoin(cond_true, cond_false, expr->id()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4512 | set_current_block(join); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4513 | if (join != NULL && !ast_context()->IsEffect()) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4514 | return ast_context()->ReturnValue(Pop()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4515 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4516 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4517 | } |
| 4518 | |
| 4519 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4520 | HGraphBuilder::GlobalPropertyAccess HGraphBuilder::LookupGlobalProperty( |
| 4521 | Variable* var, LookupResult* lookup, bool is_store) { |
| 4522 | if (var->is_this() || !info()->has_global_object()) { |
| 4523 | return kUseGeneric; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4524 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4525 | Handle<GlobalObject> global(info()->global_object()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4526 | global->Lookup(*var->name(), lookup); |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 4527 | if (!lookup->IsNormal() || |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4528 | (is_store && lookup->IsReadOnly()) || |
| 4529 | lookup->holder() != *global) { |
| 4530 | return kUseGeneric; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4531 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4532 | |
| 4533 | return kUseCell; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
| 4536 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4537 | HValue* HGraphBuilder::BuildContextChainWalk(Variable* var) { |
| 4538 | ASSERT(var->IsContextSlot()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 4539 | HValue* context = environment()->LookupContext(); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4540 | int length = info()->scope()->ContextChainLength(var->scope()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4541 | while (length-- > 0) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 4542 | HInstruction* context_instruction = new(zone()) HOuterContext(context); |
| 4543 | AddInstruction(context_instruction); |
| 4544 | context = context_instruction; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4545 | } |
| 4546 | return context; |
| 4547 | } |
| 4548 | |
| 4549 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4550 | void HGraphBuilder::VisitVariableProxy(VariableProxy* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4551 | ASSERT(!HasStackOverflow()); |
| 4552 | ASSERT(current_block() != NULL); |
| 4553 | ASSERT(current_block()->HasPredecessor()); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 4554 | Variable* variable = expr->var(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 4555 | switch (variable->location()) { |
| 4556 | case Variable::UNALLOCATED: { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4557 | if (variable->mode() == LET || variable->mode() == CONST_HARMONY) { |
| 4558 | return Bailout("reference to global harmony declared variable"); |
| 4559 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4560 | // Handle known global constants like 'undefined' specially to avoid a |
| 4561 | // load from a global cell for them. |
| 4562 | Handle<Object> constant_value = |
| 4563 | isolate()->factory()->GlobalConstantFor(variable->name()); |
| 4564 | if (!constant_value.is_null()) { |
| 4565 | HConstant* instr = |
| 4566 | new(zone()) HConstant(constant_value, Representation::Tagged()); |
| 4567 | return ast_context()->ReturnInstruction(instr, expr->id()); |
| 4568 | } |
| 4569 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4570 | LookupResult lookup(isolate()); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 4571 | GlobalPropertyAccess type = |
| 4572 | LookupGlobalProperty(variable, &lookup, false); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4573 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 4574 | if (type == kUseCell && |
| 4575 | info()->global_object()->IsAccessCheckNeeded()) { |
| 4576 | type = kUseGeneric; |
| 4577 | } |
| 4578 | |
| 4579 | if (type == kUseCell) { |
| 4580 | Handle<GlobalObject> global(info()->global_object()); |
| 4581 | Handle<JSGlobalPropertyCell> cell(global->GetPropertyCell(&lookup)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4582 | HLoadGlobalCell* instr = |
| 4583 | new(zone()) HLoadGlobalCell(cell, lookup.GetPropertyDetails()); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 4584 | return ast_context()->ReturnInstruction(instr, expr->id()); |
| 4585 | } else { |
| 4586 | HValue* context = environment()->LookupContext(); |
| 4587 | HGlobalObject* global_object = new(zone()) HGlobalObject(context); |
| 4588 | AddInstruction(global_object); |
| 4589 | HLoadGlobalGeneric* instr = |
| 4590 | new(zone()) HLoadGlobalGeneric(context, |
| 4591 | global_object, |
| 4592 | variable->name(), |
| 4593 | ast_context()->is_for_typeof()); |
| 4594 | instr->set_position(expr->position()); |
| 4595 | return ast_context()->ReturnInstruction(instr, expr->id()); |
| 4596 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4597 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4598 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 4599 | case Variable::PARAMETER: |
| 4600 | case Variable::LOCAL: { |
| 4601 | HValue* value = environment()->Lookup(variable); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4602 | if (value == graph()->GetConstantHole()) { |
| 4603 | ASSERT(variable->mode() == CONST || |
| 4604 | variable->mode() == CONST_HARMONY || |
| 4605 | variable->mode() == LET); |
| 4606 | return Bailout("reference to uninitialized variable"); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 4607 | } |
| 4608 | return ast_context()->ReturnValue(value); |
| 4609 | } |
| 4610 | |
| 4611 | case Variable::CONTEXT: { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 4612 | HValue* context = BuildContextChainWalk(variable); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4613 | HLoadContextSlot* instr = new(zone()) HLoadContextSlot(context, variable); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4614 | return ast_context()->ReturnInstruction(instr, expr->id()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4615 | } |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 4616 | |
| 4617 | case Variable::LOOKUP: |
| 4618 | return Bailout("reference to a variable which requires dynamic lookup"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4619 | } |
| 4620 | } |
| 4621 | |
| 4622 | |
| 4623 | void HGraphBuilder::VisitLiteral(Literal* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4624 | ASSERT(!HasStackOverflow()); |
| 4625 | ASSERT(current_block() != NULL); |
| 4626 | ASSERT(current_block()->HasPredecessor()); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4627 | HConstant* instr = |
| 4628 | new(zone()) HConstant(expr->handle(), Representation::Tagged()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4629 | return ast_context()->ReturnInstruction(instr, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
| 4632 | |
| 4633 | void HGraphBuilder::VisitRegExpLiteral(RegExpLiteral* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4634 | ASSERT(!HasStackOverflow()); |
| 4635 | ASSERT(current_block() != NULL); |
| 4636 | ASSERT(current_block()->HasPredecessor()); |
yangguo@chromium.org | 9c741c8 | 2012-06-28 15:04:22 +0000 | [diff] [blame] | 4637 | Handle<JSFunction> closure = function_state()->compilation_info()->closure(); |
| 4638 | Handle<FixedArray> literals(closure->literals()); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 4639 | HValue* context = environment()->LookupContext(); |
| 4640 | |
| 4641 | HRegExpLiteral* instr = new(zone()) HRegExpLiteral(context, |
yangguo@chromium.org | 9c741c8 | 2012-06-28 15:04:22 +0000 | [diff] [blame] | 4642 | literals, |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 4643 | expr->pattern(), |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4644 | expr->flags(), |
| 4645 | expr->literal_index()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4646 | return ast_context()->ReturnInstruction(instr, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4647 | } |
| 4648 | |
| 4649 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4650 | // Determines whether the given array or object literal boilerplate satisfies |
| 4651 | // all limits to be considered for fast deep-copying and computes the total |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4652 | // size of all objects that are part of the graph. |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4653 | static bool IsFastLiteral(Handle<JSObject> boilerplate, |
| 4654 | int max_depth, |
| 4655 | int* max_properties, |
| 4656 | int* total_size) { |
| 4657 | ASSERT(max_depth >= 0 && *max_properties >= 0); |
| 4658 | if (max_depth == 0) return false; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4659 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4660 | Handle<FixedArrayBase> elements(boilerplate->elements()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4661 | if (elements->length() > 0 && |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4662 | elements->map() != boilerplate->GetHeap()->fixed_cow_array_map()) { |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 4663 | if (boilerplate->HasFastDoubleElements()) { |
| 4664 | *total_size += FixedDoubleArray::SizeFor(elements->length()); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 4665 | } else if (boilerplate->HasFastObjectElements()) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 4666 | Handle<FixedArray> fast_elements = Handle<FixedArray>::cast(elements); |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 4667 | int length = elements->length(); |
| 4668 | for (int i = 0; i < length; i++) { |
| 4669 | if ((*max_properties)-- == 0) return false; |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 4670 | Handle<Object> value(fast_elements->get(i)); |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 4671 | if (value->IsJSObject()) { |
| 4672 | Handle<JSObject> value_object = Handle<JSObject>::cast(value); |
| 4673 | if (!IsFastLiteral(value_object, |
| 4674 | max_depth - 1, |
| 4675 | max_properties, |
| 4676 | total_size)) { |
| 4677 | return false; |
| 4678 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4679 | } |
| 4680 | } |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 4681 | *total_size += FixedArray::SizeFor(length); |
| 4682 | } else { |
| 4683 | return false; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4684 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4687 | Handle<FixedArray> properties(boilerplate->properties()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4688 | if (properties->length() > 0) { |
| 4689 | return false; |
| 4690 | } else { |
| 4691 | int nof = boilerplate->map()->inobject_properties(); |
| 4692 | for (int i = 0; i < nof; i++) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4693 | if ((*max_properties)-- == 0) return false; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4694 | Handle<Object> value(boilerplate->InObjectPropertyAt(i)); |
| 4695 | if (value->IsJSObject()) { |
| 4696 | Handle<JSObject> value_object = Handle<JSObject>::cast(value); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4697 | if (!IsFastLiteral(value_object, |
| 4698 | max_depth - 1, |
| 4699 | max_properties, |
| 4700 | total_size)) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4701 | return false; |
| 4702 | } |
| 4703 | } |
| 4704 | } |
| 4705 | } |
| 4706 | |
| 4707 | *total_size += boilerplate->map()->instance_size(); |
| 4708 | return true; |
| 4709 | } |
| 4710 | |
| 4711 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4712 | void HGraphBuilder::VisitObjectLiteral(ObjectLiteral* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4713 | ASSERT(!HasStackOverflow()); |
| 4714 | ASSERT(current_block() != NULL); |
| 4715 | ASSERT(current_block()->HasPredecessor()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4716 | Handle<JSFunction> closure = function_state()->compilation_info()->closure(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 4717 | HValue* context = environment()->LookupContext(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4718 | HInstruction* literal; |
| 4719 | |
| 4720 | // Check whether to use fast or slow deep-copying for boilerplate. |
| 4721 | int total_size = 0; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4722 | int max_properties = HFastLiteral::kMaxLiteralProperties; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4723 | Handle<Object> boilerplate(closure->literals()->get(expr->literal_index())); |
| 4724 | if (boilerplate->IsJSObject() && |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4725 | IsFastLiteral(Handle<JSObject>::cast(boilerplate), |
| 4726 | HFastLiteral::kMaxLiteralDepth, |
| 4727 | &max_properties, |
| 4728 | &total_size)) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4729 | Handle<JSObject> boilerplate_object = Handle<JSObject>::cast(boilerplate); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4730 | literal = new(zone()) HFastLiteral(context, |
| 4731 | boilerplate_object, |
| 4732 | total_size, |
| 4733 | expr->literal_index(), |
| 4734 | expr->depth()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4735 | } else { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4736 | literal = new(zone()) HObjectLiteral(context, |
| 4737 | expr->constant_properties(), |
| 4738 | expr->fast_elements(), |
| 4739 | expr->literal_index(), |
| 4740 | expr->depth(), |
| 4741 | expr->has_function()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4742 | } |
| 4743 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4744 | // The object is expected in the bailout environment during computation |
| 4745 | // 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] | 4746 | PushAndAdd(literal); |
| 4747 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4748 | expr->CalculateEmitStore(zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4749 | |
| 4750 | for (int i = 0; i < expr->properties()->length(); i++) { |
| 4751 | ObjectLiteral::Property* property = expr->properties()->at(i); |
| 4752 | if (property->IsCompileTimeValue()) continue; |
| 4753 | |
| 4754 | Literal* key = property->key(); |
| 4755 | Expression* value = property->value(); |
| 4756 | |
| 4757 | switch (property->kind()) { |
| 4758 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 4759 | ASSERT(!CompileTimeValue::IsCompileTimeValue(value)); |
| 4760 | // Fall through. |
| 4761 | case ObjectLiteral::Property::COMPUTED: |
| 4762 | if (key->handle()->IsSymbol()) { |
| 4763 | if (property->emit_store()) { |
mstarzinger@chromium.org | 3233d2f | 2012-03-14 11:16:03 +0000 | [diff] [blame] | 4764 | property->RecordTypeFeedback(oracle()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4765 | CHECK_ALIVE(VisitForValue(value)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4766 | HValue* value = Pop(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4767 | HInstruction* store; |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 4768 | CHECK_ALIVE(store = BuildStoreNamed(literal, |
| 4769 | value, |
| 4770 | property->GetReceiverType(), |
| 4771 | property->key())); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4772 | AddInstruction(store); |
mstarzinger@chromium.org | 3233d2f | 2012-03-14 11:16:03 +0000 | [diff] [blame] | 4773 | if (store->HasObservableSideEffects()) AddSimulate(key->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4774 | } else { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4775 | CHECK_ALIVE(VisitForEffect(value)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4776 | } |
| 4777 | break; |
| 4778 | } |
| 4779 | // Fall through. |
| 4780 | case ObjectLiteral::Property::PROTOTYPE: |
| 4781 | case ObjectLiteral::Property::SETTER: |
| 4782 | case ObjectLiteral::Property::GETTER: |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4783 | return Bailout("Object literal with complex property"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4784 | default: UNREACHABLE(); |
| 4785 | } |
| 4786 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4787 | |
| 4788 | if (expr->has_function()) { |
| 4789 | // Return the result of the transformation to fast properties |
| 4790 | // instead of the original since this operation changes the map |
| 4791 | // of the object. This makes sure that the original object won't |
| 4792 | // be used by other optimized code before it is transformed |
| 4793 | // (e.g. because of code motion). |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4794 | HToFastProperties* result = new(zone()) HToFastProperties(Pop()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4795 | AddInstruction(result); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4796 | return ast_context()->ReturnValue(result); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4797 | } else { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4798 | return ast_context()->ReturnValue(Pop()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4799 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4800 | } |
| 4801 | |
| 4802 | |
| 4803 | void HGraphBuilder::VisitArrayLiteral(ArrayLiteral* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4804 | ASSERT(!HasStackOverflow()); |
| 4805 | ASSERT(current_block() != NULL); |
| 4806 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4807 | ZoneList<Expression*>* subexprs = expr->values(); |
| 4808 | int length = subexprs->length(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 4809 | HValue* context = environment()->LookupContext(); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4810 | HInstruction* literal; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4811 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4812 | Handle<FixedArray> literals(environment()->closure()->literals()); |
| 4813 | Handle<Object> raw_boilerplate(literals->get(expr->literal_index())); |
| 4814 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4815 | if (raw_boilerplate->IsUndefined()) { |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 4816 | raw_boilerplate = Runtime::CreateArrayLiteralBoilerplate( |
| 4817 | isolate(), literals, expr->constant_elements()); |
| 4818 | if (raw_boilerplate.is_null()) { |
| 4819 | return Bailout("array boilerplate creation failed"); |
| 4820 | } |
| 4821 | literals->set(expr->literal_index(), *raw_boilerplate); |
| 4822 | if (JSObject::cast(*raw_boilerplate)->elements()->map() == |
| 4823 | isolate()->heap()->fixed_cow_array_map()) { |
| 4824 | isolate()->counters()->cow_arrays_created_runtime()->Increment(); |
| 4825 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4826 | } |
| 4827 | |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 4828 | Handle<JSObject> boilerplate = Handle<JSObject>::cast(raw_boilerplate); |
| 4829 | ElementsKind boilerplate_elements_kind = |
| 4830 | Handle<JSObject>::cast(boilerplate)->GetElementsKind(); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4831 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4832 | // Check whether to use fast or slow deep-copying for boilerplate. |
| 4833 | int total_size = 0; |
| 4834 | int max_properties = HFastLiteral::kMaxLiteralProperties; |
| 4835 | if (IsFastLiteral(boilerplate, |
| 4836 | HFastLiteral::kMaxLiteralDepth, |
| 4837 | &max_properties, |
| 4838 | &total_size)) { |
| 4839 | literal = new(zone()) HFastLiteral(context, |
| 4840 | boilerplate, |
| 4841 | total_size, |
| 4842 | expr->literal_index(), |
| 4843 | expr->depth()); |
| 4844 | } else { |
| 4845 | literal = new(zone()) HArrayLiteral(context, |
| 4846 | boilerplate, |
| 4847 | length, |
| 4848 | expr->literal_index(), |
| 4849 | expr->depth()); |
| 4850 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4851 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4852 | // The array is expected in the bailout environment during computation |
| 4853 | // 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] | 4854 | PushAndAdd(literal); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4855 | |
| 4856 | HLoadElements* elements = NULL; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4857 | |
| 4858 | for (int i = 0; i < length; i++) { |
| 4859 | Expression* subexpr = subexprs->at(i); |
| 4860 | // If the subexpression is a literal or a simple materialized literal it |
| 4861 | // is already set in the cloned array. |
| 4862 | if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue; |
| 4863 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4864 | CHECK_ALIVE(VisitForValue(subexpr)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4865 | HValue* value = Pop(); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 4866 | 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] | 4867 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4868 | elements = new(zone()) HLoadElements(literal); |
| 4869 | AddInstruction(elements); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4870 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4871 | HValue* key = AddInstruction( |
| 4872 | new(zone()) HConstant(Handle<Object>(Smi::FromInt(i)), |
| 4873 | Representation::Integer32())); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4874 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4875 | switch (boilerplate_elements_kind) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 4876 | case FAST_SMI_ELEMENTS: |
| 4877 | case FAST_HOLEY_SMI_ELEMENTS: |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 4878 | // Smi-only arrays need a smi check. |
| 4879 | AddInstruction(new(zone()) HCheckSmi(value)); |
| 4880 | // Fall through. |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4881 | case FAST_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 4882 | case FAST_HOLEY_ELEMENTS: |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4883 | AddInstruction(new(zone()) HStoreKeyedFastElement( |
| 4884 | elements, |
| 4885 | key, |
| 4886 | value, |
| 4887 | boilerplate_elements_kind)); |
| 4888 | break; |
| 4889 | case FAST_DOUBLE_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 4890 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4891 | AddInstruction(new(zone()) HStoreKeyedFastDoubleElement(elements, |
| 4892 | key, |
| 4893 | value)); |
| 4894 | break; |
| 4895 | default: |
| 4896 | UNREACHABLE(); |
| 4897 | break; |
| 4898 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4899 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4900 | AddSimulate(expr->GetIdForElement(i)); |
| 4901 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4902 | return ast_context()->ReturnValue(Pop()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4903 | } |
| 4904 | |
| 4905 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 4906 | // Sets the lookup result and returns true if the load/store can be inlined. |
| 4907 | static bool ComputeLoadStoreField(Handle<Map> type, |
| 4908 | Handle<String> name, |
| 4909 | LookupResult* lookup, |
| 4910 | bool is_store) { |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 4911 | // If we directly find a field, the access can be inlined. |
| 4912 | type->LookupDescriptor(NULL, *name, lookup); |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 4913 | if (lookup->IsField()) return true; |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 4914 | |
| 4915 | // For a load, we are out of luck if there is no such field. |
| 4916 | if (!is_store) return false; |
| 4917 | |
| 4918 | // 2nd chance: A store into a non-existent field can still be inlined if we |
| 4919 | // have a matching transition and some room left in the object. |
| 4920 | type->LookupTransition(NULL, *name, lookup); |
| 4921 | return lookup->IsTransitionToField(*type) && |
| 4922 | (type->unused_property_fields() > 0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4923 | } |
| 4924 | |
| 4925 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 4926 | static int ComputeLoadStoreFieldIndex(Handle<Map> type, |
| 4927 | Handle<String> name, |
| 4928 | LookupResult* lookup) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 4929 | ASSERT(lookup->IsField() || lookup->IsTransitionToField(*type)); |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 4930 | if (lookup->IsField()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4931 | return lookup->GetLocalFieldIndexFromMap(*type); |
| 4932 | } else { |
| 4933 | Map* transition = lookup->GetTransitionMapFromMap(*type); |
| 4934 | return transition->PropertyIndexFor(*name) - type->inobject_properties(); |
| 4935 | } |
| 4936 | } |
| 4937 | |
| 4938 | |
| 4939 | HInstruction* HGraphBuilder::BuildStoreNamedField(HValue* object, |
| 4940 | Handle<String> name, |
| 4941 | HValue* value, |
| 4942 | Handle<Map> type, |
| 4943 | LookupResult* lookup, |
| 4944 | bool smi_and_map_check) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4945 | ASSERT(lookup->IsFound()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4946 | if (smi_and_map_check) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4947 | AddInstruction(new(zone()) HCheckNonSmi(object)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4948 | AddInstruction(HCheckMaps::NewWithTransitions(object, type, zone())); |
| 4949 | } |
| 4950 | |
| 4951 | // If the property does not exist yet, we have to check that it wasn't made |
| 4952 | // readonly or turned into a setter by some meanwhile modifications on the |
| 4953 | // prototype chain. |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 4954 | if (!lookup->IsProperty() && type->prototype()->IsJSReceiver()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4955 | Object* proto = type->prototype(); |
| 4956 | // First check that the prototype chain isn't affected already. |
| 4957 | LookupResult proto_result(isolate()); |
| 4958 | proto->Lookup(*name, &proto_result); |
| 4959 | if (proto_result.IsProperty()) { |
| 4960 | // If the inherited property could induce readonly-ness, bail out. |
| 4961 | if (proto_result.IsReadOnly() || !proto_result.IsCacheable()) { |
| 4962 | Bailout("improper object on prototype chain for store"); |
| 4963 | return NULL; |
| 4964 | } |
| 4965 | // We only need to check up to the preexisting property. |
| 4966 | proto = proto_result.holder(); |
| 4967 | } else { |
| 4968 | // Otherwise, find the top prototype. |
| 4969 | while (proto->GetPrototype()->IsJSObject()) proto = proto->GetPrototype(); |
| 4970 | ASSERT(proto->GetPrototype()->IsNull()); |
| 4971 | } |
| 4972 | ASSERT(proto->IsJSObject()); |
| 4973 | AddInstruction(new(zone()) HCheckPrototypeMaps( |
| 4974 | Handle<JSObject>(JSObject::cast(type->prototype())), |
| 4975 | Handle<JSObject>(JSObject::cast(proto)))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4976 | } |
| 4977 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 4978 | int index = ComputeLoadStoreFieldIndex(type, name, lookup); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4979 | bool is_in_object = index < 0; |
| 4980 | int offset = index * kPointerSize; |
| 4981 | if (index < 0) { |
| 4982 | // Negative property indices are in-object properties, indexed |
| 4983 | // from the end of the fixed part of the object. |
| 4984 | offset += type->instance_size(); |
| 4985 | } else { |
| 4986 | offset += FixedArray::kHeaderSize; |
| 4987 | } |
| 4988 | HStoreNamedField* instr = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4989 | new(zone()) HStoreNamedField(object, name, value, is_in_object, offset); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 4990 | if (lookup->IsTransitionToField(*type)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4991 | Handle<Map> transition(lookup->GetTransitionMapFromMap(*type)); |
| 4992 | instr->set_transition(transition); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 4993 | // TODO(fschneider): Record the new map type of the object in the IR to |
| 4994 | // enable elimination of redundant checks after the transition store. |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 4995 | instr->SetGVNFlag(kChangesMaps); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4996 | } |
| 4997 | return instr; |
| 4998 | } |
| 4999 | |
| 5000 | |
| 5001 | HInstruction* HGraphBuilder::BuildStoreNamedGeneric(HValue* object, |
| 5002 | Handle<String> name, |
| 5003 | HValue* value) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5004 | HValue* context = environment()->LookupContext(); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 5005 | return new(zone()) HStoreNamedGeneric( |
| 5006 | context, |
| 5007 | object, |
| 5008 | name, |
| 5009 | value, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5010 | function_strict_mode_flag()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5011 | } |
| 5012 | |
| 5013 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 5014 | static void LookupInPrototypes(Handle<Map> map, |
| 5015 | Handle<String> name, |
| 5016 | LookupResult* lookup) { |
| 5017 | while (map->prototype()->IsJSObject()) { |
| 5018 | Handle<JSObject> holder(JSObject::cast(map->prototype())); |
| 5019 | if (!holder->HasFastProperties()) break; |
| 5020 | map = Handle<Map>(holder->map()); |
| 5021 | map->LookupDescriptor(*holder, *name, lookup); |
| 5022 | if (lookup->IsFound()) return; |
| 5023 | } |
| 5024 | lookup->NotFound(); |
| 5025 | } |
| 5026 | |
| 5027 | |
| 5028 | HInstruction* HGraphBuilder::BuildCallSetter(HValue* obj, |
| 5029 | Handle<String> name, |
| 5030 | HValue* value, |
| 5031 | Handle<Map> map, |
| 5032 | Handle<Object> callback, |
| 5033 | Handle<JSObject> holder) { |
| 5034 | if (!callback->IsAccessorPair()) { |
| 5035 | return BuildStoreNamedGeneric(obj, name, value); |
| 5036 | } |
| 5037 | Handle<Object> setter(Handle<AccessorPair>::cast(callback)->setter()); |
| 5038 | Handle<JSFunction> function(Handle<JSFunction>::cast(setter)); |
| 5039 | AddCheckConstantFunction(holder, obj, map, true); |
| 5040 | AddInstruction(new(zone()) HPushArgument(obj)); |
| 5041 | AddInstruction(new(zone()) HPushArgument(value)); |
| 5042 | return new(zone()) HCallConstantFunction(function, 2); |
| 5043 | } |
| 5044 | |
| 5045 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5046 | HInstruction* HGraphBuilder::BuildStoreNamed(HValue* object, |
| 5047 | HValue* value, |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 5048 | Handle<Map> type, |
| 5049 | Expression* key) { |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 5050 | // If we don't know the monomorphic type, do a generic store. |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 5051 | Handle<String> name = Handle<String>::cast(key->AsLiteral()->handle()); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 5052 | if (type.is_null()) return BuildStoreNamedGeneric(object, name, value); |
mstarzinger@chromium.org | 3233d2f | 2012-03-14 11:16:03 +0000 | [diff] [blame] | 5053 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 5054 | // Handle a store to a known field. |
mstarzinger@chromium.org | 3233d2f | 2012-03-14 11:16:03 +0000 | [diff] [blame] | 5055 | LookupResult lookup(isolate()); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 5056 | if (ComputeLoadStoreField(type, name, &lookup, true)) { |
| 5057 | // true = needs smi and map check. |
| 5058 | return BuildStoreNamedField(object, name, value, type, &lookup, true); |
| 5059 | } |
mstarzinger@chromium.org | 3233d2f | 2012-03-14 11:16:03 +0000 | [diff] [blame] | 5060 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 5061 | // Handle a known setter directly in the receiver. |
| 5062 | type->LookupDescriptor(NULL, *name, &lookup); |
| 5063 | if (lookup.IsPropertyCallbacks()) { |
| 5064 | Handle<Object> callback(lookup.GetValueFromMap(*type)); |
| 5065 | Handle<JSObject> holder; |
| 5066 | return BuildCallSetter(object, name, value, type, callback, holder); |
| 5067 | } |
| 5068 | |
| 5069 | // Handle a known setter somewhere in the prototype chain. |
| 5070 | LookupInPrototypes(type, name, &lookup); |
| 5071 | if (lookup.IsPropertyCallbacks()) { |
| 5072 | Handle<Object> callback(lookup.GetValue()); |
| 5073 | Handle<JSObject> holder(lookup.holder()); |
| 5074 | return BuildCallSetter(object, name, value, type, callback, holder); |
| 5075 | } |
| 5076 | |
| 5077 | // No luck, do a generic store. |
| 5078 | return BuildStoreNamedGeneric(object, name, value); |
mstarzinger@chromium.org | 3233d2f | 2012-03-14 11:16:03 +0000 | [diff] [blame] | 5079 | } |
| 5080 | |
| 5081 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 5082 | void HGraphBuilder::HandlePolymorphicLoadNamedField(Property* expr, |
| 5083 | HValue* object, |
| 5084 | SmallMapList* types, |
| 5085 | Handle<String> name) { |
| 5086 | int count = 0; |
| 5087 | int previous_field_offset = 0; |
| 5088 | bool previous_field_is_in_object = false; |
| 5089 | bool is_monomorphic_field = true; |
| 5090 | Handle<Map> map; |
| 5091 | LookupResult lookup(isolate()); |
| 5092 | for (int i = 0; i < types->length() && count < kMaxLoadPolymorphism; ++i) { |
| 5093 | map = types->at(i); |
| 5094 | if (ComputeLoadStoreField(map, name, &lookup, false)) { |
| 5095 | int index = ComputeLoadStoreFieldIndex(map, name, &lookup); |
| 5096 | bool is_in_object = index < 0; |
| 5097 | int offset = index * kPointerSize; |
| 5098 | if (index < 0) { |
| 5099 | // Negative property indices are in-object properties, indexed |
| 5100 | // from the end of the fixed part of the object. |
| 5101 | offset += map->instance_size(); |
| 5102 | } else { |
| 5103 | offset += FixedArray::kHeaderSize; |
| 5104 | } |
| 5105 | if (count == 0) { |
| 5106 | previous_field_offset = offset; |
| 5107 | previous_field_is_in_object = is_in_object; |
| 5108 | } else if (is_monomorphic_field) { |
| 5109 | is_monomorphic_field = (offset == previous_field_offset) && |
| 5110 | (is_in_object == previous_field_is_in_object); |
| 5111 | } |
| 5112 | ++count; |
| 5113 | } |
| 5114 | } |
| 5115 | |
| 5116 | // Use monomorphic load if property lookup results in the same field index |
| 5117 | // for all maps. Requires special map check on the set of all handled maps. |
| 5118 | HInstruction* instr; |
| 5119 | if (count == types->length() && is_monomorphic_field) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5120 | AddInstruction(new(zone()) HCheckMaps(object, types, zone())); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 5121 | instr = BuildLoadNamedField(object, expr, map, &lookup, false); |
| 5122 | } else { |
| 5123 | HValue* context = environment()->LookupContext(); |
| 5124 | instr = new(zone()) HLoadNamedFieldPolymorphic(context, |
| 5125 | object, |
| 5126 | types, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5127 | name, |
| 5128 | zone()); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 5129 | } |
| 5130 | |
| 5131 | instr->set_position(expr->position()); |
| 5132 | return ast_context()->ReturnInstruction(instr, expr->id()); |
| 5133 | } |
| 5134 | |
| 5135 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5136 | void HGraphBuilder::HandlePolymorphicStoreNamedField(Assignment* expr, |
| 5137 | HValue* object, |
| 5138 | HValue* value, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5139 | SmallMapList* types, |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5140 | Handle<String> name) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 5141 | // TODO(ager): We should recognize when the prototype chains for different |
| 5142 | // maps are identical. In that case we can avoid repeatedly generating the |
| 5143 | // same prototype map checks. |
| 5144 | int count = 0; |
| 5145 | HBasicBlock* join = NULL; |
| 5146 | for (int i = 0; i < types->length() && count < kMaxStorePolymorphism; ++i) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5147 | Handle<Map> map = types->at(i); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5148 | LookupResult lookup(isolate()); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 5149 | if (ComputeLoadStoreField(map, name, &lookup, true)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 5150 | if (count == 0) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5151 | AddInstruction(new(zone()) HCheckNonSmi(object)); // Only needed once. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 5152 | join = graph()->CreateBasicBlock(); |
| 5153 | } |
| 5154 | ++count; |
| 5155 | HBasicBlock* if_true = graph()->CreateBasicBlock(); |
| 5156 | HBasicBlock* if_false = graph()->CreateBasicBlock(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5157 | HCompareMap* compare = |
| 5158 | new(zone()) HCompareMap(object, map, if_true, if_false); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 5159 | current_block()->Finish(compare); |
| 5160 | |
| 5161 | set_current_block(if_true); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5162 | HInstruction* instr; |
| 5163 | CHECK_ALIVE(instr = |
| 5164 | BuildStoreNamedField(object, name, value, map, &lookup, false)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5165 | instr->set_position(expr->position()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 5166 | // Goto will add the HSimulate for the store. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5167 | AddInstruction(instr); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 5168 | if (!ast_context()->IsEffect()) Push(value); |
| 5169 | current_block()->Goto(join); |
| 5170 | |
| 5171 | set_current_block(if_false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5172 | } |
| 5173 | } |
| 5174 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 5175 | // Finish up. Unconditionally deoptimize if we've handled all the maps we |
| 5176 | // know about and do not want to handle ones we've never seen. Otherwise |
| 5177 | // use a generic IC. |
| 5178 | if (count == types->length() && FLAG_deoptimize_uncommon_cases) { |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 5179 | current_block()->FinishExitWithDeoptimization(HDeoptimize::kNoUses); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 5180 | } else { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 5181 | HInstruction* instr = BuildStoreNamedGeneric(object, name, value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5182 | instr->set_position(expr->position()); |
| 5183 | AddInstruction(instr); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5184 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 5185 | if (join != NULL) { |
| 5186 | if (!ast_context()->IsEffect()) Push(value); |
| 5187 | current_block()->Goto(join); |
| 5188 | } else { |
| 5189 | // The HSimulate for the store should not see the stored value in |
| 5190 | // effect contexts (it is not materialized at expr->id() in the |
| 5191 | // unoptimized code). |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5192 | if (instr->HasObservableSideEffects()) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 5193 | if (ast_context()->IsEffect()) { |
| 5194 | AddSimulate(expr->id()); |
| 5195 | } else { |
| 5196 | Push(value); |
| 5197 | AddSimulate(expr->id()); |
| 5198 | Drop(1); |
| 5199 | } |
| 5200 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5201 | return ast_context()->ReturnValue(value); |
kmillikin@chromium.org | c278c96 | 2011-01-06 08:52:11 +0000 | [diff] [blame] | 5202 | } |
lrn@chromium.org | 8541d77 | 2010-12-15 12:05:09 +0000 | [diff] [blame] | 5203 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 5204 | |
| 5205 | ASSERT(join != NULL); |
| 5206 | join->SetJoinId(expr->id()); |
| 5207 | set_current_block(join); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5208 | if (!ast_context()->IsEffect()) return ast_context()->ReturnValue(Pop()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5209 | } |
| 5210 | |
| 5211 | |
| 5212 | void HGraphBuilder::HandlePropertyAssignment(Assignment* expr) { |
| 5213 | Property* prop = expr->target()->AsProperty(); |
| 5214 | ASSERT(prop != NULL); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5215 | expr->RecordTypeFeedback(oracle(), zone()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5216 | CHECK_ALIVE(VisitForValue(prop->obj())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5217 | |
| 5218 | HValue* value = NULL; |
| 5219 | HInstruction* instr = NULL; |
| 5220 | |
| 5221 | if (prop->key()->IsPropertyName()) { |
| 5222 | // Named store. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5223 | CHECK_ALIVE(VisitForValue(expr->value())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5224 | value = Pop(); |
| 5225 | HValue* object = Pop(); |
| 5226 | |
| 5227 | Literal* key = prop->key()->AsLiteral(); |
| 5228 | Handle<String> name = Handle<String>::cast(key->handle()); |
| 5229 | ASSERT(!name.is_null()); |
| 5230 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5231 | SmallMapList* types = expr->GetReceiverTypes(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5232 | if (expr->IsMonomorphic()) { |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 5233 | CHECK_ALIVE(instr = BuildStoreNamed(object, |
| 5234 | value, |
| 5235 | types->first(), |
| 5236 | prop->key())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5237 | |
| 5238 | } else if (types != NULL && types->length() > 1) { |
| 5239 | HandlePolymorphicStoreNamedField(expr, object, value, types, name); |
| 5240 | return; |
| 5241 | |
| 5242 | } else { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 5243 | instr = BuildStoreNamedGeneric(object, name, value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5244 | } |
| 5245 | |
| 5246 | } else { |
| 5247 | // Keyed store. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5248 | CHECK_ALIVE(VisitForValue(prop->key())); |
| 5249 | CHECK_ALIVE(VisitForValue(expr->value())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5250 | value = Pop(); |
| 5251 | HValue* key = Pop(); |
| 5252 | HValue* object = Pop(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5253 | bool has_side_effects = false; |
| 5254 | HandleKeyedElementAccess(object, key, value, expr, expr->AssignmentId(), |
| 5255 | expr->position(), |
| 5256 | true, // is_store |
| 5257 | &has_side_effects); |
| 5258 | Push(value); |
| 5259 | ASSERT(has_side_effects); // Stores always have side effects. |
| 5260 | AddSimulate(expr->AssignmentId()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5261 | return ast_context()->ReturnValue(Pop()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5262 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5263 | Push(value); |
| 5264 | instr->set_position(expr->position()); |
| 5265 | AddInstruction(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5266 | if (instr->HasObservableSideEffects()) AddSimulate(expr->AssignmentId()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5267 | return ast_context()->ReturnValue(Pop()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5268 | } |
| 5269 | |
| 5270 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5271 | // Because not every expression has a position and there is not common |
| 5272 | // superclass of Assignment and CountOperation, we cannot just pass the |
| 5273 | // owning expression instead of position and ast_id separately. |
| 5274 | void HGraphBuilder::HandleGlobalVariableAssignment(Variable* var, |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5275 | HValue* value, |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5276 | int position, |
| 5277 | int ast_id) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5278 | LookupResult lookup(isolate()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5279 | GlobalPropertyAccess type = LookupGlobalProperty(var, &lookup, true); |
| 5280 | if (type == kUseCell) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5281 | Handle<GlobalObject> global(info()->global_object()); |
| 5282 | Handle<JSGlobalPropertyCell> cell(global->GetPropertyCell(&lookup)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5283 | HInstruction* instr = |
| 5284 | new(zone()) HStoreGlobalCell(value, cell, lookup.GetPropertyDetails()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5285 | instr->set_position(position); |
| 5286 | AddInstruction(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5287 | if (instr->HasObservableSideEffects()) AddSimulate(ast_id); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5288 | } else { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5289 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5290 | HGlobalObject* global_object = new(zone()) HGlobalObject(context); |
| 5291 | AddInstruction(global_object); |
| 5292 | HStoreGlobalGeneric* instr = |
| 5293 | new(zone()) HStoreGlobalGeneric(context, |
| 5294 | global_object, |
| 5295 | var->name(), |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 5296 | value, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5297 | function_strict_mode_flag()); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5298 | instr->set_position(position); |
| 5299 | AddInstruction(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5300 | ASSERT(instr->HasObservableSideEffects()); |
| 5301 | if (instr->HasObservableSideEffects()) AddSimulate(ast_id); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5302 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5303 | } |
| 5304 | |
| 5305 | |
| 5306 | void HGraphBuilder::HandleCompoundAssignment(Assignment* expr) { |
| 5307 | Expression* target = expr->target(); |
| 5308 | VariableProxy* proxy = target->AsVariableProxy(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5309 | Property* prop = target->AsProperty(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5310 | ASSERT(proxy == NULL || prop == NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5311 | |
| 5312 | // We have a second position recorded in the FullCodeGenerator to have |
| 5313 | // type feedback for the binary operation. |
| 5314 | BinaryOperation* operation = expr->binary_operation(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5315 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5316 | if (proxy != NULL) { |
| 5317 | Variable* var = proxy->var(); |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 5318 | if (var->mode() == LET) { |
| 5319 | return Bailout("unsupported let compound assignment"); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 5320 | } |
| 5321 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5322 | CHECK_ALIVE(VisitForValue(operation)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5323 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5324 | switch (var->location()) { |
| 5325 | case Variable::UNALLOCATED: |
| 5326 | HandleGlobalVariableAssignment(var, |
| 5327 | Top(), |
| 5328 | expr->position(), |
| 5329 | expr->AssignmentId()); |
| 5330 | break; |
| 5331 | |
| 5332 | case Variable::PARAMETER: |
| 5333 | case Variable::LOCAL: |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 5334 | if (var->mode() == CONST) { |
| 5335 | return Bailout("unsupported const compound assignment"); |
| 5336 | } |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5337 | Bind(var, Top()); |
| 5338 | break; |
| 5339 | |
| 5340 | case Variable::CONTEXT: { |
| 5341 | // Bail out if we try to mutate a parameter value in a function |
| 5342 | // using the arguments object. We do not (yet) correctly handle the |
| 5343 | // arguments property of the function. |
| 5344 | if (info()->scope()->arguments() != NULL) { |
| 5345 | // Parameters will be allocated to context slots. We have no |
| 5346 | // direct way to detect that the variable is a parameter so we do |
| 5347 | // a linear search of the parameter variables. |
| 5348 | int count = info()->scope()->num_parameters(); |
| 5349 | for (int i = 0; i < count; ++i) { |
| 5350 | if (var == info()->scope()->parameter(i)) { |
| 5351 | Bailout( |
| 5352 | "assignment to parameter, function uses arguments object"); |
| 5353 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5354 | } |
| 5355 | } |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5356 | |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 5357 | HStoreContextSlot::Mode mode; |
| 5358 | |
| 5359 | switch (var->mode()) { |
| 5360 | case LET: |
| 5361 | mode = HStoreContextSlot::kCheckDeoptimize; |
| 5362 | break; |
| 5363 | case CONST: |
| 5364 | return ast_context()->ReturnValue(Pop()); |
| 5365 | case CONST_HARMONY: |
| 5366 | // This case is checked statically so no need to |
| 5367 | // perform checks here |
| 5368 | UNREACHABLE(); |
| 5369 | default: |
| 5370 | mode = HStoreContextSlot::kNoCheck; |
| 5371 | } |
| 5372 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5373 | HValue* context = BuildContextChainWalk(var); |
| 5374 | HStoreContextSlot* instr = |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 5375 | new(zone()) HStoreContextSlot(context, var->index(), mode, Top()); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5376 | AddInstruction(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5377 | if (instr->HasObservableSideEffects()) { |
| 5378 | AddSimulate(expr->AssignmentId()); |
| 5379 | } |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5380 | break; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5381 | } |
| 5382 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5383 | case Variable::LOOKUP: |
| 5384 | return Bailout("compound assignment to lookup slot"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5385 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5386 | return ast_context()->ReturnValue(Pop()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5387 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5388 | } else if (prop != NULL) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5389 | prop->RecordTypeFeedback(oracle(), zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5390 | |
| 5391 | if (prop->key()->IsPropertyName()) { |
| 5392 | // Named property. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5393 | CHECK_ALIVE(VisitForValue(prop->obj())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5394 | HValue* obj = Top(); |
| 5395 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 5396 | Handle<Map> map; |
| 5397 | HInstruction* load; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5398 | if (prop->IsMonomorphic()) { |
| 5399 | Handle<String> name = prop->key()->AsLiteral()->AsPropertyName(); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 5400 | map = prop->GetReceiverTypes()->first(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5401 | load = BuildLoadNamed(obj, prop, map, name); |
| 5402 | } else { |
| 5403 | load = BuildLoadNamedGeneric(obj, prop); |
| 5404 | } |
| 5405 | PushAndAdd(load); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5406 | if (load->HasObservableSideEffects()) AddSimulate(expr->CompoundLoadId()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5407 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5408 | CHECK_ALIVE(VisitForValue(expr->value())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5409 | HValue* right = Pop(); |
| 5410 | HValue* left = Pop(); |
| 5411 | |
| 5412 | HInstruction* instr = BuildBinaryOperation(operation, left, right); |
| 5413 | PushAndAdd(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5414 | if (instr->HasObservableSideEffects()) AddSimulate(operation->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5415 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5416 | HInstruction* store; |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 5417 | CHECK_ALIVE(store = BuildStoreNamed(obj, instr, map, prop->key())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5418 | AddInstruction(store); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5419 | // Drop the simulated receiver and value. Return the value. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5420 | Drop(2); |
| 5421 | Push(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5422 | if (store->HasObservableSideEffects()) AddSimulate(expr->AssignmentId()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5423 | return ast_context()->ReturnValue(Pop()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5424 | |
| 5425 | } else { |
| 5426 | // Keyed property. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5427 | CHECK_ALIVE(VisitForValue(prop->obj())); |
| 5428 | CHECK_ALIVE(VisitForValue(prop->key())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5429 | HValue* obj = environment()->ExpressionStackAt(1); |
| 5430 | HValue* key = environment()->ExpressionStackAt(0); |
| 5431 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5432 | bool has_side_effects = false; |
| 5433 | HValue* load = HandleKeyedElementAccess( |
| 5434 | obj, key, NULL, prop, expr->CompoundLoadId(), RelocInfo::kNoPosition, |
| 5435 | false, // is_store |
| 5436 | &has_side_effects); |
| 5437 | Push(load); |
| 5438 | if (has_side_effects) AddSimulate(expr->CompoundLoadId()); |
| 5439 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5440 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5441 | CHECK_ALIVE(VisitForValue(expr->value())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5442 | HValue* right = Pop(); |
| 5443 | HValue* left = Pop(); |
| 5444 | |
| 5445 | HInstruction* instr = BuildBinaryOperation(operation, left, right); |
| 5446 | PushAndAdd(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5447 | if (instr->HasObservableSideEffects()) AddSimulate(operation->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5448 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5449 | expr->RecordTypeFeedback(oracle(), zone()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5450 | HandleKeyedElementAccess(obj, key, instr, expr, expr->AssignmentId(), |
| 5451 | RelocInfo::kNoPosition, |
| 5452 | true, // is_store |
| 5453 | &has_side_effects); |
| 5454 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5455 | // Drop the simulated receiver, key, and value. Return the value. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5456 | Drop(3); |
| 5457 | Push(instr); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5458 | ASSERT(has_side_effects); // Stores always have side effects. |
| 5459 | AddSimulate(expr->AssignmentId()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5460 | return ast_context()->ReturnValue(Pop()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5461 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5462 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5463 | } else { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5464 | return Bailout("invalid lhs in compound assignment"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5465 | } |
| 5466 | } |
| 5467 | |
| 5468 | |
| 5469 | void HGraphBuilder::VisitAssignment(Assignment* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5470 | ASSERT(!HasStackOverflow()); |
| 5471 | ASSERT(current_block() != NULL); |
| 5472 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5473 | VariableProxy* proxy = expr->target()->AsVariableProxy(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5474 | Property* prop = expr->target()->AsProperty(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5475 | ASSERT(proxy == NULL || prop == NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5476 | |
| 5477 | if (expr->is_compound()) { |
| 5478 | HandleCompoundAssignment(expr); |
| 5479 | return; |
| 5480 | } |
| 5481 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5482 | if (prop != NULL) { |
| 5483 | HandlePropertyAssignment(expr); |
| 5484 | } else if (proxy != NULL) { |
| 5485 | Variable* var = proxy->var(); |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 5486 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 5487 | if (var->mode() == CONST) { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 5488 | if (expr->op() != Token::INIT_CONST) { |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 5489 | CHECK_ALIVE(VisitForValue(expr->value())); |
| 5490 | return ast_context()->ReturnValue(Pop()); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 5491 | } |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 5492 | |
| 5493 | if (var->IsStackAllocated()) { |
| 5494 | // We insert a use of the old value to detect unsupported uses of const |
| 5495 | // variables (e.g. initialization inside a loop). |
| 5496 | HValue* old_value = environment()->Lookup(var); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5497 | AddInstruction(new(zone()) HUseConst(old_value)); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 5498 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 5499 | } else if (var->mode() == CONST_HARMONY) { |
| 5500 | if (expr->op() != Token::INIT_CONST_HARMONY) { |
| 5501 | return Bailout("non-initializer assignment to const"); |
| 5502 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 5503 | } |
| 5504 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5505 | if (proxy->IsArguments()) return Bailout("assignment to arguments"); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5506 | |
| 5507 | // Handle the assignment. |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5508 | switch (var->location()) { |
| 5509 | case Variable::UNALLOCATED: |
| 5510 | CHECK_ALIVE(VisitForValue(expr->value())); |
| 5511 | HandleGlobalVariableAssignment(var, |
| 5512 | Top(), |
| 5513 | expr->position(), |
| 5514 | expr->AssignmentId()); |
| 5515 | return ast_context()->ReturnValue(Pop()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 5516 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5517 | case Variable::PARAMETER: |
| 5518 | case Variable::LOCAL: { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 5519 | // Perform an initialization check for let declared variables |
| 5520 | // or parameters. |
| 5521 | if (var->mode() == LET && expr->op() == Token::ASSIGN) { |
| 5522 | HValue* env_value = environment()->Lookup(var); |
| 5523 | if (env_value == graph()->GetConstantHole()) { |
| 5524 | return Bailout("assignment to let variable before initialization"); |
| 5525 | } |
| 5526 | } |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5527 | // We do not allow the arguments object to occur in a context where it |
| 5528 | // may escape, but assignments to stack-allocated locals are |
| 5529 | // permitted. |
| 5530 | CHECK_ALIVE(VisitForValue(expr->value(), ARGUMENTS_ALLOWED)); |
| 5531 | HValue* value = Pop(); |
| 5532 | Bind(var, value); |
| 5533 | return ast_context()->ReturnValue(value); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5534 | } |
| 5535 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5536 | case Variable::CONTEXT: { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5537 | // Bail out if we try to mutate a parameter value in a function using |
| 5538 | // the arguments object. We do not (yet) correctly handle the |
| 5539 | // arguments property of the function. |
| 5540 | if (info()->scope()->arguments() != NULL) { |
| 5541 | // Parameters will rewrite to context slots. We have no direct way |
| 5542 | // to detect that the variable is a parameter. |
| 5543 | int count = info()->scope()->num_parameters(); |
| 5544 | for (int i = 0; i < count; ++i) { |
| 5545 | if (var == info()->scope()->parameter(i)) { |
| 5546 | return Bailout("assignment to parameter in arguments object"); |
| 5547 | } |
| 5548 | } |
| 5549 | } |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 5550 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5551 | CHECK_ALIVE(VisitForValue(expr->value())); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 5552 | HStoreContextSlot::Mode mode; |
| 5553 | if (expr->op() == Token::ASSIGN) { |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 5554 | switch (var->mode()) { |
| 5555 | case LET: |
| 5556 | mode = HStoreContextSlot::kCheckDeoptimize; |
| 5557 | break; |
| 5558 | case CONST: |
| 5559 | return ast_context()->ReturnValue(Pop()); |
| 5560 | case CONST_HARMONY: |
| 5561 | // This case is checked statically so no need to |
| 5562 | // perform checks here |
| 5563 | UNREACHABLE(); |
| 5564 | default: |
| 5565 | mode = HStoreContextSlot::kNoCheck; |
| 5566 | } |
| 5567 | } else if (expr->op() == Token::INIT_VAR || |
| 5568 | expr->op() == Token::INIT_LET || |
| 5569 | expr->op() == Token::INIT_CONST_HARMONY) { |
| 5570 | mode = HStoreContextSlot::kNoCheck; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 5571 | } else { |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 5572 | ASSERT(expr->op() == Token::INIT_CONST); |
| 5573 | |
| 5574 | mode = HStoreContextSlot::kCheckIgnoreAssignment; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 5575 | } |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 5576 | |
| 5577 | HValue* context = BuildContextChainWalk(var); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 5578 | HStoreContextSlot* instr = new(zone()) HStoreContextSlot( |
| 5579 | context, var->index(), mode, Top()); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5580 | AddInstruction(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5581 | if (instr->HasObservableSideEffects()) { |
| 5582 | AddSimulate(expr->AssignmentId()); |
| 5583 | } |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5584 | return ast_context()->ReturnValue(Pop()); |
| 5585 | } |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 5586 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 5587 | case Variable::LOOKUP: |
| 5588 | return Bailout("assignment to LOOKUP variable"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5589 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5590 | } else { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5591 | return Bailout("invalid left-hand side in assignment"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5592 | } |
| 5593 | } |
| 5594 | |
| 5595 | |
| 5596 | void HGraphBuilder::VisitThrow(Throw* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5597 | ASSERT(!HasStackOverflow()); |
| 5598 | ASSERT(current_block() != NULL); |
| 5599 | ASSERT(current_block()->HasPredecessor()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5600 | // We don't optimize functions with invalid left-hand sides in |
| 5601 | // assignments, count operations, or for-in. Consequently throw can |
| 5602 | // currently only occur in an effect context. |
| 5603 | ASSERT(ast_context()->IsEffect()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 5604 | CHECK_ALIVE(VisitForValue(expr->exception())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5605 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5606 | HValue* context = environment()->LookupContext(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5607 | HValue* value = environment()->Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 5608 | HThrow* instr = new(zone()) HThrow(context, value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5609 | instr->set_position(expr->position()); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 5610 | AddInstruction(instr); |
| 5611 | AddSimulate(expr->id()); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5612 | current_block()->FinishExit(new(zone()) HAbnormalExit); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 5613 | set_current_block(NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5614 | } |
| 5615 | |
| 5616 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 5617 | HLoadNamedField* HGraphBuilder::BuildLoadNamedField(HValue* object, |
| 5618 | Property* expr, |
| 5619 | Handle<Map> type, |
| 5620 | LookupResult* lookup, |
| 5621 | bool smi_and_map_check) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5622 | if (smi_and_map_check) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5623 | AddInstruction(new(zone()) HCheckNonSmi(object)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5624 | AddInstruction(HCheckMaps::NewWithTransitions(object, type, zone())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5625 | } |
| 5626 | |
| 5627 | int index = lookup->GetLocalFieldIndexFromMap(*type); |
| 5628 | if (index < 0) { |
| 5629 | // Negative property indices are in-object properties, indexed |
| 5630 | // from the end of the fixed part of the object. |
| 5631 | int offset = (index * kPointerSize) + type->instance_size(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5632 | return new(zone()) HLoadNamedField(object, true, offset); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5633 | } else { |
| 5634 | // Non-negative property indices are in the properties array. |
| 5635 | int offset = (index * kPointerSize) + FixedArray::kHeaderSize; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5636 | return new(zone()) HLoadNamedField(object, false, offset); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5637 | } |
| 5638 | } |
| 5639 | |
| 5640 | |
| 5641 | HInstruction* HGraphBuilder::BuildLoadNamedGeneric(HValue* obj, |
| 5642 | Property* expr) { |
ulan@chromium.org | 6ff6514 | 2012-03-21 09:52:17 +0000 | [diff] [blame] | 5643 | if (expr->IsUninitialized() && !FLAG_always_opt) { |
jkummerow@chromium.org | 531dfe8 | 2012-03-20 13:01:16 +0000 | [diff] [blame] | 5644 | AddInstruction(new(zone()) HSoftDeoptimize); |
| 5645 | current_block()->MarkAsDeoptimizing(); |
| 5646 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5647 | ASSERT(expr->key()->IsPropertyName()); |
| 5648 | Handle<Object> name = expr->key()->AsLiteral()->handle(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5649 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5650 | return new(zone()) HLoadNamedGeneric(context, obj, name); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5651 | } |
| 5652 | |
| 5653 | |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 5654 | HInstruction* HGraphBuilder::BuildCallGetter(HValue* obj, |
| 5655 | Property* expr, |
| 5656 | Handle<Map> map, |
| 5657 | Handle<Object> callback, |
| 5658 | Handle<JSObject> holder) { |
| 5659 | if (!callback->IsAccessorPair()) return BuildLoadNamedGeneric(obj, expr); |
| 5660 | Handle<Object> getter(Handle<AccessorPair>::cast(callback)->getter()); |
| 5661 | Handle<JSFunction> function(Handle<JSFunction>::cast(getter)); |
| 5662 | AddCheckConstantFunction(holder, obj, map, true); |
| 5663 | AddInstruction(new(zone()) HPushArgument(obj)); |
| 5664 | return new(zone()) HCallConstantFunction(function, 1); |
| 5665 | } |
| 5666 | |
| 5667 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5668 | HInstruction* HGraphBuilder::BuildLoadNamed(HValue* obj, |
| 5669 | Property* expr, |
| 5670 | Handle<Map> map, |
| 5671 | Handle<String> name) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5672 | LookupResult lookup(isolate()); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 5673 | map->LookupDescriptor(NULL, *name, &lookup); |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 5674 | if (lookup.IsField()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5675 | return BuildLoadNamedField(obj, |
| 5676 | expr, |
| 5677 | map, |
| 5678 | &lookup, |
| 5679 | true); |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 5680 | } else if (lookup.IsConstantFunction()) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5681 | AddInstruction(new(zone()) HCheckNonSmi(obj)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5682 | AddInstruction(HCheckMaps::NewWithTransitions(obj, map, zone())); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5683 | Handle<JSFunction> function(lookup.GetConstantFunctionFromMap(*map)); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5684 | return new(zone()) HConstant(function, Representation::Tagged()); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 5685 | } else if (lookup.IsPropertyCallbacks()) { |
| 5686 | Handle<Object> callback(lookup.GetValueFromMap(*map)); |
| 5687 | Handle<JSObject> holder; |
| 5688 | return BuildCallGetter(obj, expr, map, callback, holder); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5689 | } else { |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 5690 | LookupInPrototypes(map, name, &lookup); |
| 5691 | if (lookup.IsPropertyCallbacks()) { |
| 5692 | Handle<Object> callback(lookup.GetValue()); |
| 5693 | Handle<JSObject> holder(lookup.holder()); |
| 5694 | return BuildCallGetter(obj, expr, map, callback, holder); |
| 5695 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5696 | return BuildLoadNamedGeneric(obj, expr); |
| 5697 | } |
| 5698 | } |
| 5699 | |
| 5700 | |
| 5701 | HInstruction* HGraphBuilder::BuildLoadKeyedGeneric(HValue* object, |
| 5702 | HValue* key) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 5703 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5704 | return new(zone()) HLoadKeyedGeneric(context, object, key); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5705 | } |
| 5706 | |
| 5707 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5708 | HInstruction* HGraphBuilder::BuildExternalArrayElementAccess( |
| 5709 | HValue* external_elements, |
| 5710 | HValue* checked_key, |
| 5711 | HValue* val, |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 5712 | ElementsKind elements_kind, |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5713 | bool is_store) { |
| 5714 | if (is_store) { |
| 5715 | ASSERT(val != NULL); |
| 5716 | switch (elements_kind) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 5717 | case EXTERNAL_PIXEL_ELEMENTS: { |
fschneider@chromium.org | 35814e5 | 2012-03-01 15:43:35 +0000 | [diff] [blame] | 5718 | val = AddInstruction(new(zone()) HClampToUint8(val)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5719 | break; |
| 5720 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 5721 | case EXTERNAL_BYTE_ELEMENTS: |
| 5722 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 5723 | case EXTERNAL_SHORT_ELEMENTS: |
| 5724 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 5725 | case EXTERNAL_INT_ELEMENTS: |
| 5726 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
fschneider@chromium.org | 35814e5 | 2012-03-01 15:43:35 +0000 | [diff] [blame] | 5727 | if (!val->representation().IsInteger32()) { |
| 5728 | val = AddInstruction(new(zone()) HChange( |
| 5729 | val, |
| 5730 | Representation::Integer32(), |
| 5731 | true, // Truncate to int32. |
| 5732 | false)); // Don't deoptimize undefined (irrelevant here). |
| 5733 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5734 | break; |
| 5735 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 5736 | case EXTERNAL_FLOAT_ELEMENTS: |
| 5737 | case EXTERNAL_DOUBLE_ELEMENTS: |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5738 | break; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5739 | case FAST_SMI_ELEMENTS: |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 5740 | case FAST_ELEMENTS: |
| 5741 | case FAST_DOUBLE_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5742 | case FAST_HOLEY_SMI_ELEMENTS: |
| 5743 | case FAST_HOLEY_ELEMENTS: |
| 5744 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 5745 | case DICTIONARY_ELEMENTS: |
| 5746 | case NON_STRICT_ARGUMENTS_ELEMENTS: |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5747 | UNREACHABLE(); |
| 5748 | break; |
| 5749 | } |
| 5750 | return new(zone()) HStoreKeyedSpecializedArrayElement( |
| 5751 | external_elements, checked_key, val, elements_kind); |
| 5752 | } else { |
fschneider@chromium.org | 35814e5 | 2012-03-01 15:43:35 +0000 | [diff] [blame] | 5753 | ASSERT(val == NULL); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5754 | return new(zone()) HLoadKeyedSpecializedArrayElement( |
| 5755 | external_elements, checked_key, elements_kind); |
| 5756 | } |
| 5757 | } |
| 5758 | |
| 5759 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5760 | HInstruction* HGraphBuilder::BuildFastElementAccess(HValue* elements, |
| 5761 | HValue* checked_key, |
| 5762 | HValue* val, |
| 5763 | ElementsKind elements_kind, |
| 5764 | bool is_store) { |
| 5765 | if (is_store) { |
| 5766 | ASSERT(val != NULL); |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 5767 | switch (elements_kind) { |
| 5768 | case FAST_DOUBLE_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5769 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 5770 | return new(zone()) HStoreKeyedFastDoubleElement( |
| 5771 | elements, checked_key, val); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5772 | case FAST_SMI_ELEMENTS: |
| 5773 | case FAST_HOLEY_SMI_ELEMENTS: |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 5774 | // Smi-only arrays need a smi check. |
| 5775 | AddInstruction(new(zone()) HCheckSmi(val)); |
| 5776 | // Fall through. |
| 5777 | case FAST_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5778 | case FAST_HOLEY_ELEMENTS: |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 5779 | return new(zone()) HStoreKeyedFastElement( |
| 5780 | elements, checked_key, val, elements_kind); |
| 5781 | default: |
| 5782 | UNREACHABLE(); |
| 5783 | return NULL; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5784 | } |
| 5785 | } |
| 5786 | // It's an element load (!is_store). |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5787 | HoleCheckMode mode = IsFastPackedElementsKind(elements_kind) ? |
| 5788 | OMIT_HOLE_CHECK : |
| 5789 | PERFORM_HOLE_CHECK; |
| 5790 | if (IsFastDoubleElementsKind(elements_kind)) { |
| 5791 | return new(zone()) HLoadKeyedFastDoubleElement(elements, checked_key, mode); |
| 5792 | } else { // Smi or Object elements. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5793 | return new(zone()) HLoadKeyedFastElement(elements, checked_key, |
| 5794 | elements_kind); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5795 | } |
| 5796 | } |
| 5797 | |
| 5798 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5799 | HInstruction* HGraphBuilder::BuildMonomorphicElementAccess(HValue* object, |
| 5800 | HValue* key, |
| 5801 | HValue* val, |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5802 | HValue* dependency, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5803 | Handle<Map> map, |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5804 | bool is_store) { |
mstarzinger@chromium.org | c6d9cee | 2012-07-03 10:03:19 +0000 | [diff] [blame] | 5805 | HCheckMaps* mapcheck = new(zone()) HCheckMaps(object, map, |
| 5806 | zone(), dependency); |
| 5807 | AddInstruction(mapcheck); |
| 5808 | if (dependency) { |
| 5809 | mapcheck->ClearGVNFlag(kDependsOnElementsKind); |
| 5810 | } |
| 5811 | return BuildUncheckedMonomorphicElementAccess(object, key, val, |
| 5812 | mapcheck, map, is_store); |
| 5813 | } |
| 5814 | |
| 5815 | |
| 5816 | HInstruction* HGraphBuilder::BuildUncheckedMonomorphicElementAccess( |
| 5817 | HValue* object, |
| 5818 | HValue* key, |
| 5819 | HValue* val, |
| 5820 | HCheckMaps* mapcheck, |
| 5821 | Handle<Map> map, |
| 5822 | bool is_store) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5823 | // No GVNFlag is necessary for ElementsKind if there is an explicit dependency |
| 5824 | // on a HElementsTransition instruction. The flag can also be removed if the |
| 5825 | // map to check has FAST_HOLEY_ELEMENTS, since there can be no further |
| 5826 | // ElementsKind transitions. Finally, the dependency can be removed for stores |
| 5827 | // for FAST_ELEMENTS, since a transition to HOLEY elements won't change the |
| 5828 | // generated store code. |
mstarzinger@chromium.org | c6d9cee | 2012-07-03 10:03:19 +0000 | [diff] [blame] | 5829 | if ((map->elements_kind() == FAST_HOLEY_ELEMENTS) || |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5830 | (map->elements_kind() == FAST_ELEMENTS && is_store)) { |
| 5831 | mapcheck->ClearGVNFlag(kDependsOnElementsKind); |
| 5832 | } |
| 5833 | bool fast_smi_only_elements = map->has_fast_smi_elements(); |
| 5834 | bool fast_elements = map->has_fast_object_elements(); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 5835 | HInstruction* elements = AddInstruction(new(zone()) HLoadElements(object)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5836 | if (is_store && (fast_elements || fast_smi_only_elements)) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5837 | HCheckMaps* check_cow_map = new(zone()) HCheckMaps( |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5838 | elements, isolate()->factory()->fixed_array_map(), zone()); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5839 | check_cow_map->ClearGVNFlag(kDependsOnElementsKind); |
| 5840 | AddInstruction(check_cow_map); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 5841 | } |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 5842 | HInstruction* length = NULL; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5843 | HInstruction* checked_key = NULL; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5844 | if (map->has_external_array_elements()) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 5845 | length = AddInstruction(new(zone()) HFixedArrayBaseLength(elements)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5846 | checked_key = AddInstruction(new(zone()) HBoundsCheck(key, length)); |
| 5847 | HLoadExternalArrayPointer* external_elements = |
| 5848 | new(zone()) HLoadExternalArrayPointer(elements); |
| 5849 | AddInstruction(external_elements); |
| 5850 | return BuildExternalArrayElementAccess(external_elements, checked_key, |
| 5851 | val, map->elements_kind(), is_store); |
| 5852 | } |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 5853 | ASSERT(fast_smi_only_elements || |
| 5854 | fast_elements || |
| 5855 | map->has_fast_double_elements()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5856 | if (map->instance_type() == JS_ARRAY_TYPE) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5857 | length = AddInstruction(new(zone()) HJSArrayLength(object, mapcheck, |
| 5858 | HType::Smi())); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 5859 | } else { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 5860 | length = AddInstruction(new(zone()) HFixedArrayBaseLength(elements)); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 5861 | } |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 5862 | checked_key = AddInstruction(new(zone()) HBoundsCheck(key, length)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5863 | return BuildFastElementAccess(elements, checked_key, val, |
| 5864 | map->elements_kind(), is_store); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5865 | } |
| 5866 | |
| 5867 | |
mstarzinger@chromium.org | c6d9cee | 2012-07-03 10:03:19 +0000 | [diff] [blame] | 5868 | HInstruction* HGraphBuilder::TryBuildConsolidatedElementLoad( |
| 5869 | HValue* object, |
| 5870 | HValue* key, |
| 5871 | HValue* val, |
| 5872 | SmallMapList* maps) { |
| 5873 | // For polymorphic loads of similar elements kinds (i.e. all tagged or all |
| 5874 | // double), always use the "worst case" code without a transition. This is |
| 5875 | // much faster than transitioning the elements to the worst case, trading a |
| 5876 | // HTransitionElements for a HCheckMaps, and avoiding mutation of the array. |
| 5877 | bool has_double_maps = false; |
| 5878 | bool has_smi_or_object_maps = false; |
| 5879 | bool has_js_array_access = false; |
| 5880 | bool has_non_js_array_access = false; |
| 5881 | Handle<Map> most_general_consolidated_map; |
| 5882 | for (int i = 0; i < maps->length(); ++i) { |
| 5883 | Handle<Map> map = maps->at(i); |
| 5884 | // Don't allow mixing of JSArrays with JSObjects. |
| 5885 | if (map->instance_type() == JS_ARRAY_TYPE) { |
| 5886 | if (has_non_js_array_access) return NULL; |
| 5887 | has_js_array_access = true; |
| 5888 | } else if (has_js_array_access) { |
| 5889 | return NULL; |
| 5890 | } else { |
| 5891 | has_non_js_array_access = true; |
| 5892 | } |
| 5893 | // Don't allow mixed, incompatible elements kinds. |
| 5894 | if (map->has_fast_double_elements()) { |
| 5895 | if (has_smi_or_object_maps) return NULL; |
| 5896 | has_double_maps = true; |
| 5897 | } else if (map->has_fast_smi_or_object_elements()) { |
| 5898 | if (has_double_maps) return NULL; |
| 5899 | has_smi_or_object_maps = true; |
| 5900 | } else { |
| 5901 | return NULL; |
| 5902 | } |
| 5903 | // Remember the most general elements kind, the code for its load will |
| 5904 | // properly handle all of the more specific cases. |
| 5905 | if ((i == 0) || IsMoreGeneralElementsKindTransition( |
| 5906 | most_general_consolidated_map->elements_kind(), |
| 5907 | map->elements_kind())) { |
| 5908 | most_general_consolidated_map = map; |
| 5909 | } |
| 5910 | } |
| 5911 | if (!has_double_maps && !has_smi_or_object_maps) return NULL; |
| 5912 | |
| 5913 | HCheckMaps* check_maps = new(zone()) HCheckMaps(object, maps, zone()); |
| 5914 | AddInstruction(check_maps); |
| 5915 | HInstruction* instr = BuildUncheckedMonomorphicElementAccess( |
| 5916 | object, key, val, check_maps, most_general_consolidated_map, false); |
| 5917 | return instr; |
| 5918 | } |
| 5919 | |
| 5920 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5921 | HValue* HGraphBuilder::HandlePolymorphicElementAccess(HValue* object, |
| 5922 | HValue* key, |
| 5923 | HValue* val, |
| 5924 | Expression* prop, |
| 5925 | int ast_id, |
| 5926 | int position, |
| 5927 | bool is_store, |
| 5928 | bool* has_side_effects) { |
| 5929 | *has_side_effects = false; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5930 | AddInstruction(new(zone()) HCheckNonSmi(object)); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5931 | SmallMapList* maps = prop->GetReceiverTypes(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5932 | bool todo_external_array = false; |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 5933 | |
mstarzinger@chromium.org | c6d9cee | 2012-07-03 10:03:19 +0000 | [diff] [blame] | 5934 | if (!is_store) { |
| 5935 | HInstruction* consolidated_load = |
| 5936 | TryBuildConsolidatedElementLoad(object, key, val, maps); |
| 5937 | if (consolidated_load != NULL) { |
| 5938 | AddInstruction(consolidated_load); |
| 5939 | *has_side_effects |= consolidated_load->HasObservableSideEffects(); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 5940 | if (position != RelocInfo::kNoPosition) { |
| 5941 | consolidated_load->set_position(position); |
| 5942 | } |
mstarzinger@chromium.org | c6d9cee | 2012-07-03 10:03:19 +0000 | [diff] [blame] | 5943 | return consolidated_load; |
| 5944 | } |
| 5945 | } |
| 5946 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 5947 | static const int kNumElementTypes = kElementsKindCount; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5948 | bool type_todo[kNumElementTypes]; |
| 5949 | for (int i = 0; i < kNumElementTypes; ++i) { |
| 5950 | type_todo[i] = false; |
| 5951 | } |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 5952 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5953 | // Elements_kind transition support. |
| 5954 | MapHandleList transition_target(maps->length()); |
| 5955 | // Collect possible transition targets. |
| 5956 | MapHandleList possible_transitioned_maps(maps->length()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5957 | for (int i = 0; i < maps->length(); ++i) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5958 | Handle<Map> map = maps->at(i); |
| 5959 | ElementsKind elements_kind = map->elements_kind(); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5960 | if (IsFastElementsKind(elements_kind) && |
| 5961 | elements_kind != GetInitialFastElementsKind()) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5962 | possible_transitioned_maps.Add(map); |
| 5963 | } |
| 5964 | } |
| 5965 | // Get transition target for each map (NULL == no transition). |
| 5966 | for (int i = 0; i < maps->length(); ++i) { |
| 5967 | Handle<Map> map = maps->at(i); |
| 5968 | Handle<Map> transitioned_map = |
| 5969 | map->FindTransitionedMap(&possible_transitioned_maps); |
| 5970 | transition_target.Add(transitioned_map); |
| 5971 | } |
| 5972 | |
| 5973 | int num_untransitionable_maps = 0; |
| 5974 | Handle<Map> untransitionable_map; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5975 | HTransitionElementsKind* transition = NULL; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5976 | for (int i = 0; i < maps->length(); ++i) { |
| 5977 | Handle<Map> map = maps->at(i); |
| 5978 | ASSERT(map->IsMap()); |
| 5979 | if (!transition_target.at(i).is_null()) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5980 | ASSERT(Map::IsValidElementsTransition( |
| 5981 | map->elements_kind(), |
| 5982 | transition_target.at(i)->elements_kind())); |
| 5983 | transition = new(zone()) HTransitionElementsKind( |
| 5984 | object, map, transition_target.at(i)); |
| 5985 | AddInstruction(transition); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5986 | } else { |
| 5987 | type_todo[map->elements_kind()] = true; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5988 | if (IsExternalArrayElementsKind(map->elements_kind())) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5989 | todo_external_array = true; |
| 5990 | } |
| 5991 | num_untransitionable_maps++; |
| 5992 | untransitionable_map = map; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 5993 | } |
| 5994 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5995 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5996 | // If only one map is left after transitioning, handle this case |
| 5997 | // monomorphically. |
| 5998 | if (num_untransitionable_maps == 1) { |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 5999 | HInstruction* instr = NULL; |
| 6000 | if (untransitionable_map->has_slow_elements_kind()) { |
| 6001 | instr = AddInstruction(is_store ? BuildStoreKeyedGeneric(object, key, val) |
| 6002 | : BuildLoadKeyedGeneric(object, key)); |
| 6003 | } else { |
| 6004 | instr = AddInstruction(BuildMonomorphicElementAccess( |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6005 | object, key, val, transition, untransitionable_map, is_store)); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 6006 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 6007 | *has_side_effects |= instr->HasObservableSideEffects(); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 6008 | if (position != RelocInfo::kNoPosition) instr->set_position(position); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6009 | return is_store ? NULL : instr; |
| 6010 | } |
| 6011 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 6012 | AddInstruction(HCheckInstanceType::NewIsSpecObject(object, zone())); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6013 | HBasicBlock* join = graph()->CreateBasicBlock(); |
| 6014 | |
| 6015 | HInstruction* elements_kind_instr = |
| 6016 | AddInstruction(new(zone()) HElementsKind(object)); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 6017 | HCompareConstantEqAndBranch* elements_kind_branch = NULL; |
| 6018 | HInstruction* elements = AddInstruction(new(zone()) HLoadElements(object)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6019 | HLoadExternalArrayPointer* external_elements = NULL; |
| 6020 | HInstruction* checked_key = NULL; |
| 6021 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6022 | // Generated code assumes that FAST_* and DICTIONARY_ELEMENTS ElementsKinds |
| 6023 | // are handled before external arrays. |
| 6024 | STATIC_ASSERT(FAST_SMI_ELEMENTS < FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND); |
| 6025 | STATIC_ASSERT(FAST_HOLEY_ELEMENTS < FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6026 | STATIC_ASSERT(FAST_DOUBLE_ELEMENTS < FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND); |
| 6027 | STATIC_ASSERT(DICTIONARY_ELEMENTS < FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6028 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6029 | for (ElementsKind elements_kind = FIRST_ELEMENTS_KIND; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 6030 | elements_kind <= LAST_ELEMENTS_KIND; |
| 6031 | elements_kind = ElementsKind(elements_kind + 1)) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6032 | // After having handled FAST_* and DICTIONARY_ELEMENTS, we need to add some |
| 6033 | // code that's executed for all external array cases. |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 6034 | STATIC_ASSERT(LAST_EXTERNAL_ARRAY_ELEMENTS_KIND == |
| 6035 | LAST_ELEMENTS_KIND); |
| 6036 | if (elements_kind == FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6037 | && todo_external_array) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6038 | HInstruction* length = |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 6039 | AddInstruction(new(zone()) HFixedArrayBaseLength(elements)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6040 | checked_key = AddInstruction(new(zone()) HBoundsCheck(key, length)); |
| 6041 | external_elements = new(zone()) HLoadExternalArrayPointer(elements); |
| 6042 | AddInstruction(external_elements); |
| 6043 | } |
| 6044 | if (type_todo[elements_kind]) { |
| 6045 | HBasicBlock* if_true = graph()->CreateBasicBlock(); |
| 6046 | HBasicBlock* if_false = graph()->CreateBasicBlock(); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 6047 | elements_kind_branch = new(zone()) HCompareConstantEqAndBranch( |
| 6048 | elements_kind_instr, elements_kind, Token::EQ_STRICT); |
| 6049 | elements_kind_branch->SetSuccessorAt(0, if_true); |
| 6050 | elements_kind_branch->SetSuccessorAt(1, if_false); |
| 6051 | current_block()->Finish(elements_kind_branch); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6052 | |
| 6053 | set_current_block(if_true); |
| 6054 | HInstruction* access; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6055 | if (IsFastElementsKind(elements_kind)) { |
| 6056 | if (is_store && !IsFastDoubleElementsKind(elements_kind)) { |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 6057 | AddInstruction(new(zone()) HCheckMaps( |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 6058 | elements, isolate()->factory()->fixed_array_map(), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 6059 | zone(), elements_kind_branch)); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 6060 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6061 | // TODO(jkummerow): The need for these two blocks could be avoided |
| 6062 | // in one of two ways: |
| 6063 | // (1) Introduce ElementsKinds for JSArrays that are distinct from |
| 6064 | // those for fast objects. |
| 6065 | // (2) Put the common instructions into a third "join" block. This |
| 6066 | // requires additional AST IDs that we can deopt to from inside |
| 6067 | // that join block. They must be added to the Property class (when |
| 6068 | // it's a keyed property) and registered in the full codegen. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6069 | HBasicBlock* if_jsarray = graph()->CreateBasicBlock(); |
| 6070 | HBasicBlock* if_fastobject = graph()->CreateBasicBlock(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6071 | HHasInstanceTypeAndBranch* typecheck = |
| 6072 | new(zone()) HHasInstanceTypeAndBranch(object, JS_ARRAY_TYPE); |
| 6073 | typecheck->SetSuccessorAt(0, if_jsarray); |
| 6074 | typecheck->SetSuccessorAt(1, if_fastobject); |
| 6075 | current_block()->Finish(typecheck); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6076 | |
| 6077 | set_current_block(if_jsarray); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6078 | HInstruction* length; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 6079 | length = AddInstruction(new(zone()) HJSArrayLength(object, typecheck, |
| 6080 | HType::Smi())); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6081 | checked_key = AddInstruction(new(zone()) HBoundsCheck(key, length)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6082 | access = AddInstruction(BuildFastElementAccess( |
| 6083 | elements, checked_key, val, elements_kind, is_store)); |
| 6084 | if (!is_store) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6085 | Push(access); |
| 6086 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6087 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 6088 | *has_side_effects |= access->HasObservableSideEffects(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6089 | if (position != -1) { |
| 6090 | access->set_position(position); |
| 6091 | } |
| 6092 | if_jsarray->Goto(join); |
| 6093 | |
| 6094 | set_current_block(if_fastobject); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 6095 | length = AddInstruction(new(zone()) HFixedArrayBaseLength(elements)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6096 | checked_key = AddInstruction(new(zone()) HBoundsCheck(key, length)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6097 | access = AddInstruction(BuildFastElementAccess( |
| 6098 | elements, checked_key, val, elements_kind, is_store)); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 6099 | } else if (elements_kind == DICTIONARY_ELEMENTS) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6100 | if (is_store) { |
| 6101 | access = AddInstruction(BuildStoreKeyedGeneric(object, key, val)); |
| 6102 | } else { |
| 6103 | access = AddInstruction(BuildLoadKeyedGeneric(object, key)); |
| 6104 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6105 | } else { // External array elements. |
| 6106 | access = AddInstruction(BuildExternalArrayElementAccess( |
| 6107 | external_elements, checked_key, val, elements_kind, is_store)); |
| 6108 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 6109 | *has_side_effects |= access->HasObservableSideEffects(); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 6110 | if (position != RelocInfo::kNoPosition) access->set_position(position); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6111 | if (!is_store) { |
| 6112 | Push(access); |
| 6113 | } |
| 6114 | current_block()->Goto(join); |
| 6115 | set_current_block(if_false); |
| 6116 | } |
| 6117 | } |
| 6118 | |
| 6119 | // Deopt if none of the cases matched. |
| 6120 | current_block()->FinishExitWithDeoptimization(HDeoptimize::kNoUses); |
| 6121 | join->SetJoinId(ast_id); |
| 6122 | set_current_block(join); |
| 6123 | return is_store ? NULL : Pop(); |
| 6124 | } |
| 6125 | |
| 6126 | |
| 6127 | HValue* HGraphBuilder::HandleKeyedElementAccess(HValue* obj, |
| 6128 | HValue* key, |
| 6129 | HValue* val, |
| 6130 | Expression* expr, |
| 6131 | int ast_id, |
| 6132 | int position, |
| 6133 | bool is_store, |
| 6134 | bool* has_side_effects) { |
| 6135 | ASSERT(!expr->IsPropertyName()); |
| 6136 | HInstruction* instr = NULL; |
| 6137 | if (expr->IsMonomorphic()) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6138 | Handle<Map> map = expr->GetMonomorphicReceiverType(); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 6139 | if (map->has_slow_elements_kind()) { |
| 6140 | instr = is_store ? BuildStoreKeyedGeneric(obj, key, val) |
| 6141 | : BuildLoadKeyedGeneric(obj, key); |
| 6142 | } else { |
| 6143 | AddInstruction(new(zone()) HCheckNonSmi(obj)); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6144 | instr = BuildMonomorphicElementAccess(obj, key, val, NULL, map, is_store); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 6145 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6146 | } else if (expr->GetReceiverTypes() != NULL && |
| 6147 | !expr->GetReceiverTypes()->is_empty()) { |
| 6148 | return HandlePolymorphicElementAccess( |
| 6149 | obj, key, val, expr, ast_id, position, is_store, has_side_effects); |
| 6150 | } else { |
| 6151 | if (is_store) { |
| 6152 | instr = BuildStoreKeyedGeneric(obj, key, val); |
| 6153 | } else { |
| 6154 | instr = BuildLoadKeyedGeneric(obj, key); |
| 6155 | } |
| 6156 | } |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 6157 | if (position != RelocInfo::kNoPosition) instr->set_position(position); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6158 | AddInstruction(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 6159 | *has_side_effects = instr->HasObservableSideEffects(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6160 | return instr; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 6161 | } |
| 6162 | |
| 6163 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6164 | HInstruction* HGraphBuilder::BuildStoreKeyedGeneric(HValue* object, |
| 6165 | HValue* key, |
| 6166 | HValue* value) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6167 | HValue* context = environment()->LookupContext(); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 6168 | return new(zone()) HStoreKeyedGeneric( |
| 6169 | context, |
| 6170 | object, |
| 6171 | key, |
| 6172 | value, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6173 | function_strict_mode_flag()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6174 | } |
| 6175 | |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6176 | |
| 6177 | void HGraphBuilder::EnsureArgumentsArePushedForAccess() { |
| 6178 | // Outermost function already has arguments on the stack. |
| 6179 | if (function_state()->outer() == NULL) return; |
| 6180 | |
| 6181 | if (function_state()->arguments_pushed()) return; |
| 6182 | |
| 6183 | // Push arguments when entering inlined function. |
| 6184 | HEnterInlined* entry = function_state()->entry(); |
| 6185 | |
| 6186 | ZoneList<HValue*>* arguments_values = entry->arguments_values(); |
| 6187 | |
| 6188 | HInstruction* insert_after = entry; |
| 6189 | for (int i = 0; i < arguments_values->length(); i++) { |
| 6190 | HValue* argument = arguments_values->at(i); |
| 6191 | HInstruction* push_argument = new(zone()) HPushArgument(argument); |
| 6192 | push_argument->InsertAfter(insert_after); |
| 6193 | insert_after = push_argument; |
| 6194 | } |
| 6195 | |
| 6196 | HArgumentsElements* arguments_elements = |
| 6197 | new(zone()) HArgumentsElements(true); |
| 6198 | arguments_elements->ClearFlag(HValue::kUseGVN); |
| 6199 | arguments_elements->InsertAfter(insert_after); |
| 6200 | function_state()->set_arguments_elements(arguments_elements); |
| 6201 | } |
| 6202 | |
| 6203 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6204 | bool HGraphBuilder::TryArgumentsAccess(Property* expr) { |
| 6205 | VariableProxy* proxy = expr->obj()->AsVariableProxy(); |
| 6206 | if (proxy == NULL) return false; |
| 6207 | if (!proxy->var()->IsStackAllocated()) return false; |
| 6208 | if (!environment()->Lookup(proxy->var())->CheckFlag(HValue::kIsArguments)) { |
| 6209 | return false; |
| 6210 | } |
| 6211 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6212 | HInstruction* result = NULL; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6213 | if (expr->key()->IsPropertyName()) { |
| 6214 | Handle<String> name = expr->key()->AsLiteral()->AsPropertyName(); |
| 6215 | if (!name->IsEqualTo(CStrVector("length"))) return false; |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6216 | |
| 6217 | if (function_state()->outer() == NULL) { |
| 6218 | HInstruction* elements = AddInstruction( |
| 6219 | new(zone()) HArgumentsElements(false)); |
| 6220 | result = new(zone()) HArgumentsLength(elements); |
| 6221 | } else { |
| 6222 | // Number of arguments without receiver. |
| 6223 | int argument_count = environment()-> |
| 6224 | arguments_environment()->parameter_count() - 1; |
| 6225 | result = new(zone()) HConstant( |
| 6226 | Handle<Object>(Smi::FromInt(argument_count)), |
| 6227 | Representation::Integer32()); |
| 6228 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6229 | } else { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 6230 | Push(graph()->GetArgumentsObject()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6231 | VisitForValue(expr->key()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6232 | if (HasStackOverflow() || current_block() == NULL) return true; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6233 | HValue* key = Pop(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 6234 | Drop(1); // Arguments object. |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6235 | if (function_state()->outer() == NULL) { |
| 6236 | HInstruction* elements = AddInstruction( |
| 6237 | new(zone()) HArgumentsElements(false)); |
| 6238 | HInstruction* length = AddInstruction( |
| 6239 | new(zone()) HArgumentsLength(elements)); |
| 6240 | HInstruction* checked_key = |
| 6241 | AddInstruction(new(zone()) HBoundsCheck(key, length)); |
| 6242 | result = new(zone()) HAccessArgumentsAt(elements, length, checked_key); |
| 6243 | } else { |
| 6244 | EnsureArgumentsArePushedForAccess(); |
| 6245 | |
| 6246 | // Number of arguments without receiver. |
| 6247 | HInstruction* elements = function_state()->arguments_elements(); |
| 6248 | int argument_count = environment()-> |
| 6249 | arguments_environment()->parameter_count() - 1; |
| 6250 | HInstruction* length = AddInstruction(new(zone()) HConstant( |
| 6251 | Handle<Object>(Smi::FromInt(argument_count)), |
| 6252 | Representation::Integer32())); |
| 6253 | HInstruction* checked_key = |
| 6254 | AddInstruction(new(zone()) HBoundsCheck(key, length)); |
| 6255 | result = new(zone()) HAccessArgumentsAt(elements, length, checked_key); |
| 6256 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6257 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6258 | ast_context()->ReturnInstruction(result, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6259 | return true; |
| 6260 | } |
| 6261 | |
| 6262 | |
| 6263 | void HGraphBuilder::VisitProperty(Property* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6264 | ASSERT(!HasStackOverflow()); |
| 6265 | ASSERT(current_block() != NULL); |
| 6266 | ASSERT(current_block()->HasPredecessor()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 6267 | expr->RecordTypeFeedback(oracle(), zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6268 | |
| 6269 | if (TryArgumentsAccess(expr)) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6270 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6271 | CHECK_ALIVE(VisitForValue(expr->obj())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6272 | |
| 6273 | HInstruction* instr = NULL; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 6274 | if (expr->AsProperty()->IsArrayLength()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6275 | HValue* array = Pop(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6276 | AddInstruction(new(zone()) HCheckNonSmi(array)); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 6277 | HInstruction* mapcheck = |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 6278 | AddInstruction(HCheckInstanceType::NewIsJSArray(array, zone())); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 6279 | instr = new(zone()) HJSArrayLength(array, mapcheck); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 6280 | } else if (expr->IsStringLength()) { |
| 6281 | HValue* string = Pop(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6282 | AddInstruction(new(zone()) HCheckNonSmi(string)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 6283 | AddInstruction(HCheckInstanceType::NewIsString(string, zone())); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6284 | instr = new(zone()) HStringLength(string); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 6285 | } else if (expr->IsStringAccess()) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6286 | CHECK_ALIVE(VisitForValue(expr->key())); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 6287 | HValue* index = Pop(); |
| 6288 | HValue* string = Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6289 | HValue* context = environment()->LookupContext(); |
| 6290 | HStringCharCodeAt* char_code = |
| 6291 | BuildStringCharCodeAt(context, string, index); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 6292 | AddInstruction(char_code); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6293 | instr = new(zone()) HStringCharFromCode(context, char_code); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 6294 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 6295 | } else if (expr->IsFunctionPrototype()) { |
| 6296 | HValue* function = Pop(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6297 | AddInstruction(new(zone()) HCheckNonSmi(function)); |
| 6298 | instr = new(zone()) HLoadFunctionPrototype(function); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6299 | |
| 6300 | } else if (expr->key()->IsPropertyName()) { |
| 6301 | Handle<String> name = expr->key()->AsLiteral()->AsPropertyName(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 6302 | SmallMapList* types = expr->GetReceiverTypes(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6303 | |
| 6304 | HValue* obj = Pop(); |
| 6305 | if (expr->IsMonomorphic()) { |
| 6306 | instr = BuildLoadNamed(obj, expr, types->first(), name); |
| 6307 | } else if (types != NULL && types->length() > 1) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6308 | AddInstruction(new(zone()) HCheckNonSmi(obj)); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 6309 | HandlePolymorphicLoadNamedField(expr, obj, types, name); |
| 6310 | return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6311 | } else { |
| 6312 | instr = BuildLoadNamedGeneric(obj, expr); |
| 6313 | } |
| 6314 | |
| 6315 | } else { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6316 | CHECK_ALIVE(VisitForValue(expr->key())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6317 | |
| 6318 | HValue* key = Pop(); |
| 6319 | HValue* obj = Pop(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6320 | |
| 6321 | bool has_side_effects = false; |
| 6322 | HValue* load = HandleKeyedElementAccess( |
| 6323 | obj, key, NULL, expr, expr->id(), expr->position(), |
| 6324 | false, // is_store |
| 6325 | &has_side_effects); |
| 6326 | if (has_side_effects) { |
| 6327 | if (ast_context()->IsEffect()) { |
| 6328 | AddSimulate(expr->id()); |
| 6329 | } else { |
| 6330 | Push(load); |
| 6331 | AddSimulate(expr->id()); |
| 6332 | Drop(1); |
| 6333 | } |
| 6334 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6335 | return ast_context()->ReturnValue(load); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6336 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6337 | instr->set_position(expr->position()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6338 | return ast_context()->ReturnInstruction(instr, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6339 | } |
| 6340 | |
| 6341 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6342 | void HGraphBuilder::AddCheckConstantFunction(Handle<JSObject> holder, |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6343 | HValue* receiver, |
| 6344 | Handle<Map> receiver_map, |
| 6345 | bool smi_and_map_check) { |
| 6346 | // Constant functions have the nice property that the map will change if they |
| 6347 | // are overwritten. Therefore it is enough to check the map of the holder and |
| 6348 | // its prototypes. |
| 6349 | if (smi_and_map_check) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6350 | AddInstruction(new(zone()) HCheckNonSmi(receiver)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 6351 | AddInstruction(HCheckMaps::NewWithTransitions(receiver, receiver_map, |
| 6352 | zone())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6353 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6354 | if (!holder.is_null()) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6355 | AddInstruction(new(zone()) HCheckPrototypeMaps( |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6356 | Handle<JSObject>(JSObject::cast(receiver_map->prototype())), holder)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6357 | } |
| 6358 | } |
| 6359 | |
| 6360 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 6361 | class FunctionSorter { |
| 6362 | public: |
| 6363 | FunctionSorter() : index_(0), ticks_(0), ast_length_(0), src_length_(0) { } |
| 6364 | FunctionSorter(int index, int ticks, int ast_length, int src_length) |
| 6365 | : index_(index), |
| 6366 | ticks_(ticks), |
| 6367 | ast_length_(ast_length), |
| 6368 | src_length_(src_length) { } |
| 6369 | |
| 6370 | int index() const { return index_; } |
| 6371 | int ticks() const { return ticks_; } |
| 6372 | int ast_length() const { return ast_length_; } |
| 6373 | int src_length() const { return src_length_; } |
| 6374 | |
| 6375 | private: |
| 6376 | int index_; |
| 6377 | int ticks_; |
| 6378 | int ast_length_; |
| 6379 | int src_length_; |
| 6380 | }; |
| 6381 | |
| 6382 | |
| 6383 | static int CompareHotness(void const* a, void const* b) { |
| 6384 | FunctionSorter const* function1 = reinterpret_cast<FunctionSorter const*>(a); |
| 6385 | FunctionSorter const* function2 = reinterpret_cast<FunctionSorter const*>(b); |
| 6386 | int diff = function1->ticks() - function2->ticks(); |
| 6387 | if (diff != 0) return -diff; |
| 6388 | diff = function1->ast_length() - function2->ast_length(); |
| 6389 | if (diff != 0) return diff; |
| 6390 | return function1->src_length() - function2->src_length(); |
| 6391 | } |
| 6392 | |
| 6393 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6394 | void HGraphBuilder::HandlePolymorphicCallNamed(Call* expr, |
| 6395 | HValue* receiver, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 6396 | SmallMapList* types, |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6397 | Handle<String> name) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6398 | // TODO(ager): We should recognize when the prototype chains for different |
| 6399 | // maps are identical. In that case we can avoid repeatedly generating the |
| 6400 | // same prototype map checks. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 6401 | int argument_count = expr->arguments()->length() + 1; // Includes receiver. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 6402 | HBasicBlock* join = NULL; |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 6403 | FunctionSorter order[kMaxCallPolymorphism]; |
| 6404 | int ordered_functions = 0; |
| 6405 | for (int i = 0; |
| 6406 | i < types->length() && ordered_functions < kMaxCallPolymorphism; |
| 6407 | ++i) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6408 | Handle<Map> map = types->at(i); |
| 6409 | if (expr->ComputeTarget(map, name)) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 6410 | order[ordered_functions++] = |
| 6411 | FunctionSorter(i, |
| 6412 | expr->target()->shared()->profiler_ticks(), |
| 6413 | InliningAstSize(expr->target()), |
| 6414 | expr->target()->shared()->SourceSize()); |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 6415 | } |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 6416 | } |
| 6417 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 6418 | qsort(reinterpret_cast<void*>(&order[0]), |
| 6419 | ordered_functions, |
| 6420 | sizeof(order[0]), |
| 6421 | &CompareHotness); |
| 6422 | |
| 6423 | for (int fn = 0; fn < ordered_functions; ++fn) { |
| 6424 | int i = order[fn].index(); |
| 6425 | Handle<Map> map = types->at(i); |
| 6426 | if (fn == 0) { |
| 6427 | // Only needed once. |
| 6428 | AddInstruction(new(zone()) HCheckNonSmi(receiver)); |
| 6429 | join = graph()->CreateBasicBlock(); |
| 6430 | } |
| 6431 | HBasicBlock* if_true = graph()->CreateBasicBlock(); |
| 6432 | HBasicBlock* if_false = graph()->CreateBasicBlock(); |
| 6433 | HCompareMap* compare = |
| 6434 | new(zone()) HCompareMap(receiver, map, if_true, if_false); |
| 6435 | current_block()->Finish(compare); |
| 6436 | |
| 6437 | set_current_block(if_true); |
| 6438 | expr->ComputeTarget(map, name); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6439 | AddCheckConstantFunction(expr->holder(), receiver, map, false); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 6440 | if (FLAG_trace_inlining && FLAG_polymorphic_inlining) { |
| 6441 | Handle<JSFunction> caller = info()->closure(); |
| 6442 | SmartArrayPointer<char> caller_name = |
| 6443 | caller->shared()->DebugName()->ToCString(); |
| 6444 | PrintF("Trying to inline the polymorphic call to %s from %s\n", |
| 6445 | *name->ToCString(), |
| 6446 | *caller_name); |
| 6447 | } |
| 6448 | if (FLAG_polymorphic_inlining && TryInlineCall(expr)) { |
| 6449 | // Trying to inline will signal that we should bailout from the |
| 6450 | // entire compilation by setting stack overflow on the visitor. |
| 6451 | if (HasStackOverflow()) return; |
| 6452 | } else { |
| 6453 | HCallConstantFunction* call = |
| 6454 | new(zone()) HCallConstantFunction(expr->target(), argument_count); |
| 6455 | call->set_position(expr->position()); |
| 6456 | PreProcessCall(call); |
| 6457 | AddInstruction(call); |
| 6458 | if (!ast_context()->IsEffect()) Push(call); |
| 6459 | } |
| 6460 | |
| 6461 | if (current_block() != NULL) current_block()->Goto(join); |
| 6462 | set_current_block(if_false); |
| 6463 | } |
| 6464 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 6465 | // Finish up. Unconditionally deoptimize if we've handled all the maps we |
| 6466 | // know about and do not want to handle ones we've never seen. Otherwise |
| 6467 | // use a generic IC. |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 6468 | if (ordered_functions == types->length() && FLAG_deoptimize_uncommon_cases) { |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 6469 | current_block()->FinishExitWithDeoptimization(HDeoptimize::kNoUses); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 6470 | } else { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6471 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6472 | HCallNamed* call = new(zone()) HCallNamed(context, name, argument_count); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6473 | call->set_position(expr->position()); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 6474 | PreProcessCall(call); |
lrn@chromium.org | 8541d77 | 2010-12-15 12:05:09 +0000 | [diff] [blame] | 6475 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 6476 | if (join != NULL) { |
| 6477 | AddInstruction(call); |
| 6478 | if (!ast_context()->IsEffect()) Push(call); |
| 6479 | current_block()->Goto(join); |
| 6480 | } else { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6481 | return ast_context()->ReturnInstruction(call, expr->id()); |
kmillikin@chromium.org | c278c96 | 2011-01-06 08:52:11 +0000 | [diff] [blame] | 6482 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6483 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 6484 | |
| 6485 | // We assume that control flow is always live after an expression. So |
| 6486 | // even without predecessors to the join block, we set it as the exit |
| 6487 | // block and continue by adding instructions there. |
| 6488 | ASSERT(join != NULL); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 6489 | if (join->HasPredecessor()) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6490 | set_current_block(join); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 6491 | join->SetJoinId(expr->id()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6492 | if (!ast_context()->IsEffect()) return ast_context()->ReturnValue(Pop()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6493 | } else { |
| 6494 | set_current_block(NULL); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 6495 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6496 | } |
| 6497 | |
| 6498 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 6499 | void HGraphBuilder::TraceInline(Handle<JSFunction> target, |
| 6500 | Handle<JSFunction> caller, |
| 6501 | const char* reason) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6502 | if (FLAG_trace_inlining) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 6503 | SmartArrayPointer<char> target_name = |
| 6504 | target->shared()->DebugName()->ToCString(); |
| 6505 | SmartArrayPointer<char> caller_name = |
| 6506 | caller->shared()->DebugName()->ToCString(); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6507 | if (reason == NULL) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 6508 | PrintF("Inlined %s called from %s.\n", *target_name, *caller_name); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6509 | } else { |
| 6510 | PrintF("Did not inline %s called from %s (%s).\n", |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 6511 | *target_name, *caller_name, reason); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6512 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6513 | } |
| 6514 | } |
| 6515 | |
| 6516 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 6517 | static const int kNotInlinable = 1000000000; |
| 6518 | |
| 6519 | |
| 6520 | int HGraphBuilder::InliningAstSize(Handle<JSFunction> target) { |
| 6521 | if (!FLAG_use_inlining) return kNotInlinable; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6522 | |
| 6523 | // Precondition: call is monomorphic and we have found a target with the |
| 6524 | // appropriate arity. |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 6525 | Handle<JSFunction> caller = info()->closure(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 6526 | Handle<SharedFunctionInfo> target_shared(target->shared()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6527 | |
| 6528 | // Do a quick check on source code length to avoid parsing large |
| 6529 | // inlining candidates. |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 6530 | if (target_shared->SourceSize() > |
| 6531 | Min(FLAG_max_inlined_source_size, kUnlimitedMaxInlinedSourceSize)) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 6532 | TraceInline(target, caller, "target text too big"); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 6533 | return kNotInlinable; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6534 | } |
| 6535 | |
| 6536 | // Target must be inlineable. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6537 | if (!target->IsInlineable()) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 6538 | TraceInline(target, caller, "target not inlineable"); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 6539 | return kNotInlinable; |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6540 | } |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 6541 | if (target_shared->dont_inline() || target_shared->dont_optimize()) { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 6542 | TraceInline(target, caller, "target contains unsupported syntax [early]"); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 6543 | return kNotInlinable; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 6544 | } |
| 6545 | |
| 6546 | int nodes_added = target_shared->ast_node_count(); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 6547 | return nodes_added; |
| 6548 | } |
| 6549 | |
| 6550 | |
| 6551 | bool HGraphBuilder::TryInline(CallKind call_kind, |
| 6552 | Handle<JSFunction> target, |
| 6553 | ZoneList<Expression*>* arguments, |
| 6554 | HValue* receiver, |
| 6555 | int ast_id, |
| 6556 | int return_id, |
| 6557 | ReturnHandlingFlag return_handling) { |
| 6558 | int nodes_added = InliningAstSize(target); |
| 6559 | if (nodes_added == kNotInlinable) return false; |
| 6560 | |
| 6561 | Handle<JSFunction> caller = info()->closure(); |
| 6562 | |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 6563 | if (nodes_added > Min(FLAG_max_inlined_nodes, kUnlimitedMaxInlinedNodes)) { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 6564 | TraceInline(target, caller, "target AST is too large [early]"); |
| 6565 | return false; |
| 6566 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6567 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 6568 | Handle<SharedFunctionInfo> target_shared(target->shared()); |
| 6569 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6570 | #if !defined(V8_TARGET_ARCH_IA32) |
| 6571 | // Target must be able to use caller's context. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6572 | CompilationInfo* outer_info = info(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6573 | if (target->context() != outer_info->closure()->context() || |
| 6574 | outer_info->scope()->contains_with() || |
| 6575 | outer_info->scope()->num_heap_slots() > 0) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 6576 | TraceInline(target, caller, "target requires context change"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6577 | return false; |
| 6578 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6579 | #endif |
| 6580 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6581 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6582 | // Don't inline deeper than kMaxInliningLevels calls. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6583 | HEnvironment* env = environment(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6584 | int current_level = 1; |
| 6585 | while (env->outer() != NULL) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6586 | if (current_level == Compiler::kMaxInliningLevels) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 6587 | TraceInline(target, caller, "inline depth limit reached"); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6588 | return false; |
| 6589 | } |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 6590 | if (env->outer()->frame_type() == JS_FUNCTION) { |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 6591 | current_level++; |
| 6592 | } |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6593 | env = env->outer(); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6594 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6595 | |
| 6596 | // Don't inline recursive functions. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6597 | for (FunctionState* state = function_state(); |
| 6598 | state != NULL; |
| 6599 | state = state->outer()) { |
| 6600 | if (state->compilation_info()->closure()->shared() == *target_shared) { |
| 6601 | TraceInline(target, caller, "target is recursive"); |
| 6602 | return false; |
| 6603 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6604 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6605 | |
| 6606 | // We don't want to add more than a certain number of nodes from inlining. |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 6607 | if (inlined_count_ > Min(FLAG_max_inlined_nodes_cumulative, |
| 6608 | kUnlimitedMaxInlinedNodesCumulative)) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 6609 | TraceInline(target, caller, "cumulative AST node limit reached"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6610 | return false; |
| 6611 | } |
| 6612 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6613 | // Parse and allocate variables. |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 6614 | CompilationInfo target_info(target, zone()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 6615 | if (!ParserApi::Parse(&target_info, kNoParsingFlags) || |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6616 | !Scope::Analyze(&target_info)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6617 | if (target_info.isolate()->has_pending_exception()) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6618 | // Parse or scope error, never optimize this function. |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 6619 | SetStackOverflow(); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 6620 | target_shared->DisableOptimization(); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 6621 | } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 6622 | TraceInline(target, caller, "parse failure"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6623 | return false; |
| 6624 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6625 | |
| 6626 | if (target_info.scope()->num_heap_slots() > 0) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 6627 | TraceInline(target, caller, "target has context-allocated variables"); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6628 | return false; |
| 6629 | } |
| 6630 | FunctionLiteral* function = target_info.function(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6631 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 6632 | // The following conditions must be checked again after re-parsing, because |
| 6633 | // earlier the information might not have been complete due to lazy parsing. |
| 6634 | nodes_added = function->ast_node_count(); |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 6635 | if (nodes_added > Min(FLAG_max_inlined_nodes, kUnlimitedMaxInlinedNodes)) { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 6636 | TraceInline(target, caller, "target AST is too large [late]"); |
| 6637 | return false; |
| 6638 | } |
| 6639 | AstProperties::Flags* flags(function->flags()); |
| 6640 | if (flags->Contains(kDontInline) || flags->Contains(kDontOptimize)) { |
| 6641 | TraceInline(target, caller, "target contains unsupported syntax [late]"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6642 | return false; |
| 6643 | } |
| 6644 | |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 6645 | // If the function uses the arguments object check that inlining of functions |
| 6646 | // with arguments object is enabled and the arguments-variable is |
| 6647 | // stack allocated. |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 6648 | if (function->scope()->arguments() != NULL) { |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 6649 | if (!FLAG_inline_arguments) { |
| 6650 | TraceInline(target, caller, "target uses arguments object"); |
| 6651 | return false; |
| 6652 | } |
| 6653 | |
| 6654 | if (!function->scope()->arguments()->IsStackAllocated()) { |
| 6655 | TraceInline(target, |
| 6656 | caller, |
| 6657 | "target uses non-stackallocated arguments object"); |
| 6658 | return false; |
| 6659 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6660 | } |
| 6661 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 6662 | // All declarations must be inlineable. |
| 6663 | ZoneList<Declaration*>* decls = target_info.scope()->declarations(); |
| 6664 | int decl_count = decls->length(); |
| 6665 | for (int i = 0; i < decl_count; ++i) { |
| 6666 | if (!decls->at(i)->IsInlineable()) { |
| 6667 | TraceInline(target, caller, "target has non-trivial declaration"); |
| 6668 | return false; |
| 6669 | } |
| 6670 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6671 | |
| 6672 | // Generate the deoptimization data for the unoptimized version of |
| 6673 | // the target function if we don't already have it. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6674 | if (!target_shared->has_deoptimization_support()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6675 | // Note that we compile here using the same AST that we will use for |
| 6676 | // generating the optimized inline code. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6677 | target_info.EnableDeoptimizationSupport(); |
| 6678 | if (!FullCodeGenerator::MakeCode(&target_info)) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 6679 | TraceInline(target, caller, "could not generate deoptimization info"); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6680 | return false; |
| 6681 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 6682 | if (target_shared->scope_info() == ScopeInfo::Empty()) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6683 | // The scope info might not have been set if a lazily compiled |
| 6684 | // function is inlined before being called for the first time. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 6685 | Handle<ScopeInfo> target_scope_info = |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 6686 | ScopeInfo::Create(target_info.scope(), zone()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 6687 | target_shared->set_scope_info(*target_scope_info); |
| 6688 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6689 | target_shared->EnableDeoptimizationSupport(*target_info.code()); |
| 6690 | Compiler::RecordFunctionCompilation(Logger::FUNCTION_TAG, |
| 6691 | &target_info, |
| 6692 | target_shared); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6693 | } |
| 6694 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6695 | // ---------------------------------------------------------------- |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 6696 | // After this point, we've made a decision to inline this function (so |
| 6697 | // TryInline should always return true). |
| 6698 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6699 | // Save the pending call context and type feedback oracle. Set up new ones |
| 6700 | // for the inlined function. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6701 | ASSERT(target_shared->has_deoptimization_support()); |
| 6702 | TypeFeedbackOracle target_oracle( |
| 6703 | Handle<Code>(target_shared->code()), |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 6704 | Handle<Context>(target->context()->global_context()), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 6705 | isolate(), |
| 6706 | zone()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6707 | // The function state is new-allocated because we need to delete it |
| 6708 | // in two different places. |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 6709 | FunctionState* target_state = new FunctionState( |
| 6710 | this, &target_info, &target_oracle, return_handling); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6711 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 6712 | HConstant* undefined = graph()->GetConstantUndefined(); |
| 6713 | HEnvironment* inner_env = |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 6714 | environment()->CopyForInlining(target, |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 6715 | arguments->length(), |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 6716 | function, |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 6717 | undefined, |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 6718 | call_kind, |
| 6719 | function_state()->is_construct()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6720 | #ifdef V8_TARGET_ARCH_IA32 |
| 6721 | // IA32 only, overwrite the caller's context in the deoptimization |
| 6722 | // environment with the correct one. |
| 6723 | // |
| 6724 | // TODO(kmillikin): implement the same inlining on other platforms so we |
| 6725 | // can remove the unsightly ifdefs in this function. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 6726 | HConstant* context = |
| 6727 | new(zone()) HConstant(Handle<Context>(target->context()), |
| 6728 | Representation::Tagged()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6729 | AddInstruction(context); |
| 6730 | inner_env->BindContext(context); |
| 6731 | #endif |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6732 | |
danno@chromium.org | b2a1c07 | 2012-03-23 15:47:56 +0000 | [diff] [blame] | 6733 | AddSimulate(return_id); |
| 6734 | current_block()->UpdateEnvironment(inner_env); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6735 | |
| 6736 | ZoneList<HValue*>* arguments_values = NULL; |
| 6737 | |
| 6738 | // If the function uses arguments copy current arguments values |
| 6739 | // to use them for materialization. |
| 6740 | if (function->scope()->arguments() != NULL) { |
| 6741 | HEnvironment* arguments_env = inner_env->arguments_environment(); |
| 6742 | int arguments_count = arguments_env->parameter_count(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 6743 | arguments_values = new(zone()) ZoneList<HValue*>(arguments_count, zone()); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6744 | for (int i = 0; i < arguments_count; i++) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 6745 | arguments_values->Add(arguments_env->Lookup(i), zone()); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6746 | } |
| 6747 | } |
| 6748 | |
| 6749 | HEnterInlined* enter_inlined = |
| 6750 | new(zone()) HEnterInlined(target, |
| 6751 | arguments->length(), |
| 6752 | function, |
| 6753 | call_kind, |
| 6754 | function_state()->is_construct(), |
| 6755 | function->scope()->arguments(), |
| 6756 | arguments_values); |
| 6757 | function_state()->set_entry(enter_inlined); |
| 6758 | AddInstruction(enter_inlined); |
| 6759 | |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 6760 | // If the function uses arguments object create and bind one. |
| 6761 | if (function->scope()->arguments() != NULL) { |
| 6762 | ASSERT(function->scope()->arguments()->IsStackAllocated()); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6763 | inner_env->Bind(function->scope()->arguments(), |
| 6764 | graph()->GetArgumentsObject()); |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 6765 | } |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6766 | |
| 6767 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 6768 | VisitDeclarations(target_info.scope()->declarations()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 6769 | VisitStatements(function->body()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6770 | if (HasStackOverflow()) { |
| 6771 | // Bail out if the inline function did, as we cannot residualize a call |
| 6772 | // instead. |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 6773 | TraceInline(target, caller, "inline graph construction failed"); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 6774 | target_shared->DisableOptimization(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 6775 | inline_bailout_ = true; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6776 | delete target_state; |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6777 | return true; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6778 | } |
| 6779 | |
| 6780 | // Update inlined nodes count. |
| 6781 | inlined_count_ += nodes_added; |
| 6782 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 6783 | TraceInline(target, caller, NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6784 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 6785 | if (current_block() != NULL) { |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 6786 | // Add default return value (i.e. undefined for normals calls or the newly |
| 6787 | // allocated receiver for construct calls) if control can fall off the |
| 6788 | // body. In a test context, undefined is false and any JSObject is true. |
| 6789 | if (call_context()->IsValue()) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6790 | ASSERT(function_return() != NULL); |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 6791 | HValue* return_value = function_state()->is_construct() |
| 6792 | ? receiver |
| 6793 | : undefined; |
| 6794 | current_block()->AddLeaveInlined(return_value, |
| 6795 | function_return(), |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6796 | function_state()); |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 6797 | } else if (call_context()->IsEffect()) { |
| 6798 | ASSERT(function_return() != NULL); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6799 | current_block()->Goto(function_return(), function_state()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6800 | } else { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6801 | ASSERT(call_context()->IsTest()); |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 6802 | ASSERT(inlined_test_context() != NULL); |
| 6803 | HBasicBlock* target = function_state()->is_construct() |
| 6804 | ? inlined_test_context()->if_true() |
| 6805 | : inlined_test_context()->if_false(); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6806 | current_block()->Goto(target, function_state()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6807 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6808 | } |
| 6809 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6810 | // Fix up the function exits. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6811 | if (inlined_test_context() != NULL) { |
| 6812 | HBasicBlock* if_true = inlined_test_context()->if_true(); |
| 6813 | HBasicBlock* if_false = inlined_test_context()->if_false(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 6814 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6815 | // Pop the return test context from the expression context stack. |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 6816 | ASSERT(ast_context() == inlined_test_context()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6817 | ClearInlinedTestContext(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6818 | delete target_state; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6819 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6820 | // Forward to the real test context. |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 6821 | if (if_true->HasPredecessor()) { |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 6822 | if_true->SetJoinId(ast_id); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 6823 | HBasicBlock* true_target = TestContext::cast(ast_context())->if_true(); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6824 | if_true->Goto(true_target, function_state()); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 6825 | } |
| 6826 | if (if_false->HasPredecessor()) { |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 6827 | if_false->SetJoinId(ast_id); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 6828 | HBasicBlock* false_target = TestContext::cast(ast_context())->if_false(); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6829 | if_false->Goto(false_target, function_state()); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 6830 | } |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 6831 | set_current_block(NULL); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6832 | return true; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6833 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6834 | } else if (function_return()->HasPredecessor()) { |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 6835 | function_return()->SetJoinId(ast_id); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 6836 | set_current_block(function_return()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6837 | } else { |
| 6838 | set_current_block(NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6839 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6840 | delete target_state; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6841 | return true; |
| 6842 | } |
| 6843 | |
| 6844 | |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 6845 | bool HGraphBuilder::TryInlineCall(Call* expr, bool drop_extra) { |
| 6846 | // The function call we are inlining is a method call if the call |
| 6847 | // is a property call. |
| 6848 | CallKind call_kind = (expr->expression()->AsProperty() == NULL) |
| 6849 | ? CALL_AS_FUNCTION |
| 6850 | : CALL_AS_METHOD; |
| 6851 | |
| 6852 | return TryInline(call_kind, |
| 6853 | expr->target(), |
| 6854 | expr->arguments(), |
| 6855 | NULL, |
| 6856 | expr->id(), |
| 6857 | expr->ReturnId(), |
| 6858 | drop_extra ? DROP_EXTRA_ON_RETURN : NORMAL_RETURN); |
| 6859 | } |
| 6860 | |
| 6861 | |
| 6862 | bool HGraphBuilder::TryInlineConstruct(CallNew* expr, HValue* receiver) { |
| 6863 | return TryInline(CALL_AS_FUNCTION, |
| 6864 | expr->target(), |
| 6865 | expr->arguments(), |
| 6866 | receiver, |
| 6867 | expr->id(), |
| 6868 | expr->ReturnId(), |
| 6869 | CONSTRUCT_CALL_RETURN); |
| 6870 | } |
| 6871 | |
| 6872 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 6873 | bool HGraphBuilder::TryInlineBuiltinFunctionCall(Call* expr, bool drop_extra) { |
| 6874 | if (!expr->target()->shared()->HasBuiltinFunctionId()) return false; |
| 6875 | BuiltinFunctionId id = expr->target()->shared()->builtin_function_id(); |
| 6876 | switch (id) { |
| 6877 | case kMathRound: |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 6878 | case kMathAbs: |
| 6879 | case kMathSqrt: |
| 6880 | case kMathLog: |
| 6881 | case kMathSin: |
| 6882 | case kMathCos: |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 6883 | case kMathTan: |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 6884 | if (expr->arguments()->length() == 1) { |
| 6885 | HValue* argument = Pop(); |
| 6886 | HValue* context = environment()->LookupContext(); |
| 6887 | Drop(1); // Receiver. |
| 6888 | HUnaryMathOperation* op = |
| 6889 | new(zone()) HUnaryMathOperation(context, argument, id); |
| 6890 | op->set_position(expr->position()); |
| 6891 | if (drop_extra) Drop(1); // Optionally drop the function. |
| 6892 | ast_context()->ReturnInstruction(op, expr->id()); |
| 6893 | return true; |
| 6894 | } |
| 6895 | break; |
| 6896 | default: |
| 6897 | // Not supported for inlining yet. |
| 6898 | break; |
| 6899 | } |
| 6900 | return false; |
| 6901 | } |
| 6902 | |
| 6903 | |
| 6904 | bool HGraphBuilder::TryInlineBuiltinMethodCall(Call* expr, |
| 6905 | HValue* receiver, |
| 6906 | Handle<Map> receiver_map, |
| 6907 | CheckType check_type) { |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 6908 | ASSERT(check_type != RECEIVER_MAP_CHECK || !receiver_map.is_null()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6909 | // 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] | 6910 | if (!expr->target()->shared()->HasBuiltinFunctionId()) return false; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6911 | BuiltinFunctionId id = expr->target()->shared()->builtin_function_id(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6912 | int argument_count = expr->arguments()->length() + 1; // Plus receiver. |
| 6913 | switch (id) { |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 6914 | case kStringCharCodeAt: |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 6915 | case kStringCharAt: |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 6916 | if (argument_count == 2 && check_type == STRING_CHECK) { |
| 6917 | HValue* index = Pop(); |
| 6918 | HValue* string = Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6919 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 6920 | ASSERT(!expr->holder().is_null()); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6921 | AddInstruction(new(zone()) HCheckPrototypeMaps( |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 6922 | oracle()->GetPrototypeForPrimitiveCheck(STRING_CHECK), |
| 6923 | expr->holder())); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6924 | HStringCharCodeAt* char_code = |
| 6925 | BuildStringCharCodeAt(context, string, index); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 6926 | if (id == kStringCharCodeAt) { |
| 6927 | ast_context()->ReturnInstruction(char_code, expr->id()); |
| 6928 | return true; |
| 6929 | } |
| 6930 | AddInstruction(char_code); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6931 | HStringCharFromCode* result = |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6932 | new(zone()) HStringCharFromCode(context, char_code); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 6933 | ast_context()->ReturnInstruction(result, expr->id()); |
| 6934 | return true; |
| 6935 | } |
| 6936 | break; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6937 | case kMathRound: |
| 6938 | case kMathFloor: |
| 6939 | case kMathAbs: |
| 6940 | case kMathSqrt: |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6941 | case kMathLog: |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 6942 | case kMathSin: |
| 6943 | case kMathCos: |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 6944 | case kMathTan: |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 6945 | if (argument_count == 2 && check_type == RECEIVER_MAP_CHECK) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6946 | AddCheckConstantFunction(expr->holder(), receiver, receiver_map, true); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6947 | HValue* argument = Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6948 | HValue* context = environment()->LookupContext(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6949 | Drop(1); // Receiver. |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6950 | HUnaryMathOperation* op = |
| 6951 | new(zone()) HUnaryMathOperation(context, argument, id); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6952 | op->set_position(expr->position()); |
| 6953 | ast_context()->ReturnInstruction(op, expr->id()); |
| 6954 | return true; |
| 6955 | } |
| 6956 | break; |
| 6957 | case kMathPow: |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 6958 | if (argument_count == 3 && check_type == RECEIVER_MAP_CHECK) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6959 | AddCheckConstantFunction(expr->holder(), receiver, receiver_map, true); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6960 | HValue* right = Pop(); |
| 6961 | HValue* left = Pop(); |
| 6962 | Pop(); // Pop receiver. |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6963 | HValue* context = environment()->LookupContext(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6964 | HInstruction* result = NULL; |
| 6965 | // Use sqrt() if exponent is 0.5 or -0.5. |
| 6966 | if (right->IsConstant() && HConstant::cast(right)->HasDoubleValue()) { |
| 6967 | double exponent = HConstant::cast(right)->DoubleValue(); |
| 6968 | if (exponent == 0.5) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6969 | result = |
| 6970 | new(zone()) HUnaryMathOperation(context, left, kMathPowHalf); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6971 | } else if (exponent == -0.5) { |
| 6972 | HConstant* double_one = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6973 | new(zone()) HConstant(Handle<Object>(Smi::FromInt(1)), |
| 6974 | Representation::Double()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6975 | AddInstruction(double_one); |
| 6976 | HUnaryMathOperation* square_root = |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6977 | new(zone()) HUnaryMathOperation(context, left, kMathPowHalf); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6978 | AddInstruction(square_root); |
| 6979 | // MathPowHalf doesn't have side effects so there's no need for |
| 6980 | // an environment simulation here. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 6981 | ASSERT(!square_root->HasObservableSideEffects()); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6982 | result = new(zone()) HDiv(context, double_one, square_root); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6983 | } else if (exponent == 2.0) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6984 | result = new(zone()) HMul(context, left, left); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6985 | } |
| 6986 | } else if (right->IsConstant() && |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 6987 | HConstant::cast(right)->HasInteger32Value() && |
| 6988 | HConstant::cast(right)->Integer32Value() == 2) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 6989 | result = new(zone()) HMul(context, left, left); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6990 | } |
| 6991 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 6992 | if (result == NULL) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 6993 | result = new(zone()) HPower(left, right); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 6994 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6995 | ast_context()->ReturnInstruction(result, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6996 | return true; |
| 6997 | } |
| 6998 | break; |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6999 | case kMathRandom: |
| 7000 | if (argument_count == 1 && check_type == RECEIVER_MAP_CHECK) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7001 | AddCheckConstantFunction(expr->holder(), receiver, receiver_map, true); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 7002 | Drop(1); // Receiver. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 7003 | HValue* context = environment()->LookupContext(); |
| 7004 | HGlobalObject* global_object = new(zone()) HGlobalObject(context); |
| 7005 | AddInstruction(global_object); |
| 7006 | HRandom* result = new(zone()) HRandom(global_object); |
| 7007 | ast_context()->ReturnInstruction(result, expr->id()); |
| 7008 | return true; |
| 7009 | } |
| 7010 | break; |
| 7011 | case kMathMax: |
| 7012 | case kMathMin: |
| 7013 | if (argument_count == 3 && check_type == RECEIVER_MAP_CHECK) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7014 | AddCheckConstantFunction(expr->holder(), receiver, receiver_map, true); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 7015 | HValue* right = Pop(); |
| 7016 | HValue* left = Pop(); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 7017 | Pop(); // Pop receiver. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 7018 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 7019 | HValue* left_operand = left; |
| 7020 | HValue* right_operand = right; |
| 7021 | |
| 7022 | // If we do not have two integers, we convert to double for comparison. |
| 7023 | if (!left->representation().IsInteger32() || |
| 7024 | !right->representation().IsInteger32()) { |
| 7025 | if (!left->representation().IsDouble()) { |
| 7026 | HChange* left_convert = new(zone()) HChange( |
| 7027 | left, |
| 7028 | Representation::Double(), |
| 7029 | false, // Do not truncate when converting to double. |
| 7030 | true); // Deoptimize for undefined. |
| 7031 | left_convert->SetFlag(HValue::kBailoutOnMinusZero); |
| 7032 | left_operand = AddInstruction(left_convert); |
| 7033 | } |
| 7034 | if (!right->representation().IsDouble()) { |
| 7035 | HChange* right_convert = new(zone()) HChange( |
| 7036 | right, |
| 7037 | Representation::Double(), |
| 7038 | false, // Do not truncate when converting to double. |
| 7039 | true); // Deoptimize for undefined. |
| 7040 | right_convert->SetFlag(HValue::kBailoutOnMinusZero); |
| 7041 | right_operand = AddInstruction(right_convert); |
| 7042 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 7043 | } |
| 7044 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 7045 | ASSERT(left_operand->representation().Equals( |
| 7046 | right_operand->representation())); |
| 7047 | ASSERT(!left_operand->representation().IsTagged()); |
| 7048 | |
| 7049 | Token::Value op = (id == kMathMin) ? Token::LT : Token::GT; |
| 7050 | |
| 7051 | HCompareIDAndBranch* compare = |
| 7052 | new(zone()) HCompareIDAndBranch(left_operand, right_operand, op); |
| 7053 | compare->SetInputRepresentation(left_operand->representation()); |
| 7054 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 7055 | HBasicBlock* return_left = graph()->CreateBasicBlock(); |
| 7056 | HBasicBlock* return_right = graph()->CreateBasicBlock(); |
| 7057 | |
| 7058 | compare->SetSuccessorAt(0, return_left); |
| 7059 | compare->SetSuccessorAt(1, return_right); |
| 7060 | current_block()->Finish(compare); |
| 7061 | |
| 7062 | set_current_block(return_left); |
| 7063 | Push(left); |
| 7064 | set_current_block(return_right); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 7065 | // The branch above always returns the right operand if either of |
| 7066 | // them is NaN, but the spec requires that max/min(NaN, X) = NaN. |
| 7067 | // We add another branch that checks if the left operand is NaN or not. |
| 7068 | if (left_operand->representation().IsDouble()) { |
| 7069 | // If left_operand != left_operand then it is NaN. |
| 7070 | HCompareIDAndBranch* compare_nan = new(zone()) HCompareIDAndBranch( |
| 7071 | left_operand, left_operand, Token::EQ); |
| 7072 | compare_nan->SetInputRepresentation(left_operand->representation()); |
| 7073 | HBasicBlock* left_is_number = graph()->CreateBasicBlock(); |
| 7074 | HBasicBlock* left_is_nan = graph()->CreateBasicBlock(); |
| 7075 | compare_nan->SetSuccessorAt(0, left_is_number); |
| 7076 | compare_nan->SetSuccessorAt(1, left_is_nan); |
| 7077 | current_block()->Finish(compare_nan); |
| 7078 | set_current_block(left_is_nan); |
| 7079 | Push(left); |
| 7080 | set_current_block(left_is_number); |
| 7081 | Push(right); |
| 7082 | return_right = CreateJoin(left_is_number, left_is_nan, expr->id()); |
| 7083 | } else { |
| 7084 | Push(right); |
| 7085 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 7086 | |
| 7087 | HBasicBlock* join = CreateJoin(return_left, return_right, expr->id()); |
| 7088 | set_current_block(join); |
| 7089 | ast_context()->ReturnValue(Pop()); |
| 7090 | return true; |
| 7091 | } |
| 7092 | break; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7093 | default: |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7094 | // Not yet supported for inlining. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7095 | break; |
| 7096 | } |
| 7097 | return false; |
| 7098 | } |
| 7099 | |
| 7100 | |
| 7101 | bool HGraphBuilder::TryCallApply(Call* expr) { |
| 7102 | Expression* callee = expr->expression(); |
| 7103 | Property* prop = callee->AsProperty(); |
| 7104 | ASSERT(prop != NULL); |
| 7105 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 7106 | if (!expr->IsMonomorphic() || expr->check_type() != RECEIVER_MAP_CHECK) { |
| 7107 | return false; |
| 7108 | } |
| 7109 | Handle<Map> function_map = expr->GetReceiverTypes()->first(); |
| 7110 | if (function_map->instance_type() != JS_FUNCTION_TYPE || |
| 7111 | !expr->target()->shared()->HasBuiltinFunctionId() || |
| 7112 | expr->target()->shared()->builtin_function_id() != kFunctionApply) { |
| 7113 | return false; |
| 7114 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7115 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 7116 | if (info()->scope()->arguments() == NULL) return false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7117 | |
| 7118 | ZoneList<Expression*>* args = expr->arguments(); |
| 7119 | if (args->length() != 2) return false; |
| 7120 | |
| 7121 | VariableProxy* arg_two = args->at(1)->AsVariableProxy(); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 7122 | if (arg_two == NULL || !arg_two->var()->IsStackAllocated()) return false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7123 | HValue* arg_two_value = environment()->Lookup(arg_two->var()); |
| 7124 | if (!arg_two_value->CheckFlag(HValue::kIsArguments)) return false; |
| 7125 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7126 | // Found pattern f.apply(receiver, arguments). |
| 7127 | VisitForValue(prop->obj()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7128 | if (HasStackOverflow() || current_block() == NULL) return true; |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 7129 | HValue* function = Top(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7130 | AddCheckConstantFunction(expr->holder(), function, function_map, true); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 7131 | Drop(1); |
| 7132 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7133 | VisitForValue(args->at(0)); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7134 | if (HasStackOverflow() || current_block() == NULL) return true; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7135 | HValue* receiver = Pop(); |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7136 | |
| 7137 | if (function_state()->outer() == NULL) { |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 7138 | HInstruction* elements = AddInstruction( |
| 7139 | new(zone()) HArgumentsElements(false)); |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7140 | HInstruction* length = |
| 7141 | AddInstruction(new(zone()) HArgumentsLength(elements)); |
| 7142 | HValue* wrapped_receiver = |
| 7143 | AddInstruction(new(zone()) HWrapReceiver(receiver, function)); |
| 7144 | HInstruction* result = |
| 7145 | new(zone()) HApplyArguments(function, |
| 7146 | wrapped_receiver, |
| 7147 | length, |
| 7148 | elements); |
| 7149 | result->set_position(expr->position()); |
| 7150 | ast_context()->ReturnInstruction(result, expr->id()); |
| 7151 | return true; |
| 7152 | } else { |
| 7153 | // We are inside inlined function and we know exactly what is inside |
| 7154 | // arguments object. |
| 7155 | HValue* context = environment()->LookupContext(); |
| 7156 | |
| 7157 | HValue* wrapped_receiver = |
| 7158 | AddInstruction(new(zone()) HWrapReceiver(receiver, function)); |
| 7159 | PushAndAdd(new(zone()) HPushArgument(wrapped_receiver)); |
| 7160 | |
ulan@chromium.org | 6ff6514 | 2012-03-21 09:52:17 +0000 | [diff] [blame] | 7161 | HEnvironment* arguments_env = environment()->arguments_environment(); |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7162 | |
ulan@chromium.org | 6ff6514 | 2012-03-21 09:52:17 +0000 | [diff] [blame] | 7163 | int parameter_count = arguments_env->parameter_count(); |
| 7164 | for (int i = 1; i < arguments_env->parameter_count(); i++) { |
| 7165 | PushAndAdd(new(zone()) HPushArgument(arguments_env->Lookup(i))); |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7166 | } |
| 7167 | |
| 7168 | HInvokeFunction* call = new(zone()) HInvokeFunction( |
| 7169 | context, |
| 7170 | function, |
| 7171 | parameter_count); |
| 7172 | Drop(parameter_count); |
| 7173 | call->set_position(expr->position()); |
| 7174 | ast_context()->ReturnInstruction(call, expr->id()); |
| 7175 | return true; |
| 7176 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7177 | } |
| 7178 | |
| 7179 | |
| 7180 | void HGraphBuilder::VisitCall(Call* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7181 | ASSERT(!HasStackOverflow()); |
| 7182 | ASSERT(current_block() != NULL); |
| 7183 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7184 | Expression* callee = expr->expression(); |
| 7185 | int argument_count = expr->arguments()->length() + 1; // Plus receiver. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 7186 | HInstruction* call = NULL; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7187 | |
| 7188 | Property* prop = callee->AsProperty(); |
| 7189 | if (prop != NULL) { |
| 7190 | if (!prop->key()->IsPropertyName()) { |
| 7191 | // Keyed function call. |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7192 | CHECK_ALIVE(VisitArgument(prop->obj())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7193 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7194 | CHECK_ALIVE(VisitForValue(prop->key())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7195 | // Push receiver and key like the non-optimized code generator expects it. |
| 7196 | HValue* key = Pop(); |
| 7197 | HValue* receiver = Pop(); |
| 7198 | Push(key); |
| 7199 | Push(receiver); |
| 7200 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7201 | CHECK_ALIVE(VisitArgumentList(expr->arguments())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7202 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7203 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7204 | call = new(zone()) HCallKeyed(context, key, argument_count); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7205 | call->set_position(expr->position()); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7206 | Drop(argument_count + 1); // 1 is the key. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 7207 | return ast_context()->ReturnInstruction(call, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7208 | } |
| 7209 | |
| 7210 | // Named function call. |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 7211 | expr->RecordTypeFeedback(oracle(), CALL_AS_METHOD); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7212 | |
| 7213 | if (TryCallApply(expr)) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7214 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7215 | CHECK_ALIVE(VisitForValue(prop->obj())); |
| 7216 | CHECK_ALIVE(VisitExpressions(expr->arguments())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7217 | |
| 7218 | Handle<String> name = prop->key()->AsLiteral()->AsPropertyName(); |
| 7219 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7220 | SmallMapList* types = expr->GetReceiverTypes(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7221 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 7222 | HValue* receiver = |
| 7223 | environment()->ExpressionStackAt(expr->arguments()->length()); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 7224 | if (expr->IsMonomorphic()) { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7225 | Handle<Map> receiver_map = (types == NULL || types->is_empty()) |
| 7226 | ? Handle<Map>::null() |
| 7227 | : types->first(); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 7228 | if (TryInlineBuiltinMethodCall(expr, |
| 7229 | receiver, |
| 7230 | receiver_map, |
| 7231 | expr->check_type())) { |
| 7232 | if (FLAG_trace_inlining) { |
| 7233 | PrintF("Inlining builtin "); |
| 7234 | expr->target()->ShortPrint(); |
| 7235 | PrintF("\n"); |
| 7236 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7237 | return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7238 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7239 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7240 | if (CallStubCompiler::HasCustomCallGenerator(expr->target()) || |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 7241 | expr->check_type() != RECEIVER_MAP_CHECK) { |
| 7242 | // 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] | 7243 | // because it is likely to generate better code. Also use the IC |
| 7244 | // when a primitive receiver check is required. |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7245 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7246 | call = PreProcessCall( |
| 7247 | new(zone()) HCallNamed(context, name, argument_count)); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 7248 | } else { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7249 | AddCheckConstantFunction(expr->holder(), receiver, receiver_map, true); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 7250 | |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 7251 | if (TryInlineCall(expr)) return; |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7252 | call = PreProcessCall( |
| 7253 | new(zone()) HCallConstantFunction(expr->target(), |
| 7254 | argument_count)); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 7255 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7256 | } else if (types != NULL && types->length() > 1) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 7257 | ASSERT(expr->check_type() == RECEIVER_MAP_CHECK); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7258 | HandlePolymorphicCallNamed(expr, receiver, types, name); |
| 7259 | return; |
| 7260 | |
| 7261 | } else { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7262 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7263 | call = PreProcessCall( |
| 7264 | new(zone()) HCallNamed(context, name, argument_count)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7265 | } |
| 7266 | |
| 7267 | } else { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 7268 | expr->RecordTypeFeedback(oracle(), CALL_AS_FUNCTION); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 7269 | VariableProxy* proxy = expr->expression()->AsVariableProxy(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 7270 | bool global_call = proxy != NULL && proxy->var()->IsUnallocated(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7271 | |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 7272 | if (proxy != NULL && proxy->var()->is_possibly_eval()) { |
| 7273 | return Bailout("possible direct call to eval"); |
| 7274 | } |
| 7275 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7276 | if (global_call) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 7277 | Variable* var = proxy->var(); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 7278 | bool known_global_function = false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7279 | // If there is a global property cell for the name at compile time and |
| 7280 | // access check is not enabled we assume that the function will not change |
| 7281 | // and generate optimized code for calling the function. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7282 | LookupResult lookup(isolate()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7283 | GlobalPropertyAccess type = LookupGlobalProperty(var, &lookup, false); |
| 7284 | if (type == kUseCell && |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 7285 | !info()->global_object()->IsAccessCheckNeeded()) { |
| 7286 | Handle<GlobalObject> global(info()->global_object()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7287 | known_global_function = expr->ComputeGlobalTarget(global, &lookup); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 7288 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7289 | if (known_global_function) { |
| 7290 | // Push the global object instead of the global receiver because |
| 7291 | // code generated by the full code generator expects it. |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7292 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7293 | HGlobalObject* global_object = new(zone()) HGlobalObject(context); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 7294 | PushAndAdd(global_object); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7295 | CHECK_ALIVE(VisitExpressions(expr->arguments())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7296 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7297 | CHECK_ALIVE(VisitForValue(expr->expression())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7298 | HValue* function = Pop(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7299 | AddInstruction(new(zone()) HCheckFunction(function, expr->target())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7300 | |
| 7301 | // Replace the global object with the global receiver. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7302 | HGlobalReceiver* global_receiver = |
| 7303 | new(zone()) HGlobalReceiver(global_object); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7304 | // Index of the receiver from the top of the expression stack. |
| 7305 | const int receiver_index = argument_count - 1; |
| 7306 | AddInstruction(global_receiver); |
| 7307 | ASSERT(environment()->ExpressionStackAt(receiver_index)-> |
| 7308 | IsGlobalObject()); |
| 7309 | environment()->SetExpressionStackAt(receiver_index, global_receiver); |
| 7310 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 7311 | if (TryInlineBuiltinFunctionCall(expr, false)) { // Nothing to drop. |
| 7312 | if (FLAG_trace_inlining) { |
| 7313 | PrintF("Inlining builtin "); |
| 7314 | expr->target()->ShortPrint(); |
| 7315 | PrintF("\n"); |
| 7316 | } |
| 7317 | return; |
| 7318 | } |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 7319 | if (TryInlineCall(expr)) return; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 7320 | |
| 7321 | if (expr->target().is_identical_to(info()->closure())) { |
| 7322 | graph()->MarkRecursive(); |
| 7323 | } |
| 7324 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7325 | call = PreProcessCall(new(zone()) HCallKnownGlobal(expr->target(), |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7326 | argument_count)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7327 | } else { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7328 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7329 | HGlobalObject* receiver = new(zone()) HGlobalObject(context); |
| 7330 | AddInstruction(receiver); |
| 7331 | PushAndAdd(new(zone()) HPushArgument(receiver)); |
| 7332 | CHECK_ALIVE(VisitArgumentList(expr->arguments())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7333 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7334 | call = new(zone()) HCallGlobal(context, var->name(), argument_count); |
| 7335 | Drop(argument_count); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7336 | } |
| 7337 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 7338 | } else if (expr->IsMonomorphic()) { |
| 7339 | // The function is on the stack in the unoptimized code during |
| 7340 | // evaluation of the arguments. |
| 7341 | CHECK_ALIVE(VisitForValue(expr->expression())); |
| 7342 | HValue* function = Top(); |
| 7343 | HValue* context = environment()->LookupContext(); |
| 7344 | HGlobalObject* global = new(zone()) HGlobalObject(context); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 7345 | AddInstruction(global); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 7346 | HGlobalReceiver* receiver = new(zone()) HGlobalReceiver(global); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 7347 | PushAndAdd(receiver); |
| 7348 | CHECK_ALIVE(VisitExpressions(expr->arguments())); |
| 7349 | AddInstruction(new(zone()) HCheckFunction(function, expr->target())); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 7350 | |
| 7351 | if (TryInlineBuiltinFunctionCall(expr, true)) { // Drop the function. |
| 7352 | if (FLAG_trace_inlining) { |
| 7353 | PrintF("Inlining builtin "); |
| 7354 | expr->target()->ShortPrint(); |
| 7355 | PrintF("\n"); |
| 7356 | } |
| 7357 | return; |
| 7358 | } |
| 7359 | |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 7360 | if (TryInlineCall(expr, true)) { // Drop function from environment. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 7361 | return; |
| 7362 | } else { |
svenpanne@chromium.org | fb04633 | 2012-04-19 12:02:44 +0000 | [diff] [blame] | 7363 | call = PreProcessCall( |
| 7364 | new(zone()) HInvokeFunction(context, |
| 7365 | function, |
| 7366 | expr->target(), |
| 7367 | argument_count)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 7368 | Drop(1); // The function. |
| 7369 | } |
| 7370 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7371 | } else { |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 7372 | CHECK_ALIVE(VisitForValue(expr->expression())); |
| 7373 | HValue* function = Top(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7374 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7375 | HGlobalObject* global_object = new(zone()) HGlobalObject(context); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 7376 | AddInstruction(global_object); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 7377 | HGlobalReceiver* receiver = new(zone()) HGlobalReceiver(global_object); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7378 | AddInstruction(receiver); |
| 7379 | PushAndAdd(new(zone()) HPushArgument(receiver)); |
| 7380 | CHECK_ALIVE(VisitArgumentList(expr->arguments())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7381 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 7382 | call = new(zone()) HCallFunction(context, function, argument_count); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7383 | Drop(argument_count + 1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7384 | } |
| 7385 | } |
| 7386 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7387 | call->set_position(expr->position()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 7388 | return ast_context()->ReturnInstruction(call, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7389 | } |
| 7390 | |
| 7391 | |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 7392 | // Checks whether allocation using the given constructor can be inlined. |
| 7393 | static bool IsAllocationInlineable(Handle<JSFunction> constructor) { |
| 7394 | return constructor->has_initial_map() && |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 7395 | constructor->initial_map()->instance_type() == JS_OBJECT_TYPE && |
| 7396 | constructor->initial_map()->instance_size() < HAllocateObject::kMaxSize; |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 7397 | } |
| 7398 | |
| 7399 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7400 | void HGraphBuilder::VisitCallNew(CallNew* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7401 | ASSERT(!HasStackOverflow()); |
| 7402 | ASSERT(current_block() != NULL); |
| 7403 | ASSERT(current_block()->HasPredecessor()); |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 7404 | expr->RecordTypeFeedback(oracle()); |
| 7405 | int argument_count = expr->arguments()->length() + 1; // Plus constructor. |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7406 | HValue* context = environment()->LookupContext(); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 7407 | |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 7408 | if (FLAG_inline_construct && |
| 7409 | expr->IsMonomorphic() && |
| 7410 | IsAllocationInlineable(expr->target())) { |
| 7411 | // The constructor function is on the stack in the unoptimized code |
| 7412 | // during evaluation of the arguments. |
| 7413 | CHECK_ALIVE(VisitForValue(expr->expression())); |
| 7414 | HValue* function = Top(); |
| 7415 | CHECK_ALIVE(VisitExpressions(expr->arguments())); |
| 7416 | Handle<JSFunction> constructor = expr->target(); |
fschneider@chromium.org | 35814e5 | 2012-03-01 15:43:35 +0000 | [diff] [blame] | 7417 | HValue* check = AddInstruction( |
| 7418 | new(zone()) HCheckFunction(function, constructor)); |
| 7419 | |
| 7420 | // Force completion of inobject slack tracking before generating |
| 7421 | // allocation code to finalize instance size. |
| 7422 | if (constructor->shared()->IsInobjectSlackTrackingInProgress()) { |
| 7423 | constructor->shared()->CompleteInobjectSlackTracking(); |
| 7424 | } |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 7425 | |
| 7426 | // Replace the constructor function with a newly allocated receiver. |
| 7427 | HInstruction* receiver = new(zone()) HAllocateObject(context, constructor); |
| 7428 | // Index of the receiver from the top of the expression stack. |
| 7429 | const int receiver_index = argument_count - 1; |
| 7430 | AddInstruction(receiver); |
| 7431 | ASSERT(environment()->ExpressionStackAt(receiver_index) == function); |
| 7432 | environment()->SetExpressionStackAt(receiver_index, receiver); |
| 7433 | |
| 7434 | if (TryInlineConstruct(expr, receiver)) return; |
| 7435 | |
| 7436 | // TODO(mstarzinger): For now we remove the previous HAllocateObject and |
| 7437 | // add HPushArgument for the arguments in case inlining failed. What we |
| 7438 | // actually should do is emit HInvokeFunction on the constructor instead |
| 7439 | // of using HCallNew as a fallback. |
| 7440 | receiver->DeleteAndReplaceWith(NULL); |
fschneider@chromium.org | 35814e5 | 2012-03-01 15:43:35 +0000 | [diff] [blame] | 7441 | check->DeleteAndReplaceWith(NULL); |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 7442 | environment()->SetExpressionStackAt(receiver_index, function); |
| 7443 | HInstruction* call = PreProcessCall( |
| 7444 | new(zone()) HCallNew(context, function, argument_count)); |
| 7445 | call->set_position(expr->position()); |
| 7446 | return ast_context()->ReturnInstruction(call, expr->id()); |
| 7447 | } else { |
| 7448 | // The constructor function is both an operand to the instruction and an |
| 7449 | // argument to the construct call. |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame^] | 7450 | CHECK_ALIVE(VisitArgument(expr->expression())); |
| 7451 | HValue* constructor = HPushArgument::cast(Top())->argument(); |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 7452 | CHECK_ALIVE(VisitArgumentList(expr->arguments())); |
| 7453 | HInstruction* call = |
| 7454 | new(zone()) HCallNew(context, constructor, argument_count); |
| 7455 | Drop(argument_count); |
| 7456 | call->set_position(expr->position()); |
| 7457 | return ast_context()->ReturnInstruction(call, expr->id()); |
| 7458 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7459 | } |
| 7460 | |
| 7461 | |
| 7462 | // Support for generating inlined runtime functions. |
| 7463 | |
| 7464 | // Lookup table for generators for runtime calls that are generated inline. |
| 7465 | // Elements of the table are member pointers to functions of HGraphBuilder. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7466 | #define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \ |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7467 | &HGraphBuilder::Generate##Name, |
| 7468 | |
| 7469 | const HGraphBuilder::InlineFunctionGenerator |
| 7470 | HGraphBuilder::kInlineFunctionGenerators[] = { |
| 7471 | INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS) |
| 7472 | INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS) |
| 7473 | }; |
| 7474 | #undef INLINE_FUNCTION_GENERATOR_ADDRESS |
| 7475 | |
| 7476 | |
| 7477 | void HGraphBuilder::VisitCallRuntime(CallRuntime* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7478 | ASSERT(!HasStackOverflow()); |
| 7479 | ASSERT(current_block() != NULL); |
| 7480 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7481 | if (expr->is_jsruntime()) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7482 | return Bailout("call to a JavaScript runtime function"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7483 | } |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 7484 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7485 | const Runtime::Function* function = expr->function(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7486 | ASSERT(function != NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7487 | if (function->intrinsic_type == Runtime::INLINE) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 7488 | ASSERT(expr->name()->length() > 0); |
| 7489 | ASSERT(expr->name()->Get(0) == '_'); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7490 | // Call to an inline function. |
| 7491 | int lookup_index = static_cast<int>(function->function_id) - |
| 7492 | static_cast<int>(Runtime::kFirstInlineFunction); |
| 7493 | ASSERT(lookup_index >= 0); |
| 7494 | ASSERT(static_cast<size_t>(lookup_index) < |
| 7495 | ARRAY_SIZE(kInlineFunctionGenerators)); |
| 7496 | InlineFunctionGenerator generator = kInlineFunctionGenerators[lookup_index]; |
| 7497 | |
| 7498 | // Call the inline code generator using the pointer-to-member. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 7499 | (this->*generator)(expr); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7500 | } else { |
| 7501 | ASSERT(function->intrinsic_type == Runtime::RUNTIME); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7502 | CHECK_ALIVE(VisitArgumentList(expr->arguments())); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 7503 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 7504 | HValue* context = environment()->LookupContext(); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 7505 | Handle<String> name = expr->name(); |
| 7506 | int argument_count = expr->arguments()->length(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7507 | HCallRuntime* call = |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 7508 | new(zone()) HCallRuntime(context, name, function, argument_count); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 7509 | Drop(argument_count); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 7510 | return ast_context()->ReturnInstruction(call, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7511 | } |
| 7512 | } |
| 7513 | |
| 7514 | |
| 7515 | void HGraphBuilder::VisitUnaryOperation(UnaryOperation* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7516 | ASSERT(!HasStackOverflow()); |
| 7517 | ASSERT(current_block() != NULL); |
| 7518 | ASSERT(current_block()->HasPredecessor()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7519 | switch (expr->op()) { |
| 7520 | case Token::DELETE: return VisitDelete(expr); |
| 7521 | case Token::VOID: return VisitVoid(expr); |
| 7522 | case Token::TYPEOF: return VisitTypeof(expr); |
| 7523 | case Token::ADD: return VisitAdd(expr); |
| 7524 | case Token::SUB: return VisitSub(expr); |
| 7525 | case Token::BIT_NOT: return VisitBitNot(expr); |
| 7526 | case Token::NOT: return VisitNot(expr); |
| 7527 | default: UNREACHABLE(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7528 | } |
| 7529 | } |
| 7530 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7531 | void HGraphBuilder::VisitDelete(UnaryOperation* expr) { |
| 7532 | Property* prop = expr->expression()->AsProperty(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 7533 | VariableProxy* proxy = expr->expression()->AsVariableProxy(); |
| 7534 | if (prop != NULL) { |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 7535 | CHECK_ALIVE(VisitForValue(prop->obj())); |
| 7536 | CHECK_ALIVE(VisitForValue(prop->key())); |
| 7537 | HValue* key = Pop(); |
| 7538 | HValue* obj = Pop(); |
| 7539 | HValue* context = environment()->LookupContext(); |
| 7540 | HDeleteProperty* instr = new(zone()) HDeleteProperty(context, obj, key); |
| 7541 | return ast_context()->ReturnInstruction(instr, expr->id()); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 7542 | } else if (proxy != NULL) { |
| 7543 | Variable* var = proxy->var(); |
| 7544 | if (var->IsUnallocated()) { |
| 7545 | Bailout("delete with global variable"); |
| 7546 | } else if (var->IsStackAllocated() || var->IsContextSlot()) { |
| 7547 | // Result of deleting non-global variables is false. 'this' is not |
| 7548 | // really a variable, though we implement it as one. The |
| 7549 | // subexpression does not have side effects. |
| 7550 | HValue* value = var->is_this() |
| 7551 | ? graph()->GetConstantTrue() |
| 7552 | : graph()->GetConstantFalse(); |
| 7553 | return ast_context()->ReturnValue(value); |
| 7554 | } else { |
| 7555 | Bailout("delete with non-global variable"); |
| 7556 | } |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7557 | } else { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 7558 | // Result of deleting non-property, non-variable reference is true. |
| 7559 | // Evaluate the subexpression for side effects. |
| 7560 | CHECK_ALIVE(VisitForEffect(expr->expression())); |
| 7561 | return ast_context()->ReturnValue(graph()->GetConstantTrue()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7562 | } |
| 7563 | } |
| 7564 | |
| 7565 | |
| 7566 | void HGraphBuilder::VisitVoid(UnaryOperation* expr) { |
| 7567 | CHECK_ALIVE(VisitForEffect(expr->expression())); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 7568 | return ast_context()->ReturnValue(graph()->GetConstantUndefined()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7569 | } |
| 7570 | |
| 7571 | |
| 7572 | void HGraphBuilder::VisitTypeof(UnaryOperation* expr) { |
| 7573 | CHECK_ALIVE(VisitForTypeOf(expr->expression())); |
| 7574 | HValue* value = Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 7575 | HValue* context = environment()->LookupContext(); |
| 7576 | HInstruction* instr = new(zone()) HTypeof(context, value); |
| 7577 | return ast_context()->ReturnInstruction(instr, expr->id()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7578 | } |
| 7579 | |
| 7580 | |
| 7581 | void HGraphBuilder::VisitAdd(UnaryOperation* expr) { |
| 7582 | CHECK_ALIVE(VisitForValue(expr->expression())); |
| 7583 | HValue* value = Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 7584 | HValue* context = environment()->LookupContext(); |
| 7585 | HInstruction* instr = |
| 7586 | new(zone()) HMul(context, value, graph_->GetConstant1()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 7587 | return ast_context()->ReturnInstruction(instr, expr->id()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7588 | } |
| 7589 | |
| 7590 | |
| 7591 | void HGraphBuilder::VisitSub(UnaryOperation* expr) { |
| 7592 | CHECK_ALIVE(VisitForValue(expr->expression())); |
| 7593 | HValue* value = Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 7594 | HValue* context = environment()->LookupContext(); |
| 7595 | HInstruction* instr = |
| 7596 | new(zone()) HMul(context, value, graph_->GetConstantMinus1()); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 7597 | TypeInfo info = oracle()->UnaryType(expr); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7598 | if (info.IsUninitialized()) { |
| 7599 | AddInstruction(new(zone()) HSoftDeoptimize); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7600 | current_block()->MarkAsDeoptimizing(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7601 | info = TypeInfo::Unknown(); |
| 7602 | } |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 7603 | Representation rep = ToRepresentation(info); |
| 7604 | TraceRepresentation(expr->op(), info, instr, rep); |
| 7605 | instr->AssumeRepresentation(rep); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 7606 | return ast_context()->ReturnInstruction(instr, expr->id()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7607 | } |
| 7608 | |
| 7609 | |
| 7610 | void HGraphBuilder::VisitBitNot(UnaryOperation* expr) { |
| 7611 | CHECK_ALIVE(VisitForValue(expr->expression())); |
| 7612 | HValue* value = Pop(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7613 | TypeInfo info = oracle()->UnaryType(expr); |
| 7614 | if (info.IsUninitialized()) { |
| 7615 | AddInstruction(new(zone()) HSoftDeoptimize); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7616 | current_block()->MarkAsDeoptimizing(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7617 | } |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7618 | HInstruction* instr = new(zone()) HBitNot(value); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 7619 | return ast_context()->ReturnInstruction(instr, expr->id()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7620 | } |
| 7621 | |
| 7622 | |
| 7623 | void HGraphBuilder::VisitNot(UnaryOperation* expr) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7624 | if (ast_context()->IsTest()) { |
| 7625 | TestContext* context = TestContext::cast(ast_context()); |
| 7626 | VisitForControl(expr->expression(), |
| 7627 | context->if_false(), |
| 7628 | context->if_true()); |
| 7629 | return; |
| 7630 | } |
| 7631 | |
| 7632 | if (ast_context()->IsEffect()) { |
| 7633 | VisitForEffect(expr->expression()); |
| 7634 | return; |
| 7635 | } |
| 7636 | |
| 7637 | ASSERT(ast_context()->IsValue()); |
| 7638 | HBasicBlock* materialize_false = graph()->CreateBasicBlock(); |
| 7639 | HBasicBlock* materialize_true = graph()->CreateBasicBlock(); |
| 7640 | CHECK_BAILOUT(VisitForControl(expr->expression(), |
| 7641 | materialize_false, |
| 7642 | materialize_true)); |
| 7643 | |
| 7644 | if (materialize_false->HasPredecessor()) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7645 | materialize_false->SetJoinId(expr->MaterializeFalseId()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7646 | set_current_block(materialize_false); |
| 7647 | Push(graph()->GetConstantFalse()); |
| 7648 | } else { |
| 7649 | materialize_false = NULL; |
| 7650 | } |
| 7651 | |
| 7652 | if (materialize_true->HasPredecessor()) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7653 | materialize_true->SetJoinId(expr->MaterializeTrueId()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7654 | set_current_block(materialize_true); |
| 7655 | Push(graph()->GetConstantTrue()); |
| 7656 | } else { |
| 7657 | materialize_true = NULL; |
| 7658 | } |
| 7659 | |
| 7660 | HBasicBlock* join = |
| 7661 | CreateJoin(materialize_false, materialize_true, expr->id()); |
| 7662 | set_current_block(join); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 7663 | if (join != NULL) return ast_context()->ReturnValue(Pop()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7664 | } |
| 7665 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7666 | |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 7667 | HInstruction* HGraphBuilder::BuildIncrement(bool returns_original_input, |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 7668 | CountOperation* expr) { |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 7669 | // 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] | 7670 | TypeInfo info = oracle()->IncrementType(expr); |
| 7671 | Representation rep = ToRepresentation(info); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 7672 | if (rep.IsTagged()) { |
| 7673 | rep = Representation::Integer32(); |
| 7674 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 7675 | |
| 7676 | if (returns_original_input) { |
| 7677 | // We need an explicit HValue representing ToNumber(input). The |
| 7678 | // actual HChange instruction we need is (sometimes) added in a later |
| 7679 | // phase, so it is not available now to be used as an input to HAdd and |
| 7680 | // as the return value. |
| 7681 | HInstruction* number_input = new(zone()) HForceRepresentation(Pop(), rep); |
| 7682 | AddInstruction(number_input); |
| 7683 | Push(number_input); |
| 7684 | } |
| 7685 | |
| 7686 | // The addition has no side effects, so we do not need |
| 7687 | // to simulate the expression stack after this instruction. |
| 7688 | // Any later failures deopt to the load of the input or earlier. |
| 7689 | HConstant* delta = (expr->op() == Token::INC) |
| 7690 | ? graph_->GetConstant1() |
| 7691 | : graph_->GetConstantMinus1(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 7692 | HValue* context = environment()->LookupContext(); |
| 7693 | HInstruction* instr = new(zone()) HAdd(context, Top(), delta); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 7694 | TraceRepresentation(expr->op(), info, instr, rep); |
| 7695 | instr->AssumeRepresentation(rep); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 7696 | AddInstruction(instr); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7697 | return instr; |
| 7698 | } |
| 7699 | |
| 7700 | |
| 7701 | void HGraphBuilder::VisitCountOperation(CountOperation* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7702 | ASSERT(!HasStackOverflow()); |
| 7703 | ASSERT(current_block() != NULL); |
| 7704 | ASSERT(current_block()->HasPredecessor()); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 7705 | Expression* target = expr->expression(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7706 | VariableProxy* proxy = target->AsVariableProxy(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7707 | Property* prop = target->AsProperty(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 7708 | if (proxy == NULL && prop == NULL) { |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 7709 | return Bailout("invalid lhs in count operation"); |
| 7710 | } |
| 7711 | |
| 7712 | // Match the full code generator stack by simulating an extra stack |
| 7713 | // element for postfix operations in a non-effect context. The return |
| 7714 | // value is ToNumber(input). |
| 7715 | bool returns_original_input = |
| 7716 | expr->is_postfix() && !ast_context()->IsEffect(); |
| 7717 | HValue* input = NULL; // ToNumber(original_input). |
| 7718 | HValue* after = NULL; // The result after incrementing or decrementing. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7719 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 7720 | if (proxy != NULL) { |
| 7721 | Variable* var = proxy->var(); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 7722 | if (var->mode() == CONST) { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 7723 | return Bailout("unsupported count operation with const"); |
| 7724 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 7725 | // Argument of the count operation is a variable, not a property. |
| 7726 | ASSERT(prop == NULL); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7727 | CHECK_ALIVE(VisitForValue(target)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7728 | |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 7729 | after = BuildIncrement(returns_original_input, expr); |
| 7730 | input = returns_original_input ? Top() : Pop(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7731 | Push(after); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7732 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 7733 | switch (var->location()) { |
| 7734 | case Variable::UNALLOCATED: |
| 7735 | HandleGlobalVariableAssignment(var, |
| 7736 | after, |
| 7737 | expr->position(), |
| 7738 | expr->AssignmentId()); |
| 7739 | break; |
| 7740 | |
| 7741 | case Variable::PARAMETER: |
| 7742 | case Variable::LOCAL: |
| 7743 | Bind(var, after); |
| 7744 | break; |
| 7745 | |
| 7746 | case Variable::CONTEXT: { |
| 7747 | // Bail out if we try to mutate a parameter value in a function |
| 7748 | // using the arguments object. We do not (yet) correctly handle the |
| 7749 | // arguments property of the function. |
| 7750 | if (info()->scope()->arguments() != NULL) { |
| 7751 | // Parameters will rewrite to context slots. We have no direct |
| 7752 | // way to detect that the variable is a parameter so we use a |
| 7753 | // linear search of the parameter list. |
| 7754 | int count = info()->scope()->num_parameters(); |
| 7755 | for (int i = 0; i < count; ++i) { |
| 7756 | if (var == info()->scope()->parameter(i)) { |
| 7757 | return Bailout("assignment to parameter in arguments object"); |
| 7758 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 7759 | } |
| 7760 | } |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 7761 | |
| 7762 | HValue* context = BuildContextChainWalk(var); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 7763 | HStoreContextSlot::Mode mode = |
| 7764 | (var->mode() == LET || var->mode() == CONST_HARMONY) |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 7765 | ? HStoreContextSlot::kCheckDeoptimize : HStoreContextSlot::kNoCheck; |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 7766 | HStoreContextSlot* instr = |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 7767 | new(zone()) HStoreContextSlot(context, var->index(), mode, after); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 7768 | AddInstruction(instr); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7769 | if (instr->HasObservableSideEffects()) { |
| 7770 | AddSimulate(expr->AssignmentId()); |
| 7771 | } |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 7772 | break; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 7773 | } |
| 7774 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 7775 | case Variable::LOOKUP: |
| 7776 | return Bailout("lookup variable in count operation"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7777 | } |
| 7778 | |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 7779 | } else { |
| 7780 | // Argument of the count operation is a property. |
| 7781 | ASSERT(prop != NULL); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 7782 | prop->RecordTypeFeedback(oracle(), zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7783 | |
| 7784 | if (prop->key()->IsPropertyName()) { |
| 7785 | // Named property. |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 7786 | if (returns_original_input) Push(graph_->GetConstantUndefined()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7787 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7788 | CHECK_ALIVE(VisitForValue(prop->obj())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7789 | HValue* obj = Top(); |
| 7790 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 7791 | Handle<Map> map; |
| 7792 | HInstruction* load; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7793 | if (prop->IsMonomorphic()) { |
| 7794 | Handle<String> name = prop->key()->AsLiteral()->AsPropertyName(); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 7795 | map = prop->GetReceiverTypes()->first(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7796 | load = BuildLoadNamed(obj, prop, map, name); |
| 7797 | } else { |
| 7798 | load = BuildLoadNamedGeneric(obj, prop); |
| 7799 | } |
| 7800 | PushAndAdd(load); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7801 | if (load->HasObservableSideEffects()) AddSimulate(expr->CountId()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7802 | |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 7803 | after = BuildIncrement(returns_original_input, expr); |
| 7804 | input = Pop(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7805 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 7806 | HInstruction* store; |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 7807 | CHECK_ALIVE(store = BuildStoreNamed(obj, after, map, prop->key())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7808 | AddInstruction(store); |
| 7809 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7810 | // Overwrite the receiver in the bailout environment with the result |
| 7811 | // of the operation, and the placeholder with the original value if |
| 7812 | // necessary. |
| 7813 | environment()->SetExpressionStackAt(0, after); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 7814 | if (returns_original_input) environment()->SetExpressionStackAt(1, input); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7815 | if (store->HasObservableSideEffects()) AddSimulate(expr->AssignmentId()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7816 | |
| 7817 | } else { |
| 7818 | // Keyed property. |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 7819 | if (returns_original_input) Push(graph_->GetConstantUndefined()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7820 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7821 | CHECK_ALIVE(VisitForValue(prop->obj())); |
| 7822 | CHECK_ALIVE(VisitForValue(prop->key())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7823 | HValue* obj = environment()->ExpressionStackAt(1); |
| 7824 | HValue* key = environment()->ExpressionStackAt(0); |
| 7825 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 7826 | bool has_side_effects = false; |
| 7827 | HValue* load = HandleKeyedElementAccess( |
| 7828 | obj, key, NULL, prop, expr->CountId(), RelocInfo::kNoPosition, |
| 7829 | false, // is_store |
| 7830 | &has_side_effects); |
| 7831 | Push(load); |
| 7832 | if (has_side_effects) AddSimulate(expr->CountId()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7833 | |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 7834 | after = BuildIncrement(returns_original_input, expr); |
| 7835 | input = Pop(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7836 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 7837 | expr->RecordTypeFeedback(oracle(), zone()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 7838 | HandleKeyedElementAccess(obj, key, after, expr, expr->AssignmentId(), |
| 7839 | RelocInfo::kNoPosition, |
| 7840 | true, // is_store |
| 7841 | &has_side_effects); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7842 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7843 | // Drop the key from the bailout environment. Overwrite the receiver |
| 7844 | // with the result of the operation, and the placeholder with the |
| 7845 | // original value if necessary. |
| 7846 | Drop(1); |
| 7847 | environment()->SetExpressionStackAt(0, after); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 7848 | if (returns_original_input) environment()->SetExpressionStackAt(1, input); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 7849 | ASSERT(has_side_effects); // Stores always have side effects. |
| 7850 | AddSimulate(expr->AssignmentId()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7851 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7852 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 7853 | |
| 7854 | Drop(returns_original_input ? 2 : 1); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 7855 | return ast_context()->ReturnValue(expr->is_postfix() ? input : after); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7856 | } |
| 7857 | |
| 7858 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 7859 | HStringCharCodeAt* HGraphBuilder::BuildStringCharCodeAt(HValue* context, |
| 7860 | HValue* string, |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 7861 | HValue* index) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7862 | AddInstruction(new(zone()) HCheckNonSmi(string)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 7863 | AddInstruction(HCheckInstanceType::NewIsString(string, zone())); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7864 | HStringLength* length = new(zone()) HStringLength(string); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 7865 | AddInstruction(length); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7866 | HInstruction* checked_index = |
| 7867 | AddInstruction(new(zone()) HBoundsCheck(index, length)); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 7868 | return new(zone()) HStringCharCodeAt(context, string, checked_index); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 7869 | } |
| 7870 | |
| 7871 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7872 | HInstruction* HGraphBuilder::BuildBinaryOperation(BinaryOperation* expr, |
| 7873 | HValue* left, |
| 7874 | HValue* right) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 7875 | HValue* context = environment()->LookupContext(); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7876 | TypeInfo info = oracle()->BinaryType(expr); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7877 | if (info.IsUninitialized()) { |
| 7878 | AddInstruction(new(zone()) HSoftDeoptimize); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7879 | current_block()->MarkAsDeoptimizing(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7880 | info = TypeInfo::Unknown(); |
| 7881 | } |
| 7882 | HInstruction* instr = NULL; |
| 7883 | switch (expr->op()) { |
| 7884 | case Token::ADD: |
| 7885 | if (info.IsString()) { |
| 7886 | AddInstruction(new(zone()) HCheckNonSmi(left)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 7887 | AddInstruction(HCheckInstanceType::NewIsString(left, zone())); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7888 | AddInstruction(new(zone()) HCheckNonSmi(right)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 7889 | AddInstruction(HCheckInstanceType::NewIsString(right, zone())); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 7890 | instr = new(zone()) HStringAdd(context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7891 | } else { |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 7892 | instr = HAdd::NewHAdd(zone(), context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7893 | } |
| 7894 | break; |
| 7895 | case Token::SUB: |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 7896 | instr = HSub::NewHSub(zone(), context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7897 | break; |
| 7898 | case Token::MUL: |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 7899 | instr = HMul::NewHMul(zone(), context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7900 | break; |
| 7901 | case Token::MOD: |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 7902 | instr = HMod::NewHMod(zone(), context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7903 | break; |
| 7904 | case Token::DIV: |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 7905 | instr = HDiv::NewHDiv(zone(), context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7906 | break; |
| 7907 | case Token::BIT_XOR: |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7908 | case Token::BIT_AND: |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7909 | case Token::BIT_OR: |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 7910 | instr = HBitwise::NewHBitwise(zone(), expr->op(), context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7911 | break; |
| 7912 | case Token::SAR: |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 7913 | instr = HSar::NewHSar(zone(), context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7914 | break; |
| 7915 | case Token::SHR: |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 7916 | instr = HShr::NewHShr(zone(), context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7917 | break; |
| 7918 | case Token::SHL: |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 7919 | instr = HShl::NewHShl(zone(), context, left, right); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7920 | break; |
| 7921 | default: |
| 7922 | UNREACHABLE(); |
| 7923 | } |
| 7924 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7925 | // If we hit an uninitialized binary op stub we will get type info |
| 7926 | // for a smi operation. If one of the operands is a constant string |
| 7927 | // do not generate code assuming it is a smi operation. |
| 7928 | if (info.IsSmi() && |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 7929 | ((left->IsConstant() && HConstant::cast(left)->handle()->IsString()) || |
| 7930 | (right->IsConstant() && HConstant::cast(right)->handle()->IsString()))) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7931 | return instr; |
| 7932 | } |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 7933 | Representation rep = ToRepresentation(info); |
| 7934 | // We only generate either int32 or generic tagged bitwise operations. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 7935 | if (instr->IsBitwiseBinaryOperation()) { |
| 7936 | HBitwiseBinaryOperation::cast(instr)-> |
| 7937 | InitializeObservedInputRepresentation(rep); |
| 7938 | if (rep.IsDouble()) rep = Representation::Integer32(); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 7939 | } |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 7940 | TraceRepresentation(expr->op(), info, instr, rep); |
| 7941 | instr->AssumeRepresentation(rep); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7942 | return instr; |
| 7943 | } |
| 7944 | |
| 7945 | |
| 7946 | // Check for the form (%_ClassOf(foo) === 'BarClass'). |
| 7947 | static bool IsClassOfTest(CompareOperation* expr) { |
| 7948 | if (expr->op() != Token::EQ_STRICT) return false; |
| 7949 | CallRuntime* call = expr->left()->AsCallRuntime(); |
| 7950 | if (call == NULL) return false; |
| 7951 | Literal* literal = expr->right()->AsLiteral(); |
| 7952 | if (literal == NULL) return false; |
| 7953 | if (!literal->handle()->IsString()) return false; |
| 7954 | if (!call->name()->IsEqualTo(CStrVector("_ClassOf"))) return false; |
| 7955 | ASSERT(call->arguments()->length() == 1); |
| 7956 | return true; |
| 7957 | } |
| 7958 | |
| 7959 | |
| 7960 | void HGraphBuilder::VisitBinaryOperation(BinaryOperation* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7961 | ASSERT(!HasStackOverflow()); |
| 7962 | ASSERT(current_block() != NULL); |
| 7963 | ASSERT(current_block()->HasPredecessor()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7964 | switch (expr->op()) { |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 7965 | case Token::COMMA: |
| 7966 | return VisitComma(expr); |
| 7967 | case Token::OR: |
| 7968 | case Token::AND: |
| 7969 | return VisitLogicalExpression(expr); |
| 7970 | default: |
| 7971 | return VisitArithmeticExpression(expr); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7972 | } |
| 7973 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7974 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7975 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7976 | void HGraphBuilder::VisitComma(BinaryOperation* expr) { |
| 7977 | CHECK_ALIVE(VisitForEffect(expr->left())); |
| 7978 | // Visit the right subexpression in the same AST context as the entire |
| 7979 | // expression. |
| 7980 | Visit(expr->right()); |
| 7981 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7982 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7983 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 7984 | void HGraphBuilder::VisitLogicalExpression(BinaryOperation* expr) { |
| 7985 | bool is_logical_and = expr->op() == Token::AND; |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7986 | if (ast_context()->IsTest()) { |
| 7987 | TestContext* context = TestContext::cast(ast_context()); |
| 7988 | // Translate left subexpression. |
| 7989 | HBasicBlock* eval_right = graph()->CreateBasicBlock(); |
| 7990 | if (is_logical_and) { |
| 7991 | CHECK_BAILOUT(VisitForControl(expr->left(), |
| 7992 | eval_right, |
| 7993 | context->if_false())); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 7994 | } else { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 7995 | CHECK_BAILOUT(VisitForControl(expr->left(), |
| 7996 | context->if_true(), |
| 7997 | eval_right)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7998 | } |
| 7999 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 8000 | // Translate right subexpression by visiting it in the same AST |
| 8001 | // context as the entire expression. |
| 8002 | if (eval_right->HasPredecessor()) { |
| 8003 | eval_right->SetJoinId(expr->RightId()); |
| 8004 | set_current_block(eval_right); |
| 8005 | Visit(expr->right()); |
| 8006 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8007 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 8008 | } else if (ast_context()->IsValue()) { |
| 8009 | CHECK_ALIVE(VisitForValue(expr->left())); |
| 8010 | ASSERT(current_block() != NULL); |
| 8011 | |
| 8012 | // We need an extra block to maintain edge-split form. |
| 8013 | HBasicBlock* empty_block = graph()->CreateBasicBlock(); |
| 8014 | HBasicBlock* eval_right = graph()->CreateBasicBlock(); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 8015 | unsigned test_id = expr->left()->test_id(); |
| 8016 | ToBooleanStub::Types expected(oracle()->ToBooleanTypes(test_id)); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8017 | HBranch* test = is_logical_and |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 8018 | ? new(zone()) HBranch(Top(), eval_right, empty_block, expected) |
| 8019 | : new(zone()) HBranch(Top(), empty_block, eval_right, expected); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 8020 | current_block()->Finish(test); |
| 8021 | |
| 8022 | set_current_block(eval_right); |
| 8023 | Drop(1); // Value of the left subexpression. |
| 8024 | CHECK_BAILOUT(VisitForValue(expr->right())); |
| 8025 | |
| 8026 | HBasicBlock* join_block = |
| 8027 | CreateJoin(empty_block, current_block(), expr->id()); |
| 8028 | set_current_block(join_block); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8029 | return ast_context()->ReturnValue(Pop()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 8030 | |
| 8031 | } else { |
| 8032 | ASSERT(ast_context()->IsEffect()); |
| 8033 | // In an effect context, we don't need the value of the left subexpression, |
| 8034 | // only its control flow and side effects. We need an extra block to |
| 8035 | // maintain edge-split form. |
| 8036 | HBasicBlock* empty_block = graph()->CreateBasicBlock(); |
| 8037 | HBasicBlock* right_block = graph()->CreateBasicBlock(); |
| 8038 | if (is_logical_and) { |
| 8039 | CHECK_BAILOUT(VisitForControl(expr->left(), right_block, empty_block)); |
| 8040 | } else { |
| 8041 | CHECK_BAILOUT(VisitForControl(expr->left(), empty_block, right_block)); |
| 8042 | } |
| 8043 | |
| 8044 | // TODO(kmillikin): Find a way to fix this. It's ugly that there are |
| 8045 | // actually two empty blocks (one here and one inserted by |
| 8046 | // TestContext::BuildBranch, and that they both have an HSimulate though the |
| 8047 | // second one is not a merge node, and that we really have no good AST ID to |
| 8048 | // put on that first HSimulate. |
| 8049 | |
| 8050 | if (empty_block->HasPredecessor()) { |
| 8051 | empty_block->SetJoinId(expr->id()); |
| 8052 | } else { |
| 8053 | empty_block = NULL; |
| 8054 | } |
| 8055 | |
| 8056 | if (right_block->HasPredecessor()) { |
| 8057 | right_block->SetJoinId(expr->RightId()); |
| 8058 | set_current_block(right_block); |
| 8059 | CHECK_BAILOUT(VisitForEffect(expr->right())); |
| 8060 | right_block = current_block(); |
| 8061 | } else { |
| 8062 | right_block = NULL; |
| 8063 | } |
| 8064 | |
| 8065 | HBasicBlock* join_block = |
| 8066 | CreateJoin(empty_block, right_block, expr->id()); |
| 8067 | set_current_block(join_block); |
| 8068 | // We did not materialize any value in the predecessor environments, |
| 8069 | // so there is no need to handle it here. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8070 | } |
| 8071 | } |
| 8072 | |
| 8073 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 8074 | void HGraphBuilder::VisitArithmeticExpression(BinaryOperation* expr) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 8075 | CHECK_ALIVE(VisitForValue(expr->left())); |
| 8076 | CHECK_ALIVE(VisitForValue(expr->right())); |
| 8077 | HValue* right = Pop(); |
| 8078 | HValue* left = Pop(); |
| 8079 | HInstruction* instr = BuildBinaryOperation(expr, left, right); |
| 8080 | instr->set_position(expr->position()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8081 | return ast_context()->ReturnInstruction(instr, expr->id()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 8082 | } |
| 8083 | |
| 8084 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 8085 | void HGraphBuilder::TraceRepresentation(Token::Value op, |
| 8086 | TypeInfo info, |
| 8087 | HValue* value, |
| 8088 | Representation rep) { |
| 8089 | if (!FLAG_trace_representation) return; |
| 8090 | // TODO(svenpanne) Under which circumstances are we actually not flexible? |
| 8091 | // At first glance, this looks a bit weird... |
| 8092 | bool flexible = value->CheckFlag(HValue::kFlexibleRepresentation); |
| 8093 | PrintF("Operation %s has type info %s, %schange representation assumption " |
| 8094 | "for %s (ID %d) from %s to %s\n", |
| 8095 | Token::Name(op), |
| 8096 | info.ToString(), |
| 8097 | flexible ? "" : " DO NOT ", |
| 8098 | value->Mnemonic(), |
| 8099 | graph_->GetMaximumValueID(), |
| 8100 | value->representation().Mnemonic(), |
| 8101 | rep.Mnemonic()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8102 | } |
| 8103 | |
| 8104 | |
| 8105 | Representation HGraphBuilder::ToRepresentation(TypeInfo info) { |
| 8106 | if (info.IsSmi()) return Representation::Integer32(); |
| 8107 | if (info.IsInteger32()) return Representation::Integer32(); |
| 8108 | if (info.IsDouble()) return Representation::Double(); |
| 8109 | if (info.IsNumber()) return Representation::Double(); |
| 8110 | return Representation::Tagged(); |
| 8111 | } |
| 8112 | |
| 8113 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 8114 | void HGraphBuilder::HandleLiteralCompareTypeof(CompareOperation* expr, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8115 | HTypeof* typeof_expr, |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 8116 | Handle<String> check) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8117 | // Note: The HTypeof itself is removed during canonicalization, if possible. |
| 8118 | HValue* value = typeof_expr->value(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 8119 | HTypeofIsAndBranch* instr = new(zone()) HTypeofIsAndBranch(value, check); |
| 8120 | instr->set_position(expr->position()); |
| 8121 | return ast_context()->ReturnControl(instr, expr->id()); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 8122 | } |
| 8123 | |
| 8124 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8125 | static bool MatchLiteralCompareNil(HValue* left, |
| 8126 | Token::Value op, |
| 8127 | HValue* right, |
| 8128 | Handle<Object> nil, |
| 8129 | HValue** expr) { |
| 8130 | if (left->IsConstant() && |
| 8131 | HConstant::cast(left)->handle().is_identical_to(nil) && |
| 8132 | Token::IsEqualityOp(op)) { |
| 8133 | *expr = right; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 8134 | return true; |
| 8135 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 8136 | return false; |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 8137 | } |
| 8138 | |
| 8139 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8140 | static bool MatchLiteralCompareTypeof(HValue* left, |
| 8141 | Token::Value op, |
| 8142 | HValue* right, |
| 8143 | HTypeof** typeof_expr, |
| 8144 | Handle<String>* check) { |
| 8145 | if (left->IsTypeof() && |
| 8146 | Token::IsEqualityOp(op) && |
| 8147 | right->IsConstant() && |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 8148 | HConstant::cast(right)->handle()->IsString()) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8149 | *typeof_expr = HTypeof::cast(left); |
| 8150 | *check = Handle<String>::cast(HConstant::cast(right)->handle()); |
| 8151 | return true; |
| 8152 | } |
| 8153 | return false; |
| 8154 | } |
| 8155 | |
| 8156 | |
| 8157 | static bool IsLiteralCompareTypeof(HValue* left, |
| 8158 | Token::Value op, |
| 8159 | HValue* right, |
| 8160 | HTypeof** typeof_expr, |
| 8161 | Handle<String>* check) { |
| 8162 | return MatchLiteralCompareTypeof(left, op, right, typeof_expr, check) || |
| 8163 | MatchLiteralCompareTypeof(right, op, left, typeof_expr, check); |
| 8164 | } |
| 8165 | |
| 8166 | |
| 8167 | static bool IsLiteralCompareNil(HValue* left, |
| 8168 | Token::Value op, |
| 8169 | HValue* right, |
| 8170 | Handle<Object> nil, |
| 8171 | HValue** expr) { |
| 8172 | return MatchLiteralCompareNil(left, op, right, nil, expr) || |
| 8173 | MatchLiteralCompareNil(right, op, left, nil, expr); |
| 8174 | } |
| 8175 | |
| 8176 | |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 8177 | static bool IsLiteralCompareBool(HValue* left, |
| 8178 | Token::Value op, |
| 8179 | HValue* right) { |
| 8180 | return op == Token::EQ_STRICT && |
| 8181 | ((left->IsConstant() && HConstant::cast(left)->handle()->IsBoolean()) || |
| 8182 | (right->IsConstant() && HConstant::cast(right)->handle()->IsBoolean())); |
| 8183 | } |
| 8184 | |
| 8185 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8186 | void HGraphBuilder::VisitCompareOperation(CompareOperation* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8187 | ASSERT(!HasStackOverflow()); |
| 8188 | ASSERT(current_block() != NULL); |
| 8189 | ASSERT(current_block()->HasPredecessor()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8190 | if (IsClassOfTest(expr)) { |
| 8191 | CallRuntime* call = expr->left()->AsCallRuntime(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8192 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8193 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8194 | HValue* value = Pop(); |
| 8195 | Literal* literal = expr->right()->AsLiteral(); |
| 8196 | Handle<String> rhs = Handle<String>::cast(literal->handle()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8197 | HClassOfTestAndBranch* instr = |
| 8198 | new(zone()) HClassOfTestAndBranch(value, rhs); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8199 | instr->set_position(expr->position()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8200 | return ast_context()->ReturnControl(instr, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8201 | } |
| 8202 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 8203 | TypeInfo type_info = oracle()->CompareType(expr); |
| 8204 | // 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] | 8205 | // 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] | 8206 | if (type_info.IsUninitialized()) { |
| 8207 | AddInstruction(new(zone()) HSoftDeoptimize); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8208 | current_block()->MarkAsDeoptimizing(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 8209 | type_info = TypeInfo::Unknown(); |
| 8210 | } |
| 8211 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8212 | CHECK_ALIVE(VisitForValue(expr->left())); |
| 8213 | CHECK_ALIVE(VisitForValue(expr->right())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8214 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 8215 | HValue* context = environment()->LookupContext(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8216 | HValue* right = Pop(); |
| 8217 | HValue* left = Pop(); |
| 8218 | Token::Value op = expr->op(); |
| 8219 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8220 | HTypeof* typeof_expr = NULL; |
| 8221 | Handle<String> check; |
| 8222 | if (IsLiteralCompareTypeof(left, op, right, &typeof_expr, &check)) { |
| 8223 | return HandleLiteralCompareTypeof(expr, typeof_expr, check); |
| 8224 | } |
| 8225 | HValue* sub_expr = NULL; |
| 8226 | Factory* f = graph()->isolate()->factory(); |
| 8227 | if (IsLiteralCompareNil(left, op, right, f->undefined_value(), &sub_expr)) { |
| 8228 | return HandleLiteralCompareNil(expr, sub_expr, kUndefinedValue); |
| 8229 | } |
| 8230 | if (IsLiteralCompareNil(left, op, right, f->null_value(), &sub_expr)) { |
| 8231 | return HandleLiteralCompareNil(expr, sub_expr, kNullValue); |
| 8232 | } |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 8233 | if (IsLiteralCompareBool(left, op, right)) { |
| 8234 | HCompareObjectEqAndBranch* result = |
| 8235 | new(zone()) HCompareObjectEqAndBranch(left, right); |
| 8236 | result->set_position(expr->position()); |
| 8237 | return ast_context()->ReturnControl(result, expr->id()); |
| 8238 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8239 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8240 | if (op == Token::INSTANCEOF) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 8241 | // Check to see if the rhs of the instanceof is a global function not |
| 8242 | // residing in new space. If it is we assume that the function will stay the |
| 8243 | // same. |
| 8244 | Handle<JSFunction> target = Handle<JSFunction>::null(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 8245 | VariableProxy* proxy = expr->right()->AsVariableProxy(); |
| 8246 | bool global_function = (proxy != NULL) && proxy->var()->IsUnallocated(); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 8247 | if (global_function && |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8248 | info()->has_global_object() && |
| 8249 | !info()->global_object()->IsAccessCheckNeeded()) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 8250 | Handle<String> name = proxy->name(); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8251 | Handle<GlobalObject> global(info()->global_object()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8252 | LookupResult lookup(isolate()); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 8253 | global->Lookup(*name, &lookup); |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 8254 | if (lookup.IsNormal() && lookup.GetValue()->IsJSFunction()) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 8255 | Handle<JSFunction> candidate(JSFunction::cast(lookup.GetValue())); |
| 8256 | // If the function is in new space we assume it's more likely to |
| 8257 | // change and thus prefer the general IC code. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8258 | if (!isolate()->heap()->InNewSpace(*candidate)) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 8259 | target = candidate; |
| 8260 | } |
| 8261 | } |
| 8262 | } |
| 8263 | |
| 8264 | // If the target is not null we have found a known global function that is |
| 8265 | // assumed to stay the same for this instanceof. |
| 8266 | if (target.is_null()) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8267 | HInstanceOf* result = new(zone()) HInstanceOf(context, left, right); |
| 8268 | result->set_position(expr->position()); |
| 8269 | return ast_context()->ReturnInstruction(result, expr->id()); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 8270 | } else { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8271 | AddInstruction(new(zone()) HCheckFunction(right, target)); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8272 | HInstanceOfKnownGlobal* result = |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 8273 | new(zone()) HInstanceOfKnownGlobal(context, left, target); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8274 | result->set_position(expr->position()); |
| 8275 | return ast_context()->ReturnInstruction(result, expr->id()); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 8276 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8277 | } else if (op == Token::IN) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 8278 | HIn* result = new(zone()) HIn(context, left, right); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8279 | result->set_position(expr->position()); |
| 8280 | return ast_context()->ReturnInstruction(result, expr->id()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8281 | } else if (type_info.IsNonPrimitive()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8282 | switch (op) { |
| 8283 | case Token::EQ: |
| 8284 | case Token::EQ_STRICT: { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 8285 | // Can we get away with map check and not instance type check? |
| 8286 | Handle<Map> map = oracle()->GetCompareMap(expr); |
| 8287 | if (!map.is_null()) { |
| 8288 | AddInstruction(new(zone()) HCheckNonSmi(left)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 8289 | AddInstruction(HCheckMaps::NewWithTransitions(left, map, zone())); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 8290 | AddInstruction(new(zone()) HCheckNonSmi(right)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 8291 | AddInstruction(HCheckMaps::NewWithTransitions(right, map, zone())); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 8292 | HCompareObjectEqAndBranch* result = |
| 8293 | new(zone()) HCompareObjectEqAndBranch(left, right); |
| 8294 | result->set_position(expr->position()); |
| 8295 | return ast_context()->ReturnControl(result, expr->id()); |
| 8296 | } else { |
| 8297 | AddInstruction(new(zone()) HCheckNonSmi(left)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 8298 | AddInstruction(HCheckInstanceType::NewIsSpecObject(left, zone())); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 8299 | AddInstruction(new(zone()) HCheckNonSmi(right)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 8300 | AddInstruction(HCheckInstanceType::NewIsSpecObject(right, zone())); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 8301 | HCompareObjectEqAndBranch* result = |
| 8302 | new(zone()) HCompareObjectEqAndBranch(left, right); |
| 8303 | result->set_position(expr->position()); |
| 8304 | return ast_context()->ReturnControl(result, expr->id()); |
| 8305 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8306 | } |
| 8307 | default: |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8308 | return Bailout("Unsupported non-primitive compare"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8309 | } |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 8310 | } else if (type_info.IsString() && oracle()->IsSymbolCompare(expr) && |
| 8311 | (op == Token::EQ || op == Token::EQ_STRICT)) { |
lrn@chromium.org | ac2828d | 2011-06-23 06:29:21 +0000 | [diff] [blame] | 8312 | AddInstruction(new(zone()) HCheckNonSmi(left)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 8313 | AddInstruction(HCheckInstanceType::NewIsSymbol(left, zone())); |
lrn@chromium.org | ac2828d | 2011-06-23 06:29:21 +0000 | [diff] [blame] | 8314 | AddInstruction(new(zone()) HCheckNonSmi(right)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 8315 | AddInstruction(HCheckInstanceType::NewIsSymbol(right, zone())); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8316 | HCompareObjectEqAndBranch* result = |
| 8317 | new(zone()) HCompareObjectEqAndBranch(left, right); |
| 8318 | result->set_position(expr->position()); |
| 8319 | return ast_context()->ReturnControl(result, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8320 | } else { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8321 | Representation r = ToRepresentation(type_info); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8322 | if (r.IsTagged()) { |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 8323 | HCompareGeneric* result = |
| 8324 | new(zone()) HCompareGeneric(context, left, right, op); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8325 | result->set_position(expr->position()); |
| 8326 | return ast_context()->ReturnInstruction(result, expr->id()); |
| 8327 | } else { |
| 8328 | HCompareIDAndBranch* result = |
| 8329 | new(zone()) HCompareIDAndBranch(left, right, op); |
| 8330 | result->set_position(expr->position()); |
| 8331 | result->SetInputRepresentation(r); |
| 8332 | return ast_context()->ReturnControl(result, expr->id()); |
| 8333 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8334 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8335 | } |
| 8336 | |
| 8337 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 8338 | void HGraphBuilder::HandleLiteralCompareNil(CompareOperation* expr, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8339 | HValue* value, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 8340 | NilValue nil) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8341 | ASSERT(!HasStackOverflow()); |
| 8342 | ASSERT(current_block() != NULL); |
| 8343 | ASSERT(current_block()->HasPredecessor()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 8344 | EqualityKind kind = |
| 8345 | expr->op() == Token::EQ_STRICT ? kStrictEquality : kNonStrictEquality; |
| 8346 | HIsNilAndBranch* instr = new(zone()) HIsNilAndBranch(value, kind, nil); |
| 8347 | instr->set_position(expr->position()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8348 | return ast_context()->ReturnControl(instr, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8349 | } |
| 8350 | |
| 8351 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 8352 | HInstruction* HGraphBuilder::BuildThisFunction() { |
| 8353 | // If we share optimized code between different closures, the |
| 8354 | // this-function is not a constant, except inside an inlined body. |
| 8355 | if (function_state()->outer() != NULL) { |
| 8356 | return new(zone()) HConstant( |
| 8357 | function_state()->compilation_info()->closure(), |
| 8358 | Representation::Tagged()); |
| 8359 | } else { |
| 8360 | return new(zone()) HThisFunction; |
| 8361 | } |
| 8362 | } |
| 8363 | |
| 8364 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8365 | void HGraphBuilder::VisitThisFunction(ThisFunction* expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8366 | ASSERT(!HasStackOverflow()); |
| 8367 | ASSERT(current_block() != NULL); |
| 8368 | ASSERT(current_block()->HasPredecessor()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 8369 | HInstruction* instr = BuildThisFunction(); |
| 8370 | return ast_context()->ReturnInstruction(instr, expr->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8371 | } |
| 8372 | |
| 8373 | |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 8374 | void HGraphBuilder::VisitDeclarations(ZoneList<Declaration*>* declarations) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 8375 | ASSERT(globals_.is_empty()); |
| 8376 | AstVisitor::VisitDeclarations(declarations); |
| 8377 | if (!globals_.is_empty()) { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 8378 | Handle<FixedArray> array = |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 8379 | isolate()->factory()->NewFixedArray(globals_.length(), TENURED); |
| 8380 | for (int i = 0; i < globals_.length(); ++i) array->set(i, *globals_.at(i)); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 8381 | int flags = DeclareGlobalsEvalFlag::encode(info()->is_eval()) | |
| 8382 | DeclareGlobalsNativeFlag::encode(info()->is_native()) | |
| 8383 | DeclareGlobalsLanguageMode::encode(info()->language_mode()); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 8384 | HInstruction* result = new(zone()) HDeclareGlobals( |
| 8385 | environment()->LookupContext(), array, flags); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 8386 | AddInstruction(result); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 8387 | globals_.Clear(); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 8388 | } |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 8389 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 8390 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 8391 | |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 8392 | void HGraphBuilder::VisitVariableDeclaration(VariableDeclaration* declaration) { |
| 8393 | VariableProxy* proxy = declaration->proxy(); |
| 8394 | VariableMode mode = declaration->mode(); |
| 8395 | Variable* variable = proxy->var(); |
| 8396 | bool hole_init = mode == CONST || mode == CONST_HARMONY || mode == LET; |
| 8397 | switch (variable->location()) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 8398 | case Variable::UNALLOCATED: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 8399 | globals_.Add(variable->name(), zone()); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 8400 | globals_.Add(variable->binding_needs_init() |
| 8401 | ? isolate()->factory()->the_hole_value() |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 8402 | : isolate()->factory()->undefined_value(), zone()); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 8403 | return; |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 8404 | case Variable::PARAMETER: |
| 8405 | case Variable::LOCAL: |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 8406 | if (hole_init) { |
| 8407 | HValue* value = graph()->GetConstantHole(); |
| 8408 | environment()->Bind(variable, value); |
| 8409 | } |
| 8410 | break; |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 8411 | case Variable::CONTEXT: |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 8412 | if (hole_init) { |
| 8413 | HValue* value = graph()->GetConstantHole(); |
| 8414 | HValue* context = environment()->LookupContext(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 8415 | HStoreContextSlot* store = new(zone()) HStoreContextSlot( |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 8416 | context, variable->index(), HStoreContextSlot::kNoCheck, value); |
| 8417 | AddInstruction(store); |
| 8418 | if (store->HasObservableSideEffects()) AddSimulate(proxy->id()); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 8419 | } |
| 8420 | break; |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 8421 | case Variable::LOOKUP: |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 8422 | return Bailout("unsupported lookup slot in declaration"); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 8423 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8424 | } |
| 8425 | |
| 8426 | |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 8427 | void HGraphBuilder::VisitFunctionDeclaration(FunctionDeclaration* declaration) { |
| 8428 | VariableProxy* proxy = declaration->proxy(); |
| 8429 | Variable* variable = proxy->var(); |
| 8430 | switch (variable->location()) { |
| 8431 | case Variable::UNALLOCATED: { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 8432 | globals_.Add(variable->name(), zone()); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 8433 | Handle<SharedFunctionInfo> function = |
| 8434 | Compiler::BuildFunctionInfo(declaration->fun(), info()->script()); |
| 8435 | // Check for stack-overflow exception. |
| 8436 | if (function.is_null()) return SetStackOverflow(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 8437 | globals_.Add(function, zone()); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 8438 | return; |
| 8439 | } |
| 8440 | case Variable::PARAMETER: |
| 8441 | case Variable::LOCAL: { |
| 8442 | CHECK_ALIVE(VisitForValue(declaration->fun())); |
| 8443 | HValue* value = Pop(); |
| 8444 | environment()->Bind(variable, value); |
| 8445 | break; |
| 8446 | } |
| 8447 | case Variable::CONTEXT: { |
| 8448 | CHECK_ALIVE(VisitForValue(declaration->fun())); |
| 8449 | HValue* value = Pop(); |
| 8450 | HValue* context = environment()->LookupContext(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 8451 | HStoreContextSlot* store = new(zone()) HStoreContextSlot( |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 8452 | context, variable->index(), HStoreContextSlot::kNoCheck, value); |
| 8453 | AddInstruction(store); |
| 8454 | if (store->HasObservableSideEffects()) AddSimulate(proxy->id()); |
| 8455 | break; |
| 8456 | } |
| 8457 | case Variable::LOOKUP: |
| 8458 | return Bailout("unsupported lookup slot in declaration"); |
| 8459 | } |
| 8460 | } |
| 8461 | |
| 8462 | |
| 8463 | void HGraphBuilder::VisitModuleDeclaration(ModuleDeclaration* declaration) { |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 8464 | UNREACHABLE(); |
| 8465 | } |
| 8466 | |
| 8467 | |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 8468 | void HGraphBuilder::VisitImportDeclaration(ImportDeclaration* declaration) { |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 8469 | UNREACHABLE(); |
| 8470 | } |
| 8471 | |
| 8472 | |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 8473 | void HGraphBuilder::VisitExportDeclaration(ExportDeclaration* declaration) { |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 8474 | UNREACHABLE(); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 8475 | } |
| 8476 | |
| 8477 | |
| 8478 | void HGraphBuilder::VisitModuleLiteral(ModuleLiteral* module) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 8479 | UNREACHABLE(); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 8480 | } |
| 8481 | |
| 8482 | |
| 8483 | void HGraphBuilder::VisitModuleVariable(ModuleVariable* module) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 8484 | UNREACHABLE(); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 8485 | } |
| 8486 | |
| 8487 | |
| 8488 | void HGraphBuilder::VisitModulePath(ModulePath* module) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 8489 | UNREACHABLE(); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 8490 | } |
| 8491 | |
| 8492 | |
| 8493 | void HGraphBuilder::VisitModuleUrl(ModuleUrl* module) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 8494 | UNREACHABLE(); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 8495 | } |
| 8496 | |
| 8497 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8498 | // Generators for inline runtime functions. |
| 8499 | // Support for types. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8500 | void HGraphBuilder::GenerateIsSmi(CallRuntime* call) { |
| 8501 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8502 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8503 | HValue* value = Pop(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8504 | HIsSmiAndBranch* result = new(zone()) HIsSmiAndBranch(value); |
| 8505 | return ast_context()->ReturnControl(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8506 | } |
| 8507 | |
| 8508 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8509 | void HGraphBuilder::GenerateIsSpecObject(CallRuntime* call) { |
| 8510 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8511 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8512 | HValue* value = Pop(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8513 | HHasInstanceTypeAndBranch* result = |
| 8514 | new(zone()) HHasInstanceTypeAndBranch(value, |
| 8515 | FIRST_SPEC_OBJECT_TYPE, |
| 8516 | LAST_SPEC_OBJECT_TYPE); |
| 8517 | return ast_context()->ReturnControl(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8518 | } |
| 8519 | |
| 8520 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8521 | void HGraphBuilder::GenerateIsFunction(CallRuntime* call) { |
| 8522 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8523 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8524 | HValue* value = Pop(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8525 | HHasInstanceTypeAndBranch* result = |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 8526 | new(zone()) HHasInstanceTypeAndBranch(value, JS_FUNCTION_TYPE); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8527 | return ast_context()->ReturnControl(result, call->id()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8528 | } |
| 8529 | |
| 8530 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8531 | void HGraphBuilder::GenerateHasCachedArrayIndex(CallRuntime* call) { |
| 8532 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8533 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8534 | HValue* value = Pop(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8535 | HHasCachedArrayIndexAndBranch* result = |
| 8536 | new(zone()) HHasCachedArrayIndexAndBranch(value); |
| 8537 | return ast_context()->ReturnControl(result, call->id()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8538 | } |
| 8539 | |
| 8540 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8541 | void HGraphBuilder::GenerateIsArray(CallRuntime* call) { |
| 8542 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8543 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8544 | HValue* value = Pop(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8545 | HHasInstanceTypeAndBranch* result = |
| 8546 | new(zone()) HHasInstanceTypeAndBranch(value, JS_ARRAY_TYPE); |
| 8547 | return ast_context()->ReturnControl(result, call->id()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8548 | } |
| 8549 | |
| 8550 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8551 | void HGraphBuilder::GenerateIsRegExp(CallRuntime* call) { |
| 8552 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8553 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8554 | HValue* value = Pop(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8555 | HHasInstanceTypeAndBranch* result = |
| 8556 | new(zone()) HHasInstanceTypeAndBranch(value, JS_REGEXP_TYPE); |
| 8557 | return ast_context()->ReturnControl(result, call->id()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8558 | } |
| 8559 | |
| 8560 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8561 | void HGraphBuilder::GenerateIsObject(CallRuntime* call) { |
| 8562 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8563 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8564 | HValue* value = Pop(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8565 | HIsObjectAndBranch* result = new(zone()) HIsObjectAndBranch(value); |
| 8566 | return ast_context()->ReturnControl(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8567 | } |
| 8568 | |
| 8569 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8570 | void HGraphBuilder::GenerateIsNonNegativeSmi(CallRuntime* call) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8571 | return Bailout("inlined runtime function: IsNonNegativeSmi"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8572 | } |
| 8573 | |
| 8574 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8575 | void HGraphBuilder::GenerateIsUndetectableObject(CallRuntime* call) { |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 8576 | ASSERT(call->arguments()->length() == 1); |
| 8577 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
| 8578 | HValue* value = Pop(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8579 | HIsUndetectableAndBranch* result = |
| 8580 | new(zone()) HIsUndetectableAndBranch(value); |
| 8581 | return ast_context()->ReturnControl(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8582 | } |
| 8583 | |
| 8584 | |
| 8585 | void HGraphBuilder::GenerateIsStringWrapperSafeForDefaultValueOf( |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8586 | CallRuntime* call) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8587 | return Bailout( |
| 8588 | "inlined runtime function: IsStringWrapperSafeForDefaultValueOf"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8589 | } |
| 8590 | |
| 8591 | |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8592 | // Support for construct call checks. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8593 | void HGraphBuilder::GenerateIsConstructCall(CallRuntime* call) { |
| 8594 | ASSERT(call->arguments()->length() == 0); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8595 | if (function_state()->outer() != NULL) { |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 8596 | // We are generating graph for inlined function. |
| 8597 | HValue* value = function_state()->is_construct() |
| 8598 | ? graph()->GetConstantTrue() |
| 8599 | : graph()->GetConstantFalse(); |
| 8600 | return ast_context()->ReturnValue(value); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8601 | } else { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8602 | return ast_context()->ReturnControl(new(zone()) HIsConstructCallAndBranch, |
| 8603 | call->id()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8604 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8605 | } |
| 8606 | |
| 8607 | |
| 8608 | // Support for arguments.length and arguments[?]. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8609 | void HGraphBuilder::GenerateArgumentsLength(CallRuntime* call) { |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 8610 | // Our implementation of arguments (based on this stack frame or an |
| 8611 | // adapter below it) does not work for inlined functions. This runtime |
| 8612 | // function is blacklisted by AstNode::IsInlineable. |
| 8613 | ASSERT(function_state()->outer() == NULL); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8614 | ASSERT(call->arguments()->length() == 0); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 8615 | HInstruction* elements = AddInstruction( |
| 8616 | new(zone()) HArgumentsElements(false)); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8617 | HArgumentsLength* result = new(zone()) HArgumentsLength(elements); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8618 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8619 | } |
| 8620 | |
| 8621 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8622 | void HGraphBuilder::GenerateArguments(CallRuntime* call) { |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 8623 | // Our implementation of arguments (based on this stack frame or an |
| 8624 | // adapter below it) does not work for inlined functions. This runtime |
| 8625 | // function is blacklisted by AstNode::IsInlineable. |
| 8626 | ASSERT(function_state()->outer() == NULL); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8627 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8628 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8629 | HValue* index = Pop(); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 8630 | HInstruction* elements = AddInstruction( |
| 8631 | new(zone()) HArgumentsElements(false)); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8632 | HInstruction* length = AddInstruction(new(zone()) HArgumentsLength(elements)); |
| 8633 | HAccessArgumentsAt* result = |
| 8634 | new(zone()) HAccessArgumentsAt(elements, length, index); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8635 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8636 | } |
| 8637 | |
| 8638 | |
| 8639 | // Support for accessing the class and value fields of an object. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8640 | void HGraphBuilder::GenerateClassOf(CallRuntime* call) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8641 | // The special form detected by IsClassOfTest is detected before we get here |
| 8642 | // and does not cause a bailout. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8643 | return Bailout("inlined runtime function: ClassOf"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8644 | } |
| 8645 | |
| 8646 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8647 | void HGraphBuilder::GenerateValueOf(CallRuntime* call) { |
| 8648 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8649 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8650 | HValue* value = Pop(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8651 | HValueOf* result = new(zone()) HValueOf(value); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8652 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8653 | } |
| 8654 | |
| 8655 | |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 8656 | void HGraphBuilder::GenerateDateField(CallRuntime* call) { |
| 8657 | ASSERT(call->arguments()->length() == 2); |
| 8658 | ASSERT_NE(NULL, call->arguments()->at(1)->AsLiteral()); |
| 8659 | Smi* index = Smi::cast(*(call->arguments()->at(1)->AsLiteral()->handle())); |
| 8660 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
| 8661 | HValue* date = Pop(); |
| 8662 | HDateField* result = new(zone()) HDateField(date, index); |
| 8663 | return ast_context()->ReturnInstruction(result, call->id()); |
| 8664 | } |
| 8665 | |
| 8666 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8667 | void HGraphBuilder::GenerateSetValueOf(CallRuntime* call) { |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 8668 | ASSERT(call->arguments()->length() == 2); |
| 8669 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
| 8670 | CHECK_ALIVE(VisitForValue(call->arguments()->at(1))); |
| 8671 | HValue* value = Pop(); |
| 8672 | HValue* object = Pop(); |
| 8673 | // Check if object is a not a smi. |
| 8674 | HIsSmiAndBranch* smicheck = new(zone()) HIsSmiAndBranch(object); |
| 8675 | HBasicBlock* if_smi = graph()->CreateBasicBlock(); |
| 8676 | HBasicBlock* if_heap_object = graph()->CreateBasicBlock(); |
| 8677 | HBasicBlock* join = graph()->CreateBasicBlock(); |
| 8678 | smicheck->SetSuccessorAt(0, if_smi); |
| 8679 | smicheck->SetSuccessorAt(1, if_heap_object); |
| 8680 | current_block()->Finish(smicheck); |
| 8681 | if_smi->Goto(join); |
| 8682 | |
| 8683 | // Check if object is a JSValue. |
| 8684 | set_current_block(if_heap_object); |
| 8685 | HHasInstanceTypeAndBranch* typecheck = |
| 8686 | new(zone()) HHasInstanceTypeAndBranch(object, JS_VALUE_TYPE); |
| 8687 | HBasicBlock* if_js_value = graph()->CreateBasicBlock(); |
| 8688 | HBasicBlock* not_js_value = graph()->CreateBasicBlock(); |
| 8689 | typecheck->SetSuccessorAt(0, if_js_value); |
| 8690 | typecheck->SetSuccessorAt(1, not_js_value); |
| 8691 | current_block()->Finish(typecheck); |
| 8692 | not_js_value->Goto(join); |
| 8693 | |
| 8694 | // Create in-object property store to kValueOffset. |
| 8695 | set_current_block(if_js_value); |
| 8696 | Handle<String> name = isolate()->factory()->undefined_symbol(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 8697 | AddInstruction(new(zone()) HStoreNamedField(object, |
| 8698 | name, |
| 8699 | value, |
| 8700 | true, // in-object store. |
| 8701 | JSValue::kValueOffset)); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 8702 | if_js_value->Goto(join); |
| 8703 | join->SetJoinId(call->id()); |
| 8704 | set_current_block(join); |
| 8705 | return ast_context()->ReturnValue(value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8706 | } |
| 8707 | |
| 8708 | |
| 8709 | // Fast support for charCodeAt(n). |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8710 | void HGraphBuilder::GenerateStringCharCodeAt(CallRuntime* call) { |
| 8711 | ASSERT(call->arguments()->length() == 2); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8712 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
| 8713 | CHECK_ALIVE(VisitForValue(call->arguments()->at(1))); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 8714 | HValue* index = Pop(); |
| 8715 | HValue* string = Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 8716 | HValue* context = environment()->LookupContext(); |
| 8717 | HStringCharCodeAt* result = BuildStringCharCodeAt(context, string, index); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8718 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8719 | } |
| 8720 | |
| 8721 | |
| 8722 | // Fast support for string.charAt(n) and string[n]. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8723 | void HGraphBuilder::GenerateStringCharFromCode(CallRuntime* call) { |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 8724 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8725 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 8726 | HValue* char_code = Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 8727 | HValue* context = environment()->LookupContext(); |
| 8728 | HStringCharFromCode* result = |
| 8729 | new(zone()) HStringCharFromCode(context, char_code); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8730 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8731 | } |
| 8732 | |
| 8733 | |
| 8734 | // Fast support for string.charAt(n) and string[n]. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8735 | void HGraphBuilder::GenerateStringCharAt(CallRuntime* call) { |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 8736 | ASSERT(call->arguments()->length() == 2); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8737 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
| 8738 | CHECK_ALIVE(VisitForValue(call->arguments()->at(1))); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 8739 | HValue* index = Pop(); |
| 8740 | HValue* string = Pop(); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 8741 | HValue* context = environment()->LookupContext(); |
| 8742 | HStringCharCodeAt* char_code = BuildStringCharCodeAt(context, string, index); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 8743 | AddInstruction(char_code); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 8744 | HStringCharFromCode* result = |
| 8745 | new(zone()) HStringCharFromCode(context, char_code); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8746 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8747 | } |
| 8748 | |
| 8749 | |
| 8750 | // Fast support for object equality testing. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8751 | void HGraphBuilder::GenerateObjectEquals(CallRuntime* call) { |
| 8752 | ASSERT(call->arguments()->length() == 2); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8753 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
| 8754 | CHECK_ALIVE(VisitForValue(call->arguments()->at(1))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8755 | HValue* right = Pop(); |
| 8756 | HValue* left = Pop(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8757 | HCompareObjectEqAndBranch* result = |
| 8758 | new(zone()) HCompareObjectEqAndBranch(left, right); |
| 8759 | return ast_context()->ReturnControl(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8760 | } |
| 8761 | |
| 8762 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8763 | void HGraphBuilder::GenerateLog(CallRuntime* call) { |
| 8764 | // %_Log is ignored in optimized code. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8765 | return ast_context()->ReturnValue(graph()->GetConstantUndefined()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8766 | } |
| 8767 | |
| 8768 | |
| 8769 | // Fast support for Math.random(). |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8770 | void HGraphBuilder::GenerateRandomHeapNumber(CallRuntime* call) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 8771 | HValue* context = environment()->LookupContext(); |
| 8772 | HGlobalObject* global_object = new(zone()) HGlobalObject(context); |
| 8773 | AddInstruction(global_object); |
| 8774 | HRandom* result = new(zone()) HRandom(global_object); |
| 8775 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8776 | } |
| 8777 | |
| 8778 | |
| 8779 | // Fast support for StringAdd. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8780 | void HGraphBuilder::GenerateStringAdd(CallRuntime* call) { |
| 8781 | ASSERT_EQ(2, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8782 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 8783 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8784 | HCallStub* result = new(zone()) HCallStub(context, CodeStub::StringAdd, 2); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8785 | Drop(2); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8786 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8787 | } |
| 8788 | |
| 8789 | |
| 8790 | // Fast support for SubString. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8791 | void HGraphBuilder::GenerateSubString(CallRuntime* call) { |
| 8792 | ASSERT_EQ(3, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8793 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 8794 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8795 | HCallStub* result = new(zone()) HCallStub(context, CodeStub::SubString, 3); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8796 | Drop(3); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8797 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8798 | } |
| 8799 | |
| 8800 | |
| 8801 | // Fast support for StringCompare. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8802 | void HGraphBuilder::GenerateStringCompare(CallRuntime* call) { |
| 8803 | ASSERT_EQ(2, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8804 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 8805 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8806 | HCallStub* result = |
| 8807 | new(zone()) HCallStub(context, CodeStub::StringCompare, 2); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8808 | Drop(2); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8809 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8810 | } |
| 8811 | |
| 8812 | |
| 8813 | // Support for direct calls from JavaScript to native RegExp code. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8814 | void HGraphBuilder::GenerateRegExpExec(CallRuntime* call) { |
| 8815 | ASSERT_EQ(4, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8816 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 8817 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8818 | HCallStub* result = new(zone()) HCallStub(context, CodeStub::RegExpExec, 4); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8819 | Drop(4); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8820 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8821 | } |
| 8822 | |
| 8823 | |
| 8824 | // Construct a RegExp exec result with two in-object properties. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8825 | void HGraphBuilder::GenerateRegExpConstructResult(CallRuntime* call) { |
| 8826 | ASSERT_EQ(3, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8827 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 8828 | HValue* context = environment()->LookupContext(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8829 | HCallStub* result = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8830 | new(zone()) HCallStub(context, CodeStub::RegExpConstructResult, 3); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8831 | Drop(3); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8832 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8833 | } |
| 8834 | |
| 8835 | |
| 8836 | // Support for fast native caches. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8837 | void HGraphBuilder::GenerateGetFromCache(CallRuntime* call) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8838 | return Bailout("inlined runtime function: GetFromCache"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8839 | } |
| 8840 | |
| 8841 | |
| 8842 | // Fast support for number to string. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8843 | void HGraphBuilder::GenerateNumberToString(CallRuntime* call) { |
| 8844 | ASSERT_EQ(1, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8845 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 8846 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8847 | HCallStub* result = |
| 8848 | new(zone()) HCallStub(context, CodeStub::NumberToString, 1); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8849 | Drop(1); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8850 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8851 | } |
| 8852 | |
| 8853 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8854 | // Fast call for custom callbacks. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8855 | void HGraphBuilder::GenerateCallFunction(CallRuntime* call) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8856 | // 1 ~ The function to call is not itself an argument to the call. |
| 8857 | int arg_count = call->arguments()->length() - 1; |
| 8858 | ASSERT(arg_count >= 1); // There's always at least a receiver. |
| 8859 | |
| 8860 | for (int i = 0; i < arg_count; ++i) { |
| 8861 | CHECK_ALIVE(VisitArgument(call->arguments()->at(i))); |
| 8862 | } |
| 8863 | CHECK_ALIVE(VisitForValue(call->arguments()->last())); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 8864 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8865 | HValue* function = Pop(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 8866 | HValue* context = environment()->LookupContext(); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 8867 | |
| 8868 | // Branch for function proxies, or other non-functions. |
| 8869 | HHasInstanceTypeAndBranch* typecheck = |
| 8870 | new(zone()) HHasInstanceTypeAndBranch(function, JS_FUNCTION_TYPE); |
| 8871 | HBasicBlock* if_jsfunction = graph()->CreateBasicBlock(); |
| 8872 | HBasicBlock* if_nonfunction = graph()->CreateBasicBlock(); |
| 8873 | HBasicBlock* join = graph()->CreateBasicBlock(); |
| 8874 | typecheck->SetSuccessorAt(0, if_jsfunction); |
| 8875 | typecheck->SetSuccessorAt(1, if_nonfunction); |
| 8876 | current_block()->Finish(typecheck); |
| 8877 | |
| 8878 | set_current_block(if_jsfunction); |
| 8879 | HInstruction* invoke_result = AddInstruction( |
| 8880 | new(zone()) HInvokeFunction(context, function, arg_count)); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8881 | Drop(arg_count); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 8882 | Push(invoke_result); |
| 8883 | if_jsfunction->Goto(join); |
| 8884 | |
| 8885 | set_current_block(if_nonfunction); |
| 8886 | HInstruction* call_result = AddInstruction( |
| 8887 | new(zone()) HCallFunction(context, function, arg_count)); |
| 8888 | Drop(arg_count); |
| 8889 | Push(call_result); |
| 8890 | if_nonfunction->Goto(join); |
| 8891 | |
| 8892 | set_current_block(join); |
| 8893 | join->SetJoinId(call->id()); |
| 8894 | return ast_context()->ReturnValue(Pop()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8895 | } |
| 8896 | |
| 8897 | |
| 8898 | // Fast call to math functions. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8899 | void HGraphBuilder::GenerateMathPow(CallRuntime* call) { |
| 8900 | ASSERT_EQ(2, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8901 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
| 8902 | CHECK_ALIVE(VisitForValue(call->arguments()->at(1))); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8903 | HValue* right = Pop(); |
| 8904 | HValue* left = Pop(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8905 | HPower* result = new(zone()) HPower(left, right); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8906 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8907 | } |
| 8908 | |
| 8909 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8910 | void HGraphBuilder::GenerateMathSin(CallRuntime* call) { |
| 8911 | ASSERT_EQ(1, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8912 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 8913 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8914 | HCallStub* result = |
| 8915 | new(zone()) HCallStub(context, CodeStub::TranscendentalCache, 1); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8916 | result->set_transcendental_type(TranscendentalCache::SIN); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8917 | Drop(1); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8918 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8919 | } |
| 8920 | |
| 8921 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8922 | void HGraphBuilder::GenerateMathCos(CallRuntime* call) { |
| 8923 | ASSERT_EQ(1, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8924 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 8925 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8926 | HCallStub* result = |
| 8927 | new(zone()) HCallStub(context, CodeStub::TranscendentalCache, 1); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8928 | result->set_transcendental_type(TranscendentalCache::COS); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8929 | Drop(1); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8930 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8931 | } |
| 8932 | |
| 8933 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 8934 | void HGraphBuilder::GenerateMathTan(CallRuntime* call) { |
| 8935 | ASSERT_EQ(1, call->arguments()->length()); |
| 8936 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
| 8937 | HValue* context = environment()->LookupContext(); |
| 8938 | HCallStub* result = |
| 8939 | new(zone()) HCallStub(context, CodeStub::TranscendentalCache, 1); |
| 8940 | result->set_transcendental_type(TranscendentalCache::TAN); |
| 8941 | Drop(1); |
| 8942 | return ast_context()->ReturnInstruction(result, call->id()); |
| 8943 | } |
| 8944 | |
| 8945 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8946 | void HGraphBuilder::GenerateMathLog(CallRuntime* call) { |
| 8947 | ASSERT_EQ(1, call->arguments()->length()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8948 | CHECK_ALIVE(VisitArgumentList(call->arguments())); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 8949 | HValue* context = environment()->LookupContext(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8950 | HCallStub* result = |
| 8951 | new(zone()) HCallStub(context, CodeStub::TranscendentalCache, 1); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8952 | result->set_transcendental_type(TranscendentalCache::LOG); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8953 | Drop(1); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8954 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8955 | } |
| 8956 | |
| 8957 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8958 | void HGraphBuilder::GenerateMathSqrt(CallRuntime* call) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8959 | return Bailout("inlined runtime function: MathSqrt"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8960 | } |
| 8961 | |
| 8962 | |
| 8963 | // Check whether two RegExps are equivalent |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8964 | void HGraphBuilder::GenerateIsRegExpEquivalent(CallRuntime* call) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8965 | return Bailout("inlined runtime function: IsRegExpEquivalent"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8966 | } |
| 8967 | |
| 8968 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8969 | void HGraphBuilder::GenerateGetCachedArrayIndex(CallRuntime* call) { |
| 8970 | ASSERT(call->arguments()->length() == 1); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8971 | CHECK_ALIVE(VisitForValue(call->arguments()->at(0))); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 8972 | HValue* value = Pop(); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8973 | HGetCachedArrayIndex* result = new(zone()) HGetCachedArrayIndex(value); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 8974 | return ast_context()->ReturnInstruction(result, call->id()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8975 | } |
| 8976 | |
| 8977 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8978 | void HGraphBuilder::GenerateFastAsciiArrayJoin(CallRuntime* call) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8979 | return Bailout("inlined runtime function: FastAsciiArrayJoin"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8980 | } |
| 8981 | |
| 8982 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8983 | #undef CHECK_BAILOUT |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 8984 | #undef CHECK_ALIVE |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8985 | |
| 8986 | |
| 8987 | HEnvironment::HEnvironment(HEnvironment* outer, |
| 8988 | Scope* scope, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 8989 | Handle<JSFunction> closure, |
| 8990 | Zone* zone) |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8991 | : closure_(closure), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 8992 | values_(0, zone), |
| 8993 | assigned_variables_(4, zone), |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 8994 | frame_type_(JS_FUNCTION), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8995 | parameter_count_(0), |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 8996 | specials_count_(1), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8997 | local_count_(0), |
| 8998 | outer_(outer), |
| 8999 | pop_count_(0), |
| 9000 | push_count_(0), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9001 | ast_id_(AstNode::kNoNumber), |
| 9002 | zone_(zone) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9003 | Initialize(scope->num_parameters() + 1, scope->num_stack_slots(), 0); |
| 9004 | } |
| 9005 | |
| 9006 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9007 | HEnvironment::HEnvironment(const HEnvironment* other, Zone* zone) |
| 9008 | : values_(0, zone), |
| 9009 | assigned_variables_(0, zone), |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 9010 | frame_type_(JS_FUNCTION), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9011 | parameter_count_(0), |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 9012 | specials_count_(1), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9013 | local_count_(0), |
| 9014 | outer_(NULL), |
| 9015 | pop_count_(0), |
| 9016 | push_count_(0), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9017 | ast_id_(other->ast_id()), |
| 9018 | zone_(zone) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9019 | Initialize(other); |
| 9020 | } |
| 9021 | |
| 9022 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 9023 | HEnvironment::HEnvironment(HEnvironment* outer, |
| 9024 | Handle<JSFunction> closure, |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 9025 | FrameType frame_type, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9026 | int arguments, |
| 9027 | Zone* zone) |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 9028 | : closure_(closure), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9029 | values_(arguments, zone), |
| 9030 | assigned_variables_(0, zone), |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 9031 | frame_type_(frame_type), |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 9032 | parameter_count_(arguments), |
| 9033 | local_count_(0), |
| 9034 | outer_(outer), |
| 9035 | pop_count_(0), |
| 9036 | push_count_(0), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9037 | ast_id_(AstNode::kNoNumber), |
| 9038 | zone_(zone) { |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 9039 | } |
| 9040 | |
| 9041 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9042 | void HEnvironment::Initialize(int parameter_count, |
| 9043 | int local_count, |
| 9044 | int stack_height) { |
| 9045 | parameter_count_ = parameter_count; |
| 9046 | local_count_ = local_count; |
| 9047 | |
| 9048 | // Avoid reallocating the temporaries' backing store on the first Push. |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 9049 | int total = parameter_count + specials_count_ + local_count + stack_height; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9050 | values_.Initialize(total + 4, zone()); |
| 9051 | for (int i = 0; i < total; ++i) values_.Add(NULL, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9052 | } |
| 9053 | |
| 9054 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 9055 | void HEnvironment::Initialize(const HEnvironment* other) { |
| 9056 | closure_ = other->closure(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9057 | values_.AddAll(other->values_, zone()); |
| 9058 | assigned_variables_.AddAll(other->assigned_variables_, zone()); |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 9059 | frame_type_ = other->frame_type_; |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 9060 | parameter_count_ = other->parameter_count_; |
| 9061 | local_count_ = other->local_count_; |
| 9062 | if (other->outer_ != NULL) outer_ = other->outer_->Copy(); // Deep copy. |
| 9063 | pop_count_ = other->pop_count_; |
| 9064 | push_count_ = other->push_count_; |
| 9065 | ast_id_ = other->ast_id_; |
| 9066 | } |
| 9067 | |
| 9068 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9069 | void HEnvironment::AddIncomingEdge(HBasicBlock* block, HEnvironment* other) { |
| 9070 | ASSERT(!block->IsLoopHeader()); |
| 9071 | ASSERT(values_.length() == other->values_.length()); |
| 9072 | |
| 9073 | int length = values_.length(); |
| 9074 | for (int i = 0; i < length; ++i) { |
| 9075 | HValue* value = values_[i]; |
| 9076 | if (value != NULL && value->IsPhi() && value->block() == block) { |
| 9077 | // There is already a phi for the i'th value. |
| 9078 | HPhi* phi = HPhi::cast(value); |
| 9079 | // Assert index is correct and that we haven't missed an incoming edge. |
| 9080 | ASSERT(phi->merged_index() == i); |
| 9081 | ASSERT(phi->OperandCount() == block->predecessors()->length()); |
| 9082 | phi->AddInput(other->values_[i]); |
| 9083 | } else if (values_[i] != other->values_[i]) { |
| 9084 | // There is a fresh value on the incoming edge, a phi is needed. |
| 9085 | ASSERT(values_[i] != NULL && other->values_[i] != NULL); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9086 | HPhi* phi = new(zone()) HPhi(i, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9087 | HValue* old_value = values_[i]; |
| 9088 | for (int j = 0; j < block->predecessors()->length(); j++) { |
| 9089 | phi->AddInput(old_value); |
| 9090 | } |
| 9091 | phi->AddInput(other->values_[i]); |
| 9092 | this->values_[i] = phi; |
| 9093 | block->AddPhi(phi); |
| 9094 | } |
| 9095 | } |
| 9096 | } |
| 9097 | |
| 9098 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 9099 | void HEnvironment::Bind(int index, HValue* value) { |
| 9100 | ASSERT(value != NULL); |
| 9101 | if (!assigned_variables_.Contains(index)) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9102 | assigned_variables_.Add(index, zone()); |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 9103 | } |
| 9104 | values_[index] = value; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9105 | } |
| 9106 | |
| 9107 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 9108 | bool HEnvironment::HasExpressionAt(int index) const { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 9109 | return index >= parameter_count_ + specials_count_ + local_count_; |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 9110 | } |
| 9111 | |
| 9112 | |
| 9113 | bool HEnvironment::ExpressionStackIsEmpty() const { |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 9114 | ASSERT(length() >= first_expression_index()); |
| 9115 | return length() == first_expression_index(); |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 9116 | } |
| 9117 | |
| 9118 | |
| 9119 | void HEnvironment::SetExpressionStackAt(int index_from_top, HValue* value) { |
| 9120 | int count = index_from_top + 1; |
| 9121 | int index = values_.length() - count; |
| 9122 | ASSERT(HasExpressionAt(index)); |
| 9123 | // The push count must include at least the element in question or else |
| 9124 | // the new value will not be included in this environment's history. |
| 9125 | if (push_count_ < count) { |
| 9126 | // This is the same effect as popping then re-pushing 'count' elements. |
| 9127 | pop_count_ += (count - push_count_); |
| 9128 | push_count_ = count; |
| 9129 | } |
| 9130 | values_[index] = value; |
| 9131 | } |
| 9132 | |
| 9133 | |
| 9134 | void HEnvironment::Drop(int count) { |
| 9135 | for (int i = 0; i < count; ++i) { |
| 9136 | Pop(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9137 | } |
| 9138 | } |
| 9139 | |
| 9140 | |
| 9141 | HEnvironment* HEnvironment::Copy() const { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9142 | return new(zone()) HEnvironment(this, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9143 | } |
| 9144 | |
| 9145 | |
| 9146 | HEnvironment* HEnvironment::CopyWithoutHistory() const { |
| 9147 | HEnvironment* result = Copy(); |
| 9148 | result->ClearHistory(); |
| 9149 | return result; |
| 9150 | } |
| 9151 | |
| 9152 | |
| 9153 | HEnvironment* HEnvironment::CopyAsLoopHeader(HBasicBlock* loop_header) const { |
| 9154 | HEnvironment* new_env = Copy(); |
| 9155 | for (int i = 0; i < values_.length(); ++i) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9156 | HPhi* phi = new(zone()) HPhi(i, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9157 | phi->AddInput(values_[i]); |
| 9158 | new_env->values_[i] = phi; |
| 9159 | loop_header->AddPhi(phi); |
| 9160 | } |
| 9161 | new_env->ClearHistory(); |
| 9162 | return new_env; |
| 9163 | } |
| 9164 | |
| 9165 | |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 9166 | HEnvironment* HEnvironment::CreateStubEnvironment(HEnvironment* outer, |
| 9167 | Handle<JSFunction> target, |
| 9168 | FrameType frame_type, |
| 9169 | int arguments) const { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9170 | HEnvironment* new_env = |
| 9171 | new(zone()) HEnvironment(outer, target, frame_type, |
| 9172 | arguments + 1, zone()); |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 9173 | for (int i = 0; i <= arguments; ++i) { // Include receiver. |
| 9174 | new_env->Push(ExpressionStackAt(arguments - i)); |
| 9175 | } |
| 9176 | new_env->ClearHistory(); |
| 9177 | return new_env; |
| 9178 | } |
| 9179 | |
| 9180 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 9181 | HEnvironment* HEnvironment::CopyForInlining( |
| 9182 | Handle<JSFunction> target, |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 9183 | int arguments, |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 9184 | FunctionLiteral* function, |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 9185 | HConstant* undefined, |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 9186 | CallKind call_kind, |
| 9187 | bool is_construct) const { |
| 9188 | ASSERT(frame_type() == JS_FUNCTION); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 9189 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9190 | // Outer environment is a copy of this one without the arguments. |
| 9191 | int arity = function->scope()->num_parameters(); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 9192 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9193 | HEnvironment* outer = Copy(); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 9194 | outer->Drop(arguments + 1); // Including receiver. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9195 | outer->ClearHistory(); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 9196 | |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 9197 | if (is_construct) { |
| 9198 | // Create artificial constructor stub environment. The receiver should |
| 9199 | // actually be the constructor function, but we pass the newly allocated |
| 9200 | // object instead, DoComputeConstructStubFrame() relies on that. |
| 9201 | outer = CreateStubEnvironment(outer, target, JS_CONSTRUCT, arguments); |
| 9202 | } |
| 9203 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 9204 | if (arity != arguments) { |
| 9205 | // Create artificial arguments adaptation environment. |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 9206 | outer = CreateStubEnvironment(outer, target, ARGUMENTS_ADAPTOR, arguments); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 9207 | } |
| 9208 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 9209 | HEnvironment* inner = |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9210 | new(zone()) HEnvironment(outer, function->scope(), target, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9211 | // Get the argument values from the original environment. |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 9212 | for (int i = 0; i <= arity; ++i) { // Include receiver. |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 9213 | HValue* push = (i <= arguments) ? |
| 9214 | ExpressionStackAt(arguments - i) : undefined; |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 9215 | inner->SetValueAt(i, push); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9216 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 9217 | // If the function we are inlining is a strict mode function or a |
| 9218 | // builtin function, pass undefined as the receiver for function |
| 9219 | // calls (instead of the global receiver). |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 9220 | if ((target->shared()->native() || !function->is_classic_mode()) && |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 9221 | call_kind == CALL_AS_FUNCTION && !is_construct) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 9222 | inner->SetValueAt(0, undefined); |
| 9223 | } |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 9224 | inner->SetValueAt(arity + 1, LookupContext()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 9225 | for (int i = arity + 2; i < inner->length(); ++i) { |
| 9226 | inner->SetValueAt(i, undefined); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9227 | } |
| 9228 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 9229 | inner->set_ast_id(AstNode::kFunctionEntryId); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9230 | return inner; |
| 9231 | } |
| 9232 | |
| 9233 | |
| 9234 | void HEnvironment::PrintTo(StringStream* stream) { |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 9235 | for (int i = 0; i < length(); i++) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9236 | if (i == 0) stream->Add("parameters\n"); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 9237 | if (i == parameter_count()) stream->Add("specials\n"); |
| 9238 | if (i == parameter_count() + specials_count()) stream->Add("locals\n"); |
| 9239 | if (i == parameter_count() + specials_count() + local_count()) { |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 9240 | stream->Add("expressions\n"); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 9241 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9242 | HValue* val = values_.at(i); |
| 9243 | stream->Add("%d: ", i); |
| 9244 | if (val != NULL) { |
| 9245 | val->PrintNameTo(stream); |
| 9246 | } else { |
| 9247 | stream->Add("NULL"); |
| 9248 | } |
| 9249 | stream->Add("\n"); |
| 9250 | } |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 9251 | PrintF("\n"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9252 | } |
| 9253 | |
| 9254 | |
| 9255 | void HEnvironment::PrintToStd() { |
| 9256 | HeapStringAllocator string_allocator; |
| 9257 | StringStream trace(&string_allocator); |
| 9258 | PrintTo(&trace); |
| 9259 | PrintF("%s", *trace.ToCString()); |
| 9260 | } |
| 9261 | |
| 9262 | |
| 9263 | void HTracer::TraceCompilation(FunctionLiteral* function) { |
| 9264 | Tag tag(this, "compilation"); |
| 9265 | Handle<String> name = function->debug_name(); |
| 9266 | PrintStringProperty("name", *name->ToCString()); |
| 9267 | PrintStringProperty("method", *name->ToCString()); |
| 9268 | PrintLongProperty("date", static_cast<int64_t>(OS::TimeCurrentMillis())); |
| 9269 | } |
| 9270 | |
| 9271 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 9272 | void HTracer::TraceLithium(const char* name, LChunk* chunk) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9273 | Trace(name, chunk->graph(), chunk); |
| 9274 | } |
| 9275 | |
| 9276 | |
| 9277 | void HTracer::TraceHydrogen(const char* name, HGraph* graph) { |
| 9278 | Trace(name, graph, NULL); |
| 9279 | } |
| 9280 | |
| 9281 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 9282 | void HTracer::Trace(const char* name, HGraph* graph, LChunk* chunk) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9283 | Tag tag(this, "cfg"); |
| 9284 | PrintStringProperty("name", name); |
| 9285 | const ZoneList<HBasicBlock*>* blocks = graph->blocks(); |
| 9286 | for (int i = 0; i < blocks->length(); i++) { |
| 9287 | HBasicBlock* current = blocks->at(i); |
| 9288 | Tag block_tag(this, "block"); |
| 9289 | PrintBlockProperty("name", current->block_id()); |
| 9290 | PrintIntProperty("from_bci", -1); |
| 9291 | PrintIntProperty("to_bci", -1); |
| 9292 | |
| 9293 | if (!current->predecessors()->is_empty()) { |
| 9294 | PrintIndent(); |
| 9295 | trace_.Add("predecessors"); |
| 9296 | for (int j = 0; j < current->predecessors()->length(); ++j) { |
| 9297 | trace_.Add(" \"B%d\"", current->predecessors()->at(j)->block_id()); |
| 9298 | } |
| 9299 | trace_.Add("\n"); |
| 9300 | } else { |
| 9301 | PrintEmptyProperty("predecessors"); |
| 9302 | } |
| 9303 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 9304 | if (current->end()->SuccessorCount() == 0) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9305 | PrintEmptyProperty("successors"); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 9306 | } else { |
| 9307 | PrintIndent(); |
| 9308 | trace_.Add("successors"); |
| 9309 | for (HSuccessorIterator it(current->end()); !it.Done(); it.Advance()) { |
| 9310 | trace_.Add(" \"B%d\"", it.Current()->block_id()); |
| 9311 | } |
| 9312 | trace_.Add("\n"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9313 | } |
| 9314 | |
| 9315 | PrintEmptyProperty("xhandlers"); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 9316 | const char* flags = current->IsLoopSuccessorDominator() |
| 9317 | ? "dom-loop-succ" |
| 9318 | : ""; |
| 9319 | PrintStringProperty("flags", flags); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9320 | |
| 9321 | if (current->dominator() != NULL) { |
| 9322 | PrintBlockProperty("dominator", current->dominator()->block_id()); |
| 9323 | } |
| 9324 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9325 | PrintIntProperty("loop_depth", current->LoopNestingDepth()); |
| 9326 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9327 | if (chunk != NULL) { |
| 9328 | int first_index = current->first_instruction_index(); |
| 9329 | int last_index = current->last_instruction_index(); |
| 9330 | PrintIntProperty( |
| 9331 | "first_lir_id", |
| 9332 | LifetimePosition::FromInstructionIndex(first_index).Value()); |
| 9333 | PrintIntProperty( |
| 9334 | "last_lir_id", |
| 9335 | LifetimePosition::FromInstructionIndex(last_index).Value()); |
| 9336 | } |
| 9337 | |
| 9338 | { |
| 9339 | Tag states_tag(this, "states"); |
| 9340 | Tag locals_tag(this, "locals"); |
| 9341 | int total = current->phis()->length(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 9342 | PrintIntProperty("size", current->phis()->length()); |
| 9343 | PrintStringProperty("method", "None"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9344 | for (int j = 0; j < total; ++j) { |
| 9345 | HPhi* phi = current->phis()->at(j); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 9346 | PrintIndent(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9347 | trace_.Add("%d ", phi->merged_index()); |
| 9348 | phi->PrintNameTo(&trace_); |
| 9349 | trace_.Add(" "); |
| 9350 | phi->PrintTo(&trace_); |
| 9351 | trace_.Add("\n"); |
| 9352 | } |
| 9353 | } |
| 9354 | |
| 9355 | { |
| 9356 | Tag HIR_tag(this, "HIR"); |
| 9357 | HInstruction* instruction = current->first(); |
| 9358 | while (instruction != NULL) { |
| 9359 | int bci = 0; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9360 | int uses = instruction->UseCount(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 9361 | PrintIndent(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9362 | trace_.Add("%d %d ", bci, uses); |
| 9363 | instruction->PrintNameTo(&trace_); |
| 9364 | trace_.Add(" "); |
| 9365 | instruction->PrintTo(&trace_); |
| 9366 | trace_.Add(" <|@\n"); |
| 9367 | instruction = instruction->next(); |
| 9368 | } |
| 9369 | } |
| 9370 | |
| 9371 | |
| 9372 | if (chunk != NULL) { |
| 9373 | Tag LIR_tag(this, "LIR"); |
| 9374 | int first_index = current->first_instruction_index(); |
| 9375 | int last_index = current->last_instruction_index(); |
| 9376 | if (first_index != -1 && last_index != -1) { |
| 9377 | const ZoneList<LInstruction*>* instructions = chunk->instructions(); |
| 9378 | for (int i = first_index; i <= last_index; ++i) { |
| 9379 | LInstruction* linstr = instructions->at(i); |
| 9380 | if (linstr != NULL) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 9381 | PrintIndent(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9382 | trace_.Add("%d ", |
| 9383 | LifetimePosition::FromInstructionIndex(i).Value()); |
| 9384 | linstr->PrintTo(&trace_); |
| 9385 | trace_.Add(" <|@\n"); |
| 9386 | } |
| 9387 | } |
| 9388 | } |
| 9389 | } |
| 9390 | } |
| 9391 | } |
| 9392 | |
| 9393 | |
| 9394 | void HTracer::TraceLiveRanges(const char* name, LAllocator* allocator) { |
| 9395 | Tag tag(this, "intervals"); |
| 9396 | PrintStringProperty("name", name); |
| 9397 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 9398 | const Vector<LiveRange*>* fixed_d = allocator->fixed_double_live_ranges(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9399 | for (int i = 0; i < fixed_d->length(); ++i) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9400 | TraceLiveRange(fixed_d->at(i), "fixed", allocator->zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9401 | } |
| 9402 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 9403 | const Vector<LiveRange*>* fixed = allocator->fixed_live_ranges(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9404 | for (int i = 0; i < fixed->length(); ++i) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9405 | TraceLiveRange(fixed->at(i), "fixed", allocator->zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9406 | } |
| 9407 | |
| 9408 | const ZoneList<LiveRange*>* live_ranges = allocator->live_ranges(); |
| 9409 | for (int i = 0; i < live_ranges->length(); ++i) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9410 | TraceLiveRange(live_ranges->at(i), "object", allocator->zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9411 | } |
| 9412 | } |
| 9413 | |
| 9414 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9415 | void HTracer::TraceLiveRange(LiveRange* range, const char* type, |
| 9416 | Zone* zone) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9417 | if (range != NULL && !range->IsEmpty()) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 9418 | PrintIndent(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9419 | trace_.Add("%d %s", range->id(), type); |
| 9420 | if (range->HasRegisterAssigned()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9421 | LOperand* op = range->CreateAssignedOperand(zone); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9422 | int assigned_reg = op->index(); |
| 9423 | if (op->IsDoubleRegister()) { |
| 9424 | trace_.Add(" \"%s\"", |
| 9425 | DoubleRegister::AllocationIndexToString(assigned_reg)); |
| 9426 | } else { |
| 9427 | ASSERT(op->IsRegister()); |
| 9428 | trace_.Add(" \"%s\"", Register::AllocationIndexToString(assigned_reg)); |
| 9429 | } |
| 9430 | } else if (range->IsSpilled()) { |
| 9431 | LOperand* op = range->TopLevel()->GetSpillOperand(); |
| 9432 | if (op->IsDoubleStackSlot()) { |
| 9433 | trace_.Add(" \"double_stack:%d\"", op->index()); |
| 9434 | } else { |
| 9435 | ASSERT(op->IsStackSlot()); |
| 9436 | trace_.Add(" \"stack:%d\"", op->index()); |
| 9437 | } |
| 9438 | } |
| 9439 | int parent_index = -1; |
| 9440 | if (range->IsChild()) { |
| 9441 | parent_index = range->parent()->id(); |
| 9442 | } else { |
| 9443 | parent_index = range->id(); |
| 9444 | } |
| 9445 | LOperand* op = range->FirstHint(); |
| 9446 | int hint_index = -1; |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 9447 | if (op != NULL && op->IsUnallocated()) { |
| 9448 | hint_index = LUnallocated::cast(op)->virtual_register(); |
| 9449 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9450 | trace_.Add(" %d %d", parent_index, hint_index); |
| 9451 | UseInterval* cur_interval = range->first_interval(); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 9452 | while (cur_interval != NULL && range->Covers(cur_interval->start())) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9453 | trace_.Add(" [%d, %d[", |
| 9454 | cur_interval->start().Value(), |
| 9455 | cur_interval->end().Value()); |
| 9456 | cur_interval = cur_interval->next(); |
| 9457 | } |
| 9458 | |
| 9459 | UsePosition* current_pos = range->first_pos(); |
| 9460 | while (current_pos != NULL) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 9461 | if (current_pos->RegisterIsBeneficial() || FLAG_trace_all_uses) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9462 | trace_.Add(" %d M", current_pos->pos().Value()); |
| 9463 | } |
| 9464 | current_pos = current_pos->next(); |
| 9465 | } |
| 9466 | |
| 9467 | trace_.Add(" \"\"\n"); |
| 9468 | } |
| 9469 | } |
| 9470 | |
| 9471 | |
| 9472 | void HTracer::FlushToFile() { |
| 9473 | AppendChars(filename_, *trace_.ToCString(), trace_.length(), false); |
| 9474 | trace_.Reset(); |
| 9475 | } |
| 9476 | |
| 9477 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 9478 | void HStatistics::Initialize(CompilationInfo* info) { |
| 9479 | source_size_ += info->shared_info()->SourceSize(); |
| 9480 | } |
| 9481 | |
| 9482 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9483 | void HStatistics::Print() { |
| 9484 | PrintF("Timing results:\n"); |
| 9485 | int64_t sum = 0; |
| 9486 | for (int i = 0; i < timing_.length(); ++i) { |
| 9487 | sum += timing_[i]; |
| 9488 | } |
| 9489 | |
| 9490 | for (int i = 0; i < names_.length(); ++i) { |
| 9491 | PrintF("%30s", names_[i]); |
| 9492 | double ms = static_cast<double>(timing_[i]) / 1000; |
| 9493 | double percent = static_cast<double>(timing_[i]) * 100 / sum; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 9494 | PrintF(" - %7.3f ms / %4.1f %% ", ms, percent); |
| 9495 | |
| 9496 | unsigned size = sizes_[i]; |
| 9497 | double size_percent = static_cast<double>(size) * 100 / total_size_; |
| 9498 | PrintF(" %8u bytes / %4.1f %%\n", size, size_percent); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9499 | } |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 9500 | double source_size_in_kb = static_cast<double>(source_size_) / 1024; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 9501 | double normalized_time = source_size_in_kb > 0 |
| 9502 | ? (static_cast<double>(sum) / 1000) / source_size_in_kb |
| 9503 | : 0; |
| 9504 | double normalized_bytes = source_size_in_kb > 0 |
| 9505 | ? total_size_ / source_size_in_kb |
| 9506 | : 0; |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 9507 | PrintF("%30s - %7.3f ms %7.3f bytes\n", "Sum", |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 9508 | normalized_time, normalized_bytes); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9509 | PrintF("---------------------------------------------------------------\n"); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 9510 | 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] | 9511 | "Total", |
| 9512 | static_cast<double>(total_) / 1000, |
| 9513 | static_cast<double>(total_) / full_code_gen_); |
| 9514 | } |
| 9515 | |
| 9516 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 9517 | void HStatistics::SaveTiming(const char* name, int64_t ticks, unsigned size) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9518 | if (name == HPhase::kFullCodeGen) { |
| 9519 | full_code_gen_ += ticks; |
| 9520 | } else if (name == HPhase::kTotal) { |
| 9521 | total_ += ticks; |
| 9522 | } else { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 9523 | total_size_ += size; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9524 | for (int i = 0; i < names_.length(); ++i) { |
| 9525 | if (names_[i] == name) { |
| 9526 | timing_[i] += ticks; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 9527 | sizes_[i] += size; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9528 | return; |
| 9529 | } |
| 9530 | } |
| 9531 | names_.Add(name); |
| 9532 | timing_.Add(ticks); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 9533 | sizes_.Add(size); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9534 | } |
| 9535 | } |
| 9536 | |
| 9537 | |
| 9538 | const char* const HPhase::kFullCodeGen = "Full code generator"; |
| 9539 | const char* const HPhase::kTotal = "Total"; |
| 9540 | |
| 9541 | |
| 9542 | void HPhase::Begin(const char* name, |
| 9543 | HGraph* graph, |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 9544 | LChunk* chunk, |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9545 | LAllocator* allocator) { |
| 9546 | name_ = name; |
| 9547 | graph_ = graph; |
| 9548 | chunk_ = chunk; |
| 9549 | allocator_ = allocator; |
| 9550 | if (allocator != NULL && chunk_ == NULL) { |
| 9551 | chunk_ = allocator->chunk(); |
| 9552 | } |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 9553 | if (FLAG_hydrogen_stats) start_ = OS::Ticks(); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 9554 | start_allocation_size_ = Zone::allocation_size_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9555 | } |
| 9556 | |
| 9557 | |
| 9558 | void HPhase::End() const { |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 9559 | if (FLAG_hydrogen_stats) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9560 | int64_t end = OS::Ticks(); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 9561 | unsigned size = Zone::allocation_size_ - start_allocation_size_; |
| 9562 | HStatistics::Instance()->SaveTiming(name_, end - start_, size); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9563 | } |
| 9564 | |
fschneider@chromium.org | 35814e5 | 2012-03-01 15:43:35 +0000 | [diff] [blame] | 9565 | // Produce trace output if flag is set so that the first letter of the |
| 9566 | // phase name matches the command line parameter FLAG_trace_phase. |
| 9567 | if (FLAG_trace_hydrogen && |
| 9568 | OS::StrChr(const_cast<char*>(FLAG_trace_phase), name_[0]) != NULL) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9569 | if (graph_ != NULL) HTracer::Instance()->TraceHydrogen(name_, graph_); |
| 9570 | if (chunk_ != NULL) HTracer::Instance()->TraceLithium(name_, chunk_); |
| 9571 | if (allocator_ != NULL) { |
| 9572 | HTracer::Instance()->TraceLiveRanges(name_, allocator_); |
| 9573 | } |
| 9574 | } |
| 9575 | |
| 9576 | #ifdef DEBUG |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9577 | if (graph_ != NULL) graph_->Verify(false); // No full verify. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9578 | if (allocator_ != NULL) allocator_->Verify(); |
| 9579 | #endif |
| 9580 | } |
| 9581 | |
| 9582 | } } // namespace v8::internal |