Skip to content

Commit d32fa64

Browse files
author
bmeurer@chromium.org
committed
Refactor ChangeLowering class to avoid template specialization.
Also refactor the unit tests and add support to easily match DAGs using CaptureEq() matcher. TEST=compiler-unittests BUG=v8:3489 LOG=n R=jarin@chromium.org Review URL: https://codereview.chromium.org/480863002 git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@23140 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
1 parent e5a2404 commit d32fa64

16 files changed

Lines changed: 416 additions & 311 deletions

src/compiler/change-lowering.cc

Lines changed: 61 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -4,100 +4,58 @@
44

55
#include "src/compiler/change-lowering.h"
66

7-
#include "src/compiler/common-node-cache.h"
8-
#include "src/compiler/graph.h"
7+
#include "src/compiler/js-graph.h"
98

109
namespace v8 {
1110
namespace internal {
1211
namespace compiler {
1312

14-
ChangeLoweringBase::ChangeLoweringBase(Graph* graph, Linkage* linkage,
15-
CommonNodeCache* cache)
16-
: graph_(graph),
17-
isolate_(graph->zone()->isolate()),
18-
linkage_(linkage),
19-
cache_(cache),
20-
common_(graph->zone()),
21-
machine_(graph->zone()) {}
13+
ChangeLowering::~ChangeLowering() {}
2214

2315

24-
ChangeLoweringBase::~ChangeLoweringBase() {}
25-
26-
27-
Node* ChangeLoweringBase::ExternalConstant(ExternalReference reference) {
28-
Node** loc = cache()->FindExternalConstant(reference);
29-
if (*loc == NULL) {
30-
*loc = graph()->NewNode(common()->ExternalConstant(reference));
31-
}
32-
return *loc;
33-
}
34-
35-
36-
Node* ChangeLoweringBase::HeapConstant(PrintableUnique<HeapObject> value) {
37-
// TODO(bmeurer): Use common node cache.
38-
return graph()->NewNode(common()->HeapConstant(value));
39-
}
40-
41-
42-
Node* ChangeLoweringBase::ImmovableHeapConstant(Handle<HeapObject> value) {
43-
return HeapConstant(
44-
PrintableUnique<HeapObject>::CreateImmovable(graph()->zone(), value));
45-
}
46-
47-
48-
Node* ChangeLoweringBase::Int32Constant(int32_t value) {
49-
Node** loc = cache()->FindInt32Constant(value);
50-
if (*loc == NULL) {
51-
*loc = graph()->NewNode(common()->Int32Constant(value));
52-
}
53-
return *loc;
54-
}
55-
56-
57-
Node* ChangeLoweringBase::NumberConstant(double value) {
58-
Node** loc = cache()->FindNumberConstant(value);
59-
if (*loc == NULL) {
60-
*loc = graph()->NewNode(common()->NumberConstant(value));
61-
}
62-
return *loc;
63-
}
64-
65-
66-
Node* ChangeLoweringBase::CEntryStubConstant() {
67-
if (!c_entry_stub_constant_.is_set()) {
68-
c_entry_stub_constant_.set(
69-
ImmovableHeapConstant(CEntryStub(isolate(), 1).GetCode()));
16+
Reduction ChangeLowering::Reduce(Node* node) {
17+
Node* control = graph()->start();
18+
Node* effect = control;
19+
switch (node->opcode()) {
20+
case IrOpcode::kChangeBitToBool:
21+
return ChangeBitToBool(node->InputAt(0), control);
22+
case IrOpcode::kChangeBoolToBit:
23+
return ChangeBoolToBit(node->InputAt(0));
24+
case IrOpcode::kChangeInt32ToTagged:
25+
return ChangeInt32ToTagged(node->InputAt(0), effect, control);
26+
case IrOpcode::kChangeTaggedToFloat64:
27+
return ChangeTaggedToFloat64(node->InputAt(0), effect, control);
28+
default:
29+
return NoChange();
7030
}
71-
return c_entry_stub_constant_.get();
31+
UNREACHABLE();
32+
return NoChange();
7233
}
7334

7435

75-
Node* ChangeLoweringBase::TrueConstant() {
76-
if (!true_constant_.is_set()) {
77-
true_constant_.set(
78-
ImmovableHeapConstant(isolate()->factory()->true_value()));
79-
}
80-
return true_constant_.get();
36+
Node* ChangeLowering::HeapNumberValueIndexConstant() {
37+
STATIC_ASSERT(HeapNumber::kValueOffset % kPointerSize == 0);
38+
const int heap_number_value_offset =
39+
((HeapNumber::kValueOffset / kPointerSize) * (machine()->is64() ? 8 : 4));
40+
return jsgraph()->Int32Constant(heap_number_value_offset - kHeapObjectTag);
8141
}
8242

8343

84-
Node* ChangeLoweringBase::FalseConstant() {
85-
if (!false_constant_.is_set()) {
86-
false_constant_.set(
87-
ImmovableHeapConstant(isolate()->factory()->false_value()));
88-
}
89-
return false_constant_.get();
44+
Node* ChangeLowering::SmiShiftBitsConstant() {
45+
const int smi_shift_size = (machine()->is64() ? SmiTagging<8>::kSmiShiftSize
46+
: SmiTagging<4>::kSmiShiftSize);
47+
return jsgraph()->Int32Constant(smi_shift_size + kSmiTagSize);
9048
}
9149

9250

93-
Reduction ChangeLoweringBase::ChangeBitToBool(Node* val, Node* control) {
51+
Reduction ChangeLowering::ChangeBitToBool(Node* val, Node* control) {
9452
Node* branch = graph()->NewNode(common()->Branch(), val, control);
9553

9654
Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
97-
Node* true_value = TrueConstant();
55+
Node* true_value = jsgraph()->TrueConstant();
9856

9957
Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
100-
Node* false_value = FalseConstant();
58+
Node* false_value = jsgraph()->FalseConstant();
10159

10260
Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false);
10361
Node* phi =
@@ -107,51 +65,20 @@ Reduction ChangeLoweringBase::ChangeBitToBool(Node* val, Node* control) {
10765
}
10866

10967

110-
template <size_t kPointerSize>
111-
ChangeLowering<kPointerSize>::ChangeLowering(Graph* graph, Linkage* linkage)
112-
: ChangeLoweringBase(graph, linkage,
113-
new (graph->zone()) CommonNodeCache(graph->zone())) {}
114-
115-
116-
template <size_t kPointerSize>
117-
Reduction ChangeLowering<kPointerSize>::Reduce(Node* node) {
118-
Node* control = graph()->start();
119-
Node* effect = control;
120-
switch (node->opcode()) {
121-
case IrOpcode::kChangeBitToBool:
122-
return ChangeBitToBool(node->InputAt(0), control);
123-
case IrOpcode::kChangeBoolToBit:
124-
return ChangeBoolToBit(node->InputAt(0));
125-
case IrOpcode::kChangeInt32ToTagged:
126-
return ChangeInt32ToTagged(node->InputAt(0), effect, control);
127-
case IrOpcode::kChangeTaggedToFloat64:
128-
return ChangeTaggedToFloat64(node->InputAt(0), effect, control);
129-
default:
130-
return NoChange();
131-
}
132-
UNREACHABLE();
133-
return NoChange();
134-
}
135-
136-
137-
template <>
138-
Reduction ChangeLowering<4>::ChangeBoolToBit(Node* val) {
68+
Reduction ChangeLowering::ChangeBoolToBit(Node* val) {
13969
return Replace(
140-
graph()->NewNode(machine()->Word32Equal(), val, TrueConstant()));
70+
graph()->NewNode(machine()->WordEqual(), val, jsgraph()->TrueConstant()));
14171
}
14272

14373

144-
template <>
145-
Reduction ChangeLowering<8>::ChangeBoolToBit(Node* val) {
146-
return Replace(
147-
graph()->NewNode(machine()->Word64Equal(), val, TrueConstant()));
148-
}
149-
74+
Reduction ChangeLowering::ChangeInt32ToTagged(Node* val, Node* effect,
75+
Node* control) {
76+
if (machine()->is64()) {
77+
return Replace(
78+
graph()->NewNode(machine()->WordShl(), val, SmiShiftBitsConstant()));
79+
}
15080

151-
template <>
152-
Reduction ChangeLowering<4>::ChangeInt32ToTagged(Node* val, Node* effect,
153-
Node* control) {
154-
Node* context = NumberConstant(0);
81+
Node* context = jsgraph()->SmiConstant(0);
15582

15683
Node* add = graph()->NewNode(machine()->Int32AddWithOverflow(), val, val);
15784
Node* ovf = graph()->NewNode(common()->Projection(1), add);
@@ -167,15 +94,14 @@ Reduction ChangeLowering<4>::ChangeInt32ToTagged(Node* val, Node* effect,
16794
DCHECK_EQ(0, fn->nargs);
16895
CallDescriptor* desc = linkage()->GetRuntimeCallDescriptor(
16996
fn->function_id, 0, Operator::kNoProperties);
170-
Node* heap_number =
171-
graph()->NewNode(common()->Call(desc), CEntryStubConstant(),
172-
ExternalConstant(ExternalReference(fn, isolate())),
173-
Int32Constant(0), context, effect, if_true);
97+
Node* heap_number = graph()->NewNode(
98+
common()->Call(desc), jsgraph()->CEntryStubConstant(),
99+
jsgraph()->ExternalConstant(ExternalReference(fn, isolate())),
100+
jsgraph()->ZeroConstant(), context, effect, if_true);
174101

175102
Node* store = graph()->NewNode(
176103
machine()->Store(kMachFloat64, kNoWriteBarrier), heap_number,
177-
Int32Constant(HeapNumber::kValueOffset - kHeapObjectTag), number, effect,
178-
heap_number);
104+
HeapNumberValueIndexConstant(), number, effect, heap_number);
179105

180106
Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
181107
Node* smi = graph()->NewNode(common()->Projection(0), add);
@@ -187,34 +113,26 @@ Reduction ChangeLowering<4>::ChangeInt32ToTagged(Node* val, Node* effect,
187113
}
188114

189115

190-
template <>
191-
Reduction ChangeLowering<8>::ChangeInt32ToTagged(Node* val, Node* effect,
192-
Node* control) {
193-
return Replace(graph()->NewNode(
194-
machine()->Word64Shl(), val,
195-
Int32Constant(SmiTagging<8>::kSmiShiftSize + kSmiTagSize)));
196-
}
197-
116+
Reduction ChangeLowering::ChangeTaggedToFloat64(Node* val, Node* effect,
117+
Node* control) {
118+
STATIC_ASSERT(kSmiTagMask == 1);
198119

199-
template <>
200-
Reduction ChangeLowering<4>::ChangeTaggedToFloat64(Node* val, Node* effect,
201-
Node* control) {
202-
Node* branch = graph()->NewNode(
203-
common()->Branch(),
204-
graph()->NewNode(machine()->Word32And(), val, Int32Constant(kSmiTagMask)),
205-
control);
120+
Node* tag = graph()->NewNode(machine()->WordAnd(), val,
121+
jsgraph()->Int32Constant(kSmiTagMask));
122+
Node* branch = graph()->NewNode(common()->Branch(), tag, control);
206123

207124
Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
208-
Node* load = graph()->NewNode(
209-
machine()->Load(kMachFloat64), val,
210-
Int32Constant(HeapNumber::kValueOffset - kHeapObjectTag), if_true);
125+
Node* load = graph()->NewNode(machine()->Load(kMachFloat64), val,
126+
HeapNumberValueIndexConstant(), if_true);
211127

212128
Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
129+
Node* integer =
130+
graph()->NewNode(machine()->WordSar(), val, SmiShiftBitsConstant());
213131
Node* number = graph()->NewNode(
214132
machine()->ChangeInt32ToFloat64(),
215-
graph()->NewNode(
216-
machine()->Word32Sar(), val,
217-
Int32Constant(SmiTagging<4>::kSmiShiftSize + kSmiTagSize)));
133+
machine()->is64()
134+
? graph()->NewNode(machine()->ConvertInt64ToInt32(), integer)
135+
: integer);
218136

219137
Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false);
220138
Node* phi = graph()->NewNode(common()->Phi(2), load, number, merge);
@@ -223,38 +141,16 @@ Reduction ChangeLowering<4>::ChangeTaggedToFloat64(Node* val, Node* effect,
223141
}
224142

225143

226-
template <>
227-
Reduction ChangeLowering<8>::ChangeTaggedToFloat64(Node* val, Node* effect,
228-
Node* control) {
229-
Node* branch = graph()->NewNode(
230-
common()->Branch(),
231-
graph()->NewNode(machine()->Word64And(), val, Int32Constant(kSmiTagMask)),
232-
control);
144+
Isolate* ChangeLowering::isolate() const { return jsgraph()->isolate(); }
233145

234-
Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
235-
Node* load = graph()->NewNode(
236-
machine()->Load(kMachFloat64), val,
237-
Int32Constant(HeapNumber::kValueOffset - kHeapObjectTag), if_true);
238146

239-
Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
240-
Node* number = graph()->NewNode(
241-
machine()->ChangeInt32ToFloat64(),
242-
graph()->NewNode(
243-
machine()->ConvertInt64ToInt32(),
244-
graph()->NewNode(
245-
machine()->Word64Sar(), val,
246-
Int32Constant(SmiTagging<8>::kSmiShiftSize + kSmiTagSize))));
147+
Graph* ChangeLowering::graph() const { return jsgraph()->graph(); }
247148

248-
Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false);
249-
Node* phi = graph()->NewNode(common()->Phi(2), load, number, merge);
250149

251-
return Replace(phi);
150+
CommonOperatorBuilder* ChangeLowering::common() const {
151+
return jsgraph()->common();
252152
}
253153

254-
255-
template class ChangeLowering<4>;
256-
template class ChangeLowering<8>;
257-
258154
} // namespace compiler
259155
} // namespace internal
260156
} // namespace v8

src/compiler/change-lowering.h

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,71 +5,47 @@
55
#ifndef V8_COMPILER_CHANGE_LOWERING_H_
66
#define V8_COMPILER_CHANGE_LOWERING_H_
77

8-
#include "include/v8.h"
9-
#include "src/compiler/common-operator.h"
108
#include "src/compiler/graph-reducer.h"
11-
#include "src/compiler/machine-operator.h"
129

1310
namespace v8 {
1411
namespace internal {
1512
namespace compiler {
1613

1714
// Forward declarations.
18-
class CommonNodeCache;
15+
class CommonOperatorBuilder;
16+
class JSGraph;
1917
class Linkage;
18+
class MachineOperatorBuilder;
2019

21-
class ChangeLoweringBase : public Reducer {
20+
class ChangeLowering V8_FINAL : public Reducer {
2221
public:
23-
ChangeLoweringBase(Graph* graph, Linkage* linkage, CommonNodeCache* cache);
24-
virtual ~ChangeLoweringBase();
22+
ChangeLowering(JSGraph* jsgraph, Linkage* linkage,
23+
MachineOperatorBuilder* machine)
24+
: jsgraph_(jsgraph), linkage_(linkage), machine_(machine) {}
25+
virtual ~ChangeLowering();
26+
27+
virtual Reduction Reduce(Node* node) V8_OVERRIDE;
2528

2629
protected:
27-
Node* ExternalConstant(ExternalReference reference);
28-
Node* HeapConstant(PrintableUnique<HeapObject> value);
29-
Node* ImmovableHeapConstant(Handle<HeapObject> value);
30-
Node* Int32Constant(int32_t value);
31-
Node* NumberConstant(double value);
32-
Node* CEntryStubConstant();
33-
Node* TrueConstant();
34-
Node* FalseConstant();
30+
Node* HeapNumberValueIndexConstant();
31+
Node* SmiShiftBitsConstant();
3532

3633
Reduction ChangeBitToBool(Node* val, Node* control);
34+
Reduction ChangeBoolToBit(Node* val);
35+
Reduction ChangeInt32ToTagged(Node* val, Node* effect, Node* control);
36+
Reduction ChangeTaggedToFloat64(Node* val, Node* effect, Node* control);
3737

38-
Graph* graph() const { return graph_; }
39-
Isolate* isolate() const { return isolate_; }
38+
Graph* graph() const;
39+
Isolate* isolate() const;
40+
JSGraph* jsgraph() const { return jsgraph_; }
4041
Linkage* linkage() const { return linkage_; }
41-
CommonNodeCache* cache() const { return cache_; }
42-
CommonOperatorBuilder* common() { return &common_; }
43-
MachineOperatorBuilder* machine() { return &machine_; }
42+
CommonOperatorBuilder* common() const;
43+
MachineOperatorBuilder* machine() const { return machine_; }
4444

4545
private:
46-
Graph* graph_;
47-
Isolate* isolate_;
46+
JSGraph* jsgraph_;
4847
Linkage* linkage_;
49-
CommonNodeCache* cache_;
50-
CommonOperatorBuilder common_;
51-
MachineOperatorBuilder machine_;
52-
53-
SetOncePointer<Node> c_entry_stub_constant_;
54-
SetOncePointer<Node> true_constant_;
55-
SetOncePointer<Node> false_constant_;
56-
};
57-
58-
59-
template <size_t kPointerSize = kApiPointerSize>
60-
class ChangeLowering V8_FINAL : public ChangeLoweringBase {
61-
public:
62-
ChangeLowering(Graph* graph, Linkage* linkage);
63-
ChangeLowering(Graph* graph, Linkage* linkage, CommonNodeCache* cache)
64-
: ChangeLoweringBase(graph, linkage, cache) {}
65-
virtual ~ChangeLowering() {}
66-
67-
virtual Reduction Reduce(Node* node) V8_OVERRIDE;
68-
69-
private:
70-
Reduction ChangeBoolToBit(Node* val);
71-
Reduction ChangeInt32ToTagged(Node* val, Node* effect, Node* control);
72-
Reduction ChangeTaggedToFloat64(Node* val, Node* effect, Node* control);
48+
MachineOperatorBuilder* machine_;
7349
};
7450

7551
} // namespace compiler

0 commit comments

Comments
 (0)