1 /*
2 * Copyright 2010-2012, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "slang_backend.h"
18
19 #include <string>
20 #include <vector>
21
22 #include "bcinfo/BitcodeWrapper.h"
23
24 #include "clang/AST/ASTContext.h"
25 #include "clang/AST/Decl.h"
26 #include "clang/AST/DeclGroup.h"
27
28 #include "clang/Basic/Diagnostic.h"
29 #include "clang/Basic/TargetInfo.h"
30 #include "clang/Basic/TargetOptions.h"
31
32 #include "clang/CodeGen/ModuleBuilder.h"
33
34 #include "clang/Frontend/CodeGenOptions.h"
35 #include "clang/Frontend/FrontendDiagnostic.h"
36
37 #include "llvm/Assembly/PrintModulePass.h"
38
39 #include "llvm/Bitcode/ReaderWriter.h"
40
41 #include "llvm/CodeGen/RegAllocRegistry.h"
42 #include "llvm/CodeGen/SchedulerRegistry.h"
43
44 #include "llvm/IR/LLVMContext.h"
45 #include "llvm/IR/Module.h"
46 #include "llvm/IR/Metadata.h"
47
48 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
49
50 #include "llvm/IR/DataLayout.h"
51 #include "llvm/Target/TargetMachine.h"
52 #include "llvm/Target/TargetOptions.h"
53 #include "llvm/Support/TargetRegistry.h"
54
55 #include "llvm/MC/SubtargetFeature.h"
56
57 #include "slang_assert.h"
58 #include "BitWriter_2_9/ReaderWriter_2_9.h"
59 #include "BitWriter_2_9_func/ReaderWriter_2_9_func.h"
60 #include "BitWriter_3_2/ReaderWriter_3_2.h"
61
62 namespace slang {
63
CreateFunctionPasses()64 void Backend::CreateFunctionPasses() {
65 if (!mPerFunctionPasses) {
66 mPerFunctionPasses = new llvm::FunctionPassManager(mpModule);
67 mPerFunctionPasses->add(new llvm::DataLayout(mpModule));
68
69 llvm::PassManagerBuilder PMBuilder;
70 PMBuilder.OptLevel = mCodeGenOpts.OptimizationLevel;
71 PMBuilder.populateFunctionPassManager(*mPerFunctionPasses);
72 }
73 return;
74 }
75
CreateModulePasses()76 void Backend::CreateModulePasses() {
77 if (!mPerModulePasses) {
78 mPerModulePasses = new llvm::PassManager();
79 mPerModulePasses->add(new llvm::DataLayout(mpModule));
80
81 llvm::PassManagerBuilder PMBuilder;
82 PMBuilder.OptLevel = mCodeGenOpts.OptimizationLevel;
83 PMBuilder.SizeLevel = mCodeGenOpts.OptimizeSize;
84 PMBuilder.SizeLevel = mCodeGenOpts.OptimizeSize;
85 if (mCodeGenOpts.UnitAtATime) {
86 PMBuilder.DisableUnitAtATime = 0;
87 } else {
88 PMBuilder.DisableUnitAtATime = 1;
89 }
90
91 if (mCodeGenOpts.UnrollLoops) {
92 PMBuilder.DisableUnrollLoops = 0;
93 } else {
94 PMBuilder.DisableUnrollLoops = 1;
95 }
96
97 PMBuilder.populateModulePassManager(*mPerModulePasses);
98 }
99 return;
100 }
101
CreateCodeGenPasses()102 bool Backend::CreateCodeGenPasses() {
103 if ((mOT != Slang::OT_Assembly) && (mOT != Slang::OT_Object))
104 return true;
105
106 // Now we add passes for code emitting
107 if (mCodeGenPasses) {
108 return true;
109 } else {
110 mCodeGenPasses = new llvm::FunctionPassManager(mpModule);
111 mCodeGenPasses->add(new llvm::DataLayout(mpModule));
112 }
113
114 // Create the TargetMachine for generating code.
115 std::string Triple = mpModule->getTargetTriple();
116
117 std::string Error;
118 const llvm::Target* TargetInfo =
119 llvm::TargetRegistry::lookupTarget(Triple, Error);
120 if (TargetInfo == NULL) {
121 mDiagEngine.Report(clang::diag::err_fe_unable_to_create_target) << Error;
122 return false;
123 }
124
125 // Target Machine Options
126 llvm::TargetOptions Options;
127
128 Options.NoFramePointerElim = mCodeGenOpts.DisableFPElim;
129
130 // Use hardware FPU.
131 //
132 // FIXME: Need to detect the CPU capability and decide whether to use softfp.
133 // To use softfp, change following 2 lines to
134 //
135 // Options.FloatABIType = llvm::FloatABI::Soft;
136 // Options.UseSoftFloat = true;
137 Options.FloatABIType = llvm::FloatABI::Hard;
138 Options.UseSoftFloat = false;
139
140 // BCC needs all unknown symbols resolved at compilation time. So we don't
141 // need any relocation model.
142 llvm::Reloc::Model RM = llvm::Reloc::Static;
143
144 // This is set for the linker (specify how large of the virtual addresses we
145 // can access for all unknown symbols.)
146 llvm::CodeModel::Model CM;
147 if (mpModule->getPointerSize() == llvm::Module::Pointer32) {
148 CM = llvm::CodeModel::Small;
149 } else {
150 // The target may have pointer size greater than 32 (e.g. x86_64
151 // architecture) may need large data address model
152 CM = llvm::CodeModel::Medium;
153 }
154
155 // Setup feature string
156 std::string FeaturesStr;
157 if (mTargetOpts.CPU.size() || mTargetOpts.Features.size()) {
158 llvm::SubtargetFeatures Features;
159
160 for (std::vector<std::string>::const_iterator
161 I = mTargetOpts.Features.begin(), E = mTargetOpts.Features.end();
162 I != E;
163 I++)
164 Features.AddFeature(*I);
165
166 FeaturesStr = Features.getString();
167 }
168
169 llvm::TargetMachine *TM =
170 TargetInfo->createTargetMachine(Triple, mTargetOpts.CPU, FeaturesStr,
171 Options, RM, CM);
172
173 // Register scheduler
174 llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler);
175
176 // Register allocation policy:
177 // createFastRegisterAllocator: fast but bad quality
178 // createGreedyRegisterAllocator: not so fast but good quality
179 llvm::RegisterRegAlloc::setDefault((mCodeGenOpts.OptimizationLevel == 0) ?
180 llvm::createFastRegisterAllocator :
181 llvm::createGreedyRegisterAllocator);
182
183 llvm::CodeGenOpt::Level OptLevel = llvm::CodeGenOpt::Default;
184 if (mCodeGenOpts.OptimizationLevel == 0) {
185 OptLevel = llvm::CodeGenOpt::None;
186 } else if (mCodeGenOpts.OptimizationLevel == 3) {
187 OptLevel = llvm::CodeGenOpt::Aggressive;
188 }
189
190 llvm::TargetMachine::CodeGenFileType CGFT =
191 llvm::TargetMachine::CGFT_AssemblyFile;
192 if (mOT == Slang::OT_Object) {
193 CGFT = llvm::TargetMachine::CGFT_ObjectFile;
194 }
195 if (TM->addPassesToEmitFile(*mCodeGenPasses, FormattedOutStream,
196 CGFT, OptLevel)) {
197 mDiagEngine.Report(clang::diag::err_fe_unable_to_interface_with_target);
198 return false;
199 }
200
201 return true;
202 }
203
Backend(clang::DiagnosticsEngine * DiagEngine,const clang::CodeGenOptions & CodeGenOpts,const clang::TargetOptions & TargetOpts,PragmaList * Pragmas,llvm::raw_ostream * OS,Slang::OutputType OT)204 Backend::Backend(clang::DiagnosticsEngine *DiagEngine,
205 const clang::CodeGenOptions &CodeGenOpts,
206 const clang::TargetOptions &TargetOpts,
207 PragmaList *Pragmas,
208 llvm::raw_ostream *OS,
209 Slang::OutputType OT)
210 : ASTConsumer(),
211 mTargetOpts(TargetOpts),
212 mpModule(NULL),
213 mpOS(OS),
214 mOT(OT),
215 mGen(NULL),
216 mPerFunctionPasses(NULL),
217 mPerModulePasses(NULL),
218 mCodeGenPasses(NULL),
219 mLLVMContext(llvm::getGlobalContext()),
220 mDiagEngine(*DiagEngine),
221 mCodeGenOpts(CodeGenOpts),
222 mPragmas(Pragmas) {
223 FormattedOutStream.setStream(*mpOS,
224 llvm::formatted_raw_ostream::PRESERVE_STREAM);
225 mGen = CreateLLVMCodeGen(mDiagEngine, "", mCodeGenOpts,
226 mTargetOpts, mLLVMContext);
227 return;
228 }
229
Initialize(clang::ASTContext & Ctx)230 void Backend::Initialize(clang::ASTContext &Ctx) {
231 mGen->Initialize(Ctx);
232
233 mpModule = mGen->GetModule();
234
235 return;
236 }
237
238 // Encase the Bitcode in a wrapper containing RS version information.
WrapBitcode(llvm::raw_string_ostream & Bitcode)239 void Backend::WrapBitcode(llvm::raw_string_ostream &Bitcode) {
240 bcinfo::AndroidBitcodeWrapper wrapper;
241 size_t actualWrapperLen = bcinfo::writeAndroidBitcodeWrapper(
242 &wrapper, Bitcode.str().length(), getTargetAPI(),
243 SlangVersion::CURRENT, mCodeGenOpts.OptimizationLevel);
244
245 slangAssert(actualWrapperLen > 0);
246
247 // Write out the bitcode wrapper.
248 FormattedOutStream.write(reinterpret_cast<char*>(&wrapper), actualWrapperLen);
249
250 // Write out the actual encoded bitcode.
251 FormattedOutStream << Bitcode.str();
252 return;
253 }
254
HandleTopLevelDecl(clang::DeclGroupRef D)255 bool Backend::HandleTopLevelDecl(clang::DeclGroupRef D) {
256 return mGen->HandleTopLevelDecl(D);
257 }
258
HandleTranslationUnit(clang::ASTContext & Ctx)259 void Backend::HandleTranslationUnit(clang::ASTContext &Ctx) {
260 HandleTranslationUnitPre(Ctx);
261
262 mGen->HandleTranslationUnit(Ctx);
263
264 // Here, we complete a translation unit (whole translation unit is now in LLVM
265 // IR). Now, interact with LLVM backend to generate actual machine code (asm
266 // or machine code, whatever.)
267
268 // Silently ignore if we weren't initialized for some reason.
269 if (!mpModule)
270 return;
271
272 llvm::Module *M = mGen->ReleaseModule();
273 if (!M) {
274 // The module has been released by IR gen on failures, do not double free.
275 mpModule = NULL;
276 return;
277 }
278
279 slangAssert(mpModule == M &&
280 "Unexpected module change during LLVM IR generation");
281
282 // Insert #pragma information into metadata section of module
283 if (!mPragmas->empty()) {
284 llvm::NamedMDNode *PragmaMetadata =
285 mpModule->getOrInsertNamedMetadata(Slang::PragmaMetadataName);
286 for (PragmaList::const_iterator I = mPragmas->begin(), E = mPragmas->end();
287 I != E;
288 I++) {
289 llvm::SmallVector<llvm::Value*, 2> Pragma;
290 // Name goes first
291 Pragma.push_back(llvm::MDString::get(mLLVMContext, I->first));
292 // And then value
293 Pragma.push_back(llvm::MDString::get(mLLVMContext, I->second));
294
295 // Create MDNode and insert into PragmaMetadata
296 PragmaMetadata->addOperand(
297 llvm::MDNode::get(mLLVMContext, Pragma));
298 }
299 }
300
301 HandleTranslationUnitPost(mpModule);
302
303 // Create passes for optimization and code emission
304
305 // Create and run per-function passes
306 CreateFunctionPasses();
307 if (mPerFunctionPasses) {
308 mPerFunctionPasses->doInitialization();
309
310 for (llvm::Module::iterator I = mpModule->begin(), E = mpModule->end();
311 I != E;
312 I++)
313 if (!I->isDeclaration())
314 mPerFunctionPasses->run(*I);
315
316 mPerFunctionPasses->doFinalization();
317 }
318
319 // Create and run module passes
320 CreateModulePasses();
321 if (mPerModulePasses)
322 mPerModulePasses->run(*mpModule);
323
324 switch (mOT) {
325 case Slang::OT_Assembly:
326 case Slang::OT_Object: {
327 if (!CreateCodeGenPasses())
328 return;
329
330 mCodeGenPasses->doInitialization();
331
332 for (llvm::Module::iterator I = mpModule->begin(), E = mpModule->end();
333 I != E;
334 I++)
335 if (!I->isDeclaration())
336 mCodeGenPasses->run(*I);
337
338 mCodeGenPasses->doFinalization();
339 break;
340 }
341 case Slang::OT_LLVMAssembly: {
342 llvm::PassManager *LLEmitPM = new llvm::PassManager();
343 LLEmitPM->add(llvm::createPrintModulePass(&FormattedOutStream));
344 LLEmitPM->run(*mpModule);
345 break;
346 }
347 case Slang::OT_Bitcode: {
348 llvm::PassManager *BCEmitPM = new llvm::PassManager();
349 std::string BCStr;
350 llvm::raw_string_ostream Bitcode(BCStr);
351 unsigned int TargetAPI = getTargetAPI();
352 switch (TargetAPI) {
353 case SLANG_HC_TARGET_API:
354 case SLANG_HC_MR1_TARGET_API:
355 case SLANG_HC_MR2_TARGET_API: {
356 // Pre-ICS targets must use the LLVM 2.9 BitcodeWriter
357 BCEmitPM->add(llvm_2_9::createBitcodeWriterPass(Bitcode));
358 break;
359 }
360 case SLANG_ICS_TARGET_API:
361 case SLANG_ICS_MR1_TARGET_API: {
362 // ICS targets must use the LLVM 2.9_func BitcodeWriter
363 BCEmitPM->add(llvm_2_9_func::createBitcodeWriterPass(Bitcode));
364 break;
365 }
366 default: {
367 if (TargetAPI < SLANG_MINIMUM_TARGET_API ||
368 TargetAPI > SLANG_MAXIMUM_TARGET_API) {
369 slangAssert(false && "Invalid target API value");
370 }
371 // Switch to the 3.2 BitcodeWriter by default, and don't use
372 // LLVM's included BitcodeWriter at all (for now).
373 BCEmitPM->add(llvm_3_2::createBitcodeWriterPass(Bitcode));
374 //BCEmitPM->add(llvm::createBitcodeWriterPass(Bitcode));
375 break;
376 }
377 }
378
379 BCEmitPM->run(*mpModule);
380 WrapBitcode(Bitcode);
381 break;
382 }
383 case Slang::OT_Nothing: {
384 return;
385 }
386 default: {
387 slangAssert(false && "Unknown output type");
388 }
389 }
390
391 FormattedOutStream.flush();
392
393 return;
394 }
395
HandleTagDeclDefinition(clang::TagDecl * D)396 void Backend::HandleTagDeclDefinition(clang::TagDecl *D) {
397 mGen->HandleTagDeclDefinition(D);
398 return;
399 }
400
CompleteTentativeDefinition(clang::VarDecl * D)401 void Backend::CompleteTentativeDefinition(clang::VarDecl *D) {
402 mGen->CompleteTentativeDefinition(D);
403 return;
404 }
405
~Backend()406 Backend::~Backend() {
407 delete mpModule;
408 delete mGen;
409 delete mPerFunctionPasses;
410 delete mPerModulePasses;
411 delete mCodeGenPasses;
412 return;
413 }
414
415 } // namespace slang
416