xref: /freebsd/contrib/llvm-project/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMatInt.cpp (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
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"
10 #include "MCTargetDesc/LoongArchMCTargetDesc.h"
11 #include "llvm/Support/MathExtras.h"
12 
13 using namespace llvm;
14 
15 LoongArchMatInt::InstSeq LoongArchMatInt::generateInstSeq(int64_t Val) {
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   if (Highest12 != 0 && SignExtend64<52>(Val) == 0) {
30     Insts.push_back(Inst(LoongArch::LU52I_D, SignExtend64<12>(Highest12)));
31     return Insts;
32   }
33 
34   if (Hi20 == 0)
35     Insts.push_back(Inst(LoongArch::ORI, Lo12));
36   else if (SignExtend32<1>(Lo12 >> 11) == SignExtend32<20>(Hi20))
37     Insts.push_back(Inst(LoongArch::ADDI_W, SignExtend64<12>(Lo12)));
38   else {
39     Insts.push_back(Inst(LoongArch::LU12I_W, SignExtend64<20>(Hi20)));
40     if (Lo12 != 0)
41       Insts.push_back(Inst(LoongArch::ORI, Lo12));
42   }
43 
44   if (SignExtend32<1>(Hi20 >> 19) != SignExtend32<20>(Higher20))
45     Insts.push_back(Inst(LoongArch::LU32I_D, SignExtend64<20>(Higher20)));
46 
47   if (SignExtend32<1>(Higher20 >> 19) != SignExtend32<12>(Highest12))
48     Insts.push_back(Inst(LoongArch::LU52I_D, SignExtend64<12>(Highest12)));
49 
50   return Insts;
51 }
52