X86FixupSetCC.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //===---- X86FixupSetCC.cpp - optimize usage of LEA 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 defines a pass that fixes zero-extension of setcc patterns.
  10. // X86 setcc instructions are modeled to have no input arguments, and a single
  11. // GR8 output argument. This is consistent with other similar instructions
  12. // (e.g. movb), but means it is impossible to directly generate a setcc into
  13. // the lower GR8 of a specified GR32.
  14. // This means that ISel must select (zext (setcc)) into something like
  15. // seta %al; movzbl %al, %eax.
  16. // Unfortunately, this can cause a stall due to the partial register write
  17. // performed by the setcc. Instead, we can use:
  18. // xor %eax, %eax; seta %al
  19. // This both avoids the stall, and encodes shorter.
  20. //===----------------------------------------------------------------------===//
  21. #include "X86.h"
  22. #include "X86InstrInfo.h"
  23. #include "X86Subtarget.h"
  24. #include "llvm/ADT/Statistic.h"
  25. #include "llvm/CodeGen/MachineFunctionPass.h"
  26. #include "llvm/CodeGen/MachineInstrBuilder.h"
  27. #include "llvm/CodeGen/MachineRegisterInfo.h"
  28. using namespace llvm;
  29. #define DEBUG_TYPE "x86-fixup-setcc"
  30. STATISTIC(NumSubstZexts, "Number of setcc + zext pairs substituted");
  31. namespace {
  32. class X86FixupSetCCPass : public MachineFunctionPass {
  33. public:
  34. static char ID;
  35. X86FixupSetCCPass() : MachineFunctionPass(ID) {}
  36. StringRef getPassName() const override { return "X86 Fixup SetCC"; }
  37. bool runOnMachineFunction(MachineFunction &MF) override;
  38. private:
  39. MachineRegisterInfo *MRI = nullptr;
  40. const X86InstrInfo *TII = nullptr;
  41. enum { SearchBound = 16 };
  42. };
  43. } // end anonymous namespace
  44. char X86FixupSetCCPass::ID = 0;
  45. INITIALIZE_PASS(X86FixupSetCCPass, DEBUG_TYPE, DEBUG_TYPE, false, false)
  46. FunctionPass *llvm::createX86FixupSetCC() { return new X86FixupSetCCPass(); }
  47. bool X86FixupSetCCPass::runOnMachineFunction(MachineFunction &MF) {
  48. bool Changed = false;
  49. MRI = &MF.getRegInfo();
  50. TII = MF.getSubtarget<X86Subtarget>().getInstrInfo();
  51. SmallVector<MachineInstr*, 4> ToErase;
  52. for (auto &MBB : MF) {
  53. MachineInstr *FlagsDefMI = nullptr;
  54. for (auto &MI : MBB) {
  55. // Remember the most recent preceding eflags defining instruction.
  56. if (MI.definesRegister(X86::EFLAGS))
  57. FlagsDefMI = &MI;
  58. // Find a setcc that is used by a zext.
  59. // This doesn't have to be the only use, the transformation is safe
  60. // regardless.
  61. if (MI.getOpcode() != X86::SETCCr)
  62. continue;
  63. MachineInstr *ZExt = nullptr;
  64. for (auto &Use : MRI->use_instructions(MI.getOperand(0).getReg()))
  65. if (Use.getOpcode() == X86::MOVZX32rr8)
  66. ZExt = &Use;
  67. if (!ZExt)
  68. continue;
  69. if (!FlagsDefMI)
  70. continue;
  71. // We'd like to put something that clobbers eflags directly before
  72. // FlagsDefMI. This can't hurt anything after FlagsDefMI, because
  73. // it, itself, by definition, clobbers eflags. But it may happen that
  74. // FlagsDefMI also *uses* eflags, in which case the transformation is
  75. // invalid.
  76. if (FlagsDefMI->readsRegister(X86::EFLAGS))
  77. continue;
  78. // On 32-bit, we need to be careful to force an ABCD register.
  79. const TargetRegisterClass *RC = MF.getSubtarget<X86Subtarget>().is64Bit()
  80. ? &X86::GR32RegClass
  81. : &X86::GR32_ABCDRegClass;
  82. if (!MRI->constrainRegClass(ZExt->getOperand(0).getReg(), RC)) {
  83. // If we cannot constrain the register, we would need an additional copy
  84. // and are better off keeping the MOVZX32rr8 we have now.
  85. continue;
  86. }
  87. ++NumSubstZexts;
  88. Changed = true;
  89. // Initialize a register with 0. This must go before the eflags def
  90. Register ZeroReg = MRI->createVirtualRegister(RC);
  91. BuildMI(MBB, FlagsDefMI, MI.getDebugLoc(), TII->get(X86::MOV32r0),
  92. ZeroReg);
  93. // X86 setcc only takes an output GR8, so fake a GR32 input by inserting
  94. // the setcc result into the low byte of the zeroed register.
  95. BuildMI(*ZExt->getParent(), ZExt, ZExt->getDebugLoc(),
  96. TII->get(X86::INSERT_SUBREG), ZExt->getOperand(0).getReg())
  97. .addReg(ZeroReg)
  98. .addReg(MI.getOperand(0).getReg())
  99. .addImm(X86::sub_8bit);
  100. ToErase.push_back(ZExt);
  101. }
  102. }
  103. for (auto &I : ToErase)
  104. I->eraseFromParent();
  105. return Changed;
  106. }