1*b5893f02SDimitry Andric //===- RISCVMatInt.cpp - Immediate materialisation -------------*- C++ -*--===//
2*b5893f02SDimitry Andric //
3*b5893f02SDimitry Andric //                     The LLVM Compiler Infrastructure
4*b5893f02SDimitry Andric //
5*b5893f02SDimitry Andric // This file is distributed under the University of Illinois Open Source
6*b5893f02SDimitry Andric // License. See LICENSE.TXT for details.
7*b5893f02SDimitry Andric //
8*b5893f02SDimitry Andric //===----------------------------------------------------------------------===//
9*b5893f02SDimitry Andric 
10*b5893f02SDimitry Andric #include "RISCVMatInt.h"
11*b5893f02SDimitry Andric #include "MCTargetDesc/RISCVMCTargetDesc.h"
12*b5893f02SDimitry Andric #include "llvm/ADT/SmallVector.h"
13*b5893f02SDimitry Andric #include "llvm/Support/MachineValueType.h"
14*b5893f02SDimitry Andric #include "llvm/Support/MathExtras.h"
15*b5893f02SDimitry Andric #include <cstdint>
16*b5893f02SDimitry Andric 
17*b5893f02SDimitry Andric namespace llvm {
18*b5893f02SDimitry Andric 
19*b5893f02SDimitry Andric namespace RISCVMatInt {
generateInstSeq(int64_t Val,bool Is64Bit,InstSeq & Res)20*b5893f02SDimitry Andric void generateInstSeq(int64_t Val, bool Is64Bit, InstSeq &Res) {
21*b5893f02SDimitry Andric   if (isInt<32>(Val)) {
22*b5893f02SDimitry Andric     // Depending on the active bits in the immediate Value v, the following
23*b5893f02SDimitry Andric     // instruction sequences are emitted:
24*b5893f02SDimitry Andric     //
25*b5893f02SDimitry Andric     // v == 0                        : ADDI
26*b5893f02SDimitry Andric     // v[0,12) != 0 && v[12,32) == 0 : ADDI
27*b5893f02SDimitry Andric     // v[0,12) == 0 && v[12,32) != 0 : LUI
28*b5893f02SDimitry Andric     // v[0,32) != 0                  : LUI+ADDI(W)
29*b5893f02SDimitry Andric     int64_t Hi20 = ((Val + 0x800) >> 12) & 0xFFFFF;
30*b5893f02SDimitry Andric     int64_t Lo12 = SignExtend64<12>(Val);
31*b5893f02SDimitry Andric 
32*b5893f02SDimitry Andric     if (Hi20)
33*b5893f02SDimitry Andric       Res.push_back(Inst(RISCV::LUI, Hi20));
34*b5893f02SDimitry Andric 
35*b5893f02SDimitry Andric     if (Lo12 || Hi20 == 0) {
36*b5893f02SDimitry Andric       unsigned AddiOpc = (Is64Bit && Hi20) ? RISCV::ADDIW : RISCV::ADDI;
37*b5893f02SDimitry Andric       Res.push_back(Inst(AddiOpc, Lo12));
38*b5893f02SDimitry Andric     }
39*b5893f02SDimitry Andric     return;
40*b5893f02SDimitry Andric   }
41*b5893f02SDimitry Andric 
42*b5893f02SDimitry Andric   assert(Is64Bit && "Can't emit >32-bit imm for non-RV64 target");
43*b5893f02SDimitry Andric 
44*b5893f02SDimitry Andric   // In the worst case, for a full 64-bit constant, a sequence of 8 instructions
45*b5893f02SDimitry Andric   // (i.e., LUI+ADDIW+SLLI+ADDI+SLLI+ADDI+SLLI+ADDI) has to be emmitted. Note
46*b5893f02SDimitry Andric   // that the first two instructions (LUI+ADDIW) can contribute up to 32 bits
47*b5893f02SDimitry Andric   // while the following ADDI instructions contribute up to 12 bits each.
48*b5893f02SDimitry Andric   //
49*b5893f02SDimitry Andric   // On the first glance, implementing this seems to be possible by simply
50*b5893f02SDimitry Andric   // emitting the most significant 32 bits (LUI+ADDIW) followed by as many left
51*b5893f02SDimitry Andric   // shift (SLLI) and immediate additions (ADDI) as needed. However, due to the
52*b5893f02SDimitry Andric   // fact that ADDI performs a sign extended addition, doing it like that would
53*b5893f02SDimitry Andric   // only be possible when at most 11 bits of the ADDI instructions are used.
54*b5893f02SDimitry Andric   // Using all 12 bits of the ADDI instructions, like done by GAS, actually
55*b5893f02SDimitry Andric   // requires that the constant is processed starting with the least significant
56*b5893f02SDimitry Andric   // bit.
57*b5893f02SDimitry Andric   //
58*b5893f02SDimitry Andric   // In the following, constants are processed from LSB to MSB but instruction
59*b5893f02SDimitry Andric   // emission is performed from MSB to LSB by recursively calling
60*b5893f02SDimitry Andric   // generateInstSeq. In each recursion, first the lowest 12 bits are removed
61*b5893f02SDimitry Andric   // from the constant and the optimal shift amount, which can be greater than
62*b5893f02SDimitry Andric   // 12 bits if the constant is sparse, is determined. Then, the shifted
63*b5893f02SDimitry Andric   // remaining constant is processed recursively and gets emitted as soon as it
64*b5893f02SDimitry Andric   // fits into 32 bits. The emission of the shifts and additions is subsequently
65*b5893f02SDimitry Andric   // performed when the recursion returns.
66*b5893f02SDimitry Andric 
67*b5893f02SDimitry Andric   int64_t Lo12 = SignExtend64<12>(Val);
68*b5893f02SDimitry Andric   int64_t Hi52 = (Val + 0x800) >> 12;
69*b5893f02SDimitry Andric   int ShiftAmount = 12 + findFirstSet((uint64_t)Hi52);
70*b5893f02SDimitry Andric   Hi52 = SignExtend64(Hi52 >> (ShiftAmount - 12), 64 - ShiftAmount);
71*b5893f02SDimitry Andric 
72*b5893f02SDimitry Andric   generateInstSeq(Hi52, Is64Bit, Res);
73*b5893f02SDimitry Andric 
74*b5893f02SDimitry Andric   Res.push_back(Inst(RISCV::SLLI, ShiftAmount));
75*b5893f02SDimitry Andric   if (Lo12)
76*b5893f02SDimitry Andric     Res.push_back(Inst(RISCV::ADDI, Lo12));
77*b5893f02SDimitry Andric }
78*b5893f02SDimitry Andric } // namespace RISCVMatInt
79*b5893f02SDimitry Andric } // namespace llvm
80