1218885Sdim//===-- Instrumentation.cpp - TransformUtils Infrastructure ---------------===// 2218885Sdim// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6218885Sdim// 7218885Sdim//===----------------------------------------------------------------------===// 8218885Sdim// 9218885Sdim// This file defines the common initialization infrastructure for the 10218885Sdim// Instrumentation library. 11218885Sdim// 12218885Sdim//===----------------------------------------------------------------------===// 13218885Sdim 14296417Sdim#include "llvm/Transforms/Instrumentation.h" 15296417Sdim#include "llvm-c/Initialization.h" 16344779Sdim#include "llvm/ADT/Triple.h" 17296417Sdim#include "llvm/IR/IntrinsicInst.h" 18344779Sdim#include "llvm/IR/Module.h" 19218885Sdim#include "llvm/InitializePasses.h" 20251662Sdim#include "llvm/PassRegistry.h" 21218885Sdim 22218885Sdimusing namespace llvm; 23218885Sdim 24296417Sdim/// Moves I before IP. Returns new insert point. 25296417Sdimstatic BasicBlock::iterator moveBeforeInsertPoint(BasicBlock::iterator I, BasicBlock::iterator IP) { 26296417Sdim // If I is IP, move the insert point down. 27353358Sdim if (I == IP) { 28353358Sdim ++IP; 29353358Sdim } else { 30353358Sdim // Otherwise, move I before IP and return IP. 31353358Sdim I->moveBefore(&*IP); 32353358Sdim } 33296417Sdim return IP; 34296417Sdim} 35296417Sdim 36296417Sdim/// Instrumentation passes often insert conditional checks into entry blocks. 37296417Sdim/// Call this function before splitting the entry block to move instructions 38296417Sdim/// that must remain in the entry block up before the split point. Static 39296417Sdim/// allocas and llvm.localescape calls, for example, must remain in the entry 40296417Sdim/// block. 41296417SdimBasicBlock::iterator llvm::PrepareToSplitEntryBlock(BasicBlock &BB, 42296417Sdim BasicBlock::iterator IP) { 43296417Sdim assert(&BB.getParent()->getEntryBlock() == &BB); 44296417Sdim for (auto I = IP, E = BB.end(); I != E; ++I) { 45296417Sdim bool KeepInEntry = false; 46296417Sdim if (auto *AI = dyn_cast<AllocaInst>(I)) { 47296417Sdim if (AI->isStaticAlloca()) 48296417Sdim KeepInEntry = true; 49296417Sdim } else if (auto *II = dyn_cast<IntrinsicInst>(I)) { 50296417Sdim if (II->getIntrinsicID() == llvm::Intrinsic::localescape) 51296417Sdim KeepInEntry = true; 52296417Sdim } 53296417Sdim if (KeepInEntry) 54296417Sdim IP = moveBeforeInsertPoint(I, IP); 55296417Sdim } 56296417Sdim return IP; 57296417Sdim} 58296417Sdim 59344779Sdim// Create a constant for Str so that we can pass it to the run-time lib. 60344779SdimGlobalVariable *llvm::createPrivateGlobalForString(Module &M, StringRef Str, 61344779Sdim bool AllowMerging, 62344779Sdim const char *NamePrefix) { 63344779Sdim Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str); 64344779Sdim // We use private linkage for module-local strings. If they can be merged 65344779Sdim // with another one, we set the unnamed_addr attribute. 66344779Sdim GlobalVariable *GV = 67344779Sdim new GlobalVariable(M, StrConst->getType(), true, 68344779Sdim GlobalValue::PrivateLinkage, StrConst, NamePrefix); 69344779Sdim if (AllowMerging) 70344779Sdim GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); 71360784Sdim GV->setAlignment(Align::None()); // Strings may not be merged w/o setting 72360784Sdim // alignment explicitly. 73344779Sdim return GV; 74344779Sdim} 75344779Sdim 76344779SdimComdat *llvm::GetOrCreateFunctionComdat(Function &F, Triple &T, 77344779Sdim const std::string &ModuleId) { 78344779Sdim if (auto Comdat = F.getComdat()) return Comdat; 79344779Sdim assert(F.hasName()); 80344779Sdim Module *M = F.getParent(); 81344779Sdim std::string Name = F.getName(); 82344779Sdim 83344779Sdim // Make a unique comdat name for internal linkage things on ELF. On COFF, the 84344779Sdim // name of the comdat group identifies the leader symbol of the comdat group. 85344779Sdim // The linkage of the leader symbol is considered during comdat resolution, 86344779Sdim // and internal symbols with the same name from different objects will not be 87344779Sdim // merged. 88344779Sdim if (T.isOSBinFormatELF() && F.hasLocalLinkage()) { 89344779Sdim if (ModuleId.empty()) 90344779Sdim return nullptr; 91344779Sdim Name += ModuleId; 92344779Sdim } 93344779Sdim 94344779Sdim // Make a new comdat for the function. Use the "no duplicates" selection kind 95344779Sdim // for non-weak symbols if the object file format supports it. 96344779Sdim Comdat *C = M->getOrInsertComdat(Name); 97344779Sdim if (T.isOSBinFormatCOFF() && !F.isWeakForLinker()) 98344779Sdim C->setSelectionKind(Comdat::NoDuplicates); 99344779Sdim F.setComdat(C); 100344779Sdim return C; 101344779Sdim} 102344779Sdim 103218885Sdim/// initializeInstrumentation - Initialize all passes in the TransformUtils 104218885Sdim/// library. 105218885Sdimvoid llvm::initializeInstrumentation(PassRegistry &Registry) { 106353358Sdim initializeAddressSanitizerLegacyPassPass(Registry); 107353358Sdim initializeModuleAddressSanitizerLegacyPassPass(Registry); 108327952Sdim initializeBoundsCheckingLegacyPassPass(Registry); 109344779Sdim initializeControlHeightReductionLegacyPassPass(Registry); 110309124Sdim initializeGCOVProfilerLegacyPassPass(Registry); 111309124Sdim initializePGOInstrumentationGenLegacyPassPass(Registry); 112309124Sdim initializePGOInstrumentationUseLegacyPassPass(Registry); 113309124Sdim initializePGOIndirectCallPromotionLegacyPassPass(Registry); 114321369Sdim initializePGOMemOPSizeOptLegacyPassPass(Registry); 115353358Sdim initializeInstrOrderFileLegacyPassPass(Registry); 116309124Sdim initializeInstrProfilingLegacyPassPass(Registry); 117344779Sdim initializeMemorySanitizerLegacyPassPass(Registry); 118353358Sdim initializeHWAddressSanitizerLegacyPassPass(Registry); 119344779Sdim initializeThreadSanitizerLegacyPassPass(Registry); 120360784Sdim initializeModuleSanitizerCoverageLegacyPassPass(Registry); 121261991Sdim initializeDataFlowSanitizerPass(Registry); 122218885Sdim} 123218885Sdim 124218885Sdim/// LLVMInitializeInstrumentation - C binding for 125218885Sdim/// initializeInstrumentation. 126218885Sdimvoid LLVMInitializeInstrumentation(LLVMPassRegistryRef R) { 127218885Sdim initializeInstrumentation(*unwrap(R)); 128218885Sdim} 129