LLVM 20.0.0git
HexagonMask.cpp
Go to the documentation of this file.
1//===-- HexagonMask.cpp - replace const ext tfri with mask ------===//
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//===----------------------------------------------------------------------===//
10
11#define DEBUG_TYPE "mask"
12
14#include "HexagonSubtarget.h"
17#include "llvm/ADT/Statistic.h"
18#include "llvm/ADT/Twine.h"
22#include "llvm/CodeGen/Passes.h"
24#include "llvm/IR/Function.h"
25#include "llvm/IR/Module.h"
27#include "llvm/Support/Debug.h"
30
31using namespace llvm;
32
33namespace llvm {
36
38public:
39 static char ID;
43 }
44
45 StringRef getPassName() const override {
46 return "Hexagon replace const ext tfri with mask";
47 }
48 bool runOnMachineFunction(MachineFunction &MF) override;
49
50private:
51 const HexagonInstrInfo *HII;
52 void replaceConstExtTransferImmWithMask(MachineFunction &MF);
53};
54
55char HexagonMask::ID = 0;
56
57void HexagonMask::replaceConstExtTransferImmWithMask(MachineFunction &MF) {
58 for (auto &MBB : MF) {
59 for (auto &MI : llvm::make_early_inc_range(MBB)) {
60 if (MI.getOpcode() != Hexagon::A2_tfrsi)
61 continue;
62
63 const MachineOperand &Op0 = MI.getOperand(0);
64 const MachineOperand &Op1 = MI.getOperand(1);
65 if (!Op1.isImm())
66 continue;
67 int32_t V = Op1.getImm();
68 if (isInt<16>(V))
69 continue;
70
71 unsigned Idx, Len;
72 if (!isShiftedMask_32(V, Idx, Len))
73 continue;
74 if (!isUInt<5>(Idx) || !isUInt<5>(Len))
75 continue;
76
77 BuildMI(MBB, MI, MI.getDebugLoc(), HII->get(Hexagon::S2_mask),
78 Op0.getReg())
79 .addImm(Len)
80 .addImm(Idx);
81 MBB.erase(MI);
82 }
83 }
84}
85
87 auto &HST = MF.getSubtarget<HexagonSubtarget>();
88 HII = HST.getInstrInfo();
89 const Function &F = MF.getFunction();
90
91 if (!F.hasFnAttribute(Attribute::OptimizeForSize))
92 return false;
93 // Mask instruction is available only from v66
94 if (!HST.hasV66Ops())
95 return false;
96 // The mask instruction available in v66 can be used to generate values in
97 // registers using 2 immediates Eg. to form 0x07fffffc in R0, you would write
98 // "R0 = mask(#25,#2)" Since it is a single-word instruction, it takes less
99 // code size than a constant-extended transfer at Os
100 replaceConstExtTransferImmWithMask(MF);
101
102 return true;
103}
104
105} // namespace llvm
106
107//===----------------------------------------------------------------------===//
108// Public Constructor Functions
109//===----------------------------------------------------------------------===//
110
111INITIALIZE_PASS(HexagonMask, "hexagon-mask", "Hexagon mask", false, false)
112
MachineBasicBlock & MBB
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
Module.h This file contains the declarations for the Module class.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
This file defines the SmallString class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
static char ID
Definition: HexagonMask.cpp:39
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: HexagonMask.cpp:86
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
Definition: HexagonMask.cpp:45
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
Register getReg() const
getReg - Returns the register number.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:37
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:656
constexpr bool isShiftedMask_32(uint32_t Value)
Return true if the argument contains a non-empty sequence of ones with the remainder zero (32 bit ver...
Definition: MathExtras.h:279
void initializeHexagonMaskPass(PassRegistry &)
FunctionPass * createHexagonMask()