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
53 MachineFunctionProperties getRequiredProperties() const override {
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;
92 MachineFunction *MF = MBB->getParent();
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::TCRETURN_WIN64ri:
273 case X86::TCRETURN_HIPE32ri:
274 case X86::TCRETURNmi:
275 case X86::TCRETURNdi64:
276 case X86::TCRETURNdi64cc:
277 case X86::TCRETURNri64:
278 case X86::TCRETURNri64_ImpCall:
279 case X86::TCRETURNmi64:
280 case X86::TCRETURN_WINmi64: {
281 bool isMem = Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64 ||
282 Opcode == X86::TCRETURN_WINmi64;
283 MachineOperand &JumpTarget = MBBI->getOperand(0);
284 MachineOperand &StackAdjust = MBBI->getOperand(isMem ? X86::AddrNumOperands
285 : 1);
286 assert(StackAdjust.isImm() && "Expecting immediate value.");
287
288 // Adjust stack pointer.
289 int StackAdj = StackAdjust.getImm();
290 int MaxTCDelta = X86FI->getTCReturnAddrDelta();
291 int64_t Offset = 0;
292 assert(MaxTCDelta <= 0 && "MaxTCDelta should never be positive");
293
294 // Incoporate the retaddr area.
295 Offset = StackAdj - MaxTCDelta;
296 assert(Offset >= 0 && "Offset should never be negative");
297
298 if (Opcode == X86::TCRETURNdicc || Opcode == X86::TCRETURNdi64cc) {
299 assert(Offset == 0 && "Conditional tail call cannot adjust the stack.");
300 }
301
302 if (Offset) {
303 // Check for possible merge with preceding ADD instruction.
304 Offset = X86FL->mergeSPAdd(MBB, MBBI, Offset, true);
305 X86FL->emitSPUpdate(MBB, MBBI, DL, Offset, /*InEpilogue=*/true);
306 }
307
308 // Use this predicate to set REX prefix for X86_64 targets.
309 bool IsX64 = STI->isTargetWin64() || STI->isTargetUEFI64();
310 // Jump to label or value in register.
311 if (Opcode == X86::TCRETURNdi || Opcode == X86::TCRETURNdicc ||
312 Opcode == X86::TCRETURNdi64 || Opcode == X86::TCRETURNdi64cc) {
313 unsigned Op;
314 switch (Opcode) {
315 case X86::TCRETURNdi:
316 Op = X86::TAILJMPd;
317 break;
318 case X86::TCRETURNdicc:
319 Op = X86::TAILJMPd_CC;
320 break;
321 case X86::TCRETURNdi64cc:
323 "Conditional tail calls confuse "
324 "the Win64 unwinder.");
325 Op = X86::TAILJMPd64_CC;
326 break;
327 default:
328 // Note: Win64 uses REX prefixes indirect jumps out of functions, but
329 // not direct ones.
330 Op = X86::TAILJMPd64;
331 break;
332 }
333 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
334 if (JumpTarget.isGlobal()) {
335 MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
336 JumpTarget.getTargetFlags());
337 } else {
338 assert(JumpTarget.isSymbol());
339 MIB.addExternalSymbol(JumpTarget.getSymbolName(),
340 JumpTarget.getTargetFlags());
341 }
342 if (Op == X86::TAILJMPd_CC || Op == X86::TAILJMPd64_CC) {
343 MIB.addImm(MBBI->getOperand(2).getImm());
344 }
345
346 } else if (Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64 ||
347 Opcode == X86::TCRETURN_WINmi64) {
348 unsigned Op = (Opcode == X86::TCRETURNmi)
349 ? X86::TAILJMPm
350 : (IsX64 ? X86::TAILJMPm64_REX : X86::TAILJMPm64);
351 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
352 for (unsigned i = 0; i != X86::AddrNumOperands; ++i)
353 MIB.add(MBBI->getOperand(i));
354 } else if (Opcode == X86::TCRETURNri64 ||
355 Opcode == X86::TCRETURNri64_ImpCall ||
356 Opcode == X86::TCRETURN_WIN64ri) {
357 JumpTarget.setIsKill();
358 BuildMI(MBB, MBBI, DL,
359 TII->get(IsX64 ? X86::TAILJMPr64_REX : X86::TAILJMPr64))
360 .add(JumpTarget);
361 } else {
362 assert(!IsX64 && "Win64 and UEFI64 require REX for indirect jumps.");
363 JumpTarget.setIsKill();
364 BuildMI(MBB, MBBI, DL, TII->get(X86::TAILJMPr))
365 .add(JumpTarget);
366 }
367
368 MachineInstr &NewMI = *std::prev(MBBI);
369 NewMI.copyImplicitOps(*MBBI->getParent()->getParent(), *MBBI);
370 NewMI.setCFIType(*MBB.getParent(), MI.getCFIType());
371
372 // Update the call info.
373 if (MBBI->isCandidateForAdditionalCallInfo())
375
376 // Delete the pseudo instruction TCRETURN.
377 MBB.erase(MBBI);
378
379 return true;
380 }
381 case X86::EH_RETURN:
382 case X86::EH_RETURN64: {
383 MachineOperand &DestAddr = MBBI->getOperand(0);
384 assert(DestAddr.isReg() && "Offset should be in register!");
385 const bool Uses64BitFramePtr = STI->isTarget64BitLP64();
386 Register StackPtr = TRI->getStackRegister();
387 BuildMI(MBB, MBBI, DL,
388 TII->get(Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr), StackPtr)
389 .addReg(DestAddr.getReg());
390 // The EH_RETURN pseudo is really removed during the MC Lowering.
391 return true;
392 }
393 case X86::IRET: {
394 // Adjust stack to erase error code
395 int64_t StackAdj = MBBI->getOperand(0).getImm();
396 X86FL->emitSPUpdate(MBB, MBBI, DL, StackAdj, true);
397 // Replace pseudo with machine iret
398 unsigned RetOp = STI->is64Bit() ? X86::IRET64 : X86::IRET32;
399 // Use UIRET if UINTR is present (except for building kernel)
400 if (STI->is64Bit() && STI->hasUINTR() &&
402 RetOp = X86::UIRET;
403 BuildMI(MBB, MBBI, DL, TII->get(RetOp));
404 MBB.erase(MBBI);
405 return true;
406 }
407 case X86::RET: {
408 // Adjust stack to erase error code
409 int64_t StackAdj = MBBI->getOperand(0).getImm();
410 MachineInstrBuilder MIB;
411 if (StackAdj == 0) {
412 MIB = BuildMI(MBB, MBBI, DL,
413 TII->get(STI->is64Bit() ? X86::RET64 : X86::RET32));
414 } else if (isUInt<16>(StackAdj)) {
415 MIB = BuildMI(MBB, MBBI, DL,
416 TII->get(STI->is64Bit() ? X86::RETI64 : X86::RETI32))
417 .addImm(StackAdj);
418 } else {
419 assert(!STI->is64Bit() &&
420 "shouldn't need to do this for x86_64 targets!");
421 // A ret can only handle immediates as big as 2**16-1. If we need to pop
422 // off bytes before the return address, we must do it manually.
423 BuildMI(MBB, MBBI, DL, TII->get(X86::POP32r)).addReg(X86::ECX, RegState::Define);
424 X86FL->emitSPUpdate(MBB, MBBI, DL, StackAdj, /*InEpilogue=*/true);
425 BuildMI(MBB, MBBI, DL, TII->get(X86::PUSH32r)).addReg(X86::ECX);
426 MIB = BuildMI(MBB, MBBI, DL, TII->get(X86::RET32));
427 }
428 for (unsigned I = 1, E = MBBI->getNumOperands(); I != E; ++I)
429 MIB.add(MBBI->getOperand(I));
430 MBB.erase(MBBI);
431 return true;
432 }
433 case X86::LCMPXCHG16B_SAVE_RBX: {
434 // Perform the following transformation.
435 // SaveRbx = pseudocmpxchg Addr, <4 opds for the address>, InArg, SaveRbx
436 // =>
437 // RBX = InArg
438 // actualcmpxchg Addr
439 // RBX = SaveRbx
440 const MachineOperand &InArg = MBBI->getOperand(6);
441 Register SaveRbx = MBBI->getOperand(7).getReg();
442
443 // Copy the input argument of the pseudo into the argument of the
444 // actual instruction.
445 // NOTE: We don't copy the kill flag since the input might be the same reg
446 // as one of the other operands of LCMPXCHG16B.
447 TII->copyPhysReg(MBB, MBBI, DL, X86::RBX, InArg.getReg(), false);
448 // Create the actual instruction.
449 MachineInstr *NewInstr = BuildMI(MBB, MBBI, DL, TII->get(X86::LCMPXCHG16B));
450 // Copy the operands related to the address. If we access a frame variable,
451 // we need to replace the RBX base with SaveRbx, as RBX has another value.
452 const MachineOperand &Base = MBBI->getOperand(1);
453 if (Base.getReg() == X86::RBX || Base.getReg() == X86::EBX)
455 Base.getReg() == X86::RBX
456 ? SaveRbx
457 : Register(TRI->getSubReg(SaveRbx, X86::sub_32bit)),
458 /*IsDef=*/false));
459 else
460 NewInstr->addOperand(Base);
461 for (unsigned Idx = 1 + 1; Idx < 1 + X86::AddrNumOperands; ++Idx)
462 NewInstr->addOperand(MBBI->getOperand(Idx));
463 // Finally, restore the value of RBX.
464 TII->copyPhysReg(MBB, MBBI, DL, X86::RBX, SaveRbx,
465 /*SrcIsKill*/ true);
466
467 // Delete the pseudo.
469 return true;
470 }
471 // Loading/storing mask pairs requires two kmov operations. The second one of
472 // these needs a 2 byte displacement relative to the specified address (with
473 // 32 bit spill size). The pairs of 1bit masks up to 16 bit masks all use the
474 // same spill size, they all are stored using MASKPAIR16STORE, loaded using
475 // MASKPAIR16LOAD.
476 //
477 // The displacement value might wrap around in theory, thus the asserts in
478 // both cases.
479 case X86::MASKPAIR16LOAD: {
480 int64_t Disp = MBBI->getOperand(1 + X86::AddrDisp).getImm();
481 assert(Disp >= 0 && Disp <= INT32_MAX - 2 && "Unexpected displacement");
482 Register Reg = MBBI->getOperand(0).getReg();
483 bool DstIsDead = MBBI->getOperand(0).isDead();
484 Register Reg0 = TRI->getSubReg(Reg, X86::sub_mask_0);
485 Register Reg1 = TRI->getSubReg(Reg, X86::sub_mask_1);
486
487 auto MIBLo =
488 BuildMI(MBB, MBBI, DL, TII->get(GET_EGPR_IF_ENABLED(X86::KMOVWkm)))
489 .addReg(Reg0, RegState::Define | getDeadRegState(DstIsDead));
490 auto MIBHi =
491 BuildMI(MBB, MBBI, DL, TII->get(GET_EGPR_IF_ENABLED(X86::KMOVWkm)))
492 .addReg(Reg1, RegState::Define | getDeadRegState(DstIsDead));
493
494 for (int i = 0; i < X86::AddrNumOperands; ++i) {
495 MIBLo.add(MBBI->getOperand(1 + i));
496 if (i == X86::AddrDisp)
497 MIBHi.addImm(Disp + 2);
498 else
499 MIBHi.add(MBBI->getOperand(1 + i));
500 }
501
502 // Split the memory operand, adjusting the offset and size for the halves.
503 MachineMemOperand *OldMMO = MBBI->memoperands().front();
504 MachineFunction *MF = MBB.getParent();
505 MachineMemOperand *MMOLo = MF->getMachineMemOperand(OldMMO, 0, 2);
506 MachineMemOperand *MMOHi = MF->getMachineMemOperand(OldMMO, 2, 2);
507
508 MIBLo.setMemRefs(MMOLo);
509 MIBHi.setMemRefs(MMOHi);
510
511 // Delete the pseudo.
512 MBB.erase(MBBI);
513 return true;
514 }
515 case X86::MASKPAIR16STORE: {
516 int64_t Disp = MBBI->getOperand(X86::AddrDisp).getImm();
517 assert(Disp >= 0 && Disp <= INT32_MAX - 2 && "Unexpected displacement");
518 Register Reg = MBBI->getOperand(X86::AddrNumOperands).getReg();
519 bool SrcIsKill = MBBI->getOperand(X86::AddrNumOperands).isKill();
520 Register Reg0 = TRI->getSubReg(Reg, X86::sub_mask_0);
521 Register Reg1 = TRI->getSubReg(Reg, X86::sub_mask_1);
522
523 auto MIBLo =
524 BuildMI(MBB, MBBI, DL, TII->get(GET_EGPR_IF_ENABLED(X86::KMOVWmk)));
525 auto MIBHi =
526 BuildMI(MBB, MBBI, DL, TII->get(GET_EGPR_IF_ENABLED(X86::KMOVWmk)));
527
528 for (int i = 0; i < X86::AddrNumOperands; ++i) {
529 MIBLo.add(MBBI->getOperand(i));
530 if (i == X86::AddrDisp)
531 MIBHi.addImm(Disp + 2);
532 else
533 MIBHi.add(MBBI->getOperand(i));
534 }
535 MIBLo.addReg(Reg0, getKillRegState(SrcIsKill));
536 MIBHi.addReg(Reg1, getKillRegState(SrcIsKill));
537
538 // Split the memory operand, adjusting the offset and size for the halves.
539 MachineMemOperand *OldMMO = MBBI->memoperands().front();
540 MachineFunction *MF = MBB.getParent();
541 MachineMemOperand *MMOLo = MF->getMachineMemOperand(OldMMO, 0, 2);
542 MachineMemOperand *MMOHi = MF->getMachineMemOperand(OldMMO, 2, 2);
543
544 MIBLo.setMemRefs(MMOLo);
545 MIBHi.setMemRefs(MMOHi);
546
547 // Delete the pseudo.
548 MBB.erase(MBBI);
549 return true;
550 }
551 case X86::MWAITX_SAVE_RBX: {
552 // Perform the following transformation.
553 // SaveRbx = pseudomwaitx InArg, SaveRbx
554 // =>
555 // [E|R]BX = InArg
556 // actualmwaitx
557 // [E|R]BX = SaveRbx
558 const MachineOperand &InArg = MBBI->getOperand(1);
559 // Copy the input argument of the pseudo into the argument of the
560 // actual instruction.
561 TII->copyPhysReg(MBB, MBBI, DL, X86::EBX, InArg.getReg(), InArg.isKill());
562 // Create the actual instruction.
563 BuildMI(MBB, MBBI, DL, TII->get(X86::MWAITXrrr));
564 // Finally, restore the value of RBX.
565 Register SaveRbx = MBBI->getOperand(2).getReg();
566 TII->copyPhysReg(MBB, MBBI, DL, X86::RBX, SaveRbx, /*SrcIsKill*/ true);
567 // Delete the pseudo.
569 return true;
570 }
571 case TargetOpcode::ICALL_BRANCH_FUNNEL:
572 expandICallBranchFunnel(&MBB, MBBI);
573 return true;
574 case X86::PLDTILECFGV: {
575 MI.setDesc(TII->get(GET_EGPR_IF_ENABLED(X86::LDTILECFG)));
576 return true;
577 }
578 case X86::PTILELOADDV:
579 case X86::PTILELOADDT1V:
580 case X86::PTILELOADDRSV:
581 case X86::PTILELOADDRST1V:
582 case X86::PTCVTROWD2PSrreV:
583 case X86::PTCVTROWD2PSrriV:
584 case X86::PTCVTROWPS2BF16HrreV:
585 case X86::PTCVTROWPS2BF16HrriV:
586 case X86::PTCVTROWPS2BF16LrreV:
587 case X86::PTCVTROWPS2BF16LrriV:
588 case X86::PTCVTROWPS2PHHrreV:
589 case X86::PTCVTROWPS2PHHrriV:
590 case X86::PTCVTROWPS2PHLrreV:
591 case X86::PTCVTROWPS2PHLrriV:
592 case X86::PTILEMOVROWrreV:
593 case X86::PTILEMOVROWrriV: {
594 for (unsigned i = 2; i > 0; --i)
595 MI.removeOperand(i);
596 unsigned Opc;
597 switch (Opcode) {
598 case X86::PTILELOADDRSV:
599 Opc = GET_EGPR_IF_ENABLED(X86::TILELOADDRS);
600 break;
601 case X86::PTILELOADDRST1V:
602 Opc = GET_EGPR_IF_ENABLED(X86::TILELOADDRST1);
603 break;
604 case X86::PTILELOADDV:
605 Opc = GET_EGPR_IF_ENABLED(X86::TILELOADD);
606 break;
607 case X86::PTILELOADDT1V:
608 Opc = GET_EGPR_IF_ENABLED(X86::TILELOADDT1);
609 break;
610 case X86::PTCVTROWD2PSrreV:
611 Opc = X86::TCVTROWD2PSrre;
612 break;
613 case X86::PTCVTROWD2PSrriV:
614 Opc = X86::TCVTROWD2PSrri;
615 break;
616 case X86::PTCVTROWPS2BF16HrreV:
617 Opc = X86::TCVTROWPS2BF16Hrre;
618 break;
619 case X86::PTCVTROWPS2BF16HrriV:
620 Opc = X86::TCVTROWPS2BF16Hrri;
621 break;
622 case X86::PTCVTROWPS2BF16LrreV:
623 Opc = X86::TCVTROWPS2BF16Lrre;
624 break;
625 case X86::PTCVTROWPS2BF16LrriV:
626 Opc = X86::TCVTROWPS2BF16Lrri;
627 break;
628 case X86::PTCVTROWPS2PHHrreV:
629 Opc = X86::TCVTROWPS2PHHrre;
630 break;
631 case X86::PTCVTROWPS2PHHrriV:
632 Opc = X86::TCVTROWPS2PHHrri;
633 break;
634 case X86::PTCVTROWPS2PHLrreV:
635 Opc = X86::TCVTROWPS2PHLrre;
636 break;
637 case X86::PTCVTROWPS2PHLrriV:
638 Opc = X86::TCVTROWPS2PHLrri;
639 break;
640 case X86::PTILEMOVROWrreV:
641 Opc = X86::TILEMOVROWrre;
642 break;
643 case X86::PTILEMOVROWrriV:
644 Opc = X86::TILEMOVROWrri;
645 break;
646 default:
647 llvm_unreachable("Unexpected Opcode");
648 }
649 MI.setDesc(TII->get(Opc));
650 return true;
651 }
652 // TILEPAIRLOAD is just for TILEPair spill, we don't have corresponding
653 // AMX instruction to support it. So, split it to 2 load instructions:
654 // "TILEPAIRLOAD TMM0:TMM1, Base, Scale, Index, Offset, Segment" -->
655 // "TILELOAD TMM0, Base, Scale, Index, Offset, Segment" +
656 // "TILELOAD TMM1, Base, Scale, Index, Offset + TMM_SIZE, Segment"
657 case X86::PTILEPAIRLOAD: {
658 int64_t Disp = MBBI->getOperand(1 + X86::AddrDisp).getImm();
659 Register TReg = MBBI->getOperand(0).getReg();
660 bool DstIsDead = MBBI->getOperand(0).isDead();
661 Register TReg0 = TRI->getSubReg(TReg, X86::sub_t0);
662 Register TReg1 = TRI->getSubReg(TReg, X86::sub_t1);
663 unsigned TmmSize = TRI->getRegSizeInBits(X86::TILERegClass) / 8;
664
665 MachineInstrBuilder MIBLo =
666 BuildMI(MBB, MBBI, DL, TII->get(X86::TILELOADD))
667 .addReg(TReg0, RegState::Define | getDeadRegState(DstIsDead));
668 MachineInstrBuilder MIBHi =
669 BuildMI(MBB, MBBI, DL, TII->get(X86::TILELOADD))
670 .addReg(TReg1, RegState::Define | getDeadRegState(DstIsDead));
671
672 for (int i = 0; i < X86::AddrNumOperands; ++i) {
673 MIBLo.add(MBBI->getOperand(1 + i));
674 if (i == X86::AddrDisp)
675 MIBHi.addImm(Disp + TmmSize);
676 else
677 MIBHi.add(MBBI->getOperand(1 + i));
678 }
679
680 // Make sure the first stride reg used in first tileload is alive.
681 MachineOperand &Stride =
683 Stride.setIsKill(false);
684
685 // Split the memory operand, adjusting the offset and size for the halves.
686 MachineMemOperand *OldMMO = MBBI->memoperands().front();
687 MachineFunction *MF = MBB.getParent();
688 MachineMemOperand *MMOLo = MF->getMachineMemOperand(OldMMO, 0, TmmSize);
689 MachineMemOperand *MMOHi =
690 MF->getMachineMemOperand(OldMMO, TmmSize, TmmSize);
691
692 MIBLo.setMemRefs(MMOLo);
693 MIBHi.setMemRefs(MMOHi);
694
695 // Delete the pseudo.
696 MBB.erase(MBBI);
697 return true;
698 }
699 // Similar with TILEPAIRLOAD, TILEPAIRSTORE is just for TILEPair spill, no
700 // corresponding AMX instruction to support it. So, split it too:
701 // "TILEPAIRSTORE Base, Scale, Index, Offset, Segment, TMM0:TMM1" -->
702 // "TILESTORE Base, Scale, Index, Offset, Segment, TMM0" +
703 // "TILESTORE Base, Scale, Index, Offset + TMM_SIZE, Segment, TMM1"
704 case X86::PTILEPAIRSTORE: {
705 int64_t Disp = MBBI->getOperand(X86::AddrDisp).getImm();
706 Register TReg = MBBI->getOperand(X86::AddrNumOperands).getReg();
707 bool SrcIsKill = MBBI->getOperand(X86::AddrNumOperands).isKill();
708 Register TReg0 = TRI->getSubReg(TReg, X86::sub_t0);
709 Register TReg1 = TRI->getSubReg(TReg, X86::sub_t1);
710 unsigned TmmSize = TRI->getRegSizeInBits(X86::TILERegClass) / 8;
711
712 MachineInstrBuilder MIBLo =
713 BuildMI(MBB, MBBI, DL, TII->get(X86::TILESTORED));
714 MachineInstrBuilder MIBHi =
715 BuildMI(MBB, MBBI, DL, TII->get(X86::TILESTORED));
716
717 for (int i = 0; i < X86::AddrNumOperands; ++i) {
718 MIBLo.add(MBBI->getOperand(i));
719 if (i == X86::AddrDisp)
720 MIBHi.addImm(Disp + TmmSize);
721 else
722 MIBHi.add(MBBI->getOperand(i));
723 }
724 MIBLo.addReg(TReg0, getKillRegState(SrcIsKill));
725 MIBHi.addReg(TReg1, getKillRegState(SrcIsKill));
726
727 // Make sure the first stride reg used in first tilestore is alive.
728 MachineOperand &Stride = MIBLo.getInstr()->getOperand(X86::AddrIndexReg);
729 Stride.setIsKill(false);
730
731 // Split the memory operand, adjusting the offset and size for the halves.
732 MachineMemOperand *OldMMO = MBBI->memoperands().front();
733 MachineFunction *MF = MBB.getParent();
734 MachineMemOperand *MMOLo = MF->getMachineMemOperand(OldMMO, 0, TmmSize);
735 MachineMemOperand *MMOHi =
736 MF->getMachineMemOperand(OldMMO, TmmSize, TmmSize);
737
738 MIBLo.setMemRefs(MMOLo);
739 MIBHi.setMemRefs(MMOHi);
740
741 // Delete the pseudo.
742 MBB.erase(MBBI);
743 return true;
744 }
745 case X86::PT2RPNTLVWZ0V:
746 case X86::PT2RPNTLVWZ0T1V:
747 case X86::PT2RPNTLVWZ1V:
748 case X86::PT2RPNTLVWZ1T1V:
749 case X86::PT2RPNTLVWZ0RSV:
750 case X86::PT2RPNTLVWZ0RST1V:
751 case X86::PT2RPNTLVWZ1RSV:
752 case X86::PT2RPNTLVWZ1RST1V: {
753 for (unsigned i = 3; i > 0; --i)
754 MI.removeOperand(i);
755 unsigned Opc;
756 switch (Opcode) {
757 case X86::PT2RPNTLVWZ0V:
758 Opc = GET_EGPR_IF_ENABLED(X86::T2RPNTLVWZ0);
759 break;
760 case X86::PT2RPNTLVWZ0T1V:
761 Opc = GET_EGPR_IF_ENABLED(X86::T2RPNTLVWZ0T1);
762 break;
763 case X86::PT2RPNTLVWZ1V:
764 Opc = GET_EGPR_IF_ENABLED(X86::T2RPNTLVWZ1);
765 break;
766 case X86::PT2RPNTLVWZ1T1V:
767 Opc = GET_EGPR_IF_ENABLED(X86::T2RPNTLVWZ1T1);
768 break;
769 case X86::PT2RPNTLVWZ0RSV:
770 Opc = GET_EGPR_IF_ENABLED(X86::T2RPNTLVWZ0RS);
771 break;
772 case X86::PT2RPNTLVWZ0RST1V:
773 Opc = GET_EGPR_IF_ENABLED(X86::T2RPNTLVWZ0RST1);
774 break;
775 case X86::PT2RPNTLVWZ1RSV:
776 Opc = GET_EGPR_IF_ENABLED(X86::T2RPNTLVWZ1RS);
777 break;
778 case X86::PT2RPNTLVWZ1RST1V:
779 Opc = GET_EGPR_IF_ENABLED(X86::T2RPNTLVWZ1RST1);
780 break;
781 default:
782 llvm_unreachable("Impossible Opcode!");
783 }
784 MI.setDesc(TII->get(Opc));
785 return true;
786 }
787 case X86::PTTRANSPOSEDV:
788 case X86::PTCONJTFP16V: {
789 for (int i = 2; i > 0; --i)
790 MI.removeOperand(i);
791 MI.setDesc(TII->get(Opcode == X86::PTTRANSPOSEDV ? X86::TTRANSPOSED
792 : X86::TCONJTFP16));
793 return true;
794 }
795 case X86::PTCMMIMFP16PSV:
796 case X86::PTCMMRLFP16PSV:
797 case X86::PTDPBSSDV:
798 case X86::PTDPBSUDV:
799 case X86::PTDPBUSDV:
800 case X86::PTDPBUUDV:
801 case X86::PTDPBF16PSV:
802 case X86::PTDPFP16PSV:
803 case X86::PTTDPBF16PSV:
804 case X86::PTTDPFP16PSV:
805 case X86::PTTCMMIMFP16PSV:
806 case X86::PTTCMMRLFP16PSV:
807 case X86::PTCONJTCMMIMFP16PSV:
808 case X86::PTMMULTF32PSV:
809 case X86::PTTMMULTF32PSV:
810 case X86::PTDPBF8PSV:
811 case X86::PTDPBHF8PSV:
812 case X86::PTDPHBF8PSV:
813 case X86::PTDPHF8PSV: {
814 MI.untieRegOperand(4);
815 for (unsigned i = 3; i > 0; --i)
816 MI.removeOperand(i);
817 unsigned Opc;
818 switch (Opcode) {
819 case X86::PTCMMIMFP16PSV: Opc = X86::TCMMIMFP16PS; break;
820 case X86::PTCMMRLFP16PSV: Opc = X86::TCMMRLFP16PS; break;
821 case X86::PTDPBSSDV: Opc = X86::TDPBSSD; break;
822 case X86::PTDPBSUDV: Opc = X86::TDPBSUD; break;
823 case X86::PTDPBUSDV: Opc = X86::TDPBUSD; break;
824 case X86::PTDPBUUDV: Opc = X86::TDPBUUD; break;
825 case X86::PTDPBF16PSV: Opc = X86::TDPBF16PS; break;
826 case X86::PTDPFP16PSV: Opc = X86::TDPFP16PS; break;
827 case X86::PTTDPBF16PSV:
828 Opc = X86::TTDPBF16PS;
829 break;
830 case X86::PTTDPFP16PSV:
831 Opc = X86::TTDPFP16PS;
832 break;
833 case X86::PTTCMMIMFP16PSV:
834 Opc = X86::TTCMMIMFP16PS;
835 break;
836 case X86::PTTCMMRLFP16PSV:
837 Opc = X86::TTCMMRLFP16PS;
838 break;
839 case X86::PTCONJTCMMIMFP16PSV:
840 Opc = X86::TCONJTCMMIMFP16PS;
841 break;
842 case X86::PTMMULTF32PSV:
843 Opc = X86::TMMULTF32PS;
844 break;
845 case X86::PTTMMULTF32PSV:
846 Opc = X86::TTMMULTF32PS;
847 break;
848 case X86::PTDPBF8PSV:
849 Opc = X86::TDPBF8PS;
850 break;
851 case X86::PTDPBHF8PSV:
852 Opc = X86::TDPBHF8PS;
853 break;
854 case X86::PTDPHBF8PSV:
855 Opc = X86::TDPHBF8PS;
856 break;
857 case X86::PTDPHF8PSV:
858 Opc = X86::TDPHF8PS;
859 break;
860
861 default:
862 llvm_unreachable("Unexpected Opcode");
863 }
864 MI.setDesc(TII->get(Opc));
865 MI.tieOperands(0, 1);
866 return true;
867 }
868 case X86::PTILESTOREDV: {
869 for (int i = 1; i >= 0; --i)
870 MI.removeOperand(i);
871 MI.setDesc(TII->get(GET_EGPR_IF_ENABLED(X86::TILESTORED)));
872 return true;
873 }
874#undef GET_EGPR_IF_ENABLED
875 case X86::PTILEZEROV: {
876 for (int i = 2; i > 0; --i) // Remove row, col
877 MI.removeOperand(i);
878 MI.setDesc(TII->get(X86::TILEZERO));
879 return true;
880 }
881 case X86::CALL64pcrel32_RVMARKER:
882 case X86::CALL64r_RVMARKER:
883 case X86::CALL64m_RVMARKER:
884 expandCALL_RVMARKER(MBB, MBBI);
885 return true;
886 case X86::CALL64r_ImpCall:
887 MI.setDesc(TII->get(X86::CALL64r));
888 return true;
889 case X86::ADD32mi_ND:
890 case X86::ADD64mi32_ND:
891 case X86::SUB32mi_ND:
892 case X86::SUB64mi32_ND:
893 case X86::AND32mi_ND:
894 case X86::AND64mi32_ND:
895 case X86::OR32mi_ND:
896 case X86::OR64mi32_ND:
897 case X86::XOR32mi_ND:
898 case X86::XOR64mi32_ND:
899 case X86::ADC32mi_ND:
900 case X86::ADC64mi32_ND:
901 case X86::SBB32mi_ND:
902 case X86::SBB64mi32_ND: {
903 // It's possible for an EVEX-encoded legacy instruction to reach the 15-byte
904 // instruction length limit: 4 bytes of EVEX prefix + 1 byte of opcode + 1
905 // byte of ModRM + 1 byte of SIB + 4 bytes of displacement + 4 bytes of
906 // immediate = 15 bytes in total, e.g.
907 //
908 // subq $184, %fs:257(%rbx, %rcx), %rax
909 //
910 // In such a case, no additional (ADSIZE or segment override) prefix can be
911 // used. To resolve the issue, we split the “long” instruction into 2
912 // instructions:
913 //
914 // movq %fs:257(%rbx, %rcx),%rax
915 // subq $184, %rax
916 //
917 // Therefore we consider the OPmi_ND to be a pseudo instruction to some
918 // extent.
919 const MachineOperand &ImmOp =
920 MI.getOperand(MI.getNumExplicitOperands() - 1);
921 // If the immediate is a expr, conservatively estimate 4 bytes.
922 if (ImmOp.isImm() && isInt<8>(ImmOp.getImm()))
923 return false;
924 int MemOpNo = X86::getFirstAddrOperandIdx(MI);
925 const MachineOperand &DispOp = MI.getOperand(MemOpNo + X86::AddrDisp);
926 Register Base = MI.getOperand(MemOpNo + X86::AddrBaseReg).getReg();
927 // If the displacement is a expr, conservatively estimate 4 bytes.
928 if (Base && DispOp.isImm() && isInt<8>(DispOp.getImm()))
929 return false;
930 // There can only be one of three: SIB, segment override register, ADSIZE
931 Register Index = MI.getOperand(MemOpNo + X86::AddrIndexReg).getReg();
932 unsigned Count = !!MI.getOperand(MemOpNo + X86::AddrSegmentReg).getReg();
933 if (X86II::needSIB(Base, Index, /*In64BitMode=*/true))
934 ++Count;
935 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(Base) ||
936 X86MCRegisterClasses[X86::GR32RegClassID].contains(Index))
937 ++Count;
938 if (Count < 2)
939 return false;
940 unsigned Opc, LoadOpc;
941 switch (Opcode) {
942#define MI_TO_RI(OP) \
943 case X86::OP##32mi_ND: \
944 Opc = X86::OP##32ri; \
945 LoadOpc = X86::MOV32rm; \
946 break; \
947 case X86::OP##64mi32_ND: \
948 Opc = X86::OP##64ri32; \
949 LoadOpc = X86::MOV64rm; \
950 break;
951
952 default:
953 llvm_unreachable("Unexpected Opcode");
954 MI_TO_RI(ADD);
955 MI_TO_RI(SUB);
956 MI_TO_RI(AND);
957 MI_TO_RI(OR);
958 MI_TO_RI(XOR);
959 MI_TO_RI(ADC);
960 MI_TO_RI(SBB);
961#undef MI_TO_RI
962 }
963 // Insert OPri.
964 Register DestReg = MI.getOperand(0).getReg();
965 BuildMI(MBB, std::next(MBBI), DL, TII->get(Opc), DestReg)
966 .addReg(DestReg)
967 .add(ImmOp);
968 // Change OPmi_ND to MOVrm.
969 for (unsigned I = MI.getNumImplicitOperands() + 1; I != 0; --I)
970 MI.removeOperand(MI.getNumOperands() - 1);
971 MI.setDesc(TII->get(LoadOpc));
972 return true;
973 }
974 }
975 llvm_unreachable("Previous switch has a fallthrough?");
976}
977
978// This function creates additional block for storing varargs guarded
979// registers. It adds check for %al into entry block, to skip
980// GuardedRegsBlk if xmm registers should not be stored.
981//
982// EntryBlk[VAStartPseudoInstr] EntryBlk
983// | | .
984// | | .
985// | | GuardedRegsBlk
986// | => | .
987// | | .
988// | TailBlk
989// | |
990// | |
991//
992void X86ExpandPseudo::expandVastartSaveXmmRegs(
993 MachineBasicBlock *EntryBlk,
994 MachineBasicBlock::iterator VAStartPseudoInstr) const {
995 assert(VAStartPseudoInstr->getOpcode() == X86::VASTART_SAVE_XMM_REGS);
996
997 MachineFunction *Func = EntryBlk->getParent();
998 const TargetInstrInfo *TII = STI->getInstrInfo();
999 const DebugLoc &DL = VAStartPseudoInstr->getDebugLoc();
1000 Register CountReg = VAStartPseudoInstr->getOperand(0).getReg();
1001
1002 // Calculate liveins for newly created blocks.
1003 LivePhysRegs LiveRegs(*STI->getRegisterInfo());
1005
1006 LiveRegs.addLiveIns(*EntryBlk);
1007 for (MachineInstr &MI : EntryBlk->instrs()) {
1008 if (MI.getOpcode() == VAStartPseudoInstr->getOpcode())
1009 break;
1010
1011 LiveRegs.stepForward(MI, Clobbers);
1012 }
1013
1014 // Create the new basic blocks. One block contains all the XMM stores,
1015 // and another block is the final destination regardless of whether any
1016 // stores were performed.
1017 const BasicBlock *LLVMBlk = EntryBlk->getBasicBlock();
1018 MachineFunction::iterator EntryBlkIter = ++EntryBlk->getIterator();
1019 MachineBasicBlock *GuardedRegsBlk = Func->CreateMachineBasicBlock(LLVMBlk);
1020 MachineBasicBlock *TailBlk = Func->CreateMachineBasicBlock(LLVMBlk);
1021 Func->insert(EntryBlkIter, GuardedRegsBlk);
1022 Func->insert(EntryBlkIter, TailBlk);
1023
1024 // Transfer the remainder of EntryBlk and its successor edges to TailBlk.
1025 TailBlk->splice(TailBlk->begin(), EntryBlk,
1026 std::next(MachineBasicBlock::iterator(VAStartPseudoInstr)),
1027 EntryBlk->end());
1028 TailBlk->transferSuccessorsAndUpdatePHIs(EntryBlk);
1029
1030 uint64_t FrameOffset = VAStartPseudoInstr->getOperand(4).getImm();
1031 uint64_t VarArgsRegsOffset = VAStartPseudoInstr->getOperand(6).getImm();
1032
1033 // TODO: add support for YMM and ZMM here.
1034 unsigned MOVOpc = STI->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
1035
1036 // In the XMM save block, save all the XMM argument registers.
1037 for (int64_t OpndIdx = 7, RegIdx = 0;
1038 OpndIdx < VAStartPseudoInstr->getNumOperands() - 1;
1039 OpndIdx++, RegIdx++) {
1040 auto NewMI = BuildMI(GuardedRegsBlk, DL, TII->get(MOVOpc));
1041 for (int i = 0; i < X86::AddrNumOperands; ++i) {
1042 if (i == X86::AddrDisp)
1043 NewMI.addImm(FrameOffset + VarArgsRegsOffset + RegIdx * 16);
1044 else
1045 NewMI.add(VAStartPseudoInstr->getOperand(i + 1));
1046 }
1047 NewMI.addReg(VAStartPseudoInstr->getOperand(OpndIdx).getReg());
1048 assert(VAStartPseudoInstr->getOperand(OpndIdx).getReg().isPhysical());
1049 }
1050
1051 // The original block will now fall through to the GuardedRegsBlk.
1052 EntryBlk->addSuccessor(GuardedRegsBlk);
1053 // The GuardedRegsBlk will fall through to the TailBlk.
1054 GuardedRegsBlk->addSuccessor(TailBlk);
1055
1056 if (!STI->isCallingConvWin64(Func->getFunction().getCallingConv())) {
1057 // If %al is 0, branch around the XMM save block.
1058 BuildMI(EntryBlk, DL, TII->get(X86::TEST8rr))
1059 .addReg(CountReg)
1060 .addReg(CountReg);
1061 BuildMI(EntryBlk, DL, TII->get(X86::JCC_1))
1062 .addMBB(TailBlk)
1064 EntryBlk->addSuccessor(TailBlk);
1065 }
1066
1067 // Add liveins to the created block.
1068 addLiveIns(*GuardedRegsBlk, LiveRegs);
1069 addLiveIns(*TailBlk, LiveRegs);
1070
1071 // Delete the pseudo.
1072 VAStartPseudoInstr->eraseFromParent();
1073}
1074
1075/// Expand all pseudo instructions contained in \p MBB.
1076/// \returns true if any expansion occurred for \p MBB.
1077bool X86ExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
1078 bool Modified = false;
1079
1080 // MBBI may be invalidated by the expansion.
1082 while (MBBI != E) {
1083 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
1084 Modified |= expandMI(MBB, MBBI);
1085 MBBI = NMBBI;
1086 }
1087
1088 return Modified;
1089}
1090
1091bool X86ExpandPseudo::expandPseudosWhichAffectControlFlow(MachineFunction &MF) {
1092 // Currently pseudo which affects control flow is only
1093 // X86::VASTART_SAVE_XMM_REGS which is located in Entry block.
1094 // So we do not need to evaluate other blocks.
1095 for (MachineInstr &Instr : MF.front().instrs()) {
1096 if (Instr.getOpcode() == X86::VASTART_SAVE_XMM_REGS) {
1097 expandVastartSaveXmmRegs(&(MF.front()), Instr);
1098 return true;
1099 }
1100 }
1101
1102 return false;
1103}
1104
1105bool X86ExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
1106 STI = &MF.getSubtarget<X86Subtarget>();
1107 TII = STI->getInstrInfo();
1108 TRI = STI->getRegisterInfo();
1109 X86FI = MF.getInfo<X86MachineFunctionInfo>();
1110 X86FL = STI->getFrameLowering();
1111
1112 bool Modified = expandPseudosWhichAffectControlFlow(MF);
1113
1114 for (MachineBasicBlock &MBB : MF)
1115 Modified |= expandMBB(MBB);
1116 return Modified;
1117}
1118
1119/// Returns an instance of the pseudo instruction expansion pass.
1121 return new X86ExpandPseudo();
1122}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define DEBUG_TYPE
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 Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#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 GET_EGPR_IF_ENABLED(OPC)
#define MI_TO_RI(OP)
#define X86_EXPAND_PSEUDO_NAME
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
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.
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.
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 '...
MachineInstrBundleIterator< MachineInstr > iterator
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.
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)
CreateMachineInstr - Allocate a new MachineInstr.
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.
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.
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.
const MachineOperand & getOperand(unsigned i) const
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.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
CodeModel::Model getCodeModel() const
Returns the code model.
Target - Wrapper for Target specific information.
bool isOSWindows() const
Tests whether the OS is Windows.
Definition Triple.h:679
int64_t mergeSPAdd(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, int64_t AddOffset, bool doMergeWithPrevious) const
Equivalent to: mergeSPUpdates(MBB, MBBI, [AddOffset](int64_t Offset) { return AddOffset + Offset; }...
void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, const DebugLoc &DL, int64_t NumBytes, bool InEpilogue) const
Emit a series of instructions to increment / decrement the stack pointer by a constant value.
X86MachineFunctionInfo - This class is derived from MachineFunction and contains private X86 target-s...
bool isTargetWin64() const
bool isTarget64BitLP64() const
Is this x86_64 with the LP64 programming model (standard AMD64, no x32)?
const Triple & getTargetTriple() const
const X86InstrInfo * getInstrInfo() const override
bool isCallingConvWin64(CallingConv::ID CC) const
bool isTargetUEFI64() const
const X86RegisterInfo * getRegisterInfo() const override
bool hasAVX() const
const X86FrameLowering * getFrameLowering() const override
self_iterator getIterator()
Definition ilist_node.h:130
#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
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Dead
Unused definition.
@ Define
Register definition.
@ X86
Windows x64, Windows Itanium (IA-64)
Definition MCAsmInfo.h:50
bool needSIB(MCRegister BaseReg, MCRegister IndexReg, bool In64BitMode)
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
NodeAddr< InstrNode * > Instr
Definition RDFGraph.h:389
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
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:311
@ 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.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:174
static bool isMem(const MachineInstr &MI, unsigned Op)
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...
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:198
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
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.