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