2008-03-16 20:08:03 +00:00
|
|
|
//===-- Target.cpp --------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-10-07 20:05:18 +00:00
|
|
|
// This file implements the common infrastructure (including C bindings) for
|
2010-10-07 18:50:11 +00:00
|
|
|
// libLLVMTarget.a, which implements target information.
|
2008-03-16 20:08:03 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm-c/Target.h"
|
2010-10-07 18:50:11 +00:00
|
|
|
#include "llvm-c/Initialization.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2013-05-01 20:59:00 +00:00
|
|
|
#include "llvm/IR/Value.h"
|
2010-10-07 18:50:11 +00:00
|
|
|
#include "llvm/InitializePasses.h"
|
2015-02-13 10:01:29 +00:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2015-01-15 02:16:27 +00:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2008-04-04 16:08:00 +00:00
|
|
|
#include <cstring>
|
2008-03-16 20:08:03 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
[PM] Rework how the TargetLibraryInfo pass integrates with the new pass
manager to support the actual uses of it. =]
When I ported instcombine to the new pass manager I discover that it
didn't work because TLI wasn't available in the right places. This is
a somewhat surprising and/or subtle aspect of the new pass manager
design that came up before but I think is useful to be reminded of:
While the new pass manager *allows* a function pass to query a module
analysis, it requires that the module analysis is already run and cached
prior to the function pass manager starting up, possibly with
a 'require<foo>' style utility in the pass pipeline. This is an
intentional hurdle because using a module analysis from a function pass
*requires* that the module analysis is run prior to entering the
function pass manager. Otherwise the other functions in the module could
be in who-knows-what state, etc.
A somewhat surprising consequence of this design decision (at least to
me) is that you have to design a function pass that leverages
a module analysis to do so as an optional feature. Even if that means
your function pass does no work in the absence of the module analysis,
you have to handle that possibility and remain conservatively correct.
This is a natural consequence of things being able to invalidate the
module analysis and us being unable to re-run it. And it's a generally
good thing because it lets us reorder passes arbitrarily without
breaking correctness, etc.
This ends up causing problems in one case. What if we have a module
analysis that is *definitionally* impossible to invalidate. In the
places this might come up, the analysis is usually also definitionally
trivial to run even while other transformation passes run on the module,
regardless of the state of anything. And so, it follows that it is
natural to have a hard requirement on such analyses from a function
pass.
It turns out, that TargetLibraryInfo is just such an analysis, and
InstCombine has a hard requirement on it.
The approach I've taken here is to produce an analysis that models this
flexibility by making it both a module and a function analysis. This
exposes the fact that it is in fact safe to compute at any point. We can
even make it a valid CGSCC analysis at some point if that is useful.
However, we don't want to have a copy of the actual target library info
state for each function! This state is specific to the triple. The
somewhat direct and blunt approach here is to turn TLI into a pimpl,
with the state and mutators in the implementation class and the query
routines primarily in the wrapper. Then the analysis can lazily
construct and cache the implementations, keyed on the triple, and
on-demand produce wrappers of them for each function.
One minor annoyance is that we will end up with a wrapper for each
function in the module. While this is a bit wasteful (one pointer per
function) it seems tolerable. And it has the advantage of ensuring that
we pay the absolute minimum synchronization cost to access this
information should we end up with a nice parallel function pass manager
in the future. We could look into trying to mark when analysis results
are especially cheap to recompute and more eagerly GC-ing the cached
results, or we could look at supporting a variant of analyses whose
results are specifically *not* cached and expected to just be used and
discarded by the consumer. Either way, these seem like incremental
enhancements that should happen when we start profiling the memory and
CPU usage of the new pass manager and not before.
The other minor annoyance is that if we end up using the TLI in both
a module pass and a function pass, those will be produced by two
separate analyses, and thus will point to separate copies of the
implementation state. While a minor issue, I dislike this and would like
to find a way to cleanly allow a single analysis instance to be used
across multiple IR unit managers. But I don't have a good solution to
this today, and I don't want to hold up all of the work waiting to come
up with one. This too seems like a reasonable thing to incrementally
improve later.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226981 91177308-0d34-0410-b5e6-96231b3b80d8
2015-01-24 02:06:09 +00:00
|
|
|
inline TargetLibraryInfoImpl *unwrap(LLVMTargetLibraryInfoRef P) {
|
|
|
|
return reinterpret_cast<TargetLibraryInfoImpl*>(P);
|
2013-04-22 22:47:22 +00:00
|
|
|
}
|
|
|
|
|
[PM] Rework how the TargetLibraryInfo pass integrates with the new pass
manager to support the actual uses of it. =]
When I ported instcombine to the new pass manager I discover that it
didn't work because TLI wasn't available in the right places. This is
a somewhat surprising and/or subtle aspect of the new pass manager
design that came up before but I think is useful to be reminded of:
While the new pass manager *allows* a function pass to query a module
analysis, it requires that the module analysis is already run and cached
prior to the function pass manager starting up, possibly with
a 'require<foo>' style utility in the pass pipeline. This is an
intentional hurdle because using a module analysis from a function pass
*requires* that the module analysis is run prior to entering the
function pass manager. Otherwise the other functions in the module could
be in who-knows-what state, etc.
A somewhat surprising consequence of this design decision (at least to
me) is that you have to design a function pass that leverages
a module analysis to do so as an optional feature. Even if that means
your function pass does no work in the absence of the module analysis,
you have to handle that possibility and remain conservatively correct.
This is a natural consequence of things being able to invalidate the
module analysis and us being unable to re-run it. And it's a generally
good thing because it lets us reorder passes arbitrarily without
breaking correctness, etc.
This ends up causing problems in one case. What if we have a module
analysis that is *definitionally* impossible to invalidate. In the
places this might come up, the analysis is usually also definitionally
trivial to run even while other transformation passes run on the module,
regardless of the state of anything. And so, it follows that it is
natural to have a hard requirement on such analyses from a function
pass.
It turns out, that TargetLibraryInfo is just such an analysis, and
InstCombine has a hard requirement on it.
The approach I've taken here is to produce an analysis that models this
flexibility by making it both a module and a function analysis. This
exposes the fact that it is in fact safe to compute at any point. We can
even make it a valid CGSCC analysis at some point if that is useful.
However, we don't want to have a copy of the actual target library info
state for each function! This state is specific to the triple. The
somewhat direct and blunt approach here is to turn TLI into a pimpl,
with the state and mutators in the implementation class and the query
routines primarily in the wrapper. Then the analysis can lazily
construct and cache the implementations, keyed on the triple, and
on-demand produce wrappers of them for each function.
One minor annoyance is that we will end up with a wrapper for each
function in the module. While this is a bit wasteful (one pointer per
function) it seems tolerable. And it has the advantage of ensuring that
we pay the absolute minimum synchronization cost to access this
information should we end up with a nice parallel function pass manager
in the future. We could look into trying to mark when analysis results
are especially cheap to recompute and more eagerly GC-ing the cached
results, or we could look at supporting a variant of analyses whose
results are specifically *not* cached and expected to just be used and
discarded by the consumer. Either way, these seem like incremental
enhancements that should happen when we start profiling the memory and
CPU usage of the new pass manager and not before.
The other minor annoyance is that if we end up using the TLI in both
a module pass and a function pass, those will be produced by two
separate analyses, and thus will point to separate copies of the
implementation state. While a minor issue, I dislike this and would like
to find a way to cleanly allow a single analysis instance to be used
across multiple IR unit managers. But I don't have a good solution to
this today, and I don't want to hold up all of the work waiting to come
up with one. This too seems like a reasonable thing to incrementally
improve later.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226981 91177308-0d34-0410-b5e6-96231b3b80d8
2015-01-24 02:06:09 +00:00
|
|
|
inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfoImpl *P) {
|
|
|
|
TargetLibraryInfoImpl *X = const_cast<TargetLibraryInfoImpl*>(P);
|
2013-04-22 22:47:22 +00:00
|
|
|
return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
|
|
|
|
}
|
|
|
|
|
2010-10-07 18:50:11 +00:00
|
|
|
void llvm::initializeTarget(PassRegistry &Registry) {
|
2015-01-15 10:41:28 +00:00
|
|
|
initializeTargetLibraryInfoWrapperPassPass(Registry);
|
[PM] Change the core design of the TTI analysis to use a polymorphic
type erased interface and a single analysis pass rather than an
extremely complex analysis group.
The end result is that the TTI analysis can contain a type erased
implementation that supports the polymorphic TTI interface. We can build
one from a target-specific implementation or from a dummy one in the IR.
I've also factored all of the code into "mix-in"-able base classes,
including CRTP base classes to facilitate calling back up to the most
specialized form when delegating horizontally across the surface. These
aren't as clean as I would like and I'm planning to work on cleaning
some of this up, but I wanted to start by putting into the right form.
There are a number of reasons for this change, and this particular
design. The first and foremost reason is that an analysis group is
complete overkill, and the chaining delegation strategy was so opaque,
confusing, and high overhead that TTI was suffering greatly for it.
Several of the TTI functions had failed to be implemented in all places
because of the chaining-based delegation making there be no checking of
this. A few other functions were implemented with incorrect delegation.
The message to me was very clear working on this -- the delegation and
analysis group structure was too confusing to be useful here.
The other reason of course is that this is *much* more natural fit for
the new pass manager. This will lay the ground work for a type-erased
per-function info object that can look up the correct subtarget and even
cache it.
Yet another benefit is that this will significantly simplify the
interaction of the pass managers and the TargetMachine. See the future
work below.
The downside of this change is that it is very, very verbose. I'm going
to work to improve that, but it is somewhat an implementation necessity
in C++ to do type erasure. =/ I discussed this design really extensively
with Eric and Hal prior to going down this path, and afterward showed
them the result. No one was really thrilled with it, but there doesn't
seem to be a substantially better alternative. Using a base class and
virtual method dispatch would make the code much shorter, but as
discussed in the update to the programmer's manual and elsewhere,
a polymorphic interface feels like the more principled approach even if
this is perhaps the least compelling example of it. ;]
Ultimately, there is still a lot more to be done here, but this was the
huge chunk that I couldn't really split things out of because this was
the interface change to TTI. I've tried to minimize all the other parts
of this. The follow up work should include at least:
1) Improving the TargetMachine interface by having it directly return
a TTI object. Because we have a non-pass object with value semantics
and an internal type erasure mechanism, we can narrow the interface
of the TargetMachine to *just* do what we need: build and return
a TTI object that we can then insert into the pass pipeline.
2) Make the TTI object be fully specialized for a particular function.
This will include splitting off a minimal form of it which is
sufficient for the inliner and the old pass manager.
3) Add a new pass manager analysis which produces TTI objects from the
target machine for each function. This may actually be done as part
of #2 in order to use the new analysis to implement #2.
4) Work on narrowing the API between TTI and the targets so that it is
easier to understand and less verbose to type erase.
5) Work on narrowing the API between TTI and its clients so that it is
easier to understand and less verbose to forward.
6) Try to improve the CRTP-based delegation. I feel like this code is
just a bit messy and exacerbating the complexity of implementing
the TTI in each target.
Many thanks to Eric and Hal for their help here. I ended up blocked on
this somewhat more abruptly than I expected, and so I appreciate getting
it sorted out very quickly.
Differential Revision: http://reviews.llvm.org/D7293
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@227669 91177308-0d34-0410-b5e6-96231b3b80d8
2015-01-31 03:43:40 +00:00
|
|
|
initializeTargetTransformInfoWrapperPassPass(Registry);
|
2010-10-07 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMInitializeTarget(LLVMPassRegistryRef R) {
|
|
|
|
initializeTarget(*unwrap(R));
|
|
|
|
}
|
|
|
|
|
2008-03-16 20:08:03 +00:00
|
|
|
LLVMTargetDataRef LLVMCreateTargetData(const char *StringRep) {
|
2012-10-08 16:38:25 +00:00
|
|
|
return wrap(new DataLayout(StringRep));
|
2008-03-16 20:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMAddTargetData(LLVMTargetDataRef TD, LLVMPassManagerRef PM) {
|
|
|
|
}
|
|
|
|
|
2011-07-25 21:20:54 +00:00
|
|
|
void LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI,
|
|
|
|
LLVMPassManagerRef PM) {
|
2015-01-15 10:41:28 +00:00
|
|
|
unwrap(PM)->add(new TargetLibraryInfoWrapperPass(*unwrap(TLI)));
|
2011-07-25 21:20:54 +00:00
|
|
|
}
|
|
|
|
|
2008-03-16 20:08:03 +00:00
|
|
|
char *LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD) {
|
|
|
|
std::string StringRep = unwrap(TD)->getStringRepresentation();
|
|
|
|
return strdup(StringRep.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMByteOrdering LLVMByteOrder(LLVMTargetDataRef TD) {
|
2010-01-09 22:27:07 +00:00
|
|
|
return unwrap(TD)->isLittleEndian() ? LLVMLittleEndian : LLVMBigEndian;
|
2008-03-16 20:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned LLVMPointerSize(LLVMTargetDataRef TD) {
|
2012-10-15 16:24:29 +00:00
|
|
|
return unwrap(TD)->getPointerSize(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned LLVMPointerSizeForAS(LLVMTargetDataRef TD, unsigned AS) {
|
|
|
|
return unwrap(TD)->getPointerSize(AS);
|
2008-03-16 20:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD) {
|
2012-11-01 08:07:29 +00:00
|
|
|
return wrap(unwrap(TD)->getIntPtrType(getGlobalContext()));
|
2008-03-16 20:08:03 +00:00
|
|
|
}
|
|
|
|
|
2012-10-15 16:24:29 +00:00
|
|
|
LLVMTypeRef LLVMIntPtrTypeForAS(LLVMTargetDataRef TD, unsigned AS) {
|
|
|
|
return wrap(unwrap(TD)->getIntPtrType(getGlobalContext(), AS));
|
|
|
|
}
|
|
|
|
|
2013-10-17 18:51:01 +00:00
|
|
|
LLVMTypeRef LLVMIntPtrTypeInContext(LLVMContextRef C, LLVMTargetDataRef TD) {
|
|
|
|
return wrap(unwrap(TD)->getIntPtrType(*unwrap(C)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMTypeRef LLVMIntPtrTypeForASInContext(LLVMContextRef C, LLVMTargetDataRef TD, unsigned AS) {
|
|
|
|
return wrap(unwrap(TD)->getIntPtrType(*unwrap(C), AS));
|
|
|
|
}
|
|
|
|
|
2008-03-16 20:08:03 +00:00
|
|
|
unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
|
|
|
|
return unwrap(TD)->getTypeSizeInBits(unwrap(Ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
|
|
|
|
return unwrap(TD)->getTypeStoreSize(unwrap(Ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long LLVMABISizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
|
2009-05-09 07:06:46 +00:00
|
|
|
return unwrap(TD)->getTypeAllocSize(unwrap(Ty));
|
2008-03-16 20:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
|
|
|
|
return unwrap(TD)->getABITypeAlignment(unwrap(Ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
|
2014-01-01 22:29:43 +00:00
|
|
|
return unwrap(TD)->getABITypeAlignment(unwrap(Ty));
|
2008-03-16 20:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
|
|
|
|
return unwrap(TD)->getPrefTypeAlignment(unwrap(Ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,
|
|
|
|
LLVMValueRef GlobalVar) {
|
|
|
|
return unwrap(TD)->getPreferredAlignment(unwrap<GlobalVariable>(GlobalVar));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
|
|
|
|
unsigned long long Offset) {
|
2011-07-18 04:54:35 +00:00
|
|
|
StructType *STy = unwrap<StructType>(StructTy);
|
2008-03-16 20:08:03 +00:00
|
|
|
return unwrap(TD)->getStructLayout(STy)->getElementContainingOffset(Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
|
|
|
|
unsigned Element) {
|
2011-07-18 04:54:35 +00:00
|
|
|
StructType *STy = unwrap<StructType>(StructTy);
|
2008-03-16 20:08:03 +00:00
|
|
|
return unwrap(TD)->getStructLayout(STy)->getElementOffset(Element);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeTargetData(LLVMTargetDataRef TD) {
|
|
|
|
delete unwrap(TD);
|
|
|
|
}
|