#include "llvm-internal.h"
#include "llvm-debug.h"
#include "llvm-file-ostream.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/PassManager.h"
#include "llvm/ValueSymbolTable.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Analysis/Verifier.h"
#include "llvm/Assembly/Writer.h"
#include "llvm/Assembly/PrintModulePass.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/CodeGen/RegAllocRegistry.h"
#include "llvm/Target/SubtargetFeature.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetLowering.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetRegistry.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/StandardPasses.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/System/Program.h"
#include <cassert>
#undef VISIBILITY_HIDDEN
extern "C" {
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "except.h"
#include "flags.h"
#include "tree.h"
#include "diagnostic.h"
#include "output.h"
#include "target.h"
#include "toplev.h"
#include "timevar.h"
#include "function.h"
#include "tree-inline.h"
#include "langhooks.h"
#include "cgraph.h"
#include "params.h"
}
int flag_llvm_pch_read;
int flag_no_simplify_libcalls;
static int flag_disable_red_zone = 0;
static int flag_no_implicit_float = 0;
Module *TheModule = 0;
DebugInfo *TheDebugInfo = 0;
TargetMachine *TheTarget = 0;
TargetFolder *TheFolder = 0;
TypeConverter *TheTypeConverter = 0;
std::vector<tree> Thunks;
static cl::opt<bool> DisableLLVMOptimizations("disable-llvm-optzns");
std::vector<std::pair<Constant*, int> > StaticCtors, StaticDtors;
SmallSetVector<Constant*, 32> AttributeUsedGlobals;
SmallSetVector<Constant*, 32> AttributeCompilerUsedGlobals;
std::vector<Constant*> AttributeAnnotateGlobals;
static FunctionPassManager *PerFunctionPasses = 0;
static PassManager *PerModulePasses = 0;
static FunctionPassManager *CodeGenPasses = 0;
static void createPerFunctionOptimizationPasses();
static void createPerModuleOptimizationPasses();
static void destroyOptimizationPasses();
static std::vector<Value*> LLVMValues;
typedef DenseMap<Value*, unsigned> LLVMValuesMapTy;
static LLVMValuesMapTy LLVMValuesMap;
static std::vector<unsigned> LocalLLVMValueIDs;
void llvm_set_decl(tree Tr, Value *V) {
if (!V) {
if (GET_DECL_LLVM_INDEX(Tr))
SET_DECL_LLVM_INDEX(Tr, 0);
return;
}
unsigned &ValueSlot = LLVMValuesMap[V];
if (ValueSlot) {
SET_DECL_LLVM_INDEX(Tr, ValueSlot);
return;
}
LLVMValues.push_back(V);
unsigned Index = LLVMValues.size();
SET_DECL_LLVM_INDEX(Tr, Index);
LLVMValuesMap[V] = Index;
if (!isa<Constant>(V))
LocalLLVMValueIDs.push_back(Index);
}
bool llvm_set_decl_p(tree Tr) {
unsigned Index = GET_DECL_LLVM_INDEX(Tr);
if (Index == 0)
return false;
return LLVMValues[Index - 1] != 0;
}
Value *llvm_get_decl(tree Tr) {
unsigned Index = GET_DECL_LLVM_INDEX(Tr);
if (Index == 0) {
make_decl_llvm(Tr);
Index = GET_DECL_LLVM_INDEX(Tr);
if (Index == 0) return 0;
}
assert((Index - 1) < LLVMValues.size() && "Invalid LLVM value index");
assert(LLVMValues[Index - 1] && "Trying to use deleted LLVM value!");
return LLVMValues[Index - 1];
}
void changeLLVMConstant(Constant *Old, Constant *New) {
assert(Old->use_empty() && "Old value has uses!");
if (AttributeUsedGlobals.count(Old)) {
AttributeUsedGlobals.remove(Old);
AttributeUsedGlobals.insert(New);
}
if (AttributeCompilerUsedGlobals.count(Old)) {
AttributeCompilerUsedGlobals.remove(Old);
AttributeCompilerUsedGlobals.insert(New);
}
for (unsigned i = 0, e = StaticCtors.size(); i != e; ++i) {
if (StaticCtors[i].first == Old)
StaticCtors[i].first = New;
}
for (unsigned i = 0, e = StaticDtors.size(); i != e; ++i) {
if (StaticDtors[i].first == Old)
StaticDtors[i].first = New;
}
assert(!LLVMValuesMap.count(New) && "New cannot be in the LLVMValues map!");
LLVMValuesMapTy::iterator I = LLVMValuesMap.find(Old);
if (I == LLVMValuesMap.end()) return;
unsigned Idx = I->second-1;
assert(Idx < LLVMValues.size() && "Out of range index!");
assert(LLVMValues[Idx] == Old && "Inconsistent LLVMValues mapping!");
LLVMValues[Idx] = New;
LLVMValuesMap.erase(I);
if (New)
LLVMValuesMap[New] = Idx+1;
}
void readLLVMValues() {
GlobalValue *V = TheModule->getNamedGlobal("llvm.pch.values");
if (!V)
return;
GlobalVariable *GV = cast<GlobalVariable>(V);
ConstantStruct *ValuesFromPCH = cast<ConstantStruct>(GV->getOperand(0));
for (unsigned i = 0; i < ValuesFromPCH->getNumOperands(); ++i) {
Value *Va = ValuesFromPCH->getOperand(i);
if (!Va) {
LLVMValues.push_back(Va);
continue;
}
if (ConstantArray *CA = dyn_cast<ConstantArray>(Va)) {
std::string Str = CA->getAsString();
Va = TheModule->getValueSymbolTable().lookup(Str);
}
assert (Va != NULL && "Invalid Value in LLVMValues string table");
LLVMValues.push_back(Va);
}
GV->eraseFromParent();
}
void writeLLVMValues() {
if (LLVMValues.empty())
return;
LLVMContext &Context = getGlobalContext();
std::vector<Constant *> ValuesForPCH;
for (std::vector<Value *>::iterator I = LLVMValues.begin(),
E = LLVMValues.end(); I != E; ++I) {
if (Constant *C = dyn_cast_or_null<Constant>(*I))
ValuesForPCH.push_back(C);
else
ValuesForPCH.push_back(Constant::getNullValue(Type::getInt32Ty(Context)));
}
Constant *LLVMValuesTable = ConstantStruct::get(Context, ValuesForPCH, false);
new GlobalVariable(*TheModule, LLVMValuesTable->getType(), true,
GlobalValue::ExternalLinkage,
LLVMValuesTable,
"llvm.pch.values");
}
void eraseLocalLLVMValues() {
while (!LocalLLVMValueIDs.empty()) {
unsigned Idx = LocalLLVMValueIDs.back()-1;
LocalLLVMValueIDs.pop_back();
if (Value *V = LLVMValues[Idx]) {
assert(!isa<Constant>(V) && "Found global value");
LLVMValuesMap.erase(V);
}
if (Idx == LLVMValues.size()-1)
LLVMValues.pop_back();
else
LLVMValues[Idx] = 0;
}
}
void handleVisibility(tree decl, GlobalValue *GV) {
if (TREE_PUBLIC(decl) &&
(DECL_VISIBILITY_SPECIFIED(decl) || !DECL_EXTERNAL(decl))) {
if (DECL_VISIBILITY(decl) == VISIBILITY_HIDDEN)
GV->setVisibility(GlobalValue::HiddenVisibility);
else if (DECL_VISIBILITY(decl) == VISIBILITY_PROTECTED)
GV->setVisibility(GlobalValue::ProtectedVisibility);
else if (DECL_VISIBILITY(decl) == VISIBILITY_DEFAULT)
GV->setVisibility(Function::DefaultVisibility);
}
}
#ifndef LLVM_TARGET_NAME
#error LLVM_TARGET_NAME macro not specified by GCC backend
#endif
namespace llvm {
#define Declare2(TARG, MOD) extern "C" void LLVMInitialize ## TARG ## MOD()
#define Declare(T, M) Declare2(T, M)
Declare(LLVM_TARGET_NAME, TargetInfo);
Declare(LLVM_TARGET_NAME, Target);
Declare(LLVM_TARGET_NAME, AsmPrinter);
#undef Declare
#undef Declare2
}
static unsigned GuessAtInliningThreshold() {
if (optimize_size)
return 75;
if (optimize >= 3)
return 275;
return 225;
}
void llvm_initialize_backend(void) {
#define DoInit2(TARG, MOD) LLVMInitialize ## TARG ## MOD()
#define DoInit(T, M) DoInit2(T, M)
DoInit(LLVM_TARGET_NAME, TargetInfo);
DoInit(LLVM_TARGET_NAME, Target);
DoInit(LLVM_TARGET_NAME, AsmPrinter);
#undef DoInit
#undef DoInit2
std::vector<const char*> Args;
Args.push_back(progname);
#ifdef LLVM_SET_RED_ZONE_FLAG
LLVM_SET_RED_ZONE_FLAG(flag_disable_red_zone)
#endif
#ifdef LLVM_SET_TARGET_OPTIONS
LLVM_SET_TARGET_OPTIONS(Args);
#endif
#ifdef LLVM_SET_MACHINE_OPTIONS
LLVM_SET_MACHINE_OPTIONS(Args);
#endif
#ifdef LLVM_SET_IMPLICIT_FLOAT
LLVM_SET_IMPLICIT_FLOAT(flag_no_implicit_float)
#endif
if (time_report)
Args.push_back("--time-passes");
if (fast_math_flags_set_p())
Args.push_back("--enable-unsafe-fp-math");
if (flag_finite_math_only)
Args.push_back("--enable-finite-only-fp-math");
if (!flag_omit_frame_pointer)
Args.push_back("--disable-fp-elim");
if (!flag_zero_initialized_in_bss)
Args.push_back("--nozero-initialized-in-bss");
if (flag_verbose_asm)
Args.push_back("--asm-verbose");
if (flag_debug_pass_structure)
Args.push_back("--debug-pass=Structure");
if (flag_debug_pass_arguments)
Args.push_back("--debug-pass=Arguments");
if (flag_unwind_tables)
Args.push_back("--unwind-tables");
if (!flag_schedule_insns)
Args.push_back("--pre-RA-sched=source");
std::vector<std::string> ArgStrings;
if (flag_inline_trees > 1) {
unsigned threshold = GuessAtInliningThreshold();
std::string Arg("--inline-threshold="+utostr(threshold));
ArgStrings.push_back(Arg);
}
if (flag_reorder_blocks) {
ArgStrings.push_back("--tail-dup-size=5");
ArgStrings.push_back("--tail-merge-size=6");
}
if (flag_limited_precision > 0) {
std::string Arg("--limit-float-precision="+utostr(flag_limited_precision));
ArgStrings.push_back(Arg);
}
if (flag_stack_protect > 0) {
std::string Arg("--stack-protector-buffer-size=" +
utostr(PARAM_VALUE(PARAM_SSP_BUFFER_SIZE)));
ArgStrings.push_back(Arg);
}
for (unsigned i = 0, e = ArgStrings.size(); i != e; ++i)
Args.push_back(ArgStrings[i].c_str());
std::vector<std::string> LLVM_Optns; if (llvm_optns) {
llvm::SmallVector<llvm::StringRef, 16> Buf;
SplitString(llvm_optns, Buf);
for(unsigned i = 0, e = Buf.size(); i != e; ++i) {
LLVM_Optns.push_back(Buf[i]);
Args.push_back(LLVM_Optns.back().c_str());
}
}
Args.push_back(0); int pseudo_argc = Args.size()-1;
cl::ParseCommandLineOptions(pseudo_argc, (char**)&Args[0]);
TheModule = new Module("", getGlobalContext());
std::string TargetTriple = TARGET_NAME;
#ifdef LLVM_OVERRIDE_TARGET_ARCH
std::string Arch = LLVM_OVERRIDE_TARGET_ARCH();
if (!Arch.empty()) {
std::string::size_type DashPos = TargetTriple.find('-');
if (DashPos != std::string::npos) TargetTriple = Arch + TargetTriple.substr(DashPos);
}
#endif
#ifdef LLVM_OVERRIDE_TARGET_VERSION
char *NewTriple;
bool OverRidden = LLVM_OVERRIDE_TARGET_VERSION(TargetTriple.c_str(),
&NewTriple);
if (OverRidden)
TargetTriple = std::string(NewTriple);
#endif
TheModule->setTargetTriple(TargetTriple);
TheTypeConverter = new TypeConverter();
std::string Err;
const Target *TME =
TargetRegistry::lookupTarget(TargetTriple, Err);
if (!TME) {
errs() << "Did not get a target machine! Triplet is " << TargetTriple<<'\n';
exit(1);
}
std::string FeatureStr;
#ifdef LLVM_SET_SUBTARGET_FEATURES
SubtargetFeatures Features;
LLVM_SET_SUBTARGET_FEATURES(Features);
FeatureStr = Features.getString();
#endif
TheTarget = TME->createTargetMachine(TargetTriple, FeatureStr);
assert(TheTarget->getTargetData()->isBigEndian() == BYTES_BIG_ENDIAN);
TheFolder = new TargetFolder(TheTarget->getTargetData());
TheModule->setDataLayout(TheTarget->getTargetData()->
getStringRepresentation());
if (optimize)
RegisterRegAlloc::setDefault(createLinearScanRegisterAllocator);
else
RegisterRegAlloc::setDefault(createLocalRegisterAllocator);
if (!flag_pch_file &&
debug_info_level > DINFO_LEVEL_NONE)
TheDebugInfo = new DebugInfo(TheModule);
else
TheDebugInfo = 0;
}
void performLateBackendInitialization(void) {
if (USING_SJLJ_EXCEPTIONS)
SjLjExceptionHandling = flag_exceptions;
else
DwarfExceptionHandling = flag_exceptions;
for (Module::iterator I = TheModule->begin(), E = TheModule->end();
I != E; ++I)
if (!I->isDeclaration()) {
if (flag_disable_red_zone)
I->addFnAttr(Attribute::NoRedZone);
if (flag_no_implicit_float)
I->addFnAttr(Attribute::NoImplicitFloat);
}
}
void llvm_lang_dependent_init(const char *Name) {
if (TheDebugInfo)
TheDebugInfo->Initialize();
if (Name)
TheModule->setModuleIdentifier(Name);
}
oFILEstream *AsmOutStream = 0;
static formatted_raw_ostream *AsmOutRawStream = 0;
oFILEstream *AsmIntermediateOutStream = 0;
void llvm_pch_read(const unsigned char *Buffer, unsigned Size) {
std::string ModuleName = TheModule->getModuleIdentifier();
delete TheModule;
delete TheDebugInfo;
clearTargetBuiltinCache();
MemoryBuffer *MB = MemoryBuffer::getNewMemBuffer(Size, ModuleName.c_str());
memcpy((char*)MB->getBufferStart(), Buffer, Size);
std::string ErrMsg;
TheModule = ParseBitcodeFile(MB, getGlobalContext(), &ErrMsg);
delete MB;
if (!flag_pch_file && debug_info_level > DINFO_LEVEL_NONE) {
TheDebugInfo = new DebugInfo(TheModule);
TheDebugInfo->Initialize();
}
if (!TheModule) {
errs() << "Error reading bytecodes from PCH file\n";
errs() << ErrMsg << "\n";
exit(1);
}
if (PerFunctionPasses || PerModulePasses) {
destroyOptimizationPasses();
if (flag_pch_file)
llvm_pch_write_init();
}
readLLVMTypesStringTable();
readLLVMValues();
flag_llvm_pch_read = 1;
}
void llvm_pch_write_init(void) {
timevar_push(TV_LLVM_INIT);
AsmOutStream = new oFILEstream(asm_out_file);
AsmOutRawStream =
new formatted_raw_ostream(*new raw_os_ostream(*AsmOutStream),
formatted_raw_ostream::DELETE_STREAM);
PerModulePasses = new PassManager();
PerModulePasses->add(new TargetData(*TheTarget->getTargetData()));
if (asm_out_file == stdout)
sys::Program::ChangeStdoutToBinary();
PerModulePasses->add(createBitcodeWriterPass(*AsmOutRawStream));
flag_no_ident = 1;
flag_llvm_pch_read = 0;
timevar_pop(TV_LLVM_INIT);
}
static void destroyOptimizationPasses() {
delete PerFunctionPasses;
delete PerModulePasses;
delete CodeGenPasses;
PerFunctionPasses = 0;
PerModulePasses = 0;
CodeGenPasses = 0;
}
static void createPerFunctionOptimizationPasses() {
if (PerFunctionPasses)
return;
PerFunctionPasses =
new FunctionPassManager(TheModule);
PerFunctionPasses->add(new TargetData(*TheTarget->getTargetData()));
bool HasPerFunctionPasses = false;
#ifdef ENABLE_CHECKING
PerFunctionPasses->add(createVerifierPass());
HasPerFunctionPasses = true;
#endif
if (optimize > 0 && !DisableLLVMOptimizations) {
HasPerFunctionPasses = true;
PerFunctionPasses->add(createCFGSimplificationPass());
if (optimize == 1)
PerFunctionPasses->add(createPromoteMemoryToRegisterPass());
else
PerFunctionPasses->add(createScalarReplAggregatesPass());
PerFunctionPasses->add(createInstructionCombiningPass());
}
if (!emit_llvm_bc && !emit_llvm && 0) {
FunctionPassManager *PM = PerFunctionPasses;
HasPerFunctionPasses = true;
CodeGenOpt::Level OptLevel = CodeGenOpt::Default; if (optimize == 0)
OptLevel = CodeGenOpt::None;
else if (optimize == 1)
OptLevel = CodeGenOpt::Less;
else if (optimize == 3)
OptLevel = CodeGenOpt::Aggressive;
#ifndef NDEBUG
bool DisableVerify = false;
#else
bool DisableVerify = true;
#endif
if (TheTarget->addPassesToEmitFile(*PM, *AsmOutRawStream,
TargetMachine::CGFT_AssemblyFile,
OptLevel, DisableVerify)) {
errs() << "Error interfacing to target machine!\n";
exit(1);
}
}
if (HasPerFunctionPasses) {
PerFunctionPasses->doInitialization();
} else {
delete PerFunctionPasses;
PerFunctionPasses = 0;
}
}
static void createPerModuleOptimizationPasses() {
if (PerModulePasses)
return;
PerModulePasses = new PassManager();
PerModulePasses->add(new TargetData(*TheTarget->getTargetData()));
bool HasPerModulePasses = false;
if (!DisableLLVMOptimizations) {
bool NeedAlwaysInliner = false;
llvm::Pass *InliningPass = 0;
if (flag_inline_trees > 1) { InliningPass = createFunctionInliningPass(); } else {
for (Module::iterator I = TheModule->begin(), E = TheModule->end();
I != E; ++I)
if (I->hasFnAttr(Attribute::AlwaysInline)) {
NeedAlwaysInliner = true;
break;
}
if (NeedAlwaysInliner)
InliningPass = createAlwaysInlinerPass(); }
HasPerModulePasses = true;
createStandardModulePasses(PerModulePasses, optimize,
optimize_size,
flag_unit_at_a_time, flag_unroll_loops,
!flag_no_simplify_libcalls, flag_exceptions,
InliningPass);
}
if (emit_llvm_bc) {
PerModulePasses->add(createBitcodeWriterPass(*AsmOutRawStream));
HasPerModulePasses = true;
} else if (emit_llvm) {
PerModulePasses->add(createPrintModulePass(AsmOutRawStream));
HasPerModulePasses = true;
} else {
if (PerModulePasses || 1) {
FunctionPassManager *PM = CodeGenPasses =
new FunctionPassManager(TheModule);
PM->add(new TargetData(*TheTarget->getTargetData()));
CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
switch (optimize) {
default: break;
case 0: OptLevel = CodeGenOpt::None; break;
case 3: OptLevel = CodeGenOpt::Aggressive; break;
}
#ifndef NDEBUG
bool DisableVerify = false;
#else
bool DisableVerify = true;
#endif
if (TheTarget->addPassesToEmitFile(*PM, *AsmOutRawStream,
TargetMachine::CGFT_AssemblyFile,
OptLevel, DisableVerify)) {
errs() << "Error interfacing to target machine!\n";
exit(1);
}
}
}
if (!HasPerModulePasses) {
delete PerModulePasses;
PerModulePasses = 0;
}
}
void llvm_asm_file_start(void) {
timevar_push(TV_LLVM_INIT);
AsmOutStream = new oFILEstream(asm_out_file);
AsmOutRawStream =
new formatted_raw_ostream(*new raw_os_ostream(*AsmOutStream),
formatted_raw_ostream::DELETE_STREAM);
flag_llvm_pch_read = 0;
if (emit_llvm_bc || emit_llvm)
flag_no_ident = 1;
if (asm_out_file == stdout)
sys::Program::ChangeStdoutToBinary();
AttributeUsedGlobals.clear();
AttributeCompilerUsedGlobals.clear();
timevar_pop(TV_LLVM_INIT);
}
static void CreateStructorsList(std::vector<std::pair<Constant*, int> > &Tors,
const char *Name) {
std::vector<Constant*> InitList;
std::vector<Constant*> StructInit;
StructInit.resize(2);
LLVMContext &Context = getGlobalContext();
const Type *FPTy =
FunctionType::get(Type::getVoidTy(Context),
std::vector<const Type*>(), false);
FPTy = FPTy->getPointerTo();
for (unsigned i = 0, e = Tors.size(); i != e; ++i) {
StructInit[0] = ConstantInt::get(Type::getInt32Ty(Context), Tors[i].second);
StructInit[1] = TheFolder->CreateBitCast(Tors[i].first, FPTy);
InitList.push_back(ConstantStruct::get(Context, StructInit, false));
}
Constant *Array = ConstantArray::get(
ArrayType::get(InitList[0]->getType(), InitList.size()), InitList);
new GlobalVariable(*TheModule, Array->getType(), false,
GlobalValue::AppendingLinkage,
Array, Name);
}
void llvm_asm_file_end(void) {
timevar_push(TV_LLVM_PERFILE);
LLVMContext &Context = getGlobalContext();
SmallPtrSet<tree, 4> ThunkOfThunk;
for (std::vector<tree>::iterator
I = Thunks.begin(), E = Thunks.end(); I != E; ++I) {
tree thunk = *I;
tree thunk_target = lang_hooks.thunk_target(thunk);
if (lang_hooks.function_is_thunk_p (thunk_target)) {
ThunkOfThunk.insert(thunk);
continue;
}
Function *Thunk = cast<Function>(DECL_LLVM(thunk));
const Function *ThunkTarget = cast<Function>(DECL_LLVM(thunk_target));
Thunk->setLinkage(ThunkTarget->getLinkage());
Thunk->setVisibility(ThunkTarget->getVisibility());
}
while (!ThunkOfThunk.empty())
for (SmallPtrSet<tree, 4>::iterator
I = ThunkOfThunk.begin(), E = ThunkOfThunk.end(); I != E; ++I) {
tree thunk = *I;
tree thunk_target = lang_hooks.thunk_target(thunk);
if (!ThunkOfThunk.count(thunk_target)) {
Function *Thunk = cast<Function>(DECL_LLVM(thunk));
const Function *ThunkTarget = cast<Function>(DECL_LLVM(thunk_target));
Thunk->setLinkage(ThunkTarget->getLinkage());
Thunk->setVisibility(ThunkTarget->getVisibility());
ThunkOfThunk.erase(thunk);
break;
}
}
performLateBackendInitialization();
createPerFunctionOptimizationPasses();
if (flag_pch_file) {
writeLLVMTypesStringTable();
writeLLVMValues();
writeLLVMTypeUsers();
}
if (!StaticCtors.empty())
CreateStructorsList(StaticCtors, "llvm.global_ctors");
if (!StaticDtors.empty())
CreateStructorsList(StaticDtors, "llvm.global_dtors");
if (!AttributeUsedGlobals.empty()) {
std::vector<Constant *> AUGs;
const Type *SBP= Type::getInt8PtrTy(Context);
for (SmallSetVector<Constant *,32>::iterator
AI = AttributeUsedGlobals.begin(),
AE = AttributeUsedGlobals.end(); AI != AE; ++AI) {
Constant *C = *AI;
AUGs.push_back(TheFolder->CreateBitCast(C, SBP));
}
ArrayType *AT = ArrayType::get(SBP, AUGs.size());
Constant *Init = ConstantArray::get(AT, AUGs);
GlobalValue *gv = new GlobalVariable(*TheModule, AT, false,
GlobalValue::AppendingLinkage, Init,
"llvm.used");
gv->setSection("llvm.metadata");
AttributeUsedGlobals.clear();
}
if (!AttributeCompilerUsedGlobals.empty()) {
std::vector<Constant *> ACUGs;
const Type *SBP= Type::getInt8PtrTy(Context);
for (SmallSetVector<Constant *,32>::iterator
AI = AttributeCompilerUsedGlobals.begin(),
AE = AttributeCompilerUsedGlobals.end(); AI != AE; ++AI) {
Constant *C = *AI;
ACUGs.push_back(TheFolder->CreateBitCast(C, SBP));
}
ArrayType *AT = ArrayType::get(SBP, ACUGs.size());
Constant *Init = ConstantArray::get(AT, ACUGs);
GlobalValue *gv = new GlobalVariable(*TheModule, AT, false,
GlobalValue::AppendingLinkage, Init,
"llvm.compiler.used");
gv->setSection("llvm.metadata");
AttributeCompilerUsedGlobals.clear();
}
if (!AttributeAnnotateGlobals.empty()) {
Constant *Array = ConstantArray::get(
ArrayType::get(AttributeAnnotateGlobals[0]->getType(),
AttributeAnnotateGlobals.size()),
AttributeAnnotateGlobals);
GlobalValue *gv = new GlobalVariable(*TheModule, Array->getType(), false,
GlobalValue::AppendingLinkage, Array,
"llvm.global.annotations");
gv->setSection("llvm.metadata");
AttributeAnnotateGlobals.clear();
}
if (PerFunctionPasses)
PerFunctionPasses->doFinalization();
if (flag_debug_llvm_module_opt) {
static PassManager *IntermediatePM = new PassManager();
IntermediatePM->add(new TargetData(*TheTarget->getTargetData()));
char asm_intermediate_out_filename[MAXPATHLEN];
strcpy(&asm_intermediate_out_filename[0], asm_file_name);
strcat(&asm_intermediate_out_filename[0],".0");
FILE *asm_intermediate_out_file = fopen(asm_intermediate_out_filename, "w+b");
AsmIntermediateOutStream = new oFILEstream(asm_intermediate_out_file);
raw_ostream *AsmIntermediateRawOutStream =
new raw_os_ostream(*AsmIntermediateOutStream);
if (emit_llvm_bc)
IntermediatePM->add(createBitcodeWriterPass(*AsmIntermediateRawOutStream));
if (emit_llvm)
IntermediatePM->add(createPrintModulePass(AsmIntermediateRawOutStream));
IntermediatePM->run(*TheModule);
AsmIntermediateRawOutStream->flush();
delete AsmIntermediateRawOutStream;
AsmIntermediateRawOutStream = 0;
AsmIntermediateOutStream->flush();
fflush(asm_intermediate_out_file);
delete AsmIntermediateOutStream;
AsmIntermediateOutStream = 0;
}
createPerModuleOptimizationPasses();
if (PerModulePasses)
PerModulePasses->run(*TheModule);
if (CodeGenPasses) {
CodeGenPasses->doInitialization();
for (Module::iterator I = TheModule->begin(), E = TheModule->end();
I != E; ++I)
if (!I->isDeclaration())
CodeGenPasses->run(*I);
CodeGenPasses->doFinalization();
}
AsmOutRawStream->flush();
AsmOutStream->flush();
fflush(asm_out_file);
delete AsmOutRawStream;
AsmOutRawStream = 0;
delete AsmOutStream;
AsmOutStream = 0;
timevar_pop(TV_LLVM_PERFILE);
}
void llvm_call_llvm_shutdown(void) {
#ifndef NDEBUG
delete TheModule;
llvm_shutdown();
#endif
}
void llvm_emit_code_for_current_function(tree fndecl) {
if (cfun->nonlocal_goto_save_area)
sorry("%Jnon-local gotos not supported by LLVM", fndecl);
if (errorcount || sorrycount) {
TREE_ASM_WRITTEN(fndecl) = 1;
return; }
{
static bool done = false;
if (!done && flag_llvm_pch_read) {
readLLVMTypeUsers();
done = true;
}
}
timevar_push(TV_LLVM_FUNCS);
Function *Fn;
{
TreeToLLVM *Emitter = new TreeToLLVM(fndecl);
enum symbol_visibility vis = DECL_VISIBILITY (fndecl);
if (vis != VISIBILITY_DEFAULT)
targetm.asm_out.visibility(fndecl, vis);
Fn = TheTreeToLLVM->EmitFunction();
Emitter->~TreeToLLVM();
}
#if 0
if (dump_file) {
fprintf (dump_file,
"\n\n;;\n;; Full LLVM generated for this function:\n;;\n");
Fn->dump();
}
#endif
performLateBackendInitialization();
createPerFunctionOptimizationPasses();
if (PerFunctionPasses)
PerFunctionPasses->run(*Fn);
DECL_DEFER_OUTPUT(fndecl) = 0;
TREE_ASM_WRITTEN(fndecl) = 1;
timevar_pop(TV_LLVM_FUNCS);
}
void emit_alias_to_llvm(tree decl, tree target, tree target_decl) {
if (errorcount || sorrycount) {
TREE_ASM_WRITTEN(decl) = 1;
return; }
timevar_push(TV_LLVM_GLOBALS);
GlobalValue *V = cast<GlobalValue>(DECL_LLVM(decl));
GlobalValue *Aliasee = NULL;
if (target_decl)
Aliasee = cast<GlobalValue>(DECL_LLVM(target_decl));
else {
const char* AliaseeName = IDENTIFIER_POINTER(target);
Aliasee =
dyn_cast_or_null<GlobalValue>(TheModule->
getValueSymbolTable().lookup(AliaseeName));
if (!Aliasee) {
std::string starred = std::string("\001") + AliaseeName;
Aliasee =
dyn_cast_or_null<GlobalValue>(TheModule->
getValueSymbolTable().lookup(starred));
}
if (!Aliasee) {
if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))) {
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
Aliasee = new GlobalVariable(*TheModule, GV->getType(),
GV->isConstant(),
GlobalVariable::ExternalWeakLinkage,
NULL, AliaseeName);
else if (Function *F = dyn_cast<Function>(V))
Aliasee = Function::Create(F->getFunctionType(),
Function::ExternalWeakLinkage,
AliaseeName, TheModule);
else
assert(0 && "Unsuported global value");
} else {
error ("%J%qD aliased to undefined symbol %qs", decl, decl, AliaseeName);
timevar_pop(TV_LLVM_GLOBALS);
return;
}
}
}
GlobalValue::LinkageTypes Linkage;
if (DECL_LLVM_PRIVATE(decl))
Linkage = GlobalValue::PrivateLinkage;
else if (DECL_LLVM_LINKER_PRIVATE(decl))
Linkage = GlobalValue::LinkerPrivateLinkage;
else if (DECL_LLVM_LINKER_PRIVATE_WEAK(decl))
Linkage = GlobalValue::LinkerPrivateWeakLinkage;
else if (DECL_WEAK(decl))
Linkage = GlobalValue::WeakAnyLinkage;
else if (!TREE_PUBLIC(decl))
Linkage = GlobalValue::InternalLinkage;
else
Linkage = GlobalValue::ExternalLinkage;
GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), Linkage, "",
Aliasee, TheModule);
handleVisibility(decl, GA);
if (GA->getType()->canLosslesslyBitCastTo(V->getType()))
V->replaceAllUsesWith(ConstantExpr::getBitCast(GA, V->getType()));
else if (!V->use_empty()) {
error ("%J Alias %qD used with invalid type!", decl, decl);
timevar_pop(TV_LLVM_GLOBALS);
return;
}
changeLLVMConstant(V, GA);
GA->takeName(V);
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
GV->eraseFromParent();
else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
GA->eraseFromParent();
else if (Function *F = dyn_cast<Function>(V))
F->eraseFromParent();
else
assert(0 && "Unsuported global value");
TREE_ASM_WRITTEN(decl) = 1;
timevar_pop(TV_LLVM_GLOBALS);
return;
}
Constant* ConvertMetadataStringToGV(const char *str) {
Constant *Init = ConstantArray::get(getGlobalContext(), std::string(str));
static std::map<Constant*, GlobalVariable*> StringCSTCache;
GlobalVariable *&Slot = StringCSTCache[Init];
if (Slot) return Slot;
GlobalVariable *GV = new GlobalVariable(*TheModule, Init->getType(), true,
GlobalVariable::PrivateLinkage,
Init, ".str");
GV->setSection("llvm.metadata");
Slot = GV;
return GV;
}
void AddAnnotateAttrsToGlobal(GlobalValue *GV, tree decl) {
LLVMContext &Context = getGlobalContext();
tree annotateAttr = lookup_attribute("annotate", DECL_ATTRIBUTES (decl));
if (annotateAttr == 0)
return;
Constant *lineNo = ConstantInt::get(Type::getInt32Ty(Context),
DECL_SOURCE_LINE(decl));
Constant *file = ConvertMetadataStringToGV(DECL_SOURCE_FILE(decl));
const Type *SBP= Type::getInt8PtrTy(Context);
file = TheFolder->CreateBitCast(file, SBP);
while (annotateAttr) {
tree args = TREE_VALUE(annotateAttr);
for (tree a = args; a; a = TREE_CHAIN(a)) {
tree val = TREE_VALUE(a);
assert(TREE_CODE(val) == STRING_CST &&
"Annotate attribute arg should always be a string");
Constant *strGV = TreeConstantToLLVM::EmitLV_STRING_CST(val);
Constant *Element[4] = {
TheFolder->CreateBitCast(GV,SBP),
TheFolder->CreateBitCast(strGV,SBP),
file,
lineNo
};
AttributeAnnotateGlobals.push_back(
ConstantStruct::get(Context, Element, 4, false));
}
annotateAttr = TREE_CHAIN(annotateAttr);
if (annotateAttr)
annotateAttr = lookup_attribute("annotate", annotateAttr);
}
}
void reset_initializer_llvm(tree decl) {
if (errorcount || sorrycount)
return;
GlobalVariable *GV = cast<GlobalVariable>(DECL_LLVM(decl));
handleVisibility(decl, GV);
Constant *Init = TreeConstantToLLVM::Convert(DECL_INITIAL(decl));
GV->setInitializer(Init);
}
void reset_type_and_initializer_llvm(tree decl) {
if (errorcount || sorrycount)
return;
GlobalVariable *GV = cast<GlobalVariable>(DECL_LLVM(decl));
handleVisibility(decl, GV);
GV->setInitializer(UndefValue::get(GV->getType()->getElementType()));
Constant *Init = TreeConstantToLLVM::Convert(DECL_INITIAL(decl));
if (GV->getType()->getElementType() != Init->getType()) {
GV->removeFromParent();
GlobalVariable *NGV = new GlobalVariable(*TheModule, Init->getType(),
GV->isConstant(),
GV->getLinkage(), 0,
GV->getName());
NGV->setVisibility(GV->getVisibility());
NGV->setSection(GV->getSection());
NGV->setAlignment(GV->getAlignment());
NGV->setLinkage(GV->getLinkage());
GV->replaceAllUsesWith(TheFolder->CreateBitCast(NGV, GV->getType()));
changeLLVMConstant(GV, NGV);
delete GV;
SET_DECL_LLVM(decl, NGV);
GV = NGV;
}
GV->setInitializer(Init);
}
void emit_global_to_llvm(tree decl) {
if (errorcount || sorrycount) {
TREE_ASM_WRITTEN(decl) = 1;
return; }
if (TREE_CODE(decl) == VAR_DECL && DECL_REGISTER(decl))
return;
if (CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_WITH_VIS)
&& (DECL_DEFER_OUTPUT(decl)))
return;
if (!TYPE_SIZE(TREE_TYPE(decl)))
return;
timevar_push(TV_LLVM_GLOBALS);
GlobalVariable *GV = cast<GlobalVariable>(DECL_LLVM(decl));
Constant *Init;
if (DECL_INITIAL(decl) == 0 || DECL_INITIAL(decl) == error_mark_node) {
Init = Constant::getNullValue(ConvertType(TREE_TYPE(decl)));
} else {
assert((TREE_CONSTANT(DECL_INITIAL(decl)) ||
TREE_CODE(DECL_INITIAL(decl)) == STRING_CST) &&
"Global initializer should be constant!");
GV->setInitializer(UndefValue::get(GV->getType()->getElementType()));
Init = TreeConstantToLLVM::Convert(DECL_INITIAL(decl));
}
if (GV->getType()->getElementType() != Init->getType()) {
GV->removeFromParent();
GlobalVariable *NGV = new GlobalVariable(*TheModule, Init->getType(),
GV->isConstant(),
GlobalValue::ExternalLinkage, 0,
GV->getName());
GV->replaceAllUsesWith(TheFolder->CreateBitCast(NGV, GV->getType()));
changeLLVMConstant(GV, NGV);
delete GV;
SET_DECL_LLVM(decl, NGV);
GV = NGV;
}
GV->setInitializer(Init);
if (TREE_CODE(decl) == VAR_DECL && DECL_THREAD_LOCAL_P(decl))
GV->setThreadLocal(true);
GlobalValue::LinkageTypes Linkage;
if (CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_WITH_VIS)
&& DECL_LLVM_PRIVATE(decl)) {
Linkage = GlobalValue::PrivateLinkage;
} else if (CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_WITH_VIS)
&& DECL_LLVM_LINKER_PRIVATE(decl)) {
Linkage = GlobalValue::LinkerPrivateLinkage;
} else if (CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_WITH_VIS)
&& DECL_LLVM_LINKER_PRIVATE_WEAK(decl)) {
Linkage = GlobalValue::LinkerPrivateWeakLinkage;
} else if (!TREE_PUBLIC(decl)) {
Linkage = GlobalValue::InternalLinkage;
} else if (DECL_WEAK(decl)) {
Linkage = GlobalValue::WeakAnyLinkage;
} else if (DECL_ONE_ONLY(decl)) {
Linkage = GlobalValue::getWeakLinkage(flag_odr);
} else if (DECL_COMMON(decl) && (!DECL_INITIAL(decl) || DECL_INITIAL(decl) == error_mark_node)) {
Linkage = GlobalValue::CommonLinkage;
} else if (DECL_COMDAT(decl)) {
Linkage = GlobalValue::getLinkOnceLinkage(flag_odr);
} else {
Linkage = GV->getLinkage();
}
if (GV->isConstant()) {
if (Linkage == GlobalValue::WeakAnyLinkage)
Linkage = GlobalValue::WeakODRLinkage;
else if (Linkage == GlobalValue::LinkOnceAnyLinkage)
Linkage = GlobalValue::LinkOnceODRLinkage;
}
GV->setLinkage(Linkage);
#ifdef TARGET_ADJUST_LLVM_LINKAGE
TARGET_ADJUST_LLVM_LINKAGE(GV, decl);
#endif
handleVisibility(decl, GV);
if (TREE_CODE(decl) == VAR_DECL) {
if (DECL_SECTION_NAME(decl)) {
GV->setSection(TREE_STRING_POINTER(DECL_SECTION_NAME(decl)));
#ifdef LLVM_IMPLICIT_TARGET_GLOBAL_VAR_SECTION
} else if (const char *Section =
LLVM_IMPLICIT_TARGET_GLOBAL_VAR_SECTION(decl)) {
GV->setSection(Section);
#endif
}
if (DECL_ALIGN(decl)) {
unsigned TargetAlign =
getTargetData().getABITypeAlignment(GV->getType()->getElementType());
if (DECL_USER_ALIGN(decl) ||
8 * TargetAlign < (unsigned)DECL_ALIGN(decl)) {
GV->setAlignment(DECL_ALIGN(decl) / 8);
}
#ifdef TARGET_ADJUST_CSTRING_ALIGN
else if (DECL_INITIAL(decl) != error_mark_node && DECL_INITIAL(decl) &&
TREE_CODE(DECL_INITIAL(decl)) == STRING_CST) {
TARGET_ADJUST_CSTRING_ALIGN(GV);
}
#endif
}
if (DECL_PRESERVE_P (decl)) {
if (DECL_LLVM_LINKER_PRIVATE (decl) ||
DECL_LLVM_LINKER_PRIVATE_WEAK (decl))
AttributeCompilerUsedGlobals.insert(GV);
else
AttributeUsedGlobals.insert(GV);
}
if (DECL_ATTRIBUTES(decl))
AddAnnotateAttrsToGlobal(GV, decl);
#ifdef LLVM_IMPLICIT_TARGET_GLOBAL_VAR_SECTION
} else if (TREE_CODE(decl) == CONST_DECL) {
if (const char *Section =
LLVM_IMPLICIT_TARGET_GLOBAL_VAR_SECTION(decl)) {
GV->setSection(Section);
#ifdef TARGET_ADJUST_CFSTRING_NAME
TARGET_ADJUST_CFSTRING_NAME(GV, Section);
#endif
}
#endif
}
if (TheDebugInfo) {
std::string Name = GV->getNameStr();
const char LPrefix[] = "\01L_OBJC_";
const char lPrefix[] = "\01l_OBJC_";
if (flag_objc_abi == -1 || flag_objc_abi == 0 ||
(strncmp(Name.c_str(), LPrefix, sizeof(LPrefix) - 1) != 0 &&
strncmp(Name.c_str(), lPrefix, sizeof(lPrefix) - 1) != 0))
TheDebugInfo->EmitGlobalVariable(GV, decl);
}
TREE_ASM_WRITTEN(decl) = 1;
timevar_pop(TV_LLVM_GLOBALS);
}
bool ValidateRegisterVariable(tree decl) {
int RegNumber = decode_reg_name(extractRegisterName(decl));
const Type *Ty = ConvertType(TREE_TYPE(decl));
if (errorcount || sorrycount)
return true;
if (RegNumber == -1)
error("%Jregister name not specified for %qD", decl, decl);
else if (RegNumber < 0)
error("%Jinvalid register name for %qD", decl, decl);
else if (TYPE_MODE(TREE_TYPE(decl)) == BLKmode)
error("%Jdata type of %qD isn%'t suitable for a register", decl, decl);
#if 0 // FIXME: enable this.
else if (!HARD_REGNO_MODE_OK(RegNumber, TYPE_MODE(TREE_TYPE(decl))))
error("%Jregister specified for %qD isn%'t suitable for data type",
decl, decl);
#endif
else if (DECL_INITIAL(decl) != 0 && TREE_STATIC(decl))
error("global register variable has initial value");
else if (!Ty->isSingleValueType())
sorry("%JLLVM cannot handle register variable %qD, report a bug",
decl, decl);
else {
if (TREE_THIS_VOLATILE(decl))
warning(0, "volatile register variables don%'t work as you might wish");
return false; }
return true;
}
void make_decl_llvm(tree decl) {
#ifdef ENABLE_CHECKING
if (TREE_CODE(decl) == PARM_DECL || TREE_CODE(decl) == RESULT_DECL
|| (TREE_CODE(decl) == VAR_DECL && !TREE_STATIC(decl) &&
!TREE_PUBLIC(decl) && !DECL_EXTERNAL(decl) && !DECL_REGISTER(decl)))
abort();
else if (TREE_CODE(decl) == TYPE_DECL || TREE_CODE(decl) == LABEL_DECL)
abort ();
#endif
LLVMContext &Context = getGlobalContext();
if (DECL_LLVM_SET_P(decl)) return;
if (errorcount || sorrycount)
return;
if (TREE_CODE(decl) != FUNCTION_DECL && DECL_REGISTER(decl)) {
ValidateRegisterVariable(decl);
return;
}
timevar_push(TV_LLVM_GLOBALS);
const char *Name = "";
if (DECL_NAME(decl))
if (tree AssemblerName = DECL_ASSEMBLER_NAME(decl))
Name = IDENTIFIER_POINTER(AssemblerName);
if (Name[0] == 1) {
#ifdef REGISTER_PREFIX
if (strlen (REGISTER_PREFIX) != 0) {
int reg_number = decode_reg_name(Name);
if (reg_number >= 0 || reg_number == -3)
error("%Jregister name given for non-register variable %qD",
decl, decl);
}
#endif
}
if (TREE_CODE(decl) == VAR_DECL && DECL_SECTION_NAME(decl) != NULL_TREE &&
DECL_INITIAL(decl) == NULL_TREE && DECL_COMMON(decl))
DECL_COMMON(decl) = 0;
if (TREE_CODE(decl) == VAR_DECL && DECL_WEAK(decl))
DECL_COMMON(decl) = 0;
if (TREE_CODE(decl) == FUNCTION_DECL) {
assert(Name[0] && "Function with empty name!");
Function *FnEntry = TheModule->getFunction(Name);
if (FnEntry == 0) {
CallingConv::ID CC;
AttrListPtr PAL;
const FunctionType *Ty =
TheTypeConverter->ConvertFunctionType(TREE_TYPE(decl), decl, NULL,
CC, PAL);
FnEntry = Function::Create(Ty, Function::ExternalLinkage, Name, TheModule);
FnEntry->setCallingConv(CC);
FnEntry->setAttributes(PAL);
if (DECL_EXTERNAL(decl) && DECL_WEAK(decl))
FnEntry->setLinkage(Function::ExternalWeakLinkage);
#ifdef TARGET_ADJUST_LLVM_LINKAGE
TARGET_ADJUST_LLVM_LINKAGE(FnEntry,decl);
#endif
handleVisibility(decl, FnEntry);
if (FnEntry->getName() != Name) {
GlobalVariable *G = TheModule->getGlobalVariable(Name, true);
assert(G && G->isDeclaration() && "A global turned into a function?");
Constant *GInNewType = TheFolder->CreateBitCast(FnEntry, G->getType());
G->replaceAllUsesWith(GInNewType);
changeLLVMConstant(G, GInNewType);
FnEntry->takeName(G);
G->eraseFromParent();
}
}
SET_DECL_LLVM(decl, FnEntry);
} else {
assert((TREE_CODE(decl) == VAR_DECL ||
TREE_CODE(decl) == CONST_DECL) && "Not a function or var decl?");
const Type *Ty = ConvertType(TREE_TYPE(decl));
GlobalVariable *GV ;
if (Ty->isVoidTy())
Ty = StructType::get(Context);
if (Name[0] == 0) { GV = new GlobalVariable(*TheModule, Ty, false,
GlobalValue::ExternalLinkage, 0, "");
if (DECL_EXTERNAL(decl) && DECL_WEAK(decl))
GV->setLinkage(GlobalValue::ExternalWeakLinkage);
#ifdef TARGET_ADJUST_LLVM_LINKAGE
TARGET_ADJUST_LLVM_LINKAGE(GV,decl);
#endif
handleVisibility(decl, GV);
} else {
GlobalVariable *GVE = TheModule->getGlobalVariable(Name, true);
if (GVE == 0) {
GV = new GlobalVariable(*TheModule, Ty, false,
GlobalValue::ExternalLinkage, 0, Name);
if (DECL_EXTERNAL(decl) && DECL_WEAK(decl))
GV->setLinkage(GlobalValue::ExternalWeakLinkage);
#ifdef TARGET_ADJUST_LLVM_LINKAGE
TARGET_ADJUST_LLVM_LINKAGE(GV,decl);
#endif
handleVisibility(decl, GV);
if (GV->getName() != Name) {
Function *F = TheModule->getFunction(Name);
assert(F && F->isDeclaration() && "A function turned into a global?");
Constant *FInNewType = TheFolder->CreateBitCast(GV, F->getType());
F->replaceAllUsesWith(FInNewType);
changeLLVMConstant(F, FInNewType);
GV->takeName(F);
F->eraseFromParent();
}
} else {
GV = GVE; }
}
if ((TREE_READONLY(decl) && !TREE_SIDE_EFFECTS(decl)) ||
TREE_CODE(decl) == CONST_DECL) {
if (DECL_EXTERNAL(decl)) {
GV->setConstant(true);
} else {
if (DECL_INITIAL(decl) != error_mark_node && DECL_INITIAL(decl) &&
(TREE_CONSTANT(DECL_INITIAL(decl)) ||
TREE_CODE(DECL_INITIAL(decl)) == STRING_CST))
GV->setConstant(true);
}
}
if (TREE_CODE(decl) == VAR_DECL && DECL_THREAD_LOCAL_P(decl))
GV->setThreadLocal(true);
SET_DECL_LLVM(decl, GV);
}
timevar_pop(TV_LLVM_GLOBALS);
}
const char *llvm_get_decl_name(void *LLVM) {
if (LLVM)
if (const ValueName *VN = ((Value*)LLVM)->getValueName())
return VN->getKeyData();
return "";
}
void llvm_mark_decl_weak(tree decl) {
assert(DECL_LLVM_SET_P(decl) && DECL_WEAK(decl) &&
isa<GlobalValue>(DECL_LLVM(decl)) && "Decl isn't marked weak!");
GlobalValue *GV = cast<GlobalValue>(DECL_LLVM(decl));
if (GV->hasExternalLinkage()) {
GlobalValue::LinkageTypes Linkage;
if (GV->isDeclaration()) {
Linkage = GlobalValue::ExternalWeakLinkage;
} else {
Linkage = GlobalValue::WeakAnyLinkage;
if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
if (GVar->isConstant())
Linkage = GlobalValue::WeakODRLinkage;
}
GV->setLinkage(Linkage);
}
}
void llvm_emit_ctor_dtor(tree FnDecl, int InitPrio, int isCtor) {
mark_decl_referenced(FnDecl);
if (errorcount || sorrycount) return;
Constant *C = cast<Constant>(DECL_LLVM(FnDecl));
(isCtor ? &StaticCtors:&StaticDtors)->push_back(std::make_pair(C, InitPrio));
}
void llvm_emit_typedef(tree decl) {
return;
}
void llvm_emit_file_scope_asm(const char *string) {
if (TheModule->getModuleInlineAsm().empty())
TheModule->setModuleInlineAsm(string);
else
TheModule->setModuleInlineAsm(TheModule->getModuleInlineAsm() + "\n" +
string);
}
void print_llvm(FILE *file, void *LLVM) {
oFILEstream FS(file);
raw_os_ostream FSS(FS);
FSS << "LLVM: ";
WriteAsOperand(FSS, (Value*)LLVM, true, TheModule);
}
void print_llvm_type(FILE *file, void *LLVM) {
oFILEstream FS(file);
FS << "LLVM: ";
raw_os_ostream RO(FS);
WriteTypeSymbolic(RO, (const Type*)LLVM, TheModule);
}
const char* extractRegisterName(tree decl) {
const char* Name = IDENTIFIER_POINTER(DECL_ASSEMBLER_NAME(decl));
return (*Name == 1) ? Name + 1 : Name;
}