xref: /freebsd/contrib/llvm-project/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMatInt.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
1*81ad6265SDimitry Andric //===- LoongArchMatInt.cpp - Immediate materialisation ---------*- C++ -*--===//
2*81ad6265SDimitry Andric //
3*81ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*81ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*81ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*81ad6265SDimitry Andric //
7*81ad6265SDimitry Andric //===----------------------------------------------------------------------===//
8*81ad6265SDimitry Andric 
9*81ad6265SDimitry Andric #include "LoongArchMatInt.h"
10*81ad6265SDimitry Andric #include "MCTargetDesc/LoongArchMCTargetDesc.h"
11*81ad6265SDimitry Andric #include "llvm/Support/MathExtras.h"
12*81ad6265SDimitry Andric 
13*81ad6265SDimitry Andric using namespace llvm;
14*81ad6265SDimitry Andric 
generateInstSeq(int64_t Val)15*81ad6265SDimitry Andric LoongArchMatInt::InstSeq LoongArchMatInt::generateInstSeq(int64_t Val) {
16*81ad6265SDimitry Andric   // Val:
17*81ad6265SDimitry Andric   // |            hi32              |              lo32            |
18*81ad6265SDimitry Andric   // +-----------+------------------+------------------+-----------+
19*81ad6265SDimitry Andric   // | Highest12 |    Higher20      |       Hi20       |    Lo12   |
20*81ad6265SDimitry Andric   // +-----------+------------------+------------------+-----------+
21*81ad6265SDimitry Andric   // 63        52 51              32 31              12 11         0
22*81ad6265SDimitry Andric   //
23*81ad6265SDimitry Andric   const int64_t Highest12 = Val >> 52 & 0xFFF;
24*81ad6265SDimitry Andric   const int64_t Higher20 = Val >> 32 & 0xFFFFF;
25*81ad6265SDimitry Andric   const int64_t Hi20 = Val >> 12 & 0xFFFFF;
26*81ad6265SDimitry Andric   const int64_t Lo12 = Val & 0xFFF;
27*81ad6265SDimitry Andric   InstSeq Insts;
28*81ad6265SDimitry Andric 
29*81ad6265SDimitry Andric   if (Highest12 != 0 && SignExtend64<52>(Val) == 0) {
30*81ad6265SDimitry Andric     Insts.push_back(Inst(LoongArch::LU52I_D, SignExtend64<12>(Highest12)));
31*81ad6265SDimitry Andric     return Insts;
32*81ad6265SDimitry Andric   }
33*81ad6265SDimitry Andric 
34*81ad6265SDimitry Andric   if (Hi20 == 0)
35*81ad6265SDimitry Andric     Insts.push_back(Inst(LoongArch::ORI, Lo12));
36*81ad6265SDimitry Andric   else if (SignExtend32<1>(Lo12 >> 11) == SignExtend32<20>(Hi20))
37*81ad6265SDimitry Andric     Insts.push_back(Inst(LoongArch::ADDI_W, SignExtend64<12>(Lo12)));
38*81ad6265SDimitry Andric   else {
39*81ad6265SDimitry Andric     Insts.push_back(Inst(LoongArch::LU12I_W, SignExtend64<20>(Hi20)));
40*81ad6265SDimitry Andric     if (Lo12 != 0)
41*81ad6265SDimitry Andric       Insts.push_back(Inst(LoongArch::ORI, Lo12));
42*81ad6265SDimitry Andric   }
43*81ad6265SDimitry Andric 
44*81ad6265SDimitry Andric   if (SignExtend32<1>(Hi20 >> 19) != SignExtend32<20>(Higher20))
45*81ad6265SDimitry Andric     Insts.push_back(Inst(LoongArch::LU32I_D, SignExtend64<20>(Higher20)));
46*81ad6265SDimitry Andric 
47*81ad6265SDimitry Andric   if (SignExtend32<1>(Higher20 >> 19) != SignExtend32<12>(Highest12))
48*81ad6265SDimitry Andric     Insts.push_back(Inst(LoongArch::LU52I_D, SignExtend64<12>(Highest12)));
49*81ad6265SDimitry Andric 
50*81ad6265SDimitry Andric   return Insts;
51*81ad6265SDimitry Andric }
52