Index: include/llvm-c/Core.h =================================================================== --- include/llvm-c/Core.h +++ include/llvm-c/Core.h @@ -308,6 +308,34 @@ } LLVMCallConv; typedef enum { + LLVMArgumentValueKind, + LLVMBasicBlockValueKind, + LLVMInlineAsmValueKind, + LLVMMDNodeValueKind, + LLVMMDStringValueKind, + LLVMUserValueKind, + LLVMConstantValueKind, + LLVMBlockAddressValueKind, + LLVMConstantAggregateZeroValueKind, + LLVMConstantArrayValueKind, + LLVMConstantDataSequentialValueKind, + LLVMConstantDataArrayValueKind, + LLVMConstantDataVectorValueKind, + LLVMConstantExprValueKind, + LLVMConstantFPValueKind, + LLVMConstantIntValueKind, + LLVMConstantPointerNullValueKind, + LLVMConstantStructValueKind, + LLVMConstantVectorValueKind, + LLVMGlobalValueValueKind, + LLVMFunctionValueKind, + LLVMGlobalAliasValueKind, + LLVMGlobalVariableValueKind, + LLVMUndefValueValueKind, + LLVMInstructionValueKind +} LLVMValueKind; + +typedef enum { LLVMIntEQ = 32, /**< equal */ LLVMIntNE, /**< not equal */ LLVMIntUGT, /**< unsigned greater than */ @@ -401,6 +429,11 @@ the old one */ } LLVMAtomicRMWBinOp; +typedef enum { + LLVMSynchronizationScopeSingleThread, + LLVMSynchronizationScopeCrossThread +} LLVMSynchronizationScope; + /** * @} */ @@ -522,6 +555,19 @@ void LLVMDisposeModule(LLVMModuleRef M); /** + * Obtain the identifier of a module. The result must be discarded with + * LLVMDisposeMessage. + * @see Module::getModuleIdentifier() + */ +char* LLVMGetModuleIdentifier(LLVMModuleRef M); + +/** + * Set the identifier of a module. + * @see Module::setModuleIdentifier() + */ +void LLVMSetModuleIdentifier(LLVMModuleRef M, const char* ID); + +/** * Obtain the data layout for a module. * * @see Module::getDataLayout() @@ -1060,6 +1106,11 @@ LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C); /** + * Create a metadata type in a context. + */ +LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C); + +/** * Create a X86 MMX type in a context. */ LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C); @@ -1070,6 +1121,7 @@ */ LLVMTypeRef LLVMVoidType(void); LLVMTypeRef LLVMLabelType(void); +LLVMTypeRef LLVMMetadataType(void); LLVMTypeRef LLVMX86MMXType(void); /** @@ -1101,6 +1153,29 @@ * @{ */ +#define LLVM_FOR_EACH_VALUE_KIND(macro) \ + macro(Argument) \ + macro(BasicBlock) \ + macro(InlineAsm) \ + macro(MDNode) \ + macro(MDString) \ + macro(BlockAddress) \ + macro(ConstantAggregateZero) \ + macro(ConstantArray) \ + macro(ConstantDataArray) \ + macro(ConstantDataVector) \ + macro(ConstantExpr) \ + macro(ConstantFP) \ + macro(ConstantInt) \ + macro(ConstantPointerNull) \ + macro(ConstantStruct) \ + macro(ConstantVector) \ + macro(Function) \ + macro(GlobalAlias) \ + macro(GlobalVariable) \ + macro(UndefValue) \ + macro(Instruction) + #define LLVM_FOR_EACH_VALUE_SUBCLASS(macro) \ macro(Argument) \ macro(BasicBlock) \ @@ -1193,6 +1268,13 @@ LLVMTypeRef LLVMTypeOf(LLVMValueRef Val); /** + * Obtain the enumerated type of a Value instance. + * + * @see llvm::Type:getValueID() + */ +LLVMValueKind LLVMGetValueKind(LLVMValueRef Val); + +/** * Obtain the string name of a value. * * @see llvm::Value::getName() @@ -1455,6 +1537,30 @@ unsigned SLen, uint8_t Radix); /** + * Obtain the zero extended value for an integer constant value. + * + * @see llvm::ConstantInt::getZExtValue() + */ +unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal); + +/** + * Obtain the sign extended value for an integer constant value. + * + * @see llvm::ConstantInt::getSExtValue() + */ +long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal); + +/** + * Obtain the words of an integer constant value. \p n should point to + * a variable which will hold the amount of words. The return value + * points to an array of \p n words, owned by the context. The caller + * must not capture pointer to the array. + * + * @see llvm::ConstantInt::getValue() + */ +const uint64_t *LLVMConstIntGetWords(LLVMValueRef v, unsigned *n); + +/** * Obtain a constant value referring to a double floating point value. */ LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N); @@ -1474,18 +1580,30 @@ unsigned SLen); /** - * Obtain the zero extended value for an integer constant value. + * Obtain a constant value for a floating point value of arbitrary precision. * - * @see llvm::ConstantInt::getZExtValue() + * @see llvm::ConstantFP::get() */ -unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal); +LLVMValueRef LLVMConstRealOfArbitraryPrecision(LLVMTypeRef RealTy, + unsigned NumWords, + const uint64_t Words[]); /** - * Obtain the sign extended value for an integer constant value. + * Obtain the double precision value for a floating point constant value. * - * @see llvm::ConstantInt::getSExtValue() + * @see llvm::APFloat::convertToDouble() */ -long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal); +double LLVMConstRealGetDouble(LLVMValueRef ConstantVal); + +/** + * Obtain the words of a floating point constant value. \p n should point to + * a variable which will hold the amount of words. The return value + * points to an array of \p n words, owned by the context. The caller + * must not capture pointer to the array. + * + * @see llvm::ConstantFP::getValueAPF() + */ +const uint64_t *LLVMConstRealGetWords(LLVMValueRef v, unsigned *n); /** * @} @@ -1577,6 +1695,7 @@ * @{ */ LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal); +LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef ConstantVal, unsigned n); LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty); LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty); LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal); @@ -1663,6 +1782,18 @@ LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB); /** + * Obtain the function of a blockaddress expression. + * @see llvm::BlockAddress::getFunction() + */ +LLVMValueRef LLVMGetBlockAddressFunction(LLVMValueRef BA); + +/** + * Obtain the basic block of a blockaddress expression. + * @see llvm::BlockAddress::getBasicBlock() + */ +LLVMBasicBlockRef LLVMGetBlockAddressBlock(LLVMValueRef BA); + +/** * @} */ @@ -1687,10 +1818,27 @@ void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz); /** + * Obtain whether the address of a global is not important. + * @see llvm::GlobalValue::hasUnnamedAddr() + */ +LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global); + +/** + * Set whether the address of a global is not important. + * @see llvm::GlobalValue::setUnnamedAddr() + */ +void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr); + +/** + * @} + */ + +/** * @defgroup LLVMCCoreValueWithAlignment Values with alignment * * Functions in this group only apply to values with alignment, i.e. * global variables, load and store instructions. + * @{ */ /** @@ -1758,6 +1906,26 @@ */ LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee, const char *Name); +LLVMValueRef LLVMGetNamedAlias(LLVMModuleRef M, const char *Name); +LLVMValueRef LLVMGetFirstAlias(LLVMModuleRef M); +LLVMValueRef LLVMGetLastAlias(LLVMModuleRef M); +LLVMValueRef LLVMGetNextAlias(LLVMValueRef Alias); +LLVMValueRef LLVMGetPreviousAlias(LLVMValueRef Alias); +void LLVMDeleteAlias(LLVMValueRef GlobalVar); + +/** + * Get the aliased value of an alias. The value can be a constant or a constant + * bitcast. + * @see llvm::GlobalAlias::getAliasee() + */ +LLVMValueRef LLVMGetAliasee(LLVMValueRef A); + +/** + * Set the aliased value of an alias. The value can be a constant or a constant + * bitcast. + * @see llvm::GlobalAlias::setAliasee() + */ +void LLVMSetAliasee(LLVMValueRef A, LLVMValueRef Aliasee); /** * @} @@ -2320,6 +2488,211 @@ LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst); /** + * Obtain the predicate of an instruction. + * + * This is only valid for instructions that correspond to llvm::FCmpInst + * or llvm::ConstantExpr whose opcode is llvm::Instruction::FCmp. + * + * @see llvm::FCmpInst::getPredicate() + */ +LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst); + +/** + * @defgroup LLVMCCoreValueInstructionGEP GetElementPtr + * + * Functions in this group apply to instructions that refer to getelementptr + * instructions or constant expressions. These correspond to C++ types in the + * llvm::GetElementPtrInst or llvm::GetElementPtrConstantExpr classes. + * + * @{ + */ + +/** + * Obtain whether a getelementptr is "inbounds". + * + * This is only valid for instructions that correspond to + * llvm::GetElementPtrInst or llvm::GetElementPtrConstantExpr. + * + * @see llvm::GetElementPtrInst::isInBounds() + */ +LLVMBool LLVMIsInBounds(LLVMValueRef Inst); + +/** + * Set whether a getelementptr is "inbounds". + * + * This is only valid for instructions that correspond to + * llvm::GetElementPtrInst or llvm::GetElementPtrConstantExpr. + * + * @see llvm::GetElementPtrInst::setIsInBounds() + */ +void LLVMSetIsInBounds(LLVMValueRef Inst, LLVMBool IsInBounds); + +/** + * @} + */ + +/** + * @defgroup LLVMCCoreValueInstructionBinOp Binary Operators + * + * Functions in this group apply to instructions that refer to binary + * operators. These correspond to C++ types in the llvm::BinaryOperator class + * tree. + * + * @{ + */ + +/** + * Obtain whether the operation cannot result in a signed integer wraparound. + * @see llvm::BinaryOperator::hasNoSignedWrap() + */ +LLVMBool LLVMHasNoSignedWrap(LLVMValueRef Instr); + +/** + * Set whether the operation cannot result in a signed integer wraparound. + * @see llvm::BinaryOperator::hasNoSignedWrap() + */ +void LLVMSetHasNoSignedWrap(LLVMValueRef Instr, LLVMBool HasNoSignedWrap); + +/** + * Obtain whether the operation cannot result in a unsigned integer wraparound. + * @see llvm::BinaryOperator::hasNoUnsignedWrap() + */ +LLVMBool LLVMHasNoUnsignedWrap(LLVMValueRef Instr); + +/** + * Set whether the operation cannot result in a unsigned integer wraparound. + * @see llvm::BinaryOperator::hasNoUnsignedWrap() + */ +void LLVMSetHasNoUnsignedWrap(LLVMValueRef Instr, LLVMBool HasNoSignedWrap); + +/** + * Obtain whether the operation cannot result in an inexact result. + * @see llvm::BinaryOperator::isExact() + */ +LLVMBool LLVMIsExact(LLVMValueRef Instr); + +/** + * Set whether the operation cannot result in an inexact result. + * @see llvm::BinaryOperator::isExact() + */ +void LLVMSetIsExact(LLVMValueRef Instr, LLVMBool IsExact); + +/** + * @} + */ + +/** + * @defgroup LLVMCCoreValueInstructionAlloca Alloca + * + * Functions in this group apply to the alloca instruction. + * @see llvm::AllocaInst + * + * @{ + */ + +/** + * Obtain whether an alloca instruction allocates an array. + * @see llvm::AllocaInst::isArrayAllocation() + */ +LLVMBool LLVMIsArrayAlloca(LLVMValueRef Instr); + +/** + * Obtain the number of elements allocated by an alloca instruction. + * @see llvm::AllocaInst::getArraySize() + */ +LLVMValueRef LLVMGetAllocaNumElements(LLVMValueRef Instr); + +/** + * Obtain the type of elements allocated by an alloca instruction. + * @see llvm::AllocaInst::getAllocatedType() + */ +LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Instr); + +/** + * @} + */ + +/** + * @defgroup LLVMCCoreValueInstructionAtomic Atomic instructions + * + * Functions in this group apply to instructions with atomic forms, that is + * load, store, fence, atomicrmw and cmpxchg. + * @see llvm::AtomicRMWInst + * + * @{ + */ + +/** + * Obtain the atomic ordering of an instruction with an atomic form. + * @see llvm::LoadInst::getAtomicOrdering() + * @see llvm::StoreInst::getAtomicOrdering() + * @see llvm::FenceInst::getAtomicOrdering() + * @see llvm::AtomicRMWInst::getAtomicOrdering() + * @see llvm::AtomicCmpXchgInst::getAtomicOrdering() + */ +LLVMAtomicOrdering LLVMGetAtomicOrdering(LLVMValueRef Instr); + +/** + * Set the atomic ordering of an instruction with an atomic form. + * @see llvm::LoadInst::setAtomicOrdering() + * @see llvm::StoreInst::setAtomicOrdering() + * @see llvm::FenceInst::setAtomicOrdering() + * @see llvm::AtomicRMWInst::setAtomicOrdering() + * @see llvm::AtomicCmpXchgInst::setAtomicOrdering() + */ +void LLVMSetAtomicOrdering(LLVMValueRef Instr, LLVMAtomicOrdering Ordering); + +/** + * Obtain the synchronization scope of an instruction with an atomic form. + * @see llvm::LoadInst::getSynchronizationScope() + * @see llvm::StoreInst::getSynchronizationScope() + * @see llvm::FenceInst::getSynchronizationScope() + * @see llvm::AtomicRMWInst::getSynchronizationScope() + * @see llvm::AtomicCmpXchgInst::getSynchronizationScope() + */ +LLVMSynchronizationScope LLVMGetSynchronizationScope(LLVMValueRef Instr); + +/** + * Set the atomic ordering of an instruction with an atomic form. + * @see llvm::LoadInst::setSynchronizationScope() + * @see llvm::StoreInst::setSynchronizationScope() + * @see llvm::FenceInst::setSynchronizationScope() + * @see llvm::AtomicRMWInst::setSynchronizationScope() + * @see llvm::AtomicCmpXchgInst::setSynchronizationScope() + */ +void LLVMSetSynchronizationScope(LLVMValueRef Instr, + LLVMSynchronizationScope Scope); + +/** + * @} + */ + +/** + * @defgroup LLVMCCoreValueInstructionAtomicRMW AtomicRMW + * + * Functions in this group apply to the atomicrmw instruction. + * @see llvm::AtomicRMWInst + * + * @{ + */ + +/** + * Obtain the operation of an atomicrmw instruction. + * @see llvm::AtomicRMWInst::getOperation() + */ +LLVMAtomicRMWBinOp LLVMGetAtomicRMWOperation(LLVMValueRef Instr); + +/** + * Set the operation of an atomicrmw instruction. + * @see llvm::AtomicRMWInst::setOperation() + */ +void LLVMSetAtomicRMWOperation(LLVMValueRef Instr, LLVMAtomicRMWBinOp Op); + +/** + * @} + */ + +/** * @defgroup LLVMCCoreValueInstructionCall Call Sites and Invocations * * Functions in this group apply to instructions that refer to call @@ -2350,6 +2723,12 @@ */ unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr); +/** + * Obtain the called value for a call instruction. + * @see llvm::CallInst::getCalledValue() + * @see llvm::InvokeInst::getCalledValue() + */ +LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr); void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, LLVMAttribute); void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, @@ -2380,15 +2759,82 @@ */ /** - * Obtain the default destination basic block of a switch instruction. + * @defgroup LLVMCCoreValueInstructionSwitch Switch Instructions + * + * Functions in this group only apply to switch instructions. * - * This only works on llvm::SwitchInst instructions. + * @{ + */ + +/** + * Add a case to a switch instruction. \p OnVal must be a constant integer. + * + * @see llvm::SwitchInst::addCase() + */ +void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal, + LLVMBasicBlockRef Dest); + +/** + * Obtain the default destination basic block of a switch instruction. * * @see llvm::SwitchInst::getDefaultDest() */ LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef SwitchInstr); /** + * Obtain the amount of non-default destinations of a switch instruction. + * + * @see llvm::SwitchInst::getNumCases() + */ +unsigned LLVMGetSwitchNumCases(LLVMValueRef SwitchInstr); + +/** + * Obtain the case values and destinations for all cases of a switch + * instruction. \p Values and \p Successors should point to arrays of + * sufficient size, which can be determined by \p LLVMGetSwitchNumCases. + */ +void LLVMGetSwitchCases(LLVMValueRef SwitchInstr, LLVMValueRef *Values, + LLVMBasicBlockRef *Successors); + +/** + * @} + */ + +/** + * @defgroup LLVMCCoreValueInstructionIndirectBr Indirectbr Instructions + * + * Functions in this group only apply to indirectbr instructions. + * + * @{ + */ + +/** + * Add a destination to the indirectbr instruction. + * + * @see llvm::IndirectBrInst::addDestination() + */ +void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest); + +/** + * Obtain the amount of destinations of an indirectbr instruction. + * + * @see llvm::IndirectBrInst::getNumDestinations() + */ +unsigned LLVMGetIndirectBrNumDestinations(LLVMValueRef IndirectBr); + +/** + * Obtain the destinations for all cases of an indirectbr instruction. + * \p Successors should point to an array of sufficient size, which can be + * determined by \p LLVMGetIndirectBrNumDestinations. + */ +void LLVMGetIndirectBrDestinations(LLVMValueRef IndirectBr, + LLVMBasicBlockRef *Successors); + +/** + * @} + */ + +/** * @defgroup LLVMCCoreValueInstructionPHINode PHI Nodes * * Functions in this group only apply to instructions that map to @@ -2479,18 +2925,20 @@ LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn); LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef); -/* Add a case to the switch instruction */ -void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal, - LLVMBasicBlockRef Dest); - -/* Add a destination to the indirectbr instruction */ -void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest); - /* Add a catch or filter clause to the landingpad instruction */ void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal); -/* Set the 'cleanup' flag in the landingpad instruction */ -void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val); +/** + * Obtain the cleanup flag in the landingpad instruction. + * @see llvm::LandingPadInst::isCleanup() + */ +LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad); + +/** + * Set the cleanup flag in the landingpad instruction + * @see llvm::LandingPadInst::setCleanup() + */ +void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Cleanup); /* Arithmetic */ LLVMValueRef LLVMBuildAdd(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS, @@ -2662,6 +3110,11 @@ LLVMValueRef PTR, LLVMValueRef Val, LLVMAtomicOrdering ordering, LLVMBool singleThread); +LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr, + LLVMValueRef Cmp, LLVMValueRef Val, + LLVMAtomicOrdering Ordering, + LLVMSynchronizationScope Scope, + const char *Name); /** * @} Index: lib/IR/Core.cpp =================================================================== --- lib/IR/Core.cpp +++ lib/IR/Core.cpp @@ -106,6 +106,15 @@ delete unwrap(M); } +char* LLVMGetModuleIdentifier(LLVMModuleRef M) { + return strdup(unwrap(M)->getModuleIdentifier().c_str()); +} + +void LLVMSetModuleIdentifier(LLVMModuleRef M, const char* ID) { + unwrap(M)->setModuleIdentifier(ID); +} + + /*--.. Data layout .........................................................--*/ const char * LLVMGetDataLayout(LLVMModuleRef M) { return unwrap(M)->getDataLayout().c_str(); @@ -446,6 +455,9 @@ LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) { return wrap(Type::getLabelTy(*unwrap(C))); } +LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) { + return wrap(Type::getMetadataTy(*unwrap(C))); +} LLVMTypeRef LLVMVoidType(void) { return LLVMVoidTypeInContext(LLVMGetGlobalContext()); @@ -453,6 +465,9 @@ LLVMTypeRef LLVMLabelType(void) { return LLVMLabelTypeInContext(LLVMGetGlobalContext()); } +LLVMTypeRef LLVMMetadataType(void) { + return LLVMMetadataTypeInContext(LLVMGetGlobalContext()); +} /*===-- Operations on values ----------------------------------------------===*/ @@ -462,6 +477,16 @@ return wrap(unwrap(Val)->getType()); } +LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) { + switch(unwrap(Val)->getValueID()) { +#define VALUE_KIND_CASE(class) case Value::class ## Val: return LLVM ## class ## ValueKind; +LLVM_FOR_EACH_VALUE_KIND(VALUE_KIND_CASE) +#undef VALUE_KIND_CASE + default: break; + } + return LLVMValueKind(0); +} + const char *LLVMGetValueName(LLVMValueRef Val) { return unwrap(Val)->getName().data(); } @@ -684,6 +709,21 @@ Radix)); } +unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) { + return unwrap(ConstantVal)->getZExtValue(); +} + +long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) { + return unwrap(ConstantVal)->getSExtValue(); +} + +const uint64_t *LLVMConstIntGetWords(LLVMValueRef ConstantVal, unsigned *n) { + const APInt &Int = unwrap(ConstantVal)->getValue(); + + *n = Int.getNumWords(); + return Int.getRawData(); +} + LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) { return wrap(ConstantFP::get(unwrap(RealTy), N)); } @@ -697,12 +737,27 @@ return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen))); } -unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) { - return unwrap(ConstantVal)->getZExtValue(); +LLVMValueRef LLVMConstRealOfArbitraryPrecision(LLVMTypeRef RealTy, + unsigned NumWords, + const uint64_t Words[]) { + Type *Ty = unwrap(RealTy); + APInt IntRepr = APInt(Ty->getPrimitiveSizeInBits(), + makeArrayRef(Words, NumWords)); + APFloat Float = APFloat(Ty->getFltSemantics(), IntRepr); + + return wrap(ConstantFP::get(Ty->getContext(), Float)); } -long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) { - return unwrap(ConstantVal)->getSExtValue(); +double LLVMConstRealGetDouble(LLVMValueRef ConstantVal) { + return unwrap(ConstantVal)->getValueAPF().convertToDouble(); +} + +const uint64_t *LLVMConstRealGetWords(LLVMValueRef ConstantVal, unsigned *n) { + const APFloat &Float = unwrap(ConstantVal)->getValueAPF(); + const APInt &IntRepr = Float.bitcastToAPInt(); + + *n = IntRepr.getNumWords(); + return IntRepr.getRawData(); } /*--.. Operations on composite constants ...................................--*/ @@ -781,6 +836,11 @@ return map_to_llvmopcode(unwrap(ConstantVal)->getOpcode()); } +LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef ConstantVal, unsigned n) { + return wrap(unwrap(ConstantVal)-> + getElementAsConstant(n)); +} + LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) { return wrap(ConstantExpr::getAlignOf(unwrap(Ty))); } @@ -1124,6 +1184,14 @@ return wrap(BlockAddress::get(unwrap(F), unwrap(BB))); } +LLVMValueRef LLVMGetBlockAddressFunction(LLVMValueRef BA) { + return wrap(unwrap(BA)->getFunction()); +} + +LLVMBasicBlockRef LLVMGetBlockAddressBlock(LLVMValueRef BA) { + return wrap(unwrap(BA)->getBasicBlock()); +} + /*--.. Operations on global variables, functions, and aliases (globals) ....--*/ LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) { @@ -1249,18 +1317,29 @@ ->setVisibility(static_cast(Viz)); } +LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) { + return unwrap(Global)->hasUnnamedAddr(); +} + +void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) { + unwrap(Global)->setUnnamedAddr(HasUnnamedAddr != 0); +} + /*--.. Operations on global variables, load and store instructions .........--*/ unsigned LLVMGetAlignment(LLVMValueRef V) { Value *P = unwrap(V); if (GlobalValue *GV = dyn_cast(P)) return GV->getAlignment(); - if (LoadInst *LI = dyn_cast(P)) + else if (LoadInst *LI = dyn_cast(P)) return LI->getAlignment(); - if (StoreInst *SI = dyn_cast(P)) + else if (StoreInst *SI = dyn_cast(P)) return SI->getAlignment(); + else if (AllocaInst *AI = dyn_cast(P)) + return AI->getAlignment(); - llvm_unreachable("only GlobalValue, LoadInst and StoreInst have alignment"); + llvm_unreachable("only GlobalValue, AllocaInst, LoadInst and StoreInst " + "have alignment"); } void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) { @@ -1271,8 +1350,11 @@ LI->setAlignment(Bytes); else if (StoreInst *SI = dyn_cast(P)) SI->setAlignment(Bytes); + else if (AllocaInst *AI = dyn_cast(P)) + AI->setAlignment(Bytes); else - llvm_unreachable("only GlobalValue, LoadInst and StoreInst have alignment"); + llvm_unreachable("only GlobalValue, AllocaInst, LoadInst and StoreInst " + "have alignment"); } /*--.. Operations on global variables ......................................--*/ @@ -1413,6 +1495,54 @@ unwrap(Aliasee), unwrap (M))); } +LLVMValueRef LLVMGetNamedAlias(LLVMModuleRef M, const char *Name) { + return wrap(unwrap(M)->getNamedAlias(Name)); +} + +LLVMValueRef LLVMGetFirstAlias(LLVMModuleRef M) { + Module *Mod = unwrap(M); + Module::alias_iterator I = Mod->alias_begin(); + if (I == Mod->alias_end()) + return 0; + return wrap(I); +} + +LLVMValueRef LLVMGetLastAlias(LLVMModuleRef M) { + Module *Mod = unwrap(M); + Module::alias_iterator I = Mod->alias_end(); + if (I == Mod->alias_begin()) + return 0; + return wrap(--I); +} + +LLVMValueRef LLVMGetNextAlias(LLVMValueRef Alias) { + GlobalAlias *GV = unwrap(Alias); + Module::alias_iterator I = GV; + if (++I == GV->getParent()->alias_end()) + return 0; + return wrap(I); +} + +LLVMValueRef LLVMGetPreviousAlias(LLVMValueRef Alias) { + GlobalAlias *GV = unwrap(Alias); + Module::alias_iterator I = GV; + if (I == GV->getParent()->alias_begin()) + return 0; + return wrap(--I); +} + +void LLVMDeleteAlias(LLVMValueRef Alias) { + unwrap(Alias)->eraseFromParent(); +} + +LLVMValueRef LLVMGetAliasee(LLVMValueRef A) { + return wrap(unwrap(A)->getAliasee()); +} + +void LLVMSetAliasee(LLVMValueRef A, LLVMValueRef Aliasee) { + unwrap(A)->setAliasee(unwrap(Aliasee)); +} + /*--.. Operations on functions .............................................--*/ LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name, @@ -1770,12 +1900,241 @@ return (LLVMIntPredicate)0; } +LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) { + if (FCmpInst *F = dyn_cast(unwrap(Inst))) + return (LLVMRealPredicate)F->getPredicate(); + if (ConstantExpr *CE = dyn_cast(unwrap(Inst))) + if (CE->getOpcode() == Instruction::FCmp) + return (LLVMRealPredicate)CE->getPredicate(); + return (LLVMRealPredicate)0; +} + LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) { if (Instruction *C = dyn_cast(unwrap(Inst))) return map_to_llvmopcode(C->getOpcode()); return (LLVMOpcode)0; } +/*--.. GetElementPtr instruction ...........................................--*/ + +LLVMBool LLVMIsInBounds(LLVMValueRef Inst) { + return unwrap(Inst)->isInBounds(); +} + +void LLVMSetIsInBounds(LLVMValueRef Inst, LLVMBool IsInBounds) { + unwrap(Inst)->setIsInBounds(IsInBounds != 0); +} + +/*--.. Binary operators ....................................................--*/ + +LLVMBool LLVMHasNoSignedWrap(LLVMValueRef Instr) { + return unwrap(Instr)->hasNoSignedWrap(); +} + +void LLVMSetHasNoSignedWrap(LLVMValueRef Instr, LLVMBool HasNoSignedWrap) { + unwrap(Instr)->setHasNoSignedWrap(HasNoSignedWrap != 0); +} + +LLVMBool LLVMHasNoUnsignedWrap(LLVMValueRef Instr) { + return unwrap(Instr)->hasNoUnsignedWrap(); +} + +void LLVMSetHasNoUnsignedWrap(LLVMValueRef Instr, LLVMBool HasNoUnsignedWrap) { + unwrap(Instr)->setHasNoUnsignedWrap(HasNoUnsignedWrap != 0); +} + +LLVMBool LLVMIsExact(LLVMValueRef Instr) { + return unwrap(Instr)->isExact(); +} + +void LLVMSetIsExact(LLVMValueRef Instr, LLVMBool IsExact) { + unwrap(Instr)->setIsExact(IsExact != 0); +} + +/*--.. Alloca instructions .................................................--*/ + +LLVMBool LLVMIsArrayAlloca(LLVMValueRef Instr) { + return unwrap(Instr)->isArrayAllocation(); +} + +LLVMValueRef LLVMGetAllocaNumElements(LLVMValueRef Instr) { + return wrap(unwrap(Instr)->getArraySize()); +} + +LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Instr) { + return wrap(unwrap(Instr)->getAllocatedType()); +} + +/*--.. Atomic instructions .................................................--*/ + +static LLVMAtomicOrdering wrap(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; + } +} + +static AtomicOrdering unwrap(LLVMAtomicOrdering ExtOrdering) { + switch(ExtOrdering) { + 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; + } +} + +LLVMAtomicOrdering LLVMGetAtomicOrdering(LLVMValueRef Instr) { + AtomicOrdering Ordering; + + if(LoadInst *LI = dyn_cast_or_null(unwrap(Instr))) + Ordering = LI->getOrdering(); + else if(StoreInst *SI = dyn_cast_or_null(unwrap(Instr))) + Ordering = SI->getOrdering(); + else if(FenceInst *FI = dyn_cast_or_null(unwrap(Instr))) + Ordering = FI->getOrdering(); + else if(AtomicRMWInst *ARMWI = dyn_cast_or_null(unwrap(Instr))) + Ordering = ARMWI->getOrdering(); + else if(AtomicCmpXchgInst *ACXI = + dyn_cast_or_null(unwrap(Instr))) + Ordering = ACXI->getOrdering(); + + return wrap(Ordering); +} + +void LLVMSetAtomicOrdering(LLVMValueRef Instr, LLVMAtomicOrdering ExtOrdering) { + AtomicOrdering Ordering = unwrap(ExtOrdering); + + if(LoadInst *LI = dyn_cast_or_null(unwrap(Instr))) + LI->setOrdering(Ordering); + else if(StoreInst *SI = dyn_cast_or_null(unwrap(Instr))) + SI->setOrdering(Ordering); + else if(FenceInst *FI = dyn_cast_or_null(unwrap(Instr))) + FI->setOrdering(Ordering); + else if(AtomicRMWInst *ARMWI = dyn_cast_or_null(unwrap(Instr))) + ARMWI->setOrdering(Ordering); + else if(AtomicCmpXchgInst *ACXI = + dyn_cast_or_null(unwrap(Instr))) + ACXI->setOrdering(Ordering); +} + +static LLVMSynchronizationScope wrap(SynchronizationScope Scope) { + switch(Scope) { + case SingleThread: + return LLVMSynchronizationScopeSingleThread; + case CrossThread: + return LLVMSynchronizationScopeCrossThread; + } +} + +static SynchronizationScope unwrap(LLVMSynchronizationScope ExtScope) { + switch(ExtScope) { + case LLVMSynchronizationScopeSingleThread: + return SingleThread; + case LLVMSynchronizationScopeCrossThread: + return CrossThread; + } +} + +LLVMSynchronizationScope LLVMGetSynchronizationScope(LLVMValueRef Instr) { + SynchronizationScope Scope; + + if(LoadInst *LI = dyn_cast_or_null(unwrap(Instr))) + Scope = LI->getSynchScope(); + else if(StoreInst *SI = dyn_cast_or_null(unwrap(Instr))) + Scope = SI->getSynchScope(); + else if(FenceInst *FI = dyn_cast_or_null(unwrap(Instr))) + Scope = FI->getSynchScope(); + else if(AtomicRMWInst *ARMWI = dyn_cast_or_null(unwrap(Instr))) + Scope = ARMWI->getSynchScope(); + else if(AtomicCmpXchgInst *ACXI = + dyn_cast_or_null(unwrap(Instr))) + Scope = ACXI->getSynchScope(); + + return wrap(Scope); +} + +void LLVMSetSynchronizationScope(LLVMValueRef Instr, + LLVMSynchronizationScope ExtScope) { + SynchronizationScope Scope = unwrap(ExtScope); + + if(LoadInst *LI = dyn_cast_or_null(unwrap(Instr))) + LI->setSynchScope(Scope); + else if(StoreInst *SI = dyn_cast_or_null(unwrap(Instr))) + SI->setSynchScope(Scope); + else if(FenceInst *FI = dyn_cast_or_null(unwrap(Instr))) + FI->setSynchScope(Scope); + else if(AtomicRMWInst *ARMWI = dyn_cast_or_null(unwrap(Instr))) + ARMWI->setSynchScope(Scope); + else if(AtomicCmpXchgInst *ACXI = + dyn_cast_or_null(unwrap(Instr))) + ACXI->setSynchScope(Scope); +} + +/*--.. AtomicRMW instructions ..............................................--*/ + +static LLVMAtomicRMWBinOp wrap(AtomicRMWInst::BinOp Op) { + switch (Op) { + default: llvm_unreachable("Unknown LLVMAtomicRMWBinOp"); + case AtomicRMWInst::Xchg: return LLVMAtomicRMWBinOpXchg; + case AtomicRMWInst::Add: return LLVMAtomicRMWBinOpAdd; + case AtomicRMWInst::Sub: return LLVMAtomicRMWBinOpSub; + case AtomicRMWInst::And: return LLVMAtomicRMWBinOpAnd; + case AtomicRMWInst::Nand: return LLVMAtomicRMWBinOpNand; + case AtomicRMWInst::Or: return LLVMAtomicRMWBinOpOr; + case AtomicRMWInst::Xor: return LLVMAtomicRMWBinOpXor; + case AtomicRMWInst::Max: return LLVMAtomicRMWBinOpMax; + case AtomicRMWInst::Min: return LLVMAtomicRMWBinOpMin; + case AtomicRMWInst::UMax: return LLVMAtomicRMWBinOpUMax; + case AtomicRMWInst::UMin: return LLVMAtomicRMWBinOpUMin; + } +} + +static AtomicRMWInst::BinOp unwrap(LLVMAtomicRMWBinOp ExtOp) { + switch (ExtOp) { + case LLVMAtomicRMWBinOpXchg: return AtomicRMWInst::Xchg; + case LLVMAtomicRMWBinOpAdd: return AtomicRMWInst::Add; + case LLVMAtomicRMWBinOpSub: return AtomicRMWInst::Sub; + case LLVMAtomicRMWBinOpAnd: return AtomicRMWInst::And; + case LLVMAtomicRMWBinOpNand: return AtomicRMWInst::Nand; + case LLVMAtomicRMWBinOpOr: return AtomicRMWInst::Or; + case LLVMAtomicRMWBinOpXor: return AtomicRMWInst::Xor; + case LLVMAtomicRMWBinOpMax: return AtomicRMWInst::Max; + case LLVMAtomicRMWBinOpMin: return AtomicRMWInst::Min; + case LLVMAtomicRMWBinOpUMax: return AtomicRMWInst::UMax; + case LLVMAtomicRMWBinOpUMin: return AtomicRMWInst::UMin; + } +} + +LLVMAtomicRMWBinOp LLVMGetAtomicRMWOperation(LLVMValueRef Instr) { + return wrap(unwrap(Instr)->getOperation()); +} + +void LLVMSetAtomicRMWOperation(LLVMValueRef Instr, LLVMAtomicRMWBinOp ExtOp) { + dyn_cast(unwrap(Instr))->setOperation(unwrap(ExtOp)); +} + /*--.. Call and invoke instructions ........................................--*/ unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) { @@ -1839,10 +2198,50 @@ /*--.. Operations on switch instructions (only) ............................--*/ +void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal, + LLVMBasicBlockRef Dest) { + unwrap(Switch)->addCase(unwrap(OnVal), unwrap(Dest)); +} + LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) { return wrap(unwrap(Switch)->getDefaultDest()); } +unsigned LLVMGetSwitchNumCases(LLVMValueRef SwitchInstr) { + return unwrap(SwitchInstr)->getNumCases(); +} + +void LLVMGetSwitchCases(LLVMValueRef SwitchInstr, LLVMValueRef *Values, + LLVMBasicBlockRef *Successors) { + SwitchInst *SI = unwrap(SwitchInstr); + + for (SwitchInst::CaseIt i = SI->case_begin(); + i != SI->case_end(); ++i, ++Values, ++Successors) { + *Values = wrap(i.getCaseValue()); + *Successors = wrap(i.getCaseSuccessor()); + } +} + +/*--.. Operations on indirectbr instructions (only) ........................--*/ + +void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) { + unwrap(IndirectBr)->addDestination(unwrap(Dest)); +} + +unsigned LLVMGetIndirectBrNumDestinations(LLVMValueRef IndirectBr) { + return unwrap(IndirectBr)->getNumDestinations(); +} + +void LLVMGetIndirectBrDestinations(LLVMValueRef IndirectBr, + LLVMBasicBlockRef *Successors) { + IndirectBrInst *IBI = unwrap(IndirectBr); + + for (unsigned i = 0; i != IBI->getNumDestinations(); ++i, ++Successors) { + *Successors = wrap(IBI->getDestination(i)); + } +} + + /*--.. Operations on phi nodes .............................................--*/ void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues, @@ -1989,15 +2388,6 @@ return wrap(unwrap(B)->CreateUnreachable()); } -void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal, - LLVMBasicBlockRef Dest) { - unwrap(Switch)->addCase(unwrap(OnVal), unwrap(Dest)); -} - -void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) { - unwrap(IndirectBr)->addDestination(unwrap(Dest)); -} - void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) { unwrap(LandingPad)-> addClause(cast(unwrap(ClauseVal))); @@ -2447,42 +2837,25 @@ return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name)); } -LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op, - LLVMValueRef PTR, LLVMValueRef Val, - LLVMAtomicOrdering ordering, - 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; - } - AtomicOrdering intordering; - switch (ordering) { - case LLVMAtomicOrderingNotAtomic: intordering = NotAtomic; break; - case LLVMAtomicOrderingUnordered: intordering = Unordered; break; - case LLVMAtomicOrderingMonotonic: intordering = Monotonic; break; - case LLVMAtomicOrderingAcquire: intordering = Acquire; break; - case LLVMAtomicOrderingRelease: intordering = Release; break; - case LLVMAtomicOrderingAcquireRelease: - intordering = AcquireRelease; - break; - case LLVMAtomicOrderingSequentiallyConsistent: - intordering = SequentiallyConsistent; - break; - } - return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val), - intordering, singleThread ? SingleThread : CrossThread)); +LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B, LLVMAtomicRMWBinOp Op, + LLVMValueRef Ptr, LLVMValueRef Val, + LLVMAtomicOrdering Ordering, + LLVMBool oneThread) { + return wrap(unwrap(B)->CreateAtomicRMW(unwrap(Op), unwrap(Ptr), unwrap(Val), + unwrap(Ordering), oneThread ? SingleThread : CrossThread)); } +LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr, + LLVMValueRef Cmp, LLVMValueRef Val, + LLVMAtomicOrdering Ordering, + LLVMSynchronizationScope Scope, + const char *Name) { + Value *Instr = unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp), + unwrap(Val), unwrap(Ordering), unwrap(Scope)); + Instr->setName(Name); + + return wrap(Instr); +} /*===-- Module providers --------------------------------------------------===*/