1169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org// Copyright 2013 the V8 project authors. All rights reserved.
2169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org// Redistribution and use in source and binary forms, with or without
3169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org// modification, are permitted provided that the following conditions are
4169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org// met:
5169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org//
6169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org//     * Redistributions of source code must retain the above copyright
7169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org//       notice, this list of conditions and the following disclaimer.
8169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org//     * Redistributions in binary form must reproduce the above
9169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org//       copyright notice, this list of conditions and the following
10169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org//       disclaimer in the documentation and/or other materials provided
11169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org//       with the distribution.
12169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org//     * Neither the name of Google Inc. nor the names of its
13169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org//       contributors may be used to endorse or promote products derived
14169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org//       from this software without specific prior written permission.
15169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org//
16169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org
28169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org#include "hydrogen-canonicalize.h"
29169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org
30169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.orgnamespace v8 {
31169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.orgnamespace internal {
32169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org
33169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.orgvoid HCanonicalizePhase::Run() {
34169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org  const ZoneList<HBasicBlock*>* blocks(graph()->blocks());
35169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org  // Before removing no-op instructions, save their semantic value.
36169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org  // We must be careful not to set the flag unnecessarily, because GVN
37169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org  // cannot identify two instructions when their flag value differs.
38169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org  for (int i = 0; i < blocks->length(); ++i) {
39169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org    for (HInstructionIterator it(blocks->at(i)); !it.Done(); it.Advance()) {
40169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org      HInstruction* instr = it.Current();
41fb732b17922ea75830be4db6b80534c4827d8a55jkummerow@chromium.org      if (instr->IsArithmeticBinaryOperation()) {
42fb732b17922ea75830be4db6b80534c4827d8a55jkummerow@chromium.org        if (instr->representation().IsInteger32()) {
43fb732b17922ea75830be4db6b80534c4827d8a55jkummerow@chromium.org          if (instr->HasAtLeastOneUseWithFlagAndNoneWithout(
44fb732b17922ea75830be4db6b80534c4827d8a55jkummerow@chromium.org                  HInstruction::kTruncatingToInt32)) {
45fb732b17922ea75830be4db6b80534c4827d8a55jkummerow@chromium.org            instr->SetFlag(HInstruction::kAllUsesTruncatingToInt32);
46fb732b17922ea75830be4db6b80534c4827d8a55jkummerow@chromium.org          }
47fb732b17922ea75830be4db6b80534c4827d8a55jkummerow@chromium.org        } else if (instr->representation().IsSmi()) {
48fb732b17922ea75830be4db6b80534c4827d8a55jkummerow@chromium.org          if (instr->HasAtLeastOneUseWithFlagAndNoneWithout(
49fb732b17922ea75830be4db6b80534c4827d8a55jkummerow@chromium.org                  HInstruction::kTruncatingToSmi)) {
50fb732b17922ea75830be4db6b80534c4827d8a55jkummerow@chromium.org            instr->SetFlag(HInstruction::kAllUsesTruncatingToSmi);
51fb732b17922ea75830be4db6b80534c4827d8a55jkummerow@chromium.org          }
52fb732b17922ea75830be4db6b80534c4827d8a55jkummerow@chromium.org        }
53169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org      }
54169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org    }
55169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org  }
56169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org  // Perform actual Canonicalization pass.
57169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org  for (int i = 0; i < blocks->length(); ++i) {
58169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org    for (HInstructionIterator it(blocks->at(i)); !it.Done(); it.Advance()) {
59169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org      HInstruction* instr = it.Current();
60169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org      HValue* value = instr->Canonicalize();
61169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org      if (value != instr) instr->DeleteAndReplaceWith(value);
62169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org    }
63169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org  }
64169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org}
65169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org
66169691d93a961c8b511f8ac8fd8ee33d081ca10fdanno@chromium.org} }  // namespace v8::internal
67