2017-09-14 05:15:20 +08:00
|
|
|
//===- RegAllocFast.cpp - A fast register allocator for debug code --------===//
|
2010-04-22 02:02:42 +08:00
|
|
|
//
|
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
|
2010-04-22 02:02:42 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2017-09-09 08:52:46 +08:00
|
|
|
/// \file This register allocator allocates registers to a basic block at a
|
|
|
|
/// time, attempting to keep values in registers and reusing registers as
|
|
|
|
/// appropriate.
|
2010-04-22 02:02:42 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-09-14 05:15:20 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/IndexedMap.h"
|
|
|
|
#include "llvm/ADT/SmallSet.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/SparseSet.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2017-09-14 05:15:20 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2017-09-14 05:15:20 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2010-04-22 02:02:42 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2010-08-05 02:42:02 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2017-09-14 05:15:20 +08:00
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
2010-04-22 02:02:42 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/RegAllocRegistry.h"
|
2012-06-07 04:29:31 +08:00
|
|
|
#include "llvm/CodeGen/RegisterClassInfo.h"
|
2017-11-08 09:01:31 +08:00
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetOpcodes.h"
|
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2017-09-14 05:15:20 +08:00
|
|
|
#include "llvm/IR/DebugLoc.h"
|
|
|
|
#include "llvm/IR/Metadata.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2017-09-14 05:15:20 +08:00
|
|
|
#include "llvm/MC/MCInstrDesc.h"
|
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
2010-04-22 02:02:42 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2017-09-14 05:15:20 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <tuple>
|
|
|
|
#include <vector>
|
|
|
|
|
2010-04-22 02:02:42 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:02:50 +08:00
|
|
|
#define DEBUG_TYPE "regalloc"
|
|
|
|
|
2010-04-22 02:02:42 +08:00
|
|
|
STATISTIC(NumStores, "Number of stores added");
|
|
|
|
STATISTIC(NumLoads , "Number of loads added");
|
2018-11-07 10:04:07 +08:00
|
|
|
STATISTIC(NumCoalesced, "Number of copies coalesced");
|
2010-04-22 02:02:42 +08:00
|
|
|
|
|
|
|
static RegisterRegAlloc
|
|
|
|
fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
|
|
|
|
|
|
|
|
namespace {
|
2017-09-14 05:15:20 +08:00
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
class RegAllocFast : public MachineFunctionPass {
|
2010-04-22 02:02:42 +08:00
|
|
|
public:
|
|
|
|
static char ID;
|
2017-09-14 05:15:20 +08:00
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
RegAllocFast() : MachineFunctionPass(ID), StackSlotForVirtReg(-1) {}
|
2016-03-29 01:05:30 +08:00
|
|
|
|
2010-04-22 02:02:42 +08:00
|
|
|
private:
|
2017-09-09 08:52:46 +08:00
|
|
|
MachineFrameInfo *MFI;
|
2010-05-13 08:19:43 +08:00
|
|
|
MachineRegisterInfo *MRI;
|
2010-04-22 02:02:42 +08:00
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
const TargetInstrInfo *TII;
|
2011-06-03 02:35:30 +08:00
|
|
|
RegisterClassInfo RegClassInfo;
|
2010-04-22 02:02:42 +08:00
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// Basic block currently being allocated.
|
2010-05-17 10:07:22 +08:00
|
|
|
MachineBasicBlock *MBB;
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// Maps virtual regs to the frame index where these values are spilled.
|
2010-04-22 02:02:42 +08:00
|
|
|
IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// Everything we know about a live virtual register.
|
2010-05-12 07:24:45 +08:00
|
|
|
struct LiveReg {
|
2017-09-14 05:15:20 +08:00
|
|
|
MachineInstr *LastUse = nullptr; ///< Last instr to use reg.
|
2019-10-31 05:01:58 +08:00
|
|
|
Register VirtReg; ///< Virtual register number.
|
2017-09-14 05:15:20 +08:00
|
|
|
MCPhysReg PhysReg = 0; ///< Currently held here.
|
|
|
|
unsigned short LastOpNum = 0; ///< OpNum on LastUse.
|
|
|
|
bool Dirty = false; ///< Register needs spill.
|
2010-05-12 07:24:45 +08:00
|
|
|
|
2019-10-31 05:01:58 +08:00
|
|
|
explicit LiveReg(Register VirtReg) : VirtReg(VirtReg) {}
|
2012-02-22 09:02:37 +08:00
|
|
|
|
2012-04-21 04:05:28 +08:00
|
|
|
unsigned getSparseSetIndex() const {
|
2019-08-02 07:27:28 +08:00
|
|
|
return Register::virtReg2Index(VirtReg);
|
2012-02-22 09:02:37 +08:00
|
|
|
}
|
2010-05-12 07:24:45 +08:00
|
|
|
};
|
|
|
|
|
2017-09-14 05:15:20 +08:00
|
|
|
using LiveRegMap = SparseSet<LiveReg>;
|
2017-09-09 08:52:46 +08:00
|
|
|
/// This map contains entries for each virtual register that is currently
|
|
|
|
/// available in a physical register.
|
2010-05-12 07:24:45 +08:00
|
|
|
LiveRegMap LiveVirtRegs;
|
2010-04-22 02:02:42 +08:00
|
|
|
|
2018-11-07 10:04:11 +08:00
|
|
|
DenseMap<unsigned, SmallVector<MachineInstr *, 2>> LiveDbgValueMap;
|
2010-08-05 02:42:02 +08:00
|
|
|
|
2019-05-04 03:06:57 +08:00
|
|
|
/// Has a bit set for every virtual register for which it was determined
|
|
|
|
/// that it is alive across blocks.
|
|
|
|
BitVector MayLiveAcrossBlocks;
|
|
|
|
|
2018-11-07 10:04:11 +08:00
|
|
|
/// State of a physical register.
|
2010-05-12 02:54:45 +08:00
|
|
|
enum RegState {
|
2017-09-09 08:52:46 +08:00
|
|
|
/// A disabled register is not available for allocation, but an alias may
|
|
|
|
/// be in use. A register can only be moved out of the disabled state if
|
|
|
|
/// all aliases are disabled.
|
2010-05-12 02:54:45 +08:00
|
|
|
regDisabled,
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// A free register is not currently in use and can be allocated
|
|
|
|
/// immediately without checking aliases.
|
2010-05-12 02:54:45 +08:00
|
|
|
regFree,
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// A reserved register has been assigned explicitly (e.g., setting up a
|
|
|
|
/// call parameter), and it remains reserved until it is used.
|
2010-05-12 02:54:45 +08:00
|
|
|
regReserved
|
2010-04-22 02:02:42 +08:00
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// A register state may also be a virtual register number, indication
|
|
|
|
/// that the physical register is currently allocated to a virtual
|
|
|
|
/// register. In that case, LiveVirtRegs contains the inverse mapping.
|
2010-05-12 02:54:45 +08:00
|
|
|
};
|
|
|
|
|
2018-11-07 10:04:11 +08:00
|
|
|
/// Maps each physical register to a RegState enum or a virtual register.
|
2010-05-12 02:54:45 +08:00
|
|
|
std::vector<unsigned> PhysRegState;
|
2010-04-22 02:02:42 +08:00
|
|
|
|
2019-10-31 05:01:58 +08:00
|
|
|
SmallVector<Register, 16> VirtDead;
|
2017-09-14 05:15:20 +08:00
|
|
|
SmallVector<MachineInstr *, 32> Coalesced;
|
2017-09-09 08:52:45 +08:00
|
|
|
|
2018-11-07 10:04:11 +08:00
|
|
|
using RegUnitSet = SparseSet<uint16_t, identity<uint16_t>>;
|
2017-09-09 08:52:46 +08:00
|
|
|
/// Set of register units that are used in the current instruction, and so
|
|
|
|
/// cannot be allocated.
|
2018-11-07 10:04:11 +08:00
|
|
|
RegUnitSet UsedInInstr;
|
2010-04-22 02:02:42 +08:00
|
|
|
|
2018-11-07 14:57:00 +08:00
|
|
|
void setPhysRegState(MCPhysReg PhysReg, unsigned NewState);
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// Mark a physreg as used in this instruction.
|
|
|
|
void markRegUsedInInstr(MCPhysReg PhysReg) {
|
2013-02-22 03:35:21 +08:00
|
|
|
for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
|
|
|
|
UsedInInstr.insert(*Units);
|
|
|
|
}
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// Check if a physreg or any of its aliases are used in this instruction.
|
|
|
|
bool isRegUsedInInstr(MCPhysReg PhysReg) const {
|
2013-02-22 03:35:21 +08:00
|
|
|
for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
|
|
|
|
if (UsedInInstr.count(*Units))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-02 11:20:38 +08:00
|
|
|
enum : unsigned {
|
2018-11-07 10:04:11 +08:00
|
|
|
spillClean = 50,
|
2010-05-17 23:30:32 +08:00
|
|
|
spillDirty = 100,
|
2019-05-16 20:50:39 +08:00
|
|
|
spillPrefBonus = 20,
|
2010-05-17 23:30:32 +08:00
|
|
|
spillImpossible = ~0u
|
|
|
|
};
|
2017-09-14 05:15:20 +08:00
|
|
|
|
2010-04-22 02:02:42 +08:00
|
|
|
public:
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override { return "Fast Register Allocator"; }
|
2010-04-22 02:02:42 +08:00
|
|
|
|
2014-03-07 17:26:03 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2010-04-22 02:02:42 +08:00
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2016-08-24 05:19:49 +08:00
|
|
|
MachineFunctionProperties getRequiredProperties() const override {
|
|
|
|
return MachineFunctionProperties().set(
|
|
|
|
MachineFunctionProperties::Property::NoPHIs);
|
|
|
|
}
|
|
|
|
|
2016-03-29 01:05:30 +08:00
|
|
|
MachineFunctionProperties getSetProperties() const override {
|
|
|
|
return MachineFunctionProperties().set(
|
2016-08-25 09:27:13 +08:00
|
|
|
MachineFunctionProperties::Property::NoVRegs);
|
2016-03-29 01:05:30 +08:00
|
|
|
}
|
|
|
|
|
2010-04-22 02:02:42 +08:00
|
|
|
private:
|
2018-07-17 02:51:40 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2018-11-07 10:04:11 +08:00
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
void allocateBasicBlock(MachineBasicBlock &MBB);
|
2018-11-10 08:36:27 +08:00
|
|
|
void allocateInstruction(MachineInstr &MI);
|
|
|
|
void handleDebugValue(MachineInstr &MI);
|
2017-09-09 08:52:46 +08:00
|
|
|
void handleThroughOperands(MachineInstr &MI,
|
2019-10-31 05:01:58 +08:00
|
|
|
SmallVectorImpl<Register> &VirtDead);
|
2017-09-09 08:52:46 +08:00
|
|
|
bool isLastUseOfLocalReg(const MachineOperand &MO) const;
|
2010-05-15 14:09:08 +08:00
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
void addKillFlag(const LiveReg &LRI);
|
2018-11-07 14:57:03 +08:00
|
|
|
void killVirtReg(LiveReg &LR);
|
2019-10-31 05:01:58 +08:00
|
|
|
void killVirtReg(Register VirtReg);
|
2018-11-07 14:57:03 +08:00
|
|
|
void spillVirtReg(MachineBasicBlock::iterator MI, LiveReg &LR);
|
2019-10-31 05:01:58 +08:00
|
|
|
void spillVirtReg(MachineBasicBlock::iterator MI, Register VirtReg);
|
2010-05-15 02:03:25 +08:00
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
void usePhysReg(MachineOperand &MO);
|
2018-01-30 07:42:37 +08:00
|
|
|
void definePhysReg(MachineBasicBlock::iterator MI, MCPhysReg PhysReg,
|
|
|
|
RegState NewState);
|
2017-09-09 08:52:46 +08:00
|
|
|
unsigned calcSpillCost(MCPhysReg PhysReg) const;
|
2018-01-30 07:42:37 +08:00
|
|
|
void assignVirtToPhysReg(LiveReg &, MCPhysReg PhysReg);
|
2017-09-14 05:15:20 +08:00
|
|
|
|
2019-10-31 05:01:58 +08:00
|
|
|
LiveRegMap::iterator findLiveVirtReg(Register VirtReg) {
|
2019-08-02 07:27:28 +08:00
|
|
|
return LiveVirtRegs.find(Register::virtReg2Index(VirtReg));
|
2012-02-22 09:02:37 +08:00
|
|
|
}
|
2017-09-14 05:15:20 +08:00
|
|
|
|
2019-10-31 05:01:58 +08:00
|
|
|
LiveRegMap::const_iterator findLiveVirtReg(Register VirtReg) const {
|
2019-08-02 07:27:28 +08:00
|
|
|
return LiveVirtRegs.find(Register::virtReg2Index(VirtReg));
|
2012-02-22 09:02:37 +08:00
|
|
|
}
|
2017-09-14 05:15:20 +08:00
|
|
|
|
2019-10-31 05:01:58 +08:00
|
|
|
void allocVirtReg(MachineInstr &MI, LiveReg &LR, Register Hint);
|
2019-03-20 03:16:04 +08:00
|
|
|
void allocVirtRegUndef(MachineOperand &MO);
|
2019-10-31 05:01:58 +08:00
|
|
|
MCPhysReg defineVirtReg(MachineInstr &MI, unsigned OpNum, Register VirtReg,
|
|
|
|
Register Hint);
|
|
|
|
LiveReg &reloadVirtReg(MachineInstr &MI, unsigned OpNum, Register VirtReg,
|
|
|
|
Register Hint);
|
2019-05-04 03:06:57 +08:00
|
|
|
void spillAll(MachineBasicBlock::iterator MI, bool OnlyLiveOut);
|
2018-11-10 08:36:27 +08:00
|
|
|
bool setPhysReg(MachineInstr &MI, MachineOperand &MO, MCPhysReg PhysReg);
|
2017-09-09 08:52:46 +08:00
|
|
|
|
2019-10-31 05:01:58 +08:00
|
|
|
Register traceCopies(Register VirtReg) const;
|
|
|
|
Register traceCopyChain(Register Reg) const;
|
2019-05-16 20:50:39 +08:00
|
|
|
|
2019-10-31 05:01:58 +08:00
|
|
|
int getStackSpaceFor(Register VirtReg);
|
|
|
|
void spill(MachineBasicBlock::iterator Before, Register VirtReg,
|
2018-11-07 10:04:12 +08:00
|
|
|
MCPhysReg AssignedReg, bool Kill);
|
2019-10-31 05:01:58 +08:00
|
|
|
void reload(MachineBasicBlock::iterator Before, Register VirtReg,
|
2018-11-07 10:04:12 +08:00
|
|
|
MCPhysReg PhysReg);
|
|
|
|
|
2019-10-31 05:01:58 +08:00
|
|
|
bool mayLiveOut(Register VirtReg);
|
|
|
|
bool mayLiveIn(Register VirtReg);
|
2019-05-04 03:06:57 +08:00
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
void dumpState();
|
2010-04-22 02:02:42 +08:00
|
|
|
};
|
2017-09-14 05:15:20 +08:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
char RegAllocFast::ID = 0;
|
2010-04-22 02:02:42 +08:00
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
INITIALIZE_PASS(RegAllocFast, "regallocfast", "Fast Register Allocator", false,
|
|
|
|
false)
|
2017-07-08 03:25:42 +08:00
|
|
|
|
2018-11-07 14:57:00 +08:00
|
|
|
void RegAllocFast::setPhysRegState(MCPhysReg PhysReg, unsigned NewState) {
|
|
|
|
PhysRegState[PhysReg] = NewState;
|
|
|
|
}
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// This allocates space for the specified virtual register to be held on the
|
|
|
|
/// stack.
|
2019-10-31 05:01:58 +08:00
|
|
|
int RegAllocFast::getStackSpaceFor(Register VirtReg) {
|
2010-04-22 02:02:42 +08:00
|
|
|
// Find the location Reg would belong...
|
|
|
|
int SS = StackSlotForVirtReg[VirtReg];
|
2017-09-09 08:52:46 +08:00
|
|
|
// Already has space allocated?
|
2010-04-22 02:02:42 +08:00
|
|
|
if (SS != -1)
|
2017-09-09 08:52:46 +08:00
|
|
|
return SS;
|
2010-04-22 02:02:42 +08:00
|
|
|
|
|
|
|
// Allocate a new stack object for this spill location...
|
2018-11-07 10:04:11 +08:00
|
|
|
const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
|
2017-09-09 08:52:46 +08:00
|
|
|
unsigned Size = TRI->getSpillSize(RC);
|
|
|
|
unsigned Align = TRI->getSpillAlignment(RC);
|
|
|
|
int FrameIdx = MFI->CreateSpillStackObject(Size, Align);
|
2010-04-22 02:02:42 +08:00
|
|
|
|
|
|
|
// Assign the slot.
|
|
|
|
StackSlotForVirtReg[VirtReg] = FrameIdx;
|
|
|
|
return FrameIdx;
|
|
|
|
}
|
|
|
|
|
2019-05-04 03:06:57 +08:00
|
|
|
/// Returns false if \p VirtReg is known to not live out of the current block.
|
2019-10-31 05:01:58 +08:00
|
|
|
bool RegAllocFast::mayLiveOut(Register VirtReg) {
|
2019-08-02 07:27:28 +08:00
|
|
|
if (MayLiveAcrossBlocks.test(Register::virtReg2Index(VirtReg))) {
|
2019-05-04 03:06:57 +08:00
|
|
|
// Cannot be live-out if there are no successors.
|
|
|
|
return !MBB->succ_empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this block loops back to itself, it would be necessary to check whether
|
|
|
|
// the use comes after the def.
|
2019-05-28 04:37:31 +08:00
|
|
|
if (MBB->isSuccessor(MBB)) {
|
2019-08-02 07:27:28 +08:00
|
|
|
MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg));
|
2019-05-04 03:06:57 +08:00
|
|
|
return true;
|
2019-05-28 04:37:31 +08:00
|
|
|
}
|
2019-05-04 03:06:57 +08:00
|
|
|
|
|
|
|
// See if the first \p Limit uses of the register are all in the current
|
|
|
|
// block.
|
|
|
|
static const unsigned Limit = 8;
|
|
|
|
unsigned C = 0;
|
|
|
|
for (const MachineInstr &UseInst : MRI->reg_nodbg_instructions(VirtReg)) {
|
|
|
|
if (UseInst.getParent() != MBB || ++C >= Limit) {
|
2019-08-02 07:27:28 +08:00
|
|
|
MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg));
|
2019-05-04 03:06:57 +08:00
|
|
|
// Cannot be live-out if there are no successors.
|
|
|
|
return !MBB->succ_empty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-28 04:37:31 +08:00
|
|
|
/// Returns false if \p VirtReg is known to not be live into the current block.
|
2019-10-31 05:01:58 +08:00
|
|
|
bool RegAllocFast::mayLiveIn(Register VirtReg) {
|
2019-08-02 07:27:28 +08:00
|
|
|
if (MayLiveAcrossBlocks.test(Register::virtReg2Index(VirtReg)))
|
2019-05-28 04:37:31 +08:00
|
|
|
return !MBB->pred_empty();
|
|
|
|
|
|
|
|
// See if the first \p Limit def of the register are all in the current block.
|
|
|
|
static const unsigned Limit = 8;
|
|
|
|
unsigned C = 0;
|
|
|
|
for (const MachineInstr &DefInst : MRI->def_instructions(VirtReg)) {
|
|
|
|
if (DefInst.getParent() != MBB || ++C >= Limit) {
|
2019-08-02 07:27:28 +08:00
|
|
|
MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg));
|
2019-05-28 04:37:31 +08:00
|
|
|
return !MBB->pred_empty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-07 10:04:12 +08:00
|
|
|
/// Insert spill instruction for \p AssignedReg before \p Before. Update
|
|
|
|
/// DBG_VALUEs with \p VirtReg operands with the stack slot.
|
2019-10-31 05:01:58 +08:00
|
|
|
void RegAllocFast::spill(MachineBasicBlock::iterator Before, Register VirtReg,
|
2018-11-07 10:04:12 +08:00
|
|
|
MCPhysReg AssignedReg, bool Kill) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Spilling " << printReg(VirtReg, TRI)
|
|
|
|
<< " in " << printReg(AssignedReg, TRI));
|
|
|
|
int FI = getStackSpaceFor(VirtReg);
|
2018-11-07 14:57:02 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " to stack slot #" << FI << '\n');
|
2018-11-07 10:04:12 +08:00
|
|
|
|
|
|
|
const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
|
|
|
|
TII->storeRegToStackSlot(*MBB, Before, AssignedReg, Kill, FI, &RC, TRI);
|
|
|
|
++NumStores;
|
|
|
|
|
|
|
|
// If this register is used by DBG_VALUE then insert new DBG_VALUE to
|
|
|
|
// identify spilled location as the place to find corresponding variable's
|
|
|
|
// value.
|
|
|
|
SmallVectorImpl<MachineInstr *> &LRIDbgValues = LiveDbgValueMap[VirtReg];
|
|
|
|
for (MachineInstr *DBG : LRIDbgValues) {
|
|
|
|
MachineInstr *NewDV = buildDbgValueForSpill(*MBB, Before, *DBG, FI);
|
|
|
|
assert(NewDV->getParent() == MBB && "dangling parent pointer");
|
|
|
|
(void)NewDV;
|
|
|
|
LLVM_DEBUG(dbgs() << "Inserting debug info due to spill:\n" << *NewDV);
|
|
|
|
}
|
|
|
|
// Now this register is spilled there is should not be any DBG_VALUE
|
|
|
|
// pointing to this register because they are all pointing to spilled value
|
|
|
|
// now.
|
|
|
|
LRIDbgValues.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert reload instruction for \p PhysReg before \p Before.
|
2019-10-31 05:01:58 +08:00
|
|
|
void RegAllocFast::reload(MachineBasicBlock::iterator Before, Register VirtReg,
|
2018-11-07 10:04:12 +08:00
|
|
|
MCPhysReg PhysReg) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Reloading " << printReg(VirtReg, TRI) << " into "
|
2018-11-07 14:57:02 +08:00
|
|
|
<< printReg(PhysReg, TRI) << '\n');
|
2018-11-07 10:04:12 +08:00
|
|
|
int FI = getStackSpaceFor(VirtReg);
|
|
|
|
const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
|
|
|
|
TII->loadRegFromStackSlot(*MBB, Before, PhysReg, FI, &RC, TRI);
|
|
|
|
++NumLoads;
|
|
|
|
}
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// Return true if MO is the only remaining reference to its virtual register,
|
|
|
|
/// and it is guaranteed to be a block-local register.
|
|
|
|
bool RegAllocFast::isLastUseOfLocalReg(const MachineOperand &MO) const {
|
2010-05-15 14:09:08 +08:00
|
|
|
// If the register has ever been spilled or reloaded, we conservatively assume
|
|
|
|
// it is a global register used in multiple blocks.
|
|
|
|
if (StackSlotForVirtReg[MO.getReg()] != -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check that the use/def chain has exactly one operand - MO.
|
2012-08-09 07:44:01 +08:00
|
|
|
MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
|
2014-03-14 07:12:04 +08:00
|
|
|
if (&*I != &MO)
|
2012-08-09 07:44:01 +08:00
|
|
|
return false;
|
|
|
|
return ++I == MRI->reg_nodbg_end();
|
2010-05-15 14:09:08 +08:00
|
|
|
}
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// Set kill flags on last use of a virtual register.
|
|
|
|
void RegAllocFast::addKillFlag(const LiveReg &LR) {
|
2010-05-17 10:07:29 +08:00
|
|
|
if (!LR.LastUse) return;
|
|
|
|
MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
|
2010-05-20 05:36:05 +08:00
|
|
|
if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
|
|
|
|
if (MO.getReg() == LR.PhysReg)
|
2010-05-19 05:10:50 +08:00
|
|
|
MO.setIsKill();
|
2017-07-08 03:25:45 +08:00
|
|
|
// else, don't do anything we are problably redefining a
|
|
|
|
// subreg of this register and given we don't track which
|
|
|
|
// lanes are actually dead, we cannot insert a kill flag here.
|
|
|
|
// Otherwise we may end up in a situation like this:
|
2017-12-07 18:40:31 +08:00
|
|
|
// ... = (MO) physreg:sub1, implicit killed physreg
|
2017-07-08 03:25:45 +08:00
|
|
|
// ... <== Here we would allow later pass to reuse physreg:sub1
|
|
|
|
// which is potentially wrong.
|
|
|
|
// LR:sub0 = ...
|
|
|
|
// ... = LR.sub1 <== This is going to use physreg:sub1
|
2010-05-19 05:10:50 +08:00
|
|
|
}
|
2010-05-13 02:46:03 +08:00
|
|
|
}
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// Mark virtreg as no longer available.
|
2018-11-07 14:57:03 +08:00
|
|
|
void RegAllocFast::killVirtReg(LiveReg &LR) {
|
|
|
|
addKillFlag(LR);
|
|
|
|
assert(PhysRegState[LR.PhysReg] == LR.VirtReg &&
|
2012-02-23 00:50:46 +08:00
|
|
|
"Broken RegState mapping");
|
2018-11-07 14:57:03 +08:00
|
|
|
setPhysRegState(LR.PhysReg, regFree);
|
|
|
|
LR.PhysReg = 0;
|
2010-05-12 07:24:45 +08:00
|
|
|
}
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// Mark virtreg as no longer available.
|
2019-10-31 05:01:58 +08:00
|
|
|
void RegAllocFast::killVirtReg(Register VirtReg) {
|
2019-08-02 07:27:28 +08:00
|
|
|
assert(Register::isVirtualRegister(VirtReg) &&
|
2010-05-12 02:54:45 +08:00
|
|
|
"killVirtReg needs a virtual register");
|
2012-02-22 09:02:37 +08:00
|
|
|
LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
|
2018-11-07 14:57:03 +08:00
|
|
|
if (LRI != LiveVirtRegs.end() && LRI->PhysReg)
|
|
|
|
killVirtReg(*LRI);
|
2010-04-22 02:02:42 +08:00
|
|
|
}
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// This method spills the value specified by VirtReg into the corresponding
|
|
|
|
/// stack slot if needed.
|
|
|
|
void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI,
|
2019-10-31 05:01:58 +08:00
|
|
|
Register VirtReg) {
|
2019-08-02 07:27:28 +08:00
|
|
|
assert(Register::isVirtualRegister(VirtReg) &&
|
2010-05-12 02:54:45 +08:00
|
|
|
"Spilling a physical register is illegal!");
|
2012-02-22 09:02:37 +08:00
|
|
|
LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
|
2018-11-07 14:57:03 +08:00
|
|
|
assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
|
|
|
|
"Spilling unmapped virtual register");
|
|
|
|
spillVirtReg(MI, *LRI);
|
2010-05-14 08:02:20 +08:00
|
|
|
}
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// Do the actual work of spilling.
|
2018-11-07 14:57:03 +08:00
|
|
|
void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI, LiveReg &LR) {
|
|
|
|
assert(PhysRegState[LR.PhysReg] == LR.VirtReg && "Broken RegState mapping");
|
2010-05-12 07:24:45 +08:00
|
|
|
|
2010-05-12 07:24:47 +08:00
|
|
|
if (LR.Dirty) {
|
2010-05-17 10:07:32 +08:00
|
|
|
// If this physreg is used by the instruction, we want to kill it on the
|
|
|
|
// instruction, not on the spill.
|
2016-07-01 23:03:37 +08:00
|
|
|
bool SpillKill = MachineBasicBlock::iterator(LR.LastUse) != MI;
|
2010-05-12 07:24:47 +08:00
|
|
|
LR.Dirty = false;
|
2018-11-07 10:04:12 +08:00
|
|
|
|
2018-11-07 14:57:03 +08:00
|
|
|
spill(MI, LR.VirtReg, LR.PhysReg, SpillKill);
|
2018-11-07 10:04:12 +08:00
|
|
|
|
2010-05-17 10:49:15 +08:00
|
|
|
if (SpillKill)
|
2014-04-14 08:51:57 +08:00
|
|
|
LR.LastUse = nullptr; // Don't kill register again
|
2010-05-12 02:54:45 +08:00
|
|
|
}
|
2018-11-07 14:57:03 +08:00
|
|
|
killVirtReg(LR);
|
2010-04-22 02:02:42 +08:00
|
|
|
}
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// Spill all dirty virtregs without killing them.
|
2019-05-04 03:06:57 +08:00
|
|
|
void RegAllocFast::spillAll(MachineBasicBlock::iterator MI, bool OnlyLiveOut) {
|
2018-11-10 08:36:27 +08:00
|
|
|
if (LiveVirtRegs.empty())
|
|
|
|
return;
|
2010-05-18 04:01:22 +08:00
|
|
|
// The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
|
|
|
|
// of spilling here is deterministic, if arbitrary.
|
2018-11-07 14:57:03 +08:00
|
|
|
for (LiveReg &LR : LiveVirtRegs) {
|
|
|
|
if (!LR.PhysReg)
|
|
|
|
continue;
|
2019-05-04 03:06:57 +08:00
|
|
|
if (OnlyLiveOut && !mayLiveOut(LR.VirtReg))
|
|
|
|
continue;
|
2018-11-07 14:57:03 +08:00
|
|
|
spillVirtReg(MI, LR);
|
|
|
|
}
|
2010-05-17 10:07:32 +08:00
|
|
|
LiveVirtRegs.clear();
|
2010-05-12 02:54:45 +08:00
|
|
|
}
|
2010-04-22 02:02:42 +08:00
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// Handle the direct use of a physical register. Check that the register is
|
|
|
|
/// not used by a virtreg. Kill the physreg, marking it free. This may add
|
|
|
|
/// implicit kills to MO->getParent() and invalidate MO.
|
|
|
|
void RegAllocFast::usePhysReg(MachineOperand &MO) {
|
2016-05-19 00:10:17 +08:00
|
|
|
// Ignore undef uses.
|
|
|
|
if (MO.isUndef())
|
|
|
|
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
|
|
|
Register PhysReg = MO.getReg();
|
2019-10-31 05:01:58 +08:00
|
|
|
assert(PhysReg.isPhysical() && "Bad usePhysReg operand");
|
2017-09-09 08:52:46 +08:00
|
|
|
|
2013-02-22 03:35:21 +08:00
|
|
|
markRegUsedInInstr(PhysReg);
|
2010-05-15 02:03:25 +08:00
|
|
|
switch (PhysRegState[PhysReg]) {
|
2010-05-12 02:54:45 +08:00
|
|
|
case regDisabled:
|
|
|
|
break;
|
|
|
|
case regReserved:
|
|
|
|
PhysRegState[PhysReg] = regFree;
|
2016-08-18 04:30:52 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2010-05-15 02:03:25 +08:00
|
|
|
case regFree:
|
|
|
|
MO.setIsKill();
|
2010-05-12 02:54:45 +08:00
|
|
|
return;
|
|
|
|
default:
|
2010-12-09 05:35:09 +08:00
|
|
|
// The physreg was allocated to a virtual register. That means the value we
|
2010-05-15 02:03:25 +08:00
|
|
|
// wanted has been clobbered.
|
|
|
|
llvm_unreachable("Instruction uses an allocated register");
|
2010-04-22 02:02:42 +08:00
|
|
|
}
|
|
|
|
|
2010-05-15 02:03:25 +08:00
|
|
|
// Maybe a superregister is reserved?
|
2012-06-02 07:28:30 +08:00
|
|
|
for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
|
2017-09-09 08:52:46 +08:00
|
|
|
MCPhysReg Alias = *AI;
|
2010-05-15 02:03:25 +08:00
|
|
|
switch (PhysRegState[Alias]) {
|
2010-05-12 02:54:45 +08:00
|
|
|
case regDisabled:
|
|
|
|
break;
|
|
|
|
case regReserved:
|
2014-12-04 07:38:08 +08:00
|
|
|
// Either PhysReg is a subregister of Alias and we mark the
|
|
|
|
// whole register as free, or PhysReg is the superregister of
|
|
|
|
// Alias and we mark all the aliases as disabled before freeing
|
|
|
|
// PhysReg.
|
|
|
|
// In the latter case, since PhysReg was disabled, this means that
|
|
|
|
// its value is defined only by physical sub-registers. This check
|
|
|
|
// is performed by the assert of the default case in this loop.
|
|
|
|
// Note: The value of the superregister may only be partial
|
|
|
|
// defined, that is why regDisabled is a valid state for aliases.
|
|
|
|
assert((TRI->isSuperRegister(PhysReg, Alias) ||
|
|
|
|
TRI->isSuperRegister(Alias, PhysReg)) &&
|
2010-05-15 02:03:25 +08:00
|
|
|
"Instruction is not using a subregister of a reserved register");
|
2016-08-18 04:30:52 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2010-05-15 02:03:25 +08:00
|
|
|
case regFree:
|
|
|
|
if (TRI->isSuperRegister(PhysReg, Alias)) {
|
|
|
|
// Leave the superregister in the working set.
|
2018-11-07 14:57:00 +08:00
|
|
|
setPhysRegState(Alias, regFree);
|
2010-05-15 02:03:25 +08:00
|
|
|
MO.getParent()->addRegisterKilled(Alias, TRI, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Some other alias was in the working set - clear it.
|
2018-11-07 14:57:00 +08:00
|
|
|
setPhysRegState(Alias, regDisabled);
|
2010-05-12 02:54:45 +08:00
|
|
|
break;
|
|
|
|
default:
|
2010-05-15 02:03:25 +08:00
|
|
|
llvm_unreachable("Instruction uses an alias of an allocated register");
|
2010-05-12 02:54:45 +08:00
|
|
|
}
|
2010-04-22 02:02:42 +08:00
|
|
|
}
|
2010-05-15 02:03:25 +08:00
|
|
|
|
|
|
|
// All aliases are disabled, bring register into working set.
|
2018-11-07 14:57:00 +08:00
|
|
|
setPhysRegState(PhysReg, regFree);
|
2010-05-15 02:03:25 +08:00
|
|
|
MO.setIsKill();
|
2010-04-22 02:02:42 +08:00
|
|
|
}
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// Mark PhysReg as reserved or free after spilling any virtregs. This is very
|
|
|
|
/// similar to defineVirtReg except the physreg is reserved instead of
|
|
|
|
/// allocated.
|
2018-01-30 07:42:37 +08:00
|
|
|
void RegAllocFast::definePhysReg(MachineBasicBlock::iterator MI,
|
|
|
|
MCPhysReg PhysReg, RegState NewState) {
|
2013-02-22 03:35:21 +08:00
|
|
|
markRegUsedInInstr(PhysReg);
|
2019-10-31 05:01:58 +08:00
|
|
|
switch (Register VirtReg = PhysRegState[PhysReg]) {
|
2010-05-12 02:54:45 +08:00
|
|
|
case regDisabled:
|
|
|
|
break;
|
2010-05-15 02:03:25 +08:00
|
|
|
default:
|
2010-05-17 10:07:32 +08:00
|
|
|
spillVirtReg(MI, VirtReg);
|
2016-08-18 04:30:52 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2010-05-12 02:54:45 +08:00
|
|
|
case regFree:
|
|
|
|
case regReserved:
|
2018-11-07 14:57:00 +08:00
|
|
|
setPhysRegState(PhysReg, NewState);
|
2010-05-12 02:54:45 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-15 02:03:25 +08:00
|
|
|
// This is a disabled register, disable all aliases.
|
2018-11-07 14:57:00 +08:00
|
|
|
setPhysRegState(PhysReg, NewState);
|
2012-06-02 07:28:30 +08:00
|
|
|
for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
|
2017-09-09 08:52:46 +08:00
|
|
|
MCPhysReg Alias = *AI;
|
2019-10-31 05:01:58 +08:00
|
|
|
switch (Register VirtReg = PhysRegState[Alias]) {
|
2010-05-12 02:54:45 +08:00
|
|
|
case regDisabled:
|
|
|
|
break;
|
|
|
|
default:
|
2010-05-17 10:07:32 +08:00
|
|
|
spillVirtReg(MI, VirtReg);
|
2016-08-18 04:30:52 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2010-05-15 02:03:25 +08:00
|
|
|
case regFree:
|
|
|
|
case regReserved:
|
2018-11-07 14:57:00 +08:00
|
|
|
setPhysRegState(Alias, regDisabled);
|
2010-05-15 02:03:25 +08:00
|
|
|
if (TRI->isSuperRegister(PhysReg, Alias))
|
|
|
|
return;
|
2010-05-12 02:54:45 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-04-22 02:02:42 +08:00
|
|
|
|
2018-11-10 08:36:27 +08:00
|
|
|
/// Return the cost of spilling clearing out PhysReg and aliases so it is free
|
|
|
|
/// for allocation. Returns 0 when PhysReg is free or disabled with all aliases
|
|
|
|
/// disabled - it can be allocated directly.
|
2017-09-09 08:52:46 +08:00
|
|
|
/// \returns spillImpossible when PhysReg or an alias can't be spilled.
|
|
|
|
unsigned RegAllocFast::calcSpillCost(MCPhysReg PhysReg) const {
|
2013-02-22 03:35:21 +08:00
|
|
|
if (isRegUsedInInstr(PhysReg)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI)
|
|
|
|
<< " is already used in instr.\n");
|
2010-05-18 05:02:08 +08:00
|
|
|
return spillImpossible;
|
2011-04-13 06:17:44 +08:00
|
|
|
}
|
2019-10-31 05:01:58 +08:00
|
|
|
switch (Register VirtReg = PhysRegState[PhysReg]) {
|
2010-05-17 23:30:32 +08:00
|
|
|
case regDisabled:
|
|
|
|
break;
|
|
|
|
case regFree:
|
|
|
|
return 0;
|
|
|
|
case regReserved:
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << printReg(VirtReg, TRI) << " corresponding "
|
|
|
|
<< printReg(PhysReg, TRI) << " is reserved already.\n");
|
2010-05-17 23:30:32 +08:00
|
|
|
return spillImpossible;
|
2012-02-22 09:02:37 +08:00
|
|
|
default: {
|
2018-11-07 14:57:03 +08:00
|
|
|
LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg);
|
|
|
|
assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
|
|
|
|
"Missing VirtReg entry");
|
|
|
|
return LRI->Dirty ? spillDirty : spillClean;
|
2012-02-22 09:02:37 +08:00
|
|
|
}
|
2010-05-17 23:30:32 +08:00
|
|
|
}
|
|
|
|
|
2011-04-12 08:48:08 +08:00
|
|
|
// This is a disabled register, add up cost of aliases.
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << " is disabled.\n");
|
2010-05-17 23:30:32 +08:00
|
|
|
unsigned Cost = 0;
|
2012-06-02 07:28:30 +08:00
|
|
|
for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
|
2017-09-09 08:52:46 +08:00
|
|
|
MCPhysReg Alias = *AI;
|
2019-10-31 05:01:58 +08:00
|
|
|
switch (Register VirtReg = PhysRegState[Alias]) {
|
2010-05-17 23:30:32 +08:00
|
|
|
case regDisabled:
|
|
|
|
break;
|
|
|
|
case regFree:
|
|
|
|
++Cost;
|
|
|
|
break;
|
|
|
|
case regReserved:
|
|
|
|
return spillImpossible;
|
2012-02-22 09:02:37 +08:00
|
|
|
default: {
|
2018-11-07 14:57:03 +08:00
|
|
|
LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg);
|
|
|
|
assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
|
|
|
|
"Missing VirtReg entry");
|
|
|
|
Cost += LRI->Dirty ? spillDirty : spillClean;
|
2010-05-17 23:30:32 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-02-22 09:02:37 +08:00
|
|
|
}
|
2010-05-17 23:30:32 +08:00
|
|
|
}
|
|
|
|
return Cost;
|
|
|
|
}
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// This method updates local state so that we know that PhysReg is the
|
2017-09-09 08:52:46 +08:00
|
|
|
/// proper container for VirtReg now. The physical register must not be used
|
|
|
|
/// for anything else when this is called.
|
|
|
|
void RegAllocFast::assignVirtToPhysReg(LiveReg &LR, MCPhysReg PhysReg) {
|
2019-10-31 05:01:58 +08:00
|
|
|
Register VirtReg = LR.VirtReg;
|
2018-11-07 14:57:00 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Assigning " << printReg(VirtReg, TRI) << " to "
|
2018-11-07 14:57:02 +08:00
|
|
|
<< printReg(PhysReg, TRI) << '\n');
|
2018-11-07 14:57:00 +08:00
|
|
|
assert(LR.PhysReg == 0 && "Already assigned a physreg");
|
|
|
|
assert(PhysReg != 0 && "Trying to assign no register");
|
2012-02-22 09:02:37 +08:00
|
|
|
LR.PhysReg = PhysReg;
|
2018-11-07 14:57:00 +08:00
|
|
|
setPhysRegState(PhysReg, VirtReg);
|
2012-02-22 09:02:37 +08:00
|
|
|
}
|
|
|
|
|
2019-05-16 20:50:39 +08:00
|
|
|
static bool isCoalescable(const MachineInstr &MI) {
|
|
|
|
return MI.isFullCopy();
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:01:58 +08:00
|
|
|
Register RegAllocFast::traceCopyChain(Register Reg) const {
|
2019-05-16 20:50:39 +08:00
|
|
|
static const unsigned ChainLengthLimit = 3;
|
|
|
|
unsigned C = 0;
|
|
|
|
do {
|
2019-10-31 05:01:58 +08:00
|
|
|
if (Reg.isPhysical())
|
2019-05-16 20:50:39 +08:00
|
|
|
return Reg;
|
2019-10-31 05:01:58 +08:00
|
|
|
assert(Reg.isVirtual());
|
2019-05-16 20:50:39 +08:00
|
|
|
|
|
|
|
MachineInstr *VRegDef = MRI->getUniqueVRegDef(Reg);
|
|
|
|
if (!VRegDef || !isCoalescable(*VRegDef))
|
|
|
|
return 0;
|
|
|
|
Reg = VRegDef->getOperand(1).getReg();
|
|
|
|
} while (++C <= ChainLengthLimit);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if any of \p VirtReg's definitions is a copy. If it is follow the
|
|
|
|
/// chain of copies to check whether we reach a physical register we can
|
|
|
|
/// coalesce with.
|
2019-10-31 05:01:58 +08:00
|
|
|
Register RegAllocFast::traceCopies(Register VirtReg) const {
|
2019-05-16 20:50:39 +08:00
|
|
|
static const unsigned DefLimit = 3;
|
|
|
|
unsigned C = 0;
|
|
|
|
for (const MachineInstr &MI : MRI->def_instructions(VirtReg)) {
|
|
|
|
if (isCoalescable(MI)) {
|
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 Reg = MI.getOperand(1).getReg();
|
2019-05-16 20:50:39 +08:00
|
|
|
Reg = traceCopyChain(Reg);
|
2019-10-31 05:01:58 +08:00
|
|
|
if (Reg.isValid())
|
2019-05-16 20:50:39 +08:00
|
|
|
return Reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (++C >= DefLimit)
|
|
|
|
break;
|
|
|
|
}
|
2019-10-31 05:01:58 +08:00
|
|
|
return Register();
|
2019-05-16 20:50:39 +08:00
|
|
|
}
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// Allocates a physical register for VirtReg.
|
2019-10-31 05:01:58 +08:00
|
|
|
void RegAllocFast::allocVirtReg(MachineInstr &MI, LiveReg &LR, Register Hint0) {
|
|
|
|
const Register VirtReg = LR.VirtReg;
|
2010-05-17 10:07:29 +08:00
|
|
|
|
2019-08-02 07:27:28 +08:00
|
|
|
assert(Register::isVirtualRegister(VirtReg) &&
|
2010-05-12 02:54:45 +08:00
|
|
|
"Can only allocate virtual registers");
|
2010-04-22 02:02:42 +08:00
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
|
2018-11-07 14:57:02 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Search register for " << printReg(VirtReg)
|
2019-03-18 05:31:40 +08:00
|
|
|
<< " in class " << TRI->getRegClassName(&RC)
|
2019-05-16 20:50:39 +08:00
|
|
|
<< " with hint " << printReg(Hint0, TRI) << '\n');
|
2018-11-07 14:57:02 +08:00
|
|
|
|
|
|
|
// Take hint when possible.
|
2019-10-31 05:01:58 +08:00
|
|
|
if (Hint0.isPhysical() && MRI->isAllocatable(Hint0) &&
|
2019-08-02 07:27:28 +08:00
|
|
|
RC.contains(Hint0)) {
|
2011-06-13 11:26:46 +08:00
|
|
|
// Ignore the hint if we would have to spill a dirty register.
|
2019-05-16 20:50:39 +08:00
|
|
|
unsigned Cost = calcSpillCost(Hint0);
|
2011-06-13 11:26:46 +08:00
|
|
|
if (Cost < spillDirty) {
|
2019-05-16 20:50:39 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\tPreferred Register 1: " << printReg(Hint0, TRI)
|
|
|
|
<< '\n');
|
2011-06-13 11:26:46 +08:00
|
|
|
if (Cost)
|
2019-05-16 20:50:39 +08:00
|
|
|
definePhysReg(MI, Hint0, regFree);
|
|
|
|
assignVirtToPhysReg(LR, Hint0);
|
2018-11-07 14:57:03 +08:00
|
|
|
return;
|
2019-05-16 20:50:39 +08:00
|
|
|
} else {
|
|
|
|
LLVM_DEBUG(dbgs() << "\tPreferred Register 1: " << printReg(Hint0, TRI)
|
|
|
|
<< "occupied\n");
|
2010-05-13 08:19:43 +08:00
|
|
|
}
|
2019-05-16 20:50:39 +08:00
|
|
|
} else {
|
2019-10-31 05:01:58 +08:00
|
|
|
Hint0 = Register();
|
2019-05-16 20:50:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try other hint.
|
2019-10-31 05:01:58 +08:00
|
|
|
Register Hint1 = traceCopies(VirtReg);
|
|
|
|
if (Hint1.isPhysical() && MRI->isAllocatable(Hint1) &&
|
2019-08-02 07:27:28 +08:00
|
|
|
RC.contains(Hint1) && !isRegUsedInInstr(Hint1)) {
|
2019-05-16 20:50:39 +08:00
|
|
|
// Ignore the hint if we would have to spill a dirty register.
|
|
|
|
unsigned Cost = calcSpillCost(Hint1);
|
|
|
|
if (Cost < spillDirty) {
|
|
|
|
LLVM_DEBUG(dbgs() << "\tPreferred Register 0: " << printReg(Hint1, TRI)
|
|
|
|
<< '\n');
|
|
|
|
if (Cost)
|
|
|
|
definePhysReg(MI, Hint1, regFree);
|
|
|
|
assignVirtToPhysReg(LR, Hint1);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
LLVM_DEBUG(dbgs() << "\tPreferred Register 0: " << printReg(Hint1, TRI)
|
|
|
|
<< "occupied\n");
|
|
|
|
}
|
|
|
|
} else {
|
2019-10-31 05:01:58 +08:00
|
|
|
Hint1 = Register();
|
2010-05-13 08:19:43 +08:00
|
|
|
}
|
|
|
|
|
2018-11-10 08:36:27 +08:00
|
|
|
MCPhysReg BestReg = 0;
|
2017-09-09 08:52:46 +08:00
|
|
|
unsigned BestCost = spillImpossible;
|
2019-03-20 03:01:34 +08:00
|
|
|
ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC);
|
2018-11-07 14:57:02 +08:00
|
|
|
for (MCPhysReg PhysReg : AllocationOrder) {
|
|
|
|
LLVM_DEBUG(dbgs() << "\tRegister: " << printReg(PhysReg, TRI) << ' ');
|
2017-09-09 08:52:46 +08:00
|
|
|
unsigned Cost = calcSpillCost(PhysReg);
|
2018-11-07 14:57:02 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Cost: " << Cost << " BestCost: " << BestCost << '\n');
|
2018-11-10 08:36:27 +08:00
|
|
|
// Immediate take a register with cost 0.
|
2012-02-22 09:02:37 +08:00
|
|
|
if (Cost == 0) {
|
2018-11-07 14:57:03 +08:00
|
|
|
assignVirtToPhysReg(LR, PhysReg);
|
|
|
|
return;
|
2012-02-22 09:02:37 +08:00
|
|
|
}
|
2019-05-16 20:50:39 +08:00
|
|
|
|
|
|
|
if (PhysReg == Hint1 || PhysReg == Hint0)
|
|
|
|
Cost -= spillPrefBonus;
|
|
|
|
|
2018-11-07 14:57:02 +08:00
|
|
|
if (Cost < BestCost) {
|
|
|
|
BestReg = PhysReg;
|
|
|
|
BestCost = Cost;
|
|
|
|
}
|
2010-04-22 02:02:42 +08:00
|
|
|
}
|
|
|
|
|
2018-11-07 14:57:02 +08:00
|
|
|
if (!BestReg) {
|
2018-11-10 08:36:27 +08:00
|
|
|
// Nothing we can do: Report an error and keep going with an invalid
|
|
|
|
// allocation.
|
2018-11-07 14:57:02 +08:00
|
|
|
if (MI.isInlineAsm())
|
|
|
|
MI.emitError("inline assembly requires more registers than available");
|
|
|
|
else
|
|
|
|
MI.emitError("ran out of registers during register allocation");
|
|
|
|
definePhysReg(MI, *AllocationOrder.begin(), regFree);
|
2018-11-07 14:57:03 +08:00
|
|
|
assignVirtToPhysReg(LR, *AllocationOrder.begin());
|
|
|
|
return;
|
2010-05-12 02:54:45 +08:00
|
|
|
}
|
2010-04-22 02:02:42 +08:00
|
|
|
|
2018-11-07 14:57:02 +08:00
|
|
|
definePhysReg(MI, BestReg, regFree);
|
2018-11-07 14:57:03 +08:00
|
|
|
assignVirtToPhysReg(LR, BestReg);
|
2010-04-22 02:02:42 +08:00
|
|
|
}
|
|
|
|
|
2019-03-20 03:16:04 +08:00
|
|
|
void RegAllocFast::allocVirtRegUndef(MachineOperand &MO) {
|
|
|
|
assert(MO.isUndef() && "expected undef use");
|
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 VirtReg = MO.getReg();
|
2019-08-02 07:27:28 +08:00
|
|
|
assert(Register::isVirtualRegister(VirtReg) && "Expected virtreg");
|
2019-03-20 03:16:04 +08:00
|
|
|
|
|
|
|
LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg);
|
|
|
|
MCPhysReg PhysReg;
|
|
|
|
if (LRI != LiveVirtRegs.end() && LRI->PhysReg) {
|
|
|
|
PhysReg = LRI->PhysReg;
|
|
|
|
} else {
|
|
|
|
const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
|
|
|
|
ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC);
|
|
|
|
assert(!AllocationOrder.empty() && "Allocation order must not be empty");
|
|
|
|
PhysReg = AllocationOrder[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned SubRegIdx = MO.getSubReg();
|
|
|
|
if (SubRegIdx != 0) {
|
|
|
|
PhysReg = TRI->getSubReg(PhysReg, SubRegIdx);
|
|
|
|
MO.setSubReg(0);
|
|
|
|
}
|
|
|
|
MO.setReg(PhysReg);
|
|
|
|
MO.setIsRenamable(true);
|
|
|
|
}
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// Allocates a register for VirtReg and mark it as dirty.
|
2018-11-07 14:57:03 +08:00
|
|
|
MCPhysReg RegAllocFast::defineVirtReg(MachineInstr &MI, unsigned OpNum,
|
2019-10-31 05:01:58 +08:00
|
|
|
Register VirtReg, Register Hint) {
|
2019-08-02 07:27:28 +08:00
|
|
|
assert(Register::isVirtualRegister(VirtReg) && "Not a virtual register");
|
2010-05-17 10:49:15 +08:00
|
|
|
LiveRegMap::iterator LRI;
|
2010-05-17 10:07:29 +08:00
|
|
|
bool New;
|
2014-03-02 21:30:33 +08:00
|
|
|
std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
|
2018-11-07 14:57:03 +08:00
|
|
|
if (!LRI->PhysReg) {
|
2010-05-17 12:50:57 +08:00
|
|
|
// If there is no hint, peek at the only use of this register.
|
2019-10-31 05:01:58 +08:00
|
|
|
if ((!Hint || !Hint.isPhysical()) &&
|
2010-05-17 12:50:57 +08:00
|
|
|
MRI->hasOneNonDBGUse(VirtReg)) {
|
2014-03-14 07:12:04 +08:00
|
|
|
const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
|
2010-05-17 12:50:57 +08:00
|
|
|
// It's a copy, use the destination register as a hint.
|
2010-07-03 08:04:37 +08:00
|
|
|
if (UseMI.isCopyLike())
|
|
|
|
Hint = UseMI.getOperand(0).getReg();
|
2010-05-17 12:50:57 +08:00
|
|
|
}
|
2018-11-07 14:57:03 +08:00
|
|
|
allocVirtReg(MI, *LRI, Hint);
|
2012-02-22 09:02:37 +08:00
|
|
|
} else if (LRI->LastUse) {
|
2010-05-19 05:10:50 +08:00
|
|
|
// Redefining a live register - kill at the last use, unless it is this
|
|
|
|
// instruction defining VirtReg multiple times.
|
2016-07-01 23:03:37 +08:00
|
|
|
if (LRI->LastUse != &MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
|
2012-02-22 09:02:37 +08:00
|
|
|
addKillFlag(*LRI);
|
2010-05-19 05:10:50 +08:00
|
|
|
}
|
2012-02-22 09:02:37 +08:00
|
|
|
assert(LRI->PhysReg && "Register not assigned");
|
2016-07-01 23:03:37 +08:00
|
|
|
LRI->LastUse = &MI;
|
2012-02-22 09:02:37 +08:00
|
|
|
LRI->LastOpNum = OpNum;
|
|
|
|
LRI->Dirty = true;
|
2013-02-22 03:35:21 +08:00
|
|
|
markRegUsedInInstr(LRI->PhysReg);
|
2018-11-07 14:57:03 +08:00
|
|
|
return LRI->PhysReg;
|
2010-05-12 02:54:45 +08:00
|
|
|
}
|
2010-04-22 02:02:42 +08:00
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// Make sure VirtReg is available in a physreg and return it.
|
2018-11-07 14:57:03 +08:00
|
|
|
RegAllocFast::LiveReg &RegAllocFast::reloadVirtReg(MachineInstr &MI,
|
|
|
|
unsigned OpNum,
|
2019-10-31 05:01:58 +08:00
|
|
|
Register VirtReg,
|
|
|
|
Register Hint) {
|
2019-08-02 07:27:28 +08:00
|
|
|
assert(Register::isVirtualRegister(VirtReg) && "Not a virtual register");
|
2010-05-17 10:49:15 +08:00
|
|
|
LiveRegMap::iterator LRI;
|
2010-05-17 10:07:29 +08:00
|
|
|
bool New;
|
2014-03-02 21:30:33 +08:00
|
|
|
std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
|
2016-07-01 23:03:37 +08:00
|
|
|
MachineOperand &MO = MI.getOperand(OpNum);
|
2018-11-07 14:57:03 +08:00
|
|
|
if (!LRI->PhysReg) {
|
|
|
|
allocVirtReg(MI, *LRI, Hint);
|
2018-11-07 10:04:12 +08:00
|
|
|
reload(MI, VirtReg, LRI->PhysReg);
|
2012-02-22 09:02:37 +08:00
|
|
|
} else if (LRI->Dirty) {
|
2010-05-15 14:09:08 +08:00
|
|
|
if (isLastUseOfLocalReg(MO)) {
|
2018-11-07 14:57:02 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Killing last use: " << MO << '\n');
|
2010-06-30 03:15:30 +08:00
|
|
|
if (MO.isUse())
|
|
|
|
MO.setIsKill();
|
|
|
|
else
|
|
|
|
MO.setIsDead();
|
2010-05-15 14:09:08 +08:00
|
|
|
} else if (MO.isKill()) {
|
2018-11-07 14:57:02 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Clearing dubious kill: " << MO << '\n');
|
2010-05-15 14:09:08 +08:00
|
|
|
MO.setIsKill(false);
|
2010-06-30 03:15:30 +08:00
|
|
|
} else if (MO.isDead()) {
|
2018-11-07 14:57:02 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Clearing dubious dead: " << MO << '\n');
|
2010-06-30 03:15:30 +08:00
|
|
|
MO.setIsDead(false);
|
2010-05-15 14:09:08 +08:00
|
|
|
}
|
2010-05-17 11:26:06 +08:00
|
|
|
} else if (MO.isKill()) {
|
|
|
|
// We must remove kill flags from uses of reloaded registers because the
|
|
|
|
// register would be killed immediately, and there might be a second use:
|
2017-12-07 18:40:31 +08:00
|
|
|
// %foo = OR killed %x, %x
|
2010-05-17 11:26:06 +08:00
|
|
|
// This would cause a second reload of %x into a different register.
|
2018-11-07 14:57:02 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Clearing clean kill: " << MO << '\n');
|
2010-05-17 11:26:06 +08:00
|
|
|
MO.setIsKill(false);
|
2010-06-30 03:15:30 +08:00
|
|
|
} else if (MO.isDead()) {
|
2018-11-07 14:57:02 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Clearing clean dead: " << MO << '\n');
|
2010-06-30 03:15:30 +08:00
|
|
|
MO.setIsDead(false);
|
2010-04-22 02:02:42 +08:00
|
|
|
}
|
2012-02-22 09:02:37 +08:00
|
|
|
assert(LRI->PhysReg && "Register not assigned");
|
2016-07-01 23:03:37 +08:00
|
|
|
LRI->LastUse = &MI;
|
2012-02-22 09:02:37 +08:00
|
|
|
LRI->LastOpNum = OpNum;
|
2013-02-22 03:35:21 +08:00
|
|
|
markRegUsedInInstr(LRI->PhysReg);
|
2018-11-07 14:57:03 +08:00
|
|
|
return *LRI;
|
2010-05-12 02:54:45 +08:00
|
|
|
}
|
2010-04-22 02:02:42 +08:00
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
/// Changes operand OpNum in MI the refer the PhysReg, considering subregs. This
|
|
|
|
/// may invalidate any operand pointers. Return true if the operand kills its
|
|
|
|
/// register.
|
2018-11-10 08:36:27 +08:00
|
|
|
bool RegAllocFast::setPhysReg(MachineInstr &MI, MachineOperand &MO,
|
2017-09-09 08:52:46 +08:00
|
|
|
MCPhysReg PhysReg) {
|
2012-05-15 05:30:58 +08:00
|
|
|
bool Dead = MO.isDead();
|
2010-05-17 10:49:21 +08:00
|
|
|
if (!MO.getSubReg()) {
|
2010-05-12 02:54:45 +08:00
|
|
|
MO.setReg(PhysReg);
|
[MachineOperand][Target] MachineOperand::isRenamable semantics changes
Summary:
Add a target option AllowRegisterRenaming that is used to opt in to
post-register-allocation renaming of registers. This is set to 0 by
default, which causes the hasExtraSrcRegAllocReq/hasExtraDstRegAllocReq
fields of all opcodes to be set to 1, causing
MachineOperand::isRenamable to always return false.
Set the AllowRegisterRenaming flag to 1 for all in-tree targets that
have lit tests that were effected by enabling COPY forwarding in
MachineCopyPropagation (AArch64, AMDGPU, ARM, Hexagon, Mips, PowerPC,
RISCV, Sparc, SystemZ and X86).
Add some more comments describing the semantics of the
MachineOperand::isRenamable function and how it is set and maintained.
Change isRenamable to check the operand's opcode
hasExtraSrcRegAllocReq/hasExtraDstRegAllocReq bit directly instead of
relying on it being consistently reflected in the IsRenamable bit
setting.
Clear the IsRenamable bit when changing an operand's register value.
Remove target code that was clearing the IsRenamable bit when changing
registers/opcodes now that this is done conservatively by default.
Change setting of hasExtraSrcRegAllocReq in AMDGPU target to be done in
one place covering all opcodes that have constant pipe read limit
restrictions.
Reviewers: qcolombet, MatzeB
Subscribers: aemerson, arsenm, jyknight, mcrosier, sdardis, nhaehnle, javed.absar, tpr, arichardson, kristof.beyls, kbarton, fedor.sergeev, asb, rbar, johnrusso, simoncook, jordy.potman.lists, apazos, sabuasal, niosHD, escha, nemanjai, llvm-commits
Differential Revision: https://reviews.llvm.org/D43042
llvm-svn: 325931
2018-02-24 02:25:08 +08:00
|
|
|
MO.setIsRenamable(true);
|
2012-05-15 05:30:58 +08:00
|
|
|
return MO.isKill() || Dead;
|
2010-05-17 10:49:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle subregister index.
|
2019-08-03 04:23:00 +08:00
|
|
|
MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : Register());
|
[MachineOperand][Target] MachineOperand::isRenamable semantics changes
Summary:
Add a target option AllowRegisterRenaming that is used to opt in to
post-register-allocation renaming of registers. This is set to 0 by
default, which causes the hasExtraSrcRegAllocReq/hasExtraDstRegAllocReq
fields of all opcodes to be set to 1, causing
MachineOperand::isRenamable to always return false.
Set the AllowRegisterRenaming flag to 1 for all in-tree targets that
have lit tests that were effected by enabling COPY forwarding in
MachineCopyPropagation (AArch64, AMDGPU, ARM, Hexagon, Mips, PowerPC,
RISCV, Sparc, SystemZ and X86).
Add some more comments describing the semantics of the
MachineOperand::isRenamable function and how it is set and maintained.
Change isRenamable to check the operand's opcode
hasExtraSrcRegAllocReq/hasExtraDstRegAllocReq bit directly instead of
relying on it being consistently reflected in the IsRenamable bit
setting.
Clear the IsRenamable bit when changing an operand's register value.
Remove target code that was clearing the IsRenamable bit when changing
registers/opcodes now that this is done conservatively by default.
Change setting of hasExtraSrcRegAllocReq in AMDGPU target to be done in
one place covering all opcodes that have constant pipe read limit
restrictions.
Reviewers: qcolombet, MatzeB
Subscribers: aemerson, arsenm, jyknight, mcrosier, sdardis, nhaehnle, javed.absar, tpr, arichardson, kristof.beyls, kbarton, fedor.sergeev, asb, rbar, johnrusso, simoncook, jordy.potman.lists, apazos, sabuasal, niosHD, escha, nemanjai, llvm-commits
Differential Revision: https://reviews.llvm.org/D43042
llvm-svn: 325931
2018-02-24 02:25:08 +08:00
|
|
|
MO.setIsRenamable(true);
|
2010-05-17 10:49:21 +08:00
|
|
|
MO.setSubReg(0);
|
2010-05-20 05:36:05 +08:00
|
|
|
|
|
|
|
// A kill flag implies killing the full register. Add corresponding super
|
|
|
|
// register kill.
|
|
|
|
if (MO.isKill()) {
|
2017-09-09 08:52:46 +08:00
|
|
|
MI.addRegisterKilled(PhysReg, TRI, true);
|
2010-05-17 10:49:21 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-05-15 05:10:25 +08:00
|
|
|
|
|
|
|
// A <def,read-undef> of a sub-register requires an implicit def of the full
|
|
|
|
// register.
|
|
|
|
if (MO.isDef() && MO.isUndef())
|
2017-09-09 08:52:46 +08:00
|
|
|
MI.addRegisterDefined(PhysReg, TRI);
|
2012-05-15 05:10:25 +08:00
|
|
|
|
2012-05-15 05:30:58 +08:00
|
|
|
return Dead;
|
2010-04-22 02:02:42 +08:00
|
|
|
}
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
// Handles special instruction operand like early clobbers and tied ops when
|
2010-06-29 02:34:34 +08:00
|
|
|
// there are additional physreg defines.
|
2017-09-09 08:52:46 +08:00
|
|
|
void RegAllocFast::handleThroughOperands(MachineInstr &MI,
|
2019-10-31 05:01:58 +08:00
|
|
|
SmallVectorImpl<Register> &VirtDead) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Scanning for through registers:");
|
2019-10-31 05:01:58 +08:00
|
|
|
SmallSet<Register, 8> ThroughRegs;
|
2017-09-09 08:52:46 +08:00
|
|
|
for (const MachineOperand &MO : MI.operands()) {
|
2010-06-29 02:34:34 +08:00
|
|
|
if (!MO.isReg()) continue;
|
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 Reg = MO.getReg();
|
2019-10-31 05:01:58 +08:00
|
|
|
if (!Reg.isVirtual())
|
2011-01-10 10:58:51 +08:00
|
|
|
continue;
|
2017-09-09 08:52:46 +08:00
|
|
|
if (MO.isEarlyClobber() || (MO.isUse() && MO.isTied()) ||
|
|
|
|
(MO.getSubReg() && MI.readsVirtualRegister(Reg))) {
|
2014-11-19 15:49:26 +08:00
|
|
|
if (ThroughRegs.insert(Reg).second)
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << ' ' << printReg(Reg));
|
2010-06-29 02:34:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If any physreg defines collide with preallocated through registers,
|
|
|
|
// we must spill and reallocate.
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
|
2017-09-09 08:52:46 +08:00
|
|
|
for (const MachineOperand &MO : MI.operands()) {
|
2010-06-29 02:34:34 +08:00
|
|
|
if (!MO.isReg() || !MO.isDef()) continue;
|
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 Reg = MO.getReg();
|
2019-10-31 05:01:58 +08:00
|
|
|
if (!Reg || !Reg.isPhysical())
|
2019-08-02 07:27:28 +08:00
|
|
|
continue;
|
2013-02-22 03:35:21 +08:00
|
|
|
markRegUsedInInstr(Reg);
|
2012-06-02 06:38:17 +08:00
|
|
|
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
|
|
|
|
if (ThroughRegs.count(PhysRegState[*AI]))
|
2017-09-09 08:52:46 +08:00
|
|
|
definePhysReg(MI, *AI, regFree);
|
2010-06-29 02:34:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:01:58 +08:00
|
|
|
SmallVector<Register, 8> PartialDefs;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Allocating tied uses.\n");
|
2017-09-09 08:52:46 +08:00
|
|
|
for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
|
2018-11-10 08:36:27 +08:00
|
|
|
MachineOperand &MO = MI.getOperand(I);
|
2010-06-29 02:34:34 +08:00
|
|
|
if (!MO.isReg()) continue;
|
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 Reg = MO.getReg();
|
2019-08-02 07:27:28 +08:00
|
|
|
if (!Register::isVirtualRegister(Reg))
|
|
|
|
continue;
|
2010-06-29 02:34:34 +08:00
|
|
|
if (MO.isUse()) {
|
2017-09-09 08:52:46 +08:00
|
|
|
if (!MO.isTied()) continue;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Operand " << I << "(" << MO
|
|
|
|
<< ") is tied to operand " << MI.findTiedOperandIdx(I)
|
|
|
|
<< ".\n");
|
2018-11-07 14:57:03 +08:00
|
|
|
LiveReg &LR = reloadVirtReg(MI, I, Reg, 0);
|
|
|
|
MCPhysReg PhysReg = LR.PhysReg;
|
2018-11-10 08:36:27 +08:00
|
|
|
setPhysReg(MI, MO, PhysReg);
|
2010-06-30 03:15:30 +08:00
|
|
|
// Note: we don't update the def operand yet. That would cause the normal
|
|
|
|
// def-scan to attempt spilling.
|
2017-09-09 08:52:46 +08:00
|
|
|
} else if (MO.getSubReg() && MI.readsVirtualRegister(Reg)) {
|
2018-11-07 14:57:02 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Partial redefine: " << MO << '\n');
|
2010-06-30 03:15:30 +08:00
|
|
|
// Reload the register, but don't assign to the operand just yet.
|
|
|
|
// That would confuse the later phys-def processing pass.
|
2018-11-07 14:57:03 +08:00
|
|
|
LiveReg &LR = reloadVirtReg(MI, I, Reg, 0);
|
|
|
|
PartialDefs.push_back(LR.PhysReg);
|
2010-06-29 02:34:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Allocating early clobbers.\n");
|
2017-09-09 08:52:46 +08:00
|
|
|
for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
|
|
|
|
const MachineOperand &MO = MI.getOperand(I);
|
2011-11-22 14:27:18 +08:00
|
|
|
if (!MO.isReg()) continue;
|
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 Reg = MO.getReg();
|
2019-08-02 07:27:28 +08:00
|
|
|
if (!Register::isVirtualRegister(Reg))
|
|
|
|
continue;
|
2011-11-22 14:27:18 +08:00
|
|
|
if (!MO.isEarlyClobber())
|
|
|
|
continue;
|
|
|
|
// Note: defineVirtReg may invalidate MO.
|
2018-11-07 14:57:03 +08:00
|
|
|
MCPhysReg PhysReg = defineVirtReg(MI, I, Reg, 0);
|
2018-11-10 08:36:27 +08:00
|
|
|
if (setPhysReg(MI, MI.getOperand(I), PhysReg))
|
2011-11-22 14:27:18 +08:00
|
|
|
VirtDead.push_back(Reg);
|
|
|
|
}
|
|
|
|
|
2010-06-29 02:34:34 +08:00
|
|
|
// Restore UsedInInstr to a state usable for allocating normal virtual uses.
|
2012-10-17 09:37:59 +08:00
|
|
|
UsedInInstr.clear();
|
2017-09-09 08:52:46 +08:00
|
|
|
for (const MachineOperand &MO : MI.operands()) {
|
2010-06-29 02:34:34 +08:00
|
|
|
if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
|
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 Reg = MO.getReg();
|
2019-10-31 05:01:58 +08:00
|
|
|
if (!Reg || !Reg.isPhysical())
|
2019-08-02 07:27:28 +08:00
|
|
|
continue;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\tSetting " << printReg(Reg, TRI)
|
|
|
|
<< " as used in instr\n");
|
2013-02-22 03:35:21 +08:00
|
|
|
markRegUsedInInstr(Reg);
|
2010-06-29 02:34:34 +08:00
|
|
|
}
|
2010-06-30 03:15:30 +08:00
|
|
|
|
|
|
|
// Also mark PartialDefs as used to avoid reallocation.
|
2019-10-31 05:01:58 +08:00
|
|
|
for (Register PartialDef : PartialDefs)
|
2017-09-09 08:52:46 +08:00
|
|
|
markRegUsedInInstr(PartialDef);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
void RegAllocFast::dumpState() {
|
|
|
|
for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
|
|
|
|
if (PhysRegState[Reg] == regDisabled) continue;
|
2017-12-01 00:12:24 +08:00
|
|
|
dbgs() << " " << printReg(Reg, TRI);
|
2017-09-09 08:52:46 +08:00
|
|
|
switch(PhysRegState[Reg]) {
|
|
|
|
case regFree:
|
|
|
|
break;
|
|
|
|
case regReserved:
|
|
|
|
dbgs() << "*";
|
|
|
|
break;
|
|
|
|
default: {
|
2017-11-28 20:42:37 +08:00
|
|
|
dbgs() << '=' << printReg(PhysRegState[Reg]);
|
2018-11-07 14:57:03 +08:00
|
|
|
LiveRegMap::iterator LRI = findLiveVirtReg(PhysRegState[Reg]);
|
|
|
|
assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
|
|
|
|
"Missing VirtReg entry");
|
|
|
|
if (LRI->Dirty)
|
2017-09-09 08:52:46 +08:00
|
|
|
dbgs() << "*";
|
2018-11-07 14:57:03 +08:00
|
|
|
assert(LRI->PhysReg == Reg && "Bad inverse map");
|
2017-09-09 08:52:46 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dbgs() << '\n';
|
|
|
|
// Check that LiveVirtRegs is the inverse.
|
|
|
|
for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
|
|
|
|
e = LiveVirtRegs.end(); i != e; ++i) {
|
2018-11-07 14:57:03 +08:00
|
|
|
if (!i->PhysReg)
|
|
|
|
continue;
|
2019-10-31 05:01:58 +08:00
|
|
|
assert(i->VirtReg.isVirtual() && "Bad map key");
|
2019-08-02 07:27:28 +08:00
|
|
|
assert(Register::isPhysicalRegister(i->PhysReg) && "Bad map value");
|
2017-09-09 08:52:46 +08:00
|
|
|
assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
|
|
|
|
}
|
2010-06-29 02:34:34 +08:00
|
|
|
}
|
2017-09-09 08:52:46 +08:00
|
|
|
#endif
|
2010-06-29 02:34:34 +08:00
|
|
|
|
2018-11-10 08:36:27 +08:00
|
|
|
void RegAllocFast::allocateInstruction(MachineInstr &MI) {
|
|
|
|
const MCInstrDesc &MCID = MI.getDesc();
|
|
|
|
|
|
|
|
// If this is a copy, we may be able to coalesce.
|
2019-10-31 05:01:58 +08:00
|
|
|
Register CopySrcReg;
|
|
|
|
Register CopyDstReg;
|
2018-11-10 08:36:27 +08:00
|
|
|
unsigned CopySrcSub = 0;
|
|
|
|
unsigned CopyDstSub = 0;
|
|
|
|
if (MI.isCopy()) {
|
|
|
|
CopyDstReg = MI.getOperand(0).getReg();
|
|
|
|
CopySrcReg = MI.getOperand(1).getReg();
|
|
|
|
CopyDstSub = MI.getOperand(0).getSubReg();
|
|
|
|
CopySrcSub = MI.getOperand(1).getSubReg();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Track registers used by instruction.
|
|
|
|
UsedInInstr.clear();
|
|
|
|
|
|
|
|
// First scan.
|
|
|
|
// Mark physreg uses and early clobbers as used.
|
|
|
|
// Find the end of the virtreg operands
|
|
|
|
unsigned VirtOpEnd = 0;
|
|
|
|
bool hasTiedOps = false;
|
|
|
|
bool hasEarlyClobbers = false;
|
|
|
|
bool hasPartialRedefs = false;
|
|
|
|
bool hasPhysDefs = false;
|
|
|
|
for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI.getOperand(i);
|
|
|
|
// Make sure MRI knows about registers clobbered by regmasks.
|
|
|
|
if (MO.isRegMask()) {
|
|
|
|
MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!MO.isReg()) continue;
|
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 Reg = MO.getReg();
|
2018-11-10 08:36:27 +08:00
|
|
|
if (!Reg) continue;
|
2019-08-02 07:27:28 +08:00
|
|
|
if (Register::isVirtualRegister(Reg)) {
|
2018-11-10 08:36:27 +08:00
|
|
|
VirtOpEnd = i+1;
|
|
|
|
if (MO.isUse()) {
|
|
|
|
hasTiedOps = hasTiedOps ||
|
|
|
|
MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
|
|
|
|
} else {
|
|
|
|
if (MO.isEarlyClobber())
|
|
|
|
hasEarlyClobbers = true;
|
|
|
|
if (MO.getSubReg() && MI.readsVirtualRegister(Reg))
|
|
|
|
hasPartialRedefs = true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!MRI->isAllocatable(Reg)) continue;
|
|
|
|
if (MO.isUse()) {
|
|
|
|
usePhysReg(MO);
|
|
|
|
} else if (MO.isEarlyClobber()) {
|
|
|
|
definePhysReg(MI, Reg,
|
|
|
|
(MO.isImplicit() || MO.isDead()) ? regFree : regReserved);
|
|
|
|
hasEarlyClobbers = true;
|
|
|
|
} else
|
|
|
|
hasPhysDefs = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The instruction may have virtual register operands that must be allocated
|
|
|
|
// the same register at use-time and def-time: early clobbers and tied
|
|
|
|
// operands. If there are also physical defs, these registers must avoid
|
|
|
|
// both physical defs and uses, making them more constrained than normal
|
|
|
|
// operands.
|
|
|
|
// Similarly, if there are multiple defs and tied operands, we must make
|
|
|
|
// sure the same register is allocated to uses and defs.
|
|
|
|
// We didn't detect inline asm tied operands above, so just make this extra
|
|
|
|
// pass for all inline asm.
|
|
|
|
if (MI.isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
|
|
|
|
(hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
|
|
|
|
handleThroughOperands(MI, VirtDead);
|
|
|
|
// Don't attempt coalescing when we have funny stuff going on.
|
2019-10-31 05:01:58 +08:00
|
|
|
CopyDstReg = Register();
|
2018-11-10 08:36:27 +08:00
|
|
|
// Pretend we have early clobbers so the use operands get marked below.
|
|
|
|
// This is not necessary for the common case of a single tied use.
|
|
|
|
hasEarlyClobbers = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second scan.
|
|
|
|
// Allocate virtreg uses.
|
2019-03-20 03:16:04 +08:00
|
|
|
bool HasUndefUse = false;
|
2018-11-10 08:36:27 +08:00
|
|
|
for (unsigned I = 0; I != VirtOpEnd; ++I) {
|
|
|
|
MachineOperand &MO = MI.getOperand(I);
|
|
|
|
if (!MO.isReg()) continue;
|
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 Reg = MO.getReg();
|
2019-10-31 05:01:58 +08:00
|
|
|
if (!Reg.isVirtual())
|
2019-08-02 07:27:28 +08:00
|
|
|
continue;
|
2018-11-10 08:36:27 +08:00
|
|
|
if (MO.isUse()) {
|
2019-03-20 03:16:04 +08:00
|
|
|
if (MO.isUndef()) {
|
|
|
|
HasUndefUse = true;
|
|
|
|
// There is no need to allocate a register for an undef use.
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-28 04:37:31 +08:00
|
|
|
|
|
|
|
// Populate MayLiveAcrossBlocks in case the use block is allocated before
|
|
|
|
// the def block (removing the vreg uses).
|
|
|
|
mayLiveIn(Reg);
|
|
|
|
|
2018-11-10 08:36:27 +08:00
|
|
|
LiveReg &LR = reloadVirtReg(MI, I, Reg, CopyDstReg);
|
|
|
|
MCPhysReg PhysReg = LR.PhysReg;
|
|
|
|
CopySrcReg = (CopySrcReg == Reg || CopySrcReg == PhysReg) ? PhysReg : 0;
|
|
|
|
if (setPhysReg(MI, MO, PhysReg))
|
|
|
|
killVirtReg(LR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 03:16:04 +08:00
|
|
|
// Allocate undef operands. This is a separate step because in a situation
|
|
|
|
// like ` = OP undef %X, %X` both operands need the same register assign
|
|
|
|
// so we should perform the normal assignment first.
|
|
|
|
if (HasUndefUse) {
|
|
|
|
for (MachineOperand &MO : MI.uses()) {
|
|
|
|
if (!MO.isReg() || !MO.isUse())
|
|
|
|
continue;
|
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 Reg = MO.getReg();
|
2019-10-31 05:01:58 +08:00
|
|
|
if (!Reg.isVirtual())
|
2019-03-20 03:16:04 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
assert(MO.isUndef() && "Should only have undef virtreg uses left");
|
|
|
|
allocVirtRegUndef(MO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-10 08:36:27 +08:00
|
|
|
// Track registers defined by instruction - early clobbers and tied uses at
|
|
|
|
// this point.
|
|
|
|
UsedInInstr.clear();
|
|
|
|
if (hasEarlyClobbers) {
|
|
|
|
for (const MachineOperand &MO : MI.operands()) {
|
|
|
|
if (!MO.isReg()) continue;
|
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 Reg = MO.getReg();
|
2019-10-31 05:01:58 +08:00
|
|
|
if (!Reg || !Reg.isPhysical())
|
2019-08-02 07:27:28 +08:00
|
|
|
continue;
|
2018-11-10 08:36:27 +08:00
|
|
|
// Look for physreg defs and tied uses.
|
|
|
|
if (!MO.isDef() && !MO.isTied()) continue;
|
|
|
|
markRegUsedInInstr(Reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned DefOpEnd = MI.getNumOperands();
|
|
|
|
if (MI.isCall()) {
|
|
|
|
// Spill all virtregs before a call. This serves one purpose: If an
|
|
|
|
// exception is thrown, the landing pad is going to expect to find
|
|
|
|
// registers in their spill slots.
|
|
|
|
// Note: although this is appealing to just consider all definitions
|
|
|
|
// as call-clobbered, this is not correct because some of those
|
|
|
|
// definitions may be used later on and we do not want to reuse
|
|
|
|
// those for virtual registers in between.
|
|
|
|
LLVM_DEBUG(dbgs() << " Spilling remaining registers before call.\n");
|
2019-05-04 03:06:57 +08:00
|
|
|
spillAll(MI, /*OnlyLiveOut*/ false);
|
2018-11-10 08:36:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Third scan.
|
2019-05-09 02:30:26 +08:00
|
|
|
// Mark all physreg defs as used before allocating virtreg defs.
|
|
|
|
for (unsigned I = 0; I != DefOpEnd; ++I) {
|
|
|
|
const MachineOperand &MO = MI.getOperand(I);
|
|
|
|
if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
|
|
|
|
continue;
|
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 Reg = MO.getReg();
|
2019-05-09 02:30:26 +08:00
|
|
|
|
2019-10-31 05:01:58 +08:00
|
|
|
if (!Reg || !Reg.isPhysical() || !MRI->isAllocatable(Reg))
|
2019-05-09 02:30:26 +08:00
|
|
|
continue;
|
|
|
|
definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fourth scan.
|
2018-11-10 08:36:27 +08:00
|
|
|
// Allocate defs and collect dead defs.
|
|
|
|
for (unsigned I = 0; I != DefOpEnd; ++I) {
|
|
|
|
const MachineOperand &MO = MI.getOperand(I);
|
|
|
|
if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
|
|
|
|
continue;
|
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 Reg = MO.getReg();
|
2018-11-10 08:36:27 +08:00
|
|
|
|
2019-05-09 02:30:26 +08:00
|
|
|
// We have already dealt with phys regs in the previous scan.
|
2019-10-31 05:01:58 +08:00
|
|
|
if (Reg.isPhysical())
|
2018-11-10 08:36:27 +08:00
|
|
|
continue;
|
|
|
|
MCPhysReg PhysReg = defineVirtReg(MI, I, Reg, CopySrcReg);
|
|
|
|
if (setPhysReg(MI, MI.getOperand(I), PhysReg)) {
|
|
|
|
VirtDead.push_back(Reg);
|
2019-10-31 05:01:58 +08:00
|
|
|
CopyDstReg = Register(); // cancel coalescing;
|
2018-11-10 08:36:27 +08:00
|
|
|
} else
|
|
|
|
CopyDstReg = (CopyDstReg == Reg || CopyDstReg == PhysReg) ? PhysReg : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kill dead defs after the scan to ensure that multiple defs of the same
|
|
|
|
// register are allocated identically. We didn't need to do this for uses
|
|
|
|
// because we are crerating our own kill flags, and they are always at the
|
|
|
|
// last use.
|
2019-10-31 05:01:58 +08:00
|
|
|
for (Register VirtReg : VirtDead)
|
2018-11-10 08:36:27 +08:00
|
|
|
killVirtReg(VirtReg);
|
|
|
|
VirtDead.clear();
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "<< " << MI);
|
|
|
|
if (CopyDstReg && CopyDstReg == CopySrcReg && CopyDstSub == CopySrcSub) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Mark identity copy for removal\n");
|
|
|
|
Coalesced.push_back(&MI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegAllocFast::handleDebugValue(MachineInstr &MI) {
|
|
|
|
MachineOperand &MO = MI.getOperand(0);
|
|
|
|
|
|
|
|
// Ignore DBG_VALUEs that aren't based on virtual registers. These are
|
|
|
|
// mostly constants and frame indices.
|
|
|
|
if (!MO.isReg())
|
|
|
|
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
|
|
|
Register Reg = MO.getReg();
|
2019-08-02 07:27:28 +08:00
|
|
|
if (!Register::isVirtualRegister(Reg))
|
2018-11-10 08:36:27 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// See if this virtual register has already been allocated to a physical
|
|
|
|
// register or spilled to a stack slot.
|
|
|
|
LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
|
|
|
|
if (LRI != LiveVirtRegs.end() && LRI->PhysReg) {
|
|
|
|
setPhysReg(MI, MO, LRI->PhysReg);
|
|
|
|
} else {
|
|
|
|
int SS = StackSlotForVirtReg[Reg];
|
|
|
|
if (SS != -1) {
|
|
|
|
// Modify DBG_VALUE now that the value is in a spill slot.
|
|
|
|
updateDbgValueForSpill(MI, SS);
|
|
|
|
LLVM_DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << MI);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can't allocate a physreg for a DebugValue, sorry!
|
|
|
|
LLVM_DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
|
2019-10-31 05:01:58 +08:00
|
|
|
MO.setReg(Register());
|
2018-11-10 08:36:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If Reg hasn't been spilled, put this DBG_VALUE in LiveDbgValueMap so
|
|
|
|
// that future spills of Reg will have DBG_VALUEs.
|
|
|
|
LiveDbgValueMap[Reg].push_back(&MI);
|
|
|
|
}
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
void RegAllocFast::allocateBasicBlock(MachineBasicBlock &MBB) {
|
|
|
|
this->MBB = &MBB;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\nAllocating " << MBB);
|
2011-02-05 06:44:08 +08:00
|
|
|
|
2010-05-12 02:54:45 +08:00
|
|
|
PhysRegState.assign(TRI->getNumRegs(), regDisabled);
|
2012-02-22 09:02:37 +08:00
|
|
|
assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
|
2010-04-22 02:02:42 +08:00
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
MachineBasicBlock::iterator MII = MBB.begin();
|
2010-05-12 02:54:45 +08:00
|
|
|
|
|
|
|
// Add live-in registers as live.
|
2020-01-02 00:23:21 +08:00
|
|
|
for (const MachineBasicBlock::RegisterMaskPair &LI : MBB.liveins())
|
2015-09-10 02:08:03 +08:00
|
|
|
if (MRI->isAllocatable(LI.PhysReg))
|
2018-01-30 07:42:37 +08:00
|
|
|
definePhysReg(MII, LI.PhysReg, regReserved);
|
2010-05-12 02:54:45 +08:00
|
|
|
|
2017-09-09 08:52:45 +08:00
|
|
|
VirtDead.clear();
|
|
|
|
Coalesced.clear();
|
2010-04-22 02:02:42 +08:00
|
|
|
|
|
|
|
// Otherwise, sequentially allocate each instruction in the MBB.
|
2017-09-09 08:52:46 +08:00
|
|
|
for (MachineInstr &MI : MBB) {
|
2018-11-10 08:36:27 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "\n>> " << MI << "Regs:";
|
|
|
|
dumpState()
|
|
|
|
);
|
2010-04-22 02:02:42 +08:00
|
|
|
|
2018-11-10 08:36:27 +08:00
|
|
|
// Special handling for debug values. Note that they are not allowed to
|
|
|
|
// affect codegen of the other instructions in any way.
|
2017-09-09 08:52:46 +08:00
|
|
|
if (MI.isDebugValue()) {
|
2018-11-10 08:36:27 +08:00
|
|
|
handleDebugValue(MI);
|
2018-05-09 10:42:00 +08:00
|
|
|
continue;
|
2010-06-29 02:34:34 +08:00
|
|
|
}
|
|
|
|
|
2018-11-10 08:36:27 +08:00
|
|
|
allocateInstruction(MI);
|
2010-04-22 02:02:42 +08:00
|
|
|
}
|
|
|
|
|
2010-05-12 02:54:45 +08:00
|
|
|
// Spill all physical registers holding virtual registers now.
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Spilling live registers at end of block.\n");
|
2019-05-04 03:06:57 +08:00
|
|
|
spillAll(MBB.getFirstTerminator(), /*OnlyLiveOut*/ true);
|
2010-04-22 02:02:42 +08:00
|
|
|
|
2010-05-14 12:30:51 +08:00
|
|
|
// Erase all the coalesced copies. We are delaying it until now because
|
2010-05-17 10:07:32 +08:00
|
|
|
// LiveVirtRegs might refer to the instrs.
|
2017-09-09 08:52:46 +08:00
|
|
|
for (MachineInstr *MI : Coalesced)
|
|
|
|
MBB.erase(MI);
|
2018-11-07 10:04:07 +08:00
|
|
|
NumCoalesced += Coalesced.size();
|
2010-05-14 12:30:51 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(MBB.dump());
|
2010-04-22 02:02:42 +08:00
|
|
|
}
|
|
|
|
|
2017-09-09 08:52:46 +08:00
|
|
|
bool RegAllocFast::runOnMachineFunction(MachineFunction &MF) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
|
|
|
|
<< "********** Function: " << MF.getName() << '\n');
|
2017-09-09 08:52:46 +08:00
|
|
|
MRI = &MF.getRegInfo();
|
|
|
|
const TargetSubtargetInfo &STI = MF.getSubtarget();
|
|
|
|
TRI = STI.getRegisterInfo();
|
|
|
|
TII = STI.getInstrInfo();
|
|
|
|
MFI = &MF.getFrameInfo();
|
|
|
|
MRI->freezeReservedRegs(MF);
|
|
|
|
RegClassInfo.runOnMachineFunction(MF);
|
2012-10-17 09:37:59 +08:00
|
|
|
UsedInInstr.clear();
|
2013-02-22 03:35:21 +08:00
|
|
|
UsedInInstr.setUniverse(TRI->getNumRegUnits());
|
2010-04-22 02:02:42 +08:00
|
|
|
|
|
|
|
// initialize the virtual->physical register map to have a 'null'
|
|
|
|
// mapping for all virtual registers
|
2017-09-09 08:52:46 +08:00
|
|
|
unsigned NumVirtRegs = MRI->getNumVirtRegs();
|
|
|
|
StackSlotForVirtReg.resize(NumVirtRegs);
|
|
|
|
LiveVirtRegs.setUniverse(NumVirtRegs);
|
2019-05-04 03:06:57 +08:00
|
|
|
MayLiveAcrossBlocks.clear();
|
|
|
|
MayLiveAcrossBlocks.resize(NumVirtRegs);
|
2010-04-22 02:02:42 +08:00
|
|
|
|
|
|
|
// Loop over all of the basic blocks, eliminating virtual register references
|
2017-09-09 08:52:46 +08:00
|
|
|
for (MachineBasicBlock &MBB : MF)
|
|
|
|
allocateBasicBlock(MBB);
|
2010-04-22 02:02:42 +08:00
|
|
|
|
2012-02-21 12:51:23 +08:00
|
|
|
// All machine operands and other references to virtual registers have been
|
|
|
|
// replaced. Remove the virtual registers.
|
|
|
|
MRI->clearVirtRegs();
|
|
|
|
|
2010-04-22 02:02:42 +08:00
|
|
|
StackSlotForVirtReg.clear();
|
2010-08-05 02:42:02 +08:00
|
|
|
LiveDbgValueMap.clear();
|
2010-04-22 02:02:42 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createFastRegisterAllocator() {
|
2017-09-09 08:52:46 +08:00
|
|
|
return new RegAllocFast();
|
2010-04-22 02:02:42 +08:00
|
|
|
}
|