Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
MergeFunctions.cpp
Go to the documentation of this file.
1//===- MergeFunctions.cpp - Merge identical functions ---------------------===//
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 looks for equivalent functions that are mergable and folds them.
10//
11// Order relation is defined on set of functions. It was made through
12// special function comparison procedure that returns
13// 0 when functions are equal,
14// -1 when Left function is less than right function, and
15// 1 for opposite case. We need total-ordering, so we need to maintain
16// four properties on the functions set:
17// a <= a (reflexivity)
18// if a <= b and b <= a then a = b (antisymmetry)
19// if a <= b and b <= c then a <= c (transitivity).
20// for all a and b: a <= b or b <= a (totality).
21//
22// Comparison iterates through each instruction in each basic block.
23// Functions are kept on binary tree. For each new function F we perform
24// lookup in binary tree.
25// In practice it works the following way:
26// -- We define Function* container class with custom "operator<" (FunctionPtr).
27// -- "FunctionPtr" instances are stored in std::set collection, so every
28// std::set::insert operation will give you result in log(N) time.
29//
30// As an optimization, a hash of the function structure is calculated first, and
31// two functions are only compared if they have the same hash. This hash is
32// cheap to compute, and has the property that if function F == G according to
33// the comparison function, then hash(F) == hash(G). This consistency property
34// is critical to ensuring all possible merging opportunities are exploited.
35// Collisions in the hash affect the speed of the pass but not the correctness
36// or determinism of the resulting transformation.
37//
38// When a match is found the functions are folded. If both functions are
39// overridable, we move the functionality into a new internal function and
40// leave two overridable thunks to it.
41//
42//===----------------------------------------------------------------------===//
43//
44// Future work:
45//
46// * virtual functions.
47//
48// Many functions have their address taken by the virtual function table for
49// the object they belong to. However, as long as it's only used for a lookup
50// and call, this is irrelevant, and we'd like to fold such functions.
51//
52// * be smarter about bitcasts.
53//
54// In order to fold functions, we will sometimes add either bitcast instructions
55// or bitcast constant expressions. Unfortunately, this can confound further
56// analysis since the two functions differ where one has a bitcast and the
57// other doesn't. We should learn to look through bitcasts.
58//
59// * Compare complex types with pointer types inside.
60// * Compare cross-reference cases.
61// * Compare complex expressions.
62//
63// All the three issues above could be described as ability to prove that
64// fA == fB == fC == fE == fF == fG in example below:
65//
66// void fA() {
67// fB();
68// }
69// void fB() {
70// fA();
71// }
72//
73// void fE() {
74// fF();
75// }
76// void fF() {
77// fG();
78// }
79// void fG() {
80// fE();
81// }
82//
83// Simplest cross-reference case (fA <--> fB) was implemented in previous
84// versions of MergeFunctions, though it presented only in two function pairs
85// in test-suite (that counts >50k functions)
86// Though possibility to detect complex cross-referencing (e.g.: A->B->C->D->A)
87// could cover much more cases.
88//
89//===----------------------------------------------------------------------===//
90
91#include "llvm/Transforms/IPO/MergeFunctions.h"
92#include "llvm/ADT/ArrayRef.h"
93#include "llvm/ADT/SmallVector.h"
94#include "llvm/ADT/Statistic.h"
95#include "llvm/IR/Argument.h"
96#include "llvm/IR/BasicBlock.h"
97#include "llvm/IR/Constants.h"
98#include "llvm/IR/DebugInfoMetadata.h"
99#include "llvm/IR/DebugLoc.h"
100#include "llvm/IR/DerivedTypes.h"
101#include "llvm/IR/Function.h"
102#include "llvm/IR/GlobalValue.h"
103#include "llvm/IR/IRBuilder.h"
104#include "llvm/IR/InstrTypes.h"
105#include "llvm/IR/Instruction.h"
106#include "llvm/IR/Instructions.h"
107#include "llvm/IR/IntrinsicInst.h"
108#include "llvm/IR/Module.h"
109#include "llvm/IR/StructuralHash.h"
110#include "llvm/IR/Type.h"
111#include "llvm/IR/Use.h"
112#include "llvm/IR/User.h"
113#include "llvm/IR/Value.h"
114#include "llvm/IR/ValueHandle.h"
115#include "llvm/Support/Casting.h"
116#include "llvm/Support/CommandLine.h"
117#include "llvm/Support/Debug.h"
118#include "llvm/Support/raw_ostream.h"
119#include "llvm/Transforms/IPO.h"
120#include "llvm/Transforms/Utils/FunctionComparator.h"
121#include "llvm/Transforms/Utils/ModuleUtils.h"
122#include <algorithm>
123#include <cassert>
124#include <iterator>
125#include <set>
126#include <utility>
127#include <vector>
128
129using namespacellvm;
130
131#define DEBUG_TYPE "mergefunc"
132
133STATISTIC(NumFunctionsMerged,"Number of functions merged");
134STATISTIC(NumThunksWritten,"Number of thunks generated");
135STATISTIC(NumAliasesWritten,"Number of aliases generated");
136STATISTIC(NumDoubleWeak,"Number of new functions created");
137
138staticcl::opt<unsigned>NumFunctionsForVerificationCheck(
139"mergefunc-verify",
140cl::desc("How many functions in a module could be used for "
141"MergeFunctions to pass a basic correctness check. "
142"'0' disables this check. Works only with '-debug' key."),
143cl::init(0),cl::Hidden);
144
145// Under option -mergefunc-preserve-debug-info we:
146// - Do not create a new function for a thunk.
147// - Retain the debug info for a thunk's parameters (and associated
148// instructions for the debug info) from the entry block.
149// Note: -debug will display the algorithm at work.
150// - Create debug-info for the call (to the shared implementation) made by
151// a thunk and its return value.
152// - Erase the rest of the function, retaining the (minimally sized) entry
153// block to create a thunk.
154// - Preserve a thunk's call site to point to the thunk even when both occur
155// within the same translation unit, to aid debugability. Note that this
156// behaviour differs from the underlying -mergefunc implementation which
157// modifies the thunk's call site to point to the shared implementation
158// when both occur within the same translation unit.
159staticcl::opt<bool>
160MergeFunctionsPDI("mergefunc-preserve-debug-info",cl::Hidden,
161cl::init(false),
162cl::desc("Preserve debug info in thunk when mergefunc "
163"transformations are made."));
164
165staticcl::opt<bool>
166MergeFunctionsAliases("mergefunc-use-aliases",cl::Hidden,
167cl::init(false),
168cl::desc("Allow mergefunc to create aliases"));
169
170namespace{
171
172classFunctionNode {
173mutableAssertingVH<Function>F;
174stable_hash Hash;
175
176public:
177// Note the hash is recalculated potentially multiple times, but it is cheap.
178 FunctionNode(Function *F) :F(F), Hash(StructuralHash(*F)) {}
179
180Function *getFunc() const{returnF; }
181stable_hash getHash() const{return Hash; }
182
183 /// Replace the reference to the function F by the function G, assuming their
184 /// implementations are equal.
185void replaceBy(Function *G) const{
186F =G;
187 }
188};
189
190/// MergeFunctions finds functions which will generate identical machine code,
191/// by considering all pointer types to be equivalent. Once identified,
192/// MergeFunctions will fold them by replacing a call to one to a call to a
193/// bitcast of the other.
194classMergeFunctions {
195public:
196 MergeFunctions() : FnTree(FunctionNodeCmp(&GlobalNumbers)) {
197 }
198
199template <typename FuncContainer>boolrun(FuncContainer &Functions);
200DenseMap<Function *, Function *> runOnFunctions(ArrayRef<Function *>F);
201
202SmallPtrSet<GlobalValue *, 4> &getUsed();
203
204private:
205// The function comparison operator is provided here so that FunctionNodes do
206// not need to become larger with another pointer.
207classFunctionNodeCmp {
208GlobalNumberState* GlobalNumbers;
209
210public:
211 FunctionNodeCmp(GlobalNumberState* GN) : GlobalNumbers(GN) {}
212
213bool operator()(const FunctionNode &LHS,const FunctionNode &RHS) const{
214// Order first by hashes, then full function comparison.
215if (LHS.getHash() !=RHS.getHash())
216returnLHS.getHash() <RHS.getHash();
217FunctionComparator FCmp(LHS.getFunc(),RHS.getFunc(), GlobalNumbers);
218return FCmp.compare() < 0;
219 }
220 };
221usingFnTreeType = std::set<FunctionNode, FunctionNodeCmp>;
222
223GlobalNumberState GlobalNumbers;
224
225 /// A work queue of functions that may have been modified and should be
226 /// analyzed again.
227 std::vector<WeakTrackingVH> Deferred;
228
229 /// Set of values marked as used in llvm.used and llvm.compiler.used.
230SmallPtrSet<GlobalValue *, 4>Used;
231
232#ifndef NDEBUG
233 /// Checks the rules of order relation introduced among functions set.
234 /// Returns true, if check has been passed, and false if failed.
235bool doFunctionalCheck(std::vector<WeakTrackingVH> &Worklist);
236#endif
237
238 /// Insert a ComparableFunction into the FnTree, or merge it away if it's
239 /// equal to one that's already present.
240bool insert(Function *NewFunction);
241
242 /// Remove a Function from the FnTree and queue it up for a second sweep of
243 /// analysis.
244voidremove(Function *F);
245
246 /// Find the functions that use this Value and remove them from FnTree and
247 /// queue the functions.
248void removeUsers(Value *V);
249
250 /// Replace all direct calls of Old with calls of New. Will bitcast New if
251 /// necessary to make types match.
252void replaceDirectCallers(Function *Old,Function *New);
253
254 /// Merge two equivalent functions. Upon completion, G may be deleted, or may
255 /// be converted into a thunk. In either case, it should never be visited
256 /// again.
257void mergeTwoFunctions(Function *F,Function *G);
258
259 /// Fill PDIUnrelatedWL with instructions from the entry block that are
260 /// unrelated to parameter related debug info.
261 /// \param PDVRUnrelatedWL The equivalent non-intrinsic debug records.
262void
263 filterInstsUnrelatedToPDI(BasicBlock *GEntryBlock,
264 std::vector<Instruction *> &PDIUnrelatedWL,
265 std::vector<DbgVariableRecord *> &PDVRUnrelatedWL);
266
267 /// Erase the rest of the CFG (i.e. barring the entry block).
268void eraseTail(Function *G);
269
270 /// Erase the instructions in PDIUnrelatedWL as they are unrelated to the
271 /// parameter debug info, from the entry block.
272 /// \param PDVRUnrelatedWL contains the equivalent set of non-instruction
273 /// debug-info records.
274void
275 eraseInstsUnrelatedToPDI(std::vector<Instruction *> &PDIUnrelatedWL,
276 std::vector<DbgVariableRecord *> &PDVRUnrelatedWL);
277
278 /// Replace G with a simple tail call to bitcast(F). Also (unless
279 /// MergeFunctionsPDI holds) replace direct uses of G with bitcast(F),
280 /// delete G.
281void writeThunk(Function *F,Function *G);
282
283// Replace G with an alias to F (deleting function G)
284void writeAlias(Function *F,Function *G);
285
286// Replace G with an alias to F if possible, or a thunk to F if possible.
287// Returns false if neither is the case.
288bool writeThunkOrAlias(Function *F,Function *G);
289
290 /// Replace function F with function G in the function tree.
291void replaceFunctionInTree(const FunctionNode &FN,Function *G);
292
293 /// The set of all distinct functions. Use the insert() and remove() methods
294 /// to modify it. The map allows efficient lookup and deferring of Functions.
295 FnTreeType FnTree;
296
297// Map functions to the iterators of the FunctionNode which contains them
298// in the FnTree. This must be updated carefully whenever the FnTree is
299// modified, i.e. in insert(), remove(), and replaceFunctionInTree(), to avoid
300// dangling iterators into FnTree. The invariant that preserves this is that
301// there is exactly one mapping F -> FN for each FunctionNode FN in FnTree.
302DenseMap<AssertingVH<Function>, FnTreeType::iterator> FNodesInTree;
303
304 /// Deleted-New functions mapping
305DenseMap<Function *, Function *> DelToNewMap;
306};
307}// end anonymous namespace
308
309PreservedAnalysesMergeFunctionsPass::run(Module &M,
310ModuleAnalysisManager &AM) {
311if (!MergeFunctionsPass::runOnModule(M))
312returnPreservedAnalyses::all();
313returnPreservedAnalyses::none();
314}
315
316SmallPtrSet<GlobalValue *, 4> &MergeFunctions::getUsed() {return Used; }
317
318boolMergeFunctionsPass::runOnModule(Module &M) {
319 MergeFunctions MF;
320SmallVector<GlobalValue *, 4> UsedV;
321collectUsedGlobalVariables(M, UsedV,/*CompilerUsed=*/false);
322collectUsedGlobalVariables(M, UsedV,/*CompilerUsed=*/true);
323 MF.getUsed().insert(UsedV.begin(), UsedV.end());
324return MF.run(M);
325}
326
327DenseMap<Function *, Function *>
328MergeFunctionsPass::runOnFunctions(ArrayRef<Function *>F) {
329 MergeFunctions MF;
330return MF.runOnFunctions(F);
331}
332
333#ifndef NDEBUG
334bool MergeFunctions::doFunctionalCheck(std::vector<WeakTrackingVH> &Worklist) {
335if (constunsigned Max =NumFunctionsForVerificationCheck) {
336unsigned TripleNumber = 0;
337bool Valid =true;
338
339dbgs() <<"MERGEFUNC-VERIFY: Started for first " << Max <<" functions.\n";
340
341unsigned i = 0;
342for (std::vector<WeakTrackingVH>::iteratorI = Worklist.begin(),
343 E = Worklist.end();
344I != E && i < Max; ++I, ++i) {
345unsigned j = i;
346for (std::vector<WeakTrackingVH>::iterator J =I; J != E && j < Max;
347 ++J, ++j) {
348Function *F1 = cast<Function>(*I);
349Function *F2 = cast<Function>(*J);
350int Res1 =FunctionComparator(F1, F2, &GlobalNumbers).compare();
351int Res2 =FunctionComparator(F2, F1, &GlobalNumbers).compare();
352
353// If F1 <= F2, then F2 >= F1, otherwise report failure.
354if (Res1 != -Res2) {
355dbgs() <<"MERGEFUNC-VERIFY: Non-symmetric; triple: " << TripleNumber
356 <<"\n";
357dbgs() << *F1 <<'\n' << *F2 <<'\n';
358 Valid =false;
359 }
360
361if (Res1 == 0)
362continue;
363
364unsigned k = j;
365for (std::vector<WeakTrackingVH>::iterator K = J; K != E && k < Max;
366 ++k, ++K, ++TripleNumber) {
367if (K == J)
368continue;
369
370Function *F3 = cast<Function>(*K);
371int Res3 =FunctionComparator(F1, F3, &GlobalNumbers).compare();
372int Res4 =FunctionComparator(F2, F3, &GlobalNumbers).compare();
373
374bool Transitive =true;
375
376if (Res1 != 0 && Res1 == Res4) {
377// F1 > F2, F2 > F3 => F1 > F3
378 Transitive = Res3 == Res1;
379 }elseif (Res3 != 0 && Res3 == -Res4) {
380// F1 > F3, F3 > F2 => F1 > F2
381 Transitive = Res3 == Res1;
382 }elseif (Res4 != 0 && -Res3 == Res4) {
383// F2 > F3, F3 > F1 => F2 > F1
384 Transitive = Res4 == -Res1;
385 }
386
387if (!Transitive) {
388dbgs() <<"MERGEFUNC-VERIFY: Non-transitive; triple: "
389 << TripleNumber <<"\n";
390dbgs() <<"Res1, Res3, Res4: " << Res1 <<", " << Res3 <<", "
391 << Res4 <<"\n";
392dbgs() << *F1 <<'\n' << *F2 <<'\n' << *F3 <<'\n';
393 Valid =false;
394 }
395 }
396 }
397 }
398
399dbgs() <<"MERGEFUNC-VERIFY: " << (Valid ?"Passed." :"Failed.") <<"\n";
400return Valid;
401 }
402returntrue;
403}
404#endif
405
406/// Check whether \p F has an intrinsic which references
407/// distinct metadata as an operand. The most common
408/// instance of this would be CFI checks for function-local types.
409staticboolhasDistinctMetadataIntrinsic(constFunction &F) {
410for (constBasicBlock &BB :F) {
411for (constInstruction &I : BB.instructionsWithoutDebug()) {
412if (!isa<IntrinsicInst>(&I))
413continue;
414
415for (Value *Op :I.operands()) {
416auto *MDL = dyn_cast<MetadataAsValue>(Op);
417if (!MDL)
418continue;
419if (MDNode *N = dyn_cast<MDNode>(MDL->getMetadata()))
420if (N->isDistinct())
421returntrue;
422 }
423 }
424 }
425returnfalse;
426}
427
428/// Check whether \p F is eligible for function merging.
429staticboolisEligibleForMerging(Function &F) {
430return !F.isDeclaration() && !F.hasAvailableExternallyLinkage() &&
431 !hasDistinctMetadataIntrinsic(F);
432}
433
434inlineFunction *asPtr(Function *Fn) {return Fn; }
435inlineFunction *asPtr(Function &Fn) {return &Fn; }
436
437template <typename FuncContainer>bool MergeFunctions::run(FuncContainer &M) {
438bool Changed =false;
439
440// All functions in the module, ordered by hash. Functions with a unique
441// hash value are easily eliminated.
442 std::vector<std::pair<stable_hash, Function *>> HashedFuncs;
443for (auto &Func : M) {
444Function *FuncPtr =asPtr(Func);
445if (isEligibleForMerging(*FuncPtr)) {
446 HashedFuncs.push_back({StructuralHash(*FuncPtr), FuncPtr});
447 }
448 }
449
450llvm::stable_sort(HashedFuncs,less_first());
451
452auto S = HashedFuncs.begin();
453for (autoI = HashedFuncs.begin(), IE = HashedFuncs.end();I != IE; ++I) {
454// If the hash value matches the previous value or the next one, we must
455// consider merging it. Otherwise it is dropped and never considered again.
456if ((I != S && std::prev(I)->first ==I->first) ||
457 (std::next(I) != IE && std::next(I)->first ==I->first)) {
458 Deferred.push_back(WeakTrackingVH(I->second));
459 }
460 }
461
462do {
463 std::vector<WeakTrackingVH> Worklist;
464 Deferred.swap(Worklist);
465
466LLVM_DEBUG(doFunctionalCheck(Worklist));
467
468LLVM_DEBUG(dbgs() <<"size of module: " <<M.size() <<'\n');
469LLVM_DEBUG(dbgs() <<"size of worklist: " << Worklist.size() <<'\n');
470
471// Insert functions and merge them.
472for (WeakTrackingVH &I : Worklist) {
473if (!I)
474continue;
475Function *F = cast<Function>(I);
476if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage()) {
477 Changed |= insert(F);
478 }
479 }
480LLVM_DEBUG(dbgs() <<"size of FnTree: " << FnTree.size() <<'\n');
481 }while (!Deferred.empty());
482
483 FnTree.clear();
484 FNodesInTree.clear();
485 GlobalNumbers.clear();
486Used.clear();
487
488return Changed;
489}
490
491DenseMap<Function *, Function *>
492MergeFunctions::runOnFunctions(ArrayRef<Function *>F) {
493 [[maybe_unused]]bool MergeResult = this->run(F);
494assert(MergeResult == !DelToNewMap.empty());
495return this->DelToNewMap;
496}
497
498// Replace direct callers of Old with New.
499void MergeFunctions::replaceDirectCallers(Function *Old,Function *New) {
500for (Use &U :make_early_inc_range(Old->uses())) {
501CallBase *CB = dyn_cast<CallBase>(U.getUser());
502if (CB && CB->isCallee(&U)) {
503// Do not copy attributes from the called function to the call-site.
504// Function comparison ensures that the attributes are the same up to
505// type congruences in byval(), in which case we need to keep the byval
506// type of the call-site, not the callee function.
507remove(CB->getFunction());
508U.set(New);
509 }
510 }
511}
512
513// Helper for writeThunk,
514// Selects proper bitcast operation,
515// but a bit simpler then CastInst::getCastOpcode.
516staticValue *createCast(IRBuilder<> &Builder,Value *V,Type *DestTy) {
517Type *SrcTy = V->getType();
518if (SrcTy->isStructTy()) {
519assert(DestTy->isStructTy());
520assert(SrcTy->getStructNumElements() == DestTy->getStructNumElements());
521Value *Result =PoisonValue::get(DestTy);
522for (unsignedintI = 0, E = SrcTy->getStructNumElements();I < E; ++I) {
523Value *Element =
524createCast(Builder, Builder.CreateExtractValue(V,ArrayRef(I)),
525 DestTy->getStructElementType(I));
526
527 Result = Builder.CreateInsertValue(Result, Element,ArrayRef(I));
528 }
529return Result;
530 }
531assert(!DestTy->isStructTy());
532if (SrcTy->isIntegerTy() && DestTy->isPointerTy())
533return Builder.CreateIntToPtr(V, DestTy);
534elseif (SrcTy->isPointerTy() && DestTy->isIntegerTy())
535return Builder.CreatePtrToInt(V, DestTy);
536else
537return Builder.CreateBitCast(V, DestTy);
538}
539
540// Erase the instructions in PDIUnrelatedWL as they are unrelated to the
541// parameter debug info, from the entry block.
542void MergeFunctions::eraseInstsUnrelatedToPDI(
543 std::vector<Instruction *> &PDIUnrelatedWL,
544 std::vector<DbgVariableRecord *> &PDVRUnrelatedWL) {
545LLVM_DEBUG(
546dbgs() <<" Erasing instructions (in reverse order of appearance in "
547"entry block) unrelated to parameter debug info from entry "
548"block: {\n");
549while (!PDIUnrelatedWL.empty()) {
550Instruction *I = PDIUnrelatedWL.back();
551LLVM_DEBUG(dbgs() <<" Deleting Instruction: ");
552LLVM_DEBUG(I->print(dbgs()));
553LLVM_DEBUG(dbgs() <<"\n");
554I->eraseFromParent();
555 PDIUnrelatedWL.pop_back();
556 }
557
558while (!PDVRUnrelatedWL.empty()) {
559DbgVariableRecord *DVR = PDVRUnrelatedWL.back();
560LLVM_DEBUG(dbgs() <<" Deleting DbgVariableRecord ");
561LLVM_DEBUG(DVR->print(dbgs()));
562LLVM_DEBUG(dbgs() <<"\n");
563 DVR->eraseFromParent();
564 PDVRUnrelatedWL.pop_back();
565 }
566
567LLVM_DEBUG(dbgs() <<" } // Done erasing instructions unrelated to parameter "
568"debug info from entry block. \n");
569}
570
571// Reduce G to its entry block.
572void MergeFunctions::eraseTail(Function *G) {
573 std::vector<BasicBlock *> WorklistBB;
574for (BasicBlock &BB :drop_begin(*G)) {
575 BB.dropAllReferences();
576 WorklistBB.push_back(&BB);
577 }
578while (!WorklistBB.empty()) {
579BasicBlock *BB = WorklistBB.back();
580 BB->eraseFromParent();
581 WorklistBB.pop_back();
582 }
583}
584
585// We are interested in the following instructions from the entry block as being
586// related to parameter debug info:
587// - @llvm.dbg.declare
588// - stores from the incoming parameters to locations on the stack-frame
589// - allocas that create these locations on the stack-frame
590// - @llvm.dbg.value
591// - the entry block's terminator
592// The rest are unrelated to debug info for the parameters; fill up
593// PDIUnrelatedWL with such instructions.
594void MergeFunctions::filterInstsUnrelatedToPDI(
595BasicBlock *GEntryBlock, std::vector<Instruction *> &PDIUnrelatedWL,
596 std::vector<DbgVariableRecord *> &PDVRUnrelatedWL) {
597 std::set<Instruction *> PDIRelated;
598 std::set<DbgVariableRecord *> PDVRRelated;
599
600// Work out whether a dbg.value intrinsic or an equivalent DbgVariableRecord
601// is a parameter to be preserved.
602auto ExamineDbgValue = [](auto *DbgVal,auto &Container) {
603LLVM_DEBUG(dbgs() <<" Deciding: ");
604LLVM_DEBUG(DbgVal->print(dbgs()));
605LLVM_DEBUG(dbgs() <<"\n");
606DILocalVariable *DILocVar = DbgVal->getVariable();
607if (DILocVar->isParameter()) {
608LLVM_DEBUG(dbgs() <<" Include (parameter): ");
609LLVM_DEBUG(DbgVal->print(dbgs()));
610LLVM_DEBUG(dbgs() <<"\n");
611 Container.insert(DbgVal);
612 }else {
613LLVM_DEBUG(dbgs() <<" Delete (!parameter): ");
614LLVM_DEBUG(DbgVal->print(dbgs()));
615LLVM_DEBUG(dbgs() <<"\n");
616 }
617 };
618
619auto ExamineDbgDeclare = [&PDIRelated](auto *DbgDecl,auto &Container) {
620LLVM_DEBUG(dbgs() <<" Deciding: ");
621LLVM_DEBUG(DbgDecl->print(dbgs()));
622LLVM_DEBUG(dbgs() <<"\n");
623DILocalVariable *DILocVar = DbgDecl->getVariable();
624if (DILocVar->isParameter()) {
625LLVM_DEBUG(dbgs() <<" Parameter: ");
626LLVM_DEBUG(DILocVar->print(dbgs()));
627AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DbgDecl->getAddress());
628if (AI) {
629LLVM_DEBUG(dbgs() <<" Processing alloca users: ");
630LLVM_DEBUG(dbgs() <<"\n");
631for (User *U : AI->users()) {
632if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
633if (Value *Arg =SI->getValueOperand()) {
634if (isa<Argument>(Arg)) {
635LLVM_DEBUG(dbgs() <<" Include: ");
636LLVM_DEBUG(AI->print(dbgs()));
637LLVM_DEBUG(dbgs() <<"\n");
638 PDIRelated.insert(AI);
639LLVM_DEBUG(dbgs() <<" Include (parameter): ");
640LLVM_DEBUG(SI->print(dbgs()));
641LLVM_DEBUG(dbgs() <<"\n");
642 PDIRelated.insert(SI);
643LLVM_DEBUG(dbgs() <<" Include: ");
644LLVM_DEBUG(DbgDecl->print(dbgs()));
645LLVM_DEBUG(dbgs() <<"\n");
646 Container.insert(DbgDecl);
647 }else {
648LLVM_DEBUG(dbgs() <<" Delete (!parameter): ");
649LLVM_DEBUG(SI->print(dbgs()));
650LLVM_DEBUG(dbgs() <<"\n");
651 }
652 }
653 }else {
654LLVM_DEBUG(dbgs() <<" Defer: ");
655LLVM_DEBUG(U->print(dbgs()));
656LLVM_DEBUG(dbgs() <<"\n");
657 }
658 }
659 }else {
660LLVM_DEBUG(dbgs() <<" Delete (alloca NULL): ");
661LLVM_DEBUG(DbgDecl->print(dbgs()));
662LLVM_DEBUG(dbgs() <<"\n");
663 }
664 }else {
665LLVM_DEBUG(dbgs() <<" Delete (!parameter): ");
666LLVM_DEBUG(DbgDecl->print(dbgs()));
667LLVM_DEBUG(dbgs() <<"\n");
668 }
669 };
670
671for (BasicBlock::iterator BI = GEntryBlock->begin(), BIE = GEntryBlock->end();
672 BI != BIE; ++BI) {
673// Examine DbgVariableRecords as they happen "before" the instruction. Are
674// they connected to parameters?
675for (DbgVariableRecord &DVR :filterDbgVars(BI->getDbgRecordRange())) {
676if (DVR.isDbgValue() || DVR.isDbgAssign()) {
677 ExamineDbgValue(&DVR, PDVRRelated);
678 }else {
679assert(DVR.isDbgDeclare());
680 ExamineDbgDeclare(&DVR, PDVRRelated);
681 }
682 }
683
684if (auto *DVI = dyn_cast<DbgValueInst>(&*BI)) {
685 ExamineDbgValue(DVI, PDIRelated);
686 }elseif (auto *DDI = dyn_cast<DbgDeclareInst>(&*BI)) {
687 ExamineDbgDeclare(DDI, PDIRelated);
688 }elseif (BI->isTerminator() && &*BI == GEntryBlock->getTerminator()) {
689LLVM_DEBUG(dbgs() <<" Will Include Terminator: ");
690LLVM_DEBUG(BI->print(dbgs()));
691LLVM_DEBUG(dbgs() <<"\n");
692 PDIRelated.insert(&*BI);
693 }else {
694LLVM_DEBUG(dbgs() <<" Defer: ");
695LLVM_DEBUG(BI->print(dbgs()));
696LLVM_DEBUG(dbgs() <<"\n");
697 }
698 }
699LLVM_DEBUG(
700dbgs()
701 <<" Report parameter debug info related/related instructions: {\n");
702
703auto IsPDIRelated = [](auto *Rec,auto &Container,auto &UnrelatedCont) {
704if (Container.find(Rec) == Container.end()) {
705LLVM_DEBUG(dbgs() <<" !PDIRelated: ");
706LLVM_DEBUG(Rec->print(dbgs()));
707LLVM_DEBUG(dbgs() <<"\n");
708 UnrelatedCont.push_back(Rec);
709 }else {
710LLVM_DEBUG(dbgs() <<" PDIRelated: ");
711LLVM_DEBUG(Rec->print(dbgs()));
712LLVM_DEBUG(dbgs() <<"\n");
713 }
714 };
715
716// Collect the set of unrelated instructions and debug records.
717for (Instruction &I : *GEntryBlock) {
718for (DbgVariableRecord &DVR :filterDbgVars(I.getDbgRecordRange()))
719 IsPDIRelated(&DVR, PDVRRelated, PDVRUnrelatedWL);
720 IsPDIRelated(&I, PDIRelated, PDIUnrelatedWL);
721 }
722LLVM_DEBUG(dbgs() <<" }\n");
723}
724
725/// Whether this function may be replaced by a forwarding thunk.
726staticboolcanCreateThunkFor(Function *F) {
727if (F->isVarArg())
728returnfalse;
729
730// Don't merge tiny functions using a thunk, since it can just end up
731// making the function larger.
732if (F->size() == 1) {
733if (F->front().sizeWithoutDebug() < 2) {
734LLVM_DEBUG(dbgs() <<"canCreateThunkFor: " <<F->getName()
735 <<" is too small to bother creating a thunk for\n");
736returnfalse;
737 }
738 }
739returntrue;
740}
741
742/// Copy all metadata of a specific kind from one function to another.
743staticvoidcopyMetadataIfPresent(Function *From,Function *To,
744StringRef Kind) {
745SmallVector<MDNode *, 4> MDs;
746From->getMetadata(Kind, MDs);
747for (MDNode *MD : MDs)
748 To->addMetadata(Kind, *MD);
749}
750
751// Replace G with a simple tail call to bitcast(F). Also (unless
752// MergeFunctionsPDI holds) replace direct uses of G with bitcast(F),
753// delete G. Under MergeFunctionsPDI, we use G itself for creating
754// the thunk as we preserve the debug info (and associated instructions)
755// from G's entry block pertaining to G's incoming arguments which are
756// passed on as corresponding arguments in the call that G makes to F.
757// For better debugability, under MergeFunctionsPDI, we do not modify G's
758// call sites to point to F even when within the same translation unit.
759void MergeFunctions::writeThunk(Function *F,Function *G) {
760BasicBlock *GEntryBlock =nullptr;
761 std::vector<Instruction *> PDIUnrelatedWL;
762 std::vector<DbgVariableRecord *> PDVRUnrelatedWL;
763BasicBlock *BB =nullptr;
764Function *NewG =nullptr;
765if (MergeFunctionsPDI) {
766LLVM_DEBUG(dbgs() <<"writeThunk: (MergeFunctionsPDI) Do not create a new "
767"function as thunk; retain original: "
768 <<G->getName() <<"()\n");
769 GEntryBlock = &G->getEntryBlock();
770LLVM_DEBUG(
771dbgs() <<"writeThunk: (MergeFunctionsPDI) filter parameter related "
772"debug info for "
773 <<G->getName() <<"() {\n");
774 filterInstsUnrelatedToPDI(GEntryBlock, PDIUnrelatedWL, PDVRUnrelatedWL);
775 GEntryBlock->getTerminator()->eraseFromParent();
776 BB = GEntryBlock;
777 }else {
778 NewG =Function::Create(G->getFunctionType(),G->getLinkage(),
779G->getAddressSpace(),"",G->getParent());
780 NewG->setComdat(G->getComdat());
781 NewG->IsNewDbgInfoFormat =G->IsNewDbgInfoFormat;
782 BB =BasicBlock::Create(F->getContext(),"", NewG);
783 }
784
785IRBuilder<> Builder(BB);
786Function *H =MergeFunctionsPDI ?G : NewG;
787SmallVector<Value *, 16>Args;
788unsigned i = 0;
789FunctionType *FFTy =F->getFunctionType();
790for (Argument &AI :H->args()) {
791Args.push_back(createCast(Builder, &AI, FFTy->getParamType(i)));
792 ++i;
793 }
794
795CallInst *CI = Builder.CreateCall(F, Args);
796ReturnInst *RI =nullptr;
797bool isSwiftTailCall =F->getCallingConv() ==CallingConv::SwiftTail &&
798G->getCallingConv() ==CallingConv::SwiftTail;
799 CI->setTailCallKind(isSwiftTailCall ?CallInst::TCK_MustTail
800 :CallInst::TCK_Tail);
801 CI->setCallingConv(F->getCallingConv());
802 CI->setAttributes(F->getAttributes());
803if (H->getReturnType()->isVoidTy()) {
804 RI = Builder.CreateRetVoid();
805 }else {
806 RI = Builder.CreateRet(createCast(Builder, CI,H->getReturnType()));
807 }
808
809if (MergeFunctionsPDI) {
810DISubprogram *DIS =G->getSubprogram();
811if (DIS) {
812DebugLoc CIDbgLoc =
813DILocation::get(DIS->getContext(), DIS->getScopeLine(), 0, DIS);
814DebugLoc RIDbgLoc =
815DILocation::get(DIS->getContext(), DIS->getScopeLine(), 0, DIS);
816 CI->setDebugLoc(CIDbgLoc);
817 RI->setDebugLoc(RIDbgLoc);
818 }else {
819LLVM_DEBUG(
820dbgs() <<"writeThunk: (MergeFunctionsPDI) No DISubprogram for "
821 <<G->getName() <<"()\n");
822 }
823 eraseTail(G);
824 eraseInstsUnrelatedToPDI(PDIUnrelatedWL, PDVRUnrelatedWL);
825LLVM_DEBUG(
826dbgs() <<"} // End of parameter related debug info filtering for: "
827 <<G->getName() <<"()\n");
828 }else {
829 NewG->copyAttributesFrom(G);
830 NewG->takeName(G);
831// Ensure CFI type metadata is propagated to the new function.
832copyMetadataIfPresent(G, NewG,"type");
833copyMetadataIfPresent(G, NewG,"kcfi_type");
834 removeUsers(G);
835G->replaceAllUsesWith(NewG);
836G->eraseFromParent();
837 }
838
839LLVM_DEBUG(dbgs() <<"writeThunk: " <<H->getName() <<'\n');
840 ++NumThunksWritten;
841}
842
843// Whether this function may be replaced by an alias
844staticboolcanCreateAliasFor(Function *F) {
845if (!MergeFunctionsAliases || !F->hasGlobalUnnamedAddr())
846returnfalse;
847
848// We should only see linkages supported by aliases here
849assert(F->hasLocalLinkage() ||F->hasExternalLinkage()
850 ||F->hasWeakLinkage() ||F->hasLinkOnceLinkage());
851returntrue;
852}
853
854// Replace G with an alias to F (deleting function G)
855void MergeFunctions::writeAlias(Function *F,Function *G) {
856PointerType *PtrType =G->getType();
857auto *GA =GlobalAlias::create(G->getValueType(), PtrType->getAddressSpace(),
858G->getLinkage(),"",F,G->getParent());
859
860constMaybeAlign FAlign =F->getAlign();
861constMaybeAlign GAlign =G->getAlign();
862if (FAlign || GAlign)
863F->setAlignment(std::max(FAlign.valueOrOne(), GAlign.valueOrOne()));
864else
865F->setAlignment(std::nullopt);
866 GA->takeName(G);
867 GA->setVisibility(G->getVisibility());
868 GA->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
869
870 removeUsers(G);
871G->replaceAllUsesWith(GA);
872G->eraseFromParent();
873
874LLVM_DEBUG(dbgs() <<"writeAlias: " << GA->getName() <<'\n');
875 ++NumAliasesWritten;
876}
877
878// Replace G with an alias to F if possible, or a thunk to F if
879// profitable. Returns false if neither is the case.
880bool MergeFunctions::writeThunkOrAlias(Function *F,Function *G) {
881if (canCreateAliasFor(G)) {
882 writeAlias(F,G);
883returntrue;
884 }
885if (canCreateThunkFor(F)) {
886 writeThunk(F,G);
887returntrue;
888 }
889returnfalse;
890}
891
892// Merge two equivalent functions. Upon completion, Function G is deleted.
893void MergeFunctions::mergeTwoFunctions(Function *F,Function *G) {
894if (F->isInterposable()) {
895assert(G->isInterposable());
896
897// Both writeThunkOrAlias() calls below must succeed, either because we can
898// create aliases for G and NewF, or because a thunk for F is profitable.
899// F here has the same signature as NewF below, so that's what we check.
900if (!canCreateThunkFor(F) &&
901 (!canCreateAliasFor(F) || !canCreateAliasFor(G)))
902return;
903
904// Make them both thunks to the same internal function.
905Function *NewF =Function::Create(F->getFunctionType(),F->getLinkage(),
906F->getAddressSpace(),"",F->getParent());
907 NewF->copyAttributesFrom(F);
908 NewF->takeName(F);
909 NewF->IsNewDbgInfoFormat =F->IsNewDbgInfoFormat;
910// Ensure CFI type metadata is propagated to the new function.
911copyMetadataIfPresent(F, NewF,"type");
912copyMetadataIfPresent(F, NewF,"kcfi_type");
913 removeUsers(F);
914F->replaceAllUsesWith(NewF);
915
916// We collect alignment before writeThunkOrAlias that overwrites NewF and
917// G's content.
918constMaybeAlign NewFAlign = NewF->getAlign();
919constMaybeAlign GAlign =G->getAlign();
920
921 writeThunkOrAlias(F,G);
922 writeThunkOrAlias(F, NewF);
923
924if (NewFAlign || GAlign)
925F->setAlignment(std::max(NewFAlign.valueOrOne(), GAlign.valueOrOne()));
926else
927F->setAlignment(std::nullopt);
928F->setLinkage(GlobalValue::PrivateLinkage);
929 ++NumDoubleWeak;
930 ++NumFunctionsMerged;
931 }else {
932// For better debugability, under MergeFunctionsPDI, we do not modify G's
933// call sites to point to F even when within the same translation unit.
934if (!G->isInterposable() && !MergeFunctionsPDI) {
935// Functions referred to by llvm.used/llvm.compiler.used are special:
936// there are uses of the symbol name that are not visible to LLVM,
937// usually from inline asm.
938if (G->hasGlobalUnnamedAddr() && !Used.contains(G)) {
939// G might have been a key in our GlobalNumberState, and it's illegal
940// to replace a key in ValueMap<GlobalValue *> with a non-global.
941 GlobalNumbers.erase(G);
942// If G's address is not significant, replace it entirely.
943 removeUsers(G);
944G->replaceAllUsesWith(F);
945 }else {
946// Redirect direct callers of G to F. (See note on MergeFunctionsPDI
947// above).
948 replaceDirectCallers(G,F);
949 }
950 }
951
952// If G was internal then we may have replaced all uses of G with F. If so,
953// stop here and delete G. There's no need for a thunk. (See note on
954// MergeFunctionsPDI above).
955if (G->isDiscardableIfUnused() &&G->use_empty() && !MergeFunctionsPDI) {
956G->eraseFromParent();
957 ++NumFunctionsMerged;
958return;
959 }
960
961if (writeThunkOrAlias(F,G)) {
962 ++NumFunctionsMerged;
963 }
964 }
965}
966
967/// Replace function F by function G.
968void MergeFunctions::replaceFunctionInTree(const FunctionNode &FN,
969Function *G) {
970Function *F = FN.getFunc();
971assert(FunctionComparator(F,G, &GlobalNumbers).compare() == 0 &&
972"The two functions must be equal");
973
974autoI = FNodesInTree.find(F);
975assert(I != FNodesInTree.end() &&"F should be in FNodesInTree");
976assert(FNodesInTree.count(G) == 0 &&"FNodesInTree should not contain G");
977
978 FnTreeType::iterator IterToFNInFnTree =I->second;
979assert(&(*IterToFNInFnTree) == &FN &&"F should map to FN in FNodesInTree.");
980// Remove F -> FN and insert G -> FN
981 FNodesInTree.erase(I);
982 FNodesInTree.insert({G, IterToFNInFnTree});
983// Replace F with G in FN, which is stored inside the FnTree.
984 FN.replaceBy(G);
985}
986
987// Ordering for functions that are equal under FunctionComparator
988staticboolisFuncOrderCorrect(constFunction *F,constFunction *G) {
989if (F->isInterposable() !=G->isInterposable()) {
990// Strong before weak, because the weak function may call the strong
991// one, but not the other way around.
992return !F->isInterposable();
993 }
994if (F->hasLocalLinkage() !=G->hasLocalLinkage()) {
995// External before local, because we definitely have to keep the external
996// function, but may be able to drop the local one.
997return !F->hasLocalLinkage();
998 }
999// Impose a total order (by name) on the replacement of functions. This is
1000// important when operating on more than one module independently to prevent
1001// cycles of thunks calling each other when the modules are linked together.
1002returnF->getName() <=G->getName();
1003}
1004
1005// Insert a ComparableFunction into the FnTree, or merge it away if equal to one
1006// that was already inserted.
1007bool MergeFunctions::insert(Function *NewFunction) {
1008 std::pair<FnTreeType::iterator, bool>Result =
1009 FnTree.insert(FunctionNode(NewFunction));
1010
1011if (Result.second) {
1012assert(FNodesInTree.count(NewFunction) == 0);
1013 FNodesInTree.insert({NewFunction,Result.first});
1014LLVM_DEBUG(dbgs() <<"Inserting as unique: " << NewFunction->getName()
1015 <<'\n');
1016returnfalse;
1017 }
1018
1019const FunctionNode &OldF = *Result.first;
1020
1021if (!isFuncOrderCorrect(OldF.getFunc(), NewFunction)) {
1022// Swap the two functions.
1023Function *F = OldF.getFunc();
1024 replaceFunctionInTree(*Result.first, NewFunction);
1025 NewFunction =F;
1026assert(OldF.getFunc() !=F &&"Must have swapped the functions.");
1027 }
1028
1029LLVM_DEBUG(dbgs() <<" " << OldF.getFunc()->getName()
1030 <<" == " << NewFunction->getName() <<'\n');
1031
1032Function *DeleteF = NewFunction;
1033 mergeTwoFunctions(OldF.getFunc(), DeleteF);
1034 this->DelToNewMap.insert({DeleteF, OldF.getFunc()});
1035returntrue;
1036}
1037
1038// Remove a function from FnTree. If it was already in FnTree, add
1039// it to Deferred so that we'll look at it in the next round.
1040void MergeFunctions::remove(Function *F) {
1041autoI = FNodesInTree.find(F);
1042if (I != FNodesInTree.end()) {
1043LLVM_DEBUG(dbgs() <<"Deferred " <<F->getName() <<".\n");
1044 FnTree.erase(I->second);
1045// I->second has been invalidated, remove it from the FNodesInTree map to
1046// preserve the invariant.
1047 FNodesInTree.erase(I);
1048 Deferred.emplace_back(F);
1049 }
1050}
1051
1052// For each instruction used by the value, remove() the function that contains
1053// the instruction. This should happen right before a call to RAUW.
1054void MergeFunctions::removeUsers(Value *V) {
1055for (User *U :V->users())
1056if (auto *I = dyn_cast<Instruction>(U))
1057remove(I->getFunction());
1058}
ArrayRef.h
From
BlockVerifier::State From
Definition:BlockVerifier.cpp:57
Casting.h
CommandLine.h
Constants.h
This file contains the declarations for the subclasses of Constant, which represent the different fla...
DebugInfoMetadata.h
DebugLoc.h
Debug.h
LLVM_DEBUG
#define LLVM_DEBUG(...)
Definition:Debug.h:106
DerivedTypes.h
FunctionComparator.h
createCast
static Value * createCast(IRBuilder<> &Builder, Value *V, Type *DestTy)
Definition:GlobalMergeFunctions.cpp:143
GlobalValue.h
IPO.h
IRBuilder.h
Argument.h
BasicBlock.h
Function.h
Instruction.h
IntrinsicInst.h
Module.h
Module.h This file contains the declarations for the Module class.
StructuralHash.h
Type.h
Use.h
This defines the Use class.
User.h
Value.h
InstrTypes.h
Instructions.h
F
#define F(x, y, z)
Definition:MD5.cpp:55
I
#define I(x, y, z)
Definition:MD5.cpp:58
G
#define G(x, y, z)
Definition:MD5.cpp:56
H
#define H(x, y, z)
Definition:MD5.cpp:57
canCreateAliasFor
static bool canCreateAliasFor(Function *F)
Definition:MergeFunctions.cpp:844
isEligibleForMerging
static bool isEligibleForMerging(Function &F)
Check whether F is eligible for function merging.
Definition:MergeFunctions.cpp:429
NumFunctionsForVerificationCheck
static cl::opt< unsigned > NumFunctionsForVerificationCheck("mergefunc-verify", cl::desc("How many functions in a module could be used for " "MergeFunctions to pass a basic correctness check. " "'0' disables this check. Works only with '-debug' key."), cl::init(0), cl::Hidden)
canCreateThunkFor
static bool canCreateThunkFor(Function *F)
Whether this function may be replaced by a forwarding thunk.
Definition:MergeFunctions.cpp:726
MergeFunctionsPDI
static cl::opt< bool > MergeFunctionsPDI("mergefunc-preserve-debug-info", cl::Hidden, cl::init(false), cl::desc("Preserve debug info in thunk when mergefunc " "transformations are made."))
hasDistinctMetadataIntrinsic
static bool hasDistinctMetadataIntrinsic(const Function &F)
Check whether F has an intrinsic which references distinct metadata as an operand.
Definition:MergeFunctions.cpp:409
asPtr
Function * asPtr(Function *Fn)
Definition:MergeFunctions.cpp:434
copyMetadataIfPresent
static void copyMetadataIfPresent(Function *From, Function *To, StringRef Kind)
Copy all metadata of a specific kind from one function to another.
Definition:MergeFunctions.cpp:743
MergeFunctionsAliases
static cl::opt< bool > MergeFunctionsAliases("mergefunc-use-aliases", cl::Hidden, cl::init(false), cl::desc("Allow mergefunc to create aliases"))
isFuncOrderCorrect
static bool isFuncOrderCorrect(const Function *F, const Function *G)
Definition:MergeFunctions.cpp:988
MergeFunctions.h
ModuleUtils.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SmallVector.h
This file defines the SmallVector class.
Statistic.h
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
STATISTIC
#define STATISTIC(VARNAME, DESC)
Definition:Statistic.h:166
ValueHandle.h
RHS
Value * RHS
Definition:X86PartialReduction.cpp:74
LHS
Value * LHS
Definition:X86PartialReduction.cpp:73
FunctionType
Definition:ItaniumDemangle.h:823
PointerType
Definition:ItaniumDemangle.h:627
llvm::AllocaInst
an instruction to allocate memory on the stack
Definition:Instructions.h:63
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition:PassManager.h:253
llvm::Argument
This class represents an incoming formal argument to a Function.
Definition:Argument.h:31
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition:ArrayRef.h:41
llvm::AssertingVH
Value handle that asserts if the Value is deleted.
Definition:ValueHandle.h:264
llvm::BasicBlock
LLVM Basic Block Representation.
Definition:BasicBlock.h:61
llvm::BasicBlock::end
iterator end()
Definition:BasicBlock.h:474
llvm::BasicBlock::begin
iterator begin()
Instruction iterator methods.
Definition:BasicBlock.h:461
llvm::BasicBlock::Create
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition:BasicBlock.h:213
llvm::BasicBlock::eraseFromParent
SymbolTableList< BasicBlock >::iterator eraseFromParent()
Unlink 'this' from the containing function and delete it.
Definition:BasicBlock.cpp:279
llvm::BasicBlock::iterator
InstListType::iterator iterator
Instruction iterators...
Definition:BasicBlock.h:177
llvm::BasicBlock::getTerminator
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition:BasicBlock.h:240
llvm::BasicBlock::back
const Instruction & back() const
Definition:BasicBlock.h:486
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition:InstrTypes.h:1112
llvm::CallBase::setCallingConv
void setCallingConv(CallingConv::ID CC)
Definition:InstrTypes.h:1403
llvm::CallBase::isCallee
bool isCallee(Value::const_user_iterator UI) const
Determine whether the passed iterator points to the callee operand's Use.
Definition:InstrTypes.h:1352
llvm::CallBase::setAttributes
void setAttributes(AttributeList A)
Set the attributes for this call.
Definition:InstrTypes.h:1420
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition:Instructions.h:1479
llvm::CallInst::setTailCallKind
void setTailCallKind(TailCallKind TCK)
Definition:Instructions.h:1598
llvm::CallInst::TCK_MustTail
@ TCK_MustTail
Definition:Instructions.h:1575
llvm::CallInst::TCK_Tail
@ TCK_Tail
Definition:Instructions.h:1574
llvm::DILocalVariable
Local variable.
Definition:DebugInfoMetadata.h:3460
llvm::DILocalVariable::isParameter
bool isParameter() const
Definition:DebugInfoMetadata.h:3522
llvm::DISubprogram
Subprogram description.
Definition:DebugInfoMetadata.h:1710
llvm::DWARFExpression::Operation
This class represents an Operation in the Expression.
Definition:DWARFExpression.h:32
llvm::DbgRecord::eraseFromParent
void eraseFromParent()
Definition:DebugProgramInstruction.cpp:679
llvm::DbgVariableRecord
Record of a variable value-assignment, aka a non instruction representation of the dbg....
Definition:DebugProgramInstruction.h:270
llvm::DbgVariableRecord::isDbgValue
bool isDbgValue() const
Definition:DebugProgramInstruction.h:410
llvm::DbgVariableRecord::print
void print(raw_ostream &O, bool IsForDebug=false) const
Definition:AsmWriter.cpp:5001
llvm::DbgVariableRecord::isDbgAssign
bool isDbgAssign() const
Definition:DebugProgramInstruction.h:506
llvm::DbgVariableRecord::isDbgDeclare
bool isDbgDeclare() const
Definition:DebugProgramInstruction.h:409
llvm::DebugLoc
A debug info location.
Definition:DebugLoc.h:33
llvm::DenseMap
Definition:DenseMap.h:727
llvm::FunctionComparator
FunctionComparator - Compares two functions to determine whether or not they will generate machine co...
Definition:FunctionComparator.h:93
llvm::FunctionComparator::compare
int compare()
Test whether the two functions have equivalent behaviour.
Definition:FunctionComparator.cpp:1007
llvm::Function
Definition:Function.h:63
llvm::Function::Create
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition:Function.h:173
llvm::Function::IsNewDbgInfoFormat
bool IsNewDbgInfoFormat
Is this function using intrinsics to record the position of debugging information,...
Definition:Function.h:116
llvm::Function::copyAttributesFrom
void copyAttributesFrom(const Function *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a Function) from the ...
Definition:Function.cpp:860
llvm::GlobalAlias::create
static GlobalAlias * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Aliasee, Module *Parent)
If a parent module is specified, the alias is automatically inserted into the end of the specified mo...
Definition:Globals.cpp:557
llvm::GlobalNumberState
GlobalNumberState assigns an integer to each global value in the program, which is used by the compar...
Definition:FunctionComparator.h:54
llvm::GlobalNumberState::clear
void clear()
Definition:FunctionComparator.h:84
llvm::GlobalNumberState::erase
void erase(GlobalValue *Global)
Definition:FunctionComparator.h:80
llvm::GlobalObject::getAlign
MaybeAlign getAlign() const
Returns the alignment of the given variable or function.
Definition:GlobalObject.h:79
llvm::GlobalObject::setComdat
void setComdat(Comdat *C)
Definition:Globals.cpp:212
llvm::GlobalObject::addMetadata
void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
Definition:Metadata.cpp:1565
llvm::GlobalValue::UnnamedAddr::Global
@ Global
llvm::GlobalValue::PrivateLinkage
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition:GlobalValue.h:60
llvm::IRBuilderBase::CreateInsertValue
Value * CreateInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition:IRBuilder.h:2562
llvm::IRBuilderBase::CreateExtractValue
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition:IRBuilder.h:2555
llvm::IRBuilderBase::CreateIntToPtr
Value * CreateIntToPtr(Value *V, Type *DestTy, const Twine &Name="")
Definition:IRBuilder.h:2147
llvm::IRBuilderBase::CreateBitCast
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition:IRBuilder.h:2152
llvm::IRBuilderBase::CreatePtrToInt
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
Definition:IRBuilder.h:2142
llvm::IRBuilder
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition:IRBuilder.h:2705
llvm::Instruction
Definition:Instruction.h:68
llvm::Instruction::eraseFromParent
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition:Instruction.cpp:94
llvm::Instruction::getFunction
const Function * getFunction() const
Return the function this instruction belongs to.
Definition:Instruction.cpp:72
llvm::Instruction::setDebugLoc
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition:Instruction.h:508
llvm::MDNode
Metadata node.
Definition:Metadata.h:1073
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition:Metadata.h:1549
llvm::MDNode::getContext
LLVMContext & getContext() const
Definition:Metadata.h:1237
llvm::MergeFunctionsPass::runOnFunctions
static DenseMap< Function *, Function * > runOnFunctions(ArrayRef< Function * > F)
Definition:MergeFunctions.cpp:328
llvm::MergeFunctionsPass::runOnModule
static bool runOnModule(Module &M)
Definition:MergeFunctions.cpp:318
llvm::MergeFunctionsPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition:MergeFunctions.cpp:309
llvm::Metadata::print
void print(raw_ostream &OS, const Module *M=nullptr, bool IsForDebug=false) const
Print.
Definition:AsmWriter.cpp:5260
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition:Module.h:65
llvm::PoisonValue::get
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition:Constants.cpp:1878
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition:Analysis.h:111
llvm::PreservedAnalyses::none
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition:Analysis.h:114
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition:Analysis.h:117
llvm::ReturnInst
Return a value (possibly void), from a function.
Definition:Instructions.h:2938
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition:SmallPtrSet.h:519
llvm::SmallVectorTemplateCommon::end
iterator end()
Definition:SmallVector.h:269
llvm::SmallVectorTemplateCommon::begin
iterator begin()
Definition:SmallVector.h:267
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition:SmallVector.h:1196
llvm::StoreInst
An instruction for storing to memory.
Definition:Instructions.h:292
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition:Type.h:45
llvm::Type::getStructElementType
Type * getStructElementType(unsigned N) const
llvm::Type::isPointerTy
bool isPointerTy() const
True if this is an instance of PointerType.
Definition:Type.h:264
llvm::Type::getStructNumElements
unsigned getStructNumElements() const
llvm::Type::isStructTy
bool isStructTy() const
True if this is an instance of StructType.
Definition:Type.h:258
llvm::Type::isIntegerTy
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition:Type.h:237
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition:Use.h:43
llvm::User
Definition:User.h:44
llvm::Value
LLVM Value Representation.
Definition:Value.h:74
llvm::Value::print
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
Definition:AsmWriter.cpp:5061
llvm::Value::users
iterator_range< user_iterator > users()
Definition:Value.h:421
llvm::Value::uses
iterator_range< use_iterator > uses()
Definition:Value.h:376
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition:Value.cpp:309
llvm::Value::takeName
void takeName(Value *V)
Transfer the name from V to this value.
Definition:Value.cpp:383
llvm::WeakTrackingVH
Value handle that is nullable, but tries to track the Value.
Definition:ValueHandle.h:204
llvm::cl::opt
Definition:CommandLine.h:1423
llvm::iplist_impl::pop_back
void pop_back()
Definition:ilist.h:255
uint64_t
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition:AMDGPUMetadata.h:395
llvm::ARM::ProfileKind::M
@ M
llvm::CallingConv::SwiftTail
@ SwiftTail
This follows the Swift calling convention in how arguments are passed but guarantees tail calls will ...
Definition:CallingConv.h:87
llvm::M68k::MemAddrModeKind::U
@ U
llvm::M68k::MemAddrModeKind::V
@ V
llvm::SIEncodingFamily::SI
@ SI
Definition:SIDefines.h:36
llvm::ScaledNumbers::compare
int compare(DigitsT LDigits, int16_t LScale, DigitsT RDigits, int16_t RScale)
Compare two scaled numbers.
Definition:ScaledNumber.h:252
llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Used
@ Used
llvm::cl::Hidden
@ Hidden
Definition:CommandLine.h:137
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition:CommandLine.h:443
llvm::dxil::PointerTypeAnalysis::run
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
Definition:PointerTypeAnalysis.cpp:191
llvm::ms_demangle::QualifierMangleMode::Result
@ Result
llvm::sys::fs::remove
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::drop_begin
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition:STLExtras.h:329
llvm::stable_sort
void stable_sort(R &&Range)
Definition:STLExtras.h:2037
llvm::make_early_inc_range
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition:STLExtras.h:657
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition:Debug.cpp:163
llvm::filterDbgVars
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.
Definition:DebugProgramInstruction.h:555
llvm::StructuralHash
stable_hash StructuralHash(const Function &F, bool DetailedHash=false)
Returns a hash of the function F.
Definition:StructuralHash.cpp:329
llvm::collectUsedGlobalVariables
GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallVectorImpl< GlobalValue * > &Vec, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
Definition:Module.cpp:865
raw_ostream.h
N
#define N
llvm::MaybeAlign
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition:Alignment.h:117
llvm::MaybeAlign::valueOrOne
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition:Alignment.h:141
llvm::cl::desc
Definition:CommandLine.h:409
llvm::less_first
Function object to check whether the first component of a container supported by std::get (like std::...
Definition:STLExtras.h:1467

Generated on Fri Jul 18 2025 15:39:33 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp