xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/X86/X86FixupSetCC.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1*0fca6ea1SDimitry Andric //===- X86FixupSetCC.cpp - fix zero-extension of setcc patterns -----------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file defines a pass that fixes zero-extension of setcc patterns.
100b57cec5SDimitry Andric // X86 setcc instructions are modeled to have no input arguments, and a single
110b57cec5SDimitry Andric // GR8 output argument. This is consistent with other similar instructions
120b57cec5SDimitry Andric // (e.g. movb), but means it is impossible to directly generate a setcc into
130b57cec5SDimitry Andric // the lower GR8 of a specified GR32.
140b57cec5SDimitry Andric // This means that ISel must select (zext (setcc)) into something like
150b57cec5SDimitry Andric // seta %al; movzbl %al, %eax.
160b57cec5SDimitry Andric // Unfortunately, this can cause a stall due to the partial register write
170b57cec5SDimitry Andric // performed by the setcc. Instead, we can use:
180b57cec5SDimitry Andric // xor %eax, %eax; seta %al
190b57cec5SDimitry Andric // This both avoids the stall, and encodes shorter.
20*0fca6ea1SDimitry Andric //
21*0fca6ea1SDimitry Andric // Furthurmore, we can use:
22*0fca6ea1SDimitry Andric // setzua %al
23*0fca6ea1SDimitry Andric // if feature zero-upper is available. It's faster than the xor+setcc sequence.
24*0fca6ea1SDimitry Andric // When r16-r31 is used, it even encodes shorter.
250b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric #include "X86.h"
280b57cec5SDimitry Andric #include "X86InstrInfo.h"
290b57cec5SDimitry Andric #include "X86Subtarget.h"
300b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric using namespace llvm;
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric #define DEBUG_TYPE "x86-fixup-setcc"
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric STATISTIC(NumSubstZexts, "Number of setcc + zext pairs substituted");
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric namespace {
420b57cec5SDimitry Andric class X86FixupSetCCPass : public MachineFunctionPass {
430b57cec5SDimitry Andric public:
445ffd83dbSDimitry Andric   static char ID;
455ffd83dbSDimitry Andric 
460b57cec5SDimitry Andric   X86FixupSetCCPass() : MachineFunctionPass(ID) {}
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric   StringRef getPassName() const override { return "X86 Fixup SetCC"; }
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric private:
53480093f4SDimitry Andric   MachineRegisterInfo *MRI = nullptr;
54*0fca6ea1SDimitry Andric   const X86Subtarget *ST = nullptr;
55480093f4SDimitry Andric   const X86InstrInfo *TII = nullptr;
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   enum { SearchBound = 16 };
580b57cec5SDimitry Andric };
595ffd83dbSDimitry Andric } // end anonymous namespace
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric char X86FixupSetCCPass::ID = 0;
625ffd83dbSDimitry Andric 
635ffd83dbSDimitry Andric INITIALIZE_PASS(X86FixupSetCCPass, DEBUG_TYPE, DEBUG_TYPE, false, false)
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric FunctionPass *llvm::createX86FixupSetCC() { return new X86FixupSetCCPass(); }
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric bool X86FixupSetCCPass::runOnMachineFunction(MachineFunction &MF) {
680b57cec5SDimitry Andric   bool Changed = false;
690b57cec5SDimitry Andric   MRI = &MF.getRegInfo();
70*0fca6ea1SDimitry Andric   ST = &MF.getSubtarget<X86Subtarget>();
71*0fca6ea1SDimitry Andric   TII = ST->getInstrInfo();
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   SmallVector<MachineInstr*, 4> ToErase;
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric   for (auto &MBB : MF) {
76480093f4SDimitry Andric     MachineInstr *FlagsDefMI = nullptr;
770b57cec5SDimitry Andric     for (auto &MI : MBB) {
78480093f4SDimitry Andric       // Remember the most recent preceding eflags defining instruction.
79*0fca6ea1SDimitry Andric       if (MI.definesRegister(X86::EFLAGS, /*TRI=*/nullptr))
80480093f4SDimitry Andric         FlagsDefMI = &MI;
81480093f4SDimitry Andric 
820b57cec5SDimitry Andric       // Find a setcc that is used by a zext.
830b57cec5SDimitry Andric       // This doesn't have to be the only use, the transformation is safe
840b57cec5SDimitry Andric       // regardless.
850b57cec5SDimitry Andric       if (MI.getOpcode() != X86::SETCCr)
860b57cec5SDimitry Andric         continue;
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric       MachineInstr *ZExt = nullptr;
89*0fca6ea1SDimitry Andric       Register Reg0 = MI.getOperand(0).getReg();
90*0fca6ea1SDimitry Andric       for (auto &Use : MRI->use_instructions(Reg0))
910b57cec5SDimitry Andric         if (Use.getOpcode() == X86::MOVZX32rr8)
920b57cec5SDimitry Andric           ZExt = &Use;
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric       if (!ZExt)
950b57cec5SDimitry Andric         continue;
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric       if (!FlagsDefMI)
980b57cec5SDimitry Andric         continue;
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric       // We'd like to put something that clobbers eflags directly before
1010b57cec5SDimitry Andric       // FlagsDefMI. This can't hurt anything after FlagsDefMI, because
1020b57cec5SDimitry Andric       // it, itself, by definition, clobbers eflags. But it may happen that
1030b57cec5SDimitry Andric       // FlagsDefMI also *uses* eflags, in which case the transformation is
1040b57cec5SDimitry Andric       // invalid.
105*0fca6ea1SDimitry Andric       if (FlagsDefMI->readsRegister(X86::EFLAGS, /*TRI=*/nullptr))
1060b57cec5SDimitry Andric         continue;
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric       // On 32-bit, we need to be careful to force an ABCD register.
109*0fca6ea1SDimitry Andric       const TargetRegisterClass *RC =
110*0fca6ea1SDimitry Andric           ST->is64Bit() ? &X86::GR32RegClass : &X86::GR32_ABCDRegClass;
111e8d8bef9SDimitry Andric       if (!MRI->constrainRegClass(ZExt->getOperand(0).getReg(), RC)) {
112e8d8bef9SDimitry Andric         // If we cannot constrain the register, we would need an additional copy
113e8d8bef9SDimitry Andric         // and are better off keeping the MOVZX32rr8 we have now.
114e8d8bef9SDimitry Andric         continue;
115e8d8bef9SDimitry Andric       }
116e8d8bef9SDimitry Andric 
117e8d8bef9SDimitry Andric       ++NumSubstZexts;
118e8d8bef9SDimitry Andric       Changed = true;
1190b57cec5SDimitry Andric 
120*0fca6ea1SDimitry Andric       // X86 setcc/setzucc only takes an output GR8, so fake a GR32 input by
121*0fca6ea1SDimitry Andric       // inserting the setcc/setzucc result into the low byte of the zeroed
122*0fca6ea1SDimitry Andric       // register.
123e8d8bef9SDimitry Andric       Register ZeroReg = MRI->createVirtualRegister(RC);
124*0fca6ea1SDimitry Andric       if (ST->hasZU()) {
125*0fca6ea1SDimitry Andric         MI.setDesc(TII->get(X86::SETZUCCr));
126*0fca6ea1SDimitry Andric         BuildMI(*ZExt->getParent(), ZExt, ZExt->getDebugLoc(),
127*0fca6ea1SDimitry Andric                 TII->get(TargetOpcode::IMPLICIT_DEF), ZeroReg);
128*0fca6ea1SDimitry Andric       } else {
129*0fca6ea1SDimitry Andric         // Initialize a register with 0. This must go before the eflags def
1300b57cec5SDimitry Andric         BuildMI(MBB, FlagsDefMI, MI.getDebugLoc(), TII->get(X86::MOV32r0),
1310b57cec5SDimitry Andric                 ZeroReg);
132*0fca6ea1SDimitry Andric       }
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric       BuildMI(*ZExt->getParent(), ZExt, ZExt->getDebugLoc(),
135e8d8bef9SDimitry Andric               TII->get(X86::INSERT_SUBREG), ZExt->getOperand(0).getReg())
1360b57cec5SDimitry Andric           .addReg(ZeroReg)
137*0fca6ea1SDimitry Andric           .addReg(Reg0)
1380b57cec5SDimitry Andric           .addImm(X86::sub_8bit);
1390b57cec5SDimitry Andric       ToErase.push_back(ZExt);
1400b57cec5SDimitry Andric     }
1410b57cec5SDimitry Andric   }
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric   for (auto &I : ToErase)
1440b57cec5SDimitry Andric     I->eraseFromParent();
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric   return Changed;
1470b57cec5SDimitry Andric }
148