1 //===-- MVEVPTBlockPass.cpp - Insert MVE VPT blocks -----------------------===//
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
7 //===----------------------------------------------------------------------===//
10 #include "ARMMachineFunctionInfo.h"
11 #include "ARMSubtarget.h"
12 #include "MCTargetDesc/ARMBaseInfo.h"
13 #include "Thumb2InstrInfo.h"
14 #include "llvm/ADT/SmallSet.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/CodeGen/MachineBasicBlock.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/MachineInstr.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineInstrBundle.h"
24 #include "llvm/CodeGen/MachineOperand.h"
25 #include "llvm/IR/DebugLoc.h"
26 #include "llvm/MC/MCInstrDesc.h"
27 #include "llvm/MC/MCRegisterInfo.h"
28 #include "llvm/Support/Debug.h"
34 #define DEBUG_TYPE "arm-mve-vpt"
37 class MVEVPTBlock
: public MachineFunctionPass
{
40 const Thumb2InstrInfo
*TII
;
41 const TargetRegisterInfo
*TRI
;
43 MVEVPTBlock() : MachineFunctionPass(ID
) {}
45 bool runOnMachineFunction(MachineFunction
&Fn
) override
;
47 MachineFunctionProperties
getRequiredProperties() const override
{
48 return MachineFunctionProperties().set(
49 MachineFunctionProperties::Property::NoVRegs
);
52 StringRef
getPassName() const override
{
53 return "MVE VPT block insertion pass";
57 bool InsertVPTBlocks(MachineBasicBlock
&MBB
);
60 char MVEVPTBlock::ID
= 0;
62 } // end anonymous namespace
64 INITIALIZE_PASS(MVEVPTBlock
, DEBUG_TYPE
, "ARM MVE VPT block pass", false, false)
84 static unsigned VCMPOpcodeToVPT(unsigned Opcode
) {
86 case ARM::MVE_VCMPf32
:
87 return ARM::MVE_VPTv4f32
;
88 case ARM::MVE_VCMPf16
:
89 return ARM::MVE_VPTv8f16
;
91 return ARM::MVE_VPTv16i8
;
92 case ARM::MVE_VCMPi16
:
93 return ARM::MVE_VPTv8i16
;
94 case ARM::MVE_VCMPi32
:
95 return ARM::MVE_VPTv4i32
;
97 return ARM::MVE_VPTv16u8
;
98 case ARM::MVE_VCMPu16
:
99 return ARM::MVE_VPTv8u16
;
100 case ARM::MVE_VCMPu32
:
101 return ARM::MVE_VPTv4u32
;
102 case ARM::MVE_VCMPs8
:
103 return ARM::MVE_VPTv16s8
;
104 case ARM::MVE_VCMPs16
:
105 return ARM::MVE_VPTv8s16
;
106 case ARM::MVE_VCMPs32
:
107 return ARM::MVE_VPTv4s32
;
109 case ARM::MVE_VCMPf32r
:
110 return ARM::MVE_VPTv4f32r
;
111 case ARM::MVE_VCMPf16r
:
112 return ARM::MVE_VPTv8f16r
;
113 case ARM::MVE_VCMPi8r
:
114 return ARM::MVE_VPTv16i8r
;
115 case ARM::MVE_VCMPi16r
:
116 return ARM::MVE_VPTv8i16r
;
117 case ARM::MVE_VCMPi32r
:
118 return ARM::MVE_VPTv4i32r
;
119 case ARM::MVE_VCMPu8r
:
120 return ARM::MVE_VPTv16u8r
;
121 case ARM::MVE_VCMPu16r
:
122 return ARM::MVE_VPTv8u16r
;
123 case ARM::MVE_VCMPu32r
:
124 return ARM::MVE_VPTv4u32r
;
125 case ARM::MVE_VCMPs8r
:
126 return ARM::MVE_VPTv16s8r
;
127 case ARM::MVE_VCMPs16r
:
128 return ARM::MVE_VPTv8s16r
;
129 case ARM::MVE_VCMPs32r
:
130 return ARM::MVE_VPTv4s32r
;
137 static MachineInstr
*findVCMPToFoldIntoVPST(MachineBasicBlock::iterator MI
,
138 const TargetRegisterInfo
*TRI
,
139 unsigned &NewOpcode
) {
140 // Search backwards to the instruction that defines VPR. This may or not
141 // be a VCMP, we check that after this loop. If we find another instruction
142 // that reads cpsr, we return nullptr.
143 MachineBasicBlock::iterator CmpMI
= MI
;
144 while (CmpMI
!= MI
->getParent()->begin()) {
146 if (CmpMI
->modifiesRegister(ARM::VPR
, TRI
))
148 if (CmpMI
->readsRegister(ARM::VPR
, TRI
))
154 NewOpcode
= VCMPOpcodeToVPT(CmpMI
->getOpcode());
158 // Search forward from CmpMI to MI, checking if either register was def'd
159 if (registerDefinedBetween(CmpMI
->getOperand(1).getReg(), std::next(CmpMI
),
162 if (registerDefinedBetween(CmpMI
->getOperand(2).getReg(), std::next(CmpMI
),
168 bool MVEVPTBlock::InsertVPTBlocks(MachineBasicBlock
&Block
) {
169 bool Modified
= false;
170 MachineBasicBlock::instr_iterator MBIter
= Block
.instr_begin();
171 MachineBasicBlock::instr_iterator EndIter
= Block
.instr_end();
173 while (MBIter
!= EndIter
) {
174 MachineInstr
*MI
= &*MBIter
;
175 unsigned PredReg
= 0;
176 DebugLoc dl
= MI
->getDebugLoc();
178 ARMVCC::VPTCodes Pred
= getVPTInstrPredicate(*MI
, PredReg
);
180 // The idea of the predicate is that None, Then and Else are for use when
181 // handling assembly language: they correspond to the three possible
182 // suffixes "", "t" and "e" on the mnemonic. So when instructions are read
183 // from assembly source or disassembled from object code, you expect to see
184 // a mixture whenever there's a long VPT block. But in code generation, we
185 // hope we'll never generate an Else as input to this pass.
186 assert(Pred
!= ARMVCC::Else
&& "VPT block pass does not expect Else preds");
188 if (Pred
== ARMVCC::None
) {
193 LLVM_DEBUG(dbgs() << "VPT block created for: "; MI
->dump());
195 ARMVCC::VPTCodes NextPred
;
197 // Look at subsequent instructions, checking if they can be in the same VPT
200 while (MBIter
!= EndIter
&& VPTInstCnt
< 4) {
201 NextPred
= getVPTInstrPredicate(*MBIter
, PredReg
);
202 assert(NextPred
!= ARMVCC::Else
&&
203 "VPT block pass does not expect Else preds");
204 if (NextPred
!= Pred
)
206 LLVM_DEBUG(dbgs() << " adding : "; MBIter
->dump());
211 unsigned BlockMask
= 0;
212 switch (VPTInstCnt
) {
214 BlockMask
= VPTMaskValue::T
;
217 BlockMask
= VPTMaskValue::TT
;
220 BlockMask
= VPTMaskValue::TTT
;
223 BlockMask
= VPTMaskValue::TTTT
;
226 llvm_unreachable("Unexpected number of instruction in a VPT block");
229 // Search back for a VCMP that can be folded to create a VPT, or else create
231 MachineInstrBuilder MIBuilder
;
233 MachineInstr
*VCMP
= findVCMPToFoldIntoVPST(MI
, TRI
, NewOpcode
);
235 LLVM_DEBUG(dbgs() << " folding VCMP into VPST: "; VCMP
->dump());
236 MIBuilder
= BuildMI(Block
, MI
, dl
, TII
->get(NewOpcode
));
237 MIBuilder
.addImm(BlockMask
);
238 MIBuilder
.add(VCMP
->getOperand(1));
239 MIBuilder
.add(VCMP
->getOperand(2));
240 MIBuilder
.add(VCMP
->getOperand(3));
241 VCMP
->eraseFromParent();
243 MIBuilder
= BuildMI(Block
, MI
, dl
, TII
->get(ARM::MVE_VPST
));
244 MIBuilder
.addImm(BlockMask
);
248 Block
, MachineBasicBlock::instr_iterator(MIBuilder
.getInstr()), MBIter
);
255 bool MVEVPTBlock::runOnMachineFunction(MachineFunction
&Fn
) {
256 const ARMSubtarget
&STI
=
257 static_cast<const ARMSubtarget
&>(Fn
.getSubtarget());
259 if (!STI
.isThumb2() || !STI
.hasMVEIntegerOps())
262 TII
= static_cast<const Thumb2InstrInfo
*>(STI
.getInstrInfo());
263 TRI
= STI
.getRegisterInfo();
265 LLVM_DEBUG(dbgs() << "********** ARM MVE VPT BLOCKS **********\n"
266 << "********** Function: " << Fn
.getName() << '\n');
268 bool Modified
= false;
269 for (MachineBasicBlock
&MBB
: Fn
)
270 Modified
|= InsertVPTBlocks(MBB
);
272 LLVM_DEBUG(dbgs() << "**************************************\n");
276 /// createMVEVPTBlock - Returns an instance of the MVE VPT block
278 FunctionPass
*llvm::createMVEVPTBlockPass() { return new MVEVPTBlock(); }