1/*
2 * Copyright 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 "bcc/Support/CompilerConfig.h"
18
19#include <llvm/CodeGen/SchedulerRegistry.h>
20#include <llvm/MC/SubtargetFeature.h>
21#include <llvm/Support/TargetRegistry.h>
22
23#include "bcc/Support/Log.h"
24#include "bcc/Support/TargetCompilerConfigs.h"
25
26using namespace bcc;
27
28CompilerConfig::CompilerConfig(const std::string &pTriple)
29  : mTriple(pTriple), mTarget(NULL) {
30  //===--------------------------------------------------------------------===//
31  // Default setting of register sheduler
32  //===--------------------------------------------------------------------===//
33  llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler);
34
35  //===--------------------------------------------------------------------===//
36  // Default setting of target options
37  //===--------------------------------------------------------------------===//
38  // Use hardfloat ABI by default.
39  //
40  // TODO(all): Need to detect the CPU capability and decide whether to use
41  // softfp. To use softfp, change the following 2 lines to
42  //
43  // options.FloatABIType = llvm::FloatABI::Soft;
44  // options.UseSoftFloat = true;
45  mTargetOpts.FloatABIType = llvm::FloatABI::Soft;
46  mTargetOpts.UseSoftFloat = false;
47
48  // Enable frame pointer elimination optimization by default.
49  mTargetOpts.NoFramePointerElim = false;
50
51  //===--------------------------------------------------------------------===//
52  // Default setting for code model
53  //===--------------------------------------------------------------------===//
54  mCodeModel = llvm::CodeModel::Small;
55
56  //===--------------------------------------------------------------------===//
57  // Default setting for relocation model
58  //===--------------------------------------------------------------------===//
59  mRelocModel = llvm::Reloc::Default;
60
61  //===--------------------------------------------------------------------===//
62  // Default setting for optimization level (-O2)
63  //===--------------------------------------------------------------------===//
64  mOptLevel = llvm::CodeGenOpt::Default;
65
66  //===--------------------------------------------------------------------===//
67  // Default setting for architecture type
68  //===--------------------------------------------------------------------===//
69  mArchType = llvm::Triple::UnknownArch;
70
71  initializeTarget();
72  initializeArch();
73
74  return;
75}
76
77bool CompilerConfig::initializeTarget() {
78  std::string error;
79  mTarget = llvm::TargetRegistry::lookupTarget(mTriple, error);
80  if (mTarget != NULL) {
81    return true;
82  } else {
83    ALOGE("Cannot initialize llvm::Target for given triple '%s'! (%s)",
84          mTriple.c_str(), error.c_str());
85    return false;
86  }
87}
88
89void CompilerConfig::initializeArch() {
90  if (mTarget != NULL) {
91    mArchType = llvm::Triple::getArchTypeForLLVMName(mTarget->getName());
92  } else {
93    mArchType = llvm::Triple::UnknownArch;
94  }
95  return;
96}
97
98void CompilerConfig::setFeatureString(const std::vector<std::string> &pAttrs) {
99  llvm::SubtargetFeatures f;
100
101  for (std::vector<std::string>::const_iterator attr_iter = pAttrs.begin(),
102           attr_end = pAttrs.end();
103       attr_iter != attr_end; attr_iter++) {
104    f.AddFeature(*attr_iter);
105  }
106
107  mFeatureString = f.getString();
108  return;
109}
110