LLVM  14.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 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 #include "llvm/IR/Operator.h"
34 #include "llvm/IR/ValueHandle.h"
35 #include "llvm/InitializePasses.h"
36 #include "llvm/Pass.h"
37 
38 using namespace llvm;
39 
40 namespace {
41 
42 static Constant *canonicalizeAlias(Constant *C, bool &Changed) {
43  if (auto *GA = dyn_cast<GlobalAlias>(C)) {
44  auto *NewAliasee = canonicalizeAlias(GA->getAliasee(), Changed);
45  if (NewAliasee != GA->getAliasee()) {
46  GA->setAliasee(NewAliasee);
47  Changed = true;
48  }
49  return NewAliasee;
50  }
51 
52  auto *CE = dyn_cast<ConstantExpr>(C);
53  if (!CE)
54  return C;
55 
56  std::vector<Constant *> Ops;
57  for (Use &U : CE->operands())
58  Ops.push_back(canonicalizeAlias(cast<Constant>(U), Changed));
59  return CE->getWithOperands(Ops);
60 }
61 
62 /// Convert aliases to canonical form.
63 static bool canonicalizeAliases(Module &M) {
64  bool Changed = false;
65  for (auto &GA : M.aliases())
66  canonicalizeAlias(&GA, Changed);
67  return Changed;
68 }
69 
70 // Legacy pass that canonicalizes aliases.
71 class CanonicalizeAliasesLegacyPass : public ModulePass {
72 
73 public:
74  /// Pass identification, replacement for typeid
75  static char ID;
76 
77  /// Specify pass name for debug output
78  StringRef getPassName() const override { return "Canonicalize Aliases"; }
79 
80  explicit CanonicalizeAliasesLegacyPass() : ModulePass(ID) {}
81 
82  bool runOnModule(Module &M) override { return canonicalizeAliases(M); }
83 };
85 
86 } // anonymous namespace
87 
90  if (!canonicalizeAliases(M))
91  return PreservedAnalyses::all();
92 
93  return PreservedAnalyses::none();
94 }
95 
96 INITIALIZE_PASS_BEGIN(CanonicalizeAliasesLegacyPass, "canonicalize-aliases",
97  "Canonicalize aliases", false, false)
98 INITIALIZE_PASS_END(CanonicalizeAliasesLegacyPass, "canonicalize-aliases",
99  "Canonicalize aliases", false, false)
100 
101 namespace llvm {
103  return new CanonicalizeAliasesLegacyPass();
104 }
105 } // namespace llvm
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
aliases
canonicalize aliases
Definition: CanonicalizeAliases.cpp:98
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(CanonicalizeAliasesLegacyPass, "canonicalize-aliases", "Canonicalize aliases", false, false) INITIALIZE_PASS_END(CanonicalizeAliasesLegacyPass
Pass.h
llvm::PreservedAnalyses::none
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:158
Operator.h
llvm::createCanonicalizeAliasesPass
ModulePass * createCanonicalizeAliasesPass()
Definition: CanonicalizeAliases.cpp:102
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
false
Definition: StackSlotColoring.cpp:142
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::WinEH::EncodingType::CE
@ CE
Windows NT (Windows on ARM)
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::CanonicalizeAliasesPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: CanonicalizeAliases.cpp:88
CanonicalizeAliases.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
ValueHandle.h
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
InitializePasses.h
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:44
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37