1 //===-- X86InstrInfo.cpp - X86 Instruction Information --------------------===//
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 //===----------------------------------------------------------------------===//
9 // This file contains the X86 implementation of the TargetInstrInfo class.
11 //===----------------------------------------------------------------------===//
13 #include "X86InstrInfo.h"
15 #include "X86InstrBuilder.h"
16 #include "X86InstrFoldTables.h"
17 #include "X86MachineFunctionInfo.h"
18 #include "X86Subtarget.h"
19 #include "X86TargetMachine.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/Sequence.h"
22 #include "llvm/CodeGen/LivePhysRegs.h"
23 #include "llvm/CodeGen/LiveVariables.h"
24 #include "llvm/CodeGen/MachineConstantPool.h"
25 #include "llvm/CodeGen/MachineDominators.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/CodeGen/MachineRegisterInfo.h"
30 #include "llvm/CodeGen/StackMaps.h"
31 #include "llvm/IR/DerivedTypes.h"
32 #include "llvm/IR/Function.h"
33 #include "llvm/IR/DebugInfoMetadata.h"
34 #include "llvm/MC/MCAsmInfo.h"
35 #include "llvm/MC/MCExpr.h"
36 #include "llvm/MC/MCInst.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include "llvm/Target/TargetOptions.h"
45 #define DEBUG_TYPE "x86-instr-info"
47 #define GET_INSTRINFO_CTOR_DTOR
48 #include "X86GenInstrInfo.inc"
51 NoFusing("disable-spill-fusing",
52 cl::desc("Disable fusing of spill code into instructions"),
55 PrintFailedFusing("print-failed-fuse-candidates",
56 cl::desc("Print instructions that the allocator wants to"
57 " fuse, but the X86 backend currently can't"),
60 ReMatPICStubLoad("remat-pic-stub-load",
61 cl::desc("Re-materialize load from stub in PIC mode"),
62 cl::init(false), cl::Hidden
);
63 static cl::opt
<unsigned>
64 PartialRegUpdateClearance("partial-reg-update-clearance",
65 cl::desc("Clearance between two register writes "
66 "for inserting XOR to avoid partial "
68 cl::init(64), cl::Hidden
);
69 static cl::opt
<unsigned>
70 UndefRegClearance("undef-reg-clearance",
71 cl::desc("How many idle instructions we would like before "
72 "certain undef register reads"),
73 cl::init(128), cl::Hidden
);
76 // Pin the vtable to this file.
77 void X86InstrInfo::anchor() {}
79 X86InstrInfo::X86InstrInfo(X86Subtarget
&STI
)
80 : X86GenInstrInfo((STI
.isTarget64BitLP64() ? X86::ADJCALLSTACKDOWN64
81 : X86::ADJCALLSTACKDOWN32
),
82 (STI
.isTarget64BitLP64() ? X86::ADJCALLSTACKUP64
83 : X86::ADJCALLSTACKUP32
),
85 (STI
.is64Bit() ? X86::RETQ
: X86::RETL
)),
86 Subtarget(STI
), RI(STI
.getTargetTriple()) {
90 X86InstrInfo::isCoalescableExtInstr(const MachineInstr
&MI
,
91 unsigned &SrcReg
, unsigned &DstReg
,
92 unsigned &SubIdx
) const {
93 switch (MI
.getOpcode()) {
100 if (!Subtarget
.is64Bit())
101 // It's not always legal to reference the low 8-bit of the larger
102 // register in 32-bit mode.
105 case X86::MOVSX32rr16
:
106 case X86::MOVZX32rr16
:
107 case X86::MOVSX64rr16
:
108 case X86::MOVSX64rr32
: {
109 if (MI
.getOperand(0).getSubReg() || MI
.getOperand(1).getSubReg())
112 SrcReg
= MI
.getOperand(1).getReg();
113 DstReg
= MI
.getOperand(0).getReg();
114 switch (MI
.getOpcode()) {
115 default: llvm_unreachable("Unreachable!");
116 case X86::MOVSX16rr8
:
117 case X86::MOVZX16rr8
:
118 case X86::MOVSX32rr8
:
119 case X86::MOVZX32rr8
:
120 case X86::MOVSX64rr8
:
121 SubIdx
= X86::sub_8bit
;
123 case X86::MOVSX32rr16
:
124 case X86::MOVZX32rr16
:
125 case X86::MOVSX64rr16
:
126 SubIdx
= X86::sub_16bit
;
128 case X86::MOVSX64rr32
:
129 SubIdx
= X86::sub_32bit
;
138 int X86InstrInfo::getSPAdjust(const MachineInstr
&MI
) const {
139 const MachineFunction
*MF
= MI
.getParent()->getParent();
140 const TargetFrameLowering
*TFI
= MF
->getSubtarget().getFrameLowering();
142 if (isFrameInstr(MI
)) {
143 unsigned StackAlign
= TFI
->getStackAlignment();
144 int SPAdj
= alignTo(getFrameSize(MI
), StackAlign
);
145 SPAdj
-= getFrameAdjustment(MI
);
146 if (!isFrameSetup(MI
))
151 // To know whether a call adjusts the stack, we need information
152 // that is bound to the following ADJCALLSTACKUP pseudo.
153 // Look for the next ADJCALLSTACKUP that follows the call.
155 const MachineBasicBlock
*MBB
= MI
.getParent();
156 auto I
= ++MachineBasicBlock::const_iterator(MI
);
157 for (auto E
= MBB
->end(); I
!= E
; ++I
) {
158 if (I
->getOpcode() == getCallFrameDestroyOpcode() ||
163 // If we could not find a frame destroy opcode, then it has already
164 // been simplified, so we don't care.
165 if (I
->getOpcode() != getCallFrameDestroyOpcode())
168 return -(I
->getOperand(1).getImm());
171 // Currently handle only PUSHes we can reasonably expect to see
173 switch (MI
.getOpcode()) {
191 /// Return true and the FrameIndex if the specified
192 /// operand and follow operands form a reference to the stack frame.
193 bool X86InstrInfo::isFrameOperand(const MachineInstr
&MI
, unsigned int Op
,
194 int &FrameIndex
) const {
195 if (MI
.getOperand(Op
+ X86::AddrBaseReg
).isFI() &&
196 MI
.getOperand(Op
+ X86::AddrScaleAmt
).isImm() &&
197 MI
.getOperand(Op
+ X86::AddrIndexReg
).isReg() &&
198 MI
.getOperand(Op
+ X86::AddrDisp
).isImm() &&
199 MI
.getOperand(Op
+ X86::AddrScaleAmt
).getImm() == 1 &&
200 MI
.getOperand(Op
+ X86::AddrIndexReg
).getReg() == 0 &&
201 MI
.getOperand(Op
+ X86::AddrDisp
).getImm() == 0) {
202 FrameIndex
= MI
.getOperand(Op
+ X86::AddrBaseReg
).getIndex();
208 static bool isFrameLoadOpcode(int Opcode
, unsigned &MemBytes
) {
222 case X86::MOVSSrm_alt
:
224 case X86::VMOVSSrm_alt
:
226 case X86::VMOVSSZrm_alt
:
233 case X86::MOVSDrm_alt
:
235 case X86::VMOVSDrm_alt
:
237 case X86::VMOVSDZrm_alt
:
238 case X86::MMX_MOVD64rm
:
239 case X86::MMX_MOVQ64rm
:
255 case X86::VMOVAPSZ128rm
:
256 case X86::VMOVUPSZ128rm
:
257 case X86::VMOVAPSZ128rm_NOVLX
:
258 case X86::VMOVUPSZ128rm_NOVLX
:
259 case X86::VMOVAPDZ128rm
:
260 case X86::VMOVUPDZ128rm
:
261 case X86::VMOVDQU8Z128rm
:
262 case X86::VMOVDQU16Z128rm
:
263 case X86::VMOVDQA32Z128rm
:
264 case X86::VMOVDQU32Z128rm
:
265 case X86::VMOVDQA64Z128rm
:
266 case X86::VMOVDQU64Z128rm
:
269 case X86::VMOVAPSYrm
:
270 case X86::VMOVUPSYrm
:
271 case X86::VMOVAPDYrm
:
272 case X86::VMOVUPDYrm
:
273 case X86::VMOVDQAYrm
:
274 case X86::VMOVDQUYrm
:
275 case X86::VMOVAPSZ256rm
:
276 case X86::VMOVUPSZ256rm
:
277 case X86::VMOVAPSZ256rm_NOVLX
:
278 case X86::VMOVUPSZ256rm_NOVLX
:
279 case X86::VMOVAPDZ256rm
:
280 case X86::VMOVUPDZ256rm
:
281 case X86::VMOVDQU8Z256rm
:
282 case X86::VMOVDQU16Z256rm
:
283 case X86::VMOVDQA32Z256rm
:
284 case X86::VMOVDQU32Z256rm
:
285 case X86::VMOVDQA64Z256rm
:
286 case X86::VMOVDQU64Z256rm
:
289 case X86::VMOVAPSZrm
:
290 case X86::VMOVUPSZrm
:
291 case X86::VMOVAPDZrm
:
292 case X86::VMOVUPDZrm
:
293 case X86::VMOVDQU8Zrm
:
294 case X86::VMOVDQU16Zrm
:
295 case X86::VMOVDQA32Zrm
:
296 case X86::VMOVDQU32Zrm
:
297 case X86::VMOVDQA64Zrm
:
298 case X86::VMOVDQU64Zrm
:
304 static bool isFrameStoreOpcode(int Opcode
, unsigned &MemBytes
) {
328 case X86::MMX_MOVD64mr
:
329 case X86::MMX_MOVQ64mr
:
330 case X86::MMX_MOVNTQmr
:
346 case X86::VMOVUPSZ128mr
:
347 case X86::VMOVAPSZ128mr
:
348 case X86::VMOVUPSZ128mr_NOVLX
:
349 case X86::VMOVAPSZ128mr_NOVLX
:
350 case X86::VMOVUPDZ128mr
:
351 case X86::VMOVAPDZ128mr
:
352 case X86::VMOVDQA32Z128mr
:
353 case X86::VMOVDQU32Z128mr
:
354 case X86::VMOVDQA64Z128mr
:
355 case X86::VMOVDQU64Z128mr
:
356 case X86::VMOVDQU8Z128mr
:
357 case X86::VMOVDQU16Z128mr
:
360 case X86::VMOVUPSYmr
:
361 case X86::VMOVAPSYmr
:
362 case X86::VMOVUPDYmr
:
363 case X86::VMOVAPDYmr
:
364 case X86::VMOVDQUYmr
:
365 case X86::VMOVDQAYmr
:
366 case X86::VMOVUPSZ256mr
:
367 case X86::VMOVAPSZ256mr
:
368 case X86::VMOVUPSZ256mr_NOVLX
:
369 case X86::VMOVAPSZ256mr_NOVLX
:
370 case X86::VMOVUPDZ256mr
:
371 case X86::VMOVAPDZ256mr
:
372 case X86::VMOVDQU8Z256mr
:
373 case X86::VMOVDQU16Z256mr
:
374 case X86::VMOVDQA32Z256mr
:
375 case X86::VMOVDQU32Z256mr
:
376 case X86::VMOVDQA64Z256mr
:
377 case X86::VMOVDQU64Z256mr
:
380 case X86::VMOVUPSZmr
:
381 case X86::VMOVAPSZmr
:
382 case X86::VMOVUPDZmr
:
383 case X86::VMOVAPDZmr
:
384 case X86::VMOVDQU8Zmr
:
385 case X86::VMOVDQU16Zmr
:
386 case X86::VMOVDQA32Zmr
:
387 case X86::VMOVDQU32Zmr
:
388 case X86::VMOVDQA64Zmr
:
389 case X86::VMOVDQU64Zmr
:
396 unsigned X86InstrInfo::isLoadFromStackSlot(const MachineInstr
&MI
,
397 int &FrameIndex
) const {
399 return X86InstrInfo::isLoadFromStackSlot(MI
, FrameIndex
, Dummy
);
402 unsigned X86InstrInfo::isLoadFromStackSlot(const MachineInstr
&MI
,
404 unsigned &MemBytes
) const {
405 if (isFrameLoadOpcode(MI
.getOpcode(), MemBytes
))
406 if (MI
.getOperand(0).getSubReg() == 0 && isFrameOperand(MI
, 1, FrameIndex
))
407 return MI
.getOperand(0).getReg();
411 unsigned X86InstrInfo::isLoadFromStackSlotPostFE(const MachineInstr
&MI
,
412 int &FrameIndex
) const {
414 if (isFrameLoadOpcode(MI
.getOpcode(), Dummy
)) {
416 if ((Reg
= isLoadFromStackSlot(MI
, FrameIndex
)))
418 // Check for post-frame index elimination operations
419 SmallVector
<const MachineMemOperand
*, 1> Accesses
;
420 if (hasLoadFromStackSlot(MI
, Accesses
)) {
422 cast
<FixedStackPseudoSourceValue
>(Accesses
.front()->getPseudoValue())
430 unsigned X86InstrInfo::isStoreToStackSlot(const MachineInstr
&MI
,
431 int &FrameIndex
) const {
433 return X86InstrInfo::isStoreToStackSlot(MI
, FrameIndex
, Dummy
);
436 unsigned X86InstrInfo::isStoreToStackSlot(const MachineInstr
&MI
,
438 unsigned &MemBytes
) const {
439 if (isFrameStoreOpcode(MI
.getOpcode(), MemBytes
))
440 if (MI
.getOperand(X86::AddrNumOperands
).getSubReg() == 0 &&
441 isFrameOperand(MI
, 0, FrameIndex
))
442 return MI
.getOperand(X86::AddrNumOperands
).getReg();
446 unsigned X86InstrInfo::isStoreToStackSlotPostFE(const MachineInstr
&MI
,
447 int &FrameIndex
) const {
449 if (isFrameStoreOpcode(MI
.getOpcode(), Dummy
)) {
451 if ((Reg
= isStoreToStackSlot(MI
, FrameIndex
)))
453 // Check for post-frame index elimination operations
454 SmallVector
<const MachineMemOperand
*, 1> Accesses
;
455 if (hasStoreToStackSlot(MI
, Accesses
)) {
457 cast
<FixedStackPseudoSourceValue
>(Accesses
.front()->getPseudoValue())
465 /// Return true if register is PIC base; i.e.g defined by X86::MOVPC32r.
466 static bool regIsPICBase(unsigned BaseReg
, const MachineRegisterInfo
&MRI
) {
467 // Don't waste compile time scanning use-def chains of physregs.
468 if (!Register::isVirtualRegister(BaseReg
))
470 bool isPICBase
= false;
471 for (MachineRegisterInfo::def_instr_iterator I
= MRI
.def_instr_begin(BaseReg
),
472 E
= MRI
.def_instr_end(); I
!= E
; ++I
) {
473 MachineInstr
*DefMI
= &*I
;
474 if (DefMI
->getOpcode() != X86::MOVPC32r
)
476 assert(!isPICBase
&& "More than one PIC base?");
482 bool X86InstrInfo::isReallyTriviallyReMaterializable(const MachineInstr
&MI
,
483 AliasAnalysis
*AA
) const {
484 switch (MI
.getOpcode()) {
486 // This function should only be called for opcodes with the ReMaterializable
488 llvm_unreachable("Unknown rematerializable operation!");
491 case X86::LOAD_STACK_GUARD
:
492 case X86::AVX1_SETALLONES
:
493 case X86::AVX2_SETALLONES
:
494 case X86::AVX512_128_SET0
:
495 case X86::AVX512_256_SET0
:
496 case X86::AVX512_512_SET0
:
497 case X86::AVX512_512_SETALLONES
:
498 case X86::AVX512_FsFLD0SD
:
499 case X86::AVX512_FsFLD0SS
:
510 case X86::MOV32ImmSExti8
:
515 case X86::MOV64ImmSExti8
:
517 case X86::V_SETALLONES
:
526 case X86::MOV8rm_NOREX
:
531 case X86::MOVSSrm_alt
:
533 case X86::MOVSDrm_alt
:
541 case X86::VMOVSSrm_alt
:
543 case X86::VMOVSDrm_alt
:
550 case X86::VMOVAPSYrm
:
551 case X86::VMOVUPSYrm
:
552 case X86::VMOVAPDYrm
:
553 case X86::VMOVUPDYrm
:
554 case X86::VMOVDQAYrm
:
555 case X86::VMOVDQUYrm
:
556 case X86::MMX_MOVD64rm
:
557 case X86::MMX_MOVQ64rm
:
560 case X86::VMOVSSZrm_alt
:
562 case X86::VMOVSDZrm_alt
:
563 case X86::VMOVAPDZ128rm
:
564 case X86::VMOVAPDZ256rm
:
565 case X86::VMOVAPDZrm
:
566 case X86::VMOVAPSZ128rm
:
567 case X86::VMOVAPSZ256rm
:
568 case X86::VMOVAPSZ128rm_NOVLX
:
569 case X86::VMOVAPSZ256rm_NOVLX
:
570 case X86::VMOVAPSZrm
:
571 case X86::VMOVDQA32Z128rm
:
572 case X86::VMOVDQA32Z256rm
:
573 case X86::VMOVDQA32Zrm
:
574 case X86::VMOVDQA64Z128rm
:
575 case X86::VMOVDQA64Z256rm
:
576 case X86::VMOVDQA64Zrm
:
577 case X86::VMOVDQU16Z128rm
:
578 case X86::VMOVDQU16Z256rm
:
579 case X86::VMOVDQU16Zrm
:
580 case X86::VMOVDQU32Z128rm
:
581 case X86::VMOVDQU32Z256rm
:
582 case X86::VMOVDQU32Zrm
:
583 case X86::VMOVDQU64Z128rm
:
584 case X86::VMOVDQU64Z256rm
:
585 case X86::VMOVDQU64Zrm
:
586 case X86::VMOVDQU8Z128rm
:
587 case X86::VMOVDQU8Z256rm
:
588 case X86::VMOVDQU8Zrm
:
589 case X86::VMOVUPDZ128rm
:
590 case X86::VMOVUPDZ256rm
:
591 case X86::VMOVUPDZrm
:
592 case X86::VMOVUPSZ128rm
:
593 case X86::VMOVUPSZ256rm
:
594 case X86::VMOVUPSZ128rm_NOVLX
:
595 case X86::VMOVUPSZ256rm_NOVLX
:
596 case X86::VMOVUPSZrm
: {
597 // Loads from constant pools are trivially rematerializable.
598 if (MI
.getOperand(1 + X86::AddrBaseReg
).isReg() &&
599 MI
.getOperand(1 + X86::AddrScaleAmt
).isImm() &&
600 MI
.getOperand(1 + X86::AddrIndexReg
).isReg() &&
601 MI
.getOperand(1 + X86::AddrIndexReg
).getReg() == 0 &&
602 MI
.isDereferenceableInvariantLoad(AA
)) {
603 Register BaseReg
= MI
.getOperand(1 + X86::AddrBaseReg
).getReg();
604 if (BaseReg
== 0 || BaseReg
== X86::RIP
)
606 // Allow re-materialization of PIC load.
607 if (!ReMatPICStubLoad
&& MI
.getOperand(1 + X86::AddrDisp
).isGlobal())
609 const MachineFunction
&MF
= *MI
.getParent()->getParent();
610 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
611 return regIsPICBase(BaseReg
, MRI
);
618 if (MI
.getOperand(1 + X86::AddrScaleAmt
).isImm() &&
619 MI
.getOperand(1 + X86::AddrIndexReg
).isReg() &&
620 MI
.getOperand(1 + X86::AddrIndexReg
).getReg() == 0 &&
621 !MI
.getOperand(1 + X86::AddrDisp
).isReg()) {
622 // lea fi#, lea GV, etc. are all rematerializable.
623 if (!MI
.getOperand(1 + X86::AddrBaseReg
).isReg())
625 Register BaseReg
= MI
.getOperand(1 + X86::AddrBaseReg
).getReg();
628 // Allow re-materialization of lea PICBase + x.
629 const MachineFunction
&MF
= *MI
.getParent()->getParent();
630 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
631 return regIsPICBase(BaseReg
, MRI
);
638 void X86InstrInfo::reMaterialize(MachineBasicBlock
&MBB
,
639 MachineBasicBlock::iterator I
,
640 unsigned DestReg
, unsigned SubIdx
,
641 const MachineInstr
&Orig
,
642 const TargetRegisterInfo
&TRI
) const {
643 bool ClobbersEFLAGS
= Orig
.modifiesRegister(X86::EFLAGS
, &TRI
);
644 if (ClobbersEFLAGS
&& !isSafeToClobberEFLAGS(MBB
, I
)) {
645 // The instruction clobbers EFLAGS. Re-materialize as MOV32ri to avoid side
648 switch (Orig
.getOpcode()) {
649 case X86::MOV32r0
: Value
= 0; break;
650 case X86::MOV32r1
: Value
= 1; break;
651 case X86::MOV32r_1
: Value
= -1; break;
653 llvm_unreachable("Unexpected instruction!");
656 const DebugLoc
&DL
= Orig
.getDebugLoc();
657 BuildMI(MBB
, I
, DL
, get(X86::MOV32ri
))
658 .add(Orig
.getOperand(0))
661 MachineInstr
*MI
= MBB
.getParent()->CloneMachineInstr(&Orig
);
665 MachineInstr
&NewMI
= *std::prev(I
);
666 NewMI
.substituteRegister(Orig
.getOperand(0).getReg(), DestReg
, SubIdx
, TRI
);
669 /// True if MI has a condition code def, e.g. EFLAGS, that is not marked dead.
670 bool X86InstrInfo::hasLiveCondCodeDef(MachineInstr
&MI
) const {
671 for (unsigned i
= 0, e
= MI
.getNumOperands(); i
!= e
; ++i
) {
672 MachineOperand
&MO
= MI
.getOperand(i
);
673 if (MO
.isReg() && MO
.isDef() &&
674 MO
.getReg() == X86::EFLAGS
&& !MO
.isDead()) {
681 /// Check whether the shift count for a machine operand is non-zero.
682 inline static unsigned getTruncatedShiftCount(const MachineInstr
&MI
,
683 unsigned ShiftAmtOperandIdx
) {
684 // The shift count is six bits with the REX.W prefix and five bits without.
685 unsigned ShiftCountMask
= (MI
.getDesc().TSFlags
& X86II::REX_W
) ? 63 : 31;
686 unsigned Imm
= MI
.getOperand(ShiftAmtOperandIdx
).getImm();
687 return Imm
& ShiftCountMask
;
690 /// Check whether the given shift count is appropriate
691 /// can be represented by a LEA instruction.
692 inline static bool isTruncatedShiftCountForLEA(unsigned ShAmt
) {
693 // Left shift instructions can be transformed into load-effective-address
694 // instructions if we can encode them appropriately.
695 // A LEA instruction utilizes a SIB byte to encode its scale factor.
696 // The SIB.scale field is two bits wide which means that we can encode any
697 // shift amount less than 4.
698 return ShAmt
< 4 && ShAmt
> 0;
701 bool X86InstrInfo::classifyLEAReg(MachineInstr
&MI
, const MachineOperand
&Src
,
702 unsigned Opc
, bool AllowSP
, Register
&NewSrc
,
703 bool &isKill
, MachineOperand
&ImplicitOp
,
704 LiveVariables
*LV
) const {
705 MachineFunction
&MF
= *MI
.getParent()->getParent();
706 const TargetRegisterClass
*RC
;
708 RC
= Opc
!= X86::LEA32r
? &X86::GR64RegClass
: &X86::GR32RegClass
;
710 RC
= Opc
!= X86::LEA32r
?
711 &X86::GR64_NOSPRegClass
: &X86::GR32_NOSPRegClass
;
713 Register SrcReg
= Src
.getReg();
715 // For both LEA64 and LEA32 the register already has essentially the right
716 // type (32-bit or 64-bit) we may just need to forbid SP.
717 if (Opc
!= X86::LEA64_32r
) {
719 isKill
= Src
.isKill();
720 assert(!Src
.isUndef() && "Undef op doesn't need optimization");
722 if (Register::isVirtualRegister(NewSrc
) &&
723 !MF
.getRegInfo().constrainRegClass(NewSrc
, RC
))
729 // This is for an LEA64_32r and incoming registers are 32-bit. One way or
730 // another we need to add 64-bit registers to the final MI.
731 if (Register::isPhysicalRegister(SrcReg
)) {
733 ImplicitOp
.setImplicit();
735 NewSrc
= getX86SubSuperRegister(Src
.getReg(), 64);
736 isKill
= Src
.isKill();
737 assert(!Src
.isUndef() && "Undef op doesn't need optimization");
739 // Virtual register of the wrong class, we have to create a temporary 64-bit
740 // vreg to feed into the LEA.
741 NewSrc
= MF
.getRegInfo().createVirtualRegister(RC
);
743 BuildMI(*MI
.getParent(), MI
, MI
.getDebugLoc(), get(TargetOpcode::COPY
))
744 .addReg(NewSrc
, RegState::Define
| RegState::Undef
, X86::sub_32bit
)
747 // Which is obviously going to be dead after we're done with it.
751 LV
->replaceKillInstruction(SrcReg
, MI
, *Copy
);
754 // We've set all the parameters without issue.
758 MachineInstr
*X86InstrInfo::convertToThreeAddressWithLEA(
759 unsigned MIOpc
, MachineFunction::iterator
&MFI
, MachineInstr
&MI
,
760 LiveVariables
*LV
, bool Is8BitOp
) const {
761 // We handle 8-bit adds and various 16-bit opcodes in the switch below.
762 MachineRegisterInfo
&RegInfo
= MFI
->getParent()->getRegInfo();
763 assert((Is8BitOp
|| RegInfo
.getTargetRegisterInfo()->getRegSizeInBits(
764 *RegInfo
.getRegClass(MI
.getOperand(0).getReg())) == 16) &&
765 "Unexpected type for LEA transform");
767 // TODO: For a 32-bit target, we need to adjust the LEA variables with
768 // something like this:
769 // Opcode = X86::LEA32r;
770 // InRegLEA = RegInfo.createVirtualRegister(&X86::GR32_NOSPRegClass);
772 // Is8BitOp ? RegInfo.createVirtualRegister(&X86::GR32ABCD_RegClass)
773 // : RegInfo.createVirtualRegister(&X86::GR32RegClass);
774 if (!Subtarget
.is64Bit())
777 unsigned Opcode
= X86::LEA64_32r
;
778 Register InRegLEA
= RegInfo
.createVirtualRegister(&X86::GR64_NOSPRegClass
);
779 Register OutRegLEA
= RegInfo
.createVirtualRegister(&X86::GR32RegClass
);
781 // Build and insert into an implicit UNDEF value. This is OK because
782 // we will be shifting and then extracting the lower 8/16-bits.
783 // This has the potential to cause partial register stall. e.g.
784 // movw (%rbp,%rcx,2), %dx
785 // leal -65(%rdx), %esi
786 // But testing has shown this *does* help performance in 64-bit mode (at
787 // least on modern x86 machines).
788 MachineBasicBlock::iterator MBBI
= MI
.getIterator();
789 Register Dest
= MI
.getOperand(0).getReg();
790 Register Src
= MI
.getOperand(1).getReg();
791 bool IsDead
= MI
.getOperand(0).isDead();
792 bool IsKill
= MI
.getOperand(1).isKill();
793 unsigned SubReg
= Is8BitOp
? X86::sub_8bit
: X86::sub_16bit
;
794 assert(!MI
.getOperand(1).isUndef() && "Undef op doesn't need optimization");
795 BuildMI(*MFI
, MBBI
, MI
.getDebugLoc(), get(X86::IMPLICIT_DEF
), InRegLEA
);
796 MachineInstr
*InsMI
=
797 BuildMI(*MFI
, MBBI
, MI
.getDebugLoc(), get(TargetOpcode::COPY
))
798 .addReg(InRegLEA
, RegState::Define
, SubReg
)
799 .addReg(Src
, getKillRegState(IsKill
));
801 MachineInstrBuilder MIB
=
802 BuildMI(*MFI
, MBBI
, MI
.getDebugLoc(), get(Opcode
), OutRegLEA
);
804 default: llvm_unreachable("Unreachable!");
807 unsigned ShAmt
= MI
.getOperand(2).getImm();
808 MIB
.addReg(0).addImm(1ULL << ShAmt
)
809 .addReg(InRegLEA
, RegState::Kill
).addImm(0).addReg(0);
814 addRegOffset(MIB
, InRegLEA
, true, 1);
818 addRegOffset(MIB
, InRegLEA
, true, -1);
824 case X86::ADD16ri_DB
:
825 case X86::ADD16ri8_DB
:
826 addRegOffset(MIB
, InRegLEA
, true, MI
.getOperand(2).getImm());
831 case X86::ADD16rr_DB
: {
832 Register Src2
= MI
.getOperand(2).getReg();
833 bool IsKill2
= MI
.getOperand(2).isKill();
834 assert(!MI
.getOperand(2).isUndef() && "Undef op doesn't need optimization");
835 unsigned InRegLEA2
= 0;
836 MachineInstr
*InsMI2
= nullptr;
838 // ADD8rr/ADD16rr killed %reg1028, %reg1028
839 // just a single insert_subreg.
840 addRegReg(MIB
, InRegLEA
, true, InRegLEA
, false);
842 if (Subtarget
.is64Bit())
843 InRegLEA2
= RegInfo
.createVirtualRegister(&X86::GR64_NOSPRegClass
);
845 InRegLEA2
= RegInfo
.createVirtualRegister(&X86::GR32_NOSPRegClass
);
846 // Build and insert into an implicit UNDEF value. This is OK because
847 // we will be shifting and then extracting the lower 8/16-bits.
848 BuildMI(*MFI
, &*MIB
, MI
.getDebugLoc(), get(X86::IMPLICIT_DEF
), InRegLEA2
);
849 InsMI2
= BuildMI(*MFI
, &*MIB
, MI
.getDebugLoc(), get(TargetOpcode::COPY
))
850 .addReg(InRegLEA2
, RegState::Define
, SubReg
)
851 .addReg(Src2
, getKillRegState(IsKill2
));
852 addRegReg(MIB
, InRegLEA
, true, InRegLEA2
, true);
854 if (LV
&& IsKill2
&& InsMI2
)
855 LV
->replaceKillInstruction(Src2
, MI
, *InsMI2
);
860 MachineInstr
*NewMI
= MIB
;
861 MachineInstr
*ExtMI
=
862 BuildMI(*MFI
, MBBI
, MI
.getDebugLoc(), get(TargetOpcode::COPY
))
863 .addReg(Dest
, RegState::Define
| getDeadRegState(IsDead
))
864 .addReg(OutRegLEA
, RegState::Kill
, SubReg
);
867 // Update live variables.
868 LV
->getVarInfo(InRegLEA
).Kills
.push_back(NewMI
);
869 LV
->getVarInfo(OutRegLEA
).Kills
.push_back(ExtMI
);
871 LV
->replaceKillInstruction(Src
, MI
, *InsMI
);
873 LV
->replaceKillInstruction(Dest
, MI
, *ExtMI
);
879 /// This method must be implemented by targets that
880 /// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
881 /// may be able to convert a two-address instruction into a true
882 /// three-address instruction on demand. This allows the X86 target (for
883 /// example) to convert ADD and SHL instructions into LEA instructions if they
884 /// would require register copies due to two-addressness.
886 /// This method returns a null pointer if the transformation cannot be
887 /// performed, otherwise it returns the new instruction.
890 X86InstrInfo::convertToThreeAddress(MachineFunction::iterator
&MFI
,
891 MachineInstr
&MI
, LiveVariables
*LV
) const {
892 // The following opcodes also sets the condition code register(s). Only
893 // convert them to equivalent lea if the condition code register def's
895 if (hasLiveCondCodeDef(MI
))
898 MachineFunction
&MF
= *MI
.getParent()->getParent();
899 // All instructions input are two-addr instructions. Get the known operands.
900 const MachineOperand
&Dest
= MI
.getOperand(0);
901 const MachineOperand
&Src
= MI
.getOperand(1);
903 // Ideally, operations with undef should be folded before we get here, but we
904 // can't guarantee it. Bail out because optimizing undefs is a waste of time.
905 // Without this, we have to forward undef state to new register operands to
906 // avoid machine verifier errors.
909 if (MI
.getNumOperands() > 2)
910 if (MI
.getOperand(2).isReg() && MI
.getOperand(2).isUndef())
913 MachineInstr
*NewMI
= nullptr;
914 bool Is64Bit
= Subtarget
.is64Bit();
916 bool Is8BitOp
= false;
917 unsigned MIOpc
= MI
.getOpcode();
919 default: llvm_unreachable("Unreachable!");
921 assert(MI
.getNumOperands() >= 3 && "Unknown shift instruction!");
922 unsigned ShAmt
= getTruncatedShiftCount(MI
, 2);
923 if (!isTruncatedShiftCountForLEA(ShAmt
)) return nullptr;
925 // LEA can't handle RSP.
926 if (Register::isVirtualRegister(Src
.getReg()) &&
927 !MF
.getRegInfo().constrainRegClass(Src
.getReg(),
928 &X86::GR64_NOSPRegClass
))
931 NewMI
= BuildMI(MF
, MI
.getDebugLoc(), get(X86::LEA64r
))
934 .addImm(1ULL << ShAmt
)
941 assert(MI
.getNumOperands() >= 3 && "Unknown shift instruction!");
942 unsigned ShAmt
= getTruncatedShiftCount(MI
, 2);
943 if (!isTruncatedShiftCountForLEA(ShAmt
)) return nullptr;
945 unsigned Opc
= Is64Bit
? X86::LEA64_32r
: X86::LEA32r
;
947 // LEA can't handle ESP.
950 MachineOperand ImplicitOp
= MachineOperand::CreateReg(0, false);
951 if (!classifyLEAReg(MI
, Src
, Opc
, /*AllowSP=*/ false,
952 SrcReg
, isKill
, ImplicitOp
, LV
))
955 MachineInstrBuilder MIB
=
956 BuildMI(MF
, MI
.getDebugLoc(), get(Opc
))
959 .addImm(1ULL << ShAmt
)
960 .addReg(SrcReg
, getKillRegState(isKill
))
963 if (ImplicitOp
.getReg() != 0)
973 assert(MI
.getNumOperands() >= 3 && "Unknown shift instruction!");
974 unsigned ShAmt
= getTruncatedShiftCount(MI
, 2);
975 if (!isTruncatedShiftCountForLEA(ShAmt
))
977 return convertToThreeAddressWithLEA(MIOpc
, MFI
, MI
, LV
, Is8BitOp
);
981 assert(MI
.getNumOperands() >= 2 && "Unknown inc instruction!");
982 unsigned Opc
= MIOpc
== X86::INC64r
? X86::LEA64r
:
983 (Is64Bit
? X86::LEA64_32r
: X86::LEA32r
);
986 MachineOperand ImplicitOp
= MachineOperand::CreateReg(0, false);
987 if (!classifyLEAReg(MI
, Src
, Opc
, /*AllowSP=*/ false, SrcReg
, isKill
,
991 MachineInstrBuilder MIB
=
992 BuildMI(MF
, MI
.getDebugLoc(), get(Opc
))
994 .addReg(SrcReg
, getKillRegState(isKill
));
995 if (ImplicitOp
.getReg() != 0)
998 NewMI
= addOffset(MIB
, 1);
1003 assert(MI
.getNumOperands() >= 2 && "Unknown dec instruction!");
1004 unsigned Opc
= MIOpc
== X86::DEC64r
? X86::LEA64r
1005 : (Is64Bit
? X86::LEA64_32r
: X86::LEA32r
);
1009 MachineOperand ImplicitOp
= MachineOperand::CreateReg(0, false);
1010 if (!classifyLEAReg(MI
, Src
, Opc
, /*AllowSP=*/ false, SrcReg
, isKill
,
1014 MachineInstrBuilder MIB
= BuildMI(MF
, MI
.getDebugLoc(), get(Opc
))
1016 .addReg(SrcReg
, getKillRegState(isKill
));
1017 if (ImplicitOp
.getReg() != 0)
1018 MIB
.add(ImplicitOp
);
1020 NewMI
= addOffset(MIB
, -1);
1030 return convertToThreeAddressWithLEA(MIOpc
, MFI
, MI
, LV
, Is8BitOp
);
1032 case X86::ADD64rr_DB
:
1034 case X86::ADD32rr_DB
: {
1035 assert(MI
.getNumOperands() >= 3 && "Unknown add instruction!");
1037 if (MIOpc
== X86::ADD64rr
|| MIOpc
== X86::ADD64rr_DB
)
1040 Opc
= Is64Bit
? X86::LEA64_32r
: X86::LEA32r
;
1044 MachineOperand ImplicitOp
= MachineOperand::CreateReg(0, false);
1045 if (!classifyLEAReg(MI
, Src
, Opc
, /*AllowSP=*/ true,
1046 SrcReg
, isKill
, ImplicitOp
, LV
))
1049 const MachineOperand
&Src2
= MI
.getOperand(2);
1052 MachineOperand ImplicitOp2
= MachineOperand::CreateReg(0, false);
1053 if (!classifyLEAReg(MI
, Src2
, Opc
, /*AllowSP=*/ false,
1054 SrcReg2
, isKill2
, ImplicitOp2
, LV
))
1057 MachineInstrBuilder MIB
= BuildMI(MF
, MI
.getDebugLoc(), get(Opc
)).add(Dest
);
1058 if (ImplicitOp
.getReg() != 0)
1059 MIB
.add(ImplicitOp
);
1060 if (ImplicitOp2
.getReg() != 0)
1061 MIB
.add(ImplicitOp2
);
1063 NewMI
= addRegReg(MIB
, SrcReg
, isKill
, SrcReg2
, isKill2
);
1064 if (LV
&& Src2
.isKill())
1065 LV
->replaceKillInstruction(SrcReg2
, MI
, *NewMI
);
1069 case X86::ADD8rr_DB
:
1073 case X86::ADD16rr_DB
:
1074 return convertToThreeAddressWithLEA(MIOpc
, MFI
, MI
, LV
, Is8BitOp
);
1075 case X86::ADD64ri32
:
1077 case X86::ADD64ri32_DB
:
1078 case X86::ADD64ri8_DB
:
1079 assert(MI
.getNumOperands() >= 3 && "Unknown add instruction!");
1081 BuildMI(MF
, MI
.getDebugLoc(), get(X86::LEA64r
)).add(Dest
).add(Src
),
1086 case X86::ADD32ri_DB
:
1087 case X86::ADD32ri8_DB
: {
1088 assert(MI
.getNumOperands() >= 3 && "Unknown add instruction!");
1089 unsigned Opc
= Is64Bit
? X86::LEA64_32r
: X86::LEA32r
;
1093 MachineOperand ImplicitOp
= MachineOperand::CreateReg(0, false);
1094 if (!classifyLEAReg(MI
, Src
, Opc
, /*AllowSP=*/ true,
1095 SrcReg
, isKill
, ImplicitOp
, LV
))
1098 MachineInstrBuilder MIB
= BuildMI(MF
, MI
.getDebugLoc(), get(Opc
))
1100 .addReg(SrcReg
, getKillRegState(isKill
));
1101 if (ImplicitOp
.getReg() != 0)
1102 MIB
.add(ImplicitOp
);
1104 NewMI
= addOffset(MIB
, MI
.getOperand(2));
1108 case X86::ADD8ri_DB
:
1113 case X86::ADD16ri_DB
:
1114 case X86::ADD16ri8_DB
:
1115 return convertToThreeAddressWithLEA(MIOpc
, MFI
, MI
, LV
, Is8BitOp
);
1119 /// FIXME: Support these similar to ADD8ri/ADD16ri*.
1122 case X86::SUB32ri
: {
1123 int64_t Imm
= MI
.getOperand(2).getImm();
1124 if (!isInt
<32>(-Imm
))
1127 assert(MI
.getNumOperands() >= 3 && "Unknown add instruction!");
1128 unsigned Opc
= Is64Bit
? X86::LEA64_32r
: X86::LEA32r
;
1132 MachineOperand ImplicitOp
= MachineOperand::CreateReg(0, false);
1133 if (!classifyLEAReg(MI
, Src
, Opc
, /*AllowSP=*/ true,
1134 SrcReg
, isKill
, ImplicitOp
, LV
))
1137 MachineInstrBuilder MIB
= BuildMI(MF
, MI
.getDebugLoc(), get(Opc
))
1139 .addReg(SrcReg
, getKillRegState(isKill
));
1140 if (ImplicitOp
.getReg() != 0)
1141 MIB
.add(ImplicitOp
);
1143 NewMI
= addOffset(MIB
, -Imm
);
1148 case X86::SUB64ri32
: {
1149 int64_t Imm
= MI
.getOperand(2).getImm();
1150 if (!isInt
<32>(-Imm
))
1153 assert(MI
.getNumOperands() >= 3 && "Unknown sub instruction!");
1155 MachineInstrBuilder MIB
= BuildMI(MF
, MI
.getDebugLoc(),
1156 get(X86::LEA64r
)).add(Dest
).add(Src
);
1157 NewMI
= addOffset(MIB
, -Imm
);
1161 case X86::VMOVDQU8Z128rmk
:
1162 case X86::VMOVDQU8Z256rmk
:
1163 case X86::VMOVDQU8Zrmk
:
1164 case X86::VMOVDQU16Z128rmk
:
1165 case X86::VMOVDQU16Z256rmk
:
1166 case X86::VMOVDQU16Zrmk
:
1167 case X86::VMOVDQU32Z128rmk
: case X86::VMOVDQA32Z128rmk
:
1168 case X86::VMOVDQU32Z256rmk
: case X86::VMOVDQA32Z256rmk
:
1169 case X86::VMOVDQU32Zrmk
: case X86::VMOVDQA32Zrmk
:
1170 case X86::VMOVDQU64Z128rmk
: case X86::VMOVDQA64Z128rmk
:
1171 case X86::VMOVDQU64Z256rmk
: case X86::VMOVDQA64Z256rmk
:
1172 case X86::VMOVDQU64Zrmk
: case X86::VMOVDQA64Zrmk
:
1173 case X86::VMOVUPDZ128rmk
: case X86::VMOVAPDZ128rmk
:
1174 case X86::VMOVUPDZ256rmk
: case X86::VMOVAPDZ256rmk
:
1175 case X86::VMOVUPDZrmk
: case X86::VMOVAPDZrmk
:
1176 case X86::VMOVUPSZ128rmk
: case X86::VMOVAPSZ128rmk
:
1177 case X86::VMOVUPSZ256rmk
: case X86::VMOVAPSZ256rmk
:
1178 case X86::VMOVUPSZrmk
: case X86::VMOVAPSZrmk
: {
1181 default: llvm_unreachable("Unreachable!");
1182 case X86::VMOVDQU8Z128rmk
: Opc
= X86::VPBLENDMBZ128rmk
; break;
1183 case X86::VMOVDQU8Z256rmk
: Opc
= X86::VPBLENDMBZ256rmk
; break;
1184 case X86::VMOVDQU8Zrmk
: Opc
= X86::VPBLENDMBZrmk
; break;
1185 case X86::VMOVDQU16Z128rmk
: Opc
= X86::VPBLENDMWZ128rmk
; break;
1186 case X86::VMOVDQU16Z256rmk
: Opc
= X86::VPBLENDMWZ256rmk
; break;
1187 case X86::VMOVDQU16Zrmk
: Opc
= X86::VPBLENDMWZrmk
; break;
1188 case X86::VMOVDQU32Z128rmk
: Opc
= X86::VPBLENDMDZ128rmk
; break;
1189 case X86::VMOVDQU32Z256rmk
: Opc
= X86::VPBLENDMDZ256rmk
; break;
1190 case X86::VMOVDQU32Zrmk
: Opc
= X86::VPBLENDMDZrmk
; break;
1191 case X86::VMOVDQU64Z128rmk
: Opc
= X86::VPBLENDMQZ128rmk
; break;
1192 case X86::VMOVDQU64Z256rmk
: Opc
= X86::VPBLENDMQZ256rmk
; break;
1193 case X86::VMOVDQU64Zrmk
: Opc
= X86::VPBLENDMQZrmk
; break;
1194 case X86::VMOVUPDZ128rmk
: Opc
= X86::VBLENDMPDZ128rmk
; break;
1195 case X86::VMOVUPDZ256rmk
: Opc
= X86::VBLENDMPDZ256rmk
; break;
1196 case X86::VMOVUPDZrmk
: Opc
= X86::VBLENDMPDZrmk
; break;
1197 case X86::VMOVUPSZ128rmk
: Opc
= X86::VBLENDMPSZ128rmk
; break;
1198 case X86::VMOVUPSZ256rmk
: Opc
= X86::VBLENDMPSZ256rmk
; break;
1199 case X86::VMOVUPSZrmk
: Opc
= X86::VBLENDMPSZrmk
; break;
1200 case X86::VMOVDQA32Z128rmk
: Opc
= X86::VPBLENDMDZ128rmk
; break;
1201 case X86::VMOVDQA32Z256rmk
: Opc
= X86::VPBLENDMDZ256rmk
; break;
1202 case X86::VMOVDQA32Zrmk
: Opc
= X86::VPBLENDMDZrmk
; break;
1203 case X86::VMOVDQA64Z128rmk
: Opc
= X86::VPBLENDMQZ128rmk
; break;
1204 case X86::VMOVDQA64Z256rmk
: Opc
= X86::VPBLENDMQZ256rmk
; break;
1205 case X86::VMOVDQA64Zrmk
: Opc
= X86::VPBLENDMQZrmk
; break;
1206 case X86::VMOVAPDZ128rmk
: Opc
= X86::VBLENDMPDZ128rmk
; break;
1207 case X86::VMOVAPDZ256rmk
: Opc
= X86::VBLENDMPDZ256rmk
; break;
1208 case X86::VMOVAPDZrmk
: Opc
= X86::VBLENDMPDZrmk
; break;
1209 case X86::VMOVAPSZ128rmk
: Opc
= X86::VBLENDMPSZ128rmk
; break;
1210 case X86::VMOVAPSZ256rmk
: Opc
= X86::VBLENDMPSZ256rmk
; break;
1211 case X86::VMOVAPSZrmk
: Opc
= X86::VBLENDMPSZrmk
; break;
1214 NewMI
= BuildMI(MF
, MI
.getDebugLoc(), get(Opc
))
1216 .add(MI
.getOperand(2))
1218 .add(MI
.getOperand(3))
1219 .add(MI
.getOperand(4))
1220 .add(MI
.getOperand(5))
1221 .add(MI
.getOperand(6))
1222 .add(MI
.getOperand(7));
1225 case X86::VMOVDQU8Z128rrk
:
1226 case X86::VMOVDQU8Z256rrk
:
1227 case X86::VMOVDQU8Zrrk
:
1228 case X86::VMOVDQU16Z128rrk
:
1229 case X86::VMOVDQU16Z256rrk
:
1230 case X86::VMOVDQU16Zrrk
:
1231 case X86::VMOVDQU32Z128rrk
: case X86::VMOVDQA32Z128rrk
:
1232 case X86::VMOVDQU32Z256rrk
: case X86::VMOVDQA32Z256rrk
:
1233 case X86::VMOVDQU32Zrrk
: case X86::VMOVDQA32Zrrk
:
1234 case X86::VMOVDQU64Z128rrk
: case X86::VMOVDQA64Z128rrk
:
1235 case X86::VMOVDQU64Z256rrk
: case X86::VMOVDQA64Z256rrk
:
1236 case X86::VMOVDQU64Zrrk
: case X86::VMOVDQA64Zrrk
:
1237 case X86::VMOVUPDZ128rrk
: case X86::VMOVAPDZ128rrk
:
1238 case X86::VMOVUPDZ256rrk
: case X86::VMOVAPDZ256rrk
:
1239 case X86::VMOVUPDZrrk
: case X86::VMOVAPDZrrk
:
1240 case X86::VMOVUPSZ128rrk
: case X86::VMOVAPSZ128rrk
:
1241 case X86::VMOVUPSZ256rrk
: case X86::VMOVAPSZ256rrk
:
1242 case X86::VMOVUPSZrrk
: case X86::VMOVAPSZrrk
: {
1245 default: llvm_unreachable("Unreachable!");
1246 case X86::VMOVDQU8Z128rrk
: Opc
= X86::VPBLENDMBZ128rrk
; break;
1247 case X86::VMOVDQU8Z256rrk
: Opc
= X86::VPBLENDMBZ256rrk
; break;
1248 case X86::VMOVDQU8Zrrk
: Opc
= X86::VPBLENDMBZrrk
; break;
1249 case X86::VMOVDQU16Z128rrk
: Opc
= X86::VPBLENDMWZ128rrk
; break;
1250 case X86::VMOVDQU16Z256rrk
: Opc
= X86::VPBLENDMWZ256rrk
; break;
1251 case X86::VMOVDQU16Zrrk
: Opc
= X86::VPBLENDMWZrrk
; break;
1252 case X86::VMOVDQU32Z128rrk
: Opc
= X86::VPBLENDMDZ128rrk
; break;
1253 case X86::VMOVDQU32Z256rrk
: Opc
= X86::VPBLENDMDZ256rrk
; break;
1254 case X86::VMOVDQU32Zrrk
: Opc
= X86::VPBLENDMDZrrk
; break;
1255 case X86::VMOVDQU64Z128rrk
: Opc
= X86::VPBLENDMQZ128rrk
; break;
1256 case X86::VMOVDQU64Z256rrk
: Opc
= X86::VPBLENDMQZ256rrk
; break;
1257 case X86::VMOVDQU64Zrrk
: Opc
= X86::VPBLENDMQZrrk
; break;
1258 case X86::VMOVUPDZ128rrk
: Opc
= X86::VBLENDMPDZ128rrk
; break;
1259 case X86::VMOVUPDZ256rrk
: Opc
= X86::VBLENDMPDZ256rrk
; break;
1260 case X86::VMOVUPDZrrk
: Opc
= X86::VBLENDMPDZrrk
; break;
1261 case X86::VMOVUPSZ128rrk
: Opc
= X86::VBLENDMPSZ128rrk
; break;
1262 case X86::VMOVUPSZ256rrk
: Opc
= X86::VBLENDMPSZ256rrk
; break;
1263 case X86::VMOVUPSZrrk
: Opc
= X86::VBLENDMPSZrrk
; break;
1264 case X86::VMOVDQA32Z128rrk
: Opc
= X86::VPBLENDMDZ128rrk
; break;
1265 case X86::VMOVDQA32Z256rrk
: Opc
= X86::VPBLENDMDZ256rrk
; break;
1266 case X86::VMOVDQA32Zrrk
: Opc
= X86::VPBLENDMDZrrk
; break;
1267 case X86::VMOVDQA64Z128rrk
: Opc
= X86::VPBLENDMQZ128rrk
; break;
1268 case X86::VMOVDQA64Z256rrk
: Opc
= X86::VPBLENDMQZ256rrk
; break;
1269 case X86::VMOVDQA64Zrrk
: Opc
= X86::VPBLENDMQZrrk
; break;
1270 case X86::VMOVAPDZ128rrk
: Opc
= X86::VBLENDMPDZ128rrk
; break;
1271 case X86::VMOVAPDZ256rrk
: Opc
= X86::VBLENDMPDZ256rrk
; break;
1272 case X86::VMOVAPDZrrk
: Opc
= X86::VBLENDMPDZrrk
; break;
1273 case X86::VMOVAPSZ128rrk
: Opc
= X86::VBLENDMPSZ128rrk
; break;
1274 case X86::VMOVAPSZ256rrk
: Opc
= X86::VBLENDMPSZ256rrk
; break;
1275 case X86::VMOVAPSZrrk
: Opc
= X86::VBLENDMPSZrrk
; break;
1278 NewMI
= BuildMI(MF
, MI
.getDebugLoc(), get(Opc
))
1280 .add(MI
.getOperand(2))
1282 .add(MI
.getOperand(3));
1287 if (!NewMI
) return nullptr;
1289 if (LV
) { // Update live variables
1291 LV
->replaceKillInstruction(Src
.getReg(), MI
, *NewMI
);
1293 LV
->replaceKillInstruction(Dest
.getReg(), MI
, *NewMI
);
1296 MFI
->insert(MI
.getIterator(), NewMI
); // Insert the new inst
1300 /// This determines which of three possible cases of a three source commute
1301 /// the source indexes correspond to taking into account any mask operands.
1302 /// All prevents commuting a passthru operand. Returns -1 if the commute isn't
1304 /// Case 0 - Possible to commute the first and second operands.
1305 /// Case 1 - Possible to commute the first and third operands.
1306 /// Case 2 - Possible to commute the second and third operands.
1307 static unsigned getThreeSrcCommuteCase(uint64_t TSFlags
, unsigned SrcOpIdx1
,
1308 unsigned SrcOpIdx2
) {
1309 // Put the lowest index to SrcOpIdx1 to simplify the checks below.
1310 if (SrcOpIdx1
> SrcOpIdx2
)
1311 std::swap(SrcOpIdx1
, SrcOpIdx2
);
1313 unsigned Op1
= 1, Op2
= 2, Op3
= 3;
1314 if (X86II::isKMasked(TSFlags
)) {
1319 if (SrcOpIdx1
== Op1
&& SrcOpIdx2
== Op2
)
1321 if (SrcOpIdx1
== Op1
&& SrcOpIdx2
== Op3
)
1323 if (SrcOpIdx1
== Op2
&& SrcOpIdx2
== Op3
)
1325 llvm_unreachable("Unknown three src commute case.");
1328 unsigned X86InstrInfo::getFMA3OpcodeToCommuteOperands(
1329 const MachineInstr
&MI
, unsigned SrcOpIdx1
, unsigned SrcOpIdx2
,
1330 const X86InstrFMA3Group
&FMA3Group
) const {
1332 unsigned Opc
= MI
.getOpcode();
1334 // TODO: Commuting the 1st operand of FMA*_Int requires some additional
1335 // analysis. The commute optimization is legal only if all users of FMA*_Int
1336 // use only the lowest element of the FMA*_Int instruction. Such analysis are
1337 // not implemented yet. So, just return 0 in that case.
1338 // When such analysis are available this place will be the right place for
1340 assert(!(FMA3Group
.isIntrinsic() && (SrcOpIdx1
== 1 || SrcOpIdx2
== 1)) &&
1341 "Intrinsic instructions can't commute operand 1");
1343 // Determine which case this commute is or if it can't be done.
1344 unsigned Case
= getThreeSrcCommuteCase(MI
.getDesc().TSFlags
, SrcOpIdx1
,
1346 assert(Case
< 3 && "Unexpected case number!");
1348 // Define the FMA forms mapping array that helps to map input FMA form
1349 // to output FMA form to preserve the operation semantics after
1350 // commuting the operands.
1351 const unsigned Form132Index
= 0;
1352 const unsigned Form213Index
= 1;
1353 const unsigned Form231Index
= 2;
1354 static const unsigned FormMapping
[][3] = {
1355 // 0: SrcOpIdx1 == 1 && SrcOpIdx2 == 2;
1356 // FMA132 A, C, b; ==> FMA231 C, A, b;
1357 // FMA213 B, A, c; ==> FMA213 A, B, c;
1358 // FMA231 C, A, b; ==> FMA132 A, C, b;
1359 { Form231Index
, Form213Index
, Form132Index
},
1360 // 1: SrcOpIdx1 == 1 && SrcOpIdx2 == 3;
1361 // FMA132 A, c, B; ==> FMA132 B, c, A;
1362 // FMA213 B, a, C; ==> FMA231 C, a, B;
1363 // FMA231 C, a, B; ==> FMA213 B, a, C;
1364 { Form132Index
, Form231Index
, Form213Index
},
1365 // 2: SrcOpIdx1 == 2 && SrcOpIdx2 == 3;
1366 // FMA132 a, C, B; ==> FMA213 a, B, C;
1367 // FMA213 b, A, C; ==> FMA132 b, C, A;
1368 // FMA231 c, A, B; ==> FMA231 c, B, A;
1369 { Form213Index
, Form132Index
, Form231Index
}
1372 unsigned FMAForms
[3];
1373 FMAForms
[0] = FMA3Group
.get132Opcode();
1374 FMAForms
[1] = FMA3Group
.get213Opcode();
1375 FMAForms
[2] = FMA3Group
.get231Opcode();
1377 for (FormIndex
= 0; FormIndex
< 3; FormIndex
++)
1378 if (Opc
== FMAForms
[FormIndex
])
1381 // Everything is ready, just adjust the FMA opcode and return it.
1382 FormIndex
= FormMapping
[Case
][FormIndex
];
1383 return FMAForms
[FormIndex
];
1386 static void commuteVPTERNLOG(MachineInstr
&MI
, unsigned SrcOpIdx1
,
1387 unsigned SrcOpIdx2
) {
1388 // Determine which case this commute is or if it can't be done.
1389 unsigned Case
= getThreeSrcCommuteCase(MI
.getDesc().TSFlags
, SrcOpIdx1
,
1391 assert(Case
< 3 && "Unexpected case value!");
1393 // For each case we need to swap two pairs of bits in the final immediate.
1394 static const uint8_t SwapMasks
[3][4] = {
1395 { 0x04, 0x10, 0x08, 0x20 }, // Swap bits 2/4 and 3/5.
1396 { 0x02, 0x10, 0x08, 0x40 }, // Swap bits 1/4 and 3/6.
1397 { 0x02, 0x04, 0x20, 0x40 }, // Swap bits 1/2 and 5/6.
1400 uint8_t Imm
= MI
.getOperand(MI
.getNumOperands()-1).getImm();
1401 // Clear out the bits we are swapping.
1402 uint8_t NewImm
= Imm
& ~(SwapMasks
[Case
][0] | SwapMasks
[Case
][1] |
1403 SwapMasks
[Case
][2] | SwapMasks
[Case
][3]);
1404 // If the immediate had a bit of the pair set, then set the opposite bit.
1405 if (Imm
& SwapMasks
[Case
][0]) NewImm
|= SwapMasks
[Case
][1];
1406 if (Imm
& SwapMasks
[Case
][1]) NewImm
|= SwapMasks
[Case
][0];
1407 if (Imm
& SwapMasks
[Case
][2]) NewImm
|= SwapMasks
[Case
][3];
1408 if (Imm
& SwapMasks
[Case
][3]) NewImm
|= SwapMasks
[Case
][2];
1409 MI
.getOperand(MI
.getNumOperands()-1).setImm(NewImm
);
1412 // Returns true if this is a VPERMI2 or VPERMT2 instruction that can be
1414 static bool isCommutableVPERMV3Instruction(unsigned Opcode
) {
1415 #define VPERM_CASES(Suffix) \
1416 case X86::VPERMI2##Suffix##128rr: case X86::VPERMT2##Suffix##128rr: \
1417 case X86::VPERMI2##Suffix##256rr: case X86::VPERMT2##Suffix##256rr: \
1418 case X86::VPERMI2##Suffix##rr: case X86::VPERMT2##Suffix##rr: \
1419 case X86::VPERMI2##Suffix##128rm: case X86::VPERMT2##Suffix##128rm: \
1420 case X86::VPERMI2##Suffix##256rm: case X86::VPERMT2##Suffix##256rm: \
1421 case X86::VPERMI2##Suffix##rm: case X86::VPERMT2##Suffix##rm: \
1422 case X86::VPERMI2##Suffix##128rrkz: case X86::VPERMT2##Suffix##128rrkz: \
1423 case X86::VPERMI2##Suffix##256rrkz: case X86::VPERMT2##Suffix##256rrkz: \
1424 case X86::VPERMI2##Suffix##rrkz: case X86::VPERMT2##Suffix##rrkz: \
1425 case X86::VPERMI2##Suffix##128rmkz: case X86::VPERMT2##Suffix##128rmkz: \
1426 case X86::VPERMI2##Suffix##256rmkz: case X86::VPERMT2##Suffix##256rmkz: \
1427 case X86::VPERMI2##Suffix##rmkz: case X86::VPERMT2##Suffix##rmkz:
1429 #define VPERM_CASES_BROADCAST(Suffix) \
1430 VPERM_CASES(Suffix) \
1431 case X86::VPERMI2##Suffix##128rmb: case X86::VPERMT2##Suffix##128rmb: \
1432 case X86::VPERMI2##Suffix##256rmb: case X86::VPERMT2##Suffix##256rmb: \
1433 case X86::VPERMI2##Suffix##rmb: case X86::VPERMT2##Suffix##rmb: \
1434 case X86::VPERMI2##Suffix##128rmbkz: case X86::VPERMT2##Suffix##128rmbkz: \
1435 case X86::VPERMI2##Suffix##256rmbkz: case X86::VPERMT2##Suffix##256rmbkz: \
1436 case X86::VPERMI2##Suffix##rmbkz: case X86::VPERMT2##Suffix##rmbkz:
1439 default: return false;
1441 VPERM_CASES_BROADCAST(D
)
1442 VPERM_CASES_BROADCAST(PD
)
1443 VPERM_CASES_BROADCAST(PS
)
1444 VPERM_CASES_BROADCAST(Q
)
1448 #undef VPERM_CASES_BROADCAST
1452 // Returns commuted opcode for VPERMI2 and VPERMT2 instructions by switching
1453 // from the I opcode to the T opcode and vice versa.
1454 static unsigned getCommutedVPERMV3Opcode(unsigned Opcode
) {
1455 #define VPERM_CASES(Orig, New) \
1456 case X86::Orig##128rr: return X86::New##128rr; \
1457 case X86::Orig##128rrkz: return X86::New##128rrkz; \
1458 case X86::Orig##128rm: return X86::New##128rm; \
1459 case X86::Orig##128rmkz: return X86::New##128rmkz; \
1460 case X86::Orig##256rr: return X86::New##256rr; \
1461 case X86::Orig##256rrkz: return X86::New##256rrkz; \
1462 case X86::Orig##256rm: return X86::New##256rm; \
1463 case X86::Orig##256rmkz: return X86::New##256rmkz; \
1464 case X86::Orig##rr: return X86::New##rr; \
1465 case X86::Orig##rrkz: return X86::New##rrkz; \
1466 case X86::Orig##rm: return X86::New##rm; \
1467 case X86::Orig##rmkz: return X86::New##rmkz;
1469 #define VPERM_CASES_BROADCAST(Orig, New) \
1470 VPERM_CASES(Orig, New) \
1471 case X86::Orig##128rmb: return X86::New##128rmb; \
1472 case X86::Orig##128rmbkz: return X86::New##128rmbkz; \
1473 case X86::Orig##256rmb: return X86::New##256rmb; \
1474 case X86::Orig##256rmbkz: return X86::New##256rmbkz; \
1475 case X86::Orig##rmb: return X86::New##rmb; \
1476 case X86::Orig##rmbkz: return X86::New##rmbkz;
1479 VPERM_CASES(VPERMI2B
, VPERMT2B
)
1480 VPERM_CASES_BROADCAST(VPERMI2D
, VPERMT2D
)
1481 VPERM_CASES_BROADCAST(VPERMI2PD
, VPERMT2PD
)
1482 VPERM_CASES_BROADCAST(VPERMI2PS
, VPERMT2PS
)
1483 VPERM_CASES_BROADCAST(VPERMI2Q
, VPERMT2Q
)
1484 VPERM_CASES(VPERMI2W
, VPERMT2W
)
1485 VPERM_CASES(VPERMT2B
, VPERMI2B
)
1486 VPERM_CASES_BROADCAST(VPERMT2D
, VPERMI2D
)
1487 VPERM_CASES_BROADCAST(VPERMT2PD
, VPERMI2PD
)
1488 VPERM_CASES_BROADCAST(VPERMT2PS
, VPERMI2PS
)
1489 VPERM_CASES_BROADCAST(VPERMT2Q
, VPERMI2Q
)
1490 VPERM_CASES(VPERMT2W
, VPERMI2W
)
1493 llvm_unreachable("Unreachable!");
1494 #undef VPERM_CASES_BROADCAST
1498 MachineInstr
*X86InstrInfo::commuteInstructionImpl(MachineInstr
&MI
, bool NewMI
,
1500 unsigned OpIdx2
) const {
1501 auto cloneIfNew
= [NewMI
](MachineInstr
&MI
) -> MachineInstr
& {
1503 return *MI
.getParent()->getParent()->CloneMachineInstr(&MI
);
1507 switch (MI
.getOpcode()) {
1508 case X86::SHRD16rri8
: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
1509 case X86::SHLD16rri8
: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
1510 case X86::SHRD32rri8
: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
1511 case X86::SHLD32rri8
: // A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
1512 case X86::SHRD64rri8
: // A = SHRD64rri8 B, C, I -> A = SHLD64rri8 C, B, (64-I)
1513 case X86::SHLD64rri8
:{// A = SHLD64rri8 B, C, I -> A = SHRD64rri8 C, B, (64-I)
1516 switch (MI
.getOpcode()) {
1517 default: llvm_unreachable("Unreachable!");
1518 case X86::SHRD16rri8
: Size
= 16; Opc
= X86::SHLD16rri8
; break;
1519 case X86::SHLD16rri8
: Size
= 16; Opc
= X86::SHRD16rri8
; break;
1520 case X86::SHRD32rri8
: Size
= 32; Opc
= X86::SHLD32rri8
; break;
1521 case X86::SHLD32rri8
: Size
= 32; Opc
= X86::SHRD32rri8
; break;
1522 case X86::SHRD64rri8
: Size
= 64; Opc
= X86::SHLD64rri8
; break;
1523 case X86::SHLD64rri8
: Size
= 64; Opc
= X86::SHRD64rri8
; break;
1525 unsigned Amt
= MI
.getOperand(3).getImm();
1526 auto &WorkingMI
= cloneIfNew(MI
);
1527 WorkingMI
.setDesc(get(Opc
));
1528 WorkingMI
.getOperand(3).setImm(Size
- Amt
);
1529 return TargetInstrInfo::commuteInstructionImpl(WorkingMI
, /*NewMI=*/false,
1533 case X86::PFSUBRrr
: {
1534 // PFSUB x, y: x = x - y
1535 // PFSUBR x, y: x = y - x
1537 (X86::PFSUBRrr
== MI
.getOpcode() ? X86::PFSUBrr
: X86::PFSUBRrr
);
1538 auto &WorkingMI
= cloneIfNew(MI
);
1539 WorkingMI
.setDesc(get(Opc
));
1540 return TargetInstrInfo::commuteInstructionImpl(WorkingMI
, /*NewMI=*/false,
1543 case X86::BLENDPDrri
:
1544 case X86::BLENDPSrri
:
1545 case X86::VBLENDPDrri
:
1546 case X86::VBLENDPSrri
:
1547 // If we're optimizing for size, try to use MOVSD/MOVSS.
1548 if (MI
.getParent()->getParent()->getFunction().hasOptSize()) {
1550 switch (MI
.getOpcode()) {
1551 default: llvm_unreachable("Unreachable!");
1552 case X86::BLENDPDrri
: Opc
= X86::MOVSDrr
; Mask
= 0x03; break;
1553 case X86::BLENDPSrri
: Opc
= X86::MOVSSrr
; Mask
= 0x0F; break;
1554 case X86::VBLENDPDrri
: Opc
= X86::VMOVSDrr
; Mask
= 0x03; break;
1555 case X86::VBLENDPSrri
: Opc
= X86::VMOVSSrr
; Mask
= 0x0F; break;
1557 if ((MI
.getOperand(3).getImm() ^ Mask
) == 1) {
1558 auto &WorkingMI
= cloneIfNew(MI
);
1559 WorkingMI
.setDesc(get(Opc
));
1560 WorkingMI
.RemoveOperand(3);
1561 return TargetInstrInfo::commuteInstructionImpl(WorkingMI
,
1567 case X86::PBLENDWrri
:
1568 case X86::VBLENDPDYrri
:
1569 case X86::VBLENDPSYrri
:
1570 case X86::VPBLENDDrri
:
1571 case X86::VPBLENDWrri
:
1572 case X86::VPBLENDDYrri
:
1573 case X86::VPBLENDWYrri
:{
1575 switch (MI
.getOpcode()) {
1576 default: llvm_unreachable("Unreachable!");
1577 case X86::BLENDPDrri
: Mask
= (int8_t)0x03; break;
1578 case X86::BLENDPSrri
: Mask
= (int8_t)0x0F; break;
1579 case X86::PBLENDWrri
: Mask
= (int8_t)0xFF; break;
1580 case X86::VBLENDPDrri
: Mask
= (int8_t)0x03; break;
1581 case X86::VBLENDPSrri
: Mask
= (int8_t)0x0F; break;
1582 case X86::VBLENDPDYrri
: Mask
= (int8_t)0x0F; break;
1583 case X86::VBLENDPSYrri
: Mask
= (int8_t)0xFF; break;
1584 case X86::VPBLENDDrri
: Mask
= (int8_t)0x0F; break;
1585 case X86::VPBLENDWrri
: Mask
= (int8_t)0xFF; break;
1586 case X86::VPBLENDDYrri
: Mask
= (int8_t)0xFF; break;
1587 case X86::VPBLENDWYrri
: Mask
= (int8_t)0xFF; break;
1589 // Only the least significant bits of Imm are used.
1590 // Using int8_t to ensure it will be sign extended to the int64_t that
1591 // setImm takes in order to match isel behavior.
1592 int8_t Imm
= MI
.getOperand(3).getImm() & Mask
;
1593 auto &WorkingMI
= cloneIfNew(MI
);
1594 WorkingMI
.getOperand(3).setImm(Mask
^ Imm
);
1595 return TargetInstrInfo::commuteInstructionImpl(WorkingMI
, /*NewMI=*/false,
1598 case X86::INSERTPSrr
:
1599 case X86::VINSERTPSrr
:
1600 case X86::VINSERTPSZrr
: {
1601 unsigned Imm
= MI
.getOperand(MI
.getNumOperands() - 1).getImm();
1602 unsigned ZMask
= Imm
& 15;
1603 unsigned DstIdx
= (Imm
>> 4) & 3;
1604 unsigned SrcIdx
= (Imm
>> 6) & 3;
1606 // We can commute insertps if we zero 2 of the elements, the insertion is
1607 // "inline" and we don't override the insertion with a zero.
1608 if (DstIdx
== SrcIdx
&& (ZMask
& (1 << DstIdx
)) == 0 &&
1609 countPopulation(ZMask
) == 2) {
1610 unsigned AltIdx
= findFirstSet((ZMask
| (1 << DstIdx
)) ^ 15);
1611 assert(AltIdx
< 4 && "Illegal insertion index");
1612 unsigned AltImm
= (AltIdx
<< 6) | (AltIdx
<< 4) | ZMask
;
1613 auto &WorkingMI
= cloneIfNew(MI
);
1614 WorkingMI
.getOperand(MI
.getNumOperands() - 1).setImm(AltImm
);
1615 return TargetInstrInfo::commuteInstructionImpl(WorkingMI
, /*NewMI=*/false,
1623 case X86::VMOVSSrr
:{
1624 // On SSE41 or later we can commute a MOVSS/MOVSD to a BLENDPS/BLENDPD.
1625 if (Subtarget
.hasSSE41()) {
1627 switch (MI
.getOpcode()) {
1628 default: llvm_unreachable("Unreachable!");
1629 case X86::MOVSDrr
: Opc
= X86::BLENDPDrri
; Mask
= 0x02; break;
1630 case X86::MOVSSrr
: Opc
= X86::BLENDPSrri
; Mask
= 0x0E; break;
1631 case X86::VMOVSDrr
: Opc
= X86::VBLENDPDrri
; Mask
= 0x02; break;
1632 case X86::VMOVSSrr
: Opc
= X86::VBLENDPSrri
; Mask
= 0x0E; break;
1635 auto &WorkingMI
= cloneIfNew(MI
);
1636 WorkingMI
.setDesc(get(Opc
));
1637 WorkingMI
.addOperand(MachineOperand::CreateImm(Mask
));
1638 return TargetInstrInfo::commuteInstructionImpl(WorkingMI
, /*NewMI=*/false,
1642 // Convert to SHUFPD.
1643 assert(MI
.getOpcode() == X86::MOVSDrr
&&
1644 "Can only commute MOVSDrr without SSE4.1");
1646 auto &WorkingMI
= cloneIfNew(MI
);
1647 WorkingMI
.setDesc(get(X86::SHUFPDrri
));
1648 WorkingMI
.addOperand(MachineOperand::CreateImm(0x02));
1649 return TargetInstrInfo::commuteInstructionImpl(WorkingMI
, /*NewMI=*/false,
1652 case X86::SHUFPDrri
: {
1653 // Commute to MOVSD.
1654 assert(MI
.getOperand(3).getImm() == 0x02 && "Unexpected immediate!");
1655 auto &WorkingMI
= cloneIfNew(MI
);
1656 WorkingMI
.setDesc(get(X86::MOVSDrr
));
1657 WorkingMI
.RemoveOperand(3);
1658 return TargetInstrInfo::commuteInstructionImpl(WorkingMI
, /*NewMI=*/false,
1661 case X86::PCLMULQDQrr
:
1662 case X86::VPCLMULQDQrr
:
1663 case X86::VPCLMULQDQYrr
:
1664 case X86::VPCLMULQDQZrr
:
1665 case X86::VPCLMULQDQZ128rr
:
1666 case X86::VPCLMULQDQZ256rr
: {
1667 // SRC1 64bits = Imm[0] ? SRC1[127:64] : SRC1[63:0]
1668 // SRC2 64bits = Imm[4] ? SRC2[127:64] : SRC2[63:0]
1669 unsigned Imm
= MI
.getOperand(3).getImm();
1670 unsigned Src1Hi
= Imm
& 0x01;
1671 unsigned Src2Hi
= Imm
& 0x10;
1672 auto &WorkingMI
= cloneIfNew(MI
);
1673 WorkingMI
.getOperand(3).setImm((Src1Hi
<< 4) | (Src2Hi
>> 4));
1674 return TargetInstrInfo::commuteInstructionImpl(WorkingMI
, /*NewMI=*/false,
1677 case X86::VPCMPBZ128rri
: case X86::VPCMPUBZ128rri
:
1678 case X86::VPCMPBZ256rri
: case X86::VPCMPUBZ256rri
:
1679 case X86::VPCMPBZrri
: case X86::VPCMPUBZrri
:
1680 case X86::VPCMPDZ128rri
: case X86::VPCMPUDZ128rri
:
1681 case X86::VPCMPDZ256rri
: case X86::VPCMPUDZ256rri
:
1682 case X86::VPCMPDZrri
: case X86::VPCMPUDZrri
:
1683 case X86::VPCMPQZ128rri
: case X86::VPCMPUQZ128rri
:
1684 case X86::VPCMPQZ256rri
: case X86::VPCMPUQZ256rri
:
1685 case X86::VPCMPQZrri
: case X86::VPCMPUQZrri
:
1686 case X86::VPCMPWZ128rri
: case X86::VPCMPUWZ128rri
:
1687 case X86::VPCMPWZ256rri
: case X86::VPCMPUWZ256rri
:
1688 case X86::VPCMPWZrri
: case X86::VPCMPUWZrri
:
1689 case X86::VPCMPBZ128rrik
: case X86::VPCMPUBZ128rrik
:
1690 case X86::VPCMPBZ256rrik
: case X86::VPCMPUBZ256rrik
:
1691 case X86::VPCMPBZrrik
: case X86::VPCMPUBZrrik
:
1692 case X86::VPCMPDZ128rrik
: case X86::VPCMPUDZ128rrik
:
1693 case X86::VPCMPDZ256rrik
: case X86::VPCMPUDZ256rrik
:
1694 case X86::VPCMPDZrrik
: case X86::VPCMPUDZrrik
:
1695 case X86::VPCMPQZ128rrik
: case X86::VPCMPUQZ128rrik
:
1696 case X86::VPCMPQZ256rrik
: case X86::VPCMPUQZ256rrik
:
1697 case X86::VPCMPQZrrik
: case X86::VPCMPUQZrrik
:
1698 case X86::VPCMPWZ128rrik
: case X86::VPCMPUWZ128rrik
:
1699 case X86::VPCMPWZ256rrik
: case X86::VPCMPUWZ256rrik
:
1700 case X86::VPCMPWZrrik
: case X86::VPCMPUWZrrik
: {
1701 // Flip comparison mode immediate (if necessary).
1702 unsigned Imm
= MI
.getOperand(MI
.getNumOperands() - 1).getImm() & 0x7;
1703 Imm
= X86::getSwappedVPCMPImm(Imm
);
1704 auto &WorkingMI
= cloneIfNew(MI
);
1705 WorkingMI
.getOperand(MI
.getNumOperands() - 1).setImm(Imm
);
1706 return TargetInstrInfo::commuteInstructionImpl(WorkingMI
, /*NewMI=*/false,
1709 case X86::VPCOMBri
: case X86::VPCOMUBri
:
1710 case X86::VPCOMDri
: case X86::VPCOMUDri
:
1711 case X86::VPCOMQri
: case X86::VPCOMUQri
:
1712 case X86::VPCOMWri
: case X86::VPCOMUWri
: {
1713 // Flip comparison mode immediate (if necessary).
1714 unsigned Imm
= MI
.getOperand(3).getImm() & 0x7;
1715 Imm
= X86::getSwappedVPCOMImm(Imm
);
1716 auto &WorkingMI
= cloneIfNew(MI
);
1717 WorkingMI
.getOperand(3).setImm(Imm
);
1718 return TargetInstrInfo::commuteInstructionImpl(WorkingMI
, /*NewMI=*/false,
1721 case X86::VPERM2F128rr
:
1722 case X86::VPERM2I128rr
: {
1723 // Flip permute source immediate.
1724 // Imm & 0x02: lo = if set, select Op1.lo/hi else Op0.lo/hi.
1725 // Imm & 0x20: hi = if set, select Op1.lo/hi else Op0.lo/hi.
1726 int8_t Imm
= MI
.getOperand(3).getImm() & 0xFF;
1727 auto &WorkingMI
= cloneIfNew(MI
);
1728 WorkingMI
.getOperand(3).setImm(Imm
^ 0x22);
1729 return TargetInstrInfo::commuteInstructionImpl(WorkingMI
, /*NewMI=*/false,
1732 case X86::MOVHLPSrr
:
1733 case X86::UNPCKHPDrr
:
1734 case X86::VMOVHLPSrr
:
1735 case X86::VUNPCKHPDrr
:
1736 case X86::VMOVHLPSZrr
:
1737 case X86::VUNPCKHPDZ128rr
: {
1738 assert(Subtarget
.hasSSE2() && "Commuting MOVHLP/UNPCKHPD requires SSE2!");
1740 unsigned Opc
= MI
.getOpcode();
1742 default: llvm_unreachable("Unreachable!");
1743 case X86::MOVHLPSrr
: Opc
= X86::UNPCKHPDrr
; break;
1744 case X86::UNPCKHPDrr
: Opc
= X86::MOVHLPSrr
; break;
1745 case X86::VMOVHLPSrr
: Opc
= X86::VUNPCKHPDrr
; break;
1746 case X86::VUNPCKHPDrr
: Opc
= X86::VMOVHLPSrr
; break;
1747 case X86::VMOVHLPSZrr
: Opc
= X86::VUNPCKHPDZ128rr
; break;
1748 case X86::VUNPCKHPDZ128rr
: Opc
= X86::VMOVHLPSZrr
; break;
1750 auto &WorkingMI
= cloneIfNew(MI
);
1751 WorkingMI
.setDesc(get(Opc
));
1752 return TargetInstrInfo::commuteInstructionImpl(WorkingMI
, /*NewMI=*/false,
1755 case X86::CMOV16rr
: case X86::CMOV32rr
: case X86::CMOV64rr
: {
1756 auto &WorkingMI
= cloneIfNew(MI
);
1757 unsigned OpNo
= MI
.getDesc().getNumOperands() - 1;
1758 X86::CondCode CC
= static_cast<X86::CondCode
>(MI
.getOperand(OpNo
).getImm());
1759 WorkingMI
.getOperand(OpNo
).setImm(X86::GetOppositeBranchCondition(CC
));
1760 return TargetInstrInfo::commuteInstructionImpl(WorkingMI
, /*NewMI=*/false,
1763 case X86::VPTERNLOGDZrri
: case X86::VPTERNLOGDZrmi
:
1764 case X86::VPTERNLOGDZ128rri
: case X86::VPTERNLOGDZ128rmi
:
1765 case X86::VPTERNLOGDZ256rri
: case X86::VPTERNLOGDZ256rmi
:
1766 case X86::VPTERNLOGQZrri
: case X86::VPTERNLOGQZrmi
:
1767 case X86::VPTERNLOGQZ128rri
: case X86::VPTERNLOGQZ128rmi
:
1768 case X86::VPTERNLOGQZ256rri
: case X86::VPTERNLOGQZ256rmi
:
1769 case X86::VPTERNLOGDZrrik
:
1770 case X86::VPTERNLOGDZ128rrik
:
1771 case X86::VPTERNLOGDZ256rrik
:
1772 case X86::VPTERNLOGQZrrik
:
1773 case X86::VPTERNLOGQZ128rrik
:
1774 case X86::VPTERNLOGQZ256rrik
:
1775 case X86::VPTERNLOGDZrrikz
: case X86::VPTERNLOGDZrmikz
:
1776 case X86::VPTERNLOGDZ128rrikz
: case X86::VPTERNLOGDZ128rmikz
:
1777 case X86::VPTERNLOGDZ256rrikz
: case X86::VPTERNLOGDZ256rmikz
:
1778 case X86::VPTERNLOGQZrrikz
: case X86::VPTERNLOGQZrmikz
:
1779 case X86::VPTERNLOGQZ128rrikz
: case X86::VPTERNLOGQZ128rmikz
:
1780 case X86::VPTERNLOGQZ256rrikz
: case X86::VPTERNLOGQZ256rmikz
:
1781 case X86::VPTERNLOGDZ128rmbi
:
1782 case X86::VPTERNLOGDZ256rmbi
:
1783 case X86::VPTERNLOGDZrmbi
:
1784 case X86::VPTERNLOGQZ128rmbi
:
1785 case X86::VPTERNLOGQZ256rmbi
:
1786 case X86::VPTERNLOGQZrmbi
:
1787 case X86::VPTERNLOGDZ128rmbikz
:
1788 case X86::VPTERNLOGDZ256rmbikz
:
1789 case X86::VPTERNLOGDZrmbikz
:
1790 case X86::VPTERNLOGQZ128rmbikz
:
1791 case X86::VPTERNLOGQZ256rmbikz
:
1792 case X86::VPTERNLOGQZrmbikz
: {
1793 auto &WorkingMI
= cloneIfNew(MI
);
1794 commuteVPTERNLOG(WorkingMI
, OpIdx1
, OpIdx2
);
1795 return TargetInstrInfo::commuteInstructionImpl(WorkingMI
, /*NewMI=*/false,
1799 if (isCommutableVPERMV3Instruction(MI
.getOpcode())) {
1800 unsigned Opc
= getCommutedVPERMV3Opcode(MI
.getOpcode());
1801 auto &WorkingMI
= cloneIfNew(MI
);
1802 WorkingMI
.setDesc(get(Opc
));
1803 return TargetInstrInfo::commuteInstructionImpl(WorkingMI
, /*NewMI=*/false,
1807 const X86InstrFMA3Group
*FMA3Group
= getFMA3Group(MI
.getOpcode(),
1808 MI
.getDesc().TSFlags
);
1811 getFMA3OpcodeToCommuteOperands(MI
, OpIdx1
, OpIdx2
, *FMA3Group
);
1812 auto &WorkingMI
= cloneIfNew(MI
);
1813 WorkingMI
.setDesc(get(Opc
));
1814 return TargetInstrInfo::commuteInstructionImpl(WorkingMI
, /*NewMI=*/false,
1818 return TargetInstrInfo::commuteInstructionImpl(MI
, NewMI
, OpIdx1
, OpIdx2
);
1824 X86InstrInfo::findThreeSrcCommutedOpIndices(const MachineInstr
&MI
,
1825 unsigned &SrcOpIdx1
,
1826 unsigned &SrcOpIdx2
,
1827 bool IsIntrinsic
) const {
1828 uint64_t TSFlags
= MI
.getDesc().TSFlags
;
1830 unsigned FirstCommutableVecOp
= 1;
1831 unsigned LastCommutableVecOp
= 3;
1832 unsigned KMaskOp
= -1U;
1833 if (X86II::isKMasked(TSFlags
)) {
1834 // For k-zero-masked operations it is Ok to commute the first vector
1836 // For regular k-masked operations a conservative choice is done as the
1837 // elements of the first vector operand, for which the corresponding bit
1838 // in the k-mask operand is set to 0, are copied to the result of the
1840 // TODO/FIXME: The commute still may be legal if it is known that the
1841 // k-mask operand is set to either all ones or all zeroes.
1842 // It is also Ok to commute the 1st operand if all users of MI use only
1843 // the elements enabled by the k-mask operand. For example,
1844 // v4 = VFMADD213PSZrk v1, k, v2, v3; // v1[i] = k[i] ? v2[i]*v1[i]+v3[i]
1846 // VMOVAPSZmrk <mem_addr>, k, v4; // this is the ONLY user of v4 ->
1847 // // Ok, to commute v1 in FMADD213PSZrk.
1849 // The k-mask operand has index = 2 for masked and zero-masked operations.
1852 // The operand with index = 1 is used as a source for those elements for
1853 // which the corresponding bit in the k-mask is set to 0.
1854 if (X86II::isKMergeMasked(TSFlags
))
1855 FirstCommutableVecOp
= 3;
1857 LastCommutableVecOp
++;
1858 } else if (IsIntrinsic
) {
1859 // Commuting the first operand of an intrinsic instruction isn't possible
1860 // unless we can prove that only the lowest element of the result is used.
1861 FirstCommutableVecOp
= 2;
1864 if (isMem(MI
, LastCommutableVecOp
))
1865 LastCommutableVecOp
--;
1867 // Only the first RegOpsNum operands are commutable.
1868 // Also, the value 'CommuteAnyOperandIndex' is valid here as it means
1869 // that the operand is not specified/fixed.
1870 if (SrcOpIdx1
!= CommuteAnyOperandIndex
&&
1871 (SrcOpIdx1
< FirstCommutableVecOp
|| SrcOpIdx1
> LastCommutableVecOp
||
1872 SrcOpIdx1
== KMaskOp
))
1874 if (SrcOpIdx2
!= CommuteAnyOperandIndex
&&
1875 (SrcOpIdx2
< FirstCommutableVecOp
|| SrcOpIdx2
> LastCommutableVecOp
||
1876 SrcOpIdx2
== KMaskOp
))
1879 // Look for two different register operands assumed to be commutable
1880 // regardless of the FMA opcode. The FMA opcode is adjusted later.
1881 if (SrcOpIdx1
== CommuteAnyOperandIndex
||
1882 SrcOpIdx2
== CommuteAnyOperandIndex
) {
1883 unsigned CommutableOpIdx2
= SrcOpIdx2
;
1885 // At least one of operands to be commuted is not specified and
1886 // this method is free to choose appropriate commutable operands.
1887 if (SrcOpIdx1
== SrcOpIdx2
)
1888 // Both of operands are not fixed. By default set one of commutable
1889 // operands to the last register operand of the instruction.
1890 CommutableOpIdx2
= LastCommutableVecOp
;
1891 else if (SrcOpIdx2
== CommuteAnyOperandIndex
)
1892 // Only one of operands is not fixed.
1893 CommutableOpIdx2
= SrcOpIdx1
;
1895 // CommutableOpIdx2 is well defined now. Let's choose another commutable
1896 // operand and assign its index to CommutableOpIdx1.
1897 Register Op2Reg
= MI
.getOperand(CommutableOpIdx2
).getReg();
1899 unsigned CommutableOpIdx1
;
1900 for (CommutableOpIdx1
= LastCommutableVecOp
;
1901 CommutableOpIdx1
>= FirstCommutableVecOp
; CommutableOpIdx1
--) {
1902 // Just ignore and skip the k-mask operand.
1903 if (CommutableOpIdx1
== KMaskOp
)
1906 // The commuted operands must have different registers.
1907 // Otherwise, the commute transformation does not change anything and
1909 if (Op2Reg
!= MI
.getOperand(CommutableOpIdx1
).getReg())
1913 // No appropriate commutable operands were found.
1914 if (CommutableOpIdx1
< FirstCommutableVecOp
)
1917 // Assign the found pair of commutable indices to SrcOpIdx1 and SrcOpidx2
1918 // to return those values.
1919 if (!fixCommutedOpIndices(SrcOpIdx1
, SrcOpIdx2
,
1920 CommutableOpIdx1
, CommutableOpIdx2
))
1927 bool X86InstrInfo::findCommutedOpIndices(MachineInstr
&MI
, unsigned &SrcOpIdx1
,
1928 unsigned &SrcOpIdx2
) const {
1929 const MCInstrDesc
&Desc
= MI
.getDesc();
1930 if (!Desc
.isCommutable())
1933 switch (MI
.getOpcode()) {
1940 case X86::VCMPPDrri
:
1941 case X86::VCMPPSrri
:
1942 case X86::VCMPPDYrri
:
1943 case X86::VCMPPSYrri
:
1944 case X86::VCMPSDZrr
:
1945 case X86::VCMPSSZrr
:
1946 case X86::VCMPPDZrri
:
1947 case X86::VCMPPSZrri
:
1948 case X86::VCMPPDZ128rri
:
1949 case X86::VCMPPSZ128rri
:
1950 case X86::VCMPPDZ256rri
:
1951 case X86::VCMPPSZ256rri
:
1952 case X86::VCMPPDZrrik
:
1953 case X86::VCMPPSZrrik
:
1954 case X86::VCMPPDZ128rrik
:
1955 case X86::VCMPPSZ128rrik
:
1956 case X86::VCMPPDZ256rrik
:
1957 case X86::VCMPPSZ256rrik
: {
1958 unsigned OpOffset
= X86II::isKMasked(Desc
.TSFlags
) ? 1 : 0;
1960 // Float comparison can be safely commuted for
1961 // Ordered/Unordered/Equal/NotEqual tests
1962 unsigned Imm
= MI
.getOperand(3 + OpOffset
).getImm() & 0x7;
1965 case 0x03: // UNORDERED
1966 case 0x04: // NOT EQUAL
1967 case 0x07: // ORDERED
1968 // The indices of the commutable operands are 1 and 2 (or 2 and 3
1970 // Assign them to the returned operand indices here.
1971 return fixCommutedOpIndices(SrcOpIdx1
, SrcOpIdx2
, 1 + OpOffset
,
1977 // X86::MOVSDrr is always commutable. MOVSS is only commutable if we can
1978 // form sse4.1 blend. We assume VMOVSSrr/VMOVSDrr is always commutable since
1979 // AVX implies sse4.1.
1980 if (Subtarget
.hasSSE41())
1981 return TargetInstrInfo::findCommutedOpIndices(MI
, SrcOpIdx1
, SrcOpIdx2
);
1983 case X86::SHUFPDrri
:
1984 // We can commute this to MOVSD.
1985 if (MI
.getOperand(3).getImm() == 0x02)
1986 return TargetInstrInfo::findCommutedOpIndices(MI
, SrcOpIdx1
, SrcOpIdx2
);
1988 case X86::MOVHLPSrr
:
1989 case X86::UNPCKHPDrr
:
1990 case X86::VMOVHLPSrr
:
1991 case X86::VUNPCKHPDrr
:
1992 case X86::VMOVHLPSZrr
:
1993 case X86::VUNPCKHPDZ128rr
:
1994 if (Subtarget
.hasSSE2())
1995 return TargetInstrInfo::findCommutedOpIndices(MI
, SrcOpIdx1
, SrcOpIdx2
);
1997 case X86::VPTERNLOGDZrri
: case X86::VPTERNLOGDZrmi
:
1998 case X86::VPTERNLOGDZ128rri
: case X86::VPTERNLOGDZ128rmi
:
1999 case X86::VPTERNLOGDZ256rri
: case X86::VPTERNLOGDZ256rmi
:
2000 case X86::VPTERNLOGQZrri
: case X86::VPTERNLOGQZrmi
:
2001 case X86::VPTERNLOGQZ128rri
: case X86::VPTERNLOGQZ128rmi
:
2002 case X86::VPTERNLOGQZ256rri
: case X86::VPTERNLOGQZ256rmi
:
2003 case X86::VPTERNLOGDZrrik
:
2004 case X86::VPTERNLOGDZ128rrik
:
2005 case X86::VPTERNLOGDZ256rrik
:
2006 case X86::VPTERNLOGQZrrik
:
2007 case X86::VPTERNLOGQZ128rrik
:
2008 case X86::VPTERNLOGQZ256rrik
:
2009 case X86::VPTERNLOGDZrrikz
: case X86::VPTERNLOGDZrmikz
:
2010 case X86::VPTERNLOGDZ128rrikz
: case X86::VPTERNLOGDZ128rmikz
:
2011 case X86::VPTERNLOGDZ256rrikz
: case X86::VPTERNLOGDZ256rmikz
:
2012 case X86::VPTERNLOGQZrrikz
: case X86::VPTERNLOGQZrmikz
:
2013 case X86::VPTERNLOGQZ128rrikz
: case X86::VPTERNLOGQZ128rmikz
:
2014 case X86::VPTERNLOGQZ256rrikz
: case X86::VPTERNLOGQZ256rmikz
:
2015 case X86::VPTERNLOGDZ128rmbi
:
2016 case X86::VPTERNLOGDZ256rmbi
:
2017 case X86::VPTERNLOGDZrmbi
:
2018 case X86::VPTERNLOGQZ128rmbi
:
2019 case X86::VPTERNLOGQZ256rmbi
:
2020 case X86::VPTERNLOGQZrmbi
:
2021 case X86::VPTERNLOGDZ128rmbikz
:
2022 case X86::VPTERNLOGDZ256rmbikz
:
2023 case X86::VPTERNLOGDZrmbikz
:
2024 case X86::VPTERNLOGQZ128rmbikz
:
2025 case X86::VPTERNLOGQZ256rmbikz
:
2026 case X86::VPTERNLOGQZrmbikz
:
2027 return findThreeSrcCommutedOpIndices(MI
, SrcOpIdx1
, SrcOpIdx2
);
2028 case X86::VPDPWSSDZ128r
:
2029 case X86::VPDPWSSDZ128rk
:
2030 case X86::VPDPWSSDZ128rkz
:
2031 case X86::VPDPWSSDZ256r
:
2032 case X86::VPDPWSSDZ256rk
:
2033 case X86::VPDPWSSDZ256rkz
:
2034 case X86::VPDPWSSDZr
:
2035 case X86::VPDPWSSDZrk
:
2036 case X86::VPDPWSSDZrkz
:
2037 case X86::VPDPWSSDSZ128r
:
2038 case X86::VPDPWSSDSZ128rk
:
2039 case X86::VPDPWSSDSZ128rkz
:
2040 case X86::VPDPWSSDSZ256r
:
2041 case X86::VPDPWSSDSZ256rk
:
2042 case X86::VPDPWSSDSZ256rkz
:
2043 case X86::VPDPWSSDSZr
:
2044 case X86::VPDPWSSDSZrk
:
2045 case X86::VPDPWSSDSZrkz
:
2046 case X86::VPMADD52HUQZ128r
:
2047 case X86::VPMADD52HUQZ128rk
:
2048 case X86::VPMADD52HUQZ128rkz
:
2049 case X86::VPMADD52HUQZ256r
:
2050 case X86::VPMADD52HUQZ256rk
:
2051 case X86::VPMADD52HUQZ256rkz
:
2052 case X86::VPMADD52HUQZr
:
2053 case X86::VPMADD52HUQZrk
:
2054 case X86::VPMADD52HUQZrkz
:
2055 case X86::VPMADD52LUQZ128r
:
2056 case X86::VPMADD52LUQZ128rk
:
2057 case X86::VPMADD52LUQZ128rkz
:
2058 case X86::VPMADD52LUQZ256r
:
2059 case X86::VPMADD52LUQZ256rk
:
2060 case X86::VPMADD52LUQZ256rkz
:
2061 case X86::VPMADD52LUQZr
:
2062 case X86::VPMADD52LUQZrk
:
2063 case X86::VPMADD52LUQZrkz
: {
2064 unsigned CommutableOpIdx1
= 2;
2065 unsigned CommutableOpIdx2
= 3;
2066 if (X86II::isKMasked(Desc
.TSFlags
)) {
2067 // Skip the mask register.
2071 if (!fixCommutedOpIndices(SrcOpIdx1
, SrcOpIdx2
,
2072 CommutableOpIdx1
, CommutableOpIdx2
))
2074 if (!MI
.getOperand(SrcOpIdx1
).isReg() ||
2075 !MI
.getOperand(SrcOpIdx2
).isReg())
2082 const X86InstrFMA3Group
*FMA3Group
= getFMA3Group(MI
.getOpcode(),
2083 MI
.getDesc().TSFlags
);
2085 return findThreeSrcCommutedOpIndices(MI
, SrcOpIdx1
, SrcOpIdx2
,
2086 FMA3Group
->isIntrinsic());
2088 // Handled masked instructions since we need to skip over the mask input
2089 // and the preserved input.
2090 if (X86II::isKMasked(Desc
.TSFlags
)) {
2091 // First assume that the first input is the mask operand and skip past it.
2092 unsigned CommutableOpIdx1
= Desc
.getNumDefs() + 1;
2093 unsigned CommutableOpIdx2
= Desc
.getNumDefs() + 2;
2094 // Check if the first input is tied. If there isn't one then we only
2095 // need to skip the mask operand which we did above.
2096 if ((MI
.getDesc().getOperandConstraint(Desc
.getNumDefs(),
2097 MCOI::TIED_TO
) != -1)) {
2098 // If this is zero masking instruction with a tied operand, we need to
2099 // move the first index back to the first input since this must
2100 // be a 3 input instruction and we want the first two non-mask inputs.
2101 // Otherwise this is a 2 input instruction with a preserved input and
2102 // mask, so we need to move the indices to skip one more input.
2103 if (X86II::isKMergeMasked(Desc
.TSFlags
)) {
2111 if (!fixCommutedOpIndices(SrcOpIdx1
, SrcOpIdx2
,
2112 CommutableOpIdx1
, CommutableOpIdx2
))
2115 if (!MI
.getOperand(SrcOpIdx1
).isReg() ||
2116 !MI
.getOperand(SrcOpIdx2
).isReg())
2122 return TargetInstrInfo::findCommutedOpIndices(MI
, SrcOpIdx1
, SrcOpIdx2
);
2127 X86::CondCode
X86::getCondFromBranch(const MachineInstr
&MI
) {
2128 switch (MI
.getOpcode()) {
2129 default: return X86::COND_INVALID
;
2131 return static_cast<X86::CondCode
>(
2132 MI
.getOperand(MI
.getDesc().getNumOperands() - 1).getImm());
2136 /// Return condition code of a SETCC opcode.
2137 X86::CondCode
X86::getCondFromSETCC(const MachineInstr
&MI
) {
2138 switch (MI
.getOpcode()) {
2139 default: return X86::COND_INVALID
;
2140 case X86::SETCCr
: case X86::SETCCm
:
2141 return static_cast<X86::CondCode
>(
2142 MI
.getOperand(MI
.getDesc().getNumOperands() - 1).getImm());
2146 /// Return condition code of a CMov opcode.
2147 X86::CondCode
X86::getCondFromCMov(const MachineInstr
&MI
) {
2148 switch (MI
.getOpcode()) {
2149 default: return X86::COND_INVALID
;
2150 case X86::CMOV16rr
: case X86::CMOV32rr
: case X86::CMOV64rr
:
2151 case X86::CMOV16rm
: case X86::CMOV32rm
: case X86::CMOV64rm
:
2152 return static_cast<X86::CondCode
>(
2153 MI
.getOperand(MI
.getDesc().getNumOperands() - 1).getImm());
2157 /// Return the inverse of the specified condition,
2158 /// e.g. turning COND_E to COND_NE.
2159 X86::CondCode
X86::GetOppositeBranchCondition(X86::CondCode CC
) {
2161 default: llvm_unreachable("Illegal condition code!");
2162 case X86::COND_E
: return X86::COND_NE
;
2163 case X86::COND_NE
: return X86::COND_E
;
2164 case X86::COND_L
: return X86::COND_GE
;
2165 case X86::COND_LE
: return X86::COND_G
;
2166 case X86::COND_G
: return X86::COND_LE
;
2167 case X86::COND_GE
: return X86::COND_L
;
2168 case X86::COND_B
: return X86::COND_AE
;
2169 case X86::COND_BE
: return X86::COND_A
;
2170 case X86::COND_A
: return X86::COND_BE
;
2171 case X86::COND_AE
: return X86::COND_B
;
2172 case X86::COND_S
: return X86::COND_NS
;
2173 case X86::COND_NS
: return X86::COND_S
;
2174 case X86::COND_P
: return X86::COND_NP
;
2175 case X86::COND_NP
: return X86::COND_P
;
2176 case X86::COND_O
: return X86::COND_NO
;
2177 case X86::COND_NO
: return X86::COND_O
;
2178 case X86::COND_NE_OR_P
: return X86::COND_E_AND_NP
;
2179 case X86::COND_E_AND_NP
: return X86::COND_NE_OR_P
;
2183 /// Assuming the flags are set by MI(a,b), return the condition code if we
2184 /// modify the instructions such that flags are set by MI(b,a).
2185 static X86::CondCode
getSwappedCondition(X86::CondCode CC
) {
2187 default: return X86::COND_INVALID
;
2188 case X86::COND_E
: return X86::COND_E
;
2189 case X86::COND_NE
: return X86::COND_NE
;
2190 case X86::COND_L
: return X86::COND_G
;
2191 case X86::COND_LE
: return X86::COND_GE
;
2192 case X86::COND_G
: return X86::COND_L
;
2193 case X86::COND_GE
: return X86::COND_LE
;
2194 case X86::COND_B
: return X86::COND_A
;
2195 case X86::COND_BE
: return X86::COND_AE
;
2196 case X86::COND_A
: return X86::COND_B
;
2197 case X86::COND_AE
: return X86::COND_BE
;
2201 std::pair
<X86::CondCode
, bool>
2202 X86::getX86ConditionCode(CmpInst::Predicate Predicate
) {
2203 X86::CondCode CC
= X86::COND_INVALID
;
2204 bool NeedSwap
= false;
2205 switch (Predicate
) {
2207 // Floating-point Predicates
2208 case CmpInst::FCMP_UEQ
: CC
= X86::COND_E
; break;
2209 case CmpInst::FCMP_OLT
: NeedSwap
= true; LLVM_FALLTHROUGH
;
2210 case CmpInst::FCMP_OGT
: CC
= X86::COND_A
; break;
2211 case CmpInst::FCMP_OLE
: NeedSwap
= true; LLVM_FALLTHROUGH
;
2212 case CmpInst::FCMP_OGE
: CC
= X86::COND_AE
; break;
2213 case CmpInst::FCMP_UGT
: NeedSwap
= true; LLVM_FALLTHROUGH
;
2214 case CmpInst::FCMP_ULT
: CC
= X86::COND_B
; break;
2215 case CmpInst::FCMP_UGE
: NeedSwap
= true; LLVM_FALLTHROUGH
;
2216 case CmpInst::FCMP_ULE
: CC
= X86::COND_BE
; break;
2217 case CmpInst::FCMP_ONE
: CC
= X86::COND_NE
; break;
2218 case CmpInst::FCMP_UNO
: CC
= X86::COND_P
; break;
2219 case CmpInst::FCMP_ORD
: CC
= X86::COND_NP
; break;
2220 case CmpInst::FCMP_OEQ
: LLVM_FALLTHROUGH
;
2221 case CmpInst::FCMP_UNE
: CC
= X86::COND_INVALID
; break;
2223 // Integer Predicates
2224 case CmpInst::ICMP_EQ
: CC
= X86::COND_E
; break;
2225 case CmpInst::ICMP_NE
: CC
= X86::COND_NE
; break;
2226 case CmpInst::ICMP_UGT
: CC
= X86::COND_A
; break;
2227 case CmpInst::ICMP_UGE
: CC
= X86::COND_AE
; break;
2228 case CmpInst::ICMP_ULT
: CC
= X86::COND_B
; break;
2229 case CmpInst::ICMP_ULE
: CC
= X86::COND_BE
; break;
2230 case CmpInst::ICMP_SGT
: CC
= X86::COND_G
; break;
2231 case CmpInst::ICMP_SGE
: CC
= X86::COND_GE
; break;
2232 case CmpInst::ICMP_SLT
: CC
= X86::COND_L
; break;
2233 case CmpInst::ICMP_SLE
: CC
= X86::COND_LE
; break;
2236 return std::make_pair(CC
, NeedSwap
);
2239 /// Return a setcc opcode based on whether it has memory operand.
2240 unsigned X86::getSETOpc(bool HasMemoryOperand
) {
2241 return HasMemoryOperand
? X86::SETCCr
: X86::SETCCm
;
2244 /// Return a cmov opcode for the given register size in bytes, and operand type.
2245 unsigned X86::getCMovOpcode(unsigned RegBytes
, bool HasMemoryOperand
) {
2247 default: llvm_unreachable("Illegal register size!");
2248 case 2: return HasMemoryOperand
? X86::CMOV16rm
: X86::CMOV16rr
;
2249 case 4: return HasMemoryOperand
? X86::CMOV32rm
: X86::CMOV32rr
;
2250 case 8: return HasMemoryOperand
? X86::CMOV32rm
: X86::CMOV64rr
;
2254 /// Get the VPCMP immediate for the given condition.
2255 unsigned X86::getVPCMPImmForCond(ISD::CondCode CC
) {
2257 default: llvm_unreachable("Unexpected SETCC condition");
2258 case ISD::SETNE
: return 4;
2259 case ISD::SETEQ
: return 0;
2261 case ISD::SETLT
: return 1;
2263 case ISD::SETGT
: return 6;
2265 case ISD::SETGE
: return 5;
2267 case ISD::SETLE
: return 2;
2271 /// Get the VPCMP immediate if the opcodes are swapped.
2272 unsigned X86::getSwappedVPCMPImm(unsigned Imm
) {
2274 default: llvm_unreachable("Unreachable!");
2275 case 0x01: Imm
= 0x06; break; // LT -> NLE
2276 case 0x02: Imm
= 0x05; break; // LE -> NLT
2277 case 0x05: Imm
= 0x02; break; // NLT -> LE
2278 case 0x06: Imm
= 0x01; break; // NLE -> LT
2289 /// Get the VPCOM immediate if the opcodes are swapped.
2290 unsigned X86::getSwappedVPCOMImm(unsigned Imm
) {
2292 default: llvm_unreachable("Unreachable!");
2293 case 0x00: Imm
= 0x02; break; // LT -> GT
2294 case 0x01: Imm
= 0x03; break; // LE -> GE
2295 case 0x02: Imm
= 0x00; break; // GT -> LT
2296 case 0x03: Imm
= 0x01; break; // GE -> LE
2307 bool X86InstrInfo::isUnpredicatedTerminator(const MachineInstr
&MI
) const {
2308 if (!MI
.isTerminator()) return false;
2310 // Conditional branch is a special case.
2311 if (MI
.isBranch() && !MI
.isBarrier())
2313 if (!MI
.isPredicable())
2315 return !isPredicated(MI
);
2318 bool X86InstrInfo::isUnconditionalTailCall(const MachineInstr
&MI
) const {
2319 switch (MI
.getOpcode()) {
2320 case X86::TCRETURNdi
:
2321 case X86::TCRETURNri
:
2322 case X86::TCRETURNmi
:
2323 case X86::TCRETURNdi64
:
2324 case X86::TCRETURNri64
:
2325 case X86::TCRETURNmi64
:
2332 bool X86InstrInfo::canMakeTailCallConditional(
2333 SmallVectorImpl
<MachineOperand
> &BranchCond
,
2334 const MachineInstr
&TailCall
) const {
2335 if (TailCall
.getOpcode() != X86::TCRETURNdi
&&
2336 TailCall
.getOpcode() != X86::TCRETURNdi64
) {
2337 // Only direct calls can be done with a conditional branch.
2341 const MachineFunction
*MF
= TailCall
.getParent()->getParent();
2342 if (Subtarget
.isTargetWin64() && MF
->hasWinCFI()) {
2343 // Conditional tail calls confuse the Win64 unwinder.
2347 assert(BranchCond
.size() == 1);
2348 if (BranchCond
[0].getImm() > X86::LAST_VALID_COND
) {
2349 // Can't make a conditional tail call with this condition.
2353 const X86MachineFunctionInfo
*X86FI
= MF
->getInfo
<X86MachineFunctionInfo
>();
2354 if (X86FI
->getTCReturnAddrDelta() != 0 ||
2355 TailCall
.getOperand(1).getImm() != 0) {
2356 // A conditional tail call cannot do any stack adjustment.
2363 void X86InstrInfo::replaceBranchWithTailCall(
2364 MachineBasicBlock
&MBB
, SmallVectorImpl
<MachineOperand
> &BranchCond
,
2365 const MachineInstr
&TailCall
) const {
2366 assert(canMakeTailCallConditional(BranchCond
, TailCall
));
2368 MachineBasicBlock::iterator I
= MBB
.end();
2369 while (I
!= MBB
.begin()) {
2371 if (I
->isDebugInstr())
2374 assert(0 && "Can't find the branch to replace!");
2376 X86::CondCode CC
= X86::getCondFromBranch(*I
);
2377 assert(BranchCond
.size() == 1);
2378 if (CC
!= BranchCond
[0].getImm())
2384 unsigned Opc
= TailCall
.getOpcode() == X86::TCRETURNdi
? X86::TCRETURNdicc
2385 : X86::TCRETURNdi64cc
;
2387 auto MIB
= BuildMI(MBB
, I
, MBB
.findDebugLoc(I
), get(Opc
));
2388 MIB
->addOperand(TailCall
.getOperand(0)); // Destination.
2389 MIB
.addImm(0); // Stack offset (not used).
2390 MIB
->addOperand(BranchCond
[0]); // Condition.
2391 MIB
.copyImplicitOps(TailCall
); // Regmask and (imp-used) parameters.
2393 // Add implicit uses and defs of all live regs potentially clobbered by the
2394 // call. This way they still appear live across the call.
2395 LivePhysRegs
LiveRegs(getRegisterInfo());
2396 LiveRegs
.addLiveOuts(MBB
);
2397 SmallVector
<std::pair
<MCPhysReg
, const MachineOperand
*>, 8> Clobbers
;
2398 LiveRegs
.stepForward(*MIB
, Clobbers
);
2399 for (const auto &C
: Clobbers
) {
2400 MIB
.addReg(C
.first
, RegState::Implicit
);
2401 MIB
.addReg(C
.first
, RegState::Implicit
| RegState::Define
);
2404 I
->eraseFromParent();
2407 // Given a MBB and its TBB, find the FBB which was a fallthrough MBB (it may
2408 // not be a fallthrough MBB now due to layout changes). Return nullptr if the
2409 // fallthrough MBB cannot be identified.
2410 static MachineBasicBlock
*getFallThroughMBB(MachineBasicBlock
*MBB
,
2411 MachineBasicBlock
*TBB
) {
2412 // Look for non-EHPad successors other than TBB. If we find exactly one, it
2413 // is the fallthrough MBB. If we find zero, then TBB is both the target MBB
2414 // and fallthrough MBB. If we find more than one, we cannot identify the
2415 // fallthrough MBB and should return nullptr.
2416 MachineBasicBlock
*FallthroughBB
= nullptr;
2417 for (auto SI
= MBB
->succ_begin(), SE
= MBB
->succ_end(); SI
!= SE
; ++SI
) {
2418 if ((*SI
)->isEHPad() || (*SI
== TBB
&& FallthroughBB
))
2420 // Return a nullptr if we found more than one fallthrough successor.
2421 if (FallthroughBB
&& FallthroughBB
!= TBB
)
2423 FallthroughBB
= *SI
;
2425 return FallthroughBB
;
2428 bool X86InstrInfo::AnalyzeBranchImpl(
2429 MachineBasicBlock
&MBB
, MachineBasicBlock
*&TBB
, MachineBasicBlock
*&FBB
,
2430 SmallVectorImpl
<MachineOperand
> &Cond
,
2431 SmallVectorImpl
<MachineInstr
*> &CondBranches
, bool AllowModify
) const {
2433 // Start from the bottom of the block and work up, examining the
2434 // terminator instructions.
2435 MachineBasicBlock::iterator I
= MBB
.end();
2436 MachineBasicBlock::iterator UnCondBrIter
= MBB
.end();
2437 while (I
!= MBB
.begin()) {
2439 if (I
->isDebugInstr())
2442 // Working from the bottom, when we see a non-terminator instruction, we're
2444 if (!isUnpredicatedTerminator(*I
))
2447 // A terminator that isn't a branch can't easily be handled by this
2452 // Handle unconditional branches.
2453 if (I
->getOpcode() == X86::JMP_1
) {
2457 TBB
= I
->getOperand(0).getMBB();
2461 // If the block has any instructions after a JMP, delete them.
2462 while (std::next(I
) != MBB
.end())
2463 std::next(I
)->eraseFromParent();
2468 // Delete the JMP if it's equivalent to a fall-through.
2469 if (MBB
.isLayoutSuccessor(I
->getOperand(0).getMBB())) {
2471 I
->eraseFromParent();
2473 UnCondBrIter
= MBB
.end();
2477 // TBB is used to indicate the unconditional destination.
2478 TBB
= I
->getOperand(0).getMBB();
2482 // Handle conditional branches.
2483 X86::CondCode BranchCode
= X86::getCondFromBranch(*I
);
2484 if (BranchCode
== X86::COND_INVALID
)
2485 return true; // Can't handle indirect branch.
2487 // In practice we should never have an undef eflags operand, if we do
2488 // abort here as we are not prepared to preserve the flag.
2489 if (I
->findRegisterUseOperand(X86::EFLAGS
)->isUndef())
2492 // Working from the bottom, handle the first conditional branch.
2494 MachineBasicBlock
*TargetBB
= I
->getOperand(0).getMBB();
2495 if (AllowModify
&& UnCondBrIter
!= MBB
.end() &&
2496 MBB
.isLayoutSuccessor(TargetBB
)) {
2497 // If we can modify the code and it ends in something like:
2505 // Then we can change this to:
2512 // Which is a bit more efficient.
2513 // We conditionally jump to the fall-through block.
2514 BranchCode
= GetOppositeBranchCondition(BranchCode
);
2515 MachineBasicBlock::iterator OldInst
= I
;
2517 BuildMI(MBB
, UnCondBrIter
, MBB
.findDebugLoc(I
), get(X86::JCC_1
))
2518 .addMBB(UnCondBrIter
->getOperand(0).getMBB())
2519 .addImm(BranchCode
);
2520 BuildMI(MBB
, UnCondBrIter
, MBB
.findDebugLoc(I
), get(X86::JMP_1
))
2523 OldInst
->eraseFromParent();
2524 UnCondBrIter
->eraseFromParent();
2526 // Restart the analysis.
2527 UnCondBrIter
= MBB
.end();
2533 TBB
= I
->getOperand(0).getMBB();
2534 Cond
.push_back(MachineOperand::CreateImm(BranchCode
));
2535 CondBranches
.push_back(&*I
);
2539 // Handle subsequent conditional branches. Only handle the case where all
2540 // conditional branches branch to the same destination and their condition
2541 // opcodes fit one of the special multi-branch idioms.
2542 assert(Cond
.size() == 1);
2545 // If the conditions are the same, we can leave them alone.
2546 X86::CondCode OldBranchCode
= (X86::CondCode
)Cond
[0].getImm();
2547 auto NewTBB
= I
->getOperand(0).getMBB();
2548 if (OldBranchCode
== BranchCode
&& TBB
== NewTBB
)
2551 // If they differ, see if they fit one of the known patterns. Theoretically,
2552 // we could handle more patterns here, but we shouldn't expect to see them
2553 // if instruction selection has done a reasonable job.
2554 if (TBB
== NewTBB
&&
2555 ((OldBranchCode
== X86::COND_P
&& BranchCode
== X86::COND_NE
) ||
2556 (OldBranchCode
== X86::COND_NE
&& BranchCode
== X86::COND_P
))) {
2557 BranchCode
= X86::COND_NE_OR_P
;
2558 } else if ((OldBranchCode
== X86::COND_NP
&& BranchCode
== X86::COND_NE
) ||
2559 (OldBranchCode
== X86::COND_E
&& BranchCode
== X86::COND_P
)) {
2560 if (NewTBB
!= (FBB
? FBB
: getFallThroughMBB(&MBB
, TBB
)))
2563 // X86::COND_E_AND_NP usually has two different branch destinations.
2571 // Here this condition branches to B2 only if NP && E. It has another
2580 // Similarly it branches to B2 only if E && NP. That is why this condition
2581 // is named with COND_E_AND_NP.
2582 BranchCode
= X86::COND_E_AND_NP
;
2586 // Update the MachineOperand.
2587 Cond
[0].setImm(BranchCode
);
2588 CondBranches
.push_back(&*I
);
2594 bool X86InstrInfo::analyzeBranch(MachineBasicBlock
&MBB
,
2595 MachineBasicBlock
*&TBB
,
2596 MachineBasicBlock
*&FBB
,
2597 SmallVectorImpl
<MachineOperand
> &Cond
,
2598 bool AllowModify
) const {
2599 SmallVector
<MachineInstr
*, 4> CondBranches
;
2600 return AnalyzeBranchImpl(MBB
, TBB
, FBB
, Cond
, CondBranches
, AllowModify
);
2603 bool X86InstrInfo::analyzeBranchPredicate(MachineBasicBlock
&MBB
,
2604 MachineBranchPredicate
&MBP
,
2605 bool AllowModify
) const {
2606 using namespace std::placeholders
;
2608 SmallVector
<MachineOperand
, 4> Cond
;
2609 SmallVector
<MachineInstr
*, 4> CondBranches
;
2610 if (AnalyzeBranchImpl(MBB
, MBP
.TrueDest
, MBP
.FalseDest
, Cond
, CondBranches
,
2614 if (Cond
.size() != 1)
2617 assert(MBP
.TrueDest
&& "expected!");
2620 MBP
.FalseDest
= MBB
.getNextNode();
2622 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
2624 MachineInstr
*ConditionDef
= nullptr;
2625 bool SingleUseCondition
= true;
2627 for (auto I
= std::next(MBB
.rbegin()), E
= MBB
.rend(); I
!= E
; ++I
) {
2628 if (I
->modifiesRegister(X86::EFLAGS
, TRI
)) {
2633 if (I
->readsRegister(X86::EFLAGS
, TRI
))
2634 SingleUseCondition
= false;
2640 if (SingleUseCondition
) {
2641 for (auto *Succ
: MBB
.successors())
2642 if (Succ
->isLiveIn(X86::EFLAGS
))
2643 SingleUseCondition
= false;
2646 MBP
.ConditionDef
= ConditionDef
;
2647 MBP
.SingleUseCondition
= SingleUseCondition
;
2649 // Currently we only recognize the simple pattern:
2654 const unsigned TestOpcode
=
2655 Subtarget
.is64Bit() ? X86::TEST64rr
: X86::TEST32rr
;
2657 if (ConditionDef
->getOpcode() == TestOpcode
&&
2658 ConditionDef
->getNumOperands() == 3 &&
2659 ConditionDef
->getOperand(0).isIdenticalTo(ConditionDef
->getOperand(1)) &&
2660 (Cond
[0].getImm() == X86::COND_NE
|| Cond
[0].getImm() == X86::COND_E
)) {
2661 MBP
.LHS
= ConditionDef
->getOperand(0);
2662 MBP
.RHS
= MachineOperand::CreateImm(0);
2663 MBP
.Predicate
= Cond
[0].getImm() == X86::COND_NE
2664 ? MachineBranchPredicate::PRED_NE
2665 : MachineBranchPredicate::PRED_EQ
;
2672 unsigned X86InstrInfo::removeBranch(MachineBasicBlock
&MBB
,
2673 int *BytesRemoved
) const {
2674 assert(!BytesRemoved
&& "code size not handled");
2676 MachineBasicBlock::iterator I
= MBB
.end();
2679 while (I
!= MBB
.begin()) {
2681 if (I
->isDebugInstr())
2683 if (I
->getOpcode() != X86::JMP_1
&&
2684 X86::getCondFromBranch(*I
) == X86::COND_INVALID
)
2686 // Remove the branch.
2687 I
->eraseFromParent();
2695 unsigned X86InstrInfo::insertBranch(MachineBasicBlock
&MBB
,
2696 MachineBasicBlock
*TBB
,
2697 MachineBasicBlock
*FBB
,
2698 ArrayRef
<MachineOperand
> Cond
,
2700 int *BytesAdded
) const {
2701 // Shouldn't be a fall through.
2702 assert(TBB
&& "insertBranch must not be told to insert a fallthrough");
2703 assert((Cond
.size() == 1 || Cond
.size() == 0) &&
2704 "X86 branch conditions have one component!");
2705 assert(!BytesAdded
&& "code size not handled");
2708 // Unconditional branch?
2709 assert(!FBB
&& "Unconditional branch with multiple successors!");
2710 BuildMI(&MBB
, DL
, get(X86::JMP_1
)).addMBB(TBB
);
2714 // If FBB is null, it is implied to be a fall-through block.
2715 bool FallThru
= FBB
== nullptr;
2717 // Conditional branch.
2719 X86::CondCode CC
= (X86::CondCode
)Cond
[0].getImm();
2721 case X86::COND_NE_OR_P
:
2722 // Synthesize NE_OR_P with two branches.
2723 BuildMI(&MBB
, DL
, get(X86::JCC_1
)).addMBB(TBB
).addImm(X86::COND_NE
);
2725 BuildMI(&MBB
, DL
, get(X86::JCC_1
)).addMBB(TBB
).addImm(X86::COND_P
);
2728 case X86::COND_E_AND_NP
:
2729 // Use the next block of MBB as FBB if it is null.
2730 if (FBB
== nullptr) {
2731 FBB
= getFallThroughMBB(&MBB
, TBB
);
2732 assert(FBB
&& "MBB cannot be the last block in function when the false "
2733 "body is a fall-through.");
2735 // Synthesize COND_E_AND_NP with two branches.
2736 BuildMI(&MBB
, DL
, get(X86::JCC_1
)).addMBB(FBB
).addImm(X86::COND_NE
);
2738 BuildMI(&MBB
, DL
, get(X86::JCC_1
)).addMBB(TBB
).addImm(X86::COND_NP
);
2742 BuildMI(&MBB
, DL
, get(X86::JCC_1
)).addMBB(TBB
).addImm(CC
);
2747 // Two-way Conditional branch. Insert the second branch.
2748 BuildMI(&MBB
, DL
, get(X86::JMP_1
)).addMBB(FBB
);
2755 canInsertSelect(const MachineBasicBlock
&MBB
,
2756 ArrayRef
<MachineOperand
> Cond
,
2757 unsigned TrueReg
, unsigned FalseReg
,
2758 int &CondCycles
, int &TrueCycles
, int &FalseCycles
) const {
2759 // Not all subtargets have cmov instructions.
2760 if (!Subtarget
.hasCMov())
2762 if (Cond
.size() != 1)
2764 // We cannot do the composite conditions, at least not in SSA form.
2765 if ((X86::CondCode
)Cond
[0].getImm() > X86::LAST_VALID_COND
)
2768 // Check register classes.
2769 const MachineRegisterInfo
&MRI
= MBB
.getParent()->getRegInfo();
2770 const TargetRegisterClass
*RC
=
2771 RI
.getCommonSubClass(MRI
.getRegClass(TrueReg
), MRI
.getRegClass(FalseReg
));
2775 // We have cmov instructions for 16, 32, and 64 bit general purpose registers.
2776 if (X86::GR16RegClass
.hasSubClassEq(RC
) ||
2777 X86::GR32RegClass
.hasSubClassEq(RC
) ||
2778 X86::GR64RegClass
.hasSubClassEq(RC
)) {
2779 // This latency applies to Pentium M, Merom, Wolfdale, Nehalem, and Sandy
2780 // Bridge. Probably Ivy Bridge as well.
2787 // Can't do vectors.
2791 void X86InstrInfo::insertSelect(MachineBasicBlock
&MBB
,
2792 MachineBasicBlock::iterator I
,
2793 const DebugLoc
&DL
, unsigned DstReg
,
2794 ArrayRef
<MachineOperand
> Cond
, unsigned TrueReg
,
2795 unsigned FalseReg
) const {
2796 MachineRegisterInfo
&MRI
= MBB
.getParent()->getRegInfo();
2797 const TargetRegisterInfo
&TRI
= *MRI
.getTargetRegisterInfo();
2798 const TargetRegisterClass
&RC
= *MRI
.getRegClass(DstReg
);
2799 assert(Cond
.size() == 1 && "Invalid Cond array");
2800 unsigned Opc
= X86::getCMovOpcode(TRI
.getRegSizeInBits(RC
) / 8,
2801 false /*HasMemoryOperand*/);
2802 BuildMI(MBB
, I
, DL
, get(Opc
), DstReg
)
2805 .addImm(Cond
[0].getImm());
2808 /// Test if the given register is a physical h register.
2809 static bool isHReg(unsigned Reg
) {
2810 return X86::GR8_ABCD_HRegClass
.contains(Reg
);
2813 // Try and copy between VR128/VR64 and GR64 registers.
2814 static unsigned CopyToFromAsymmetricReg(unsigned DestReg
, unsigned SrcReg
,
2815 const X86Subtarget
&Subtarget
) {
2816 bool HasAVX
= Subtarget
.hasAVX();
2817 bool HasAVX512
= Subtarget
.hasAVX512();
2819 // SrcReg(MaskReg) -> DestReg(GR64)
2820 // SrcReg(MaskReg) -> DestReg(GR32)
2822 // All KMASK RegClasses hold the same k registers, can be tested against anyone.
2823 if (X86::VK16RegClass
.contains(SrcReg
)) {
2824 if (X86::GR64RegClass
.contains(DestReg
)) {
2825 assert(Subtarget
.hasBWI());
2826 return X86::KMOVQrk
;
2828 if (X86::GR32RegClass
.contains(DestReg
))
2829 return Subtarget
.hasBWI() ? X86::KMOVDrk
: X86::KMOVWrk
;
2832 // SrcReg(GR64) -> DestReg(MaskReg)
2833 // SrcReg(GR32) -> DestReg(MaskReg)
2835 // All KMASK RegClasses hold the same k registers, can be tested against anyone.
2836 if (X86::VK16RegClass
.contains(DestReg
)) {
2837 if (X86::GR64RegClass
.contains(SrcReg
)) {
2838 assert(Subtarget
.hasBWI());
2839 return X86::KMOVQkr
;
2841 if (X86::GR32RegClass
.contains(SrcReg
))
2842 return Subtarget
.hasBWI() ? X86::KMOVDkr
: X86::KMOVWkr
;
2846 // SrcReg(VR128) -> DestReg(GR64)
2847 // SrcReg(VR64) -> DestReg(GR64)
2848 // SrcReg(GR64) -> DestReg(VR128)
2849 // SrcReg(GR64) -> DestReg(VR64)
2851 if (X86::GR64RegClass
.contains(DestReg
)) {
2852 if (X86::VR128XRegClass
.contains(SrcReg
))
2853 // Copy from a VR128 register to a GR64 register.
2854 return HasAVX512
? X86::VMOVPQIto64Zrr
:
2855 HasAVX
? X86::VMOVPQIto64rr
:
2857 if (X86::VR64RegClass
.contains(SrcReg
))
2858 // Copy from a VR64 register to a GR64 register.
2859 return X86::MMX_MOVD64from64rr
;
2860 } else if (X86::GR64RegClass
.contains(SrcReg
)) {
2861 // Copy from a GR64 register to a VR128 register.
2862 if (X86::VR128XRegClass
.contains(DestReg
))
2863 return HasAVX512
? X86::VMOV64toPQIZrr
:
2864 HasAVX
? X86::VMOV64toPQIrr
:
2866 // Copy from a GR64 register to a VR64 register.
2867 if (X86::VR64RegClass
.contains(DestReg
))
2868 return X86::MMX_MOVD64to64rr
;
2871 // SrcReg(VR128) -> DestReg(GR32)
2872 // SrcReg(GR32) -> DestReg(VR128)
2874 if (X86::GR32RegClass
.contains(DestReg
) &&
2875 X86::VR128XRegClass
.contains(SrcReg
))
2876 // Copy from a VR128 register to a GR32 register.
2877 return HasAVX512
? X86::VMOVPDI2DIZrr
:
2878 HasAVX
? X86::VMOVPDI2DIrr
:
2881 if (X86::VR128XRegClass
.contains(DestReg
) &&
2882 X86::GR32RegClass
.contains(SrcReg
))
2883 // Copy from a VR128 register to a VR128 register.
2884 return HasAVX512
? X86::VMOVDI2PDIZrr
:
2885 HasAVX
? X86::VMOVDI2PDIrr
:
2890 void X86InstrInfo::copyPhysReg(MachineBasicBlock
&MBB
,
2891 MachineBasicBlock::iterator MI
,
2892 const DebugLoc
&DL
, unsigned DestReg
,
2893 unsigned SrcReg
, bool KillSrc
) const {
2894 // First deal with the normal symmetric copies.
2895 bool HasAVX
= Subtarget
.hasAVX();
2896 bool HasVLX
= Subtarget
.hasVLX();
2898 if (X86::GR64RegClass
.contains(DestReg
, SrcReg
))
2900 else if (X86::GR32RegClass
.contains(DestReg
, SrcReg
))
2902 else if (X86::GR16RegClass
.contains(DestReg
, SrcReg
))
2904 else if (X86::GR8RegClass
.contains(DestReg
, SrcReg
)) {
2905 // Copying to or from a physical H register on x86-64 requires a NOREX
2906 // move. Otherwise use a normal move.
2907 if ((isHReg(DestReg
) || isHReg(SrcReg
)) &&
2908 Subtarget
.is64Bit()) {
2909 Opc
= X86::MOV8rr_NOREX
;
2910 // Both operands must be encodable without an REX prefix.
2911 assert(X86::GR8_NOREXRegClass
.contains(SrcReg
, DestReg
) &&
2912 "8-bit H register can not be copied outside GR8_NOREX");
2916 else if (X86::VR64RegClass
.contains(DestReg
, SrcReg
))
2917 Opc
= X86::MMX_MOVQ64rr
;
2918 else if (X86::VR128XRegClass
.contains(DestReg
, SrcReg
)) {
2920 Opc
= X86::VMOVAPSZ128rr
;
2921 else if (X86::VR128RegClass
.contains(DestReg
, SrcReg
))
2922 Opc
= HasAVX
? X86::VMOVAPSrr
: X86::MOVAPSrr
;
2924 // If this an extended register and we don't have VLX we need to use a
2926 Opc
= X86::VMOVAPSZrr
;
2927 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
2928 DestReg
= TRI
->getMatchingSuperReg(DestReg
, X86::sub_xmm
,
2929 &X86::VR512RegClass
);
2930 SrcReg
= TRI
->getMatchingSuperReg(SrcReg
, X86::sub_xmm
,
2931 &X86::VR512RegClass
);
2933 } else if (X86::VR256XRegClass
.contains(DestReg
, SrcReg
)) {
2935 Opc
= X86::VMOVAPSZ256rr
;
2936 else if (X86::VR256RegClass
.contains(DestReg
, SrcReg
))
2937 Opc
= X86::VMOVAPSYrr
;
2939 // If this an extended register and we don't have VLX we need to use a
2941 Opc
= X86::VMOVAPSZrr
;
2942 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
2943 DestReg
= TRI
->getMatchingSuperReg(DestReg
, X86::sub_ymm
,
2944 &X86::VR512RegClass
);
2945 SrcReg
= TRI
->getMatchingSuperReg(SrcReg
, X86::sub_ymm
,
2946 &X86::VR512RegClass
);
2948 } else if (X86::VR512RegClass
.contains(DestReg
, SrcReg
))
2949 Opc
= X86::VMOVAPSZrr
;
2950 // All KMASK RegClasses hold the same k registers, can be tested against anyone.
2951 else if (X86::VK16RegClass
.contains(DestReg
, SrcReg
))
2952 Opc
= Subtarget
.hasBWI() ? X86::KMOVQkk
: X86::KMOVWkk
;
2954 Opc
= CopyToFromAsymmetricReg(DestReg
, SrcReg
, Subtarget
);
2957 BuildMI(MBB
, MI
, DL
, get(Opc
), DestReg
)
2958 .addReg(SrcReg
, getKillRegState(KillSrc
));
2962 if (SrcReg
== X86::EFLAGS
|| DestReg
== X86::EFLAGS
) {
2963 // FIXME: We use a fatal error here because historically LLVM has tried
2964 // lower some of these physreg copies and we want to ensure we get
2965 // reasonable bug reports if someone encounters a case no other testing
2966 // found. This path should be removed after the LLVM 7 release.
2967 report_fatal_error("Unable to copy EFLAGS physical register!");
2970 LLVM_DEBUG(dbgs() << "Cannot copy " << RI
.getName(SrcReg
) << " to "
2971 << RI
.getName(DestReg
) << '\n');
2972 report_fatal_error("Cannot emit physreg copy instruction");
2975 bool X86InstrInfo::isCopyInstrImpl(const MachineInstr
&MI
,
2976 const MachineOperand
*&Src
,
2977 const MachineOperand
*&Dest
) const {
2978 if (MI
.isMoveReg()) {
2979 Dest
= &MI
.getOperand(0);
2980 Src
= &MI
.getOperand(1);
2986 static unsigned getLoadStoreRegOpcode(unsigned Reg
,
2987 const TargetRegisterClass
*RC
,
2988 bool isStackAligned
,
2989 const X86Subtarget
&STI
,
2991 bool HasAVX
= STI
.hasAVX();
2992 bool HasAVX512
= STI
.hasAVX512();
2993 bool HasVLX
= STI
.hasVLX();
2995 switch (STI
.getRegisterInfo()->getSpillSize(*RC
)) {
2997 llvm_unreachable("Unknown spill size");
2999 assert(X86::GR8RegClass
.hasSubClassEq(RC
) && "Unknown 1-byte regclass");
3001 // Copying to or from a physical H register on x86-64 requires a NOREX
3002 // move. Otherwise use a normal move.
3003 if (isHReg(Reg
) || X86::GR8_ABCD_HRegClass
.hasSubClassEq(RC
))
3004 return load
? X86::MOV8rm_NOREX
: X86::MOV8mr_NOREX
;
3005 return load
? X86::MOV8rm
: X86::MOV8mr
;
3007 if (X86::VK16RegClass
.hasSubClassEq(RC
))
3008 return load
? X86::KMOVWkm
: X86::KMOVWmk
;
3009 assert(X86::GR16RegClass
.hasSubClassEq(RC
) && "Unknown 2-byte regclass");
3010 return load
? X86::MOV16rm
: X86::MOV16mr
;
3012 if (X86::GR32RegClass
.hasSubClassEq(RC
))
3013 return load
? X86::MOV32rm
: X86::MOV32mr
;
3014 if (X86::FR32XRegClass
.hasSubClassEq(RC
))
3016 (HasAVX512
? X86::VMOVSSZrm_alt
:
3017 HasAVX
? X86::VMOVSSrm_alt
:
3019 (HasAVX512
? X86::VMOVSSZmr
:
3020 HasAVX
? X86::VMOVSSmr
:
3022 if (X86::RFP32RegClass
.hasSubClassEq(RC
))
3023 return load
? X86::LD_Fp32m
: X86::ST_Fp32m
;
3024 if (X86::VK32RegClass
.hasSubClassEq(RC
)) {
3025 assert(STI
.hasBWI() && "KMOVD requires BWI");
3026 return load
? X86::KMOVDkm
: X86::KMOVDmk
;
3028 // All of these mask pair classes have the same spill size, the same kind
3029 // of kmov instructions can be used with all of them.
3030 if (X86::VK1PAIRRegClass
.hasSubClassEq(RC
) ||
3031 X86::VK2PAIRRegClass
.hasSubClassEq(RC
) ||
3032 X86::VK4PAIRRegClass
.hasSubClassEq(RC
) ||
3033 X86::VK8PAIRRegClass
.hasSubClassEq(RC
) ||
3034 X86::VK16PAIRRegClass
.hasSubClassEq(RC
))
3035 return load
? X86::MASKPAIR16LOAD
: X86::MASKPAIR16STORE
;
3036 llvm_unreachable("Unknown 4-byte regclass");
3038 if (X86::GR64RegClass
.hasSubClassEq(RC
))
3039 return load
? X86::MOV64rm
: X86::MOV64mr
;
3040 if (X86::FR64XRegClass
.hasSubClassEq(RC
))
3042 (HasAVX512
? X86::VMOVSDZrm_alt
:
3043 HasAVX
? X86::VMOVSDrm_alt
:
3045 (HasAVX512
? X86::VMOVSDZmr
:
3046 HasAVX
? X86::VMOVSDmr
:
3048 if (X86::VR64RegClass
.hasSubClassEq(RC
))
3049 return load
? X86::MMX_MOVQ64rm
: X86::MMX_MOVQ64mr
;
3050 if (X86::RFP64RegClass
.hasSubClassEq(RC
))
3051 return load
? X86::LD_Fp64m
: X86::ST_Fp64m
;
3052 if (X86::VK64RegClass
.hasSubClassEq(RC
)) {
3053 assert(STI
.hasBWI() && "KMOVQ requires BWI");
3054 return load
? X86::KMOVQkm
: X86::KMOVQmk
;
3056 llvm_unreachable("Unknown 8-byte regclass");
3058 assert(X86::RFP80RegClass
.hasSubClassEq(RC
) && "Unknown 10-byte regclass");
3059 return load
? X86::LD_Fp80m
: X86::ST_FpP80m
;
3061 if (X86::VR128XRegClass
.hasSubClassEq(RC
)) {
3062 // If stack is realigned we can use aligned stores.
3065 (HasVLX
? X86::VMOVAPSZ128rm
:
3066 HasAVX512
? X86::VMOVAPSZ128rm_NOVLX
:
3067 HasAVX
? X86::VMOVAPSrm
:
3069 (HasVLX
? X86::VMOVAPSZ128mr
:
3070 HasAVX512
? X86::VMOVAPSZ128mr_NOVLX
:
3071 HasAVX
? X86::VMOVAPSmr
:
3075 (HasVLX
? X86::VMOVUPSZ128rm
:
3076 HasAVX512
? X86::VMOVUPSZ128rm_NOVLX
:
3077 HasAVX
? X86::VMOVUPSrm
:
3079 (HasVLX
? X86::VMOVUPSZ128mr
:
3080 HasAVX512
? X86::VMOVUPSZ128mr_NOVLX
:
3081 HasAVX
? X86::VMOVUPSmr
:
3084 if (X86::BNDRRegClass
.hasSubClassEq(RC
)) {
3086 return load
? X86::BNDMOV64rm
: X86::BNDMOV64mr
;
3088 return load
? X86::BNDMOV32rm
: X86::BNDMOV32mr
;
3090 llvm_unreachable("Unknown 16-byte regclass");
3093 assert(X86::VR256XRegClass
.hasSubClassEq(RC
) && "Unknown 32-byte regclass");
3094 // If stack is realigned we can use aligned stores.
3097 (HasVLX
? X86::VMOVAPSZ256rm
:
3098 HasAVX512
? X86::VMOVAPSZ256rm_NOVLX
:
3100 (HasVLX
? X86::VMOVAPSZ256mr
:
3101 HasAVX512
? X86::VMOVAPSZ256mr_NOVLX
:
3105 (HasVLX
? X86::VMOVUPSZ256rm
:
3106 HasAVX512
? X86::VMOVUPSZ256rm_NOVLX
:
3108 (HasVLX
? X86::VMOVUPSZ256mr
:
3109 HasAVX512
? X86::VMOVUPSZ256mr_NOVLX
:
3112 assert(X86::VR512RegClass
.hasSubClassEq(RC
) && "Unknown 64-byte regclass");
3113 assert(STI
.hasAVX512() && "Using 512-bit register requires AVX512");
3115 return load
? X86::VMOVAPSZrm
: X86::VMOVAPSZmr
;
3117 return load
? X86::VMOVUPSZrm
: X86::VMOVUPSZmr
;
3121 bool X86InstrInfo::getMemOperandWithOffset(
3122 const MachineInstr
&MemOp
, const MachineOperand
*&BaseOp
, int64_t &Offset
,
3123 const TargetRegisterInfo
*TRI
) const {
3124 const MCInstrDesc
&Desc
= MemOp
.getDesc();
3125 int MemRefBegin
= X86II::getMemoryOperandNo(Desc
.TSFlags
);
3126 if (MemRefBegin
< 0)
3129 MemRefBegin
+= X86II::getOperandBias(Desc
);
3131 BaseOp
= &MemOp
.getOperand(MemRefBegin
+ X86::AddrBaseReg
);
3132 if (!BaseOp
->isReg()) // Can be an MO_FrameIndex
3135 if (MemOp
.getOperand(MemRefBegin
+ X86::AddrScaleAmt
).getImm() != 1)
3138 if (MemOp
.getOperand(MemRefBegin
+ X86::AddrIndexReg
).getReg() !=
3142 const MachineOperand
&DispMO
= MemOp
.getOperand(MemRefBegin
+ X86::AddrDisp
);
3144 // Displacement can be symbolic
3145 if (!DispMO
.isImm())
3148 Offset
= DispMO
.getImm();
3150 assert(BaseOp
->isReg() && "getMemOperandWithOffset only supports base "
3151 "operands of type register.");
3155 static unsigned getStoreRegOpcode(unsigned SrcReg
,
3156 const TargetRegisterClass
*RC
,
3157 bool isStackAligned
,
3158 const X86Subtarget
&STI
) {
3159 return getLoadStoreRegOpcode(SrcReg
, RC
, isStackAligned
, STI
, false);
3163 static unsigned getLoadRegOpcode(unsigned DestReg
,
3164 const TargetRegisterClass
*RC
,
3165 bool isStackAligned
,
3166 const X86Subtarget
&STI
) {
3167 return getLoadStoreRegOpcode(DestReg
, RC
, isStackAligned
, STI
, true);
3170 void X86InstrInfo::storeRegToStackSlot(MachineBasicBlock
&MBB
,
3171 MachineBasicBlock::iterator MI
,
3172 unsigned SrcReg
, bool isKill
, int FrameIdx
,
3173 const TargetRegisterClass
*RC
,
3174 const TargetRegisterInfo
*TRI
) const {
3175 const MachineFunction
&MF
= *MBB
.getParent();
3176 assert(MF
.getFrameInfo().getObjectSize(FrameIdx
) >= TRI
->getSpillSize(*RC
) &&
3177 "Stack slot too small for store");
3178 unsigned Alignment
= std::max
<uint32_t>(TRI
->getSpillSize(*RC
), 16);
3180 (Subtarget
.getFrameLowering()->getStackAlignment() >= Alignment
) ||
3181 RI
.canRealignStack(MF
);
3182 unsigned Opc
= getStoreRegOpcode(SrcReg
, RC
, isAligned
, Subtarget
);
3183 addFrameReference(BuildMI(MBB
, MI
, DebugLoc(), get(Opc
)), FrameIdx
)
3184 .addReg(SrcReg
, getKillRegState(isKill
));
3187 void X86InstrInfo::loadRegFromStackSlot(MachineBasicBlock
&MBB
,
3188 MachineBasicBlock::iterator MI
,
3189 unsigned DestReg
, int FrameIdx
,
3190 const TargetRegisterClass
*RC
,
3191 const TargetRegisterInfo
*TRI
) const {
3192 const MachineFunction
&MF
= *MBB
.getParent();
3193 unsigned Alignment
= std::max
<uint32_t>(TRI
->getSpillSize(*RC
), 16);
3195 (Subtarget
.getFrameLowering()->getStackAlignment() >= Alignment
) ||
3196 RI
.canRealignStack(MF
);
3197 unsigned Opc
= getLoadRegOpcode(DestReg
, RC
, isAligned
, Subtarget
);
3198 addFrameReference(BuildMI(MBB
, MI
, DebugLoc(), get(Opc
), DestReg
), FrameIdx
);
3201 bool X86InstrInfo::analyzeCompare(const MachineInstr
&MI
, unsigned &SrcReg
,
3202 unsigned &SrcReg2
, int &CmpMask
,
3203 int &CmpValue
) const {
3204 switch (MI
.getOpcode()) {
3206 case X86::CMP64ri32
:
3213 SrcReg
= MI
.getOperand(0).getReg();
3215 if (MI
.getOperand(1).isImm()) {
3217 CmpValue
= MI
.getOperand(1).getImm();
3219 CmpMask
= CmpValue
= 0;
3222 // A SUB can be used to perform comparison.
3227 SrcReg
= MI
.getOperand(1).getReg();
3236 SrcReg
= MI
.getOperand(1).getReg();
3237 SrcReg2
= MI
.getOperand(2).getReg();
3241 case X86::SUB64ri32
:
3248 SrcReg
= MI
.getOperand(1).getReg();
3250 if (MI
.getOperand(2).isImm()) {
3252 CmpValue
= MI
.getOperand(2).getImm();
3254 CmpMask
= CmpValue
= 0;
3261 SrcReg
= MI
.getOperand(0).getReg();
3262 SrcReg2
= MI
.getOperand(1).getReg();
3270 SrcReg
= MI
.getOperand(0).getReg();
3271 if (MI
.getOperand(1).getReg() != SrcReg
)
3273 // Compare against zero.
3282 /// Check whether the first instruction, whose only
3283 /// purpose is to update flags, can be made redundant.
3284 /// CMPrr can be made redundant by SUBrr if the operands are the same.
3285 /// This function can be extended later on.
3286 /// SrcReg, SrcRegs: register operands for FlagI.
3287 /// ImmValue: immediate for FlagI if it takes an immediate.
3288 inline static bool isRedundantFlagInstr(const MachineInstr
&FlagI
,
3289 unsigned SrcReg
, unsigned SrcReg2
,
3290 int ImmMask
, int ImmValue
,
3291 const MachineInstr
&OI
) {
3292 if (((FlagI
.getOpcode() == X86::CMP64rr
&& OI
.getOpcode() == X86::SUB64rr
) ||
3293 (FlagI
.getOpcode() == X86::CMP32rr
&& OI
.getOpcode() == X86::SUB32rr
) ||
3294 (FlagI
.getOpcode() == X86::CMP16rr
&& OI
.getOpcode() == X86::SUB16rr
) ||
3295 (FlagI
.getOpcode() == X86::CMP8rr
&& OI
.getOpcode() == X86::SUB8rr
)) &&
3296 ((OI
.getOperand(1).getReg() == SrcReg
&&
3297 OI
.getOperand(2).getReg() == SrcReg2
) ||
3298 (OI
.getOperand(1).getReg() == SrcReg2
&&
3299 OI
.getOperand(2).getReg() == SrcReg
)))
3303 ((FlagI
.getOpcode() == X86::CMP64ri32
&&
3304 OI
.getOpcode() == X86::SUB64ri32
) ||
3305 (FlagI
.getOpcode() == X86::CMP64ri8
&&
3306 OI
.getOpcode() == X86::SUB64ri8
) ||
3307 (FlagI
.getOpcode() == X86::CMP32ri
&& OI
.getOpcode() == X86::SUB32ri
) ||
3308 (FlagI
.getOpcode() == X86::CMP32ri8
&&
3309 OI
.getOpcode() == X86::SUB32ri8
) ||
3310 (FlagI
.getOpcode() == X86::CMP16ri
&& OI
.getOpcode() == X86::SUB16ri
) ||
3311 (FlagI
.getOpcode() == X86::CMP16ri8
&&
3312 OI
.getOpcode() == X86::SUB16ri8
) ||
3313 (FlagI
.getOpcode() == X86::CMP8ri
&& OI
.getOpcode() == X86::SUB8ri
)) &&
3314 OI
.getOperand(1).getReg() == SrcReg
&&
3315 OI
.getOperand(2).getImm() == ImmValue
)
3320 /// Check whether the definition can be converted
3321 /// to remove a comparison against zero.
3322 inline static bool isDefConvertible(const MachineInstr
&MI
, bool &NoSignFlag
) {
3325 switch (MI
.getOpcode()) {
3326 default: return false;
3328 // The shift instructions only modify ZF if their shift count is non-zero.
3329 // N.B.: The processor truncates the shift count depending on the encoding.
3330 case X86::SAR8ri
: case X86::SAR16ri
: case X86::SAR32ri
:case X86::SAR64ri
:
3331 case X86::SHR8ri
: case X86::SHR16ri
: case X86::SHR32ri
:case X86::SHR64ri
:
3332 return getTruncatedShiftCount(MI
, 2) != 0;
3334 // Some left shift instructions can be turned into LEA instructions but only
3335 // if their flags aren't used. Avoid transforming such instructions.
3336 case X86::SHL8ri
: case X86::SHL16ri
: case X86::SHL32ri
:case X86::SHL64ri
:{
3337 unsigned ShAmt
= getTruncatedShiftCount(MI
, 2);
3338 if (isTruncatedShiftCountForLEA(ShAmt
)) return false;
3342 case X86::SHRD16rri8
:case X86::SHRD32rri8
:case X86::SHRD64rri8
:
3343 case X86::SHLD16rri8
:case X86::SHLD32rri8
:case X86::SHLD64rri8
:
3344 return getTruncatedShiftCount(MI
, 3) != 0;
3346 case X86::SUB64ri32
: case X86::SUB64ri8
: case X86::SUB32ri
:
3347 case X86::SUB32ri8
: case X86::SUB16ri
: case X86::SUB16ri8
:
3348 case X86::SUB8ri
: case X86::SUB64rr
: case X86::SUB32rr
:
3349 case X86::SUB16rr
: case X86::SUB8rr
: case X86::SUB64rm
:
3350 case X86::SUB32rm
: case X86::SUB16rm
: case X86::SUB8rm
:
3351 case X86::DEC64r
: case X86::DEC32r
: case X86::DEC16r
: case X86::DEC8r
:
3352 case X86::ADD64ri32
: case X86::ADD64ri8
: case X86::ADD32ri
:
3353 case X86::ADD32ri8
: case X86::ADD16ri
: case X86::ADD16ri8
:
3354 case X86::ADD8ri
: case X86::ADD64rr
: case X86::ADD32rr
:
3355 case X86::ADD16rr
: case X86::ADD8rr
: case X86::ADD64rm
:
3356 case X86::ADD32rm
: case X86::ADD16rm
: case X86::ADD8rm
:
3357 case X86::INC64r
: case X86::INC32r
: case X86::INC16r
: case X86::INC8r
:
3358 case X86::AND64ri32
: case X86::AND64ri8
: case X86::AND32ri
:
3359 case X86::AND32ri8
: case X86::AND16ri
: case X86::AND16ri8
:
3360 case X86::AND8ri
: case X86::AND64rr
: case X86::AND32rr
:
3361 case X86::AND16rr
: case X86::AND8rr
: case X86::AND64rm
:
3362 case X86::AND32rm
: case X86::AND16rm
: case X86::AND8rm
:
3363 case X86::XOR64ri32
: case X86::XOR64ri8
: case X86::XOR32ri
:
3364 case X86::XOR32ri8
: case X86::XOR16ri
: case X86::XOR16ri8
:
3365 case X86::XOR8ri
: case X86::XOR64rr
: case X86::XOR32rr
:
3366 case X86::XOR16rr
: case X86::XOR8rr
: case X86::XOR64rm
:
3367 case X86::XOR32rm
: case X86::XOR16rm
: case X86::XOR8rm
:
3368 case X86::OR64ri32
: case X86::OR64ri8
: case X86::OR32ri
:
3369 case X86::OR32ri8
: case X86::OR16ri
: case X86::OR16ri8
:
3370 case X86::OR8ri
: case X86::OR64rr
: case X86::OR32rr
:
3371 case X86::OR16rr
: case X86::OR8rr
: case X86::OR64rm
:
3372 case X86::OR32rm
: case X86::OR16rm
: case X86::OR8rm
:
3373 case X86::ADC64ri32
: case X86::ADC64ri8
: case X86::ADC32ri
:
3374 case X86::ADC32ri8
: case X86::ADC16ri
: case X86::ADC16ri8
:
3375 case X86::ADC8ri
: case X86::ADC64rr
: case X86::ADC32rr
:
3376 case X86::ADC16rr
: case X86::ADC8rr
: case X86::ADC64rm
:
3377 case X86::ADC32rm
: case X86::ADC16rm
: case X86::ADC8rm
:
3378 case X86::SBB64ri32
: case X86::SBB64ri8
: case X86::SBB32ri
:
3379 case X86::SBB32ri8
: case X86::SBB16ri
: case X86::SBB16ri8
:
3380 case X86::SBB8ri
: case X86::SBB64rr
: case X86::SBB32rr
:
3381 case X86::SBB16rr
: case X86::SBB8rr
: case X86::SBB64rm
:
3382 case X86::SBB32rm
: case X86::SBB16rm
: case X86::SBB8rm
:
3383 case X86::NEG8r
: case X86::NEG16r
: case X86::NEG32r
: case X86::NEG64r
:
3384 case X86::SAR8r1
: case X86::SAR16r1
: case X86::SAR32r1
:case X86::SAR64r1
:
3385 case X86::SHR8r1
: case X86::SHR16r1
: case X86::SHR32r1
:case X86::SHR64r1
:
3386 case X86::SHL8r1
: case X86::SHL16r1
: case X86::SHL32r1
:case X86::SHL64r1
:
3387 case X86::ANDN32rr
: case X86::ANDN32rm
:
3388 case X86::ANDN64rr
: case X86::ANDN64rm
:
3389 case X86::BLSI32rr
: case X86::BLSI32rm
:
3390 case X86::BLSI64rr
: case X86::BLSI64rm
:
3391 case X86::BLSMSK32rr
:case X86::BLSMSK32rm
:
3392 case X86::BLSMSK64rr
:case X86::BLSMSK64rm
:
3393 case X86::BLSR32rr
: case X86::BLSR32rm
:
3394 case X86::BLSR64rr
: case X86::BLSR64rm
:
3395 case X86::BZHI32rr
: case X86::BZHI32rm
:
3396 case X86::BZHI64rr
: case X86::BZHI64rm
:
3397 case X86::LZCNT16rr
: case X86::LZCNT16rm
:
3398 case X86::LZCNT32rr
: case X86::LZCNT32rm
:
3399 case X86::LZCNT64rr
: case X86::LZCNT64rm
:
3400 case X86::POPCNT16rr
:case X86::POPCNT16rm
:
3401 case X86::POPCNT32rr
:case X86::POPCNT32rm
:
3402 case X86::POPCNT64rr
:case X86::POPCNT64rm
:
3403 case X86::TZCNT16rr
: case X86::TZCNT16rm
:
3404 case X86::TZCNT32rr
: case X86::TZCNT32rm
:
3405 case X86::TZCNT64rr
: case X86::TZCNT64rm
:
3406 case X86::BLCFILL32rr
: case X86::BLCFILL32rm
:
3407 case X86::BLCFILL64rr
: case X86::BLCFILL64rm
:
3408 case X86::BLCI32rr
: case X86::BLCI32rm
:
3409 case X86::BLCI64rr
: case X86::BLCI64rm
:
3410 case X86::BLCIC32rr
: case X86::BLCIC32rm
:
3411 case X86::BLCIC64rr
: case X86::BLCIC64rm
:
3412 case X86::BLCMSK32rr
: case X86::BLCMSK32rm
:
3413 case X86::BLCMSK64rr
: case X86::BLCMSK64rm
:
3414 case X86::BLCS32rr
: case X86::BLCS32rm
:
3415 case X86::BLCS64rr
: case X86::BLCS64rm
:
3416 case X86::BLSFILL32rr
: case X86::BLSFILL32rm
:
3417 case X86::BLSFILL64rr
: case X86::BLSFILL64rm
:
3418 case X86::BLSIC32rr
: case X86::BLSIC32rm
:
3419 case X86::BLSIC64rr
: case X86::BLSIC64rm
:
3420 case X86::T1MSKC32rr
: case X86::T1MSKC32rm
:
3421 case X86::T1MSKC64rr
: case X86::T1MSKC64rm
:
3422 case X86::TZMSK32rr
: case X86::TZMSK32rm
:
3423 case X86::TZMSK64rr
: case X86::TZMSK64rm
:
3425 case X86::BEXTR32rr
: case X86::BEXTR64rr
:
3426 case X86::BEXTR32rm
: case X86::BEXTR64rm
:
3427 case X86::BEXTRI32ri
: case X86::BEXTRI32mi
:
3428 case X86::BEXTRI64ri
: case X86::BEXTRI64mi
:
3429 // BEXTR doesn't update the sign flag so we can't use it.
3435 /// Check whether the use can be converted to remove a comparison against zero.
3436 static X86::CondCode
isUseDefConvertible(const MachineInstr
&MI
) {
3437 switch (MI
.getOpcode()) {
3438 default: return X86::COND_INVALID
;
3443 return X86::COND_AE
;
3444 case X86::LZCNT16rr
:
3445 case X86::LZCNT32rr
:
3446 case X86::LZCNT64rr
:
3448 case X86::POPCNT16rr
:
3449 case X86::POPCNT32rr
:
3450 case X86::POPCNT64rr
:
3452 case X86::TZCNT16rr
:
3453 case X86::TZCNT32rr
:
3454 case X86::TZCNT64rr
:
3465 return X86::COND_AE
;
3468 case X86::BLSMSK32rr
:
3469 case X86::BLSMSK64rr
:
3471 // TODO: TBM instructions.
3475 /// Check if there exists an earlier instruction that
3476 /// operates on the same source operands and sets flags in the same way as
3477 /// Compare; remove Compare if possible.
3478 bool X86InstrInfo::optimizeCompareInstr(MachineInstr
&CmpInstr
, unsigned SrcReg
,
3479 unsigned SrcReg2
, int CmpMask
,
3481 const MachineRegisterInfo
*MRI
) const {
3482 // Check whether we can replace SUB with CMP.
3483 switch (CmpInstr
.getOpcode()) {
3485 case X86::SUB64ri32
:
3500 if (!MRI
->use_nodbg_empty(CmpInstr
.getOperand(0).getReg()))
3502 // There is no use of the destination register, we can replace SUB with CMP.
3503 unsigned NewOpcode
= 0;
3504 switch (CmpInstr
.getOpcode()) {
3505 default: llvm_unreachable("Unreachable!");
3506 case X86::SUB64rm
: NewOpcode
= X86::CMP64rm
; break;
3507 case X86::SUB32rm
: NewOpcode
= X86::CMP32rm
; break;
3508 case X86::SUB16rm
: NewOpcode
= X86::CMP16rm
; break;
3509 case X86::SUB8rm
: NewOpcode
= X86::CMP8rm
; break;
3510 case X86::SUB64rr
: NewOpcode
= X86::CMP64rr
; break;
3511 case X86::SUB32rr
: NewOpcode
= X86::CMP32rr
; break;
3512 case X86::SUB16rr
: NewOpcode
= X86::CMP16rr
; break;
3513 case X86::SUB8rr
: NewOpcode
= X86::CMP8rr
; break;
3514 case X86::SUB64ri32
: NewOpcode
= X86::CMP64ri32
; break;
3515 case X86::SUB64ri8
: NewOpcode
= X86::CMP64ri8
; break;
3516 case X86::SUB32ri
: NewOpcode
= X86::CMP32ri
; break;
3517 case X86::SUB32ri8
: NewOpcode
= X86::CMP32ri8
; break;
3518 case X86::SUB16ri
: NewOpcode
= X86::CMP16ri
; break;
3519 case X86::SUB16ri8
: NewOpcode
= X86::CMP16ri8
; break;
3520 case X86::SUB8ri
: NewOpcode
= X86::CMP8ri
; break;
3522 CmpInstr
.setDesc(get(NewOpcode
));
3523 CmpInstr
.RemoveOperand(0);
3524 // Fall through to optimize Cmp if Cmp is CMPrr or CMPri.
3525 if (NewOpcode
== X86::CMP64rm
|| NewOpcode
== X86::CMP32rm
||
3526 NewOpcode
== X86::CMP16rm
|| NewOpcode
== X86::CMP8rm
)
3531 // Get the unique definition of SrcReg.
3532 MachineInstr
*MI
= MRI
->getUniqueVRegDef(SrcReg
);
3533 if (!MI
) return false;
3535 // CmpInstr is the first instruction of the BB.
3536 MachineBasicBlock::iterator I
= CmpInstr
, Def
= MI
;
3538 // If we are comparing against zero, check whether we can use MI to update
3539 // EFLAGS. If MI is not in the same BB as CmpInstr, do not optimize.
3540 bool IsCmpZero
= (CmpMask
!= 0 && CmpValue
== 0);
3541 if (IsCmpZero
&& MI
->getParent() != CmpInstr
.getParent())
3544 // If we have a use of the source register between the def and our compare
3545 // instruction we can eliminate the compare iff the use sets EFLAGS in the
3547 bool ShouldUpdateCC
= false;
3548 bool NoSignFlag
= false;
3549 X86::CondCode NewCC
= X86::COND_INVALID
;
3550 if (IsCmpZero
&& !isDefConvertible(*MI
, NoSignFlag
)) {
3551 // Scan forward from the use until we hit the use we're looking for or the
3552 // compare instruction.
3553 for (MachineBasicBlock::iterator J
= MI
;; ++J
) {
3554 // Do we have a convertible instruction?
3555 NewCC
= isUseDefConvertible(*J
);
3556 if (NewCC
!= X86::COND_INVALID
&& J
->getOperand(1).isReg() &&
3557 J
->getOperand(1).getReg() == SrcReg
) {
3558 assert(J
->definesRegister(X86::EFLAGS
) && "Must be an EFLAGS def!");
3559 ShouldUpdateCC
= true; // Update CC later on.
3560 // This is not a def of SrcReg, but still a def of EFLAGS. Keep going
3561 // with the new def.
3572 // We are searching for an earlier instruction that can make CmpInstr
3573 // redundant and that instruction will be saved in Sub.
3574 MachineInstr
*Sub
= nullptr;
3575 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
3577 // We iterate backward, starting from the instruction before CmpInstr and
3578 // stop when reaching the definition of a source register or done with the BB.
3579 // RI points to the instruction before CmpInstr.
3580 // If the definition is in this basic block, RE points to the definition;
3581 // otherwise, RE is the rend of the basic block.
3582 MachineBasicBlock::reverse_iterator
3583 RI
= ++I
.getReverse(),
3584 RE
= CmpInstr
.getParent() == MI
->getParent()
3585 ? Def
.getReverse() /* points to MI */
3586 : CmpInstr
.getParent()->rend();
3587 MachineInstr
*Movr0Inst
= nullptr;
3588 for (; RI
!= RE
; ++RI
) {
3589 MachineInstr
&Instr
= *RI
;
3590 // Check whether CmpInstr can be made redundant by the current instruction.
3591 if (!IsCmpZero
&& isRedundantFlagInstr(CmpInstr
, SrcReg
, SrcReg2
, CmpMask
,
3597 if (Instr
.modifiesRegister(X86::EFLAGS
, TRI
) ||
3598 Instr
.readsRegister(X86::EFLAGS
, TRI
)) {
3599 // This instruction modifies or uses EFLAGS.
3601 // MOV32r0 etc. are implemented with xor which clobbers condition code.
3602 // They are safe to move up, if the definition to EFLAGS is dead and
3603 // earlier instructions do not read or write EFLAGS.
3604 if (!Movr0Inst
&& Instr
.getOpcode() == X86::MOV32r0
&&
3605 Instr
.registerDefIsDead(X86::EFLAGS
, TRI
)) {
3610 // We can't remove CmpInstr.
3615 // Return false if no candidates exist.
3616 if (!IsCmpZero
&& !Sub
)
3620 (SrcReg2
!= 0 && Sub
&& Sub
->getOperand(1).getReg() == SrcReg2
&&
3621 Sub
->getOperand(2).getReg() == SrcReg
);
3623 // Scan forward from the instruction after CmpInstr for uses of EFLAGS.
3624 // It is safe to remove CmpInstr if EFLAGS is redefined or killed.
3625 // If we are done with the basic block, we need to check whether EFLAGS is
3627 bool IsSafe
= false;
3628 SmallVector
<std::pair
<MachineInstr
*, X86::CondCode
>, 4> OpsToUpdate
;
3629 MachineBasicBlock::iterator E
= CmpInstr
.getParent()->end();
3630 for (++I
; I
!= E
; ++I
) {
3631 const MachineInstr
&Instr
= *I
;
3632 bool ModifyEFLAGS
= Instr
.modifiesRegister(X86::EFLAGS
, TRI
);
3633 bool UseEFLAGS
= Instr
.readsRegister(X86::EFLAGS
, TRI
);
3634 // We should check the usage if this instruction uses and updates EFLAGS.
3635 if (!UseEFLAGS
&& ModifyEFLAGS
) {
3636 // It is safe to remove CmpInstr if EFLAGS is updated again.
3640 if (!UseEFLAGS
&& !ModifyEFLAGS
)
3643 // EFLAGS is used by this instruction.
3644 X86::CondCode OldCC
= X86::COND_INVALID
;
3645 if (IsCmpZero
|| IsSwapped
) {
3646 // We decode the condition code from opcode.
3647 if (Instr
.isBranch())
3648 OldCC
= X86::getCondFromBranch(Instr
);
3650 OldCC
= X86::getCondFromSETCC(Instr
);
3651 if (OldCC
== X86::COND_INVALID
)
3652 OldCC
= X86::getCondFromCMov(Instr
);
3654 if (OldCC
== X86::COND_INVALID
) return false;
3656 X86::CondCode ReplacementCC
= X86::COND_INVALID
;
3660 case X86::COND_A
: case X86::COND_AE
:
3661 case X86::COND_B
: case X86::COND_BE
:
3662 case X86::COND_G
: case X86::COND_GE
:
3663 case X86::COND_L
: case X86::COND_LE
:
3664 case X86::COND_O
: case X86::COND_NO
:
3665 // CF and OF are used, we can't perform this optimization.
3667 case X86::COND_S
: case X86::COND_NS
:
3668 // If SF is used, but the instruction doesn't update the SF, then we
3669 // can't do the optimization.
3675 // If we're updating the condition code check if we have to reverse the
3682 ReplacementCC
= NewCC
;
3685 ReplacementCC
= GetOppositeBranchCondition(NewCC
);
3688 } else if (IsSwapped
) {
3689 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code needs
3690 // to be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
3691 // We swap the condition code and synthesize the new opcode.
3692 ReplacementCC
= getSwappedCondition(OldCC
);
3693 if (ReplacementCC
== X86::COND_INVALID
) return false;
3696 if ((ShouldUpdateCC
|| IsSwapped
) && ReplacementCC
!= OldCC
) {
3697 // Push the MachineInstr to OpsToUpdate.
3698 // If it is safe to remove CmpInstr, the condition code of these
3699 // instructions will be modified.
3700 OpsToUpdate
.push_back(std::make_pair(&*I
, ReplacementCC
));
3702 if (ModifyEFLAGS
|| Instr
.killsRegister(X86::EFLAGS
, TRI
)) {
3703 // It is safe to remove CmpInstr if EFLAGS is updated again or killed.
3709 // If EFLAGS is not killed nor re-defined, we should check whether it is
3710 // live-out. If it is live-out, do not optimize.
3711 if ((IsCmpZero
|| IsSwapped
) && !IsSafe
) {
3712 MachineBasicBlock
*MBB
= CmpInstr
.getParent();
3713 for (MachineBasicBlock
*Successor
: MBB
->successors())
3714 if (Successor
->isLiveIn(X86::EFLAGS
))
3718 // The instruction to be updated is either Sub or MI.
3719 Sub
= IsCmpZero
? MI
: Sub
;
3720 // Move Movr0Inst to the appropriate place before Sub.
3722 // Look backwards until we find a def that doesn't use the current EFLAGS.
3724 MachineBasicBlock::reverse_iterator InsertI
= Def
.getReverse(),
3725 InsertE
= Sub
->getParent()->rend();
3726 for (; InsertI
!= InsertE
; ++InsertI
) {
3727 MachineInstr
*Instr
= &*InsertI
;
3728 if (!Instr
->readsRegister(X86::EFLAGS
, TRI
) &&
3729 Instr
->modifiesRegister(X86::EFLAGS
, TRI
)) {
3730 Sub
->getParent()->remove(Movr0Inst
);
3731 Instr
->getParent()->insert(MachineBasicBlock::iterator(Instr
),
3736 if (InsertI
== InsertE
)
3740 // Make sure Sub instruction defines EFLAGS and mark the def live.
3741 MachineOperand
*FlagDef
= Sub
->findRegisterDefOperand(X86::EFLAGS
);
3742 assert(FlagDef
&& "Unable to locate a def EFLAGS operand");
3743 FlagDef
->setIsDead(false);
3745 CmpInstr
.eraseFromParent();
3747 // Modify the condition code of instructions in OpsToUpdate.
3748 for (auto &Op
: OpsToUpdate
) {
3749 Op
.first
->getOperand(Op
.first
->getDesc().getNumOperands() - 1)
3755 /// Try to remove the load by folding it to a register
3756 /// operand at the use. We fold the load instructions if load defines a virtual
3757 /// register, the virtual register is used once in the same BB, and the
3758 /// instructions in-between do not load or store, and have no side effects.
3759 MachineInstr
*X86InstrInfo::optimizeLoadInstr(MachineInstr
&MI
,
3760 const MachineRegisterInfo
*MRI
,
3761 unsigned &FoldAsLoadDefReg
,
3762 MachineInstr
*&DefMI
) const {
3763 // Check whether we can move DefMI here.
3764 DefMI
= MRI
->getVRegDef(FoldAsLoadDefReg
);
3766 bool SawStore
= false;
3767 if (!DefMI
->isSafeToMove(nullptr, SawStore
))
3770 // Collect information about virtual register operands of MI.
3771 SmallVector
<unsigned, 1> SrcOperandIds
;
3772 for (unsigned i
= 0, e
= MI
.getNumOperands(); i
!= e
; ++i
) {
3773 MachineOperand
&MO
= MI
.getOperand(i
);
3776 Register Reg
= MO
.getReg();
3777 if (Reg
!= FoldAsLoadDefReg
)
3779 // Do not fold if we have a subreg use or a def.
3780 if (MO
.getSubReg() || MO
.isDef())
3782 SrcOperandIds
.push_back(i
);
3784 if (SrcOperandIds
.empty())
3787 // Check whether we can fold the def into SrcOperandId.
3788 if (MachineInstr
*FoldMI
= foldMemoryOperand(MI
, SrcOperandIds
, *DefMI
)) {
3789 FoldAsLoadDefReg
= 0;
3796 /// Expand a single-def pseudo instruction to a two-addr
3797 /// instruction with two undef reads of the register being defined.
3798 /// This is used for mapping:
3801 /// %xmm4 = PXORrr undef %xmm4, undef %xmm4
3803 static bool Expand2AddrUndef(MachineInstrBuilder
&MIB
,
3804 const MCInstrDesc
&Desc
) {
3805 assert(Desc
.getNumOperands() == 3 && "Expected two-addr instruction.");
3806 Register Reg
= MIB
->getOperand(0).getReg();
3809 // MachineInstr::addOperand() will insert explicit operands before any
3810 // implicit operands.
3811 MIB
.addReg(Reg
, RegState::Undef
).addReg(Reg
, RegState::Undef
);
3812 // But we don't trust that.
3813 assert(MIB
->getOperand(1).getReg() == Reg
&&
3814 MIB
->getOperand(2).getReg() == Reg
&& "Misplaced operand");
3818 /// Expand a single-def pseudo instruction to a two-addr
3819 /// instruction with two %k0 reads.
3820 /// This is used for mapping:
3823 /// %k4 = KXNORrr %k0, %k0
3824 static bool Expand2AddrKreg(MachineInstrBuilder
&MIB
,
3825 const MCInstrDesc
&Desc
, unsigned Reg
) {
3826 assert(Desc
.getNumOperands() == 3 && "Expected two-addr instruction.");
3828 MIB
.addReg(Reg
, RegState::Undef
).addReg(Reg
, RegState::Undef
);
3832 static bool expandMOV32r1(MachineInstrBuilder
&MIB
, const TargetInstrInfo
&TII
,
3834 MachineBasicBlock
&MBB
= *MIB
->getParent();
3835 DebugLoc DL
= MIB
->getDebugLoc();
3836 Register Reg
= MIB
->getOperand(0).getReg();
3839 BuildMI(MBB
, MIB
.getInstr(), DL
, TII
.get(X86::XOR32rr
), Reg
)
3840 .addReg(Reg
, RegState::Undef
)
3841 .addReg(Reg
, RegState::Undef
);
3843 // Turn the pseudo into an INC or DEC.
3844 MIB
->setDesc(TII
.get(MinusOne
? X86::DEC32r
: X86::INC32r
));
3850 static bool ExpandMOVImmSExti8(MachineInstrBuilder
&MIB
,
3851 const TargetInstrInfo
&TII
,
3852 const X86Subtarget
&Subtarget
) {
3853 MachineBasicBlock
&MBB
= *MIB
->getParent();
3854 DebugLoc DL
= MIB
->getDebugLoc();
3855 int64_t Imm
= MIB
->getOperand(1).getImm();
3856 assert(Imm
!= 0 && "Using push/pop for 0 is not efficient.");
3857 MachineBasicBlock::iterator I
= MIB
.getInstr();
3859 int StackAdjustment
;
3861 if (Subtarget
.is64Bit()) {
3862 assert(MIB
->getOpcode() == X86::MOV64ImmSExti8
||
3863 MIB
->getOpcode() == X86::MOV32ImmSExti8
);
3865 // Can't use push/pop lowering if the function might write to the red zone.
3866 X86MachineFunctionInfo
*X86FI
=
3867 MBB
.getParent()->getInfo
<X86MachineFunctionInfo
>();
3868 if (X86FI
->getUsesRedZone()) {
3869 MIB
->setDesc(TII
.get(MIB
->getOpcode() ==
3870 X86::MOV32ImmSExti8
? X86::MOV32ri
: X86::MOV64ri
));
3874 // 64-bit mode doesn't have 32-bit push/pop, so use 64-bit operations and
3875 // widen the register if necessary.
3876 StackAdjustment
= 8;
3877 BuildMI(MBB
, I
, DL
, TII
.get(X86::PUSH64i8
)).addImm(Imm
);
3878 MIB
->setDesc(TII
.get(X86::POP64r
));
3880 .setReg(getX86SubSuperRegister(MIB
->getOperand(0).getReg(), 64));
3882 assert(MIB
->getOpcode() == X86::MOV32ImmSExti8
);
3883 StackAdjustment
= 4;
3884 BuildMI(MBB
, I
, DL
, TII
.get(X86::PUSH32i8
)).addImm(Imm
);
3885 MIB
->setDesc(TII
.get(X86::POP32r
));
3888 // Build CFI if necessary.
3889 MachineFunction
&MF
= *MBB
.getParent();
3890 const X86FrameLowering
*TFL
= Subtarget
.getFrameLowering();
3891 bool IsWin64Prologue
= MF
.getTarget().getMCAsmInfo()->usesWindowsCFI();
3892 bool NeedsDwarfCFI
=
3894 (MF
.getMMI().hasDebugInfo() || MF
.getFunction().needsUnwindTableEntry());
3895 bool EmitCFI
= !TFL
->hasFP(MF
) && NeedsDwarfCFI
;
3897 TFL
->BuildCFI(MBB
, I
, DL
,
3898 MCCFIInstruction::createAdjustCfaOffset(nullptr, StackAdjustment
));
3899 TFL
->BuildCFI(MBB
, std::next(I
), DL
,
3900 MCCFIInstruction::createAdjustCfaOffset(nullptr, -StackAdjustment
));
3906 // LoadStackGuard has so far only been implemented for 64-bit MachO. Different
3907 // code sequence is needed for other targets.
3908 static void expandLoadStackGuard(MachineInstrBuilder
&MIB
,
3909 const TargetInstrInfo
&TII
) {
3910 MachineBasicBlock
&MBB
= *MIB
->getParent();
3911 DebugLoc DL
= MIB
->getDebugLoc();
3912 Register Reg
= MIB
->getOperand(0).getReg();
3913 const GlobalValue
*GV
=
3914 cast
<GlobalValue
>((*MIB
->memoperands_begin())->getValue());
3915 auto Flags
= MachineMemOperand::MOLoad
|
3916 MachineMemOperand::MODereferenceable
|
3917 MachineMemOperand::MOInvariant
;
3918 MachineMemOperand
*MMO
= MBB
.getParent()->getMachineMemOperand(
3919 MachinePointerInfo::getGOT(*MBB
.getParent()), Flags
, 8, 8);
3920 MachineBasicBlock::iterator I
= MIB
.getInstr();
3922 BuildMI(MBB
, I
, DL
, TII
.get(X86::MOV64rm
), Reg
).addReg(X86::RIP
).addImm(1)
3923 .addReg(0).addGlobalAddress(GV
, 0, X86II::MO_GOTPCREL
).addReg(0)
3924 .addMemOperand(MMO
);
3925 MIB
->setDebugLoc(DL
);
3926 MIB
->setDesc(TII
.get(X86::MOV64rm
));
3927 MIB
.addReg(Reg
, RegState::Kill
).addImm(1).addReg(0).addImm(0).addReg(0);
3930 static bool expandXorFP(MachineInstrBuilder
&MIB
, const TargetInstrInfo
&TII
) {
3931 MachineBasicBlock
&MBB
= *MIB
->getParent();
3932 MachineFunction
&MF
= *MBB
.getParent();
3933 const X86Subtarget
&Subtarget
= MF
.getSubtarget
<X86Subtarget
>();
3934 const X86RegisterInfo
*TRI
= Subtarget
.getRegisterInfo();
3936 MIB
->getOpcode() == X86::XOR64_FP
? X86::XOR64rr
: X86::XOR32rr
;
3937 MIB
->setDesc(TII
.get(XorOp
));
3938 MIB
.addReg(TRI
->getFrameRegister(MF
), RegState::Undef
);
3942 // This is used to handle spills for 128/256-bit registers when we have AVX512,
3943 // but not VLX. If it uses an extended register we need to use an instruction
3944 // that loads the lower 128/256-bit, but is available with only AVX512F.
3945 static bool expandNOVLXLoad(MachineInstrBuilder
&MIB
,
3946 const TargetRegisterInfo
*TRI
,
3947 const MCInstrDesc
&LoadDesc
,
3948 const MCInstrDesc
&BroadcastDesc
,
3950 Register DestReg
= MIB
->getOperand(0).getReg();
3951 // Check if DestReg is XMM16-31 or YMM16-31.
3952 if (TRI
->getEncodingValue(DestReg
) < 16) {
3953 // We can use a normal VEX encoded load.
3954 MIB
->setDesc(LoadDesc
);
3956 // Use a 128/256-bit VBROADCAST instruction.
3957 MIB
->setDesc(BroadcastDesc
);
3958 // Change the destination to a 512-bit register.
3959 DestReg
= TRI
->getMatchingSuperReg(DestReg
, SubIdx
, &X86::VR512RegClass
);
3960 MIB
->getOperand(0).setReg(DestReg
);
3965 // This is used to handle spills for 128/256-bit registers when we have AVX512,
3966 // but not VLX. If it uses an extended register we need to use an instruction
3967 // that stores the lower 128/256-bit, but is available with only AVX512F.
3968 static bool expandNOVLXStore(MachineInstrBuilder
&MIB
,
3969 const TargetRegisterInfo
*TRI
,
3970 const MCInstrDesc
&StoreDesc
,
3971 const MCInstrDesc
&ExtractDesc
,
3973 Register SrcReg
= MIB
->getOperand(X86::AddrNumOperands
).getReg();
3974 // Check if DestReg is XMM16-31 or YMM16-31.
3975 if (TRI
->getEncodingValue(SrcReg
) < 16) {
3976 // We can use a normal VEX encoded store.
3977 MIB
->setDesc(StoreDesc
);
3979 // Use a VEXTRACTF instruction.
3980 MIB
->setDesc(ExtractDesc
);
3981 // Change the destination to a 512-bit register.
3982 SrcReg
= TRI
->getMatchingSuperReg(SrcReg
, SubIdx
, &X86::VR512RegClass
);
3983 MIB
->getOperand(X86::AddrNumOperands
).setReg(SrcReg
);
3984 MIB
.addImm(0x0); // Append immediate to extract from the lower bits.
3990 static bool expandSHXDROT(MachineInstrBuilder
&MIB
, const MCInstrDesc
&Desc
) {
3992 int64_t ShiftAmt
= MIB
->getOperand(2).getImm();
3993 // Temporarily remove the immediate so we can add another source register.
3994 MIB
->RemoveOperand(2);
3995 // Add the register. Don't copy the kill flag if there is one.
3996 MIB
.addReg(MIB
->getOperand(1).getReg(),
3997 getUndefRegState(MIB
->getOperand(1).isUndef()));
3998 // Add back the immediate.
3999 MIB
.addImm(ShiftAmt
);
4003 bool X86InstrInfo::expandPostRAPseudo(MachineInstr
&MI
) const {
4004 bool HasAVX
= Subtarget
.hasAVX();
4005 MachineInstrBuilder
MIB(*MI
.getParent()->getParent(), MI
);
4006 switch (MI
.getOpcode()) {
4008 return Expand2AddrUndef(MIB
, get(X86::XOR32rr
));
4010 return expandMOV32r1(MIB
, *this, /*MinusOne=*/ false);
4012 return expandMOV32r1(MIB
, *this, /*MinusOne=*/ true);
4013 case X86::MOV32ImmSExti8
:
4014 case X86::MOV64ImmSExti8
:
4015 return ExpandMOVImmSExti8(MIB
, *this, Subtarget
);
4017 return Expand2AddrUndef(MIB
, get(X86::SBB8rr
));
4018 case X86::SETB_C16r
:
4019 return Expand2AddrUndef(MIB
, get(X86::SBB16rr
));
4020 case X86::SETB_C32r
:
4021 return Expand2AddrUndef(MIB
, get(X86::SBB32rr
));
4022 case X86::SETB_C64r
:
4023 return Expand2AddrUndef(MIB
, get(X86::SBB64rr
));
4025 return Expand2AddrUndef(MIB
, get(X86::MMX_PXORirr
));
4029 return Expand2AddrUndef(MIB
, get(HasAVX
? X86::VXORPSrr
: X86::XORPSrr
));
4030 case X86::AVX_SET0
: {
4031 assert(HasAVX
&& "AVX not supported");
4032 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
4033 Register SrcReg
= MIB
->getOperand(0).getReg();
4034 Register XReg
= TRI
->getSubReg(SrcReg
, X86::sub_xmm
);
4035 MIB
->getOperand(0).setReg(XReg
);
4036 Expand2AddrUndef(MIB
, get(X86::VXORPSrr
));
4037 MIB
.addReg(SrcReg
, RegState::ImplicitDefine
);
4040 case X86::AVX512_128_SET0
:
4041 case X86::AVX512_FsFLD0SS
:
4042 case X86::AVX512_FsFLD0SD
: {
4043 bool HasVLX
= Subtarget
.hasVLX();
4044 Register SrcReg
= MIB
->getOperand(0).getReg();
4045 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
4046 if (HasVLX
|| TRI
->getEncodingValue(SrcReg
) < 16)
4047 return Expand2AddrUndef(MIB
,
4048 get(HasVLX
? X86::VPXORDZ128rr
: X86::VXORPSrr
));
4049 // Extended register without VLX. Use a larger XOR.
4051 TRI
->getMatchingSuperReg(SrcReg
, X86::sub_xmm
, &X86::VR512RegClass
);
4052 MIB
->getOperand(0).setReg(SrcReg
);
4053 return Expand2AddrUndef(MIB
, get(X86::VPXORDZrr
));
4055 case X86::AVX512_256_SET0
:
4056 case X86::AVX512_512_SET0
: {
4057 bool HasVLX
= Subtarget
.hasVLX();
4058 Register SrcReg
= MIB
->getOperand(0).getReg();
4059 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
4060 if (HasVLX
|| TRI
->getEncodingValue(SrcReg
) < 16) {
4061 Register XReg
= TRI
->getSubReg(SrcReg
, X86::sub_xmm
);
4062 MIB
->getOperand(0).setReg(XReg
);
4063 Expand2AddrUndef(MIB
,
4064 get(HasVLX
? X86::VPXORDZ128rr
: X86::VXORPSrr
));
4065 MIB
.addReg(SrcReg
, RegState::ImplicitDefine
);
4068 if (MI
.getOpcode() == X86::AVX512_256_SET0
) {
4069 // No VLX so we must reference a zmm.
4071 TRI
->getMatchingSuperReg(SrcReg
, X86::sub_ymm
, &X86::VR512RegClass
);
4072 MIB
->getOperand(0).setReg(ZReg
);
4074 return Expand2AddrUndef(MIB
, get(X86::VPXORDZrr
));
4076 case X86::V_SETALLONES
:
4077 return Expand2AddrUndef(MIB
, get(HasAVX
? X86::VPCMPEQDrr
: X86::PCMPEQDrr
));
4078 case X86::AVX2_SETALLONES
:
4079 return Expand2AddrUndef(MIB
, get(X86::VPCMPEQDYrr
));
4080 case X86::AVX1_SETALLONES
: {
4081 Register Reg
= MIB
->getOperand(0).getReg();
4082 // VCMPPSYrri with an immediate 0xf should produce VCMPTRUEPS.
4083 MIB
->setDesc(get(X86::VCMPPSYrri
));
4084 MIB
.addReg(Reg
, RegState::Undef
).addReg(Reg
, RegState::Undef
).addImm(0xf);
4087 case X86::AVX512_512_SETALLONES
: {
4088 Register Reg
= MIB
->getOperand(0).getReg();
4089 MIB
->setDesc(get(X86::VPTERNLOGDZrri
));
4090 // VPTERNLOGD needs 3 register inputs and an immediate.
4091 // 0xff will return 1s for any input.
4092 MIB
.addReg(Reg
, RegState::Undef
).addReg(Reg
, RegState::Undef
)
4093 .addReg(Reg
, RegState::Undef
).addImm(0xff);
4096 case X86::AVX512_512_SEXT_MASK_32
:
4097 case X86::AVX512_512_SEXT_MASK_64
: {
4098 Register Reg
= MIB
->getOperand(0).getReg();
4099 Register MaskReg
= MIB
->getOperand(1).getReg();
4100 unsigned MaskState
= getRegState(MIB
->getOperand(1));
4101 unsigned Opc
= (MI
.getOpcode() == X86::AVX512_512_SEXT_MASK_64
) ?
4102 X86::VPTERNLOGQZrrikz
: X86::VPTERNLOGDZrrikz
;
4103 MI
.RemoveOperand(1);
4104 MIB
->setDesc(get(Opc
));
4105 // VPTERNLOG needs 3 register inputs and an immediate.
4106 // 0xff will return 1s for any input.
4107 MIB
.addReg(Reg
, RegState::Undef
).addReg(MaskReg
, MaskState
)
4108 .addReg(Reg
, RegState::Undef
).addReg(Reg
, RegState::Undef
).addImm(0xff);
4111 case X86::VMOVAPSZ128rm_NOVLX
:
4112 return expandNOVLXLoad(MIB
, &getRegisterInfo(), get(X86::VMOVAPSrm
),
4113 get(X86::VBROADCASTF32X4rm
), X86::sub_xmm
);
4114 case X86::VMOVUPSZ128rm_NOVLX
:
4115 return expandNOVLXLoad(MIB
, &getRegisterInfo(), get(X86::VMOVUPSrm
),
4116 get(X86::VBROADCASTF32X4rm
), X86::sub_xmm
);
4117 case X86::VMOVAPSZ256rm_NOVLX
:
4118 return expandNOVLXLoad(MIB
, &getRegisterInfo(), get(X86::VMOVAPSYrm
),
4119 get(X86::VBROADCASTF64X4rm
), X86::sub_ymm
);
4120 case X86::VMOVUPSZ256rm_NOVLX
:
4121 return expandNOVLXLoad(MIB
, &getRegisterInfo(), get(X86::VMOVUPSYrm
),
4122 get(X86::VBROADCASTF64X4rm
), X86::sub_ymm
);
4123 case X86::VMOVAPSZ128mr_NOVLX
:
4124 return expandNOVLXStore(MIB
, &getRegisterInfo(), get(X86::VMOVAPSmr
),
4125 get(X86::VEXTRACTF32x4Zmr
), X86::sub_xmm
);
4126 case X86::VMOVUPSZ128mr_NOVLX
:
4127 return expandNOVLXStore(MIB
, &getRegisterInfo(), get(X86::VMOVUPSmr
),
4128 get(X86::VEXTRACTF32x4Zmr
), X86::sub_xmm
);
4129 case X86::VMOVAPSZ256mr_NOVLX
:
4130 return expandNOVLXStore(MIB
, &getRegisterInfo(), get(X86::VMOVAPSYmr
),
4131 get(X86::VEXTRACTF64x4Zmr
), X86::sub_ymm
);
4132 case X86::VMOVUPSZ256mr_NOVLX
:
4133 return expandNOVLXStore(MIB
, &getRegisterInfo(), get(X86::VMOVUPSYmr
),
4134 get(X86::VEXTRACTF64x4Zmr
), X86::sub_ymm
);
4135 case X86::MOV32ri64
: {
4136 Register Reg
= MIB
->getOperand(0).getReg();
4137 Register Reg32
= RI
.getSubReg(Reg
, X86::sub_32bit
);
4138 MI
.setDesc(get(X86::MOV32ri
));
4139 MIB
->getOperand(0).setReg(Reg32
);
4140 MIB
.addReg(Reg
, RegState::ImplicitDefine
);
4144 // KNL does not recognize dependency-breaking idioms for mask registers,
4145 // so kxnor %k1, %k1, %k2 has a RAW dependence on %k1.
4146 // Using %k0 as the undef input register is a performance heuristic based
4147 // on the assumption that %k0 is used less frequently than the other mask
4148 // registers, since it is not usable as a write mask.
4149 // FIXME: A more advanced approach would be to choose the best input mask
4150 // register based on context.
4151 case X86::KSET0W
: return Expand2AddrKreg(MIB
, get(X86::KXORWrr
), X86::K0
);
4152 case X86::KSET0D
: return Expand2AddrKreg(MIB
, get(X86::KXORDrr
), X86::K0
);
4153 case X86::KSET0Q
: return Expand2AddrKreg(MIB
, get(X86::KXORQrr
), X86::K0
);
4154 case X86::KSET1W
: return Expand2AddrKreg(MIB
, get(X86::KXNORWrr
), X86::K0
);
4155 case X86::KSET1D
: return Expand2AddrKreg(MIB
, get(X86::KXNORDrr
), X86::K0
);
4156 case X86::KSET1Q
: return Expand2AddrKreg(MIB
, get(X86::KXNORQrr
), X86::K0
);
4157 case TargetOpcode::LOAD_STACK_GUARD
:
4158 expandLoadStackGuard(MIB
, *this);
4162 return expandXorFP(MIB
, *this);
4163 case X86::SHLDROT32ri
: return expandSHXDROT(MIB
, get(X86::SHLD32rri8
));
4164 case X86::SHLDROT64ri
: return expandSHXDROT(MIB
, get(X86::SHLD64rri8
));
4165 case X86::SHRDROT32ri
: return expandSHXDROT(MIB
, get(X86::SHRD32rri8
));
4166 case X86::SHRDROT64ri
: return expandSHXDROT(MIB
, get(X86::SHRD64rri8
));
4167 case X86::ADD8rr_DB
: MIB
->setDesc(get(X86::OR8rr
)); break;
4168 case X86::ADD16rr_DB
: MIB
->setDesc(get(X86::OR16rr
)); break;
4169 case X86::ADD32rr_DB
: MIB
->setDesc(get(X86::OR32rr
)); break;
4170 case X86::ADD64rr_DB
: MIB
->setDesc(get(X86::OR64rr
)); break;
4171 case X86::ADD8ri_DB
: MIB
->setDesc(get(X86::OR8ri
)); break;
4172 case X86::ADD16ri_DB
: MIB
->setDesc(get(X86::OR16ri
)); break;
4173 case X86::ADD32ri_DB
: MIB
->setDesc(get(X86::OR32ri
)); break;
4174 case X86::ADD64ri32_DB
: MIB
->setDesc(get(X86::OR64ri32
)); break;
4175 case X86::ADD16ri8_DB
: MIB
->setDesc(get(X86::OR16ri8
)); break;
4176 case X86::ADD32ri8_DB
: MIB
->setDesc(get(X86::OR32ri8
)); break;
4177 case X86::ADD64ri8_DB
: MIB
->setDesc(get(X86::OR64ri8
)); break;
4182 /// Return true for all instructions that only update
4183 /// the first 32 or 64-bits of the destination register and leave the rest
4184 /// unmodified. This can be used to avoid folding loads if the instructions
4185 /// only update part of the destination register, and the non-updated part is
4186 /// not needed. e.g. cvtss2sd, sqrtss. Unfolding the load from these
4187 /// instructions breaks the partial register dependency and it can improve
4188 /// performance. e.g.:
4190 /// movss (%rdi), %xmm0
4191 /// cvtss2sd %xmm0, %xmm0
4194 /// cvtss2sd (%rdi), %xmm0
4196 /// FIXME: This should be turned into a TSFlags.
4198 static bool hasPartialRegUpdate(unsigned Opcode
,
4199 const X86Subtarget
&Subtarget
,
4200 bool ForLoadFold
= false) {
4202 case X86::CVTSI2SSrr
:
4203 case X86::CVTSI2SSrm
:
4204 case X86::CVTSI642SSrr
:
4205 case X86::CVTSI642SSrm
:
4206 case X86::CVTSI2SDrr
:
4207 case X86::CVTSI2SDrm
:
4208 case X86::CVTSI642SDrr
:
4209 case X86::CVTSI642SDrm
:
4210 // Load folding won't effect the undef register update since the input is
4212 return !ForLoadFold
;
4213 case X86::CVTSD2SSrr
:
4214 case X86::CVTSD2SSrm
:
4215 case X86::CVTSS2SDrr
:
4216 case X86::CVTSS2SDrm
:
4223 case X86::RCPSSr_Int
:
4224 case X86::RCPSSm_Int
:
4231 case X86::RSQRTSSr_Int
:
4232 case X86::RSQRTSSm_Int
:
4235 case X86::SQRTSSr_Int
:
4236 case X86::SQRTSSm_Int
:
4239 case X86::SQRTSDr_Int
:
4240 case X86::SQRTSDm_Int
:
4243 case X86::POPCNT32rm
:
4244 case X86::POPCNT32rr
:
4245 case X86::POPCNT64rm
:
4246 case X86::POPCNT64rr
:
4247 return Subtarget
.hasPOPCNTFalseDeps();
4248 case X86::LZCNT32rm
:
4249 case X86::LZCNT32rr
:
4250 case X86::LZCNT64rm
:
4251 case X86::LZCNT64rr
:
4252 case X86::TZCNT32rm
:
4253 case X86::TZCNT32rr
:
4254 case X86::TZCNT64rm
:
4255 case X86::TZCNT64rr
:
4256 return Subtarget
.hasLZCNTFalseDeps();
4262 /// Inform the BreakFalseDeps pass how many idle
4263 /// instructions we would like before a partial register update.
4264 unsigned X86InstrInfo::getPartialRegUpdateClearance(
4265 const MachineInstr
&MI
, unsigned OpNum
,
4266 const TargetRegisterInfo
*TRI
) const {
4267 if (OpNum
!= 0 || !hasPartialRegUpdate(MI
.getOpcode(), Subtarget
))
4270 // If MI is marked as reading Reg, the partial register update is wanted.
4271 const MachineOperand
&MO
= MI
.getOperand(0);
4272 Register Reg
= MO
.getReg();
4273 if (Register::isVirtualRegister(Reg
)) {
4274 if (MO
.readsReg() || MI
.readsVirtualRegister(Reg
))
4277 if (MI
.readsRegister(Reg
, TRI
))
4281 // If any instructions in the clearance range are reading Reg, insert a
4282 // dependency breaking instruction, which is inexpensive and is likely to
4283 // be hidden in other instruction's cycles.
4284 return PartialRegUpdateClearance
;
4287 // Return true for any instruction the copies the high bits of the first source
4288 // operand into the unused high bits of the destination operand.
4289 static bool hasUndefRegUpdate(unsigned Opcode
, bool ForLoadFold
= false) {
4291 case X86::VCVTSI2SSrr
:
4292 case X86::VCVTSI2SSrm
:
4293 case X86::VCVTSI2SSrr_Int
:
4294 case X86::VCVTSI2SSrm_Int
:
4295 case X86::VCVTSI642SSrr
:
4296 case X86::VCVTSI642SSrm
:
4297 case X86::VCVTSI642SSrr_Int
:
4298 case X86::VCVTSI642SSrm_Int
:
4299 case X86::VCVTSI2SDrr
:
4300 case X86::VCVTSI2SDrm
:
4301 case X86::VCVTSI2SDrr_Int
:
4302 case X86::VCVTSI2SDrm_Int
:
4303 case X86::VCVTSI642SDrr
:
4304 case X86::VCVTSI642SDrm
:
4305 case X86::VCVTSI642SDrr_Int
:
4306 case X86::VCVTSI642SDrm_Int
:
4308 case X86::VCVTSI2SSZrr
:
4309 case X86::VCVTSI2SSZrm
:
4310 case X86::VCVTSI2SSZrr_Int
:
4311 case X86::VCVTSI2SSZrrb_Int
:
4312 case X86::VCVTSI2SSZrm_Int
:
4313 case X86::VCVTSI642SSZrr
:
4314 case X86::VCVTSI642SSZrm
:
4315 case X86::VCVTSI642SSZrr_Int
:
4316 case X86::VCVTSI642SSZrrb_Int
:
4317 case X86::VCVTSI642SSZrm_Int
:
4318 case X86::VCVTSI2SDZrr
:
4319 case X86::VCVTSI2SDZrm
:
4320 case X86::VCVTSI2SDZrr_Int
:
4321 case X86::VCVTSI2SDZrm_Int
:
4322 case X86::VCVTSI642SDZrr
:
4323 case X86::VCVTSI642SDZrm
:
4324 case X86::VCVTSI642SDZrr_Int
:
4325 case X86::VCVTSI642SDZrrb_Int
:
4326 case X86::VCVTSI642SDZrm_Int
:
4327 case X86::VCVTUSI2SSZrr
:
4328 case X86::VCVTUSI2SSZrm
:
4329 case X86::VCVTUSI2SSZrr_Int
:
4330 case X86::VCVTUSI2SSZrrb_Int
:
4331 case X86::VCVTUSI2SSZrm_Int
:
4332 case X86::VCVTUSI642SSZrr
:
4333 case X86::VCVTUSI642SSZrm
:
4334 case X86::VCVTUSI642SSZrr_Int
:
4335 case X86::VCVTUSI642SSZrrb_Int
:
4336 case X86::VCVTUSI642SSZrm_Int
:
4337 case X86::VCVTUSI2SDZrr
:
4338 case X86::VCVTUSI2SDZrm
:
4339 case X86::VCVTUSI2SDZrr_Int
:
4340 case X86::VCVTUSI2SDZrm_Int
:
4341 case X86::VCVTUSI642SDZrr
:
4342 case X86::VCVTUSI642SDZrm
:
4343 case X86::VCVTUSI642SDZrr_Int
:
4344 case X86::VCVTUSI642SDZrrb_Int
:
4345 case X86::VCVTUSI642SDZrm_Int
:
4346 // Load folding won't effect the undef register update since the input is
4348 return !ForLoadFold
;
4349 case X86::VCVTSD2SSrr
:
4350 case X86::VCVTSD2SSrm
:
4351 case X86::VCVTSD2SSrr_Int
:
4352 case X86::VCVTSD2SSrm_Int
:
4353 case X86::VCVTSS2SDrr
:
4354 case X86::VCVTSS2SDrm
:
4355 case X86::VCVTSS2SDrr_Int
:
4356 case X86::VCVTSS2SDrm_Int
:
4358 case X86::VRCPSSr_Int
:
4360 case X86::VRCPSSm_Int
:
4361 case X86::VROUNDSDr
:
4362 case X86::VROUNDSDm
:
4363 case X86::VROUNDSDr_Int
:
4364 case X86::VROUNDSDm_Int
:
4365 case X86::VROUNDSSr
:
4366 case X86::VROUNDSSm
:
4367 case X86::VROUNDSSr_Int
:
4368 case X86::VROUNDSSm_Int
:
4369 case X86::VRSQRTSSr
:
4370 case X86::VRSQRTSSr_Int
:
4371 case X86::VRSQRTSSm
:
4372 case X86::VRSQRTSSm_Int
:
4374 case X86::VSQRTSSr_Int
:
4376 case X86::VSQRTSSm_Int
:
4378 case X86::VSQRTSDr_Int
:
4380 case X86::VSQRTSDm_Int
:
4382 case X86::VCVTSD2SSZrr
:
4383 case X86::VCVTSD2SSZrr_Int
:
4384 case X86::VCVTSD2SSZrrb_Int
:
4385 case X86::VCVTSD2SSZrm
:
4386 case X86::VCVTSD2SSZrm_Int
:
4387 case X86::VCVTSS2SDZrr
:
4388 case X86::VCVTSS2SDZrr_Int
:
4389 case X86::VCVTSS2SDZrrb_Int
:
4390 case X86::VCVTSS2SDZrm
:
4391 case X86::VCVTSS2SDZrm_Int
:
4392 case X86::VGETEXPSDZr
:
4393 case X86::VGETEXPSDZrb
:
4394 case X86::VGETEXPSDZm
:
4395 case X86::VGETEXPSSZr
:
4396 case X86::VGETEXPSSZrb
:
4397 case X86::VGETEXPSSZm
:
4398 case X86::VGETMANTSDZrri
:
4399 case X86::VGETMANTSDZrrib
:
4400 case X86::VGETMANTSDZrmi
:
4401 case X86::VGETMANTSSZrri
:
4402 case X86::VGETMANTSSZrrib
:
4403 case X86::VGETMANTSSZrmi
:
4404 case X86::VRNDSCALESDZr
:
4405 case X86::VRNDSCALESDZr_Int
:
4406 case X86::VRNDSCALESDZrb_Int
:
4407 case X86::VRNDSCALESDZm
:
4408 case X86::VRNDSCALESDZm_Int
:
4409 case X86::VRNDSCALESSZr
:
4410 case X86::VRNDSCALESSZr_Int
:
4411 case X86::VRNDSCALESSZrb_Int
:
4412 case X86::VRNDSCALESSZm
:
4413 case X86::VRNDSCALESSZm_Int
:
4414 case X86::VRCP14SDZrr
:
4415 case X86::VRCP14SDZrm
:
4416 case X86::VRCP14SSZrr
:
4417 case X86::VRCP14SSZrm
:
4418 case X86::VRCP28SDZr
:
4419 case X86::VRCP28SDZrb
:
4420 case X86::VRCP28SDZm
:
4421 case X86::VRCP28SSZr
:
4422 case X86::VRCP28SSZrb
:
4423 case X86::VRCP28SSZm
:
4424 case X86::VREDUCESSZrmi
:
4425 case X86::VREDUCESSZrri
:
4426 case X86::VREDUCESSZrrib
:
4427 case X86::VRSQRT14SDZrr
:
4428 case X86::VRSQRT14SDZrm
:
4429 case X86::VRSQRT14SSZrr
:
4430 case X86::VRSQRT14SSZrm
:
4431 case X86::VRSQRT28SDZr
:
4432 case X86::VRSQRT28SDZrb
:
4433 case X86::VRSQRT28SDZm
:
4434 case X86::VRSQRT28SSZr
:
4435 case X86::VRSQRT28SSZrb
:
4436 case X86::VRSQRT28SSZm
:
4437 case X86::VSQRTSSZr
:
4438 case X86::VSQRTSSZr_Int
:
4439 case X86::VSQRTSSZrb_Int
:
4440 case X86::VSQRTSSZm
:
4441 case X86::VSQRTSSZm_Int
:
4442 case X86::VSQRTSDZr
:
4443 case X86::VSQRTSDZr_Int
:
4444 case X86::VSQRTSDZrb_Int
:
4445 case X86::VSQRTSDZm
:
4446 case X86::VSQRTSDZm_Int
:
4453 /// Inform the BreakFalseDeps pass how many idle instructions we would like
4454 /// before certain undef register reads.
4456 /// This catches the VCVTSI2SD family of instructions:
4458 /// vcvtsi2sdq %rax, undef %xmm0, %xmm14
4460 /// We should to be careful *not* to catch VXOR idioms which are presumably
4461 /// handled specially in the pipeline:
4463 /// vxorps undef %xmm1, undef %xmm1, %xmm1
4465 /// Like getPartialRegUpdateClearance, this makes a strong assumption that the
4466 /// high bits that are passed-through are not live.
4468 X86InstrInfo::getUndefRegClearance(const MachineInstr
&MI
, unsigned &OpNum
,
4469 const TargetRegisterInfo
*TRI
) const {
4470 if (!hasUndefRegUpdate(MI
.getOpcode()))
4473 // Set the OpNum parameter to the first source operand.
4476 const MachineOperand
&MO
= MI
.getOperand(OpNum
);
4477 if (MO
.isUndef() && Register::isPhysicalRegister(MO
.getReg())) {
4478 return UndefRegClearance
;
4483 void X86InstrInfo::breakPartialRegDependency(
4484 MachineInstr
&MI
, unsigned OpNum
, const TargetRegisterInfo
*TRI
) const {
4485 Register Reg
= MI
.getOperand(OpNum
).getReg();
4486 // If MI kills this register, the false dependence is already broken.
4487 if (MI
.killsRegister(Reg
, TRI
))
4490 if (X86::VR128RegClass
.contains(Reg
)) {
4491 // These instructions are all floating point domain, so xorps is the best
4493 unsigned Opc
= Subtarget
.hasAVX() ? X86::VXORPSrr
: X86::XORPSrr
;
4494 BuildMI(*MI
.getParent(), MI
, MI
.getDebugLoc(), get(Opc
), Reg
)
4495 .addReg(Reg
, RegState::Undef
)
4496 .addReg(Reg
, RegState::Undef
);
4497 MI
.addRegisterKilled(Reg
, TRI
, true);
4498 } else if (X86::VR256RegClass
.contains(Reg
)) {
4499 // Use vxorps to clear the full ymm register.
4500 // It wants to read and write the xmm sub-register.
4501 Register XReg
= TRI
->getSubReg(Reg
, X86::sub_xmm
);
4502 BuildMI(*MI
.getParent(), MI
, MI
.getDebugLoc(), get(X86::VXORPSrr
), XReg
)
4503 .addReg(XReg
, RegState::Undef
)
4504 .addReg(XReg
, RegState::Undef
)
4505 .addReg(Reg
, RegState::ImplicitDefine
);
4506 MI
.addRegisterKilled(Reg
, TRI
, true);
4507 } else if (X86::GR64RegClass
.contains(Reg
)) {
4508 // Using XOR32rr because it has shorter encoding and zeros up the upper bits
4510 Register XReg
= TRI
->getSubReg(Reg
, X86::sub_32bit
);
4511 BuildMI(*MI
.getParent(), MI
, MI
.getDebugLoc(), get(X86::XOR32rr
), XReg
)
4512 .addReg(XReg
, RegState::Undef
)
4513 .addReg(XReg
, RegState::Undef
)
4514 .addReg(Reg
, RegState::ImplicitDefine
);
4515 MI
.addRegisterKilled(Reg
, TRI
, true);
4516 } else if (X86::GR32RegClass
.contains(Reg
)) {
4517 BuildMI(*MI
.getParent(), MI
, MI
.getDebugLoc(), get(X86::XOR32rr
), Reg
)
4518 .addReg(Reg
, RegState::Undef
)
4519 .addReg(Reg
, RegState::Undef
);
4520 MI
.addRegisterKilled(Reg
, TRI
, true);
4524 static void addOperands(MachineInstrBuilder
&MIB
, ArrayRef
<MachineOperand
> MOs
,
4525 int PtrOffset
= 0) {
4526 unsigned NumAddrOps
= MOs
.size();
4528 if (NumAddrOps
< 4) {
4529 // FrameIndex only - add an immediate offset (whether its zero or not).
4530 for (unsigned i
= 0; i
!= NumAddrOps
; ++i
)
4532 addOffset(MIB
, PtrOffset
);
4534 // General Memory Addressing - we need to add any offset to an existing
4536 assert(MOs
.size() == 5 && "Unexpected memory operand list length");
4537 for (unsigned i
= 0; i
!= NumAddrOps
; ++i
) {
4538 const MachineOperand
&MO
= MOs
[i
];
4539 if (i
== 3 && PtrOffset
!= 0) {
4540 MIB
.addDisp(MO
, PtrOffset
);
4548 static void updateOperandRegConstraints(MachineFunction
&MF
,
4549 MachineInstr
&NewMI
,
4550 const TargetInstrInfo
&TII
) {
4551 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
4552 const TargetRegisterInfo
&TRI
= *MRI
.getTargetRegisterInfo();
4554 for (int Idx
: llvm::seq
<int>(0, NewMI
.getNumOperands())) {
4555 MachineOperand
&MO
= NewMI
.getOperand(Idx
);
4556 // We only need to update constraints on virtual register operands.
4559 Register Reg
= MO
.getReg();
4560 if (!Register::isVirtualRegister(Reg
))
4563 auto *NewRC
= MRI
.constrainRegClass(
4564 Reg
, TII
.getRegClass(NewMI
.getDesc(), Idx
, &TRI
, MF
));
4567 dbgs() << "WARNING: Unable to update register constraint for operand "
4568 << Idx
<< " of instruction:\n";
4569 NewMI
.dump(); dbgs() << "\n");
4574 static MachineInstr
*FuseTwoAddrInst(MachineFunction
&MF
, unsigned Opcode
,
4575 ArrayRef
<MachineOperand
> MOs
,
4576 MachineBasicBlock::iterator InsertPt
,
4578 const TargetInstrInfo
&TII
) {
4579 // Create the base instruction with the memory operand as the first part.
4580 // Omit the implicit operands, something BuildMI can't do.
4581 MachineInstr
*NewMI
=
4582 MF
.CreateMachineInstr(TII
.get(Opcode
), MI
.getDebugLoc(), true);
4583 MachineInstrBuilder
MIB(MF
, NewMI
);
4584 addOperands(MIB
, MOs
);
4586 // Loop over the rest of the ri operands, converting them over.
4587 unsigned NumOps
= MI
.getDesc().getNumOperands() - 2;
4588 for (unsigned i
= 0; i
!= NumOps
; ++i
) {
4589 MachineOperand
&MO
= MI
.getOperand(i
+ 2);
4592 for (unsigned i
= NumOps
+ 2, e
= MI
.getNumOperands(); i
!= e
; ++i
) {
4593 MachineOperand
&MO
= MI
.getOperand(i
);
4597 updateOperandRegConstraints(MF
, *NewMI
, TII
);
4599 MachineBasicBlock
*MBB
= InsertPt
->getParent();
4600 MBB
->insert(InsertPt
, NewMI
);
4605 static MachineInstr
*FuseInst(MachineFunction
&MF
, unsigned Opcode
,
4606 unsigned OpNo
, ArrayRef
<MachineOperand
> MOs
,
4607 MachineBasicBlock::iterator InsertPt
,
4608 MachineInstr
&MI
, const TargetInstrInfo
&TII
,
4609 int PtrOffset
= 0) {
4610 // Omit the implicit operands, something BuildMI can't do.
4611 MachineInstr
*NewMI
=
4612 MF
.CreateMachineInstr(TII
.get(Opcode
), MI
.getDebugLoc(), true);
4613 MachineInstrBuilder
MIB(MF
, NewMI
);
4615 for (unsigned i
= 0, e
= MI
.getNumOperands(); i
!= e
; ++i
) {
4616 MachineOperand
&MO
= MI
.getOperand(i
);
4618 assert(MO
.isReg() && "Expected to fold into reg operand!");
4619 addOperands(MIB
, MOs
, PtrOffset
);
4625 updateOperandRegConstraints(MF
, *NewMI
, TII
);
4627 MachineBasicBlock
*MBB
= InsertPt
->getParent();
4628 MBB
->insert(InsertPt
, NewMI
);
4633 static MachineInstr
*MakeM0Inst(const TargetInstrInfo
&TII
, unsigned Opcode
,
4634 ArrayRef
<MachineOperand
> MOs
,
4635 MachineBasicBlock::iterator InsertPt
,
4637 MachineInstrBuilder MIB
= BuildMI(*InsertPt
->getParent(), InsertPt
,
4638 MI
.getDebugLoc(), TII
.get(Opcode
));
4639 addOperands(MIB
, MOs
);
4640 return MIB
.addImm(0);
4643 MachineInstr
*X86InstrInfo::foldMemoryOperandCustom(
4644 MachineFunction
&MF
, MachineInstr
&MI
, unsigned OpNum
,
4645 ArrayRef
<MachineOperand
> MOs
, MachineBasicBlock::iterator InsertPt
,
4646 unsigned Size
, unsigned Align
) const {
4647 switch (MI
.getOpcode()) {
4648 case X86::INSERTPSrr
:
4649 case X86::VINSERTPSrr
:
4650 case X86::VINSERTPSZrr
:
4651 // Attempt to convert the load of inserted vector into a fold load
4652 // of a single float.
4654 unsigned Imm
= MI
.getOperand(MI
.getNumOperands() - 1).getImm();
4655 unsigned ZMask
= Imm
& 15;
4656 unsigned DstIdx
= (Imm
>> 4) & 3;
4657 unsigned SrcIdx
= (Imm
>> 6) & 3;
4659 const TargetRegisterInfo
&TRI
= *MF
.getSubtarget().getRegisterInfo();
4660 const TargetRegisterClass
*RC
= getRegClass(MI
.getDesc(), OpNum
, &RI
, MF
);
4661 unsigned RCSize
= TRI
.getRegSizeInBits(*RC
) / 8;
4662 if ((Size
== 0 || Size
>= 16) && RCSize
>= 16 && 4 <= Align
) {
4663 int PtrOffset
= SrcIdx
* 4;
4664 unsigned NewImm
= (DstIdx
<< 4) | ZMask
;
4665 unsigned NewOpCode
=
4666 (MI
.getOpcode() == X86::VINSERTPSZrr
) ? X86::VINSERTPSZrm
:
4667 (MI
.getOpcode() == X86::VINSERTPSrr
) ? X86::VINSERTPSrm
:
4669 MachineInstr
*NewMI
=
4670 FuseInst(MF
, NewOpCode
, OpNum
, MOs
, InsertPt
, MI
, *this, PtrOffset
);
4671 NewMI
->getOperand(NewMI
->getNumOperands() - 1).setImm(NewImm
);
4676 case X86::MOVHLPSrr
:
4677 case X86::VMOVHLPSrr
:
4678 case X86::VMOVHLPSZrr
:
4679 // Move the upper 64-bits of the second operand to the lower 64-bits.
4680 // To fold the load, adjust the pointer to the upper and use (V)MOVLPS.
4681 // TODO: In most cases AVX doesn't have a 8-byte alignment requirement.
4683 const TargetRegisterInfo
&TRI
= *MF
.getSubtarget().getRegisterInfo();
4684 const TargetRegisterClass
*RC
= getRegClass(MI
.getDesc(), OpNum
, &RI
, MF
);
4685 unsigned RCSize
= TRI
.getRegSizeInBits(*RC
) / 8;
4686 if ((Size
== 0 || Size
>= 16) && RCSize
>= 16 && 8 <= Align
) {
4687 unsigned NewOpCode
=
4688 (MI
.getOpcode() == X86::VMOVHLPSZrr
) ? X86::VMOVLPSZ128rm
:
4689 (MI
.getOpcode() == X86::VMOVHLPSrr
) ? X86::VMOVLPSrm
:
4691 MachineInstr
*NewMI
=
4692 FuseInst(MF
, NewOpCode
, OpNum
, MOs
, InsertPt
, MI
, *this, 8);
4697 case X86::UNPCKLPDrr
:
4698 // If we won't be able to fold this to the memory form of UNPCKL, use
4699 // MOVHPD instead. Done as custom because we can't have this in the load
4702 const TargetRegisterInfo
&TRI
= *MF
.getSubtarget().getRegisterInfo();
4703 const TargetRegisterClass
*RC
= getRegClass(MI
.getDesc(), OpNum
, &RI
, MF
);
4704 unsigned RCSize
= TRI
.getRegSizeInBits(*RC
) / 8;
4705 if ((Size
== 0 || Size
>= 16) && RCSize
>= 16 && Align
< 16) {
4706 MachineInstr
*NewMI
=
4707 FuseInst(MF
, X86::MOVHPDrm
, OpNum
, MOs
, InsertPt
, MI
, *this);
4717 static bool shouldPreventUndefRegUpdateMemFold(MachineFunction
&MF
,
4719 if (!hasUndefRegUpdate(MI
.getOpcode(), /*ForLoadFold*/true) ||
4720 !MI
.getOperand(1).isReg())
4723 // The are two cases we need to handle depending on where in the pipeline
4724 // the folding attempt is being made.
4725 // -Register has the undef flag set.
4726 // -Register is produced by the IMPLICIT_DEF instruction.
4728 if (MI
.getOperand(1).isUndef())
4731 MachineRegisterInfo
&RegInfo
= MF
.getRegInfo();
4732 MachineInstr
*VRegDef
= RegInfo
.getUniqueVRegDef(MI
.getOperand(1).getReg());
4733 return VRegDef
&& VRegDef
->isImplicitDef();
4737 MachineInstr
*X86InstrInfo::foldMemoryOperandImpl(
4738 MachineFunction
&MF
, MachineInstr
&MI
, unsigned OpNum
,
4739 ArrayRef
<MachineOperand
> MOs
, MachineBasicBlock::iterator InsertPt
,
4740 unsigned Size
, unsigned Align
, bool AllowCommute
) const {
4741 bool isSlowTwoMemOps
= Subtarget
.slowTwoMemOps();
4742 bool isTwoAddrFold
= false;
4744 // For CPUs that favor the register form of a call or push,
4745 // do not fold loads into calls or pushes, unless optimizing for size
4747 if (isSlowTwoMemOps
&& !MF
.getFunction().hasMinSize() &&
4748 (MI
.getOpcode() == X86::CALL32r
|| MI
.getOpcode() == X86::CALL64r
||
4749 MI
.getOpcode() == X86::PUSH16r
|| MI
.getOpcode() == X86::PUSH32r
||
4750 MI
.getOpcode() == X86::PUSH64r
))
4753 // Avoid partial and undef register update stalls unless optimizing for size.
4754 if (!MF
.getFunction().hasOptSize() &&
4755 (hasPartialRegUpdate(MI
.getOpcode(), Subtarget
, /*ForLoadFold*/true) ||
4756 shouldPreventUndefRegUpdateMemFold(MF
, MI
)))
4759 unsigned NumOps
= MI
.getDesc().getNumOperands();
4761 NumOps
> 1 && MI
.getDesc().getOperandConstraint(1, MCOI::TIED_TO
) != -1;
4763 // FIXME: AsmPrinter doesn't know how to handle
4764 // X86II::MO_GOT_ABSOLUTE_ADDRESS after folding.
4765 if (MI
.getOpcode() == X86::ADD32ri
&&
4766 MI
.getOperand(2).getTargetFlags() == X86II::MO_GOT_ABSOLUTE_ADDRESS
)
4769 // GOTTPOFF relocation loads can only be folded into add instructions.
4770 // FIXME: Need to exclude other relocations that only support specific
4772 if (MOs
.size() == X86::AddrNumOperands
&&
4773 MOs
[X86::AddrDisp
].getTargetFlags() == X86II::MO_GOTTPOFF
&&
4774 MI
.getOpcode() != X86::ADD64rr
)
4777 MachineInstr
*NewMI
= nullptr;
4779 // Attempt to fold any custom cases we have.
4780 if (MachineInstr
*CustomMI
=
4781 foldMemoryOperandCustom(MF
, MI
, OpNum
, MOs
, InsertPt
, Size
, Align
))
4784 const X86MemoryFoldTableEntry
*I
= nullptr;
4786 // Folding a memory location into the two-address part of a two-address
4787 // instruction is different than folding it other places. It requires
4788 // replacing the *two* registers with the memory location.
4789 if (isTwoAddr
&& NumOps
>= 2 && OpNum
< 2 && MI
.getOperand(0).isReg() &&
4790 MI
.getOperand(1).isReg() &&
4791 MI
.getOperand(0).getReg() == MI
.getOperand(1).getReg()) {
4792 I
= lookupTwoAddrFoldTable(MI
.getOpcode());
4793 isTwoAddrFold
= true;
4796 if (MI
.getOpcode() == X86::MOV32r0
) {
4797 NewMI
= MakeM0Inst(*this, X86::MOV32mi
, MOs
, InsertPt
, MI
);
4803 I
= lookupFoldTable(MI
.getOpcode(), OpNum
);
4807 unsigned Opcode
= I
->DstOp
;
4808 unsigned MinAlign
= (I
->Flags
& TB_ALIGN_MASK
) >> TB_ALIGN_SHIFT
;
4809 MinAlign
= MinAlign
? 1 << (MinAlign
- 1) : 0;
4810 if (Align
< MinAlign
)
4812 bool NarrowToMOV32rm
= false;
4814 const TargetRegisterInfo
&TRI
= *MF
.getSubtarget().getRegisterInfo();
4815 const TargetRegisterClass
*RC
= getRegClass(MI
.getDesc(), OpNum
,
4817 unsigned RCSize
= TRI
.getRegSizeInBits(*RC
) / 8;
4818 if (Size
< RCSize
) {
4819 // FIXME: Allow scalar intrinsic instructions like ADDSSrm_Int.
4820 // Check if it's safe to fold the load. If the size of the object is
4821 // narrower than the load width, then it's not.
4822 if (Opcode
!= X86::MOV64rm
|| RCSize
!= 8 || Size
!= 4)
4824 // If this is a 64-bit load, but the spill slot is 32, then we can do
4825 // a 32-bit load which is implicitly zero-extended. This likely is
4826 // due to live interval analysis remat'ing a load from stack slot.
4827 if (MI
.getOperand(0).getSubReg() || MI
.getOperand(1).getSubReg())
4829 Opcode
= X86::MOV32rm
;
4830 NarrowToMOV32rm
= true;
4835 NewMI
= FuseTwoAddrInst(MF
, Opcode
, MOs
, InsertPt
, MI
, *this);
4837 NewMI
= FuseInst(MF
, Opcode
, OpNum
, MOs
, InsertPt
, MI
, *this);
4839 if (NarrowToMOV32rm
) {
4840 // If this is the special case where we use a MOV32rm to load a 32-bit
4841 // value and zero-extend the top bits. Change the destination register
4843 Register DstReg
= NewMI
->getOperand(0).getReg();
4844 if (Register::isPhysicalRegister(DstReg
))
4845 NewMI
->getOperand(0).setReg(RI
.getSubReg(DstReg
, X86::sub_32bit
));
4847 NewMI
->getOperand(0).setSubReg(X86::sub_32bit
);
4852 // If the instruction and target operand are commutable, commute the
4853 // instruction and try again.
4855 unsigned CommuteOpIdx1
= OpNum
, CommuteOpIdx2
= CommuteAnyOperandIndex
;
4856 if (findCommutedOpIndices(MI
, CommuteOpIdx1
, CommuteOpIdx2
)) {
4857 bool HasDef
= MI
.getDesc().getNumDefs();
4858 Register Reg0
= HasDef
? MI
.getOperand(0).getReg() : Register();
4859 Register Reg1
= MI
.getOperand(CommuteOpIdx1
).getReg();
4860 Register Reg2
= MI
.getOperand(CommuteOpIdx2
).getReg();
4862 0 == MI
.getDesc().getOperandConstraint(CommuteOpIdx1
, MCOI::TIED_TO
);
4864 0 == MI
.getDesc().getOperandConstraint(CommuteOpIdx2
, MCOI::TIED_TO
);
4866 // If either of the commutable operands are tied to the destination
4867 // then we can not commute + fold.
4868 if ((HasDef
&& Reg0
== Reg1
&& Tied1
) ||
4869 (HasDef
&& Reg0
== Reg2
&& Tied2
))
4872 MachineInstr
*CommutedMI
=
4873 commuteInstruction(MI
, false, CommuteOpIdx1
, CommuteOpIdx2
);
4875 // Unable to commute.
4878 if (CommutedMI
!= &MI
) {
4879 // New instruction. We can't fold from this.
4880 CommutedMI
->eraseFromParent();
4884 // Attempt to fold with the commuted version of the instruction.
4885 NewMI
= foldMemoryOperandImpl(MF
, MI
, CommuteOpIdx2
, MOs
, InsertPt
,
4886 Size
, Align
, /*AllowCommute=*/false);
4890 // Folding failed again - undo the commute before returning.
4891 MachineInstr
*UncommutedMI
=
4892 commuteInstruction(MI
, false, CommuteOpIdx1
, CommuteOpIdx2
);
4893 if (!UncommutedMI
) {
4894 // Unable to commute.
4897 if (UncommutedMI
!= &MI
) {
4898 // New instruction. It doesn't need to be kept.
4899 UncommutedMI
->eraseFromParent();
4903 // Return here to prevent duplicate fuse failure report.
4909 if (PrintFailedFusing
&& !MI
.isCopy())
4910 dbgs() << "We failed to fuse operand " << OpNum
<< " in " << MI
;
4915 X86InstrInfo::foldMemoryOperandImpl(MachineFunction
&MF
, MachineInstr
&MI
,
4916 ArrayRef
<unsigned> Ops
,
4917 MachineBasicBlock::iterator InsertPt
,
4918 int FrameIndex
, LiveIntervals
*LIS
,
4919 VirtRegMap
*VRM
) const {
4920 // Check switch flag
4924 // Avoid partial and undef register update stalls unless optimizing for size.
4925 if (!MF
.getFunction().hasOptSize() &&
4926 (hasPartialRegUpdate(MI
.getOpcode(), Subtarget
, /*ForLoadFold*/true) ||
4927 shouldPreventUndefRegUpdateMemFold(MF
, MI
)))
4930 // Don't fold subreg spills, or reloads that use a high subreg.
4931 for (auto Op
: Ops
) {
4932 MachineOperand
&MO
= MI
.getOperand(Op
);
4933 auto SubReg
= MO
.getSubReg();
4934 if (SubReg
&& (MO
.isDef() || SubReg
== X86::sub_8bit_hi
))
4938 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
4939 unsigned Size
= MFI
.getObjectSize(FrameIndex
);
4940 unsigned Alignment
= MFI
.getObjectAlignment(FrameIndex
);
4941 // If the function stack isn't realigned we don't want to fold instructions
4942 // that need increased alignment.
4943 if (!RI
.needsStackRealignment(MF
))
4945 std::min(Alignment
, Subtarget
.getFrameLowering()->getStackAlignment());
4946 if (Ops
.size() == 2 && Ops
[0] == 0 && Ops
[1] == 1) {
4947 unsigned NewOpc
= 0;
4948 unsigned RCSize
= 0;
4949 switch (MI
.getOpcode()) {
4950 default: return nullptr;
4951 case X86::TEST8rr
: NewOpc
= X86::CMP8ri
; RCSize
= 1; break;
4952 case X86::TEST16rr
: NewOpc
= X86::CMP16ri8
; RCSize
= 2; break;
4953 case X86::TEST32rr
: NewOpc
= X86::CMP32ri8
; RCSize
= 4; break;
4954 case X86::TEST64rr
: NewOpc
= X86::CMP64ri8
; RCSize
= 8; break;
4956 // Check if it's safe to fold the load. If the size of the object is
4957 // narrower than the load width, then it's not.
4960 // Change to CMPXXri r, 0 first.
4961 MI
.setDesc(get(NewOpc
));
4962 MI
.getOperand(1).ChangeToImmediate(0);
4963 } else if (Ops
.size() != 1)
4966 return foldMemoryOperandImpl(MF
, MI
, Ops
[0],
4967 MachineOperand::CreateFI(FrameIndex
), InsertPt
,
4968 Size
, Alignment
, /*AllowCommute=*/true);
4971 /// Check if \p LoadMI is a partial register load that we can't fold into \p MI
4972 /// because the latter uses contents that wouldn't be defined in the folded
4973 /// version. For instance, this transformation isn't legal:
4974 /// movss (%rdi), %xmm0
4975 /// addps %xmm0, %xmm0
4977 /// addps (%rdi), %xmm0
4979 /// But this one is:
4980 /// movss (%rdi), %xmm0
4981 /// addss %xmm0, %xmm0
4983 /// addss (%rdi), %xmm0
4985 static bool isNonFoldablePartialRegisterLoad(const MachineInstr
&LoadMI
,
4986 const MachineInstr
&UserMI
,
4987 const MachineFunction
&MF
) {
4988 unsigned Opc
= LoadMI
.getOpcode();
4989 unsigned UserOpc
= UserMI
.getOpcode();
4990 const TargetRegisterInfo
&TRI
= *MF
.getSubtarget().getRegisterInfo();
4991 const TargetRegisterClass
*RC
=
4992 MF
.getRegInfo().getRegClass(LoadMI
.getOperand(0).getReg());
4993 unsigned RegSize
= TRI
.getRegSizeInBits(*RC
);
4995 if ((Opc
== X86::MOVSSrm
|| Opc
== X86::VMOVSSrm
|| Opc
== X86::VMOVSSZrm
||
4996 Opc
== X86::MOVSSrm_alt
|| Opc
== X86::VMOVSSrm_alt
||
4997 Opc
== X86::VMOVSSZrm_alt
) &&
4999 // These instructions only load 32 bits, we can't fold them if the
5000 // destination register is wider than 32 bits (4 bytes), and its user
5001 // instruction isn't scalar (SS).
5003 case X86::ADDSSrr_Int
: case X86::VADDSSrr_Int
: case X86::VADDSSZrr_Int
:
5004 case X86::CMPSSrr_Int
: case X86::VCMPSSrr_Int
: case X86::VCMPSSZrr_Int
:
5005 case X86::DIVSSrr_Int
: case X86::VDIVSSrr_Int
: case X86::VDIVSSZrr_Int
:
5006 case X86::MAXSSrr_Int
: case X86::VMAXSSrr_Int
: case X86::VMAXSSZrr_Int
:
5007 case X86::MINSSrr_Int
: case X86::VMINSSrr_Int
: case X86::VMINSSZrr_Int
:
5008 case X86::MULSSrr_Int
: case X86::VMULSSrr_Int
: case X86::VMULSSZrr_Int
:
5009 case X86::SUBSSrr_Int
: case X86::VSUBSSrr_Int
: case X86::VSUBSSZrr_Int
:
5010 case X86::VADDSSZrr_Intk
: case X86::VADDSSZrr_Intkz
:
5011 case X86::VCMPSSZrr_Intk
:
5012 case X86::VDIVSSZrr_Intk
: case X86::VDIVSSZrr_Intkz
:
5013 case X86::VMAXSSZrr_Intk
: case X86::VMAXSSZrr_Intkz
:
5014 case X86::VMINSSZrr_Intk
: case X86::VMINSSZrr_Intkz
:
5015 case X86::VMULSSZrr_Intk
: case X86::VMULSSZrr_Intkz
:
5016 case X86::VSUBSSZrr_Intk
: case X86::VSUBSSZrr_Intkz
:
5017 case X86::VFMADDSS4rr_Int
: case X86::VFNMADDSS4rr_Int
:
5018 case X86::VFMSUBSS4rr_Int
: case X86::VFNMSUBSS4rr_Int
:
5019 case X86::VFMADD132SSr_Int
: case X86::VFNMADD132SSr_Int
:
5020 case X86::VFMADD213SSr_Int
: case X86::VFNMADD213SSr_Int
:
5021 case X86::VFMADD231SSr_Int
: case X86::VFNMADD231SSr_Int
:
5022 case X86::VFMSUB132SSr_Int
: case X86::VFNMSUB132SSr_Int
:
5023 case X86::VFMSUB213SSr_Int
: case X86::VFNMSUB213SSr_Int
:
5024 case X86::VFMSUB231SSr_Int
: case X86::VFNMSUB231SSr_Int
:
5025 case X86::VFMADD132SSZr_Int
: case X86::VFNMADD132SSZr_Int
:
5026 case X86::VFMADD213SSZr_Int
: case X86::VFNMADD213SSZr_Int
:
5027 case X86::VFMADD231SSZr_Int
: case X86::VFNMADD231SSZr_Int
:
5028 case X86::VFMSUB132SSZr_Int
: case X86::VFNMSUB132SSZr_Int
:
5029 case X86::VFMSUB213SSZr_Int
: case X86::VFNMSUB213SSZr_Int
:
5030 case X86::VFMSUB231SSZr_Int
: case X86::VFNMSUB231SSZr_Int
:
5031 case X86::VFMADD132SSZr_Intk
: case X86::VFNMADD132SSZr_Intk
:
5032 case X86::VFMADD213SSZr_Intk
: case X86::VFNMADD213SSZr_Intk
:
5033 case X86::VFMADD231SSZr_Intk
: case X86::VFNMADD231SSZr_Intk
:
5034 case X86::VFMSUB132SSZr_Intk
: case X86::VFNMSUB132SSZr_Intk
:
5035 case X86::VFMSUB213SSZr_Intk
: case X86::VFNMSUB213SSZr_Intk
:
5036 case X86::VFMSUB231SSZr_Intk
: case X86::VFNMSUB231SSZr_Intk
:
5037 case X86::VFMADD132SSZr_Intkz
: case X86::VFNMADD132SSZr_Intkz
:
5038 case X86::VFMADD213SSZr_Intkz
: case X86::VFNMADD213SSZr_Intkz
:
5039 case X86::VFMADD231SSZr_Intkz
: case X86::VFNMADD231SSZr_Intkz
:
5040 case X86::VFMSUB132SSZr_Intkz
: case X86::VFNMSUB132SSZr_Intkz
:
5041 case X86::VFMSUB213SSZr_Intkz
: case X86::VFNMSUB213SSZr_Intkz
:
5042 case X86::VFMSUB231SSZr_Intkz
: case X86::VFNMSUB231SSZr_Intkz
:
5049 if ((Opc
== X86::MOVSDrm
|| Opc
== X86::VMOVSDrm
|| Opc
== X86::VMOVSDZrm
||
5050 Opc
== X86::MOVSDrm_alt
|| Opc
== X86::VMOVSDrm_alt
||
5051 Opc
== X86::VMOVSDZrm_alt
) &&
5053 // These instructions only load 64 bits, we can't fold them if the
5054 // destination register is wider than 64 bits (8 bytes), and its user
5055 // instruction isn't scalar (SD).
5057 case X86::ADDSDrr_Int
: case X86::VADDSDrr_Int
: case X86::VADDSDZrr_Int
:
5058 case X86::CMPSDrr_Int
: case X86::VCMPSDrr_Int
: case X86::VCMPSDZrr_Int
:
5059 case X86::DIVSDrr_Int
: case X86::VDIVSDrr_Int
: case X86::VDIVSDZrr_Int
:
5060 case X86::MAXSDrr_Int
: case X86::VMAXSDrr_Int
: case X86::VMAXSDZrr_Int
:
5061 case X86::MINSDrr_Int
: case X86::VMINSDrr_Int
: case X86::VMINSDZrr_Int
:
5062 case X86::MULSDrr_Int
: case X86::VMULSDrr_Int
: case X86::VMULSDZrr_Int
:
5063 case X86::SUBSDrr_Int
: case X86::VSUBSDrr_Int
: case X86::VSUBSDZrr_Int
:
5064 case X86::VADDSDZrr_Intk
: case X86::VADDSDZrr_Intkz
:
5065 case X86::VCMPSDZrr_Intk
:
5066 case X86::VDIVSDZrr_Intk
: case X86::VDIVSDZrr_Intkz
:
5067 case X86::VMAXSDZrr_Intk
: case X86::VMAXSDZrr_Intkz
:
5068 case X86::VMINSDZrr_Intk
: case X86::VMINSDZrr_Intkz
:
5069 case X86::VMULSDZrr_Intk
: case X86::VMULSDZrr_Intkz
:
5070 case X86::VSUBSDZrr_Intk
: case X86::VSUBSDZrr_Intkz
:
5071 case X86::VFMADDSD4rr_Int
: case X86::VFNMADDSD4rr_Int
:
5072 case X86::VFMSUBSD4rr_Int
: case X86::VFNMSUBSD4rr_Int
:
5073 case X86::VFMADD132SDr_Int
: case X86::VFNMADD132SDr_Int
:
5074 case X86::VFMADD213SDr_Int
: case X86::VFNMADD213SDr_Int
:
5075 case X86::VFMADD231SDr_Int
: case X86::VFNMADD231SDr_Int
:
5076 case X86::VFMSUB132SDr_Int
: case X86::VFNMSUB132SDr_Int
:
5077 case X86::VFMSUB213SDr_Int
: case X86::VFNMSUB213SDr_Int
:
5078 case X86::VFMSUB231SDr_Int
: case X86::VFNMSUB231SDr_Int
:
5079 case X86::VFMADD132SDZr_Int
: case X86::VFNMADD132SDZr_Int
:
5080 case X86::VFMADD213SDZr_Int
: case X86::VFNMADD213SDZr_Int
:
5081 case X86::VFMADD231SDZr_Int
: case X86::VFNMADD231SDZr_Int
:
5082 case X86::VFMSUB132SDZr_Int
: case X86::VFNMSUB132SDZr_Int
:
5083 case X86::VFMSUB213SDZr_Int
: case X86::VFNMSUB213SDZr_Int
:
5084 case X86::VFMSUB231SDZr_Int
: case X86::VFNMSUB231SDZr_Int
:
5085 case X86::VFMADD132SDZr_Intk
: case X86::VFNMADD132SDZr_Intk
:
5086 case X86::VFMADD213SDZr_Intk
: case X86::VFNMADD213SDZr_Intk
:
5087 case X86::VFMADD231SDZr_Intk
: case X86::VFNMADD231SDZr_Intk
:
5088 case X86::VFMSUB132SDZr_Intk
: case X86::VFNMSUB132SDZr_Intk
:
5089 case X86::VFMSUB213SDZr_Intk
: case X86::VFNMSUB213SDZr_Intk
:
5090 case X86::VFMSUB231SDZr_Intk
: case X86::VFNMSUB231SDZr_Intk
:
5091 case X86::VFMADD132SDZr_Intkz
: case X86::VFNMADD132SDZr_Intkz
:
5092 case X86::VFMADD213SDZr_Intkz
: case X86::VFNMADD213SDZr_Intkz
:
5093 case X86::VFMADD231SDZr_Intkz
: case X86::VFNMADD231SDZr_Intkz
:
5094 case X86::VFMSUB132SDZr_Intkz
: case X86::VFNMSUB132SDZr_Intkz
:
5095 case X86::VFMSUB213SDZr_Intkz
: case X86::VFNMSUB213SDZr_Intkz
:
5096 case X86::VFMSUB231SDZr_Intkz
: case X86::VFNMSUB231SDZr_Intkz
:
5106 MachineInstr
*X86InstrInfo::foldMemoryOperandImpl(
5107 MachineFunction
&MF
, MachineInstr
&MI
, ArrayRef
<unsigned> Ops
,
5108 MachineBasicBlock::iterator InsertPt
, MachineInstr
&LoadMI
,
5109 LiveIntervals
*LIS
) const {
5111 // TODO: Support the case where LoadMI loads a wide register, but MI
5112 // only uses a subreg.
5113 for (auto Op
: Ops
) {
5114 if (MI
.getOperand(Op
).getSubReg())
5118 // If loading from a FrameIndex, fold directly from the FrameIndex.
5119 unsigned NumOps
= LoadMI
.getDesc().getNumOperands();
5121 if (isLoadFromStackSlot(LoadMI
, FrameIndex
)) {
5122 if (isNonFoldablePartialRegisterLoad(LoadMI
, MI
, MF
))
5124 return foldMemoryOperandImpl(MF
, MI
, Ops
, InsertPt
, FrameIndex
, LIS
);
5127 // Check switch flag
5128 if (NoFusing
) return nullptr;
5130 // Avoid partial and undef register update stalls unless optimizing for size.
5131 if (!MF
.getFunction().hasOptSize() &&
5132 (hasPartialRegUpdate(MI
.getOpcode(), Subtarget
, /*ForLoadFold*/true) ||
5133 shouldPreventUndefRegUpdateMemFold(MF
, MI
)))
5136 // Determine the alignment of the load.
5137 unsigned Alignment
= 0;
5138 if (LoadMI
.hasOneMemOperand())
5139 Alignment
= (*LoadMI
.memoperands_begin())->getAlignment();
5141 switch (LoadMI
.getOpcode()) {
5142 case X86::AVX512_512_SET0
:
5143 case X86::AVX512_512_SETALLONES
:
5146 case X86::AVX2_SETALLONES
:
5147 case X86::AVX1_SETALLONES
:
5149 case X86::AVX512_256_SET0
:
5153 case X86::V_SETALLONES
:
5154 case X86::AVX512_128_SET0
:
5159 case X86::AVX512_FsFLD0SD
:
5163 case X86::AVX512_FsFLD0SS
:
5169 if (Ops
.size() == 2 && Ops
[0] == 0 && Ops
[1] == 1) {
5170 unsigned NewOpc
= 0;
5171 switch (MI
.getOpcode()) {
5172 default: return nullptr;
5173 case X86::TEST8rr
: NewOpc
= X86::CMP8ri
; break;
5174 case X86::TEST16rr
: NewOpc
= X86::CMP16ri8
; break;
5175 case X86::TEST32rr
: NewOpc
= X86::CMP32ri8
; break;
5176 case X86::TEST64rr
: NewOpc
= X86::CMP64ri8
; break;
5178 // Change to CMPXXri r, 0 first.
5179 MI
.setDesc(get(NewOpc
));
5180 MI
.getOperand(1).ChangeToImmediate(0);
5181 } else if (Ops
.size() != 1)
5184 // Make sure the subregisters match.
5185 // Otherwise we risk changing the size of the load.
5186 if (LoadMI
.getOperand(0).getSubReg() != MI
.getOperand(Ops
[0]).getSubReg())
5189 SmallVector
<MachineOperand
,X86::AddrNumOperands
> MOs
;
5190 switch (LoadMI
.getOpcode()) {
5193 case X86::V_SETALLONES
:
5194 case X86::AVX2_SETALLONES
:
5195 case X86::AVX1_SETALLONES
:
5197 case X86::AVX512_128_SET0
:
5198 case X86::AVX512_256_SET0
:
5199 case X86::AVX512_512_SET0
:
5200 case X86::AVX512_512_SETALLONES
:
5202 case X86::AVX512_FsFLD0SD
:
5204 case X86::AVX512_FsFLD0SS
: {
5205 // Folding a V_SET0 or V_SETALLONES as a load, to ease register pressure.
5206 // Create a constant-pool entry and operands to load from it.
5208 // Medium and large mode can't fold loads this way.
5209 if (MF
.getTarget().getCodeModel() != CodeModel::Small
&&
5210 MF
.getTarget().getCodeModel() != CodeModel::Kernel
)
5213 // x86-32 PIC requires a PIC base register for constant pools.
5214 unsigned PICBase
= 0;
5215 if (MF
.getTarget().isPositionIndependent()) {
5216 if (Subtarget
.is64Bit())
5219 // FIXME: PICBase = getGlobalBaseReg(&MF);
5220 // This doesn't work for several reasons.
5221 // 1. GlobalBaseReg may have been spilled.
5222 // 2. It may not be live at MI.
5226 // Create a constant-pool entry.
5227 MachineConstantPool
&MCP
= *MF
.getConstantPool();
5229 unsigned Opc
= LoadMI
.getOpcode();
5230 if (Opc
== X86::FsFLD0SS
|| Opc
== X86::AVX512_FsFLD0SS
)
5231 Ty
= Type::getFloatTy(MF
.getFunction().getContext());
5232 else if (Opc
== X86::FsFLD0SD
|| Opc
== X86::AVX512_FsFLD0SD
)
5233 Ty
= Type::getDoubleTy(MF
.getFunction().getContext());
5234 else if (Opc
== X86::AVX512_512_SET0
|| Opc
== X86::AVX512_512_SETALLONES
)
5235 Ty
= VectorType::get(Type::getInt32Ty(MF
.getFunction().getContext()),16);
5236 else if (Opc
== X86::AVX2_SETALLONES
|| Opc
== X86::AVX_SET0
||
5237 Opc
== X86::AVX512_256_SET0
|| Opc
== X86::AVX1_SETALLONES
)
5238 Ty
= VectorType::get(Type::getInt32Ty(MF
.getFunction().getContext()), 8);
5239 else if (Opc
== X86::MMX_SET0
)
5240 Ty
= VectorType::get(Type::getInt32Ty(MF
.getFunction().getContext()), 2);
5242 Ty
= VectorType::get(Type::getInt32Ty(MF
.getFunction().getContext()), 4);
5244 bool IsAllOnes
= (Opc
== X86::V_SETALLONES
|| Opc
== X86::AVX2_SETALLONES
||
5245 Opc
== X86::AVX512_512_SETALLONES
||
5246 Opc
== X86::AVX1_SETALLONES
);
5247 const Constant
*C
= IsAllOnes
? Constant::getAllOnesValue(Ty
) :
5248 Constant::getNullValue(Ty
);
5249 unsigned CPI
= MCP
.getConstantPoolIndex(C
, Alignment
);
5251 // Create operands to load from the constant pool entry.
5252 MOs
.push_back(MachineOperand::CreateReg(PICBase
, false));
5253 MOs
.push_back(MachineOperand::CreateImm(1));
5254 MOs
.push_back(MachineOperand::CreateReg(0, false));
5255 MOs
.push_back(MachineOperand::CreateCPI(CPI
, 0));
5256 MOs
.push_back(MachineOperand::CreateReg(0, false));
5260 if (isNonFoldablePartialRegisterLoad(LoadMI
, MI
, MF
))
5263 // Folding a normal load. Just copy the load's address operands.
5264 MOs
.append(LoadMI
.operands_begin() + NumOps
- X86::AddrNumOperands
,
5265 LoadMI
.operands_begin() + NumOps
);
5269 return foldMemoryOperandImpl(MF
, MI
, Ops
[0], MOs
, InsertPt
,
5270 /*Size=*/0, Alignment
, /*AllowCommute=*/true);
5273 static SmallVector
<MachineMemOperand
*, 2>
5274 extractLoadMMOs(ArrayRef
<MachineMemOperand
*> MMOs
, MachineFunction
&MF
) {
5275 SmallVector
<MachineMemOperand
*, 2> LoadMMOs
;
5277 for (MachineMemOperand
*MMO
: MMOs
) {
5281 if (!MMO
->isStore()) {
5283 LoadMMOs
.push_back(MMO
);
5285 // Clone the MMO and unset the store flag.
5286 LoadMMOs
.push_back(MF
.getMachineMemOperand(
5287 MMO
, MMO
->getFlags() & ~MachineMemOperand::MOStore
));
5294 static SmallVector
<MachineMemOperand
*, 2>
5295 extractStoreMMOs(ArrayRef
<MachineMemOperand
*> MMOs
, MachineFunction
&MF
) {
5296 SmallVector
<MachineMemOperand
*, 2> StoreMMOs
;
5298 for (MachineMemOperand
*MMO
: MMOs
) {
5299 if (!MMO
->isStore())
5302 if (!MMO
->isLoad()) {
5304 StoreMMOs
.push_back(MMO
);
5306 // Clone the MMO and unset the load flag.
5307 StoreMMOs
.push_back(MF
.getMachineMemOperand(
5308 MMO
, MMO
->getFlags() & ~MachineMemOperand::MOLoad
));
5315 static unsigned getBroadcastOpcode(const X86MemoryFoldTableEntry
*I
,
5316 const TargetRegisterClass
*RC
,
5317 const X86Subtarget
&STI
) {
5318 assert(STI
.hasAVX512() && "Expected at least AVX512!");
5319 unsigned SpillSize
= STI
.getRegisterInfo()->getSpillSize(*RC
);
5320 assert((SpillSize
== 64 || STI
.hasVLX()) &&
5321 "Can't broadcast less than 64 bytes without AVX512VL!");
5323 switch (I
->Flags
& TB_BCAST_MASK
) {
5324 default: llvm_unreachable("Unexpected broadcast type!");
5326 switch (SpillSize
) {
5327 default: llvm_unreachable("Unknown spill size");
5328 case 16: return X86::VPBROADCASTDZ128m
;
5329 case 32: return X86::VPBROADCASTDZ256m
;
5330 case 64: return X86::VPBROADCASTDZm
;
5334 switch (SpillSize
) {
5335 default: llvm_unreachable("Unknown spill size");
5336 case 16: return X86::VPBROADCASTQZ128m
;
5337 case 32: return X86::VPBROADCASTQZ256m
;
5338 case 64: return X86::VPBROADCASTQZm
;
5342 switch (SpillSize
) {
5343 default: llvm_unreachable("Unknown spill size");
5344 case 16: return X86::VBROADCASTSSZ128m
;
5345 case 32: return X86::VBROADCASTSSZ256m
;
5346 case 64: return X86::VBROADCASTSSZm
;
5350 switch (SpillSize
) {
5351 default: llvm_unreachable("Unknown spill size");
5352 case 16: return X86::VMOVDDUPZ128rm
;
5353 case 32: return X86::VBROADCASTSDZ256m
;
5354 case 64: return X86::VBROADCASTSDZm
;
5360 bool X86InstrInfo::unfoldMemoryOperand(
5361 MachineFunction
&MF
, MachineInstr
&MI
, unsigned Reg
, bool UnfoldLoad
,
5362 bool UnfoldStore
, SmallVectorImpl
<MachineInstr
*> &NewMIs
) const {
5363 const X86MemoryFoldTableEntry
*I
= lookupUnfoldTable(MI
.getOpcode());
5366 unsigned Opc
= I
->DstOp
;
5367 unsigned Index
= I
->Flags
& TB_INDEX_MASK
;
5368 bool FoldedLoad
= I
->Flags
& TB_FOLDED_LOAD
;
5369 bool FoldedStore
= I
->Flags
& TB_FOLDED_STORE
;
5370 bool FoldedBCast
= I
->Flags
& TB_FOLDED_BCAST
;
5371 if (UnfoldLoad
&& !FoldedLoad
)
5373 UnfoldLoad
&= FoldedLoad
;
5374 if (UnfoldStore
&& !FoldedStore
)
5376 UnfoldStore
&= FoldedStore
;
5378 const MCInstrDesc
&MCID
= get(Opc
);
5380 const TargetRegisterClass
*RC
= getRegClass(MCID
, Index
, &RI
, MF
);
5381 const TargetRegisterInfo
&TRI
= *MF
.getSubtarget().getRegisterInfo();
5382 // TODO: Check if 32-byte or greater accesses are slow too?
5383 if (!MI
.hasOneMemOperand() && RC
== &X86::VR128RegClass
&&
5384 Subtarget
.isUnalignedMem16Slow())
5385 // Without memoperands, loadRegFromAddr and storeRegToStackSlot will
5386 // conservatively assume the address is unaligned. That's bad for
5389 SmallVector
<MachineOperand
, X86::AddrNumOperands
> AddrOps
;
5390 SmallVector
<MachineOperand
,2> BeforeOps
;
5391 SmallVector
<MachineOperand
,2> AfterOps
;
5392 SmallVector
<MachineOperand
,4> ImpOps
;
5393 for (unsigned i
= 0, e
= MI
.getNumOperands(); i
!= e
; ++i
) {
5394 MachineOperand
&Op
= MI
.getOperand(i
);
5395 if (i
>= Index
&& i
< Index
+ X86::AddrNumOperands
)
5396 AddrOps
.push_back(Op
);
5397 else if (Op
.isReg() && Op
.isImplicit())
5398 ImpOps
.push_back(Op
);
5400 BeforeOps
.push_back(Op
);
5402 AfterOps
.push_back(Op
);
5405 // Emit the load or broadcast instruction.
5407 auto MMOs
= extractLoadMMOs(MI
.memoperands(), MF
);
5411 Opc
= getBroadcastOpcode(I
, RC
, Subtarget
);
5413 unsigned Alignment
= std::max
<uint32_t>(TRI
.getSpillSize(*RC
), 16);
5414 bool isAligned
= !MMOs
.empty() && MMOs
.front()->getAlignment() >= Alignment
;
5415 Opc
= getLoadRegOpcode(Reg
, RC
, isAligned
, Subtarget
);
5419 MachineInstrBuilder MIB
= BuildMI(MF
, DL
, get(Opc
), Reg
);
5420 for (unsigned i
= 0, e
= AddrOps
.size(); i
!= e
; ++i
)
5421 MIB
.add(AddrOps
[i
]);
5422 MIB
.setMemRefs(MMOs
);
5423 NewMIs
.push_back(MIB
);
5426 // Address operands cannot be marked isKill.
5427 for (unsigned i
= 1; i
!= 1 + X86::AddrNumOperands
; ++i
) {
5428 MachineOperand
&MO
= NewMIs
[0]->getOperand(i
);
5430 MO
.setIsKill(false);
5435 // Emit the data processing instruction.
5436 MachineInstr
*DataMI
= MF
.CreateMachineInstr(MCID
, MI
.getDebugLoc(), true);
5437 MachineInstrBuilder
MIB(MF
, DataMI
);
5440 MIB
.addReg(Reg
, RegState::Define
);
5441 for (MachineOperand
&BeforeOp
: BeforeOps
)
5445 for (MachineOperand
&AfterOp
: AfterOps
)
5447 for (MachineOperand
&ImpOp
: ImpOps
) {
5448 MIB
.addReg(ImpOp
.getReg(),
5449 getDefRegState(ImpOp
.isDef()) |
5450 RegState::Implicit
|
5451 getKillRegState(ImpOp
.isKill()) |
5452 getDeadRegState(ImpOp
.isDead()) |
5453 getUndefRegState(ImpOp
.isUndef()));
5455 // Change CMP32ri r, 0 back to TEST32rr r, r, etc.
5456 switch (DataMI
->getOpcode()) {
5458 case X86::CMP64ri32
:
5465 MachineOperand
&MO0
= DataMI
->getOperand(0);
5466 MachineOperand
&MO1
= DataMI
->getOperand(1);
5467 if (MO1
.getImm() == 0) {
5469 switch (DataMI
->getOpcode()) {
5470 default: llvm_unreachable("Unreachable!");
5472 case X86::CMP64ri32
: NewOpc
= X86::TEST64rr
; break;
5474 case X86::CMP32ri
: NewOpc
= X86::TEST32rr
; break;
5476 case X86::CMP16ri
: NewOpc
= X86::TEST16rr
; break;
5477 case X86::CMP8ri
: NewOpc
= X86::TEST8rr
; break;
5479 DataMI
->setDesc(get(NewOpc
));
5480 MO1
.ChangeToRegister(MO0
.getReg(), false);
5484 NewMIs
.push_back(DataMI
);
5486 // Emit the store instruction.
5488 const TargetRegisterClass
*DstRC
= getRegClass(MCID
, 0, &RI
, MF
);
5489 auto MMOs
= extractStoreMMOs(MI
.memoperands(), MF
);
5490 unsigned Alignment
= std::max
<uint32_t>(TRI
.getSpillSize(*DstRC
), 16);
5491 bool isAligned
= !MMOs
.empty() && MMOs
.front()->getAlignment() >= Alignment
;
5492 unsigned Opc
= getStoreRegOpcode(Reg
, DstRC
, isAligned
, Subtarget
);
5494 MachineInstrBuilder MIB
= BuildMI(MF
, DL
, get(Opc
));
5495 for (unsigned i
= 0, e
= AddrOps
.size(); i
!= e
; ++i
)
5496 MIB
.add(AddrOps
[i
]);
5497 MIB
.addReg(Reg
, RegState::Kill
);
5498 MIB
.setMemRefs(MMOs
);
5499 NewMIs
.push_back(MIB
);
5506 X86InstrInfo::unfoldMemoryOperand(SelectionDAG
&DAG
, SDNode
*N
,
5507 SmallVectorImpl
<SDNode
*> &NewNodes
) const {
5508 if (!N
->isMachineOpcode())
5511 const X86MemoryFoldTableEntry
*I
= lookupUnfoldTable(N
->getMachineOpcode());
5514 unsigned Opc
= I
->DstOp
;
5515 unsigned Index
= I
->Flags
& TB_INDEX_MASK
;
5516 bool FoldedLoad
= I
->Flags
& TB_FOLDED_LOAD
;
5517 bool FoldedStore
= I
->Flags
& TB_FOLDED_STORE
;
5518 bool FoldedBCast
= I
->Flags
& TB_FOLDED_BCAST
;
5519 const MCInstrDesc
&MCID
= get(Opc
);
5520 MachineFunction
&MF
= DAG
.getMachineFunction();
5521 const TargetRegisterInfo
&TRI
= *MF
.getSubtarget().getRegisterInfo();
5522 const TargetRegisterClass
*RC
= getRegClass(MCID
, Index
, &RI
, MF
);
5523 unsigned NumDefs
= MCID
.NumDefs
;
5524 std::vector
<SDValue
> AddrOps
;
5525 std::vector
<SDValue
> BeforeOps
;
5526 std::vector
<SDValue
> AfterOps
;
5528 unsigned NumOps
= N
->getNumOperands();
5529 for (unsigned i
= 0; i
!= NumOps
-1; ++i
) {
5530 SDValue Op
= N
->getOperand(i
);
5531 if (i
>= Index
-NumDefs
&& i
< Index
-NumDefs
+ X86::AddrNumOperands
)
5532 AddrOps
.push_back(Op
);
5533 else if (i
< Index
-NumDefs
)
5534 BeforeOps
.push_back(Op
);
5535 else if (i
> Index
-NumDefs
)
5536 AfterOps
.push_back(Op
);
5538 SDValue Chain
= N
->getOperand(NumOps
-1);
5539 AddrOps
.push_back(Chain
);
5541 // Emit the load instruction.
5542 SDNode
*Load
= nullptr;
5544 EVT VT
= *TRI
.legalclasstypes_begin(*RC
);
5545 auto MMOs
= extractLoadMMOs(cast
<MachineSDNode
>(N
)->memoperands(), MF
);
5546 if (MMOs
.empty() && RC
== &X86::VR128RegClass
&&
5547 Subtarget
.isUnalignedMem16Slow())
5548 // Do not introduce a slow unaligned load.
5550 // FIXME: If a VR128 can have size 32, we should be checking if a 32-byte
5551 // memory access is slow above.
5555 Opc
= getBroadcastOpcode(I
, RC
, Subtarget
);
5557 unsigned Alignment
= std::max
<uint32_t>(TRI
.getSpillSize(*RC
), 16);
5558 bool isAligned
= !MMOs
.empty() && MMOs
.front()->getAlignment() >= Alignment
;
5559 Opc
= getLoadRegOpcode(0, RC
, isAligned
, Subtarget
);
5562 Load
= DAG
.getMachineNode(Opc
, dl
, VT
, MVT::Other
, AddrOps
);
5563 NewNodes
.push_back(Load
);
5565 // Preserve memory reference information.
5566 DAG
.setNodeMemRefs(cast
<MachineSDNode
>(Load
), MMOs
);
5569 // Emit the data processing instruction.
5570 std::vector
<EVT
> VTs
;
5571 const TargetRegisterClass
*DstRC
= nullptr;
5572 if (MCID
.getNumDefs() > 0) {
5573 DstRC
= getRegClass(MCID
, 0, &RI
, MF
);
5574 VTs
.push_back(*TRI
.legalclasstypes_begin(*DstRC
));
5576 for (unsigned i
= 0, e
= N
->getNumValues(); i
!= e
; ++i
) {
5577 EVT VT
= N
->getValueType(i
);
5578 if (VT
!= MVT::Other
&& i
>= (unsigned)MCID
.getNumDefs())
5582 BeforeOps
.push_back(SDValue(Load
, 0));
5583 BeforeOps
.insert(BeforeOps
.end(), AfterOps
.begin(), AfterOps
.end());
5584 // Change CMP32ri r, 0 back to TEST32rr r, r, etc.
5587 case X86::CMP64ri32
:
5594 if (isNullConstant(BeforeOps
[1])) {
5596 default: llvm_unreachable("Unreachable!");
5598 case X86::CMP64ri32
: Opc
= X86::TEST64rr
; break;
5600 case X86::CMP32ri
: Opc
= X86::TEST32rr
; break;
5602 case X86::CMP16ri
: Opc
= X86::TEST16rr
; break;
5603 case X86::CMP8ri
: Opc
= X86::TEST8rr
; break;
5605 BeforeOps
[1] = BeforeOps
[0];
5608 SDNode
*NewNode
= DAG
.getMachineNode(Opc
, dl
, VTs
, BeforeOps
);
5609 NewNodes
.push_back(NewNode
);
5611 // Emit the store instruction.
5614 AddrOps
.push_back(SDValue(NewNode
, 0));
5615 AddrOps
.push_back(Chain
);
5616 auto MMOs
= extractStoreMMOs(cast
<MachineSDNode
>(N
)->memoperands(), MF
);
5617 if (MMOs
.empty() && RC
== &X86::VR128RegClass
&&
5618 Subtarget
.isUnalignedMem16Slow())
5619 // Do not introduce a slow unaligned store.
5621 // FIXME: If a VR128 can have size 32, we should be checking if a 32-byte
5622 // memory access is slow above.
5623 unsigned Alignment
= std::max
<uint32_t>(TRI
.getSpillSize(*RC
), 16);
5624 bool isAligned
= !MMOs
.empty() && MMOs
.front()->getAlignment() >= Alignment
;
5626 DAG
.getMachineNode(getStoreRegOpcode(0, DstRC
, isAligned
, Subtarget
),
5627 dl
, MVT::Other
, AddrOps
);
5628 NewNodes
.push_back(Store
);
5630 // Preserve memory reference information.
5631 DAG
.setNodeMemRefs(cast
<MachineSDNode
>(Store
), MMOs
);
5637 unsigned X86InstrInfo::getOpcodeAfterMemoryUnfold(unsigned Opc
,
5638 bool UnfoldLoad
, bool UnfoldStore
,
5639 unsigned *LoadRegIndex
) const {
5640 const X86MemoryFoldTableEntry
*I
= lookupUnfoldTable(Opc
);
5643 bool FoldedLoad
= I
->Flags
& TB_FOLDED_LOAD
;
5644 bool FoldedStore
= I
->Flags
& TB_FOLDED_STORE
;
5645 if (UnfoldLoad
&& !FoldedLoad
)
5647 if (UnfoldStore
&& !FoldedStore
)
5650 *LoadRegIndex
= I
->Flags
& TB_INDEX_MASK
;
5655 X86InstrInfo::areLoadsFromSameBasePtr(SDNode
*Load1
, SDNode
*Load2
,
5656 int64_t &Offset1
, int64_t &Offset2
) const {
5657 if (!Load1
->isMachineOpcode() || !Load2
->isMachineOpcode())
5659 unsigned Opc1
= Load1
->getMachineOpcode();
5660 unsigned Opc2
= Load2
->getMachineOpcode();
5662 default: return false;
5671 case X86::MOVSSrm_alt
:
5673 case X86::MOVSDrm_alt
:
5674 case X86::MMX_MOVD64rm
:
5675 case X86::MMX_MOVQ64rm
:
5682 // AVX load instructions
5684 case X86::VMOVSSrm_alt
:
5686 case X86::VMOVSDrm_alt
:
5687 case X86::VMOVAPSrm
:
5688 case X86::VMOVUPSrm
:
5689 case X86::VMOVAPDrm
:
5690 case X86::VMOVUPDrm
:
5691 case X86::VMOVDQArm
:
5692 case X86::VMOVDQUrm
:
5693 case X86::VMOVAPSYrm
:
5694 case X86::VMOVUPSYrm
:
5695 case X86::VMOVAPDYrm
:
5696 case X86::VMOVUPDYrm
:
5697 case X86::VMOVDQAYrm
:
5698 case X86::VMOVDQUYrm
:
5699 // AVX512 load instructions
5700 case X86::VMOVSSZrm
:
5701 case X86::VMOVSSZrm_alt
:
5702 case X86::VMOVSDZrm
:
5703 case X86::VMOVSDZrm_alt
:
5704 case X86::VMOVAPSZ128rm
:
5705 case X86::VMOVUPSZ128rm
:
5706 case X86::VMOVAPSZ128rm_NOVLX
:
5707 case X86::VMOVUPSZ128rm_NOVLX
:
5708 case X86::VMOVAPDZ128rm
:
5709 case X86::VMOVUPDZ128rm
:
5710 case X86::VMOVDQU8Z128rm
:
5711 case X86::VMOVDQU16Z128rm
:
5712 case X86::VMOVDQA32Z128rm
:
5713 case X86::VMOVDQU32Z128rm
:
5714 case X86::VMOVDQA64Z128rm
:
5715 case X86::VMOVDQU64Z128rm
:
5716 case X86::VMOVAPSZ256rm
:
5717 case X86::VMOVUPSZ256rm
:
5718 case X86::VMOVAPSZ256rm_NOVLX
:
5719 case X86::VMOVUPSZ256rm_NOVLX
:
5720 case X86::VMOVAPDZ256rm
:
5721 case X86::VMOVUPDZ256rm
:
5722 case X86::VMOVDQU8Z256rm
:
5723 case X86::VMOVDQU16Z256rm
:
5724 case X86::VMOVDQA32Z256rm
:
5725 case X86::VMOVDQU32Z256rm
:
5726 case X86::VMOVDQA64Z256rm
:
5727 case X86::VMOVDQU64Z256rm
:
5728 case X86::VMOVAPSZrm
:
5729 case X86::VMOVUPSZrm
:
5730 case X86::VMOVAPDZrm
:
5731 case X86::VMOVUPDZrm
:
5732 case X86::VMOVDQU8Zrm
:
5733 case X86::VMOVDQU16Zrm
:
5734 case X86::VMOVDQA32Zrm
:
5735 case X86::VMOVDQU32Zrm
:
5736 case X86::VMOVDQA64Zrm
:
5737 case X86::VMOVDQU64Zrm
:
5745 default: return false;
5754 case X86::MOVSSrm_alt
:
5756 case X86::MOVSDrm_alt
:
5757 case X86::MMX_MOVD64rm
:
5758 case X86::MMX_MOVQ64rm
:
5765 // AVX load instructions
5767 case X86::VMOVSSrm_alt
:
5769 case X86::VMOVSDrm_alt
:
5770 case X86::VMOVAPSrm
:
5771 case X86::VMOVUPSrm
:
5772 case X86::VMOVAPDrm
:
5773 case X86::VMOVUPDrm
:
5774 case X86::VMOVDQArm
:
5775 case X86::VMOVDQUrm
:
5776 case X86::VMOVAPSYrm
:
5777 case X86::VMOVUPSYrm
:
5778 case X86::VMOVAPDYrm
:
5779 case X86::VMOVUPDYrm
:
5780 case X86::VMOVDQAYrm
:
5781 case X86::VMOVDQUYrm
:
5782 // AVX512 load instructions
5783 case X86::VMOVSSZrm
:
5784 case X86::VMOVSSZrm_alt
:
5785 case X86::VMOVSDZrm
:
5786 case X86::VMOVSDZrm_alt
:
5787 case X86::VMOVAPSZ128rm
:
5788 case X86::VMOVUPSZ128rm
:
5789 case X86::VMOVAPSZ128rm_NOVLX
:
5790 case X86::VMOVUPSZ128rm_NOVLX
:
5791 case X86::VMOVAPDZ128rm
:
5792 case X86::VMOVUPDZ128rm
:
5793 case X86::VMOVDQU8Z128rm
:
5794 case X86::VMOVDQU16Z128rm
:
5795 case X86::VMOVDQA32Z128rm
:
5796 case X86::VMOVDQU32Z128rm
:
5797 case X86::VMOVDQA64Z128rm
:
5798 case X86::VMOVDQU64Z128rm
:
5799 case X86::VMOVAPSZ256rm
:
5800 case X86::VMOVUPSZ256rm
:
5801 case X86::VMOVAPSZ256rm_NOVLX
:
5802 case X86::VMOVUPSZ256rm_NOVLX
:
5803 case X86::VMOVAPDZ256rm
:
5804 case X86::VMOVUPDZ256rm
:
5805 case X86::VMOVDQU8Z256rm
:
5806 case X86::VMOVDQU16Z256rm
:
5807 case X86::VMOVDQA32Z256rm
:
5808 case X86::VMOVDQU32Z256rm
:
5809 case X86::VMOVDQA64Z256rm
:
5810 case X86::VMOVDQU64Z256rm
:
5811 case X86::VMOVAPSZrm
:
5812 case X86::VMOVUPSZrm
:
5813 case X86::VMOVAPDZrm
:
5814 case X86::VMOVUPDZrm
:
5815 case X86::VMOVDQU8Zrm
:
5816 case X86::VMOVDQU16Zrm
:
5817 case X86::VMOVDQA32Zrm
:
5818 case X86::VMOVDQU32Zrm
:
5819 case X86::VMOVDQA64Zrm
:
5820 case X86::VMOVDQU64Zrm
:
5828 // Lambda to check if both the loads have the same value for an operand index.
5829 auto HasSameOp
= [&](int I
) {
5830 return Load1
->getOperand(I
) == Load2
->getOperand(I
);
5833 // All operands except the displacement should match.
5834 if (!HasSameOp(X86::AddrBaseReg
) || !HasSameOp(X86::AddrScaleAmt
) ||
5835 !HasSameOp(X86::AddrIndexReg
) || !HasSameOp(X86::AddrSegmentReg
))
5838 // Chain Operand must be the same.
5842 // Now let's examine if the displacements are constants.
5843 auto Disp1
= dyn_cast
<ConstantSDNode
>(Load1
->getOperand(X86::AddrDisp
));
5844 auto Disp2
= dyn_cast
<ConstantSDNode
>(Load2
->getOperand(X86::AddrDisp
));
5845 if (!Disp1
|| !Disp2
)
5848 Offset1
= Disp1
->getSExtValue();
5849 Offset2
= Disp2
->getSExtValue();
5853 bool X86InstrInfo::shouldScheduleLoadsNear(SDNode
*Load1
, SDNode
*Load2
,
5854 int64_t Offset1
, int64_t Offset2
,
5855 unsigned NumLoads
) const {
5856 assert(Offset2
> Offset1
);
5857 if ((Offset2
- Offset1
) / 8 > 64)
5860 unsigned Opc1
= Load1
->getMachineOpcode();
5861 unsigned Opc2
= Load2
->getMachineOpcode();
5863 return false; // FIXME: overly conservative?
5870 case X86::MMX_MOVD64rm
:
5871 case X86::MMX_MOVQ64rm
:
5875 EVT VT
= Load1
->getValueType(0);
5876 switch (VT
.getSimpleVT().SimpleTy
) {
5878 // XMM registers. In 64-bit mode we can be a bit more aggressive since we
5879 // have 16 of them to play with.
5880 if (Subtarget
.is64Bit()) {
5883 } else if (NumLoads
) {
5902 reverseBranchCondition(SmallVectorImpl
<MachineOperand
> &Cond
) const {
5903 assert(Cond
.size() == 1 && "Invalid X86 branch condition!");
5904 X86::CondCode CC
= static_cast<X86::CondCode
>(Cond
[0].getImm());
5905 Cond
[0].setImm(GetOppositeBranchCondition(CC
));
5910 isSafeToMoveRegClassDefs(const TargetRegisterClass
*RC
) const {
5911 // FIXME: Return false for x87 stack register classes for now. We can't
5912 // allow any loads of these registers before FpGet_ST0_80.
5913 return !(RC
== &X86::CCRRegClass
|| RC
== &X86::DFCCRRegClass
||
5914 RC
== &X86::RFP32RegClass
|| RC
== &X86::RFP64RegClass
||
5915 RC
== &X86::RFP80RegClass
);
5918 /// Return a virtual register initialized with the
5919 /// the global base register value. Output instructions required to
5920 /// initialize the register in the function entry block, if necessary.
5922 /// TODO: Eliminate this and move the code to X86MachineFunctionInfo.
5924 unsigned X86InstrInfo::getGlobalBaseReg(MachineFunction
*MF
) const {
5925 assert((!Subtarget
.is64Bit() ||
5926 MF
->getTarget().getCodeModel() == CodeModel::Medium
||
5927 MF
->getTarget().getCodeModel() == CodeModel::Large
) &&
5928 "X86-64 PIC uses RIP relative addressing");
5930 X86MachineFunctionInfo
*X86FI
= MF
->getInfo
<X86MachineFunctionInfo
>();
5931 unsigned GlobalBaseReg
= X86FI
->getGlobalBaseReg();
5932 if (GlobalBaseReg
!= 0)
5933 return GlobalBaseReg
;
5935 // Create the register. The code to initialize it is inserted
5936 // later, by the CGBR pass (below).
5937 MachineRegisterInfo
&RegInfo
= MF
->getRegInfo();
5938 GlobalBaseReg
= RegInfo
.createVirtualRegister(
5939 Subtarget
.is64Bit() ? &X86::GR64_NOSPRegClass
: &X86::GR32_NOSPRegClass
);
5940 X86FI
->setGlobalBaseReg(GlobalBaseReg
);
5941 return GlobalBaseReg
;
5944 // These are the replaceable SSE instructions. Some of these have Int variants
5945 // that we don't include here. We don't want to replace instructions selected
5947 static const uint16_t ReplaceableInstrs
[][3] = {
5948 //PackedSingle PackedDouble PackedInt
5949 { X86::MOVAPSmr
, X86::MOVAPDmr
, X86::MOVDQAmr
},
5950 { X86::MOVAPSrm
, X86::MOVAPDrm
, X86::MOVDQArm
},
5951 { X86::MOVAPSrr
, X86::MOVAPDrr
, X86::MOVDQArr
},
5952 { X86::MOVUPSmr
, X86::MOVUPDmr
, X86::MOVDQUmr
},
5953 { X86::MOVUPSrm
, X86::MOVUPDrm
, X86::MOVDQUrm
},
5954 { X86::MOVLPSmr
, X86::MOVLPDmr
, X86::MOVPQI2QImr
},
5955 { X86::MOVSDmr
, X86::MOVSDmr
, X86::MOVPQI2QImr
},
5956 { X86::MOVSSmr
, X86::MOVSSmr
, X86::MOVPDI2DImr
},
5957 { X86::MOVSDrm
, X86::MOVSDrm
, X86::MOVQI2PQIrm
},
5958 { X86::MOVSDrm_alt
,X86::MOVSDrm_alt
,X86::MOVQI2PQIrm
},
5959 { X86::MOVSSrm
, X86::MOVSSrm
, X86::MOVDI2PDIrm
},
5960 { X86::MOVSSrm_alt
,X86::MOVSSrm_alt
,X86::MOVDI2PDIrm
},
5961 { X86::MOVNTPSmr
, X86::MOVNTPDmr
, X86::MOVNTDQmr
},
5962 { X86::ANDNPSrm
, X86::ANDNPDrm
, X86::PANDNrm
},
5963 { X86::ANDNPSrr
, X86::ANDNPDrr
, X86::PANDNrr
},
5964 { X86::ANDPSrm
, X86::ANDPDrm
, X86::PANDrm
},
5965 { X86::ANDPSrr
, X86::ANDPDrr
, X86::PANDrr
},
5966 { X86::ORPSrm
, X86::ORPDrm
, X86::PORrm
},
5967 { X86::ORPSrr
, X86::ORPDrr
, X86::PORrr
},
5968 { X86::XORPSrm
, X86::XORPDrm
, X86::PXORrm
},
5969 { X86::XORPSrr
, X86::XORPDrr
, X86::PXORrr
},
5970 { X86::UNPCKLPDrm
, X86::UNPCKLPDrm
, X86::PUNPCKLQDQrm
},
5971 { X86::MOVLHPSrr
, X86::UNPCKLPDrr
, X86::PUNPCKLQDQrr
},
5972 { X86::UNPCKHPDrm
, X86::UNPCKHPDrm
, X86::PUNPCKHQDQrm
},
5973 { X86::UNPCKHPDrr
, X86::UNPCKHPDrr
, X86::PUNPCKHQDQrr
},
5974 { X86::UNPCKLPSrm
, X86::UNPCKLPSrm
, X86::PUNPCKLDQrm
},
5975 { X86::UNPCKLPSrr
, X86::UNPCKLPSrr
, X86::PUNPCKLDQrr
},
5976 { X86::UNPCKHPSrm
, X86::UNPCKHPSrm
, X86::PUNPCKHDQrm
},
5977 { X86::UNPCKHPSrr
, X86::UNPCKHPSrr
, X86::PUNPCKHDQrr
},
5978 { X86::EXTRACTPSmr
, X86::EXTRACTPSmr
, X86::PEXTRDmr
},
5979 { X86::EXTRACTPSrr
, X86::EXTRACTPSrr
, X86::PEXTRDrr
},
5980 // AVX 128-bit support
5981 { X86::VMOVAPSmr
, X86::VMOVAPDmr
, X86::VMOVDQAmr
},
5982 { X86::VMOVAPSrm
, X86::VMOVAPDrm
, X86::VMOVDQArm
},
5983 { X86::VMOVAPSrr
, X86::VMOVAPDrr
, X86::VMOVDQArr
},
5984 { X86::VMOVUPSmr
, X86::VMOVUPDmr
, X86::VMOVDQUmr
},
5985 { X86::VMOVUPSrm
, X86::VMOVUPDrm
, X86::VMOVDQUrm
},
5986 { X86::VMOVLPSmr
, X86::VMOVLPDmr
, X86::VMOVPQI2QImr
},
5987 { X86::VMOVSDmr
, X86::VMOVSDmr
, X86::VMOVPQI2QImr
},
5988 { X86::VMOVSSmr
, X86::VMOVSSmr
, X86::VMOVPDI2DImr
},
5989 { X86::VMOVSDrm
, X86::VMOVSDrm
, X86::VMOVQI2PQIrm
},
5990 { X86::VMOVSDrm_alt
,X86::VMOVSDrm_alt
,X86::VMOVQI2PQIrm
},
5991 { X86::VMOVSSrm
, X86::VMOVSSrm
, X86::VMOVDI2PDIrm
},
5992 { X86::VMOVSSrm_alt
,X86::VMOVSSrm_alt
,X86::VMOVDI2PDIrm
},
5993 { X86::VMOVNTPSmr
, X86::VMOVNTPDmr
, X86::VMOVNTDQmr
},
5994 { X86::VANDNPSrm
, X86::VANDNPDrm
, X86::VPANDNrm
},
5995 { X86::VANDNPSrr
, X86::VANDNPDrr
, X86::VPANDNrr
},
5996 { X86::VANDPSrm
, X86::VANDPDrm
, X86::VPANDrm
},
5997 { X86::VANDPSrr
, X86::VANDPDrr
, X86::VPANDrr
},
5998 { X86::VORPSrm
, X86::VORPDrm
, X86::VPORrm
},
5999 { X86::VORPSrr
, X86::VORPDrr
, X86::VPORrr
},
6000 { X86::VXORPSrm
, X86::VXORPDrm
, X86::VPXORrm
},
6001 { X86::VXORPSrr
, X86::VXORPDrr
, X86::VPXORrr
},
6002 { X86::VUNPCKLPDrm
, X86::VUNPCKLPDrm
, X86::VPUNPCKLQDQrm
},
6003 { X86::VMOVLHPSrr
, X86::VUNPCKLPDrr
, X86::VPUNPCKLQDQrr
},
6004 { X86::VUNPCKHPDrm
, X86::VUNPCKHPDrm
, X86::VPUNPCKHQDQrm
},
6005 { X86::VUNPCKHPDrr
, X86::VUNPCKHPDrr
, X86::VPUNPCKHQDQrr
},
6006 { X86::VUNPCKLPSrm
, X86::VUNPCKLPSrm
, X86::VPUNPCKLDQrm
},
6007 { X86::VUNPCKLPSrr
, X86::VUNPCKLPSrr
, X86::VPUNPCKLDQrr
},
6008 { X86::VUNPCKHPSrm
, X86::VUNPCKHPSrm
, X86::VPUNPCKHDQrm
},
6009 { X86::VUNPCKHPSrr
, X86::VUNPCKHPSrr
, X86::VPUNPCKHDQrr
},
6010 { X86::VEXTRACTPSmr
, X86::VEXTRACTPSmr
, X86::VPEXTRDmr
},
6011 { X86::VEXTRACTPSrr
, X86::VEXTRACTPSrr
, X86::VPEXTRDrr
},
6012 // AVX 256-bit support
6013 { X86::VMOVAPSYmr
, X86::VMOVAPDYmr
, X86::VMOVDQAYmr
},
6014 { X86::VMOVAPSYrm
, X86::VMOVAPDYrm
, X86::VMOVDQAYrm
},
6015 { X86::VMOVAPSYrr
, X86::VMOVAPDYrr
, X86::VMOVDQAYrr
},
6016 { X86::VMOVUPSYmr
, X86::VMOVUPDYmr
, X86::VMOVDQUYmr
},
6017 { X86::VMOVUPSYrm
, X86::VMOVUPDYrm
, X86::VMOVDQUYrm
},
6018 { X86::VMOVNTPSYmr
, X86::VMOVNTPDYmr
, X86::VMOVNTDQYmr
},
6019 { X86::VPERMPSYrm
, X86::VPERMPSYrm
, X86::VPERMDYrm
},
6020 { X86::VPERMPSYrr
, X86::VPERMPSYrr
, X86::VPERMDYrr
},
6021 { X86::VPERMPDYmi
, X86::VPERMPDYmi
, X86::VPERMQYmi
},
6022 { X86::VPERMPDYri
, X86::VPERMPDYri
, X86::VPERMQYri
},
6024 { X86::VMOVLPSZ128mr
, X86::VMOVLPDZ128mr
, X86::VMOVPQI2QIZmr
},
6025 { X86::VMOVNTPSZ128mr
, X86::VMOVNTPDZ128mr
, X86::VMOVNTDQZ128mr
},
6026 { X86::VMOVNTPSZ256mr
, X86::VMOVNTPDZ256mr
, X86::VMOVNTDQZ256mr
},
6027 { X86::VMOVNTPSZmr
, X86::VMOVNTPDZmr
, X86::VMOVNTDQZmr
},
6028 { X86::VMOVSDZmr
, X86::VMOVSDZmr
, X86::VMOVPQI2QIZmr
},
6029 { X86::VMOVSSZmr
, X86::VMOVSSZmr
, X86::VMOVPDI2DIZmr
},
6030 { X86::VMOVSDZrm
, X86::VMOVSDZrm
, X86::VMOVQI2PQIZrm
},
6031 { X86::VMOVSDZrm_alt
, X86::VMOVSDZrm_alt
, X86::VMOVQI2PQIZrm
},
6032 { X86::VMOVSSZrm
, X86::VMOVSSZrm
, X86::VMOVDI2PDIZrm
},
6033 { X86::VMOVSSZrm_alt
, X86::VMOVSSZrm_alt
, X86::VMOVDI2PDIZrm
},
6034 { X86::VBROADCASTSSZ128r
, X86::VBROADCASTSSZ128r
, X86::VPBROADCASTDZ128r
},
6035 { X86::VBROADCASTSSZ128m
, X86::VBROADCASTSSZ128m
, X86::VPBROADCASTDZ128m
},
6036 { X86::VBROADCASTSSZ256r
, X86::VBROADCASTSSZ256r
, X86::VPBROADCASTDZ256r
},
6037 { X86::VBROADCASTSSZ256m
, X86::VBROADCASTSSZ256m
, X86::VPBROADCASTDZ256m
},
6038 { X86::VBROADCASTSSZr
, X86::VBROADCASTSSZr
, X86::VPBROADCASTDZr
},
6039 { X86::VBROADCASTSSZm
, X86::VBROADCASTSSZm
, X86::VPBROADCASTDZm
},
6040 { X86::VMOVDDUPZ128rr
, X86::VMOVDDUPZ128rr
, X86::VPBROADCASTQZ128r
},
6041 { X86::VMOVDDUPZ128rm
, X86::VMOVDDUPZ128rm
, X86::VPBROADCASTQZ128m
},
6042 { X86::VBROADCASTSDZ256r
, X86::VBROADCASTSDZ256r
, X86::VPBROADCASTQZ256r
},
6043 { X86::VBROADCASTSDZ256m
, X86::VBROADCASTSDZ256m
, X86::VPBROADCASTQZ256m
},
6044 { X86::VBROADCASTSDZr
, X86::VBROADCASTSDZr
, X86::VPBROADCASTQZr
},
6045 { X86::VBROADCASTSDZm
, X86::VBROADCASTSDZm
, X86::VPBROADCASTQZm
},
6046 { X86::VINSERTF32x4Zrr
, X86::VINSERTF32x4Zrr
, X86::VINSERTI32x4Zrr
},
6047 { X86::VINSERTF32x4Zrm
, X86::VINSERTF32x4Zrm
, X86::VINSERTI32x4Zrm
},
6048 { X86::VINSERTF32x8Zrr
, X86::VINSERTF32x8Zrr
, X86::VINSERTI32x8Zrr
},
6049 { X86::VINSERTF32x8Zrm
, X86::VINSERTF32x8Zrm
, X86::VINSERTI32x8Zrm
},
6050 { X86::VINSERTF64x2Zrr
, X86::VINSERTF64x2Zrr
, X86::VINSERTI64x2Zrr
},
6051 { X86::VINSERTF64x2Zrm
, X86::VINSERTF64x2Zrm
, X86::VINSERTI64x2Zrm
},
6052 { X86::VINSERTF64x4Zrr
, X86::VINSERTF64x4Zrr
, X86::VINSERTI64x4Zrr
},
6053 { X86::VINSERTF64x4Zrm
, X86::VINSERTF64x4Zrm
, X86::VINSERTI64x4Zrm
},
6054 { X86::VINSERTF32x4Z256rr
,X86::VINSERTF32x4Z256rr
,X86::VINSERTI32x4Z256rr
},
6055 { X86::VINSERTF32x4Z256rm
,X86::VINSERTF32x4Z256rm
,X86::VINSERTI32x4Z256rm
},
6056 { X86::VINSERTF64x2Z256rr
,X86::VINSERTF64x2Z256rr
,X86::VINSERTI64x2Z256rr
},
6057 { X86::VINSERTF64x2Z256rm
,X86::VINSERTF64x2Z256rm
,X86::VINSERTI64x2Z256rm
},
6058 { X86::VEXTRACTF32x4Zrr
, X86::VEXTRACTF32x4Zrr
, X86::VEXTRACTI32x4Zrr
},
6059 { X86::VEXTRACTF32x4Zmr
, X86::VEXTRACTF32x4Zmr
, X86::VEXTRACTI32x4Zmr
},
6060 { X86::VEXTRACTF32x8Zrr
, X86::VEXTRACTF32x8Zrr
, X86::VEXTRACTI32x8Zrr
},
6061 { X86::VEXTRACTF32x8Zmr
, X86::VEXTRACTF32x8Zmr
, X86::VEXTRACTI32x8Zmr
},
6062 { X86::VEXTRACTF64x2Zrr
, X86::VEXTRACTF64x2Zrr
, X86::VEXTRACTI64x2Zrr
},
6063 { X86::VEXTRACTF64x2Zmr
, X86::VEXTRACTF64x2Zmr
, X86::VEXTRACTI64x2Zmr
},
6064 { X86::VEXTRACTF64x4Zrr
, X86::VEXTRACTF64x4Zrr
, X86::VEXTRACTI64x4Zrr
},
6065 { X86::VEXTRACTF64x4Zmr
, X86::VEXTRACTF64x4Zmr
, X86::VEXTRACTI64x4Zmr
},
6066 { X86::VEXTRACTF32x4Z256rr
,X86::VEXTRACTF32x4Z256rr
,X86::VEXTRACTI32x4Z256rr
},
6067 { X86::VEXTRACTF32x4Z256mr
,X86::VEXTRACTF32x4Z256mr
,X86::VEXTRACTI32x4Z256mr
},
6068 { X86::VEXTRACTF64x2Z256rr
,X86::VEXTRACTF64x2Z256rr
,X86::VEXTRACTI64x2Z256rr
},
6069 { X86::VEXTRACTF64x2Z256mr
,X86::VEXTRACTF64x2Z256mr
,X86::VEXTRACTI64x2Z256mr
},
6070 { X86::VPERMILPSmi
, X86::VPERMILPSmi
, X86::VPSHUFDmi
},
6071 { X86::VPERMILPSri
, X86::VPERMILPSri
, X86::VPSHUFDri
},
6072 { X86::VPERMILPSZ128mi
, X86::VPERMILPSZ128mi
, X86::VPSHUFDZ128mi
},
6073 { X86::VPERMILPSZ128ri
, X86::VPERMILPSZ128ri
, X86::VPSHUFDZ128ri
},
6074 { X86::VPERMILPSZ256mi
, X86::VPERMILPSZ256mi
, X86::VPSHUFDZ256mi
},
6075 { X86::VPERMILPSZ256ri
, X86::VPERMILPSZ256ri
, X86::VPSHUFDZ256ri
},
6076 { X86::VPERMILPSZmi
, X86::VPERMILPSZmi
, X86::VPSHUFDZmi
},
6077 { X86::VPERMILPSZri
, X86::VPERMILPSZri
, X86::VPSHUFDZri
},
6078 { X86::VPERMPSZ256rm
, X86::VPERMPSZ256rm
, X86::VPERMDZ256rm
},
6079 { X86::VPERMPSZ256rr
, X86::VPERMPSZ256rr
, X86::VPERMDZ256rr
},
6080 { X86::VPERMPDZ256mi
, X86::VPERMPDZ256mi
, X86::VPERMQZ256mi
},
6081 { X86::VPERMPDZ256ri
, X86::VPERMPDZ256ri
, X86::VPERMQZ256ri
},
6082 { X86::VPERMPDZ256rm
, X86::VPERMPDZ256rm
, X86::VPERMQZ256rm
},
6083 { X86::VPERMPDZ256rr
, X86::VPERMPDZ256rr
, X86::VPERMQZ256rr
},
6084 { X86::VPERMPSZrm
, X86::VPERMPSZrm
, X86::VPERMDZrm
},
6085 { X86::VPERMPSZrr
, X86::VPERMPSZrr
, X86::VPERMDZrr
},
6086 { X86::VPERMPDZmi
, X86::VPERMPDZmi
, X86::VPERMQZmi
},
6087 { X86::VPERMPDZri
, X86::VPERMPDZri
, X86::VPERMQZri
},
6088 { X86::VPERMPDZrm
, X86::VPERMPDZrm
, X86::VPERMQZrm
},
6089 { X86::VPERMPDZrr
, X86::VPERMPDZrr
, X86::VPERMQZrr
},
6090 { X86::VUNPCKLPDZ256rm
, X86::VUNPCKLPDZ256rm
, X86::VPUNPCKLQDQZ256rm
},
6091 { X86::VUNPCKLPDZ256rr
, X86::VUNPCKLPDZ256rr
, X86::VPUNPCKLQDQZ256rr
},
6092 { X86::VUNPCKHPDZ256rm
, X86::VUNPCKHPDZ256rm
, X86::VPUNPCKHQDQZ256rm
},
6093 { X86::VUNPCKHPDZ256rr
, X86::VUNPCKHPDZ256rr
, X86::VPUNPCKHQDQZ256rr
},
6094 { X86::VUNPCKLPSZ256rm
, X86::VUNPCKLPSZ256rm
, X86::VPUNPCKLDQZ256rm
},
6095 { X86::VUNPCKLPSZ256rr
, X86::VUNPCKLPSZ256rr
, X86::VPUNPCKLDQZ256rr
},
6096 { X86::VUNPCKHPSZ256rm
, X86::VUNPCKHPSZ256rm
, X86::VPUNPCKHDQZ256rm
},
6097 { X86::VUNPCKHPSZ256rr
, X86::VUNPCKHPSZ256rr
, X86::VPUNPCKHDQZ256rr
},
6098 { X86::VUNPCKLPDZ128rm
, X86::VUNPCKLPDZ128rm
, X86::VPUNPCKLQDQZ128rm
},
6099 { X86::VMOVLHPSZrr
, X86::VUNPCKLPDZ128rr
, X86::VPUNPCKLQDQZ128rr
},
6100 { X86::VUNPCKHPDZ128rm
, X86::VUNPCKHPDZ128rm
, X86::VPUNPCKHQDQZ128rm
},
6101 { X86::VUNPCKHPDZ128rr
, X86::VUNPCKHPDZ128rr
, X86::VPUNPCKHQDQZ128rr
},
6102 { X86::VUNPCKLPSZ128rm
, X86::VUNPCKLPSZ128rm
, X86::VPUNPCKLDQZ128rm
},
6103 { X86::VUNPCKLPSZ128rr
, X86::VUNPCKLPSZ128rr
, X86::VPUNPCKLDQZ128rr
},
6104 { X86::VUNPCKHPSZ128rm
, X86::VUNPCKHPSZ128rm
, X86::VPUNPCKHDQZ128rm
},
6105 { X86::VUNPCKHPSZ128rr
, X86::VUNPCKHPSZ128rr
, X86::VPUNPCKHDQZ128rr
},
6106 { X86::VUNPCKLPDZrm
, X86::VUNPCKLPDZrm
, X86::VPUNPCKLQDQZrm
},
6107 { X86::VUNPCKLPDZrr
, X86::VUNPCKLPDZrr
, X86::VPUNPCKLQDQZrr
},
6108 { X86::VUNPCKHPDZrm
, X86::VUNPCKHPDZrm
, X86::VPUNPCKHQDQZrm
},
6109 { X86::VUNPCKHPDZrr
, X86::VUNPCKHPDZrr
, X86::VPUNPCKHQDQZrr
},
6110 { X86::VUNPCKLPSZrm
, X86::VUNPCKLPSZrm
, X86::VPUNPCKLDQZrm
},
6111 { X86::VUNPCKLPSZrr
, X86::VUNPCKLPSZrr
, X86::VPUNPCKLDQZrr
},
6112 { X86::VUNPCKHPSZrm
, X86::VUNPCKHPSZrm
, X86::VPUNPCKHDQZrm
},
6113 { X86::VUNPCKHPSZrr
, X86::VUNPCKHPSZrr
, X86::VPUNPCKHDQZrr
},
6114 { X86::VEXTRACTPSZmr
, X86::VEXTRACTPSZmr
, X86::VPEXTRDZmr
},
6115 { X86::VEXTRACTPSZrr
, X86::VEXTRACTPSZrr
, X86::VPEXTRDZrr
},
6118 static const uint16_t ReplaceableInstrsAVX2
[][3] = {
6119 //PackedSingle PackedDouble PackedInt
6120 { X86::VANDNPSYrm
, X86::VANDNPDYrm
, X86::VPANDNYrm
},
6121 { X86::VANDNPSYrr
, X86::VANDNPDYrr
, X86::VPANDNYrr
},
6122 { X86::VANDPSYrm
, X86::VANDPDYrm
, X86::VPANDYrm
},
6123 { X86::VANDPSYrr
, X86::VANDPDYrr
, X86::VPANDYrr
},
6124 { X86::VORPSYrm
, X86::VORPDYrm
, X86::VPORYrm
},
6125 { X86::VORPSYrr
, X86::VORPDYrr
, X86::VPORYrr
},
6126 { X86::VXORPSYrm
, X86::VXORPDYrm
, X86::VPXORYrm
},
6127 { X86::VXORPSYrr
, X86::VXORPDYrr
, X86::VPXORYrr
},
6128 { X86::VPERM2F128rm
, X86::VPERM2F128rm
, X86::VPERM2I128rm
},
6129 { X86::VPERM2F128rr
, X86::VPERM2F128rr
, X86::VPERM2I128rr
},
6130 { X86::VBROADCASTSSrm
, X86::VBROADCASTSSrm
, X86::VPBROADCASTDrm
},
6131 { X86::VBROADCASTSSrr
, X86::VBROADCASTSSrr
, X86::VPBROADCASTDrr
},
6132 { X86::VMOVDDUPrm
, X86::VMOVDDUPrm
, X86::VPBROADCASTQrm
},
6133 { X86::VMOVDDUPrr
, X86::VMOVDDUPrr
, X86::VPBROADCASTQrr
},
6134 { X86::VBROADCASTSSYrr
, X86::VBROADCASTSSYrr
, X86::VPBROADCASTDYrr
},
6135 { X86::VBROADCASTSSYrm
, X86::VBROADCASTSSYrm
, X86::VPBROADCASTDYrm
},
6136 { X86::VBROADCASTSDYrr
, X86::VBROADCASTSDYrr
, X86::VPBROADCASTQYrr
},
6137 { X86::VBROADCASTSDYrm
, X86::VBROADCASTSDYrm
, X86::VPBROADCASTQYrm
},
6138 { X86::VBROADCASTF128
, X86::VBROADCASTF128
, X86::VBROADCASTI128
},
6139 { X86::VBLENDPSYrri
, X86::VBLENDPSYrri
, X86::VPBLENDDYrri
},
6140 { X86::VBLENDPSYrmi
, X86::VBLENDPSYrmi
, X86::VPBLENDDYrmi
},
6141 { X86::VPERMILPSYmi
, X86::VPERMILPSYmi
, X86::VPSHUFDYmi
},
6142 { X86::VPERMILPSYri
, X86::VPERMILPSYri
, X86::VPSHUFDYri
},
6143 { X86::VUNPCKLPDYrm
, X86::VUNPCKLPDYrm
, X86::VPUNPCKLQDQYrm
},
6144 { X86::VUNPCKLPDYrr
, X86::VUNPCKLPDYrr
, X86::VPUNPCKLQDQYrr
},
6145 { X86::VUNPCKHPDYrm
, X86::VUNPCKHPDYrm
, X86::VPUNPCKHQDQYrm
},
6146 { X86::VUNPCKHPDYrr
, X86::VUNPCKHPDYrr
, X86::VPUNPCKHQDQYrr
},
6147 { X86::VUNPCKLPSYrm
, X86::VUNPCKLPSYrm
, X86::VPUNPCKLDQYrm
},
6148 { X86::VUNPCKLPSYrr
, X86::VUNPCKLPSYrr
, X86::VPUNPCKLDQYrr
},
6149 { X86::VUNPCKHPSYrm
, X86::VUNPCKHPSYrm
, X86::VPUNPCKHDQYrm
},
6150 { X86::VUNPCKHPSYrr
, X86::VUNPCKHPSYrr
, X86::VPUNPCKHDQYrr
},
6153 static const uint16_t ReplaceableInstrsFP
[][3] = {
6154 //PackedSingle PackedDouble
6155 { X86::MOVLPSrm
, X86::MOVLPDrm
, X86::INSTRUCTION_LIST_END
},
6156 { X86::MOVHPSrm
, X86::MOVHPDrm
, X86::INSTRUCTION_LIST_END
},
6157 { X86::MOVHPSmr
, X86::MOVHPDmr
, X86::INSTRUCTION_LIST_END
},
6158 { X86::VMOVLPSrm
, X86::VMOVLPDrm
, X86::INSTRUCTION_LIST_END
},
6159 { X86::VMOVHPSrm
, X86::VMOVHPDrm
, X86::INSTRUCTION_LIST_END
},
6160 { X86::VMOVHPSmr
, X86::VMOVHPDmr
, X86::INSTRUCTION_LIST_END
},
6161 { X86::VMOVLPSZ128rm
, X86::VMOVLPDZ128rm
, X86::INSTRUCTION_LIST_END
},
6162 { X86::VMOVHPSZ128rm
, X86::VMOVHPDZ128rm
, X86::INSTRUCTION_LIST_END
},
6163 { X86::VMOVHPSZ128mr
, X86::VMOVHPDZ128mr
, X86::INSTRUCTION_LIST_END
},
6166 static const uint16_t ReplaceableInstrsAVX2InsertExtract
[][3] = {
6167 //PackedSingle PackedDouble PackedInt
6168 { X86::VEXTRACTF128mr
, X86::VEXTRACTF128mr
, X86::VEXTRACTI128mr
},
6169 { X86::VEXTRACTF128rr
, X86::VEXTRACTF128rr
, X86::VEXTRACTI128rr
},
6170 { X86::VINSERTF128rm
, X86::VINSERTF128rm
, X86::VINSERTI128rm
},
6171 { X86::VINSERTF128rr
, X86::VINSERTF128rr
, X86::VINSERTI128rr
},
6174 static const uint16_t ReplaceableInstrsAVX512
[][4] = {
6175 // Two integer columns for 64-bit and 32-bit elements.
6176 //PackedSingle PackedDouble PackedInt PackedInt
6177 { X86::VMOVAPSZ128mr
, X86::VMOVAPDZ128mr
, X86::VMOVDQA64Z128mr
, X86::VMOVDQA32Z128mr
},
6178 { X86::VMOVAPSZ128rm
, X86::VMOVAPDZ128rm
, X86::VMOVDQA64Z128rm
, X86::VMOVDQA32Z128rm
},
6179 { X86::VMOVAPSZ128rr
, X86::VMOVAPDZ128rr
, X86::VMOVDQA64Z128rr
, X86::VMOVDQA32Z128rr
},
6180 { X86::VMOVUPSZ128mr
, X86::VMOVUPDZ128mr
, X86::VMOVDQU64Z128mr
, X86::VMOVDQU32Z128mr
},
6181 { X86::VMOVUPSZ128rm
, X86::VMOVUPDZ128rm
, X86::VMOVDQU64Z128rm
, X86::VMOVDQU32Z128rm
},
6182 { X86::VMOVAPSZ256mr
, X86::VMOVAPDZ256mr
, X86::VMOVDQA64Z256mr
, X86::VMOVDQA32Z256mr
},
6183 { X86::VMOVAPSZ256rm
, X86::VMOVAPDZ256rm
, X86::VMOVDQA64Z256rm
, X86::VMOVDQA32Z256rm
},
6184 { X86::VMOVAPSZ256rr
, X86::VMOVAPDZ256rr
, X86::VMOVDQA64Z256rr
, X86::VMOVDQA32Z256rr
},
6185 { X86::VMOVUPSZ256mr
, X86::VMOVUPDZ256mr
, X86::VMOVDQU64Z256mr
, X86::VMOVDQU32Z256mr
},
6186 { X86::VMOVUPSZ256rm
, X86::VMOVUPDZ256rm
, X86::VMOVDQU64Z256rm
, X86::VMOVDQU32Z256rm
},
6187 { X86::VMOVAPSZmr
, X86::VMOVAPDZmr
, X86::VMOVDQA64Zmr
, X86::VMOVDQA32Zmr
},
6188 { X86::VMOVAPSZrm
, X86::VMOVAPDZrm
, X86::VMOVDQA64Zrm
, X86::VMOVDQA32Zrm
},
6189 { X86::VMOVAPSZrr
, X86::VMOVAPDZrr
, X86::VMOVDQA64Zrr
, X86::VMOVDQA32Zrr
},
6190 { X86::VMOVUPSZmr
, X86::VMOVUPDZmr
, X86::VMOVDQU64Zmr
, X86::VMOVDQU32Zmr
},
6191 { X86::VMOVUPSZrm
, X86::VMOVUPDZrm
, X86::VMOVDQU64Zrm
, X86::VMOVDQU32Zrm
},
6194 static const uint16_t ReplaceableInstrsAVX512DQ
[][4] = {
6195 // Two integer columns for 64-bit and 32-bit elements.
6196 //PackedSingle PackedDouble PackedInt PackedInt
6197 { X86::VANDNPSZ128rm
, X86::VANDNPDZ128rm
, X86::VPANDNQZ128rm
, X86::VPANDNDZ128rm
},
6198 { X86::VANDNPSZ128rr
, X86::VANDNPDZ128rr
, X86::VPANDNQZ128rr
, X86::VPANDNDZ128rr
},
6199 { X86::VANDPSZ128rm
, X86::VANDPDZ128rm
, X86::VPANDQZ128rm
, X86::VPANDDZ128rm
},
6200 { X86::VANDPSZ128rr
, X86::VANDPDZ128rr
, X86::VPANDQZ128rr
, X86::VPANDDZ128rr
},
6201 { X86::VORPSZ128rm
, X86::VORPDZ128rm
, X86::VPORQZ128rm
, X86::VPORDZ128rm
},
6202 { X86::VORPSZ128rr
, X86::VORPDZ128rr
, X86::VPORQZ128rr
, X86::VPORDZ128rr
},
6203 { X86::VXORPSZ128rm
, X86::VXORPDZ128rm
, X86::VPXORQZ128rm
, X86::VPXORDZ128rm
},
6204 { X86::VXORPSZ128rr
, X86::VXORPDZ128rr
, X86::VPXORQZ128rr
, X86::VPXORDZ128rr
},
6205 { X86::VANDNPSZ256rm
, X86::VANDNPDZ256rm
, X86::VPANDNQZ256rm
, X86::VPANDNDZ256rm
},
6206 { X86::VANDNPSZ256rr
, X86::VANDNPDZ256rr
, X86::VPANDNQZ256rr
, X86::VPANDNDZ256rr
},
6207 { X86::VANDPSZ256rm
, X86::VANDPDZ256rm
, X86::VPANDQZ256rm
, X86::VPANDDZ256rm
},
6208 { X86::VANDPSZ256rr
, X86::VANDPDZ256rr
, X86::VPANDQZ256rr
, X86::VPANDDZ256rr
},
6209 { X86::VORPSZ256rm
, X86::VORPDZ256rm
, X86::VPORQZ256rm
, X86::VPORDZ256rm
},
6210 { X86::VORPSZ256rr
, X86::VORPDZ256rr
, X86::VPORQZ256rr
, X86::VPORDZ256rr
},
6211 { X86::VXORPSZ256rm
, X86::VXORPDZ256rm
, X86::VPXORQZ256rm
, X86::VPXORDZ256rm
},
6212 { X86::VXORPSZ256rr
, X86::VXORPDZ256rr
, X86::VPXORQZ256rr
, X86::VPXORDZ256rr
},
6213 { X86::VANDNPSZrm
, X86::VANDNPDZrm
, X86::VPANDNQZrm
, X86::VPANDNDZrm
},
6214 { X86::VANDNPSZrr
, X86::VANDNPDZrr
, X86::VPANDNQZrr
, X86::VPANDNDZrr
},
6215 { X86::VANDPSZrm
, X86::VANDPDZrm
, X86::VPANDQZrm
, X86::VPANDDZrm
},
6216 { X86::VANDPSZrr
, X86::VANDPDZrr
, X86::VPANDQZrr
, X86::VPANDDZrr
},
6217 { X86::VORPSZrm
, X86::VORPDZrm
, X86::VPORQZrm
, X86::VPORDZrm
},
6218 { X86::VORPSZrr
, X86::VORPDZrr
, X86::VPORQZrr
, X86::VPORDZrr
},
6219 { X86::VXORPSZrm
, X86::VXORPDZrm
, X86::VPXORQZrm
, X86::VPXORDZrm
},
6220 { X86::VXORPSZrr
, X86::VXORPDZrr
, X86::VPXORQZrr
, X86::VPXORDZrr
},
6223 static const uint16_t ReplaceableInstrsAVX512DQMasked
[][4] = {
6224 // Two integer columns for 64-bit and 32-bit elements.
6225 //PackedSingle PackedDouble
6226 //PackedInt PackedInt
6227 { X86::VANDNPSZ128rmk
, X86::VANDNPDZ128rmk
,
6228 X86::VPANDNQZ128rmk
, X86::VPANDNDZ128rmk
},
6229 { X86::VANDNPSZ128rmkz
, X86::VANDNPDZ128rmkz
,
6230 X86::VPANDNQZ128rmkz
, X86::VPANDNDZ128rmkz
},
6231 { X86::VANDNPSZ128rrk
, X86::VANDNPDZ128rrk
,
6232 X86::VPANDNQZ128rrk
, X86::VPANDNDZ128rrk
},
6233 { X86::VANDNPSZ128rrkz
, X86::VANDNPDZ128rrkz
,
6234 X86::VPANDNQZ128rrkz
, X86::VPANDNDZ128rrkz
},
6235 { X86::VANDPSZ128rmk
, X86::VANDPDZ128rmk
,
6236 X86::VPANDQZ128rmk
, X86::VPANDDZ128rmk
},
6237 { X86::VANDPSZ128rmkz
, X86::VANDPDZ128rmkz
,
6238 X86::VPANDQZ128rmkz
, X86::VPANDDZ128rmkz
},
6239 { X86::VANDPSZ128rrk
, X86::VANDPDZ128rrk
,
6240 X86::VPANDQZ128rrk
, X86::VPANDDZ128rrk
},
6241 { X86::VANDPSZ128rrkz
, X86::VANDPDZ128rrkz
,
6242 X86::VPANDQZ128rrkz
, X86::VPANDDZ128rrkz
},
6243 { X86::VORPSZ128rmk
, X86::VORPDZ128rmk
,
6244 X86::VPORQZ128rmk
, X86::VPORDZ128rmk
},
6245 { X86::VORPSZ128rmkz
, X86::VORPDZ128rmkz
,
6246 X86::VPORQZ128rmkz
, X86::VPORDZ128rmkz
},
6247 { X86::VORPSZ128rrk
, X86::VORPDZ128rrk
,
6248 X86::VPORQZ128rrk
, X86::VPORDZ128rrk
},
6249 { X86::VORPSZ128rrkz
, X86::VORPDZ128rrkz
,
6250 X86::VPORQZ128rrkz
, X86::VPORDZ128rrkz
},
6251 { X86::VXORPSZ128rmk
, X86::VXORPDZ128rmk
,
6252 X86::VPXORQZ128rmk
, X86::VPXORDZ128rmk
},
6253 { X86::VXORPSZ128rmkz
, X86::VXORPDZ128rmkz
,
6254 X86::VPXORQZ128rmkz
, X86::VPXORDZ128rmkz
},
6255 { X86::VXORPSZ128rrk
, X86::VXORPDZ128rrk
,
6256 X86::VPXORQZ128rrk
, X86::VPXORDZ128rrk
},
6257 { X86::VXORPSZ128rrkz
, X86::VXORPDZ128rrkz
,
6258 X86::VPXORQZ128rrkz
, X86::VPXORDZ128rrkz
},
6259 { X86::VANDNPSZ256rmk
, X86::VANDNPDZ256rmk
,
6260 X86::VPANDNQZ256rmk
, X86::VPANDNDZ256rmk
},
6261 { X86::VANDNPSZ256rmkz
, X86::VANDNPDZ256rmkz
,
6262 X86::VPANDNQZ256rmkz
, X86::VPANDNDZ256rmkz
},
6263 { X86::VANDNPSZ256rrk
, X86::VANDNPDZ256rrk
,
6264 X86::VPANDNQZ256rrk
, X86::VPANDNDZ256rrk
},
6265 { X86::VANDNPSZ256rrkz
, X86::VANDNPDZ256rrkz
,
6266 X86::VPANDNQZ256rrkz
, X86::VPANDNDZ256rrkz
},
6267 { X86::VANDPSZ256rmk
, X86::VANDPDZ256rmk
,
6268 X86::VPANDQZ256rmk
, X86::VPANDDZ256rmk
},
6269 { X86::VANDPSZ256rmkz
, X86::VANDPDZ256rmkz
,
6270 X86::VPANDQZ256rmkz
, X86::VPANDDZ256rmkz
},
6271 { X86::VANDPSZ256rrk
, X86::VANDPDZ256rrk
,
6272 X86::VPANDQZ256rrk
, X86::VPANDDZ256rrk
},
6273 { X86::VANDPSZ256rrkz
, X86::VANDPDZ256rrkz
,
6274 X86::VPANDQZ256rrkz
, X86::VPANDDZ256rrkz
},
6275 { X86::VORPSZ256rmk
, X86::VORPDZ256rmk
,
6276 X86::VPORQZ256rmk
, X86::VPORDZ256rmk
},
6277 { X86::VORPSZ256rmkz
, X86::VORPDZ256rmkz
,
6278 X86::VPORQZ256rmkz
, X86::VPORDZ256rmkz
},
6279 { X86::VORPSZ256rrk
, X86::VORPDZ256rrk
,
6280 X86::VPORQZ256rrk
, X86::VPORDZ256rrk
},
6281 { X86::VORPSZ256rrkz
, X86::VORPDZ256rrkz
,
6282 X86::VPORQZ256rrkz
, X86::VPORDZ256rrkz
},
6283 { X86::VXORPSZ256rmk
, X86::VXORPDZ256rmk
,
6284 X86::VPXORQZ256rmk
, X86::VPXORDZ256rmk
},
6285 { X86::VXORPSZ256rmkz
, X86::VXORPDZ256rmkz
,
6286 X86::VPXORQZ256rmkz
, X86::VPXORDZ256rmkz
},
6287 { X86::VXORPSZ256rrk
, X86::VXORPDZ256rrk
,
6288 X86::VPXORQZ256rrk
, X86::VPXORDZ256rrk
},
6289 { X86::VXORPSZ256rrkz
, X86::VXORPDZ256rrkz
,
6290 X86::VPXORQZ256rrkz
, X86::VPXORDZ256rrkz
},
6291 { X86::VANDNPSZrmk
, X86::VANDNPDZrmk
,
6292 X86::VPANDNQZrmk
, X86::VPANDNDZrmk
},
6293 { X86::VANDNPSZrmkz
, X86::VANDNPDZrmkz
,
6294 X86::VPANDNQZrmkz
, X86::VPANDNDZrmkz
},
6295 { X86::VANDNPSZrrk
, X86::VANDNPDZrrk
,
6296 X86::VPANDNQZrrk
, X86::VPANDNDZrrk
},
6297 { X86::VANDNPSZrrkz
, X86::VANDNPDZrrkz
,
6298 X86::VPANDNQZrrkz
, X86::VPANDNDZrrkz
},
6299 { X86::VANDPSZrmk
, X86::VANDPDZrmk
,
6300 X86::VPANDQZrmk
, X86::VPANDDZrmk
},
6301 { X86::VANDPSZrmkz
, X86::VANDPDZrmkz
,
6302 X86::VPANDQZrmkz
, X86::VPANDDZrmkz
},
6303 { X86::VANDPSZrrk
, X86::VANDPDZrrk
,
6304 X86::VPANDQZrrk
, X86::VPANDDZrrk
},
6305 { X86::VANDPSZrrkz
, X86::VANDPDZrrkz
,
6306 X86::VPANDQZrrkz
, X86::VPANDDZrrkz
},
6307 { X86::VORPSZrmk
, X86::VORPDZrmk
,
6308 X86::VPORQZrmk
, X86::VPORDZrmk
},
6309 { X86::VORPSZrmkz
, X86::VORPDZrmkz
,
6310 X86::VPORQZrmkz
, X86::VPORDZrmkz
},
6311 { X86::VORPSZrrk
, X86::VORPDZrrk
,
6312 X86::VPORQZrrk
, X86::VPORDZrrk
},
6313 { X86::VORPSZrrkz
, X86::VORPDZrrkz
,
6314 X86::VPORQZrrkz
, X86::VPORDZrrkz
},
6315 { X86::VXORPSZrmk
, X86::VXORPDZrmk
,
6316 X86::VPXORQZrmk
, X86::VPXORDZrmk
},
6317 { X86::VXORPSZrmkz
, X86::VXORPDZrmkz
,
6318 X86::VPXORQZrmkz
, X86::VPXORDZrmkz
},
6319 { X86::VXORPSZrrk
, X86::VXORPDZrrk
,
6320 X86::VPXORQZrrk
, X86::VPXORDZrrk
},
6321 { X86::VXORPSZrrkz
, X86::VXORPDZrrkz
,
6322 X86::VPXORQZrrkz
, X86::VPXORDZrrkz
},
6323 // Broadcast loads can be handled the same as masked operations to avoid
6324 // changing element size.
6325 { X86::VANDNPSZ128rmb
, X86::VANDNPDZ128rmb
,
6326 X86::VPANDNQZ128rmb
, X86::VPANDNDZ128rmb
},
6327 { X86::VANDPSZ128rmb
, X86::VANDPDZ128rmb
,
6328 X86::VPANDQZ128rmb
, X86::VPANDDZ128rmb
},
6329 { X86::VORPSZ128rmb
, X86::VORPDZ128rmb
,
6330 X86::VPORQZ128rmb
, X86::VPORDZ128rmb
},
6331 { X86::VXORPSZ128rmb
, X86::VXORPDZ128rmb
,
6332 X86::VPXORQZ128rmb
, X86::VPXORDZ128rmb
},
6333 { X86::VANDNPSZ256rmb
, X86::VANDNPDZ256rmb
,
6334 X86::VPANDNQZ256rmb
, X86::VPANDNDZ256rmb
},
6335 { X86::VANDPSZ256rmb
, X86::VANDPDZ256rmb
,
6336 X86::VPANDQZ256rmb
, X86::VPANDDZ256rmb
},
6337 { X86::VORPSZ256rmb
, X86::VORPDZ256rmb
,
6338 X86::VPORQZ256rmb
, X86::VPORDZ256rmb
},
6339 { X86::VXORPSZ256rmb
, X86::VXORPDZ256rmb
,
6340 X86::VPXORQZ256rmb
, X86::VPXORDZ256rmb
},
6341 { X86::VANDNPSZrmb
, X86::VANDNPDZrmb
,
6342 X86::VPANDNQZrmb
, X86::VPANDNDZrmb
},
6343 { X86::VANDPSZrmb
, X86::VANDPDZrmb
,
6344 X86::VPANDQZrmb
, X86::VPANDDZrmb
},
6345 { X86::VANDPSZrmb
, X86::VANDPDZrmb
,
6346 X86::VPANDQZrmb
, X86::VPANDDZrmb
},
6347 { X86::VORPSZrmb
, X86::VORPDZrmb
,
6348 X86::VPORQZrmb
, X86::VPORDZrmb
},
6349 { X86::VXORPSZrmb
, X86::VXORPDZrmb
,
6350 X86::VPXORQZrmb
, X86::VPXORDZrmb
},
6351 { X86::VANDNPSZ128rmbk
, X86::VANDNPDZ128rmbk
,
6352 X86::VPANDNQZ128rmbk
, X86::VPANDNDZ128rmbk
},
6353 { X86::VANDPSZ128rmbk
, X86::VANDPDZ128rmbk
,
6354 X86::VPANDQZ128rmbk
, X86::VPANDDZ128rmbk
},
6355 { X86::VORPSZ128rmbk
, X86::VORPDZ128rmbk
,
6356 X86::VPORQZ128rmbk
, X86::VPORDZ128rmbk
},
6357 { X86::VXORPSZ128rmbk
, X86::VXORPDZ128rmbk
,
6358 X86::VPXORQZ128rmbk
, X86::VPXORDZ128rmbk
},
6359 { X86::VANDNPSZ256rmbk
, X86::VANDNPDZ256rmbk
,
6360 X86::VPANDNQZ256rmbk
, X86::VPANDNDZ256rmbk
},
6361 { X86::VANDPSZ256rmbk
, X86::VANDPDZ256rmbk
,
6362 X86::VPANDQZ256rmbk
, X86::VPANDDZ256rmbk
},
6363 { X86::VORPSZ256rmbk
, X86::VORPDZ256rmbk
,
6364 X86::VPORQZ256rmbk
, X86::VPORDZ256rmbk
},
6365 { X86::VXORPSZ256rmbk
, X86::VXORPDZ256rmbk
,
6366 X86::VPXORQZ256rmbk
, X86::VPXORDZ256rmbk
},
6367 { X86::VANDNPSZrmbk
, X86::VANDNPDZrmbk
,
6368 X86::VPANDNQZrmbk
, X86::VPANDNDZrmbk
},
6369 { X86::VANDPSZrmbk
, X86::VANDPDZrmbk
,
6370 X86::VPANDQZrmbk
, X86::VPANDDZrmbk
},
6371 { X86::VANDPSZrmbk
, X86::VANDPDZrmbk
,
6372 X86::VPANDQZrmbk
, X86::VPANDDZrmbk
},
6373 { X86::VORPSZrmbk
, X86::VORPDZrmbk
,
6374 X86::VPORQZrmbk
, X86::VPORDZrmbk
},
6375 { X86::VXORPSZrmbk
, X86::VXORPDZrmbk
,
6376 X86::VPXORQZrmbk
, X86::VPXORDZrmbk
},
6377 { X86::VANDNPSZ128rmbkz
,X86::VANDNPDZ128rmbkz
,
6378 X86::VPANDNQZ128rmbkz
,X86::VPANDNDZ128rmbkz
},
6379 { X86::VANDPSZ128rmbkz
, X86::VANDPDZ128rmbkz
,
6380 X86::VPANDQZ128rmbkz
, X86::VPANDDZ128rmbkz
},
6381 { X86::VORPSZ128rmbkz
, X86::VORPDZ128rmbkz
,
6382 X86::VPORQZ128rmbkz
, X86::VPORDZ128rmbkz
},
6383 { X86::VXORPSZ128rmbkz
, X86::VXORPDZ128rmbkz
,
6384 X86::VPXORQZ128rmbkz
, X86::VPXORDZ128rmbkz
},
6385 { X86::VANDNPSZ256rmbkz
,X86::VANDNPDZ256rmbkz
,
6386 X86::VPANDNQZ256rmbkz
,X86::VPANDNDZ256rmbkz
},
6387 { X86::VANDPSZ256rmbkz
, X86::VANDPDZ256rmbkz
,
6388 X86::VPANDQZ256rmbkz
, X86::VPANDDZ256rmbkz
},
6389 { X86::VORPSZ256rmbkz
, X86::VORPDZ256rmbkz
,
6390 X86::VPORQZ256rmbkz
, X86::VPORDZ256rmbkz
},
6391 { X86::VXORPSZ256rmbkz
, X86::VXORPDZ256rmbkz
,
6392 X86::VPXORQZ256rmbkz
, X86::VPXORDZ256rmbkz
},
6393 { X86::VANDNPSZrmbkz
, X86::VANDNPDZrmbkz
,
6394 X86::VPANDNQZrmbkz
, X86::VPANDNDZrmbkz
},
6395 { X86::VANDPSZrmbkz
, X86::VANDPDZrmbkz
,
6396 X86::VPANDQZrmbkz
, X86::VPANDDZrmbkz
},
6397 { X86::VANDPSZrmbkz
, X86::VANDPDZrmbkz
,
6398 X86::VPANDQZrmbkz
, X86::VPANDDZrmbkz
},
6399 { X86::VORPSZrmbkz
, X86::VORPDZrmbkz
,
6400 X86::VPORQZrmbkz
, X86::VPORDZrmbkz
},
6401 { X86::VXORPSZrmbkz
, X86::VXORPDZrmbkz
,
6402 X86::VPXORQZrmbkz
, X86::VPXORDZrmbkz
},
6405 // NOTE: These should only be used by the custom domain methods.
6406 static const uint16_t ReplaceableBlendInstrs
[][3] = {
6407 //PackedSingle PackedDouble PackedInt
6408 { X86::BLENDPSrmi
, X86::BLENDPDrmi
, X86::PBLENDWrmi
},
6409 { X86::BLENDPSrri
, X86::BLENDPDrri
, X86::PBLENDWrri
},
6410 { X86::VBLENDPSrmi
, X86::VBLENDPDrmi
, X86::VPBLENDWrmi
},
6411 { X86::VBLENDPSrri
, X86::VBLENDPDrri
, X86::VPBLENDWrri
},
6412 { X86::VBLENDPSYrmi
, X86::VBLENDPDYrmi
, X86::VPBLENDWYrmi
},
6413 { X86::VBLENDPSYrri
, X86::VBLENDPDYrri
, X86::VPBLENDWYrri
},
6415 static const uint16_t ReplaceableBlendAVX2Instrs
[][3] = {
6416 //PackedSingle PackedDouble PackedInt
6417 { X86::VBLENDPSrmi
, X86::VBLENDPDrmi
, X86::VPBLENDDrmi
},
6418 { X86::VBLENDPSrri
, X86::VBLENDPDrri
, X86::VPBLENDDrri
},
6419 { X86::VBLENDPSYrmi
, X86::VBLENDPDYrmi
, X86::VPBLENDDYrmi
},
6420 { X86::VBLENDPSYrri
, X86::VBLENDPDYrri
, X86::VPBLENDDYrri
},
6423 // Special table for changing EVEX logic instructions to VEX.
6424 // TODO: Should we run EVEX->VEX earlier?
6425 static const uint16_t ReplaceableCustomAVX512LogicInstrs
[][4] = {
6426 // Two integer columns for 64-bit and 32-bit elements.
6427 //PackedSingle PackedDouble PackedInt PackedInt
6428 { X86::VANDNPSrm
, X86::VANDNPDrm
, X86::VPANDNQZ128rm
, X86::VPANDNDZ128rm
},
6429 { X86::VANDNPSrr
, X86::VANDNPDrr
, X86::VPANDNQZ128rr
, X86::VPANDNDZ128rr
},
6430 { X86::VANDPSrm
, X86::VANDPDrm
, X86::VPANDQZ128rm
, X86::VPANDDZ128rm
},
6431 { X86::VANDPSrr
, X86::VANDPDrr
, X86::VPANDQZ128rr
, X86::VPANDDZ128rr
},
6432 { X86::VORPSrm
, X86::VORPDrm
, X86::VPORQZ128rm
, X86::VPORDZ128rm
},
6433 { X86::VORPSrr
, X86::VORPDrr
, X86::VPORQZ128rr
, X86::VPORDZ128rr
},
6434 { X86::VXORPSrm
, X86::VXORPDrm
, X86::VPXORQZ128rm
, X86::VPXORDZ128rm
},
6435 { X86::VXORPSrr
, X86::VXORPDrr
, X86::VPXORQZ128rr
, X86::VPXORDZ128rr
},
6436 { X86::VANDNPSYrm
, X86::VANDNPDYrm
, X86::VPANDNQZ256rm
, X86::VPANDNDZ256rm
},
6437 { X86::VANDNPSYrr
, X86::VANDNPDYrr
, X86::VPANDNQZ256rr
, X86::VPANDNDZ256rr
},
6438 { X86::VANDPSYrm
, X86::VANDPDYrm
, X86::VPANDQZ256rm
, X86::VPANDDZ256rm
},
6439 { X86::VANDPSYrr
, X86::VANDPDYrr
, X86::VPANDQZ256rr
, X86::VPANDDZ256rr
},
6440 { X86::VORPSYrm
, X86::VORPDYrm
, X86::VPORQZ256rm
, X86::VPORDZ256rm
},
6441 { X86::VORPSYrr
, X86::VORPDYrr
, X86::VPORQZ256rr
, X86::VPORDZ256rr
},
6442 { X86::VXORPSYrm
, X86::VXORPDYrm
, X86::VPXORQZ256rm
, X86::VPXORDZ256rm
},
6443 { X86::VXORPSYrr
, X86::VXORPDYrr
, X86::VPXORQZ256rr
, X86::VPXORDZ256rr
},
6446 // FIXME: Some shuffle and unpack instructions have equivalents in different
6447 // domains, but they require a bit more work than just switching opcodes.
6449 static const uint16_t *lookup(unsigned opcode
, unsigned domain
,
6450 ArrayRef
<uint16_t[3]> Table
) {
6451 for (const uint16_t (&Row
)[3] : Table
)
6452 if (Row
[domain
-1] == opcode
)
6457 static const uint16_t *lookupAVX512(unsigned opcode
, unsigned domain
,
6458 ArrayRef
<uint16_t[4]> Table
) {
6459 // If this is the integer domain make sure to check both integer columns.
6460 for (const uint16_t (&Row
)[4] : Table
)
6461 if (Row
[domain
-1] == opcode
|| (domain
== 3 && Row
[3] == opcode
))
6466 // Helper to attempt to widen/narrow blend masks.
6467 static bool AdjustBlendMask(unsigned OldMask
, unsigned OldWidth
,
6468 unsigned NewWidth
, unsigned *pNewMask
= nullptr) {
6469 assert(((OldWidth
% NewWidth
) == 0 || (NewWidth
% OldWidth
) == 0) &&
6470 "Illegal blend mask scale");
6471 unsigned NewMask
= 0;
6473 if ((OldWidth
% NewWidth
) == 0) {
6474 unsigned Scale
= OldWidth
/ NewWidth
;
6475 unsigned SubMask
= (1u << Scale
) - 1;
6476 for (unsigned i
= 0; i
!= NewWidth
; ++i
) {
6477 unsigned Sub
= (OldMask
>> (i
* Scale
)) & SubMask
;
6479 NewMask
|= (1u << i
);
6480 else if (Sub
!= 0x0)
6484 unsigned Scale
= NewWidth
/ OldWidth
;
6485 unsigned SubMask
= (1u << Scale
) - 1;
6486 for (unsigned i
= 0; i
!= OldWidth
; ++i
) {
6487 if (OldMask
& (1 << i
)) {
6488 NewMask
|= (SubMask
<< (i
* Scale
));
6494 *pNewMask
= NewMask
;
6498 uint16_t X86InstrInfo::getExecutionDomainCustom(const MachineInstr
&MI
) const {
6499 unsigned Opcode
= MI
.getOpcode();
6500 unsigned NumOperands
= MI
.getDesc().getNumOperands();
6502 auto GetBlendDomains
= [&](unsigned ImmWidth
, bool Is256
) {
6503 uint16_t validDomains
= 0;
6504 if (MI
.getOperand(NumOperands
- 1).isImm()) {
6505 unsigned Imm
= MI
.getOperand(NumOperands
- 1).getImm();
6506 if (AdjustBlendMask(Imm
, ImmWidth
, Is256
? 8 : 4))
6507 validDomains
|= 0x2; // PackedSingle
6508 if (AdjustBlendMask(Imm
, ImmWidth
, Is256
? 4 : 2))
6509 validDomains
|= 0x4; // PackedDouble
6510 if (!Is256
|| Subtarget
.hasAVX2())
6511 validDomains
|= 0x8; // PackedInt
6513 return validDomains
;
6517 case X86::BLENDPDrmi
:
6518 case X86::BLENDPDrri
:
6519 case X86::VBLENDPDrmi
:
6520 case X86::VBLENDPDrri
:
6521 return GetBlendDomains(2, false);
6522 case X86::VBLENDPDYrmi
:
6523 case X86::VBLENDPDYrri
:
6524 return GetBlendDomains(4, true);
6525 case X86::BLENDPSrmi
:
6526 case X86::BLENDPSrri
:
6527 case X86::VBLENDPSrmi
:
6528 case X86::VBLENDPSrri
:
6529 case X86::VPBLENDDrmi
:
6530 case X86::VPBLENDDrri
:
6531 return GetBlendDomains(4, false);
6532 case X86::VBLENDPSYrmi
:
6533 case X86::VBLENDPSYrri
:
6534 case X86::VPBLENDDYrmi
:
6535 case X86::VPBLENDDYrri
:
6536 return GetBlendDomains(8, true);
6537 case X86::PBLENDWrmi
:
6538 case X86::PBLENDWrri
:
6539 case X86::VPBLENDWrmi
:
6540 case X86::VPBLENDWrri
:
6541 // Treat VPBLENDWY as a 128-bit vector as it repeats the lo/hi masks.
6542 case X86::VPBLENDWYrmi
:
6543 case X86::VPBLENDWYrri
:
6544 return GetBlendDomains(8, false);
6545 case X86::VPANDDZ128rr
: case X86::VPANDDZ128rm
:
6546 case X86::VPANDDZ256rr
: case X86::VPANDDZ256rm
:
6547 case X86::VPANDQZ128rr
: case X86::VPANDQZ128rm
:
6548 case X86::VPANDQZ256rr
: case X86::VPANDQZ256rm
:
6549 case X86::VPANDNDZ128rr
: case X86::VPANDNDZ128rm
:
6550 case X86::VPANDNDZ256rr
: case X86::VPANDNDZ256rm
:
6551 case X86::VPANDNQZ128rr
: case X86::VPANDNQZ128rm
:
6552 case X86::VPANDNQZ256rr
: case X86::VPANDNQZ256rm
:
6553 case X86::VPORDZ128rr
: case X86::VPORDZ128rm
:
6554 case X86::VPORDZ256rr
: case X86::VPORDZ256rm
:
6555 case X86::VPORQZ128rr
: case X86::VPORQZ128rm
:
6556 case X86::VPORQZ256rr
: case X86::VPORQZ256rm
:
6557 case X86::VPXORDZ128rr
: case X86::VPXORDZ128rm
:
6558 case X86::VPXORDZ256rr
: case X86::VPXORDZ256rm
:
6559 case X86::VPXORQZ128rr
: case X86::VPXORQZ128rm
:
6560 case X86::VPXORQZ256rr
: case X86::VPXORQZ256rm
:
6561 // If we don't have DQI see if we can still switch from an EVEX integer
6562 // instruction to a VEX floating point instruction.
6563 if (Subtarget
.hasDQI())
6566 if (RI
.getEncodingValue(MI
.getOperand(0).getReg()) >= 16)
6568 if (RI
.getEncodingValue(MI
.getOperand(1).getReg()) >= 16)
6570 // Register forms will have 3 operands. Memory form will have more.
6571 if (NumOperands
== 3 &&
6572 RI
.getEncodingValue(MI
.getOperand(2).getReg()) >= 16)
6575 // All domains are valid.
6577 case X86::MOVHLPSrr
:
6578 // We can swap domains when both inputs are the same register.
6579 // FIXME: This doesn't catch all the cases we would like. If the input
6580 // register isn't KILLed by the instruction, the two address instruction
6581 // pass puts a COPY on one input. The other input uses the original
6582 // register. This prevents the same physical register from being used by
6584 if (MI
.getOperand(1).getReg() == MI
.getOperand(2).getReg() &&
6585 MI
.getOperand(0).getSubReg() == 0 &&
6586 MI
.getOperand(1).getSubReg() == 0 &&
6587 MI
.getOperand(2).getSubReg() == 0)
6590 case X86::SHUFPDrri
:
6596 bool X86InstrInfo::setExecutionDomainCustom(MachineInstr
&MI
,
6597 unsigned Domain
) const {
6598 assert(Domain
> 0 && Domain
< 4 && "Invalid execution domain");
6599 uint16_t dom
= (MI
.getDesc().TSFlags
>> X86II::SSEDomainShift
) & 3;
6600 assert(dom
&& "Not an SSE instruction");
6602 unsigned Opcode
= MI
.getOpcode();
6603 unsigned NumOperands
= MI
.getDesc().getNumOperands();
6605 auto SetBlendDomain
= [&](unsigned ImmWidth
, bool Is256
) {
6606 if (MI
.getOperand(NumOperands
- 1).isImm()) {
6607 unsigned Imm
= MI
.getOperand(NumOperands
- 1).getImm() & 255;
6608 Imm
= (ImmWidth
== 16 ? ((Imm
<< 8) | Imm
) : Imm
);
6609 unsigned NewImm
= Imm
;
6611 const uint16_t *table
= lookup(Opcode
, dom
, ReplaceableBlendInstrs
);
6613 table
= lookup(Opcode
, dom
, ReplaceableBlendAVX2Instrs
);
6615 if (Domain
== 1) { // PackedSingle
6616 AdjustBlendMask(Imm
, ImmWidth
, Is256
? 8 : 4, &NewImm
);
6617 } else if (Domain
== 2) { // PackedDouble
6618 AdjustBlendMask(Imm
, ImmWidth
, Is256
? 4 : 2, &NewImm
);
6619 } else if (Domain
== 3) { // PackedInt
6620 if (Subtarget
.hasAVX2()) {
6621 // If we are already VPBLENDW use that, else use VPBLENDD.
6622 if ((ImmWidth
/ (Is256
? 2 : 1)) != 8) {
6623 table
= lookup(Opcode
, dom
, ReplaceableBlendAVX2Instrs
);
6624 AdjustBlendMask(Imm
, ImmWidth
, Is256
? 8 : 4, &NewImm
);
6627 assert(!Is256
&& "128-bit vector expected");
6628 AdjustBlendMask(Imm
, ImmWidth
, 8, &NewImm
);
6632 assert(table
&& table
[Domain
- 1] && "Unknown domain op");
6633 MI
.setDesc(get(table
[Domain
- 1]));
6634 MI
.getOperand(NumOperands
- 1).setImm(NewImm
& 255);
6640 case X86::BLENDPDrmi
:
6641 case X86::BLENDPDrri
:
6642 case X86::VBLENDPDrmi
:
6643 case X86::VBLENDPDrri
:
6644 return SetBlendDomain(2, false);
6645 case X86::VBLENDPDYrmi
:
6646 case X86::VBLENDPDYrri
:
6647 return SetBlendDomain(4, true);
6648 case X86::BLENDPSrmi
:
6649 case X86::BLENDPSrri
:
6650 case X86::VBLENDPSrmi
:
6651 case X86::VBLENDPSrri
:
6652 case X86::VPBLENDDrmi
:
6653 case X86::VPBLENDDrri
:
6654 return SetBlendDomain(4, false);
6655 case X86::VBLENDPSYrmi
:
6656 case X86::VBLENDPSYrri
:
6657 case X86::VPBLENDDYrmi
:
6658 case X86::VPBLENDDYrri
:
6659 return SetBlendDomain(8, true);
6660 case X86::PBLENDWrmi
:
6661 case X86::PBLENDWrri
:
6662 case X86::VPBLENDWrmi
:
6663 case X86::VPBLENDWrri
:
6664 return SetBlendDomain(8, false);
6665 case X86::VPBLENDWYrmi
:
6666 case X86::VPBLENDWYrri
:
6667 return SetBlendDomain(16, true);
6668 case X86::VPANDDZ128rr
: case X86::VPANDDZ128rm
:
6669 case X86::VPANDDZ256rr
: case X86::VPANDDZ256rm
:
6670 case X86::VPANDQZ128rr
: case X86::VPANDQZ128rm
:
6671 case X86::VPANDQZ256rr
: case X86::VPANDQZ256rm
:
6672 case X86::VPANDNDZ128rr
: case X86::VPANDNDZ128rm
:
6673 case X86::VPANDNDZ256rr
: case X86::VPANDNDZ256rm
:
6674 case X86::VPANDNQZ128rr
: case X86::VPANDNQZ128rm
:
6675 case X86::VPANDNQZ256rr
: case X86::VPANDNQZ256rm
:
6676 case X86::VPORDZ128rr
: case X86::VPORDZ128rm
:
6677 case X86::VPORDZ256rr
: case X86::VPORDZ256rm
:
6678 case X86::VPORQZ128rr
: case X86::VPORQZ128rm
:
6679 case X86::VPORQZ256rr
: case X86::VPORQZ256rm
:
6680 case X86::VPXORDZ128rr
: case X86::VPXORDZ128rm
:
6681 case X86::VPXORDZ256rr
: case X86::VPXORDZ256rm
:
6682 case X86::VPXORQZ128rr
: case X86::VPXORQZ128rm
:
6683 case X86::VPXORQZ256rr
: case X86::VPXORQZ256rm
: {
6684 // Without DQI, convert EVEX instructions to VEX instructions.
6685 if (Subtarget
.hasDQI())
6688 const uint16_t *table
= lookupAVX512(MI
.getOpcode(), dom
,
6689 ReplaceableCustomAVX512LogicInstrs
);
6690 assert(table
&& "Instruction not found in table?");
6691 // Don't change integer Q instructions to D instructions and
6692 // use D intructions if we started with a PS instruction.
6693 if (Domain
== 3 && (dom
== 1 || table
[3] == MI
.getOpcode()))
6695 MI
.setDesc(get(table
[Domain
- 1]));
6698 case X86::UNPCKHPDrr
:
6699 case X86::MOVHLPSrr
:
6700 // We just need to commute the instruction which will switch the domains.
6701 if (Domain
!= dom
&& Domain
!= 3 &&
6702 MI
.getOperand(1).getReg() == MI
.getOperand(2).getReg() &&
6703 MI
.getOperand(0).getSubReg() == 0 &&
6704 MI
.getOperand(1).getSubReg() == 0 &&
6705 MI
.getOperand(2).getSubReg() == 0) {
6706 commuteInstruction(MI
, false);
6709 // We must always return true for MOVHLPSrr.
6710 if (Opcode
== X86::MOVHLPSrr
)
6713 case X86::SHUFPDrri
: {
6715 unsigned Imm
= MI
.getOperand(3).getImm();
6716 unsigned NewImm
= 0x44;
6717 if (Imm
& 1) NewImm
|= 0x0a;
6718 if (Imm
& 2) NewImm
|= 0xa0;
6719 MI
.getOperand(3).setImm(NewImm
);
6720 MI
.setDesc(get(X86::SHUFPSrri
));
6728 std::pair
<uint16_t, uint16_t>
6729 X86InstrInfo::getExecutionDomain(const MachineInstr
&MI
) const {
6730 uint16_t domain
= (MI
.getDesc().TSFlags
>> X86II::SSEDomainShift
) & 3;
6731 unsigned opcode
= MI
.getOpcode();
6732 uint16_t validDomains
= 0;
6734 // Attempt to match for custom instructions.
6735 validDomains
= getExecutionDomainCustom(MI
);
6737 return std::make_pair(domain
, validDomains
);
6739 if (lookup(opcode
, domain
, ReplaceableInstrs
)) {
6741 } else if (lookup(opcode
, domain
, ReplaceableInstrsAVX2
)) {
6742 validDomains
= Subtarget
.hasAVX2() ? 0xe : 0x6;
6743 } else if (lookup(opcode
, domain
, ReplaceableInstrsFP
)) {
6745 } else if (lookup(opcode
, domain
, ReplaceableInstrsAVX2InsertExtract
)) {
6746 // Insert/extract instructions should only effect domain if AVX2
6748 if (!Subtarget
.hasAVX2())
6749 return std::make_pair(0, 0);
6751 } else if (lookupAVX512(opcode
, domain
, ReplaceableInstrsAVX512
)) {
6753 } else if (Subtarget
.hasDQI() && lookupAVX512(opcode
, domain
,
6754 ReplaceableInstrsAVX512DQ
)) {
6756 } else if (Subtarget
.hasDQI()) {
6757 if (const uint16_t *table
= lookupAVX512(opcode
, domain
,
6758 ReplaceableInstrsAVX512DQMasked
)) {
6759 if (domain
== 1 || (domain
== 3 && table
[3] == opcode
))
6766 return std::make_pair(domain
, validDomains
);
6769 void X86InstrInfo::setExecutionDomain(MachineInstr
&MI
, unsigned Domain
) const {
6770 assert(Domain
>0 && Domain
<4 && "Invalid execution domain");
6771 uint16_t dom
= (MI
.getDesc().TSFlags
>> X86II::SSEDomainShift
) & 3;
6772 assert(dom
&& "Not an SSE instruction");
6774 // Attempt to match for custom instructions.
6775 if (setExecutionDomainCustom(MI
, Domain
))
6778 const uint16_t *table
= lookup(MI
.getOpcode(), dom
, ReplaceableInstrs
);
6779 if (!table
) { // try the other table
6780 assert((Subtarget
.hasAVX2() || Domain
< 3) &&
6781 "256-bit vector operations only available in AVX2");
6782 table
= lookup(MI
.getOpcode(), dom
, ReplaceableInstrsAVX2
);
6784 if (!table
) { // try the FP table
6785 table
= lookup(MI
.getOpcode(), dom
, ReplaceableInstrsFP
);
6786 assert((!table
|| Domain
< 3) &&
6787 "Can only select PackedSingle or PackedDouble");
6789 if (!table
) { // try the other table
6790 assert(Subtarget
.hasAVX2() &&
6791 "256-bit insert/extract only available in AVX2");
6792 table
= lookup(MI
.getOpcode(), dom
, ReplaceableInstrsAVX2InsertExtract
);
6794 if (!table
) { // try the AVX512 table
6795 assert(Subtarget
.hasAVX512() && "Requires AVX-512");
6796 table
= lookupAVX512(MI
.getOpcode(), dom
, ReplaceableInstrsAVX512
);
6797 // Don't change integer Q instructions to D instructions.
6798 if (table
&& Domain
== 3 && table
[3] == MI
.getOpcode())
6801 if (!table
) { // try the AVX512DQ table
6802 assert((Subtarget
.hasDQI() || Domain
>= 3) && "Requires AVX-512DQ");
6803 table
= lookupAVX512(MI
.getOpcode(), dom
, ReplaceableInstrsAVX512DQ
);
6804 // Don't change integer Q instructions to D instructions and
6805 // use D intructions if we started with a PS instruction.
6806 if (table
&& Domain
== 3 && (dom
== 1 || table
[3] == MI
.getOpcode()))
6809 if (!table
) { // try the AVX512DQMasked table
6810 assert((Subtarget
.hasDQI() || Domain
>= 3) && "Requires AVX-512DQ");
6811 table
= lookupAVX512(MI
.getOpcode(), dom
, ReplaceableInstrsAVX512DQMasked
);
6812 if (table
&& Domain
== 3 && (dom
== 1 || table
[3] == MI
.getOpcode()))
6815 assert(table
&& "Cannot change domain");
6816 MI
.setDesc(get(table
[Domain
- 1]));
6819 /// Return the noop instruction to use for a noop.
6820 void X86InstrInfo::getNoop(MCInst
&NopInst
) const {
6821 NopInst
.setOpcode(X86::NOOP
);
6824 bool X86InstrInfo::isHighLatencyDef(int opc
) const {
6826 default: return false;
6832 case X86::DIVSDrm_Int
:
6834 case X86::DIVSDrr_Int
:
6836 case X86::DIVSSrm_Int
:
6838 case X86::DIVSSrr_Int
:
6844 case X86::SQRTSDm_Int
:
6846 case X86::SQRTSDr_Int
:
6848 case X86::SQRTSSm_Int
:
6850 case X86::SQRTSSr_Int
:
6851 // AVX instructions with high latency
6854 case X86::VDIVPDYrm
:
6855 case X86::VDIVPDYrr
:
6858 case X86::VDIVPSYrm
:
6859 case X86::VDIVPSYrr
:
6861 case X86::VDIVSDrm_Int
:
6863 case X86::VDIVSDrr_Int
:
6865 case X86::VDIVSSrm_Int
:
6867 case X86::VDIVSSrr_Int
:
6870 case X86::VSQRTPDYm
:
6871 case X86::VSQRTPDYr
:
6874 case X86::VSQRTPSYm
:
6875 case X86::VSQRTPSYr
:
6877 case X86::VSQRTSDm_Int
:
6879 case X86::VSQRTSDr_Int
:
6881 case X86::VSQRTSSm_Int
:
6883 case X86::VSQRTSSr_Int
:
6884 // AVX512 instructions with high latency
6885 case X86::VDIVPDZ128rm
:
6886 case X86::VDIVPDZ128rmb
:
6887 case X86::VDIVPDZ128rmbk
:
6888 case X86::VDIVPDZ128rmbkz
:
6889 case X86::VDIVPDZ128rmk
:
6890 case X86::VDIVPDZ128rmkz
:
6891 case X86::VDIVPDZ128rr
:
6892 case X86::VDIVPDZ128rrk
:
6893 case X86::VDIVPDZ128rrkz
:
6894 case X86::VDIVPDZ256rm
:
6895 case X86::VDIVPDZ256rmb
:
6896 case X86::VDIVPDZ256rmbk
:
6897 case X86::VDIVPDZ256rmbkz
:
6898 case X86::VDIVPDZ256rmk
:
6899 case X86::VDIVPDZ256rmkz
:
6900 case X86::VDIVPDZ256rr
:
6901 case X86::VDIVPDZ256rrk
:
6902 case X86::VDIVPDZ256rrkz
:
6903 case X86::VDIVPDZrrb
:
6904 case X86::VDIVPDZrrbk
:
6905 case X86::VDIVPDZrrbkz
:
6906 case X86::VDIVPDZrm
:
6907 case X86::VDIVPDZrmb
:
6908 case X86::VDIVPDZrmbk
:
6909 case X86::VDIVPDZrmbkz
:
6910 case X86::VDIVPDZrmk
:
6911 case X86::VDIVPDZrmkz
:
6912 case X86::VDIVPDZrr
:
6913 case X86::VDIVPDZrrk
:
6914 case X86::VDIVPDZrrkz
:
6915 case X86::VDIVPSZ128rm
:
6916 case X86::VDIVPSZ128rmb
:
6917 case X86::VDIVPSZ128rmbk
:
6918 case X86::VDIVPSZ128rmbkz
:
6919 case X86::VDIVPSZ128rmk
:
6920 case X86::VDIVPSZ128rmkz
:
6921 case X86::VDIVPSZ128rr
:
6922 case X86::VDIVPSZ128rrk
:
6923 case X86::VDIVPSZ128rrkz
:
6924 case X86::VDIVPSZ256rm
:
6925 case X86::VDIVPSZ256rmb
:
6926 case X86::VDIVPSZ256rmbk
:
6927 case X86::VDIVPSZ256rmbkz
:
6928 case X86::VDIVPSZ256rmk
:
6929 case X86::VDIVPSZ256rmkz
:
6930 case X86::VDIVPSZ256rr
:
6931 case X86::VDIVPSZ256rrk
:
6932 case X86::VDIVPSZ256rrkz
:
6933 case X86::VDIVPSZrrb
:
6934 case X86::VDIVPSZrrbk
:
6935 case X86::VDIVPSZrrbkz
:
6936 case X86::VDIVPSZrm
:
6937 case X86::VDIVPSZrmb
:
6938 case X86::VDIVPSZrmbk
:
6939 case X86::VDIVPSZrmbkz
:
6940 case X86::VDIVPSZrmk
:
6941 case X86::VDIVPSZrmkz
:
6942 case X86::VDIVPSZrr
:
6943 case X86::VDIVPSZrrk
:
6944 case X86::VDIVPSZrrkz
:
6945 case X86::VDIVSDZrm
:
6946 case X86::VDIVSDZrr
:
6947 case X86::VDIVSDZrm_Int
:
6948 case X86::VDIVSDZrm_Intk
:
6949 case X86::VDIVSDZrm_Intkz
:
6950 case X86::VDIVSDZrr_Int
:
6951 case X86::VDIVSDZrr_Intk
:
6952 case X86::VDIVSDZrr_Intkz
:
6953 case X86::VDIVSDZrrb_Int
:
6954 case X86::VDIVSDZrrb_Intk
:
6955 case X86::VDIVSDZrrb_Intkz
:
6956 case X86::VDIVSSZrm
:
6957 case X86::VDIVSSZrr
:
6958 case X86::VDIVSSZrm_Int
:
6959 case X86::VDIVSSZrm_Intk
:
6960 case X86::VDIVSSZrm_Intkz
:
6961 case X86::VDIVSSZrr_Int
:
6962 case X86::VDIVSSZrr_Intk
:
6963 case X86::VDIVSSZrr_Intkz
:
6964 case X86::VDIVSSZrrb_Int
:
6965 case X86::VDIVSSZrrb_Intk
:
6966 case X86::VDIVSSZrrb_Intkz
:
6967 case X86::VSQRTPDZ128m
:
6968 case X86::VSQRTPDZ128mb
:
6969 case X86::VSQRTPDZ128mbk
:
6970 case X86::VSQRTPDZ128mbkz
:
6971 case X86::VSQRTPDZ128mk
:
6972 case X86::VSQRTPDZ128mkz
:
6973 case X86::VSQRTPDZ128r
:
6974 case X86::VSQRTPDZ128rk
:
6975 case X86::VSQRTPDZ128rkz
:
6976 case X86::VSQRTPDZ256m
:
6977 case X86::VSQRTPDZ256mb
:
6978 case X86::VSQRTPDZ256mbk
:
6979 case X86::VSQRTPDZ256mbkz
:
6980 case X86::VSQRTPDZ256mk
:
6981 case X86::VSQRTPDZ256mkz
:
6982 case X86::VSQRTPDZ256r
:
6983 case X86::VSQRTPDZ256rk
:
6984 case X86::VSQRTPDZ256rkz
:
6985 case X86::VSQRTPDZm
:
6986 case X86::VSQRTPDZmb
:
6987 case X86::VSQRTPDZmbk
:
6988 case X86::VSQRTPDZmbkz
:
6989 case X86::VSQRTPDZmk
:
6990 case X86::VSQRTPDZmkz
:
6991 case X86::VSQRTPDZr
:
6992 case X86::VSQRTPDZrb
:
6993 case X86::VSQRTPDZrbk
:
6994 case X86::VSQRTPDZrbkz
:
6995 case X86::VSQRTPDZrk
:
6996 case X86::VSQRTPDZrkz
:
6997 case X86::VSQRTPSZ128m
:
6998 case X86::VSQRTPSZ128mb
:
6999 case X86::VSQRTPSZ128mbk
:
7000 case X86::VSQRTPSZ128mbkz
:
7001 case X86::VSQRTPSZ128mk
:
7002 case X86::VSQRTPSZ128mkz
:
7003 case X86::VSQRTPSZ128r
:
7004 case X86::VSQRTPSZ128rk
:
7005 case X86::VSQRTPSZ128rkz
:
7006 case X86::VSQRTPSZ256m
:
7007 case X86::VSQRTPSZ256mb
:
7008 case X86::VSQRTPSZ256mbk
:
7009 case X86::VSQRTPSZ256mbkz
:
7010 case X86::VSQRTPSZ256mk
:
7011 case X86::VSQRTPSZ256mkz
:
7012 case X86::VSQRTPSZ256r
:
7013 case X86::VSQRTPSZ256rk
:
7014 case X86::VSQRTPSZ256rkz
:
7015 case X86::VSQRTPSZm
:
7016 case X86::VSQRTPSZmb
:
7017 case X86::VSQRTPSZmbk
:
7018 case X86::VSQRTPSZmbkz
:
7019 case X86::VSQRTPSZmk
:
7020 case X86::VSQRTPSZmkz
:
7021 case X86::VSQRTPSZr
:
7022 case X86::VSQRTPSZrb
:
7023 case X86::VSQRTPSZrbk
:
7024 case X86::VSQRTPSZrbkz
:
7025 case X86::VSQRTPSZrk
:
7026 case X86::VSQRTPSZrkz
:
7027 case X86::VSQRTSDZm
:
7028 case X86::VSQRTSDZm_Int
:
7029 case X86::VSQRTSDZm_Intk
:
7030 case X86::VSQRTSDZm_Intkz
:
7031 case X86::VSQRTSDZr
:
7032 case X86::VSQRTSDZr_Int
:
7033 case X86::VSQRTSDZr_Intk
:
7034 case X86::VSQRTSDZr_Intkz
:
7035 case X86::VSQRTSDZrb_Int
:
7036 case X86::VSQRTSDZrb_Intk
:
7037 case X86::VSQRTSDZrb_Intkz
:
7038 case X86::VSQRTSSZm
:
7039 case X86::VSQRTSSZm_Int
:
7040 case X86::VSQRTSSZm_Intk
:
7041 case X86::VSQRTSSZm_Intkz
:
7042 case X86::VSQRTSSZr
:
7043 case X86::VSQRTSSZr_Int
:
7044 case X86::VSQRTSSZr_Intk
:
7045 case X86::VSQRTSSZr_Intkz
:
7046 case X86::VSQRTSSZrb_Int
:
7047 case X86::VSQRTSSZrb_Intk
:
7048 case X86::VSQRTSSZrb_Intkz
:
7050 case X86::VGATHERDPDYrm
:
7051 case X86::VGATHERDPDZ128rm
:
7052 case X86::VGATHERDPDZ256rm
:
7053 case X86::VGATHERDPDZrm
:
7054 case X86::VGATHERDPDrm
:
7055 case X86::VGATHERDPSYrm
:
7056 case X86::VGATHERDPSZ128rm
:
7057 case X86::VGATHERDPSZ256rm
:
7058 case X86::VGATHERDPSZrm
:
7059 case X86::VGATHERDPSrm
:
7060 case X86::VGATHERPF0DPDm
:
7061 case X86::VGATHERPF0DPSm
:
7062 case X86::VGATHERPF0QPDm
:
7063 case X86::VGATHERPF0QPSm
:
7064 case X86::VGATHERPF1DPDm
:
7065 case X86::VGATHERPF1DPSm
:
7066 case X86::VGATHERPF1QPDm
:
7067 case X86::VGATHERPF1QPSm
:
7068 case X86::VGATHERQPDYrm
:
7069 case X86::VGATHERQPDZ128rm
:
7070 case X86::VGATHERQPDZ256rm
:
7071 case X86::VGATHERQPDZrm
:
7072 case X86::VGATHERQPDrm
:
7073 case X86::VGATHERQPSYrm
:
7074 case X86::VGATHERQPSZ128rm
:
7075 case X86::VGATHERQPSZ256rm
:
7076 case X86::VGATHERQPSZrm
:
7077 case X86::VGATHERQPSrm
:
7078 case X86::VPGATHERDDYrm
:
7079 case X86::VPGATHERDDZ128rm
:
7080 case X86::VPGATHERDDZ256rm
:
7081 case X86::VPGATHERDDZrm
:
7082 case X86::VPGATHERDDrm
:
7083 case X86::VPGATHERDQYrm
:
7084 case X86::VPGATHERDQZ128rm
:
7085 case X86::VPGATHERDQZ256rm
:
7086 case X86::VPGATHERDQZrm
:
7087 case X86::VPGATHERDQrm
:
7088 case X86::VPGATHERQDYrm
:
7089 case X86::VPGATHERQDZ128rm
:
7090 case X86::VPGATHERQDZ256rm
:
7091 case X86::VPGATHERQDZrm
:
7092 case X86::VPGATHERQDrm
:
7093 case X86::VPGATHERQQYrm
:
7094 case X86::VPGATHERQQZ128rm
:
7095 case X86::VPGATHERQQZ256rm
:
7096 case X86::VPGATHERQQZrm
:
7097 case X86::VPGATHERQQrm
:
7098 case X86::VSCATTERDPDZ128mr
:
7099 case X86::VSCATTERDPDZ256mr
:
7100 case X86::VSCATTERDPDZmr
:
7101 case X86::VSCATTERDPSZ128mr
:
7102 case X86::VSCATTERDPSZ256mr
:
7103 case X86::VSCATTERDPSZmr
:
7104 case X86::VSCATTERPF0DPDm
:
7105 case X86::VSCATTERPF0DPSm
:
7106 case X86::VSCATTERPF0QPDm
:
7107 case X86::VSCATTERPF0QPSm
:
7108 case X86::VSCATTERPF1DPDm
:
7109 case X86::VSCATTERPF1DPSm
:
7110 case X86::VSCATTERPF1QPDm
:
7111 case X86::VSCATTERPF1QPSm
:
7112 case X86::VSCATTERQPDZ128mr
:
7113 case X86::VSCATTERQPDZ256mr
:
7114 case X86::VSCATTERQPDZmr
:
7115 case X86::VSCATTERQPSZ128mr
:
7116 case X86::VSCATTERQPSZ256mr
:
7117 case X86::VSCATTERQPSZmr
:
7118 case X86::VPSCATTERDDZ128mr
:
7119 case X86::VPSCATTERDDZ256mr
:
7120 case X86::VPSCATTERDDZmr
:
7121 case X86::VPSCATTERDQZ128mr
:
7122 case X86::VPSCATTERDQZ256mr
:
7123 case X86::VPSCATTERDQZmr
:
7124 case X86::VPSCATTERQDZ128mr
:
7125 case X86::VPSCATTERQDZ256mr
:
7126 case X86::VPSCATTERQDZmr
:
7127 case X86::VPSCATTERQQZ128mr
:
7128 case X86::VPSCATTERQQZ256mr
:
7129 case X86::VPSCATTERQQZmr
:
7134 bool X86InstrInfo::hasHighOperandLatency(const TargetSchedModel
&SchedModel
,
7135 const MachineRegisterInfo
*MRI
,
7136 const MachineInstr
&DefMI
,
7138 const MachineInstr
&UseMI
,
7139 unsigned UseIdx
) const {
7140 return isHighLatencyDef(DefMI
.getOpcode());
7143 bool X86InstrInfo::hasReassociableOperands(const MachineInstr
&Inst
,
7144 const MachineBasicBlock
*MBB
) const {
7145 assert((Inst
.getNumOperands() == 3 || Inst
.getNumOperands() == 4) &&
7146 "Reassociation needs binary operators");
7148 // Integer binary math/logic instructions have a third source operand:
7149 // the EFLAGS register. That operand must be both defined here and never
7150 // used; ie, it must be dead. If the EFLAGS operand is live, then we can
7151 // not change anything because rearranging the operands could affect other
7152 // instructions that depend on the exact status flags (zero, sign, etc.)
7153 // that are set by using these particular operands with this operation.
7154 if (Inst
.getNumOperands() == 4) {
7155 assert(Inst
.getOperand(3).isReg() &&
7156 Inst
.getOperand(3).getReg() == X86::EFLAGS
&&
7157 "Unexpected operand in reassociable instruction");
7158 if (!Inst
.getOperand(3).isDead())
7162 return TargetInstrInfo::hasReassociableOperands(Inst
, MBB
);
7165 // TODO: There are many more machine instruction opcodes to match:
7166 // 1. Other data types (integer, vectors)
7167 // 2. Other math / logic operations (xor, or)
7168 // 3. Other forms of the same operation (intrinsics and other variants)
7169 bool X86InstrInfo::isAssociativeAndCommutative(const MachineInstr
&Inst
) const {
7170 switch (Inst
.getOpcode()) {
7215 case X86::VPANDDZ128rr
:
7216 case X86::VPANDDZ256rr
:
7217 case X86::VPANDDZrr
:
7218 case X86::VPANDQZ128rr
:
7219 case X86::VPANDQZ256rr
:
7220 case X86::VPANDQZrr
:
7223 case X86::VPORDZ128rr
:
7224 case X86::VPORDZ256rr
:
7226 case X86::VPORQZ128rr
:
7227 case X86::VPORQZ256rr
:
7231 case X86::VPXORDZ128rr
:
7232 case X86::VPXORDZ256rr
:
7233 case X86::VPXORDZrr
:
7234 case X86::VPXORQZ128rr
:
7235 case X86::VPXORQZ256rr
:
7236 case X86::VPXORQZrr
:
7239 case X86::VANDPDYrr
:
7240 case X86::VANDPSYrr
:
7241 case X86::VANDPDZ128rr
:
7242 case X86::VANDPSZ128rr
:
7243 case X86::VANDPDZ256rr
:
7244 case X86::VANDPSZ256rr
:
7245 case X86::VANDPDZrr
:
7246 case X86::VANDPSZrr
:
7251 case X86::VORPDZ128rr
:
7252 case X86::VORPSZ128rr
:
7253 case X86::VORPDZ256rr
:
7254 case X86::VORPSZ256rr
:
7259 case X86::VXORPDYrr
:
7260 case X86::VXORPSYrr
:
7261 case X86::VXORPDZ128rr
:
7262 case X86::VXORPSZ128rr
:
7263 case X86::VXORPDZ256rr
:
7264 case X86::VXORPSZ256rr
:
7265 case X86::VXORPDZrr
:
7266 case X86::VXORPSZrr
:
7287 case X86::VPADDBYrr
:
7288 case X86::VPADDWYrr
:
7289 case X86::VPADDDYrr
:
7290 case X86::VPADDQYrr
:
7291 case X86::VPADDBZ128rr
:
7292 case X86::VPADDWZ128rr
:
7293 case X86::VPADDDZ128rr
:
7294 case X86::VPADDQZ128rr
:
7295 case X86::VPADDBZ256rr
:
7296 case X86::VPADDWZ256rr
:
7297 case X86::VPADDDZ256rr
:
7298 case X86::VPADDQZ256rr
:
7299 case X86::VPADDBZrr
:
7300 case X86::VPADDWZrr
:
7301 case X86::VPADDDZrr
:
7302 case X86::VPADDQZrr
:
7303 case X86::VPMULLWrr
:
7304 case X86::VPMULLWYrr
:
7305 case X86::VPMULLWZ128rr
:
7306 case X86::VPMULLWZ256rr
:
7307 case X86::VPMULLWZrr
:
7308 case X86::VPMULLDrr
:
7309 case X86::VPMULLDYrr
:
7310 case X86::VPMULLDZ128rr
:
7311 case X86::VPMULLDZ256rr
:
7312 case X86::VPMULLDZrr
:
7313 case X86::VPMULLQZ128rr
:
7314 case X86::VPMULLQZ256rr
:
7315 case X86::VPMULLQZrr
:
7316 case X86::VPMAXSBrr
:
7317 case X86::VPMAXSBYrr
:
7318 case X86::VPMAXSBZ128rr
:
7319 case X86::VPMAXSBZ256rr
:
7320 case X86::VPMAXSBZrr
:
7321 case X86::VPMAXSDrr
:
7322 case X86::VPMAXSDYrr
:
7323 case X86::VPMAXSDZ128rr
:
7324 case X86::VPMAXSDZ256rr
:
7325 case X86::VPMAXSDZrr
:
7326 case X86::VPMAXSQZ128rr
:
7327 case X86::VPMAXSQZ256rr
:
7328 case X86::VPMAXSQZrr
:
7329 case X86::VPMAXSWrr
:
7330 case X86::VPMAXSWYrr
:
7331 case X86::VPMAXSWZ128rr
:
7332 case X86::VPMAXSWZ256rr
:
7333 case X86::VPMAXSWZrr
:
7334 case X86::VPMAXUBrr
:
7335 case X86::VPMAXUBYrr
:
7336 case X86::VPMAXUBZ128rr
:
7337 case X86::VPMAXUBZ256rr
:
7338 case X86::VPMAXUBZrr
:
7339 case X86::VPMAXUDrr
:
7340 case X86::VPMAXUDYrr
:
7341 case X86::VPMAXUDZ128rr
:
7342 case X86::VPMAXUDZ256rr
:
7343 case X86::VPMAXUDZrr
:
7344 case X86::VPMAXUQZ128rr
:
7345 case X86::VPMAXUQZ256rr
:
7346 case X86::VPMAXUQZrr
:
7347 case X86::VPMAXUWrr
:
7348 case X86::VPMAXUWYrr
:
7349 case X86::VPMAXUWZ128rr
:
7350 case X86::VPMAXUWZ256rr
:
7351 case X86::VPMAXUWZrr
:
7352 case X86::VPMINSBrr
:
7353 case X86::VPMINSBYrr
:
7354 case X86::VPMINSBZ128rr
:
7355 case X86::VPMINSBZ256rr
:
7356 case X86::VPMINSBZrr
:
7357 case X86::VPMINSDrr
:
7358 case X86::VPMINSDYrr
:
7359 case X86::VPMINSDZ128rr
:
7360 case X86::VPMINSDZ256rr
:
7361 case X86::VPMINSDZrr
:
7362 case X86::VPMINSQZ128rr
:
7363 case X86::VPMINSQZ256rr
:
7364 case X86::VPMINSQZrr
:
7365 case X86::VPMINSWrr
:
7366 case X86::VPMINSWYrr
:
7367 case X86::VPMINSWZ128rr
:
7368 case X86::VPMINSWZ256rr
:
7369 case X86::VPMINSWZrr
:
7370 case X86::VPMINUBrr
:
7371 case X86::VPMINUBYrr
:
7372 case X86::VPMINUBZ128rr
:
7373 case X86::VPMINUBZ256rr
:
7374 case X86::VPMINUBZrr
:
7375 case X86::VPMINUDrr
:
7376 case X86::VPMINUDYrr
:
7377 case X86::VPMINUDZ128rr
:
7378 case X86::VPMINUDZ256rr
:
7379 case X86::VPMINUDZrr
:
7380 case X86::VPMINUQZ128rr
:
7381 case X86::VPMINUQZ256rr
:
7382 case X86::VPMINUQZrr
:
7383 case X86::VPMINUWrr
:
7384 case X86::VPMINUWYrr
:
7385 case X86::VPMINUWZ128rr
:
7386 case X86::VPMINUWZ256rr
:
7387 case X86::VPMINUWZrr
:
7388 // Normal min/max instructions are not commutative because of NaN and signed
7389 // zero semantics, but these are. Thus, there's no need to check for global
7390 // relaxed math; the instructions themselves have the properties we need.
7399 case X86::VMAXCPDrr
:
7400 case X86::VMAXCPSrr
:
7401 case X86::VMAXCPDYrr
:
7402 case X86::VMAXCPSYrr
:
7403 case X86::VMAXCPDZ128rr
:
7404 case X86::VMAXCPSZ128rr
:
7405 case X86::VMAXCPDZ256rr
:
7406 case X86::VMAXCPSZ256rr
:
7407 case X86::VMAXCPDZrr
:
7408 case X86::VMAXCPSZrr
:
7409 case X86::VMAXCSDrr
:
7410 case X86::VMAXCSSrr
:
7411 case X86::VMAXCSDZrr
:
7412 case X86::VMAXCSSZrr
:
7413 case X86::VMINCPDrr
:
7414 case X86::VMINCPSrr
:
7415 case X86::VMINCPDYrr
:
7416 case X86::VMINCPSYrr
:
7417 case X86::VMINCPDZ128rr
:
7418 case X86::VMINCPSZ128rr
:
7419 case X86::VMINCPDZ256rr
:
7420 case X86::VMINCPSZ256rr
:
7421 case X86::VMINCPDZrr
:
7422 case X86::VMINCPSZrr
:
7423 case X86::VMINCSDrr
:
7424 case X86::VMINCSSrr
:
7425 case X86::VMINCSDZrr
:
7426 case X86::VMINCSSZrr
:
7438 case X86::VADDPDYrr
:
7439 case X86::VADDPSYrr
:
7440 case X86::VADDPDZ128rr
:
7441 case X86::VADDPSZ128rr
:
7442 case X86::VADDPDZ256rr
:
7443 case X86::VADDPSZ256rr
:
7444 case X86::VADDPDZrr
:
7445 case X86::VADDPSZrr
:
7448 case X86::VADDSDZrr
:
7449 case X86::VADDSSZrr
:
7452 case X86::VMULPDYrr
:
7453 case X86::VMULPSYrr
:
7454 case X86::VMULPDZ128rr
:
7455 case X86::VMULPSZ128rr
:
7456 case X86::VMULPDZ256rr
:
7457 case X86::VMULPSZ256rr
:
7458 case X86::VMULPDZrr
:
7459 case X86::VMULPSZrr
:
7462 case X86::VMULSDZrr
:
7463 case X86::VMULSSZrr
:
7464 return Inst
.getParent()->getParent()->getTarget().Options
.UnsafeFPMath
;
7470 Optional
<ParamLoadedValue
>
7471 X86InstrInfo::describeLoadedValue(const MachineInstr
&MI
) const {
7472 const MachineOperand
*Op
= nullptr;
7473 DIExpression
*Expr
= nullptr;
7475 switch (MI
.getOpcode()) {
7478 case X86::LEA64_32r
: {
7479 // Operand 4 could be global address. For now we do not support
7481 if (!MI
.getOperand(4).isImm() || !MI
.getOperand(2).isImm())
7484 const MachineOperand
&Op1
= MI
.getOperand(1);
7485 const MachineOperand
&Op2
= MI
.getOperand(3);
7486 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
7487 assert(Op2
.isReg() && (Op2
.getReg() == X86::NoRegister
||
7488 Register::isPhysicalRegister(Op2
.getReg())));
7490 // Omit situations like:
7491 // %rsi = lea %rsi, 4, ...
7492 if ((Op1
.isReg() && Op1
.getReg() == MI
.getOperand(0).getReg()) ||
7493 Op2
.getReg() == MI
.getOperand(0).getReg())
7495 else if ((Op1
.isReg() && Op1
.getReg() != X86::NoRegister
&&
7496 TRI
->regsOverlap(Op1
.getReg(), MI
.getOperand(0).getReg())) ||
7497 (Op2
.getReg() != X86::NoRegister
&&
7498 TRI
->regsOverlap(Op2
.getReg(), MI
.getOperand(0).getReg())))
7501 int64_t Coef
= MI
.getOperand(2).getImm();
7502 int64_t Offset
= MI
.getOperand(4).getImm();
7503 SmallVector
<uint64_t, 8> Ops
;
7505 if ((Op1
.isReg() && Op1
.getReg() != X86::NoRegister
)) {
7507 } else if (Op1
.isFI())
7510 if (Op
&& Op
->isReg() && Op
->getReg() == Op2
.getReg() && Coef
> 0) {
7511 Ops
.push_back(dwarf::DW_OP_constu
);
7512 Ops
.push_back(Coef
+ 1);
7513 Ops
.push_back(dwarf::DW_OP_mul
);
7515 if (Op
&& Op2
.getReg() != X86::NoRegister
) {
7516 int dwarfReg
= TRI
->getDwarfRegNum(Op2
.getReg(), false);
7519 else if (dwarfReg
< 32) {
7520 Ops
.push_back(dwarf::DW_OP_breg0
+ dwarfReg
);
7523 Ops
.push_back(dwarf::DW_OP_bregx
);
7524 Ops
.push_back(dwarfReg
);
7528 assert(Op2
.getReg() != X86::NoRegister
);
7533 assert(Op2
.getReg() != X86::NoRegister
);
7534 Ops
.push_back(dwarf::DW_OP_constu
);
7535 Ops
.push_back(Coef
);
7536 Ops
.push_back(dwarf::DW_OP_mul
);
7539 if (((Op1
.isReg() && Op1
.getReg() != X86::NoRegister
) || Op1
.isFI()) &&
7540 Op2
.getReg() != X86::NoRegister
) {
7541 Ops
.push_back(dwarf::DW_OP_plus
);
7545 DIExpression::appendOffset(Ops
, Offset
);
7546 Expr
= DIExpression::get(MI
.getMF()->getFunction().getContext(), Ops
);
7548 return ParamLoadedValue(Op
, Expr
);;
7551 return TargetInstrInfo::describeLoadedValue(MI
);
7555 /// This is an architecture-specific helper function of reassociateOps.
7556 /// Set special operand attributes for new instructions after reassociation.
7557 void X86InstrInfo::setSpecialOperandAttr(MachineInstr
&OldMI1
,
7558 MachineInstr
&OldMI2
,
7559 MachineInstr
&NewMI1
,
7560 MachineInstr
&NewMI2
) const {
7561 // Integer instructions define an implicit EFLAGS source register operand as
7562 // the third source (fourth total) operand.
7563 if (OldMI1
.getNumOperands() != 4 || OldMI2
.getNumOperands() != 4)
7566 assert(NewMI1
.getNumOperands() == 4 && NewMI2
.getNumOperands() == 4 &&
7567 "Unexpected instruction type for reassociation");
7569 MachineOperand
&OldOp1
= OldMI1
.getOperand(3);
7570 MachineOperand
&OldOp2
= OldMI2
.getOperand(3);
7571 MachineOperand
&NewOp1
= NewMI1
.getOperand(3);
7572 MachineOperand
&NewOp2
= NewMI2
.getOperand(3);
7574 assert(OldOp1
.isReg() && OldOp1
.getReg() == X86::EFLAGS
&& OldOp1
.isDead() &&
7575 "Must have dead EFLAGS operand in reassociable instruction");
7576 assert(OldOp2
.isReg() && OldOp2
.getReg() == X86::EFLAGS
&& OldOp2
.isDead() &&
7577 "Must have dead EFLAGS operand in reassociable instruction");
7582 assert(NewOp1
.isReg() && NewOp1
.getReg() == X86::EFLAGS
&&
7583 "Unexpected operand in reassociable instruction");
7584 assert(NewOp2
.isReg() && NewOp2
.getReg() == X86::EFLAGS
&&
7585 "Unexpected operand in reassociable instruction");
7587 // Mark the new EFLAGS operands as dead to be helpful to subsequent iterations
7588 // of this pass or other passes. The EFLAGS operands must be dead in these new
7589 // instructions because the EFLAGS operands in the original instructions must
7590 // be dead in order for reassociation to occur.
7595 std::pair
<unsigned, unsigned>
7596 X86InstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF
) const {
7597 return std::make_pair(TF
, 0u);
7600 ArrayRef
<std::pair
<unsigned, const char *>>
7601 X86InstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
7602 using namespace X86II
;
7603 static const std::pair
<unsigned, const char *> TargetFlags
[] = {
7604 {MO_GOT_ABSOLUTE_ADDRESS
, "x86-got-absolute-address"},
7605 {MO_PIC_BASE_OFFSET
, "x86-pic-base-offset"},
7606 {MO_GOT
, "x86-got"},
7607 {MO_GOTOFF
, "x86-gotoff"},
7608 {MO_GOTPCREL
, "x86-gotpcrel"},
7609 {MO_PLT
, "x86-plt"},
7610 {MO_TLSGD
, "x86-tlsgd"},
7611 {MO_TLSLD
, "x86-tlsld"},
7612 {MO_TLSLDM
, "x86-tlsldm"},
7613 {MO_GOTTPOFF
, "x86-gottpoff"},
7614 {MO_INDNTPOFF
, "x86-indntpoff"},
7615 {MO_TPOFF
, "x86-tpoff"},
7616 {MO_DTPOFF
, "x86-dtpoff"},
7617 {MO_NTPOFF
, "x86-ntpoff"},
7618 {MO_GOTNTPOFF
, "x86-gotntpoff"},
7619 {MO_DLLIMPORT
, "x86-dllimport"},
7620 {MO_DARWIN_NONLAZY
, "x86-darwin-nonlazy"},
7621 {MO_DARWIN_NONLAZY_PIC_BASE
, "x86-darwin-nonlazy-pic-base"},
7622 {MO_TLVP
, "x86-tlvp"},
7623 {MO_TLVP_PIC_BASE
, "x86-tlvp-pic-base"},
7624 {MO_SECREL
, "x86-secrel"},
7625 {MO_COFFSTUB
, "x86-coffstub"}};
7626 return makeArrayRef(TargetFlags
);
7630 /// Create Global Base Reg pass. This initializes the PIC
7631 /// global base register for x86-32.
7632 struct CGBR
: public MachineFunctionPass
{
7634 CGBR() : MachineFunctionPass(ID
) {}
7636 bool runOnMachineFunction(MachineFunction
&MF
) override
{
7637 const X86TargetMachine
*TM
=
7638 static_cast<const X86TargetMachine
*>(&MF
.getTarget());
7639 const X86Subtarget
&STI
= MF
.getSubtarget
<X86Subtarget
>();
7641 // Don't do anything in the 64-bit small and kernel code models. They use
7642 // RIP-relative addressing for everything.
7643 if (STI
.is64Bit() && (TM
->getCodeModel() == CodeModel::Small
||
7644 TM
->getCodeModel() == CodeModel::Kernel
))
7647 // Only emit a global base reg in PIC mode.
7648 if (!TM
->isPositionIndependent())
7651 X86MachineFunctionInfo
*X86FI
= MF
.getInfo
<X86MachineFunctionInfo
>();
7652 unsigned GlobalBaseReg
= X86FI
->getGlobalBaseReg();
7654 // If we didn't need a GlobalBaseReg, don't insert code.
7655 if (GlobalBaseReg
== 0)
7658 // Insert the set of GlobalBaseReg into the first MBB of the function
7659 MachineBasicBlock
&FirstMBB
= MF
.front();
7660 MachineBasicBlock::iterator MBBI
= FirstMBB
.begin();
7661 DebugLoc DL
= FirstMBB
.findDebugLoc(MBBI
);
7662 MachineRegisterInfo
&RegInfo
= MF
.getRegInfo();
7663 const X86InstrInfo
*TII
= STI
.getInstrInfo();
7666 if (STI
.isPICStyleGOT())
7667 PC
= RegInfo
.createVirtualRegister(&X86::GR32RegClass
);
7671 if (STI
.is64Bit()) {
7672 if (TM
->getCodeModel() == CodeModel::Medium
) {
7673 // In the medium code model, use a RIP-relative LEA to materialize the
7675 BuildMI(FirstMBB
, MBBI
, DL
, TII
->get(X86::LEA64r
), PC
)
7679 .addExternalSymbol("_GLOBAL_OFFSET_TABLE_")
7681 } else if (TM
->getCodeModel() == CodeModel::Large
) {
7682 // In the large code model, we are aiming for this code, though the
7683 // register allocation may vary:
7684 // leaq .LN$pb(%rip), %rax
7685 // movq $_GLOBAL_OFFSET_TABLE_ - .LN$pb, %rcx
7687 // RAX now holds address of _GLOBAL_OFFSET_TABLE_.
7688 Register PBReg
= RegInfo
.createVirtualRegister(&X86::GR64RegClass
);
7689 Register GOTReg
= RegInfo
.createVirtualRegister(&X86::GR64RegClass
);
7690 BuildMI(FirstMBB
, MBBI
, DL
, TII
->get(X86::LEA64r
), PBReg
)
7694 .addSym(MF
.getPICBaseSymbol())
7696 std::prev(MBBI
)->setPreInstrSymbol(MF
, MF
.getPICBaseSymbol());
7697 BuildMI(FirstMBB
, MBBI
, DL
, TII
->get(X86::MOV64ri
), GOTReg
)
7698 .addExternalSymbol("_GLOBAL_OFFSET_TABLE_",
7699 X86II::MO_PIC_BASE_OFFSET
);
7700 BuildMI(FirstMBB
, MBBI
, DL
, TII
->get(X86::ADD64rr
), PC
)
7701 .addReg(PBReg
, RegState::Kill
)
7702 .addReg(GOTReg
, RegState::Kill
);
7704 llvm_unreachable("unexpected code model");
7707 // Operand of MovePCtoStack is completely ignored by asm printer. It's
7708 // only used in JIT code emission as displacement to pc.
7709 BuildMI(FirstMBB
, MBBI
, DL
, TII
->get(X86::MOVPC32r
), PC
).addImm(0);
7711 // If we're using vanilla 'GOT' PIC style, we should use relative
7712 // addressing not to pc, but to _GLOBAL_OFFSET_TABLE_ external.
7713 if (STI
.isPICStyleGOT()) {
7714 // Generate addl $__GLOBAL_OFFSET_TABLE_ + [.-piclabel],
7716 BuildMI(FirstMBB
, MBBI
, DL
, TII
->get(X86::ADD32ri
), GlobalBaseReg
)
7718 .addExternalSymbol("_GLOBAL_OFFSET_TABLE_",
7719 X86II::MO_GOT_ABSOLUTE_ADDRESS
);
7726 StringRef
getPassName() const override
{
7727 return "X86 PIC Global Base Reg Initialization";
7730 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
7731 AU
.setPreservesCFG();
7732 MachineFunctionPass::getAnalysisUsage(AU
);
7739 llvm::createX86GlobalBaseRegPass() { return new CGBR(); }
7742 struct LDTLSCleanup
: public MachineFunctionPass
{
7744 LDTLSCleanup() : MachineFunctionPass(ID
) {}
7746 bool runOnMachineFunction(MachineFunction
&MF
) override
{
7747 if (skipFunction(MF
.getFunction()))
7750 X86MachineFunctionInfo
*MFI
= MF
.getInfo
<X86MachineFunctionInfo
>();
7751 if (MFI
->getNumLocalDynamicTLSAccesses() < 2) {
7752 // No point folding accesses if there isn't at least two.
7756 MachineDominatorTree
*DT
= &getAnalysis
<MachineDominatorTree
>();
7757 return VisitNode(DT
->getRootNode(), 0);
7760 // Visit the dominator subtree rooted at Node in pre-order.
7761 // If TLSBaseAddrReg is non-null, then use that to replace any
7762 // TLS_base_addr instructions. Otherwise, create the register
7763 // when the first such instruction is seen, and then use it
7764 // as we encounter more instructions.
7765 bool VisitNode(MachineDomTreeNode
*Node
, unsigned TLSBaseAddrReg
) {
7766 MachineBasicBlock
*BB
= Node
->getBlock();
7767 bool Changed
= false;
7769 // Traverse the current block.
7770 for (MachineBasicBlock::iterator I
= BB
->begin(), E
= BB
->end(); I
!= E
;
7772 switch (I
->getOpcode()) {
7773 case X86::TLS_base_addr32
:
7774 case X86::TLS_base_addr64
:
7776 I
= ReplaceTLSBaseAddrCall(*I
, TLSBaseAddrReg
);
7778 I
= SetRegister(*I
, &TLSBaseAddrReg
);
7786 // Visit the children of this block in the dominator tree.
7787 for (MachineDomTreeNode::iterator I
= Node
->begin(), E
= Node
->end();
7789 Changed
|= VisitNode(*I
, TLSBaseAddrReg
);
7795 // Replace the TLS_base_addr instruction I with a copy from
7796 // TLSBaseAddrReg, returning the new instruction.
7797 MachineInstr
*ReplaceTLSBaseAddrCall(MachineInstr
&I
,
7798 unsigned TLSBaseAddrReg
) {
7799 MachineFunction
*MF
= I
.getParent()->getParent();
7800 const X86Subtarget
&STI
= MF
->getSubtarget
<X86Subtarget
>();
7801 const bool is64Bit
= STI
.is64Bit();
7802 const X86InstrInfo
*TII
= STI
.getInstrInfo();
7804 // Insert a Copy from TLSBaseAddrReg to RAX/EAX.
7805 MachineInstr
*Copy
=
7806 BuildMI(*I
.getParent(), I
, I
.getDebugLoc(),
7807 TII
->get(TargetOpcode::COPY
), is64Bit
? X86::RAX
: X86::EAX
)
7808 .addReg(TLSBaseAddrReg
);
7810 // Erase the TLS_base_addr instruction.
7811 I
.eraseFromParent();
7816 // Create a virtual register in *TLSBaseAddrReg, and populate it by
7817 // inserting a copy instruction after I. Returns the new instruction.
7818 MachineInstr
*SetRegister(MachineInstr
&I
, unsigned *TLSBaseAddrReg
) {
7819 MachineFunction
*MF
= I
.getParent()->getParent();
7820 const X86Subtarget
&STI
= MF
->getSubtarget
<X86Subtarget
>();
7821 const bool is64Bit
= STI
.is64Bit();
7822 const X86InstrInfo
*TII
= STI
.getInstrInfo();
7824 // Create a virtual register for the TLS base address.
7825 MachineRegisterInfo
&RegInfo
= MF
->getRegInfo();
7826 *TLSBaseAddrReg
= RegInfo
.createVirtualRegister(is64Bit
7827 ? &X86::GR64RegClass
7828 : &X86::GR32RegClass
);
7830 // Insert a copy from RAX/EAX to TLSBaseAddrReg.
7831 MachineInstr
*Next
= I
.getNextNode();
7832 MachineInstr
*Copy
=
7833 BuildMI(*I
.getParent(), Next
, I
.getDebugLoc(),
7834 TII
->get(TargetOpcode::COPY
), *TLSBaseAddrReg
)
7835 .addReg(is64Bit
? X86::RAX
: X86::EAX
);
7840 StringRef
getPassName() const override
{
7841 return "Local Dynamic TLS Access Clean-up";
7844 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
7845 AU
.setPreservesCFG();
7846 AU
.addRequired
<MachineDominatorTree
>();
7847 MachineFunctionPass::getAnalysisUsage(AU
);
7852 char LDTLSCleanup::ID
= 0;
7854 llvm::createCleanupLocalDynamicTLSPass() { return new LDTLSCleanup(); }
7856 /// Constants defining how certain sequences should be outlined.
7858 /// \p MachineOutlinerDefault implies that the function is called with a call
7859 /// instruction, and a return must be emitted for the outlined function frame.
7863 /// I1 OUTLINED_FUNCTION:
7864 /// I2 --> call OUTLINED_FUNCTION I1
7869 /// * Call construction overhead: 1 (call instruction)
7870 /// * Frame construction overhead: 1 (return instruction)
7872 /// \p MachineOutlinerTailCall implies that the function is being tail called.
7873 /// A jump is emitted instead of a call, and the return is already present in
7874 /// the outlined sequence. That is,
7876 /// I1 OUTLINED_FUNCTION:
7877 /// I2 --> jmp OUTLINED_FUNCTION I1
7881 /// * Call construction overhead: 1 (jump instruction)
7882 /// * Frame construction overhead: 0 (don't need to return)
7884 enum MachineOutlinerClass
{
7885 MachineOutlinerDefault
,
7886 MachineOutlinerTailCall
7889 outliner::OutlinedFunction
X86InstrInfo::getOutliningCandidateInfo(
7890 std::vector
<outliner::Candidate
> &RepeatedSequenceLocs
) const {
7891 unsigned SequenceSize
=
7892 std::accumulate(RepeatedSequenceLocs
[0].front(),
7893 std::next(RepeatedSequenceLocs
[0].back()), 0,
7894 [](unsigned Sum
, const MachineInstr
&MI
) {
7895 // FIXME: x86 doesn't implement getInstSizeInBytes, so
7896 // we can't tell the cost. Just assume each instruction
7898 if (MI
.isDebugInstr() || MI
.isKill())
7903 // FIXME: Use real size in bytes for call and ret instructions.
7904 if (RepeatedSequenceLocs
[0].back()->isTerminator()) {
7905 for (outliner::Candidate
&C
: RepeatedSequenceLocs
)
7906 C
.setCallInfo(MachineOutlinerTailCall
, 1);
7908 return outliner::OutlinedFunction(RepeatedSequenceLocs
, SequenceSize
,
7909 0, // Number of bytes to emit frame.
7910 MachineOutlinerTailCall
// Type of frame.
7914 for (outliner::Candidate
&C
: RepeatedSequenceLocs
)
7915 C
.setCallInfo(MachineOutlinerDefault
, 1);
7917 return outliner::OutlinedFunction(RepeatedSequenceLocs
, SequenceSize
, 1,
7918 MachineOutlinerDefault
);
7921 bool X86InstrInfo::isFunctionSafeToOutlineFrom(MachineFunction
&MF
,
7922 bool OutlineFromLinkOnceODRs
) const {
7923 const Function
&F
= MF
.getFunction();
7925 // Does the function use a red zone? If it does, then we can't risk messing
7927 if (Subtarget
.getFrameLowering()->has128ByteRedZone(MF
)) {
7928 // It could have a red zone. If it does, then we don't want to touch it.
7929 const X86MachineFunctionInfo
*X86FI
= MF
.getInfo
<X86MachineFunctionInfo
>();
7930 if (!X86FI
|| X86FI
->getUsesRedZone())
7934 // If we *don't* want to outline from things that could potentially be deduped
7935 // then return false.
7936 if (!OutlineFromLinkOnceODRs
&& F
.hasLinkOnceODRLinkage())
7939 // This function is viable for outlining, so return true.
7944 X86InstrInfo::getOutliningType(MachineBasicBlock::iterator
&MIT
, unsigned Flags
) const {
7945 MachineInstr
&MI
= *MIT
;
7946 // Don't allow debug values to impact outlining type.
7947 if (MI
.isDebugInstr() || MI
.isIndirectDebugValue())
7948 return outliner::InstrType::Invisible
;
7950 // At this point, KILL instructions don't really tell us much so we can go
7951 // ahead and skip over them.
7953 return outliner::InstrType::Invisible
;
7955 // Is this a tail call? If yes, we can outline as a tail call.
7957 return outliner::InstrType::Legal
;
7959 // Is this the terminator of a basic block?
7960 if (MI
.isTerminator() || MI
.isReturn()) {
7962 // Does its parent have any successors in its MachineFunction?
7963 if (MI
.getParent()->succ_empty())
7964 return outliner::InstrType::Legal
;
7966 // It does, so we can't tail call it.
7967 return outliner::InstrType::Illegal
;
7970 // Don't outline anything that modifies or reads from the stack pointer.
7972 // FIXME: There are instructions which are being manually built without
7973 // explicit uses/defs so we also have to check the MCInstrDesc. We should be
7974 // able to remove the extra checks once those are fixed up. For example,
7975 // sometimes we might get something like %rax = POP64r 1. This won't be
7976 // caught by modifiesRegister or readsRegister even though the instruction
7977 // really ought to be formed so that modifiesRegister/readsRegister would
7979 if (MI
.modifiesRegister(X86::RSP
, &RI
) || MI
.readsRegister(X86::RSP
, &RI
) ||
7980 MI
.getDesc().hasImplicitUseOfPhysReg(X86::RSP
) ||
7981 MI
.getDesc().hasImplicitDefOfPhysReg(X86::RSP
))
7982 return outliner::InstrType::Illegal
;
7984 // Outlined calls change the instruction pointer, so don't read from it.
7985 if (MI
.readsRegister(X86::RIP
, &RI
) ||
7986 MI
.getDesc().hasImplicitUseOfPhysReg(X86::RIP
) ||
7987 MI
.getDesc().hasImplicitDefOfPhysReg(X86::RIP
))
7988 return outliner::InstrType::Illegal
;
7990 // Positions can't safely be outlined.
7991 if (MI
.isPosition())
7992 return outliner::InstrType::Illegal
;
7994 // Make sure none of the operands of this instruction do anything tricky.
7995 for (const MachineOperand
&MOP
: MI
.operands())
7996 if (MOP
.isCPI() || MOP
.isJTI() || MOP
.isCFIIndex() || MOP
.isFI() ||
7997 MOP
.isTargetIndex())
7998 return outliner::InstrType::Illegal
;
8000 return outliner::InstrType::Legal
;
8003 void X86InstrInfo::buildOutlinedFrame(MachineBasicBlock
&MBB
,
8004 MachineFunction
&MF
,
8005 const outliner::OutlinedFunction
&OF
)
8007 // If we're a tail call, we already have a return, so don't do anything.
8008 if (OF
.FrameConstructionID
== MachineOutlinerTailCall
)
8011 // We're a normal call, so our sequence doesn't have a return instruction.
8013 MachineInstr
*retq
= BuildMI(MF
, DebugLoc(), get(X86::RETQ
));
8014 MBB
.insert(MBB
.end(), retq
);
8017 MachineBasicBlock::iterator
8018 X86InstrInfo::insertOutlinedCall(Module
&M
, MachineBasicBlock
&MBB
,
8019 MachineBasicBlock::iterator
&It
,
8020 MachineFunction
&MF
,
8021 const outliner::Candidate
&C
) const {
8022 // Is it a tail call?
8023 if (C
.CallConstructionID
== MachineOutlinerTailCall
) {
8024 // Yes, just insert a JMP.
8026 BuildMI(MF
, DebugLoc(), get(X86::TAILJMPd64
))
8027 .addGlobalAddress(M
.getNamedValue(MF
.getName())));
8029 // No, insert a call.
8031 BuildMI(MF
, DebugLoc(), get(X86::CALL64pcrel32
))
8032 .addGlobalAddress(M
.getNamedValue(MF
.getName())));
8038 #define GET_INSTRINFO_HELPERS
8039 #include "X86GenInstrInfo.inc"