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