LLVM 24.0.0git
MVETPAndVPTOptimisationsPass.cpp
Go to the documentation of this file.
1//===-- MVETPAndVPTOptimisationsPass.cpp ----------------------------------===//
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/// \file This pass does a few optimisations related to Tail predicated loops
10/// and MVE VPT blocks before register allocation is performed. For VPT blocks
11/// the goal is to maximize the sizes of the blocks that will be created by the
12/// MVE VPT Block Insertion pass (which runs after register allocation). For
13/// tail predicated loops we transform the loop into something that will
14/// hopefully make the backend ARMLowOverheadLoops pass's job easier.
15///
16//===----------------------------------------------------------------------===//
17
18#include "ARM.h"
19#include "ARMSubtarget.h"
20#include "MVETailPredUtils.h"
21#include "Thumb2InstrInfo.h"
31#include "llvm/Support/Debug.h"
32#include <cassert>
33
34using namespace llvm;
35
36#define DEBUG_TYPE "arm-mve-vpt-opts"
37
38static cl::opt<bool>
39MergeEndDec("arm-enable-merge-loopenddec", cl::Hidden,
40 cl::desc("Enable merging Loop End and Dec instructions."),
41 cl::init(true));
42
43static cl::opt<bool>
44SetLRPredicate("arm-set-lr-predicate", cl::Hidden,
45 cl::desc("Enable setting lr as a predicate in tail predication regions."),
46 cl::init(true));
47
48namespace {
49class MVETPAndVPTOptimisations : public MachineFunctionPass {
50public:
51 static char ID;
52 const Thumb2InstrInfo *TII;
54
55 MVETPAndVPTOptimisations() : MachineFunctionPass(ID) {}
56
57 bool runOnMachineFunction(MachineFunction &Fn) override;
58
59 void getAnalysisUsage(AnalysisUsage &AU) const override {
60 AU.addRequired<MachineLoopInfoWrapperPass>();
61 AU.addPreserved<MachineLoopInfoWrapperPass>();
62 AU.addRequired<MachineDominatorTreeWrapperPass>();
63 AU.addPreserved<MachineDominatorTreeWrapperPass>();
64 AU.addPreserved<MachineRegisterClassInfoWrapperPass>();
66 }
67
68 StringRef getPassName() const override {
69 return "ARM MVE TailPred and VPT Optimisation Pass";
70 }
71
72private:
73 bool LowerWhileLoopStart(MachineLoop *ML);
74 bool MergeLoopEnd(MachineLoop *ML);
75 bool ConvertTailPredLoop(MachineLoop *ML, MachineDominatorTree *DT);
76 MachineInstr &ReplaceRegisterUseWithVPNOT(MachineBasicBlock &MBB,
77 MachineInstr &Instr,
78 MachineOperand &User,
79 Register Target);
80 bool ReduceOldVCCRValueUses(MachineBasicBlock &MBB);
81 bool ReplaceVCMPsByVPNOTs(MachineBasicBlock &MBB);
82 bool ReplaceConstByVPNOTs(MachineBasicBlock &MBB, MachineDominatorTree *DT);
83 bool ConvertVPSEL(MachineBasicBlock &MBB);
84 bool HintDoLoopStartReg(MachineBasicBlock &MBB);
85 MachineInstr *CheckForLRUseInPredecessors(MachineBasicBlock *PreHeader,
86 MachineInstr *LoopStart);
87};
88
89char MVETPAndVPTOptimisations::ID = 0;
90
91} // end anonymous namespace
92
93INITIALIZE_PASS_BEGIN(MVETPAndVPTOptimisations, DEBUG_TYPE,
94 "ARM MVE TailPred and VPT Optimisations pass", false,
95 false)
98INITIALIZE_PASS_END(MVETPAndVPTOptimisations, DEBUG_TYPE,
99 "ARM MVE TailPred and VPT Optimisations pass", false, false)
100
103 while (MI && MI->getOpcode() == TargetOpcode::COPY &&
104 MI->getOperand(1).getReg().isVirtual())
105 MI = MRI->getVRegDef(MI->getOperand(1).getReg());
106 return MI;
107}
108
109// Given a loop ML, this attempts to find the t2LoopEnd, t2LoopDec and
110// corresponding PHI that make up a low overhead loop. Only handles 'do' loops
111// at the moment, returning a t2DoLoopStart in LoopStart.
113 MachineInstr *&LoopStart, MachineInstr *&LoopPhi,
114 MachineInstr *&LoopDec, MachineInstr *&LoopEnd) {
115 MachineBasicBlock *Header = ML->getHeader();
116 MachineBasicBlock *Latch = ML->getLoopLatch();
117 if (!Header || !Latch) {
118 LLVM_DEBUG(dbgs() << " no Loop Latch or Header\n");
119 return false;
120 }
121
122 // Find the loop end from the terminators.
123 LoopEnd = nullptr;
124 for (auto &T : Latch->terminators()) {
125 if (T.getOpcode() == ARM::t2LoopEnd && T.getOperand(1).getMBB() == Header) {
126 LoopEnd = &T;
127 break;
128 }
129 if (T.getOpcode() == ARM::t2LoopEndDec &&
130 T.getOperand(2).getMBB() == Header) {
131 LoopEnd = &T;
132 break;
133 }
134 }
135 if (!LoopEnd) {
136 LLVM_DEBUG(dbgs() << " no LoopEnd\n");
137 return false;
138 }
139 LLVM_DEBUG(dbgs() << " found loop end: " << *LoopEnd);
140
141 // Find the dec from the use of the end. There may be copies between
142 // instructions. We expect the loop to loop like:
143 // $vs = t2DoLoopStart ...
144 // loop:
145 // $vp = phi [ $vs ], [ $vd ]
146 // ...
147 // $vd = t2LoopDec $vp
148 // ...
149 // t2LoopEnd $vd, loop
150 if (LoopEnd->getOpcode() == ARM::t2LoopEndDec)
151 LoopDec = LoopEnd;
152 else {
153 LoopDec =
154 LookThroughCOPY(MRI->getVRegDef(LoopEnd->getOperand(0).getReg()), MRI);
155 if (!LoopDec || LoopDec->getOpcode() != ARM::t2LoopDec) {
156 LLVM_DEBUG(dbgs() << " didn't find LoopDec where we expected!\n");
157 return false;
158 }
159 }
160 LLVM_DEBUG(dbgs() << " found loop dec: " << *LoopDec);
161
162 LoopPhi =
163 LookThroughCOPY(MRI->getVRegDef(LoopDec->getOperand(1).getReg()), MRI);
164 if (!LoopPhi || LoopPhi->getOpcode() != TargetOpcode::PHI ||
165 LoopPhi->getNumOperands() != 5 ||
166 (LoopPhi->getOperand(2).getMBB() != Latch &&
167 LoopPhi->getOperand(4).getMBB() != Latch)) {
168 LLVM_DEBUG(dbgs() << " didn't find PHI where we expected!\n");
169 return false;
170 }
171 LLVM_DEBUG(dbgs() << " found loop phi: " << *LoopPhi);
172
173 Register StartReg = LoopPhi->getOperand(2).getMBB() == Latch
174 ? LoopPhi->getOperand(3).getReg()
175 : LoopPhi->getOperand(1).getReg();
176 LoopStart = LookThroughCOPY(MRI->getVRegDef(StartReg), MRI);
177 if (!LoopStart || (LoopStart->getOpcode() != ARM::t2DoLoopStart &&
178 LoopStart->getOpcode() != ARM::t2WhileLoopSetup &&
179 LoopStart->getOpcode() != ARM::t2WhileLoopStartLR)) {
180 LLVM_DEBUG(dbgs() << " didn't find Start where we expected!\n");
181 return false;
182 }
183 LLVM_DEBUG(dbgs() << " found loop start: " << *LoopStart);
184
185 return true;
186}
187
189 MachineBasicBlock *MBB = MI->getParent();
190 assert(MI->getOpcode() == ARM::t2WhileLoopSetup &&
191 "Only expected a t2WhileLoopSetup in RevertWhileLoopStart!");
192
193 // Subs
195 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(ARM::t2SUBri));
196 MIB.add(MI->getOperand(0));
197 MIB.add(MI->getOperand(1));
198 MIB.addImm(0);
199 MIB.addImm(ARMCC::AL);
200 MIB.addReg(Register());
201 MIB.addReg(ARM::CPSR, RegState::Define);
202
203 // Attempt to find a t2WhileLoopStart and revert to a t2Bcc.
204 for (MachineInstr &I : MBB->terminators()) {
205 if (I.getOpcode() == ARM::t2WhileLoopStart) {
207 BuildMI(*MBB, &I, I.getDebugLoc(), TII->get(ARM::t2Bcc));
208 MIB.add(MI->getOperand(1)); // branch target
209 MIB.addImm(ARMCC::EQ);
210 MIB.addReg(ARM::CPSR);
211 I.eraseFromParent();
212 break;
213 }
214 }
215
216 MI->eraseFromParent();
217}
218
219// The Hardware Loop insertion and ISel Lowering produce the pseudos for the
220// start of a while loop:
221// %a:gprlr = t2WhileLoopSetup %Cnt
222// t2WhileLoopStart %a, %BB
223// We want to convert those to a single instruction which, like t2LoopEndDec and
224// t2DoLoopStartTP is both a terminator and produces a value:
225// %a:grplr: t2WhileLoopStartLR %Cnt, %BB
226//
227// Otherwise if we can't, we revert the loop. t2WhileLoopSetup and
228// t2WhileLoopStart are not valid past regalloc.
229bool MVETPAndVPTOptimisations::LowerWhileLoopStart(MachineLoop *ML) {
230 LLVM_DEBUG(dbgs() << "LowerWhileLoopStart on loop "
231 << ML->getHeader()->getName() << "\n");
232
233 MachineInstr *LoopEnd, *LoopPhi, *LoopStart, *LoopDec;
234 if (!findLoopComponents(ML, MRI, LoopStart, LoopPhi, LoopDec, LoopEnd))
235 return false;
236
237 if (LoopStart->getOpcode() != ARM::t2WhileLoopSetup)
238 return false;
239
240 Register LR = LoopStart->getOperand(0).getReg();
241 auto WLSIt = find_if(MRI->use_nodbg_instructions(LR), [](auto &MI) {
242 return MI.getOpcode() == ARM::t2WhileLoopStart;
243 });
244 if (!MergeEndDec || WLSIt == MRI->use_instr_nodbg_end()) {
245 RevertWhileLoopSetup(LoopStart, TII);
246 RevertLoopDec(LoopStart, TII);
247 RevertLoopEnd(LoopStart, TII);
248 return true;
249 }
250
251 MachineInstrBuilder MI =
252 BuildMI(*WLSIt->getParent(), *WLSIt, WLSIt->getDebugLoc(),
253 TII->get(ARM::t2WhileLoopStartLR), LR)
254 .add(LoopStart->getOperand(1))
255 .add(WLSIt->getOperand(1))
256 .setOperandDead(3); // implicit-def $cpsr
257 (void)MI;
258 LLVM_DEBUG(dbgs() << "Lowered WhileLoopStart into: " << *MI.getInstr());
259
260 WLSIt->eraseFromParent();
261 LoopStart->eraseFromParent();
262 return true;
263}
264
265// Return true if this instruction is invalid in a low overhead loop, usually
266// because it clobbers LR.
268 return MI.isCall() || isLoopStart(MI);
269}
270
271// Starting from PreHeader, search for invalid instructions back until the
272// LoopStart block is reached. If invalid instructions are found, the loop start
273// is reverted from a WhileLoopStart to a DoLoopStart on the same loop. Will
274// return the new DLS LoopStart if updated.
275MachineInstr *MVETPAndVPTOptimisations::CheckForLRUseInPredecessors(
276 MachineBasicBlock *PreHeader, MachineInstr *LoopStart) {
278 SmallPtrSet<MachineBasicBlock *, 4> Visited;
279 Worklist.push_back(PreHeader);
280 Visited.insert(LoopStart->getParent());
281
282 while (!Worklist.empty()) {
283 MachineBasicBlock *MBB = Worklist.pop_back_val();
284 if (Visited.count(MBB))
285 continue;
286
287 for (MachineInstr &MI : *MBB) {
289 continue;
290
291 LLVM_DEBUG(dbgs() << "Found LR use in predecessors, reverting: " << MI);
292 Register DefReg = LoopStart->getOperand(0).getReg();
293 Register SrcReg = LoopStart->getOperand(1).getReg();
294
295 // Create a t2DoLoopStart at the end of the preheader.
296 MachineInstrBuilder MIB =
297 BuildMI(*PreHeader, PreHeader->getFirstTerminator(),
298 LoopStart->getDebugLoc(), TII->get(ARM::t2DoLoopStart));
299 MIB.add(LoopStart->getOperand(0));
300 MIB.add(LoopStart->getOperand(1));
301
302 // Make sure to remove the kill flags, to prevent them from being invalid.
303 LoopStart->getOperand(1).setIsKill(false);
304
305 // Revert the t2WhileLoopStartLR to a CMP and Br.
306 RevertWhileLoopStartLR(LoopStart, TII, ARM::t2Bcc, true);
307 // If we looked through a COPY to find the WhileLoopStart, replace it's
308 // use with SrcReg.
309 for (MachineOperand &MO :
311 if (MO.getParent()->getOpcode() == TargetOpcode::COPY)
312 MO.setReg(SrcReg);
313 }
314 return MIB;
315 }
316
317 Visited.insert(MBB);
318 llvm::append_range(Worklist, MBB->predecessors());
319 }
320 return LoopStart;
321}
322
323// This function converts loops with t2LoopEnd and t2LoopEnd instructions into
324// a single t2LoopEndDec instruction. To do that it needs to make sure that LR
325// will be valid to be used for the low overhead loop, which means nothing else
326// is using LR (especially calls) and there are no superfluous copies in the
327// loop. The t2LoopEndDec is a branching terminator that produces a value (the
328// decrement) around the loop edge, which means we need to be careful that they
329// will be valid to allocate without any spilling.
330bool MVETPAndVPTOptimisations::MergeLoopEnd(MachineLoop *ML) {
331 if (!MergeEndDec)
332 return false;
333
334 LLVM_DEBUG(dbgs() << "MergeLoopEnd on loop " << ML->getHeader()->getName()
335 << "\n");
336
337 MachineInstr *LoopEnd, *LoopPhi, *LoopStart, *LoopDec;
338 if (!findLoopComponents(ML, MRI, LoopStart, LoopPhi, LoopDec, LoopEnd))
339 return false;
340
341 // Check if there is an illegal instruction (a call) in the low overhead loop
342 // and if so revert it now before we get any further. While loops also need to
343 // check the preheaders, but can be reverted to a DLS loop if needed.
344 auto *PreHeader = ML->getLoopPreheader();
345 if (LoopStart->getOpcode() == ARM::t2WhileLoopStartLR && PreHeader)
346 LoopStart = CheckForLRUseInPredecessors(PreHeader, LoopStart);
347
348 for (MachineBasicBlock *MBB : ML->blocks()) {
349 for (MachineInstr &MI : *MBB) {
351 LLVM_DEBUG(dbgs() << "Found LR use in loop, reverting: " << MI);
352 if (LoopStart->getOpcode() == ARM::t2DoLoopStart)
353 RevertDoLoopStart(LoopStart, TII);
354 else
355 RevertWhileLoopStartLR(LoopStart, TII);
356 RevertLoopDec(LoopDec, TII);
357 RevertLoopEnd(LoopEnd, TII);
358 return true;
359 }
360 }
361 }
362
363 // Remove any copies from the loop, to ensure the phi that remains is both
364 // simpler and contains no extra uses. Because t2LoopEndDec is a terminator
365 // that cannot spill, we need to be careful what remains in the loop.
366 Register PhiReg = LoopPhi->getOperand(0).getReg();
367 Register DecReg = LoopDec->getOperand(0).getReg();
368 Register StartReg = LoopStart->getOperand(0).getReg();
369 // Ensure the uses are expected, and collect any copies we want to remove.
370 SmallVector<MachineInstr *, 4> Copies;
371 auto CheckUsers = [&Copies](Register BaseReg,
372 ArrayRef<MachineInstr *> ExpectedUsers,
373 MachineRegisterInfo *MRI) {
375 Worklist.push_back(BaseReg);
376 while (!Worklist.empty()) {
377 Register Reg = Worklist.pop_back_val();
378 for (MachineInstr &MI : MRI->use_nodbg_instructions(Reg)) {
379 if (llvm::is_contained(ExpectedUsers, &MI))
380 continue;
381 if (MI.getOpcode() != TargetOpcode::COPY ||
382 !MI.getOperand(0).getReg().isVirtual()) {
383 LLVM_DEBUG(dbgs() << "Extra users of register found: " << MI);
384 return false;
385 }
386 Worklist.push_back(MI.getOperand(0).getReg());
387 Copies.push_back(&MI);
388 }
389 }
390 return true;
391 };
392 if (!CheckUsers(PhiReg, {LoopDec}, MRI) ||
393 !CheckUsers(DecReg, {LoopPhi, LoopEnd}, MRI) ||
394 !CheckUsers(StartReg, {LoopPhi}, MRI)) {
395 // Don't leave a t2WhileLoopStartLR without the LoopDecEnd.
396 if (LoopStart->getOpcode() == ARM::t2WhileLoopStartLR) {
397 RevertWhileLoopStartLR(LoopStart, TII);
398 RevertLoopDec(LoopDec, TII);
399 RevertLoopEnd(LoopEnd, TII);
400 return true;
401 }
402 return false;
403 }
404
405 MRI->constrainRegClass(StartReg, &ARM::GPRlrRegClass);
406 MRI->constrainRegClass(PhiReg, &ARM::GPRlrRegClass);
407 MRI->constrainRegClass(DecReg, &ARM::GPRlrRegClass);
408
409 if (LoopPhi->getOperand(2).getMBB() == ML->getLoopLatch()) {
410 LoopPhi->getOperand(3).setReg(StartReg);
411 LoopPhi->getOperand(1).setReg(DecReg);
412 } else {
413 LoopPhi->getOperand(1).setReg(StartReg);
414 LoopPhi->getOperand(3).setReg(DecReg);
415 }
416
417 SmallVector<MachineOperand, 4> Cond; // For analyzeBranch.
418 MachineBasicBlock *TBB = nullptr, *FBB = nullptr; // For analyzeBranch.
419 if (!TII->analyzeBranch(*LoopEnd->getParent(), TBB, FBB, Cond) && !FBB) {
420 // If the LoopEnd falls through, need to insert a t2B to the fall-through
421 // block so that the non-analyzable t2LoopEndDec doesn't fall through.
423 BuildMI(LoopEnd->getParent(), DebugLoc(), TII->get(ARM::t2B))
424 .addMBB(&*MBBI)
426 }
427
428 // Replace the loop dec and loop end as a single instruction.
429 MachineInstrBuilder MI =
430 BuildMI(*LoopEnd->getParent(), *LoopEnd, LoopEnd->getDebugLoc(),
431 TII->get(ARM::t2LoopEndDec), DecReg)
432 .addReg(PhiReg)
433 .add(LoopEnd->getOperand(1))
434 .setOperandDead(3); // implicit-def $cpsr
435 (void)MI;
436 LLVM_DEBUG(dbgs() << "Merged LoopDec and End into: " << *MI.getInstr());
437
438 LoopDec->eraseFromParent();
439 LoopEnd->eraseFromParent();
440 for (auto *MI : Copies)
441 MI->eraseFromParent();
442 return true;
443}
444
445// Convert t2DoLoopStart to t2DoLoopStartTP if the loop contains VCTP
446// instructions. This keeps the VCTP count reg operand on the t2DoLoopStartTP
447// instruction, making the backend ARMLowOverheadLoops passes job of finding the
448// VCTP operand much simpler.
449bool MVETPAndVPTOptimisations::ConvertTailPredLoop(MachineLoop *ML,
450 MachineDominatorTree *DT) {
451 LLVM_DEBUG(dbgs() << "ConvertTailPredLoop on loop "
452 << ML->getHeader()->getName() << "\n");
453
454 // Find some loop components including the LoopEnd/Dec/Start, and any VCTP's
455 // in the loop.
456 MachineInstr *LoopEnd, *LoopPhi, *LoopStart, *LoopDec;
457 if (!findLoopComponents(ML, MRI, LoopStart, LoopPhi, LoopDec, LoopEnd))
458 return false;
459 if (LoopDec != LoopEnd || (LoopStart->getOpcode() != ARM::t2DoLoopStart &&
460 LoopStart->getOpcode() != ARM::t2WhileLoopStartLR))
461 return false;
462
463 SmallVector<MachineInstr *, 4> VCTPs;
464 SmallVector<MachineInstr *, 4> MVEInstrs;
465 for (MachineBasicBlock *BB : ML->blocks()) {
466 for (MachineInstr &MI : *BB)
467 if (isVCTP(&MI))
468 VCTPs.push_back(&MI);
469 else if (findFirstVPTPredOperandIdx(MI) != -1)
470 MVEInstrs.push_back(&MI);
471 }
472
473 if (VCTPs.empty()) {
474 LLVM_DEBUG(dbgs() << " no VCTPs\n");
475 return false;
476 }
477
478 // Check all VCTPs are the same.
479 MachineInstr *FirstVCTP = *VCTPs.begin();
480 for (MachineInstr *VCTP : VCTPs) {
481 LLVM_DEBUG(dbgs() << " with VCTP " << *VCTP);
482 if (VCTP->getOpcode() != FirstVCTP->getOpcode() ||
483 VCTP->getOperand(0).getReg() != FirstVCTP->getOperand(0).getReg()) {
484 LLVM_DEBUG(dbgs() << " VCTP's are not identical\n");
485 return false;
486 }
487 }
488
489 // Check for the register being used can be setup before the loop. We expect
490 // this to be:
491 // $vx = ...
492 // loop:
493 // $vp = PHI [ $vx ], [ $vd ]
494 // ..
495 // $vpr = VCTP $vp
496 // ..
497 // $vd = t2SUBri $vp, #n
498 // ..
499 Register CountReg = FirstVCTP->getOperand(1).getReg();
500 if (!CountReg.isVirtual()) {
501 LLVM_DEBUG(dbgs() << " cannot determine VCTP PHI\n");
502 return false;
503 }
504 MachineInstr *Phi = LookThroughCOPY(MRI->getVRegDef(CountReg), MRI);
505 if (!Phi || Phi->getOpcode() != TargetOpcode::PHI ||
506 Phi->getNumOperands() != 5 ||
507 (Phi->getOperand(2).getMBB() != ML->getLoopLatch() &&
508 Phi->getOperand(4).getMBB() != ML->getLoopLatch())) {
509 LLVM_DEBUG(dbgs() << " cannot determine VCTP Count\n");
510 return false;
511 }
512 CountReg = Phi->getOperand(2).getMBB() == ML->getLoopLatch()
513 ? Phi->getOperand(3).getReg()
514 : Phi->getOperand(1).getReg();
515
516 // Replace the t2DoLoopStart with the t2DoLoopStartTP, move it to the end of
517 // the preheader and add the new CountReg to it. We attempt to place it late
518 // in the preheader, but may need to move that earlier based on uses.
519 MachineBasicBlock *MBB = LoopStart->getParent();
521 for (MachineInstr &Use :
522 MRI->use_instructions(LoopStart->getOperand(0).getReg()))
523 if ((InsertPt != MBB->end() && !DT->dominates(&*InsertPt, &Use)) ||
524 !DT->dominates(ML->getHeader(), Use.getParent())) {
525 LLVM_DEBUG(dbgs() << " InsertPt could not be a terminator!\n");
526 return false;
527 }
528
529 unsigned NewOpc = LoopStart->getOpcode() == ARM::t2DoLoopStart
530 ? ARM::t2DoLoopStartTP
531 : ARM::t2WhileLoopStartTP;
532 MachineInstrBuilder MI =
533 BuildMI(*MBB, InsertPt, LoopStart->getDebugLoc(), TII->get(NewOpc))
534 .add(LoopStart->getOperand(0))
535 .add(LoopStart->getOperand(1))
536 .addReg(CountReg);
537 if (NewOpc == ARM::t2WhileLoopStartTP)
538 MI.add(LoopStart->getOperand(2)).setOperandDead(4); // implicit-def $cpsr
539 LLVM_DEBUG(dbgs() << "Replacing " << *LoopStart << " with "
540 << *MI.getInstr());
541 MRI->constrainRegClass(CountReg, &ARM::rGPRRegClass);
542 LoopStart->eraseFromParent();
543
544 if (SetLRPredicate) {
545 // Each instruction in the loop needs to be using LR as the predicate from
546 // the Phi as the predicate.
547 Register LR = LoopPhi->getOperand(0).getReg();
548 for (MachineInstr *MI : MVEInstrs) {
550 MI->getOperand(Idx + ARM::SUBOP_vpred_n_tp_reg).setReg(LR);
551 }
552 }
553
554 return true;
555}
556
557// Returns true if Opcode is any VCMP Opcode.
558static bool IsVCMP(unsigned Opcode) { return VCMPOpcodeToVPT(Opcode) != 0; }
559
560// Returns true if a VCMP with this Opcode can have its operands swapped.
561// There is 2 kind of VCMP that can't have their operands swapped: Float VCMPs,
562// and VCMPr instructions (since the r is always on the right).
563static bool CanHaveSwappedOperands(unsigned Opcode) {
564 switch (Opcode) {
565 default:
566 return true;
567 case ARM::MVE_VCMPf32:
568 case ARM::MVE_VCMPf16:
569 case ARM::MVE_VCMPf32r:
570 case ARM::MVE_VCMPf16r:
571 case ARM::MVE_VCMPi8r:
572 case ARM::MVE_VCMPi16r:
573 case ARM::MVE_VCMPi32r:
574 case ARM::MVE_VCMPu8r:
575 case ARM::MVE_VCMPu16r:
576 case ARM::MVE_VCMPu32r:
577 case ARM::MVE_VCMPs8r:
578 case ARM::MVE_VCMPs16r:
579 case ARM::MVE_VCMPs32r:
580 return false;
581 }
582}
583
584// Returns the CondCode of a VCMP Instruction.
586 assert(IsVCMP(Instr.getOpcode()) && "Inst must be a VCMP");
587 return ARMCC::CondCodes(Instr.getOperand(3).getImm());
588}
589
590// Returns true if Cond is equivalent to a VPNOT instruction on the result of
591// Prev. Cond and Prev must be VCMPs.
593 assert(IsVCMP(Cond.getOpcode()) && IsVCMP(Prev.getOpcode()));
594
595 // Opcodes must match.
596 if (Cond.getOpcode() != Prev.getOpcode())
597 return false;
598
599 MachineOperand &CondOP1 = Cond.getOperand(1), &CondOP2 = Cond.getOperand(2);
600 MachineOperand &PrevOP1 = Prev.getOperand(1), &PrevOP2 = Prev.getOperand(2);
601
602 // If the VCMP has the opposite condition with the same operands, we can
603 // replace it with a VPNOT
604 ARMCC::CondCodes ExpectedCode = GetCondCode(Cond);
605 ExpectedCode = ARMCC::getOppositeCondition(ExpectedCode);
606 if (ExpectedCode == GetCondCode(Prev))
607 if (CondOP1.isIdenticalTo(PrevOP1) && CondOP2.isIdenticalTo(PrevOP2))
608 return true;
609 // Check again with operands swapped if possible
610 if (!CanHaveSwappedOperands(Cond.getOpcode()))
611 return false;
612 ExpectedCode = ARMCC::getSwappedCondition(ExpectedCode);
613 return ExpectedCode == GetCondCode(Prev) && CondOP1.isIdenticalTo(PrevOP2) &&
614 CondOP2.isIdenticalTo(PrevOP1);
615}
616
617// Returns true if Instr writes to VCCR.
618static bool IsWritingToVCCR(MachineInstr &Instr) {
619 if (Instr.getNumOperands() == 0)
620 return false;
621 MachineOperand &Dst = Instr.getOperand(0);
622 if (!Dst.isReg())
623 return false;
624 Register DstReg = Dst.getReg();
625 if (!DstReg.isVirtual())
626 return false;
627 MachineRegisterInfo &RegInfo = Instr.getMF()->getRegInfo();
628 const TargetRegisterClass *RegClass = RegInfo.getRegClassOrNull(DstReg);
629 return RegClass && (RegClass->getID() == ARM::VCCRRegClassID);
630}
631
632// Transforms
633// <Instr that uses %A ('User' Operand)>
634// Into
635// %K = VPNOT %Target
636// <Instr that uses %K ('User' Operand)>
637// And returns the newly inserted VPNOT.
638// This optimization is done in the hopes of preventing spills/reloads of VPR by
639// reducing the number of VCCR values with overlapping lifetimes.
640MachineInstr &MVETPAndVPTOptimisations::ReplaceRegisterUseWithVPNOT(
641 MachineBasicBlock &MBB, MachineInstr &Instr, MachineOperand &User,
642 Register Target) {
643 Register NewResult = MRI->createVirtualRegister(MRI->getRegClass(Target));
644
645 MachineInstrBuilder MIBuilder =
646 BuildMI(MBB, &Instr, Instr.getDebugLoc(), TII->get(ARM::MVE_VPNOT))
647 .addDef(NewResult)
648 .addReg(Target);
650
651 // Make the user use NewResult instead, and clear its kill flag.
652 User.setReg(NewResult);
653 User.setIsKill(false);
654
655 LLVM_DEBUG(dbgs() << " Inserting VPNOT (for spill prevention): ";
656 MIBuilder.getInstr()->dump());
657
658 return *MIBuilder.getInstr();
659}
660
661// Moves a VPNOT before its first user if an instruction that uses Reg is found
662// in-between the VPNOT and its user.
663// Returns true if there is at least one user of the VPNOT in the block.
666 Register Reg) {
667 assert(Iter->getOpcode() == ARM::MVE_VPNOT && "Not a VPNOT!");
669 "The VPNOT cannot be predicated");
670
671 MachineInstr &VPNOT = *Iter;
672 Register VPNOTResult = VPNOT.getOperand(0).getReg();
673 Register VPNOTOperand = VPNOT.getOperand(1).getReg();
674
675 // Whether the VPNOT will need to be moved, and whether we found a user of the
676 // VPNOT.
677 bool MustMove = false, HasUser = false;
678 MachineOperand *VPNOTOperandKiller = nullptr;
679 for (; Iter != MBB.end(); ++Iter) {
680 if (MachineOperand *MO =
681 Iter->findRegisterUseOperand(VPNOTOperand, /*TRI=*/nullptr,
682 /*isKill*/ true)) {
683 // If we find the operand that kills the VPNOTOperand's result, save it.
684 VPNOTOperandKiller = MO;
685 }
686
687 if (Iter->findRegisterUseOperandIdx(Reg, /*TRI=*/nullptr) != -1) {
688 MustMove = true;
689 continue;
690 }
691
692 if (Iter->findRegisterUseOperandIdx(VPNOTResult, /*TRI=*/nullptr) == -1)
693 continue;
694
695 HasUser = true;
696 if (!MustMove)
697 break;
698
699 // Move the VPNOT right before Iter
700 LLVM_DEBUG(dbgs() << "Moving: "; VPNOT.dump(); dbgs() << " Before: ";
701 Iter->dump());
702 MBB.splice(Iter, &MBB, VPNOT.getIterator());
703 // If we move the instr, and its operand was killed earlier, remove the kill
704 // flag.
705 if (VPNOTOperandKiller)
706 VPNOTOperandKiller->setIsKill(false);
707
708 break;
709 }
710 return HasUser;
711}
712
713// This optimisation attempts to reduce the number of overlapping lifetimes of
714// VCCR values by replacing uses of old VCCR values with VPNOTs. For example,
715// this replaces
716// %A:vccr = (something)
717// %B:vccr = VPNOT %A
718// %Foo = (some op that uses %B)
719// %Bar = (some op that uses %A)
720// With
721// %A:vccr = (something)
722// %B:vccr = VPNOT %A
723// %Foo = (some op that uses %B)
724// %TMP2:vccr = VPNOT %B
725// %Bar = (some op that uses %A)
726bool MVETPAndVPTOptimisations::ReduceOldVCCRValueUses(MachineBasicBlock &MBB) {
728 SmallVector<MachineInstr *, 4> DeadInstructions;
729 bool Modified = false;
730
731 while (Iter != End) {
732 Register VCCRValue, OppositeVCCRValue;
733 // The first loop looks for 2 unpredicated instructions:
734 // %A:vccr = (instr) ; A is stored in VCCRValue
735 // %B:vccr = VPNOT %A ; B is stored in OppositeVCCRValue
736 for (; Iter != End; ++Iter) {
737 // We're only interested in unpredicated instructions that write to VCCR.
738 if (!IsWritingToVCCR(*Iter) ||
740 continue;
741 Register Dst = Iter->getOperand(0).getReg();
742
743 // If we already have a VCCRValue, and this is a VPNOT on VCCRValue, we've
744 // found what we were looking for.
745 if (VCCRValue && Iter->getOpcode() == ARM::MVE_VPNOT &&
746 Iter->findRegisterUseOperandIdx(VCCRValue, /*TRI=*/nullptr) != -1) {
747 // Move the VPNOT closer to its first user if needed, and ignore if it
748 // has no users.
749 if (!MoveVPNOTBeforeFirstUser(MBB, Iter, VCCRValue))
750 continue;
751
752 OppositeVCCRValue = Dst;
753 ++Iter;
754 break;
755 }
756
757 // Else, just set VCCRValue.
758 VCCRValue = Dst;
759 }
760
761 // If the first inner loop didn't find anything, stop here.
762 if (Iter == End)
763 break;
764
765 assert(VCCRValue && OppositeVCCRValue &&
766 "VCCRValue and OppositeVCCRValue shouldn't be empty if the loop "
767 "stopped before the end of the block!");
768 assert(VCCRValue != OppositeVCCRValue &&
769 "VCCRValue should not be equal to OppositeVCCRValue!");
770
771 // LastVPNOTResult always contains the same value as OppositeVCCRValue.
772 Register LastVPNOTResult = OppositeVCCRValue;
773
774 // This second loop tries to optimize the remaining instructions.
775 for (; Iter != End; ++Iter) {
776 bool IsInteresting = false;
777
778 if (MachineOperand *MO =
779 Iter->findRegisterUseOperand(VCCRValue, /*TRI=*/nullptr)) {
780 IsInteresting = true;
781
782 // - If the instruction is a VPNOT, it can be removed, and we can just
783 // replace its uses with LastVPNOTResult.
784 // - Else, insert a new VPNOT on LastVPNOTResult to recompute VCCRValue.
785 if (Iter->getOpcode() == ARM::MVE_VPNOT) {
786 Register Result = Iter->getOperand(0).getReg();
787
788 MRI->replaceRegWith(Result, LastVPNOTResult);
789 DeadInstructions.push_back(&*Iter);
790 Modified = true;
791
793 << "Replacing all uses of '" << printReg(Result)
794 << "' with '" << printReg(LastVPNOTResult) << "'\n");
795 } else {
796 MachineInstr &VPNOT =
797 ReplaceRegisterUseWithVPNOT(MBB, *Iter, *MO, LastVPNOTResult);
798 Modified = true;
799
800 LastVPNOTResult = VPNOT.getOperand(0).getReg();
801 std::swap(VCCRValue, OppositeVCCRValue);
802
803 LLVM_DEBUG(dbgs() << "Replacing use of '" << printReg(VCCRValue)
804 << "' with '" << printReg(LastVPNOTResult)
805 << "' in instr: " << *Iter);
806 }
807 } else {
808 // If the instr uses OppositeVCCRValue, make it use LastVPNOTResult
809 // instead as they contain the same value.
810 if (MachineOperand *MO = Iter->findRegisterUseOperand(
811 OppositeVCCRValue, /*TRI=*/nullptr)) {
812 IsInteresting = true;
813
814 // This is pointless if LastVPNOTResult == OppositeVCCRValue.
815 if (LastVPNOTResult != OppositeVCCRValue) {
816 LLVM_DEBUG(dbgs() << "Replacing usage of '"
817 << printReg(OppositeVCCRValue) << "' with '"
818 << printReg(LastVPNOTResult) << " for instr: ";
819 Iter->dump());
820 MO->setReg(LastVPNOTResult);
821 Modified = true;
822 }
823
824 MO->setIsKill(false);
825 }
826
827 // If this is an unpredicated VPNOT on
828 // LastVPNOTResult/OppositeVCCRValue, we can act like we inserted it.
829 if (Iter->getOpcode() == ARM::MVE_VPNOT &&
831 Register VPNOTOperand = Iter->getOperand(1).getReg();
832 if (VPNOTOperand == LastVPNOTResult ||
833 VPNOTOperand == OppositeVCCRValue) {
834 IsInteresting = true;
835
836 std::swap(VCCRValue, OppositeVCCRValue);
837 LastVPNOTResult = Iter->getOperand(0).getReg();
838 }
839 }
840 }
841
842 // If this instruction was not interesting, and it writes to VCCR, stop.
843 if (!IsInteresting && IsWritingToVCCR(*Iter))
844 break;
845 }
846 }
847
848 for (MachineInstr *DeadInstruction : DeadInstructions)
849 DeadInstruction->eraseFromParent();
850
851 return Modified;
852}
853
854// This optimisation replaces VCMPs with VPNOTs when they are equivalent.
855bool MVETPAndVPTOptimisations::ReplaceVCMPsByVPNOTs(MachineBasicBlock &MBB) {
856 SmallVector<MachineInstr *, 4> DeadInstructions;
857
858 // The last VCMP that we have seen and that couldn't be replaced.
859 // This is reset when an instruction that writes to VCCR/VPR is found, or when
860 // a VCMP is replaced with a VPNOT.
861 // We'll only replace VCMPs with VPNOTs when this is not null, and when the
862 // current VCMP is the opposite of PrevVCMP.
863 MachineInstr *PrevVCMP = nullptr;
864 // If we find an instruction that kills the result of PrevVCMP, we save the
865 // operand here to remove the kill flag in case we need to use PrevVCMP's
866 // result.
867 MachineOperand *PrevVCMPResultKiller = nullptr;
868
869 for (MachineInstr &Instr : MBB.instrs()) {
870 if (PrevVCMP) {
871 if (MachineOperand *MO =
872 Instr.findRegisterUseOperand(PrevVCMP->getOperand(0).getReg(),
873 /*TRI=*/nullptr, /*isKill*/ true)) {
874 // If we come across the instr that kills PrevVCMP's result, record it
875 // so we can remove the kill flag later if we need to.
876 PrevVCMPResultKiller = MO;
877 }
878 }
879
880 // Ignore predicated instructions.
882 continue;
883
884 // Only look at VCMPs
885 if (!IsVCMP(Instr.getOpcode())) {
886 // If the instruction writes to VCCR, forget the previous VCMP.
887 if (IsWritingToVCCR(Instr))
888 PrevVCMP = nullptr;
889 continue;
890 }
891
892 if (!PrevVCMP || !IsVPNOTEquivalent(Instr, *PrevVCMP)) {
893 PrevVCMP = &Instr;
894 continue;
895 }
896
897 // The register containing the result of the VCMP that we're going to
898 // replace.
899 Register PrevVCMPResultReg = PrevVCMP->getOperand(0).getReg();
900
901 // Build a VPNOT to replace the VCMP, reusing its operands.
902 MachineInstrBuilder MIBuilder =
903 BuildMI(MBB, &Instr, Instr.getDebugLoc(), TII->get(ARM::MVE_VPNOT))
904 .add(Instr.getOperand(0))
905 .addReg(PrevVCMPResultReg);
907 LLVM_DEBUG(dbgs() << "Inserting VPNOT (to replace VCMP): ";
908 MIBuilder.getInstr()->dump(); dbgs() << " Removed VCMP: ";
909 Instr.dump());
910
911 // If we found an instruction that uses, and kills PrevVCMP's result,
912 // remove the kill flag.
913 if (PrevVCMPResultKiller)
914 PrevVCMPResultKiller->setIsKill(false);
915
916 // Finally, mark the old VCMP for removal and reset
917 // PrevVCMP/PrevVCMPResultKiller.
918 DeadInstructions.push_back(&Instr);
919 PrevVCMP = nullptr;
920 PrevVCMPResultKiller = nullptr;
921 }
922
923 for (MachineInstr *DeadInstruction : DeadInstructions)
924 DeadInstruction->eraseFromParent();
925
926 return !DeadInstructions.empty();
927}
928
929bool MVETPAndVPTOptimisations::ReplaceConstByVPNOTs(MachineBasicBlock &MBB,
930 MachineDominatorTree *DT) {
931 // Scan through the block, looking for instructions that use constants moves
932 // into VPR that are the negative of one another. These are expected to be
933 // COPY's to VCCRRegClass, from a t2MOVi or t2MOVi16. The last seen constant
934 // mask is kept it or and VPNOT's of it are added or reused as we scan through
935 // the function.
936 unsigned LastVPTImm = 0;
937 Register LastVPTReg = 0;
938 SmallPtrSet<MachineInstr *, 4> DeadInstructions;
939
940 for (MachineInstr &Instr : MBB.instrs()) {
941 // Look for predicated MVE instructions.
942 int PIdx = llvm::findFirstVPTPredOperandIdx(Instr);
943 if (PIdx == -1)
944 continue;
945 Register VPR = Instr.getOperand(PIdx + 1).getReg();
946 if (!VPR.isVirtual())
947 continue;
948
949 // From that we are looking for an instruction like %11:vccr = COPY %9:rgpr.
950 MachineInstr *Copy = MRI->getVRegDef(VPR);
951 if (!Copy || Copy->getOpcode() != TargetOpcode::COPY ||
952 !Copy->getOperand(1).getReg().isVirtual() ||
953 MRI->getRegClass(Copy->getOperand(1).getReg()) == &ARM::VCCRRegClass) {
954 LastVPTReg = 0;
955 continue;
956 }
957 Register GPR = Copy->getOperand(1).getReg();
958
959 // Find the Immediate used by the copy.
960 auto getImm = [&](Register GPR) -> unsigned {
961 MachineInstr *Def = MRI->getVRegDef(GPR);
962 if (Def && (Def->getOpcode() == ARM::t2MOVi ||
963 Def->getOpcode() == ARM::t2MOVi16))
964 return Def->getOperand(1).getImm();
965 return -1U;
966 };
967 unsigned Imm = getImm(GPR);
968 if (Imm == -1U) {
969 LastVPTReg = 0;
970 continue;
971 }
972
973 unsigned NotImm = ~Imm & 0xffff;
974 if (LastVPTReg != 0 && LastVPTReg != VPR && LastVPTImm == Imm) {
975 MRI->clearKillFlags(LastVPTReg);
976 Instr.getOperand(PIdx + 1).setReg(LastVPTReg);
977 if (MRI->use_empty(VPR)) {
978 DeadInstructions.insert(Copy);
979 if (MRI->hasOneUse(GPR))
980 DeadInstructions.insert(MRI->getVRegDef(GPR));
981 }
982 LLVM_DEBUG(dbgs() << "Reusing predicate: in " << Instr);
983 VPR = LastVPTReg;
984 } else if (LastVPTReg != 0 && LastVPTImm == NotImm) {
985 // We have found the not of a previous constant. Create a VPNot of the
986 // earlier predicate reg and use it instead of the copy.
987 Register NewVPR = MRI->createVirtualRegister(&ARM::VCCRRegClass);
988 auto VPNot = BuildMI(MBB, &Instr, Instr.getDebugLoc(),
989 TII->get(ARM::MVE_VPNOT), NewVPR)
990 .addReg(LastVPTReg);
992
993 // Use the new register and check if the def is now dead.
994 Instr.getOperand(PIdx + 1).setReg(NewVPR);
995 if (MRI->use_empty(VPR)) {
996 DeadInstructions.insert(Copy);
997 if (MRI->hasOneUse(GPR))
998 DeadInstructions.insert(MRI->getVRegDef(GPR));
999 }
1000 MRI->clearKillFlags(LastVPTReg);
1001 LLVM_DEBUG(dbgs() << "Adding VPNot: " << *VPNot << " to replace use at "
1002 << Instr);
1003 VPR = NewVPR;
1004 }
1005
1006 LastVPTImm = Imm;
1007 LastVPTReg = VPR;
1008 }
1009
1010 for (MachineInstr *DI : DeadInstructions)
1011 DI->eraseFromParent();
1012
1013 return !DeadInstructions.empty();
1014}
1015
1016// Replace VPSEL with a predicated VMOV in blocks with a VCTP. This is a
1017// somewhat blunt approximation to allow tail predicated with vpsel
1018// instructions. We turn a vselect into a VPSEL in ISEL, but they have slightly
1019// different semantics under tail predication. Until that is modelled we just
1020// convert to a VMOVT (via a predicated VORR) instead.
1021bool MVETPAndVPTOptimisations::ConvertVPSEL(MachineBasicBlock &MBB) {
1022 bool HasVCTP = false;
1023 SmallVector<MachineInstr *, 4> DeadInstructions;
1024
1025 for (MachineInstr &MI : MBB.instrs()) {
1026 if (isVCTP(&MI)) {
1027 HasVCTP = true;
1028 continue;
1029 }
1030
1031 if (!HasVCTP || MI.getOpcode() != ARM::MVE_VPSEL)
1032 continue;
1033
1034 MachineInstrBuilder MIBuilder =
1035 BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(ARM::MVE_VORR))
1036 .add(MI.getOperand(0))
1037 .add(MI.getOperand(1))
1038 .add(MI.getOperand(1))
1040 .add(MI.getOperand(4))
1041 .add(MI.getOperand(5))
1042 .add(MI.getOperand(2));
1043 // Silence unused variable warning in release builds.
1044 (void)MIBuilder;
1045 LLVM_DEBUG(dbgs() << "Replacing VPSEL: "; MI.dump();
1046 dbgs() << " with VMOVT: "; MIBuilder.getInstr()->dump());
1047 DeadInstructions.push_back(&MI);
1048 }
1049
1050 for (MachineInstr *DeadInstruction : DeadInstructions)
1051 DeadInstruction->eraseFromParent();
1052
1053 return !DeadInstructions.empty();
1054}
1055
1056// Add a registry allocation hint for t2DoLoopStart to hint it towards LR, as
1057// the instruction may be removable as a noop.
1058bool MVETPAndVPTOptimisations::HintDoLoopStartReg(MachineBasicBlock &MBB) {
1059 bool Changed = false;
1060 for (MachineInstr &MI : MBB.instrs()) {
1061 if (MI.getOpcode() != ARM::t2DoLoopStart)
1062 continue;
1063 Register R = MI.getOperand(1).getReg();
1064 MachineFunction *MF = MI.getParent()->getParent();
1066 Changed = true;
1067 }
1068 return Changed;
1069}
1070
1071bool MVETPAndVPTOptimisations::runOnMachineFunction(MachineFunction &Fn) {
1072 const ARMSubtarget &STI = Fn.getSubtarget<ARMSubtarget>();
1073
1074 if (!STI.isThumb2() || !STI.hasLOB())
1075 return false;
1076
1077 TII = static_cast<const Thumb2InstrInfo *>(STI.getInstrInfo());
1078 MRI = &Fn.getRegInfo();
1079 MachineLoopInfo *MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
1080 MachineDominatorTree *DT =
1081 &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
1082
1083 LLVM_DEBUG(dbgs() << "********** ARM MVE VPT Optimisations **********\n"
1084 << "********** Function: " << Fn.getName() << '\n');
1085
1086 bool Modified = false;
1087 for (MachineLoop *ML : MLI->getLoopsInPreorder()) {
1088 Modified |= LowerWhileLoopStart(ML);
1089 Modified |= MergeLoopEnd(ML);
1090 Modified |= ConvertTailPredLoop(ML, DT);
1091 }
1092
1093 for (MachineBasicBlock &MBB : Fn) {
1094 Modified |= HintDoLoopStartReg(MBB);
1095 Modified |= ReplaceConstByVPNOTs(MBB, DT);
1096 Modified |= ReplaceVCMPsByVPNOTs(MBB);
1097 Modified |= ReduceOldVCCRValueUses(MBB);
1098 Modified |= ConvertVPSEL(MBB);
1099 }
1100
1101 LLVM_DEBUG(dbgs() << "**************************************\n");
1102 return Modified;
1103}
1104
1105/// createMVETPAndVPTOptimisationsPass
1107 return new MVETPAndVPTOptimisations();
1108}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t Imm
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator MBBI
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
ARM MVE TailPred and VPT Optimisations static false MachineInstr * LookThroughCOPY(MachineInstr *MI, MachineRegisterInfo *MRI)
static void RevertWhileLoopSetup(MachineInstr *MI, const TargetInstrInfo *TII)
static cl::opt< bool > SetLRPredicate("arm-set-lr-predicate", cl::Hidden, cl::desc("Enable setting lr as a predicate in tail predication regions."), cl::init(true))
static bool findLoopComponents(MachineLoop *ML, MachineRegisterInfo *MRI, MachineInstr *&LoopStart, MachineInstr *&LoopPhi, MachineInstr *&LoopDec, MachineInstr *&LoopEnd)
static bool MoveVPNOTBeforeFirstUser(MachineBasicBlock &MBB, MachineBasicBlock::iterator Iter, Register Reg)
static cl::opt< bool > MergeEndDec("arm-enable-merge-loopenddec", cl::Hidden, cl::desc("Enable merging Loop End and Dec instructions."), cl::init(true))
static ARMCC::CondCodes GetCondCode(MachineInstr &Instr)
static bool IsVPNOTEquivalent(MachineInstr &Cond, MachineInstr &Prev)
static bool IsInvalidTPInstruction(MachineInstr &MI)
static bool IsVCMP(unsigned Opcode)
static bool IsWritingToVCCR(MachineInstr &Instr)
static bool CanHaveSwappedOperands(unsigned Opcode)
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define T
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
SmallVector< MachineOperand, 4 > Cond
MachineRegisterInfo & MRI
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
SI Lower i1 Copies
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
const ARMBaseInstrInfo * getInstrInfo() const override
bool isThumb2() const
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const override
Analyze the branching code at the end of MBB, returning true if it cannot be understood (e....
SmallVector< LoopT *, 4 > getLoopsInPreorder() const
Return all of the loops in the function in preorder across the loop nests, with siblings in forward p...
unsigned getID() const
getID() - Return the register class ID number.
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
iterator_range< iterator > terminators()
iterator_range< pred_iterator > predecessors()
MachineInstrBundleIterator< MachineInstr > iterator
Analysis pass which computes a MachineDominatorTree.
bool dominates(const MachineInstr *A, const MachineInstr *B) const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
BasicBlockListType::iterator iterator
const MachineInstrBuilder & setOperandDead(unsigned OpIdx) const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
unsigned getNumOperands() const
Retuns the total number of operands.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
LLVM_ABI void dump() const
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
MachineOperand class - Representation of each machine instruction operand.
MachineBasicBlock * getMBB() const
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
void setIsKill(bool Val=true)
Register getReg() const
getReg - Returns the register number.
LLVM_ABI bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
LLVM_ABI void clearKillFlags(Register Reg) const
clearKillFlags - Iterate over all the uses of the given register and clear the kill flag from the Mac...
LLVM_ABI LLVM_READONLY MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
bool hasOneUse(Register RegNo) const
hasOneUse - Return true if there is exactly one instruction using the specified register.
iterator_range< use_instr_nodbg_iterator > use_nodbg_instructions(Register Reg) const
void setRegAllocationHint(Register VReg, unsigned Type, Register PrefReg)
setRegAllocationHint - Specify a register allocation hint for the specified virtual register.
iterator_range< use_instr_iterator > use_instructions(Register Reg) const
LLVM_ABI const TargetRegisterClass * constrainRegClass(Register Reg, const TargetRegisterClass *RC, unsigned MinNumRegs=0)
constrainRegClass - Constrain the register class of the specified virtual register to be a common sub...
iterator_range< use_iterator > use_operands(Register Reg) const
bool use_empty(Register RegNo) const
use_empty - Return true if there are no instructions using the specified register.
LLVM_ABI void replaceRegWith(Register FromReg, Register ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
static use_instr_nodbg_iterator use_instr_nodbg_end()
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
void push_back(const T &Elt)
TargetInstrInfo - Interface to description of machine instruction set.
self_iterator getIterator()
Definition ilist_node.h:123
static CondCodes getOppositeCondition(CondCodes CC)
Definition ARMBaseInfo.h:49
static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC)
getSwappedCondition - assume the flags are set by MI(a,b), return the condition code if we modify the...
Definition ARMBaseInfo.h:72
initializer< Ty > init(const Ty &Val)
@ User
could "use" a pointer
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
NodeAddr< InstrNode * > Instr
Definition RDFGraph.h:389
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:390
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
Definition SFrame.h:77
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
int findFirstVPTPredOperandIdx(const MachineInstr &MI)
FunctionPass * createMVETPAndVPTOptimisationsPass()
createMVETPAndVPTOptimisationsPass
ARMVCC::VPTCodes getVPTInstrPredicate(const MachineInstr &MI, Register &PredReg)
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
static bool isVCTP(const MachineInstr *MI)
@ Define
Register definition.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2224
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:649
static std::array< MachineOperand, 2 > predOps(ARMCC::CondCodes Pred, unsigned PredReg=0)
Get the operands corresponding to the given Pred value.
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
static bool isLoopStart(const MachineInstr &MI)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
void RevertWhileLoopStartLR(MachineInstr *MI, const TargetInstrInfo *TII, unsigned BrOpc=ARM::t2Bcc, bool UseCmp=false)
ArrayRef(const T &OneElt) -> ArrayRef< T >
void RevertLoopEnd(MachineInstr *MI, const TargetInstrInfo *TII, unsigned BrOpc=ARM::t2Bcc, bool SkipCmp=false)
void RevertLoopDec(MachineInstr *MI, const TargetInstrInfo *TII, bool SetFlags=false)
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1788
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
static unsigned VCMPOpcodeToVPT(unsigned Opcode)
void RevertDoLoopStart(MachineInstr *MI, const TargetInstrInfo *TII)
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
void addUnpredicatedMveVpredNOp(MachineInstrBuilder &MIB)
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880