1 //===- CanonicalizeAliases.cpp - ThinLTO Support: Canonicalize Aliases ----===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Currently this file implements partial alias canonicalization, to
10 // flatten chains of aliases (also done by GlobalOpt, but not on for
12 // @a = alias i8, i8 *@b
13 // @b = alias i8, i8 *@g
15 // will be converted to:
16 // @a = alias i8, i8 *@g <-- @a is now an alias to base object @g
17 // @b = alias i8, i8 *@g
19 // Eventually this file will implement full alias canonicalation, so that
20 // all aliasees are private anonymous values. E.g.
21 // @a = alias i8, i8 *@g
24 // will be converted to:
25 // @0 = private global
26 // @a = alias i8, i8* @0
27 // @g = alias i8, i8* @0
29 // This simplifies optimization and ThinLTO linking of the original symbols.
30 //===----------------------------------------------------------------------===//
32 #include "llvm/Transforms/Utils/CanonicalizeAliases.h"
34 #include "llvm/IR/Operator.h"
35 #include "llvm/IR/ValueHandle.h"
41 static Constant *canonicalizeAlias(Constant *C, bool &Changed) {
42 if (auto *GA = dyn_cast<GlobalAlias>(C)) {
43 auto *NewAliasee = canonicalizeAlias(GA->getAliasee(), Changed);
44 if (NewAliasee != GA->getAliasee()) {
45 GA->setAliasee(NewAliasee);
51 auto *CE = dyn_cast<ConstantExpr>(C);
55 std::vector<Constant *> Ops;
56 for (Use &U : CE->operands())
57 Ops.push_back(canonicalizeAlias(cast<Constant>(U), Changed));
58 return CE->getWithOperands(Ops);
61 /// Convert aliases to canonical form.
62 static bool canonicalizeAliases(Module &M) {
64 for (auto &GA : M.aliases())
65 canonicalizeAlias(&GA, Changed);
69 // Legacy pass that canonicalizes aliases.
70 class CanonicalizeAliasesLegacyPass : public ModulePass {
73 /// Pass identification, replacement for typeid
76 /// Specify pass name for debug output
77 StringRef getPassName() const override { return "Canonicalize Aliases"; }
79 explicit CanonicalizeAliasesLegacyPass() : ModulePass(ID) {}
81 bool runOnModule(Module &M) override { return canonicalizeAliases(M); }
83 char CanonicalizeAliasesLegacyPass::ID = 0;
85 } // anonymous namespace
87 PreservedAnalyses CanonicalizeAliasesPass::run(Module &M,
88 ModuleAnalysisManager &AM) {
89 if (!canonicalizeAliases(M))
90 return PreservedAnalyses::all();
92 return PreservedAnalyses::none();
95 INITIALIZE_PASS_BEGIN(CanonicalizeAliasesLegacyPass, "canonicalize-aliases",
96 "Canonicalize aliases", false, false)
97 INITIALIZE_PASS_END(CanonicalizeAliasesLegacyPass, "canonicalize-aliases",
98 "Canonicalize aliases", false, false)
101 ModulePass *createCanonicalizeAliasesPass() {
102 return new CanonicalizeAliasesLegacyPass();