blob: 55b449ffca1edcb921716499027f1308a434eb92 [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 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.
27 if (I == IP)
28 return ++IP;
29 // Otherwise, move I before IP and return IP.
Duncan P. N. Exon Smithe82c2862015-10-13 17:39:10 +000030 I->moveBefore(&*IP);
Reid Klecknera57d0152015-08-14 16:45:42 +000031 return IP;
32}
33
34/// Instrumentation passes often insert conditional checks into entry blocks.
35/// Call this function before splitting the entry block to move instructions
36/// that must remain in the entry block up before the split point. Static
37/// allocas and llvm.localescape calls, for example, must remain in the entry
38/// block.
39BasicBlock::iterator llvm::PrepareToSplitEntryBlock(BasicBlock &BB,
40 BasicBlock::iterator IP) {
41 assert(&BB.getParent()->getEntryBlock() == &BB);
42 for (auto I = IP, E = BB.end(); I != E; ++I) {
43 bool KeepInEntry = false;
44 if (auto *AI = dyn_cast<AllocaInst>(I)) {
45 if (AI->isStaticAlloca())
46 KeepInEntry = true;
47 } else if (auto *II = dyn_cast<IntrinsicInst>(I)) {
48 if (II->getIntrinsicID() == llvm::Intrinsic::localescape)
49 KeepInEntry = true;
50 }
51 if (KeepInEntry)
52 IP = moveBeforeInsertPoint(I, IP);
53 }
54 return IP;
55}
56
Kostya Serebryanyd891ac92018-10-11 23:03:27 +000057// Create a constant for Str so that we can pass it to the run-time lib.
58GlobalVariable *llvm::createPrivateGlobalForString(Module &M, StringRef Str,
59 bool AllowMerging,
60 const char *NamePrefix) {
61 Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
62 // We use private linkage for module-local strings. If they can be merged
63 // with another one, we set the unnamed_addr attribute.
64 GlobalVariable *GV =
65 new GlobalVariable(M, StrConst->getType(), true,
66 GlobalValue::PrivateLinkage, StrConst, NamePrefix);
67 if (AllowMerging)
68 GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
69 GV->setAlignment(1); // Strings may not be merged w/o setting align 1.
70 return GV;
71}
72
Kostya Serebryanybc504552018-10-12 23:21:48 +000073Comdat *llvm::GetOrCreateFunctionComdat(Function &F,
74 const std::string &ModuleId) {
75 if (auto Comdat = F.getComdat()) return Comdat;
76 assert(F.hasName());
77 Module *M = F.getParent();
78 std::string Name = F.getName();
79 if (F.hasLocalLinkage()) {
80 if (ModuleId.empty())
81 return nullptr;
82 Name += ModuleId;
83 }
84 F.setComdat(M->getOrInsertComdat(Name));
85 return F.getComdat();
86}
87
Owen Anderson97868682010-10-07 20:17:24 +000088/// initializeInstrumentation - Initialize all passes in the TransformUtils
89/// library.
90void llvm::initializeInstrumentation(PassRegistry &Registry) {
Leonard Chan64e21b52018-10-11 18:31:51 +000091 initializeAddressSanitizerLegacyPassPass(Registry);
92 initializeAddressSanitizerModuleLegacyPassPass(Registry);
Chandler Carruth00a301d2017-11-14 01:30:04 +000093 initializeBoundsCheckingLegacyPassPass(Registry);
Hiroshi Yamauchi9775a622018-09-04 17:19:13 +000094 initializeControlHeightReductionLegacyPassPass(Registry);
Xinliang David Lifb3137c2016-06-05 03:40:03 +000095 initializeGCOVProfilerLegacyPassPass(Registry);
Xinliang David Li8aebf442016-05-06 05:49:19 +000096 initializePGOInstrumentationGenLegacyPassPass(Registry);
Xinliang David Lid55827f2016-05-07 05:39:12 +000097 initializePGOInstrumentationUseLegacyPassPass(Registry);
Xinliang David Li72616182016-05-15 01:04:24 +000098 initializePGOIndirectCallPromotionLegacyPassPass(Registry);
Rong Xu48596b62017-04-04 16:42:20 +000099 initializePGOMemOPSizeOptLegacyPassPass(Registry);
Xinliang David Lie6b89292016-04-18 17:47:38 +0000100 initializeInstrProfilingLegacyPassPass(Registry);
Evgeniy Stepanovd4bd7b72012-11-29 09:57:20 +0000101 initializeMemorySanitizerPass(Registry);
Evgeniy Stepanovc667c1f2017-12-09 00:21:41 +0000102 initializeHWAddressSanitizerPass(Registry);
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000103 initializeThreadSanitizerPass(Registry);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000104 initializeSanitizerCoverageModulePass(Registry);
Peter Collingbournee5d5b0c2013-08-07 22:47:18 +0000105 initializeDataFlowSanitizerPass(Registry);
Derek Brueningd862c172016-04-21 21:30:22 +0000106 initializeEfficiencySanitizerPass(Registry);
Owen Anderson97868682010-10-07 20:17:24 +0000107}
108
109/// LLVMInitializeInstrumentation - C binding for
110/// initializeInstrumentation.
111void LLVMInitializeInstrumentation(LLVMPassRegistryRef R) {
112 initializeInstrumentation(*unwrap(R));
113}