| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
//===- RISCVMatInt.cpp - Immediate materialisation -------------*- C++ |
| 7 |
//-*--===// |
| 8 |
// |
| 9 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM |
| 10 |
// Exceptions. See https://llvm.org/LICENSE.txt for license information. |
| 11 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 12 |
// |
| 13 |
//===----------------------------------------------------------------------===// |
| 14 |
#include "mozilla/MathAlgorithms.h" |
| 15 |
#include "mozilla/Maybe.h" |
| 16 |
|
| 17 |
#include "gc/Marking.h" |
| 18 |
#include "jit/AutoWritableJitCode.h" |
| 19 |
#include "jit/ExecutableAllocator.h" |
| 20 |
#include "jit/riscv64/Assembler-riscv64.h" |
| 21 |
#include "jit/riscv64/disasm/Disasm-riscv64.h" |
| 22 |
#include "vm/Realm.h" |
| 23 |
namespace js { |
| 24 |
namespace jit { |
| 25 |
void Assembler::RecursiveLi(Register rd, int64_t val) { |
| 26 |
if (val > 0 && RecursiveLiImplCount(val) > 2) { |
| 27 |
unsigned LeadingZeros = mozilla::CountLeadingZeroes64((uint64_t)val); |
| 28 |
uint64_t ShiftedVal = (uint64_t)val << LeadingZeros; |
| 29 |
int countFillZero = RecursiveLiImplCount(ShiftedVal) + 1; |
| 30 |
if (countFillZero < RecursiveLiImplCount(val)) { |
| 31 |
RecursiveLiImpl(rd, ShiftedVal); |
| 32 |
srli(rd, rd, LeadingZeros); |
| 33 |
return; |
| 34 |
} |
| 35 |
} |
| 36 |
RecursiveLiImpl(rd, val); |
| 37 |
} |
| 38 |
|
| 39 |
int Assembler::RecursiveLiCount(int64_t val) { |
| 40 |
if (val > 0 && RecursiveLiImplCount(val) > 2) { |
| 41 |
unsigned LeadingZeros = mozilla::CountLeadingZeroes64((uint64_t)val); |
| 42 |
uint64_t ShiftedVal = (uint64_t)val << LeadingZeros; |
| 43 |
// Fill in the bits that will be shifted out with 1s. An example where |
| 44 |
// this helps is trailing one masks with 32 or more ones. This will |
| 45 |
// generate ADDI -1 and an SRLI. |
| 46 |
int countFillZero = RecursiveLiImplCount(ShiftedVal) + 1; |
| 47 |
if (countFillZero < RecursiveLiImplCount(val)) { |
| 48 |
return countFillZero; |
| 49 |
} |
| 50 |
} |
| 51 |
return RecursiveLiImplCount(val); |
| 52 |
} |
| 53 |
|
| 54 |
inline int64_t signExtend(uint64_t V, int N) { |
| 55 |
return int64_t(V << (64 - N)) >> (64 - N); |
| 56 |
} |
| 57 |
|
| 58 |
void Assembler::RecursiveLiImpl(Register rd, int64_t Val) { |
| 59 |
if (is_int32(Val)) { |
| 60 |
// Depending on the active bits in the immediate Value v, the following |
| 61 |
// instruction sequences are emitted: |
| 62 |
// |
| 63 |
// v == 0 : ADDI |
| 64 |
// v[0,12) != 0 && v[12,32) == 0 : ADDI |
| 65 |
// v[0,12) == 0 && v[12,32) != 0 : LUI |
| 66 |
// v[0,32) != 0 : LUI+ADDI(W) |
| 67 |
int64_t Hi20 = ((Val + 0x800) >> 12) & 0xFFFFF; |
| 68 |
int64_t Lo12 = Val << 52 >> 52; |
| 69 |
|
| 70 |
if (Hi20) { |
| 71 |
lui(rd, (int32_t)Hi20); |
| 72 |
} |
| 73 |
|
| 74 |
if (Lo12 || Hi20 == 0) { |
| 75 |
if (Hi20) { |
| 76 |
addiw(rd, rd, Lo12); |
| 77 |
} else { |
| 78 |
addi(rd, zero_reg, Lo12); |
| 79 |
} |
| 80 |
} |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
// In the worst case, for a full 64-bit constant, a sequence of 8 |
| 85 |
// instructions (i.e., LUI+ADDIW+SLLI+ADDI+SLLI+ADDI+SLLI+ADDI) has to be |
| 86 |
// emitted. Note that the first two instructions (LUI+ADDIW) can contribute |
| 87 |
// up to 32 bits while the following ADDI instructions contribute up to 12 |
| 88 |
// bits each. |
| 89 |
// |
| 90 |
// On the first glance, implementing this seems to be possible by simply |
| 91 |
// emitting the most significant 32 bits (LUI+ADDIW) followed by as many |
| 92 |
// left shift (SLLI) and immediate additions (ADDI) as needed. However, due |
| 93 |
// to the fact that ADDI performs a sign extended addition, doing it like |
| 94 |
// that would only be possible when at most 11 bits of the ADDI instructions |
| 95 |
// are used. Using all 12 bits of the ADDI instructions, like done by GAS, |
| 96 |
// actually requires that the constant is processed starting with the least |
| 97 |
// significant bit. |
| 98 |
// |
| 99 |
// In the following, constants are processed from LSB to MSB but instruction |
| 100 |
// emission is performed from MSB to LSB by recursively calling |
| 101 |
// generateInstSeq. In each recursion, first the lowest 12 bits are removed |
| 102 |
// from the constant and the optimal shift amount, which can be greater than |
| 103 |
// 12 bits if the constant is sparse, is determined. Then, the shifted |
| 104 |
// remaining constant is processed recursively and gets emitted as soon as |
| 105 |
// it fits into 32 bits. The emission of the shifts and additions is |
| 106 |
// subsequently performed when the recursion returns. |
| 107 |
|
| 108 |
int64_t Lo12 = Val << 52 >> 52; |
| 109 |
int64_t Hi52 = ((uint64_t)Val + 0x800ull) >> 12; |
| 110 |
int ShiftAmount = 12 + mozilla::CountTrailingZeroes64((uint64_t)Hi52); |
| 111 |
Hi52 = signExtend(Hi52 >> (ShiftAmount - 12), 64 - ShiftAmount); |
| 112 |
|
| 113 |
// If the remaining bits don't fit in 12 bits, we might be able to reduce |
| 114 |
// the shift amount in order to use LUI which will zero the lower 12 bits. |
| 115 |
bool Unsigned = false; |
| 116 |
if (ShiftAmount > 12 && !is_int12(Hi52)) { |
| 117 |
if (is_int32((uint64_t)Hi52 << 12)) { |
| 118 |
// Reduce the shift amount and add zeros to the LSBs so it will match |
| 119 |
// LUI. |
| 120 |
ShiftAmount -= 12; |
| 121 |
Hi52 = (uint64_t)Hi52 << 12; |
| 122 |
} |
| 123 |
} |
| 124 |
RecursiveLi(rd, Hi52); |
| 125 |
|
| 126 |
if (Unsigned) { |
| 127 |
} else { |
| 128 |
slli(rd, rd, ShiftAmount); |
| 129 |
} |
| 130 |
if (Lo12) { |
| 131 |
addi(rd, rd, Lo12); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
int Assembler::RecursiveLiImplCount(int64_t Val) { |
| 136 |
int count = 0; |
| 137 |
if (is_int32(Val)) { |
| 138 |
// Depending on the active bits in the immediate Value v, the following |
| 139 |
// instruction sequences are emitted: |
| 140 |
// |
| 141 |
// v == 0 : ADDI |
| 142 |
// v[0,12) != 0 && v[12,32) == 0 : ADDI |
| 143 |
// v[0,12) == 0 && v[12,32) != 0 : LUI |
| 144 |
// v[0,32) != 0 : LUI+ADDI(W) |
| 145 |
int64_t Hi20 = ((Val + 0x800) >> 12) & 0xFFFFF; |
| 146 |
int64_t Lo12 = Val << 52 >> 52; |
| 147 |
|
| 148 |
if (Hi20) { |
| 149 |
// lui(rd, (int32_t)Hi20); |
| 150 |
count++; |
| 151 |
} |
| 152 |
|
| 153 |
if (Lo12 || Hi20 == 0) { |
| 154 |
// unsigned AddiOpc = (IsRV64 && Hi20) ? RISCV::ADDIW : RISCV::ADDI; |
| 155 |
// Res.push_back(RISCVMatInt::Inst(AddiOpc, Lo12)); |
| 156 |
count++; |
| 157 |
} |
| 158 |
return count; |
| 159 |
} |
| 160 |
|
| 161 |
// In the worst case, for a full 64-bit constant, a sequence of 8 |
| 162 |
// instructions (i.e., LUI+ADDIW+SLLI+ADDI+SLLI+ADDI+SLLI+ADDI) has to be |
| 163 |
// emitted. Note that the first two instructions (LUI+ADDIW) can contribute |
| 164 |
// up to 32 bits while the following ADDI instructions contribute up to 12 |
| 165 |
// bits each. |
| 166 |
// |
| 167 |
// On the first glance, implementing this seems to be possible by simply |
| 168 |
// emitting the most significant 32 bits (LUI+ADDIW) followed by as many |
| 169 |
// left shift (SLLI) and immediate additions (ADDI) as needed. However, due |
| 170 |
// to the fact that ADDI performs a sign extended addition, doing it like |
| 171 |
// that would only be possible when at most 11 bits of the ADDI instructions |
| 172 |
// are used. Using all 12 bits of the ADDI instructions, like done by GAS, |
| 173 |
// actually requires that the constant is processed starting with the least |
| 174 |
// significant bit. |
| 175 |
// |
| 176 |
// In the following, constants are processed from LSB to MSB but instruction |
| 177 |
// emission is performed from MSB to LSB by recursively calling |
| 178 |
// generateInstSeq. In each recursion, first the lowest 12 bits are removed |
| 179 |
// from the constant and the optimal shift amount, which can be greater than |
| 180 |
// 12 bits if the constant is sparse, is determined. Then, the shifted |
| 181 |
// remaining constant is processed recursively and gets emitted as soon as |
| 182 |
// it fits into 32 bits. The emission of the shifts and additions is |
| 183 |
// subsequently performed when the recursion returns. |
| 184 |
|
| 185 |
int64_t Lo12 = Val << 52 >> 52; |
| 186 |
int64_t Hi52 = ((uint64_t)Val + 0x800ull) >> 12; |
| 187 |
int ShiftAmount = 12 + mozilla::CountTrailingZeroes64((uint64_t)Hi52); |
| 188 |
Hi52 = signExtend(Hi52 >> (ShiftAmount - 12), 64 - ShiftAmount); |
| 189 |
|
| 190 |
// If the remaining bits don't fit in 12 bits, we might be able to reduce |
| 191 |
// the shift amount in order to use LUI which will zero the lower 12 bits. |
| 192 |
bool Unsigned = false; |
| 193 |
if (ShiftAmount > 12 && !is_int12(Hi52)) { |
| 194 |
if (is_int32((uint64_t)Hi52 << 12)) { |
| 195 |
// Reduce the shift amount and add zeros to the LSBs so it will match |
| 196 |
// LUI. |
| 197 |
ShiftAmount -= 12; |
| 198 |
Hi52 = (uint64_t)Hi52 << 12; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
count += RecursiveLiImplCount(Hi52); |
| 203 |
|
| 204 |
if (Unsigned) { |
| 205 |
} else { |
| 206 |
// slli(rd, rd, ShiftAmount); |
| 207 |
count++; |
| 208 |
} |
| 209 |
if (Lo12) { |
| 210 |
// addi(rd, rd, Lo12); |
| 211 |
count++; |
| 212 |
} |
| 213 |
return count; |
| 214 |
} |
| 215 |
|
| 216 |
} // namespace jit |
| 217 |
} // namespace js |