[PM] Change the core design of the TTI analysis to use a polymorphic
[oota-llvm.git] / lib / Target / Target.cpp
1 //===-- Target.cpp --------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the common infrastructure (including C bindings) for 
11 // libLLVMTarget.a, which implements target information.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm-c/Target.h"
16 #include "llvm-c/Initialization.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/Value.h"
20 #include "llvm/InitializePasses.h"
21 #include "llvm/PassManager.h"
22 #include "llvm/Analysis/TargetLibraryInfo.h"
23 #include <cstring>
24
25 using namespace llvm;
26
27 inline TargetLibraryInfoImpl *unwrap(LLVMTargetLibraryInfoRef P) {
28   return reinterpret_cast<TargetLibraryInfoImpl*>(P);
29 }
30
31 inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfoImpl *P) {
32   TargetLibraryInfoImpl *X = const_cast<TargetLibraryInfoImpl*>(P);
33   return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
34 }
35
36 void llvm::initializeTarget(PassRegistry &Registry) {
37   initializeDataLayoutPassPass(Registry);
38   initializeTargetLibraryInfoWrapperPassPass(Registry);
39   initializeTargetTransformInfoWrapperPassPass(Registry);
40 }
41
42 void LLVMInitializeTarget(LLVMPassRegistryRef R) {
43   initializeTarget(*unwrap(R));
44 }
45
46 LLVMTargetDataRef LLVMCreateTargetData(const char *StringRep) {
47   return wrap(new DataLayout(StringRep));
48 }
49
50 void LLVMAddTargetData(LLVMTargetDataRef TD, LLVMPassManagerRef PM) {
51   // The DataLayoutPass must now be in sync with the module. Unfortunatelly we
52   // cannot enforce that from the C api.
53   unwrap(PM)->add(new DataLayoutPass());
54 }
55
56 void LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI,
57                               LLVMPassManagerRef PM) {
58   unwrap(PM)->add(new TargetLibraryInfoWrapperPass(*unwrap(TLI)));
59 }
60
61 char *LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD) {
62   std::string StringRep = unwrap(TD)->getStringRepresentation();
63   return strdup(StringRep.c_str());
64 }
65
66 LLVMByteOrdering LLVMByteOrder(LLVMTargetDataRef TD) {
67   return unwrap(TD)->isLittleEndian() ? LLVMLittleEndian : LLVMBigEndian;
68 }
69
70 unsigned LLVMPointerSize(LLVMTargetDataRef TD) {
71   return unwrap(TD)->getPointerSize(0);
72 }
73
74 unsigned LLVMPointerSizeForAS(LLVMTargetDataRef TD, unsigned AS) {
75   return unwrap(TD)->getPointerSize(AS);
76 }
77
78 LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD) {
79   return wrap(unwrap(TD)->getIntPtrType(getGlobalContext()));
80 }
81
82 LLVMTypeRef LLVMIntPtrTypeForAS(LLVMTargetDataRef TD, unsigned AS) {
83   return wrap(unwrap(TD)->getIntPtrType(getGlobalContext(), AS));
84 }
85
86 LLVMTypeRef LLVMIntPtrTypeInContext(LLVMContextRef C, LLVMTargetDataRef TD) {
87   return wrap(unwrap(TD)->getIntPtrType(*unwrap(C)));
88 }
89
90 LLVMTypeRef LLVMIntPtrTypeForASInContext(LLVMContextRef C, LLVMTargetDataRef TD, unsigned AS) {
91   return wrap(unwrap(TD)->getIntPtrType(*unwrap(C), AS));
92 }
93
94 unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
95   return unwrap(TD)->getTypeSizeInBits(unwrap(Ty));
96 }
97
98 unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
99   return unwrap(TD)->getTypeStoreSize(unwrap(Ty));
100 }
101
102 unsigned long long LLVMABISizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
103   return unwrap(TD)->getTypeAllocSize(unwrap(Ty));
104 }
105
106 unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
107   return unwrap(TD)->getABITypeAlignment(unwrap(Ty));
108 }
109
110 unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
111   return unwrap(TD)->getABITypeAlignment(unwrap(Ty));
112 }
113
114 unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
115   return unwrap(TD)->getPrefTypeAlignment(unwrap(Ty));
116 }
117
118 unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,
119                                         LLVMValueRef GlobalVar) {
120   return unwrap(TD)->getPreferredAlignment(unwrap<GlobalVariable>(GlobalVar));
121 }
122
123 unsigned LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
124                              unsigned long long Offset) {
125   StructType *STy = unwrap<StructType>(StructTy);
126   return unwrap(TD)->getStructLayout(STy)->getElementContainingOffset(Offset);
127 }
128
129 unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
130                                        unsigned Element) {
131   StructType *STy = unwrap<StructType>(StructTy);
132   return unwrap(TD)->getStructLayout(STy)->getElementOffset(Element);
133 }
134
135 void LLVMDisposeTargetData(LLVMTargetDataRef TD) {
136   delete unwrap(TD);
137 }