1 //===-- MVEVPTBlockPass.cpp - Insert MVE VPT blocks -----------------------===//
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 #include "ARM.h"
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"
29 #include <cassert>
30 #include <new>
31 
32 using namespace llvm;
33 
34 #define DEBUG_TYPE "arm-mve-vpt"
35 
36 namespace {
37 class MVEVPTBlock : public MachineFunctionPass {
38 public:
39   static char ID;
40   const Thumb2InstrInfo *TII;
41   const TargetRegisterInfo *TRI;
42 
43   MVEVPTBlock() : MachineFunctionPass(ID) {}
44 
45   bool runOnMachineFunction(MachineFunction &Fn) override;
46 
47   MachineFunctionProperties getRequiredProperties() const override {
48     return MachineFunctionProperties().set(
49         MachineFunctionProperties::Property::NoVRegs);
50   }
51 
52   StringRef getPassName() const override {
53     return "MVE VPT block insertion pass";
54   }
55 
56 private:
57   bool InsertVPTBlocks(MachineBasicBlock &MBB);
58 };
59 
60 char MVEVPTBlock::ID = 0;
61 
62 } // end anonymous namespace
63 
64 INITIALIZE_PASS(MVEVPTBlock, DEBUG_TYPE, "ARM MVE VPT block pass", false, false)
65 
66 static MachineInstr *findVCMPToFoldIntoVPST(MachineBasicBlock::iterator MI,
67                                             const TargetRegisterInfo *TRI,
68                                             unsigned &NewOpcode) {
69   // Search backwards to the instruction that defines VPR. This may or not
70   // be a VCMP, we check that after this loop. If we find another instruction
71   // that reads cpsr, we return nullptr.
72   MachineBasicBlock::iterator CmpMI = MI;
73   while (CmpMI != MI->getParent()->begin()) {
74     --CmpMI;
75     if (CmpMI->modifiesRegister(ARM::VPR, TRI))
76       break;
77     if (CmpMI->readsRegister(ARM::VPR, TRI))
78       break;
79   }
80 
81   if (CmpMI == MI)
82     return nullptr;
83   NewOpcode = VCMPOpcodeToVPT(CmpMI->getOpcode());
84   if (NewOpcode == 0)
85     return nullptr;
86 
87   // Search forward from CmpMI to MI, checking if either register was def'd
88   if (registerDefinedBetween(CmpMI->getOperand(1).getReg(), std::next(CmpMI),
89                              MI, TRI))
90     return nullptr;
91   if (registerDefinedBetween(CmpMI->getOperand(2).getReg(), std::next(CmpMI),
92                              MI, TRI))
93     return nullptr;
94   return &*CmpMI;
95 }
96 
97 static ARM::PredBlockMask ExpandBlockMask(ARM::PredBlockMask BlockMask,
98                                           ARMVCC::VPTCodes Kind) {
99   using PredBlockMask = ARM::PredBlockMask;
100   assert(Kind != ARMVCC::None && "Cannot expand mask with 'None'");
101   assert(countTrailingZeros((unsigned)BlockMask) != 0 &&
102          "Mask is already full");
103 
104   auto ChooseMask = [&](PredBlockMask AddedThen, PredBlockMask AddedElse) {
105     return (Kind == ARMVCC::Then) ? AddedThen : AddedElse;
106   };
107 
108   switch (BlockMask) {
109   case PredBlockMask::T:
110     return ChooseMask(PredBlockMask::TT, PredBlockMask::TE);
111   case PredBlockMask::TT:
112     return ChooseMask(PredBlockMask::TTT, PredBlockMask::TTE);
113   case PredBlockMask::TE:
114     return ChooseMask(PredBlockMask::TET, PredBlockMask::TEE);
115   case PredBlockMask::TTT:
116     return ChooseMask(PredBlockMask::TTTT, PredBlockMask::TTTE);
117   case PredBlockMask::TTE:
118     return ChooseMask(PredBlockMask::TTET, PredBlockMask::TTEE);
119   case PredBlockMask::TET:
120     return ChooseMask(PredBlockMask::TETT, PredBlockMask::TETE);
121   case PredBlockMask::TEE:
122     return ChooseMask(PredBlockMask::TEET, PredBlockMask::TEEE);
123   default:
124     llvm_unreachable("Unknown Mask");
125   }
126 }
127 
128 // Advances Iter past a block of predicated instructions.
129 // Returns true if it successfully skipped the whole block of predicated
130 // instructions. Returns false when it stopped early (due to MaxSteps), or if
131 // Iter didn't point to a predicated instruction.
132 static bool StepOverPredicatedInstrs(MachineBasicBlock::instr_iterator &Iter,
133                                      MachineBasicBlock::instr_iterator EndIter,
134                                      unsigned MaxSteps,
135                                      unsigned &NumInstrsSteppedOver) {
136   ARMVCC::VPTCodes NextPred = ARMVCC::None;
137   Register PredReg;
138   NumInstrsSteppedOver = 0;
139 
140   while (Iter != EndIter) {
141     NextPred = getVPTInstrPredicate(*Iter, PredReg);
142     assert(NextPred != ARMVCC::Else &&
143            "VPT block pass does not expect Else preds");
144     if (NextPred == ARMVCC::None || MaxSteps == 0)
145       break;
146     --MaxSteps;
147     ++Iter;
148     ++NumInstrsSteppedOver;
149   };
150 
151   return NumInstrsSteppedOver != 0 &&
152          (NextPred == ARMVCC::None || Iter == EndIter);
153 }
154 
155 // Returns true if at least one instruction in the range [Iter, End) defines
156 // or kills VPR.
157 static bool IsVPRDefinedOrKilledByBlock(MachineBasicBlock::iterator Iter,
158                                         MachineBasicBlock::iterator End) {
159   for (; Iter != End; ++Iter)
160     if (Iter->definesRegister(ARM::VPR) || Iter->killsRegister(ARM::VPR))
161       return true;
162   return false;
163 }
164 
165 // Given an iterator (Iter) that points at an instruction with a "Then"
166 // predicate, tries to create the largest block of continuous predicated
167 // instructions possible, and returns the VPT Block Mask of that block.
168 //
169 // This will try to perform some minor optimization in order to maximize the
170 // size of the block.
171 static ARM::PredBlockMask
172 CreateVPTBlock(MachineBasicBlock::instr_iterator &Iter,
173                MachineBasicBlock::instr_iterator EndIter,
174                SmallVectorImpl<MachineInstr *> &DeadInstructions) {
175   MachineBasicBlock::instr_iterator BlockBeg = Iter;
176   (void)BlockBeg;
177   assert(getVPTInstrPredicate(*Iter) == ARMVCC::Then &&
178          "Expected a Predicated Instruction");
179 
180   LLVM_DEBUG(dbgs() << "VPT block created for: "; Iter->dump());
181 
182   unsigned BlockSize;
183   StepOverPredicatedInstrs(Iter, EndIter, 4, BlockSize);
184 
185   LLVM_DEBUG(for (MachineBasicBlock::instr_iterator AddedInstIter =
186                       std::next(BlockBeg);
187                   AddedInstIter != Iter; ++AddedInstIter) {
188     dbgs() << "  adding: ";
189     AddedInstIter->dump();
190   });
191 
192   // Generate the initial BlockMask
193   ARM::PredBlockMask BlockMask = getARMVPTBlockMask(BlockSize);
194 
195   // Remove VPNOTs while there's still room in the block, so we can make the
196   // largest block possible.
197   ARMVCC::VPTCodes CurrentPredicate = ARMVCC::Else;
198   while (BlockSize < 4 && Iter != EndIter &&
199          Iter->getOpcode() == ARM::MVE_VPNOT) {
200 
201     // Try to skip all of the predicated instructions after the VPNOT, stopping
202     // after (4 - BlockSize). If we can't skip them all, stop.
203     unsigned ElseInstCnt = 0;
204     MachineBasicBlock::instr_iterator VPNOTBlockEndIter = std::next(Iter);
205     if (!StepOverPredicatedInstrs(VPNOTBlockEndIter, EndIter, (4 - BlockSize),
206                                   ElseInstCnt))
207       break;
208 
209     // Check if this VPNOT can be removed or not: It can only be removed if at
210     // least one of the predicated instruction that follows it kills or sets
211     // VPR.
212     if (!IsVPRDefinedOrKilledByBlock(Iter, VPNOTBlockEndIter))
213       break;
214 
215     LLVM_DEBUG(dbgs() << "  removing VPNOT: "; Iter->dump(););
216 
217     // Record the new size of the block
218     BlockSize += ElseInstCnt;
219     assert(BlockSize <= 4 && "Block is too large!");
220 
221     // Record the VPNot to remove it later.
222     DeadInstructions.push_back(&*Iter);
223     ++Iter;
224 
225     // Replace the predicates of the instructions we're adding.
226     // Note that we are using "Iter" to iterate over the block so we can update
227     // it at the same time.
228     for (; Iter != VPNOTBlockEndIter; ++Iter) {
229       // Find the register in which the predicate is
230       int OpIdx = findFirstVPTPredOperandIdx(*Iter);
231       assert(OpIdx != -1);
232 
233       // Change the predicate and update the mask
234       Iter->getOperand(OpIdx).setImm(CurrentPredicate);
235       BlockMask = ExpandBlockMask(BlockMask, CurrentPredicate);
236 
237       LLVM_DEBUG(dbgs() << "  adding : "; Iter->dump());
238     }
239 
240     CurrentPredicate =
241         (CurrentPredicate == ARMVCC::Then ? ARMVCC::Else : ARMVCC::Then);
242   }
243   return BlockMask;
244 }
245 
246 bool MVEVPTBlock::InsertVPTBlocks(MachineBasicBlock &Block) {
247   bool Modified = false;
248   MachineBasicBlock::instr_iterator MBIter = Block.instr_begin();
249   MachineBasicBlock::instr_iterator EndIter = Block.instr_end();
250 
251   SmallVector<MachineInstr *, 4> DeadInstructions;
252 
253   while (MBIter != EndIter) {
254     MachineInstr *MI = &*MBIter;
255     Register PredReg;
256     DebugLoc DL = MI->getDebugLoc();
257 
258     ARMVCC::VPTCodes Pred = getVPTInstrPredicate(*MI, PredReg);
259 
260     // The idea of the predicate is that None, Then and Else are for use when
261     // handling assembly language: they correspond to the three possible
262     // suffixes "", "t" and "e" on the mnemonic. So when instructions are read
263     // from assembly source or disassembled from object code, you expect to
264     // see a mixture whenever there's a long VPT block. But in code
265     // generation, we hope we'll never generate an Else as input to this pass.
266     assert(Pred != ARMVCC::Else && "VPT block pass does not expect Else preds");
267 
268     if (Pred == ARMVCC::None) {
269       ++MBIter;
270       continue;
271     }
272 
273     ARM::PredBlockMask BlockMask =
274         CreateVPTBlock(MBIter, EndIter, DeadInstructions);
275 
276     // Search back for a VCMP that can be folded to create a VPT, or else
277     // create a VPST directly
278     MachineInstrBuilder MIBuilder;
279     unsigned NewOpcode;
280     LLVM_DEBUG(dbgs() << "  final block mask: " << (unsigned)BlockMask << "\n");
281     if (MachineInstr *VCMP = findVCMPToFoldIntoVPST(MI, TRI, NewOpcode)) {
282       LLVM_DEBUG(dbgs() << "  folding VCMP into VPST: "; VCMP->dump());
283       MIBuilder = BuildMI(Block, MI, DL, TII->get(NewOpcode));
284       MIBuilder.addImm((uint64_t)BlockMask);
285       MIBuilder.add(VCMP->getOperand(1));
286       MIBuilder.add(VCMP->getOperand(2));
287       MIBuilder.add(VCMP->getOperand(3));
288       VCMP->eraseFromParent();
289     } else {
290       MIBuilder = BuildMI(Block, MI, DL, TII->get(ARM::MVE_VPST));
291       MIBuilder.addImm((uint64_t)BlockMask);
292     }
293 
294     finalizeBundle(
295         Block, MachineBasicBlock::instr_iterator(MIBuilder.getInstr()), MBIter);
296 
297     Modified = true;
298   }
299 
300   // Erase all dead instructions
301   for (MachineInstr *DeadMI : DeadInstructions) {
302     if (DeadMI->isInsideBundle())
303       DeadMI->eraseFromBundle();
304     else
305       DeadMI->eraseFromParent();
306   }
307 
308   return Modified;
309 }
310 
311 bool MVEVPTBlock::runOnMachineFunction(MachineFunction &Fn) {
312   const ARMSubtarget &STI =
313       static_cast<const ARMSubtarget &>(Fn.getSubtarget());
314 
315   if (!STI.isThumb2() || !STI.hasMVEIntegerOps())
316     return false;
317 
318   TII = static_cast<const Thumb2InstrInfo *>(STI.getInstrInfo());
319   TRI = STI.getRegisterInfo();
320 
321   LLVM_DEBUG(dbgs() << "********** ARM MVE VPT BLOCKS **********\n"
322                     << "********** Function: " << Fn.getName() << '\n');
323 
324   bool Modified = false;
325   for (MachineBasicBlock &MBB : Fn)
326     Modified |= InsertVPTBlocks(MBB);
327 
328   LLVM_DEBUG(dbgs() << "**************************************\n");
329   return Modified;
330 }
331 
332 /// createMVEVPTBlock - Returns an instance of the MVE VPT block
333 /// insertion pass.
334 FunctionPass *llvm::createMVEVPTBlockPass() { return new MVEVPTBlock(); }
335