DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "jit/RegisterSets.h"

#include "jsapi-tests/tests.h"

using namespace js;
using namespace js::jit;

static bool CoPrime(size_t a, size_t b) {
  if (b <= 1) {
    return a == 1 || b == 1;
  }
  return CoPrime(b, a % b);
}

// This macros are use to iterave over all registers in a large number of
// non-looping sequences, which does not rely on the getFirst / getLast
// functions.
#define BEGIN_INDEX_WALK(RegTotal)                      \
  static const size_t Total = RegTotal;                 \
  for (size_t walk = 1; walk < RegTotal; walk += 2) {   \
    if (!CoPrime(RegTotal, walk)) continue;             \
    for (size_t start = 0; start < RegTotal; start++) { \
      size_t index = start;

#define END_INDEX_WALK \
  }                    \
  }

#define BEGIN_All_WALK(RegTotal)        \
  static const size_t Total = RegTotal; \
  size_t walk = 1;                      \
  size_t start = 0;                     \
  size_t index = start;

#define FOR_ALL_REGISTERS(Register, reg) \
  do {                                   \
    Register reg = Register::FromCode(index);

#define END_FOR_ALL_REGISTERS     \
  index = (index + walk) % Total; \
  }                               \
  while (index != start)

BEGIN_TEST(testJitRegisterSet_GPR) {
  BEGIN_INDEX_WALK(Registers::Total)

  LiveGeneralRegisterSet liveRegs;
  AllocatableGeneralRegisterSet pool(GeneralRegisterSet::All());
  CHECK(liveRegs.empty());
  CHECK(pool.set() == GeneralRegisterSet::All());

  FOR_ALL_REGISTERS(Register, reg) {
    CHECK(!pool.has(reg) || !liveRegs.has(reg));
    if (pool.has(reg)) {
      CHECK(!liveRegs.has(reg));
      pool.take(reg);
      liveRegs.add(reg);
      CHECK(liveRegs.has(reg));
      CHECK(!pool.has(reg));
    }
    CHECK(!pool.has(reg) || !liveRegs.has(reg));
  }
  END_FOR_ALL_REGISTERS;

  CHECK(pool.empty());

  FOR_ALL_REGISTERS(Register, reg) {
    CHECK(!pool.has(reg) || !liveRegs.has(reg));
    if (liveRegs.has(reg)) {
      CHECK(!pool.has(reg));
      liveRegs.take(reg);
      pool.add(reg);
      CHECK(pool.has(reg));
      CHECK(!liveRegs.has(reg));
    }
    CHECK(!pool.has(reg) || !liveRegs.has(reg));
  }
  END_FOR_ALL_REGISTERS;

  CHECK(liveRegs.empty());
  CHECK(pool.set() == GeneralRegisterSet::All());

  END_INDEX_WALK
  return true;
}
END_TEST(testJitRegisterSet_GPR)

BEGIN_TEST(testJitRegisterSet_FPU) {
  BEGIN_INDEX_WALK(FloatRegisters::Total)

  LiveFloatRegisterSet liveRegs;
  AllocatableFloatRegisterSet pool(FloatRegisterSet::All());
  CHECK(liveRegs.empty());
  CHECK(pool.set() == FloatRegisterSet::All());

  FOR_ALL_REGISTERS(FloatRegister, reg) {
    CHECK(!pool.has(reg) || !liveRegs.has(reg));
    if (pool.has(reg)) {
      CHECK(!liveRegs.has(reg));
      pool.take(reg);
      liveRegs.add(reg);
      CHECK(liveRegs.has(reg));
      CHECK(!pool.has(reg));
    }
    CHECK(!pool.has(reg) || !liveRegs.has(reg));
  }
  END_FOR_ALL_REGISTERS;

  CHECK(pool.empty());

  FOR_ALL_REGISTERS(FloatRegister, reg) {
    CHECK(!pool.has(reg) || !liveRegs.has(reg));
    if (liveRegs.has(reg)) {
      CHECK(!pool.has(reg));
      liveRegs.take(reg);
      pool.add(reg);
      CHECK(pool.has(reg));
      CHECK(!liveRegs.has(reg));
    }
    CHECK(!pool.has(reg) || !liveRegs.has(reg));
  }
  END_FOR_ALL_REGISTERS;

  CHECK(liveRegs.empty());
  CHECK(pool.set() == FloatRegisterSet::All());

  END_INDEX_WALK
  return true;
}
END_TEST(testJitRegisterSet_FPU)

void pullAllFpus(AllocatableFloatRegisterSet& set, uint32_t& max_bits,
                 uint32_t bits) {
  FloatRegisterSet allocSet(set.bits());
  FloatRegisterSet available_f32(
      allocSet.allAllocatable<RegTypeName::Float32>());
  FloatRegisterSet available_f64(
      allocSet.allAllocatable<RegTypeName::Float64>());
  FloatRegisterSet available_v128(
      allocSet.allAllocatable<RegTypeName::Vector128>());
  for (FloatRegisterIterator it(available_f32); it.more(); ++it) {
    FloatRegister tmp = *it;
    set.take(tmp);
    pullAllFpus(set, max_bits, bits + 32);
    set.add(tmp);
  }
  for (FloatRegisterIterator it(available_f64); it.more(); ++it) {
    FloatRegister tmp = *it;
    set.take(tmp);
    pullAllFpus(set, max_bits, bits + 64);
    set.add(tmp);
  }
  for (FloatRegisterIterator it(available_v128); it.more(); ++it) {
    FloatRegister tmp = *it;
    set.take(tmp);
    pullAllFpus(set, max_bits, bits + 128);
    set.add(tmp);
  }
  if (bits >= max_bits) {
    max_bits = bits;
  }
}

BEGIN_TEST(testJitRegisterSet_FPU_Aliases) {
  BEGIN_All_WALK(FloatRegisters::Total);
  FOR_ALL_REGISTERS(FloatRegister, reg) {
    AllocatableFloatRegisterSet pool;
    pool.add(reg);

    uint32_t alias_bits = 0;
    for (uint32_t i = 0; i < reg.numAlignedAliased(); i++) {
      FloatRegister alias = reg.alignedAliased(i);

      if (alias.isSingle()) {
        if (alias_bits <= 32) {
          alias_bits = 32;
        }
      } else if (alias.isDouble()) {
        if (alias_bits <= 64) {
          alias_bits = 64;
        }
      } else if (alias.isSimd128()) {
        if (alias_bits <= 128) {
          alias_bits = 128;
        }
      }
    }

    uint32_t max_bits = 0;
    pullAllFpus(pool, max_bits, 0);

    // By adding one register, we expect that we should not be able to pull
    // more than any of its aligned aliases.  This rule should hold for both
    // x64 and ARM.
    CHECK(max_bits <= alias_bits);

    // We added one register, we expect to be able to pull it back.
    CHECK(max_bits > 0);
  }
  END_FOR_ALL_REGISTERS;

  return true;
}
END_TEST(testJitRegisterSet_FPU_Aliases)