• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "inline_ir_utils.h"
17 
18 #include <llvm/IR/Module.h>
19 #include <llvm/IR/Function.h>
20 
21 using llvm::Constant;
22 using llvm::dyn_cast;
23 using llvm::GlobalAlias;
24 using llvm::GlobalValue;
25 using llvm::Module;
26 using llvm::SmallVector;
27 
28 namespace ark::llvmbackend {
29 
30 /**
31  * Remove dangling alias from the module
32  *
33  * A dangled alias points to declaration instead of definitions.
34  * LLVM IR verifier disallows these aliases
35  *
36  * @return true if at least one alias was removed
37  */
RemoveDanglingAliases(Module & module)38 bool RemoveDanglingAliases(Module &module)
39 {
40     SmallVector<GlobalAlias *> aliases;
41     for (GlobalAlias &alias : module.aliases()) {
42         const Constant *aliasee = alias.getAliasee();
43         const auto *gv = dyn_cast<GlobalValue>(aliasee);
44         if (gv != nullptr && gv->isDeclarationForLinker()) {
45             aliases.push_back(&alias);
46         }
47     }
48     for (auto alias : aliases) {
49         alias->replaceAllUsesWith(alias->getAliasee());
50         alias->eraseFromParent();
51     }
52     return !aliases.empty();
53 }
54 
55 }  // namespace ark::llvmbackend
56