LLVM  14.0.0git
SanitizerStats.cpp
Go to the documentation of this file.
1 //===- SanitizerStats.cpp - Sanitizer statistics gathering ----------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implements code generation for sanitizer statistics gathering.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/DerivedTypes.h"
17 #include "llvm/IR/GlobalVariable.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/Module.h"
21 
22 using namespace llvm;
23 
25  StatTy = ArrayType::get(Type::getInt8PtrTy(M->getContext()), 2);
26  EmptyModuleStatsTy = makeModuleStatsTy();
27 
28  ModuleStatsGV = new GlobalVariable(*M, EmptyModuleStatsTy, false,
30 }
31 
32 ArrayType *SanitizerStatReport::makeModuleStatsArrayTy() {
33  return ArrayType::get(StatTy, Inits.size());
34 }
35 
36 StructType *SanitizerStatReport::makeModuleStatsTy() {
37  return StructType::get(M->getContext(), {Type::getInt8PtrTy(M->getContext()),
38  Type::getInt32Ty(M->getContext()),
39  makeModuleStatsArrayTy()});
40 }
41 
43  Function *F = B.GetInsertBlock()->getParent();
44  Module *M = F->getParent();
45  PointerType *Int8PtrTy = B.getInt8PtrTy();
46  IntegerType *IntPtrTy = B.getIntPtrTy(M->getDataLayout());
47  ArrayType *StatTy = ArrayType::get(Int8PtrTy, 2);
48 
49  Inits.push_back(ConstantArray::get(
50  StatTy,
51  {Constant::getNullValue(Int8PtrTy),
53  ConstantInt::get(IntPtrTy, uint64_t(SK) << (IntPtrTy->getBitWidth() -
55  Int8PtrTy)}));
56 
57  FunctionType *StatReportTy =
58  FunctionType::get(B.getVoidTy(), Int8PtrTy, false);
59  FunctionCallee StatReport =
60  M->getOrInsertFunction("__sanitizer_stat_report", StatReportTy);
61 
62  auto InitAddr = ConstantExpr::getGetElementPtr(
63  EmptyModuleStatsTy, ModuleStatsGV,
65  ConstantInt::get(IntPtrTy, 0), ConstantInt::get(B.getInt32Ty(), 2),
66  ConstantInt::get(IntPtrTy, Inits.size() - 1),
67  });
68  B.CreateCall(StatReport, ConstantExpr::getBitCast(InitAddr, Int8PtrTy));
69 }
70 
72  if (Inits.empty()) {
73  ModuleStatsGV->eraseFromParent();
74  return;
75  }
76 
77  PointerType *Int8PtrTy = Type::getInt8PtrTy(M->getContext());
79  Type *VoidTy = Type::getVoidTy(M->getContext());
80 
81  // Create a new ModuleStatsGV to replace the old one. We can't just set the
82  // old one's initializer because its type is different.
83  auto NewModuleStatsGV = new GlobalVariable(
84  *M, makeModuleStatsTy(), false, GlobalValue::InternalLinkage,
86  {Constant::getNullValue(Int8PtrTy),
87  ConstantInt::get(Int32Ty, Inits.size()),
88  ConstantArray::get(makeModuleStatsArrayTy(), Inits)}));
89  ModuleStatsGV->replaceAllUsesWith(
90  ConstantExpr::getBitCast(NewModuleStatsGV, ModuleStatsGV->getType()));
91  ModuleStatsGV->eraseFromParent();
92 
93  // Create a global constructor to register NewModuleStatsGV.
94  auto F = Function::Create(FunctionType::get(VoidTy, false),
96  auto BB = BasicBlock::Create(M->getContext(), "", F);
97  IRBuilder<> B(BB);
98 
99  FunctionType *StatInitTy = FunctionType::get(VoidTy, Int8PtrTy, false);
100  FunctionCallee StatInit =
101  M->getOrInsertFunction("__sanitizer_stat_init", StatInitTy);
102 
103  B.CreateCall(StatInit, ConstantExpr::getBitCast(NewModuleStatsGV, Int8PtrTy));
104  B.CreateRetVoid();
105 
106  appendToGlobalCtors(*M, F, 0);
107 }
Int32Ty
IntegerType * Int32Ty
Definition: NVVMIntrRange.cpp:67
llvm::GlobalVariable::eraseFromParent
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition: Globals.cpp:385
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::StructType::get
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition: Type.cpp:370
llvm::Type::getInt8PtrTy
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:255
llvm::Function
Definition: Function.h:61
llvm::IRBuilder<>
llvm::GlobalVariable
Definition: GlobalVariable.h:40
llvm::ConstantExpr::getBitCast
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2208
llvm::FunctionType::get
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:325
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Module.h
llvm::Module::getOrInsertFunction
FunctionCallee getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
Definition: Module.cpp:147
llvm::ArrayType
Class to represent array types.
Definition: DerivedTypes.h:357
llvm::Type::getInt32Ty
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:203
F
#define F(x, y, z)
Definition: MD5.cpp:56
Constants.h
llvm::ConstantExpr::getIntToPtr
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2194
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::IntegerType
Class to represent integer types.
Definition: DerivedTypes.h:40
llvm::ConstantInt::get
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:900
llvm::GlobalValue::InternalLinkage
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
llvm::SanitizerStatReport::finish
void finish()
Finalize module stats array and add global constructor to register it.
Definition: SanitizerStats.cpp:71
uint64_t
llvm::SanitizerStatKind
SanitizerStatKind
Definition: SanitizerStats.h:24
llvm::PointerType
Class to represent pointers.
Definition: DerivedTypes.h:632
llvm::ConstantStruct::getAnon
static Constant * getAnon(ArrayRef< Constant * > V, bool Packed=false)
Return an anonymous struct that has the specified elements.
Definition: Constants.h:462
llvm::Function::Create
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:138
IRBuilder.h
llvm::ArrayType::get
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:602
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
Triple.h
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::StructType
Class to represent struct types.
Definition: DerivedTypes.h:213
llvm::Value::replaceAllUsesWith
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:520
llvm::BasicBlock::Create
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:100
llvm::kSanitizerStatKindBits
@ kSanitizerStatKindBits
Definition: SanitizerStats.h:22
llvm::Constant::getNullValue
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:348
llvm::SanitizerStatReport::create
void create(IRBuilder<> &B, SanitizerStatKind SK)
Generates code into B that increments a location-specific counter tagged with the given sanitizer kin...
Definition: SanitizerStats.cpp:42
GlobalVariable.h
llvm::Module::getContext
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:261
llvm::ConstantArray::get
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1263
llvm::ConstantExpr::getGetElementPtr
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1210
llvm::Type::getVoidTy
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:186
SanitizerStats.h
ModuleUtils.h
llvm::IntegerType::getBitWidth
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:72
llvm::FunctionCallee
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:165
llvm::GlobalValue::getType
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:271
DerivedTypes.h
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
llvm::appendToGlobalCtors
void appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Append F to the list of global ctors of module M with the given Priority.
Definition: ModuleUtils.cpp:66
llvm::SanitizerStatReport::SanitizerStatReport
SanitizerStatReport(Module *M)
Definition: SanitizerStats.cpp:24
llvm::FunctionType
Class to represent function types.
Definition: DerivedTypes.h:103