1//===- FatLtoCleanup.cpp - clean up IR for the FatLTO pipeline --*- C++ -*-===//
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// This file defines operations used to clean up IR for the FatLTO pipeline.
10// Instrumentation that is beneficial for bitcode sections used in LTO may
11// need to be cleaned up to finish non-LTO compilation. llvm.checked.load is
12// an example of an instruction that we want to preserve for LTO, but is
13// incorrect to leave unchanged during the per-TU compilation in FatLTO.
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/Transforms/IPO/FatLTOCleanup.h"
18#include "llvm/IR/Function.h"
19#include "llvm/IR/Intrinsics.h"
20#include "llvm/IR/Module.h"
21#include "llvm/IR/PassManager.h"
22#include "llvm/Transforms/IPO/LowerTypeTests.h"
23
24using namespace llvm;
25
26#define DEBUG_TYPE "fatlto-cleanup"
27
28PreservedAnalyses FatLtoCleanup::run(Module &M, ModuleAnalysisManager &AM) {
29 // Undo the frontend checked-load instrumentation: it is not correct for
30 // the non-LTO part of a FatLTO object, where whole-program metadata is
31 // unavailable, and the backend cannot select these intrinsics anyway.
32 Function *TypeCheckedLoadFn =
33 Intrinsic::getDeclarationIfExists(M: &M, id: Intrinsic::type_checked_load);
34 Function *TypeCheckedLoadRelFn = Intrinsic::getDeclarationIfExists(
35 M: &M, id: Intrinsic::type_checked_load_relative);
36
37 bool Changed = false;
38 if (TypeCheckedLoadFn)
39 Changed |=
40 lowertypetests::cleanUpTypeCheckedLoad(M, CheckedLoadFn&: *TypeCheckedLoadFn, IsRelative: false);
41 if (TypeCheckedLoadRelFn)
42 Changed |=
43 lowertypetests::cleanUpTypeCheckedLoad(M, CheckedLoadFn&: *TypeCheckedLoadRelFn, IsRelative: true);
44
45 if (Changed)
46 return PreservedAnalyses::none();
47 return PreservedAnalyses::all();
48}
49