1 // Copyright 2019 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/decompression-optimizer.h"
6 
7 #include "src/compiler/graph.h"
8 #include "src/compiler/node-properties.h"
9 
10 namespace v8 {
11 namespace internal {
12 namespace compiler {
13 
14 namespace {
15 
IsMachineLoad(Node * const node)16 bool IsMachineLoad(Node* const node) {
17   const IrOpcode::Value opcode = node->opcode();
18   return opcode == IrOpcode::kLoad || opcode == IrOpcode::kPoisonedLoad ||
19          opcode == IrOpcode::kProtectedLoad ||
20          opcode == IrOpcode::kUnalignedLoad;
21 }
22 
IsTaggedMachineLoad(Node * const node)23 bool IsTaggedMachineLoad(Node* const node) {
24   return IsMachineLoad(node) &&
25          CanBeTaggedPointer(LoadRepresentationOf(node->op()).representation());
26 }
27 
IsHeapConstant(Node * const node)28 bool IsHeapConstant(Node* const node) {
29   return node->opcode() == IrOpcode::kHeapConstant;
30 }
31 
IsTaggedPhi(Node * const node)32 bool IsTaggedPhi(Node* const node) {
33   if (node->opcode() == IrOpcode::kPhi) {
34     return CanBeTaggedPointer(PhiRepresentationOf(node->op()));
35   }
36   return false;
37 }
38 
CanBeCompressed(Node * const node)39 bool CanBeCompressed(Node* const node) {
40   return IsHeapConstant(node) || IsTaggedMachineLoad(node) || IsTaggedPhi(node);
41 }
42 
43 }  // anonymous namespace
44 
DecompressionOptimizer(Zone * zone,Graph * graph,CommonOperatorBuilder * common,MachineOperatorBuilder * machine)45 DecompressionOptimizer::DecompressionOptimizer(Zone* zone, Graph* graph,
46                                                CommonOperatorBuilder* common,
47                                                MachineOperatorBuilder* machine)
48     : graph_(graph),
49       common_(common),
50       machine_(machine),
51       states_(graph, static_cast<uint32_t>(State::kNumberOfStates)),
52       to_visit_(zone),
53       compressed_candidate_nodes_(zone) {}
54 
MarkNodes()55 void DecompressionOptimizer::MarkNodes() {
56   MaybeMarkAndQueueForRevisit(graph()->end(), State::kOnly32BitsObserved);
57   while (!to_visit_.empty()) {
58     Node* const node = to_visit_.front();
59     to_visit_.pop_front();
60     MarkNodeInputs(node);
61   }
62 }
63 
MarkNodeInputs(Node * node)64 void DecompressionOptimizer::MarkNodeInputs(Node* node) {
65   // Mark the value inputs.
66   switch (node->opcode()) {
67     // UNOPS.
68     case IrOpcode::kBitcastTaggedToWord:
69     case IrOpcode::kBitcastTaggedToWordForTagAndSmiBits:
70       // Replicate the bitcast's state for its input.
71       DCHECK_EQ(node->op()->ValueInputCount(), 1);
72       MaybeMarkAndQueueForRevisit(node->InputAt(0),
73                                   states_.Get(node));  // value
74       break;
75     case IrOpcode::kTruncateInt64ToInt32:
76       DCHECK_EQ(node->op()->ValueInputCount(), 1);
77       MaybeMarkAndQueueForRevisit(node->InputAt(0),
78                                   State::kOnly32BitsObserved);  // value
79       break;
80     // BINOPS.
81     case IrOpcode::kInt32LessThan:
82     case IrOpcode::kInt32LessThanOrEqual:
83     case IrOpcode::kUint32LessThan:
84     case IrOpcode::kUint32LessThanOrEqual:
85     case IrOpcode::kWord32And:
86     case IrOpcode::kWord32Equal:
87     case IrOpcode::kWord32Shl:
88       DCHECK_EQ(node->op()->ValueInputCount(), 2);
89       MaybeMarkAndQueueForRevisit(node->InputAt(0),
90                                   State::kOnly32BitsObserved);  // value_0
91       MaybeMarkAndQueueForRevisit(node->InputAt(1),
92                                   State::kOnly32BitsObserved);  // value_1
93       break;
94     // SPECIAL CASES.
95     // SPECIAL CASES - Store.
96     case IrOpcode::kStore:
97     case IrOpcode::kProtectedStore:
98     case IrOpcode::kUnalignedStore:
99       DCHECK_EQ(node->op()->ValueInputCount(), 3);
100       MaybeMarkAndQueueForRevisit(node->InputAt(0),
101                                   State::kEverythingObserved);  // base pointer
102       MaybeMarkAndQueueForRevisit(node->InputAt(1),
103                                   State::kEverythingObserved);  // index
104       // TODO(v8:7703): When the implementation is done, check if this ternary
105       // operator is too restrictive, since we only mark Tagged stores as 32
106       // bits.
107       MaybeMarkAndQueueForRevisit(
108           node->InputAt(2),
109           IsAnyTagged(StoreRepresentationOf(node->op()).representation())
110               ? State::kOnly32BitsObserved
111               : State::kEverythingObserved);  // value
112       break;
113     // SPECIAL CASES - Variable inputs.
114     // The deopt code knows how to handle Compressed inputs, both
115     // MachineRepresentation kCompressed values and CompressedHeapConstants.
116     case IrOpcode::kFrameState:  // Fall through.
117     // TODO(v8:7703): kStateValues doesn't appear in any test linked to Loads or
118     // HeapConstants. Do we care about this case?
119     case IrOpcode::kStateValues:  // Fall through.
120     case IrOpcode::kTypedStateValues:
121       for (int i = 0; i < node->op()->ValueInputCount(); ++i) {
122         MaybeMarkAndQueueForRevisit(node->InputAt(i),
123                                     State::kOnly32BitsObserved);
124       }
125       break;
126     case IrOpcode::kPhi: {
127       // Replicate the phi's state for its inputs.
128       State curr_state = states_.Get(node);
129       for (int i = 0; i < node->op()->ValueInputCount(); ++i) {
130         MaybeMarkAndQueueForRevisit(node->InputAt(i), curr_state);
131       }
132       break;
133     }
134     default:
135       // To be conservative, we assume that all value inputs need to be 64 bits
136       // unless noted otherwise.
137       for (int i = 0; i < node->op()->ValueInputCount(); ++i) {
138         MaybeMarkAndQueueForRevisit(node->InputAt(i),
139                                     State::kEverythingObserved);
140       }
141       break;
142   }
143 
144   // We always mark the non-value input nodes as kOnly32BitsObserved so that
145   // they will be visited. If they need to be kEverythingObserved, they will be
146   // marked as such in a future pass.
147   for (int i = node->op()->ValueInputCount(); i < node->InputCount(); ++i) {
148     MaybeMarkAndQueueForRevisit(node->InputAt(i), State::kOnly32BitsObserved);
149   }
150 }
151 
MaybeMarkAndQueueForRevisit(Node * const node,State state)152 void DecompressionOptimizer::MaybeMarkAndQueueForRevisit(Node* const node,
153                                                          State state) {
154   DCHECK_NE(state, State::kUnvisited);
155   State previous_state = states_.Get(node);
156   // Only update the state if we have relevant new information.
157   if (previous_state == State::kUnvisited ||
158       (previous_state == State::kOnly32BitsObserved &&
159        state == State::kEverythingObserved)) {
160     states_.Set(node, state);
161     to_visit_.push_back(node);
162 
163     if (state == State::kOnly32BitsObserved && CanBeCompressed(node)) {
164       compressed_candidate_nodes_.push_back(node);
165     }
166   }
167 }
168 
ChangeHeapConstant(Node * const node)169 void DecompressionOptimizer::ChangeHeapConstant(Node* const node) {
170   DCHECK(IsHeapConstant(node));
171   NodeProperties::ChangeOp(
172       node, common()->CompressedHeapConstant(HeapConstantOf(node->op())));
173 }
174 
ChangePhi(Node * const node)175 void DecompressionOptimizer::ChangePhi(Node* const node) {
176   DCHECK(IsTaggedPhi(node));
177 
178   MachineRepresentation mach_rep = PhiRepresentationOf(node->op());
179   if (mach_rep == MachineRepresentation::kTagged) {
180     mach_rep = MachineRepresentation::kCompressed;
181   } else {
182     DCHECK_EQ(mach_rep, MachineRepresentation::kTaggedPointer);
183     mach_rep = MachineRepresentation::kCompressedPointer;
184   }
185 
186   NodeProperties::ChangeOp(
187       node, common()->Phi(mach_rep, node->op()->ValueInputCount()));
188 }
189 
ChangeLoad(Node * const node)190 void DecompressionOptimizer::ChangeLoad(Node* const node) {
191   DCHECK(IsMachineLoad(node));
192   // Change to a Compressed MachRep to avoid the full decompression.
193   LoadRepresentation load_rep = LoadRepresentationOf(node->op());
194   LoadRepresentation compressed_load_rep;
195   if (load_rep == MachineType::AnyTagged()) {
196     compressed_load_rep = MachineType::AnyCompressed();
197   } else {
198     DCHECK_EQ(load_rep, MachineType::TaggedPointer());
199     compressed_load_rep = MachineType::CompressedPointer();
200   }
201 
202   // Change to the Operator with the Compressed MachineRepresentation.
203   switch (node->opcode()) {
204     case IrOpcode::kLoad:
205       NodeProperties::ChangeOp(node, machine()->Load(compressed_load_rep));
206       break;
207     case IrOpcode::kPoisonedLoad:
208       NodeProperties::ChangeOp(node,
209                                machine()->PoisonedLoad(compressed_load_rep));
210       break;
211     case IrOpcode::kProtectedLoad:
212       NodeProperties::ChangeOp(node,
213                                machine()->ProtectedLoad(compressed_load_rep));
214       break;
215     case IrOpcode::kUnalignedLoad:
216       NodeProperties::ChangeOp(node,
217                                machine()->UnalignedLoad(compressed_load_rep));
218       break;
219     default:
220       UNREACHABLE();
221   }
222 }
223 
ChangeNodes()224 void DecompressionOptimizer::ChangeNodes() {
225   for (Node* const node : compressed_candidate_nodes_) {
226     // compressed_candidate_nodes_ contains all the nodes that once had the
227     // State::kOnly32BitsObserved. If we later updated the state to be
228     // State::IsEverythingObserved, then we have to ignore them. This is less
229     // costly than removing them from the compressed_candidate_nodes_ NodeVector
230     // when we update them to State::IsEverythingObserved.
231     if (IsEverythingObserved(node)) continue;
232 
233     switch (node->opcode()) {
234       case IrOpcode::kHeapConstant:
235         ChangeHeapConstant(node);
236         break;
237       case IrOpcode::kPhi:
238         ChangePhi(node);
239         break;
240       default:
241         ChangeLoad(node);
242         break;
243     }
244   }
245 }
246 
Reduce()247 void DecompressionOptimizer::Reduce() {
248   MarkNodes();
249   ChangeNodes();
250 }
251 
252 }  // namespace compiler
253 }  // namespace internal
254 }  // namespace v8
255