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
13#include "HexagonSubtarget.h"
14#include "llvm/ADT/Statistic.h"
18#include "llvm/CodeGen/Passes.h"
19#include "llvm/IR/Function.h"
22
23using namespace llvm;
24
25namespace llvm {
28
30public:
31 static char ID;
35 }
36
37 StringRef getPassName() const override {
38 return "Hexagon replace const ext tfri with mask";
39 }
40 bool runOnMachineFunction(MachineFunction &MF) override;
41
42private:
43 const HexagonInstrInfo *HII;
44 void replaceConstExtTransferImmWithMask(MachineFunction &MF);
45};
46
47char HexagonMask::ID = 0;
48
49void HexagonMask::replaceConstExtTransferImmWithMask(MachineFunction &MF) {
50 for (auto &MBB : MF) {
51 for (auto &MI : llvm::make_early_inc_range(MBB)) {
52 if (MI.getOpcode() != Hexagon::A2_tfrsi)
53 continue;
54
55 const MachineOperand &Op0 = MI.getOperand(0);
56 const MachineOperand &Op1 = MI.getOperand(1);
57 if (!Op1.isImm())
58 continue;
59 int32_t V = Op1.getImm();
60 if (isInt<16>(V))
61 continue;
62
63 unsigned Idx, Len;
64 if (!isShiftedMask_32(V, Idx, Len))
65 continue;
66 if (!isUInt<5>(Idx) || !isUInt<5>(Len))
67 continue;
68
69 BuildMI(MBB, MI, MI.getDebugLoc(), HII->get(Hexagon::S2_mask),
70 Op0.getReg())
71 .addImm(Len)
72 .addImm(Idx);
73 MBB.erase(MI);
74 }
75 }
76}
77
79 auto &HST = MF.getSubtarget<HexagonSubtarget>();
80 HII = HST.getInstrInfo();
81 const Function &F = MF.getFunction();
82
83 if (!F.hasFnAttribute(Attribute::OptimizeForSize))
84 return false;
85 // Mask instruction is available only from v66
86 if (!HST.hasV66Ops())
87 return false;
88 // The mask instruction available in v66 can be used to generate values in
89 // registers using 2 immediates Eg. to form 0x07fffffc in R0, you would write
90 // "R0 = mask(#25,#2)" Since it is a single-word instruction, it takes less
91 // code size than a constant-extended transfer at Os
92 replaceConstExtTransferImmWithMask(MF);
93
94 return true;
95}
96
97} // namespace llvm
98
99//===----------------------------------------------------------------------===//
100// Public Constructor Functions
101//===----------------------------------------------------------------------===//
102
103INITIALIZE_PASS(HexagonMask, "hexagon-mask", "Hexagon mask", false, false)
104
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
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
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:31
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: HexagonMask.cpp:78
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
Definition: HexagonMask.cpp:37
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:51
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:657
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()