LLVM 22.0.0git
GlobalOpt.cpp
Go to the documentation of this file.
1//===- GlobalOpt.cpp - Optimize Global Variables --------------------------===//
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 transforms simple global variables that never have their address
10// taken. If obviously true, it marks read/write globals as constant, deletes
11// variables only stored to, etc.
12//
13//===----------------------------------------------------------------------===//
14
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/Statistic.h"
21#include "llvm/ADT/Twine.h"
30#include "llvm/IR/Attributes.h"
31#include "llvm/IR/BasicBlock.h"
32#include "llvm/IR/CallingConv.h"
33#include "llvm/IR/Constant.h"
34#include "llvm/IR/Constants.h"
35#include "llvm/IR/DataLayout.h"
38#include "llvm/IR/Dominators.h"
39#include "llvm/IR/Function.h"
40#include "llvm/IR/GlobalAlias.h"
41#include "llvm/IR/GlobalValue.h"
43#include "llvm/IR/IRBuilder.h"
44#include "llvm/IR/InstrTypes.h"
45#include "llvm/IR/Instruction.h"
48#include "llvm/IR/Module.h"
49#include "llvm/IR/Operator.h"
50#include "llvm/IR/Type.h"
51#include "llvm/IR/Use.h"
52#include "llvm/IR/User.h"
53#include "llvm/IR/Value.h"
54#include "llvm/IR/ValueHandle.h"
58#include "llvm/Support/Debug.h"
61#include "llvm/Transforms/IPO.h"
66#include <cassert>
67#include <cstdint>
68#include <optional>
69#include <utility>
70#include <vector>
71
72using namespace llvm;
73
74#define DEBUG_TYPE "globalopt"
75
76STATISTIC(NumMarked , "Number of globals marked constant");
77STATISTIC(NumUnnamed , "Number of globals marked unnamed_addr");
78STATISTIC(NumSRA , "Number of aggregate globals broken into scalars");
79STATISTIC(NumSubstitute,"Number of globals with initializers stored into them");
80STATISTIC(NumDeleted , "Number of globals deleted");
81STATISTIC(NumGlobUses , "Number of global uses devirtualized");
82STATISTIC(NumLocalized , "Number of globals localized");
83STATISTIC(NumShrunkToBool , "Number of global vars shrunk to booleans");
84STATISTIC(NumFastCallFns , "Number of functions converted to fastcc");
85STATISTIC(NumCtorsEvaluated, "Number of static ctors evaluated");
86STATISTIC(NumNestRemoved , "Number of nest attributes removed");
87STATISTIC(NumAliasesResolved, "Number of global aliases resolved");
88STATISTIC(NumAliasesRemoved, "Number of global aliases eliminated");
89STATISTIC(NumCXXDtorsRemoved, "Number of global C++ destructors removed");
90STATISTIC(NumAtExitRemoved, "Number of atexit handlers removed");
91STATISTIC(NumInternalFunc, "Number of internal functions");
92STATISTIC(NumColdCC, "Number of functions marked coldcc");
93STATISTIC(NumIFuncsResolved, "Number of statically resolved IFuncs");
94STATISTIC(NumIFuncsDeleted, "Number of IFuncs removed");
95
96static cl::opt<bool>
97 OptimizeNonFMVCallers("optimize-non-fmv-callers",
98 cl::desc("Statically resolve calls to versioned "
99 "functions from non-versioned callers."),
100 cl::init(true), cl::Hidden);
101
102static cl::opt<bool>
103 EnableColdCCStressTest("enable-coldcc-stress-test",
104 cl::desc("Enable stress test of coldcc by adding "
105 "calling conv to all internal functions."),
106 cl::init(false), cl::Hidden);
107
109 "coldcc-rel-freq", cl::Hidden, cl::init(2),
110 cl::desc(
111 "Maximum block frequency, expressed as a percentage of caller's "
112 "entry frequency, for a call site to be considered cold for enabling "
113 "coldcc"));
114
115/// Is this global variable possibly used by a leak checker as a root? If so,
116/// we might not really want to eliminate the stores to it.
118 // A global variable is a root if it is a pointer, or could plausibly contain
119 // a pointer. There are two challenges; one is that we could have a struct
120 // the has an inner member which is a pointer. We recurse through the type to
121 // detect these (up to a point). The other is that we may actually be a union
122 // of a pointer and another type, and so our LLVM type is an integer which
123 // gets converted into a pointer, or our type is an [i8 x #] with a pointer
124 // potentially contained here.
125
126 if (GV->hasPrivateLinkage())
127 return false;
128
130 Types.push_back(GV->getValueType());
131
132 unsigned Limit = 20;
133 do {
134 Type *Ty = Types.pop_back_val();
135 switch (Ty->getTypeID()) {
136 default: break;
138 return true;
141 if (cast<VectorType>(Ty)->getElementType()->isPointerTy())
142 return true;
143 break;
144 case Type::ArrayTyID:
145 Types.push_back(cast<ArrayType>(Ty)->getElementType());
146 break;
147 case Type::StructTyID: {
148 StructType *STy = cast<StructType>(Ty);
149 if (STy->isOpaque()) return true;
150 for (Type *InnerTy : STy->elements()) {
151 if (isa<PointerType>(InnerTy)) return true;
152 if (isa<StructType>(InnerTy) || isa<ArrayType>(InnerTy) ||
153 isa<VectorType>(InnerTy))
154 Types.push_back(InnerTy);
155 }
156 break;
157 }
158 }
159 if (--Limit == 0) return true;
160 } while (!Types.empty());
161 return false;
162}
163
164/// Given a value that is stored to a global but never read, determine whether
165/// it's safe to remove the store and the chain of computation that feeds the
166/// store.
168 Value *V, function_ref<TargetLibraryInfo &(Function &)> GetTLI) {
169 do {
170 if (isa<Constant>(V))
171 return true;
172 if (!V->hasOneUse())
173 return false;
174 if (isa<LoadInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V) ||
175 isa<GlobalValue>(V))
176 return false;
177 if (isAllocationFn(V, GetTLI))
178 return true;
179
180 Instruction *I = cast<Instruction>(V);
181 if (I->mayHaveSideEffects())
182 return false;
183 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
184 if (!GEP->hasAllConstantIndices())
185 return false;
186 } else if (I->getNumOperands() != 1) {
187 return false;
188 }
189
190 V = I->getOperand(0);
191 } while (true);
192}
193
194/// This GV is a pointer root. Loop over all users of the global and clean up
195/// any that obviously don't assign the global a value that isn't dynamically
196/// allocated.
197static bool
200 // A brief explanation of leak checkers. The goal is to find bugs where
201 // pointers are forgotten, causing an accumulating growth in memory
202 // usage over time. The common strategy for leak checkers is to explicitly
203 // allow the memory pointed to by globals at exit. This is popular because it
204 // also solves another problem where the main thread of a C++ program may shut
205 // down before other threads that are still expecting to use those globals. To
206 // handle that case, we expect the program may create a singleton and never
207 // destroy it.
208
209 bool Changed = false;
210
211 // If Dead[n].first is the only use of a malloc result, we can delete its
212 // chain of computation and the store to the global in Dead[n].second.
214
215 SmallVector<User *> Worklist(GV->users());
216 // Constants can't be pointers to dynamically allocated memory.
217 while (!Worklist.empty()) {
218 User *U = Worklist.pop_back_val();
219 if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
220 Value *V = SI->getValueOperand();
221 if (isa<Constant>(V)) {
222 Changed = true;
223 SI->eraseFromParent();
224 } else if (Instruction *I = dyn_cast<Instruction>(V)) {
225 if (I->hasOneUse())
226 Dead.push_back(std::make_pair(I, SI));
227 }
228 } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(U)) {
229 if (isa<Constant>(MSI->getValue())) {
230 Changed = true;
231 MSI->eraseFromParent();
232 } else if (Instruction *I = dyn_cast<Instruction>(MSI->getValue())) {
233 if (I->hasOneUse())
234 Dead.push_back(std::make_pair(I, MSI));
235 }
236 } else if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(U)) {
237 GlobalVariable *MemSrc = dyn_cast<GlobalVariable>(MTI->getSource());
238 if (MemSrc && MemSrc->isConstant()) {
239 Changed = true;
240 MTI->eraseFromParent();
241 } else if (Instruction *I = dyn_cast<Instruction>(MTI->getSource())) {
242 if (I->hasOneUse())
243 Dead.push_back(std::make_pair(I, MTI));
244 }
245 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
246 if (isa<GEPOperator>(CE))
247 append_range(Worklist, CE->users());
248 }
249 }
250
251 for (const auto &[Inst, Store] : Dead) {
252 if (IsSafeComputationToRemove(Inst, GetTLI)) {
253 Store->eraseFromParent();
254 Instruction *I = Inst;
255 do {
256 if (isAllocationFn(I, GetTLI))
257 break;
258 Instruction *J = dyn_cast<Instruction>(I->getOperand(0));
259 if (!J)
260 break;
261 I->eraseFromParent();
262 I = J;
263 } while (true);
264 I->eraseFromParent();
265 Changed = true;
266 }
267 }
268
270 return Changed;
271}
272
273/// We just marked GV constant. Loop over all users of the global, cleaning up
274/// the obvious ones. This is largely just a quick scan over the use list to
275/// clean up the easy and obvious cruft. This returns true if it made a change.
277 const DataLayout &DL) {
279 SmallVector<User *, 8> WorkList(GV->users());
281 bool Changed = false;
282
283 SmallVector<WeakTrackingVH> MaybeDeadInsts;
284 auto EraseFromParent = [&](Instruction *I) {
285 for (Value *Op : I->operands())
286 if (auto *OpI = dyn_cast<Instruction>(Op))
287 MaybeDeadInsts.push_back(OpI);
288 I->eraseFromParent();
289 Changed = true;
290 };
291 while (!WorkList.empty()) {
292 User *U = WorkList.pop_back_val();
293 if (!Visited.insert(U).second)
294 continue;
295
296 if (auto *BO = dyn_cast<BitCastOperator>(U))
297 append_range(WorkList, BO->users());
298 if (auto *ASC = dyn_cast<AddrSpaceCastOperator>(U))
299 append_range(WorkList, ASC->users());
300 else if (auto *GEP = dyn_cast<GEPOperator>(U))
301 append_range(WorkList, GEP->users());
302 else if (auto *LI = dyn_cast<LoadInst>(U)) {
303 // A load from a uniform value is always the same, regardless of any
304 // applied offset.
305 Type *Ty = LI->getType();
307 LI->replaceAllUsesWith(Res);
308 EraseFromParent(LI);
309 continue;
310 }
311
312 Value *PtrOp = LI->getPointerOperand();
313 APInt Offset(DL.getIndexTypeSizeInBits(PtrOp->getType()), 0);
315 DL, Offset, /* AllowNonInbounds */ true);
316 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(PtrOp)) {
317 if (II->getIntrinsicID() == Intrinsic::threadlocal_address)
318 PtrOp = II->getArgOperand(0);
319 }
320 if (PtrOp == GV) {
321 if (auto *Value = ConstantFoldLoadFromConst(Init, Ty, Offset, DL)) {
322 LI->replaceAllUsesWith(Value);
323 EraseFromParent(LI);
324 }
325 }
326 } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
327 // Store must be unreachable or storing Init into the global.
328 EraseFromParent(SI);
329 } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U)) { // memset/cpy/mv
330 if (getUnderlyingObject(MI->getRawDest()) == GV)
331 EraseFromParent(MI);
332 } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(U)) {
333 if (II->getIntrinsicID() == Intrinsic::threadlocal_address)
334 append_range(WorkList, II->users());
335 }
336 }
337
338 Changed |=
341 return Changed;
342}
343
344/// Part of the global at a specific offset, which is only accessed through
345/// loads and stores with the given type.
349 bool IsLoaded = false;
350 bool IsStored = false;
351};
352
353/// Look at all uses of the global and determine which (offset, type) pairs it
354/// can be split into.
356 GlobalVariable *GV, const DataLayout &DL) {
357 SmallVector<Use *, 16> Worklist;
359 auto AppendUses = [&](Value *V) {
360 for (Use &U : V->uses())
361 if (Visited.insert(&U).second)
362 Worklist.push_back(&U);
363 };
364 AppendUses(GV);
365 while (!Worklist.empty()) {
366 Use *U = Worklist.pop_back_val();
367 User *V = U->getUser();
368
369 auto *GEP = dyn_cast<GEPOperator>(V);
370 if (isa<BitCastOperator>(V) || isa<AddrSpaceCastOperator>(V) ||
371 (GEP && GEP->hasAllConstantIndices())) {
372 AppendUses(V);
373 continue;
374 }
375
377 // This is storing the global address into somewhere, not storing into
378 // the global.
379 if (isa<StoreInst>(V) && U->getOperandNo() == 0)
380 return false;
381
382 APInt Offset(DL.getIndexTypeSizeInBits(Ptr->getType()), 0);
383 Ptr = Ptr->stripAndAccumulateConstantOffsets(DL, Offset,
384 /* AllowNonInbounds */ true);
385 if (Ptr != GV || Offset.getActiveBits() >= 64)
386 return false;
387
388 // TODO: We currently require that all accesses at a given offset must
389 // use the same type. This could be relaxed.
390 Type *Ty = getLoadStoreType(V);
391 const auto &[It, Inserted] =
392 Parts.try_emplace(Offset.getZExtValue(), GlobalPart{Ty});
393 if (Ty != It->second.Ty)
394 return false;
395
396 if (Inserted) {
397 It->second.Initializer =
399 if (!It->second.Initializer) {
400 LLVM_DEBUG(dbgs() << "Global SRA: Failed to evaluate initializer of "
401 << *GV << " with type " << *Ty << " at offset "
402 << Offset.getZExtValue());
403 return false;
404 }
405 }
406
407 // Scalable types not currently supported.
408 if (Ty->isScalableTy())
409 return false;
410
411 auto IsStored = [](Value *V, Constant *Initializer) {
412 auto *SI = dyn_cast<StoreInst>(V);
413 if (!SI)
414 return false;
415
416 Constant *StoredConst = dyn_cast<Constant>(SI->getOperand(0));
417 if (!StoredConst)
418 return true;
419
420 // Don't consider stores that only write the initializer value.
421 return Initializer != StoredConst;
422 };
423
424 It->second.IsLoaded |= isa<LoadInst>(V);
425 It->second.IsStored |= IsStored(V, It->second.Initializer);
426 continue;
427 }
428
429 // Ignore dead constant users.
430 if (auto *C = dyn_cast<Constant>(V)) {
432 return false;
433 continue;
434 }
435
436 // Unknown user.
437 return false;
438 }
439
440 return true;
441}
442
443/// Copy over the debug info for a variable to its SRA replacements.
445 uint64_t FragmentOffsetInBits,
446 uint64_t FragmentSizeInBits,
447 uint64_t VarSize) {
449 GV->getDebugInfo(GVs);
450 for (auto *GVE : GVs) {
451 DIVariable *Var = GVE->getVariable();
452 DIExpression *Expr = GVE->getExpression();
453 int64_t CurVarOffsetInBytes = 0;
454 uint64_t CurVarOffsetInBits = 0;
455 uint64_t FragmentEndInBits = FragmentOffsetInBits + FragmentSizeInBits;
456
457 // Calculate the offset (Bytes), Continue if unknown.
458 if (!Expr->extractIfOffset(CurVarOffsetInBytes))
459 continue;
460
461 // Ignore negative offset.
462 if (CurVarOffsetInBytes < 0)
463 continue;
464
465 // Convert offset to bits.
466 CurVarOffsetInBits = CHAR_BIT * (uint64_t)CurVarOffsetInBytes;
467
468 // Current var starts after the fragment, ignore.
469 if (CurVarOffsetInBits >= FragmentEndInBits)
470 continue;
471
472 uint64_t CurVarSize = Var->getType()->getSizeInBits();
473 uint64_t CurVarEndInBits = CurVarOffsetInBits + CurVarSize;
474 // Current variable ends before start of fragment, ignore.
475 if (CurVarSize != 0 && /* CurVarSize is known */
476 CurVarEndInBits <= FragmentOffsetInBits)
477 continue;
478
479 // Current variable fits in (not greater than) the fragment,
480 // does not need fragment expression.
481 if (CurVarSize != 0 && /* CurVarSize is known */
482 CurVarOffsetInBits >= FragmentOffsetInBits &&
483 CurVarEndInBits <= FragmentEndInBits) {
484 uint64_t CurVarOffsetInFragment =
485 (CurVarOffsetInBits - FragmentOffsetInBits) / 8;
486 if (CurVarOffsetInFragment != 0)
487 Expr = DIExpression::get(Expr->getContext(), {dwarf::DW_OP_plus_uconst,
488 CurVarOffsetInFragment});
489 else
490 Expr = DIExpression::get(Expr->getContext(), {});
491 auto *NGVE =
492 DIGlobalVariableExpression::get(GVE->getContext(), Var, Expr);
493 NGV->addDebugInfo(NGVE);
494 continue;
495 }
496 // Current variable does not fit in single fragment,
497 // emit a fragment expression.
498 if (FragmentSizeInBits < VarSize) {
499 if (CurVarOffsetInBits > FragmentOffsetInBits)
500 continue;
501 uint64_t CurVarFragmentOffsetInBits =
502 FragmentOffsetInBits - CurVarOffsetInBits;
503 uint64_t CurVarFragmentSizeInBits = FragmentSizeInBits;
504 if (CurVarSize != 0 && CurVarEndInBits < FragmentEndInBits)
505 CurVarFragmentSizeInBits -= (FragmentEndInBits - CurVarEndInBits);
506 if (CurVarOffsetInBits)
507 Expr = DIExpression::get(Expr->getContext(), {});
509 Expr, CurVarFragmentOffsetInBits, CurVarFragmentSizeInBits))
510 Expr = *E;
511 else
512 continue;
513 }
514 auto *NGVE = DIGlobalVariableExpression::get(GVE->getContext(), Var, Expr);
515 NGV->addDebugInfo(NGVE);
516 }
517}
518
519/// Perform scalar replacement of aggregates on the specified global variable.
520/// This opens the door for other optimizations by exposing the behavior of the
521/// program in a more fine-grained way. We have determined that this
522/// transformation is safe already. We return the first global variable we
523/// insert so that the caller can reprocess it.
525 assert(GV->hasLocalLinkage());
526
527 // Collect types to split into.
529 if (!collectSRATypes(Parts, GV, DL) || Parts.empty())
530 return nullptr;
531
532 // Make sure we don't SRA back to the same type.
533 if (Parts.size() == 1 && Parts.begin()->second.Ty == GV->getValueType())
534 return nullptr;
535
536 // Don't perform SRA if we would have to split into many globals. Ignore
537 // parts that are either only loaded or only stored, because we expect them
538 // to be optimized away.
539 unsigned NumParts = count_if(Parts, [](const auto &Pair) {
540 return Pair.second.IsLoaded && Pair.second.IsStored;
541 });
542 if (NumParts > 16)
543 return nullptr;
544
545 // Sort by offset.
547 for (const auto &Pair : Parts) {
548 TypesVector.push_back(
549 {Pair.first, Pair.second.Ty, Pair.second.Initializer});
550 }
551 sort(TypesVector, llvm::less_first());
552
553 // Check that the types are non-overlapping.
554 uint64_t Offset = 0;
555 for (const auto &[OffsetForTy, Ty, _] : TypesVector) {
556 // Overlaps with previous type.
557 if (OffsetForTy < Offset)
558 return nullptr;
559
560 Offset = OffsetForTy + DL.getTypeAllocSize(Ty);
561 }
562
563 // Some accesses go beyond the end of the global, don't bother.
564 if (Offset > DL.getTypeAllocSize(GV->getValueType()))
565 return nullptr;
566
567 LLVM_DEBUG(dbgs() << "PERFORMING GLOBAL SRA ON: " << *GV << "\n");
568
569 // Get the alignment of the global, either explicit or target-specific.
570 Align StartAlignment =
571 DL.getValueOrABITypeAlignment(GV->getAlign(), GV->getValueType());
572 uint64_t VarSize = DL.getTypeSizeInBits(GV->getValueType());
573
574 // Create replacement globals.
576 unsigned NameSuffix = 0;
577 for (auto &[OffsetForTy, Ty, Initializer] : TypesVector) {
579 *GV->getParent(), Ty, false, GlobalVariable::InternalLinkage,
580 Initializer, GV->getName() + "." + Twine(NameSuffix++), GV,
582 // Start out by copying attributes from the original, including alignment.
583 NGV->copyAttributesFrom(GV);
584 NewGlobals.insert({OffsetForTy, NGV});
585
586 // Calculate the known alignment of the field. If the original aggregate
587 // had 256 byte alignment for example, then the element at a given offset
588 // may also have a known alignment, and something might depend on that:
589 // propagate info to each field.
590 Align NewAlign = commonAlignment(StartAlignment, OffsetForTy);
591 NGV->setAlignment(NewAlign);
592
593 // Copy over the debug info for the variable.
594 transferSRADebugInfo(GV, NGV, OffsetForTy * 8,
595 DL.getTypeAllocSizeInBits(Ty), VarSize);
596 }
597
598 // Replace uses of the original global with uses of the new global.
602 auto AppendUsers = [&](Value *V) {
603 for (User *U : V->users())
604 if (Visited.insert(U).second)
605 Worklist.push_back(U);
606 };
607 AppendUsers(GV);
608 while (!Worklist.empty()) {
609 Value *V = Worklist.pop_back_val();
610 if (isa<BitCastOperator>(V) || isa<AddrSpaceCastOperator>(V) ||
611 isa<GEPOperator>(V)) {
612 AppendUsers(V);
613 if (isa<Instruction>(V))
614 DeadInsts.push_back(V);
615 continue;
616 }
617
619 APInt Offset(DL.getIndexTypeSizeInBits(Ptr->getType()), 0);
620 Ptr = Ptr->stripAndAccumulateConstantOffsets(DL, Offset,
621 /* AllowNonInbounds */ true);
622 assert(Ptr == GV && "Load/store must be from/to global");
623 GlobalVariable *NGV = NewGlobals[Offset.getZExtValue()];
624 assert(NGV && "Must have replacement global for this offset");
625
626 // Update the pointer operand and recalculate alignment.
627 Align PrefAlign = DL.getPrefTypeAlign(getLoadStoreType(V));
628 Align NewAlign =
629 getOrEnforceKnownAlignment(NGV, PrefAlign, DL, cast<Instruction>(V));
630
631 if (auto *LI = dyn_cast<LoadInst>(V)) {
632 LI->setOperand(0, NGV);
633 LI->setAlignment(NewAlign);
634 } else {
635 auto *SI = cast<StoreInst>(V);
636 SI->setOperand(1, NGV);
637 SI->setAlignment(NewAlign);
638 }
639 continue;
640 }
641
642 assert(isa<Constant>(V) && isSafeToDestroyConstant(cast<Constant>(V)) &&
643 "Other users can only be dead constants");
644 }
645
646 // Delete old instructions and global.
649 GV->eraseFromParent();
650 ++NumSRA;
651
652 assert(NewGlobals.size() > 0);
653 return NewGlobals.begin()->second;
654}
655
656/// Return true if all users of the specified value will trap if the value is
657/// dynamically null. PHIs keeps track of any phi nodes we've seen to avoid
658/// reprocessing them.
661 for (const User *U : V->users()) {
662 if (const Instruction *I = dyn_cast<Instruction>(U)) {
663 // If null pointer is considered valid, then all uses are non-trapping.
664 // Non address-space 0 globals have already been pruned by the caller.
665 if (NullPointerIsDefined(I->getFunction()))
666 return false;
667 }
668 if (isa<LoadInst>(U)) {
669 // Will trap.
670 } else if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
671 if (SI->getOperand(0) == V) {
672 return false; // Storing the value.
673 }
674 } else if (const CallInst *CI = dyn_cast<CallInst>(U)) {
675 if (CI->getCalledOperand() != V) {
676 return false; // Not calling the ptr
677 }
678 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(U)) {
679 if (II->getCalledOperand() != V) {
680 return false; // Not calling the ptr
681 }
682 } else if (const AddrSpaceCastInst *CI = dyn_cast<AddrSpaceCastInst>(U)) {
683 if (!AllUsesOfValueWillTrapIfNull(CI, PHIs))
684 return false;
685 } else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
686 if (!AllUsesOfValueWillTrapIfNull(GEPI, PHIs)) return false;
687 } else if (const PHINode *PN = dyn_cast<PHINode>(U)) {
688 // If we've already seen this phi node, ignore it, it has already been
689 // checked.
690 if (PHIs.insert(PN).second && !AllUsesOfValueWillTrapIfNull(PN, PHIs))
691 return false;
692 } else if (isa<ICmpInst>(U) &&
693 !ICmpInst::isSigned(cast<ICmpInst>(U)->getPredicate()) &&
694 isa<LoadInst>(U->getOperand(0)) &&
695 isa<ConstantPointerNull>(U->getOperand(1))) {
696 assert(isa<GlobalValue>(cast<LoadInst>(U->getOperand(0))
697 ->getPointerOperand()
698 ->stripPointerCasts()) &&
699 "Should be GlobalVariable");
700 // This and only this kind of non-signed ICmpInst is to be replaced with
701 // the comparing of the value of the created global init bool later in
702 // optimizeGlobalAddressOfAllocation for the global variable.
703 } else {
704 return false;
705 }
706 }
707 return true;
708}
709
710/// Return true if all uses of any loads from GV will trap if the loaded value
711/// is null. Note that this also permits comparisons of the loaded value
712/// against null, as a special case.
715 Worklist.push_back(GV);
716 while (!Worklist.empty()) {
717 const Value *P = Worklist.pop_back_val();
718 for (const auto *U : P->users()) {
719 if (auto *LI = dyn_cast<LoadInst>(U)) {
720 if (!LI->isSimple())
721 return false;
723 if (!AllUsesOfValueWillTrapIfNull(LI, PHIs))
724 return false;
725 } else if (auto *SI = dyn_cast<StoreInst>(U)) {
726 if (!SI->isSimple())
727 return false;
728 // Ignore stores to the global.
729 if (SI->getPointerOperand() != P)
730 return false;
731 } else if (auto *CE = dyn_cast<ConstantExpr>(U)) {
732 if (CE->stripPointerCasts() != GV)
733 return false;
734 // Check further the ConstantExpr.
735 Worklist.push_back(CE);
736 } else {
737 // We don't know or understand this user, bail out.
738 return false;
739 }
740 }
741 }
742
743 return true;
744}
745
746/// Get all the loads/store uses for global variable \p GV.
750 Worklist.push_back(GV);
751 while (!Worklist.empty()) {
752 auto *P = Worklist.pop_back_val();
753 for (auto *U : P->users()) {
754 if (auto *CE = dyn_cast<ConstantExpr>(U)) {
755 Worklist.push_back(CE);
756 continue;
757 }
758
759 assert((isa<LoadInst>(U) || isa<StoreInst>(U)) &&
760 "Expect only load or store instructions");
761 Uses.push_back(U);
762 }
763 }
764}
765
767 bool Changed = false;
768 for (auto UI = V->user_begin(), E = V->user_end(); UI != E; ) {
769 Instruction *I = cast<Instruction>(*UI++);
770 // Uses are non-trapping if null pointer is considered valid.
771 // Non address-space 0 globals are already pruned by the caller.
772 if (NullPointerIsDefined(I->getFunction()))
773 return false;
774 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
775 LI->setOperand(0, NewV);
776 Changed = true;
777 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
778 if (SI->getOperand(1) == V) {
779 SI->setOperand(1, NewV);
780 Changed = true;
781 }
782 } else if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
783 CallBase *CB = cast<CallBase>(I);
784 if (CB->getCalledOperand() == V) {
785 // Calling through the pointer! Turn into a direct call, but be careful
786 // that the pointer is not also being passed as an argument.
787 CB->setCalledOperand(NewV);
788 Changed = true;
789 bool PassedAsArg = false;
790 for (unsigned i = 0, e = CB->arg_size(); i != e; ++i)
791 if (CB->getArgOperand(i) == V) {
792 PassedAsArg = true;
793 CB->setArgOperand(i, NewV);
794 }
795
796 if (PassedAsArg) {
797 // Being passed as an argument also. Be careful to not invalidate UI!
798 UI = V->user_begin();
799 }
800 }
801 } else if (AddrSpaceCastInst *CI = dyn_cast<AddrSpaceCastInst>(I)) {
803 CI, ConstantExpr::getAddrSpaceCast(NewV, CI->getType()));
804 if (CI->use_empty()) {
805 Changed = true;
806 CI->eraseFromParent();
807 }
808 } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
809 // Should handle GEP here.
811 Idxs.reserve(GEPI->getNumOperands()-1);
812 for (User::op_iterator i = GEPI->op_begin() + 1, e = GEPI->op_end();
813 i != e; ++i)
814 if (Constant *C = dyn_cast<Constant>(*i))
815 Idxs.push_back(C);
816 else
817 break;
818 if (Idxs.size() == GEPI->getNumOperands()-1)
820 GEPI, ConstantExpr::getGetElementPtr(GEPI->getSourceElementType(),
821 NewV, Idxs));
822 if (GEPI->use_empty()) {
823 Changed = true;
824 GEPI->eraseFromParent();
825 }
826 }
827 }
828
829 return Changed;
830}
831
832/// The specified global has only one non-null value stored into it. If there
833/// are uses of the loaded value that would trap if the loaded value is
834/// dynamically null, then we know that they cannot be reachable with a null
835/// optimize away the load.
837 GlobalVariable *GV, Constant *LV, const DataLayout &DL,
839 bool Changed = false;
840
841 // Keep track of whether we are able to remove all the uses of the global
842 // other than the store that defines it.
843 bool AllNonStoreUsesGone = true;
844
845 // Replace all uses of loads with uses of uses of the stored value.
846 for (User *GlobalUser : llvm::make_early_inc_range(GV->users())) {
847 if (LoadInst *LI = dyn_cast<LoadInst>(GlobalUser)) {
848 Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV);
849 // If we were able to delete all uses of the loads
850 if (LI->use_empty()) {
851 LI->eraseFromParent();
852 Changed = true;
853 } else {
854 AllNonStoreUsesGone = false;
855 }
856 } else if (isa<StoreInst>(GlobalUser)) {
857 // Ignore the store that stores "LV" to the global.
858 assert(GlobalUser->getOperand(1) == GV &&
859 "Must be storing *to* the global");
860 } else {
861 AllNonStoreUsesGone = false;
862
863 // If we get here we could have other crazy uses that are transitively
864 // loaded.
865 assert((isa<PHINode>(GlobalUser) || isa<SelectInst>(GlobalUser) ||
866 isa<ConstantExpr>(GlobalUser) || isa<CmpInst>(GlobalUser) ||
867 isa<BitCastInst>(GlobalUser) ||
868 isa<GetElementPtrInst>(GlobalUser) ||
869 isa<AddrSpaceCastInst>(GlobalUser)) &&
870 "Only expect load and stores!");
871 }
872 }
873
874 if (Changed) {
875 LLVM_DEBUG(dbgs() << "OPTIMIZED LOADS FROM STORED ONCE POINTER: " << *GV
876 << "\n");
877 ++NumGlobUses;
878 }
879
880 // If we nuked all of the loads, then none of the stores are needed either,
881 // nor is the global.
882 if (AllNonStoreUsesGone) {
883 if (isLeakCheckerRoot(GV)) {
884 Changed |= CleanupPointerRootUsers(GV, GetTLI);
885 } else {
886 Changed = true;
888 }
889 if (GV->use_empty()) {
890 LLVM_DEBUG(dbgs() << " *** GLOBAL NOW DEAD!\n");
891 Changed = true;
892 GV->eraseFromParent();
893 ++NumDeleted;
894 }
895 }
896 return Changed;
897}
898
899/// Walk the use list of V, constant folding all of the instructions that are
900/// foldable.
901static void ConstantPropUsersOf(Value *V, const DataLayout &DL,
902 TargetLibraryInfo *TLI) {
903 for (Value::user_iterator UI = V->user_begin(), E = V->user_end(); UI != E; )
904 if (Instruction *I = dyn_cast<Instruction>(*UI++))
905 if (Constant *NewC = ConstantFoldInstruction(I, DL, TLI)) {
906 I->replaceAllUsesWith(NewC);
907
908 // Advance UI to the next non-I use to avoid invalidating it!
909 // Instructions could multiply use V.
910 while (UI != E && *UI == I)
911 ++UI;
913 I->eraseFromParent();
914 }
915}
916
917/// This function takes the specified global variable, and transforms the
918/// program as if it always contained the result of the specified malloc.
919/// Because it is always the result of the specified malloc, there is no reason
920/// to actually DO the malloc. Instead, turn the malloc into a global, and any
921/// loads of GV as uses of the new global.
922static GlobalVariable *
924 uint64_t AllocSize, Constant *InitVal,
925 const DataLayout &DL,
926 TargetLibraryInfo *TLI) {
927 LLVM_DEBUG(errs() << "PROMOTING GLOBAL: " << *GV << " CALL = " << *CI
928 << '\n');
929
930 // Create global of type [AllocSize x i8].
931 Type *GlobalType = ArrayType::get(Type::getInt8Ty(GV->getContext()),
932 AllocSize);
933
934 // Create the new global variable. The contents of the allocated memory is
935 // undefined initially, so initialize with an undef value.
936 GlobalVariable *NewGV = new GlobalVariable(
937 *GV->getParent(), GlobalType, false, GlobalValue::InternalLinkage,
938 UndefValue::get(GlobalType), GV->getName() + ".body", nullptr,
939 GV->getThreadLocalMode());
940
941 // Initialize the global at the point of the original call. Note that this
942 // is a different point from the initialization referred to below for the
943 // nullability handling. Sublety: We have not proven the original global was
944 // only initialized once. As such, we can not fold this into the initializer
945 // of the new global as may need to re-init the storage multiple times.
946 if (!isa<UndefValue>(InitVal)) {
947 IRBuilder<> Builder(CI->getNextNode());
948 // TODO: Use alignment above if align!=1
949 Builder.CreateMemSet(NewGV, InitVal, AllocSize, std::nullopt);
950 }
951
952 // Update users of the allocation to use the new global instead.
953 CI->replaceAllUsesWith(NewGV);
954
955 // If there is a comparison against null, we will insert a global bool to
956 // keep track of whether the global was initialized yet or not.
957 GlobalVariable *InitBool = new GlobalVariable(
959 ConstantInt::getFalse(GV->getContext()), GV->getName() + ".init",
961 bool InitBoolUsed = false;
962
963 // Loop over all instruction uses of GV, processing them in turn.
965 allUsesOfLoadAndStores(GV, Guses);
966 for (auto *U : Guses) {
967 if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
968 // The global is initialized when the store to it occurs. If the stored
969 // value is null value, the global bool is set to false, otherwise true.
970 auto *NewSI = new StoreInst(
971 ConstantInt::getBool(GV->getContext(), !isa<ConstantPointerNull>(
972 SI->getValueOperand())),
973 InitBool, false, Align(1), SI->getOrdering(), SI->getSyncScopeID(),
974 SI->getIterator());
975 NewSI->setDebugLoc(SI->getDebugLoc());
976 SI->eraseFromParent();
977 continue;
978 }
979
980 LoadInst *LI = cast<LoadInst>(U);
981 while (!LI->use_empty()) {
982 Use &LoadUse = *LI->use_begin();
983 ICmpInst *ICI = dyn_cast<ICmpInst>(LoadUse.getUser());
984 if (!ICI) {
985 LoadUse.set(NewGV);
986 continue;
987 }
988
989 // Replace the cmp X, 0 with a use of the bool value.
990 Value *LV = new LoadInst(InitBool->getValueType(), InitBool,
991 InitBool->getName() + ".val", false, Align(1),
992 LI->getOrdering(), LI->getSyncScopeID(),
993 LI->getIterator());
994 // FIXME: Should we use the DebugLoc of the load used by the predicate, or
995 // the predicate? The load seems most appropriate, but there's an argument
996 // that the new load does not represent the old load, but is simply a
997 // component of recomputing the predicate.
998 cast<LoadInst>(LV)->setDebugLoc(LI->getDebugLoc());
999 InitBoolUsed = true;
1000 switch (ICI->getPredicate()) {
1001 default: llvm_unreachable("Unknown ICmp Predicate!");
1002 case ICmpInst::ICMP_ULT: // X < null -> always false
1004 break;
1005 case ICmpInst::ICMP_UGE: // X >= null -> always true
1006 LV = ConstantInt::getTrue(GV->getContext());
1007 break;
1008 case ICmpInst::ICMP_ULE:
1009 case ICmpInst::ICMP_EQ:
1010 LV = BinaryOperator::CreateNot(LV, "notinit", ICI->getIterator());
1011 cast<BinaryOperator>(LV)->setDebugLoc(ICI->getDebugLoc());
1012 break;
1013 case ICmpInst::ICMP_NE:
1014 case ICmpInst::ICMP_UGT:
1015 break; // no change.
1016 }
1017 ICI->replaceAllUsesWith(LV);
1018 ICI->eraseFromParent();
1019 }
1020 LI->eraseFromParent();
1021 }
1022
1023 // If the initialization boolean was used, insert it, otherwise delete it.
1024 if (!InitBoolUsed) {
1025 while (!InitBool->use_empty()) // Delete initializations
1026 cast<StoreInst>(InitBool->user_back())->eraseFromParent();
1027 delete InitBool;
1028 } else
1029 GV->getParent()->insertGlobalVariable(GV->getIterator(), InitBool);
1030
1031 // Now the GV is dead, nuke it and the allocation..
1032 GV->eraseFromParent();
1033 CI->eraseFromParent();
1034
1035 // To further other optimizations, loop over all users of NewGV and try to
1036 // constant prop them. This will promote GEP instructions with constant
1037 // indices into GEP constant-exprs, which will allow global-opt to hack on it.
1038 ConstantPropUsersOf(NewGV, DL, TLI);
1039
1040 return NewGV;
1041}
1042
1043/// Scan the use-list of GV checking to make sure that there are no complex uses
1044/// of GV. We permit simple things like dereferencing the pointer, but not
1045/// storing through the address, unless it is to the specified global.
1046static bool
1048 const GlobalVariable *GV) {
1051 Worklist.push_back(CI);
1052
1053 while (!Worklist.empty()) {
1054 const Value *V = Worklist.pop_back_val();
1055 if (!Visited.insert(V).second)
1056 continue;
1057
1058 for (const Use &VUse : V->uses()) {
1059 const User *U = VUse.getUser();
1060 if (isa<LoadInst>(U) || isa<CmpInst>(U))
1061 continue; // Fine, ignore.
1062
1063 if (auto *SI = dyn_cast<StoreInst>(U)) {
1064 if (SI->getValueOperand() == V &&
1065 SI->getPointerOperand()->stripPointerCasts() != GV)
1066 return false; // Storing the pointer not into GV... bad.
1067 continue; // Otherwise, storing through it, or storing into GV... fine.
1068 }
1069
1070 if (auto *GEPI = dyn_cast<GetElementPtrInst>(U)) {
1071 Worklist.push_back(GEPI);
1072 continue;
1073 }
1074
1075 return false;
1076 }
1077 }
1078
1079 return true;
1080}
1081
1082/// If we have a global that is only initialized with a fixed size allocation
1083/// try to transform the program to use global memory instead of heap
1084/// allocated memory. This eliminates dynamic allocation, avoids an indirection
1085/// accessing the data, and exposes the resultant global to further GlobalOpt.
1087 CallInst *CI,
1088 const DataLayout &DL,
1089 TargetLibraryInfo *TLI) {
1090 if (!isRemovableAlloc(CI, TLI))
1091 // Must be able to remove the call when we get done..
1092 return false;
1093
1094 Type *Int8Ty = Type::getInt8Ty(CI->getFunction()->getContext());
1095 Constant *InitVal = getInitialValueOfAllocation(CI, TLI, Int8Ty);
1096 if (!InitVal)
1097 // Must be able to emit a memset for initialization
1098 return false;
1099
1100 uint64_t AllocSize;
1101 if (!getObjectSize(CI, AllocSize, DL, TLI, ObjectSizeOpts()))
1102 return false;
1103
1104 // Restrict this transformation to only working on small allocations
1105 // (2048 bytes currently), as we don't want to introduce a 16M global or
1106 // something.
1107 if (AllocSize >= 2048)
1108 return false;
1109
1110 // We can't optimize this global unless all uses of it are *known* to be
1111 // of the malloc value, not of the null initializer value (consider a use
1112 // that compares the global's value against zero to see if the malloc has
1113 // been reached). To do this, we check to see if all uses of the global
1114 // would trap if the global were null: this proves that they must all
1115 // happen after the malloc.
1117 return false;
1118
1119 // We can't optimize this if the malloc itself is used in a complex way,
1120 // for example, being stored into multiple globals. This allows the
1121 // malloc to be stored into the specified global, loaded, gep, icmp'd.
1122 // These are all things we could transform to using the global for.
1124 return false;
1125
1126 OptimizeGlobalAddressOfAllocation(GV, CI, AllocSize, InitVal, DL, TLI);
1127 return true;
1128}
1129
1130// Try to optimize globals based on the knowledge that only one value (besides
1131// its initializer) is ever stored to the global.
1132static bool
1134 const DataLayout &DL,
1136 // If we are dealing with a pointer global that is initialized to null and
1137 // only has one (non-null) value stored into it, then we can optimize any
1138 // users of the loaded value (often calls and loads) that would trap if the
1139 // value was null.
1140 if (GV->getInitializer()->getType()->isPointerTy() &&
1141 GV->getInitializer()->isNullValue() &&
1142 StoredOnceVal->getType()->isPointerTy() &&
1144 nullptr /* F */,
1146 if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal)) {
1147 // Optimize away any trapping uses of the loaded value.
1148 if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC, DL, GetTLI))
1149 return true;
1150 } else if (isAllocationFn(StoredOnceVal, GetTLI)) {
1151 if (auto *CI = dyn_cast<CallInst>(StoredOnceVal)) {
1152 auto *TLI = &GetTLI(*CI->getFunction());
1154 return true;
1155 }
1156 }
1157 }
1158
1159 return false;
1160}
1161
1162/// At this point, we have learned that the only two values ever stored into GV
1163/// are its initializer and OtherVal. See if we can shrink the global into a
1164/// boolean and select between the two values whenever it is used. This exposes
1165/// the values to other scalar optimizations.
1167 Type *GVElType = GV->getValueType();
1168
1169 // If GVElType is already i1, it is already shrunk. If the type of the GV is
1170 // an FP value, pointer or vector, don't do this optimization because a select
1171 // between them is very expensive and unlikely to lead to later
1172 // simplification. In these cases, we typically end up with "cond ? v1 : v2"
1173 // where v1 and v2 both require constant pool loads, a big loss.
1174 if (GVElType == Type::getInt1Ty(GV->getContext()) ||
1175 GVElType->isFloatingPointTy() ||
1176 GVElType->isPointerTy() || GVElType->isVectorTy())
1177 return false;
1178
1179 // Walk the use list of the global seeing if all the uses are load or store.
1180 // If there is anything else, bail out.
1181 for (User *U : GV->users()) {
1182 if (!isa<LoadInst>(U) && !isa<StoreInst>(U))
1183 return false;
1184 if (getLoadStoreType(U) != GVElType)
1185 return false;
1186 }
1187
1188 LLVM_DEBUG(dbgs() << " *** SHRINKING TO BOOL: " << *GV << "\n");
1189
1190 // Create the new global, initializing it to false.
1192 false,
1195 GV->getName()+".b",
1196 GV->getThreadLocalMode(),
1197 GV->getType()->getAddressSpace());
1198 NewGV->copyAttributesFrom(GV);
1199 GV->getParent()->insertGlobalVariable(GV->getIterator(), NewGV);
1200
1201 Constant *InitVal = GV->getInitializer();
1202 assert(InitVal->getType() != Type::getInt1Ty(GV->getContext()) &&
1203 "No reason to shrink to bool!");
1204
1206 GV->getDebugInfo(GVs);
1207
1208 // If initialized to zero and storing one into the global, we can use a cast
1209 // instead of a select to synthesize the desired value.
1210 bool IsOneZero = false;
1211 bool EmitOneOrZero = true;
1212 auto *CI = dyn_cast<ConstantInt>(OtherVal);
1213 if (CI && CI->getValue().getActiveBits() <= 64) {
1214 IsOneZero = InitVal->isNullValue() && CI->isOne();
1215
1216 auto *CIInit = dyn_cast<ConstantInt>(GV->getInitializer());
1217 if (CIInit && CIInit->getValue().getActiveBits() <= 64) {
1218 uint64_t ValInit = CIInit->getZExtValue();
1219 uint64_t ValOther = CI->getZExtValue();
1220 uint64_t ValMinus = ValOther - ValInit;
1221
1222 for(auto *GVe : GVs){
1223 DIGlobalVariable *DGV = GVe->getVariable();
1224 DIExpression *E = GVe->getExpression();
1225 const DataLayout &DL = GV->getDataLayout();
1226 unsigned SizeInOctets =
1227 DL.getTypeAllocSizeInBits(NewGV->getValueType()) / 8;
1228
1229 // It is expected that the address of global optimized variable is on
1230 // top of the stack. After optimization, value of that variable will
1231 // be ether 0 for initial value or 1 for other value. The following
1232 // expression should return constant integer value depending on the
1233 // value at global object address:
1234 // val * (ValOther - ValInit) + ValInit:
1235 // DW_OP_deref DW_OP_constu <ValMinus>
1236 // DW_OP_mul DW_OP_constu <ValInit> DW_OP_plus DW_OP_stack_value
1238 dwarf::DW_OP_deref_size, SizeInOctets,
1239 dwarf::DW_OP_constu, ValMinus,
1240 dwarf::DW_OP_mul, dwarf::DW_OP_constu, ValInit,
1241 dwarf::DW_OP_plus};
1242 bool WithStackValue = true;
1243 E = DIExpression::prependOpcodes(E, Ops, WithStackValue);
1245 DIGlobalVariableExpression::get(NewGV->getContext(), DGV, E);
1246 NewGV->addDebugInfo(DGVE);
1247 }
1248 EmitOneOrZero = false;
1249 }
1250 }
1251
1252 if (EmitOneOrZero) {
1253 // FIXME: This will only emit address for debugger on which will
1254 // be written only 0 or 1.
1255 for(auto *GV : GVs)
1256 NewGV->addDebugInfo(GV);
1257 }
1258
1259 while (!GV->use_empty()) {
1260 Instruction *UI = cast<Instruction>(GV->user_back());
1261 if (StoreInst *SI = dyn_cast<StoreInst>(UI)) {
1262 // Change the store into a boolean store.
1263 bool StoringOther = SI->getOperand(0) == OtherVal;
1264 // Only do this if we weren't storing a loaded value.
1265 Value *StoreVal;
1266 if (StoringOther || SI->getOperand(0) == InitVal) {
1267 StoreVal = ConstantInt::get(Type::getInt1Ty(GV->getContext()),
1268 StoringOther);
1269 } else {
1270 // Otherwise, we are storing a previously loaded copy. To do this,
1271 // change the copy from copying the original value to just copying the
1272 // bool.
1273 Instruction *StoredVal = cast<Instruction>(SI->getOperand(0));
1274
1275 // If we've already replaced the input, StoredVal will be a cast or
1276 // select instruction. If not, it will be a load of the original
1277 // global.
1278 if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) {
1279 assert(LI->getOperand(0) == GV && "Not a copy!");
1280 // Insert a new load, to preserve the saved value.
1281 StoreVal =
1282 new LoadInst(NewGV->getValueType(), NewGV, LI->getName() + ".b",
1283 false, Align(1), LI->getOrdering(),
1284 LI->getSyncScopeID(), LI->getIterator());
1285 cast<LoadInst>(StoreVal)->setDebugLoc(LI->getDebugLoc());
1286 } else {
1287 assert((isa<CastInst>(StoredVal) || isa<SelectInst>(StoredVal)) &&
1288 "This is not a form that we understand!");
1289 StoreVal = StoredVal->getOperand(0);
1290 assert(isa<LoadInst>(StoreVal) && "Not a load of NewGV!");
1291 }
1292 }
1293 StoreInst *NSI =
1294 new StoreInst(StoreVal, NewGV, false, Align(1), SI->getOrdering(),
1295 SI->getSyncScopeID(), SI->getIterator());
1296 NSI->setDebugLoc(SI->getDebugLoc());
1297 } else {
1298 // Change the load into a load of bool then a select.
1299 LoadInst *LI = cast<LoadInst>(UI);
1300 LoadInst *NLI = new LoadInst(
1301 NewGV->getValueType(), NewGV, LI->getName() + ".b", false, Align(1),
1302 LI->getOrdering(), LI->getSyncScopeID(), LI->getIterator());
1303 Instruction *NSI;
1304 if (IsOneZero)
1305 NSI = new ZExtInst(NLI, LI->getType(), "", LI->getIterator());
1306 else
1307 NSI = SelectInst::Create(NLI, OtherVal, InitVal, "", LI->getIterator());
1308 NSI->takeName(LI);
1309 // Since LI is split into two instructions, NLI and NSI both inherit the
1310 // same DebugLoc
1311 NLI->setDebugLoc(LI->getDebugLoc());
1312 NSI->setDebugLoc(LI->getDebugLoc());
1313 LI->replaceAllUsesWith(NSI);
1314 }
1315 UI->eraseFromParent();
1316 }
1317
1318 // Retain the name of the old global variable. People who are debugging their
1319 // programs may expect these variables to be named the same.
1320 NewGV->takeName(GV);
1321 GV->eraseFromParent();
1322 return true;
1323}
1324
1325static bool
1327 SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats,
1328 function_ref<void(Function &)> DeleteFnCallback = nullptr) {
1330
1331 if (!GV.isDiscardableIfUnused() && !GV.isDeclaration())
1332 return false;
1333
1334 if (const Comdat *C = GV.getComdat())
1335 if (!GV.hasLocalLinkage() && NotDiscardableComdats.count(C))
1336 return false;
1337
1338 bool Dead;
1339 if (auto *F = dyn_cast<Function>(&GV))
1340 Dead = (F->isDeclaration() && F->use_empty()) || F->isDefTriviallyDead();
1341 else
1342 Dead = GV.use_empty();
1343 if (!Dead)
1344 return false;
1345
1346 LLVM_DEBUG(dbgs() << "GLOBAL DEAD: " << GV << "\n");
1347 if (auto *F = dyn_cast<Function>(&GV)) {
1348 if (DeleteFnCallback)
1349 DeleteFnCallback(*F);
1350 }
1352 GV.eraseFromParent();
1353 ++NumDeleted;
1354 return true;
1355}
1356
1358 const Function *F, GlobalValue *GV,
1359 function_ref<DominatorTree &(Function &)> LookupDomTree) {
1360 // Find all uses of GV. We expect them all to be in F, and if we can't
1361 // identify any of the uses we bail out.
1362 //
1363 // On each of these uses, identify if the memory that GV points to is
1364 // used/required/live at the start of the function. If it is not, for example
1365 // if the first thing the function does is store to the GV, the GV can
1366 // possibly be demoted.
1367 //
1368 // We don't do an exhaustive search for memory operations - simply look
1369 // through bitcasts as they're quite common and benign.
1370 const DataLayout &DL = GV->getDataLayout();
1373 for (auto *U : GV->users()) {
1374 Instruction *I = dyn_cast<Instruction>(U);
1375 if (!I)
1376 return false;
1377 assert(I->getParent()->getParent() == F);
1378
1379 if (auto *LI = dyn_cast<LoadInst>(I))
1380 Loads.push_back(LI);
1381 else if (auto *SI = dyn_cast<StoreInst>(I))
1382 Stores.push_back(SI);
1383 else
1384 return false;
1385 }
1386
1387 // We have identified all uses of GV into loads and stores. Now check if all
1388 // of them are known not to depend on the value of the global at the function
1389 // entry point. We do this by ensuring that every load is dominated by at
1390 // least one store.
1391 auto &DT = LookupDomTree(*const_cast<Function *>(F));
1392
1393 // The below check is quadratic. Check we're not going to do too many tests.
1394 // FIXME: Even though this will always have worst-case quadratic time, we
1395 // could put effort into minimizing the average time by putting stores that
1396 // have been shown to dominate at least one load at the beginning of the
1397 // Stores array, making subsequent dominance checks more likely to succeed
1398 // early.
1399 //
1400 // The threshold here is fairly large because global->local demotion is a
1401 // very powerful optimization should it fire.
1402 const unsigned Threshold = 100;
1403 if (Loads.size() * Stores.size() > Threshold)
1404 return false;
1405
1406 for (auto *L : Loads) {
1407 auto *LTy = L->getType();
1408 if (none_of(Stores, [&](const StoreInst *S) {
1409 auto *STy = S->getValueOperand()->getType();
1410 // The load is only dominated by the store if DomTree says so
1411 // and the number of bits loaded in L is less than or equal to
1412 // the number of bits stored in S.
1413 return DT.dominates(S, L) &&
1414 DL.getTypeStoreSize(LTy).getFixedValue() <=
1415 DL.getTypeStoreSize(STy).getFixedValue();
1416 }))
1417 return false;
1418 }
1419 // All loads have known dependences inside F, so the global can be localized.
1420 return true;
1421}
1422
1423// For a global variable with one store, if the store dominates any loads,
1424// those loads will always load the stored value (as opposed to the
1425// initializer), even in the presence of recursion.
1427 GlobalVariable *GV, const StoreInst *StoredOnceStore,
1428 function_ref<DominatorTree &(Function &)> LookupDomTree) {
1429 const Value *StoredOnceValue = StoredOnceStore->getValueOperand();
1430 // We can do this optimization for non-constants in nosync + norecurse
1431 // functions, but globals used in exactly one norecurse functions are already
1432 // promoted to an alloca.
1433 if (!isa<Constant>(StoredOnceValue))
1434 return false;
1435 const Function *F = StoredOnceStore->getFunction();
1437 for (User *U : GV->users()) {
1438 if (auto *LI = dyn_cast<LoadInst>(U)) {
1439 if (LI->getFunction() == F &&
1440 LI->getType() == StoredOnceValue->getType() && LI->isSimple())
1441 Loads.push_back(LI);
1442 }
1443 }
1444 // Only compute DT if we have any loads to examine.
1445 bool MadeChange = false;
1446 if (!Loads.empty()) {
1447 auto &DT = LookupDomTree(*const_cast<Function *>(F));
1448 for (auto *LI : Loads) {
1449 if (DT.dominates(StoredOnceStore, LI)) {
1450 LI->replaceAllUsesWith(const_cast<Value *>(StoredOnceValue));
1451 LI->eraseFromParent();
1452 MadeChange = true;
1453 }
1454 }
1455 }
1456 return MadeChange;
1457}
1458
1459/// Analyze the specified global variable and optimize
1460/// it if possible. If we make a change, return true.
1461static bool
1465 function_ref<DominatorTree &(Function &)> LookupDomTree) {
1466 auto &DL = GV->getDataLayout();
1467 // If this is a first class global and has only one accessing function and
1468 // this function is non-recursive, we replace the global with a local alloca
1469 // in this function.
1470 //
1471 // NOTE: It doesn't make sense to promote non-single-value types since we
1472 // are just replacing static memory to stack memory.
1473 //
1474 // If the global is in different address space, don't bring it to stack.
1475 if (!GS.HasMultipleAccessingFunctions &&
1476 GS.AccessingFunction &&
1478 GV->getType()->getAddressSpace() == DL.getAllocaAddrSpace() &&
1479 !GV->isExternallyInitialized() &&
1480 GS.AccessingFunction->doesNotRecurse() &&
1481 isPointerValueDeadOnEntryToFunction(GS.AccessingFunction, GV,
1482 LookupDomTree)) {
1483 const DataLayout &DL = GV->getDataLayout();
1484
1485 LLVM_DEBUG(dbgs() << "LOCALIZING GLOBAL: " << *GV << "\n");
1486 BasicBlock::iterator FirstI =
1487 GS.AccessingFunction->getEntryBlock().begin().getNonConst();
1488 Type *ElemTy = GV->getValueType();
1489 // FIXME: Pass Global's alignment when globals have alignment
1490 AllocaInst *Alloca = new AllocaInst(ElemTy, DL.getAllocaAddrSpace(),
1491 nullptr, GV->getName(), FirstI);
1493 if (!isa<UndefValue>(GV->getInitializer())) {
1494 auto *SI = new StoreInst(GV->getInitializer(), Alloca, FirstI);
1495 // FIXME: We're localizing a global and creating a store instruction for
1496 // the initial value of that global. Could we logically use the global
1497 // variable's (if one exists) line for this?
1498 SI->setDebugLoc(DebugLoc::getCompilerGenerated());
1499 }
1500
1501 GV->replaceAllUsesWith(Alloca);
1502 GV->eraseFromParent();
1503 ++NumLocalized;
1504 return true;
1505 }
1506
1507 bool Changed = false;
1508
1509 // If the global is never loaded (but may be stored to), it is dead.
1510 // Delete it now.
1511 if (!GS.IsLoaded) {
1512 LLVM_DEBUG(dbgs() << "GLOBAL NEVER LOADED: " << *GV << "\n");
1513
1514 if (isLeakCheckerRoot(GV)) {
1515 // Delete any constant stores to the global.
1516 Changed = CleanupPointerRootUsers(GV, GetTLI);
1517 } else {
1518 // Delete any stores we can find to the global. We may not be able to
1519 // make it completely dead though.
1520 Changed = CleanupConstantGlobalUsers(GV, DL);
1521 }
1522
1523 // If the global is dead now, delete it.
1524 if (GV->use_empty()) {
1525 GV->eraseFromParent();
1526 ++NumDeleted;
1527 Changed = true;
1528 }
1529 return Changed;
1530
1531 }
1532 if (GS.StoredType <= GlobalStatus::InitializerStored) {
1533 LLVM_DEBUG(dbgs() << "MARKING CONSTANT: " << *GV << "\n");
1534
1535 // Don't actually mark a global constant if it's atomic because atomic loads
1536 // are implemented by a trivial cmpxchg in some edge-cases and that usually
1537 // requires write access to the variable even if it's not actually changed.
1538 if (GS.Ordering == AtomicOrdering::NotAtomic) {
1539 assert(!GV->isConstant() && "Expected a non-constant global");
1540 GV->setConstant(true);
1541 Changed = true;
1542 }
1543
1544 // Clean up any obviously simplifiable users now.
1545 Changed |= CleanupConstantGlobalUsers(GV, DL);
1546
1547 // If the global is dead now, just nuke it.
1548 if (GV->use_empty()) {
1549 LLVM_DEBUG(dbgs() << " *** Marking constant allowed us to simplify "
1550 << "all users and delete global!\n");
1551 GV->eraseFromParent();
1552 ++NumDeleted;
1553 return true;
1554 }
1555
1556 // Fall through to the next check; see if we can optimize further.
1557 ++NumMarked;
1558 }
1559 if (!GV->getInitializer()->getType()->isSingleValueType()) {
1560 const DataLayout &DL = GV->getDataLayout();
1561 if (SRAGlobal(GV, DL))
1562 return true;
1563 }
1564 Value *StoredOnceValue = GS.getStoredOnceValue();
1565 if (GS.StoredType == GlobalStatus::StoredOnce && StoredOnceValue) {
1566 Function &StoreFn =
1567 const_cast<Function &>(*GS.StoredOnceStore->getFunction());
1568 bool CanHaveNonUndefGlobalInitializer =
1569 GetTTI(StoreFn).canHaveNonUndefGlobalInitializerInAddressSpace(
1570 GV->getType()->getAddressSpace());
1571 // If the initial value for the global was an undef value, and if only
1572 // one other value was stored into it, we can just change the
1573 // initializer to be the stored value, then delete all stores to the
1574 // global. This allows us to mark it constant.
1575 // This is restricted to address spaces that allow globals to have
1576 // initializers. NVPTX, for example, does not support initializers for
1577 // shared memory (AS 3).
1578 auto *SOVConstant = dyn_cast<Constant>(StoredOnceValue);
1579 if (SOVConstant && isa<UndefValue>(GV->getInitializer()) &&
1580 DL.getTypeAllocSize(SOVConstant->getType()) ==
1581 DL.getTypeAllocSize(GV->getValueType()) &&
1582 CanHaveNonUndefGlobalInitializer) {
1583 if (SOVConstant->getType() == GV->getValueType()) {
1584 // Change the initializer in place.
1585 GV->setInitializer(SOVConstant);
1586 } else {
1587 // Create a new global with adjusted type.
1588 auto *NGV = new GlobalVariable(
1589 *GV->getParent(), SOVConstant->getType(), GV->isConstant(),
1590 GV->getLinkage(), SOVConstant, "", GV, GV->getThreadLocalMode(),
1591 GV->getAddressSpace());
1592 NGV->takeName(GV);
1593 NGV->copyAttributesFrom(GV);
1594 GV->replaceAllUsesWith(NGV);
1595 GV->eraseFromParent();
1596 GV = NGV;
1597 }
1598
1599 // Clean up any obviously simplifiable users now.
1601
1602 if (GV->use_empty()) {
1603 LLVM_DEBUG(dbgs() << " *** Substituting initializer allowed us to "
1604 << "simplify all users and delete global!\n");
1605 GV->eraseFromParent();
1606 ++NumDeleted;
1607 }
1608 ++NumSubstitute;
1609 return true;
1610 }
1611
1612 // Try to optimize globals based on the knowledge that only one value
1613 // (besides its initializer) is ever stored to the global.
1614 if (optimizeOnceStoredGlobal(GV, StoredOnceValue, DL, GetTLI))
1615 return true;
1616
1617 // Try to forward the store to any loads. If we have more than one store, we
1618 // may have a store of the initializer between StoredOnceStore and a load.
1619 if (GS.NumStores == 1)
1620 if (forwardStoredOnceStore(GV, GS.StoredOnceStore, LookupDomTree))
1621 return true;
1622
1623 // Otherwise, if the global was not a boolean, we can shrink it to be a
1624 // boolean. Skip this optimization for AS that doesn't allow an initializer.
1625 if (SOVConstant && GS.Ordering == AtomicOrdering::NotAtomic &&
1626 (!isa<UndefValue>(GV->getInitializer()) ||
1627 CanHaveNonUndefGlobalInitializer)) {
1628 if (TryToShrinkGlobalToBoolean(GV, SOVConstant)) {
1629 ++NumShrunkToBool;
1630 return true;
1631 }
1632 }
1633 }
1634
1635 return Changed;
1636}
1637
1638/// Analyze the specified global variable and optimize it if possible. If we
1639/// make a change, return true.
1640static bool
1644 function_ref<DominatorTree &(Function &)> LookupDomTree) {
1645 if (GV.getName().starts_with("llvm."))
1646 return false;
1647
1648 GlobalStatus GS;
1649
1650 if (GlobalStatus::analyzeGlobal(&GV, GS))
1651 return false;
1652
1653 bool Changed = false;
1654 if (!GS.IsCompared && !GV.hasGlobalUnnamedAddr()) {
1655 auto NewUnnamedAddr = GV.hasLocalLinkage() ? GlobalValue::UnnamedAddr::Global
1656 : GlobalValue::UnnamedAddr::Local;
1657 if (NewUnnamedAddr != GV.getUnnamedAddr()) {
1658 GV.setUnnamedAddr(NewUnnamedAddr);
1659 NumUnnamed++;
1660 Changed = true;
1661 }
1662 }
1663
1664 // Do more involved optimizations if the global is internal.
1665 if (!GV.hasLocalLinkage())
1666 return Changed;
1667
1668 auto *GVar = dyn_cast<GlobalVariable>(&GV);
1669 if (!GVar)
1670 return Changed;
1671
1672 if (GVar->isConstant() || !GVar->hasInitializer())
1673 return Changed;
1674
1675 return processInternalGlobal(GVar, GS, GetTTI, GetTLI, LookupDomTree) ||
1676 Changed;
1677}
1678
1679/// Walk all of the direct calls of the specified function, changing them to
1680/// FastCC.
1682 for (User *U : F->users())
1683 cast<CallBase>(U)->setCallingConv(CallingConv::Fast);
1684}
1685
1688 unsigned AttrIndex;
1689 if (Attrs.hasAttrSomewhere(A, &AttrIndex))
1690 return Attrs.removeAttributeAtIndex(C, AttrIndex, A);
1691 return Attrs;
1692}
1693
1695 F->setAttributes(StripAttr(F->getContext(), F->getAttributes(), A));
1696 for (User *U : F->users()) {
1697 CallBase *CB = cast<CallBase>(U);
1698 CB->setAttributes(StripAttr(F->getContext(), CB->getAttributes(), A));
1699 }
1700}
1701
1702/// Return true if this is a calling convention that we'd like to change. The
1703/// idea here is that we don't want to mess with the convention if the user
1704/// explicitly requested something with performance implications like coldcc,
1705/// GHC, or anyregcc.
1707 CallingConv::ID CC = F->getCallingConv();
1708
1709 // FIXME: Is it worth transforming x86_stdcallcc and x86_fastcallcc?
1710 if (CC != CallingConv::C && CC != CallingConv::X86_ThisCall)
1711 return false;
1712
1713 if (F->isVarArg())
1714 return false;
1715
1716 // FIXME: Change CC for the whole chain of musttail calls when possible.
1717 //
1718 // Can't change CC of the function that either has musttail calls, or is a
1719 // musttail callee itself
1720 for (User *U : F->users()) {
1721 CallInst* CI = dyn_cast<CallInst>(U);
1722 if (!CI)
1723 continue;
1724
1725 if (CI->isMustTailCall())
1726 return false;
1727 }
1728
1729 for (BasicBlock &BB : *F)
1730 if (BB.getTerminatingMustTailCall())
1731 return false;
1732
1733 return !F->hasAddressTaken();
1734}
1735
1738 ChangeableCCCacheTy &ChangeableCCCache) {
1739 auto Res = ChangeableCCCache.try_emplace(F, false);
1740 if (Res.second)
1741 Res.first->second = hasChangeableCCImpl(F);
1742 return Res.first->second;
1743}
1744
1745/// Return true if the block containing the call site has a BlockFrequency of
1746/// less than ColdCCRelFreq% of the entry block.
1747static bool isColdCallSite(CallBase &CB, BlockFrequencyInfo &CallerBFI) {
1748 const BranchProbability ColdProb(ColdCCRelFreq, 100);
1749 auto *CallSiteBB = CB.getParent();
1750 auto CallSiteFreq = CallerBFI.getBlockFreq(CallSiteBB);
1751 auto CallerEntryFreq =
1752 CallerBFI.getBlockFreq(&(CB.getCaller()->getEntryBlock()));
1753 return CallSiteFreq < CallerEntryFreq * ColdProb;
1754}
1755
1756// This function checks if the input function F is cold at all call sites. It
1757// also looks each call site's containing function, returning false if the
1758// caller function contains other non cold calls. The input vector AllCallsCold
1759// contains a list of functions that only have call sites in cold blocks.
1760static bool
1763 const std::vector<Function *> &AllCallsCold) {
1764
1765 if (F.user_empty())
1766 return false;
1767
1768 for (User *U : F.users()) {
1769 CallBase &CB = cast<CallBase>(*U);
1770 Function *CallerFunc = CB.getParent()->getParent();
1771 BlockFrequencyInfo &CallerBFI = GetBFI(*CallerFunc);
1772 if (!isColdCallSite(CB, CallerBFI))
1773 return false;
1774 if (!llvm::is_contained(AllCallsCold, CallerFunc))
1775 return false;
1776 }
1777 return true;
1778}
1779
1781 for (User *U : F->users())
1782 cast<CallBase>(U)->setCallingConv(CallingConv::Cold);
1783}
1784
1785// This function iterates over all the call instructions in the input Function
1786// and checks that all call sites are in cold blocks and are allowed to use the
1787// coldcc calling convention.
1788static bool
1791 ChangeableCCCacheTy &ChangeableCCCache) {
1792 for (BasicBlock &BB : F) {
1793 for (Instruction &I : BB) {
1794 if (CallInst *CI = dyn_cast<CallInst>(&I)) {
1795 // Skip over isline asm instructions since they aren't function calls.
1796 if (CI->isInlineAsm())
1797 continue;
1798 Function *CalledFn = CI->getCalledFunction();
1799 if (!CalledFn)
1800 return false;
1801 // Skip over intrinsics since they won't remain as function calls.
1802 // Important to do this check before the linkage check below so we
1803 // won't bail out on debug intrinsics, possibly making the generated
1804 // code dependent on the presence of debug info.
1805 if (CalledFn->getIntrinsicID() != Intrinsic::not_intrinsic)
1806 continue;
1807 if (!CalledFn->hasLocalLinkage())
1808 return false;
1809 // Check if it's valid to use coldcc calling convention.
1810 if (!hasChangeableCC(CalledFn, ChangeableCCCache))
1811 return false;
1812 BlockFrequencyInfo &CallerBFI = GetBFI(F);
1813 if (!isColdCallSite(*CI, CallerBFI))
1814 return false;
1815 }
1816 }
1817 }
1818 return true;
1819}
1820
1822 for (User *U : F->users()) {
1823 CallBase *CB = cast<CallBase>(U);
1824 if (CB->isMustTailCall())
1825 return true;
1826 }
1827 return false;
1828}
1829
1831 for (User *U : F->users())
1832 if (isa<InvokeInst>(U))
1833 return true;
1834 return false;
1835}
1836
1838 RemoveAttribute(F, Attribute::Preallocated);
1839
1840 auto *M = F->getParent();
1841
1842 IRBuilder<> Builder(M->getContext());
1843
1844 // Cannot modify users() while iterating over it, so make a copy.
1845 SmallVector<User *, 4> PreallocatedCalls(F->users());
1846 for (User *U : PreallocatedCalls) {
1847 CallBase *CB = dyn_cast<CallBase>(U);
1848 if (!CB)
1849 continue;
1850
1851 assert(
1852 !CB->isMustTailCall() &&
1853 "Shouldn't call RemotePreallocated() on a musttail preallocated call");
1854 // Create copy of call without "preallocated" operand bundle.
1856 CB->getOperandBundlesAsDefs(OpBundles);
1857 CallBase *PreallocatedSetup = nullptr;
1858 for (auto *It = OpBundles.begin(); It != OpBundles.end(); ++It) {
1859 if (It->getTag() == "preallocated") {
1860 PreallocatedSetup = cast<CallBase>(*It->input_begin());
1861 OpBundles.erase(It);
1862 break;
1863 }
1864 }
1865 assert(PreallocatedSetup && "Did not find preallocated bundle");
1866 uint64_t ArgCount =
1867 cast<ConstantInt>(PreallocatedSetup->getArgOperand(0))->getZExtValue();
1868
1869 assert((isa<CallInst>(CB) || isa<InvokeInst>(CB)) &&
1870 "Unknown indirect call type");
1871 CallBase *NewCB = CallBase::Create(CB, OpBundles, CB->getIterator());
1872 CB->replaceAllUsesWith(NewCB);
1873 NewCB->takeName(CB);
1874 CB->eraseFromParent();
1875
1876 Builder.SetInsertPoint(PreallocatedSetup);
1877 auto *StackSave = Builder.CreateStackSave();
1878 Builder.SetInsertPoint(NewCB->getNextNode());
1879 Builder.CreateStackRestore(StackSave);
1880
1881 // Replace @llvm.call.preallocated.arg() with alloca.
1882 // Cannot modify users() while iterating over it, so make a copy.
1883 // @llvm.call.preallocated.arg() can be called with the same index multiple
1884 // times. So for each @llvm.call.preallocated.arg(), we see if we have
1885 // already created a Value* for the index, and if not, create an alloca and
1886 // bitcast right after the @llvm.call.preallocated.setup() so that it
1887 // dominates all uses.
1888 SmallVector<Value *, 2> ArgAllocas(ArgCount);
1889 SmallVector<User *, 2> PreallocatedArgs(PreallocatedSetup->users());
1890 for (auto *User : PreallocatedArgs) {
1891 auto *UseCall = cast<CallBase>(User);
1892 assert(UseCall->getCalledFunction()->getIntrinsicID() ==
1893 Intrinsic::call_preallocated_arg &&
1894 "preallocated token use was not a llvm.call.preallocated.arg");
1895 uint64_t AllocArgIndex =
1896 cast<ConstantInt>(UseCall->getArgOperand(1))->getZExtValue();
1897 Value *AllocaReplacement = ArgAllocas[AllocArgIndex];
1898 if (!AllocaReplacement) {
1899 auto AddressSpace = UseCall->getType()->getPointerAddressSpace();
1900 auto *ArgType =
1901 UseCall->getFnAttr(Attribute::Preallocated).getValueAsType();
1902 auto *InsertBefore = PreallocatedSetup->getNextNode();
1903 Builder.SetInsertPoint(InsertBefore);
1904 auto *Alloca =
1905 Builder.CreateAlloca(ArgType, AddressSpace, nullptr, "paarg");
1906 ArgAllocas[AllocArgIndex] = Alloca;
1907 AllocaReplacement = Alloca;
1908 }
1909
1910 UseCall->replaceAllUsesWith(AllocaReplacement);
1911 UseCall->eraseFromParent();
1912 }
1913 // Remove @llvm.call.preallocated.setup().
1914 cast<Instruction>(PreallocatedSetup)->eraseFromParent();
1915 }
1916}
1917
1918static bool
1923 function_ref<DominatorTree &(Function &)> LookupDomTree,
1924 SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats,
1925 function_ref<void(Function &F)> ChangedCFGCallback,
1926 function_ref<void(Function &F)> DeleteFnCallback) {
1927
1928 bool Changed = false;
1929
1930 ChangeableCCCacheTy ChangeableCCCache;
1931 std::vector<Function *> AllCallsCold;
1933 if (hasOnlyColdCalls(F, GetBFI, ChangeableCCCache))
1934 AllCallsCold.push_back(&F);
1935
1936 // Optimize functions.
1938 // Don't perform global opt pass on naked functions; we don't want fast
1939 // calling conventions for naked functions.
1940 if (F.hasFnAttribute(Attribute::Naked))
1941 continue;
1942
1943 // Functions without names cannot be referenced outside this module.
1944 if (!F.hasName() && !F.isDeclaration() && !F.hasLocalLinkage())
1945 F.setLinkage(GlobalValue::InternalLinkage);
1946
1947 if (deleteIfDead(F, NotDiscardableComdats, DeleteFnCallback)) {
1948 Changed = true;
1949 continue;
1950 }
1951
1952 // LLVM's definition of dominance allows instructions that are cyclic
1953 // in unreachable blocks, e.g.:
1954 // %pat = select i1 %condition, @global, i16* %pat
1955 // because any instruction dominates an instruction in a block that's
1956 // not reachable from entry.
1957 // So, remove unreachable blocks from the function, because a) there's
1958 // no point in analyzing them and b) GlobalOpt should otherwise grow
1959 // some more complicated logic to break these cycles.
1960 // Notify the analysis manager that we've modified the function's CFG.
1961 if (!F.isDeclaration()) {
1963 Changed = true;
1964 ChangedCFGCallback(F);
1965 }
1966 }
1967
1968 Changed |= processGlobal(F, GetTTI, GetTLI, LookupDomTree);
1969
1970 if (!F.hasLocalLinkage())
1971 continue;
1972
1973 // If we have an inalloca parameter that we can safely remove the
1974 // inalloca attribute from, do so. This unlocks optimizations that
1975 // wouldn't be safe in the presence of inalloca.
1976 // FIXME: We should also hoist alloca affected by this to the entry
1977 // block if possible.
1978 if (F.getAttributes().hasAttrSomewhere(Attribute::InAlloca) &&
1979 !F.hasAddressTaken() && !hasMustTailCallers(&F) && !F.isVarArg()) {
1980 RemoveAttribute(&F, Attribute::InAlloca);
1981 Changed = true;
1982 }
1983
1984 // FIXME: handle invokes
1985 // FIXME: handle musttail
1986 if (F.getAttributes().hasAttrSomewhere(Attribute::Preallocated)) {
1987 if (!F.hasAddressTaken() && !hasMustTailCallers(&F) &&
1988 !hasInvokeCallers(&F)) {
1990 Changed = true;
1991 }
1992 continue;
1993 }
1994
1995 if (hasChangeableCC(&F, ChangeableCCCache)) {
1996 NumInternalFunc++;
1997 TargetTransformInfo &TTI = GetTTI(F);
1998 // Change the calling convention to coldcc if either stress testing is
1999 // enabled or the target would like to use coldcc on functions which are
2000 // cold at all call sites and the callers contain no other non coldcc
2001 // calls.
2004 isValidCandidateForColdCC(F, GetBFI, AllCallsCold))) {
2005 ChangeableCCCache.erase(&F);
2006 F.setCallingConv(CallingConv::Cold);
2008 Changed = true;
2009 NumColdCC++;
2010 }
2011 }
2012
2013 if (hasChangeableCC(&F, ChangeableCCCache)) {
2014 // If this function has a calling convention worth changing, is not a
2015 // varargs function, and is only called directly, promote it to use the
2016 // Fast calling convention.
2017 F.setCallingConv(CallingConv::Fast);
2019 ++NumFastCallFns;
2020 Changed = true;
2021 }
2022
2023 if (F.getAttributes().hasAttrSomewhere(Attribute::Nest) &&
2024 !F.hasAddressTaken()) {
2025 // The function is not used by a trampoline intrinsic, so it is safe
2026 // to remove the 'nest' attribute.
2027 RemoveAttribute(&F, Attribute::Nest);
2028 ++NumNestRemoved;
2029 Changed = true;
2030 }
2031 }
2032 return Changed;
2033}
2034
2035static bool
2039 function_ref<DominatorTree &(Function &)> LookupDomTree,
2040 SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats) {
2041 bool Changed = false;
2042
2043 for (GlobalVariable &GV : llvm::make_early_inc_range(M.globals())) {
2044 // Global variables without names cannot be referenced outside this module.
2045 if (!GV.hasName() && !GV.isDeclaration() && !GV.hasLocalLinkage())
2047 // Simplify the initializer.
2048 if (GV.hasInitializer())
2049 if (auto *C = dyn_cast<Constant>(GV.getInitializer())) {
2050 auto &DL = M.getDataLayout();
2051 // TLI is not used in the case of a Constant, so use default nullptr
2052 // for that optional parameter, since we don't have a Function to
2053 // provide GetTLI anyway.
2054 Constant *New = ConstantFoldConstant(C, DL, /*TLI*/ nullptr);
2055 if (New != C)
2056 GV.setInitializer(New);
2057 }
2058
2059 if (deleteIfDead(GV, NotDiscardableComdats)) {
2060 Changed = true;
2061 continue;
2062 }
2063
2064 Changed |= processGlobal(GV, GetTTI, GetTLI, LookupDomTree);
2065 }
2066 return Changed;
2067}
2068
2069/// Evaluate static constructors in the function, if we can. Return true if we
2070/// can, false otherwise.
2072 TargetLibraryInfo *TLI) {
2073 // Skip external functions.
2074 if (F->isDeclaration())
2075 return false;
2076 // Call the function.
2077 Evaluator Eval(DL, TLI);
2078 Constant *RetValDummy;
2079 bool EvalSuccess = Eval.EvaluateFunction(F, RetValDummy,
2081
2082 if (EvalSuccess) {
2083 ++NumCtorsEvaluated;
2084
2085 // We succeeded at evaluation: commit the result.
2086 auto NewInitializers = Eval.getMutatedInitializers();
2087 LLVM_DEBUG(dbgs() << "FULLY EVALUATED GLOBAL CTOR FUNCTION '"
2088 << F->getName() << "' to " << NewInitializers.size()
2089 << " stores.\n");
2090 for (const auto &Pair : NewInitializers)
2091 Pair.first->setInitializer(Pair.second);
2092 for (GlobalVariable *GV : Eval.getInvariants())
2093 GV->setConstant(true);
2094 }
2095
2096 return EvalSuccess;
2097}
2098
2099static int compareNames(Constant *const *A, Constant *const *B) {
2100 Value *AStripped = (*A)->stripPointerCasts();
2101 Value *BStripped = (*B)->stripPointerCasts();
2102 return AStripped->getName().compare(BStripped->getName());
2103}
2104
2107 if (Init.empty()) {
2108 V.eraseFromParent();
2109 return;
2110 }
2111
2112 // Get address space of pointers in the array of pointers.
2113 const Type *UsedArrayType = V.getValueType();
2114 const auto *VAT = cast<ArrayType>(UsedArrayType);
2115 const auto *VEPT = cast<PointerType>(VAT->getArrayElementType());
2116
2117 // Type of pointer to the array of pointers.
2118 PointerType *PtrTy =
2119 PointerType::get(V.getContext(), VEPT->getAddressSpace());
2120
2122 for (GlobalValue *GV : Init) {
2124 UsedArray.push_back(Cast);
2125 }
2126
2127 // Sort to get deterministic order.
2128 array_pod_sort(UsedArray.begin(), UsedArray.end(), compareNames);
2129 ArrayType *ATy = ArrayType::get(PtrTy, UsedArray.size());
2130
2131 Module *M = V.getParent();
2132 V.removeFromParent();
2133 GlobalVariable *NV =
2135 ConstantArray::get(ATy, UsedArray), "");
2136 NV->takeName(&V);
2137 NV->setSection("llvm.metadata");
2138 delete &V;
2139}
2140
2141namespace {
2142
2143/// An easy to access representation of llvm.used and llvm.compiler.used.
2144class LLVMUsed {
2146 SmallPtrSet<GlobalValue *, 4> CompilerUsed;
2147 GlobalVariable *UsedV;
2148 GlobalVariable *CompilerUsedV;
2149
2150public:
2151 LLVMUsed(Module &M) {
2153 UsedV = collectUsedGlobalVariables(M, Vec, false);
2154 Used = {llvm::from_range, Vec};
2155 Vec.clear();
2156 CompilerUsedV = collectUsedGlobalVariables(M, Vec, true);
2157 CompilerUsed = {llvm::from_range, Vec};
2158 }
2159
2161 using used_iterator_range = iterator_range<iterator>;
2162
2163 iterator usedBegin() { return Used.begin(); }
2164 iterator usedEnd() { return Used.end(); }
2165
2166 used_iterator_range used() {
2167 return used_iterator_range(usedBegin(), usedEnd());
2168 }
2169
2170 iterator compilerUsedBegin() { return CompilerUsed.begin(); }
2171 iterator compilerUsedEnd() { return CompilerUsed.end(); }
2172
2173 used_iterator_range compilerUsed() {
2174 return used_iterator_range(compilerUsedBegin(), compilerUsedEnd());
2175 }
2176
2177 bool usedCount(GlobalValue *GV) const { return Used.count(GV); }
2178
2179 bool compilerUsedCount(GlobalValue *GV) const {
2180 return CompilerUsed.count(GV);
2181 }
2182
2183 bool usedErase(GlobalValue *GV) { return Used.erase(GV); }
2184 bool compilerUsedErase(GlobalValue *GV) { return CompilerUsed.erase(GV); }
2185 bool usedInsert(GlobalValue *GV) { return Used.insert(GV).second; }
2186
2187 bool compilerUsedInsert(GlobalValue *GV) {
2188 return CompilerUsed.insert(GV).second;
2189 }
2190
2191 void syncVariablesAndSets() {
2192 if (UsedV)
2193 setUsedInitializer(*UsedV, Used);
2194 if (CompilerUsedV)
2195 setUsedInitializer(*CompilerUsedV, CompilerUsed);
2196 }
2197};
2198
2199} // end anonymous namespace
2200
2201static bool hasUseOtherThanLLVMUsed(GlobalAlias &GA, const LLVMUsed &U) {
2202 if (GA.use_empty()) // No use at all.
2203 return false;
2204
2205 assert((!U.usedCount(&GA) || !U.compilerUsedCount(&GA)) &&
2206 "We should have removed the duplicated "
2207 "element from llvm.compiler.used");
2208 if (!GA.hasOneUse())
2209 // Strictly more than one use. So at least one is not in llvm.used and
2210 // llvm.compiler.used.
2211 return true;
2212
2213 // Exactly one use. Check if it is in llvm.used or llvm.compiler.used.
2214 return !U.usedCount(&GA) && !U.compilerUsedCount(&GA);
2215}
2216
2217static bool mayHaveOtherReferences(GlobalValue &GV, const LLVMUsed &U) {
2218 if (!GV.hasLocalLinkage())
2219 return true;
2220
2221 return U.usedCount(&GV) || U.compilerUsedCount(&GV);
2222}
2223
2224static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U,
2225 bool &RenameTarget) {
2226 if (GA.isWeakForLinker())
2227 return false;
2228
2229 RenameTarget = false;
2230 bool Ret = false;
2231 if (hasUseOtherThanLLVMUsed(GA, U))
2232 Ret = true;
2233
2234 // If the alias is externally visible, we may still be able to simplify it.
2235 if (!mayHaveOtherReferences(GA, U))
2236 return Ret;
2237
2238 // If the aliasee has internal linkage and no other references (e.g.,
2239 // @llvm.used, @llvm.compiler.used), give it the name and linkage of the
2240 // alias, and delete the alias. This turns:
2241 // define internal ... @f(...)
2242 // @a = alias ... @f
2243 // into:
2244 // define ... @a(...)
2245 Constant *Aliasee = GA.getAliasee();
2246 GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts());
2248 return Ret;
2249
2250 RenameTarget = true;
2251 return true;
2252}
2253
2254static bool
2256 SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats) {
2257 bool Changed = false;
2258 LLVMUsed Used(M);
2259
2260 for (GlobalValue *GV : Used.used())
2261 Used.compilerUsedErase(GV);
2262
2263 // Return whether GV is explicitly or implicitly dso_local and not replaceable
2264 // by another definition in the current linkage unit.
2265 auto IsModuleLocal = [](GlobalValue &GV) {
2267 (GV.isDSOLocal() || GV.isImplicitDSOLocal());
2268 };
2269
2270 for (GlobalAlias &J : llvm::make_early_inc_range(M.aliases())) {
2271 // Aliases without names cannot be referenced outside this module.
2272 if (!J.hasName() && !J.isDeclaration() && !J.hasLocalLinkage())
2273 J.setLinkage(GlobalValue::InternalLinkage);
2274
2275 if (deleteIfDead(J, NotDiscardableComdats)) {
2276 Changed = true;
2277 continue;
2278 }
2279
2280 // If the alias can change at link time, nothing can be done - bail out.
2281 if (!IsModuleLocal(J))
2282 continue;
2283
2284 Constant *Aliasee = J.getAliasee();
2285 GlobalValue *Target = dyn_cast<GlobalValue>(Aliasee->stripPointerCasts());
2286 // We can't trivially replace the alias with the aliasee if the aliasee is
2287 // non-trivial in some way. We also can't replace the alias with the aliasee
2288 // if the aliasee may be preemptible at runtime. On ELF, a non-preemptible
2289 // alias can be used to access the definition as if preemption did not
2290 // happen.
2291 // TODO: Try to handle non-zero GEPs of local aliasees.
2292 if (!Target || !IsModuleLocal(*Target))
2293 continue;
2294
2295 Target->removeDeadConstantUsers();
2296
2297 // Make all users of the alias use the aliasee instead.
2298 bool RenameTarget;
2299 if (!hasUsesToReplace(J, Used, RenameTarget))
2300 continue;
2301
2302 J.replaceAllUsesWith(Aliasee);
2303 ++NumAliasesResolved;
2304 Changed = true;
2305
2306 if (RenameTarget) {
2307 // Give the aliasee the name, linkage and other attributes of the alias.
2308 Target->takeName(&J);
2309 Target->setLinkage(J.getLinkage());
2310 Target->setDSOLocal(J.isDSOLocal());
2311 Target->setVisibility(J.getVisibility());
2312 Target->setDLLStorageClass(J.getDLLStorageClass());
2313
2314 if (Used.usedErase(&J))
2315 Used.usedInsert(Target);
2316
2317 if (Used.compilerUsedErase(&J))
2318 Used.compilerUsedInsert(Target);
2319 } else if (mayHaveOtherReferences(J, Used))
2320 continue;
2321
2322 // Delete the alias.
2323 M.eraseAlias(&J);
2324 ++NumAliasesRemoved;
2325 Changed = true;
2326 }
2327
2328 Used.syncVariablesAndSets();
2329
2330 return Changed;
2331}
2332
2333static Function *
2336 LibFunc Func) {
2337 // Hack to get a default TLI before we have actual Function.
2338 auto FuncIter = M.begin();
2339 if (FuncIter == M.end())
2340 return nullptr;
2341 auto *TLI = &GetTLI(*FuncIter);
2342
2343 if (!TLI->has(Func))
2344 return nullptr;
2345
2346 Function *Fn = M.getFunction(TLI->getName(Func));
2347 if (!Fn)
2348 return nullptr;
2349
2350 // Now get the actual TLI for Fn.
2351 TLI = &GetTLI(*Fn);
2352
2353 // Make sure that the function has the correct prototype.
2354 LibFunc F;
2355 if (!TLI->getLibFunc(*Fn, F) || F != Func)
2356 return nullptr;
2357
2358 return Fn;
2359}
2360
2361/// Returns whether the given function is an empty C++ destructor or atexit
2362/// handler and can therefore be eliminated. Note that we assume that other
2363/// optimization passes have already simplified the code so we simply check for
2364/// 'ret'.
2365static bool IsEmptyAtExitFunction(const Function &Fn) {
2366 // FIXME: We could eliminate C++ destructors if they're readonly/readnone and
2367 // nounwind, but that doesn't seem worth doing.
2368 if (Fn.isDeclaration())
2369 return false;
2370
2371 for (const auto &I : Fn.getEntryBlock()) {
2372 if (I.isDebugOrPseudoInst())
2373 continue;
2374 if (isa<ReturnInst>(I))
2375 return true;
2376 break;
2377 }
2378 return false;
2379}
2380
2381static bool OptimizeEmptyGlobalAtExitDtors(Function *CXAAtExitFn, bool isCXX) {
2382 /// Itanium C++ ABI p3.3.5:
2383 ///
2384 /// After constructing a global (or local static) object, that will require
2385 /// destruction on exit, a termination function is registered as follows:
2386 ///
2387 /// extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
2388 ///
2389 /// This registration, e.g. __cxa_atexit(f,p,d), is intended to cause the
2390 /// call f(p) when DSO d is unloaded, before all such termination calls
2391 /// registered before this one. It returns zero if registration is
2392 /// successful, nonzero on failure.
2393
2394 // This pass will look for calls to __cxa_atexit or atexit where the function
2395 // is trivial and remove them.
2396 bool Changed = false;
2397
2398 for (User *U : llvm::make_early_inc_range(CXAAtExitFn->users())) {
2399 // We're only interested in calls. Theoretically, we could handle invoke
2400 // instructions as well, but neither llvm-gcc nor clang generate invokes
2401 // to __cxa_atexit.
2402 CallInst *CI = dyn_cast<CallInst>(U);
2403 if (!CI)
2404 continue;
2405
2406 Function *DtorFn =
2407 dyn_cast<Function>(CI->getArgOperand(0)->stripPointerCasts());
2408 if (!DtorFn || !IsEmptyAtExitFunction(*DtorFn))
2409 continue;
2410
2411 // Just remove the call.
2413 CI->eraseFromParent();
2414
2415 if (isCXX)
2416 ++NumCXXDtorsRemoved;
2417 else
2418 ++NumAtExitRemoved;
2419
2420 Changed |= true;
2421 }
2422
2423 return Changed;
2424}
2425
2427 if (IF.isInterposable())
2428 return nullptr;
2429
2430 Function *Resolver = IF.getResolverFunction();
2431 if (!Resolver)
2432 return nullptr;
2433
2434 if (Resolver->isInterposable())
2435 return nullptr;
2436
2437 // Only handle functions that have been optimized into a single basic block.
2438 auto It = Resolver->begin();
2439 if (++It != Resolver->end())
2440 return nullptr;
2441
2442 BasicBlock &BB = Resolver->getEntryBlock();
2443
2444 if (any_of(BB, [](Instruction &I) { return I.mayHaveSideEffects(); }))
2445 return nullptr;
2446
2447 auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator());
2448 if (!Ret)
2449 return nullptr;
2450
2451 return dyn_cast<Function>(Ret->getReturnValue());
2452}
2453
2454/// Find IFuncs that have resolvers that always point at the same statically
2455/// known callee, and replace their callers with a direct call.
2457 bool Changed = false;
2458 for (GlobalIFunc &IF : M.ifuncs())
2460 if (!IF.use_empty() &&
2461 (!Callee->isDeclaration() ||
2462 none_of(IF.users(), [](User *U) { return isa<GlobalAlias>(U); }))) {
2463 IF.replaceAllUsesWith(Callee);
2464 NumIFuncsResolved++;
2465 Changed = true;
2466 }
2467 return Changed;
2468}
2469
2470static bool
2472 SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats) {
2473 bool Changed = false;
2474 for (GlobalIFunc &IF : make_early_inc_range(M.ifuncs()))
2475 if (deleteIfDead(IF, NotDiscardableComdats)) {
2476 NumIFuncsDeleted++;
2477 Changed = true;
2478 }
2479 return Changed;
2480}
2481
2482// Follows the use-def chain of \p V backwards until it finds a Function,
2483// in which case it collects in \p Versions. Return true on successful
2484// use-def chain traversal, false otherwise.
2486 SmallVectorImpl<Function *> &Versions) {
2487 if (auto *F = dyn_cast<Function>(V)) {
2489 return false;
2490 Versions.push_back(F);
2491 } else if (auto *Sel = dyn_cast<SelectInst>(V)) {
2492 if (!collectVersions(TTI, Sel->getTrueValue(), Versions))
2493 return false;
2494 if (!collectVersions(TTI, Sel->getFalseValue(), Versions))
2495 return false;
2496 } else if (auto *Phi = dyn_cast<PHINode>(V)) {
2497 for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I)
2498 if (!collectVersions(TTI, Phi->getIncomingValue(I), Versions))
2499 return false;
2500 } else {
2501 // Unknown instruction type. Bail.
2502 return false;
2503 }
2504 return true;
2505}
2506
2507// Bypass the IFunc Resolver of MultiVersioned functions when possible. To
2508// deduce whether the optimization is legal we need to compare the target
2509// features between caller and callee versions. The criteria for bypassing
2510// the resolver are the following:
2511//
2512// * If the callee's feature set is a subset of the caller's feature set,
2513// then the callee is a candidate for direct call.
2514//
2515// * Among such candidates the one of highest priority is the best match
2516// and it shall be picked, unless there is a version of the callee with
2517// higher priority than the best match which cannot be picked from a
2518// higher priority caller (directly or through the resolver).
2519//
2520// * For every higher priority callee version than the best match, there
2521// is a higher priority caller version whose feature set availability
2522// is implied by the callee's feature set.
2523//
2526 bool Changed = false;
2527
2528 // Cache containing the mask constructed from a function's target features.
2529 DenseMap<Function *, APInt> FeatureMask;
2530
2531 for (GlobalIFunc &IF : M.ifuncs()) {
2532 if (IF.isInterposable())
2533 continue;
2534
2535 Function *Resolver = IF.getResolverFunction();
2536 if (!Resolver)
2537 continue;
2538
2539 if (Resolver->isInterposable())
2540 continue;
2541
2542 TargetTransformInfo &TTI = GetTTI(*Resolver);
2543
2544 // Discover the callee versions.
2546 if (any_of(*Resolver, [&TTI, &Callees](BasicBlock &BB) {
2547 if (auto *Ret = dyn_cast_or_null<ReturnInst>(BB.getTerminator()))
2548 if (!collectVersions(TTI, Ret->getReturnValue(), Callees))
2549 return true;
2550 return false;
2551 }))
2552 continue;
2553
2554 assert(!Callees.empty() && "Expecting successful collection of versions");
2555
2556 LLVM_DEBUG(dbgs() << "Statically resolving calls to function "
2557 << Resolver->getName() << "\n");
2558
2559 // Cache the feature mask for each callee.
2560 for (Function *Callee : Callees) {
2561 auto [It, Inserted] = FeatureMask.try_emplace(Callee);
2562 if (Inserted)
2563 It->second = TTI.getFeatureMask(*Callee);
2564 }
2565
2566 // Sort the callee versions in decreasing priority order.
2567 sort(Callees, [&](auto *LHS, auto *RHS) {
2568 return FeatureMask[LHS].ugt(FeatureMask[RHS]);
2569 });
2570
2571 // Find the callsites and cache the feature mask for each caller.
2574 for (User *U : IF.users()) {
2575 if (auto *CB = dyn_cast<CallBase>(U)) {
2576 if (CB->getCalledOperand() == &IF) {
2577 Function *Caller = CB->getFunction();
2578 auto [FeatIt, FeatInserted] = FeatureMask.try_emplace(Caller);
2579 if (FeatInserted)
2580 FeatIt->second = TTI.getFeatureMask(*Caller);
2581 auto [CallIt, CallInserted] = CallSites.try_emplace(Caller);
2582 if (CallInserted)
2583 Callers.push_back(Caller);
2584 CallIt->second.push_back(CB);
2585 }
2586 }
2587 }
2588
2589 // Sort the caller versions in decreasing priority order.
2590 sort(Callers, [&](auto *LHS, auto *RHS) {
2591 return FeatureMask[LHS].ugt(FeatureMask[RHS]);
2592 });
2593
2594 auto implies = [](APInt A, APInt B) { return B.isSubsetOf(A); };
2595
2596 // Index to the highest priority candidate.
2597 unsigned I = 0;
2598 // Now try to redirect calls starting from higher priority callers.
2599 for (Function *Caller : Callers) {
2600 assert(I < Callees.size() && "Found callers of equal priority");
2601
2602 Function *Callee = Callees[I];
2603 APInt CallerBits = FeatureMask[Caller];
2604 APInt CalleeBits = FeatureMask[Callee];
2605
2606 // In the case of FMV callers, we know that all higher priority callers
2607 // than the current one did not get selected at runtime, which helps
2608 // reason about the callees (if they have versions that mandate presence
2609 // of the features which we already know are unavailable on this target).
2610 if (TTI.isMultiversionedFunction(*Caller)) {
2611 // If the feature set of the caller implies the feature set of the
2612 // highest priority candidate then it shall be picked. In case of
2613 // identical sets advance the candidate index one position.
2614 if (CallerBits == CalleeBits)
2615 ++I;
2616 else if (!implies(CallerBits, CalleeBits)) {
2617 // Keep advancing the candidate index as long as the caller's
2618 // features are a subset of the current candidate's.
2619 while (implies(CalleeBits, CallerBits)) {
2620 if (++I == Callees.size())
2621 break;
2622 CalleeBits = FeatureMask[Callees[I]];
2623 }
2624 continue;
2625 }
2626 } else {
2627 // We can't reason much about non-FMV callers. Just pick the highest
2628 // priority callee if it matches, otherwise bail.
2629 if (!OptimizeNonFMVCallers || I > 0 || !implies(CallerBits, CalleeBits))
2630 continue;
2631 }
2632 auto &Calls = CallSites[Caller];
2633 for (CallBase *CS : Calls) {
2634 LLVM_DEBUG(dbgs() << "Redirecting call " << Caller->getName() << " -> "
2635 << Callee->getName() << "\n");
2636 CS->setCalledOperand(Callee);
2637 }
2638 Changed = true;
2639 }
2640 if (IF.use_empty() ||
2641 all_of(IF.users(), [](User *U) { return isa<GlobalAlias>(U); }))
2642 NumIFuncsResolved++;
2643 }
2644 return Changed;
2645}
2646
2647static bool
2652 function_ref<DominatorTree &(Function &)> LookupDomTree,
2653 function_ref<void(Function &F)> ChangedCFGCallback,
2654 function_ref<void(Function &F)> DeleteFnCallback) {
2655 SmallPtrSet<const Comdat *, 8> NotDiscardableComdats;
2656 bool Changed = false;
2657 bool LocalChange = true;
2658 std::optional<uint32_t> FirstNotFullyEvaluatedPriority;
2659
2660 while (LocalChange) {
2661 LocalChange = false;
2662
2663 NotDiscardableComdats.clear();
2664 for (const GlobalVariable &GV : M.globals())
2665 if (const Comdat *C = GV.getComdat())
2666 if (!GV.isDiscardableIfUnused() || !GV.use_empty())
2667 NotDiscardableComdats.insert(C);
2668 for (Function &F : M)
2669 if (const Comdat *C = F.getComdat())
2670 if (!F.isDefTriviallyDead())
2671 NotDiscardableComdats.insert(C);
2672 for (GlobalAlias &GA : M.aliases())
2673 if (const Comdat *C = GA.getComdat())
2674 if (!GA.isDiscardableIfUnused() || !GA.use_empty())
2675 NotDiscardableComdats.insert(C);
2676
2677 // Delete functions that are trivially dead, ccc -> fastcc
2678 LocalChange |= OptimizeFunctions(M, GetTLI, GetTTI, GetBFI, LookupDomTree,
2679 NotDiscardableComdats, ChangedCFGCallback,
2680 DeleteFnCallback);
2681
2682 // Optimize global_ctors list.
2683 LocalChange |=
2684 optimizeGlobalCtorsList(M, [&](uint32_t Priority, Function *F) {
2685 if (FirstNotFullyEvaluatedPriority &&
2686 *FirstNotFullyEvaluatedPriority != Priority)
2687 return false;
2688 bool Evaluated = EvaluateStaticConstructor(F, DL, &GetTLI(*F));
2689 if (!Evaluated)
2690 FirstNotFullyEvaluatedPriority = Priority;
2691 return Evaluated;
2692 });
2693
2694 // Optimize non-address-taken globals.
2695 LocalChange |= OptimizeGlobalVars(M, GetTTI, GetTLI, LookupDomTree,
2696 NotDiscardableComdats);
2697
2698 // Resolve aliases, when possible.
2699 LocalChange |= OptimizeGlobalAliases(M, NotDiscardableComdats);
2700
2701 // Try to remove trivial global destructors if they are not removed
2702 // already.
2703 if (Function *CXAAtExitFn =
2704 FindAtExitLibFunc(M, GetTLI, LibFunc_cxa_atexit))
2705 LocalChange |= OptimizeEmptyGlobalAtExitDtors(CXAAtExitFn, true);
2706
2707 if (Function *AtExitFn = FindAtExitLibFunc(M, GetTLI, LibFunc_atexit))
2708 LocalChange |= OptimizeEmptyGlobalAtExitDtors(AtExitFn, false);
2709
2710 // Optimize IFuncs whose callee's are statically known.
2711 LocalChange |= OptimizeStaticIFuncs(M);
2712
2713 // Optimize IFuncs based on the target features of the caller.
2714 LocalChange |= OptimizeNonTrivialIFuncs(M, GetTTI);
2715
2716 // Remove any IFuncs that are now dead.
2717 LocalChange |= DeleteDeadIFuncs(M, NotDiscardableComdats);
2718
2719 Changed |= LocalChange;
2720 }
2721
2722 // TODO: Move all global ctors functions to the end of the module for code
2723 // layout.
2724
2725 return Changed;
2726}
2727
2729 auto &DL = M.getDataLayout();
2730 auto &FAM =
2732 auto LookupDomTree = [&FAM](Function &F) -> DominatorTree &{
2734 };
2735 auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
2737 };
2738 auto GetTTI = [&FAM](Function &F) -> TargetTransformInfo & {
2740 };
2741
2742 auto GetBFI = [&FAM](Function &F) -> BlockFrequencyInfo & {
2744 };
2745 auto ChangedCFGCallback = [&FAM](Function &F) {
2747 };
2748 auto DeleteFnCallback = [&FAM](Function &F) { FAM.clear(F, F.getName()); };
2749
2750 if (!optimizeGlobalsInModule(M, DL, GetTLI, GetTTI, GetBFI, LookupDomTree,
2751 ChangedCFGCallback, DeleteFnCallback))
2752 return PreservedAnalyses::all();
2753
2755 // We made sure to clear analyses for deleted functions.
2757 // The only place we modify the CFG is when calling
2758 // removeUnreachableBlocks(), but there we make sure to invalidate analyses
2759 // for modified functions.
2761 return PA;
2762}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Atomic ordering constants.
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
This file contains constants used for implementing Dwarf debug support.
static bool IsSafeComputationToRemove(Value *V, function_ref< TargetLibraryInfo &(Function &)> GetTLI)
Given a value that is stored to a global but never read, determine whether it's safe to remove the st...
Definition: GlobalOpt.cpp:167
static Function * FindAtExitLibFunc(Module &M, function_ref< TargetLibraryInfo &(Function &)> GetTLI, LibFunc Func)
Definition: GlobalOpt.cpp:2334
static bool optimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal, const DataLayout &DL, function_ref< TargetLibraryInfo &(Function &)> GetTLI)
Definition: GlobalOpt.cpp:1133
static Function * hasSideeffectFreeStaticResolution(GlobalIFunc &IF)
Definition: GlobalOpt.cpp:2426
static bool tryToOptimizeStoreOfAllocationToGlobal(GlobalVariable *GV, CallInst *CI, const DataLayout &DL, TargetLibraryInfo *TLI)
If we have a global that is only initialized with a fixed size allocation try to transform the progra...
Definition: GlobalOpt.cpp:1086
static void ConstantPropUsersOf(Value *V, const DataLayout &DL, TargetLibraryInfo *TLI)
Walk the use list of V, constant folding all of the instructions that are foldable.
Definition: GlobalOpt.cpp:901
static bool hasOnlyColdCalls(Function &F, function_ref< BlockFrequencyInfo &(Function &)> GetBFI, ChangeableCCCacheTy &ChangeableCCCache)
Definition: GlobalOpt.cpp:1789
static bool allUsesOfLoadedValueWillTrapIfNull(const GlobalVariable *GV)
Return true if all uses of any loads from GV will trap if the loaded value is null.
Definition: GlobalOpt.cpp:713
static bool hasChangeableCCImpl(Function *F)
Return true if this is a calling convention that we'd like to change.
Definition: GlobalOpt.cpp:1706
static bool AllUsesOfValueWillTrapIfNull(const Value *V, SmallPtrSetImpl< const PHINode * > &PHIs)
Return true if all users of the specified value will trap if the value is dynamically null.
Definition: GlobalOpt.cpp:659
static GlobalVariable * OptimizeGlobalAddressOfAllocation(GlobalVariable *GV, CallInst *CI, uint64_t AllocSize, Constant *InitVal, const DataLayout &DL, TargetLibraryInfo *TLI)
This function takes the specified global variable, and transforms the program as if it always contain...
Definition: GlobalOpt.cpp:923
Returns whether the given function is an empty C destructor or atexit handler and can therefore be eliminated Note that we assume that other optimization passes have already simplified the code so we simply check for static ret bool IsEmptyAtExitFunction(const Function &Fn)
Definition: GlobalOpt.cpp:2365
static bool collectSRATypes(DenseMap< uint64_t, GlobalPart > &Parts, GlobalVariable *GV, const DataLayout &DL)
Look at all uses of the global and determine which (offset, type) pairs it can be split into.
Definition: GlobalOpt.cpp:355
static bool valueIsOnlyUsedLocallyOrStoredToOneGlobal(const CallInst *CI, const GlobalVariable *GV)
Scan the use-list of GV checking to make sure that there are no complex uses of GV.
Definition: GlobalOpt.cpp:1047
static bool OptimizeFunctions(Module &M, function_ref< TargetLibraryInfo &(Function &)> GetTLI, function_ref< TargetTransformInfo &(Function &)> GetTTI, function_ref< BlockFrequencyInfo &(Function &)> GetBFI, function_ref< DominatorTree &(Function &)> LookupDomTree, SmallPtrSetImpl< const Comdat * > &NotDiscardableComdats, function_ref< void(Function &F)> ChangedCFGCallback, function_ref< void(Function &F)> DeleteFnCallback)
Definition: GlobalOpt.cpp:1919
static bool DeleteDeadIFuncs(Module &M, SmallPtrSetImpl< const Comdat * > &NotDiscardableComdats)
Definition: GlobalOpt.cpp:2471
static void RemoveAttribute(Function *F, Attribute::AttrKind A)
Definition: GlobalOpt.cpp:1694
static bool hasChangeableCC(Function *F, ChangeableCCCacheTy &ChangeableCCCache)
Definition: GlobalOpt.cpp:1737
static bool deleteIfDead(GlobalValue &GV, SmallPtrSetImpl< const Comdat * > &NotDiscardableComdats, function_ref< void(Function &)> DeleteFnCallback=nullptr)
Definition: GlobalOpt.cpp:1326
static void RemovePreallocated(Function *F)
Definition: GlobalOpt.cpp:1837
static cl::opt< bool > OptimizeNonFMVCallers("optimize-non-fmv-callers", cl::desc("Statically resolve calls to versioned " "functions from non-versioned callers."), cl::init(true), cl::Hidden)
static bool processGlobal(GlobalValue &GV, function_ref< TargetTransformInfo &(Function &)> GetTTI, function_ref< TargetLibraryInfo &(Function &)> GetTLI, function_ref< DominatorTree &(Function &)> LookupDomTree)
Analyze the specified global variable and optimize it if possible.
Definition: GlobalOpt.cpp:1641
static bool isColdCallSite(CallBase &CB, BlockFrequencyInfo &CallerBFI)
Return true if the block containing the call site has a BlockFrequency of less than ColdCCRelFreq% of...
Definition: GlobalOpt.cpp:1747
static void transferSRADebugInfo(GlobalVariable *GV, GlobalVariable *NGV, uint64_t FragmentOffsetInBits, uint64_t FragmentSizeInBits, uint64_t VarSize)
Copy over the debug info for a variable to its SRA replacements.
Definition: GlobalOpt.cpp:444
static cl::opt< bool > EnableColdCCStressTest("enable-coldcc-stress-test", cl::desc("Enable stress test of coldcc by adding " "calling conv to all internal functions."), cl::init(false), cl::Hidden)
static bool OptimizeGlobalAliases(Module &M, SmallPtrSetImpl< const Comdat * > &NotDiscardableComdats)
Definition: GlobalOpt.cpp:2255
static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal)
At this point, we have learned that the only two values ever stored into GV are its initializer and O...
Definition: GlobalOpt.cpp:1166
static void ChangeCalleesToFastCall(Function *F)
Walk all of the direct calls of the specified function, changing them to FastCC.
Definition: GlobalOpt.cpp:1681
static bool hasMustTailCallers(Function *F)
Definition: GlobalOpt.cpp:1821
static bool OptimizeNonTrivialIFuncs(Module &M, function_ref< TargetTransformInfo &(Function &)> GetTTI)
Definition: GlobalOpt.cpp:2524
static bool OptimizeGlobalVars(Module &M, function_ref< TargetTransformInfo &(Function &)> GetTTI, function_ref< TargetLibraryInfo &(Function &)> GetTLI, function_ref< DominatorTree &(Function &)> LookupDomTree, SmallPtrSetImpl< const Comdat * > &NotDiscardableComdats)
Definition: GlobalOpt.cpp:2036
static void allUsesOfLoadAndStores(GlobalVariable *GV, SmallVector< Value *, 4 > &Uses)
Get all the loads/store uses for global variable GV.
Definition: GlobalOpt.cpp:747
static bool OptimizeEmptyGlobalAtExitDtors(Function *CXAAtExitFn, bool isCXX)
Definition: GlobalOpt.cpp:2381
static bool mayHaveOtherReferences(GlobalValue &GV, const LLVMUsed &U)
Definition: GlobalOpt.cpp:2217
static void changeCallSitesToColdCC(Function *F)
Definition: GlobalOpt.cpp:1780
static AttributeList StripAttr(LLVMContext &C, AttributeList Attrs, Attribute::AttrKind A)
Definition: GlobalOpt.cpp:1686
static bool hasInvokeCallers(Function *F)
Definition: GlobalOpt.cpp:1830
static void setUsedInitializer(GlobalVariable &V, const SmallPtrSetImpl< GlobalValue * > &Init)
Definition: GlobalOpt.cpp:2105
static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV, const DataLayout &DL, function_ref< TargetLibraryInfo &(Function &)> GetTLI)
The specified global has only one non-null value stored into it.
Definition: GlobalOpt.cpp:836
static bool isValidCandidateForColdCC(Function &F, function_ref< BlockFrequencyInfo &(Function &)> GetBFI, const std::vector< Function * > &AllCallsCold)
Definition: GlobalOpt.cpp:1761
static cl::opt< int > ColdCCRelFreq("coldcc-rel-freq", cl::Hidden, cl::init(2), cl::desc("Maximum block frequency, expressed as a percentage of caller's " "entry frequency, for a call site to be considered cold for enabling " "coldcc"))
static bool optimizeGlobalsInModule(Module &M, const DataLayout &DL, function_ref< TargetLibraryInfo &(Function &)> GetTLI, function_ref< TargetTransformInfo &(Function &)> GetTTI, function_ref< BlockFrequencyInfo &(Function &)> GetBFI, function_ref< DominatorTree &(Function &)> LookupDomTree, function_ref< void(Function &F)> ChangedCFGCallback, function_ref< void(Function &F)> DeleteFnCallback)
Definition: GlobalOpt.cpp:2648
static bool EvaluateStaticConstructor(Function *F, const DataLayout &DL, TargetLibraryInfo *TLI)
Evaluate static constructors in the function, if we can.
Definition: GlobalOpt.cpp:2071
static bool CleanupConstantGlobalUsers(GlobalVariable *GV, const DataLayout &DL)
We just marked GV constant.
Definition: GlobalOpt.cpp:276
Find IFuncs that have resolvers that always point at the same statically known and replace their callers with a direct static call bool OptimizeStaticIFuncs(Module &M)
Definition: GlobalOpt.cpp:2456
static bool isLeakCheckerRoot(GlobalVariable *GV)
Is this global variable possibly used by a leak checker as a root? If so, we might not really want to...
Definition: GlobalOpt.cpp:117
static bool forwardStoredOnceStore(GlobalVariable *GV, const StoreInst *StoredOnceStore, function_ref< DominatorTree &(Function &)> LookupDomTree)
Definition: GlobalOpt.cpp:1426
static int compareNames(Constant *const *A, Constant *const *B)
Definition: GlobalOpt.cpp:2099
static bool collectVersions(TargetTransformInfo &TTI, Value *V, SmallVectorImpl< Function * > &Versions)
Definition: GlobalOpt.cpp:2485
static bool CleanupPointerRootUsers(GlobalVariable *GV, function_ref< TargetLibraryInfo &(Function &)> GetTLI)
This GV is a pointer root.
Definition: GlobalOpt.cpp:198
static bool isPointerValueDeadOnEntryToFunction(const Function *F, GlobalValue *GV, function_ref< DominatorTree &(Function &)> LookupDomTree)
Definition: GlobalOpt.cpp:1357
static bool processInternalGlobal(GlobalVariable *GV, const GlobalStatus &GS, function_ref< TargetTransformInfo &(Function &)> GetTTI, function_ref< TargetLibraryInfo &(Function &)> GetTLI, function_ref< DominatorTree &(Function &)> LookupDomTree)
Analyze the specified global variable and optimize it if possible.
Definition: GlobalOpt.cpp:1462
static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U, bool &RenameTarget)
Definition: GlobalOpt.cpp:2224
static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV)
Definition: GlobalOpt.cpp:766
static GlobalVariable * SRAGlobal(GlobalVariable *GV, const DataLayout &DL)
Perform scalar replacement of aggregates on the specified global variable.
Definition: GlobalOpt.cpp:524
static bool hasUseOtherThanLLVMUsed(GlobalAlias &GA, const LLVMUsed &U)
Definition: GlobalOpt.cpp:2201
Hexagon Common GEP
#define _
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
This defines the Use class.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
uint64_t IntrinsicInst * II
#define P(N)
FunctionAnalysisManager FAM
if(PassOpts->AAPipeline)
Remove Loads Into Fake Uses
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallPtrSet class.
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
#define LLVM_DEBUG(...)
Definition: Debug.h:119
static SymbolRef::Type getType(const Symbol *Sym)
Definition: TapiFile.cpp:39
This pass exposes codegen information to IR-level passes.
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition: APInt.h:78
This class represents a conversion between pointers from one address space to another.
an instruction to allocate memory on the stack
Definition: Instructions.h:64
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
void clear(IRUnitT &IR, llvm::StringRef Name)
Clear any cached analysis results for a single unit of IR.
void invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Invalidate cached analyses for an IR unit.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:412
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition: Attributes.h:88
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:170
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:233
static LLVM_ABI BinaryOperator * CreateNot(Value *Op, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Analysis pass which computes BlockFrequencyInfo.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
LLVM_ABI BlockFrequency getBlockFreq(const BasicBlock *BB) const
getblockFreq - Return block frequency.
Represents analyses that only rely on functions' control flow.
Definition: Analysis.h:73
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1116
LLVM_ABI void getOperandBundlesAsDefs(SmallVectorImpl< OperandBundleDef > &Defs) const
Return the list of operand bundles attached to this instruction as a vector of OperandBundleDefs.
LLVM_ABI bool isMustTailCall() const
Tests if this call site must be tail call optimized.
Value * getCalledOperand() const
Definition: InstrTypes.h:1340
void setAttributes(AttributeList A)
Set the attributes for this call.
Definition: InstrTypes.h:1427
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1292
void setArgOperand(unsigned i, Value *v)
Definition: InstrTypes.h:1297
static LLVM_ABI CallBase * Create(CallBase *CB, ArrayRef< OperandBundleDef > Bundles, InsertPosition InsertPt=nullptr)
Create a clone of CB with a different set of operand bundles and insert it before InsertPt.
void setCalledOperand(Value *V)
Definition: InstrTypes.h:1384
unsigned arg_size() const
Definition: InstrTypes.h:1290
AttributeList getAttributes() const
Return the attributes for this call.
Definition: InstrTypes.h:1424
LLVM_ABI Function * getCaller()
Helper to get the caller (the parent function).
This class represents a function call, abstracting a target machine's calling convention.
bool isMustTailCall() const
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:767
static LLVM_ABI Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1314
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:1120
static LLVM_ABI Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:2261
static LLVM_ABI Constant * getAddrSpaceCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2340
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, GEPNoWrapFlags NW=GEPNoWrapFlags::none(), std::optional< ConstantRange > InRange=std::nullopt, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1274
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:868
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:875
static LLVM_ABI ConstantInt * getBool(LLVMContext &Context, bool V)
Definition: Constants.cpp:882
This is an important base class in LLVM.
Definition: Constant.h:43
const Constant * stripPointerCasts() const
Definition: Constant.h:219
LLVM_ABI void removeDeadConstantUsers() const
If there are any dead constant users dangling off of this constant, remove them.
Definition: Constants.cpp:739
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:373
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:90
DWARF expression.
LLVM_ABI bool extractIfOffset(int64_t &Offset) const
If this is a constant offset, extract it.
static LLVM_ABI std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
static LLVM_ABI DIExpression * prependOpcodes(const DIExpression *Expr, SmallVectorImpl< uint64_t > &Ops, bool StackValue=false, bool EntryValue=false)
Prepend DIExpr with the given opcodes and optionally turn it into a stack value.
A pair of DIGlobalVariable and DIExpression.
uint64_t getSizeInBits() const
Base class for variables.
DIType * getType() const
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
static DebugLoc getCompilerGenerated()
Definition: DebugLoc.h:163
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition: DenseMap.h:245
bool erase(const KeyT &Val)
Definition: DenseMap.h:319
unsigned size() const
Definition: DenseMap.h:120
bool empty() const
Definition: DenseMap.h:119
iterator begin()
Definition: DenseMap.h:78
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:230
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:284
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:165
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Definition: Dominators.cpp:135
This class evaluates LLVM IR, producing the Constant representing each SSA instruction.
Definition: Evaluator.h:37
DenseMap< GlobalVariable *, Constant * > getMutatedInitializers() const
Definition: Evaluator.h:102
bool EvaluateFunction(Function *F, Constant *&RetVal, const SmallVectorImpl< Constant * > &ActualArgs)
Evaluate a call to function F, returning true if successful, false if we can't evaluate it.
Definition: Evaluator.cpp:595
const SmallPtrSetImpl< GlobalVariable * > & getInvariants() const
Definition: Evaluator.h:109
const BasicBlock & getEntryBlock() const
Definition: Function.h:807
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition: Function.h:244
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:359
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:949
const Constant * getAliasee() const
Definition: GlobalAlias.h:87
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: GlobalOpt.cpp:2728
bool isDSOLocal() const
Definition: GlobalValue.h:307
bool isImplicitDSOLocal() const
Definition: GlobalValue.h:300
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:316
LinkageTypes getLinkage() const
Definition: GlobalValue.h:548
void setUnnamedAddr(UnnamedAddr Val)
Definition: GlobalValue.h:233
bool hasLocalLinkage() const
Definition: GlobalValue.h:530
bool hasPrivateLinkage() const
Definition: GlobalValue.h:529
LLVM_ABI const Comdat * getComdat() const
Definition: Globals.cpp:201
ThreadLocalMode getThreadLocalMode() const
Definition: GlobalValue.h:273
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:539
unsigned getAddressSpace() const
Definition: GlobalValue.h:207
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:663
LLVM_ABI void eraseFromParent()
This method unlinks 'this' from the containing module and deletes it.
Definition: Globals.cpp:93
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:296
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition: Globals.cpp:132
static bool isInterposableLinkage(LinkageTypes Linkage)
Whether the definition of this global may be replaced by something non-equivalent at link time.
Definition: GlobalValue.h:427
bool hasGlobalUnnamedAddr() const
Definition: GlobalValue.h:217
UnnamedAddr getUnnamedAddr() const
Definition: GlobalValue.h:230
static bool isWeakForLinker(LinkageTypes Linkage)
Whether the definition of this global may be replaced at link time.
Definition: GlobalValue.h:460
static bool isDiscardableIfUnused(LinkageTypes Linkage)
Whether the definition of this global may be discarded if it is not used in its compilation unit.
Definition: GlobalValue.h:451
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:60
@ AppendingLinkage
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:59
Type * getValueType() const
Definition: GlobalValue.h:298
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
LLVM_ABI void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition: Globals.cpp:511
bool isExternallyInitialized() const
MaybeAlign getAlign() const
Returns the alignment of the given variable.
void setConstant(bool Val)
LLVM_ABI void copyAttributesFrom(const GlobalVariable *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...
Definition: Globals.cpp:540
LLVM_ABI void getDebugInfo(SmallVectorImpl< DIGlobalVariableExpression * > &GVs) const
Fill the vector with all debug info attachements.
Definition: Metadata.cpp:1932
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
LLVM_ABI void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition: Globals.cpp:507
LLVM_ABI void addDebugInfo(DIGlobalVariableExpression *GV)
Attach a DIGlobalVariableExpression.
Definition: Metadata.cpp:1928
void setAlignment(Align Align)
Sets the alignment attribute of the GlobalVariable.
This instruction compares its operands according to the predicate given to the constructor.
AllocaInst * CreateAlloca(Type *Ty, unsigned AddrSpace, Value *ArraySize=nullptr, const Twine &Name="")
Definition: IRBuilder.h:1830
CallInst * CreateStackSave(const Twine &Name="")
Create a call to llvm.stacksave.
Definition: IRBuilder.h:1121
CallInst * CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, MaybeAlign Align, bool isVolatile=false, const AAMDNodes &AAInfo=AAMDNodes())
Create and insert a memset to the specified pointer and the specified value.
Definition: IRBuilder.h:630
CallInst * CreateStackRestore(Value *Ptr, const Twine &Name="")
Create a call to llvm.stackrestore.
Definition: IRBuilder.h:1128
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:207
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2780
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:585
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:513
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:82
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:510
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:49
Invoke instruction.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
An instruction for reading from memory.
Definition: Instructions.h:180
AtomicOrdering getOrdering() const
Returns the ordering constraint of this load instruction.
Definition: Instructions.h:224
SyncScope::ID getSyncScopeID() const
Returns the synchronization scope ID of this load instruction.
Definition: Instructions.h:234
LLVMContext & getContext() const
Definition: Metadata.h:1241
This is the common base class for memset/memcpy/memmove.
This class wraps the llvm.memset and llvm.memset.inline intrinsics.
This class wraps the llvm.memcpy/memmove intrinsics.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
void insertGlobalVariable(GlobalVariable *GV)
Insert global variable GV at the end of the global variable list and take ownership.
Definition: Module.h:568
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:740
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition: Analysis.h:151
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition: Analysis.h:132
static LLVM_ABI void SalvageDebugInfo(const Constant &C)
Replace all uses of the constant with Undef in debug info metadata.
Definition: Metadata.cpp:331
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2196
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", InsertPosition InsertBefore=nullptr, Instruction *MDFrom=nullptr)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:380
bool erase(PtrType Ptr)
Remove pointer from the set.
Definition: SmallPtrSet.h:418
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:470
iterator end() const
Definition: SmallPtrSet.h:499
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:401
iterator begin() const
Definition: SmallPtrSet.h:494
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:541
bool empty() const
Definition: SmallVector.h:82
size_t size() const
Definition: SmallVector.h:79
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
void reserve(size_type N)
Definition: SmallVector.h:664
iterator erase(const_iterator CI)
Definition: SmallVector.h:738
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
An instruction for storing to memory.
Definition: Instructions.h:296
Value * getValueOperand()
Definition: Instructions.h:383
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:269
int compare(StringRef RHS) const
compare - Compare two strings; the result is negative, zero, or positive if this string is lexicograp...
Definition: StringRef.h:187
Class to represent struct types.
Definition: DerivedTypes.h:218
ArrayRef< Type * > elements() const
Definition: DerivedTypes.h:360
bool isOpaque() const
Return true if this is a type with an identity that has no body specified yet.
Definition: DerivedTypes.h:294
Analysis pass providing the TargetTransformInfo.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
LLVM_ABI bool isMultiversionedFunction(const Function &F) const
Returns true if this is an instance of a function with multiple versions.
LLVM_ABI bool useColdCCForColdCall(Function &F) const
Return true if the input function which is cold at all call sites, should use coldcc calling conventi...
LLVM_ABI APInt getFeatureMask(const Function &F) const
Returns a bitmask constructed from the target-features or fmv-features metadata of a function.
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:273
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:267
@ ArrayTyID
Arrays.
Definition: Type.h:74
@ ScalableVectorTyID
Scalable SIMD vector type.
Definition: Type.h:76
@ StructTyID
Structures.
Definition: Type.h:73
@ FixedVectorTyID
Fixed width SIMD vector type.
Definition: Type.h:75
@ PointerTyID
Pointers.
Definition: Type.h:72
bool isSingleValueType() const
Return true if the type is a valid type for a register in codegen.
Definition: Type.h:296
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
LLVM_ABI bool isScalableTy(SmallPtrSetImpl< const Type * > &Visited) const
Return true if this is a type whose size is a known multiple of vscale.
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition: Type.h:184
TypeID getTypeID() const
Return the type id for the type.
Definition: Type.h:136
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1866
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
LLVM_ABI void set(Value *Val)
Definition: Value.h:905
User * getUser() const
Returns the User that contains this Use.
Definition: Use.h:61
Value * getOperand(unsigned i) const
Definition: User.h:232
LLVM Value Representation.
Definition: Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition: Value.h:439
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:546
iterator_range< user_iterator > users()
Definition: Value.h:426
use_iterator use_begin()
Definition: Value.h:364
User * user_back()
Definition: Value.h:412
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:701
bool use_empty() const
Definition: Value.h:346
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1098
user_iterator_impl< User > user_iterator
Definition: Value.h:391
bool hasName() const
Definition: Value.h:262
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:322
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:396
This class represents zero extension of integer types.
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
Definition: ilist_node.h:34
self_iterator getIterator()
Definition: ilist_node.h:134
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition: ilist_node.h:359
A range adaptor for a pair of iterators.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Cold
Attempts to make code in the caller as efficient as possible under the assumption that the call is no...
Definition: CallingConv.h:47
@ X86_ThisCall
Similar to X86_StdCall.
Definition: CallingConv.h:122
@ Fast
Attempts to make calls as fast as possible (e.g.
Definition: CallingConv.h:41
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:444
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1744
LLVM_ABI Constant * getInitialValueOfAllocation(const Value *V, const TargetLibraryInfo *TLI, Type *Ty)
If this is a call to an allocation function that initializes memory to a fixed value,...
LLVM_ABI bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
If the specified value is a trivially dead instruction, delete it.
Definition: Local.cpp:533
LLVM_ABI Constant * ConstantFoldInstruction(const Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
LLVM_ABI bool isRemovableAlloc(const CallBase *V, const TargetLibraryInfo *TLI)
Return true if this is a call to an allocation function that does not have side effects that we are r...
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
constexpr from_range_t from_range
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2155
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:663
LLVM_ABI Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1751
LLVM_ABI bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition: Local.cpp:402
LLVM_ABI bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL, const TargetLibraryInfo *TLI, ObjectSizeOpts Opts={})
Compute the size of the object pointed by Ptr.
LLVM_ABI Constant * ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty, const DataLayout &DL)
If C is a uniform value where all bits are the same (either all zero, all ones, all undef or all pois...
bool isSafeToDestroyConstant(const Constant *C)
It is safe to destroy a constant iff it is only used by constants itself.
LLVM_ABI Align getOrEnforceKnownAlignment(Value *V, MaybeAlign PrefAlign, const DataLayout &DL, const Instruction *CxtI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr)
Try to ensure that the alignment of V is at least PrefAlign bytes.
Definition: Local.cpp:1566
bool optimizeGlobalCtorsList(Module &M, function_ref< bool(uint32_t, Function *)> ShouldRemove)
Call "ShouldRemove" for every entry in M's global_ctor list and remove the entries for which it retur...
Definition: CtorUtils.cpp:110
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1669
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
Definition: Function.cpp:1172
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
bool isPointerTy(const Type *T)
Definition: SPIRVUtils.h:288
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1758
LLVM_ABI Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)
Extract value of C at the given Offset reinterpreted as Ty.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ABI bool RecursivelyDeleteTriviallyDeadInstructionsPermissive(SmallVectorImpl< WeakTrackingVH > &DeadInsts, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
Same functionality as RecursivelyDeleteTriviallyDeadInstructions, but allow instructions that are not...
Definition: Local.cpp:548
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition: STLExtras.h:1980
LLVM_ABI bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI)
Tests if a value is a call or invoke to a library function that allocates or reallocates memory (eith...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1916
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition: Alignment.h:212
Type * getLoadStoreType(const Value *I)
A helper function that returns the type of a load or store instruction.
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
Definition: STLExtras.h:1629
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Remove all blocks that can not be reached from the function's entry.
Definition: Local.cpp:2883
LLVM_ABI 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:863
Part of the global at a specific offset, which is only accessed through loads and stores with the giv...
Definition: GlobalOpt.cpp:346
bool IsStored
Definition: GlobalOpt.cpp:350
Constant * Initializer
Definition: GlobalOpt.cpp:348
bool IsLoaded
Definition: GlobalOpt.cpp:349
Type * Ty
Definition: GlobalOpt.cpp:347
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
As we analyze each global or thread-local variable, keep track of some information about it.
Definition: GlobalStatus.h:30
@ InitializerStored
This global is stored to, but the only thing stored is the constant it was initialized with.
Definition: GlobalStatus.h:48
@ StoredOnce
This global is stored to, but only its initializer and one other value is ever stored to it.
Definition: GlobalStatus.h:54
static bool analyzeGlobal(const Value *V, GlobalStatus &GS)
Look at all uses of the global and fill in the GlobalStatus structure.
Various options to control the behavior of getObjectSize.
Function object to check whether the first component of a container supported by std::get (like std::...
Definition: STLExtras.h:1472