Path: blob/main/contrib/llvm-project/llvm/lib/Target/X86/X86FixupSetCC.cpp
35269 views
//===- X86FixupSetCC.cpp - fix zero-extension of setcc patterns -----------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines a pass that fixes zero-extension of setcc patterns.9// X86 setcc instructions are modeled to have no input arguments, and a single10// GR8 output argument. This is consistent with other similar instructions11// (e.g. movb), but means it is impossible to directly generate a setcc into12// the lower GR8 of a specified GR32.13// This means that ISel must select (zext (setcc)) into something like14// seta %al; movzbl %al, %eax.15// Unfortunately, this can cause a stall due to the partial register write16// performed by the setcc. Instead, we can use:17// xor %eax, %eax; seta %al18// This both avoids the stall, and encodes shorter.19//20// Furthurmore, we can use:21// setzua %al22// if feature zero-upper is available. It's faster than the xor+setcc sequence.23// When r16-r31 is used, it even encodes shorter.24//===----------------------------------------------------------------------===//2526#include "X86.h"27#include "X86InstrInfo.h"28#include "X86Subtarget.h"29#include "llvm/ADT/Statistic.h"30#include "llvm/CodeGen/MachineFunctionPass.h"31#include "llvm/CodeGen/MachineInstrBuilder.h"32#include "llvm/CodeGen/MachineRegisterInfo.h"3334using namespace llvm;3536#define DEBUG_TYPE "x86-fixup-setcc"3738STATISTIC(NumSubstZexts, "Number of setcc + zext pairs substituted");3940namespace {41class X86FixupSetCCPass : public MachineFunctionPass {42public:43static char ID;4445X86FixupSetCCPass() : MachineFunctionPass(ID) {}4647StringRef getPassName() const override { return "X86 Fixup SetCC"; }4849bool runOnMachineFunction(MachineFunction &MF) override;5051private:52MachineRegisterInfo *MRI = nullptr;53const X86Subtarget *ST = nullptr;54const X86InstrInfo *TII = nullptr;5556enum { SearchBound = 16 };57};58} // end anonymous namespace5960char X86FixupSetCCPass::ID = 0;6162INITIALIZE_PASS(X86FixupSetCCPass, DEBUG_TYPE, DEBUG_TYPE, false, false)6364FunctionPass *llvm::createX86FixupSetCC() { return new X86FixupSetCCPass(); }6566bool X86FixupSetCCPass::runOnMachineFunction(MachineFunction &MF) {67bool Changed = false;68MRI = &MF.getRegInfo();69ST = &MF.getSubtarget<X86Subtarget>();70TII = ST->getInstrInfo();7172SmallVector<MachineInstr*, 4> ToErase;7374for (auto &MBB : MF) {75MachineInstr *FlagsDefMI = nullptr;76for (auto &MI : MBB) {77// Remember the most recent preceding eflags defining instruction.78if (MI.definesRegister(X86::EFLAGS, /*TRI=*/nullptr))79FlagsDefMI = &MI;8081// Find a setcc that is used by a zext.82// This doesn't have to be the only use, the transformation is safe83// regardless.84if (MI.getOpcode() != X86::SETCCr)85continue;8687MachineInstr *ZExt = nullptr;88Register Reg0 = MI.getOperand(0).getReg();89for (auto &Use : MRI->use_instructions(Reg0))90if (Use.getOpcode() == X86::MOVZX32rr8)91ZExt = &Use;9293if (!ZExt)94continue;9596if (!FlagsDefMI)97continue;9899// We'd like to put something that clobbers eflags directly before100// FlagsDefMI. This can't hurt anything after FlagsDefMI, because101// it, itself, by definition, clobbers eflags. But it may happen that102// FlagsDefMI also *uses* eflags, in which case the transformation is103// invalid.104if (FlagsDefMI->readsRegister(X86::EFLAGS, /*TRI=*/nullptr))105continue;106107// On 32-bit, we need to be careful to force an ABCD register.108const TargetRegisterClass *RC =109ST->is64Bit() ? &X86::GR32RegClass : &X86::GR32_ABCDRegClass;110if (!MRI->constrainRegClass(ZExt->getOperand(0).getReg(), RC)) {111// If we cannot constrain the register, we would need an additional copy112// and are better off keeping the MOVZX32rr8 we have now.113continue;114}115116++NumSubstZexts;117Changed = true;118119// X86 setcc/setzucc only takes an output GR8, so fake a GR32 input by120// inserting the setcc/setzucc result into the low byte of the zeroed121// register.122Register ZeroReg = MRI->createVirtualRegister(RC);123if (ST->hasZU()) {124MI.setDesc(TII->get(X86::SETZUCCr));125BuildMI(*ZExt->getParent(), ZExt, ZExt->getDebugLoc(),126TII->get(TargetOpcode::IMPLICIT_DEF), ZeroReg);127} else {128// Initialize a register with 0. This must go before the eflags def129BuildMI(MBB, FlagsDefMI, MI.getDebugLoc(), TII->get(X86::MOV32r0),130ZeroReg);131}132133BuildMI(*ZExt->getParent(), ZExt, ZExt->getDebugLoc(),134TII->get(X86::INSERT_SUBREG), ZExt->getOperand(0).getReg())135.addReg(ZeroReg)136.addReg(Reg0)137.addImm(X86::sub_8bit);138ToErase.push_back(ZExt);139}140}141142for (auto &I : ToErase)143I->eraseFromParent();144145return Changed;146}147148149