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