LLVM 24.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"
26#include "llvm/CodeGen/Passes.h" // For IDs of passes that are preserved.
27#include "llvm/IR/Analysis.h"
29#include "llvm/IR/GlobalValue.h"
31using namespace llvm;
32
33#define DEBUG_TYPE "x86-expand-pseudo"
34#define X86_EXPAND_PSEUDO_NAME "X86 pseudo instruction expansion pass"
35
36namespace {
37class X86ExpandPseudoImpl {
38public:
39 const X86Subtarget *STI = nullptr;
40 const X86InstrInfo *TII = nullptr;
41 const X86RegisterInfo *TRI = nullptr;
42 const X86MachineFunctionInfo *X86FI = nullptr;
43 const X86FrameLowering *X86FL = nullptr;
44 bool CFGChanged = false;
45
46 bool runOnMachineFunction(MachineFunction &MF);
47
48private:
49 void expandICallBranchFunnel(MachineBasicBlock *MBB,
51 void expandCALL_RVMARKER(MachineBasicBlock &MBB,
55
56 /// This function expands pseudos which affects control flow.
57 /// It is done in separate pass to simplify blocks navigation in main
58 /// pass(calling expandMBB).
59 bool expandPseudosWhichAffectControlFlow(MachineFunction &MF);
60
61 /// Expand X86::VASTART_SAVE_XMM_REGS into set of xmm copying instructions,
62 /// placed into separate block guarded by check for al register(for SystemV
63 /// abi).
64 void expandVastartSaveXmmRegs(
65 MachineBasicBlock *EntryBlk,
66 MachineBasicBlock::iterator VAStartPseudoInstr) const;
67};
68
69class X86ExpandPseudoLegacy : public MachineFunctionPass {
70public:
71 static char ID;
72 X86ExpandPseudoLegacy() : MachineFunctionPass(ID) {}
73
74 void getAnalysisUsage(AnalysisUsage &AU) const override {
75 AU.setPreservesCFG();
77 }
78
79 const X86Subtarget *STI = nullptr;
80 const X86InstrInfo *TII = nullptr;
81 const X86RegisterInfo *TRI = nullptr;
82 const X86MachineFunctionInfo *X86FI = nullptr;
83 const X86FrameLowering *X86FL = nullptr;
84
85 bool runOnMachineFunction(MachineFunction &MF) override;
86
87 MachineFunctionProperties getRequiredProperties() const override {
88 return MachineFunctionProperties().setNoVRegs();
89 }
90
91 StringRef getPassName() const override {
92 return "X86 pseudo instruction expansion pass";
93 }
94};
95char X86ExpandPseudoLegacy::ID = 0;
96} // End anonymous namespace.
97
99 false, false)
100
101void X86ExpandPseudoImpl::expandICallBranchFunnel(
103 CFGChanged = true;
104 MachineBasicBlock *JTMBB = MBB;
105 MachineInstr *JTInst = &*MBBI;
106 MachineFunction *MF = MBB->getParent();
107 const BasicBlock *BB = MBB->getBasicBlock();
108 auto InsPt = MachineFunction::iterator(MBB);
109 ++InsPt;
110
111 std::vector<std::pair<MachineBasicBlock *, unsigned>> TargetMBBs;
112 const DebugLoc &DL = JTInst->getDebugLoc();
113 MachineOperand Selector = JTInst->getOperand(0);
114 const GlobalValue *CombinedGlobal = JTInst->getOperand(1).getGlobal();
115
116 auto CmpTarget = [&](unsigned Target) {
117 if (Selector.isReg())
118 MBB->addLiveIn(Selector.getReg());
119 BuildMI(*MBB, MBBI, DL, TII->get(X86::LEA64r), X86::R11)
120 .addReg(X86::RIP)
121 .addImm(1)
122 .addReg(0)
123 .addGlobalAddress(CombinedGlobal,
124 JTInst->getOperand(2 + 2 * Target).getImm())
125 .addReg(0);
126 BuildMI(*MBB, MBBI, DL, TII->get(X86::CMP64rr))
127 .add(Selector)
128 .addReg(X86::R11);
129 };
130
131 auto CreateMBB = [&]() {
132 auto *NewMBB = MF->CreateMachineBasicBlock(BB);
133 MBB->addSuccessor(NewMBB);
134 if (!MBB->isLiveIn(X86::EFLAGS))
135 MBB->addLiveIn(X86::EFLAGS);
136 return NewMBB;
137 };
138
139 auto EmitCondJump = [&](unsigned CC, MachineBasicBlock *ThenMBB) {
140 BuildMI(*MBB, MBBI, DL, TII->get(X86::JCC_1)).addMBB(ThenMBB).addImm(CC);
141
142 auto *ElseMBB = CreateMBB();
143 MF->insert(InsPt, ElseMBB);
144 MBB = ElseMBB;
145 MBBI = MBB->end();
146 };
147
148 auto EmitCondJumpTarget = [&](unsigned CC, unsigned Target) {
149 auto *ThenMBB = CreateMBB();
150 TargetMBBs.push_back({ThenMBB, Target});
151 EmitCondJump(CC, ThenMBB);
152 };
153
154 auto EmitTailCall = [&](unsigned Target) {
155 BuildMI(*MBB, MBBI, DL, TII->get(X86::TAILJMPd64))
156 .add(JTInst->getOperand(3 + 2 * Target));
157 };
158
159 std::function<void(unsigned, unsigned)> EmitBranchFunnel =
160 [&](unsigned FirstTarget, unsigned NumTargets) {
161 if (NumTargets == 1) {
162 EmitTailCall(FirstTarget);
163 return;
164 }
165
166 if (NumTargets == 2) {
167 CmpTarget(FirstTarget + 1);
168 EmitCondJumpTarget(X86::COND_B, FirstTarget);
169 EmitTailCall(FirstTarget + 1);
170 return;
171 }
172
173 if (NumTargets < 6) {
174 CmpTarget(FirstTarget + 1);
175 EmitCondJumpTarget(X86::COND_B, FirstTarget);
176 EmitCondJumpTarget(X86::COND_E, FirstTarget + 1);
177 EmitBranchFunnel(FirstTarget + 2, NumTargets - 2);
178 return;
179 }
180
181 auto *ThenMBB = CreateMBB();
182 CmpTarget(FirstTarget + (NumTargets / 2));
183 EmitCondJump(X86::COND_B, ThenMBB);
184 EmitCondJumpTarget(X86::COND_E, FirstTarget + (NumTargets / 2));
185 EmitBranchFunnel(FirstTarget + (NumTargets / 2) + 1,
186 NumTargets - (NumTargets / 2) - 1);
187
188 MF->insert(InsPt, ThenMBB);
189 MBB = ThenMBB;
190 MBBI = MBB->end();
191 EmitBranchFunnel(FirstTarget, NumTargets / 2);
192 };
193
194 EmitBranchFunnel(0, (JTInst->getNumOperands() - 2) / 2);
195 for (auto P : TargetMBBs) {
196 MF->insert(InsPt, P.first);
197 BuildMI(P.first, DL, TII->get(X86::TAILJMPd64))
198 .add(JTInst->getOperand(3 + 2 * P.second));
199 }
200 JTMBB->erase(JTInst);
201}
202
203void X86ExpandPseudoImpl::expandCALL_RVMARKER(
205 // Expand CALL_RVMARKER pseudo to call instruction, followed by the special
206 //"movq %rax, %rdi" marker.
207 MachineInstr &MI = *MBBI;
208
209 MachineInstr *OriginalCall;
210 assert((MI.getOperand(1).isGlobal() || MI.getOperand(1).isReg()) &&
211 "invalid operand for regular call");
212 unsigned Opc = -1;
213 if (MI.getOpcode() == X86::CALL64m_RVMARKER)
214 Opc = X86::CALL64m;
215 else if (MI.getOpcode() == X86::CALL64r_RVMARKER)
216 Opc = X86::CALL64r;
217 else if (MI.getOpcode() == X86::CALL64pcrel32_RVMARKER)
218 Opc = X86::CALL64pcrel32;
219 else
220 llvm_unreachable("unexpected opcode");
221
222 OriginalCall = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opc)).getInstr();
223 bool RAXImplicitDead = false;
224 for (MachineOperand &Op : llvm::drop_begin(MI.operands())) {
225 // RAX may be 'implicit dead', if there are no other users of the return
226 // value. We introduce a new use, so change it to 'implicit def'.
227 if (Op.isReg() && Op.isImplicit() && Op.isDead() &&
228 TRI->regsOverlap(Op.getReg(), X86::RAX)) {
229 Op.setIsDead(false);
230 Op.setIsDef(true);
231 RAXImplicitDead = true;
232 }
233 OriginalCall->addOperand(Op);
234 }
235
236 // Emit marker "movq %rax, %rdi". %rdi is not callee-saved, so it cannot be
237 // live across the earlier call. The call to the ObjC runtime function returns
238 // the first argument, so the value of %rax is unchanged after the ObjC
239 // runtime call. On Windows targets, the runtime call follows the regular
240 // x64 calling convention and expects the first argument in %rcx.
241 auto TargetReg = STI->getTargetTriple().isOSWindows() ? X86::RCX : X86::RDI;
242 auto *Marker = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(X86::MOV64rr))
243 .addReg(TargetReg, RegState::Define)
244 .addReg(X86::RAX)
245 .getInstr();
246 if (MI.shouldUpdateAdditionalCallInfo())
248
249 // Emit call to ObjC runtime.
250 const uint32_t *RegMask =
251 TRI->getCallPreservedMask(*MBB.getParent(), CallingConv::C);
252 MachineInstr *RtCall =
253 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(X86::CALL64pcrel32))
254 .addGlobalAddress(MI.getOperand(0).getGlobal(), 0, 0)
255 .addRegMask(RegMask)
256 .addReg(X86::RAX,
257 RegState::Implicit |
258 (RAXImplicitDead ? (RegState::Dead | RegState::Define)
259 : RegState::Define))
260 .getInstr();
262
263 auto &TM = MBB.getParent()->getTarget();
264 // On Darwin platforms, wrap the expanded sequence in a bundle to prevent
265 // later optimizations from breaking up the sequence.
266 if (TM.getTargetTriple().isOSDarwin())
267 finalizeBundle(MBB, OriginalCall->getIterator(),
268 std::next(RtCall->getIterator()));
269}
270
271/// If \p MBBI is a pseudo instruction, this method expands
272/// it to the corresponding (sequence of) actual instruction(s).
273/// \returns true if \p MBBI has been expanded.
274bool X86ExpandPseudoImpl::expandMI(MachineBasicBlock &MBB,
276 MachineInstr &MI = *MBBI;
277 unsigned Opcode = MI.getOpcode();
278 const DebugLoc &DL = MBBI->getDebugLoc();
279#define GET_EGPR_IF_ENABLED(OPC) (STI->hasEGPR() ? OPC##_EVEX : OPC)
280 switch (Opcode) {
281 default:
282 return false;
283 case X86::TCRETURNdi:
284 case X86::TCRETURNdicc:
285 case X86::TCRETURNri:
286 case X86::TCRETURN_WIN64ri:
287 case X86::TCRETURN_HIPE32ri:
288 case X86::TCRETURNmi:
289 case X86::TCRETURNdi64:
290 case X86::TCRETURNdi64cc:
291 case X86::TCRETURNri64:
292 case X86::TCRETURNri64_ImpCall:
293 case X86::TCRETURNmi64:
294 case X86::TCRETURN_WINmi64: {
295 bool isMem = Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64 ||
296 Opcode == X86::TCRETURN_WINmi64;
297 MachineOperand &JumpTarget = MBBI->getOperand(0);
298 MachineOperand &StackAdjust = MBBI->getOperand(isMem ? X86::AddrNumOperands
299 : 1);
300 assert(StackAdjust.isImm() && "Expecting immediate value.");
301
302 // Adjust stack pointer.
303 int StackAdj = StackAdjust.getImm();
304 int MaxTCDelta = X86FI->getTCReturnAddrDelta();
305 int64_t Offset = 0;
306 assert(MaxTCDelta <= 0 && "MaxTCDelta should never be positive");
307
308 // Incoporate the retaddr area.
309 Offset = StackAdj - MaxTCDelta;
310 assert(Offset >= 0 && "Offset should never be negative");
311
312 if (Opcode == X86::TCRETURNdicc || Opcode == X86::TCRETURNdi64cc) {
313 assert(Offset == 0 && "Conditional tail call cannot adjust the stack.");
314 }
315
316 if (Offset) {
317 // Check for possible merge with preceding ADD instruction.
318 Offset = X86FL->mergeSPAdd(MBB, MBBI, Offset, true);
319 X86FL->emitSPUpdate(MBB, MBBI, DL, Offset, /*InEpilogue=*/true);
320 }
321
322 // Use this predicate to set REX prefix for X86_64 targets.
323 bool IsX64 = STI->isTargetWin64() || STI->isTargetUEFI64();
324 // Jump to label or value in register.
325 if (Opcode == X86::TCRETURNdi || Opcode == X86::TCRETURNdicc ||
326 Opcode == X86::TCRETURNdi64 || Opcode == X86::TCRETURNdi64cc) {
327 unsigned Op;
328 switch (Opcode) {
329 case X86::TCRETURNdi:
330 Op = X86::TAILJMPd;
331 break;
332 case X86::TCRETURNdicc:
333 Op = X86::TAILJMPd_CC;
334 break;
335 case X86::TCRETURNdi64cc:
337 "Conditional tail calls confuse "
338 "the Win64 unwinder.");
339 Op = X86::TAILJMPd64_CC;
340 break;
341 default:
342 // Note: Win64 uses REX prefixes indirect jumps out of functions, but
343 // not direct ones.
344 Op = X86::TAILJMPd64;
345 break;
346 }
347 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
348 if (JumpTarget.isGlobal()) {
349 MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
350 JumpTarget.getTargetFlags());
351 } else {
352 assert(JumpTarget.isSymbol());
353 MIB.addExternalSymbol(JumpTarget.getSymbolName(),
354 JumpTarget.getTargetFlags());
355 }
356 if (Op == X86::TAILJMPd_CC || Op == X86::TAILJMPd64_CC) {
357 MIB.addImm(MBBI->getOperand(2).getImm());
358 }
359
360 } else if (Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64 ||
361 Opcode == X86::TCRETURN_WINmi64) {
362 unsigned Op = (Opcode == X86::TCRETURNmi)
363 ? X86::TAILJMPm
364 : (IsX64 ? X86::TAILJMPm64_REX : X86::TAILJMPm64);
365 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
366 for (unsigned i = 0; i != X86::AddrNumOperands; ++i)
367 MIB.add(MBBI->getOperand(i));
368 } else if (Opcode == X86::TCRETURNri64 ||
369 Opcode == X86::TCRETURNri64_ImpCall ||
370 Opcode == X86::TCRETURN_WIN64ri) {
371 JumpTarget.setIsKill();
372 BuildMI(MBB, MBBI, DL,
373 TII->get(IsX64 ? X86::TAILJMPr64_REX : X86::TAILJMPr64))
374 .add(JumpTarget);
375 } else {
376 assert(!IsX64 && "Win64 and UEFI64 require REX for indirect jumps.");
377 JumpTarget.setIsKill();
378 BuildMI(MBB, MBBI, DL, TII->get(X86::TAILJMPr))
379 .add(JumpTarget);
380 }
381
382 MachineInstr &NewMI = *std::prev(MBBI);
383 NewMI.copyImplicitOps(*MBBI->getParent()->getParent(), *MBBI);
384 NewMI.setCFIType(*MBB.getParent(), MI.getCFIType());
385
386 // Update the call info.
387 if (MBBI->isCandidateForAdditionalCallInfo())
389
390 // Delete the pseudo instruction TCRETURN.
391 MBB.erase(MBBI);
392
393 return true;
394 }
395 case X86::EH_RETURN:
396 case X86::EH_RETURN64: {
397 MachineOperand &DestAddr = MBBI->getOperand(0);
398 assert(DestAddr.isReg() && "Offset should be in register!");
399 const bool Uses64BitFramePtr = STI->isTarget64BitLP64();
400 Register StackPtr = TRI->getStackRegister();
401 BuildMI(MBB, MBBI, DL,
402 TII->get(Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr), StackPtr)
403 .addReg(DestAddr.getReg());
404 if (STI->hasSHSTK()) {
405 unsigned PopOpcode = STI->is64Bit() ? X86::POP64r : X86::POP32r;
406 unsigned JumpOpcode = X86::JMP32r;
407 if (Uses64BitFramePtr)
408 JumpOpcode = STI->isTargetWin64() || STI->isTargetUEFI64()
409 ? X86::JMP64r_REX
410 : X86::JMP64r;
411 BuildMI(MBB, MBBI, DL, TII->get(PopOpcode))
412 .addReg(DestAddr.getReg(), RegState::Define);
413 BuildMI(MBB, MBBI, DL, TII->get(JumpOpcode)).addReg(DestAddr.getReg());
414 MBB.erase(MBBI);
415 }
416 // The EH_RETURN pseudo is really removed during the MC Lowering.
417 return true;
418 }
419 case X86::IRET: {
420 // Adjust stack to erase error code
421 int64_t StackAdj = MBBI->getOperand(0).getImm();
422 X86FL->emitSPUpdate(MBB, MBBI, DL, StackAdj, true);
423 // Replace pseudo with machine iret
424 unsigned RetOp = STI->is64Bit() ? X86::IRET64 : X86::IRET32;
425 // Use UIRET if UINTR is present (except for building kernel)
426 if (STI->is64Bit() && STI->hasUINTR() &&
428 RetOp = X86::UIRET;
429 BuildMI(MBB, MBBI, DL, TII->get(RetOp));
430 MBB.erase(MBBI);
431 return true;
432 }
433 case X86::RET: {
434 // Adjust stack to erase error code
435 int64_t StackAdj = MBBI->getOperand(0).getImm();
436 MachineInstrBuilder MIB;
437 if (StackAdj == 0) {
438 MIB = BuildMI(MBB, MBBI, DL,
439 TII->get(STI->is64Bit() ? X86::RET64 : X86::RET32));
440 } else if (isUInt<16>(StackAdj)) {
441 MIB = BuildMI(MBB, MBBI, DL,
442 TII->get(STI->is64Bit() ? X86::RETI64 : X86::RETI32))
443 .addImm(StackAdj);
444 } else {
445 assert(!STI->is64Bit() &&
446 "shouldn't need to do this for x86_64 targets!");
447 // A ret can only handle immediates as big as 2**16-1. If we need to pop
448 // off bytes before the return address, we must do it manually.
449 BuildMI(MBB, MBBI, DL, TII->get(X86::POP32r)).addReg(X86::ECX, RegState::Define);
450 X86FL->emitSPUpdate(MBB, MBBI, DL, StackAdj, /*InEpilogue=*/true);
451 BuildMI(MBB, MBBI, DL, TII->get(X86::PUSH32r)).addReg(X86::ECX);
452 MIB = BuildMI(MBB, MBBI, DL, TII->get(X86::RET32));
453 }
454 for (unsigned I = 1, E = MBBI->getNumOperands(); I != E; ++I)
455 MIB.add(MBBI->getOperand(I));
456 MBB.erase(MBBI);
457 return true;
458 }
459 case X86::LCMPXCHG16B_SAVE_RBX: {
460 // Perform the following transformation.
461 // SaveRbx = pseudocmpxchg Addr, <4 opds for the address>, InArg, SaveRbx
462 // =>
463 // RBX = InArg
464 // actualcmpxchg Addr
465 // RBX = SaveRbx
466 const MachineOperand &InArg = MBBI->getOperand(6);
467 Register SaveRbx = MBBI->getOperand(7).getReg();
468
469 // Copy the input argument of the pseudo into the argument of the
470 // actual instruction.
471 // NOTE: We don't copy the kill flag since the input might be the same reg
472 // as one of the other operands of LCMPXCHG16B.
473 TII->copyPhysReg(MBB, MBBI, DL, X86::RBX, InArg.getReg(), false);
474 // Create the actual instruction.
475 MachineInstr *NewInstr = BuildMI(MBB, MBBI, DL, TII->get(X86::LCMPXCHG16B));
476 // Copy the operands related to the address. If we access a frame variable,
477 // we need to replace the RBX base with SaveRbx, as RBX has another value.
478 const MachineOperand &Base = MBBI->getOperand(1);
479 if (Base.getReg() == X86::RBX || Base.getReg() == X86::EBX)
481 Base.getReg() == X86::RBX
482 ? SaveRbx
483 : Register(TRI->getSubReg(SaveRbx, X86::sub_32bit)),
484 /*IsDef=*/false));
485 else
486 NewInstr->addOperand(Base);
487 for (unsigned Idx = 1 + 1; Idx < 1 + X86::AddrNumOperands; ++Idx)
488 NewInstr->addOperand(MBBI->getOperand(Idx));
489 NewInstr->getOperand(7).setIsDead(MBBI->getOperand(11).isDead());
490 // Finally, restore the value of RBX.
491 TII->copyPhysReg(MBB, MBBI, DL, X86::RBX, SaveRbx,
492 /*SrcIsKill*/ true);
493
494 // Delete the pseudo.
496 return true;
497 }
498 // Loading/storing mask pairs requires two kmov operations. The second one of
499 // these needs a 2 byte displacement relative to the specified address (with
500 // 32 bit spill size). The pairs of 1bit masks up to 16 bit masks all use the
501 // same spill size, they all are stored using MASKPAIR16STORE, loaded using
502 // MASKPAIR16LOAD.
503 //
504 // The displacement value might wrap around in theory, thus the asserts in
505 // both cases.
506 case X86::MASKPAIR16LOAD: {
507 int64_t Disp = MBBI->getOperand(1 + X86::AddrDisp).getImm();
508 assert(Disp >= 0 && Disp <= INT32_MAX - 2 && "Unexpected displacement");
509 Register Reg = MBBI->getOperand(0).getReg();
510 bool DstIsDead = MBBI->getOperand(0).isDead();
511 Register Reg0 = TRI->getSubReg(Reg, X86::sub_mask_0);
512 Register Reg1 = TRI->getSubReg(Reg, X86::sub_mask_1);
513
514 auto MIBLo =
515 BuildMI(MBB, MBBI, DL, TII->get(GET_EGPR_IF_ENABLED(X86::KMOVWkm)))
516 .addReg(Reg0, RegState::Define | getDeadRegState(DstIsDead));
517 auto MIBHi =
518 BuildMI(MBB, MBBI, DL, TII->get(GET_EGPR_IF_ENABLED(X86::KMOVWkm)))
519 .addReg(Reg1, RegState::Define | getDeadRegState(DstIsDead));
520
521 for (int i = 0; i < X86::AddrNumOperands; ++i) {
522 MIBLo.add(MBBI->getOperand(1 + i));
523 if (i == X86::AddrDisp)
524 MIBHi.addImm(Disp + 2);
525 else
526 MIBHi.add(MBBI->getOperand(1 + i));
527 }
528
529 // Split the memory operand, adjusting the offset and size for the halves.
530 MachineMemOperand *OldMMO = MBBI->memoperands().front();
532 MachineMemOperand *MMOLo = MF->getMachineMemOperand(OldMMO, 0, 2);
533 MachineMemOperand *MMOHi = MF->getMachineMemOperand(OldMMO, 2, 2);
534
535 MIBLo.setMemRefs(MMOLo);
536 MIBHi.setMemRefs(MMOHi);
537
538 // Delete the pseudo.
539 MBB.erase(MBBI);
540 return true;
541 }
542 case X86::MASKPAIR16STORE: {
543 int64_t Disp = MBBI->getOperand(X86::AddrDisp).getImm();
544 assert(Disp >= 0 && Disp <= INT32_MAX - 2 && "Unexpected displacement");
545 Register Reg = MBBI->getOperand(X86::AddrNumOperands).getReg();
546 bool SrcIsKill = MBBI->getOperand(X86::AddrNumOperands).isKill();
547 Register Reg0 = TRI->getSubReg(Reg, X86::sub_mask_0);
548 Register Reg1 = TRI->getSubReg(Reg, X86::sub_mask_1);
549
550 auto MIBLo =
551 BuildMI(MBB, MBBI, DL, TII->get(GET_EGPR_IF_ENABLED(X86::KMOVWmk)));
552 auto MIBHi =
553 BuildMI(MBB, MBBI, DL, TII->get(GET_EGPR_IF_ENABLED(X86::KMOVWmk)));
554
555 for (int i = 0; i < X86::AddrNumOperands; ++i) {
556 MIBLo.add(MBBI->getOperand(i));
557 if (i == X86::AddrDisp)
558 MIBHi.addImm(Disp + 2);
559 else
560 MIBHi.add(MBBI->getOperand(i));
561 }
562 MIBLo.addReg(Reg0, getKillRegState(SrcIsKill));
563 MIBHi.addReg(Reg1, getKillRegState(SrcIsKill));
564
565 // Split the memory operand, adjusting the offset and size for the halves.
566 MachineMemOperand *OldMMO = MBBI->memoperands().front();
568 MachineMemOperand *MMOLo = MF->getMachineMemOperand(OldMMO, 0, 2);
569 MachineMemOperand *MMOHi = MF->getMachineMemOperand(OldMMO, 2, 2);
570
571 MIBLo.setMemRefs(MMOLo);
572 MIBHi.setMemRefs(MMOHi);
573
574 // Delete the pseudo.
575 MBB.erase(MBBI);
576 return true;
577 }
578 case X86::MWAITX_SAVE_RBX: {
579 // Perform the following transformation.
580 // SaveRbx = pseudomwaitx InArg, SaveRbx
581 // =>
582 // [E|R]BX = InArg
583 // actualmwaitx
584 // [E|R]BX = SaveRbx
585 const MachineOperand &InArg = MBBI->getOperand(1);
586 // Copy the input argument of the pseudo into the argument of the
587 // actual instruction.
588 TII->copyPhysReg(MBB, MBBI, DL, X86::EBX, InArg.getReg(), InArg.isKill());
589 // Create the actual instruction.
590 BuildMI(MBB, MBBI, DL, TII->get(X86::MWAITXrrr));
591 // Finally, restore the value of RBX.
592 Register SaveRbx = MBBI->getOperand(2).getReg();
593 TII->copyPhysReg(MBB, MBBI, DL, X86::RBX, SaveRbx, /*SrcIsKill*/ true);
594 // Delete the pseudo.
596 return true;
597 }
598 case TargetOpcode::ICALL_BRANCH_FUNNEL:
599 expandICallBranchFunnel(&MBB, MBBI);
600 return true;
601 case X86::PLDTILECFGV: {
602 MI.setDesc(TII->get(GET_EGPR_IF_ENABLED(X86::LDTILECFG)));
603 return true;
604 }
605 case X86::PTILELOADDV:
606 case X86::PTILELOADDT1V:
607 case X86::PTILELOADDRSV:
608 case X86::PTILELOADDRST1V:
609 case X86::PTCVTROWD2PSrteV:
610 case X86::PTCVTROWD2PSrtiV:
611 case X86::PTCVTROWPS2BF16HrteV:
612 case X86::PTCVTROWPS2BF16HrtiV:
613 case X86::PTCVTROWPS2BF16LrteV:
614 case X86::PTCVTROWPS2BF16LrtiV:
615 case X86::PTCVTROWPS2PHHrteV:
616 case X86::PTCVTROWPS2PHHrtiV:
617 case X86::PTCVTROWPS2PHLrteV:
618 case X86::PTCVTROWPS2PHLrtiV:
619 case X86::PTILEMOVROWrteV:
620 case X86::PTILEMOVROWrtiV:
621 case X86::ACE_PTCVTROWD2PSrteV:
622 case X86::ACE_PTCVTROWD2PSrtiV:
623 case X86::ACE_PTCVTROWPS2BF16HrteV:
624 case X86::ACE_PTCVTROWPS2BF16HrtiV:
625 case X86::ACE_PTCVTROWPS2BF16LrteV:
626 case X86::ACE_PTCVTROWPS2BF16LrtiV:
627 case X86::ACE_PTCVTROWPS2PHHrteV:
628 case X86::ACE_PTCVTROWPS2PHHrtiV:
629 case X86::ACE_PTCVTROWPS2PHLrteV:
630 case X86::ACE_PTCVTROWPS2PHLrtiV:
631 case X86::ACE_PTILEMOVROWrteV:
632 case X86::ACE_PTILEMOVROWrtiV: {
633 for (unsigned i = 2; i > 0; --i)
634 MI.removeOperand(i);
635 unsigned Opc;
636 switch (Opcode) {
637 case X86::PTILELOADDRSV:
638 Opc = GET_EGPR_IF_ENABLED(X86::TILELOADDRS);
639 break;
640 case X86::PTILELOADDRST1V:
641 Opc = GET_EGPR_IF_ENABLED(X86::TILELOADDRST1);
642 break;
643 case X86::PTILELOADDV:
644 Opc = GET_EGPR_IF_ENABLED(X86::TILELOADD);
645 break;
646 case X86::PTILELOADDT1V:
647 Opc = GET_EGPR_IF_ENABLED(X86::TILELOADDT1);
648 break;
649 case X86::PTCVTROWD2PSrteV:
650 case X86::ACE_PTCVTROWD2PSrteV:
651 Opc = X86::TCVTROWD2PSrte;
652 break;
653 case X86::PTCVTROWD2PSrtiV:
654 case X86::ACE_PTCVTROWD2PSrtiV:
655 Opc = X86::TCVTROWD2PSrti;
656 break;
657 case X86::PTCVTROWPS2BF16HrteV:
658 case X86::ACE_PTCVTROWPS2BF16HrteV:
659 Opc = X86::TCVTROWPS2BF16Hrte;
660 break;
661 case X86::PTCVTROWPS2BF16HrtiV:
662 case X86::ACE_PTCVTROWPS2BF16HrtiV:
663 Opc = X86::TCVTROWPS2BF16Hrti;
664 break;
665 case X86::PTCVTROWPS2BF16LrteV:
666 case X86::ACE_PTCVTROWPS2BF16LrteV:
667 Opc = X86::TCVTROWPS2BF16Lrte;
668 break;
669 case X86::PTCVTROWPS2BF16LrtiV:
670 case X86::ACE_PTCVTROWPS2BF16LrtiV:
671 Opc = X86::TCVTROWPS2BF16Lrti;
672 break;
673 case X86::PTCVTROWPS2PHHrteV:
674 case X86::ACE_PTCVTROWPS2PHHrteV:
675 Opc = X86::TCVTROWPS2PHHrte;
676 break;
677 case X86::PTCVTROWPS2PHHrtiV:
678 case X86::ACE_PTCVTROWPS2PHHrtiV:
679 Opc = X86::TCVTROWPS2PHHrti;
680 break;
681 case X86::PTCVTROWPS2PHLrteV:
682 case X86::ACE_PTCVTROWPS2PHLrteV:
683 Opc = X86::TCVTROWPS2PHLrte;
684 break;
685 case X86::PTCVTROWPS2PHLrtiV:
686 case X86::ACE_PTCVTROWPS2PHLrtiV:
687 Opc = X86::TCVTROWPS2PHLrti;
688 break;
689 case X86::PTILEMOVROWrteV:
690 case X86::ACE_PTILEMOVROWrteV:
691 Opc = X86::TILEMOVROWrte;
692 break;
693 case X86::PTILEMOVROWrtiV:
694 case X86::ACE_PTILEMOVROWrtiV:
695 Opc = X86::TILEMOVROWrti;
696 break;
697 default:
698 llvm_unreachable("Unexpected Opcode");
699 }
700 MI.setDesc(TII->get(Opc));
701 return true;
702 }
703 case X86::PTCMMIMFP16PSV:
704 case X86::PTCMMRLFP16PSV:
705 case X86::PTDPBSSDV:
706 case X86::PTDPBSUDV:
707 case X86::PTDPBUSDV:
708 case X86::PTDPBUUDV:
709 case X86::PTDPBF16PSV:
710 case X86::PTDPFP16PSV:
711 case X86::PTDPBF8PSV:
712 case X86::PTDPBHF8PSV:
713 case X86::PTDPHBF8PSV:
714 case X86::PTDPHF8PSV: {
715 MI.untieRegOperand(4);
716 for (unsigned i = 3; i > 0; --i)
717 MI.removeOperand(i);
718 unsigned Opc;
719 switch (Opcode) {
720 // clang-format off
721 case X86::PTCMMIMFP16PSV: Opc = X86::TCMMIMFP16PS; break;
722 case X86::PTCMMRLFP16PSV: Opc = X86::TCMMRLFP16PS; break;
723 case X86::PTDPBSSDV: Opc = X86::TDPBSSD; break;
724 case X86::PTDPBSUDV: Opc = X86::TDPBSUD; break;
725 case X86::PTDPBUSDV: Opc = X86::TDPBUSD; break;
726 case X86::PTDPBUUDV: Opc = X86::TDPBUUD; break;
727 case X86::PTDPBF16PSV: Opc = X86::TDPBF16PS; break;
728 case X86::PTDPFP16PSV: Opc = X86::TDPFP16PS; break;
729 case X86::PTDPBF8PSV: Opc = X86::TDPBF8PS; break;
730 case X86::PTDPBHF8PSV: Opc = X86::TDPBHF8PS; break;
731 case X86::PTDPHBF8PSV: Opc = X86::TDPHBF8PS; break;
732 case X86::PTDPHF8PSV: Opc = X86::TDPHF8PS; break;
733 // clang-format on
734 default:
735 llvm_unreachable("Unexpected Opcode");
736 }
737 MI.setDesc(TII->get(Opc));
738 MI.tieOperands(0, 1);
739 return true;
740 }
741 case X86::PTOP2BF16PStrrV:
742 case X86::PTOP4BUUDtrrV:
743 case X86::PTOP4BUSDtrrV:
744 case X86::PTOP4BSSDtrrV:
745 case X86::PTOP4BSUDtrrV: {
746 MI.untieRegOperand(3);
747 for (unsigned i = 2; i > 0; --i)
748 MI.removeOperand(i);
749 unsigned Opc = 0;
750 switch (Opcode) {
751 case X86::PTOP2BF16PStrrV:
752 Opc = X86::TOP2BF16PStrr;
753 break;
754 case X86::PTOP4BUUDtrrV:
755 Opc = X86::TOP4BUUDtrr;
756 break;
757 case X86::PTOP4BUSDtrrV:
758 Opc = X86::TOP4BUSDtrr;
759 break;
760 case X86::PTOP4BSSDtrrV:
761 Opc = X86::TOP4BSSDtrr;
762 break;
763 case X86::PTOP4BSUDtrrV:
764 Opc = X86::TOP4BSUDtrr;
765 break;
766 default:
767 llvm_unreachable("Unexpected ACE opcode");
768 }
769 MI.setDesc(TII->get(Opc));
770 MI.tieOperands(0, 1);
771 return true;
772 }
773 case X86::PTOP4MXHF8PStrriV:
774 case X86::PTOP4MXBHF8PStrriV:
775 case X86::PTOP4MXHBF8PStrriV:
776 case X86::PTOP4MXBF8PStrriV:
777 case X86::PTOP4MXBSSPStrriV: {
778 MI.untieRegOperand(4);
779 for (unsigned i = 2; i > 0; --i)
780 MI.removeOperand(i);
781 unsigned ImmVal = MI.getOperand(1).getImm();
782 MI.removeOperand(1);
783 unsigned Opc = 0;
784 switch (Opcode) {
785 case X86::PTOP4MXHF8PStrriV:
786 Opc = X86::TOP4MXHF8PStrri;
787 break;
788 case X86::PTOP4MXBHF8PStrriV:
789 Opc = X86::TOP4MXBHF8PStrri;
790 break;
791 case X86::PTOP4MXHBF8PStrriV:
792 Opc = X86::TOP4MXHBF8PStrri;
793 break;
794 case X86::PTOP4MXBF8PStrriV:
795 Opc = X86::TOP4MXBF8PStrri;
796 break;
797 case X86::PTOP4MXBSSPStrriV:
798 Opc = X86::TOP4MXBSSPStrri;
799 break;
800 default:
801 llvm_unreachable("Unexpected ACE TOP4MX opcode");
802 }
803 MI.setDesc(TII->get(Opc));
804 MI.addOperand(MachineOperand::CreateImm(ImmVal));
805 MI.tieOperands(0, 1);
806 return true;
807 }
808 case X86::PTILEMOVCOLtriV:
809 case X86::PTILEMOVROWtriV:
810 case X86::PTILEMOVCOLtreV:
811 case X86::PTILEMOVROWtreV: {
812 MI.untieRegOperand(3);
813 for (unsigned i = 2; i > 0; --i)
814 MI.removeOperand(i);
815 unsigned Opc;
816 switch (Opcode) {
817 default:
818 llvm_unreachable("Unexpected ACE tile move opcode");
819 case X86::PTILEMOVCOLtriV:
820 Opc = X86::TILEMOVCOLtri;
821 break;
822 case X86::PTILEMOVROWtriV:
823 Opc = X86::TILEMOVROWtri;
824 break;
825 case X86::PTILEMOVCOLtreV:
826 Opc = X86::TILEMOVCOLtre;
827 break;
828 case X86::PTILEMOVROWtreV:
829 Opc = X86::TILEMOVROWtre;
830 break;
831 }
832 MI.setDesc(TII->get(Opc));
833 MI.tieOperands(0, 1);
834 return true;
835 }
836 case X86::PTILESTOREDV: {
837 for (int i = 1; i >= 0; --i)
838 MI.removeOperand(i);
839 MI.setDesc(TII->get(GET_EGPR_IF_ENABLED(X86::TILESTORED)));
840 return true;
841 }
842#undef GET_EGPR_IF_ENABLED
843 case X86::PTILEZEROV:
844 case X86::ACE_PTILEZEROV: {
845 for (int i = 2; i > 0; --i) // Remove row, col
846 MI.removeOperand(i);
847 MI.setDesc(TII->get(X86::TILEZERO));
848 return true;
849 }
850 case X86::CALL64pcrel32_RVMARKER:
851 case X86::CALL64r_RVMARKER:
852 case X86::CALL64m_RVMARKER:
853 expandCALL_RVMARKER(MBB, MBBI);
854 return true;
855 case X86::CALL64r_ImpCall:
856 MI.setDesc(TII->get(X86::CALL64r));
857 return true;
858 case X86::ADD32mi_ND:
859 case X86::ADD64mi32_ND:
860 case X86::SUB32mi_ND:
861 case X86::SUB64mi32_ND:
862 case X86::AND32mi_ND:
863 case X86::AND64mi32_ND:
864 case X86::OR32mi_ND:
865 case X86::OR64mi32_ND:
866 case X86::XOR32mi_ND:
867 case X86::XOR64mi32_ND:
868 case X86::ADC32mi_ND:
869 case X86::ADC64mi32_ND:
870 case X86::SBB32mi_ND:
871 case X86::SBB64mi32_ND: {
872 // It's possible for an EVEX-encoded legacy instruction to reach the 15-byte
873 // instruction length limit: 4 bytes of EVEX prefix + 1 byte of opcode + 1
874 // byte of ModRM + 1 byte of SIB + 4 bytes of displacement + 4 bytes of
875 // immediate = 15 bytes in total, e.g.
876 //
877 // subq $184, %fs:257(%rbx, %rcx), %rax
878 //
879 // In such a case, no additional (ADSIZE or segment override) prefix can be
880 // used. To resolve the issue, we split the “long” instruction into 2
881 // instructions:
882 //
883 // movq %fs:257(%rbx, %rcx),%rax
884 // subq $184, %rax
885 //
886 // Therefore we consider the OPmi_ND to be a pseudo instruction to some
887 // extent.
888 const MachineOperand &ImmOp =
889 MI.getOperand(MI.getNumExplicitOperands() - 1);
890 // If the immediate is a expr, conservatively estimate 4 bytes.
891 if (ImmOp.isImm() && isInt<8>(ImmOp.getImm()))
892 return false;
893 int MemOpNo = X86::getFirstAddrOperandIdx(MI);
894 const MachineOperand &DispOp = MI.getOperand(MemOpNo + X86::AddrDisp);
895 Register Base = MI.getOperand(MemOpNo + X86::AddrBaseReg).getReg();
896 // If the displacement is a expr, conservatively estimate 4 bytes.
897 if (Base && DispOp.isImm() && isInt<8>(DispOp.getImm()))
898 return false;
899 // There can only be one of three: SIB, segment override register, ADSIZE
900 Register Index = MI.getOperand(MemOpNo + X86::AddrIndexReg).getReg();
901 unsigned Count = !!MI.getOperand(MemOpNo + X86::AddrSegmentReg).getReg();
902 if (X86II::needSIB(Base, Index, /*In64BitMode=*/true))
903 ++Count;
904 if (getX86MCRegisterClass(X86::GR32RegClassID).contains(Base) ||
905 getX86MCRegisterClass(X86::GR32RegClassID).contains(Index))
906 ++Count;
907 if (Count < 2)
908 return false;
909 unsigned Opc, LoadOpc;
910 switch (Opcode) {
911#define MI_TO_RI(OP) \
912 case X86::OP##32mi_ND: \
913 Opc = X86::OP##32ri; \
914 LoadOpc = X86::MOV32rm; \
915 break; \
916 case X86::OP##64mi32_ND: \
917 Opc = X86::OP##64ri32; \
918 LoadOpc = X86::MOV64rm; \
919 break;
920
921 default:
922 llvm_unreachable("Unexpected Opcode");
923 MI_TO_RI(ADD);
924 MI_TO_RI(SUB);
925 MI_TO_RI(AND);
926 MI_TO_RI(OR);
927 MI_TO_RI(XOR);
928 MI_TO_RI(ADC);
929 MI_TO_RI(SBB);
930#undef MI_TO_RI
931 }
932 // Insert OPri.
933 Register DestReg = MI.getOperand(0).getReg();
934 BuildMI(MBB, std::next(MBBI), DL, TII->get(Opc), DestReg)
935 .addReg(DestReg)
936 .add(ImmOp);
937 // Change OPmi_ND to MOVrm.
938 for (unsigned I = MI.getNumImplicitOperands() + 1; I != 0; --I)
939 MI.removeOperand(MI.getNumOperands() - 1);
940 MI.setDesc(TII->get(LoadOpc));
941 return true;
942 }
943 }
944 llvm_unreachable("Previous switch has a fallthrough?");
945}
946
947// This function creates additional block for storing varargs guarded
948// registers. It adds check for %al into entry block, to skip
949// GuardedRegsBlk if xmm registers should not be stored.
950//
951// EntryBlk[VAStartPseudoInstr] EntryBlk
952// | | .
953// | | .
954// | | GuardedRegsBlk
955// | => | .
956// | | .
957// | TailBlk
958// | |
959// | |
960//
961void X86ExpandPseudoImpl::expandVastartSaveXmmRegs(
962 MachineBasicBlock *EntryBlk,
963 MachineBasicBlock::iterator VAStartPseudoInstr) const {
964 assert(VAStartPseudoInstr->getOpcode() == X86::VASTART_SAVE_XMM_REGS);
965
966 MachineFunction *Func = EntryBlk->getParent();
967 const TargetInstrInfo *TII = STI->getInstrInfo();
968 const DebugLoc &DL = VAStartPseudoInstr->getDebugLoc();
969 Register CountReg = VAStartPseudoInstr->getOperand(0).getReg();
970
971 // Calculate liveins for newly created blocks.
972 LivePhysRegs LiveRegs(*STI->getRegisterInfo());
974
975 LiveRegs.addLiveIns(*EntryBlk);
976 for (MachineInstr &MI : EntryBlk->instrs()) {
977 if (MI.getOpcode() == VAStartPseudoInstr->getOpcode())
978 break;
979
980 LiveRegs.stepForward(MI, Clobbers);
981 }
982
983 // Create the new basic blocks. One block contains all the XMM stores,
984 // and another block is the final destination regardless of whether any
985 // stores were performed.
986 const BasicBlock *LLVMBlk = EntryBlk->getBasicBlock();
987 MachineFunction::iterator EntryBlkIter = ++EntryBlk->getIterator();
988 MachineBasicBlock *GuardedRegsBlk = Func->CreateMachineBasicBlock(LLVMBlk);
989 MachineBasicBlock *TailBlk = Func->CreateMachineBasicBlock(LLVMBlk);
990 Func->insert(EntryBlkIter, GuardedRegsBlk);
991 Func->insert(EntryBlkIter, TailBlk);
992
993 // Transfer the remainder of EntryBlk and its successor edges to TailBlk.
994 TailBlk->splice(TailBlk->begin(), EntryBlk,
995 std::next(MachineBasicBlock::iterator(VAStartPseudoInstr)),
996 EntryBlk->end());
997 TailBlk->transferSuccessorsAndUpdatePHIs(EntryBlk);
998
999 uint64_t FrameOffset = VAStartPseudoInstr->getOperand(4).getImm();
1000 uint64_t VarArgsRegsOffset = VAStartPseudoInstr->getOperand(6).getImm();
1001
1002 // TODO: add support for YMM and ZMM here.
1003 unsigned MOVOpc = STI->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
1004
1005 // In the XMM save block, save all the XMM argument registers.
1006 for (int64_t OpndIdx = 7, RegIdx = 0;
1007 OpndIdx < VAStartPseudoInstr->getNumOperands() - 1;
1008 OpndIdx++, RegIdx++) {
1009 auto NewMI = BuildMI(GuardedRegsBlk, DL, TII->get(MOVOpc));
1010 for (int i = 0; i < X86::AddrNumOperands; ++i) {
1011 if (i == X86::AddrDisp)
1012 NewMI.addImm(FrameOffset + VarArgsRegsOffset + RegIdx * 16);
1013 else
1014 NewMI.add(VAStartPseudoInstr->getOperand(i + 1));
1015 }
1016 NewMI.addReg(VAStartPseudoInstr->getOperand(OpndIdx).getReg());
1017 assert(VAStartPseudoInstr->getOperand(OpndIdx).getReg().isPhysical());
1018 }
1019
1020 // The original block will now fall through to the GuardedRegsBlk.
1021 EntryBlk->addSuccessor(GuardedRegsBlk);
1022 // The GuardedRegsBlk will fall through to the TailBlk.
1023 GuardedRegsBlk->addSuccessor(TailBlk);
1024
1025 if (!STI->isCallingConvWin64(Func->getFunction().getCallingConv())) {
1026 // If %al is 0, branch around the XMM save block.
1027 BuildMI(EntryBlk, DL, TII->get(X86::TEST8rr))
1028 .addReg(CountReg)
1029 .addReg(CountReg);
1030 BuildMI(EntryBlk, DL, TII->get(X86::JCC_1))
1031 .addMBB(TailBlk)
1033 EntryBlk->addSuccessor(TailBlk);
1034 }
1035
1036 // Add liveins to the created block.
1037 addLiveIns(*GuardedRegsBlk, LiveRegs);
1038 addLiveIns(*TailBlk, LiveRegs);
1039
1040 // Delete the pseudo.
1041 VAStartPseudoInstr->eraseFromParent();
1042}
1043
1044/// Expand all pseudo instructions contained in \p MBB.
1045/// \returns true if any expansion occurred for \p MBB.
1046bool X86ExpandPseudoImpl::expandMBB(MachineBasicBlock &MBB) {
1047 bool Modified = false;
1048
1049 // MBBI may be invalidated by the expansion.
1051 while (MBBI != E) {
1052 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
1054 MBBI = NMBBI;
1055 }
1056
1057 return Modified;
1058}
1059
1060bool X86ExpandPseudoImpl::expandPseudosWhichAffectControlFlow(
1061 MachineFunction &MF) {
1062 // Currently pseudo which affects control flow is only
1063 // X86::VASTART_SAVE_XMM_REGS. It is created in the entry block, but the
1064 // prologue can split that block, e.g. for an inline stack probe loop.
1065 if (!X86FI->getHasVAStartSaveXMMRegs())
1066 return false;
1067
1068 for (MachineBasicBlock &MBB : MF) {
1069 for (MachineInstr &Instr : MBB.instrs()) {
1070 if (Instr.getOpcode() == X86::VASTART_SAVE_XMM_REGS) {
1071 expandVastartSaveXmmRegs(&MBB, Instr);
1072 CFGChanged = true;
1073 return true;
1074 }
1075 }
1076 }
1077
1078 return false;
1079}
1080
1081bool X86ExpandPseudoImpl::runOnMachineFunction(MachineFunction &MF) {
1082 STI = &MF.getSubtarget<X86Subtarget>();
1083 TII = STI->getInstrInfo();
1084 TRI = STI->getRegisterInfo();
1085 X86FI = MF.getInfo<X86MachineFunctionInfo>();
1086 X86FL = STI->getFrameLowering();
1087
1088 bool Modified = expandPseudosWhichAffectControlFlow(MF);
1089
1090 for (MachineBasicBlock &MBB : MF)
1092 return Modified;
1093}
1094
1095/// Returns an instance of the pseudo instruction expansion pass.
1097 return new X86ExpandPseudoLegacy();
1098}
1099
1100bool X86ExpandPseudoLegacy::runOnMachineFunction(MachineFunction &MF) {
1101 X86ExpandPseudoImpl Impl;
1102 return Impl.runOnMachineFunction(MF);
1103}
1104
1105PreservedAnalyses
1108 X86ExpandPseudoImpl Impl;
1109 bool Changed = Impl.runOnMachineFunction(MF);
1110 if (!Changed)
1111 return PreservedAnalyses::all();
1112
1114 // TODO: We should propagate this info directly once we are done with NewPM
1115 // transition.
1116 if (!Impl.CFGChanged)
1118 return PA;
1119}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AArch64InstrInfo & TII
uint64_t uint64_t
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define DEBUG_TYPE
IRTranslator LLVM IR MI
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
static bool expandMBB(MachineBasicBlock &MBB, const LoongArchInstrInfo *TII)
static bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineBasicBlock::iterator &NextMBBI, const LoongArchInstrInfo *TII)
unsigned Opc
#define I(x, y, z)
Definition MD5.cpp:57
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
RISCVCC::CondCode CC
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:479
static Target * FirstTarget
#define GET_EGPR_IF_ENABLED(OPC)
#define MI_TO_RI(OP)
#define X86_EXPAND_PSEUDO_NAME
void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, const DebugLoc &DL, Register DestReg, Register SrcReg, bool KillSrc, bool RenamableDest=false, bool RenamableSrc=false) const override
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:280
LLVM Basic Block Representation.
Definition BasicBlock.h:61
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
A debug info location.
Definition DebugLoc.h:126
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
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.
BasicBlockListType::iterator iterator
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags F, LLT MemTy, Align BaseAlignment, const MMOMetadata &Metadata=MMOMetadata(), SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
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 & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
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 & 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
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
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.
void setIsDead(bool Val=true)
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
static MachineOperand CreateImm(int64_t Val)
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.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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:781
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
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:123
Pass manager infrastructure for declaring and invalidating analyses.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:83
@ X86
Windows x64, Windows Itanium (IA-64)
Definition MCAsmInfo.h:53
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:37
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:316
@ Offset
Definition DWP.cpp:577
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...
FunctionPass * createX86ExpandPseudoLegacyPass()
Returns an instance of the pseudo instruction expansion pass.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
static bool isMem(const MachineInstr &MI, unsigned Op)
constexpr RegState getKillRegState(bool B)
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
constexpr RegState getDeadRegState(bool B)
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
DWARFExpression::Operation Op
LLVM_ABI void addLiveIns(MachineBasicBlock &MBB, const LivePhysRegs &LiveRegs)
Adds registers contained in LiveRegs to the block live-in list of MBB.