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