LLVM 24.0.0git
LoongArchMatInt.cpp
Go to the documentation of this file.
1//===- LoongArchMatInt.cpp - Immediate materialisation ---------*- C++ -*--===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "LoongArchMatInt.h"
12
13using namespace llvm;
14
16 // Val:
17 // | hi32 | lo32 |
18 // +-----------+------------------+------------------+-----------+
19 // | Highest12 | Higher20 | Hi20 | Lo12 |
20 // +-----------+------------------+------------------+-----------+
21 // 63 52 51 32 31 12 11 0
22 //
23 const int64_t Highest12 = Val >> 52 & 0xFFF;
24 const int64_t Higher20 = Val >> 32 & 0xFFFFF;
25 const int64_t Hi20 = Val >> 12 & 0xFFFFF;
26 const int64_t Lo12 = Val & 0xFFF;
27 InstSeq Insts;
28
29 // LU52I_D used for: Bits[63:52] | Bits[51:0].
30 if (Highest12 != 0 && SignExtend64<52>(Val) == 0) {
31 Insts.push_back(Inst(LoongArch::LU52I_D, SignExtend64<12>(Highest12)));
32 return Insts;
33 }
34
35 if ((Val & 0xFFFFFFFF) == 0) {
36 const int32_t Hi32 = Val >> 32;
37 const int TZC = llvm::countr_zero(static_cast<uint32_t>(Hi32));
38 if (TZC < 20 && isInt<20>(Hi32 >> TZC)) {
39 Insts.push_back(Inst(LoongArch::LU12I_W, Hi32 >> TZC));
40 Insts.push_back(Inst(LoongArch::SLLI_D, TZC + 20));
41 return Insts;
42 }
43 }
44
45 // lo32
46 if (Hi20 == 0)
47 Insts.push_back(Inst(LoongArch::ORI, Lo12));
48 else if (SignExtend32<1>(Lo12 >> 11) == SignExtend32<20>(Hi20))
49 Insts.push_back(Inst(LoongArch::ADDI_W, SignExtend64<12>(Lo12)));
50 else {
51 Insts.push_back(Inst(LoongArch::LU12I_W, SignExtend64<20>(Hi20)));
52 if (Lo12 != 0)
53 Insts.push_back(Inst(LoongArch::ORI, Lo12));
54 }
55
56 // hi32
57 // Higher20
58 if (SignExtend32<1>(Hi20 >> 19) != SignExtend32<20>(Higher20))
59 Insts.push_back(Inst(LoongArch::LU32I_D, SignExtend64<20>(Higher20)));
60
61 // Highest12
62 if (SignExtend32<1>(Higher20 >> 19) != SignExtend32<12>(Highest12))
63 Insts.push_back(Inst(LoongArch::LU52I_D, SignExtend64<12>(Highest12)));
64
65 size_t N = Insts.size();
66 if (N < 3)
67 return Insts;
68
69 // When the number of instruction sequences is greater than 2, we have the
70 // opportunity to optimize using the BSTRINS_D instruction. The scenario is as
71 // follows:
72 //
73 // N of Insts = 3
74 // 1. ORI + LU32I_D + LU52I_D => ORI + BSTRINS_D, TmpVal = ORI
75 // 2. ADDI_W + LU32I_D + LU52I_D => ADDI_W + BSTRINS_D, TmpVal = ADDI_W
76 // 3. LU12I_W + ORI + LU32I_D => ORI + BSTRINS_D, TmpVal = ORI
77 // 4. LU12I_W + LU32I_D + LU52I_D => LU12I_W + BSTRINS_D, TmpVal = LU12I_W
78 //
79 // N of Insts = 4
80 // 5. LU12I_W + ORI + LU32I_D + LU52I_D => LU12I_W + ORI + BSTRINS_D
81 // => ORI + LU52I_D + BSTRINS_D
82 // TmpVal = (LU12I_W | ORI) or (ORI | LU52I_D)
83 // The BSTRINS_D instruction will use the `TmpVal` to construct the `Val`.
84 uint64_t TmpVal1 = 0;
85 uint64_t TmpVal2 = 0;
86 switch (Insts[0].Opc) {
87 default:
88 llvm_unreachable("unexpected opcode");
89 break;
90 case LoongArch::LU12I_W:
91 if (Insts[1].Opc == LoongArch::ORI) {
92 TmpVal1 = Insts[1].Imm;
93 if (N == 3)
94 break;
95 TmpVal2 = static_cast<uint64_t>(Insts[3].Imm) << 52 | TmpVal1;
96 }
97 TmpVal1 |= static_cast<uint64_t>(Insts[0].Imm) << 12;
98 break;
99 case LoongArch::ORI:
100 case LoongArch::ADDI_W:
101 TmpVal1 = Insts[0].Imm;
102 break;
103 }
104
105 uint64_t Msb = 32;
106 uint64_t HighMask = ~((1ULL << (Msb + 1)) - 1);
107 for (; Msb < 64; ++Msb, HighMask = HighMask << 1) {
108 for (uint64_t Lsb = Msb; Lsb > 0; --Lsb) {
109 uint64_t LowMask = (1ULL << Lsb) - 1;
110 uint64_t Mask = HighMask | LowMask;
111 uint64_t LsbToZero = TmpVal1 & ((1ULL << (Msb - Lsb + 1)) - 1);
112 uint64_t MsbToLsb = LsbToZero << Lsb;
113 if ((MsbToLsb | (TmpVal1 & Mask)) == (uint64_t)Val) {
114 if (Insts[1].Opc == LoongArch::ORI && N == 3)
115 Insts[0] = Insts[1];
116 Insts.pop_back_n(2);
117 Insts.push_back(Inst(LoongArch::BSTRINS_D, Msb << 32 | Lsb));
118 return Insts;
119 }
120 if (TmpVal2 != 0) {
121 LsbToZero = TmpVal2 & ((1ULL << (Msb - Lsb + 1)) - 1);
122 MsbToLsb = LsbToZero << Lsb;
123 if ((MsbToLsb | (TmpVal2 & Mask)) == (uint64_t)Val) {
124 Insts[0] = Insts[1];
125 Insts[1] = Insts[3];
126 Insts.pop_back_n(2);
127 Insts.push_back(Inst(LoongArch::BSTRINS_D, Msb << 32 | Lsb));
128 return Insts;
129 }
130 }
131 }
132 }
133
134 return Insts;
135}
void pop_back_n(size_type NumItems)
void push_back(const T &Elt)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
InstSeq generateInstSeq(int64_t Val)
SmallVector< Inst, 4 > InstSeq
This is an optimization pass for GlobalISel generic memory operations.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
constexpr int32_t SignExtend32(uint32_t X)
Sign-extend the number in the bottom B bits of X to a 32-bit integer.
Definition MathExtras.h:554
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
#define N