blob: eb6a3730ad9a8ec8b5134a8aaa764f922680c49c [file] [log] [blame]
Owen Anderson97868682010-10-07 20:17:24 +00001//===-- Instrumentation.cpp - TransformUtils Infrastructure ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the common initialization infrastructure for the
11// Instrumentation library.
12//
13//===----------------------------------------------------------------------===//
14
Reid Klecknera57d0152015-08-14 16:45:42 +000015#include "llvm/Transforms/Instrumentation.h"
Owen Anderson97868682010-10-07 20:17:24 +000016#include "llvm-c/Initialization.h"
Reid Klecknerb41b3722018-11-08 00:57:33 +000017#include "llvm/ADT/Triple.h"
Reid Klecknera57d0152015-08-14 16:45:42 +000018#include "llvm/IR/IntrinsicInst.h"
Kostya Serebryanyd891ac92018-10-11 23:03:27 +000019#include "llvm/IR/Module.h"
Reid Klecknera57d0152015-08-14 16:45:42 +000020#include "llvm/InitializePasses.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000021#include "llvm/PassRegistry.h"
Owen Anderson97868682010-10-07 20:17:24 +000022
23using namespace llvm;
24
Reid Klecknera57d0152015-08-14 16:45:42 +000025/// Moves I before IP. Returns new insert point.
26static BasicBlock::iterator moveBeforeInsertPoint(BasicBlock::iterator I, BasicBlock::iterator IP) {
27 // If I is IP, move the insert point down.
28 if (I == IP)
29 return ++IP;
30 // Otherwise, move I before IP and return IP.
Duncan P. N. Exon Smithe82c2862015-10-13 17:39:10 +000031 I->moveBefore(&*IP);
Reid Klecknera57d0152015-08-14 16:45:42 +000032 return IP;
33}
34
35/// Instrumentation passes often insert conditional checks into entry blocks.
36/// Call this function before splitting the entry block to move instructions
37/// that must remain in the entry block up before the split point. Static
38/// allocas and llvm.localescape calls, for example, must remain in the entry
39/// block.
40BasicBlock::iterator llvm::PrepareToSplitEntryBlock(BasicBlock &BB,
41 BasicBlock::iterator IP) {
42 assert(&BB.getParent()->getEntryBlock() == &BB);
43 for (auto I = IP, E = BB.end(); I != E; ++I) {
44 bool KeepInEntry = false;
45 if (auto *AI = dyn_cast<AllocaInst>(I)) {
46 if (AI->isStaticAlloca())
47 KeepInEntry = true;
48 } else if (auto *II = dyn_cast<IntrinsicInst>(I)) {
49 if (II->getIntrinsicID() == llvm::Intrinsic::localescape)
50 KeepInEntry = true;
51 }
52 if (KeepInEntry)
53 IP = moveBeforeInsertPoint(I, IP);
54 }
55 return IP;
56}
57
Kostya Serebryanyd891ac92018-10-11 23:03:27 +000058// Create a constant for Str so that we can pass it to the run-time lib.
59GlobalVariable *llvm::createPrivateGlobalForString(Module &M, StringRef Str,
60 bool AllowMerging,
61 const char *NamePrefix) {
62 Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
63 // We use private linkage for module-local strings. If they can be merged
64 // with another one, we set the unnamed_addr attribute.
65 GlobalVariable *GV =
66 new GlobalVariable(M, StrConst->getType(), true,
67 GlobalValue::PrivateLinkage, StrConst, NamePrefix);
68 if (AllowMerging)
69 GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
70 GV->setAlignment(1); // Strings may not be merged w/o setting align 1.
71 return GV;
72}
73
Reid Klecknerb41b3722018-11-08 00:57:33 +000074Comdat *llvm::GetOrCreateFunctionComdat(Function &F, Triple &T,
Kostya Serebryanybc504552018-10-12 23:21:48 +000075 const std::string &ModuleId) {
76 if (auto Comdat = F.getComdat()) return Comdat;
77 assert(F.hasName());
78 Module *M = F.getParent();
79 std::string Name = F.getName();
Reid Klecknerb41b3722018-11-08 00:57:33 +000080
81 // Make a unique comdat name for internal linkage things on ELF. On COFF, the
82 // name of the comdat group identifies the leader symbol of the comdat group.
83 // The linkage of the leader symbol is considered during comdat resolution,
84 // and internal symbols with the same name from different objects will not be
85 // merged.
86 if (T.isOSBinFormatELF() && F.hasLocalLinkage()) {
Kostya Serebryanybc504552018-10-12 23:21:48 +000087 if (ModuleId.empty())
88 return nullptr;
89 Name += ModuleId;
90 }
Reid Klecknerb41b3722018-11-08 00:57:33 +000091
92 // Make a new comdat for the function. Use the "no duplicates" selection kind
93 // for non-weak symbols if the object file format supports it.
94 Comdat *C = M->getOrInsertComdat(Name);
95 if (T.isOSBinFormatCOFF() && !F.isWeakForLinker())
96 C->setSelectionKind(Comdat::NoDuplicates);
97 F.setComdat(C);
98 return C;
Kostya Serebryanybc504552018-10-12 23:21:48 +000099}
100
Owen Anderson97868682010-10-07 20:17:24 +0000101/// initializeInstrumentation - Initialize all passes in the TransformUtils
102/// library.
103void llvm::initializeInstrumentation(PassRegistry &Registry) {
Leonard Chaneebecb32018-10-26 22:51:51 +0000104 initializeAddressSanitizerPass(Registry);
105 initializeAddressSanitizerModulePass(Registry);
Chandler Carruth00a301d2017-11-14 01:30:04 +0000106 initializeBoundsCheckingLegacyPassPass(Registry);
Hiroshi Yamauchi9775a622018-09-04 17:19:13 +0000107 initializeControlHeightReductionLegacyPassPass(Registry);
Xinliang David Lifb3137c2016-06-05 03:40:03 +0000108 initializeGCOVProfilerLegacyPassPass(Registry);
Xinliang David Li8aebf442016-05-06 05:49:19 +0000109 initializePGOInstrumentationGenLegacyPassPass(Registry);
Xinliang David Lid55827f2016-05-07 05:39:12 +0000110 initializePGOInstrumentationUseLegacyPassPass(Registry);
Xinliang David Li72616182016-05-15 01:04:24 +0000111 initializePGOIndirectCallPromotionLegacyPassPass(Registry);
Rong Xu48596b62017-04-04 16:42:20 +0000112 initializePGOMemOPSizeOptLegacyPassPass(Registry);
Xinliang David Lie6b89292016-04-18 17:47:38 +0000113 initializeInstrProfilingLegacyPassPass(Registry);
Evgeniy Stepanovd4bd7b72012-11-29 09:57:20 +0000114 initializeMemorySanitizerPass(Registry);
Evgeniy Stepanovc667c1f2017-12-09 00:21:41 +0000115 initializeHWAddressSanitizerPass(Registry);
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000116 initializeThreadSanitizerPass(Registry);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000117 initializeSanitizerCoverageModulePass(Registry);
Peter Collingbournee5d5b0c2013-08-07 22:47:18 +0000118 initializeDataFlowSanitizerPass(Registry);
Derek Brueningd862c172016-04-21 21:30:22 +0000119 initializeEfficiencySanitizerPass(Registry);
Owen Anderson97868682010-10-07 20:17:24 +0000120}
121
122/// LLVMInitializeInstrumentation - C binding for
123/// initializeInstrumentation.
124void LLVMInitializeInstrumentation(LLVMPassRegistryRef R) {
125 initializeInstrumentation(*unwrap(R));
126}