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