xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMatInt.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1e8d8bef9SDimitry Andric //===- RISCVMatInt.h - Immediate materialisation ---------------*- C++ -*--===//
2e8d8bef9SDimitry Andric //
3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8d8bef9SDimitry Andric //
7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8e8d8bef9SDimitry Andric 
904eeddc0SDimitry Andric #ifndef LLVM_LIB_TARGET_RISCV_MCTARGETDESC_MATINT_H
1004eeddc0SDimitry Andric #define LLVM_LIB_TARGET_RISCV_MCTARGETDESC_MATINT_H
11e8d8bef9SDimitry Andric 
12e8d8bef9SDimitry Andric #include "llvm/ADT/SmallVector.h"
13*0fca6ea1SDimitry Andric #include "llvm/MC/MCRegister.h"
145f757f3fSDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
15e8d8bef9SDimitry Andric #include <cstdint>
16e8d8bef9SDimitry Andric 
17e8d8bef9SDimitry Andric namespace llvm {
18e8d8bef9SDimitry Andric class APInt;
19e8d8bef9SDimitry Andric 
20e8d8bef9SDimitry Andric namespace RISCVMatInt {
2181ad6265SDimitry Andric 
2281ad6265SDimitry Andric enum OpndKind {
2381ad6265SDimitry Andric   RegImm, // ADDI/ADDIW/SLLI/SRLI/BSETI/BCLRI
2481ad6265SDimitry Andric   Imm,    // LUI
2581ad6265SDimitry Andric   RegReg, // SH1ADD/SH2ADD/SH3ADD
2681ad6265SDimitry Andric   RegX0,  // ADD_UW
2781ad6265SDimitry Andric };
2881ad6265SDimitry Andric 
29bdd1243dSDimitry Andric class Inst {
30e8d8bef9SDimitry Andric   unsigned Opc;
31bdd1243dSDimitry Andric   int32_t Imm; // The largest value we need to store is 20 bits.
32e8d8bef9SDimitry Andric 
33bdd1243dSDimitry Andric public:
34bdd1243dSDimitry Andric   Inst(unsigned Opc, int64_t I) : Opc(Opc), Imm(I) {
35bdd1243dSDimitry Andric     assert(I == Imm && "truncated");
36bdd1243dSDimitry Andric   }
37bdd1243dSDimitry Andric 
38bdd1243dSDimitry Andric   unsigned getOpcode() const { return Opc; }
39bdd1243dSDimitry Andric   int64_t getImm() const { return Imm; }
4081ad6265SDimitry Andric 
4181ad6265SDimitry Andric   OpndKind getOpndKind() const;
42e8d8bef9SDimitry Andric };
43e8d8bef9SDimitry Andric using InstSeq = SmallVector<Inst, 8>;
44e8d8bef9SDimitry Andric 
45e8d8bef9SDimitry Andric // Helper to generate an instruction sequence that will materialise the given
46fe6060f1SDimitry Andric // immediate value into a register. A sequence of instructions represented by a
47fe6060f1SDimitry Andric // simple struct is produced rather than directly emitting the instructions in
48e8d8bef9SDimitry Andric // order to allow this helper to be used from both the MC layer and during
49e8d8bef9SDimitry Andric // instruction selection.
505f757f3fSDimitry Andric InstSeq generateInstSeq(int64_t Val, const MCSubtargetInfo &STI);
515f757f3fSDimitry Andric 
52*0fca6ea1SDimitry Andric // Helper to generate the generateInstSeq instruction sequence using MCInsts
53*0fca6ea1SDimitry Andric void generateMCInstSeq(int64_t Val, const MCSubtargetInfo &STI,
54*0fca6ea1SDimitry Andric                        MCRegister DestReg, SmallVectorImpl<MCInst> &Insts);
55*0fca6ea1SDimitry Andric 
565f757f3fSDimitry Andric // Helper to generate an instruction sequence that can materialize the given
575f757f3fSDimitry Andric // immediate value into a register using an additional temporary register. This
585f757f3fSDimitry Andric // handles cases where the constant can be generated by (ADD (SLLI X, C), X) or
595f757f3fSDimitry Andric // (ADD_UW (SLLI X, C) X). The sequence to generate X is returned. ShiftAmt is
605f757f3fSDimitry Andric // provides the SLLI and AddOpc indicates ADD or ADD_UW.
615f757f3fSDimitry Andric InstSeq generateTwoRegInstSeq(int64_t Val, const MCSubtargetInfo &STI,
625f757f3fSDimitry Andric                               unsigned &ShiftAmt, unsigned &AddOpc);
63e8d8bef9SDimitry Andric 
64e8d8bef9SDimitry Andric // Helper to estimate the number of instructions required to materialise the
65e8d8bef9SDimitry Andric // given immediate value into a register. This estimate does not account for
66e8d8bef9SDimitry Andric // `Val` possibly fitting into an immediate, and so may over-estimate.
67e8d8bef9SDimitry Andric //
68e8d8bef9SDimitry Andric // This will attempt to produce instructions to materialise `Val` as an
69fe6060f1SDimitry Andric // `Size`-bit immediate.
70fe6060f1SDimitry Andric //
71fe6060f1SDimitry Andric // If CompressionCost is true it will use a different cost calculation if RVC is
72fe6060f1SDimitry Andric // enabled. This should be used to compare two different sequences to determine
73fe6060f1SDimitry Andric // which is more compressible.
74*0fca6ea1SDimitry Andric //
75*0fca6ea1SDimitry Andric // If FreeZeroes is true, it will be assumed free to materialize any
76*0fca6ea1SDimitry Andric // XLen-sized chunks that are 0. This is appropriate to use in instances when
77*0fca6ea1SDimitry Andric // the zero register can be used, e.g. when estimating the cost of
78*0fca6ea1SDimitry Andric // materializing a value used by a particular operation.
795f757f3fSDimitry Andric int getIntMatCost(const APInt &Val, unsigned Size, const MCSubtargetInfo &STI,
80*0fca6ea1SDimitry Andric                   bool CompressionCost = false, bool FreeZeroes = false);
81e8d8bef9SDimitry Andric } // namespace RISCVMatInt
82e8d8bef9SDimitry Andric } // namespace llvm
83e8d8bef9SDimitry Andric #endif
84