LLVM 24.0.0git
GlobalMergeFunctions.cpp
Go to the documentation of this file.
1//===---- GlobalMergeFunctions.cpp - Global merge functions -------*- C++ -===//
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// This pass implements the global merge function pass.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/Statistic.h"
18#include "llvm/CodeGen/Passes.h"
19#include "llvm/IR/IRBuilder.h"
24
25#define DEBUG_TYPE "global-merge-func"
26
27using namespace llvm;
28using namespace llvm::support;
29
31 "disable-cgdata-for-merging", cl::Hidden,
32 cl::desc("Disable codegen data for function merging. Local "
33 "merging is still enabled within a module."),
34 cl::init(false));
35
36STATISTIC(NumMergedFunctions,
37 "Number of functions that are actually merged using function hash");
38STATISTIC(NumAnalyzedModues, "Number of modules that are analyzed");
39STATISTIC(NumAnalyzedFunctions, "Number of functions that are analyzed");
40STATISTIC(NumEligibleFunctions, "Number of functions that are eligible");
41
42/// Returns true if the \OpIdx operand of \p CI is the callee operand.
43static bool isCalleeOperand(const CallBase *CI, unsigned OpIdx) {
44 return &CI->getCalledOperandUse() == &CI->getOperandUse(OpIdx);
45}
46
47static bool canParameterizeCallOperand(const CallBase *CI, unsigned OpIdx) {
48 if (CI->isInlineAsm())
49 return false;
50 Function *Callee = CI->getCalledOperand()
53 : nullptr;
54 if (Callee) {
55 if (Callee->isIntrinsic())
56 return false;
57 auto Name = Callee->getName();
58 // objc_msgSend stubs must be called, and can't have their address taken.
59 if (Name.starts_with("objc_msgSend$"))
60 return false;
61 // Calls to dtrace probes must generate unique patchpoints.
62 if (Name.starts_with("__dtrace"))
63 return false;
64 }
65 if (isCalleeOperand(CI, OpIdx)) {
66 // The operand is the callee and it has already been signed. Ignore this
67 // because we cannot add another ptrauth bundle to the call instruction.
68 if (CI->getOperandBundle(LLVMContext::OB_ptrauth).has_value())
69 return false;
70 // Parameterizing the callee would turn this into an indirect call, which
71 // must be authenticated in a "ptrauth-calls" function.
72 if (CI->getFunction()->hasFnAttribute("ptrauth-calls"))
73 return false;
74 } else {
75 // The target of the arc-attached call must be a constant and cannot be
76 // parameterized.
78 OpIdx))
79 return false;
80 }
81 return true;
82}
83
84/// Returns true if function \p F is eligible for merging.
86 if (F->isDeclaration())
87 return false;
88
89 if (F->hasFnAttribute(llvm::Attribute::NoMerge) ||
90 F->hasFnAttribute(llvm::Attribute::AlwaysInline))
91 return false;
92
93 if (F->hasAvailableExternallyLinkage())
94 return false;
95
96 if (F->getFunctionType()->isVarArg())
97 return false;
98
99 if (F->getCallingConv() == CallingConv::SwiftTail)
100 return false;
101
102 // Unnamed functions are skipped for simplicity.
103 if (!F->hasName())
104 return false;
105
106 // If function contains callsites with musttail, if we merge
107 // it, the merged function will have the musttail callsite, but
108 // the number of parameters can change, thus the parameter count
109 // of the callsite will mismatch with the function itself.
110 for (const BasicBlock &BB : *F) {
111 for (const Instruction &I : BB) {
112 const auto *CB = dyn_cast<CallBase>(&I);
113 if (CB && CB->isMustTailCall())
114 return false;
115 }
116 }
117
118 return true;
119}
120
122 switch (I->getOpcode()) {
123 case Instruction::Load:
124 case Instruction::Store:
125 case Instruction::Call:
126 case Instruction::Invoke:
127 return true;
128 default:
129 return false;
130 }
131}
132
133// This function takes an instruction, \p I, and an operand index, \p OpIdx.
134// It returns true if the operand should be ignored in the hash computation.
135// If \p OpIdx is out of range based on the other instruction context, it cannot
136// be ignored.
137static bool ignoreOp(const Instruction *I, unsigned OpIdx) {
138 if (OpIdx >= I->getNumOperands())
139 return false;
140
142 return false;
143
144 if (!isa<Constant>(I->getOperand(OpIdx)))
145 return false;
146
147 if (const auto *CI = dyn_cast<CallBase>(I))
148 return canParameterizeCallOperand(CI, OpIdx);
149
150 return true;
151}
152
154 // Globals with local linkage are qualified with their module's source file
155 // name so that same-named locals in different modules are not mistaken for
156 // identical operands when the merged map is finalized across modules.
158 /*SourceQualifyLocalGlobals=*/true);
159}
160
162 ++NumAnalyzedModues;
163 for (Function &Func : M) {
164 ++NumAnalyzedFunctions;
165 if (isEligibleFunction(&Func)) {
166 ++NumEligibleFunctions;
167
168 auto FI = hashFunction(Func);
169
170 // Convert the operand map to a vector for a serialization-friendly
171 // format.
172 IndexOperandHashVecType IndexOperandHashes;
173 for (auto &Pair : *FI.IndexOperandHashMap)
174 IndexOperandHashes.emplace_back(Pair);
175
176 StableFunction SF(FI.FunctionHash, get_stable_name(Func.getName()).str(),
177 M.getModuleIdentifier(), FI.IndexInstruction->size(),
178 std::move(IndexOperandHashes));
179
180 LocalFunctionMap->insert(SF);
181 }
182 }
183}
184
185/// Tuple to hold function info to process merging.
194
195// Given the func info, and the parameterized locations, create and return
196// a new merged function by replacing the original constants with the new
197// parameters.
199 ArrayRef<Type *> ConstParamTypes,
200 const ParamLocsVecTy &ParamLocsVec) {
201 // Synthesize a new merged function name by appending ".Tgm" to the root
202 // function's name.
203 auto *MergedFunc = FI.F;
204 std::string NewFunctionName =
206 auto *M = MergedFunc->getParent();
207 assert(!M->getFunction(NewFunctionName));
208
209 FunctionType *OrigTy = MergedFunc->getFunctionType();
210 // Get the original params' types.
211 SmallVector<Type *> ParamTypes(OrigTy->param_begin(), OrigTy->param_end());
212 // Append const parameter types that are passed in.
213 ParamTypes.append(ConstParamTypes.begin(), ConstParamTypes.end());
214 FunctionType *FuncType = FunctionType::get(OrigTy->getReturnType(),
215 ParamTypes, /*isVarArg=*/false);
216
217 // Declare a new function
218 Function *NewFunction =
219 Function::Create(FuncType, MergedFunc->getLinkage(), NewFunctionName);
220 if (auto *SP = MergedFunc->getSubprogram())
221 NewFunction->setSubprogram(SP);
222 NewFunction->copyAttributesFrom(MergedFunc);
223 // Preserve entry count for the merged function. Branch weights for blocks
224 // are automatically preserved via splice() which moves the basic blocks.
225 if (auto EC = MergedFunc->getEntryCount())
226 NewFunction->setEntryCount(*EC);
228
230 NewFunction->addFnAttr(Attribute::NoInline);
231
232 // Add the new function before the root function.
233 M->getFunctionList().insert(MergedFunc->getIterator(), NewFunction);
234
235 // Move the body of MergedFunc into the NewFunction.
236 NewFunction->splice(NewFunction->begin(), MergedFunc);
237
238 // Update the original args by the new args.
239 auto NewArgIter = NewFunction->arg_begin();
240 for (Argument &OrigArg : MergedFunc->args()) {
241 Argument &NewArg = *NewArgIter++;
242 OrigArg.replaceAllUsesWith(&NewArg);
243 }
244
245 // Replace the original Constants by the new args.
246 unsigned NumOrigArgs = MergedFunc->arg_size();
247 for (unsigned ParamIdx = 0; ParamIdx < ParamLocsVec.size(); ++ParamIdx) {
248 Argument *NewArg = NewFunction->getArg(NumOrigArgs + ParamIdx);
249 for (auto [InstIndex, OpndIndex] : ParamLocsVec[ParamIdx]) {
250 auto *Inst = FI.IndexInstruction->lookup(InstIndex);
251 auto *OrigC = Inst->getOperand(OpndIndex);
252 if (OrigC->getType() != NewArg->getType()) {
253 IRBuilder<> Builder(Inst->getIterator());
254 Inst->setOperand(OpndIndex,
255 Builder.CreateAggregateCast(NewArg, OrigC->getType()));
256 } else {
257 Inst->setOperand(OpndIndex, NewArg);
258 }
259 }
260 }
261
262 return NewFunction;
263}
264
265// Given the original function (Thunk) and the merged function (ToFunc), create
266// a thunk to the merged function.
268 Function *ToFunc) {
269 auto *Thunk = FI.F;
270
271 assert(Thunk->arg_size() + Params.size() ==
272 ToFunc->getFunctionType()->getNumParams());
273
274 // Save entry count before dropping references (which clears metadata).
275 auto EC = Thunk->getEntryCount();
276
277 Thunk->dropAllReferences();
278
279 BasicBlock *BB = BasicBlock::Create(Thunk->getContext(), "", Thunk);
280 IRBuilder<> Builder(BB);
281
283 unsigned ParamIdx = 0;
284 FunctionType *ToFuncTy = ToFunc->getFunctionType();
285
286 // Add arguments which are passed through Thunk.
287 for (Argument &AI : Thunk->args()) {
288 Args.push_back(
289 Builder.CreateAggregateCast(&AI, ToFuncTy->getParamType(ParamIdx)));
290 ++ParamIdx;
291 }
292
293 // Add new arguments defined by Params.
294 for (auto *Param : Params) {
295 assert(ParamIdx < ToFuncTy->getNumParams());
296 Args.push_back(
297 Builder.CreateAggregateCast(Param, ToFuncTy->getParamType(ParamIdx)));
298 ++ParamIdx;
299 }
300
301 CallInst *CI = Builder.CreateCall(ToFunc, Args);
302 bool isSwiftTailCall = ToFunc->getCallingConv() == CallingConv::SwiftTail &&
303 Thunk->getCallingConv() == CallingConv::SwiftTail;
306 CI->setCallingConv(ToFunc->getCallingConv());
307 CI->setAttributes(ToFunc->getAttributes());
308 if (Thunk->getReturnType()->isVoidTy())
309 Builder.CreateRetVoid();
310 else
311 Builder.CreateRet(Builder.CreateAggregateCast(CI, Thunk->getReturnType()));
312
313 // Restore the thunk's original entry count.
314 if (EC)
315 Thunk->setEntryCount(*EC);
316}
317
318// Check if the old merged/optimized IndexOperandHashMap is compatible with
319// the current IndexOperandHashMap. An operand hash may not be stable across
320// different builds due to varying modules combined. To address this, we relax
321// the hash check condition by comparing Const hash patterns instead of absolute
322// hash values. For example, let's assume we have three Consts located at idx1,
323// idx3, and idx6, where their corresponding hashes are hash1, hash2, and hash1
324// in the old merged map below:
325// Old (Merged): [(idx1, hash1), (idx3, hash2), (idx6, hash1)]
326// Current: [(idx1, hash1'), (idx3, hash2'), (idx6, hash1')]
327// If the current function also has three Consts in the same locations,
328// with hash sequences hash1', hash2', and hash1' where the first and third
329// are the same as the old hash sequences, we consider them matched.
331 const DenseMap<IndexPair, stable_hash> &OldInstOpndIndexToConstHash,
332 const DenseMap<IndexPair, stable_hash> &CurrInstOpndIndexToConstHash) {
333
334 DenseMap<stable_hash, stable_hash> OldHashToCurrHash;
335 for (const auto &[Index, OldHash] : OldInstOpndIndexToConstHash) {
336 auto It = CurrInstOpndIndexToConstHash.find(Index);
337 if (It == CurrInstOpndIndexToConstHash.end())
338 return false;
339
340 auto CurrHash = It->second;
341 auto J = OldHashToCurrHash.find(OldHash);
342 if (J == OldHashToCurrHash.end())
343 OldHashToCurrHash.insert({OldHash, CurrHash});
344 else if (J->second != CurrHash)
345 return false;
346 }
347
348 return true;
349}
350
351// Validate the locations pointed by a param has the same hash and Constant.
352static bool
354 const IndexInstrMap &IndexInstruction,
355 const ParamLocsVecTy &ParamLocsVec) {
356 for (auto &ParamLocs : ParamLocsVec) {
357 std::optional<stable_hash> OldHash;
358 std::optional<Constant *> OldConst;
359 for (auto &Loc : ParamLocs) {
360 assert(SF.IndexOperandHashMap->count(Loc));
361 auto CurrHash = SF.IndexOperandHashMap->at(Loc);
362 auto [InstIndex, OpndIndex] = Loc;
363 assert(InstIndex < IndexInstruction.size());
364 const auto *Inst = IndexInstruction.lookup(InstIndex);
365 auto *CurrConst = cast<Constant>(Inst->getOperand(OpndIndex));
366 if (!OldHash) {
367 OldHash = CurrHash;
368 OldConst = CurrConst;
369 } else if (CurrConst != *OldConst || CurrHash != *OldHash) {
370 return false;
371 }
372 }
373 }
374 return true;
375}
376
377static ParamLocsVecTy
379 std::map<std::vector<stable_hash>, ParamLocs> HashSeqToLocs;
380 auto &RSF = *SFS[0];
381 unsigned StableFunctionCount = SFS.size();
382
383 for (auto &[IndexPair, Hash] : *RSF.IndexOperandHashMap) {
384 // Const hash sequence across stable functions.
385 // We will allocate a parameter per unique hash squence.
386 // can't use SmallVector as key
387 std::vector<stable_hash> ConstHashSeq;
388 ConstHashSeq.push_back(Hash);
389 bool Identical = true;
390 for (unsigned J = 1; J < StableFunctionCount; ++J) {
391 auto &SF = SFS[J];
392 auto SHash = SF->IndexOperandHashMap->at(IndexPair);
393 if (Hash != SHash)
394 Identical = false;
395 ConstHashSeq.push_back(SHash);
396 }
397
398 if (Identical)
399 continue;
400
401 // For each unique Const hash sequence (parameter), add the locations.
402 HashSeqToLocs[ConstHashSeq].push_back(IndexPair);
403 }
404
405 ParamLocsVecTy ParamLocsVec;
406 for (auto &[HashSeq, Locs] : HashSeqToLocs)
407 ParamLocsVec.push_back(std::move(Locs));
408
409 llvm::sort(ParamLocsVec, [&](const ParamLocs &L, const ParamLocs &R) {
410 return L[0] < R[0];
411 });
412
413 return ParamLocsVec;
414}
415
416bool GlobalMergeFunc::merge(Module &M, const StableFunctionMap *FunctionMap) {
417 bool Changed = false;
418
419 // Collect stable functions related to the current module.
421 HashToFuncs;
422 for (auto &F : M) {
423 if (!isEligibleFunction(&F))
424 continue;
425 auto FI = hashFunction(F);
426 if (FunctionMap->contains(FI.FunctionHash))
427 HashToFuncs[FI.FunctionHash].emplace_back(&F, std::move(FI));
428 }
429
430 for (auto &[Hash, Funcs] : HashToFuncs) {
431 std::optional<ParamLocsVecTy> ParamLocsVec;
432 SmallVector<FuncMergeInfo> FuncMergeInfos;
433 auto &SFS = FunctionMap->at(Hash);
434 assert(!SFS.empty());
435 auto &RFS = SFS[0];
436
437 // Iterate functions with the same hash.
438 for (auto &[F, FI] : Funcs) {
439 // Check if the function is compatible with any stable function
440 // in terms of the number of instructions and ignored operands.
441 if (RFS->InstCount != FI.IndexInstruction->size())
442 continue;
443
444 auto hasValidSharedConst = [&](StableFunctionMap::StableFunctionEntry *SF,
445 FunctionHashInfo &FHI) {
446 for (auto &[Index, Hash] : *SF->IndexOperandHashMap) {
447 auto [InstIndex, OpndIndex] = Index;
448 assert(InstIndex < FHI.IndexInstruction->size());
449 auto *Inst = FHI.IndexInstruction->lookup(InstIndex);
450 if (!ignoreOp(Inst, OpndIndex))
451 return false;
452 }
453 return true;
454 };
455 if (!hasValidSharedConst(RFS.get(), FI))
456 continue;
457
458 for (auto &SF : SFS) {
459 assert(SF->InstCount == FI.IndexInstruction->size());
460 assert(hasValidSharedConst(SF.get(), FI));
461 // Check if there is any stable function that is compatiable with the
462 // current one.
464 *FI.IndexOperandHashMap))
465 continue;
466 if (!ParamLocsVec.has_value()) {
467 ParamLocsVec = computeParamInfo(SFS);
468 LLVM_DEBUG(dbgs() << "[GlobalMergeFunc] Merging hash: " << Hash
469 << " with Params " << ParamLocsVec->size() << "\n");
470 }
471 if (!checkConstLocationCompatible(*SF, *FI.IndexInstruction,
472 *ParamLocsVec))
473 continue;
474
475 // If a stable function matching the current one is found,
476 // create a candidate for merging and proceed to the next function.
477 FuncMergeInfos.emplace_back(SF.get(), F, FI.IndexInstruction.get());
478 break;
479 }
480 }
481 unsigned FuncMergeInfoSize = FuncMergeInfos.size();
482 if (FuncMergeInfoSize == 0)
483 continue;
484
485 LLVM_DEBUG(dbgs() << "[GlobalMergeFunc] Merging function count "
486 << FuncMergeInfoSize << " for hash: " << Hash << "\n");
487
488 for (auto &FMI : FuncMergeInfos) {
489 Changed = true;
490
491 // We've already validated all locations of constant operands pointed by
492 // the parameters. Populate parameters pointing to the original constants.
494 SmallVector<Type *> ParamTypes;
495 for (auto &ParamLocs : *ParamLocsVec) {
496 assert(!ParamLocs.empty());
497 auto &[InstIndex, OpndIndex] = ParamLocs[0];
498 auto *Inst = FMI.IndexInstruction->lookup(InstIndex);
499 auto *Opnd = cast<Constant>(Inst->getOperand(OpndIndex));
500 Params.push_back(Opnd);
501 ParamTypes.push_back(Opnd->getType());
502 }
503
504 // Create a merged function derived from the current function.
505 Function *MergedFunc =
506 createMergedFunction(FMI, ParamTypes, *ParamLocsVec);
507
508 LLVM_DEBUG({
509 dbgs() << "[GlobalMergeFunc] Merged function (hash:" << FMI.SF->Hash
510 << ") " << MergedFunc->getName() << " generated from "
511 << FMI.F->getName() << ":\n";
512 MergedFunc->dump();
513 });
514
515 // Transform the current function into a thunk that calls the merged
516 // function.
517 createThunk(FMI, Params, MergedFunc);
518 LLVM_DEBUG({
519 dbgs() << "[GlobalMergeFunc] Thunk generated: \n";
520 FMI.F->dump();
521 });
522 ++NumMergedFunctions;
523 }
524 }
525
526 return Changed;
527}
528
530 // Initialize the local function map regardless of the merger mode.
531 LocalFunctionMap = std::make_unique<StableFunctionMap>();
532
533 // Disable codegen data for merging. The local merge is still enabled.
535 return;
536
537 // (Full)LTO module does not have functions added to the index.
538 // In this case, we run a local merger without using codegen data.
539 if (Index && !Index->hasExportedFunctions(M))
540 return;
541
542 const cl::LibraryOptionsContext &LibraryOptions =
543 M.getContext().getLibraryOptions();
544 if (cgdata::emitCGData(LibraryOptions))
546 else if (cgdata::hasStableFunctionMap(LibraryOptions))
548}
549
551 LLVM_DEBUG(dbgs() << "Emit function map. Size: " << LocalFunctionMap->size()
552 << "\n");
553 // No need to emit the function map if it is empty.
554 if (LocalFunctionMap->empty())
555 return;
557 raw_svector_ostream OS(Buf);
558
559 std::vector<CGDataPatchItem> PatchItems;
560 StableFunctionMapRecord::serialize(OS, LocalFunctionMap.get(), PatchItems);
561 CGDataOStream COS(OS);
562 COS.patch(PatchItems);
563
564 std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer(
565 OS.str(), "in-memory stable function map", false);
566
567 Triple TT(M.getTargetTriple());
568 embedBufferInModule(M, *Buffer,
569 getCodeGenDataSectionName(CG_merge, TT.getObjectFormat()),
570 Align(4));
571}
572
575 const cl::LibraryOptionsContext &LibraryOptions =
576 M.getContext().getLibraryOptions();
577
578 const StableFunctionMap *FuncMap;
579 if (MergerMode == HashFunctionMode::UsingHashFunction) {
580 // Use the prior CG data to optimistically create global merge candidates.
581 FuncMap = cgdata::getStableFunctionMap(LibraryOptions);
582 } else {
583 analyze(M);
584 // Emit the local function map to the custom section, __llvm_merge before
585 // finalizing it.
588 LocalFunctionMap->finalize(LibraryOptions);
589 FuncMap = LocalFunctionMap.get();
590 }
591
592 return merge(M, FuncMap);
593}
594
595namespace {
596
597class GlobalMergeFuncPassWrapper : public ModulePass {
598
599public:
600 static char ID;
601
602 GlobalMergeFuncPassWrapper() : ModulePass(ID) {}
603
604 void getAnalysisUsage(AnalysisUsage &AU) const override {
606 AU.setPreservesAll();
608 }
609
610 StringRef getPassName() const override { return "Global Merge Functions"; }
611
612 bool runOnModule(Module &M) override;
613};
614
615} // namespace
616
617char GlobalMergeFuncPassWrapper::ID = 0;
618INITIALIZE_PASS(GlobalMergeFuncPassWrapper, "global-merge-func",
619 "Global merge function pass", false, false)
620
622 return new GlobalMergeFuncPassWrapper();
623}
624
625bool GlobalMergeFuncPassWrapper::runOnModule(Module &M) {
626 const ModuleSummaryIndex *Index = nullptr;
627 if (auto *IndexWrapperPass =
628 getAnalysisIfAvailable<ImmutableModuleSummaryIndexWrapperPass>())
629 Index = IndexWrapperPass->getIndex();
630
631 return GlobalMergeFunc(Index).run(M);
632}
633
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static FunctionHashInfo hashFunction(const Function &F)
static bool checkConstHashCompatible(const DenseMap< IndexPair, stable_hash > &OldInstOpndIndexToConstHash, const DenseMap< IndexPair, stable_hash > &CurrInstOpndIndexToConstHash)
static ParamLocsVecTy computeParamInfo(const StableFunctionMap::StableFunctionEntries &SFS)
static cl::opt< bool > DisableCGDataForMerging("disable-cgdata-for-merging", cl::Hidden, cl::desc("Disable codegen data for function merging. Local " "merging is still enabled within a module."), cl::init(false))
static bool canParameterizeCallOperand(const CallBase *CI, unsigned OpIdx)
static Function * createMergedFunction(FuncMergeInfo &FI, ArrayRef< Type * > ConstParamTypes, const ParamLocsVecTy &ParamLocsVec)
static void createThunk(FuncMergeInfo &FI, ArrayRef< Constant * > Params, Function *ToFunc)
static bool isEligibleInstructionForConstantSharing(const Instruction *I)
static bool ignoreOp(const Instruction *I, unsigned OpIdx)
bool isEligibleFunction(Function *F)
Returns true if function F is eligible for merging.
static bool checkConstLocationCompatible(const StableFunctionMap::StableFunctionEntry &SF, const IndexInstrMap &IndexInstruction, const ParamLocsVecTy &ParamLocsVec)
static bool isCalleeOperand(const CallBase *CI, unsigned OpIdx)
Returns true if the \OpIdx operand of CI is the callee operand.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Machine Check Debug Module
This is the interface to build a ModuleSummaryIndex for a module.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
A container for analyses that lazily runs them and caches their results.
Represent the analysis usage information of a pass.
AnalysisUsage & addUsedIfAvailable()
Add the specified Pass class to the set of analyses used by this pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:130
size_t size() const
Get the array size.
Definition ArrayRef.h:141
iterator begin() const
Definition ArrayRef.h:129
LLVM Basic Block Representation.
Definition BasicBlock.h:62
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
A wrapper class to abstract writer stream with support of bytes back patching.
LLVM_ABI void patch(ArrayRef< CGDataPatchItem > P)
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
bool isInlineAsm() const
Check if this call is an inline asm statement.
void setCallingConv(CallingConv::ID CC)
bool isOperandBundleOfType(uint32_t ID, unsigned Idx) const
Return true if the operand at index Idx is a bundle operand that has tag ID ID.
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
Value * getCalledOperand() const
const Use & getCalledOperandUse() const
void setAttributes(AttributeList A)
Set the attributes for this call.
This class represents a function call, abstracting a target machine's calling convention.
void setTailCallKind(TailCallKind TCK)
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:766
iterator end()
Definition DenseMap.h:686
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:827
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
void addFnAttr(Attribute::AttrKind Kind)
Add function attributes to this function.
Definition Function.cpp:641
void setSubprogram(DISubprogram *SP)
Set the attached subprogram.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition Function.h:169
void splice(Function::iterator ToIt, Function *FromF)
Transfer all blocks from FromF to this function at ToIt.
Definition Function.h:746
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition Function.h:212
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:273
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:329
iterator begin()
Definition Function.h:838
arg_iterator arg_begin()
Definition Function.h:853
void setEntryCount(uint64_t Count, const DenseSet< GlobalValue::GUID > *Imports=nullptr)
Set the entry count for this function.
Argument * getArg(unsigned i) const
Definition Function.h:871
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:731
void copyAttributesFrom(const Function *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a Function) from the ...
Definition Function.cpp:846
GlobalMergeFunc is a ModulePass that implements a function merging mechanism using stable function ha...
LLVM_ABI void analyze(Module &M)
Analyze module to create stable function into LocalFunctionMap.
LLVM_ABI void initializeMergerMode(const Module &M)
LLVM_ABI bool merge(Module &M, const StableFunctionMap *FunctionMap)
Merge functions in the module using the given function map.
LLVM_ABI void emitFunctionMap(Module &M)
Emit LocalFunctionMap into __llvm_merge section.
LLVM_ABI bool run(Module &M)
static constexpr char MergingInstanceSuffix[]
The suffix used to identify the merged function that parameterizes the constant values.
void setDLLStorageClass(DLLStorageClassTypes C)
void setLinkage(LinkageTypes LT)
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2909
Legacy wrapper pass to provide the ModuleSummaryIndex object.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
ValueT lookup(const KeyT &Key) const
Definition MapVector.h:110
size_type size() const
Definition MapVector.h:58
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:253
Class to hold module path string table and global value map, and encapsulate methods for operating on...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition Pass.cpp:114
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
reference emplace_back(ArgTypes &&... Args)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
const Use & getOperandUse(unsigned i) const
Definition User.h:220
Value * getOperand(unsigned i) const
Definition User.h:207
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:549
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:708
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:315
LLVM_ABI void dump() const
Support for debugging, callable in GDB: V->dump()
A raw_ostream that writes to an SmallVector or SmallString.
StringRef str() const
Return a StringRef for the vector contents.
@ SwiftTail
This follows the Swift calling convention in how arguments are passed but guarantees tail calls will ...
Definition CallingConv.h:87
bool hasStableFunctionMap(const cl::LibraryOptionsContext &LibraryOptions)
const StableFunctionMap * getStableFunctionMap(const cl::LibraryOptionsContext &LibraryOptions)
bool emitCGData(const cl::LibraryOptionsContext &LibraryOptions)
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1685
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
MapVector< unsigned, Instruction * > IndexInstrMap
A map from an instruction index to an instruction pointer.
LLVM_ABI ModulePass * createGlobalMergeFuncPass()
This pass performs merging similar functions globally.
SmallVector< IndexPairHash > IndexOperandHashVecType
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1652
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
StringRef get_stable_name(StringRef Name)
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
SmallVector< IndexPair, 4 > ParamLocs
std::pair< unsigned, unsigned > IndexPair
The pair of an instruction index and a operand index.
LLVM_ABI FunctionHashInfo StructuralHashWithDifferences(const Function &F, IgnoreOperandFunc IgnoreOp, bool SourceQualifyLocalGlobals=false)
Computes a structural hash of a given function, considering the structure and content of the function...
SmallVector< ParamLocs, 8 > ParamLocsVecTy
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1933
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI GlobalVariable * embedBufferInModule(Module &M, MemoryBufferRef Buf, StringRef SectionName, Align Alignment=Align(1), bool SectionExclude=true)
Embed the memory buffer Buf into the module M as a global using the specified section name.
LLVM_ABI std::string getCodeGenDataSectionName(CGDataSectKind CGSK, Triple::ObjectFormatType OF, bool AddSegmentInfo=true)
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
Tuple to hold function info to process merging.
FuncMergeInfo(StableFunctionMap::StableFunctionEntry *SF, Function *F, IndexInstrMap *IndexInstruction)
StableFunctionMap::StableFunctionEntry * SF
IndexInstrMap * IndexInstruction
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
LLVM_ABI PreservedAnalyses run(Module &M, AnalysisManager< Module > &)
const ModuleSummaryIndex * ImportSummary
static LLVM_ABI void serialize(raw_ostream &OS, const StableFunctionMap *FunctionMap, std::vector< CGDataPatchItem > &PatchItems)
A static helper function to serialize the stable function map without owning the stable function map.
An efficient form of StableFunction for fast look-up.
std::unique_ptr< IndexOperandHashMapType > IndexOperandHashMap
A map from an IndexPair to a stable_hash which was skipped.
unsigned InstCount
The number of instructions.
SmallVector< std::unique_ptr< StableFunctionEntry > > StableFunctionEntries
LLVM_ABI const StableFunctionEntries & at(HashFuncsMapType::key_type FunctionHash) const
bool contains(HashFuncsMapType::key_type FunctionHash) const
A stable function is a function with a stable hash while tracking the locations of ignored operands a...