LLVM 22.0.0git
X86ExpandPseudo.cpp
Go to the documentation of this file.
1//===------- X86ExpandPseudo.cpp - Expand pseudo instructions -------------===//
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 a pass that expands pseudo instructions into target
10// instructions to allow proper scheduling, if-conversion, other late
11// optimizations, or simply the encoding of the instructions.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
16#include "X86FrameLowering.h"
17#include "X86InstrInfo.h"
19#include "X86Subtarget.h"
23#include "llvm/CodeGen/Passes.h" // For IDs of passes that are preserved.
25#include "llvm/IR/GlobalValue.h"
27using namespace llvm;
28
29#define DEBUG_TYPE "x86-pseudo"
30#define X86_EXPAND_PSEUDO_NAME "X86 pseudo instruction expansion pass"
31
32namespace {
33class X86ExpandPseudo : public MachineFunctionPass {
34public:
35 static char ID;
36 X86ExpandPseudo() : MachineFunctionPass(ID) {}
37
38 void getAnalysisUsage(AnalysisUsage &AU) const override {
39 AU.setPreservesCFG();
43 }
44
45 const X86Subtarget *STI = nullptr;
46 const X86InstrInfo *TII = nullptr;
47 const X86RegisterInfo *TRI = nullptr;
48 const X86MachineFunctionInfo *X86FI = nullptr;
49 const X86FrameLowering *X86FL = nullptr;
50
51 bool runOnMachineFunction(MachineFunction &MF) override;
52
54 return MachineFunctionProperties().setNoVRegs();
55 }
56
57 StringRef getPassName() const override {
58 return "X86 pseudo instruction expansion pass";
59 }
60
61private:
62 void expandICallBranchFunnel(MachineBasicBlock *MBB,
64 void expandCALL_RVMARKER(MachineBasicBlock &MBB,
67 bool expandMBB(MachineBasicBlock &MBB);
68
69 /// This function expands pseudos which affects control flow.
70 /// It is done in separate pass to simplify blocks navigation in main
71 /// pass(calling expandMBB).
72 bool expandPseudosWhichAffectControlFlow(MachineFunction &MF);
73
74 /// Expand X86::VASTART_SAVE_XMM_REGS into set of xmm copying instructions,
75 /// placed into separate block guarded by check for al register(for SystemV
76 /// abi).
77 void expandVastartSaveXmmRegs(
78 MachineBasicBlock *EntryBlk,
79 MachineBasicBlock::iterator VAStartPseudoInstr) const;
80};
81char X86ExpandPseudo::ID = 0;
82
83} // End anonymous namespace.
84
86 false)
87
88void X86ExpandPseudo::expandICallBranchFunnel(
90 MachineBasicBlock *JTMBB = MBB;
91 MachineInstr *JTInst = &*MBBI;
93 const BasicBlock *BB = MBB->getBasicBlock();
94 auto InsPt = MachineFunction::iterator(MBB);
95 ++InsPt;
96
97 std::vector<std::pair<MachineBasicBlock *, unsigned>> TargetMBBs;
98 const DebugLoc &DL = JTInst->getDebugLoc();
99 MachineOperand Selector = JTInst->getOperand(0);
100 const GlobalValue *CombinedGlobal = JTInst->getOperand(1).getGlobal();
101
102 auto CmpTarget = [&](unsigned Target) {
103 if (Selector.isReg())
104 MBB->addLiveIn(Selector.getReg());
105 BuildMI(*MBB, MBBI, DL, TII->get(X86::LEA64r), X86::R11)
106 .addReg(X86::RIP)
107 .addImm(1)
108 .addReg(0)
109 .addGlobalAddress(CombinedGlobal,
110 JTInst->getOperand(2 + 2 * Target).getImm())
111 .addReg(0);
112 BuildMI(*MBB, MBBI, DL, TII->get(X86::CMP64rr))
113 .add(Selector)
114 .addReg(X86::R11);
115 };
116
117 auto CreateMBB = [&]() {
118 auto *NewMBB = MF->CreateMachineBasicBlock(BB);
119 MBB->addSuccessor(NewMBB);
120 if (!MBB->isLiveIn(X86::EFLAGS))
121 MBB->addLiveIn(X86::EFLAGS);
122 return NewMBB;
123 };
124
125 auto EmitCondJump = [&](unsigned CC, MachineBasicBlock *ThenMBB) {
126 BuildMI(*MBB, MBBI, DL, TII->get(X86::JCC_1)).addMBB(ThenMBB).addImm(CC);
127
128 auto *ElseMBB = CreateMBB();
129 MF->insert(InsPt, ElseMBB);
130 MBB = ElseMBB;
131 MBBI = MBB->end();
132 };
133
134 auto EmitCondJumpTarget = [&](unsigned CC, unsigned Target) {
135 auto *ThenMBB = CreateMBB();
136 TargetMBBs.push_back({ThenMBB, Target});
137 EmitCondJump(CC, ThenMBB);
138 };
139
140 auto EmitTailCall = [&](unsigned Target) {
141 BuildMI(*MBB, MBBI, DL, TII->get(X86::TAILJMPd64))
142 .add(JTInst->getOperand(3 + 2 * Target));
143 };
144
145 std::function<void(unsigned, unsigned)> EmitBranchFunnel =
146 [&](unsigned FirstTarget, unsigned NumTargets) {
147 if (NumTargets == 1) {
148 EmitTailCall(FirstTarget);
149 return;
150 }
151
152 if (NumTargets == 2) {
153 CmpTarget(FirstTarget + 1);
154 EmitCondJumpTarget(X86::COND_B, FirstTarget);
155 EmitTailCall(FirstTarget + 1);
156 return;
157 }
158
159 if (NumTargets < 6) {
160 CmpTarget(FirstTarget + 1);
161 EmitCondJumpTarget(X86::COND_B, FirstTarget);
162 EmitCondJumpTarget(X86::COND_E, FirstTarget + 1);
163 EmitBranchFunnel(FirstTarget + 2, NumTargets - 2);
164 return;
165 }
166
167 auto *ThenMBB = CreateMBB();
168 CmpTarget(FirstTarget + (NumTargets / 2));
169 EmitCondJump(X86::COND_B, ThenMBB);
170 EmitCondJumpTarget(X86::COND_E, FirstTarget + (NumTargets / 2));
171 EmitBranchFunnel(FirstTarget + (NumTargets / 2) + 1,
172 NumTargets - (NumTargets / 2) - 1);
173
174 MF->insert(InsPt, ThenMBB);
175 MBB = ThenMBB;
176 MBBI = MBB->end();
177 EmitBranchFunnel(FirstTarget, NumTargets / 2);
178 };
179
180 EmitBranchFunnel(0, (JTInst->getNumOperands() - 2) / 2);
181 for (auto P : TargetMBBs) {
182 MF->insert(InsPt, P.first);
183 BuildMI(P.first, DL, TII->get(X86::TAILJMPd64))
184 .add(JTInst->getOperand(3 + 2 * P.second));
185 }
186 JTMBB->erase(JTInst);
187}
188
189void X86ExpandPseudo::expandCALL_RVMARKER(MachineBasicBlock &MBB,
191 // Expand CALL_RVMARKER pseudo to call instruction, followed by the special
192 //"movq %rax, %rdi" marker.
193 MachineInstr &MI = *MBBI;
194
195 MachineInstr *OriginalCall;
196 assert((MI.getOperand(1).isGlobal() || MI.getOperand(1).isReg()) &&
197 "invalid operand for regular call");
198 unsigned Opc = -1;
199 if (MI.getOpcode() == X86::CALL64m_RVMARKER)
200 Opc = X86::CALL64m;
201 else if (MI.getOpcode() == X86::CALL64r_RVMARKER)
202 Opc = X86::CALL64r;
203 else if (MI.getOpcode() == X86::CALL64pcrel32_RVMARKER)
204 Opc = X86::CALL64pcrel32;
205 else
206 llvm_unreachable("unexpected opcode");
207
208 OriginalCall = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opc)).getInstr();
209 bool RAXImplicitDead = false;
210 for (MachineOperand &Op : llvm::drop_begin(MI.operands())) {
211 // RAX may be 'implicit dead', if there are no other users of the return
212 // value. We introduce a new use, so change it to 'implicit def'.
213 if (Op.isReg() && Op.isImplicit() && Op.isDead() &&
214 TRI->regsOverlap(Op.getReg(), X86::RAX)) {
215 Op.setIsDead(false);
216 Op.setIsDef(true);
217 RAXImplicitDead = true;
218 }
219 OriginalCall->addOperand(Op);
220 }
221
222 // Emit marker "movq %rax, %rdi". %rdi is not callee-saved, so it cannot be
223 // live across the earlier call. The call to the ObjC runtime function returns
224 // the first argument, so the value of %rax is unchanged after the ObjC
225 // runtime call. On Windows targets, the runtime call follows the regular
226 // x64 calling convention and expects the first argument in %rcx.
227 auto TargetReg = STI->getTargetTriple().isOSWindows() ? X86::RCX : X86::RDI;
228 auto *Marker = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(X86::MOV64rr))
229 .addReg(TargetReg, RegState::Define)
230 .addReg(X86::RAX)
231 .getInstr();
232 if (MI.shouldUpdateAdditionalCallInfo())
234
235 // Emit call to ObjC runtime.
236 const uint32_t *RegMask =
237 TRI->getCallPreservedMask(*MBB.getParent(), CallingConv::C);
238 MachineInstr *RtCall =
239 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(X86::CALL64pcrel32))
240 .addGlobalAddress(MI.getOperand(0).getGlobal(), 0, 0)
241 .addRegMask(RegMask)
242 .addReg(X86::RAX,
244 (RAXImplicitDead ? (RegState::Dead | RegState::Define)
246 .getInstr();
247 MI.eraseFromParent();
248
249 auto &TM = MBB.getParent()->getTarget();
250 // On Darwin platforms, wrap the expanded sequence in a bundle to prevent
251 // later optimizations from breaking up the sequence.
252 if (TM.getTargetTriple().isOSDarwin())
253 finalizeBundle(MBB, OriginalCall->getIterator(),
254 std::next(RtCall->getIterator()));
255}
256
257/// If \p MBBI is a pseudo instruction, this method expands
258/// it to the corresponding (sequence of) actual instruction(s).
259/// \returns true if \p MBBI has been expanded.
260bool X86ExpandPseudo::expandMI(MachineBasicBlock &MBB,
262 MachineInstr &MI = *MBBI;
263 unsigned Opcode = MI.getOpcode();
264 const DebugLoc &DL = MBBI->getDebugLoc();
265#define GET_EGPR_IF_ENABLED(OPC) (STI->hasEGPR() ? OPC##_EVEX : OPC)
266 switch (Opcode) {
267 default:
268 return false;
269 case X86::TCRETURNdi:
270 case X86::TCRETURNdicc:
271 case X86::TCRETURNri:
272 case X86::TCRETURNmi:
273 case X86::TCRETURNdi64:
274 case X86::TCRETURNdi64cc:
275 case X86::TCRETURNri64:
276 case X86::TCRETURNri64_ImpCall:
277 case X86::TCRETURNmi64: {
278 bool isMem = Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64;
279 MachineOperand &JumpTarget = MBBI->getOperand(0);
280 MachineOperand &StackAdjust = MBBI->getOperand(isMem ? X86::AddrNumOperands
281 : 1);
282 assert(StackAdjust.isImm() && "Expecting immediate value.");
283
284 // Adjust stack pointer.
285 int StackAdj = StackAdjust.getImm();
286 int MaxTCDelta = X86FI->getTCReturnAddrDelta();
287 int64_t Offset = 0;
288 assert(MaxTCDelta <= 0 && "MaxTCDelta should never be positive");
289
290 // Incoporate the retaddr area.
291 Offset = StackAdj - MaxTCDelta;
292 assert(Offset >= 0 && "Offset should never be negative");
293
294 if (Opcode == X86::TCRETURNdicc || Opcode == X86::TCRETURNdi64cc) {
295 assert(Offset == 0 && "Conditional tail call cannot adjust the stack.");
296 }
297
298 if (Offset) {
299 // Check for possible merge with preceding ADD instruction.
300 Offset = X86FL->mergeSPAdd(MBB, MBBI, Offset, true);
301 X86FL->emitSPUpdate(MBB, MBBI, DL, Offset, /*InEpilogue=*/true);
302 }
303
304 // Use this predicate to set REX prefix for X86_64 targets.
305 bool IsX64 = STI->isTargetWin64() || STI->isTargetUEFI64();
306 // Jump to label or value in register.
307 if (Opcode == X86::TCRETURNdi || Opcode == X86::TCRETURNdicc ||
308 Opcode == X86::TCRETURNdi64 || Opcode == X86::TCRETURNdi64cc) {
309 unsigned Op;
310 switch (Opcode) {
311 case X86::TCRETURNdi:
312 Op = X86::TAILJMPd;
313 break;
314 case X86::TCRETURNdicc:
315 Op = X86::TAILJMPd_CC;
316 break;
317 case X86::TCRETURNdi64cc:
319 "Conditional tail calls confuse "
320 "the Win64 unwinder.");
321 Op = X86::TAILJMPd64_CC;
322 break;
323 default:
324 // Note: Win64 uses REX prefixes indirect jumps out of functions, but
325 // not direct ones.
326 Op = X86::TAILJMPd64;
327 break;
328 }
329 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
330 if (JumpTarget.isGlobal()) {
331 MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
332 JumpTarget.getTargetFlags());
333 } else {
334 assert(JumpTarget.isSymbol());
335 MIB.addExternalSymbol(JumpTarget.getSymbolName(),
336 JumpTarget.getTargetFlags());
337 }
338 if (Op == X86::TAILJMPd_CC || Op == X86::TAILJMPd64_CC) {
339 MIB.addImm(MBBI->getOperand(2).getImm());
340 }
341
342 } else if (Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64) {
343 unsigned Op = (Opcode == X86::TCRETURNmi)
344 ? X86::TAILJMPm
345 : (IsX64 ? X86::TAILJMPm64_REX : X86::TAILJMPm64);
346 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
347 for (unsigned i = 0; i != X86::AddrNumOperands; ++i)
348 MIB.add(MBBI->getOperand(i));
349 } else if ((Opcode == X86::TCRETURNri64) ||
350 (Opcode == X86::TCRETURNri64_ImpCall)) {
351 JumpTarget.setIsKill();
352 BuildMI(MBB, MBBI, DL,
353 TII->get(IsX64 ? X86::TAILJMPr64_REX : X86::TAILJMPr64))
354 .add(JumpTarget);
355 } else {
356 assert(!IsX64 && "Win64 and UEFI64 require REX for indirect jumps.");
357 JumpTarget.setIsKill();
358 BuildMI(MBB, MBBI, DL, TII->get(X86::TAILJMPr))
359 .add(JumpTarget);
360 }
361
362 MachineInstr &NewMI = *std::prev(MBBI);
363 NewMI.copyImplicitOps(*MBBI->getParent()->getParent(), *MBBI);
364 NewMI.setCFIType(*MBB.getParent(), MI.getCFIType());
365
366 // Update the call info.
367 if (MBBI->isCandidateForAdditionalCallInfo())
369
370 // Delete the pseudo instruction TCRETURN.
371 MBB.erase(MBBI);
372
373 return true;
374 }
375 case X86::EH_RETURN:
376 case X86::EH_RETURN64: {
377 MachineOperand &DestAddr = MBBI->getOperand(0);
378 assert(DestAddr.isReg() && "Offset should be in register!");
379 const bool Uses64BitFramePtr = STI->isTarget64BitLP64();
380 Register StackPtr = TRI->getStackRegister();
381 BuildMI(MBB, MBBI, DL,
382 TII->get(Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr), StackPtr)
383 .addReg(DestAddr.getReg());
384 // The EH_RETURN pseudo is really removed during the MC Lowering.
385 return true;
386 }
387 case X86::IRET: {
388 // Adjust stack to erase error code
389 int64_t StackAdj = MBBI->getOperand(0).getImm();
390 X86FL->emitSPUpdate(MBB, MBBI, DL, StackAdj, true);
391 // Replace pseudo with machine iret
392 unsigned RetOp = STI->is64Bit() ? X86::IRET64 : X86::IRET32;
393 // Use UIRET if UINTR is present (except for building kernel)
394 if (STI->is64Bit() && STI->hasUINTR() &&
396 RetOp = X86::UIRET;
397 BuildMI(MBB, MBBI, DL, TII->get(RetOp));
398 MBB.erase(MBBI);
399 return true;
400 }
401 case X86::RET: {
402 // Adjust stack to erase error code
403 int64_t StackAdj = MBBI->getOperand(0).getImm();
405 if (StackAdj == 0) {
406 MIB = BuildMI(MBB, MBBI, DL,
407 TII->get(STI->is64Bit() ? X86::RET64 : X86::RET32));
408 } else if (isUInt<16>(StackAdj)) {
409 MIB = BuildMI(MBB, MBBI, DL,
410 TII->get(STI->is64Bit() ? X86::RETI64 : X86::RETI32))
411 .addImm(StackAdj);
412 } else {
413 assert(!STI->is64Bit() &&
414 "shouldn't need to do this for x86_64 targets!");
415 // A ret can only handle immediates as big as 2**16-1. If we need to pop
416 // off bytes before the return address, we must do it manually.
417 BuildMI(MBB, MBBI, DL, TII->get(X86::POP32r)).addReg(X86::ECX, RegState::Define);
418 X86FL->emitSPUpdate(MBB, MBBI, DL, StackAdj, /*InEpilogue=*/true);
419 BuildMI(MBB, MBBI, DL, TII->get(X86::PUSH32r)).addReg(X86::ECX);
420 MIB = BuildMI(MBB, MBBI, DL, TII->get(X86::RET32));
421 }
422 for (unsigned I = 1, E = MBBI->getNumOperands(); I != E; ++I)
423 MIB.add(MBBI->getOperand(I));
424 MBB.erase(MBBI);
425 return true;
426 }
427 case X86::LCMPXCHG16B_SAVE_RBX: {
428 // Perform the following transformation.
429 // SaveRbx = pseudocmpxchg Addr, <4 opds for the address>, InArg, SaveRbx
430 // =>
431 // RBX = InArg
432 // actualcmpxchg Addr
433 // RBX = SaveRbx
434 const MachineOperand &InArg = MBBI->getOperand(6);
435 Register SaveRbx = MBBI->getOperand(7).getReg();
436
437 // Copy the input argument of the pseudo into the argument of the
438 // actual instruction.
439 // NOTE: We don't copy the kill flag since the input might be the same reg
440 // as one of the other operands of LCMPXCHG16B.
441 TII->copyPhysReg(MBB, MBBI, DL, X86::RBX, InArg.getReg(), false);
442 // Create the actual instruction.
443 MachineInstr *NewInstr = BuildMI(MBB, MBBI, DL, TII->get(X86::LCMPXCHG16B));
444 // Copy the operands related to the address. If we access a frame variable,
445 // we need to replace the RBX base with SaveRbx, as RBX has another value.
446 const MachineOperand &Base = MBBI->getOperand(1);
447 if (Base.getReg() == X86::RBX || Base.getReg() == X86::EBX)
449 Base.getReg() == X86::RBX
450 ? SaveRbx
451 : Register(TRI->getSubReg(SaveRbx, X86::sub_32bit)),
452 /*IsDef=*/false));
453 else
454 NewInstr->addOperand(Base);
455 for (unsigned Idx = 1 + 1; Idx < 1 + X86::AddrNumOperands; ++Idx)
456 NewInstr->addOperand(MBBI->getOperand(Idx));
457 // Finally, restore the value of RBX.
458 TII->copyPhysReg(MBB, MBBI, DL, X86::RBX, SaveRbx,
459 /*SrcIsKill*/ true);
460
461 // Delete the pseudo.
463 return true;
464 }
465 // Loading/storing mask pairs requires two kmov operations. The second one of
466 // these needs a 2 byte displacement relative to the specified address (with
467 // 32 bit spill size). The pairs of 1bit masks up to 16 bit masks all use the
468 // same spill size, they all are stored using MASKPAIR16STORE, loaded using
469 // MASKPAIR16LOAD.
470 //
471 // The displacement value might wrap around in theory, thus the asserts in
472 // both cases.
473 case X86::MASKPAIR16LOAD: {
474 int64_t Disp = MBBI->getOperand(1 + X86::AddrDisp).getImm();
475 assert(Disp >= 0 && Disp <= INT32_MAX - 2 && "Unexpected displacement");
476 Register Reg = MBBI->getOperand(0).getReg();
477 bool DstIsDead = MBBI->getOperand(0).isDead();
478 Register Reg0 = TRI->getSubReg(Reg, X86::sub_mask_0);
479 Register Reg1 = TRI->getSubReg(Reg, X86::sub_mask_1);
480
481 auto MIBLo =
482 BuildMI(MBB, MBBI, DL, TII->get(GET_EGPR_IF_ENABLED(X86::KMOVWkm)))
483 .addReg(Reg0, RegState::Define | getDeadRegState(DstIsDead));
484 auto MIBHi =
485 BuildMI(MBB, MBBI, DL, TII->get(GET_EGPR_IF_ENABLED(X86::KMOVWkm)))
486 .addReg(Reg1, RegState::Define | getDeadRegState(DstIsDead));
487
488 for (int i = 0; i < X86::AddrNumOperands; ++i) {
489 MIBLo.add(MBBI->getOperand(1 + i));
490 if (i == X86::AddrDisp)
491 MIBHi.addImm(Disp + 2);
492 else
493 MIBHi.add(MBBI->getOperand(1 + i));
494 }
495
496 // Split the memory operand, adjusting the offset and size for the halves.
497 MachineMemOperand *OldMMO = MBBI->memoperands().front();
499 MachineMemOperand *MMOLo = MF->getMachineMemOperand(OldMMO, 0, 2);
500 MachineMemOperand *MMOHi = MF->getMachineMemOperand(OldMMO, 2, 2);
501
502 MIBLo.setMemRefs(MMOLo);
503 MIBHi.setMemRefs(MMOHi);
504
505 // Delete the pseudo.
506 MBB.erase(MBBI);
507 return true;
508 }
509 case X86::MASKPAIR16STORE: {
510 int64_t Disp = MBBI->getOperand(X86::AddrDisp).getImm();
511 assert(Disp >= 0 && Disp <= INT32_MAX - 2 && "Unexpected displacement");
512 Register Reg = MBBI->getOperand(X86::AddrNumOperands).getReg();
513 bool SrcIsKill = MBBI->getOperand(X86::AddrNumOperands).isKill();
514 Register Reg0 = TRI->getSubReg(Reg, X86::sub_mask_0);
515 Register Reg1 = TRI->getSubReg(Reg, X86::sub_mask_1);
516
517 auto MIBLo =
518 BuildMI(MBB, MBBI, DL, TII->get(GET_EGPR_IF_ENABLED(X86::KMOVWmk)));
519 auto MIBHi =
520 BuildMI(MBB, MBBI, DL, TII->get(GET_EGPR_IF_ENABLED(X86::KMOVWmk)));
521
522 for (int i = 0; i < X86::AddrNumOperands; ++i) {
523 MIBLo.add(MBBI->getOperand(i));
524 if (i == X86::AddrDisp)
525 MIBHi.addImm(Disp + 2);
526 else
527 MIBHi.add(MBBI->getOperand(i));
528 }
529 MIBLo.addReg(Reg0, getKillRegState(SrcIsKill));
530 MIBHi.addReg(Reg1, getKillRegState(SrcIsKill));
531
532 // Split the memory operand, adjusting the offset and size for the halves.
533 MachineMemOperand *OldMMO = MBBI->memoperands().front();
535 MachineMemOperand *MMOLo = MF->getMachineMemOperand(OldMMO, 0, 2);
536 MachineMemOperand *MMOHi = MF->getMachineMemOperand(OldMMO, 2, 2);
537
538 MIBLo.setMemRefs(MMOLo);
539 MIBHi.setMemRefs(MMOHi);
540
541 // Delete the pseudo.
542 MBB.erase(MBBI);
543 return true;
544 }
545 case X86::MWAITX_SAVE_RBX: {
546 // Perform the following transformation.
547 // SaveRbx = pseudomwaitx InArg, SaveRbx
548 // =>
549 // [E|R]BX = InArg
550 // actualmwaitx
551 // [E|R]BX = SaveRbx
552 const MachineOperand &InArg = MBBI->getOperand(1);
553 // Copy the input argument of the pseudo into the argument of the
554 // actual instruction.
555 TII->copyPhysReg(MBB, MBBI, DL, X86::EBX, InArg.getReg(), InArg.isKill());
556 // Create the actual instruction.
557 BuildMI(MBB, MBBI, DL, TII->get(X86::MWAITXrrr));
558 // Finally, restore the value of RBX.
559 Register SaveRbx = MBBI->getOperand(2).getReg();
560 TII->copyPhysReg(MBB, MBBI, DL, X86::RBX, SaveRbx, /*SrcIsKill*/ true);
561 // Delete the pseudo.
563 return true;
564 }
565 case TargetOpcode::ICALL_BRANCH_FUNNEL:
566 expandICallBranchFunnel(&MBB, MBBI);
567 return true;
568 case X86::PLDTILECFGV: {
569 MI.setDesc(TII->get(GET_EGPR_IF_ENABLED(X86::LDTILECFG)));
570 return true;
571 }
572 case X86::PTILELOADDV:
573 case X86::PTILELOADDT1V:
574 case X86::PTILELOADDRSV:
575 case X86::PTILELOADDRST1V:
576 case X86::PTCVTROWD2PSrreV:
577 case X86::PTCVTROWD2PSrriV:
578 case X86::PTCVTROWPS2BF16HrreV:
579 case X86::PTCVTROWPS2BF16HrriV:
580 case X86::PTCVTROWPS2BF16LrreV:
581 case X86::PTCVTROWPS2BF16LrriV:
582 case X86::PTCVTROWPS2PHHrreV:
583 case X86::PTCVTROWPS2PHHrriV:
584 case X86::PTCVTROWPS2PHLrreV:
585 case X86::PTCVTROWPS2PHLrriV:
586 case X86::PTILEMOVROWrreV:
587 case X86::PTILEMOVROWrriV: {
588 for (unsigned i = 2; i > 0; --i)
589 MI.removeOperand(i);
590 unsigned Opc;
591 switch (Opcode) {
592 case X86::PTILELOADDRSV:
593 Opc = GET_EGPR_IF_ENABLED(X86::TILELOADDRS);
594 break;
595 case X86::PTILELOADDRST1V:
596 Opc = GET_EGPR_IF_ENABLED(X86::TILELOADDRST1);
597 break;
598 case X86::PTILELOADDV:
599 Opc = GET_EGPR_IF_ENABLED(X86::TILELOADD);
600 break;
601 case X86::PTILELOADDT1V:
602 Opc = GET_EGPR_IF_ENABLED(X86::TILELOADDT1);
603 break;
604 case X86::PTCVTROWD2PSrreV:
605 Opc = X86::TCVTROWD2PSrre;
606 break;
607 case X86::PTCVTROWD2PSrriV:
608 Opc = X86::TCVTROWD2PSrri;
609 break;
610 case X86::PTCVTROWPS2BF16HrreV:
611 Opc = X86::TCVTROWPS2BF16Hrre;
612 break;
613 case X86::PTCVTROWPS2BF16HrriV:
614 Opc = X86::TCVTROWPS2BF16Hrri;
615 break;
616 case X86::PTCVTROWPS2BF16LrreV:
617 Opc = X86::TCVTROWPS2BF16Lrre;
618 break;
619 case X86::PTCVTROWPS2BF16LrriV:
620 Opc = X86::TCVTROWPS2BF16Lrri;
621 break;
622 case X86::PTCVTROWPS2PHHrreV:
623 Opc = X86::TCVTROWPS2PHHrre;
624 break;
625 case X86::PTCVTROWPS2PHHrriV:
626 Opc = X86::TCVTROWPS2PHHrri;
627 break;
628 case X86::PTCVTROWPS2PHLrreV:
629 Opc = X86::TCVTROWPS2PHLrre;
630 break;
631 case X86::PTCVTROWPS2PHLrriV:
632 Opc = X86::TCVTROWPS2PHLrri;
633 break;
634 case X86::PTILEMOVROWrreV:
635 Opc = X86::TILEMOVROWrre;
636 break;
637 case X86::PTILEMOVROWrriV:
638 Opc = X86::TILEMOVROWrri;
639 break;
640 default:
641 llvm_unreachable("Unexpected Opcode");
642 }
643 MI.setDesc(TII->get(Opc));
644 return true;
645 }
646 // TILEPAIRLOAD is just for TILEPair spill, we don't have corresponding
647 // AMX instruction to support it. So, split it to 2 load instructions:
648 // "TILEPAIRLOAD TMM0:TMM1, Base, Scale, Index, Offset, Segment" -->
649 // "TILELOAD TMM0, Base, Scale, Index, Offset, Segment" +
650 // "TILELOAD TMM1, Base, Scale, Index, Offset + TMM_SIZE, Segment"
651 case X86::PTILEPAIRLOAD: {
652 int64_t Disp = MBBI->getOperand(1 + X86::AddrDisp).getImm();
653 Register TReg = MBBI->getOperand(0).getReg();
654 bool DstIsDead = MBBI->getOperand(0).isDead();
655 Register TReg0 = TRI->getSubReg(TReg, X86::sub_t0);
656 Register TReg1 = TRI->getSubReg(TReg, X86::sub_t1);
657 unsigned TmmSize = TRI->getRegSizeInBits(X86::TILERegClass) / 8;
658
659 MachineInstrBuilder MIBLo =
660 BuildMI(MBB, MBBI, DL, TII->get(X86::TILELOADD))
661 .addReg(TReg0, RegState::Define | getDeadRegState(DstIsDead));
662 MachineInstrBuilder MIBHi =
663 BuildMI(MBB, MBBI, DL, TII->get(X86::TILELOADD))
664 .addReg(TReg1, RegState::Define | getDeadRegState(DstIsDead));
665
666 for (int i = 0; i < X86::AddrNumOperands; ++i) {
667 MIBLo.add(MBBI->getOperand(1 + i));
668 if (i == X86::AddrDisp)
669 MIBHi.addImm(Disp + TmmSize);
670 else
671 MIBHi.add(MBBI->getOperand(1 + i));
672 }
673
674 // Make sure the first stride reg used in first tileload is alive.
675 MachineOperand &Stride =
677 Stride.setIsKill(false);
678
679 // Split the memory operand, adjusting the offset and size for the halves.
680 MachineMemOperand *OldMMO = MBBI->memoperands().front();
682 MachineMemOperand *MMOLo = MF->getMachineMemOperand(OldMMO, 0, TmmSize);
683 MachineMemOperand *MMOHi =
684 MF->getMachineMemOperand(OldMMO, TmmSize, TmmSize);
685
686 MIBLo.setMemRefs(MMOLo);
687 MIBHi.setMemRefs(MMOHi);
688
689 // Delete the pseudo.
690 MBB.erase(MBBI);
691 return true;
692 }
693 // Similar with TILEPAIRLOAD, TILEPAIRSTORE is just for TILEPair spill, no
694 // corresponding AMX instruction to support it. So, split it too:
695 // "TILEPAIRSTORE Base, Scale, Index, Offset, Segment, TMM0:TMM1" -->
696 // "TILESTORE Base, Scale, Index, Offset, Segment, TMM0" +
697 // "TILESTORE Base, Scale, Index, Offset + TMM_SIZE, Segment, TMM1"
698 case X86::PTILEPAIRSTORE: {
699 int64_t Disp = MBBI->getOperand(X86::AddrDisp).getImm();
700 Register TReg = MBBI->getOperand(X86::AddrNumOperands).getReg();
701 bool SrcIsKill = MBBI->getOperand(X86::AddrNumOperands).isKill();
702 Register TReg0 = TRI->getSubReg(TReg, X86::sub_t0);
703 Register TReg1 = TRI->getSubReg(TReg, X86::sub_t1);
704 unsigned TmmSize = TRI->getRegSizeInBits(X86::TILERegClass) / 8;
705
706 MachineInstrBuilder MIBLo =
707 BuildMI(MBB, MBBI, DL, TII->get(X86::TILESTORED));
708 MachineInstrBuilder MIBHi =
709 BuildMI(MBB, MBBI, DL, TII->get(X86::TILESTORED));
710
711 for (int i = 0; i < X86::AddrNumOperands; ++i) {
712 MIBLo.add(MBBI->getOperand(i));
713 if (i == X86::AddrDisp)
714 MIBHi.addImm(Disp + TmmSize);
715 else
716 MIBHi.add(MBBI->getOperand(i));
717 }
718 MIBLo.addReg(TReg0, getKillRegState(SrcIsKill));
719 MIBHi.addReg(TReg1, getKillRegState(SrcIsKill));
720
721 // Make sure the first stride reg used in first tilestore is alive.
723 Stride.setIsKill(false);
724
725 // Split the memory operand, adjusting the offset and size for the halves.
726 MachineMemOperand *OldMMO = MBBI->memoperands().front();
728 MachineMemOperand *MMOLo = MF->getMachineMemOperand(OldMMO, 0, TmmSize);
729 MachineMemOperand *MMOHi =
730 MF->getMachineMemOperand(OldMMO, TmmSize, TmmSize);
731
732 MIBLo.setMemRefs(MMOLo);
733 MIBHi.setMemRefs(MMOHi);
734
735 // Delete the pseudo.
736 MBB.erase(MBBI);
737 return true;
738 }
739 case X86::PT2RPNTLVWZ0V:
740 case X86::PT2RPNTLVWZ0T1V:
741 case X86::PT2RPNTLVWZ1V:
742 case X86::PT2RPNTLVWZ1T1V:
743 case X86::PT2RPNTLVWZ0RSV:
744 case X86::PT2RPNTLVWZ0RST1V:
745 case X86::PT2RPNTLVWZ1RSV:
746 case X86::PT2RPNTLVWZ1RST1V: {
747 for (unsigned i = 3; i > 0; --i)
748 MI.removeOperand(i);
749 unsigned Opc;
750 switch (Opcode) {
751 case X86::PT2RPNTLVWZ0V:
752 Opc = GET_EGPR_IF_ENABLED(X86::T2RPNTLVWZ0);
753 break;
754 case X86::PT2RPNTLVWZ0T1V:
755 Opc = GET_EGPR_IF_ENABLED(X86::T2RPNTLVWZ0T1);
756 break;
757 case X86::PT2RPNTLVWZ1V:
758 Opc = GET_EGPR_IF_ENABLED(X86::T2RPNTLVWZ1);
759 break;
760 case X86::PT2RPNTLVWZ1T1V:
761 Opc = GET_EGPR_IF_ENABLED(X86::T2RPNTLVWZ1T1);
762 break;
763 case X86::PT2RPNTLVWZ0RSV:
764 Opc = GET_EGPR_IF_ENABLED(X86::T2RPNTLVWZ0RS);
765 break;
766 case X86::PT2RPNTLVWZ0RST1V:
767 Opc = GET_EGPR_IF_ENABLED(X86::T2RPNTLVWZ0RST1);
768 break;
769 case X86::PT2RPNTLVWZ1RSV:
770 Opc = GET_EGPR_IF_ENABLED(X86::T2RPNTLVWZ1RS);
771 break;
772 case X86::PT2RPNTLVWZ1RST1V:
773 Opc = GET_EGPR_IF_ENABLED(X86::T2RPNTLVWZ1RST1);
774 break;
775 default:
776 llvm_unreachable("Impossible Opcode!");
777 }
778 MI.setDesc(TII->get(Opc));
779 return true;
780 }
781 case X86::PTTRANSPOSEDV:
782 case X86::PTCONJTFP16V: {
783 for (int i = 2; i > 0; --i)
784 MI.removeOperand(i);
785 MI.setDesc(TII->get(Opcode == X86::PTTRANSPOSEDV ? X86::TTRANSPOSED
786 : X86::TCONJTFP16));
787 return true;
788 }
789 case X86::PTCMMIMFP16PSV:
790 case X86::PTCMMRLFP16PSV:
791 case X86::PTDPBSSDV:
792 case X86::PTDPBSUDV:
793 case X86::PTDPBUSDV:
794 case X86::PTDPBUUDV:
795 case X86::PTDPBF16PSV:
796 case X86::PTDPFP16PSV:
797 case X86::PTTDPBF16PSV:
798 case X86::PTTDPFP16PSV:
799 case X86::PTTCMMIMFP16PSV:
800 case X86::PTTCMMRLFP16PSV:
801 case X86::PTCONJTCMMIMFP16PSV:
802 case X86::PTMMULTF32PSV:
803 case X86::PTTMMULTF32PSV:
804 case X86::PTDPBF8PSV:
805 case X86::PTDPBHF8PSV:
806 case X86::PTDPHBF8PSV:
807 case X86::PTDPHF8PSV: {
808 MI.untieRegOperand(4);
809 for (unsigned i = 3; i > 0; --i)
810 MI.removeOperand(i);
811 unsigned Opc;
812 switch (Opcode) {
813 case X86::PTCMMIMFP16PSV: Opc = X86::TCMMIMFP16PS; break;
814 case X86::PTCMMRLFP16PSV: Opc = X86::TCMMRLFP16PS; break;
815 case X86::PTDPBSSDV: Opc = X86::TDPBSSD; break;
816 case X86::PTDPBSUDV: Opc = X86::TDPBSUD; break;
817 case X86::PTDPBUSDV: Opc = X86::TDPBUSD; break;
818 case X86::PTDPBUUDV: Opc = X86::TDPBUUD; break;
819 case X86::PTDPBF16PSV: Opc = X86::TDPBF16PS; break;
820 case X86::PTDPFP16PSV: Opc = X86::TDPFP16PS; break;
821 case X86::PTTDPBF16PSV:
822 Opc = X86::TTDPBF16PS;
823 break;
824 case X86::PTTDPFP16PSV:
825 Opc = X86::TTDPFP16PS;
826 break;
827 case X86::PTTCMMIMFP16PSV:
828 Opc = X86::TTCMMIMFP16PS;
829 break;
830 case X86::PTTCMMRLFP16PSV:
831 Opc = X86::TTCMMRLFP16PS;
832 break;
833 case X86::PTCONJTCMMIMFP16PSV:
834 Opc = X86::TCONJTCMMIMFP16PS;
835 break;
836 case X86::PTMMULTF32PSV:
837 Opc = X86::TMMULTF32PS;
838 break;
839 case X86::PTTMMULTF32PSV:
840 Opc = X86::TTMMULTF32PS;
841 break;
842 case X86::PTDPBF8PSV:
843 Opc = X86::TDPBF8PS;
844 break;
845 case X86::PTDPBHF8PSV:
846 Opc = X86::TDPBHF8PS;
847 break;
848 case X86::PTDPHBF8PSV:
849 Opc = X86::TDPHBF8PS;
850 break;
851 case X86::PTDPHF8PSV:
852 Opc = X86::TDPHF8PS;
853 break;
854
855 default:
856 llvm_unreachable("Unexpected Opcode");
857 }
858 MI.setDesc(TII->get(Opc));
859 MI.tieOperands(0, 1);
860 return true;
861 }
862 case X86::PTILESTOREDV: {
863 for (int i = 1; i >= 0; --i)
864 MI.removeOperand(i);
865 MI.setDesc(TII->get(GET_EGPR_IF_ENABLED(X86::TILESTORED)));
866 return true;
867 }
868#undef GET_EGPR_IF_ENABLED
869 case X86::PTILEZEROV: {
870 for (int i = 2; i > 0; --i) // Remove row, col
871 MI.removeOperand(i);
872 MI.setDesc(TII->get(X86::TILEZERO));
873 return true;
874 }
875 case X86::CALL64pcrel32_RVMARKER:
876 case X86::CALL64r_RVMARKER:
877 case X86::CALL64m_RVMARKER:
878 expandCALL_RVMARKER(MBB, MBBI);
879 return true;
880 case X86::CALL64r_ImpCall:
881 MI.setDesc(TII->get(X86::CALL64r));
882 return true;
883 case X86::ADD32mi_ND:
884 case X86::ADD64mi32_ND:
885 case X86::SUB32mi_ND:
886 case X86::SUB64mi32_ND:
887 case X86::AND32mi_ND:
888 case X86::AND64mi32_ND:
889 case X86::OR32mi_ND:
890 case X86::OR64mi32_ND:
891 case X86::XOR32mi_ND:
892 case X86::XOR64mi32_ND:
893 case X86::ADC32mi_ND:
894 case X86::ADC64mi32_ND:
895 case X86::SBB32mi_ND:
896 case X86::SBB64mi32_ND: {
897 // It's possible for an EVEX-encoded legacy instruction to reach the 15-byte
898 // instruction length limit: 4 bytes of EVEX prefix + 1 byte of opcode + 1
899 // byte of ModRM + 1 byte of SIB + 4 bytes of displacement + 4 bytes of
900 // immediate = 15 bytes in total, e.g.
901 //
902 // subq $184, %fs:257(%rbx, %rcx), %rax
903 //
904 // In such a case, no additional (ADSIZE or segment override) prefix can be
905 // used. To resolve the issue, we split the “long” instruction into 2
906 // instructions:
907 //
908 // movq %fs:257(%rbx, %rcx),%rax
909 // subq $184, %rax
910 //
911 // Therefore we consider the OPmi_ND to be a pseudo instruction to some
912 // extent.
913 const MachineOperand &ImmOp =
914 MI.getOperand(MI.getNumExplicitOperands() - 1);
915 // If the immediate is a expr, conservatively estimate 4 bytes.
916 if (ImmOp.isImm() && isInt<8>(ImmOp.getImm()))
917 return false;
918 int MemOpNo = X86::getFirstAddrOperandIdx(MI);
919 const MachineOperand &DispOp = MI.getOperand(MemOpNo + X86::AddrDisp);
920 Register Base = MI.getOperand(MemOpNo + X86::AddrBaseReg).getReg();
921 // If the displacement is a expr, conservatively estimate 4 bytes.
922 if (Base && DispOp.isImm() && isInt<8>(DispOp.getImm()))
923 return false;
924 // There can only be one of three: SIB, segment override register, ADSIZE
925 Register Index = MI.getOperand(MemOpNo + X86::AddrIndexReg).getReg();
926 unsigned Count = !!MI.getOperand(MemOpNo + X86::AddrSegmentReg).getReg();
927 if (X86II::needSIB(Base, Index, /*In64BitMode=*/true))
928 ++Count;
929 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(Base) ||
930 X86MCRegisterClasses[X86::GR32RegClassID].contains(Index))
931 ++Count;
932 if (Count < 2)
933 return false;
934 unsigned Opc, LoadOpc;
935 switch (Opcode) {
936#define MI_TO_RI(OP) \
937 case X86::OP##32mi_ND: \
938 Opc = X86::OP##32ri; \
939 LoadOpc = X86::MOV32rm; \
940 break; \
941 case X86::OP##64mi32_ND: \
942 Opc = X86::OP##64ri32; \
943 LoadOpc = X86::MOV64rm; \
944 break;
945
946 default:
947 llvm_unreachable("Unexpected Opcode");
948 MI_TO_RI(ADD);
949 MI_TO_RI(SUB);
950 MI_TO_RI(AND);
951 MI_TO_RI(OR);
952 MI_TO_RI(XOR);
953 MI_TO_RI(ADC);
954 MI_TO_RI(SBB);
955#undef MI_TO_RI
956 }
957 // Insert OPri.
958 Register DestReg = MI.getOperand(0).getReg();
959 BuildMI(MBB, std::next(MBBI), DL, TII->get(Opc), DestReg)
960 .addReg(DestReg)
961 .add(ImmOp);
962 // Change OPmi_ND to MOVrm.
963 for (unsigned I = MI.getNumImplicitOperands() + 1; I != 0; --I)
964 MI.removeOperand(MI.getNumOperands() - 1);
965 MI.setDesc(TII->get(LoadOpc));
966 return true;
967 }
968 }
969 llvm_unreachable("Previous switch has a fallthrough?");
970}
971
972// This function creates additional block for storing varargs guarded
973// registers. It adds check for %al into entry block, to skip
974// GuardedRegsBlk if xmm registers should not be stored.
975//
976// EntryBlk[VAStartPseudoInstr] EntryBlk
977// | | .
978// | | .
979// | | GuardedRegsBlk
980// | => | .
981// | | .
982// | TailBlk
983// | |
984// | |
985//
986void X86ExpandPseudo::expandVastartSaveXmmRegs(
987 MachineBasicBlock *EntryBlk,
988 MachineBasicBlock::iterator VAStartPseudoInstr) const {
989 assert(VAStartPseudoInstr->getOpcode() == X86::VASTART_SAVE_XMM_REGS);
990
991 MachineFunction *Func = EntryBlk->getParent();
992 const TargetInstrInfo *TII = STI->getInstrInfo();
993 const DebugLoc &DL = VAStartPseudoInstr->getDebugLoc();
994 Register CountReg = VAStartPseudoInstr->getOperand(0).getReg();
995
996 // Calculate liveins for newly created blocks.
997 LivePhysRegs LiveRegs(*STI->getRegisterInfo());
999
1000 LiveRegs.addLiveIns(*EntryBlk);
1001 for (MachineInstr &MI : EntryBlk->instrs()) {
1002 if (MI.getOpcode() == VAStartPseudoInstr->getOpcode())
1003 break;
1004
1005 LiveRegs.stepForward(MI, Clobbers);
1006 }
1007
1008 // Create the new basic blocks. One block contains all the XMM stores,
1009 // and another block is the final destination regardless of whether any
1010 // stores were performed.
1011 const BasicBlock *LLVMBlk = EntryBlk->getBasicBlock();
1012 MachineFunction::iterator EntryBlkIter = ++EntryBlk->getIterator();
1013 MachineBasicBlock *GuardedRegsBlk = Func->CreateMachineBasicBlock(LLVMBlk);
1014 MachineBasicBlock *TailBlk = Func->CreateMachineBasicBlock(LLVMBlk);
1015 Func->insert(EntryBlkIter, GuardedRegsBlk);
1016 Func->insert(EntryBlkIter, TailBlk);
1017
1018 // Transfer the remainder of EntryBlk and its successor edges to TailBlk.
1019 TailBlk->splice(TailBlk->begin(), EntryBlk,
1020 std::next(MachineBasicBlock::iterator(VAStartPseudoInstr)),
1021 EntryBlk->end());
1022 TailBlk->transferSuccessorsAndUpdatePHIs(EntryBlk);
1023
1024 uint64_t FrameOffset = VAStartPseudoInstr->getOperand(4).getImm();
1025 uint64_t VarArgsRegsOffset = VAStartPseudoInstr->getOperand(6).getImm();
1026
1027 // TODO: add support for YMM and ZMM here.
1028 unsigned MOVOpc = STI->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
1029
1030 // In the XMM save block, save all the XMM argument registers.
1031 for (int64_t OpndIdx = 7, RegIdx = 0;
1032 OpndIdx < VAStartPseudoInstr->getNumOperands() - 1;
1033 OpndIdx++, RegIdx++) {
1034 auto NewMI = BuildMI(GuardedRegsBlk, DL, TII->get(MOVOpc));
1035 for (int i = 0; i < X86::AddrNumOperands; ++i) {
1036 if (i == X86::AddrDisp)
1037 NewMI.addImm(FrameOffset + VarArgsRegsOffset + RegIdx * 16);
1038 else
1039 NewMI.add(VAStartPseudoInstr->getOperand(i + 1));
1040 }
1041 NewMI.addReg(VAStartPseudoInstr->getOperand(OpndIdx).getReg());
1042 assert(VAStartPseudoInstr->getOperand(OpndIdx).getReg().isPhysical());
1043 }
1044
1045 // The original block will now fall through to the GuardedRegsBlk.
1046 EntryBlk->addSuccessor(GuardedRegsBlk);
1047 // The GuardedRegsBlk will fall through to the TailBlk.
1048 GuardedRegsBlk->addSuccessor(TailBlk);
1049
1050 if (!STI->isCallingConvWin64(Func->getFunction().getCallingConv())) {
1051 // If %al is 0, branch around the XMM save block.
1052 BuildMI(EntryBlk, DL, TII->get(X86::TEST8rr))
1053 .addReg(CountReg)
1054 .addReg(CountReg);
1055 BuildMI(EntryBlk, DL, TII->get(X86::JCC_1))
1056 .addMBB(TailBlk)
1058 EntryBlk->addSuccessor(TailBlk);
1059 }
1060
1061 // Add liveins to the created block.
1062 addLiveIns(*GuardedRegsBlk, LiveRegs);
1063 addLiveIns(*TailBlk, LiveRegs);
1064
1065 // Delete the pseudo.
1066 VAStartPseudoInstr->eraseFromParent();
1067}
1068
1069/// Expand all pseudo instructions contained in \p MBB.
1070/// \returns true if any expansion occurred for \p MBB.
1071bool X86ExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
1072 bool Modified = false;
1073
1074 // MBBI may be invalidated by the expansion.
1076 while (MBBI != E) {
1077 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
1078 Modified |= expandMI(MBB, MBBI);
1079 MBBI = NMBBI;
1080 }
1081
1082 return Modified;
1083}
1084
1085bool X86ExpandPseudo::expandPseudosWhichAffectControlFlow(MachineFunction &MF) {
1086 // Currently pseudo which affects control flow is only
1087 // X86::VASTART_SAVE_XMM_REGS which is located in Entry block.
1088 // So we do not need to evaluate other blocks.
1089 for (MachineInstr &Instr : MF.front().instrs()) {
1090 if (Instr.getOpcode() == X86::VASTART_SAVE_XMM_REGS) {
1091 expandVastartSaveXmmRegs(&(MF.front()), Instr);
1092 return true;
1093 }
1094 }
1095
1096 return false;
1097}
1098
1099bool X86ExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
1100 STI = &MF.getSubtarget<X86Subtarget>();
1101 TII = STI->getInstrInfo();
1102 TRI = STI->getRegisterInfo();
1103 X86FI = MF.getInfo<X86MachineFunctionInfo>();
1104 X86FL = STI->getFrameLowering();
1105
1106 bool Modified = expandPseudosWhichAffectControlFlow(MF);
1107
1108 for (MachineBasicBlock &MBB : MF)
1109 Modified |= expandMBB(MBB);
1110 return Modified;
1111}
1112
1113/// Returns an instance of the pseudo instruction expansion pass.
1115 return new X86ExpandPseudo();
1116}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
#define I(x, y, z)
Definition: MD5.cpp:58
Register const TargetRegisterInfo * TRI
#define P(N)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:56
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition: Value.cpp:480
static Target * FirstTarget
#define MI_TO_RI(OP)
#define GET_EGPR_IF_ENABLED(OPC)
#define X86_EXPAND_PSEUDO_NAME
#define DEBUG_TYPE
Represent the analysis usage information of a pass.
AnalysisUsage & addPreservedID(const void *ID)
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:270
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
This class represents an Operation in the Expression.
A debug info location.
Definition: DebugLoc.h:124
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:314
void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, const DebugLoc &DL, Register DestReg, Register SrcReg, bool KillSrc, bool RenamableDest=false, bool RenamableSrc=false) const override
Emit instructions to copy a pair of physical registers.
A set of physical registers with utility functions to track liveness when walking backward/forward th...
Definition: LivePhysRegs.h:52
LLVM_ABI void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
LLVM_ABI void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
LLVM_ABI bool isLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
virtual MachineFunctionProperties getRequiredProperties() const
Properties which a MachineFunction may have at a given point in time.
void moveAdditionalCallInfo(const MachineInstr *Old, const MachineInstr *New)
Move the call site info from Old to \New call site info.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
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.
BasicBlockListType::iterator iterator
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineBasicBlock & front() const
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & setMemRefs(ArrayRef< MachineMemOperand * > MMOs) const
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addRegMask(const uint32_t *Mask) const
const MachineInstrBuilder & addGlobalAddress(const GlobalValue *GV, int64_t Offset=0, unsigned 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
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
Representation of each machine instruction.
Definition: MachineInstr.h:72
LLVM_ABI void setCFIType(MachineFunction &MF, uint32_t Type)
Set the CFI type for the instruction.
unsigned getNumOperands() const
Retuns the total number of operands.
Definition: MachineInstr.h:590
LLVM_ABI void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
LLVM_ABI void copyImplicitOps(MachineFunction &MF, const MachineInstr &MI)
Copy implicit register operands from specified instruction to this instruction.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:511
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:595
A description of a memory reference used in the backend.
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
void setIsKill(bool Val=true)
unsigned getTargetFlags() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
const char * getSymbolName() const
Register getReg() const
getReg - Returns the register number.
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.
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:85
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
TargetInstrInfo - Interface to description of machine instruction set.
CodeModel::Model getCodeModel() const
Returns the code model.
Target - Wrapper for Target specific information.
X86MachineFunctionInfo - This class is derived from MachineFunction and contains private X86 target-s...
self_iterator getIterator()
Definition: ilist_node.h:134
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Dead
Unused definition.
@ Define
Register definition.
Reg
All possible values of the reg field in the ModR/M byte.
bool needSIB(MCRegister BaseReg, MCRegister IndexReg, bool In64BitMode)
Definition: X86BaseInfo.h:1324
int getFirstAddrOperandIdx(const MachineInstr &MI)
Return the index of the instruction's first address operand, if it has a memory reference,...
@ AddrSegmentReg
Definition: X86BaseInfo.h:34
@ AddrIndexReg
Definition: X86BaseInfo.h:31
@ AddrNumOperands
Definition: X86BaseInfo.h:36
NodeAddr< InstrNode * > Instr
Definition: RDFGraph.h:389
NodeAddr< FuncNode * > Func
Definition: RDFGraph.h:393
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h:338
@ Offset
Definition: DWP.cpp:477
LLVM_ABI void finalizeBundle(MachineBasicBlock &MBB, MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
finalizeBundle - Finalize a machine instruction bundle which includes a sequence of instructions star...
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
static bool isMem(const MachineInstr &MI, unsigned Op)
Definition: X86InstrInfo.h:170
LLVM_ABI char & MachineDominatorsID
MachineDominators - This pass is a machine dominators analysis pass.
unsigned getDeadRegState(bool B)
LLVM_ABI char & MachineLoopInfoID
MachineLoopInfo - This pass is a loop analysis pass.
FunctionPass * createX86ExpandPseudoPass()
Return a Machine IR pass that expands X86-specific pseudo instructions into a sequence of actual inst...
unsigned getKillRegState(bool B)
DWARFExpression::Operation Op
void addLiveIns(MachineBasicBlock &MBB, const LivePhysRegs &LiveRegs)
Adds registers contained in LiveRegs to the block live-in list of MBB.