2007-09-18 11:18:57 +08:00
|
|
|
//===-- Core.cpp ----------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-09-18 11:18:57 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-10-08 03:51:21 +08:00
|
|
|
// This file implements the common infrastructure (including the C bindings)
|
|
|
|
// for libLLVMCore.a, which implements the LLVM intermediate representation.
|
2007-09-18 11:18:57 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm-c/Core.h"
|
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Attributes.h"
|
2014-03-04 19:01:28 +08:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
2014-06-13 01:38:55 +08:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2014-04-17 01:45:04 +08:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/GlobalAlias.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/InlineAsm.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2015-02-13 18:01:29 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2013-05-02 04:59:00 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2010-01-28 04:34:15 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-12 04:10:48 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2014-04-30 07:26:49 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2013-02-18 00:35:51 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2010-01-28 04:34:15 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/Support/Threading.h"
|
2010-01-28 04:34:15 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-09-18 11:18:57 +08:00
|
|
|
#include <cassert>
|
2007-12-20 06:30:40 +08:00
|
|
|
#include <cstdlib>
|
2008-02-20 19:08:44 +08:00
|
|
|
#include <cstring>
|
2014-06-13 01:38:55 +08:00
|
|
|
#include <system_error>
|
2007-09-18 11:18:57 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 06:55:11 +08:00
|
|
|
#define DEBUG_TYPE "ir"
|
|
|
|
|
2010-10-08 03:51:21 +08:00
|
|
|
void llvm::initializeCore(PassRegistry &Registry) {
|
2014-01-13 21:07:17 +08:00
|
|
|
initializeDominatorTreeWrapperPassPass(Registry);
|
2014-01-12 20:15:39 +08:00
|
|
|
initializePrintModulePassWrapperPass(Registry);
|
|
|
|
initializePrintFunctionPassWrapperPass(Registry);
|
2013-02-09 07:37:41 +08:00
|
|
|
initializePrintBasicBlockPassPass(Registry);
|
2014-01-20 19:34:08 +08:00
|
|
|
initializeVerifierLegacyPassPass(Registry);
|
2010-10-08 03:51:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMInitializeCore(LLVMPassRegistryRef R) {
|
|
|
|
initializeCore(*unwrap(R));
|
|
|
|
}
|
2007-09-18 11:18:57 +08:00
|
|
|
|
2013-02-18 00:35:51 +08:00
|
|
|
void LLVMShutdown() {
|
|
|
|
llvm_shutdown();
|
|
|
|
}
|
|
|
|
|
2007-12-20 06:30:40 +08:00
|
|
|
/*===-- Error handling ----------------------------------------------------===*/
|
|
|
|
|
2013-05-22 10:46:43 +08:00
|
|
|
char *LLVMCreateMessage(const char *Message) {
|
|
|
|
return strdup(Message);
|
|
|
|
}
|
|
|
|
|
2007-12-20 06:30:40 +08:00
|
|
|
void LLVMDisposeMessage(char *Message) {
|
|
|
|
free(Message);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-02 00:58:40 +08:00
|
|
|
/*===-- Operations on contexts --------------------------------------------===*/
|
|
|
|
|
|
|
|
LLVMContextRef LLVMContextCreate() {
|
|
|
|
return wrap(new LLVMContext());
|
|
|
|
}
|
|
|
|
|
2009-07-02 08:16:38 +08:00
|
|
|
LLVMContextRef LLVMGetGlobalContext() {
|
|
|
|
return wrap(&getGlobalContext());
|
|
|
|
}
|
|
|
|
|
2014-04-17 01:45:04 +08:00
|
|
|
void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
|
|
|
|
LLVMDiagnosticHandler Handler,
|
|
|
|
void *DiagnosticContext) {
|
|
|
|
unwrap(C)->setDiagnosticHandler(
|
|
|
|
LLVM_EXTENSION reinterpret_cast<LLVMContext::DiagnosticHandlerTy>(Handler),
|
|
|
|
DiagnosticContext);
|
|
|
|
}
|
|
|
|
|
2014-05-16 10:33:15 +08:00
|
|
|
void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
|
|
|
|
void *OpaqueHandle) {
|
|
|
|
auto YieldCallback =
|
|
|
|
LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
|
|
|
|
unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
|
|
|
|
}
|
|
|
|
|
2009-07-02 00:58:40 +08:00
|
|
|
void LLVMContextDispose(LLVMContextRef C) {
|
|
|
|
delete unwrap(C);
|
|
|
|
}
|
|
|
|
|
2010-02-28 17:45:59 +08:00
|
|
|
unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
|
|
|
|
unsigned SLen) {
|
|
|
|
return unwrap(C)->getMDKindID(StringRef(Name, SLen));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
|
|
|
|
return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
|
|
|
|
}
|
|
|
|
|
2014-04-17 01:45:04 +08:00
|
|
|
char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
|
2014-06-27 06:52:05 +08:00
|
|
|
std::string MsgStorage;
|
|
|
|
raw_string_ostream Stream(MsgStorage);
|
|
|
|
DiagnosticPrinterRawOStream DP(Stream);
|
|
|
|
|
2014-04-17 01:45:04 +08:00
|
|
|
unwrap(DI)->print(DP);
|
2014-06-27 06:52:05 +08:00
|
|
|
Stream.flush();
|
|
|
|
|
|
|
|
return LLVMCreateMessage(MsgStorage.c_str());
|
2014-04-17 01:45:04 +08:00
|
|
|
}
|
|
|
|
|
2016-03-08 05:39:20 +08:00
|
|
|
LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) {
|
2014-04-17 01:45:04 +08:00
|
|
|
LLVMDiagnosticSeverity severity;
|
|
|
|
|
|
|
|
switch(unwrap(DI)->getSeverity()) {
|
|
|
|
default:
|
|
|
|
severity = LLVMDSError;
|
|
|
|
break;
|
|
|
|
case DS_Warning:
|
|
|
|
severity = LLVMDSWarning;
|
|
|
|
break;
|
|
|
|
case DS_Remark:
|
|
|
|
severity = LLVMDSRemark;
|
|
|
|
break;
|
|
|
|
case DS_Note:
|
|
|
|
severity = LLVMDSNote;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return severity;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-18 11:18:57 +08:00
|
|
|
/*===-- Operations on modules ---------------------------------------------===*/
|
|
|
|
|
2009-07-02 15:17:57 +08:00
|
|
|
LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
|
|
|
|
return wrap(new Module(ModuleID, getGlobalContext()));
|
|
|
|
}
|
|
|
|
|
2013-09-19 07:31:10 +08:00
|
|
|
LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
|
2009-07-02 15:17:57 +08:00
|
|
|
LLVMContextRef C) {
|
2009-07-02 05:22:36 +08:00
|
|
|
return wrap(new Module(ModuleID, *unwrap(C)));
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeModule(LLVMModuleRef M) {
|
|
|
|
delete unwrap(M);
|
|
|
|
}
|
|
|
|
|
2007-12-28 04:13:47 +08:00
|
|
|
/*--.. Data layout .........................................................--*/
|
2016-02-16 08:23:52 +08:00
|
|
|
const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
|
2014-02-26 04:01:08 +08:00
|
|
|
return unwrap(M)->getDataLayoutStr().c_str();
|
2007-12-28 04:13:47 +08:00
|
|
|
}
|
|
|
|
|
2016-02-16 08:23:52 +08:00
|
|
|
const char *LLVMGetDataLayout(LLVMModuleRef M) {
|
|
|
|
return LLVMGetDataLayoutStr(M);
|
|
|
|
}
|
|
|
|
|
2016-02-16 07:40:06 +08:00
|
|
|
void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
|
|
|
|
unwrap(M)->setDataLayout(DataLayoutStr);
|
2007-12-28 04:13:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*--.. Target triple .......................................................--*/
|
|
|
|
const char * LLVMGetTarget(LLVMModuleRef M) {
|
|
|
|
return unwrap(M)->getTargetTriple().c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
|
|
|
|
unwrap(M)->setTargetTriple(Triple);
|
|
|
|
}
|
|
|
|
|
2008-03-15 07:58:56 +08:00
|
|
|
void LLVMDumpModule(LLVMModuleRef M) {
|
|
|
|
unwrap(M)->dump();
|
|
|
|
}
|
|
|
|
|
2012-05-10 00:54:17 +08:00
|
|
|
LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
|
|
|
|
char **ErrorMessage) {
|
2014-08-26 02:16:47 +08:00
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream dest(Filename, EC, sys::fs::F_Text);
|
|
|
|
if (EC) {
|
|
|
|
*ErrorMessage = strdup(EC.message().c_str());
|
2012-05-10 00:54:17 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-09 14:08:46 +08:00
|
|
|
unwrap(M)->print(dest, nullptr);
|
2012-05-10 00:54:17 +08:00
|
|
|
|
2014-08-26 02:16:47 +08:00
|
|
|
dest.close();
|
|
|
|
|
|
|
|
if (dest.has_error()) {
|
|
|
|
*ErrorMessage = strdup("Error printing to file");
|
2012-05-10 00:54:17 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-26 02:16:47 +08:00
|
|
|
|
2012-05-10 00:54:17 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-17 02:00:54 +08:00
|
|
|
char *LLVMPrintModuleToString(LLVMModuleRef M) {
|
2014-06-27 06:52:05 +08:00
|
|
|
std::string buf;
|
|
|
|
raw_string_ostream os(buf);
|
|
|
|
|
2014-04-09 14:08:46 +08:00
|
|
|
unwrap(M)->print(os, nullptr);
|
2014-06-27 06:52:05 +08:00
|
|
|
os.flush();
|
|
|
|
|
|
|
|
return strdup(buf.c_str());
|
2013-10-17 02:00:54 +08:00
|
|
|
}
|
|
|
|
|
2010-04-11 01:52:58 +08:00
|
|
|
/*--.. Operations on inline assembler ......................................--*/
|
|
|
|
void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
|
|
|
|
unwrap(M)->setModuleInlineAsm(StringRef(Asm));
|
|
|
|
}
|
|
|
|
|
2007-09-18 11:18:57 +08:00
|
|
|
|
2010-11-29 04:03:44 +08:00
|
|
|
/*--.. Operations on module contexts ......................................--*/
|
|
|
|
LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
|
|
|
|
return wrap(&unwrap(M)->getContext());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-18 11:18:57 +08:00
|
|
|
/*===-- Operations on types -----------------------------------------------===*/
|
|
|
|
|
|
|
|
/*--.. Operations on all types (mostly) ....................................--*/
|
|
|
|
|
|
|
|
LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
|
2009-07-16 06:00:31 +08:00
|
|
|
switch (unwrap(Ty)->getTypeID()) {
|
|
|
|
case Type::VoidTyID:
|
|
|
|
return LLVMVoidTypeKind;
|
2011-12-17 08:04:22 +08:00
|
|
|
case Type::HalfTyID:
|
|
|
|
return LLVMHalfTypeKind;
|
2009-07-16 06:00:31 +08:00
|
|
|
case Type::FloatTyID:
|
|
|
|
return LLVMFloatTypeKind;
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
return LLVMDoubleTypeKind;
|
|
|
|
case Type::X86_FP80TyID:
|
|
|
|
return LLVMX86_FP80TypeKind;
|
|
|
|
case Type::FP128TyID:
|
|
|
|
return LLVMFP128TypeKind;
|
|
|
|
case Type::PPC_FP128TyID:
|
|
|
|
return LLVMPPC_FP128TypeKind;
|
|
|
|
case Type::LabelTyID:
|
|
|
|
return LLVMLabelTypeKind;
|
|
|
|
case Type::MetadataTyID:
|
|
|
|
return LLVMMetadataTypeKind;
|
|
|
|
case Type::IntegerTyID:
|
|
|
|
return LLVMIntegerTypeKind;
|
|
|
|
case Type::FunctionTyID:
|
|
|
|
return LLVMFunctionTypeKind;
|
|
|
|
case Type::StructTyID:
|
|
|
|
return LLVMStructTypeKind;
|
|
|
|
case Type::ArrayTyID:
|
|
|
|
return LLVMArrayTypeKind;
|
|
|
|
case Type::PointerTyID:
|
|
|
|
return LLVMPointerTypeKind;
|
|
|
|
case Type::VectorTyID:
|
|
|
|
return LLVMVectorTypeKind;
|
2010-09-11 04:55:01 +08:00
|
|
|
case Type::X86_MMXTyID:
|
|
|
|
return LLVMX86_MMXTypeKind;
|
2015-08-14 13:09:07 +08:00
|
|
|
case Type::TokenTyID:
|
|
|
|
return LLVMTokenTypeKind;
|
2009-07-16 06:00:31 +08:00
|
|
|
}
|
2013-12-07 10:27:52 +08:00
|
|
|
llvm_unreachable("Unhandled TypeID.");
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
2011-10-06 20:13:28 +08:00
|
|
|
LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
|
|
|
|
{
|
|
|
|
return unwrap(Ty)->isSized();
|
|
|
|
}
|
|
|
|
|
2009-08-14 08:01:31 +08:00
|
|
|
LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
|
|
|
|
return wrap(&unwrap(Ty)->getContext());
|
|
|
|
}
|
|
|
|
|
2013-10-17 05:30:25 +08:00
|
|
|
void LLVMDumpType(LLVMTypeRef Ty) {
|
|
|
|
return unwrap(Ty)->dump();
|
|
|
|
}
|
|
|
|
|
2013-10-22 14:58:34 +08:00
|
|
|
char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
|
2014-06-27 06:52:05 +08:00
|
|
|
std::string buf;
|
|
|
|
raw_string_ostream os(buf);
|
2013-10-22 14:58:34 +08:00
|
|
|
|
2014-06-21 10:43:02 +08:00
|
|
|
if (unwrap(Ty))
|
|
|
|
unwrap(Ty)->print(os);
|
|
|
|
else
|
|
|
|
os << "Printing <null> Type";
|
|
|
|
|
2014-06-27 06:52:05 +08:00
|
|
|
os.flush();
|
|
|
|
|
|
|
|
return strdup(buf.c_str());
|
2013-10-22 14:58:34 +08:00
|
|
|
}
|
|
|
|
|
2007-09-18 11:18:57 +08:00
|
|
|
/*--.. Operations on integer types .........................................--*/
|
|
|
|
|
2009-08-14 08:01:31 +08:00
|
|
|
LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
|
|
|
|
return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
|
|
|
|
}
|
|
|
|
LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
|
|
|
|
return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
|
|
|
|
}
|
|
|
|
LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
|
|
|
|
return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
|
|
|
|
}
|
|
|
|
LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
|
|
|
|
return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
|
|
|
|
}
|
|
|
|
LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
|
|
|
|
return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
|
|
|
|
}
|
2015-04-17 23:32:15 +08:00
|
|
|
LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
|
|
|
|
return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
|
|
|
|
}
|
2009-08-14 08:01:31 +08:00
|
|
|
LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
|
|
|
|
return wrap(IntegerType::get(*unwrap(C), NumBits));
|
|
|
|
}
|
|
|
|
|
2009-08-14 05:58:54 +08:00
|
|
|
LLVMTypeRef LLVMInt1Type(void) {
|
2009-08-14 08:01:31 +08:00
|
|
|
return LLVMInt1TypeInContext(LLVMGetGlobalContext());
|
2009-08-14 05:58:54 +08:00
|
|
|
}
|
|
|
|
LLVMTypeRef LLVMInt8Type(void) {
|
2009-08-14 08:01:31 +08:00
|
|
|
return LLVMInt8TypeInContext(LLVMGetGlobalContext());
|
2009-08-14 05:58:54 +08:00
|
|
|
}
|
|
|
|
LLVMTypeRef LLVMInt16Type(void) {
|
2009-08-14 08:01:31 +08:00
|
|
|
return LLVMInt16TypeInContext(LLVMGetGlobalContext());
|
2009-08-14 05:58:54 +08:00
|
|
|
}
|
|
|
|
LLVMTypeRef LLVMInt32Type(void) {
|
2009-08-14 08:01:31 +08:00
|
|
|
return LLVMInt32TypeInContext(LLVMGetGlobalContext());
|
2009-08-14 05:58:54 +08:00
|
|
|
}
|
|
|
|
LLVMTypeRef LLVMInt64Type(void) {
|
2009-08-14 08:01:31 +08:00
|
|
|
return LLVMInt64TypeInContext(LLVMGetGlobalContext());
|
2009-08-14 05:58:54 +08:00
|
|
|
}
|
2015-04-17 23:32:15 +08:00
|
|
|
LLVMTypeRef LLVMInt128Type(void) {
|
|
|
|
return LLVMInt128TypeInContext(LLVMGetGlobalContext());
|
|
|
|
}
|
2007-10-07 00:05:20 +08:00
|
|
|
LLVMTypeRef LLVMIntType(unsigned NumBits) {
|
2009-08-14 08:01:31 +08:00
|
|
|
return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
|
2007-09-18 11:18:57 +08:00
|
|
|
return unwrap<IntegerType>(IntegerTy)->getBitWidth();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--.. Operations on real types ............................................--*/
|
|
|
|
|
2011-12-17 08:04:22 +08:00
|
|
|
LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
|
|
|
|
return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
|
|
|
|
}
|
2009-08-14 08:01:31 +08:00
|
|
|
LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
|
|
|
|
return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
|
|
|
|
}
|
|
|
|
LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
|
|
|
|
return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
|
|
|
|
}
|
|
|
|
LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
|
|
|
|
return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
|
|
|
|
}
|
|
|
|
LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
|
|
|
|
return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
|
|
|
|
}
|
|
|
|
LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
|
|
|
|
return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
|
|
|
|
}
|
2010-09-11 04:55:01 +08:00
|
|
|
LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
|
|
|
|
return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
|
|
|
|
}
|
2015-08-14 13:09:07 +08:00
|
|
|
LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
|
|
|
|
return (LLVMTypeRef) Type::getTokenTy(*unwrap(C));
|
|
|
|
}
|
2009-08-14 08:01:31 +08:00
|
|
|
|
2011-12-17 08:04:22 +08:00
|
|
|
LLVMTypeRef LLVMHalfType(void) {
|
|
|
|
return LLVMHalfTypeInContext(LLVMGetGlobalContext());
|
|
|
|
}
|
2009-08-14 05:58:54 +08:00
|
|
|
LLVMTypeRef LLVMFloatType(void) {
|
2009-08-14 08:01:31 +08:00
|
|
|
return LLVMFloatTypeInContext(LLVMGetGlobalContext());
|
2009-08-14 05:58:54 +08:00
|
|
|
}
|
|
|
|
LLVMTypeRef LLVMDoubleType(void) {
|
2009-08-14 08:01:31 +08:00
|
|
|
return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
|
2009-08-14 05:58:54 +08:00
|
|
|
}
|
|
|
|
LLVMTypeRef LLVMX86FP80Type(void) {
|
2009-08-14 08:01:31 +08:00
|
|
|
return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
|
2009-08-14 05:58:54 +08:00
|
|
|
}
|
|
|
|
LLVMTypeRef LLVMFP128Type(void) {
|
2009-08-14 08:01:31 +08:00
|
|
|
return LLVMFP128TypeInContext(LLVMGetGlobalContext());
|
2009-08-14 05:58:54 +08:00
|
|
|
}
|
|
|
|
LLVMTypeRef LLVMPPCFP128Type(void) {
|
2009-08-14 08:01:31 +08:00
|
|
|
return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
|
2009-08-14 05:58:54 +08:00
|
|
|
}
|
2010-09-11 04:55:01 +08:00
|
|
|
LLVMTypeRef LLVMX86MMXType(void) {
|
|
|
|
return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
|
|
|
|
}
|
2007-09-18 11:18:57 +08:00
|
|
|
|
|
|
|
/*--.. Operations on function types ........................................--*/
|
|
|
|
|
2007-10-07 00:05:20 +08:00
|
|
|
LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
|
|
|
|
LLVMTypeRef *ParamTypes, unsigned ParamCount,
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool IsVarArg) {
|
2011-07-14 19:44:09 +08:00
|
|
|
ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
|
2009-07-30 06:17:13 +08:00
|
|
|
return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
|
2007-09-18 11:18:57 +08:00
|
|
|
return unwrap<FunctionType>(FunctionTy)->isVarArg();
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
|
2007-09-18 11:18:57 +08:00
|
|
|
return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
|
2007-09-18 11:18:57 +08:00
|
|
|
return unwrap<FunctionType>(FunctionTy)->getNumParams();
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
|
2007-09-18 11:18:57 +08:00
|
|
|
FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
|
|
|
|
for (FunctionType::param_iterator I = Ty->param_begin(),
|
|
|
|
E = Ty->param_end(); I != E; ++I)
|
|
|
|
*Dest++ = wrap(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--.. Operations on struct types ..........................................--*/
|
|
|
|
|
2009-08-14 08:01:31 +08:00
|
|
|
LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
|
2010-01-10 06:27:07 +08:00
|
|
|
unsigned ElementCount, LLVMBool Packed) {
|
2011-07-14 19:44:09 +08:00
|
|
|
ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
|
2009-08-14 08:01:31 +08:00
|
|
|
return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
2009-08-14 08:01:31 +08:00
|
|
|
LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
|
2010-01-10 06:27:07 +08:00
|
|
|
unsigned ElementCount, LLVMBool Packed) {
|
2009-08-14 08:01:31 +08:00
|
|
|
return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
|
|
|
|
ElementCount, Packed);
|
|
|
|
}
|
|
|
|
|
2011-07-14 13:53:17 +08:00
|
|
|
LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
|
|
|
|
{
|
2011-08-13 02:07:07 +08:00
|
|
|
return wrap(StructType::create(*unwrap(C), Name));
|
2011-07-14 13:53:17 +08:00
|
|
|
}
|
|
|
|
|
2011-10-06 20:12:50 +08:00
|
|
|
const char *LLVMGetStructName(LLVMTypeRef Ty)
|
|
|
|
{
|
2011-10-15 04:37:42 +08:00
|
|
|
StructType *Type = unwrap<StructType>(Ty);
|
|
|
|
if (!Type->hasName())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2011-10-15 04:37:42 +08:00
|
|
|
return Type->getName().data();
|
2011-10-06 20:12:50 +08:00
|
|
|
}
|
|
|
|
|
2011-07-14 13:53:17 +08:00
|
|
|
void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
|
|
|
|
unsigned ElementCount, LLVMBool Packed) {
|
2011-07-14 19:44:09 +08:00
|
|
|
ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
|
2011-07-14 13:53:17 +08:00
|
|
|
unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
|
|
|
|
}
|
2009-08-14 08:01:31 +08:00
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
|
2007-09-18 11:18:57 +08:00
|
|
|
return unwrap<StructType>(StructTy)->getNumElements();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
|
|
|
|
StructType *Ty = unwrap<StructType>(StructTy);
|
2011-04-21 06:52:37 +08:00
|
|
|
for (StructType::element_iterator I = Ty->element_begin(),
|
2007-09-18 11:18:57 +08:00
|
|
|
E = Ty->element_end(); I != E; ++I)
|
|
|
|
*Dest++ = wrap(*I);
|
|
|
|
}
|
|
|
|
|
2015-06-04 17:09:53 +08:00
|
|
|
LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
|
|
|
|
StructType *Ty = unwrap<StructType>(StructTy);
|
|
|
|
return wrap(Ty->getTypeAtIndex(i));
|
|
|
|
}
|
|
|
|
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
|
2007-09-18 11:18:57 +08:00
|
|
|
return unwrap<StructType>(StructTy)->isPacked();
|
|
|
|
}
|
|
|
|
|
2011-07-15 00:20:28 +08:00
|
|
|
LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
|
|
|
|
return unwrap<StructType>(StructTy)->isOpaque();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
|
|
|
|
return wrap(unwrap(M)->getTypeByName(Name));
|
|
|
|
}
|
|
|
|
|
2007-09-18 11:18:57 +08:00
|
|
|
/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
|
|
|
|
|
2007-12-18 00:08:32 +08:00
|
|
|
LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
|
2009-07-30 06:17:13 +08:00
|
|
|
return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
2007-12-18 00:08:32 +08:00
|
|
|
LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
|
2009-07-30 06:17:13 +08:00
|
|
|
return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
2007-12-18 00:08:32 +08:00
|
|
|
LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
|
2009-07-30 06:17:13 +08:00
|
|
|
return wrap(VectorType::get(unwrap(ElementType), ElementCount));
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
|
|
|
|
return wrap(unwrap<SequentialType>(Ty)->getElementType());
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
|
|
|
|
return unwrap<ArrayType>(ArrayTy)->getNumElements();
|
|
|
|
}
|
|
|
|
|
2007-12-18 00:08:32 +08:00
|
|
|
unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
|
|
|
|
return unwrap<PointerType>(PointerTy)->getAddressSpace();
|
|
|
|
}
|
|
|
|
|
2007-09-18 11:18:57 +08:00
|
|
|
unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
|
|
|
|
return unwrap<VectorType>(VectorTy)->getNumElements();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--.. Operations on other types ...........................................--*/
|
|
|
|
|
2009-08-14 08:01:31 +08:00
|
|
|
LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
|
|
|
|
return wrap(Type::getVoidTy(*unwrap(C)));
|
|
|
|
}
|
|
|
|
LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
|
|
|
|
return wrap(Type::getLabelTy(*unwrap(C)));
|
|
|
|
}
|
|
|
|
|
2009-08-14 05:58:54 +08:00
|
|
|
LLVMTypeRef LLVMVoidType(void) {
|
2009-08-14 08:01:31 +08:00
|
|
|
return LLVMVoidTypeInContext(LLVMGetGlobalContext());
|
2009-08-14 05:58:54 +08:00
|
|
|
}
|
|
|
|
LLVMTypeRef LLVMLabelType(void) {
|
2009-08-14 08:01:31 +08:00
|
|
|
return LLVMLabelTypeInContext(LLVMGetGlobalContext());
|
2009-08-14 05:58:54 +08:00
|
|
|
}
|
2007-09-18 11:18:57 +08:00
|
|
|
|
|
|
|
/*===-- Operations on values ----------------------------------------------===*/
|
|
|
|
|
|
|
|
/*--.. Operations on all values ............................................--*/
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
|
2007-09-18 11:18:57 +08:00
|
|
|
return wrap(unwrap(Val)->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *LLVMGetValueName(LLVMValueRef Val) {
|
2009-07-26 15:49:05 +08:00
|
|
|
return unwrap(Val)->getName().data();
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
|
|
|
|
unwrap(Val)->setName(Name);
|
|
|
|
}
|
|
|
|
|
2007-10-06 08:08:49 +08:00
|
|
|
void LLVMDumpValue(LLVMValueRef Val) {
|
|
|
|
unwrap(Val)->dump();
|
|
|
|
}
|
|
|
|
|
2013-11-06 17:21:01 +08:00
|
|
|
char* LLVMPrintValueToString(LLVMValueRef Val) {
|
2014-06-27 06:52:05 +08:00
|
|
|
std::string buf;
|
|
|
|
raw_string_ostream os(buf);
|
2013-11-06 17:21:01 +08:00
|
|
|
|
2014-06-21 10:43:02 +08:00
|
|
|
if (unwrap(Val))
|
|
|
|
unwrap(Val)->print(os);
|
|
|
|
else
|
|
|
|
os << "Printing <null> Value";
|
|
|
|
|
2014-06-27 06:52:05 +08:00
|
|
|
os.flush();
|
|
|
|
|
|
|
|
return strdup(buf.c_str());
|
2013-11-06 17:21:01 +08:00
|
|
|
}
|
|
|
|
|
2009-10-12 12:01:02 +08:00
|
|
|
void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
|
|
|
|
unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
|
|
|
|
}
|
2008-12-20 02:39:45 +08:00
|
|
|
|
2010-02-28 17:45:59 +08:00
|
|
|
int LLVMHasMetadata(LLVMValueRef Inst) {
|
|
|
|
return unwrap<Instruction>(Inst)->hasMetadata();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
auto *I = unwrap<Instruction>(Inst);
|
|
|
|
assert(I && "Expected instruction");
|
|
|
|
if (auto *MD = I->getMetadata(KindID))
|
|
|
|
return wrap(MetadataAsValue::get(I->getContext(), MD));
|
|
|
|
return nullptr;
|
2010-02-28 17:45:59 +08:00
|
|
|
}
|
|
|
|
|
2015-01-29 00:35:59 +08:00
|
|
|
// MetadataAsValue uses a canonical format which strips the actual MDNode for
|
|
|
|
// MDNode with just a single constant value, storing just a ConstantAsMetadata
|
|
|
|
// This undoes this canonicalization, reconstructing the MDNode.
|
|
|
|
static MDNode *extractMDNode(MetadataAsValue *MAV) {
|
|
|
|
Metadata *MD = MAV->getMetadata();
|
|
|
|
assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
|
|
|
|
"Expected a metadata node or a canonicalized constant");
|
|
|
|
|
|
|
|
if (MDNode *N = dyn_cast<MDNode>(MD))
|
|
|
|
return N;
|
|
|
|
|
|
|
|
return MDNode::get(MAV->getContext(), MD);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
|
|
|
|
MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
|
|
|
|
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
unwrap<Instruction>(Inst)->setMetadata(KindID, N);
|
2010-02-28 17:45:59 +08:00
|
|
|
}
|
|
|
|
|
2008-12-20 02:39:45 +08:00
|
|
|
/*--.. Conversion functions ................................................--*/
|
|
|
|
|
|
|
|
#define LLVM_DEFINE_VALUE_CAST(name) \
|
|
|
|
LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
|
|
|
|
return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
|
|
|
|
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
|
|
|
|
if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
|
|
|
|
if (isa<MDNode>(MD->getMetadata()) ||
|
|
|
|
isa<ValueAsMetadata>(MD->getMetadata()))
|
|
|
|
return Val;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
|
|
|
|
if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
|
|
|
|
if (isa<MDString>(MD->getMetadata()))
|
|
|
|
return Val;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2009-10-12 12:01:02 +08:00
|
|
|
/*--.. Operations on Uses ..................................................--*/
|
2010-03-03 04:32:28 +08:00
|
|
|
LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
|
2009-10-12 12:01:02 +08:00
|
|
|
Value *V = unwrap(Val);
|
|
|
|
Value::use_iterator I = V->use_begin();
|
|
|
|
if (I == V->use_end())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2014-03-09 11:16:01 +08:00
|
|
|
return wrap(&*I);
|
2009-10-12 12:01:02 +08:00
|
|
|
}
|
|
|
|
|
2010-03-03 04:32:28 +08:00
|
|
|
LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
|
|
|
|
Use *Next = unwrap(U)->getNext();
|
|
|
|
if (Next)
|
|
|
|
return wrap(Next);
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2009-10-12 12:01:02 +08:00
|
|
|
}
|
|
|
|
|
2010-03-03 04:32:28 +08:00
|
|
|
LLVMValueRef LLVMGetUser(LLVMUseRef U) {
|
|
|
|
return wrap(unwrap(U)->getUser());
|
2009-10-12 12:01:02 +08:00
|
|
|
}
|
|
|
|
|
2010-03-03 04:32:28 +08:00
|
|
|
LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
|
|
|
|
return wrap(unwrap(U)->get());
|
2009-10-12 12:01:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*--.. Operations on Users .................................................--*/
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
|
|
|
|
static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
|
|
|
|
unsigned Index) {
|
|
|
|
Metadata *Op = N->getOperand(Index);
|
|
|
|
if (!Op)
|
|
|
|
return nullptr;
|
|
|
|
if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
|
|
|
|
return wrap(C->getValue());
|
|
|
|
return wrap(MetadataAsValue::get(Context, Op));
|
|
|
|
}
|
|
|
|
|
2009-10-12 12:01:02 +08:00
|
|
|
LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
|
2011-10-06 20:13:11 +08:00
|
|
|
Value *V = unwrap(Val);
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
|
|
|
|
if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
|
|
|
|
assert(Index == 0 && "Function-local metadata can only have one operand");
|
|
|
|
return wrap(L->getValue());
|
|
|
|
}
|
|
|
|
return getMDNodeOperandImpl(V->getContext(),
|
|
|
|
cast<MDNode>(MD->getMetadata()), Index);
|
|
|
|
}
|
|
|
|
|
2011-10-06 20:13:11 +08:00
|
|
|
return wrap(cast<User>(V)->getOperand(Index));
|
2009-10-12 12:01:02 +08:00
|
|
|
}
|
2008-12-20 02:39:45 +08:00
|
|
|
|
2014-08-12 10:55:40 +08:00
|
|
|
LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
|
|
|
|
Value *V = unwrap(Val);
|
|
|
|
return wrap(&cast<User>(V)->getOperandUse(Index));
|
|
|
|
}
|
|
|
|
|
2010-08-20 22:51:22 +08:00
|
|
|
void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
|
|
|
|
unwrap<User>(Val)->setOperand(Index, unwrap(Op));
|
|
|
|
}
|
|
|
|
|
|
|
|
int LLVMGetNumOperands(LLVMValueRef Val) {
|
2011-10-06 20:13:11 +08:00
|
|
|
Value *V = unwrap(Val);
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
if (isa<MetadataAsValue>(V))
|
|
|
|
return LLVMGetMDNodeNumOperands(Val);
|
|
|
|
|
2011-10-06 20:13:11 +08:00
|
|
|
return cast<User>(V)->getNumOperands();
|
2010-08-20 22:51:22 +08:00
|
|
|
}
|
|
|
|
|
2007-09-18 11:18:57 +08:00
|
|
|
/*--.. Operations on constants of any type .................................--*/
|
|
|
|
|
2007-10-06 23:11:06 +08:00
|
|
|
LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
|
2009-08-01 04:28:14 +08:00
|
|
|
return wrap(Constant::getNullValue(unwrap(Ty)));
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
2007-10-06 23:11:06 +08:00
|
|
|
LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
|
2009-08-01 04:28:14 +08:00
|
|
|
return wrap(Constant::getAllOnesValue(unwrap(Ty)));
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
|
2009-07-31 07:03:37 +08:00
|
|
|
return wrap(UndefValue::get(unwrap(Ty)));
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
|
2007-09-19 02:07:51 +08:00
|
|
|
return isa<Constant>(unwrap(Ty));
|
|
|
|
}
|
|
|
|
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool LLVMIsNull(LLVMValueRef Val) {
|
2007-09-18 11:18:57 +08:00
|
|
|
if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
|
|
|
|
return C->isNullValue();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool LLVMIsUndef(LLVMValueRef Val) {
|
2007-09-19 02:07:51 +08:00
|
|
|
return isa<UndefValue>(unwrap(Val));
|
|
|
|
}
|
|
|
|
|
2009-07-07 01:29:59 +08:00
|
|
|
LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
|
2009-07-08 05:33:58 +08:00
|
|
|
return
|
2009-07-31 07:03:37 +08:00
|
|
|
wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
|
2009-07-07 01:29:59 +08:00
|
|
|
}
|
|
|
|
|
2010-02-28 17:45:59 +08:00
|
|
|
/*--.. Operations on metadata nodes ........................................--*/
|
|
|
|
|
|
|
|
LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
|
|
|
|
unsigned SLen) {
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
LLVMContext &Context = *unwrap(C);
|
|
|
|
return wrap(MetadataAsValue::get(
|
|
|
|
Context, MDString::get(Context, StringRef(Str, SLen))));
|
2010-02-28 17:45:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
|
|
|
|
return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
|
|
|
|
unsigned Count) {
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
LLVMContext &Context = *unwrap(C);
|
|
|
|
SmallVector<Metadata *, 8> MDs;
|
|
|
|
for (auto *OV : makeArrayRef(Vals, Count)) {
|
|
|
|
Value *V = unwrap(OV);
|
|
|
|
Metadata *MD;
|
|
|
|
if (!V)
|
|
|
|
MD = nullptr;
|
|
|
|
else if (auto *C = dyn_cast<Constant>(V))
|
|
|
|
MD = ConstantAsMetadata::get(C);
|
|
|
|
else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
|
|
|
|
MD = MDV->getMetadata();
|
|
|
|
assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
|
|
|
|
"outside of direct argument to call");
|
|
|
|
} else {
|
|
|
|
// This is function-local metadata. Pretend to make an MDNode.
|
|
|
|
assert(Count == 1 &&
|
|
|
|
"Expected only one operand to function-local metadata");
|
|
|
|
return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
|
|
|
|
}
|
|
|
|
|
|
|
|
MDs.push_back(MD);
|
|
|
|
}
|
|
|
|
return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
|
2010-02-28 17:45:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
|
|
|
|
return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
|
|
|
|
}
|
|
|
|
|
2011-10-06 20:13:11 +08:00
|
|
|
const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
|
|
|
|
if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
|
|
|
|
*Len = S->getString().size();
|
|
|
|
return S->getString().data();
|
|
|
|
}
|
2011-10-15 04:37:42 +08:00
|
|
|
*Len = 0;
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2011-10-06 20:13:11 +08:00
|
|
|
}
|
|
|
|
|
2012-09-20 04:29:39 +08:00
|
|
|
unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
|
|
|
|
{
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
auto *MD = cast<MetadataAsValue>(unwrap(V));
|
|
|
|
if (isa<ValueAsMetadata>(MD->getMetadata()))
|
|
|
|
return 1;
|
|
|
|
return cast<MDNode>(MD->getMetadata())->getNumOperands();
|
2012-09-20 04:29:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
|
|
|
|
{
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
auto *MD = cast<MetadataAsValue>(unwrap(V));
|
|
|
|
if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
|
|
|
|
*Dest = wrap(MDV->getValue());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto *N = cast<MDNode>(MD->getMetadata());
|
2012-09-20 04:29:39 +08:00
|
|
|
const unsigned numOperands = N->getNumOperands();
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
LLVMContext &Context = unwrap(V)->getContext();
|
2012-09-20 04:29:39 +08:00
|
|
|
for (unsigned i = 0; i < numOperands; i++)
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
Dest[i] = getMDNodeOperandImpl(Context, N, i);
|
2012-09-20 04:29:39 +08:00
|
|
|
}
|
|
|
|
|
2011-10-06 20:13:11 +08:00
|
|
|
unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
|
|
|
|
{
|
2011-10-15 04:37:42 +08:00
|
|
|
if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
|
|
|
|
return N->getNumOperands();
|
|
|
|
}
|
|
|
|
return 0;
|
2011-10-06 20:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
|
|
|
|
{
|
2011-10-15 04:37:42 +08:00
|
|
|
NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
|
|
|
|
if (!N)
|
|
|
|
return;
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
LLVMContext &Context = unwrap(M)->getContext();
|
2011-10-15 04:37:42 +08:00
|
|
|
for (unsigned i=0;i<N->getNumOperands();i++)
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
|
2011-10-06 20:13:11 +08:00
|
|
|
}
|
|
|
|
|
2011-12-21 03:29:36 +08:00
|
|
|
void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
|
|
|
|
LLVMValueRef Val)
|
|
|
|
{
|
|
|
|
NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
|
|
|
|
if (!N)
|
|
|
|
return;
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
if (!Val)
|
|
|
|
return;
|
2015-01-29 00:35:59 +08:00
|
|
|
N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
|
2011-12-21 03:29:36 +08:00
|
|
|
}
|
|
|
|
|
2007-09-18 11:18:57 +08:00
|
|
|
/*--.. Operations on scalar constants ......................................--*/
|
|
|
|
|
2007-10-06 23:11:06 +08:00
|
|
|
LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool SignExtend) {
|
2009-07-25 07:12:02 +08:00
|
|
|
return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
2010-11-23 10:47:22 +08:00
|
|
|
LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
|
|
|
|
unsigned NumWords,
|
|
|
|
const uint64_t Words[]) {
|
|
|
|
IntegerType *Ty = unwrap<IntegerType>(IntTy);
|
|
|
|
return wrap(ConstantInt::get(Ty->getContext(),
|
2011-07-19 05:45:40 +08:00
|
|
|
APInt(Ty->getBitWidth(),
|
|
|
|
makeArrayRef(Words, NumWords))));
|
2010-11-23 10:47:22 +08:00
|
|
|
}
|
|
|
|
|
2009-08-17 07:36:46 +08:00
|
|
|
LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
|
|
|
|
uint8_t Radix) {
|
|
|
|
return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
|
|
|
|
Radix));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
|
|
|
|
unsigned SLen, uint8_t Radix) {
|
|
|
|
return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
|
|
|
|
Radix));
|
2008-02-02 09:07:50 +08:00
|
|
|
}
|
|
|
|
|
2007-10-06 23:11:06 +08:00
|
|
|
LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
|
2009-08-17 07:36:46 +08:00
|
|
|
return wrap(ConstantFP::get(unwrap(RealTy), N));
|
2008-02-02 09:07:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
|
2009-08-17 07:36:46 +08:00
|
|
|
return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
|
|
|
|
unsigned SLen) {
|
|
|
|
return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
2009-10-12 12:01:02 +08:00
|
|
|
unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
|
|
|
|
return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
|
|
|
|
return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
|
|
|
|
}
|
|
|
|
|
2014-10-29 03:46:44 +08:00
|
|
|
double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
|
|
|
|
ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
|
|
|
|
Type *Ty = cFP->getType();
|
|
|
|
|
|
|
|
if (Ty->isFloatTy()) {
|
|
|
|
*LosesInfo = false;
|
|
|
|
return cFP->getValueAPF().convertToFloat();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Ty->isDoubleTy()) {
|
|
|
|
*LosesInfo = false;
|
|
|
|
return cFP->getValueAPF().convertToDouble();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool APFLosesInfo;
|
|
|
|
APFloat APF = cFP->getValueAPF();
|
|
|
|
APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &APFLosesInfo);
|
|
|
|
*LosesInfo = APFLosesInfo;
|
|
|
|
return APF.convertToDouble();
|
|
|
|
}
|
|
|
|
|
2007-09-18 11:18:57 +08:00
|
|
|
/*--.. Operations on composite constants ...................................--*/
|
|
|
|
|
2009-08-14 08:01:31 +08:00
|
|
|
LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
|
2010-01-10 06:27:07 +08:00
|
|
|
unsigned Length,
|
|
|
|
LLVMBool DontNullTerminate) {
|
2007-09-18 11:18:57 +08:00
|
|
|
/* Inverted the sense of AddNull because ', 0)' is a
|
|
|
|
better mnemonic for null termination than ', 1)'. */
|
2012-02-05 10:29:43 +08:00
|
|
|
return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
|
|
|
|
DontNullTerminate == 0));
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
2016-03-13 08:54:40 +08:00
|
|
|
|
2009-08-14 08:01:31 +08:00
|
|
|
LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool DontNullTerminate) {
|
2009-08-14 08:01:31 +08:00
|
|
|
return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
|
|
|
|
DontNullTerminate);
|
|
|
|
}
|
2014-08-04 07:54:16 +08:00
|
|
|
|
2016-03-13 08:54:40 +08:00
|
|
|
LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
|
|
|
|
return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
|
2014-08-04 07:54:16 +08:00
|
|
|
}
|
|
|
|
|
2016-03-13 08:54:40 +08:00
|
|
|
LLVMBool LLVMIsConstantString(LLVMValueRef C) {
|
|
|
|
return unwrap<ConstantDataSequential>(C)->isString();
|
2014-08-04 07:54:16 +08:00
|
|
|
}
|
|
|
|
|
2016-03-13 08:54:40 +08:00
|
|
|
const char *LLVMGetAsString(LLVMValueRef C, size_t* Length) {
|
|
|
|
StringRef str = unwrap<ConstantDataSequential>(C)->getAsString();
|
2014-08-04 07:54:16 +08:00
|
|
|
*Length = str.size();
|
|
|
|
return str.data();
|
|
|
|
}
|
|
|
|
|
2007-10-06 23:11:06 +08:00
|
|
|
LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
|
|
|
|
LLVMValueRef *ConstantVals, unsigned Length) {
|
2011-06-22 17:24:39 +08:00
|
|
|
ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
|
|
|
|
return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
2014-08-04 07:54:16 +08:00
|
|
|
|
2016-03-13 08:40:12 +08:00
|
|
|
LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
|
|
|
|
LLVMValueRef *ConstantVals,
|
|
|
|
unsigned Count, LLVMBool Packed) {
|
|
|
|
Constant **Elements = unwrap<Constant>(ConstantVals, Count);
|
|
|
|
return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
|
|
|
|
Packed != 0));
|
|
|
|
}
|
|
|
|
|
2007-10-06 23:11:06 +08:00
|
|
|
LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool Packed) {
|
2009-08-14 08:01:31 +08:00
|
|
|
return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
|
|
|
|
Packed);
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
2011-07-15 03:09:08 +08:00
|
|
|
|
|
|
|
LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
|
|
|
|
LLVMValueRef *ConstantVals,
|
|
|
|
unsigned Count) {
|
|
|
|
Constant **Elements = unwrap<Constant>(ConstantVals, Count);
|
2011-07-18 12:54:35 +08:00
|
|
|
StructType *Ty = cast<StructType>(unwrap(StructTy));
|
2011-07-15 03:09:08 +08:00
|
|
|
|
2011-07-18 20:00:32 +08:00
|
|
|
return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
|
2011-07-15 03:09:08 +08:00
|
|
|
}
|
|
|
|
|
2007-10-06 23:11:06 +08:00
|
|
|
LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
|
2011-07-18 20:00:32 +08:00
|
|
|
return wrap(ConstantVector::get(makeArrayRef(
|
2011-02-15 08:14:00 +08:00
|
|
|
unwrap<Constant>(ScalarConstantVals, Size), Size)));
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
2011-10-06 20:39:34 +08:00
|
|
|
|
|
|
|
/*-- Opcode mapping */
|
|
|
|
|
|
|
|
static LLVMOpcode map_to_llvmopcode(int opcode)
|
|
|
|
{
|
|
|
|
switch (opcode) {
|
2012-02-06 06:14:15 +08:00
|
|
|
default: llvm_unreachable("Unhandled Opcode.");
|
2011-10-06 20:39:34 +08:00
|
|
|
#define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instruction.def"
|
2011-10-06 20:39:34 +08:00
|
|
|
#undef HANDLE_INST
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int map_from_llvmopcode(LLVMOpcode code)
|
|
|
|
{
|
|
|
|
switch (code) {
|
|
|
|
#define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instruction.def"
|
2011-10-06 20:39:34 +08:00
|
|
|
#undef HANDLE_INST
|
|
|
|
}
|
2012-01-17 07:24:27 +08:00
|
|
|
llvm_unreachable("Unhandled Opcode.");
|
2011-10-06 20:39:34 +08:00
|
|
|
}
|
|
|
|
|
2007-10-06 22:29:36 +08:00
|
|
|
/*--.. Constant expressions ................................................--*/
|
|
|
|
|
2009-10-12 12:01:02 +08:00
|
|
|
LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
|
2011-10-06 20:39:34 +08:00
|
|
|
return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
|
2009-10-12 12:01:02 +08:00
|
|
|
}
|
|
|
|
|
2009-05-21 23:52:21 +08:00
|
|
|
LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
|
2009-07-30 02:55:55 +08:00
|
|
|
return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
|
2009-05-21 23:52:21 +08:00
|
|
|
}
|
|
|
|
|
2007-10-06 22:29:36 +08:00
|
|
|
LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
|
2009-07-30 02:55:55 +08:00
|
|
|
return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
|
2007-10-06 22:29:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
|
2009-08-16 10:20:12 +08:00
|
|
|
}
|
|
|
|
|
2010-02-28 13:51:43 +08:00
|
|
|
LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
|
2010-02-28 13:51:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
|
2010-02-28 13:51:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-16 10:20:12 +08:00
|
|
|
LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
|
2007-10-06 22:29:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
|
2007-10-06 22:29:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
2009-09-04 07:34:49 +08:00
|
|
|
LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
|
|
|
|
LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
|
2009-09-04 07:34:49 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
2010-02-28 13:51:43 +08:00
|
|
|
LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
|
|
|
|
LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
|
2010-02-28 13:51:43 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
2009-08-16 10:20:12 +08:00
|
|
|
LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
|
2009-08-16 10:20:12 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
2007-10-06 22:29:36 +08:00
|
|
|
LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
2010-02-28 13:51:43 +08:00
|
|
|
LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
|
|
|
|
LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
|
2010-02-28 13:51:43 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
|
|
|
|
LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
|
2010-02-28 13:51:43 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
2009-08-16 10:20:12 +08:00
|
|
|
LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
|
|
|
return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
|
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
2007-10-06 22:29:36 +08:00
|
|
|
LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
2010-02-28 13:51:43 +08:00
|
|
|
LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
|
|
|
|
LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
|
2010-02-28 13:51:43 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
|
|
|
|
LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
|
2010-02-28 13:51:43 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
2009-08-16 10:20:12 +08:00
|
|
|
LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
|
2009-08-16 10:20:12 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
2007-10-06 22:29:36 +08:00
|
|
|
LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
2009-09-04 07:34:49 +08:00
|
|
|
LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
|
|
|
|
LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
|
2009-09-04 07:34:49 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
2007-10-06 22:29:36 +08:00
|
|
|
LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
|
|
|
|
LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
2009-07-30 02:55:55 +08:00
|
|
|
return wrap(ConstantExpr::getICmp(Predicate,
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(LHSConstant),
|
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
|
|
|
|
LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
2009-07-30 02:55:55 +08:00
|
|
|
return wrap(ConstantExpr::getFCmp(Predicate,
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(LHSConstant),
|
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
|
|
|
|
unwrap<Constant>(RHSConstant)));
|
2007-10-06 22:29:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(RHSConstant)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
|
|
|
|
LLVMValueRef *ConstantIndices, unsigned NumIndices) {
|
2011-07-21 22:31:17 +08:00
|
|
|
ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
|
|
|
|
NumIndices);
|
2015-04-03 02:55:32 +08:00
|
|
|
return wrap(ConstantExpr::getGetElementPtr(
|
|
|
|
nullptr, unwrap<Constant>(ConstantVal), IdxList));
|
2007-10-06 22:29:36 +08:00
|
|
|
}
|
|
|
|
|
2009-09-04 07:34:49 +08:00
|
|
|
LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
|
|
|
|
LLVMValueRef *ConstantIndices,
|
|
|
|
unsigned NumIndices) {
|
|
|
|
Constant* Val = unwrap<Constant>(ConstantVal);
|
2011-07-21 22:31:17 +08:00
|
|
|
ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
|
|
|
|
NumIndices);
|
2015-04-03 02:55:32 +08:00
|
|
|
return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList));
|
2009-09-04 07:34:49 +08:00
|
|
|
}
|
|
|
|
|
2007-10-06 22:29:36 +08:00
|
|
|
LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap(ToType)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap(ToType)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap(ToType)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap(ToType)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap(ToType)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap(ToType)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
|
2009-07-30 02:55:55 +08:00
|
|
|
return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap(ToType)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
|
2009-07-30 02:55:55 +08:00
|
|
|
return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap(ToType)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap(ToType)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap(ToType)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap(ToType)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap(ToType)));
|
|
|
|
}
|
|
|
|
|
2013-11-15 09:34:59 +08:00
|
|
|
LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
|
|
|
|
LLVMTypeRef ToType) {
|
|
|
|
return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
|
|
|
|
unwrap(ToType)));
|
|
|
|
}
|
|
|
|
|
2009-08-16 10:20:12 +08:00
|
|
|
LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
|
|
|
|
LLVMTypeRef ToType) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
|
2009-08-16 10:20:12 +08:00
|
|
|
unwrap(ToType)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
|
|
|
|
LLVMTypeRef ToType) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
|
2009-08-16 10:20:12 +08:00
|
|
|
unwrap(ToType)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
|
|
|
|
LLVMTypeRef ToType) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
|
2009-08-16 10:20:12 +08:00
|
|
|
unwrap(ToType)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
|
|
|
|
LLVMTypeRef ToType) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
|
2009-08-16 10:20:12 +08:00
|
|
|
unwrap(ToType)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool isSigned) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
|
|
|
|
unwrap(ToType), isSigned));
|
2009-08-16 10:20:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
|
2009-08-16 10:20:12 +08:00
|
|
|
unwrap(ToType)));
|
|
|
|
}
|
|
|
|
|
2007-10-06 22:29:36 +08:00
|
|
|
LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
|
|
|
|
LLVMValueRef ConstantIfTrue,
|
|
|
|
LLVMValueRef ConstantIfFalse) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(ConstantIfTrue),
|
|
|
|
unwrap<Constant>(ConstantIfFalse)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
|
|
|
|
LLVMValueRef IndexConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(IndexConstant)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
|
|
|
|
LLVMValueRef ElementValueConstant,
|
|
|
|
LLVMValueRef IndexConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(ElementValueConstant),
|
|
|
|
unwrap<Constant>(IndexConstant)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
|
|
|
|
LLVMValueRef VectorBConstant,
|
|
|
|
LLVMValueRef MaskConstant) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
|
2007-10-06 22:29:36 +08:00
|
|
|
unwrap<Constant>(VectorBConstant),
|
|
|
|
unwrap<Constant>(MaskConstant)));
|
|
|
|
}
|
2007-09-18 11:18:57 +08:00
|
|
|
|
2008-11-04 06:55:43 +08:00
|
|
|
LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
|
|
|
|
unsigned NumIdx) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
|
2011-07-18 20:00:32 +08:00
|
|
|
makeArrayRef(IdxList, NumIdx)));
|
2008-11-04 06:55:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
|
|
|
|
LLVMValueRef ElementValueConstant,
|
|
|
|
unsigned *IdxList, unsigned NumIdx) {
|
2011-02-15 08:14:00 +08:00
|
|
|
return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
|
2008-11-04 06:55:43 +08:00
|
|
|
unwrap<Constant>(ElementValueConstant),
|
2011-07-18 20:00:32 +08:00
|
|
|
makeArrayRef(IdxList, NumIdx)));
|
2008-11-04 06:55:43 +08:00
|
|
|
}
|
|
|
|
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
|
|
|
|
const char *Constraints,
|
|
|
|
LLVMBool HasSideEffects,
|
2012-09-10 19:52:00 +08:00
|
|
|
LLVMBool IsAlignStack) {
|
2010-01-10 06:27:07 +08:00
|
|
|
return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
|
2012-09-10 19:52:00 +08:00
|
|
|
Constraints, HasSideEffects, IsAlignStack));
|
2008-12-18 05:39:50 +08:00
|
|
|
}
|
|
|
|
|
2010-02-28 17:46:06 +08:00
|
|
|
LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
|
|
|
|
return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
|
|
|
|
}
|
|
|
|
|
2007-09-18 11:18:57 +08:00
|
|
|
/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
|
|
|
|
|
2008-03-19 09:11:35 +08:00
|
|
|
LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
|
|
|
|
return wrap(unwrap<GlobalValue>(Global)->getParent());
|
|
|
|
}
|
|
|
|
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
|
2007-09-18 11:18:57 +08:00
|
|
|
return unwrap<GlobalValue>(Global)->isDeclaration();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
|
2009-07-21 04:34:46 +08:00
|
|
|
switch (unwrap<GlobalValue>(Global)->getLinkage()) {
|
|
|
|
case GlobalValue::ExternalLinkage:
|
|
|
|
return LLVMExternalLinkage;
|
|
|
|
case GlobalValue::AvailableExternallyLinkage:
|
|
|
|
return LLVMAvailableExternallyLinkage;
|
|
|
|
case GlobalValue::LinkOnceAnyLinkage:
|
|
|
|
return LLVMLinkOnceAnyLinkage;
|
|
|
|
case GlobalValue::LinkOnceODRLinkage:
|
|
|
|
return LLVMLinkOnceODRLinkage;
|
|
|
|
case GlobalValue::WeakAnyLinkage:
|
|
|
|
return LLVMWeakAnyLinkage;
|
|
|
|
case GlobalValue::WeakODRLinkage:
|
|
|
|
return LLVMWeakODRLinkage;
|
|
|
|
case GlobalValue::AppendingLinkage:
|
|
|
|
return LLVMAppendingLinkage;
|
|
|
|
case GlobalValue::InternalLinkage:
|
|
|
|
return LLVMInternalLinkage;
|
|
|
|
case GlobalValue::PrivateLinkage:
|
|
|
|
return LLVMPrivateLinkage;
|
|
|
|
case GlobalValue::ExternalWeakLinkage:
|
|
|
|
return LLVMExternalWeakLinkage;
|
|
|
|
case GlobalValue::CommonLinkage:
|
|
|
|
return LLVMCommonLinkage;
|
|
|
|
}
|
|
|
|
|
2012-01-17 15:00:13 +08:00
|
|
|
llvm_unreachable("Invalid GlobalValue linkage!");
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
|
2009-07-21 04:34:46 +08:00
|
|
|
GlobalValue *GV = unwrap<GlobalValue>(Global);
|
|
|
|
|
|
|
|
switch (Linkage) {
|
|
|
|
case LLVMExternalLinkage:
|
|
|
|
GV->setLinkage(GlobalValue::ExternalLinkage);
|
|
|
|
break;
|
|
|
|
case LLVMAvailableExternallyLinkage:
|
|
|
|
GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
|
|
|
|
break;
|
|
|
|
case LLVMLinkOnceAnyLinkage:
|
|
|
|
GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
|
|
|
|
break;
|
|
|
|
case LLVMLinkOnceODRLinkage:
|
|
|
|
GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
|
|
|
|
break;
|
2012-08-18 02:33:14 +08:00
|
|
|
case LLVMLinkOnceODRAutoHideLinkage:
|
2013-11-02 01:09:14 +08:00
|
|
|
DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
|
|
|
|
"longer supported.");
|
2012-08-18 02:33:14 +08:00
|
|
|
break;
|
2009-07-21 04:34:46 +08:00
|
|
|
case LLVMWeakAnyLinkage:
|
|
|
|
GV->setLinkage(GlobalValue::WeakAnyLinkage);
|
|
|
|
break;
|
|
|
|
case LLVMWeakODRLinkage:
|
|
|
|
GV->setLinkage(GlobalValue::WeakODRLinkage);
|
|
|
|
break;
|
|
|
|
case LLVMAppendingLinkage:
|
|
|
|
GV->setLinkage(GlobalValue::AppendingLinkage);
|
|
|
|
break;
|
|
|
|
case LLVMInternalLinkage:
|
|
|
|
GV->setLinkage(GlobalValue::InternalLinkage);
|
|
|
|
break;
|
|
|
|
case LLVMPrivateLinkage:
|
|
|
|
GV->setLinkage(GlobalValue::PrivateLinkage);
|
|
|
|
break;
|
|
|
|
case LLVMLinkerPrivateLinkage:
|
Remove the linker_private and linker_private_weak linkages.
These linkages were introduced some time ago, but it was never very
clear what exactly their semantics were or what they should be used
for. Some investigation found these uses:
* utf-16 strings in clang.
* non-unnamed_addr strings produced by the sanitizers.
It turns out they were just working around a more fundamental problem.
For some sections a MachO linker needs a symbol in order to split the
section into atoms, and llvm had no idea that was the case. I fixed
that in r201700 and it is now safe to use the private linkage. When
the object ends up in a section that requires symbols, llvm will use a
'l' prefix instead of a 'L' prefix and things just work.
With that, these linkages were already dead, but there was a potential
future user in the objc metadata information. I am still looking at
CGObjcMac.cpp, but at this point I am convinced that linker_private
and linker_private_weak are not what they need.
The objc uses are currently split in
* Regular symbols (no '\01' prefix). LLVM already directly provides
whatever semantics they need.
* Uses of a private name (start with "\01L" or "\01l") and private
linkage. We can drop the "\01L" and "\01l" prefixes as soon as llvm
agrees with clang on L being ok or not for a given section. I have two
patches in code review for this.
* Uses of private name and weak linkage.
The last case is the one that one could think would fit one of these
linkages. That is not the case. The semantics are
* the linker will merge these symbol by *name*.
* the linker will hide them in the final DSO.
Given that the merging is done by name, any of the private (or
internal) linkages would be a bad match. They allow llvm to rename the
symbols, and that is really not what we want. From the llvm point of
view, these objects should really be (linkonce|weak)(_odr)?.
For now, just keeping the "\01l" prefix is probably the best for these
symbols. If we one day want to have a more direct support in llvm,
IMHO what we should add is not a linkage, it is just a hidden_symbol
attribute. It would be applicable to multiple linkages. For example,
on weak it would produce the current behavior we have for objc
metadata. On internal, it would be equivalent to private (and we
should then remove private).
llvm-svn: 203866
2014-03-14 07:18:37 +08:00
|
|
|
GV->setLinkage(GlobalValue::PrivateLinkage);
|
2009-07-21 04:34:46 +08:00
|
|
|
break;
|
2010-07-02 05:55:59 +08:00
|
|
|
case LLVMLinkerPrivateWeakLinkage:
|
Remove the linker_private and linker_private_weak linkages.
These linkages were introduced some time ago, but it was never very
clear what exactly their semantics were or what they should be used
for. Some investigation found these uses:
* utf-16 strings in clang.
* non-unnamed_addr strings produced by the sanitizers.
It turns out they were just working around a more fundamental problem.
For some sections a MachO linker needs a symbol in order to split the
section into atoms, and llvm had no idea that was the case. I fixed
that in r201700 and it is now safe to use the private linkage. When
the object ends up in a section that requires symbols, llvm will use a
'l' prefix instead of a 'L' prefix and things just work.
With that, these linkages were already dead, but there was a potential
future user in the objc metadata information. I am still looking at
CGObjcMac.cpp, but at this point I am convinced that linker_private
and linker_private_weak are not what they need.
The objc uses are currently split in
* Regular symbols (no '\01' prefix). LLVM already directly provides
whatever semantics they need.
* Uses of a private name (start with "\01L" or "\01l") and private
linkage. We can drop the "\01L" and "\01l" prefixes as soon as llvm
agrees with clang on L being ok or not for a given section. I have two
patches in code review for this.
* Uses of private name and weak linkage.
The last case is the one that one could think would fit one of these
linkages. That is not the case. The semantics are
* the linker will merge these symbol by *name*.
* the linker will hide them in the final DSO.
Given that the merging is done by name, any of the private (or
internal) linkages would be a bad match. They allow llvm to rename the
symbols, and that is really not what we want. From the llvm point of
view, these objects should really be (linkonce|weak)(_odr)?.
For now, just keeping the "\01l" prefix is probably the best for these
symbols. If we one day want to have a more direct support in llvm,
IMHO what we should add is not a linkage, it is just a hidden_symbol
attribute. It would be applicable to multiple linkages. For example,
on weak it would produce the current behavior we have for objc
metadata. On internal, it would be equivalent to private (and we
should then remove private).
llvm-svn: 203866
2014-03-14 07:18:37 +08:00
|
|
|
GV->setLinkage(GlobalValue::PrivateLinkage);
|
2010-07-02 05:55:59 +08:00
|
|
|
break;
|
2009-07-21 04:34:46 +08:00
|
|
|
case LLVMDLLImportLinkage:
|
2014-01-14 23:22:47 +08:00
|
|
|
DEBUG(errs()
|
|
|
|
<< "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
|
2009-07-21 04:34:46 +08:00
|
|
|
break;
|
|
|
|
case LLVMDLLExportLinkage:
|
2014-01-14 23:22:47 +08:00
|
|
|
DEBUG(errs()
|
|
|
|
<< "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
|
2009-07-21 04:34:46 +08:00
|
|
|
break;
|
|
|
|
case LLVMExternalWeakLinkage:
|
|
|
|
GV->setLinkage(GlobalValue::ExternalWeakLinkage);
|
|
|
|
break;
|
|
|
|
case LLVMGhostLinkage:
|
2010-01-28 04:34:15 +08:00
|
|
|
DEBUG(errs()
|
|
|
|
<< "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
|
2009-07-21 04:34:46 +08:00
|
|
|
break;
|
|
|
|
case LLVMCommonLinkage:
|
|
|
|
GV->setLinkage(GlobalValue::CommonLinkage);
|
|
|
|
break;
|
|
|
|
}
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *LLVMGetSection(LLVMValueRef Global) {
|
2014-06-03 10:41:57 +08:00
|
|
|
return unwrap<GlobalValue>(Global)->getSection();
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetSection(LLVMValueRef Global, const char *Section) {
|
2014-05-14 02:45:48 +08:00
|
|
|
unwrap<GlobalObject>(Global)->setSection(Section);
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
|
|
|
|
return static_cast<LLVMVisibility>(
|
|
|
|
unwrap<GlobalValue>(Global)->getVisibility());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
|
|
|
|
unwrap<GlobalValue>(Global)
|
|
|
|
->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
|
|
|
|
}
|
|
|
|
|
2014-03-05 10:34:23 +08:00
|
|
|
LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
|
|
|
|
return static_cast<LLVMDLLStorageClass>(
|
|
|
|
unwrap<GlobalValue>(Global)->getDLLStorageClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
|
|
|
|
unwrap<GlobalValue>(Global)->setDLLStorageClass(
|
|
|
|
static_cast<GlobalValue::DLLStorageClassTypes>(Class));
|
|
|
|
}
|
|
|
|
|
2014-03-11 03:24:35 +08:00
|
|
|
LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
|
|
|
|
return unwrap<GlobalValue>(Global)->hasUnnamedAddr();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
|
|
|
|
unwrap<GlobalValue>(Global)->setUnnamedAddr(HasUnnamedAddr);
|
|
|
|
}
|
|
|
|
|
2013-10-29 17:02:02 +08:00
|
|
|
/*--.. Operations on global variables, load and store instructions .........--*/
|
|
|
|
|
|
|
|
unsigned LLVMGetAlignment(LLVMValueRef V) {
|
|
|
|
Value *P = unwrap<Value>(V);
|
|
|
|
if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
|
|
|
|
return GV->getAlignment();
|
2014-03-05 13:05:34 +08:00
|
|
|
if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
|
|
|
|
return AI->getAlignment();
|
2013-10-29 17:02:02 +08:00
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(P))
|
|
|
|
return LI->getAlignment();
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(P))
|
|
|
|
return SI->getAlignment();
|
|
|
|
|
2014-03-05 13:05:34 +08:00
|
|
|
llvm_unreachable(
|
|
|
|
"only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
|
2013-10-29 17:02:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
|
|
|
|
Value *P = unwrap<Value>(V);
|
2014-05-14 02:45:48 +08:00
|
|
|
if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
|
2013-10-29 17:02:02 +08:00
|
|
|
GV->setAlignment(Bytes);
|
2014-03-05 13:05:34 +08:00
|
|
|
else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
|
|
|
|
AI->setAlignment(Bytes);
|
2013-10-29 17:02:02 +08:00
|
|
|
else if (LoadInst *LI = dyn_cast<LoadInst>(P))
|
|
|
|
LI->setAlignment(Bytes);
|
|
|
|
else if (StoreInst *SI = dyn_cast<StoreInst>(P))
|
|
|
|
SI->setAlignment(Bytes);
|
2013-10-29 17:37:28 +08:00
|
|
|
else
|
2014-03-05 13:05:34 +08:00
|
|
|
llvm_unreachable(
|
|
|
|
"only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*--.. Operations on global variables ......................................--*/
|
|
|
|
|
|
|
|
LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
|
2009-07-09 03:03:57 +08:00
|
|
|
return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
|
2014-04-09 14:08:46 +08:00
|
|
|
GlobalValue::ExternalLinkage, nullptr, Name));
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
2010-02-28 17:46:13 +08:00
|
|
|
LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
|
|
|
|
const char *Name,
|
|
|
|
unsigned AddressSpace) {
|
|
|
|
return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
|
2014-04-09 14:08:46 +08:00
|
|
|
GlobalValue::ExternalLinkage, nullptr, Name,
|
|
|
|
nullptr, GlobalVariable::NotThreadLocal,
|
|
|
|
AddressSpace));
|
2010-02-28 17:46:13 +08:00
|
|
|
}
|
|
|
|
|
2007-10-08 11:45:09 +08:00
|
|
|
LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
|
|
|
|
return wrap(unwrap(M)->getNamedGlobal(Name));
|
|
|
|
}
|
|
|
|
|
2008-03-19 11:47:18 +08:00
|
|
|
LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
|
|
|
|
Module *Mod = unwrap(M);
|
|
|
|
Module::global_iterator I = Mod->global_begin();
|
|
|
|
if (I == Mod->global_end())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*I);
|
2008-03-19 11:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
|
|
|
|
Module *Mod = unwrap(M);
|
|
|
|
Module::global_iterator I = Mod->global_end();
|
|
|
|
if (I == Mod->global_begin())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*--I);
|
2008-03-19 11:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
|
|
|
|
GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
|
2015-10-09 07:49:46 +08:00
|
|
|
Module::global_iterator I(GV);
|
2008-03-19 11:47:18 +08:00
|
|
|
if (++I == GV->getParent()->global_end())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*I);
|
2008-03-19 11:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
|
|
|
|
GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
|
2015-10-09 07:49:46 +08:00
|
|
|
Module::global_iterator I(GV);
|
2008-03-24 06:21:29 +08:00
|
|
|
if (I == GV->getParent()->global_begin())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*--I);
|
2008-03-19 11:47:18 +08:00
|
|
|
}
|
|
|
|
|
2007-09-18 11:18:57 +08:00
|
|
|
void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
|
|
|
|
unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
|
2009-10-12 12:01:02 +08:00
|
|
|
GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
|
|
|
|
if ( !GV->hasInitializer() )
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2009-10-12 12:01:02 +08:00
|
|
|
return wrap(GV->getInitializer());
|
2007-09-18 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
|
|
|
|
unwrap<GlobalVariable>(GlobalVar)
|
|
|
|
->setInitializer(unwrap<Constant>(ConstantVal));
|
|
|
|
}
|
|
|
|
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
|
2007-09-18 11:18:57 +08:00
|
|
|
return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
|
|
|
|
}
|
|
|
|
|
2010-01-10 06:27:07 +08:00
|
|
|
void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
|
2007-09-18 11:18:57 +08:00
|
|
|
unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
|
|
|
|
}
|
|
|
|
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
|
2007-10-08 01:31:42 +08:00
|
|
|
return unwrap<GlobalVariable>(GlobalVar)->isConstant();
|
|
|
|
}
|
|
|
|
|
2010-01-10 06:27:07 +08:00
|
|
|
void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
|
2007-10-08 01:31:42 +08:00
|
|
|
unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
|
|
|
|
}
|
|
|
|
|
2013-04-16 16:58:59 +08:00
|
|
|
LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
|
|
|
|
switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
|
|
|
|
case GlobalVariable::NotThreadLocal:
|
|
|
|
return LLVMNotThreadLocal;
|
|
|
|
case GlobalVariable::GeneralDynamicTLSModel:
|
|
|
|
return LLVMGeneralDynamicTLSModel;
|
|
|
|
case GlobalVariable::LocalDynamicTLSModel:
|
|
|
|
return LLVMLocalDynamicTLSModel;
|
|
|
|
case GlobalVariable::InitialExecTLSModel:
|
|
|
|
return LLVMInitialExecTLSModel;
|
|
|
|
case GlobalVariable::LocalExecTLSModel:
|
|
|
|
return LLVMLocalExecTLSModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Invalid GlobalVariable thread local mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
|
|
|
|
GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
|
|
|
|
|
|
|
|
switch (Mode) {
|
|
|
|
case LLVMNotThreadLocal:
|
|
|
|
GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
|
|
|
|
break;
|
|
|
|
case LLVMGeneralDynamicTLSModel:
|
|
|
|
GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
|
|
|
|
break;
|
|
|
|
case LLVMLocalDynamicTLSModel:
|
|
|
|
GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
|
|
|
|
break;
|
|
|
|
case LLVMInitialExecTLSModel:
|
|
|
|
GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
|
|
|
|
break;
|
|
|
|
case LLVMLocalExecTLSModel:
|
|
|
|
GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
|
|
|
|
return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
|
|
|
|
unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
|
|
|
|
}
|
|
|
|
|
2008-12-18 05:39:50 +08:00
|
|
|
/*--.. Operations on aliases ......................................--*/
|
|
|
|
|
|
|
|
LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
|
|
|
|
const char *Name) {
|
2014-05-16 21:34:04 +08:00
|
|
|
auto *PTy = cast<PointerType>(unwrap(Ty));
|
2015-09-15 02:01:59 +08:00
|
|
|
return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
|
|
|
|
GlobalValue::ExternalLinkage, Name,
|
2015-02-24 05:51:06 +08:00
|
|
|
unwrap<Constant>(Aliasee), unwrap(M)));
|
2008-12-18 05:39:50 +08:00
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
/*--.. Operations on functions .............................................--*/
|
|
|
|
|
|
|
|
LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
|
|
|
|
LLVMTypeRef FunctionTy) {
|
2008-04-07 04:25:17 +08:00
|
|
|
return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
|
|
|
|
GlobalValue::ExternalLinkage, Name, unwrap(M)));
|
2007-09-27 04:56:12 +08:00
|
|
|
}
|
|
|
|
|
2007-10-08 11:45:09 +08:00
|
|
|
LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
|
|
|
|
return wrap(unwrap(M)->getFunction(Name));
|
|
|
|
}
|
|
|
|
|
2008-03-19 11:47:18 +08:00
|
|
|
LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
|
|
|
|
Module *Mod = unwrap(M);
|
|
|
|
Module::iterator I = Mod->begin();
|
|
|
|
if (I == Mod->end())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*I);
|
2008-03-19 11:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
|
|
|
|
Module *Mod = unwrap(M);
|
|
|
|
Module::iterator I = Mod->end();
|
|
|
|
if (I == Mod->begin())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*--I);
|
2008-03-19 11:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
|
|
|
|
Function *Func = unwrap<Function>(Fn);
|
2015-10-09 07:49:46 +08:00
|
|
|
Module::iterator I(Func);
|
2008-03-19 11:47:18 +08:00
|
|
|
if (++I == Func->getParent()->end())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*I);
|
2008-03-19 11:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
|
|
|
|
Function *Func = unwrap<Function>(Fn);
|
2015-10-09 07:49:46 +08:00
|
|
|
Module::iterator I(Func);
|
2008-03-24 06:21:29 +08:00
|
|
|
if (I == Func->getParent()->begin())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*--I);
|
2008-03-19 11:47:18 +08:00
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
void LLVMDeleteFunction(LLVMValueRef Fn) {
|
|
|
|
unwrap<Function>(Fn)->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2016-02-19 04:38:32 +08:00
|
|
|
LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
|
|
|
|
return unwrap<Function>(Fn)->hasPersonalityFn();
|
|
|
|
}
|
|
|
|
|
2015-07-14 09:23:06 +08:00
|
|
|
LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
|
|
|
|
return wrap(unwrap<Function>(Fn)->getPersonalityFn());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
|
|
|
|
unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
|
|
|
|
if (Function *F = dyn_cast<Function>(unwrap(Fn)))
|
|
|
|
return F->getIntrinsicID();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
|
|
|
|
return unwrap<Function>(Fn)->getCallingConv();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
|
2009-09-02 16:44:58 +08:00
|
|
|
return unwrap<Function>(Fn)->setCallingConv(
|
|
|
|
static_cast<CallingConv::ID>(CC));
|
2007-09-27 04:56:12 +08:00
|
|
|
}
|
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
const char *LLVMGetGC(LLVMValueRef Fn) {
|
2007-12-10 11:18:06 +08:00
|
|
|
Function *F = unwrap<Function>(Fn);
|
2016-01-08 10:28:20 +08:00
|
|
|
return F->hasGC()? F->getGC().c_str() : nullptr;
|
2007-12-10 11:18:06 +08:00
|
|
|
}
|
|
|
|
|
2008-08-18 02:44:35 +08:00
|
|
|
void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
|
2007-12-10 11:18:06 +08:00
|
|
|
Function *F = unwrap<Function>(Fn);
|
2008-08-18 02:44:35 +08:00
|
|
|
if (GC)
|
|
|
|
F->setGC(GC);
|
2007-12-10 11:18:06 +08:00
|
|
|
else
|
2008-08-18 02:44:35 +08:00
|
|
|
F->clearGC();
|
2007-12-10 11:18:06 +08:00
|
|
|
}
|
|
|
|
|
2009-05-06 20:21:17 +08:00
|
|
|
void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
|
|
|
|
Function *Func = unwrap<Function>(Fn);
|
2012-12-08 07:16:57 +08:00
|
|
|
const AttributeSet PAL = Func->getAttributes();
|
2012-10-16 04:35:56 +08:00
|
|
|
AttrBuilder B(PA);
|
2012-12-08 07:16:57 +08:00
|
|
|
const AttributeSet PALnew =
|
2013-01-31 07:40:31 +08:00
|
|
|
PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
|
|
|
|
AttributeSet::get(Func->getContext(),
|
|
|
|
AttributeSet::FunctionIndex, B));
|
2009-05-06 20:21:17 +08:00
|
|
|
Func->setAttributes(PALnew);
|
|
|
|
}
|
|
|
|
|
2013-04-17 07:12:43 +08:00
|
|
|
void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
|
|
|
|
const char *V) {
|
|
|
|
Function *Func = unwrap<Function>(Fn);
|
2013-04-18 02:26:02 +08:00
|
|
|
AttributeSet::AttrIndex Idx =
|
|
|
|
AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
|
2013-04-17 07:12:43 +08:00
|
|
|
AttrBuilder B;
|
|
|
|
|
|
|
|
B.addAttribute(A, V);
|
|
|
|
AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
|
|
|
|
Func->addAttributes(Idx, Set);
|
|
|
|
}
|
|
|
|
|
2009-05-06 20:21:17 +08:00
|
|
|
void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
|
|
|
|
Function *Func = unwrap<Function>(Fn);
|
2012-12-08 07:16:57 +08:00
|
|
|
const AttributeSet PAL = Func->getAttributes();
|
2012-10-16 04:35:56 +08:00
|
|
|
AttrBuilder B(PA);
|
2012-12-08 07:16:57 +08:00
|
|
|
const AttributeSet PALnew =
|
2013-01-23 08:45:55 +08:00
|
|
|
PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
|
|
|
|
AttributeSet::get(Func->getContext(),
|
|
|
|
AttributeSet::FunctionIndex, B));
|
2009-05-06 20:21:17 +08:00
|
|
|
Func->setAttributes(PALnew);
|
|
|
|
}
|
|
|
|
|
2009-10-12 12:01:02 +08:00
|
|
|
LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
|
|
|
|
Function *Func = unwrap<Function>(Fn);
|
2012-12-08 07:16:57 +08:00
|
|
|
const AttributeSet PAL = Func->getAttributes();
|
2013-01-10 07:36:50 +08:00
|
|
|
return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
|
2009-10-12 12:01:02 +08:00
|
|
|
}
|
|
|
|
|
2008-03-19 09:11:35 +08:00
|
|
|
/*--.. Operations on parameters ............................................--*/
|
|
|
|
|
|
|
|
unsigned LLVMCountParams(LLVMValueRef FnRef) {
|
|
|
|
// This function is strictly redundant to
|
|
|
|
// LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
|
2008-06-22 06:06:54 +08:00
|
|
|
return unwrap<Function>(FnRef)->arg_size();
|
2008-03-19 09:11:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
|
|
|
|
Function *Fn = unwrap<Function>(FnRef);
|
|
|
|
for (Function::arg_iterator I = Fn->arg_begin(),
|
|
|
|
E = Fn->arg_end(); I != E; I++)
|
2015-10-09 07:49:46 +08:00
|
|
|
*ParamRefs++ = wrap(&*I);
|
2008-03-19 09:11:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
|
|
|
|
Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
|
|
|
|
while (index --> 0)
|
|
|
|
AI++;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*AI);
|
2008-03-19 09:11:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
|
|
|
|
return wrap(unwrap<Argument>(V)->getParent());
|
|
|
|
}
|
|
|
|
|
2008-03-24 06:21:29 +08:00
|
|
|
LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
|
|
|
|
Function *Func = unwrap<Function>(Fn);
|
|
|
|
Function::arg_iterator I = Func->arg_begin();
|
|
|
|
if (I == Func->arg_end())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*I);
|
2008-03-24 06:21:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
|
|
|
|
Function *Func = unwrap<Function>(Fn);
|
|
|
|
Function::arg_iterator I = Func->arg_end();
|
|
|
|
if (I == Func->arg_begin())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*--I);
|
2008-03-24 06:21:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
|
|
|
|
Argument *A = unwrap<Argument>(Arg);
|
2015-10-09 07:49:46 +08:00
|
|
|
Function::arg_iterator I(A);
|
2008-03-24 06:21:29 +08:00
|
|
|
if (++I == A->getParent()->arg_end())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*I);
|
2008-03-24 06:21:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
|
|
|
|
Argument *A = unwrap<Argument>(Arg);
|
2015-10-09 07:49:46 +08:00
|
|
|
Function::arg_iterator I(A);
|
2008-03-24 06:21:29 +08:00
|
|
|
if (I == A->getParent()->arg_begin())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*--I);
|
2008-03-24 06:21:29 +08:00
|
|
|
}
|
|
|
|
|
2008-09-26 05:00:45 +08:00
|
|
|
void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
|
2012-10-15 12:46:55 +08:00
|
|
|
Argument *A = unwrap<Argument>(Arg);
|
2012-10-16 04:35:56 +08:00
|
|
|
AttrBuilder B(PA);
|
2013-01-23 14:14:59 +08:00
|
|
|
A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
|
2008-04-29 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
2008-09-26 05:00:45 +08:00
|
|
|
void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
|
2012-10-15 12:46:55 +08:00
|
|
|
Argument *A = unwrap<Argument>(Arg);
|
2012-10-16 04:35:56 +08:00
|
|
|
AttrBuilder B(PA);
|
2013-01-23 14:14:59 +08:00
|
|
|
A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
|
2008-04-29 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
2009-10-12 12:01:02 +08:00
|
|
|
LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
|
|
|
|
Argument *A = unwrap<Argument>(Arg);
|
2012-12-30 21:50:49 +08:00
|
|
|
return (LLVMAttribute)A->getParent()->getAttributes().
|
2013-01-10 07:36:50 +08:00
|
|
|
Raw(A->getArgNo()+1);
|
2009-10-12 12:01:02 +08:00
|
|
|
}
|
2013-09-19 07:31:10 +08:00
|
|
|
|
2009-10-12 12:01:02 +08:00
|
|
|
|
2008-04-29 01:37:06 +08:00
|
|
|
void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
|
2013-01-23 14:14:59 +08:00
|
|
|
Argument *A = unwrap<Argument>(Arg);
|
2012-10-16 04:35:56 +08:00
|
|
|
AttrBuilder B;
|
2012-10-14 11:58:29 +08:00
|
|
|
B.addAlignmentAttr(align);
|
2013-01-23 14:14:59 +08:00
|
|
|
A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
|
2008-04-29 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
/*--.. Operations on basic blocks ..........................................--*/
|
|
|
|
|
2008-03-19 09:11:35 +08:00
|
|
|
LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
|
|
|
|
return wrap(static_cast<Value*>(unwrap(BB)));
|
2007-09-27 04:56:12 +08:00
|
|
|
}
|
|
|
|
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
|
2007-09-27 04:56:12 +08:00
|
|
|
return isa<BasicBlock>(unwrap(Val));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
|
|
|
|
return wrap(unwrap<BasicBlock>(Val));
|
|
|
|
}
|
|
|
|
|
2016-02-10 06:36:41 +08:00
|
|
|
const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
|
|
|
|
return unwrap(BB)->getName().data();
|
|
|
|
}
|
|
|
|
|
2008-03-24 06:21:29 +08:00
|
|
|
LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
|
|
|
|
return wrap(unwrap(BB)->getParent());
|
2008-03-19 09:11:35 +08:00
|
|
|
}
|
|
|
|
|
2011-08-24 04:27:46 +08:00
|
|
|
LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
|
|
|
|
return wrap(unwrap(BB)->getTerminator());
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
|
2008-06-22 06:06:54 +08:00
|
|
|
return unwrap<Function>(FnRef)->size();
|
2007-09-27 04:56:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
|
|
|
|
Function *Fn = unwrap<Function>(FnRef);
|
|
|
|
for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
|
2015-10-09 07:49:46 +08:00
|
|
|
*BasicBlocksRefs++ = wrap(&*I);
|
2007-09-27 04:56:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
|
|
|
|
return wrap(&unwrap<Function>(Fn)->getEntryBlock());
|
|
|
|
}
|
|
|
|
|
2008-03-19 11:47:18 +08:00
|
|
|
LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
|
|
|
|
Function *Func = unwrap<Function>(Fn);
|
|
|
|
Function::iterator I = Func->begin();
|
|
|
|
if (I == Func->end())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*I);
|
2008-03-19 11:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
|
|
|
|
Function *Func = unwrap<Function>(Fn);
|
|
|
|
Function::iterator I = Func->end();
|
|
|
|
if (I == Func->begin())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*--I);
|
2008-03-19 11:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
|
|
|
|
BasicBlock *Block = unwrap(BB);
|
2015-10-09 07:49:46 +08:00
|
|
|
Function::iterator I(Block);
|
2008-03-19 11:47:18 +08:00
|
|
|
if (++I == Block->getParent()->end())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*I);
|
2008-03-19 11:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
|
|
|
|
BasicBlock *Block = unwrap(BB);
|
2015-10-09 07:49:46 +08:00
|
|
|
Function::iterator I(Block);
|
2008-03-19 11:47:18 +08:00
|
|
|
if (I == Block->getParent()->begin())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*--I);
|
2008-03-19 11:47:18 +08:00
|
|
|
}
|
|
|
|
|
2009-08-14 08:01:31 +08:00
|
|
|
LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
|
|
|
|
LLVMValueRef FnRef,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
|
2009-08-14 08:01:31 +08:00
|
|
|
return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
|
|
|
|
LLVMBasicBlockRef BBRef,
|
|
|
|
const char *Name) {
|
|
|
|
BasicBlock *BB = unwrap(BBRef);
|
|
|
|
return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
|
2007-09-27 04:56:12 +08:00
|
|
|
}
|
|
|
|
|
2009-08-14 08:01:31 +08:00
|
|
|
LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
|
2007-09-27 04:56:12 +08:00
|
|
|
const char *Name) {
|
2009-08-14 08:01:31 +08:00
|
|
|
return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
|
2007-09-27 04:56:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
|
|
|
|
unwrap(BBRef)->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2011-08-24 04:27:46 +08:00
|
|
|
void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
|
|
|
|
unwrap(BBRef)->removeFromParent();
|
|
|
|
}
|
|
|
|
|
2010-07-19 23:31:07 +08:00
|
|
|
void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
|
|
|
|
unwrap(BB)->moveBefore(unwrap(MovePos));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
|
|
|
|
unwrap(BB)->moveAfter(unwrap(MovePos));
|
|
|
|
}
|
|
|
|
|
2008-03-19 09:11:35 +08:00
|
|
|
/*--.. Operations on instructions ..........................................--*/
|
|
|
|
|
|
|
|
LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
|
|
|
|
return wrap(unwrap<Instruction>(Inst)->getParent());
|
|
|
|
}
|
|
|
|
|
2008-03-19 11:47:18 +08:00
|
|
|
LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
|
|
|
|
BasicBlock *Block = unwrap(BB);
|
|
|
|
BasicBlock::iterator I = Block->begin();
|
|
|
|
if (I == Block->end())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*I);
|
2008-03-19 11:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
|
|
|
|
BasicBlock *Block = unwrap(BB);
|
|
|
|
BasicBlock::iterator I = Block->end();
|
|
|
|
if (I == Block->begin())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*--I);
|
2008-03-19 11:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
|
|
|
|
Instruction *Instr = unwrap<Instruction>(Inst);
|
2015-10-09 07:49:46 +08:00
|
|
|
BasicBlock::iterator I(Instr);
|
2008-03-19 11:47:18 +08:00
|
|
|
if (++I == Instr->getParent()->end())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*I);
|
2008-03-19 11:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
|
|
|
|
Instruction *Instr = unwrap<Instruction>(Inst);
|
2015-10-09 07:49:46 +08:00
|
|
|
BasicBlock::iterator I(Instr);
|
2008-03-19 11:47:18 +08:00
|
|
|
if (I == Instr->getParent()->begin())
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2015-10-09 07:49:46 +08:00
|
|
|
return wrap(&*--I);
|
2008-03-19 11:47:18 +08:00
|
|
|
}
|
|
|
|
|
2016-02-12 05:37:54 +08:00
|
|
|
void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
|
|
|
|
unwrap<Instruction>(Inst)->removeFromParent();
|
|
|
|
}
|
|
|
|
|
2011-10-04 04:59:18 +08:00
|
|
|
void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
|
|
|
|
unwrap<Instruction>(Inst)->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2011-10-06 20:13:20 +08:00
|
|
|
LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
|
2011-10-15 04:37:42 +08:00
|
|
|
if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
|
|
|
|
return (LLVMIntPredicate)I->getPredicate();
|
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
|
|
|
|
if (CE->getOpcode() == Instruction::ICmp)
|
|
|
|
return (LLVMIntPredicate)CE->getPredicate();
|
|
|
|
return (LLVMIntPredicate)0;
|
2011-10-06 20:13:20 +08:00
|
|
|
}
|
|
|
|
|
2014-10-29 03:46:44 +08:00
|
|
|
LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
|
|
|
|
if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
|
|
|
|
return (LLVMRealPredicate)I->getPredicate();
|
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
|
|
|
|
if (CE->getOpcode() == Instruction::FCmp)
|
|
|
|
return (LLVMRealPredicate)CE->getPredicate();
|
|
|
|
return (LLVMRealPredicate)0;
|
|
|
|
}
|
|
|
|
|
2011-10-15 04:37:49 +08:00
|
|
|
LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
|
|
|
|
if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
|
|
|
|
return map_to_llvmopcode(C->getOpcode());
|
|
|
|
return (LLVMOpcode)0;
|
|
|
|
}
|
|
|
|
|
2014-10-17 09:02:34 +08:00
|
|
|
LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
|
|
|
|
if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
|
|
|
|
return wrap(C->clone());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
/*--.. Call and invoke instructions ........................................--*/
|
|
|
|
|
2016-02-10 08:09:37 +08:00
|
|
|
unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
|
|
|
|
return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands();
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
|
2016-02-10 08:09:37 +08:00
|
|
|
return CallSite(unwrap<Instruction>(Instr)).getCallingConv();
|
2007-09-27 04:56:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
|
2016-02-10 08:09:37 +08:00
|
|
|
return CallSite(unwrap<Instruction>(Instr))
|
|
|
|
.setCallingConv(static_cast<CallingConv::ID>(CC));
|
2007-09-27 04:56:12 +08:00
|
|
|
}
|
|
|
|
|
2013-09-19 07:31:10 +08:00
|
|
|
void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
|
2008-09-26 05:00:45 +08:00
|
|
|
LLVMAttribute PA) {
|
2008-04-29 01:37:06 +08:00
|
|
|
CallSite Call = CallSite(unwrap<Instruction>(Instr));
|
2012-10-16 04:35:56 +08:00
|
|
|
AttrBuilder B(PA);
|
2008-09-26 05:00:45 +08:00
|
|
|
Call.setAttributes(
|
2013-01-23 05:15:51 +08:00
|
|
|
Call.getAttributes().addAttributes(Call->getContext(), index,
|
|
|
|
AttributeSet::get(Call->getContext(),
|
|
|
|
index, B)));
|
2008-04-29 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
2013-09-19 07:31:10 +08:00
|
|
|
void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
|
2008-09-26 05:00:45 +08:00
|
|
|
LLVMAttribute PA) {
|
2008-04-29 01:37:06 +08:00
|
|
|
CallSite Call = CallSite(unwrap<Instruction>(Instr));
|
2012-10-16 04:35:56 +08:00
|
|
|
AttrBuilder B(PA);
|
2013-01-23 08:45:55 +08:00
|
|
|
Call.setAttributes(Call.getAttributes()
|
|
|
|
.removeAttributes(Call->getContext(), index,
|
|
|
|
AttributeSet::get(Call->getContext(),
|
|
|
|
index, B)));
|
2008-04-29 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
2013-09-19 07:31:10 +08:00
|
|
|
void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
|
2008-04-29 01:37:06 +08:00
|
|
|
unsigned align) {
|
|
|
|
CallSite Call = CallSite(unwrap<Instruction>(Instr));
|
2012-10-16 04:35:56 +08:00
|
|
|
AttrBuilder B;
|
2012-10-14 11:58:29 +08:00
|
|
|
B.addAlignmentAttr(align);
|
2013-01-23 05:15:51 +08:00
|
|
|
Call.setAttributes(Call.getAttributes()
|
|
|
|
.addAttributes(Call->getContext(), index,
|
|
|
|
AttributeSet::get(Call->getContext(),
|
|
|
|
index, B)));
|
2008-04-29 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
2016-02-10 08:09:37 +08:00
|
|
|
LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
|
|
|
|
return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue());
|
|
|
|
}
|
|
|
|
|
2008-08-31 00:34:54 +08:00
|
|
|
/*--.. Operations on call instructions (only) ..............................--*/
|
|
|
|
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
|
2008-08-31 00:34:54 +08:00
|
|
|
return unwrap<CallInst>(Call)->isTailCall();
|
|
|
|
}
|
|
|
|
|
2010-01-10 06:27:07 +08:00
|
|
|
void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
|
2008-08-31 00:34:54 +08:00
|
|
|
unwrap<CallInst>(Call)->setTailCall(isTailCall);
|
|
|
|
}
|
|
|
|
|
2016-02-19 04:38:32 +08:00
|
|
|
/*--.. Operations on invoke instructions (only) ............................--*/
|
|
|
|
|
|
|
|
LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
|
|
|
|
return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
|
|
|
|
return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
|
|
|
|
unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
|
|
|
|
unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
|
|
|
|
}
|
|
|
|
|
2014-10-29 03:46:56 +08:00
|
|
|
/*--.. Operations on terminators ...........................................--*/
|
|
|
|
|
|
|
|
unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
|
|
|
|
return unwrap<TerminatorInst>(Term)->getNumSuccessors();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
|
|
|
|
return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
|
|
|
|
return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--.. Operations on branch instructions (only) ............................--*/
|
|
|
|
|
|
|
|
LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
|
|
|
|
return unwrap<BranchInst>(Branch)->isConditional();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
|
|
|
|
return wrap(unwrap<BranchInst>(Branch)->getCondition());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
|
|
|
|
return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
|
|
|
|
}
|
|
|
|
|
2011-08-24 04:27:46 +08:00
|
|
|
/*--.. Operations on switch instructions (only) ............................--*/
|
|
|
|
|
|
|
|
LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
|
|
|
|
return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
|
|
|
|
}
|
|
|
|
|
2016-02-10 06:50:53 +08:00
|
|
|
/*--.. Operations on alloca instructions (only) ............................--*/
|
|
|
|
|
|
|
|
LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
|
|
|
|
return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
|
|
|
|
}
|
|
|
|
|
2016-02-18 06:51:03 +08:00
|
|
|
/*--.. Operations on gep instructions (only) ...............................--*/
|
|
|
|
|
|
|
|
LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
|
|
|
|
return unwrap<GetElementPtrInst>(GEP)->isInBounds();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool b) {
|
|
|
|
return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(b);
|
|
|
|
}
|
|
|
|
|
2007-10-09 02:14:39 +08:00
|
|
|
/*--.. Operations on phi nodes .............................................--*/
|
|
|
|
|
|
|
|
void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
|
|
|
|
LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
|
|
|
|
PHINode *PhiVal = unwrap<PHINode>(PhiNode);
|
|
|
|
for (unsigned I = 0; I != Count; ++I)
|
|
|
|
PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
|
|
|
|
return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
|
|
|
|
return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
|
|
|
|
return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
|
|
|
|
}
|
|
|
|
|
2016-02-10 08:38:50 +08:00
|
|
|
/*--.. Operations on extractvalue and insertvalue nodes ....................--*/
|
|
|
|
|
|
|
|
unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
|
|
|
|
auto *I = unwrap(Inst);
|
2016-02-18 06:51:03 +08:00
|
|
|
if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
|
|
|
|
return GEP->getNumIndices();
|
2016-02-10 08:38:50 +08:00
|
|
|
if (auto *EV = dyn_cast<ExtractValueInst>(I))
|
|
|
|
return EV->getNumIndices();
|
|
|
|
if (auto *IV = dyn_cast<InsertValueInst>(I))
|
|
|
|
return IV->getNumIndices();
|
|
|
|
llvm_unreachable(
|
|
|
|
"LLVMGetNumIndices applies only to extractvalue and insertvalue!");
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
|
|
|
|
auto *I = unwrap(Inst);
|
|
|
|
if (auto *EV = dyn_cast<ExtractValueInst>(I))
|
|
|
|
return EV->getIndices().data();
|
|
|
|
if (auto *IV = dyn_cast<InsertValueInst>(I))
|
|
|
|
return IV->getIndices().data();
|
|
|
|
llvm_unreachable(
|
|
|
|
"LLVMGetIndices applies only to extractvalue and insertvalue!");
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
|
|
|
|
/*===-- Instruction builders ----------------------------------------------===*/
|
|
|
|
|
2009-08-14 08:01:31 +08:00
|
|
|
LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
|
|
|
|
return wrap(new IRBuilder<>(*unwrap(C)));
|
|
|
|
}
|
|
|
|
|
2008-05-04 20:55:34 +08:00
|
|
|
LLVMBuilderRef LLVMCreateBuilder(void) {
|
2009-08-14 08:01:31 +08:00
|
|
|
return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
|
2007-09-27 04:56:12 +08:00
|
|
|
}
|
|
|
|
|
2008-03-19 11:47:18 +08:00
|
|
|
void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
|
|
|
|
LLVMValueRef Instr) {
|
|
|
|
BasicBlock *BB = unwrap(Block);
|
|
|
|
Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
|
2015-10-09 07:49:46 +08:00
|
|
|
unwrap(Builder)->SetInsertPoint(BB, I->getIterator());
|
2008-03-19 11:47:18 +08:00
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
|
|
|
|
Instruction *I = unwrap<Instruction>(Instr);
|
2015-10-09 07:49:46 +08:00
|
|
|
unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
|
2007-09-27 04:56:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
|
|
|
|
BasicBlock *BB = unwrap(Block);
|
|
|
|
unwrap(Builder)->SetInsertPoint(BB);
|
|
|
|
}
|
|
|
|
|
2008-03-19 09:11:35 +08:00
|
|
|
LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
|
|
|
|
return wrap(unwrap(Builder)->GetInsertBlock());
|
|
|
|
}
|
|
|
|
|
2008-12-18 05:39:50 +08:00
|
|
|
void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
|
2010-04-01 14:31:45 +08:00
|
|
|
unwrap(Builder)->ClearInsertionPoint();
|
2008-12-18 05:39:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
|
|
|
|
unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
|
|
|
|
}
|
|
|
|
|
2009-08-16 10:20:57 +08:00
|
|
|
void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
|
|
|
|
const char *Name) {
|
|
|
|
unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
|
|
|
|
delete unwrap(Builder);
|
|
|
|
}
|
|
|
|
|
2010-02-28 17:45:59 +08:00
|
|
|
/*--.. Metadata builders ...................................................--*/
|
|
|
|
|
|
|
|
void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
MDNode *Loc =
|
|
|
|
L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
|
2015-03-31 03:40:05 +08:00
|
|
|
unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
|
2010-02-28 17:45:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
LLVMContext &Context = unwrap(Builder)->getContext();
|
|
|
|
return wrap(MetadataAsValue::get(
|
2015-03-31 03:40:05 +08:00
|
|
|
Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
|
2010-02-28 17:45:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
|
|
|
|
unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
/*--.. Instruction builders ................................................--*/
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
|
|
|
|
return wrap(unwrap(B)->CreateRetVoid());
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
|
|
|
|
return wrap(unwrap(B)->CreateRet(unwrap(V)));
|
|
|
|
}
|
|
|
|
|
2009-08-16 10:19:59 +08:00
|
|
|
LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
|
|
|
|
unsigned N) {
|
|
|
|
return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
|
|
|
|
return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
|
|
|
|
LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
|
|
|
|
return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
|
|
|
|
LLVMBasicBlockRef Else, unsigned NumCases) {
|
|
|
|
return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
|
|
|
|
}
|
|
|
|
|
2010-02-28 17:46:06 +08:00
|
|
|
LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
|
|
|
|
unsigned NumDests) {
|
|
|
|
return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
|
|
|
|
LLVMValueRef *Args, unsigned NumArgs,
|
|
|
|
LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
|
2011-07-18 20:00:32 +08:00
|
|
|
makeArrayRef(unwrap(Args), NumArgs),
|
2007-09-27 04:56:12 +08:00
|
|
|
Name));
|
|
|
|
}
|
|
|
|
|
2011-08-13 04:24:12 +08:00
|
|
|
LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
|
2015-07-16 09:16:39 +08:00
|
|
|
LLVMValueRef PersFn, unsigned NumClauses,
|
|
|
|
const char *Name) {
|
|
|
|
// The personality used to live on the landingpad instruction, but now it
|
|
|
|
// lives on the parent function. For compatibility, take the provided
|
|
|
|
// personality and put it on the parent function.
|
|
|
|
if (PersFn)
|
|
|
|
unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
|
|
|
|
cast<Function>(unwrap(PersFn)));
|
2015-06-18 04:52:32 +08:00
|
|
|
return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
|
2011-08-13 04:24:12 +08:00
|
|
|
}
|
|
|
|
|
2011-07-31 14:30:59 +08:00
|
|
|
LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
|
|
|
|
return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
|
|
|
|
return wrap(unwrap(B)->CreateUnreachable());
|
|
|
|
}
|
|
|
|
|
2008-01-01 13:50:53 +08:00
|
|
|
void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
|
|
|
|
LLVMBasicBlockRef Dest) {
|
|
|
|
unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
|
|
|
|
}
|
|
|
|
|
2010-02-28 17:46:06 +08:00
|
|
|
void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
|
|
|
|
unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
|
|
|
|
}
|
|
|
|
|
2016-02-19 04:38:32 +08:00
|
|
|
unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
|
|
|
|
return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
|
|
|
|
return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
|
|
|
|
}
|
|
|
|
|
2011-08-13 04:24:12 +08:00
|
|
|
void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
|
|
|
|
unwrap<LandingPadInst>(LandingPad)->
|
|
|
|
addClause(cast<Constant>(unwrap(ClauseVal)));
|
|
|
|
}
|
|
|
|
|
2016-02-19 04:38:32 +08:00
|
|
|
LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
|
|
|
|
return unwrap<LandingPadInst>(LandingPad)->isCleanup();
|
|
|
|
}
|
|
|
|
|
2011-08-13 04:24:12 +08:00
|
|
|
void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
|
|
|
|
unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
/*--.. Arithmetic ..........................................................--*/
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
2009-08-16 10:19:59 +08:00
|
|
|
LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
2010-02-28 13:51:43 +08:00
|
|
|
LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
2009-08-16 10:19:59 +08:00
|
|
|
LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
2010-02-28 13:51:43 +08:00
|
|
|
LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
2009-08-16 10:19:59 +08:00
|
|
|
LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
2010-02-28 13:51:43 +08:00
|
|
|
LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
2009-08-16 10:19:59 +08:00
|
|
|
LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
2009-08-16 10:19:59 +08:00
|
|
|
LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
|
|
|
|
LLVMValueRef RHS, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
2010-02-28 13:51:27 +08:00
|
|
|
LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
|
|
|
|
LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
2011-10-06 20:39:34 +08:00
|
|
|
return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
|
2010-02-28 13:51:27 +08:00
|
|
|
unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
|
|
|
|
}
|
|
|
|
|
2010-02-28 13:51:43 +08:00
|
|
|
LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
|
|
|
|
}
|
|
|
|
|
2009-09-29 05:51:41 +08:00
|
|
|
LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--.. Memory ..............................................................--*/
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
|
|
|
|
const char *Name) {
|
2011-07-18 12:54:35 +08:00
|
|
|
Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
|
2009-11-07 08:16:28 +08:00
|
|
|
Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
|
|
|
|
AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
|
2013-09-19 07:31:10 +08:00
|
|
|
Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
|
|
|
|
ITy, unwrap(Ty), AllocSize,
|
2014-04-09 14:08:46 +08:00
|
|
|
nullptr, nullptr, "");
|
2009-11-07 08:16:28 +08:00
|
|
|
return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
|
2007-09-27 04:56:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
|
|
|
|
LLVMValueRef Val, const char *Name) {
|
2011-07-18 12:54:35 +08:00
|
|
|
Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
|
2009-11-07 08:16:28 +08:00
|
|
|
Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
|
|
|
|
AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
|
2013-09-19 07:31:10 +08:00
|
|
|
Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
|
|
|
|
ITy, unwrap(Ty), AllocSize,
|
2014-04-09 14:08:46 +08:00
|
|
|
unwrap(Val), nullptr, "");
|
2009-11-07 08:16:28 +08:00
|
|
|
return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
|
2007-09-27 04:56:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
|
|
|
|
const char *Name) {
|
2014-04-09 14:08:46 +08:00
|
|
|
return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
|
2007-09-27 04:56:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
|
|
|
|
LLVMValueRef Val, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
|
2009-10-27 07:43:48 +08:00
|
|
|
return wrap(unwrap(B)->Insert(
|
|
|
|
CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
|
2007-09-27 04:56:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
|
|
|
|
}
|
|
|
|
|
2013-09-19 07:31:10 +08:00
|
|
|
LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
|
2007-09-27 04:56:12 +08:00
|
|
|
LLVMValueRef PointerVal) {
|
|
|
|
return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
|
|
|
|
}
|
|
|
|
|
2013-11-20 08:07:49 +08:00
|
|
|
static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
|
|
|
|
switch (Ordering) {
|
|
|
|
case LLVMAtomicOrderingNotAtomic: return NotAtomic;
|
|
|
|
case LLVMAtomicOrderingUnordered: return Unordered;
|
|
|
|
case LLVMAtomicOrderingMonotonic: return Monotonic;
|
|
|
|
case LLVMAtomicOrderingAcquire: return Acquire;
|
|
|
|
case LLVMAtomicOrderingRelease: return Release;
|
|
|
|
case LLVMAtomicOrderingAcquireRelease: return AcquireRelease;
|
|
|
|
case LLVMAtomicOrderingSequentiallyConsistent:
|
|
|
|
return SequentiallyConsistent;
|
|
|
|
}
|
2014-10-29 03:46:44 +08:00
|
|
|
|
2013-11-20 08:07:49 +08:00
|
|
|
llvm_unreachable("Invalid LLVMAtomicOrdering value!");
|
|
|
|
}
|
|
|
|
|
2015-08-02 20:16:57 +08:00
|
|
|
static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
|
|
|
|
switch (Ordering) {
|
|
|
|
case NotAtomic: return LLVMAtomicOrderingNotAtomic;
|
|
|
|
case Unordered: return LLVMAtomicOrderingUnordered;
|
|
|
|
case Monotonic: return LLVMAtomicOrderingMonotonic;
|
|
|
|
case Acquire: return LLVMAtomicOrderingAcquire;
|
|
|
|
case Release: return LLVMAtomicOrderingRelease;
|
|
|
|
case AcquireRelease: return LLVMAtomicOrderingAcquireRelease;
|
|
|
|
case SequentiallyConsistent:
|
|
|
|
return LLVMAtomicOrderingSequentiallyConsistent;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Invalid AtomicOrdering value!");
|
|
|
|
}
|
|
|
|
|
2013-11-20 08:07:49 +08:00
|
|
|
LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
|
|
|
|
LLVMBool isSingleThread, const char *Name) {
|
|
|
|
return wrap(
|
|
|
|
unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
|
|
|
|
isSingleThread ? SingleThread : CrossThread,
|
|
|
|
Name));
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
|
|
|
|
LLVMValueRef *Indices, unsigned NumIndices,
|
|
|
|
const char *Name) {
|
2011-07-22 16:16:57 +08:00
|
|
|
ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
|
2015-03-15 09:03:19 +08:00
|
|
|
return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name));
|
2007-09-27 04:56:12 +08:00
|
|
|
}
|
|
|
|
|
2009-08-16 10:19:59 +08:00
|
|
|
LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
|
|
|
|
LLVMValueRef *Indices, unsigned NumIndices,
|
|
|
|
const char *Name) {
|
2011-07-22 16:16:57 +08:00
|
|
|
ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
|
2015-04-04 05:33:42 +08:00
|
|
|
return wrap(
|
|
|
|
unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name));
|
2009-08-16 10:19:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
|
|
|
|
unsigned Idx, const char *Name) {
|
2015-04-06 06:41:44 +08:00
|
|
|
return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name));
|
2009-08-16 10:19:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateGlobalString(Str, Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
|
|
|
|
}
|
|
|
|
|
2012-03-22 11:54:15 +08:00
|
|
|
LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
|
|
|
|
Value *P = unwrap<Value>(MemAccessInst);
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(P))
|
|
|
|
return LI->isVolatile();
|
|
|
|
return cast<StoreInst>(P)->isVolatile();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
|
|
|
|
Value *P = unwrap<Value>(MemAccessInst);
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(P))
|
|
|
|
return LI->setVolatile(isVolatile);
|
|
|
|
return cast<StoreInst>(P)->setVolatile(isVolatile);
|
|
|
|
}
|
|
|
|
|
2015-08-02 20:16:57 +08:00
|
|
|
LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
|
|
|
|
Value *P = unwrap<Value>(MemAccessInst);
|
|
|
|
AtomicOrdering O;
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(P))
|
|
|
|
O = LI->getOrdering();
|
|
|
|
else
|
|
|
|
O = cast<StoreInst>(P)->getOrdering();
|
|
|
|
return mapToLLVMOrdering(O);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
|
|
|
|
Value *P = unwrap<Value>(MemAccessInst);
|
|
|
|
AtomicOrdering O = mapFromLLVMOrdering(Ordering);
|
|
|
|
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(P))
|
|
|
|
return LI->setOrdering(O);
|
|
|
|
return cast<StoreInst>(P)->setOrdering(O);
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
/*--.. Casts ...............................................................--*/
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
|
|
|
|
}
|
|
|
|
|
2013-11-15 09:34:59 +08:00
|
|
|
LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
|
|
|
|
}
|
|
|
|
|
2009-08-16 10:19:59 +08:00
|
|
|
LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
|
|
|
|
Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
|
|
|
|
Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
|
|
|
|
Name));
|
|
|
|
}
|
|
|
|
|
2010-02-28 13:51:27 +08:00
|
|
|
LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
|
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
2011-10-06 20:39:34 +08:00
|
|
|
return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
|
2010-02-28 13:51:27 +08:00
|
|
|
unwrap(DestTy), Name));
|
|
|
|
}
|
|
|
|
|
2009-08-16 10:19:59 +08:00
|
|
|
LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
|
2009-11-23 18:49:03 +08:00
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
|
|
|
|
/*isSigned*/true, Name));
|
2009-08-16 10:19:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
LLVMTypeRef DestTy, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
|
|
|
|
}
|
|
|
|
|
2007-09-27 04:56:12 +08:00
|
|
|
/*--.. Comparisons .........................................................--*/
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
|
|
|
|
LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
|
|
|
|
unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
|
|
|
|
LLVMValueRef LHS, LLVMValueRef RHS,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
|
|
|
|
unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--.. Miscellaneous instructions ..........................................--*/
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
|
2011-03-30 19:28:46 +08:00
|
|
|
return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
|
2007-09-27 04:56:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
|
|
|
|
LLVMValueRef *Args, unsigned NumArgs,
|
|
|
|
const char *Name) {
|
2011-07-15 16:37:34 +08:00
|
|
|
return wrap(unwrap(B)->CreateCall(unwrap(Fn),
|
2011-07-18 20:00:32 +08:00
|
|
|
makeArrayRef(unwrap(Args), NumArgs),
|
2011-07-15 16:37:34 +08:00
|
|
|
Name));
|
2007-09-27 04:56:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
|
|
|
|
LLVMValueRef Then, LLVMValueRef Else,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
|
|
|
|
Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
|
|
|
|
LLVMTypeRef Ty, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
|
|
|
|
LLVMValueRef Index, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
|
|
|
|
Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
|
|
|
|
LLVMValueRef EltVal, LLVMValueRef Index,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
|
|
|
|
unwrap(Index), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
|
|
|
|
LLVMValueRef V2, LLVMValueRef Mask,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
|
|
|
|
unwrap(Mask), Name));
|
|
|
|
}
|
2007-12-12 09:04:30 +08:00
|
|
|
|
2008-11-04 06:55:43 +08:00
|
|
|
LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
|
|
|
|
unsigned Index, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
|
|
|
|
LLVMValueRef EltVal, unsigned Index,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
|
|
|
|
Index, Name));
|
|
|
|
}
|
|
|
|
|
2009-08-16 10:19:59 +08:00
|
|
|
LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
|
|
|
|
LLVMValueRef RHS, const char *Name) {
|
|
|
|
return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
|
|
|
|
}
|
|
|
|
|
2013-09-19 07:31:10 +08:00
|
|
|
LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
|
|
|
|
LLVMValueRef PTR, LLVMValueRef Val,
|
|
|
|
LLVMAtomicOrdering ordering,
|
2013-04-23 21:21:19 +08:00
|
|
|
LLVMBool singleThread) {
|
|
|
|
AtomicRMWInst::BinOp intop;
|
|
|
|
switch (op) {
|
|
|
|
case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
|
|
|
|
case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
|
|
|
|
case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
|
|
|
|
case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
|
|
|
|
case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
|
|
|
|
case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
|
|
|
|
case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
|
|
|
|
case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
|
|
|
|
case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
|
|
|
|
case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
|
|
|
|
case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
|
|
|
|
}
|
2013-09-19 07:31:10 +08:00
|
|
|
return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
|
2013-11-20 08:07:49 +08:00
|
|
|
mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
|
2013-04-23 21:21:19 +08:00
|
|
|
}
|
|
|
|
|
2016-03-20 05:28:28 +08:00
|
|
|
LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
|
|
|
|
LLVMValueRef Cmp, LLVMValueRef New,
|
|
|
|
LLVMAtomicOrdering SuccessOrdering,
|
|
|
|
LLVMAtomicOrdering FailureOrdering,
|
|
|
|
LLVMBool singleThread) {
|
|
|
|
|
|
|
|
return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
|
|
|
|
unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
|
|
|
|
mapFromLLVMOrdering(FailureOrdering),
|
|
|
|
singleThread ? SingleThread : CrossThread));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
|
|
|
|
Value *P = unwrap<Value>(AtomicInst);
|
|
|
|
|
|
|
|
if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
|
|
|
|
return I->getSynchScope() == SingleThread;
|
|
|
|
return cast<AtomicCmpXchgInst>(P)->getSynchScope() == SingleThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
|
|
|
|
Value *P = unwrap<Value>(AtomicInst);
|
|
|
|
SynchronizationScope Sync = NewValue ? SingleThread : CrossThread;
|
|
|
|
|
|
|
|
if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
|
|
|
|
return I->setSynchScope(Sync);
|
|
|
|
return cast<AtomicCmpXchgInst>(P)->setSynchScope(Sync);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
|
|
|
|
Value *P = unwrap<Value>(CmpXchgInst);
|
|
|
|
return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
|
|
|
|
LLVMAtomicOrdering Ordering) {
|
|
|
|
Value *P = unwrap<Value>(CmpXchgInst);
|
|
|
|
AtomicOrdering O = mapFromLLVMOrdering(Ordering);
|
|
|
|
|
|
|
|
return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
|
|
|
|
Value *P = unwrap<Value>(CmpXchgInst);
|
|
|
|
return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
|
|
|
|
LLVMAtomicOrdering Ordering) {
|
|
|
|
Value *P = unwrap<Value>(CmpXchgInst);
|
|
|
|
AtomicOrdering O = mapFromLLVMOrdering(Ordering);
|
|
|
|
|
|
|
|
return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
|
|
|
|
}
|
2007-12-12 09:04:30 +08:00
|
|
|
|
|
|
|
/*===-- Module providers --------------------------------------------------===*/
|
|
|
|
|
|
|
|
LLVMModuleProviderRef
|
|
|
|
LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
|
2010-01-28 04:34:15 +08:00
|
|
|
return reinterpret_cast<LLVMModuleProviderRef>(M);
|
2007-12-12 09:04:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
|
|
|
|
delete unwrap(MP);
|
|
|
|
}
|
|
|
|
|
2007-12-20 06:30:40 +08:00
|
|
|
|
|
|
|
/*===-- Memory buffers ----------------------------------------------------===*/
|
|
|
|
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
|
|
|
|
const char *Path,
|
|
|
|
LLVMMemoryBufferRef *OutMemBuf,
|
|
|
|
char **OutMessage) {
|
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
|
|
|
|
if (std::error_code EC = MBOrErr.getError()) {
|
|
|
|
*OutMessage = strdup(EC.message().c_str());
|
|
|
|
return 1;
|
2007-12-20 06:30:40 +08:00
|
|
|
}
|
2014-07-07 01:43:13 +08:00
|
|
|
*OutMemBuf = wrap(MBOrErr.get().release());
|
|
|
|
return 0;
|
2007-12-20 06:30:40 +08:00
|
|
|
}
|
|
|
|
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
|
|
|
|
char **OutMessage) {
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
|
|
|
|
if (std::error_code EC = MBOrErr.getError()) {
|
|
|
|
*OutMessage = strdup(EC.message().c_str());
|
|
|
|
return 1;
|
2007-12-20 06:30:40 +08:00
|
|
|
}
|
2014-07-07 01:43:13 +08:00
|
|
|
*OutMemBuf = wrap(MBOrErr.get().release());
|
|
|
|
return 0;
|
2007-12-20 06:30:40 +08:00
|
|
|
}
|
|
|
|
|
2013-02-15 03:11:28 +08:00
|
|
|
LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
|
|
|
|
const char *InputData,
|
|
|
|
size_t InputDataLength,
|
|
|
|
const char *BufferName,
|
2013-02-15 03:40:27 +08:00
|
|
|
LLVMBool RequiresNullTerminator) {
|
2013-02-15 03:11:28 +08:00
|
|
|
|
2014-08-28 04:03:13 +08:00
|
|
|
return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
|
|
|
|
StringRef(BufferName),
|
|
|
|
RequiresNullTerminator).release());
|
2013-02-15 03:11:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
|
|
|
|
const char *InputData,
|
|
|
|
size_t InputDataLength,
|
|
|
|
const char *BufferName) {
|
|
|
|
|
2014-08-28 04:03:13 +08:00
|
|
|
return wrap(
|
|
|
|
MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
|
|
|
|
StringRef(BufferName)).release());
|
2013-02-15 03:11:28 +08:00
|
|
|
}
|
|
|
|
|
2013-04-19 03:50:53 +08:00
|
|
|
const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
|
2013-04-17 07:12:47 +08:00
|
|
|
return unwrap(MemBuf)->getBufferStart();
|
|
|
|
}
|
2013-02-15 03:11:28 +08:00
|
|
|
|
2013-04-17 07:12:51 +08:00
|
|
|
size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
|
|
|
|
return unwrap(MemBuf)->getBufferSize();
|
|
|
|
}
|
|
|
|
|
2007-12-20 06:30:40 +08:00
|
|
|
void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
|
|
|
|
delete unwrap(MemBuf);
|
|
|
|
}
|
2010-08-07 08:43:20 +08:00
|
|
|
|
2010-10-08 01:55:47 +08:00
|
|
|
/*===-- Pass Registry -----------------------------------------------------===*/
|
|
|
|
|
|
|
|
LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
|
|
|
|
return wrap(PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-08-07 08:43:20 +08:00
|
|
|
|
|
|
|
/*===-- Pass Manager ------------------------------------------------------===*/
|
|
|
|
|
|
|
|
LLVMPassManagerRef LLVMCreatePassManager() {
|
2015-02-13 18:01:29 +08:00
|
|
|
return wrap(new legacy::PassManager());
|
2010-08-07 08:43:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
|
2015-02-13 18:01:29 +08:00
|
|
|
return wrap(new legacy::FunctionPassManager(unwrap(M)));
|
2010-08-07 08:43:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
|
|
|
|
return LLVMCreateFunctionPassManagerForModule(
|
|
|
|
reinterpret_cast<LLVMModuleRef>(P));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
|
2015-02-13 18:01:29 +08:00
|
|
|
return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
|
2010-08-07 08:43:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
|
2015-02-13 18:01:29 +08:00
|
|
|
return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
|
2010-08-07 08:43:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
|
2015-02-13 18:01:29 +08:00
|
|
|
return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
|
2010-08-07 08:43:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
|
2015-02-13 18:01:29 +08:00
|
|
|
return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
|
2010-08-07 08:43:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposePassManager(LLVMPassManagerRef PM) {
|
|
|
|
delete unwrap(PM);
|
|
|
|
}
|
2013-02-18 00:35:51 +08:00
|
|
|
|
|
|
|
/*===-- Threading ------------------------------------------------------===*/
|
|
|
|
|
|
|
|
LLVMBool LLVMStartMultithreaded() {
|
2014-06-27 23:13:01 +08:00
|
|
|
return LLVMIsMultithreaded();
|
2013-02-18 00:35:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMStopMultithreaded() {
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMIsMultithreaded() {
|
|
|
|
return llvm_is_multithreaded();
|
|
|
|
}
|