13e3652aeSMichael Kuperstein //===---- X86FixupSetCC.cpp - optimize usage of LEA instructions ----------===//
23e3652aeSMichael Kuperstein //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63e3652aeSMichael Kuperstein //
73e3652aeSMichael Kuperstein //===----------------------------------------------------------------------===//
83e3652aeSMichael Kuperstein //
93e3652aeSMichael Kuperstein // This file defines a pass that fixes zero-extension of setcc patterns.
103e3652aeSMichael Kuperstein // X86 setcc instructions are modeled to have no input arguments, and a single
113e3652aeSMichael Kuperstein // GR8 output argument. This is consistent with other similar instructions
123e3652aeSMichael Kuperstein // (e.g. movb), but means it is impossible to directly generate a setcc into
133e3652aeSMichael Kuperstein // the lower GR8 of a specified GR32.
143e3652aeSMichael Kuperstein // This means that ISel must select (zext (setcc)) into something like
153e3652aeSMichael Kuperstein // seta %al; movzbl %al, %eax.
163e3652aeSMichael Kuperstein // Unfortunately, this can cause a stall due to the partial register write
173e3652aeSMichael Kuperstein // performed by the setcc. Instead, we can use:
183e3652aeSMichael Kuperstein // xor %eax, %eax; seta %al
193e3652aeSMichael Kuperstein // This both avoids the stall, and encodes shorter.
203e3652aeSMichael Kuperstein //===----------------------------------------------------------------------===//
213e3652aeSMichael Kuperstein
223e3652aeSMichael Kuperstein #include "X86.h"
233e3652aeSMichael Kuperstein #include "X86InstrInfo.h"
243e3652aeSMichael Kuperstein #include "X86Subtarget.h"
253e3652aeSMichael Kuperstein #include "llvm/ADT/Statistic.h"
263e3652aeSMichael Kuperstein #include "llvm/CodeGen/MachineFunctionPass.h"
273e3652aeSMichael Kuperstein #include "llvm/CodeGen/MachineInstrBuilder.h"
283e3652aeSMichael Kuperstein #include "llvm/CodeGen/MachineRegisterInfo.h"
293e3652aeSMichael Kuperstein
303e3652aeSMichael Kuperstein using namespace llvm;
313e3652aeSMichael Kuperstein
323e3652aeSMichael Kuperstein #define DEBUG_TYPE "x86-fixup-setcc"
333e3652aeSMichael Kuperstein
343e3652aeSMichael Kuperstein STATISTIC(NumSubstZexts, "Number of setcc + zext pairs substituted");
353e3652aeSMichael Kuperstein
363e3652aeSMichael Kuperstein namespace {
373e3652aeSMichael Kuperstein class X86FixupSetCCPass : public MachineFunctionPass {
383e3652aeSMichael Kuperstein public:
3952a6d47aSCraig Topper static char ID;
4052a6d47aSCraig Topper
X86FixupSetCCPass()413e3652aeSMichael Kuperstein X86FixupSetCCPass() : MachineFunctionPass(ID) {}
423e3652aeSMichael Kuperstein
getPassName() const43117296c0SMehdi Amini StringRef getPassName() const override { return "X86 Fixup SetCC"; }
443e3652aeSMichael Kuperstein
453e3652aeSMichael Kuperstein bool runOnMachineFunction(MachineFunction &MF) override;
463e3652aeSMichael Kuperstein
473e3652aeSMichael Kuperstein private:
48a8653da4SSimon Pilgrim MachineRegisterInfo *MRI = nullptr;
49a8653da4SSimon Pilgrim const X86InstrInfo *TII = nullptr;
503e3652aeSMichael Kuperstein
513e3652aeSMichael Kuperstein enum { SearchBound = 16 };
523e3652aeSMichael Kuperstein };
5352a6d47aSCraig Topper } // end anonymous namespace
543e3652aeSMichael Kuperstein
553e3652aeSMichael Kuperstein char X86FixupSetCCPass::ID = 0;
5652a6d47aSCraig Topper
INITIALIZE_PASS(X86FixupSetCCPass,DEBUG_TYPE,DEBUG_TYPE,false,false)5752a6d47aSCraig Topper INITIALIZE_PASS(X86FixupSetCCPass, DEBUG_TYPE, DEBUG_TYPE, false, false)
583e3652aeSMichael Kuperstein
593e3652aeSMichael Kuperstein FunctionPass *llvm::createX86FixupSetCC() { return new X86FixupSetCCPass(); }
603e3652aeSMichael Kuperstein
runOnMachineFunction(MachineFunction & MF)613e3652aeSMichael Kuperstein bool X86FixupSetCCPass::runOnMachineFunction(MachineFunction &MF) {
623e3652aeSMichael Kuperstein bool Changed = false;
633e3652aeSMichael Kuperstein MRI = &MF.getRegInfo();
643e3652aeSMichael Kuperstein TII = MF.getSubtarget<X86Subtarget>().getInstrInfo();
653e3652aeSMichael Kuperstein
663e3652aeSMichael Kuperstein SmallVector<MachineInstr*, 4> ToErase;
673e3652aeSMichael Kuperstein
683e3652aeSMichael Kuperstein for (auto &MBB : MF) {
694af5b23dSCraig Topper MachineInstr *FlagsDefMI = nullptr;
703e3652aeSMichael Kuperstein for (auto &MI : MBB) {
714af5b23dSCraig Topper // Remember the most recent preceding eflags defining instruction.
724af5b23dSCraig Topper if (MI.definesRegister(X86::EFLAGS))
734af5b23dSCraig Topper FlagsDefMI = &MI;
744af5b23dSCraig Topper
753e3652aeSMichael Kuperstein // Find a setcc that is used by a zext.
763e3652aeSMichael Kuperstein // This doesn't have to be the only use, the transformation is safe
773e3652aeSMichael Kuperstein // regardless.
787323c2bfSCraig Topper if (MI.getOpcode() != X86::SETCCr)
793e3652aeSMichael Kuperstein continue;
803e3652aeSMichael Kuperstein
813e3652aeSMichael Kuperstein MachineInstr *ZExt = nullptr;
823e3652aeSMichael Kuperstein for (auto &Use : MRI->use_instructions(MI.getOperand(0).getReg()))
833e3652aeSMichael Kuperstein if (Use.getOpcode() == X86::MOVZX32rr8)
843e3652aeSMichael Kuperstein ZExt = &Use;
853e3652aeSMichael Kuperstein
863e3652aeSMichael Kuperstein if (!ZExt)
873e3652aeSMichael Kuperstein continue;
883e3652aeSMichael Kuperstein
893e3652aeSMichael Kuperstein if (!FlagsDefMI)
903e3652aeSMichael Kuperstein continue;
913e3652aeSMichael Kuperstein
923e3652aeSMichael Kuperstein // We'd like to put something that clobbers eflags directly before
933e3652aeSMichael Kuperstein // FlagsDefMI. This can't hurt anything after FlagsDefMI, because
943e3652aeSMichael Kuperstein // it, itself, by definition, clobbers eflags. But it may happen that
953e3652aeSMichael Kuperstein // FlagsDefMI also *uses* eflags, in which case the transformation is
963e3652aeSMichael Kuperstein // invalid.
9727dc4c31SCraig Topper if (FlagsDefMI->readsRegister(X86::EFLAGS))
983e3652aeSMichael Kuperstein continue;
993e3652aeSMichael Kuperstein
1003e3652aeSMichael Kuperstein // On 32-bit, we need to be careful to force an ABCD register.
1013e3652aeSMichael Kuperstein const TargetRegisterClass *RC = MF.getSubtarget<X86Subtarget>().is64Bit()
1023e3652aeSMichael Kuperstein ? &X86::GR32RegClass
1033e3652aeSMichael Kuperstein : &X86::GR32_ABCDRegClass;
104*47e2fafbSHarald van Dijk if (!MRI->constrainRegClass(ZExt->getOperand(0).getReg(), RC)) {
105*47e2fafbSHarald van Dijk // If we cannot constrain the register, we would need an additional copy
106*47e2fafbSHarald van Dijk // and are better off keeping the MOVZX32rr8 we have now.
107*47e2fafbSHarald van Dijk continue;
108*47e2fafbSHarald van Dijk }
109*47e2fafbSHarald van Dijk
110*47e2fafbSHarald van Dijk ++NumSubstZexts;
111*47e2fafbSHarald van Dijk Changed = true;
1123e3652aeSMichael Kuperstein
1133e3652aeSMichael Kuperstein // Initialize a register with 0. This must go before the eflags def
114*47e2fafbSHarald van Dijk Register ZeroReg = MRI->createVirtualRegister(RC);
1153e3652aeSMichael Kuperstein BuildMI(MBB, FlagsDefMI, MI.getDebugLoc(), TII->get(X86::MOV32r0),
1163e3652aeSMichael Kuperstein ZeroReg);
1173e3652aeSMichael Kuperstein
1183e3652aeSMichael Kuperstein // X86 setcc only takes an output GR8, so fake a GR32 input by inserting
1193e3652aeSMichael Kuperstein // the setcc result into the low byte of the zeroed register.
1203e3652aeSMichael Kuperstein BuildMI(*ZExt->getParent(), ZExt, ZExt->getDebugLoc(),
121*47e2fafbSHarald van Dijk TII->get(X86::INSERT_SUBREG), ZExt->getOperand(0).getReg())
1223e3652aeSMichael Kuperstein .addReg(ZeroReg)
1233e3652aeSMichael Kuperstein .addReg(MI.getOperand(0).getReg())
1243e3652aeSMichael Kuperstein .addImm(X86::sub_8bit);
1253e3652aeSMichael Kuperstein ToErase.push_back(ZExt);
1263e3652aeSMichael Kuperstein }
1273e3652aeSMichael Kuperstein }
1283e3652aeSMichael Kuperstein
1293e3652aeSMichael Kuperstein for (auto &I : ToErase)
1303e3652aeSMichael Kuperstein I->eraseFromParent();
1313e3652aeSMichael Kuperstein
1323e3652aeSMichael Kuperstein return Changed;
1333e3652aeSMichael Kuperstein }
134