Path: blob/master/test/hotspot/gtest/opto/test_regmask.cpp
41144 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "opto/opcodes.hpp"26#include "opto/regmask.hpp"27#include "unittest.hpp"2829// Sanity tests for RegMask and RegMaskIterator3031static void contains_expected_num_of_registers(const RegMask& rm, unsigned int expected) {3233ASSERT_TRUE(rm.Size() == expected);34if (expected > 0) {35ASSERT_TRUE(rm.is_NotEmpty());36} else {37ASSERT_TRUE(!rm.is_NotEmpty());38ASSERT_TRUE(!rm.is_AllStack());39}4041RegMaskIterator rmi(rm);42unsigned int count = 0;43OptoReg::Name reg = OptoReg::Bad;44while (rmi.has_next()) {45reg = rmi.next();46ASSERT_TRUE(OptoReg::is_valid(reg));47count++;48}49ASSERT_EQ(OptoReg::Bad, rmi.next());50ASSERT_TRUE(count == expected);51}5253TEST_VM(RegMask, empty) {54RegMask rm;55contains_expected_num_of_registers(rm, 0);56}5758TEST_VM(RegMask, iteration) {59RegMask rm;60rm.Insert(30);61rm.Insert(31);62rm.Insert(32);63rm.Insert(33);64rm.Insert(62);65rm.Insert(63);66rm.Insert(64);67rm.Insert(65);6869RegMaskIterator rmi(rm);70ASSERT_TRUE(rmi.next() == OptoReg::Name(30));71ASSERT_TRUE(rmi.next() == OptoReg::Name(31));72ASSERT_TRUE(rmi.next() == OptoReg::Name(32));73ASSERT_TRUE(rmi.next() == OptoReg::Name(33));74ASSERT_TRUE(rmi.next() == OptoReg::Name(62));75ASSERT_TRUE(rmi.next() == OptoReg::Name(63));76ASSERT_TRUE(rmi.next() == OptoReg::Name(64));77ASSERT_TRUE(rmi.next() == OptoReg::Name(65));78ASSERT_FALSE(rmi.has_next());79}8081TEST_VM(RegMask, Set_ALL) {82// Check that Set_All doesn't add bits outside of CHUNK_SIZE83RegMask rm;84rm.Set_All();85ASSERT_TRUE(rm.Size() == RegMask::CHUNK_SIZE);86ASSERT_TRUE(rm.is_NotEmpty());87// Set_All sets AllStack bit88ASSERT_TRUE(rm.is_AllStack());89contains_expected_num_of_registers(rm, RegMask::CHUNK_SIZE);90}9192TEST_VM(RegMask, Clear) {93// Check that Clear doesn't leave any stray bits94RegMask rm;95rm.Set_All();96rm.Clear();97contains_expected_num_of_registers(rm, 0);98}99100TEST_VM(RegMask, AND) {101RegMask rm1;102rm1.Insert(OptoReg::Name(1));103contains_expected_num_of_registers(rm1, 1);104ASSERT_TRUE(rm1.Member(OptoReg::Name(1)));105106rm1.AND(rm1);107contains_expected_num_of_registers(rm1, 1);108109RegMask rm2;110rm1.AND(rm2);111contains_expected_num_of_registers(rm1, 0);112contains_expected_num_of_registers(rm2, 0);113}114115TEST_VM(RegMask, OR) {116RegMask rm1;117rm1.Insert(OptoReg::Name(1));118contains_expected_num_of_registers(rm1, 1);119ASSERT_TRUE(rm1.Member(OptoReg::Name(1)));120121rm1.OR(rm1);122contains_expected_num_of_registers(rm1, 1);123124RegMask rm2;125rm1.OR(rm2);126contains_expected_num_of_registers(rm1, 1);127contains_expected_num_of_registers(rm2, 0);128}129130TEST_VM(RegMask, SUBTRACT) {131RegMask rm1;132RegMask rm2;133134rm2.Set_All();135for (int i = 17; i < RegMask::CHUNK_SIZE; i++) {136rm1.Insert(i);137}138ASSERT_TRUE(rm1.is_AllStack());139rm2.SUBTRACT(rm1);140contains_expected_num_of_registers(rm1, RegMask::CHUNK_SIZE - 17);141contains_expected_num_of_registers(rm2, 17);142}143144TEST_VM(RegMask, is_bound1) {145RegMask rm;146ASSERT_FALSE(rm.is_bound1());147for (int i = 0; i < RegMask::CHUNK_SIZE - 1; i++) {148rm.Insert(i);149ASSERT_TRUE(rm.is_bound1()) << "Index " << i;150ASSERT_TRUE(rm.is_bound(Op_RegI)) << "Index " << i;151contains_expected_num_of_registers(rm, 1);152rm.Remove(i);153}154// AllStack bit does not count as a bound register155rm.set_AllStack();156ASSERT_FALSE(rm.is_bound1());157}158159TEST_VM(RegMask, is_bound_pair) {160RegMask rm;161ASSERT_TRUE(rm.is_bound_pair());162for (int i = 0; i < RegMask::CHUNK_SIZE - 2; i++) {163rm.Insert(i);164rm.Insert(i + 1);165ASSERT_TRUE(rm.is_bound_pair()) << "Index " << i;166ASSERT_TRUE(rm.is_bound_set(2)) << "Index " << i;167ASSERT_TRUE(rm.is_bound(Op_RegI)) << "Index " << i;168contains_expected_num_of_registers(rm, 2);169rm.Clear();170}171// A pair with the AllStack bit does not count as a bound pair172rm.Clear();173rm.Insert(RegMask::CHUNK_SIZE - 2);174rm.Insert(RegMask::CHUNK_SIZE - 1);175ASSERT_FALSE(rm.is_bound_pair());176}177178TEST_VM(RegMask, is_bound_set) {179RegMask rm;180for (int size = 1; size <= 16; size++) {181ASSERT_TRUE(rm.is_bound_set(size));182for (int i = 0; i < RegMask::CHUNK_SIZE - size; i++) {183for (int j = i; j < i + size; j++) {184rm.Insert(j);185}186ASSERT_TRUE(rm.is_bound_set(size)) << "Size " << size << " Index " << i;187contains_expected_num_of_registers(rm, size);188rm.Clear();189}190// A set with the AllStack bit does not count as a bound set191for (int j = RegMask::CHUNK_SIZE - size; j < RegMask::CHUNK_SIZE; j++) {192rm.Insert(j);193}194ASSERT_FALSE(rm.is_bound_set(size));195rm.Clear();196}197}198199