xref: /aosp_15_r20/external/llvm/lib/Target/TargetMachineC.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- TargetMachine.cpp -------------------------------------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the LLVM-C part of TargetMachine.h
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #include "llvm-c/TargetMachine.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm-c/Core.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm-c/Target.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetTransformInfo.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DataLayout.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LegacyPassManager.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CodeGenCWrappers.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/FileSystem.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/FormattedStream.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Host.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/TargetRegistry.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
29*9880d681SAndroid Build Coastguard Worker #include <cassert>
30*9880d681SAndroid Build Coastguard Worker #include <cstdlib>
31*9880d681SAndroid Build Coastguard Worker #include <cstring>
32*9880d681SAndroid Build Coastguard Worker 
33*9880d681SAndroid Build Coastguard Worker using namespace llvm;
34*9880d681SAndroid Build Coastguard Worker 
unwrap(LLVMTargetMachineRef P)35*9880d681SAndroid Build Coastguard Worker static TargetMachine *unwrap(LLVMTargetMachineRef P) {
36*9880d681SAndroid Build Coastguard Worker   return reinterpret_cast<TargetMachine *>(P);
37*9880d681SAndroid Build Coastguard Worker }
unwrap(LLVMTargetRef P)38*9880d681SAndroid Build Coastguard Worker static Target *unwrap(LLVMTargetRef P) {
39*9880d681SAndroid Build Coastguard Worker   return reinterpret_cast<Target*>(P);
40*9880d681SAndroid Build Coastguard Worker }
wrap(const TargetMachine * P)41*9880d681SAndroid Build Coastguard Worker static LLVMTargetMachineRef wrap(const TargetMachine *P) {
42*9880d681SAndroid Build Coastguard Worker   return reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine *>(P));
43*9880d681SAndroid Build Coastguard Worker }
wrap(const Target * P)44*9880d681SAndroid Build Coastguard Worker static LLVMTargetRef wrap(const Target * P) {
45*9880d681SAndroid Build Coastguard Worker   return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
46*9880d681SAndroid Build Coastguard Worker }
47*9880d681SAndroid Build Coastguard Worker 
LLVMGetFirstTarget()48*9880d681SAndroid Build Coastguard Worker LLVMTargetRef LLVMGetFirstTarget() {
49*9880d681SAndroid Build Coastguard Worker   if (TargetRegistry::targets().begin() == TargetRegistry::targets().end()) {
50*9880d681SAndroid Build Coastguard Worker     return nullptr;
51*9880d681SAndroid Build Coastguard Worker   }
52*9880d681SAndroid Build Coastguard Worker 
53*9880d681SAndroid Build Coastguard Worker   const Target *target = &*TargetRegistry::targets().begin();
54*9880d681SAndroid Build Coastguard Worker   return wrap(target);
55*9880d681SAndroid Build Coastguard Worker }
LLVMGetNextTarget(LLVMTargetRef T)56*9880d681SAndroid Build Coastguard Worker LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
57*9880d681SAndroid Build Coastguard Worker   return wrap(unwrap(T)->getNext());
58*9880d681SAndroid Build Coastguard Worker }
59*9880d681SAndroid Build Coastguard Worker 
LLVMGetTargetFromName(const char * Name)60*9880d681SAndroid Build Coastguard Worker LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
61*9880d681SAndroid Build Coastguard Worker   StringRef NameRef = Name;
62*9880d681SAndroid Build Coastguard Worker   auto I = std::find_if(
63*9880d681SAndroid Build Coastguard Worker       TargetRegistry::targets().begin(), TargetRegistry::targets().end(),
64*9880d681SAndroid Build Coastguard Worker       [&](const Target &T) { return T.getName() == NameRef; });
65*9880d681SAndroid Build Coastguard Worker   return I != TargetRegistry::targets().end() ? wrap(&*I) : nullptr;
66*9880d681SAndroid Build Coastguard Worker }
67*9880d681SAndroid Build Coastguard Worker 
LLVMGetTargetFromTriple(const char * TripleStr,LLVMTargetRef * T,char ** ErrorMessage)68*9880d681SAndroid Build Coastguard Worker LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
69*9880d681SAndroid Build Coastguard Worker                                  char **ErrorMessage) {
70*9880d681SAndroid Build Coastguard Worker   std::string Error;
71*9880d681SAndroid Build Coastguard Worker 
72*9880d681SAndroid Build Coastguard Worker   *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
73*9880d681SAndroid Build Coastguard Worker 
74*9880d681SAndroid Build Coastguard Worker   if (!*T) {
75*9880d681SAndroid Build Coastguard Worker     if (ErrorMessage)
76*9880d681SAndroid Build Coastguard Worker       *ErrorMessage = strdup(Error.c_str());
77*9880d681SAndroid Build Coastguard Worker 
78*9880d681SAndroid Build Coastguard Worker     return 1;
79*9880d681SAndroid Build Coastguard Worker   }
80*9880d681SAndroid Build Coastguard Worker 
81*9880d681SAndroid Build Coastguard Worker   return 0;
82*9880d681SAndroid Build Coastguard Worker }
83*9880d681SAndroid Build Coastguard Worker 
LLVMGetTargetName(LLVMTargetRef T)84*9880d681SAndroid Build Coastguard Worker const char * LLVMGetTargetName(LLVMTargetRef T) {
85*9880d681SAndroid Build Coastguard Worker   return unwrap(T)->getName();
86*9880d681SAndroid Build Coastguard Worker }
87*9880d681SAndroid Build Coastguard Worker 
LLVMGetTargetDescription(LLVMTargetRef T)88*9880d681SAndroid Build Coastguard Worker const char * LLVMGetTargetDescription(LLVMTargetRef T) {
89*9880d681SAndroid Build Coastguard Worker   return unwrap(T)->getShortDescription();
90*9880d681SAndroid Build Coastguard Worker }
91*9880d681SAndroid Build Coastguard Worker 
LLVMTargetHasJIT(LLVMTargetRef T)92*9880d681SAndroid Build Coastguard Worker LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
93*9880d681SAndroid Build Coastguard Worker   return unwrap(T)->hasJIT();
94*9880d681SAndroid Build Coastguard Worker }
95*9880d681SAndroid Build Coastguard Worker 
LLVMTargetHasTargetMachine(LLVMTargetRef T)96*9880d681SAndroid Build Coastguard Worker LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
97*9880d681SAndroid Build Coastguard Worker   return unwrap(T)->hasTargetMachine();
98*9880d681SAndroid Build Coastguard Worker }
99*9880d681SAndroid Build Coastguard Worker 
LLVMTargetHasAsmBackend(LLVMTargetRef T)100*9880d681SAndroid Build Coastguard Worker LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
101*9880d681SAndroid Build Coastguard Worker   return unwrap(T)->hasMCAsmBackend();
102*9880d681SAndroid Build Coastguard Worker }
103*9880d681SAndroid Build Coastguard Worker 
LLVMCreateTargetMachine(LLVMTargetRef T,const char * Triple,const char * CPU,const char * Features,LLVMCodeGenOptLevel Level,LLVMRelocMode Reloc,LLVMCodeModel CodeModel)104*9880d681SAndroid Build Coastguard Worker LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
105*9880d681SAndroid Build Coastguard Worker         const char* Triple, const char* CPU, const char* Features,
106*9880d681SAndroid Build Coastguard Worker         LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
107*9880d681SAndroid Build Coastguard Worker         LLVMCodeModel CodeModel) {
108*9880d681SAndroid Build Coastguard Worker   Optional<Reloc::Model> RM;
109*9880d681SAndroid Build Coastguard Worker   switch (Reloc){
110*9880d681SAndroid Build Coastguard Worker     case LLVMRelocStatic:
111*9880d681SAndroid Build Coastguard Worker       RM = Reloc::Static;
112*9880d681SAndroid Build Coastguard Worker       break;
113*9880d681SAndroid Build Coastguard Worker     case LLVMRelocPIC:
114*9880d681SAndroid Build Coastguard Worker       RM = Reloc::PIC_;
115*9880d681SAndroid Build Coastguard Worker       break;
116*9880d681SAndroid Build Coastguard Worker     case LLVMRelocDynamicNoPic:
117*9880d681SAndroid Build Coastguard Worker       RM = Reloc::DynamicNoPIC;
118*9880d681SAndroid Build Coastguard Worker       break;
119*9880d681SAndroid Build Coastguard Worker     default:
120*9880d681SAndroid Build Coastguard Worker       break;
121*9880d681SAndroid Build Coastguard Worker   }
122*9880d681SAndroid Build Coastguard Worker 
123*9880d681SAndroid Build Coastguard Worker   CodeModel::Model CM = unwrap(CodeModel);
124*9880d681SAndroid Build Coastguard Worker 
125*9880d681SAndroid Build Coastguard Worker   CodeGenOpt::Level OL;
126*9880d681SAndroid Build Coastguard Worker   switch (Level) {
127*9880d681SAndroid Build Coastguard Worker     case LLVMCodeGenLevelNone:
128*9880d681SAndroid Build Coastguard Worker       OL = CodeGenOpt::None;
129*9880d681SAndroid Build Coastguard Worker       break;
130*9880d681SAndroid Build Coastguard Worker     case LLVMCodeGenLevelLess:
131*9880d681SAndroid Build Coastguard Worker       OL = CodeGenOpt::Less;
132*9880d681SAndroid Build Coastguard Worker       break;
133*9880d681SAndroid Build Coastguard Worker     case LLVMCodeGenLevelAggressive:
134*9880d681SAndroid Build Coastguard Worker       OL = CodeGenOpt::Aggressive;
135*9880d681SAndroid Build Coastguard Worker       break;
136*9880d681SAndroid Build Coastguard Worker     default:
137*9880d681SAndroid Build Coastguard Worker       OL = CodeGenOpt::Default;
138*9880d681SAndroid Build Coastguard Worker       break;
139*9880d681SAndroid Build Coastguard Worker   }
140*9880d681SAndroid Build Coastguard Worker 
141*9880d681SAndroid Build Coastguard Worker   TargetOptions opt;
142*9880d681SAndroid Build Coastguard Worker   return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
143*9880d681SAndroid Build Coastguard Worker     CM, OL));
144*9880d681SAndroid Build Coastguard Worker }
145*9880d681SAndroid Build Coastguard Worker 
LLVMDisposeTargetMachine(LLVMTargetMachineRef T)146*9880d681SAndroid Build Coastguard Worker void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) { delete unwrap(T); }
147*9880d681SAndroid Build Coastguard Worker 
LLVMGetTargetMachineTarget(LLVMTargetMachineRef T)148*9880d681SAndroid Build Coastguard Worker LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
149*9880d681SAndroid Build Coastguard Worker   const Target* target = &(unwrap(T)->getTarget());
150*9880d681SAndroid Build Coastguard Worker   return wrap(target);
151*9880d681SAndroid Build Coastguard Worker }
152*9880d681SAndroid Build Coastguard Worker 
LLVMGetTargetMachineTriple(LLVMTargetMachineRef T)153*9880d681SAndroid Build Coastguard Worker char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
154*9880d681SAndroid Build Coastguard Worker   std::string StringRep = unwrap(T)->getTargetTriple().str();
155*9880d681SAndroid Build Coastguard Worker   return strdup(StringRep.c_str());
156*9880d681SAndroid Build Coastguard Worker }
157*9880d681SAndroid Build Coastguard Worker 
LLVMGetTargetMachineCPU(LLVMTargetMachineRef T)158*9880d681SAndroid Build Coastguard Worker char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
159*9880d681SAndroid Build Coastguard Worker   std::string StringRep = unwrap(T)->getTargetCPU();
160*9880d681SAndroid Build Coastguard Worker   return strdup(StringRep.c_str());
161*9880d681SAndroid Build Coastguard Worker }
162*9880d681SAndroid Build Coastguard Worker 
LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T)163*9880d681SAndroid Build Coastguard Worker char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
164*9880d681SAndroid Build Coastguard Worker   std::string StringRep = unwrap(T)->getTargetFeatureString();
165*9880d681SAndroid Build Coastguard Worker   return strdup(StringRep.c_str());
166*9880d681SAndroid Build Coastguard Worker }
167*9880d681SAndroid Build Coastguard Worker 
LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,LLVMBool VerboseAsm)168*9880d681SAndroid Build Coastguard Worker void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
169*9880d681SAndroid Build Coastguard Worker                                       LLVMBool VerboseAsm) {
170*9880d681SAndroid Build Coastguard Worker   unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
171*9880d681SAndroid Build Coastguard Worker }
172*9880d681SAndroid Build Coastguard Worker 
LLVMCreateTargetDataLayout(LLVMTargetMachineRef T)173*9880d681SAndroid Build Coastguard Worker LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T) {
174*9880d681SAndroid Build Coastguard Worker   return wrap(new DataLayout(unwrap(T)->createDataLayout()));
175*9880d681SAndroid Build Coastguard Worker }
176*9880d681SAndroid Build Coastguard Worker 
LLVMTargetMachineEmit(LLVMTargetMachineRef T,LLVMModuleRef M,raw_pwrite_stream & OS,LLVMCodeGenFileType codegen,char ** ErrorMessage)177*9880d681SAndroid Build Coastguard Worker static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
178*9880d681SAndroid Build Coastguard Worker                                       raw_pwrite_stream &OS,
179*9880d681SAndroid Build Coastguard Worker                                       LLVMCodeGenFileType codegen,
180*9880d681SAndroid Build Coastguard Worker                                       char **ErrorMessage) {
181*9880d681SAndroid Build Coastguard Worker   TargetMachine* TM = unwrap(T);
182*9880d681SAndroid Build Coastguard Worker   Module* Mod = unwrap(M);
183*9880d681SAndroid Build Coastguard Worker 
184*9880d681SAndroid Build Coastguard Worker   legacy::PassManager pass;
185*9880d681SAndroid Build Coastguard Worker 
186*9880d681SAndroid Build Coastguard Worker   std::string error;
187*9880d681SAndroid Build Coastguard Worker 
188*9880d681SAndroid Build Coastguard Worker   Mod->setDataLayout(TM->createDataLayout());
189*9880d681SAndroid Build Coastguard Worker 
190*9880d681SAndroid Build Coastguard Worker   TargetMachine::CodeGenFileType ft;
191*9880d681SAndroid Build Coastguard Worker   switch (codegen) {
192*9880d681SAndroid Build Coastguard Worker     case LLVMAssemblyFile:
193*9880d681SAndroid Build Coastguard Worker       ft = TargetMachine::CGFT_AssemblyFile;
194*9880d681SAndroid Build Coastguard Worker       break;
195*9880d681SAndroid Build Coastguard Worker     default:
196*9880d681SAndroid Build Coastguard Worker       ft = TargetMachine::CGFT_ObjectFile;
197*9880d681SAndroid Build Coastguard Worker       break;
198*9880d681SAndroid Build Coastguard Worker   }
199*9880d681SAndroid Build Coastguard Worker   if (TM->addPassesToEmitFile(pass, OS, ft)) {
200*9880d681SAndroid Build Coastguard Worker     error = "TargetMachine can't emit a file of this type";
201*9880d681SAndroid Build Coastguard Worker     *ErrorMessage = strdup(error.c_str());
202*9880d681SAndroid Build Coastguard Worker     return true;
203*9880d681SAndroid Build Coastguard Worker   }
204*9880d681SAndroid Build Coastguard Worker 
205*9880d681SAndroid Build Coastguard Worker   pass.run(*Mod);
206*9880d681SAndroid Build Coastguard Worker 
207*9880d681SAndroid Build Coastguard Worker   OS.flush();
208*9880d681SAndroid Build Coastguard Worker   return false;
209*9880d681SAndroid Build Coastguard Worker }
210*9880d681SAndroid Build Coastguard Worker 
LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T,LLVMModuleRef M,char * Filename,LLVMCodeGenFileType codegen,char ** ErrorMessage)211*9880d681SAndroid Build Coastguard Worker LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
212*9880d681SAndroid Build Coastguard Worker   char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
213*9880d681SAndroid Build Coastguard Worker   std::error_code EC;
214*9880d681SAndroid Build Coastguard Worker   raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
215*9880d681SAndroid Build Coastguard Worker   if (EC) {
216*9880d681SAndroid Build Coastguard Worker     *ErrorMessage = strdup(EC.message().c_str());
217*9880d681SAndroid Build Coastguard Worker     return true;
218*9880d681SAndroid Build Coastguard Worker   }
219*9880d681SAndroid Build Coastguard Worker   bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
220*9880d681SAndroid Build Coastguard Worker   dest.flush();
221*9880d681SAndroid Build Coastguard Worker   return Result;
222*9880d681SAndroid Build Coastguard Worker }
223*9880d681SAndroid Build Coastguard Worker 
LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,LLVMModuleRef M,LLVMCodeGenFileType codegen,char ** ErrorMessage,LLVMMemoryBufferRef * OutMemBuf)224*9880d681SAndroid Build Coastguard Worker LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
225*9880d681SAndroid Build Coastguard Worker   LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
226*9880d681SAndroid Build Coastguard Worker   LLVMMemoryBufferRef *OutMemBuf) {
227*9880d681SAndroid Build Coastguard Worker   SmallString<0> CodeString;
228*9880d681SAndroid Build Coastguard Worker   raw_svector_ostream OStream(CodeString);
229*9880d681SAndroid Build Coastguard Worker   bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
230*9880d681SAndroid Build Coastguard Worker 
231*9880d681SAndroid Build Coastguard Worker   StringRef Data = OStream.str();
232*9880d681SAndroid Build Coastguard Worker   *OutMemBuf =
233*9880d681SAndroid Build Coastguard Worker       LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), "");
234*9880d681SAndroid Build Coastguard Worker   return Result;
235*9880d681SAndroid Build Coastguard Worker }
236*9880d681SAndroid Build Coastguard Worker 
LLVMGetDefaultTargetTriple(void)237*9880d681SAndroid Build Coastguard Worker char *LLVMGetDefaultTargetTriple(void) {
238*9880d681SAndroid Build Coastguard Worker   return strdup(sys::getDefaultTargetTriple().c_str());
239*9880d681SAndroid Build Coastguard Worker }
240*9880d681SAndroid Build Coastguard Worker 
LLVMAddAnalysisPasses(LLVMTargetMachineRef T,LLVMPassManagerRef PM)241*9880d681SAndroid Build Coastguard Worker void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) {
242*9880d681SAndroid Build Coastguard Worker   unwrap(PM)->add(
243*9880d681SAndroid Build Coastguard Worker       createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));
244*9880d681SAndroid Build Coastguard Worker }
245