1 /* 2 * Copyright (c) 2012, 2015, 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 */ 23 package org.graalvm.compiler.lir.sparc; 24 25 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG; 26 import static jdk.vm.ci.code.ValueUtil.asRegister; 27 import static jdk.vm.ci.code.ValueUtil.isRegister; 28 import static jdk.vm.ci.sparc.SPARC.g0; 29 import static jdk.vm.ci.sparc.SPARCKind.WORD; 30 import static jdk.vm.ci.sparc.SPARCKind.XWORD; 31 32 import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler; 33 import org.graalvm.compiler.core.common.LIRKind; 34 import org.graalvm.compiler.debug.GraalError; 35 import org.graalvm.compiler.lir.LIRInstructionClass; 36 import org.graalvm.compiler.lir.Opcode; 37 import org.graalvm.compiler.lir.asm.CompilationResultBuilder; 38 import org.graalvm.compiler.lir.gen.LIRGeneratorTool; 39 40 import jdk.vm.ci.code.Register; 41 import jdk.vm.ci.meta.AllocatableValue; 42 import jdk.vm.ci.meta.PlatformKind; 43 import jdk.vm.ci.meta.Value; 44 45 public final class SPARCBitManipulationOp extends SPARCLIRInstruction { 46 public static final LIRInstructionClass<SPARCBitManipulationOp> TYPE = LIRInstructionClass.create(SPARCBitManipulationOp.class); 47 48 public enum IntrinsicOpcode { 49 IBSR(SizeEstimate.create(13)), 50 LBSR(SizeEstimate.create(14)), 51 BSF(SizeEstimate.create(4)); 52 53 final SizeEstimate size; 54 55 IntrinsicOpcode(SizeEstimate size) { 56 this.size = size; 57 } 58 } 59 60 @Opcode private final IntrinsicOpcode opcode; 61 @Def protected AllocatableValue result; 62 @Alive({REG}) protected AllocatableValue input; 63 @Temp({REG}) protected Value scratch; 64 65 public SPARCBitManipulationOp(IntrinsicOpcode opcode, AllocatableValue result, AllocatableValue input, LIRGeneratorTool gen) { 66 super(TYPE, opcode.size); 67 this.opcode = opcode; 68 this.result = result; 69 this.input = input; 70 scratch = gen.newVariable(LIRKind.combine(input)); 71 } 72 73 @Override 74 public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) { 75 Register dst = asRegister(result, WORD); 76 if (isRegister(input)) { 77 Register src = asRegister(input); 78 switch (opcode) { 79 case BSF: 80 PlatformKind tkind = input.getPlatformKind(); 81 if (tkind == WORD) { 82 masm.sub(src, 1, dst); 83 masm.andn(dst, src, dst); 84 masm.srl(dst, g0, dst); 85 masm.popc(dst, dst); 86 } else if (tkind == XWORD) { 87 masm.sub(src, 1, dst); 88 masm.andn(dst, src, dst); 89 masm.popc(dst, dst); 90 } else { 91 throw GraalError.shouldNotReachHere("missing: " + tkind); 92 } 93 break; 94 case IBSR: { 95 PlatformKind ikind = input.getPlatformKind(); 96 assert ikind == WORD; 97 Register tmp = asRegister(scratch); 98 assert !tmp.equals(dst); 99 masm.srl(src, 1, tmp); 100 masm.srl(src, 0, dst); 101 masm.or(dst, tmp, dst); 102 masm.srl(dst, 2, tmp); 103 masm.or(dst, tmp, dst); 104 masm.srl(dst, 4, tmp); 105 masm.or(dst, tmp, dst); 106 masm.srl(dst, 8, tmp); 107 masm.or(dst, tmp, dst); 108 masm.srl(dst, 16, tmp); 109 masm.or(dst, tmp, dst); 110 masm.popc(dst, dst); 111 masm.sub(dst, 1, dst); 112 break; 113 } 114 case LBSR: { 115 PlatformKind lkind = input.getPlatformKind(); 116 assert lkind == XWORD; 117 Register tmp = asRegister(scratch); 118 assert !tmp.equals(dst); 119 masm.srlx(src, 1, tmp); 120 masm.or(src, tmp, dst); 121 masm.srlx(dst, 2, tmp); 122 masm.or(dst, tmp, dst); 123 masm.srlx(dst, 4, tmp); 124 masm.or(dst, tmp, dst); 125 masm.srlx(dst, 8, tmp); 126 masm.or(dst, tmp, dst); 127 masm.srlx(dst, 16, tmp); 128 masm.or(dst, tmp, dst); 129 masm.srlx(dst, 32, tmp); 130 masm.or(dst, tmp, dst); 131 masm.popc(dst, dst); 132 masm.sub(dst, 1, dst); // This is required to fit the given structure. 133 break; 134 } 135 default: 136 throw GraalError.shouldNotReachHere(); 137 138 } 139 } else { 140 throw GraalError.shouldNotReachHere(); 141 } 142 } 143 }