LLVM 24.0.0git
HexagonNewValueJump.cpp
Go to the documentation of this file.
1//===- HexagonNewValueJump.cpp - Hexagon Backend New Value Jump -----------===//
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// This implements NewValueJump pass in Hexagon.
10// Ideally, we should merge this as a Peephole pass prior to register
11// allocation, but because we have a spill in between the feeder and new value
12// jump instructions, we are forced to write after register allocation.
13// Having said that, we should re-attempt to pull this earlier at some point
14// in future.
15
16// The basic approach looks for sequence of predicated jump, compare instruction
17// that generates the predicate and, the feeder to the predicate. Once it finds
18// all, it collapses compare and jump instruction into a new value jump
19// instructions.
20//
21//===----------------------------------------------------------------------===//
22
23#include "Hexagon.h"
24#include "HexagonInstrInfo.h"
25#include "HexagonRegisterInfo.h"
26#include "HexagonSubtarget.h"
29#include "llvm/ADT/Statistic.h"
41#include "llvm/IR/DebugLoc.h"
43#include "llvm/MC/MCInstrDesc.h"
44#include "llvm/Pass.h"
47#include "llvm/Support/Debug.h"
51#include <cassert>
52#include <cstdint>
53#include <iterator>
54
55using namespace llvm;
56
57#define DEBUG_TYPE "hexagon-nvj"
58
59STATISTIC(NumNVJGenerated, "Number of New Value Jump Instructions created");
60
62 cl::desc("Maximum number of predicated jumps to be converted to "
63 "New Value Jump"));
64
66 cl::desc("Disable New Value Jumps"));
67
69
70namespace {
71
72 struct HexagonNewValueJump : public MachineFunctionPass {
73 static char ID;
74
75 HexagonNewValueJump() : MachineFunctionPass(ID) {}
76
77 void getAnalysisUsage(AnalysisUsage &AU) const override {
78 AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
80 }
81
82 StringRef getPassName() const override { return "Hexagon NewValueJump"; }
83
84 bool runOnMachineFunction(MachineFunction &Fn) override;
85
86 MachineFunctionProperties getRequiredProperties() const override {
87 return MachineFunctionProperties().setNoVRegs();
88 }
89
90 private:
91 const HexagonInstrInfo *QII;
92 const HexagonRegisterInfo *QRI;
93
94 /// A handle to the branch probability pass.
95 const MachineBranchProbabilityInfo *MBPI;
96
97 bool isNewValueJumpCandidate(const MachineInstr &MI) const;
98 };
99
100} // end anonymous namespace
101
102char HexagonNewValueJump::ID = 0;
103
104INITIALIZE_PASS_BEGIN(HexagonNewValueJump, "hexagon-nvj",
105 "Hexagon NewValueJump", false, false)
107INITIALIZE_PASS_END(HexagonNewValueJump, "hexagon-nvj",
108 "Hexagon NewValueJump", false, false)
109
110// We have identified this II could be feeder to NVJ,
111// verify that it can be.
117 MachineFunction &MF) {
118 // Predicated instruction can not be feeder to NVJ.
119 if (QII->isPredicated(*II))
120 return false;
121
122 // Bail out if feederReg is a paired register (double regs in
123 // our case). One would think that we can check to see if a given
124 // register cmpReg1 or cmpReg2 is a sub register of feederReg
125 // using -- if (QRI->isSubRegister(feederReg, cmpReg1) logic
126 // before the callsite of this function
127 // But we can not as it comes in the following fashion.
128 // %d0 = Hexagon_S2_lsr_r_p killed %d0, killed %r2
129 // %r0 = KILL %r0, implicit killed %d0
130 // %p0 = CMPEQri killed %r0, 0
131 // Hence, we need to check if it's a KILL instruction.
132 if (II->getOpcode() == TargetOpcode::KILL)
133 return false;
134
135 if (II->isImplicitDef())
136 return false;
137
138 if (QII->isSolo(*II))
139 return false;
140
141 if (QII->isFloat(*II))
142 return false;
143
144 // Make sure that the (unique) def operand is a register from IntRegs.
145 bool HadDef = false;
146 for (const MachineOperand &Op : II->operands()) {
147 if (!Op.isReg() || !Op.isDef())
148 continue;
149 if (HadDef)
150 return false;
151 HadDef = true;
152 if (!Hexagon::IntRegsRegClass.contains(Op.getReg()))
153 return false;
154 }
155 assert(HadDef);
156
157 // Make sure there is no 'def' or 'use' of any of the uses of
158 // feeder insn between its definition, this MI and jump, jmpInst
159 // skipping compare, cmpInst.
160 // Here's the example.
161 // r21=memub(r22+r24<<#0)
162 // p0 = cmp.eq(r21, #0)
163 // r4=memub(r3+r21<<#0)
164 // if (p0.new) jump:t .LBB29_45
165 // Without this check, it will be converted into
166 // r4=memub(r3+r21<<#0)
167 // r21=memub(r22+r24<<#0)
168 // p0 = cmp.eq(r21, #0)
169 // if (p0.new) jump:t .LBB29_45
170 // and result WAR hazards if converted to New Value Jump.
171 for (unsigned i = 0; i < II->getNumOperands(); ++i) {
172 if (II->getOperand(i).isReg() &&
173 (II->getOperand(i).isUse() || II->getOperand(i).isDef())) {
175 ++localII;
176 Register Reg = II->getOperand(i).getReg();
177 for (MachineBasicBlock::iterator localBegin = localII; localBegin != end;
178 ++localBegin) {
179 if (localBegin == skip)
180 continue;
181 if (localBegin->isDebugInstr())
182 continue;
183 // Check for Subregisters too.
184 if (localBegin->modifiesRegister(Reg, TRI) ||
185 localBegin->readsRegister(Reg, TRI))
186 return false;
187 }
188 }
189 }
190 return true;
191}
192
193// These are the common checks that need to performed
194// to determine if
195// 1. compare instruction can be moved before jump.
196// 2. feeder to the compare instruction can be moved before jump.
197static bool commonChecksToProhibitNewValueJump(bool afterRA,
199 // If store in path, bail out.
200 if (MII->mayStore())
201 return false;
202
203 // if call in path, bail out.
204 if (MII->isCall())
205 return false;
206
207 // if NVJ is running prior to RA, do the following checks.
208 if (!afterRA) {
209 // The following Target Opcode instructions are spurious
210 // to new value jump. If they are in the path, bail out.
211 // KILL sets kill flag on the opcode. It also sets up a
212 // single register, out of pair.
213 // %d0 = S2_lsr_r_p killed %d0, killed %r2
214 // %r0 = KILL %r0, implicit killed %d0
215 // %p0 = C2_cmpeqi killed %r0, 0
216 // PHI can be anything after RA.
217 // COPY can remateriaze things in between feeder, compare and nvj.
218 if (MII->getOpcode() == TargetOpcode::KILL ||
219 MII->getOpcode() == TargetOpcode::PHI ||
220 MII->getOpcode() == TargetOpcode::COPY)
221 return false;
222
223 // The following pseudo Hexagon instructions sets "use" and "def"
224 // of registers by individual passes in the backend. At this time,
225 // we don't know the scope of usage and definitions of these
226 // instructions.
227 if (MII->getOpcode() == Hexagon::LDriw_pred ||
228 MII->getOpcode() == Hexagon::STriw_pred)
229 return false;
230 }
231
232 return true;
233}
234
236 const TargetRegisterInfo *TRI,
238 unsigned pReg,
239 bool secondReg,
240 bool optLocation,
242 MachineFunction &MF) {
243 MachineInstr &MI = *II;
244
245 // If the second operand of the compare is an imm, make sure it's in the
246 // range specified by the arch.
247 if (!secondReg) {
248 const MachineOperand &Op2 = MI.getOperand(2);
249 if (!Op2.isImm())
250 return false;
251
252 int64_t v = Op2.getImm();
253 bool Valid = false;
254
255 switch (MI.getOpcode()) {
256 case Hexagon::C2_cmpeqi:
257 case Hexagon::C4_cmpneqi:
258 case Hexagon::C2_cmpgti:
259 case Hexagon::C4_cmpltei:
260 Valid = (isUInt<5>(v) || v == -1);
261 break;
262 case Hexagon::C2_cmpgtui:
263 case Hexagon::C4_cmplteui:
264 Valid = isUInt<5>(v);
265 break;
266 case Hexagon::S2_tstbit_i:
267 case Hexagon::S4_ntstbit_i:
268 Valid = (v == 0);
269 break;
270 }
271
272 if (!Valid)
273 return false;
274 }
275
276 unsigned cmpReg1, cmpOp2 = 0; // cmpOp2 assignment silences compiler warning.
277 cmpReg1 = MI.getOperand(1).getReg();
278
279 if (secondReg) {
280 cmpOp2 = MI.getOperand(2).getReg();
281
282 // If the same register appears as both operands, we cannot generate a new
283 // value compare. Only one operand may use the .new suffix.
284 if (cmpReg1 == cmpOp2)
285 return false;
286
287 // Make sure that the second register is not from COPY
288 // at machine code level, we don't need this, but if we decide
289 // to move new value jump prior to RA, we would be needing this.
291 if (!Register::isPhysicalRegister(cmpOp2)) {
292 MachineInstr *def = MRI.getVRegDef(cmpOp2);
293 if (def->getOpcode() == TargetOpcode::COPY)
294 return false;
295 }
296 }
297
298 // Walk the instructions after the compare (predicate def) to the jump,
299 // and satisfy the following conditions.
300 ++II;
301 for (MachineBasicBlock::iterator localII = II; localII != end; ++localII) {
302 if (localII->isDebugInstr())
303 continue;
304
305 // Check 1.
306 // If "common" checks fail, bail out.
307 if (!commonChecksToProhibitNewValueJump(optLocation, localII))
308 return false;
309
310 // Check 2.
311 // If there is a def or use of predicate (result of compare), bail out.
312 if (localII->modifiesRegister(pReg, TRI) ||
313 localII->readsRegister(pReg, TRI))
314 return false;
315
316 // Check 3.
317 // If there is a def of any of the use of the compare (operands of compare),
318 // bail out.
319 // Eg.
320 // p0 = cmp.eq(r2, r0)
321 // r2 = r4
322 // if (p0.new) jump:t .LBB28_3
323 if (localII->modifiesRegister(cmpReg1, TRI) ||
324 (secondReg && localII->modifiesRegister(cmpOp2, TRI)))
325 return false;
326 }
327 return true;
328}
329
330// Given a compare operator, return a matching New Value Jump compare operator.
331// Make sure that MI here is included in isNewValueJumpCandidate.
332static unsigned getNewValueJumpOpcode(MachineInstr *MI, int reg,
333 bool secondRegNewified,
334 MachineBasicBlock *jmpTarget,
336 *MBPI) {
337 bool taken = false;
338 MachineBasicBlock *Src = MI->getParent();
339 const BranchProbability Prediction =
340 MBPI->getEdgeProbability(Src, jmpTarget);
341
342 if (Prediction >= BranchProbability(1,2))
343 taken = true;
344
345 switch (MI->getOpcode()) {
346 case Hexagon::C2_cmpeq:
347 return taken ? Hexagon::J4_cmpeq_t_jumpnv_t
348 : Hexagon::J4_cmpeq_t_jumpnv_nt;
349
350 case Hexagon::C2_cmpeqi:
351 if (reg >= 0)
352 return taken ? Hexagon::J4_cmpeqi_t_jumpnv_t
353 : Hexagon::J4_cmpeqi_t_jumpnv_nt;
354 return taken ? Hexagon::J4_cmpeqn1_t_jumpnv_t
355 : Hexagon::J4_cmpeqn1_t_jumpnv_nt;
356
357 case Hexagon::C4_cmpneqi:
358 if (reg >= 0)
359 return taken ? Hexagon::J4_cmpeqi_f_jumpnv_t
360 : Hexagon::J4_cmpeqi_f_jumpnv_nt;
361 return taken ? Hexagon::J4_cmpeqn1_f_jumpnv_t :
362 Hexagon::J4_cmpeqn1_f_jumpnv_nt;
363
364 case Hexagon::C2_cmpgt:
365 if (secondRegNewified)
366 return taken ? Hexagon::J4_cmplt_t_jumpnv_t
367 : Hexagon::J4_cmplt_t_jumpnv_nt;
368 return taken ? Hexagon::J4_cmpgt_t_jumpnv_t
369 : Hexagon::J4_cmpgt_t_jumpnv_nt;
370
371 case Hexagon::C2_cmpgti:
372 if (reg >= 0)
373 return taken ? Hexagon::J4_cmpgti_t_jumpnv_t
374 : Hexagon::J4_cmpgti_t_jumpnv_nt;
375 return taken ? Hexagon::J4_cmpgtn1_t_jumpnv_t
376 : Hexagon::J4_cmpgtn1_t_jumpnv_nt;
377
378 case Hexagon::C2_cmpgtu:
379 if (secondRegNewified)
380 return taken ? Hexagon::J4_cmpltu_t_jumpnv_t
381 : Hexagon::J4_cmpltu_t_jumpnv_nt;
382 return taken ? Hexagon::J4_cmpgtu_t_jumpnv_t
383 : Hexagon::J4_cmpgtu_t_jumpnv_nt;
384
385 case Hexagon::C2_cmpgtui:
386 return taken ? Hexagon::J4_cmpgtui_t_jumpnv_t
387 : Hexagon::J4_cmpgtui_t_jumpnv_nt;
388
389 case Hexagon::C4_cmpneq:
390 return taken ? Hexagon::J4_cmpeq_f_jumpnv_t
391 : Hexagon::J4_cmpeq_f_jumpnv_nt;
392
393 case Hexagon::C4_cmplte:
394 if (secondRegNewified)
395 return taken ? Hexagon::J4_cmplt_f_jumpnv_t
396 : Hexagon::J4_cmplt_f_jumpnv_nt;
397 return taken ? Hexagon::J4_cmpgt_f_jumpnv_t
398 : Hexagon::J4_cmpgt_f_jumpnv_nt;
399
400 case Hexagon::C4_cmplteu:
401 if (secondRegNewified)
402 return taken ? Hexagon::J4_cmpltu_f_jumpnv_t
403 : Hexagon::J4_cmpltu_f_jumpnv_nt;
404 return taken ? Hexagon::J4_cmpgtu_f_jumpnv_t
405 : Hexagon::J4_cmpgtu_f_jumpnv_nt;
406
407 case Hexagon::C4_cmpltei:
408 if (reg >= 0)
409 return taken ? Hexagon::J4_cmpgti_f_jumpnv_t
410 : Hexagon::J4_cmpgti_f_jumpnv_nt;
411 return taken ? Hexagon::J4_cmpgtn1_f_jumpnv_t
412 : Hexagon::J4_cmpgtn1_f_jumpnv_nt;
413
414 case Hexagon::C4_cmplteui:
415 return taken ? Hexagon::J4_cmpgtui_f_jumpnv_t
416 : Hexagon::J4_cmpgtui_f_jumpnv_nt;
417
418 default:
419 llvm_unreachable("Could not find matching New Value Jump instruction.");
420 }
421 // return *some value* to avoid compiler warning
422 return 0;
423}
424
425bool HexagonNewValueJump::isNewValueJumpCandidate(
426 const MachineInstr &MI) const {
427 switch (MI.getOpcode()) {
428 case Hexagon::C2_cmpeq:
429 case Hexagon::C2_cmpeqi:
430 case Hexagon::C2_cmpgt:
431 case Hexagon::C2_cmpgti:
432 case Hexagon::C2_cmpgtu:
433 case Hexagon::C2_cmpgtui:
434 case Hexagon::C4_cmpneq:
435 case Hexagon::C4_cmpneqi:
436 case Hexagon::C4_cmplte:
437 case Hexagon::C4_cmplteu:
438 case Hexagon::C4_cmpltei:
439 case Hexagon::C4_cmplteui:
440 return true;
441
442 default:
443 return false;
444 }
445}
446
447bool HexagonNewValueJump::runOnMachineFunction(MachineFunction &MF) {
448 LLVM_DEBUG(dbgs() << "********** Hexagon New Value Jump **********\n"
449 << "********** Function: " << MF.getName() << "\n");
450
451 if (skipFunction(MF.getFunction()))
452 return false;
453
454 // If we move NewValueJump before register allocation we'll need live variable
455 // analysis here too.
456
457 QII = static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo());
458 QRI = static_cast<const HexagonRegisterInfo *>(
459 MF.getSubtarget().getRegisterInfo());
460 MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
461
462 // New value jumps require the feeder instruction to be in the same packet.
463 // If packetization is disabled, we cannot generate new value jumps.
465 !MF.getSubtarget<HexagonSubtarget>().useNewValueJumps())
466 return false;
467
468 int nvjCount = DbgNVJCount;
469 int nvjGenerated = 0;
470
471 // Loop through all the bb's of the function
472 for (MachineFunction::iterator MBBb = MF.begin(), MBBe = MF.end();
473 MBBb != MBBe; ++MBBb) {
474 MachineBasicBlock *MBB = &*MBBb;
475
476 LLVM_DEBUG(dbgs() << "** dumping bb ** " << MBB->getNumber() << "\n");
477 LLVM_DEBUG(MBB->dump());
478 LLVM_DEBUG(dbgs() << "\n"
479 << "********** dumping instr bottom up **********\n");
480 bool foundJump = false;
481 bool foundCompare = false;
482 bool invertPredicate = false;
483 unsigned predReg = 0; // predicate reg of the jump.
484 unsigned cmpReg1 = 0;
485 int cmpOp2 = 0;
488 MachineInstr *cmpInstr = nullptr, *jmpInstr = nullptr;
489 MachineBasicBlock *jmpTarget = nullptr;
490 bool afterRA = false;
491 bool isSecondOpReg = false;
492 bool isSecondOpNewified = false;
493 // Traverse the basic block - bottom up
494 for (MachineBasicBlock::iterator MII = MBB->end(), E = MBB->begin();
495 MII != E;) {
496 MachineInstr &MI = *--MII;
497 if (MI.isDebugInstr()) {
498 continue;
499 }
500
501 if ((nvjCount == 0) || (nvjCount > -1 && nvjCount <= nvjGenerated))
502 break;
503
504 LLVM_DEBUG(dbgs() << "Instr: "; MI.dump(); dbgs() << "\n");
505
506 if (!foundJump && (MI.getOpcode() == Hexagon::J2_jumpt ||
507 MI.getOpcode() == Hexagon::J2_jumptpt ||
508 MI.getOpcode() == Hexagon::J2_jumpf ||
509 MI.getOpcode() == Hexagon::J2_jumpfpt ||
510 MI.getOpcode() == Hexagon::J2_jumptnewpt ||
511 MI.getOpcode() == Hexagon::J2_jumptnew ||
512 MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
513 MI.getOpcode() == Hexagon::J2_jumpfnew)) {
514 // This is where you would insert your compare and
515 // instr that feeds compare
516 jmpPos = MII;
517 jmpInstr = &MI;
518 predReg = MI.getOperand(0).getReg();
519 afterRA = Register::isPhysicalRegister(predReg);
520
521 // If ifconverter had not messed up with the kill flags of the
522 // operands, the following check on the kill flag would suffice.
523 // if(!jmpInstr->getOperand(0).isKill()) break;
524
525 // This predicate register is live out of BB
526 // this would only work if we can actually use Live
527 // variable analysis on phy regs - but LLVM does not
528 // provide LV analysis on phys regs.
529 //if(LVs.isLiveOut(predReg, *MBB)) break;
530
531 // Get all the successors of this block - which will always
532 // be 2. Check if the predicate register is live-in in those
533 // successor. If yes, we can not delete the predicate -
534 // I am doing this only because LLVM does not provide LiveOut
535 // at the BB level.
536 bool predLive = false;
537 for (const MachineBasicBlock *SuccMBB : MBB->successors())
538 if (SuccMBB->isLiveIn(predReg))
539 predLive = true;
540 if (predLive)
541 break;
542
543 if (!MI.getOperand(1).isMBB())
544 continue;
545 jmpTarget = MI.getOperand(1).getMBB();
546 foundJump = true;
547 if (MI.getOpcode() == Hexagon::J2_jumpf ||
548 MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
549 MI.getOpcode() == Hexagon::J2_jumpfnew) {
550 invertPredicate = true;
551 }
552 continue;
553 }
554
555 // No new value jump if there is a barrier. A barrier has to be in its
556 // own packet. A barrier has zero operands. We conservatively bail out
557 // here if we see any instruction with zero operands.
558 if (foundJump && MI.getNumOperands() == 0)
559 break;
560
561 if (foundJump && !foundCompare && MI.getOperand(0).isReg() &&
562 MI.getOperand(0).getReg() == predReg) {
563 // Not all compares can be new value compare. Arch Spec: 7.6.1.1
564 if (isNewValueJumpCandidate(MI)) {
565 assert(
566 (MI.getDesc().isCompare()) &&
567 "Only compare instruction can be collapsed into New Value Jump");
568 isSecondOpReg = MI.getOperand(2).isReg();
569
570 if (!canCompareBeNewValueJump(QII, QRI, MII, predReg, isSecondOpReg,
571 afterRA, jmpPos, MF))
572 break;
573
574 cmpInstr = &MI;
575 cmpPos = MII;
576 foundCompare = true;
577
578 // We need cmpReg1 and cmpOp2(imm or reg) while building
579 // new value jump instruction.
580 cmpReg1 = MI.getOperand(1).getReg();
581
582 if (isSecondOpReg)
583 cmpOp2 = MI.getOperand(2).getReg();
584 else
585 cmpOp2 = MI.getOperand(2).getImm();
586 continue;
587 }
588 }
589
590 if (foundCompare && foundJump) {
591 // If "common" checks fail, bail out on this BB.
592 if (!commonChecksToProhibitNewValueJump(afterRA, MII))
593 break;
594
595 bool foundFeeder = false;
596 MachineBasicBlock::iterator feederPos = MII;
597 if (MI.getOperand(0).isReg() && MI.getOperand(0).isDef() &&
598 (MI.getOperand(0).getReg() == cmpReg1 ||
599 (isSecondOpReg &&
600 MI.getOperand(0).getReg() == (unsigned)cmpOp2))) {
601
602 Register feederReg = MI.getOperand(0).getReg();
603
604 // First try to see if we can get the feeder from the first operand
605 // of the compare. If we can not, and if secondOpReg is true
606 // (second operand of the compare is also register), try that one.
607 // TODO: Try to come up with some heuristic to figure out which
608 // feeder would benefit.
609
610 if (feederReg == cmpReg1) {
611 if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF)) {
612 if (!isSecondOpReg)
613 break;
614 else
615 continue;
616 } else
617 foundFeeder = true;
618 }
619
620 if (!foundFeeder && isSecondOpReg && feederReg == (unsigned)cmpOp2)
621 if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF))
622 break;
623
624 if (isSecondOpReg) {
625 // In case of CMPLT, or CMPLTU, or EQ with the second register
626 // to newify, swap the operands.
627 unsigned COp = cmpInstr->getOpcode();
628 if ((COp == Hexagon::C2_cmpeq || COp == Hexagon::C4_cmpneq) &&
629 (feederReg == (unsigned)cmpOp2)) {
630 unsigned tmp = cmpReg1;
631 cmpReg1 = cmpOp2;
632 cmpOp2 = tmp;
633 }
634
635 // Now we have swapped the operands, all we need to check is,
636 // if the second operand (after swap) is the feeder.
637 // And if it is, make a note.
638 if (feederReg == (unsigned)cmpOp2)
639 isSecondOpNewified = true;
640 }
641
642 // Now that we are moving feeder close the jump,
643 // make sure we are respecting the kill values of
644 // the operands of the feeder.
645
646 auto TransferKills = [jmpPos,cmpPos] (MachineInstr &MI) {
647 for (MachineOperand &MO : MI.operands()) {
648 if (!MO.isReg() || !MO.isUse())
649 continue;
650 Register UseR = MO.getReg();
651 for (auto I = std::next(MI.getIterator()); I != jmpPos; ++I) {
652 if (I == cmpPos)
653 continue;
654 for (MachineOperand &Op : I->operands()) {
655 if (!Op.isReg() || !Op.isUse() || !Op.isKill())
656 continue;
657 if (Op.getReg() != UseR)
658 continue;
659 // We found that there is kill of a use register
660 // Set up a kill flag on the register
661 Op.setIsKill(false);
662 MO.setIsKill(true);
663 return;
664 }
665 }
666 }
667 };
668
669 TransferKills(*feederPos);
670 TransferKills(*cmpPos);
671 bool MO1IsKill = cmpPos->killsRegister(cmpReg1, QRI);
672 bool MO2IsKill = isSecondOpReg && cmpPos->killsRegister(cmpOp2, QRI);
673
674 // The feeder moves down to the jump. Debug values between them that
675 // read its register would precede it, so set them undef. Entry values
676 // do not depend on the register and are kept. No copies: the
677 // packetizer needs the feeder next to the jump.
678 for (auto I = std::next(MI.getIterator());
679 I != jmpInstr->getIterator(); ++I)
680 if (I->isDebugValue() && !I->isDebugEntryValue() &&
681 I->readsRegister(feederReg, QRI))
682 I->setDebugValueUndef();
683
684 // The compare is removed, so the predicate it defines is no longer
685 // written. Debug values that read it after the compare are stale.
686 // Set them undef in the rest of the block, and in the blocks after
687 // it, until the predicate is written again or a call may clobber it.
688 auto UndefPredicateReaders = [&](MachineBasicBlock::iterator Begin,
689 MachineBasicBlock &Blk) {
690 for (auto I = Begin; I != Blk.end(); ++I) {
691 if (I->isDebugValue() && !I->isDebugEntryValue() &&
692 I->readsRegister(predReg, QRI))
693 I->setDebugValueUndef();
694 else if (!I->isDebugInstr() &&
695 (I->isCall() || (I->modifiesRegister(predReg, QRI) &&
696 !QII->isPredicated(*I))))
697 return true;
698 }
699 return false;
700 };
701 SmallVector<MachineBasicBlock *, 4> Todo;
702 SmallPtrSet<MachineBasicBlock *, 8> Visited;
703 if (!UndefPredicateReaders(std::next(cmpInstr->getIterator()), *MBB))
704 Todo.append(MBB->succ_begin(), MBB->succ_end());
705 while (!Todo.empty()) {
706 MachineBasicBlock *Next = Todo.pop_back_val();
707 if (Visited.insert(Next).second &&
708 !UndefPredicateReaders(Next->begin(), *Next))
709 Todo.append(Next->succ_begin(), Next->succ_end());
710 }
711
712 MBB->splice(jmpPos, MI.getParent(), MI);
713 MBB->splice(jmpPos, MI.getParent(), cmpInstr);
714 DebugLoc dl = MI.getDebugLoc();
715 MachineInstr *NewMI;
716
717 assert((isNewValueJumpCandidate(*cmpInstr)) &&
718 "This compare is not a New Value Jump candidate.");
719 unsigned opc = getNewValueJumpOpcode(cmpInstr, cmpOp2,
720 isSecondOpNewified,
721 jmpTarget, MBPI);
722 if (invertPredicate)
723 opc = QII->getInvertedPredicatedOpcode(opc);
724
725 if (isSecondOpReg)
726 NewMI = BuildMI(*MBB, jmpPos, dl, QII->get(opc))
727 .addReg(cmpReg1, getKillRegState(MO1IsKill))
728 .addReg(cmpOp2, getKillRegState(MO2IsKill))
729 .addMBB(jmpTarget);
730
731 else
732 NewMI = BuildMI(*MBB, jmpPos, dl, QII->get(opc))
733 .addReg(cmpReg1, getKillRegState(MO1IsKill))
734 .addImm(cmpOp2)
735 .addMBB(jmpTarget);
736
737 assert(NewMI && "New Value Jump Instruction Not created!");
738 (void)NewMI;
739 if (cmpInstr->getOperand(0).isReg() &&
740 cmpInstr->getOperand(0).isKill())
741 cmpInstr->getOperand(0).setIsKill(false);
742 if (cmpInstr->getOperand(1).isReg() &&
743 cmpInstr->getOperand(1).isKill())
744 cmpInstr->getOperand(1).setIsKill(false);
745 cmpInstr->eraseFromParent();
746 jmpInstr->eraseFromParent();
747 ++nvjGenerated;
748 ++NumNVJGenerated;
749 break;
750 }
751 }
752 }
753 }
754
755 return true;
756}
757
759 return new HexagonNewValueJump();
760}
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
MachineBasicBlock & MBB
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static cl::opt< bool > DisableNewValueJumps("disable-nvjump", cl::Hidden, cl::desc("Disable New Value Jumps"))
static bool canCompareBeNewValueJump(const HexagonInstrInfo *QII, const TargetRegisterInfo *TRI, MachineBasicBlock::iterator II, unsigned pReg, bool secondReg, bool optLocation, MachineBasicBlock::iterator end, MachineFunction &MF)
static bool commonChecksToProhibitNewValueJump(bool afterRA, MachineBasicBlock::iterator MII)
cl::opt< bool > DisablePacketizer
hexagon Hexagon static false bool canBeFeederToNewValueJump(const HexagonInstrInfo *QII, const TargetRegisterInfo *TRI, MachineBasicBlock::iterator II, MachineBasicBlock::iterator end, MachineBasicBlock::iterator skip, MachineFunction &MF)
static cl::opt< int > DbgNVJCount("nvj-count", cl::init(-1), cl::Hidden, cl::desc("Maximum number of predicated jumps to be converted to " "New Value Jump"))
static unsigned getNewValueJumpOpcode(MachineInstr *MI, int reg, bool secondRegNewified, MachineBasicBlock *jmpTarget, const MachineBranchProbabilityInfo *MBPI)
IRTranslator LLVM IR MI
static bool skip(GsymDataExtractor &Data, uint64_t &Offset, bool SkippedRanges)
Skip an InlineInfo object in the specified data at the specified offset.
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
uint64_t IntrinsicInst * II
if(PassOpts->AAPipeline)
#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
MachineRegisterInfo & MRI
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
AnalysisUsage & addRequired()
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
bool isPredicated(const MachineInstr &MI) const override
Returns true if the instruction is already predicated.
unsigned getInvertedPredicatedOpcode(const int Opc) const
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
LLVM_ABI void dump() const
iterator_range< succ_iterator > successors()
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI BranchProbability getEdgeProbability(const MachineBasicBlock *Src, const MachineBasicBlock *Dst) 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.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
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 & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
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.
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
void setIsKill(bool Val=true)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Wrapper class representing virtual and physical registers.
Definition Register.h:20
static constexpr bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
Definition Register.h:60
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
self_iterator getIterator()
Definition ilist_node.h:123
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr RegState getKillRegState(bool B)
FunctionPass * createHexagonNewValueJump()
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
DWARFExpression::Operation Op
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147