Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "src/compiler/jump-threading.h" | |
| 6 #include "src/compiler/code-generator-impl.h" | |
| 7 | |
| 8 namespace v8 { | |
| 9 namespace internal { | |
| 10 namespace compiler { | |
| 11 | |
| 12 typedef BasicBlock::RpoNumber RpoNumber; | |
| 13 | |
| 14 #define TRACE(x) \ | |
| 15 if (FLAG_trace_turbo_jt) PrintF x | |
| 16 | |
| 17 struct JumpThreadingState { | |
| 18 bool forwarded; | |
| 19 ZoneVector<RpoNumber>& result; | |
| 20 ZoneStack<RpoNumber>& stack; | |
| 21 | |
| 22 void Clear(size_t count) { result.assign(count, unvisited()); } | |
| 23 void PushIfUnvisited(RpoNumber num) { | |
| 24 if (result[num.ToInt()] == unvisited()) { | |
| 25 stack.push(num); | |
| 26 result[num.ToInt()] = onstack(); | |
| 27 } | |
| 28 } | |
| 29 void Forward(RpoNumber to) { | |
| 30 RpoNumber from = stack.top(); | |
| 31 RpoNumber to_to = result[to.ToInt()]; | |
| 32 bool pop = true; | |
| 33 if (to == from) { | |
| 34 TRACE((" xx %d\n", from.ToInt())); | |
| 35 result[from.ToInt()] = from; | |
| 36 } else if (to_to == unvisited()) { | |
| 37 TRACE((" fw %d -> %d (recurse)\n", from.ToInt(), to.ToInt())); | |
| 38 stack.push(to); | |
| 39 result[to.ToInt()] = onstack(); | |
| 40 pop = false; // recurse. | |
| 41 } else if (to_to == onstack()) { | |
| 42 TRACE((" fw %d -> %d (cycle)\n", from.ToInt(), to.ToInt())); | |
| 43 result[from.ToInt()] = to; // break the cycle. | |
| 44 forwarded = true; | |
| 45 } else { | |
| 46 TRACE((" fw %d -> %d (forward)\n", from.ToInt(), to.ToInt())); | |
| 47 result[from.ToInt()] = to_to; // forward the block. | |
| 48 forwarded = true; | |
| 49 } | |
| 50 if (pop) stack.pop(); | |
| 51 } | |
| 52 RpoNumber unvisited() { return RpoNumber::FromInt(-1); } | |
| 53 RpoNumber onstack() { return RpoNumber::FromInt(-2); } | |
| 54 }; | |
| 55 | |
| 56 | |
| 57 bool JumpThreading::ComputeForwarding(Zone* local_zone, | |
| 58 ZoneVector<RpoNumber>& result, | |
| 59 InstructionSequence* code) { | |
| 60 ZoneStack<RpoNumber> stack(local_zone); | |
| 61 JumpThreadingState state = {false, result, stack}; | |
| 62 state.Clear(code->InstructionBlockCount()); | |
| 63 | |
| 64 // Iterate over the blocks forward, pushing the blocks onto the stack. | |
| 65 for (auto const block : code->instruction_blocks()) { | |
| 66 RpoNumber current = block->rpo_number(); | |
| 67 state.PushIfUnvisited(current); | |
| 68 | |
| 69 // Process the stack, which implements DFS through empty blocks. | |
| 70 while (!state.stack.empty()) { | |
| 71 InstructionBlock* block = code->InstructionBlockAt(state.stack.top()); | |
| 72 // Process the instructions in a block up to a non-empty instruction. | |
| 73 TRACE(("jt [%d] B%d RPO%d\n", static_cast<int>(stack.size()), | |
| 74 block->id().ToInt(), block->rpo_number().ToInt())); | |
| 75 bool fallthru = true; | |
| 76 RpoNumber fw = block->rpo_number(); | |
| 77 for (int i = block->code_start(); i < block->code_end(); ++i) { | |
| 78 Instruction* instr = code->InstructionAt(i); | |
| 79 if (instr->IsGapMoves() && GapInstruction::cast(instr)->IsRedundant()) { | |
| 80 // skip redundant gap moves. | |
| 81 TRACE((" nop gap\n")); | |
| 82 continue; | |
| 83 } else if (FlagsModeField::decode(instr->opcode()) != kFlags_none) { | |
| 84 // can't skip instructions with flags continuations. | |
| 85 TRACE((" flags\n")); | |
| 86 fallthru = false; | |
| 87 } else if (instr->arch_opcode() == kArchNop) { | |
|
dcarney
2014/11/24 15:59:09
a bunch of stuff is encoded as nops - you want to
| |
| 88 // skip nops. | |
| 89 TRACE((" nop\n")); | |
| 90 continue; | |
| 91 } else if (instr->arch_opcode() == kArchJmp) { | |
| 92 // try to forward the jump instruction. | |
| 93 TRACE((" jmp\n")); | |
| 94 fw = code->InputRpo(instr, 0); | |
| 95 fallthru = false; | |
| 96 } else { | |
| 97 // can't skip other instructions. | |
| 98 TRACE((" other\n")); | |
| 99 fallthru = false; | |
| 100 } | |
| 101 break; | |
| 102 } | |
| 103 if (fallthru) { | |
| 104 int next = 1 + block->rpo_number().ToInt(); | |
| 105 if (next < code->InstructionBlockCount()) fw = RpoNumber::FromInt(next); | |
| 106 } | |
| 107 state.Forward(fw); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 #if DEBUG | |
| 112 for (RpoNumber num : result) DCHECK(num.IsValid()); | |
| 113 #endif | |
| 114 | |
| 115 for (int i = 0; i < static_cast<int>(result.size()); i++) { | |
| 116 TRACE(("RPO%d B%d ", i, | |
| 117 code->InstructionBlockAt(RpoNumber::FromInt(i))->id().ToInt())); | |
| 118 int to = result[i].ToInt(); | |
| 119 if (i != to) { | |
| 120 TRACE(("-> B%d\n", | |
| 121 code->InstructionBlockAt(RpoNumber::FromInt(to))->id().ToInt())); | |
| 122 } else { | |
| 123 TRACE(("\n")); | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 return state.forwarded; | |
| 128 } | |
| 129 | |
| 130 | |
| 131 static void ForwardInputRpo(ZoneVector<RpoNumber>& result, | |
| 132 InstructionSequence* code, Instruction* instr, | |
| 133 int index) { | |
| 134 RpoNumber to = code->InputRpo(instr, index); | |
| 135 RpoNumber new_to = result[to.ToInt()]; | |
| 136 if (!(new_to == to)) { | |
| 137 TRACE(("[B%d -> B%d]", code->InstructionBlockAt(to)->id().ToInt(), | |
| 138 code->InstructionBlockAt(new_to)->id().ToInt())); | |
| 139 int rpo = code->AddImmediate(Constant(new_to.ToInt())); | |
| 140 instr->SetInputAt(index, ImmediateOperand::Create(rpo, code->zone())); | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 void JumpThreading::ApplyForwarding(ZoneVector<RpoNumber>& result, | |
| 145 InstructionSequence* code) { | |
| 146 if (!FLAG_turbo_jt) return; | |
| 147 | |
| 148 Zone local_zone(code->zone()->isolate()); | |
| 149 ZoneVector<bool> skip(result.size(), false, &local_zone); | |
| 150 | |
| 151 bool prev_fallthru = true; | |
| 152 for (auto const block : code->instruction_blocks()) { | |
| 153 // Skip a block if the previous doesn't fall through and we should forward. | |
| 154 int block_num = block->rpo_number().ToInt(); | |
| 155 skip[block_num] = !prev_fallthru && result[block_num].ToInt() != block_num; | |
| 156 | |
| 157 bool fallthru = true; | |
| 158 for (int i = block->code_start(); i < block->code_end(); ++i) { | |
| 159 Instruction* instr = code->InstructionAt(i); | |
| 160 if (FlagsModeField::decode(instr->opcode()) == kFlags_branch) { | |
| 161 // forward the targets of a branch. | |
| 162 TRACE(("jt-fw br @%d ", i)); | |
| 163 ForwardInputRpo(result, code, instr, instr->InputCount() - 2); | |
| 164 ForwardInputRpo(result, code, instr, instr->InputCount() - 1); | |
| 165 TRACE(("\n")); | |
| 166 fallthru = false; | |
| 167 } else if (instr->arch_opcode() == kArchJmp) { | |
| 168 if (skip[block_num]) { | |
| 169 // Overwrite a redundant jump with a nop if the previous block | |
| 170 // did not fall through to this one. | |
| 171 TRACE(("jt-fw nop @%d\n", i)); | |
| 172 instr->OverwriteWithNop(); | |
| 173 } else { | |
| 174 // forward the target of a jump. | |
| 175 TRACE(("jt-fw jmp @%d ", i)); | |
| 176 ForwardInputRpo(result, code, instr, 0); | |
| 177 TRACE(("\n")); | |
| 178 } | |
| 179 fallthru = false; | |
| 180 } | |
| 181 } | |
| 182 prev_fallthru = fallthru; | |
| 183 } | |
| 184 // Recompute assembly order numbers. | |
| 185 int ao = 0; | |
| 186 for (auto const block : code->instruction_blocks()) { | |
| 187 if (!block->IsDeferred()) { | |
| 188 block->set_ao_number(RpoNumber::FromInt(ao)); | |
| 189 if (!skip[block->rpo_number().ToInt()]) ao++; | |
| 190 } | |
| 191 } | |
| 192 for (auto const block : code->instruction_blocks()) { | |
| 193 if (block->IsDeferred()) { | |
| 194 block->set_ao_number(RpoNumber::FromInt(ao)); | |
| 195 if (!skip[block->rpo_number().ToInt()]) ao++; | |
| 196 } | |
| 197 } | |
| 198 } | |
| 199 | |
| 200 } // namespace compiler | |
| 201 } // namespace internal | |
| 202 } // namespace v8 | |
| OLD | NEW |