LLVM 24.0.0git
X86FixupBWInsts.cpp
Go to the documentation of this file.
1//===-- X86FixupBWInsts.cpp - Fixup Byte or Word 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/// \file
9/// This file defines the pass that looks through the machine instructions
10/// late in the compilation, and finds byte or word instructions that
11/// can be profitably replaced with 32 bit instructions that give equivalent
12/// results for the bits of the results that are used. There are two possible
13/// reasons to do this.
14///
15/// One reason is to avoid false-dependences on the upper portions
16/// of the registers. Only instructions that have a destination register
17/// which is not in any of the source registers can be affected by this.
18/// Any instruction where one of the source registers is also the destination
19/// register is unaffected, because it has a true dependence on the source
20/// register already. So, this consideration primarily affects load
21/// instructions and register-to-register moves. It would
22/// seem like cmov(s) would also be affected, but because of the way cmov is
23/// really implemented by most machines as reading both the destination and
24/// and source registers, and then "merging" the two based on a condition,
25/// it really already should be considered as having a true dependence on the
26/// destination register as well.
27///
28/// The other reason to do this is for potential code size savings. Word
29/// operations need an extra override byte compared to their 32 bit
30/// versions. So this can convert many word operations to their larger
31/// size, saving a byte in encoding. This could introduce partial register
32/// dependences where none existed however. As an example take:
33/// orw ax, $0x1000
34/// addw ax, $3
35/// now if this were to get transformed into
36/// orw ax, $1000
37/// addl eax, $3
38/// because the addl encodes shorter than the addw, this would introduce
39/// a use of a register that was only partially written earlier. On older
40/// Intel processors this can be quite a performance penalty, so this should
41/// probably only be done when it can be proven that a new partial dependence
42/// wouldn't be created, or when your know a newer processor is being
43/// targeted, or when optimizing for minimum code size.
44///
45/// Widening the loads in this pass can leave behind zero and sign extends of
46/// values that are already extended, so as a second step these are removed.
47///
48//===----------------------------------------------------------------------===//
49
50#include "X86.h"
51#include "X86InstrInfo.h"
52#include "X86Subtarget.h"
53#include "llvm/ADT/DenseMap.h"
54#include "llvm/ADT/Statistic.h"
63#include "llvm/CodeGen/Passes.h"
65#include "llvm/Support/Debug.h"
67#include <optional>
68using namespace llvm;
69
70#define FIXUPBW_DESC "X86 Byte/Word Instruction Fixup"
71#define FIXUPBW_NAME "x86-fixup-bw-insts"
72
73#define DEBUG_TYPE FIXUPBW_NAME
74
75namespace {
76class X86FixupBWInstImpl {
77public:
78 X86FixupBWInstImpl(ProfileSummaryInfo *PSI, MachineBlockFrequencyInfo *MBFI)
79 : PSI(PSI), MBFI(MBFI) {}
80 bool runOnMachineFunction(MachineFunction &MF);
81
82private:
83 /// Loop over all of the instructions in the basic block replacing applicable
84 /// byte or word instructions with better alternatives.
86
87 /// This returns the 32 bit super reg of the original destination register of
88 /// the MachineInstr passed in, if that super register is dead just prior to
89 /// \p OrigMI. Otherwise it returns Register().
90 Register getSuperRegDestIfDead(MachineInstr *OrigMI) const;
91
92 /// Change the MachineInstr \p MI into the equivalent extending load to 32 bit
93 /// register if it is safe to do so. Return the replacement instruction if
94 /// OK, otherwise return nullptr.
95 MachineInstr *tryReplaceLoad(unsigned New32BitOpcode, MachineInstr *MI) const;
96
97 /// Change the MachineInstr \p MI into the equivalent 32-bit copy if it is
98 /// safe to do so. Return the replacement instruction if OK, otherwise return
99 /// nullptr.
100 MachineInstr *tryReplaceCopy(MachineInstr *MI) const;
101
102 /// Change the MachineInstr \p MI into the equivalent extend to 32 bit
103 /// register if it is safe to do so. Return the replacement instruction if
104 /// OK, otherwise return nullptr.
105 MachineInstr *tryReplaceExtend(unsigned New32BitOpcode,
106 MachineInstr *MI) const;
107
108 // Change the MachineInstr \p MI into an eqivalent 32 bit instruction if
109 // possible. Return the replacement instruction if OK, return nullptr
110 // otherwise.
111 MachineInstr *tryReplaceInstr(MachineInstr *MI, MachineBasicBlock &MBB) const;
112
113 /// Remove the zero and sign extends in \p MBB of values that an earlier
114 /// instruction in the same block (widened by this pass) has already extended.
115 void eliminateRedundantExtends(MachineBasicBlock &MBB);
116
117 MachineFunction *MF = nullptr;
118
119 /// Machine instruction info used throughout the class.
120 const X86InstrInfo *TII = nullptr;
121
122 const TargetRegisterInfo *TRI = nullptr;
123
124 /// Local member for function's OptForSize attribute.
125 bool OptForSize = false;
126
127 /// Register Liveness information after the current instruction.
128 LiveRegUnits LiveUnits;
129
130 ProfileSummaryInfo *PSI = nullptr;
131 MachineBlockFrequencyInfo *MBFI = nullptr;
132};
133
134class X86FixupBWInstLegacy : public MachineFunctionPass {
135public:
136 static char ID;
137
138 StringRef getPassName() const override { return FIXUPBW_DESC; }
139
140 X86FixupBWInstLegacy() : MachineFunctionPass(ID) {}
141
142 void getAnalysisUsage(AnalysisUsage &AU) const override {
146 }
147
148 /// Loop over all of the basic blocks, replacing byte and word instructions by
149 /// equivalent 32 bit instructions where performance or code size can be
150 /// improved.
151 bool runOnMachineFunction(MachineFunction &MF) override;
152
153 MachineFunctionProperties getRequiredProperties() const override {
154 return MachineFunctionProperties().setNoVRegs();
155 }
156};
157
158char X86FixupBWInstLegacy::ID = 0;
159
160} // namespace
161
162INITIALIZE_PASS(X86FixupBWInstLegacy, FIXUPBW_NAME, FIXUPBW_DESC, false, false)
163
165 return new X86FixupBWInstLegacy();
166}
167
168bool X86FixupBWInstImpl::runOnMachineFunction(MachineFunction &MF) {
169 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
170 if (!ST.getCLOpts().fixup_byte_word_insts)
171 return false;
172
173 this->MF = &MF;
174 TII = ST.getInstrInfo();
176 LiveUnits.init(TII->getRegisterInfo());
177
178 LLVM_DEBUG(dbgs() << "Start X86FixupBWInsts\n";);
179
180 // Process all basic blocks.
181 for (auto &MBB : MF)
183
184 LLVM_DEBUG(dbgs() << "End X86FixupBWInsts\n";);
185
186 return true;
187}
188
189/// Check if after \p OrigMI the only portion of super register
190/// of the destination register of \p OrigMI that is alive is that
191/// destination register.
192///
193/// If so, return that super register in \p SuperDestReg.
194Register X86FixupBWInstImpl::getSuperRegDestIfDead(MachineInstr *OrigMI) const {
195 const X86RegisterInfo *TRI = &TII->getRegisterInfo();
196 Register OrigDestReg = OrigMI->getOperand(0).getReg();
197 Register SuperDestReg = getX86SubSuperRegister(OrigDestReg, 32);
198 assert(SuperDestReg.isValid() && "Invalid Operand");
199
200 const auto SubRegIdx = TRI->getSubRegIndex(SuperDestReg, OrigDestReg);
201
202 // Make sure that the sub-register that this instruction has as its
203 // destination is the lowest order sub-register of the super-register.
204 // If it isn't, then the register isn't really dead even if the
205 // super-register is considered dead.
206 if (SubRegIdx == X86::sub_8bit_hi)
207 return Register();
208
209 // Test all regunits of the super register that are not part of the
210 // sub register. If none of them are live then the super register is safe to
211 // use.
212 bool SuperIsLive = false;
213 auto Range = TRI->regunits(OrigDestReg);
214 MCRegUnitIterator I = Range.begin(), E = Range.end();
215 for (MCRegUnit S : TRI->regunits(SuperDestReg)) {
216 I = std::lower_bound(I, E, S);
217 if ((I == E || *I > S) &&
218 LiveUnits.getBitVector().test(static_cast<unsigned>(S))) {
219 SuperIsLive = true;
220 break;
221 }
222 }
223 if (!SuperIsLive)
224 return SuperDestReg;
225
226 // If we get here, the super-register destination (or some part of it) is
227 // marked as live after the original instruction.
228 //
229 // The X86 backend does not have subregister liveness tracking enabled,
230 // so liveness information might be overly conservative. Specifically, the
231 // super register might be marked as live because it is implicitly defined
232 // by the instruction we are examining.
233 //
234 // However, for some specific instructions (this pass only cares about MOVs)
235 // we can produce more precise results by analysing that MOV's operands.
236 //
237 // Indeed, if super-register is not live before the mov it means that it
238 // was originally <read-undef> and so we are free to modify these
239 // undef upper bits. That may happen in case where the use is in another MBB
240 // and the vreg/physreg corresponding to the move has higher width than
241 // necessary (e.g. due to register coalescing with a "truncate" copy).
242 // So, we would like to handle patterns like this:
243 //
244 // %bb.2: derived from LLVM BB %if.then
245 // Live Ins: %rdi
246 // Predecessors according to CFG: %bb.0
247 // %ax<def> = MOV16rm killed %rdi, 1, %noreg, 0, %noreg, implicit-def %eax
248 // ; No implicit %eax
249 // Successors according to CFG: %bb.3(?%)
250 //
251 // %bb.3: derived from LLVM BB %if.end
252 // Live Ins: %eax Only %ax is actually live
253 // Predecessors according to CFG: %bb.2 %bb.1
254 // %ax = KILL %ax, implicit killed %eax
255 // RET 0, %ax
256 unsigned Opc = OrigMI->getOpcode();
257 // These are the opcodes currently known to work with the code below, if
258 // something // else will be added we need to ensure that new opcode has the
259 // same properties.
260 if (Opc != X86::MOV8rm && Opc != X86::MOV16rm && Opc != X86::MOV8rr &&
261 Opc != X86::MOV16rr)
262 return Register();
263
264 bool IsDefined = false;
265 for (auto &MO: OrigMI->implicit_operands()) {
266 if (!MO.isReg())
267 continue;
268
269 if (MO.isDef() && TRI->isSuperRegisterEq(OrigDestReg, MO.getReg()))
270 IsDefined = true;
271
272 // If MO is a use of any part of the destination register but is not equal
273 // to OrigDestReg or one of its subregisters, we cannot use SuperDestReg.
274 // For example, if OrigDestReg is %al then an implicit use of %ah, %ax,
275 // %eax, or %rax will prevent us from using the %eax register.
276 if (MO.isUse() && !TRI->isSubRegisterEq(OrigDestReg, MO.getReg()) &&
277 TRI->regsOverlap(SuperDestReg, MO.getReg()))
278 return Register();
279 }
280 // Reg is not Imp-def'ed -> it's live both before/after the instruction.
281 if (!IsDefined)
282 return Register();
283
284 // Otherwise, the Reg is not live before the MI and the MOV can't
285 // make it really live, so it's in fact dead even after the MI.
286 return SuperDestReg;
287}
288
289MachineInstr *X86FixupBWInstImpl::tryReplaceLoad(unsigned New32BitOpcode,
290 MachineInstr *MI) const {
291 // We are going to try to rewrite this load to a larger zero-extending
292 // load. This is safe if all portions of the 32 bit super-register
293 // of the original destination register, except for the original destination
294 // register are dead. getSuperRegDestIfDead checks that.
295 Register NewDestReg = getSuperRegDestIfDead(MI);
296 if (!NewDestReg)
297 return nullptr;
298
299 // Safe to change the instruction.
300 MachineInstrBuilder MIB =
301 BuildMI(*MF, MIMetadata(*MI), TII->get(New32BitOpcode), NewDestReg);
302
303 unsigned NumArgs = MI->getNumOperands();
304 for (unsigned i = 1; i < NumArgs; ++i)
305 MIB.add(MI->getOperand(i));
306
307 MIB.setMemRefs(MI->memoperands());
308
309 // If it was debug tracked, record a substitution.
310 if (unsigned OldInstrNum = MI->peekDebugInstrNum()) {
311 unsigned Subreg = TRI->getSubRegIndex(MIB->getOperand(0).getReg(),
312 MI->getOperand(0).getReg());
313 unsigned NewInstrNum = MIB->getDebugInstrNum(*MF);
314 MF->makeDebugValueSubstitution({OldInstrNum, 0}, {NewInstrNum, 0}, Subreg);
315 }
316
317 return MIB;
318}
319
320MachineInstr *X86FixupBWInstImpl::tryReplaceCopy(MachineInstr *MI) const {
321 assert(MI->getNumExplicitOperands() == 2);
322 auto &OldDest = MI->getOperand(0);
323 auto &OldSrc = MI->getOperand(1);
324
325 Register NewDestReg = getSuperRegDestIfDead(MI);
326 if (!NewDestReg)
327 return nullptr;
328
329 Register NewSrcReg = getX86SubSuperRegister(OldSrc.getReg(), 32);
330 assert(NewSrcReg.isValid() && "Invalid Operand");
331
332 // This is only correct if we access the same subregister index: otherwise,
333 // we could try to replace "movb %ah, %al" with "movl %eax, %eax".
334 const X86RegisterInfo *TRI = &TII->getRegisterInfo();
335 if (TRI->getSubRegIndex(NewSrcReg, OldSrc.getReg()) !=
336 TRI->getSubRegIndex(NewDestReg, OldDest.getReg()))
337 return nullptr;
338
339 // Safe to change the instruction.
340 // Don't set src flags, as we don't know if we're also killing the superreg.
341 // However, the superregister might not be defined; make it explicit that
342 // we don't care about the higher bits by reading it as Undef, and adding
343 // an imp-use on the original subregister.
344 MachineInstrBuilder MIB =
345 BuildMI(*MF, MIMetadata(*MI), TII->get(X86::MOV32rr), NewDestReg)
346 .addReg(NewSrcReg, RegState::Undef)
347 .addReg(OldSrc.getReg(), RegState::Implicit);
348
349 // Drop imp-defs/uses that would be redundant with the new def/use.
350 for (auto &Op : MI->implicit_operands())
351 if (Op.getReg() != (Op.isDef() ? NewDestReg : NewSrcReg))
352 MIB.add(Op);
353
354 return MIB;
355}
356
357MachineInstr *X86FixupBWInstImpl::tryReplaceExtend(unsigned New32BitOpcode,
358 MachineInstr *MI) const {
359 Register NewDestReg = getSuperRegDestIfDead(MI);
360 if (!NewDestReg)
361 return nullptr;
362
363 // Don't interfere with formation of CBW instructions which should be a
364 // shorter encoding than even the MOVSX32rr8. It's also immune to partial
365 // merge issues on Intel CPUs.
366 if (MI->getOpcode() == X86::MOVSX16rr8 &&
367 MI->getOperand(0).getReg() == X86::AX &&
368 MI->getOperand(1).getReg() == X86::AL)
369 return nullptr;
370
371 // Safe to change the instruction.
372 MachineInstrBuilder MIB =
373 BuildMI(*MF, MIMetadata(*MI), TII->get(New32BitOpcode), NewDestReg);
374
375 unsigned NumArgs = MI->getNumOperands();
376 for (unsigned i = 1; i < NumArgs; ++i)
377 MIB.add(MI->getOperand(i));
378
379 MIB.setMemRefs(MI->memoperands());
380
381 if (unsigned OldInstrNum = MI->peekDebugInstrNum()) {
382 unsigned Subreg = TRI->getSubRegIndex(MIB->getOperand(0).getReg(),
383 MI->getOperand(0).getReg());
384 unsigned NewInstrNum = MIB->getDebugInstrNum(*MF);
385 MF->makeDebugValueSubstitution({OldInstrNum, 0}, {NewInstrNum, 0}, Subreg);
386 }
387
388 return MIB;
389}
390
391MachineInstr *
392X86FixupBWInstImpl::tryReplaceInstr(MachineInstr *MI,
393 MachineBasicBlock &MBB) const {
394 // See if this is an instruction of the type we are currently looking for.
395 switch (MI->getOpcode()) {
396
397 case X86::MOV8rm:
398 // Replace 8-bit loads with the zero-extending version if not optimizing
399 // for size. The extending op is cheaper across a wide range of uarch and
400 // it avoids a potentially expensive partial register stall. It takes an
401 // extra byte to encode, however, so don't do this when optimizing for size.
402 if (!OptForSize)
403 return tryReplaceLoad(X86::MOVZX32rm8, MI);
404 break;
405
406 case X86::MOV16rm:
407 // Always try to replace 16 bit load with 32 bit zero extending.
408 // Code size is the same, and there is sometimes a perf advantage
409 // from eliminating a false dependence on the upper portion of
410 // the register.
411 return tryReplaceLoad(X86::MOVZX32rm16, MI);
412
413 case X86::MOV8rr:
414 case X86::MOV16rr:
415 // Always try to replace 8/16 bit copies with a 32 bit copy.
416 // Code size is either less (16) or equal (8), and there is sometimes a
417 // perf advantage from eliminating a false dependence on the upper portion
418 // of the register.
419 return tryReplaceCopy(MI);
420
421 case X86::MOVSX16rr8:
422 return tryReplaceExtend(X86::MOVSX32rr8, MI);
423 case X86::MOVSX16rm8:
424 return tryReplaceExtend(X86::MOVSX32rm8, MI);
425 case X86::MOVZX16rr8:
426 return tryReplaceExtend(X86::MOVZX32rr8, MI);
427 case X86::MOVZX16rm8:
428 return tryReplaceExtend(X86::MOVZX32rm8, MI);
429
430 default:
431 // nothing to do here.
432 break;
433 }
434
435 return nullptr;
436}
437
438void X86FixupBWInstImpl::eliminateRedundantExtends(MachineBasicBlock &MBB) {
439 // A value that is the zero or sign extension of its low Bits bits.
440 struct Extension {
441 unsigned Bits;
442 bool IsSigned;
443 };
444
445 // Return the extension done by the extending mov.
446 auto DefinedExtension =
447 [](const MachineInstr &MI) -> std::optional<Extension> {
448 switch (MI.getOpcode()) {
449 case X86::MOVZX32rm8:
450 case X86::MOVZX32rr8:
451 return Extension{8, false};
452 case X86::MOVZX32rm16:
453 case X86::MOVZX32rr16:
454 return Extension{16, false};
455 case X86::MOVSX32rm8:
456 case X86::MOVSX32rr8:
457 return Extension{8, true};
458 case X86::MOVSX32rm16:
459 case X86::MOVSX32rr16:
460 return Extension{16, true};
461 default:
462 return std::nullopt;
463 }
464 };
465
466 // Whether extending the low Ext.Bits bits of a register known to hold
467 // KnownExt leaves its value unchanged. Zero extending a sign extended value
468 // never does, as the value may be negative. Sign extending a zero extended
469 // value does if the sign bit is known to be zero, i.e. the value was zero
470 // extended from fewer bits.
471 auto IsRedundant = [](Extension KnownExt, Extension Ext) {
472 if (KnownExt.IsSigned == Ext.IsSigned)
473 return KnownExt.Bits <= Ext.Bits;
474 return !KnownExt.IsSigned && KnownExt.Bits < Ext.Bits;
475 };
476
477 // Maps what is currently known about each 32 bit register to the extension
478 // its value is known to be the result of: a value zero extended from 8 bits
479 // is in [0, 0xFF] and one sign extended from 8 bits is in [-0x80, 0x7F], and
480 // likewise for 16 bits. Only tracks registers within a single block.
481 SmallDenseMap<MCRegister, Extension, 8> Known;
482
483 for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
484 if (MI.isDebugInstr())
485 continue;
486
487 std::optional<Extension> Ext = DefinedExtension(MI);
488
489 // Only match 8 and 16 bit register to register extends to 32 bits. Loads
490 // can't be eliminated. 64 bit zero extends are done by the 32 bit versions,
491 // while 64 bit sign extends are not handled.
492 unsigned Opc = MI.getOpcode();
493 if (Opc == X86::MOVZX32rr8 || Opc == X86::MOVZX32rr16 ||
494 Opc == X86::MOVSX32rr8 || Opc == X86::MOVSX32rr16) {
495 MCRegister Dst = MI.getOperand(0).getReg().asMCReg();
496 MCRegister Src = MI.getOperand(1).getReg().asMCReg();
497 MCRegister SrcSuper = getX86SubSuperRegister(Src, 32);
498 auto It = Known.find(SrcSuper);
499 // Reading %ah and friends does not read the part of the super register
500 // that is known to hold the whole value, so insist on the low bits.
501 if (It != Known.end() && IsRedundant(It->second, *Ext) &&
502 getX86SubSuperRegister(SrcSuper, Ext->Bits) == Src) {
503
504 if (Dst == SrcSuper) {
505 // The extend writes back the value that is already in the register.
506 LLVM_DEBUG(dbgs() << "Removing redundant extend: " << MI);
507 MI.eraseFromParent();
508 continue;
509 }
510
511 // The extend is redundant but the move is not. A 32 bit copy is a
512 // byte shorter and can be eliminated at rename.
513 LLVM_DEBUG(dbgs() << "Turning extend into a copy: " << MI);
514 MachineInstrBuilder MIB =
515 BuildMI(MBB, MI, MIMetadata(MI), TII->get(X86::MOV32rr), Dst)
516 .addReg(SrcSuper);
517 if (unsigned OldInstrNum = MI.peekDebugInstrNum()) {
518 unsigned NewInstrNum = MIB->getDebugInstrNum(*MF);
519 MF->makeDebugValueSubstitution({OldInstrNum, 0}, {NewInstrNum, 0}, 0);
520 }
521 MI.eraseFromParent();
522
523 // The copy leaves the destination holding the value the source had,
524 // so it inherits what was known about it.
525 Extension SrcExt = It->second;
526 Known[Dst] = SrcExt;
527 continue;
528 }
529 }
530
531 // Clear what this instruction overwrites. Two 32 bit registers never
532 // overlap, so a definition can only invalidate the one it is a sub or
533 // super register of.
534 for (const MachineOperand &MO : MI.operands()) {
535 if (MO.isRegMask()) {
537 for (auto &KnownReg : Known)
538 if (MO.clobbersPhysReg(KnownReg.first))
539 Clobbered.push_back(KnownReg.first);
540 for (MCRegister Reg : Clobbered)
541 Known.erase(Reg);
542 } else if (MO.isReg() && MO.isDef()) {
543 Known.erase(getX86SubSuperRegister(MO.getReg().asMCReg(), 32));
544 }
545 }
546
547 if (Ext) {
548 MCRegister Dst = MI.getOperand(0).getReg().asMCReg();
549 Known[getX86SubSuperRegister(Dst, 32)] = *Ext;
550 }
551 }
552}
553
554void X86FixupBWInstImpl::processBasicBlock(MachineFunction &MF,
555 MachineBasicBlock &MBB) {
556
557 // This algorithm doesn't delete the instructions it is replacing
558 // right away. By leaving the existing instructions in place, the
559 // register liveness information doesn't change, and this makes the
560 // analysis that goes on be better than if the replaced instructions
561 // were immediately removed.
562 //
563 // This algorithm always creates a replacement instruction
564 // and notes that and the original in a data structure, until the
565 // whole BB has been analyzed. This keeps the replacement instructions
566 // from making it seem as if the larger register might be live.
568
569 // Start computing liveness for this block. We iterate from the end to be able
570 // to update this for each instruction.
571 LiveUnits.clear();
572 // We run after PEI, so we need to AddPristinesAndCSRs.
573 LiveUnits.addLiveOuts(MBB);
574
575 OptForSize = llvm::shouldOptimizeForSize(&MBB, PSI, MBFI);
576
577 for (MachineInstr &MI : llvm::reverse(MBB)) {
578 if (MachineInstr *NewMI = tryReplaceInstr(&MI, MBB))
579 MIReplacements.push_back(std::make_pair(&MI, NewMI));
580
581 // We're done with this instruction, update liveness for the next one.
582 if (!MI.isDebugInstr())
583 LiveUnits.stepBackward(MI);
584 }
585
586 while (!MIReplacements.empty()) {
587 MachineInstr *MI = MIReplacements.back().first;
588 MachineInstr *NewMI = MIReplacements.back().second;
589 MIReplacements.pop_back();
590 MBB.insert(MI, NewMI);
591 MBB.erase(MI);
592 }
593
594 // Finally, clean up any extends made redundant by widening.
595 eliminateRedundantExtends(MBB);
596}
597
598bool X86FixupBWInstLegacy::runOnMachineFunction(MachineFunction &MF) {
599 if (skipFunction(MF.getFunction()))
600 return false;
601 ProfileSummaryInfo *PSI =
602 &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
603 MachineBlockFrequencyInfo *MBFI =
604 (PSI && PSI->hasProfileSummary())
605 ? &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI()
606 : nullptr;
607 X86FixupBWInstImpl Impl(PSI, MBFI);
608 return Impl.runOnMachineFunction(MF);
609}
610
611PreservedAnalyses
614 ProfileSummaryInfo *PSI =
616 .getCachedResult<ProfileSummaryAnalysis>(
617 *MF.getFunction().getParent());
618 MachineBlockFrequencyInfo *MBFI = nullptr;
619 if (PSI && PSI->hasProfileSummary())
621 X86FixupBWInstImpl Impl(PSI, MBFI);
622 bool Changed = Impl.runOnMachineFunction(MF);
626}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
===- LazyMachineBlockFrequencyInfo.h - Lazy Block Frequency -*- C++ -*–===//
A set of register units.
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
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define LLVM_DEBUG(...)
Definition Debug.h:119
#define FIXUPBW_DESC
#define FIXUPBW_NAME
static bool processBasicBlock(MachineBasicBlock &MBB, BlockStateMap &BlockStates, DirtySuccessorsWorkList &DirtySuccessors, bool IsX86INTR, const TargetInstrInfo *TII)
Loop over all of the instructions in the basic block, inserting vzeroupper instructions before functi...
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
bool test(unsigned Idx) const
Returns true if bit Idx is set.
Definition BitVector.h:483
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
Module * getParent()
Get the module that this global value is contained inside of...
const HexagonRegisterInfo & getRegisterInfo() const
This is an alternative analysis pass to MachineBlockFrequencyInfo.
A set of register units used to track register liveness.
const BitVector & getBitVector() const
Return the internal bitvector representation of the set.
void init(const TargetRegisterInfo &TRI)
Initialize and clear the set.
LLVM_ABI void stepBackward(const MachineInstr &MI)
Updates liveness when stepping backwards over the instruction MI.
LLVM_ABI void addLiveOuts(const MachineBasicBlock &MBB)
Adds registers living out of block MBB.
void clear()
Clears the set.
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
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.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & setMemRefs(ArrayRef< MachineMemOperand * > MMOs) const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
mop_range implicit_operands()
LLVM_ABI unsigned getDebugInstrNum()
Fetch the instruction number of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
Register getReg() const
getReg - Returns the register number.
const TargetRegisterInfo * getTargetRegisterInfo() const
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
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
Analysis providing profile information.
bool hasProfileSummary() const
Returns true if profile summary is available.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isValid() const
Definition Register.h:109
void push_back(const T &Elt)
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
This is an optimization pass for GlobalISel generic memory operations.
OuterAnalysisManagerProxy< ModuleAnalysisManager, MachineFunction > ModuleAnalysisManagerMachineFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
@ Known
Known to have no common set bits.
MCRegister getX86SubSuperRegister(MCRegister Reg, unsigned Size, bool High=false)
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:649
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
FunctionPass * createX86FixupBWInstsLegacyPass()
DWARFExpression::Operation Op