Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Utils/CanonicalizeAliases.cpp
35271 views
//===- CanonicalizeAliases.cpp - ThinLTO Support: Canonicalize Aliases ----===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Currently this file implements partial alias canonicalization, to9// flatten chains of aliases (also done by GlobalOpt, but not on for10// O0 compiles). E.g.11// @a = alias i8, i8 *@b12// @b = alias i8, i8 *@g13//14// will be converted to:15// @a = alias i8, i8 *@g <-- @a is now an alias to base object @g16// @b = alias i8, i8 *@g17//18// Eventually this file will implement full alias canonicalization, so that19// all aliasees are private anonymous values. E.g.20// @a = alias i8, i8 *@g21// @g = global i8 022//23// will be converted to:24// @0 = private global25// @a = alias i8, i8* @026// @g = alias i8, i8* @027//28// This simplifies optimization and ThinLTO linking of the original symbols.29//===----------------------------------------------------------------------===//3031#include "llvm/Transforms/Utils/CanonicalizeAliases.h"32#include "llvm/IR/Constants.h"33#include "llvm/IR/Module.h"3435using namespace llvm;3637namespace {3839static Constant *canonicalizeAlias(Constant *C, bool &Changed) {40if (auto *GA = dyn_cast<GlobalAlias>(C)) {41auto *NewAliasee = canonicalizeAlias(GA->getAliasee(), Changed);42if (NewAliasee != GA->getAliasee()) {43GA->setAliasee(NewAliasee);44Changed = true;45}46return NewAliasee;47}4849auto *CE = dyn_cast<ConstantExpr>(C);50if (!CE)51return C;5253std::vector<Constant *> Ops;54for (Use &U : CE->operands())55Ops.push_back(canonicalizeAlias(cast<Constant>(U), Changed));56return CE->getWithOperands(Ops);57}5859/// Convert aliases to canonical form.60static bool canonicalizeAliases(Module &M) {61bool Changed = false;62for (auto &GA : M.aliases())63canonicalizeAlias(&GA, Changed);64return Changed;65}66} // anonymous namespace6768PreservedAnalyses CanonicalizeAliasesPass::run(Module &M,69ModuleAnalysisManager &AM) {70if (!canonicalizeAliases(M))71return PreservedAnalyses::all();7273return PreservedAnalyses::none();74}757677