9cedb8bb69
Upgrade our copy of llvm/clang to 3.4 release. This version supports all of the features in the current working draft of the upcoming C++ standard, provisionally named C++1y. The code generator's performance is greatly increased, and the loop auto-vectorizer is now enabled at -Os and -O2 in addition to -O3. The PowerPC backend has made several major improvements to code generation quality and compile time, and the X86, SPARC, ARM32, Aarch64 and SystemZ backends have all seen major feature work. Release notes for llvm and clang can be found here: <http://llvm.org/releases/3.4/docs/ReleaseNotes.html> <http://llvm.org/releases/3.4/tools/clang/docs/ReleaseNotes.html> MFC 262121 (by emaste): Update lldb for clang/llvm 3.4 import This commit largely restores the lldb source to the upstream r196259 snapshot with the addition of threaded inferior support and a few bug fixes. Specific upstream lldb revisions restored include: SVN git 181387 779e6ac 181703 7bef4e2 182099 b31044e 182650 f2dcf35 182683 0d91b80 183862 15c1774 183929 99447a6 184177 0b2934b 184948 4dc3761 184954 007e7bc 186990 eebd175 Sponsored by: DARPA, AFRL MFC 262186 (by emaste): Fix mismerge in r262121 A break statement was lost in the merge. The error had no functional impact, but restore it to reduce the diff against upstream. MFC 262303: Pull in r197521 from upstream clang trunk (by rdivacky): Use the integrated assembler by default on FreeBSD/ppc and ppc64. Requested by: jhibbits MFC 262611: Pull in r196874 from upstream llvm trunk: Fix a crash that occurs when PWD is invalid. MCJIT needs to be able to run in hostile environments, even when PWD is invalid. There's no need to crash MCJIT in this case. The obvious fix is to simply leave MCContext's CompilationDir empty when PWD can't be determined. This way, MCJIT clients, and other clients that link with LLVM don't need a valid working directory. If we do want to guarantee valid CompilationDir, that should be done only for clients of getCompilationDir(). This is as simple as checking for an empty string. The only current use of getCompilationDir is EmitGenDwarfInfo, which won't conceivably run with an invalid working dir. However, in the purely hypothetically and untestable case that this happens, the AT_comp_dir will be omitted from the compilation_unit DIE. This should help fix assertions occurring with ports-mgmt/tinderbox, when it is using jails, and sometimes invalidates clang's current working directory. Reported by: decke MFC 262809: Pull in r203007 from upstream clang trunk: Don't produce an alias between destructors with different calling conventions. Fixes pr19007. (Please note that is an LLVM PR identifier, not a FreeBSD one.) This should fix Firefox and/or libxul crashes (due to problems with regparm/stdcall calling conventions) on i386. Reported by: multiple users on freebsd-current PR: bin/187103 MFC 263048: Repair recognition of "CC" as an alias for the C++ compiler, since it was silently broken by upstream for a Windows-specific use-case. Apparently some versions of CMake still rely on this archaic feature... Reported by: rakuco MFC 263049: Garbage collect the old way of adding the libstdc++ include directories in clang's InitHeaderSearch.cpp. This has been superseded by David Chisnall's commit in r255321. Moreover, if libc++ is used, the libstdc++ include directories should not be in the search path at all. These directories are now only used if you pass -stdlib=libstdc++.
289 lines
11 KiB
C++
289 lines
11 KiB
C++
//===-- llvm/Target/TargetSchedule.cpp - Sched Machine Model ----*- C++ -*-===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file implements a wrapper around MCSchedModel that allows the interface
|
|
// to benefit from information currently only available in TargetInstrInfo.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/CodeGen/TargetSchedule.h"
|
|
#include "llvm/Support/CommandLine.h"
|
|
#include "llvm/Support/raw_ostream.h"
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
#include "llvm/Target/TargetMachine.h"
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
|
|
|
using namespace llvm;
|
|
|
|
static cl::opt<bool> EnableSchedModel("schedmodel", cl::Hidden, cl::init(true),
|
|
cl::desc("Use TargetSchedModel for latency lookup"));
|
|
|
|
static cl::opt<bool> EnableSchedItins("scheditins", cl::Hidden, cl::init(true),
|
|
cl::desc("Use InstrItineraryData for latency lookup"));
|
|
|
|
bool TargetSchedModel::hasInstrSchedModel() const {
|
|
return EnableSchedModel && SchedModel.hasInstrSchedModel();
|
|
}
|
|
|
|
bool TargetSchedModel::hasInstrItineraries() const {
|
|
return EnableSchedItins && !InstrItins.isEmpty();
|
|
}
|
|
|
|
static unsigned gcd(unsigned Dividend, unsigned Divisor) {
|
|
// Dividend and Divisor will be naturally swapped as needed.
|
|
while(Divisor) {
|
|
unsigned Rem = Dividend % Divisor;
|
|
Dividend = Divisor;
|
|
Divisor = Rem;
|
|
};
|
|
return Dividend;
|
|
}
|
|
static unsigned lcm(unsigned A, unsigned B) {
|
|
unsigned LCM = (uint64_t(A) * B) / gcd(A, B);
|
|
assert((LCM >= A && LCM >= B) && "LCM overflow");
|
|
return LCM;
|
|
}
|
|
|
|
void TargetSchedModel::init(const MCSchedModel &sm,
|
|
const TargetSubtargetInfo *sti,
|
|
const TargetInstrInfo *tii) {
|
|
SchedModel = sm;
|
|
STI = sti;
|
|
TII = tii;
|
|
STI->initInstrItins(InstrItins);
|
|
|
|
unsigned NumRes = SchedModel.getNumProcResourceKinds();
|
|
ResourceFactors.resize(NumRes);
|
|
ResourceLCM = SchedModel.IssueWidth;
|
|
for (unsigned Idx = 0; Idx < NumRes; ++Idx) {
|
|
unsigned NumUnits = SchedModel.getProcResource(Idx)->NumUnits;
|
|
if (NumUnits > 0)
|
|
ResourceLCM = lcm(ResourceLCM, NumUnits);
|
|
}
|
|
MicroOpFactor = ResourceLCM / SchedModel.IssueWidth;
|
|
for (unsigned Idx = 0; Idx < NumRes; ++Idx) {
|
|
unsigned NumUnits = SchedModel.getProcResource(Idx)->NumUnits;
|
|
ResourceFactors[Idx] = NumUnits ? (ResourceLCM / NumUnits) : 0;
|
|
}
|
|
}
|
|
|
|
unsigned TargetSchedModel::getNumMicroOps(const MachineInstr *MI,
|
|
const MCSchedClassDesc *SC) const {
|
|
if (hasInstrItineraries()) {
|
|
int UOps = InstrItins.getNumMicroOps(MI->getDesc().getSchedClass());
|
|
return (UOps >= 0) ? UOps : TII->getNumMicroOps(&InstrItins, MI);
|
|
}
|
|
if (hasInstrSchedModel()) {
|
|
if (!SC)
|
|
SC = resolveSchedClass(MI);
|
|
if (SC->isValid())
|
|
return SC->NumMicroOps;
|
|
}
|
|
return MI->isTransient() ? 0 : 1;
|
|
}
|
|
|
|
// The machine model may explicitly specify an invalid latency, which
|
|
// effectively means infinite latency. Since users of the TargetSchedule API
|
|
// don't know how to handle this, we convert it to a very large latency that is
|
|
// easy to distinguish when debugging the DAG but won't induce overflow.
|
|
static unsigned capLatency(int Cycles) {
|
|
return Cycles >= 0 ? Cycles : 1000;
|
|
}
|
|
|
|
/// Return the MCSchedClassDesc for this instruction. Some SchedClasses require
|
|
/// evaluation of predicates that depend on instruction operands or flags.
|
|
const MCSchedClassDesc *TargetSchedModel::
|
|
resolveSchedClass(const MachineInstr *MI) const {
|
|
|
|
// Get the definition's scheduling class descriptor from this machine model.
|
|
unsigned SchedClass = MI->getDesc().getSchedClass();
|
|
const MCSchedClassDesc *SCDesc = SchedModel.getSchedClassDesc(SchedClass);
|
|
if (!SCDesc->isValid())
|
|
return SCDesc;
|
|
|
|
#ifndef NDEBUG
|
|
unsigned NIter = 0;
|
|
#endif
|
|
while (SCDesc->isVariant()) {
|
|
assert(++NIter < 6 && "Variants are nested deeper than the magic number");
|
|
|
|
SchedClass = STI->resolveSchedClass(SchedClass, MI, this);
|
|
SCDesc = SchedModel.getSchedClassDesc(SchedClass);
|
|
}
|
|
return SCDesc;
|
|
}
|
|
|
|
/// Find the def index of this operand. This index maps to the machine model and
|
|
/// is independent of use operands. Def operands may be reordered with uses or
|
|
/// merged with uses without affecting the def index (e.g. before/after
|
|
/// regalloc). However, an instruction's def operands must never be reordered
|
|
/// with respect to each other.
|
|
static unsigned findDefIdx(const MachineInstr *MI, unsigned DefOperIdx) {
|
|
unsigned DefIdx = 0;
|
|
for (unsigned i = 0; i != DefOperIdx; ++i) {
|
|
const MachineOperand &MO = MI->getOperand(i);
|
|
if (MO.isReg() && MO.isDef())
|
|
++DefIdx;
|
|
}
|
|
return DefIdx;
|
|
}
|
|
|
|
/// Find the use index of this operand. This is independent of the instruction's
|
|
/// def operands.
|
|
///
|
|
/// Note that uses are not determined by the operand's isUse property, which
|
|
/// is simply the inverse of isDef. Here we consider any readsReg operand to be
|
|
/// a "use". The machine model allows an operand to be both a Def and Use.
|
|
static unsigned findUseIdx(const MachineInstr *MI, unsigned UseOperIdx) {
|
|
unsigned UseIdx = 0;
|
|
for (unsigned i = 0; i != UseOperIdx; ++i) {
|
|
const MachineOperand &MO = MI->getOperand(i);
|
|
if (MO.isReg() && MO.readsReg())
|
|
++UseIdx;
|
|
}
|
|
return UseIdx;
|
|
}
|
|
|
|
// Top-level API for clients that know the operand indices.
|
|
unsigned TargetSchedModel::computeOperandLatency(
|
|
const MachineInstr *DefMI, unsigned DefOperIdx,
|
|
const MachineInstr *UseMI, unsigned UseOperIdx) const {
|
|
|
|
if (!hasInstrSchedModel() && !hasInstrItineraries())
|
|
return TII->defaultDefLatency(&SchedModel, DefMI);
|
|
|
|
if (hasInstrItineraries()) {
|
|
int OperLatency = 0;
|
|
if (UseMI) {
|
|
OperLatency = TII->getOperandLatency(&InstrItins, DefMI, DefOperIdx,
|
|
UseMI, UseOperIdx);
|
|
}
|
|
else {
|
|
unsigned DefClass = DefMI->getDesc().getSchedClass();
|
|
OperLatency = InstrItins.getOperandCycle(DefClass, DefOperIdx);
|
|
}
|
|
if (OperLatency >= 0)
|
|
return OperLatency;
|
|
|
|
// No operand latency was found.
|
|
unsigned InstrLatency = TII->getInstrLatency(&InstrItins, DefMI);
|
|
|
|
// Expected latency is the max of the stage latency and itinerary props.
|
|
// Rather than directly querying InstrItins stage latency, we call a TII
|
|
// hook to allow subtargets to specialize latency. This hook is only
|
|
// applicable to the InstrItins model. InstrSchedModel should model all
|
|
// special cases without TII hooks.
|
|
InstrLatency = std::max(InstrLatency,
|
|
TII->defaultDefLatency(&SchedModel, DefMI));
|
|
return InstrLatency;
|
|
}
|
|
// hasInstrSchedModel()
|
|
const MCSchedClassDesc *SCDesc = resolveSchedClass(DefMI);
|
|
unsigned DefIdx = findDefIdx(DefMI, DefOperIdx);
|
|
if (DefIdx < SCDesc->NumWriteLatencyEntries) {
|
|
// Lookup the definition's write latency in SubtargetInfo.
|
|
const MCWriteLatencyEntry *WLEntry =
|
|
STI->getWriteLatencyEntry(SCDesc, DefIdx);
|
|
unsigned WriteID = WLEntry->WriteResourceID;
|
|
unsigned Latency = capLatency(WLEntry->Cycles);
|
|
if (!UseMI)
|
|
return Latency;
|
|
|
|
// Lookup the use's latency adjustment in SubtargetInfo.
|
|
const MCSchedClassDesc *UseDesc = resolveSchedClass(UseMI);
|
|
if (UseDesc->NumReadAdvanceEntries == 0)
|
|
return Latency;
|
|
unsigned UseIdx = findUseIdx(UseMI, UseOperIdx);
|
|
int Advance = STI->getReadAdvanceCycles(UseDesc, UseIdx, WriteID);
|
|
if (Advance > 0 && (unsigned)Advance > Latency) // unsigned wrap
|
|
return 0;
|
|
return Latency - Advance;
|
|
}
|
|
// If DefIdx does not exist in the model (e.g. implicit defs), then return
|
|
// unit latency (defaultDefLatency may be too conservative).
|
|
#ifndef NDEBUG
|
|
if (SCDesc->isValid() && !DefMI->getOperand(DefOperIdx).isImplicit()
|
|
&& !DefMI->getDesc().OpInfo[DefOperIdx].isOptionalDef()
|
|
&& SchedModel.isComplete()) {
|
|
std::string Err;
|
|
raw_string_ostream ss(Err);
|
|
ss << "DefIdx " << DefIdx << " exceeds machine model writes for "
|
|
<< *DefMI;
|
|
report_fatal_error(ss.str());
|
|
}
|
|
#endif
|
|
// FIXME: Automatically giving all implicit defs defaultDefLatency is
|
|
// undesirable. We should only do it for defs that are known to the MC
|
|
// desc like flags. Truly implicit defs should get 1 cycle latency.
|
|
return DefMI->isTransient() ? 0 : TII->defaultDefLatency(&SchedModel, DefMI);
|
|
}
|
|
|
|
unsigned
|
|
TargetSchedModel::computeInstrLatency(const MachineInstr *MI,
|
|
bool UseDefaultDefLatency) const {
|
|
// For the itinerary model, fall back to the old subtarget hook.
|
|
// Allow subtargets to compute Bundle latencies outside the machine model.
|
|
if (hasInstrItineraries() || MI->isBundle() ||
|
|
(!hasInstrSchedModel() && !UseDefaultDefLatency))
|
|
return TII->getInstrLatency(&InstrItins, MI);
|
|
|
|
if (hasInstrSchedModel()) {
|
|
const MCSchedClassDesc *SCDesc = resolveSchedClass(MI);
|
|
if (SCDesc->isValid()) {
|
|
unsigned Latency = 0;
|
|
for (unsigned DefIdx = 0, DefEnd = SCDesc->NumWriteLatencyEntries;
|
|
DefIdx != DefEnd; ++DefIdx) {
|
|
// Lookup the definition's write latency in SubtargetInfo.
|
|
const MCWriteLatencyEntry *WLEntry =
|
|
STI->getWriteLatencyEntry(SCDesc, DefIdx);
|
|
Latency = std::max(Latency, capLatency(WLEntry->Cycles));
|
|
}
|
|
return Latency;
|
|
}
|
|
}
|
|
return TII->defaultDefLatency(&SchedModel, MI);
|
|
}
|
|
|
|
unsigned TargetSchedModel::
|
|
computeOutputLatency(const MachineInstr *DefMI, unsigned DefOperIdx,
|
|
const MachineInstr *DepMI) const {
|
|
if (SchedModel.MicroOpBufferSize <= 1)
|
|
return 1;
|
|
|
|
// MicroOpBufferSize > 1 indicates an out-of-order processor that can dispatch
|
|
// WAW dependencies in the same cycle.
|
|
|
|
// Treat predication as a data dependency for out-of-order cpus. In-order
|
|
// cpus do not need to treat predicated writes specially.
|
|
//
|
|
// TODO: The following hack exists because predication passes do not
|
|
// correctly append imp-use operands, and readsReg() strangely returns false
|
|
// for predicated defs.
|
|
unsigned Reg = DefMI->getOperand(DefOperIdx).getReg();
|
|
const MachineFunction &MF = *DefMI->getParent()->getParent();
|
|
const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
|
|
if (!DepMI->readsRegister(Reg, TRI) && TII->isPredicated(DepMI))
|
|
return computeInstrLatency(DefMI);
|
|
|
|
// If we have a per operand scheduling model, check if this def is writing
|
|
// an unbuffered resource. If so, it treated like an in-order cpu.
|
|
if (hasInstrSchedModel()) {
|
|
const MCSchedClassDesc *SCDesc = resolveSchedClass(DefMI);
|
|
if (SCDesc->isValid()) {
|
|
for (const MCWriteProcResEntry *PRI = STI->getWriteProcResBegin(SCDesc),
|
|
*PRE = STI->getWriteProcResEnd(SCDesc); PRI != PRE; ++PRI) {
|
|
if (!SchedModel.getProcResource(PRI->ProcResourceIdx)->BufferSize)
|
|
return 1;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|