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