blob: a6c2c9b464b634cfd20605d37e4800003e275153 [file] [log] [blame]
Owen Anderson97868682010-10-07 20:17:24 +00001//===-- Instrumentation.cpp - TransformUtils Infrastructure ---------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Owen Anderson97868682010-10-07 20:17:24 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the common initialization infrastructure for the
10// Instrumentation library.
11//
12//===----------------------------------------------------------------------===//
13
Reid Klecknera57d0152015-08-14 16:45:42 +000014#include "llvm/Transforms/Instrumentation.h"
Owen Anderson97868682010-10-07 20:17:24 +000015#include "llvm-c/Initialization.h"
Reid Klecknerb41b3722018-11-08 00:57:33 +000016#include "llvm/ADT/Triple.h"
Reid Klecknera57d0152015-08-14 16:45:42 +000017#include "llvm/IR/IntrinsicInst.h"
Kostya Serebryanyd891ac92018-10-11 23:03:27 +000018#include "llvm/IR/Module.h"
Reid Klecknera57d0152015-08-14 16:45:42 +000019#include "llvm/InitializePasses.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000020#include "llvm/PassRegistry.h"
Owen Anderson97868682010-10-07 20:17:24 +000021
22using namespace llvm;
23
Reid Klecknera57d0152015-08-14 16:45:42 +000024/// Moves I before IP. Returns new insert point.
25static BasicBlock::iterator moveBeforeInsertPoint(BasicBlock::iterator I, BasicBlock::iterator IP) {
26 // If I is IP, move the insert point down.
David Blaikie0c4dbf92019-04-25 20:09:00 +000027 if (I == IP) {
28 ++IP;
29 } else {
30 // Otherwise, move I before IP and return IP.
31 I->moveBefore(&*IP);
32 }
Reid Klecknera57d0152015-08-14 16:45:42 +000033 return IP;
34}
35
36/// Instrumentation passes often insert conditional checks into entry blocks.
37/// Call this function before splitting the entry block to move instructions
38/// that must remain in the entry block up before the split point. Static
39/// allocas and llvm.localescape calls, for example, must remain in the entry
40/// block.
41BasicBlock::iterator llvm::PrepareToSplitEntryBlock(BasicBlock &BB,
42 BasicBlock::iterator IP) {
43 assert(&BB.getParent()->getEntryBlock() == &BB);
44 for (auto I = IP, E = BB.end(); I != E; ++I) {
45 bool KeepInEntry = false;
46 if (auto *AI = dyn_cast<AllocaInst>(I)) {
47 if (AI->isStaticAlloca())
48 KeepInEntry = true;
49 } else if (auto *II = dyn_cast<IntrinsicInst>(I)) {
50 if (II->getIntrinsicID() == llvm::Intrinsic::localescape)
51 KeepInEntry = true;
52 }
53 if (KeepInEntry)
54 IP = moveBeforeInsertPoint(I, IP);
55 }
56 return IP;
57}
58
Kostya Serebryanyd891ac92018-10-11 23:03:27 +000059// Create a constant for Str so that we can pass it to the run-time lib.
60GlobalVariable *llvm::createPrivateGlobalForString(Module &M, StringRef Str,
61 bool AllowMerging,
62 const char *NamePrefix) {
63 Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
64 // We use private linkage for module-local strings. If they can be merged
65 // with another one, we set the unnamed_addr attribute.
66 GlobalVariable *GV =
67 new GlobalVariable(M, StrConst->getType(), true,
68 GlobalValue::PrivateLinkage, StrConst, NamePrefix);
69 if (AllowMerging)
70 GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
Guillaume Chatelet0e620112019-10-15 11:24:36 +000071 GV->setAlignment(Align::None()); // Strings may not be merged w/o setting
72 // alignment explicitly.
Kostya Serebryanyd891ac92018-10-11 23:03:27 +000073 return GV;
74}
75
Reid Klecknerb41b3722018-11-08 00:57:33 +000076Comdat *llvm::GetOrCreateFunctionComdat(Function &F, Triple &T,
Kostya Serebryanybc504552018-10-12 23:21:48 +000077 const std::string &ModuleId) {
78 if (auto Comdat = F.getComdat()) return Comdat;
79 assert(F.hasName());
80 Module *M = F.getParent();
81 std::string Name = F.getName();
Reid Klecknerb41b3722018-11-08 00:57:33 +000082
83 // Make a unique comdat name for internal linkage things on ELF. On COFF, the
84 // name of the comdat group identifies the leader symbol of the comdat group.
85 // The linkage of the leader symbol is considered during comdat resolution,
86 // and internal symbols with the same name from different objects will not be
87 // merged.
88 if (T.isOSBinFormatELF() && F.hasLocalLinkage()) {
Kostya Serebryanybc504552018-10-12 23:21:48 +000089 if (ModuleId.empty())
90 return nullptr;
91 Name += ModuleId;
92 }
Reid Klecknerb41b3722018-11-08 00:57:33 +000093
94 // Make a new comdat for the function. Use the "no duplicates" selection kind
95 // for non-weak symbols if the object file format supports it.
96 Comdat *C = M->getOrInsertComdat(Name);
97 if (T.isOSBinFormatCOFF() && !F.isWeakForLinker())
98 C->setSelectionKind(Comdat::NoDuplicates);
99 F.setComdat(C);
100 return C;
Kostya Serebryanybc504552018-10-12 23:21:48 +0000101}
102
Owen Anderson97868682010-10-07 20:17:24 +0000103/// initializeInstrumentation - Initialize all passes in the TransformUtils
104/// library.
105void llvm::initializeInstrumentation(PassRegistry &Registry) {
Leonard Chan436fb2b2019-02-13 22:22:48 +0000106 initializeAddressSanitizerLegacyPassPass(Registry);
107 initializeModuleAddressSanitizerLegacyPassPass(Registry);
Chandler Carruth00a301d2017-11-14 01:30:04 +0000108 initializeBoundsCheckingLegacyPassPass(Registry);
Hiroshi Yamauchi9775a622018-09-04 17:19:13 +0000109 initializeControlHeightReductionLegacyPassPass(Registry);
Xinliang David Lifb3137c2016-06-05 03:40:03 +0000110 initializeGCOVProfilerLegacyPassPass(Registry);
Xinliang David Li8aebf442016-05-06 05:49:19 +0000111 initializePGOInstrumentationGenLegacyPassPass(Registry);
Xinliang David Lid55827f2016-05-07 05:39:12 +0000112 initializePGOInstrumentationUseLegacyPassPass(Registry);
Xinliang David Li72616182016-05-15 01:04:24 +0000113 initializePGOIndirectCallPromotionLegacyPassPass(Registry);
Rong Xu48596b62017-04-04 16:42:20 +0000114 initializePGOMemOPSizeOptLegacyPassPass(Registry);
Manman Ren18295122019-02-28 20:13:38 +0000115 initializeInstrOrderFileLegacyPassPass(Registry);
Xinliang David Lie6b89292016-04-18 17:47:38 +0000116 initializeInstrProfilingLegacyPassPass(Registry);
Philip Pfaffeb39a97c2019-01-03 13:42:44 +0000117 initializeMemorySanitizerLegacyPassPass(Registry);
Leonard Chan0cdd3b12019-05-14 21:17:21 +0000118 initializeHWAddressSanitizerLegacyPassPass(Registry);
Philip Pfaffe685c76d2019-01-16 09:28:01 +0000119 initializeThreadSanitizerLegacyPassPass(Registry);
Leonard Chaneca01b02019-09-04 20:30:29 +0000120 initializeModuleSanitizerCoverageLegacyPassPass(Registry);
Peter Collingbournee5d5b0c2013-08-07 22:47:18 +0000121 initializeDataFlowSanitizerPass(Registry);
Owen Anderson97868682010-10-07 20:17:24 +0000122}
123
124/// LLVMInitializeInstrumentation - C binding for
125/// initializeInstrumentation.
126void LLVMInitializeInstrumentation(LLVMPassRegistryRef R) {
127 initializeInstrumentation(*unwrap(R));
128}