Repository navigation
Expand file tree
/
Copy pathtransforms.cpp
More file actions
81 lines (78 loc) · 3.13 KB
/
Copy pathtransforms.cpp
File metadata and controls
81 lines (78 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// Generic LLVM utilities exposed to Go; policy belongs to the caller.
#include "transforms.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Transforms/Utils/Local.h"
using namespace llvm;
// Keep the Go TailCallKind constants and the C boundary in sync with LLVM.
static_assert(CallInst::TCK_None == 0 && CallInst::TCK_Tail == 1 &&
CallInst::TCK_MustTail == 2 && CallInst::TCK_NoTail == 3,
"update the Go TailCallKind constants for this LLVM version");
LLVMValueRef LLVMGoGetNamedValue(LLVMModuleRef M, const char *Name,
size_t Len) {
return wrap(unwrap(M)->getNamedValue(StringRef(Name, Len)));
}
LLVMValueRef LLVMGoCloneFunction(LLVMValueRef Fn) {
auto *F = Fn ? dyn_cast<Function>(unwrap(Fn)) : nullptr;
if (!F)
return nullptr;
ValueToValueMapTy Map;
return wrap(CloneFunction(F, Map));
}
LLVMBool LLVMGoSimplifyInstructionsInBlock(LLVMBasicBlockRef BB) {
return SimplifyInstructionsInBlock(unwrap(BB));
}
LLVMBool LLVMGoConstantFoldTerminator(LLVMBasicBlockRef BB,
LLVMBool DeleteDead) {
return ConstantFoldTerminator(unwrap(BB), DeleteDead);
}
int LLVMGoRemoveUnreachableBlocks(LLVMValueRef Fn) {
auto *F = Fn ? dyn_cast<Function>(unwrap(Fn)) : nullptr;
if (!F || F->isDeclaration())
return -1;
return removeUnreachableBlocks(*F);
}
LLVMBool LLVMGoSetSubprogramLinkageName(LLVMMetadataRef SP, const char *Name,
size_t Len) {
auto *Subprogram = SP ? dyn_cast<DISubprogram>(unwrap(SP)) : nullptr;
if (!Subprogram)
return false;
Subprogram->replaceLinkageName(
MDString::get(Subprogram->getContext(), StringRef(Name, Len)));
return true;
}
LLVMBool LLVMGoSetTailCallKind(LLVMValueRef Call, unsigned Kind) {
auto *CI = Call ? dyn_cast<CallInst>(unwrap(Call)) : nullptr;
if (!CI || Kind > CallInst::TCK_NoTail)
return false;
CI->setTailCallKind(static_cast<CallInst::TailCallKind>(Kind));
return true;
}
unsigned LLVMGoGetTailCallKind(LLVMValueRef Call) {
auto *CI = Call ? dyn_cast<CallInst>(unwrap(Call)) : nullptr;
return CI ? CI->getTailCallKind() : ~0u;
}
LLVMGoInlineAsmInfo LLVMGoGetInlineAsmInfo(LLVMValueRef Asm) {
auto *A = Asm ? dyn_cast<InlineAsm>(unwrap(Asm)) : nullptr;
if (!A)
return {};
// Borrow InlineAsm's storage with either the older std::string& API or the
// newer StringRef API. Copying a std::string here would return dangling data.
StringRef Text = A->getAsmString();
StringRef Constraints = A->getConstraintString();
return {wrap(A->getFunctionType()),
Text.data(),
Constraints.data(),
Text.size(),
Constraints.size(),
A->hasSideEffects(),
A->isAlignStack(),
A->canThrow(),
static_cast<LLVMInlineAsmDialect>(A->getDialect())};
}