2013-04-26 04:29:37 +08:00
|
|
|
//===-- X86FixupLEAs.cpp - use or replace LEA instructions -----------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-04-26 04:29:37 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2014-07-17 04:18:49 +08:00
|
|
|
// This file defines the pass that finds instructions that can be
|
|
|
|
// re-written as LEA instructions in order to reduce pipeline delays.
|
2019-05-25 14:17:47 +08:00
|
|
|
// It replaces LEAs with ADD/INC/DEC when that is better for size/speed.
|
2013-04-26 04:29:37 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "X86.h"
|
|
|
|
#include "X86InstrInfo.h"
|
|
|
|
#include "X86Subtarget.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2020-07-08 01:19:54 +08:00
|
|
|
#include "llvm/Analysis/ProfileSummaryInfo.h"
|
|
|
|
#include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
|
2013-04-26 04:29:37 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2020-07-08 01:19:54 +08:00
|
|
|
#include "llvm/CodeGen/MachineSizeOpts.h"
|
2013-04-26 04:29:37 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2018-04-13 23:09:39 +08:00
|
|
|
#include "llvm/CodeGen/TargetSchedule.h"
|
2013-04-26 04:29:37 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2017-05-18 16:11:50 +08:00
|
|
|
#define FIXUPLEA_DESC "X86 LEA Fixup"
|
|
|
|
#define FIXUPLEA_NAME "x86-fixup-LEAs"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE FIXUPLEA_NAME
|
2014-04-22 10:41:26 +08:00
|
|
|
|
2013-04-26 04:29:37 +08:00
|
|
|
STATISTIC(NumLEAs, "Number of LEA instructions created");
|
|
|
|
|
|
|
|
namespace {
|
2014-06-04 05:01:35 +08:00
|
|
|
class FixupLEAPass : public MachineFunctionPass {
|
|
|
|
enum RegUsageState { RU_NotUsed, RU_Write, RU_Read };
|
2017-05-18 16:11:50 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Given a machine register, look for the instruction
|
2014-06-04 05:01:35 +08:00
|
|
|
/// which writes it in the current basic block. If found,
|
|
|
|
/// try to replace it with an equivalent LEA instruction.
|
2015-06-19 09:53:21 +08:00
|
|
|
/// If replacement succeeds, then also process the newly created
|
2014-06-04 05:01:35 +08:00
|
|
|
/// instruction.
|
|
|
|
void seekLEAFixup(MachineOperand &p, MachineBasicBlock::iterator &I,
|
2019-05-01 01:56:28 +08:00
|
|
|
MachineBasicBlock &MBB);
|
2013-04-26 05:31:33 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Given a memory access or LEA instruction
|
2014-06-04 05:01:35 +08:00
|
|
|
/// whose address mode uses a base and/or index register, look for
|
|
|
|
/// an opportunity to replace the instruction which sets the base or index
|
|
|
|
/// register with an equivalent LEA instruction.
|
|
|
|
void processInstruction(MachineBasicBlock::iterator &I,
|
2019-05-01 01:56:28 +08:00
|
|
|
MachineBasicBlock &MBB);
|
2013-04-26 05:31:33 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Given a LEA instruction which is unprofitable
|
2018-11-01 22:57:07 +08:00
|
|
|
/// on SlowLEA targets try to replace it with an equivalent ADD instruction.
|
|
|
|
void processInstructionForSlowLEA(MachineBasicBlock::iterator &I,
|
2019-05-01 01:56:28 +08:00
|
|
|
MachineBasicBlock &MBB);
|
2017-05-18 16:11:50 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Given a LEA instruction which is unprofitable
|
2017-05-18 16:11:50 +08:00
|
|
|
/// on SNB+ try to replace it with other instructions.
|
|
|
|
/// According to Intel's Optimization Reference Manual:
|
|
|
|
/// " For LEA instructions with three source operands and some specific
|
|
|
|
/// situations, instruction latency has increased to 3 cycles, and must
|
|
|
|
/// dispatch via port 1:
|
|
|
|
/// - LEA that has all three source operands: base, index, and offset
|
|
|
|
/// - LEA that uses base and index registers where the base is EBP, RBP,
|
|
|
|
/// or R13
|
|
|
|
/// - LEA that uses RIP relative addressing mode
|
|
|
|
/// - LEA that uses 16-bit addressing mode "
|
|
|
|
/// This function currently handles the first 2 cases only.
|
2019-10-07 14:27:55 +08:00
|
|
|
void processInstrForSlow3OpLEA(MachineBasicBlock::iterator &I,
|
|
|
|
MachineBasicBlock &MBB, bool OptIncDec);
|
2017-05-18 16:11:50 +08:00
|
|
|
|
2019-05-25 14:17:47 +08:00
|
|
|
/// Look for LEAs that are really two address LEAs that we might be able to
|
|
|
|
/// turn into regular ADD instructions.
|
|
|
|
bool optTwoAddrLEA(MachineBasicBlock::iterator &I,
|
|
|
|
MachineBasicBlock &MBB, bool OptIncDec,
|
|
|
|
bool UseLEAForSP) const;
|
2015-11-11 19:44:31 +08:00
|
|
|
|
2021-07-17 01:16:03 +08:00
|
|
|
/// Look for and transform the sequence
|
|
|
|
/// lea (reg1, reg2), reg3
|
|
|
|
/// sub reg3, reg4
|
|
|
|
/// to
|
|
|
|
/// sub reg1, reg4
|
|
|
|
/// sub reg2, reg4
|
|
|
|
/// It can also optimize the sequence lea/add similarly.
|
|
|
|
bool optLEAALU(MachineBasicBlock::iterator &I, MachineBasicBlock &MBB) const;
|
|
|
|
|
|
|
|
/// Step forwards in MBB, looking for an ADD/SUB instruction which uses
|
|
|
|
/// the dest register of LEA instruction I.
|
|
|
|
MachineBasicBlock::iterator searchALUInst(MachineBasicBlock::iterator &I,
|
|
|
|
MachineBasicBlock &MBB) const;
|
|
|
|
|
|
|
|
/// Check instructions between LeaI and AluI (exclusively).
|
|
|
|
/// Set BaseIndexDef to true if base or index register from LeaI is defined.
|
|
|
|
/// Set AluDestRef to true if the dest register of AluI is used or defined.
|
|
|
|
/// *KilledBase is set to the killed base register usage.
|
|
|
|
/// *KilledIndex is set to the killed index register usage.
|
|
|
|
void checkRegUsage(MachineBasicBlock::iterator &LeaI,
|
|
|
|
MachineBasicBlock::iterator &AluI, bool &BaseIndexDef,
|
|
|
|
bool &AluDestRef, MachineOperand **KilledBase,
|
|
|
|
MachineOperand **KilledIndex) const;
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Determine if an instruction references a machine register
|
2014-06-04 05:01:35 +08:00
|
|
|
/// and, if so, whether it reads or writes the register.
|
|
|
|
RegUsageState usesRegister(MachineOperand &p, MachineBasicBlock::iterator I);
|
2013-04-26 05:31:33 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Step backwards through a basic block, looking
|
2014-06-04 05:01:35 +08:00
|
|
|
/// for an instruction which writes a register within
|
|
|
|
/// a maximum of INSTR_DISTANCE_THRESHOLD instruction latency cycles.
|
|
|
|
MachineBasicBlock::iterator searchBackwards(MachineOperand &p,
|
|
|
|
MachineBasicBlock::iterator &I,
|
2019-05-01 01:56:28 +08:00
|
|
|
MachineBasicBlock &MBB);
|
2013-04-26 05:31:33 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// if an instruction can be converted to an
|
2014-06-04 05:01:35 +08:00
|
|
|
/// equivalent LEA, insert the new instruction into the basic block
|
|
|
|
/// and return a pointer to it. Otherwise, return zero.
|
2019-05-01 01:56:28 +08:00
|
|
|
MachineInstr *postRAConvertToLEA(MachineBasicBlock &MBB,
|
2014-06-04 05:01:35 +08:00
|
|
|
MachineBasicBlock::iterator &MBBI) const;
|
2013-04-26 04:29:37 +08:00
|
|
|
|
2014-06-04 05:01:35 +08:00
|
|
|
public:
|
2017-05-18 16:11:50 +08:00
|
|
|
static char ID;
|
|
|
|
|
|
|
|
StringRef getPassName() const override { return FIXUPLEA_DESC; }
|
|
|
|
|
2019-06-13 10:09:32 +08:00
|
|
|
FixupLEAPass() : MachineFunctionPass(ID) { }
|
2013-04-26 04:29:37 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Loop over all of the basic blocks,
|
2014-06-04 05:01:35 +08:00
|
|
|
/// replacing instructions by equivalent LEA instructions
|
|
|
|
/// if needed and when possible.
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2013-04-26 04:29:37 +08:00
|
|
|
|
2016-04-05 01:09:25 +08:00
|
|
|
// This pass runs after regalloc and doesn't support VReg operands.
|
|
|
|
MachineFunctionProperties getRequiredProperties() const override {
|
|
|
|
return MachineFunctionProperties().set(
|
2016-08-25 09:27:13 +08:00
|
|
|
MachineFunctionProperties::Property::NoVRegs);
|
2016-04-05 01:09:25 +08:00
|
|
|
}
|
|
|
|
|
2020-07-08 01:19:54 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.addRequired<ProfileSummaryInfoWrapperPass>();
|
|
|
|
AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2014-06-04 05:01:35 +08:00
|
|
|
private:
|
2018-04-13 23:09:39 +08:00
|
|
|
TargetSchedModel TSM;
|
2019-11-05 01:24:18 +08:00
|
|
|
const X86InstrInfo *TII = nullptr;
|
|
|
|
const X86RegisterInfo *TRI = nullptr;
|
2014-06-04 05:01:35 +08:00
|
|
|
};
|
2017-05-17 03:55:03 +08:00
|
|
|
}
|
2017-05-17 00:01:36 +08:00
|
|
|
|
2017-05-18 16:11:50 +08:00
|
|
|
char FixupLEAPass::ID = 0;
|
|
|
|
|
|
|
|
INITIALIZE_PASS(FixupLEAPass, FIXUPLEA_NAME, FIXUPLEA_DESC, false, false)
|
|
|
|
|
2013-04-26 04:29:37 +08:00
|
|
|
MachineInstr *
|
2019-05-01 01:56:28 +08:00
|
|
|
FixupLEAPass::postRAConvertToLEA(MachineBasicBlock &MBB,
|
2013-04-26 05:31:33 +08:00
|
|
|
MachineBasicBlock::iterator &MBBI) const {
|
2016-06-30 08:01:54 +08:00
|
|
|
MachineInstr &MI = *MBBI;
|
|
|
|
switch (MI.getOpcode()) {
|
2014-05-20 16:55:50 +08:00
|
|
|
case X86::MOV32rr:
|
2013-04-26 04:29:37 +08:00
|
|
|
case X86::MOV64rr: {
|
2016-06-30 08:01:54 +08:00
|
|
|
const MachineOperand &Src = MI.getOperand(1);
|
|
|
|
const MachineOperand &Dest = MI.getOperand(0);
|
|
|
|
MachineInstr *NewMI =
|
2019-05-01 01:56:28 +08:00
|
|
|
BuildMI(MBB, MBBI, MI.getDebugLoc(),
|
2016-06-30 08:01:54 +08:00
|
|
|
TII->get(MI.getOpcode() == X86::MOV32rr ? X86::LEA32r
|
|
|
|
: X86::LEA64r))
|
2017-01-13 17:58:52 +08:00
|
|
|
.add(Dest)
|
|
|
|
.add(Src)
|
2016-06-30 08:01:54 +08:00
|
|
|
.addImm(1)
|
|
|
|
.addReg(0)
|
|
|
|
.addImm(0)
|
|
|
|
.addReg(0);
|
2013-04-26 04:29:37 +08:00
|
|
|
return NewMI;
|
|
|
|
}
|
2019-04-03 04:52:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!MI.isConvertibleTo3Addr())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
switch (MI.getOpcode()) {
|
2019-07-11 09:01:39 +08:00
|
|
|
default:
|
|
|
|
// Only convert instructions that we've verified are safe.
|
|
|
|
return nullptr;
|
2013-04-26 04:29:37 +08:00
|
|
|
case X86::ADD64ri32:
|
|
|
|
case X86::ADD64ri8:
|
|
|
|
case X86::ADD64ri32_DB:
|
|
|
|
case X86::ADD64ri8_DB:
|
|
|
|
case X86::ADD32ri:
|
|
|
|
case X86::ADD32ri8:
|
|
|
|
case X86::ADD32ri_DB:
|
|
|
|
case X86::ADD32ri8_DB:
|
2016-06-30 08:01:54 +08:00
|
|
|
if (!MI.getOperand(2).isImm()) {
|
2013-04-26 04:29:37 +08:00
|
|
|
// convertToThreeAddress will call getImm()
|
|
|
|
// which requires isImm() to be true
|
2014-04-25 13:30:21 +08:00
|
|
|
return nullptr;
|
2013-04-26 04:29:37 +08:00
|
|
|
}
|
2013-10-01 07:51:22 +08:00
|
|
|
break;
|
2019-07-11 09:01:39 +08:00
|
|
|
case X86::SHL64ri:
|
|
|
|
case X86::SHL32ri:
|
|
|
|
case X86::INC64r:
|
|
|
|
case X86::INC32r:
|
|
|
|
case X86::DEC64r:
|
|
|
|
case X86::DEC32r:
|
|
|
|
case X86::ADD64rr:
|
|
|
|
case X86::ADD64rr_DB:
|
|
|
|
case X86::ADD32rr:
|
|
|
|
case X86::ADD32rr_DB:
|
|
|
|
// These instructions are all fine to convert.
|
|
|
|
break;
|
2013-04-26 04:29:37 +08:00
|
|
|
}
|
2019-05-01 01:56:28 +08:00
|
|
|
MachineFunction::iterator MFI = MBB.getIterator();
|
2016-06-30 08:01:54 +08:00
|
|
|
return TII->convertToThreeAddress(MFI, MI, nullptr);
|
2013-04-26 04:29:37 +08:00
|
|
|
}
|
|
|
|
|
2014-06-04 05:01:35 +08:00
|
|
|
FunctionPass *llvm::createX86FixupLEAs() { return new FixupLEAPass(); }
|
2013-04-26 04:29:37 +08:00
|
|
|
|
2019-05-01 14:53:03 +08:00
|
|
|
static bool isLEA(unsigned Opcode) {
|
2019-05-03 06:46:23 +08:00
|
|
|
return Opcode == X86::LEA32r || Opcode == X86::LEA64r ||
|
|
|
|
Opcode == X86::LEA64_32r;
|
2019-05-01 14:53:03 +08:00
|
|
|
}
|
|
|
|
|
2019-05-01 01:56:28 +08:00
|
|
|
bool FixupLEAPass::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
if (skipFunction(MF.getFunction()))
|
2016-04-27 05:44:24 +08:00
|
|
|
return false;
|
|
|
|
|
2019-05-01 01:56:28 +08:00
|
|
|
const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
|
2018-11-07 20:26:00 +08:00
|
|
|
bool IsSlowLEA = ST.slowLEA();
|
|
|
|
bool IsSlow3OpsLEA = ST.slow3OpsLEA();
|
2019-05-01 01:56:28 +08:00
|
|
|
bool LEAUsesAG = ST.LEAusesAG();
|
2018-11-07 20:26:00 +08:00
|
|
|
|
2019-05-01 01:56:28 +08:00
|
|
|
bool OptIncDec = !ST.slowIncDec() || MF.getFunction().hasOptSize();
|
2019-05-25 14:17:47 +08:00
|
|
|
bool UseLEAForSP = ST.useLeaForSP();
|
2014-05-22 09:46:02 +08:00
|
|
|
|
2019-05-01 01:56:28 +08:00
|
|
|
TSM.init(&ST);
|
2015-02-06 03:27:01 +08:00
|
|
|
TII = ST.getInstrInfo();
|
2019-05-25 14:17:47 +08:00
|
|
|
TRI = ST.getRegisterInfo();
|
2020-07-08 01:19:54 +08:00
|
|
|
auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
|
|
|
|
auto *MBFI = (PSI && PSI->hasProfileSummary())
|
|
|
|
? &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI()
|
|
|
|
: nullptr;
|
2013-04-26 04:29:37 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Start X86FixupLEAs\n";);
|
2019-05-01 01:56:28 +08:00
|
|
|
for (MachineBasicBlock &MBB : MF) {
|
|
|
|
// First pass. Try to remove or optimize existing LEAs.
|
2020-07-08 01:19:54 +08:00
|
|
|
bool OptIncDecPerBB =
|
|
|
|
OptIncDec || llvm::shouldOptimizeForSize(&MBB, PSI, MBFI);
|
2019-05-01 01:56:28 +08:00
|
|
|
for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
|
2019-05-01 14:53:03 +08:00
|
|
|
if (!isLEA(I->getOpcode()))
|
|
|
|
continue;
|
|
|
|
|
2020-07-08 01:19:54 +08:00
|
|
|
if (optTwoAddrLEA(I, MBB, OptIncDecPerBB, UseLEAForSP))
|
2019-05-01 01:56:28 +08:00
|
|
|
continue;
|
|
|
|
|
2019-10-07 14:27:55 +08:00
|
|
|
if (IsSlowLEA)
|
2019-05-01 01:56:28 +08:00
|
|
|
processInstructionForSlowLEA(I, MBB);
|
2019-10-07 14:27:55 +08:00
|
|
|
else if (IsSlow3OpsLEA)
|
2020-07-08 01:19:54 +08:00
|
|
|
processInstrForSlow3OpLEA(I, MBB, OptIncDecPerBB);
|
2019-05-01 01:56:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Second pass for creating LEAs. This may reverse some of the
|
|
|
|
// transformations above.
|
|
|
|
if (LEAUsesAG) {
|
|
|
|
for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I)
|
|
|
|
processInstruction(I, MBB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "End X86FixupLEAs\n";);
|
2013-04-26 04:29:37 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-04 05:01:35 +08:00
|
|
|
FixupLEAPass::RegUsageState
|
|
|
|
FixupLEAPass::usesRegister(MachineOperand &p, MachineBasicBlock::iterator I) {
|
2013-04-26 04:29:37 +08:00
|
|
|
RegUsageState RegUsage = RU_NotUsed;
|
2016-07-12 11:18:50 +08:00
|
|
|
MachineInstr &MI = *I;
|
2013-04-26 04:29:37 +08:00
|
|
|
|
2019-04-02 08:50:58 +08:00
|
|
|
for (unsigned i = 0; i < MI.getNumOperands(); ++i) {
|
2016-07-12 11:18:50 +08:00
|
|
|
MachineOperand &opnd = MI.getOperand(i);
|
2014-06-04 05:01:35 +08:00
|
|
|
if (opnd.isReg() && opnd.getReg() == p.getReg()) {
|
2013-04-26 04:29:37 +08:00
|
|
|
if (opnd.isDef())
|
|
|
|
return RU_Write;
|
|
|
|
RegUsage = RU_Read;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return RegUsage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getPreviousInstr - Given a reference to an instruction in a basic
|
|
|
|
/// block, return a reference to the previous instruction in the block,
|
|
|
|
/// wrapping around to the last instruction of the block if the block
|
|
|
|
/// branches to itself.
|
2014-06-04 05:01:35 +08:00
|
|
|
static inline bool getPreviousInstr(MachineBasicBlock::iterator &I,
|
2019-05-01 01:56:28 +08:00
|
|
|
MachineBasicBlock &MBB) {
|
|
|
|
if (I == MBB.begin()) {
|
|
|
|
if (MBB.isPredecessor(&MBB)) {
|
|
|
|
I = --MBB.end();
|
2013-04-26 04:29:37 +08:00
|
|
|
return true;
|
2014-06-04 05:01:35 +08:00
|
|
|
} else
|
2013-04-26 04:29:37 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
--I;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-04 05:01:35 +08:00
|
|
|
MachineBasicBlock::iterator
|
|
|
|
FixupLEAPass::searchBackwards(MachineOperand &p, MachineBasicBlock::iterator &I,
|
2019-05-01 01:56:28 +08:00
|
|
|
MachineBasicBlock &MBB) {
|
2013-04-26 04:29:37 +08:00
|
|
|
int InstrDistance = 1;
|
|
|
|
MachineBasicBlock::iterator CurInst;
|
|
|
|
static const int INSTR_DISTANCE_THRESHOLD = 5;
|
|
|
|
|
|
|
|
CurInst = I;
|
|
|
|
bool Found;
|
2019-05-01 01:56:28 +08:00
|
|
|
Found = getPreviousInstr(CurInst, MBB);
|
2014-06-04 05:01:35 +08:00
|
|
|
while (Found && I != CurInst) {
|
2013-04-26 04:29:37 +08:00
|
|
|
if (CurInst->isCall() || CurInst->isInlineAsm())
|
|
|
|
break;
|
|
|
|
if (InstrDistance > INSTR_DISTANCE_THRESHOLD)
|
|
|
|
break; // too far back to make a difference
|
2014-06-04 05:01:35 +08:00
|
|
|
if (usesRegister(p, CurInst) == RU_Write) {
|
2013-04-26 04:29:37 +08:00
|
|
|
return CurInst;
|
|
|
|
}
|
2018-04-13 23:09:39 +08:00
|
|
|
InstrDistance += TSM.computeInstrLatency(&*CurInst);
|
2019-05-01 01:56:28 +08:00
|
|
|
Found = getPreviousInstr(CurInst, MBB);
|
2013-04-26 04:29:37 +08:00
|
|
|
}
|
2016-07-12 11:18:50 +08:00
|
|
|
return MachineBasicBlock::iterator();
|
2013-04-26 04:29:37 +08:00
|
|
|
}
|
|
|
|
|
2019-04-02 08:50:58 +08:00
|
|
|
static inline bool isInefficientLEAReg(unsigned Reg) {
|
2018-08-03 11:45:19 +08:00
|
|
|
return Reg == X86::EBP || Reg == X86::RBP ||
|
|
|
|
Reg == X86::R13D || Reg == X86::R13;
|
2017-05-18 16:11:50 +08:00
|
|
|
}
|
|
|
|
|
2018-11-07 20:26:00 +08:00
|
|
|
/// Returns true if this LEA uses base an index registers, and the base register
|
|
|
|
/// is known to be inefficient for the subtarget.
|
2018-07-20 00:42:15 +08:00
|
|
|
// TODO: use a variant scheduling class to model the latency profile
|
|
|
|
// of LEA instructions, and implement this logic as a scheduling predicate.
|
2017-05-18 16:11:50 +08:00
|
|
|
static inline bool hasInefficientLEABaseReg(const MachineOperand &Base,
|
|
|
|
const MachineOperand &Index) {
|
2019-10-07 14:27:55 +08:00
|
|
|
return Base.isReg() && isInefficientLEAReg(Base.getReg()) && Index.isReg() &&
|
|
|
|
Index.getReg() != X86::NoRegister;
|
2017-05-18 16:11:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool hasLEAOffset(const MachineOperand &Offset) {
|
|
|
|
return (Offset.isImm() && Offset.getImm() != 0) || Offset.isGlobal();
|
|
|
|
}
|
|
|
|
|
2019-04-02 08:50:58 +08:00
|
|
|
static inline unsigned getADDrrFromLEA(unsigned LEAOpcode) {
|
2017-05-18 16:11:50 +08:00
|
|
|
switch (LEAOpcode) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unexpected LEA instruction");
|
|
|
|
case X86::LEA32r:
|
|
|
|
case X86::LEA64_32r:
|
2019-05-25 14:17:47 +08:00
|
|
|
return X86::ADD32rr;
|
2017-05-18 16:11:50 +08:00
|
|
|
case X86::LEA64r:
|
|
|
|
return X86::ADD64rr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 01:16:03 +08:00
|
|
|
static inline unsigned getSUBrrFromLEA(unsigned LEAOpcode) {
|
|
|
|
switch (LEAOpcode) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unexpected LEA instruction");
|
|
|
|
case X86::LEA32r:
|
|
|
|
case X86::LEA64_32r:
|
|
|
|
return X86::SUB32rr;
|
|
|
|
case X86::LEA64r:
|
|
|
|
return X86::SUB64rr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 08:50:58 +08:00
|
|
|
static inline unsigned getADDriFromLEA(unsigned LEAOpcode,
|
|
|
|
const MachineOperand &Offset) {
|
2017-05-18 16:11:50 +08:00
|
|
|
bool IsInt8 = Offset.isImm() && isInt<8>(Offset.getImm());
|
|
|
|
switch (LEAOpcode) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unexpected LEA instruction");
|
|
|
|
case X86::LEA32r:
|
|
|
|
case X86::LEA64_32r:
|
|
|
|
return IsInt8 ? X86::ADD32ri8 : X86::ADD32ri;
|
|
|
|
case X86::LEA64r:
|
|
|
|
return IsInt8 ? X86::ADD64ri8 : X86::ADD64ri32;
|
|
|
|
}
|
2015-11-11 19:44:31 +08:00
|
|
|
}
|
|
|
|
|
2019-05-25 14:17:47 +08:00
|
|
|
static inline unsigned getINCDECFromLEA(unsigned LEAOpcode, bool IsINC) {
|
|
|
|
switch (LEAOpcode) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unexpected LEA instruction");
|
|
|
|
case X86::LEA32r:
|
|
|
|
case X86::LEA64_32r:
|
|
|
|
return IsINC ? X86::INC32r : X86::DEC32r;
|
|
|
|
case X86::LEA64r:
|
|
|
|
return IsINC ? X86::INC64r : X86::DEC64r;
|
|
|
|
}
|
2015-11-11 19:44:31 +08:00
|
|
|
}
|
|
|
|
|
2021-07-17 01:16:03 +08:00
|
|
|
MachineBasicBlock::iterator
|
|
|
|
FixupLEAPass::searchALUInst(MachineBasicBlock::iterator &I,
|
|
|
|
MachineBasicBlock &MBB) const {
|
|
|
|
const int InstrDistanceThreshold = 5;
|
|
|
|
int InstrDistance = 1;
|
|
|
|
MachineBasicBlock::iterator CurInst = std::next(I);
|
|
|
|
|
|
|
|
unsigned LEAOpcode = I->getOpcode();
|
|
|
|
unsigned AddOpcode = getADDrrFromLEA(LEAOpcode);
|
|
|
|
unsigned SubOpcode = getSUBrrFromLEA(LEAOpcode);
|
|
|
|
Register DestReg = I->getOperand(0).getReg();
|
|
|
|
|
|
|
|
while (CurInst != MBB.end()) {
|
|
|
|
if (CurInst->isCall() || CurInst->isInlineAsm())
|
|
|
|
break;
|
|
|
|
if (InstrDistance > InstrDistanceThreshold)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Check if the lea dest register is used in an add/sub instruction only.
|
|
|
|
for (unsigned I = 0, E = CurInst->getNumOperands(); I != E; ++I) {
|
|
|
|
MachineOperand &Opnd = CurInst->getOperand(I);
|
|
|
|
if (Opnd.isReg()) {
|
|
|
|
if (Opnd.getReg() == DestReg) {
|
|
|
|
if (Opnd.isDef() || !Opnd.isKill())
|
|
|
|
return MachineBasicBlock::iterator();
|
|
|
|
|
|
|
|
unsigned AluOpcode = CurInst->getOpcode();
|
|
|
|
if (AluOpcode != AddOpcode && AluOpcode != SubOpcode)
|
|
|
|
return MachineBasicBlock::iterator();
|
|
|
|
|
|
|
|
MachineOperand &Opnd2 = CurInst->getOperand(3 - I);
|
|
|
|
MachineOperand AluDest = CurInst->getOperand(0);
|
|
|
|
if (Opnd2.getReg() != AluDest.getReg())
|
|
|
|
return MachineBasicBlock::iterator();
|
|
|
|
|
|
|
|
// X - (Y + Z) may generate different flags than (X - Y) - Z when
|
|
|
|
// there is overflow. So we can't change the alu instruction if the
|
|
|
|
// flags register is live.
|
|
|
|
if (!CurInst->registerDefIsDead(X86::EFLAGS, TRI))
|
|
|
|
return MachineBasicBlock::iterator();
|
|
|
|
|
|
|
|
return CurInst;
|
|
|
|
}
|
|
|
|
if (TRI->regsOverlap(DestReg, Opnd.getReg()))
|
|
|
|
return MachineBasicBlock::iterator();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InstrDistance++;
|
|
|
|
++CurInst;
|
|
|
|
}
|
|
|
|
return MachineBasicBlock::iterator();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FixupLEAPass::checkRegUsage(MachineBasicBlock::iterator &LeaI,
|
|
|
|
MachineBasicBlock::iterator &AluI,
|
|
|
|
bool &BaseIndexDef, bool &AluDestRef,
|
|
|
|
MachineOperand **KilledBase,
|
|
|
|
MachineOperand **KilledIndex) const {
|
|
|
|
BaseIndexDef = AluDestRef = false;
|
|
|
|
*KilledBase = *KilledIndex = nullptr;
|
|
|
|
Register BaseReg = LeaI->getOperand(1 + X86::AddrBaseReg).getReg();
|
|
|
|
Register IndexReg = LeaI->getOperand(1 + X86::AddrIndexReg).getReg();
|
|
|
|
Register AluDestReg = AluI->getOperand(0).getReg();
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator CurInst = std::next(LeaI);
|
|
|
|
while (CurInst != AluI) {
|
|
|
|
for (unsigned I = 0, E = CurInst->getNumOperands(); I != E; ++I) {
|
|
|
|
MachineOperand &Opnd = CurInst->getOperand(I);
|
|
|
|
if (!Opnd.isReg())
|
|
|
|
continue;
|
|
|
|
Register Reg = Opnd.getReg();
|
|
|
|
if (TRI->regsOverlap(Reg, AluDestReg))
|
|
|
|
AluDestRef = true;
|
|
|
|
if (TRI->regsOverlap(Reg, BaseReg)) {
|
|
|
|
if (Opnd.isDef())
|
|
|
|
BaseIndexDef = true;
|
|
|
|
else if (Opnd.isKill())
|
|
|
|
*KilledBase = &Opnd;
|
|
|
|
}
|
|
|
|
if (TRI->regsOverlap(Reg, IndexReg)) {
|
|
|
|
if (Opnd.isDef())
|
|
|
|
BaseIndexDef = true;
|
|
|
|
else if (Opnd.isKill())
|
|
|
|
*KilledIndex = &Opnd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++CurInst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FixupLEAPass::optLEAALU(MachineBasicBlock::iterator &I,
|
|
|
|
MachineBasicBlock &MBB) const {
|
|
|
|
// Look for an add/sub instruction which uses the result of lea.
|
|
|
|
MachineBasicBlock::iterator AluI = searchALUInst(I, MBB);
|
|
|
|
if (AluI == MachineBasicBlock::iterator())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check if there are any related register usage between lea and alu.
|
|
|
|
bool BaseIndexDef, AluDestRef;
|
|
|
|
MachineOperand *KilledBase, *KilledIndex;
|
|
|
|
checkRegUsage(I, AluI, BaseIndexDef, AluDestRef, &KilledBase, &KilledIndex);
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator InsertPos = AluI;
|
|
|
|
if (BaseIndexDef) {
|
|
|
|
if (AluDestRef)
|
|
|
|
return false;
|
|
|
|
InsertPos = I;
|
|
|
|
KilledBase = KilledIndex = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if there are same registers.
|
|
|
|
Register AluDestReg = AluI->getOperand(0).getReg();
|
|
|
|
Register BaseReg = I->getOperand(1 + X86::AddrBaseReg).getReg();
|
|
|
|
Register IndexReg = I->getOperand(1 + X86::AddrIndexReg).getReg();
|
|
|
|
if (I->getOpcode() == X86::LEA64_32r) {
|
|
|
|
BaseReg = TRI->getSubReg(BaseReg, X86::sub_32bit);
|
|
|
|
IndexReg = TRI->getSubReg(IndexReg, X86::sub_32bit);
|
|
|
|
}
|
|
|
|
if (AluDestReg == IndexReg) {
|
|
|
|
if (BaseReg == IndexReg)
|
|
|
|
return false;
|
|
|
|
std::swap(BaseReg, IndexReg);
|
|
|
|
std::swap(KilledBase, KilledIndex);
|
|
|
|
}
|
|
|
|
if (BaseReg == IndexReg)
|
|
|
|
KilledBase = nullptr;
|
|
|
|
|
|
|
|
// Now it's safe to change instructions.
|
|
|
|
MachineInstr *NewMI1, *NewMI2;
|
|
|
|
unsigned NewOpcode = AluI->getOpcode();
|
|
|
|
NewMI1 = BuildMI(MBB, InsertPos, AluI->getDebugLoc(), TII->get(NewOpcode),
|
|
|
|
AluDestReg)
|
|
|
|
.addReg(AluDestReg, RegState::Kill)
|
|
|
|
.addReg(BaseReg, KilledBase ? RegState::Kill : 0);
|
|
|
|
NewMI1->addRegisterDead(X86::EFLAGS, TRI);
|
|
|
|
NewMI2 = BuildMI(MBB, InsertPos, AluI->getDebugLoc(), TII->get(NewOpcode),
|
|
|
|
AluDestReg)
|
|
|
|
.addReg(AluDestReg, RegState::Kill)
|
|
|
|
.addReg(IndexReg, KilledIndex ? RegState::Kill : 0);
|
|
|
|
NewMI2->addRegisterDead(X86::EFLAGS, TRI);
|
|
|
|
|
|
|
|
// Clear the old Kill flags.
|
|
|
|
if (KilledBase)
|
|
|
|
KilledBase->setIsKill(false);
|
|
|
|
if (KilledIndex)
|
|
|
|
KilledIndex->setIsKill(false);
|
|
|
|
|
|
|
|
MBB.getParent()->substituteDebugValuesForInst(*AluI, *NewMI1, 1);
|
|
|
|
MBB.getParent()->substituteDebugValuesForInst(*AluI, *NewMI2, 1);
|
|
|
|
MBB.erase(I);
|
|
|
|
MBB.erase(AluI);
|
|
|
|
I = NewMI1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-25 14:17:47 +08:00
|
|
|
bool FixupLEAPass::optTwoAddrLEA(MachineBasicBlock::iterator &I,
|
|
|
|
MachineBasicBlock &MBB, bool OptIncDec,
|
|
|
|
bool UseLEAForSP) const {
|
2016-07-12 11:18:50 +08:00
|
|
|
MachineInstr &MI = *I;
|
2015-11-11 19:44:31 +08:00
|
|
|
|
2019-05-25 14:17:47 +08:00
|
|
|
const MachineOperand &Base = MI.getOperand(1 + X86::AddrBaseReg);
|
|
|
|
const MachineOperand &Scale = MI.getOperand(1 + X86::AddrScaleAmt);
|
|
|
|
const MachineOperand &Index = MI.getOperand(1 + X86::AddrIndexReg);
|
|
|
|
const MachineOperand &Disp = MI.getOperand(1 + X86::AddrDisp);
|
|
|
|
const MachineOperand &Segment = MI.getOperand(1 + X86::AddrSegmentReg);
|
2015-11-11 19:44:31 +08:00
|
|
|
|
2019-05-25 14:17:47 +08:00
|
|
|
if (Segment.getReg() != 0 || !Disp.isImm() || Scale.getImm() > 1 ||
|
2020-08-09 03:00:15 +08:00
|
|
|
MBB.computeRegisterLiveness(TRI, X86::EFLAGS, I) !=
|
|
|
|
MachineBasicBlock::LQR_Dead)
|
2019-05-25 14:17:47 +08:00
|
|
|
return false;
|
|
|
|
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register DestReg = MI.getOperand(0).getReg();
|
|
|
|
Register BaseReg = Base.getReg();
|
|
|
|
Register IndexReg = Index.getReg();
|
2019-05-25 14:17:47 +08:00
|
|
|
|
|
|
|
// Don't change stack adjustment LEAs.
|
|
|
|
if (UseLEAForSP && (DestReg == X86::ESP || DestReg == X86::RSP))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// LEA64_32 has 64-bit operands but 32-bit result.
|
|
|
|
if (MI.getOpcode() == X86::LEA64_32r) {
|
|
|
|
if (BaseReg != 0)
|
|
|
|
BaseReg = TRI->getSubReg(BaseReg, X86::sub_32bit);
|
|
|
|
if (IndexReg != 0)
|
|
|
|
IndexReg = TRI->getSubReg(IndexReg, X86::sub_32bit);
|
2015-11-11 19:44:31 +08:00
|
|
|
}
|
2019-05-25 14:17:47 +08:00
|
|
|
|
|
|
|
MachineInstr *NewMI = nullptr;
|
|
|
|
|
2021-07-17 01:16:03 +08:00
|
|
|
// Case 1.
|
2019-05-25 14:17:47 +08:00
|
|
|
// Look for lea(%reg1, %reg2), %reg1 or lea(%reg2, %reg1), %reg1
|
|
|
|
// which can be turned into add %reg2, %reg1
|
|
|
|
if (BaseReg != 0 && IndexReg != 0 && Disp.getImm() == 0 &&
|
|
|
|
(DestReg == BaseReg || DestReg == IndexReg)) {
|
|
|
|
unsigned NewOpcode = getADDrrFromLEA(MI.getOpcode());
|
|
|
|
if (DestReg != BaseReg)
|
|
|
|
std::swap(BaseReg, IndexReg);
|
|
|
|
|
|
|
|
if (MI.getOpcode() == X86::LEA64_32r) {
|
|
|
|
// TODO: Do we need the super register implicit use?
|
|
|
|
NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpcode), DestReg)
|
|
|
|
.addReg(BaseReg).addReg(IndexReg)
|
|
|
|
.addReg(Base.getReg(), RegState::Implicit)
|
|
|
|
.addReg(Index.getReg(), RegState::Implicit);
|
|
|
|
} else {
|
|
|
|
NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpcode), DestReg)
|
|
|
|
.addReg(BaseReg).addReg(IndexReg);
|
|
|
|
}
|
|
|
|
} else if (DestReg == BaseReg && IndexReg == 0) {
|
2021-07-17 01:16:03 +08:00
|
|
|
// Case 2.
|
2019-05-25 14:17:47 +08:00
|
|
|
// This is an LEA with only a base register and a displacement,
|
|
|
|
// We can use ADDri or INC/DEC.
|
|
|
|
|
|
|
|
// Does this LEA have one these forms:
|
|
|
|
// lea %reg, 1(%reg)
|
|
|
|
// lea %reg, -1(%reg)
|
|
|
|
if (OptIncDec && (Disp.getImm() == 1 || Disp.getImm() == -1)) {
|
|
|
|
bool IsINC = Disp.getImm() == 1;
|
|
|
|
unsigned NewOpcode = getINCDECFromLEA(MI.getOpcode(), IsINC);
|
|
|
|
|
|
|
|
if (MI.getOpcode() == X86::LEA64_32r) {
|
|
|
|
// TODO: Do we need the super register implicit use?
|
|
|
|
NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpcode), DestReg)
|
|
|
|
.addReg(BaseReg).addReg(Base.getReg(), RegState::Implicit);
|
|
|
|
} else {
|
|
|
|
NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpcode), DestReg)
|
|
|
|
.addReg(BaseReg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unsigned NewOpcode = getADDriFromLEA(MI.getOpcode(), Disp);
|
|
|
|
if (MI.getOpcode() == X86::LEA64_32r) {
|
|
|
|
// TODO: Do we need the super register implicit use?
|
|
|
|
NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpcode), DestReg)
|
|
|
|
.addReg(BaseReg).addImm(Disp.getImm())
|
|
|
|
.addReg(Base.getReg(), RegState::Implicit);
|
|
|
|
} else {
|
|
|
|
NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpcode), DestReg)
|
|
|
|
.addReg(BaseReg).addImm(Disp.getImm());
|
|
|
|
}
|
|
|
|
}
|
2021-07-17 01:16:03 +08:00
|
|
|
} else if (BaseReg != 0 && IndexReg != 0 && Disp.getImm() == 0) {
|
|
|
|
// Case 3.
|
|
|
|
// Look for and transform the sequence
|
|
|
|
// lea (reg1, reg2), reg3
|
|
|
|
// sub reg3, reg4
|
|
|
|
return optLEAALU(I, MBB);
|
2019-05-25 14:17:47 +08:00
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
|
2020-10-22 19:48:57 +08:00
|
|
|
MBB.getParent()->substituteDebugValuesForInst(*I, *NewMI, 1);
|
2019-05-25 14:17:47 +08:00
|
|
|
MBB.erase(I);
|
|
|
|
I = NewMI;
|
|
|
|
return true;
|
2015-11-11 19:44:31 +08:00
|
|
|
}
|
|
|
|
|
2014-06-04 05:01:35 +08:00
|
|
|
void FixupLEAPass::processInstruction(MachineBasicBlock::iterator &I,
|
2019-05-01 01:56:28 +08:00
|
|
|
MachineBasicBlock &MBB) {
|
2013-04-26 04:29:37 +08:00
|
|
|
// Process a load, store, or LEA instruction.
|
2016-07-12 11:18:50 +08:00
|
|
|
MachineInstr &MI = *I;
|
|
|
|
const MCInstrDesc &Desc = MI.getDesc();
|
2016-04-28 13:58:46 +08:00
|
|
|
int AddrOffset = X86II::getMemoryOperandNo(Desc.TSFlags);
|
2013-04-26 04:29:37 +08:00
|
|
|
if (AddrOffset >= 0) {
|
|
|
|
AddrOffset += X86II::getOperandBias(Desc);
|
2016-07-12 11:18:50 +08:00
|
|
|
MachineOperand &p = MI.getOperand(AddrOffset + X86::AddrBaseReg);
|
2013-04-26 04:29:37 +08:00
|
|
|
if (p.isReg() && p.getReg() != X86::ESP) {
|
2019-05-01 01:56:28 +08:00
|
|
|
seekLEAFixup(p, I, MBB);
|
2013-04-26 04:29:37 +08:00
|
|
|
}
|
2016-07-12 11:18:50 +08:00
|
|
|
MachineOperand &q = MI.getOperand(AddrOffset + X86::AddrIndexReg);
|
2013-04-26 04:29:37 +08:00
|
|
|
if (q.isReg() && q.getReg() != X86::ESP) {
|
2019-05-01 01:56:28 +08:00
|
|
|
seekLEAFixup(q, I, MBB);
|
2013-04-26 04:29:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 05:01:35 +08:00
|
|
|
void FixupLEAPass::seekLEAFixup(MachineOperand &p,
|
|
|
|
MachineBasicBlock::iterator &I,
|
2019-05-01 01:56:28 +08:00
|
|
|
MachineBasicBlock &MBB) {
|
|
|
|
MachineBasicBlock::iterator MBI = searchBackwards(p, I, MBB);
|
2016-07-12 11:18:50 +08:00
|
|
|
if (MBI != MachineBasicBlock::iterator()) {
|
2019-05-01 01:56:28 +08:00
|
|
|
MachineInstr *NewMI = postRAConvertToLEA(MBB, MBI);
|
2013-04-26 04:29:37 +08:00
|
|
|
if (NewMI) {
|
|
|
|
++NumLEAs;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "FixLEA: Candidate to replace:"; MBI->dump(););
|
2013-04-26 04:29:37 +08:00
|
|
|
// now to replace with an equivalent LEA...
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "FixLEA: Replaced by: "; NewMI->dump(););
|
2020-10-22 19:48:57 +08:00
|
|
|
MBB.getParent()->substituteDebugValuesForInst(*MBI, *NewMI, 1);
|
2019-05-01 01:56:28 +08:00
|
|
|
MBB.erase(MBI);
|
2013-04-26 04:29:37 +08:00
|
|
|
MachineBasicBlock::iterator J =
|
2014-06-04 05:01:35 +08:00
|
|
|
static_cast<MachineBasicBlock::iterator>(NewMI);
|
2019-05-01 01:56:28 +08:00
|
|
|
processInstruction(J, MBB);
|
2013-04-26 04:29:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 22:57:07 +08:00
|
|
|
void FixupLEAPass::processInstructionForSlowLEA(MachineBasicBlock::iterator &I,
|
2019-05-01 01:56:28 +08:00
|
|
|
MachineBasicBlock &MBB) {
|
2016-07-12 11:18:50 +08:00
|
|
|
MachineInstr &MI = *I;
|
2019-04-02 08:50:58 +08:00
|
|
|
const unsigned Opcode = MI.getOpcode();
|
2018-12-22 09:34:47 +08:00
|
|
|
|
|
|
|
const MachineOperand &Dst = MI.getOperand(0);
|
|
|
|
const MachineOperand &Base = MI.getOperand(1 + X86::AddrBaseReg);
|
|
|
|
const MachineOperand &Scale = MI.getOperand(1 + X86::AddrScaleAmt);
|
|
|
|
const MachineOperand &Index = MI.getOperand(1 + X86::AddrIndexReg);
|
|
|
|
const MachineOperand &Offset = MI.getOperand(1 + X86::AddrDisp);
|
|
|
|
const MachineOperand &Segment = MI.getOperand(1 + X86::AddrSegmentReg);
|
|
|
|
|
|
|
|
if (Segment.getReg() != 0 || !Offset.isImm() ||
|
2020-08-09 03:00:15 +08:00
|
|
|
MBB.computeRegisterLiveness(TRI, X86::EFLAGS, I, 4) !=
|
|
|
|
MachineBasicBlock::LQR_Dead)
|
2014-05-20 16:55:50 +08:00
|
|
|
return;
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
const Register DstR = Dst.getReg();
|
|
|
|
const Register SrcR1 = Base.getReg();
|
|
|
|
const Register SrcR2 = Index.getReg();
|
2014-05-20 16:55:50 +08:00
|
|
|
if ((SrcR1 == 0 || SrcR1 != DstR) && (SrcR2 == 0 || SrcR2 != DstR))
|
|
|
|
return;
|
2018-12-22 09:34:47 +08:00
|
|
|
if (Scale.getImm() > 1)
|
2014-05-20 16:55:50 +08:00
|
|
|
return;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "FixLEA: Candidate to replace:"; I->dump(););
|
|
|
|
LLVM_DEBUG(dbgs() << "FixLEA: Replaced by: ";);
|
2014-06-09 06:29:17 +08:00
|
|
|
MachineInstr *NewMI = nullptr;
|
2014-05-20 16:55:50 +08:00
|
|
|
// Make ADD instruction for two registers writing to LEA's destination
|
|
|
|
if (SrcR1 != 0 && SrcR2 != 0) {
|
2017-05-18 16:11:50 +08:00
|
|
|
const MCInstrDesc &ADDrr = TII->get(getADDrrFromLEA(Opcode));
|
2018-12-22 09:34:47 +08:00
|
|
|
const MachineOperand &Src = SrcR1 == DstR ? Index : Base;
|
2017-05-18 16:11:50 +08:00
|
|
|
NewMI =
|
2019-05-01 01:56:28 +08:00
|
|
|
BuildMI(MBB, I, MI.getDebugLoc(), ADDrr, DstR).addReg(DstR).add(Src);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(NewMI->dump(););
|
2014-05-20 16:55:50 +08:00
|
|
|
}
|
|
|
|
// Make ADD instruction for immediate
|
2018-12-22 09:34:47 +08:00
|
|
|
if (Offset.getImm() != 0) {
|
2017-05-18 16:11:50 +08:00
|
|
|
const MCInstrDesc &ADDri =
|
2018-12-22 09:34:47 +08:00
|
|
|
TII->get(getADDriFromLEA(Opcode, Offset));
|
|
|
|
const MachineOperand &SrcR = SrcR1 == DstR ? Base : Index;
|
2019-05-01 01:56:28 +08:00
|
|
|
NewMI = BuildMI(MBB, I, MI.getDebugLoc(), ADDri, DstR)
|
2017-01-13 17:58:52 +08:00
|
|
|
.add(SrcR)
|
2018-12-22 09:34:47 +08:00
|
|
|
.addImm(Offset.getImm());
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(NewMI->dump(););
|
2014-05-20 16:55:50 +08:00
|
|
|
}
|
|
|
|
if (NewMI) {
|
2020-10-22 19:48:57 +08:00
|
|
|
MBB.getParent()->substituteDebugValuesForInst(*I, *NewMI, 1);
|
2019-05-01 01:56:28 +08:00
|
|
|
MBB.erase(I);
|
2017-05-18 16:11:50 +08:00
|
|
|
I = NewMI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 14:27:55 +08:00
|
|
|
void FixupLEAPass::processInstrForSlow3OpLEA(MachineBasicBlock::iterator &I,
|
|
|
|
MachineBasicBlock &MBB,
|
|
|
|
bool OptIncDec) {
|
|
|
|
MachineInstr &MI = *I;
|
2019-04-02 08:50:58 +08:00
|
|
|
const unsigned LEAOpcode = MI.getOpcode();
|
2017-05-18 16:11:50 +08:00
|
|
|
|
2019-10-07 14:27:55 +08:00
|
|
|
const MachineOperand &Dest = MI.getOperand(0);
|
2018-12-22 09:34:47 +08:00
|
|
|
const MachineOperand &Base = MI.getOperand(1 + X86::AddrBaseReg);
|
|
|
|
const MachineOperand &Scale = MI.getOperand(1 + X86::AddrScaleAmt);
|
|
|
|
const MachineOperand &Index = MI.getOperand(1 + X86::AddrIndexReg);
|
|
|
|
const MachineOperand &Offset = MI.getOperand(1 + X86::AddrDisp);
|
|
|
|
const MachineOperand &Segment = MI.getOperand(1 + X86::AddrSegmentReg);
|
2017-05-18 16:11:50 +08:00
|
|
|
|
2019-10-07 14:27:55 +08:00
|
|
|
if (!(TII->isThreeOperandsLEA(MI) || hasInefficientLEABaseReg(Base, Index)) ||
|
2020-08-09 03:00:15 +08:00
|
|
|
MBB.computeRegisterLiveness(TRI, X86::EFLAGS, I, 4) !=
|
|
|
|
MachineBasicBlock::LQR_Dead ||
|
2017-05-18 16:11:50 +08:00
|
|
|
Segment.getReg() != X86::NoRegister)
|
2019-10-07 14:27:55 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
Register DestReg = Dest.getReg();
|
|
|
|
Register BaseReg = Base.getReg();
|
|
|
|
Register IndexReg = Index.getReg();
|
|
|
|
|
|
|
|
if (MI.getOpcode() == X86::LEA64_32r) {
|
|
|
|
if (BaseReg != 0)
|
|
|
|
BaseReg = TRI->getSubReg(BaseReg, X86::sub_32bit);
|
|
|
|
if (IndexReg != 0)
|
|
|
|
IndexReg = TRI->getSubReg(IndexReg, X86::sub_32bit);
|
|
|
|
}
|
2017-05-18 16:11:50 +08:00
|
|
|
|
|
|
|
bool IsScale1 = Scale.getImm() == 1;
|
2019-10-07 14:27:55 +08:00
|
|
|
bool IsInefficientBase = isInefficientLEAReg(BaseReg);
|
|
|
|
bool IsInefficientIndex = isInefficientLEAReg(IndexReg);
|
2017-05-18 16:11:50 +08:00
|
|
|
|
|
|
|
// Skip these cases since it takes more than 2 instructions
|
|
|
|
// to replace the LEA instruction.
|
2019-10-07 14:27:55 +08:00
|
|
|
if (IsInefficientBase && DestReg == BaseReg && !IsScale1)
|
|
|
|
return;
|
2017-05-18 16:11:50 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "FixLEA: Candidate to replace:"; MI.dump(););
|
|
|
|
LLVM_DEBUG(dbgs() << "FixLEA: Replaced by: ";);
|
2017-05-18 16:11:50 +08:00
|
|
|
|
2019-10-07 14:27:55 +08:00
|
|
|
MachineInstr *NewMI = nullptr;
|
|
|
|
|
2017-05-18 16:11:50 +08:00
|
|
|
// First try to replace LEA with one or two (for the 3-op LEA case)
|
|
|
|
// add instructions:
|
|
|
|
// 1.lea (%base,%index,1), %base => add %index,%base
|
|
|
|
// 2.lea (%base,%index,1), %index => add %base,%index
|
2019-10-07 14:27:55 +08:00
|
|
|
if (IsScale1 && (DestReg == BaseReg || DestReg == IndexReg)) {
|
|
|
|
unsigned NewOpc = getADDrrFromLEA(MI.getOpcode());
|
|
|
|
if (DestReg != BaseReg)
|
|
|
|
std::swap(BaseReg, IndexReg);
|
|
|
|
|
|
|
|
if (MI.getOpcode() == X86::LEA64_32r) {
|
|
|
|
// TODO: Do we need the super register implicit use?
|
|
|
|
NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpc), DestReg)
|
|
|
|
.addReg(BaseReg)
|
|
|
|
.addReg(IndexReg)
|
|
|
|
.addReg(Base.getReg(), RegState::Implicit)
|
|
|
|
.addReg(Index.getReg(), RegState::Implicit);
|
|
|
|
} else {
|
|
|
|
NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpc), DestReg)
|
|
|
|
.addReg(BaseReg)
|
|
|
|
.addReg(IndexReg);
|
2017-05-18 16:11:50 +08:00
|
|
|
}
|
2019-10-07 14:27:55 +08:00
|
|
|
} else if (!IsInefficientBase || (!IsInefficientIndex && IsScale1)) {
|
|
|
|
// If the base is inefficient try switching the index and base operands,
|
|
|
|
// otherwise just break the 3-Ops LEA inst into 2-Ops LEA + ADD instruction:
|
|
|
|
// lea offset(%base,%index,scale),%dst =>
|
|
|
|
// lea (%base,%index,scale); add offset,%dst
|
|
|
|
NewMI = BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(LEAOpcode))
|
|
|
|
.add(Dest)
|
|
|
|
.add(IsInefficientBase ? Index : Base)
|
|
|
|
.add(Scale)
|
|
|
|
.add(IsInefficientBase ? Base : Index)
|
|
|
|
.addImm(0)
|
|
|
|
.add(Segment);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(NewMI->dump(););
|
2019-10-07 14:27:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If either replacement succeeded above, add the offset if needed, then
|
|
|
|
// replace the instruction.
|
|
|
|
if (NewMI) {
|
2017-05-18 16:11:50 +08:00
|
|
|
// Create ADD instruction for the Offset in case of 3-Ops LEA.
|
|
|
|
if (hasLEAOffset(Offset)) {
|
2019-10-07 14:27:55 +08:00
|
|
|
if (OptIncDec && Offset.isImm() &&
|
|
|
|
(Offset.getImm() == 1 || Offset.getImm() == -1)) {
|
|
|
|
unsigned NewOpc =
|
|
|
|
getINCDECFromLEA(MI.getOpcode(), Offset.getImm() == 1);
|
|
|
|
NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpc), DestReg)
|
|
|
|
.addReg(DestReg);
|
|
|
|
LLVM_DEBUG(NewMI->dump(););
|
|
|
|
} else {
|
|
|
|
unsigned NewOpc = getADDriFromLEA(MI.getOpcode(), Offset);
|
|
|
|
NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpc), DestReg)
|
|
|
|
.addReg(DestReg)
|
|
|
|
.add(Offset);
|
|
|
|
LLVM_DEBUG(NewMI->dump(););
|
|
|
|
}
|
2017-05-18 16:11:50 +08:00
|
|
|
}
|
2019-10-07 14:27:55 +08:00
|
|
|
|
2020-10-22 19:48:57 +08:00
|
|
|
MBB.getParent()->substituteDebugValuesForInst(*I, *NewMI, 1);
|
2019-10-07 14:27:55 +08:00
|
|
|
MBB.erase(I);
|
|
|
|
I = NewMI;
|
|
|
|
return;
|
2017-05-18 16:11:50 +08:00
|
|
|
}
|
2019-10-07 14:27:55 +08:00
|
|
|
|
2017-05-18 16:11:50 +08:00
|
|
|
// Handle the rest of the cases with inefficient base register:
|
2019-10-07 14:27:55 +08:00
|
|
|
assert(DestReg != BaseReg && "DestReg == BaseReg should be handled already!");
|
2017-05-18 16:11:50 +08:00
|
|
|
assert(IsInefficientBase && "efficient base should be handled already!");
|
|
|
|
|
2019-10-07 14:27:55 +08:00
|
|
|
// FIXME: Handle LEA64_32r.
|
|
|
|
if (LEAOpcode == X86::LEA64_32r)
|
|
|
|
return;
|
|
|
|
|
2017-05-18 16:11:50 +08:00
|
|
|
// lea (%base,%index,1), %dst => mov %base,%dst; add %index,%dst
|
|
|
|
if (IsScale1 && !hasLEAOffset(Offset)) {
|
2019-10-07 14:27:55 +08:00
|
|
|
bool BIK = Base.isKill() && BaseReg != IndexReg;
|
|
|
|
TII->copyPhysReg(MBB, MI, MI.getDebugLoc(), DestReg, BaseReg, BIK);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(MI.getPrevNode()->dump(););
|
2017-05-18 16:11:50 +08:00
|
|
|
|
2019-10-07 14:27:55 +08:00
|
|
|
unsigned NewOpc = getADDrrFromLEA(MI.getOpcode());
|
|
|
|
NewMI = BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(NewOpc), DestReg)
|
|
|
|
.addReg(DestReg)
|
|
|
|
.add(Index);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(NewMI->dump(););
|
2019-12-11 23:51:02 +08:00
|
|
|
|
2020-10-22 19:48:57 +08:00
|
|
|
MBB.getParent()->substituteDebugValuesForInst(*I, *NewMI, 1);
|
2019-12-11 23:51:02 +08:00
|
|
|
MBB.erase(I);
|
|
|
|
I = NewMI;
|
2019-10-07 14:27:55 +08:00
|
|
|
return;
|
2014-05-20 16:55:50 +08:00
|
|
|
}
|
2019-10-07 14:27:55 +08:00
|
|
|
|
2017-05-18 16:11:50 +08:00
|
|
|
// lea offset(%base,%index,scale), %dst =>
|
|
|
|
// lea offset( ,%index,scale), %dst; add %base,%dst
|
2019-10-07 14:27:55 +08:00
|
|
|
NewMI = BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(LEAOpcode))
|
|
|
|
.add(Dest)
|
|
|
|
.addReg(0)
|
|
|
|
.add(Scale)
|
|
|
|
.add(Index)
|
|
|
|
.add(Offset)
|
|
|
|
.add(Segment);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(NewMI->dump(););
|
2017-05-18 16:11:50 +08:00
|
|
|
|
2019-10-07 14:27:55 +08:00
|
|
|
unsigned NewOpc = getADDrrFromLEA(MI.getOpcode());
|
|
|
|
NewMI = BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(NewOpc), DestReg)
|
|
|
|
.addReg(DestReg)
|
|
|
|
.add(Base);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(NewMI->dump(););
|
2019-10-07 14:27:55 +08:00
|
|
|
|
2020-10-22 19:48:57 +08:00
|
|
|
MBB.getParent()->substituteDebugValuesForInst(*I, *NewMI, 1);
|
2019-10-07 14:27:55 +08:00
|
|
|
MBB.erase(I);
|
|
|
|
I = NewMI;
|
2014-05-20 16:55:50 +08:00
|
|
|
}
|