Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Utils/CanonicalizeAliases.cpp
Line
Count
Source
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
32
#include "llvm/Transforms/Utils/CanonicalizeAliases.h"
33
34
#include "llvm/IR/Operator.h"
35
#include "llvm/IR/ValueHandle.h"
36
37
using namespace llvm;
38
39
namespace {
40
41
266
static Constant *canonicalizeAlias(Constant *C, bool &Changed) {
42
266
  if (auto *GA = dyn_cast<GlobalAlias>(C)) {
43
63
    auto *NewAliasee = canonicalizeAlias(GA->getAliasee(), Changed);
44
63
    if (NewAliasee != GA->getAliasee()) {
45
12
      GA->setAliasee(NewAliasee);
46
12
      Changed = true;
47
12
    }
48
63
    return NewAliasee;
49
63
  }
50
203
51
203
  auto *CE = dyn_cast<ConstantExpr>(C);
52
203
  if (!CE)
53
162
    return C;
54
41
55
41
  std::vector<Constant *> Ops;
56
41
  for (Use &U : CE->operands())
57
152
    Ops.push_back(canonicalizeAlias(cast<Constant>(U), Changed));
58
41
  return CE->getWithOperands(Ops);
59
41
}
60
61
/// Convert aliases to canonical form.
62
92
static bool canonicalizeAliases(Module &M) {
63
92
  bool Changed = false;
64
92
  for (auto &GA : M.aliases())
65
51
    canonicalizeAlias(&GA, Changed);
66
92
  return Changed;
67
92
}
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
5
  StringRef getPassName() const override { return "Canonicalize Aliases"; }
78
79
58
  explicit CanonicalizeAliasesLegacyPass() : ModulePass(ID) {}
80
81
58
  bool runOnModule(Module &M) override { return canonicalizeAliases(M); }
82
};
83
char CanonicalizeAliasesLegacyPass::ID = 0;
84
85
} // anonymous namespace
86
87
PreservedAnalyses CanonicalizeAliasesPass::run(Module &M,
88
34
                                               ModuleAnalysisManager &AM) {
89
34
  if (!canonicalizeAliases(M))
90
33
    return PreservedAnalyses::all();
91
1
92
1
  return PreservedAnalyses::none();
93
1
}
94
95
11.0k
INITIALIZE_PASS_BEGIN(CanonicalizeAliasesLegacyPass, "canonicalize-aliases",
96
11.0k
                      "Canonicalize aliases", false, false)
97
11.0k
INITIALIZE_PASS_END(CanonicalizeAliasesLegacyPass, "canonicalize-aliases",
98
                    "Canonicalize aliases", false, false)
99
100
namespace llvm {
101
57
ModulePass *createCanonicalizeAliasesPass() {
102
57
  return new CanonicalizeAliasesLegacyPass();
103
57
}
104
} // namespace llvm