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