2010-03-25 17:25:00 +00:00
|
|
|
//===- SSEDomainFix.cpp - Use proper int/float domain for SSE ---*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains the SSEDomainFix pass.
|
|
|
|
//
|
|
|
|
// Some SSE instructions like mov, and, or, xor are available in different
|
|
|
|
// variants for different operand types. These variant instructions are
|
|
|
|
// equivalent, but on Nehalem and newer cpus there is extra latency
|
|
|
|
// transferring data between integer and floating point domains.
|
|
|
|
//
|
|
|
|
// This pass changes the variant instructions to minimize domain crossings.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "sse-domain-fix"
|
|
|
|
#include "X86InstrInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2010-04-04 18:00:21 +00:00
|
|
|
#include "llvm/Support/Allocator.h"
|
2010-03-25 17:25:00 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2010-03-31 20:32:51 +00:00
|
|
|
/// A DomainValue is a bit like LiveIntervals' ValNo, but it also keeps track
|
2010-03-29 23:24:21 +00:00
|
|
|
/// of execution domains.
|
|
|
|
///
|
|
|
|
/// An open DomainValue represents a set of instructions that can still switch
|
|
|
|
/// execution domain. Multiple registers may refer to the same open
|
|
|
|
/// DomainValue - they will eventually be collapsed to the same execution
|
|
|
|
/// domain.
|
|
|
|
///
|
|
|
|
/// A collapsed DomainValue represents a single register that has been forced
|
|
|
|
/// into one of more execution domains. There is a separate collapsed
|
|
|
|
/// DomainValue for each register, but it may contain multiple execution
|
|
|
|
/// domains. A register value is initially created in a single execution
|
|
|
|
/// domain, but if we were forced to pay the penalty of a domain crossing, we
|
|
|
|
/// keep track of the fact the the register is now available in multiple
|
|
|
|
/// domains.
|
2010-04-04 18:00:21 +00:00
|
|
|
namespace {
|
2010-03-29 23:24:21 +00:00
|
|
|
struct DomainValue {
|
|
|
|
// Basic reference counting.
|
|
|
|
unsigned Refs;
|
|
|
|
|
2010-04-04 21:27:26 +00:00
|
|
|
// Bitmask of available domains. For an open DomainValue, it is the still
|
|
|
|
// possible domains for collapsing. For a collapsed DomainValue it is the
|
|
|
|
// domains where the register is available for free.
|
|
|
|
unsigned AvailableDomains;
|
2010-03-29 23:24:21 +00:00
|
|
|
|
|
|
|
// Position of the last defining instruction.
|
|
|
|
unsigned Dist;
|
|
|
|
|
|
|
|
// Twiddleable instructions using or defining these registers.
|
|
|
|
SmallVector<MachineInstr*, 8> Instrs;
|
|
|
|
|
2010-04-04 21:27:26 +00:00
|
|
|
// A collapsed DomainValue has no instructions to twiddle - it simply keeps
|
2010-03-29 23:24:21 +00:00
|
|
|
// track of the domains where the registers are already available.
|
2010-04-04 21:27:26 +00:00
|
|
|
bool isCollapsed() const { return Instrs.empty(); }
|
2010-03-29 23:24:21 +00:00
|
|
|
|
2010-04-04 21:27:26 +00:00
|
|
|
// Is domain available?
|
|
|
|
bool hasDomain(unsigned domain) const {
|
|
|
|
return AvailableDomains & (1u << domain);
|
2010-03-29 23:24:21 +00:00
|
|
|
}
|
|
|
|
|
2010-03-30 20:04:01 +00:00
|
|
|
// Mark domain as available.
|
2010-04-04 21:27:26 +00:00
|
|
|
void addDomain(unsigned domain) {
|
|
|
|
AvailableDomains |= 1u << domain;
|
2010-03-29 23:24:21 +00:00
|
|
|
}
|
|
|
|
|
2010-04-04 21:27:26 +00:00
|
|
|
// Restrict to a single domain available.
|
|
|
|
void setSingleDomain(unsigned domain) {
|
|
|
|
AvailableDomains = 1u << domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return bitmask of domains that are available and in mask.
|
|
|
|
unsigned getCommonDomains(unsigned mask) const {
|
|
|
|
return AvailableDomains & mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First domain available.
|
|
|
|
unsigned getFirstDomain() const {
|
|
|
|
return CountTrailingZeros_32(AvailableDomains);
|
2010-03-30 20:04:01 +00:00
|
|
|
}
|
|
|
|
|
2010-03-29 23:24:21 +00:00
|
|
|
DomainValue() { clear(); }
|
|
|
|
|
|
|
|
void clear() {
|
2010-04-04 21:27:26 +00:00
|
|
|
Refs = AvailableDomains = Dist = 0;
|
2010-03-29 23:24:21 +00:00
|
|
|
Instrs.clear();
|
|
|
|
}
|
|
|
|
};
|
2010-04-04 18:00:21 +00:00
|
|
|
}
|
2010-03-29 23:24:21 +00:00
|
|
|
|
2010-03-30 20:04:01 +00:00
|
|
|
static const unsigned NumRegs = 16;
|
|
|
|
|
2010-04-04 18:00:21 +00:00
|
|
|
namespace {
|
2010-03-25 17:25:00 +00:00
|
|
|
class SSEDomainFixPass : public MachineFunctionPass {
|
|
|
|
static char ID;
|
2010-04-04 18:00:21 +00:00
|
|
|
SpecificBumpPtrAllocator<DomainValue> Allocator;
|
|
|
|
SmallVector<DomainValue*,16> Avail;
|
2010-03-25 17:25:00 +00:00
|
|
|
|
|
|
|
MachineFunction *MF;
|
2010-03-29 23:24:21 +00:00
|
|
|
const X86InstrInfo *TII;
|
|
|
|
const TargetRegisterInfo *TRI;
|
2010-03-25 17:25:00 +00:00
|
|
|
MachineBasicBlock *MBB;
|
2010-03-30 20:04:01 +00:00
|
|
|
DomainValue **LiveRegs;
|
|
|
|
typedef DenseMap<MachineBasicBlock*,DomainValue**> LiveOutMap;
|
|
|
|
LiveOutMap LiveOuts;
|
|
|
|
unsigned Distance;
|
2010-03-29 23:24:21 +00:00
|
|
|
|
2010-03-25 17:25:00 +00:00
|
|
|
public:
|
2010-08-06 18:33:48 +00:00
|
|
|
SSEDomainFixPass() : MachineFunctionPass(ID) {}
|
2010-03-25 17:25:00 +00:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool runOnMachineFunction(MachineFunction &MF);
|
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "SSE execution domain fixup";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2010-03-29 23:24:21 +00:00
|
|
|
// Register mapping.
|
|
|
|
int RegIndex(unsigned Reg);
|
|
|
|
|
2010-04-04 18:00:21 +00:00
|
|
|
// DomainValue allocation.
|
|
|
|
DomainValue *Alloc(int domain = -1);
|
|
|
|
void Recycle(DomainValue*);
|
|
|
|
|
2010-03-29 23:24:21 +00:00
|
|
|
// LiveRegs manipulations.
|
|
|
|
void SetLiveReg(int rx, DomainValue *DV);
|
|
|
|
void Kill(int rx);
|
|
|
|
void Force(int rx, unsigned domain);
|
|
|
|
void Collapse(DomainValue *dv, unsigned domain);
|
|
|
|
bool Merge(DomainValue *A, DomainValue *B);
|
|
|
|
|
2010-03-30 20:04:01 +00:00
|
|
|
void enterBasicBlock();
|
2010-03-29 23:24:21 +00:00
|
|
|
void visitGenericInstr(MachineInstr*);
|
|
|
|
void visitSoftInstr(MachineInstr*, unsigned mask);
|
|
|
|
void visitHardInstr(MachineInstr*, unsigned domain);
|
2010-03-25 17:25:00 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
char SSEDomainFixPass::ID = 0;
|
|
|
|
|
2010-03-29 23:24:21 +00:00
|
|
|
/// Translate TRI register number to an index into our smaller tables of
|
|
|
|
/// interesting registers. Return -1 for boring registers.
|
|
|
|
int SSEDomainFixPass::RegIndex(unsigned reg) {
|
Add StringRef::compare_numeric and use it to sort TableGen register records.
This means that our Registers are now ordered R7, R8, R9, R10, R12, ...
Not R1, R10, R11, R12, R2, R3, ...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@104745 91177308-0d34-0410-b5e6-96231b3b80d8
2010-05-26 21:47:28 +00:00
|
|
|
assert(X86::XMM15 == X86::XMM0+NumRegs-1 && "Unexpected sort");
|
2010-03-29 23:24:21 +00:00
|
|
|
reg -= X86::XMM0;
|
2010-04-06 23:35:53 +00:00
|
|
|
return reg < NumRegs ? (int) reg : -1;
|
2010-03-29 23:24:21 +00:00
|
|
|
}
|
|
|
|
|
2010-04-04 18:00:21 +00:00
|
|
|
DomainValue *SSEDomainFixPass::Alloc(int domain) {
|
|
|
|
DomainValue *dv = Avail.empty() ?
|
|
|
|
new(Allocator.Allocate()) DomainValue :
|
|
|
|
Avail.pop_back_val();
|
|
|
|
dv->Dist = Distance;
|
|
|
|
if (domain >= 0)
|
2010-04-04 21:27:26 +00:00
|
|
|
dv->addDomain(domain);
|
2010-04-04 18:00:21 +00:00
|
|
|
return dv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSEDomainFixPass::Recycle(DomainValue *dv) {
|
|
|
|
assert(dv && "Cannot recycle NULL");
|
|
|
|
dv->clear();
|
|
|
|
Avail.push_back(dv);
|
|
|
|
}
|
|
|
|
|
2010-03-29 23:24:21 +00:00
|
|
|
/// Set LiveRegs[rx] = dv, updating reference counts.
|
|
|
|
void SSEDomainFixPass::SetLiveReg(int rx, DomainValue *dv) {
|
2010-03-30 20:04:01 +00:00
|
|
|
assert(unsigned(rx) < NumRegs && "Invalid index");
|
2010-04-04 21:27:26 +00:00
|
|
|
if (!LiveRegs) {
|
|
|
|
LiveRegs = new DomainValue*[NumRegs];
|
|
|
|
std::fill(LiveRegs, LiveRegs+NumRegs, (DomainValue*)0);
|
|
|
|
}
|
2010-03-30 20:04:01 +00:00
|
|
|
|
2010-03-29 23:24:21 +00:00
|
|
|
if (LiveRegs[rx] == dv)
|
|
|
|
return;
|
|
|
|
if (LiveRegs[rx]) {
|
|
|
|
assert(LiveRegs[rx]->Refs && "Bad refcount");
|
2010-04-04 18:00:21 +00:00
|
|
|
if (--LiveRegs[rx]->Refs == 0) Recycle(LiveRegs[rx]);
|
2010-03-29 23:24:21 +00:00
|
|
|
}
|
|
|
|
LiveRegs[rx] = dv;
|
|
|
|
if (dv) ++dv->Refs;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kill register rx, recycle or collapse any DomainValue.
|
|
|
|
void SSEDomainFixPass::Kill(int rx) {
|
2010-03-30 20:04:01 +00:00
|
|
|
assert(unsigned(rx) < NumRegs && "Invalid index");
|
|
|
|
if (!LiveRegs || !LiveRegs[rx]) return;
|
2010-03-29 23:24:21 +00:00
|
|
|
|
|
|
|
// Before killing the last reference to an open DomainValue, collapse it to
|
|
|
|
// the first available domain.
|
2010-04-04 21:27:26 +00:00
|
|
|
if (LiveRegs[rx]->Refs == 1 && !LiveRegs[rx]->isCollapsed())
|
|
|
|
Collapse(LiveRegs[rx], LiveRegs[rx]->getFirstDomain());
|
2010-03-29 23:24:21 +00:00
|
|
|
else
|
|
|
|
SetLiveReg(rx, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Force register rx into domain.
|
|
|
|
void SSEDomainFixPass::Force(int rx, unsigned domain) {
|
2010-03-30 20:04:01 +00:00
|
|
|
assert(unsigned(rx) < NumRegs && "Invalid index");
|
2010-03-30 00:09:32 +00:00
|
|
|
DomainValue *dv;
|
2010-03-30 20:04:01 +00:00
|
|
|
if (LiveRegs && (dv = LiveRegs[rx])) {
|
2010-04-04 21:27:26 +00:00
|
|
|
if (dv->isCollapsed())
|
|
|
|
dv->addDomain(domain);
|
2010-04-06 19:48:56 +00:00
|
|
|
else if (dv->hasDomain(domain))
|
2010-03-29 23:24:21 +00:00
|
|
|
Collapse(dv, domain);
|
2010-04-06 19:48:56 +00:00
|
|
|
else {
|
|
|
|
// This is an incompatible open DomainValue. Collapse it to whatever and force
|
|
|
|
// the new value into domain. This costs a domain crossing.
|
|
|
|
Collapse(dv, dv->getFirstDomain());
|
|
|
|
assert(LiveRegs[rx] && "Not live after collapse?");
|
|
|
|
LiveRegs[rx]->addDomain(domain);
|
|
|
|
}
|
2010-03-29 23:24:21 +00:00
|
|
|
} else {
|
2010-03-30 20:04:01 +00:00
|
|
|
// Set up basic collapsed DomainValue.
|
2010-04-04 18:00:21 +00:00
|
|
|
SetLiveReg(rx, Alloc(domain));
|
2010-03-29 23:24:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Collapse open DomainValue into given domain. If there are multiple
|
|
|
|
/// registers using dv, they each get a unique collapsed DomainValue.
|
|
|
|
void SSEDomainFixPass::Collapse(DomainValue *dv, unsigned domain) {
|
2010-04-04 21:27:26 +00:00
|
|
|
assert(dv->hasDomain(domain) && "Cannot collapse");
|
2010-03-29 23:24:21 +00:00
|
|
|
|
|
|
|
// Collapse all the instructions.
|
2010-04-04 21:27:26 +00:00
|
|
|
while (!dv->Instrs.empty())
|
|
|
|
TII->SetSSEDomain(dv->Instrs.pop_back_val(), domain);
|
|
|
|
dv->setSingleDomain(domain);
|
2010-03-29 23:24:21 +00:00
|
|
|
|
|
|
|
// If there are multiple users, give them new, unique DomainValues.
|
2010-04-04 18:00:21 +00:00
|
|
|
if (LiveRegs && dv->Refs > 1)
|
2010-03-30 20:04:01 +00:00
|
|
|
for (unsigned rx = 0; rx != NumRegs; ++rx)
|
2010-04-04 18:00:21 +00:00
|
|
|
if (LiveRegs[rx] == dv)
|
|
|
|
SetLiveReg(rx, Alloc(domain));
|
2010-03-29 23:24:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Merge - All instructions and registers in B are moved to A, and B is
|
|
|
|
/// released.
|
|
|
|
bool SSEDomainFixPass::Merge(DomainValue *A, DomainValue *B) {
|
2010-04-04 21:27:26 +00:00
|
|
|
assert(!A->isCollapsed() && "Cannot merge into collapsed");
|
|
|
|
assert(!B->isCollapsed() && "Cannot merge from collapsed");
|
2010-03-31 20:05:12 +00:00
|
|
|
if (A == B)
|
2010-03-31 17:13:16 +00:00
|
|
|
return true;
|
2010-04-04 21:27:26 +00:00
|
|
|
// Restrict to the domains that A and B have in common.
|
|
|
|
unsigned common = A->getCommonDomains(B->AvailableDomains);
|
|
|
|
if (!common)
|
2010-03-29 23:24:21 +00:00
|
|
|
return false;
|
2010-04-04 21:27:26 +00:00
|
|
|
A->AvailableDomains = common;
|
2010-03-29 23:24:21 +00:00
|
|
|
A->Dist = std::max(A->Dist, B->Dist);
|
|
|
|
A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
|
2010-03-30 20:04:01 +00:00
|
|
|
for (unsigned rx = 0; rx != NumRegs; ++rx)
|
2010-03-29 23:24:21 +00:00
|
|
|
if (LiveRegs[rx] == B)
|
|
|
|
SetLiveReg(rx, A);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-30 20:04:01 +00:00
|
|
|
void SSEDomainFixPass::enterBasicBlock() {
|
|
|
|
// Try to coalesce live-out registers from predecessors.
|
2010-04-13 16:57:55 +00:00
|
|
|
for (MachineBasicBlock::livein_iterator i = MBB->livein_begin(),
|
2010-03-30 20:04:01 +00:00
|
|
|
e = MBB->livein_end(); i != e; ++i) {
|
|
|
|
int rx = RegIndex(*i);
|
|
|
|
if (rx < 0) continue;
|
|
|
|
for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
|
|
|
|
pe = MBB->pred_end(); pi != pe; ++pi) {
|
2010-03-31 00:40:08 +00:00
|
|
|
LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
|
2010-03-30 20:04:01 +00:00
|
|
|
if (fi == LiveOuts.end()) continue;
|
|
|
|
DomainValue *pdv = fi->second[rx];
|
|
|
|
if (!pdv) continue;
|
2010-03-31 20:32:51 +00:00
|
|
|
if (!LiveRegs || !LiveRegs[rx]) {
|
2010-03-30 20:04:01 +00:00
|
|
|
SetLiveReg(rx, pdv);
|
2010-03-31 20:32:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have a live DomainValue from more than one predecessor.
|
2010-04-04 21:27:26 +00:00
|
|
|
if (LiveRegs[rx]->isCollapsed()) {
|
2010-03-31 20:32:51 +00:00
|
|
|
// We are already collapsed, but predecessor is not. Force him.
|
2010-04-06 19:48:56 +00:00
|
|
|
unsigned domain = LiveRegs[rx]->getFirstDomain();
|
|
|
|
if (!pdv->isCollapsed() && pdv->hasDomain(domain))
|
|
|
|
Collapse(pdv, domain);
|
2010-03-31 20:32:51 +00:00
|
|
|
continue;
|
2010-03-30 20:04:01 +00:00
|
|
|
}
|
2010-04-04 18:00:21 +00:00
|
|
|
|
2010-03-31 20:32:51 +00:00
|
|
|
// Currently open, merge in predecessor.
|
2010-04-04 21:27:26 +00:00
|
|
|
if (!pdv->isCollapsed())
|
2010-03-31 20:32:51 +00:00
|
|
|
Merge(LiveRegs[rx], pdv);
|
|
|
|
else
|
2010-04-06 19:48:56 +00:00
|
|
|
Force(rx, pdv->getFirstDomain());
|
2010-03-30 20:04:01 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-29 23:24:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A hard instruction only works in one domain. All input registers will be
|
|
|
|
// forced into that domain.
|
|
|
|
void SSEDomainFixPass::visitHardInstr(MachineInstr *mi, unsigned domain) {
|
|
|
|
// Collapse all uses.
|
|
|
|
for (unsigned i = mi->getDesc().getNumDefs(),
|
|
|
|
e = mi->getDesc().getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &mo = mi->getOperand(i);
|
|
|
|
if (!mo.isReg()) continue;
|
|
|
|
int rx = RegIndex(mo.getReg());
|
|
|
|
if (rx < 0) continue;
|
|
|
|
Force(rx, domain);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kill all defs and force them.
|
|
|
|
for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
|
|
|
|
MachineOperand &mo = mi->getOperand(i);
|
|
|
|
if (!mo.isReg()) continue;
|
|
|
|
int rx = RegIndex(mo.getReg());
|
|
|
|
if (rx < 0) continue;
|
|
|
|
Kill(rx);
|
|
|
|
Force(rx, domain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A soft instruction can be changed to work in other domains given by mask.
|
|
|
|
void SSEDomainFixPass::visitSoftInstr(MachineInstr *mi, unsigned mask) {
|
2010-04-04 21:27:26 +00:00
|
|
|
// Bitmask of available domains for this instruction after taking collapsed
|
|
|
|
// operands into account.
|
|
|
|
unsigned available = mask;
|
|
|
|
|
2010-03-29 23:24:21 +00:00
|
|
|
// Scan the explicit use operands for incoming domains.
|
|
|
|
SmallVector<int, 4> used;
|
2010-03-30 20:04:01 +00:00
|
|
|
if (LiveRegs)
|
|
|
|
for (unsigned i = mi->getDesc().getNumDefs(),
|
|
|
|
e = mi->getDesc().getNumOperands(); i != e; ++i) {
|
2010-03-31 20:32:51 +00:00
|
|
|
MachineOperand &mo = mi->getOperand(i);
|
|
|
|
if (!mo.isReg()) continue;
|
|
|
|
int rx = RegIndex(mo.getReg());
|
|
|
|
if (rx < 0) continue;
|
|
|
|
if (DomainValue *dv = LiveRegs[rx]) {
|
2010-04-04 21:27:26 +00:00
|
|
|
// Bitmask of domains that dv and available have in common.
|
|
|
|
unsigned common = dv->getCommonDomains(available);
|
2010-03-31 20:32:51 +00:00
|
|
|
// Is it possible to use this collapsed register for free?
|
2010-04-04 21:27:26 +00:00
|
|
|
if (dv->isCollapsed()) {
|
|
|
|
// Restrict available domains to the ones in common with the operand.
|
|
|
|
// If there are no common domains, we must pay the cross-domain
|
|
|
|
// penalty for this operand.
|
|
|
|
if (common) available = common;
|
|
|
|
} else if (common)
|
|
|
|
// Open DomainValue is compatible, save it for merging.
|
2010-03-31 20:32:51 +00:00
|
|
|
used.push_back(rx);
|
|
|
|
else
|
2010-04-04 21:27:26 +00:00
|
|
|
// Open DomainValue is not compatible with instruction. It is useless
|
|
|
|
// now.
|
2010-03-31 20:32:51 +00:00
|
|
|
Kill(rx);
|
|
|
|
}
|
2010-03-29 23:24:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the collapsed operands force a single domain, propagate the collapse.
|
2010-04-04 21:27:26 +00:00
|
|
|
if (isPowerOf2_32(available)) {
|
|
|
|
unsigned domain = CountTrailingZeros_32(available);
|
2010-03-29 23:24:21 +00:00
|
|
|
TII->SetSSEDomain(mi, domain);
|
|
|
|
visitHardInstr(mi, domain);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-04 21:27:26 +00:00
|
|
|
// Kill off any remaining uses that don't match available, and build a list of
|
|
|
|
// incoming DomainValues that we want to merge.
|
2010-03-29 23:24:21 +00:00
|
|
|
SmallVector<DomainValue*,4> doms;
|
|
|
|
for (SmallVector<int, 4>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
|
|
|
|
int rx = *i;
|
|
|
|
DomainValue *dv = LiveRegs[rx];
|
2010-03-30 20:04:01 +00:00
|
|
|
// This useless DomainValue could have been missed above.
|
2010-04-04 21:27:26 +00:00
|
|
|
if (!dv->getCommonDomains(available)) {
|
2010-03-29 23:24:21 +00:00
|
|
|
Kill(*i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// sorted, uniqued insert.
|
|
|
|
bool inserted = false;
|
|
|
|
for (SmallVector<DomainValue*,4>::iterator i = doms.begin(), e = doms.end();
|
|
|
|
i != e && !inserted; ++i) {
|
|
|
|
if (dv == *i)
|
|
|
|
inserted = true;
|
|
|
|
else if (dv->Dist < (*i)->Dist) {
|
|
|
|
inserted = true;
|
|
|
|
doms.insert(i, dv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!inserted)
|
|
|
|
doms.push_back(dv);
|
|
|
|
}
|
|
|
|
|
2010-04-04 21:27:26 +00:00
|
|
|
// doms are now sorted in order of appearance. Try to merge them all, giving
|
|
|
|
// priority to the latest ones.
|
2010-03-29 23:24:21 +00:00
|
|
|
DomainValue *dv = 0;
|
|
|
|
while (!doms.empty()) {
|
2010-03-31 20:32:51 +00:00
|
|
|
if (!dv) {
|
|
|
|
dv = doms.pop_back_val();
|
|
|
|
continue;
|
|
|
|
}
|
2010-04-04 18:00:21 +00:00
|
|
|
|
2010-04-04 21:27:26 +00:00
|
|
|
DomainValue *latest = doms.pop_back_val();
|
|
|
|
if (Merge(dv, latest)) continue;
|
2010-04-04 18:00:21 +00:00
|
|
|
|
2010-04-04 21:27:26 +00:00
|
|
|
// If latest didn't merge, it is useless now. Kill all registers using it.
|
2010-03-31 20:32:51 +00:00
|
|
|
for (SmallVector<int,4>::iterator i=used.begin(), e=used.end(); i != e; ++i)
|
2010-04-04 21:27:26 +00:00
|
|
|
if (LiveRegs[*i] == latest)
|
2010-03-31 20:32:51 +00:00
|
|
|
Kill(*i);
|
2010-03-29 23:24:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// dv is the DomainValue we are going to use for this instruction.
|
|
|
|
if (!dv)
|
2010-04-04 18:00:21 +00:00
|
|
|
dv = Alloc();
|
2010-03-30 20:04:01 +00:00
|
|
|
dv->Dist = Distance;
|
2010-04-04 21:27:26 +00:00
|
|
|
dv->AvailableDomains = available;
|
2010-03-29 23:24:21 +00:00
|
|
|
dv->Instrs.push_back(mi);
|
|
|
|
|
|
|
|
// Finally set all defs and non-collapsed uses to dv.
|
|
|
|
for (unsigned i = 0, e = mi->getDesc().getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &mo = mi->getOperand(i);
|
|
|
|
if (!mo.isReg()) continue;
|
|
|
|
int rx = RegIndex(mo.getReg());
|
|
|
|
if (rx < 0) continue;
|
2010-03-30 20:04:01 +00:00
|
|
|
if (!LiveRegs || !LiveRegs[rx] || (mo.isDef() && LiveRegs[rx]!=dv)) {
|
2010-03-29 23:24:21 +00:00
|
|
|
Kill(rx);
|
|
|
|
SetLiveReg(rx, dv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSEDomainFixPass::visitGenericInstr(MachineInstr *mi) {
|
2010-03-30 20:04:01 +00:00
|
|
|
// Process explicit defs, kill any XMM registers redefined.
|
2010-03-29 23:24:21 +00:00
|
|
|
for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
|
|
|
|
MachineOperand &mo = mi->getOperand(i);
|
|
|
|
if (!mo.isReg()) continue;
|
|
|
|
int rx = RegIndex(mo.getReg());
|
|
|
|
if (rx < 0) continue;
|
|
|
|
Kill(rx);
|
|
|
|
}
|
2010-03-25 17:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SSEDomainFixPass::runOnMachineFunction(MachineFunction &mf) {
|
|
|
|
MF = &mf;
|
|
|
|
TII = static_cast<const X86InstrInfo*>(MF->getTarget().getInstrInfo());
|
2010-03-29 23:24:21 +00:00
|
|
|
TRI = MF->getTarget().getRegisterInfo();
|
|
|
|
MBB = 0;
|
2010-03-30 20:04:01 +00:00
|
|
|
LiveRegs = 0;
|
|
|
|
Distance = 0;
|
|
|
|
assert(NumRegs == X86::VR128RegClass.getNumRegs() && "Bad regclass");
|
2010-03-25 17:25:00 +00:00
|
|
|
|
|
|
|
// If no XMM registers are used in the function, we can skip it completely.
|
2010-03-29 23:24:21 +00:00
|
|
|
bool anyregs = false;
|
2010-03-25 17:25:00 +00:00
|
|
|
for (TargetRegisterClass::const_iterator I = X86::VR128RegClass.begin(),
|
|
|
|
E = X86::VR128RegClass.end(); I != E; ++I)
|
|
|
|
if (MF->getRegInfo().isPhysRegUsed(*I)) {
|
2010-03-29 23:24:21 +00:00
|
|
|
anyregs = true;
|
2010-03-25 17:25:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-03-29 23:24:21 +00:00
|
|
|
if (!anyregs) return false;
|
2010-03-25 17:25:00 +00:00
|
|
|
|
|
|
|
MachineBasicBlock *Entry = MF->begin();
|
|
|
|
SmallPtrSet<MachineBasicBlock*, 16> Visited;
|
2010-03-29 23:24:21 +00:00
|
|
|
for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*, 16> >
|
2010-03-25 17:25:00 +00:00
|
|
|
DFI = df_ext_begin(Entry, Visited), DFE = df_ext_end(Entry, Visited);
|
2010-03-29 23:24:21 +00:00
|
|
|
DFI != DFE; ++DFI) {
|
2010-03-30 20:04:01 +00:00
|
|
|
MBB = *DFI;
|
|
|
|
enterBasicBlock();
|
2010-03-25 17:25:00 +00:00
|
|
|
for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
|
|
|
|
++I) {
|
2010-03-29 23:24:21 +00:00
|
|
|
MachineInstr *mi = I;
|
|
|
|
if (mi->isDebugValue()) continue;
|
2010-03-30 20:04:01 +00:00
|
|
|
++Distance;
|
2010-03-29 23:24:21 +00:00
|
|
|
std::pair<uint16_t, uint16_t> domp = TII->GetSSEDomain(mi);
|
|
|
|
if (domp.first)
|
|
|
|
if (domp.second)
|
|
|
|
visitSoftInstr(mi, domp.second);
|
|
|
|
else
|
|
|
|
visitHardInstr(mi, domp.first);
|
2010-03-30 20:04:01 +00:00
|
|
|
else if (LiveRegs)
|
2010-03-29 23:24:21 +00:00
|
|
|
visitGenericInstr(mi);
|
2010-03-25 17:25:00 +00:00
|
|
|
}
|
2010-03-30 20:04:01 +00:00
|
|
|
|
|
|
|
// Save live registers at end of MBB - used by enterBasicBlock().
|
|
|
|
if (LiveRegs)
|
|
|
|
LiveOuts.insert(std::make_pair(MBB, LiveRegs));
|
|
|
|
LiveRegs = 0;
|
2010-03-25 17:25:00 +00:00
|
|
|
}
|
2010-03-29 23:24:21 +00:00
|
|
|
|
2010-03-30 20:04:01 +00:00
|
|
|
// Clear the LiveOuts vectors. Should we also collapse any remaining
|
|
|
|
// DomainValues?
|
|
|
|
for (LiveOutMap::const_iterator i = LiveOuts.begin(), e = LiveOuts.end();
|
|
|
|
i != e; ++i)
|
2010-04-04 21:27:26 +00:00
|
|
|
delete[] i->second;
|
2010-03-30 20:04:01 +00:00
|
|
|
LiveOuts.clear();
|
2010-04-04 18:00:21 +00:00
|
|
|
Avail.clear();
|
|
|
|
Allocator.DestroyAll();
|
2010-03-29 23:24:21 +00:00
|
|
|
|
2010-03-25 17:25:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createSSEDomainFixPass() {
|
|
|
|
return new SSEDomainFixPass();
|
|
|
|
}
|