1//===-- Instrumentor.cpp - Highly configurable instrumentation pass -------===//
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// The implementation of the Instrumentor, a highly configurable instrumentation
10// pass.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Transforms/IPO/Instrumentor.h"
15#include "IPOOptions.h"
16#include "llvm/Transforms/IPO/InstrumentorConfigFile.h"
17#include "llvm/Transforms/IPO/InstrumentorRuntimeHelper.h"
18#include "llvm/Transforms/IPO/InstrumentorStubPrinter.h"
19
20#include "llvm/ADT/PostOrderIterator.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/SmallPtrSet.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/StringMap.h"
26#include "llvm/ADT/iterator.h"
27#include "llvm/Analysis/ValueTracking.h"
28#include "llvm/Demangle/Demangle.h"
29#include "llvm/IR/Constant.h"
30#include "llvm/IR/Constants.h"
31#include "llvm/IR/DataLayout.h"
32#include "llvm/IR/DebugInfoMetadata.h"
33#include "llvm/IR/DiagnosticInfo.h"
34#include "llvm/IR/Dominators.h"
35#include "llvm/IR/Function.h"
36#include "llvm/IR/IRBuilder.h"
37#include "llvm/IR/InstrTypes.h"
38#include "llvm/IR/Instruction.h"
39#include "llvm/IR/Instructions.h"
40#include "llvm/IR/IntrinsicInst.h"
41#include "llvm/IR/Intrinsics.h"
42#include "llvm/IR/LLVMContext.h"
43#include "llvm/IR/Metadata.h"
44#include "llvm/IR/Module.h"
45#include "llvm/IR/PassManager.h"
46#include "llvm/IR/Verifier.h"
47#include "llvm/IRReader/IRReader.h"
48#include "llvm/Linker/Linker.h"
49#include "llvm/Support/CommandLine.h"
50#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/Regex.h"
52#include "llvm/Support/VirtualFileSystem.h"
53#include "llvm/Transforms/IPO/InstrumentorUtils.h"
54#include "llvm/Transforms/Utils/Cloning.h"
55#include "llvm/Transforms/Utils/ModuleUtils.h"
56#include "llvm/Transforms/Utils/PromoteMemToReg.h"
57
58#include <cassert>
59#include <cstdint>
60#include <functional>
61#include <iterator>
62#include <memory>
63#include <string>
64#include <type_traits>
65
66using namespace llvm;
67using namespace llvm::instrumentor;
68
69#define DEBUG_TYPE "instrumentor"
70
71namespace {
72
73/// The user option to specify input JSON files to read the configuration from.
74static cl::list<std::string>
75 ConfigFiles("instrumentor-read-config-files",
76 cl::desc("Read the instrumentor configuration from the "
77 "specified JSON files (comma separated)"),
78 cl::CommaSeparated);
79
80/// Set the debug location, if not set, after changing the insertion point of
81/// the IR builder \p IRB.
82template <typename IRBuilderTy> void ensureDbgLoc(IRBuilderTy &IRB) {
83 if (IRB.getCurrentDebugLocation())
84 return;
85 auto *BB = IRB.GetInsertBlock();
86 if (auto *SP = BB->getParent()->getSubprogram())
87 IRB.SetCurrentDebugLocation(DILocation::get(BB->getContext(), 0, 0, SP));
88}
89
90/// Attempt to cast \p V to type \p Ty using only bit-preserving casts.
91/// This ensures that floating-point values are converted via bitcast (not
92/// fptosi/fptoui) to preserve their exact bit representation.
93template <typename IRBTy>
94Value *tryToCast(IRBTy &IRB, Value *V, Type *Ty, const DataLayout &DL,
95 bool AllowTruncate = false) {
96 if (!V)
97 return Constant::getAllOnesValue(Ty);
98 Type *VTy = V->getType();
99 if (VTy == Ty)
100 return V;
101 if (VTy->isAggregateType() || VTy->isVectorTy())
102 return V;
103 if (VTy->isPointerTy() && Ty->isPointerTy())
104 return IRB.CreatePointerBitCastOrAddrSpaceCast(V, Ty);
105 TypeSize RequestedSize = DL.getTypeSizeInBits(Ty);
106 TypeSize ValueSize = DL.getTypeSizeInBits(Ty: VTy);
107 bool ShouldTruncate = RequestedSize < ValueSize;
108 if (ShouldTruncate && !AllowTruncate)
109 return V;
110 if (ShouldTruncate && AllowTruncate) {
111 // First convert to integer of the same size if needed.
112 Value *IntV = V;
113 if (VTy->isFloatingPointTy())
114 IntV = IRB.CreateBitCast(V, IRB.getIntNTy(ValueSize));
115 return tryToCast(IRB,
116 IRB.CreateIntCast(IntV, IRB.getIntNTy(RequestedSize),
117 /*IsSigned=*/false),
118 Ty, DL, AllowTruncate);
119 }
120 if (VTy->isIntegerTy() && Ty->isIntegerTy())
121 return IRB.CreateIntCast(V, Ty, /*IsSigned=*/false);
122 // Use bit-preserving casts for floating-point values: convert float to int
123 // of the same size via bitcast, then extend/truncate the integer if needed.
124 if (VTy->isFloatingPointTy() && Ty->isIntOrPtrTy()) {
125 return tryToCast(IRB, IRB.CreateBitCast(V, IRB.getIntNTy(ValueSize)), Ty,
126 DL, AllowTruncate);
127 }
128 // When converting int to float, never use sitofp/uitofp as they perform value
129 // conversion, not bit-preserving cast.
130 if (VTy->isIntegerTy() && Ty->isFloatingPointTy()) {
131 if (ValueSize == RequestedSize)
132 return IRB.CreateBitCast(V, Ty);
133 return tryToCast(
134 IRB,
135 IRB.CreateIntCast(V, IRB.getIntNTy(RequestedSize), /*IsSigned=*/false),
136 Ty, DL, AllowTruncate);
137 }
138 return IRB.CreateBitOrPointerCast(V, Ty);
139}
140
141/// Get a constant integer/boolean of type \p IT and value \p Val.
142template <typename Ty>
143Constant *getCI(Type *IT, Ty Val, bool IsSigned = false) {
144 return ConstantInt::get(IT, Val, IsSigned);
145}
146
147Constant *getSubTypeID(Type &OpTy, Type &ReqTy) {
148 switch (OpTy.getTypeID()) {
149 case Type::TypeID::ArrayTyID:
150 case Type::TypeID::FixedVectorTyID:
151 case Type::TypeID::ScalableVectorTyID:
152 return getCI(IT: &ReqTy, Val: OpTy.getContainedType(i: 0)->getTypeID());
153 default:
154 break;
155 }
156
157 return getCI(IT: &ReqTy, Val: -1, /*IsSigned=*/true);
158}
159
160/// The core of the instrumentor pass, which instruments the module as the
161/// instrumentation configuration mandates.
162class InstrumentorImpl final {
163public:
164 /// Construct an instrumentor implementation using the configuration \p IConf.
165 InstrumentorImpl(InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB,
166 Module &M)
167 : IConf(IConf), M(M), IIRB(IIRB) {}
168
169 /// Instrument the module, public entry point.
170 bool instrument();
171
172 // Reset the state to allow reuse of the instrumentor with a different
173 // configuration.
174 void clear() {
175 InstChoicesPRE.clear();
176 InstChoicesPOST.clear();
177 ParsedFunctionRegex = Regex();
178 }
179
180private:
181 /// Link the runtime bitcodes into the module. Returns true if the module
182 /// was changed.
183 bool linkRuntime();
184
185 /// Indicate if the module should be instrumented based on the target.
186 bool shouldInstrumentTarget();
187
188 /// Indicate if the function \p Fn should be instrumented.
189 bool shouldInstrumentFunction(Function &Fn);
190 bool shouldInstrumentGlobalVariable(GlobalVariable &GV);
191
192 /// Instrument instruction \p I if needed, and use the argument caches in \p
193 /// ICaches.
194 bool instrumentInstruction(Instruction &I, InstrumentationCaches &ICaches);
195
196 /// Instrument function \p Fn.
197 bool instrumentFunction(Function &Fn);
198 bool instrumentModule();
199
200 /// The instrumentation opportunities for instructions indexed by
201 /// their opcode.
202 DenseMap<unsigned, InstrumentationOpportunity *> InstChoicesPRE,
203 InstChoicesPOST;
204
205 /// The instrumentor configuration.
206 InstrumentationConfig &IConf;
207
208 /// The function regex filter, if any.
209 Regex ParsedFunctionRegex;
210
211 /// The underlying module.
212 Module &M;
213
214protected:
215 /// A special IR builder that keeps track of the inserted instructions.
216 InstrumentorIRBuilderTy &IIRB;
217};
218
219} // end anonymous namespace
220
221static Regex createRegex(StringRef Str, StringRef Name, LLVMContext &Ctx) {
222 if (!Str.empty()) {
223 Regex RX(Str);
224 std::string ErrMsg;
225 if (!RX.isValid(Error&: ErrMsg)) {
226 Ctx.diagnose(DI: DiagnosticInfoInstrumentation(
227 Twine("failed to parse ") + Name + " regex: " + ErrMsg, DS_Error));
228 return Regex();
229 }
230 return RX;
231 }
232 return Regex();
233}
234
235bool InstrumentorImpl::linkRuntime() {
236 ArrayRef<StringRef> RuntimeBitcodes = IConf.RuntimeBitcodes->getStringList();
237 if (RuntimeBitcodes.empty())
238 return false;
239
240 bool Changed = false;
241
242 for (StringRef RuntimeBitcode : RuntimeBitcodes) {
243 if (RuntimeBitcode.empty())
244 continue;
245
246 SMDiagnostic Err;
247 auto RTM = parseIRFile(Filename: RuntimeBitcode, Err, Context&: M.getContext());
248 if (!RTM) {
249 IIRB.Ctx.diagnose(DI: DiagnosticInfoInstrumentation(
250 Twine("Failed to parse runtime bitcode file '") + RuntimeBitcode +
251 Twine("': ") + Err.getMessage(),
252 DS_Error));
253 return Changed;
254 }
255
256 if (Linker::linkModules(Dest&: M, Src: std::move(RTM))) {
257 IIRB.Ctx.diagnose(DI: DiagnosticInfoInstrumentation(
258 Twine("Failed to link in runtime bitcode file '") + RuntimeBitcode +
259 "'",
260 DS_Error));
261 return Changed;
262 }
263 Changed = true;
264 }
265
266 if (!IConf.InlineRuntimeEagerly->getBool())
267 return Changed;
268
269 for (auto [I, _] : IIRB.NewInsts) {
270 auto *CI = dyn_cast<CallInst>(Val: I);
271 if (!CI || isa<IntrinsicInst>(Val: CI))
272 continue;
273
274 InlineFunctionInfo IFI;
275 auto InlineResult = InlineFunction(CB&: *CI, IFI);
276 if (!InlineResult.isSuccess()) {
277 std::string WarnMsg;
278 raw_string_ostream SS(WarnMsg);
279 SS << "Inlining of runtime call failed: "
280 << CI->getCalledFunction()->getName() << "\n";
281 SS << "Reason: " << InlineResult.getFailureReason() << "\n";
282 SS << "Signatures: " << *CI->getFunctionType() << " vs "
283 << *CI->getCalledFunction()->getFunctionType() << "\n";
284 IIRB.Ctx.diagnose(DI: DiagnosticInfoInstrumentation(WarnMsg, DS_Warning));
285 }
286 }
287
288 // Promote any eligible instrumentor-associated allocas to registers.
289 for (auto It : IIRB.AllocaMap) {
290 auto *Fn = It.first.first;
291 DominatorTree DT(*Fn);
292 auto &Allocas = *It.second;
293 erase_if(C&: Allocas,
294 P: [](const AllocaInst *AI) { return !isAllocaPromotable(AI); });
295 PromoteMemToReg(Allocas, DT);
296 delete It.second;
297 }
298 IIRB.AllocaMap.clear();
299 return Changed;
300}
301
302bool InstrumentorImpl::shouldInstrumentTarget() {
303 const Triple &T = M.getTargetTriple();
304 const bool IsGPU = T.isAMDGPU() || T.isNVPTX();
305
306 bool RegexMatches = true;
307 Regex RX = createRegex(Str: IConf.TargetRegex->getString(), Name: "target", Ctx&: IIRB.Ctx);
308 if (RX.isValid())
309 RegexMatches = RX.match(String: T.str());
310
311 // Only instrument the module if the target has to be instrumented.
312 return ((IsGPU && IConf.GPUEnabled->getBool()) ||
313 (!IsGPU && IConf.HostEnabled->getBool())) &&
314 RegexMatches;
315}
316
317bool InstrumentorImpl::shouldInstrumentFunction(Function &Fn) {
318 if (Fn.isDeclaration())
319 return false;
320 if (Fn.hasFnAttribute(Kind: Attribute::DisableSanitizerInstrumentation))
321 return false;
322 bool RegexMatches = true;
323 if (ParsedFunctionRegex.isValid())
324 RegexMatches = ParsedFunctionRegex.match(String: Fn.getName());
325 return (RegexMatches && !Fn.getName().starts_with(Prefix: IConf.getRTName())) ||
326 Fn.hasFnAttribute(Kind: "instrument");
327}
328
329bool InstrumentorImpl::shouldInstrumentGlobalVariable(GlobalVariable &GV) {
330 return !GV.getName().starts_with(Prefix: "llvm.") &&
331 !GV.getName().starts_with(Prefix: IConf.getRTName());
332}
333
334bool InstrumentorImpl::instrumentInstruction(Instruction &I,
335 InstrumentationCaches &ICaches) {
336 bool Changed = false;
337
338 // Skip instrumentation instructions.
339 if (IIRB.NewInsts.contains(Val: &I))
340 return Changed;
341
342 // Count epochs eagerly.
343 ++IIRB.Epoch;
344
345 Value *IPtr = &I;
346 if (auto *IO = InstChoicesPRE.lookup(Val: I.getOpcode())) {
347 IIRB.IRB.SetInsertPoint(&I);
348 ensureDbgLoc(IRB&: IIRB.IRB);
349 IO->instrument(V&: IPtr, Changed, IConf, IIRB, ICaches);
350 }
351
352 if (auto *IO = InstChoicesPOST.lookup(Val: I.getOpcode())) {
353 IIRB.IRB.SetInsertPoint(I.getNextNode());
354 ensureDbgLoc(IRB&: IIRB.IRB);
355 IO->instrument(V&: IPtr, Changed, IConf, IIRB, ICaches);
356 }
357 IIRB.returnAllocas();
358
359 return Changed;
360}
361
362bool InstrumentorImpl::instrumentFunction(Function &Fn) {
363 bool Changed = false;
364 if (!shouldInstrumentFunction(Fn))
365 return Changed;
366
367 InstrumentationCaches ICaches;
368 SmallVector<Instruction *> FinalTIs;
369 ReversePostOrderTraversal<Function *> RPOT(&Fn);
370 for (auto &It : RPOT) {
371 for (auto &I : *It)
372 Changed |= instrumentInstruction(I, ICaches);
373
374 auto *TI = It->getTerminator();
375 if (!TI->getNumSuccessors())
376 FinalTIs.push_back(Elt: TI);
377 }
378
379 Value *FPtr = &Fn;
380 for (auto &[Name, IO] :
381 IConf.IChoices[InstrumentationLocation::FUNCTION_PRE]) {
382 if (!IO->Enabled)
383 continue;
384 // Count epochs eagerly.
385 ++IIRB.Epoch;
386
387 IIRB.IRB.SetInsertPoint(
388 cast<Function>(Val: FPtr)->getEntryBlock().getFirstNonPHIOrAlloca());
389 ensureDbgLoc(IRB&: IIRB.IRB);
390 IO->instrument(V&: FPtr, Changed, IConf, IIRB, ICaches);
391 IIRB.returnAllocas();
392 }
393
394 for (auto &[Name, IO] :
395 IConf.IChoices[InstrumentationLocation::FUNCTION_POST]) {
396 if (!IO->Enabled)
397 continue;
398 // Count epochs eagerly.
399 ++IIRB.Epoch;
400
401 for (Instruction *FinalTI : FinalTIs) {
402 IIRB.IRB.SetInsertPoint(FinalTI);
403 ensureDbgLoc(IRB&: IIRB.IRB);
404 IO->instrument(V&: FPtr, Changed, IConf, IIRB, ICaches);
405 IIRB.returnAllocas();
406 }
407 }
408 return Changed;
409}
410
411bool InstrumentorImpl::instrumentModule() {
412 SmallVector<GlobalVariable *> Globals;
413 Globals.reserve(N: M.global_size());
414 for (GlobalVariable &GV : M.globals()) {
415 // llvm.metadata contains globals such as llvm.used.
416 if (GV.getSection() == "llvm.metadata" ||
417 GV.getName() == "llvm.global_dtors" ||
418 GV.getName() == "llvm.global_ctors")
419 continue;
420 Globals.push_back(Elt: &GV);
421 }
422
423 auto CreateYtor = [&](bool Ctor) {
424 Function *YtorFn = Function::Create(
425 Ty: FunctionType::get(Result: IIRB.VoidTy, isVarArg: false), Linkage: GlobalValue::PrivateLinkage,
426 N: IConf.getRTName(Prefix: Ctor ? "ctor" : "dtor", Name: ""), M);
427
428 auto *EntryBB = BasicBlock::Create(Context&: IIRB.Ctx, Name: "entry", Parent: YtorFn);
429 IIRB.IRB.SetInsertPoint(EntryBB->begin());
430 ensureDbgLoc(IRB&: IIRB.IRB);
431 IIRB.IRB.CreateRetVoid();
432
433 if (Ctor)
434 appendToGlobalCtors(M, F: YtorFn, Priority: 1000);
435 else
436 appendToGlobalDtors(M, F: YtorFn, Priority: 1000);
437 return YtorFn;
438 };
439
440 InstrumentationCaches ICaches;
441
442 Function *CtorFn = nullptr, *DtorFn = nullptr;
443 bool Changed = false;
444 for (auto Loc : {InstrumentationLocation::MODULE_PRE,
445 InstrumentationLocation::MODULE_POST}) {
446 bool IsPRE = InstrumentationLocation::isPRE(Kind: Loc);
447 Function *&YtorFn = IsPRE ? CtorFn : DtorFn;
448 for (auto &ChoiceIt : IConf.IChoices[Loc]) {
449 auto *IO = ChoiceIt.second;
450 if (!IO->Enabled)
451 continue;
452 if (!YtorFn) {
453 YtorFn = CreateYtor(IsPRE);
454 Changed = true;
455 }
456 IIRB.IRB.SetInsertPointPastAllocas(YtorFn);
457 ensureDbgLoc(IRB&: IIRB.IRB);
458 Value *YtorPtr = YtorFn;
459
460 // Count epochs eagerly.
461 ++IIRB.Epoch;
462
463 IO->instrument(V&: YtorPtr, Changed, IConf, IIRB, ICaches);
464 IIRB.returnAllocas();
465 }
466 }
467
468 for (auto Loc : {InstrumentationLocation::GLOBAL_PRE,
469 InstrumentationLocation::GLOBAL_POST}) {
470 bool IsPRE = InstrumentationLocation::isPRE(Kind: Loc);
471 Function *&YtorFn = IsPRE ? CtorFn : DtorFn;
472 for (auto &ChoiceIt : IConf.IChoices[Loc]) {
473 auto *IO = ChoiceIt.second;
474 if (!IO->Enabled)
475 continue;
476 if (!YtorFn) {
477 YtorFn = CreateYtor(IsPRE);
478 Changed = true;
479 }
480 for (GlobalVariable *GV : Globals) {
481 if (!shouldInstrumentGlobalVariable(GV&: *GV))
482 continue;
483 if (IsPRE)
484 IIRB.IRB.SetInsertPoint(YtorFn->getEntryBlock().getTerminator());
485 else
486 IIRB.IRB.SetInsertPointPastAllocas(YtorFn);
487 ensureDbgLoc(IRB&: IIRB.IRB);
488 Value *GVPtr = GV;
489
490 // Count epochs eagerly.
491 ++IIRB.Epoch;
492
493 IO->instrument(V&: GVPtr, Changed, IConf, IIRB, ICaches);
494 IIRB.returnAllocas();
495 }
496 }
497 }
498
499 return Changed;
500}
501
502bool InstrumentorImpl::instrument() {
503 bool Changed = false;
504 if (!shouldInstrumentTarget())
505 return Changed;
506
507 StringRef FunctionRegexStr = IConf.FunctionRegex->getString();
508 ParsedFunctionRegex = createRegex(Str: FunctionRegexStr, Name: "function", Ctx&: IIRB.Ctx);
509
510 // Helper to register an IO for all its opcodes.
511 auto RegisterForAllOpcodes = [](auto &InstChoices,
512 InstrumentationOpportunity *IO) {
513 ArrayRef<unsigned> Opcodes = IO->getAllOpcodes();
514 // Register for all opcodes.
515 for (unsigned Opcode : Opcodes)
516 InstChoices[Opcode] = IO;
517 };
518
519 for (auto &[Name, IO] :
520 IConf.IChoices[InstrumentationLocation::INSTRUCTION_PRE])
521 if (IO->Enabled)
522 RegisterForAllOpcodes(InstChoicesPRE, IO);
523 for (auto &[Name, IO] :
524 IConf.IChoices[InstrumentationLocation::INSTRUCTION_POST])
525 if (IO->Enabled)
526 RegisterForAllOpcodes(InstChoicesPOST, IO);
527 Changed |= instrumentModule();
528
529 for (Function &Fn : M)
530 Changed |= instrumentFunction(Fn);
531
532 Changed |= linkRuntime();
533
534 return Changed;
535}
536
537InstrumentorPass::InstrumentorPass(IntrusiveRefCntPtr<vfs::FileSystem> FS,
538 InstrumentationConfig *IC,
539 InstrumentorIRBuilderTy *IIRB)
540 : FS(FS), UserIConf(IC), UserIIRB(IIRB) {
541 if (!FS)
542 this->FS = vfs::getRealFileSystem();
543}
544
545PreservedAnalyses InstrumentorPass::run(Module &M, InstrumentationConfig &IConf,
546 InstrumentorIRBuilderTy &IIRB,
547 bool ReadConfig) {
548 const IPOOptions &Opts = getIPOOptions(M);
549 bool Changed = false;
550 InstrumentorImpl Impl(IConf, IIRB, M);
551
552 // If this is a configuration driven run, iterate over all configurations
553 // provided by the user, if not, use the config as is and run the instrumentor
554 // once.
555 if (ReadConfig)
556 readConfigPathsFile(InputFile: Opts.instrumentor_read_config_paths_file, Configs&: ConfigFiles,
557 Ctx&: IIRB.Ctx, FS&: *FS);
558
559 bool MultipleConfigs = ConfigFiles.size() > 1;
560 unsigned Idx = 0;
561 do {
562 std::string ConfigFile =
563 ReadConfig && !ConfigFiles.empty() ? ConfigFiles[Idx] : "";
564
565 // Initialize the config to the base state but keep the caches around.
566 Impl.clear();
567 IConf.init(IIRB);
568
569 if (!readConfigFromJSON(IConf, InputFile: ConfigFile, Ctx&: IIRB.Ctx, FS&: *FS))
570 continue;
571
572 writeConfigToJSON(
573 IConf,
574 OutputFile: MultipleConfigs
575 ? (Opts.instrumentor_write_config_file + "." + std::to_string(val: Idx))
576 .str()
577 : Opts.instrumentor_write_config_file.str(),
578 Ctx&: IIRB.Ctx);
579
580 printRuntimeStub(IConf, StubRuntimeName: IConf.RuntimeStubsFile->getString(), Ctx&: IIRB.Ctx);
581
582 Changed |= Impl.instrument();
583 } while (++Idx < ConfigFiles.size());
584
585 if (!Changed)
586 return PreservedAnalyses::all();
587 return PreservedAnalyses::none();
588}
589
590PreservedAnalyses InstrumentorPass::run(Module &M, ModuleAnalysisManager &MAM) {
591 // Only create them if the user did not provide them.
592 std::unique_ptr<InstrumentationConfig> IConfInt(
593 !UserIConf ? new InstrumentationConfig() : nullptr);
594 std::unique_ptr<InstrumentorIRBuilderTy> IIRBInt(
595 !UserIIRB ? new InstrumentorIRBuilderTy(M) : nullptr);
596
597 auto *IConf = IConfInt ? IConfInt.get() : UserIConf;
598 auto *IIRB = IIRBInt ? IIRBInt.get() : UserIIRB;
599
600 auto PA = run(M, IConf&: *IConf, IIRB&: *IIRB, ReadConfig: !UserIConf);
601
602 assert(!verifyModule(M, &errs()));
603 return PA;
604}
605
606std::unique_ptr<BaseConfigurationOption>
607BaseConfigurationOption::createBoolOption(InstrumentationConfig &IConf,
608 StringRef Name, StringRef Description,
609 bool DefaultValue) {
610 auto BCO =
611 std::make_unique<BaseConfigurationOption>(args&: Name, args&: Description, args: BOOLEAN);
612 BCO->setBool(DefaultValue);
613 IConf.addBaseChoice(BCO: BCO.get());
614 return BCO;
615}
616
617std::unique_ptr<BaseConfigurationOption>
618BaseConfigurationOption::createStringOption(InstrumentationConfig &IConf,
619 StringRef Name,
620 StringRef Description,
621 StringRef DefaultValue) {
622 auto BCO =
623 std::make_unique<BaseConfigurationOption>(args&: Name, args&: Description, args: STRING);
624 BCO->setString(DefaultValue);
625 IConf.addBaseChoice(BCO: BCO.get());
626 return BCO;
627}
628
629std::unique_ptr<BaseConfigurationOption>
630BaseConfigurationOption::createStringListOption(
631 InstrumentationConfig &IConf, StringRef Name, StringRef Description,
632 ArrayRef<StringRef> DefaultValue) {
633 auto BCO =
634 std::make_unique<BaseConfigurationOption>(args&: Name, args&: Description, args: STRING_LIST);
635 BCO->setStringList(DefaultValue);
636 IConf.addBaseChoice(BCO: BCO.get());
637 return BCO;
638}
639
640void InstrumentationConfig::populate(InstrumentorIRBuilderTy &IIRB) {
641 /// List of all instrumentation opportunities.
642 BasePointerIO::populate(IConf&: *this, IIRB);
643 ModuleIO::populate(IConf&: *this, IIRB);
644 GlobalVarIO::populate(IConf&: *this, IIRB);
645 FunctionIO::populate(IConf&: *this, IIRB);
646 AllocaIO::populate(IConf&: *this, IIRB);
647 UnreachableIO::populate(IConf&: *this, IIRB);
648 LoadIO::populate(IConf&: *this, IIRB);
649 StoreIO::populate(IConf&: *this, IIRB);
650 CastIO::populate(IConf&: *this, IIRB);
651 NumericIO::populate(IConf&: *this, IIRB);
652 CompareIO::populate(IConf&: *this, IIRB);
653}
654
655void InstrumentationConfig::addChoice(InstrumentationOpportunity &IO,
656 LLVMContext &Ctx) {
657 auto *&ICPtr = IChoices[IO.getLocationKind()][IO.getName()];
658 if (ICPtr) {
659 Ctx.diagnose(DI: DiagnosticInfoInstrumentation(
660 Twine("registered two instrumentation opportunities for the same "
661 "location (") +
662 ICPtr->getName() + Twine(" vs ") + IO.getName() + Twine(")"),
663 DS_Warning));
664 }
665 ICPtr = &IO;
666}
667
668Value *
669InstrumentationConfig::getBasePointerInfo(Value &V,
670 InstrumentorIRBuilderTy &IIRB) {
671 Function *Fn = IIRB.IRB.GetInsertBlock()->getParent();
672
673 Value *Obj;
674 {
675 Value *&UnderlyingObj = UnderlyingObjsMap[&V];
676 if (!UnderlyingObj)
677 UnderlyingObj = const_cast<Value *>(getUnderlyingObjectAggressive(V: &V));
678 Obj = UnderlyingObj;
679 }
680
681 Value *&BPI = BasePointerInfoMap[{Obj, Fn}];
682 if (BPI)
683 return BPI;
684
685 auto *BPIO =
686 IChoices[InstrumentationLocation::SPECIAL_VALUE]["base_pointer_info"];
687 if (!BPIO || !BPIO->Enabled) {
688 IIRB.Ctx.diagnose(DI: DiagnosticInfoInstrumentation(
689 "Base pointer info disabled but required, passing nullptr.",
690 DS_Warning));
691 return BPI = Constant::getNullValue(Ty: BPIO->getRetTy(Ctx&: IIRB.Ctx));
692 }
693
694 IRBuilderBase::InsertPointGuard IP(IIRB.IRB);
695 if (auto *BasePtrI = dyn_cast<Instruction>(Val: Obj)) {
696 std::optional<BasicBlock::iterator> IP =
697 BasePtrI->getInsertionPointAfterDef();
698 if (IP) {
699 IIRB.IRB.SetInsertPoint(*IP);
700 } else {
701 IIRB.Ctx.diagnose(DI: DiagnosticInfoInstrumentation(
702 "Base pointer info could not be placed, passing nullptr.",
703 DS_Warning));
704 return BPI = Constant::getNullValue(Ty: BPIO->getRetTy(Ctx&: IIRB.Ctx));
705 }
706 } else if (isa<Constant>(Val: Obj) || isa<Argument>(Val: Obj)) {
707 IIRB.IRB.SetInsertPointPastAllocas(IIRB.IRB.GetInsertBlock()->getParent());
708 } else {
709 LLVM_DEBUG(Obj->dump());
710 llvm_unreachable("Unexpected base pointer!");
711 }
712 ensureDbgLoc(IRB&: IIRB.IRB);
713
714 // Use fresh caches for safety, as this function may be called from
715 // another instrumentation opportunity.
716 bool Changed;
717 InstrumentationCaches ICaches;
718 BPI = BPIO->instrument(V&: Obj, Changed, IConf&: *this, IIRB, ICaches);
719 IIRB.returnAllocas();
720 if (!BPI)
721 BPI = Constant::getNullValue(Ty: BPIO->getRetTy(Ctx&: IIRB.Ctx));
722 return BPI;
723}
724
725Value *InstrumentationOpportunity::getIdPre(Value &V, Type &Ty,
726 InstrumentationConfig &IConf,
727 InstrumentorIRBuilderTy &IIRB) {
728 return getCI(IT: &Ty, Val: getIdFromEpoch(CurrentEpoch: IIRB.Epoch));
729}
730
731Value *InstrumentationOpportunity::getIdPost(Value &V, Type &Ty,
732 InstrumentationConfig &IConf,
733 InstrumentorIRBuilderTy &IIRB) {
734 return getCI(IT: &Ty, Val: -getIdFromEpoch(CurrentEpoch: IIRB.Epoch), /*IsSigned=*/true);
735}
736
737Value *InstrumentationOpportunity::forceCast(Value &V, Type &Ty,
738 InstrumentorIRBuilderTy &IIRB) {
739 if (V.getType()->isVoidTy())
740 return Ty.isVoidTy() ? &V : Constant::getNullValue(Ty: &Ty);
741 return tryToCast(IRB&: IIRB.IRB, V: &V, Ty: &Ty, DL: IIRB.IRB.getDataLayout());
742}
743
744Value *InstrumentationOpportunity::replaceValue(Value &V, Value &NewV,
745 InstrumentationConfig &IConf,
746 InstrumentorIRBuilderTy &IIRB) {
747 if (V.getType()->isVoidTy())
748 return &V;
749
750 auto *NewVCasted = &NewV;
751 if (auto *I = dyn_cast<Instruction>(Val: &NewV)) {
752 IRBuilderBase::InsertPointGuard IPG(IIRB.IRB);
753 IIRB.IRB.SetInsertPoint(I->getNextNode());
754 ensureDbgLoc(IRB&: IIRB.IRB);
755 NewVCasted = tryToCast(IRB&: IIRB.IRB, V: &NewV, Ty: V.getType(), DL: IIRB.DL,
756 /*AllowTruncate=*/true);
757 }
758 V.replaceUsesWithIf(New: NewVCasted, ShouldReplace: [&](Use &U) {
759 if (IIRB.NewInsts.lookup(Val: cast<Instruction>(Val: U.getUser())) == IIRB.Epoch)
760 return false;
761 return !isa<LifetimeIntrinsic>(Val: U.getUser()) && !U.getUser()->isDroppable();
762 });
763
764 return &V;
765}
766
767IRTCallDescription::IRTCallDescription(InstrumentationOpportunity &IO,
768 Type *RetTy)
769 : IO(IO), RetTy(RetTy) {
770 for (auto &It : IO.IRTArgs) {
771 if (!It.Enabled)
772 continue;
773 NumReplaceableArgs += bool(It.Flags & IRTArg::REPLACABLE);
774 MightRequireIndirection |= It.Flags & IRTArg::POTENTIALLY_INDIRECT;
775 }
776 if (NumReplaceableArgs > 1)
777 MightRequireIndirection = RequiresIndirection = true;
778}
779
780FunctionType *IRTCallDescription::createLLVMSignature(
781 InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB,
782 const DataLayout &DL, bool ForceIndirection) {
783 assert(((ForceIndirection && MightRequireIndirection) ||
784 (!ForceIndirection && !RequiresIndirection)) &&
785 "Wrong indirection setting!");
786
787 SmallVector<Type *> ParamTypes;
788 for (auto &It : IO.IRTArgs) {
789 if (!It.Enabled)
790 continue;
791 if (!ForceIndirection || !isPotentiallyIndirect(IRTA&: It)) {
792 ParamTypes.push_back(Elt: It.Ty);
793 if (!RetTy && NumReplaceableArgs == 1 && (It.Flags & IRTArg::REPLACABLE))
794 RetTy = It.Ty;
795 continue;
796 }
797
798 // The indirection pointer and the size of the value.
799 ParamTypes.push_back(Elt: IIRB.PtrTy);
800 if (!(It.Flags & IRTArg::INDIRECT_HAS_SIZE))
801 ParamTypes.push_back(Elt: IIRB.Int32Ty);
802 }
803 if (!RetTy)
804 RetTy = IIRB.VoidTy;
805
806 return FunctionType::get(Result: RetTy, Params: ParamTypes, /*isVarArg=*/false);
807}
808
809CallInst *IRTCallDescription::createLLVMCall(Value *&V,
810 InstrumentationConfig &IConf,
811 InstrumentorIRBuilderTy &IIRB,
812 const DataLayout &DL,
813 InstrumentationCaches &ICaches) {
814 SmallVector<Value *> CallParams;
815
816 IRBuilderBase::InsertPointGuard IRP(IIRB.IRB);
817 auto IP = IIRB.IRB.GetInsertPoint();
818
819 bool ForceIndirection = RequiresIndirection;
820 for (auto &It : IO.IRTArgs) {
821 if (!It.Enabled)
822 continue;
823 auto *&Param = ICaches.DirectArgCache[{IIRB.Epoch, IO.getName(), It.Name}];
824 if (!Param || It.NoCache)
825 // Avoid passing the caches to the getter.
826 Param = It.GetterCB(*V, *It.Ty, IConf, IIRB);
827 assert(Param);
828
829 if (Param->getType()->isVoidTy()) {
830 Param = Constant::getNullValue(Ty: It.Ty);
831 } else if (Param->getType()->isAggregateType() ||
832 Param->getType()->isVectorTy() ||
833 DL.getTypeSizeInBits(Ty: Param->getType()) >
834 DL.getTypeSizeInBits(Ty: It.Ty)) {
835 if (!isPotentiallyIndirect(IRTA&: It)) {
836 IIRB.Ctx.diagnose(DI: DiagnosticInfoInstrumentation(
837 Twine("indirection needed for ") + It.Name + Twine(" in ") +
838 IO.getName() +
839 Twine(", but not indicated. Instrumentation is skipped"),
840 DS_Warning));
841 return nullptr;
842 }
843 ForceIndirection = true;
844 } else {
845 Param = tryToCast(IRB&: IIRB.IRB, V: Param, Ty: It.Ty, DL);
846 }
847 CallParams.push_back(Elt: Param);
848 }
849
850 if (ForceIndirection) {
851 Function *Fn = IIRB.IRB.GetInsertBlock()->getParent();
852
853 unsigned Offset = 0;
854 for (auto &It : IO.IRTArgs) {
855 if (!It.Enabled)
856 continue;
857
858 if (!isPotentiallyIndirect(IRTA&: It)) {
859 ++Offset;
860 continue;
861 }
862 auto *&CallParam = CallParams[Offset++];
863 if (!(It.Flags & IRTArg::INDIRECT_HAS_SIZE)) {
864 CallParams.insert(I: &CallParam + 1, Elt: IIRB.IRB.getInt32(C: DL.getTypeStoreSize(
865 Ty: CallParam->getType())));
866 Offset += 1;
867 }
868
869 auto *&CachedParam =
870 ICaches.IndirectArgCache[{IIRB.Epoch, IO.getName(), It.Name}];
871 if (CachedParam) {
872 CallParam = CachedParam;
873 continue;
874 }
875
876 auto *AI = IIRB.getAlloca(Fn, Ty: CallParam->getType());
877 IIRB.IRB.CreateStore(Val: CallParam, Ptr: AI);
878 CallParam = CachedParam = tryToCast(IRB&: IIRB.IRB, V: AI, Ty: IIRB.PtrTy, DL);
879 }
880 }
881
882 if (!ForceIndirection)
883 IIRB.IRB.SetInsertPoint(IP);
884 ensureDbgLoc(IRB&: IIRB.IRB);
885
886 auto *FnTy = createLLVMSignature(IConf, IIRB, DL, ForceIndirection);
887 auto CompleteName =
888 IConf.getRTName(Prefix: IO.IP.isPRE() ? "pre_" : "post_", Name: IO.getName(),
889 Suffix1: ForceIndirection ? "_ind" : "");
890 auto FC = IIRB.IRB.getModule()->getOrInsertFunction(Name: CompleteName, T: FnTy);
891 auto *CI = IIRB.IRB.CreateCall(Callee: FC, Args: CallParams);
892 CI->addFnAttr(Attr: Attribute::get(Context&: IIRB.Ctx, Kind: Attribute::WillReturn));
893
894 for (unsigned I = 0, E = IO.IRTArgs.size(); I < E; ++I) {
895 if (!IO.IRTArgs[I].Enabled)
896 continue;
897 if (!isReplacable(IRTA&: IO.IRTArgs[I]))
898 continue;
899 bool IsCustomReplaceable = IO.IRTArgs[I].Flags & IRTArg::REPLACABLE_CUSTOM;
900 Value *NewValue = FnTy->isVoidTy() || IsCustomReplaceable
901 ? ICaches.DirectArgCache[{IIRB.Epoch, IO.getName(),
902 IO.IRTArgs[I].Name}]
903 : CI;
904 assert(NewValue);
905 if (ForceIndirection && !IsCustomReplaceable &&
906 isPotentiallyIndirect(IRTA&: IO.IRTArgs[I])) {
907 auto *Q =
908 ICaches
909 .IndirectArgCache[{IIRB.Epoch, IO.getName(), IO.IRTArgs[I].Name}];
910 NewValue = IIRB.IRB.CreateLoad(Ty: V->getType(), Ptr: Q);
911 }
912 V = IO.IRTArgs[I].SetterCB(*V, *NewValue, IConf, IIRB);
913 }
914 return CI;
915}
916
917template <typename Ty> constexpr static Value *getValue(Ty &ValueOrUse) {
918 if constexpr (std::is_same<Ty, Use>::value)
919 return ValueOrUse.get();
920 else
921 return static_cast<Value *>(&ValueOrUse);
922}
923
924template <typename Range>
925static Value *createValuePack(const Range &R, InstrumentationConfig &IConf,
926 InstrumentorIRBuilderTy &IIRB) {
927 auto *Fn = IIRB.IRB.GetInsertBlock()->getParent();
928 auto *I32Ty = IIRB.IRB.getInt32Ty();
929 SmallVector<Constant *> ConstantValues;
930 SmallVector<std::pair<Value *, uint32_t>> Values;
931 SmallVector<Type *> Types;
932 for (auto &RE : R) {
933 Value *V = getValue(RE);
934 if (!V->getType()->isSized())
935 continue;
936 auto VSize = IIRB.DL.getTypeAllocSize(Ty: V->getType());
937 ConstantValues.push_back(Elt: getCI(IT: I32Ty, Val: VSize));
938 Types.push_back(Elt: I32Ty);
939 ConstantValues.push_back(Elt: getCI(IT: I32Ty, Val: V->getType()->getTypeID()));
940 Types.push_back(Elt: I32Ty);
941 if (uint32_t MisAlign = VSize % 8) {
942 Types.push_back(Elt: ArrayType::get(ElementType: IIRB.Int8Ty, NumElements: 8 - MisAlign));
943 ConstantValues.push_back(Elt: ConstantArray::getNullValue(Ty: Types.back()));
944 }
945 Types.push_back(Elt: V->getType());
946 if (auto *C = dyn_cast<Constant>(Val: V)) {
947 ConstantValues.push_back(Elt: C);
948 continue;
949 }
950 Values.push_back(Elt: {V, ConstantValues.size()});
951 ConstantValues.push_back(Elt: Constant::getNullValue(Ty: V->getType()));
952 }
953 if (Types.empty())
954 return ConstantPointerNull::get(T: IIRB.PtrTy);
955
956 StructType *STy = StructType::get(Context&: Fn->getContext(), Elements: Types, /*isPacked=*/true);
957 Constant *Initializer = ConstantStruct::get(T: STy, V: ConstantValues);
958
959 GlobalVariable *&GV = IConf.ConstantGlobalsCache[Initializer];
960 if (!GV)
961 GV = new GlobalVariable(*Fn->getParent(), STy, false,
962 GlobalValue::InternalLinkage, Initializer,
963 IConf.getRTName(Prefix: "", Name: "value_pack"));
964
965 auto *AI = IIRB.getAlloca(Fn, Ty: STy);
966 IIRB.IRB.CreateMemCpy(Dst: AI, DstAlign: AI->getAlign(), Src: GV, SrcAlign: GV->getAlign(),
967 Size: IIRB.DL.getTypeAllocSize(Ty: STy));
968 for (auto [Param, Idx] : Values) {
969 auto *Ptr = IIRB.IRB.CreateStructGEP(Ty: STy, Ptr: AI, Idx);
970 IIRB.IRB.CreateStore(Val: Param, Ptr);
971 }
972 return AI;
973}
974
975template <typename Range>
976static void readValuePack(const Range &R, Value &Pack,
977 InstrumentorIRBuilderTy &IIRB,
978 function_ref<void(int, Value *)> SetterCB) {
979 auto *Fn = IIRB.IRB.GetInsertBlock()->getParent();
980 auto &DL = Fn->getDataLayout();
981 unsigned Offset = 0;
982 for (const auto &[Idx, RE] : enumerate(R)) {
983 Value *V = getValue(RE);
984 if (!V->getType()->isSized())
985 continue;
986 Offset += 8;
987 auto VSize = DL.getTypeAllocSize(Ty: V->getType());
988 auto Padding = alignTo(Size: VSize, Align: 8) - VSize;
989 Offset += Padding;
990 auto *Ptr = IIRB.IRB.CreateConstInBoundsGEP1_32(Ty: IIRB.Int8Ty, Ptr: &Pack, Idx0: Offset);
991 auto *NewV = IIRB.IRB.CreateLoad(Ty: V->getType(), Ptr);
992 SetterCB(Idx, NewV);
993 Offset += VSize;
994 }
995}
996
997Value *BaseInstructionIO::getOpcode(Value &V, Type &Ty,
998 InstrumentationConfig &IConf,
999 InstrumentorIRBuilderTy &IIRB) {
1000 auto &I = cast<Instruction>(Val&: V);
1001 return getCI(IT: &Ty, Val: I.getOpcode());
1002}
1003
1004Value *BaseInstructionIO::getTypeSize(Value &V, Type &Ty,
1005 InstrumentationConfig &IConf,
1006 InstrumentorIRBuilderTy &IIRB) {
1007 auto &I = cast<Instruction>(Val&: V);
1008 auto &DL = I.getDataLayout();
1009 return getCI(IT: &Ty, Val: DL.getTypeStoreSize(Ty: V.getType()));
1010}
1011
1012Value *BaseInstructionIO::getLeftOperand(Value &V, Type &Ty,
1013 InstrumentationConfig &IConf,
1014 InstrumentorIRBuilderTy &IIRB) {
1015 auto &I = cast<Instruction>(Val&: V);
1016 return I.getOperand(i: 0);
1017}
1018
1019Value *BaseInstructionIO::getRightOperand(Value &V, Type &Ty,
1020 InstrumentationConfig &IConf,
1021 InstrumentorIRBuilderTy &IIRB) {
1022 auto &I = cast<Instruction>(Val&: V);
1023 if (I.getNumOperands() > 1)
1024 return I.getOperand(i: 1);
1025 return PoisonValue::get(T: &Ty);
1026}
1027
1028Value *BaseInstructionIO::getTypeId(Value &V, Type &Ty,
1029 InstrumentationConfig &IConf,
1030 InstrumentorIRBuilderTy &IIRB) {
1031 return getCI(IT: &Ty, Val: V.getType()->getTypeID());
1032}
1033
1034Value *BaseInstructionIO::getSubTypeId(Value &V, Type &Ty,
1035 InstrumentationConfig &IConf,
1036 InstrumentorIRBuilderTy &IIRB) {
1037 return getSubTypeID(OpTy&: *V.getType(), ReqTy&: Ty);
1038}
1039
1040/// FunctionIO
1041/// {
1042void FunctionIO::init(InstrumentationConfig &IConf,
1043 InstrumentorIRBuilderTy &IIRB, ConfigTy *UserConfig) {
1044 using namespace std::placeholders;
1045 if (UserConfig)
1046 Config = *UserConfig;
1047
1048 bool IsPRE = getLocationKind() == InstrumentationLocation::FUNCTION_PRE;
1049 if (Config.has(Opt: PassAddress))
1050 IRTArgs.push_back(Elt: IRTArg(IIRB.PtrTy, "address", "The function address.",
1051 IRTArg::NONE, getFunctionAddress));
1052 if (Config.has(Opt: PassName))
1053 IRTArgs.push_back(Elt: IRTArg(IIRB.PtrTy, "name", "The function name.",
1054 IRTArg::STRING, getFunctionName));
1055 if (Config.has(Opt: PassNumArguments))
1056 IRTArgs.push_back(
1057 Elt: IRTArg(IIRB.Int32Ty, "num_arguments",
1058 "Number of function arguments (without varargs).", IRTArg::NONE,
1059 std::bind(f: &FunctionIO::getNumArguments, args: this, args: _1, args: _2, args: _3, args: _4)));
1060 if (Config.has(Opt: PassArguments))
1061 IRTArgs.push_back(Elt: IRTArg(
1062 IIRB.PtrTy, "arguments", "Description of the arguments.",
1063 (IsPRE && Config.has(Opt: ReplaceArguments) ? IRTArg::REPLACABLE_CUSTOM
1064 : IRTArg::NONE) |
1065 IRTArg::VALUE_PACK,
1066 std::bind(f: &FunctionIO::getArguments, args: this, args: _1, args: _2, args: _3, args: _4),
1067 std::bind(f: &FunctionIO::setArguments, args: this, args: _1, args: _2, args: _3, args: _4)));
1068 if (Config.has(Opt: PassIsMain))
1069 IRTArgs.push_back(Elt: IRTArg(IIRB.Int8Ty, "is_main",
1070 "Flag to indicate it is the main function.",
1071 IRTArg::NONE, isMainFunction));
1072 addCommonArgs(IConf, Ctx&: IIRB.Ctx, PassId: Config.has(Opt: PassId));
1073 IConf.addChoice(IO&: *this, Ctx&: IIRB.Ctx);
1074}
1075
1076Value *FunctionIO::getFunctionAddress(Value &V, Type &Ty,
1077 InstrumentationConfig &IConf,
1078 InstrumentorIRBuilderTy &IIRB) {
1079 auto &Fn = cast<Function>(Val&: V);
1080 if (Fn.isIntrinsic())
1081 return Constant::getNullValue(Ty: &Ty);
1082 return &V;
1083}
1084Value *FunctionIO::getFunctionName(Value &V, Type &Ty,
1085 InstrumentationConfig &IConf,
1086 InstrumentorIRBuilderTy &IIRB) {
1087 auto &Fn = cast<Function>(Val&: V);
1088 return IConf.getGlobalString(S: IConf.DemangleFunctionNames->getBool()
1089 ? demangle(MangledName: Fn.getName())
1090 : Fn.getName(),
1091 IIRB);
1092}
1093Value *FunctionIO::getNumArguments(Value &V, Type &Ty,
1094 InstrumentationConfig &IConf,
1095 InstrumentorIRBuilderTy &IIRB) {
1096 auto &Fn = cast<Function>(Val&: V);
1097 if (!Config.ArgFilter)
1098 return getCI(IT: &Ty, Val: Fn.arg_size());
1099 auto FRange = make_filter_range(Range: Fn.args(), Pred: Config.ArgFilter);
1100 return getCI(IT: &Ty, Val: std::distance(first: FRange.begin(), last: FRange.end()));
1101}
1102Value *FunctionIO::getArguments(Value &V, Type &Ty,
1103 InstrumentationConfig &IConf,
1104 InstrumentorIRBuilderTy &IIRB) {
1105 auto &Fn = cast<Function>(Val&: V);
1106 if (!Config.ArgFilter)
1107 return createValuePack(R: Fn.args(), IConf, IIRB);
1108 return createValuePack(R: make_filter_range(Range: Fn.args(), Pred: Config.ArgFilter), IConf,
1109 IIRB);
1110}
1111Value *FunctionIO::setArguments(Value &V, Value &NewV,
1112 InstrumentationConfig &IConf,
1113 InstrumentorIRBuilderTy &IIRB) {
1114 auto &Fn = cast<Function>(Val&: V);
1115 auto *AIt = Fn.arg_begin();
1116 auto CB = [&](int Idx, Value *ReplV) {
1117 while (Config.ArgFilter && !Config.ArgFilter(*AIt))
1118 ++AIt;
1119 Fn.getArg(i: Idx)->replaceUsesWithIf(New: ReplV, ShouldReplace: [&](Use &U) {
1120 return IIRB.NewInsts.lookup(Val: cast<Instruction>(Val: U.getUser())) != IIRB.Epoch;
1121 });
1122 ++AIt;
1123 };
1124 if (!Config.ArgFilter)
1125 readValuePack(R: Fn.args(), Pack&: NewV, IIRB, SetterCB: CB);
1126 else
1127 readValuePack(R: make_filter_range(Range: Fn.args(), Pred: Config.ArgFilter), Pack&: NewV, IIRB,
1128 SetterCB: CB);
1129 return &Fn;
1130}
1131Value *FunctionIO::isMainFunction(Value &V, Type &Ty,
1132 InstrumentationConfig &IConf,
1133 InstrumentorIRBuilderTy &IIRB) {
1134 auto &Fn = cast<Function>(Val&: V);
1135 return getCI(IT: &Ty, Val: Fn.getName() == "main");
1136}
1137
1138/// UnreachableIO
1139///{
1140void UnreachableIO::init(InstrumentationConfig &IConf,
1141 InstrumentorIRBuilderTy &IIRB, ConfigTy *UserConfig) {
1142 if (UserConfig)
1143 Config = *UserConfig;
1144 addCommonArgs(IConf, Ctx&: IIRB.Ctx, PassId: Config.has(Opt: PassId));
1145 IConf.addChoice(IO&: *this, Ctx&: IIRB.Ctx);
1146}
1147///}
1148
1149/// AllocaIO
1150///{
1151void AllocaIO::init(InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB,
1152 ConfigTy *UserConfig) {
1153 if (UserConfig)
1154 Config = *UserConfig;
1155
1156 bool IsPRE = getLocationKind() == InstrumentationLocation::INSTRUCTION_PRE;
1157 if (!IsPRE && Config.has(Opt: PassAddress))
1158 IRTArgs.push_back(
1159 Elt: IRTArg(IIRB.PtrTy, "address", "The allocated memory address.",
1160 Config.has(Opt: ReplaceAddress) ? IRTArg::REPLACABLE : IRTArg::NONE,
1161 InstrumentationOpportunity::getValue,
1162 InstrumentationOpportunity::replaceValue));
1163 if (Config.has(Opt: PassSize))
1164 IRTArgs.push_back(Elt: IRTArg(
1165 IIRB.Int64Ty, "size", "The allocation size.",
1166 (IsPRE && Config.has(Opt: ReplaceSize)) ? IRTArg::REPLACABLE : IRTArg::NONE,
1167 getSize, setSize));
1168 if (Config.has(Opt: PassAlignment))
1169 IRTArgs.push_back(Elt: IRTArg(IIRB.Int64Ty, "alignment",
1170 "The allocation alignment.", IRTArg::NONE,
1171 getAlignment));
1172
1173 addCommonArgs(IConf, Ctx&: IIRB.Ctx, PassId: Config.has(Opt: PassId));
1174 IConf.addChoice(IO&: *this, Ctx&: IIRB.Ctx);
1175}
1176
1177Value *AllocaIO::getSize(Value &V, Type &Ty, InstrumentationConfig &IO,
1178 InstrumentorIRBuilderTy &IIRB) {
1179 auto &AI = cast<AllocaInst>(Val&: V);
1180 return IIRB.IRB.CreateAllocationSize(DestTy: &Ty, AI: &AI);
1181}
1182
1183Value *AllocaIO::setSize(Value &V, Value &NewV, InstrumentationConfig &IO,
1184 InstrumentorIRBuilderTy &IIRB) {
1185 auto &AI = cast<AllocaInst>(Val&: V);
1186 const DataLayout &DL = AI.getDataLayout();
1187 auto *NewAI = IIRB.IRB.CreateAlloca(Ty: IIRB.IRB.getInt8Ty(),
1188 AddrSpace: DL.getAllocaAddrSpace(), ArraySize: &NewV);
1189 NewAI->setAlignment(AI.getAlign());
1190 AI.replaceAllUsesWith(V: NewAI);
1191 IIRB.eraseLater(I: &AI);
1192 return NewAI;
1193}
1194
1195Value *AllocaIO::getAlignment(Value &V, Type &Ty, InstrumentationConfig &IConf,
1196 InstrumentorIRBuilderTy &IIRB) {
1197 return getCI(IT: &Ty, Val: cast<AllocaInst>(Val&: V).getAlign().value());
1198}
1199///}
1200
1201void StoreIO::init(InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB,
1202 ConfigTy *UserConfig) {
1203 if (UserConfig)
1204 Config = *UserConfig;
1205
1206 bool IsPRE = getLocationKind() == InstrumentationLocation::INSTRUCTION_PRE;
1207 if (Config.has(Opt: PassPointer)) {
1208 IRTArgs.push_back(
1209 Elt: IRTArg(IIRB.PtrTy, "pointer", "The accessed pointer.",
1210 ((IsPRE && Config.has(Opt: ReplacePointer)) ? IRTArg::REPLACABLE
1211 : IRTArg::NONE),
1212 getPointer, setPointer));
1213 }
1214 if (Config.has(Opt: PassPointerAS)) {
1215 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "pointer_as",
1216 "The address space of the accessed pointer.",
1217 IRTArg::NONE, getPointerAS));
1218 }
1219 if (Config.has(Opt: PassBasePointerInfo)) {
1220 IRTArgs.push_back(Elt: IRTArg(IIRB.PtrTy, "base_pointer_info",
1221 "The runtime provided base pointer info.",
1222 IRTArg::NONE, getBasePointerInfo));
1223 }
1224 if (Config.has(Opt: PassStoredValue)) {
1225 IRTArgs.push_back(
1226 Elt: IRTArg(getValueType(IIRB), "value", "The stored value.",
1227 IRTArg::POTENTIALLY_INDIRECT |
1228 (Config.has(Opt: PassStoredValueSize) ? IRTArg::INDIRECT_HAS_SIZE
1229 : IRTArg::NONE),
1230 getValue));
1231 }
1232 if (Config.has(Opt: PassStoredValueSize)) {
1233 IRTArgs.push_back(Elt: IRTArg(IIRB.Int64Ty, "value_size",
1234 "The size of the stored value.", IRTArg::NONE,
1235 getValueSize));
1236 }
1237 if (Config.has(Opt: PassAlignment)) {
1238 IRTArgs.push_back(Elt: IRTArg(IIRB.Int64Ty, "alignment",
1239 "The known access alignment.", IRTArg::NONE,
1240 getAlignment));
1241 }
1242 if (Config.has(Opt: PassValueTypeId)) {
1243 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "value_type_id",
1244 "The type id of the stored value.", IRTArg::TYPEID,
1245 getValueTypeId));
1246 }
1247 if (Config.has(Opt: PassValueSubTypeId)) {
1248 IRTArgs.push_back(Elt: IRTArg(
1249 IIRB.Int32Ty, "value_sub_type_id",
1250 "The type id of the stored value (for arrays and vectors, or -1).",
1251 IRTArg::TYPEID, getValueSubTypeId));
1252 }
1253 if (Config.has(Opt: PassAtomicityOrdering)) {
1254 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "atomicity_ordering",
1255 "The atomicity ordering of the store.",
1256 IRTArg::NONE, getAtomicityOrdering));
1257 }
1258 if (Config.has(Opt: PassSyncScopeId)) {
1259 IRTArgs.push_back(Elt: IRTArg(IIRB.Int8Ty, "sync_scope_id",
1260 "The sync scope id of the store.", IRTArg::NONE,
1261 getSyncScopeId));
1262 }
1263 if (Config.has(Opt: PassIsVolatile)) {
1264 IRTArgs.push_back(Elt: IRTArg(IIRB.Int8Ty, "is_volatile",
1265 "Flag indicating a volatile store.", IRTArg::NONE,
1266 isVolatile));
1267 }
1268
1269 addCommonArgs(IConf, Ctx&: IIRB.Ctx, PassId: Config.has(Opt: PassId));
1270 IConf.addChoice(IO&: *this, Ctx&: IIRB.Ctx);
1271}
1272
1273Value *StoreIO::getPointer(Value &V, Type &Ty, InstrumentationConfig &IConf,
1274 InstrumentorIRBuilderTy &IIRB) {
1275 auto &SI = cast<StoreInst>(Val&: V);
1276 return SI.getPointerOperand();
1277}
1278
1279Value *StoreIO::setPointer(Value &V, Value &NewV, InstrumentationConfig &IConf,
1280 InstrumentorIRBuilderTy &IIRB) {
1281 auto &SI = cast<StoreInst>(Val&: V);
1282 SI.setOperand(i_nocapture: SI.getPointerOperandIndex(), Val_nocapture: &NewV);
1283 return &SI;
1284}
1285
1286Value *StoreIO::getPointerAS(Value &V, Type &Ty, InstrumentationConfig &IConf,
1287 InstrumentorIRBuilderTy &IIRB) {
1288 auto &SI = cast<StoreInst>(Val&: V);
1289 return getCI(IT: &Ty, Val: SI.getPointerAddressSpace());
1290}
1291
1292Value *StoreIO::getBasePointerInfo(Value &V, Type &Ty,
1293 InstrumentationConfig &IConf,
1294 InstrumentorIRBuilderTy &IIRB) {
1295 auto &SI = cast<StoreInst>(Val&: V);
1296 return IConf.getBasePointerInfo(V&: *SI.getPointerOperand(), IIRB);
1297}
1298
1299Value *StoreIO::getValue(Value &V, Type &Ty, InstrumentationConfig &IConf,
1300 InstrumentorIRBuilderTy &IIRB) {
1301 auto &SI = cast<StoreInst>(Val&: V);
1302 return SI.getValueOperand();
1303}
1304
1305Value *StoreIO::getValueSize(Value &V, Type &Ty, InstrumentationConfig &IConf,
1306 InstrumentorIRBuilderTy &IIRB) {
1307 auto &SI = cast<StoreInst>(Val&: V);
1308 auto &DL = SI.getDataLayout();
1309 return getCI(IT: &Ty, Val: DL.getTypeStoreSize(Ty: SI.getValueOperand()->getType()));
1310}
1311
1312Value *StoreIO::getAlignment(Value &V, Type &Ty, InstrumentationConfig &IConf,
1313 InstrumentorIRBuilderTy &IIRB) {
1314 auto &SI = cast<StoreInst>(Val&: V);
1315 return getCI(IT: &Ty, Val: SI.getAlign().value());
1316}
1317
1318Value *StoreIO::getValueTypeId(Value &V, Type &Ty, InstrumentationConfig &IConf,
1319 InstrumentorIRBuilderTy &IIRB) {
1320 auto &SI = cast<StoreInst>(Val&: V);
1321 return getCI(IT: &Ty, Val: SI.getValueOperand()->getType()->getTypeID());
1322}
1323
1324Value *StoreIO::getValueSubTypeId(Value &V, Type &Ty,
1325 InstrumentationConfig &IConf,
1326 InstrumentorIRBuilderTy &IIRB) {
1327 auto &SI = cast<StoreInst>(Val&: V);
1328 return getSubTypeID(OpTy&: *SI.getValueOperand()->getType(), ReqTy&: Ty);
1329}
1330
1331Value *StoreIO::getAtomicityOrdering(Value &V, Type &Ty,
1332 InstrumentationConfig &IConf,
1333 InstrumentorIRBuilderTy &IIRB) {
1334 auto &SI = cast<StoreInst>(Val&: V);
1335 return getCI(IT: &Ty, Val: uint64_t(SI.getOrdering()));
1336}
1337
1338Value *StoreIO::getSyncScopeId(Value &V, Type &Ty, InstrumentationConfig &IConf,
1339 InstrumentorIRBuilderTy &IIRB) {
1340 auto &SI = cast<StoreInst>(Val&: V);
1341 return getCI(IT: &Ty, Val: uint64_t(SI.getSyncScopeID()));
1342}
1343
1344Value *StoreIO::isVolatile(Value &V, Type &Ty, InstrumentationConfig &IConf,
1345 InstrumentorIRBuilderTy &IIRB) {
1346 auto &SI = cast<StoreInst>(Val&: V);
1347 return getCI(IT: &Ty, Val: SI.isVolatile());
1348}
1349
1350void LoadIO::init(InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB,
1351 ConfigTy *UserConfig) {
1352 bool IsPRE = getLocationKind() == InstrumentationLocation::INSTRUCTION_PRE;
1353 if (UserConfig)
1354 Config = *UserConfig;
1355 if (Config.has(Opt: PassPointer)) {
1356 IRTArgs.push_back(
1357 Elt: IRTArg(IIRB.PtrTy, "pointer", "The accessed pointer.",
1358 ((IsPRE && Config.has(Opt: ReplacePointer)) ? IRTArg::REPLACABLE
1359 : IRTArg::NONE),
1360 getPointer, setPointer));
1361 }
1362 if (Config.has(Opt: PassPointerAS)) {
1363 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "pointer_as",
1364 "The address space of the accessed pointer.",
1365 IRTArg::NONE, getPointerAS));
1366 }
1367 if (Config.has(Opt: PassBasePointerInfo)) {
1368 IRTArgs.push_back(Elt: IRTArg(IIRB.PtrTy, "base_pointer_info",
1369 "The runtime provided base pointer info.",
1370 IRTArg::NONE, getBasePointerInfo));
1371 }
1372 if (!IsPRE && Config.has(Opt: PassValue)) {
1373 IRTArgs.push_back(
1374 Elt: IRTArg(getValueType(IIRB), "value", "The loaded value.",
1375 Config.has(Opt: ReplaceValue)
1376 ? IRTArg::REPLACABLE | IRTArg::POTENTIALLY_INDIRECT |
1377 (Config.has(Opt: PassValueSize) ? IRTArg::INDIRECT_HAS_SIZE
1378 : IRTArg::NONE)
1379 : IRTArg::NONE,
1380 getValue, Config.has(Opt: ReplaceValue) ? replaceValue : nullptr));
1381 }
1382 if (Config.has(Opt: PassValueSize)) {
1383 IRTArgs.push_back(Elt: IRTArg(IIRB.Int64Ty, "value_size",
1384 "The size of the loaded value.", IRTArg::NONE,
1385 getValueSize));
1386 }
1387 if (Config.has(Opt: PassAlignment)) {
1388 IRTArgs.push_back(Elt: IRTArg(IIRB.Int64Ty, "alignment",
1389 "The known access alignment.", IRTArg::NONE,
1390 getAlignment));
1391 }
1392 if (Config.has(Opt: PassValueTypeId)) {
1393 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "value_type_id",
1394 "The type id of the loaded value.", IRTArg::TYPEID,
1395 getValueTypeId));
1396 }
1397 if (Config.has(Opt: PassValueSubTypeId)) {
1398 IRTArgs.push_back(Elt: IRTArg(
1399 IIRB.Int32Ty, "value_sub_type_id",
1400 "The sub type id of the loaded value (for arrays and vectors, or -1).",
1401 IRTArg::TYPEID, getValueSubTypeId));
1402 }
1403 if (Config.has(Opt: PassAtomicityOrdering)) {
1404 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "atomicity_ordering",
1405 "The atomicity ordering of the load.",
1406 IRTArg::NONE, getAtomicityOrdering));
1407 }
1408 if (Config.has(Opt: PassSyncScopeId)) {
1409 IRTArgs.push_back(Elt: IRTArg(IIRB.Int8Ty, "sync_scope_id",
1410 "The sync scope id of the load.", IRTArg::NONE,
1411 getSyncScopeId));
1412 }
1413 if (Config.has(Opt: PassIsVolatile)) {
1414 IRTArgs.push_back(Elt: IRTArg(IIRB.Int8Ty, "is_volatile",
1415 "Flag indicating a volatile load.", IRTArg::NONE,
1416 isVolatile));
1417 }
1418
1419 addCommonArgs(IConf, Ctx&: IIRB.Ctx, PassId: Config.has(Opt: PassId));
1420 IConf.addChoice(IO&: *this, Ctx&: IIRB.Ctx);
1421}
1422
1423Value *LoadIO::getPointer(Value &V, Type &Ty, InstrumentationConfig &IConf,
1424 InstrumentorIRBuilderTy &IIRB) {
1425 auto &LI = cast<LoadInst>(Val&: V);
1426 return LI.getPointerOperand();
1427}
1428
1429Value *LoadIO::setPointer(Value &V, Value &NewV, InstrumentationConfig &IConf,
1430 InstrumentorIRBuilderTy &IIRB) {
1431 auto &LI = cast<LoadInst>(Val&: V);
1432 LI.setOperand(i_nocapture: LI.getPointerOperandIndex(), Val_nocapture: &NewV);
1433 return &LI;
1434}
1435
1436Value *LoadIO::getPointerAS(Value &V, Type &Ty, InstrumentationConfig &IConf,
1437 InstrumentorIRBuilderTy &IIRB) {
1438 auto &LI = cast<LoadInst>(Val&: V);
1439 return getCI(IT: &Ty, Val: LI.getPointerAddressSpace());
1440}
1441
1442Value *LoadIO::getBasePointerInfo(Value &V, Type &Ty,
1443 InstrumentationConfig &IConf,
1444 InstrumentorIRBuilderTy &IIRB) {
1445 auto &LI = cast<LoadInst>(Val&: V);
1446 return IConf.getBasePointerInfo(V&: *LI.getPointerOperand(), IIRB);
1447}
1448
1449Value *LoadIO::getValue(Value &V, Type &Ty, InstrumentationConfig &IConf,
1450 InstrumentorIRBuilderTy &IIRB) {
1451 return &V;
1452}
1453
1454Value *LoadIO::getValueSize(Value &V, Type &Ty, InstrumentationConfig &IConf,
1455 InstrumentorIRBuilderTy &IIRB) {
1456 auto &LI = cast<LoadInst>(Val&: V);
1457 auto &DL = LI.getDataLayout();
1458 return getCI(IT: &Ty, Val: DL.getTypeStoreSize(Ty: LI.getType()));
1459}
1460
1461Value *LoadIO::getAlignment(Value &V, Type &Ty, InstrumentationConfig &IConf,
1462 InstrumentorIRBuilderTy &IIRB) {
1463 auto &LI = cast<LoadInst>(Val&: V);
1464 return getCI(IT: &Ty, Val: LI.getAlign().value());
1465}
1466
1467Value *LoadIO::getValueTypeId(Value &V, Type &Ty, InstrumentationConfig &IConf,
1468 InstrumentorIRBuilderTy &IIRB) {
1469 auto &LI = cast<LoadInst>(Val&: V);
1470 return getCI(IT: &Ty, Val: LI.getType()->getTypeID());
1471}
1472
1473Value *LoadIO::getValueSubTypeId(Value &V, Type &Ty,
1474 InstrumentationConfig &IConf,
1475 InstrumentorIRBuilderTy &IIRB) {
1476 auto &LI = cast<LoadInst>(Val&: V);
1477 return getSubTypeID(OpTy&: *LI.getType(), ReqTy&: Ty);
1478}
1479
1480Value *LoadIO::getAtomicityOrdering(Value &V, Type &Ty,
1481 InstrumentationConfig &IConf,
1482 InstrumentorIRBuilderTy &IIRB) {
1483 auto &LI = cast<LoadInst>(Val&: V);
1484 return getCI(IT: &Ty, Val: uint64_t(LI.getOrdering()));
1485}
1486
1487Value *LoadIO::getSyncScopeId(Value &V, Type &Ty, InstrumentationConfig &IConf,
1488 InstrumentorIRBuilderTy &IIRB) {
1489 auto &LI = cast<LoadInst>(Val&: V);
1490 return getCI(IT: &Ty, Val: uint64_t(LI.getSyncScopeID()));
1491}
1492
1493Value *LoadIO::isVolatile(Value &V, Type &Ty, InstrumentationConfig &IConf,
1494 InstrumentorIRBuilderTy &IIRB) {
1495 auto &LI = cast<LoadInst>(Val&: V);
1496 return getCI(IT: &Ty, Val: LI.isVolatile());
1497}
1498
1499void BasePointerIO::init(InstrumentationConfig &IConf,
1500 InstrumentorIRBuilderTy &IIRB, ConfigTy *UserConfig) {
1501 if (UserConfig)
1502 Config = *UserConfig;
1503 if (Config.has(Opt: PassPointer))
1504 IRTArgs.push_back(Elt: IRTArg(IIRB.PtrTy, "base_pointer",
1505 "The base pointer in question.",
1506 IRTArg::REPLACABLE, getValue, setValueNoop));
1507 if (Config.has(Opt: PassPointerKind))
1508 IRTArgs.push_back(Elt: IRTArg(
1509 IIRB.Int32Ty, "base_pointer_kind",
1510 "The base pointer kind (argument, global, instruction, unknown).",
1511 IRTArg::NONE, getPointerKind));
1512 addCommonArgs(IConf, Ctx&: IIRB.Ctx, PassId: Config.has(Opt: PassId));
1513 IConf.addChoice(IO&: *this, Ctx&: IIRB.Ctx);
1514}
1515
1516Value *BasePointerIO::getPointerKind(Value &V, Type &Ty,
1517 InstrumentationConfig &IConf,
1518 InstrumentorIRBuilderTy &IIRB) {
1519 if (isa<Argument>(Val: V))
1520 return getCI(IT: &Ty, Val: 0);
1521 if (isa<GlobalValue>(Val: V))
1522 return getCI(IT: &Ty, Val: 1);
1523 if (isa<Instruction>(Val: V))
1524 return getCI(IT: &Ty, Val: 2);
1525 return getCI(IT: &Ty, Val: 3);
1526}
1527
1528void ModuleIO::init(InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB,
1529 ConfigTy *UserConfig) {
1530 if (UserConfig)
1531 Config = *UserConfig;
1532
1533 if (Config.has(Opt: PassName))
1534 IRTArgs.push_back(Elt: IRTArg(IIRB.PtrTy, "module_name",
1535 "The module/translation unit name.",
1536 IRTArg::STRING, getModuleName));
1537 if (Config.has(Opt: PassTargetTriple))
1538 IRTArgs.push_back(Elt: IRTArg(IIRB.PtrTy, "target_triple", "The target triple.",
1539 IRTArg::STRING, getTargetTriple));
1540
1541 addCommonArgs(IConf, Ctx&: IIRB.Ctx, PassId: Config.has(Opt: PassId));
1542 IConf.addChoice(IO&: *this, Ctx&: IIRB.Ctx);
1543}
1544Value *ModuleIO::getModuleName(Value &V, Type &Ty, InstrumentationConfig &IConf,
1545 InstrumentorIRBuilderTy &IIRB) {
1546 // V is a constructor or destructor of the module we can place code in.
1547 auto &Fn = cast<Function>(Val&: V);
1548 return IConf.getGlobalString(S: Fn.getParent()->getName(), IIRB);
1549}
1550Value *ModuleIO::getTargetTriple(Value &V, Type &Ty,
1551 InstrumentationConfig &IConf,
1552 InstrumentorIRBuilderTy &IIRB) {
1553 // V is a constructor or destructor of the module we can place code in.
1554 auto &Fn = cast<Function>(Val&: V);
1555 return IConf.getGlobalString(S: Fn.getParent()->getTargetTriple().getTriple(),
1556 IIRB);
1557}
1558
1559void GlobalVarIO::init(InstrumentationConfig &IConf,
1560 InstrumentorIRBuilderTy &IIRB, ConfigTy *UserConfig) {
1561 if (UserConfig)
1562 Config = *UserConfig;
1563 bool IsPRE = InstrumentationLocation::isPRE(Kind: getLocationKind());
1564 if (Config.has(Opt: PassAddress))
1565 IRTArgs.push_back(Elt: IRTArg(
1566 IIRB.PtrTy, "address",
1567 "The address of the global (replaceable for definitions).",
1568 IsPRE && Config.has(Opt: ReplaceAddress) ? IRTArg::REPLACABLE : IRTArg::NONE,
1569 getAddress, setAddress));
1570 if (Config.has(Opt: PassAS))
1571 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "address_space",
1572 "The address space of the global.", IRTArg::NONE,
1573 getAS));
1574 if (Config.has(Opt: PassDeclaredSize))
1575 IRTArgs.push_back(Elt: IRTArg(IIRB.Int64Ty, "declared_size",
1576 "The size of the declared type of the global.",
1577 IRTArg::NONE, getDeclaredSize));
1578 if (Config.has(Opt: PassAlignment))
1579 IRTArgs.push_back(Elt: IRTArg(IIRB.Int64Ty, "alignment",
1580 "The allocation alignment.", IRTArg::NONE,
1581 getAlignment));
1582 if (Config.has(Opt: PassName))
1583 IRTArgs.push_back(Elt: IRTArg(IIRB.PtrTy, "name", "The name of the global.",
1584 IRTArg::STRING, getSymbolName));
1585 if (Config.has(Opt: PassInitialValue))
1586 IRTArgs.push_back(Elt: IRTArg(
1587 IIRB.Int64Ty, "initial_value", "The initial value of the global.",
1588 IRTArg::POTENTIALLY_INDIRECT | IRTArg::INDIRECT_HAS_SIZE,
1589 getInitialValue));
1590 if (Config.has(Opt: PassIsConstant))
1591 IRTArgs.push_back(Elt: IRTArg(IIRB.Int8Ty, "is_constant",
1592 "Flag to indicate constant globals.", IRTArg::NONE,
1593 isConstant));
1594 if (Config.has(Opt: PassIsDefinition))
1595 IRTArgs.push_back(Elt: IRTArg(IIRB.Int8Ty, "is_definition",
1596 "Flag to indicate global definitions.",
1597 IRTArg::NONE, isDefinition));
1598 addCommonArgs(IConf, Ctx&: IIRB.Ctx, PassId: Config.has(Opt: PassId));
1599 IConf.addChoice(IO&: *this, Ctx&: IIRB.Ctx);
1600}
1601Value *GlobalVarIO::getAddress(Value &V, Type &Ty, InstrumentationConfig &IConf,
1602 InstrumentorIRBuilderTy &IIRB) {
1603 GlobalVariable &GV = cast<GlobalVariable>(Val&: V);
1604 if (GV.getAddressSpace())
1605 return ConstantExpr::getAddrSpaceCast(C: &GV, Ty: IIRB.PtrTy);
1606 return &GV;
1607}
1608Value *GlobalVarIO::setAddress(Value &V, Value &NewV,
1609 InstrumentationConfig &IConf,
1610 InstrumentorIRBuilderTy &IIRB) {
1611 GlobalVariable &GV = cast<GlobalVariable>(Val&: V);
1612
1613 GlobalVariable *ShadowGV = nullptr;
1614 auto ShadowName = IConf.getRTName(Prefix: "shadow.", Name: GV.getName());
1615 auto &DL = GV.getDataLayout();
1616 if (GV.isDeclaration()) {
1617 ShadowGV = new GlobalVariable(*GV.getParent(), GV.getType(), false,
1618 GlobalVariable::WeakODRLinkage, &GV,
1619 ShadowName, &GV, GV.getThreadLocalMode(),
1620 DL.getDefaultGlobalsAddressSpace());
1621 } else {
1622 ShadowGV = new GlobalVariable(
1623 *GV.getParent(), NewV.getType(), false, GV.getLinkage(),
1624 PoisonValue::get(T: NewV.getType()), ShadowName, &GV);
1625 IIRB.IRB.CreateStore(Val: &NewV, Ptr: ShadowGV);
1626 }
1627
1628 SmallVector<Use *> Worklist(make_pointer_range(Range: GV.uses()));
1629 SmallPtrSet<Use *, 32> Done;
1630 DenseMap<Value *, Instruction *> ConstToInstMap;
1631 DenseMap<Function *, Instruction *> ReloadMap;
1632
1633 auto MakeInstForConst = [&](Use &U) {
1634 Instruction *&I = ConstToInstMap[U];
1635 if (I)
1636 return;
1637 if (U == &GV) {
1638 } else if (auto *CE = dyn_cast<ConstantExpr>(Val&: U)) {
1639 I = CE->getAsInstruction();
1640 }
1641 };
1642
1643 auto InsertConsts = [&](Instruction *UserI, Use &UserU) {
1644 SmallVector<std::pair<Instruction *, Use *>> Worklist;
1645 auto *&Reload = ReloadMap[UserI->getFunction()];
1646 if (!Reload) {
1647 Reload = new LoadInst(
1648 GV.getType(), ShadowGV, GV.getName() + ".shadow_load",
1649 UserI->getFunction()->getEntryBlock().getFirstNonPHIOrAlloca());
1650 IIRB.NewInsts.insert(KV: {Reload, IIRB.Epoch});
1651 }
1652 Worklist.push_back(Elt: {UserI, &UserU});
1653 while (!Worklist.empty()) {
1654 auto [I, U] = Worklist.pop_back_val();
1655 if (*U == &GV) {
1656 U->set(ReloadMap[I->getFunction()]);
1657 continue;
1658 }
1659 if (auto *CI = ConstToInstMap[*U]) {
1660 auto *CIClone = CI->clone();
1661 IIRB.NewInsts.insert(KV: {CIClone, IIRB.Epoch});
1662 if (auto *PHI = dyn_cast<PHINode>(Val: I)) {
1663 auto *BB = PHI->getIncomingBlock(i: U->getOperandNo());
1664 CIClone->insertBefore(InsertPos: BB->getTerminator()->getIterator());
1665 } else {
1666 CIClone->insertBefore(InsertPos: I->getIterator());
1667 }
1668 U->set(CIClone);
1669 for (auto &CICUse : CIClone->operands()) {
1670 Worklist.push_back(Elt: {CIClone, &CICUse});
1671 }
1672 }
1673 }
1674 };
1675
1676 while (!Worklist.empty()) {
1677 Use *U = Worklist.pop_back_val();
1678 if (!Done.insert(Ptr: U).second)
1679 continue;
1680 MakeInstForConst(*U);
1681 auto *I = dyn_cast<Instruction>(Val: U->getUser());
1682 if (!I) {
1683 append_range(C&: Worklist, R: make_pointer_range(Range: U->getUser()->uses()));
1684 continue;
1685 }
1686 if (IIRB.NewInsts.lookup(Val: I) == IIRB.Epoch)
1687 continue;
1688 if (isa<LandingPadInst>(Val: I))
1689 continue;
1690 if (auto *II = dyn_cast<IntrinsicInst>(Val: I))
1691 if (II->getIntrinsicID() == Intrinsic::eh_typeid_for)
1692 continue;
1693 if (I->getParent())
1694 InsertConsts(I, *U);
1695 }
1696
1697 for (auto &It : ConstToInstMap)
1698 if (It.second)
1699 It.second->deleteValue();
1700
1701 return &V;
1702}
1703Value *GlobalVarIO::getAS(Value &V, Type &Ty, InstrumentationConfig &IConf,
1704 InstrumentorIRBuilderTy &IIRB) {
1705 GlobalVariable &GV = cast<GlobalVariable>(Val&: V);
1706 return getCI(IT: &Ty, Val: GV.getAddressSpace());
1707}
1708Value *GlobalVarIO::getAlignment(Value &V, Type &Ty,
1709 InstrumentationConfig &IConf,
1710 InstrumentorIRBuilderTy &IIRB) {
1711 GlobalVariable &GV = cast<GlobalVariable>(Val&: V);
1712 MaybeAlign Alignment = GV.getAlign();
1713 return getCI(IT: &Ty, Val: Alignment ? Alignment->value() : 0);
1714}
1715Value *GlobalVarIO::getDeclaredSize(Value &V, Type &Ty,
1716 InstrumentationConfig &IConf,
1717 InstrumentorIRBuilderTy &IIRB) {
1718 GlobalVariable &GV = cast<GlobalVariable>(Val&: V);
1719 auto &DL = GV.getDataLayout();
1720 return getCI(IT: &Ty, Val: DL.getTypeAllocSize(Ty: GV.getValueType()));
1721}
1722Value *GlobalVarIO::getSymbolName(Value &V, Type &Ty,
1723 InstrumentationConfig &IConf,
1724 InstrumentorIRBuilderTy &IIRB) {
1725 GlobalVariable &GV = cast<GlobalVariable>(Val&: V);
1726 return IConf.getGlobalString(S: GV.getName(), IIRB);
1727}
1728Value *GlobalVarIO::getInitialValue(Value &V, Type &Ty,
1729 InstrumentationConfig &IConf,
1730 InstrumentorIRBuilderTy &IIRB) {
1731 GlobalVariable &GV = cast<GlobalVariable>(Val&: V);
1732 return GV.hasInitializer() ? GV.getInitializer()
1733 : Constant::getNullValue(Ty: &Ty);
1734}
1735Value *GlobalVarIO::isConstant(Value &V, Type &Ty, InstrumentationConfig &IConf,
1736 InstrumentorIRBuilderTy &IIRB) {
1737 GlobalVariable &GV = cast<GlobalVariable>(Val&: V);
1738 return getCI(IT: &Ty, Val: GV.isConstant());
1739}
1740Value *GlobalVarIO::isDefinition(Value &V, Type &Ty,
1741 InstrumentationConfig &IConf,
1742 InstrumentorIRBuilderTy &IIRB) {
1743 GlobalVariable &GV = cast<GlobalVariable>(Val&: V);
1744 return getCI(IT: &Ty, Val: !GV.isDeclaration());
1745}
1746
1747/// CastIO
1748/// {
1749void CastIO::init(InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB,
1750 ConfigTy *UserConfig) {
1751 if (UserConfig)
1752 Config = *UserConfig;
1753 bool IsPRE = getLocationKind() == InstrumentationLocation::INSTRUCTION_PRE;
1754 if (Config.has(Opt: PassInput))
1755 IRTArgs.push_back(
1756 Elt: IRTArg(IIRB.Int64Ty, "input", "Input value of the cast.",
1757 IRTArg::POTENTIALLY_INDIRECT |
1758 (Config.has(Opt: PassResultSize) ? IRTArg::INDIRECT_HAS_SIZE
1759 : IRTArg::NONE),
1760 getInput));
1761 if (Config.has(Opt: PassInputTypeId))
1762 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "input_type_id",
1763 "The type id of the input value.", IRTArg::TYPEID,
1764 getInputTypeId));
1765 if (Config.has(Opt: PassInputSubTypeId))
1766 IRTArgs.push_back(Elt: IRTArg(
1767 IIRB.Int32Ty, "input_sub_type_id",
1768 "The sub type id of the input value (for arrays and vectors, or -1).",
1769 IRTArg::TYPEID, getInputSubTypeId));
1770 if (Config.has(Opt: PassInputSize))
1771 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "input_size",
1772 "The size of the input value.", IRTArg::NONE,
1773 getInputSize));
1774 if (!IsPRE && Config.has(Opt: PassResult))
1775 IRTArgs.push_back(
1776 Elt: IRTArg(IIRB.Int64Ty, "result", "Result of the cast.",
1777 (IRTArg::REPLACABLE | IRTArg::POTENTIALLY_INDIRECT) |
1778 (Config.has(Opt: PassResultSize) ? IRTArg::INDIRECT_HAS_SIZE
1779 : IRTArg::NONE),
1780 getValue, Config.has(Opt: ReplaceResult) ? replaceValue : nullptr));
1781 if (Config.has(Opt: PassResultTypeId))
1782 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "result_type_id",
1783 "The type id of the result value.", IRTArg::TYPEID,
1784 getResultTypeId));
1785 if (Config.has(Opt: PassResultSubTypeId))
1786 IRTArgs.push_back(Elt: IRTArg(
1787 IIRB.Int32Ty, "result_sub_type_id",
1788 "The sub type id of the result value (for arrays and vectors, or -1).",
1789 IRTArg::TYPEID, getResultSubTypeId));
1790 if (Config.has(Opt: PassResultSize))
1791 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "result_size",
1792 "The size of the result value.", IRTArg::NONE,
1793 getResultSize));
1794 if (Config.has(Opt: PassOpcode))
1795 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "opcode",
1796 "The opcode of the cast instruction.",
1797 IRTArg::NONE, getOpcode));
1798
1799 addCommonArgs(IConf, Ctx&: IIRB.Ctx, PassId: Config.has(Opt: PassId));
1800 IConf.addChoice(IO&: *this, Ctx&: IIRB.Ctx);
1801}
1802
1803Value *CastIO::getInput(Value &V, Type &Ty, InstrumentationConfig &IConf,
1804 InstrumentorIRBuilderTy &IIRB) {
1805 auto &CI = cast<CastInst>(Val&: V);
1806 return CI.getOperand(i_nocapture: 0);
1807}
1808
1809Value *CastIO::getInputTypeId(Value &V, Type &Ty, InstrumentationConfig &IConf,
1810 InstrumentorIRBuilderTy &IIRB) {
1811 auto &CI = cast<CastInst>(Val&: V);
1812 return getCI(IT: &Ty, Val: CI.getSrcTy()->getTypeID());
1813}
1814
1815Value *CastIO::getInputSubTypeId(Value &V, Type &Ty,
1816 InstrumentationConfig &IConf,
1817 InstrumentorIRBuilderTy &IIRB) {
1818 auto &CI = cast<CastInst>(Val&: V);
1819 return getSubTypeID(OpTy&: *CI.getSrcTy(), ReqTy&: Ty);
1820}
1821
1822Value *CastIO::getInputSize(Value &V, Type &Ty, InstrumentationConfig &IConf,
1823 InstrumentorIRBuilderTy &IIRB) {
1824 auto &CI = cast<CastInst>(Val&: V);
1825 auto &DL = CI.getDataLayout();
1826 return getCI(IT: &Ty, Val: DL.getTypeStoreSize(Ty: CI.getSrcTy()));
1827}
1828
1829Value *CastIO::getResultTypeId(Value &V, Type &Ty, InstrumentationConfig &IConf,
1830 InstrumentorIRBuilderTy &IIRB) {
1831 auto &CI = cast<CastInst>(Val&: V);
1832 return getCI(IT: &Ty, Val: CI.getDestTy()->getTypeID());
1833}
1834
1835Value *CastIO::getResultSubTypeId(Value &V, Type &Ty,
1836 InstrumentationConfig &IConf,
1837 InstrumentorIRBuilderTy &IIRB) {
1838 auto &CI = cast<CastInst>(Val&: V);
1839 return getSubTypeID(OpTy&: *CI.getDestTy(), ReqTy&: Ty);
1840}
1841
1842Value *CastIO::getResultSize(Value &V, Type &Ty, InstrumentationConfig &IConf,
1843 InstrumentorIRBuilderTy &IIRB) {
1844 auto &CI = cast<CastInst>(Val&: V);
1845 auto &DL = CI.getDataLayout();
1846 return getCI(IT: &Ty, Val: DL.getTypeStoreSize(Ty: CI.getDestTy()));
1847}
1848///}
1849
1850Value *NumericIO::getFlags(Value &V, Type &Ty, InstrumentationConfig &IConf,
1851 InstrumentorIRBuilderTy &IIRB) {
1852 auto &I = cast<Instruction>(Val&: V);
1853 uint64_t Flag = NUMERIC_FLAG_NONE;
1854
1855 switch (I.getOpcode()) {
1856 case Instruction::Add:
1857 case Instruction::Sub:
1858 case Instruction::Mul:
1859 case Instruction::Shl:
1860 if (I.hasNoSignedWrap())
1861 Flag |= NUMERIC_FLAG_NO_SIGNED_WRAP;
1862 if (I.hasNoUnsignedWrap())
1863 Flag |= NUMERIC_FLAG_NO_UNSIGNED_WRAP;
1864 break;
1865 case Instruction::FAdd:
1866 case Instruction::FSub:
1867 case Instruction::FMul:
1868 case Instruction::FDiv:
1869 case Instruction::FNeg:
1870 if (I.hasNoNaNs())
1871 Flag |= NUMERIC_FLAG_HAS_NO_NANS;
1872 if (I.hasNoInfs())
1873 Flag |= NUMERIC_FLAG_HAS_NO_INFS;
1874 if (I.hasNoSignedZeros())
1875 Flag |= NUMERIC_FLAG_HAS_NO_SIGNED_ZEROS;
1876 break;
1877 case Instruction::AShr:
1878 case Instruction::LShr:
1879 case Instruction::SDiv:
1880 case Instruction::UDiv:
1881 if (I.isExact())
1882 Flag |= NUMERIC_FLAG_IS_EXACT;
1883 break;
1884 }
1885
1886 if (auto *DI = dyn_cast<PossiblyDisjointInst>(Val: &V))
1887 if (DI->isDisjoint())
1888 Flag |= NUMERIC_FLAG_IS_DISJOINT;
1889
1890 return getCI(IT: &Ty, Val: Flag);
1891}
1892
1893void NumericIO::addFlagNames() {
1894 FlagNames["nsw"] = NUMERIC_FLAG_NO_SIGNED_WRAP;
1895 FlagNames["nuw"] = NUMERIC_FLAG_NO_UNSIGNED_WRAP;
1896 FlagNames["nnan"] = NUMERIC_FLAG_HAS_NO_NANS;
1897 FlagNames["ninf"] = NUMERIC_FLAG_HAS_NO_INFS;
1898 FlagNames["nsz"] = NUMERIC_FLAG_HAS_NO_SIGNED_ZEROS;
1899 FlagNames["exact"] = NUMERIC_FLAG_IS_EXACT;
1900}
1901
1902void NumericIO::init(InstrumentationConfig &IConf,
1903 InstrumentorIRBuilderTy &IIRB, ConfigTy *UserConfig) {
1904 if (UserConfig)
1905 Config = UserConfig;
1906 bool IsPRE = getLocationKind() == InstrumentationLocation::INSTRUCTION_PRE;
1907 const auto ValArgOpts =
1908 IRTArg::POTENTIALLY_INDIRECT |
1909 (Config.has(Opt: PassSize) ? IRTArg::INDIRECT_HAS_SIZE : IRTArg::NONE);
1910 if (Config.has(Opt: PassTypeId))
1911 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "type_id",
1912 "The operation's type id.", IRTArg::TYPEID,
1913 getTypeId));
1914 if (Config.has(Opt: PassSubTypeId))
1915 IRTArgs.push_back(
1916 Elt: IRTArg(IIRB.Int32Ty, "sub_type_id",
1917 "The operation's sub type id (for arrays and vectors, or -1).",
1918 IRTArg::TYPEID, getSubTypeId));
1919 if (Config.has(Opt: PassSize))
1920 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "size", "The operation's type size.",
1921 IRTArg::NONE, getTypeSize));
1922 if (Config.has(Opt: PassOpcode))
1923 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "opcode", "The instruction opcode.",
1924 IRTArg::NONE, getOpcode));
1925 if (Config.has(Opt: PassLeft))
1926 IRTArgs.push_back(Elt: IRTArg(IIRB.Int64Ty, "left",
1927 "The operation's left operand.", ValArgOpts,
1928 getLeftOperand));
1929 if (Config.has(Opt: PassRight))
1930 IRTArgs.push_back(Elt: IRTArg(IIRB.Int64Ty, "right",
1931 "The operation's right operand. This value is "
1932 "poison for unary operations.",
1933 ValArgOpts, getRightOperand));
1934 if (!IsPRE && Config.has(Opt: PassResult))
1935 IRTArgs.push_back(
1936 Elt: IRTArg(IIRB.Int64Ty, "result", "Result of the operation.",
1937 IRTArg::REPLACABLE | ValArgOpts, getValue,
1938 Config.has(Opt: ReplaceResult) ? replaceValue : nullptr));
1939 if (Config.has(Opt: PassFlags))
1940 IRTArgs.push_back(
1941 Elt: IRTArg(IIRB.Int64Ty, "flags",
1942 "A bitmask value signaling which instruction flags are present.",
1943 IRTArg::NONE, getFlags));
1944 addCommonArgs(IConf, Ctx&: IIRB.Ctx, PassId: Config.has(Opt: PassId));
1945 addFlagNames();
1946 IConf.addChoice(IO&: *this, Ctx&: IIRB.Ctx);
1947}
1948
1949Value *CompareIO::getOperandTypeId(Value &V, Type &Ty,
1950 InstrumentationConfig &IConf,
1951 InstrumentorIRBuilderTy &IIRB) {
1952 auto &I = cast<Instruction>(Val&: V);
1953 return getCI(IT: &Ty, Val: I.getOperand(i: 0)->getType()->getTypeID());
1954}
1955
1956Value *CompareIO::getOperandSize(Value &V, Type &Ty,
1957 InstrumentationConfig &IConf,
1958 InstrumentorIRBuilderTy &IIRB) {
1959 auto &I = cast<Instruction>(Val&: V);
1960 auto &DL = I.getDataLayout();
1961 return getCI(IT: &Ty, Val: DL.getTypeStoreSize(Ty: I.getOperand(i: 0)->getType()));
1962}
1963
1964Value *CompareIO::getPredicate(Value &V, Type &Ty, InstrumentationConfig &IConf,
1965 InstrumentorIRBuilderTy &IIRB) {
1966 auto *CI = dyn_cast<CmpInst>(Val: &V);
1967 return getCI(IT: &Ty, Val: CI->getPredicate());
1968}
1969
1970void CompareIO::addFlagNames() {
1971 FlagNames["samesign"] = COMPARE_FLAG_SAMESIGN;
1972 FlagNames["nnan"] = COMPARE_FLAG_HAS_NO_NANS;
1973 FlagNames["ninf"] = COMPARE_FLAG_HAS_NO_INFS;
1974 FlagNames["nsz"] = COMPARE_FLAG_HAS_NO_SIGNED_ZEROS;
1975}
1976
1977Value *CompareIO::getFlags(Value &V, Type &Ty, InstrumentationConfig &IConf,
1978 InstrumentorIRBuilderTy &IIRB) {
1979 auto &I = cast<Instruction>(Val&: V);
1980 uint64_t Flag = NUMERIC_FLAG_NONE;
1981
1982 switch (I.getOpcode()) {
1983 case Instruction::ICmp:
1984 if (dyn_cast<ICmpInst>(Val: &V)->hasSameSign())
1985 Flag |= COMPARE_FLAG_SAMESIGN;
1986 break;
1987 case Instruction::FCmp:
1988 if (I.hasNoNaNs())
1989 Flag |= COMPARE_FLAG_HAS_NO_NANS;
1990 if (I.hasNoInfs())
1991 Flag |= COMPARE_FLAG_HAS_NO_INFS;
1992 if (I.hasNoSignedZeros())
1993 Flag |= COMPARE_FLAG_HAS_NO_SIGNED_ZEROS;
1994 break;
1995 }
1996
1997 return getCI(IT: &Ty, Val: Flag);
1998}
1999
2000void CompareIO::init(InstrumentationConfig &IConf,
2001 InstrumentorIRBuilderTy &IIRB, ConfigTy *UserConfig) {
2002 if (UserConfig)
2003 Config = UserConfig;
2004 bool IsPRE = getLocationKind() == InstrumentationLocation::INSTRUCTION_PRE;
2005 const auto OperandArgOpts =
2006 IRTArg::POTENTIALLY_INDIRECT |
2007 (Config.has(Opt: PassOpSize) ? IRTArg::INDIRECT_HAS_SIZE : IRTArg::NONE);
2008 if (Config.has(Opt: PassOpTypeId))
2009 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "operand_type_id",
2010 "The operand type id.", IRTArg::NONE,
2011 getOperandTypeId));
2012 if (Config.has(Opt: PassOpSize))
2013 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "operand_size",
2014 "The operand type size.", IRTArg::NONE,
2015 getOperandSize));
2016 if (Config.has(Opt: PassOpcode))
2017 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "opcode", "The instruction opcode.",
2018 IRTArg::NONE, getOpcode));
2019 if (Config.has(Opt: PassPredicate))
2020 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "predicate",
2021 "The comparison predicate ID.", IRTArg::NONE,
2022 getPredicate));
2023 if (Config.has(Opt: PassLeft))
2024 IRTArgs.push_back(Elt: IRTArg(IIRB.Int64Ty, "left",
2025 "The comparison's left operand.", OperandArgOpts,
2026 getLeftOperand));
2027 if (Config.has(Opt: PassRight))
2028 IRTArgs.push_back(Elt: IRTArg(IIRB.Int64Ty, "right",
2029 "The comparison's right operand.", OperandArgOpts,
2030 getRightOperand));
2031 if (!IsPRE && Config.has(Opt: PassResultSize))
2032 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "result_type_id",
2033 "The result value's type ID.", IRTArg::NONE,
2034 getTypeId));
2035 if (!IsPRE && Config.has(Opt: PassResultSize))
2036 IRTArgs.push_back(Elt: IRTArg(IIRB.Int32Ty, "result_size",
2037 "Size of the result value.", IRTArg::NONE,
2038 getTypeSize));
2039 if (!IsPRE && Config.has(Opt: PassResult))
2040 IRTArgs.push_back(
2041 Elt: IRTArg(IIRB.Int64Ty, "result", "Result of the operation.",
2042 IRTArg::REPLACABLE | IRTArg::POTENTIALLY_INDIRECT |
2043 (Config.has(Opt: PassResultSize) ? IRTArg::INDIRECT_HAS_SIZE
2044 : IRTArg::NONE),
2045 getValue, Config.has(Opt: ReplaceResult) ? replaceValue : nullptr));
2046 if (Config.has(Opt: PassFlags))
2047 IRTArgs.push_back(
2048 Elt: IRTArg(IIRB.Int64Ty, "flags",
2049 "A bitmask value signaling which instruction flags are present.",
2050 IRTArg::NONE, getFlags));
2051 addFlagNames();
2052 addCommonArgs(IConf, Ctx&: IIRB.Ctx, PassId: Config.has(Opt: PassId));
2053 IConf.addChoice(IO&: *this, Ctx&: IIRB.Ctx);
2054}
2055