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 <atomic>
48#include <optional>
49
50using namespace llvm;
51
52#define DEBUG_TYPE "x86-instr-info"
53
54#define GET_INSTRINFO_CTOR_DTOR
55#include "X86GenInstrInfo.inc"
56
58
59static cl::opt<bool>
60 NoFusing("disable-spill-fusing",
61 cl::desc("Disable fusing of spill code into instructions"),
63static cl::opt<bool>
64 PrintFailedFusing("print-failed-fuse-candidates",
65 cl::desc("Print instructions that the allocator wants to"
66 " fuse, but the X86 backend currently can't"),
68static cl::opt<bool>
69 ReMatPICStubLoad("remat-pic-stub-load",
70 cl::desc("Re-materialize load from stub in PIC mode"),
71 cl::init(false), cl::Hidden);
73 PartialRegUpdateClearance("partial-reg-update-clearance",
74 cl::desc("Clearance between two register writes "
75 "for inserting XOR to avoid partial "
76 "register update"),
77 cl::init(64), cl::Hidden);
79 "undef-reg-clearance",
80 cl::desc("How many idle instructions we would like before "
81 "certain undef register reads"),
82 cl::init(128), cl::Hidden);
83
85 "x86-max-nf-conversions-for-cmp-reuse",
86 cl::desc("Maximum number of NF conversions allowed to reuse EFLAGS from a "
87 "producer dominating a multi-predecessor block"),
89
90// Pin the vtable to this file.
91void X86InstrInfo::anchor() {}
92
94 : X86GenInstrInfo(STI, RI,
95 (STI.isTarget64BitLP64() ? X86::ADJCALLSTACKDOWN64
96 : X86::ADJCALLSTACKDOWN32),
97 (STI.isTarget64BitLP64() ? X86::ADJCALLSTACKUP64
98 : X86::ADJCALLSTACKUP32),
99 X86::CATCHRET, (STI.is64Bit() ? X86::RET64 : X86::RET32)),
100 Subtarget(STI), RI(STI.getTargetTriple()) {}
101
103 unsigned OpNum) const {
104 auto *RC = TargetInstrInfo::getRegClass(MCID, OpNum);
105 // If the target does not have egpr, then r16-r31 will be resereved for all
106 // instructions.
107 if (!RC || !Subtarget.hasEGPR())
108 return RC;
109
111 return RC;
112
113 const X86RegisterInfo *RI = Subtarget.getRegisterInfo();
114 return RI->constrainRegClassToNonRex2(RC);
115}
116
118 Register &SrcReg, Register &DstReg,
119 unsigned &SubIdx) const {
120 switch (MI.getOpcode()) {
121 default:
122 break;
123 case X86::MOVSX16rr8:
124 case X86::MOVZX16rr8:
125 case X86::MOVSX32rr8:
126 case X86::MOVZX32rr8:
127 case X86::MOVSX64rr8:
128 if (!Subtarget.is64Bit())
129 // It's not always legal to reference the low 8-bit of the larger
130 // register in 32-bit mode.
131 return false;
132 [[fallthrough]];
133 case X86::MOVSX32rr16:
134 case X86::MOVZX32rr16:
135 case X86::MOVSX64rr16:
136 case X86::MOVSX64rr32: {
137 if (MI.getOperand(0).getSubReg() || MI.getOperand(1).getSubReg())
138 // Be conservative.
139 return false;
140 SrcReg = MI.getOperand(1).getReg();
141 DstReg = MI.getOperand(0).getReg();
142 switch (MI.getOpcode()) {
143 default:
144 llvm_unreachable("Unreachable!");
145 case X86::MOVSX16rr8:
146 case X86::MOVZX16rr8:
147 case X86::MOVSX32rr8:
148 case X86::MOVZX32rr8:
149 case X86::MOVSX64rr8:
150 SubIdx = X86::sub_8bit;
151 break;
152 case X86::MOVSX32rr16:
153 case X86::MOVZX32rr16:
154 case X86::MOVSX64rr16:
155 SubIdx = X86::sub_16bit;
156 break;
157 case X86::MOVSX64rr32:
158 SubIdx = X86::sub_32bit;
159 break;
160 }
161 return true;
162 }
163 }
164 return false;
165}
166
168 if (MI.mayLoad() || MI.mayStore())
169 return false;
170
171 // Some target-independent operations that trivially lower to data-invariant
172 // instructions.
173 if (MI.isCopyLike() || MI.isInsertSubreg())
174 return true;
175
176 unsigned Opcode = MI.getOpcode();
177 using namespace X86;
178 // On x86 it is believed that imul is constant time w.r.t. the loaded data.
179 // However, they set flags and are perhaps the most surprisingly constant
180 // time operations so we call them out here separately.
181 if (isIMUL(Opcode))
182 return true;
183 // Bit scanning and counting instructions that are somewhat surprisingly
184 // constant time as they scan across bits and do other fairly complex
185 // operations like popcnt, but are believed to be constant time on x86.
186 // However, these set flags.
187 if (isBSF(Opcode) || isBSR(Opcode) || isLZCNT(Opcode) || isPOPCNT(Opcode) ||
188 isTZCNT(Opcode))
189 return true;
190 // Bit manipulation instructions are effectively combinations of basic
191 // arithmetic ops, and should still execute in constant time. These also
192 // set flags.
193 if (isBLCFILL(Opcode) || isBLCI(Opcode) || isBLCIC(Opcode) ||
194 isBLCMSK(Opcode) || isBLCS(Opcode) || isBLSFILL(Opcode) ||
195 isBLSI(Opcode) || isBLSIC(Opcode) || isBLSMSK(Opcode) || isBLSR(Opcode) ||
196 isTZMSK(Opcode))
197 return true;
198 // Bit extracting and clearing instructions should execute in constant time,
199 // and set flags.
200 if (isBEXTR(Opcode) || isBZHI(Opcode))
201 return true;
202 // Shift and rotate.
203 if (isROL(Opcode) || isROR(Opcode) || isSAR(Opcode) || isSHL(Opcode) ||
204 isSHR(Opcode) || isSHLD(Opcode) || isSHRD(Opcode))
205 return true;
206 // Basic arithmetic is constant time on the input but does set flags.
207 if (isADC(Opcode) || isADD(Opcode) || isAND(Opcode) || isOR(Opcode) ||
208 isSBB(Opcode) || isSUB(Opcode) || isXOR(Opcode))
209 return true;
210 // Arithmetic with just 32-bit and 64-bit variants and no immediates.
211 if (isANDN(Opcode))
212 return true;
213 // Unary arithmetic operations.
214 if (isDEC(Opcode) || isINC(Opcode) || isNEG(Opcode))
215 return true;
216 // Unlike other arithmetic, NOT doesn't set EFLAGS.
217 if (isNOT(Opcode))
218 return true;
219 // Various move instructions used to zero or sign extend things. Note that we
220 // intentionally don't support the _NOREX variants as we can't handle that
221 // register constraint anyways.
222 if (isMOVSX(Opcode) || isMOVZX(Opcode) || isMOVSXD(Opcode) || isMOV(Opcode))
223 return true;
224 // Arithmetic instructions that are both constant time and don't set flags.
225 if (isRORX(Opcode) || isSARX(Opcode) || isSHLX(Opcode) || isSHRX(Opcode))
226 return true;
227 // LEA doesn't actually access memory, and its arithmetic is constant time.
228 if (isLEA(Opcode))
229 return true;
230 // By default, assume that the instruction is not data invariant.
231 return false;
232}
233
235 switch (MI.getOpcode()) {
236 default:
237 // By default, assume that the load will immediately leak.
238 return false;
239
240 // On x86 it is believed that imul is constant time w.r.t. the loaded data.
241 // However, they set flags and are perhaps the most surprisingly constant
242 // time operations so we call them out here separately.
243 case X86::IMUL16rm:
244 case X86::IMUL16rmi:
245 case X86::IMUL32rm:
246 case X86::IMUL32rmi:
247 case X86::IMUL64rm:
248 case X86::IMUL64rmi32:
249
250 // Bit scanning and counting instructions that are somewhat surprisingly
251 // constant time as they scan across bits and do other fairly complex
252 // operations like popcnt, but are believed to be constant time on x86.
253 // However, these set flags.
254 case X86::BSF16rm:
255 case X86::BSF32rm:
256 case X86::BSF64rm:
257 case X86::BSR16rm:
258 case X86::BSR32rm:
259 case X86::BSR64rm:
260 case X86::LZCNT16rm:
261 case X86::LZCNT32rm:
262 case X86::LZCNT64rm:
263 case X86::POPCNT16rm:
264 case X86::POPCNT32rm:
265 case X86::POPCNT64rm:
266 case X86::TZCNT16rm:
267 case X86::TZCNT32rm:
268 case X86::TZCNT64rm:
269
270 // Bit manipulation instructions are effectively combinations of basic
271 // arithmetic ops, and should still execute in constant time. These also
272 // set flags.
273 case X86::BLCFILL32rm:
274 case X86::BLCFILL64rm:
275 case X86::BLCI32rm:
276 case X86::BLCI64rm:
277 case X86::BLCIC32rm:
278 case X86::BLCIC64rm:
279 case X86::BLCMSK32rm:
280 case X86::BLCMSK64rm:
281 case X86::BLCS32rm:
282 case X86::BLCS64rm:
283 case X86::BLSFILL32rm:
284 case X86::BLSFILL64rm:
285 case X86::BLSI32rm:
286 case X86::BLSI64rm:
287 case X86::BLSIC32rm:
288 case X86::BLSIC64rm:
289 case X86::BLSMSK32rm:
290 case X86::BLSMSK64rm:
291 case X86::BLSR32rm:
292 case X86::BLSR64rm:
293 case X86::TZMSK32rm:
294 case X86::TZMSK64rm:
295
296 // Bit extracting and clearing instructions should execute in constant time,
297 // and set flags.
298 case X86::BEXTR32rm:
299 case X86::BEXTR64rm:
300 case X86::BEXTRI32mi:
301 case X86::BEXTRI64mi:
302 case X86::BZHI32rm:
303 case X86::BZHI64rm:
304
305 // Basic arithmetic is constant time on the input but does set flags.
306 case X86::ADC8rm:
307 case X86::ADC16rm:
308 case X86::ADC32rm:
309 case X86::ADC64rm:
310 case X86::ADD8rm:
311 case X86::ADD16rm:
312 case X86::ADD32rm:
313 case X86::ADD64rm:
314 case X86::AND8rm:
315 case X86::AND16rm:
316 case X86::AND32rm:
317 case X86::AND64rm:
318 case X86::ANDN32rm:
319 case X86::ANDN64rm:
320 case X86::OR8rm:
321 case X86::OR16rm:
322 case X86::OR32rm:
323 case X86::OR64rm:
324 case X86::SBB8rm:
325 case X86::SBB16rm:
326 case X86::SBB32rm:
327 case X86::SBB64rm:
328 case X86::SUB8rm:
329 case X86::SUB16rm:
330 case X86::SUB32rm:
331 case X86::SUB64rm:
332 case X86::XOR8rm:
333 case X86::XOR16rm:
334 case X86::XOR32rm:
335 case X86::XOR64rm:
336
337 // Integer multiply w/o affecting flags is still believed to be constant
338 // time on x86. Called out separately as this is among the most surprising
339 // instructions to exhibit that behavior.
340 case X86::MULX32rm:
341 case X86::MULX64rm:
342
343 // Arithmetic instructions that are both constant time and don't set flags.
344 case X86::RORX32mi:
345 case X86::RORX64mi:
346 case X86::SARX32rm:
347 case X86::SARX64rm:
348 case X86::SHLX32rm:
349 case X86::SHLX64rm:
350 case X86::SHRX32rm:
351 case X86::SHRX64rm:
352
353 // Conversions are believed to be constant time and don't set flags.
354 case X86::CVTTSD2SI64rm:
355 case X86::VCVTTSD2SI64rm:
356 case X86::VCVTTSD2SI64Zrm:
357 case X86::CVTTSD2SIrm:
358 case X86::VCVTTSD2SIrm:
359 case X86::VCVTTSD2SIZrm:
360 case X86::CVTTSS2SI64rm:
361 case X86::VCVTTSS2SI64rm:
362 case X86::VCVTTSS2SI64Zrm:
363 case X86::CVTTSS2SIrm:
364 case X86::VCVTTSS2SIrm:
365 case X86::VCVTTSS2SIZrm:
366 case X86::CVTSI2SDrm:
367 case X86::VCVTSI2SDrm:
368 case X86::VCVTSI2SDZrm:
369 case X86::CVTSI2SSrm:
370 case X86::VCVTSI2SSrm:
371 case X86::VCVTSI2SSZrm:
372 case X86::CVTSI642SDrm:
373 case X86::VCVTSI642SDrm:
374 case X86::VCVTSI642SDZrm:
375 case X86::CVTSI642SSrm:
376 case X86::VCVTSI642SSrm:
377 case X86::VCVTSI642SSZrm:
378 case X86::CVTSS2SDrm:
379 case X86::VCVTSS2SDrm:
380 case X86::VCVTSS2SDZrm:
381 case X86::CVTSD2SSrm:
382 case X86::VCVTSD2SSrm:
383 case X86::VCVTSD2SSZrm:
384 // AVX512 added unsigned integer conversions.
385 case X86::VCVTTSD2USI64Zrm:
386 case X86::VCVTTSD2USIZrm:
387 case X86::VCVTTSS2USI64Zrm:
388 case X86::VCVTTSS2USIZrm:
389 case X86::VCVTUSI2SDZrm:
390 case X86::VCVTUSI642SDZrm:
391 case X86::VCVTUSI2SSZrm:
392 case X86::VCVTUSI642SSZrm:
393
394 // Loads to register don't set flags.
395 case X86::MOV8rm:
396 case X86::MOV8rm_NOREX:
397 case X86::MOV16rm:
398 case X86::MOV32rm:
399 case X86::MOV64rm:
400 case X86::MOVSX16rm8:
401 case X86::MOVSX32rm16:
402 case X86::MOVSX32rm8:
403 case X86::MOVSX32rm8_NOREX:
404 case X86::MOVSX64rm16:
405 case X86::MOVSX64rm32:
406 case X86::MOVSX64rm8:
407 case X86::MOVZX16rm8:
408 case X86::MOVZX32rm16:
409 case X86::MOVZX32rm8:
410 case X86::MOVZX32rm8_NOREX:
411 case X86::MOVZX64rm16:
412 case X86::MOVZX64rm8:
413 return true;
414 }
415}
416
418 const MachineFunction *MF = MI.getParent()->getParent();
420
421 if (isFrameInstr(MI)) {
422 int SPAdj = alignTo(getFrameSize(MI), TFI->getStackAlign());
423 SPAdj -= getFrameAdjustment(MI);
424 if (!isFrameSetup(MI))
425 SPAdj = -SPAdj;
426 return SPAdj;
427 }
428
429 // To know whether a call adjusts the stack, we need information
430 // that is bound to the following ADJCALLSTACKUP pseudo.
431 // Look for the next ADJCALLSTACKUP that follows the call.
432 if (MI.isCall()) {
433 const MachineBasicBlock *MBB = MI.getParent();
435 for (auto E = MBB->end(); I != E; ++I) {
436 if (I->getOpcode() == getCallFrameDestroyOpcode() || I->isCall())
437 break;
438 }
439
440 // If we could not find a frame destroy opcode, then it has already
441 // been simplified, so we don't care.
442 if (I->getOpcode() != getCallFrameDestroyOpcode())
443 return 0;
444
445 return -(I->getOperand(1).getImm());
446 }
447
448 // Currently handle only PUSHes we can reasonably expect to see
449 // in call sequences
450 switch (MI.getOpcode()) {
451 default:
452 return 0;
453 case X86::PUSH32r:
454 case X86::PUSH32rmm:
455 case X86::PUSH32rmr:
456 case X86::PUSH32i:
457 return 4;
458 case X86::PUSH64r:
459 case X86::PUSH64rmm:
460 case X86::PUSH64rmr:
461 case X86::PUSH64i32:
462 return 8;
463 }
464}
465
466/// Return true and the FrameIndex if the specified
467/// operand and follow operands form a reference to the stack frame.
468bool X86InstrInfo::isFrameOperand(const MachineInstr &MI, unsigned int Op,
469 int &FrameIndex) const {
470 if (MI.getOperand(Op + X86::AddrBaseReg).isFI() &&
471 MI.getOperand(Op + X86::AddrScaleAmt).isImm() &&
472 MI.getOperand(Op + X86::AddrIndexReg).isReg() &&
473 MI.getOperand(Op + X86::AddrDisp).isImm() &&
474 MI.getOperand(Op + X86::AddrScaleAmt).getImm() == 1 &&
475 MI.getOperand(Op + X86::AddrIndexReg).getReg() == 0 &&
476 MI.getOperand(Op + X86::AddrDisp).getImm() == 0) {
477 FrameIndex = MI.getOperand(Op + X86::AddrBaseReg).getIndex();
478 return true;
479 }
480 return false;
481}
482
483static bool isFrameLoadOpcode(int Opcode, TypeSize &MemBytes) {
484 switch (Opcode) {
485 default:
486 return false;
487 case X86::MOV8rm:
488 case X86::KMOVBkm:
489 case X86::KMOVBkm_EVEX:
490 MemBytes = TypeSize::getFixed(1);
491 return true;
492 case X86::MOV16rm:
493 case X86::KMOVWkm:
494 case X86::KMOVWkm_EVEX:
495 case X86::VMOVSHZrm:
496 case X86::VMOVSHZrm_alt:
497 MemBytes = TypeSize::getFixed(2);
498 return true;
499 case X86::MOV32rm:
500 case X86::MOVSSrm:
501 case X86::MOVSSrm_alt:
502 case X86::VMOVSSrm:
503 case X86::VMOVSSrm_alt:
504 case X86::VMOVSSZrm:
505 case X86::VMOVSSZrm_alt:
506 case X86::KMOVDkm:
507 case X86::KMOVDkm_EVEX:
508 MemBytes = TypeSize::getFixed(4);
509 return true;
510 case X86::MOV64rm:
511 case X86::LD_Fp64m:
512 case X86::MOVSDrm:
513 case X86::MOVSDrm_alt:
514 case X86::VMOVSDrm:
515 case X86::VMOVSDrm_alt:
516 case X86::VMOVSDZrm:
517 case X86::VMOVSDZrm_alt:
518 case X86::MMX_MOVD64rm:
519 case X86::MMX_MOVQ64rm:
520 case X86::KMOVQkm:
521 case X86::KMOVQkm_EVEX:
522 MemBytes = TypeSize::getFixed(8);
523 return true;
524 case X86::MOVAPSrm:
525 case X86::MOVUPSrm:
526 case X86::MOVAPDrm:
527 case X86::MOVUPDrm:
528 case X86::MOVDQArm:
529 case X86::MOVDQUrm:
530 case X86::VMOVAPSrm:
531 case X86::VMOVUPSrm:
532 case X86::VMOVAPDrm:
533 case X86::VMOVUPDrm:
534 case X86::VMOVDQArm:
535 case X86::VMOVDQUrm:
536 case X86::VMOVAPSZ128rm:
537 case X86::VMOVUPSZ128rm:
538 case X86::VMOVAPSZ128rm_NOVLX:
539 case X86::VMOVUPSZ128rm_NOVLX:
540 case X86::VMOVAPDZ128rm:
541 case X86::VMOVUPDZ128rm:
542 case X86::VMOVDQU8Z128rm:
543 case X86::VMOVDQU16Z128rm:
544 case X86::VMOVDQA32Z128rm:
545 case X86::VMOVDQU32Z128rm:
546 case X86::VMOVDQA64Z128rm:
547 case X86::VMOVDQU64Z128rm:
548 MemBytes = TypeSize::getFixed(16);
549 return true;
550 case X86::VMOVAPSYrm:
551 case X86::VMOVUPSYrm:
552 case X86::VMOVAPDYrm:
553 case X86::VMOVUPDYrm:
554 case X86::VMOVDQAYrm:
555 case X86::VMOVDQUYrm:
556 case X86::VMOVAPSZ256rm:
557 case X86::VMOVUPSZ256rm:
558 case X86::VMOVAPSZ256rm_NOVLX:
559 case X86::VMOVUPSZ256rm_NOVLX:
560 case X86::VMOVAPDZ256rm:
561 case X86::VMOVUPDZ256rm:
562 case X86::VMOVDQU8Z256rm:
563 case X86::VMOVDQU16Z256rm:
564 case X86::VMOVDQA32Z256rm:
565 case X86::VMOVDQU32Z256rm:
566 case X86::VMOVDQA64Z256rm:
567 case X86::VMOVDQU64Z256rm:
568 MemBytes = TypeSize::getFixed(32);
569 return true;
570 case X86::VMOVAPSZrm:
571 case X86::VMOVUPSZrm:
572 case X86::VMOVAPDZrm:
573 case X86::VMOVUPDZrm:
574 case X86::VMOVDQU8Zrm:
575 case X86::VMOVDQU16Zrm:
576 case X86::VMOVDQA32Zrm:
577 case X86::VMOVDQU32Zrm:
578 case X86::VMOVDQA64Zrm:
579 case X86::VMOVDQU64Zrm:
580 MemBytes = TypeSize::getFixed(64);
581 return true;
582 }
583}
584
585static bool isFrameStoreOpcode(int Opcode, TypeSize &MemBytes) {
586 switch (Opcode) {
587 default:
588 return false;
589 case X86::MOV8mr:
590 case X86::KMOVBmk:
591 case X86::KMOVBmk_EVEX:
592 MemBytes = TypeSize::getFixed(1);
593 return true;
594 case X86::MOV16mr:
595 case X86::KMOVWmk:
596 case X86::KMOVWmk_EVEX:
597 case X86::VMOVSHZmr:
598 MemBytes = TypeSize::getFixed(2);
599 return true;
600 case X86::MOV32mr:
601 case X86::MOVSSmr:
602 case X86::VMOVSSmr:
603 case X86::VMOVSSZmr:
604 case X86::KMOVDmk:
605 case X86::KMOVDmk_EVEX:
606 MemBytes = TypeSize::getFixed(4);
607 return true;
608 case X86::MOV64mr:
609 case X86::ST_FpP64m:
610 case X86::MOVSDmr:
611 case X86::VMOVSDmr:
612 case X86::VMOVSDZmr:
613 case X86::MMX_MOVD64mr:
614 case X86::MMX_MOVQ64mr:
615 case X86::MMX_MOVNTQmr:
616 case X86::KMOVQmk:
617 case X86::KMOVQmk_EVEX:
618 MemBytes = TypeSize::getFixed(8);
619 return true;
620 case X86::MOVAPSmr:
621 case X86::MOVUPSmr:
622 case X86::MOVAPDmr:
623 case X86::MOVUPDmr:
624 case X86::MOVDQAmr:
625 case X86::MOVDQUmr:
626 case X86::VMOVAPSmr:
627 case X86::VMOVUPSmr:
628 case X86::VMOVAPDmr:
629 case X86::VMOVUPDmr:
630 case X86::VMOVDQAmr:
631 case X86::VMOVDQUmr:
632 case X86::VMOVUPSZ128mr:
633 case X86::VMOVAPSZ128mr:
634 case X86::VMOVUPSZ128mr_NOVLX:
635 case X86::VMOVAPSZ128mr_NOVLX:
636 case X86::VMOVUPDZ128mr:
637 case X86::VMOVAPDZ128mr:
638 case X86::VMOVDQA32Z128mr:
639 case X86::VMOVDQU32Z128mr:
640 case X86::VMOVDQA64Z128mr:
641 case X86::VMOVDQU64Z128mr:
642 case X86::VMOVDQU8Z128mr:
643 case X86::VMOVDQU16Z128mr:
644 MemBytes = TypeSize::getFixed(16);
645 return true;
646 case X86::VMOVUPSYmr:
647 case X86::VMOVAPSYmr:
648 case X86::VMOVUPDYmr:
649 case X86::VMOVAPDYmr:
650 case X86::VMOVDQUYmr:
651 case X86::VMOVDQAYmr:
652 case X86::VMOVUPSZ256mr:
653 case X86::VMOVAPSZ256mr:
654 case X86::VMOVUPSZ256mr_NOVLX:
655 case X86::VMOVAPSZ256mr_NOVLX:
656 case X86::VMOVUPDZ256mr:
657 case X86::VMOVAPDZ256mr:
658 case X86::VMOVDQU8Z256mr:
659 case X86::VMOVDQU16Z256mr:
660 case X86::VMOVDQA32Z256mr:
661 case X86::VMOVDQU32Z256mr:
662 case X86::VMOVDQA64Z256mr:
663 case X86::VMOVDQU64Z256mr:
664 MemBytes = TypeSize::getFixed(32);
665 return true;
666 case X86::VMOVUPSZmr:
667 case X86::VMOVAPSZmr:
668 case X86::VMOVUPDZmr:
669 case X86::VMOVAPDZmr:
670 case X86::VMOVDQU8Zmr:
671 case X86::VMOVDQU16Zmr:
672 case X86::VMOVDQA32Zmr:
673 case X86::VMOVDQU32Zmr:
674 case X86::VMOVDQA64Zmr:
675 case X86::VMOVDQU64Zmr:
676 MemBytes = TypeSize::getFixed(64);
677 return true;
678 }
679 return false;
680}
681
683 int &FrameIndex) const {
684 TypeSize Dummy = TypeSize::getZero();
685 return X86InstrInfo::isLoadFromStackSlot(MI, FrameIndex, Dummy);
686}
687
689 int &FrameIndex,
690 TypeSize &MemBytes) const {
691 if (isFrameLoadOpcode(MI.getOpcode(), MemBytes))
692 if (MI.getOperand(0).getSubReg() == 0 && isFrameOperand(MI, 1, FrameIndex))
693 return MI.getOperand(0).getReg();
694 return Register();
695}
696
698 int &FrameIndex) const {
699 TypeSize Dummy = TypeSize::getZero();
700 if (isFrameLoadOpcode(MI.getOpcode(), Dummy)) {
701 if (Register Reg = isLoadFromStackSlot(MI, FrameIndex))
702 return Reg;
703 // Check for post-frame index elimination operations
705 if (hasLoadFromStackSlot(MI, Accesses)) {
706 FrameIndex =
707 cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue())
708 ->getFrameIndex();
709 return MI.getOperand(0).getReg();
710 }
711 }
712 return Register();
713}
714
716 int &FrameIndex) const {
717 TypeSize Dummy = TypeSize::getZero();
718 return X86InstrInfo::isStoreToStackSlot(MI, FrameIndex, Dummy);
719}
720
722 int &FrameIndex,
723 TypeSize &MemBytes) const {
724 if (isFrameStoreOpcode(MI.getOpcode(), MemBytes))
725 if (MI.getOperand(X86::AddrNumOperands).getSubReg() == 0 &&
726 isFrameOperand(MI, 0, FrameIndex))
727 return MI.getOperand(X86::AddrNumOperands).getReg();
728 return Register();
729}
730
732 int &FrameIndex) const {
733 TypeSize Dummy = TypeSize::getZero();
734 if (isFrameStoreOpcode(MI.getOpcode(), Dummy)) {
735 if (Register Reg = isStoreToStackSlot(MI, FrameIndex))
736 return Reg;
737 // Check for post-frame index elimination operations
739 if (hasStoreToStackSlot(MI, Accesses)) {
740 FrameIndex =
741 cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue())
742 ->getFrameIndex();
743 return MI.getOperand(X86::AddrNumOperands).getReg();
744 }
745 }
746 return Register();
747}
748
749/// Return true if register is PIC base; i.e.g defined by X86::MOVPC32r.
750static bool regIsPICBase(Register BaseReg, const MachineRegisterInfo &MRI) {
751 // Don't waste compile time scanning use-def chains of physregs.
752 if (!BaseReg.isVirtual())
753 return false;
754 bool isPICBase = false;
755 for (const MachineInstr &DefMI : MRI.def_instructions(BaseReg)) {
756 if (DefMI.getOpcode() != X86::MOVPC32r)
757 return false;
758 assert(!isPICBase && "More than one PIC base?");
759 isPICBase = true;
760 }
761 return isPICBase;
762}
763
765 const MachineInstr &MI) const {
766 switch (MI.getOpcode()) {
767 default:
768 // This function should only be called for opcodes with the ReMaterializable
769 // flag set.
770 llvm_unreachable("Unknown rematerializable operation!");
771 break;
772 case X86::IMPLICIT_DEF:
773 // Defer to generic logic.
774 break;
775 case X86::LOAD_STACK_GUARD:
776 case X86::LD_Fp032:
777 case X86::LD_Fp064:
778 case X86::LD_Fp080:
779 case X86::LD_Fp132:
780 case X86::LD_Fp164:
781 case X86::LD_Fp180:
782 case X86::AVX1_SETALLONES:
783 case X86::AVX2_SETALLONES:
784 case X86::AVX512_128_SET0:
785 case X86::AVX512_128_SETALLONES:
786 case X86::AVX512_256_SETALLONES:
787 case X86::AVX512_512_SETALLONES:
788 case X86::AVX512_FsFLD0SD:
789 case X86::AVX512_FsFLD0SH:
790 case X86::AVX512_FsFLD0SS:
791 case X86::AVX512_FsFLD0F128:
792 case X86::FsFLD0SD:
793 case X86::FsFLD0SS:
794 case X86::FsFLD0SH:
795 case X86::FsFLD0F128:
796 case X86::KSET0B:
797 case X86::KSET0D:
798 case X86::KSET0Q:
799 case X86::KSET0W:
800 case X86::KSET1B:
801 case X86::KSET1D:
802 case X86::KSET1Q:
803 case X86::KSET1W:
804 case X86::MMX_SET0:
805 case X86::MOV32ImmSExti8:
806 case X86::MOV32r0:
807 case X86::MOV32r1:
808 case X86::MOV32r_1:
809 case X86::MOV32ri64:
810 case X86::MOV64ImmSExti8:
811 case X86::V_SET0:
812 case X86::V_SETALLONES:
813 case X86::MOV16ri:
814 case X86::MOV32ri:
815 case X86::MOV64ri:
816 case X86::MOV64ri32:
817 case X86::MOV8ri:
818 case X86::PTILEZEROV:
819 return true;
820
821 case X86::MOV8rm:
822 case X86::MOV8rm_NOREX:
823 case X86::MOV16rm:
824 case X86::MOV32rm:
825 case X86::MOV64rm:
826 case X86::MOVSSrm:
827 case X86::MOVSSrm_alt:
828 case X86::MOVSDrm:
829 case X86::MOVSDrm_alt:
830 case X86::MOVAPSrm:
831 case X86::MOVUPSrm:
832 case X86::MOVAPDrm:
833 case X86::MOVUPDrm:
834 case X86::MOVDQArm:
835 case X86::MOVDQUrm:
836 case X86::VMOVSSrm:
837 case X86::VMOVSSrm_alt:
838 case X86::VMOVSDrm:
839 case X86::VMOVSDrm_alt:
840 case X86::VMOVAPSrm:
841 case X86::VMOVUPSrm:
842 case X86::VMOVAPDrm:
843 case X86::VMOVUPDrm:
844 case X86::VMOVDQArm:
845 case X86::VMOVDQUrm:
846 case X86::VMOVAPSYrm:
847 case X86::VMOVUPSYrm:
848 case X86::VMOVAPDYrm:
849 case X86::VMOVUPDYrm:
850 case X86::VMOVDQAYrm:
851 case X86::VMOVDQUYrm:
852 case X86::MMX_MOVD64rm:
853 case X86::MMX_MOVQ64rm:
854 case X86::VBROADCASTSSrm:
855 case X86::VBROADCASTSSYrm:
856 case X86::VBROADCASTSDYrm:
857 // AVX-512
858 case X86::VPBROADCASTBZ128rm:
859 case X86::VPBROADCASTBZ256rm:
860 case X86::VPBROADCASTBZrm:
861 case X86::VBROADCASTF32X2Z256rm:
862 case X86::VBROADCASTF32X2Zrm:
863 case X86::VBROADCASTI32X2Z128rm:
864 case X86::VBROADCASTI32X2Z256rm:
865 case X86::VBROADCASTI32X2Zrm:
866 case X86::VPBROADCASTWZ128rm:
867 case X86::VPBROADCASTWZ256rm:
868 case X86::VPBROADCASTWZrm:
869 case X86::VPBROADCASTDZ128rm:
870 case X86::VPBROADCASTDZ256rm:
871 case X86::VPBROADCASTDZrm:
872 case X86::VBROADCASTSSZ128rm:
873 case X86::VBROADCASTSSZ256rm:
874 case X86::VBROADCASTSSZrm:
875 case X86::VPBROADCASTQZ128rm:
876 case X86::VPBROADCASTQZ256rm:
877 case X86::VPBROADCASTQZrm:
878 case X86::VBROADCASTSDZ256rm:
879 case X86::VBROADCASTSDZrm:
880 case X86::VMOVSSZrm:
881 case X86::VMOVSSZrm_alt:
882 case X86::VMOVSDZrm:
883 case X86::VMOVSDZrm_alt:
884 case X86::VMOVSHZrm:
885 case X86::VMOVSHZrm_alt:
886 case X86::VMOVAPDZ128rm:
887 case X86::VMOVAPDZ256rm:
888 case X86::VMOVAPDZrm:
889 case X86::VMOVAPSZ128rm:
890 case X86::VMOVAPSZ256rm:
891 case X86::VMOVAPSZ128rm_NOVLX:
892 case X86::VMOVAPSZ256rm_NOVLX:
893 case X86::VMOVAPSZrm:
894 case X86::VMOVDQA32Z128rm:
895 case X86::VMOVDQA32Z256rm:
896 case X86::VMOVDQA32Zrm:
897 case X86::VMOVDQA64Z128rm:
898 case X86::VMOVDQA64Z256rm:
899 case X86::VMOVDQA64Zrm:
900 case X86::VMOVDQU16Z128rm:
901 case X86::VMOVDQU16Z256rm:
902 case X86::VMOVDQU16Zrm:
903 case X86::VMOVDQU32Z128rm:
904 case X86::VMOVDQU32Z256rm:
905 case X86::VMOVDQU32Zrm:
906 case X86::VMOVDQU64Z128rm:
907 case X86::VMOVDQU64Z256rm:
908 case X86::VMOVDQU64Zrm:
909 case X86::VMOVDQU8Z128rm:
910 case X86::VMOVDQU8Z256rm:
911 case X86::VMOVDQU8Zrm:
912 case X86::VMOVUPDZ128rm:
913 case X86::VMOVUPDZ256rm:
914 case X86::VMOVUPDZrm:
915 case X86::VMOVUPSZ128rm:
916 case X86::VMOVUPSZ256rm:
917 case X86::VMOVUPSZ128rm_NOVLX:
918 case X86::VMOVUPSZ256rm_NOVLX:
919 case X86::VMOVUPSZrm: {
920 // Loads from constant pools are trivially rematerializable.
921 if (MI.getOperand(1 + X86::AddrBaseReg).isReg() &&
922 MI.getOperand(1 + X86::AddrScaleAmt).isImm() &&
923 MI.getOperand(1 + X86::AddrIndexReg).isReg() &&
924 MI.getOperand(1 + X86::AddrIndexReg).getReg() == 0 &&
925 MI.isDereferenceableInvariantLoad()) {
926 Register BaseReg = MI.getOperand(1 + X86::AddrBaseReg).getReg();
927 if (BaseReg == 0 || BaseReg == X86::RIP)
928 return true;
929 // Allow re-materialization of PIC load.
930 if (!(!ReMatPICStubLoad && MI.getOperand(1 + X86::AddrDisp).isGlobal())) {
931 const MachineFunction &MF = *MI.getParent()->getParent();
932 const MachineRegisterInfo &MRI = MF.getRegInfo();
933 if (regIsPICBase(BaseReg, MRI))
934 return true;
935 }
936 }
937 break;
938 }
939
940 case X86::LEA32r:
941 case X86::LEA64r: {
942 if (MI.getOperand(1 + X86::AddrScaleAmt).isImm() &&
943 MI.getOperand(1 + X86::AddrIndexReg).isReg() &&
944 MI.getOperand(1 + X86::AddrIndexReg).getReg() == 0 &&
945 !MI.getOperand(1 + X86::AddrDisp).isReg()) {
946 // lea fi#, lea GV, etc. are all rematerializable.
947 if (!MI.getOperand(1 + X86::AddrBaseReg).isReg())
948 return true;
949 Register BaseReg = MI.getOperand(1 + X86::AddrBaseReg).getReg();
950 if (BaseReg == 0)
951 return true;
952 // Allow re-materialization of lea PICBase + x.
953 const MachineFunction &MF = *MI.getParent()->getParent();
954 const MachineRegisterInfo &MRI = MF.getRegInfo();
955 if (regIsPICBase(BaseReg, MRI))
956 return true;
957 }
958 break;
959 }
960 }
962}
963
966 Register DestReg, unsigned SubIdx,
967 const MachineInstr &Orig,
968 LaneBitmask UsedLanes) const {
969 bool ClobbersEFLAGS = Orig.modifiesRegister(X86::EFLAGS, &TRI);
970 if (ClobbersEFLAGS && MBB.computeRegisterLiveness(&TRI, X86::EFLAGS, I) !=
972 // The instruction clobbers EFLAGS. Re-materialize as MOV32ri to avoid side
973 // effects.
974 int Value;
975 switch (Orig.getOpcode()) {
976 case X86::MOV32r0:
977 Value = 0;
978 break;
979 case X86::MOV32r1:
980 Value = 1;
981 break;
982 case X86::MOV32r_1:
983 Value = -1;
984 break;
985 default:
986 llvm_unreachable("Unexpected instruction!");
987 }
988
989 const DebugLoc &DL = Orig.getDebugLoc();
990 BuildMI(MBB, I, DL, get(X86::MOV32ri))
991 .add(Orig.getOperand(0))
992 .addImm(Value);
993 } else {
994 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig);
995 MBB.insert(I, MI);
996 }
997
998 MachineInstr &NewMI = *std::prev(I);
999 NewMI.substituteRegister(Orig.getOperand(0).getReg(), DestReg, SubIdx, TRI);
1000}
1001
1002/// True if MI has a condition code def, e.g. EFLAGS, that is not marked dead.
1004 for (const MachineOperand &MO : MI.operands()) {
1005 if (MO.isReg() && MO.isDef() && MO.getReg() == X86::EFLAGS &&
1006 !MO.isDead()) {
1007 return true;
1008 }
1009 }
1010 return false;
1011}
1012
1013/// Check whether the shift count for a machine operand is non-zero.
1014inline static unsigned getTruncatedShiftCount(const MachineInstr &MI,
1015 unsigned ShiftAmtOperandIdx) {
1016 // The shift count is six bits with the REX.W prefix and five bits without.
1017 unsigned ShiftCountMask = (MI.getDesc().TSFlags & X86II::REX_W) ? 63 : 31;
1018 unsigned Imm = MI.getOperand(ShiftAmtOperandIdx).getImm();
1019 return Imm & ShiftCountMask;
1020}
1021
1022/// Check whether the given shift count is appropriate
1023/// can be represented by a LEA instruction.
1024inline static bool isTruncatedShiftCountForLEA(unsigned ShAmt) {
1025 // Left shift instructions can be transformed into load-effective-address
1026 // instructions if we can encode them appropriately.
1027 // A LEA instruction utilizes a SIB byte to encode its scale factor.
1028 // The SIB.scale field is two bits wide which means that we can encode any
1029 // shift amount less than 4.
1030 return ShAmt < 4 && ShAmt > 0;
1031}
1032
1033static bool
1035 const MachineRegisterInfo *MRI, MachineInstr **AndInstr,
1036 const TargetRegisterInfo *TRI, const X86Subtarget &ST,
1037 bool &NoSignFlag, bool &ClearsOverflowFlag) {
1038 if (!(CmpValDefInstr.getOpcode() == X86::SUBREG_TO_REG &&
1039 CmpInstr.getOpcode() == X86::TEST64rr) &&
1040 !(CmpValDefInstr.getOpcode() == X86::COPY &&
1041 CmpInstr.getOpcode() == X86::TEST16rr))
1042 return false;
1043
1044 // CmpInstr is a TEST16rr/TEST64rr instruction, and
1045 // `X86InstrInfo::analyzeCompare` guarantees that it's analyzable only if two
1046 // registers are identical.
1047 assert((CmpInstr.getOperand(0).getReg() == CmpInstr.getOperand(1).getReg()) &&
1048 "CmpInstr is an analyzable TEST16rr/TEST64rr, and "
1049 "`X86InstrInfo::analyzeCompare` requires two reg operands are the"
1050 "same.");
1051
1052 // Caller (`X86InstrInfo::optimizeCompareInstr`) guarantees that
1053 // `CmpValDefInstr` defines the value that's used by `CmpInstr`; in this case
1054 // if `CmpValDefInstr` sets the EFLAGS, it is likely that `CmpInstr` is
1055 // redundant.
1056 assert(
1057 (MRI->getVRegDef(CmpInstr.getOperand(0).getReg()) == &CmpValDefInstr) &&
1058 "Caller guarantees that TEST64rr is a user of SUBREG_TO_REG or TEST16rr "
1059 "is a user of COPY sub16bit.");
1060 MachineInstr *VregDefInstr = nullptr;
1061 if (CmpInstr.getOpcode() == X86::TEST16rr) {
1062 if (!CmpValDefInstr.getOperand(1).getReg().isVirtual())
1063 return false;
1064 VregDefInstr = MRI->getVRegDef(CmpValDefInstr.getOperand(1).getReg());
1065 if (!VregDefInstr)
1066 return false;
1067 // We can only remove test when AND32ri or AND64ri32 whose imm can fit 16bit
1068 // size, others 32/64 bit ops would test higher bits which test16rr don't
1069 // want to.
1070 if (!((VregDefInstr->getOpcode() == X86::AND32ri ||
1071 VregDefInstr->getOpcode() == X86::AND64ri32) &&
1072 isUInt<16>(VregDefInstr->getOperand(2).getImm())))
1073 return false;
1074 }
1075
1076 if (CmpInstr.getOpcode() == X86::TEST64rr) {
1077 // As seen in X86 td files, CmpValDefInstr.getOperand(3) is typically
1078 // sub_32bit or sub_xmm.
1079 if (CmpValDefInstr.getOperand(2).getImm() != X86::sub_32bit)
1080 return false;
1081
1082 VregDefInstr = MRI->getVRegDef(CmpValDefInstr.getOperand(1).getReg());
1083 }
1084
1085 assert(VregDefInstr && "Must have a definition (SSA)");
1086
1087 // Requires `CmpValDefInstr` and `VregDefInstr` are from the same MBB
1088 // to simplify the subsequent analysis.
1089 //
1090 // FIXME: If `VregDefInstr->getParent()` is the only predecessor of
1091 // `CmpValDefInstr.getParent()`, this could be handled.
1092 if (VregDefInstr->getParent() != CmpValDefInstr.getParent())
1093 return false;
1094
1095 if (X86::isAND(VregDefInstr->getOpcode()) &&
1096 (!ST.hasNF() || VregDefInstr->modifiesRegister(X86::EFLAGS, TRI))) {
1097 // Get a sequence of instructions like
1098 // %reg = and* ... // Set EFLAGS
1099 // ... // EFLAGS not changed
1100 // %extended_reg = subreg_to_reg %reg, %subreg.sub_32bit
1101 // test64rr %extended_reg, %extended_reg, implicit-def $eflags
1102 // or
1103 // %reg = and32* ...
1104 // ... // EFLAGS not changed.
1105 // %src_reg = copy %reg.sub_16bit:gr32
1106 // test16rr %src_reg, %src_reg, implicit-def $eflags
1107 //
1108 // If subsequent readers use a subset of bits that don't change
1109 // after `and*` instructions, it's likely that the test64rr could
1110 // be optimized away.
1111 for (const MachineInstr &Instr :
1112 make_range(std::next(MachineBasicBlock::iterator(VregDefInstr)),
1113 MachineBasicBlock::iterator(CmpValDefInstr))) {
1114 // There are instructions between 'VregDefInstr' and
1115 // 'CmpValDefInstr' that modifies EFLAGS.
1116 if (Instr.modifiesRegister(X86::EFLAGS, TRI))
1117 return false;
1118 }
1119
1120 *AndInstr = VregDefInstr;
1121
1122 // AND instruction will essentially update SF and clear OF, so
1123 // NoSignFlag should be false in the sense that SF is modified by `AND`.
1124 //
1125 // However, the implementation artifically sets `NoSignFlag` to true
1126 // to poison the SF bit; that is to say, if SF is looked at later, the
1127 // optimization (to erase TEST64rr) will be disabled.
1128 //
1129 // The reason to poison SF bit is that SF bit value could be different
1130 // in the `AND` and `TEST` operation; signed bit is not known for `AND`,
1131 // and is known to be 0 as a result of `TEST64rr`.
1132 //
1133 // FIXME: As opposed to poisoning the SF bit directly, consider peeking into
1134 // the AND instruction and using the static information to guide peephole
1135 // optimization if possible. For example, it's possible to fold a
1136 // conditional move into a copy if the relevant EFLAG bits could be deduced
1137 // from an immediate operand of and operation.
1138 //
1139 NoSignFlag = true;
1140 // ClearsOverflowFlag is true for AND operation (no surprise).
1141 ClearsOverflowFlag = true;
1142 return true;
1143 }
1144 return false;
1145}
1146
1148 unsigned Opc, bool AllowSP, Register &NewSrc,
1149 unsigned &NewSrcSubReg, bool &isKill,
1150 MachineOperand &ImplicitOp, LiveVariables *LV,
1151 LiveIntervals *LIS) const {
1152 MachineFunction &MF = *MI.getParent()->getParent();
1153 const TargetRegisterClass *RC;
1154 if (AllowSP) {
1155 RC = Opc != X86::LEA32r ? &X86::GR64RegClass : &X86::GR32RegClass;
1156 } else {
1157 RC = Opc != X86::LEA32r ? &X86::GR64_NOSPRegClass : &X86::GR32_NOSPRegClass;
1158 }
1159 Register SrcReg = Src.getReg();
1160 unsigned SubReg = Src.getSubReg();
1161 isKill = MI.killsRegister(SrcReg, /*TRI=*/nullptr);
1162
1163 NewSrcSubReg = X86::NoSubRegister;
1164
1165 // For both LEA64 and LEA32 the register already has essentially the right
1166 // type (32-bit or 64-bit) we may just need to forbid SP.
1167 if (Opc != X86::LEA64_32r) {
1168 NewSrc = SrcReg;
1169 NewSrcSubReg = SubReg;
1170 assert(!Src.isUndef() && "Undef op doesn't need optimization");
1171
1172 if (NewSrc.isVirtual() && !MF.getRegInfo().constrainRegClass(NewSrc, RC))
1173 return false;
1174
1175 return true;
1176 }
1177
1178 // This is for an LEA64_32r and incoming registers are 32-bit. One way or
1179 // another we need to add 64-bit registers to the final MI.
1180 if (SrcReg.isPhysical()) {
1181 ImplicitOp = Src;
1182 ImplicitOp.setImplicit();
1183
1184 NewSrc = getX86SubSuperRegister(SrcReg, 64);
1185 assert(!SubReg && "no superregister for source");
1186 assert(NewSrc.isValid() && "Invalid Operand");
1187 assert(!Src.isUndef() && "Undef op doesn't need optimization");
1188 } else {
1189 // Virtual register of the wrong class, we have to create a temporary 64-bit
1190 // vreg to feed into the LEA.
1191 NewSrc = MF.getRegInfo().createVirtualRegister(RC);
1192 NewSrcSubReg = X86::NoSubRegister;
1193 MachineInstr *Copy =
1194 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(TargetOpcode::COPY))
1195 .addReg(NewSrc, RegState::Define | RegState::Undef, X86::sub_32bit)
1196 .addReg(SrcReg, getKillRegState(isKill), SubReg);
1197
1198 // Which is obviously going to be dead after we're done with it.
1199 isKill = true;
1200
1201 if (LV)
1202 LV->replaceKillInstruction(SrcReg, MI, *Copy);
1203
1204 if (LIS) {
1205 SlotIndex CopyIdx = LIS->InsertMachineInstrInMaps(*Copy);
1206 SlotIndex Idx = LIS->getInstructionIndex(MI);
1207 LiveInterval &LI = LIS->getInterval(SrcReg);
1209 if (S->end.getBaseIndex() == Idx)
1210 S->end = CopyIdx.getRegSlot();
1211 }
1212 }
1213
1214 // We've set all the parameters without issue.
1215 return true;
1216}
1217
1218MachineInstr *X86InstrInfo::convertToThreeAddressWithLEA(unsigned MIOpc,
1220 LiveVariables *LV,
1221 LiveIntervals *LIS,
1222 bool Is8BitOp) const {
1223 // We handle 8-bit adds and various 16-bit opcodes in the switch below.
1224 MachineBasicBlock &MBB = *MI.getParent();
1225 MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
1226 assert((Is8BitOp ||
1227 RegInfo.getTargetRegisterInfo()->getRegSizeInBits(
1228 *RegInfo.getRegClass(MI.getOperand(0).getReg())) == 16) &&
1229 "Unexpected type for LEA transform");
1230
1231 // TODO: For a 32-bit target, we need to adjust the LEA variables with
1232 // something like this:
1233 // Opcode = X86::LEA32r;
1234 // InRegLEA = RegInfo.createVirtualRegister(&X86::GR32_NOSPRegClass);
1235 // OutRegLEA =
1236 // Is8BitOp ? RegInfo.createVirtualRegister(&X86::GR32ABCD_RegClass)
1237 // : RegInfo.createVirtualRegister(&X86::GR32RegClass);
1238 if (!Subtarget.is64Bit())
1239 return nullptr;
1240
1241 unsigned Opcode = X86::LEA64_32r;
1242 Register InRegLEA = RegInfo.createVirtualRegister(&X86::GR64_NOSPRegClass);
1243 Register OutRegLEA = RegInfo.createVirtualRegister(&X86::GR32RegClass);
1244 Register InRegLEA2;
1245
1246 // Build and insert into an implicit UNDEF value. This is OK because
1247 // we will be shifting and then extracting the lower 8/16-bits.
1248 // This has the potential to cause partial register stall. e.g.
1249 // movw (%rbp,%rcx,2), %dx
1250 // leal -65(%rdx), %esi
1251 // But testing has shown this *does* help performance in 64-bit mode (at
1252 // least on modern x86 machines).
1253 MachineBasicBlock::iterator MBBI = MI.getIterator();
1254 Register Dest = MI.getOperand(0).getReg();
1255 Register Src = MI.getOperand(1).getReg();
1256 unsigned SrcSubReg = MI.getOperand(1).getSubReg();
1257 Register Src2;
1258 unsigned Src2SubReg;
1259 bool IsDead = MI.getOperand(0).isDead();
1260 bool IsKill = MI.getOperand(1).isKill();
1261 unsigned SubReg = Is8BitOp ? X86::sub_8bit : X86::sub_16bit;
1262 assert(!MI.getOperand(1).isUndef() && "Undef op doesn't need optimization");
1263 MachineInstr *ImpDef =
1264 BuildMI(MBB, MBBI, MI.getDebugLoc(), get(X86::IMPLICIT_DEF), InRegLEA);
1265 MachineInstr *InsMI =
1266 BuildMI(MBB, MBBI, MI.getDebugLoc(), get(TargetOpcode::COPY))
1267 .addReg(InRegLEA, RegState::Define, SubReg)
1268 .addReg(Src, getKillRegState(IsKill), SrcSubReg);
1269 MachineInstr *ImpDef2 = nullptr;
1270 MachineInstr *InsMI2 = nullptr;
1271
1273 BuildMI(MBB, MBBI, MI.getDebugLoc(), get(Opcode), OutRegLEA);
1274#define CASE_NF(OP) \
1275 case X86::OP: \
1276 case X86::OP##_NF:
1277 switch (MIOpc) {
1278 default:
1279 llvm_unreachable("Unreachable!");
1280 CASE_NF(SHL8ri)
1281 CASE_NF(SHL16ri) {
1282 unsigned ShAmt = MI.getOperand(2).getImm();
1283 MIB.addReg(0)
1284 .addImm(1LL << ShAmt)
1285 .addReg(InRegLEA, RegState::Kill)
1286 .addImm(0)
1287 .addReg(0);
1288 break;
1289 }
1290 CASE_NF(INC8r)
1291 CASE_NF(INC16r)
1292 addRegOffset(MIB, InRegLEA, true, 1);
1293 break;
1294 CASE_NF(DEC8r)
1295 CASE_NF(DEC16r)
1296 addRegOffset(MIB, InRegLEA, true, -1);
1297 break;
1298 CASE_NF(ADD8ri)
1299 CASE_NF(ADD16ri)
1300 case X86::ADD8ri_DB:
1301 case X86::ADD16ri_DB:
1302 addRegOffset(MIB, InRegLEA, true, MI.getOperand(2).getImm());
1303 break;
1304 CASE_NF(ADD8rr)
1305 CASE_NF(ADD16rr)
1306 case X86::ADD8rr_DB:
1307 case X86::ADD16rr_DB: {
1308 Src2 = MI.getOperand(2).getReg();
1309 Src2SubReg = MI.getOperand(2).getSubReg();
1310 bool IsKill2 = MI.getOperand(2).isKill();
1311 assert(!MI.getOperand(2).isUndef() && "Undef op doesn't need optimization");
1312 if (Src == Src2) {
1313 // ADD8rr/ADD16rr killed %reg1028, %reg1028
1314 // just a single insert_subreg.
1315 addRegReg(MIB, InRegLEA, true, X86::NoSubRegister, InRegLEA, false,
1316 X86::NoSubRegister);
1317 } else {
1318 if (Subtarget.is64Bit())
1319 InRegLEA2 = RegInfo.createVirtualRegister(&X86::GR64_NOSPRegClass);
1320 else
1321 InRegLEA2 = RegInfo.createVirtualRegister(&X86::GR32_NOSPRegClass);
1322 // Build and insert into an implicit UNDEF value. This is OK because
1323 // we will be shifting and then extracting the lower 8/16-bits.
1324 ImpDef2 = BuildMI(MBB, &*MIB, MI.getDebugLoc(), get(X86::IMPLICIT_DEF),
1325 InRegLEA2);
1326 InsMI2 = BuildMI(MBB, &*MIB, MI.getDebugLoc(), get(TargetOpcode::COPY))
1327 .addReg(InRegLEA2, RegState::Define, SubReg)
1328 .addReg(Src2, getKillRegState(IsKill2), Src2SubReg);
1329 addRegReg(MIB, InRegLEA, true, X86::NoSubRegister, InRegLEA2, true,
1330 X86::NoSubRegister);
1331 }
1332 if (LV && IsKill2 && InsMI2)
1333 LV->replaceKillInstruction(Src2, MI, *InsMI2);
1334 break;
1335 }
1336 }
1337
1338 MachineInstr *NewMI = MIB;
1339 MachineInstr *ExtMI =
1340 BuildMI(MBB, MBBI, MI.getDebugLoc(), get(TargetOpcode::COPY))
1342 .addReg(OutRegLEA, RegState::Kill, SubReg);
1343
1344 if (LV) {
1345 // Update live variables.
1346 LV->getVarInfo(InRegLEA).Kills.push_back(NewMI);
1347 if (InRegLEA2)
1348 LV->getVarInfo(InRegLEA2).Kills.push_back(NewMI);
1349 LV->getVarInfo(OutRegLEA).Kills.push_back(ExtMI);
1350 if (IsKill)
1351 LV->replaceKillInstruction(Src, MI, *InsMI);
1352 if (IsDead)
1353 LV->replaceKillInstruction(Dest, MI, *ExtMI);
1354 }
1355
1356 if (LIS) {
1357 LIS->InsertMachineInstrInMaps(*ImpDef);
1358 SlotIndex InsIdx = LIS->InsertMachineInstrInMaps(*InsMI);
1359 if (ImpDef2)
1360 LIS->InsertMachineInstrInMaps(*ImpDef2);
1361 SlotIndex Ins2Idx;
1362 if (InsMI2)
1363 Ins2Idx = LIS->InsertMachineInstrInMaps(*InsMI2);
1364 SlotIndex NewIdx = LIS->ReplaceMachineInstrInMaps(MI, *NewMI);
1365 SlotIndex ExtIdx = LIS->InsertMachineInstrInMaps(*ExtMI);
1366 LIS->getInterval(InRegLEA);
1367 LIS->getInterval(OutRegLEA);
1368 if (InRegLEA2)
1369 LIS->getInterval(InRegLEA2);
1370
1371 // Move the use of Src up to InsMI.
1372 LiveInterval &SrcLI = LIS->getInterval(Src);
1373 LiveRange::Segment *SrcSeg = SrcLI.getSegmentContaining(NewIdx);
1374 if (SrcSeg->end == NewIdx.getRegSlot())
1375 SrcSeg->end = InsIdx.getRegSlot();
1376
1377 if (InsMI2) {
1378 // Move the use of Src2 up to InsMI2.
1379 LiveInterval &Src2LI = LIS->getInterval(Src2);
1380 LiveRange::Segment *Src2Seg = Src2LI.getSegmentContaining(NewIdx);
1381 if (Src2Seg->end == NewIdx.getRegSlot())
1382 Src2Seg->end = Ins2Idx.getRegSlot();
1383 }
1384
1385 // Move the definition of Dest down to ExtMI.
1386 LiveInterval &DestLI = LIS->getInterval(Dest);
1387 LiveRange::Segment *DestSeg =
1388 DestLI.getSegmentContaining(NewIdx.getRegSlot());
1389 assert(DestSeg->start == NewIdx.getRegSlot() &&
1390 DestSeg->valno->def == NewIdx.getRegSlot());
1391 DestSeg->start = ExtIdx.getRegSlot();
1392 DestSeg->valno->def = ExtIdx.getRegSlot();
1393 }
1394
1395 return ExtMI;
1396}
1397
1398/// This method must be implemented by targets that
1399/// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
1400/// may be able to convert a two-address instruction into a true
1401/// three-address instruction on demand. This allows the X86 target (for
1402/// example) to convert ADD and SHL instructions into LEA instructions if they
1403/// would require register copies due to two-addressness.
1404///
1405/// This method returns a null pointer if the transformation cannot be
1406/// performed, otherwise it returns the new instruction.
1407///
1409 LiveVariables *LV,
1410 LiveIntervals *LIS) const {
1411 // The following opcodes also sets the condition code register(s). Only
1412 // convert them to equivalent lea if the condition code register def's
1413 // are dead!
1415 return nullptr;
1416
1417 MachineFunction &MF = *MI.getParent()->getParent();
1418 // All instructions input are two-addr instructions. Get the known operands.
1419 const MachineOperand &Dest = MI.getOperand(0);
1420 const MachineOperand &Src = MI.getOperand(1);
1421
1422 // Ideally, operations with undef should be folded before we get here, but we
1423 // can't guarantee it. Bail out because optimizing undefs is a waste of time.
1424 // Without this, we have to forward undef state to new register operands to
1425 // avoid machine verifier errors.
1426 if (Src.isUndef())
1427 return nullptr;
1428 if (MI.getNumOperands() > 2)
1429 if (MI.getOperand(2).isReg() && MI.getOperand(2).isUndef())
1430 return nullptr;
1431
1432 MachineInstr *NewMI = nullptr;
1433 Register SrcReg, SrcReg2;
1434 unsigned SrcSubReg, SrcSubReg2;
1435 bool Is64Bit = Subtarget.is64Bit();
1436
1437 bool Is8BitOp = false;
1438 unsigned NumRegOperands = 2;
1439 unsigned MIOpc = MI.getOpcode();
1440 switch (MIOpc) {
1441 default:
1442 llvm_unreachable("Unreachable!");
1443 CASE_NF(SHL64ri) {
1444 assert(MI.getNumOperands() >= 3 && "Unknown shift instruction!");
1445 unsigned ShAmt = getTruncatedShiftCount(MI, 2);
1446 if (!isTruncatedShiftCountForLEA(ShAmt))
1447 return nullptr;
1448
1449 // LEA can't handle RSP.
1450 if (Src.getReg().isVirtual() && !MF.getRegInfo().constrainRegClass(
1451 Src.getReg(), &X86::GR64_NOSPRegClass))
1452 return nullptr;
1453
1454 NewMI = BuildMI(MF, MI.getDebugLoc(), get(X86::LEA64r))
1455 .add(Dest)
1456 .addReg(0)
1457 .addImm(1LL << ShAmt)
1458 .add(Src)
1459 .addImm(0)
1460 .addReg(0);
1461 break;
1462 }
1463 CASE_NF(SHL32ri) {
1464 assert(MI.getNumOperands() >= 3 && "Unknown shift instruction!");
1465 unsigned ShAmt = getTruncatedShiftCount(MI, 2);
1466 if (!isTruncatedShiftCountForLEA(ShAmt))
1467 return nullptr;
1468
1469 unsigned Opc = Is64Bit ? X86::LEA64_32r : X86::LEA32r;
1470
1471 // LEA can't handle ESP.
1472 bool isKill;
1473 MachineOperand ImplicitOp = MachineOperand::CreateReg(0, false);
1474 if (!classifyLEAReg(MI, Src, Opc, /*AllowSP=*/false, SrcReg, SrcSubReg,
1475 isKill, ImplicitOp, LV, LIS))
1476 return nullptr;
1477
1479 BuildMI(MF, MI.getDebugLoc(), get(Opc))
1480 .add(Dest)
1481 .addReg(0)
1482 .addImm(1LL << ShAmt)
1483 .addReg(SrcReg, getKillRegState(isKill), SrcSubReg)
1484 .addImm(0)
1485 .addReg(0);
1486 if (ImplicitOp.getReg() != 0)
1487 MIB.add(ImplicitOp);
1488 NewMI = MIB;
1489
1490 // Add kills if classifyLEAReg created a new register.
1491 if (LV && SrcReg != Src.getReg())
1492 LV->getVarInfo(SrcReg).Kills.push_back(NewMI);
1493 break;
1494 }
1495 CASE_NF(SHL8ri)
1496 Is8BitOp = true;
1497 [[fallthrough]];
1498 CASE_NF(SHL16ri) {
1499 assert(MI.getNumOperands() >= 3 && "Unknown shift instruction!");
1500 unsigned ShAmt = getTruncatedShiftCount(MI, 2);
1501 if (!isTruncatedShiftCountForLEA(ShAmt))
1502 return nullptr;
1503 return convertToThreeAddressWithLEA(MIOpc, MI, LV, LIS, Is8BitOp);
1504 }
1505 CASE_NF(INC64r)
1506 CASE_NF(INC32r) {
1507 assert(MI.getNumOperands() >= 2 && "Unknown inc instruction!");
1508 unsigned Opc = (MIOpc == X86::INC64r || MIOpc == X86::INC64r_NF)
1509 ? X86::LEA64r
1510 : (Is64Bit ? X86::LEA64_32r : X86::LEA32r);
1511 bool isKill;
1512 MachineOperand ImplicitOp = MachineOperand::CreateReg(0, false);
1513 if (!classifyLEAReg(MI, Src, Opc, /*AllowSP=*/false, SrcReg, SrcSubReg,
1514 isKill, ImplicitOp, LV, LIS))
1515 return nullptr;
1516
1517 MachineInstrBuilder MIB = BuildMI(MF, MI.getDebugLoc(), get(Opc))
1518 .add(Dest)
1519 .addReg(SrcReg, getKillRegState(isKill));
1520 if (ImplicitOp.getReg() != 0)
1521 MIB.add(ImplicitOp);
1522
1523 NewMI = addOffset(MIB, 1);
1524
1525 // Add kills if classifyLEAReg created a new register.
1526 if (LV && SrcReg != Src.getReg())
1527 LV->getVarInfo(SrcReg).Kills.push_back(NewMI);
1528 break;
1529 }
1530 CASE_NF(DEC64r)
1531 CASE_NF(DEC32r) {
1532 assert(MI.getNumOperands() >= 2 && "Unknown dec instruction!");
1533 unsigned Opc = (MIOpc == X86::DEC64r || MIOpc == X86::DEC64r_NF)
1534 ? X86::LEA64r
1535 : (Is64Bit ? X86::LEA64_32r : X86::LEA32r);
1536
1537 bool isKill;
1538 MachineOperand ImplicitOp = MachineOperand::CreateReg(0, false);
1539 if (!classifyLEAReg(MI, Src, Opc, /*AllowSP=*/false, SrcReg, SrcSubReg,
1540 isKill, ImplicitOp, LV, LIS))
1541 return nullptr;
1542
1543 MachineInstrBuilder MIB = BuildMI(MF, MI.getDebugLoc(), get(Opc))
1544 .add(Dest)
1545 .addReg(SrcReg, getKillRegState(isKill));
1546 if (ImplicitOp.getReg() != 0)
1547 MIB.add(ImplicitOp);
1548
1549 NewMI = addOffset(MIB, -1);
1550
1551 // Add kills if classifyLEAReg created a new register.
1552 if (LV && SrcReg != Src.getReg())
1553 LV->getVarInfo(SrcReg).Kills.push_back(NewMI);
1554 break;
1555 }
1556 CASE_NF(DEC8r)
1557 CASE_NF(INC8r)
1558 Is8BitOp = true;
1559 [[fallthrough]];
1560 CASE_NF(DEC16r)
1561 CASE_NF(INC16r)
1562 return convertToThreeAddressWithLEA(MIOpc, MI, LV, LIS, Is8BitOp);
1563 CASE_NF(ADD64rr)
1564 CASE_NF(ADD32rr)
1565 case X86::ADD64rr_DB:
1566 case X86::ADD32rr_DB: {
1567 assert(MI.getNumOperands() >= 3 && "Unknown add instruction!");
1568 unsigned Opc;
1569 if (MIOpc == X86::ADD64rr || MIOpc == X86::ADD64rr_NF ||
1570 MIOpc == X86::ADD64rr_DB)
1571 Opc = X86::LEA64r;
1572 else
1573 Opc = Is64Bit ? X86::LEA64_32r : X86::LEA32r;
1574
1575 const MachineOperand &Src2 = MI.getOperand(2);
1576 bool isKill2;
1577 MachineOperand ImplicitOp2 = MachineOperand::CreateReg(0, false);
1578 if (!classifyLEAReg(MI, Src2, Opc, /*AllowSP=*/false, SrcReg2, SrcSubReg2,
1579 isKill2, ImplicitOp2, LV, LIS))
1580 return nullptr;
1581
1582 bool isKill;
1583 MachineOperand ImplicitOp = MachineOperand::CreateReg(0, false);
1584 if (Src.getReg() == Src2.getReg()) {
1585 // Don't call classify LEAReg a second time on the same register, in case
1586 // the first call inserted a COPY from Src2 and marked it as killed.
1587 isKill = isKill2;
1588 SrcReg = SrcReg2;
1589 SrcSubReg = SrcSubReg2;
1590 } else {
1591 if (!classifyLEAReg(MI, Src, Opc, /*AllowSP=*/true, SrcReg, SrcSubReg,
1592 isKill, ImplicitOp, LV, LIS))
1593 return nullptr;
1594 }
1595
1596 MachineInstrBuilder MIB = BuildMI(MF, MI.getDebugLoc(), get(Opc)).add(Dest);
1597 if (ImplicitOp.getReg() != 0)
1598 MIB.add(ImplicitOp);
1599 if (ImplicitOp2.getReg() != 0)
1600 MIB.add(ImplicitOp2);
1601
1602 NewMI =
1603 addRegReg(MIB, SrcReg, isKill, SrcSubReg, SrcReg2, isKill2, SrcSubReg2);
1604
1605 // Add kills if classifyLEAReg created a new register.
1606 if (LV) {
1607 if (SrcReg2 != Src2.getReg())
1608 LV->getVarInfo(SrcReg2).Kills.push_back(NewMI);
1609 if (SrcReg != SrcReg2 && SrcReg != Src.getReg())
1610 LV->getVarInfo(SrcReg).Kills.push_back(NewMI);
1611 }
1612 NumRegOperands = 3;
1613 break;
1614 }
1615 CASE_NF(ADD8rr)
1616 case X86::ADD8rr_DB:
1617 Is8BitOp = true;
1618 [[fallthrough]];
1619 CASE_NF(ADD16rr)
1620 case X86::ADD16rr_DB:
1621 return convertToThreeAddressWithLEA(MIOpc, MI, LV, LIS, Is8BitOp);
1622 CASE_NF(ADD64ri32)
1623 case X86::ADD64ri32_DB:
1624 assert(MI.getNumOperands() >= 3 && "Unknown add instruction!");
1625 NewMI = addOffset(
1626 BuildMI(MF, MI.getDebugLoc(), get(X86::LEA64r)).add(Dest).add(Src),
1627 MI.getOperand(2));
1628 break;
1629 CASE_NF(ADD32ri)
1630 case X86::ADD32ri_DB: {
1631 assert(MI.getNumOperands() >= 3 && "Unknown add instruction!");
1632 unsigned Opc = Is64Bit ? X86::LEA64_32r : X86::LEA32r;
1633
1634 bool isKill;
1635 MachineOperand ImplicitOp = MachineOperand::CreateReg(0, false);
1636 if (!classifyLEAReg(MI, Src, Opc, /*AllowSP=*/true, SrcReg, SrcSubReg,
1637 isKill, ImplicitOp, LV, LIS))
1638 return nullptr;
1639
1641 BuildMI(MF, MI.getDebugLoc(), get(Opc))
1642 .add(Dest)
1643 .addReg(SrcReg, getKillRegState(isKill), SrcSubReg);
1644 if (ImplicitOp.getReg() != 0)
1645 MIB.add(ImplicitOp);
1646
1647 NewMI = addOffset(MIB, MI.getOperand(2));
1648
1649 // Add kills if classifyLEAReg created a new register.
1650 if (LV && SrcReg != Src.getReg())
1651 LV->getVarInfo(SrcReg).Kills.push_back(NewMI);
1652 break;
1653 }
1654 CASE_NF(ADD8ri)
1655 case X86::ADD8ri_DB:
1656 Is8BitOp = true;
1657 [[fallthrough]];
1658 CASE_NF(ADD16ri)
1659 case X86::ADD16ri_DB:
1660 return convertToThreeAddressWithLEA(MIOpc, MI, LV, LIS, Is8BitOp);
1661 CASE_NF(SUB8ri)
1662 CASE_NF(SUB16ri)
1663 /// FIXME: Support these similar to ADD8ri/ADD16ri*.
1664 return nullptr;
1665 CASE_NF(SUB32ri) {
1666 if (!MI.getOperand(2).isImm())
1667 return nullptr;
1668 int64_t Imm = MI.getOperand(2).getImm();
1669 if (!isInt<32>(-Imm))
1670 return nullptr;
1671
1672 assert(MI.getNumOperands() >= 3 && "Unknown add instruction!");
1673 unsigned Opc = Is64Bit ? X86::LEA64_32r : X86::LEA32r;
1674
1675 bool isKill;
1676 MachineOperand ImplicitOp = MachineOperand::CreateReg(0, false);
1677 if (!classifyLEAReg(MI, Src, Opc, /*AllowSP=*/true, SrcReg, SrcSubReg,
1678 isKill, ImplicitOp, LV, LIS))
1679 return nullptr;
1680
1682 BuildMI(MF, MI.getDebugLoc(), get(Opc))
1683 .add(Dest)
1684 .addReg(SrcReg, getKillRegState(isKill), SrcSubReg);
1685 if (ImplicitOp.getReg() != 0)
1686 MIB.add(ImplicitOp);
1687
1688 NewMI = addOffset(MIB, -Imm);
1689
1690 // Add kills if classifyLEAReg created a new register.
1691 if (LV && SrcReg != Src.getReg())
1692 LV->getVarInfo(SrcReg).Kills.push_back(NewMI);
1693 break;
1694 }
1695
1696 CASE_NF(SUB64ri32) {
1697 if (!MI.getOperand(2).isImm())
1698 return nullptr;
1699 int64_t Imm = MI.getOperand(2).getImm();
1700 if (!isInt<32>(-Imm))
1701 return nullptr;
1702
1703 assert(MI.getNumOperands() >= 3 && "Unknown sub instruction!");
1704
1706 BuildMI(MF, MI.getDebugLoc(), get(X86::LEA64r)).add(Dest).add(Src);
1707 NewMI = addOffset(MIB, -Imm);
1708 break;
1709 }
1710
1711 case X86::VMOVDQU8Z128rmk:
1712 case X86::VMOVDQU8Z256rmk:
1713 case X86::VMOVDQU8Zrmk:
1714 case X86::VMOVDQU16Z128rmk:
1715 case X86::VMOVDQU16Z256rmk:
1716 case X86::VMOVDQU16Zrmk:
1717 case X86::VMOVDQU32Z128rmk:
1718 case X86::VMOVDQA32Z128rmk:
1719 case X86::VMOVDQU32Z256rmk:
1720 case X86::VMOVDQA32Z256rmk:
1721 case X86::VMOVDQU32Zrmk:
1722 case X86::VMOVDQA32Zrmk:
1723 case X86::VMOVDQU64Z128rmk:
1724 case X86::VMOVDQA64Z128rmk:
1725 case X86::VMOVDQU64Z256rmk:
1726 case X86::VMOVDQA64Z256rmk:
1727 case X86::VMOVDQU64Zrmk:
1728 case X86::VMOVDQA64Zrmk:
1729 case X86::VMOVUPDZ128rmk:
1730 case X86::VMOVAPDZ128rmk:
1731 case X86::VMOVUPDZ256rmk:
1732 case X86::VMOVAPDZ256rmk:
1733 case X86::VMOVUPDZrmk:
1734 case X86::VMOVAPDZrmk:
1735 case X86::VMOVUPSZ128rmk:
1736 case X86::VMOVAPSZ128rmk:
1737 case X86::VMOVUPSZ256rmk:
1738 case X86::VMOVAPSZ256rmk:
1739 case X86::VMOVUPSZrmk:
1740 case X86::VMOVAPSZrmk:
1741 case X86::VBROADCASTSDZ256rmk:
1742 case X86::VBROADCASTSDZrmk:
1743 case X86::VBROADCASTSSZ128rmk:
1744 case X86::VBROADCASTSSZ256rmk:
1745 case X86::VBROADCASTSSZrmk:
1746 case X86::VPBROADCASTDZ128rmk:
1747 case X86::VPBROADCASTDZ256rmk:
1748 case X86::VPBROADCASTDZrmk:
1749 case X86::VPBROADCASTQZ128rmk:
1750 case X86::VPBROADCASTQZ256rmk:
1751 case X86::VPBROADCASTQZrmk: {
1752 unsigned Opc;
1753 switch (MIOpc) {
1754 default:
1755 llvm_unreachable("Unreachable!");
1756 case X86::VMOVDQU8Z128rmk:
1757 Opc = X86::VPBLENDMBZ128rmk;
1758 break;
1759 case X86::VMOVDQU8Z256rmk:
1760 Opc = X86::VPBLENDMBZ256rmk;
1761 break;
1762 case X86::VMOVDQU8Zrmk:
1763 Opc = X86::VPBLENDMBZrmk;
1764 break;
1765 case X86::VMOVDQU16Z128rmk:
1766 Opc = X86::VPBLENDMWZ128rmk;
1767 break;
1768 case X86::VMOVDQU16Z256rmk:
1769 Opc = X86::VPBLENDMWZ256rmk;
1770 break;
1771 case X86::VMOVDQU16Zrmk:
1772 Opc = X86::VPBLENDMWZrmk;
1773 break;
1774 case X86::VMOVDQU32Z128rmk:
1775 Opc = X86::VPBLENDMDZ128rmk;
1776 break;
1777 case X86::VMOVDQU32Z256rmk:
1778 Opc = X86::VPBLENDMDZ256rmk;
1779 break;
1780 case X86::VMOVDQU32Zrmk:
1781 Opc = X86::VPBLENDMDZrmk;
1782 break;
1783 case X86::VMOVDQU64Z128rmk:
1784 Opc = X86::VPBLENDMQZ128rmk;
1785 break;
1786 case X86::VMOVDQU64Z256rmk:
1787 Opc = X86::VPBLENDMQZ256rmk;
1788 break;
1789 case X86::VMOVDQU64Zrmk:
1790 Opc = X86::VPBLENDMQZrmk;
1791 break;
1792 case X86::VMOVUPDZ128rmk:
1793 Opc = X86::VBLENDMPDZ128rmk;
1794 break;
1795 case X86::VMOVUPDZ256rmk:
1796 Opc = X86::VBLENDMPDZ256rmk;
1797 break;
1798 case X86::VMOVUPDZrmk:
1799 Opc = X86::VBLENDMPDZrmk;
1800 break;
1801 case X86::VMOVUPSZ128rmk:
1802 Opc = X86::VBLENDMPSZ128rmk;
1803 break;
1804 case X86::VMOVUPSZ256rmk:
1805 Opc = X86::VBLENDMPSZ256rmk;
1806 break;
1807 case X86::VMOVUPSZrmk:
1808 Opc = X86::VBLENDMPSZrmk;
1809 break;
1810 case X86::VMOVDQA32Z128rmk:
1811 Opc = X86::VPBLENDMDZ128rmk;
1812 break;
1813 case X86::VMOVDQA32Z256rmk:
1814 Opc = X86::VPBLENDMDZ256rmk;
1815 break;
1816 case X86::VMOVDQA32Zrmk:
1817 Opc = X86::VPBLENDMDZrmk;
1818 break;
1819 case X86::VMOVDQA64Z128rmk:
1820 Opc = X86::VPBLENDMQZ128rmk;
1821 break;
1822 case X86::VMOVDQA64Z256rmk:
1823 Opc = X86::VPBLENDMQZ256rmk;
1824 break;
1825 case X86::VMOVDQA64Zrmk:
1826 Opc = X86::VPBLENDMQZrmk;
1827 break;
1828 case X86::VMOVAPDZ128rmk:
1829 Opc = X86::VBLENDMPDZ128rmk;
1830 break;
1831 case X86::VMOVAPDZ256rmk:
1832 Opc = X86::VBLENDMPDZ256rmk;
1833 break;
1834 case X86::VMOVAPDZrmk:
1835 Opc = X86::VBLENDMPDZrmk;
1836 break;
1837 case X86::VMOVAPSZ128rmk:
1838 Opc = X86::VBLENDMPSZ128rmk;
1839 break;
1840 case X86::VMOVAPSZ256rmk:
1841 Opc = X86::VBLENDMPSZ256rmk;
1842 break;
1843 case X86::VMOVAPSZrmk:
1844 Opc = X86::VBLENDMPSZrmk;
1845 break;
1846 case X86::VBROADCASTSDZ256rmk:
1847 Opc = X86::VBLENDMPDZ256rmbk;
1848 break;
1849 case X86::VBROADCASTSDZrmk:
1850 Opc = X86::VBLENDMPDZrmbk;
1851 break;
1852 case X86::VBROADCASTSSZ128rmk:
1853 Opc = X86::VBLENDMPSZ128rmbk;
1854 break;
1855 case X86::VBROADCASTSSZ256rmk:
1856 Opc = X86::VBLENDMPSZ256rmbk;
1857 break;
1858 case X86::VBROADCASTSSZrmk:
1859 Opc = X86::VBLENDMPSZrmbk;
1860 break;
1861 case X86::VPBROADCASTDZ128rmk:
1862 Opc = X86::VPBLENDMDZ128rmbk;
1863 break;
1864 case X86::VPBROADCASTDZ256rmk:
1865 Opc = X86::VPBLENDMDZ256rmbk;
1866 break;
1867 case X86::VPBROADCASTDZrmk:
1868 Opc = X86::VPBLENDMDZrmbk;
1869 break;
1870 case X86::VPBROADCASTQZ128rmk:
1871 Opc = X86::VPBLENDMQZ128rmbk;
1872 break;
1873 case X86::VPBROADCASTQZ256rmk:
1874 Opc = X86::VPBLENDMQZ256rmbk;
1875 break;
1876 case X86::VPBROADCASTQZrmk:
1877 Opc = X86::VPBLENDMQZrmbk;
1878 break;
1879 }
1880
1881 NewMI = BuildMI(MF, MI.getDebugLoc(), get(Opc))
1882 .add(Dest)
1883 .add(MI.getOperand(2))
1884 .add(Src)
1885 .add(MI.getOperand(3))
1886 .add(MI.getOperand(4))
1887 .add(MI.getOperand(5))
1888 .add(MI.getOperand(6))
1889 .add(MI.getOperand(7));
1890 NumRegOperands = 4;
1891 break;
1892 }
1893
1894 case X86::VMOVDQU8Z128rrk:
1895 case X86::VMOVDQU8Z256rrk:
1896 case X86::VMOVDQU8Zrrk:
1897 case X86::VMOVDQU16Z128rrk:
1898 case X86::VMOVDQU16Z256rrk:
1899 case X86::VMOVDQU16Zrrk:
1900 case X86::VMOVDQU32Z128rrk:
1901 case X86::VMOVDQA32Z128rrk:
1902 case X86::VMOVDQU32Z256rrk:
1903 case X86::VMOVDQA32Z256rrk:
1904 case X86::VMOVDQU32Zrrk:
1905 case X86::VMOVDQA32Zrrk:
1906 case X86::VMOVDQU64Z128rrk:
1907 case X86::VMOVDQA64Z128rrk:
1908 case X86::VMOVDQU64Z256rrk:
1909 case X86::VMOVDQA64Z256rrk:
1910 case X86::VMOVDQU64Zrrk:
1911 case X86::VMOVDQA64Zrrk:
1912 case X86::VMOVUPDZ128rrk:
1913 case X86::VMOVAPDZ128rrk:
1914 case X86::VMOVUPDZ256rrk:
1915 case X86::VMOVAPDZ256rrk:
1916 case X86::VMOVUPDZrrk:
1917 case X86::VMOVAPDZrrk:
1918 case X86::VMOVUPSZ128rrk:
1919 case X86::VMOVAPSZ128rrk:
1920 case X86::VMOVUPSZ256rrk:
1921 case X86::VMOVAPSZ256rrk:
1922 case X86::VMOVUPSZrrk:
1923 case X86::VMOVAPSZrrk: {
1924 unsigned Opc;
1925 switch (MIOpc) {
1926 default:
1927 llvm_unreachable("Unreachable!");
1928 case X86::VMOVDQU8Z128rrk:
1929 Opc = X86::VPBLENDMBZ128rrk;
1930 break;
1931 case X86::VMOVDQU8Z256rrk:
1932 Opc = X86::VPBLENDMBZ256rrk;
1933 break;
1934 case X86::VMOVDQU8Zrrk:
1935 Opc = X86::VPBLENDMBZrrk;
1936 break;
1937 case X86::VMOVDQU16Z128rrk:
1938 Opc = X86::VPBLENDMWZ128rrk;
1939 break;
1940 case X86::VMOVDQU16Z256rrk:
1941 Opc = X86::VPBLENDMWZ256rrk;
1942 break;
1943 case X86::VMOVDQU16Zrrk:
1944 Opc = X86::VPBLENDMWZrrk;
1945 break;
1946 case X86::VMOVDQU32Z128rrk:
1947 Opc = X86::VPBLENDMDZ128rrk;
1948 break;
1949 case X86::VMOVDQU32Z256rrk:
1950 Opc = X86::VPBLENDMDZ256rrk;
1951 break;
1952 case X86::VMOVDQU32Zrrk:
1953 Opc = X86::VPBLENDMDZrrk;
1954 break;
1955 case X86::VMOVDQU64Z128rrk:
1956 Opc = X86::VPBLENDMQZ128rrk;
1957 break;
1958 case X86::VMOVDQU64Z256rrk:
1959 Opc = X86::VPBLENDMQZ256rrk;
1960 break;
1961 case X86::VMOVDQU64Zrrk:
1962 Opc = X86::VPBLENDMQZrrk;
1963 break;
1964 case X86::VMOVUPDZ128rrk:
1965 Opc = X86::VBLENDMPDZ128rrk;
1966 break;
1967 case X86::VMOVUPDZ256rrk:
1968 Opc = X86::VBLENDMPDZ256rrk;
1969 break;
1970 case X86::VMOVUPDZrrk:
1971 Opc = X86::VBLENDMPDZrrk;
1972 break;
1973 case X86::VMOVUPSZ128rrk:
1974 Opc = X86::VBLENDMPSZ128rrk;
1975 break;
1976 case X86::VMOVUPSZ256rrk:
1977 Opc = X86::VBLENDMPSZ256rrk;
1978 break;
1979 case X86::VMOVUPSZrrk:
1980 Opc = X86::VBLENDMPSZrrk;
1981 break;
1982 case X86::VMOVDQA32Z128rrk:
1983 Opc = X86::VPBLENDMDZ128rrk;
1984 break;
1985 case X86::VMOVDQA32Z256rrk:
1986 Opc = X86::VPBLENDMDZ256rrk;
1987 break;
1988 case X86::VMOVDQA32Zrrk:
1989 Opc = X86::VPBLENDMDZrrk;
1990 break;
1991 case X86::VMOVDQA64Z128rrk:
1992 Opc = X86::VPBLENDMQZ128rrk;
1993 break;
1994 case X86::VMOVDQA64Z256rrk:
1995 Opc = X86::VPBLENDMQZ256rrk;
1996 break;
1997 case X86::VMOVDQA64Zrrk:
1998 Opc = X86::VPBLENDMQZrrk;
1999 break;
2000 case X86::VMOVAPDZ128rrk:
2001 Opc = X86::VBLENDMPDZ128rrk;
2002 break;
2003 case X86::VMOVAPDZ256rrk:
2004 Opc = X86::VBLENDMPDZ256rrk;
2005 break;
2006 case X86::VMOVAPDZrrk:
2007 Opc = X86::VBLENDMPDZrrk;
2008 break;
2009 case X86::VMOVAPSZ128rrk:
2010 Opc = X86::VBLENDMPSZ128rrk;
2011 break;
2012 case X86::VMOVAPSZ256rrk:
2013 Opc = X86::VBLENDMPSZ256rrk;
2014 break;
2015 case X86::VMOVAPSZrrk:
2016 Opc = X86::VBLENDMPSZrrk;
2017 break;
2018 }
2019
2020 NewMI = BuildMI(MF, MI.getDebugLoc(), get(Opc))
2021 .add(Dest)
2022 .add(MI.getOperand(2))
2023 .add(Src)
2024 .add(MI.getOperand(3));
2025 NumRegOperands = 4;
2026 break;
2027 }
2028 }
2029#undef CASE_NF
2030
2031 if (!NewMI)
2032 return nullptr;
2033
2034 if (LV) { // Update live variables
2035 for (unsigned I = 0; I < NumRegOperands; ++I) {
2036 MachineOperand &Op = MI.getOperand(I);
2037 if (Op.isReg() && (Op.isDead() || Op.isKill()))
2038 LV->replaceKillInstruction(Op.getReg(), MI, *NewMI);
2039 }
2040 }
2041
2042 MachineBasicBlock &MBB = *MI.getParent();
2043 MBB.insert(MI.getIterator(), NewMI); // Insert the new inst
2044
2045 if (LIS) {
2046 LIS->ReplaceMachineInstrInMaps(MI, *NewMI);
2047 if (SrcReg)
2048 LIS->getInterval(SrcReg);
2049 if (SrcReg2)
2050 LIS->getInterval(SrcReg2);
2051 }
2052
2053 return NewMI;
2054}
2055
2056/// This determines which of three possible cases of a three source commute
2057/// the source indexes correspond to taking into account any mask operands.
2058/// All prevents commuting a passthru operand. Returns -1 if the commute isn't
2059/// possible.
2060/// Case 0 - Possible to commute the first and second operands.
2061/// Case 1 - Possible to commute the first and third operands.
2062/// Case 2 - Possible to commute the second and third operands.
2063static unsigned getThreeSrcCommuteCase(uint64_t TSFlags, unsigned SrcOpIdx1,
2064 unsigned SrcOpIdx2) {
2065 // Put the lowest index to SrcOpIdx1 to simplify the checks below.
2066 if (SrcOpIdx1 > SrcOpIdx2)
2067 std::swap(SrcOpIdx1, SrcOpIdx2);
2068
2069 unsigned Op1 = 1, Op2 = 2, Op3 = 3;
2070 if (X86II::isKMasked(TSFlags)) {
2071 Op2++;
2072 Op3++;
2073 }
2074
2075 if (SrcOpIdx1 == Op1 && SrcOpIdx2 == Op2)
2076 return 0;
2077 if (SrcOpIdx1 == Op1 && SrcOpIdx2 == Op3)
2078 return 1;
2079 if (SrcOpIdx1 == Op2 && SrcOpIdx2 == Op3)
2080 return 2;
2081 llvm_unreachable("Unknown three src commute case.");
2082}
2083
2085 const MachineInstr &MI, unsigned SrcOpIdx1, unsigned SrcOpIdx2,
2086 const X86InstrFMA3Group &FMA3Group) const {
2087
2088 unsigned Opc = MI.getOpcode();
2089
2090 // TODO: Commuting the 1st operand of FMA*_Int requires some additional
2091 // analysis. The commute optimization is legal only if all users of FMA*_Int
2092 // use only the lowest element of the FMA*_Int instruction. Such analysis are
2093 // not implemented yet. So, just return 0 in that case.
2094 // When such analysis are available this place will be the right place for
2095 // calling it.
2096 assert(!(FMA3Group.isIntrinsic() && (SrcOpIdx1 == 1 || SrcOpIdx2 == 1)) &&
2097 "Intrinsic instructions can't commute operand 1");
2098
2099 // Determine which case this commute is or if it can't be done.
2100 unsigned Case =
2101 getThreeSrcCommuteCase(MI.getDesc().TSFlags, SrcOpIdx1, SrcOpIdx2);
2102 assert(Case < 3 && "Unexpected case number!");
2103
2104 // Define the FMA forms mapping array that helps to map input FMA form
2105 // to output FMA form to preserve the operation semantics after
2106 // commuting the operands.
2107 const unsigned Form132Index = 0;
2108 const unsigned Form213Index = 1;
2109 const unsigned Form231Index = 2;
2110 static const unsigned FormMapping[][3] = {
2111 // 0: SrcOpIdx1 == 1 && SrcOpIdx2 == 2;
2112 // FMA132 A, C, b; ==> FMA231 C, A, b;
2113 // FMA213 B, A, c; ==> FMA213 A, B, c;
2114 // FMA231 C, A, b; ==> FMA132 A, C, b;
2115 {Form231Index, Form213Index, Form132Index},
2116 // 1: SrcOpIdx1 == 1 && SrcOpIdx2 == 3;
2117 // FMA132 A, c, B; ==> FMA132 B, c, A;
2118 // FMA213 B, a, C; ==> FMA231 C, a, B;
2119 // FMA231 C, a, B; ==> FMA213 B, a, C;
2120 {Form132Index, Form231Index, Form213Index},
2121 // 2: SrcOpIdx1 == 2 && SrcOpIdx2 == 3;
2122 // FMA132 a, C, B; ==> FMA213 a, B, C;
2123 // FMA213 b, A, C; ==> FMA132 b, C, A;
2124 // FMA231 c, A, B; ==> FMA231 c, B, A;
2125 {Form213Index, Form132Index, Form231Index}};
2126
2127 unsigned FMAForms[3];
2128 FMAForms[0] = FMA3Group.get132Opcode();
2129 FMAForms[1] = FMA3Group.get213Opcode();
2130 FMAForms[2] = FMA3Group.get231Opcode();
2131
2132 // Everything is ready, just adjust the FMA opcode and return it.
2133 for (unsigned FormIndex = 0; FormIndex < 3; FormIndex++)
2134 if (Opc == FMAForms[FormIndex])
2135 return FMAForms[FormMapping[Case][FormIndex]];
2136
2137 llvm_unreachable("Illegal FMA3 format");
2138}
2139
2140static void commuteVPTERNLOG(MachineInstr &MI, unsigned SrcOpIdx1,
2141 unsigned SrcOpIdx2) {
2142 // Determine which case this commute is or if it can't be done.
2143 unsigned Case =
2144 getThreeSrcCommuteCase(MI.getDesc().TSFlags, SrcOpIdx1, SrcOpIdx2);
2145 assert(Case < 3 && "Unexpected case value!");
2146
2147 // For each case we need to swap two pairs of bits in the final immediate.
2148 static const uint8_t SwapMasks[3][4] = {
2149 {0x04, 0x10, 0x08, 0x20}, // Swap bits 2/4 and 3/5.
2150 {0x02, 0x10, 0x08, 0x40}, // Swap bits 1/4 and 3/6.
2151 {0x02, 0x04, 0x20, 0x40}, // Swap bits 1/2 and 5/6.
2152 };
2153
2154 uint8_t Imm = MI.getOperand(MI.getNumOperands() - 1).getImm();
2155 // Clear out the bits we are swapping.
2156 uint8_t NewImm = Imm & ~(SwapMasks[Case][0] | SwapMasks[Case][1] |
2157 SwapMasks[Case][2] | SwapMasks[Case][3]);
2158 // If the immediate had a bit of the pair set, then set the opposite bit.
2159 if (Imm & SwapMasks[Case][0])
2160 NewImm |= SwapMasks[Case][1];
2161 if (Imm & SwapMasks[Case][1])
2162 NewImm |= SwapMasks[Case][0];
2163 if (Imm & SwapMasks[Case][2])
2164 NewImm |= SwapMasks[Case][3];
2165 if (Imm & SwapMasks[Case][3])
2166 NewImm |= SwapMasks[Case][2];
2167 MI.getOperand(MI.getNumOperands() - 1).setImm(NewImm);
2168}
2169
2170// Returns true if this is a VPERMI2 or VPERMT2 instruction that can be
2171// commuted.
2172static bool isCommutableVPERMV3Instruction(unsigned Opcode) {
2173#define VPERM_CASES(Suffix) \
2174 case X86::VPERMI2##Suffix##Z128rr: \
2175 case X86::VPERMT2##Suffix##Z128rr: \
2176 case X86::VPERMI2##Suffix##Z256rr: \
2177 case X86::VPERMT2##Suffix##Z256rr: \
2178 case X86::VPERMI2##Suffix##Zrr: \
2179 case X86::VPERMT2##Suffix##Zrr: \
2180 case X86::VPERMI2##Suffix##Z128rm: \
2181 case X86::VPERMT2##Suffix##Z128rm: \
2182 case X86::VPERMI2##Suffix##Z256rm: \
2183 case X86::VPERMT2##Suffix##Z256rm: \
2184 case X86::VPERMI2##Suffix##Zrm: \
2185 case X86::VPERMT2##Suffix##Zrm: \
2186 case X86::VPERMI2##Suffix##Z128rrkz: \
2187 case X86::VPERMT2##Suffix##Z128rrkz: \
2188 case X86::VPERMI2##Suffix##Z256rrkz: \
2189 case X86::VPERMT2##Suffix##Z256rrkz: \
2190 case X86::VPERMI2##Suffix##Zrrkz: \
2191 case X86::VPERMT2##Suffix##Zrrkz: \
2192 case X86::VPERMI2##Suffix##Z128rmkz: \
2193 case X86::VPERMT2##Suffix##Z128rmkz: \
2194 case X86::VPERMI2##Suffix##Z256rmkz: \
2195 case X86::VPERMT2##Suffix##Z256rmkz: \
2196 case X86::VPERMI2##Suffix##Zrmkz: \
2197 case X86::VPERMT2##Suffix##Zrmkz:
2198
2199#define VPERM_CASES_BROADCAST(Suffix) \
2200 VPERM_CASES(Suffix) \
2201 case X86::VPERMI2##Suffix##Z128rmb: \
2202 case X86::VPERMT2##Suffix##Z128rmb: \
2203 case X86::VPERMI2##Suffix##Z256rmb: \
2204 case X86::VPERMT2##Suffix##Z256rmb: \
2205 case X86::VPERMI2##Suffix##Zrmb: \
2206 case X86::VPERMT2##Suffix##Zrmb: \
2207 case X86::VPERMI2##Suffix##Z128rmbkz: \
2208 case X86::VPERMT2##Suffix##Z128rmbkz: \
2209 case X86::VPERMI2##Suffix##Z256rmbkz: \
2210 case X86::VPERMT2##Suffix##Z256rmbkz: \
2211 case X86::VPERMI2##Suffix##Zrmbkz: \
2212 case X86::VPERMT2##Suffix##Zrmbkz:
2213
2214 switch (Opcode) {
2215 default:
2216 return false;
2217 VPERM_CASES(B)
2222 VPERM_CASES(W)
2223 return true;
2224 }
2225#undef VPERM_CASES_BROADCAST
2226#undef VPERM_CASES
2227}
2228
2229// Returns commuted opcode for VPERMI2 and VPERMT2 instructions by switching
2230// from the I opcode to the T opcode and vice versa.
2231static unsigned getCommutedVPERMV3Opcode(unsigned Opcode) {
2232#define VPERM_CASES(Orig, New) \
2233 case X86::Orig##Z128rr: \
2234 return X86::New##Z128rr; \
2235 case X86::Orig##Z128rrkz: \
2236 return X86::New##Z128rrkz; \
2237 case X86::Orig##Z128rm: \
2238 return X86::New##Z128rm; \
2239 case X86::Orig##Z128rmkz: \
2240 return X86::New##Z128rmkz; \
2241 case X86::Orig##Z256rr: \
2242 return X86::New##Z256rr; \
2243 case X86::Orig##Z256rrkz: \
2244 return X86::New##Z256rrkz; \
2245 case X86::Orig##Z256rm: \
2246 return X86::New##Z256rm; \
2247 case X86::Orig##Z256rmkz: \
2248 return X86::New##Z256rmkz; \
2249 case X86::Orig##Zrr: \
2250 return X86::New##Zrr; \
2251 case X86::Orig##Zrrkz: \
2252 return X86::New##Zrrkz; \
2253 case X86::Orig##Zrm: \
2254 return X86::New##Zrm; \
2255 case X86::Orig##Zrmkz: \
2256 return X86::New##Zrmkz;
2257
2258#define VPERM_CASES_BROADCAST(Orig, New) \
2259 VPERM_CASES(Orig, New) \
2260 case X86::Orig##Z128rmb: \
2261 return X86::New##Z128rmb; \
2262 case X86::Orig##Z128rmbkz: \
2263 return X86::New##Z128rmbkz; \
2264 case X86::Orig##Z256rmb: \
2265 return X86::New##Z256rmb; \
2266 case X86::Orig##Z256rmbkz: \
2267 return X86::New##Z256rmbkz; \
2268 case X86::Orig##Zrmb: \
2269 return X86::New##Zrmb; \
2270 case X86::Orig##Zrmbkz: \
2271 return X86::New##Zrmbkz;
2272
2273 switch (Opcode) {
2274 VPERM_CASES(VPERMI2B, VPERMT2B)
2275 VPERM_CASES_BROADCAST(VPERMI2D, VPERMT2D)
2276 VPERM_CASES_BROADCAST(VPERMI2PD, VPERMT2PD)
2277 VPERM_CASES_BROADCAST(VPERMI2PS, VPERMT2PS)
2278 VPERM_CASES_BROADCAST(VPERMI2Q, VPERMT2Q)
2279 VPERM_CASES(VPERMI2W, VPERMT2W)
2280 VPERM_CASES(VPERMT2B, VPERMI2B)
2281 VPERM_CASES_BROADCAST(VPERMT2D, VPERMI2D)
2282 VPERM_CASES_BROADCAST(VPERMT2PD, VPERMI2PD)
2283 VPERM_CASES_BROADCAST(VPERMT2PS, VPERMI2PS)
2284 VPERM_CASES_BROADCAST(VPERMT2Q, VPERMI2Q)
2285 VPERM_CASES(VPERMT2W, VPERMI2W)
2286 }
2287
2288 llvm_unreachable("Unreachable!");
2289#undef VPERM_CASES_BROADCAST
2290#undef VPERM_CASES
2291}
2292
2294 unsigned OpIdx1,
2295 unsigned OpIdx2) const {
2296 auto CloneIfNew = [&](MachineInstr &MI) {
2297 return std::exchange(NewMI, false)
2298 ? MI.getParent()->getParent()->CloneMachineInstr(&MI)
2299 : &MI;
2300 };
2301 MachineInstr *WorkingMI = nullptr;
2302 unsigned Opc = MI.getOpcode();
2303
2304#define CASE_ND(OP) \
2305 case X86::OP: \
2306 case X86::OP##_ND:
2307
2308 switch (Opc) {
2309 // SHLD B, C, I <-> SHRD C, B, (BitWidth - I)
2310 CASE_ND(SHRD16rri8)
2311 CASE_ND(SHLD16rri8)
2312 CASE_ND(SHRD32rri8)
2313 CASE_ND(SHLD32rri8)
2314 CASE_ND(SHRD64rri8)
2315 CASE_ND(SHLD64rri8) {
2316 unsigned Size;
2317 switch (Opc) {
2318 default:
2319 llvm_unreachable("Unreachable!");
2320#define FROM_TO_SIZE(A, B, S) \
2321 case X86::A: \
2322 Opc = X86::B; \
2323 Size = S; \
2324 break; \
2325 case X86::A##_ND: \
2326 Opc = X86::B##_ND; \
2327 Size = S; \
2328 break; \
2329 case X86::B: \
2330 Opc = X86::A; \
2331 Size = S; \
2332 break; \
2333 case X86::B##_ND: \
2334 Opc = X86::A##_ND; \
2335 Size = S; \
2336 break;
2337
2338 FROM_TO_SIZE(SHRD16rri8, SHLD16rri8, 16)
2339 FROM_TO_SIZE(SHRD32rri8, SHLD32rri8, 32)
2340 FROM_TO_SIZE(SHRD64rri8, SHLD64rri8, 64)
2341#undef FROM_TO_SIZE
2342 }
2343 WorkingMI = CloneIfNew(MI);
2344 WorkingMI->setDesc(get(Opc));
2345 WorkingMI->getOperand(3).setImm(Size - MI.getOperand(3).getImm());
2346 break;
2347 }
2348 case X86::PFSUBrr:
2349 case X86::PFSUBRrr:
2350 // PFSUB x, y: x = x - y
2351 // PFSUBR x, y: x = y - x
2352 WorkingMI = CloneIfNew(MI);
2353 WorkingMI->setDesc(
2354 get(X86::PFSUBRrr == Opc ? X86::PFSUBrr : X86::PFSUBRrr));
2355 break;
2356 case X86::BLENDPDrri:
2357 case X86::BLENDPSrri:
2358 case X86::PBLENDWrri:
2359 case X86::VBLENDPDrri:
2360 case X86::VBLENDPSrri:
2361 case X86::VBLENDPDYrri:
2362 case X86::VBLENDPSYrri:
2363 case X86::VPBLENDDrri:
2364 case X86::VPBLENDWrri:
2365 case X86::VPBLENDDYrri:
2366 case X86::VPBLENDWYrri: {
2367 int8_t Mask;
2368 switch (Opc) {
2369 default:
2370 llvm_unreachable("Unreachable!");
2371 case X86::BLENDPDrri:
2372 Mask = (int8_t)0x03;
2373 break;
2374 case X86::BLENDPSrri:
2375 Mask = (int8_t)0x0F;
2376 break;
2377 case X86::PBLENDWrri:
2378 Mask = (int8_t)0xFF;
2379 break;
2380 case X86::VBLENDPDrri:
2381 Mask = (int8_t)0x03;
2382 break;
2383 case X86::VBLENDPSrri:
2384 Mask = (int8_t)0x0F;
2385 break;
2386 case X86::VBLENDPDYrri:
2387 Mask = (int8_t)0x0F;
2388 break;
2389 case X86::VBLENDPSYrri:
2390 Mask = (int8_t)0xFF;
2391 break;
2392 case X86::VPBLENDDrri:
2393 Mask = (int8_t)0x0F;
2394 break;
2395 case X86::VPBLENDWrri:
2396 Mask = (int8_t)0xFF;
2397 break;
2398 case X86::VPBLENDDYrri:
2399 Mask = (int8_t)0xFF;
2400 break;
2401 case X86::VPBLENDWYrri:
2402 Mask = (int8_t)0xFF;
2403 break;
2404 }
2405 // Only the least significant bits of Imm are used.
2406 // Using int8_t to ensure it will be sign extended to the int64_t that
2407 // setImm takes in order to match isel behavior.
2408 int8_t Imm = MI.getOperand(3).getImm() & Mask;
2409 WorkingMI = CloneIfNew(MI);
2410 WorkingMI->getOperand(3).setImm(Mask ^ Imm);
2411 break;
2412 }
2413 case X86::INSERTPSrri:
2414 case X86::VINSERTPSrri:
2415 case X86::VINSERTPSZrri: {
2416 unsigned Imm = MI.getOperand(MI.getNumOperands() - 1).getImm();
2417 unsigned ZMask = Imm & 15;
2418 unsigned DstIdx = (Imm >> 4) & 3;
2419 unsigned SrcIdx = (Imm >> 6) & 3;
2420
2421 // We can commute insertps if we zero 2 of the elements, the insertion is
2422 // "inline" and we don't override the insertion with a zero.
2423 if (DstIdx == SrcIdx && (ZMask & (1 << DstIdx)) == 0 &&
2424 llvm::popcount(ZMask) == 2) {
2425 unsigned AltIdx = llvm::countr_zero((ZMask | (1 << DstIdx)) ^ 15);
2426 assert(AltIdx < 4 && "Illegal insertion index");
2427 unsigned AltImm = (AltIdx << 6) | (AltIdx << 4) | ZMask;
2428 WorkingMI = CloneIfNew(MI);
2429 WorkingMI->getOperand(MI.getNumOperands() - 1).setImm(AltImm);
2430 break;
2431 }
2432 return nullptr;
2433 }
2434 case X86::MOVSDrr:
2435 case X86::MOVSSrr:
2436 case X86::VMOVSDrr:
2437 case X86::VMOVSSrr: {
2438 // On SSE41 or later we can commute a MOVSS/MOVSD to a BLENDPS/BLENDPD.
2439 if (Subtarget.hasSSE41()) {
2440 unsigned Mask;
2441 switch (Opc) {
2442 default:
2443 llvm_unreachable("Unreachable!");
2444 case X86::MOVSDrr:
2445 Opc = X86::BLENDPDrri;
2446 Mask = 0x02;
2447 break;
2448 case X86::MOVSSrr:
2449 Opc = X86::BLENDPSrri;
2450 Mask = 0x0E;
2451 break;
2452 case X86::VMOVSDrr:
2453 Opc = X86::VBLENDPDrri;
2454 Mask = 0x02;
2455 break;
2456 case X86::VMOVSSrr:
2457 Opc = X86::VBLENDPSrri;
2458 Mask = 0x0E;
2459 break;
2460 }
2461
2462 WorkingMI = CloneIfNew(MI);
2463 WorkingMI->setDesc(get(Opc));
2464 WorkingMI->addOperand(MachineOperand::CreateImm(Mask));
2465 break;
2466 }
2467
2468 assert(Opc == X86::MOVSDrr && "Only MOVSD can commute to SHUFPD");
2469 WorkingMI = CloneIfNew(MI);
2470 WorkingMI->setDesc(get(X86::SHUFPDrri));
2471 WorkingMI->addOperand(MachineOperand::CreateImm(0x02));
2472 break;
2473 }
2474 case X86::SHUFPDrri: {
2475 // Commute to MOVSD.
2476 assert(MI.getOperand(3).getImm() == 0x02 && "Unexpected immediate!");
2477 WorkingMI = CloneIfNew(MI);
2478 WorkingMI->setDesc(get(X86::MOVSDrr));
2479 WorkingMI->removeOperand(3);
2480 break;
2481 }
2482 case X86::PCLMULQDQrri:
2483 case X86::VPCLMULQDQrri:
2484 case X86::VPCLMULQDQYrri:
2485 case X86::VPCLMULQDQZrri:
2486 case X86::VPCLMULQDQZ128rri:
2487 case X86::VPCLMULQDQZ256rri: {
2488 // SRC1 64bits = Imm[0] ? SRC1[127:64] : SRC1[63:0]
2489 // SRC2 64bits = Imm[4] ? SRC2[127:64] : SRC2[63:0]
2490 unsigned Imm = MI.getOperand(3).getImm();
2491 unsigned Src1Hi = Imm & 0x01;
2492 unsigned Src2Hi = Imm & 0x10;
2493 WorkingMI = CloneIfNew(MI);
2494 WorkingMI->getOperand(3).setImm((Src1Hi << 4) | (Src2Hi >> 4));
2495 break;
2496 }
2497 case X86::VPCMPBZ128rri:
2498 case X86::VPCMPUBZ128rri:
2499 case X86::VPCMPBZ256rri:
2500 case X86::VPCMPUBZ256rri:
2501 case X86::VPCMPBZrri:
2502 case X86::VPCMPUBZrri:
2503 case X86::VPCMPDZ128rri:
2504 case X86::VPCMPUDZ128rri:
2505 case X86::VPCMPDZ256rri:
2506 case X86::VPCMPUDZ256rri:
2507 case X86::VPCMPDZrri:
2508 case X86::VPCMPUDZrri:
2509 case X86::VPCMPQZ128rri:
2510 case X86::VPCMPUQZ128rri:
2511 case X86::VPCMPQZ256rri:
2512 case X86::VPCMPUQZ256rri:
2513 case X86::VPCMPQZrri:
2514 case X86::VPCMPUQZrri:
2515 case X86::VPCMPWZ128rri:
2516 case X86::VPCMPUWZ128rri:
2517 case X86::VPCMPWZ256rri:
2518 case X86::VPCMPUWZ256rri:
2519 case X86::VPCMPWZrri:
2520 case X86::VPCMPUWZrri:
2521 case X86::VPCMPBZ128rrik:
2522 case X86::VPCMPUBZ128rrik:
2523 case X86::VPCMPBZ256rrik:
2524 case X86::VPCMPUBZ256rrik:
2525 case X86::VPCMPBZrrik:
2526 case X86::VPCMPUBZrrik:
2527 case X86::VPCMPDZ128rrik:
2528 case X86::VPCMPUDZ128rrik:
2529 case X86::VPCMPDZ256rrik:
2530 case X86::VPCMPUDZ256rrik:
2531 case X86::VPCMPDZrrik:
2532 case X86::VPCMPUDZrrik:
2533 case X86::VPCMPQZ128rrik:
2534 case X86::VPCMPUQZ128rrik:
2535 case X86::VPCMPQZ256rrik:
2536 case X86::VPCMPUQZ256rrik:
2537 case X86::VPCMPQZrrik:
2538 case X86::VPCMPUQZrrik:
2539 case X86::VPCMPWZ128rrik:
2540 case X86::VPCMPUWZ128rrik:
2541 case X86::VPCMPWZ256rrik:
2542 case X86::VPCMPUWZ256rrik:
2543 case X86::VPCMPWZrrik:
2544 case X86::VPCMPUWZrrik:
2545 WorkingMI = CloneIfNew(MI);
2546 // Flip comparison mode immediate (if necessary).
2547 WorkingMI->getOperand(MI.getNumOperands() - 1)
2549 MI.getOperand(MI.getNumOperands() - 1).getImm() & 0x7));
2550 break;
2551 case X86::VPCOMBri:
2552 case X86::VPCOMUBri:
2553 case X86::VPCOMDri:
2554 case X86::VPCOMUDri:
2555 case X86::VPCOMQri:
2556 case X86::VPCOMUQri:
2557 case X86::VPCOMWri:
2558 case X86::VPCOMUWri:
2559 WorkingMI = CloneIfNew(MI);
2560 // Flip comparison mode immediate (if necessary).
2561 WorkingMI->getOperand(3).setImm(
2562 X86::getSwappedVPCOMImm(MI.getOperand(3).getImm() & 0x7));
2563 break;
2564 case X86::VCMPSDZrri:
2565 case X86::VCMPSSZrri:
2566 case X86::VCMPPDZrri:
2567 case X86::VCMPPSZrri:
2568 case X86::VCMPSHZrri:
2569 case X86::VCMPPHZrri:
2570 case X86::VCMPPHZ128rri:
2571 case X86::VCMPPHZ256rri:
2572 case X86::VCMPPDZ128rri:
2573 case X86::VCMPPSZ128rri:
2574 case X86::VCMPPDZ256rri:
2575 case X86::VCMPPSZ256rri:
2576 case X86::VCMPPDZrrik:
2577 case X86::VCMPPSZrrik:
2578 case X86::VCMPPHZrrik:
2579 case X86::VCMPPDZ128rrik:
2580 case X86::VCMPPSZ128rrik:
2581 case X86::VCMPPHZ128rrik:
2582 case X86::VCMPPDZ256rrik:
2583 case X86::VCMPPSZ256rrik:
2584 case X86::VCMPPHZ256rrik:
2585 WorkingMI = CloneIfNew(MI);
2586 WorkingMI->getOperand(MI.getNumExplicitOperands() - 1)
2588 MI.getOperand(MI.getNumExplicitOperands() - 1).getImm() & 0x1f));
2589 break;
2590 case X86::VPERM2F128rri:
2591 case X86::VPERM2I128rri:
2592 // Flip permute source immediate.
2593 // Imm & 0x02: lo = if set, select Op1.lo/hi else Op0.lo/hi.
2594 // Imm & 0x20: hi = if set, select Op1.lo/hi else Op0.lo/hi.
2595 WorkingMI = CloneIfNew(MI);
2596 WorkingMI->getOperand(3).setImm((MI.getOperand(3).getImm() & 0xFF) ^ 0x22);
2597 break;
2598 case X86::MOVHLPSrr:
2599 case X86::UNPCKHPDrr:
2600 case X86::VMOVHLPSrr:
2601 case X86::VUNPCKHPDrr:
2602 case X86::VMOVHLPSZrr:
2603 case X86::VUNPCKHPDZ128rr:
2604 assert(Subtarget.hasSSE2() && "Commuting MOVHLP/UNPCKHPD requires SSE2!");
2605
2606 switch (Opc) {
2607 default:
2608 llvm_unreachable("Unreachable!");
2609 case X86::MOVHLPSrr:
2610 Opc = X86::UNPCKHPDrr;
2611 break;
2612 case X86::UNPCKHPDrr:
2613 Opc = X86::MOVHLPSrr;
2614 break;
2615 case X86::VMOVHLPSrr:
2616 Opc = X86::VUNPCKHPDrr;
2617 break;
2618 case X86::VUNPCKHPDrr:
2619 Opc = X86::VMOVHLPSrr;
2620 break;
2621 case X86::VMOVHLPSZrr:
2622 Opc = X86::VUNPCKHPDZ128rr;
2623 break;
2624 case X86::VUNPCKHPDZ128rr:
2625 Opc = X86::VMOVHLPSZrr;
2626 break;
2627 }
2628 WorkingMI = CloneIfNew(MI);
2629 WorkingMI->setDesc(get(Opc));
2630 break;
2631 CASE_ND(CMOV16rr)
2632 CASE_ND(CMOV32rr)
2633 CASE_ND(CMOV64rr) {
2634 WorkingMI = CloneIfNew(MI);
2635 unsigned OpNo = MI.getDesc().getNumOperands() - 1;
2636 X86::CondCode CC = static_cast<X86::CondCode>(MI.getOperand(OpNo).getImm());
2638 break;
2639 }
2640 case X86::VPTERNLOGDZrri:
2641 case X86::VPTERNLOGDZrmi:
2642 case X86::VPTERNLOGDZ128rri:
2643 case X86::VPTERNLOGDZ128rmi:
2644 case X86::VPTERNLOGDZ256rri:
2645 case X86::VPTERNLOGDZ256rmi:
2646 case X86::VPTERNLOGQZrri:
2647 case X86::VPTERNLOGQZrmi:
2648 case X86::VPTERNLOGQZ128rri:
2649 case X86::VPTERNLOGQZ128rmi:
2650 case X86::VPTERNLOGQZ256rri:
2651 case X86::VPTERNLOGQZ256rmi:
2652 case X86::VPTERNLOGDZrrik:
2653 case X86::VPTERNLOGDZ128rrik:
2654 case X86::VPTERNLOGDZ256rrik:
2655 case X86::VPTERNLOGQZrrik:
2656 case X86::VPTERNLOGQZ128rrik:
2657 case X86::VPTERNLOGQZ256rrik:
2658 case X86::VPTERNLOGDZrrikz:
2659 case X86::VPTERNLOGDZrmikz:
2660 case X86::VPTERNLOGDZ128rrikz:
2661 case X86::VPTERNLOGDZ128rmikz:
2662 case X86::VPTERNLOGDZ256rrikz:
2663 case X86::VPTERNLOGDZ256rmikz:
2664 case X86::VPTERNLOGQZrrikz:
2665 case X86::VPTERNLOGQZrmikz:
2666 case X86::VPTERNLOGQZ128rrikz:
2667 case X86::VPTERNLOGQZ128rmikz:
2668 case X86::VPTERNLOGQZ256rrikz:
2669 case X86::VPTERNLOGQZ256rmikz:
2670 case X86::VPTERNLOGDZ128rmbi:
2671 case X86::VPTERNLOGDZ256rmbi:
2672 case X86::VPTERNLOGDZrmbi:
2673 case X86::VPTERNLOGQZ128rmbi:
2674 case X86::VPTERNLOGQZ256rmbi:
2675 case X86::VPTERNLOGQZrmbi:
2676 case X86::VPTERNLOGDZ128rmbikz:
2677 case X86::VPTERNLOGDZ256rmbikz:
2678 case X86::VPTERNLOGDZrmbikz:
2679 case X86::VPTERNLOGQZ128rmbikz:
2680 case X86::VPTERNLOGQZ256rmbikz:
2681 case X86::VPTERNLOGQZrmbikz: {
2682 WorkingMI = CloneIfNew(MI);
2683 commuteVPTERNLOG(*WorkingMI, OpIdx1, OpIdx2);
2684 break;
2685 }
2686 default:
2688 WorkingMI = CloneIfNew(MI);
2690 break;
2691 }
2692
2693 if (auto *FMA3Group = getFMA3Group(Opc, MI.getDesc().TSFlags)) {
2694 WorkingMI = CloneIfNew(MI);
2695 WorkingMI->setDesc(
2696 get(getFMA3OpcodeToCommuteOperands(MI, OpIdx1, OpIdx2, *FMA3Group)));
2697 break;
2698 }
2699 }
2700 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
2701}
2702
2703bool X86InstrInfo::findThreeSrcCommutedOpIndices(const MachineInstr &MI,
2704 unsigned &SrcOpIdx1,
2705 unsigned &SrcOpIdx2,
2706 bool IsIntrinsic) const {
2707 uint64_t TSFlags = MI.getDesc().TSFlags;
2708
2709 unsigned FirstCommutableVecOp = 1;
2710 unsigned LastCommutableVecOp = 3;
2711 unsigned KMaskOp = -1U;
2712 if (X86II::isKMasked(TSFlags)) {
2713 // For k-zero-masked operations it is Ok to commute the first vector
2714 // operand. Unless this is an intrinsic instruction.
2715 // For regular k-masked operations a conservative choice is done as the
2716 // elements of the first vector operand, for which the corresponding bit
2717 // in the k-mask operand is set to 0, are copied to the result of the
2718 // instruction.
2719 // TODO/FIXME: The commute still may be legal if it is known that the
2720 // k-mask operand is set to either all ones or all zeroes.
2721 // It is also Ok to commute the 1st operand if all users of MI use only
2722 // the elements enabled by the k-mask operand. For example,
2723 // v4 = VFMADD213PSZrk v1, k, v2, v3; // v1[i] = k[i] ? v2[i]*v1[i]+v3[i]
2724 // : v1[i];
2725 // VMOVAPSZmrk <mem_addr>, k, v4; // this is the ONLY user of v4 ->
2726 // // Ok, to commute v1 in FMADD213PSZrk.
2727
2728 // The k-mask operand has index = 2 for masked and zero-masked operations.
2729 KMaskOp = 2;
2730
2731 // The operand with index = 1 is used as a source for those elements for
2732 // which the corresponding bit in the k-mask is set to 0.
2733 if (X86II::isKMergeMasked(TSFlags) || IsIntrinsic)
2734 FirstCommutableVecOp = 3;
2735
2736 LastCommutableVecOp++;
2737 } else if (IsIntrinsic) {
2738 // Commuting the first operand of an intrinsic instruction isn't possible
2739 // unless we can prove that only the lowest element of the result is used.
2740 FirstCommutableVecOp = 2;
2741 }
2742
2743 if (isMem(MI, LastCommutableVecOp))
2744 LastCommutableVecOp--;
2745
2746 // Only the first RegOpsNum operands are commutable.
2747 // Also, the value 'CommuteAnyOperandIndex' is valid here as it means
2748 // that the operand is not specified/fixed.
2749 if (SrcOpIdx1 != CommuteAnyOperandIndex &&
2750 (SrcOpIdx1 < FirstCommutableVecOp || SrcOpIdx1 > LastCommutableVecOp ||
2751 SrcOpIdx1 == KMaskOp))
2752 return false;
2753 if (SrcOpIdx2 != CommuteAnyOperandIndex &&
2754 (SrcOpIdx2 < FirstCommutableVecOp || SrcOpIdx2 > LastCommutableVecOp ||
2755 SrcOpIdx2 == KMaskOp))
2756 return false;
2757
2758 // Look for two different register operands assumed to be commutable
2759 // regardless of the FMA opcode. The FMA opcode is adjusted later.
2760 if (SrcOpIdx1 == CommuteAnyOperandIndex ||
2761 SrcOpIdx2 == CommuteAnyOperandIndex) {
2762 unsigned CommutableOpIdx2 = SrcOpIdx2;
2763
2764 // At least one of operands to be commuted is not specified and
2765 // this method is free to choose appropriate commutable operands.
2766 if (SrcOpIdx1 == SrcOpIdx2)
2767 // Both of operands are not fixed. By default set one of commutable
2768 // operands to the last register operand of the instruction.
2769 CommutableOpIdx2 = LastCommutableVecOp;
2770 else if (SrcOpIdx2 == CommuteAnyOperandIndex)
2771 // Only one of operands is not fixed.
2772 CommutableOpIdx2 = SrcOpIdx1;
2773
2774 // CommutableOpIdx2 is well defined now. Let's choose another commutable
2775 // operand and assign its index to CommutableOpIdx1.
2776 Register Op2Reg = MI.getOperand(CommutableOpIdx2).getReg();
2777
2778 unsigned CommutableOpIdx1;
2779 for (CommutableOpIdx1 = LastCommutableVecOp;
2780 CommutableOpIdx1 >= FirstCommutableVecOp; CommutableOpIdx1--) {
2781 // Just ignore and skip the k-mask operand.
2782 if (CommutableOpIdx1 == KMaskOp)
2783 continue;
2784
2785 // The commuted operands must have different registers.
2786 // Otherwise, the commute transformation does not change anything and
2787 // is useless then.
2788 if (Op2Reg != MI.getOperand(CommutableOpIdx1).getReg())
2789 break;
2790 }
2791
2792 // No appropriate commutable operands were found.
2793 if (CommutableOpIdx1 < FirstCommutableVecOp)
2794 return false;
2795
2796 // Assign the found pair of commutable indices to SrcOpIdx1 and SrcOpidx2
2797 // to return those values.
2798 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1,
2799 CommutableOpIdx2))
2800 return false;
2801 }
2802
2803 return true;
2804}
2805
2807 unsigned &SrcOpIdx1,
2808 unsigned &SrcOpIdx2) const {
2809 const MCInstrDesc &Desc = MI.getDesc();
2810 if (!Desc.isCommutable())
2811 return false;
2812
2813 switch (MI.getOpcode()) {
2814 case X86::CMPSDrri:
2815 case X86::CMPSSrri:
2816 case X86::CMPPDrri:
2817 case X86::CMPPSrri:
2818 case X86::VCMPSDrri:
2819 case X86::VCMPSSrri:
2820 case X86::VCMPPDrri:
2821 case X86::VCMPPSrri:
2822 case X86::VCMPPDYrri:
2823 case X86::VCMPPSYrri:
2824 case X86::VCMPSDZrri:
2825 case X86::VCMPSSZrri:
2826 case X86::VCMPPDZrri:
2827 case X86::VCMPPSZrri:
2828 case X86::VCMPSHZrri:
2829 case X86::VCMPPHZrri:
2830 case X86::VCMPPHZ128rri:
2831 case X86::VCMPPHZ256rri:
2832 case X86::VCMPPDZ128rri:
2833 case X86::VCMPPSZ128rri:
2834 case X86::VCMPPDZ256rri:
2835 case X86::VCMPPSZ256rri:
2836 case X86::VCMPPDZrrik:
2837 case X86::VCMPPSZrrik:
2838 case X86::VCMPPHZrrik:
2839 case X86::VCMPPDZ128rrik:
2840 case X86::VCMPPSZ128rrik:
2841 case X86::VCMPPHZ128rrik:
2842 case X86::VCMPPDZ256rrik:
2843 case X86::VCMPPSZ256rrik:
2844 case X86::VCMPPHZ256rrik: {
2845 unsigned OpOffset = X86II::isKMasked(Desc.TSFlags) ? 1 : 0;
2846
2847 // Float comparison can be safely commuted for
2848 // Ordered/Unordered/Equal/NotEqual tests
2849 unsigned Imm = MI.getOperand(3 + OpOffset).getImm() & 0x7;
2850 switch (Imm) {
2851 default:
2852 // EVEX versions can be commuted.
2853 if ((Desc.TSFlags & X86II::EncodingMask) == X86II::EVEX)
2854 break;
2855 return false;
2856 case 0x00: // EQUAL
2857 case 0x03: // UNORDERED
2858 case 0x04: // NOT EQUAL
2859 case 0x07: // ORDERED
2860 break;
2861 }
2862
2863 // The indices of the commutable operands are 1 and 2 (or 2 and 3
2864 // when masked).
2865 // Assign them to the returned operand indices here.
2866 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 1 + OpOffset,
2867 2 + OpOffset);
2868 }
2869 case X86::MOVSSrr:
2870 // X86::MOVSDrr is always commutable. MOVSS is only commutable if we can
2871 // form sse4.1 blend. We assume VMOVSSrr/VMOVSDrr is always commutable since
2872 // AVX implies sse4.1.
2873 if (Subtarget.hasSSE41())
2874 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
2875 return false;
2876 case X86::SHUFPDrri:
2877 // We can commute this to MOVSD.
2878 if (MI.getOperand(3).getImm() == 0x02)
2879 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
2880 return false;
2881 case X86::MOVHLPSrr:
2882 case X86::UNPCKHPDrr:
2883 case X86::VMOVHLPSrr:
2884 case X86::VUNPCKHPDrr:
2885 case X86::VMOVHLPSZrr:
2886 case X86::VUNPCKHPDZ128rr:
2887 if (Subtarget.hasSSE2())
2888 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
2889 return false;
2890 case X86::VPTERNLOGDZrri:
2891 case X86::VPTERNLOGDZrmi:
2892 case X86::VPTERNLOGDZ128rri:
2893 case X86::VPTERNLOGDZ128rmi:
2894 case X86::VPTERNLOGDZ256rri:
2895 case X86::VPTERNLOGDZ256rmi:
2896 case X86::VPTERNLOGQZrri:
2897 case X86::VPTERNLOGQZrmi:
2898 case X86::VPTERNLOGQZ128rri:
2899 case X86::VPTERNLOGQZ128rmi:
2900 case X86::VPTERNLOGQZ256rri:
2901 case X86::VPTERNLOGQZ256rmi:
2902 case X86::VPTERNLOGDZrrik:
2903 case X86::VPTERNLOGDZ128rrik:
2904 case X86::VPTERNLOGDZ256rrik:
2905 case X86::VPTERNLOGQZrrik:
2906 case X86::VPTERNLOGQZ128rrik:
2907 case X86::VPTERNLOGQZ256rrik:
2908 case X86::VPTERNLOGDZrrikz:
2909 case X86::VPTERNLOGDZrmikz:
2910 case X86::VPTERNLOGDZ128rrikz:
2911 case X86::VPTERNLOGDZ128rmikz:
2912 case X86::VPTERNLOGDZ256rrikz:
2913 case X86::VPTERNLOGDZ256rmikz:
2914 case X86::VPTERNLOGQZrrikz:
2915 case X86::VPTERNLOGQZrmikz:
2916 case X86::VPTERNLOGQZ128rrikz:
2917 case X86::VPTERNLOGQZ128rmikz:
2918 case X86::VPTERNLOGQZ256rrikz:
2919 case X86::VPTERNLOGQZ256rmikz:
2920 case X86::VPTERNLOGDZ128rmbi:
2921 case X86::VPTERNLOGDZ256rmbi:
2922 case X86::VPTERNLOGDZrmbi:
2923 case X86::VPTERNLOGQZ128rmbi:
2924 case X86::VPTERNLOGQZ256rmbi:
2925 case X86::VPTERNLOGQZrmbi:
2926 case X86::VPTERNLOGDZ128rmbikz:
2927 case X86::VPTERNLOGDZ256rmbikz:
2928 case X86::VPTERNLOGDZrmbikz:
2929 case X86::VPTERNLOGQZ128rmbikz:
2930 case X86::VPTERNLOGQZ256rmbikz:
2931 case X86::VPTERNLOGQZrmbikz:
2932 return findThreeSrcCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
2933 case X86::VPDPWSSDYrr:
2934 case X86::VPDPWSSDrr:
2935 case X86::VPDPWSSDSYrr:
2936 case X86::VPDPWSSDSrr:
2937 case X86::VPDPWUUDrr:
2938 case X86::VPDPWUUDYrr:
2939 case X86::VPDPWUUDSrr:
2940 case X86::VPDPWUUDSYrr:
2941 case X86::VPDPBSSDSrr:
2942 case X86::VPDPBSSDSYrr:
2943 case X86::VPDPBSSDrr:
2944 case X86::VPDPBSSDYrr:
2945 case X86::VPDPBUUDSrr:
2946 case X86::VPDPBUUDSYrr:
2947 case X86::VPDPBUUDrr:
2948 case X86::VPDPBUUDYrr:
2949 case X86::VPDPBSSDSZ128rr:
2950 case X86::VPDPBSSDSZ128rrk:
2951 case X86::VPDPBSSDSZ128rrkz:
2952 case X86::VPDPBSSDSZ256rr:
2953 case X86::VPDPBSSDSZ256rrk:
2954 case X86::VPDPBSSDSZ256rrkz:
2955 case X86::VPDPBSSDSZrr:
2956 case X86::VPDPBSSDSZrrk:
2957 case X86::VPDPBSSDSZrrkz:
2958 case X86::VPDPBSSDZ128rr:
2959 case X86::VPDPBSSDZ128rrk:
2960 case X86::VPDPBSSDZ128rrkz:
2961 case X86::VPDPBSSDZ256rr:
2962 case X86::VPDPBSSDZ256rrk:
2963 case X86::VPDPBSSDZ256rrkz:
2964 case X86::VPDPBSSDZrr:
2965 case X86::VPDPBSSDZrrk:
2966 case X86::VPDPBSSDZrrkz:
2967 case X86::VPDPBUUDSZ128rr:
2968 case X86::VPDPBUUDSZ128rrk:
2969 case X86::VPDPBUUDSZ128rrkz:
2970 case X86::VPDPBUUDSZ256rr:
2971 case X86::VPDPBUUDSZ256rrk:
2972 case X86::VPDPBUUDSZ256rrkz:
2973 case X86::VPDPBUUDSZrr:
2974 case X86::VPDPBUUDSZrrk:
2975 case X86::VPDPBUUDSZrrkz:
2976 case X86::VPDPBUUDZ128rr:
2977 case X86::VPDPBUUDZ128rrk:
2978 case X86::VPDPBUUDZ128rrkz:
2979 case X86::VPDPBUUDZ256rr:
2980 case X86::VPDPBUUDZ256rrk:
2981 case X86::VPDPBUUDZ256rrkz:
2982 case X86::VPDPBUUDZrr:
2983 case X86::VPDPBUUDZrrk:
2984 case X86::VPDPBUUDZrrkz:
2985 case X86::VPDPWSSDZ128rr:
2986 case X86::VPDPWSSDZ128rrk:
2987 case X86::VPDPWSSDZ128rrkz:
2988 case X86::VPDPWSSDZ256rr:
2989 case X86::VPDPWSSDZ256rrk:
2990 case X86::VPDPWSSDZ256rrkz:
2991 case X86::VPDPWSSDZrr:
2992 case X86::VPDPWSSDZrrk:
2993 case X86::VPDPWSSDZrrkz:
2994 case X86::VPDPWSSDSZ128rr:
2995 case X86::VPDPWSSDSZ128rrk:
2996 case X86::VPDPWSSDSZ128rrkz:
2997 case X86::VPDPWSSDSZ256rr:
2998 case X86::VPDPWSSDSZ256rrk:
2999 case X86::VPDPWSSDSZ256rrkz:
3000 case X86::VPDPWSSDSZrr:
3001 case X86::VPDPWSSDSZrrk:
3002 case X86::VPDPWSSDSZrrkz:
3003 case X86::VPDPWUUDZ128rr:
3004 case X86::VPDPWUUDZ128rrk:
3005 case X86::VPDPWUUDZ128rrkz:
3006 case X86::VPDPWUUDZ256rr:
3007 case X86::VPDPWUUDZ256rrk:
3008 case X86::VPDPWUUDZ256rrkz:
3009 case X86::VPDPWUUDZrr:
3010 case X86::VPDPWUUDZrrk:
3011 case X86::VPDPWUUDZrrkz:
3012 case X86::VPDPWUUDSZ128rr:
3013 case X86::VPDPWUUDSZ128rrk:
3014 case X86::VPDPWUUDSZ128rrkz:
3015 case X86::VPDPWUUDSZ256rr:
3016 case X86::VPDPWUUDSZ256rrk:
3017 case X86::VPDPWUUDSZ256rrkz:
3018 case X86::VPDPWUUDSZrr:
3019 case X86::VPDPWUUDSZrrk:
3020 case X86::VPDPWUUDSZrrkz:
3021 case X86::VPMADD52HUQrr:
3022 case X86::VPMADD52HUQYrr:
3023 case X86::VPMADD52HUQZ128r:
3024 case X86::VPMADD52HUQZ128rk:
3025 case X86::VPMADD52HUQZ128rkz:
3026 case X86::VPMADD52HUQZ256r:
3027 case X86::VPMADD52HUQZ256rk:
3028 case X86::VPMADD52HUQZ256rkz:
3029 case X86::VPMADD52HUQZr:
3030 case X86::VPMADD52HUQZrk:
3031 case X86::VPMADD52HUQZrkz:
3032 case X86::VPMADD52LUQrr:
3033 case X86::VPMADD52LUQYrr:
3034 case X86::VPMADD52LUQZ128r:
3035 case X86::VPMADD52LUQZ128rk:
3036 case X86::VPMADD52LUQZ128rkz:
3037 case X86::VPMADD52LUQZ256r:
3038 case X86::VPMADD52LUQZ256rk:
3039 case X86::VPMADD52LUQZ256rkz:
3040 case X86::VPMADD52LUQZr:
3041 case X86::VPMADD52LUQZrk:
3042 case X86::VPMADD52LUQZrkz:
3043 case X86::VFMADDCPHZr:
3044 case X86::VFMADDCPHZrk:
3045 case X86::VFMADDCPHZrkz:
3046 case X86::VFMADDCPHZ128r:
3047 case X86::VFMADDCPHZ128rk:
3048 case X86::VFMADDCPHZ128rkz:
3049 case X86::VFMADDCPHZ256r:
3050 case X86::VFMADDCPHZ256rk:
3051 case X86::VFMADDCPHZ256rkz:
3052 case X86::VFMADDCSHZr:
3053 case X86::VFMADDCSHZrk:
3054 case X86::VFMADDCSHZrkz: {
3055 unsigned CommutableOpIdx1 = 2;
3056 unsigned CommutableOpIdx2 = 3;
3057 if (X86II::isKMasked(Desc.TSFlags)) {
3058 // Skip the mask register.
3059 ++CommutableOpIdx1;
3060 ++CommutableOpIdx2;
3061 }
3062 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1,
3063 CommutableOpIdx2))
3064 return false;
3065 if (!MI.getOperand(SrcOpIdx1).isReg() || !MI.getOperand(SrcOpIdx2).isReg())
3066 // No idea.
3067 return false;
3068 return true;
3069 }
3070
3071 default:
3072 const X86InstrFMA3Group *FMA3Group =
3073 getFMA3Group(MI.getOpcode(), MI.getDesc().TSFlags);
3074 if (FMA3Group)
3075 return findThreeSrcCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2,
3076 FMA3Group->isIntrinsic());
3077
3078 // Handled masked instructions since we need to skip over the mask input
3079 // and the preserved input.
3080 if (X86II::isKMasked(Desc.TSFlags)) {
3081 // First assume that the first input is the mask operand and skip past it.
3082 unsigned CommutableOpIdx1 = Desc.getNumDefs() + 1;
3083 unsigned CommutableOpIdx2 = Desc.getNumDefs() + 2;
3084 // Check if the first input is tied. If there isn't one then we only
3085 // need to skip the mask operand which we did above.
3086 if ((MI.getDesc().getOperandConstraint(Desc.getNumDefs(),
3087 MCOI::TIED_TO) != -1)) {
3088 // If this is zero masking instruction with a tied operand, we need to
3089 // move the first index back to the first input since this must
3090 // be a 3 input instruction and we want the first two non-mask inputs.
3091 // Otherwise this is a 2 input instruction with a preserved input and
3092 // mask, so we need to move the indices to skip one more input.
3093 if (X86II::isKMergeMasked(Desc.TSFlags)) {
3094 ++CommutableOpIdx1;
3095 ++CommutableOpIdx2;
3096 } else {
3097 --CommutableOpIdx1;
3098 }
3099 }
3100
3101 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1,
3102 CommutableOpIdx2))
3103 return false;
3104
3105 if (!MI.getOperand(SrcOpIdx1).isReg() ||
3106 !MI.getOperand(SrcOpIdx2).isReg())
3107 // No idea.
3108 return false;
3109 return true;
3110 }
3111
3112 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
3113 }
3114 return false;
3115}
3116
3118 unsigned Opcode = MI->getOpcode();
3119 if (Opcode != X86::LEA32r && Opcode != X86::LEA64r &&
3120 Opcode != X86::LEA64_32r)
3121 return false;
3122
3123 const MachineOperand &Scale = MI->getOperand(1 + X86::AddrScaleAmt);
3124 const MachineOperand &Disp = MI->getOperand(1 + X86::AddrDisp);
3125 const MachineOperand &Segment = MI->getOperand(1 + X86::AddrSegmentReg);
3126
3127 if (Segment.getReg() != 0 || !Disp.isImm() || Disp.getImm() != 0 ||
3128 Scale.getImm() > 1)
3129 return false;
3130
3131 return true;
3132}
3133
3135 // Currently we're interested in following sequence only.
3136 // r3 = lea r1, r2
3137 // r5 = add r3, r4
3138 // Both r3 and r4 are killed in add, we hope the add instruction has the
3139 // operand order
3140 // r5 = add r4, r3
3141 // So later in X86FixupLEAs the lea instruction can be rewritten as add.
3142 unsigned Opcode = MI.getOpcode();
3143 if (Opcode != X86::ADD32rr && Opcode != X86::ADD64rr)
3144 return false;
3145
3146 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
3147 Register Reg1 = MI.getOperand(1).getReg();
3148 Register Reg2 = MI.getOperand(2).getReg();
3149
3150 // Check if Reg1 comes from LEA in the same MBB.
3151 if (MachineInstr *Inst = MRI.getUniqueVRegDef(Reg1)) {
3152 if (isConvertibleLEA(Inst) && Inst->getParent() == MI.getParent()) {
3153 Commute = true;
3154 return true;
3155 }
3156 }
3157
3158 // Check if Reg2 comes from LEA in the same MBB.
3159 if (MachineInstr *Inst = MRI.getUniqueVRegDef(Reg2)) {
3160 if (isConvertibleLEA(Inst) && Inst->getParent() == MI.getParent()) {
3161 Commute = false;
3162 return true;
3163 }
3164 }
3165
3166 return false;
3167}
3168
3170 unsigned Opcode = MCID.getOpcode();
3171 if (!(X86::isJCC(Opcode) || X86::isSETCC(Opcode) || X86::isSETZUCC(Opcode) ||
3172 X86::isCMOVCC(Opcode) || X86::isCFCMOVCC(Opcode) ||
3173 X86::isCCMPCC(Opcode) || X86::isCTESTCC(Opcode)))
3174 return -1;
3175 // Assume that condition code is always the last use operand.
3176 unsigned NumUses = MCID.getNumOperands() - MCID.getNumDefs();
3177 return NumUses - 1;
3178}
3179
3181 const MCInstrDesc &MCID = MI.getDesc();
3182 int CondNo = getCondSrcNoFromDesc(MCID);
3183 if (CondNo < 0)
3184 return X86::COND_INVALID;
3185 CondNo += MCID.getNumDefs();
3186 return static_cast<X86::CondCode>(MI.getOperand(CondNo).getImm());
3187}
3188
3190 return X86::isJCC(MI.getOpcode()) ? X86::getCondFromMI(MI)
3192}
3193
3195 return X86::isSETCC(MI.getOpcode()) || X86::isSETZUCC(MI.getOpcode())
3198}
3199
3201 return X86::isCMOVCC(MI.getOpcode()) ? X86::getCondFromMI(MI)
3203}
3204
3206 return X86::isCFCMOVCC(MI.getOpcode()) ? X86::getCondFromMI(MI)
3208}
3209
3211 return X86::isCCMPCC(MI.getOpcode()) || X86::isCTESTCC(MI.getOpcode())
3214}
3215
3217 // CCMP/CTEST has two conditional operands:
3218 // - SCC: source conditonal code (same as CMOV)
3219 // - DCF: destination conditional flags, which has 4 valid bits
3220 //
3221 // +----+----+----+----+
3222 // | OF | SF | ZF | CF |
3223 // +----+----+----+----+
3224 //
3225 // If SCC(source conditional code) evaluates to false, CCMP/CTEST will updates
3226 // the conditional flags by as follows:
3227 //
3228 // OF = DCF.OF
3229 // SF = DCF.SF
3230 // ZF = DCF.ZF
3231 // CF = DCF.CF
3232 // PF = DCF.CF
3233 // AF = 0 (Auxiliary Carry Flag)
3234 //
3235 // Otherwise, the CMP or TEST is executed and it updates the
3236 // CSPAZO flags normally.
3237 //
3238 // NOTE:
3239 // If SCC = P, then SCC evaluates to true regardless of the CSPAZO value.
3240 // If SCC = NP, then SCC evaluates to false regardless of the CSPAZO value.
3241
3242 enum { CF = 1, ZF = 2, SF = 4, OF = 8, PF = CF };
3243
3244 switch (CC) {
3245 default:
3246 llvm_unreachable("Illegal condition code!");
3247 case X86::COND_NO:
3248 case X86::COND_NE:
3249 case X86::COND_GE:
3250 case X86::COND_G:
3251 case X86::COND_AE:
3252 case X86::COND_A:
3253 case X86::COND_NS:
3254 case X86::COND_NP:
3255 return 0;
3256 case X86::COND_O:
3257 return OF;
3258 case X86::COND_B:
3259 case X86::COND_BE:
3260 return CF;
3261 break;
3262 case X86::COND_E:
3263 case X86::COND_LE:
3264 return ZF;
3265 case X86::COND_S:
3266 case X86::COND_L:
3267 return SF;
3268 case X86::COND_P:
3269 return PF;
3270 }
3271}
3272
3273#define GET_X86_NF_TRANSFORM_TABLE
3274#define GET_X86_ND2NONND_TABLE
3275#include "X86GenInstrMapping.inc"
3276
3278 unsigned Opc) {
3279 const auto I = llvm::lower_bound(Table, Opc);
3280 return (I == Table.end() || I->OldOpc != Opc) ? 0U : I->NewOpc;
3281}
3282unsigned X86::getNFVariant(unsigned Opc) {
3283#if defined(EXPENSIVE_CHECKS) && !defined(NDEBUG)
3284 // Make sure the tables are sorted.
3285 static std::atomic<bool> NFTableChecked(false);
3286 if (!NFTableChecked.load(std::memory_order_relaxed)) {
3287 assert(llvm::is_sorted(X86NFTransformTable) &&
3288 "X86NFTransformTable is not sorted!");
3289 NFTableChecked.store(true, std::memory_order_relaxed);
3290 }
3291#endif
3292 return getNewOpcFromTable(X86NFTransformTable, Opc);
3293}
3294
3296 const TargetRegisterInfo *TRI) {
3297 if (!MI.registerDefIsDead(X86::EFLAGS, TRI))
3298 return 0;
3299 // For the instructions are ADDrm/ADDmr with relocation, we'll skip the
3300 // optimization for replacing non-NF with NF. This is to keep backward
3301 // compatiblity with old version of linkers without APX relocation type
3302 // support on Linux OS.
3304 return 0;
3305 return X86::getNFVariant(MI.getOpcode());
3306}
3307
3308unsigned X86::getNonNDVariant(unsigned Opc) {
3309#if defined(EXPENSIVE_CHECKS) && !defined(NDEBUG)
3310 // Make sure the tables are sorted.
3311 static std::atomic<bool> NDTableChecked(false);
3312 if (!NDTableChecked.load(std::memory_order_relaxed)) {
3313 assert(llvm::is_sorted(X86ND2NonNDTable) &&
3314 "X86ND2NonNDTableis not sorted!");
3315 NDTableChecked.store(true, std::memory_order_relaxed);
3316 }
3317#endif
3318 return getNewOpcFromTable(X86ND2NonNDTable, Opc);
3319}
3320
3321/// Return the inverse of the specified condition,
3322/// e.g. turning COND_E to COND_NE.
3324 switch (CC) {
3325 default:
3326 llvm_unreachable("Illegal condition code!");
3327 case X86::COND_E:
3328 return X86::COND_NE;
3329 case X86::COND_NE:
3330 return X86::COND_E;
3331 case X86::COND_L:
3332 return X86::COND_GE;
3333 case X86::COND_LE:
3334 return X86::COND_G;
3335 case X86::COND_G:
3336 return X86::COND_LE;
3337 case X86::COND_GE:
3338 return X86::COND_L;
3339 case X86::COND_B:
3340 return X86::COND_AE;
3341 case X86::COND_BE:
3342 return X86::COND_A;
3343 case X86::COND_A:
3344 return X86::COND_BE;
3345 case X86::COND_AE:
3346 return X86::COND_B;
3347 case X86::COND_S:
3348 return X86::COND_NS;
3349 case X86::COND_NS:
3350 return X86::COND_S;
3351 case X86::COND_P:
3352 return X86::COND_NP;
3353 case X86::COND_NP:
3354 return X86::COND_P;
3355 case X86::COND_O:
3356 return X86::COND_NO;
3357 case X86::COND_NO:
3358 return X86::COND_O;
3359 case X86::COND_NE_OR_P:
3360 return X86::COND_E_AND_NP;
3361 case X86::COND_E_AND_NP:
3362 return X86::COND_NE_OR_P;
3363 }
3364}
3365
3366/// Assuming the flags are set by MI(a,b), return the condition code if we
3367/// modify the instructions such that flags are set by MI(b,a).
3369 switch (CC) {
3370 default:
3371 return X86::COND_INVALID;
3372 case X86::COND_E:
3373 return X86::COND_E;
3374 case X86::COND_NE:
3375 return X86::COND_NE;
3376 case X86::COND_L:
3377 return X86::COND_G;
3378 case X86::COND_LE:
3379 return X86::COND_GE;
3380 case X86::COND_G:
3381 return X86::COND_L;
3382 case X86::COND_GE:
3383 return X86::COND_LE;
3384 case X86::COND_B:
3385 return X86::COND_A;
3386 case X86::COND_BE:
3387 return X86::COND_AE;
3388 case X86::COND_A:
3389 return X86::COND_B;
3390 case X86::COND_AE:
3391 return X86::COND_BE;
3392 }
3393}
3394
3395std::pair<X86::CondCode, bool>
3398 bool NeedSwap = false;
3399 switch (Predicate) {
3400 default:
3401 break;
3402 // Floating-point Predicates
3403 case CmpInst::FCMP_UEQ:
3404 CC = X86::COND_E;
3405 break;
3406 case CmpInst::FCMP_OLT:
3407 NeedSwap = true;
3408 [[fallthrough]];
3409 case CmpInst::FCMP_OGT:
3410 CC = X86::COND_A;
3411 break;
3412 case CmpInst::FCMP_OLE:
3413 NeedSwap = true;
3414 [[fallthrough]];
3415 case CmpInst::FCMP_OGE:
3416 CC = X86::COND_AE;
3417 break;
3418 case CmpInst::FCMP_UGT:
3419 NeedSwap = true;
3420 [[fallthrough]];
3421 case CmpInst::FCMP_ULT:
3422 CC = X86::COND_B;
3423 break;
3424 case CmpInst::FCMP_UGE:
3425 NeedSwap = true;
3426 [[fallthrough]];
3427 case CmpInst::FCMP_ULE:
3428 CC = X86::COND_BE;
3429 break;
3430 case CmpInst::FCMP_ONE:
3431 CC = X86::COND_NE;
3432 break;
3433 case CmpInst::FCMP_UNO:
3434 CC = X86::COND_P;
3435 break;
3436 case CmpInst::FCMP_ORD:
3437 CC = X86::COND_NP;
3438 break;
3439 case CmpInst::FCMP_OEQ:
3440 [[fallthrough]];
3441 case CmpInst::FCMP_UNE:
3442 CC = X86::COND_INVALID;
3443 break;
3444
3445 // Integer Predicates
3446 case CmpInst::ICMP_EQ:
3447 CC = X86::COND_E;
3448 break;
3449 case CmpInst::ICMP_NE:
3450 CC = X86::COND_NE;
3451 break;
3452 case CmpInst::ICMP_UGT:
3453 CC = X86::COND_A;
3454 break;
3455 case CmpInst::ICMP_UGE:
3456 CC = X86::COND_AE;
3457 break;
3458 case CmpInst::ICMP_ULT:
3459 CC = X86::COND_B;
3460 break;
3461 case CmpInst::ICMP_ULE:
3462 CC = X86::COND_BE;
3463 break;
3464 case CmpInst::ICMP_SGT:
3465 CC = X86::COND_G;
3466 break;
3467 case CmpInst::ICMP_SGE:
3468 CC = X86::COND_GE;
3469 break;
3470 case CmpInst::ICMP_SLT:
3471 CC = X86::COND_L;
3472 break;
3473 case CmpInst::ICMP_SLE:
3474 CC = X86::COND_LE;
3475 break;
3476 }
3477
3478 return std::make_pair(CC, NeedSwap);
3479}
3480
3481/// Return a cmov opcode for the given register size in bytes, and operand type.
3482unsigned X86::getCMovOpcode(unsigned RegBytes, bool HasMemoryOperand,
3483 bool HasNDD) {
3484 switch (RegBytes) {
3485 default:
3486 llvm_unreachable("Illegal register size!");
3487#define GET_ND_IF_ENABLED(OPC) (HasNDD ? OPC##_ND : OPC)
3488 case 2:
3489 return HasMemoryOperand ? GET_ND_IF_ENABLED(X86::CMOV16rm)
3490 : GET_ND_IF_ENABLED(X86::CMOV16rr);
3491 case 4:
3492 return HasMemoryOperand ? GET_ND_IF_ENABLED(X86::CMOV32rm)
3493 : GET_ND_IF_ENABLED(X86::CMOV32rr);
3494 case 8:
3495 return HasMemoryOperand ? GET_ND_IF_ENABLED(X86::CMOV64rm)
3496 : GET_ND_IF_ENABLED(X86::CMOV64rr);
3497 }
3498}
3499
3500unsigned X86::getMOVriOpcode(bool Use64BitReg, int64_t Imm) {
3501 if (!Use64BitReg)
3502 return X86::MOV32ri;
3503
3504 if (isUInt<32>(Imm))
3505 return X86::MOV32ri64;
3506 if (isInt<32>(Imm))
3507 return X86::MOV64ri32;
3508 return X86::MOV64ri;
3509}
3510
3511/// Get the VPCMP immediate for the given condition.
3513 switch (CC) {
3514 default:
3515 llvm_unreachable("Unexpected SETCC condition");
3516 case ISD::SETNE:
3517 return 4;
3518 case ISD::SETEQ:
3519 return 0;
3520 case ISD::SETULT:
3521 case ISD::SETLT:
3522 return 1;
3523 case ISD::SETUGT:
3524 case ISD::SETGT:
3525 return 6;
3526 case ISD::SETUGE:
3527 case ISD::SETGE:
3528 return 5;
3529 case ISD::SETULE:
3530 case ISD::SETLE:
3531 return 2;
3532 }
3533}
3534
3535/// Get the VPCMP immediate if the operands are swapped.
3536unsigned X86::getSwappedVPCMPImm(unsigned Imm) {
3537 switch (Imm) {
3538 default:
3539 llvm_unreachable("Unreachable!");
3540 case 0x01:
3541 Imm = 0x06;
3542 break; // LT -> NLE
3543 case 0x02:
3544 Imm = 0x05;
3545 break; // LE -> NLT
3546 case 0x05:
3547 Imm = 0x02;
3548 break; // NLT -> LE
3549 case 0x06:
3550 Imm = 0x01;
3551 break; // NLE -> LT
3552 case 0x00: // EQ
3553 case 0x03: // FALSE
3554 case 0x04: // NE
3555 case 0x07: // TRUE
3556 break;
3557 }
3558
3559 return Imm;
3560}
3561
3562/// Get the VPCOM immediate if the operands are swapped.
3563unsigned X86::getSwappedVPCOMImm(unsigned Imm) {
3564 switch (Imm) {
3565 default:
3566 llvm_unreachable("Unreachable!");
3567 case 0x00:
3568 Imm = 0x02;
3569 break; // LT -> GT
3570 case 0x01:
3571 Imm = 0x03;
3572 break; // LE -> GE
3573 case 0x02:
3574 Imm = 0x00;
3575 break; // GT -> LT
3576 case 0x03:
3577 Imm = 0x01;
3578 break; // GE -> LE
3579 case 0x04: // EQ
3580 case 0x05: // NE
3581 case 0x06: // FALSE
3582 case 0x07: // TRUE
3583 break;
3584 }
3585
3586 return Imm;
3587}
3588
3589/// Get the VCMP immediate if the operands are swapped.
3590unsigned X86::getSwappedVCMPImm(unsigned Imm) {
3591 // Only need the lower 2 bits to distinquish.
3592 switch (Imm & 0x3) {
3593 default:
3594 llvm_unreachable("Unreachable!");
3595 case 0x00:
3596 case 0x03:
3597 // EQ/NE/TRUE/FALSE/ORD/UNORD don't change immediate when commuted.
3598 break;
3599 case 0x01:
3600 case 0x02:
3601 // Need to toggle bits 3:0. Bit 4 stays the same.
3602 Imm ^= 0xf;
3603 break;
3604 }
3605
3606 return Imm;
3607}
3608
3610 if (Info.RegClass == X86::VR128RegClassID ||
3611 Info.RegClass == X86::VR128XRegClassID)
3612 return 128;
3613 if (Info.RegClass == X86::VR256RegClassID ||
3614 Info.RegClass == X86::VR256XRegClassID)
3615 return 256;
3616 if (Info.RegClass == X86::VR512RegClassID)
3617 return 512;
3618 llvm_unreachable("Unknown register class!");
3619}
3620
3621/// Return true if the Reg is X87 register.
3622static bool isX87Reg(Register Reg) {
3623 return (Reg == X86::FPCW || Reg == X86::FPSW ||
3624 (Reg >= X86::ST0 && Reg <= X86::ST7));
3625}
3626
3627/// check if the instruction is X87 instruction
3629 // Call and inlineasm defs X87 register, so we special case it here because
3630 // otherwise calls are incorrectly flagged as x87 instructions
3631 // as a result.
3632 if (MI.isCall() || MI.isInlineAsm())
3633 return false;
3634 for (const MachineOperand &MO : MI.operands()) {
3635 if (!MO.isReg())
3636 continue;
3637 if (isX87Reg(MO.getReg()))
3638 return true;
3639 }
3640 return false;
3641}
3642
3644 auto IsMemOp = [](const MCOperandInfo &OpInfo) {
3645 return OpInfo.OperandType == MCOI::OPERAND_MEMORY;
3646 };
3647
3648 const MCInstrDesc &Desc = MI.getDesc();
3649
3650 // Directly invoke the MC-layer routine for real (i.e., non-pseudo)
3651 // instructions (fast case).
3652 if (!X86II::isPseudo(Desc.TSFlags)) {
3653 int MemRefIdx = X86II::getMemoryOperandIdx(Desc);
3654 if (MemRefIdx >= 0)
3655 return MemRefIdx;
3656#ifdef EXPENSIVE_CHECKS
3657 assert(none_of(Desc.operands(), IsMemOp) &&
3658 "Got false negative from X86II::getMemoryOperandIdx()!");
3659#endif
3660 return -1;
3661 }
3662
3663 // Otherwise, handle pseudo instructions by examining the type of their
3664 // operands (slow case). An instruction cannot have a memory reference if it
3665 // has fewer than AddrNumOperands (= 5) explicit operands.
3666 unsigned NumOps = Desc.getNumOperands();
3668#ifdef EXPENSIVE_CHECKS
3669 assert(none_of(Desc.operands(), IsMemOp) &&
3670 "Expected no operands to have OPERAND_MEMORY type!");
3671#endif
3672 return -1;
3673 }
3674
3675 // The first operand with type OPERAND_MEMORY indicates the start of a memory
3676 // reference. We expect the following AddrNumOperand-1 operands to also have
3677 // OPERAND_MEMORY type.
3678 for (unsigned I = 0, E = NumOps - X86::AddrNumOperands; I != E; ++I) {
3679 if (IsMemOp(Desc.operands()[I])) {
3680#ifdef EXPENSIVE_CHECKS
3681 assert(std::all_of(Desc.operands().begin() + I,
3682 Desc.operands().begin() + I + X86::AddrNumOperands,
3683 IsMemOp) &&
3684 "Expected all five operands in the memory reference to have "
3685 "OPERAND_MEMORY type!");
3686#endif
3687 return I;
3688 }
3689 }
3690
3691 return -1;
3692}
3693
3695 unsigned OpNo) {
3696 assert(MI.getNumOperands() >= (OpNo + X86::AddrNumOperands) &&
3697 "Unexpected number of operands!");
3698
3699 const MachineOperand &Index = MI.getOperand(OpNo + X86::AddrIndexReg);
3700 if (!Index.isReg() || Index.getReg() != X86::NoRegister)
3701 return nullptr;
3702
3703 const MachineOperand &Disp = MI.getOperand(OpNo + X86::AddrDisp);
3704 if (!Disp.isCPI() || Disp.getOffset() != 0)
3705 return nullptr;
3706
3708 MI.getParent()->getParent()->getConstantPool()->getConstants();
3709 const MachineConstantPoolEntry &ConstantEntry = Constants[Disp.getIndex()];
3710
3711 // Bail if this is a machine constant pool entry, we won't be able to dig out
3712 // anything useful.
3713 if (ConstantEntry.isMachineConstantPoolEntry())
3714 return nullptr;
3715
3716 return ConstantEntry.Val.ConstVal;
3717}
3718
3720 switch (MI.getOpcode()) {
3721 case X86::TCRETURNdi:
3722 case X86::TCRETURNri:
3723 case X86::TCRETURNmi:
3724 case X86::TCRETURNdi64:
3725 case X86::TCRETURNri64:
3726 case X86::TCRETURNri64_ImpCall:
3727 case X86::TCRETURNmi64:
3728 return true;
3729 default:
3730 return false;
3731 }
3732}
3733
3736 const MachineInstr &TailCall) const {
3737
3738 const MachineFunction *MF = TailCall.getMF();
3739
3740 if (MF->getTarget().getCodeModel() == CodeModel::Kernel) {
3741 // Kernel patches thunk calls in runtime, these should never be conditional.
3742 const MachineOperand &Target = TailCall.getOperand(0);
3743 if (Target.isSymbol()) {
3744 StringRef Symbol(Target.getSymbolName());
3745 // this is currently only relevant to r11/kernel indirect thunk.
3746 if (Symbol == "__x86_indirect_thunk_r11")
3747 return false;
3748 }
3749 }
3750
3751 if (TailCall.getOpcode() != X86::TCRETURNdi &&
3752 TailCall.getOpcode() != X86::TCRETURNdi64) {
3753 // Only direct calls can be done with a conditional branch.
3754 return false;
3755 }
3756
3757 if (Subtarget.isTargetWin64() && MF->hasWinCFI()) {
3758 // Conditional tail calls confuse the Win64 unwinder.
3759 return false;
3760 }
3761
3762 assert(BranchCond.size() == 1);
3763 if (BranchCond[0].getImm() > X86::LAST_VALID_COND) {
3764 // Can't make a conditional tail call with this condition.
3765 return false;
3766 }
3767
3769 if (X86FI->getTCReturnAddrDelta() != 0 ||
3770 TailCall.getOperand(1).getImm() != 0) {
3771 // A conditional tail call cannot do any stack adjustment.
3772 return false;
3773 }
3774
3775 return true;
3776}
3777
3780 const MachineInstr &TailCall) const {
3781 assert(canMakeTailCallConditional(BranchCond, TailCall));
3782
3784 while (I != MBB.begin()) {
3785 --I;
3786 if (I->isDebugInstr())
3787 continue;
3788 if (!I->isBranch())
3789 assert(0 && "Can't find the branch to replace!");
3790
3792 assert(BranchCond.size() == 1);
3793 if (CC != BranchCond[0].getImm())
3794 continue;
3795
3796 break;
3797 }
3798
3799 unsigned Opc = TailCall.getOpcode() == X86::TCRETURNdi ? X86::TCRETURNdicc
3800 : X86::TCRETURNdi64cc;
3801
3802 auto MIB = BuildMI(MBB, I, MBB.findDebugLoc(I), get(Opc));
3803 MIB->addOperand(TailCall.getOperand(0)); // Destination.
3804 MIB.addImm(0); // Stack offset (not used).
3805 MIB->addOperand(BranchCond[0]); // Condition.
3806 MIB.copyImplicitOps(TailCall); // Regmask and (imp-used) parameters.
3807
3808 // Add implicit uses and defs of all live regs potentially clobbered by the
3809 // call. This way they still appear live across the call.
3811 LiveRegs.addLiveOuts(MBB);
3813 LiveRegs.stepForward(*MIB, Clobbers);
3814 for (const auto &C : Clobbers) {
3815 MIB.addReg(C.first, RegState::Implicit);
3817 }
3818
3819 I->eraseFromParent();
3820}
3821
3822// Given a MBB and its TBB, find the FBB which was a fallthrough MBB (it may
3823// not be a fallthrough MBB now due to layout changes). Return nullptr if the
3824// fallthrough MBB cannot be identified.
3827 // Look for non-EHPad successors other than TBB. If we find exactly one, it
3828 // is the fallthrough MBB. If we find zero, then TBB is both the target MBB
3829 // and fallthrough MBB. If we find more than one, we cannot identify the
3830 // fallthrough MBB and should return nullptr.
3831 MachineBasicBlock *FallthroughBB = nullptr;
3832 for (MachineBasicBlock *Succ : MBB->successors()) {
3833 if (Succ->isEHPad() || (Succ == TBB && FallthroughBB))
3834 continue;
3835 // Return a nullptr if we found more than one fallthrough successor.
3836 if (FallthroughBB && FallthroughBB != TBB)
3837 return nullptr;
3838 FallthroughBB = Succ;
3839 }
3840 return FallthroughBB;
3841}
3842
3843bool X86InstrInfo::analyzeBranchImpl(
3846 SmallVectorImpl<MachineInstr *> &CondBranches, bool AllowModify) const {
3847
3848 // Start from the bottom of the block and work up, examining the
3849 // terminator instructions.
3851 MachineBasicBlock::iterator UnCondBrIter = MBB.end();
3852 while (I != MBB.begin()) {
3853 --I;
3854 if (I->isDebugInstr())
3855 continue;
3856
3857 // Working from the bottom, when we see a non-terminator instruction, we're
3858 // done.
3859 if (!isUnpredicatedTerminator(*I))
3860 break;
3861
3862 // A terminator that isn't a branch can't easily be handled by this
3863 // analysis.
3864 if (!I->isBranch())
3865 return true;
3866
3867 // Handle unconditional branches.
3868 if (I->getOpcode() == X86::JMP_1) {
3869 UnCondBrIter = I;
3870
3871 if (!AllowModify) {
3872 TBB = I->getOperand(0).getMBB();
3873 continue;
3874 }
3875
3876 // If the block has any instructions after a JMP, delete them.
3877 MBB.erase(std::next(I), MBB.end());
3878
3879 Cond.clear();
3880 FBB = nullptr;
3881
3882 // Delete the JMP if it's equivalent to a fall-through.
3883 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
3884 TBB = nullptr;
3885 I->eraseFromParent();
3886 I = MBB.end();
3887 UnCondBrIter = MBB.end();
3888 continue;
3889 }
3890
3891 // TBB is used to indicate the unconditional destination.
3892 TBB = I->getOperand(0).getMBB();
3893 continue;
3894 }
3895
3896 // Handle conditional branches.
3897 X86::CondCode BranchCode = X86::getCondFromBranch(*I);
3898 if (BranchCode == X86::COND_INVALID)
3899 return true; // Can't handle indirect branch.
3900
3901 // In practice we should never have an undef eflags operand, if we do
3902 // abort here as we are not prepared to preserve the flag.
3903 if (I->findRegisterUseOperand(X86::EFLAGS, /*TRI=*/nullptr)->isUndef())
3904 return true;
3905
3906 // Working from the bottom, handle the first conditional branch.
3907 if (Cond.empty()) {
3908 FBB = TBB;
3909 TBB = I->getOperand(0).getMBB();
3911 CondBranches.push_back(&*I);
3912 continue;
3913 }
3914
3915 // Handle subsequent conditional branches. Only handle the case where all
3916 // conditional branches branch to the same destination and their condition
3917 // opcodes fit one of the special multi-branch idioms.
3918 assert(Cond.size() == 1);
3919 assert(TBB);
3920
3921 // If the conditions are the same, we can leave them alone.
3922 X86::CondCode OldBranchCode = (X86::CondCode)Cond[0].getImm();
3923 auto NewTBB = I->getOperand(0).getMBB();
3924 if (OldBranchCode == BranchCode && TBB == NewTBB)
3925 continue;
3926
3927 // If they differ, see if they fit one of the known patterns. Theoretically,
3928 // we could handle more patterns here, but we shouldn't expect to see them
3929 // if instruction selection has done a reasonable job.
3930 if (TBB == NewTBB &&
3931 ((OldBranchCode == X86::COND_P && BranchCode == X86::COND_NE) ||
3932 (OldBranchCode == X86::COND_NE && BranchCode == X86::COND_P))) {
3933 BranchCode = X86::COND_NE_OR_P;
3934 } else if ((OldBranchCode == X86::COND_NP && BranchCode == X86::COND_NE) ||
3935 (OldBranchCode == X86::COND_E && BranchCode == X86::COND_P)) {
3936 if (NewTBB != (FBB ? FBB : getFallThroughMBB(&MBB, TBB)))
3937 return true;
3938
3939 // X86::COND_E_AND_NP usually has two different branch destinations.
3940 //
3941 // JP B1
3942 // JE B2
3943 // JMP B1
3944 // B1:
3945 // B2:
3946 //
3947 // Here this condition branches to B2 only if NP && E. It has another
3948 // equivalent form:
3949 //
3950 // JNE B1
3951 // JNP B2
3952 // JMP B1
3953 // B1:
3954 // B2:
3955 //
3956 // Similarly it branches to B2 only if E && NP. That is why this condition
3957 // is named with COND_E_AND_NP.
3958 BranchCode = X86::COND_E_AND_NP;
3959 } else
3960 return true;
3961
3962 // Update the MachineOperand.
3963 Cond[0].setImm(BranchCode);
3964 CondBranches.push_back(&*I);
3965 }
3966
3967 return false;
3968}
3969
3972 MachineBasicBlock *&FBB,
3974 bool AllowModify) const {
3975 SmallVector<MachineInstr *, 4> CondBranches;
3976 return analyzeBranchImpl(MBB, TBB, FBB, Cond, CondBranches, AllowModify);
3977}
3978
3980 int MemRefBegin = X86II::getMemoryOperandIdx(MI.getDesc());
3981 assert(MemRefBegin >= 0 && "Expected a memory operand");
3982
3983 const MachineOperand &MO = MI.getOperand(MemRefBegin + X86::AddrDisp);
3984 if (!MO.isJTI())
3985 return -1;
3986
3987 return MO.getIndex();
3988}
3989
3991 Register Reg) {
3992 if (!Reg.isVirtual())
3993 return -1;
3995 if (MI == nullptr)
3996 return -1;
3997 unsigned Opcode = MI->getOpcode();
3998 if (Opcode != X86::LEA64r && Opcode != X86::LEA32r)
3999 return -1;
4001}
4002
4004 unsigned Opcode = MI.getOpcode();
4005 // Switch-jump pattern for non-PIC code looks like:
4006 // JMP64m $noreg, 8, %X, %jump-table.X, $noreg
4007 if (Opcode == X86::JMP64m || Opcode == X86::JMP32m) {
4009 }
4010 // The pattern for PIC code looks like:
4011 // %0 = LEA64r $rip, 1, $noreg, %jump-table.X
4012 // %1 = MOVSX64rm32 %0, 4, XX, 0, $noreg
4013 // %2 = ADD64rr %1, %0
4014 // JMP64r %2
4015 if (Opcode == X86::JMP64r || Opcode == X86::JMP32r) {
4016 Register Reg = MI.getOperand(0).getReg();
4017 if (!Reg.isVirtual())
4018 return -1;
4019 const MachineFunction &MF = *MI.getParent()->getParent();
4020 const MachineRegisterInfo &MRI = MF.getRegInfo();
4021 MachineInstr *Add = MRI.getUniqueVRegDef(Reg);
4022 if (Add == nullptr)
4023 return -1;
4024 if (Add->getOpcode() != X86::ADD64rr && Add->getOpcode() != X86::ADD32rr)
4025 return -1;
4026 int JTI1 = getJumpTableIndexFromReg(MRI, Add->getOperand(1).getReg());
4027 if (JTI1 >= 0)
4028 return JTI1;
4029 int JTI2 = getJumpTableIndexFromReg(MRI, Add->getOperand(2).getReg());
4030 if (JTI2 >= 0)
4031 return JTI2;
4032 }
4033 return -1;
4034}
4035
4037 MachineBranchPredicate &MBP,
4038 bool AllowModify) const {
4039 using namespace std::placeholders;
4040
4042 SmallVector<MachineInstr *, 4> CondBranches;
4043 if (analyzeBranchImpl(MBB, MBP.TrueDest, MBP.FalseDest, Cond, CondBranches,
4044 AllowModify))
4045 return true;
4046
4047 if (Cond.size() != 1)
4048 return true;
4049
4050 assert(MBP.TrueDest && "expected!");
4051
4052 if (!MBP.FalseDest)
4053 MBP.FalseDest = MBB.getNextNode();
4054
4056
4057 MachineInstr *ConditionDef = nullptr;
4058 bool SingleUseCondition = true;
4059
4061 if (MI.modifiesRegister(X86::EFLAGS, TRI)) {
4062 ConditionDef = &MI;
4063 break;
4064 }
4065
4066 if (MI.readsRegister(X86::EFLAGS, TRI))
4067 SingleUseCondition = false;
4068 }
4069
4070 if (!ConditionDef)
4071 return true;
4072
4073 if (SingleUseCondition) {
4074 for (auto *Succ : MBB.successors())
4075 if (Succ->isLiveIn(X86::EFLAGS))
4076 SingleUseCondition = false;
4077 }
4078
4079 MBP.ConditionDef = ConditionDef;
4080 MBP.SingleUseCondition = SingleUseCondition;
4081
4082 // Currently we only recognize the simple pattern:
4083 //
4084 // test %reg, %reg
4085 // je %label
4086 //
4087 const unsigned TestOpcode =
4088 Subtarget.is64Bit() ? X86::TEST64rr : X86::TEST32rr;
4089
4090 if (ConditionDef->getOpcode() == TestOpcode &&
4091 ConditionDef->getNumOperands() == 3 &&
4092 ConditionDef->getOperand(0).isIdenticalTo(ConditionDef->getOperand(1)) &&
4093 (Cond[0].getImm() == X86::COND_NE || Cond[0].getImm() == X86::COND_E)) {
4094 MBP.LHS = ConditionDef->getOperand(0);
4095 MBP.RHS = MachineOperand::CreateImm(0);
4096 MBP.Predicate = Cond[0].getImm() == X86::COND_NE
4097 ? MachineBranchPredicate::PRED_NE
4098 : MachineBranchPredicate::PRED_EQ;
4099 return false;
4100 }
4101
4102 return true;
4103}
4104
4106 int *BytesRemoved) const {
4107 assert(!BytesRemoved && "code size not handled");
4108
4110 unsigned Count = 0;
4111
4112 while (I != MBB.begin()) {
4113 --I;
4114 if (I->isDebugInstr())
4115 continue;
4116 if (I->getOpcode() != X86::JMP_1 &&
4118 break;
4119 // Remove the branch.
4120 I->eraseFromParent();
4121 I = MBB.end();
4122 ++Count;
4123 }
4124
4125 return Count;
4126}
4127
4130 MachineBasicBlock *FBB,
4132 const DebugLoc &DL, int *BytesAdded) const {
4133 // Shouldn't be a fall through.
4134 assert(TBB && "insertBranch must not be told to insert a fallthrough");
4135 assert((Cond.size() == 1 || Cond.size() == 0) &&
4136 "X86 branch conditions have one component!");
4137 assert(!BytesAdded && "code size not handled");
4138
4139 if (Cond.empty()) {
4140 // Unconditional branch?
4141 assert(!FBB && "Unconditional branch with multiple successors!");
4142 BuildMI(&MBB, DL, get(X86::JMP_1)).addMBB(TBB);
4143 return 1;
4144 }
4145
4146 // If FBB is null, it is implied to be a fall-through block.
4147 bool FallThru = FBB == nullptr;
4148
4149 // Conditional branch.
4150 unsigned Count = 0;
4152 switch (CC) {
4153 case X86::COND_NE_OR_P:
4154 // Synthesize NE_OR_P with two branches.
4155 BuildMI(&MBB, DL, get(X86::JCC_1)).addMBB(TBB).addImm(X86::COND_NE);
4156 ++Count;
4157 BuildMI(&MBB, DL, get(X86::JCC_1)).addMBB(TBB).addImm(X86::COND_P);
4158 ++Count;
4159 break;
4160 case X86::COND_E_AND_NP:
4161 // Use the next block of MBB as FBB if it is null.
4162 if (FBB == nullptr) {
4163 FBB = getFallThroughMBB(&MBB, TBB);
4164 assert(FBB && "MBB cannot be the last block in function when the false "
4165 "body is a fall-through.");
4166 }
4167 // Synthesize COND_E_AND_NP with two branches.
4168 BuildMI(&MBB, DL, get(X86::JCC_1)).addMBB(FBB).addImm(X86::COND_NE);
4169 ++Count;
4170 BuildMI(&MBB, DL, get(X86::JCC_1)).addMBB(TBB).addImm(X86::COND_NP);
4171 ++Count;
4172 break;
4173 default: {
4174 BuildMI(&MBB, DL, get(X86::JCC_1)).addMBB(TBB).addImm(CC);
4175 ++Count;
4176 }
4177 }
4178 if (!FallThru) {
4179 // Two-way Conditional branch. Insert the second branch.
4180 BuildMI(&MBB, DL, get(X86::JMP_1)).addMBB(FBB);
4181 ++Count;
4182 }
4183 return Count;
4184}
4185
4188 Register DstReg, Register TrueReg,
4189 Register FalseReg, int &CondCycles,
4190 int &TrueCycles, int &FalseCycles) const {
4191 // Not all subtargets have cmov instructions.
4192 if (!Subtarget.canUseCMOV())
4193 return false;
4194 if (Cond.size() != 1)
4195 return false;
4196 // We cannot do the composite conditions, at least not in SSA form.
4198 return false;
4199
4200 // Check register classes.
4201 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
4202 const TargetRegisterClass *RC =
4203 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
4204 if (!RC)
4205 return false;
4206
4207 // We have cmov instructions for 16, 32, and 64 bit general purpose registers.
4208 if (X86::GR16RegClass.hasSubClassEq(RC) ||
4209 X86::GR32RegClass.hasSubClassEq(RC) ||
4210 X86::GR64RegClass.hasSubClassEq(RC)) {
4211 // This latency applies to Pentium M, Merom, Wolfdale, Nehalem, and Sandy
4212 // Bridge. Probably Ivy Bridge as well.
4213 CondCycles = 2;
4214 TrueCycles = 2;
4215 FalseCycles = 2;
4216 return true;
4217 }
4218
4219 // Can't do vectors.
4220 return false;
4221}
4222
4225 const DebugLoc &DL, Register DstReg,
4227 Register FalseReg) const {
4228 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
4230 const TargetRegisterClass &RC = *MRI.getRegClass(DstReg);
4231 assert(Cond.size() == 1 && "Invalid Cond array");
4232 unsigned Opc =
4233 X86::getCMovOpcode(TRI.getRegSizeInBits(RC) / 8,
4234 false /*HasMemoryOperand*/, Subtarget.hasNDD());
4235 BuildMI(MBB, I, DL, get(Opc), DstReg)
4236 .addReg(FalseReg)
4237 .addReg(TrueReg)
4238 .addImm(Cond[0].getImm());
4239}
4240
4241/// Test if the given register is a physical h register.
4242static bool isHReg(Register Reg) {
4243 return X86::GR8_ABCD_HRegClass.contains(Reg);
4244}
4245
4246// Try and copy between VR128/VR64 and GR64 registers.
4247static unsigned CopyToFromAsymmetricReg(Register DestReg, Register SrcReg,
4248 const X86Subtarget &Subtarget) {
4249 bool HasAVX = Subtarget.hasAVX();
4250 bool HasAVX512 = Subtarget.hasAVX512();
4251 bool HasEGPR = Subtarget.hasEGPR();
4252
4253 // SrcReg(MaskReg) -> DestReg(GR64)
4254 // SrcReg(MaskReg) -> DestReg(GR32)
4255
4256 // All KMASK RegClasses hold the same k registers, can be tested against
4257 // anyone.
4258 if (X86::VK16RegClass.contains(SrcReg)) {
4259 if (X86::GR64RegClass.contains(DestReg)) {
4260 assert(Subtarget.hasBWI());
4261 return HasEGPR ? X86::KMOVQrk_EVEX : X86::KMOVQrk;
4262 }
4263 if (X86::GR32RegClass.contains(DestReg))
4264 return Subtarget.hasBWI() ? (HasEGPR ? X86::KMOVDrk_EVEX : X86::KMOVDrk)
4265 : (HasEGPR ? X86::KMOVWrk_EVEX : X86::KMOVWrk);
4266 }
4267
4268 // SrcReg(GR64) -> DestReg(MaskReg)
4269 // SrcReg(GR32) -> DestReg(MaskReg)
4270
4271 // All KMASK RegClasses hold the same k registers, can be tested against
4272 // anyone.
4273 if (X86::VK16RegClass.contains(DestReg)) {
4274 if (X86::GR64RegClass.contains(SrcReg)) {
4275 assert(Subtarget.hasBWI());
4276 return HasEGPR ? X86::KMOVQkr_EVEX : X86::KMOVQkr;
4277 }
4278 if (X86::GR32RegClass.contains(SrcReg))
4279 return Subtarget.hasBWI() ? (HasEGPR ? X86::KMOVDkr_EVEX : X86::KMOVDkr)
4280 : (HasEGPR ? X86::KMOVWkr_EVEX : X86::KMOVWkr);
4281 }
4282
4283 // SrcReg(VR128) -> DestReg(GR64)
4284 // SrcReg(VR64) -> DestReg(GR64)
4285 // SrcReg(GR64) -> DestReg(VR128)
4286 // SrcReg(GR64) -> DestReg(VR64)
4287
4288 if (X86::GR64RegClass.contains(DestReg)) {
4289 if (X86::VR128XRegClass.contains(SrcReg))
4290 // Copy from a VR128 register to a GR64 register.
4291 return HasAVX512 ? X86::VMOVPQIto64Zrr
4292 : HasAVX ? X86::VMOVPQIto64rr
4293 : X86::MOVPQIto64rr;
4294 if (X86::VR64RegClass.contains(SrcReg))
4295 // Copy from a VR64 register to a GR64 register.
4296 return X86::MMX_MOVD64from64rr;
4297 } else if (X86::GR64RegClass.contains(SrcReg)) {
4298 // Copy from a GR64 register to a VR128 register.
4299 if (X86::VR128XRegClass.contains(DestReg))
4300 return HasAVX512 ? X86::VMOV64toPQIZrr
4301 : HasAVX ? X86::VMOV64toPQIrr
4302 : X86::MOV64toPQIrr;
4303 // Copy from a GR64 register to a VR64 register.
4304 if (X86::VR64RegClass.contains(DestReg))
4305 return X86::MMX_MOVD64to64rr;
4306 }
4307
4308 // SrcReg(VR128) -> DestReg(GR32)
4309 // SrcReg(GR32) -> DestReg(VR128)
4310
4311 if (X86::GR32RegClass.contains(DestReg) &&
4312 X86::VR128XRegClass.contains(SrcReg))
4313 // Copy from a VR128 register to a GR32 register.
4314 return HasAVX512 ? X86::VMOVPDI2DIZrr
4315 : HasAVX ? X86::VMOVPDI2DIrr
4316 : X86::MOVPDI2DIrr;
4317
4318 if (X86::VR128XRegClass.contains(DestReg) &&
4319 X86::GR32RegClass.contains(SrcReg))
4320 // Copy from a GR32 register to a VR128 register.
4321 return HasAVX512 ? X86::VMOVDI2PDIZrr
4322 : HasAVX ? X86::VMOVDI2PDIrr
4323 : X86::MOVDI2PDIrr;
4324
4325 return 0;
4326}
4327
4330 const DebugLoc &DL, Register DestReg,
4331 Register SrcReg, bool KillSrc,
4332 bool RenamableDest, bool RenamableSrc) const {
4333 // First deal with the normal symmetric copies.
4334 bool HasAVX = Subtarget.hasAVX();
4335 bool HasVLX = Subtarget.hasVLX();
4336 bool HasEGPR = Subtarget.hasEGPR();
4337 unsigned Opc = 0;
4338 if (X86::GR64RegClass.contains(DestReg, SrcReg))
4339 Opc = X86::MOV64rr;
4340 else if (X86::GR32RegClass.contains(DestReg, SrcReg))
4341 Opc = X86::MOV32rr;
4342 else if (X86::GR16RegClass.contains(DestReg, SrcReg))
4343 Opc = X86::MOV16rr;
4344 else if (X86::GR8RegClass.contains(DestReg, SrcReg)) {
4345 // Copying to or from a physical H register on x86-64 requires a NOREX
4346 // move. Otherwise use a normal move.
4347 if ((isHReg(DestReg) || isHReg(SrcReg)) && Subtarget.is64Bit()) {
4348 Opc = X86::MOV8rr_NOREX;
4349 // Both operands must be encodable without an REX prefix.
4350 assert(X86::GR8_NOREXRegClass.contains(SrcReg, DestReg) &&
4351 "8-bit H register can not be copied outside GR8_NOREX");
4352 } else
4353 Opc = X86::MOV8rr;
4354 } else if (X86::VR64RegClass.contains(DestReg, SrcReg))
4355 Opc = X86::MMX_MOVQ64rr;
4356 else if (X86::VR128XRegClass.contains(DestReg, SrcReg)) {
4357 if (HasVLX)
4358 Opc = X86::VMOVAPSZ128rr;
4359 else if (X86::VR128RegClass.contains(DestReg, SrcReg))
4360 Opc = HasAVX ? X86::VMOVAPSrr : X86::MOVAPSrr;
4361 else {
4362 // If this an extended register and we don't have VLX we need to use a
4363 // 512-bit move.
4364 Opc = X86::VMOVAPSZrr;
4366 DestReg =
4367 TRI->getMatchingSuperReg(DestReg, X86::sub_xmm, &X86::VR512RegClass);
4368 SrcReg =
4369 TRI->getMatchingSuperReg(SrcReg, X86::sub_xmm, &X86::VR512RegClass);
4370 }
4371 } else if (X86::VR256XRegClass.contains(DestReg, SrcReg)) {
4372 if (HasVLX)
4373 Opc = X86::VMOVAPSZ256rr;
4374 else if (X86::VR256RegClass.contains(DestReg, SrcReg))
4375 Opc = X86::VMOVAPSYrr;
4376 else {
4377 // If this an extended register and we don't have VLX we need to use a
4378 // 512-bit move.
4379 Opc = X86::VMOVAPSZrr;
4381 DestReg =
4382 TRI->getMatchingSuperReg(DestReg, X86::sub_ymm, &X86::VR512RegClass);
4383 SrcReg =
4384 TRI->getMatchingSuperReg(SrcReg, X86::sub_ymm, &X86::VR512RegClass);
4385 }
4386 } else if (X86::VR512RegClass.contains(DestReg, SrcReg))
4387 Opc = X86::VMOVAPSZrr;
4388 // All KMASK RegClasses hold the same k registers, can be tested against
4389 // anyone.
4390 else if (X86::VK16RegClass.contains(DestReg, SrcReg))
4391 Opc = Subtarget.hasBWI() ? (HasEGPR ? X86::KMOVQkk_EVEX : X86::KMOVQkk)
4392 : (HasEGPR ? X86::KMOVWkk_EVEX : X86::KMOVWkk);
4393
4394 if (!Opc)
4395 Opc = CopyToFromAsymmetricReg(DestReg, SrcReg, Subtarget);
4396
4397 if (Opc) {
4398 BuildMI(MBB, MI, DL, get(Opc), DestReg)
4399 .addReg(SrcReg, getKillRegState(KillSrc));
4400 return;
4401 }
4402
4403 if (SrcReg == X86::EFLAGS || DestReg == X86::EFLAGS) {
4404 // FIXME: We use a fatal error here because historically LLVM has tried
4405 // lower some of these physreg copies and we want to ensure we get
4406 // reasonable bug reports if someone encounters a case no other testing
4407 // found. This path should be removed after the LLVM 7 release.
4408 report_fatal_error("Unable to copy EFLAGS physical register!");
4409 }
4410
4411 LLVM_DEBUG(dbgs() << "Cannot copy " << RI.getName(SrcReg) << " to "
4412 << RI.getName(DestReg) << '\n');
4413 report_fatal_error("Cannot emit physreg copy instruction");
4414}
4415
4416std::optional<DestSourcePair>
4418 if (MI.isMoveReg()) {
4419 // FIXME: Dirty hack for apparent invariant that doesn't hold when
4420 // subreg_to_reg is coalesced with ordinary copies, such that the bits that
4421 // were asserted as 0 are now undef.
4422 if (MI.getOperand(0).isUndef() && MI.getOperand(0).getSubReg())
4423 return std::nullopt;
4424
4425 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
4426 }
4427 return std::nullopt;
4428}
4429
4430static unsigned getLoadStoreOpcodeForFP16(bool Load, const X86Subtarget &STI) {
4431 if (STI.hasFP16())
4432 return Load ? X86::VMOVSHZrm_alt : X86::VMOVSHZmr;
4433 if (Load)
4434 return X86::MOVSHPrm;
4435 return X86::MOVSHPmr;
4436}
4437
4439 const TargetRegisterClass *RC,
4440 bool IsStackAligned,
4441 const X86Subtarget &STI, bool Load) {
4442 bool HasAVX = STI.hasAVX();
4443 bool HasAVX512 = STI.hasAVX512();
4444 bool HasVLX = STI.hasVLX();
4445 bool HasEGPR = STI.hasEGPR();
4446
4447 assert(RC != nullptr && "Invalid target register class");
4448 switch (STI.getRegisterInfo()->getSpillSize(*RC)) {
4449 default:
4450 llvm_unreachable("Unknown spill size");
4451 case 1:
4452 assert(X86::GR8RegClass.hasSubClassEq(RC) && "Unknown 1-byte regclass");
4453 if (STI.is64Bit())
4454 // Copying to or from a physical H register on x86-64 requires a NOREX
4455 // move. Otherwise use a normal move.
4456 if (isHReg(Reg) || X86::GR8_ABCD_HRegClass.hasSubClassEq(RC))
4457 return Load ? X86::MOV8rm_NOREX : X86::MOV8mr_NOREX;
4458 return Load ? X86::MOV8rm : X86::MOV8mr;
4459 case 2:
4460 if (X86::VK16RegClass.hasSubClassEq(RC))
4461 return Load ? (HasEGPR ? X86::KMOVWkm_EVEX : X86::KMOVWkm)
4462 : (HasEGPR ? X86::KMOVWmk_EVEX : X86::KMOVWmk);
4463 assert(X86::GR16RegClass.hasSubClassEq(RC) && "Unknown 2-byte regclass");
4464 return Load ? X86::MOV16rm : X86::MOV16mr;
4465 case 4:
4466 if (X86::GR32RegClass.hasSubClassEq(RC))
4467 return Load ? X86::MOV32rm : X86::MOV32mr;
4468 if (X86::FR32XRegClass.hasSubClassEq(RC))
4469 return Load ? (HasAVX512 ? X86::VMOVSSZrm_alt
4470 : HasAVX ? X86::VMOVSSrm_alt
4471 : X86::MOVSSrm_alt)
4472 : (HasAVX512 ? X86::VMOVSSZmr
4473 : HasAVX ? X86::VMOVSSmr
4474 : X86::MOVSSmr);
4475 if (X86::RFP32RegClass.hasSubClassEq(RC))
4476 return Load ? X86::LD_Fp32m : X86::ST_Fp32m;
4477 if (X86::VK32RegClass.hasSubClassEq(RC)) {
4478 assert(STI.hasBWI() && "KMOVD requires BWI");
4479 return Load ? (HasEGPR ? X86::KMOVDkm_EVEX : X86::KMOVDkm)
4480 : (HasEGPR ? X86::KMOVDmk_EVEX : X86::KMOVDmk);
4481 }
4482 // All of these mask pair classes have the same spill size, the same kind
4483 // of kmov instructions can be used with all of them.
4484 if (X86::VK1PAIRRegClass.hasSubClassEq(RC) ||
4485 X86::VK2PAIRRegClass.hasSubClassEq(RC) ||
4486 X86::VK4PAIRRegClass.hasSubClassEq(RC) ||
4487 X86::VK8PAIRRegClass.hasSubClassEq(RC) ||
4488 X86::VK16PAIRRegClass.hasSubClassEq(RC))
4489 return Load ? X86::MASKPAIR16LOAD : X86::MASKPAIR16STORE;
4490 if (X86::FR16RegClass.hasSubClassEq(RC) ||
4491 X86::FR16XRegClass.hasSubClassEq(RC))
4492 return getLoadStoreOpcodeForFP16(Load, STI);
4493 llvm_unreachable("Unknown 4-byte regclass");
4494 case 8:
4495 if (X86::GR64RegClass.hasSubClassEq(RC))
4496 return Load ? X86::MOV64rm : X86::MOV64mr;
4497 if (X86::FR64XRegClass.hasSubClassEq(RC))
4498 return Load ? (HasAVX512 ? X86::VMOVSDZrm_alt
4499 : HasAVX ? X86::VMOVSDrm_alt
4500 : X86::MOVSDrm_alt)
4501 : (HasAVX512 ? X86::VMOVSDZmr
4502 : HasAVX ? X86::VMOVSDmr
4503 : X86::MOVSDmr);
4504 if (X86::VR64RegClass.hasSubClassEq(RC))
4505 return Load ? X86::MMX_MOVQ64rm : X86::MMX_MOVQ64mr;
4506 if (X86::RFP64RegClass.hasSubClassEq(RC))
4507 return Load ? X86::LD_Fp64m : X86::ST_Fp64m;
4508 if (X86::VK64RegClass.hasSubClassEq(RC)) {
4509 assert(STI.hasBWI() && "KMOVQ requires BWI");
4510 return Load ? (HasEGPR ? X86::KMOVQkm_EVEX : X86::KMOVQkm)
4511 : (HasEGPR ? X86::KMOVQmk_EVEX : X86::KMOVQmk);
4512 }
4513 llvm_unreachable("Unknown 8-byte regclass");
4514 case 10:
4515 assert(X86::RFP80RegClass.hasSubClassEq(RC) && "Unknown 10-byte regclass");
4516 return Load ? X86::LD_Fp80m : X86::ST_FpP80m;
4517 case 16: {
4518 if (X86::VR128XRegClass.hasSubClassEq(RC)) {
4519 // If stack is realigned we can use aligned stores.
4520 if (IsStackAligned)
4521 return Load ? (HasVLX ? X86::VMOVAPSZ128rm
4522 : HasAVX512 ? X86::VMOVAPSZ128rm_NOVLX
4523 : HasAVX ? X86::VMOVAPSrm
4524 : X86::MOVAPSrm)
4525 : (HasVLX ? X86::VMOVAPSZ128mr
4526 : HasAVX512 ? X86::VMOVAPSZ128mr_NOVLX
4527 : HasAVX ? X86::VMOVAPSmr
4528 : X86::MOVAPSmr);
4529 else
4530 return Load ? (HasVLX ? X86::VMOVUPSZ128rm
4531 : HasAVX512 ? X86::VMOVUPSZ128rm_NOVLX
4532 : HasAVX ? X86::VMOVUPSrm
4533 : X86::MOVUPSrm)
4534 : (HasVLX ? X86::VMOVUPSZ128mr
4535 : HasAVX512 ? X86::VMOVUPSZ128mr_NOVLX
4536 : HasAVX ? X86::VMOVUPSmr
4537 : X86::MOVUPSmr);
4538 }
4539 llvm_unreachable("Unknown 16-byte regclass");
4540 }
4541 case 32:
4542 assert(X86::VR256XRegClass.hasSubClassEq(RC) && "Unknown 32-byte regclass");
4543 // If stack is realigned we can use aligned stores.
4544 if (IsStackAligned)
4545 return Load ? (HasVLX ? X86::VMOVAPSZ256rm
4546 : HasAVX512 ? X86::VMOVAPSZ256rm_NOVLX
4547 : X86::VMOVAPSYrm)
4548 : (HasVLX ? X86::VMOVAPSZ256mr
4549 : HasAVX512 ? X86::VMOVAPSZ256mr_NOVLX
4550 : X86::VMOVAPSYmr);
4551 else
4552 return Load ? (HasVLX ? X86::VMOVUPSZ256rm
4553 : HasAVX512 ? X86::VMOVUPSZ256rm_NOVLX
4554 : X86::VMOVUPSYrm)
4555 : (HasVLX ? X86::VMOVUPSZ256mr
4556 : HasAVX512 ? X86::VMOVUPSZ256mr_NOVLX
4557 : X86::VMOVUPSYmr);
4558 case 64:
4559 assert(X86::VR512RegClass.hasSubClassEq(RC) && "Unknown 64-byte regclass");
4560 assert(STI.hasAVX512() && "Using 512-bit register requires AVX512");
4561 if (IsStackAligned)
4562 return Load ? X86::VMOVAPSZrm : X86::VMOVAPSZmr;
4563 else
4564 return Load ? X86::VMOVUPSZrm : X86::VMOVUPSZmr;
4565 case 1024:
4566 assert(X86::TILERegClass.hasSubClassEq(RC) && "Unknown 1024-byte regclass");
4567 assert(STI.hasAMXTILE() && "Using 8*1024-bit register requires AMX-TILE");
4568#define GET_EGPR_IF_ENABLED(OPC) (STI.hasEGPR() ? OPC##_EVEX : OPC)
4569 return Load ? GET_EGPR_IF_ENABLED(X86::TILELOADD)
4570 : GET_EGPR_IF_ENABLED(X86::TILESTORED);
4571#undef GET_EGPR_IF_ENABLED
4572 }
4573}
4574
4575std::optional<ExtAddrMode>
4577 const TargetRegisterInfo *TRI) const {
4578 int MemRefBegin = X86II::getMemoryOperandIdx(MemI.getDesc());
4579 if (MemRefBegin < 0)
4580 return std::nullopt;
4581
4582 auto &BaseOp = MemI.getOperand(MemRefBegin + X86::AddrBaseReg);
4583 if (!BaseOp.isReg()) // Can be an MO_FrameIndex
4584 return std::nullopt;
4585
4586 const MachineOperand &DispMO = MemI.getOperand(MemRefBegin + X86::AddrDisp);
4587 // Displacement can be symbolic
4588 if (!DispMO.isImm())
4589 return std::nullopt;
4590
4591 ExtAddrMode AM;
4592 AM.BaseReg = BaseOp.getReg();
4593 AM.ScaledReg = MemI.getOperand(MemRefBegin + X86::AddrIndexReg).getReg();
4594 AM.Scale = MemI.getOperand(MemRefBegin + X86::AddrScaleAmt).getImm();
4595 AM.Displacement = DispMO.getImm();
4596 return AM;
4597}
4598
4600 StringRef &ErrInfo) const {
4601 std::optional<ExtAddrMode> AMOrNone = getAddrModeFromMemoryOp(MI, nullptr);
4602 if (!AMOrNone)
4603 return true;
4604
4605 ExtAddrMode AM = *AMOrNone;
4607 if (AM.ScaledReg != X86::NoRegister) {
4608 switch (AM.Scale) {
4609 case 1:
4610 case 2:
4611 case 4:
4612 case 8:
4613 break;
4614 default:
4615 ErrInfo = "Scale factor in address must be 1, 2, 4 or 8";
4616 return false;
4617 }
4618 }
4619 if (!isInt<32>(AM.Displacement)) {
4620 ErrInfo = "Displacement in address must fit into 32-bit signed "
4621 "integer";
4622 return false;
4623 }
4624
4625 return true;
4626}
4627
4629 const Register Reg,
4630 int64_t &ImmVal) const {
4631 Register MovReg = Reg;
4632 const MachineInstr *MovMI = &MI;
4633
4634 // Follow use-def for SUBREG_TO_REG to find the real move immediate
4635 // instruction. It is quite common for x86-64.
4636 if (MI.isSubregToReg()) {
4637 // We use following pattern to setup 64b immediate.
4638 // %8:gr32 = MOV32r0 implicit-def dead $eflags
4639 // %6:gr64 = SUBREG_TO_REG killed %8:gr32, %subreg.sub_32bit
4640 unsigned SubIdx = MI.getOperand(2).getImm();
4641 MovReg = MI.getOperand(1).getReg();
4642 if (SubIdx != X86::sub_32bit)
4643 return false;
4644 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
4645 MovMI = MRI.getUniqueVRegDef(MovReg);
4646 if (!MovMI)
4647 return false;
4648 }
4649
4650 if (MovMI->getOpcode() == X86::MOV32r0 &&
4651 MovMI->getOperand(0).getReg() == MovReg) {
4652 ImmVal = 0;
4653 return true;
4654 }
4655
4656 if (MovMI->getOpcode() != X86::MOV32ri &&
4657 MovMI->getOpcode() != X86::MOV64ri &&
4658 MovMI->getOpcode() != X86::MOV32ri64 && MovMI->getOpcode() != X86::MOV8ri)
4659 return false;
4660 // Mov Src can be a global address.
4661 if (!MovMI->getOperand(1).isImm() || MovMI->getOperand(0).getReg() != MovReg)
4662 return false;
4663 ImmVal = MovMI->getOperand(1).getImm();
4664 return true;
4665}
4666
4668 const MachineInstr *MI, const Register NullValueReg,
4669 const TargetRegisterInfo *TRI) const {
4670 if (!MI->modifiesRegister(NullValueReg, TRI))
4671 return true;
4672 switch (MI->getOpcode()) {
4673 // Shift right/left of a null unto itself is still a null, i.e. rax = shl rax
4674 // X.
4675 case X86::SHR64ri:
4676 case X86::SHR32ri:
4677 case X86::SHL64ri:
4678 case X86::SHL32ri:
4679 assert(MI->getOperand(0).isDef() && MI->getOperand(1).isUse() &&
4680 "expected for shift opcode!");
4681 return MI->getOperand(0).getReg() == NullValueReg &&
4682 MI->getOperand(1).getReg() == NullValueReg;
4683 // Zero extend of a sub-reg of NullValueReg into itself does not change the
4684 // null value.
4685 case X86::MOV32rr:
4686 return llvm::all_of(MI->operands(), [&](const MachineOperand &MO) {
4687 return TRI->isSubRegisterEq(NullValueReg, MO.getReg());
4688 });
4689 default:
4690 return false;
4691 }
4692 llvm_unreachable("Should be handled above!");
4693}
4694
4697 int64_t &Offset, bool &OffsetIsScalable, LocationSize &Width,
4698 const TargetRegisterInfo *TRI) const {
4699 int MemRefBegin = X86II::getMemoryOperandIdx(MemOp.getDesc());
4700 if (MemRefBegin < 0)
4701 return false;
4702
4703 const MachineOperand *BaseOp =
4704 &MemOp.getOperand(MemRefBegin + X86::AddrBaseReg);
4705 if (!BaseOp->isReg()) // Can be an MO_FrameIndex
4706 return false;
4707
4708 if (MemOp.getOperand(MemRefBegin + X86::AddrScaleAmt).getImm() != 1)
4709 return false;
4710
4711 if (MemOp.getOperand(MemRefBegin + X86::AddrIndexReg).getReg() !=
4712 X86::NoRegister)
4713 return false;
4714
4715 const MachineOperand &DispMO = MemOp.getOperand(MemRefBegin + X86::AddrDisp);
4716
4717 // Displacement can be symbolic
4718 if (!DispMO.isImm())
4719 return false;
4720
4721 Offset = DispMO.getImm();
4722
4723 if (!BaseOp->isReg())
4724 return false;
4725
4726 OffsetIsScalable = false;
4727 // FIXME: Relying on memoperands() may not be right thing to do here. Check
4728 // with X86 maintainers, and fix it accordingly. For now, it is ok, since
4729 // there is no use of `Width` for X86 back-end at the moment.
4730 Width = !MemOp.memoperands_empty() ? MemOp.memoperands().front()->getSize()
4732 BaseOps.push_back(BaseOp);
4733 return true;
4734}
4735
4736static unsigned getStoreRegOpcode(Register SrcReg,
4737 const TargetRegisterClass *RC,
4738 bool IsStackAligned,
4739 const X86Subtarget &STI) {
4740 return getLoadStoreRegOpcode(SrcReg, RC, IsStackAligned, STI, false);
4741}
4742
4743static unsigned getLoadRegOpcode(Register DestReg,
4744 const TargetRegisterClass *RC,
4745 bool IsStackAligned, const X86Subtarget &STI) {
4746 return getLoadStoreRegOpcode(DestReg, RC, IsStackAligned, STI, true);
4747}
4748
4749static bool isAMXOpcode(unsigned Opc) {
4750 switch (Opc) {
4751 default:
4752 return false;
4753 case X86::TILELOADD:
4754 case X86::TILESTORED:
4755 case X86::TILELOADD_EVEX:
4756 case X86::TILESTORED_EVEX:
4757 return true;
4758 }
4759}
4760
4763 unsigned Opc, Register Reg, int FrameIdx,
4764 bool isKill) const {
4765 switch (Opc) {
4766 default:
4767 llvm_unreachable("Unexpected special opcode!");
4768 case X86::TILESTORED:
4769 case X86::TILESTORED_EVEX: {
4770 // tilestored %tmm, (%sp, %idx)
4771 MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
4772 Register VirtReg = RegInfo.createVirtualRegister(&X86::GR64_NOSPRegClass);
4773 BuildMI(MBB, MI, DebugLoc(), get(X86::MOV64ri), VirtReg).addImm(64);
4774 MachineInstr *NewMI =
4775 addFrameReference(BuildMI(MBB, MI, DebugLoc(), get(Opc)), FrameIdx)
4776 .addReg(Reg, getKillRegState(isKill));
4778 MO.setReg(VirtReg);
4779 MO.setIsKill(true);
4780 break;
4781 }
4782 case X86::TILELOADD:
4783 case X86::TILELOADD_EVEX: {
4784 // tileloadd (%sp, %idx), %tmm
4785 MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
4786 Register VirtReg = RegInfo.createVirtualRegister(&X86::GR64_NOSPRegClass);
4787 BuildMI(MBB, MI, DebugLoc(), get(X86::MOV64ri), VirtReg).addImm(64);
4789 BuildMI(MBB, MI, DebugLoc(), get(Opc), Reg), FrameIdx);
4791 MO.setReg(VirtReg);
4792 MO.setIsKill(true);
4793 break;
4794 }
4795 }
4796}
4797
4800 bool isKill, int FrameIdx, const TargetRegisterClass *RC,
4801
4802 Register VReg, MachineInstr::MIFlag Flags) const {
4803 const MachineFunction &MF = *MBB.getParent();
4804 const MachineFrameInfo &MFI = MF.getFrameInfo();
4805 assert(MFI.getObjectSize(FrameIdx) >= RI.getSpillSize(*RC) &&
4806 "Stack slot too small for store");
4807
4808 unsigned Alignment = std::max<uint32_t>(RI.getSpillSize(*RC), 16);
4809 bool isAligned =
4810 (Subtarget.getFrameLowering()->getStackAlign() >= Alignment) ||
4811 (RI.canRealignStack(MF) && !MFI.isFixedObjectIndex(FrameIdx));
4812
4813 unsigned Opc = getStoreRegOpcode(SrcReg, RC, isAligned, Subtarget);
4814 if (isAMXOpcode(Opc))
4815 loadStoreTileReg(MBB, MI, Opc, SrcReg, FrameIdx, isKill);
4816 else
4817 addFrameReference(BuildMI(MBB, MI, DebugLoc(), get(Opc)), FrameIdx)
4818 .addReg(SrcReg, getKillRegState(isKill))
4819 .setMIFlag(Flags);
4820}
4821
4824 Register DestReg, int FrameIdx,
4825 const TargetRegisterClass *RC,
4826 Register VReg, unsigned SubReg,
4827 MachineInstr::MIFlag Flags) const {
4828 const MachineFunction &MF = *MBB.getParent();
4829 const MachineFrameInfo &MFI = MF.getFrameInfo();
4830 assert(MFI.getObjectSize(FrameIdx) >= RI.getSpillSize(*RC) &&
4831 "Load size exceeds stack slot");
4832 unsigned Alignment = std::max<uint32_t>(RI.getSpillSize(*RC), 16);
4833 bool isAligned =
4834 (Subtarget.getFrameLowering()->getStackAlign() >= Alignment) ||
4835 (RI.canRealignStack(MF) && !MFI.isFixedObjectIndex(FrameIdx));
4836
4837 unsigned Opc = getLoadRegOpcode(DestReg, RC, isAligned, Subtarget);
4838 if (isAMXOpcode(Opc))
4839 loadStoreTileReg(MBB, MI, Opc, DestReg, FrameIdx);
4840 else
4841 addFrameReference(BuildMI(MBB, MI, DebugLoc(), get(Opc), DestReg), FrameIdx)
4842 .setMIFlag(Flags);
4843}
4844
4846 Register &SrcReg2, int64_t &CmpMask,
4847 int64_t &CmpValue) const {
4848 switch (MI.getOpcode()) {
4849 default:
4850 break;
4851 case X86::CMP64ri32:
4852 case X86::CMP32ri:
4853 case X86::CMP16ri:
4854 case X86::CMP8ri:
4855 SrcReg = MI.getOperand(0).getReg();
4856 SrcReg2 = 0;
4857 if (MI.getOperand(1).isImm()) {
4858 CmpMask = ~0;
4859 CmpValue = MI.getOperand(1).getImm();
4860 } else {
4861 CmpMask = CmpValue = 0;
4862 }
4863 return true;
4864 // A SUB can be used to perform comparison.
4865 CASE_ND(SUB64rm)
4866 CASE_ND(SUB32rm)
4867 CASE_ND(SUB16rm)
4868 CASE_ND(SUB8rm)
4869 SrcReg = MI.getOperand(1).getReg();
4870 SrcReg2 = 0;
4871 CmpMask = 0;
4872 CmpValue = 0;
4873 return true;
4874 CASE_ND(SUB64rr)
4875 CASE_ND(SUB32rr)
4876 CASE_ND(SUB16rr)
4877 CASE_ND(SUB8rr)
4878 SrcReg = MI.getOperand(1).getReg();
4879 SrcReg2 = MI.getOperand(2).getReg();
4880 CmpMask = 0;
4881 CmpValue = 0;
4882 return true;
4883 CASE_ND(SUB64ri32)
4884 CASE_ND(SUB32ri)
4885 CASE_ND(SUB16ri)
4886 CASE_ND(SUB8ri)
4887 SrcReg = MI.getOperand(1).getReg();
4888 SrcReg2 = 0;
4889 if (MI.getOperand(2).isImm()) {
4890 CmpMask = ~0;
4891 CmpValue = MI.getOperand(2).getImm();
4892 } else {
4893 CmpMask = CmpValue = 0;
4894 }
4895 return true;
4896 case X86::CMP64rr:
4897 case X86::CMP32rr:
4898 case X86::CMP16rr:
4899 case X86::CMP8rr:
4900 SrcReg = MI.getOperand(0).getReg();
4901 SrcReg2 = MI.getOperand(1).getReg();
4902 CmpMask = 0;
4903 CmpValue = 0;
4904 return true;
4905 case X86::TEST8rr:
4906 case X86::TEST16rr:
4907 case X86::TEST32rr:
4908 case X86::TEST64rr:
4909 SrcReg = MI.getOperand(0).getReg();
4910 if (MI.getOperand(1).getReg() != SrcReg)
4911 return false;
4912 // Compare against zero.
4913 SrcReg2 = 0;
4914 CmpMask = ~0;
4915 CmpValue = 0;
4916 return true;
4917 case X86::TEST64ri32:
4918 case X86::TEST32ri:
4919 case X86::TEST16ri:
4920 case X86::TEST8ri:
4921 SrcReg = MI.getOperand(0).getReg();
4922 SrcReg2 = 0;
4923 // Force identical compare.
4924 CmpMask = 0;
4925 CmpValue = 0;
4926 return true;
4927 }
4928 return false;
4929}
4930
4931bool X86InstrInfo::isRedundantFlagInstr(const MachineInstr &FlagI,
4932 Register SrcReg, Register SrcReg2,
4933 int64_t ImmMask, int64_t ImmValue,
4934 const MachineInstr &OI, bool *IsSwapped,
4935 int64_t *ImmDelta) const {
4936 switch (OI.getOpcode()) {
4937 case X86::CMP64rr:
4938 case X86::CMP32rr:
4939 case X86::CMP16rr:
4940 case X86::CMP8rr:
4941 CASE_ND(SUB64rr)
4942 CASE_ND(SUB32rr)
4943 CASE_ND(SUB16rr)
4944 CASE_ND(SUB8rr) {
4945 Register OISrcReg;
4946 Register OISrcReg2;
4947 int64_t OIMask;
4948 int64_t OIValue;
4949 if (!analyzeCompare(OI, OISrcReg, OISrcReg2, OIMask, OIValue) ||
4950 OIMask != ImmMask || OIValue != ImmValue)
4951 return false;
4952 if (SrcReg == OISrcReg && SrcReg2 == OISrcReg2) {
4953 *IsSwapped = false;
4954 return true;
4955 }
4956 if (SrcReg == OISrcReg2 && SrcReg2 == OISrcReg) {
4957 *IsSwapped = true;
4958 return true;
4959 }
4960 return false;
4961 }
4962 case X86::CMP64ri32:
4963 case X86::CMP32ri:
4964 case X86::CMP16ri:
4965 case X86::CMP8ri:
4966 case X86::TEST64ri32:
4967 case X86::TEST32ri:
4968 case X86::TEST16ri:
4969 case X86::TEST8ri:
4970 CASE_ND(SUB64ri32)
4971 CASE_ND(SUB32ri)
4972 CASE_ND(SUB16ri)
4973 CASE_ND(SUB8ri)
4974 case X86::TEST64rr:
4975 case X86::TEST32rr:
4976 case X86::TEST16rr:
4977 case X86::TEST8rr: {
4978 if (ImmMask != 0) {
4979 Register OISrcReg;
4980 Register OISrcReg2;
4981 int64_t OIMask;
4982 int64_t OIValue;
4983 if (analyzeCompare(OI, OISrcReg, OISrcReg2, OIMask, OIValue) &&
4984 SrcReg == OISrcReg && ImmMask == OIMask) {
4985 if (OIValue == ImmValue) {
4986 *ImmDelta = 0;
4987 return true;
4988 } else if (static_cast<uint64_t>(ImmValue) ==
4989 static_cast<uint64_t>(OIValue) - 1) {
4990 *ImmDelta = -1;
4991 return true;
4992 } else if (static_cast<uint64_t>(ImmValue) ==
4993 static_cast<uint64_t>(OIValue) + 1) {
4994 *ImmDelta = 1;
4995 return true;
4996 } else {
4997 return false;
4998 }
4999 }
5000 }
5001 return FlagI.isIdenticalTo(OI);
5002 }
5003 default:
5004 return false;
5005 }
5006}
5007
5008inline static bool isCmpRedundantAfterLTZCNT(Register SrcReg, Register SrcReg2,
5009 int64_t ImmMask, int64_t ImmValue,
5010 const MachineInstr &OI) {
5011 switch (OI.getOpcode()) {
5012 default:
5013 return false;
5014 case X86::LZCNT16rr:
5015 case X86::LZCNT32rr:
5016 case X86::LZCNT64rr:
5017 case X86::TZCNT16rr:
5018 case X86::TZCNT32rr:
5019 case X86::TZCNT64rr: {
5020 if (ImmMask != 0 && !SrcReg2.isValid() && ImmValue == 1 &&
5021 OI.getOperand(1).isReg() && SrcReg == OI.getOperand(1).getReg()) {
5022 return true;
5023 }
5024 return false;
5025 }
5026 }
5027}
5028
5029#define CASE_EVEX(OP) \
5030 case X86::OP: \
5031 case X86::OP##_EVEX:
5032
5033/// Check whether the definition can be converted
5034/// to remove a comparison against zero.
5035inline static bool isDefConvertible(const MachineInstr &MI, bool &NoSignFlag,
5036 bool &ClearsOverflowFlag) {
5037 NoSignFlag = false;
5038 ClearsOverflowFlag = false;
5039
5040 // "ELF Handling for Thread-Local Storage" specifies that x86-64 GOTTPOFF, and
5041 // i386 GOTNTPOFF/INDNTPOFF relocations can convert an ADD to a LEA during
5042 // Initial Exec to Local Exec relaxation. In these cases, we must not depend
5043 // on the EFLAGS modification of ADD actually happening in the final binary.
5044 if (MI.getOpcode() == X86::ADD64rm || MI.getOpcode() == X86::ADD32rm) {
5045 unsigned Flags = MI.getOperand(5).getTargetFlags();
5046 if (Flags == X86II::MO_GOTTPOFF || Flags == X86II::MO_INDNTPOFF ||
5047 Flags == X86II::MO_GOTNTPOFF)
5048 return false;
5049 }
5050
5051 switch (MI.getOpcode()) {
5052 default:
5053 return false;
5054
5055 // The shift instructions only modify ZF if their shift count is non-zero.
5056 // N.B.: The processor truncates the shift count depending on the encoding.
5057 CASE_ND(SAR8ri)
5058 CASE_ND(SAR16ri)
5059 CASE_ND(SAR32ri)
5060 CASE_ND(SAR64ri)
5061 CASE_ND(SHR8ri)
5062 CASE_ND(SHR16ri)
5063 CASE_ND(SHR32ri)
5064 CASE_ND(SHR64ri)
5065 return getTruncatedShiftCount(MI, 2) != 0;
5066
5067 // Some left shift instructions can be turned into LEA instructions but only
5068 // if their flags aren't used. Avoid transforming such instructions.
5069 CASE_ND(SHL8ri)
5070 CASE_ND(SHL16ri)
5071 CASE_ND(SHL32ri)
5072 CASE_ND(SHL64ri) {
5073 unsigned ShAmt = getTruncatedShiftCount(MI, 2);
5074 if (isTruncatedShiftCountForLEA(ShAmt))
5075 return false;
5076 return ShAmt != 0;
5077 }
5078
5079 CASE_ND(SHRD16rri8)
5080 CASE_ND(SHRD32rri8)
5081 CASE_ND(SHRD64rri8)
5082 CASE_ND(SHLD16rri8)
5083 CASE_ND(SHLD32rri8)
5084 CASE_ND(SHLD64rri8)
5085 return getTruncatedShiftCount(MI, 3) != 0;
5086
5087 CASE_ND(SUB64ri32)
5088 CASE_ND(SUB32ri)
5089 CASE_ND(SUB16ri)
5090 CASE_ND(SUB8ri)
5091 CASE_ND(SUB64rr)
5092 CASE_ND(SUB32rr)
5093 CASE_ND(SUB16rr)
5094 CASE_ND(SUB8rr)
5095 CASE_ND(SUB64rm)
5096 CASE_ND(SUB32rm)
5097 CASE_ND(SUB16rm)
5098 CASE_ND(SUB8rm)
5099 CASE_ND(DEC64r)
5100 CASE_ND(DEC32r)
5101 CASE_ND(DEC16r)
5102 CASE_ND(DEC8r)
5103 CASE_ND(ADD64ri32)
5104 CASE_ND(ADD32ri)
5105 CASE_ND(ADD16ri)
5106 CASE_ND(ADD8ri)
5107 CASE_ND(ADD64rr)
5108 CASE_ND(ADD32rr)
5109 CASE_ND(ADD16rr)
5110 CASE_ND(ADD8rr)
5111 CASE_ND(ADD64rm)
5112 CASE_ND(ADD32rm)
5113 CASE_ND(ADD16rm)
5114 CASE_ND(ADD8rm)
5115 CASE_ND(INC64r)
5116 CASE_ND(INC32r)
5117 CASE_ND(INC16r)
5118 CASE_ND(INC8r)
5119 CASE_ND(ADC64ri32)
5120 CASE_ND(ADC32ri)
5121 CASE_ND(ADC16ri)
5122 CASE_ND(ADC8ri)
5123 CASE_ND(ADC64rr)
5124 CASE_ND(ADC32rr)
5125 CASE_ND(ADC16rr)
5126 CASE_ND(ADC8rr)
5127 CASE_ND(ADC64rm)
5128 CASE_ND(ADC32rm)
5129 CASE_ND(ADC16rm)
5130 CASE_ND(ADC8rm)
5131 CASE_ND(SBB64ri32)
5132 CASE_ND(SBB32ri)
5133 CASE_ND(SBB16ri)
5134 CASE_ND(SBB8ri)
5135 CASE_ND(SBB64rr)
5136 CASE_ND(SBB32rr)
5137 CASE_ND(SBB16rr)
5138 CASE_ND(SBB8rr)
5139 CASE_ND(SBB64rm)
5140 CASE_ND(SBB32rm)
5141 CASE_ND(SBB16rm)
5142 CASE_ND(SBB8rm)
5143 CASE_ND(NEG8r)
5144 CASE_ND(NEG16r)
5145 CASE_ND(NEG32r)
5146 CASE_ND(NEG64r)
5147 case X86::LZCNT16rr:
5148 case X86::LZCNT16rm:
5149 case X86::LZCNT32rr:
5150 case X86::LZCNT32rm:
5151 case X86::LZCNT64rr:
5152 case X86::LZCNT64rm:
5153 case X86::POPCNT16rr:
5154 case X86::POPCNT16rm:
5155 case X86::POPCNT32rr:
5156 case X86::POPCNT32rm:
5157 case X86::POPCNT64rr:
5158 case X86::POPCNT64rm:
5159 case X86::TZCNT16rr:
5160 case X86::TZCNT16rm:
5161 case X86::TZCNT32rr:
5162 case X86::TZCNT32rm:
5163 case X86::TZCNT64rr:
5164 case X86::TZCNT64rm:
5165 return true;
5166 CASE_ND(AND64ri32)
5167 CASE_ND(AND32ri)
5168 CASE_ND(AND16ri)
5169 CASE_ND(AND8ri)
5170 CASE_ND(AND64rr)
5171 CASE_ND(AND32rr)
5172 CASE_ND(AND16rr)
5173 CASE_ND(AND8rr)
5174 CASE_ND(AND64rm)
5175 CASE_ND(AND32rm)
5176 CASE_ND(AND16rm)
5177 CASE_ND(AND8rm)
5178 CASE_ND(XOR64ri32)
5179 CASE_ND(XOR32ri)
5180 CASE_ND(XOR16ri)
5181 CASE_ND(XOR8ri)
5182 CASE_ND(XOR64rr)
5183 CASE_ND(XOR32rr)
5184 CASE_ND(XOR16rr)
5185 CASE_ND(XOR8rr)
5186 CASE_ND(XOR64rm)
5187 CASE_ND(XOR32rm)
5188 CASE_ND(XOR16rm)
5189 CASE_ND(XOR8rm)
5190 CASE_ND(OR64ri32)
5191 CASE_ND(OR32ri)
5192 CASE_ND(OR16ri)
5193 CASE_ND(OR8ri)
5194 CASE_ND(OR64rr)
5195 CASE_ND(OR32rr)
5196 CASE_ND(OR16rr)
5197 CASE_ND(OR8rr)
5198 CASE_ND(OR64rm)
5199 CASE_ND(OR32rm)
5200 CASE_ND(OR16rm)
5201 CASE_ND(OR8rm)
5202 CASE_EVEX(ANDN32rr)
5203 CASE_EVEX(ANDN32rm)
5204 CASE_EVEX(ANDN64rr)
5205 CASE_EVEX(ANDN64rm)
5206 CASE_EVEX(BLSI32rr)
5207 CASE_EVEX(BLSI32rm)
5208 CASE_EVEX(BLSI64rr)
5209 CASE_EVEX(BLSI64rm)
5210 CASE_EVEX(BLSMSK32rr)
5211 CASE_EVEX(BLSMSK32rm)
5212 CASE_EVEX(BLSMSK64rr)
5213 CASE_EVEX(BLSMSK64rm)
5214 CASE_EVEX(BLSR32rr)
5215 CASE_EVEX(BLSR32rm)
5216 CASE_EVEX(BLSR64rr)
5217 CASE_EVEX(BLSR64rm)
5218 case X86::BLCFILL32rr:
5219 case X86::BLCFILL32rm:
5220 case X86::BLCFILL64rr:
5221 case X86::BLCFILL64rm:
5222 case X86::BLCI32rr:
5223 case X86::BLCI32rm:
5224 case X86::BLCI64rr:
5225 case X86::BLCI64rm:
5226 case X86::BLCIC32rr:
5227 case X86::BLCIC32rm:
5228 case X86::BLCIC64rr:
5229 case X86::BLCIC64rm:
5230 case X86::BLCMSK32rr:
5231 case X86::BLCMSK32rm:
5232 case X86::BLCMSK64rr:
5233 case X86::BLCMSK64rm:
5234 case X86::BLCS32rr:
5235 case X86::BLCS32rm:
5236 case X86::BLCS64rr:
5237 case X86::BLCS64rm:
5238 case X86::BLSFILL32rr:
5239 case X86::BLSFILL32rm:
5240 case X86::BLSFILL64rr:
5241 case X86::BLSFILL64rm:
5242 case X86::BLSIC32rr:
5243 case X86::BLSIC32rm:
5244 case X86::BLSIC64rr:
5245 case X86::BLSIC64rm:
5246 CASE_EVEX(BZHI32rr)
5247 CASE_EVEX(BZHI32rm)
5248 CASE_EVEX(BZHI64rr)
5249 CASE_EVEX(BZHI64rm)
5250 case X86::T1MSKC32rr:
5251 case X86::T1MSKC32rm:
5252 case X86::T1MSKC64rr:
5253 case X86::T1MSKC64rm:
5254 case X86::TZMSK32rr:
5255 case X86::TZMSK32rm:
5256 case X86::TZMSK64rr:
5257 case X86::TZMSK64rm:
5258 // These instructions clear the overflow flag just like TEST.
5259 // FIXME: These are not the only instructions in this switch that clear the
5260 // overflow flag.
5261 ClearsOverflowFlag = true;
5262 return true;
5263 CASE_EVEX(BEXTR32rr)
5264 CASE_EVEX(BEXTR64rr)
5265 CASE_EVEX(BEXTR32rm)
5266 CASE_EVEX(BEXTR64rm)
5267 case X86::BEXTRI32ri:
5268 case X86::BEXTRI32mi:
5269 case X86::BEXTRI64ri:
5270 case X86::BEXTRI64mi:
5271 // BEXTR doesn't update the sign flag so we can't use it. It does clear
5272 // the overflow flag, but that's not useful without the sign flag.
5273 NoSignFlag = true;
5274 return true;
5275 }
5276}
5277
5278/// Check whether the use can be converted to remove a comparison against zero.
5279/// Returns the EFLAGS condition and the operand that we are comparing against zero.
5280static std::pair<X86::CondCode, unsigned> isUseDefConvertible(const MachineInstr &MI) {
5281 switch (MI.getOpcode()) {
5282 default:
5283 return std::make_pair(X86::COND_INVALID, ~0U);
5284 CASE_ND(NEG8r)
5285 CASE_ND(NEG16r)
5286 CASE_ND(NEG32r)
5287 CASE_ND(NEG64r)
5288 return std::make_pair(X86::COND_AE, 1U);
5289 case X86::LZCNT16rr:
5290 case X86::LZCNT32rr:
5291 case X86::LZCNT64rr:
5292 return std::make_pair(X86::COND_B, 1U);
5293 case X86::POPCNT16rr:
5294 case X86::POPCNT32rr:
5295 case X86::POPCNT64rr:
5296 return std::make_pair(X86::COND_E, 1U);
5297 case X86::TZCNT16rr:
5298 case X86::TZCNT32rr:
5299 case X86::TZCNT64rr:
5300 return std::make_pair(X86::COND_B, 1U);
5301 case X86::BSF16rr:
5302 case X86::BSF32rr:
5303 case X86::BSF64rr:
5304 case X86::BSR16rr:
5305 case X86::BSR32rr:
5306 case X86::BSR64rr:
5307 return std::make_pair(X86::COND_E, 2U);
5308 CASE_EVEX(BLSI32rr)
5309 CASE_EVEX(BLSI64rr)
5310 return std::make_pair(X86::COND_AE, 1U);
5311 CASE_EVEX(BLSR32rr)
5312 CASE_EVEX(BLSR64rr)
5313 CASE_EVEX(BLSMSK32rr)
5314 CASE_EVEX(BLSMSK64rr)
5315 return std::make_pair(X86::COND_B, 1U);
5316 // TODO: TBM instructions.
5317 }
5318}
5319#undef CASE_EVEX
5320
5321MachineInstr *X86InstrInfo::findDominatingRedundantFlagInstr(
5322 MachineInstr &CmpInstr, Register SrcReg, Register SrcReg2, int64_t CmpMask,
5323 int64_t CmpValue, MachineBasicBlock *MultiPredMBB, bool &IsSwapped,
5324 int64_t &ImmDelta,
5325 SmallVectorImpl<std::pair<MachineInstr *, unsigned>> &InstsToUpdate) const {
5326 assert(Subtarget.hasNF() && "NF feature required");
5327 const TargetRegisterInfo *TRI = &getRegisterInfo();
5328
5329 // The caller already scanned MultiPredMBB without finding the producer, so it
5330 // must live in a block that strictly dominates MultiPredMBB. Walk
5331 // predecessors backward to find it and prove dominance, avoiding a
5332 // whole-function MachineDominatorTree that would be rebuilt in O(function
5333 // size) per compare.
5334 //
5335 // The producer's block dominates MultiPredMBB iff every backward path funnels
5336 // through it before a function-entry block, so expand predecessors but stop
5337 // at a block holding the producer. Bail if a predecessor-less block is
5338 // reached without the producer (a path bypasses it) or the producer is found
5339 // in two blocks (neither dominates alone). Within a block, scan backward,
5340 // collecting the NF-convertible EFLAGS clobbers above the producer and
5341 // bailing on any other clobber (it would shadow the producer's flags from
5342 // CmpInstr).
5343 //
5344 // Clobbers are staged in Pending and committed only on success. Visited
5345 // (seeded with MultiPredMBB) stops the walk from revisiting a block or
5346 // re-entering the single-predecessor chain, so none is collected twice.
5347 //
5348 // Each NF conversion trades a compact legacy/EVEX-compressed encoding for a
5349 // wider EVEX (often NDD three-operand) one, growing code size, while the
5350 // reuse only removes a single compare. Cap the total number of conversions
5351 // (those the caller already collected on the single-predecessor chain plus
5352 // those the walk stages) so the reuse cannot bloat code just to delete one
5353 // compare.
5354 MachineInstr *Sub = nullptr;
5355 MachineBasicBlock *SubMBB = nullptr;
5357 SmallPtrSet<MachineBasicBlock *, 8> Visited;
5359 Visited.insert(MultiPredMBB);
5360 for (MachineBasicBlock *Pred : MultiPredMBB->predecessors())
5361 if (Visited.insert(Pred).second)
5362 Worklist.push_back(Pred);
5363 while (!Worklist.empty()) {
5364 MachineBasicBlock *MBB = Worklist.pop_back_val();
5365 MachineInstr *Producer = nullptr;
5366 for (MachineInstr &Inst : reverse(*MBB)) {
5367 if (!Inst.modifiesRegister(X86::EFLAGS, TRI))
5368 continue;
5369 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpMask, CmpValue,
5370 Inst, &IsSwapped, &ImmDelta)) {
5371 Producer = &Inst;
5372 break;
5373 }
5374 unsigned NewOpc = X86::getNFVariantIfClobberRemovable(Inst, TRI);
5375 if (!NewOpc)
5376 return nullptr;
5377 if (InstsToUpdate.size() + Pending.size() >= MaxNFConversions)
5378 return nullptr;
5379 Pending.push_back(std::make_pair(&Inst, NewOpc));
5380 }
5381 if (Producer) {
5382 // A producer in a second block means neither dominates alone.
5383 if (Sub && SubMBB != MBB)
5384 return nullptr;
5385 Sub = Producer;
5386 SubMBB = MBB;
5387 continue;
5388 }
5389 // Entry reached without the producer: some path bypasses it.
5390 if (MBB->pred_empty())
5391 return nullptr;
5392 for (MachineBasicBlock *Pred : MBB->predecessors())
5393 if (Visited.insert(Pred).second)
5394 Worklist.push_back(Pred);
5395 }
5396 if (!Sub)
5397 return nullptr;
5398
5399 // The forward condition-code fixup in the caller (OpsToUpdate) only rewrites
5400 // EFLAGS users within CmpMBB. When the producer's flags require a condition
5401 // swap or an immediate adjustment, EFLAGS users elsewhere in the dominated
5402 // region or in CmpMBB's successors (when EFLAGS is live-out) would also need
5403 // rewriting, which is not handled here. Restrict the multi-predecessor case
5404 // to producers that yield identical flags.
5405 if (IsSwapped || ImmDelta != 0)
5406 return nullptr;
5407
5408 InstsToUpdate.append(Pending.begin(), Pending.end());
5409 return Sub;
5410}
5411
5412/// Check if there exists an earlier instruction that
5413/// operates on the same source operands and sets flags in the same way as
5414/// Compare; remove Compare if possible.
5416 Register SrcReg2, int64_t CmpMask,
5417 int64_t CmpValue,
5418 const MachineRegisterInfo *MRI) const {
5419 // Check whether we can replace SUB with CMP.
5420 switch (CmpInstr.getOpcode()) {
5421 default:
5422 break;
5423 CASE_ND(SUB64ri32)
5424 CASE_ND(SUB32ri)
5425 CASE_ND(SUB16ri)
5426 CASE_ND(SUB8ri)
5427 CASE_ND(SUB64rm)
5428 CASE_ND(SUB32rm)
5429 CASE_ND(SUB16rm)
5430 CASE_ND(SUB8rm)
5431 CASE_ND(SUB64rr)
5432 CASE_ND(SUB32rr)
5433 CASE_ND(SUB16rr)
5434 CASE_ND(SUB8rr) {
5435 if (!MRI->use_nodbg_empty(CmpInstr.getOperand(0).getReg()))
5436 return false;
5437 // There is no use of the destination register, we can replace SUB with CMP.
5438 unsigned NewOpcode = 0;
5439#define FROM_TO(A, B) \
5440 CASE_ND(A) NewOpcode = X86::B; \
5441 break;
5442 switch (CmpInstr.getOpcode()) {
5443 default:
5444 llvm_unreachable("Unreachable!");
5445 FROM_TO(SUB64rm, CMP64rm)
5446 FROM_TO(SUB32rm, CMP32rm)
5447 FROM_TO(SUB16rm, CMP16rm)
5448 FROM_TO(SUB8rm, CMP8rm)
5449 FROM_TO(SUB64rr, CMP64rr)
5450 FROM_TO(SUB32rr, CMP32rr)
5451 FROM_TO(SUB16rr, CMP16rr)
5452 FROM_TO(SUB8rr, CMP8rr)
5453 FROM_TO(SUB64ri32, CMP64ri32)
5454 FROM_TO(SUB32ri, CMP32ri)
5455 FROM_TO(SUB16ri, CMP16ri)
5456 FROM_TO(SUB8ri, CMP8ri)
5457 }
5458#undef FROM_TO
5459 CmpInstr.setDesc(get(NewOpcode));
5460 CmpInstr.removeOperand(0);
5461 // Mutating this instruction invalidates any debug data associated with it.
5462 CmpInstr.dropDebugNumber();
5463 // Fall through to optimize Cmp if Cmp is CMPrr or CMPri.
5464 if (NewOpcode == X86::CMP64rm || NewOpcode == X86::CMP32rm ||
5465 NewOpcode == X86::CMP16rm || NewOpcode == X86::CMP8rm)
5466 return false;
5467 }
5468 }
5469
5470 // The following code tries to remove the comparison by re-using EFLAGS
5471 // from earlier instructions.
5472
5473 bool IsCmpZero = (CmpMask != 0 && CmpValue == 0);
5474
5475 // Transformation currently requires SSA values.
5476 if (SrcReg2.isPhysical())
5477 return false;
5478 MachineInstr *SrcRegDef = MRI->getVRegDef(SrcReg);
5479 assert(SrcRegDef && "Must have a definition (SSA)");
5480
5481 MachineInstr *MI = nullptr;
5482 MachineInstr *Sub = nullptr;
5483 MachineInstr *Movr0Inst = nullptr;
5484 MachineInstr *LTZCNTInst = nullptr;
5486 bool NoSignFlag = false;
5487 bool ClearsOverflowFlag = false;
5488 bool ShouldUpdateCC = false;
5489 bool IsSwapped = false;
5490 bool HasNF = Subtarget.hasNF();
5491 unsigned OpNo = 0;
5493 int64_t ImmDelta = 0;
5494
5495 // Search backward from CmpInstr for the next instruction defining EFLAGS.
5497 MachineBasicBlock &CmpMBB = *CmpInstr.getParent();
5499 std::next(MachineBasicBlock::reverse_iterator(CmpInstr));
5500 for (MachineBasicBlock *MBB = &CmpMBB;;) {
5501 for (MachineInstr &Inst : make_range(From, MBB->rend())) {
5502 // Try to use EFLAGS from the instruction defining %SrcReg. Example:
5503 // %eax = addl ...
5504 // ... // EFLAGS not changed
5505 // testl %eax, %eax // <-- can be removed
5506 if (&Inst == SrcRegDef) {
5507 if (IsCmpZero &&
5508 isDefConvertible(Inst, NoSignFlag, ClearsOverflowFlag)) {
5509 MI = &Inst;
5510 break;
5511 }
5512
5513 // Look back for the following pattern, in which case the
5514 // test16rr/test64rr instruction could be erased.
5515 //
5516 // Example for test16rr:
5517 // %reg = and32ri %in_reg, 5
5518 // ... // EFLAGS not changed.
5519 // %src_reg = copy %reg.sub_16bit:gr32
5520 // test16rr %src_reg, %src_reg, implicit-def $eflags
5521 // Example for test64rr:
5522 // %reg = and32ri %in_reg, 5
5523 // ... // EFLAGS not changed.
5524 // %src_reg = subreg_to_reg %reg, %subreg.sub_index
5525 // test64rr %src_reg, %src_reg, implicit-def $eflags
5526 MachineInstr *AndInstr = nullptr;
5527 if (IsCmpZero &&
5528 findRedundantFlagInstr(CmpInstr, Inst, MRI, &AndInstr, TRI,
5529 Subtarget, NoSignFlag, ClearsOverflowFlag)) {
5530 assert(AndInstr != nullptr && X86::isAND(AndInstr->getOpcode()));
5531 MI = AndInstr;
5532 break;
5533 }
5534 // Cannot find other candidates before definition of SrcReg.
5535 return false;
5536 }
5537
5538 if (Inst.modifiesRegister(X86::EFLAGS, TRI)) {
5539 // Try to use EFLAGS produced by an instruction reading %SrcReg.
5540 // Example:
5541 // %eax = ...
5542 // ...
5543 // popcntl %eax
5544 // ... // EFLAGS not changed
5545 // testl %eax, %eax // <-- can be removed
5546 if (IsCmpZero) {
5547 std::tie(NewCC, OpNo) = isUseDefConvertible(Inst);
5548 if (NewCC != X86::COND_INVALID && Inst.getOperand(OpNo).isReg() &&
5549 Inst.getOperand(OpNo).getReg() == SrcReg) {
5550 ShouldUpdateCC = true;
5551 MI = &Inst;
5552 break;
5553 }
5554 }
5555
5556 // Try to use EFLAGS from an instruction with similar flag results.
5557 // Example:
5558 // sub x, y or cmp x, y
5559 // ... // EFLAGS not changed
5560 // cmp x, y // <-- can be removed
5561 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpMask, CmpValue,
5562 Inst, &IsSwapped, &ImmDelta)) {
5563 Sub = &Inst;
5564 break;
5565 }
5566
5567 if (isCmpRedundantAfterLTZCNT(SrcReg, SrcReg2, CmpMask, CmpValue,
5568 Inst)) {
5569 LTZCNTInst = &Inst;
5570 break;
5571 }
5572
5573 // MOV32r0 is implemented with xor which clobbers condition code. It is
5574 // safe to move up, if the definition to EFLAGS is dead and earlier
5575 // instructions do not read or write EFLAGS.
5576 if (!Movr0Inst && Inst.getOpcode() == X86::MOV32r0 &&
5577 Inst.registerDefIsDead(X86::EFLAGS, TRI)) {
5578 Movr0Inst = &Inst;
5579 continue;
5580 }
5581
5582 // Try to replace non-NF with NF instructions.
5583 if (HasNF) {
5584 unsigned NewOp = X86::getNFVariantIfClobberRemovable(Inst, TRI);
5585 if (!NewOp)
5586 return false;
5587
5588 InstsToUpdate.push_back(std::make_pair(&Inst, NewOp));
5589 continue;
5590 }
5591
5592 // Cannot do anything for any other EFLAG changes.
5593 return false;
5594 }
5595 }
5596
5597 if (MI || Sub || LTZCNTInst)
5598 break;
5599
5600 // Reached the begin of the basic block. If it has exactly one predecessor,
5601 // continue the backward scan there. Otherwise (multiple predecessors), try
5602 // to reuse EFLAGS from a dominating producer (handled below).
5603 if (MBB->pred_size() != 1) {
5604 // The block has multiple predecessors. We can still reuse EFLAGS from an
5605 // equivalent flag producer that dominates CmpInstr, provided every path
5606 // from that producer to CmpInstr only clobbers EFLAGS via instructions
5607 // that have an NF (no-flags) variant (which requires APX). This handles
5608 // patterns like (CMP duplicated by CodeGenPrepare across a diamond):
5609 // entry: cmp %x, C ; br
5610 // bb1: imul ... ; clobbers EFLAGS -> {nf} imul
5611 // bb2: ...
5612 // bb3: cmp %x, C ; <-- redundant, reuse EFLAGS from entry
5613 // cmovcc ...
5614 // The helper caps the total number of NF conversions so this cannot grow
5615 // code size without bound just to delete one compare.
5616 if (HasNF)
5617 Sub = findDominatingRedundantFlagInstr(
5618 CmpInstr, SrcReg, SrcReg2, CmpMask, CmpValue, MBB, IsSwapped,
5619 ImmDelta, InstsToUpdate);
5620 if (!Sub)
5621 return false;
5622 break;
5623 }
5624 MBB = *MBB->pred_begin();
5625 From = MBB->rbegin();
5626 }
5627
5628 // Scan forward from the instruction after CmpInstr for uses of EFLAGS.
5629 // It is safe to remove CmpInstr if EFLAGS is redefined or killed.
5630 // If we are done with the basic block, we need to check whether EFLAGS is
5631 // live-out.
5632 bool FlagsMayLiveOut = true;
5634 MachineBasicBlock::iterator AfterCmpInstr =
5635 std::next(MachineBasicBlock::iterator(CmpInstr));
5636 for (MachineInstr &Instr : make_range(AfterCmpInstr, CmpMBB.end())) {
5637 bool ModifyEFLAGS = Instr.modifiesRegister(X86::EFLAGS, TRI);
5638 bool UseEFLAGS = Instr.readsRegister(X86::EFLAGS, TRI);
5639 // We should check the usage if this instruction uses and updates EFLAGS.
5640 if (!UseEFLAGS && ModifyEFLAGS) {
5641 // It is safe to remove CmpInstr if EFLAGS is updated again.
5642 FlagsMayLiveOut = false;
5643 break;
5644 }
5645 if (!UseEFLAGS && !ModifyEFLAGS)
5646 continue;
5647
5648 // EFLAGS is used by this instruction.
5649 X86::CondCode OldCC = X86::getCondFromMI(Instr);
5650 if ((MI || IsSwapped || ImmDelta != 0) && OldCC == X86::COND_INVALID)
5651 return false;
5652
5653 X86::CondCode ReplacementCC = X86::COND_INVALID;
5654 if (MI) {
5655 switch (OldCC) {
5656 default:
5657 break;
5658 case X86::COND_A:
5659 case X86::COND_AE:
5660 case X86::COND_B:
5661 case X86::COND_BE:
5662 // CF is used, we can't perform this optimization.
5663 return false;
5664 case X86::COND_G:
5665 case X86::COND_GE:
5666 case X86::COND_L:
5667 case X86::COND_LE:
5668 // If SF is used, but the instruction doesn't update the SF, then we
5669 // can't do the optimization.
5670 if (NoSignFlag)
5671 return false;
5672 [[fallthrough]];
5673 case X86::COND_O:
5674 case X86::COND_NO:
5675 // If OF is used, the instruction needs to clear it like CmpZero does.
5676 if (!ClearsOverflowFlag)
5677 return false;
5678 break;
5679 case X86::COND_S:
5680 case X86::COND_NS:
5681 // If SF is used, but the instruction doesn't update the SF, then we
5682 // can't do the optimization.
5683 if (NoSignFlag)
5684 return false;
5685 break;
5686 }
5687
5688 // If we're updating the condition code check if we have to reverse the
5689 // condition.
5690 if (ShouldUpdateCC)
5691 switch (OldCC) {
5692 default:
5693 return false;
5694 case X86::COND_E:
5695 ReplacementCC = NewCC;
5696 break;
5697 case X86::COND_NE:
5698 ReplacementCC = GetOppositeBranchCondition(NewCC);
5699 break;
5700 }
5701 } else if (IsSwapped) {
5702 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code needs
5703 // to be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
5704 // We swap the condition code and synthesize the new opcode.
5705 ReplacementCC = getSwappedCondition(OldCC);
5706 if (ReplacementCC == X86::COND_INVALID)
5707 return false;
5708 ShouldUpdateCC = true;
5709 } else if (ImmDelta != 0) {
5710 unsigned BitWidth = RI.getRegSizeInBits(*MRI->getRegClass(SrcReg));
5711 // Shift amount for min/max constants to adjust for 8/16/32 instruction
5712 // sizes.
5713 switch (OldCC) {
5714 case X86::COND_L: // x <s (C + 1) --> x <=s C
5715 if (ImmDelta != 1 || APInt::getSignedMinValue(BitWidth) == CmpValue)
5716 return false;
5717 ReplacementCC = X86::COND_LE;
5718 break;
5719 case X86::COND_B: // x <u (C + 1) --> x <=u C
5720 if (ImmDelta != 1 || CmpValue == 0)
5721 return false;
5722 ReplacementCC = X86::COND_BE;
5723 break;
5724 case X86::COND_GE: // x >=s (C + 1) --> x >s C
5725 if (ImmDelta != 1 || APInt::getSignedMinValue(BitWidth) == CmpValue)
5726 return false;
5727 ReplacementCC = X86::COND_G;
5728 break;
5729 case X86::COND_AE: // x >=u (C + 1) --> x >u C
5730 if (ImmDelta != 1 || CmpValue == 0)
5731 return false;
5732 ReplacementCC = X86::COND_A;
5733 break;
5734 case X86::COND_G: // x >s (C - 1) --> x >=s C
5735 if (ImmDelta != -1 || APInt::getSignedMaxValue(BitWidth) == CmpValue)
5736 return false;
5737 ReplacementCC = X86::COND_GE;
5738 break;
5739 case X86::COND_A: // x >u (C - 1) --> x >=u C
5740 if (ImmDelta != -1 || APInt::getMaxValue(BitWidth) == CmpValue)
5741 return false;
5742 ReplacementCC = X86::COND_AE;
5743 break;
5744 case X86::COND_LE: // x <=s (C - 1) --> x <s C
5745 if (ImmDelta != -1 || APInt::getSignedMaxValue(BitWidth) == CmpValue)
5746 return false;
5747 ReplacementCC = X86::COND_L;
5748 break;
5749 case X86::COND_BE: // x <=u (C - 1) --> x <u C
5750 if (ImmDelta != -1 || APInt::getMaxValue(BitWidth) == CmpValue)
5751 return false;
5752 ReplacementCC = X86::COND_B;
5753 break;
5754 default:
5755 return false;
5756 }
5757 ShouldUpdateCC = true;
5758 }
5759
5760 if (LTZCNTInst) {
5761 unsigned InstCode = Instr.getOpcode();
5762 if (!X86::isADC(InstCode) && !X86::isSBB(InstCode) &&
5763 !X86::isRCL(InstCode) && !X86::isRCR(InstCode))
5764 return false;
5765
5766 MI = LTZCNTInst;
5767 }
5768
5769 if (ShouldUpdateCC && ReplacementCC != OldCC) {
5770 // Push the MachineInstr to OpsToUpdate.
5771 // If it is safe to remove CmpInstr, the condition code of these
5772 // instructions will be modified.
5773 OpsToUpdate.push_back(std::make_pair(&Instr, ReplacementCC));
5774 }
5775 if (ModifyEFLAGS || Instr.killsRegister(X86::EFLAGS, TRI)) {
5776 // It is safe to remove CmpInstr if EFLAGS is updated again or killed.
5777 FlagsMayLiveOut = false;
5778 break;
5779 }
5780 }
5781
5782 // If we have to update users but EFLAGS is live-out abort, since we cannot
5783 // easily find all of the users.
5784 if ((MI != nullptr || ShouldUpdateCC) && FlagsMayLiveOut) {
5785 for (MachineBasicBlock *Successor : CmpMBB.successors())
5786 if (Successor->isLiveIn(X86::EFLAGS))
5787 return false;
5788 }
5789
5790 // The instruction to be updated is either Sub or MI.
5791 assert((MI == nullptr || Sub == nullptr) && "Should not have Sub and MI set");
5792 Sub = MI != nullptr ? MI : Sub;
5793 MachineBasicBlock *SubBB = Sub->getParent();
5794 // Move Movr0Inst to the appropriate place before Sub.
5795 if (Movr0Inst) {
5796 // Only move within the same block so we don't accidentally move to a
5797 // block with higher execution frequency.
5798 if (&CmpMBB != SubBB)
5799 return false;
5800 // Look backwards until we find a def that doesn't use the current EFLAGS.
5802 InsertE = Sub->getParent()->rend();
5803 for (; InsertI != InsertE; ++InsertI) {
5804 MachineInstr *Instr = &*InsertI;
5805 if (!Instr->readsRegister(X86::EFLAGS, TRI) &&
5806 Instr->modifiesRegister(X86::EFLAGS, TRI)) {
5807 Movr0Inst->getParent()->remove(Movr0Inst);
5808 Instr->getParent()->insert(MachineBasicBlock::iterator(Instr),
5809 Movr0Inst);
5810 break;
5811 }
5812 }
5813 if (InsertI == InsertE)
5814 return false;
5815 }
5816
5817 // Replace non-NF with NF instructions.
5818 for (auto &Inst : InstsToUpdate) {
5819 Inst.first->setDesc(get(Inst.second));
5820 Inst.first->removeOperand(
5821 Inst.first->findRegisterDefOperandIdx(X86::EFLAGS, /*TRI=*/nullptr));
5822 }
5823
5824 // Make sure Sub instruction defines EFLAGS and mark the def live.
5825 MachineOperand *FlagDef =
5826 Sub->findRegisterDefOperand(X86::EFLAGS, /*TRI=*/nullptr);
5827 assert(FlagDef && "Unable to locate a def EFLAGS operand");
5828 FlagDef->setIsDead(false);
5829
5830 CmpInstr.eraseFromParent();
5831
5832 // Modify the condition code of instructions in OpsToUpdate.
5833 for (auto &Op : OpsToUpdate) {
5834 Op.first->getOperand(Op.first->getDesc().getNumOperands() - 1)
5835 .setImm(Op.second);
5836 }
5837 // Add EFLAGS to block live-ins between CmpBB and block of flags producer.
5838 // Walk the CFG backward from CmpMBB up to (but excluding) SubBB, marking
5839 // EFLAGS live-in on every block in between. SubBB dominates CmpMBB (whether
5840 // the producer was found by the single-predecessor backward walk or the
5841 // multi-predecessor dominator search), so the walk reaches SubBB on every
5842 // path and never escapes above it. A single-predecessor chain is just the
5843 // degenerate case where every block has exactly one predecessor.
5845 SmallVector<MachineBasicBlock *, 8> Worklist(1, &CmpMBB);
5846 Visited.insert(&CmpMBB);
5847 while (!Worklist.empty()) {
5848 MachineBasicBlock *MBB = Worklist.pop_back_val();
5849 // EFLAGS is produced inside SubBB, so it is not live-in there.
5850 if (MBB == SubBB)
5851 continue;
5852 if (!MBB->isLiveIn(X86::EFLAGS))
5853 MBB->addLiveIn(X86::EFLAGS);
5854 for (MachineBasicBlock *Pred : MBB->predecessors())
5855 if (Visited.insert(Pred).second)
5856 Worklist.push_back(Pred);
5857 }
5858 return true;
5859}
5860
5861/// \returns true if the instruction can be changed to COPY when imm is 0.
5862static bool canConvert2Copy(unsigned Opc) {
5863 switch (Opc) {
5864 default:
5865 return false;
5866 CASE_ND(ADD64ri32)
5867 CASE_ND(SUB64ri32)
5868 CASE_ND(OR64ri32)
5869 CASE_ND(XOR64ri32)
5870 CASE_ND(ADD32ri)
5871 CASE_ND(SUB32ri)
5872 CASE_ND(OR32ri)
5873 CASE_ND(XOR32ri)
5874 return true;
5875 }
5876}
5877
5878/// Convert an ALUrr opcode to corresponding ALUri opcode. Such as
5879/// ADD32rr ==> ADD32ri
5880static unsigned convertALUrr2ALUri(unsigned Opc) {
5881 switch (Opc) {
5882 default:
5883 return 0;
5884#define FROM_TO(FROM, TO) \
5885 case X86::FROM: \
5886 return X86::TO; \
5887 case X86::FROM##_ND: \
5888 return X86::TO##_ND;
5889 FROM_TO(ADC64rr, ADC64ri32)
5890 FROM_TO(SBB64rr, SBB64ri32)
5891 FROM_TO(AND64rr, AND64ri32)
5892 FROM_TO(OR64rr, OR64ri32)
5893 FROM_TO(XOR64rr, XOR64ri32)
5894 FROM_TO(SHR64rCL, SHR64ri)
5895 FROM_TO(SHL64rCL, SHL64ri)
5896 FROM_TO(SAR64rCL, SAR64ri)
5897 FROM_TO(ROL64rCL, ROL64ri)
5898 FROM_TO(ROR64rCL, ROR64ri)
5899 FROM_TO(RCL64rCL, RCL64ri)
5900 FROM_TO(RCR64rCL, RCR64ri)
5901 FROM_TO(ADD32rr, ADD32ri)
5902 FROM_TO(ADC32rr, ADC32ri)
5903 FROM_TO(SUB32rr, SUB32ri)
5904 FROM_TO(SBB32rr, SBB32ri)
5905 FROM_TO(AND32rr, AND32ri)
5906 FROM_TO(OR32rr, OR32ri)
5907 FROM_TO(XOR32rr, XOR32ri)
5908 FROM_TO(SHR32rCL, SHR32ri)
5909 FROM_TO(SHL32rCL, SHL32ri)
5910 FROM_TO(SAR32rCL, SAR32ri)
5911 FROM_TO(ROL32rCL, ROL32ri)
5912 FROM_TO(ROR32rCL, ROR32ri)
5913 FROM_TO(RCL32rCL, RCL32ri)
5914 FROM_TO(RCR32rCL, RCR32ri)
5915#undef FROM_TO
5916#define FROM_TO(FROM, TO) \
5917 case X86::FROM: \
5918 return X86::TO;
5919 FROM_TO(ADD64rr, ADD64ri32)
5920 FROM_TO(SUB64rr, SUB64ri32)
5921 FROM_TO(TEST64rr, TEST64ri32)
5922 FROM_TO(CTEST64rr, CTEST64ri32)
5923 FROM_TO(CMP64rr, CMP64ri32)
5924 FROM_TO(CCMP64rr, CCMP64ri32)
5925 FROM_TO(TEST32rr, TEST32ri)
5926 FROM_TO(CTEST32rr, CTEST32ri)
5927 FROM_TO(CMP32rr, CMP32ri)
5928 FROM_TO(CCMP32rr, CCMP32ri)
5929#undef FROM_TO
5930 case X86::ADD64rr_ND:
5931 return X86::ADD64ri32_ND;
5932 case X86::SUB64rr_ND:
5933 return X86::SUB64ri32_ND;
5934 }
5935}
5936
5937/// Reg is assigned ImmVal in DefMI, and is used in UseMI.
5938/// If MakeChange is true, this function tries to replace Reg by ImmVal in
5939/// UseMI. If MakeChange is false, just check if folding is possible.
5940//
5941/// \returns true if folding is successful or possible.
5942bool X86InstrInfo::foldImmediateImpl(MachineInstr &UseMI, MachineInstr *DefMI,
5943 Register Reg, int64_t ImmVal,
5945 bool MakeChange) const {
5946 bool Modified = false;
5947
5948 // 64 bit operations accept sign extended 32 bit immediates.
5949 // 32 bit operations accept all 32 bit immediates, so we don't need to check
5950 // them.
5951 const TargetRegisterClass *RC = nullptr;
5952 if (Reg.isVirtual())
5953 RC = MRI->getRegClass(Reg);
5954 if ((Reg.isPhysical() && X86::GR64RegClass.contains(Reg)) ||
5955 (Reg.isVirtual() && X86::GR64RegClass.hasSubClassEq(RC))) {
5956 if (!isInt<32>(ImmVal))
5957 return false;
5958 }
5959
5960 if (UseMI.findRegisterUseOperand(Reg, /*TRI=*/nullptr)->getSubReg())
5961 return false;
5962 // Immediate has larger code size than register. So avoid folding the
5963 // immediate if it has more than 1 use and we are optimizing for size.
5964 if (UseMI.getMF()->getFunction().hasOptSize() && Reg.isVirtual() &&
5965 !MRI->hasOneNonDBGUse(Reg))
5966 return false;
5967
5968 unsigned Opc = UseMI.getOpcode();
5969 unsigned NewOpc;
5970 if (Opc == TargetOpcode::COPY) {
5971 Register ToReg = UseMI.getOperand(0).getReg();
5972 const TargetRegisterClass *RC = nullptr;
5973 if (ToReg.isVirtual())
5974 RC = MRI->getRegClass(ToReg);
5975 bool GR32Reg = (ToReg.isVirtual() && X86::GR32RegClass.hasSubClassEq(RC)) ||
5976 (ToReg.isPhysical() && X86::GR32RegClass.contains(ToReg));
5977 bool GR64Reg = (ToReg.isVirtual() && X86::GR64RegClass.hasSubClassEq(RC)) ||
5978 (ToReg.isPhysical() && X86::GR64RegClass.contains(ToReg));
5979 bool GR8Reg = (ToReg.isVirtual() && X86::GR8RegClass.hasSubClassEq(RC)) ||
5980 (ToReg.isPhysical() && X86::GR8RegClass.contains(ToReg));
5981
5982 if (ImmVal == 0) {
5983 // We have MOV32r0 only.
5984 if (!GR32Reg)
5985 return false;
5986 }
5987
5988 if (GR64Reg) {
5989 if (isUInt<32>(ImmVal))
5990 NewOpc = X86::MOV32ri64;
5991 else
5992 NewOpc = X86::MOV64ri;
5993 } else if (GR32Reg) {
5994 NewOpc = X86::MOV32ri;
5995 if (ImmVal == 0) {
5996 // MOV32r0 clobbers EFLAGS.
5997 const TargetRegisterInfo *TRI = &getRegisterInfo();
5998 if (UseMI.getParent()->computeRegisterLiveness(
5999 TRI, X86::EFLAGS, UseMI) != MachineBasicBlock::LQR_Dead)
6000 return false;
6001
6002 // MOV32r0 is different than other cases because it doesn't encode the
6003 // immediate in the instruction. So we directly modify it here.
6004 if (!MakeChange)
6005 return true;
6006 UseMI.setDesc(get(X86::MOV32r0));
6007 UseMI.removeOperand(
6008 UseMI.findRegisterUseOperandIdx(Reg, /*TRI=*/nullptr));
6009 UseMI.addOperand(MachineOperand::CreateReg(X86::EFLAGS, /*isDef=*/true,
6010 /*isImp=*/true,
6011 /*isKill=*/false,
6012 /*isDead=*/true));
6013 Modified = true;
6014 }
6015 } else if (GR8Reg)
6016 NewOpc = X86::MOV8ri;
6017 else
6018 return false;
6019 } else
6020 NewOpc = convertALUrr2ALUri(Opc);
6021
6022 if (!NewOpc)
6023 return false;
6024
6025 // For SUB instructions the immediate can only be the second source operand.
6026 if ((NewOpc == X86::SUB64ri32 || NewOpc == X86::SUB32ri ||
6027 NewOpc == X86::SBB64ri32 || NewOpc == X86::SBB32ri ||
6028 NewOpc == X86::SUB64ri32_ND || NewOpc == X86::SUB32ri_ND ||
6029 NewOpc == X86::SBB64ri32_ND || NewOpc == X86::SBB32ri_ND) &&
6030 UseMI.findRegisterUseOperandIdx(Reg, /*TRI=*/nullptr) != 2)
6031 return false;
6032 // For CMP instructions the immediate can only be at index 1.
6033 if (((NewOpc == X86::CMP64ri32 || NewOpc == X86::CMP32ri) ||
6034 (NewOpc == X86::CCMP64ri32 || NewOpc == X86::CCMP32ri)) &&
6035 UseMI.findRegisterUseOperandIdx(Reg, /*TRI=*/nullptr) != 1)
6036 return false;
6037
6038 using namespace X86;
6039 if (isSHL(Opc) || isSHR(Opc) || isSAR(Opc) || isROL(Opc) || isROR(Opc) ||
6040 isRCL(Opc) || isRCR(Opc)) {
6041 unsigned RegIdx = UseMI.findRegisterUseOperandIdx(Reg, /*TRI=*/nullptr);
6042 if (RegIdx < 2)
6043 return false;
6044 if (!isInt<8>(ImmVal))
6045 return false;
6046 assert(Reg == X86::CL);
6047
6048 if (!MakeChange)
6049 return true;
6050 UseMI.setDesc(get(NewOpc));
6051 UseMI.removeOperand(RegIdx);
6052 UseMI.addOperand(MachineOperand::CreateImm(ImmVal));
6053 // Reg is physical register $cl, so we don't know if DefMI is dead through
6054 // MRI. Let the caller handle it, or pass dead-mi-elimination can delete
6055 // the dead physical register define instruction.
6056 return true;
6057 }
6058
6059 if (!MakeChange)
6060 return true;
6061
6062 if (!Modified) {
6063 // Modify the instruction.
6064 if (ImmVal == 0 && canConvert2Copy(NewOpc) &&
6065 UseMI.registerDefIsDead(X86::EFLAGS, /*TRI=*/nullptr)) {
6066 // %100 = add %101, 0
6067 // ==>
6068 // %100 = COPY %101
6069 UseMI.setDesc(get(TargetOpcode::COPY));
6070 UseMI.removeOperand(
6071 UseMI.findRegisterUseOperandIdx(Reg, /*TRI=*/nullptr));
6072 UseMI.removeOperand(
6073 UseMI.findRegisterDefOperandIdx(X86::EFLAGS, /*TRI=*/nullptr));
6074 UseMI.untieRegOperand(0);
6077 } else {
6078 unsigned Op1 = 1, Op2 = CommuteAnyOperandIndex;
6079 unsigned ImmOpNum = 2;
6080 if (!UseMI.getOperand(0).isDef()) {
6081 Op1 = 0; // TEST, CMP, CTEST, CCMP
6082 ImmOpNum = 1;
6083 }
6084 if (Opc == TargetOpcode::COPY)
6085 ImmOpNum = 1;
6086 if (findCommutedOpIndices(UseMI, Op1, Op2) &&
6087 UseMI.getOperand(Op1).getReg() == Reg)
6088 commuteInstruction(UseMI);
6089
6090 assert(UseMI.getOperand(ImmOpNum).getReg() == Reg);
6091 UseMI.setDesc(get(NewOpc));
6092 UseMI.getOperand(ImmOpNum).ChangeToImmediate(ImmVal);
6093 }
6094 }
6095
6096 if (Reg.isVirtual() && MRI->use_nodbg_empty(Reg))
6098
6099 return true;
6100}
6101
6102/// foldImmediate - 'Reg' is known to be defined by a move immediate
6103/// instruction, try to fold the immediate into the use instruction.
6105 Register Reg, MachineRegisterInfo *MRI) const {
6106 int64_t ImmVal;
6107 if (!getConstValDefinedInReg(DefMI, Reg, ImmVal))
6108 return false;
6109
6110 return foldImmediateImpl(UseMI, &DefMI, Reg, ImmVal, MRI, true);
6111}
6112
6113/// Expand a single-def pseudo instruction to a two-addr
6114/// instruction with two undef reads of the register being defined.
6115/// This is used for mapping:
6116/// %xmm4 = V_SET0
6117/// to:
6118/// %xmm4 = PXORrr undef %xmm4, undef %xmm4
6119///
6121 const MCInstrDesc &Desc) {
6122 assert(Desc.getNumOperands() == 3 && "Expected two-addr instruction.");
6123 Register Reg = MIB.getReg(0);
6124 MIB->setDesc(Desc);
6125
6126 // MachineInstr::addOperand() will insert explicit operands before any
6127 // implicit operands.
6129 // But we don't trust that.
6130 assert(MIB.getReg(1) == Reg && MIB.getReg(2) == Reg && "Misplaced operand");
6131 return true;
6132}
6133
6134/// Expand a single-def pseudo instruction to a two-addr
6135/// instruction with two %k0 reads.
6136/// This is used for mapping:
6137/// %k4 = K_SET1
6138/// to:
6139/// %k4 = KXNORrr %k0, %k0
6141 Register Reg) {
6142 assert(Desc.getNumOperands() == 3 && "Expected two-addr instruction.");
6143 MIB->setDesc(Desc);
6145 return true;
6146}
6147
6149 bool MinusOne) {
6150 MachineBasicBlock &MBB = *MIB->getParent();
6151 const DebugLoc &DL = MIB->getDebugLoc();
6152 Register Reg = MIB.getReg(0);
6153
6154 // Insert the XOR.
6155 BuildMI(MBB, MIB.getInstr(), DL, TII.get(X86::XOR32rr), Reg)
6158
6159 // Turn the pseudo into an INC or DEC.
6160 MIB->setDesc(TII.get(MinusOne ? X86::DEC32r : X86::INC32r));
6161 MIB.addReg(Reg);
6162
6163 return true;
6164}
6165
6167 const TargetInstrInfo &TII,
6168 const X86Subtarget &Subtarget) {
6169 MachineBasicBlock &MBB = *MIB->getParent();
6170 const DebugLoc &DL = MIB->getDebugLoc();
6171 int64_t Imm = MIB->getOperand(1).getImm();
6172 assert(Imm != 0 && "Using push/pop for 0 is not efficient.");
6174
6175 int StackAdjustment;
6176
6177 if (Subtarget.is64Bit()) {
6178 assert(MIB->getOpcode() == X86::MOV64ImmSExti8 ||
6179 MIB->getOpcode() == X86::MOV32ImmSExti8);
6180
6181 // Can't use push/pop lowering if the function might write to the red zone.
6182 X86MachineFunctionInfo *X86FI =
6183 MBB.getParent()->getInfo<X86MachineFunctionInfo>();
6184 if (X86FI->getUsesRedZone()) {
6185 MIB->setDesc(TII.get(MIB->getOpcode() == X86::MOV32ImmSExti8
6186 ? X86::MOV32ri
6187 : X86::MOV64ri));
6188 return true;
6189 }
6190
6191 // 64-bit mode doesn't have 32-bit push/pop, so use 64-bit operations and
6192 // widen the register if necessary.
6193 StackAdjustment = 8;
6194 BuildMI(MBB, I, DL, TII.get(X86::PUSH64i32)).addImm(Imm);
6195 MIB->setDesc(TII.get(X86::POP64r));
6196 MIB->getOperand(0).setReg(getX86SubSuperRegister(MIB.getReg(0), 64));
6197 } else {
6198 assert(MIB->getOpcode() == X86::MOV32ImmSExti8);
6199 StackAdjustment = 4;
6200 BuildMI(MBB, I, DL, TII.get(X86::PUSH32i)).addImm(Imm);
6201 MIB->setDesc(TII.get(X86::POP32r));
6202 }
6203 MIB->removeOperand(1);
6204 MIB->addImplicitDefUseOperands(*MBB.getParent());
6205
6206 // Build CFI if necessary.
6207 MachineFunction &MF = *MBB.getParent();
6208 const X86FrameLowering *TFL = Subtarget.getFrameLowering();
6209 bool IsWin64Prologue = MF.getTarget().getMCAsmInfo().usesWindowsCFI();
6210 bool NeedsDwarfCFI = !IsWin64Prologue && MF.needsFrameMoves();
6211 bool EmitCFI = !TFL->hasFP(MF) && NeedsDwarfCFI;
6212 if (EmitCFI) {
6213 TFL->BuildCFI(
6214 MBB, I, DL,
6215 MCCFIInstruction::createAdjustCfaOffset(nullptr, StackAdjustment));
6216 TFL->BuildCFI(
6217 MBB, std::next(I), DL,
6218 MCCFIInstruction::createAdjustCfaOffset(nullptr, -StackAdjustment));
6219 }
6220
6221 return true;
6222}
6223
6224// LoadStackGuard has so far only been implemented for 64-bit MachO. Different
6225// code sequence is needed for other targets.
6227 const TargetInstrInfo &TII) {
6228 MachineBasicBlock &MBB = *MIB->getParent();
6229 const DebugLoc &DL = MIB->getDebugLoc();
6230 Register Reg = MIB.getReg(0);
6231 const GlobalValue *GV =
6232 cast<GlobalValue>((*MIB->memoperands_begin())->getValue());
6233 auto Flags = MachineMemOperand::MOLoad |
6236 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(
6237 MachinePointerInfo::getGOT(*MBB.getParent()), Flags, 8, Align(8));
6239
6240 BuildMI(MBB, I, DL, TII.get(X86::MOV64rm), Reg)
6241 .addReg(X86::RIP)
6242 .addImm(1)
6243 .addReg(0)
6245 .addReg(0)
6246 .addMemOperand(MMO);
6247 MIB->setDebugLoc(DL);
6248 MIB->setDesc(TII.get(X86::MOV64rm));
6250}
6251
6253 MachineBasicBlock &MBB = *MIB->getParent();
6254 MachineFunction &MF = *MBB.getParent();
6255 const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>();
6256 const X86RegisterInfo *TRI = Subtarget.getRegisterInfo();
6257 unsigned XorOp =
6258 MIB->getOpcode() == X86::XOR64_FP ? X86::XOR64rr : X86::XOR32rr;
6259 MIB->setDesc(TII.get(XorOp));
6260 MIB.addReg(TRI->getFrameRegister(MF), RegState::Undef);
6261 return true;
6262}
6263
6264// This is used to handle spills for 128/256-bit registers when we have AVX512,
6265// but not VLX. If it uses an extended register we need to use an instruction
6266// that loads the lower 128/256-bit, but is available with only AVX512F.
6268 const TargetRegisterInfo *TRI,
6269 const MCInstrDesc &LoadDesc,
6270 const MCInstrDesc &BroadcastDesc, unsigned SubIdx) {
6271 Register DestReg = MIB.getReg(0);
6272 // Check if DestReg is XMM16-31 or YMM16-31.
6273 if (TRI->getEncodingValue(DestReg) < 16) {
6274 // We can use a normal VEX encoded load.
6275 MIB->setDesc(LoadDesc);
6276 } else {
6277 // Use a 128/256-bit VBROADCAST instruction.
6278 MIB->setDesc(BroadcastDesc);
6279 // Change the destination to a 512-bit register.
6280 DestReg = TRI->getMatchingSuperReg(DestReg, SubIdx, &X86::VR512RegClass);
6281 MIB->getOperand(0).setReg(DestReg);
6282 }
6283 return true;
6284}
6285
6286// This is used to handle spills for 128/256-bit registers when we have AVX512,
6287// but not VLX. If it uses an extended register we need to use an instruction
6288// that stores the lower 128/256-bit, but is available with only AVX512F.
6290 const TargetRegisterInfo *TRI,
6291 const MCInstrDesc &StoreDesc,
6292 const MCInstrDesc &ExtractDesc, unsigned SubIdx) {
6293 Register SrcReg = MIB.getReg(X86::AddrNumOperands);
6294 // Check if DestReg is XMM16-31 or YMM16-31.
6295 if (TRI->getEncodingValue(SrcReg) < 16) {
6296 // We can use a normal VEX encoded store.
6297 MIB->setDesc(StoreDesc);
6298 } else {
6299 // Use a VEXTRACTF instruction.
6300 MIB->setDesc(ExtractDesc);
6301 // Change the destination to a 512-bit register.
6302 SrcReg = TRI->getMatchingSuperReg(SrcReg, SubIdx, &X86::VR512RegClass);
6304 MIB.addImm(0x0); // Append immediate to extract from the lower bits.
6305 }
6306
6307 return true;
6308}
6309
6311 MIB->setDesc(Desc);
6312 int64_t ShiftAmt = MIB->getOperand(2).getImm();
6313 // Temporarily remove the immediate so we can add another source register.
6314 MIB->removeOperand(2);
6315 // Add the register. Don't copy the kill flag if there is one.
6316 MIB.addReg(MIB.getReg(1), getUndefRegState(MIB->getOperand(1).isUndef()));
6317 // Add back the immediate.
6318 MIB.addImm(ShiftAmt);
6319 return true;
6320}
6321
6323 const TargetInstrInfo &TII, bool HasAVX) {
6324 unsigned NewOpc;
6325 if (MI.getOpcode() == X86::MOVSHPrm) {
6326 NewOpc = HasAVX ? X86::VMOVSSrm : X86::MOVSSrm;
6327 Register Reg = MI.getOperand(0).getReg();
6328 if (Reg > X86::XMM15)
6329 NewOpc = X86::VMOVSSZrm;
6330 } else {
6331 NewOpc = HasAVX ? X86::VMOVSSmr : X86::MOVSSmr;
6332 Register Reg = MI.getOperand(5).getReg();
6333 if (Reg > X86::XMM15)
6334 NewOpc = X86::VMOVSSZmr;
6335 }
6336
6337 MIB->setDesc(TII.get(NewOpc));
6338 return true;
6339}
6340
6342 bool HasAVX = Subtarget.hasAVX();
6343 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
6344 switch (MI.getOpcode()) {
6345 case X86::MOV32r0:
6346 return Expand2AddrUndef(MIB, get(X86::XOR32rr));
6347 case X86::MOV32r1:
6348 return expandMOV32r1(MIB, *this, /*MinusOne=*/false);
6349 case X86::MOV32r_1:
6350 return expandMOV32r1(MIB, *this, /*MinusOne=*/true);
6351 case X86::MOV32ImmSExti8:
6352 case X86::MOV64ImmSExti8:
6353 return ExpandMOVImmSExti8(MIB, *this, Subtarget);
6354 case X86::SETB_C32r:
6355 return Expand2AddrUndef(MIB, get(X86::SBB32rr));
6356 case X86::SETB_C64r:
6357 return Expand2AddrUndef(MIB, get(X86::SBB64rr));
6358 case X86::MMX_SET0:
6359 return Expand2AddrUndef(MIB, get(X86::MMX_PXORrr));
6360 case X86::V_SET0:
6361 case X86::FsFLD0SS:
6362 case X86::FsFLD0SD:
6363 case X86::FsFLD0SH:
6364 case X86::FsFLD0F128:
6365 return Expand2AddrUndef(MIB, get(HasAVX ? X86::VXORPSrr : X86::XORPSrr));
6366 case X86::AVX512_128_SET0:
6367 case X86::AVX512_FsFLD0SH:
6368 case X86::AVX512_FsFLD0SS:
6369 case X86::AVX512_FsFLD0SD:
6370 case X86::AVX512_FsFLD0F128: {
6371 bool HasVLX = Subtarget.hasVLX();
6372 Register SrcReg = MIB.getReg(0);
6374 if (HasVLX || TRI->getEncodingValue(SrcReg) < 16)
6375 return Expand2AddrUndef(MIB,
6376 get(HasVLX ? X86::VPXORDZ128rr : X86::VXORPSrr));
6377 // Extended register without VLX. Use a larger XOR.
6378 SrcReg =
6379 TRI->getMatchingSuperReg(SrcReg, X86::sub_xmm, &X86::VR512RegClass);
6380 MIB->getOperand(0).setReg(SrcReg);
6381 return Expand2AddrUndef(MIB, get(X86::VPXORDZrr));
6382 }
6383 case X86::MOVSHPmr:
6384 case X86::MOVSHPrm:
6385 return expandMOVSHP(MIB, MI, *this, Subtarget.hasAVX());
6386 case X86::V_SETALLONES:
6387 return Expand2AddrUndef(MIB,
6388 get(HasAVX ? X86::VPCMPEQDrr : X86::PCMPEQDrr));
6389 case X86::AVX2_SETALLONES:
6390 return Expand2AddrUndef(MIB, get(X86::VPCMPEQDYrr));
6391 case X86::AVX1_SETALLONES: {
6392 Register Reg = MIB.getReg(0);
6393 // VCMPPSYrri with an immediate 0xf should produce VCMPTRUEPS.
6394 MIB->setDesc(get(X86::VCMPPSYrri));
6395 MIB.addReg(Reg, RegState::Undef).addReg(Reg, RegState::Undef).addImm(0xf);
6396 return true;
6397 }
6398 case X86::AVX512_128_SETALLONES:
6399 case X86::AVX512_256_SETALLONES:
6400 case X86::AVX512_512_SETALLONES: {
6401 Register Reg = MIB.getReg(0);
6402 unsigned Opc;
6403 switch (MI.getOpcode()) {
6404 case X86::AVX512_128_SETALLONES: {
6405 if (X86::VR128RegClass.contains(Reg))
6406 return Expand2AddrUndef(MIB, get(X86::VPCMPEQDrr));
6407
6408 Opc = X86::VPTERNLOGDZ128rri;
6409 break;
6410 }
6411 case X86::AVX512_256_SETALLONES: {
6412 if (X86::VR256RegClass.contains(Reg))
6413 return Expand2AddrUndef(MIB, get(X86::VPCMPEQDYrr));
6414
6415 Opc = X86::VPTERNLOGDZ256rri;
6416 break;
6417 }
6418 case X86::AVX512_512_SETALLONES:
6419 Opc = X86::VPTERNLOGDZrri;
6420 break;
6421 }
6422 MIB->setDesc(get(Opc));
6423 // VPTERNLOGD needs 3 register inputs and an immediate.
6424 // 0xff will return 1s for any input.
6425 MIB.addReg(Reg, RegState::Undef)
6426 .addReg(Reg, RegState::Undef)
6427 .addReg(Reg, RegState::Undef)
6428 .addImm(0xff);
6429 return true;
6430 }
6431 case X86::AVX512_512_SEXT_MASK_32:
6432 case X86::AVX512_512_SEXT_MASK_64: {
6433 Register Reg = MIB.getReg(0);
6434 Register MaskReg = MIB.getReg(1);
6435 RegState MaskState = getRegState(MIB->getOperand(1));
6436 unsigned Opc = (MI.getOpcode() == X86::AVX512_512_SEXT_MASK_64)
6437 ? X86::VPTERNLOGQZrrikz
6438 : X86::VPTERNLOGDZrrikz;
6439 MI.removeOperand(1);
6440 MIB->setDesc(get(Opc));
6441 // VPTERNLOG needs 3 register inputs and an immediate.
6442 // 0xff will return 1s for any input.
6443 MIB.addReg(Reg, RegState::Undef)
6444 .addReg(MaskReg, MaskState)
6445 .addReg(Reg, RegState::Undef)
6446 .addReg(Reg, RegState::Undef)
6447 .addImm(0xff);
6448 return true;
6449 }
6450 case X86::VMOVAPSZ128rm_NOVLX:
6451 return expandNOVLXLoad(MIB, &getRegisterInfo(), get(X86::VMOVAPSrm),
6452 get(X86::VBROADCASTF32X4Zrm), X86::sub_xmm);
6453 case X86::VMOVUPSZ128rm_NOVLX:
6454 return expandNOVLXLoad(MIB, &getRegisterInfo(), get(X86::VMOVUPSrm),
6455 get(X86::VBROADCASTF32X4Zrm), X86::sub_xmm);
6456 case X86::VMOVAPSZ256rm_NOVLX:
6457 return expandNOVLXLoad(MIB, &getRegisterInfo(), get(X86::VMOVAPSYrm),
6458 get(X86::VBROADCASTF64X4Zrm), X86::sub_ymm);
6459 case X86::VMOVUPSZ256rm_NOVLX:
6460 return expandNOVLXLoad(MIB, &getRegisterInfo(), get(X86::VMOVUPSYrm),
6461 get(X86::VBROADCASTF64X4Zrm), X86::sub_ymm);
6462 case X86::VMOVAPSZ128mr_NOVLX:
6463 return expandNOVLXStore(MIB, &getRegisterInfo(), get(X86::VMOVAPSmr),
6464 get(X86::VEXTRACTF32X4Zmri), X86::sub_xmm);
6465 case X86::VMOVUPSZ128mr_NOVLX:
6466 return expandNOVLXStore(MIB, &getRegisterInfo(), get(X86::VMOVUPSmr),
6467 get(X86::VEXTRACTF32X4Zmri), X86::sub_xmm);
6468 case X86::VMOVAPSZ256mr_NOVLX:
6469 return expandNOVLXStore(MIB, &getRegisterInfo(), get(X86::VMOVAPSYmr),
6470 get(X86::VEXTRACTF64X4Zmri), X86::sub_ymm);
6471 case X86::VMOVUPSZ256mr_NOVLX:
6472 return expandNOVLXStore(MIB, &getRegisterInfo(), get(X86::VMOVUPSYmr),
6473 get(X86::VEXTRACTF64X4Zmri), X86::sub_ymm);
6474 case X86::MOV32ri64: {
6475 Register Reg = MIB.getReg(0);
6476 Register Reg32 = RI.getSubReg(Reg, X86::sub_32bit);
6477 MI.setDesc(get(X86::MOV32ri));
6478 MIB->getOperand(0).setReg(Reg32);
6480 return true;
6481 }
6482
6483 case X86::RDFLAGS32:
6484 case X86::RDFLAGS64: {
6485 unsigned Is64Bit = MI.getOpcode() == X86::RDFLAGS64;
6486 MachineBasicBlock &MBB = *MIB->getParent();
6487
6488 MachineInstr *NewMI = BuildMI(MBB, MI, MIB->getDebugLoc(),
6489 get(Is64Bit ? X86::PUSHF64 : X86::PUSHF32))
6490 .getInstr();
6491
6492 // Permit reads of the EFLAGS and DF registers without them being defined.
6493 // This intrinsic exists to read external processor state in flags, such as
6494 // the trap flag, interrupt flag, and direction flag, none of which are
6495 // modeled by the backend.
6496 assert(NewMI->getOperand(2).getReg() == X86::EFLAGS &&
6497 "Unexpected register in operand! Should be EFLAGS.");
6498 NewMI->getOperand(2).setIsUndef();
6499 assert(NewMI->getOperand(3).getReg() == X86::DF &&
6500 "Unexpected register in operand! Should be DF.");
6501 NewMI->getOperand(3).setIsUndef();
6502
6503 MIB->setDesc(get(Is64Bit ? X86::POP64r : X86::POP32r));
6504 return true;
6505 }
6506
6507 case X86::WRFLAGS32:
6508 case X86::WRFLAGS64: {
6509 unsigned Is64Bit = MI.getOpcode() == X86::WRFLAGS64;
6510 MachineBasicBlock &MBB = *MIB->getParent();
6511
6512 BuildMI(MBB, MI, MIB->getDebugLoc(),
6513 get(Is64Bit ? X86::PUSH64r : X86::PUSH32r))
6514 .addReg(MI.getOperand(0).getReg());
6515 BuildMI(MBB, MI, MIB->getDebugLoc(),
6516 get(Is64Bit ? X86::POPF64 : X86::POPF32));
6517 MI.eraseFromParent();
6518 return true;
6519 }
6520
6521 // KNL does not recognize dependency-breaking idioms for mask registers,
6522 // so kxnor %k1, %k1, %k2 has a RAW dependence on %k1.
6523 // Using %k0 as the undef input register is a performance heuristic based
6524 // on the assumption that %k0 is used less frequently than the other mask
6525 // registers, since it is not usable as a write mask.
6526 // FIXME: A more advanced approach would be to choose the best input mask
6527 // register based on context.
6528 case X86::KSET0B:
6529 return Expand2AddrKreg(MIB, get(X86::KXORBkk), X86::K0);
6530 case X86::KSET0W:
6531 return Expand2AddrKreg(MIB, get(X86::KXORWkk), X86::K0);
6532 case X86::KSET0D:
6533 return Expand2AddrKreg(MIB, get(X86::KXORDkk), X86::K0);
6534 case X86::KSET0Q:
6535 return Expand2AddrKreg(MIB, get(X86::KXORQkk), X86::K0);
6536 case X86::KSET1B:
6537 return Expand2AddrKreg(MIB, get(X86::KXNORBkk), X86::K0);
6538 case X86::KSET1W:
6539 return Expand2AddrKreg(MIB, get(X86::KXNORWkk), X86::K0);
6540 case X86::KSET1D:
6541 return Expand2AddrKreg(MIB, get(X86::KXNORDkk), X86::K0);
6542 case X86::KSET1Q:
6543 return Expand2AddrKreg(MIB, get(X86::KXNORQkk), X86::K0);
6544 case TargetOpcode::LOAD_STACK_GUARD:
6545 expandLoadStackGuard(MIB, *this);
6546 return true;
6547 case X86::XOR64_FP:
6548 case X86::XOR32_FP:
6549 return expandXorFP(MIB, *this);
6550 case X86::SHLDROT32ri:
6551 return expandSHXDROT(MIB, get(X86::SHLD32rri8));
6552 case X86::SHLDROT64ri:
6553 return expandSHXDROT(MIB, get(X86::SHLD64rri8));
6554 case X86::SHRDROT32ri:
6555 return expandSHXDROT(MIB, get(X86::SHRD32rri8));
6556 case X86::SHRDROT64ri:
6557 return expandSHXDROT(MIB, get(X86::SHRD64rri8));
6558 case X86::ADD8rr_DB:
6559 MIB->setDesc(get(X86::OR8rr));
6560 break;
6561 case X86::ADD16rr_DB:
6562 MIB->setDesc(get(X86::OR16rr));
6563 break;
6564 case X86::ADD32rr_DB:
6565 MIB->setDesc(get(X86::OR32rr));
6566 break;
6567 case X86::ADD64rr_DB:
6568 MIB->setDesc(get(X86::OR64rr));
6569 break;
6570 case X86::ADD8ri_DB:
6571 MIB->setDesc(get(X86::OR8ri));
6572 break;
6573 case X86::ADD16ri_DB:
6574 MIB->setDesc(get(X86::OR16ri));
6575 break;
6576 case X86::ADD32ri_DB:
6577 MIB->setDesc(get(X86::OR32ri));
6578 break;
6579 case X86::ADD64ri32_DB:
6580 MIB->setDesc(get(X86::OR64ri32));
6581 break;
6582 }
6583 return false;
6584}
6585
6586/// Return true for all instructions that only update
6587/// the first 32 or 64-bits of the destination register and leave the rest
6588/// unmodified. This can be used to avoid folding loads if the instructions
6589/// only update part of the destination register, and the non-updated part is
6590/// not needed. e.g. cvtss2sd, sqrtss. Unfolding the load from these
6591/// instructions breaks the partial register dependency and it can improve
6592/// performance. e.g.:
6593///
6594/// movss (%rdi), %xmm0
6595/// cvtss2sd %xmm0, %xmm0
6596///
6597/// Instead of
6598/// cvtss2sd (%rdi), %xmm0
6599///
6600/// FIXME: This should be turned into a TSFlags.
6601///
6602static bool hasPartialRegUpdate(unsigned Opcode, const X86Subtarget &Subtarget,
6603 bool ForLoadFold = false) {
6604 switch (Opcode) {
6605 case X86::CVTSI2SSrr:
6606 case X86::CVTSI2SSrm:
6607 case X86::CVTSI642SSrr:
6608 case X86::CVTSI642SSrm:
6609 case X86::CVTSI2SDrr:
6610 case X86::CVTSI2SDrm:
6611 case X86::CVTSI642SDrr:
6612 case X86::CVTSI642SDrm:
6613 // Load folding won't effect the undef register update since the input is
6614 // a GPR.
6615 return !ForLoadFold;
6616 case X86::CVTSD2SSrr:
6617 case X86::CVTSD2SSrm:
6618 case X86::CVTSS2SDrr:
6619 case X86::CVTSS2SDrm:
6620 case X86::MOVHPDrm:
6621 case X86::MOVHPSrm:
6622 case X86::MOVLPDrm:
6623 case X86::MOVLPSrm:
6624 case X86::RCPSSr:
6625 case X86::RCPSSm:
6626 case X86::RCPSSr_Int:
6627 case X86::RCPSSm_Int:
6628 case X86::ROUNDSDri:
6629 case X86::ROUNDSDmi:
6630 case X86::ROUNDSSri:
6631 case X86::ROUNDSSmi:
6632 case X86::RSQRTSSr:
6633 case X86::RSQRTSSm:
6634 case X86::RSQRTSSr_Int:
6635 case X86::RSQRTSSm_Int:
6636 case X86::SQRTSSr:
6637 case X86::SQRTSSm:
6638 case X86::SQRTSSr_Int:
6639 case X86::SQRTSSm_Int:
6640 case X86::SQRTSDr:
6641 case X86::SQRTSDm:
6642 case X86::SQRTSDr_Int:
6643 case X86::SQRTSDm_Int:
6644 return true;
6645 case X86::VFCMULCPHZ128rm:
6646 case X86::VFCMULCPHZ128rmb:
6647 case X86::VFCMULCPHZ128rmbkz:
6648 case X86::VFCMULCPHZ128rmkz:
6649 case X86::VFCMULCPHZ128rr:
6650 case X86::VFCMULCPHZ128rrkz:
6651 case X86::VFCMULCPHZ256rm:
6652 case X86::VFCMULCPHZ256rmb:
6653 case X86::VFCMULCPHZ256rmbkz:
6654 case X86::VFCMULCPHZ256rmkz:
6655 case X86::VFCMULCPHZ256rr:
6656 case X86::VFCMULCPHZ256rrkz:
6657 case X86::VFCMULCPHZrm:
6658 case X86::VFCMULCPHZrmb:
6659 case X86::VFCMULCPHZrmbkz:
6660 case X86::VFCMULCPHZrmkz:
6661 case X86::VFCMULCPHZrr:
6662 case X86::VFCMULCPHZrrb:
6663 case X86::VFCMULCPHZrrbkz:
6664 case X86::VFCMULCPHZrrkz:
6665 case X86::VFMULCPHZ128rm:
6666 case X86::VFMULCPHZ128rmb:
6667 case X86::VFMULCPHZ128rmbkz:
6668 case X86::VFMULCPHZ128rmkz:
6669 case X86::VFMULCPHZ128rr:
6670 case X86::VFMULCPHZ128rrkz:
6671 case X86::VFMULCPHZ256rm:
6672 case X86::VFMULCPHZ256rmb:
6673 case X86::VFMULCPHZ256rmbkz:
6674 case X86::VFMULCPHZ256rmkz:
6675 case X86::VFMULCPHZ256rr:
6676 case X86::VFMULCPHZ256rrkz:
6677 case X86::VFMULCPHZrm:
6678 case X86::VFMULCPHZrmb:
6679 case X86::VFMULCPHZrmbkz:
6680 case X86::VFMULCPHZrmkz:
6681 case X86::VFMULCPHZrr:
6682 case X86::VFMULCPHZrrb:
6683 case X86::VFMULCPHZrrbkz:
6684 case X86::VFMULCPHZrrkz:
6685 case X86::VFCMULCSHZrm:
6686 case X86::VFCMULCSHZrmkz:
6687 case X86::VFCMULCSHZrr:
6688 case X86::VFCMULCSHZrrb:
6689 case X86::VFCMULCSHZrrbkz:
6690 case X86::VFCMULCSHZrrkz:
6691 case X86::VFMULCSHZrm:
6692 case X86::VFMULCSHZrmkz:
6693 case X86::VFMULCSHZrr:
6694 case X86::VFMULCSHZrrb:
6695 case X86::VFMULCSHZrrbkz:
6696 case X86::VFMULCSHZrrkz:
6697 return Subtarget.hasMULCFalseDeps();
6698 case X86::VPERMDYrm:
6699 case X86::VPERMDYrr:
6700 case X86::VPERMQYmi:
6701 case X86::VPERMQYri:
6702 case X86::VPERMPSYrm:
6703 case X86::VPERMPSYrr:
6704 case X86::VPERMPDYmi:
6705 case X86::VPERMPDYri:
6706 case X86::VPERMDZ256rm:
6707 case X86::VPERMDZ256rmb:
6708 case X86::VPERMDZ256rmbkz:
6709 case X86::VPERMDZ256rmkz:
6710 case X86::VPERMDZ256rr:
6711 case X86::VPERMDZ256rrkz:
6712 case X86::VPERMDZrm:
6713 case X86::VPERMDZrmb:
6714 case X86::VPERMDZrmbkz:
6715 case X86::VPERMDZrmkz:
6716 case X86::VPERMDZrr:
6717 case X86::VPERMDZrrkz:
6718 case X86::VPERMQZ256mbi:
6719 case X86::VPERMQZ256mbikz:
6720 case X86::VPERMQZ256mi:
6721 case X86::VPERMQZ256mikz:
6722 case X86::VPERMQZ256ri:
6723 case X86::VPERMQZ256rikz:
6724 case X86::VPERMQZ256rm:
6725 case X86::VPERMQZ256rmb:
6726 case X86::VPERMQZ256rmbkz:
6727 case X86::VPERMQZ256rmkz:
6728 case X86::VPERMQZ256rr:
6729 case X86::VPERMQZ256rrkz:
6730 case X86::VPERMQZmbi:
6731 case X86::VPERMQZmbikz:
6732 case X86::VPERMQZmi:
6733 case X86::VPERMQZmikz:
6734 case X86::VPERMQZri:
6735 case X86::VPERMQZrikz:
6736 case X86::VPERMQZrm:
6737 case X86::VPERMQZrmb:
6738 case X86::VPERMQZrmbkz:
6739 case X86::VPERMQZrmkz:
6740 case X86::VPERMQZrr:
6741 case X86::VPERMQZrrkz:
6742 case X86::VPERMPSZ256rm:
6743 case X86::VPERMPSZ256rmb:
6744 case X86::VPERMPSZ256rmbkz:
6745 case X86::VPERMPSZ256rmkz:
6746 case X86::VPERMPSZ256rr:
6747 case X86::VPERMPSZ256rrkz:
6748 case X86::VPERMPSZrm:
6749 case X86::VPERMPSZrmb:
6750 case X86::VPERMPSZrmbkz:
6751 case X86::VPERMPSZrmkz:
6752 case X86::VPERMPSZrr:
6753 case X86::VPERMPSZrrkz:
6754 case X86::VPERMPDZ256mbi:
6755 case X86::VPERMPDZ256mbikz:
6756 case X86::VPERMPDZ256mi:
6757 case X86::VPERMPDZ256mikz:
6758 case X86::VPERMPDZ256ri:
6759 case X86::VPERMPDZ256rikz:
6760 case X86::VPERMPDZ256rm:
6761 case X86::VPERMPDZ256rmb:
6762 case X86::VPERMPDZ256rmbkz:
6763 case X86::VPERMPDZ256rmkz:
6764 case X86::VPERMPDZ256rr:
6765 case X86::VPERMPDZ256rrkz:
6766 case X86::VPERMPDZmbi:
6767 case X86::VPERMPDZmbikz:
6768 case X86::VPERMPDZmi:
6769 case X86::VPERMPDZmikz:
6770 case X86::VPERMPDZri:
6771 case X86::VPERMPDZrikz:
6772 case X86::VPERMPDZrm:
6773 case X86::VPERMPDZrmb:
6774 case X86::VPERMPDZrmbkz:
6775 case X86::VPERMPDZrmkz:
6776 case X86::VPERMPDZrr:
6777 case X86::VPERMPDZrrkz:
6778 return Subtarget.hasPERMFalseDeps();
6779 case X86::VRANGEPDZ128rmbi:
6780 case X86::VRANGEPDZ128rmbikz:
6781 case X86::VRANGEPDZ128rmi:
6782 case X86::VRANGEPDZ128rmikz:
6783 case X86::VRANGEPDZ128rri:
6784 case X86::VRANGEPDZ128rrikz:
6785 case X86::VRANGEPDZ256rmbi:
6786 case X86::VRANGEPDZ256rmbikz:
6787 case X86::VRANGEPDZ256rmi:
6788 case X86::VRANGEPDZ256rmikz:
6789 case X86::VRANGEPDZ256rri:
6790 case X86::VRANGEPDZ256rrikz:
6791 case X86::VRANGEPDZrmbi:
6792 case X86::VRANGEPDZrmbikz:
6793 case X86::VRANGEPDZrmi:
6794 case X86::VRANGEPDZrmikz:
6795 case X86::VRANGEPDZrri:
6796 case X86::VRANGEPDZrrib:
6797 case X86::VRANGEPDZrribkz:
6798 case X86::VRANGEPDZrrikz:
6799 case X86::VRANGEPSZ128rmbi:
6800 case X86::VRANGEPSZ128rmbikz:
6801 case X86::VRANGEPSZ128rmi:
6802 case X86::VRANGEPSZ128rmikz:
6803 case X86::VRANGEPSZ128rri:
6804 case X86::VRANGEPSZ128rrikz:
6805 case X86::VRANGEPSZ256rmbi:
6806 case X86::VRANGEPSZ256rmbikz:
6807 case X86::VRANGEPSZ256rmi:
6808 case X86::VRANGEPSZ256rmikz:
6809 case X86::VRANGEPSZ256rri:
6810 case X86::VRANGEPSZ256rrikz:
6811 case X86::VRANGEPSZrmbi:
6812 case X86::VRANGEPSZrmbikz:
6813 case X86::VRANGEPSZrmi:
6814 case X86::VRANGEPSZrmikz:
6815 case X86::VRANGEPSZrri:
6816 case X86::VRANGEPSZrrib:
6817 case X86::VRANGEPSZrribkz:
6818 case X86::VRANGEPSZrrikz:
6819 case X86::VRANGESDZrmi:
6820 case X86::VRANGESDZrmikz:
6821 case X86::VRANGESDZrri:
6822 case X86::VRANGESDZrrib:
6823 case X86::VRANGESDZrribkz:
6824 case X86::VRANGESDZrrikz:
6825 case X86::VRANGESSZrmi:
6826 case X86::VRANGESSZrmikz:
6827 case X86::VRANGESSZrri:
6828 case X86::VRANGESSZrrib:
6829 case X86::VRANGESSZrribkz:
6830 case X86::VRANGESSZrrikz:
6831 return Subtarget.hasRANGEFalseDeps();
6832 case X86::VGETMANTSSZrmi:
6833 case X86::VGETMANTSSZrmikz:
6834 case X86::VGETMANTSSZrri:
6835 case X86::VGETMANTSSZrrib:
6836 case X86::VGETMANTSSZrribkz:
6837 case X86::VGETMANTSSZrrikz:
6838 case X86::VGETMANTSDZrmi:
6839 case X86::VGETMANTSDZrmikz:
6840 case X86::VGETMANTSDZrri:
6841 case X86::VGETMANTSDZrrib:
6842 case X86::VGETMANTSDZrribkz:
6843 case X86::VGETMANTSDZrrikz:
6844 case X86::VGETMANTSHZrmi:
6845 case X86::VGETMANTSHZrmikz:
6846 case X86::VGETMANTSHZrri:
6847 case X86::VGETMANTSHZrrib:
6848 case X86::VGETMANTSHZrribkz:
6849 case X86::VGETMANTSHZrrikz:
6850 case X86::VGETMANTPSZ128rmbi:
6851 case X86::VGETMANTPSZ128rmbikz:
6852 case X86::VGETMANTPSZ128rmi:
6853 case X86::VGETMANTPSZ128rmikz:
6854 case X86::VGETMANTPSZ256rmbi:
6855 case X86::VGETMANTPSZ256rmbikz:
6856 case X86::VGETMANTPSZ256rmi:
6857 case X86::VGETMANTPSZ256rmikz:
6858 case X86::VGETMANTPSZrmbi:
6859 case X86::VGETMANTPSZrmbikz:
6860 case X86::VGETMANTPSZrmi:
6861 case X86::VGETMANTPSZrmikz:
6862 case X86::VGETMANTPDZ128rmbi:
6863 case X86::VGETMANTPDZ128rmbikz:
6864 case X86::VGETMANTPDZ128rmi:
6865 case X86::VGETMANTPDZ128rmikz:
6866 case X86::VGETMANTPDZ256rmbi:
6867 case X86::VGETMANTPDZ256rmbikz:
6868 case X86::VGETMANTPDZ256rmi:
6869 case X86::VGETMANTPDZ256rmikz:
6870 case X86::VGETMANTPDZrmbi:
6871 case X86::VGETMANTPDZrmbikz:
6872 case X86::VGETMANTPDZrmi:
6873 case X86::VGETMANTPDZrmikz:
6874 return Subtarget.hasGETMANTFalseDeps();
6875 case X86::VPMULLQZ128rm:
6876 case X86::VPMULLQZ128rmb:
6877 case X86::VPMULLQZ128rmbkz:
6878 case X86::VPMULLQZ128rmkz:
6879 case X86::VPMULLQZ128rr:
6880 case X86::VPMULLQZ128rrkz:
6881 case X86::VPMULLQZ256rm:
6882 case X86::VPMULLQZ256rmb:
6883 case X86::VPMULLQZ256rmbkz:
6884 case X86::VPMULLQZ256rmkz:
6885 case X86::VPMULLQZ256rr:
6886 case X86::VPMULLQZ256rrkz:
6887 case X86::VPMULLQZrm:
6888 case X86::VPMULLQZrmb:
6889 case X86::VPMULLQZrmbkz:
6890 case X86::VPMULLQZrmkz:
6891 case X86::VPMULLQZrr:
6892 case X86::VPMULLQZrrkz:
6893 return Subtarget.hasMULLQFalseDeps();
6894 case X86::VPCOMPRESSBZ128rrkz:
6895 case X86::VPCOMPRESSBZ256rrkz:
6896 case X86::VPCOMPRESSBZrrkz:
6897 case X86::VPCOMPRESSWZ128rrkz:
6898 case X86::VPCOMPRESSWZ256rrkz:
6899 case X86::VPCOMPRESSWZrrkz:
6900 case X86::VPCOMPRESSDZ128rrkz:
6901 case X86::VPCOMPRESSDZ256rrkz:
6902 case X86::VPCOMPRESSDZrrkz:
6903 case X86::VPCOMPRESSQZ128rrkz:
6904 case X86::VPCOMPRESSQZ256rrkz:
6905 case X86::VPCOMPRESSQZrrkz:
6906 case X86::VCOMPRESSPSZ128rrkz:
6907 case X86::VCOMPRESSPSZ256rrkz:
6908 case X86::VCOMPRESSPSZrrkz:
6909 case X86::VCOMPRESSPDZ128rrkz:
6910 case X86::VCOMPRESSPDZ256rrkz:
6911 case X86::VCOMPRESSPDZrrkz:
6912 return Subtarget.hasCOMPRESSFalseDeps();
6913 case X86::VPEXPANDBZ128rmkz:
6914 case X86::VPEXPANDBZ128rrkz:
6915 case X86::VPEXPANDBZ256rmkz:
6916 case X86::VPEXPANDBZ256rrkz:
6917 case X86::VPEXPANDBZrmkz:
6918 case X86::VPEXPANDBZrrkz:
6919 case X86::VPEXPANDWZ128rmkz:
6920 case X86::VPEXPANDWZ128rrkz:
6921 case X86::VPEXPANDWZ256rmkz:
6922 case X86::VPEXPANDWZ256rrkz:
6923 case X86::VPEXPANDWZrmkz:
6924 case X86::VPEXPANDWZrrkz:
6925 case X86::VPEXPANDDZ128rmkz:
6926 case X86::VPEXPANDDZ128rrkz:
6927 case X86::VPEXPANDDZ256rmkz:
6928 case X86::VPEXPANDDZ256rrkz:
6929 case X86::VPEXPANDDZrmkz:
6930 case X86::VPEXPANDDZrrkz:
6931 case X86::VPEXPANDQZ128rmkz:
6932 case X86::VPEXPANDQZ128rrkz:
6933 case X86::VPEXPANDQZ256rmkz:
6934 case X86::VPEXPANDQZ256rrkz:
6935 case X86::VPEXPANDQZrmkz:
6936 case X86::VPEXPANDQZrrkz:
6937 case X86::VEXPANDPSZ128rmkz:
6938 case X86::VEXPANDPSZ128rrkz:
6939 case X86::VEXPANDPSZ256rmkz:
6940 case X86::VEXPANDPSZ256rrkz:
6941 case X86::VEXPANDPSZrmkz:
6942 case X86::VEXPANDPSZrrkz:
6943 case X86::VEXPANDPDZ128rmkz:
6944 case X86::VEXPANDPDZ128rrkz:
6945 case X86::VEXPANDPDZ256rmkz:
6946 case X86::VEXPANDPDZ256rrkz:
6947 case X86::VEXPANDPDZrmkz:
6948 case X86::VEXPANDPDZrrkz:
6949 return Subtarget.hasEXPANDFalseDeps();
6950 // GPR
6951 case X86::POPCNT32rm:
6952 case X86::POPCNT32rr:
6953 case X86::POPCNT64rm:
6954 case X86::POPCNT64rr:
6955 return Subtarget.hasPOPCNTFalseDeps();
6956 case X86::LZCNT32rm:
6957 case X86::LZCNT32rr:
6958 case X86::LZCNT64rm:
6959 case X86::LZCNT64rr:
6960 return Subtarget.hasLZCNTFalseDeps();
6961 case X86::TZCNT32rm:
6962 case X86::TZCNT32rr:
6963 case X86::TZCNT64rm:
6964 case X86::TZCNT64rr:
6965 return Subtarget.hasTZCNTFalseDeps();
6966 case X86::BLSR32rr:
6967 case X86::BLSR32rm:
6968 case X86::BLSR64rr:
6969 case X86::BLSR64rm:
6970 case X86::BLSI32rr:
6971 case X86::BLSI32rm:
6972 case X86::BLSI64rr:
6973 case X86::BLSI64rm:
6974 case X86::BLSMSK32rr:
6975 case X86::BLSMSK32rm:
6976 case X86::BLSMSK64rr:
6977 case X86::BLSMSK64rm:
6978 return Subtarget.hasBLSFalseDeps() && !ForLoadFold; // Preserve load folding
6979 }
6980
6981 return false;
6982}
6983
6984/// Inform the BreakFalseDeps pass how many idle
6985/// instructions we would like before a partial register update.
6987 const MachineInstr &MI, unsigned OpNum,
6988 const TargetRegisterInfo *TRI) const {
6989
6990 if (OpNum != 0)
6991 return 0;
6992
6993 // NDD ops with 8/16b results may appear to be partial register
6994 // updates after register allocation.
6995 bool HasNDDPartialWrite = false;
6996 if (X86II::hasNewDataDest(MI.getDesc().TSFlags)) {
6997 Register Reg = MI.getOperand(0).getReg();
6998 if (!Reg.isVirtual())
6999 HasNDDPartialWrite =
7000 X86::GR8RegClass.contains(Reg) || X86::GR16RegClass.contains(Reg);
7001 }
7002
7003 if (!(HasNDDPartialWrite || hasPartialRegUpdate(MI.getOpcode(), Subtarget)))
7004 return 0;
7005
7006 // Check if the result register is also used as a source.
7007 // For non-NDD ops, this means a partial update is wanted, hence we return 0.
7008 // For NDD ops, this means it is possible to compress the instruction
7009 // to a legacy form in CompressEVEX, which would create an unwanted partial
7010 // update, so we return the clearance.
7011 const MachineOperand &MO = MI.getOperand(0);
7012 Register Reg = MO.getReg();
7013 bool ReadsReg = false;
7014 if (Reg.isVirtual())
7015 ReadsReg = (MO.readsReg() || MI.readsVirtualRegister(Reg));
7016 else
7017 ReadsReg = MI.readsRegister(Reg, TRI);
7018 if (ReadsReg != HasNDDPartialWrite)
7019 return 0;
7020
7021 // If any instructions in the clearance range are reading Reg, insert a
7022 // dependency breaking instruction, which is inexpensive and is likely to
7023 // be hidden in other instruction's cycles.
7025}
7026
7027// Return true for any instruction the copies the high bits of the first source
7028// operand into the unused high bits of the destination operand.
7029// Also returns true for instructions that have two inputs where one may
7030// be undef and we want it to use the same register as the other input.
7031static bool hasUndefRegUpdate(unsigned Opcode, unsigned OpNum,
7032 bool ForLoadFold = false) {
7033 // Set the OpNum parameter to the first source operand.
7034 switch (Opcode) {
7035 case X86::MMX_PUNPCKHBWrr:
7036 case X86::MMX_PUNPCKHWDrr:
7037 case X86::MMX_PUNPCKHDQrr:
7038 case X86::MMX_PUNPCKLBWrr:
7039 case X86::MMX_PUNPCKLWDrr:
7040 case X86::MMX_PUNPCKLDQrr:
7041 case X86::MOVHLPSrr:
7042 case X86::PACKSSWBrr:
7043 case X86::PACKUSWBrr:
7044 case X86::PACKSSDWrr:
7045 case X86::PACKUSDWrr:
7046 case X86::PUNPCKHBWrr:
7047 case X86::PUNPCKLBWrr:
7048 case X86::PUNPCKHWDrr:
7049 case X86::PUNPCKLWDrr:
7050 case X86::PUNPCKHDQrr:
7051 case X86::PUNPCKLDQrr:
7052 case X86::PUNPCKHQDQrr:
7053 case X86::PUNPCKLQDQrr:
7054 case X86::SHUFPDrri:
7055 case X86::SHUFPSrri:
7056 // These instructions are sometimes used with an undef first or second
7057 // source. Return true here so BreakFalseDeps will assign this source to the
7058 // same register as the first source to avoid a false dependency.
7059 // Operand 1 of these instructions is tied so they're separate from their
7060 // VEX counterparts.
7061 return OpNum == 2 && !ForLoadFold;
7062
7063 case X86::VMOVLHPSrr:
7064 case X86::VMOVLHPSZrr:
7065 case X86::VPACKSSWBrr:
7066 case X86::VPACKUSWBrr:
7067 case X86::VPACKSSDWrr:
7068 case X86::VPACKUSDWrr:
7069 case X86::VPACKSSWBZ128rr:
7070 case X86::VPACKUSWBZ128rr:
7071 case X86::VPACKSSDWZ128rr:
7072 case X86::VPACKUSDWZ128rr:
7073 case X86::VPERM2F128rri:
7074 case X86::VPERM2I128rri:
7075 case X86::VSHUFF32X4Z256rri:
7076 case X86::VSHUFF32X4Zrri:
7077 case X86::VSHUFF64X2Z256rri:
7078 case X86::VSHUFF64X2Zrri:
7079 case X86::VSHUFI32X4Z256rri:
7080 case X86::VSHUFI32X4Zrri:
7081 case X86::VSHUFI64X2Z256rri:
7082 case X86::VSHUFI64X2Zrri:
7083 case X86::VPUNPCKHBWrr:
7084 case X86::VPUNPCKLBWrr:
7085 case X86::VPUNPCKHBWYrr:
7086 case X86::VPUNPCKLBWYrr:
7087 case X86::VPUNPCKHBWZ128rr:
7088 case X86::VPUNPCKLBWZ128rr:
7089 case X86::VPUNPCKHBWZ256rr:
7090 case X86::VPUNPCKLBWZ256rr:
7091 case X86::VPUNPCKHBWZrr:
7092 case X86::VPUNPCKLBWZrr:
7093 case X86::VPUNPCKHWDrr:
7094 case X86::VPUNPCKLWDrr:
7095 case X86::VPUNPCKHWDYrr:
7096 case X86::VPUNPCKLWDYrr:
7097 case X86::VPUNPCKHWDZ128rr:
7098 case X86::VPUNPCKLWDZ128rr:
7099 case X86::VPUNPCKHWDZ256rr:
7100 case X86::VPUNPCKLWDZ256rr:
7101 case X86::VPUNPCKHWDZrr:
7102 case X86::VPUNPCKLWDZrr:
7103 case X86::VPUNPCKHDQrr:
7104 case X86::VPUNPCKLDQrr:
7105 case X86::VPUNPCKHDQYrr:
7106 case X86::VPUNPCKLDQYrr:
7107 case X86::VPUNPCKHDQZ128rr:
7108 case X86::VPUNPCKLDQZ128rr:
7109 case X86::VPUNPCKHDQZ256rr:
7110 case X86::VPUNPCKLDQZ256rr:
7111 case X86::VPUNPCKHDQZrr:
7112 case X86::VPUNPCKLDQZrr:
7113 case X86::VPUNPCKHQDQrr:
7114 case X86::VPUNPCKLQDQrr:
7115 case X86::VPUNPCKHQDQYrr:
7116 case X86::VPUNPCKLQDQYrr:
7117 case X86::VPUNPCKHQDQZ128rr:
7118 case X86::VPUNPCKLQDQZ128rr:
7119 case X86::VPUNPCKHQDQZ256rr:
7120 case X86::VPUNPCKLQDQZ256rr:
7121 case X86::VPUNPCKHQDQZrr:
7122 case X86::VPUNPCKLQDQZrr:
7123 // These instructions are sometimes used with an undef first or second
7124 // source. Return true here so BreakFalseDeps will assign this source to the
7125 // same register as the first source to avoid a false dependency.
7126 return (OpNum == 1 || OpNum == 2) && !ForLoadFold;
7127
7128 case X86::VCVTSI2SSrr:
7129 case X86::VCVTSI2SSrm:
7130 case X86::VCVTSI2SSrr_Int:
7131 case X86::VCVTSI2SSrm_Int:
7132 case X86::VCVTSI642SSrr:
7133 case X86::VCVTSI642SSrm:
7134 case X86::VCVTSI642SSrr_Int:
7135 case X86::VCVTSI642SSrm_Int:
7136 case X86::VCVTSI2SDrr:
7137 case X86::VCVTSI2SDrm:
7138 case X86::VCVTSI2SDrr_Int:
7139 case X86::VCVTSI2SDrm_Int:
7140 case X86::VCVTSI642SDrr:
7141 case X86::VCVTSI642SDrm:
7142 case X86::VCVTSI642SDrr_Int:
7143 case X86::VCVTSI642SDrm_Int:
7144 // AVX-512
7145 case X86::VCVTSI2SSZrr:
7146 case X86::VCVTSI2SSZrm:
7147 case X86::VCVTSI2SSZrr_Int:
7148 case X86::VCVTSI2SSZrrb_Int:
7149 case X86::VCVTSI2SSZrm_Int:
7150 case X86::VCVTSI642SSZrr:
7151 case X86::VCVTSI642SSZrm:
7152 case X86::VCVTSI642SSZrr_Int:
7153 case X86::VCVTSI642SSZrrb_Int:
7154 case X86::VCVTSI642SSZrm_Int:
7155 case X86::VCVTSI2SDZrr:
7156 case X86::VCVTSI2SDZrm:
7157 case X86::VCVTSI2SDZrr_Int:
7158 case X86::VCVTSI2SDZrm_Int:
7159 case X86::VCVTSI642SDZrr:
7160 case X86::VCVTSI642SDZrm:
7161 case X86::VCVTSI642SDZrr_Int:
7162 case X86::VCVTSI642SDZrrb_Int:
7163 case X86::VCVTSI642SDZrm_Int:
7164 case X86::VCVTUSI2SSZrr:
7165 case X86::VCVTUSI2SSZrm:
7166 case X86::VCVTUSI2SSZrr_Int:
7167 case X86::VCVTUSI2SSZrrb_Int:
7168 case X86::VCVTUSI2SSZrm_Int:
7169 case X86::VCVTUSI642SSZrr:
7170 case X86::VCVTUSI642SSZrm:
7171 case X86::VCVTUSI642SSZrr_Int:
7172 case X86::VCVTUSI642SSZrrb_Int:
7173 case X86::VCVTUSI642SSZrm_Int:
7174 case X86::VCVTUSI2SDZrr:
7175 case X86::VCVTUSI2SDZrm:
7176 case X86::VCVTUSI2SDZrr_Int:
7177 case X86::VCVTUSI2SDZrm_Int:
7178 case X86::VCVTUSI642SDZrr:
7179 case X86::VCVTUSI642SDZrm:
7180 case X86::VCVTUSI642SDZrr_Int:
7181 case X86::VCVTUSI642SDZrrb_Int:
7182 case X86::VCVTUSI642SDZrm_Int:
7183 case X86::VCVTSI2SHZrr:
7184 case X86::VCVTSI2SHZrm:
7185 case X86::VCVTSI2SHZrr_Int:
7186 case X86::VCVTSI2SHZrrb_Int:
7187 case X86::VCVTSI2SHZrm_Int:
7188 case X86::VCVTSI642SHZrr:
7189 case X86::VCVTSI642SHZrm:
7190 case X86::VCVTSI642SHZrr_Int:
7191 case X86::VCVTSI642SHZrrb_Int:
7192 case X86::VCVTSI642SHZrm_Int:
7193 case X86::VCVTUSI2SHZrr:
7194 case X86::VCVTUSI2SHZrm:
7195 case X86::VCVTUSI2SHZrr_Int:
7196 case X86::VCVTUSI2SHZrrb_Int:
7197 case X86::VCVTUSI2SHZrm_Int:
7198 case X86::VCVTUSI642SHZrr:
7199 case X86::VCVTUSI642SHZrm:
7200 case X86::VCVTUSI642SHZrr_Int:
7201 case X86::VCVTUSI642SHZrrb_Int:
7202 case X86::VCVTUSI642SHZrm_Int:
7203 // Load folding won't effect the undef register update since the input is
7204 // a GPR.
7205 return OpNum == 1 && !ForLoadFold;
7206 case X86::VCVTSD2SSrr:
7207 case X86::VCVTSD2SSrm:
7208 case X86::VCVTSD2SSrr_Int:
7209 case X86::VCVTSD2SSrm_Int:
7210 case X86::VCVTSS2SDrr:
7211 case X86::VCVTSS2SDrm:
7212 case X86::VCVTSS2SDrr_Int:
7213 case X86::VCVTSS2SDrm_Int:
7214 case X86::VRCPSSr:
7215 case X86::VRCPSSr_Int:
7216 case X86::VRCPSSm:
7217 case X86::VRCPSSm_Int:
7218 case X86::VROUNDSDri:
7219 case X86::VROUNDSDmi:
7220 case X86::VROUNDSDri_Int:
7221 case X86::VROUNDSDmi_Int:
7222 case X86::VROUNDSSri:
7223 case X86::VROUNDSSmi:
7224 case X86::VROUNDSSri_Int:
7225 case X86::VROUNDSSmi_Int:
7226 case X86::VRSQRTSSr:
7227 case X86::VRSQRTSSr_Int:
7228 case X86::VRSQRTSSm:
7229 case X86::VRSQRTSSm_Int:
7230 case X86::VSQRTSSr:
7231 case X86::VSQRTSSr_Int:
7232 case X86::VSQRTSSm:
7233 case X86::VSQRTSSm_Int:
7234 case X86::VSQRTSDr:
7235 case X86::VSQRTSDr_Int:
7236 case X86::VSQRTSDm:
7237 case X86::VSQRTSDm_Int:
7238 // AVX-512
7239 case X86::VCVTSD2SSZrr:
7240 case X86::VCVTSD2SSZrr_Int:
7241 case X86::VCVTSD2SSZrrb_Int:
7242 case X86::VCVTSD2SSZrm:
7243 case X86::VCVTSD2SSZrm_Int:
7244 case X86::VCVTSS2SDZrr:
7245 case X86::VCVTSS2SDZrr_Int:
7246 case X86::VCVTSS2SDZrrb_Int:
7247 case X86::VCVTSS2SDZrm:
7248 case X86::VCVTSS2SDZrm_Int:
7249 case X86::VGETEXPSDZr:
7250 case X86::VGETEXPSDZrb:
7251 case X86::VGETEXPSDZm:
7252 case X86::VGETEXPSSZr:
7253 case X86::VGETEXPSSZrb:
7254 case X86::VGETEXPSSZm:
7255 case X86::VGETMANTSDZrri:
7256 case X86::VGETMANTSDZrrib:
7257 case X86::VGETMANTSDZrmi:
7258 case X86::VGETMANTSSZrri:
7259 case X86::VGETMANTSSZrrib:
7260 case X86::VGETMANTSSZrmi:
7261 case X86::VRNDSCALESDZrri:
7262 case X86::VRNDSCALESDZrri_Int:
7263 case X86::VRNDSCALESDZrrib_Int:
7264 case X86::VRNDSCALESDZrmi:
7265 case X86::VRNDSCALESDZrmi_Int:
7266 case X86::VRNDSCALESSZrri:
7267 case X86::VRNDSCALESSZrri_Int:
7268 case X86::VRNDSCALESSZrrib_Int:
7269 case X86::VRNDSCALESSZrmi:
7270 case X86::VRNDSCALESSZrmi_Int:
7271 case X86::VRCP14SDZrr:
7272 case X86::VRCP14SDZrm:
7273 case X86::VRCP14SSZrr:
7274 case X86::VRCP14SSZrm:
7275 case X86::VRCPSHZrr:
7276 case X86::VRCPSHZrm:
7277 case X86::VRSQRTSHZrr:
7278 case X86::VRSQRTSHZrm:
7279 case X86::VREDUCESHZrmi:
7280 case X86::VREDUCESHZrri:
7281 case X86::VREDUCESHZrrib:
7282 case X86::VGETEXPSHZr:
7283 case X86::VGETEXPSHZrb:
7284 case X86::VGETEXPSHZm:
7285 case X86::VGETMANTSHZrri:
7286 case X86::VGETMANTSHZrrib:
7287 case X86::VGETMANTSHZrmi:
7288 case X86::VRNDSCALESHZrri:
7289 case X86::VRNDSCALESHZrri_Int:
7290 case X86::VRNDSCALESHZrrib_Int:
7291 case X86::VRNDSCALESHZrmi:
7292 case X86::VRNDSCALESHZrmi_Int:
7293 case X86::VSQRTSHZr:
7294 case X86::VSQRTSHZr_Int:
7295 case X86::VSQRTSHZrb_Int:
7296 case X86::VSQRTSHZm:
7297 case X86::VSQRTSHZm_Int:
7298 case X86::VRCP28SDZr:
7299 case X86::VRCP28SDZrb:
7300 case X86::VRCP28SDZm:
7301 case X86::VRCP28SSZr:
7302 case X86::VRCP28SSZrb:
7303 case X86::VRCP28SSZm:
7304 case X86::VREDUCESSZrmi:
7305 case X86::VREDUCESSZrri:
7306 case X86::VREDUCESSZrrib:
7307 case X86::VRSQRT14SDZrr:
7308 case X86::VRSQRT14SDZrm:
7309 case X86::VRSQRT14SSZrr:
7310 case X86::VRSQRT14SSZrm:
7311 case X86::VRSQRT28SDZr:
7312 case X86::VRSQRT28SDZrb:
7313 case X86::VRSQRT28SDZm:
7314 case X86::VRSQRT28SSZr:
7315 case X86::VRSQRT28SSZrb:
7316 case X86::VRSQRT28SSZm:
7317 case X86::VSQRTSSZr:
7318 case X86::VSQRTSSZr_Int:
7319 case X86::VSQRTSSZrb_Int:
7320 case X86::VSQRTSSZm:
7321 case X86::VSQRTSSZm_Int:
7322 case X86::VSQRTSDZr:
7323 case X86::VSQRTSDZr_Int:
7324 case X86::VSQRTSDZrb_Int:
7325 case X86::VSQRTSDZm:
7326 case X86::VSQRTSDZm_Int:
7327 case X86::VCVTSD2SHZrr:
7328 case X86::VCVTSD2SHZrr_Int:
7329 case X86::VCVTSD2SHZrrb_Int:
7330 case X86::VCVTSD2SHZrm:
7331 case X86::VCVTSD2SHZrm_Int:
7332 case X86::VCVTSS2SHZrr:
7333 case X86::VCVTSS2SHZrr_Int:
7334 case X86::VCVTSS2SHZrrb_Int:
7335 case X86::VCVTSS2SHZrm:
7336 case X86::VCVTSS2SHZrm_Int:
7337 case X86::VCVTSH2SDZrr:
7338 case X86::VCVTSH2SDZrr_Int:
7339 case X86::VCVTSH2SDZrrb_Int:
7340 case X86::VCVTSH2SDZrm:
7341 case X86::VCVTSH2SDZrm_Int:
7342 case X86::VCVTSH2SSZrr:
7343 case X86::VCVTSH2SSZrr_Int:
7344 case X86::VCVTSH2SSZrrb_Int:
7345 case X86::VCVTSH2SSZrm:
7346 case X86::VCVTSH2SSZrm_Int:
7347 return OpNum == 1;
7348 case X86::VMOVSSZrrk:
7349 case X86::VMOVSDZrrk:
7350 return OpNum == 3 && !ForLoadFold;
7351 case X86::VMOVSSZrrkz:
7352 case X86::VMOVSDZrrkz:
7353 return OpNum == 2 && !ForLoadFold;
7354 }
7355
7356 return false;
7357}
7358
7359/// Inform the BreakFalseDeps pass how many idle instructions we would like
7360/// before certain undef register reads.
7361///
7362/// This catches the VCVTSI2SD family of instructions:
7363///
7364/// vcvtsi2sdq %rax, undef %xmm0, %xmm14
7365///
7366/// We should to be careful *not* to catch VXOR idioms which are presumably
7367/// handled specially in the pipeline:
7368///
7369/// vxorps undef %xmm1, undef %xmm1, %xmm1
7370///
7371/// Like getPartialRegUpdateClearance, this makes a strong assumption that the
7372/// high bits that are passed-through are not live.
7373unsigned
7375 const TargetRegisterInfo *TRI) const {
7376 const MachineOperand &MO = MI.getOperand(OpNum);
7377 if (MO.getReg().isPhysical() && hasUndefRegUpdate(MI.getOpcode(), OpNum))
7378 return UndefRegClearance;
7379
7380 return 0;
7381}
7382
7384 MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const {
7385 Register Reg = MI.getOperand(OpNum).getReg();
7386 // If MI kills this register, the false dependence is already broken.
7387 if (MI.killsRegister(Reg, TRI))
7388 return;
7389
7390 if (X86::VR128RegClass.contains(Reg)) {
7391 // These instructions are all floating point domain, so xorps is the best
7392 // choice.
7393 unsigned Opc = Subtarget.hasAVX() ? X86::VXORPSrr : X86::XORPSrr;
7394 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(Opc), Reg)
7395 .addReg(Reg, RegState::Undef)
7396 .addReg(Reg, RegState::Undef);
7397 MI.addRegisterKilled(Reg, TRI, true);
7398 } else if (X86::VR256RegClass.contains(Reg)) {
7399 // Use vxorps to clear the full ymm register.
7400 // It wants to read and write the xmm sub-register.
7401 Register XReg = TRI->getSubReg(Reg, X86::sub_xmm);
7402 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(X86::VXORPSrr), XReg)
7403 .addReg(XReg, RegState::Undef)
7404 .addReg(XReg, RegState::Undef)
7406 MI.addRegisterKilled(Reg, TRI, true);
7407 } else if (X86::VR128XRegClass.contains(Reg)) {
7408 // Only handle VLX targets.
7409 if (!Subtarget.hasVLX())
7410 return;
7411 // Since vxorps requires AVX512DQ, vpxord should be the best choice.
7412 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(X86::VPXORDZ128rr), Reg)
7413 .addReg(Reg, RegState::Undef)
7414 .addReg(Reg, RegState::Undef);
7415 MI.addRegisterKilled(Reg, TRI, true);
7416 } else if (X86::VR256XRegClass.contains(Reg) ||
7417 X86::VR512RegClass.contains(Reg)) {
7418 // Only handle VLX targets.
7419 if (!Subtarget.hasVLX())
7420 return;
7421 // Use vpxord to clear the full ymm/zmm register.
7422 // It wants to read and write the xmm sub-register.
7423 Register XReg = TRI->getSubReg(Reg, X86::sub_xmm);
7424 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(X86::VPXORDZ128rr), XReg)
7425 .addReg(XReg, RegState::Undef)
7426 .addReg(XReg, RegState::Undef)
7428 MI.addRegisterKilled(Reg, TRI, true);
7429 } else if (X86::GR64RegClass.contains(Reg)) {
7430 // Using XOR32rr because it has shorter encoding and zeros up the upper bits
7431 // as well.
7432 Register XReg = TRI->getSubReg(Reg, X86::sub_32bit);
7433 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(X86::XOR32rr), XReg)
7434 .addReg(XReg, RegState::Undef)
7435 .addReg(XReg, RegState::Undef)
7437 MI.addRegisterKilled(Reg, TRI, true);
7438 } else if (X86::GR32RegClass.contains(Reg)) {
7439 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(X86::XOR32rr), Reg)
7440 .addReg(Reg, RegState::Undef)
7441 .addReg(Reg, RegState::Undef);
7442 MI.addRegisterKilled(Reg, TRI, true);
7443 } else if ((X86::GR16RegClass.contains(Reg) ||
7444 X86::GR8RegClass.contains(Reg)) &&
7445 X86II::hasNewDataDest(MI.getDesc().TSFlags)) {
7446 // This case is only expected for NDD ops which appear to be partial
7447 // writes, but are not due to the zeroing of the upper part. Here
7448 // we add an implicit def of the superegister, which prevents
7449 // CompressEVEX from converting this to a legacy form.
7450 Register SuperReg = getX86SubSuperRegister(Reg, 64);
7451 MachineInstrBuilder BuildMI(*MI.getParent()->getParent(), &MI);
7452 if (!MI.definesRegister(SuperReg, /*TRI=*/nullptr))
7453 BuildMI.addReg(SuperReg, RegState::ImplicitDefine);
7454 }
7455}
7456
7458 int PtrOffset = 0) {
7459 unsigned NumAddrOps = MOs.size();
7460
7461 if (NumAddrOps < 4) {
7462 // FrameIndex only - add an immediate offset (whether its zero or not).
7463 for (unsigned i = 0; i != NumAddrOps; ++i)
7464 MIB.add(MOs[i]);
7465 addOffset(MIB, PtrOffset);
7466 } else {
7467 // General Memory Addressing - we need to add any offset to an existing
7468 // offset.
7469 assert(MOs.size() == 5 && "Unexpected memory operand list length");
7470 for (unsigned i = 0; i != NumAddrOps; ++i) {
7471 const MachineOperand &MO = MOs[i];
7472 if (i == 3 && PtrOffset != 0) {
7473 MIB.addDisp(MO, PtrOffset);
7474 } else {
7475 MIB.add(MO);
7476 }
7477 }
7478 }
7479}
7480
7482 MachineInstr &NewMI,
7483 const TargetInstrInfo &TII) {
7484 MachineRegisterInfo &MRI = MF.getRegInfo();
7485
7486 for (int Idx : llvm::seq<int>(0, NewMI.getNumOperands())) {
7487 MachineOperand &MO = NewMI.getOperand(Idx);
7488 // We only need to update constraints on virtual register operands.
7489 if (!MO.isReg())
7490 continue;
7491 Register Reg = MO.getReg();
7492 if (!Reg.isVirtual())
7493 continue;
7494
7495 auto *NewRC =
7496 MRI.constrainRegClass(Reg, TII.getRegClass(NewMI.getDesc(), Idx));
7497 if (!NewRC) {
7498 LLVM_DEBUG(
7499 dbgs() << "WARNING: Unable to update register constraint for operand "
7500 << Idx << " of instruction:\n";
7501 NewMI.dump(); dbgs() << "\n");
7502 }
7503 }
7504}
7505
7506static MachineInstr *fuseTwoAddrInst(MachineFunction &MF, unsigned Opcode,
7510 const TargetInstrInfo &TII) {
7511 // Create the base instruction with the memory operand as the first part.
7512 // Omit the implicit operands, something BuildMI can't do.
7513 MachineInstr *NewMI =
7514 MF.CreateMachineInstr(TII.get(Opcode), MI.getDebugLoc(), true);
7515 MachineInstrBuilder MIB(MF, NewMI);
7516 addOperands(MIB, MOs);
7517
7518 // Loop over the rest of the ri operands, converting them over.
7519 unsigned NumOps = MI.getDesc().getNumOperands() - 2;
7520 for (unsigned i = 0; i != NumOps; ++i) {
7521 MachineOperand &MO = MI.getOperand(i + 2);
7522 MIB.add(MO);
7523 }
7524 for (const MachineOperand &MO : llvm::drop_begin(MI.operands(), NumOps + 2))
7525 MIB.add(MO);
7526
7527 updateOperandRegConstraints(MF, *NewMI, TII);
7528
7529 MachineBasicBlock *MBB = InsertPt->getParent();
7530 MBB->insert(InsertPt, NewMI);
7531
7532 return MIB;
7533}
7534
7535static MachineInstr *fuseInst(MachineFunction &MF, unsigned Opcode,
7536 unsigned OpNo, ArrayRef<MachineOperand> MOs,
7539 int PtrOffset = 0) {
7540 // Omit the implicit operands, something BuildMI can't do.
7541 MachineInstr *NewMI =
7542 MF.CreateMachineInstr(TII.get(Opcode), MI.getDebugLoc(), true);
7543 MachineInstrBuilder MIB(MF, NewMI);
7544
7545 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
7546 MachineOperand &MO = MI.getOperand(i);
7547 if (i == OpNo) {
7548 assert(MO.isReg() && "Expected to fold into reg operand!");
7549 addOperands(MIB, MOs, PtrOffset);
7550 } else {
7551 MIB.add(MO);
7552 }
7553 }
7554
7555 updateOperandRegConstraints(MF, *NewMI, TII);
7556
7557 // Copy the NoFPExcept flag from the instruction we're fusing.
7560
7561 MachineBasicBlock *MBB = InsertPt->getParent();
7562 MBB->insert(InsertPt, NewMI);
7563
7564 return MIB;
7565}
7566
7567static MachineInstr *makeM0Inst(const TargetInstrInfo &TII, unsigned Opcode,
7570 MachineInstr &MI) {
7571 MachineInstrBuilder MIB = BuildMI(*InsertPt->getParent(), InsertPt,
7572 MI.getDebugLoc(), TII.get(Opcode));
7573 addOperands(MIB, MOs);
7574 return MIB.addImm(0);
7575}
7576
7577MachineInstr *X86InstrInfo::foldMemoryOperandCustom(
7578 MachineFunction &MF, MachineInstr &MI, unsigned OpNum,
7580 unsigned Size, Align Alignment) const {
7581 switch (MI.getOpcode()) {
7582 case X86::INSERTPSrri:
7583 case X86::VINSERTPSrri:
7584 case X86::VINSERTPSZrri:
7585 // Attempt to convert the load of inserted vector into a fold load
7586 // of a single float.
7587 if (OpNum == 2) {
7588 unsigned Imm = MI.getOperand(MI.getNumOperands() - 1).getImm();
7589 unsigned ZMask = Imm & 15;
7590 unsigned DstIdx = (Imm >> 4) & 3;
7591 unsigned SrcIdx = (Imm >> 6) & 3;
7592
7593 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
7594 const TargetRegisterClass *RC = getRegClass(MI.getDesc(), OpNum);
7595 unsigned RCSize = TRI.getRegSizeInBits(*RC) / 8;
7596 if ((Size == 0 || Size >= 16) && RCSize >= 16 &&
7597 (MI.getOpcode() != X86::INSERTPSrri || Alignment >= Align(4))) {
7598 int PtrOffset = SrcIdx * 4;
7599 unsigned NewImm = (DstIdx << 4) | ZMask;
7600 unsigned NewOpCode =
7601 (MI.getOpcode() == X86::VINSERTPSZrri) ? X86::VINSERTPSZrmi
7602 : (MI.getOpcode() == X86::VINSERTPSrri) ? X86::VINSERTPSrmi
7603 : X86::INSERTPSrmi;
7604 MachineInstr *NewMI =
7605 fuseInst(MF, NewOpCode, OpNum, MOs, InsertPt, MI, *this, PtrOffset);
7606 NewMI->getOperand(NewMI->getNumOperands() - 1).setImm(NewImm);
7607 return NewMI;
7608 }
7609 }
7610 break;
7611 case X86::MOVHLPSrr:
7612 case X86::VMOVHLPSrr:
7613 case X86::VMOVHLPSZrr:
7614 // Move the upper 64-bits of the second operand to the lower 64-bits.
7615 // To fold the load, adjust the pointer to the upper and use (V)MOVLPS.
7616 // TODO: In most cases AVX doesn't have a 8-byte alignment requirement.
7617 if (OpNum == 2) {
7618 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
7619 const TargetRegisterClass *RC = getRegClass(MI.getDesc(), OpNum);
7620 unsigned RCSize = TRI.getRegSizeInBits(*RC) / 8;
7621 if ((Size == 0 || Size >= 16) && RCSize >= 16 && Alignment >= Align(8)) {
7622 unsigned NewOpCode =
7623 (MI.getOpcode() == X86::VMOVHLPSZrr) ? X86::VMOVLPSZ128rm
7624 : (MI.getOpcode() == X86::VMOVHLPSrr) ? X86::VMOVLPSrm
7625 : X86::MOVLPSrm;
7626 MachineInstr *NewMI =
7627 fuseInst(MF, NewOpCode, OpNum, MOs, InsertPt, MI, *this, 8);
7628 return NewMI;
7629 }
7630 }
7631 break;
7632 case X86::UNPCKLPDrr:
7633 // If we won't be able to fold this to the memory form of UNPCKL, use
7634 // MOVHPD instead. Done as custom because we can't have this in the load
7635 // table twice.
7636 if (OpNum == 2) {
7637 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
7638 const TargetRegisterClass *RC = getRegClass(MI.getDesc(), OpNum);
7639 unsigned RCSize = TRI.getRegSizeInBits(*RC) / 8;
7640 if ((Size == 0 || Size >= 16) && RCSize >= 16 && Alignment < Align(16)) {
7641 MachineInstr *NewMI =
7642 fuseInst(MF, X86::MOVHPDrm, OpNum, MOs, InsertPt, MI, *this);
7643 return NewMI;
7644 }
7645 }
7646 break;
7647 case X86::MOV32r0:
7648 if (auto *NewMI =
7649 makeM0Inst(*this, (Size == 4) ? X86::MOV32mi : X86::MOV64mi32, MOs,
7650 InsertPt, MI))
7651 return NewMI;
7652 break;
7653 }
7654
7655 return nullptr;
7656}
7657
7659 MachineInstr &MI) {
7660 if (!hasUndefRegUpdate(MI.getOpcode(), 1, /*ForLoadFold*/ true) ||
7661 !MI.getOperand(1).isReg())
7662 return false;
7663
7664 // The are two cases we need to handle depending on where in the pipeline
7665 // the folding attempt is being made.
7666 // -Register has the undef flag set.
7667 // -Register is produced by the IMPLICIT_DEF instruction.
7668
7669 if (MI.getOperand(1).isUndef())
7670 return true;
7671
7673 MachineInstr *VRegDef = RegInfo.getUniqueVRegDef(MI.getOperand(1).getReg());
7674 return VRegDef && VRegDef->isImplicitDef();
7675}
7676
7677unsigned X86InstrInfo::commuteOperandsForFold(MachineInstr &MI,
7678 unsigned Idx1) const {
7679 unsigned Idx2 = CommuteAnyOperandIndex;
7680 if (!findCommutedOpIndices(MI, Idx1, Idx2))
7681 return Idx1;
7682
7683 bool HasDef = MI.getDesc().getNumDefs();
7684 Register Reg0 = HasDef ? MI.getOperand(0).getReg() : Register();
7685 Register Reg1 = MI.getOperand(Idx1).getReg();
7686 Register Reg2 = MI.getOperand(Idx2).getReg();
7687 bool Tied1 = 0 == MI.getDesc().getOperandConstraint(Idx1, MCOI::TIED_TO);
7688 bool Tied2 = 0 == MI.getDesc().getOperandConstraint(Idx2, MCOI::TIED_TO);
7689
7690 // If either of the commutable operands are tied to the destination
7691 // then we can not commute + fold.
7692 if ((HasDef && Reg0 == Reg1 && Tied1) || (HasDef && Reg0 == Reg2 && Tied2))
7693 return Idx1;
7694
7695 return commuteInstruction(MI, false, Idx1, Idx2) ? Idx2 : Idx1;
7696}
7697
7698static void printFailMsgforFold(const MachineInstr &MI, unsigned Idx) {
7699 if (PrintFailedFusing && !MI.isCopy())
7700 dbgs() << "We failed to fuse operand " << Idx << " in " << MI;
7701}
7702
7704 MachineFunction &MF, MachineInstr &MI, unsigned OpNum,
7706 unsigned Size, Align Alignment, bool AllowCommute, MachineInstr *&CopyMI,
7707 VirtRegMap *VRM) const {
7708 bool isSlowTwoMemOps = Subtarget.slowTwoMemOps();
7709 bool isSlowIndirectCall = Subtarget.slowIndirectCall();
7710 unsigned Opc = MI.getOpcode();
7711
7712 // For CPUs that favor the register form of a call,
7713 // do not fold loads into calls, unless optimizing for size aggressively.
7714 if ((isSlowTwoMemOps || isSlowIndirectCall) &&
7715 !MF.getFunction().hasMinSize() &&
7716 (Opc == X86::CALL32r || Opc == X86::CALL64r ||
7717 Opc == X86::CALL64r_ImpCall))
7718 return nullptr;
7719
7720 // For CPUs that favor the register form of a push,
7721 // do not fold loads into pushes, unless optimizing for size aggressively.
7722 if (isSlowTwoMemOps && !MF.getFunction().hasMinSize() &&
7723 (Opc == X86::PUSH16r || Opc == X86::PUSH32r || Opc == X86::PUSH64r))
7724 return nullptr;
7725
7726 // Avoid partial and undef register update stalls unless optimizing for size.
7727 if (!MF.getFunction().hasOptSize() &&
7728 (hasPartialRegUpdate(Opc, Subtarget, /*ForLoadFold*/ true) ||
7730 return nullptr;
7731
7732 unsigned NumOps = MI.getDesc().getNumOperands();
7733 bool IsTwoAddr = NumOps > 1 && OpNum < 2 && MI.getOperand(0).isReg() &&
7734 MI.getOperand(1).isReg() &&
7735 MI.getOperand(0).getReg() == MI.getOperand(1).getReg();
7736
7737 // FIXME: AsmPrinter doesn't know how to handle
7738 // X86II::MO_GOT_ABSOLUTE_ADDRESS after folding.
7739 if (Opc == X86::ADD32ri &&
7740 MI.getOperand(2).getTargetFlags() == X86II::MO_GOT_ABSOLUTE_ADDRESS)
7741 return nullptr;
7742
7743 // GOTTPOFF relocation loads can only be folded into add instructions.
7744 // FIXME: Need to exclude other relocations that only support specific
7745 // instructions.
7746 if (MOs.size() == X86::AddrNumOperands &&
7747 MOs[X86::AddrDisp].getTargetFlags() == X86II::MO_GOTTPOFF &&
7748 Opc != X86::ADD64rr)
7749 return nullptr;
7750
7751 // Don't fold loads into indirect calls that need a KCFI check as we'll
7752 // have to unfold these in X86TargetLowering::EmitKCFICheck anyway.
7753 if (MI.isCall() && MI.getCFIType())
7754 return nullptr;
7755
7756 // Attempt to fold any custom cases we have.
7757 if (auto *CustomMI = foldMemoryOperandCustom(MF, MI, OpNum, MOs, InsertPt,
7758 Size, Alignment))
7759 return CustomMI;
7760
7761 // Folding a memory location into the two-address part of a two-address
7762 // instruction is different than folding it other places. It requires
7763 // replacing the *two* registers with the memory location.
7764 //
7765 // Utilize the mapping NonNDD -> RMW for the NDD variant.
7766 unsigned NonNDOpc = Subtarget.hasNDD() ? X86::getNonNDVariant(Opc) : 0U;
7767 // Utilize the mapping NonNDD if NDD memory variant is not preferred.
7768 bool NoNDDM = NonNDOpc && !Subtarget.hasNDDM();
7769
7770 MachineRegisterInfo &MRI = MF.getRegInfo();
7771 if (NoNDDM && !IsTwoAddr && !MRI.isSSA()) {
7772 // Bail out if dst has subreg. It happens during register-coalescer from
7773 // 704B %19:gr32 = SUB32rr_ND killed %0:gr32, killed %7:gr32, ...
7774 // 752B undef %23.sub_32bit:gr64 = COPY killed %19:gr32
7775 // 768B %25:gr32 = LEA64_32r killed %23:gr64, 1, killed %21:gr64_nosp, ...
7776 // to
7777 // 704B undef %23.sub_32bit:gr64_with_sub_8bit = SUB32rr_ND %0:gr32, ...
7778 // 768B %25:gr32 = LEA64_32r %23:gr64_with_sub_8bit, 1, %21:gr64_nosp, ...
7779 // Machine verifier fails if we try to tie %23 to the source.
7780 if (MI.getOperand(0).getSubReg())
7781 return nullptr;
7782
7783 // Bail out if dst has been assigned a physical register. Otherwise, we
7784 // cannot update LiveRegMatrix properly.
7785 Register Dst = MI.getOperand(0).getReg();
7786 if (VRM && Dst != MI.getOperand(1).getReg() &&
7787 (!Dst.isVirtual() || VRM->getPhys(Dst)))
7788 return nullptr;
7789 }
7790
7791 const X86FoldTableEntry *I =
7792 IsTwoAddr ? lookupTwoAddrFoldTable(NonNDOpc ? NonNDOpc : Opc)
7793 : lookupFoldTable(NoNDDM ? NonNDOpc : Opc, OpNum);
7794
7795 MachineInstr *NewMI = nullptr;
7796 if (I) {
7797 unsigned Opcode = I->DstOp;
7798 if (Alignment <
7799 Align(1ULL << ((I->Flags & TB_ALIGN_MASK) >> TB_ALIGN_SHIFT)))
7800 return nullptr;
7801 bool NarrowToMOV32rm = false;
7802 if (Size) {
7804 const TargetRegisterClass *RC = getRegClass(MI.getDesc(), OpNum);
7805 unsigned RCSize = TRI.getRegSizeInBits(*RC) / 8;
7806 // Check if it's safe to fold the load. If the size of the object is
7807 // narrower than the load width, then it's not.
7808 // FIXME: Allow scalar intrinsic instructions like ADDSSrm_Int.
7809 if ((I->Flags & TB_FOLDED_LOAD) && Size < RCSize) {
7810 // If this is a 64-bit load, but the spill slot is 32, then we can do
7811 // a 32-bit load which is implicitly zero-extended. This likely is
7812 // due to live interval analysis remat'ing a load from stack slot.
7813 if (Opcode != X86::MOV64rm || RCSize != 8 || Size != 4)
7814 return nullptr;
7815 if (MI.getOperand(0).getSubReg() || MI.getOperand(1).getSubReg())
7816 return nullptr;
7817 Opcode = X86::MOV32rm;
7818 NarrowToMOV32rm = true;
7819 }
7820 // For stores, make sure the size of the object is equal to the size of
7821 // the store. If the object is larger, the extra bits would be garbage. If
7822 // the object is smaller we might overwrite another object or fault.
7823 if ((I->Flags & TB_FOLDED_STORE) && Size != RCSize)
7824 return nullptr;
7825 }
7826
7827 NewMI = IsTwoAddr ? fuseTwoAddrInst(MF, Opcode, MOs, InsertPt, MI, *this)
7828 : fuseInst(MF, Opcode, OpNum, MOs, InsertPt, MI, *this);
7829
7830 if (NarrowToMOV32rm) {
7831 // If this is the special case where we use a MOV32rm to load a 32-bit
7832 // value and zero-extend the top bits. Change the destination register
7833 // to a 32-bit one.
7834 Register DstReg = NewMI->getOperand(0).getReg();
7835 if (DstReg.isPhysical())
7836 NewMI->getOperand(0).setReg(RI.getSubReg(DstReg, X86::sub_32bit));
7837 else
7838 NewMI->getOperand(0).setSubReg(X86::sub_32bit);
7839 }
7840
7841 if (NoNDDM && !IsTwoAddr) {
7842 Register SrcReg = MI.getOperand(1).getReg();
7843 unsigned SrcSub = MI.getOperand(1).getSubReg();
7844 if (MI.killsRegister(SrcReg, /*TRI=*/nullptr) ||
7845 MI.getOperand(0).getReg() == SrcReg)
7846 return NewMI;
7847
7848 Register NewSrc = MI.getOperand(0).getReg();
7849 if (MRI.isSSA())
7850 NewSrc = MRI.createVirtualRegister(getRegClass(NewMI->getDesc(), 1));
7851
7852 CopyMI = BuildMI(*NewMI->getParent(), *NewMI, MI.getDebugLoc(),
7853 get(TargetOpcode::COPY))
7854 .addDef(NewSrc)
7855 .addReg(SrcReg, {}, SrcSub);
7856 NewMI->getOperand(1).setReg(NewSrc);
7857 NewMI->getOperand(1).setSubReg(0);
7858 }
7859 return NewMI;
7860 }
7861
7862 if (AllowCommute) {
7863 // If the instruction and target operand are commutable, commute the
7864 // instruction and try again.
7865 unsigned CommuteOpIdx2 = commuteOperandsForFold(MI, OpNum);
7866 if (CommuteOpIdx2 == OpNum) {
7867 printFailMsgforFold(MI, OpNum);
7868 return nullptr;
7869 }
7870 // Attempt to fold with the commuted version of the instruction.
7871 NewMI = foldMemoryOperandImpl(MF, MI, CommuteOpIdx2, MOs, InsertPt, Size,
7872 Alignment, /*AllowCommute=*/false, CopyMI);
7873 if (NewMI)
7874 return NewMI;
7875 // Folding failed again - undo the commute before returning.
7876 commuteInstruction(MI, false, OpNum, CommuteOpIdx2);
7877 }
7878
7879 printFailMsgforFold(MI, OpNum);
7880 return nullptr;
7881}
7882
7885 ArrayRef<unsigned> Ops, int FrameIndex,
7886 MachineInstr *&CopyMI, LiveIntervals *LIS,
7887 VirtRegMap *VRM) const {
7889 // Check switch flag
7890 if (NoFusing)
7891 return nullptr;
7892
7893 // Avoid partial and undef register update stalls unless optimizing for size.
7894 if (!MF.getFunction().hasOptSize() &&
7895 (hasPartialRegUpdate(MI.getOpcode(), Subtarget, /*ForLoadFold*/ true) ||
7897 return nullptr;
7898
7899 // Don't fold subreg spills, or reloads that use a high subreg.
7900 for (auto Op : Ops) {
7901 MachineOperand &MO = MI.getOperand(Op);
7902 auto SubReg = MO.getSubReg();
7903 // MOV32r0 is special b/c it's used to clear a 64-bit register too.
7904 // (See patterns for MOV32r0 in TD files).
7905 if (MI.getOpcode() == X86::MOV32r0 && SubReg == X86::sub_32bit)
7906 continue;
7907 if (SubReg && (MO.isDef() || SubReg == X86::sub_8bit_hi))
7908 return nullptr;
7909 }
7910
7911 const MachineFrameInfo &MFI = MF.getFrameInfo();
7912 unsigned Size = MFI.getObjectSize(FrameIndex);
7913 Align Alignment = MFI.getObjectAlign(FrameIndex);
7914 // If the function stack isn't realigned we don't want to fold instructions
7915 // that need increased alignment.
7916 if (!RI.hasStackRealignment(MF))
7917 Alignment =
7918 std::min(Alignment, Subtarget.getFrameLowering()->getStackAlign());
7919
7920 auto Impl = [&]() {
7921 return foldMemoryOperandImpl(
7922 MF, MI, Ops[0], MachineOperand::CreateFI(FrameIndex), InsertPt, Size,
7923 Alignment, /*AllowCommute=*/true, CopyMI, VRM);
7924 };
7925 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
7926 unsigned NewOpc = 0;
7927 unsigned RCSize = 0;
7928 unsigned Opc = MI.getOpcode();
7929 switch (Opc) {
7930 default:
7931 // NDD can be folded into RMW though its Op0 and Op1 are not tied.
7932 return (Subtarget.hasNDD() ? X86::getNonNDVariant(Opc) : 0U) ? Impl()
7933 : nullptr;
7934 case X86::TEST8rr:
7935 NewOpc = X86::CMP8ri;
7936 RCSize = 1;
7937 break;
7938 case X86::TEST16rr:
7939 NewOpc = X86::CMP16ri;
7940 RCSize = 2;
7941 break;
7942 case X86::TEST32rr:
7943 NewOpc = X86::CMP32ri;
7944 RCSize = 4;
7945 break;
7946 case X86::TEST64rr:
7947 NewOpc = X86::CMP64ri32;
7948 RCSize = 8;
7949 break;
7950 }
7951 // Check if it's safe to fold the load. If the size of the object is
7952 // narrower than the load width, then it's not.
7953 if (Size < RCSize)
7954 return nullptr;
7955 // Change to CMPXXri r, 0 first.
7956 MI.setDesc(get(NewOpc));
7957 MI.getOperand(1).ChangeToImmediate(0);
7958 } else if (Ops.size() != 1)
7959 return nullptr;
7960
7961 return Impl();
7962}
7963
7964/// Check if \p LoadMI is a partial register load that we can't fold into \p MI
7965/// because the latter uses contents that wouldn't be defined in the folded
7966/// version. For instance, this transformation isn't legal:
7967/// movss (%rdi), %xmm0
7968/// addps %xmm0, %xmm0
7969/// ->
7970/// addps (%rdi), %xmm0
7971///
7972/// But this one is:
7973/// movss (%rdi), %xmm0
7974/// addss %xmm0, %xmm0
7975/// ->
7976/// addss (%rdi), %xmm0
7977///
7979 const MachineInstr &UserMI,
7980 const MachineFunction &MF) {
7981 unsigned Opc = LoadMI.getOpcode();
7982 unsigned UserOpc = UserMI.getOpcode();
7984 const TargetRegisterClass *RC =
7985 MF.getRegInfo().getRegClass(LoadMI.getOperand(0).getReg());
7986 unsigned RegSize = TRI.getRegSizeInBits(*RC);
7987
7988 if ((Opc == X86::MOVSSrm || Opc == X86::VMOVSSrm || Opc == X86::VMOVSSZrm ||
7989 Opc == X86::MOVSSrm_alt || Opc == X86::VMOVSSrm_alt ||
7990 Opc == X86::VMOVSSZrm_alt) &&
7991 RegSize > 32) {
7992 // These instructions only load 32 bits, we can't fold them if the
7993 // destination register is wider than 32 bits (4 bytes), and its user
7994 // instruction isn't scalar (SS).
7995 switch (UserOpc) {
7996 case X86::CVTSS2SDrr_Int:
7997 case X86::VCVTSS2SDrr_Int:
7998 case X86::VCVTSS2SDZrr_Int:
7999 case X86::VCVTSS2SDZrrk_Int:
8000 case X86::VCVTSS2SDZrrkz_Int:
8001 case X86::CVTSS2SIrr_Int:
8002 case X86::CVTSS2SI64rr_Int:
8003 case X86::VCVTSS2SIrr_Int:
8004 case X86::VCVTSS2SI64rr_Int:
8005 case X86::VCVTSS2SIZrr_Int:
8006 case X86::VCVTSS2SI64Zrr_Int:
8007 case X86::CVTTSS2SIrr_Int:
8008 case X86::CVTTSS2SI64rr_Int:
8009 case X86::VCVTTSS2SIrr_Int:
8010 case X86::VCVTTSS2SI64rr_Int:
8011 case X86::VCVTTSS2SIZrr_Int:
8012 case X86::VCVTTSS2SI64Zrr_Int:
8013 case X86::VCVTSS2USIZrr_Int:
8014 case X86::VCVTSS2USI64Zrr_Int:
8015 case X86::VCVTTSS2USIZrr_Int:
8016 case X86::VCVTTSS2USI64Zrr_Int:
8017 case X86::RCPSSr_Int:
8018 case X86::VRCPSSr_Int:
8019 case X86::RSQRTSSr_Int:
8020 case X86::VRSQRTSSr_Int:
8021 case X86::ROUNDSSri_Int:
8022 case X86::VROUNDSSri_Int:
8023 case X86::COMISSrr_Int:
8024 case X86::VCOMISSrr_Int:
8025 case X86::VCOMISSZrr_Int:
8026 case X86::UCOMISSrr_Int:
8027 case X86::VUCOMISSrr_Int:
8028 case X86::VUCOMISSZrr_Int:
8029 case X86::ADDSSrr_Int:
8030 case X86::VADDSSrr_Int:
8031 case X86::VADDSSZrr_Int:
8032 case X86::CMPSSrri_Int:
8033 case X86::VCMPSSrri_Int:
8034 case X86::VCMPSSZrri_Int:
8035 case X86::DIVSSrr_Int:
8036 case X86::VDIVSSrr_Int:
8037 case X86::VDIVSSZrr_Int:
8038 case X86::MAXSSrr_Int:
8039 case X86::VMAXSSrr_Int:
8040 case X86::VMAXSSZrr_Int:
8041 case X86::MINSSrr_Int:
8042 case X86::VMINSSrr_Int:
8043 case X86::VMINSSZrr_Int:
8044 case X86::MULSSrr_Int:
8045 case X86::VMULSSrr_Int:
8046 case X86::VMULSSZrr_Int:
8047 case X86::SQRTSSr_Int:
8048 case X86::VSQRTSSr_Int:
8049 case X86::VSQRTSSZr_Int:
8050 case X86::SUBSSrr_Int:
8051 case X86::VSUBSSrr_Int:
8052 case X86::VSUBSSZrr_Int:
8053 case X86::VADDSSZrrk_Int:
8054 case X86::VADDSSZrrkz_Int:
8055 case X86::VCMPSSZrrik_Int:
8056 case X86::VDIVSSZrrk_Int:
8057 case X86::VDIVSSZrrkz_Int:
8058 case X86::VMAXSSZrrk_Int:
8059 case X86::VMAXSSZrrkz_Int:
8060 case X86::VMINSSZrrk_Int:
8061 case X86::VMINSSZrrkz_Int:
8062 case X86::VMULSSZrrk_Int:
8063 case X86::VMULSSZrrkz_Int:
8064 case X86::VSQRTSSZrk_Int:
8065 case X86::VSQRTSSZrkz_Int:
8066 case X86::VSUBSSZrrk_Int:
8067 case X86::VSUBSSZrrkz_Int:
8068 case X86::VFMADDSS4rr_Int:
8069 case X86::VFNMADDSS4rr_Int:
8070 case X86::VFMSUBSS4rr_Int:
8071 case X86::VFNMSUBSS4rr_Int:
8072 case X86::VFMADD132SSr_Int:
8073 case X86::VFNMADD132SSr_Int:
8074 case X86::VFMADD213SSr_Int:
8075 case X86::VFNMADD213SSr_Int:
8076 case X86::VFMADD231SSr_Int:
8077 case X86::VFNMADD231SSr_Int:
8078 case X86::VFMSUB132SSr_Int:
8079 case X86::VFNMSUB132SSr_Int:
8080 case X86::VFMSUB213SSr_Int:
8081 case X86::VFNMSUB213SSr_Int:
8082 case X86::VFMSUB231SSr_Int:
8083 case X86::VFNMSUB231SSr_Int:
8084 case X86::VFMADD132SSZr_Int:
8085 case X86::VFNMADD132SSZr_Int:
8086 case X86::VFMADD213SSZr_Int:
8087 case X86::VFNMADD213SSZr_Int:
8088 case X86::VFMADD231SSZr_Int:
8089 case X86::VFNMADD231SSZr_Int:
8090 case X86::VFMSUB132SSZr_Int:
8091 case X86::VFNMSUB132SSZr_Int:
8092 case X86::VFMSUB213SSZr_Int:
8093 case X86::VFNMSUB213SSZr_Int:
8094 case X86::VFMSUB231SSZr_Int:
8095 case X86::VFNMSUB231SSZr_Int:
8096 case X86::VFMADD132SSZrk_Int:
8097 case X86::VFNMADD132SSZrk_Int:
8098 case X86::VFMADD213SSZrk_Int:
8099 case X86::VFNMADD213SSZrk_Int:
8100 case X86::VFMADD231SSZrk_Int:
8101 case X86::VFNMADD231SSZrk_Int:
8102 case X86::VFMSUB132SSZrk_Int:
8103 case X86::VFNMSUB132SSZrk_Int:
8104 case X86::VFMSUB213SSZrk_Int:
8105 case X86::VFNMSUB213SSZrk_Int:
8106 case X86::VFMSUB231SSZrk_Int:
8107 case X86::VFNMSUB231SSZrk_Int:
8108 case X86::VFMADD132SSZrkz_Int:
8109 case X86::VFNMADD132SSZrkz_Int:
8110 case X86::VFMADD213SSZrkz_Int:
8111 case X86::VFNMADD213SSZrkz_Int:
8112 case X86::VFMADD231SSZrkz_Int:
8113 case X86::VFNMADD231SSZrkz_Int:
8114 case X86::VFMSUB132SSZrkz_Int:
8115 case X86::VFNMSUB132SSZrkz_Int:
8116 case X86::VFMSUB213SSZrkz_Int:
8117 case X86::VFNMSUB213SSZrkz_Int:
8118 case X86::VFMSUB231SSZrkz_Int:
8119 case X86::VFNMSUB231SSZrkz_Int:
8120 case X86::VFIXUPIMMSSZrri:
8121 case X86::VFIXUPIMMSSZrrik:
8122 case X86::VFIXUPIMMSSZrrikz:
8123 case X86::VFPCLASSSSZri:
8124 case X86::VFPCLASSSSZrik:
8125 case X86::VGETEXPSSZr:
8126 case X86::VGETEXPSSZrk:
8127 case X86::VGETEXPSSZrkz:
8128 case X86::VGETMANTSSZrri:
8129 case X86::VGETMANTSSZrrik:
8130 case X86::VGETMANTSSZrrikz:
8131 case X86::VRANGESSZrri:
8132 case X86::VRANGESSZrrik:
8133 case X86::VRANGESSZrrikz:
8134 case X86::VRCP14SSZrr:
8135 case X86::VRCP14SSZrrk:
8136 case X86::VRCP14SSZrrkz:
8137 case X86::VRCP28SSZr:
8138 case X86::VRCP28SSZrk:
8139 case X86::VRCP28SSZrkz:
8140 case X86::VREDUCESSZrri:
8141 case X86::VREDUCESSZrrik:
8142 case X86::VREDUCESSZrrikz:
8143 case X86::VRNDSCALESSZrri_Int:
8144 case X86::VRNDSCALESSZrrik_Int:
8145 case X86::VRNDSCALESSZrrikz_Int:
8146 case X86::VRSQRT14SSZrr:
8147 case X86::VRSQRT14SSZrrk:
8148 case X86::VRSQRT14SSZrrkz:
8149 case X86::VRSQRT28SSZr:
8150 case X86::VRSQRT28SSZrk:
8151 case X86::VRSQRT28SSZrkz:
8152 case X86::VSCALEFSSZrr:
8153 case X86::VSCALEFSSZrrk:
8154 case X86::VSCALEFSSZrrkz:
8155 return false;
8156 default:
8157 return true;
8158 }
8159 }
8160
8161 if ((Opc == X86::MOVSDrm || Opc == X86::VMOVSDrm || Opc == X86::VMOVSDZrm ||
8162 Opc == X86::MOVSDrm_alt || Opc == X86::VMOVSDrm_alt ||
8163 Opc == X86::VMOVSDZrm_alt) &&
8164 RegSize > 64) {
8165 // These instructions only load 64 bits, we can't fold them if the
8166 // destination register is wider than 64 bits (8 bytes), and its user
8167 // instruction isn't scalar (SD).
8168 switch (UserOpc) {
8169 case X86::CVTSD2SSrr_Int:
8170 case X86::VCVTSD2SSrr_Int:
8171 case X86::VCVTSD2SSZrr_Int:
8172 case X86::VCVTSD2SSZrrk_Int:
8173 case X86::VCVTSD2SSZrrkz_Int:
8174 case X86::CVTSD2SIrr_Int:
8175 case X86::CVTSD2SI64rr_Int:
8176 case X86::VCVTSD2SIrr_Int:
8177 case X86::VCVTSD2SI64rr_Int:
8178 case X86::VCVTSD2SIZrr_Int:
8179 case X86::VCVTSD2SI64Zrr_Int:
8180 case X86::CVTTSD2SIrr_Int:
8181 case X86::CVTTSD2SI64rr_Int:
8182 case X86::VCVTTSD2SIrr_Int:
8183 case X86::VCVTTSD2SI64rr_Int:
8184 case X86::VCVTTSD2SIZrr_Int:
8185 case X86::VCVTTSD2SI64Zrr_Int:
8186 case X86::VCVTSD2USIZrr_Int:
8187 case X86::VCVTSD2USI64Zrr_Int:
8188 case X86::VCVTTSD2USIZrr_Int:
8189 case X86::VCVTTSD2USI64Zrr_Int:
8190 case X86::ROUNDSDri_Int:
8191 case X86::VROUNDSDri_Int:
8192 case X86::COMISDrr_Int:
8193 case X86::VCOMISDrr_Int:
8194 case X86::VCOMISDZrr_Int:
8195 case X86::UCOMISDrr_Int:
8196 case X86::VUCOMISDrr_Int:
8197 case X86::VUCOMISDZrr_Int:
8198 case X86::ADDSDrr_Int:
8199 case X86::VADDSDrr_Int:
8200 case X86::VADDSDZrr_Int:
8201 case X86::CMPSDrri_Int:
8202 case X86::VCMPSDrri_Int:
8203 case X86::VCMPSDZrri_Int:
8204 case X86::DIVSDrr_Int:
8205 case X86::VDIVSDrr_Int:
8206 case X86::VDIVSDZrr_Int:
8207 case X86::MAXSDrr_Int:
8208 case X86::VMAXSDrr_Int:
8209 case X86::VMAXSDZrr_Int:
8210 case X86::MINSDrr_Int:
8211 case X86::VMINSDrr_Int:
8212 case X86::VMINSDZrr_Int:
8213 case X86::MULSDrr_Int:
8214 case X86::VMULSDrr_Int:
8215 case X86::VMULSDZrr_Int:
8216 case X86::SQRTSDr_Int:
8217 case X86::VSQRTSDr_Int:
8218 case X86::VSQRTSDZr_Int:
8219 case X86::SUBSDrr_Int:
8220 case X86::VSUBSDrr_Int:
8221 case X86::VSUBSDZrr_Int:
8222 case X86::VADDSDZrrk_Int:
8223 case X86::VADDSDZrrkz_Int:
8224 case X86::VCMPSDZrrik_Int:
8225 case X86::VDIVSDZrrk_Int:
8226 case X86::VDIVSDZrrkz_Int:
8227 case X86::VMAXSDZrrk_Int:
8228 case X86::VMAXSDZrrkz_Int:
8229 case X86::VMINSDZrrk_Int:
8230 case X86::VMINSDZrrkz_Int:
8231 case X86::VMULSDZrrk_Int:
8232 case X86::VMULSDZrrkz_Int:
8233 case X86::VSQRTSDZrk_Int:
8234 case X86::VSQRTSDZrkz_Int:
8235 case X86::VSUBSDZrrk_Int:
8236 case X86::VSUBSDZrrkz_Int:
8237 case X86::VFMADDSD4rr_Int:
8238 case X86::VFNMADDSD4rr_Int:
8239 case X86::VFMSUBSD4rr_Int:
8240 case X86::VFNMSUBSD4rr_Int:
8241 case X86::VFMADD132SDr_Int:
8242 case X86::VFNMADD132SDr_Int:
8243 case X86::VFMADD213SDr_Int:
8244 case X86::VFNMADD213SDr_Int:
8245 case X86::VFMADD231SDr_Int:
8246 case X86::VFNMADD231SDr_Int:
8247 case X86::VFMSUB132SDr_Int:
8248 case X86::VFNMSUB132SDr_Int:
8249 case X86::VFMSUB213SDr_Int:
8250 case X86::VFNMSUB213SDr_Int:
8251 case X86::VFMSUB231SDr_Int:
8252 case X86::VFNMSUB231SDr_Int:
8253 case X86::VFMADD132SDZr_Int:
8254 case X86::VFNMADD132SDZr_Int:
8255 case X86::VFMADD213SDZr_Int:
8256 case X86::VFNMADD213SDZr_Int:
8257 case X86::VFMADD231SDZr_Int:
8258 case X86::VFNMADD231SDZr_Int:
8259 case X86::VFMSUB132SDZr_Int:
8260 case X86::VFNMSUB132SDZr_Int:
8261 case X86::VFMSUB213SDZr_Int:
8262 case X86::VFNMSUB213SDZr_Int:
8263 case X86::VFMSUB231SDZr_Int:
8264 case X86::VFNMSUB231SDZr_Int:
8265 case X86::VFMADD132SDZrk_Int:
8266 case X86::VFNMADD132SDZrk_Int:
8267 case X86::VFMADD213SDZrk_Int:
8268 case X86::VFNMADD213SDZrk_Int:
8269 case X86::VFMADD231SDZrk_Int:
8270 case X86::VFNMADD231SDZrk_Int:
8271 case X86::VFMSUB132SDZrk_Int:
8272 case X86::VFNMSUB132SDZrk_Int:
8273 case X86::VFMSUB213SDZrk_Int:
8274 case X86::VFNMSUB213SDZrk_Int:
8275 case X86::VFMSUB231SDZrk_Int:
8276 case X86::VFNMSUB231SDZrk_Int:
8277 case X86::VFMADD132SDZrkz_Int:
8278 case X86::VFNMADD132SDZrkz_Int:
8279 case X86::VFMADD213SDZrkz_Int:
8280 case X86::VFNMADD213SDZrkz_Int:
8281 case X86::VFMADD231SDZrkz_Int:
8282 case X86::VFNMADD231SDZrkz_Int:
8283 case X86::VFMSUB132SDZrkz_Int:
8284 case X86::VFNMSUB132SDZrkz_Int:
8285 case X86::VFMSUB213SDZrkz_Int:
8286 case X86::VFNMSUB213SDZrkz_Int:
8287 case X86::VFMSUB231SDZrkz_Int:
8288 case X86::VFNMSUB231SDZrkz_Int:
8289 case X86::VFIXUPIMMSDZrri:
8290 case X86::VFIXUPIMMSDZrrik:
8291 case X86::VFIXUPIMMSDZrrikz:
8292 case X86::VFPCLASSSDZri:
8293 case X86::VFPCLASSSDZrik:
8294 case X86::VGETEXPSDZr:
8295 case X86::VGETEXPSDZrk:
8296 case X86::VGETEXPSDZrkz:
8297 case X86::VGETMANTSDZrri:
8298 case X86::VGETMANTSDZrrik:
8299 case X86::VGETMANTSDZrrikz:
8300 case X86::VRANGESDZrri:
8301 case X86::VRANGESDZrrik:
8302 case X86::VRANGESDZrrikz:
8303 case X86::VRCP14SDZrr:
8304 case X86::VRCP14SDZrrk:
8305 case X86::VRCP14SDZrrkz:
8306 case X86::VRCP28SDZr:
8307 case X86::VRCP28SDZrk:
8308 case X86::VRCP28SDZrkz:
8309 case X86::VREDUCESDZrri:
8310 case X86::VREDUCESDZrrik:
8311 case X86::VREDUCESDZrrikz:
8312 case X86::VRNDSCALESDZrri_Int:
8313 case X86::VRNDSCALESDZrrik_Int:
8314 case X86::VRNDSCALESDZrrikz_Int:
8315 case X86::VRSQRT14SDZrr:
8316 case X86::VRSQRT14SDZrrk:
8317 case X86::VRSQRT14SDZrrkz:
8318 case X86::VRSQRT28SDZr:
8319 case X86::VRSQRT28SDZrk:
8320 case X86::VRSQRT28SDZrkz:
8321 case X86::VSCALEFSDZrr:
8322 case X86::VSCALEFSDZrrk:
8323 case X86::VSCALEFSDZrrkz:
8324 return false;
8325 default:
8326 return true;
8327 }
8328 }
8329
8330 if ((Opc == X86::VMOVSHZrm || Opc == X86::VMOVSHZrm_alt) && RegSize > 16) {
8331 // These instructions only load 16 bits, we can't fold them if the
8332 // destination register is wider than 16 bits (2 bytes), and its user
8333 // instruction isn't scalar (SH).
8334 switch (UserOpc) {
8335 case X86::VADDSHZrr_Int:
8336 case X86::VCMPSHZrri_Int:
8337 case X86::VDIVSHZrr_Int:
8338 case X86::VMAXSHZrr_Int:
8339 case X86::VMINSHZrr_Int:
8340 case X86::VMULSHZrr_Int:
8341 case X86::VSUBSHZrr_Int:
8342 case X86::VADDSHZrrk_Int:
8343 case X86::VADDSHZrrkz_Int:
8344 case X86::VCMPSHZrrik_Int:
8345 case X86::VDIVSHZrrk_Int:
8346 case X86::VDIVSHZrrkz_Int:
8347 case X86::VMAXSHZrrk_Int:
8348 case X86::VMAXSHZrrkz_Int:
8349 case X86::VMINSHZrrk_Int:
8350 case X86::VMINSHZrrkz_Int:
8351 case X86::VMULSHZrrk_Int:
8352 case X86::VMULSHZrrkz_Int:
8353 case X86::VSUBSHZrrk_Int:
8354 case X86::VSUBSHZrrkz_Int:
8355 case X86::VFMADD132SHZr_Int:
8356 case X86::VFNMADD132SHZr_Int:
8357 case X86::VFMADD213SHZr_Int:
8358 case X86::VFNMADD213SHZr_Int:
8359 case X86::VFMADD231SHZr_Int:
8360 case X86::VFNMADD231SHZr_Int:
8361 case X86::VFMSUB132SHZr_Int:
8362 case X86::VFNMSUB132SHZr_Int:
8363 case X86::VFMSUB213SHZr_Int:
8364 case X86::VFNMSUB213SHZr_Int:
8365 case X86::VFMSUB231SHZr_Int:
8366 case X86::VFNMSUB231SHZr_Int:
8367 case X86::VFMADD132SHZrk_Int:
8368 case X86::VFNMADD132SHZrk_Int:
8369 case X86::VFMADD213SHZrk_Int:
8370 case X86::VFNMADD213SHZrk_Int:
8371 case X86::VFMADD231SHZrk_Int:
8372 case X86::VFNMADD231SHZrk_Int:
8373 case X86::VFMSUB132SHZrk_Int:
8374 case X86::VFNMSUB132SHZrk_Int:
8375 case X86::VFMSUB213SHZrk_Int:
8376 case X86::VFNMSUB213SHZrk_Int:
8377 case X86::VFMSUB231SHZrk_Int:
8378 case X86::VFNMSUB231SHZrk_Int:
8379 case X86::VFMADD132SHZrkz_Int:
8380 case X86::VFNMADD132SHZrkz_Int:
8381 case X86::VFMADD213SHZrkz_Int:
8382 case X86::VFNMADD213SHZrkz_Int:
8383 case X86::VFMADD231SHZrkz_Int:
8384 case X86::VFNMADD231SHZrkz_Int:
8385 case X86::VFMSUB132SHZrkz_Int:
8386 case X86::VFNMSUB132SHZrkz_Int:
8387 case X86::VFMSUB213SHZrkz_Int:
8388 case X86::VFNMSUB213SHZrkz_Int:
8389 case X86::VFMSUB231SHZrkz_Int:
8390 case X86::VFNMSUB231SHZrkz_Int:
8391 return false;
8392 default:
8393 return true;
8394 }
8395 }
8396
8397 return false;
8398}
8399
8403 MachineInstr &LoadMI, MachineInstr *&CopyMI,
8404 LiveIntervals *LIS, VirtRegMap *VRM) const {
8406
8407 // If LoadMI is a masked load, check MI having the same mask.
8408 const MCInstrDesc &MCID = get(LoadMI.getOpcode());
8409 unsigned NumOps = MCID.getNumOperands();
8410 if (NumOps >= 3) {
8411 Register MaskReg;
8412 const MachineOperand &Op1 = LoadMI.getOperand(1);
8413 const MachineOperand &Op2 = LoadMI.getOperand(2);
8414
8415 auto IsVKWMClass = [](const TargetRegisterClass *RC) {
8416 return RC == &X86::VK2WMRegClass || RC == &X86::VK4WMRegClass ||
8417 RC == &X86::VK8WMRegClass || RC == &X86::VK16WMRegClass ||
8418 RC == &X86::VK32WMRegClass || RC == &X86::VK64WMRegClass;
8419 };
8420
8421 if (Op1.isReg() && IsVKWMClass(getRegClass(MCID, 1)))
8422 MaskReg = Op1.getReg();
8423 else if (Op2.isReg() && IsVKWMClass(getRegClass(MCID, 2)))
8424 MaskReg = Op2.getReg();
8425
8426 if (MaskReg) {
8427 // Some instructions are invalid to fold into even with the same mask.
8428 // Folding is unsafe if an active destination element may read from a
8429 // source element that is masked off.
8430 if (isNonFoldableWithSameMask(MI.getOpcode()))
8431 return nullptr;
8432 bool HasSameMask = false;
8433 for (unsigned I = 1, E = MI.getDesc().getNumOperands(); I < E; ++I) {
8434 const MachineOperand &Op = MI.getOperand(I);
8435 if (Op.isReg() && Op.getReg() == MaskReg) {
8436 HasSameMask = true;
8437 break;
8438 }
8439 }
8440 if (!HasSameMask)
8441 return nullptr;
8442 }
8443 }
8444
8445 // TODO: Support the case where LoadMI loads a wide register, but MI
8446 // only uses a subreg.
8447 for (auto Op : Ops) {
8448 if (MI.getOperand(Op).getSubReg())
8449 return nullptr;
8450 }
8451
8452 // If loading from a FrameIndex, fold directly from the FrameIndex.
8453 int FrameIndex;
8454 if (isLoadFromStackSlot(LoadMI, FrameIndex)) {
8455 if (isNonFoldablePartialRegisterLoad(LoadMI, MI, MF))
8456 return nullptr;
8457 return foldMemoryOperandImpl(MF, MI, Ops, FrameIndex, CopyMI, LIS, VRM);
8458 }
8459
8460 // Check switch flag
8461 if (NoFusing)
8462 return nullptr;
8463
8464 // Avoid partial and undef register update stalls unless optimizing for size.
8465 if (!MF.getFunction().hasOptSize() &&
8466 (hasPartialRegUpdate(MI.getOpcode(), Subtarget, /*ForLoadFold*/ true) ||
8468 return nullptr;
8469
8470 // Do not fold a NDD instruction and a memory instruction with relocation to
8471 // avoid emit APX relocation when the flag is disabled for backward
8472 // compatibility.
8473 uint64_t TSFlags = MI.getDesc().TSFlags;
8475 X86II::hasNewDataDest(TSFlags))
8476 return nullptr;
8477
8478 // Determine the alignment of the load.
8479 Align Alignment;
8480 unsigned LoadOpc = LoadMI.getOpcode();
8481 if (LoadMI.hasOneMemOperand())
8482 Alignment = (*LoadMI.memoperands_begin())->getAlign();
8483 else
8484 switch (LoadOpc) {
8485 case X86::AVX512_512_SETALLONES:
8486 Alignment = Align(64);
8487 break;
8488 case X86::AVX2_SETALLONES:
8489 case X86::AVX1_SETALLONES:
8490 case X86::AVX512_256_SETALLONES:
8491 Alignment = Align(32);
8492 break;
8493 case X86::V_SET0:
8494 case X86::V_SETALLONES:
8495 case X86::AVX512_128_SET0:
8496 case X86::FsFLD0F128:
8497 case X86::AVX512_FsFLD0F128:
8498 case X86::AVX512_128_SETALLONES:
8499 Alignment = Align(16);
8500 break;
8501 case X86::MMX_SET0:
8502 case X86::FsFLD0SD:
8503 case X86::AVX512_FsFLD0SD:
8504 Alignment = Align(8);
8505 break;
8506 case X86::FsFLD0SS:
8507 case X86::AVX512_FsFLD0SS:
8508 Alignment = Align(4);
8509 break;
8510 case X86::FsFLD0SH:
8511 case X86::AVX512_FsFLD0SH:
8512 Alignment = Align(2);
8513 break;
8514 default:
8515 return nullptr;
8516 }
8517 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
8518 unsigned NewOpc = 0;
8519 switch (MI.getOpcode()) {
8520 default:
8521 return nullptr;
8522 case X86::TEST8rr:
8523 NewOpc = X86::CMP8ri;
8524 break;
8525 case X86::TEST16rr:
8526 NewOpc = X86::CMP16ri;
8527 break;
8528 case X86::TEST32rr:
8529 NewOpc = X86::CMP32ri;
8530 break;
8531 case X86::TEST64rr:
8532 NewOpc = X86::CMP64ri32;
8533 break;
8534 }
8535 // Change to CMPXXri r, 0 first.
8536 MI.setDesc(get(NewOpc));
8537 MI.getOperand(1).ChangeToImmediate(0);
8538 } else if (Ops.size() != 1)
8539 return nullptr;
8540
8541 // Make sure the subregisters match.
8542 // Otherwise we risk changing the size of the load.
8543 if (LoadMI.getOperand(0).getSubReg() != MI.getOperand(Ops[0]).getSubReg())
8544 return nullptr;
8545
8547 switch (LoadOpc) {
8548 case X86::MMX_SET0:
8549 case X86::V_SET0:
8550 case X86::V_SETALLONES:
8551 case X86::AVX2_SETALLONES:
8552 case X86::AVX1_SETALLONES:
8553 case X86::AVX512_128_SET0:
8554 case X86::AVX512_128_SETALLONES:
8555 case X86::AVX512_256_SETALLONES:
8556 case X86::AVX512_512_SETALLONES:
8557 case X86::FsFLD0SH:
8558 case X86::AVX512_FsFLD0SH:
8559 case X86::FsFLD0SD:
8560 case X86::AVX512_FsFLD0SD:
8561 case X86::FsFLD0SS:
8562 case X86::AVX512_FsFLD0SS:
8563 case X86::FsFLD0F128:
8564 case X86::AVX512_FsFLD0F128: {
8565 // Folding a V_SET0 or V_SETALLONES as a load, to ease register pressure.
8566 // Create a constant-pool entry and operands to load from it.
8567
8568 // Large code model can't fold loads this way.
8570 return nullptr;
8571
8572 // x86-32 PIC requires a PIC base register for constant pools.
8573 unsigned PICBase = 0;
8574 // Since we're using Small or Kernel code model, we can always use
8575 // RIP-relative addressing for a smaller encoding.
8576 if (Subtarget.is64Bit()) {
8577 PICBase = X86::RIP;
8578 } else if (MF.getTarget().isPositionIndependent()) {
8579 // FIXME: PICBase = getGlobalBaseReg(&MF);
8580 // This doesn't work for several reasons.
8581 // 1. GlobalBaseReg may have been spilled.
8582 // 2. It may not be live at MI.
8583 return nullptr;
8584 }
8585
8586 // Create a constant-pool entry.
8588 Type *Ty;
8589 bool IsAllOnes = false;
8590 switch (LoadOpc) {
8591 case X86::FsFLD0SS:
8592 case X86::AVX512_FsFLD0SS:
8594 break;
8595 case X86::FsFLD0SD:
8596 case X86::AVX512_FsFLD0SD:
8598 break;
8599 case X86::FsFLD0F128:
8600 case X86::AVX512_FsFLD0F128:
8602 break;
8603 case X86::FsFLD0SH:
8604 case X86::AVX512_FsFLD0SH:
8606 break;
8607 case X86::AVX512_512_SETALLONES:
8608 IsAllOnes = true;
8609 [[fallthrough]];
8610 case X86::AVX1_SETALLONES:
8611 case X86::AVX2_SETALLONES:
8612 case X86::AVX512_256_SETALLONES:
8613 IsAllOnes = true;
8615 8);
8616
8617 break;
8618 case X86::MMX_SET0:
8620 2);
8621 break;
8622 case X86::V_SETALLONES:
8623 case X86::AVX512_128_SETALLONES:
8624 IsAllOnes = true;
8625 [[fallthrough]];
8626 case X86::V_SET0:
8627 case X86::AVX512_128_SET0:
8629 4);
8630 break;
8631 }
8632
8633 const Constant *C =
8635 unsigned CPI = MCP.getConstantPoolIndex(C, Alignment);
8636
8637 // Create operands to load from the constant pool entry.
8638 MOs.push_back(MachineOperand::CreateReg(PICBase, false));
8640 MOs.push_back(MachineOperand::CreateReg(0, false));
8642 MOs.push_back(MachineOperand::CreateReg(0, false));
8643 break;
8644 }
8645 case X86::VPBROADCASTBZ128rm:
8646 case X86::VPBROADCASTBZ256rm:
8647 case X86::VPBROADCASTBZrm:
8648 case X86::VBROADCASTF32X2Z256rm:
8649 case X86::VBROADCASTF32X2Zrm:
8650 case X86::VBROADCASTI32X2Z128rm:
8651 case X86::VBROADCASTI32X2Z256rm:
8652 case X86::VBROADCASTI32X2Zrm:
8653 // No instructions currently fuse with 8bits or 32bits x 2.
8654 return nullptr;
8655
8656#define FOLD_BROADCAST(SIZE) \
8657 MOs.append(LoadMI.operands_begin() + NumOps - X86::AddrNumOperands, \
8658 LoadMI.operands_begin() + NumOps); \
8659 return foldMemoryBroadcast(MF, MI, Ops[0], MOs, InsertPt, /*Size=*/SIZE, \
8660 /*AllowCommute=*/true);
8661 case X86::VPBROADCASTWZ128rm:
8662 case X86::VPBROADCASTWZ256rm:
8663 case X86::VPBROADCASTWZrm:
8664 FOLD_BROADCAST(16);
8665 case X86::VPBROADCASTDZ128rm:
8666 case X86::VPBROADCASTDZ256rm:
8667 case X86::VPBROADCASTDZrm:
8668 case X86::VBROADCASTSSZ128rm:
8669 case X86::VBROADCASTSSZ256rm:
8670 case X86::VBROADCASTSSZrm:
8671 FOLD_BROADCAST(32);
8672 case X86::VPBROADCASTQZ128rm:
8673 case X86::VPBROADCASTQZ256rm:
8674 case X86::VPBROADCASTQZrm:
8675 case X86::VBROADCASTSDZ256rm:
8676 case X86::VBROADCASTSDZrm:
8677 FOLD_BROADCAST(64);
8678 default: {
8679 if (isNonFoldablePartialRegisterLoad(LoadMI, MI, MF))
8680 return nullptr;
8681
8682 // Folding a normal load. Just copy the load's address operands.
8684 LoadMI.operands_begin() + NumOps);
8685 break;
8686 }
8687 }
8688 return foldMemoryOperandImpl(MF, MI, Ops[0], MOs, InsertPt,
8689 /*Size=*/0, Alignment, /*AllowCommute=*/true,
8690 CopyMI);
8691}
8692
8694X86InstrInfo::foldMemoryBroadcast(MachineFunction &MF, MachineInstr &MI,
8695 unsigned OpNum, ArrayRef<MachineOperand> MOs,
8697 unsigned BitsSize, bool AllowCommute) const {
8698
8699 if (auto *I = lookupBroadcastFoldTable(MI.getOpcode(), OpNum))
8700 return matchBroadcastSize(*I, BitsSize)
8701 ? fuseInst(MF, I->DstOp, OpNum, MOs, InsertPt, MI, *this)
8702 : nullptr;
8703
8704 if (AllowCommute) {
8705 // If the instruction and target operand are commutable, commute the
8706 // instruction and try again.
8707 unsigned CommuteOpIdx2 = commuteOperandsForFold(MI, OpNum);
8708 if (CommuteOpIdx2 == OpNum) {
8709 printFailMsgforFold(MI, OpNum);
8710 return nullptr;
8711 }
8712 MachineInstr *NewMI =
8713 foldMemoryBroadcast(MF, MI, CommuteOpIdx2, MOs, InsertPt, BitsSize,
8714 /*AllowCommute=*/false);
8715 if (NewMI)
8716 return NewMI;
8717 // Folding failed again - undo the commute before returning.
8718 commuteInstruction(MI, false, OpNum, CommuteOpIdx2);
8719 }
8720
8721 printFailMsgforFold(MI, OpNum);
8722 return nullptr;
8723}
8724
8728
8729 for (MachineMemOperand *MMO : MMOs) {
8730 if (!MMO->isLoad())
8731 continue;
8732
8733 if (!MMO->isStore()) {
8734 // Reuse the MMO.
8735 LoadMMOs.push_back(MMO);
8736 } else {
8737 // Clone the MMO and unset the store flag.
8738 LoadMMOs.push_back(MF.getMachineMemOperand(
8739 MMO, MMO->getFlags() & ~MachineMemOperand::MOStore));
8740 }
8741 }
8742
8743 return LoadMMOs;
8744}
8745
8749
8750 for (MachineMemOperand *MMO : MMOs) {
8751 if (!MMO->isStore())
8752 continue;
8753
8754 if (!MMO->isLoad()) {
8755 // Reuse the MMO.
8756 StoreMMOs.push_back(MMO);
8757 } else {
8758 // Clone the MMO and unset the load flag.
8759 StoreMMOs.push_back(MF.getMachineMemOperand(
8760 MMO, MMO->getFlags() & ~MachineMemOperand::MOLoad));
8761 }
8762 }
8763
8764 return StoreMMOs;
8765}
8766
8768 const TargetRegisterClass *RC,
8769 const X86Subtarget &STI) {
8770 assert(STI.hasAVX512() && "Expected at least AVX512!");
8771 unsigned SpillSize = STI.getRegisterInfo()->getSpillSize(*RC);
8772 assert((SpillSize == 64 || STI.hasVLX()) &&
8773 "Can't broadcast less than 64 bytes without AVX512VL!");
8774
8775#define CASE_BCAST_TYPE_OPC(TYPE, OP16, OP32, OP64) \
8776 case TYPE: \
8777 switch (SpillSize) { \
8778 default: \
8779 llvm_unreachable("Unknown spill size"); \
8780 case 16: \
8781 return X86::OP16; \
8782 case 32: \
8783 return X86::OP32; \
8784 case 64: \
8785 return X86::OP64; \
8786 } \
8787 break;
8788
8789 switch (I->Flags & TB_BCAST_MASK) {
8790 default:
8791 llvm_unreachable("Unexpected broadcast type!");
8792 CASE_BCAST_TYPE_OPC(TB_BCAST_W, VPBROADCASTWZ128rm, VPBROADCASTWZ256rm,
8793 VPBROADCASTWZrm)
8794 CASE_BCAST_TYPE_OPC(TB_BCAST_D, VPBROADCASTDZ128rm, VPBROADCASTDZ256rm,
8795 VPBROADCASTDZrm)
8796 CASE_BCAST_TYPE_OPC(TB_BCAST_Q, VPBROADCASTQZ128rm, VPBROADCASTQZ256rm,
8797 VPBROADCASTQZrm)
8798 CASE_BCAST_TYPE_OPC(TB_BCAST_SH, VPBROADCASTWZ128rm, VPBROADCASTWZ256rm,
8799 VPBROADCASTWZrm)
8800 CASE_BCAST_TYPE_OPC(TB_BCAST_SS, VBROADCASTSSZ128rm, VBROADCASTSSZ256rm,
8801 VBROADCASTSSZrm)
8802 CASE_BCAST_TYPE_OPC(TB_BCAST_SD, VMOVDDUPZ128rm, VBROADCASTSDZ256rm,
8803 VBROADCASTSDZrm)
8804 }
8805}
8806
8808 MachineFunction &MF, MachineInstr &MI, Register Reg, bool UnfoldLoad,
8809 bool UnfoldStore, SmallVectorImpl<MachineInstr *> &NewMIs) const {
8810 const X86FoldTableEntry *I = lookupUnfoldTable(MI.getOpcode());
8811 if (I == nullptr)
8812 return false;
8813 unsigned Opc = I->DstOp;
8814 unsigned Index = I->Flags & TB_INDEX_MASK;
8815 bool FoldedLoad = I->Flags & TB_FOLDED_LOAD;
8816 bool FoldedStore = I->Flags & TB_FOLDED_STORE;
8817 if (UnfoldLoad && !FoldedLoad)
8818 return false;
8819 UnfoldLoad &= FoldedLoad;
8820 if (UnfoldStore && !FoldedStore)
8821 return false;
8822 UnfoldStore &= FoldedStore;
8823
8824 const MCInstrDesc &MCID = get(Opc);
8825
8826 const TargetRegisterClass *RC = getRegClass(MCID, Index);
8828 // TODO: Check if 32-byte or greater accesses are slow too?
8829 if (!MI.hasOneMemOperand() && RC == &X86::VR128RegClass &&
8830 Subtarget.isUnalignedMem16Slow())
8831 // Without memoperands, loadRegFromAddr and storeRegToStackSlot will
8832 // conservatively assume the address is unaligned. That's bad for
8833 // performance.
8834 return false;
8839 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
8840 MachineOperand &Op = MI.getOperand(i);
8841 if (i >= Index && i < Index + X86::AddrNumOperands)
8842 AddrOps.push_back(Op);
8843 else if (Op.isReg() && Op.isImplicit())
8844 ImpOps.push_back(Op);
8845 else if (i < Index)
8846 BeforeOps.push_back(Op);
8847 else if (i > Index)
8848 AfterOps.push_back(Op);
8849 }
8850
8851 // Emit the load or broadcast instruction.
8852 if (UnfoldLoad) {
8853 auto MMOs = extractLoadMMOs(MI.memoperands(), MF);
8854
8855 unsigned Opc;
8856 if (I->Flags & TB_BCAST_MASK) {
8857 Opc = getBroadcastOpcode(I, RC, Subtarget);
8858 } else {
8859 unsigned Alignment = std::max<uint32_t>(TRI.getSpillSize(*RC), 16);
8860 bool isAligned = !MMOs.empty() && MMOs.front()->getAlign() >= Alignment;
8861 Opc = getLoadRegOpcode(Reg, RC, isAligned, Subtarget);
8862 }
8863
8864 DebugLoc DL;
8865 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), Reg);
8866 for (const MachineOperand &AddrOp : AddrOps)
8867 MIB.add(AddrOp);
8868 MIB.setMemRefs(MMOs);
8869 NewMIs.push_back(MIB);
8870
8871 if (UnfoldStore) {
8872 // Address operands cannot be marked isKill.
8873 for (unsigned i = 1; i != 1 + X86::AddrNumOperands; ++i) {
8874 MachineOperand &MO = NewMIs[0]->getOperand(i);
8875 if (MO.isReg())
8876 MO.setIsKill(false);
8877 }
8878 }
8879 }
8880
8881 // Emit the data processing instruction.
8882 MachineInstr *DataMI = MF.CreateMachineInstr(MCID, MI.getDebugLoc(), true);
8883 MachineInstrBuilder MIB(MF, DataMI);
8884
8885 if (FoldedStore)
8886 MIB.addReg(Reg, RegState::Define);
8887 for (MachineOperand &BeforeOp : BeforeOps)
8888 MIB.add(BeforeOp);
8889 if (FoldedLoad)
8890 MIB.addReg(Reg);
8891 for (MachineOperand &AfterOp : AfterOps)
8892 MIB.add(AfterOp);
8893 for (MachineOperand &ImpOp : ImpOps) {
8894 MIB.addReg(ImpOp.getReg(), getDefRegState(ImpOp.isDef()) |
8896 getKillRegState(ImpOp.isKill()) |
8897 getDeadRegState(ImpOp.isDead()) |
8898 getUndefRegState(ImpOp.isUndef()));
8899 }
8900 // Change CMP32ri r, 0 back to TEST32rr r, r, etc.
8901 switch (DataMI->getOpcode()) {
8902 default:
8903 break;
8904 case X86::CMP64ri32:
8905 case X86::CMP32ri:
8906 case X86::CMP16ri:
8907 case X86::CMP8ri: {
8908 MachineOperand &MO0 = DataMI->getOperand(0);
8909 MachineOperand &MO1 = DataMI->getOperand(1);
8910 if (MO1.isImm() && MO1.getImm() == 0) {
8911 unsigned NewOpc;
8912 switch (DataMI->getOpcode()) {
8913 default:
8914 llvm_unreachable("Unreachable!");
8915 case X86::CMP64ri32:
8916 NewOpc = X86::TEST64rr;
8917 break;
8918 case X86::CMP32ri:
8919 NewOpc = X86::TEST32rr;
8920 break;
8921 case X86::CMP16ri:
8922 NewOpc = X86::TEST16rr;
8923 break;
8924 case X86::CMP8ri:
8925 NewOpc = X86::TEST8rr;
8926 break;
8927 }
8928 DataMI->setDesc(get(NewOpc));
8929 MO1.ChangeToRegister(MO0.getReg(), false);
8930 }
8931 }
8932 }
8933 NewMIs.push_back(DataMI);
8934
8935 // Emit the store instruction.
8936 if (UnfoldStore) {
8937 const TargetRegisterClass *DstRC = getRegClass(MCID, 0);
8938 auto MMOs = extractStoreMMOs(MI.memoperands(), MF);
8939 unsigned Alignment = std::max<uint32_t>(TRI.getSpillSize(*DstRC), 16);
8940 bool isAligned = !MMOs.empty() && MMOs.front()->getAlign() >= Alignment;
8941 unsigned Opc = getStoreRegOpcode(Reg, DstRC, isAligned, Subtarget);
8942 DebugLoc DL;
8943 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc));
8944 for (const MachineOperand &AddrOp : AddrOps)
8945 MIB.add(AddrOp);
8946 MIB.addReg(Reg, RegState::Kill);
8947 MIB.setMemRefs(MMOs);
8948 NewMIs.push_back(MIB);
8949 }
8950
8951 return true;
8952}
8953
8955 SelectionDAG &DAG, SDNode *N, SmallVectorImpl<SDNode *> &NewNodes) const {
8956 if (!N->isMachineOpcode())
8957 return false;
8958
8959 const X86FoldTableEntry *I = lookupUnfoldTable(N->getMachineOpcode());
8960 if (I == nullptr)
8961 return false;
8962 unsigned Opc = I->DstOp;
8963 unsigned Index = I->Flags & TB_INDEX_MASK;
8964 bool FoldedLoad = I->Flags & TB_FOLDED_LOAD;
8965 bool FoldedStore = I->Flags & TB_FOLDED_STORE;
8966 const MCInstrDesc &MCID = get(Opc);
8969 const TargetRegisterClass *RC = getRegClass(MCID, Index);
8970 unsigned NumDefs = MCID.NumDefs;
8971 std::vector<SDValue> AddrOps;
8972 std::vector<SDValue> BeforeOps;
8973 std::vector<SDValue> AfterOps;
8974 SDLoc dl(N);
8975 unsigned NumOps = N->getNumOperands();
8976 for (unsigned i = 0; i != NumOps - 1; ++i) {
8977 SDValue Op = N->getOperand(i);
8978 if (i >= Index - NumDefs && i < Index - NumDefs + X86::AddrNumOperands)
8979 AddrOps.push_back(Op);
8980 else if (i < Index - NumDefs)
8981 BeforeOps.push_back(Op);
8982 else if (i > Index - NumDefs)
8983 AfterOps.push_back(Op);
8984 }
8985 SDValue Chain = N->getOperand(NumOps - 1);
8986 AddrOps.push_back(Chain);
8987
8988 // Emit the load instruction.
8989 SDNode *Load = nullptr;
8990 if (FoldedLoad) {
8991 EVT VT = *TRI.legalclasstypes_begin(*RC);
8992 auto MMOs = extractLoadMMOs(cast<MachineSDNode>(N)->memoperands(), MF);
8993 if (MMOs.empty() && RC == &X86::VR128RegClass &&
8994 Subtarget.isUnalignedMem16Slow())
8995 // Do not introduce a slow unaligned load.
8996 return false;
8997 // FIXME: If a VR128 can have size 32, we should be checking if a 32-byte
8998 // memory access is slow above.
8999
9000 unsigned Opc;
9001 if (I->Flags & TB_BCAST_MASK) {
9002 Opc = getBroadcastOpcode(I, RC, Subtarget);
9003 } else {
9004 unsigned Alignment = std::max<uint32_t>(TRI.getSpillSize(*RC), 16);
9005 bool isAligned = !MMOs.empty() && MMOs.front()->getAlign() >= Alignment;
9006 Opc = getLoadRegOpcode(0, RC, isAligned, Subtarget);
9007 }
9008
9009 Load = DAG.getMachineNode(Opc, dl, VT, MVT::Other, AddrOps);
9010 NewNodes.push_back(Load);
9011
9012 // Preserve memory reference information.
9014 }
9015
9016 // Emit the data processing instruction.
9017 std::vector<EVT> VTs;
9018 const TargetRegisterClass *DstRC = nullptr;
9019 if (MCID.getNumDefs() > 0) {
9020 DstRC = getRegClass(MCID, 0);
9021 VTs.push_back(*TRI.legalclasstypes_begin(*DstRC));
9022 }
9023 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
9024 EVT VT = N->getValueType(i);
9025 if (VT != MVT::Other && i >= (unsigned)MCID.getNumDefs())
9026 VTs.push_back(VT);
9027 }
9028 if (Load)
9029 BeforeOps.push_back(SDValue(Load, 0));
9030 llvm::append_range(BeforeOps, AfterOps);
9031 // Change CMP32ri r, 0 back to TEST32rr r, r, etc.
9032 switch (Opc) {
9033 default:
9034 break;
9035 case X86::CMP64ri32:
9036 case X86::CMP32ri:
9037 case X86::CMP16ri:
9038 case X86::CMP8ri:
9039 if (isNullConstant(BeforeOps[1])) {
9040 switch (Opc) {
9041 default:
9042 llvm_unreachable("Unreachable!");
9043 case X86::CMP64ri32:
9044 Opc = X86::TEST64rr;
9045 break;
9046 case X86::CMP32ri:
9047 Opc = X86::TEST32rr;
9048 break;
9049 case X86::CMP16ri:
9050 Opc = X86::TEST16rr;
9051 break;
9052 case X86::CMP8ri:
9053 Opc = X86::TEST8rr;
9054 break;
9055 }
9056 BeforeOps[1] = BeforeOps[0];
9057 }
9058 }
9059 SDNode *NewNode = DAG.getMachineNode(Opc, dl, VTs, BeforeOps);
9060 NewNodes.push_back(NewNode);
9061
9062 // Emit the store instruction.
9063 if (FoldedStore) {
9064 AddrOps.pop_back();
9065 AddrOps.push_back(SDValue(NewNode, 0));
9066 AddrOps.push_back(Chain);
9067 auto MMOs = extractStoreMMOs(cast<MachineSDNode>(N)->memoperands(), MF);
9068 if (MMOs.empty() && RC == &X86::VR128RegClass &&
9069 Subtarget.isUnalignedMem16Slow())
9070 // Do not introduce a slow unaligned store.
9071 return false;
9072 // FIXME: If a VR128 can have size 32, we should be checking if a 32-byte
9073 // memory access is slow above.
9074 unsigned Alignment = std::max<uint32_t>(TRI.getSpillSize(*RC), 16);
9075 bool isAligned = !MMOs.empty() && MMOs.front()->getAlign() >= Alignment;
9076 SDNode *Store =
9077 DAG.getMachineNode(getStoreRegOpcode(0, DstRC, isAligned, Subtarget),
9078 dl, MVT::Other, AddrOps);
9079 NewNodes.push_back(Store);
9080
9081 // Preserve memory reference information.
9083 }
9084
9085 return true;
9086}
9087
9088unsigned
9090 bool UnfoldStore,
9091 unsigned *LoadRegIndex) const {
9093 if (I == nullptr)
9094 return 0;
9095 bool FoldedLoad = I->Flags & TB_FOLDED_LOAD;
9096 bool FoldedStore = I->Flags & TB_FOLDED_STORE;
9097 if (UnfoldLoad && !FoldedLoad)
9098 return 0;
9099 if (UnfoldStore && !FoldedStore)
9100 return 0;
9101 if (LoadRegIndex)
9102 *LoadRegIndex = I->Flags & TB_INDEX_MASK;
9103 return I->DstOp;
9104}
9105
9107 int64_t &Offset1,
9108 int64_t &Offset2) const {
9109 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
9110 return false;
9111
9112 auto IsLoadOpcode = [&](unsigned Opcode) {
9113 switch (Opcode) {
9114 default:
9115 return false;
9116 case X86::MOV8rm:
9117 case X86::MOV16rm:
9118 case X86::MOV32rm:
9119 case X86::MOV64rm:
9120 case X86::LD_Fp32m:
9121 case X86::LD_Fp64m:
9122 case X86::LD_Fp80m:
9123 case X86::MOVSSrm:
9124 case X86::MOVSSrm_alt:
9125 case X86::MOVSDrm:
9126 case X86::MOVSDrm_alt:
9127 case X86::MMX_MOVD64rm:
9128 case X86::MMX_MOVQ64rm:
9129 case X86::MOVAPSrm:
9130 case X86::MOVUPSrm:
9131 case X86::MOVAPDrm:
9132 case X86::MOVUPDrm:
9133 case X86::MOVDQArm:
9134 case X86::MOVDQUrm:
9135 // AVX load instructions
9136 case X86::VMOVSSrm:
9137 case X86::VMOVSSrm_alt:
9138 case X86::VMOVSDrm:
9139 case X86::VMOVSDrm_alt:
9140 case X86::VMOVAPSrm:
9141 case X86::VMOVUPSrm:
9142 case X86::VMOVAPDrm:
9143 case X86::VMOVUPDrm:
9144 case X86::VMOVDQArm:
9145 case X86::VMOVDQUrm:
9146 case X86::VMOVAPSYrm:
9147 case X86::VMOVUPSYrm:
9148 case X86::VMOVAPDYrm:
9149 case X86::VMOVUPDYrm:
9150 case X86::VMOVDQAYrm:
9151 case X86::VMOVDQUYrm:
9152 // AVX512 load instructions
9153 case X86::VMOVSSZrm:
9154 case X86::VMOVSSZrm_alt:
9155 case X86::VMOVSDZrm:
9156 case X86::VMOVSDZrm_alt:
9157 case X86::VMOVAPSZ128rm:
9158 case X86::VMOVUPSZ128rm:
9159 case X86::VMOVAPSZ128rm_NOVLX:
9160 case X86::VMOVUPSZ128rm_NOVLX:
9161 case X86::VMOVAPDZ128rm:
9162 case X86::VMOVUPDZ128rm:
9163 case X86::VMOVDQU8Z128rm:
9164 case X86::VMOVDQU16Z128rm:
9165 case X86::VMOVDQA32Z128rm:
9166 case X86::VMOVDQU32Z128rm:
9167 case X86::VMOVDQA64Z128rm:
9168 case X86::VMOVDQU64Z128rm:
9169 case X86::VMOVAPSZ256rm:
9170 case X86::VMOVUPSZ256rm:
9171 case X86::VMOVAPSZ256rm_NOVLX:
9172 case X86::VMOVUPSZ256rm_NOVLX:
9173 case X86::VMOVAPDZ256rm:
9174 case X86::VMOVUPDZ256rm:
9175 case X86::VMOVDQU8Z256rm:
9176 case X86::VMOVDQU16Z256rm:
9177 case X86::VMOVDQA32Z256rm:
9178 case X86::VMOVDQU32Z256rm:
9179 case X86::VMOVDQA64Z256rm:
9180 case X86::VMOVDQU64Z256rm:
9181 case X86::VMOVAPSZrm:
9182 case X86::VMOVUPSZrm:
9183 case X86::VMOVAPDZrm:
9184 case X86::VMOVUPDZrm:
9185 case X86::VMOVDQU8Zrm:
9186 case X86::VMOVDQU16Zrm:
9187 case X86::VMOVDQA32Zrm:
9188 case X86::VMOVDQU32Zrm:
9189 case X86::VMOVDQA64Zrm:
9190 case X86::VMOVDQU64Zrm:
9191 case X86::KMOVBkm:
9192 case X86::KMOVBkm_EVEX:
9193 case X86::KMOVWkm:
9194 case X86::KMOVWkm_EVEX:
9195 case X86::KMOVDkm:
9196 case X86::KMOVDkm_EVEX:
9197 case X86::KMOVQkm:
9198 case X86::KMOVQkm_EVEX:
9199 return true;
9200 }
9201 };
9202
9203 if (!IsLoadOpcode(Load1->getMachineOpcode()) ||
9204 !IsLoadOpcode(Load2->getMachineOpcode()))
9205 return false;
9206
9207 // Lambda to check if both the loads have the same value for an operand index.
9208 auto HasSameOp = [&](int I) {
9209 return Load1->getOperand(I) == Load2->getOperand(I);
9210 };
9211
9212 // All operands except the displacement should match.
9213 if (!HasSameOp(X86::AddrBaseReg) || !HasSameOp(X86::AddrScaleAmt) ||
9214 !HasSameOp(X86::AddrIndexReg) || !HasSameOp(X86::AddrSegmentReg))
9215 return false;
9216
9217 // Chain Operand must be the same.
9218 if (!HasSameOp(5))
9219 return false;
9220
9221 // Now let's examine if the displacements are constants.
9224 if (!Disp1 || !Disp2)
9225 return false;
9226
9227 Offset1 = Disp1->getSExtValue();
9228 Offset2 = Disp2->getSExtValue();
9229 return true;
9230}
9231
9233 int64_t Offset1, int64_t Offset2,
9234 unsigned NumLoads) const {
9235 assert(Offset2 > Offset1);
9236 if ((Offset2 - Offset1) / 8 > 64)
9237 return false;
9238
9239 unsigned Opc1 = Load1->getMachineOpcode();
9240 unsigned Opc2 = Load2->getMachineOpcode();
9241 if (Opc1 != Opc2)
9242 return false; // FIXME: overly conservative?
9243
9244 switch (Opc1) {
9245 default:
9246 break;
9247 case X86::LD_Fp32m:
9248 case X86::LD_Fp64m:
9249 case X86::LD_Fp80m:
9250 case X86::MMX_MOVD64rm:
9251 case X86::MMX_MOVQ64rm:
9252 return false;
9253 }
9254
9255 EVT VT = Load1->getValueType(0);
9256 switch (VT.getSimpleVT().SimpleTy) {
9257 default:
9258 // XMM registers. In 64-bit mode we can be a bit more aggressive since we
9259 // have 16 of them to play with.
9260 if (Subtarget.is64Bit()) {
9261 if (NumLoads >= 3)
9262 return false;
9263 } else if (NumLoads) {
9264 return false;
9265 }
9266 break;
9267 case MVT::i8:
9268 case MVT::i16:
9269 case MVT::i32:
9270 case MVT::i64:
9271 case MVT::f32:
9272 case MVT::f64:
9273 if (NumLoads)
9274 return false;
9275 break;
9276 }
9277
9278 return true;
9279}
9280
9282 const MachineBasicBlock *MBB,
9283 const MachineFunction &MF) const {
9284
9285 // ENDBR instructions should not be scheduled around.
9286 unsigned Opcode = MI.getOpcode();
9287 if (Opcode == X86::ENDBR64 || Opcode == X86::ENDBR32 ||
9288 Opcode == X86::PLDTILECFGV)
9289 return true;
9290
9291 // Frame setup and destroy can't be scheduled around.
9292 if (MI.getFlag(MachineInstr::FrameSetup) ||
9294 return true;
9295
9297}
9298
9301 assert(Cond.size() == 1 && "Invalid X86 branch condition!");
9302 X86::CondCode CC = static_cast<X86::CondCode>(Cond[0].getImm());
9303 Cond[0].setImm(GetOppositeBranchCondition(CC));
9304 return false;
9305}
9306
9308 const TargetRegisterClass *RC) const {
9309 // FIXME: Return false for x87 stack register classes for now. We can't
9310 // allow any loads of these registers before FpGet_ST0_80.
9311 return !(RC == &X86::CCRRegClass || RC == &X86::DFCCRRegClass ||
9312 RC == &X86::RFP32RegClass || RC == &X86::RFP64RegClass ||
9313 RC == &X86::RFP80RegClass);
9314}
9315
9316/// Return a virtual register initialized with the
9317/// the global base register value. Output instructions required to
9318/// initialize the register in the function entry block, if necessary.
9319///
9320/// TODO: Eliminate this and move the code to X86MachineFunctionInfo.
9321///
9324 Register GlobalBaseReg = X86FI->getGlobalBaseReg();
9325 if (GlobalBaseReg)
9326 return GlobalBaseReg;
9327
9328 // Create the register. The code to initialize it is inserted
9329 // later, by the CGBR pass (below).
9330 MachineRegisterInfo &RegInfo = MF->getRegInfo();
9331 GlobalBaseReg = RegInfo.createVirtualRegister(
9332 Subtarget.is64Bit() ? &X86::GR64_NOSPRegClass : &X86::GR32_NOSPRegClass);
9333 X86FI->setGlobalBaseReg(GlobalBaseReg);
9334 return GlobalBaseReg;
9335}
9336
9337// FIXME: Some shuffle and unpack instructions have equivalents in different
9338// domains, but they require a bit more work than just switching opcodes.
9339
9340static const uint16_t *lookup(unsigned opcode, unsigned domain,
9341 ArrayRef<uint16_t[3]> Table) {
9342 for (const uint16_t(&Row)[3] : Table)
9343 if (Row[domain - 1] == opcode)
9344 return Row;
9345 return nullptr;
9346}
9347
9348static const uint16_t *lookupAVX512(unsigned opcode, unsigned domain,
9349 ArrayRef<uint16_t[4]> Table) {
9350 // If this is the integer domain make sure to check both integer columns.
9351 for (const uint16_t(&Row)[4] : Table)
9352 if (Row[domain - 1] == opcode || (domain == 3 && Row[3] == opcode))
9353 return Row;
9354 return nullptr;
9355}
9356
9357// Helper to attempt to widen/narrow blend masks.
9358static bool AdjustBlendMask(unsigned OldMask, unsigned OldWidth,
9359 unsigned NewWidth, unsigned *pNewMask = nullptr) {
9360 assert(((OldWidth % NewWidth) == 0 || (NewWidth % OldWidth) == 0) &&
9361 "Illegal blend mask scale");
9362 unsigned NewMask = 0;
9363
9364 if ((OldWidth % NewWidth) == 0) {
9365 unsigned Scale = OldWidth / NewWidth;
9366 unsigned SubMask = (1u << Scale) - 1;
9367 for (unsigned i = 0; i != NewWidth; ++i) {
9368 unsigned Sub = (OldMask >> (i * Scale)) & SubMask;
9369 if (Sub == SubMask)
9370 NewMask |= (1u << i);
9371 else if (Sub != 0x0)
9372 return false;
9373 }
9374 } else {
9375 unsigned Scale = NewWidth / OldWidth;
9376 unsigned SubMask = (1u << Scale) - 1;
9377 for (unsigned i = 0; i != OldWidth; ++i) {
9378 if (OldMask & (1 << i)) {
9379 NewMask |= (SubMask << (i * Scale));
9380 }
9381 }
9382 }
9383
9384 if (pNewMask)
9385 *pNewMask = NewMask;
9386 return true;
9387}
9388
9390 unsigned Opcode = MI.getOpcode();
9391 unsigned NumOperands = MI.getDesc().getNumOperands();
9392
9393 auto GetBlendDomains = [&](unsigned ImmWidth, bool Is256) {
9394 uint16_t validDomains = 0;
9395 if (MI.getOperand(NumOperands - 1).isImm()) {
9396 unsigned Imm = MI.getOperand(NumOperands - 1).getImm();
9397 if (AdjustBlendMask(Imm, ImmWidth, Is256 ? 8 : 4))
9398 validDomains |= 0x2; // PackedSingle
9399 if (AdjustBlendMask(Imm, ImmWidth, Is256 ? 4 : 2))
9400 validDomains |= 0x4; // PackedDouble
9401 if (!Is256 || Subtarget.hasAVX2())
9402 validDomains |= 0x8; // PackedInt
9403 }
9404 return validDomains;
9405 };
9406
9407 switch (Opcode) {
9408 case X86::BLENDPDrmi:
9409 case X86::BLENDPDrri:
9410 case X86::VBLENDPDrmi:
9411 case X86::VBLENDPDrri:
9412 return GetBlendDomains(2, false);
9413 case X86::VBLENDPDYrmi:
9414 case X86::VBLENDPDYrri:
9415 return GetBlendDomains(4, true);
9416 case X86::BLENDPSrmi:
9417 case X86::BLENDPSrri:
9418 case X86::VBLENDPSrmi:
9419 case X86::VBLENDPSrri:
9420 case X86::VPBLENDDrmi:
9421 case X86::VPBLENDDrri:
9422 return GetBlendDomains(4, false);
9423 case X86::VBLENDPSYrmi:
9424 case X86::VBLENDPSYrri:
9425 case X86::VPBLENDDYrmi:
9426 case X86::VPBLENDDYrri:
9427 return GetBlendDomains(8, true);
9428 case X86::PBLENDWrmi:
9429 case X86::PBLENDWrri:
9430 case X86::VPBLENDWrmi:
9431 case X86::VPBLENDWrri:
9432 // Treat VPBLENDWY as a 128-bit vector as it repeats the lo/hi masks.
9433 case X86::VPBLENDWYrmi:
9434 case X86::VPBLENDWYrri:
9435 return GetBlendDomains(8, false);
9436 case X86::VPANDDZ128rr:
9437 case X86::VPANDDZ128rm:
9438 case X86::VPANDDZ256rr:
9439 case X86::VPANDDZ256rm:
9440 case X86::VPANDQZ128rr:
9441 case X86::VPANDQZ128rm:
9442 case X86::VPANDQZ256rr:
9443 case X86::VPANDQZ256rm:
9444 case X86::VPANDNDZ128rr:
9445 case X86::VPANDNDZ128rm:
9446 case X86::VPANDNDZ256rr:
9447 case X86::VPANDNDZ256rm:
9448 case X86::VPANDNQZ128rr:
9449 case X86::VPANDNQZ128rm:
9450 case X86::VPANDNQZ256rr:
9451 case X86::VPANDNQZ256rm:
9452 case X86::VPORDZ128rr:
9453 case X86::VPORDZ128rm:
9454 case X86::VPORDZ256rr:
9455 case X86::VPORDZ256rm:
9456 case X86::VPORQZ128rr:
9457 case X86::VPORQZ128rm:
9458 case X86::VPORQZ256rr:
9459 case X86::VPORQZ256rm:
9460 case X86::VPXORDZ128rr:
9461 case X86::VPXORDZ128rm:
9462 case X86::VPXORDZ256rr:
9463 case X86::VPXORDZ256rm:
9464 case X86::VPXORQZ128rr:
9465 case X86::VPXORQZ128rm:
9466 case X86::VPXORQZ256rr:
9467 case X86::VPXORQZ256rm:
9468 // If we don't have DQI see if we can still switch from an EVEX integer
9469 // instruction to a VEX floating point instruction.
9470 if (Subtarget.hasDQI())
9471 return 0;
9472
9473 if (RI.getEncodingValue(MI.getOperand(0).getReg()) >= 16)
9474 return 0;
9475 if (RI.getEncodingValue(MI.getOperand(1).getReg()) >= 16)
9476 return 0;
9477 // Register forms will have 3 operands. Memory form will have more.
9478 if (NumOperands == 3 &&
9479 RI.getEncodingValue(MI.getOperand(2).getReg()) >= 16)
9480 return 0;
9481
9482 // All domains are valid.
9483 return 0xe;
9484 case X86::MOVHLPSrr:
9485 // We can swap domains when both inputs are the same register.
9486 // FIXME: This doesn't catch all the cases we would like. If the input
9487 // register isn't KILLed by the instruction, the two address instruction
9488 // pass puts a COPY on one input. The other input uses the original
9489 // register. This prevents the same physical register from being used by
9490 // both inputs.
9491 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg() &&
9492 MI.getOperand(0).getSubReg() == 0 &&
9493 MI.getOperand(1).getSubReg() == 0 && MI.getOperand(2).getSubReg() == 0)
9494 return 0x6;
9495 return 0;
9496 case X86::SHUFPDrri:
9497 return 0x6;
9498 }
9499 return 0;
9500}
9501
9502#include "X86ReplaceableInstrs.def"
9503
9505 unsigned Domain) const {
9506 assert(Domain > 0 && Domain < 4 && "Invalid execution domain");
9507 uint16_t dom = (MI.getDesc().TSFlags >> X86II::SSEDomainShift) & 3;
9508 assert(dom && "Not an SSE instruction");
9509
9510 unsigned Opcode = MI.getOpcode();
9511 unsigned NumOperands = MI.getDesc().getNumOperands();
9512
9513 auto SetBlendDomain = [&](unsigned ImmWidth, bool Is256) {
9514 if (MI.getOperand(NumOperands - 1).isImm()) {
9515 unsigned Imm = MI.getOperand(NumOperands - 1).getImm() & 255;
9516 Imm = (ImmWidth == 16 ? ((Imm << 8) | Imm) : Imm);
9517 unsigned NewImm = Imm;
9518
9519 const uint16_t *table = lookup(Opcode, dom, ReplaceableBlendInstrs);
9520 if (!table)
9521 table = lookup(Opcode, dom, ReplaceableBlendAVX2Instrs);
9522
9523 if (Domain == 1) { // PackedSingle
9524 AdjustBlendMask(Imm, ImmWidth, Is256 ? 8 : 4, &NewImm);
9525 } else if (Domain == 2) { // PackedDouble
9526 AdjustBlendMask(Imm, ImmWidth, Is256 ? 4 : 2, &NewImm);
9527 } else if (Domain == 3) { // PackedInt
9528 if (Subtarget.hasAVX2()) {
9529 // If we are already VPBLENDW use that, else use VPBLENDD.
9530 if ((ImmWidth / (Is256 ? 2 : 1)) != 8) {
9531 table = lookup(Opcode, dom, ReplaceableBlendAVX2Instrs);
9532 AdjustBlendMask(Imm, ImmWidth, Is256 ? 8 : 4, &NewImm);
9533 }
9534 } else {
9535 assert(!Is256 && "128-bit vector expected");
9536 AdjustBlendMask(Imm, ImmWidth, 8, &NewImm);
9537 }
9538 }
9539
9540 assert(table && table[Domain - 1] && "Unknown domain op");
9541 MI.setDesc(get(table[Domain - 1]));
9542 MI.getOperand(NumOperands - 1).setImm(NewImm & 255);
9543 }
9544 return true;
9545 };
9546
9547 switch (Opcode) {
9548 case X86::BLENDPDrmi:
9549 case X86::BLENDPDrri:
9550 case X86::VBLENDPDrmi:
9551 case X86::VBLENDPDrri:
9552 return SetBlendDomain(2, false);
9553 case X86::VBLENDPDYrmi:
9554 case X86::VBLENDPDYrri:
9555 return SetBlendDomain(4, true);
9556 case X86::BLENDPSrmi:
9557 case X86::BLENDPSrri:
9558 case X86::VBLENDPSrmi:
9559 case X86::VBLENDPSrri:
9560 case X86::VPBLENDDrmi:
9561 case X86::VPBLENDDrri:
9562 return SetBlendDomain(4, false);
9563 case X86::VBLENDPSYrmi:
9564 case X86::VBLENDPSYrri:
9565 case X86::VPBLENDDYrmi:
9566 case X86::VPBLENDDYrri:
9567 return SetBlendDomain(8, true);
9568 case X86::PBLENDWrmi:
9569 case X86::PBLENDWrri:
9570 case X86::VPBLENDWrmi:
9571 case X86::VPBLENDWrri:
9572 return SetBlendDomain(8, false);
9573 case X86::VPBLENDWYrmi:
9574 case X86::VPBLENDWYrri:
9575 return SetBlendDomain(16, true);
9576 case X86::VPANDDZ128rr:
9577 case X86::VPANDDZ128rm:
9578 case X86::VPANDDZ256rr:
9579 case X86::VPANDDZ256rm:
9580 case X86::VPANDQZ128rr:
9581 case X86::VPANDQZ128rm:
9582 case X86::VPANDQZ256rr:
9583 case X86::VPANDQZ256rm:
9584 case X86::VPANDNDZ128rr:
9585 case X86::VPANDNDZ128rm:
9586 case X86::VPANDNDZ256rr:
9587 case X86::VPANDNDZ256rm:
9588 case X86::VPANDNQZ128rr:
9589 case X86::VPANDNQZ128rm:
9590 case X86::VPANDNQZ256rr:
9591 case X86::VPANDNQZ256rm:
9592 case X86::VPORDZ128rr:
9593 case X86::VPORDZ128rm:
9594 case X86::VPORDZ256rr:
9595 case X86::VPORDZ256rm:
9596 case X86::VPORQZ128rr:
9597 case X86::VPORQZ128rm:
9598 case X86::VPORQZ256rr:
9599 case X86::VPORQZ256rm:
9600 case X86::VPXORDZ128rr:
9601 case X86::VPXORDZ128rm:
9602 case X86::VPXORDZ256rr:
9603 case X86::VPXORDZ256rm:
9604 case X86::VPXORQZ128rr:
9605 case X86::VPXORQZ128rm:
9606 case X86::VPXORQZ256rr:
9607 case X86::VPXORQZ256rm: {
9608 // Without DQI, convert EVEX instructions to VEX instructions.
9609 if (Subtarget.hasDQI())
9610 return false;
9611
9612 const uint16_t *table =
9613 lookupAVX512(MI.getOpcode(), dom, ReplaceableCustomAVX512LogicInstrs);
9614 assert(table && "Instruction not found in table?");
9615 // Don't change integer Q instructions to D instructions and
9616 // use D intructions if we started with a PS instruction.
9617 if (Domain == 3 && (dom == 1 || table[3] == MI.getOpcode()))
9618 Domain = 4;
9619 MI.setDesc(get(table[Domain - 1]));
9620 return true;
9621 }
9622 case X86::UNPCKHPDrr:
9623 case X86::MOVHLPSrr:
9624 // We just need to commute the instruction which will switch the domains.
9625 if (Domain != dom && Domain != 3 &&
9626 MI.getOperand(1).getReg() == MI.getOperand(2).getReg() &&
9627 MI.getOperand(0).getSubReg() == 0 &&
9628 MI.getOperand(1).getSubReg() == 0 &&
9629 MI.getOperand(2).getSubReg() == 0) {
9630 commuteInstruction(MI, false);
9631 return true;
9632 }
9633 // We must always return true for MOVHLPSrr.
9634 if (Opcode == X86::MOVHLPSrr)
9635 return true;
9636 break;
9637 case X86::SHUFPDrri: {
9638 if (Domain == 1) {
9639 unsigned Imm = MI.getOperand(3).getImm();
9640 unsigned NewImm = 0x44;
9641 if (Imm & 1)
9642 NewImm |= 0x0a;
9643 if (Imm & 2)
9644 NewImm |= 0xa0;
9645 MI.getOperand(3).setImm(NewImm);
9646 MI.setDesc(get(X86::SHUFPSrri));
9647 }
9648 return true;
9649 }
9650 }
9651 return false;
9652}
9653
9654std::pair<uint16_t, uint16_t>
9656 uint16_t domain = (MI.getDesc().TSFlags >> X86II::SSEDomainShift) & 3;
9657 unsigned opcode = MI.getOpcode();
9658 uint16_t validDomains = 0;
9659 if (domain) {
9660 // Attempt to match for custom instructions.
9661 validDomains = getExecutionDomainCustom(MI);
9662 if (validDomains)
9663 return std::make_pair(domain, validDomains);
9664
9665 if (lookup(opcode, domain, ReplaceableInstrs)) {
9666 validDomains = 0xe;
9667 } else if (lookup(opcode, domain, ReplaceableInstrsAVX2)) {
9668 validDomains = Subtarget.hasAVX2() ? 0xe : 0x6;
9669 } else if (lookup(opcode, domain, ReplaceableInstrsFP)) {
9670 validDomains = 0x6;
9671 } else if (lookup(opcode, domain, ReplaceableInstrsAVX2InsertExtract)) {
9672 // Insert/extract instructions should only effect domain if AVX2
9673 // is enabled.
9674 if (!Subtarget.hasAVX2())
9675 return std::make_pair(0, 0);
9676 validDomains = 0xe;
9677 } else if (lookupAVX512(opcode, domain, ReplaceableInstrsAVX512)) {
9678 validDomains = 0xe;
9679 } else if (Subtarget.hasDQI() &&
9680 lookupAVX512(opcode, domain, ReplaceableInstrsAVX512DQ)) {
9681 validDomains = 0xe;
9682 } else if (Subtarget.hasDQI()) {
9683 if (const uint16_t *table =
9684 lookupAVX512(opcode, domain, ReplaceableInstrsAVX512DQMasked)) {
9685 if (domain == 1 || (domain == 3 && table[3] == opcode))
9686 validDomains = 0xa;
9687 else
9688 validDomains = 0xc;
9689 }
9690 }
9691 }
9692 return std::make_pair(domain, validDomains);
9693}
9694
9696 assert(Domain > 0 && Domain < 4 && "Invalid execution domain");
9697 uint16_t dom = (MI.getDesc().TSFlags >> X86II::SSEDomainShift) & 3;
9698 assert(dom && "Not an SSE instruction");
9699
9700 // Attempt to match for custom instructions.
9702 return;
9703
9704 const uint16_t *table = lookup(MI.getOpcode(), dom, ReplaceableInstrs);
9705 if (!table) { // try the other table
9706 assert((Subtarget.hasAVX2() || Domain < 3) &&
9707 "256-bit vector operations only available in AVX2");
9708 table = lookup(MI.getOpcode(), dom, ReplaceableInstrsAVX2);
9709 }
9710 if (!table) { // try the FP table
9711 table = lookup(MI.getOpcode(), dom, ReplaceableInstrsFP);
9712 assert((!table || Domain < 3) &&
9713 "Can only select PackedSingle or PackedDouble");
9714 }
9715 if (!table) { // try the other table
9716 assert(Subtarget.hasAVX2() &&
9717 "256-bit insert/extract only available in AVX2");
9718 table = lookup(MI.getOpcode(), dom, ReplaceableInstrsAVX2InsertExtract);
9719 }
9720 if (!table) { // try the AVX512 table
9721 assert(Subtarget.hasAVX512() && "Requires AVX-512");
9722 table = lookupAVX512(MI.getOpcode(), dom, ReplaceableInstrsAVX512);
9723 // Don't change integer Q instructions to D instructions.
9724 if (table && Domain == 3 && table[3] == MI.getOpcode())
9725 Domain = 4;
9726 }
9727 if (!table) { // try the AVX512DQ table
9728 assert((Subtarget.hasDQI() || Domain >= 3) && "Requires AVX-512DQ");
9729 table = lookupAVX512(MI.getOpcode(), dom, ReplaceableInstrsAVX512DQ);
9730 // Don't change integer Q instructions to D instructions and
9731 // use D instructions if we started with a PS instruction.
9732 if (table && Domain == 3 && (dom == 1 || table[3] == MI.getOpcode()))
9733 Domain = 4;
9734 }
9735 if (!table) { // try the AVX512DQMasked table
9736 assert((Subtarget.hasDQI() || Domain >= 3) && "Requires AVX-512DQ");
9737 table = lookupAVX512(MI.getOpcode(), dom, ReplaceableInstrsAVX512DQMasked);
9738 if (table && Domain == 3 && (dom == 1 || table[3] == MI.getOpcode()))
9739 Domain = 4;
9740 }
9741 assert(table && "Cannot change domain");
9742 MI.setDesc(get(table[Domain - 1]));
9743}
9744
9750
9751/// Return the noop instruction to use for a noop.
9753 MCInst Nop;
9754 Nop.setOpcode(X86::NOOP);
9755 return Nop;
9756}
9757
9759 switch (opc) {
9760 default:
9761 return false;
9762 case X86::DIVPDrm:
9763 case X86::DIVPDrr:
9764 case X86::DIVPSrm:
9765 case X86::DIVPSrr:
9766 case X86::DIVSDrm:
9767 case X86::DIVSDrm_Int:
9768 case X86::DIVSDrr:
9769 case X86::DIVSDrr_Int:
9770 case X86::DIVSSrm:
9771 case X86::DIVSSrm_Int:
9772 case X86::DIVSSrr:
9773 case X86::DIVSSrr_Int:
9774 case X86::SQRTPDm:
9775 case X86::SQRTPDr:
9776 case X86::SQRTPSm:
9777 case X86::SQRTPSr:
9778 case X86::SQRTSDm:
9779 case X86::SQRTSDm_Int:
9780 case X86::SQRTSDr:
9781 case X86::SQRTSDr_Int:
9782 case X86::SQRTSSm:
9783 case X86::SQRTSSm_Int:
9784 case X86::SQRTSSr:
9785 case X86::SQRTSSr_Int:
9786 // AVX instructions with high latency
9787 case X86::VDIVPDrm:
9788 case X86::VDIVPDrr:
9789 case X86::VDIVPDYrm:
9790 case X86::VDIVPDYrr:
9791 case X86::VDIVPSrm:
9792 case X86::VDIVPSrr:
9793 case X86::VDIVPSYrm:
9794 case X86::VDIVPSYrr:
9795 case X86::VDIVSDrm:
9796 case X86::VDIVSDrm_Int:
9797 case X86::VDIVSDrr:
9798 case X86::VDIVSDrr_Int:
9799 case X86::VDIVSSrm:
9800 case X86::VDIVSSrm_Int:
9801 case X86::VDIVSSrr:
9802 case X86::VDIVSSrr_Int:
9803 case X86::VSQRTPDm:
9804 case X86::VSQRTPDr:
9805 case X86::VSQRTPDYm:
9806 case X86::VSQRTPDYr:
9807 case X86::VSQRTPSm:
9808 case X86::VSQRTPSr:
9809 case X86::VSQRTPSYm:
9810 case X86::VSQRTPSYr:
9811 case X86::VSQRTSDm:
9812 case X86::VSQRTSDm_Int:
9813 case X86::VSQRTSDr:
9814 case X86::VSQRTSDr_Int:
9815 case X86::VSQRTSSm:
9816 case X86::VSQRTSSm_Int:
9817 case X86::VSQRTSSr:
9818 case X86::VSQRTSSr_Int:
9819 // AVX512 instructions with high latency
9820 case X86::VDIVPDZ128rm:
9821 case X86::VDIVPDZ128rmb:
9822 case X86::VDIVPDZ128rmbk:
9823 case X86::VDIVPDZ128rmbkz:
9824 case X86::VDIVPDZ128rmk:
9825 case X86::VDIVPDZ128rmkz:
9826 case X86::VDIVPDZ128rr:
9827 case X86::VDIVPDZ128rrk:
9828 case X86::VDIVPDZ128rrkz:
9829 case X86::VDIVPDZ256rm:
9830 case X86::VDIVPDZ256rmb:
9831 case X86::VDIVPDZ256rmbk:
9832 case X86::VDIVPDZ256rmbkz:
9833 case X86::VDIVPDZ256rmk:
9834 case X86::VDIVPDZ256rmkz:
9835 case X86::VDIVPDZ256rr:
9836 case X86::VDIVPDZ256rrk:
9837 case X86::VDIVPDZ256rrkz:
9838 case X86::VDIVPDZrrb:
9839 case X86::VDIVPDZrrbk:
9840 case X86::VDIVPDZrrbkz:
9841 case X86::VDIVPDZrm:
9842 case X86::VDIVPDZrmb:
9843 case X86::VDIVPDZrmbk:
9844 case X86::VDIVPDZrmbkz:
9845 case X86::VDIVPDZrmk:
9846 case X86::VDIVPDZrmkz:
9847 case X86::VDIVPDZrr:
9848 case X86::VDIVPDZrrk:
9849 case X86::VDIVPDZrrkz:
9850 case X86::VDIVPSZ128rm:
9851 case X86::VDIVPSZ128rmb:
9852 case X86::VDIVPSZ128rmbk:
9853 case X86::VDIVPSZ128rmbkz:
9854 case X86::VDIVPSZ128rmk:
9855 case X86::VDIVPSZ128rmkz:
9856 case X86::VDIVPSZ128rr:
9857 case X86::VDIVPSZ128rrk:
9858 case X86::VDIVPSZ128rrkz:
9859 case X86::VDIVPSZ256rm:
9860 case X86::VDIVPSZ256rmb:
9861 case X86::VDIVPSZ256rmbk:
9862 case X86::VDIVPSZ256rmbkz:
9863 case X86::VDIVPSZ256rmk:
9864 case X86::VDIVPSZ256rmkz:
9865 case X86::VDIVPSZ256rr:
9866 case X86::VDIVPSZ256rrk:
9867 case X86::VDIVPSZ256rrkz:
9868 case X86::VDIVPSZrrb:
9869 case X86::VDIVPSZrrbk:
9870 case X86::VDIVPSZrrbkz:
9871 case X86::VDIVPSZrm:
9872 case X86::VDIVPSZrmb:
9873 case X86::VDIVPSZrmbk:
9874 case X86::VDIVPSZrmbkz:
9875 case X86::VDIVPSZrmk:
9876 case X86::VDIVPSZrmkz:
9877 case X86::VDIVPSZrr:
9878 case X86::VDIVPSZrrk:
9879 case X86::VDIVPSZrrkz:
9880 case X86::VDIVSDZrm:
9881 case X86::VDIVSDZrr:
9882 case X86::VDIVSDZrm_Int:
9883 case X86::VDIVSDZrmk_Int:
9884 case X86::VDIVSDZrmkz_Int:
9885 case X86::VDIVSDZrr_Int:
9886 case X86::VDIVSDZrrk_Int:
9887 case X86::VDIVSDZrrkz_Int:
9888 case X86::VDIVSDZrrb_Int:
9889 case X86::VDIVSDZrrbk_Int:
9890 case X86::VDIVSDZrrbkz_Int:
9891 case X86::VDIVSSZrm:
9892 case X86::VDIVSSZrr:
9893 case X86::VDIVSSZrm_Int:
9894 case X86::VDIVSSZrmk_Int:
9895 case X86::VDIVSSZrmkz_Int:
9896 case X86::VDIVSSZrr_Int:
9897 case X86::VDIVSSZrrk_Int:
9898 case X86::VDIVSSZrrkz_Int:
9899 case X86::VDIVSSZrrb_Int:
9900 case X86::VDIVSSZrrbk_Int:
9901 case X86::VDIVSSZrrbkz_Int:
9902 case X86::VSQRTPDZ128m:
9903 case X86::VSQRTPDZ128mb:
9904 case X86::VSQRTPDZ128mbk:
9905 case X86::VSQRTPDZ128mbkz:
9906 case X86::VSQRTPDZ128mk:
9907 case X86::VSQRTPDZ128mkz:
9908 case X86::VSQRTPDZ128r:
9909 case X86::VSQRTPDZ128rk:
9910 case X86::VSQRTPDZ128rkz:
9911 case X86::VSQRTPDZ256m:
9912 case X86::VSQRTPDZ256mb:
9913 case X86::VSQRTPDZ256mbk:
9914 case X86::VSQRTPDZ256mbkz:
9915 case X86::VSQRTPDZ256mk:
9916 case X86::VSQRTPDZ256mkz:
9917 case X86::VSQRTPDZ256r:
9918 case X86::VSQRTPDZ256rk:
9919 case X86::VSQRTPDZ256rkz:
9920 case X86::VSQRTPDZm:
9921 case X86::VSQRTPDZmb:
9922 case X86::VSQRTPDZmbk:
9923 case X86::VSQRTPDZmbkz:
9924 case X86::VSQRTPDZmk:
9925 case X86::VSQRTPDZmkz:
9926 case X86::VSQRTPDZr:
9927 case X86::VSQRTPDZrb:
9928 case X86::VSQRTPDZrbk:
9929 case X86::VSQRTPDZrbkz:
9930 case X86::VSQRTPDZrk:
9931 case X86::VSQRTPDZrkz:
9932 case X86::VSQRTPSZ128m:
9933 case X86::VSQRTPSZ128mb:
9934 case X86::VSQRTPSZ128mbk:
9935 case X86::VSQRTPSZ128mbkz:
9936 case X86::VSQRTPSZ128mk:
9937 case X86::VSQRTPSZ128mkz:
9938 case X86::VSQRTPSZ128r:
9939 case X86::VSQRTPSZ128rk:
9940 case X86::VSQRTPSZ128rkz:
9941 case X86::VSQRTPSZ256m:
9942 case X86::VSQRTPSZ256mb:
9943 case X86::VSQRTPSZ256mbk:
9944 case X86::VSQRTPSZ256mbkz:
9945 case X86::VSQRTPSZ256mk:
9946 case X86::VSQRTPSZ256mkz:
9947 case X86::VSQRTPSZ256r:
9948 case X86::VSQRTPSZ256rk:
9949 case X86::VSQRTPSZ256rkz:
9950 case X86::VSQRTPSZm:
9951 case X86::VSQRTPSZmb:
9952 case X86::VSQRTPSZmbk:
9953 case X86::VSQRTPSZmbkz:
9954 case X86::VSQRTPSZmk:
9955 case X86::VSQRTPSZmkz:
9956 case X86::VSQRTPSZr:
9957 case X86::VSQRTPSZrb:
9958 case X86::VSQRTPSZrbk:
9959 case X86::VSQRTPSZrbkz:
9960 case X86::VSQRTPSZrk:
9961 case X86::VSQRTPSZrkz:
9962 case X86::VSQRTSDZm:
9963 case X86::VSQRTSDZm_Int:
9964 case X86::VSQRTSDZmk_Int:
9965 case X86::VSQRTSDZmkz_Int:
9966 case X86::VSQRTSDZr:
9967 case X86::VSQRTSDZr_Int:
9968 case X86::VSQRTSDZrk_Int:
9969 case X86::VSQRTSDZrkz_Int:
9970 case X86::VSQRTSDZrb_Int:
9971 case X86::VSQRTSDZrbk_Int:
9972 case X86::VSQRTSDZrbkz_Int:
9973 case X86::VSQRTSSZm:
9974 case X86::VSQRTSSZm_Int:
9975 case X86::VSQRTSSZmk_Int:
9976 case X86::VSQRTSSZmkz_Int:
9977 case X86::VSQRTSSZr:
9978 case X86::VSQRTSSZr_Int:
9979 case X86::VSQRTSSZrk_Int:
9980 case X86::VSQRTSSZrkz_Int:
9981 case X86::VSQRTSSZrb_Int:
9982 case X86::VSQRTSSZrbk_Int:
9983 case X86::VSQRTSSZrbkz_Int:
9984
9985 case X86::VGATHERDPDYrm:
9986 case X86::VGATHERDPDZ128rm:
9987 case X86::VGATHERDPDZ256rm:
9988 case X86::VGATHERDPDZrm:
9989 case X86::VGATHERDPDrm:
9990 case X86::VGATHERDPSYrm:
9991 case X86::VGATHERDPSZ128rm:
9992 case X86::VGATHERDPSZ256rm:
9993 case X86::VGATHERDPSZrm:
9994 case X86::VGATHERDPSrm:
9995 case X86::VGATHERPF0DPDm:
9996 case X86::VGATHERPF0DPSm:
9997 case X86::VGATHERPF0QPDm:
9998 case X86::VGATHERPF0QPSm:
9999 case X86::VGATHERPF1DPDm:
10000 case X86::VGATHERPF1DPSm:
10001 case X86::VGATHERPF1QPDm:
10002 case X86::VGATHERPF1QPSm:
10003 case X86::VGATHERQPDYrm:
10004 case X86::VGATHERQPDZ128rm:
10005 case X86::VGATHERQPDZ256rm:
10006 case X86::VGATHERQPDZrm:
10007 case X86::VGATHERQPDrm:
10008 case X86::VGATHERQPSYrm:
10009 case X86::VGATHERQPSZ128rm:
10010 case X86::VGATHERQPSZ256rm:
10011 case X86::VGATHERQPSZrm:
10012 case X86::VGATHERQPSrm:
10013 case X86::VPGATHERDDYrm:
10014 case X86::VPGATHERDDZ128rm:
10015 case X86::VPGATHERDDZ256rm:
10016 case X86::VPGATHERDDZrm:
10017 case X86::VPGATHERDDrm:
10018 case X86::VPGATHERDQYrm:
10019 case X86::VPGATHERDQZ128rm:
10020 case X86::VPGATHERDQZ256rm:
10021 case X86::VPGATHERDQZrm:
10022 case X86::VPGATHERDQrm:
10023 case X86::VPGATHERQDYrm:
10024 case X86::VPGATHERQDZ128rm:
10025 case X86::VPGATHERQDZ256rm:
10026 case X86::VPGATHERQDZrm:
10027 case X86::VPGATHERQDrm:
10028 case X86::VPGATHERQQYrm:
10029 case X86::VPGATHERQQZ128rm:
10030 case X86::VPGATHERQQZ256rm:
10031 case X86::VPGATHERQQZrm:
10032 case X86::VPGATHERQQrm:
10033 case X86::VSCATTERDPDZ128mr:
10034 case X86::VSCATTERDPDZ256mr:
10035 case X86::VSCATTERDPDZmr:
10036 case X86::VSCATTERDPSZ128mr:
10037 case X86::VSCATTERDPSZ256mr:
10038 case X86::VSCATTERDPSZmr:
10039 case X86::VSCATTERPF0DPDm:
10040 case X86::VSCATTERPF0DPSm:
10041 case X86::VSCATTERPF0QPDm:
10042 case X86::VSCATTERPF0QPSm:
10043 case X86::VSCATTERPF1DPDm:
10044 case X86::VSCATTERPF1DPSm:
10045 case X86::VSCATTERPF1QPDm:
10046 case X86::VSCATTERPF1QPSm:
10047 case X86::VSCATTERQPDZ128mr:
10048 case X86::VSCATTERQPDZ256mr:
10049 case X86::VSCATTERQPDZmr:
10050 case X86::VSCATTERQPSZ128mr:
10051 case X86::VSCATTERQPSZ256mr:
10052 case X86::VSCATTERQPSZmr:
10053 case X86::VPSCATTERDDZ128mr:
10054 case X86::VPSCATTERDDZ256mr:
10055 case X86::VPSCATTERDDZmr:
10056 case X86::VPSCATTERDQZ128mr:
10057 case X86::VPSCATTERDQZ256mr:
10058 case X86::VPSCATTERDQZmr:
10059 case X86::VPSCATTERQDZ128mr:
10060 case X86::VPSCATTERQDZ256mr:
10061 case X86::VPSCATTERQDZmr:
10062 case X86::VPSCATTERQQZ128mr:
10063 case X86::VPSCATTERQQZ256mr:
10064 case X86::VPSCATTERQQZmr:
10065 return true;
10066 }
10067}
10068
10070 const MachineRegisterInfo *MRI,
10071 const MachineInstr &DefMI,
10072 unsigned DefIdx,
10073 const MachineInstr &UseMI,
10074 unsigned UseIdx) const {
10075 return isHighLatencyDef(DefMI.getOpcode());
10076}
10077
10079 const MachineBasicBlock *MBB) const {
10080 assert(Inst.getNumExplicitOperands() == 3 && Inst.getNumExplicitDefs() == 1 &&
10081 Inst.getNumDefs() <= 2 && "Reassociation needs binary operators");
10082
10083 // Integer binary math/logic instructions have a third source operand:
10084 // the EFLAGS register. That operand must be both defined here and never
10085 // used; ie, it must be dead. If the EFLAGS operand is live, then we can
10086 // not change anything because rearranging the operands could affect other
10087 // instructions that depend on the exact status flags (zero, sign, etc.)
10088 // that are set by using these particular operands with this operation.
10089 const MachineOperand *FlagDef =
10090 Inst.findRegisterDefOperand(X86::EFLAGS, /*TRI=*/nullptr);
10091 assert((Inst.getNumDefs() == 1 || FlagDef) && "Implicit def isn't flags?");
10092 if (FlagDef && !FlagDef->isDead())
10093 return false;
10094
10096}
10097
10098// TODO: There are many more machine instruction opcodes to match:
10099// 1. Other data types (integer, vectors)
10100// 2. Other math / logic operations (xor, or)
10101// 3. Other forms of the same operation (intrinsics and other variants)
10103 bool Invert) const {
10104 if (Invert)
10105 return false;
10106 switch (Inst.getOpcode()) {
10107 CASE_ND(ADD8rr)
10108 CASE_ND(ADD16rr)
10109 CASE_ND(ADD32rr)
10110 CASE_ND(ADD64rr)
10111 CASE_ND(AND8rr)
10112 CASE_ND(AND16rr)
10113 CASE_ND(AND32rr)
10114 CASE_ND(AND64rr)
10115 CASE_ND(OR8rr)
10116 CASE_ND(OR16rr)
10117 CASE_ND(OR32rr)
10118 CASE_ND(OR64rr)
10119 CASE_ND(XOR8rr)
10120 CASE_ND(XOR16rr)
10121 CASE_ND(XOR32rr)
10122 CASE_ND(XOR64rr)
10123 CASE_ND(IMUL16rr)
10124 CASE_ND(IMUL32rr)
10125 CASE_ND(IMUL64rr)
10126 case X86::PANDrr:
10127 case X86::PORrr:
10128 case X86::PXORrr:
10129 case X86::ANDPDrr:
10130 case X86::ANDPSrr:
10131 case X86::ORPDrr:
10132 case X86::ORPSrr:
10133 case X86::XORPDrr:
10134 case X86::XORPSrr:
10135 case X86::PADDBrr:
10136 case X86::PADDWrr:
10137 case X86::PADDDrr:
10138 case X86::PADDQrr:
10139 case X86::PMULLWrr:
10140 case X86::PMULLDrr:
10141 case X86::PMAXSBrr:
10142 case X86::PMAXSDrr:
10143 case X86::PMAXSWrr:
10144 case X86::PMAXUBrr:
10145 case X86::PMAXUDrr:
10146 case X86::PMAXUWrr:
10147 case X86::PMINSBrr:
10148 case X86::PMINSDrr:
10149 case X86::PMINSWrr:
10150 case X86::PMINUBrr:
10151 case X86::PMINUDrr:
10152 case X86::PMINUWrr:
10153 case X86::VPANDrr:
10154 case X86::VPANDYrr:
10155 case X86::VPANDDZ128rr:
10156 case X86::VPANDDZ256rr:
10157 case X86::VPANDDZrr:
10158 case X86::VPANDQZ128rr:
10159 case X86::VPANDQZ256rr:
10160 case X86::VPANDQZrr:
10161 case X86::VPORrr:
10162 case X86::VPORYrr:
10163 case X86::VPORDZ128rr:
10164 case X86::VPORDZ256rr:
10165 case X86::VPORDZrr:
10166 case X86::VPORQZ128rr:
10167 case X86::VPORQZ256rr:
10168 case X86::VPORQZrr:
10169 case X86::VPXORrr:
10170 case X86::VPXORYrr:
10171 case X86::VPXORDZ128rr:
10172 case X86::VPXORDZ256rr:
10173 case X86::VPXORDZrr:
10174 case X86::VPXORQZ128rr:
10175 case X86::VPXORQZ256rr:
10176 case X86::VPXORQZrr:
10177 case X86::VANDPDrr:
10178 case X86::VANDPSrr:
10179 case X86::VANDPDYrr:
10180 case X86::VANDPSYrr:
10181 case X86::VANDPDZ128rr:
10182 case X86::VANDPSZ128rr:
10183 case X86::VANDPDZ256rr:
10184 case X86::VANDPSZ256rr:
10185 case X86::VANDPDZrr:
10186 case X86::VANDPSZrr:
10187 case X86::VORPDrr:
10188 case X86::VORPSrr:
10189 case X86::VORPDYrr:
10190 case X86::VORPSYrr:
10191 case X86::VORPDZ128rr:
10192 case X86::VORPSZ128rr:
10193 case X86::VORPDZ256rr:
10194 case X86::VORPSZ256rr:
10195 case X86::VORPDZrr:
10196 case X86::VORPSZrr:
10197 case X86::VXORPDrr:
10198 case X86::VXORPSrr:
10199 case X86::VXORPDYrr:
10200 case X86::VXORPSYrr:
10201 case X86::VXORPDZ128rr:
10202 case X86::VXORPSZ128rr:
10203 case X86::VXORPDZ256rr:
10204 case X86::VXORPSZ256rr:
10205 case X86::VXORPDZrr:
10206 case X86::VXORPSZrr:
10207 case X86::KADDBkk:
10208 case X86::KADDWkk:
10209 case X86::KADDDkk:
10210 case X86::KADDQkk:
10211 case X86::KANDBkk:
10212 case X86::KANDWkk:
10213 case X86::KANDDkk:
10214 case X86::KANDQkk:
10215 case X86::KORBkk:
10216 case X86::KORWkk:
10217 case X86::KORDkk:
10218 case X86::KORQkk:
10219 case X86::KXORBkk:
10220 case X86::KXORWkk:
10221 case X86::KXORDkk:
10222 case X86::KXORQkk:
10223 case X86::VPADDBrr:
10224 case X86::VPADDWrr:
10225 case X86::VPADDDrr:
10226 case X86::VPADDQrr:
10227 case X86::VPADDBYrr:
10228 case X86::VPADDWYrr:
10229 case X86::VPADDDYrr:
10230 case X86::VPADDQYrr:
10231 case X86::VPADDBZ128rr:
10232 case X86::VPADDWZ128rr:
10233 case X86::VPADDDZ128rr:
10234 case X86::VPADDQZ128rr:
10235 case X86::VPADDBZ256rr:
10236 case X86::VPADDWZ256rr:
10237 case X86::VPADDDZ256rr:
10238 case X86::VPADDQZ256rr:
10239 case X86::VPADDBZrr:
10240 case X86::VPADDWZrr:
10241 case X86::VPADDDZrr:
10242 case X86::VPADDQZrr:
10243 case X86::VPMULLWrr:
10244 case X86::VPMULLWYrr:
10245 case X86::VPMULLWZ128rr:
10246 case X86::VPMULLWZ256rr:
10247 case X86::VPMULLWZrr:
10248 case X86::VPMULLDrr:
10249 case X86::VPMULLDYrr:
10250 case X86::VPMULLDZ128rr:
10251 case X86::VPMULLDZ256rr:
10252 case X86::VPMULLDZrr:
10253 case X86::VPMULLQZ128rr:
10254 case X86::VPMULLQZ256rr:
10255 case X86::VPMULLQZrr:
10256 case X86::VPMAXSBrr:
10257 case X86::VPMAXSBYrr:
10258 case X86::VPMAXSBZ128rr:
10259 case X86::VPMAXSBZ256rr:
10260 case X86::VPMAXSBZrr:
10261 case X86::VPMAXSDrr:
10262 case X86::VPMAXSDYrr:
10263 case X86::VPMAXSDZ128rr:
10264 case X86::VPMAXSDZ256rr:
10265 case X86::VPMAXSDZrr:
10266 case X86::VPMAXSQZ128rr:
10267 case X86::VPMAXSQZ256rr:
10268 case X86::VPMAXSQZrr:
10269 case X86::VPMAXSWrr:
10270 case X86::VPMAXSWYrr:
10271 case X86::VPMAXSWZ128rr:
10272 case X86::VPMAXSWZ256rr:
10273 case X86::VPMAXSWZrr:
10274 case X86::VPMAXUBrr:
10275 case X86::VPMAXUBYrr:
10276 case X86::VPMAXUBZ128rr:
10277 case X86::VPMAXUBZ256rr:
10278 case X86::VPMAXUBZrr:
10279 case X86::VPMAXUDrr:
10280 case X86::VPMAXUDYrr:
10281 case X86::VPMAXUDZ128rr:
10282 case X86::VPMAXUDZ256rr:
10283 case X86::VPMAXUDZrr:
10284 case X86::VPMAXUQZ128rr:
10285 case X86::VPMAXUQZ256rr:
10286 case X86::VPMAXUQZrr:
10287 case X86::VPMAXUWrr:
10288 case X86::VPMAXUWYrr:
10289 case X86::VPMAXUWZ128rr:
10290 case X86::VPMAXUWZ256rr:
10291 case X86::VPMAXUWZrr:
10292 case X86::VPMINSBrr:
10293 case X86::VPMINSBYrr:
10294 case X86::VPMINSBZ128rr:
10295 case X86::VPMINSBZ256rr:
10296 case X86::VPMINSBZrr:
10297 case X86::VPMINSDrr:
10298 case X86::VPMINSDYrr:
10299 case X86::VPMINSDZ128rr:
10300 case X86::VPMINSDZ256rr:
10301 case X86::VPMINSDZrr:
10302 case X86::VPMINSQZ128rr:
10303 case X86::VPMINSQZ256rr:
10304 case X86::VPMINSQZrr:
10305 case X86::VPMINSWrr:
10306 case X86::VPMINSWYrr:
10307 case X86::VPMINSWZ128rr:
10308 case X86::VPMINSWZ256rr:
10309 case X86::VPMINSWZrr:
10310 case X86::VPMINUBrr:
10311 case X86::VPMINUBYrr:
10312 case X86::VPMINUBZ128rr:
10313 case X86::VPMINUBZ256rr:
10314 case X86::VPMINUBZrr:
10315 case X86::VPMINUDrr:
10316 case X86::VPMINUDYrr:
10317 case X86::VPMINUDZ128rr:
10318 case X86::VPMINUDZ256rr:
10319 case X86::VPMINUDZrr:
10320 case X86::VPMINUQZ128rr:
10321 case X86::VPMINUQZ256rr:
10322 case X86::VPMINUQZrr:
10323 case X86::VPMINUWrr:
10324 case X86::VPMINUWYrr:
10325 case X86::VPMINUWZ128rr:
10326 case X86::VPMINUWZ256rr:
10327 case X86::VPMINUWZrr:
10328 // Normal min/max instructions are not commutative because of NaN and signed
10329 // zero semantics, but these are. Thus, there's no need to check for global
10330 // relaxed math; the instructions themselves have the properties we need.
10331 case X86::MAXCPDrr:
10332 case X86::MAXCPSrr:
10333 case X86::MAXCSDrr:
10334 case X86::MAXCSSrr:
10335 case X86::MINCPDrr:
10336 case X86::MINCPSrr:
10337 case X86::MINCSDrr:
10338 case X86::MINCSSrr:
10339 case X86::VMAXCPDrr:
10340 case X86::VMAXCPSrr:
10341 case X86::VMAXCPDYrr:
10342 case X86::VMAXCPSYrr:
10343 case X86::VMAXCPDZ128rr:
10344 case X86::VMAXCPSZ128rr:
10345 case X86::VMAXCPDZ256rr:
10346 case X86::VMAXCPSZ256rr:
10347 case X86::VMAXCPDZrr:
10348 case X86::VMAXCPSZrr:
10349 case X86::VMAXCSDrr:
10350 case X86::VMAXCSSrr:
10351 case X86::VMAXCSDZrr:
10352 case X86::VMAXCSSZrr:
10353 case X86::VMINCPDrr:
10354 case X86::VMINCPSrr:
10355 case X86::VMINCPDYrr:
10356 case X86::VMINCPSYrr:
10357 case X86::VMINCPDZ128rr:
10358 case X86::VMINCPSZ128rr:
10359 case X86::VMINCPDZ256rr:
10360 case X86::VMINCPSZ256rr:
10361 case X86::VMINCPDZrr:
10362 case X86::VMINCPSZrr:
10363 case X86::VMINCSDrr:
10364 case X86::VMINCSSrr:
10365 case X86::VMINCSDZrr:
10366 case X86::VMINCSSZrr:
10367 case X86::VMAXCPHZ128rr:
10368 case X86::VMAXCPHZ256rr:
10369 case X86::VMAXCPHZrr:
10370 case X86::VMAXCSHZrr:
10371 case X86::VMINCPHZ128rr:
10372 case X86::VMINCPHZ256rr:
10373 case X86::VMINCPHZrr:
10374 case X86::VMINCSHZrr:
10375 return true;
10376 case X86::ADDPDrr:
10377 case X86::ADDPSrr:
10378 case X86::ADDSDrr:
10379 case X86::ADDSSrr:
10380 case X86::MULPDrr:
10381 case X86::MULPSrr:
10382 case X86::MULSDrr:
10383 case X86::MULSSrr:
10384 case X86::VADDPDrr:
10385 case X86::VADDPSrr:
10386 case X86::VADDPDYrr:
10387 case X86::VADDPSYrr:
10388 case X86::VADDPDZ128rr:
10389 case X86::VADDPSZ128rr:
10390 case X86::VADDPDZ256rr:
10391 case X86::VADDPSZ256rr:
10392 case X86::VADDPDZrr:
10393 case X86::VADDPSZrr:
10394 case X86::VADDSDrr:
10395 case X86::VADDSSrr:
10396 case X86::VADDSDZrr:
10397 case X86::VADDSSZrr:
10398 case X86::VMULPDrr:
10399 case X86::VMULPSrr:
10400 case X86::VMULPDYrr:
10401 case X86::VMULPSYrr:
10402 case X86::VMULPDZ128rr:
10403 case X86::VMULPSZ128rr:
10404 case X86::VMULPDZ256rr:
10405 case X86::VMULPSZ256rr:
10406 case X86::VMULPDZrr:
10407 case X86::VMULPSZrr:
10408 case X86::VMULSDrr:
10409 case X86::VMULSSrr:
10410 case X86::VMULSDZrr:
10411 case X86::VMULSSZrr:
10412 case X86::VADDPHZ128rr:
10413 case X86::VADDPHZ256rr:
10414 case X86::VADDPHZrr:
10415 case X86::VADDSHZrr:
10416 case X86::VMULPHZ128rr:
10417 case X86::VMULPHZ256rr:
10418 case X86::VMULPHZrr:
10419 case X86::VMULSHZrr:
10422 default:
10423 return false;
10424 }
10425}
10426
10427/// If \p DescribedReg overlaps with the MOVrr instruction's destination
10428/// register then, if possible, describe the value in terms of the source
10429/// register.
10430static std::optional<ParamLoadedValue>
10432 const TargetRegisterInfo *TRI) {
10433 Register DestReg = MI.getOperand(0).getReg();
10434 Register SrcReg = MI.getOperand(1).getReg();
10435
10436 auto Expr = DIExpression::get(MI.getMF()->getFunction().getContext(), {});
10437
10438 // If the described register is the destination, just return the source.
10439 if (DestReg == DescribedReg)
10440 return ParamLoadedValue(MachineOperand::CreateReg(SrcReg, false), Expr);
10441
10442 // If the described register is a sub-register of the destination register,
10443 // then pick out the source register's corresponding sub-register.
10444 if (unsigned SubRegIdx = TRI->getSubRegIndex(DestReg, DescribedReg)) {
10445 Register SrcSubReg = TRI->getSubReg(SrcReg, SubRegIdx);
10446 return ParamLoadedValue(MachineOperand::CreateReg(SrcSubReg, false), Expr);
10447 }
10448
10449 // The remaining case to consider is when the described register is a
10450 // super-register of the destination register. MOV8rr and MOV16rr does not
10451 // write to any of the other bytes in the register, meaning that we'd have to
10452 // describe the value using a combination of the source register and the
10453 // non-overlapping bits in the described register, which is not currently
10454 // possible.
10455 if (MI.getOpcode() == X86::MOV8rr || MI.getOpcode() == X86::MOV16rr ||
10456 !TRI->isSuperRegister(DestReg, DescribedReg))
10457 return std::nullopt;
10458
10459 assert(MI.getOpcode() == X86::MOV32rr && "Unexpected super-register case");
10460 return ParamLoadedValue(MachineOperand::CreateReg(SrcReg, false), Expr);
10461}
10462
10463std::optional<ParamLoadedValue>
10465 const MachineOperand *Op = nullptr;
10466 DIExpression *Expr = nullptr;
10467
10469
10470 switch (MI.getOpcode()) {
10471 case X86::LEA32r:
10472 case X86::LEA64r:
10473 case X86::LEA64_32r: {
10474 // We may need to describe a 64-bit parameter with a 32-bit LEA.
10475 if (!TRI->isSuperRegisterEq(MI.getOperand(0).getReg(), Reg))
10476 return std::nullopt;
10477
10478 // Operand 4 could be global address. For now we do not support
10479 // such situation.
10480 if (!MI.getOperand(4).isImm() || !MI.getOperand(2).isImm())
10481 return std::nullopt;
10482
10483 const MachineOperand &Op1 = MI.getOperand(1);
10484 const MachineOperand &Op2 = MI.getOperand(3);
10485 assert(Op2.isReg() &&
10486 (Op2.getReg() == X86::NoRegister || Op2.getReg().isPhysical()));
10487
10488 // Omit situations like:
10489 // %rsi = lea %rsi, 4, ...
10490 if ((Op1.isReg() && Op1.getReg() == MI.getOperand(0).getReg()) ||
10491 Op2.getReg() == MI.getOperand(0).getReg())
10492 return std::nullopt;
10493 else if ((Op1.isReg() && Op1.getReg() != X86::NoRegister &&
10494 TRI->regsOverlap(Op1.getReg(), MI.getOperand(0).getReg())) ||
10495 (Op2.getReg() != X86::NoRegister &&
10496 TRI->regsOverlap(Op2.getReg(), MI.getOperand(0).getReg())))
10497 return std::nullopt;
10498
10499 int64_t Coef = MI.getOperand(2).getImm();
10500 int64_t Offset = MI.getOperand(4).getImm();
10502
10503 if ((Op1.isReg() && Op1.getReg() != X86::NoRegister)) {
10504 Op = &Op1;
10505 } else if (Op1.isFI())
10506 Op = &Op1;
10507
10508 if (Op && Op->isReg() && Op->getReg() == Op2.getReg() && Coef > 0) {
10509 Ops.push_back(dwarf::DW_OP_constu);
10510 Ops.push_back(Coef + 1);
10511 Ops.push_back(dwarf::DW_OP_mul);
10512 } else {
10513 if (Op && Op2.getReg() != X86::NoRegister) {
10514 int dwarfReg = TRI->getDwarfRegNum(Op2.getReg(), false);
10515 if (dwarfReg < 0)
10516 return std::nullopt;
10517 else if (dwarfReg < 32) {
10518 Ops.push_back(dwarf::DW_OP_breg0 + dwarfReg);
10519 Ops.push_back(0);
10520 } else {
10521 Ops.push_back(dwarf::DW_OP_bregx);
10522 Ops.push_back(dwarfReg);
10523 Ops.push_back(0);
10524 }
10525 } else if (!Op) {
10526 assert(Op2.getReg() != X86::NoRegister);
10527 Op = &Op2;
10528 }
10529
10530 if (Coef > 1) {
10531 assert(Op2.getReg() != X86::NoRegister);
10532 Ops.push_back(dwarf::DW_OP_constu);
10533 Ops.push_back(Coef);
10534 Ops.push_back(dwarf::DW_OP_mul);
10535 }
10536
10537 if (((Op1.isReg() && Op1.getReg() != X86::NoRegister) || Op1.isFI()) &&
10538 Op2.getReg() != X86::NoRegister) {
10539 Ops.push_back(dwarf::DW_OP_plus);
10540 }
10541 }
10542
10544 Expr = DIExpression::get(MI.getMF()->getFunction().getContext(), Ops);
10545
10546 return ParamLoadedValue(*Op, Expr);
10547 }
10548 case X86::MOV8ri:
10549 case X86::MOV16ri:
10550 // TODO: Handle MOV8ri and MOV16ri.
10551 return std::nullopt;
10552 case X86::MOV32ri:
10553 case X86::MOV64ri:
10554 case X86::MOV64ri32:
10555 // MOV32ri may be used for producing zero-extended 32-bit immediates in
10556 // 64-bit parameters, so we need to consider super-registers.
10557 if (!TRI->isSuperRegisterEq(MI.getOperand(0).getReg(), Reg))
10558 return std::nullopt;
10559 return ParamLoadedValue(MI.getOperand(1), Expr);
10560 case X86::MOV8rr:
10561 case X86::MOV16rr:
10562 case X86::MOV32rr:
10563 case X86::MOV64rr:
10564 return describeMOVrrLoadedValue(MI, Reg, TRI);
10565 case X86::XOR32rr: {
10566 // 64-bit parameters are zero-materialized using XOR32rr, so also consider
10567 // super-registers.
10568 if (!TRI->isSuperRegisterEq(MI.getOperand(0).getReg(), Reg))
10569 return std::nullopt;
10570 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg())
10572 return std::nullopt;
10573 }
10574 case X86::MOVSX64rr32: {
10575 // We may need to describe the lower 32 bits of the MOVSX; for example, in
10576 // cases like this:
10577 //
10578 // $ebx = [...]
10579 // $rdi = MOVSX64rr32 $ebx
10580 // $esi = MOV32rr $edi
10581 if (!TRI->isSubRegisterEq(MI.getOperand(0).getReg(), Reg))
10582 return std::nullopt;
10583
10584 Expr = DIExpression::get(MI.getMF()->getFunction().getContext(), {});
10585
10586 // If the described register is the destination register we need to
10587 // sign-extend the source register from 32 bits. The other case we handle
10588 // is when the described register is the 32-bit sub-register of the
10589 // destination register, in case we just need to return the source
10590 // register.
10591 if (Reg == MI.getOperand(0).getReg())
10592 Expr = DIExpression::appendExt(Expr, 32, 64, true);
10593 else
10594 assert(getX86MCRegisterClass(X86::GR32RegClassID).contains(Reg) &&
10595 "Unhandled sub-register case for MOVSX64rr32");
10596
10597 return ParamLoadedValue(MI.getOperand(1), Expr);
10598 }
10599 default:
10600 assert(!MI.isMoveImmediate() && "Unexpected MoveImm instruction");
10602 }
10603}
10604
10605/// This is an architecture-specific helper function of reassociateOps.
10606/// Set special operand attributes for new instructions after reassociation.
10608 MachineInstr &OldMI2,
10609 MachineInstr &NewMI1,
10610 MachineInstr &NewMI2) const {
10611 // Integer instructions may define an implicit EFLAGS dest register operand.
10612 MachineOperand *OldFlagDef1 =
10613 OldMI1.findRegisterDefOperand(X86::EFLAGS, /*TRI=*/nullptr);
10614 MachineOperand *OldFlagDef2 =
10615 OldMI2.findRegisterDefOperand(X86::EFLAGS, /*TRI=*/nullptr);
10616
10617 assert(!OldFlagDef1 == !OldFlagDef2 &&
10618 "Unexpected instruction type for reassociation");
10619
10620 if (!OldFlagDef1 || !OldFlagDef2)
10621 return;
10622
10623 assert(OldFlagDef1->isDead() && OldFlagDef2->isDead() &&
10624 "Must have dead EFLAGS operand in reassociable instruction");
10625
10626 MachineOperand *NewFlagDef1 =
10627 NewMI1.findRegisterDefOperand(X86::EFLAGS, /*TRI=*/nullptr);
10628 MachineOperand *NewFlagDef2 =
10629 NewMI2.findRegisterDefOperand(X86::EFLAGS, /*TRI=*/nullptr);
10630
10631 assert(NewFlagDef1 && NewFlagDef2 &&
10632 "Unexpected operand in reassociable instruction");
10633
10634 // Mark the new EFLAGS operands as dead to be helpful to subsequent iterations
10635 // of this pass or other passes. The EFLAGS operands must be dead in these new
10636 // instructions because the EFLAGS operands in the original instructions must
10637 // be dead in order for reassociation to occur.
10638 NewFlagDef1->setIsDead();
10639 NewFlagDef2->setIsDead();
10640}
10641
10642std::pair<unsigned, unsigned>
10644 return std::make_pair(TF, 0u);
10645}
10646
10649 using namespace X86II;
10650 static const std::pair<unsigned, const char *> TargetFlags[] = {
10651 {MO_GOT_ABSOLUTE_ADDRESS, "x86-got-absolute-address"},
10652 {MO_PIC_BASE_OFFSET, "x86-pic-base-offset"},
10653 {MO_GOT, "x86-got"},
10654 {MO_GOTOFF, "x86-gotoff"},
10655 {MO_GOTPCREL, "x86-gotpcrel"},
10656 {MO_GOTPCREL_NORELAX, "x86-gotpcrel-norelax"},
10657 {MO_PLT, "x86-plt"},
10658 {MO_TLSGD, "x86-tlsgd"},
10659 {MO_TLSLD, "x86-tlsld"},
10660 {MO_TLSLDM, "x86-tlsldm"},
10661 {MO_GOTTPOFF, "x86-gottpoff"},
10662 {MO_INDNTPOFF, "x86-indntpoff"},
10663 {MO_TPOFF, "x86-tpoff"},
10664 {MO_DTPOFF, "x86-dtpoff"},
10665 {MO_NTPOFF, "x86-ntpoff"},
10666 {MO_GOTNTPOFF, "x86-gotntpoff"},
10667 {MO_DLLIMPORT, "x86-dllimport"},
10668 {MO_DARWIN_NONLAZY, "x86-darwin-nonlazy"},
10669 {MO_DARWIN_NONLAZY_PIC_BASE, "x86-darwin-nonlazy-pic-base"},
10670 {MO_TLVP, "x86-tlvp"},
10671 {MO_TLVP_PIC_BASE, "x86-tlvp-pic-base"},
10672 {MO_SECREL, "x86-secrel"},
10673 {MO_COFFSTUB, "x86-coffstub"}};
10674 return ArrayRef(TargetFlags);
10675}
10676
10677/// Constants defining how certain sequences should be outlined.
10678///
10679/// \p MachineOutlinerDefault implies that the function is called with a call
10680/// instruction, and a return must be emitted for the outlined function frame.
10681///
10682/// That is,
10683///
10684/// I1 OUTLINED_FUNCTION:
10685/// I2 --> call OUTLINED_FUNCTION I1
10686/// I3 I2
10687/// I3
10688/// ret
10689///
10690/// * Call construction overhead: 1 (call instruction)
10691/// * Frame construction overhead: 1 (return instruction)
10692///
10693/// \p MachineOutlinerTailCall implies that the function is being tail called.
10694/// A jump is emitted instead of a call, and the return is already present in
10695/// the outlined sequence. That is,
10696///
10697/// I1 OUTLINED_FUNCTION:
10698/// I2 --> jmp OUTLINED_FUNCTION I1
10699/// ret I2
10700/// ret
10701///
10702/// * Call construction overhead: 1 (jump instruction)
10703/// * Frame construction overhead: 0 (don't need to return)
10704///
10706
10707std::optional<std::unique_ptr<outliner::OutlinedFunction>>
10709 const MachineModuleInfo &MMI,
10710 std::vector<outliner::Candidate> &RepeatedSequenceLocs,
10711 unsigned MinRepeats) const {
10712 unsigned SequenceSize = 0;
10713 for (auto &MI : RepeatedSequenceLocs[0]) {
10714 // FIXME: x86 doesn't implement getInstSizeInBytes, so
10715 // we can't tell the cost. Just assume each instruction
10716 // is one byte.
10717 if (MI.isDebugInstr() || MI.isKill())
10718 continue;
10719 SequenceSize += 1;
10720 }
10721
10722 // We check to see if CFI Instructions are present, and if they are
10723 // we find the number of CFI Instructions in the candidates.
10724 unsigned CFICount = 0;
10725 for (auto &I : RepeatedSequenceLocs[0]) {
10726 if (I.isCFIInstruction())
10727 CFICount++;
10728 }
10729
10730 // We compare the number of found CFI Instructions to the number of CFI
10731 // instructions in the parent function for each candidate. We must check this
10732 // since if we outline one of the CFI instructions in a function, we have to
10733 // outline them all for correctness. If we do not, the address offsets will be
10734 // incorrect between the two sections of the program.
10735 for (outliner::Candidate &C : RepeatedSequenceLocs) {
10736 std::vector<MCCFIInstruction> CFIInstructions =
10737 C.getMF()->getFrameInstructions();
10738
10739 if (CFICount > 0 && CFICount != CFIInstructions.size())
10740 return std::nullopt;
10741 }
10742
10743 // FIXME: Use real size in bytes for call and ret instructions.
10744 if (RepeatedSequenceLocs[0].back().isTerminator()) {
10745 for (outliner::Candidate &C : RepeatedSequenceLocs)
10746 C.setCallInfo(MachineOutlinerTailCall, 1);
10747
10748 return std::make_unique<outliner::OutlinedFunction>(
10749 RepeatedSequenceLocs, SequenceSize,
10750 0, // Number of bytes to emit frame.
10751 MachineOutlinerTailCall // Type of frame.
10752 );
10753 }
10754
10755 if (CFICount > 0)
10756 return std::nullopt;
10757
10758 for (outliner::Candidate &C : RepeatedSequenceLocs)
10759 C.setCallInfo(MachineOutlinerDefault, 1);
10760
10761 return std::make_unique<outliner::OutlinedFunction>(
10762 RepeatedSequenceLocs, SequenceSize, 1, MachineOutlinerDefault);
10763}
10764
10766 MachineFunction &MF, bool OutlineFromLinkOnceODRs) const {
10767 const Function &F = MF.getFunction();
10768
10769 // Does the function use a red zone? If it does, then we can't risk messing
10770 // with the stack.
10771 if (Subtarget.getFrameLowering()->has128ByteRedZone(MF)) {
10772 // It could have a red zone. If it does, then we don't want to touch it.
10774 if (!X86FI || X86FI->getUsesRedZone())
10775 return false;
10776 }
10777
10778 // If we *don't* want to outline from things that could potentially be deduped
10779 // then return false.
10780 if (!OutlineFromLinkOnceODRs && F.hasLinkOnceODRLinkage())
10781 return false;
10782
10783 // This function is viable for outlining, so return true.
10784 return true;
10785}
10786
10790 unsigned Flags) const {
10791 MachineInstr &MI = *MIT;
10792
10793 // Is this a terminator for a basic block?
10794 if (MI.isTerminator())
10795 // TargetInstrInfo::getOutliningType has already filtered out anything
10796 // that would break this, so we can allow it here.
10798
10799 // Don't outline anything that modifies or reads from the stack pointer.
10800 //
10801 // FIXME: There are instructions which are being manually built without
10802 // explicit uses/defs so we also have to check the MCInstrDesc. We should be
10803 // able to remove the extra checks once those are fixed up. For example,
10804 // sometimes we might get something like %rax = POP64r 1. This won't be
10805 // caught by modifiesRegister or readsRegister even though the instruction
10806 // really ought to be formed so that modifiesRegister/readsRegister would
10807 // catch it.
10808 if (MI.modifiesRegister(X86::RSP, &RI) || MI.readsRegister(X86::RSP, &RI) ||
10809 MI.getDesc().hasImplicitUseOfPhysReg(X86::RSP) ||
10810 MI.getDesc().hasImplicitDefOfPhysReg(X86::RSP))
10812
10813 // Outlined calls change the instruction pointer, so don't read from it.
10814 if (MI.readsRegister(X86::RIP, &RI) ||
10815 MI.getDesc().hasImplicitUseOfPhysReg(X86::RIP) ||
10816 MI.getDesc().hasImplicitDefOfPhysReg(X86::RIP))
10818
10819 // Don't outline CFI instructions.
10820 if (MI.isCFIInstruction())
10822
10824}
10825
10828 const outliner::OutlinedFunction &OF) const {
10829 // If we're a tail call, we already have a return, so don't do anything.
10830 if (OF.FrameConstructionID == MachineOutlinerTailCall)
10831 return;
10832
10833 // We're a normal call, so our sequence doesn't have a return instruction.
10834 // Add it in.
10835 MachineInstr *retq = BuildMI(MF, DebugLoc(), get(X86::RET64));
10836 MBB.insert(MBB.end(), retq);
10837}
10838
10842 // Is it a tail call?
10843 if (C.CallConstructionID == MachineOutlinerTailCall) {
10844 // Yes, just insert a JMP.
10845 It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(X86::TAILJMPd64))
10846 .addGlobalAddress(M.getNamedValue(MF.getName())));
10847 } else {
10848 // No, insert a call.
10849 It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(X86::CALL64pcrel32))
10850 .addGlobalAddress(M.getNamedValue(MF.getName())));
10851 }
10852
10853 return It;
10854}
10855
10858 DebugLoc &DL,
10859 bool AllowSideEffects) const {
10860 const MachineFunction &MF = *MBB.getParent();
10861 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
10863
10864 if (ST.hasMMX() && X86::VR64RegClass.contains(Reg))
10865 // FIXME: Should we ignore MMX registers?
10866 return;
10867
10868 if (TRI.isGeneralPurposeRegister(MF, Reg)) {
10869 // Convert register to the 32-bit version. Both 'movl' and 'xorl' clear the
10870 // upper bits of a 64-bit register automagically.
10871 Reg = getX86SubSuperRegister(Reg, 32);
10872
10873 if (!AllowSideEffects)
10874 // XOR affects flags, so use a MOV instead.
10875 BuildMI(MBB, Iter, DL, get(X86::MOV32ri), Reg).addImm(0);
10876 else
10877 BuildMI(MBB, Iter, DL, get(X86::XOR32rr), Reg)
10878 .addReg(Reg, RegState::Undef)
10879 .addReg(Reg, RegState::Undef);
10880 } else if (X86::VR128RegClass.contains(Reg)) {
10881 // XMM#
10882 if (!ST.hasSSE1())
10883 return;
10884
10885 BuildMI(MBB, Iter, DL, get(X86::V_SET0), Reg);
10886 } else if (X86::VR256RegClass.contains(Reg)) {
10887 // YMM#
10888 if (!ST.hasAVX())
10889 return;
10890
10891 BuildMI(MBB, Iter, DL, get(X86::V_SET0), TRI.getSubReg(Reg, X86::sub_xmm));
10892 } else if (X86::VR512RegClass.contains(Reg)) {
10893 // ZMM#
10894 if (!ST.hasAVX512())
10895 return;
10896
10897 BuildMI(MBB, Iter, DL, get(X86::AVX512_128_SET0),
10898 TRI.getSubReg(Reg, X86::sub_xmm));
10899 } else if (X86::VK1RegClass.contains(Reg) || X86::VK2RegClass.contains(Reg) ||
10900 X86::VK4RegClass.contains(Reg) || X86::VK8RegClass.contains(Reg) ||
10901 X86::VK16RegClass.contains(Reg)) {
10902 if (!ST.hasVLX())
10903 return;
10904
10905 unsigned Op = ST.hasBWI() ? X86::KSET0Q : X86::KSET0W;
10906 BuildMI(MBB, Iter, DL, get(Op), Reg);
10907 }
10908}
10909
10911 MachineInstr &Root, SmallVectorImpl<unsigned> &Patterns,
10912 bool DoRegPressureReduce) const {
10913 unsigned Opc = Root.getOpcode();
10914 switch (Opc) {
10915 case X86::VPDPWSSDrr:
10916 case X86::VPDPWSSDrm:
10917 case X86::VPDPWSSDYrr:
10918 case X86::VPDPWSSDYrm: {
10919 if (!Subtarget.hasFastDPWSSD()) {
10921 return true;
10922 }
10923 break;
10924 }
10925 case X86::VPDPWSSDZ128rr:
10926 case X86::VPDPWSSDZ128rm:
10927 case X86::VPDPWSSDZ256rr:
10928 case X86::VPDPWSSDZ256rm:
10929 case X86::VPDPWSSDZrr:
10930 case X86::VPDPWSSDZrm: {
10931 if (Subtarget.hasBWI() && !Subtarget.hasFastDPWSSD()) {
10933 return true;
10934 }
10935 break;
10936 }
10937 }
10939 Patterns, DoRegPressureReduce);
10940}
10941
10942static void
10946 DenseMap<Register, unsigned> &InstrIdxForVirtReg) {
10947 MachineFunction *MF = Root.getMF();
10949
10950 unsigned Opc = Root.getOpcode();
10951 unsigned AddOpc = 0;
10952 unsigned MaddOpc = 0;
10953 switch (Opc) {
10954 default:
10955 assert(false && "It should not reach here");
10956 break;
10957 // vpdpwssd xmm2,xmm3,xmm1
10958 // -->
10959 // vpmaddwd xmm3,xmm3,xmm1
10960 // vpaddd xmm2,xmm2,xmm3
10961 case X86::VPDPWSSDrr:
10962 MaddOpc = X86::VPMADDWDrr;
10963 AddOpc = X86::VPADDDrr;
10964 break;
10965 case X86::VPDPWSSDrm:
10966 MaddOpc = X86::VPMADDWDrm;
10967 AddOpc = X86::VPADDDrr;
10968 break;
10969 case X86::VPDPWSSDZ128rr:
10970 MaddOpc = X86::VPMADDWDZ128rr;
10971 AddOpc = X86::VPADDDZ128rr;
10972 break;
10973 case X86::VPDPWSSDZ128rm:
10974 MaddOpc = X86::VPMADDWDZ128rm;
10975 AddOpc = X86::VPADDDZ128rr;
10976 break;
10977 // vpdpwssd ymm2,ymm3,ymm1
10978 // -->
10979 // vpmaddwd ymm3,ymm3,ymm1
10980 // vpaddd ymm2,ymm2,ymm3
10981 case X86::VPDPWSSDYrr:
10982 MaddOpc = X86::VPMADDWDYrr;
10983 AddOpc = X86::VPADDDYrr;
10984 break;
10985 case X86::VPDPWSSDYrm:
10986 MaddOpc = X86::VPMADDWDYrm;
10987 AddOpc = X86::VPADDDYrr;
10988 break;
10989 case X86::VPDPWSSDZ256rr:
10990 MaddOpc = X86::VPMADDWDZ256rr;
10991 AddOpc = X86::VPADDDZ256rr;
10992 break;
10993 case X86::VPDPWSSDZ256rm:
10994 MaddOpc = X86::VPMADDWDZ256rm;
10995 AddOpc = X86::VPADDDZ256rr;
10996 break;
10997 // vpdpwssd zmm2,zmm3,zmm1
10998 // -->
10999 // vpmaddwd zmm3,zmm3,zmm1
11000 // vpaddd zmm2,zmm2,zmm3
11001 case X86::VPDPWSSDZrr:
11002 MaddOpc = X86::VPMADDWDZrr;
11003 AddOpc = X86::VPADDDZrr;
11004 break;
11005 case X86::VPDPWSSDZrm:
11006 MaddOpc = X86::VPMADDWDZrm;
11007 AddOpc = X86::VPADDDZrr;
11008 break;
11009 }
11010 // Create vpmaddwd.
11011 const TargetRegisterClass *RC =
11012 RegInfo.getRegClass(Root.getOperand(0).getReg());
11013 Register NewReg = RegInfo.createVirtualRegister(RC);
11014 MachineInstr *Madd = Root.getMF()->CloneMachineInstr(&Root);
11015 Madd->setDesc(TII.get(MaddOpc));
11016 Madd->untieRegOperand(1);
11017 Madd->removeOperand(1);
11018 Madd->getOperand(0).setReg(NewReg);
11019 InstrIdxForVirtReg.insert(std::make_pair(NewReg, 0));
11020 // Create vpaddd.
11021 Register DstReg = Root.getOperand(0).getReg();
11022 bool IsKill = Root.getOperand(1).isKill();
11023 MachineInstr *Add =
11024 BuildMI(*MF, MIMetadata(Root), TII.get(AddOpc), DstReg)
11025 .addReg(Root.getOperand(1).getReg(), getKillRegState(IsKill))
11026 .addReg(Madd->getOperand(0).getReg(), getKillRegState(true));
11027 InsInstrs.push_back(Madd);
11028 InsInstrs.push_back(Add);
11029 DelInstrs.push_back(&Root);
11030}
11031
11033 MachineInstr &Root, unsigned Pattern,
11036 DenseMap<Register, unsigned> &InstrIdxForVirtReg) const {
11037 switch (Pattern) {
11038 default:
11039 // Reassociate instructions.
11041 DelInstrs, InstrIdxForVirtReg);
11042 return;
11044 genAlternativeDpCodeSequence(Root, *this, InsInstrs, DelInstrs,
11045 InstrIdxForVirtReg);
11046 return;
11047 }
11048}
11049
11050// See also: X86DAGToDAGISel::SelectInlineAsmMemoryOperand().
11052 int FI) const {
11055 M.Base.FrameIndex = FI;
11056 M.getFullAddress(Ops);
11057}
11058
11060X86InstrInfo::insertCodePrefetchInstr(MachineBasicBlock &MBB,
11061 MachineBasicBlock::iterator InsertBefore,
11062 const GlobalValue *GV) const {
11063 MachineFunction &MF = *MBB.getParent();
11064 MachineInstr *PrefetchInstr = MF.CreateMachineInstr(
11065 get(X86::PREFETCHIT1),
11066 InsertBefore == MBB.instr_end() ? MBB.findPrevDebugLoc(InsertBefore)
11067 : InsertBefore->getDebugLoc(),
11068 true);
11069 MachineInstrBuilder MIB(MF, PrefetchInstr);
11072 /*base_alignment=*/llvm::Align(1)));
11073 MIB.addReg(X86::RIP).addImm(1).addReg(X86::NoRegister);
11074 MIB.addGlobalAddress(GV);
11075 MIB.addReg(X86::NoRegister);
11076 MBB.insert(InsertBefore, PrefetchInstr);
11077 return PrefetchInstr;
11078}
11079
11080#define GET_INSTRINFO_HELPERS
11081#include "X86GenInstrInfo.inc"
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
return SDValue()
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!")
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:207
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
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:284
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:867
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Definition Function.h:691
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:688
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:353
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)
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:86
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1567
Set of metadata that should be preserved when using BuildMI().
SimpleValueType SimpleTy
MachineInstrBundleIterator< const MachineInstr > const_iterator
void push_back(MachineInstr *MI)
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 void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
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 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 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:67
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:343
static constexpr TypeSize getZero()
Definition TypeSize.h:349
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:309
static LLVM_ABI Type * getFP128Ty(LLVMContext &C)
Definition Type.cpp:291
static LLVM_ABI Type * getDoubleTy(LLVMContext &C)
Definition Type.cpp:287
static LLVM_ABI Type * getFloatTy(LLVMContext &C)
Definition Type.cpp:286
static LLVM_ABI Type * getHalfTy(LLVMContext &C)
Definition Type.cpp:284
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
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:94
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:36
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:315
@ Offset
Definition DWP.cpp:578
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:1739
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:2208
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:407
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:1753
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:1970
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:2052
@ 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.