aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/CodeGen/GlobalISel/LegalizerHelper.h
blob: 0fdc89feaba36f450d89c7cfa26903faab6fef02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_CODEGEN_GLOBALISEL_LEGALIZERHELPER_H
#define RCC_CODEGEN_GLOBALISEL_LEGALIZERHELPER_H

#include "rcc/CodeGen/GenericOpcodes.h"
#include "rcc/CodeGen/GlobalISel/LegalizerInfo.h"
#include "rcc/CodeGen/MachineIRBuilder.h"
#include "rcc/Support/Error.h"

#include <vector>

namespace rcc {

class MachineFunction;
class MachineInstr;
class GISelChangeObserver;

class LegalizerHelper final {
public:
  MachineIRBuilder &MIRBuilder;
  GISelChangeObserver &Observer;

  LegalizerHelper(MachineFunction &Function, const LegalizerInfo &Info,
                  GISelChangeObserver &Observer, MachineIRBuilder &MIRBuilder);

  [[nodiscard]] Expected<void> legalize(MachineInstr &Instruction,
                                        const LegalizationDecision &Decision);
  [[nodiscard]] Expected<void> lower(MachineInstr &Instruction,
                                     const LegalizationDecision &Decision);
  [[nodiscard]] Expected<void> lowerAbsToAddXor(MachineInstr &Instruction);
  [[nodiscard]] Expected<void> lowerAbsDiffToSelect(MachineInstr &Instruction);
  [[nodiscard]] Expected<void> lowerAbsDiffToMinMax(MachineInstr &Instruction);
  [[nodiscard]] Expected<void> lowerVAArg(MachineInstr &Instruction);
  [[nodiscard]] Expected<void> widenScalar(MachineInstr &Instruction, std::size_t TypeIndex,
                                           LowLevelType WideType);
  [[nodiscard]] Expected<void> widenScalarSrc(MachineInstr &Instruction, LowLevelType WideType,
                                              std::size_t OperandIndex,
                                              GenericOpcode ExtensionOpcode);
  [[nodiscard]] Expected<void> widenScalarDst(MachineInstr &Instruction, LowLevelType WideType,
                                              std::size_t OperandIndex = 0,
                                              GenericOpcode TruncOpcode = GenericOpcode::G_TRUNC);

  [[nodiscard]] Expected<void> narrowScalar(MachineInstr &Instruction, std::size_t TypeIndex,
                                            LowLevelType NarrowType);

private:
  [[nodiscard]] Expected<void> widenScalarMergeValues(MachineInstr &Instruction,
                                                      std::size_t TypeIndex, LowLevelType WideType);
  [[nodiscard]] Expected<void>
  widenScalarUnmergeValues(MachineInstr &Instruction, std::size_t TypeIndex, LowLevelType WideType);
  [[nodiscard]] Expected<void> insertParts(Register Destination, LowLevelType ResultType,
                                           LowLevelType PartType,
                                           const std::vector<Register> &PartRegisters,
                                           LowLevelType LeftoverType = {},
                                           const std::vector<Register> &LeftoverRegisters = {});
  [[nodiscard]] Expected<void> extractGCDType(std::vector<Register> &Parts, LowLevelType GCDType,
                                              Register Source);
  [[nodiscard]] Expected<LowLevelType> extractGCDType(std::vector<Register> &Parts,
                                                      LowLevelType DestinationType,
                                                      LowLevelType NarrowType, Register Source);
  [[nodiscard]] Expected<LowLevelType>
  buildLCMMergePieces(LowLevelType DestinationType, LowLevelType NarrowType, LowLevelType GCDType,
                      std::vector<Register> &Registers,
                      GenericOpcode PadStrategy = GenericOpcode::G_ANYEXT);
  [[nodiscard]] Expected<void>
  buildWidenedRemergeToDst(Register Destination, LowLevelType LCMType,
                           const std::vector<Register> &RemergeRegisters);
  [[nodiscard]] Expected<void> narrowScalarAddSub(MachineInstr &Instruction, std::size_t TypeIndex,
                                                  LowLevelType NarrowType);
  [[nodiscard]] Expected<void> narrowScalarExt(MachineInstr &Instruction, std::size_t TypeIndex,
                                               LowLevelType NarrowType);

  MachineFunction &Function;
  MachineRegisterInfo &MRI;
  const LegalizerInfo &Info;
};

} // namespace rcc

#endif // RCC_CODEGEN_GLOBALISEL_LEGALIZERHELPER_H