2014-10-16 22:48:02 +00:00
|
|
|
//===- IRBindings.cpp - Additional bindings for ir ------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines additional C bindings for the ir component.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "IRBindings.h"
|
|
|
|
|
|
|
|
#include "llvm/IR/Attributes.h"
|
2014-12-13 02:25:49 +00:00
|
|
|
#include "llvm/IR/DebugLoc.h"
|
2014-10-16 22:48:02 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
2014-12-13 02:25:49 +00:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2014-10-16 22:48:02 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
void LLVMAddFunctionAttr2(LLVMValueRef Fn, uint64_t PA) {
|
|
|
|
Function *Func = unwrap<Function>(Fn);
|
|
|
|
const AttributeSet PAL = Func->getAttributes();
|
|
|
|
AttrBuilder B(PA);
|
|
|
|
const AttributeSet PALnew =
|
|
|
|
PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
|
|
|
|
AttributeSet::get(Func->getContext(),
|
|
|
|
AttributeSet::FunctionIndex, B));
|
|
|
|
Func->setAttributes(PALnew);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t LLVMGetFunctionAttr2(LLVMValueRef Fn) {
|
|
|
|
Function *Func = unwrap<Function>(Fn);
|
|
|
|
const AttributeSet PAL = Func->getAttributes();
|
|
|
|
return PAL.Raw(AttributeSet::FunctionIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMRemoveFunctionAttr2(LLVMValueRef Fn, uint64_t PA) {
|
|
|
|
Function *Func = unwrap<Function>(Fn);
|
|
|
|
const AttributeSet PAL = Func->getAttributes();
|
|
|
|
AttrBuilder B(PA);
|
|
|
|
const AttributeSet PALnew =
|
|
|
|
PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
|
|
|
|
AttributeSet::get(Func->getContext(),
|
|
|
|
AttributeSet::FunctionIndex, B));
|
|
|
|
Func->setAttributes(PALnew);
|
|
|
|
}
|
2014-12-13 02:25:49 +00:00
|
|
|
|
2014-12-13 02:25:57 +00:00
|
|
|
LLVMMetadataRef LLVMConstantAsMetadata(LLVMValueRef C) {
|
|
|
|
return wrap(ConstantAsMetadata::get(unwrap<Constant>(C)));
|
|
|
|
}
|
|
|
|
|
2014-12-13 02:25:49 +00:00
|
|
|
LLVMMetadataRef LLVMMDString2(LLVMContextRef C, const char *Str, unsigned SLen) {
|
|
|
|
return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMMetadataRef LLVMMDNode2(LLVMContextRef C, LLVMMetadataRef *MDs,
|
|
|
|
unsigned Count) {
|
|
|
|
return wrap(
|
|
|
|
MDNode::get(*unwrap(C), ArrayRef<Metadata *>(unwrap(MDs), Count)));
|
|
|
|
}
|
|
|
|
|
2014-12-13 02:25:54 +00:00
|
|
|
LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef C, LLVMMetadataRef *MDs,
|
|
|
|
unsigned Count) {
|
2015-01-19 21:30:18 +00:00
|
|
|
return wrap(MDTuple::getTemporary(*unwrap(C),
|
|
|
|
ArrayRef<Metadata *>(unwrap(MDs), Count))
|
|
|
|
.release());
|
2014-12-13 02:25:54 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 02:25:49 +00:00
|
|
|
void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name,
|
|
|
|
LLVMMetadataRef Val) {
|
|
|
|
NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
|
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
if (!Val)
|
|
|
|
return;
|
|
|
|
N->addOperand(unwrap<MDNode>(Val));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD) {
|
|
|
|
MDNode *N = MD ? unwrap<MDNode>(MD) : nullptr;
|
|
|
|
unwrap<Instruction>(Inst)->setMetadata(KindID, N);
|
|
|
|
}
|
|
|
|
|
2014-12-13 02:25:51 +00:00
|
|
|
void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef MD, LLVMMetadataRef New) {
|
IR: Remove MDNodeFwdDecl
Remove `MDNodeFwdDecl` (as promised in r226481). Aside from API
changes, there's no real functionality change here.
`MDNode::getTemporary()` now forwards to `MDTuple::getTemporary()`,
which returns a tuple with `isTemporary()` equal to true.
The main point is that we can now add temporaries of other `MDNode`
subclasses, needed for PR22235 (I introduced `MDNodeFwdDecl` in the
first place because I didn't recognize this need, and thought they were
only needed to handle forward references).
A few things left out of (or highlighted by) this commit:
- I've had to remove the (few) uses of `std::unique_ptr<>` to deal
with temporaries, since the destructor is no longer public.
`getTemporary()` should probably return the equivalent of
`std::unique_ptr<T, MDNode::deleteTemporary>`.
- `MDLocation::getTemporary()` doesn't exist yet (worse, it actually
does exist, but does the wrong thing: `MDNode::getTemporary()` is
inherited and returns an `MDTuple`).
- `MDNode` now only has one subclass, `UniquableMDNode`, and the
distinction between them is actually somewhat confusing.
I'll fix those up next.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226501 91177308-0d34-0410-b5e6-96231b3b80d8
2015-01-19 20:36:39 +00:00
|
|
|
auto *Node = unwrap<MDTuple>(MD);
|
|
|
|
assert(Node->isTemporary() && "Expected temporary node");
|
2014-12-13 02:25:51 +00:00
|
|
|
Node->replaceAllUsesWith(unwrap<MDNode>(New));
|
|
|
|
MDNode::deleteTemporary(Node);
|
|
|
|
}
|
|
|
|
|
2014-12-13 02:25:49 +00:00
|
|
|
void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Bref, unsigned Line,
|
|
|
|
unsigned Col, LLVMMetadataRef Scope,
|
|
|
|
LLVMMetadataRef InlinedAt) {
|
|
|
|
unwrap(Bref)->SetCurrentDebugLocation(
|
|
|
|
DebugLoc::get(Line, Col, Scope ? unwrap<MDNode>(Scope) : nullptr,
|
|
|
|
InlinedAt ? unwrap<MDNode>(InlinedAt) : nullptr));
|
|
|
|
}
|