SPARCBranchBailoutTest.java revision 13083:b9a173f12fe6
1/*
2 * Copyright (c) 2016, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.graalvm.compiler.lir.jtt;
24
25import org.graalvm.compiler.api.directives.GraalDirectives;
26import org.graalvm.compiler.core.common.PermanentBailoutException;
27import org.graalvm.compiler.debug.Debug;
28import org.graalvm.compiler.debug.DebugConfigScope;
29import org.graalvm.compiler.debug.GraalError;
30import org.graalvm.compiler.lir.LIRInstruction;
31import org.graalvm.compiler.lir.LIRInstructionClass;
32import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
33import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
34import org.junit.Assert;
35import org.junit.Assume;
36import org.junit.Test;
37
38import jdk.vm.ci.code.BailoutException;
39import jdk.vm.ci.meta.ResolvedJavaMethod;
40import jdk.vm.ci.meta.Value;
41import jdk.vm.ci.sparc.SPARC;
42
43/**
44 * Tests the {@link BailoutException} thrown, when trying to compile huge methods, which have branch
45 * displacements which does not fit into 19 bit signed.
46 */
47public class SPARCBranchBailoutTest extends LIRTest {
48    private static class BranchSpec extends LIRTestSpecification {
49        private final int n;
50
51        BranchSpec(int n) {
52            super();
53            this.n = n;
54        }
55
56        @Override
57        public void generate(LIRGeneratorTool gen, Value a) {
58            gen.append(new LargeOp(n));
59            setResult(a);
60        }
61    }
62
63    private static final BranchSpec spec = new BranchSpec(1 << 20);
64
65    @LIRIntrinsic
66    public static int branch(@SuppressWarnings("unused") BranchSpec s, int a) {
67        return a;
68    }
69
70    public static int testBranch(int length) {
71        int res = 1;
72        if (length > 0) {
73            res = branch(spec, 1);
74        } else {
75            res = branch(spec, 2);
76        }
77        return GraalDirectives.opaque(res);
78    }
79
80    @SuppressWarnings("try")
81    @Test
82    public void testBailoutOnBranchOverflow() throws Throwable {
83        Assume.assumeTrue(getBackend().getTarget().arch instanceof SPARC);
84        ResolvedJavaMethod m = getResolvedJavaMethod("testBranch");
85        try {
86            try (DebugConfigScope s = Debug.setConfig(Debug.silentConfig())) {
87                compile(m, null);
88            }
89        } catch (GraalError e) {
90            Assert.assertEquals(PermanentBailoutException.class, e.getCause().getClass());
91        }
92    }
93
94    public static class LargeOp extends LIRInstruction {
95        private static final LIRInstructionClass<LargeOp> TYPE = LIRInstructionClass.create(LargeOp.class);
96        private final int n;
97
98        public LargeOp(int n) {
99            super(TYPE);
100            this.n = n;
101        }
102
103        @Override
104        public void emitCode(CompilationResultBuilder crb) {
105            for (int i = 0; i < n; i++) {
106                crb.asm.emitInt(0);
107            }
108        }
109    }
110}
111