CanonicalizeAliases.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #include "llvm/Transforms/Utils/CanonicalizeAliases.h"
  32. #include "llvm/IR/Constants.h"
  33. #include "llvm/InitializePasses.h"
  34. #include "llvm/Pass.h"
  35. using namespace llvm;
  36. namespace {
  37. static Constant *canonicalizeAlias(Constant *C, bool &Changed) {
  38. if (auto *GA = dyn_cast<GlobalAlias>(C)) {
  39. auto *NewAliasee = canonicalizeAlias(GA->getAliasee(), Changed);
  40. if (NewAliasee != GA->getAliasee()) {
  41. GA->setAliasee(NewAliasee);
  42. Changed = true;
  43. }
  44. return NewAliasee;
  45. }
  46. auto *CE = dyn_cast<ConstantExpr>(C);
  47. if (!CE)
  48. return C;
  49. std::vector<Constant *> Ops;
  50. for (Use &U : CE->operands())
  51. Ops.push_back(canonicalizeAlias(cast<Constant>(U), Changed));
  52. return CE->getWithOperands(Ops);
  53. }
  54. /// Convert aliases to canonical form.
  55. static bool canonicalizeAliases(Module &M) {
  56. bool Changed = false;
  57. for (auto &GA : M.aliases())
  58. canonicalizeAlias(&GA, Changed);
  59. return Changed;
  60. }
  61. } // anonymous namespace
  62. PreservedAnalyses CanonicalizeAliasesPass::run(Module &M,
  63. ModuleAnalysisManager &AM) {
  64. if (!canonicalizeAliases(M))
  65. return PreservedAnalyses::all();
  66. return PreservedAnalyses::none();
  67. }