| 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/IRBuilder.h" |
| 20 | #include "llvm/IR/Intrinsics.h" |
| 21 | #include "llvm/IR/Module.h" |
| 22 | #include "llvm/IR/PassManager.h" |
| 23 | #include "llvm/IR/Use.h" |
| 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/Transforms/IPO/LowerTypeTests.h" |
| 26 | |
| 27 | using namespace llvm; |
| 28 | |
| 29 | #define DEBUG_TYPE "fatlto-cleanup" |
| 30 | |
| 31 | PreservedAnalyses FatLtoCleanup::run(Module &M, ModuleAnalysisManager &AM) { |
| 32 | // Undo the frontend checked-load instrumentation: it is not correct for |
| 33 | // the non-LTO part of a FatLTO object, where whole-program metadata is |
| 34 | // unavailable, and the backend cannot select these intrinsics anyway. |
| 35 | Function *TypeCheckedLoadFn = |
| 36 | Intrinsic::getDeclarationIfExists(M: &M, id: Intrinsic::type_checked_load); |
| 37 | Function *TypeCheckedLoadRelFn = Intrinsic::getDeclarationIfExists( |
| 38 | M: &M, id: Intrinsic::type_checked_load_relative); |
| 39 | |
| 40 | bool Changed = false; |
| 41 | if (TypeCheckedLoadFn) |
| 42 | Changed |= |
| 43 | lowertypetests::cleanUpTypeCheckedLoad(M, CheckedLoadFn&: *TypeCheckedLoadFn, IsRelative: false); |
| 44 | if (TypeCheckedLoadRelFn) |
| 45 | Changed |= |
| 46 | lowertypetests::cleanUpTypeCheckedLoad(M, CheckedLoadFn&: *TypeCheckedLoadRelFn, IsRelative: true); |
| 47 | |
| 48 | if (Changed) |
| 49 | return PreservedAnalyses::none(); |
| 50 | return PreservedAnalyses::all(); |
| 51 | } |
| 52 | |