LLVM  9.0.0svn
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 canonicalation, 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 
34 #include "llvm/IR/Operator.h"
35 #include "llvm/IR/ValueHandle.h"
36 
37 using namespace llvm;
38 
39 namespace {
40 
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);
46  Changed = true;
47  }
48  return NewAliasee;
49  }
50 
51  auto *CE = dyn_cast<ConstantExpr>(C);
52  if (!CE)
53  return C;
54 
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);
59 }
60 
61 /// Convert aliases to canonical form.
62 static bool canonicalizeAliases(Module &M) {
63  bool Changed = false;
64  for (auto &GA : M.aliases())
65  canonicalizeAlias(&GA, Changed);
66  return Changed;
67 }
68 
69 // Legacy pass that canonicalizes aliases.
70 class CanonicalizeAliasesLegacyPass : public ModulePass {
71 
72 public:
73  /// Pass identification, replacement for typeid
74  static char ID;
75 
76  /// Specify pass name for debug output
77  StringRef getPassName() const override { return "Canonicalize Aliases"; }
78 
79  explicit CanonicalizeAliasesLegacyPass() : ModulePass(ID) {}
80 
81  bool runOnModule(Module &M) override { return canonicalizeAliases(M); }
82 };
84 
85 } // anonymous namespace
86 
89  if (!canonicalizeAliases(M))
90  return PreservedAnalyses::all();
91 
92  return PreservedAnalyses::none();
93 }
94 
95 INITIALIZE_PASS_BEGIN(CanonicalizeAliasesLegacyPass, "canonicalize-aliases",
96  "Canonicalize aliases", false, false)
97 INITIALIZE_PASS_END(CanonicalizeAliasesLegacyPass, "canonicalize-aliases",
98  "Canonicalize aliases", false, false)
99 
100 namespace llvm {
102  return new CanonicalizeAliasesLegacyPass();
103 }
104 } // namespace llvm
uint64_t CallInst * C
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
INITIALIZE_PASS_BEGIN(CanonicalizeAliasesLegacyPass, "canonicalize-aliases", "Canonicalize aliases", false, false) INITIALIZE_PASS_END(CanonicalizeAliasesLegacyPass
ModulePass * createCanonicalizeAliasesPass()
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
Windows NT (Windows on ARM)
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:888
canonicalize aliases
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:156
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
This is an important base class in LLVM.
Definition: Constant.h:41
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:224
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
A container for analyses that lazily runs them and caches their results.
iterator_range< alias_iterator > aliases()
Definition: Module.h:627