xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMatInt.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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"
13fe6060f1SDimitry Andric #include "llvm/MC/SubtargetFeature.h"
14e8d8bef9SDimitry Andric #include <cstdint>
15e8d8bef9SDimitry Andric 
16e8d8bef9SDimitry Andric namespace llvm {
17e8d8bef9SDimitry Andric class APInt;
18e8d8bef9SDimitry Andric 
19e8d8bef9SDimitry Andric namespace RISCVMatInt {
2081ad6265SDimitry Andric 
2181ad6265SDimitry Andric enum OpndKind {
2281ad6265SDimitry Andric   RegImm, // ADDI/ADDIW/SLLI/SRLI/BSETI/BCLRI
2381ad6265SDimitry Andric   Imm,    // LUI
2481ad6265SDimitry Andric   RegReg, // SH1ADD/SH2ADD/SH3ADD
2581ad6265SDimitry Andric   RegX0,  // ADD_UW
2681ad6265SDimitry Andric };
2781ad6265SDimitry Andric 
28*bdd1243dSDimitry Andric class Inst {
29e8d8bef9SDimitry Andric   unsigned Opc;
30*bdd1243dSDimitry Andric   int32_t Imm; // The largest value we need to store is 20 bits.
31e8d8bef9SDimitry Andric 
32*bdd1243dSDimitry Andric public:
33*bdd1243dSDimitry Andric   Inst(unsigned Opc, int64_t I) : Opc(Opc), Imm(I) {
34*bdd1243dSDimitry Andric     assert(I == Imm && "truncated");
35*bdd1243dSDimitry Andric   }
36*bdd1243dSDimitry Andric 
37*bdd1243dSDimitry Andric   unsigned getOpcode() const { return Opc; }
38*bdd1243dSDimitry Andric   int64_t getImm() const { return Imm; }
3981ad6265SDimitry Andric 
4081ad6265SDimitry Andric   OpndKind getOpndKind() const;
41e8d8bef9SDimitry Andric };
42e8d8bef9SDimitry Andric using InstSeq = SmallVector<Inst, 8>;
43e8d8bef9SDimitry Andric 
44e8d8bef9SDimitry Andric // Helper to generate an instruction sequence that will materialise the given
45fe6060f1SDimitry Andric // immediate value into a register. A sequence of instructions represented by a
46fe6060f1SDimitry Andric // simple struct is produced rather than directly emitting the instructions in
47e8d8bef9SDimitry Andric // order to allow this helper to be used from both the MC layer and during
48e8d8bef9SDimitry Andric // instruction selection.
49fe6060f1SDimitry Andric InstSeq generateInstSeq(int64_t Val, const FeatureBitset &ActiveFeatures);
50e8d8bef9SDimitry Andric 
51e8d8bef9SDimitry Andric // Helper to estimate the number of instructions required to materialise the
52e8d8bef9SDimitry Andric // given immediate value into a register. This estimate does not account for
53e8d8bef9SDimitry Andric // `Val` possibly fitting into an immediate, and so may over-estimate.
54e8d8bef9SDimitry Andric //
55e8d8bef9SDimitry Andric // This will attempt to produce instructions to materialise `Val` as an
56fe6060f1SDimitry Andric // `Size`-bit immediate.
57fe6060f1SDimitry Andric //
58fe6060f1SDimitry Andric // If CompressionCost is true it will use a different cost calculation if RVC is
59fe6060f1SDimitry Andric // enabled. This should be used to compare two different sequences to determine
60fe6060f1SDimitry Andric // which is more compressible.
61fe6060f1SDimitry Andric int getIntMatCost(const APInt &Val, unsigned Size,
62fe6060f1SDimitry Andric                   const FeatureBitset &ActiveFeatures,
63fe6060f1SDimitry Andric                   bool CompressionCost = false);
64e8d8bef9SDimitry Andric } // namespace RISCVMatInt
65e8d8bef9SDimitry Andric } // namespace llvm
66e8d8bef9SDimitry Andric #endif
67