LLVM 24.0.0git
X86InstrInfo.cpp
Go to the documentation of this file.
1//===-- X86InstrInfo.cpp - X86 Instruction Information --------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the X86 implementation of the TargetInstrInfo class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "X86InstrInfo.h"
14#include "X86.h"
15#include "X86InstrBuilder.h"
16#include "X86InstrFoldTables.h"
18#include "X86Subtarget.h"
19#include "X86TargetMachine.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/Sequence.h"
35#include "llvm/IR/Function.h"
36#include "llvm/IR/InstrTypes.h"
37#include "llvm/IR/Module.h"
38#include "llvm/MC/MCAsmInfo.h"
39#include "llvm/MC/MCExpr.h"
40#include "llvm/MC/MCInst.h"
42#include "llvm/Support/Debug.h"
47#include <optional>
48
49using namespace llvm;
50
51#define DEBUG_TYPE "x86-instr-info"
52
53#define GET_INSTRINFO_CTOR_DTOR
54#include "X86GenInstrInfo.inc"
55
57
58static cl::opt<bool>
59 NoFusing("disable-spill-fusing",
60 cl::desc("Disable fusing of spill code into instructions"),
62static cl::opt<bool>
63 PrintFailedFusing("print-failed-fuse-candidates",
64 cl::desc("Print instructions that the allocator wants to"
65 " fuse, but the X86 backend currently can't"),
67static cl::opt<bool>
68 ReMatPICStubLoad("remat-pic-stub-load",
69 cl::desc("Re-materialize load from stub in PIC mode"),
70 cl::init(false), cl::Hidden);
72 PartialRegUpdateClearance("partial-reg-update-clearance",
73 cl::desc("Clearance between two register writes "
74 "for inserting XOR to avoid partial "
75 "register update"),
76 cl::init(64), cl::Hidden);
78 "undef-reg-clearance",
79 cl::desc("How many idle instructions we would like before "
80 "certain undef register reads"),
81 cl::init(128), cl::Hidden);
82
84 "x86-max-nf-conversions-for-cmp-reuse",
85 cl::desc("Maximum number of NF conversions allowed to reuse EFLAGS from a "
86 "producer dominating a multi-predecessor block"),
88
89// Pin the vtable to this file.
90void X86InstrInfo::anchor() {}
91
93 : X86GenInstrInfo(STI, RI,
94 (STI.isTarget64BitLP64() ? X86::ADJCALLSTACKDOWN64
95 : X86::ADJCALLSTACKDOWN32),
96 (STI.isTarget64BitLP64() ? X86::ADJCALLSTACKUP64
97 : X86::ADJCALLSTACKUP32),
98 X86::CATCHRET, (STI.is64Bit() ? X86::RET64 : X86::RET32)),
99 Subtarget(STI), RI(STI.getTargetTriple()) {}
100
102 unsigned OpNum) const {
103 auto *RC = TargetInstrInfo::getRegClass(MCID, OpNum);
104 // If the target does not have egpr, then r16-r31 will be resereved for all
105 // instructions.
106 if (!RC || !Subtarget.hasEGPR())
107 return RC;
108
110 return RC;
111
112 const X86RegisterInfo *RI = Subtarget.getRegisterInfo();
113 return RI->constrainRegClassToNonRex2(RC);
114}
115
118 if (Subtarget.isTarget64BitLP64())
119 return &X86::GR64RegClass;
120 // If the target is 64bit but we have been told to use 32bit addresses, we can
121 // still use 64-bit register as long as we know the high bits are zeros.
122 // Reflect that in the returned register class.
123 return Subtarget.is64Bit() ? &X86::LOW32_ADDR_ACCESSRegClass
124 : &X86::GR32RegClass;
125}
126
128 Register &SrcReg, Register &DstReg,
129 unsigned &SubIdx) const {
130 switch (MI.getOpcode()) {
131 default:
132 break;
133 case X86::MOVSX16rr8:
134 case X86::MOVZX16rr8:
135 case X86::MOVSX32rr8:
136 case X86::MOVZX32rr8:
137 case X86::MOVSX64rr8:
138 if (!Subtarget.is64Bit())
139 // It's not always legal to reference the low 8-bit of the larger
140 // register in 32-bit mode.
141 return false;
142 [[fallthrough]];
143 case X86::MOVSX32rr16:
144 case X86::MOVZX32rr16:
145 case X86::MOVSX64rr16:
146 case X86::MOVSX64rr32: {
147 if (MI.getOperand(0).getSubReg() || MI.getOperand(1).getSubReg())
148 // Be conservative.
149 return false;
150 SrcReg = MI.getOperand(1).getReg();
151 DstReg = MI.getOperand(0).getReg();
152 switch (MI.getOpcode()) {
153 default:
154 llvm_unreachable("Unreachable!");
155 case X86::MOVSX16rr8:
156 case X86::MOVZX16rr8:
157 case X86::MOVSX32rr8:
158 case X86::MOVZX32rr8:
159 case X86::MOVSX64rr8:
160 SubIdx = X86::sub_8bit;
161 break;
162 case X86::MOVSX32rr16:
163 case X86::MOVZX32rr16:
164 case X86::MOVSX64rr16:
165 SubIdx = X86::sub_16bit;
166 break;
167 case X86::MOVSX64rr32:
168 SubIdx = X86::sub_32bit;
169 break;
170 }
171 return true;
172 }
173 }
174 return false;
175}
176
178 if (MI.mayLoad() || MI.mayStore())
179 return false;
180
181 // Some target-independent operations that trivially lower to data-invariant
182 // instructions.
183 if (MI.isCopyLike() || MI.isInsertSubreg())
184 return true;
185
186 unsigned Opcode = MI.getOpcode();
187 using namespace X86;
188 // On x86 it is believed that imul is constant time w.r.t. the loaded data.
189 // However, they set flags and are perhaps the most surprisingly constant
190 // time operations so we call them out here separately.
191 if (isIMUL(Opcode))
192 return true;
193 // Bit scanning and counting instructions that are somewhat surprisingly
194 // constant time as they scan across bits and do other fairly complex
195 // operations like popcnt, but are believed to be constant time on x86.
196 // However, these set flags.
197 if (isBSF(Opcode) || isBSR(Opcode) || isLZCNT(Opcode) || isPOPCNT(Opcode) ||
198 isTZCNT(Opcode))
199 return true;
200 // Bit manipulation instructions are effectively combinations of basic
201 // arithmetic ops, and should still execute in constant time. These also
202 // set flags.
203 if (isBLCFILL(Opcode) || isBLCI(Opcode) || isBLCIC(Opcode) ||
204 isBLCMSK(Opcode) || isBLCS(Opcode) || isBLSFILL(Opcode) ||
205 isBLSI(Opcode) || isBLSIC(Opcode) || isBLSMSK(Opcode) || isBLSR(Opcode) ||
206 isTZMSK(Opcode))
207 return true;
208 // Bit extracting and clearing instructions should execute in constant time,
209 // and set flags.
210 if (isBEXTR(Opcode) || isBZHI(Opcode))
211 return true;
212 // Shift and rotate.
213 if (isROL(Opcode) || isROR(Opcode) || isSAR(Opcode) || isSHL(Opcode) ||
214 isSHR(Opcode) || isSHLD(Opcode) || isSHRD(Opcode))
215 return true;
216 // Basic arithmetic is constant time on the input but does set flags.
217 if (isADC(Opcode) || isADD(Opcode) || isAND(Opcode) || isOR(Opcode) ||
218 isSBB(Opcode) || isSUB(Opcode) || isXOR(Opcode))
219 return true;
220 // Arithmetic with just 32-bit and 64-bit variants and no immediates.
221 if (isANDN(Opcode))
222 return true;
223 // Unary arithmetic operations.
224 if (isDEC(Opcode) || isINC(Opcode) || isNEG(Opcode))
225 return true;
226 // Unlike other arithmetic, NOT doesn't set EFLAGS.
227 if (isNOT(Opcode))
228 return true;
229 // Various move instructions used to zero or sign extend things. Note that we
230 // intentionally don't support the _NOREX variants as we can't handle that
231 // register constraint anyways.
232 if (isMOVSX(Opcode) || isMOVZX(Opcode) || isMOVSXD(Opcode) || isMOV(Opcode))
233 return true;
234 // Arithmetic instructions that are both constant time and don't set flags.
235 if (isRORX(Opcode) || isSARX(Opcode) || isSHLX(Opcode) || isSHRX(Opcode))
236 return true;
237 // LEA doesn't actually access memory, and its arithmetic is constant time.
238 if (isLEA(Opcode))
239 return true;
240 // By default, assume that the instruction is not data invariant.
241 return false;
242}
243
245 switch (MI.getOpcode()) {
246 default:
247 // By default, assume that the load will immediately leak.
248 return false;
249
250 // On x86 it is believed that imul is constant time w.r.t. the loaded data.
251 // However, they set flags and are perhaps the most surprisingly constant
252 // time operations so we call them out here separately.
253 case X86::IMUL16rm:
254 case X86::IMUL16rmi:
255 case X86::IMUL32rm:
256 case X86::IMUL32rmi:
257 case X86::IMUL64rm:
258 case X86::IMUL64rmi32:
259
260 // Bit scanning and counting instructions that are somewhat surprisingly
261 // constant time as they scan across bits and do other fairly complex
262 // operations like popcnt, but are believed to be constant time on x86.
263 // However, these set flags.
264 case X86::BSF16rm:
265 case X86::BSF32rm:
266 case X86::BSF64rm:
267 case X86::BSR16rm:
268 case X86::BSR32rm:
269 case X86::BSR64rm:
270 case X86::LZCNT16rm:
271 case X86::LZCNT32rm:
272 case X86::LZCNT64rm:
273 case X86::POPCNT16rm:
274 case X86::POPCNT32rm:
275 case X86::POPCNT64rm:
276 case X86::TZCNT16rm:
277 case X86::TZCNT32rm:
278 case X86::TZCNT64rm:
279
280 // Bit manipulation instructions are effectively combinations of basic
281 // arithmetic ops, and should still execute in constant time. These also
282 // set flags.
283 case X86::BLCFILL32rm:
284 case X86::BLCFILL64rm:
285 case X86::BLCI32rm:
286 case X86::BLCI64rm:
287 case X86::BLCIC32rm:
288 case X86::BLCIC64rm:
289 case X86::BLCMSK32rm:
290 case X86::BLCMSK64rm:
291 case X86::BLCS32rm:
292 case X86::BLCS64rm:
293 case X86::BLSFILL32rm:
294 case X86::BLSFILL64rm:
295 case X86::BLSI32rm:
296 case X86::BLSI64rm:
297 case X86::BLSIC32rm:
298 case X86::BLSIC64rm:
299 case X86::BLSMSK32rm:
300 case X86::BLSMSK64rm:
301 case X86::BLSR32rm:
302 case X86::BLSR64rm:
303 case X86::TZMSK32rm:
304 case X86::TZMSK64rm:
305
306 // Bit extracting and clearing instructions should execute in constant time,
307 // and set flags.
308 case X86::BEXTR32rm:
309 case X86::BEXTR64rm:
310 case X86::BEXTRI32mi:
311 case X86::BEXTRI64mi:
312 case X86::BZHI32rm:
313 case X86::BZHI64rm:
314
315 // Basic arithmetic is constant time on the input but does set flags.
316 case X86::ADC8rm:
317 case X86::ADC16rm:
318 case X86::ADC32rm:
319 case X86::ADC64rm:
320 case X86::ADD8rm:
321 case X86::ADD16rm:
322 case X86::ADD32rm:
323 case X86::ADD64rm:
324 case X86::AND8rm:
325 case X86::AND16rm:
326 case X86::AND32rm:
327 case X86::AND64rm:
328 case X86::ANDN32rm:
329 case X86::ANDN64rm:
330 case X86::OR8rm:
331 case X86::OR16rm:
332 case X86::OR32rm:
333 case X86::OR64rm:
334 case X86::SBB8rm:
335 case X86::SBB16rm:
336 case X86::SBB32rm:
337 case X86::SBB64rm:
338 case X86::SUB8rm:
339 case X86::SUB16rm:
340 case X86::SUB32rm:
341 case X86::SUB64rm:
342 case X86::XOR8rm:
343 case X86::XOR16rm:
344 case X86::XOR32rm:
345 case X86::XOR64rm:
346
347 // Integer multiply w/o affecting flags is still believed to be constant
348 // time on x86. Called out separately as this is among the most surprising
349 // instructions to exhibit that behavior.
350 case X86::MULX32rm:
351 case X86::MULX64rm:
352
353 // Arithmetic instructions that are both constant time and don't set flags.
354 case X86::RORX32mi:
355 case X86::RORX64mi:
356 case X86::SARX32rm:
357 case X86::SARX64rm:
358 case X86::SHLX32rm:
359 case X86::SHLX64rm:
360 case X86::SHRX32rm:
361 case X86::SHRX64rm:
362
363 // Conversions are believed to be constant time and don't set flags.
364 case X86::CVTTSD2SI64rm:
365 case X86::VCVTTSD2SI64rm:
366 case X86::VCVTTSD2SI64Zrm:
367 case X86::CVTTSD2SIrm:
368 case X86::VCVTTSD2SIrm:
369 case X86::VCVTTSD2SIZrm:
370 case X86::CVTTSS2SI64rm:
371 case X86::VCVTTSS2SI64rm:
372 case X86::VCVTTSS2SI64Zrm:
373 case X86::CVTTSS2SIrm:
374 case X86::VCVTTSS2SIrm:
375 case X86::VCVTTSS2SIZrm:
376 case X86::CVTSI2SDrm:
377 case X86::VCVTSI2SDrm:
378 case X86::VCVTSI2SDZrm:
379 case X86::CVTSI2SSrm:
380 case X86::VCVTSI2SSrm:
381 case X86::VCVTSI2SSZrm:
382 case X86::CVTSI642SDrm:
383 case X86::VCVTSI642SDrm:
384 case X86::VCVTSI642SDZrm:
385 case X86::CVTSI642SSrm:
386 case X86::VCVTSI642SSrm:
387 case X86::VCVTSI642SSZrm:
388 case X86::CVTSS2SDrm:
389 case X86::VCVTSS2SDrm:
390 case X86::VCVTSS2SDZrm:
391 case X86::CVTSD2SSrm:
392 case X86::VCVTSD2SSrm:
393 case X86::VCVTSD2SSZrm:
394 // AVX512 added unsigned integer conversions.
395 case X86::VCVTTSD2USI64Zrm:
396 case X86::VCVTTSD2USIZrm:
397 case X86::VCVTTSS2USI64Zrm:
398 case X86::VCVTTSS2USIZrm:
399 case X86::VCVTUSI2SDZrm:
400 case X86::VCVTUSI642SDZrm:
401 case X86::VCVTUSI2SSZrm:
402 case X86::VCVTUSI642SSZrm:
403
404 // Loads to register don't set flags.
405 case X86::MOV8rm:
406 case X86::MOV8rm_NOREX:
407 case X86::MOV16rm:
408 case X86::MOV32rm:
409 case X86::MOV64rm:
410 case X86::MOVSX16rm8:
411 case X86::MOVSX32rm16:
412 case X86::MOVSX32rm8:
413 case X86::MOVSX32rm8_NOREX:
414 case X86::MOVSX64rm16:
415 case X86::MOVSX64rm32:
416 case X86::MOVSX64rm8:
417 case X86::MOVZX16rm8:
418 case X86::MOVZX32rm16:
419 case X86::MOVZX32rm8:
420 case X86::MOVZX32rm8_NOREX:
421 case X86::MOVZX64rm16:
422 case X86::MOVZX64rm8:
423 return true;
424 }
425}
426
428 const MachineFunction *MF = MI.getParent()->getParent();
430
431 if (isFrameInstr(MI)) {
432 int SPAdj = alignTo(getFrameSize(MI), TFI->getStackAlign());
433 SPAdj -= getFrameAdjustment(MI);
434 if (!isFrameSetup(MI))
435 SPAdj = -SPAdj;
436 return SPAdj;
437 }
438
439 // To know whether a call adjusts the stack, we need information
440 // that is bound to the following ADJCALLSTACKUP pseudo.
441 // Look for the next ADJCALLSTACKUP that follows the call.
442 if (MI.isCall()) {
443 const MachineBasicBlock *MBB = MI.getParent();
445 for (auto E = MBB->end(); I != E; ++I) {
446 if (I->getOpcode() == getCallFrameDestroyOpcode() || I->isCall())
447 break;
448 }
449
450 // If we could not find a frame destroy opcode, then it has already
451 // been simplified, so we don't care.
452 if (I->getOpcode() != getCallFrameDestroyOpcode())
453 return 0;
454
455 return -(I->getOperand(1).getImm());
456 }
457
458 // Currently handle only PUSHes we can reasonably expect to see
459 // in call sequences
460 switch (MI.getOpcode()) {
461 default:
462 return 0;
463 case X86::PUSH32r:
464 case X86::PUSH32rmm:
465 case X86::PUSH32rmr:
466 case X86::PUSH32i:
467 return 4;
468 case X86::PUSH64r:
469 case X86::PUSH64rmm:
470 case X86::PUSH64rmr:
471 case X86::PUSH64i32:
472 return 8;
473 }
474}
475
476/// Return true and the FrameIndex if the specified
477/// operand and follow operands form a reference to the stack frame.
478bool X86InstrInfo::isFrameOperand(const MachineInstr &MI, unsigned int Op,
479 int &FrameIndex) const {
480 if (MI.getOperand(Op + X86::AddrBaseReg).isFI() &&
481 MI.getOperand(Op + X86::AddrScaleAmt).isImm() &&
482 MI.getOperand(Op + X86::AddrIndexReg).isReg() &&
483 MI.getOperand(Op + X86::AddrDisp).isImm() &&
484 MI.getOperand(Op + X86::AddrScaleAmt).getImm() == 1 &&
485 MI.getOperand(Op + X86::AddrIndexReg).getReg() == 0 &&
486 MI.getOperand(Op + X86::AddrDisp).getImm() == 0) {
487 FrameIndex = MI.getOperand(Op + X86::AddrBaseReg).getIndex();
488 return true;
489 }
490 return false;
491}
492
493static bool isFrameLoadOpcode(int Opcode, TypeSize &MemBytes) {
494 switch (Opcode) {
495 default:
496 return false;
497 case X86::MOV8rm:
498 case X86::KMOVBkm:
499 case X86::KMOVBkm_EVEX:
500 MemBytes = TypeSize::getFixed(1);
501 return true;
502 case X86::MOV16rm:
503 case X86::KMOVWkm:
504 case X86::KMOVWkm_EVEX:
505 case X86::VMOVSHZrm:
506 case X86::VMOVSHZrm_alt:
507 MemBytes = TypeSize::getFixed(2);
508 return true;
509 case X86::MOV32rm:
510 case X86::MOVSSrm:
511 case X86::MOVSSrm_alt:
512 case X86::VMOVSSrm:
513 case X86::VMOVSSrm_alt:
514 case X86::VMOVSSZrm:
515 case X86::VMOVSSZrm_alt:
516 case X86::KMOVDkm:
517 case X86::KMOVDkm_EVEX:
518 MemBytes = TypeSize::getFixed(4);
519 return true;
520 case X86::MOV64rm:
521 case X86::LD_Fp64m:
522 case X86::MOVSDrm:
523 case X86::MOVSDrm_alt:
524 case X86::VMOVSDrm:
525 case X86::VMOVSDrm_alt:
526 case X86::VMOVSDZrm:
527 case X86::VMOVSDZrm_alt:
528 case X86::MMX_MOVD64rm:
529 case X86::MMX_MOVQ64rm:
530 case X86::KMOVQkm:
531 case X86::KMOVQkm_EVEX:
532 MemBytes = TypeSize::getFixed(8);
533 return true;
534 case X86::MOVAPSrm:
535 case X86::MOVUPSrm:
536 case X86::MOVAPDrm:
537 case X86::MOVUPDrm:
538 case X86::MOVDQArm:
539 case X86::MOVDQUrm:
540 case X86::VMOVAPSrm:
541 case X86::VMOVUPSrm:
542 case X86::VMOVAPDrm:
543 case X86::VMOVUPDrm:
544 case X86::VMOVDQArm:
545 case X86::VMOVDQUrm:
546 case X86::VMOVAPSZ128rm:
547 case X86::VMOVUPSZ128rm:
548 case X86::VMOVAPSZ128rm_NOVLX:
549 case X86::VMOVUPSZ128rm_NOVLX:
550 case X86::VMOVAPDZ128rm:
551 case X86::VMOVUPDZ128rm:
552 case X86::VMOVDQU8Z128rm:
553 case X86::VMOVDQU16Z128rm:
554 case X86::VMOVDQA32Z128rm:
555 case X86::VMOVDQU32Z128rm:
556 case X86::VMOVDQA64Z128rm:
557 case X86::VMOVDQU64Z128rm:
558 MemBytes = TypeSize::getFixed(16);
559 return true;
560 case X86::VMOVAPSYrm:
561 case X86::VMOVUPSYrm:
562 case X86::VMOVAPDYrm:
563 case X86::VMOVUPDYrm:
564 case X86::VMOVDQAYrm:
565 case X86::VMOVDQUYrm:
566 case X86::VMOVAPSZ256rm:
567 case X86::VMOVUPSZ256rm:
568 case X86::VMOVAPSZ256rm_NOVLX:
569 case X86::VMOVUPSZ256rm_NOVLX:
570 case X86::VMOVAPDZ256rm:
571 case X86::VMOVUPDZ256rm:
572 case X86::VMOVDQU8Z256rm:
573 case X86::VMOVDQU16Z256rm:
574 case X86::VMOVDQA32Z256rm:
575 case X86::VMOVDQU32Z256rm:
576 case X86::VMOVDQA64Z256rm:
577 case X86::VMOVDQU64Z256rm:
578 MemBytes = TypeSize::getFixed(32);
579 return true;
580 case X86::VMOVAPSZrm:
581 case X86::VMOVUPSZrm:
582 case X86::VMOVAPDZrm:
583 case X86::VMOVUPDZrm:
584 case X86::VMOVDQU8Zrm:
585 case X86::VMOVDQU16Zrm:
586 case X86::VMOVDQA32Zrm:
587 case X86::VMOVDQU32Zrm:
588 case X86::VMOVDQA64Zrm:
589 case X86::VMOVDQU64Zrm:
590 MemBytes = TypeSize::getFixed(64);
591 return true;
592 }
593}
594
595static bool isFrameStoreOpcode(int Opcode, TypeSize &MemBytes) {
596 switch (Opcode) {
597 default:
598 return false;
599 case X86::MOV8mr:
600 case X86::KMOVBmk:
601 case X86::KMOVBmk_EVEX:
602 MemBytes = TypeSize::getFixed(1);
603 return true;
604 case X86::MOV16mr:
605 case X86::KMOVWmk:
606 case X86::KMOVWmk_EVEX:
607 case X86::VMOVSHZmr:
608 MemBytes = TypeSize::getFixed(2);
609 return true;
610 case X86::MOV32mr:
611 case X86::MOVSSmr:
612 case X86::VMOVSSmr:
613 case X86::VMOVSSZmr:
614 case X86::KMOVDmk:
615 case X86::KMOVDmk_EVEX:
616 MemBytes = TypeSize::getFixed(4);
617 return true;
618 case X86::MOV64mr:
619 case X86::ST_FpP64m:
620 case X86::MOVSDmr:
621 case X86::VMOVSDmr:
622 case X86::VMOVSDZmr:
623 case X86::MMX_MOVD64mr:
624 case X86::MMX_MOVQ64mr:
625 case X86::MMX_MOVNTQmr:
626 case X86::KMOVQmk:
627 case X86::KMOVQmk_EVEX:
628 MemBytes = TypeSize::getFixed(8);
629 return true;
630 case X86::MOVAPSmr:
631 case X86::MOVUPSmr:
632 case X86::MOVAPDmr:
633 case X86::MOVUPDmr:
634 case X86::MOVDQAmr:
635 case X86::MOVDQUmr:
636 case X86::VMOVAPSmr:
637 case X86::VMOVUPSmr:
638 case X86::VMOVAPDmr:
639 case X86::VMOVUPDmr:
640 case X86::VMOVDQAmr:
641 case X86::VMOVDQUmr:
642 case X86::VMOVUPSZ128mr:
643 case X86::VMOVAPSZ128mr:
644 case X86::VMOVUPSZ128mr_NOVLX:
645 case X86::VMOVAPSZ128mr_NOVLX:
646 case X86::VMOVUPDZ128mr:
647 case X86::VMOVAPDZ128mr:
648 case X86::VMOVDQA32Z128mr:
649 case X86::VMOVDQU32Z128mr:
650 case X86::VMOVDQA64Z128mr:
651 case X86::VMOVDQU64Z128mr:
652 case X86::VMOVDQU8Z128mr:
653 case X86::VMOVDQU16Z128mr:
654 MemBytes = TypeSize::getFixed(16);
655 return true;
656 case X86::VMOVUPSYmr:
657 case X86::VMOVAPSYmr:
658 case X86::VMOVUPDYmr:
659 case X86::VMOVAPDYmr:
660 case X86::VMOVDQUYmr:
661 case X86::VMOVDQAYmr:
662 case X86::VMOVUPSZ256mr:
663 case X86::VMOVAPSZ256mr:
664 case X86::VMOVUPSZ256mr_NOVLX:
665 case X86::VMOVAPSZ256mr_NOVLX:
666 case X86::VMOVUPDZ256mr:
667 case X86::VMOVAPDZ256mr:
668 case X86::VMOVDQU8Z256mr:
669 case X86::VMOVDQU16Z256mr:
670 case X86::VMOVDQA32Z256mr:
671 case X86::VMOVDQU32Z256mr:
672 case X86::VMOVDQA64Z256mr:
673 case X86::VMOVDQU64Z256mr:
674 MemBytes = TypeSize::getFixed(32);
675 return true;
676 case X86::VMOVUPSZmr:
677 case X86::VMOVAPSZmr:
678 case X86::VMOVUPDZmr:
679 case X86::VMOVAPDZmr:
680 case X86::VMOVDQU8Zmr:
681 case X86::VMOVDQU16Zmr:
682 case X86::VMOVDQA32Zmr:
683 case X86::VMOVDQU32Zmr:
684 case X86::VMOVDQA64Zmr:
685 case X86::VMOVDQU64Zmr:
686 MemBytes = TypeSize::getFixed(64);
687 return true;
688 }
689 return false;
690}
691
693 int &FrameIndex) const {
694 TypeSize Dummy = TypeSize::getZero();
695 return X86InstrInfo::isLoadFromStackSlot(MI, FrameIndex, Dummy);
696}
697
699 int &FrameIndex,
700 TypeSize &MemBytes) const {
701 if (isFrameLoadOpcode(MI.getOpcode(), MemBytes))
702 if (MI.getOperand(0).getSubReg() == 0 && isFrameOperand(MI, 1, FrameIndex))
703 return MI.getOperand(0).getReg();
704 return Register();
705}
706
708 int &FrameIndex) const {
709 TypeSize Dummy = TypeSize::getZero();
710 if (isFrameLoadOpcode(MI.getOpcode(), Dummy)) {
711 if (Register Reg = isLoadFromStackSlot(MI, FrameIndex))
712 return Reg;
713 // Check for post-frame index elimination operations
715 if (hasLoadFromStackSlot(MI, Accesses)) {
716 FrameIndex =
717 cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue())
718 ->getFrameIndex();
719 return MI.getOperand(0).getReg();
720 }
721 }
722 return Register();
723}
724
726 int &FrameIndex) const {
727 TypeSize Dummy = TypeSize::getZero();
728 return X86InstrInfo::isStoreToStackSlot(MI, FrameIndex, Dummy);
729}
730
732 int &FrameIndex,
733 TypeSize &MemBytes) const {
734 if (isFrameStoreOpcode(MI.getOpcode(), MemBytes))
735 if (MI.getOperand(X86::AddrNumOperands).getSubReg() == 0 &&
736 isFrameOperand(MI, 0, FrameIndex))
737 return MI.getOperand(X86::AddrNumOperands).getReg();
738 return Register();
739}
740
742 int &FrameIndex) const {
743 TypeSize Dummy = TypeSize::getZero();
744 if (isFrameStoreOpcode(MI.getOpcode(), Dummy)) {
745 if (Register Reg = isStoreToStackSlot(MI, FrameIndex))
746 return Reg;
747 // Check for post-frame index elimination operations
749 if (hasStoreToStackSlot(MI, Accesses)) {
750 FrameIndex =
751 cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue())
752 ->getFrameIndex();
753 return MI.getOperand(X86::AddrNumOperands).getReg();
754 }
755 }
756 return Register();
757}
758
759/// Return true if register is PIC base; i.e.g defined by X86::MOVPC32r.
760static bool regIsPICBase(Register BaseReg, const MachineRegisterInfo &MRI) {
761 // Don't waste compile time scanning use-def chains of physregs.
762 if (!BaseReg.isVirtual())
763 return false;
764 bool isPICBase = false;
765 for (const MachineInstr &DefMI : MRI.def_instructions(BaseReg)) {
766 if (DefMI.getOpcode() != X86::MOVPC32r)
767 return false;
768 assert(!isPICBase && "More than one PIC base?");
769 isPICBase = true;
770 }
771 return isPICBase;
772}
773
775 const MachineInstr &MI) const {
776 switch (MI.getOpcode()) {
777 default:
778 // This function should only be called for opcodes with the ReMaterializable
779 // flag set.
780 llvm_unreachable("Unknown rematerializable operation!");
781 break;
782 case X86::IMPLICIT_DEF:
783 // Defer to generic logic.
784 break;
785 case X86::LOAD_STACK_GUARD:
786 case X86::LD_Fp032:
787 case X86::LD_Fp064:
788 case X86::LD_Fp080:
789 case X86::LD_Fp132:
790 case X86::LD_Fp164:
791 case X86::LD_Fp180:
792 case X86::AVX1_SETALLONES:
793 case X86::AVX2_SETALLONES:
794 case X86::AVX512_128_SET0:
795 case X86::AVX512_128_SETALLONES:
796 case X86::AVX512_256_SETALLONES:
797 case X86::AVX512_512_SETALLONES:
798 case X86::AVX512_FsFLD0SD:
799 case X86::AVX512_FsFLD0SH:
800 case X86::AVX512_FsFLD0SS:
801 case X86::AVX512_FsFLD0F128:
802 case X86::FsFLD0SD:
803 case X86::FsFLD0SS:
804 case X86::FsFLD0SH:
805 case X86::FsFLD0F128:
806 case X86::KSET0B:
807 case X86::KSET0D:
808 case X86::KSET0Q:
809 case X86::KSET0W:
810 case X86::KSET1B:
811 case X86::KSET1D:
812 case X86::KSET1Q:
813 case X86::KSET1W:
814 case X86::MMX_SET0:
815 case X86::MOV32ImmSExti8:
816 case X86::MOV32r0:
817 case X86::MOV32r1:
818 case X86::MOV32r_1:
819 case X86::MOV32ri64:
820 case X86::MOV64ImmSExti8:
821 case X86::V_SET0:
822 case X86::V_SETALLONES:
823 case X86::MOV16ri:
824 case X86::MOV32ri:
825 case X86::MOV64ri:
826 case X86::MOV64ri32:
827 case X86::MOV8ri:
828 case X86::PTILEZEROV:
829 return true;
830
831 case X86::MOV8rm:
832 case X86::MOV8rm_NOREX:
833 case X86::MOV16rm:
834 case X86::MOV32rm:
835 case X86::MOV64rm:
836 case X86::MOVSSrm:
837 case X86::MOVSSrm_alt:
838 case X86::MOVSDrm:
839 case X86::MOVSDrm_alt:
840 case X86::MOVAPSrm:
841 case X86::MOVUPSrm:
842 case X86::MOVAPDrm:
843 case X86::MOVUPDrm:
844 case X86::MOVDQArm:
845 case X86::MOVDQUrm:
846 case X86::VMOVSSrm:
847 case X86::VMOVSSrm_alt:
848 case X86::VMOVSDrm:
849 case X86::VMOVSDrm_alt:
850 case X86::VMOVAPSrm:
851 case X86::VMOVUPSrm:
852 case X86::VMOVAPDrm:
853 case X86::VMOVUPDrm:
854 case X86::VMOVDQArm:
855 case X86::VMOVDQUrm:
856 case X86::VMOVAPSYrm:
857 case X86::VMOVUPSYrm:
858 case X86::VMOVAPDYrm:
859 case X86::VMOVUPDYrm:
860 case X86::VMOVDQAYrm:
861 case X86::VMOVDQUYrm:
862 case X86::MMX_MOVD64rm:
863 case X86::MMX_MOVQ64rm:
864 case X86::VBROADCASTSSrm:
865 case X86::VBROADCASTSSYrm:
866 case X86::VBROADCASTSDYrm:
867 // AVX-512
868 case X86::VPBROADCASTBZ128rm:
869 case X86::VPBROADCASTBZ256rm:
870 case X86::VPBROADCASTBZrm:
871 case X86::VBROADCASTF32X2Z256rm:
872 case X86::VBROADCASTF32X2Zrm:
873 case X86::VBROADCASTI32X2Z128rm:
874 case X86::VBROADCASTI32X2Z256rm:
875 case X86::VBROADCASTI32X2Zrm:
876 case X86::VPBROADCASTWZ128rm:
877 case X86::VPBROADCASTWZ256rm:
878 case X86::VPBROADCASTWZrm:
879 case X86::VPBROADCASTDZ128rm:
880 case X86::VPBROADCASTDZ256rm:
881 case X86::VPBROADCASTDZrm:
882 case X86::VBROADCASTSSZ128rm:
883 case X86::VBROADCASTSSZ256rm:
884 case X86::VBROADCASTSSZrm:
885 case X86::VPBROADCASTQZ128rm:
886 case X86::VPBROADCASTQZ256rm:
887 case X86::VPBROADCASTQZrm:
888 case X86::VBROADCASTSDZ256rm:
889 case X86::VBROADCASTSDZrm:
890 case X86::VMOVSSZrm:
891 case X86::VMOVSSZrm_alt:
892 case X86::VMOVSDZrm:
893 case X86::VMOVSDZrm_alt:
894 case X86::VMOVSHZrm:
895 case X86::VMOVSHZrm_alt:
896 case X86::VMOVAPDZ128rm:
897 case X86::VMOVAPDZ256rm:
898 case X86::VMOVAPDZrm:
899 case X86::VMOVAPSZ128rm:
900 case X86::VMOVAPSZ256rm:
901 case X86::VMOVAPSZ128rm_NOVLX:
902 case X86::VMOVAPSZ256rm_NOVLX:
903 case X86::VMOVAPSZrm:
904 case X86::VMOVDQA32Z128rm:
905 case X86::VMOVDQA32Z256rm:
906 case X86::VMOVDQA32Zrm:
907 case X86::VMOVDQA64Z128rm:
908 case X86::VMOVDQA64Z256rm:
909 case X86::VMOVDQA64Zrm:
910 case X86::VMOVDQU16Z128rm:
911 case X86::VMOVDQU16Z256rm:
912 case X86::VMOVDQU16Zrm:
913 case X86::VMOVDQU32Z128rm:
914 case X86::VMOVDQU32Z256rm:
915 case X86::VMOVDQU32Zrm:
916 case X86::VMOVDQU64Z128rm:
917 case X86::VMOVDQU64Z256rm:
918 case X86::VMOVDQU64Zrm:
919 case X86::VMOVDQU8Z128rm:
920 case X86::VMOVDQU8Z256rm:
921 case X86::VMOVDQU8Zrm:
922 case X86::VMOVUPDZ128rm:
923 case X86::VMOVUPDZ256rm:
924 case X86::VMOVUPDZrm:
925 case X86::VMOVUPSZ128rm:
926 case X86::VMOVUPSZ256rm:
927 case X86::VMOVUPSZ128rm_NOVLX:
928 case X86::VMOVUPSZ256rm_NOVLX:
929 case X86::VMOVUPSZrm: {
930 // Loads from constant pools are trivially rematerializable.
931 if (MI.getOperand(1 + X86::AddrBaseReg).isReg() &&
932 MI.getOperand(1 + X86::AddrScaleAmt).isImm() &&
933 MI.getOperand(1 + X86::AddrIndexReg).isReg() &&
934 MI.getOperand(1 + X86::AddrIndexReg).getReg() == 0 &&
935 MI.isDereferenceableInvariantLoad()) {
936 Register BaseReg = MI.getOperand(1 + X86::AddrBaseReg).getReg();
937 if (BaseReg == 0 || BaseReg == X86::RIP)
938 return true;
939 // Allow re-materialization of PIC load.
940 if (!(!ReMatPICStubLoad && MI.getOperand(1 + X86::AddrDisp).isGlobal())) {
941 const MachineFunction &MF = *MI.getParent()->getParent();
942 const MachineRegisterInfo &MRI = MF.getRegInfo();
943 if (regIsPICBase(BaseReg, MRI))
944 return true;
945 }
946 }
947 break;
948 }
949
950 case X86::LEA32r:
951 case X86::LEA64r: {
952 if (MI.getOperand(1 + X86::AddrScaleAmt).isImm() &&
953 MI.getOperand(1 + X86::AddrIndexReg).isReg() &&
954 MI.getOperand(1 + X86::AddrIndexReg).getReg() == 0 &&
955 !MI.getOperand(1 + X86::AddrDisp).isReg()) {
956 // lea fi#, lea GV, etc. are all rematerializable.
957 if (!MI.getOperand(1 + X86::AddrBaseReg).isReg())
958 return true;
959 Register BaseReg = MI.getOperand(1 + X86::AddrBaseReg).getReg();
960 if (BaseReg == 0)
961 return true;
962 // Allow re-materialization of lea PICBase + x.
963 const MachineFunction &MF = *MI.getParent()->getParent();
964 const MachineRegisterInfo &MRI = MF.getRegInfo();
965 if (regIsPICBase(BaseReg, MRI))
966 return true;
967 }
968 break;
969 }
970 }
972}
973
976 Register DestReg, unsigned SubIdx,
977 const MachineInstr &Orig,
978 LaneBitmask UsedLanes) const {
979 bool ClobbersEFLAGS = Orig.modifiesRegister(X86::EFLAGS, &TRI);
980 if (ClobbersEFLAGS && MBB.computeRegisterLiveness(&TRI, X86::EFLAGS, I) !=
982 // The instruction clobbers EFLAGS. Re-materialize as MOV32ri to avoid side
983 // effects.
984 int Value;
985 switch (Orig.getOpcode()) {
986 case X86::MOV32r0:
987 Value = 0;
988 break;
989 case X86::MOV32r1:
990 Value = 1;
991 break;
992 case X86::MOV32r_1:
993 Value = -1;
994 break;
995 default:
996 llvm_unreachable("Unexpected instruction!");
997 }
998
999 const DebugLoc &DL = Orig.getDebugLoc();
1000 BuildMI(MBB, I, DL, get(X86::MOV32ri))
1001 .add(Orig.getOperand(0))
1002 .addImm(Value);
1003 } else {
1004 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig);
1005 MBB.insert(I, MI);
1006 }
1007
1008 MachineInstr &NewMI = *std::prev(I);
1009 NewMI.substituteRegister(Orig.getOperand(0).getReg(), DestReg, SubIdx, TRI);
1010}
1011
1012/// True if MI has a condition code def, e.g. EFLAGS, that is not marked dead.
1014 for (const MachineOperand &MO : MI.operands()) {
1015 if (MO.isReg() && MO.isDef() && MO.getReg() == X86::EFLAGS &&
1016 !MO.isDead()) {
1017 return true;
1018 }
1019 }
1020 return false;
1021}
1022
1023/// Check whether the shift count for a machine operand is non-zero.
1024inline static unsigned getTruncatedShiftCount(const MachineInstr &MI,
1025 unsigned ShiftAmtOperandIdx) {
1026 // The shift count is six bits with the REX.W prefix and five bits without.
1027 unsigned ShiftCountMask = (MI.getDesc().TSFlags & X86II::REX_W) ? 63 : 31;
1028 unsigned Imm = MI.getOperand(ShiftAmtOperandIdx).getImm();
1029 return Imm & ShiftCountMask;
1030}
1031
1032/// Check whether the given shift count is appropriate
1033/// can be represented by a LEA instruction.
1034inline static bool isTruncatedShiftCountForLEA(unsigned ShAmt) {
1035 // Left shift instructions can be transformed into load-effective-address
1036 // instructions if we can encode them appropriately.
1037 // A LEA instruction utilizes a SIB byte to encode its scale factor.
1038 // The SIB.scale field is two bits wide which means that we can encode any
1039 // shift amount less than 4.
1040 return ShAmt < 4 && ShAmt > 0;
1041}
1042
1043static bool
1045 const MachineRegisterInfo *MRI, MachineInstr **AndInstr,
1046 const TargetRegisterInfo *TRI, const X86Subtarget &ST,
1047 bool &NoSignFlag, bool &ClearsOverflowFlag) {
1048 if (!(CmpValDefInstr.getOpcode() == X86::SUBREG_TO_REG &&
1049 CmpInstr.getOpcode() == X86::TEST64rr) &&
1050 !(CmpValDefInstr.getOpcode() == X86::COPY &&
1051 CmpInstr.getOpcode() == X86::TEST16rr))
1052 return false;
1053
1054 // CmpInstr is a TEST16rr/TEST64rr instruction, and
1055 // `X86InstrInfo::analyzeCompare` guarantees that it's analyzable only if two
1056 // registers are identical.
1057 assert((CmpInstr.getOperand(0).getReg() == CmpInstr.getOperand(1).getReg()) &&
1058 "CmpInstr is an analyzable TEST16rr/TEST64rr, and "
1059 "`X86InstrInfo::analyzeCompare` requires two reg operands are the"
1060 "same.");
1061
1062 // Caller (`X86InstrInfo::optimizeCompareInstr`) guarantees that
1063 // `CmpValDefInstr` defines the value that's used by `CmpInstr`; in this case
1064 // if `CmpValDefInstr` sets the EFLAGS, it is likely that `CmpInstr` is
1065 // redundant.
1066 assert(
1067 (MRI->getVRegDef(CmpInstr.getOperand(0).getReg()) == &CmpValDefInstr) &&
1068 "Caller guarantees that TEST64rr is a user of SUBREG_TO_REG or TEST16rr "
1069 "is a user of COPY sub16bit.");
1070 MachineInstr *VregDefInstr = nullptr;
1071 if (CmpInstr.getOpcode() == X86::TEST16rr) {
1072 if (!CmpValDefInstr.getOperand(1).getReg().isVirtual())
1073 return false;
1074 VregDefInstr = MRI->getVRegDef(CmpValDefInstr.getOperand(1).getReg());
1075 if (!VregDefInstr)
1076 return false;
1077 // We can only remove test when AND32ri or AND64ri32 whose imm can fit 16bit
1078 // size, others 32/64 bit ops would test higher bits which test16rr don't
1079 // want to.
1080 if (!((VregDefInstr->getOpcode() == X86::AND32ri ||
1081 VregDefInstr->getOpcode() == X86::AND64ri32) &&
1082 isUInt<16>(VregDefInstr->getOperand(2).getImm())))
1083 return false;
1084 }
1085
1086 if (CmpInstr.getOpcode() == X86::TEST64rr) {
1087 // As seen in X86 td files, CmpValDefInstr.getOperand(3) is typically
1088 // sub_32bit or sub_xmm.
1089 if (CmpValDefInstr.getOperand(2).getImm() != X86::sub_32bit)
1090 return false;
1091
1092 VregDefInstr = MRI->getVRegDef(CmpValDefInstr.getOperand(1).getReg());
1093 }
1094
1095 assert(VregDefInstr && "Must have a definition (SSA)");
1096
1097 // Requires `CmpValDefInstr` and `VregDefInstr` are from the same MBB
1098 // to simplify the subsequent analysis.
1099 //
1100 // FIXME: If `VregDefInstr->getParent()` is the only predecessor of
1101 // `CmpValDefInstr.getParent()`, this could be handled.
1102 if (VregDefInstr->getParent() != CmpValDefInstr.getParent())
1103 return false;
1104
1105 if (X86::isAND(VregDefInstr->getOpcode()) &&
1106 (!ST.hasNF() || VregDefInstr->modifiesRegister(X86::EFLAGS, TRI))) {
1107 // Get a sequence of instructions like
1108 // %reg = and* ... // Set EFLAGS
1109 // ... // EFLAGS not changed
1110 // %extended_reg = subreg_to_reg %reg, %subreg.sub_32bit
1111 // test64rr %extended_reg, %extended_reg, implicit-def $eflags
1112 // or
1113 // %reg = and32* ...
1114 // ... // EFLAGS not changed.
1115 // %src_reg = copy %reg.sub_16bit:gr32
1116 // test16rr %src_reg, %src_reg, implicit-def $eflags
1117 //
1118 // If subsequent readers use a subset of bits that don't change
1119 // after `and*` instructions, it's likely that the test64rr could
1120 // be optimized away.
1121 for (const MachineInstr &Instr :
1122 make_range(std::next(MachineBasicBlock::iterator(VregDefInstr)),
1123 MachineBasicBlock::iterator(CmpValDefInstr))) {
1124 // There are instructions between 'VregDefInstr' and
1125 // 'CmpValDefInstr' that modifies EFLAGS.
1126 if (Instr.modifiesRegister(X86::EFLAGS, TRI))
1127 return false;
1128 }
1129
1130 *AndInstr = VregDefInstr;
1131
1132 // AND instruction will essentially update SF and clear OF, so
1133 // NoSignFlag should be false in the sense that SF is modified by `AND`.
1134 //
1135 // However, the implementation artifically sets `NoSignFlag` to true
1136 // to poison the SF bit; that is to say, if SF is looked at later, the
1137 // optimization (to erase TEST64rr) will be disabled.
1138 //
1139 // The reason to poison SF bit is that SF bit value could be different
1140 // in the `AND` and `TEST` operation; signed bit is not known for `AND`,
1141 // and is known to be 0 as a result of `TEST64rr`.
1142 //
1143 // FIXME: As opposed to poisoning the SF bit directly, consider peeking into
1144 // the AND instruction and using the static information to guide peephole
1145 // optimization if possible. For example, it's possible to fold a
1146 // conditional move into a copy if the relevant EFLAG bits could be deduced
1147 // from an immediate operand of and operation.
1148 //
1149 NoSignFlag = true;
1150 // ClearsOverflowFlag is true for AND operation (no surprise).
1151 ClearsOverflowFlag = true;
1152 return true;
1153 }
1154 return false;
1155}
1156
1158 unsigned Opc, bool AllowSP, Register &NewSrc,
1159 unsigned &NewSrcSubReg, bool &isKill,
1160 MachineOperand &ImplicitOp, LiveVariables *LV,
1161 LiveIntervals *LIS) const {
1162 MachineFunction &MF = *MI.getParent()->getParent();
1163 const TargetRegisterClass *RC;
1164 if (AllowSP) {
1165 RC = Opc != X86::LEA32r ? &X86::GR64RegClass : &X86::GR32RegClass;
1166 } else {
1167 RC = Opc != X86::LEA32r ? &X86::GR64_NOSPRegClass : &X86::GR32_NOSPRegClass;
1168 }
1169 Register SrcReg = Src.getReg();
1170 unsigned SubReg = Src.getSubReg();
1171 isKill = MI.killsRegister(SrcReg, /*TRI=*/nullptr);
1172
1173 NewSrcSubReg = X86::NoSubRegister;
1174
1175 // For both LEA64 and LEA32 the register already has essentially the right
1176 // type (32-bit or 64-bit) we may just need to forbid SP.
1177 if (Opc != X86::LEA64_32r) {
1178 NewSrc = SrcReg;
1179 NewSrcSubReg = SubReg;
1180 assert(!Src.isUndef() && "Undef op doesn't need optimization");
1181
1182 if (NewSrc.isVirtual() && !MF.getRegInfo().constrainRegClass(NewSrc, RC))
1183 return false;
1184
1185 return true;
1186 }
1187
1188 // This is for an LEA64_32r and incoming registers are 32-bit. One way or
1189 // another we need to add 64-bit registers to the final MI.
1190 if (SrcReg.isPhysical()) {
1191 ImplicitOp = Src;
1192 ImplicitOp.setImplicit();
1193
1194 NewSrc = getX86SubSuperRegister(SrcReg, 64);
1195 assert(!SubReg && "no superregister for source");
1196 assert(NewSrc.isValid() && "Invalid Operand");
1197 assert(!Src.isUndef() && "Undef op doesn't need optimization");
1198 } else {
1199 // Virtual register of the wrong class, we have to create a temporary 64-bit
1200 // vreg to feed into the LEA.
1201 NewSrc = MF.getRegInfo().createVirtualRegister(RC);
1202 NewSrcSubReg = X86::NoSubRegister;
1203 MachineInstr *Copy =
1204 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(TargetOpcode::COPY))
1205 .addReg(NewSrc, RegState::Define | RegState::Undef, X86::sub_32bit)
1206 .addReg(SrcReg, getKillRegState(isKill), SubReg);
1207
1208 // Which is obviously going to be dead after we're done with it.
1209 isKill = true;
1210
1211 if (LV)
1212 LV->replaceKillInstruction(SrcReg, MI, *Copy);
1213
1214 if (LIS) {
1215 SlotIndex CopyIdx = LIS->InsertMachineInstrInMaps(*Copy);
1216 SlotIndex Idx = LIS->getInstructionIndex(MI);
1217 LiveInterval &LI = LIS->getInterval(SrcReg);
1219 if (S->end.getBaseIndex() == Idx)
1220 S->end = CopyIdx.getRegSlot();
1221 }
1222 }
1223
1224 // We've set all the parameters without issue.
1225 return true;
1226}
1227
1228MachineInstr *X86InstrInfo::convertToThreeAddressWithLEA(unsigned MIOpc,
1230 LiveVariables *LV,
1231 LiveIntervals *LIS,
1232 bool Is8BitOp) const {
1233 // We handle 8-bit adds and various 16-bit opcodes in the switch below.
1234 MachineBasicBlock &MBB = *MI.getParent();
1235 MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
1236 assert((Is8BitOp ||
1237 RegInfo.getTargetRegisterInfo()->getRegSizeInBits(
1238 *RegInfo.getRegClass(MI.getOperand(0).getReg())) == 16) &&
1239 "Unexpected type for LEA transform");
1240
1241 // TODO: For a 32-bit target, we need to adjust the LEA variables with
1242 // something like this:
1243 // Opcode = X86::LEA32r;
1244 // InRegLEA = RegInfo.createVirtualRegister(&X86::GR32_NOSPRegClass);
1245 // OutRegLEA =
1246 // Is8BitOp ? RegInfo.createVirtualRegister(&X86::GR32ABCD_RegClass)
1247 // : RegInfo.createVirtualRegister(&X86::GR32RegClass);
1248 if (!Subtarget.is64Bit())
1249 return nullptr;
1250
1251 unsigned Opcode = X86::LEA64_32r;
1252 Register InRegLEA = RegInfo.createVirtualRegister(&X86::GR64_NOSPRegClass);
1253 Register OutRegLEA = RegInfo.createVirtualRegister(&X86::GR32RegClass);
1254 Register InRegLEA2;
1255
1256 // Build and insert into an implicit UNDEF value. This is OK because
1257 // we will be shifting and then extracting the lower 8/16-bits.
1258 // This has the potential to cause partial register stall. e.g.
1259 // movw (%rbp,%rcx,2), %dx
1260 // leal -65(%rdx), %esi
1261 // But testing has shown this *does* help performance in 64-bit mode (at
1262 // least on modern x86 machines).
1263 MachineBasicBlock::iterator MBBI = MI.getIterator();
1264 Register Dest = MI.getOperand(0).getReg();
1265 Register Src = MI.getOperand(1).getReg();
1266 unsigned SrcSubReg = MI.getOperand(1).getSubReg();
1267 Register Src2;
1268 unsigned Src2SubReg;
1269 bool IsDead = MI.getOperand(0).isDead();
1270 bool IsKill = MI.getOperand(1).isKill();
1271 unsigned SubReg = Is8BitOp ? X86::sub_8bit : X86::sub_16bit;
1272 assert(!MI.getOperand(1).isUndef() && "Undef op doesn't need optimization");
1273 MachineInstr *ImpDef =
1274 BuildMI(MBB, MBBI, MI.getDebugLoc(), get(X86::IMPLICIT_DEF), InRegLEA);
1275 MachineInstr *InsMI =
1276 BuildMI(MBB, MBBI, MI.getDebugLoc(), get(TargetOpcode::COPY))
1277 .addReg(InRegLEA, RegState::Define, SubReg)
1278 .addReg(Src, getKillRegState(IsKill), SrcSubReg);
1279 MachineInstr *ImpDef2 = nullptr;
1280 MachineInstr *InsMI2 = nullptr;
1281
1283 BuildMI(MBB, MBBI, MI.getDebugLoc(), get(Opcode), OutRegLEA);
1284#define CASE_NF(OP) \
1285 case X86::OP: \
1286 case X86::OP##_NF:
1287 switch (MIOpc) {
1288 default:
1289 llvm_unreachable("Unreachable!");
1290 CASE_NF(SHL8ri)
1291 CASE_NF(SHL16ri) {
1292 unsigned ShAmt = MI.getOperand(2).getImm();
1293 MIB.addReg(0)
1294 .addImm(1LL << ShAmt)
1295 .addReg(InRegLEA, RegState::Kill)
1296 .addImm(0)
1297 .addReg(0);
1298 break;
1299 }
1300 CASE_NF(INC8r)
1301 CASE_NF(INC16r)
1302 addRegOffset(MIB, InRegLEA, true, 1);
1303 break;
1304 CASE_NF(DEC8r)
1305 CASE_NF(DEC16r)
1306 addRegOffset(MIB, InRegLEA, true, -1);
1307 break;
1308 CASE_NF(ADD8ri)
1309 CASE_NF(ADD16ri)
1310 case X86::ADD8ri_DB:
1311 case X86::ADD16ri_DB:
1312 addRegOffset(MIB, InRegLEA, true, MI.getOperand(2).getImm());
1313 break;
1314 CASE_NF(ADD8rr)
1315 CASE_NF(ADD16rr)
1316 case X86::ADD8rr_DB:
1317 case X86::ADD16rr_DB: {
1318 Src2 = MI.getOperand(2).getReg();
1319 Src2SubReg = MI.getOperand(2).getSubReg();
1320 bool IsKill2 = MI.getOperand(2).isKill();
1321 assert(!MI.getOperand(2).isUndef() && "Undef op doesn't need optimization");
1322 if (Src == Src2) {
1323 // ADD8rr/ADD16rr killed %reg1028, %reg1028
1324 // just a single insert_subreg.
1325 addRegReg(MIB, InRegLEA, true, X86::NoSubRegister, InRegLEA, false,
1326 X86::NoSubRegister);
1327 } else {
1328 if (Subtarget.is64Bit())
1329 InRegLEA2 = RegInfo.createVirtualRegister(&X86::GR64_NOSPRegClass);
1330 else
1331 InRegLEA2 = RegInfo.createVirtualRegister(&X86::GR32_NOSPRegClass);
1332 // Build and insert into an implicit UNDEF value. This is OK because
1333 // we will be shifting and then extracting the lower 8/16-bits.
1334 ImpDef2 = BuildMI(MBB, &*MIB, MI.getDebugLoc(), get(X86::IMPLICIT_DEF),
1335 InRegLEA2);
1336 InsMI2 = BuildMI(MBB, &*MIB, MI.getDebugLoc(), get(TargetOpcode::COPY))
1337 .addReg(InRegLEA2, RegState::Define, SubReg)
1338 .addReg(Src2, getKillRegState(IsKill2), Src2SubReg);
1339 addRegReg(MIB, InRegLEA, true, X86::NoSubRegister, InRegLEA2, true,
1340 X86::NoSubRegister);
1341 }
1342 if (LV && IsKill2 && InsMI2)
1343 LV->replaceKillInstruction(Src2, MI, *InsMI2);
1344 break;
1345 }
1346 }
1347
1348 MachineInstr *NewMI = MIB;
1349 MachineInstr *ExtMI =
1350 BuildMI(MBB, MBBI, MI.getDebugLoc(), get(TargetOpcode::COPY))
1352 .addReg(OutRegLEA, RegState::Kill, SubReg);
1353
1354 if (LV) {
1355 // Update live variables.
1356 LV->getVarInfo(InRegLEA).Kills.push_back(NewMI);
1357 if (InRegLEA2)
1358 LV->getVarInfo(InRegLEA2).Kills.push_back(NewMI);
1359 LV->getVarInfo(OutRegLEA).Kills.push_back(ExtMI);
1360 if (IsKill)
1361 LV->replaceKillInstruction(Src, MI, *InsMI);
1362 if (IsDead)
1363 LV->replaceKillInstruction(Dest, MI, *ExtMI);
1364 }
1365
1366 if (LIS) {
1367 LIS->InsertMachineInstrInMaps(*ImpDef);
1368 SlotIndex InsIdx = LIS->InsertMachineInstrInMaps(*InsMI);
1369 if (ImpDef2)
1370 LIS->InsertMachineInstrInMaps(*ImpDef2);
1371 SlotIndex Ins2Idx;
1372 if (InsMI2)
1373 Ins2Idx = LIS->InsertMachineInstrInMaps(*InsMI2);
1374 SlotIndex NewIdx = LIS->ReplaceMachineInstrInMaps(MI, *NewMI);
1375 SlotIndex ExtIdx = LIS->InsertMachineInstrInMaps(*ExtMI);
1376
1377 // Drop the dead EFLAGS def MI had; the replacement does not define EFLAGS.
1378 LIS->removePhysRegDefAt(X86::EFLAGS, NewIdx.getRegSlot());
1379
1380 LIS->getInterval(InRegLEA);
1381 LIS->getInterval(OutRegLEA);
1382 if (InRegLEA2)
1383 LIS->getInterval(InRegLEA2);
1384
1385 // Move the use of Src up to InsMI.
1386 LiveInterval &SrcLI = LIS->getInterval(Src);
1387 LiveRange::Segment *SrcSeg = SrcLI.getSegmentContaining(NewIdx);
1388 if (SrcSeg->end == NewIdx.getRegSlot())
1389 SrcSeg->end = InsIdx.getRegSlot();
1390
1391 if (InsMI2) {
1392 // Move the use of Src2 up to InsMI2.
1393 LiveInterval &Src2LI = LIS->getInterval(Src2);
1394 LiveRange::Segment *Src2Seg = Src2LI.getSegmentContaining(NewIdx);
1395 if (Src2Seg->end == NewIdx.getRegSlot())
1396 Src2Seg->end = Ins2Idx.getRegSlot();
1397 }
1398
1399 // Move the definition of Dest down to ExtMI.
1400 LiveInterval &DestLI = LIS->getInterval(Dest);
1401 LiveRange::Segment *DestSeg =
1402 DestLI.getSegmentContaining(NewIdx.getRegSlot());
1403 assert(DestSeg->start == NewIdx.getRegSlot() &&
1404 DestSeg->valno->def == NewIdx.getRegSlot());
1405 DestSeg->start = ExtIdx.getRegSlot();
1406 DestSeg->valno->def = ExtIdx.getRegSlot();
1407 }
1408
1409 return ExtMI;
1410}
1411
1412/// This method must be implemented by targets that
1413/// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
1414/// may be able to convert a two-address instruction into a true
1415/// three-address instruction on demand. This allows the X86 target (for
1416/// example) to convert ADD and SHL instructions into LEA instructions if they
1417/// would require register copies due to two-addressness.
1418///
1419/// This method returns a null pointer if the transformation cannot be
1420/// performed, otherwise it returns the new instruction.
1421///
1423 LiveVariables *LV,
1424 LiveIntervals *LIS) const {
1425 // The following opcodes also sets the condition code register(s). Only
1426 // convert them to equivalent lea if the condition code register def's
1427 // are dead!
1429 return nullptr;
1430
1431 MachineFunction &MF = *MI.getParent()->getParent();
1432 // All instructions input are two-addr instructions. Get the known operands.
1433 const MachineOperand &Dest = MI.getOperand(0);
1434 const MachineOperand &Src = MI.getOperand(1);
1435
1436 // Ideally, operations with undef should be folded before we get here, but we
1437 // can't guarantee it. Bail out because optimizing undefs is a waste of time.
1438 // Without this, we have to forward undef state to new register operands to
1439 // avoid machine verifier errors.
1440 if (Src.isUndef())
1441 return nullptr;
1442 if (MI.getNumOperands() > 2)
1443 if (MI.getOperand(2).isReg() && MI.getOperand(2).isUndef())
1444 return nullptr;
1445
1446 MachineInstr *NewMI = nullptr;
1447 Register SrcReg, SrcReg2;
1448 unsigned SrcSubReg, SrcSubReg2;
1449 bool Is64Bit = Subtarget.is64Bit();
1450
1451 bool Is8BitOp = false;
1452 unsigned NumRegOperands = 2;
1453 unsigned MIOpc = MI.getOpcode();
1454 switch (MIOpc) {
1455 default:
1456 llvm_unreachable("Unreachable!");
1457 CASE_NF(SHL64ri) {
1458 assert(MI.getNumOperands() >= 3 && "Unknown shift instruction!");
1459 unsigned ShAmt = getTruncatedShiftCount(MI, 2);
1460 if (!isTruncatedShiftCountForLEA(ShAmt))
1461 return nullptr;
1462
1463 // LEA can't handle RSP.
1464 if (Src.getReg().isVirtual() && !MF.getRegInfo().constrainRegClass(
1465 Src.getReg(), &X86::GR64_NOSPRegClass))
1466 return nullptr;
1467
1468 NewMI = BuildMI(MF, MI.getDebugLoc(), get(X86::LEA64r))
1469 .add(Dest)
1470 .addReg(0)
1471 .addImm(1LL << ShAmt)
1472 .add(Src)
1473 .addImm(0)
1474 .addReg(0);
1475 break;
1476 }
1477 CASE_NF(SHL32ri) {
1478 assert(MI.getNumOperands() >= 3 && "Unknown shift instruction!");
1479 unsigned ShAmt = getTruncatedShiftCount(MI, 2);
1480 if (!isTruncatedShiftCountForLEA(ShAmt))
1481 return nullptr;
1482
1483 unsigned Opc = Is64Bit ? X86::LEA64_32r : X86::LEA32r;
1484
1485 // LEA can't handle ESP.
1486 bool isKill;
1487 MachineOperand ImplicitOp = MachineOperand::CreateReg(0, false);
1488 if (!classifyLEAReg(MI, Src, Opc, /*AllowSP=*/false, SrcReg, SrcSubReg,
1489 isKill, ImplicitOp, LV, LIS))
1490 return nullptr;
1491
1493 BuildMI(MF, MI.getDebugLoc(), get(Opc))
1494 .add(Dest)
1495 .addReg(0)
1496 .addImm(1LL << ShAmt)
1497 .addReg(SrcReg, getKillRegState(isKill), SrcSubReg)
1498 .addImm(0)
1499 .addReg(0);
1500 if (ImplicitOp.getReg() != 0)
1501 MIB.add(ImplicitOp);
1502 NewMI = MIB;
1503
1504 // Add kills if classifyLEAReg created a new register.
1505 if (LV && SrcReg != Src.getReg())
1506 LV->getVarInfo(SrcReg).Kills.push_back(NewMI);
1507 break;
1508 }
1509 CASE_NF(SHL8ri)
1510 Is8BitOp = true;
1511 [[fallthrough]];
1512 CASE_NF(SHL16ri) {
1513 assert(MI.getNumOperands() >= 3 && "Unknown shift instruction!");
1514 unsigned ShAmt = getTruncatedShiftCount(MI, 2);
1515 if (!isTruncatedShiftCountForLEA(ShAmt))
1516 return nullptr;
1517 return convertToThreeAddressWithLEA(MIOpc, MI, LV, LIS, Is8BitOp);
1518 }
1519 CASE_NF(INC64r)
1520 CASE_NF(INC32r) {
1521 assert(MI.getNumOperands() >= 2 && "Unknown inc instruction!");
1522 unsigned Opc = (MIOpc == X86::INC64r || MIOpc == X86::INC64r_NF)
1523 ? X86::LEA64r
1524 : (Is64Bit ? X86::LEA64_32r : X86::LEA32r);
1525 bool isKill;
1526 MachineOperand ImplicitOp = MachineOperand::CreateReg(0, false);
1527 if (!classifyLEAReg(MI, Src, Opc, /*AllowSP=*/false, SrcReg, SrcSubReg,
1528 isKill, ImplicitOp, LV, LIS))
1529 return nullptr;
1530
1531 MachineInstrBuilder MIB = BuildMI(MF, MI.getDebugLoc(), get(Opc))
1532 .add(Dest)
1533 .addReg(SrcReg, getKillRegState(isKill));
1534 if (ImplicitOp.getReg() != 0)
1535 MIB.add(ImplicitOp);
1536
1537 NewMI = addOffset(MIB, 1);
1538
1539 // Add kills if classifyLEAReg created a new register.
1540 if (LV && SrcReg != Src.getReg())
1541 LV->getVarInfo(SrcReg).Kills.push_back(NewMI);
1542 break;
1543 }
1544 CASE_NF(DEC64r)
1545 CASE_NF(DEC32r) {
1546 assert(MI.getNumOperands() >= 2 && "Unknown dec instruction!");
1547 unsigned Opc = (MIOpc == X86::DEC64r || MIOpc == X86::DEC64r_NF)
1548 ? X86::LEA64r
1549 : (Is64Bit ? X86::LEA64_32r : X86::LEA32r);
1550
1551 bool isKill;
1552 MachineOperand ImplicitOp = MachineOperand::CreateReg(0, false);
1553 if (!classifyLEAReg(MI, Src, Opc, /*AllowSP=*/false, SrcReg, SrcSubReg,
1554 isKill, ImplicitOp, LV, LIS))
1555 return nullptr;
1556
1557 MachineInstrBuilder MIB = BuildMI(MF, MI.getDebugLoc(), get(Opc))
1558 .add(Dest)
1559 .addReg(SrcReg, getKillRegState(isKill));
1560 if (ImplicitOp.getReg() != 0)
1561 MIB.add(ImplicitOp);
1562
1563 NewMI = addOffset(MIB, -1);
1564
1565 // Add kills if classifyLEAReg created a new register.
1566 if (LV && SrcReg != Src.getReg())
1567 LV->getVarInfo(SrcReg).Kills.push_back(NewMI);
1568 break;
1569 }
1570 CASE_NF(DEC8r)
1571 CASE_NF(INC8r)
1572 Is8BitOp = true;
1573 [[fallthrough]];
1574 CASE_NF(DEC16r)
1575 CASE_NF(INC16r)
1576 return convertToThreeAddressWithLEA(MIOpc, MI, LV, LIS, Is8BitOp);
1577 CASE_NF(ADD64rr)
1578 CASE_NF(ADD32rr)
1579 case X86::ADD64rr_DB:
1580 case X86::ADD32rr_DB: {
1581 assert(MI.getNumOperands() >= 3 && "Unknown add instruction!");
1582 unsigned Opc;
1583 if (MIOpc == X86::ADD64rr || MIOpc == X86::ADD64rr_NF ||
1584 MIOpc == X86::ADD64rr_DB)
1585 Opc = X86::LEA64r;
1586 else
1587 Opc = Is64Bit ? X86::LEA64_32r : X86::LEA32r;
1588
1589 const MachineOperand &Src2 = MI.getOperand(2);
1590 bool isKill2;
1591 MachineOperand ImplicitOp2 = MachineOperand::CreateReg(0, false);
1592 if (!classifyLEAReg(MI, Src2, Opc, /*AllowSP=*/false, SrcReg2, SrcSubReg2,
1593 isKill2, ImplicitOp2, LV, LIS))
1594 return nullptr;
1595
1596 bool isKill;
1597 MachineOperand ImplicitOp = MachineOperand::CreateReg(0, false);
1598 if (Src.getReg() == Src2.getReg()) {
1599 // Don't call classify LEAReg a second time on the same register, in case
1600 // the first call inserted a COPY from Src2 and marked it as killed.
1601 isKill = isKill2;
1602 SrcReg = SrcReg2;
1603 SrcSubReg = SrcSubReg2;
1604 } else {
1605 if (!classifyLEAReg(MI, Src, Opc, /*AllowSP=*/true, SrcReg, SrcSubReg,
1606 isKill, ImplicitOp, LV, LIS))
1607 return nullptr;
1608 }
1609
1610 MachineInstrBuilder MIB = BuildMI(MF, MI.getDebugLoc(), get(Opc)).add(Dest);
1611 if (ImplicitOp.getReg() != 0)
1612 MIB.add(ImplicitOp);
1613 if (ImplicitOp2.getReg() != 0)
1614 MIB.add(ImplicitOp2);
1615
1616 NewMI =
1617 addRegReg(MIB, SrcReg, isKill, SrcSubReg, SrcReg2, isKill2, SrcSubReg2);
1618
1619 // Add kills if classifyLEAReg created a new register.
1620 if (LV) {
1621 if (SrcReg2 != Src2.getReg())
1622 LV->getVarInfo(SrcReg2).Kills.push_back(NewMI);
1623 if (SrcReg != SrcReg2 && SrcReg != Src.getReg())
1624 LV->getVarInfo(SrcReg).Kills.push_back(NewMI);
1625 }
1626 NumRegOperands = 3;
1627 break;
1628 }
1629 CASE_NF(ADD8rr)
1630 case X86::ADD8rr_DB:
1631 Is8BitOp = true;
1632 [[fallthrough]];
1633 CASE_NF(ADD16rr)
1634 case X86::ADD16rr_DB:
1635 return convertToThreeAddressWithLEA(MIOpc, MI, LV, LIS, Is8BitOp);
1636 CASE_NF(ADD64ri32)
1637 case X86::ADD64ri32_DB:
1638 assert(MI.getNumOperands() >= 3 && "Unknown add instruction!");
1639 NewMI = addOffset(
1640 BuildMI(MF, MI.getDebugLoc(), get(X86::LEA64r)).add(Dest).add(Src),
1641 MI.getOperand(2));
1642 break;
1643 CASE_NF(ADD32ri)
1644 case X86::ADD32ri_DB: {
1645 assert(MI.getNumOperands() >= 3 && "Unknown add instruction!");
1646 unsigned Opc = Is64Bit ? X86::LEA64_32r : X86::LEA32r;
1647
1648 bool isKill;
1649 MachineOperand ImplicitOp = MachineOperand::CreateReg(0, false);
1650 if (!classifyLEAReg(MI, Src, Opc, /*AllowSP=*/true, SrcReg, SrcSubReg,
1651 isKill, ImplicitOp, LV, LIS))
1652 return nullptr;
1653
1655 BuildMI(MF, MI.getDebugLoc(), get(Opc))
1656 .add(Dest)
1657 .addReg(SrcReg, getKillRegState(isKill), SrcSubReg);
1658 if (ImplicitOp.getReg() != 0)
1659 MIB.add(ImplicitOp);
1660
1661 NewMI = addOffset(MIB, MI.getOperand(2));
1662
1663 // Add kills if classifyLEAReg created a new register.
1664 if (LV && SrcReg != Src.getReg())
1665 LV->getVarInfo(SrcReg).Kills.push_back(NewMI);
1666 break;
1667 }
1668 CASE_NF(ADD8ri)
1669 case X86::ADD8ri_DB:
1670 Is8BitOp = true;
1671 [[fallthrough]];
1672 CASE_NF(ADD16ri)
1673 case X86::ADD16ri_DB:
1674 return convertToThreeAddressWithLEA(MIOpc, MI, LV, LIS, Is8BitOp);
1675 CASE_NF(SUB8ri)
1676 CASE_NF(SUB16ri)
1677 /// FIXME: Support these similar to ADD8ri/ADD16ri*.
1678 return nullptr;
1679 CASE_NF(SUB32ri) {
1680 if (!MI.getOperand(2).isImm())
1681 return nullptr;
1682 int64_t Imm = MI.getOperand(2).getImm();
1683 if (!isInt<32>(-Imm))
1684 return nullptr;
1685
1686 assert(MI.getNumOperands() >= 3 && "Unknown add instruction!");
1687 unsigned Opc = Is64Bit ? X86::LEA64_32r : X86::LEA32r;
1688
1689 bool isKill;
1690 MachineOperand ImplicitOp = MachineOperand::CreateReg(0, false);
1691 if (!classifyLEAReg(MI, Src, Opc, /*AllowSP=*/true, SrcReg, SrcSubReg,
1692 isKill, ImplicitOp, LV, LIS))
1693 return nullptr;
1694
1696 BuildMI(MF, MI.getDebugLoc(), get(Opc))
1697 .add(Dest)
1698 .addReg(SrcReg, getKillRegState(isKill), SrcSubReg);
1699 if (ImplicitOp.getReg() != 0)
1700 MIB.add(ImplicitOp);
1701
1702 NewMI = addOffset(MIB, -Imm);
1703
1704 // Add kills if classifyLEAReg created a new register.
1705 if (LV && SrcReg != Src.getReg())
1706 LV->getVarInfo(SrcReg).Kills.push_back(NewMI);
1707 break;
1708 }
1709
1710 CASE_NF(SUB64ri32) {
1711 if (!MI.getOperand(2).isImm())
1712 return nullptr;
1713 int64_t Imm = MI.getOperand(2).getImm();
1714 if (!isInt<32>(-Imm))
1715 return nullptr;
1716
1717 assert(MI.getNumOperands() >= 3 && "Unknown sub instruction!");
1718
1720 BuildMI(MF, MI.getDebugLoc(), get(X86::LEA64r)).add(Dest).add(Src);
1721 NewMI = addOffset(MIB, -Imm);
1722 break;
1723 }
1724
1725 case X86::VMOVDQU8Z128rmk:
1726 case X86::VMOVDQU8Z256rmk:
1727 case X86::VMOVDQU8Zrmk:
1728 case X86::VMOVDQU16Z128rmk:
1729 case X86::VMOVDQU16Z256rmk:
1730 case X86::VMOVDQU16Zrmk:
1731 case X86::VMOVDQU32Z128rmk:
1732 case X86::VMOVDQA32Z128rmk:
1733 case X86::VMOVDQU32Z256rmk:
1734 case X86::VMOVDQA32Z256rmk:
1735 case X86::VMOVDQU32Zrmk:
1736 case X86::VMOVDQA32Zrmk:
1737 case X86::VMOVDQU64Z128rmk:
1738 case X86::VMOVDQA64Z128rmk:
1739 case X86::VMOVDQU64Z256rmk:
1740 case X86::VMOVDQA64Z256rmk:
1741 case X86::VMOVDQU64Zrmk:
1742 case X86::VMOVDQA64Zrmk:
1743 case X86::VMOVUPDZ128rmk:
1744 case X86::VMOVAPDZ128rmk:
1745 case X86::VMOVUPDZ256rmk:
1746 case X86::VMOVAPDZ256rmk:
1747 case X86::VMOVUPDZrmk:
1748 case X86::VMOVAPDZrmk:
1749 case X86::VMOVUPSZ128rmk:
1750 case X86::VMOVAPSZ128rmk:
1751 case X86::VMOVUPSZ256rmk:
1752 case X86::VMOVAPSZ256rmk:
1753 case X86::VMOVUPSZrmk:
1754 case X86::VMOVAPSZrmk:
1755 case X86::VBROADCASTSDZ256rmk:
1756 case X86::VBROADCASTSDZrmk:
1757 case X86::VBROADCASTSSZ128rmk:
1758 case X86::VBROADCASTSSZ256rmk:
1759 case X86::VBROADCASTSSZrmk:
1760 case X86::VPBROADCASTDZ128rmk:
1761 case X86::VPBROADCASTDZ256rmk:
1762 case X86::VPBROADCASTDZrmk:
1763 case X86::VPBROADCASTQZ128rmk:
1764 case X86::VPBROADCASTQZ256rmk:
1765 case X86::VPBROADCASTQZrmk: {
1766 unsigned Opc;
1767 switch (MIOpc) {
1768 default:
1769 llvm_unreachable("Unreachable!");
1770 case X86::VMOVDQU8Z128rmk:
1771 Opc = X86::VPBLENDMBZ128rmk;
1772 break;
1773 case X86::VMOVDQU8Z256rmk:
1774 Opc = X86::VPBLENDMBZ256rmk;
1775 break;
1776 case X86::VMOVDQU8Zrmk:
1777 Opc = X86::VPBLENDMBZrmk;
1778 break;
1779 case X86::VMOVDQU16Z128rmk:
1780 Opc = X86::VPBLENDMWZ128rmk;
1781 break;
1782 case X86::VMOVDQU16Z256rmk:
1783 Opc = X86::VPBLENDMWZ256rmk;
1784 break;
1785 case X86::VMOVDQU16Zrmk:
1786 Opc = X86::VPBLENDMWZrmk;
1787 break;
1788 case X86::VMOVDQU32Z128rmk:
1789 Opc = X86::VPBLENDMDZ128rmk;
1790 break;
1791 case X86::VMOVDQU32Z256rmk:
1792 Opc = X86::VPBLENDMDZ256rmk;
1793 break;
1794 case X86::VMOVDQU32Zrmk:
1795 Opc = X86::VPBLENDMDZrmk;
1796 break;
1797 case X86::VMOVDQU64Z128rmk:
1798 Opc = X86::VPBLENDMQZ128rmk;
1799 break;
1800 case X86::VMOVDQU64Z256rmk:
1801 Opc = X86::VPBLENDMQZ256rmk;
1802 break;
1803 case X86::VMOVDQU64Zrmk:
1804 Opc = X86::VPBLENDMQZrmk;
1805 break;
1806 case X86::VMOVUPDZ128rmk:
1807 Opc = X86::VBLENDMPDZ128rmk;
1808 break;
1809 case X86::VMOVUPDZ256rmk:
1810 Opc = X86::VBLENDMPDZ256rmk;
1811 break;
1812 case X86::VMOVUPDZrmk:
1813 Opc = X86::VBLENDMPDZrmk;
1814 break;
1815 case X86::VMOVUPSZ128rmk:
1816 Opc = X86::VBLENDMPSZ128rmk;
1817 break;
1818 case X86::VMOVUPSZ256rmk:
1819 Opc = X86::VBLENDMPSZ256rmk;
1820 break;
1821 case X86::VMOVUPSZrmk:
1822 Opc = X86::VBLENDMPSZrmk;
1823 break;
1824 case X86::VMOVDQA32Z128rmk:
1825 Opc = X86::VPBLENDMDZ128rmk;
1826 break;
1827 case X86::VMOVDQA32Z256rmk:
1828 Opc = X86::VPBLENDMDZ256rmk;
1829 break;
1830 case X86::VMOVDQA32Zrmk:
1831 Opc = X86::VPBLENDMDZrmk;
1832 break;
1833 case X86::VMOVDQA64Z128rmk:
1834 Opc = X86::VPBLENDMQZ128rmk;
1835 break;
1836 case X86::VMOVDQA64Z256rmk:
1837 Opc = X86::VPBLENDMQZ256rmk;
1838 break;
1839 case X86::VMOVDQA64Zrmk:
1840 Opc = X86::VPBLENDMQZrmk;
1841 break;
1842 case X86::VMOVAPDZ128rmk:
1843 Opc = X86::VBLENDMPDZ128rmk;
1844 break;
1845 case X86::VMOVAPDZ256rmk:
1846 Opc = X86::VBLENDMPDZ256rmk;
1847 break;
1848 case X86::VMOVAPDZrmk:
1849 Opc = X86::VBLENDMPDZrmk;
1850 break;
1851 case X86::VMOVAPSZ128rmk:
1852 Opc = X86::VBLENDMPSZ128rmk;
1853 break;
1854 case X86::VMOVAPSZ256rmk:
1855 Opc = X86::VBLENDMPSZ256rmk;
1856 break;
1857 case X86::VMOVAPSZrmk:
1858 Opc = X86::VBLENDMPSZrmk;
1859 break;
1860 case X86::VBROADCASTSDZ256rmk:
1861 Opc = X86::VBLENDMPDZ256rmbk;
1862 break;
1863 case X86::VBROADCASTSDZrmk:
1864 Opc = X86::VBLENDMPDZrmbk;
1865 break;
1866 case X86::VBROADCASTSSZ128rmk:
1867 Opc = X86::VBLENDMPSZ128rmbk;
1868 break;
1869 case X86::VBROADCASTSSZ256rmk:
1870 Opc = X86::VBLENDMPSZ256rmbk;
1871 break;
1872 case X86::VBROADCASTSSZrmk:
1873 Opc = X86::VBLENDMPSZrmbk;
1874 break;
1875 case X86::VPBROADCASTDZ128rmk:
1876 Opc = X86::VPBLENDMDZ128rmbk;
1877 break;
1878 case X86::VPBROADCASTDZ256rmk:
1879 Opc = X86::VPBLENDMDZ256rmbk;
1880 break;
1881 case X86::VPBROADCASTDZrmk:
1882 Opc = X86::VPBLENDMDZrmbk;
1883 break;
1884 case X86::VPBROADCASTQZ128rmk:
1885 Opc = X86::VPBLENDMQZ128rmbk;
1886 break;
1887 case X86::VPBROADCASTQZ256rmk:
1888 Opc = X86::VPBLENDMQZ256rmbk;
1889 break;
1890 case X86::VPBROADCASTQZrmk:
1891 Opc = X86::VPBLENDMQZrmbk;
1892 break;
1893 }
1894
1895 NewMI = BuildMI(MF, MI.getDebugLoc(), get(Opc))
1896 .add(Dest)
1897 .add(MI.getOperand(2))
1898 .add(Src)
1899 .add(MI.getOperand(3))
1900 .add(MI.getOperand(4))
1901 .add(MI.getOperand(5))
1902 .add(MI.getOperand(6))
1903 .add(MI.getOperand(7));
1904 NumRegOperands = 4;
1905 break;
1906 }
1907
1908 case X86::VMOVDQU8Z128rrk:
1909 case X86::VMOVDQU8Z256rrk:
1910 case X86::VMOVDQU8Zrrk:
1911 case X86::VMOVDQU16Z128rrk:
1912 case X86::VMOVDQU16Z256rrk:
1913 case X86::VMOVDQU16Zrrk:
1914 case X86::VMOVDQU32Z128rrk:
1915 case X86::VMOVDQA32Z128rrk:
1916 case X86::VMOVDQU32Z256rrk:
1917 case X86::VMOVDQA32Z256rrk:
1918 case X86::VMOVDQU32Zrrk:
1919 case X86::VMOVDQA32Zrrk:
1920 case X86::VMOVDQU64Z128rrk:
1921 case X86::VMOVDQA64Z128rrk:
1922 case X86::VMOVDQU64Z256rrk:
1923 case X86::VMOVDQA64Z256rrk:
1924 case X86::VMOVDQU64Zrrk:
1925 case X86::VMOVDQA64Zrrk:
1926 case X86::VMOVUPDZ128rrk:
1927 case X86::VMOVAPDZ128rrk:
1928 case X86::VMOVUPDZ256rrk:
1929 case X86::VMOVAPDZ256rrk:
1930 case X86::VMOVUPDZrrk:
1931 case X86::VMOVAPDZrrk:
1932 case X86::VMOVUPSZ128rrk:
1933 case X86::VMOVAPSZ128rrk:
1934 case X86::VMOVUPSZ256rrk:
1935 case X86::VMOVAPSZ256rrk:
1936 case X86::VMOVUPSZrrk:
1937 case X86::VMOVAPSZrrk: {
1938 unsigned Opc;
1939 switch (MIOpc) {
1940 default:
1941 llvm_unreachable("Unreachable!");
1942 case X86::VMOVDQU8Z128rrk:
1943 Opc = X86::VPBLENDMBZ128rrk;
1944 break;
1945 case X86::VMOVDQU8Z256rrk:
1946 Opc = X86::VPBLENDMBZ256rrk;
1947 break;
1948 case X86::VMOVDQU8Zrrk:
1949 Opc = X86::VPBLENDMBZrrk;
1950 break;
1951 case X86::VMOVDQU16Z128rrk:
1952 Opc = X86::VPBLENDMWZ128rrk;
1953 break;
1954 case X86::VMOVDQU16Z256rrk:
1955 Opc = X86::VPBLENDMWZ256rrk;
1956 break;
1957 case X86::VMOVDQU16Zrrk:
1958 Opc = X86::VPBLENDMWZrrk;
1959 break;
1960 case X86::VMOVDQU32Z128rrk:
1961 Opc = X86::VPBLENDMDZ128rrk;
1962 break;
1963 case X86::VMOVDQU32Z256rrk:
1964 Opc = X86::VPBLENDMDZ256rrk;
1965 break;
1966 case X86::VMOVDQU32Zrrk:
1967 Opc = X86::VPBLENDMDZrrk;
1968 break;
1969 case X86::VMOVDQU64Z128rrk:
1970 Opc = X86::VPBLENDMQZ128rrk;
1971 break;
1972 case X86::VMOVDQU64Z256rrk:
1973 Opc = X86::VPBLENDMQZ256rrk;
1974 break;
1975 case X86::VMOVDQU64Zrrk:
1976 Opc = X86::VPBLENDMQZrrk;
1977 break;
1978 case X86::VMOVUPDZ128rrk:
1979 Opc = X86::VBLENDMPDZ128rrk;
1980 break;
1981 case X86::VMOVUPDZ256rrk:
1982 Opc = X86::VBLENDMPDZ256rrk;
1983 break;
1984 case X86::VMOVUPDZrrk:
1985 Opc = X86::VBLENDMPDZrrk;
1986 break;
1987 case X86::VMOVUPSZ128rrk:
1988 Opc = X86::VBLENDMPSZ128rrk;
1989 break;
1990 case X86::VMOVUPSZ256rrk:
1991 Opc = X86::VBLENDMPSZ256rrk;
1992 break;
1993 case X86::VMOVUPSZrrk:
1994 Opc = X86::VBLENDMPSZrrk;
1995 break;
1996 case X86::VMOVDQA32Z128rrk:
1997 Opc = X86::VPBLENDMDZ128rrk;
1998 break;
1999 case X86::VMOVDQA32Z256rrk:
2000 Opc = X86::VPBLENDMDZ256rrk;
2001 break;
2002 case X86::VMOVDQA32Zrrk:
2003 Opc = X86::VPBLENDMDZrrk;
2004 break;
2005 case X86::VMOVDQA64Z128rrk:
2006 Opc = X86::VPBLENDMQZ128rrk;
2007 break;
2008 case X86::VMOVDQA64Z256rrk:
2009 Opc = X86::VPBLENDMQZ256rrk;
2010 break;
2011 case X86::VMOVDQA64Zrrk:
2012 Opc = X86::VPBLENDMQZrrk;
2013 break;
2014 case X86::VMOVAPDZ128rrk:
2015 Opc = X86::VBLENDMPDZ128rrk;
2016 break;
2017 case X86::VMOVAPDZ256rrk:
2018 Opc = X86::VBLENDMPDZ256rrk;
2019 break;
2020 case X86::VMOVAPDZrrk:
2021 Opc = X86::VBLENDMPDZrrk;
2022 break;
2023 case X86::VMOVAPSZ128rrk:
2024 Opc = X86::VBLENDMPSZ128rrk;
2025 break;
2026 case X86::VMOVAPSZ256rrk:
2027 Opc = X86::VBLENDMPSZ256rrk;
2028 break;
2029 case X86::VMOVAPSZrrk:
2030 Opc = X86::VBLENDMPSZrrk;
2031 break;
2032 }
2033
2034 NewMI = BuildMI(MF, MI.getDebugLoc(), get(Opc))
2035 .add(Dest)
2036 .add(MI.getOperand(2))
2037 .add(Src)
2038 .add(MI.getOperand(3));
2039 NumRegOperands = 4;
2040 break;
2041 }
2042 }
2043#undef CASE_NF
2044
2045 if (!NewMI)
2046 return nullptr;
2047
2048 if (LV) { // Update live variables
2049 for (unsigned I = 0; I < NumRegOperands; ++I) {
2050 MachineOperand &Op = MI.getOperand(I);
2051 if (Op.isReg() && (Op.isDead() || Op.isKill()))
2052 LV->replaceKillInstruction(Op.getReg(), MI, *NewMI);
2053 }
2054 }
2055
2056 MachineBasicBlock &MBB = *MI.getParent();
2057 MBB.insert(MI.getIterator(), NewMI); // Insert the new inst
2058
2059 if (LIS) {
2060 // The replacement does not define EFLAGS; drop the dead EFLAGS def MI had.
2061 SlotIndex Idx = LIS->getInstructionIndex(MI);
2062 LIS->ReplaceMachineInstrInMaps(MI, *NewMI);
2063
2064 LIS->removePhysRegDefAt(X86::EFLAGS, Idx.getRegSlot());
2065 if (SrcReg)
2066 LIS->getInterval(SrcReg);
2067 if (SrcReg2)
2068 LIS->getInterval(SrcReg2);
2069 }
2070
2071 return NewMI;
2072}
2073
2074/// This determines which of three possible cases of a three source commute
2075/// the source indexes correspond to taking into account any mask operands.
2076/// All prevents commuting a passthru operand. Returns -1 if the commute isn't
2077/// possible.
2078/// Case 0 - Possible to commute the first and second operands.
2079/// Case 1 - Possible to commute the first and third operands.
2080/// Case 2 - Possible to commute the second and third operands.
2081static unsigned getThreeSrcCommuteCase(uint64_t TSFlags, unsigned SrcOpIdx1,
2082 unsigned SrcOpIdx2) {
2083 // Put the lowest index to SrcOpIdx1 to simplify the checks below.
2084 if (SrcOpIdx1 > SrcOpIdx2)
2085 std::swap(SrcOpIdx1, SrcOpIdx2);
2086
2087 unsigned Op1 = 1, Op2 = 2, Op3 = 3;
2088 if (X86II::isKMasked(TSFlags)) {
2089 Op2++;
2090 Op3++;
2091 }
2092
2093 if (SrcOpIdx1 == Op1 && SrcOpIdx2 == Op2)
2094 return 0;
2095 if (SrcOpIdx1 == Op1 && SrcOpIdx2 == Op3)
2096 return 1;
2097 if (SrcOpIdx1 == Op2 && SrcOpIdx2 == Op3)
2098 return 2;
2099 llvm_unreachable("Unknown three src commute case.");
2100}
2101
2103 const MachineInstr &MI, unsigned SrcOpIdx1, unsigned SrcOpIdx2,
2104 const X86InstrFMA3Group &FMA3Group) const {
2105
2106 unsigned Opc = MI.getOpcode();
2107
2108 // TODO: Commuting the 1st operand of FMA*_Int requires some additional
2109 // analysis. The commute optimization is legal only if all users of FMA*_Int
2110 // use only the lowest element of the FMA*_Int instruction. Such analysis are
2111 // not implemented yet. So, just return 0 in that case.
2112 // When such analysis are available this place will be the right place for
2113 // calling it.
2114 assert(!(FMA3Group.isIntrinsic() && (SrcOpIdx1 == 1 || SrcOpIdx2 == 1)) &&
2115 "Intrinsic instructions can't commute operand 1");
2116
2117 // Determine which case this commute is or if it can't be done.
2118 unsigned Case =
2119 getThreeSrcCommuteCase(MI.getDesc().TSFlags, SrcOpIdx1, SrcOpIdx2);
2120 assert(Case < 3 && "Unexpected case number!");
2121
2122 // Define the FMA forms mapping array that helps to map input FMA form
2123 // to output FMA form to preserve the operation semantics after
2124 // commuting the operands.
2125 const unsigned Form132Index = 0;
2126 const unsigned Form213Index = 1;
2127 const unsigned Form231Index = 2;
2128 static const unsigned FormMapping[][3] = {
2129 // 0: SrcOpIdx1 == 1 && SrcOpIdx2 == 2;
2130 // FMA132 A, C, b; ==> FMA231 C, A, b;
2131 // FMA213 B, A, c; ==> FMA213 A, B, c;
2132 // FMA231 C, A, b; ==> FMA132 A, C, b;
2133 {Form231Index, Form213Index, Form132Index},
2134 // 1: SrcOpIdx1 == 1 && SrcOpIdx2 == 3;
2135 // FMA132 A, c, B; ==> FMA132 B, c, A;
2136 // FMA213 B, a, C; ==> FMA231 C, a, B;
2137 // FMA231 C, a, B; ==> FMA213 B, a, C;
2138 {Form132Index, Form231Index, Form213Index},
2139 // 2: SrcOpIdx1 == 2 && SrcOpIdx2 == 3;
2140 // FMA132 a, C, B; ==> FMA213 a, B, C;
2141 // FMA213 b, A, C; ==> FMA132 b, C, A;
2142 // FMA231 c, A, B; ==> FMA231 c, B, A;
2143 {Form213Index, Form132Index, Form231Index}};
2144
2145 unsigned FMAForms[3];
2146 FMAForms[0] = FMA3Group.get132Opcode();
2147 FMAForms[1] = FMA3Group.get213Opcode();
2148 FMAForms[2] = FMA3Group.get231Opcode();
2149
2150 // Everything is ready, just adjust the FMA opcode and return it.
2151 for (unsigned FormIndex = 0; FormIndex < 3; FormIndex++)
2152 if (Opc == FMAForms[FormIndex])
2153 return FMAForms[FormMapping[Case][FormIndex]];
2154
2155 llvm_unreachable("Illegal FMA3 format");
2156}
2157
2158static void commuteVPTERNLOG(MachineInstr &MI, unsigned SrcOpIdx1,
2159 unsigned SrcOpIdx2) {
2160 // Determine which case this commute is or if it can't be done.
2161 unsigned Case =
2162 getThreeSrcCommuteCase(MI.getDesc().TSFlags, SrcOpIdx1, SrcOpIdx2);
2163 assert(Case < 3 && "Unexpected case value!");
2164
2165 // For each case we need to swap two pairs of bits in the final immediate.
2166 static const uint8_t SwapMasks[3][4] = {
2167 {0x04, 0x10, 0x08, 0x20}, // Swap bits 2/4 and 3/5.
2168 {0x02, 0x10, 0x08, 0x40}, // Swap bits 1/4 and 3/6.
2169 {0x02, 0x04, 0x20, 0x40}, // Swap bits 1/2 and 5/6.
2170 };
2171
2172 uint8_t Imm = MI.getOperand(MI.getNumOperands() - 1).getImm();
2173 // Clear out the bits we are swapping.
2174 uint8_t NewImm = Imm & ~(SwapMasks[Case][0] | SwapMasks[Case][1] |
2175 SwapMasks[Case][2] | SwapMasks[Case][3]);
2176 // If the immediate had a bit of the pair set, then set the opposite bit.
2177 if (Imm & SwapMasks[Case][0])
2178 NewImm |= SwapMasks[Case][1];
2179 if (Imm & SwapMasks[Case][1])
2180 NewImm |= SwapMasks[Case][0];
2181 if (Imm & SwapMasks[Case][2])
2182 NewImm |= SwapMasks[Case][3];
2183 if (Imm & SwapMasks[Case][3])
2184 NewImm |= SwapMasks[Case][2];
2185 MI.getOperand(MI.getNumOperands() - 1).setImm(NewImm);
2186}
2187
2188// Returns true if this is a VPERMI2 or VPERMT2 instruction that can be
2189// commuted.
2190static bool isCommutableVPERMV3Instruction(unsigned Opcode) {
2191#define VPERM_CASES(Suffix) \
2192 case X86::VPERMI2##Suffix##Z128rr: \
2193 case X86::VPERMT2##Suffix##Z128rr: \
2194 case X86::VPERMI2##Suffix##Z256rr: \
2195 case X86::VPERMT2##Suffix##Z256rr: \
2196 case X86::VPERMI2##Suffix##Zrr: \
2197 case X86::VPERMT2##Suffix##Zrr: \
2198 case X86::VPERMI2##Suffix##Z128rm: \
2199 case X86::VPERMT2##Suffix##Z128rm: \
2200 case X86::VPERMI2##Suffix##Z256rm: \
2201 case X86::VPERMT2##Suffix##Z256rm: \
2202 case X86::VPERMI2##Suffix##Zrm: \
2203 case X86::VPERMT2##Suffix##Zrm: \
2204 case X86::VPERMI2##Suffix##Z128rrkz: \
2205 case X86::VPERMT2##Suffix##Z128rrkz: \
2206 case X86::VPERMI2##Suffix##Z256rrkz: \
2207 case X86::VPERMT2##Suffix##Z256rrkz: \
2208 case X86::VPERMI2##Suffix##Zrrkz: \
2209 case X86::VPERMT2##Suffix##Zrrkz: \
2210 case X86::VPERMI2##Suffix##Z128rmkz: \
2211 case X86::VPERMT2##Suffix##Z128rmkz: \
2212 case X86::VPERMI2##Suffix##Z256rmkz: \
2213 case X86::VPERMT2##Suffix##Z256rmkz: \
2214 case X86::VPERMI2##Suffix##Zrmkz: \
2215 case X86::VPERMT2##Suffix##Zrmkz:
2216
2217#define VPERM_CASES_BROADCAST(Suffix) \
2218 VPERM_CASES(Suffix) \
2219 case X86::VPERMI2##Suffix##Z128rmb: \
2220 case X86::VPERMT2##Suffix##Z128rmb: \
2221 case X86::VPERMI2##Suffix##Z256rmb: \
2222 case X86::VPERMT2##Suffix##Z256rmb: \
2223 case X86::VPERMI2##Suffix##Zrmb: \
2224 case X86::VPERMT2##Suffix##Zrmb: \
2225 case X86::VPERMI2##Suffix##Z128rmbkz: \
2226 case X86::VPERMT2##Suffix##Z128rmbkz: \
2227 case X86::VPERMI2##Suffix##Z256rmbkz: \
2228 case X86::VPERMT2##Suffix##Z256rmbkz: \
2229 case X86::VPERMI2##Suffix##Zrmbkz: \
2230 case X86::VPERMT2##Suffix##Zrmbkz:
2231
2232 switch (Opcode) {
2233 default:
2234 return false;
2235 VPERM_CASES(B)
2240 VPERM_CASES(W)
2241 return true;
2242 }
2243#undef VPERM_CASES_BROADCAST
2244#undef VPERM_CASES
2245}
2246
2247// Returns commuted opcode for VPERMI2 and VPERMT2 instructions by switching
2248// from the I opcode to the T opcode and vice versa.
2249static unsigned getCommutedVPERMV3Opcode(unsigned Opcode) {
2250#define VPERM_CASES(Orig, New) \
2251 case X86::Orig##Z128rr: \
2252 return X86::New##Z128rr; \
2253 case X86::Orig##Z128rrkz: \
2254 return X86::New##Z128rrkz; \
2255 case X86::Orig##Z128rm: \
2256 return X86::New##Z128rm; \
2257 case X86::Orig##Z128rmkz: \
2258 return X86::New##Z128rmkz; \
2259 case X86::Orig##Z256rr: \
2260 return X86::New##Z256rr; \
2261 case X86::Orig##Z256rrkz: \
2262 return X86::New##Z256rrkz; \
2263 case X86::Orig##Z256rm: \
2264 return X86::New##Z256rm; \
2265 case X86::Orig##Z256rmkz: \
2266 return X86::New##Z256rmkz; \
2267 case X86::Orig##Zrr: \
2268 return X86::New##Zrr; \
2269 case X86::Orig##Zrrkz: \
2270 return X86::New##Zrrkz; \
2271 case X86::Orig##Zrm: \
2272 return X86::New##Zrm; \
2273 case X86::Orig##Zrmkz: \
2274 return X86::New##Zrmkz;
2275
2276#define VPERM_CASES_BROADCAST(Orig, New) \
2277 VPERM_CASES(Orig, New) \
2278 case X86::Orig##Z128rmb: \
2279 return X86::New##Z128rmb; \
2280 case X86::Orig##Z128rmbkz: \
2281 return X86::New##Z128rmbkz; \
2282 case X86::Orig##Z256rmb: \
2283 return X86::New##Z256rmb; \
2284 case X86::Orig##Z256rmbkz: \
2285 return X86::New##Z256rmbkz; \
2286 case X86::Orig##Zrmb: \
2287 return X86::New##Zrmb; \
2288 case X86::Orig##Zrmbkz: \
2289 return X86::New##Zrmbkz;
2290
2291 switch (Opcode) {
2292 VPERM_CASES(VPERMI2B, VPERMT2B)
2293 VPERM_CASES_BROADCAST(VPERMI2D, VPERMT2D)
2294 VPERM_CASES_BROADCAST(VPERMI2PD, VPERMT2PD)
2295 VPERM_CASES_BROADCAST(VPERMI2PS, VPERMT2PS)
2296 VPERM_CASES_BROADCAST(VPERMI2Q, VPERMT2Q)
2297 VPERM_CASES(VPERMI2W, VPERMT2W)
2298 VPERM_CASES(VPERMT2B, VPERMI2B)
2299 VPERM_CASES_BROADCAST(VPERMT2D, VPERMI2D)
2300 VPERM_CASES_BROADCAST(VPERMT2PD, VPERMI2PD)
2301 VPERM_CASES_BROADCAST(VPERMT2PS, VPERMI2PS)
2302 VPERM_CASES_BROADCAST(VPERMT2Q, VPERMI2Q)
2303 VPERM_CASES(VPERMT2W, VPERMI2W)
2304 }
2305
2306 llvm_unreachable("Unreachable!");
2307#undef VPERM_CASES_BROADCAST
2308#undef VPERM_CASES
2309}
2310
2312 unsigned OpIdx1,
2313 unsigned OpIdx2) const {
2314 auto CloneIfNew = [&](MachineInstr &MI) {
2315 return std::exchange(NewMI, false)
2316 ? MI.getParent()->getParent()->CloneMachineInstr(&MI)
2317 : &MI;
2318 };
2319 MachineInstr *WorkingMI = nullptr;
2320 unsigned Opc = MI.getOpcode();
2321
2322#define CASE_ND(OP) \
2323 case X86::OP: \
2324 case X86::OP##_ND:
2325
2326 switch (Opc) {
2327 // SHLD B, C, I <-> SHRD C, B, (BitWidth - I)
2328 CASE_ND(SHRD16rri8)
2329 CASE_ND(SHLD16rri8)
2330 CASE_ND(SHRD32rri8)
2331 CASE_ND(SHLD32rri8)
2332 CASE_ND(SHRD64rri8)
2333 CASE_ND(SHLD64rri8) {
2334 unsigned Size;
2335 switch (Opc) {
2336 default:
2337 llvm_unreachable("Unreachable!");
2338#define FROM_TO_SIZE(A, B, S) \
2339 case X86::A: \
2340 Opc = X86::B; \
2341 Size = S; \
2342 break; \
2343 case X86::A##_ND: \
2344 Opc = X86::B##_ND; \
2345 Size = S; \
2346 break; \
2347 case X86::B: \
2348 Opc = X86::A; \
2349 Size = S; \
2350 break; \
2351 case X86::B##_ND: \
2352 Opc = X86::A##_ND; \
2353 Size = S; \
2354 break;
2355
2356 FROM_TO_SIZE(SHRD16rri8, SHLD16rri8, 16)
2357 FROM_TO_SIZE(SHRD32rri8, SHLD32rri8, 32)
2358 FROM_TO_SIZE(SHRD64rri8, SHLD64rri8, 64)
2359#undef FROM_TO_SIZE
2360 }
2361 WorkingMI = CloneIfNew(MI);
2362 WorkingMI->setDesc(get(Opc));
2363 WorkingMI->getOperand(3).setImm(Size - MI.getOperand(3).getImm());
2364 break;
2365 }
2366 case X86::PFSUBrr:
2367 case X86::PFSUBRrr:
2368 // PFSUB x, y: x = x - y
2369 // PFSUBR x, y: x = y - x
2370 WorkingMI = CloneIfNew(MI);
2371 WorkingMI->setDesc(
2372 get(X86::PFSUBRrr == Opc ? X86::PFSUBrr : X86::PFSUBRrr));
2373 break;
2374 case X86::BLENDPDrri:
2375 case X86::BLENDPSrri:
2376 case X86::PBLENDWrri:
2377 case X86::VBLENDPDrri:
2378 case X86::VBLENDPSrri:
2379 case X86::VBLENDPDYrri:
2380 case X86::VBLENDPSYrri:
2381 case X86::VPBLENDDrri:
2382 case X86::VPBLENDWrri:
2383 case X86::VPBLENDDYrri:
2384 case X86::VPBLENDWYrri: {
2385 int8_t Mask;
2386 switch (Opc) {
2387 default:
2388 llvm_unreachable("Unreachable!");
2389 case X86::BLENDPDrri:
2390 Mask = (int8_t)0x03;
2391 break;
2392 case X86::BLENDPSrri:
2393 Mask = (int8_t)0x0F;
2394 break;
2395 case X86::PBLENDWrri:
2396 Mask = (int8_t)0xFF;
2397 break;
2398 case X86::VBLENDPDrri:
2399 Mask = (int8_t)0x03;
2400 break;
2401 case X86::VBLENDPSrri:
2402 Mask = (int8_t)0x0F;
2403 break;
2404 case X86::VBLENDPDYrri:
2405 Mask = (int8_t)0x0F;
2406 break;
2407 case X86::VBLENDPSYrri:
2408 Mask = (int8_t)0xFF;
2409 break;
2410 case X86::VPBLENDDrri:
2411 Mask = (int8_t)0x0F;
2412 break;
2413 case X86::VPBLENDWrri:
2414 Mask = (int8_t)0xFF;
2415 break;
2416 case X86::VPBLENDDYrri:
2417 Mask = (int8_t)0xFF;
2418 break;
2419 case X86::VPBLENDWYrri:
2420 Mask = (int8_t)0xFF;
2421 break;
2422 }
2423 // Only the least significant bits of Imm are used.
2424 // Using int8_t to ensure it will be sign extended to the int64_t that
2425 // setImm takes in order to match isel behavior.
2426 int8_t Imm = MI.getOperand(3).getImm() & Mask;
2427 WorkingMI = CloneIfNew(MI);
2428 WorkingMI->getOperand(3).setImm(Mask ^ Imm);
2429 break;
2430 }
2431 case X86::INSERTPSrri:
2432 case X86::VINSERTPSrri:
2433 case X86::VINSERTPSZrri: {
2434 unsigned Imm = MI.getOperand(MI.getNumOperands() - 1).getImm();
2435 unsigned ZMask = Imm & 15;
2436 unsigned DstIdx = (Imm >> 4) & 3;
2437 unsigned SrcIdx = (Imm >> 6) & 3;
2438
2439 // We can commute insertps if we zero 2 of the elements, the insertion is
2440 // "inline" and we don't override the insertion with a zero.
2441 if (DstIdx == SrcIdx && (ZMask & (1 << DstIdx)) == 0 &&
2442 llvm::popcount(ZMask) == 2) {
2443 unsigned AltIdx = llvm::countr_zero((ZMask | (1 << DstIdx)) ^ 15);
2444 assert(AltIdx < 4 && "Illegal insertion index");
2445 unsigned AltImm = (AltIdx << 6) | (AltIdx << 4) | ZMask;
2446 WorkingMI = CloneIfNew(MI);
2447 WorkingMI->getOperand(MI.getNumOperands() - 1).setImm(AltImm);
2448 break;
2449 }
2450 return nullptr;
2451 }
2452 case X86::MOVSDrr:
2453 case X86::MOVSSrr:
2454 case X86::VMOVSDrr:
2455 case X86::VMOVSSrr: {
2456 // On SSE41 or later we can commute a MOVSS/MOVSD to a BLENDPS/BLENDPD.
2457 if (Subtarget.hasSSE41()) {
2458 unsigned Mask;
2459 switch (Opc) {
2460 default:
2461 llvm_unreachable("Unreachable!");
2462 case X86::MOVSDrr:
2463 Opc = X86::BLENDPDrri;
2464 Mask = 0x02;
2465 break;
2466 case X86::MOVSSrr:
2467 Opc = X86::BLENDPSrri;
2468 Mask = 0x0E;
2469 break;
2470 case X86::VMOVSDrr:
2471 Opc = X86::VBLENDPDrri;
2472 Mask = 0x02;
2473 break;
2474 case X86::VMOVSSrr:
2475 Opc = X86::VBLENDPSrri;
2476 Mask = 0x0E;
2477 break;
2478 }
2479
2480 WorkingMI = CloneIfNew(MI);
2481 WorkingMI->setDesc(get(Opc));
2482 WorkingMI->addOperand(MachineOperand::CreateImm(Mask));
2483 break;
2484 }
2485
2486 assert(Opc == X86::MOVSDrr && "Only MOVSD can commute to SHUFPD");
2487 WorkingMI = CloneIfNew(MI);
2488 WorkingMI->setDesc(get(X86::SHUFPDrri));
2489 WorkingMI->addOperand(MachineOperand::CreateImm(0x02));
2490 break;
2491 }
2492 case X86::SHUFPDrri: {
2493 // Commute to MOVSD.
2494 assert(MI.getOperand(3).getImm() == 0x02 && "Unexpected immediate!");
2495 WorkingMI = CloneIfNew(MI);
2496 WorkingMI->setDesc(get(X86::MOVSDrr));
2497 WorkingMI->removeOperand(3);
2498 break;
2499 }
2500 case X86::PCLMULQDQrri:
2501 case X86::VPCLMULQDQrri:
2502 case X86::VPCLMULQDQYrri:
2503 case X86::VPCLMULQDQZrri:
2504 case X86::VPCLMULQDQZ128rri:
2505 case X86::VPCLMULQDQZ256rri: {
2506 // SRC1 64bits = Imm[0] ? SRC1[127:64] : SRC1[63:0]
2507 // SRC2 64bits = Imm[4] ? SRC2[127:64] : SRC2[63:0]
2508 unsigned Imm = MI.getOperand(3).getImm();
2509 unsigned Src1Hi = Imm & 0x01;
2510 unsigned Src2Hi = Imm & 0x10;
2511 WorkingMI = CloneIfNew(MI);
2512 WorkingMI->getOperand(3).setImm((Src1Hi << 4) | (Src2Hi >> 4));
2513 break;
2514 }
2515 case X86::VPCMPBZ128rri:
2516 case X86::VPCMPUBZ128rri:
2517 case X86::VPCMPBZ256rri:
2518 case X86::VPCMPUBZ256rri:
2519 case X86::VPCMPBZrri:
2520 case X86::VPCMPUBZrri:
2521 case X86::VPCMPDZ128rri:
2522 case X86::VPCMPUDZ128rri:
2523 case X86::VPCMPDZ256rri:
2524 case X86::VPCMPUDZ256rri:
2525 case X86::VPCMPDZrri:
2526 case X86::VPCMPUDZrri:
2527 case X86::VPCMPQZ128rri:
2528 case X86::VPCMPUQZ128rri:
2529 case X86::VPCMPQZ256rri:
2530 case X86::VPCMPUQZ256rri:
2531 case X86::VPCMPQZrri:
2532 case X86::VPCMPUQZrri:
2533 case X86::VPCMPWZ128rri:
2534 case X86::VPCMPUWZ128rri:
2535 case X86::VPCMPWZ256rri:
2536 case X86::VPCMPUWZ256rri:
2537 case X86::VPCMPWZrri:
2538 case X86::VPCMPUWZrri:
2539 case X86::VPCMPBZ128rrik:
2540 case X86::VPCMPUBZ128rrik:
2541 case X86::VPCMPBZ256rrik:
2542 case X86::VPCMPUBZ256rrik:
2543 case X86::VPCMPBZrrik:
2544 case X86::VPCMPUBZrrik:
2545 case X86::VPCMPDZ128rrik:
2546 case X86::VPCMPUDZ128rrik:
2547 case X86::VPCMPDZ256rrik:
2548 case X86::VPCMPUDZ256rrik:
2549 case X86::VPCMPDZrrik:
2550 case X86::VPCMPUDZrrik:
2551 case X86::VPCMPQZ128rrik:
2552 case X86::VPCMPUQZ128rrik:
2553 case X86::VPCMPQZ256rrik:
2554 case X86::VPCMPUQZ256rrik:
2555 case X86::VPCMPQZrrik:
2556 case X86::VPCMPUQZrrik:
2557 case X86::VPCMPWZ128rrik:
2558 case X86::VPCMPUWZ128rrik:
2559 case X86::VPCMPWZ256rrik:
2560 case X86::VPCMPUWZ256rrik:
2561 case X86::VPCMPWZrrik:
2562 case X86::VPCMPUWZrrik:
2563 WorkingMI = CloneIfNew(MI);
2564 // Flip comparison mode immediate (if necessary).
2565 WorkingMI->getOperand(MI.getNumOperands() - 1)
2567 MI.getOperand(MI.getNumOperands() - 1).getImm() & 0x7));
2568 break;
2569 case X86::VPCOMBri:
2570 case X86::VPCOMUBri:
2571 case X86::VPCOMDri:
2572 case X86::VPCOMUDri:
2573 case X86::VPCOMQri:
2574 case X86::VPCOMUQri:
2575 case X86::VPCOMWri:
2576 case X86::VPCOMUWri:
2577 WorkingMI = CloneIfNew(MI);
2578 // Flip comparison mode immediate (if necessary).
2579 WorkingMI->getOperand(3).setImm(
2580 X86::getSwappedVPCOMImm(MI.getOperand(3).getImm() & 0x7));
2581 break;
2582 case X86::VCMPSDZrri:
2583 case X86::VCMPSSZrri:
2584 case X86::VCMPPDZrri:
2585 case X86::VCMPPSZrri:
2586 case X86::VCMPSHZrri:
2587 case X86::VCMPPHZrri:
2588 case X86::VCMPPHZ128rri:
2589 case X86::VCMPPHZ256rri:
2590 case X86::VCMPPDZ128rri:
2591 case X86::VCMPPSZ128rri:
2592 case X86::VCMPPDZ256rri:
2593 case X86::VCMPPSZ256rri:
2594 case X86::VCMPPDZrrik:
2595 case X86::VCMPPSZrrik:
2596 case X86::VCMPPHZrrik:
2597 case X86::VCMPPDZ128rrik:
2598 case X86::VCMPPSZ128rrik:
2599 case X86::VCMPPHZ128rrik:
2600 case X86::VCMPPDZ256rrik:
2601 case X86::VCMPPSZ256rrik:
2602 case X86::VCMPPHZ256rrik:
2603 WorkingMI = CloneIfNew(MI);
2604 WorkingMI->getOperand(MI.getNumExplicitOperands() - 1)
2606 MI.getOperand(MI.getNumExplicitOperands() - 1).getImm() & 0x1f));
2607 break;
2608 case X86::VPERM2F128rri:
2609 case X86::VPERM2I128rri:
2610 // Flip permute source immediate.
2611 // Imm & 0x02: lo = if set, select Op1.lo/hi else Op0.lo/hi.
2612 // Imm & 0x20: hi = if set, select Op1.lo/hi else Op0.lo/hi.
2613 WorkingMI = CloneIfNew(MI);
2614 WorkingMI->getOperand(3).setImm((MI.getOperand(3).getImm() & 0xFF) ^ 0x22);
2615 break;
2616 case X86::MOVHLPSrr:
2617 case X86::UNPCKHPDrr:
2618 case X86::VMOVHLPSrr:
2619 case X86::VUNPCKHPDrr:
2620 case X86::VMOVHLPSZrr:
2621 case X86::VUNPCKHPDZ128rr:
2622 assert(Subtarget.hasSSE2() && "Commuting MOVHLP/UNPCKHPD requires SSE2!");
2623
2624 switch (Opc) {
2625 default:
2626 llvm_unreachable("Unreachable!");
2627 case X86::MOVHLPSrr:
2628 Opc = X86::UNPCKHPDrr;
2629 break;
2630 case X86::UNPCKHPDrr:
2631 Opc = X86::MOVHLPSrr;
2632 break;
2633 case X86::VMOVHLPSrr:
2634 Opc = X86::VUNPCKHPDrr;
2635 break;
2636 case X86::VUNPCKHPDrr:
2637 Opc = X86::VMOVHLPSrr;
2638 break;
2639 case X86::VMOVHLPSZrr:
2640 Opc = X86::VUNPCKHPDZ128rr;
2641 break;
2642 case X86::VUNPCKHPDZ128rr:
2643 Opc = X86::VMOVHLPSZrr;
2644 break;
2645 }
2646 WorkingMI = CloneIfNew(MI);
2647 WorkingMI->setDesc(get(Opc));
2648 break;
2649 CASE_ND(CMOV16rr)
2650 CASE_ND(CMOV32rr)
2651 CASE_ND(CMOV64rr) {
2652 WorkingMI = CloneIfNew(MI);
2653 unsigned OpNo = MI.getDesc().getNumOperands() - 1;
2654 X86::CondCode CC = static_cast<X86::CondCode>(MI.getOperand(OpNo).getImm());
2656 break;
2657 }
2658 case X86::VPTERNLOGDZrri:
2659 case X86::VPTERNLOGDZrmi:
2660 case X86::VPTERNLOGDZ128rri:
2661 case X86::VPTERNLOGDZ128rmi:
2662 case X86::VPTERNLOGDZ256rri:
2663 case X86::VPTERNLOGDZ256rmi:
2664 case X86::VPTERNLOGQZrri:
2665 case X86::VPTERNLOGQZrmi:
2666 case X86::VPTERNLOGQZ128rri:
2667 case X86::VPTERNLOGQZ128rmi:
2668 case X86::VPTERNLOGQZ256rri:
2669 case X86::VPTERNLOGQZ256rmi:
2670 case X86::VPTERNLOGDZrrik:
2671 case X86::VPTERNLOGDZ128rrik:
2672 case X86::VPTERNLOGDZ256rrik:
2673 case X86::VPTERNLOGQZrrik:
2674 case X86::VPTERNLOGQZ128rrik:
2675 case X86::VPTERNLOGQZ256rrik:
2676 case X86::VPTERNLOGDZrrikz:
2677 case X86::VPTERNLOGDZrmikz:
2678 case X86::VPTERNLOGDZ128rrikz:
2679 case X86::VPTERNLOGDZ128rmikz:
2680 case X86::VPTERNLOGDZ256rrikz:
2681 case X86::VPTERNLOGDZ256rmikz:
2682 case X86::VPTERNLOGQZrrikz:
2683 case X86::VPTERNLOGQZrmikz:
2684 case X86::VPTERNLOGQZ128rrikz:
2685 case X86::VPTERNLOGQZ128rmikz:
2686 case X86::VPTERNLOGQZ256rrikz:
2687 case X86::VPTERNLOGQZ256rmikz:
2688 case X86::VPTERNLOGDZ128rmbi:
2689 case X86::VPTERNLOGDZ256rmbi:
2690 case X86::VPTERNLOGDZrmbi:
2691 case X86::VPTERNLOGQZ128rmbi:
2692 case X86::VPTERNLOGQZ256rmbi:
2693 case X86::VPTERNLOGQZrmbi:
2694 case X86::VPTERNLOGDZ128rmbikz:
2695 case X86::VPTERNLOGDZ256rmbikz:
2696 case X86::VPTERNLOGDZrmbikz:
2697 case X86::VPTERNLOGQZ128rmbikz:
2698 case X86::VPTERNLOGQZ256rmbikz:
2699 case X86::VPTERNLOGQZrmbikz: {
2700 WorkingMI = CloneIfNew(MI);
2701 commuteVPTERNLOG(*WorkingMI, OpIdx1, OpIdx2);
2702 break;
2703 }
2704 default:
2706 WorkingMI = CloneIfNew(MI);
2708 break;
2709 }
2710
2711 if (auto *FMA3Group = getFMA3Group(Opc, MI.getDesc().TSFlags)) {
2712 WorkingMI = CloneIfNew(MI);
2713 WorkingMI->setDesc(
2714 get(getFMA3OpcodeToCommuteOperands(MI, OpIdx1, OpIdx2, *FMA3Group)));
2715 break;
2716 }
2717 }
2718 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
2719}
2720
2721bool X86InstrInfo::findThreeSrcCommutedOpIndices(const MachineInstr &MI,
2722 unsigned &SrcOpIdx1,
2723 unsigned &SrcOpIdx2,
2724 bool IsIntrinsic) const {
2725 uint64_t TSFlags = MI.getDesc().TSFlags;
2726
2727 unsigned FirstCommutableVecOp = 1;
2728 unsigned LastCommutableVecOp = 3;
2729 unsigned KMaskOp = -1U;
2730 if (X86II::isKMasked(TSFlags)) {
2731 // For k-zero-masked operations it is Ok to commute the first vector
2732 // operand. Unless this is an intrinsic instruction.
2733 // For regular k-masked operations a conservative choice is done as the
2734 // elements of the first vector operand, for which the corresponding bit
2735 // in the k-mask operand is set to 0, are copied to the result of the
2736 // instruction.
2737 // TODO/FIXME: The commute still may be legal if it is known that the
2738 // k-mask operand is set to either all ones or all zeroes.
2739 // It is also Ok to commute the 1st operand if all users of MI use only
2740 // the elements enabled by the k-mask operand. For example,
2741 // v4 = VFMADD213PSZrk v1, k, v2, v3; // v1[i] = k[i] ? v2[i]*v1[i]+v3[i]
2742 // : v1[i];
2743 // VMOVAPSZmrk <mem_addr>, k, v4; // this is the ONLY user of v4 ->
2744 // // Ok, to commute v1 in FMADD213PSZrk.
2745
2746 // The k-mask operand has index = 2 for masked and zero-masked operations.
2747 KMaskOp = 2;
2748
2749 // The operand with index = 1 is used as a source for those elements for
2750 // which the corresponding bit in the k-mask is set to 0.
2751 if (X86II::isKMergeMasked(TSFlags) || IsIntrinsic)
2752 FirstCommutableVecOp = 3;
2753
2754 LastCommutableVecOp++;
2755 } else if (IsIntrinsic) {
2756 // Commuting the first operand of an intrinsic instruction isn't possible
2757 // unless we can prove that only the lowest element of the result is used.
2758 FirstCommutableVecOp = 2;
2759 }
2760
2761 if (isMem(MI, LastCommutableVecOp))
2762 LastCommutableVecOp--;
2763
2764 // Only the first RegOpsNum operands are commutable.
2765 // Also, the value 'CommuteAnyOperandIndex' is valid here as it means
2766 // that the operand is not specified/fixed.
2767 if (SrcOpIdx1 != CommuteAnyOperandIndex &&
2768 (SrcOpIdx1 < FirstCommutableVecOp || SrcOpIdx1 > LastCommutableVecOp ||
2769 SrcOpIdx1 == KMaskOp))
2770 return false;
2771 if (SrcOpIdx2 != CommuteAnyOperandIndex &&
2772 (SrcOpIdx2 < FirstCommutableVecOp || SrcOpIdx2 > LastCommutableVecOp ||
2773 SrcOpIdx2 == KMaskOp))
2774 return false;
2775
2776 // Look for two different register operands assumed to be commutable
2777 // regardless of the FMA opcode. The FMA opcode is adjusted later.
2778 if (SrcOpIdx1 == CommuteAnyOperandIndex ||
2779 SrcOpIdx2 == CommuteAnyOperandIndex) {
2780 unsigned CommutableOpIdx2 = SrcOpIdx2;
2781
2782 // At least one of operands to be commuted is not specified and
2783 // this method is free to choose appropriate commutable operands.
2784 if (SrcOpIdx1 == SrcOpIdx2)
2785 // Both of operands are not fixed. By default set one of commutable
2786 // operands to the last register operand of the instruction.
2787 CommutableOpIdx2 = LastCommutableVecOp;
2788 else if (SrcOpIdx2 == CommuteAnyOperandIndex)
2789 // Only one of operands is not fixed.
2790 CommutableOpIdx2 = SrcOpIdx1;
2791
2792 // CommutableOpIdx2 is well defined now. Let's choose another commutable
2793 // operand and assign its index to CommutableOpIdx1.
2794 Register Op2Reg = MI.getOperand(CommutableOpIdx2).getReg();
2795
2796 unsigned CommutableOpIdx1;
2797 for (CommutableOpIdx1 = LastCommutableVecOp;
2798 CommutableOpIdx1 >= FirstCommutableVecOp; CommutableOpIdx1--) {
2799 // Just ignore and skip the k-mask operand.
2800 if (CommutableOpIdx1 == KMaskOp)
2801 continue;
2802
2803 // The commuted operands must have different registers.
2804 // Otherwise, the commute transformation does not change anything and
2805 // is useless then.
2806 if (Op2Reg != MI.getOperand(CommutableOpIdx1).getReg())
2807 break;
2808 }
2809
2810 // No appropriate commutable operands were found.
2811 if (CommutableOpIdx1 < FirstCommutableVecOp)
2812 return false;
2813
2814 // Assign the found pair of commutable indices to SrcOpIdx1 and SrcOpidx2
2815 // to return those values.
2816 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1,
2817 CommutableOpIdx2))
2818 return false;
2819 }
2820
2821 return true;
2822}
2823
2825 unsigned &SrcOpIdx1,
2826 unsigned &SrcOpIdx2) const {
2827 const MCInstrDesc &Desc = MI.getDesc();
2828 if (!Desc.isCommutable())
2829 return false;
2830
2831 switch (MI.getOpcode()) {
2832 case X86::CMPSDrri:
2833 case X86::CMPSSrri:
2834 case X86::CMPPDrri:
2835 case X86::CMPPSrri:
2836 case X86::VCMPSDrri:
2837 case X86::VCMPSSrri:
2838 case X86::VCMPPDrri:
2839 case X86::VCMPPSrri:
2840 case X86::VCMPPDYrri:
2841 case X86::VCMPPSYrri:
2842 case X86::VCMPSDZrri:
2843 case X86::VCMPSSZrri:
2844 case X86::VCMPPDZrri:
2845 case X86::VCMPPSZrri:
2846 case X86::VCMPSHZrri:
2847 case X86::VCMPPHZrri:
2848 case X86::VCMPPHZ128rri:
2849 case X86::VCMPPHZ256rri:
2850 case X86::VCMPPDZ128rri:
2851 case X86::VCMPPSZ128rri:
2852 case X86::VCMPPDZ256rri:
2853 case X86::VCMPPSZ256rri:
2854 case X86::VCMPPDZrrik:
2855 case X86::VCMPPSZrrik:
2856 case X86::VCMPPHZrrik:
2857 case X86::VCMPPDZ128rrik:
2858 case X86::VCMPPSZ128rrik:
2859 case X86::VCMPPHZ128rrik:
2860 case X86::VCMPPDZ256rrik:
2861 case X86::VCMPPSZ256rrik:
2862 case X86::VCMPPHZ256rrik: {
2863 unsigned OpOffset = X86II::isKMasked(Desc.TSFlags) ? 1 : 0;
2864
2865 // Float comparison can be safely commuted for
2866 // Ordered/Unordered/Equal/NotEqual tests
2867 unsigned Imm = MI.getOperand(3 + OpOffset).getImm() & 0x7;
2868 switch (Imm) {
2869 default:
2870 // EVEX versions can be commuted.
2871 if ((Desc.TSFlags & X86II::EncodingMask) == X86II::EVEX)
2872 break;
2873 return false;
2874 case 0x00: // EQUAL
2875 case 0x03: // UNORDERED
2876 case 0x04: // NOT EQUAL
2877 case 0x07: // ORDERED
2878 break;
2879 }
2880
2881 // The indices of the commutable operands are 1 and 2 (or 2 and 3
2882 // when masked).
2883 // Assign them to the returned operand indices here.
2884 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 1 + OpOffset,
2885 2 + OpOffset);
2886 }
2887 case X86::MOVSSrr:
2888 // X86::MOVSDrr is always commutable. MOVSS is only commutable if we can
2889 // form sse4.1 blend. We assume VMOVSSrr/VMOVSDrr is always commutable since
2890 // AVX implies sse4.1.
2891 if (Subtarget.hasSSE41())
2892 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
2893 return false;
2894 case X86::SHUFPDrri:
2895 // We can commute this to MOVSD.
2896 if (MI.getOperand(3).getImm() == 0x02)
2897 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
2898 return false;
2899 case X86::MOVHLPSrr:
2900 case X86::UNPCKHPDrr:
2901 case X86::VMOVHLPSrr:
2902 case X86::VUNPCKHPDrr:
2903 case X86::VMOVHLPSZrr:
2904 case X86::VUNPCKHPDZ128rr:
2905 if (Subtarget.hasSSE2())
2906 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
2907 return false;
2908 case X86::VPTERNLOGDZrri:
2909 case X86::VPTERNLOGDZrmi:
2910 case X86::VPTERNLOGDZ128rri:
2911 case X86::VPTERNLOGDZ128rmi:
2912 case X86::VPTERNLOGDZ256rri:
2913 case X86::VPTERNLOGDZ256rmi:
2914 case X86::VPTERNLOGQZrri:
2915 case X86::VPTERNLOGQZrmi:
2916 case X86::VPTERNLOGQZ128rri:
2917 case X86::VPTERNLOGQZ128rmi:
2918 case X86::VPTERNLOGQZ256rri:
2919 case X86::VPTERNLOGQZ256rmi:
2920 case X86::VPTERNLOGDZrrik:
2921 case X86::VPTERNLOGDZ128rrik:
2922 case X86::VPTERNLOGDZ256rrik:
2923 case X86::VPTERNLOGQZrrik:
2924 case X86::VPTERNLOGQZ128rrik:
2925 case X86::VPTERNLOGQZ256rrik:
2926 case X86::VPTERNLOGDZrrikz:
2927 case X86::VPTERNLOGDZrmikz:
2928 case X86::VPTERNLOGDZ128rrikz:
2929 case X86::VPTERNLOGDZ128rmikz:
2930 case X86::VPTERNLOGDZ256rrikz:
2931 case X86::VPTERNLOGDZ256rmikz:
2932 case X86::VPTERNLOGQZrrikz:
2933 case X86::VPTERNLOGQZrmikz:
2934 case X86::VPTERNLOGQZ128rrikz:
2935 case X86::VPTERNLOGQZ128rmikz:
2936 case X86::VPTERNLOGQZ256rrikz:
2937 case X86::VPTERNLOGQZ256rmikz:
2938 case X86::VPTERNLOGDZ128rmbi:
2939 case X86::VPTERNLOGDZ256rmbi:
2940 case X86::VPTERNLOGDZrmbi:
2941 case X86::VPTERNLOGQZ128rmbi:
2942 case X86::VPTERNLOGQZ256rmbi:
2943 case X86::VPTERNLOGQZrmbi:
2944 case X86::VPTERNLOGDZ128rmbikz:
2945 case X86::VPTERNLOGDZ256rmbikz:
2946 case X86::VPTERNLOGDZrmbikz:
2947 case X86::VPTERNLOGQZ128rmbikz:
2948 case X86::VPTERNLOGQZ256rmbikz:
2949 case X86::VPTERNLOGQZrmbikz:
2950 return findThreeSrcCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
2951 case X86::VPDPWSSDYrr:
2952 case X86::VPDPWSSDrr:
2953 case X86::VPDPWSSDSYrr:
2954 case X86::VPDPWSSDSrr:
2955 case X86::VPDPWUUDrr:
2956 case X86::VPDPWUUDYrr:
2957 case X86::VPDPWUUDSrr:
2958 case X86::VPDPWUUDSYrr:
2959 case X86::VPDPBSSDSrr:
2960 case X86::VPDPBSSDSYrr:
2961 case X86::VPDPBSSDrr:
2962 case X86::VPDPBSSDYrr:
2963 case X86::VPDPBUUDSrr:
2964 case X86::VPDPBUUDSYrr:
2965 case X86::VPDPBUUDrr:
2966 case X86::VPDPBUUDYrr:
2967 case X86::VPDPBSSDSZ128rr:
2968 case X86::VPDPBSSDSZ128rrk:
2969 case X86::VPDPBSSDSZ128rrkz:
2970 case X86::VPDPBSSDSZ256rr:
2971 case X86::VPDPBSSDSZ256rrk:
2972 case X86::VPDPBSSDSZ256rrkz:
2973 case X86::VPDPBSSDSZrr:
2974 case X86::VPDPBSSDSZrrk:
2975 case X86::VPDPBSSDSZrrkz:
2976 case X86::VPDPBSSDZ128rr:
2977 case X86::VPDPBSSDZ128rrk:
2978 case X86::VPDPBSSDZ128rrkz:
2979 case X86::VPDPBSSDZ256rr:
2980 case X86::VPDPBSSDZ256rrk:
2981 case X86::VPDPBSSDZ256rrkz:
2982 case X86::VPDPBSSDZrr:
2983 case X86::VPDPBSSDZrrk:
2984 case X86::VPDPBSSDZrrkz:
2985 case X86::VPDPBUUDSZ128rr:
2986 case X86::VPDPBUUDSZ128rrk:
2987 case X86::VPDPBUUDSZ128rrkz:
2988 case X86::VPDPBUUDSZ256rr:
2989 case X86::VPDPBUUDSZ256rrk:
2990 case X86::VPDPBUUDSZ256rrkz:
2991 case X86::VPDPBUUDSZrr:
2992 case X86::VPDPBUUDSZrrk:
2993 case X86::VPDPBUUDSZrrkz:
2994 case X86::VPDPBUUDZ128rr:
2995 case X86::VPDPBUUDZ128rrk:
2996 case X86::VPDPBUUDZ128rrkz:
2997 case X86::VPDPBUUDZ256rr:
2998 case X86::VPDPBUUDZ256rrk:
2999 case X86::VPDPBUUDZ256rrkz:
3000 case X86::VPDPBUUDZrr:
3001 case X86::VPDPBUUDZrrk:
3002 case X86::VPDPBUUDZrrkz:
3003 case X86::VPDPWSSDZ128rr:
3004 case X86::VPDPWSSDZ128rrk:
3005 case X86::VPDPWSSDZ128rrkz:
3006 case X86::VPDPWSSDZ256rr:
3007 case X86::VPDPWSSDZ256rrk:
3008 case X86::VPDPWSSDZ256rrkz:
3009 case X86::VPDPWSSDZrr:
3010 case X86::VPDPWSSDZrrk:
3011 case X86::VPDPWSSDZrrkz:
3012 case X86::VPDPWSSDSZ128rr:
3013 case X86::VPDPWSSDSZ128rrk:
3014 case X86::VPDPWSSDSZ128rrkz:
3015 case X86::VPDPWSSDSZ256rr:
3016 case X86::VPDPWSSDSZ256rrk:
3017 case X86::VPDPWSSDSZ256rrkz:
3018 case X86::VPDPWSSDSZrr:
3019 case X86::VPDPWSSDSZrrk:
3020 case X86::VPDPWSSDSZrrkz:
3021 case X86::VPDPWUUDZ128rr:
3022 case X86::VPDPWUUDZ128rrk:
3023 case X86::VPDPWUUDZ128rrkz:
3024 case X86::VPDPWUUDZ256rr:
3025 case X86::VPDPWUUDZ256rrk:
3026 case X86::VPDPWUUDZ256rrkz:
3027 case X86::VPDPWUUDZrr:
3028 case X86::VPDPWUUDZrrk:
3029 case X86::VPDPWUUDZrrkz:
3030 case X86::VPDPWUUDSZ128rr:
3031 case X86::VPDPWUUDSZ128rrk:
3032 case X86::VPDPWUUDSZ128rrkz:
3033 case X86::VPDPWUUDSZ256rr:
3034 case X86::VPDPWUUDSZ256rrk:
3035 case X86::VPDPWUUDSZ256rrkz:
3036 case X86::VPDPWUUDSZrr:
3037 case X86::VPDPWUUDSZrrk:
3038 case X86::VPDPWUUDSZrrkz:
3039 case X86::VPMADD52HUQrr:
3040 case X86::VPMADD52HUQYrr:
3041 case X86::VPMADD52HUQZ128r:
3042 case X86::VPMADD52HUQZ128rk:
3043 case X86::VPMADD52HUQZ128rkz:
3044 case X86::VPMADD52HUQZ256r:
3045 case X86::VPMADD52HUQZ256rk:
3046 case X86::VPMADD52HUQZ256rkz:
3047 case X86::VPMADD52HUQZr:
3048 case X86::VPMADD52HUQZrk:
3049 case X86::VPMADD52HUQZrkz:
3050 case X86::VPMADD52LUQrr:
3051 case X86::VPMADD52LUQYrr:
3052 case X86::VPMADD52LUQZ128r:
3053 case X86::VPMADD52LUQZ128rk:
3054 case X86::VPMADD52LUQZ128rkz:
3055 case X86::VPMADD52LUQZ256r:
3056 case X86::VPMADD52LUQZ256rk:
3057 case X86::VPMADD52LUQZ256rkz:
3058 case X86::VPMADD52LUQZr:
3059 case X86::VPMADD52LUQZrk:
3060 case X86::VPMADD52LUQZrkz:
3061 case X86::VFMADDCPHZr:
3062 case X86::VFMADDCPHZrk:
3063 case X86::VFMADDCPHZrkz:
3064 case X86::VFMADDCPHZ128r:
3065 case X86::VFMADDCPHZ128rk:
3066 case X86::VFMADDCPHZ128rkz:
3067 case X86::VFMADDCPHZ256r:
3068 case X86::VFMADDCPHZ256rk:
3069 case X86::VFMADDCPHZ256rkz:
3070 case X86::VFMADDCSHZr:
3071 case X86::VFMADDCSHZrk:
3072 case X86::VFMADDCSHZrkz: {
3073 unsigned CommutableOpIdx1 = 2;
3074 unsigned CommutableOpIdx2 = 3;
3075 if (X86II::isKMasked(Desc.TSFlags)) {
3076 // Skip the mask register.
3077 ++CommutableOpIdx1;
3078 ++CommutableOpIdx2;
3079 }
3080 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1,
3081 CommutableOpIdx2))
3082 return false;
3083 if (!MI.getOperand(SrcOpIdx1).isReg() || !MI.getOperand(SrcOpIdx2).isReg())
3084 // No idea.
3085 return false;
3086 return true;
3087 }
3088
3089 default:
3090 const X86InstrFMA3Group *FMA3Group =
3091 getFMA3Group(MI.getOpcode(), MI.getDesc().TSFlags);
3092 if (FMA3Group)
3093 return findThreeSrcCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2,
3094 FMA3Group->isIntrinsic());
3095
3096 // Handled masked instructions since we need to skip over the mask input
3097 // and the preserved input.
3098 if (X86II::isKMasked(Desc.TSFlags)) {
3099 // First assume that the first input is the mask operand and skip past it.
3100 unsigned CommutableOpIdx1 = Desc.getNumDefs() + 1;
3101 unsigned CommutableOpIdx2 = Desc.getNumDefs() + 2;
3102 // Check if the first input is tied. If there isn't one then we only
3103 // need to skip the mask operand which we did above.
3104 if ((MI.getDesc().getOperandConstraint(Desc.getNumDefs(),
3105 MCOI::TIED_TO) != -1)) {
3106 // If this is zero masking instruction with a tied operand, we need to
3107 // move the first index back to the first input since this must
3108 // be a 3 input instruction and we want the first two non-mask inputs.
3109 // Otherwise this is a 2 input instruction with a preserved input and
3110 // mask, so we need to move the indices to skip one more input.
3111 if (X86II::isKMergeMasked(Desc.TSFlags)) {
3112 ++CommutableOpIdx1;
3113 ++CommutableOpIdx2;
3114 } else {
3115 --CommutableOpIdx1;
3116 }
3117 }
3118
3119 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1,
3120 CommutableOpIdx2))
3121 return false;
3122
3123 if (!MI.getOperand(SrcOpIdx1).isReg() ||
3124 !MI.getOperand(SrcOpIdx2).isReg())
3125 // No idea.
3126 return false;
3127 return true;
3128 }
3129
3130 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
3131 }
3132 return false;
3133}
3134
3136 unsigned Opcode = MI->getOpcode();
3137 if (Opcode != X86::LEA32r && Opcode != X86::LEA64r &&
3138 Opcode != X86::LEA64_32r)
3139 return false;
3140
3141 const MachineOperand &Scale = MI->getOperand(1 + X86::AddrScaleAmt);
3142 const MachineOperand &Disp = MI->getOperand(1 + X86::AddrDisp);
3143 const MachineOperand &Segment = MI->getOperand(1 + X86::AddrSegmentReg);
3144
3145 if (Segment.getReg() != 0 || !Disp.isImm() || Disp.getImm() != 0 ||
3146 Scale.getImm() > 1)
3147 return false;
3148
3149 return true;
3150}
3151
3153 // Currently we're interested in following sequence only.
3154 // r3 = lea r1, r2
3155 // r5 = add r3, r4
3156 // Both r3 and r4 are killed in add, we hope the add instruction has the
3157 // operand order
3158 // r5 = add r4, r3
3159 // So later in X86FixupLEAs the lea instruction can be rewritten as add.
3160 unsigned Opcode = MI.getOpcode();
3161 if (Opcode != X86::ADD32rr && Opcode != X86::ADD64rr)
3162 return false;
3163
3164 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
3165 Register Reg1 = MI.getOperand(1).getReg();
3166 Register Reg2 = MI.getOperand(2).getReg();
3167
3168 // Check if Reg1 comes from LEA in the same MBB.
3169 if (MachineInstr *Inst = MRI.getUniqueVRegDef(Reg1)) {
3170 if (isConvertibleLEA(Inst) && Inst->getParent() == MI.getParent()) {
3171 Commute = true;
3172 return true;
3173 }
3174 }
3175
3176 // Check if Reg2 comes from LEA in the same MBB.
3177 if (MachineInstr *Inst = MRI.getUniqueVRegDef(Reg2)) {
3178 if (isConvertibleLEA(Inst) && Inst->getParent() == MI.getParent()) {
3179 Commute = false;
3180 return true;
3181 }
3182 }
3183
3184 return false;
3185}
3186
3188 unsigned Opcode = MCID.getOpcode();
3189 if (!(X86::isJCC(Opcode) || X86::isSETCC(Opcode) || X86::isSETZUCC(Opcode) ||
3190 X86::isCMOVCC(Opcode) || X86::isCFCMOVCC(Opcode) ||
3191 X86::isCCMPCC(Opcode) || X86::isCTESTCC(Opcode)))
3192 return -1;
3193 // Assume that condition code is always the last use operand.
3194 unsigned NumUses = MCID.getNumOperands() - MCID.getNumDefs();
3195 return NumUses - 1;
3196}
3197
3199 const MCInstrDesc &MCID = MI.getDesc();
3200 int CondNo = getCondSrcNoFromDesc(MCID);
3201 if (CondNo < 0)
3202 return X86::COND_INVALID;
3203 CondNo += MCID.getNumDefs();
3204 return static_cast<X86::CondCode>(MI.getOperand(CondNo).getImm());
3205}
3206
3208 return X86::isJCC(MI.getOpcode()) ? X86::getCondFromMI(MI)
3210}
3211
3213 return X86::isSETCC(MI.getOpcode()) || X86::isSETZUCC(MI.getOpcode())
3216}
3217
3219 return X86::isCMOVCC(MI.getOpcode()) ? X86::getCondFromMI(MI)
3221}
3222
3224 return X86::isCFCMOVCC(MI.getOpcode()) ? X86::getCondFromMI(MI)
3226}
3227
3229 return X86::isCCMPCC(MI.getOpcode()) || X86::isCTESTCC(MI.getOpcode())
3232}
3233
3235 // CCMP/CTEST has two conditional operands:
3236 // - SCC: source conditonal code (same as CMOV)
3237 // - DCF: destination conditional flags, which has 4 valid bits
3238 //
3239 // +----+----+----+----+
3240 // | OF | SF | ZF | CF |
3241 // +----+----+----+----+
3242 //
3243 // If SCC(source conditional code) evaluates to false, CCMP/CTEST will updates
3244 // the conditional flags by as follows:
3245 //
3246 // OF = DCF.OF
3247 // SF = DCF.SF
3248 // ZF = DCF.ZF
3249 // CF = DCF.CF
3250 // PF = DCF.CF
3251 // AF = 0 (Auxiliary Carry Flag)
3252 //
3253 // Otherwise, the CMP or TEST is executed and it updates the
3254 // CSPAZO flags normally.
3255 //
3256 // NOTE:
3257 // If SCC = P, then SCC evaluates to true regardless of the CSPAZO value.
3258 // If SCC = NP, then SCC evaluates to false regardless of the CSPAZO value.
3259
3260 enum { CF = 1, ZF = 2, SF = 4, OF = 8, PF = CF };
3261
3262 switch (CC) {
3263 default:
3264 llvm_unreachable("Illegal condition code!");
3265 case X86::COND_NO:
3266 case X86::COND_NE:
3267 case X86::COND_GE:
3268 case X86::COND_G:
3269 case X86::COND_AE:
3270 case X86::COND_A:
3271 case X86::COND_NS:
3272 case X86::COND_NP:
3273 return 0;
3274 case X86::COND_O:
3275 return OF;
3276 case X86::COND_B:
3277 case X86::COND_BE:
3278 return CF;
3279 break;
3280 case X86::COND_E:
3281 case X86::COND_LE:
3282 return ZF;
3283 case X86::COND_S:
3284 case X86::COND_L:
3285 return SF;
3286 case X86::COND_P:
3287 return PF;
3288 }
3289}
3290
3291#define GET_X86_NF_TRANSFORM_TABLE
3292#define GET_X86_ND2NONND_TABLE
3293#include "X86GenInstrMapping.inc"
3294
3296 unsigned Opc) {
3297 const auto I = llvm::lower_bound(Table, Opc);
3298 return (I == Table.end() || I->OldOpc != Opc) ? 0U : I->NewOpc;
3299}
3300unsigned X86::getNFVariant(unsigned Opc) {
3301#if defined(EXPENSIVE_CHECKS) && !defined(NDEBUG)
3302 // Make sure the tables are sorted.
3303 static std::atomic<bool> NFTableChecked(false);
3304 if (!NFTableChecked.load(std::memory_order_relaxed)) {
3305 assert(llvm::is_sorted(X86NFTransformTable) &&
3306 "X86NFTransformTable is not sorted!");
3307 NFTableChecked.store(true, std::memory_order_relaxed);
3308 }
3309#endif
3310 return getNewOpcFromTable(X86NFTransformTable, Opc);
3311}
3312
3314 const TargetRegisterInfo *TRI) {
3315 if (!MI.registerDefIsDead(X86::EFLAGS, TRI))
3316 return 0;
3317 // For the instructions are ADDrm/ADDmr with relocation, we'll skip the
3318 // optimization for replacing non-NF with NF. This is to keep backward
3319 // compatiblity with old version of linkers without APX relocation type
3320 // support on Linux OS.
3322 return 0;
3323 return X86::getNFVariant(MI.getOpcode());
3324}
3325
3326unsigned X86::getNonNDVariant(unsigned Opc) {
3327#if defined(EXPENSIVE_CHECKS) && !defined(NDEBUG)
3328 // Make sure the tables are sorted.
3329 static std::atomic<bool> NDTableChecked(false);
3330 if (!NDTableChecked.load(std::memory_order_relaxed)) {
3331 assert(llvm::is_sorted(X86ND2NonNDTable) &&
3332 "X86ND2NonNDTableis not sorted!");
3333 NDTableChecked.store(true, std::memory_order_relaxed);
3334 }
3335#endif
3336 return getNewOpcFromTable(X86ND2NonNDTable, Opc);
3337}
3338
3339/// Return the inverse of the specified condition,
3340/// e.g. turning COND_E to COND_NE.
3342 switch (CC) {
3343 default:
3344 llvm_unreachable("Illegal condition code!");
3345 case X86::COND_E:
3346 return X86::COND_NE;
3347 case X86::COND_NE:
3348 return X86::COND_E;
3349 case X86::COND_L:
3350 return X86::COND_GE;
3351 case X86::COND_LE:
3352 return X86::COND_G;
3353 case X86::COND_G:
3354 return X86::COND_LE;
3355 case X86::COND_GE:
3356 return X86::COND_L;
3357 case X86::COND_B:
3358 return X86::COND_AE;
3359 case X86::COND_BE:
3360 return X86::COND_A;
3361 case X86::COND_A:
3362 return X86::COND_BE;
3363 case X86::COND_AE:
3364 return X86::COND_B;
3365 case X86::COND_S:
3366 return X86::COND_NS;
3367 case X86::COND_NS:
3368 return X86::COND_S;
3369 case X86::COND_P:
3370 return X86::COND_NP;
3371 case X86::COND_NP:
3372 return X86::COND_P;
3373 case X86::COND_O:
3374 return X86::COND_NO;
3375 case X86::COND_NO:
3376 return X86::COND_O;
3377 case X86::COND_NE_OR_P:
3378 return X86::COND_E_AND_NP;
3379 case X86::COND_E_AND_NP:
3380 return X86::COND_NE_OR_P;
3381 }
3382}
3383
3384/// Assuming the flags are set by MI(a,b), return the condition code if we
3385/// modify the instructions such that flags are set by MI(b,a).
3387 switch (CC) {
3388 default:
3389 return X86::COND_INVALID;
3390 case X86::COND_E:
3391 return X86::COND_E;
3392 case X86::COND_NE:
3393 return X86::COND_NE;
3394 case X86::COND_L:
3395 return X86::COND_G;
3396 case X86::COND_LE:
3397 return X86::COND_GE;
3398 case X86::COND_G:
3399 return X86::COND_L;
3400 case X86::COND_GE:
3401 return X86::COND_LE;
3402 case X86::COND_B:
3403 return X86::COND_A;
3404 case X86::COND_BE:
3405 return X86::COND_AE;
3406 case X86::COND_A:
3407 return X86::COND_B;
3408 case X86::COND_AE:
3409 return X86::COND_BE;
3410 }
3411}
3412
3413std::pair<X86::CondCode, bool>
3416 bool NeedSwap = false;
3417 switch (Predicate) {
3418 default:
3419 break;
3420 // Floating-point Predicates
3421 case CmpInst::FCMP_UEQ:
3422 CC = X86::COND_E;
3423 break;
3424 case CmpInst::FCMP_OLT:
3425 NeedSwap = true;
3426 [[fallthrough]];
3427 case CmpInst::FCMP_OGT:
3428 CC = X86::COND_A;
3429 break;
3430 case CmpInst::FCMP_OLE:
3431 NeedSwap = true;
3432 [[fallthrough]];
3433 case CmpInst::FCMP_OGE:
3434 CC = X86::COND_AE;
3435 break;
3436 case CmpInst::FCMP_UGT:
3437 NeedSwap = true;
3438 [[fallthrough]];
3439 case CmpInst::FCMP_ULT:
3440 CC = X86::COND_B;
3441 break;
3442 case CmpInst::FCMP_UGE:
3443 NeedSwap = true;
3444 [[fallthrough]];
3445 case CmpInst::FCMP_ULE:
3446 CC = X86::COND_BE;
3447 break;
3448 case CmpInst::FCMP_ONE:
3449 CC = X86::COND_NE;
3450 break;
3451 case CmpInst::FCMP_UNO:
3452 CC = X86::COND_P;
3453 break;
3454 case CmpInst::FCMP_ORD:
3455 CC = X86::COND_NP;
3456 break;
3457 case CmpInst::FCMP_OEQ:
3458 [[fallthrough]];
3459 case CmpInst::FCMP_UNE:
3460 CC = X86::COND_INVALID;
3461 break;
3462
3463 // Integer Predicates
3464 case CmpInst::ICMP_EQ:
3465 CC = X86::COND_E;
3466 break;
3467 case CmpInst::ICMP_NE:
3468 CC = X86::COND_NE;
3469 break;
3470 case CmpInst::ICMP_UGT:
3471 CC = X86::COND_A;
3472 break;
3473 case CmpInst::ICMP_UGE:
3474 CC = X86::COND_AE;
3475 break;
3476 case CmpInst::ICMP_ULT:
3477 CC = X86::COND_B;
3478 break;
3479 case CmpInst::ICMP_ULE:
3480 CC = X86::COND_BE;
3481 break;
3482 case CmpInst::ICMP_SGT:
3483 CC = X86::COND_G;
3484 break;
3485 case CmpInst::ICMP_SGE:
3486 CC = X86::COND_GE;
3487 break;
3488 case CmpInst::ICMP_SLT:
3489 CC = X86::COND_L;
3490 break;
3491 case CmpInst::ICMP_SLE:
3492 CC = X86::COND_LE;
3493 break;
3494 }
3495
3496 return std::make_pair(CC, NeedSwap);
3497}
3498
3499/// Return a cmov opcode for the given register size in bytes, and operand type.
3500unsigned X86::getCMovOpcode(unsigned RegBytes, bool HasMemoryOperand,
3501 bool HasNDD) {
3502 switch (RegBytes) {
3503 default:
3504 llvm_unreachable("Illegal register size!");
3505#define GET_ND_IF_ENABLED(OPC) (HasNDD ? OPC##_ND : OPC)
3506 case 2:
3507 return HasMemoryOperand ? GET_ND_IF_ENABLED(X86::CMOV16rm)
3508 : GET_ND_IF_ENABLED(X86::CMOV16rr);
3509 case 4:
3510 return HasMemoryOperand ? GET_ND_IF_ENABLED(X86::CMOV32rm)
3511 : GET_ND_IF_ENABLED(X86::CMOV32rr);
3512 case 8:
3513 return HasMemoryOperand ? GET_ND_IF_ENABLED(X86::CMOV64rm)
3514 : GET_ND_IF_ENABLED(X86::CMOV64rr);
3515 }
3516}
3517
3518unsigned X86::getMOVriOpcode(bool Use64BitReg, int64_t Imm) {
3519 if (!Use64BitReg)
3520 return X86::MOV32ri;
3521
3522 if (isUInt<32>(Imm))
3523 return X86::MOV32ri64;
3524 if (isInt<32>(Imm))
3525 return X86::MOV64ri32;
3526 return X86::MOV64ri;
3527}
3528
3529/// Get the VPCMP immediate for the given condition.
3531 switch (CC) {
3532 default:
3533 llvm_unreachable("Unexpected SETCC condition");
3534 case ISD::SETNE:
3535 return 4;
3536 case ISD::SETEQ:
3537 return 0;
3538 case ISD::SETULT:
3539 case ISD::SETLT:
3540 return 1;
3541 case ISD::SETUGT:
3542 case ISD::SETGT:
3543 return 6;
3544 case ISD::SETUGE:
3545 case ISD::SETGE:
3546 return 5;
3547 case ISD::SETULE:
3548 case ISD::SETLE:
3549 return 2;
3550 }
3551}
3552
3553/// Get the VPCMP immediate if the operands are swapped.
3554unsigned X86::getSwappedVPCMPImm(unsigned Imm) {
3555 switch (Imm) {
3556 default:
3557 llvm_unreachable("Unreachable!");
3558 case 0x01:
3559 Imm = 0x06;
3560 break; // LT -> NLE
3561 case 0x02:
3562 Imm = 0x05;
3563 break; // LE -> NLT
3564 case 0x05:
3565 Imm = 0x02;
3566 break; // NLT -> LE
3567 case 0x06:
3568 Imm = 0x01;
3569 break; // NLE -> LT
3570 case 0x00: // EQ
3571 case 0x03: // FALSE
3572 case 0x04: // NE
3573 case 0x07: // TRUE
3574 break;
3575 }
3576
3577 return Imm;
3578}
3579
3580/// Get the VPCOM immediate if the operands are swapped.
3581unsigned X86::getSwappedVPCOMImm(unsigned Imm) {
3582 switch (Imm) {
3583 default:
3584 llvm_unreachable("Unreachable!");
3585 case 0x00:
3586 Imm = 0x02;
3587 break; // LT -> GT
3588 case 0x01:
3589 Imm = 0x03;
3590 break; // LE -> GE
3591 case 0x02:
3592 Imm = 0x00;
3593 break; // GT -> LT
3594 case 0x03:
3595 Imm = 0x01;
3596 break; // GE -> LE
3597 case 0x04: // EQ
3598 case 0x05: // NE
3599 case 0x06: // FALSE
3600 case 0x07: // TRUE
3601 break;
3602 }
3603
3604 return Imm;
3605}
3606
3607/// Get the VCMP immediate if the operands are swapped.
3608unsigned X86::getSwappedVCMPImm(unsigned Imm) {
3609 // Only need the lower 2 bits to distinquish.
3610 switch (Imm & 0x3) {
3611 default:
3612 llvm_unreachable("Unreachable!");
3613 case 0x00:
3614 case 0x03:
3615 // EQ/NE/TRUE/FALSE/ORD/UNORD don't change immediate when commuted.
3616 break;
3617 case 0x01:
3618 case 0x02:
3619 // Need to toggle bits 3:0. Bit 4 stays the same.
3620 Imm ^= 0xf;
3621 break;
3622 }
3623
3624 return Imm;
3625}
3626
3628 if (Info.RegClass == X86::VR128RegClassID ||
3629 Info.RegClass == X86::VR128XRegClassID)
3630 return 128;
3631 if (Info.RegClass == X86::VR256RegClassID ||
3632 Info.RegClass == X86::VR256XRegClassID)
3633 return 256;
3634 if (Info.RegClass == X86::VR512RegClassID)
3635 return 512;
3636 llvm_unreachable("Unknown register class!");
3637}
3638
3639/// Return true if the Reg is X87 register.
3640static bool isX87Reg(Register Reg) {
3641 return (Reg == X86::FPCW || Reg == X86::FPSW ||
3642 (Reg >= X86::ST0 && Reg <= X86::ST7));
3643}
3644
3645/// check if the instruction is X87 instruction
3647 // Call and inlineasm defs X87 register, so we special case it here because
3648 // otherwise calls are incorrectly flagged as x87 instructions
3649 // as a result.
3650 if (MI.isCall() || MI.isInlineAsm())
3651 return false;
3652 for (const MachineOperand &MO : MI.operands()) {
3653 if (!MO.isReg())
3654 continue;
3655 if (isX87Reg(MO.getReg()))
3656 return true;
3657 }
3658 return false;
3659}
3660
3662 auto IsMemOp = [](const MCOperandInfo &OpInfo) {
3663 return OpInfo.OperandType == MCOI::OPERAND_MEMORY;
3664 };
3665
3666 const MCInstrDesc &Desc = MI.getDesc();
3667
3668 // Directly invoke the MC-layer routine for real (i.e., non-pseudo)
3669 // instructions (fast case).
3670 if (!X86II::isPseudo(Desc.TSFlags)) {
3671 int MemRefIdx = X86II::getMemoryOperandIdx(Desc);
3672 if (MemRefIdx >= 0)
3673 return MemRefIdx;
3674#ifdef EXPENSIVE_CHECKS
3675 assert(none_of(Desc.operands(), IsMemOp) &&
3676 "Got false negative from X86II::getMemoryOperandIdx()!");
3677#endif
3678 return -1;
3679 }
3680
3681 // Otherwise, handle pseudo instructions by examining the type of their
3682 // operands (slow case). An instruction cannot have a memory reference if it
3683 // has fewer than AddrNumOperands (= 5) explicit operands.
3684 unsigned NumOps = Desc.getNumOperands();
3686#ifdef EXPENSIVE_CHECKS
3687 assert(none_of(Desc.operands(), IsMemOp) &&
3688 "Expected no operands to have OPERAND_MEMORY type!");
3689#endif
3690 return -1;
3691 }
3692
3693 // The first operand with type OPERAND_MEMORY indicates the start of a memory
3694 // reference. We expect the following AddrNumOperand-1 operands to also have
3695 // OPERAND_MEMORY type.
3696 for (unsigned I = 0, E = NumOps - X86::AddrNumOperands; I != E; ++I) {
3697 if (IsMemOp(Desc.operands()[I])) {
3698#ifdef EXPENSIVE_CHECKS
3699 assert(std::all_of(Desc.operands().begin() + I,
3700 Desc.operands().begin() + I + X86::AddrNumOperands,
3701 IsMemOp) &&
3702 "Expected all five operands in the memory reference to have "
3703 "OPERAND_MEMORY type!");
3704#endif
3705 return I;
3706 }
3707 }
3708
3709 return -1;
3710}
3711
3713 unsigned OpNo) {
3714 assert(MI.getNumOperands() >= (OpNo + X86::AddrNumOperands) &&
3715 "Unexpected number of operands!");
3716
3717 const MachineOperand &Index = MI.getOperand(OpNo + X86::AddrIndexReg);
3718 if (!Index.isReg() || Index.getReg() != X86::NoRegister)
3719 return nullptr;
3720
3721 const MachineOperand &Disp = MI.getOperand(OpNo + X86::AddrDisp);
3722 if (!Disp.isCPI() || Disp.getOffset() != 0)
3723 return nullptr;
3724
3726 MI.getParent()->getParent()->getConstantPool()->getConstants();
3727 const MachineConstantPoolEntry &ConstantEntry = Constants[Disp.getIndex()];
3728
3729 // Bail if this is a machine constant pool entry, we won't be able to dig out
3730 // anything useful.
3731 if (ConstantEntry.isMachineConstantPoolEntry())
3732 return nullptr;
3733
3734 return ConstantEntry.Val.ConstVal;
3735}
3736
3738 switch (MI.getOpcode()) {
3739 case X86::TCRETURNdi:
3740 case X86::TCRETURNri:
3741 case X86::TCRETURNmi:
3742 case X86::TCRETURNdi64:
3743 case X86::TCRETURNri64:
3744 case X86::TCRETURNri64_ImpCall:
3745 case X86::TCRETURNmi64:
3746 return true;
3747 default:
3748 return false;
3749 }
3750}
3751
3754 const MachineInstr &TailCall) const {
3755
3756 const MachineFunction *MF = TailCall.getMF();
3757
3758 if (MF->getTarget().getCodeModel() == CodeModel::Kernel) {
3759 // Kernel patches thunk calls in runtime, these should never be conditional.
3760 const MachineOperand &Target = TailCall.getOperand(0);
3761 if (Target.isSymbol()) {
3762 StringRef Symbol(Target.getSymbolName());
3763 // this is currently only relevant to r11/kernel indirect thunk.
3764 if (Symbol == "__x86_indirect_thunk_r11")
3765 return false;
3766 }
3767 }
3768
3769 if (TailCall.getOpcode() != X86::TCRETURNdi &&
3770 TailCall.getOpcode() != X86::TCRETURNdi64) {
3771 // Only direct calls can be done with a conditional branch.
3772 return false;
3773 }
3774
3775 if (Subtarget.isTargetWin64() && MF->hasWinCFI()) {
3776 // Conditional tail calls confuse the Win64 unwinder.
3777 return false;
3778 }
3779
3780 assert(BranchCond.size() == 1);
3781 if (BranchCond[0].getImm() > X86::LAST_VALID_COND) {
3782 // Can't make a conditional tail call with this condition.
3783 return false;
3784 }
3785
3787 if (X86FI->getTCReturnAddrDelta() != 0 ||
3788 TailCall.getOperand(1).getImm() != 0) {
3789 // A conditional tail call cannot do any stack adjustment.
3790 return false;
3791 }
3792
3793 return true;
3794}
3795
3798 const MachineInstr &TailCall) const {
3799 assert(canMakeTailCallConditional(BranchCond, TailCall));
3800
3802 while (I != MBB.begin()) {
3803 --I;
3804 if (I->isDebugInstr())
3805 continue;
3806 if (!I->isBranch())
3807 assert(0 && "Can't find the branch to replace!");
3808
3810 assert(BranchCond.size() == 1);
3811 if (CC != BranchCond[0].getImm())
3812 continue;
3813
3814 break;
3815 }
3816
3817 unsigned Opc = TailCall.getOpcode() == X86::TCRETURNdi ? X86::TCRETURNdicc
3818 : X86::TCRETURNdi64cc;
3819
3820 auto MIB = BuildMI(MBB, I, MBB.findDebugLoc(I), get(Opc));
3821 MIB->addOperand(TailCall.getOperand(0)); // Destination.
3822 MIB.addImm(0); // Stack offset (not used).
3823 MIB->addOperand(BranchCond[0]); // Condition.
3824 MIB.copyImplicitOps(TailCall); // Regmask and (imp-used) parameters.
3825
3826 // Add implicit uses and defs of all live regs potentially clobbered by the
3827 // call. This way they still appear live across the call.
3829 LiveRegs.addLiveOuts(MBB);
3831 LiveRegs.stepForward(*MIB, Clobbers);
3832 for (const auto &C : Clobbers) {
3833 MIB.addReg(C.first, RegState::Implicit);
3835 }
3836
3837 I->eraseFromParent();
3838}
3839
3840// Given a MBB and its TBB, find the FBB which was a fallthrough MBB (it may
3841// not be a fallthrough MBB now due to layout changes). Return nullptr if the
3842// fallthrough MBB cannot be identified.
3845 // Look for non-EHPad successors other than TBB. If we find exactly one, it
3846 // is the fallthrough MBB. If we find zero, then TBB is both the target MBB
3847 // and fallthrough MBB. If we find more than one, we cannot identify the
3848 // fallthrough MBB and should return nullptr.
3849 MachineBasicBlock *FallthroughBB = nullptr;
3850 for (MachineBasicBlock *Succ : MBB->successors()) {
3851 if (Succ->isEHPad() || (Succ == TBB && FallthroughBB))
3852 continue;
3853 // Return a nullptr if we found more than one fallthrough successor.
3854 if (FallthroughBB && FallthroughBB != TBB)
3855 return nullptr;
3856 FallthroughBB = Succ;
3857 }
3858 return FallthroughBB;
3859}
3860
3861bool X86InstrInfo::analyzeBranchImpl(
3864 SmallVectorImpl<MachineInstr *> &CondBranches, bool AllowModify) const {
3865
3866 // Start from the bottom of the block and work up, examining the
3867 // terminator instructions.
3869 MachineBasicBlock::iterator UnCondBrIter = MBB.end();
3870 while (I != MBB.begin()) {
3871 --I;
3872 if (I->isDebugInstr())
3873 continue;
3874
3875 // Working from the bottom, when we see a non-terminator instruction, we're
3876 // done.
3877 if (!isUnpredicatedTerminator(*I))
3878 break;
3879
3880 // A terminator that isn't a branch can't easily be handled by this
3881 // analysis.
3882 if (!I->isBranch())
3883 return true;
3884
3885 // Handle unconditional branches.
3886 if (I->getOpcode() == X86::JMP_1) {
3887 UnCondBrIter = I;
3888
3889 if (!AllowModify) {
3890 TBB = I->getOperand(0).getMBB();
3891 continue;
3892 }
3893
3894 // If the block has any instructions after a JMP, delete them.
3895 MBB.erase(std::next(I), MBB.end());
3896
3897 Cond.clear();
3898 FBB = nullptr;
3899
3900 // Delete the JMP if it's equivalent to a fall-through.
3901 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
3902 TBB = nullptr;
3903 I->eraseFromParent();
3904 I = MBB.end();
3905 UnCondBrIter = MBB.end();
3906 continue;
3907 }
3908
3909 // TBB is used to indicate the unconditional destination.
3910 TBB = I->getOperand(0).getMBB();
3911 continue;
3912 }
3913
3914 // Handle conditional branches.
3915 X86::CondCode BranchCode = X86::getCondFromBranch(*I);
3916 if (BranchCode == X86::COND_INVALID)
3917 return true; // Can't handle indirect branch.
3918
3919 // In practice we should never have an undef eflags operand, if we do
3920 // abort here as we are not prepared to preserve the flag.
3921 if (I->findRegisterUseOperand(X86::EFLAGS, /*TRI=*/nullptr)->isUndef())
3922 return true;
3923
3924 // Working from the bottom, handle the first conditional branch.
3925 if (Cond.empty()) {
3926 FBB = TBB;
3927 TBB = I->getOperand(0).getMBB();
3929 CondBranches.push_back(&*I);
3930 continue;
3931 }
3932
3933 // Handle subsequent conditional branches. Only handle the case where all
3934 // conditional branches branch to the same destination and their condition
3935 // opcodes fit one of the special multi-branch idioms.
3936 assert(Cond.size() == 1);
3937 assert(TBB);
3938
3939 // If the conditions are the same, we can leave them alone.
3940 X86::CondCode OldBranchCode = (X86::CondCode)Cond[0].getImm();
3941 auto NewTBB = I->getOperand(0).getMBB();
3942 if (OldBranchCode == BranchCode && TBB == NewTBB)
3943 continue;
3944
3945 // If they differ, see if they fit one of the known patterns. Theoretically,
3946 // we could handle more patterns here, but we shouldn't expect to see them
3947 // if instruction selection has done a reasonable job.
3948 if (TBB == NewTBB &&
3949 ((OldBranchCode == X86::COND_P && BranchCode == X86::COND_NE) ||
3950 (OldBranchCode == X86::COND_NE && BranchCode == X86::COND_P))) {
3951 BranchCode = X86::COND_NE_OR_P;
3952 } else if ((OldBranchCode == X86::COND_NP && BranchCode == X86::COND_NE) ||
3953 (OldBranchCode == X86::COND_E && BranchCode == X86::COND_P)) {
3954 if (NewTBB != (FBB ? FBB : getFallThroughMBB(&MBB, TBB)))
3955 return true;
3956
3957 // X86::COND_E_AND_NP usually has two different branch destinations.
3958 //
3959 // JP B1
3960 // JE B2
3961 // JMP B1
3962 // B1:
3963 // B2:
3964 //
3965 // Here this condition branches to B2 only if NP && E. It has another
3966 // equivalent form:
3967 //
3968 // JNE B1
3969 // JNP B2
3970 // JMP B1
3971 // B1:
3972 // B2:
3973 //
3974 // Similarly it branches to B2 only if E && NP. That is why this condition
3975 // is named with COND_E_AND_NP.
3976 BranchCode = X86::COND_E_AND_NP;
3977 } else
3978 return true;
3979
3980 // Update the MachineOperand.
3981 Cond[0].setImm(BranchCode);
3982 CondBranches.push_back(&*I);
3983 }
3984
3985 return false;
3986}
3987
3990 MachineBasicBlock *&FBB,
3992 bool AllowModify) const {
3993 SmallVector<MachineInstr *, 4> CondBranches;
3994 return analyzeBranchImpl(MBB, TBB, FBB, Cond, CondBranches, AllowModify);
3995}
3996
3998 int MemRefBegin = X86II::getMemoryOperandIdx(MI.getDesc());
3999 assert(MemRefBegin >= 0 && "Expected a memory operand");
4000
4001 const MachineOperand &MO = MI.getOperand(MemRefBegin + X86::AddrDisp);
4002 if (!MO.isJTI())
4003 return -1;
4004
4005 return MO.getIndex();
4006}
4007
4009 Register Reg) {
4010 if (!Reg.isVirtual())
4011 return -1;
4013 if (MI == nullptr)
4014 return -1;
4015 unsigned Opcode = MI->getOpcode();
4016 if (Opcode != X86::LEA64r && Opcode != X86::LEA32r)
4017 return -1;
4019}
4020
4022 unsigned Opcode = MI.getOpcode();
4023 // Switch-jump pattern for non-PIC code looks like:
4024 // JMP64m $noreg, 8, %X, %jump-table.X, $noreg
4025 if (Opcode == X86::JMP64m || Opcode == X86::JMP32m) {
4027 }
4028 // The pattern for PIC code looks like:
4029 // %0 = LEA64r $rip, 1, $noreg, %jump-table.X
4030 // %1 = MOVSX64rm32 %0, 4, XX, 0, $noreg
4031 // %2 = ADD64rr %1, %0
4032 // JMP64r %2
4033 if (Opcode == X86::JMP64r || Opcode == X86::JMP32r) {
4034 Register Reg = MI.getOperand(0).getReg();
4035 if (!Reg.isVirtual())
4036 return -1;
4037 const MachineFunction &MF = *MI.getParent()->getParent();
4038 const MachineRegisterInfo &MRI = MF.getRegInfo();
4039 MachineInstr *Add = MRI.getUniqueVRegDef(Reg);
4040 if (Add == nullptr)
4041 return -1;
4042 if (Add->getOpcode() != X86::ADD64rr && Add->getOpcode() != X86::ADD32rr)
4043 return -1;
4044 int JTI1 = getJumpTableIndexFromReg(MRI, Add->getOperand(1).getReg());
4045 if (JTI1 >= 0)
4046 return JTI1;
4047 int JTI2 = getJumpTableIndexFromReg(MRI, Add->getOperand(2).getReg());
4048 if (JTI2 >= 0)
4049 return JTI2;
4050 }
4051 return -1;
4052}
4053
4055 MachineBranchPredicate &MBP,
4056 bool AllowModify) const {
4057 using namespace std::placeholders;
4058
4060 SmallVector<MachineInstr *, 4> CondBranches;
4061 if (analyzeBranchImpl(MBB, MBP.TrueDest, MBP.FalseDest, Cond, CondBranches,
4062 AllowModify))
4063 return true;
4064
4065 if (Cond.size() != 1)
4066 return true;
4067
4068 assert(MBP.TrueDest && "expected!");
4069
4070 if (!MBP.FalseDest)
4071 MBP.FalseDest = MBB.getNextNode();
4072
4074
4075 MachineInstr *ConditionDef = nullptr;
4076 bool SingleUseCondition = true;
4077
4079 if (MI.modifiesRegister(X86::EFLAGS, TRI)) {
4080 ConditionDef = &MI;
4081 break;
4082 }
4083
4084 if (MI.readsRegister(X86::EFLAGS, TRI))
4085 SingleUseCondition = false;
4086 }
4087
4088 if (!ConditionDef)
4089 return true;
4090
4091 if (SingleUseCondition) {
4092 for (auto *Succ : MBB.successors())
4093 if (Succ->isLiveIn(X86::EFLAGS))
4094 SingleUseCondition = false;
4095 }
4096
4097 MBP.ConditionDef = ConditionDef;
4098 MBP.SingleUseCondition = SingleUseCondition;
4099
4100 // Currently we only recognize the simple pattern:
4101 //
4102 // test %reg, %reg
4103 // je %label
4104 //
4105 const unsigned TestOpcode =
4106 Subtarget.is64Bit() ? X86::TEST64rr : X86::TEST32rr;
4107
4108 if (ConditionDef->getOpcode() == TestOpcode &&
4109 ConditionDef->getNumOperands() == 3 &&
4110 ConditionDef->getOperand(0).isIdenticalTo(ConditionDef->getOperand(1)) &&
4111 (Cond[0].getImm() == X86::COND_NE || Cond[0].getImm() == X86::COND_E)) {
4112 MBP.LHS = ConditionDef->getOperand(0);
4113 MBP.RHS = MachineOperand::CreateImm(0);
4114 MBP.Predicate = Cond[0].getImm() == X86::COND_NE
4115 ? MachineBranchPredicate::PRED_NE
4116 : MachineBranchPredicate::PRED_EQ;
4117 return false;
4118 }
4119
4120 return true;
4121}
4122
4124 int *BytesRemoved) const {
4125 assert(!BytesRemoved && "code size not handled");
4126
4128 unsigned Count = 0;
4129
4130 while (I != MBB.begin()) {
4131 --I;
4132 if (I->isDebugInstr())
4133 continue;
4134 if (I->getOpcode() != X86::JMP_1 &&
4136 break;
4137 // Remove the branch.
4138 I->eraseFromParent();
4139 I = MBB.end();
4140 ++Count;
4141 }
4142
4143 return Count;
4144}
4145
4148 MachineBasicBlock *FBB,
4150 const DebugLoc &DL, int *BytesAdded) const {
4151 // Shouldn't be a fall through.
4152 assert(TBB && "insertBranch must not be told to insert a fallthrough");
4153 assert((Cond.size() == 1 || Cond.size() == 0) &&
4154 "X86 branch conditions have one component!");
4155 assert(!BytesAdded && "code size not handled");
4156
4157 if (Cond.empty()) {
4158 // Unconditional branch?
4159 assert(!FBB && "Unconditional branch with multiple successors!");
4160 BuildMI(&MBB, DL, get(X86::JMP_1)).addMBB(TBB);
4161 return 1;
4162 }
4163
4164 // If FBB is null, it is implied to be a fall-through block.
4165 bool FallThru = FBB == nullptr;
4166
4167 // Conditional branch.
4168 unsigned Count = 0;
4170 switch (CC) {
4171 case X86::COND_NE_OR_P:
4172 // Synthesize NE_OR_P with two branches.
4173 BuildMI(&MBB, DL, get(X86::JCC_1)).addMBB(TBB).addImm(X86::COND_NE);
4174 ++Count;
4175 BuildMI(&MBB, DL, get(X86::JCC_1)).addMBB(TBB).addImm(X86::COND_P);
4176 ++Count;
4177 break;
4178 case X86::COND_E_AND_NP:
4179 // Use the next block of MBB as FBB if it is null.
4180 if (FBB == nullptr) {
4181 FBB = getFallThroughMBB(&MBB, TBB);
4182 assert(FBB && "MBB cannot be the last block in function when the false "
4183 "body is a fall-through.");
4184 }
4185 // Synthesize COND_E_AND_NP with two branches.
4186 BuildMI(&MBB, DL, get(X86::JCC_1)).addMBB(FBB).addImm(X86::COND_NE);
4187 ++Count;
4188 BuildMI(&MBB, DL, get(X86::JCC_1)).addMBB(TBB).addImm(X86::COND_NP);
4189 ++Count;
4190 break;
4191 default: {
4192 BuildMI(&MBB, DL, get(X86::JCC_1)).addMBB(TBB).addImm(CC);
4193 ++Count;
4194 }
4195 }
4196 if (!FallThru) {
4197 // Two-way Conditional branch. Insert the second branch.
4198 BuildMI(&MBB, DL, get(X86::JMP_1)).addMBB(FBB);
4199 ++Count;
4200 }
4201 return Count;
4202}
4203
4206 Register DstReg, Register TrueReg,
4207 Register FalseReg, int &CondCycles,
4208 int &TrueCycles, int &FalseCycles) const {
4209 // Not all subtargets have cmov instructions.
4210 if (!Subtarget.canUseCMOV())
4211 return false;
4212 if (Cond.size() != 1)
4213 return false;
4214 // We cannot do the composite conditions, at least not in SSA form.
4216 return false;
4217
4218 // Check register classes.
4219 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
4220 const TargetRegisterClass *RC =
4221 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
4222 if (!RC)
4223 return false;
4224
4225 // We have cmov instructions for 16, 32, and 64 bit general purpose registers.
4226 if (X86::GR16RegClass.hasSubClassEq(RC) ||
4227 X86::GR32RegClass.hasSubClassEq(RC) ||
4228 X86::GR64RegClass.hasSubClassEq(RC)) {
4229 // This latency applies to Pentium M, Merom, Wolfdale, Nehalem, and Sandy
4230 // Bridge. Probably Ivy Bridge as well.
4231 CondCycles = 2;
4232 TrueCycles = 2;
4233 FalseCycles = 2;
4234 return true;
4235 }
4236
4237 // Can't do vectors.
4238 return false;
4239}
4240
4243 const DebugLoc &DL, Register DstReg,
4245 Register FalseReg) const {
4246 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
4248 const TargetRegisterClass &RC = *MRI.getRegClass(DstReg);
4249 assert(Cond.size() == 1 && "Invalid Cond array");
4250 unsigned Opc =
4251 X86::getCMovOpcode(TRI.getRegSizeInBits(RC) / 8,
4252 false /*HasMemoryOperand*/, Subtarget.hasNDD());
4253 BuildMI(MBB, I, DL, get(Opc), DstReg)
4254 .addReg(FalseReg)
4255 .addReg(TrueReg)
4256 .addImm(Cond[0].getImm());
4257}
4258
4259/// Test if the given register is a physical h register.
4260static bool isHReg(Register Reg) {
4261 return X86::GR8_ABCD_HRegClass.contains(Reg);
4262}
4263
4264// Try and copy between VR128/VR64 and GR64 registers.
4265static unsigned CopyToFromAsymmetricReg(Register DestReg, Register SrcReg,
4266 const X86Subtarget &Subtarget) {
4267 bool HasAVX = Subtarget.hasAVX();
4268 bool HasAVX512 = Subtarget.hasAVX512();
4269 bool HasEGPR = Subtarget.hasEGPR();
4270
4271 // SrcReg(MaskReg) -> DestReg(GR64)
4272 // SrcReg(MaskReg) -> DestReg(GR32)
4273
4274 // All KMASK RegClasses hold the same k registers, can be tested against
4275 // anyone.
4276 if (X86::VK16RegClass.contains(SrcReg)) {
4277 if (X86::GR64RegClass.contains(DestReg)) {
4278 assert(Subtarget.hasBWI());
4279 return HasEGPR ? X86::KMOVQrk_EVEX : X86::KMOVQrk;
4280 }
4281 if (X86::GR32RegClass.contains(DestReg))
4282 return Subtarget.hasBWI() ? (HasEGPR ? X86::KMOVDrk_EVEX : X86::KMOVDrk)
4283 : (HasEGPR ? X86::KMOVWrk_EVEX : X86::KMOVWrk);
4284 }
4285
4286 // SrcReg(GR64) -> DestReg(MaskReg)
4287 // SrcReg(GR32) -> DestReg(MaskReg)
4288
4289 // All KMASK RegClasses hold the same k registers, can be tested against
4290 // anyone.
4291 if (X86::VK16RegClass.contains(DestReg)) {
4292 if (X86::GR64RegClass.contains(SrcReg)) {
4293 assert(Subtarget.hasBWI());
4294 return HasEGPR ? X86::KMOVQkr_EVEX : X86::KMOVQkr;
4295 }
4296 if (X86::GR32RegClass.contains(SrcReg))
4297 return Subtarget.hasBWI() ? (HasEGPR ? X86::KMOVDkr_EVEX : X86::KMOVDkr)
4298 : (HasEGPR ? X86::KMOVWkr_EVEX : X86::KMOVWkr);
4299 }
4300
4301 // SrcReg(VR128) -> DestReg(GR64)
4302 // SrcReg(VR64) -> DestReg(GR64)
4303 // SrcReg(GR64) -> DestReg(VR128)
4304 // SrcReg(GR64) -> DestReg(VR64)
4305
4306 if (X86::GR64RegClass.contains(DestReg)) {
4307 if (X86::VR128XRegClass.contains(SrcReg))
4308 // Copy from a VR128 register to a GR64 register.
4309 return HasAVX512 ? X86::VMOVPQIto64Zrr
4310 : HasAVX ? X86::VMOVPQIto64rr
4311 : X86::MOVPQIto64rr;
4312 if (X86::VR64RegClass.contains(SrcReg))
4313 // Copy from a VR64 register to a GR64 register.
4314 return X86::MMX_MOVD64from64rr;
4315 } else if (X86::GR64RegClass.contains(SrcReg)) {
4316 // Copy from a GR64 register to a VR128 register.
4317 if (X86::VR128XRegClass.contains(DestReg))
4318 return HasAVX512 ? X86::VMOV64toPQIZrr
4319 : HasAVX ? X86::VMOV64toPQIrr
4320 : X86::MOV64toPQIrr;
4321 // Copy from a GR64 register to a VR64 register.
4322 if (X86::VR64RegClass.contains(DestReg))
4323 return X86::MMX_MOVD64to64rr;
4324 }
4325
4326 // SrcReg(VR128) -> DestReg(GR32)
4327 // SrcReg(GR32) -> DestReg(VR128)
4328
4329 if (X86::GR32RegClass.contains(DestReg) &&
4330 X86::VR128XRegClass.contains(SrcReg))
4331 // Copy from a VR128 register to a GR32 register.
4332 return HasAVX512 ? X86::VMOVPDI2DIZrr
4333 : HasAVX ? X86::VMOVPDI2DIrr
4334 : X86::MOVPDI2DIrr;
4335
4336 if (X86::VR128XRegClass.contains(DestReg) &&
4337 X86::GR32RegClass.contains(SrcReg))
4338 // Copy from a GR32 register to a VR128 register.
4339 return HasAVX512 ? X86::VMOVDI2PDIZrr
4340 : HasAVX ? X86::VMOVDI2PDIrr
4341 : X86::MOVDI2PDIrr;
4342
4343 return 0;
4344}
4345
4348 const DebugLoc &DL, Register DestReg,
4349 Register SrcReg, bool KillSrc,
4350 bool RenamableDest, bool RenamableSrc) const {
4351 // First deal with the normal symmetric copies.
4352 bool HasAVX = Subtarget.hasAVX();
4353 bool HasVLX = Subtarget.hasVLX();
4354 bool HasEGPR = Subtarget.hasEGPR();
4355 unsigned Opc = 0;
4356 if (X86::GR64RegClass.contains(DestReg, SrcReg))
4357 Opc = X86::MOV64rr;
4358 else if (X86::GR32RegClass.contains(DestReg, SrcReg))
4359 Opc = X86::MOV32rr;
4360 else if (X86::GR16RegClass.contains(DestReg, SrcReg))
4361 Opc = X86::MOV16rr;
4362 else if (X86::GR8RegClass.contains(DestReg, SrcReg)) {
4363 // Copying to or from a physical H register on x86-64 requires a NOREX
4364 // move. Otherwise use a normal move.
4365 if ((isHReg(DestReg) || isHReg(SrcReg)) && Subtarget.is64Bit()) {
4366 Opc = X86::MOV8rr_NOREX;
4367 // Both operands must be encodable without an REX prefix.
4368 assert(X86::GR8_NOREXRegClass.contains(SrcReg, DestReg) &&
4369 "8-bit H register can not be copied outside GR8_NOREX");
4370 } else
4371 Opc = X86::MOV8rr;
4372 } else if (X86::VR64RegClass.contains(DestReg, SrcReg))
4373 Opc = X86::MMX_MOVQ64rr;
4374 else if (X86::VR128XRegClass.contains(DestReg, SrcReg)) {
4375 if (HasVLX)
4376 Opc = X86::VMOVAPSZ128rr;
4377 else if (X86::VR128RegClass.contains(DestReg, SrcReg))
4378 Opc = HasAVX ? X86::VMOVAPSrr : X86::MOVAPSrr;
4379 else {
4380 // If this an extended register and we don't have VLX we need to use a
4381 // 512-bit move.
4382 Opc = X86::VMOVAPSZrr;
4384 DestReg =
4385 TRI->getMatchingSuperReg(DestReg, X86::sub_xmm, &X86::VR512RegClass);
4386 SrcReg =
4387 TRI->getMatchingSuperReg(SrcReg, X86::sub_xmm, &X86::VR512RegClass);
4388 }
4389 } else if (X86::VR256XRegClass.contains(DestReg, SrcReg)) {
4390 if (HasVLX)
4391 Opc = X86::VMOVAPSZ256rr;
4392 else if (X86::VR256RegClass.contains(DestReg, SrcReg))
4393 Opc = X86::VMOVAPSYrr;
4394 else {
4395 // If this an extended register and we don't have VLX we need to use a
4396 // 512-bit move.
4397 Opc = X86::VMOVAPSZrr;
4399 DestReg =
4400 TRI->getMatchingSuperReg(DestReg, X86::sub_ymm, &X86::VR512RegClass);
4401 SrcReg =
4402 TRI->getMatchingSuperReg(SrcReg, X86::sub_ymm, &X86::VR512RegClass);
4403 }
4404 } else if (X86::VR512RegClass.contains(DestReg, SrcReg))
4405 Opc = X86::VMOVAPSZrr;
4406 // All KMASK RegClasses hold the same k registers, can be tested against
4407 // anyone.
4408 else if (X86::VK16RegClass.contains(DestReg, SrcReg))
4409 Opc = Subtarget.hasBWI() ? (HasEGPR ? X86::KMOVQkk_EVEX : X86::KMOVQkk)
4410 : (HasEGPR ? X86::KMOVWkk_EVEX : X86::KMOVWkk);
4411
4412 if (!Opc)
4413 Opc = CopyToFromAsymmetricReg(DestReg, SrcReg, Subtarget);
4414
4415 if (Opc) {
4416 BuildMI(MBB, MI, DL, get(Opc), DestReg)
4417 .addReg(SrcReg, getKillRegState(KillSrc));
4418 return;
4419 }
4420
4421 if (SrcReg == X86::EFLAGS || DestReg == X86::EFLAGS) {
4422 // FIXME: We use a fatal error here because historically LLVM has tried
4423 // lower some of these physreg copies and we want to ensure we get
4424 // reasonable bug reports if someone encounters a case no other testing
4425 // found. This path should be removed after the LLVM 7 release.
4426 report_fatal_error("Unable to copy EFLAGS physical register!");
4427 }
4428
4429 LLVM_DEBUG(dbgs() << "Cannot copy " << RI.getName(SrcReg) << " to "
4430 << RI.getName(DestReg) << '\n');
4431 report_fatal_error("Cannot emit physreg copy instruction");
4432}
4433
4434std::optional<DestSourcePair>
4436 if (MI.isMoveReg()) {
4437 // FIXME: Dirty hack for apparent invariant that doesn't hold when
4438 // subreg_to_reg is coalesced with ordinary copies, such that the bits that
4439 // were asserted as 0 are now undef.
4440 if (MI.getOperand(0).isUndef() && MI.getOperand(0).getSubReg())
4441 return std::nullopt;
4442
4443 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
4444 }
4445 return std::nullopt;
4446}
4447
4448static unsigned getLoadStoreOpcodeForFP16(bool Load, const X86Subtarget &STI) {
4449 if (STI.hasFP16())
4450 return Load ? X86::VMOVSHZrm_alt : X86::VMOVSHZmr;
4451 if (Load)
4452 return X86::MOVSHPrm;
4453 return X86::MOVSHPmr;
4454}
4455
4457 const TargetRegisterClass *RC,
4458 bool IsStackAligned,
4459 const X86Subtarget &STI, bool Load) {
4460 bool HasAVX = STI.hasAVX();
4461 bool HasAVX512 = STI.hasAVX512();
4462 bool HasVLX = STI.hasVLX();
4463 bool HasEGPR = STI.hasEGPR();
4464
4465 assert(RC != nullptr && "Invalid target register class");
4466 switch (STI.getRegisterInfo()->getSpillSize(*RC)) {
4467 default:
4468 llvm_unreachable("Unknown spill size");
4469 case 1:
4470 assert(X86::GR8RegClass.hasSubClassEq(RC) && "Unknown 1-byte regclass");
4471 if (STI.is64Bit())
4472 // Copying to or from a physical H register on x86-64 requires a NOREX
4473 // move. Otherwise use a normal move.
4474 if (isHReg(Reg) || X86::GR8_ABCD_HRegClass.hasSubClassEq(RC))
4475 return Load ? X86::MOV8rm_NOREX : X86::MOV8mr_NOREX;
4476 return Load ? X86::MOV8rm : X86::MOV8mr;
4477 case 2:
4478 if (X86::VK16RegClass.hasSubClassEq(RC))
4479 return Load ? (HasEGPR ? X86::KMOVWkm_EVEX : X86::KMOVWkm)
4480 : (HasEGPR ? X86::KMOVWmk_EVEX : X86::KMOVWmk);
4481 assert(X86::GR16RegClass.hasSubClassEq(RC) && "Unknown 2-byte regclass");
4482 return Load ? X86::MOV16rm : X86::MOV16mr;
4483 case 4:
4484 if (X86::GR32RegClass.hasSubClassEq(RC))
4485 return Load ? X86::MOV32rm : X86::MOV32mr;
4486 if (X86::FR32XRegClass.hasSubClassEq(RC))
4487 return Load ? (HasAVX512 ? X86::VMOVSSZrm_alt
4488 : HasAVX ? X86::VMOVSSrm_alt
4489 : X86::MOVSSrm_alt)
4490 : (HasAVX512 ? X86::VMOVSSZmr
4491 : HasAVX ? X86::VMOVSSmr
4492 : X86::MOVSSmr);
4493 if (X86::RFP32RegClass.hasSubClassEq(RC))
4494 return Load ? X86::LD_Fp32m : X86::ST_Fp32m;
4495 if (X86::VK32RegClass.hasSubClassEq(RC)) {
4496 assert(STI.hasBWI() && "KMOVD requires BWI");
4497 return Load ? (HasEGPR ? X86::KMOVDkm_EVEX : X86::KMOVDkm)
4498 : (HasEGPR ? X86::KMOVDmk_EVEX : X86::KMOVDmk);
4499 }
4500 // All of these mask pair classes have the same spill size, the same kind
4501 // of kmov instructions can be used with all of them.
4502 if (X86::VK1PAIRRegClass.hasSubClassEq(RC) ||
4503 X86::VK2PAIRRegClass.hasSubClassEq(RC) ||
4504 X86::VK4PAIRRegClass.hasSubClassEq(RC) ||
4505 X86::VK8PAIRRegClass.hasSubClassEq(RC) ||
4506 X86::VK16PAIRRegClass.hasSubClassEq(RC))
4507 return Load ? X86::MASKPAIR16LOAD : X86::MASKPAIR16STORE;
4508 if (X86::FR16RegClass.hasSubClassEq(RC) ||
4509 X86::FR16XRegClass.hasSubClassEq(RC))
4510 return getLoadStoreOpcodeForFP16(Load, STI);
4511 llvm_unreachable("Unknown 4-byte regclass");
4512 case 8:
4513 if (X86::GR64RegClass.hasSubClassEq(RC))
4514 return Load ? X86::MOV64rm : X86::MOV64mr;
4515 if (X86::FR64XRegClass.hasSubClassEq(RC))
4516 return Load ? (HasAVX512 ? X86::VMOVSDZrm_alt
4517 : HasAVX ? X86::VMOVSDrm_alt
4518 : X86::MOVSDrm_alt)
4519 : (HasAVX512 ? X86::VMOVSDZmr
4520 : HasAVX ? X86::VMOVSDmr
4521 : X86::MOVSDmr);
4522 if (X86::VR64RegClass.hasSubClassEq(RC))
4523 return Load ? X86::MMX_MOVQ64rm : X86::MMX_MOVQ64mr;
4524 if (X86::RFP64RegClass.hasSubClassEq(RC))
4525 return Load ? X86::LD_Fp64m : X86::ST_Fp64m;
4526 if (X86::VK64RegClass.hasSubClassEq(RC)) {
4527 assert(STI.hasBWI() && "KMOVQ requires BWI");
4528 return Load ? (HasEGPR ? X86::KMOVQkm_EVEX : X86::KMOVQkm)
4529 : (HasEGPR ? X86::KMOVQmk_EVEX : X86::KMOVQmk);
4530 }
4531 llvm_unreachable("Unknown 8-byte regclass");
4532 case 10:
4533 assert(X86::RFP80RegClass.hasSubClassEq(RC) && "Unknown 10-byte regclass");
4534 return Load ? X86::LD_Fp80m : X86::ST_FpP80m;
4535 case 16: {
4536 if (X86::VR128XRegClass.hasSubClassEq(RC)) {
4537 // If stack is realigned we can use aligned stores.
4538 if (IsStackAligned)
4539 return Load ? (HasVLX ? X86::VMOVAPSZ128rm
4540 : HasAVX512 ? X86::VMOVAPSZ128rm_NOVLX
4541 : HasAVX ? X86::VMOVAPSrm
4542 : X86::MOVAPSrm)
4543 : (HasVLX ? X86::VMOVAPSZ128mr
4544 : HasAVX512 ? X86::VMOVAPSZ128mr_NOVLX
4545 : HasAVX ? X86::VMOVAPSmr
4546 : X86::MOVAPSmr);
4547 else
4548 return Load ? (HasVLX ? X86::VMOVUPSZ128rm
4549 : HasAVX512 ? X86::VMOVUPSZ128rm_NOVLX
4550 : HasAVX ? X86::VMOVUPSrm
4551 : X86::MOVUPSrm)
4552 : (HasVLX ? X86::VMOVUPSZ128mr
4553 : HasAVX512 ? X86::VMOVUPSZ128mr_NOVLX
4554 : HasAVX ? X86::VMOVUPSmr
4555 : X86::MOVUPSmr);
4556 }
4557 llvm_unreachable("Unknown 16-byte regclass");
4558 }
4559 case 32:
4560 assert(X86::VR256XRegClass.hasSubClassEq(RC) && "Unknown 32-byte regclass");
4561 // If stack is realigned we can use aligned stores.
4562 if (IsStackAligned)
4563 return Load ? (HasVLX ? X86::VMOVAPSZ256rm
4564 : HasAVX512 ? X86::VMOVAPSZ256rm_NOVLX
4565 : X86::VMOVAPSYrm)
4566 : (HasVLX ? X86::VMOVAPSZ256mr
4567 : HasAVX512 ? X86::VMOVAPSZ256mr_NOVLX
4568 : X86::VMOVAPSYmr);
4569 else
4570 return Load ? (HasVLX ? X86::VMOVUPSZ256rm
4571 : HasAVX512 ? X86::VMOVUPSZ256rm_NOVLX
4572 : X86::VMOVUPSYrm)
4573 : (HasVLX ? X86::VMOVUPSZ256mr
4574 : HasAVX512 ? X86::VMOVUPSZ256mr_NOVLX
4575 : X86::VMOVUPSYmr);
4576 case 64:
4577 assert(X86::VR512RegClass.hasSubClassEq(RC) && "Unknown 64-byte regclass");
4578 assert(STI.hasAVX512() && "Using 512-bit register requires AVX512");
4579 if (IsStackAligned)
4580 return Load ? X86::VMOVAPSZrm : X86::VMOVAPSZmr;
4581 else
4582 return Load ? X86::VMOVUPSZrm : X86::VMOVUPSZmr;
4583 case 1024:
4584 assert(X86::TILERegClass.hasSubClassEq(RC) && "Unknown 1024-byte regclass");
4585 assert(STI.hasAMXTILE() && "Using 8*1024-bit register requires AMX-TILE");
4586#define GET_EGPR_IF_ENABLED(OPC) (STI.hasEGPR() ? OPC##_EVEX : OPC)
4587 return Load ? GET_EGPR_IF_ENABLED(X86::TILELOADD)
4588 : GET_EGPR_IF_ENABLED(X86::TILESTORED);
4589#undef GET_EGPR_IF_ENABLED
4590 }
4591}
4592
4593std::optional<ExtAddrMode>
4595 const TargetRegisterInfo *TRI) const {
4596 int MemRefBegin = X86II::getMemoryOperandIdx(MemI.getDesc());
4597 if (MemRefBegin < 0)
4598 return std::nullopt;
4599
4600 auto &BaseOp = MemI.getOperand(MemRefBegin + X86::AddrBaseReg);
4601 if (!BaseOp.isReg()) // Can be an MO_FrameIndex
4602 return std::nullopt;
4603
4604 const MachineOperand &DispMO = MemI.getOperand(MemRefBegin + X86::AddrDisp);
4605 // Displacement can be symbolic
4606 if (!DispMO.isImm())
4607 return std::nullopt;
4608
4609 ExtAddrMode AM;
4610 AM.BaseReg = BaseOp.getReg();
4611 AM.ScaledReg = MemI.getOperand(MemRefBegin + X86::AddrIndexReg).getReg();
4612 AM.Scale = MemI.getOperand(MemRefBegin + X86::AddrScaleAmt).getImm();
4613 AM.Displacement = DispMO.getImm();
4614 return AM;
4615}
4616
4618 StringRef &ErrInfo) const {
4619 std::optional<ExtAddrMode> AMOrNone = getAddrModeFromMemoryOp(MI, nullptr);
4620 if (!AMOrNone)
4621 return true;
4622
4623 ExtAddrMode AM = *AMOrNone;
4625 if (AM.ScaledReg != X86::NoRegister) {
4626 switch (AM.Scale) {
4627 case 1:
4628 case 2:
4629 case 4:
4630 case 8:
4631 break;
4632 default:
4633 ErrInfo = "Scale factor in address must be 1, 2, 4 or 8";
4634 return false;
4635 }
4636 }
4637 if (!isInt<32>(AM.Displacement)) {
4638 ErrInfo = "Displacement in address must fit into 32-bit signed "
4639 "integer";
4640 return false;
4641 }
4642
4643 return true;
4644}
4645
4647 const Register Reg,
4648 int64_t &ImmVal) const {
4649 Register MovReg = Reg;
4650 const MachineInstr *MovMI = &MI;
4651
4652 // Follow use-def for SUBREG_TO_REG to find the real move immediate
4653 // instruction. It is quite common for x86-64.
4654 if (MI.isSubregToReg()) {
4655 // We use following pattern to setup 64b immediate.
4656 // %8:gr32 = MOV32r0 implicit-def dead $eflags
4657 // %6:gr64 = SUBREG_TO_REG killed %8:gr32, %subreg.sub_32bit
4658 unsigned SubIdx = MI.getOperand(2).getImm();
4659 MovReg = MI.getOperand(1).getReg();
4660 if (SubIdx != X86::sub_32bit)
4661 return false;
4662 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
4663 MovMI = MRI.getUniqueVRegDef(MovReg);
4664 if (!MovMI)
4665 return false;
4666 }
4667
4668 if (MovMI->getOpcode() == X86::MOV32r0 &&
4669 MovMI->getOperand(0).getReg() == MovReg) {
4670 ImmVal = 0;
4671 return true;
4672 }
4673
4674 if (MovMI->getOpcode() != X86::MOV32ri &&
4675 MovMI->getOpcode() != X86::MOV64ri &&
4676 MovMI->getOpcode() != X86::MOV32ri64 && MovMI->getOpcode() != X86::MOV8ri)
4677 return false;
4678 // Mov Src can be a global address.
4679 if (!MovMI->getOperand(1).isImm() || MovMI->getOperand(0).getReg() != MovReg)
4680 return false;
4681 ImmVal = MovMI->getOperand(1).getImm();
4682 return true;
4683}
4684
4686 const MachineInstr *MI, const Register NullValueReg,
4687 const TargetRegisterInfo *TRI) const {
4688 if (!MI->modifiesRegister(NullValueReg, TRI))
4689 return true;
4690 switch (MI->getOpcode()) {
4691 // Shift right/left of a null unto itself is still a null, i.e. rax = shl rax
4692 // X.
4693 case X86::SHR64ri:
4694 case X86::SHR32ri:
4695 case X86::SHL64ri:
4696 case X86::SHL32ri:
4697 assert(MI->getOperand(0).isDef() && MI->getOperand(1).isUse() &&
4698 "expected for shift opcode!");
4699 return MI->getOperand(0).getReg() == NullValueReg &&
4700 MI->getOperand(1).getReg() == NullValueReg;
4701 // Zero extend of a sub-reg of NullValueReg into itself does not change the
4702 // null value.
4703 case X86::MOV32rr:
4704 return llvm::all_of(MI->operands(), [&](const MachineOperand &MO) {
4705 return TRI->isSubRegisterEq(NullValueReg, MO.getReg());
4706 });
4707 default:
4708 return false;
4709 }
4710 llvm_unreachable("Should be handled above!");
4711}
4712
4715 int64_t &Offset, bool &OffsetIsScalable, LocationSize &Width,
4716 const TargetRegisterInfo *TRI) const {
4717 int MemRefBegin = X86II::getMemoryOperandIdx(MemOp.getDesc());
4718 if (MemRefBegin < 0)
4719 return false;
4720
4721 const MachineOperand *BaseOp =
4722 &MemOp.getOperand(MemRefBegin + X86::AddrBaseReg);
4723 if (!BaseOp->isReg()) // Can be an MO_FrameIndex
4724 return false;
4725
4726 if (MemOp.getOperand(MemRefBegin + X86::AddrScaleAmt).getImm() != 1)
4727 return false;
4728
4729 if (MemOp.getOperand(MemRefBegin + X86::AddrIndexReg).getReg() !=
4730 X86::NoRegister)
4731 return false;
4732
4733 const MachineOperand &DispMO = MemOp.getOperand(MemRefBegin + X86::AddrDisp);
4734
4735 // Displacement can be symbolic
4736 if (!DispMO.isImm())
4737 return false;
4738
4739 Offset = DispMO.getImm();
4740
4741 if (!BaseOp->isReg())
4742 return false;
4743
4744 OffsetIsScalable = false;
4745 // FIXME: Relying on memoperands() may not be right thing to do here. Check
4746 // with X86 maintainers, and fix it accordingly. For now, it is ok, since
4747 // there is no use of `Width` for X86 back-end at the moment.
4748 Width = !MemOp.memoperands_empty() ? MemOp.memoperands().front()->getSize()
4750 BaseOps.push_back(BaseOp);
4751 return true;
4752}
4753
4754static unsigned getStoreRegOpcode(Register SrcReg,
4755 const TargetRegisterClass *RC,
4756 bool IsStackAligned,
4757 const X86Subtarget &STI) {
4758 return getLoadStoreRegOpcode(SrcReg, RC, IsStackAligned, STI, false);
4759}
4760
4761static unsigned getLoadRegOpcode(Register DestReg,
4762 const TargetRegisterClass *RC,
4763 bool IsStackAligned, const X86Subtarget &STI) {
4764 return getLoadStoreRegOpcode(DestReg, RC, IsStackAligned, STI, true);
4765}
4766
4767static bool isAMXOpcode(unsigned Opc) {
4768 switch (Opc) {
4769 default:
4770 return false;
4771 case X86::TILELOADD:
4772 case X86::TILESTORED:
4773 case X86::TILELOADD_EVEX:
4774 case X86::TILESTORED_EVEX:
4775 return true;
4776 }
4777}
4778
4781 unsigned Opc, Register Reg, int FrameIdx,
4782 bool isKill) const {
4783 switch (Opc) {
4784 default:
4785 llvm_unreachable("Unexpected special opcode!");
4786 case X86::TILESTORED:
4787 case X86::TILESTORED_EVEX: {
4788 // tilestored %tmm, (%sp, %idx)
4789 MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
4790 Register VirtReg = RegInfo.createVirtualRegister(&X86::GR64_NOSPRegClass);
4791 BuildMI(MBB, MI, DebugLoc(), get(X86::MOV64ri), VirtReg).addImm(64);
4792 MachineInstr *NewMI =
4793 addFrameReference(BuildMI(MBB, MI, DebugLoc(), get(Opc)), FrameIdx)
4794 .addReg(Reg, getKillRegState(isKill));
4796 MO.setReg(VirtReg);
4797 MO.setIsKill(true);
4798 break;
4799 }
4800 case X86::TILELOADD:
4801 case X86::TILELOADD_EVEX: {
4802 // tileloadd (%sp, %idx), %tmm
4803 MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
4804 Register VirtReg = RegInfo.createVirtualRegister(&X86::GR64_NOSPRegClass);
4805 BuildMI(MBB, MI, DebugLoc(), get(X86::MOV64ri), VirtReg).addImm(64);
4807 BuildMI(MBB, MI, DebugLoc(), get(Opc), Reg), FrameIdx);
4809 MO.setReg(VirtReg);
4810 MO.setIsKill(true);
4811 break;
4812 }
4813 }
4814}
4815
4818 bool isKill, int FrameIdx, const TargetRegisterClass *RC,
4819
4820 Register VReg, MachineInstr::MIFlag Flags) const {
4821 const MachineFunction &MF = *MBB.getParent();
4822 const MachineFrameInfo &MFI = MF.getFrameInfo();
4823 assert(MFI.getObjectSize(FrameIdx) >= RI.getSpillSize(*RC) &&
4824 "Stack slot too small for store");
4825
4826 unsigned Alignment = std::max<uint32_t>(RI.getSpillSize(*RC), 16);
4827 bool isAligned =
4828 (Subtarget.getFrameLowering()->getStackAlign() >= Alignment) ||
4829 (RI.canRealignStack(MF) && !MFI.isFixedObjectIndex(FrameIdx));
4830
4831 unsigned Opc = getStoreRegOpcode(SrcReg, RC, isAligned, Subtarget);
4832 if (isAMXOpcode(Opc))
4833 loadStoreTileReg(MBB, MI, Opc, SrcReg, FrameIdx, isKill);
4834 else
4835 addFrameReference(BuildMI(MBB, MI, DebugLoc(), get(Opc)), FrameIdx)
4836 .addReg(SrcReg, getKillRegState(isKill))
4837 .setMIFlag(Flags);
4838}
4839
4842 Register DestReg, int FrameIdx,
4843 const TargetRegisterClass *RC,
4844 Register VReg, unsigned SubReg,
4845 MachineInstr::MIFlag Flags) const {
4846 const MachineFunction &MF = *MBB.getParent();
4847 const MachineFrameInfo &MFI = MF.getFrameInfo();
4848 assert(MFI.getObjectSize(FrameIdx) >= RI.getSpillSize(*RC) &&
4849 "Load size exceeds stack slot");
4850 unsigned Alignment = std::max<uint32_t>(RI.getSpillSize(*RC), 16);
4851 bool isAligned =
4852 (Subtarget.getFrameLowering()->getStackAlign() >= Alignment) ||
4853 (RI.canRealignStack(MF) && !MFI.isFixedObjectIndex(FrameIdx));
4854
4855 unsigned Opc = getLoadRegOpcode(DestReg, RC, isAligned, Subtarget);
4856 if (isAMXOpcode(Opc))
4857 loadStoreTileReg(MBB, MI, Opc, DestReg, FrameIdx);
4858 else
4859 addFrameReference(BuildMI(MBB, MI, DebugLoc(), get(Opc), DestReg), FrameIdx)
4860 .setMIFlag(Flags);
4861}
4862
4864 Register &SrcReg2, int64_t &CmpMask,
4865 int64_t &CmpValue) const {
4866 switch (MI.getOpcode()) {
4867 default:
4868 break;
4869 case X86::CMP64ri32:
4870 case X86::CMP32ri:
4871 case X86::CMP16ri:
4872 case X86::CMP8ri:
4873 SrcReg = MI.getOperand(0).getReg();
4874 SrcReg2 = 0;
4875 if (MI.getOperand(1).isImm()) {
4876 CmpMask = ~0;
4877 CmpValue = MI.getOperand(1).getImm();
4878 } else {
4879 CmpMask = CmpValue = 0;
4880 }
4881 return true;
4882 // A SUB can be used to perform comparison.
4883 CASE_ND(SUB64rm)
4884 CASE_ND(SUB32rm)
4885 CASE_ND(SUB16rm)
4886 CASE_ND(SUB8rm)
4887 SrcReg = MI.getOperand(1).getReg();
4888 SrcReg2 = 0;
4889 CmpMask = 0;
4890 CmpValue = 0;
4891 return true;
4892 CASE_ND(SUB64rr)
4893 CASE_ND(SUB32rr)
4894 CASE_ND(SUB16rr)
4895 CASE_ND(SUB8rr)
4896 SrcReg = MI.getOperand(1).getReg();
4897 SrcReg2 = MI.getOperand(2).getReg();
4898 CmpMask = 0;
4899 CmpValue = 0;
4900 return true;
4901 CASE_ND(SUB64ri32)
4902 CASE_ND(SUB32ri)
4903 CASE_ND(SUB16ri)
4904 CASE_ND(SUB8ri)
4905 SrcReg = MI.getOperand(1).getReg();
4906 SrcReg2 = 0;
4907 if (MI.getOperand(2).isImm()) {
4908 CmpMask = ~0;
4909 CmpValue = MI.getOperand(2).getImm();
4910 } else {
4911 CmpMask = CmpValue = 0;
4912 }
4913 return true;
4914 case X86::CMP64rr:
4915 case X86::CMP32rr:
4916 case X86::CMP16rr:
4917 case X86::CMP8rr:
4918 SrcReg = MI.getOperand(0).getReg();
4919 SrcReg2 = MI.getOperand(1).getReg();
4920 CmpMask = 0;
4921 CmpValue = 0;
4922 return true;
4923 case X86::TEST8rr:
4924 case X86::TEST16rr:
4925 case X86::TEST32rr:
4926 case X86::TEST64rr:
4927 SrcReg = MI.getOperand(0).getReg();
4928 if (MI.getOperand(1).getReg() != SrcReg)
4929 return false;
4930 // Compare against zero.
4931 SrcReg2 = 0;
4932 CmpMask = ~0;
4933 CmpValue = 0;
4934 return true;
4935 case X86::TEST64ri32:
4936 case X86::TEST32ri:
4937 case X86::TEST16ri:
4938 case X86::TEST8ri:
4939 SrcReg = MI.getOperand(0).getReg();
4940 SrcReg2 = 0;
4941 // Force identical compare.
4942 CmpMask = 0;
4943 CmpValue = 0;
4944 return true;
4945 }
4946 return false;
4947}
4948
4949bool X86InstrInfo::isRedundantFlagInstr(const MachineInstr &FlagI,
4950 Register SrcReg, Register SrcReg2,
4951 int64_t ImmMask, int64_t ImmValue,
4952 const MachineInstr &OI, bool *IsSwapped,
4953 int64_t *ImmDelta) const {
4954 switch (OI.getOpcode()) {
4955 case X86::CMP64rr:
4956 case X86::CMP32rr:
4957 case X86::CMP16rr:
4958 case X86::CMP8rr:
4959 CASE_ND(SUB64rr)
4960 CASE_ND(SUB32rr)
4961 CASE_ND(SUB16rr)
4962 CASE_ND(SUB8rr) {
4963 Register OISrcReg;
4964 Register OISrcReg2;
4965 int64_t OIMask;
4966 int64_t OIValue;
4967 if (!analyzeCompare(OI, OISrcReg, OISrcReg2, OIMask, OIValue) ||
4968 OIMask != ImmMask || OIValue != ImmValue)
4969 return false;
4970 if (SrcReg == OISrcReg && SrcReg2 == OISrcReg2) {
4971 *IsSwapped = false;
4972 return true;
4973 }
4974 if (SrcReg == OISrcReg2 && SrcReg2 == OISrcReg) {
4975 *IsSwapped = true;
4976 return true;
4977 }
4978 return false;
4979 }
4980 case X86::CMP64ri32:
4981 case X86::CMP32ri:
4982 case X86::CMP16ri:
4983 case X86::CMP8ri:
4984 case X86::TEST64ri32:
4985 case X86::TEST32ri:
4986 case X86::TEST16ri:
4987 case X86::TEST8ri:
4988 CASE_ND(SUB64ri32)
4989 CASE_ND(SUB32ri)
4990 CASE_ND(SUB16ri)
4991 CASE_ND(SUB8ri)
4992 case X86::TEST64rr:
4993 case X86::TEST32rr:
4994 case X86::TEST16rr:
4995 case X86::TEST8rr: {
4996 if (ImmMask != 0) {
4997 Register OISrcReg;
4998 Register OISrcReg2;
4999 int64_t OIMask;
5000 int64_t OIValue;
5001 if (analyzeCompare(OI, OISrcReg, OISrcReg2, OIMask, OIValue) &&
5002 SrcReg == OISrcReg && ImmMask == OIMask) {
5003 if (OIValue == ImmValue) {
5004 *ImmDelta = 0;
5005 return true;
5006 } else if (static_cast<uint64_t>(ImmValue) ==
5007 static_cast<uint64_t>(OIValue) - 1) {
5008 *ImmDelta = -1;
5009 return true;
5010 } else if (static_cast<uint64_t>(ImmValue) ==
5011 static_cast<uint64_t>(OIValue) + 1) {
5012 *ImmDelta = 1;
5013 return true;
5014 } else {
5015 return false;
5016 }
5017 }
5018 }
5019 return FlagI.isIdenticalTo(OI);
5020 }
5021 default:
5022 return false;
5023 }
5024}
5025
5026inline static bool isCmpRedundantAfterLTZCNT(Register SrcReg, Register SrcReg2,
5027 int64_t ImmMask, int64_t ImmValue,
5028 const MachineInstr &OI) {
5029 switch (OI.getOpcode()) {
5030 default:
5031 return false;
5032 case X86::LZCNT16rr:
5033 case X86::LZCNT32rr:
5034 case X86::LZCNT64rr:
5035 case X86::TZCNT16rr:
5036 case X86::TZCNT32rr:
5037 case X86::TZCNT64rr: {
5038 if (ImmMask != 0 && !SrcReg2.isValid() && ImmValue == 1 &&
5039 OI.getOperand(1).isReg() && SrcReg == OI.getOperand(1).getReg()) {
5040 return true;
5041 }
5042 return false;
5043 }
5044 }
5045}
5046
5047#define CASE_EVEX(OP) \
5048 case X86::OP: \
5049 case X86::OP##_EVEX:
5050
5051/// Check whether the definition can be converted
5052/// to remove a comparison against zero.
5053inline static bool isDefConvertible(const MachineInstr &MI, bool &NoSignFlag,
5054 bool &ClearsOverflowFlag) {
5055 NoSignFlag = false;
5056 ClearsOverflowFlag = false;
5057
5058 // "ELF Handling for Thread-Local Storage" specifies that x86-64 GOTTPOFF, and
5059 // i386 GOTNTPOFF/INDNTPOFF relocations can convert an ADD to a LEA during
5060 // Initial Exec to Local Exec relaxation. In these cases, we must not depend
5061 // on the EFLAGS modification of ADD actually happening in the final binary.
5062 if (MI.getOpcode() == X86::ADD64rm || MI.getOpcode() == X86::ADD32rm) {
5063 unsigned Flags = MI.getOperand(5).getTargetFlags();
5064 if (Flags == X86II::MO_GOTTPOFF || Flags == X86II::MO_INDNTPOFF ||
5065 Flags == X86II::MO_GOTNTPOFF)
5066 return false;
5067 }
5068
5069 switch (MI.getOpcode()) {
5070 default:
5071 return false;
5072
5073 // The shift instructions only modify ZF if their shift count is non-zero.
5074 // N.B.: The processor truncates the shift count depending on the encoding.
5075 CASE_ND(SAR8ri)
5076 CASE_ND(SAR16ri)
5077 CASE_ND(SAR32ri)
5078 CASE_ND(SAR64ri)
5079 CASE_ND(SHR8ri)
5080 CASE_ND(SHR16ri)
5081 CASE_ND(SHR32ri)
5082 CASE_ND(SHR64ri)
5083 return getTruncatedShiftCount(MI, 2) != 0;
5084
5085 // Some left shift instructions can be turned into LEA instructions but only
5086 // if their flags aren't used. Avoid transforming such instructions.
5087 CASE_ND(SHL8ri)
5088 CASE_ND(SHL16ri)
5089 CASE_ND(SHL32ri)
5090 CASE_ND(SHL64ri) {
5091 unsigned ShAmt = getTruncatedShiftCount(MI, 2);
5092 // Converting to LEA only pays off when the shifted operand stays live,
5093 // since it spares a register copy; when the shift is the operand's only
5094 // user, reusing the flags is strictly better.
5095 if (isTruncatedShiftCountForLEA(ShAmt)) {
5096 Register SrcReg = MI.getOperand(1).getReg();
5097 const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
5098 if (!SrcReg.isVirtual() || !MRI.hasOneNonDBGUse(SrcReg))
5099 return false;
5100 }
5101 return ShAmt != 0;
5102 }
5103
5104 CASE_ND(SHRD16rri8)
5105 CASE_ND(SHRD32rri8)
5106 CASE_ND(SHRD64rri8)
5107 CASE_ND(SHLD16rri8)
5108 CASE_ND(SHLD32rri8)
5109 CASE_ND(SHLD64rri8)
5110 return getTruncatedShiftCount(MI, 3) != 0;
5111
5112 CASE_ND(SUB64ri32)
5113 CASE_ND(SUB32ri)
5114 CASE_ND(SUB16ri)
5115 CASE_ND(SUB8ri)
5116 CASE_ND(SUB64rr)
5117 CASE_ND(SUB32rr)
5118 CASE_ND(SUB16rr)
5119 CASE_ND(SUB8rr)
5120 CASE_ND(SUB64rm)
5121 CASE_ND(SUB32rm)
5122 CASE_ND(SUB16rm)
5123 CASE_ND(SUB8rm)
5124 CASE_ND(DEC64r)
5125 CASE_ND(DEC32r)
5126 CASE_ND(DEC16r)
5127 CASE_ND(DEC8r)
5128 CASE_ND(ADD64ri32)
5129 CASE_ND(ADD32ri)
5130 CASE_ND(ADD16ri)
5131 CASE_ND(ADD8ri)
5132 CASE_ND(ADD64rr)
5133 CASE_ND(ADD32rr)
5134 CASE_ND(ADD16rr)
5135 CASE_ND(ADD8rr)
5136 CASE_ND(ADD64rm)
5137 CASE_ND(ADD32rm)
5138 CASE_ND(ADD16rm)
5139 CASE_ND(ADD8rm)
5140 CASE_ND(INC64r)
5141 CASE_ND(INC32r)
5142 CASE_ND(INC16r)
5143 CASE_ND(INC8r)
5144 CASE_ND(ADC64ri32)
5145 CASE_ND(ADC32ri)
5146 CASE_ND(ADC16ri)
5147 CASE_ND(ADC8ri)
5148 CASE_ND(ADC64rr)
5149 CASE_ND(ADC32rr)
5150 CASE_ND(ADC16rr)
5151 CASE_ND(ADC8rr)
5152 CASE_ND(ADC64rm)
5153 CASE_ND(ADC32rm)
5154 CASE_ND(ADC16rm)
5155 CASE_ND(ADC8rm)
5156 CASE_ND(SBB64ri32)
5157 CASE_ND(SBB32ri)
5158 CASE_ND(SBB16ri)
5159 CASE_ND(SBB8ri)
5160 CASE_ND(SBB64rr)
5161 CASE_ND(SBB32rr)
5162 CASE_ND(SBB16rr)
5163 CASE_ND(SBB8rr)
5164 CASE_ND(SBB64rm)
5165 CASE_ND(SBB32rm)
5166 CASE_ND(SBB16rm)
5167 CASE_ND(SBB8rm)
5168 CASE_ND(NEG8r)
5169 CASE_ND(NEG16r)
5170 CASE_ND(NEG32r)
5171 CASE_ND(NEG64r)
5172 case X86::LZCNT16rr:
5173 case X86::LZCNT16rm:
5174 case X86::LZCNT32rr:
5175 case X86::LZCNT32rm:
5176 case X86::LZCNT64rr:
5177 case X86::LZCNT64rm:
5178 case X86::POPCNT16rr:
5179 case X86::POPCNT16rm:
5180 case X86::POPCNT32rr:
5181 case X86::POPCNT32rm:
5182 case X86::POPCNT64rr:
5183 case X86::POPCNT64rm:
5184 case X86::TZCNT16rr:
5185 case X86::TZCNT16rm:
5186 case X86::TZCNT32rr:
5187 case X86::TZCNT32rm:
5188 case X86::TZCNT64rr:
5189 case X86::TZCNT64rm:
5190 return true;
5191 CASE_ND(AND64ri32)
5192 CASE_ND(AND32ri)
5193 CASE_ND(AND16ri)
5194 CASE_ND(AND8ri)
5195 CASE_ND(AND64rr)
5196 CASE_ND(AND32rr)
5197 CASE_ND(AND16rr)
5198 CASE_ND(AND8rr)
5199 CASE_ND(AND64rm)
5200 CASE_ND(AND32rm)
5201 CASE_ND(AND16rm)
5202 CASE_ND(AND8rm)
5203 CASE_ND(XOR64ri32)
5204 CASE_ND(XOR32ri)
5205 CASE_ND(XOR16ri)
5206 CASE_ND(XOR8ri)
5207 CASE_ND(XOR64rr)
5208 CASE_ND(XOR32rr)
5209 CASE_ND(XOR16rr)
5210 CASE_ND(XOR8rr)
5211 CASE_ND(XOR64rm)
5212 CASE_ND(XOR32rm)
5213 CASE_ND(XOR16rm)
5214 CASE_ND(XOR8rm)
5215 CASE_ND(OR64ri32)
5216 CASE_ND(OR32ri)
5217 CASE_ND(OR16ri)
5218 CASE_ND(OR8ri)
5219 CASE_ND(OR64rr)
5220 CASE_ND(OR32rr)
5221 CASE_ND(OR16rr)
5222 CASE_ND(OR8rr)
5223 CASE_ND(OR64rm)
5224 CASE_ND(OR32rm)
5225 CASE_ND(OR16rm)
5226 CASE_ND(OR8rm)
5227 CASE_EVEX(ANDN32rr)
5228 CASE_EVEX(ANDN32rm)
5229 CASE_EVEX(ANDN64rr)
5230 CASE_EVEX(ANDN64rm)
5231 CASE_EVEX(BLSI32rr)
5232 CASE_EVEX(BLSI32rm)
5233 CASE_EVEX(BLSI64rr)
5234 CASE_EVEX(BLSI64rm)
5235 CASE_EVEX(BLSMSK32rr)
5236 CASE_EVEX(BLSMSK32rm)
5237 CASE_EVEX(BLSMSK64rr)
5238 CASE_EVEX(BLSMSK64rm)
5239 CASE_EVEX(BLSR32rr)
5240 CASE_EVEX(BLSR32rm)
5241 CASE_EVEX(BLSR64rr)
5242 CASE_EVEX(BLSR64rm)
5243 case X86::BLCFILL32rr:
5244 case X86::BLCFILL32rm:
5245 case X86::BLCFILL64rr:
5246 case X86::BLCFILL64rm:
5247 case X86::BLCI32rr:
5248 case X86::BLCI32rm:
5249 case X86::BLCI64rr:
5250 case X86::BLCI64rm:
5251 case X86::BLCIC32rr:
5252 case X86::BLCIC32rm:
5253 case X86::BLCIC64rr:
5254 case X86::BLCIC64rm:
5255 case X86::BLCMSK32rr:
5256 case X86::BLCMSK32rm:
5257 case X86::BLCMSK64rr:
5258 case X86::BLCMSK64rm:
5259 case X86::BLCS32rr:
5260 case X86::BLCS32rm:
5261 case X86::BLCS64rr:
5262 case X86::BLCS64rm:
5263 case X86::BLSFILL32rr:
5264 case X86::BLSFILL32rm:
5265 case X86::BLSFILL64rr:
5266 case X86::BLSFILL64rm:
5267 case X86::BLSIC32rr:
5268 case X86::BLSIC32rm:
5269 case X86::BLSIC64rr:
5270 case X86::BLSIC64rm:
5271 CASE_EVEX(BZHI32rr)
5272 CASE_EVEX(BZHI32rm)
5273 CASE_EVEX(BZHI64rr)
5274 CASE_EVEX(BZHI64rm)
5275 case X86::T1MSKC32rr:
5276 case X86::T1MSKC32rm:
5277 case X86::T1MSKC64rr:
5278 case X86::T1MSKC64rm:
5279 case X86::TZMSK32rr:
5280 case X86::TZMSK32rm:
5281 case X86::TZMSK64rr:
5282 case X86::TZMSK64rm:
5283 // These instructions clear the overflow flag just like TEST.
5284 // FIXME: These are not the only instructions in this switch that clear the
5285 // overflow flag.
5286 ClearsOverflowFlag = true;
5287 return true;
5288 CASE_EVEX(BEXTR32rr)
5289 CASE_EVEX(BEXTR64rr)
5290 CASE_EVEX(BEXTR32rm)
5291 CASE_EVEX(BEXTR64rm)
5292 case X86::BEXTRI32ri:
5293 case X86::BEXTRI32mi:
5294 case X86::BEXTRI64ri:
5295 case X86::BEXTRI64mi:
5296 // BEXTR doesn't update the sign flag so we can't use it. It does clear
5297 // the overflow flag, but that's not useful without the sign flag.
5298 NoSignFlag = true;
5299 return true;
5300 }
5301}
5302
5303/// Check whether the use can be converted to remove a comparison against zero.
5304/// Returns the EFLAGS condition and the operand that we are comparing against zero.
5305static std::pair<X86::CondCode, unsigned> isUseDefConvertible(const MachineInstr &MI) {
5306 switch (MI.getOpcode()) {
5307 default:
5308 return std::make_pair(X86::COND_INVALID, ~0U);
5309 CASE_ND(NEG8r)
5310 CASE_ND(NEG16r)
5311 CASE_ND(NEG32r)
5312 CASE_ND(NEG64r)
5313 return std::make_pair(X86::COND_AE, 1U);
5314 case X86::LZCNT16rr:
5315 case X86::LZCNT32rr:
5316 case X86::LZCNT64rr:
5317 return std::make_pair(X86::COND_B, 1U);
5318 case X86::POPCNT16rr:
5319 case X86::POPCNT32rr:
5320 case X86::POPCNT64rr:
5321 return std::make_pair(X86::COND_E, 1U);
5322 case X86::TZCNT16rr:
5323 case X86::TZCNT32rr:
5324 case X86::TZCNT64rr:
5325 return std::make_pair(X86::COND_B, 1U);
5326 case X86::BSF16rr:
5327 case X86::BSF32rr:
5328 case X86::BSF64rr:
5329 case X86::BSR16rr:
5330 case X86::BSR32rr:
5331 case X86::BSR64rr:
5332 return std::make_pair(X86::COND_E, 2U);
5333 CASE_EVEX(BLSI32rr)
5334 CASE_EVEX(BLSI64rr)
5335 return std::make_pair(X86::COND_AE, 1U);
5336 CASE_EVEX(BLSR32rr)
5337 CASE_EVEX(BLSR64rr)
5338 CASE_EVEX(BLSMSK32rr)
5339 CASE_EVEX(BLSMSK64rr)
5340 return std::make_pair(X86::COND_B, 1U);
5341 // TODO: TBM instructions.
5342 }
5343}
5344#undef CASE_EVEX
5345
5346MachineInstr *X86InstrInfo::findDominatingRedundantFlagInstr(
5347 MachineInstr &CmpInstr, Register SrcReg, Register SrcReg2, int64_t CmpMask,
5348 int64_t CmpValue, MachineBasicBlock *MultiPredMBB, bool &IsSwapped,
5349 int64_t &ImmDelta,
5350 SmallVectorImpl<std::pair<MachineInstr *, unsigned>> &InstsToUpdate) const {
5351 assert(Subtarget.hasNF() && "NF feature required");
5352 const TargetRegisterInfo *TRI = &getRegisterInfo();
5353
5354 // The caller already scanned MultiPredMBB without finding the producer, so it
5355 // must live in a block that strictly dominates MultiPredMBB. Walk
5356 // predecessors backward to find it and prove dominance, avoiding a
5357 // whole-function MachineDominatorTree that would be rebuilt in O(function
5358 // size) per compare.
5359 //
5360 // The producer's block dominates MultiPredMBB iff every backward path funnels
5361 // through it before a function-entry block, so expand predecessors but stop
5362 // at a block holding the producer. Bail if a predecessor-less block is
5363 // reached without the producer (a path bypasses it) or the producer is found
5364 // in two blocks (neither dominates alone). Within a block, scan backward,
5365 // collecting the NF-convertible EFLAGS clobbers above the producer and
5366 // bailing on any other clobber (it would shadow the producer's flags from
5367 // CmpInstr).
5368 //
5369 // Clobbers are staged in Pending and committed only on success. Visited
5370 // is seeded with the caller's single-predecessor chain (CmpMBB through
5371 // MultiPredMBB) so the walk doesn't re-scan blocks the caller already
5372 // staged. The walk doubles as a cycle detector: a predecessor equal to
5373 // CmpMBB is a back-edge from CmpMBB's successors into the walked region,
5374 // which means CmpMBB is on a CFG cycle. In that case the region below
5375 // CmpInstr executes on the back-edge before the next iteration's CmpInstr
5376 // and must be checked too: bail on any non-NF-convertible EFLAGS clobber,
5377 // stage NF-convertible ones.
5378 //
5379 // Each NF conversion trades a compact legacy/EVEX-compressed encoding for a
5380 // wider EVEX (often NDD three-operand) one, growing code size, while the
5381 // reuse only removes a single compare. Cap the total number of conversions
5382 // (caller chain + predecessor walk + below-scan) so the reuse cannot bloat
5383 // code just to delete one compare.
5384 MachineInstr *Sub = nullptr;
5385 MachineBasicBlock *SubMBB = nullptr;
5387
5388 MachineBasicBlock *CmpMBB = CmpInstr.getParent();
5389 SmallPtrSet<MachineBasicBlock *, 8> Visited;
5391 for (MachineBasicBlock *MBB = CmpMBB; MBB != MultiPredMBB;
5393 Visited.insert(MBB);
5394 Visited.insert(MultiPredMBB);
5395
5396 bool CmpMBBOnCycle = false;
5397 auto TryPush = [&](MachineBasicBlock *Pred) {
5398 if (Pred == CmpMBB)
5399 CmpMBBOnCycle = true;
5400 if (Visited.insert(Pred).second)
5401 Worklist.push_back(Pred);
5402 };
5403
5404 for (MachineBasicBlock *Pred : MultiPredMBB->predecessors())
5405 TryPush(Pred);
5406 while (!Worklist.empty()) {
5407 MachineBasicBlock *MBB = Worklist.pop_back_val();
5408 MachineInstr *Producer = nullptr;
5409 for (MachineInstr &Inst : reverse(*MBB)) {
5410 if (!Inst.modifiesRegister(X86::EFLAGS, TRI))
5411 continue;
5412 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpMask, CmpValue,
5413 Inst, &IsSwapped, &ImmDelta)) {
5414 Producer = &Inst;
5415 break;
5416 }
5417 unsigned NewOpc = X86::getNFVariantIfClobberRemovable(Inst, TRI);
5418 if (!NewOpc)
5419 return nullptr;
5420 if (InstsToUpdate.size() + Pending.size() >= MaxNFConversions)
5421 return nullptr;
5422 Pending.push_back(std::make_pair(&Inst, NewOpc));
5423 }
5424 if (Producer) {
5425 // A producer in a second block means neither dominates alone.
5426 if (Sub && SubMBB != MBB)
5427 return nullptr;
5428 Sub = Producer;
5429 SubMBB = MBB;
5430 continue;
5431 }
5432 // Entry reached without the producer: some path bypasses it.
5433 if (MBB->pred_empty())
5434 return nullptr;
5435 for (MachineBasicBlock *Pred : MBB->predecessors())
5436 TryPush(Pred);
5437 }
5438 if (!Sub)
5439 return nullptr;
5440
5441 // The forward condition-code fixup in the caller (OpsToUpdate) only rewrites
5442 // EFLAGS users within CmpMBB. When the producer's flags require a condition
5443 // swap or an immediate adjustment, EFLAGS users elsewhere in the dominated
5444 // region or in CmpMBB's successors (when EFLAGS is live-out) would also need
5445 // rewriting, which is not handled here. Restrict the multi-predecessor case
5446 // to producers that yield identical flags.
5447 if (IsSwapped || ImmDelta != 0)
5448 return nullptr;
5449
5450 // If CmpMBB is on a CFG cycle, its below-CmpInstr region is on the back-edge
5451 // path and must also be free of non-NF-convertible EFLAGS clobbers.
5452 if (CmpMBBOnCycle) {
5453 for (MachineInstr &Inst : make_range(
5454 std::next(MachineBasicBlock::iterator(CmpInstr)), CmpMBB->end())) {
5455 if (!Inst.modifiesRegister(X86::EFLAGS, TRI))
5456 continue;
5457 unsigned NewOpc = X86::getNFVariantIfClobberRemovable(Inst, TRI);
5458 if (!NewOpc)
5459 return nullptr;
5460 if (InstsToUpdate.size() + Pending.size() >= MaxNFConversions)
5461 return nullptr;
5462 Pending.push_back(std::make_pair(&Inst, NewOpc));
5463 }
5464 }
5465
5466 InstsToUpdate.append(Pending.begin(), Pending.end());
5467 return Sub;
5468}
5469
5470/// Check if there exists an earlier instruction that
5471/// operates on the same source operands and sets flags in the same way as
5472/// Compare; remove Compare if possible.
5474 Register SrcReg2, int64_t CmpMask,
5475 int64_t CmpValue,
5476 const MachineRegisterInfo *MRI) const {
5477 // Check whether we can replace SUB with CMP.
5478 switch (CmpInstr.getOpcode()) {
5479 default:
5480 break;
5481 CASE_ND(SUB64ri32)
5482 CASE_ND(SUB32ri)
5483 CASE_ND(SUB16ri)
5484 CASE_ND(SUB8ri)
5485 CASE_ND(SUB64rm)
5486 CASE_ND(SUB32rm)
5487 CASE_ND(SUB16rm)
5488 CASE_ND(SUB8rm)
5489 CASE_ND(SUB64rr)
5490 CASE_ND(SUB32rr)
5491 CASE_ND(SUB16rr)
5492 CASE_ND(SUB8rr) {
5493 if (!MRI->use_nodbg_empty(CmpInstr.getOperand(0).getReg()))
5494 return false;
5495 // There is no use of the destination register, we can replace SUB with CMP.
5496 unsigned NewOpcode = 0;
5497#define FROM_TO(A, B) \
5498 CASE_ND(A) NewOpcode = X86::B; \
5499 break;
5500 switch (CmpInstr.getOpcode()) {
5501 default:
5502 llvm_unreachable("Unreachable!");
5503 FROM_TO(SUB64rm, CMP64rm)
5504 FROM_TO(SUB32rm, CMP32rm)
5505 FROM_TO(SUB16rm, CMP16rm)
5506 FROM_TO(SUB8rm, CMP8rm)
5507 FROM_TO(SUB64rr, CMP64rr)
5508 FROM_TO(SUB32rr, CMP32rr)
5509 FROM_TO(SUB16rr, CMP16rr)
5510 FROM_TO(SUB8rr, CMP8rr)
5511 FROM_TO(SUB64ri32, CMP64ri32)
5512 FROM_TO(SUB32ri, CMP32ri)
5513 FROM_TO(SUB16ri, CMP16ri)
5514 FROM_TO(SUB8ri, CMP8ri)
5515 }
5516#undef FROM_TO
5517 CmpInstr.setDesc(get(NewOpcode));
5518 CmpInstr.removeOperand(0);
5519 // Mutating this instruction invalidates any debug data associated with it.
5520 CmpInstr.dropDebugNumber();
5521 // Fall through to optimize Cmp if Cmp is CMPrr or CMPri.
5522 if (NewOpcode == X86::CMP64rm || NewOpcode == X86::CMP32rm ||
5523 NewOpcode == X86::CMP16rm || NewOpcode == X86::CMP8rm)
5524 return false;
5525 }
5526 }
5527
5528 // The following code tries to remove the comparison by re-using EFLAGS
5529 // from earlier instructions.
5530
5531 bool IsCmpZero = (CmpMask != 0 && CmpValue == 0);
5532
5533 // Transformation currently requires SSA values.
5534 if (SrcReg2.isPhysical())
5535 return false;
5536 MachineInstr *SrcRegDef = MRI->getVRegDef(SrcReg);
5537 if (!SrcRegDef)
5538 return false;
5539
5540 MachineInstr *MI = nullptr;
5541 MachineInstr *Sub = nullptr;
5542 MachineInstr *Movr0Inst = nullptr;
5543 MachineInstr *LTZCNTInst = nullptr;
5545 bool NoSignFlag = false;
5546 bool ClearsOverflowFlag = false;
5547 bool ShouldUpdateCC = false;
5548 bool IsSwapped = false;
5549 bool HasNF = Subtarget.hasNF();
5550 unsigned OpNo = 0;
5552 int64_t ImmDelta = 0;
5553
5554 // Search backward from CmpInstr for the next instruction defining EFLAGS.
5556 MachineBasicBlock &CmpMBB = *CmpInstr.getParent();
5558 std::next(MachineBasicBlock::reverse_iterator(CmpInstr));
5559 for (MachineBasicBlock *MBB = &CmpMBB;;) {
5560 for (MachineInstr &Inst : make_range(From, MBB->rend())) {
5561 // Try to use EFLAGS from the instruction defining %SrcReg. Example:
5562 // %eax = addl ...
5563 // ... // EFLAGS not changed
5564 // testl %eax, %eax // <-- can be removed
5565 if (&Inst == SrcRegDef) {
5566 if (IsCmpZero &&
5567 isDefConvertible(Inst, NoSignFlag, ClearsOverflowFlag)) {
5568 MI = &Inst;
5569 break;
5570 }
5571
5572 // Look back for the following pattern, in which case the
5573 // test16rr/test64rr instruction could be erased.
5574 //
5575 // Example for test16rr:
5576 // %reg = and32ri %in_reg, 5
5577 // ... // EFLAGS not changed.
5578 // %src_reg = copy %reg.sub_16bit:gr32
5579 // test16rr %src_reg, %src_reg, implicit-def $eflags
5580 // Example for test64rr:
5581 // %reg = and32ri %in_reg, 5
5582 // ... // EFLAGS not changed.
5583 // %src_reg = subreg_to_reg %reg, %subreg.sub_index
5584 // test64rr %src_reg, %src_reg, implicit-def $eflags
5585 MachineInstr *AndInstr = nullptr;
5586 if (IsCmpZero &&
5587 findRedundantFlagInstr(CmpInstr, Inst, MRI, &AndInstr, TRI,
5588 Subtarget, NoSignFlag, ClearsOverflowFlag)) {
5589 assert(AndInstr != nullptr && X86::isAND(AndInstr->getOpcode()));
5590 MI = AndInstr;
5591 break;
5592 }
5593 // Cannot find other candidates before definition of SrcReg.
5594 return false;
5595 }
5596
5597 if (Inst.modifiesRegister(X86::EFLAGS, TRI)) {
5598 // Try to use EFLAGS produced by an instruction reading %SrcReg.
5599 // Example:
5600 // %eax = ...
5601 // ...
5602 // popcntl %eax
5603 // ... // EFLAGS not changed
5604 // testl %eax, %eax // <-- can be removed
5605 if (IsCmpZero) {
5606 std::tie(NewCC, OpNo) = isUseDefConvertible(Inst);
5607 if (NewCC != X86::COND_INVALID && Inst.getOperand(OpNo).isReg() &&
5608 Inst.getOperand(OpNo).getReg() == SrcReg) {
5609 ShouldUpdateCC = true;
5610 MI = &Inst;
5611 break;
5612 }
5613 }
5614
5615 // Try to use EFLAGS from an instruction with similar flag results.
5616 // Example:
5617 // sub x, y or cmp x, y
5618 // ... // EFLAGS not changed
5619 // cmp x, y // <-- can be removed
5620 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpMask, CmpValue,
5621 Inst, &IsSwapped, &ImmDelta)) {
5622 Sub = &Inst;
5623 break;
5624 }
5625
5626 // Try to use CF produced by an LZCNT/TZCNT reading %SrcReg: it and
5627 // "cmp $1, %SrcReg" both set CF iff %SrcReg is zero. The other flags
5628 // differ, so all EFLAGS users need to read CF only (ADC/SBB/RCL/RCR).
5629 // Example:
5630 // lzcntq %rdi, %rax
5631 // ... // EFLAGS not changed
5632 // cmpq $1, %rdi // <-- can be removed
5633 // adcq $0, %rax // reads CF only
5634 if (isCmpRedundantAfterLTZCNT(SrcReg, SrcReg2, CmpMask, CmpValue,
5635 Inst)) {
5636 LTZCNTInst = &Inst;
5637 break;
5638 }
5639
5640 // MOV32r0 is implemented with xor which clobbers condition code. It is
5641 // safe to move up, if the definition to EFLAGS is dead and earlier
5642 // instructions do not read or write EFLAGS.
5643 if (!Movr0Inst && Inst.getOpcode() == X86::MOV32r0 &&
5644 Inst.registerDefIsDead(X86::EFLAGS, TRI)) {
5645 Movr0Inst = &Inst;
5646 continue;
5647 }
5648
5649 // Try to replace non-NF with NF instructions.
5650 if (HasNF) {
5651 unsigned NewOp = X86::getNFVariantIfClobberRemovable(Inst, TRI);
5652 if (!NewOp)
5653 return false;
5654
5655 InstsToUpdate.push_back(std::make_pair(&Inst, NewOp));
5656 continue;
5657 }
5658
5659 // Cannot do anything for any other EFLAG changes.
5660 return false;
5661 }
5662 }
5663
5664 if (MI || Sub || LTZCNTInst)
5665 break;
5666
5667 // Reached the begin of the basic block. If it has exactly one predecessor,
5668 // continue the backward scan there. Otherwise (multiple predecessors), try
5669 // to reuse EFLAGS from a dominating producer (handled below).
5670 if (MBB->pred_size() != 1) {
5671 // The block has multiple predecessors. We can still reuse EFLAGS from an
5672 // equivalent flag producer that dominates CmpInstr, provided every path
5673 // from that producer to CmpInstr only clobbers EFLAGS via instructions
5674 // that have an NF (no-flags) variant (which requires APX). This handles
5675 // patterns like (CMP duplicated by CodeGenPrepare across a diamond):
5676 // entry: cmp %x, C ; br
5677 // bb1: imul ... ; clobbers EFLAGS -> {nf} imul
5678 // bb2: ...
5679 // bb3: cmp %x, C ; <-- redundant, reuse EFLAGS from entry
5680 // cmovcc ...
5681 // The helper caps the total number of NF conversions so this cannot grow
5682 // code size without bound just to delete one compare.
5683 if (HasNF)
5684 Sub = findDominatingRedundantFlagInstr(
5685 CmpInstr, SrcReg, SrcReg2, CmpMask, CmpValue, MBB, IsSwapped,
5686 ImmDelta, InstsToUpdate);
5687 if (!Sub)
5688 return false;
5689 break;
5690 }
5691 MBB = *MBB->pred_begin();
5692 From = MBB->rbegin();
5693 }
5694
5695 // Scan forward from the instruction after CmpInstr for uses of EFLAGS.
5696 // It is safe to remove CmpInstr if EFLAGS is redefined or killed.
5697 // If we are done with the basic block, we need to check whether EFLAGS is
5698 // live-out.
5699 bool FlagsMayLiveOut = true;
5701 MachineBasicBlock::iterator AfterCmpInstr =
5702 std::next(MachineBasicBlock::iterator(CmpInstr));
5703 for (MachineInstr &Instr : make_range(AfterCmpInstr, CmpMBB.end())) {
5704 bool ModifyEFLAGS = Instr.modifiesRegister(X86::EFLAGS, TRI);
5705 bool UseEFLAGS = Instr.readsRegister(X86::EFLAGS, TRI);
5706 // We should check the usage if this instruction uses and updates EFLAGS.
5707 if (!UseEFLAGS && ModifyEFLAGS) {
5708 // It is safe to remove CmpInstr if EFLAGS is updated again.
5709 FlagsMayLiveOut = false;
5710 break;
5711 }
5712 if (!UseEFLAGS && !ModifyEFLAGS)
5713 continue;
5714
5715 // EFLAGS is used by this instruction.
5716 X86::CondCode OldCC = X86::getCondFromMI(Instr);
5717 if ((MI || IsSwapped || ImmDelta != 0) && OldCC == X86::COND_INVALID)
5718 return false;
5719
5720 X86::CondCode ReplacementCC = X86::COND_INVALID;
5721 if (MI) {
5722 switch (OldCC) {
5723 default:
5724 break;
5725 case X86::COND_A:
5726 case X86::COND_AE:
5727 case X86::COND_B:
5728 case X86::COND_BE:
5729 // CF is used, we can't perform this optimization.
5730 return false;
5731 case X86::COND_G:
5732 case X86::COND_GE:
5733 case X86::COND_L:
5734 case X86::COND_LE:
5735 // If SF is used, but the instruction doesn't update the SF, then we
5736 // can't do the optimization.
5737 if (NoSignFlag)
5738 return false;
5739 [[fallthrough]];
5740 case X86::COND_O:
5741 case X86::COND_NO:
5742 // If OF is used, the instruction needs to clear it like CmpZero does.
5743 if (!ClearsOverflowFlag)
5744 return false;
5745 break;
5746 case X86::COND_S:
5747 case X86::COND_NS:
5748 // If SF is used, but the instruction doesn't update the SF, then we
5749 // can't do the optimization.
5750 if (NoSignFlag)
5751 return false;
5752 break;
5753 }
5754
5755 // If we're updating the condition code check if we have to reverse the
5756 // condition.
5757 if (ShouldUpdateCC)
5758 switch (OldCC) {
5759 default:
5760 return false;
5761 case X86::COND_E:
5762 ReplacementCC = NewCC;
5763 break;
5764 case X86::COND_NE:
5765 ReplacementCC = GetOppositeBranchCondition(NewCC);
5766 break;
5767 }
5768 } else if (IsSwapped) {
5769 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code needs
5770 // to be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
5771 // We swap the condition code and synthesize the new opcode.
5772 ReplacementCC = getSwappedCondition(OldCC);
5773 if (ReplacementCC == X86::COND_INVALID)
5774 return false;
5775 ShouldUpdateCC = true;
5776 } else if (ImmDelta != 0) {
5777 unsigned BitWidth = RI.getRegSizeInBits(*MRI->getRegClass(SrcReg));
5778 // Shift amount for min/max constants to adjust for 8/16/32 instruction
5779 // sizes.
5780 switch (OldCC) {
5781 case X86::COND_L: // x <s (C + 1) --> x <=s C
5782 if (ImmDelta != 1 || APInt::getSignedMinValue(BitWidth) == CmpValue)
5783 return false;
5784 ReplacementCC = X86::COND_LE;
5785 break;
5786 case X86::COND_B: // x <u (C + 1) --> x <=u C
5787 if (ImmDelta != 1 || CmpValue == 0)
5788 return false;
5789 ReplacementCC = X86::COND_BE;
5790 break;
5791 case X86::COND_GE: // x >=s (C + 1) --> x >s C
5792 if (ImmDelta != 1 || APInt::getSignedMinValue(BitWidth) == CmpValue)
5793 return false;
5794 ReplacementCC = X86::COND_G;
5795 break;
5796 case X86::COND_AE: // x >=u (C + 1) --> x >u C
5797 if (ImmDelta != 1 || CmpValue == 0)
5798 return false;
5799 ReplacementCC = X86::COND_A;
5800 break;
5801 case X86::COND_G: // x >s (C - 1) --> x >=s C
5802 if (ImmDelta != -1 || APInt::getSignedMaxValue(BitWidth) == CmpValue)
5803 return false;
5804 ReplacementCC = X86::COND_GE;
5805 break;
5806 case X86::COND_A: // x >u (C - 1) --> x >=u C
5807 if (ImmDelta != -1 || APInt::getMaxValue(BitWidth) == CmpValue)
5808 return false;
5809 ReplacementCC = X86::COND_AE;
5810 break;
5811 case X86::COND_LE: // x <=s (C - 1) --> x <s C
5812 if (ImmDelta != -1 || APInt::getSignedMaxValue(BitWidth) == CmpValue)
5813 return false;
5814 ReplacementCC = X86::COND_L;
5815 break;
5816 case X86::COND_BE: // x <=u (C - 1) --> x <u C
5817 if (ImmDelta != -1 || APInt::getMaxValue(BitWidth) == CmpValue)
5818 return false;
5819 ReplacementCC = X86::COND_B;
5820 break;
5821 default:
5822 return false;
5823 }
5824 ShouldUpdateCC = true;
5825 }
5826
5827 if (LTZCNTInst) {
5828 unsigned InstCode = Instr.getOpcode();
5829 if (!X86::isADC(InstCode) && !X86::isSBB(InstCode) &&
5830 !X86::isRCL(InstCode) && !X86::isRCR(InstCode))
5831 return false;
5832
5833 MI = LTZCNTInst;
5834 }
5835
5836 if (ShouldUpdateCC && ReplacementCC != OldCC) {
5837 // Push the MachineInstr to OpsToUpdate.
5838 // If it is safe to remove CmpInstr, the condition code of these
5839 // instructions will be modified.
5840 OpsToUpdate.push_back(std::make_pair(&Instr, ReplacementCC));
5841 }
5842 if (ModifyEFLAGS || Instr.killsRegister(X86::EFLAGS, TRI)) {
5843 // It is safe to remove CmpInstr if EFLAGS is updated again or killed.
5844 FlagsMayLiveOut = false;
5845 break;
5846 }
5847 }
5848
5849 if (LTZCNTInst && !MI)
5850 return false;
5851
5852 // If we have to update users but EFLAGS is live-out abort, since we cannot
5853 // easily find all of the users.
5854 if ((MI != nullptr || ShouldUpdateCC) && FlagsMayLiveOut) {
5855 for (MachineBasicBlock *Successor : CmpMBB.successors())
5856 if (Successor->isLiveIn(X86::EFLAGS))
5857 return false;
5858 }
5859
5860 // The instruction to be updated is either Sub or MI.
5861 assert((MI == nullptr || Sub == nullptr) && "Should not have Sub and MI set");
5862 Sub = MI != nullptr ? MI : Sub;
5863 MachineBasicBlock *SubBB = Sub->getParent();
5864 // Move Movr0Inst to the appropriate place before Sub.
5865 if (Movr0Inst) {
5866 // Only move within the same block so we don't accidentally move to a
5867 // block with higher execution frequency.
5868 if (&CmpMBB != SubBB)
5869 return false;
5870 // Look backwards until we find a def that doesn't use the current EFLAGS.
5872 InsertE = Sub->getParent()->rend();
5873 for (; InsertI != InsertE; ++InsertI) {
5874 MachineInstr *Instr = &*InsertI;
5875 if (!Instr->readsRegister(X86::EFLAGS, TRI) &&
5876 Instr->modifiesRegister(X86::EFLAGS, TRI)) {
5877 Movr0Inst->getParent()->remove(Movr0Inst);
5878 Instr->getParent()->insert(MachineBasicBlock::iterator(Instr),
5879 Movr0Inst);
5880 break;
5881 }
5882 }
5883 if (InsertI == InsertE)
5884 return false;
5885 }
5886
5887 // Replace non-NF with NF instructions.
5888 for (auto &Inst : InstsToUpdate) {
5889 Inst.first->setDesc(get(Inst.second));
5890 Inst.first->removeOperand(
5891 Inst.first->findRegisterDefOperandIdx(X86::EFLAGS, /*TRI=*/nullptr));
5892 }
5893
5894 // Make sure Sub instruction defines EFLAGS and mark the def live.
5895 MachineOperand *FlagDef =
5896 Sub->findRegisterDefOperand(X86::EFLAGS, /*TRI=*/nullptr);
5897 assert(FlagDef && "Unable to locate a def EFLAGS operand");
5898 FlagDef->setIsDead(false);
5899
5900 CmpInstr.eraseFromParent();
5901
5902 // Modify the condition code of instructions in OpsToUpdate.
5903 for (auto &Op : OpsToUpdate) {
5904 Op.first->getOperand(Op.first->getDesc().getNumOperands() - 1)
5905 .setImm(Op.second);
5906 }
5907 // Add EFLAGS to block live-ins between CmpBB and block of flags producer.
5908 // Walk the CFG backward from CmpMBB up to (but excluding) SubBB, marking
5909 // EFLAGS live-in on every block in between. SubBB dominates CmpMBB (whether
5910 // the producer was found by the single-predecessor backward walk or the
5911 // multi-predecessor dominator search), so the walk reaches SubBB on every
5912 // path and never escapes above it. A single-predecessor chain is just the
5913 // degenerate case where every block has exactly one predecessor.
5915 SmallVector<MachineBasicBlock *, 8> Worklist(1, &CmpMBB);
5916 Visited.insert(&CmpMBB);
5917 while (!Worklist.empty()) {
5918 MachineBasicBlock *MBB = Worklist.pop_back_val();
5919 // EFLAGS is produced inside SubBB, so it is not live-in there.
5920 if (MBB == SubBB)
5921 continue;
5922 if (!MBB->isLiveIn(X86::EFLAGS))
5923 MBB->addLiveIn(X86::EFLAGS);
5924 for (MachineBasicBlock *Pred : MBB->predecessors())
5925 if (Visited.insert(Pred).second)
5926 Worklist.push_back(Pred);
5927 }
5928 return true;
5929}
5930
5931/// \returns true if the instruction can be changed to COPY when imm is 0.
5932static bool canConvert2Copy(unsigned Opc) {
5933 switch (Opc) {
5934 default:
5935 return false;
5936 CASE_ND(ADD64ri32)
5937 CASE_ND(SUB64ri32)
5938 CASE_ND(OR64ri32)
5939 CASE_ND(XOR64ri32)
5940 CASE_ND(ADD32ri)
5941 CASE_ND(SUB32ri)
5942 CASE_ND(OR32ri)
5943 CASE_ND(XOR32ri)
5944 return true;
5945 }
5946}
5947
5948/// Convert an ALUrr opcode to corresponding ALUri opcode. Such as
5949/// ADD32rr ==> ADD32ri
5950static unsigned convertALUrr2ALUri(unsigned Opc) {
5951 switch (Opc) {
5952 default:
5953 return 0;
5954#define FROM_TO(FROM, TO) \
5955 case X86::FROM: \
5956 return X86::TO; \
5957 case X86::FROM##_ND: \
5958 return X86::TO##_ND;
5959 FROM_TO(ADC64rr, ADC64ri32)
5960 FROM_TO(SBB64rr, SBB64ri32)
5961 FROM_TO(AND64rr, AND64ri32)
5962 FROM_TO(OR64rr, OR64ri32)
5963 FROM_TO(XOR64rr, XOR64ri32)
5964 FROM_TO(SHR64rCL, SHR64ri)
5965 FROM_TO(SHL64rCL, SHL64ri)
5966 FROM_TO(SAR64rCL, SAR64ri)
5967 FROM_TO(ROL64rCL, ROL64ri)
5968 FROM_TO(ROR64rCL, ROR64ri)
5969 FROM_TO(RCL64rCL, RCL64ri)
5970 FROM_TO(RCR64rCL, RCR64ri)
5971 FROM_TO(ADD32rr, ADD32ri)
5972 FROM_TO(ADC32rr, ADC32ri)
5973 FROM_TO(SUB32rr, SUB32ri)
5974 FROM_TO(SBB32rr, SBB32ri)
5975 FROM_TO(AND32rr, AND32ri)
5976 FROM_TO(OR32rr, OR32ri)
5977 FROM_TO(XOR32rr, XOR32ri)
5978 FROM_TO(SHR32rCL, SHR32ri)
5979 FROM_TO(SHL32rCL, SHL32ri)
5980 FROM_TO(SAR32rCL, SAR32ri)
5981 FROM_TO(ROL32rCL, ROL32ri)
5982 FROM_TO(ROR32rCL, ROR32ri)
5983 FROM_TO(RCL32rCL, RCL32ri)
5984 FROM_TO(RCR32rCL, RCR32ri)
5985#undef FROM_TO
5986#define FROM_TO(FROM, TO) \
5987 case X86::FROM: \
5988 return X86::TO;
5989 FROM_TO(ADD64rr, ADD64ri32)
5990 FROM_TO(SUB64rr, SUB64ri32)
5991 FROM_TO(TEST64rr, TEST64ri32)
5992 FROM_TO(CTEST64rr, CTEST64ri32)
5993 FROM_TO(CMP64rr, CMP64ri32)
5994 FROM_TO(CCMP64rr, CCMP64ri32)
5995 FROM_TO(TEST32rr, TEST32ri)
5996 FROM_TO(CTEST32rr, CTEST32ri)
5997 FROM_TO(CMP32rr, CMP32ri)
5998 FROM_TO(CCMP32rr, CCMP32ri)
5999#undef FROM_TO
6000 case X86::ADD64rr_ND:
6001 return X86::ADD64ri32_ND;
6002 case X86::SUB64rr_ND:
6003 return X86::SUB64ri32_ND;
6004 }
6005}
6006
6007/// Reg is assigned ImmVal in DefMI, and is used in UseMI.
6008/// If MakeChange is true, this function tries to replace Reg by ImmVal in
6009/// UseMI. If MakeChange is false, just check if folding is possible.
6010//
6011/// \returns true if folding is successful or possible.
6012bool X86InstrInfo::foldImmediateImpl(MachineInstr &UseMI, MachineInstr *DefMI,
6013 Register Reg, int64_t ImmVal,
6015 bool MakeChange) const {
6016 bool Modified = false;
6017
6018 // 64 bit operations accept sign extended 32 bit immediates.
6019 // 32 bit operations accept all 32 bit immediates, so we don't need to check
6020 // them.
6021 const TargetRegisterClass *RC = nullptr;
6022 if (Reg.isVirtual())
6023 RC = MRI->getRegClass(Reg);
6024 if ((Reg.isPhysical() && X86::GR64RegClass.contains(Reg)) ||
6025 (Reg.isVirtual() && X86::GR64RegClass.hasSubClassEq(RC))) {
6026 if (!isInt<32>(ImmVal))
6027 return false;
6028 }
6029
6030 if (UseMI.findRegisterUseOperand(Reg, /*TRI=*/nullptr)->getSubReg())
6031 return false;
6032 // Immediate has larger code size than register. So avoid folding the
6033 // immediate if it has more than 1 use and we are optimizing for size.
6034 if (UseMI.getMF()->getFunction().hasOptSize() && Reg.isVirtual() &&
6035 !MRI->hasOneNonDBGUse(Reg))
6036 return false;
6037
6038 unsigned Opc = UseMI.getOpcode();
6039 unsigned NewOpc;
6040 if (Opc == TargetOpcode::COPY) {
6041 Register ToReg = UseMI.getOperand(0).getReg();
6042 const TargetRegisterClass *RC = nullptr;
6043 if (ToReg.isVirtual())
6044 RC = MRI->getRegClass(ToReg);
6045 bool GR32Reg = (ToReg.isVirtual() && X86::GR32RegClass.hasSubClassEq(RC)) ||
6046 (ToReg.isPhysical() && X86::GR32RegClass.contains(ToReg));
6047 bool GR64Reg = (ToReg.isVirtual() && X86::GR64RegClass.hasSubClassEq(RC)) ||
6048 (ToReg.isPhysical() && X86::GR64RegClass.contains(ToReg));
6049 bool GR8Reg = (ToReg.isVirtual() && X86::GR8RegClass.hasSubClassEq(RC)) ||
6050 (ToReg.isPhysical() && X86::GR8RegClass.contains(ToReg));
6051
6052 if (ImmVal == 0) {
6053 // We have MOV32r0 only.
6054 if (!GR32Reg)
6055 return false;
6056 }
6057
6058 if (GR64Reg) {
6059 if (isUInt<32>(ImmVal))
6060 NewOpc = X86::MOV32ri64;
6061 else
6062 NewOpc = X86::MOV64ri;
6063 } else if (GR32Reg) {
6064 NewOpc = X86::MOV32ri;
6065 if (ImmVal == 0) {
6066 // MOV32r0 clobbers EFLAGS.
6067 const TargetRegisterInfo *TRI = &getRegisterInfo();
6068 if (UseMI.getParent()->computeRegisterLiveness(
6069 TRI, X86::EFLAGS, UseMI) != MachineBasicBlock::LQR_Dead)
6070 return false;
6071
6072 // MOV32r0 is different than other cases because it doesn't encode the
6073 // immediate in the instruction. So we directly modify it here.
6074 if (!MakeChange)
6075 return true;
6076 UseMI.setDesc(get(X86::MOV32r0));
6077 UseMI.removeOperand(
6078 UseMI.findRegisterUseOperandIdx(Reg, /*TRI=*/nullptr));
6079 UseMI.addOperand(MachineOperand::CreateReg(X86::EFLAGS, /*isDef=*/true,
6080 /*isImp=*/true,
6081 /*isKill=*/false,
6082 /*isDead=*/true));
6083 Modified = true;
6084 }
6085 } else if (GR8Reg)
6086 NewOpc = X86::MOV8ri;
6087 else
6088 return false;
6089 } else
6090 NewOpc = convertALUrr2ALUri(Opc);
6091
6092 if (!NewOpc)
6093 return false;
6094
6095 // For SUB instructions the immediate can only be the second source operand.
6096 if ((NewOpc == X86::SUB64ri32 || NewOpc == X86::SUB32ri ||
6097 NewOpc == X86::SBB64ri32 || NewOpc == X86::SBB32ri ||
6098 NewOpc == X86::SUB64ri32_ND || NewOpc == X86::SUB32ri_ND ||
6099 NewOpc == X86::SBB64ri32_ND || NewOpc == X86::SBB32ri_ND) &&
6100 UseMI.findRegisterUseOperandIdx(Reg, /*TRI=*/nullptr) != 2)
6101 return false;
6102 // For CMP instructions the immediate can only be at index 1.
6103 if (((NewOpc == X86::CMP64ri32 || NewOpc == X86::CMP32ri) ||
6104 (NewOpc == X86::CCMP64ri32 || NewOpc == X86::CCMP32ri)) &&
6105 UseMI.findRegisterUseOperandIdx(Reg, /*TRI=*/nullptr) != 1)
6106 return false;
6107
6108 using namespace X86;
6109 if (isSHL(Opc) || isSHR(Opc) || isSAR(Opc) || isROL(Opc) || isROR(Opc) ||
6110 isRCL(Opc) || isRCR(Opc)) {
6111 unsigned RegIdx = UseMI.findRegisterUseOperandIdx(Reg, /*TRI=*/nullptr);
6112 if (RegIdx < 2)
6113 return false;
6114 if (!isInt<8>(ImmVal))
6115 return false;
6116 assert(Reg == X86::CL);
6117
6118 if (!MakeChange)
6119 return true;
6120 UseMI.setDesc(get(NewOpc));
6121 UseMI.removeOperand(RegIdx);
6122 UseMI.addOperand(MachineOperand::CreateImm(ImmVal));
6123 // Reg is physical register $cl, so we don't know if DefMI is dead through
6124 // MRI. Let the caller handle it, or pass dead-mi-elimination can delete
6125 // the dead physical register define instruction.
6126 return true;
6127 }
6128
6129 if (!MakeChange)
6130 return true;
6131
6132 if (!Modified) {
6133 // Modify the instruction.
6134 if (ImmVal == 0 && canConvert2Copy(NewOpc) &&
6135 UseMI.registerDefIsDead(X86::EFLAGS, /*TRI=*/nullptr)) {
6136 // %100 = add %101, 0
6137 // ==>
6138 // %100 = COPY %101
6139 UseMI.setDesc(get(TargetOpcode::COPY));
6140 UseMI.removeOperand(
6141 UseMI.findRegisterUseOperandIdx(Reg, /*TRI=*/nullptr));
6142 UseMI.removeOperand(
6143 UseMI.findRegisterDefOperandIdx(X86::EFLAGS, /*TRI=*/nullptr));
6144 UseMI.untieRegOperand(0);
6147 } else {
6148 unsigned Op1 = 1, Op2 = CommuteAnyOperandIndex;
6149 unsigned ImmOpNum = 2;
6150 if (!UseMI.getOperand(0).isDef()) {
6151 Op1 = 0; // TEST, CMP, CTEST, CCMP
6152 ImmOpNum = 1;
6153 }
6154 if (Opc == TargetOpcode::COPY)
6155 ImmOpNum = 1;
6156 if (findCommutedOpIndices(UseMI, Op1, Op2) &&
6157 UseMI.getOperand(Op1).getReg() == Reg)
6158 commuteInstruction(UseMI);
6159
6160 assert(UseMI.getOperand(ImmOpNum).getReg() == Reg);
6161 UseMI.setDesc(get(NewOpc));
6162 UseMI.getOperand(ImmOpNum).ChangeToImmediate(ImmVal);
6163 }
6164 }
6165
6166 if (Reg.isVirtual() && MRI->use_nodbg_empty(Reg))
6168
6169 return true;
6170}
6171
6172/// foldImmediate - 'Reg' is known to be defined by a move immediate
6173/// instruction, try to fold the immediate into the use instruction.
6175 Register Reg, MachineRegisterInfo *MRI) const {
6176 int64_t ImmVal;
6177 if (!getConstValDefinedInReg(DefMI, Reg, ImmVal))
6178 return false;
6179
6180 return foldImmediateImpl(UseMI, &DefMI, Reg, ImmVal, MRI, true);
6181}
6182
6183/// Expand a single-def pseudo instruction to a two-addr
6184/// instruction with two undef reads of the register being defined.
6185/// This is used for mapping:
6186/// %xmm4 = V_SET0
6187/// to:
6188/// %xmm4 = PXORrr undef %xmm4, undef %xmm4
6189///
6191 const MCInstrDesc &Desc) {
6192 assert(Desc.getNumOperands() == 3 && "Expected two-addr instruction.");
6193 Register Reg = MIB.getReg(0);
6194 MIB->setDesc(Desc);
6195
6196 // MachineInstr::addOperand() will insert explicit operands before any
6197 // implicit operands.
6199 // But we don't trust that.
6200 assert(MIB.getReg(1) == Reg && MIB.getReg(2) == Reg && "Misplaced operand");
6201 return true;
6202}
6203
6204/// Expand a single-def pseudo instruction to a two-addr
6205/// instruction with two %k0 reads.
6206/// This is used for mapping:
6207/// %k4 = K_SET1
6208/// to:
6209/// %k4 = KXNORrr %k0, %k0
6211 Register Reg) {
6212 assert(Desc.getNumOperands() == 3 && "Expected two-addr instruction.");
6213 MIB->setDesc(Desc);
6215 return true;
6216}
6217
6219 bool MinusOne) {
6220 MachineBasicBlock &MBB = *MIB->getParent();
6221 const DebugLoc &DL = MIB->getDebugLoc();
6222 Register Reg = MIB.getReg(0);
6223
6224 // Insert the XOR.
6225 BuildMI(MBB, MIB.getInstr(), DL, TII.get(X86::XOR32rr), Reg)
6228
6229 // Turn the pseudo into an INC or DEC.
6230 MIB->setDesc(TII.get(MinusOne ? X86::DEC32r : X86::INC32r));
6231 MIB.addReg(Reg);
6232
6233 return true;
6234}
6235
6237 const TargetInstrInfo &TII,
6238 const X86Subtarget &Subtarget) {
6239 MachineBasicBlock &MBB = *MIB->getParent();
6240 const DebugLoc &DL = MIB->getDebugLoc();
6241 int64_t Imm = MIB->getOperand(1).getImm();
6242 assert(Imm != 0 && "Using push/pop for 0 is not efficient.");
6244
6245 int StackAdjustment;
6246
6247 if (Subtarget.is64Bit()) {
6248 assert(MIB->getOpcode() == X86::MOV64ImmSExti8 ||
6249 MIB->getOpcode() == X86::MOV32ImmSExti8);
6250
6251 // Can't use push/pop lowering if the function might write to the red zone.
6252 X86MachineFunctionInfo *X86FI =
6253 MBB.getParent()->getInfo<X86MachineFunctionInfo>();
6254 if (X86FI->getUsesRedZone()) {
6255 MIB->setDesc(TII.get(MIB->getOpcode() == X86::MOV32ImmSExti8
6256 ? X86::MOV32ri
6257 : X86::MOV64ri));
6258 return true;
6259 }
6260
6261 // 64-bit mode doesn't have 32-bit push/pop, so use 64-bit operations and
6262 // widen the register if necessary.
6263 StackAdjustment = 8;
6264 BuildMI(MBB, I, DL, TII.get(X86::PUSH64i32)).addImm(Imm);
6265 MIB->setDesc(TII.get(X86::POP64r));
6266 MIB->getOperand(0).setReg(getX86SubSuperRegister(MIB.getReg(0), 64));
6267 } else {
6268 assert(MIB->getOpcode() == X86::MOV32ImmSExti8);
6269 StackAdjustment = 4;
6270 BuildMI(MBB, I, DL, TII.get(X86::PUSH32i)).addImm(Imm);
6271 MIB->setDesc(TII.get(X86::POP32r));
6272 }
6273 MIB->removeOperand(1);
6274 MIB->addImplicitDefUseOperands(*MBB.getParent());
6275
6276 // Build CFI if necessary.
6277 MachineFunction &MF = *MBB.getParent();
6278 const X86FrameLowering *TFL = Subtarget.getFrameLowering();
6279 bool IsWin64Prologue = MF.getTarget().getMCAsmInfo().usesWindowsCFI();
6280 bool NeedsDwarfCFI = !IsWin64Prologue && MF.needsFrameMoves();
6281 bool EmitCFI = !TFL->hasFP(MF) && NeedsDwarfCFI;
6282 if (EmitCFI) {
6283 TFL->BuildCFI(
6284 MBB, I, DL,
6285 MCCFIInstruction::createAdjustCfaOffset(nullptr, StackAdjustment));
6286 TFL->BuildCFI(
6287 MBB, std::next(I), DL,
6288 MCCFIInstruction::createAdjustCfaOffset(nullptr, -StackAdjustment));
6289 }
6290
6291 return true;
6292}
6293
6294// LoadStackGuard has so far only been implemented for 64-bit MachO. Different
6295// code sequence is needed for other targets.
6297 const TargetInstrInfo &TII) {
6298 MachineBasicBlock &MBB = *MIB->getParent();
6299 const DebugLoc &DL = MIB->getDebugLoc();
6300 Register Reg = MIB.getReg(0);
6301 const GlobalValue *GV =
6302 cast<GlobalValue>((*MIB->memoperands_begin())->getValue());
6303 auto Flags = MachineMemOperand::MOLoad |
6306 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(
6307 MachinePointerInfo::getGOT(*MBB.getParent()), Flags, 8, Align(8));
6309
6310 BuildMI(MBB, I, DL, TII.get(X86::MOV64rm), Reg)
6311 .addReg(X86::RIP)
6312 .addImm(1)
6313 .addReg(0)
6315 .addReg(0)
6316 .addMemOperand(MMO);
6317 MIB->setDebugLoc(DL);
6318 MIB->setDesc(TII.get(X86::MOV64rm));
6320}
6321
6323 MachineBasicBlock &MBB = *MIB->getParent();
6324 MachineFunction &MF = *MBB.getParent();
6325 const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>();
6326 const X86RegisterInfo *TRI = Subtarget.getRegisterInfo();
6327 unsigned XorOp =
6328 MIB->getOpcode() == X86::XOR64_FP ? X86::XOR64rr : X86::XOR32rr;
6329 MIB->setDesc(TII.get(XorOp));
6330 MIB.addReg(TRI->getFrameRegister(MF), RegState::Undef);
6331 return true;
6332}
6333
6334// This is used to handle spills for 128/256-bit registers when we have AVX512,
6335// but not VLX. If it uses an extended register we need to use an instruction
6336// that loads the lower 128/256-bit, but is available with only AVX512F.
6338 const TargetRegisterInfo *TRI,
6339 const MCInstrDesc &LoadDesc,
6340 const MCInstrDesc &BroadcastDesc, unsigned SubIdx) {
6341 Register DestReg = MIB.getReg(0);
6342 // Check if DestReg is XMM16-31 or YMM16-31.
6343 if (TRI->getEncodingValue(DestReg) < 16) {
6344 // We can use a normal VEX encoded load.
6345 MIB->setDesc(LoadDesc);
6346 } else {
6347 // Use a 128/256-bit VBROADCAST instruction.
6348 MIB->setDesc(BroadcastDesc);
6349 // Change the destination to a 512-bit register.
6350 DestReg = TRI->getMatchingSuperReg(DestReg, SubIdx, &X86::VR512RegClass);
6351 MIB->getOperand(0).setReg(DestReg);
6352 }
6353 return true;
6354}
6355
6356// This is used to handle spills for 128/256-bit registers when we have AVX512,
6357// but not VLX. If it uses an extended register we need to use an instruction
6358// that stores the lower 128/256-bit, but is available with only AVX512F.
6360 const TargetRegisterInfo *TRI,
6361 const MCInstrDesc &StoreDesc,
6362 const MCInstrDesc &ExtractDesc, unsigned SubIdx) {
6363 Register SrcReg = MIB.getReg(X86::AddrNumOperands);
6364 // Check if DestReg is XMM16-31 or YMM16-31.
6365 if (TRI->getEncodingValue(SrcReg) < 16) {
6366 // We can use a normal VEX encoded store.
6367 MIB->setDesc(StoreDesc);
6368 } else {
6369 // Use a VEXTRACTF instruction.
6370 MIB->setDesc(ExtractDesc);
6371 // Change the destination to a 512-bit register.
6372 SrcReg = TRI->getMatchingSuperReg(SrcReg, SubIdx, &X86::VR512RegClass);
6374 MIB.addImm(0x0); // Append immediate to extract from the lower bits.
6375 }
6376
6377 return true;
6378}
6379
6381 MIB->setDesc(Desc);
6382 int64_t ShiftAmt = MIB->getOperand(2).getImm();
6383 // Temporarily remove the immediate so we can add another source register.
6384 MIB->removeOperand(2);
6385 // Add the register. Don't copy the kill flag if there is one.
6386 MIB.addReg(MIB.getReg(1), getUndefRegState(MIB->getOperand(1).isUndef()));
6387 // Add back the immediate.
6388 MIB.addImm(ShiftAmt);
6389 return true;
6390}
6391
6393 const TargetInstrInfo &TII, bool HasAVX) {
6394 unsigned NewOpc;
6395 if (MI.getOpcode() == X86::MOVSHPrm) {
6396 NewOpc = HasAVX ? X86::VMOVSSrm : X86::MOVSSrm;
6397 Register Reg = MI.getOperand(0).getReg();
6398 if (Reg > X86::XMM15)
6399 NewOpc = X86::VMOVSSZrm;
6400 } else {
6401 NewOpc = HasAVX ? X86::VMOVSSmr : X86::MOVSSmr;
6402 Register Reg = MI.getOperand(5).getReg();
6403 if (Reg > X86::XMM15)
6404 NewOpc = X86::VMOVSSZmr;
6405 }
6406
6407 MIB->setDesc(TII.get(NewOpc));
6408 return true;
6409}
6410
6412 bool HasAVX = Subtarget.hasAVX();
6413 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
6414 switch (MI.getOpcode()) {
6415 case X86::MOV32r0:
6416 return Expand2AddrUndef(MIB, get(X86::XOR32rr));
6417 case X86::MOV32r1:
6418 return expandMOV32r1(MIB, *this, /*MinusOne=*/false);
6419 case X86::MOV32r_1:
6420 return expandMOV32r1(MIB, *this, /*MinusOne=*/true);
6421 case X86::MOV32ImmSExti8:
6422 case X86::MOV64ImmSExti8:
6423 return ExpandMOVImmSExti8(MIB, *this, Subtarget);
6424 case X86::SETB_C32r:
6425 return Expand2AddrUndef(MIB, get(X86::SBB32rr));
6426 case X86::SETB_C64r:
6427 return Expand2AddrUndef(MIB, get(X86::SBB64rr));
6428 case X86::MMX_SET0:
6429 return Expand2AddrUndef(MIB, get(X86::MMX_PXORrr));
6430 case X86::V_SET0:
6431 case X86::FsFLD0SS:
6432 case X86::FsFLD0SD:
6433 case X86::FsFLD0SH:
6434 case X86::FsFLD0F128:
6435 return Expand2AddrUndef(MIB, get(HasAVX ? X86::VXORPSrr : X86::XORPSrr));
6436 case X86::AVX512_128_SET0:
6437 case X86::AVX512_FsFLD0SH:
6438 case X86::AVX512_FsFLD0SS:
6439 case X86::AVX512_FsFLD0SD:
6440 case X86::AVX512_FsFLD0F128: {
6441 bool HasVLX = Subtarget.hasVLX();
6442 Register SrcReg = MIB.getReg(0);
6444 if (HasVLX || TRI->getEncodingValue(SrcReg) < 16)
6445 return Expand2AddrUndef(MIB,
6446 get(HasVLX ? X86::VPXORDZ128rr : X86::VXORPSrr));
6447 // Extended register without VLX. Use a larger XOR.
6448 SrcReg =
6449 TRI->getMatchingSuperReg(SrcReg, X86::sub_xmm, &X86::VR512RegClass);
6450 MIB->getOperand(0).setReg(SrcReg);
6451 return Expand2AddrUndef(MIB, get(X86::VPXORDZrr));
6452 }
6453 case X86::MOVSHPmr:
6454 case X86::MOVSHPrm:
6455 return expandMOVSHP(MIB, MI, *this, Subtarget.hasAVX());
6456 case X86::V_SETALLONES:
6457 return Expand2AddrUndef(MIB,
6458 get(HasAVX ? X86::VPCMPEQDrr : X86::PCMPEQDrr));
6459 case X86::AVX2_SETALLONES:
6460 return Expand2AddrUndef(MIB, get(X86::VPCMPEQDYrr));
6461 case X86::AVX1_SETALLONES: {
6462 Register Reg = MIB.getReg(0);
6463 // VCMPPSYrri with an immediate 0xf should produce VCMPTRUEPS.
6464 MIB->setDesc(get(X86::VCMPPSYrri));
6465 MIB.addReg(Reg, RegState::Undef).addReg(Reg, RegState::Undef).addImm(0xf);
6466 return true;
6467 }
6468 case X86::AVX512_128_SETALLONES:
6469 case X86::AVX512_256_SETALLONES:
6470 case X86::AVX512_512_SETALLONES: {
6471 Register Reg = MIB.getReg(0);
6472 unsigned Opc;
6473 switch (MI.getOpcode()) {
6474 case X86::AVX512_128_SETALLONES: {
6475 if (X86::VR128RegClass.contains(Reg))
6476 return Expand2AddrUndef(MIB, get(X86::VPCMPEQDrr));
6477
6478 Opc = X86::VPTERNLOGDZ128rri;
6479 break;
6480 }
6481 case X86::AVX512_256_SETALLONES: {
6482 if (X86::VR256RegClass.contains(Reg))
6483 return Expand2AddrUndef(MIB, get(X86::VPCMPEQDYrr));
6484
6485 Opc = X86::VPTERNLOGDZ256rri;
6486 break;
6487 }
6488 case X86::AVX512_512_SETALLONES:
6489 Opc = X86::VPTERNLOGDZrri;
6490 break;
6491 }
6492 MIB->setDesc(get(Opc));
6493 // VPTERNLOGD needs 3 register inputs and an immediate.
6494 // 0xff will return 1s for any input.
6495 MIB.addReg(Reg, RegState::Undef)
6496 .addReg(Reg, RegState::Undef)
6497 .addReg(Reg, RegState::Undef)
6498 .addImm(0xff);
6499 return true;
6500 }
6501 case X86::AVX512_512_SEXT_MASK_32:
6502 case X86::AVX512_512_SEXT_MASK_64: {
6503 Register Reg = MIB.getReg(0);
6504 Register MaskReg = MIB.getReg(1);
6505 RegState MaskState = getRegState(MIB->getOperand(1));
6506 unsigned Opc = (MI.getOpcode() == X86::AVX512_512_SEXT_MASK_64)
6507 ? X86::VPTERNLOGQZrrikz
6508 : X86::VPTERNLOGDZrrikz;
6509 MI.removeOperand(1);
6510 MIB->setDesc(get(Opc));
6511 // VPTERNLOG needs 3 register inputs and an immediate.
6512 // 0xff will return 1s for any input.
6513 MIB.addReg(Reg, RegState::Undef)
6514 .addReg(MaskReg, MaskState)
6515 .addReg(Reg, RegState::Undef)
6516 .addReg(Reg, RegState::Undef)
6517 .addImm(0xff);
6518 return true;
6519 }
6520 case X86::VMOVAPSZ128rm_NOVLX:
6521 return expandNOVLXLoad(MIB, &getRegisterInfo(), get(X86::VMOVAPSrm),
6522 get(X86::VBROADCASTF32X4Zrm), X86::sub_xmm);
6523 case X86::VMOVUPSZ128rm_NOVLX:
6524 return expandNOVLXLoad(MIB, &getRegisterInfo(), get(X86::VMOVUPSrm),
6525 get(X86::VBROADCASTF32X4Zrm), X86::sub_xmm);
6526 case X86::VMOVAPSZ256rm_NOVLX:
6527 return expandNOVLXLoad(MIB, &getRegisterInfo(), get(X86::VMOVAPSYrm),
6528 get(X86::VBROADCASTF64X4Zrm), X86::sub_ymm);
6529 case X86::VMOVUPSZ256rm_NOVLX:
6530 return expandNOVLXLoad(MIB, &getRegisterInfo(), get(X86::VMOVUPSYrm),
6531 get(X86::VBROADCASTF64X4Zrm), X86::sub_ymm);
6532 case X86::VMOVAPSZ128mr_NOVLX:
6533 return expandNOVLXStore(MIB, &getRegisterInfo(), get(X86::VMOVAPSmr),
6534 get(X86::VEXTRACTF32X4Zmri), X86::sub_xmm);
6535 case X86::VMOVUPSZ128mr_NOVLX:
6536 return expandNOVLXStore(MIB, &getRegisterInfo(), get(X86::VMOVUPSmr),
6537 get(X86::VEXTRACTF32X4Zmri), X86::sub_xmm);
6538 case X86::VMOVAPSZ256mr_NOVLX:
6539 return expandNOVLXStore(MIB, &getRegisterInfo(), get(X86::VMOVAPSYmr),
6540 get(X86::VEXTRACTF64X4Zmri), X86::sub_ymm);
6541 case X86::VMOVUPSZ256mr_NOVLX:
6542 return expandNOVLXStore(MIB, &getRegisterInfo(), get(X86::VMOVUPSYmr),
6543 get(X86::VEXTRACTF64X4Zmri), X86::sub_ymm);
6544 case X86::MOV32ri64: {
6545 Register Reg = MIB.getReg(0);
6546 Register Reg32 = RI.getSubReg(Reg, X86::sub_32bit);
6547 MI.setDesc(get(X86::MOV32ri));
6548 MIB->getOperand(0).setReg(Reg32);
6550 return true;
6551 }
6552
6553 case X86::RDFLAGS32:
6554 case X86::RDFLAGS64: {
6555 unsigned Is64Bit = MI.getOpcode() == X86::RDFLAGS64;
6556 MachineBasicBlock &MBB = *MIB->getParent();
6557
6558 MachineInstr *NewMI = BuildMI(MBB, MI, MIB->getDebugLoc(),
6559 get(Is64Bit ? X86::PUSHF64 : X86::PUSHF32))
6560 .getInstr();
6561
6562 // Permit reads of the EFLAGS and DF registers without them being defined.
6563 // This intrinsic exists to read external processor state in flags, such as
6564 // the trap flag, interrupt flag, and direction flag, none of which are
6565 // modeled by the backend.
6566 assert(NewMI->getOperand(2).getReg() == X86::EFLAGS &&
6567 "Unexpected register in operand! Should be EFLAGS.");
6568 NewMI->getOperand(2).setIsUndef();
6569 assert(NewMI->getOperand(3).getReg() == X86::DF &&
6570 "Unexpected register in operand! Should be DF.");
6571 NewMI->getOperand(3).setIsUndef();
6572
6573 MIB->setDesc(get(Is64Bit ? X86::POP64r : X86::POP32r));
6574 return true;
6575 }
6576
6577 case X86::WRFLAGS32:
6578 case X86::WRFLAGS64: {
6579 unsigned Is64Bit = MI.getOpcode() == X86::WRFLAGS64;
6580 MachineBasicBlock &MBB = *MIB->getParent();
6581
6582 BuildMI(MBB, MI, MIB->getDebugLoc(),
6583 get(Is64Bit ? X86::PUSH64r : X86::PUSH32r))
6584 .addReg(MI.getOperand(0).getReg());
6585 BuildMI(MBB, MI, MIB->getDebugLoc(),
6586 get(Is64Bit ? X86::POPF64 : X86::POPF32));
6587 MI.eraseFromParent();
6588 return true;
6589 }
6590
6591 // KNL does not recognize dependency-breaking idioms for mask registers,
6592 // so kxnor %k1, %k1, %k2 has a RAW dependence on %k1.
6593 // Using %k0 as the undef input register is a performance heuristic based
6594 // on the assumption that %k0 is used less frequently than the other mask
6595 // registers, since it is not usable as a write mask.
6596 // FIXME: A more advanced approach would be to choose the best input mask
6597 // register based on context.
6598 case X86::KSET0B:
6599 return Expand2AddrKreg(MIB, get(X86::KXORBkk), X86::K0);
6600 case X86::KSET0W:
6601 return Expand2AddrKreg(MIB, get(X86::KXORWkk), X86::K0);
6602 case X86::KSET0D:
6603 return Expand2AddrKreg(MIB, get(X86::KXORDkk), X86::K0);
6604 case X86::KSET0Q:
6605 return Expand2AddrKreg(MIB, get(X86::KXORQkk), X86::K0);
6606 case X86::KSET1B:
6607 return Expand2AddrKreg(MIB, get(X86::KXNORBkk), X86::K0);
6608 case X86::KSET1W:
6609 return Expand2AddrKreg(MIB, get(X86::KXNORWkk), X86::K0);
6610 case X86::KSET1D:
6611 return Expand2AddrKreg(MIB, get(X86::KXNORDkk), X86::K0);
6612 case X86::KSET1Q:
6613 return Expand2AddrKreg(MIB, get(X86::KXNORQkk), X86::K0);
6614 case TargetOpcode::LOAD_STACK_GUARD:
6615 expandLoadStackGuard(MIB, *this);
6616 return true;
6617 case X86::XOR64_FP:
6618 case X86::XOR32_FP:
6619 return expandXorFP(MIB, *this);
6620 case X86::SHLDROT32ri:
6621 return expandSHXDROT(MIB, get(X86::SHLD32rri8));
6622 case X86::SHLDROT64ri:
6623 return expandSHXDROT(MIB, get(X86::SHLD64rri8));
6624 case X86::SHRDROT32ri:
6625 return expandSHXDROT(MIB, get(X86::SHRD32rri8));
6626 case X86::SHRDROT64ri:
6627 return expandSHXDROT(MIB, get(X86::SHRD64rri8));
6628 case X86::ADD8rr_DB:
6629 MIB->setDesc(get(X86::OR8rr));
6630 break;
6631 case X86::ADD16rr_DB:
6632 MIB->setDesc(get(X86::OR16rr));
6633 break;
6634 case X86::ADD32rr_DB:
6635 MIB->setDesc(get(X86::OR32rr));
6636 break;
6637 case X86::ADD64rr_DB:
6638 MIB->setDesc(get(X86::OR64rr));
6639 break;
6640 case X86::ADD8ri_DB:
6641 MIB->setDesc(get(X86::OR8ri));
6642 break;
6643 case X86::ADD16ri_DB:
6644 MIB->setDesc(get(X86::OR16ri));
6645 break;
6646 case X86::ADD32ri_DB:
6647 MIB->setDesc(get(X86::OR32ri));
6648 break;
6649 case X86::ADD64ri32_DB:
6650 MIB->setDesc(get(X86::OR64ri32));
6651 break;
6652 }
6653 return false;
6654}
6655
6656/// Return true for all instructions that only update
6657/// the first 32 or 64-bits of the destination register and leave the rest
6658/// unmodified. This can be used to avoid folding loads if the instructions
6659/// only update part of the destination register, and the non-updated part is
6660/// not needed. e.g. cvtss2sd, sqrtss. Unfolding the load from these
6661/// instructions breaks the partial register dependency and it can improve
6662/// performance. e.g.:
6663///
6664/// movss (%rdi), %xmm0
6665/// cvtss2sd %xmm0, %xmm0
6666///
6667/// Instead of
6668/// cvtss2sd (%rdi), %xmm0
6669///
6670/// FIXME: This should be turned into a TSFlags.
6671///
6672static bool hasPartialRegUpdate(unsigned Opcode, const X86Subtarget &Subtarget,
6673 bool ForLoadFold = false) {
6674 switch (Opcode) {
6675 case X86::CVTSI2SSrr:
6676 case X86::CVTSI2SSrm:
6677 case X86::CVTSI642SSrr:
6678 case X86::CVTSI642SSrm:
6679 case X86::CVTSI2SDrr:
6680 case X86::CVTSI2SDrm:
6681 case X86::CVTSI642SDrr:
6682 case X86::CVTSI642SDrm:
6683 // Load folding won't effect the undef register update since the input is
6684 // a GPR.
6685 return !ForLoadFold;
6686 case X86::CVTSD2SSrr:
6687 case X86::CVTSD2SSrm:
6688 case X86::CVTSS2SDrr:
6689 case X86::CVTSS2SDrm:
6690 case X86::MOVHPDrm:
6691 case X86::MOVHPSrm:
6692 case X86::MOVLPDrm:
6693 case X86::MOVLPSrm:
6694 case X86::RCPSSr:
6695 case X86::RCPSSm:
6696 case X86::RCPSSr_Int:
6697 case X86::RCPSSm_Int:
6698 case X86::ROUNDSDri:
6699 case X86::ROUNDSDmi:
6700 case X86::ROUNDSSri:
6701 case X86::ROUNDSSmi:
6702 case X86::RSQRTSSr:
6703 case X86::RSQRTSSm:
6704 case X86::RSQRTSSr_Int:
6705 case X86::RSQRTSSm_Int:
6706 case X86::SQRTSSr:
6707 case X86::SQRTSSm:
6708 case X86::SQRTSSr_Int:
6709 case X86::SQRTSSm_Int:
6710 case X86::SQRTSDr:
6711 case X86::SQRTSDm:
6712 case X86::SQRTSDr_Int:
6713 case X86::SQRTSDm_Int:
6714 return true;
6715 case X86::VFCMULCPHZ128rm:
6716 case X86::VFCMULCPHZ128rmb:
6717 case X86::VFCMULCPHZ128rmbkz:
6718 case X86::VFCMULCPHZ128rmkz:
6719 case X86::VFCMULCPHZ128rr:
6720 case X86::VFCMULCPHZ128rrkz:
6721 case X86::VFCMULCPHZ256rm:
6722 case X86::VFCMULCPHZ256rmb:
6723 case X86::VFCMULCPHZ256rmbkz:
6724 case X86::VFCMULCPHZ256rmkz:
6725 case X86::VFCMULCPHZ256rr:
6726 case X86::VFCMULCPHZ256rrkz:
6727 case X86::VFCMULCPHZrm:
6728 case X86::VFCMULCPHZrmb:
6729 case X86::VFCMULCPHZrmbkz:
6730 case X86::VFCMULCPHZrmkz:
6731 case X86::VFCMULCPHZrr:
6732 case X86::VFCMULCPHZrrb:
6733 case X86::VFCMULCPHZrrbkz:
6734 case X86::VFCMULCPHZrrkz:
6735 case X86::VFMULCPHZ128rm:
6736 case X86::VFMULCPHZ128rmb:
6737 case X86::VFMULCPHZ128rmbkz:
6738 case X86::VFMULCPHZ128rmkz:
6739 case X86::VFMULCPHZ128rr:
6740 case X86::VFMULCPHZ128rrkz:
6741 case X86::VFMULCPHZ256rm:
6742 case X86::VFMULCPHZ256rmb:
6743 case X86::VFMULCPHZ256rmbkz:
6744 case X86::VFMULCPHZ256rmkz:
6745 case X86::VFMULCPHZ256rr:
6746 case X86::VFMULCPHZ256rrkz:
6747 case X86::VFMULCPHZrm:
6748 case X86::VFMULCPHZrmb:
6749 case X86::VFMULCPHZrmbkz:
6750 case X86::VFMULCPHZrmkz:
6751 case X86::VFMULCPHZrr:
6752 case X86::VFMULCPHZrrb:
6753 case X86::VFMULCPHZrrbkz:
6754 case X86::VFMULCPHZrrkz:
6755 case X86::VFCMULCSHZrm:
6756 case X86::VFCMULCSHZrmkz:
6757 case X86::VFCMULCSHZrr:
6758 case X86::VFCMULCSHZrrb:
6759 case X86::VFCMULCSHZrrbkz:
6760 case X86::VFCMULCSHZrrkz:
6761 case X86::VFMULCSHZrm:
6762 case X86::VFMULCSHZrmkz:
6763 case X86::VFMULCSHZrr:
6764 case X86::VFMULCSHZrrb:
6765 case X86::VFMULCSHZrrbkz:
6766 case X86::VFMULCSHZrrkz:
6767 return Subtarget.hasMULCFalseDeps();
6768 case X86::VPERMDYrm:
6769 case X86::VPERMDYrr:
6770 case X86::VPERMQYmi:
6771 case X86::VPERMQYri:
6772 case X86::VPERMPSYrm:
6773 case X86::VPERMPSYrr:
6774 case X86::VPERMPDYmi:
6775 case X86::VPERMPDYri:
6776 case X86::VPERMDZ256rm:
6777 case X86::VPERMDZ256rmb:
6778 case X86::VPERMDZ256rmbkz:
6779 case X86::VPERMDZ256rmkz:
6780 case X86::VPERMDZ256rr:
6781 case X86::VPERMDZ256rrkz:
6782 case X86::VPERMDZrm:
6783 case X86::VPERMDZrmb:
6784 case X86::VPERMDZrmbkz:
6785 case X86::VPERMDZrmkz:
6786 case X86::VPERMDZrr:
6787 case X86::VPERMDZrrkz:
6788 case X86::VPERMQZ256mbi:
6789 case X86::VPERMQZ256mbikz:
6790 case X86::VPERMQZ256mi:
6791 case X86::VPERMQZ256mikz:
6792 case X86::VPERMQZ256ri:
6793 case X86::VPERMQZ256rikz:
6794 case X86::VPERMQZ256rm:
6795 case X86::VPERMQZ256rmb:
6796 case X86::VPERMQZ256rmbkz:
6797 case X86::VPERMQZ256rmkz:
6798 case X86::VPERMQZ256rr:
6799 case X86::VPERMQZ256rrkz:
6800 case X86::VPERMQZmbi:
6801 case X86::VPERMQZmbikz:
6802 case X86::VPERMQZmi:
6803 case X86::VPERMQZmikz:
6804 case X86::VPERMQZri:
6805 case X86::VPERMQZrikz:
6806 case X86::VPERMQZrm:
6807 case X86::VPERMQZrmb:
6808 case X86::VPERMQZrmbkz:
6809 case X86::VPERMQZrmkz:
6810 case X86::VPERMQZrr:
6811 case X86::VPERMQZrrkz:
6812 case X86::VPERMPSZ256rm:
6813 case X86::VPERMPSZ256rmb:
6814 case X86::VPERMPSZ256rmbkz:
6815 case X86::VPERMPSZ256rmkz:
6816 case X86::VPERMPSZ256rr:
6817 case X86::VPERMPSZ256rrkz:
6818 case X86::VPERMPSZrm:
6819 case X86::VPERMPSZrmb:
6820 case X86::VPERMPSZrmbkz:
6821 case X86::VPERMPSZrmkz:
6822 case X86::VPERMPSZrr:
6823 case X86::VPERMPSZrrkz:
6824 case X86::VPERMPDZ256mbi:
6825 case X86::VPERMPDZ256mbikz:
6826 case X86::VPERMPDZ256mi:
6827 case X86::VPERMPDZ256mikz:
6828 case X86::VPERMPDZ256ri:
6829 case X86::VPERMPDZ256rikz:
6830 case X86::VPERMPDZ256rm:
6831 case X86::VPERMPDZ256rmb:
6832 case X86::VPERMPDZ256rmbkz:
6833 case X86::VPERMPDZ256rmkz:
6834 case X86::VPERMPDZ256rr:
6835 case X86::VPERMPDZ256rrkz:
6836 case X86::VPERMPDZmbi:
6837 case X86::VPERMPDZmbikz:
6838 case X86::VPERMPDZmi:
6839 case X86::VPERMPDZmikz:
6840 case X86::VPERMPDZri:
6841 case X86::VPERMPDZrikz:
6842 case X86::VPERMPDZrm:
6843 case X86::VPERMPDZrmb:
6844 case X86::VPERMPDZrmbkz:
6845 case X86::VPERMPDZrmkz:
6846 case X86::VPERMPDZrr:
6847 case X86::VPERMPDZrrkz:
6848 return Subtarget.hasPERMFalseDeps();
6849 case X86::VRANGEPDZ128rmbi:
6850 case X86::VRANGEPDZ128rmbikz:
6851 case X86::VRANGEPDZ128rmi:
6852 case X86::VRANGEPDZ128rmikz:
6853 case X86::VRANGEPDZ128rri:
6854 case X86::VRANGEPDZ128rrikz:
6855 case X86::VRANGEPDZ256rmbi:
6856 case X86::VRANGEPDZ256rmbikz:
6857 case X86::VRANGEPDZ256rmi:
6858 case X86::VRANGEPDZ256rmikz:
6859 case X86::VRANGEPDZ256rri:
6860 case X86::VRANGEPDZ256rrikz:
6861 case X86::VRANGEPDZrmbi:
6862 case X86::VRANGEPDZrmbikz:
6863 case X86::VRANGEPDZrmi:
6864 case X86::VRANGEPDZrmikz:
6865 case X86::VRANGEPDZrri:
6866 case X86::VRANGEPDZrrib:
6867 case X86::VRANGEPDZrribkz:
6868 case X86::VRANGEPDZrrikz:
6869 case X86::VRANGEPSZ128rmbi:
6870 case X86::VRANGEPSZ128rmbikz:
6871 case X86::VRANGEPSZ128rmi:
6872 case X86::VRANGEPSZ128rmikz:
6873 case X86::VRANGEPSZ128rri:
6874 case X86::VRANGEPSZ128rrikz:
6875 case X86::VRANGEPSZ256rmbi:
6876 case X86::VRANGEPSZ256rmbikz:
6877 case X86::VRANGEPSZ256rmi:
6878 case X86::VRANGEPSZ256rmikz:
6879 case X86::VRANGEPSZ256rri:
6880 case X86::VRANGEPSZ256rrikz:
6881 case X86::VRANGEPSZrmbi:
6882 case X86::VRANGEPSZrmbikz:
6883 case X86::VRANGEPSZrmi:
6884 case X86::VRANGEPSZrmikz:
6885 case X86::VRANGEPSZrri:
6886 case X86::VRANGEPSZrrib:
6887 case X86::VRANGEPSZrribkz:
6888 case X86::VRANGEPSZrrikz:
6889 case X86::VRANGESDZrmi:
6890 case X86::VRANGESDZrmikz:
6891 case X86::VRANGESDZrri:
6892 case X86::VRANGESDZrrib:
6893 case X86::VRANGESDZrribkz:
6894 case X86::VRANGESDZrrikz:
6895 case X86::VRANGESSZrmi:
6896 case X86::VRANGESSZrmikz:
6897 case X86::VRANGESSZrri:
6898 case X86::VRANGESSZrrib:
6899 case X86::VRANGESSZrribkz:
6900 case X86::VRANGESSZrrikz:
6901 return Subtarget.hasRANGEFalseDeps();
6902 case X86::VGETMANTSSZrmi:
6903 case X86::VGETMANTSSZrmikz:
6904 case X86::VGETMANTSSZrri:
6905 case X86::VGETMANTSSZrrib:
6906 case X86::VGETMANTSSZrribkz:
6907 case X86::VGETMANTSSZrrikz:
6908 case X86::VGETMANTSDZrmi:
6909 case X86::VGETMANTSDZrmikz:
6910 case X86::VGETMANTSDZrri:
6911 case X86::VGETMANTSDZrrib:
6912 case X86::VGETMANTSDZrribkz:
6913 case X86::VGETMANTSDZrrikz:
6914 case X86::VGETMANTSHZrmi:
6915 case X86::VGETMANTSHZrmikz:
6916 case X86::VGETMANTSHZrri:
6917 case X86::VGETMANTSHZrrib:
6918 case X86::VGETMANTSHZrribkz:
6919 case X86::VGETMANTSHZrrikz:
6920 case X86::VGETMANTPSZ128rmbi:
6921 case X86::VGETMANTPSZ128rmbikz:
6922 case X86::VGETMANTPSZ128rmi:
6923 case X86::VGETMANTPSZ128rmikz:
6924 case X86::VGETMANTPSZ256rmbi:
6925 case X86::VGETMANTPSZ256rmbikz:
6926 case X86::VGETMANTPSZ256rmi:
6927 case X86::VGETMANTPSZ256rmikz:
6928 case X86::VGETMANTPSZrmbi:
6929 case X86::VGETMANTPSZrmbikz:
6930 case X86::VGETMANTPSZrmi:
6931 case X86::VGETMANTPSZrmikz:
6932 case X86::VGETMANTPDZ128rmbi:
6933 case X86::VGETMANTPDZ128rmbikz:
6934 case X86::VGETMANTPDZ128rmi:
6935 case X86::VGETMANTPDZ128rmikz:
6936 case X86::VGETMANTPDZ256rmbi:
6937 case X86::VGETMANTPDZ256rmbikz:
6938 case X86::VGETMANTPDZ256rmi:
6939 case X86::VGETMANTPDZ256rmikz:
6940 case X86::VGETMANTPDZrmbi:
6941 case X86::VGETMANTPDZrmbikz:
6942 case X86::VGETMANTPDZrmi:
6943 case X86::VGETMANTPDZrmikz:
6944 return Subtarget.hasGETMANTFalseDeps();
6945 case X86::VPMULLQZ128rm:
6946 case X86::VPMULLQZ128rmb:
6947 case X86::VPMULLQZ128rmbkz:
6948 case X86::VPMULLQZ128rmkz:
6949 case X86::VPMULLQZ128rr:
6950 case X86::VPMULLQZ128rrkz:
6951 case X86::VPMULLQZ256rm:
6952 case X86::VPMULLQZ256rmb:
6953 case X86::VPMULLQZ256rmbkz:
6954 case X86::VPMULLQZ256rmkz:
6955 case X86::VPMULLQZ256rr:
6956 case X86::VPMULLQZ256rrkz:
6957 case X86::VPMULLQZrm:
6958 case X86::VPMULLQZrmb:
6959 case X86::VPMULLQZrmbkz:
6960 case X86::VPMULLQZrmkz:
6961 case X86::VPMULLQZrr:
6962 case X86::VPMULLQZrrkz:
6963 return Subtarget.hasMULLQFalseDeps();
6964 case X86::VPCOMPRESSBZ128rrkz:
6965 case X86::VPCOMPRESSBZ256rrkz:
6966 case X86::VPCOMPRESSBZrrkz:
6967 case X86::VPCOMPRESSWZ128rrkz:
6968 case X86::VPCOMPRESSWZ256rrkz:
6969 case X86::VPCOMPRESSWZrrkz:
6970 case X86::VPCOMPRESSDZ128rrkz:
6971 case X86::VPCOMPRESSDZ256rrkz:
6972 case X86::VPCOMPRESSDZrrkz:
6973 case X86::VPCOMPRESSQZ128rrkz:
6974 case X86::VPCOMPRESSQZ256rrkz:
6975 case X86::VPCOMPRESSQZrrkz:
6976 case X86::VCOMPRESSPSZ128rrkz:
6977 case X86::VCOMPRESSPSZ256rrkz:
6978 case X86::VCOMPRESSPSZrrkz:
6979 case X86::VCOMPRESSPDZ128rrkz:
6980 case X86::VCOMPRESSPDZ256rrkz:
6981 case X86::VCOMPRESSPDZrrkz:
6982 return Subtarget.hasCOMPRESSFalseDeps();
6983 case X86::VPEXPANDBZ128rmkz:
6984 case X86::VPEXPANDBZ128rrkz:
6985 case X86::VPEXPANDBZ256rmkz:
6986 case X86::VPEXPANDBZ256rrkz:
6987 case X86::VPEXPANDBZrmkz:
6988 case X86::VPEXPANDBZrrkz:
6989 case X86::VPEXPANDWZ128rmkz:
6990 case X86::VPEXPANDWZ128rrkz:
6991 case X86::VPEXPANDWZ256rmkz:
6992 case X86::VPEXPANDWZ256rrkz:
6993 case X86::VPEXPANDWZrmkz:
6994 case X86::VPEXPANDWZrrkz:
6995 case X86::VPEXPANDDZ128rmkz:
6996 case X86::VPEXPANDDZ128rrkz:
6997 case X86::VPEXPANDDZ256rmkz:
6998 case X86::VPEXPANDDZ256rrkz:
6999 case X86::VPEXPANDDZrmkz:
7000 case X86::VPEXPANDDZrrkz:
7001 case X86::VPEXPANDQZ128rmkz:
7002 case X86::VPEXPANDQZ128rrkz:
7003 case X86::VPEXPANDQZ256rmkz:
7004 case X86::VPEXPANDQZ256rrkz:
7005 case X86::VPEXPANDQZrmkz:
7006 case X86::VPEXPANDQZrrkz:
7007 case X86::VEXPANDPSZ128rmkz:
7008 case X86::VEXPANDPSZ128rrkz:
7009 case X86::VEXPANDPSZ256rmkz:
7010 case X86::VEXPANDPSZ256rrkz:
7011 case X86::VEXPANDPSZrmkz:
7012 case X86::VEXPANDPSZrrkz:
7013 case X86::VEXPANDPDZ128rmkz:
7014 case X86::VEXPANDPDZ128rrkz:
7015 case X86::VEXPANDPDZ256rmkz:
7016 case X86::VEXPANDPDZ256rrkz:
7017 case X86::VEXPANDPDZrmkz:
7018 case X86::VEXPANDPDZrrkz:
7019 return Subtarget.hasEXPANDFalseDeps();
7020 // GPR
7021 case X86::POPCNT32rm:
7022 case X86::POPCNT32rr:
7023 case X86::POPCNT64rm:
7024 case X86::POPCNT64rr:
7025 return Subtarget.hasPOPCNTFalseDeps();
7026 case X86::LZCNT32rm:
7027 case X86::LZCNT32rr:
7028 case X86::LZCNT64rm:
7029 case X86::LZCNT64rr:
7030 return Subtarget.hasLZCNTFalseDeps();
7031 case X86::TZCNT32rm:
7032 case X86::TZCNT32rr:
7033 case X86::TZCNT64rm:
7034 case X86::TZCNT64rr:
7035 return Subtarget.hasTZCNTFalseDeps();
7036 case X86::BLSR32rr:
7037 case X86::BLSR32rm:
7038 case X86::BLSR64rr:
7039 case X86::BLSR64rm:
7040 case X86::BLSI32rr:
7041 case X86::BLSI32rm:
7042 case X86::BLSI64rr:
7043 case X86::BLSI64rm:
7044 case X86::BLSMSK32rr:
7045 case X86::BLSMSK32rm:
7046 case X86::BLSMSK64rr:
7047 case X86::BLSMSK64rm:
7048 return Subtarget.hasBLSFalseDeps() && !ForLoadFold; // Preserve load folding
7049 }
7050
7051 return false;
7052}
7053
7054/// Inform the BreakFalseDeps pass how many idle
7055/// instructions we would like before a partial register update.
7057 const MachineInstr &MI, unsigned OpNum,
7058 const TargetRegisterInfo *TRI) const {
7059
7060 if (OpNum != 0)
7061 return 0;
7062
7063 // NDD ops with 8/16b results may appear to be partial register
7064 // updates after register allocation.
7065 bool HasNDDPartialWrite = false;
7066 if (X86II::hasNewDataDest(MI.getDesc().TSFlags)) {
7067 Register Reg = MI.getOperand(0).getReg();
7068 if (!Reg.isVirtual())
7069 HasNDDPartialWrite =
7070 X86::GR8RegClass.contains(Reg) || X86::GR16RegClass.contains(Reg);
7071 }
7072
7073 if (!(HasNDDPartialWrite || hasPartialRegUpdate(MI.getOpcode(), Subtarget)))
7074 return 0;
7075
7076 // Check if the result register is also used as a source.
7077 // For non-NDD ops, this means a partial update is wanted, hence we return 0.
7078 // For NDD ops, this means it is possible to compress the instruction
7079 // to a legacy form in CompressEVEX, which would create an unwanted partial
7080 // update, so we return the clearance.
7081 const MachineOperand &MO = MI.getOperand(0);
7082 Register Reg = MO.getReg();
7083 bool ReadsReg = false;
7084 if (Reg.isVirtual())
7085 ReadsReg = (MO.readsReg() || MI.readsVirtualRegister(Reg));
7086 else
7087 ReadsReg = MI.readsRegister(Reg, TRI);
7088 if (ReadsReg != HasNDDPartialWrite)
7089 return 0;
7090
7091 // If any instructions in the clearance range are reading Reg, insert a
7092 // dependency breaking instruction, which is inexpensive and is likely to
7093 // be hidden in other instruction's cycles.
7095}
7096
7097// Return true for any instruction the copies the high bits of the first source
7098// operand into the unused high bits of the destination operand.
7099// Also returns true for instructions that have two inputs where one may
7100// be undef and we want it to use the same register as the other input.
7101static bool hasUndefRegUpdate(unsigned Opcode, unsigned OpNum,
7102 bool ForLoadFold = false) {
7103 // Set the OpNum parameter to the first source operand.
7104 switch (Opcode) {
7105 case X86::MMX_PUNPCKHBWrr:
7106 case X86::MMX_PUNPCKHWDrr:
7107 case X86::MMX_PUNPCKHDQrr:
7108 case X86::MMX_PUNPCKLBWrr:
7109 case X86::MMX_PUNPCKLWDrr:
7110 case X86::MMX_PUNPCKLDQrr:
7111 case X86::MOVHLPSrr:
7112 case X86::PACKSSWBrr:
7113 case X86::PACKUSWBrr:
7114 case X86::PACKSSDWrr:
7115 case X86::PACKUSDWrr:
7116 case X86::PUNPCKHBWrr:
7117 case X86::PUNPCKLBWrr:
7118 case X86::PUNPCKHWDrr:
7119 case X86::PUNPCKLWDrr:
7120 case X86::PUNPCKHDQrr:
7121 case X86::PUNPCKLDQrr:
7122 case X86::PUNPCKHQDQrr:
7123 case X86::PUNPCKLQDQrr:
7124 case X86::SHUFPDrri:
7125 case X86::SHUFPSrri:
7126 // These instructions are sometimes used with an undef first or second
7127 // source. Return true here so BreakFalseDeps will assign this source to the
7128 // same register as the first source to avoid a false dependency.
7129 // Operand 1 of these instructions is tied so they're separate from their
7130 // VEX counterparts.
7131 return OpNum == 2 && !ForLoadFold;
7132
7133 case X86::VMOVLHPSrr:
7134 case X86::VMOVLHPSZrr:
7135 case X86::VPACKSSWBrr:
7136 case X86::VPACKUSWBrr:
7137 case X86::VPACKSSDWrr:
7138 case X86::VPACKUSDWrr:
7139 case X86::VPACKSSWBZ128rr:
7140 case X86::VPACKUSWBZ128rr:
7141 case X86::VPACKSSDWZ128rr:
7142 case X86::VPACKUSDWZ128rr:
7143 case X86::VPERM2F128rri:
7144 case X86::VPERM2I128rri:
7145 case X86::VSHUFF32X4Z256rri:
7146 case X86::VSHUFF32X4Zrri:
7147 case X86::VSHUFF64X2Z256rri:
7148 case X86::VSHUFF64X2Zrri:
7149 case X86::VSHUFI32X4Z256rri:
7150 case X86::VSHUFI32X4Zrri:
7151 case X86::VSHUFI64X2Z256rri:
7152 case X86::VSHUFI64X2Zrri:
7153 case X86::VPUNPCKHBWrr:
7154 case X86::VPUNPCKLBWrr:
7155 case X86::VPUNPCKHBWYrr:
7156 case X86::VPUNPCKLBWYrr:
7157 case X86::VPUNPCKHBWZ128rr:
7158 case X86::VPUNPCKLBWZ128rr:
7159 case X86::VPUNPCKHBWZ256rr:
7160 case X86::VPUNPCKLBWZ256rr:
7161 case X86::VPUNPCKHBWZrr:
7162 case X86::VPUNPCKLBWZrr:
7163 case X86::VPUNPCKHWDrr:
7164 case X86::VPUNPCKLWDrr:
7165 case X86::VPUNPCKHWDYrr:
7166 case X86::VPUNPCKLWDYrr:
7167 case X86::VPUNPCKHWDZ128rr:
7168 case X86::VPUNPCKLWDZ128rr:
7169 case X86::VPUNPCKHWDZ256rr:
7170 case X86::VPUNPCKLWDZ256rr:
7171 case X86::VPUNPCKHWDZrr:
7172 case X86::VPUNPCKLWDZrr:
7173 case X86::VPUNPCKHDQrr:
7174 case X86::VPUNPCKLDQrr:
7175 case X86::VPUNPCKHDQYrr:
7176 case X86::VPUNPCKLDQYrr:
7177 case X86::VPUNPCKHDQZ128rr:
7178 case X86::VPUNPCKLDQZ128rr:
7179 case X86::VPUNPCKHDQZ256rr:
7180 case X86::VPUNPCKLDQZ256rr:
7181 case X86::VPUNPCKHDQZrr:
7182 case X86::VPUNPCKLDQZrr:
7183 case X86::VPUNPCKHQDQrr:
7184 case X86::VPUNPCKLQDQrr:
7185 case X86::VPUNPCKHQDQYrr:
7186 case X86::VPUNPCKLQDQYrr:
7187 case X86::VPUNPCKHQDQZ128rr:
7188 case X86::VPUNPCKLQDQZ128rr:
7189 case X86::VPUNPCKHQDQZ256rr:
7190 case X86::VPUNPCKLQDQZ256rr:
7191 case X86::VPUNPCKHQDQZrr:
7192 case X86::VPUNPCKLQDQZrr:
7193 // These instructions are sometimes used with an undef first or second
7194 // source. Return true here so BreakFalseDeps will assign this source to the
7195 // same register as the first source to avoid a false dependency.
7196 return (OpNum == 1 || OpNum == 2) && !ForLoadFold;
7197
7198 case X86::VCVTSI2SSrr:
7199 case X86::VCVTSI2SSrm:
7200 case X86::VCVTSI2SSrr_Int:
7201 case X86::VCVTSI2SSrm_Int:
7202 case X86::VCVTSI642SSrr:
7203 case X86::VCVTSI642SSrm:
7204 case X86::VCVTSI642SSrr_Int:
7205 case X86::VCVTSI642SSrm_Int:
7206 case X86::VCVTSI2SDrr:
7207 case X86::VCVTSI2SDrm:
7208 case X86::VCVTSI2SDrr_Int:
7209 case X86::VCVTSI2SDrm_Int:
7210 case X86::VCVTSI642SDrr:
7211 case X86::VCVTSI642SDrm:
7212 case X86::VCVTSI642SDrr_Int:
7213 case X86::VCVTSI642SDrm_Int:
7214 // AVX-512
7215 case X86::VCVTSI2SSZrr:
7216 case X86::VCVTSI2SSZrm:
7217 case X86::VCVTSI2SSZrr_Int:
7218 case X86::VCVTSI2SSZrrb_Int:
7219 case X86::VCVTSI2SSZrm_Int:
7220 case X86::VCVTSI642SSZrr:
7221 case X86::VCVTSI642SSZrm:
7222 case X86::VCVTSI642SSZrr_Int:
7223 case X86::VCVTSI642SSZrrb_Int:
7224 case X86::VCVTSI642SSZrm_Int:
7225 case X86::VCVTSI2SDZrr:
7226 case X86::VCVTSI2SDZrm:
7227 case X86::VCVTSI2SDZrr_Int:
7228 case X86::VCVTSI2SDZrm_Int:
7229 case X86::VCVTSI642SDZrr:
7230 case X86::VCVTSI642SDZrm:
7231 case X86::VCVTSI642SDZrr_Int:
7232 case X86::VCVTSI642SDZrrb_Int:
7233 case X86::VCVTSI642SDZrm_Int:
7234 case X86::VCVTUSI2SSZrr:
7235 case X86::VCVTUSI2SSZrm:
7236 case X86::VCVTUSI2SSZrr_Int:
7237 case X86::VCVTUSI2SSZrrb_Int:
7238 case X86::VCVTUSI2SSZrm_Int:
7239 case X86::VCVTUSI642SSZrr:
7240 case X86::VCVTUSI642SSZrm:
7241 case X86::VCVTUSI642SSZrr_Int:
7242 case X86::VCVTUSI642SSZrrb_Int:
7243 case X86::VCVTUSI642SSZrm_Int:
7244 case X86::VCVTUSI2SDZrr:
7245 case X86::VCVTUSI2SDZrm:
7246 case X86::VCVTUSI2SDZrr_Int:
7247 case X86::VCVTUSI2SDZrm_Int:
7248 case X86::VCVTUSI642SDZrr:
7249 case X86::VCVTUSI642SDZrm:
7250 case X86::VCVTUSI642SDZrr_Int:
7251 case X86::VCVTUSI642SDZrrb_Int:
7252 case X86::VCVTUSI642SDZrm_Int:
7253 case X86::VCVTSI2SHZrr:
7254 case X86::VCVTSI2SHZrm:
7255 case X86::VCVTSI2SHZrr_Int:
7256 case X86::VCVTSI2SHZrrb_Int:
7257 case X86::VCVTSI2SHZrm_Int:
7258 case X86::VCVTSI642SHZrr:
7259 case X86::VCVTSI642SHZrm:
7260 case X86::VCVTSI642SHZrr_Int:
7261 case X86::VCVTSI642SHZrrb_Int:
7262 case X86::VCVTSI642SHZrm_Int:
7263 case X86::VCVTUSI2SHZrr:
7264 case X86::VCVTUSI2SHZrm:
7265 case X86::VCVTUSI2SHZrr_Int:
7266 case X86::VCVTUSI2SHZrrb_Int:
7267 case X86::VCVTUSI2SHZrm_Int:
7268 case X86::VCVTUSI642SHZrr:
7269 case X86::VCVTUSI642SHZrm:
7270 case X86::VCVTUSI642SHZrr_Int:
7271 case X86::VCVTUSI642SHZrrb_Int:
7272 case X86::VCVTUSI642SHZrm_Int:
7273 // Load folding won't effect the undef register update since the input is
7274 // a GPR.
7275 return OpNum == 1 && !ForLoadFold;
7276 case X86::VCVTSD2SSrr:
7277 case X86::VCVTSD2SSrm:
7278 case X86::VCVTSD2SSrr_Int:
7279 case X86::VCVTSD2SSrm_Int:
7280 case X86::VCVTSS2SDrr:
7281 case X86::VCVTSS2SDrm:
7282 case X86::VCVTSS2SDrr_Int:
7283 case X86::VCVTSS2SDrm_Int:
7284 case X86::VRCPSSr:
7285 case X86::VRCPSSr_Int:
7286 case X86::VRCPSSm:
7287 case X86::VRCPSSm_Int:
7288 case X86::VROUNDSDri:
7289 case X86::VROUNDSDmi:
7290 case X86::VROUNDSDri_Int:
7291 case X86::VROUNDSDmi_Int:
7292 case X86::VROUNDSSri:
7293 case X86::VROUNDSSmi:
7294 case X86::VROUNDSSri_Int:
7295 case X86::VROUNDSSmi_Int:
7296 case X86::VRSQRTSSr:
7297 case X86::VRSQRTSSr_Int:
7298 case X86::VRSQRTSSm:
7299 case X86::VRSQRTSSm_Int:
7300 case X86::VSQRTSSr:
7301 case X86::VSQRTSSr_Int:
7302 case X86::VSQRTSSm:
7303 case X86::VSQRTSSm_Int:
7304 case X86::VSQRTSDr:
7305 case X86::VSQRTSDr_Int:
7306 case X86::VSQRTSDm:
7307 case X86::VSQRTSDm_Int:
7308 // AVX-512
7309 case X86::VCVTSD2SSZrr:
7310 case X86::VCVTSD2SSZrr_Int:
7311 case X86::VCVTSD2SSZrrb_Int:
7312 case X86::VCVTSD2SSZrm:
7313 case X86::VCVTSD2SSZrm_Int:
7314 case X86::VCVTSS2SDZrr:
7315 case X86::VCVTSS2SDZrr_Int:
7316 case X86::VCVTSS2SDZrrb_Int:
7317 case X86::VCVTSS2SDZrm:
7318 case X86::VCVTSS2SDZrm_Int:
7319 case X86::VGETEXPSDZr:
7320 case X86::VGETEXPSDZrb:
7321 case X86::VGETEXPSDZm:
7322 case X86::VGETEXPSSZr:
7323 case X86::VGETEXPSSZrb:
7324 case X86::VGETEXPSSZm:
7325 case X86::VGETMANTSDZrri:
7326 case X86::VGETMANTSDZrrib:
7327 case X86::VGETMANTSDZrmi:
7328 case X86::VGETMANTSSZrri:
7329 case X86::VGETMANTSSZrrib:
7330 case X86::VGETMANTSSZrmi:
7331 case X86::VRNDSCALESDZrri:
7332 case X86::VRNDSCALESDZrri_Int:
7333 case X86::VRNDSCALESDZrrib_Int:
7334 case X86::VRNDSCALESDZrmi:
7335 case X86::VRNDSCALESDZrmi_Int:
7336 case X86::VRNDSCALESSZrri:
7337 case X86::VRNDSCALESSZrri_Int:
7338 case X86::VRNDSCALESSZrrib_Int:
7339 case X86::VRNDSCALESSZrmi:
7340 case X86::VRNDSCALESSZrmi_Int:
7341 case X86::VRCP14SDZrr:
7342 case X86::VRCP14SDZrm:
7343 case X86::VRCP14SSZrr:
7344 case X86::VRCP14SSZrm:
7345 case X86::VRCPSHZrr:
7346 case X86::VRCPSHZrm:
7347 case X86::VRSQRTSHZrr:
7348 case X86::VRSQRTSHZrm:
7349 case X86::VREDUCESHZrmi:
7350 case X86::VREDUCESHZrri:
7351 case X86::VREDUCESHZrrib:
7352 case X86::VGETEXPSHZr:
7353 case X86::VGETEXPSHZrb:
7354 case X86::VGETEXPSHZm:
7355 case X86::VGETMANTSHZrri:
7356 case X86::VGETMANTSHZrrib:
7357 case X86::VGETMANTSHZrmi:
7358 case X86::VRNDSCALESHZrri:
7359 case X86::VRNDSCALESHZrri_Int:
7360 case X86::VRNDSCALESHZrrib_Int:
7361 case X86::VRNDSCALESHZrmi:
7362 case X86::VRNDSCALESHZrmi_Int:
7363 case X86::VSQRTSHZr:
7364 case X86::VSQRTSHZr_Int:
7365 case X86::VSQRTSHZrb_Int:
7366 case X86::VSQRTSHZm:
7367 case X86::VSQRTSHZm_Int:
7368 case X86::VRCP28SDZr:
7369 case X86::VRCP28SDZrb:
7370 case X86::VRCP28SDZm:
7371 case X86::VRCP28SSZr:
7372 case X86::VRCP28SSZrb:
7373 case X86::VRCP28SSZm:
7374 case X86::VREDUCESSZrmi:
7375 case X86::VREDUCESSZrri:
7376 case X86::VREDUCESSZrrib:
7377 case X86::VRSQRT14SDZrr:
7378 case X86::VRSQRT14SDZrm:
7379 case X86::VRSQRT14SSZrr:
7380 case X86::VRSQRT14SSZrm:
7381 case X86::VRSQRT28SDZr:
7382 case X86::VRSQRT28SDZrb:
7383 case X86::VRSQRT28SDZm:
7384 case X86::VRSQRT28SSZr:
7385 case X86::VRSQRT28SSZrb:
7386 case X86::VRSQRT28SSZm:
7387 case X86::VSQRTSSZr:
7388 case X86::VSQRTSSZr_Int:
7389 case X86::VSQRTSSZrb_Int:
7390 case X86::VSQRTSSZm:
7391 case X86::VSQRTSSZm_Int:
7392 case X86::VSQRTSDZr:
7393 case X86::VSQRTSDZr_Int:
7394 case X86::VSQRTSDZrb_Int:
7395 case X86::VSQRTSDZm:
7396 case X86::VSQRTSDZm_Int:
7397 case X86::VCVTSD2SHZrr:
7398 case X86::VCVTSD2SHZrr_Int:
7399 case X86::VCVTSD2SHZrrb_Int:
7400 case X86::VCVTSD2SHZrm:
7401 case X86::VCVTSD2SHZrm_Int:
7402 case X86::VCVTSS2SHZrr:
7403 case X86::VCVTSS2SHZrr_Int:
7404 case X86::VCVTSS2SHZrrb_Int:
7405 case X86::VCVTSS2SHZrm:
7406 case X86::VCVTSS2SHZrm_Int:
7407 case X86::VCVTSH2SDZrr:
7408 case X86::VCVTSH2SDZrr_Int:
7409 case X86::VCVTSH2SDZrrb_Int:
7410 case X86::VCVTSH2SDZrm:
7411 case X86::VCVTSH2SDZrm_Int:
7412 case X86::VCVTSH2SSZrr:
7413 case X86::VCVTSH2SSZrr_Int:
7414 case X86::VCVTSH2SSZrrb_Int:
7415 case X86::VCVTSH2SSZrm:
7416 case X86::VCVTSH2SSZrm_Int:
7417 return OpNum == 1;
7418 case X86::VMOVSSZrrk:
7419 case X86::VMOVSDZrrk:
7420 return OpNum == 3 && !ForLoadFold;
7421 case X86::VMOVSSZrrkz:
7422 case X86::VMOVSDZrrkz:
7423 return OpNum == 2 && !ForLoadFold;
7424 }
7425
7426 return false;
7427}
7428
7429/// Inform the BreakFalseDeps pass how many idle instructions we would like
7430/// before certain undef register reads.
7431///
7432/// This catches the VCVTSI2SD family of instructions:
7433///
7434/// vcvtsi2sdq %rax, undef %xmm0, %xmm14
7435///
7436/// We should to be careful *not* to catch VXOR idioms which are presumably
7437/// handled specially in the pipeline:
7438///
7439/// vxorps undef %xmm1, undef %xmm1, %xmm1
7440///
7441/// Like getPartialRegUpdateClearance, this makes a strong assumption that the
7442/// high bits that are passed-through are not live.
7443unsigned
7445 const TargetRegisterInfo *TRI) const {
7446 const MachineOperand &MO = MI.getOperand(OpNum);
7447 if (MO.getReg().isPhysical() && hasUndefRegUpdate(MI.getOpcode(), OpNum))
7448 return UndefRegClearance;
7449
7450 return 0;
7451}
7452
7454 MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const {
7455 Register Reg = MI.getOperand(OpNum).getReg();
7456 // If MI kills this register, the false dependence is already broken.
7457 if (MI.killsRegister(Reg, TRI))
7458 return;
7459
7460 if (X86::VR128RegClass.contains(Reg)) {
7461 // These instructions are all floating point domain, so xorps is the best
7462 // choice.
7463 unsigned Opc = Subtarget.hasAVX() ? X86::VXORPSrr : X86::XORPSrr;
7464 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(Opc), Reg)
7465 .addReg(Reg, RegState::Undef)
7466 .addReg(Reg, RegState::Undef);
7467 MI.addRegisterKilled(Reg, TRI, true);
7468 } else if (X86::VR256RegClass.contains(Reg)) {
7469 // Use vxorps to clear the full ymm register.
7470 // It wants to read and write the xmm sub-register.
7471 Register XReg = TRI->getSubReg(Reg, X86::sub_xmm);
7472 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(X86::VXORPSrr), XReg)
7473 .addReg(XReg, RegState::Undef)
7474 .addReg(XReg, RegState::Undef)
7476 MI.addRegisterKilled(Reg, TRI, true);
7477 } else if (X86::VR128XRegClass.contains(Reg)) {
7478 // Only handle VLX targets.
7479 if (!Subtarget.hasVLX())
7480 return;
7481 // Since vxorps requires AVX512DQ, vpxord should be the best choice.
7482 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(X86::VPXORDZ128rr), Reg)
7483 .addReg(Reg, RegState::Undef)
7484 .addReg(Reg, RegState::Undef);
7485 MI.addRegisterKilled(Reg, TRI, true);
7486 } else if (X86::VR256XRegClass.contains(Reg) ||
7487 X86::VR512RegClass.contains(Reg)) {
7488 // Only handle VLX targets.
7489 if (!Subtarget.hasVLX())
7490 return;
7491 // Use vpxord to clear the full ymm/zmm register.
7492 // It wants to read and write the xmm sub-register.
7493 Register XReg = TRI->getSubReg(Reg, X86::sub_xmm);
7494 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(X86::VPXORDZ128rr), XReg)
7495 .addReg(XReg, RegState::Undef)
7496 .addReg(XReg, RegState::Undef)
7498 MI.addRegisterKilled(Reg, TRI, true);
7499 } else if (X86::GR64RegClass.contains(Reg)) {
7500 // Using XOR32rr because it has shorter encoding and zeros up the upper bits
7501 // as well.
7502 Register XReg = TRI->getSubReg(Reg, X86::sub_32bit);
7503 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(X86::XOR32rr), XReg)
7504 .addReg(XReg, RegState::Undef)
7505 .addReg(XReg, RegState::Undef)
7507 MI.addRegisterKilled(Reg, TRI, true);
7508 } else if (X86::GR32RegClass.contains(Reg)) {
7509 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(X86::XOR32rr), Reg)
7510 .addReg(Reg, RegState::Undef)
7511 .addReg(Reg, RegState::Undef);
7512 MI.addRegisterKilled(Reg, TRI, true);
7513 } else if ((X86::GR16RegClass.contains(Reg) ||
7514 X86::GR8RegClass.contains(Reg)) &&
7515 X86II::hasNewDataDest(MI.getDesc().TSFlags)) {
7516 // This case is only expected for NDD ops which appear to be partial
7517 // writes, but are not due to the zeroing of the upper part. Here
7518 // we add an implicit def of the superegister, which prevents
7519 // CompressEVEX from converting this to a legacy form.
7520 Register SuperReg = getX86SubSuperRegister(Reg, 64);
7521 MachineInstrBuilder BuildMI(*MI.getParent()->getParent(), &MI);
7522 if (!MI.definesRegister(SuperReg, /*TRI=*/nullptr))
7523 BuildMI.addReg(SuperReg, RegState::ImplicitDefine);
7524 }
7525}
7526
7528 int PtrOffset = 0) {
7529 unsigned NumAddrOps = MOs.size();
7530
7531 if (NumAddrOps < 4) {
7532 // FrameIndex only - add an immediate offset (whether its zero or not).
7533 for (unsigned i = 0; i != NumAddrOps; ++i)
7534 MIB.add(MOs[i]);
7535 addOffset(MIB, PtrOffset);
7536 } else {
7537 // General Memory Addressing - we need to add any offset to an existing
7538 // offset.
7539 assert(MOs.size() == 5 && "Unexpected memory operand list length");
7540 for (unsigned i = 0; i != NumAddrOps; ++i) {
7541 const MachineOperand &MO = MOs[i];
7542 if (i == 3 && PtrOffset != 0) {
7543 MIB.addDisp(MO, PtrOffset);
7544 } else {
7545 MIB.add(MO);
7546 }
7547 }
7548 }
7549}
7550
7552 MachineInstr &NewMI,
7553 const TargetInstrInfo &TII) {
7554 MachineRegisterInfo &MRI = MF.getRegInfo();
7555
7556 for (int Idx : llvm::seq<int>(0, NewMI.getNumOperands())) {
7557 MachineOperand &MO = NewMI.getOperand(Idx);
7558 // We only need to update constraints on virtual register operands.
7559 if (!MO.isReg())
7560 continue;
7561 Register Reg = MO.getReg();
7562 if (!Reg.isVirtual())
7563 continue;
7564
7565 auto *NewRC =
7566 MRI.constrainRegClass(Reg, TII.getRegClass(NewMI.getDesc(), Idx));
7567 if (!NewRC) {
7568 LLVM_DEBUG(
7569 dbgs() << "WARNING: Unable to update register constraint for operand "
7570 << Idx << " of instruction:\n";
7571 NewMI.dump(); dbgs() << "\n");
7572 }
7573 }
7574}
7575
7576static MachineInstr *fuseTwoAddrInst(MachineFunction &MF, unsigned Opcode,
7580 const TargetInstrInfo &TII) {
7581 // Create the base instruction with the memory operand as the first part.
7582 // Omit the implicit operands, something BuildMI can't do.
7583 MachineInstr *NewMI =
7584 MF.CreateMachineInstr(TII.get(Opcode), MI.getDebugLoc(), true);
7585 MachineInstrBuilder MIB(MF, NewMI);
7586 addOperands(MIB, MOs);
7587
7588 // Loop over the rest of the ri operands, converting them over.
7589 unsigned NumOps = MI.getDesc().getNumOperands() - 2;
7590 for (unsigned i = 0; i != NumOps; ++i) {
7591 MachineOperand &MO = MI.getOperand(i + 2);
7592 MIB.add(MO);
7593 }
7594 for (const MachineOperand &MO : llvm::drop_begin(MI.operands(), NumOps + 2))
7595 MIB.add(MO);
7596
7597 updateOperandRegConstraints(MF, *NewMI, TII);
7598
7599 MachineBasicBlock *MBB = InsertPt->getParent();
7600 MBB->insert(InsertPt, NewMI);
7601
7602 return MIB;
7603}
7604
7605static MachineInstr *fuseInst(MachineFunction &MF, unsigned Opcode,
7606 unsigned OpNo, ArrayRef<MachineOperand> MOs,
7609 int PtrOffset = 0) {
7610 // Omit the implicit operands, something BuildMI can't do.
7611 MachineInstr *NewMI =
7612 MF.CreateMachineInstr(TII.get(Opcode), MI.getDebugLoc(), true);
7613 MachineInstrBuilder MIB(MF, NewMI);
7614
7615 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
7616 MachineOperand &MO = MI.getOperand(i);
7617 if (i == OpNo) {
7618 assert(MO.isReg() && "Expected to fold into reg operand!");
7619 addOperands(MIB, MOs, PtrOffset);
7620 } else {
7621 MIB.add(MO);
7622 }
7623 }
7624
7625 updateOperandRegConstraints(MF, *NewMI, TII);
7626
7627 // Copy the NoFPExcept flag from the instruction we're fusing.
7630
7631 MachineBasicBlock *MBB = InsertPt->getParent();
7632 MBB->insert(InsertPt, NewMI);
7633
7634 return MIB;
7635}
7636
7637static MachineInstr *makeM0Inst(const TargetInstrInfo &TII, unsigned Opcode,
7640 MachineInstr &MI) {
7641 MachineInstrBuilder MIB = BuildMI(*InsertPt->getParent(), InsertPt,
7642 MI.getDebugLoc(), TII.get(Opcode));
7643 addOperands(MIB, MOs);
7644 return MIB.addImm(0);
7645}
7646
7647MachineInstr *X86InstrInfo::foldMemoryOperandCustom(
7648 MachineFunction &MF, MachineInstr &MI, unsigned OpNum,
7650 unsigned Size, Align Alignment) const {
7651 switch (MI.getOpcode()) {
7652 case X86::INSERTPSrri:
7653 case X86::VINSERTPSrri:
7654 case X86::VINSERTPSZrri:
7655 // Attempt to convert the load of inserted vector into a fold load
7656 // of a single float.
7657 if (OpNum == 2) {
7658 unsigned Imm = MI.getOperand(MI.getNumOperands() - 1).getImm();
7659 unsigned ZMask = Imm & 15;
7660 unsigned DstIdx = (Imm >> 4) & 3;
7661 unsigned SrcIdx = (Imm >> 6) & 3;
7662
7663 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
7664 const TargetRegisterClass *RC = getRegClass(MI.getDesc(), OpNum);
7665 unsigned RCSize = TRI.getRegSizeInBits(*RC) / 8;
7666 if ((Size == 0 || Size >= 16) && RCSize >= 16 &&
7667 (MI.getOpcode() != X86::INSERTPSrri || Alignment >= Align(4))) {
7668 int PtrOffset = SrcIdx * 4;
7669 unsigned NewImm = (DstIdx << 4) | ZMask;
7670 unsigned NewOpCode =
7671 (MI.getOpcode() == X86::VINSERTPSZrri) ? X86::VINSERTPSZrmi
7672 : (MI.getOpcode() == X86::VINSERTPSrri) ? X86::VINSERTPSrmi
7673 : X86::INSERTPSrmi;
7674 MachineInstr *NewMI =
7675 fuseInst(MF, NewOpCode, OpNum, MOs, InsertPt, MI, *this, PtrOffset);
7676 NewMI->getOperand(NewMI->getNumOperands() - 1).setImm(NewImm);
7677 return NewMI;
7678 }
7679 }
7680 break;
7681 case X86::MOVHLPSrr:
7682 case X86::VMOVHLPSrr:
7683 case X86::VMOVHLPSZrr:
7684 // Move the upper 64-bits of the second operand to the lower 64-bits.
7685 // To fold the load, adjust the pointer to the upper and use (V)MOVLPS.
7686 // TODO: In most cases AVX doesn't have a 8-byte alignment requirement.
7687 if (OpNum == 2) {
7688 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
7689 const TargetRegisterClass *RC = getRegClass(MI.getDesc(), OpNum);
7690 unsigned RCSize = TRI.getRegSizeInBits(*RC) / 8;
7691 if ((Size == 0 || Size >= 16) && RCSize >= 16 && Alignment >= Align(8)) {
7692 unsigned NewOpCode =
7693 (MI.getOpcode() == X86::VMOVHLPSZrr) ? X86::VMOVLPSZ128rm
7694 : (MI.getOpcode() == X86::VMOVHLPSrr) ? X86::VMOVLPSrm
7695 : X86::MOVLPSrm;
7696 MachineInstr *NewMI =
7697 fuseInst(MF, NewOpCode, OpNum, MOs, InsertPt, MI, *this, 8);
7698 return NewMI;
7699 }
7700 }
7701 break;
7702 case X86::UNPCKLPDrr:
7703 // If we won't be able to fold this to the memory form of UNPCKL, use
7704 // MOVHPD instead. Done as custom because we can't have this in the load
7705 // table twice.
7706 if (OpNum == 2) {
7707 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
7708 const TargetRegisterClass *RC = getRegClass(MI.getDesc(), OpNum);
7709 unsigned RCSize = TRI.getRegSizeInBits(*RC) / 8;
7710 if ((Size == 0 || Size >= 16) && RCSize >= 16 && Alignment < Align(16)) {
7711 MachineInstr *NewMI =
7712 fuseInst(MF, X86::MOVHPDrm, OpNum, MOs, InsertPt, MI, *this);
7713 return NewMI;
7714 }
7715 }
7716 break;
7717 case X86::MOV32r0:
7718 if (auto *NewMI =
7719 makeM0Inst(*this, (Size == 4) ? X86::MOV32mi : X86::MOV64mi32, MOs,
7720 InsertPt, MI))
7721 return NewMI;
7722 break;
7723 }
7724
7725 return nullptr;
7726}
7727
7729 MachineInstr &MI) {
7730 if (!hasUndefRegUpdate(MI.getOpcode(), 1, /*ForLoadFold*/ true) ||
7731 !MI.getOperand(1).isReg())
7732 return false;
7733
7734 // The are two cases we need to handle depending on where in the pipeline
7735 // the folding attempt is being made.
7736 // -Register has the undef flag set.
7737 // -Register is produced by the IMPLICIT_DEF instruction.
7738
7739 if (MI.getOperand(1).isUndef())
7740 return true;
7741
7743 MachineInstr *VRegDef = RegInfo.getUniqueVRegDef(MI.getOperand(1).getReg());
7744 return VRegDef && VRegDef->isImplicitDef();
7745}
7746
7747unsigned X86InstrInfo::commuteOperandsForFold(MachineInstr &MI,
7748 unsigned Idx1) const {
7749 unsigned Idx2 = CommuteAnyOperandIndex;
7750 if (!findCommutedOpIndices(MI, Idx1, Idx2))
7751 return Idx1;
7752
7753 bool HasDef = MI.getDesc().getNumDefs();
7754 Register Reg0 = HasDef ? MI.getOperand(0).getReg() : Register();
7755 Register Reg1 = MI.getOperand(Idx1).getReg();
7756 Register Reg2 = MI.getOperand(Idx2).getReg();
7757 bool Tied1 = 0 == MI.getDesc().getOperandConstraint(Idx1, MCOI::TIED_TO);
7758 bool Tied2 = 0 == MI.getDesc().getOperandConstraint(Idx2, MCOI::TIED_TO);
7759
7760 // If either of the commutable operands are tied to the destination
7761 // then we can not commute + fold.
7762 if ((HasDef && Reg0 == Reg1 && Tied1) || (HasDef && Reg0 == Reg2 && Tied2))
7763 return Idx1;
7764
7765 return commuteInstruction(MI, false, Idx1, Idx2) ? Idx2 : Idx1;
7766}
7767
7768static void printFailMsgforFold(const MachineInstr &MI, unsigned Idx) {
7769 if (PrintFailedFusing && !MI.isCopy())
7770 dbgs() << "We failed to fuse operand " << Idx << " in " << MI;
7771}
7772
7774 MachineFunction &MF, MachineInstr &MI, unsigned OpNum,
7776 unsigned Size, Align Alignment, bool AllowCommute, MachineInstr *&CopyMI,
7777 VirtRegMap *VRM) const {
7778 bool isSlowTwoMemOps = Subtarget.slowTwoMemOps();
7779 bool isSlowIndirectCall = Subtarget.slowIndirectCall();
7780 unsigned Opc = MI.getOpcode();
7781
7782 // For CPUs that favor the register form of a call,
7783 // do not fold loads into calls, unless optimizing for size aggressively.
7784 if ((isSlowTwoMemOps || isSlowIndirectCall) &&
7785 !MF.getFunction().hasMinSize() &&
7786 (Opc == X86::CALL32r || Opc == X86::CALL64r ||
7787 Opc == X86::CALL64r_ImpCall))
7788 return nullptr;
7789
7790 // For CPUs that favor the register form of a push,
7791 // do not fold loads into pushes, unless optimizing for size aggressively.
7792 if (isSlowTwoMemOps && !MF.getFunction().hasMinSize() &&
7793 (Opc == X86::PUSH16r || Opc == X86::PUSH32r || Opc == X86::PUSH64r))
7794 return nullptr;
7795
7796 // Avoid partial and undef register update stalls unless optimizing for size.
7797 if (!MF.getFunction().hasOptSize() &&
7798 (hasPartialRegUpdate(Opc, Subtarget, /*ForLoadFold*/ true) ||
7800 return nullptr;
7801
7802 unsigned NumOps = MI.getDesc().getNumOperands();
7803 bool IsTwoAddr = NumOps > 1 && OpNum < 2 && MI.getOperand(0).isReg() &&
7804 MI.getOperand(1).isReg() &&
7805 MI.getOperand(0).getReg() == MI.getOperand(1).getReg();
7806
7807 // FIXME: AsmPrinter doesn't know how to handle
7808 // X86II::MO_GOT_ABSOLUTE_ADDRESS after folding.
7809 if (Opc == X86::ADD32ri &&
7810 MI.getOperand(2).getTargetFlags() == X86II::MO_GOT_ABSOLUTE_ADDRESS)
7811 return nullptr;
7812
7813 // GOTTPOFF relocation loads can only be folded into add instructions.
7814 // FIXME: Need to exclude other relocations that only support specific
7815 // instructions.
7816 if (MOs.size() == X86::AddrNumOperands &&
7817 MOs[X86::AddrDisp].getTargetFlags() == X86II::MO_GOTTPOFF &&
7818 Opc != X86::ADD64rr)
7819 return nullptr;
7820
7821 // Don't fold loads into indirect calls that need a KCFI check as we'll
7822 // have to unfold these in X86TargetLowering::EmitKCFICheck anyway.
7823 if (MI.isCall() && MI.getCFIType())
7824 return nullptr;
7825
7826 // Attempt to fold any custom cases we have.
7827 if (auto *CustomMI = foldMemoryOperandCustom(MF, MI, OpNum, MOs, InsertPt,
7828 Size, Alignment))
7829 return CustomMI;
7830
7831 // Folding a memory location into the two-address part of a two-address
7832 // instruction is different than folding it other places. It requires
7833 // replacing the *two* registers with the memory location.
7834 //
7835 // Utilize the mapping NonNDD -> RMW for the NDD variant.
7836 unsigned NonNDOpc = Subtarget.hasNDD() ? X86::getNonNDVariant(Opc) : 0U;
7837 // Utilize the mapping NonNDD if NDD memory variant is not preferred.
7838 bool NoNDDM = NonNDOpc && !Subtarget.hasNDDM();
7839
7840 MachineRegisterInfo &MRI = MF.getRegInfo();
7841 if (NoNDDM && !IsTwoAddr && !MRI.isSSA()) {
7842 // Bail out if dst has subreg. It happens during register-coalescer from
7843 // 704B %19:gr32 = SUB32rr_ND killed %0:gr32, killed %7:gr32, ...
7844 // 752B undef %23.sub_32bit:gr64 = COPY killed %19:gr32
7845 // 768B %25:gr32 = LEA64_32r killed %23:gr64, 1, killed %21:gr64_nosp, ...
7846 // to
7847 // 704B undef %23.sub_32bit:gr64_with_sub_8bit = SUB32rr_ND %0:gr32, ...
7848 // 768B %25:gr32 = LEA64_32r %23:gr64_with_sub_8bit, 1, %21:gr64_nosp, ...
7849 // Machine verifier fails if we try to tie %23 to the source.
7850 if (MI.getOperand(0).getSubReg())
7851 return nullptr;
7852
7853 // Bail out if dst has been assigned a physical register. Otherwise, we
7854 // cannot update LiveRegMatrix properly.
7855 Register Dst = MI.getOperand(0).getReg();
7856 if (VRM && Dst != MI.getOperand(1).getReg() &&
7857 (!Dst.isVirtual() || VRM->getPhys(Dst)))
7858 return nullptr;
7859 }
7860
7861 const X86FoldTableEntry *I =
7862 IsTwoAddr ? lookupTwoAddrFoldTable(NonNDOpc ? NonNDOpc : Opc)
7863 : lookupFoldTable(NoNDDM ? NonNDOpc : Opc, OpNum);
7864
7865 MachineInstr *NewMI = nullptr;
7866 if (I) {
7867 unsigned Opcode = I->DstOp;
7868 if (Alignment <
7869 Align(1ULL << ((I->Flags & TB_ALIGN_MASK) >> TB_ALIGN_SHIFT)))
7870 return nullptr;
7871 bool NarrowToMOV32rm = false;
7872 if (Size) {
7874 const TargetRegisterClass *RC = getRegClass(MI.getDesc(), OpNum);
7875 unsigned RCSize = TRI.getRegSizeInBits(*RC) / 8;
7876 // Check if it's safe to fold the load. If the size of the object is
7877 // narrower than the load width, then it's not.
7878 // FIXME: Allow scalar intrinsic instructions like ADDSSrm_Int.
7879 if ((I->Flags & TB_FOLDED_LOAD) && Size < RCSize) {
7880 // If this is a 64-bit load, but the spill slot is 32, then we can do
7881 // a 32-bit load which is implicitly zero-extended. This likely is
7882 // due to live interval analysis remat'ing a load from stack slot.
7883 if (Opcode != X86::MOV64rm || RCSize != 8 || Size != 4)
7884 return nullptr;
7885 if (MI.getOperand(0).getSubReg() || MI.getOperand(1).getSubReg())
7886 return nullptr;
7887 Opcode = X86::MOV32rm;
7888 NarrowToMOV32rm = true;
7889 }
7890 // For stores, make sure the size of the object is equal to the size of
7891 // the store. If the object is larger, the extra bits would be garbage. If
7892 // the object is smaller we might overwrite another object or fault.
7893 if ((I->Flags & TB_FOLDED_STORE) && Size != RCSize)
7894 return nullptr;
7895 }
7896
7897 NewMI = IsTwoAddr ? fuseTwoAddrInst(MF, Opcode, MOs, InsertPt, MI, *this)
7898 : fuseInst(MF, Opcode, OpNum, MOs, InsertPt, MI, *this);
7899
7900 if (NarrowToMOV32rm) {
7901 // If this is the special case where we use a MOV32rm to load a 32-bit
7902 // value and zero-extend the top bits. Change the destination register
7903 // to a 32-bit one.
7904 Register DstReg = NewMI->getOperand(0).getReg();
7905 if (DstReg.isPhysical())
7906 NewMI->getOperand(0).setReg(RI.getSubReg(DstReg, X86::sub_32bit));
7907 else
7908 NewMI->getOperand(0).setSubReg(X86::sub_32bit);
7909 }
7910
7911 if (NoNDDM && !IsTwoAddr) {
7912 Register SrcReg = MI.getOperand(1).getReg();
7913 unsigned SrcSub = MI.getOperand(1).getSubReg();
7914 if (MI.killsRegister(SrcReg, /*TRI=*/nullptr) ||
7915 MI.getOperand(0).getReg() == SrcReg)
7916 return NewMI;
7917
7918 Register NewSrc = MI.getOperand(0).getReg();
7919 if (MRI.isSSA())
7920 NewSrc = MRI.createVirtualRegister(getRegClass(NewMI->getDesc(), 1));
7921
7922 CopyMI = BuildMI(*NewMI->getParent(), *NewMI, MI.getDebugLoc(),
7923 get(TargetOpcode::COPY))
7924 .addDef(NewSrc)
7925 .addReg(SrcReg, {}, SrcSub);
7926 NewMI->getOperand(1).setReg(NewSrc);
7927 NewMI->getOperand(1).setSubReg(0);
7928 }
7929 return NewMI;
7930 }
7931
7932 if (AllowCommute) {
7933 // If the instruction and target operand are commutable, commute the
7934 // instruction and try again.
7935 unsigned CommuteOpIdx2 = commuteOperandsForFold(MI, OpNum);
7936 if (CommuteOpIdx2 == OpNum) {
7937 printFailMsgforFold(MI, OpNum);
7938 return nullptr;
7939 }
7940 // Attempt to fold with the commuted version of the instruction.
7941 NewMI = foldMemoryOperandImpl(MF, MI, CommuteOpIdx2, MOs, InsertPt, Size,
7942 Alignment, /*AllowCommute=*/false, CopyMI);
7943 if (NewMI)
7944 return NewMI;
7945 // Folding failed again - undo the commute before returning.
7946 commuteInstruction(MI, false, OpNum, CommuteOpIdx2);
7947 }
7948
7949 printFailMsgforFold(MI, OpNum);
7950 return nullptr;
7951}
7952
7955 ArrayRef<unsigned> Ops, int FrameIndex,
7956 MachineInstr *&CopyMI, LiveIntervals *LIS,
7957 VirtRegMap *VRM) const {
7959 // Check switch flag
7960 if (NoFusing)
7961 return nullptr;
7962
7963 // Avoid partial and undef register update stalls unless optimizing for size.
7964 if (!MF.getFunction().hasOptSize() &&
7965 (hasPartialRegUpdate(MI.getOpcode(), Subtarget, /*ForLoadFold*/ true) ||
7967 return nullptr;
7968
7969 // Don't fold subreg spills, or reloads that use a high subreg.
7970 for (auto Op : Ops) {
7971 MachineOperand &MO = MI.getOperand(Op);
7972 auto SubReg = MO.getSubReg();
7973 // MOV32r0 is special b/c it's used to clear a 64-bit register too.
7974 // (See patterns for MOV32r0 in TD files).
7975 if (MI.getOpcode() == X86::MOV32r0 && SubReg == X86::sub_32bit)
7976 continue;
7977 if (SubReg && (MO.isDef() || SubReg == X86::sub_8bit_hi))
7978 return nullptr;
7979 }
7980
7981 const MachineFrameInfo &MFI = MF.getFrameInfo();
7982 unsigned Size = MFI.getObjectSize(FrameIndex);
7983 Align Alignment = MFI.getObjectAlign(FrameIndex);
7984 // If the function stack isn't realigned we don't want to fold instructions
7985 // that need increased alignment.
7986 if (!RI.hasStackRealignment(MF))
7987 Alignment =
7988 std::min(Alignment, Subtarget.getFrameLowering()->getStackAlign());
7989
7990 auto Impl = [&]() {
7991 return foldMemoryOperandImpl(
7992 MF, MI, Ops[0], MachineOperand::CreateFI(FrameIndex), InsertPt, Size,
7993 Alignment, /*AllowCommute=*/true, CopyMI, VRM);
7994 };
7995 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
7996 unsigned NewOpc = 0;
7997 unsigned RCSize = 0;
7998 unsigned Opc = MI.getOpcode();
7999 switch (Opc) {
8000 default:
8001 // NDD can be folded into RMW though its Op0 and Op1 are not tied.
8002 return (Subtarget.hasNDD() ? X86::getNonNDVariant(Opc) : 0U) ? Impl()
8003 : nullptr;
8004 case X86::TEST8rr:
8005 NewOpc = X86::CMP8ri;
8006 RCSize = 1;
8007 break;
8008 case X86::TEST16rr:
8009 NewOpc = X86::CMP16ri;
8010 RCSize = 2;
8011 break;
8012 case X86::TEST32rr:
8013 NewOpc = X86::CMP32ri;
8014 RCSize = 4;
8015 break;
8016 case X86::TEST64rr:
8017 NewOpc = X86::CMP64ri32;
8018 RCSize = 8;
8019 break;
8020 }
8021 // Check if it's safe to fold the load. If the size of the object is
8022 // narrower than the load width, then it's not.
8023 if (Size < RCSize)
8024 return nullptr;
8025 // Change to CMPXXri r, 0 first.
8026 MI.setDesc(get(NewOpc));
8027 MI.getOperand(1).ChangeToImmediate(0);
8028 } else if (Ops.size() != 1)
8029 return nullptr;
8030
8031 return Impl();
8032}
8033
8034/// Check if \p LoadMI is a partial register load that we can't fold into \p MI
8035/// because the latter uses contents that wouldn't be defined in the folded
8036/// version. For instance, this transformation isn't legal:
8037/// movss (%rdi), %xmm0
8038/// addps %xmm0, %xmm0
8039/// ->
8040/// addps (%rdi), %xmm0
8041///
8042/// But this one is:
8043/// movss (%rdi), %xmm0
8044/// addss %xmm0, %xmm0
8045/// ->
8046/// addss (%rdi), %xmm0
8047///
8049 const MachineInstr &UserMI,
8050 const MachineFunction &MF) {
8051 unsigned Opc = LoadMI.getOpcode();
8052 unsigned UserOpc = UserMI.getOpcode();
8054 const TargetRegisterClass *RC =
8055 MF.getRegInfo().getRegClass(LoadMI.getOperand(0).getReg());
8056 unsigned RegSize = TRI.getRegSizeInBits(*RC);
8057
8058 if ((Opc == X86::MOVSSrm || Opc == X86::VMOVSSrm || Opc == X86::VMOVSSZrm ||
8059 Opc == X86::MOVSSrm_alt || Opc == X86::VMOVSSrm_alt ||
8060 Opc == X86::VMOVSSZrm_alt) &&
8061 RegSize > 32) {
8062 // These instructions only load 32 bits, we can't fold them if the
8063 // destination register is wider than 32 bits (4 bytes), and its user
8064 // instruction isn't scalar (SS).
8065 switch (UserOpc) {
8066 case X86::CVTSS2SDrr_Int:
8067 case X86::VCVTSS2SDrr_Int:
8068 case X86::VCVTSS2SDZrr_Int:
8069 case X86::VCVTSS2SDZrrk_Int:
8070 case X86::VCVTSS2SDZrrkz_Int:
8071 case X86::CVTSS2SIrr_Int:
8072 case X86::CVTSS2SI64rr_Int:
8073 case X86::VCVTSS2SIrr_Int:
8074 case X86::VCVTSS2SI64rr_Int:
8075 case X86::VCVTSS2SIZrr_Int:
8076 case X86::VCVTSS2SI64Zrr_Int:
8077 case X86::CVTTSS2SIrr_Int:
8078 case X86::CVTTSS2SI64rr_Int:
8079 case X86::VCVTTSS2SIrr_Int:
8080 case X86::VCVTTSS2SI64rr_Int:
8081 case X86::VCVTTSS2SIZrr_Int:
8082 case X86::VCVTTSS2SI64Zrr_Int:
8083 case X86::VCVTSS2USIZrr_Int:
8084 case X86::VCVTSS2USI64Zrr_Int:
8085 case X86::VCVTTSS2USIZrr_Int:
8086 case X86::VCVTTSS2USI64Zrr_Int:
8087 case X86::RCPSSr_Int:
8088 case X86::VRCPSSr_Int:
8089 case X86::RSQRTSSr_Int:
8090 case X86::VRSQRTSSr_Int:
8091 case X86::ROUNDSSri_Int:
8092 case X86::VROUNDSSri_Int:
8093 case X86::COMISSrr_Int:
8094 case X86::VCOMISSrr_Int:
8095 case X86::VCOMISSZrr_Int:
8096 case X86::UCOMISSrr_Int:
8097 case X86::VUCOMISSrr_Int:
8098 case X86::VUCOMISSZrr_Int:
8099 case X86::ADDSSrr_Int:
8100 case X86::VADDSSrr_Int:
8101 case X86::VADDSSZrr_Int:
8102 case X86::CMPSSrri_Int:
8103 case X86::VCMPSSrri_Int:
8104 case X86::VCMPSSZrri_Int:
8105 case X86::DIVSSrr_Int:
8106 case X86::VDIVSSrr_Int:
8107 case X86::VDIVSSZrr_Int:
8108 case X86::MAXSSrr_Int:
8109 case X86::VMAXSSrr_Int:
8110 case X86::VMAXSSZrr_Int:
8111 case X86::MINSSrr_Int:
8112 case X86::VMINSSrr_Int:
8113 case X86::VMINSSZrr_Int:
8114 case X86::MULSSrr_Int:
8115 case X86::VMULSSrr_Int:
8116 case X86::VMULSSZrr_Int:
8117 case X86::SQRTSSr_Int:
8118 case X86::VSQRTSSr_Int:
8119 case X86::VSQRTSSZr_Int:
8120 case X86::SUBSSrr_Int:
8121 case X86::VSUBSSrr_Int:
8122 case X86::VSUBSSZrr_Int:
8123 case X86::VADDSSZrrk_Int:
8124 case X86::VADDSSZrrkz_Int:
8125 case X86::VCMPSSZrrik_Int:
8126 case X86::VDIVSSZrrk_Int:
8127 case X86::VDIVSSZrrkz_Int:
8128 case X86::VMAXSSZrrk_Int:
8129 case X86::VMAXSSZrrkz_Int:
8130 case X86::VMINSSZrrk_Int:
8131 case X86::VMINSSZrrkz_Int:
8132 case X86::VMULSSZrrk_Int:
8133 case X86::VMULSSZrrkz_Int:
8134 case X86::VSQRTSSZrk_Int:
8135 case X86::VSQRTSSZrkz_Int:
8136 case X86::VSUBSSZrrk_Int:
8137 case X86::VSUBSSZrrkz_Int:
8138 case X86::VFMADDSS4rr_Int:
8139 case X86::VFNMADDSS4rr_Int:
8140 case X86::VFMSUBSS4rr_Int:
8141 case X86::VFNMSUBSS4rr_Int:
8142 case X86::VFMADD132SSr_Int:
8143 case X86::VFNMADD132SSr_Int:
8144 case X86::VFMADD213SSr_Int:
8145 case X86::VFNMADD213SSr_Int:
8146 case X86::VFMADD231SSr_Int:
8147 case X86::VFNMADD231SSr_Int:
8148 case X86::VFMSUB132SSr_Int:
8149 case X86::VFNMSUB132SSr_Int:
8150 case X86::VFMSUB213SSr_Int:
8151 case X86::VFNMSUB213SSr_Int:
8152 case X86::VFMSUB231SSr_Int:
8153 case X86::VFNMSUB231SSr_Int:
8154 case X86::VFMADD132SSZr_Int:
8155 case X86::VFNMADD132SSZr_Int:
8156 case X86::VFMADD213SSZr_Int:
8157 case X86::VFNMADD213SSZr_Int:
8158 case X86::VFMADD231SSZr_Int:
8159 case X86::VFNMADD231SSZr_Int:
8160 case X86::VFMSUB132SSZr_Int:
8161 case X86::VFNMSUB132SSZr_Int:
8162 case X86::VFMSUB213SSZr_Int:
8163 case X86::VFNMSUB213SSZr_Int:
8164 case X86::VFMSUB231SSZr_Int:
8165 case X86::VFNMSUB231SSZr_Int:
8166 case X86::VFMADD132SSZrk_Int:
8167 case X86::VFNMADD132SSZrk_Int:
8168 case X86::VFMADD213SSZrk_Int:
8169 case X86::VFNMADD213SSZrk_Int:
8170 case X86::VFMADD231SSZrk_Int:
8171 case X86::VFNMADD231SSZrk_Int:
8172 case X86::VFMSUB132SSZrk_Int:
8173 case X86::VFNMSUB132SSZrk_Int:
8174 case X86::VFMSUB213SSZrk_Int:
8175 case X86::VFNMSUB213SSZrk_Int:
8176 case X86::VFMSUB231SSZrk_Int:
8177 case X86::VFNMSUB231SSZrk_Int:
8178 case X86::VFMADD132SSZrkz_Int:
8179 case X86::VFNMADD132SSZrkz_Int:
8180 case X86::VFMADD213SSZrkz_Int:
8181 case X86::VFNMADD213SSZrkz_Int:
8182 case X86::VFMADD231SSZrkz_Int:
8183 case X86::VFNMADD231SSZrkz_Int:
8184 case X86::VFMSUB132SSZrkz_Int:
8185 case X86::VFNMSUB132SSZrkz_Int:
8186 case X86::VFMSUB213SSZrkz_Int:
8187 case X86::VFNMSUB213SSZrkz_Int:
8188 case X86::VFMSUB231SSZrkz_Int:
8189 case X86::VFNMSUB231SSZrkz_Int:
8190 case X86::VFIXUPIMMSSZrri:
8191 case X86::VFIXUPIMMSSZrrik:
8192 case X86::VFIXUPIMMSSZrrikz:
8193 case X86::VFPCLASSSSZri:
8194 case X86::VFPCLASSSSZrik:
8195 case X86::VGETEXPSSZr:
8196 case X86::VGETEXPSSZrk:
8197 case X86::VGETEXPSSZrkz:
8198 case X86::VGETMANTSSZrri:
8199 case X86::VGETMANTSSZrrik:
8200 case X86::VGETMANTSSZrrikz:
8201 case X86::VRANGESSZrri:
8202 case X86::VRANGESSZrrik:
8203 case X86::VRANGESSZrrikz:
8204 case X86::VRCP14SSZrr:
8205 case X86::VRCP14SSZrrk:
8206 case X86::VRCP14SSZrrkz:
8207 case X86::VRCP28SSZr:
8208 case X86::VRCP28SSZrk:
8209 case X86::VRCP28SSZrkz:
8210 case X86::VREDUCESSZrri:
8211 case X86::VREDUCESSZrrik:
8212 case X86::VREDUCESSZrrikz:
8213 case X86::VRNDSCALESSZrri_Int:
8214 case X86::VRNDSCALESSZrrik_Int:
8215 case X86::VRNDSCALESSZrrikz_Int:
8216 case X86::VRSQRT14SSZrr:
8217 case X86::VRSQRT14SSZrrk:
8218 case X86::VRSQRT14SSZrrkz:
8219 case X86::VRSQRT28SSZr:
8220 case X86::VRSQRT28SSZrk:
8221 case X86::VRSQRT28SSZrkz:
8222 case X86::VSCALEFSSZrr:
8223 case X86::VSCALEFSSZrrk:
8224 case X86::VSCALEFSSZrrkz:
8225 return false;
8226 default:
8227 return true;
8228 }
8229 }
8230
8231 if ((Opc == X86::MOVSDrm || Opc == X86::VMOVSDrm || Opc == X86::VMOVSDZrm ||
8232 Opc == X86::MOVSDrm_alt || Opc == X86::VMOVSDrm_alt ||
8233 Opc == X86::VMOVSDZrm_alt) &&
8234 RegSize > 64) {
8235 // These instructions only load 64 bits, we can't fold them if the
8236 // destination register is wider than 64 bits (8 bytes), and its user
8237 // instruction isn't scalar (SD).
8238 switch (UserOpc) {
8239 case X86::CVTSD2SSrr_Int:
8240 case X86::VCVTSD2SSrr_Int:
8241 case X86::VCVTSD2SSZrr_Int:
8242 case X86::VCVTSD2SSZrrk_Int:
8243 case X86::VCVTSD2SSZrrkz_Int:
8244 case X86::CVTSD2SIrr_Int:
8245 case X86::CVTSD2SI64rr_Int:
8246 case X86::VCVTSD2SIrr_Int:
8247 case X86::VCVTSD2SI64rr_Int:
8248 case X86::VCVTSD2SIZrr_Int:
8249 case X86::VCVTSD2SI64Zrr_Int:
8250 case X86::CVTTSD2SIrr_Int:
8251 case X86::CVTTSD2SI64rr_Int:
8252 case X86::VCVTTSD2SIrr_Int:
8253 case X86::VCVTTSD2SI64rr_Int:
8254 case X86::VCVTTSD2SIZrr_Int:
8255 case X86::VCVTTSD2SI64Zrr_Int:
8256 case X86::VCVTSD2USIZrr_Int:
8257 case X86::VCVTSD2USI64Zrr_Int:
8258 case X86::VCVTTSD2USIZrr_Int:
8259 case X86::VCVTTSD2USI64Zrr_Int:
8260 case X86::ROUNDSDri_Int:
8261 case X86::VROUNDSDri_Int:
8262 case X86::COMISDrr_Int:
8263 case X86::VCOMISDrr_Int:
8264 case X86::VCOMISDZrr_Int:
8265 case X86::UCOMISDrr_Int:
8266 case X86::VUCOMISDrr_Int:
8267 case X86::VUCOMISDZrr_Int:
8268 case X86::ADDSDrr_Int:
8269 case X86::VADDSDrr_Int:
8270 case X86::VADDSDZrr_Int:
8271 case X86::CMPSDrri_Int:
8272 case X86::VCMPSDrri_Int:
8273 case X86::VCMPSDZrri_Int:
8274 case X86::DIVSDrr_Int:
8275 case X86::VDIVSDrr_Int:
8276 case X86::VDIVSDZrr_Int:
8277 case X86::MAXSDrr_Int:
8278 case X86::VMAXSDrr_Int:
8279 case X86::VMAXSDZrr_Int:
8280 case X86::MINSDrr_Int:
8281 case X86::VMINSDrr_Int:
8282 case X86::VMINSDZrr_Int:
8283 case X86::MULSDrr_Int:
8284 case X86::VMULSDrr_Int:
8285 case X86::VMULSDZrr_Int:
8286 case X86::SQRTSDr_Int:
8287 case X86::VSQRTSDr_Int:
8288 case X86::VSQRTSDZr_Int:
8289 case X86::SUBSDrr_Int:
8290 case X86::VSUBSDrr_Int:
8291 case X86::VSUBSDZrr_Int:
8292 case X86::VADDSDZrrk_Int:
8293 case X86::VADDSDZrrkz_Int:
8294 case X86::VCMPSDZrrik_Int:
8295 case X86::VDIVSDZrrk_Int:
8296 case X86::VDIVSDZrrkz_Int:
8297 case X86::VMAXSDZrrk_Int:
8298 case X86::VMAXSDZrrkz_Int:
8299 case X86::VMINSDZrrk_Int:
8300 case X86::VMINSDZrrkz_Int:
8301 case X86::VMULSDZrrk_Int:
8302 case X86::VMULSDZrrkz_Int:
8303 case X86::VSQRTSDZrk_Int:
8304 case X86::VSQRTSDZrkz_Int:
8305 case X86::VSUBSDZrrk_Int:
8306 case X86::VSUBSDZrrkz_Int:
8307 case X86::VFMADDSD4rr_Int:
8308 case X86::VFNMADDSD4rr_Int:
8309 case X86::VFMSUBSD4rr_Int:
8310 case X86::VFNMSUBSD4rr_Int:
8311 case X86::VFMADD132SDr_Int:
8312 case X86::VFNMADD132SDr_Int:
8313 case X86::VFMADD213SDr_Int:
8314 case X86::VFNMADD213SDr_Int:
8315 case X86::VFMADD231SDr_Int:
8316 case X86::VFNMADD231SDr_Int:
8317 case X86::VFMSUB132SDr_Int:
8318 case X86::VFNMSUB132SDr_Int:
8319 case X86::VFMSUB213SDr_Int:
8320 case X86::VFNMSUB213SDr_Int:
8321 case X86::VFMSUB231SDr_Int:
8322 case X86::VFNMSUB231SDr_Int:
8323 case X86::VFMADD132SDZr_Int:
8324 case X86::VFNMADD132SDZr_Int:
8325 case X86::VFMADD213SDZr_Int:
8326 case X86::VFNMADD213SDZr_Int:
8327 case X86::VFMADD231SDZr_Int:
8328 case X86::VFNMADD231SDZr_Int:
8329 case X86::VFMSUB132SDZr_Int:
8330 case X86::VFNMSUB132SDZr_Int:
8331 case X86::VFMSUB213SDZr_Int:
8332 case X86::VFNMSUB213SDZr_Int:
8333 case X86::VFMSUB231SDZr_Int:
8334 case X86::VFNMSUB231SDZr_Int:
8335 case X86::VFMADD132SDZrk_Int:
8336 case X86::VFNMADD132SDZrk_Int:
8337 case X86::VFMADD213SDZrk_Int:
8338 case X86::VFNMADD213SDZrk_Int:
8339 case X86::VFMADD231SDZrk_Int:
8340 case X86::VFNMADD231SDZrk_Int:
8341 case X86::VFMSUB132SDZrk_Int:
8342 case X86::VFNMSUB132SDZrk_Int:
8343 case X86::VFMSUB213SDZrk_Int:
8344 case X86::VFNMSUB213SDZrk_Int:
8345 case X86::VFMSUB231SDZrk_Int:
8346 case X86::VFNMSUB231SDZrk_Int:
8347 case X86::VFMADD132SDZrkz_Int:
8348 case X86::VFNMADD132SDZrkz_Int:
8349 case X86::VFMADD213SDZrkz_Int:
8350 case X86::VFNMADD213SDZrkz_Int:
8351 case X86::VFMADD231SDZrkz_Int:
8352 case X86::VFNMADD231SDZrkz_Int:
8353 case X86::VFMSUB132SDZrkz_Int:
8354 case X86::VFNMSUB132SDZrkz_Int:
8355 case X86::VFMSUB213SDZrkz_Int:
8356 case X86::VFNMSUB213SDZrkz_Int:
8357 case X86::VFMSUB231SDZrkz_Int:
8358 case X86::VFNMSUB231SDZrkz_Int:
8359 case X86::VFIXUPIMMSDZrri:
8360 case X86::VFIXUPIMMSDZrrik:
8361 case X86::VFIXUPIMMSDZrrikz:
8362 case X86::VFPCLASSSDZri:
8363 case X86::VFPCLASSSDZrik:
8364 case X86::VGETEXPSDZr:
8365 case X86::VGETEXPSDZrk:
8366 case X86::VGETEXPSDZrkz:
8367 case X86::VGETMANTSDZrri:
8368 case X86::VGETMANTSDZrrik:
8369 case X86::VGETMANTSDZrrikz:
8370 case X86::VRANGESDZrri:
8371 case X86::VRANGESDZrrik:
8372 case X86::VRANGESDZrrikz:
8373 case X86::VRCP14SDZrr:
8374 case X86::VRCP14SDZrrk:
8375 case X86::VRCP14SDZrrkz:
8376 case X86::VRCP28SDZr:
8377 case X86::VRCP28SDZrk:
8378 case X86::VRCP28SDZrkz:
8379 case X86::VREDUCESDZrri:
8380 case X86::VREDUCESDZrrik:
8381 case X86::VREDUCESDZrrikz:
8382 case X86::VRNDSCALESDZrri_Int:
8383 case X86::VRNDSCALESDZrrik_Int:
8384 case X86::VRNDSCALESDZrrikz_Int:
8385 case X86::VRSQRT14SDZrr:
8386 case X86::VRSQRT14SDZrrk:
8387 case X86::VRSQRT14SDZrrkz:
8388 case X86::VRSQRT28SDZr:
8389 case X86::VRSQRT28SDZrk:
8390 case X86::VRSQRT28SDZrkz:
8391 case X86::VSCALEFSDZrr:
8392 case X86::VSCALEFSDZrrk:
8393 case X86::VSCALEFSDZrrkz:
8394 return false;
8395 default:
8396 return true;
8397 }
8398 }
8399
8400 if ((Opc == X86::VMOVSHZrm || Opc == X86::VMOVSHZrm_alt) && RegSize > 16) {
8401 // These instructions only load 16 bits, we can't fold them if the
8402 // destination register is wider than 16 bits (2 bytes), and its user
8403 // instruction isn't scalar (SH).
8404 switch (UserOpc) {
8405 case X86::VADDSHZrr_Int:
8406 case X86::VCMPSHZrri_Int:
8407 case X86::VDIVSHZrr_Int:
8408 case X86::VMAXSHZrr_Int:
8409 case X86::VMINSHZrr_Int:
8410 case X86::VMULSHZrr_Int:
8411 case X86::VSUBSHZrr_Int:
8412 case X86::VADDSHZrrk_Int:
8413 case X86::VADDSHZrrkz_Int:
8414 case X86::VCMPSHZrrik_Int:
8415 case X86::VDIVSHZrrk_Int:
8416 case X86::VDIVSHZrrkz_Int:
8417 case X86::VMAXSHZrrk_Int:
8418 case X86::VMAXSHZrrkz_Int:
8419 case X86::VMINSHZrrk_Int:
8420 case X86::VMINSHZrrkz_Int:
8421 case X86::VMULSHZrrk_Int:
8422 case X86::VMULSHZrrkz_Int:
8423 case X86::VSUBSHZrrk_Int:
8424 case X86::VSUBSHZrrkz_Int:
8425 case X86::VFMADD132SHZr_Int:
8426 case X86::VFNMADD132SHZr_Int:
8427 case X86::VFMADD213SHZr_Int:
8428 case X86::VFNMADD213SHZr_Int:
8429 case X86::VFMADD231SHZr_Int:
8430 case X86::VFNMADD231SHZr_Int:
8431 case X86::VFMSUB132SHZr_Int:
8432 case X86::VFNMSUB132SHZr_Int:
8433 case X86::VFMSUB213SHZr_Int:
8434 case X86::VFNMSUB213SHZr_Int:
8435 case X86::VFMSUB231SHZr_Int:
8436 case X86::VFNMSUB231SHZr_Int:
8437 case X86::VFMADD132SHZrk_Int:
8438 case X86::VFNMADD132SHZrk_Int:
8439 case X86::VFMADD213SHZrk_Int:
8440 case X86::VFNMADD213SHZrk_Int:
8441 case X86::VFMADD231SHZrk_Int:
8442 case X86::VFNMADD231SHZrk_Int:
8443 case X86::VFMSUB132SHZrk_Int:
8444 case X86::VFNMSUB132SHZrk_Int:
8445 case X86::VFMSUB213SHZrk_Int:
8446 case X86::VFNMSUB213SHZrk_Int:
8447 case X86::VFMSUB231SHZrk_Int:
8448 case X86::VFNMSUB231SHZrk_Int:
8449 case X86::VFMADD132SHZrkz_Int:
8450 case X86::VFNMADD132SHZrkz_Int:
8451 case X86::VFMADD213SHZrkz_Int:
8452 case X86::VFNMADD213SHZrkz_Int:
8453 case X86::VFMADD231SHZrkz_Int:
8454 case X86::VFNMADD231SHZrkz_Int:
8455 case X86::VFMSUB132SHZrkz_Int:
8456 case X86::VFNMSUB132SHZrkz_Int:
8457 case X86::VFMSUB213SHZrkz_Int:
8458 case X86::VFNMSUB213SHZrkz_Int:
8459 case X86::VFMSUB231SHZrkz_Int:
8460 case X86::VFNMSUB231SHZrkz_Int:
8461 return false;
8462 default:
8463 return true;
8464 }
8465 }
8466
8467 return false;
8468}
8469
8473 MachineInstr &LoadMI, MachineInstr *&CopyMI,
8474 LiveIntervals *LIS, VirtRegMap *VRM) const {
8476
8477 // If LoadMI is a masked load, check MI having the same mask.
8478 const MCInstrDesc &MCID = get(LoadMI.getOpcode());
8479 unsigned NumOps = MCID.getNumOperands();
8480 if (NumOps >= 3) {
8481 Register MaskReg;
8482 const MachineOperand &Op1 = LoadMI.getOperand(1);
8483 const MachineOperand &Op2 = LoadMI.getOperand(2);
8484
8485 auto IsVKWMClass = [](const TargetRegisterClass *RC) {
8486 return RC == &X86::VK2WMRegClass || RC == &X86::VK4WMRegClass ||
8487 RC == &X86::VK8WMRegClass || RC == &X86::VK16WMRegClass ||
8488 RC == &X86::VK32WMRegClass || RC == &X86::VK64WMRegClass;
8489 };
8490
8491 if (Op1.isReg() && IsVKWMClass(getRegClass(MCID, 1)))
8492 MaskReg = Op1.getReg();
8493 else if (Op2.isReg() && IsVKWMClass(getRegClass(MCID, 2)))
8494 MaskReg = Op2.getReg();
8495
8496 if (MaskReg) {
8497 // Some instructions are invalid to fold into even with the same mask.
8498 // Folding is unsafe if an active destination element may read from a
8499 // source element that is masked off.
8500 if (isNonFoldableWithSameMask(MI.getOpcode()))
8501 return nullptr;
8502 bool HasSameMask = false;
8503 for (unsigned I = 1, E = MI.getDesc().getNumOperands(); I < E; ++I) {
8504 const MachineOperand &Op = MI.getOperand(I);
8505 if (Op.isReg() && Op.getReg() == MaskReg) {
8506 HasSameMask = true;
8507 break;
8508 }
8509 }
8510 if (!HasSameMask)
8511 return nullptr;
8512 }
8513 }
8514
8515 // TODO: Support the case where LoadMI loads a wide register, but MI
8516 // only uses a subreg.
8517 for (auto Op : Ops) {
8518 if (MI.getOperand(Op).getSubReg())
8519 return nullptr;
8520 }
8521
8522 // If loading from a FrameIndex, fold directly from the FrameIndex.
8523 int FrameIndex;
8524 if (isLoadFromStackSlot(LoadMI, FrameIndex)) {
8525 if (isNonFoldablePartialRegisterLoad(LoadMI, MI, MF))
8526 return nullptr;
8527 return foldMemoryOperandImpl(MF, MI, Ops, FrameIndex, CopyMI, LIS, VRM);
8528 }
8529
8530 // Check switch flag
8531 if (NoFusing)
8532 return nullptr;
8533
8534 // Avoid partial and undef register update stalls unless optimizing for size.
8535 if (!MF.getFunction().hasOptSize() &&
8536 (hasPartialRegUpdate(MI.getOpcode(), Subtarget, /*ForLoadFold*/ true) ||
8538 return nullptr;
8539
8540 // Do not fold a NDD instruction and a memory instruction with relocation to
8541 // avoid emit APX relocation when the flag is disabled for backward
8542 // compatibility.
8543 uint64_t TSFlags = MI.getDesc().TSFlags;
8545 X86II::hasNewDataDest(TSFlags))
8546 return nullptr;
8547
8548 // Determine the alignment of the load.
8549 Align Alignment;
8550 unsigned LoadOpc = LoadMI.getOpcode();
8551 if (LoadMI.hasOneMemOperand())
8552 Alignment = (*LoadMI.memoperands_begin())->getAlign();
8553 else
8554 switch (LoadOpc) {
8555 case X86::AVX512_512_SETALLONES:
8556 Alignment = Align(64);
8557 break;
8558 case X86::AVX2_SETALLONES:
8559 case X86::AVX1_SETALLONES:
8560 case X86::AVX512_256_SETALLONES:
8561 Alignment = Align(32);
8562 break;
8563 case X86::V_SET0:
8564 case X86::V_SETALLONES:
8565 case X86::AVX512_128_SET0:
8566 case X86::FsFLD0F128:
8567 case X86::AVX512_FsFLD0F128:
8568 case X86::AVX512_128_SETALLONES:
8569 Alignment = Align(16);
8570 break;
8571 case X86::MMX_SET0:
8572 case X86::FsFLD0SD:
8573 case X86::AVX512_FsFLD0SD:
8574 Alignment = Align(8);
8575 break;
8576 case X86::FsFLD0SS:
8577 case X86::AVX512_FsFLD0SS:
8578 Alignment = Align(4);
8579 break;
8580 case X86::FsFLD0SH:
8581 case X86::AVX512_FsFLD0SH:
8582 Alignment = Align(2);
8583 break;
8584 default:
8585 return nullptr;
8586 }
8587 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
8588 unsigned NewOpc = 0;
8589 switch (MI.getOpcode()) {
8590 default:
8591 return nullptr;
8592 case X86::TEST8rr:
8593 NewOpc = X86::CMP8ri;
8594 break;
8595 case X86::TEST16rr:
8596 NewOpc = X86::CMP16ri;
8597 break;
8598 case X86::TEST32rr:
8599 NewOpc = X86::CMP32ri;
8600 break;
8601 case X86::TEST64rr:
8602 NewOpc = X86::CMP64ri32;
8603 break;
8604 }
8605 // Change to CMPXXri r, 0 first.
8606 MI.setDesc(get(NewOpc));
8607 MI.getOperand(1).ChangeToImmediate(0);
8608 } else if (Ops.size() != 1)
8609 return nullptr;
8610
8611 // Make sure the subregisters match.
8612 // Otherwise we risk changing the size of the load.
8613 if (LoadMI.getOperand(0).getSubReg() != MI.getOperand(Ops[0]).getSubReg())
8614 return nullptr;
8615
8617 switch (LoadOpc) {
8618 case X86::MMX_SET0:
8619 case X86::V_SET0:
8620 case X86::V_SETALLONES:
8621 case X86::AVX2_SETALLONES:
8622 case X86::AVX1_SETALLONES:
8623 case X86::AVX512_128_SET0:
8624 case X86::AVX512_128_SETALLONES:
8625 case X86::AVX512_256_SETALLONES:
8626 case X86::AVX512_512_SETALLONES:
8627 case X86::FsFLD0SH:
8628 case X86::AVX512_FsFLD0SH:
8629 case X86::FsFLD0SD:
8630 case X86::AVX512_FsFLD0SD:
8631 case X86::FsFLD0SS:
8632 case X86::AVX512_FsFLD0SS:
8633 case X86::FsFLD0F128:
8634 case X86::AVX512_FsFLD0F128: {
8635 // Folding a V_SET0 or V_SETALLONES as a load, to ease register pressure.
8636 // Create a constant-pool entry and operands to load from it.
8637
8638 // Large code model can't fold loads this way.
8640 return nullptr;
8641
8642 // x86-32 PIC requires a PIC base register for constant pools.
8643 unsigned PICBase = 0;
8644 // Since we're using Small or Kernel code model, we can always use
8645 // RIP-relative addressing for a smaller encoding.
8646 if (Subtarget.is64Bit()) {
8647 PICBase = X86::RIP;
8648 } else if (MF.getTarget().isPositionIndependent()) {
8649 // FIXME: PICBase = getGlobalBaseReg(&MF);
8650 // This doesn't work for several reasons.
8651 // 1. GlobalBaseReg may have been spilled.
8652 // 2. It may not be live at MI.
8653 return nullptr;
8654 }
8655
8656 // Create a constant-pool entry.
8658 Type *Ty;
8659 bool IsAllOnes = false;
8660 switch (LoadOpc) {
8661 case X86::FsFLD0SS:
8662 case X86::AVX512_FsFLD0SS:
8664 break;
8665 case X86::FsFLD0SD:
8666 case X86::AVX512_FsFLD0SD:
8668 break;
8669 case X86::FsFLD0F128:
8670 case X86::AVX512_FsFLD0F128:
8672 break;
8673 case X86::FsFLD0SH:
8674 case X86::AVX512_FsFLD0SH:
8676 break;
8677 case X86::AVX512_512_SETALLONES:
8678 IsAllOnes = true;
8680 16);
8681 break;
8682 case X86::AVX1_SETALLONES:
8683 case X86::AVX2_SETALLONES:
8684 case X86::AVX512_256_SETALLONES:
8685 IsAllOnes = true;
8687 8);
8688
8689 break;
8690 case X86::MMX_SET0:
8692 2);
8693 break;
8694 case X86::V_SETALLONES:
8695 case X86::AVX512_128_SETALLONES:
8696 IsAllOnes = true;
8697 [[fallthrough]];
8698 case X86::V_SET0:
8699 case X86::AVX512_128_SET0:
8701 4);
8702 break;
8703 }
8704
8705 const Constant *C =
8707 unsigned CPI = MCP.getConstantPoolIndex(C, Alignment);
8708
8709 // Create operands to load from the constant pool entry.
8710 MOs.push_back(MachineOperand::CreateReg(PICBase, false));
8712 MOs.push_back(MachineOperand::CreateReg(0, false));
8714 MOs.push_back(MachineOperand::CreateReg(0, false));
8715 break;
8716 }
8717 case X86::VPBROADCASTBZ128rm:
8718 case X86::VPBROADCASTBZ256rm:
8719 case X86::VPBROADCASTBZrm:
8720 case X86::VBROADCASTF32X2Z256rm:
8721 case X86::VBROADCASTF32X2Zrm:
8722 case X86::VBROADCASTI32X2Z128rm:
8723 case X86::VBROADCASTI32X2Z256rm:
8724 case X86::VBROADCASTI32X2Zrm:
8725 // No instructions currently fuse with 8bits or 32bits x 2.
8726 return nullptr;
8727
8728#define FOLD_BROADCAST(SIZE) \
8729 MOs.append(LoadMI.operands_begin() + NumOps - X86::AddrNumOperands, \
8730 LoadMI.operands_begin() + NumOps); \
8731 return foldMemoryBroadcast(MF, MI, Ops[0], MOs, InsertPt, /*Size=*/SIZE, \
8732 /*AllowCommute=*/true);
8733 case X86::VPBROADCASTWZ128rm:
8734 case X86::VPBROADCASTWZ256rm:
8735 case X86::VPBROADCASTWZrm:
8736 FOLD_BROADCAST(16);
8737 case X86::VPBROADCASTDZ128rm:
8738 case X86::VPBROADCASTDZ256rm:
8739 case X86::VPBROADCASTDZrm:
8740 case X86::VBROADCASTSSZ128rm:
8741 case X86::VBROADCASTSSZ256rm:
8742 case X86::VBROADCASTSSZrm:
8743 FOLD_BROADCAST(32);
8744 case X86::VPBROADCASTQZ128rm:
8745 case X86::VPBROADCASTQZ256rm:
8746 case X86::VPBROADCASTQZrm:
8747 case X86::VBROADCASTSDZ256rm:
8748 case X86::VBROADCASTSDZrm:
8749 FOLD_BROADCAST(64);
8750 default: {
8751 if (isNonFoldablePartialRegisterLoad(LoadMI, MI, MF))
8752 return nullptr;
8753
8754 // Folding a normal load. Just copy the load's address operands.
8756 LoadMI.operands_begin() + NumOps);
8757 break;
8758 }
8759 }
8760 return foldMemoryOperandImpl(MF, MI, Ops[0], MOs, InsertPt,
8761 /*Size=*/0, Alignment, /*AllowCommute=*/true,
8762 CopyMI, VRM);
8763}
8764
8766X86InstrInfo::foldMemoryBroadcast(MachineFunction &MF, MachineInstr &MI,
8767 unsigned OpNum, ArrayRef<MachineOperand> MOs,
8769 unsigned BitsSize, bool AllowCommute) const {
8770
8771 if (auto *I = lookupBroadcastFoldTable(MI.getOpcode(), OpNum))
8772 return matchBroadcastSize(*I, BitsSize)
8773 ? fuseInst(MF, I->DstOp, OpNum, MOs, InsertPt, MI, *this)
8774 : nullptr;
8775
8776 if (AllowCommute) {
8777 // If the instruction and target operand are commutable, commute the
8778 // instruction and try again.
8779 unsigned CommuteOpIdx2 = commuteOperandsForFold(MI, OpNum);
8780 if (CommuteOpIdx2 == OpNum) {
8781 printFailMsgforFold(MI, OpNum);
8782 return nullptr;
8783 }
8784 MachineInstr *NewMI =
8785 foldMemoryBroadcast(MF, MI, CommuteOpIdx2, MOs, InsertPt, BitsSize,
8786 /*AllowCommute=*/false);
8787 if (NewMI)
8788 return NewMI;
8789 // Folding failed again - undo the commute before returning.
8790 commuteInstruction(MI, false, OpNum, CommuteOpIdx2);
8791 }
8792
8793 printFailMsgforFold(MI, OpNum);
8794 return nullptr;
8795}
8796
8800
8801 for (MachineMemOperand *MMO : MMOs) {
8802 if (!MMO->isLoad())
8803 continue;
8804
8805 if (!MMO->isStore()) {
8806 // Reuse the MMO.
8807 LoadMMOs.push_back(MMO);
8808 } else {
8809 // Clone the MMO and unset the store flag.
8810 LoadMMOs.push_back(MF.getMachineMemOperand(
8811 MMO, MMO->getFlags() & ~MachineMemOperand::MOStore));
8812 }
8813 }
8814
8815 return LoadMMOs;
8816}
8817
8821
8822 for (MachineMemOperand *MMO : MMOs) {
8823 if (!MMO->isStore())
8824 continue;
8825
8826 if (!MMO->isLoad()) {
8827 // Reuse the MMO.
8828 StoreMMOs.push_back(MMO);
8829 } else {
8830 // Clone the MMO and unset the load flag.
8831 StoreMMOs.push_back(MF.getMachineMemOperand(
8832 MMO, MMO->getFlags() & ~MachineMemOperand::MOLoad));
8833 }
8834 }
8835
8836 return StoreMMOs;
8837}
8838
8840 const TargetRegisterClass *RC,
8841 const X86Subtarget &STI) {
8842 assert(STI.hasAVX512() && "Expected at least AVX512!");
8843 unsigned SpillSize = STI.getRegisterInfo()->getSpillSize(*RC);
8844 assert((SpillSize == 64 || STI.hasVLX()) &&
8845 "Can't broadcast less than 64 bytes without AVX512VL!");
8846
8847#define CASE_BCAST_TYPE_OPC(TYPE, OP16, OP32, OP64) \
8848 case TYPE: \
8849 switch (SpillSize) { \
8850 default: \
8851 llvm_unreachable("Unknown spill size"); \
8852 case 16: \
8853 return X86::OP16; \
8854 case 32: \
8855 return X86::OP32; \
8856 case 64: \
8857 return X86::OP64; \
8858 } \
8859 break;
8860
8861 switch (I->Flags & TB_BCAST_MASK) {
8862 default:
8863 llvm_unreachable("Unexpected broadcast type!");
8864 CASE_BCAST_TYPE_OPC(TB_BCAST_W, VPBROADCASTWZ128rm, VPBROADCASTWZ256rm,
8865 VPBROADCASTWZrm)
8866 CASE_BCAST_TYPE_OPC(TB_BCAST_D, VPBROADCASTDZ128rm, VPBROADCASTDZ256rm,
8867 VPBROADCASTDZrm)
8868 CASE_BCAST_TYPE_OPC(TB_BCAST_Q, VPBROADCASTQZ128rm, VPBROADCASTQZ256rm,
8869 VPBROADCASTQZrm)
8870 CASE_BCAST_TYPE_OPC(TB_BCAST_SH, VPBROADCASTWZ128rm, VPBROADCASTWZ256rm,
8871 VPBROADCASTWZrm)
8872 CASE_BCAST_TYPE_OPC(TB_BCAST_SS, VBROADCASTSSZ128rm, VBROADCASTSSZ256rm,
8873 VBROADCASTSSZrm)
8874 CASE_BCAST_TYPE_OPC(TB_BCAST_SD, VMOVDDUPZ128rm, VBROADCASTSDZ256rm,
8875 VBROADCASTSDZrm)
8876 }
8877}
8878
8880 MachineFunction &MF, MachineInstr &MI, Register Reg, bool UnfoldLoad,
8881 bool UnfoldStore, SmallVectorImpl<MachineInstr *> &NewMIs) const {
8882 const X86FoldTableEntry *I = lookupUnfoldTable(MI.getOpcode());
8883 if (I == nullptr)
8884 return false;
8885 unsigned Opc = I->DstOp;
8886 unsigned Index = I->Flags & TB_INDEX_MASK;
8887 bool FoldedLoad = I->Flags & TB_FOLDED_LOAD;
8888 bool FoldedStore = I->Flags & TB_FOLDED_STORE;
8889 if (UnfoldLoad && !FoldedLoad)
8890 return false;
8891 UnfoldLoad &= FoldedLoad;
8892 if (UnfoldStore && !FoldedStore)
8893 return false;
8894 UnfoldStore &= FoldedStore;
8895
8896 const MCInstrDesc &MCID = get(Opc);
8897
8898 const TargetRegisterClass *RC = getRegClass(MCID, Index);
8900 // TODO: Check if 32-byte or greater accesses are slow too?
8901 if (!MI.hasOneMemOperand() && RC == &X86::VR128RegClass &&
8902 Subtarget.isUnalignedMem16Slow())
8903 // Without memoperands, loadRegFromAddr and storeRegToStackSlot will
8904 // conservatively assume the address is unaligned. That's bad for
8905 // performance.
8906 return false;
8911 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
8912 MachineOperand &Op = MI.getOperand(i);
8913 if (i >= Index && i < Index + X86::AddrNumOperands)
8914 AddrOps.push_back(Op);
8915 else if (Op.isReg() && Op.isImplicit())
8916 ImpOps.push_back(Op);
8917 else if (i < Index)
8918 BeforeOps.push_back(Op);
8919 else if (i > Index)
8920 AfterOps.push_back(Op);
8921 }
8922
8923 // Emit the load or broadcast instruction.
8924 if (UnfoldLoad) {
8925 auto MMOs = extractLoadMMOs(MI.memoperands(), MF);
8926
8927 unsigned Opc;
8928 if (I->Flags & TB_BCAST_MASK) {
8929 Opc = getBroadcastOpcode(I, RC, Subtarget);
8930 } else {
8931 unsigned Alignment = std::max<uint32_t>(TRI.getSpillSize(*RC), 16);
8932 bool isAligned = !MMOs.empty() && MMOs.front()->getAlign() >= Alignment;
8933 Opc = getLoadRegOpcode(Reg, RC, isAligned, Subtarget);
8934 }
8935
8936 DebugLoc DL;
8937 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), Reg);
8938 for (const MachineOperand &AddrOp : AddrOps)
8939 MIB.add(AddrOp);
8940 MIB.setMemRefs(MMOs);
8941 NewMIs.push_back(MIB);
8942
8943 if (UnfoldStore) {
8944 // Address operands cannot be marked isKill.
8945 for (unsigned i = 1; i != 1 + X86::AddrNumOperands; ++i) {
8946 MachineOperand &MO = NewMIs[0]->getOperand(i);
8947 if (MO.isReg())
8948 MO.setIsKill(false);
8949 }
8950 }
8951 }
8952
8953 // Emit the data processing instruction.
8954 MachineInstr *DataMI = MF.CreateMachineInstr(MCID, MI.getDebugLoc(), true);
8955 MachineInstrBuilder MIB(MF, DataMI);
8956
8957 if (FoldedStore)
8958 MIB.addReg(Reg, RegState::Define);
8959 for (MachineOperand &BeforeOp : BeforeOps)
8960 MIB.add(BeforeOp);
8961 if (FoldedLoad)
8962 MIB.addReg(Reg);
8963 for (MachineOperand &AfterOp : AfterOps)
8964 MIB.add(AfterOp);
8965 for (MachineOperand &ImpOp : ImpOps) {
8966 MIB.addReg(ImpOp.getReg(), getDefRegState(ImpOp.isDef()) |
8968 getKillRegState(ImpOp.isKill()) |
8969 getDeadRegState(ImpOp.isDead()) |
8970 getUndefRegState(ImpOp.isUndef()));
8971 }
8972 // Change CMP32ri r, 0 back to TEST32rr r, r, etc.
8973 switch (DataMI->getOpcode()) {
8974 default:
8975 break;
8976 case X86::CMP64ri32:
8977 case X86::CMP32ri:
8978 case X86::CMP16ri:
8979 case X86::CMP8ri: {
8980 MachineOperand &MO0 = DataMI->getOperand(0);
8981 MachineOperand &MO1 = DataMI->getOperand(1);
8982 if (MO1.isImm() && MO1.getImm() == 0) {
8983 unsigned NewOpc;
8984 switch (DataMI->getOpcode()) {
8985 default:
8986 llvm_unreachable("Unreachable!");
8987 case X86::CMP64ri32:
8988 NewOpc = X86::TEST64rr;
8989 break;
8990 case X86::CMP32ri:
8991 NewOpc = X86::TEST32rr;
8992 break;
8993 case X86::CMP16ri:
8994 NewOpc = X86::TEST16rr;
8995 break;
8996 case X86::CMP8ri:
8997 NewOpc = X86::TEST8rr;
8998 break;
8999 }
9000 DataMI->setDesc(get(NewOpc));
9001 MO1.ChangeToRegister(MO0.getReg(), false);
9002 }
9003 }
9004 }
9005 NewMIs.push_back(DataMI);
9006
9007 // Emit the store instruction.
9008 if (UnfoldStore) {
9009 const TargetRegisterClass *DstRC = getRegClass(MCID, 0);
9010 auto MMOs = extractStoreMMOs(MI.memoperands(), MF);
9011 unsigned Alignment = std::max<uint32_t>(TRI.getSpillSize(*DstRC), 16);
9012 bool isAligned = !MMOs.empty() && MMOs.front()->getAlign() >= Alignment;
9013 unsigned Opc = getStoreRegOpcode(Reg, DstRC, isAligned, Subtarget);
9014 DebugLoc DL;
9015 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc));
9016 for (const MachineOperand &AddrOp : AddrOps)
9017 MIB.add(AddrOp);
9018 MIB.addReg(Reg, RegState::Kill);
9019 MIB.setMemRefs(MMOs);
9020 NewMIs.push_back(MIB);
9021 }
9022
9023 return true;
9024}
9025
9027 SelectionDAG &DAG, SDNode *N, SmallVectorImpl<SDNode *> &NewNodes) const {
9028 if (!N->isMachineOpcode())
9029 return false;
9030
9031 const X86FoldTableEntry *I = lookupUnfoldTable(N->getMachineOpcode());
9032 if (I == nullptr)
9033 return false;
9034 unsigned Opc = I->DstOp;
9035 unsigned Index = I->Flags & TB_INDEX_MASK;
9036 bool FoldedLoad = I->Flags & TB_FOLDED_LOAD;
9037 bool FoldedStore = I->Flags & TB_FOLDED_STORE;
9038 const MCInstrDesc &MCID = get(Opc);
9041 const TargetRegisterClass *RC = getRegClass(MCID, Index);
9042 unsigned NumDefs = MCID.NumDefs;
9043 std::vector<SDValue> AddrOps;
9044 std::vector<SDValue> BeforeOps;
9045 std::vector<SDValue> AfterOps;
9046 SDLoc dl(N);
9047 unsigned NumOps = N->getNumOperands();
9048 for (unsigned i = 0; i != NumOps - 1; ++i) {
9049 SDValue Op = N->getOperand(i);
9050 if (i >= Index - NumDefs && i < Index - NumDefs + X86::AddrNumOperands)
9051 AddrOps.push_back(Op);
9052 else if (i < Index - NumDefs)
9053 BeforeOps.push_back(Op);
9054 else if (i > Index - NumDefs)
9055 AfterOps.push_back(Op);
9056 }
9057 SDValue Chain = N->getOperand(NumOps - 1);
9058 AddrOps.push_back(Chain);
9059
9060 // Emit the load instruction.
9061 SDNode *Load = nullptr;
9062 if (FoldedLoad) {
9063 EVT VT = *TRI.legalclasstypes_begin(*RC);
9064 auto MMOs = extractLoadMMOs(cast<MachineSDNode>(N)->memoperands(), MF);
9065 if (MMOs.empty() && RC == &X86::VR128RegClass &&
9066 Subtarget.isUnalignedMem16Slow())
9067 // Do not introduce a slow unaligned load.
9068 return false;
9069 // FIXME: If a VR128 can have size 32, we should be checking if a 32-byte
9070 // memory access is slow above.
9071
9072 unsigned Opc;
9073 if (I->Flags & TB_BCAST_MASK) {
9074 Opc = getBroadcastOpcode(I, RC, Subtarget);
9075 } else {
9076 unsigned Alignment = std::max<uint32_t>(TRI.getSpillSize(*RC), 16);
9077 bool isAligned = !MMOs.empty() && MMOs.front()->getAlign() >= Alignment;
9078 Opc = getLoadRegOpcode(0, RC, isAligned, Subtarget);
9079 }
9080
9081 Load = DAG.getMachineNode(Opc, dl, VT, MVT::Other, AddrOps);
9082 NewNodes.push_back(Load);
9083
9084 // Preserve memory reference information.
9086 }
9087
9088 // Emit the data processing instruction.
9089 std::vector<EVT> VTs;
9090 const TargetRegisterClass *DstRC = nullptr;
9091 if (MCID.getNumDefs() > 0) {
9092 DstRC = getRegClass(MCID, 0);
9093 VTs.push_back(*TRI.legalclasstypes_begin(*DstRC));
9094 }
9095 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
9096 EVT VT = N->getValueType(i);
9097 if (VT != MVT::Other && i >= (unsigned)MCID.getNumDefs())
9098 VTs.push_back(VT);
9099 }
9100 if (Load)
9101 BeforeOps.push_back(SDValue(Load, 0));
9102 llvm::append_range(BeforeOps, AfterOps);
9103 // Change CMP32ri r, 0 back to TEST32rr r, r, etc.
9104 switch (Opc) {
9105 default:
9106 break;
9107 case X86::CMP64ri32:
9108 case X86::CMP32ri:
9109 case X86::CMP16ri:
9110 case X86::CMP8ri:
9111 if (isNullConstant(BeforeOps[1])) {
9112 switch (Opc) {
9113 default:
9114 llvm_unreachable("Unreachable!");
9115 case X86::CMP64ri32:
9116 Opc = X86::TEST64rr;
9117 break;
9118 case X86::CMP32ri:
9119 Opc = X86::TEST32rr;
9120 break;
9121 case X86::CMP16ri:
9122 Opc = X86::TEST16rr;
9123 break;
9124 case X86::CMP8ri:
9125 Opc = X86::TEST8rr;
9126 break;
9127 }
9128 BeforeOps[1] = BeforeOps[0];
9129 }
9130 }
9131 SDNode *NewNode = DAG.getMachineNode(Opc, dl, VTs, BeforeOps);
9132 NewNodes.push_back(NewNode);
9133
9134 // Emit the store instruction.
9135 if (FoldedStore) {
9136 AddrOps.pop_back();
9137 AddrOps.push_back(SDValue(NewNode, 0));
9138 AddrOps.push_back(Chain);
9139 auto MMOs = extractStoreMMOs(cast<MachineSDNode>(N)->memoperands(), MF);
9140 if (MMOs.empty() && RC == &X86::VR128RegClass &&
9141 Subtarget.isUnalignedMem16Slow())
9142 // Do not introduce a slow unaligned store.
9143 return false;
9144 // FIXME: If a VR128 can have size 32, we should be checking if a 32-byte
9145 // memory access is slow above.
9146 unsigned Alignment = std::max<uint32_t>(TRI.getSpillSize(*RC), 16);
9147 bool isAligned = !MMOs.empty() && MMOs.front()->getAlign() >= Alignment;
9148 SDNode *Store =
9149 DAG.getMachineNode(getStoreRegOpcode(0, DstRC, isAligned, Subtarget),
9150 dl, MVT::Other, AddrOps);
9151 NewNodes.push_back(Store);
9152
9153 // Preserve memory reference information.
9155 }
9156
9157 return true;
9158}
9159
9160unsigned
9162 bool UnfoldStore,
9163 unsigned *LoadRegIndex) const {
9165 if (I == nullptr)
9166 return 0;
9167 bool FoldedLoad = I->Flags & TB_FOLDED_LOAD;
9168 bool FoldedStore = I->Flags & TB_FOLDED_STORE;
9169 if (UnfoldLoad && !FoldedLoad)
9170 return 0;
9171 if (UnfoldStore && !FoldedStore)
9172 return 0;
9173 if (LoadRegIndex)
9174 *LoadRegIndex = I->Flags & TB_INDEX_MASK;
9175 return I->DstOp;
9176}
9177
9179 int64_t &Offset1,
9180 int64_t &Offset2) const {
9181 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
9182 return false;
9183
9184 auto IsLoadOpcode = [&](unsigned Opcode) {
9185 switch (Opcode) {
9186 default:
9187 return false;
9188 case X86::MOV8rm:
9189 case X86::MOV16rm:
9190 case X86::MOV32rm:
9191 case X86::MOV64rm:
9192 case X86::LD_Fp32m:
9193 case X86::LD_Fp64m:
9194 case X86::LD_Fp80m:
9195 case X86::MOVSSrm:
9196 case X86::MOVSSrm_alt:
9197 case X86::MOVSDrm:
9198 case X86::MOVSDrm_alt:
9199 case X86::MMX_MOVD64rm:
9200 case X86::MMX_MOVQ64rm:
9201 case X86::MOVAPSrm:
9202 case X86::MOVUPSrm:
9203 case X86::MOVAPDrm:
9204 case X86::MOVUPDrm:
9205 case X86::MOVDQArm:
9206 case X86::MOVDQUrm:
9207 // AVX load instructions
9208 case X86::VMOVSSrm:
9209 case X86::VMOVSSrm_alt:
9210 case X86::VMOVSDrm:
9211 case X86::VMOVSDrm_alt:
9212 case X86::VMOVAPSrm:
9213 case X86::VMOVUPSrm:
9214 case X86::VMOVAPDrm:
9215 case X86::VMOVUPDrm:
9216 case X86::VMOVDQArm:
9217 case X86::VMOVDQUrm:
9218 case X86::VMOVAPSYrm:
9219 case X86::VMOVUPSYrm:
9220 case X86::VMOVAPDYrm:
9221 case X86::VMOVUPDYrm:
9222 case X86::VMOVDQAYrm:
9223 case X86::VMOVDQUYrm:
9224 // AVX512 load instructions
9225 case X86::VMOVSSZrm:
9226 case X86::VMOVSSZrm_alt:
9227 case X86::VMOVSDZrm:
9228 case X86::VMOVSDZrm_alt:
9229 case X86::VMOVAPSZ128rm:
9230 case X86::VMOVUPSZ128rm:
9231 case X86::VMOVAPSZ128rm_NOVLX:
9232 case X86::VMOVUPSZ128rm_NOVLX:
9233 case X86::VMOVAPDZ128rm:
9234 case X86::VMOVUPDZ128rm:
9235 case X86::VMOVDQU8Z128rm:
9236 case X86::VMOVDQU16Z128rm:
9237 case X86::VMOVDQA32Z128rm:
9238 case X86::VMOVDQU32Z128rm:
9239 case X86::VMOVDQA64Z128rm:
9240 case X86::VMOVDQU64Z128rm:
9241 case X86::VMOVAPSZ256rm:
9242 case X86::VMOVUPSZ256rm:
9243 case X86::VMOVAPSZ256rm_NOVLX:
9244 case X86::VMOVUPSZ256rm_NOVLX:
9245 case X86::VMOVAPDZ256rm:
9246 case X86::VMOVUPDZ256rm:
9247 case X86::VMOVDQU8Z256rm:
9248 case X86::VMOVDQU16Z256rm:
9249 case X86::VMOVDQA32Z256rm:
9250 case X86::VMOVDQU32Z256rm:
9251 case X86::VMOVDQA64Z256rm:
9252 case X86::VMOVDQU64Z256rm:
9253 case X86::VMOVAPSZrm:
9254 case X86::VMOVUPSZrm:
9255 case X86::VMOVAPDZrm:
9256 case X86::VMOVUPDZrm:
9257 case X86::VMOVDQU8Zrm:
9258 case X86::VMOVDQU16Zrm:
9259 case X86::VMOVDQA32Zrm:
9260 case X86::VMOVDQU32Zrm:
9261 case X86::VMOVDQA64Zrm:
9262 case X86::VMOVDQU64Zrm:
9263 case X86::KMOVBkm:
9264 case X86::KMOVBkm_EVEX:
9265 case X86::KMOVWkm:
9266 case X86::KMOVWkm_EVEX:
9267 case X86::KMOVDkm:
9268 case X86::KMOVDkm_EVEX:
9269 case X86::KMOVQkm:
9270 case X86::KMOVQkm_EVEX:
9271 return true;
9272 }
9273 };
9274
9275 if (!IsLoadOpcode(Load1->getMachineOpcode()) ||
9276 !IsLoadOpcode(Load2->getMachineOpcode()))
9277 return false;
9278
9279 // Lambda to check if both the loads have the same value for an operand index.
9280 auto HasSameOp = [&](int I) {
9281 return Load1->getOperand(I) == Load2->getOperand(I);
9282 };
9283
9284 // All operands except the displacement should match.
9285 if (!HasSameOp(X86::AddrBaseReg) || !HasSameOp(X86::AddrScaleAmt) ||
9286 !HasSameOp(X86::AddrIndexReg) || !HasSameOp(X86::AddrSegmentReg))
9287 return false;
9288
9289 // Chain Operand must be the same.
9290 if (!HasSameOp(5))
9291 return false;
9292
9293 // Now let's examine if the displacements are constants.
9296 if (!Disp1 || !Disp2)
9297 return false;
9298
9299 Offset1 = Disp1->getSExtValue();
9300 Offset2 = Disp2->getSExtValue();
9301 return true;
9302}
9303
9305 int64_t Offset1, int64_t Offset2,
9306 unsigned NumLoads) const {
9307 assert(Offset2 > Offset1);
9308 if ((Offset2 - Offset1) / 8 > 64)
9309 return false;
9310
9311 unsigned Opc1 = Load1->getMachineOpcode();
9312 unsigned Opc2 = Load2->getMachineOpcode();
9313 if (Opc1 != Opc2)
9314 return false; // FIXME: overly conservative?
9315
9316 switch (Opc1) {
9317 default:
9318 break;
9319 case X86::LD_Fp32m:
9320 case X86::LD_Fp64m:
9321 case X86::LD_Fp80m:
9322 case X86::MMX_MOVD64rm:
9323 case X86::MMX_MOVQ64rm:
9324 return false;
9325 }
9326
9327 EVT VT = Load1->getValueType(0);
9328 switch (VT.getSimpleVT().SimpleTy) {
9329 default:
9330 // XMM registers. In 64-bit mode we can be a bit more aggressive since we
9331 // have 16 of them to play with.
9332 if (Subtarget.is64Bit()) {
9333 if (NumLoads >= 3)
9334 return false;
9335 } else if (NumLoads) {
9336 return false;
9337 }
9338 break;
9339 case MVT::i8:
9340 case MVT::i16:
9341 case MVT::i32:
9342 case MVT::i64:
9343 case MVT::f32:
9344 case MVT::f64:
9345 if (NumLoads)
9346 return false;
9347 break;
9348 }
9349
9350 return true;
9351}
9352
9354 const MachineBasicBlock *MBB,
9355 const MachineFunction &MF) const {
9356
9357 // ENDBR instructions should not be scheduled around.
9358 unsigned Opcode = MI.getOpcode();
9359 if (Opcode == X86::ENDBR64 || Opcode == X86::ENDBR32 ||
9360 Opcode == X86::PLDTILECFGV)
9361 return true;
9362
9363 // Frame setup and destroy can't be scheduled around.
9364 if (MI.getFlag(MachineInstr::FrameSetup) ||
9366 return true;
9367
9369}
9370
9373 assert(Cond.size() == 1 && "Invalid X86 branch condition!");
9374 X86::CondCode CC = static_cast<X86::CondCode>(Cond[0].getImm());
9375 Cond[0].setImm(GetOppositeBranchCondition(CC));
9376 return false;
9377}
9378
9380 const TargetRegisterClass *RC) const {
9381 // FIXME: Return false for x87 stack register classes for now. We can't
9382 // allow any loads of these registers before FpGet_ST0_80.
9383 return !(RC == &X86::CCRRegClass || RC == &X86::DFCCRRegClass ||
9384 RC == &X86::RFP32RegClass || RC == &X86::RFP64RegClass ||
9385 RC == &X86::RFP80RegClass);
9386}
9387
9388/// Return a virtual register initialized with the
9389/// the global base register value. Output instructions required to
9390/// initialize the register in the function entry block, if necessary.
9391///
9392/// TODO: Eliminate this and move the code to X86MachineFunctionInfo.
9393///
9396 Register GlobalBaseReg = X86FI->getGlobalBaseReg();
9397 if (GlobalBaseReg)
9398 return GlobalBaseReg;
9399
9400 // Create the register. The code to initialize it is inserted
9401 // later, by the CGBR pass (below).
9402 MachineRegisterInfo &RegInfo = MF->getRegInfo();
9403 GlobalBaseReg = RegInfo.createVirtualRegister(
9404 Subtarget.is64Bit() ? &X86::GR64_NOSPRegClass : &X86::GR32_NOSPRegClass);
9405 X86FI->setGlobalBaseReg(GlobalBaseReg);
9406 return GlobalBaseReg;
9407}
9408
9409// FIXME: Some shuffle and unpack instructions have equivalents in different
9410// domains, but they require a bit more work than just switching opcodes.
9411
9412static const uint16_t *lookup(unsigned opcode, unsigned domain,
9413 ArrayRef<uint16_t[3]> Table) {
9414 for (const uint16_t(&Row)[3] : Table)
9415 if (Row[domain - 1] == opcode)
9416 return Row;
9417 return nullptr;
9418}
9419
9420static const uint16_t *lookupAVX512(unsigned opcode, unsigned domain,
9421 ArrayRef<uint16_t[4]> Table) {
9422 // If this is the integer domain make sure to check both integer columns.
9423 for (const uint16_t(&Row)[4] : Table)
9424 if (Row[domain - 1] == opcode || (domain == 3 && Row[3] == opcode))
9425 return Row;
9426 return nullptr;
9427}
9428
9429// Helper to attempt to widen/narrow blend masks.
9430static bool AdjustBlendMask(unsigned OldMask, unsigned OldWidth,
9431 unsigned NewWidth, unsigned *pNewMask = nullptr) {
9432 assert(((OldWidth % NewWidth) == 0 || (NewWidth % OldWidth) == 0) &&
9433 "Illegal blend mask scale");
9434 unsigned NewMask = 0;
9435
9436 if ((OldWidth % NewWidth) == 0) {
9437 unsigned Scale = OldWidth / NewWidth;
9438 unsigned SubMask = (1u << Scale) - 1;
9439 for (unsigned i = 0; i != NewWidth; ++i) {
9440 unsigned Sub = (OldMask >> (i * Scale)) & SubMask;
9441 if (Sub == SubMask)
9442 NewMask |= (1u << i);
9443 else if (Sub != 0x0)
9444 return false;
9445 }
9446 } else {
9447 unsigned Scale = NewWidth / OldWidth;
9448 unsigned SubMask = (1u << Scale) - 1;
9449 for (unsigned i = 0; i != OldWidth; ++i) {
9450 if (OldMask & (1 << i)) {
9451 NewMask |= (SubMask << (i * Scale));
9452 }
9453 }
9454 }
9455
9456 if (pNewMask)
9457 *pNewMask = NewMask;
9458 return true;
9459}
9460
9462 unsigned Opcode = MI.getOpcode();
9463 unsigned NumOperands = MI.getDesc().getNumOperands();
9464
9465 auto GetBlendDomains = [&](unsigned ImmWidth, bool Is256) {
9466 uint16_t validDomains = 0;
9467 if (MI.getOperand(NumOperands - 1).isImm()) {
9468 unsigned Imm = MI.getOperand(NumOperands - 1).getImm();
9469 if (AdjustBlendMask(Imm, ImmWidth, Is256 ? 8 : 4))
9470 validDomains |= 0x2; // PackedSingle
9471 if (AdjustBlendMask(Imm, ImmWidth, Is256 ? 4 : 2))
9472 validDomains |= 0x4; // PackedDouble
9473 if (!Is256 || Subtarget.hasAVX2())
9474 validDomains |= 0x8; // PackedInt
9475 }
9476 return validDomains;
9477 };
9478
9479 switch (Opcode) {
9480 case X86::BLENDPDrmi:
9481 case X86::BLENDPDrri:
9482 case X86::VBLENDPDrmi:
9483 case X86::VBLENDPDrri:
9484 return GetBlendDomains(2, false);
9485 case X86::VBLENDPDYrmi:
9486 case X86::VBLENDPDYrri:
9487 return GetBlendDomains(4, true);
9488 case X86::BLENDPSrmi:
9489 case X86::BLENDPSrri:
9490 case X86::VBLENDPSrmi:
9491 case X86::VBLENDPSrri:
9492 case X86::VPBLENDDrmi:
9493 case X86::VPBLENDDrri:
9494 return GetBlendDomains(4, false);
9495 case X86::VBLENDPSYrmi:
9496 case X86::VBLENDPSYrri:
9497 case X86::VPBLENDDYrmi:
9498 case X86::VPBLENDDYrri:
9499 return GetBlendDomains(8, true);
9500 case X86::PBLENDWrmi:
9501 case X86::PBLENDWrri:
9502 case X86::VPBLENDWrmi:
9503 case X86::VPBLENDWrri:
9504 // Treat VPBLENDWY as a 128-bit vector as it repeats the lo/hi masks.
9505 case X86::VPBLENDWYrmi:
9506 case X86::VPBLENDWYrri:
9507 return GetBlendDomains(8, false);
9508 case X86::VPANDDZ128rr:
9509 case X86::VPANDDZ128rm:
9510 case X86::VPANDDZ256rr:
9511 case X86::VPANDDZ256rm:
9512 case X86::VPANDQZ128rr:
9513 case X86::VPANDQZ128rm:
9514 case X86::VPANDQZ256rr:
9515 case X86::VPANDQZ256rm:
9516 case X86::VPANDNDZ128rr:
9517 case X86::VPANDNDZ128rm:
9518 case X86::VPANDNDZ256rr:
9519 case X86::VPANDNDZ256rm:
9520 case X86::VPANDNQZ128rr:
9521 case X86::VPANDNQZ128rm:
9522 case X86::VPANDNQZ256rr:
9523 case X86::VPANDNQZ256rm:
9524 case X86::VPORDZ128rr:
9525 case X86::VPORDZ128rm:
9526 case X86::VPORDZ256rr:
9527 case X86::VPORDZ256rm:
9528 case X86::VPORQZ128rr:
9529 case X86::VPORQZ128rm:
9530 case X86::VPORQZ256rr:
9531 case X86::VPORQZ256rm:
9532 case X86::VPXORDZ128rr:
9533 case X86::VPXORDZ128rm:
9534 case X86::VPXORDZ256rr:
9535 case X86::VPXORDZ256rm:
9536 case X86::VPXORQZ128rr:
9537 case X86::VPXORQZ128rm:
9538 case X86::VPXORQZ256rr:
9539 case X86::VPXORQZ256rm:
9540 // If we don't have DQI see if we can still switch from an EVEX integer
9541 // instruction to a VEX floating point instruction.
9542 if (Subtarget.hasDQI())
9543 return 0;
9544
9545 if (RI.getEncodingValue(MI.getOperand(0).getReg()) >= 16)
9546 return 0;
9547 if (RI.getEncodingValue(MI.getOperand(1).getReg()) >= 16)
9548 return 0;
9549 // Register forms will have 3 operands. Memory form will have more.
9550 if (NumOperands == 3 &&
9551 RI.getEncodingValue(MI.getOperand(2).getReg()) >= 16)
9552 return 0;
9553
9554 // All domains are valid.
9555 return 0xe;
9556 case X86::MOVHLPSrr:
9557 // We can swap domains when both inputs are the same register.
9558 // FIXME: This doesn't catch all the cases we would like. If the input
9559 // register isn't KILLed by the instruction, the two address instruction
9560 // pass puts a COPY on one input. The other input uses the original
9561 // register. This prevents the same physical register from being used by
9562 // both inputs.
9563 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg() &&
9564 MI.getOperand(0).getSubReg() == 0 &&
9565 MI.getOperand(1).getSubReg() == 0 && MI.getOperand(2).getSubReg() == 0)
9566 return 0x6;
9567 return 0;
9568 case X86::SHUFPDrri:
9569 return 0x6;
9570 }
9571 return 0;
9572}
9573
9574#include "X86ReplaceableInstrs.def"
9575
9577 unsigned Domain) const {
9578 assert(Domain > 0 && Domain < 4 && "Invalid execution domain");
9579 uint16_t dom = (MI.getDesc().TSFlags >> X86II::SSEDomainShift) & 3;
9580 assert(dom && "Not an SSE instruction");
9581
9582 unsigned Opcode = MI.getOpcode();
9583 unsigned NumOperands = MI.getDesc().getNumOperands();
9584
9585 auto SetBlendDomain = [&](unsigned ImmWidth, bool Is256) {
9586 if (MI.getOperand(NumOperands - 1).isImm()) {
9587 unsigned Imm = MI.getOperand(NumOperands - 1).getImm() & 255;
9588 Imm = (ImmWidth == 16 ? ((Imm << 8) | Imm) : Imm);
9589 unsigned NewImm = Imm;
9590
9591 const uint16_t *table = lookup(Opcode, dom, ReplaceableBlendInstrs);
9592 if (!table)
9593 table = lookup(Opcode, dom, ReplaceableBlendAVX2Instrs);
9594
9595 if (Domain == 1) { // PackedSingle
9596 AdjustBlendMask(Imm, ImmWidth, Is256 ? 8 : 4, &NewImm);
9597 } else if (Domain == 2) { // PackedDouble
9598 AdjustBlendMask(Imm, ImmWidth, Is256 ? 4 : 2, &NewImm);
9599 } else if (Domain == 3) { // PackedInt
9600 if (Subtarget.hasAVX2()) {
9601 // If we are already VPBLENDW use that, else use VPBLENDD.
9602 if ((ImmWidth / (Is256 ? 2 : 1)) != 8) {
9603 table = lookup(Opcode, dom, ReplaceableBlendAVX2Instrs);
9604 AdjustBlendMask(Imm, ImmWidth, Is256 ? 8 : 4, &NewImm);
9605 }
9606 } else {
9607 assert(!Is256 && "128-bit vector expected");
9608 AdjustBlendMask(Imm, ImmWidth, 8, &NewImm);
9609 }
9610 }
9611
9612 assert(table && table[Domain - 1] && "Unknown domain op");
9613 MI.setDesc(get(table[Domain - 1]));
9614 MI.getOperand(NumOperands - 1).setImm(NewImm & 255);
9615 }
9616 return true;
9617 };
9618
9619 switch (Opcode) {
9620 case X86::BLENDPDrmi:
9621 case X86::BLENDPDrri:
9622 case X86::VBLENDPDrmi:
9623 case X86::VBLENDPDrri:
9624 return SetBlendDomain(2, false);
9625 case X86::VBLENDPDYrmi:
9626 case X86::VBLENDPDYrri:
9627 return SetBlendDomain(4, true);
9628 case X86::BLENDPSrmi:
9629 case X86::BLENDPSrri:
9630 case X86::VBLENDPSrmi:
9631 case X86::VBLENDPSrri:
9632 case X86::VPBLENDDrmi:
9633 case X86::VPBLENDDrri:
9634 return SetBlendDomain(4, false);
9635 case X86::VBLENDPSYrmi:
9636 case X86::VBLENDPSYrri:
9637 case X86::VPBLENDDYrmi:
9638 case X86::VPBLENDDYrri:
9639 return SetBlendDomain(8, true);
9640 case X86::PBLENDWrmi:
9641 case X86::PBLENDWrri:
9642 case X86::VPBLENDWrmi:
9643 case X86::VPBLENDWrri:
9644 return SetBlendDomain(8, false);
9645 case X86::VPBLENDWYrmi:
9646 case X86::VPBLENDWYrri:
9647 return SetBlendDomain(16, true);
9648 case X86::VPANDDZ128rr:
9649 case X86::VPANDDZ128rm:
9650 case X86::VPANDDZ256rr:
9651 case X86::VPANDDZ256rm:
9652 case X86::VPANDQZ128rr:
9653 case X86::VPANDQZ128rm:
9654 case X86::VPANDQZ256rr:
9655 case X86::VPANDQZ256rm:
9656 case X86::VPANDNDZ128rr:
9657 case X86::VPANDNDZ128rm:
9658 case X86::VPANDNDZ256rr:
9659 case X86::VPANDNDZ256rm:
9660 case X86::VPANDNQZ128rr:
9661 case X86::VPANDNQZ128rm:
9662 case X86::VPANDNQZ256rr:
9663 case X86::VPANDNQZ256rm:
9664 case X86::VPORDZ128rr:
9665 case X86::VPORDZ128rm:
9666 case X86::VPORDZ256rr:
9667 case X86::VPORDZ256rm:
9668 case X86::VPORQZ128rr:
9669 case X86::VPORQZ128rm:
9670 case X86::VPORQZ256rr:
9671 case X86::VPORQZ256rm:
9672 case X86::VPXORDZ128rr:
9673 case X86::VPXORDZ128rm:
9674 case X86::VPXORDZ256rr:
9675 case X86::VPXORDZ256rm:
9676 case X86::VPXORQZ128rr:
9677 case X86::VPXORQZ128rm:
9678 case X86::VPXORQZ256rr:
9679 case X86::VPXORQZ256rm: {
9680 // Without DQI, convert EVEX instructions to VEX instructions.
9681 if (Subtarget.hasDQI())
9682 return false;
9683
9684 const uint16_t *table =
9685 lookupAVX512(MI.getOpcode(), dom, ReplaceableCustomAVX512LogicInstrs);
9686 assert(table && "Instruction not found in table?");
9687 // Don't change integer Q instructions to D instructions and
9688 // use D intructions if we started with a PS instruction.
9689 if (Domain == 3 && (dom == 1 || table[3] == MI.getOpcode()))
9690 Domain = 4;
9691 MI.setDesc(get(table[Domain - 1]));
9692 return true;
9693 }
9694 case X86::UNPCKHPDrr:
9695 case X86::MOVHLPSrr:
9696 // We just need to commute the instruction which will switch the domains.
9697 if (Domain != dom && Domain != 3 &&
9698 MI.getOperand(1).getReg() == MI.getOperand(2).getReg() &&
9699 MI.getOperand(0).getSubReg() == 0 &&
9700 MI.getOperand(1).getSubReg() == 0 &&
9701 MI.getOperand(2).getSubReg() == 0) {
9702 commuteInstruction(MI, false);
9703 return true;
9704 }
9705 // We must always return true for MOVHLPSrr.
9706 if (Opcode == X86::MOVHLPSrr)
9707 return true;
9708 break;
9709 case X86::SHUFPDrri: {
9710 if (Domain == 1) {
9711 unsigned Imm = MI.getOperand(3).getImm();
9712 unsigned NewImm = 0x44;
9713 if (Imm & 1)
9714 NewImm |= 0x0a;
9715 if (Imm & 2)
9716 NewImm |= 0xa0;
9717 MI.getOperand(3).setImm(NewImm);
9718 MI.setDesc(get(X86::SHUFPSrri));
9719 }
9720 return true;
9721 }
9722 }
9723 return false;
9724}
9725
9726std::pair<uint16_t, uint16_t>
9728 uint16_t domain = (MI.getDesc().TSFlags >> X86II::SSEDomainShift) & 3;
9729 unsigned opcode = MI.getOpcode();
9730 uint16_t validDomains = 0;
9731 if (domain) {
9732 // Attempt to match for custom instructions.
9733 validDomains = getExecutionDomainCustom(MI);
9734 if (validDomains)
9735 return std::make_pair(domain, validDomains);
9736
9737 if (lookup(opcode, domain, ReplaceableInstrs)) {
9738 validDomains = 0xe;
9739 } else if (lookup(opcode, domain, ReplaceableInstrsAVX2)) {
9740 validDomains = Subtarget.hasAVX2() ? 0xe : 0x6;
9741 } else if (lookup(opcode, domain, ReplaceableInstrsFP)) {
9742 validDomains = 0x6;
9743 } else if (lookup(opcode, domain, ReplaceableInstrsAVX2InsertExtract)) {
9744 // Insert/extract instructions should only effect domain if AVX2
9745 // is enabled.
9746 if (!Subtarget.hasAVX2())
9747 return std::make_pair(0, 0);
9748 validDomains = 0xe;
9749 } else if (lookupAVX512(opcode, domain, ReplaceableInstrsAVX512)) {
9750 validDomains = 0xe;
9751 } else if (Subtarget.hasDQI() &&
9752 lookupAVX512(opcode, domain, ReplaceableInstrsAVX512DQ)) {
9753 validDomains = 0xe;
9754 } else if (Subtarget.hasDQI()) {
9755 if (const uint16_t *table =
9756 lookupAVX512(opcode, domain, ReplaceableInstrsAVX512DQMasked)) {
9757 if (domain == 1 || (domain == 3 && table[3] == opcode))
9758 validDomains = 0xa;
9759 else
9760 validDomains = 0xc;
9761 }
9762 }
9763 }
9764 return std::make_pair(domain, validDomains);
9765}
9766
9768 assert(Domain > 0 && Domain < 4 && "Invalid execution domain");
9769 uint16_t dom = (MI.getDesc().TSFlags >> X86II::SSEDomainShift) & 3;
9770 assert(dom && "Not an SSE instruction");
9771
9772 // Attempt to match for custom instructions.
9774 return;
9775
9776 const uint16_t *table = lookup(MI.getOpcode(), dom, ReplaceableInstrs);
9777 if (!table) { // try the other table
9778 assert((Subtarget.hasAVX2() || Domain < 3) &&
9779 "256-bit vector operations only available in AVX2");
9780 table = lookup(MI.getOpcode(), dom, ReplaceableInstrsAVX2);
9781 }
9782 if (!table) { // try the FP table
9783 table = lookup(MI.getOpcode(), dom, ReplaceableInstrsFP);
9784 assert((!table || Domain < 3) &&
9785 "Can only select PackedSingle or PackedDouble");
9786 }
9787 if (!table) { // try the other table
9788 assert(Subtarget.hasAVX2() &&
9789 "256-bit insert/extract only available in AVX2");
9790 table = lookup(MI.getOpcode(), dom, ReplaceableInstrsAVX2InsertExtract);
9791 }
9792 if (!table) { // try the AVX512 table
9793 assert(Subtarget.hasAVX512() && "Requires AVX-512");
9794 table = lookupAVX512(MI.getOpcode(), dom, ReplaceableInstrsAVX512);
9795 // Don't change integer Q instructions to D instructions.
9796 if (table && Domain == 3 && table[3] == MI.getOpcode())
9797 Domain = 4;
9798 }
9799 if (!table) { // try the AVX512DQ table
9800 assert((Subtarget.hasDQI() || Domain >= 3) && "Requires AVX-512DQ");
9801 table = lookupAVX512(MI.getOpcode(), dom, ReplaceableInstrsAVX512DQ);
9802 // Don't change integer Q instructions to D instructions and
9803 // use D instructions if we started with a PS instruction.
9804 if (table && Domain == 3 && (dom == 1 || table[3] == MI.getOpcode()))
9805 Domain = 4;
9806 }
9807 if (!table) { // try the AVX512DQMasked table
9808 assert((Subtarget.hasDQI() || Domain >= 3) && "Requires AVX-512DQ");
9809 table = lookupAVX512(MI.getOpcode(), dom, ReplaceableInstrsAVX512DQMasked);
9810 if (table && Domain == 3 && (dom == 1 || table[3] == MI.getOpcode()))
9811 Domain = 4;
9812 }
9813 assert(table && "Cannot change domain");
9814 MI.setDesc(get(table[Domain - 1]));
9815}
9816
9822
9823/// Return the noop instruction to use for a noop.
9825 MCInst Nop;
9826 Nop.setOpcode(X86::NOOP);
9827 return Nop;
9828}
9829
9831 switch (opc) {
9832 default:
9833 return false;
9834 case X86::DIVPDrm:
9835 case X86::DIVPDrr:
9836 case X86::DIVPSrm:
9837 case X86::DIVPSrr:
9838 case X86::DIVSDrm:
9839 case X86::DIVSDrm_Int:
9840 case X86::DIVSDrr:
9841 case X86::DIVSDrr_Int:
9842 case X86::DIVSSrm:
9843 case X86::DIVSSrm_Int:
9844 case X86::DIVSSrr:
9845 case X86::DIVSSrr_Int:
9846 case X86::SQRTPDm:
9847 case X86::SQRTPDr:
9848 case X86::SQRTPSm:
9849 case X86::SQRTPSr:
9850 case X86::SQRTSDm:
9851 case X86::SQRTSDm_Int:
9852 case X86::SQRTSDr:
9853 case X86::SQRTSDr_Int:
9854 case X86::SQRTSSm:
9855 case X86::SQRTSSm_Int:
9856 case X86::SQRTSSr:
9857 case X86::SQRTSSr_Int:
9858 // AVX instructions with high latency
9859 case X86::VDIVPDrm:
9860 case X86::VDIVPDrr:
9861 case X86::VDIVPDYrm:
9862 case X86::VDIVPDYrr:
9863 case X86::VDIVPSrm:
9864 case X86::VDIVPSrr:
9865 case X86::VDIVPSYrm:
9866 case X86::VDIVPSYrr:
9867 case X86::VDIVSDrm:
9868 case X86::VDIVSDrm_Int:
9869 case X86::VDIVSDrr:
9870 case X86::VDIVSDrr_Int:
9871 case X86::VDIVSSrm:
9872 case X86::VDIVSSrm_Int:
9873 case X86::VDIVSSrr:
9874 case X86::VDIVSSrr_Int:
9875 case X86::VSQRTPDm:
9876 case X86::VSQRTPDr:
9877 case X86::VSQRTPDYm:
9878 case X86::VSQRTPDYr:
9879 case X86::VSQRTPSm:
9880 case X86::VSQRTPSr:
9881 case X86::VSQRTPSYm:
9882 case X86::VSQRTPSYr:
9883 case X86::VSQRTSDm:
9884 case X86::VSQRTSDm_Int:
9885 case X86::VSQRTSDr:
9886 case X86::VSQRTSDr_Int:
9887 case X86::VSQRTSSm:
9888 case X86::VSQRTSSm_Int:
9889 case X86::VSQRTSSr:
9890 case X86::VSQRTSSr_Int:
9891 // AVX512 instructions with high latency
9892 case X86::VDIVPDZ128rm:
9893 case X86::VDIVPDZ128rmb:
9894 case X86::VDIVPDZ128rmbk:
9895 case X86::VDIVPDZ128rmbkz:
9896 case X86::VDIVPDZ128rmk:
9897 case X86::VDIVPDZ128rmkz:
9898 case X86::VDIVPDZ128rr:
9899 case X86::VDIVPDZ128rrk:
9900 case X86::VDIVPDZ128rrkz:
9901 case X86::VDIVPDZ256rm:
9902 case X86::VDIVPDZ256rmb:
9903 case X86::VDIVPDZ256rmbk:
9904 case X86::VDIVPDZ256rmbkz:
9905 case X86::VDIVPDZ256rmk:
9906 case X86::VDIVPDZ256rmkz:
9907 case X86::VDIVPDZ256rr:
9908 case X86::VDIVPDZ256rrk:
9909 case X86::VDIVPDZ256rrkz:
9910 case X86::VDIVPDZrrb:
9911 case X86::VDIVPDZrrbk:
9912 case X86::VDIVPDZrrbkz:
9913 case X86::VDIVPDZrm:
9914 case X86::VDIVPDZrmb:
9915 case X86::VDIVPDZrmbk:
9916 case X86::VDIVPDZrmbkz:
9917 case X86::VDIVPDZrmk:
9918 case X86::VDIVPDZrmkz:
9919 case X86::VDIVPDZrr:
9920 case X86::VDIVPDZrrk:
9921 case X86::VDIVPDZrrkz:
9922 case X86::VDIVPSZ128rm:
9923 case X86::VDIVPSZ128rmb:
9924 case X86::VDIVPSZ128rmbk:
9925 case X86::VDIVPSZ128rmbkz:
9926 case X86::VDIVPSZ128rmk:
9927 case X86::VDIVPSZ128rmkz:
9928 case X86::VDIVPSZ128rr:
9929 case X86::VDIVPSZ128rrk:
9930 case X86::VDIVPSZ128rrkz:
9931 case X86::VDIVPSZ256rm:
9932 case X86::VDIVPSZ256rmb:
9933 case X86::VDIVPSZ256rmbk:
9934 case X86::VDIVPSZ256rmbkz:
9935 case X86::VDIVPSZ256rmk:
9936 case X86::VDIVPSZ256rmkz:
9937 case X86::VDIVPSZ256rr:
9938 case X86::VDIVPSZ256rrk:
9939 case X86::VDIVPSZ256rrkz:
9940 case X86::VDIVPSZrrb:
9941 case X86::VDIVPSZrrbk:
9942 case X86::VDIVPSZrrbkz:
9943 case X86::VDIVPSZrm:
9944 case X86::VDIVPSZrmb:
9945 case X86::VDIVPSZrmbk:
9946 case X86::VDIVPSZrmbkz:
9947 case X86::VDIVPSZrmk:
9948 case X86::VDIVPSZrmkz:
9949 case X86::VDIVPSZrr:
9950 case X86::VDIVPSZrrk:
9951 case X86::VDIVPSZrrkz:
9952 case X86::VDIVSDZrm:
9953 case X86::VDIVSDZrr:
9954 case X86::VDIVSDZrm_Int:
9955 case X86::VDIVSDZrmk_Int:
9956 case X86::VDIVSDZrmkz_Int:
9957 case X86::VDIVSDZrr_Int:
9958 case X86::VDIVSDZrrk_Int:
9959 case X86::VDIVSDZrrkz_Int:
9960 case X86::VDIVSDZrrb_Int:
9961 case X86::VDIVSDZrrbk_Int:
9962 case X86::VDIVSDZrrbkz_Int:
9963 case X86::VDIVSSZrm:
9964 case X86::VDIVSSZrr:
9965 case X86::VDIVSSZrm_Int:
9966 case X86::VDIVSSZrmk_Int:
9967 case X86::VDIVSSZrmkz_Int:
9968 case X86::VDIVSSZrr_Int:
9969 case X86::VDIVSSZrrk_Int:
9970 case X86::VDIVSSZrrkz_Int:
9971 case X86::VDIVSSZrrb_Int:
9972 case X86::VDIVSSZrrbk_Int:
9973 case X86::VDIVSSZrrbkz_Int:
9974 case X86::VSQRTPDZ128m:
9975 case X86::VSQRTPDZ128mb:
9976 case X86::VSQRTPDZ128mbk:
9977 case X86::VSQRTPDZ128mbkz:
9978 case X86::VSQRTPDZ128mk:
9979 case X86::VSQRTPDZ128mkz:
9980 case X86::VSQRTPDZ128r:
9981 case X86::VSQRTPDZ128rk:
9982 case X86::VSQRTPDZ128rkz:
9983 case X86::VSQRTPDZ256m:
9984 case X86::VSQRTPDZ256mb:
9985 case X86::VSQRTPDZ256mbk:
9986 case X86::VSQRTPDZ256mbkz:
9987 case X86::VSQRTPDZ256mk:
9988 case X86::VSQRTPDZ256mkz:
9989 case X86::VSQRTPDZ256r:
9990 case X86::VSQRTPDZ256rk:
9991 case X86::VSQRTPDZ256rkz:
9992 case X86::VSQRTPDZm:
9993 case X86::VSQRTPDZmb:
9994 case X86::VSQRTPDZmbk:
9995 case X86::VSQRTPDZmbkz:
9996 case X86::VSQRTPDZmk:
9997 case X86::VSQRTPDZmkz:
9998 case X86::VSQRTPDZr:
9999 case X86::VSQRTPDZrb:
10000 case X86::VSQRTPDZrbk:
10001 case X86::VSQRTPDZrbkz:
10002 case X86::VSQRTPDZrk:
10003 case X86::VSQRTPDZrkz:
10004 case X86::VSQRTPSZ128m:
10005 case X86::VSQRTPSZ128mb:
10006 case X86::VSQRTPSZ128mbk:
10007 case X86::VSQRTPSZ128mbkz:
10008 case X86::VSQRTPSZ128mk:
10009 case X86::VSQRTPSZ128mkz:
10010 case X86::VSQRTPSZ128r:
10011 case X86::VSQRTPSZ128rk:
10012 case X86::VSQRTPSZ128rkz:
10013 case X86::VSQRTPSZ256m:
10014 case X86::VSQRTPSZ256mb:
10015 case X86::VSQRTPSZ256mbk:
10016 case X86::VSQRTPSZ256mbkz:
10017 case X86::VSQRTPSZ256mk:
10018 case X86::VSQRTPSZ256mkz:
10019 case X86::VSQRTPSZ256r:
10020 case X86::VSQRTPSZ256rk:
10021 case X86::VSQRTPSZ256rkz:
10022 case X86::VSQRTPSZm:
10023 case X86::VSQRTPSZmb:
10024 case X86::VSQRTPSZmbk:
10025 case X86::VSQRTPSZmbkz:
10026 case X86::VSQRTPSZmk:
10027 case X86::VSQRTPSZmkz:
10028 case X86::VSQRTPSZr:
10029 case X86::VSQRTPSZrb:
10030 case X86::VSQRTPSZrbk:
10031 case X86::VSQRTPSZrbkz:
10032 case X86::VSQRTPSZrk:
10033 case X86::VSQRTPSZrkz:
10034 case X86::VSQRTSDZm:
10035 case X86::VSQRTSDZm_Int:
10036 case X86::VSQRTSDZmk_Int:
10037 case X86::VSQRTSDZmkz_Int:
10038 case X86::VSQRTSDZr:
10039 case X86::VSQRTSDZr_Int:
10040 case X86::VSQRTSDZrk_Int:
10041 case X86::VSQRTSDZrkz_Int:
10042 case X86::VSQRTSDZrb_Int:
10043 case X86::VSQRTSDZrbk_Int:
10044 case X86::VSQRTSDZrbkz_Int:
10045 case X86::VSQRTSSZm:
10046 case X86::VSQRTSSZm_Int:
10047 case X86::VSQRTSSZmk_Int:
10048 case X86::VSQRTSSZmkz_Int:
10049 case X86::VSQRTSSZr:
10050 case X86::VSQRTSSZr_Int:
10051 case X86::VSQRTSSZrk_Int:
10052 case X86::VSQRTSSZrkz_Int:
10053 case X86::VSQRTSSZrb_Int:
10054 case X86::VSQRTSSZrbk_Int:
10055 case X86::VSQRTSSZrbkz_Int:
10056
10057 case X86::VGATHERDPDYrm:
10058 case X86::VGATHERDPDZ128rm:
10059 case X86::VGATHERDPDZ256rm:
10060 case X86::VGATHERDPDZrm:
10061 case X86::VGATHERDPDrm:
10062 case X86::VGATHERDPSYrm:
10063 case X86::VGATHERDPSZ128rm:
10064 case X86::VGATHERDPSZ256rm:
10065 case X86::VGATHERDPSZrm:
10066 case X86::VGATHERDPSrm:
10067 case X86::VGATHERPF0DPDm:
10068 case X86::VGATHERPF0DPSm:
10069 case X86::VGATHERPF0QPDm:
10070 case X86::VGATHERPF0QPSm:
10071 case X86::VGATHERPF1DPDm:
10072 case X86::VGATHERPF1DPSm:
10073 case X86::VGATHERPF1QPDm:
10074 case X86::VGATHERPF1QPSm:
10075 case X86::VGATHERQPDYrm:
10076 case X86::VGATHERQPDZ128rm:
10077 case X86::VGATHERQPDZ256rm:
10078 case X86::VGATHERQPDZrm:
10079 case X86::VGATHERQPDrm:
10080 case X86::VGATHERQPSYrm:
10081 case X86::VGATHERQPSZ128rm:
10082 case X86::VGATHERQPSZ256rm:
10083 case X86::VGATHERQPSZrm:
10084 case X86::VGATHERQPSrm:
10085 case X86::VPGATHERDDYrm:
10086 case X86::VPGATHERDDZ128rm:
10087 case X86::VPGATHERDDZ256rm:
10088 case X86::VPGATHERDDZrm:
10089 case X86::VPGATHERDDrm:
10090 case X86::VPGATHERDQYrm:
10091 case X86::VPGATHERDQZ128rm:
10092 case X86::VPGATHERDQZ256rm:
10093 case X86::VPGATHERDQZrm:
10094 case X86::VPGATHERDQrm:
10095 case X86::VPGATHERQDYrm:
10096 case X86::VPGATHERQDZ128rm:
10097 case X86::VPGATHERQDZ256rm:
10098 case X86::VPGATHERQDZrm:
10099 case X86::VPGATHERQDrm:
10100 case X86::VPGATHERQQYrm:
10101 case X86::VPGATHERQQZ128rm:
10102 case X86::VPGATHERQQZ256rm:
10103 case X86::VPGATHERQQZrm:
10104 case X86::VPGATHERQQrm:
10105 case X86::VSCATTERDPDZ128mr:
10106 case X86::VSCATTERDPDZ256mr:
10107 case X86::VSCATTERDPDZmr:
10108 case X86::VSCATTERDPSZ128mr:
10109 case X86::VSCATTERDPSZ256mr:
10110 case X86::VSCATTERDPSZmr:
10111 case X86::VSCATTERPF0DPDm:
10112 case X86::VSCATTERPF0DPSm:
10113 case X86::VSCATTERPF0QPDm:
10114 case X86::VSCATTERPF0QPSm:
10115 case X86::VSCATTERPF1DPDm:
10116 case X86::VSCATTERPF1DPSm:
10117 case X86::VSCATTERPF1QPDm:
10118 case X86::VSCATTERPF1QPSm:
10119 case X86::VSCATTERQPDZ128mr:
10120 case X86::VSCATTERQPDZ256mr:
10121 case X86::VSCATTERQPDZmr:
10122 case X86::VSCATTERQPSZ128mr:
10123 case X86::VSCATTERQPSZ256mr:
10124 case X86::VSCATTERQPSZmr:
10125 case X86::VPSCATTERDDZ128mr:
10126 case X86::VPSCATTERDDZ256mr:
10127 case X86::VPSCATTERDDZmr:
10128 case X86::VPSCATTERDQZ128mr:
10129 case X86::VPSCATTERDQZ256mr:
10130 case X86::VPSCATTERDQZmr:
10131 case X86::VPSCATTERQDZ128mr:
10132 case X86::VPSCATTERQDZ256mr:
10133 case X86::VPSCATTERQDZmr:
10134 case X86::VPSCATTERQQZ128mr:
10135 case X86::VPSCATTERQQZ256mr:
10136 case X86::VPSCATTERQQZmr:
10137 return true;
10138 }
10139}
10140
10142 const MachineRegisterInfo *MRI,
10143 const MachineInstr &DefMI,
10144 unsigned DefIdx,
10145 const MachineInstr &UseMI,
10146 unsigned UseIdx) const {
10147 return isHighLatencyDef(DefMI.getOpcode());
10148}
10149
10151 const MachineBasicBlock *MBB) const {
10152 assert(Inst.getNumExplicitOperands() == 3 && Inst.getNumExplicitDefs() == 1 &&
10153 Inst.getNumDefs() <= 2 && "Reassociation needs binary operators");
10154
10155 // Integer binary math/logic instructions have a third source operand:
10156 // the EFLAGS register. That operand must be both defined here and never
10157 // used; ie, it must be dead. If the EFLAGS operand is live, then we can
10158 // not change anything because rearranging the operands could affect other
10159 // instructions that depend on the exact status flags (zero, sign, etc.)
10160 // that are set by using these particular operands with this operation.
10161 const MachineOperand *FlagDef =
10162 Inst.findRegisterDefOperand(X86::EFLAGS, /*TRI=*/nullptr);
10163 assert((Inst.getNumDefs() == 1 || FlagDef) && "Implicit def isn't flags?");
10164 if (FlagDef && !FlagDef->isDead())
10165 return false;
10166
10168}
10169
10170// TODO: There are many more machine instruction opcodes to match:
10171// 1. Other data types (integer, vectors)
10172// 2. Other math / logic operations (xor, or)
10173// 3. Other forms of the same operation (intrinsics and other variants)
10175 bool Invert) const {
10176 if (Invert)
10177 return false;
10178 switch (Inst.getOpcode()) {
10179 CASE_ND(ADD8rr)
10180 CASE_ND(ADD16rr)
10181 CASE_ND(ADD32rr)
10182 CASE_ND(ADD64rr)
10183 CASE_ND(AND8rr)
10184 CASE_ND(AND16rr)
10185 CASE_ND(AND32rr)
10186 CASE_ND(AND64rr)
10187 CASE_ND(OR8rr)
10188 CASE_ND(OR16rr)
10189 CASE_ND(OR32rr)
10190 CASE_ND(OR64rr)
10191 CASE_ND(XOR8rr)
10192 CASE_ND(XOR16rr)
10193 CASE_ND(XOR32rr)
10194 CASE_ND(XOR64rr)
10195 CASE_ND(IMUL16rr)
10196 CASE_ND(IMUL32rr)
10197 CASE_ND(IMUL64rr)
10198 case X86::PANDrr:
10199 case X86::PORrr:
10200 case X86::PXORrr:
10201 case X86::ANDPDrr:
10202 case X86::ANDPSrr:
10203 case X86::ORPDrr:
10204 case X86::ORPSrr:
10205 case X86::XORPDrr:
10206 case X86::XORPSrr:
10207 case X86::PADDBrr:
10208 case X86::PADDWrr:
10209 case X86::PADDDrr:
10210 case X86::PADDQrr:
10211 case X86::PMULLWrr:
10212 case X86::PMULLDrr:
10213 case X86::PMAXSBrr:
10214 case X86::PMAXSDrr:
10215 case X86::PMAXSWrr:
10216 case X86::PMAXUBrr:
10217 case X86::PMAXUDrr:
10218 case X86::PMAXUWrr:
10219 case X86::PMINSBrr:
10220 case X86::PMINSDrr:
10221 case X86::PMINSWrr:
10222 case X86::PMINUBrr:
10223 case X86::PMINUDrr:
10224 case X86::PMINUWrr:
10225 case X86::VPANDrr:
10226 case X86::VPANDYrr:
10227 case X86::VPANDDZ128rr:
10228 case X86::VPANDDZ256rr:
10229 case X86::VPANDDZrr:
10230 case X86::VPANDQZ128rr:
10231 case X86::VPANDQZ256rr:
10232 case X86::VPANDQZrr:
10233 case X86::VPORrr:
10234 case X86::VPORYrr:
10235 case X86::VPORDZ128rr:
10236 case X86::VPORDZ256rr:
10237 case X86::VPORDZrr:
10238 case X86::VPORQZ128rr:
10239 case X86::VPORQZ256rr:
10240 case X86::VPORQZrr:
10241 case X86::VPXORrr:
10242 case X86::VPXORYrr:
10243 case X86::VPXORDZ128rr:
10244 case X86::VPXORDZ256rr:
10245 case X86::VPXORDZrr:
10246 case X86::VPXORQZ128rr:
10247 case X86::VPXORQZ256rr:
10248 case X86::VPXORQZrr:
10249 case X86::VANDPDrr:
10250 case X86::VANDPSrr:
10251 case X86::VANDPDYrr:
10252 case X86::VANDPSYrr:
10253 case X86::VANDPDZ128rr:
10254 case X86::VANDPSZ128rr:
10255 case X86::VANDPDZ256rr:
10256 case X86::VANDPSZ256rr:
10257 case X86::VANDPDZrr:
10258 case X86::VANDPSZrr:
10259 case X86::VORPDrr:
10260 case X86::VORPSrr:
10261 case X86::VORPDYrr:
10262 case X86::VORPSYrr:
10263 case X86::VORPDZ128rr:
10264 case X86::VORPSZ128rr:
10265 case X86::VORPDZ256rr:
10266 case X86::VORPSZ256rr:
10267 case X86::VORPDZrr:
10268 case X86::VORPSZrr:
10269 case X86::VXORPDrr:
10270 case X86::VXORPSrr:
10271 case X86::VXORPDYrr:
10272 case X86::VXORPSYrr:
10273 case X86::VXORPDZ128rr:
10274 case X86::VXORPSZ128rr:
10275 case X86::VXORPDZ256rr:
10276 case X86::VXORPSZ256rr:
10277 case X86::VXORPDZrr:
10278 case X86::VXORPSZrr:
10279 case X86::KADDBkk:
10280 case X86::KADDWkk:
10281 case X86::KADDDkk:
10282 case X86::KADDQkk:
10283 case X86::KANDBkk:
10284 case X86::KANDWkk:
10285 case X86::KANDDkk:
10286 case X86::KANDQkk:
10287 case X86::KORBkk:
10288 case X86::KORWkk:
10289 case X86::KORDkk:
10290 case X86::KORQkk:
10291 case X86::KXORBkk:
10292 case X86::KXORWkk:
10293 case X86::KXORDkk:
10294 case X86::KXORQkk:
10295 case X86::VPADDBrr:
10296 case X86::VPADDWrr:
10297 case X86::VPADDDrr:
10298 case X86::VPADDQrr:
10299 case X86::VPADDBYrr:
10300 case X86::VPADDWYrr:
10301 case X86::VPADDDYrr:
10302 case X86::VPADDQYrr:
10303 case X86::VPADDBZ128rr:
10304 case X86::VPADDWZ128rr:
10305 case X86::VPADDDZ128rr:
10306 case X86::VPADDQZ128rr:
10307 case X86::VPADDBZ256rr:
10308 case X86::VPADDWZ256rr:
10309 case X86::VPADDDZ256rr:
10310 case X86::VPADDQZ256rr:
10311 case X86::VPADDBZrr:
10312 case X86::VPADDWZrr:
10313 case X86::VPADDDZrr:
10314 case X86::VPADDQZrr:
10315 case X86::VPMULLWrr:
10316 case X86::VPMULLWYrr:
10317 case X86::VPMULLWZ128rr:
10318 case X86::VPMULLWZ256rr:
10319 case X86::VPMULLWZrr:
10320 case X86::VPMULLDrr:
10321 case X86::VPMULLDYrr:
10322 case X86::VPMULLDZ128rr:
10323 case X86::VPMULLDZ256rr:
10324 case X86::VPMULLDZrr:
10325 case X86::VPMULLQZ128rr:
10326 case X86::VPMULLQZ256rr:
10327 case X86::VPMULLQZrr:
10328 case X86::VPMAXSBrr:
10329 case X86::VPMAXSBYrr:
10330 case X86::VPMAXSBZ128rr:
10331 case X86::VPMAXSBZ256rr:
10332 case X86::VPMAXSBZrr:
10333 case X86::VPMAXSDrr:
10334 case X86::VPMAXSDYrr:
10335 case X86::VPMAXSDZ128rr:
10336 case X86::VPMAXSDZ256rr:
10337 case X86::VPMAXSDZrr:
10338 case X86::VPMAXSQZ128rr:
10339 case X86::VPMAXSQZ256rr:
10340 case X86::VPMAXSQZrr:
10341 case X86::VPMAXSWrr:
10342 case X86::VPMAXSWYrr:
10343 case X86::VPMAXSWZ128rr:
10344 case X86::VPMAXSWZ256rr:
10345 case X86::VPMAXSWZrr:
10346 case X86::VPMAXUBrr:
10347 case X86::VPMAXUBYrr:
10348 case X86::VPMAXUBZ128rr:
10349 case X86::VPMAXUBZ256rr:
10350 case X86::VPMAXUBZrr:
10351 case X86::VPMAXUDrr:
10352 case X86::VPMAXUDYrr:
10353 case X86::VPMAXUDZ128rr:
10354 case X86::VPMAXUDZ256rr:
10355 case X86::VPMAXUDZrr:
10356 case X86::VPMAXUQZ128rr:
10357 case X86::VPMAXUQZ256rr:
10358 case X86::VPMAXUQZrr:
10359 case X86::VPMAXUWrr:
10360 case X86::VPMAXUWYrr:
10361 case X86::VPMAXUWZ128rr:
10362 case X86::VPMAXUWZ256rr:
10363 case X86::VPMAXUWZrr:
10364 case X86::VPMINSBrr:
10365 case X86::VPMINSBYrr:
10366 case X86::VPMINSBZ128rr:
10367 case X86::VPMINSBZ256rr:
10368 case X86::VPMINSBZrr:
10369 case X86::VPMINSDrr:
10370 case X86::VPMINSDYrr:
10371 case X86::VPMINSDZ128rr:
10372 case X86::VPMINSDZ256rr:
10373 case X86::VPMINSDZrr:
10374 case X86::VPMINSQZ128rr:
10375 case X86::VPMINSQZ256rr:
10376 case X86::VPMINSQZrr:
10377 case X86::VPMINSWrr:
10378 case X86::VPMINSWYrr:
10379 case X86::VPMINSWZ128rr:
10380 case X86::VPMINSWZ256rr:
10381 case X86::VPMINSWZrr:
10382 case X86::VPMINUBrr:
10383 case X86::VPMINUBYrr:
10384 case X86::VPMINUBZ128rr:
10385 case X86::VPMINUBZ256rr:
10386 case X86::VPMINUBZrr:
10387 case X86::VPMINUDrr:
10388 case X86::VPMINUDYrr:
10389 case X86::VPMINUDZ128rr:
10390 case X86::VPMINUDZ256rr:
10391 case X86::VPMINUDZrr:
10392 case X86::VPMINUQZ128rr:
10393 case X86::VPMINUQZ256rr:
10394 case X86::VPMINUQZrr:
10395 case X86::VPMINUWrr:
10396 case X86::VPMINUWYrr:
10397 case X86::VPMINUWZ128rr:
10398 case X86::VPMINUWZ256rr:
10399 case X86::VPMINUWZrr:
10400 // Normal min/max instructions are not commutative because of NaN and signed
10401 // zero semantics, but these are. Thus, there's no need to check for global
10402 // relaxed math; the instructions themselves have the properties we need.
10403 case X86::MAXCPDrr:
10404 case X86::MAXCPSrr:
10405 case X86::MAXCSDrr:
10406 case X86::MAXCSSrr:
10407 case X86::MINCPDrr:
10408 case X86::MINCPSrr:
10409 case X86::MINCSDrr:
10410 case X86::MINCSSrr:
10411 case X86::VMAXCPDrr:
10412 case X86::VMAXCPSrr:
10413 case X86::VMAXCPDYrr:
10414 case X86::VMAXCPSYrr:
10415 case X86::VMAXCPDZ128rr:
10416 case X86::VMAXCPSZ128rr:
10417 case X86::VMAXCPDZ256rr:
10418 case X86::VMAXCPSZ256rr:
10419 case X86::VMAXCPDZrr:
10420 case X86::VMAXCPSZrr:
10421 case X86::VMAXCSDrr:
10422 case X86::VMAXCSSrr:
10423 case X86::VMAXCSDZrr:
10424 case X86::VMAXCSSZrr:
10425 case X86::VMINCPDrr:
10426 case X86::VMINCPSrr:
10427 case X86::VMINCPDYrr:
10428 case X86::VMINCPSYrr:
10429 case X86::VMINCPDZ128rr:
10430 case X86::VMINCPSZ128rr:
10431 case X86::VMINCPDZ256rr:
10432 case X86::VMINCPSZ256rr:
10433 case X86::VMINCPDZrr:
10434 case X86::VMINCPSZrr:
10435 case X86::VMINCSDrr:
10436 case X86::VMINCSSrr:
10437 case X86::VMINCSDZrr:
10438 case X86::VMINCSSZrr:
10439 case X86::VMAXCPHZ128rr:
10440 case X86::VMAXCPHZ256rr:
10441 case X86::VMAXCPHZrr:
10442 case X86::VMAXCSHZrr:
10443 case X86::VMINCPHZ128rr:
10444 case X86::VMINCPHZ256rr:
10445 case X86::VMINCPHZrr:
10446 case X86::VMINCSHZrr:
10447 return true;
10448 case X86::ADDPDrr:
10449 case X86::ADDPSrr:
10450 case X86::ADDSDrr:
10451 case X86::ADDSSrr:
10452 case X86::MULPDrr:
10453 case X86::MULPSrr:
10454 case X86::MULSDrr:
10455 case X86::MULSSrr:
10456 case X86::VADDPDrr:
10457 case X86::VADDPSrr:
10458 case X86::VADDPDYrr:
10459 case X86::VADDPSYrr:
10460 case X86::VADDPDZ128rr:
10461 case X86::VADDPSZ128rr:
10462 case X86::VADDPDZ256rr:
10463 case X86::VADDPSZ256rr:
10464 case X86::VADDPDZrr:
10465 case X86::VADDPSZrr:
10466 case X86::VADDSDrr:
10467 case X86::VADDSSrr:
10468 case X86::VADDSDZrr:
10469 case X86::VADDSSZrr:
10470 case X86::VMULPDrr:
10471 case X86::VMULPSrr:
10472 case X86::VMULPDYrr:
10473 case X86::VMULPSYrr:
10474 case X86::VMULPDZ128rr:
10475 case X86::VMULPSZ128rr:
10476 case X86::VMULPDZ256rr:
10477 case X86::VMULPSZ256rr:
10478 case X86::VMULPDZrr:
10479 case X86::VMULPSZrr:
10480 case X86::VMULSDrr:
10481 case X86::VMULSSrr:
10482 case X86::VMULSDZrr:
10483 case X86::VMULSSZrr:
10484 case X86::VADDPHZ128rr:
10485 case X86::VADDPHZ256rr:
10486 case X86::VADDPHZrr:
10487 case X86::VADDSHZrr:
10488 case X86::VMULPHZ128rr:
10489 case X86::VMULPHZ256rr:
10490 case X86::VMULPHZrr:
10491 case X86::VMULSHZrr:
10494 default:
10495 return false;
10496 }
10497}
10498
10499/// If \p DescribedReg overlaps with the MOVrr instruction's destination
10500/// register then, if possible, describe the value in terms of the source
10501/// register.
10502static std::optional<ParamLoadedValue>
10504 const TargetRegisterInfo *TRI) {
10505 Register DestReg = MI.getOperand(0).getReg();
10506 Register SrcReg = MI.getOperand(1).getReg();
10507
10508 auto Expr = DIExpression::get(MI.getMF()->getFunction().getContext(), {});
10509
10510 // If the described register is the destination, just return the source.
10511 if (DestReg == DescribedReg)
10512 return ParamLoadedValue(MachineOperand::CreateReg(SrcReg, false), Expr);
10513
10514 // If the described register is a sub-register of the destination register,
10515 // then pick out the source register's corresponding sub-register.
10516 if (unsigned SubRegIdx = TRI->getSubRegIndex(DestReg, DescribedReg)) {
10517 Register SrcSubReg = TRI->getSubReg(SrcReg, SubRegIdx);
10518 return ParamLoadedValue(MachineOperand::CreateReg(SrcSubReg, false), Expr);
10519 }
10520
10521 // The remaining case to consider is when the described register is a
10522 // super-register of the destination register. MOV8rr and MOV16rr does not
10523 // write to any of the other bytes in the register, meaning that we'd have to
10524 // describe the value using a combination of the source register and the
10525 // non-overlapping bits in the described register, which is not currently
10526 // possible.
10527 if (MI.getOpcode() == X86::MOV8rr || MI.getOpcode() == X86::MOV16rr ||
10528 !TRI->isSuperRegister(DestReg, DescribedReg))
10529 return std::nullopt;
10530
10531 assert(MI.getOpcode() == X86::MOV32rr && "Unexpected super-register case");
10532 return ParamLoadedValue(MachineOperand::CreateReg(SrcReg, false), Expr);
10533}
10534
10535std::optional<ParamLoadedValue>
10537 const MachineOperand *Op = nullptr;
10538 DIExpression *Expr = nullptr;
10539
10541
10542 switch (MI.getOpcode()) {
10543 case X86::LEA32r:
10544 case X86::LEA64r:
10545 case X86::LEA64_32r: {
10546 // We may need to describe a 64-bit parameter with a 32-bit LEA.
10547 if (!TRI->isSuperRegisterEq(MI.getOperand(0).getReg(), Reg))
10548 return std::nullopt;
10549
10550 // Operand 4 could be global address. For now we do not support
10551 // such situation.
10552 if (!MI.getOperand(4).isImm() || !MI.getOperand(2).isImm())
10553 return std::nullopt;
10554
10555 const MachineOperand &Op1 = MI.getOperand(1);
10556 const MachineOperand &Op2 = MI.getOperand(3);
10557 assert(Op2.isReg() &&
10558 (Op2.getReg() == X86::NoRegister || Op2.getReg().isPhysical()));
10559
10560 // Omit situations like:
10561 // %rsi = lea %rsi, 4, ...
10562 if ((Op1.isReg() && Op1.getReg() == MI.getOperand(0).getReg()) ||
10563 Op2.getReg() == MI.getOperand(0).getReg())
10564 return std::nullopt;
10565 else if ((Op1.isReg() && Op1.getReg() != X86::NoRegister &&
10566 TRI->regsOverlap(Op1.getReg(), MI.getOperand(0).getReg())) ||
10567 (Op2.getReg() != X86::NoRegister &&
10568 TRI->regsOverlap(Op2.getReg(), MI.getOperand(0).getReg())))
10569 return std::nullopt;
10570
10571 int64_t Coef = MI.getOperand(2).getImm();
10572 int64_t Offset = MI.getOperand(4).getImm();
10574
10575 if ((Op1.isReg() && Op1.getReg() != X86::NoRegister)) {
10576 Op = &Op1;
10577 } else if (Op1.isFI())
10578 Op = &Op1;
10579
10580 if (Op && Op->isReg() && Op->getReg() == Op2.getReg() && Coef > 0) {
10581 Ops.push_back(dwarf::DW_OP_constu);
10582 Ops.push_back(Coef + 1);
10583 Ops.push_back(dwarf::DW_OP_mul);
10584 } else {
10585 if (Op && Op2.getReg() != X86::NoRegister) {
10586 int dwarfReg = TRI->getDwarfRegNum(Op2.getReg(), false);
10587 if (dwarfReg < 0)
10588 return std::nullopt;
10589 else if (dwarfReg < 32) {
10590 Ops.push_back(dwarf::DW_OP_breg0 + dwarfReg);
10591 Ops.push_back(0);
10592 } else {
10593 Ops.push_back(dwarf::DW_OP_bregx);
10594 Ops.push_back(dwarfReg);
10595 Ops.push_back(0);
10596 }
10597 } else if (!Op) {
10598 assert(Op2.getReg() != X86::NoRegister);
10599 Op = &Op2;
10600 }
10601
10602 if (Coef > 1) {
10603 assert(Op2.getReg() != X86::NoRegister);
10604 Ops.push_back(dwarf::DW_OP_constu);
10605 Ops.push_back(Coef);
10606 Ops.push_back(dwarf::DW_OP_mul);
10607 }
10608
10609 if (((Op1.isReg() && Op1.getReg() != X86::NoRegister) || Op1.isFI()) &&
10610 Op2.getReg() != X86::NoRegister) {
10611 Ops.push_back(dwarf::DW_OP_plus);
10612 }
10613 }
10614
10616 Expr = DIExpression::get(MI.getMF()->getFunction().getContext(), Ops);
10617
10618 return ParamLoadedValue(*Op, Expr);
10619 }
10620 case X86::MOV8ri:
10621 case X86::MOV16ri:
10622 // TODO: Handle MOV8ri and MOV16ri.
10623 return std::nullopt;
10624 case X86::MOV32ri:
10625 case X86::MOV64ri:
10626 case X86::MOV64ri32:
10627 // MOV32ri may be used for producing zero-extended 32-bit immediates in
10628 // 64-bit parameters, so we need to consider super-registers.
10629 if (!TRI->isSuperRegisterEq(MI.getOperand(0).getReg(), Reg))
10630 return std::nullopt;
10631 return ParamLoadedValue(MI.getOperand(1), Expr);
10632 case X86::MOV8rr:
10633 case X86::MOV16rr:
10634 case X86::MOV32rr:
10635 case X86::MOV64rr:
10636 return describeMOVrrLoadedValue(MI, Reg, TRI);
10637 case X86::XOR32rr: {
10638 // 64-bit parameters are zero-materialized using XOR32rr, so also consider
10639 // super-registers.
10640 if (!TRI->isSuperRegisterEq(MI.getOperand(0).getReg(), Reg))
10641 return std::nullopt;
10642 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg())
10644 return std::nullopt;
10645 }
10646 case X86::MOVSX64rr32: {
10647 // We may need to describe the lower 32 bits of the MOVSX; for example, in
10648 // cases like this:
10649 //
10650 // $ebx = [...]
10651 // $rdi = MOVSX64rr32 $ebx
10652 // $esi = MOV32rr $edi
10653 if (!TRI->isSubRegisterEq(MI.getOperand(0).getReg(), Reg))
10654 return std::nullopt;
10655
10656 Expr = DIExpression::get(MI.getMF()->getFunction().getContext(), {});
10657
10658 // If the described register is the destination register we need to
10659 // sign-extend the source register from 32 bits. The other case we handle
10660 // is when the described register is the 32-bit sub-register of the
10661 // destination register, in case we just need to return the source
10662 // register.
10663 if (Reg == MI.getOperand(0).getReg())
10664 Expr = DIExpression::appendExt(Expr, 32, 64, true);
10665 else
10666 assert(getX86MCRegisterClass(X86::GR32RegClassID).contains(Reg) &&
10667 "Unhandled sub-register case for MOVSX64rr32");
10668
10669 return ParamLoadedValue(MI.getOperand(1), Expr);
10670 }
10671 default:
10672 assert(!MI.isMoveImmediate() && "Unexpected MoveImm instruction");
10674 }
10675}
10676
10677/// This is an architecture-specific helper function of reassociateOps.
10678/// Set special operand attributes for new instructions after reassociation.
10680 MachineInstr &OldMI2,
10681 MachineInstr &NewMI1,
10682 MachineInstr &NewMI2) const {
10683 // Integer instructions may define an implicit EFLAGS dest register operand.
10684 MachineOperand *OldFlagDef1 =
10685 OldMI1.findRegisterDefOperand(X86::EFLAGS, /*TRI=*/nullptr);
10686 MachineOperand *OldFlagDef2 =
10687 OldMI2.findRegisterDefOperand(X86::EFLAGS, /*TRI=*/nullptr);
10688
10689 assert(!OldFlagDef1 == !OldFlagDef2 &&
10690 "Unexpected instruction type for reassociation");
10691
10692 if (!OldFlagDef1 || !OldFlagDef2)
10693 return;
10694
10695 assert(OldFlagDef1->isDead() && OldFlagDef2->isDead() &&
10696 "Must have dead EFLAGS operand in reassociable instruction");
10697
10698 MachineOperand *NewFlagDef1 =
10699 NewMI1.findRegisterDefOperand(X86::EFLAGS, /*TRI=*/nullptr);
10700 MachineOperand *NewFlagDef2 =
10701 NewMI2.findRegisterDefOperand(X86::EFLAGS, /*TRI=*/nullptr);
10702
10703 assert(NewFlagDef1 && NewFlagDef2 &&
10704 "Unexpected operand in reassociable instruction");
10705
10706 // Mark the new EFLAGS operands as dead to be helpful to subsequent iterations
10707 // of this pass or other passes. The EFLAGS operands must be dead in these new
10708 // instructions because the EFLAGS operands in the original instructions must
10709 // be dead in order for reassociation to occur.
10710 NewFlagDef1->setIsDead();
10711 NewFlagDef2->setIsDead();
10712}
10713
10714std::pair<unsigned, unsigned>
10716 return std::make_pair(TF, 0u);
10717}
10718
10721 using namespace X86II;
10722 static const std::pair<unsigned, const char *> TargetFlags[] = {
10723 {MO_GOT_ABSOLUTE_ADDRESS, "x86-got-absolute-address"},
10724 {MO_PIC_BASE_OFFSET, "x86-pic-base-offset"},
10725 {MO_GOT, "x86-got"},
10726 {MO_GOTOFF, "x86-gotoff"},
10727 {MO_GOTPCREL, "x86-gotpcrel"},
10728 {MO_GOTPCREL_NORELAX, "x86-gotpcrel-norelax"},
10729 {MO_PLT, "x86-plt"},
10730 {MO_TLSGD, "x86-tlsgd"},
10731 {MO_TLSLD, "x86-tlsld"},
10732 {MO_TLSLDM, "x86-tlsldm"},
10733 {MO_GOTTPOFF, "x86-gottpoff"},
10734 {MO_INDNTPOFF, "x86-indntpoff"},
10735 {MO_TPOFF, "x86-tpoff"},
10736 {MO_DTPOFF, "x86-dtpoff"},
10737 {MO_NTPOFF, "x86-ntpoff"},
10738 {MO_GOTNTPOFF, "x86-gotntpoff"},
10739 {MO_DLLIMPORT, "x86-dllimport"},
10740 {MO_DARWIN_NONLAZY, "x86-darwin-nonlazy"},
10741 {MO_DARWIN_NONLAZY_PIC_BASE, "x86-darwin-nonlazy-pic-base"},
10742 {MO_TLVP, "x86-tlvp"},
10743 {MO_TLVP_PIC_BASE, "x86-tlvp-pic-base"},
10744 {MO_SECREL, "x86-secrel"},
10745 {MO_COFFSTUB, "x86-coffstub"}};
10746 return ArrayRef(TargetFlags);
10747}
10748
10749/// Constants defining how certain sequences should be outlined.
10750///
10751/// \p MachineOutlinerDefault implies that the function is called with a call
10752/// instruction, and a return must be emitted for the outlined function frame.
10753///
10754/// That is,
10755///
10756/// I1 OUTLINED_FUNCTION:
10757/// I2 --> call OUTLINED_FUNCTION I1
10758/// I3 I2
10759/// I3
10760/// ret
10761///
10762/// * Call construction overhead: 1 (call instruction)
10763/// * Frame construction overhead: 1 (return instruction)
10764///
10765/// \p MachineOutlinerTailCall implies that the function is being tail called.
10766/// A jump is emitted instead of a call, and the return is already present in
10767/// the outlined sequence. That is,
10768///
10769/// I1 OUTLINED_FUNCTION:
10770/// I2 --> jmp OUTLINED_FUNCTION I1
10771/// ret I2
10772/// ret
10773///
10774/// * Call construction overhead: 1 (jump instruction)
10775/// * Frame construction overhead: 0 (don't need to return)
10776///
10778
10779std::optional<std::unique_ptr<outliner::OutlinedFunction>>
10781 const MachineModuleInfo &MMI,
10782 std::vector<outliner::Candidate> &RepeatedSequenceLocs,
10783 unsigned MinRepeats) const {
10784 unsigned SequenceSize = 0;
10785 for (auto &MI : RepeatedSequenceLocs[0]) {
10786 // FIXME: x86 doesn't implement getInstSizeInBytes, so
10787 // we can't tell the cost. Just assume each instruction
10788 // is one byte.
10789 if (MI.isDebugInstr() || MI.isKill())
10790 continue;
10791 SequenceSize += 1;
10792 }
10793
10794 // We check to see if CFI Instructions are present, and if they are
10795 // we find the number of CFI Instructions in the candidates.
10796 unsigned CFICount = 0;
10797 for (auto &I : RepeatedSequenceLocs[0]) {
10798 if (I.isCFIInstruction())
10799 CFICount++;
10800 }
10801
10802 // We compare the number of found CFI Instructions to the number of CFI
10803 // instructions in the parent function for each candidate. We must check this
10804 // since if we outline one of the CFI instructions in a function, we have to
10805 // outline them all for correctness. If we do not, the address offsets will be
10806 // incorrect between the two sections of the program.
10807 for (outliner::Candidate &C : RepeatedSequenceLocs) {
10808 std::vector<MCCFIInstruction> CFIInstructions =
10809 C.getMF()->getFrameInstructions();
10810
10811 if (CFICount > 0 && CFICount != CFIInstructions.size())
10812 return std::nullopt;
10813 }
10814
10815 // FIXME: Use real size in bytes for call and ret instructions.
10816 if (RepeatedSequenceLocs[0].back().isTerminator()) {
10817 for (outliner::Candidate &C : RepeatedSequenceLocs)
10818 C.setCallInfo(MachineOutlinerTailCall, 1);
10819
10820 return std::make_unique<outliner::OutlinedFunction>(
10821 RepeatedSequenceLocs, SequenceSize,
10822 0, // Number of bytes to emit frame.
10823 MachineOutlinerTailCall // Type of frame.
10824 );
10825 }
10826
10827 if (CFICount > 0)
10828 return std::nullopt;
10829
10830 for (outliner::Candidate &C : RepeatedSequenceLocs)
10831 C.setCallInfo(MachineOutlinerDefault, 1);
10832
10833 return std::make_unique<outliner::OutlinedFunction>(
10834 RepeatedSequenceLocs, SequenceSize, 1, MachineOutlinerDefault);
10835}
10836
10838 MachineFunction &MF, bool OutlineFromLinkOnceODRs) const {
10839 const Function &F = MF.getFunction();
10840
10841 // Does the function use a red zone? If it does, then we can't risk messing
10842 // with the stack.
10843 if (Subtarget.getFrameLowering()->has128ByteRedZone(MF)) {
10844 // It could have a red zone. If it does, then we don't want to touch it.
10846 if (!X86FI || X86FI->getUsesRedZone())
10847 return false;
10848 }
10849
10850 // If we *don't* want to outline from things that could potentially be deduped
10851 // then return false.
10852 if (!OutlineFromLinkOnceODRs && F.hasLinkOnceODRLinkage())
10853 return false;
10854
10855 // This function is viable for outlining, so return true.
10856 return true;
10857}
10858
10862 unsigned Flags) const {
10863 MachineInstr &MI = *MIT;
10864
10865 // Is this a terminator for a basic block?
10866 if (MI.isTerminator())
10867 // TargetInstrInfo::getOutliningType has already filtered out anything
10868 // that would break this, so we can allow it here.
10870
10871 // Don't outline anything that modifies or reads from the stack pointer.
10872 //
10873 // FIXME: There are instructions which are being manually built without
10874 // explicit uses/defs so we also have to check the MCInstrDesc. We should be
10875 // able to remove the extra checks once those are fixed up. For example,
10876 // sometimes we might get something like %rax = POP64r 1. This won't be
10877 // caught by modifiesRegister or readsRegister even though the instruction
10878 // really ought to be formed so that modifiesRegister/readsRegister would
10879 // catch it.
10880 if (MI.modifiesRegister(X86::RSP, &RI) || MI.readsRegister(X86::RSP, &RI) ||
10881 MI.getDesc().hasImplicitUseOfPhysReg(X86::RSP) ||
10882 MI.getDesc().hasImplicitDefOfPhysReg(X86::RSP))
10884
10885 // Outlined calls change the instruction pointer, so don't read from it.
10886 if (MI.readsRegister(X86::RIP, &RI) ||
10887 MI.getDesc().hasImplicitUseOfPhysReg(X86::RIP) ||
10888 MI.getDesc().hasImplicitDefOfPhysReg(X86::RIP))
10890
10891 // Don't outline CFI instructions.
10892 if (MI.isCFIInstruction())
10894
10896}
10897
10900 const outliner::OutlinedFunction &OF) const {
10901 // If we're a tail call, we already have a return, so don't do anything.
10902 if (OF.FrameConstructionID == MachineOutlinerTailCall)
10903 return;
10904
10905 // We're a normal call, so our sequence doesn't have a return instruction.
10906 // Add it in.
10907 MachineInstr *retq = BuildMI(MF, DebugLoc(), get(X86::RET64));
10908 MBB.insert(MBB.end(), retq);
10909}
10910
10914 // Is it a tail call?
10915 if (C.CallConstructionID == MachineOutlinerTailCall) {
10916 // Yes, just insert a JMP.
10917 It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(X86::TAILJMPd64))
10918 .addGlobalAddress(M.getNamedValue(MF.getName())));
10919 } else {
10920 // No, insert a call.
10921 It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(X86::CALL64pcrel32))
10922 .addGlobalAddress(M.getNamedValue(MF.getName())));
10923 }
10924
10925 return It;
10926}
10927
10930 DebugLoc &DL,
10931 bool AllowSideEffects) const {
10932 const MachineFunction &MF = *MBB.getParent();
10933 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
10935
10936 if (ST.hasMMX() && X86::VR64RegClass.contains(Reg))
10937 // FIXME: Should we ignore MMX registers?
10938 return;
10939
10940 if (TRI.isGeneralPurposeRegister(MF, Reg)) {
10941 // Convert register to the 32-bit version. Both 'movl' and 'xorl' clear the
10942 // upper bits of a 64-bit register automagically.
10943 Reg = getX86SubSuperRegister(Reg, 32);
10944
10945 if (!AllowSideEffects)
10946 // XOR affects flags, so use a MOV instead.
10947 BuildMI(MBB, Iter, DL, get(X86::MOV32ri), Reg).addImm(0);
10948 else
10949 BuildMI(MBB, Iter, DL, get(X86::XOR32rr), Reg)
10950 .addReg(Reg, RegState::Undef)
10951 .addReg(Reg, RegState::Undef);
10952 } else if (X86::VR128RegClass.contains(Reg)) {
10953 // XMM#
10954 if (!ST.hasSSE1())
10955 return;
10956
10957 BuildMI(MBB, Iter, DL, get(X86::V_SET0), Reg);
10958 } else if (X86::VR256RegClass.contains(Reg)) {
10959 // YMM#
10960 if (!ST.hasAVX())
10961 return;
10962
10963 BuildMI(MBB, Iter, DL, get(X86::V_SET0), TRI.getSubReg(Reg, X86::sub_xmm));
10964 } else if (X86::VR512RegClass.contains(Reg)) {
10965 // ZMM#
10966 if (!ST.hasAVX512())
10967 return;
10968
10969 BuildMI(MBB, Iter, DL, get(X86::AVX512_128_SET0),
10970 TRI.getSubReg(Reg, X86::sub_xmm));
10971 } else if (X86::VK1RegClass.contains(Reg) || X86::VK2RegClass.contains(Reg) ||
10972 X86::VK4RegClass.contains(Reg) || X86::VK8RegClass.contains(Reg) ||
10973 X86::VK16RegClass.contains(Reg)) {
10974 if (!ST.hasAVX512())
10975 return;
10976
10977 unsigned Op = ST.hasBWI() ? X86::KSET0Q : X86::KSET0W;
10978 BuildMI(MBB, Iter, DL, get(Op), Reg);
10979 }
10980}
10981
10983 MachineInstr &Root, SmallVectorImpl<unsigned> &Patterns,
10984 bool DoRegPressureReduce) const {
10985 unsigned Opc = Root.getOpcode();
10986 switch (Opc) {
10987 case X86::VPDPWSSDrr:
10988 case X86::VPDPWSSDrm:
10989 case X86::VPDPWSSDYrr:
10990 case X86::VPDPWSSDYrm: {
10991 if (!Subtarget.hasFastDPWSSD()) {
10993 return true;
10994 }
10995 break;
10996 }
10997 case X86::VPDPWSSDZ128rr:
10998 case X86::VPDPWSSDZ128rm:
10999 case X86::VPDPWSSDZ256rr:
11000 case X86::VPDPWSSDZ256rm:
11001 case X86::VPDPWSSDZrr:
11002 case X86::VPDPWSSDZrm: {
11003 if (Subtarget.hasBWI() && !Subtarget.hasFastDPWSSD()) {
11005 return true;
11006 }
11007 break;
11008 }
11009 }
11011 Patterns, DoRegPressureReduce);
11012}
11013
11014static void
11018 DenseMap<Register, unsigned> &InstrIdxForVirtReg) {
11019 MachineFunction *MF = Root.getMF();
11021
11022 unsigned Opc = Root.getOpcode();
11023 unsigned AddOpc = 0;
11024 unsigned MaddOpc = 0;
11025 switch (Opc) {
11026 default:
11027 assert(false && "It should not reach here");
11028 break;
11029 // vpdpwssd xmm2,xmm3,xmm1
11030 // -->
11031 // vpmaddwd xmm3,xmm3,xmm1
11032 // vpaddd xmm2,xmm2,xmm3
11033 case X86::VPDPWSSDrr:
11034 MaddOpc = X86::VPMADDWDrr;
11035 AddOpc = X86::VPADDDrr;
11036 break;
11037 case X86::VPDPWSSDrm:
11038 MaddOpc = X86::VPMADDWDrm;
11039 AddOpc = X86::VPADDDrr;
11040 break;
11041 case X86::VPDPWSSDZ128rr:
11042 MaddOpc = X86::VPMADDWDZ128rr;
11043 AddOpc = X86::VPADDDZ128rr;
11044 break;
11045 case X86::VPDPWSSDZ128rm:
11046 MaddOpc = X86::VPMADDWDZ128rm;
11047 AddOpc = X86::VPADDDZ128rr;
11048 break;
11049 // vpdpwssd ymm2,ymm3,ymm1
11050 // -->
11051 // vpmaddwd ymm3,ymm3,ymm1
11052 // vpaddd ymm2,ymm2,ymm3
11053 case X86::VPDPWSSDYrr:
11054 MaddOpc = X86::VPMADDWDYrr;
11055 AddOpc = X86::VPADDDYrr;
11056 break;
11057 case X86::VPDPWSSDYrm:
11058 MaddOpc = X86::VPMADDWDYrm;
11059 AddOpc = X86::VPADDDYrr;
11060 break;
11061 case X86::VPDPWSSDZ256rr:
11062 MaddOpc = X86::VPMADDWDZ256rr;
11063 AddOpc = X86::VPADDDZ256rr;
11064 break;
11065 case X86::VPDPWSSDZ256rm:
11066 MaddOpc = X86::VPMADDWDZ256rm;
11067 AddOpc = X86::VPADDDZ256rr;
11068 break;
11069 // vpdpwssd zmm2,zmm3,zmm1
11070 // -->
11071 // vpmaddwd zmm3,zmm3,zmm1
11072 // vpaddd zmm2,zmm2,zmm3
11073 case X86::VPDPWSSDZrr:
11074 MaddOpc = X86::VPMADDWDZrr;
11075 AddOpc = X86::VPADDDZrr;
11076 break;
11077 case X86::VPDPWSSDZrm:
11078 MaddOpc = X86::VPMADDWDZrm;
11079 AddOpc = X86::VPADDDZrr;
11080 break;
11081 }
11082 // Create vpmaddwd.
11083 const TargetRegisterClass *RC =
11084 RegInfo.getRegClass(Root.getOperand(0).getReg());
11085 Register NewReg = RegInfo.createVirtualRegister(RC);
11086 MachineInstr *Madd = Root.getMF()->CloneMachineInstr(&Root);
11087 Madd->setDesc(TII.get(MaddOpc));
11088 Madd->untieRegOperand(1);
11089 Madd->removeOperand(1);
11090 Madd->getOperand(0).setReg(NewReg);
11091 InstrIdxForVirtReg.insert(std::make_pair(NewReg, 0));
11092 // Create vpaddd.
11093 Register DstReg = Root.getOperand(0).getReg();
11094 bool IsKill = Root.getOperand(1).isKill();
11095 MachineInstr *Add =
11096 BuildMI(*MF, MIMetadata(Root), TII.get(AddOpc), DstReg)
11097 .addReg(Root.getOperand(1).getReg(), getKillRegState(IsKill))
11098 .addReg(Madd->getOperand(0).getReg(), getKillRegState(true));
11099 InsInstrs.push_back(Madd);
11100 InsInstrs.push_back(Add);
11101 DelInstrs.push_back(&Root);
11102}
11103
11105 MachineInstr &Root, unsigned Pattern,
11108 DenseMap<Register, unsigned> &InstrIdxForVirtReg) const {
11109 switch (Pattern) {
11110 default:
11111 // Reassociate instructions.
11113 DelInstrs, InstrIdxForVirtReg);
11114 return;
11116 genAlternativeDpCodeSequence(Root, *this, InsInstrs, DelInstrs,
11117 InstrIdxForVirtReg);
11118 return;
11119 }
11120}
11121
11122// See also: X86DAGToDAGISel::SelectInlineAsmMemoryOperand().
11124 int FI) const {
11127 M.Base.FrameIndex = FI;
11128 M.getFullAddress(Ops);
11129}
11130
11132X86InstrInfo::insertCodePrefetchInstr(MachineBasicBlock &MBB,
11133 MachineBasicBlock::iterator InsertBefore,
11134 const GlobalValue *GV) const {
11135 MachineFunction &MF = *MBB.getParent();
11136 MachineInstr *PrefetchInstr = MF.CreateMachineInstr(
11137 get(X86::PREFETCHIT1),
11138 InsertBefore == MBB.instr_end() ? MBB.findPrevDebugLoc(InsertBefore)
11139 : InsertBefore->getDebugLoc(),
11140 true);
11141 MachineInstrBuilder MIB(MF, PrefetchInstr);
11144 /*base_alignment=*/llvm::Align(1)));
11145 MIB.addReg(X86::RIP).addImm(1).addReg(X86::NoRegister);
11146 MIB.addGlobalAddress(GV);
11147 MIB.addReg(X86::NoRegister);
11148 MBB.insert(InsertBefore, PrefetchInstr);
11149 return PrefetchInstr;
11150}
11151
11152#define GET_INSTRINFO_HELPERS
11153#include "X86GenInstrInfo.inc"
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
static bool isFrameStoreOpcode(int Opcode)
static bool isFrameLoadOpcode(int Opcode)
MachineOutlinerClass
Constants defining how certain sequences should be outlined.
@ MachineOutlinerTailCall
Emit a save, restore, call, and return.
@ MachineOutlinerDefault
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned Imm
unsigned uint64_t
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
DXIL Forward Handle Accesses
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
static bool lookup(const GsymReader &GR, GsymDataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
static SDValue isNOT(SDValue V, SelectionDAG &DAG)
static bool Expand2AddrUndef(MachineInstrBuilder &MIB, const MCInstrDesc &Desc)
Expand a single-def pseudo instruction to a two-addr instruction with two undef reads of the register...
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
bool IsDead
This file contains some templates that are useful if you are working with the STL at all.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:484
Provides some synthesis utilities to produce sequences of values.
static SPCC::CondCodes GetOppositeBranchCondition(SPCC::CondCodes CC)
#define LLVM_DEBUG(...)
Definition Debug.h:119
#define FROM_TO(FROM, TO)
cl::opt< bool > X86EnableAPXForRelocation
static bool is64Bit(const char *name)
#define GET_EGPR_IF_ENABLED(OPC)
static bool isLEA(unsigned Opcode)
static void addOperands(MachineInstrBuilder &MIB, ArrayRef< MachineOperand > MOs, int PtrOffset=0)
static std::optional< ParamLoadedValue > describeMOVrrLoadedValue(const MachineInstr &MI, Register DescribedReg, const TargetRegisterInfo *TRI)
If DescribedReg overlaps with the MOVrr instruction's destination register then, if possible,...
static cl::opt< unsigned > PartialRegUpdateClearance("partial-reg-update-clearance", cl::desc("Clearance between two register writes " "for inserting XOR to avoid partial " "register update"), cl::init(64), cl::Hidden)
static bool shouldPreventUndefRegUpdateMemFold(MachineFunction &MF, MachineInstr &MI)
static unsigned CopyToFromAsymmetricReg(Register DestReg, Register SrcReg, const X86Subtarget &Subtarget)
static bool isConvertibleLEA(MachineInstr *MI)
static bool ExpandMOVImmSExti8(MachineInstrBuilder &MIB, const TargetInstrInfo &TII, const X86Subtarget &Subtarget)
static bool isAMXOpcode(unsigned Opc)
static int getJumpTableIndexFromReg(const MachineRegisterInfo &MRI, Register Reg)
static void updateOperandRegConstraints(MachineFunction &MF, MachineInstr &NewMI, const TargetInstrInfo &TII)
static int getJumpTableIndexFromAddr(const MachineInstr &MI)
static bool AdjustBlendMask(unsigned OldMask, unsigned OldWidth, unsigned NewWidth, unsigned *pNewMask=nullptr)
static bool expandMOV32r1(MachineInstrBuilder &MIB, const TargetInstrInfo &TII, bool MinusOne)
static unsigned getNewOpcFromTable(ArrayRef< X86TableEntry > Table, unsigned Opc)
static unsigned getStoreRegOpcode(Register SrcReg, const TargetRegisterClass *RC, bool IsStackAligned, const X86Subtarget &STI)
#define FOLD_BROADCAST(SIZE)
static cl::opt< unsigned > UndefRegClearance("undef-reg-clearance", cl::desc("How many idle instructions we would like before " "certain undef register reads"), cl::init(128), cl::Hidden)
#define CASE_BCAST_TYPE_OPC(TYPE, OP16, OP32, OP64)
static bool isTruncatedShiftCountForLEA(unsigned ShAmt)
Check whether the given shift count is appropriate can be represented by a LEA instruction.
static cl::opt< bool > ReMatPICStubLoad("remat-pic-stub-load", cl::desc("Re-materialize load from stub in PIC mode"), cl::init(false), cl::Hidden)
static SmallVector< MachineMemOperand *, 2 > extractLoadMMOs(ArrayRef< MachineMemOperand * > MMOs, MachineFunction &MF)
static MachineInstr * fuseTwoAddrInst(MachineFunction &MF, unsigned Opcode, ArrayRef< MachineOperand > MOs, MachineBasicBlock::iterator InsertPt, MachineInstr &MI, const TargetInstrInfo &TII)
static void printFailMsgforFold(const MachineInstr &MI, unsigned Idx)
static bool canConvert2Copy(unsigned Opc)
static cl::opt< bool > NoFusing("disable-spill-fusing", cl::desc("Disable fusing of spill code into instructions"), cl::Hidden)
static bool expandNOVLXStore(MachineInstrBuilder &MIB, const TargetRegisterInfo *TRI, const MCInstrDesc &StoreDesc, const MCInstrDesc &ExtractDesc, unsigned SubIdx)
static bool isX87Reg(Register Reg)
Return true if the Reg is X87 register.
static bool Expand2AddrKreg(MachineInstrBuilder &MIB, const MCInstrDesc &Desc, Register Reg)
Expand a single-def pseudo instruction to a two-addr instruction with two k0 reads.
#define VPERM_CASES_BROADCAST(Suffix)
static std::pair< X86::CondCode, unsigned > isUseDefConvertible(const MachineInstr &MI)
Check whether the use can be converted to remove a comparison against zero.
static bool findRedundantFlagInstr(MachineInstr &CmpInstr, MachineInstr &CmpValDefInstr, const MachineRegisterInfo *MRI, MachineInstr **AndInstr, const TargetRegisterInfo *TRI, const X86Subtarget &ST, bool &NoSignFlag, bool &ClearsOverflowFlag)
static bool expandSHXDROT(MachineInstrBuilder &MIB, const MCInstrDesc &Desc)
static unsigned getLoadRegOpcode(Register DestReg, const TargetRegisterClass *RC, bool IsStackAligned, const X86Subtarget &STI)
static void expandLoadStackGuard(MachineInstrBuilder &MIB, const TargetInstrInfo &TII)
static bool hasUndefRegUpdate(unsigned Opcode, unsigned OpNum, bool ForLoadFold=false)
static MachineInstr * makeM0Inst(const TargetInstrInfo &TII, unsigned Opcode, ArrayRef< MachineOperand > MOs, MachineBasicBlock::iterator InsertPt, MachineInstr &MI)
#define GET_ND_IF_ENABLED(OPC)
static bool expandMOVSHP(MachineInstrBuilder &MIB, MachineInstr &MI, const TargetInstrInfo &TII, bool HasAVX)
static bool hasPartialRegUpdate(unsigned Opcode, const X86Subtarget &Subtarget, bool ForLoadFold=false)
Return true for all instructions that only update the first 32 or 64-bits of the destination register...
#define CASE_NF(OP)
static const uint16_t * lookupAVX512(unsigned opcode, unsigned domain, ArrayRef< uint16_t[4]> Table)
static unsigned getLoadStoreRegOpcode(Register Reg, const TargetRegisterClass *RC, bool IsStackAligned, const X86Subtarget &STI, bool Load)
#define VPERM_CASES(Suffix)
#define FROM_TO_SIZE(A, B, S)
static void commuteVPTERNLOG(MachineInstr &MI, unsigned SrcOpIdx1, unsigned SrcOpIdx2)
static bool isDefConvertible(const MachineInstr &MI, bool &NoSignFlag, bool &ClearsOverflowFlag)
Check whether the definition can be converted to remove a comparison against zero.
static MachineInstr * fuseInst(MachineFunction &MF, unsigned Opcode, unsigned OpNo, ArrayRef< MachineOperand > MOs, MachineBasicBlock::iterator InsertPt, MachineInstr &MI, const TargetInstrInfo &TII, int PtrOffset=0)
static X86::CondCode getSwappedCondition(X86::CondCode CC)
Assuming the flags are set by MI(a,b), return the condition code if we modify the instructions such t...
static unsigned getCommutedVPERMV3Opcode(unsigned Opcode)
static bool isCmpRedundantAfterLTZCNT(Register SrcReg, Register SrcReg2, int64_t ImmMask, int64_t ImmValue, const MachineInstr &OI)
static bool expandXorFP(MachineInstrBuilder &MIB, const TargetInstrInfo &TII)
static MachineBasicBlock * getFallThroughMBB(MachineBasicBlock *MBB, MachineBasicBlock *TBB)
static bool isNonFoldablePartialRegisterLoad(const MachineInstr &LoadMI, const MachineInstr &UserMI, const MachineFunction &MF)
Check if LoadMI is a partial register load that we can't fold into MI because the latter uses content...
static cl::opt< unsigned > MaxNFConversions("x86-max-nf-conversions-for-cmp-reuse", cl::desc("Maximum number of NF conversions allowed to reuse EFLAGS from a " "producer dominating a multi-predecessor block"), cl::init(6), cl::Hidden)
static unsigned getLoadStoreOpcodeForFP16(bool Load, const X86Subtarget &STI)
static bool isHReg(Register Reg)
Test if the given register is a physical h register.
static cl::opt< bool > PrintFailedFusing("print-failed-fuse-candidates", cl::desc("Print instructions that the allocator wants to" " fuse, but the X86 backend currently can't"), cl::Hidden)
static bool expandNOVLXLoad(MachineInstrBuilder &MIB, const TargetRegisterInfo *TRI, const MCInstrDesc &LoadDesc, const MCInstrDesc &BroadcastDesc, unsigned SubIdx)
#define CASE_EVEX(OP)
static void genAlternativeDpCodeSequence(MachineInstr &Root, const TargetInstrInfo &TII, SmallVectorImpl< MachineInstr * > &InsInstrs, SmallVectorImpl< MachineInstr * > &DelInstrs, DenseMap< Register, unsigned > &InstrIdxForVirtReg)
#define CASE_ND(OP)
static unsigned getThreeSrcCommuteCase(uint64_t TSFlags, unsigned SrcOpIdx1, unsigned SrcOpIdx2)
This determines which of three possible cases of a three source commute the source indexes correspond...
static unsigned getTruncatedShiftCount(const MachineInstr &MI, unsigned ShiftAmtOperandIdx)
Check whether the shift count for a machine operand is non-zero.
static SmallVector< MachineMemOperand *, 2 > extractStoreMMOs(ArrayRef< MachineMemOperand * > MMOs, MachineFunction &MF)
static unsigned getBroadcastOpcode(const X86FoldTableEntry *I, const TargetRegisterClass *RC, const X86Subtarget &STI)
static unsigned convertALUrr2ALUri(unsigned Opc)
Convert an ALUrr opcode to corresponding ALUri opcode.
static bool regIsPICBase(Register BaseReg, const MachineRegisterInfo &MRI)
Return true if register is PIC base; i.e.g defined by X86::MOVPC32r.
static bool isCommutableVPERMV3Instruction(unsigned Opcode)
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:202
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:205
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:215
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition InstrTypes.h:743
@ ICMP_SLT
signed less than
Definition InstrTypes.h:769
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:770
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:746
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:755
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:744
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:745
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:764
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:763
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:767
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:754
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition InstrTypes.h:748
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition InstrTypes.h:751
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:765
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:752
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:747
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition InstrTypes.h:749
@ ICMP_NE
not equal
Definition InstrTypes.h:762
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:768
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition InstrTypes.h:756
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:766
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:753
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition InstrTypes.h:750
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
DWARF expression.
static LLVM_ABI void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
static LLVM_ABI DIExpression * appendExt(const DIExpression *Expr, unsigned FromSize, unsigned ToSize, bool Signed)
Append a zero- or sign-extension to Expr.
A debug info location.
Definition DebugLoc.h:126
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:319
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:843
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Definition Function.h:699
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:696
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:356
LiveInterval - This class represents the liveness of a register, or stack slot.
SlotIndex InsertMachineInstrInMaps(MachineInstr &MI)
SlotIndex getInstructionIndex(const MachineInstr &Instr) const
Returns the base index of the given instruction.
LiveInterval & getInterval(Register Reg)
LLVM_ABI void removePhysRegDefAt(MCRegister Reg, SlotIndex Pos)
Remove value numbers and related live segments starting at position Pos that are part of any liverang...
SlotIndex ReplaceMachineInstrInMaps(MachineInstr &MI, MachineInstr &NewMI)
A set of physical registers with utility functions to track liveness when walking backward/forward th...
const Segment * getSegmentContaining(SlotIndex Idx) const
Return the segment that contains the specified index, or null if there is none.
LLVM_ABI void replaceKillInstruction(Register Reg, MachineInstr &OldMI, MachineInstr &NewMI)
replaceKillInstruction - Update register kill info by replacing a kill instruction with a new one.
LLVM_ABI VarInfo & getVarInfo(Register Reg)
getVarInfo - Return the VarInfo structure for the specified VIRTUAL register.
static LocationSize precise(uint64_t Value)
bool usesWindowsCFI() const
Definition MCAsmInfo.h:675
static MCCFIInstruction createAdjustCfaOffset(MCSymbol *L, int64_t Adjustment, SMLoc Loc={})
.cfi_adjust_cfa_offset Same as .cfi_def_cfa_offset, but Offset is a relative value that is added/subt...
Definition MCDwarf.h:651
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
void setOpcode(unsigned Op)
Definition MCInst.h:201
Describe properties that are true of each instruction in the target description file.
This holds information about one operand of a machine instruction, indicating the register class for ...
Definition MCInstrDesc.h:88
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1579
Set of metadata that should be preserved when using BuildMI().
SimpleValueType SimpleTy
MachineInstrBundleIterator< const MachineInstr > const_iterator
LLVM_ABI const MachineBasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor.
MachineInstr * remove(MachineInstr *I)
Remove the unbundled instruction from the instruction list without deleting it.
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
LLVM_ABI bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
iterator_range< succ_iterator > successors()
iterator_range< pred_iterator > predecessors()
MachineInstrBundleIterator< MachineInstr > iterator
@ LQR_Dead
Register is known to be fully dead.
This class is a data container for one entry in a MachineConstantPool.
union llvm::MachineConstantPoolEntry::@004270020304201266316354007027341142157160323045 Val
The constant itself.
bool isMachineConstantPoolEntry() const
isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry is indeed a target specific ...
The MachineConstantPool class keeps track of constants referenced by a function which must be spilled...
LLVM_ABI unsigned getConstantPoolIndex(const Constant *C, Align Alignment)
getConstantPoolIndex - Create a new entry in the constant pool or return an existing one.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
bool needsFrameMoves() const
True if this function needs frame moves for debug or exceptions.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
MachineConstantPool * getConstantPool()
getConstantPool - Return the constant pool object for the current function.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags F, LLT MemTy, Align BaseAlignment, const MMOMetadata &Metadata=MMOMetadata(), SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Register getReg(unsigned Idx) const
Get the register for the operand index.
const MachineInstrBuilder & setMemRefs(ArrayRef< MachineMemOperand * > MMOs) const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addGlobalAddress(const GlobalValue *GV, int64_t Offset=0, unsigned TargetFlags=0) const
const MachineInstrBuilder & addDisp(const MachineOperand &Disp, int64_t off, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
const MachineInstrBuilder & copyImplicitOps(const MachineInstr &OtherMI) const
Copy all the implicit operands from OtherMI onto this one.
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
Representation of each machine instruction.
mop_iterator operands_begin()
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
bool isImplicitDef() const
const MachineBasicBlock * getParent() const
void dropDebugNumber()
Drop any variable location debugging information associated with this instruction.
LLVM_ABI void addImplicitDefUseOperands(MachineFunction &MF)
Add all implicit def and use operands to this instruction.
bool getFlag(MIFlag Flag) const
Return whether an MI flag is set.
unsigned getNumOperands() const
Retuns the total number of operands.
LLVM_ABI void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
LLVM_ABI unsigned getNumExplicitOperands() const
Returns the number of non-implicit operands.
bool modifiesRegister(Register Reg, const TargetRegisterInfo *TRI) const
Return true if the MachineInstr modifies (fully define or partially define) the specified register.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
void untieRegOperand(unsigned OpIdx)
Break any tie involving OpIdx.
LLVM_ABI void setDesc(const MCInstrDesc &TID)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one.
LLVM_ABI unsigned getNumExplicitDefs() const
Returns the number of non-implicit definitions.
LLVM_ABI void eraseFromBundle()
Unlink 'this' from its basic block and delete it.
bool hasOneMemOperand() const
Return true if this instruction has exactly one MachineMemOperand.
LLVM_ABI void substituteRegister(Register FromReg, Register ToReg, unsigned SubIdx, const TargetRegisterInfo &RegInfo)
Replace all occurrences of FromReg with ToReg:SubIdx, properly composing subreg indices where necessa...
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
LLVM_ABI bool isIdenticalTo(const MachineInstr &Other, MICheckType Check=CheckDefs) const
Return true if this instruction is identical to Other.
LLVM_ABI const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
void setFlag(MIFlag Flag)
Set a MI flag.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
LLVM_ABI void removeOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with.
LLVM_ABI void dump() const
const MachineOperand & getOperand(unsigned i) const
unsigned getNumDefs() const
Returns the total number of definitions.
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
void setDebugLoc(DebugLoc DL)
Replace current source information with new such.
MachineOperand * findRegisterDefOperand(Register Reg, const TargetRegisterInfo *TRI, bool isDead=false, bool Overlap=false)
Wrapper for findRegisterDefOperandIdx, it returns a pointer to the MachineOperand rather than an inde...
A description of a memory reference used in the backend.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
This class contains meta information specific to a module.
MachineOperand class - Representation of each machine instruction operand.
void setSubReg(unsigned subReg)
unsigned getSubReg() const
void setImplicit(bool Val=true)
void setImm(int64_t immVal)
int64_t getImm() const
bool readsReg() const
readsReg - Returns true if this operand reads the previous value of its register.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
bool isCPI() const
isCPI - Tests if this is a MO_ConstantPoolIndex operand.
void setIsDead(bool Val=true)
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
void setIsKill(bool Val=true)
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
LLVM_ABI void ChangeToRegister(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isDebug=false)
ChangeToRegister - Replace this operand with a new register operand of the specified value.
static MachineOperand CreateImm(int64_t Val)
void setIsUndef(bool Val=true)
Register getReg() const
getReg - Returns the register number.
bool isFI() const
isFI - Tests if this is a MO_FrameIndex operand.
LLVM_ABI bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
static MachineOperand CreateCPI(unsigned Idx, int Offset, unsigned TargetFlags=0)
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
int64_t getOffset() const
Return the offset from the symbol in this operand.
static MachineOperand CreateFI(int Idx)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI bool hasOneNonDBGUse(Register RegNo) const
hasOneNonDBGUse - Return true if there is exactly one non-Debug use of the specified register.
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
LLVM_ABI LLVM_READONLY MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
iterator_range< def_instr_iterator > def_instructions(Register Reg) const
bool use_nodbg_empty(Register RegNo) const
use_nodbg_empty - Return true if there are no non-Debug instructions using the specified register.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
const TargetRegisterInfo * getTargetRegisterInfo() const
LLVM_ABI const TargetRegisterClass * constrainRegClass(Register Reg, const TargetRegisterClass *RC, unsigned MinNumRegs=0)
constrainRegClass - Constrain the register class of the specified virtual register to be a common sub...
LLVM_ABI LLVM_READONLY MachineInstr * getUniqueVRegDef(Register Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isValid() const
Definition Register.h:112
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
bool isMachineOpcode() const
Test if this node has a post-isel opcode, directly corresponding to a MachineInstr opcode.
unsigned getMachineOpcode() const
This may only be called if isMachineOpcode returns true.
const SDValue & getOperand(unsigned Num) const
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
LLVM_ABI MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
LLVM_ABI void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
MachineFunction & getMachineFunction() const
SlotIndex - An opaque wrapper around machine indexes.
Definition SlotIndexes.h:66
SlotIndex getBaseIndex() const
Returns the base index for associated with this index.
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Information about stack frame layout on the target.
bool hasFP(const MachineFunction &MF) const
hasFP - Return true if the specified function should have a dedicated frame pointer register.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
TargetInstrInfo - Interface to description of machine instruction set.
virtual const TargetRegisterClass * getRegClass(const MCInstrDesc &MCID, unsigned OpNum) const
Given a machine instruction descriptor, returns the register class constraint for OpNum,...
virtual bool findCommutedOpIndices(const MachineInstr &MI, unsigned &SrcOpIdx1, unsigned &SrcOpIdx2) const
Returns true iff the routine could find two commutable operands in the given machine instruction.
virtual bool hasReassociableOperands(const MachineInstr &Inst, const MachineBasicBlock *MBB) const
Return true when \P Inst has reassociable operands in the same \P MBB.
virtual void genAlternativeCodeSequence(MachineInstr &Root, unsigned Pattern, SmallVectorImpl< MachineInstr * > &InsInstrs, SmallVectorImpl< MachineInstr * > &DelInstrs, DenseMap< Register, unsigned > &InstIdxForVirtReg) const
When getMachineCombinerPatterns() finds patterns, this function generates the instructions that could...
virtual std::optional< ParamLoadedValue > describeLoadedValue(const MachineInstr &MI, Register Reg) const
Produce the expression describing the MI loading a value into the physical register Reg.
virtual bool getMachineCombinerPatterns(MachineInstr &Root, SmallVectorImpl< unsigned > &Patterns, bool DoRegPressureReduce) const
Return true when there is potentially a faster code sequence for an instruction chain ending in Root.
virtual bool isReMaterializableImpl(const MachineInstr &MI) const
For instructions with opcodes for which the M_REMATERIALIZABLE flag is set, this hook lets the target...
virtual bool isSchedulingBoundary(const MachineInstr &MI, const MachineBasicBlock *MBB, const MachineFunction &MF) const
Test if the given instruction should be considered a scheduling boundary.
virtual MachineInstr * commuteInstructionImpl(MachineInstr &MI, bool NewMI, unsigned OpIdx1, unsigned OpIdx2) const
This method commutes the operands of the given machine instruction MI.
bool isPositionIndependent() const
const MCAsmInfo & getMCAsmInfo() const
Return target specific asm information.
CodeModel::Model getCodeModel() const
Returns the code model.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Provide an instruction scheduling machine model to CodeGen passes.
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
Target - Wrapper for Target specific information.
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:339
static constexpr TypeSize getZero()
Definition TypeSize.h:345
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:299
static LLVM_ABI Type * getFP128Ty(LLVMContext &C)
Definition Type.cpp:281
static LLVM_ABI Type * getDoubleTy(LLVMContext &C)
Definition Type.cpp:277
static LLVM_ABI Type * getFloatTy(LLVMContext &C)
Definition Type.cpp:276
static LLVM_ABI Type * getHalfTy(LLVMContext &C)
Definition Type.cpp:274
SlotIndex def
The index of the defining instruction.
LLVM Value Representation.
Definition Value.h:75
MCRegister getPhys(Register virtReg) const
returns the physical register mapped to the specified virtual register
Definition VirtRegMap.h:91
void BuildCFI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, const MCCFIInstruction &CFIInst, MachineInstr::MIFlag Flag=MachineInstr::NoFlags) const
Wraps up getting a CFI index and building a MachineInstr for it.
void getFrameIndexOperands(SmallVectorImpl< MachineOperand > &Ops, int FI) const override
bool optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg, Register SrcReg2, int64_t CmpMask, int64_t CmpValue, const MachineRegisterInfo *MRI) const override
Check if there exists an earlier instruction that operates on the same source operands and sets eflag...
bool getMachineCombinerPatterns(MachineInstr &Root, SmallVectorImpl< unsigned > &Patterns, bool DoRegPressureReduce) const override
void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const DebugLoc &DL, Register DestReg, Register SrcReg, bool KillSrc, bool RenamableDest=false, bool RenamableSrc=false) const override
bool isSchedulingBoundary(const MachineInstr &MI, const MachineBasicBlock *MBB, const MachineFunction &MF) const override
Overrides the isSchedulingBoundary from Codegen/TargetInstrInfo.cpp to make it capable of identifying...
MachineBasicBlock::iterator insertOutlinedCall(Module &M, MachineBasicBlock &MBB, MachineBasicBlock::iterator &It, MachineFunction &MF, outliner::Candidate &C) const override
void replaceBranchWithTailCall(MachineBasicBlock &MBB, SmallVectorImpl< MachineOperand > &Cond, const MachineInstr &TailCall) const override
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const override
bool canInsertSelect(const MachineBasicBlock &, ArrayRef< MachineOperand > Cond, Register, Register, Register, int &, int &, int &) const override
void insertSelect(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const DebugLoc &DL, Register DstReg, ArrayRef< MachineOperand > Cond, Register TrueReg, Register FalseReg) const override
unsigned getOpcodeAfterMemoryUnfold(unsigned Opc, bool UnfoldLoad, bool UnfoldStore, unsigned *LoadRegIndex=nullptr) const override
bool findCommutedOpIndices(const MachineInstr &MI, unsigned &SrcOpIdx1, unsigned &SrcOpIdx2) const override
Returns true iff the routine could find two commutable operands in the given machine instruction.
bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2, int64_t &Offset1, int64_t &Offset2) const override
void loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, Register DestReg, int FrameIndex, const TargetRegisterClass *RC, Register VReg, unsigned SubReg=0, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
X86InstrInfo(const X86Subtarget &STI)
static bool isDataInvariantLoad(MachineInstr &MI)
Returns true if the instruction has no behavior (specified or otherwise) that is based on the value l...
MachineInstr * commuteInstructionImpl(MachineInstr &MI, bool NewMI, unsigned CommuteOpIdx1, unsigned CommuteOpIdx2) const override
bool isFunctionSafeToOutlineFrom(MachineFunction &MF, bool OutlineFromLinkOnceODRs) const override
const X86RegisterInfo & getRegisterInfo() const
getRegisterInfo - TargetInstrInfo is a superset of MRegister info.
bool hasCommutePreference(MachineInstr &MI, bool &Commute) const override
Returns true if we have preference on the operands order in MI, the commute decision is returned in C...
bool hasLiveCondCodeDef(MachineInstr &MI) const
True if MI has a condition code def, e.g.
std::optional< ParamLoadedValue > describeLoadedValue(const MachineInstr &MI, Register Reg) const override
bool canMakeTailCallConditional(SmallVectorImpl< MachineOperand > &Cond, const MachineInstr &TailCall) const override
bool getMemOperandsWithOffsetWidth(const MachineInstr &LdSt, SmallVectorImpl< const MachineOperand * > &BaseOps, int64_t &Offset, bool &OffsetIsScalable, LocationSize &Width, const TargetRegisterInfo *TRI) const override
bool unfoldMemoryOperand(MachineFunction &MF, MachineInstr &MI, Register Reg, bool UnfoldLoad, bool UnfoldStore, SmallVectorImpl< MachineInstr * > &NewMIs) const override
std::optional< DestSourcePair > isCopyInstrImpl(const MachineInstr &MI) const override
MachineInstr * convertToThreeAddress(MachineInstr &MI, LiveVariables *LV, LiveIntervals *LIS) const override
convertToThreeAddress - This method must be implemented by targets that set the M_CONVERTIBLE_TO_3_AD...
std::pair< unsigned, unsigned > decomposeMachineOperandsTargetFlags(unsigned TF) const override
bool expandPostRAPseudo(MachineInstr &MI) const override
void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, Register SrcReg, bool isKill, int FrameIndex, const TargetRegisterClass *RC, Register VReg, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
bool isAssociativeAndCommutative(const MachineInstr &Inst, bool Invert) const override
MCInst getNop() const override
Return the noop instruction to use for a noop.
outliner::InstrType getOutliningTypeImpl(const MachineModuleInfo &MMI, MachineBasicBlock::iterator &MIT, unsigned Flags) const override
const TargetRegisterClass * getInlineAsmMemoryOperandRegClass(InlineAsm::ConstraintCode C) const override
bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2, int64_t Offset1, int64_t Offset2, unsigned NumLoads) const override
This is a used by the pre-regalloc scheduler to determine (in conjunction with areLoadsFromSameBasePt...
bool analyzeCompare(const MachineInstr &MI, Register &SrcReg, Register &SrcReg2, int64_t &CmpMask, int64_t &CmpValue) const override
bool getConstValDefinedInReg(const MachineInstr &MI, const Register Reg, int64_t &ImmVal) const override
std::optional< ExtAddrMode > getAddrModeFromMemoryOp(const MachineInstr &MemI, const TargetRegisterInfo *TRI) const override
Register isStoreToStackSlotPostFE(const MachineInstr &MI, int &FrameIndex) const override
isStoreToStackSlotPostFE - Check for post-frame ptr elimination stack locations as well.
const TargetRegisterClass * getRegClass(const MCInstrDesc &MCID, unsigned OpNum) const override
Given a machine instruction descriptor, returns the register class constraint for OpNum,...
bool isUnconditionalTailCall(const MachineInstr &MI) const override
void reMaterialize(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, Register DestReg, unsigned SubIdx, const MachineInstr &Orig, LaneBitmask UsedLanes=LaneBitmask::getAll()) const override
bool reverseBranchCondition(SmallVectorImpl< MachineOperand > &Cond) const override
std::optional< std::unique_ptr< outliner::OutlinedFunction > > getOutliningCandidateInfo(const MachineModuleInfo &MMI, std::vector< outliner::Candidate > &RepeatedSequenceLocs, unsigned MinRepeats) const override
bool classifyLEAReg(MachineInstr &MI, const MachineOperand &Src, unsigned LEAOpcode, bool AllowSP, Register &NewSrc, unsigned &NewSrcSubReg, bool &isKill, MachineOperand &ImplicitOp, LiveVariables *LV, LiveIntervals *LIS) const
Given an operand within a MachineInstr, insert preceding code to put it into the right format for a p...
Register isLoadFromStackSlotPostFE(const MachineInstr &MI, int &FrameIndex) const override
isLoadFromStackSlotPostFE - Check for post-frame ptr elimination stack locations as well.
void setExecutionDomain(MachineInstr &MI, unsigned Domain) const override
unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, ArrayRef< MachineOperand > Cond, const DebugLoc &DL, int *BytesAdded=nullptr) const override
ArrayRef< std::pair< unsigned, const char * > > getSerializableDirectMachineOperandTargetFlags() const override
Register isStoreToStackSlot(const MachineInstr &MI, int &FrameIndex) const override
bool setExecutionDomainCustom(MachineInstr &MI, unsigned Domain) const
int getSPAdjust(const MachineInstr &MI) const override
getSPAdjust - This returns the stack pointer adjustment made by this instruction.
bool verifyInstruction(const MachineInstr &MI, StringRef &ErrInfo) const override
bool isReMaterializableImpl(const MachineInstr &MI) const override
Register getGlobalBaseReg(MachineFunction *MF) const
getGlobalBaseReg - Return a virtual register initialized with the the global base register value.
int getJumpTableIndex(const MachineInstr &MI) const override
void insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const override
void setSpecialOperandAttr(MachineInstr &OldMI1, MachineInstr &OldMI2, MachineInstr &NewMI1, MachineInstr &NewMI2) const override
This is an architecture-specific helper function of reassociateOps.
std::pair< uint16_t, uint16_t > getExecutionDomain(const MachineInstr &MI) const override
bool isCoalescableExtInstr(const MachineInstr &MI, Register &SrcReg, Register &DstReg, unsigned &SubIdx) const override
isCoalescableExtInstr - Return true if the instruction is a "coalescable" extension instruction.
void loadStoreTileReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, unsigned Opc, Register Reg, int FrameIdx, bool isKill=false) const
void genAlternativeCodeSequence(MachineInstr &Root, unsigned Pattern, SmallVectorImpl< MachineInstr * > &InsInstrs, SmallVectorImpl< MachineInstr * > &DelInstrs, DenseMap< Register, unsigned > &InstrIdxForVirtReg) const override
When getMachineCombinerPatterns() finds potential patterns, this function generates the instructions ...
bool hasReassociableOperands(const MachineInstr &Inst, const MachineBasicBlock *MBB) const override
bool analyzeBranchPredicate(MachineBasicBlock &MBB, TargetInstrInfo::MachineBranchPredicate &MBP, bool AllowModify=false) const override
static bool isDataInvariant(MachineInstr &MI)
Returns true if the instruction has no behavior (specified or otherwise) that is based on the value o...
unsigned getUndefRegClearance(const MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const override
Inform the BreakFalseDeps pass how many idle instructions we would like before certain undef register...
MachineInstr * foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI, ArrayRef< unsigned > Ops, int FrameIndex, MachineInstr *&CopyMI, LiveIntervals *LIS=nullptr, VirtRegMap *VRM=nullptr) const override
Fold a load or store of the specified stack slot into the specified machine instruction for the speci...
void breakPartialRegDependency(MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const override
void buildClearRegister(Register Reg, MachineBasicBlock &MBB, MachineBasicBlock::iterator Iter, DebugLoc &DL, bool AllowSideEffects=true) const override
Register isLoadFromStackSlot(const MachineInstr &MI, int &FrameIndex) const override
int64_t getFrameAdjustment(const MachineInstr &I) const
Returns the stack pointer adjustment that happens inside the frame setup..destroy sequence (e....
bool hasHighOperandLatency(const TargetSchedModel &SchedModel, const MachineRegisterInfo *MRI, const MachineInstr &DefMI, unsigned DefIdx, const MachineInstr &UseMI, unsigned UseIdx) const override
bool isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const override
uint16_t getExecutionDomainCustom(const MachineInstr &MI) const
bool isHighLatencyDef(int opc) const override
void buildOutlinedFrame(MachineBasicBlock &MBB, MachineFunction &MF, const outliner::OutlinedFunction &OF) const override
bool foldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, Register Reg, MachineRegisterInfo *MRI) const override
foldImmediate - 'Reg' is known to be defined by a move immediate instruction, try to fold the immedia...
unsigned removeBranch(MachineBasicBlock &MBB, int *BytesRemoved=nullptr) const override
unsigned getFMA3OpcodeToCommuteOperands(const MachineInstr &MI, unsigned SrcOpIdx1, unsigned SrcOpIdx2, const X86InstrFMA3Group &FMA3Group) const
Returns an adjusted FMA opcode that must be used in FMA instruction that performs the same computatio...
bool preservesZeroValueInReg(const MachineInstr *MI, const Register NullValueReg, const TargetRegisterInfo *TRI) const override
unsigned getPartialRegUpdateClearance(const MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const override
Inform the BreakFalseDeps pass how many idle instructions we would like before a partial register upd...
X86MachineFunctionInfo - This class is derived from MachineFunction and contains private X86 target-s...
const TargetRegisterClass * constrainRegClassToNonRex2(const TargetRegisterClass *RC) const
bool hasAVX512() const
const X86RegisterInfo * getRegisterInfo() const override
bool hasAVX() const
const X86FrameLowering * getFrameLowering() const override
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
@ X86
Windows x64, Windows Itanium (IA-64)
Definition MCAsmInfo.h:53
X86II - This namespace holds all of the target specific flags that instruction info tracks.
bool isKMergeMasked(uint64_t TSFlags)
bool hasNewDataDest(uint64_t TSFlags)
@ MO_GOT_ABSOLUTE_ADDRESS
MO_GOT_ABSOLUTE_ADDRESS - On a symbol operand, this represents a relocation of: SYMBOL_LABEL + [.
@ MO_INDNTPOFF
MO_INDNTPOFF - On a symbol operand this indicates that the immediate is the absolute address of the G...
@ MO_GOTNTPOFF
MO_GOTNTPOFF - On a symbol operand this indicates that the immediate is the offset of the GOT entry w...
@ MO_GOTTPOFF
MO_GOTTPOFF - On a symbol operand this indicates that the immediate is the offset of the GOT entry wi...
@ MO_GOTPCREL
MO_GOTPCREL - On a symbol operand this indicates that the immediate is offset to the GOT entry for th...
int getMemoryOperandIdx(const MCInstrDesc &Desc)
@ EVEX
EVEX - Specifies that this instruction use EVEX form which provides syntax support up to 32 512-bit r...
@ SSEDomainShift
Execution domain for SSE instructions.
bool canUseApxExtendedReg(const MCInstrDesc &Desc)
bool isPseudo(uint64_t TSFlags)
bool isKMasked(uint64_t TSFlags)
Define some predicates that are used for node matching.
CondCode getCondFromBranch(const MachineInstr &MI)
CondCode getCondFromCFCMov(const MachineInstr &MI)
@ LAST_VALID_COND
Definition X86BaseInfo.h:95
CondCode getCondFromMI(const MachineInstr &MI)
Return the condition code of the instruction.
int getFirstAddrOperandIdx(const MachineInstr &MI)
Return the index of the instruction's first address operand, if it has a memory reference,...
@ AddrNumOperands
Definition X86BaseInfo.h:37
unsigned getSwappedVCMPImm(unsigned Imm)
Get the VCMP immediate if the opcodes are swapped.
CondCode GetOppositeBranchCondition(CondCode CC)
GetOppositeBranchCondition - Return the inverse of the specified cond, e.g.
unsigned getSwappedVPCOMImm(unsigned Imm)
Get the VPCOM immediate if the opcodes are swapped.
bool isX87Instruction(MachineInstr &MI)
Check if the instruction is X87 instruction.
unsigned getNonNDVariant(unsigned Opc)
unsigned getVPCMPImmForCond(ISD::CondCode CC)
Get the VPCMP immediate for the given condition.
std::pair< CondCode, bool > getX86ConditionCode(CmpInst::Predicate Predicate)
Return a pair of condition code for the given predicate and whether the instruction operands should b...
CondCode getCondFromSETCC(const MachineInstr &MI)
unsigned getSwappedVPCMPImm(unsigned Imm)
Get the VPCMP immediate if the opcodes are swapped.
CondCode getCondFromCCMP(const MachineInstr &MI)
int getCCMPCondFlagsFromCondCode(CondCode CC)
int getCondSrcNoFromDesc(const MCInstrDesc &MCID)
Return the source operand # for condition code by MCID.
const Constant * getConstantFromPool(const MachineInstr &MI, unsigned OpNo)
Find any constant pool entry associated with a specific instruction operand.
unsigned getNFVariantIfClobberRemovable(const MachineInstr &MI, const TargetRegisterInfo *TRI=nullptr)
unsigned getMOVriOpcode(bool Use64BitReg, int64_t Imm)
Return a MOVri opcode for materializing Imm into a 32- or 64-bit GPR.
unsigned getCMovOpcode(unsigned RegBytes, bool HasMemoryOperand=false, bool HasNDD=false)
Return a cmov opcode for the given register size in bytes, and operand type.
unsigned getNFVariant(unsigned Opc)
unsigned getVectorRegisterWidth(const MCOperandInfo &Info)
Get the width of the vector register operand.
CondCode getCondFromCMov(const MachineInstr &MI)
initializer< Ty > init(const Ty &Val)
InstrType
Represents how an instruction should be mapped by the outliner.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
@ Offset
Definition DWP.cpp:577
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1755
static bool isAddMemInstrWithRelocation(const MachineInstr &MI)
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
LLVM_ABI bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
RegState
Flags to represent properties of register accesses.
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Kill
The last use of a register.
@ Undef
Value of the register doesn't matter.
@ Define
Register definition.
static bool isMem(const MachineInstr &MI, unsigned Op)
constexpr RegState getKillRegState(bool B)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
Definition Alignment.h:134
MCRegister getX86SubSuperRegister(MCRegister Reg, unsigned Size, bool High=false)
@ Load
The value being inserted comes from a load (InsertElement only).
@ Store
The extracted value is stored (ExtractElement only).
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2224
static const MachineInstrBuilder & addRegReg(const MachineInstrBuilder &MIB, Register Reg1, bool isKill1, unsigned SubReg1, Register Reg2, bool isKill2, unsigned SubReg2)
addRegReg - This function is used to add a memory reference of the form: [Reg + Reg].
static const MachineInstrBuilder & addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset=0, bool mem=true)
addFrameReference - This function is used to add a reference to the base of an abstract object on the...
constexpr RegState getDeadRegState(bool B)
Op::Description Desc
constexpr int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition bit.h:156
bool isNonFoldableWithSameMask(unsigned RegOp)
const X86FoldTableEntry * lookupBroadcastFoldTable(unsigned RegOp, unsigned OpNum)
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
const X86InstrFMA3Group * getFMA3Group(unsigned Opcode, uint64_t TSFlags)
Returns a reference to a group of FMA3 opcodes to where the given Opcode is included.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1769
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
const X86FoldTableEntry * lookupTwoAddrFoldTable(unsigned RegOp)
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
bool is_sorted(R &&Range, Compare C)
Wrapper function around std::is_sorted to check if elements in a range R are sorted with respect to a...
Definition STLExtras.h:1986
constexpr RegState getDefRegState(bool B)
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
RegState getRegState(const MachineOperand &RegOp)
Get all register state flags from machine operand RegOp.
static bool isMemInstrWithGOTPCREL(const MachineInstr &MI)
static const MachineInstrBuilder & addOffset(const MachineInstrBuilder &MIB, int Offset)
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition STLExtras.h:2068
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
const X86FoldTableEntry * lookupUnfoldTable(unsigned MemOp)
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
constexpr auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:341
MaybeAlign getStackAlign(const Function &F, unsigned Index)
bool matchBroadcastSize(const X86FoldTableEntry &Entry, unsigned BroadcastBits)
std::pair< MachineOperand, DIExpression * > ParamLoadedValue
const X86FoldTableEntry * lookupFoldTable(unsigned RegOp, unsigned OpNum)
static const MachineInstrBuilder & addRegOffset(const MachineInstrBuilder &MIB, Register Reg, bool isKill, int Offset)
addRegOffset - This function is used to add a memory reference of the form [Reg + Offset],...
constexpr RegState getUndefRegState(bool B)
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Extended Value Type.
Definition ValueTypes.h:35
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:339
Used to describe addressing mode similar to ExtAddrMode in CodeGenPrepare.
This represents a simple continuous liveness interval for a value.
std::vector< MachineInstr * > Kills
Kills - List of MachineInstruction's which are the last use of this virtual register (kill it) in the...
This class contains a discriminated union of information about pointers in memory operands,...
static LLVM_ABI MachinePointerInfo getGOT(MachineFunction &MF)
Return a MachinePointerInfo record that refers to a GOT entry.
X86AddressMode - This struct holds a generalized full x86 address mode.
enum llvm::X86AddressMode::@202116273335065351270200035056227005202106004277 BaseType
This class is used to group {132, 213, 231} forms of FMA opcodes together.
unsigned get213Opcode() const
Returns the 213 form of FMA opcode.
unsigned get231Opcode() const
Returns the 231 form of FMA opcode.
bool isIntrinsic() const
Returns true iff the group of FMA opcodes holds intrinsic opcodes.
unsigned get132Opcode() const
Returns the 132 form of FMA opcode.
An individual sequence of instructions to be replaced with a call to an outlined function.
The information necessary to create an outlined function for some class of candidate.