Path: blob/main/contrib/llvm-project/llvm/lib/Target/Target.cpp
35233 views
//===-- Target.cpp --------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the common infrastructure (including C bindings) for9// libLLVMTarget.a, which implements target information.10//11//===----------------------------------------------------------------------===//1213#include "llvm-c/Target.h"14#include "llvm/Analysis/TargetLibraryInfo.h"15#include "llvm/IR/DataLayout.h"16#include "llvm/IR/LLVMContext.h"17#include "llvm/IR/LegacyPassManager.h"18#include "llvm/IR/Module.h"19#include "llvm/IR/Value.h"20#include "llvm/InitializePasses.h"21#include <cstring>2223using namespace llvm;2425// Avoid including "llvm-c/Core.h" for compile time, fwd-declare this instead.26extern "C" LLVMContextRef LLVMGetGlobalContext(void);2728inline TargetLibraryInfoImpl *unwrap(LLVMTargetLibraryInfoRef P) {29return reinterpret_cast<TargetLibraryInfoImpl*>(P);30}3132inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfoImpl *P) {33TargetLibraryInfoImpl *X = const_cast<TargetLibraryInfoImpl*>(P);34return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);35}3637void llvm::initializeTarget(PassRegistry &Registry) {38initializeTargetLibraryInfoWrapperPassPass(Registry);39initializeTargetTransformInfoWrapperPassPass(Registry);40}4142LLVMTargetDataRef LLVMGetModuleDataLayout(LLVMModuleRef M) {43return wrap(&unwrap(M)->getDataLayout());44}4546void LLVMSetModuleDataLayout(LLVMModuleRef M, LLVMTargetDataRef DL) {47unwrap(M)->setDataLayout(*unwrap(DL));48}4950LLVMTargetDataRef LLVMCreateTargetData(const char *StringRep) {51return wrap(new DataLayout(StringRep));52}5354void LLVMDisposeTargetData(LLVMTargetDataRef TD) {55delete unwrap(TD);56}5758void LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI,59LLVMPassManagerRef PM) {60unwrap(PM)->add(new TargetLibraryInfoWrapperPass(*unwrap(TLI)));61}6263char *LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD) {64std::string StringRep = unwrap(TD)->getStringRepresentation();65return strdup(StringRep.c_str());66}6768LLVMByteOrdering LLVMByteOrder(LLVMTargetDataRef TD) {69return unwrap(TD)->isLittleEndian() ? LLVMLittleEndian : LLVMBigEndian;70}7172unsigned LLVMPointerSize(LLVMTargetDataRef TD) {73return unwrap(TD)->getPointerSize(0);74}7576unsigned LLVMPointerSizeForAS(LLVMTargetDataRef TD, unsigned AS) {77return unwrap(TD)->getPointerSize(AS);78}7980LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD) {81return wrap(unwrap(TD)->getIntPtrType(*unwrap(LLVMGetGlobalContext())));82}8384LLVMTypeRef LLVMIntPtrTypeForAS(LLVMTargetDataRef TD, unsigned AS) {85return wrap(unwrap(TD)->getIntPtrType(*unwrap(LLVMGetGlobalContext()), AS));86}8788LLVMTypeRef LLVMIntPtrTypeInContext(LLVMContextRef C, LLVMTargetDataRef TD) {89return wrap(unwrap(TD)->getIntPtrType(*unwrap(C)));90}9192LLVMTypeRef LLVMIntPtrTypeForASInContext(LLVMContextRef C, LLVMTargetDataRef TD, unsigned AS) {93return wrap(unwrap(TD)->getIntPtrType(*unwrap(C), AS));94}9596unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef TD, LLVMTypeRef Ty) {97return unwrap(TD)->getTypeSizeInBits(unwrap(Ty));98}99100unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {101return unwrap(TD)->getTypeStoreSize(unwrap(Ty));102}103104unsigned long long LLVMABISizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {105return unwrap(TD)->getTypeAllocSize(unwrap(Ty));106}107108unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {109return unwrap(TD)->getABITypeAlign(unwrap(Ty)).value();110}111112unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {113return unwrap(TD)->getABITypeAlign(unwrap(Ty)).value();114}115116unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {117return unwrap(TD)->getPrefTypeAlign(unwrap(Ty)).value();118}119120unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,121LLVMValueRef GlobalVar) {122return unwrap(TD)123->getPreferredAlign(unwrap<GlobalVariable>(GlobalVar))124.value();125}126127unsigned LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy,128unsigned long long Offset) {129StructType *STy = unwrap<StructType>(StructTy);130return unwrap(TD)->getStructLayout(STy)->getElementContainingOffset(Offset);131}132133unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef TD, LLVMTypeRef StructTy,134unsigned Element) {135StructType *STy = unwrap<StructType>(StructTy);136return unwrap(TD)->getStructLayout(STy)->getElementOffset(Element);137}138139140