2006-02-05 13:50:24 +08:00
|
|
|
//===-- SparcTargetMachine.cpp - Define TargetMachine for Sparc -----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-02-05 13:50:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SparcTargetMachine.h"
|
2014-11-13 17:26:31 +08:00
|
|
|
#include "SparcTargetObjectFile.h"
|
2012-03-18 02:46:09 +08:00
|
|
|
#include "Sparc.h"
|
2012-02-03 13:12:41 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2015-02-13 18:01:29 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2011-08-25 02:08:43 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2006-02-05 13:50:24 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-07-25 14:49:55 +08:00
|
|
|
extern "C" void LLVMInitializeSparcTarget() {
|
|
|
|
// Register the target.
|
2010-02-04 14:34:01 +08:00
|
|
|
RegisterTargetMachine<SparcV8TargetMachine> X(TheSparcTarget);
|
|
|
|
RegisterTargetMachine<SparcV9TargetMachine> Y(TheSparcV9Target);
|
2015-04-30 04:30:57 +08:00
|
|
|
RegisterTargetMachine<SparcelTargetMachine> Z(TheSparcelTarget);
|
2006-09-08 07:39:26 +08:00
|
|
|
}
|
|
|
|
|
2015-04-30 04:30:57 +08:00
|
|
|
static std::string computeDataLayout(const Triple &T, bool is64Bit) {
|
|
|
|
// Sparc is typically big endian, but some are little.
|
|
|
|
std::string Ret = T.getArch() == Triple::sparcel ? "e" : "E";
|
|
|
|
Ret += "-m:e";
|
2015-01-27 03:03:15 +08:00
|
|
|
|
|
|
|
// Some ABIs have 32bit pointers.
|
|
|
|
if (!is64Bit)
|
|
|
|
Ret += "-p:32:32";
|
|
|
|
|
|
|
|
// Alignments for 64 bit integers.
|
|
|
|
Ret += "-i64:64";
|
|
|
|
|
|
|
|
// On SparcV9 128 floats are aligned to 128 bits, on others only to 64.
|
|
|
|
// On SparcV9 registers can hold 64 or 32 bits, on others only 32.
|
|
|
|
if (is64Bit)
|
|
|
|
Ret += "-n32:64";
|
|
|
|
else
|
|
|
|
Ret += "-f128:64-n32";
|
|
|
|
|
|
|
|
if (is64Bit)
|
|
|
|
Ret += "-S128";
|
|
|
|
else
|
|
|
|
Ret += "-S64";
|
|
|
|
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2006-02-05 13:50:24 +08:00
|
|
|
/// SparcTargetMachine ctor - Create an ILP32 architecture model
|
|
|
|
///
|
2015-06-12 03:41:26 +08:00
|
|
|
SparcTargetMachine::SparcTargetMachine(const Target &T, const Triple &TT,
|
2011-07-19 14:37:02 +08:00
|
|
|
StringRef CPU, StringRef FS,
|
2011-12-03 06:16:29 +08:00
|
|
|
const TargetOptions &Options,
|
2011-07-20 15:51:56 +08:00
|
|
|
Reloc::Model RM, CodeModel::Model CM,
|
2015-03-12 08:07:24 +08:00
|
|
|
CodeGenOpt::Level OL, bool is64bit)
|
2015-06-12 03:41:26 +08:00
|
|
|
: LLVMTargetMachine(T, computeDataLayout(TT, is64bit), TT, CPU, FS, Options,
|
|
|
|
RM, CM, OL),
|
2015-03-12 08:07:24 +08:00
|
|
|
TLOF(make_unique<SparcELFTargetObjectFile>()),
|
2015-06-12 03:41:26 +08:00
|
|
|
Subtarget(TT, CPU, FS, *this, is64bit) {
|
2013-05-13 09:16:13 +08:00
|
|
|
initAsmInfo();
|
2006-02-05 13:50:24 +08:00
|
|
|
}
|
|
|
|
|
2014-11-21 07:37:18 +08:00
|
|
|
SparcTargetMachine::~SparcTargetMachine() {}
|
|
|
|
|
2012-02-03 13:12:41 +08:00
|
|
|
namespace {
|
|
|
|
/// Sparc Code Generator Pass Configuration Options.
|
|
|
|
class SparcPassConfig : public TargetPassConfig {
|
|
|
|
public:
|
2012-02-04 10:56:59 +08:00
|
|
|
SparcPassConfig(SparcTargetMachine *TM, PassManagerBase &PM)
|
|
|
|
: TargetPassConfig(TM, PM) {}
|
2012-02-03 13:12:41 +08:00
|
|
|
|
|
|
|
SparcTargetMachine &getSparcTargetMachine() const {
|
|
|
|
return getTM<SparcTargetMachine>();
|
|
|
|
}
|
|
|
|
|
Erase fence insertion from SelectionDAGBuilder.cpp (NFC)
Summary:
Backends can use setInsertFencesForAtomic to signal to the middle-end that
montonic is the only memory ordering they can accept for
stores/loads/rmws/cmpxchg. The code lowering those accesses with a stronger
ordering to fences + monotonic accesses is currently living in
SelectionDAGBuilder.cpp. In this patch I propose moving this logic out of it
for several reasons:
- There is lots of redundancy to avoid: extremely similar logic already
exists in AtomicExpand.
- The current code in SelectionDAGBuilder does not use any target-hooks, it
does the same transformation for every backend that requires it
- As a result it is plain *unsound*, as it was apparently designed for ARM.
It happens to mostly work for the other targets because they are extremely
conservative, but Power for example had to switch to AtomicExpand to be
able to use lwsync safely (see r218331).
- Because it produces IR-level fences, it cannot be made sound ! This is noted
in the C++11 standard (section 29.3, page 1140):
```
Fences cannot, in general, be used to restore sequential consistency for atomic
operations with weaker ordering semantics.
```
It can also be seen by the following example (called IRIW in the litterature):
```
atomic<int> x = y = 0;
int r1, r2, r3, r4;
Thread 0:
x.store(1);
Thread 1:
y.store(1);
Thread 2:
r1 = x.load();
r2 = y.load();
Thread 3:
r3 = y.load();
r4 = x.load();
```
r1 = r3 = 1 and r2 = r4 = 0 is impossible as long as the accesses are all seq_cst.
But if they are lowered to monotonic accesses, no amount of fences can prevent it..
This patch does three things (I could cut it into parts, but then some of them
would not be tested/testable, please tell me if you would prefer that):
- it provides a default implementation for emitLeadingFence/emitTrailingFence in
terms of IR-level fences, that mimic the original logic of SelectionDAGBuilder.
As we saw above, this is unsound, but the best that can be done without knowing
the targets well (and there is a comment warning about this risk).
- it then switches Mips/Sparc/XCore to use AtomicExpand, relying on this default
implementation (that exactly replicates the logic of SelectionDAGBuilder, so no
functional change)
- it finally erase this logic from SelectionDAGBuilder as it is dead-code.
Ideally, each target would define its own override for emitLeading/TrailingFence
using target-specific fences, but I do not know the Sparc/Mips/XCore memory model
well enough to do this, and they appear to be dealing fine with the ARM-inspired
default expansion for now (probably because they are overly conservative, as
Power was). If anyone wants to compile fences more agressively on these
platforms, the long comment should make it clear why he should first override
emitLeading/TrailingFence.
Test Plan: make check-all, no functional change
Reviewers: jfb, t.p.northover
Subscribers: aemerson, llvm-commits
Differential Revision: http://reviews.llvm.org/D5474
llvm-svn: 219957
2014-10-17 04:34:57 +08:00
|
|
|
void addIRPasses() override;
|
2014-04-29 15:57:13 +08:00
|
|
|
bool addInstSelector() override;
|
2014-12-12 05:26:47 +08:00
|
|
|
void addPreEmitPass() override;
|
2012-02-03 13:12:41 +08:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2012-02-04 10:56:59 +08:00
|
|
|
TargetPassConfig *SparcTargetMachine::createPassConfig(PassManagerBase &PM) {
|
|
|
|
return new SparcPassConfig(this, PM);
|
2012-02-03 13:12:41 +08:00
|
|
|
}
|
|
|
|
|
Erase fence insertion from SelectionDAGBuilder.cpp (NFC)
Summary:
Backends can use setInsertFencesForAtomic to signal to the middle-end that
montonic is the only memory ordering they can accept for
stores/loads/rmws/cmpxchg. The code lowering those accesses with a stronger
ordering to fences + monotonic accesses is currently living in
SelectionDAGBuilder.cpp. In this patch I propose moving this logic out of it
for several reasons:
- There is lots of redundancy to avoid: extremely similar logic already
exists in AtomicExpand.
- The current code in SelectionDAGBuilder does not use any target-hooks, it
does the same transformation for every backend that requires it
- As a result it is plain *unsound*, as it was apparently designed for ARM.
It happens to mostly work for the other targets because they are extremely
conservative, but Power for example had to switch to AtomicExpand to be
able to use lwsync safely (see r218331).
- Because it produces IR-level fences, it cannot be made sound ! This is noted
in the C++11 standard (section 29.3, page 1140):
```
Fences cannot, in general, be used to restore sequential consistency for atomic
operations with weaker ordering semantics.
```
It can also be seen by the following example (called IRIW in the litterature):
```
atomic<int> x = y = 0;
int r1, r2, r3, r4;
Thread 0:
x.store(1);
Thread 1:
y.store(1);
Thread 2:
r1 = x.load();
r2 = y.load();
Thread 3:
r3 = y.load();
r4 = x.load();
```
r1 = r3 = 1 and r2 = r4 = 0 is impossible as long as the accesses are all seq_cst.
But if they are lowered to monotonic accesses, no amount of fences can prevent it..
This patch does three things (I could cut it into parts, but then some of them
would not be tested/testable, please tell me if you would prefer that):
- it provides a default implementation for emitLeadingFence/emitTrailingFence in
terms of IR-level fences, that mimic the original logic of SelectionDAGBuilder.
As we saw above, this is unsound, but the best that can be done without knowing
the targets well (and there is a comment warning about this risk).
- it then switches Mips/Sparc/XCore to use AtomicExpand, relying on this default
implementation (that exactly replicates the logic of SelectionDAGBuilder, so no
functional change)
- it finally erase this logic from SelectionDAGBuilder as it is dead-code.
Ideally, each target would define its own override for emitLeading/TrailingFence
using target-specific fences, but I do not know the Sparc/Mips/XCore memory model
well enough to do this, and they appear to be dealing fine with the ARM-inspired
default expansion for now (probably because they are overly conservative, as
Power was). If anyone wants to compile fences more agressively on these
platforms, the long comment should make it clear why he should first override
emitLeading/TrailingFence.
Test Plan: make check-all, no functional change
Reviewers: jfb, t.p.northover
Subscribers: aemerson, llvm-commits
Differential Revision: http://reviews.llvm.org/D5474
llvm-svn: 219957
2014-10-17 04:34:57 +08:00
|
|
|
void SparcPassConfig::addIRPasses() {
|
|
|
|
addPass(createAtomicExpandPass(&getSparcTargetMachine()));
|
|
|
|
|
|
|
|
TargetPassConfig::addIRPasses();
|
|
|
|
}
|
|
|
|
|
2012-02-03 13:12:41 +08:00
|
|
|
bool SparcPassConfig::addInstSelector() {
|
2012-07-03 03:48:31 +08:00
|
|
|
addPass(createSparcISelDag(getSparcTargetMachine()));
|
2006-09-04 12:14:57 +08:00
|
|
|
return false;
|
|
|
|
}
|
2006-02-05 13:50:24 +08:00
|
|
|
|
2014-12-12 05:26:47 +08:00
|
|
|
void SparcPassConfig::addPreEmitPass(){
|
2012-07-03 03:48:31 +08:00
|
|
|
addPass(createSparcDelaySlotFillerPass(getSparcTargetMachine()));
|
2006-09-04 12:14:57 +08:00
|
|
|
}
|
2010-02-04 14:34:01 +08:00
|
|
|
|
2011-12-20 10:50:00 +08:00
|
|
|
void SparcV8TargetMachine::anchor() { }
|
|
|
|
|
2015-06-12 03:41:26 +08:00
|
|
|
SparcV8TargetMachine::SparcV8TargetMachine(const Target &T, const Triple &TT,
|
|
|
|
StringRef CPU, StringRef FS,
|
2011-12-03 06:16:29 +08:00
|
|
|
const TargetOptions &Options,
|
2015-06-12 03:41:26 +08:00
|
|
|
Reloc::Model RM, CodeModel::Model CM,
|
2011-11-16 16:38:26 +08:00
|
|
|
CodeGenOpt::Level OL)
|
2015-06-12 03:41:26 +08:00
|
|
|
: SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
|
2010-02-04 14:34:01 +08:00
|
|
|
|
2011-12-20 10:50:00 +08:00
|
|
|
void SparcV9TargetMachine::anchor() { }
|
|
|
|
|
2015-06-12 03:41:26 +08:00
|
|
|
SparcV9TargetMachine::SparcV9TargetMachine(const Target &T, const Triple &TT,
|
2015-04-30 04:30:57 +08:00
|
|
|
StringRef CPU, StringRef FS,
|
2011-12-03 06:16:29 +08:00
|
|
|
const TargetOptions &Options,
|
2015-04-30 04:30:57 +08:00
|
|
|
Reloc::Model RM, CodeModel::Model CM,
|
2011-11-16 16:38:26 +08:00
|
|
|
CodeGenOpt::Level OL)
|
2015-04-30 04:30:57 +08:00
|
|
|
: SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
|
|
|
|
|
|
|
|
void SparcelTargetMachine::anchor() {}
|
|
|
|
|
2015-06-12 03:41:26 +08:00
|
|
|
SparcelTargetMachine::SparcelTargetMachine(const Target &T, const Triple &TT,
|
2015-04-30 04:30:57 +08:00
|
|
|
StringRef CPU, StringRef FS,
|
|
|
|
const TargetOptions &Options,
|
|
|
|
Reloc::Model RM, CodeModel::Model CM,
|
|
|
|
CodeGenOpt::Level OL)
|
|
|
|
: SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
|