1//====- X86FlagsCopyLowering.cpp - Lowers COPY nodes of EFLAGS ------------===//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//===----------------------------------------------------------------------===//
10/// Lowers COPY nodes of EFLAGS by directly extracting and preserving individual
13/// We have to do this by carefully analyzing and rewriting the usage of the
14/// copied EFLAGS register because there is no general way to rematerialize the
15/// entire EFLAGS register safely and efficiently. Using `popf` both forces
16/// dynamic stack adjustment and can create correctness issues due to IF, TF,
17/// and other non-status flags being overwritten. Using sequences involving
18/// SAHF don't work on all x86 processors and are often quite slow compared to
19/// directly testing a single status preserved in its own GPR.
21//===----------------------------------------------------------------------===//
60 #define PASS_KEY "x86-flags-copy-lowering"
61 #define DEBUG_TYPE PASS_KEY
63 STATISTIC(NumCopiesEliminated,
"Number of copies of EFLAGS eliminated");
64 STATISTIC(NumSetCCsInserted,
"Number of setCC instructions inserted");
65 STATISTIC(NumTestsInserted,
"Number of test instructions inserted");
66 STATISTIC(NumAddsInserted,
"Number of adds instructions inserted");
67 STATISTIC(NumNFsConvertedTo,
"Number of NF instructions converted to");
73// Convenient array type for storing registers associated with each condition.
74using CondRegArray = std::array<Register, X86::LAST_VALID_COND + 1>;
80 StringRef getPassName()
const override {
return "X86 EFLAGS copy lowering"; }
81 bool runOnMachineFunction(MachineFunction &MF)
override;
82 void getAnalysisUsage(AnalysisUsage &AU)
const override;
84 /// Pass identification, replacement for typeid.
88 MachineRegisterInfo *MRI =
nullptr;
89 const X86Subtarget *Subtarget =
nullptr;
90 const X86InstrInfo *TII =
nullptr;
91 const TargetRegisterInfo *TRI =
nullptr;
92 const TargetRegisterClass *PromoteRC =
nullptr;
93 MachineDominatorTree *MDT =
nullptr;
95 CondRegArray collectCondsInRegs(MachineBasicBlock &
MBB,
101 std::pair<Register, bool> getCondOrInverseInReg(
109 CondRegArray &CondRegs);
110 void rewriteArithmetic(MachineBasicBlock &
MBB,
112 MachineInstr &
MI, CondRegArray &CondRegs);
114 const DebugLoc &Loc, MachineInstr &
MI, CondRegArray &CondRegs);
117}
// end anonymous namespace
120 "X86 EFLAGS copy lowering",
false,
false)
125 return new X86FlagsCopyLoweringPass();
128char X86FlagsCopyLoweringPass::ID = 0;
130void X86FlagsCopyLoweringPass::getAnalysisUsage(
AnalysisUsage &AU)
const {
136 return X86::isADC(
Opc) || X86::isSBB(
Opc) || X86::isRCL(
Opc) ||
137 X86::isRCR(
Opc) || (
Opc == X86::SETB_C32r ||
Opc == X86::SETB_C64r);
146 "Split instruction must be in the split block!");
148 "Only designed to split a tail of branch instructions!");
150 "Must split on an actual jCC instruction!");
152 // Dig out the previous instruction to the split point.
156 "Must split after an actual jCC instruction!");
158 "Must only have this one terminator prior to the split!");
160 // Grab the one successor edge that will stay in `MBB`.
163 // Analyze the original block to see if we are actually splitting an edge
164 // into two edges. This can happen when we have multiple conditional jumps to
165 // the same successor.
169 assert(MI.isTerminator() &&
170 "Should only have spliced terminators!");
172 MI.operands(), [&](MachineOperand &MOp) {
173 return MOp.isMBB() && MOp.getMBB() == &UnsplitSucc;
178 MachineBasicBlock &NewMBB = *MF.CreateMachineBasicBlock();
180 // Insert the new block immediately after the current one. Any existing
181 // fallthrough will be sunk into this new block anyways.
184 // Splice the tail of instructions into the new block.
187 // Copy the necessary succesors (and their probability info) into the new
190 if (IsEdgeSplit || *SI != &UnsplitSucc)
192 // Normalize the probabilities if we didn't end up splitting the edge.
196 // Now replace all of the moved successors in the original block with the new
197 // block. This will merge their probabilities.
198 for (MachineBasicBlock *Succ : NewMBB.
successors())
199 if (Succ != &UnsplitSucc)
200 MBB.replaceSuccessor(Succ, &NewMBB);
202 // We should always end up replacing at least one successor.
204 "Failed to make the new block a successor!");
206 // Now update all the PHIs.
207 for (MachineBasicBlock *Succ : NewMBB.
successors()) {
208 for (MachineInstr &
MI : *Succ) {
214 MachineOperand &OpV =
MI.getOperand(
OpIdx);
215 MachineOperand &OpMBB =
MI.getOperand(
OpIdx + 1);
216 assert(OpMBB.
isMBB() &&
"Block operand to a PHI is not a block!");
220 // Replace the operand for unsplit successors
221 if (!IsEdgeSplit || Succ != &UnsplitSucc) {
224 // We have to continue scanning as there may be multiple entries in
229 // When we have split the edge append a new successor.
230 MI.addOperand(MF, OpV);
244 MI.findRegisterDefOperand(X86::EFLAGS,
/*TRI=*/nullptr);
248 // For the instructions are ADDrm/ADDmr with relocation, we'll skip the
249 // optimization for replacing non-NF with NF. This is to keep backward
250 // compatiblity with old version of linkers without APX relocation type
251 // support on Linux OS.
261bool X86FlagsCopyLoweringPass::runOnMachineFunction(MachineFunction &MF) {
269 PromoteRC = &X86::GR8RegClass;
272 // Nothing to do for a degenerate empty function...
275 if (
none_of(
MRI->def_instructions(X86::EFLAGS), [](
const MachineInstr &
MI) {
276 return MI.getOpcode() == TargetOpcode::COPY;
280 // We change the code, so we don't preserve the dominator tree anyway. If we
281 // got a valid MDT from the pass manager, use that, otherwise construct one
282 // now. This is an optimization that avoids unnecessary MDT construction for
283 // functions that have no flag copies.
285 auto MDTWrapper = getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
286 std::unique_ptr<MachineDominatorTree> OwnedMDT;
288 MDT = &MDTWrapper->getDomTree();
290 OwnedMDT = std::make_unique<MachineDominatorTree>(MF);
291 MDT = OwnedMDT.get();
294 // Collect the copies in RPO so that when there are chains where a copy is in
295 // turn copied again we visit the first one first. This ensures we can find
296 // viable locations for testing the original EFLAGS that dominate all the
297 // uses across complex CFGs.
298 SmallSetVector<MachineInstr *, 4>
Copies;
299 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
300 for (MachineBasicBlock *
MBB : RPOT)
301 for (MachineInstr &
MI : *
MBB)
302 if (
MI.getOpcode() == TargetOpcode::COPY &&
303 MI.getOperand(0).getReg() == X86::EFLAGS)
306 // Try to elminate the copys by transform the instructions between copy and
307 // copydef to the NF (no flags update) variants, e.g.
309 // %1:gr64 = COPY $eflags
310 // OP1 implicit-def dead $eflags
312 // OP2 cc, implicit $eflags
317 // OP2 implicit $eflags
318 if (Subtarget->hasNF()) {
319 SmallSetVector<MachineInstr *, 4> RemovedCopies;
320 // CopyIIt may be invalidated by removing copies.
322 while (CopyIIt != CopyIEnd) {
323 auto NCopyIIt = std::next(CopyIIt);
324 SmallSetVector<MachineInstr *, 4> EvitableClobbers;
325 MachineInstr *CopyI = *CopyIIt;
327 MachineInstr *CopyDefI =
MRI->getVRegDef(VOp.
getReg());
328 MachineBasicBlock *CopyIMBB = CopyI->
getParent();
329 MachineBasicBlock *CopyDefIMBB = CopyDefI->
getParent();
330 // Walk all basic blocks reachable in depth-first iteration on the inverse
331 // CFG from CopyIMBB to CopyDefIMBB. These blocks are all the blocks that
332 // may be executed between the execution of CopyDefIMBB and CopyIMBB. On
333 // all execution paths, instructions from CopyDefI to CopyI (exclusive)
334 // has to be NF-convertible if it clobbers flags.
337 MachineBasicBlock *
MBB = *BI;
338 for (
auto I = (
MBB != CopyDefIMBB)
344 MachineInstr &
MI = *
I;
350 goto ProcessNextCopyI;
356 // Covert evitable clobbers into NF variants and remove the copyies.
357 RemovedCopies.
insert(CopyI);
360 RemovedCopies.
insert(CopyDefI);
363 ++NumCopiesEliminated;
364 for (
auto *Clobber : EvitableClobbers) {
366 assert(NewOpc &&
"evitable clobber must have a NF variant");
367 Clobber->setDesc(
TII->get(NewOpc));
368 Clobber->removeOperand(
369 Clobber->findRegisterDefOperand(X86::EFLAGS,
/*TRI=*/nullptr)
373 // Update liveins for basic blocks in the path
376 if (*BI != CopyDefIMBB)
377 BI->addLiveIn(X86::EFLAGS);
381 Copies.set_subtract(RemovedCopies);
384 // For the rest of copies that cannot be eliminated by NF transform, we use
385 // setcc to preserve the flags in GPR32 before OP1, and recheck its value
386 // before using the flags, e.g.
388 // %1:gr64 = COPY $eflags
389 // OP1 implicit-def dead $eflags
391 // OP2 cc, implicit $eflags
395 // %1:gr8 = SETCCr cc, implicit $eflags
396 // OP1 implicit-def dead $eflags
397 // TEST8rr %1, %1, implicit-def $eflags
398 // OP2 ne, implicit $eflags
399 for (MachineInstr *CopyI :
Copies) {
404 "The input to the copy for EFLAGS should always be a register!");
405 MachineInstr &CopyDefI = *
MRI->getVRegDef(VOp.
getReg());
406 if (CopyDefI.
getOpcode() != TargetOpcode::COPY) {
407 // FIXME: The big likely candidate here are PHI nodes. We could in theory
408 // handle PHI nodes, but it gets really, really hard. Insanely hard. Hard
409 // enough that it is probably better to change every other part of LLVM
410 // to avoid creating them. The issue is that once we have PHIs we won't
411 // know which original EFLAGS value we need to capture with our setCCs
412 // below. The end result will be computing a complete set of setCCs that
413 // we *might* want, computing them in every place where we copy *out* of
414 // EFLAGS and then doing SSA formation on all of them to insert necessary
415 // PHI nodes and consume those here. Then hoping that somehow we DCE the
416 // unnecessary ones. This DCE seems very unlikely to be successful and so
417 // we will almost certainly end up with a glut of dead setCC
418 // instructions. Until we have a motivating test case and fail to avoid
419 // it by changing other parts of LLVM's lowering, we refuse to handle
420 // this complex case here.
422 dbgs() <<
"ERROR: Encountered unexpected def of an eflags copy: ";
425 "Cannot lower EFLAGS copy unless it is defined in turn by a copy!");
429 // All uses of the EFLAGS copy are now rewritten, kill the copy into
430 // eflags and if dead the copy from.
434 ++NumCopiesEliminated;
438 assert(DOp.isDef() &&
"Expected register def!");
439 assert(DOp.getReg() == X86::EFLAGS &&
"Unexpected copy def register!");
443 MachineBasicBlock *TestMBB = CopyDefI.
getParent();
449 // Walk up across live-in EFLAGS to find where they were actually def'ed.
451 // This copy's def may just be part of a region of blocks covered by
452 // a single def of EFLAGS and we want to find the top of that region where
455 // This is essentially a search for a *candidate* reaching definition
456 // location. We don't need to ever find the actual reaching definition here,
457 // but we want to walk up the dominator tree to find the highest point which
458 // would be viable for such a definition.
461 // Scan backwards as we expect these to be relatively short and often find
462 // a clobber near the end.
465 // Flag any instruction (other than the copy we are
466 // currently rewriting) that defs EFLAGS.
467 return &
MI != CopyI &&
468 MI.findRegisterDefOperand(X86::EFLAGS,
/*TRI=*/nullptr);
471 auto HasEFLAGSClobberPath = [&](MachineBasicBlock *BeginMBB,
472 MachineBasicBlock *EndMBB) {
474 "Only support paths down the dominator tree!");
475 SmallPtrSet<MachineBasicBlock *, 4> Visited;
476 SmallVector<MachineBasicBlock *, 4> Worklist;
477 // We terminate at the beginning. No need to scan it.
483 if (!Visited.
insert(PredMBB).second)
485 if (HasEFLAGSClobber(PredMBB->begin(), PredMBB->end()))
487 // Enqueue this block to walk its predecessors.
490 }
while (!Worklist.
empty());
491 // No clobber found along a path from the begin to end.
495 !HasEFLAGSClobber(TestMBB->
begin(), TestPos)) {
496 // Find the nearest common dominator of the predecessors, as
497 // that will be the best candidate to hoist into.
498 MachineBasicBlock *HoistMBB =
501 [&](MachineBasicBlock *
LHS, MachineBasicBlock *
RHS) {
502 return MDT->findNearestCommonDominator(LHS, RHS);
505 // Now we need to scan all predecessors that may be reached along paths to
506 // the hoist block. A clobber anywhere in any of these blocks the hoist.
507 // Note that this even handles loops because we require *no* clobbers.
508 if (HasEFLAGSClobberPath(HoistMBB, TestMBB))
511 // We also need the terminators to not sneakily clobber flags.
516 // We found a viable location, hoist our test position to it.
519 // Clear the debug location as it would just be confusing after hoisting.
525 [&](MachineInstr &
MI) {
526 return MI.findRegisterDefOperand(X86::EFLAGS,
/*TRI=*/nullptr);
529 dbgs() <<
" Using EFLAGS defined by: ";
532 dbgs() <<
" Using live-in flags for BB:\n";
537 // While rewriting uses, we buffer jumps and rewrite them in a second pass
538 // because doing so will perturb the CFG that we are walking to find the
539 // uses in the first place.
540 SmallVector<MachineInstr *, 4> JmpIs;
542 // Gather the condition flags that have already been preserved in
543 // registers. We do this from scratch each time as we expect there to be
544 // very few of them and we expect to not revisit the same copy definition
545 // many times. If either of those change sufficiently we could build a map
546 // of these up front instead.
547 CondRegArray CondRegs = collectCondsInRegs(*TestMBB, TestPos);
549 // Collect the basic blocks we need to scan. Typically this will just be
550 // a single basic block but we may have to scan multiple blocks if the
551 // EFLAGS copy lives into successors.
559 // Track when if/when we find a kill of the flags in this block.
560 bool FlagsKilled =
false;
562 // In most cases, we walk from the beginning to the end of the block. But
563 // when the block is the same block as the copy is from, we will visit it
564 // twice. The first time we start from the copy and go to the end. The
565 // second time we start from the beginning and go to the copy. This lets
566 // us handle copies inside of cycles.
567 // FIXME: This loop is *super* confusing. This is at least in part
568 // a symptom of all of this routine needing to be refactored into
569 // documentable components. Once done, there may be a better way to write
576 MachineInstr &
MI = *MII++;
577 // If we are in the original copy block and encounter either the copy
578 // def or the copy itself, break so that we don't re-process any part of
579 // the block or process the instructions in the range that was copied
581 if (&
MI == CopyI || &
MI == &CopyDefI) {
583 "Should only encounter these on the second pass over the "
588 MachineOperand *FlagUse =
589 MI.findRegisterUseOperand(X86::EFLAGS,
/*TRI=*/nullptr);
590 FlagsKilled =
MI.modifiesRegister(X86::EFLAGS,
TRI);
592 if (!FlagUse && FlagsKilled)
599 // Check the kill flag before we rewrite as that may change it.
603 // Once we encounter a branch, the rest of the instructions must also be
604 // branches. We can't rewrite in place here, so we handle them below.
606 // Note that we don't have to handle tail calls here, even conditional
607 // tail calls, as those are not introduced into the X86 MI until post-RA
608 // branch folding or black placement. As a consequence, we get to deal
609 // with the simpler formulation of conditional branches followed by tail
612 auto JmpIt =
MI.getIterator();
614 JmpIs.push_back(&*JmpIt);
621 // Otherwise we can just rewrite in-place.
622 unsigned Opc =
MI.getOpcode();
623 if (
Opc == TargetOpcode::COPY) {
624 // Just replace this copy with the original copy def.
625 MRI->replaceRegWith(
MI.getOperand(0).getReg(),
627 MI.eraseFromParent();
628 }
else if (X86::isSETCC(
Opc) || X86::isSETZUCC(
Opc)) {
629 rewriteSetCC(*TestMBB, TestPos, TestLoc,
MI, CondRegs);
631 rewriteArithmetic(*TestMBB, TestPos, TestLoc,
MI, CondRegs);
633 rewriteMI(*TestMBB, TestPos, TestLoc,
MI, CondRegs);
636 // If this was the last use of the flags, we're done.
641 // If the flags were killed, we're done with this block.
645 // Otherwise we need to scan successors for ones where the flags live-in
646 // and queue those up for processing.
647 for (MachineBasicBlock *SuccMBB : UseMBB.
successors())
648 if (SuccMBB->isLiveIn(X86::EFLAGS) &&
650 // We currently don't do any PHI insertion and so we require that the
651 // test basic block dominates all of the use basic blocks. Further, we
652 // can't have a cycle from the test block back to itself as that would
653 // create a cycle requiring a PHI to break it.
655 // We could in theory do PHI insertion here if it becomes useful by
656 // just taking undef values in along every edge that we don't trace
657 // this EFLAGS copy along. This isn't as bad as fully general PHI
658 // insertion, but still seems like a great deal of complexity.
660 // Because it is theoretically possible that some earlier MI pass or
661 // other lowering transformation could induce this to happen, we do
662 // a hard check even in non-debug builds here.
663 if (SuccMBB == TestMBB || !MDT->
dominates(TestMBB, SuccMBB)) {
666 <<
"ERROR: Encountered use that is not dominated by our test "
667 "basic block! Rewriting this would require inserting PHI "
668 "nodes to track the flag state across the CFG.\n\nTest "
671 dbgs() <<
"Use block:\n";
675 "Cannot lower EFLAGS copy when original copy def "
676 "does not dominate all uses.");
681 // After this, EFLAGS will be recreated before each use.
682 SuccMBB->removeLiveIn(X86::EFLAGS);
684 }
while (!Blocks.
empty());
686 // Now rewrite the jumps that use the flags. These we handle specially
687 // because if there are multiple jumps in a single basic block we'll have
688 // to do surgery on the CFG.
689 MachineBasicBlock *LastJmpMBB =
nullptr;
690 for (MachineInstr *JmpI : JmpIs) {
691 // Past the first jump within a basic block we need to split the blocks
693 if (JmpI->getParent() == LastJmpMBB)
698 rewriteMI(*TestMBB, TestPos, TestLoc, *JmpI, CondRegs);
701 // FIXME: Mark the last use of EFLAGS before the copy's def as a kill if
702 // the copy's def operand is itself a kill.
706 for (MachineBasicBlock &
MBB : MF)
707 for (MachineInstr &
MI :
MBB)
708 if (
MI.getOpcode() == TargetOpcode::COPY &&
709 (
MI.getOperand(0).getReg() == X86::EFLAGS ||
710 MI.getOperand(1).getReg() == X86::EFLAGS)) {
720/// Collect any conditions that have already been set in registers so that we
721/// can re-use them rather than adding duplicates.
722CondRegArray X86FlagsCopyLoweringPass::collectCondsInRegs(
724 CondRegArray CondRegs = {};
726 // Scan backwards across the range of instructions with live EFLAGS.
727 for (MachineInstr &
MI :
731 MI.getOperand(0).isReg() &&
MI.getOperand(0).getReg().isVirtual()) {
733 "A non-storing SETcc should always define a register!");
734 CondRegs[
Cond] =
MI.getOperand(0).getReg();
737 // Stop scanning when we see the first definition of the EFLAGS as prior to
738 // this we would potentially capture the wrong flag state.
739 if (
MI.findRegisterDefOperand(X86::EFLAGS,
/*TRI=*/nullptr))
745Register X86FlagsCopyLoweringPass::promoteCondToReg(
749 auto SetI =
BuildMI(TestMBB, TestPos, TestLoc,
TII->get(X86::SETCCr),
Reg)
757std::pair<Register, bool> X86FlagsCopyLoweringPass::getCondOrInverseInReg(
762 if (!CondReg && !InvCondReg)
763 CondReg = promoteCondToReg(TestMBB, TestPos, TestLoc,
Cond);
766 return {CondReg,
false};
768 return {InvCondReg,
true};
771void X86FlagsCopyLoweringPass::insertTest(MachineBasicBlock &
MBB,
781void X86FlagsCopyLoweringPass::rewriteSetCC(MachineBasicBlock &
MBB,
785 CondRegArray &CondRegs) {
787 // Note that we can't usefully rewrite this to the inverse without complex
788 // analysis of the users of the setCC. Largely we rely on duplicates which
789 // could have been avoided already being avoided here.
792 CondReg = promoteCondToReg(
MBB, Pos, Loc,
Cond);
794 if (X86::isSETZUCC(
MI.getOpcode())) {
795 // SETZUCC is generated for register only for now.
796 assert(!
MI.mayStore() &&
"Cannot handle memory variants");
798 "Cannot have a non-register defined operand to SETZUcc!");
800 // Drop Kill flags on the old register before replacing. CondReg may have
801 // a longer live range.
802 MRI->clearKillFlags(OldReg);
803 for (
auto &Use :
MRI->use_instructions(OldReg)) {
804 assert(
Use.getOpcode() == X86::INSERT_SUBREG &&
805 "SETZUCC should be only used by INSERT_SUBREG");
806 Use.getOperand(2).setReg(CondReg);
807 // Recover MOV32r0 before INSERT_SUBREG, which removed by SETZUCC.
808 Register ZeroReg =
MRI->createVirtualRegister(&X86::GR32RegClass);
811 Use.getOperand(1).setReg(ZeroReg);
813 MI.eraseFromParent();
817 // Rewriting a register def is trivial: we just replace the register and
819 if (!
MI.mayStore()) {
821 "Cannot have a non-register defined operand to SETcc!");
823 // Drop Kill flags on the old register before replacing. CondReg may have
824 // a longer live range.
825 MRI->clearKillFlags(OldReg);
826 MRI->replaceRegWith(OldReg, CondReg);
827 MI.eraseFromParent();
831 // Otherwise, we need to emit a store.
832 auto MIB =
BuildMI(*
MI.getParent(),
MI.getIterator(),
MI.getDebugLoc(),
833 TII->get(X86::MOV8mr));
834 // Copy the address operands.
836 MIB.add(
MI.getOperand(i));
839 MIB.setMemRefs(
MI.memoperands());
840 MI.eraseFromParent();
843void X86FlagsCopyLoweringPass::rewriteArithmetic(
845 const DebugLoc &Loc, MachineInstr &
MI, CondRegArray &CondRegs) {
846 // Arithmetic is either reading CF or OF.
848 // The addend to use to reset CF or OF when added to the flag value.
849 // Set up an addend that when one is added will need a carry due to not
850 // having a higher bit available.
853 // Now get a register that contains the value of the flag input to the
854 // arithmetic. We require exactly this flag to simplify the arithmetic
855 // required to materialize it back into the flag.
858 CondReg = promoteCondToReg(
MBB, Pos, Loc,
Cond);
860 // Insert an instruction that will set the flag back to the desired value.
861 Register TmpReg =
MRI->createVirtualRegister(PromoteRC);
864 TII->get(Subtarget->hasNDD() ? X86::ADD8ri_ND : X86::ADD8ri))
871 MI.findRegisterUseOperand(X86::EFLAGS,
/*TRI=*/nullptr)->setIsKill(
true);
875#define FROM_TO(A, B) \
876 case X86::CMOV##A##_Fp32: \
877 case X86::CMOV##A##_Fp64: \
878 case X86::CMOV##A##_Fp80: \
879 return X86::COND_##B;
899 case X86::CMOVB_##A: \
900 case X86::CMOVE_##A: \
901 case X86::CMOVP_##A: \
902 case X86::CMOVBE_##A: \
903 case X86::CMOVNB_##A: \
904 case X86::CMOVNE_##A: \
905 case X86::CMOVNP_##A: \
906 case X86::CMOVNBE_##A: \
907 return (CC == X86::COND_E) ? X86::CMOVE_##A : X86::CMOVNE_##A;
918void X86FlagsCopyLoweringPass::rewriteMI(MachineBasicBlock &
MBB,
921 CondRegArray &CondRegs) {
922 // First get the register containing this specific condition.
923 bool IsImplicitCC =
false;
932 std::tie(CondReg, Inverted) =
933 getCondOrInverseInReg(
MBB, Pos, Loc, CC, CondRegs);
935 // Insert a direct test of the saved register.
936 insertTest(*
MI.getParent(),
MI.getIterator(),
MI.getDebugLoc(), CondReg);
938 // Rewrite the instruction to use the !ZF flag from the test, and then kill
939 // its use of the flags afterward.
944 MI.getOperand(
MI.getDesc().getNumOperands() - 1).setImm(NewCC);
946 MI.findRegisterUseOperand(X86::EFLAGS,
/*TRI=*/nullptr)->setIsKill(
true);
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define CASE(ATTRNAME, AANAME,...)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
SmallPtrSet< const BasicBlock *, 8 > VisitedBlocks
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
static const HTTPClientCleanup Cleanup
const HexagonInstrInfo * TII
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Register const TargetRegisterInfo * TRI
Promote Memory to Register
MachineInstr unsigned OpIdx
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
const SmallVectorImpl< MachineOperand > & Cond
static void splitBlock(MachineBasicBlock &MBB, MachineInstr &MI, MachineDominatorTree *MDT)
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
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)
#define FROM_TO(FROM, TO)
cl::opt< bool > X86EnableAPXForRelocation
static X86::CondCode getImplicitCondFromMI(unsigned Opc)
static unsigned getOpcodeWithCC(unsigned Opc, X86::CondCode CC)
static bool isArithmeticOp(unsigned Opc)
static EFLAGSClobber getClobberType(const MachineInstr &MI)
Represent the analysis usage information of a pass.
AnalysisUsage & addUsedIfAvailable()
Add the specified Pass class to the set of analyses used by this pass.
FunctionPass class - This class is used to implement most global optimizations.
void normalizeSuccProbs()
Normalize probabilities of all successors so that the sum of them becomes one.
instr_iterator instr_begin()
LLVM_ABI MachineBasicBlock * getFallThrough(bool JumpToFallThrough=true)
Return the fallthrough block if the block can implicitly transfer control to the block after it by fa...
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
succ_iterator succ_begin()
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
LLVM_ABI void dump() const
LLVM_ABI void copySuccessor(const MachineBasicBlock *Orig, succ_iterator I)
Copy a successor (and any probability info) from original block to this block's.
pred_iterator pred_begin()
instr_iterator instr_end()
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator_range< succ_iterator > successors()
LLVM_ABI bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
iterator_range< pred_iterator > predecessors()
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 bool isLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
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 & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
bool isBranch(QueryType Type=AnyInBundle) const
Returns true if this is a conditional, unconditional, or indirect branch.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
LLVM_ABI void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
LLVM_ABI void dump() const
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
void setMBB(MachineBasicBlock *MBB)
Register getReg() const
getReg - Returns the register number.
static MachineOperand CreateMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0)
bool isMBB() const
isMBB - Tests if this is a MO_MachineBasicBlock operand.
bool insert(const value_type &X)
Insert a new element into the SetVector.
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)
const X86InstrInfo * getInstrInfo() const override
const X86RegisterInfo * getRegisterInfo() const override
self_iterator getIterator()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
CondCode getCondFromBranch(const MachineInstr &MI)
CondCode getCondFromMI(const MachineInstr &MI)
Return the condition code of the instruction.
CondCode GetOppositeBranchCondition(CondCode CC)
GetOppositeBranchCondition - Return the inverse of the specified cond, e.g.
CondCode getCondFromSETCC(const MachineInstr &MI)
unsigned getNFVariant(unsigned Opc)
NodeAddr< UseNode * > Use
This is an optimization pass for GlobalISel generic memory operations.
static bool isAddMemInstrWithRelocation(const MachineInstr &MI)
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
auto successors(const MachineBasicBlock *BB)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
FunctionPass * createX86FlagsCopyLoweringPass()
Return a pass that lowers EFLAGS copy pseudo instructions.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
auto reverse(ContainerTy &&C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
idf_iterator< T > idf_end(const T &G)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
idf_iterator< T > idf_begin(const T &G)
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.