LLVM 19.0.0git
CanonicalizeAliases.cpp
Go to the documentation of this file.
1//===- CanonicalizeAliases.cpp - ThinLTO Support: Canonicalize Aliases ----===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// Currently this file implements partial alias canonicalization, to
10// flatten chains of aliases (also done by GlobalOpt, but not on for
11// O0 compiles). E.g.
12// @a = alias i8, i8 *@b
13// @b = alias i8, i8 *@g
14//
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
18//
19// Eventually this file will implement full alias canonicalization, so that
20// all aliasees are private anonymous values. E.g.
21// @a = alias i8, i8 *@g
22// @g = global i8 0
23//
24// will be converted to:
25// @0 = private global
26// @a = alias i8, i8* @0
27// @g = alias i8, i8* @0
28//
29// This simplifies optimization and ThinLTO linking of the original symbols.
30//===----------------------------------------------------------------------===//
31
33#include "llvm/IR/Constants.h"
34
35using namespace llvm;
36
37namespace {
38
39static Constant *canonicalizeAlias(Constant *C, bool &Changed) {
40 if (auto *GA = dyn_cast<GlobalAlias>(C)) {
41 auto *NewAliasee = canonicalizeAlias(GA->getAliasee(), Changed);
42 if (NewAliasee != GA->getAliasee()) {
43 GA->setAliasee(NewAliasee);
44 Changed = true;
45 }
46 return NewAliasee;
47 }
48
49 auto *CE = dyn_cast<ConstantExpr>(C);
50 if (!CE)
51 return C;
52
53 std::vector<Constant *> Ops;
54 for (Use &U : CE->operands())
55 Ops.push_back(canonicalizeAlias(cast<Constant>(U), Changed));
56 return CE->getWithOperands(Ops);
57}
58
59/// Convert aliases to canonical form.
60static bool canonicalizeAliases(Module &M) {
61 bool Changed = false;
62 for (auto &GA : M.aliases())
63 canonicalizeAlias(&GA, Changed);
64 return Changed;
65}
66} // anonymous namespace
67
70 if (!canonicalizeAliases(M))
72
74}
This file contains the declarations for the subclasses of Constant, which represent the different fla...
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:321
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
This is an important base class in LLVM.
Definition: Constant.h:41
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:115
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ CE
Windows NT (Windows on ARM)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18