mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-28 11:46:09 +00:00

As part of the "RemoveDIs" project, BasicBlock::iterator now carries a debug-info bit that's needed when getFirstNonPHI and similar feed into instruction insertion positions. Call-sites where that's necessary were updated a year ago; but to ensure some type safety however, we'd like to have all calls to moveBefore use iterators. This patch adds a (guaranteed dereferenceable) iterator-taking moveBefore, and changes a bunch of call-sites where it's obviously safe to change to use it by just calling getIterator() on an instruction pointer. A follow-up patch will contain less-obviously-safe changes. We'll eventually deprecate and remove the instruction-pointer insertBefore, but not before adding concise documentation of what considerations are needed (very few).
124 lines
4.7 KiB
C++
124 lines
4.7 KiB
C++
//===-- Instrumentation.cpp - TransformUtils Infrastructure ---------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file defines the common initialization infrastructure for the
|
|
// Instrumentation library.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/Transforms/Utils/Instrumentation.h"
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
#include "llvm/IR/Module.h"
|
|
#include "llvm/TargetParser/Triple.h"
|
|
|
|
using namespace llvm;
|
|
|
|
static cl::opt<bool> ClIgnoreRedundantInstrumentation(
|
|
"ignore-redundant-instrumentation",
|
|
cl::desc("Ignore redundant instrumentation"), cl::Hidden, cl::init(false));
|
|
|
|
/// Check if module has flag attached, if not add the flag.
|
|
bool llvm::checkIfAlreadyInstrumented(Module &M, StringRef Flag) {
|
|
if (!M.getModuleFlag(Flag)) {
|
|
M.addModuleFlag(Module::ModFlagBehavior::Override, Flag, 1);
|
|
return false;
|
|
}
|
|
if (ClIgnoreRedundantInstrumentation)
|
|
return true;
|
|
std::string diagInfo =
|
|
"Redundant instrumentation detected, with module flag: " +
|
|
std::string(Flag);
|
|
M.getContext().diagnose(
|
|
DiagnosticInfoInstrumentation(diagInfo, DiagnosticSeverity::DS_Warning));
|
|
return true;
|
|
}
|
|
|
|
/// Moves I before IP. Returns new insert point.
|
|
static BasicBlock::iterator moveBeforeInsertPoint(BasicBlock::iterator I,
|
|
BasicBlock::iterator IP) {
|
|
// If I is IP, move the insert point down.
|
|
if (I == IP) {
|
|
++IP;
|
|
} else {
|
|
// Otherwise, move I before IP and return IP.
|
|
I->moveBefore(IP);
|
|
}
|
|
return IP;
|
|
}
|
|
|
|
/// Instrumentation passes often insert conditional checks into entry blocks.
|
|
/// Call this function before splitting the entry block to move instructions
|
|
/// that must remain in the entry block up before the split point. Static
|
|
/// allocas and llvm.localescape calls, for example, must remain in the entry
|
|
/// block.
|
|
BasicBlock::iterator llvm::PrepareToSplitEntryBlock(BasicBlock &BB,
|
|
BasicBlock::iterator IP) {
|
|
assert(&BB.getParent()->getEntryBlock() == &BB);
|
|
for (auto I = IP, E = BB.end(); I != E; ++I) {
|
|
bool KeepInEntry = false;
|
|
if (auto *AI = dyn_cast<AllocaInst>(I)) {
|
|
if (AI->isStaticAlloca())
|
|
KeepInEntry = true;
|
|
} else if (auto *II = dyn_cast<IntrinsicInst>(I)) {
|
|
if (II->getIntrinsicID() == llvm::Intrinsic::localescape)
|
|
KeepInEntry = true;
|
|
}
|
|
if (KeepInEntry)
|
|
IP = moveBeforeInsertPoint(I, IP);
|
|
}
|
|
return IP;
|
|
}
|
|
|
|
// Create a constant for Str so that we can pass it to the run-time lib.
|
|
GlobalVariable *llvm::createPrivateGlobalForString(Module &M, StringRef Str,
|
|
bool AllowMerging,
|
|
Twine NamePrefix) {
|
|
Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
|
|
// We use private linkage for module-local strings. If they can be merged
|
|
// with another one, we set the unnamed_addr attribute.
|
|
GlobalVariable *GV =
|
|
new GlobalVariable(M, StrConst->getType(), true,
|
|
GlobalValue::PrivateLinkage, StrConst, NamePrefix);
|
|
if (AllowMerging)
|
|
GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
|
|
GV->setAlignment(Align(1)); // Strings may not be merged w/o setting
|
|
// alignment explicitly.
|
|
return GV;
|
|
}
|
|
|
|
Comdat *llvm::getOrCreateFunctionComdat(Function &F, Triple &T) {
|
|
if (auto Comdat = F.getComdat())
|
|
return Comdat;
|
|
assert(F.hasName());
|
|
Module *M = F.getParent();
|
|
|
|
// Make a new comdat for the function. Use the "no duplicates" selection kind
|
|
// if the object file format supports it. For COFF we restrict it to non-weak
|
|
// symbols.
|
|
Comdat *C = M->getOrInsertComdat(F.getName());
|
|
if (T.isOSBinFormatELF() || (T.isOSBinFormatCOFF() && !F.isWeakForLinker()))
|
|
C->setSelectionKind(Comdat::NoDeduplicate);
|
|
F.setComdat(C);
|
|
return C;
|
|
}
|
|
|
|
void llvm::setGlobalVariableLargeSection(const Triple &TargetTriple,
|
|
GlobalVariable &GV) {
|
|
// Limit to x86-64 ELF.
|
|
if (TargetTriple.getArch() != Triple::x86_64 ||
|
|
TargetTriple.getObjectFormat() != Triple::ELF)
|
|
return;
|
|
// Limit to medium/large code models.
|
|
std::optional<CodeModel::Model> CM = GV.getParent()->getCodeModel();
|
|
if (!CM || (*CM != CodeModel::Medium && *CM != CodeModel::Large))
|
|
return;
|
|
GV.setCodeModel(CodeModel::Large);
|
|
}
|