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