xref: /llvm-project/llvm/lib/Transforms/Utils/Local.cpp (revision 39601a6e5484de183bf525b7d0624e7890ccd8ab)
1 //===- Local.cpp - Functions to perform local transformations -------------===//
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 family of functions perform various local transformations to the
10 // program.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Transforms/Utils/Local.h"
15 #include "llvm/ADT/APInt.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/DenseMapInfo.h"
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/Hashing.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SetVector.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/Analysis/AssumeBundleQueries.h"
26 #include "llvm/Analysis/ConstantFolding.h"
27 #include "llvm/Analysis/DomTreeUpdater.h"
28 #include "llvm/Analysis/InstructionSimplify.h"
29 #include "llvm/Analysis/MemoryBuiltins.h"
30 #include "llvm/Analysis/MemorySSAUpdater.h"
31 #include "llvm/Analysis/TargetLibraryInfo.h"
32 #include "llvm/Analysis/ValueTracking.h"
33 #include "llvm/Analysis/VectorUtils.h"
34 #include "llvm/BinaryFormat/Dwarf.h"
35 #include "llvm/IR/Argument.h"
36 #include "llvm/IR/Attributes.h"
37 #include "llvm/IR/BasicBlock.h"
38 #include "llvm/IR/CFG.h"
39 #include "llvm/IR/Constant.h"
40 #include "llvm/IR/ConstantRange.h"
41 #include "llvm/IR/Constants.h"
42 #include "llvm/IR/DIBuilder.h"
43 #include "llvm/IR/DataLayout.h"
44 #include "llvm/IR/DebugInfo.h"
45 #include "llvm/IR/DebugInfoMetadata.h"
46 #include "llvm/IR/DebugLoc.h"
47 #include "llvm/IR/DerivedTypes.h"
48 #include "llvm/IR/Dominators.h"
49 #include "llvm/IR/EHPersonalities.h"
50 #include "llvm/IR/Function.h"
51 #include "llvm/IR/GetElementPtrTypeIterator.h"
52 #include "llvm/IR/GlobalObject.h"
53 #include "llvm/IR/IRBuilder.h"
54 #include "llvm/IR/InstrTypes.h"
55 #include "llvm/IR/Instruction.h"
56 #include "llvm/IR/Instructions.h"
57 #include "llvm/IR/IntrinsicInst.h"
58 #include "llvm/IR/Intrinsics.h"
59 #include "llvm/IR/IntrinsicsWebAssembly.h"
60 #include "llvm/IR/LLVMContext.h"
61 #include "llvm/IR/MDBuilder.h"
62 #include "llvm/IR/MemoryModelRelaxationAnnotations.h"
63 #include "llvm/IR/Metadata.h"
64 #include "llvm/IR/Module.h"
65 #include "llvm/IR/PatternMatch.h"
66 #include "llvm/IR/ProfDataUtils.h"
67 #include "llvm/IR/Type.h"
68 #include "llvm/IR/Use.h"
69 #include "llvm/IR/User.h"
70 #include "llvm/IR/Value.h"
71 #include "llvm/IR/ValueHandle.h"
72 #include "llvm/Support/Casting.h"
73 #include "llvm/Support/CommandLine.h"
74 #include "llvm/Support/Debug.h"
75 #include "llvm/Support/ErrorHandling.h"
76 #include "llvm/Support/KnownBits.h"
77 #include "llvm/Support/raw_ostream.h"
78 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
79 #include "llvm/Transforms/Utils/ValueMapper.h"
80 #include <algorithm>
81 #include <cassert>
82 #include <cstdint>
83 #include <iterator>
84 #include <map>
85 #include <optional>
86 #include <utility>
87 
88 using namespace llvm;
89 using namespace llvm::PatternMatch;
90 
91 extern cl::opt<bool> UseNewDbgInfoFormat;
92 
93 #define DEBUG_TYPE "local"
94 
95 STATISTIC(NumRemoved, "Number of unreachable basic blocks removed");
96 STATISTIC(NumPHICSEs, "Number of PHI's that got CSE'd");
97 
98 static cl::opt<bool> PHICSEDebugHash(
99     "phicse-debug-hash",
100 #ifdef EXPENSIVE_CHECKS
101     cl::init(true),
102 #else
103     cl::init(false),
104 #endif
105     cl::Hidden,
106     cl::desc("Perform extra assertion checking to verify that PHINodes's hash "
107              "function is well-behaved w.r.t. its isEqual predicate"));
108 
109 static cl::opt<unsigned> PHICSENumPHISmallSize(
110     "phicse-num-phi-smallsize", cl::init(32), cl::Hidden,
111     cl::desc(
112         "When the basic block contains not more than this number of PHI nodes, "
113         "perform a (faster!) exhaustive search instead of set-driven one."));
114 
115 static cl::opt<unsigned> MaxPhiEntriesIncreaseAfterRemovingEmptyBlock(
116     "max-phi-entries-increase-after-removing-empty-block", cl::init(1000),
117     cl::Hidden,
118     cl::desc("Stop removing an empty block if removing it will introduce more "
119              "than this number of phi entries in its successor"));
120 
121 // Max recursion depth for collectBitParts used when detecting bswap and
122 // bitreverse idioms.
123 static const unsigned BitPartRecursionMaxDepth = 48;
124 
125 //===----------------------------------------------------------------------===//
126 //  Local constant propagation.
127 //
128 
129 /// ConstantFoldTerminator - If a terminator instruction is predicated on a
130 /// constant value, convert it into an unconditional branch to the constant
131 /// destination.  This is a nontrivial operation because the successors of this
132 /// basic block must have their PHI nodes updated.
133 /// Also calls RecursivelyDeleteTriviallyDeadInstructions() on any branch/switch
134 /// conditions and indirectbr addresses this might make dead if
135 /// DeleteDeadConditions is true.
136 bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions,
137                                   const TargetLibraryInfo *TLI,
138                                   DomTreeUpdater *DTU) {
139   Instruction *T = BB->getTerminator();
140   IRBuilder<> Builder(T);
141 
142   // Branch - See if we are conditional jumping on constant
143   if (auto *BI = dyn_cast<BranchInst>(T)) {
144     if (BI->isUnconditional()) return false;  // Can't optimize uncond branch
145 
146     BasicBlock *Dest1 = BI->getSuccessor(0);
147     BasicBlock *Dest2 = BI->getSuccessor(1);
148 
149     if (Dest2 == Dest1) {       // Conditional branch to same location?
150       // This branch matches something like this:
151       //     br bool %cond, label %Dest, label %Dest
152       // and changes it into:  br label %Dest
153 
154       // Let the basic block know that we are letting go of one copy of it.
155       assert(BI->getParent() && "Terminator not inserted in block!");
156       Dest1->removePredecessor(BI->getParent());
157 
158       // Replace the conditional branch with an unconditional one.
159       BranchInst *NewBI = Builder.CreateBr(Dest1);
160 
161       // Transfer the metadata to the new branch instruction.
162       NewBI->copyMetadata(*BI, {LLVMContext::MD_loop, LLVMContext::MD_dbg,
163                                 LLVMContext::MD_annotation});
164 
165       Value *Cond = BI->getCondition();
166       BI->eraseFromParent();
167       if (DeleteDeadConditions)
168         RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI);
169       return true;
170     }
171 
172     if (auto *Cond = dyn_cast<ConstantInt>(BI->getCondition())) {
173       // Are we branching on constant?
174       // YES.  Change to unconditional branch...
175       BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2;
176       BasicBlock *OldDest = Cond->getZExtValue() ? Dest2 : Dest1;
177 
178       // Let the basic block know that we are letting go of it.  Based on this,
179       // it will adjust it's PHI nodes.
180       OldDest->removePredecessor(BB);
181 
182       // Replace the conditional branch with an unconditional one.
183       BranchInst *NewBI = Builder.CreateBr(Destination);
184 
185       // Transfer the metadata to the new branch instruction.
186       NewBI->copyMetadata(*BI, {LLVMContext::MD_loop, LLVMContext::MD_dbg,
187                                 LLVMContext::MD_annotation});
188 
189       BI->eraseFromParent();
190       if (DTU)
191         DTU->applyUpdates({{DominatorTree::Delete, BB, OldDest}});
192       return true;
193     }
194 
195     return false;
196   }
197 
198   if (auto *SI = dyn_cast<SwitchInst>(T)) {
199     // If we are switching on a constant, we can convert the switch to an
200     // unconditional branch.
201     auto *CI = dyn_cast<ConstantInt>(SI->getCondition());
202     BasicBlock *DefaultDest = SI->getDefaultDest();
203     BasicBlock *TheOnlyDest = DefaultDest;
204 
205     // If the default is unreachable, ignore it when searching for TheOnlyDest.
206     if (isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg()) &&
207         SI->getNumCases() > 0) {
208       TheOnlyDest = SI->case_begin()->getCaseSuccessor();
209     }
210 
211     bool Changed = false;
212 
213     // Figure out which case it goes to.
214     for (auto It = SI->case_begin(), End = SI->case_end(); It != End;) {
215       // Found case matching a constant operand?
216       if (It->getCaseValue() == CI) {
217         TheOnlyDest = It->getCaseSuccessor();
218         break;
219       }
220 
221       // Check to see if this branch is going to the same place as the default
222       // dest.  If so, eliminate it as an explicit compare.
223       if (It->getCaseSuccessor() == DefaultDest) {
224         MDNode *MD = getValidBranchWeightMDNode(*SI);
225         unsigned NCases = SI->getNumCases();
226         // Fold the case metadata into the default if there will be any branches
227         // left, unless the metadata doesn't match the switch.
228         if (NCases > 1 && MD) {
229           // Collect branch weights into a vector.
230           SmallVector<uint32_t, 8> Weights;
231           extractBranchWeights(MD, Weights);
232 
233           // Merge weight of this case to the default weight.
234           unsigned Idx = It->getCaseIndex();
235           // TODO: Add overflow check.
236           Weights[0] += Weights[Idx + 1];
237           // Remove weight for this case.
238           std::swap(Weights[Idx + 1], Weights.back());
239           Weights.pop_back();
240           setBranchWeights(*SI, Weights, hasBranchWeightOrigin(MD));
241         }
242         // Remove this entry.
243         BasicBlock *ParentBB = SI->getParent();
244         DefaultDest->removePredecessor(ParentBB);
245         It = SI->removeCase(It);
246         End = SI->case_end();
247 
248         // Removing this case may have made the condition constant. In that
249         // case, update CI and restart iteration through the cases.
250         if (auto *NewCI = dyn_cast<ConstantInt>(SI->getCondition())) {
251           CI = NewCI;
252           It = SI->case_begin();
253         }
254 
255         Changed = true;
256         continue;
257       }
258 
259       // Otherwise, check to see if the switch only branches to one destination.
260       // We do this by reseting "TheOnlyDest" to null when we find two non-equal
261       // destinations.
262       if (It->getCaseSuccessor() != TheOnlyDest)
263         TheOnlyDest = nullptr;
264 
265       // Increment this iterator as we haven't removed the case.
266       ++It;
267     }
268 
269     if (CI && !TheOnlyDest) {
270       // Branching on a constant, but not any of the cases, go to the default
271       // successor.
272       TheOnlyDest = SI->getDefaultDest();
273     }
274 
275     // If we found a single destination that we can fold the switch into, do so
276     // now.
277     if (TheOnlyDest) {
278       // Insert the new branch.
279       Builder.CreateBr(TheOnlyDest);
280       BasicBlock *BB = SI->getParent();
281 
282       SmallSet<BasicBlock *, 8> RemovedSuccessors;
283 
284       // Remove entries from PHI nodes which we no longer branch to...
285       BasicBlock *SuccToKeep = TheOnlyDest;
286       for (BasicBlock *Succ : successors(SI)) {
287         if (DTU && Succ != TheOnlyDest)
288           RemovedSuccessors.insert(Succ);
289         // Found case matching a constant operand?
290         if (Succ == SuccToKeep) {
291           SuccToKeep = nullptr; // Don't modify the first branch to TheOnlyDest
292         } else {
293           Succ->removePredecessor(BB);
294         }
295       }
296 
297       // Delete the old switch.
298       Value *Cond = SI->getCondition();
299       SI->eraseFromParent();
300       if (DeleteDeadConditions)
301         RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI);
302       if (DTU) {
303         std::vector<DominatorTree::UpdateType> Updates;
304         Updates.reserve(RemovedSuccessors.size());
305         for (auto *RemovedSuccessor : RemovedSuccessors)
306           Updates.push_back({DominatorTree::Delete, BB, RemovedSuccessor});
307         DTU->applyUpdates(Updates);
308       }
309       return true;
310     }
311 
312     if (SI->getNumCases() == 1) {
313       // Otherwise, we can fold this switch into a conditional branch
314       // instruction if it has only one non-default destination.
315       auto FirstCase = *SI->case_begin();
316       Value *Cond = Builder.CreateICmpEQ(SI->getCondition(),
317           FirstCase.getCaseValue(), "cond");
318 
319       // Insert the new branch.
320       BranchInst *NewBr = Builder.CreateCondBr(Cond,
321                                                FirstCase.getCaseSuccessor(),
322                                                SI->getDefaultDest());
323       SmallVector<uint32_t> Weights;
324       if (extractBranchWeights(*SI, Weights) && Weights.size() == 2) {
325         uint32_t DefWeight = Weights[0];
326         uint32_t CaseWeight = Weights[1];
327         // The TrueWeight should be the weight for the single case of SI.
328         NewBr->setMetadata(LLVMContext::MD_prof,
329                            MDBuilder(BB->getContext())
330                                .createBranchWeights(CaseWeight, DefWeight));
331       }
332 
333       // Update make.implicit metadata to the newly-created conditional branch.
334       MDNode *MakeImplicitMD = SI->getMetadata(LLVMContext::MD_make_implicit);
335       if (MakeImplicitMD)
336         NewBr->setMetadata(LLVMContext::MD_make_implicit, MakeImplicitMD);
337 
338       // Delete the old switch.
339       SI->eraseFromParent();
340       return true;
341     }
342     return Changed;
343   }
344 
345   if (auto *IBI = dyn_cast<IndirectBrInst>(T)) {
346     // indirectbr blockaddress(@F, @BB) -> br label @BB
347     if (auto *BA =
348           dyn_cast<BlockAddress>(IBI->getAddress()->stripPointerCasts())) {
349       BasicBlock *TheOnlyDest = BA->getBasicBlock();
350       SmallSet<BasicBlock *, 8> RemovedSuccessors;
351 
352       // Insert the new branch.
353       Builder.CreateBr(TheOnlyDest);
354 
355       BasicBlock *SuccToKeep = TheOnlyDest;
356       for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
357         BasicBlock *DestBB = IBI->getDestination(i);
358         if (DTU && DestBB != TheOnlyDest)
359           RemovedSuccessors.insert(DestBB);
360         if (IBI->getDestination(i) == SuccToKeep) {
361           SuccToKeep = nullptr;
362         } else {
363           DestBB->removePredecessor(BB);
364         }
365       }
366       Value *Address = IBI->getAddress();
367       IBI->eraseFromParent();
368       if (DeleteDeadConditions)
369         // Delete pointer cast instructions.
370         RecursivelyDeleteTriviallyDeadInstructions(Address, TLI);
371 
372       // Also zap the blockaddress constant if there are no users remaining,
373       // otherwise the destination is still marked as having its address taken.
374       if (BA->use_empty())
375         BA->destroyConstant();
376 
377       // If we didn't find our destination in the IBI successor list, then we
378       // have undefined behavior.  Replace the unconditional branch with an
379       // 'unreachable' instruction.
380       if (SuccToKeep) {
381         BB->getTerminator()->eraseFromParent();
382         new UnreachableInst(BB->getContext(), BB);
383       }
384 
385       if (DTU) {
386         std::vector<DominatorTree::UpdateType> Updates;
387         Updates.reserve(RemovedSuccessors.size());
388         for (auto *RemovedSuccessor : RemovedSuccessors)
389           Updates.push_back({DominatorTree::Delete, BB, RemovedSuccessor});
390         DTU->applyUpdates(Updates);
391       }
392       return true;
393     }
394   }
395 
396   return false;
397 }
398 
399 //===----------------------------------------------------------------------===//
400 //  Local dead code elimination.
401 //
402 
403 /// isInstructionTriviallyDead - Return true if the result produced by the
404 /// instruction is not used, and the instruction has no side effects.
405 ///
406 bool llvm::isInstructionTriviallyDead(Instruction *I,
407                                       const TargetLibraryInfo *TLI) {
408   if (!I->use_empty())
409     return false;
410   return wouldInstructionBeTriviallyDead(I, TLI);
411 }
412 
413 bool llvm::wouldInstructionBeTriviallyDeadOnUnusedPaths(
414     Instruction *I, const TargetLibraryInfo *TLI) {
415   // Instructions that are "markers" and have implied meaning on code around
416   // them (without explicit uses), are not dead on unused paths.
417   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
418     if (II->getIntrinsicID() == Intrinsic::stacksave ||
419         II->getIntrinsicID() == Intrinsic::launder_invariant_group ||
420         II->isLifetimeStartOrEnd())
421       return false;
422   return wouldInstructionBeTriviallyDead(I, TLI);
423 }
424 
425 bool llvm::wouldInstructionBeTriviallyDead(const Instruction *I,
426                                            const TargetLibraryInfo *TLI) {
427   if (I->isTerminator())
428     return false;
429 
430   // We don't want the landingpad-like instructions removed by anything this
431   // general.
432   if (I->isEHPad())
433     return false;
434 
435   // We don't want debug info removed by anything this general.
436   if (isa<DbgVariableIntrinsic>(I))
437     return false;
438 
439   if (const DbgLabelInst *DLI = dyn_cast<DbgLabelInst>(I)) {
440     if (DLI->getLabel())
441       return false;
442     return true;
443   }
444 
445   if (auto *CB = dyn_cast<CallBase>(I))
446     if (isRemovableAlloc(CB, TLI))
447       return true;
448 
449   if (!I->willReturn()) {
450     auto *II = dyn_cast<IntrinsicInst>(I);
451     if (!II)
452       return false;
453 
454     switch (II->getIntrinsicID()) {
455     case Intrinsic::experimental_guard: {
456       // Guards on true are operationally no-ops.  In the future we can
457       // consider more sophisticated tradeoffs for guards considering potential
458       // for check widening, but for now we keep things simple.
459       auto *Cond = dyn_cast<ConstantInt>(II->getArgOperand(0));
460       return Cond && Cond->isOne();
461     }
462     // TODO: These intrinsics are not safe to remove, because this may remove
463     // a well-defined trap.
464     case Intrinsic::wasm_trunc_signed:
465     case Intrinsic::wasm_trunc_unsigned:
466     case Intrinsic::ptrauth_auth:
467     case Intrinsic::ptrauth_resign:
468       return true;
469     default:
470       return false;
471     }
472   }
473 
474   if (!I->mayHaveSideEffects())
475     return true;
476 
477   // Special case intrinsics that "may have side effects" but can be deleted
478   // when dead.
479   if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
480     // Safe to delete llvm.stacksave and launder.invariant.group if dead.
481     if (II->getIntrinsicID() == Intrinsic::stacksave ||
482         II->getIntrinsicID() == Intrinsic::launder_invariant_group)
483       return true;
484 
485     // Intrinsics declare sideeffects to prevent them from moving, but they are
486     // nops without users.
487     if (II->getIntrinsicID() == Intrinsic::allow_runtime_check ||
488         II->getIntrinsicID() == Intrinsic::allow_ubsan_check)
489       return true;
490 
491     if (II->isLifetimeStartOrEnd()) {
492       auto *Arg = II->getArgOperand(1);
493       // Lifetime intrinsics are dead when their right-hand is undef.
494       if (isa<UndefValue>(Arg))
495         return true;
496       // If the right-hand is an alloc, global, or argument and the only uses
497       // are lifetime intrinsics then the intrinsics are dead.
498       if (isa<AllocaInst>(Arg) || isa<GlobalValue>(Arg) || isa<Argument>(Arg))
499         return llvm::all_of(Arg->uses(), [](Use &Use) {
500           if (IntrinsicInst *IntrinsicUse =
501                   dyn_cast<IntrinsicInst>(Use.getUser()))
502             return IntrinsicUse->isLifetimeStartOrEnd();
503           return false;
504         });
505       return false;
506     }
507 
508     // Assumptions are dead if their condition is trivially true.
509     if (II->getIntrinsicID() == Intrinsic::assume &&
510         isAssumeWithEmptyBundle(cast<AssumeInst>(*II))) {
511       if (ConstantInt *Cond = dyn_cast<ConstantInt>(II->getArgOperand(0)))
512         return !Cond->isZero();
513 
514       return false;
515     }
516 
517     if (auto *FPI = dyn_cast<ConstrainedFPIntrinsic>(I)) {
518       std::optional<fp::ExceptionBehavior> ExBehavior =
519           FPI->getExceptionBehavior();
520       return *ExBehavior != fp::ebStrict;
521     }
522   }
523 
524   if (auto *Call = dyn_cast<CallBase>(I)) {
525     if (Value *FreedOp = getFreedOperand(Call, TLI))
526       if (Constant *C = dyn_cast<Constant>(FreedOp))
527         return C->isNullValue() || isa<UndefValue>(C);
528     if (isMathLibCallNoop(Call, TLI))
529       return true;
530   }
531 
532   // Non-volatile atomic loads from constants can be removed.
533   if (auto *LI = dyn_cast<LoadInst>(I))
534     if (auto *GV = dyn_cast<GlobalVariable>(
535             LI->getPointerOperand()->stripPointerCasts()))
536       if (!LI->isVolatile() && GV->isConstant())
537         return true;
538 
539   return false;
540 }
541 
542 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
543 /// trivially dead instruction, delete it.  If that makes any of its operands
544 /// trivially dead, delete them too, recursively.  Return true if any
545 /// instructions were deleted.
546 bool llvm::RecursivelyDeleteTriviallyDeadInstructions(
547     Value *V, const TargetLibraryInfo *TLI, MemorySSAUpdater *MSSAU,
548     std::function<void(Value *)> AboutToDeleteCallback) {
549   Instruction *I = dyn_cast<Instruction>(V);
550   if (!I || !isInstructionTriviallyDead(I, TLI))
551     return false;
552 
553   SmallVector<WeakTrackingVH, 16> DeadInsts;
554   DeadInsts.push_back(I);
555   RecursivelyDeleteTriviallyDeadInstructions(DeadInsts, TLI, MSSAU,
556                                              AboutToDeleteCallback);
557 
558   return true;
559 }
560 
561 bool llvm::RecursivelyDeleteTriviallyDeadInstructionsPermissive(
562     SmallVectorImpl<WeakTrackingVH> &DeadInsts, const TargetLibraryInfo *TLI,
563     MemorySSAUpdater *MSSAU,
564     std::function<void(Value *)> AboutToDeleteCallback) {
565   unsigned S = 0, E = DeadInsts.size(), Alive = 0;
566   for (; S != E; ++S) {
567     auto *I = dyn_cast_or_null<Instruction>(DeadInsts[S]);
568     if (!I || !isInstructionTriviallyDead(I)) {
569       DeadInsts[S] = nullptr;
570       ++Alive;
571     }
572   }
573   if (Alive == E)
574     return false;
575   RecursivelyDeleteTriviallyDeadInstructions(DeadInsts, TLI, MSSAU,
576                                              AboutToDeleteCallback);
577   return true;
578 }
579 
580 void llvm::RecursivelyDeleteTriviallyDeadInstructions(
581     SmallVectorImpl<WeakTrackingVH> &DeadInsts, const TargetLibraryInfo *TLI,
582     MemorySSAUpdater *MSSAU,
583     std::function<void(Value *)> AboutToDeleteCallback) {
584   // Process the dead instruction list until empty.
585   while (!DeadInsts.empty()) {
586     Value *V = DeadInsts.pop_back_val();
587     Instruction *I = cast_or_null<Instruction>(V);
588     if (!I)
589       continue;
590     assert(isInstructionTriviallyDead(I, TLI) &&
591            "Live instruction found in dead worklist!");
592     assert(I->use_empty() && "Instructions with uses are not dead.");
593 
594     // Don't lose the debug info while deleting the instructions.
595     salvageDebugInfo(*I);
596 
597     if (AboutToDeleteCallback)
598       AboutToDeleteCallback(I);
599 
600     // Null out all of the instruction's operands to see if any operand becomes
601     // dead as we go.
602     for (Use &OpU : I->operands()) {
603       Value *OpV = OpU.get();
604       OpU.set(nullptr);
605 
606       if (!OpV->use_empty())
607         continue;
608 
609       // If the operand is an instruction that became dead as we nulled out the
610       // operand, and if it is 'trivially' dead, delete it in a future loop
611       // iteration.
612       if (Instruction *OpI = dyn_cast<Instruction>(OpV))
613         if (isInstructionTriviallyDead(OpI, TLI))
614           DeadInsts.push_back(OpI);
615     }
616     if (MSSAU)
617       MSSAU->removeMemoryAccess(I);
618 
619     I->eraseFromParent();
620   }
621 }
622 
623 bool llvm::replaceDbgUsesWithUndef(Instruction *I) {
624   SmallVector<DbgVariableIntrinsic *, 1> DbgUsers;
625   SmallVector<DbgVariableRecord *, 1> DPUsers;
626   findDbgUsers(DbgUsers, I, &DPUsers);
627   for (auto *DII : DbgUsers)
628     DII->setKillLocation();
629   for (auto *DVR : DPUsers)
630     DVR->setKillLocation();
631   return !DbgUsers.empty() || !DPUsers.empty();
632 }
633 
634 /// areAllUsesEqual - Check whether the uses of a value are all the same.
635 /// This is similar to Instruction::hasOneUse() except this will also return
636 /// true when there are no uses or multiple uses that all refer to the same
637 /// value.
638 static bool areAllUsesEqual(Instruction *I) {
639   Value::user_iterator UI = I->user_begin();
640   Value::user_iterator UE = I->user_end();
641   if (UI == UE)
642     return true;
643 
644   User *TheUse = *UI;
645   for (++UI; UI != UE; ++UI) {
646     if (*UI != TheUse)
647       return false;
648   }
649   return true;
650 }
651 
652 /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
653 /// dead PHI node, due to being a def-use chain of single-use nodes that
654 /// either forms a cycle or is terminated by a trivially dead instruction,
655 /// delete it.  If that makes any of its operands trivially dead, delete them
656 /// too, recursively.  Return true if a change was made.
657 bool llvm::RecursivelyDeleteDeadPHINode(PHINode *PN,
658                                         const TargetLibraryInfo *TLI,
659                                         llvm::MemorySSAUpdater *MSSAU) {
660   SmallPtrSet<Instruction*, 4> Visited;
661   for (Instruction *I = PN; areAllUsesEqual(I) && !I->mayHaveSideEffects();
662        I = cast<Instruction>(*I->user_begin())) {
663     if (I->use_empty())
664       return RecursivelyDeleteTriviallyDeadInstructions(I, TLI, MSSAU);
665 
666     // If we find an instruction more than once, we're on a cycle that
667     // won't prove fruitful.
668     if (!Visited.insert(I).second) {
669       // Break the cycle and delete the instruction and its operands.
670       I->replaceAllUsesWith(PoisonValue::get(I->getType()));
671       (void)RecursivelyDeleteTriviallyDeadInstructions(I, TLI, MSSAU);
672       return true;
673     }
674   }
675   return false;
676 }
677 
678 static bool
679 simplifyAndDCEInstruction(Instruction *I,
680                           SmallSetVector<Instruction *, 16> &WorkList,
681                           const DataLayout &DL,
682                           const TargetLibraryInfo *TLI) {
683   if (isInstructionTriviallyDead(I, TLI)) {
684     salvageDebugInfo(*I);
685 
686     // Null out all of the instruction's operands to see if any operand becomes
687     // dead as we go.
688     for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
689       Value *OpV = I->getOperand(i);
690       I->setOperand(i, nullptr);
691 
692       if (!OpV->use_empty() || I == OpV)
693         continue;
694 
695       // If the operand is an instruction that became dead as we nulled out the
696       // operand, and if it is 'trivially' dead, delete it in a future loop
697       // iteration.
698       if (Instruction *OpI = dyn_cast<Instruction>(OpV))
699         if (isInstructionTriviallyDead(OpI, TLI))
700           WorkList.insert(OpI);
701     }
702 
703     I->eraseFromParent();
704 
705     return true;
706   }
707 
708   if (Value *SimpleV = simplifyInstruction(I, DL)) {
709     // Add the users to the worklist. CAREFUL: an instruction can use itself,
710     // in the case of a phi node.
711     for (User *U : I->users()) {
712       if (U != I) {
713         WorkList.insert(cast<Instruction>(U));
714       }
715     }
716 
717     // Replace the instruction with its simplified value.
718     bool Changed = false;
719     if (!I->use_empty()) {
720       I->replaceAllUsesWith(SimpleV);
721       Changed = true;
722     }
723     if (isInstructionTriviallyDead(I, TLI)) {
724       I->eraseFromParent();
725       Changed = true;
726     }
727     return Changed;
728   }
729   return false;
730 }
731 
732 /// SimplifyInstructionsInBlock - Scan the specified basic block and try to
733 /// simplify any instructions in it and recursively delete dead instructions.
734 ///
735 /// This returns true if it changed the code, note that it can delete
736 /// instructions in other blocks as well in this block.
737 bool llvm::SimplifyInstructionsInBlock(BasicBlock *BB,
738                                        const TargetLibraryInfo *TLI) {
739   bool MadeChange = false;
740   const DataLayout &DL = BB->getDataLayout();
741 
742 #ifndef NDEBUG
743   // In debug builds, ensure that the terminator of the block is never replaced
744   // or deleted by these simplifications. The idea of simplification is that it
745   // cannot introduce new instructions, and there is no way to replace the
746   // terminator of a block without introducing a new instruction.
747   AssertingVH<Instruction> TerminatorVH(&BB->back());
748 #endif
749 
750   SmallSetVector<Instruction *, 16> WorkList;
751   // Iterate over the original function, only adding insts to the worklist
752   // if they actually need to be revisited. This avoids having to pre-init
753   // the worklist with the entire function's worth of instructions.
754   for (BasicBlock::iterator BI = BB->begin(), E = std::prev(BB->end());
755        BI != E;) {
756     assert(!BI->isTerminator());
757     Instruction *I = &*BI;
758     ++BI;
759 
760     // We're visiting this instruction now, so make sure it's not in the
761     // worklist from an earlier visit.
762     if (!WorkList.count(I))
763       MadeChange |= simplifyAndDCEInstruction(I, WorkList, DL, TLI);
764   }
765 
766   while (!WorkList.empty()) {
767     Instruction *I = WorkList.pop_back_val();
768     MadeChange |= simplifyAndDCEInstruction(I, WorkList, DL, TLI);
769   }
770   return MadeChange;
771 }
772 
773 //===----------------------------------------------------------------------===//
774 //  Control Flow Graph Restructuring.
775 //
776 
777 void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB,
778                                        DomTreeUpdater *DTU) {
779 
780   // If BB has single-entry PHI nodes, fold them.
781   while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) {
782     Value *NewVal = PN->getIncomingValue(0);
783     // Replace self referencing PHI with poison, it must be dead.
784     if (NewVal == PN) NewVal = PoisonValue::get(PN->getType());
785     PN->replaceAllUsesWith(NewVal);
786     PN->eraseFromParent();
787   }
788 
789   BasicBlock *PredBB = DestBB->getSinglePredecessor();
790   assert(PredBB && "Block doesn't have a single predecessor!");
791 
792   bool ReplaceEntryBB = PredBB->isEntryBlock();
793 
794   // DTU updates: Collect all the edges that enter
795   // PredBB. These dominator edges will be redirected to DestBB.
796   SmallVector<DominatorTree::UpdateType, 32> Updates;
797 
798   if (DTU) {
799     // To avoid processing the same predecessor more than once.
800     SmallPtrSet<BasicBlock *, 2> SeenPreds;
801     Updates.reserve(Updates.size() + 2 * pred_size(PredBB) + 1);
802     for (BasicBlock *PredOfPredBB : predecessors(PredBB))
803       // This predecessor of PredBB may already have DestBB as a successor.
804       if (PredOfPredBB != PredBB)
805         if (SeenPreds.insert(PredOfPredBB).second)
806           Updates.push_back({DominatorTree::Insert, PredOfPredBB, DestBB});
807     SeenPreds.clear();
808     for (BasicBlock *PredOfPredBB : predecessors(PredBB))
809       if (SeenPreds.insert(PredOfPredBB).second)
810         Updates.push_back({DominatorTree::Delete, PredOfPredBB, PredBB});
811     Updates.push_back({DominatorTree::Delete, PredBB, DestBB});
812   }
813 
814   // Zap anything that took the address of DestBB.  Not doing this will give the
815   // address an invalid value.
816   if (DestBB->hasAddressTaken()) {
817     BlockAddress *BA = BlockAddress::get(DestBB);
818     Constant *Replacement =
819       ConstantInt::get(Type::getInt32Ty(BA->getContext()), 1);
820     BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement,
821                                                      BA->getType()));
822     BA->destroyConstant();
823   }
824 
825   // Anything that branched to PredBB now branches to DestBB.
826   PredBB->replaceAllUsesWith(DestBB);
827 
828   // Splice all the instructions from PredBB to DestBB.
829   PredBB->getTerminator()->eraseFromParent();
830   DestBB->splice(DestBB->begin(), PredBB);
831   new UnreachableInst(PredBB->getContext(), PredBB);
832 
833   // If the PredBB is the entry block of the function, move DestBB up to
834   // become the entry block after we erase PredBB.
835   if (ReplaceEntryBB)
836     DestBB->moveAfter(PredBB);
837 
838   if (DTU) {
839     assert(PredBB->size() == 1 &&
840            isa<UnreachableInst>(PredBB->getTerminator()) &&
841            "The successor list of PredBB isn't empty before "
842            "applying corresponding DTU updates.");
843     DTU->applyUpdatesPermissive(Updates);
844     DTU->deleteBB(PredBB);
845     // Recalculation of DomTree is needed when updating a forward DomTree and
846     // the Entry BB is replaced.
847     if (ReplaceEntryBB && DTU->hasDomTree()) {
848       // The entry block was removed and there is no external interface for
849       // the dominator tree to be notified of this change. In this corner-case
850       // we recalculate the entire tree.
851       DTU->recalculate(*(DestBB->getParent()));
852     }
853   }
854 
855   else {
856     PredBB->eraseFromParent(); // Nuke BB if DTU is nullptr.
857   }
858 }
859 
860 /// Return true if we can choose one of these values to use in place of the
861 /// other. Note that we will always choose the non-undef value to keep.
862 static bool CanMergeValues(Value *First, Value *Second) {
863   return First == Second || isa<UndefValue>(First) || isa<UndefValue>(Second);
864 }
865 
866 /// Return true if we can fold BB, an almost-empty BB ending in an unconditional
867 /// branch to Succ, into Succ.
868 ///
869 /// Assumption: Succ is the single successor for BB.
870 static bool
871 CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ,
872                                 const SmallPtrSetImpl<BasicBlock *> &BBPreds) {
873   assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");
874 
875   LLVM_DEBUG(dbgs() << "Looking to fold " << BB->getName() << " into "
876                     << Succ->getName() << "\n");
877   // Shortcut, if there is only a single predecessor it must be BB and merging
878   // is always safe
879   if (Succ->getSinglePredecessor())
880     return true;
881 
882   // Look at all the phi nodes in Succ, to see if they present a conflict when
883   // merging these blocks
884   for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
885     PHINode *PN = cast<PHINode>(I);
886 
887     // If the incoming value from BB is again a PHINode in
888     // BB which has the same incoming value for *PI as PN does, we can
889     // merge the phi nodes and then the blocks can still be merged
890     PHINode *BBPN = dyn_cast<PHINode>(PN->getIncomingValueForBlock(BB));
891     if (BBPN && BBPN->getParent() == BB) {
892       for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) {
893         BasicBlock *IBB = PN->getIncomingBlock(PI);
894         if (BBPreds.count(IBB) &&
895             !CanMergeValues(BBPN->getIncomingValueForBlock(IBB),
896                             PN->getIncomingValue(PI))) {
897           LLVM_DEBUG(dbgs()
898                      << "Can't fold, phi node " << PN->getName() << " in "
899                      << Succ->getName() << " is conflicting with "
900                      << BBPN->getName() << " with regard to common predecessor "
901                      << IBB->getName() << "\n");
902           return false;
903         }
904       }
905     } else {
906       Value* Val = PN->getIncomingValueForBlock(BB);
907       for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) {
908         // See if the incoming value for the common predecessor is equal to the
909         // one for BB, in which case this phi node will not prevent the merging
910         // of the block.
911         BasicBlock *IBB = PN->getIncomingBlock(PI);
912         if (BBPreds.count(IBB) &&
913             !CanMergeValues(Val, PN->getIncomingValue(PI))) {
914           LLVM_DEBUG(dbgs() << "Can't fold, phi node " << PN->getName()
915                             << " in " << Succ->getName()
916                             << " is conflicting with regard to common "
917                             << "predecessor " << IBB->getName() << "\n");
918           return false;
919         }
920       }
921     }
922   }
923 
924   return true;
925 }
926 
927 using PredBlockVector = SmallVector<BasicBlock *, 16>;
928 using IncomingValueMap = SmallDenseMap<BasicBlock *, Value *, 16>;
929 
930 /// Determines the value to use as the phi node input for a block.
931 ///
932 /// Select between \p OldVal any value that we know flows from \p BB
933 /// to a particular phi on the basis of which one (if either) is not
934 /// undef. Update IncomingValues based on the selected value.
935 ///
936 /// \param OldVal The value we are considering selecting.
937 /// \param BB The block that the value flows in from.
938 /// \param IncomingValues A map from block-to-value for other phi inputs
939 /// that we have examined.
940 ///
941 /// \returns the selected value.
942 static Value *selectIncomingValueForBlock(Value *OldVal, BasicBlock *BB,
943                                           IncomingValueMap &IncomingValues) {
944   if (!isa<UndefValue>(OldVal)) {
945     assert((!IncomingValues.count(BB) ||
946             IncomingValues.find(BB)->second == OldVal) &&
947            "Expected OldVal to match incoming value from BB!");
948 
949     IncomingValues.insert(std::make_pair(BB, OldVal));
950     return OldVal;
951   }
952 
953   IncomingValueMap::const_iterator It = IncomingValues.find(BB);
954   if (It != IncomingValues.end()) return It->second;
955 
956   return OldVal;
957 }
958 
959 /// Create a map from block to value for the operands of a
960 /// given phi.
961 ///
962 /// Create a map from block to value for each non-undef value flowing
963 /// into \p PN.
964 ///
965 /// \param PN The phi we are collecting the map for.
966 /// \param IncomingValues [out] The map from block to value for this phi.
967 static void gatherIncomingValuesToPhi(PHINode *PN,
968                                       IncomingValueMap &IncomingValues) {
969   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
970     BasicBlock *BB = PN->getIncomingBlock(i);
971     Value *V = PN->getIncomingValue(i);
972 
973     if (!isa<UndefValue>(V))
974       IncomingValues.insert(std::make_pair(BB, V));
975   }
976 }
977 
978 /// Replace the incoming undef values to a phi with the values
979 /// from a block-to-value map.
980 ///
981 /// \param PN The phi we are replacing the undefs in.
982 /// \param IncomingValues A map from block to value.
983 static void replaceUndefValuesInPhi(PHINode *PN,
984                                     const IncomingValueMap &IncomingValues) {
985   SmallVector<unsigned> TrueUndefOps;
986   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
987     Value *V = PN->getIncomingValue(i);
988 
989     if (!isa<UndefValue>(V)) continue;
990 
991     BasicBlock *BB = PN->getIncomingBlock(i);
992     IncomingValueMap::const_iterator It = IncomingValues.find(BB);
993 
994     // Keep track of undef/poison incoming values. Those must match, so we fix
995     // them up below if needed.
996     // Note: this is conservatively correct, but we could try harder and group
997     // the undef values per incoming basic block.
998     if (It == IncomingValues.end()) {
999       TrueUndefOps.push_back(i);
1000       continue;
1001     }
1002 
1003     // There is a defined value for this incoming block, so map this undef
1004     // incoming value to the defined value.
1005     PN->setIncomingValue(i, It->second);
1006   }
1007 
1008   // If there are both undef and poison values incoming, then convert those
1009   // values to undef. It is invalid to have different values for the same
1010   // incoming block.
1011   unsigned PoisonCount = count_if(TrueUndefOps, [&](unsigned i) {
1012     return isa<PoisonValue>(PN->getIncomingValue(i));
1013   });
1014   if (PoisonCount != 0 && PoisonCount != TrueUndefOps.size()) {
1015     for (unsigned i : TrueUndefOps)
1016       PN->setIncomingValue(i, UndefValue::get(PN->getType()));
1017   }
1018 }
1019 
1020 // Only when they shares a single common predecessor, return true.
1021 // Only handles cases when BB can't be merged while its predecessors can be
1022 // redirected.
1023 static bool
1024 CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ,
1025                                 const SmallPtrSetImpl<BasicBlock *> &BBPreds,
1026                                 BasicBlock *&CommonPred) {
1027 
1028   // There must be phis in BB, otherwise BB will be merged into Succ directly
1029   if (BB->phis().empty() || Succ->phis().empty())
1030     return false;
1031 
1032   // BB must have predecessors not shared that can be redirected to Succ
1033   if (!BB->hasNPredecessorsOrMore(2))
1034     return false;
1035 
1036   if (any_of(BBPreds, [](const BasicBlock *Pred) {
1037         return isa<IndirectBrInst>(Pred->getTerminator());
1038       }))
1039     return false;
1040 
1041   // Get the single common predecessor of both BB and Succ. Return false
1042   // when there are more than one common predecessors.
1043   for (BasicBlock *SuccPred : predecessors(Succ)) {
1044     if (BBPreds.count(SuccPred)) {
1045       if (CommonPred)
1046         return false;
1047       CommonPred = SuccPred;
1048     }
1049   }
1050 
1051   return true;
1052 }
1053 
1054 /// Check whether removing \p BB will make the phis in its \p Succ have too
1055 /// many incoming entries. This function does not check whether \p BB is
1056 /// foldable or not.
1057 static bool introduceTooManyPhiEntries(BasicBlock *BB, BasicBlock *Succ) {
1058   // If BB only has one predecessor, then removing it will not introduce more
1059   // incoming edges for phis.
1060   if (BB->hasNPredecessors(1))
1061     return false;
1062   unsigned NumPreds = pred_size(BB);
1063   unsigned NumChangedPhi = 0;
1064   for (auto &Phi : Succ->phis()) {
1065     // If the incoming value is a phi and the phi is defined in BB,
1066     // then removing BB will not increase the total phi entries of the ir.
1067     if (auto *IncomingPhi = dyn_cast<PHINode>(Phi.getIncomingValueForBlock(BB)))
1068       if (IncomingPhi->getParent() == BB)
1069         continue;
1070     // Otherwise, we need to add entries to the phi
1071     NumChangedPhi++;
1072   }
1073   // For every phi that needs to be changed, (NumPreds - 1) new entries will be
1074   // added. If the total increase in phi entries exceeds
1075   // MaxPhiEntriesIncreaseAfterRemovingEmptyBlock, it will be considered as
1076   // introducing too many new phi entries.
1077   return (NumPreds - 1) * NumChangedPhi >
1078          MaxPhiEntriesIncreaseAfterRemovingEmptyBlock;
1079 }
1080 
1081 /// Replace a value flowing from a block to a phi with
1082 /// potentially multiple instances of that value flowing from the
1083 /// block's predecessors to the phi.
1084 ///
1085 /// \param BB The block with the value flowing into the phi.
1086 /// \param BBPreds The predecessors of BB.
1087 /// \param PN The phi that we are updating.
1088 /// \param CommonPred The common predecessor of BB and PN's BasicBlock
1089 static void redirectValuesFromPredecessorsToPhi(BasicBlock *BB,
1090                                                 const PredBlockVector &BBPreds,
1091                                                 PHINode *PN,
1092                                                 BasicBlock *CommonPred) {
1093   Value *OldVal = PN->removeIncomingValue(BB, false);
1094   assert(OldVal && "No entry in PHI for Pred BB!");
1095 
1096   IncomingValueMap IncomingValues;
1097 
1098   // We are merging two blocks - BB, and the block containing PN - and
1099   // as a result we need to redirect edges from the predecessors of BB
1100   // to go to the block containing PN, and update PN
1101   // accordingly. Since we allow merging blocks in the case where the
1102   // predecessor and successor blocks both share some predecessors,
1103   // and where some of those common predecessors might have undef
1104   // values flowing into PN, we want to rewrite those values to be
1105   // consistent with the non-undef values.
1106 
1107   gatherIncomingValuesToPhi(PN, IncomingValues);
1108 
1109   // If this incoming value is one of the PHI nodes in BB, the new entries
1110   // in the PHI node are the entries from the old PHI.
1111   if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) {
1112     PHINode *OldValPN = cast<PHINode>(OldVal);
1113     for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i) {
1114       // Note that, since we are merging phi nodes and BB and Succ might
1115       // have common predecessors, we could end up with a phi node with
1116       // identical incoming branches. This will be cleaned up later (and
1117       // will trigger asserts if we try to clean it up now, without also
1118       // simplifying the corresponding conditional branch).
1119       BasicBlock *PredBB = OldValPN->getIncomingBlock(i);
1120 
1121       if (PredBB == CommonPred)
1122         continue;
1123 
1124       Value *PredVal = OldValPN->getIncomingValue(i);
1125       Value *Selected =
1126           selectIncomingValueForBlock(PredVal, PredBB, IncomingValues);
1127 
1128       // And add a new incoming value for this predecessor for the
1129       // newly retargeted branch.
1130       PN->addIncoming(Selected, PredBB);
1131     }
1132     if (CommonPred)
1133       PN->addIncoming(OldValPN->getIncomingValueForBlock(CommonPred), BB);
1134 
1135   } else {
1136     for (BasicBlock *PredBB : BBPreds) {
1137       // Update existing incoming values in PN for this
1138       // predecessor of BB.
1139       if (PredBB == CommonPred)
1140         continue;
1141 
1142       Value *Selected =
1143           selectIncomingValueForBlock(OldVal, PredBB, IncomingValues);
1144 
1145       // And add a new incoming value for this predecessor for the
1146       // newly retargeted branch.
1147       PN->addIncoming(Selected, PredBB);
1148     }
1149     if (CommonPred)
1150       PN->addIncoming(OldVal, BB);
1151   }
1152 
1153   replaceUndefValuesInPhi(PN, IncomingValues);
1154 }
1155 
1156 bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
1157                                                    DomTreeUpdater *DTU) {
1158   assert(BB != &BB->getParent()->getEntryBlock() &&
1159          "TryToSimplifyUncondBranchFromEmptyBlock called on entry block!");
1160 
1161   // We can't simplify infinite loops.
1162   BasicBlock *Succ = cast<BranchInst>(BB->getTerminator())->getSuccessor(0);
1163   if (BB == Succ)
1164     return false;
1165 
1166   SmallPtrSet<BasicBlock *, 16> BBPreds(pred_begin(BB), pred_end(BB));
1167 
1168   // The single common predecessor of BB and Succ when BB cannot be killed
1169   BasicBlock *CommonPred = nullptr;
1170 
1171   bool BBKillable = CanPropagatePredecessorsForPHIs(BB, Succ, BBPreds);
1172 
1173   // Even if we can not fold BB into Succ, we may be able to redirect the
1174   // predecessors of BB to Succ.
1175   bool BBPhisMergeable = BBKillable || CanRedirectPredsOfEmptyBBToSucc(
1176                                            BB, Succ, BBPreds, CommonPred);
1177 
1178   if ((!BBKillable && !BBPhisMergeable) || introduceTooManyPhiEntries(BB, Succ))
1179     return false;
1180 
1181   // Check to see if merging these blocks/phis would cause conflicts for any of
1182   // the phi nodes in BB or Succ. If not, we can safely merge.
1183 
1184   // Check for cases where Succ has multiple predecessors and a PHI node in BB
1185   // has uses which will not disappear when the PHI nodes are merged.  It is
1186   // possible to handle such cases, but difficult: it requires checking whether
1187   // BB dominates Succ, which is non-trivial to calculate in the case where
1188   // Succ has multiple predecessors.  Also, it requires checking whether
1189   // constructing the necessary self-referential PHI node doesn't introduce any
1190   // conflicts; this isn't too difficult, but the previous code for doing this
1191   // was incorrect.
1192   //
1193   // Note that if this check finds a live use, BB dominates Succ, so BB is
1194   // something like a loop pre-header (or rarely, a part of an irreducible CFG);
1195   // folding the branch isn't profitable in that case anyway.
1196   if (!Succ->getSinglePredecessor()) {
1197     BasicBlock::iterator BBI = BB->begin();
1198     while (isa<PHINode>(*BBI)) {
1199       for (Use &U : BBI->uses()) {
1200         if (PHINode* PN = dyn_cast<PHINode>(U.getUser())) {
1201           if (PN->getIncomingBlock(U) != BB)
1202             return false;
1203         } else {
1204           return false;
1205         }
1206       }
1207       ++BBI;
1208     }
1209   }
1210 
1211   if (BBPhisMergeable && CommonPred)
1212     LLVM_DEBUG(dbgs() << "Found Common Predecessor between: " << BB->getName()
1213                       << " and " << Succ->getName() << " : "
1214                       << CommonPred->getName() << "\n");
1215 
1216   // 'BB' and 'BB->Pred' are loop latches, bail out to presrve inner loop
1217   // metadata.
1218   //
1219   // FIXME: This is a stop-gap solution to preserve inner-loop metadata given
1220   // current status (that loop metadata is implemented as metadata attached to
1221   // the branch instruction in the loop latch block). To quote from review
1222   // comments, "the current representation of loop metadata (using a loop latch
1223   // terminator attachment) is known to be fundamentally broken. Loop latches
1224   // are not uniquely associated with loops (both in that a latch can be part of
1225   // multiple loops and a loop may have multiple latches). Loop headers are. The
1226   // solution to this problem is also known: Add support for basic block
1227   // metadata, and attach loop metadata to the loop header."
1228   //
1229   // Why bail out:
1230   // In this case, we expect 'BB' is the latch for outer-loop and 'BB->Pred' is
1231   // the latch for inner-loop (see reason below), so bail out to prerserve
1232   // inner-loop metadata rather than eliminating 'BB' and attaching its metadata
1233   // to this inner-loop.
1234   // - The reason we believe 'BB' and 'BB->Pred' have different inner-most
1235   // loops: assuming 'BB' and 'BB->Pred' are from the same inner-most loop L,
1236   // then 'BB' is the header and latch of 'L' and thereby 'L' must consist of
1237   // one self-looping basic block, which is contradictory with the assumption.
1238   //
1239   // To illustrate how inner-loop metadata is dropped:
1240   //
1241   // CFG Before
1242   //
1243   // BB is while.cond.exit, attached with loop metdata md2.
1244   // BB->Pred is for.body, attached with loop metadata md1.
1245   //
1246   //      entry
1247   //        |
1248   //        v
1249   // ---> while.cond   ------------->  while.end
1250   // |       |
1251   // |       v
1252   // |   while.body
1253   // |       |
1254   // |       v
1255   // |    for.body <---- (md1)
1256   // |       |  |______|
1257   // |       v
1258   // |    while.cond.exit (md2)
1259   // |       |
1260   // |_______|
1261   //
1262   // CFG After
1263   //
1264   // while.cond1 is the merge of while.cond.exit and while.cond above.
1265   // for.body is attached with md2, and md1 is dropped.
1266   // If LoopSimplify runs later (as a part of loop pass), it could create
1267   // dedicated exits for inner-loop (essentially adding `while.cond.exit`
1268   // back), but won't it won't see 'md1' nor restore it for the inner-loop.
1269   //
1270   //       entry
1271   //         |
1272   //         v
1273   // ---> while.cond1  ------------->  while.end
1274   // |       |
1275   // |       v
1276   // |   while.body
1277   // |       |
1278   // |       v
1279   // |    for.body <---- (md2)
1280   // |_______|  |______|
1281   if (Instruction *TI = BB->getTerminator())
1282     if (TI->hasMetadata(LLVMContext::MD_loop))
1283       for (BasicBlock *Pred : predecessors(BB))
1284         if (Instruction *PredTI = Pred->getTerminator())
1285           if (PredTI->hasMetadata(LLVMContext::MD_loop))
1286             return false;
1287 
1288   if (BBKillable)
1289     LLVM_DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB);
1290   else if (BBPhisMergeable)
1291     LLVM_DEBUG(dbgs() << "Merge Phis in Trivial BB: \n" << *BB);
1292 
1293   SmallVector<DominatorTree::UpdateType, 32> Updates;
1294 
1295   if (DTU) {
1296     // To avoid processing the same predecessor more than once.
1297     SmallPtrSet<BasicBlock *, 8> SeenPreds;
1298     // All predecessors of BB (except the common predecessor) will be moved to
1299     // Succ.
1300     Updates.reserve(Updates.size() + 2 * pred_size(BB) + 1);
1301     SmallPtrSet<BasicBlock *, 16> SuccPreds(pred_begin(Succ), pred_end(Succ));
1302     for (auto *PredOfBB : predecessors(BB)) {
1303       // Do not modify those common predecessors of BB and Succ
1304       if (!SuccPreds.contains(PredOfBB))
1305         if (SeenPreds.insert(PredOfBB).second)
1306           Updates.push_back({DominatorTree::Insert, PredOfBB, Succ});
1307     }
1308 
1309     SeenPreds.clear();
1310 
1311     for (auto *PredOfBB : predecessors(BB))
1312       // When BB cannot be killed, do not remove the edge between BB and
1313       // CommonPred.
1314       if (SeenPreds.insert(PredOfBB).second && PredOfBB != CommonPred)
1315         Updates.push_back({DominatorTree::Delete, PredOfBB, BB});
1316 
1317     if (BBKillable)
1318       Updates.push_back({DominatorTree::Delete, BB, Succ});
1319   }
1320 
1321   if (isa<PHINode>(Succ->begin())) {
1322     // If there is more than one pred of succ, and there are PHI nodes in
1323     // the successor, then we need to add incoming edges for the PHI nodes
1324     //
1325     const PredBlockVector BBPreds(predecessors(BB));
1326 
1327     // Loop over all of the PHI nodes in the successor of BB.
1328     for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
1329       PHINode *PN = cast<PHINode>(I);
1330       redirectValuesFromPredecessorsToPhi(BB, BBPreds, PN, CommonPred);
1331     }
1332   }
1333 
1334   if (Succ->getSinglePredecessor()) {
1335     // BB is the only predecessor of Succ, so Succ will end up with exactly
1336     // the same predecessors BB had.
1337     // Copy over any phi, debug or lifetime instruction.
1338     BB->getTerminator()->eraseFromParent();
1339     Succ->splice(Succ->getFirstNonPHIIt(), BB);
1340   } else {
1341     while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) {
1342       // We explicitly check for such uses for merging phis.
1343       assert(PN->use_empty() && "There shouldn't be any uses here!");
1344       PN->eraseFromParent();
1345     }
1346   }
1347 
1348   // If the unconditional branch we replaced contains llvm.loop metadata, we
1349   // add the metadata to the branch instructions in the predecessors.
1350   if (Instruction *TI = BB->getTerminator())
1351     if (MDNode *LoopMD = TI->getMetadata(LLVMContext::MD_loop))
1352       for (BasicBlock *Pred : predecessors(BB))
1353         Pred->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopMD);
1354 
1355   if (BBKillable) {
1356     // Everything that jumped to BB now goes to Succ.
1357     BB->replaceAllUsesWith(Succ);
1358 
1359     if (!Succ->hasName())
1360       Succ->takeName(BB);
1361 
1362     // Clear the successor list of BB to match updates applying to DTU later.
1363     if (BB->getTerminator())
1364       BB->back().eraseFromParent();
1365 
1366     new UnreachableInst(BB->getContext(), BB);
1367     assert(succ_empty(BB) && "The successor list of BB isn't empty before "
1368                              "applying corresponding DTU updates.");
1369   } else if (BBPhisMergeable) {
1370     //  Everything except CommonPred that jumped to BB now goes to Succ.
1371     BB->replaceUsesWithIf(Succ, [BBPreds, CommonPred](Use &U) -> bool {
1372       if (Instruction *UseInst = dyn_cast<Instruction>(U.getUser()))
1373         return UseInst->getParent() != CommonPred &&
1374                BBPreds.contains(UseInst->getParent());
1375       return false;
1376     });
1377   }
1378 
1379   if (DTU)
1380     DTU->applyUpdates(Updates);
1381 
1382   if (BBKillable)
1383     DeleteDeadBlock(BB, DTU);
1384 
1385   return true;
1386 }
1387 
1388 static bool
1389 EliminateDuplicatePHINodesNaiveImpl(BasicBlock *BB,
1390                                     SmallPtrSetImpl<PHINode *> &ToRemove) {
1391   // This implementation doesn't currently consider undef operands
1392   // specially. Theoretically, two phis which are identical except for
1393   // one having an undef where the other doesn't could be collapsed.
1394 
1395   bool Changed = false;
1396 
1397   // Examine each PHI.
1398   // Note that increment of I must *NOT* be in the iteration_expression, since
1399   // we don't want to immediately advance when we restart from the beginning.
1400   for (auto I = BB->begin(); PHINode *PN = dyn_cast<PHINode>(I);) {
1401     ++I;
1402     // Is there an identical PHI node in this basic block?
1403     // Note that we only look in the upper square's triangle,
1404     // we already checked that the lower triangle PHI's aren't identical.
1405     for (auto J = I; PHINode *DuplicatePN = dyn_cast<PHINode>(J); ++J) {
1406       if (ToRemove.contains(DuplicatePN))
1407         continue;
1408       if (!DuplicatePN->isIdenticalToWhenDefined(PN))
1409         continue;
1410       // A duplicate. Replace this PHI with the base PHI.
1411       ++NumPHICSEs;
1412       DuplicatePN->replaceAllUsesWith(PN);
1413       ToRemove.insert(DuplicatePN);
1414       Changed = true;
1415 
1416       // The RAUW can change PHIs that we already visited.
1417       I = BB->begin();
1418       break; // Start over from the beginning.
1419     }
1420   }
1421   return Changed;
1422 }
1423 
1424 static bool
1425 EliminateDuplicatePHINodesSetBasedImpl(BasicBlock *BB,
1426                                        SmallPtrSetImpl<PHINode *> &ToRemove) {
1427   // This implementation doesn't currently consider undef operands
1428   // specially. Theoretically, two phis which are identical except for
1429   // one having an undef where the other doesn't could be collapsed.
1430 
1431   struct PHIDenseMapInfo {
1432     static PHINode *getEmptyKey() {
1433       return DenseMapInfo<PHINode *>::getEmptyKey();
1434     }
1435 
1436     static PHINode *getTombstoneKey() {
1437       return DenseMapInfo<PHINode *>::getTombstoneKey();
1438     }
1439 
1440     static bool isSentinel(PHINode *PN) {
1441       return PN == getEmptyKey() || PN == getTombstoneKey();
1442     }
1443 
1444     // WARNING: this logic must be kept in sync with
1445     //          Instruction::isIdenticalToWhenDefined()!
1446     static unsigned getHashValueImpl(PHINode *PN) {
1447       // Compute a hash value on the operands. Instcombine will likely have
1448       // sorted them, which helps expose duplicates, but we have to check all
1449       // the operands to be safe in case instcombine hasn't run.
1450       return static_cast<unsigned>(hash_combine(
1451           hash_combine_range(PN->value_op_begin(), PN->value_op_end()),
1452           hash_combine_range(PN->block_begin(), PN->block_end())));
1453     }
1454 
1455     static unsigned getHashValue(PHINode *PN) {
1456 #ifndef NDEBUG
1457       // If -phicse-debug-hash was specified, return a constant -- this
1458       // will force all hashing to collide, so we'll exhaustively search
1459       // the table for a match, and the assertion in isEqual will fire if
1460       // there's a bug causing equal keys to hash differently.
1461       if (PHICSEDebugHash)
1462         return 0;
1463 #endif
1464       return getHashValueImpl(PN);
1465     }
1466 
1467     static bool isEqualImpl(PHINode *LHS, PHINode *RHS) {
1468       if (isSentinel(LHS) || isSentinel(RHS))
1469         return LHS == RHS;
1470       return LHS->isIdenticalTo(RHS);
1471     }
1472 
1473     static bool isEqual(PHINode *LHS, PHINode *RHS) {
1474       // These comparisons are nontrivial, so assert that equality implies
1475       // hash equality (DenseMap demands this as an invariant).
1476       bool Result = isEqualImpl(LHS, RHS);
1477       assert(!Result || (isSentinel(LHS) && LHS == RHS) ||
1478              getHashValueImpl(LHS) == getHashValueImpl(RHS));
1479       return Result;
1480     }
1481   };
1482 
1483   // Set of unique PHINodes.
1484   DenseSet<PHINode *, PHIDenseMapInfo> PHISet;
1485   PHISet.reserve(4 * PHICSENumPHISmallSize);
1486 
1487   // Examine each PHI.
1488   bool Changed = false;
1489   for (auto I = BB->begin(); PHINode *PN = dyn_cast<PHINode>(I++);) {
1490     if (ToRemove.contains(PN))
1491       continue;
1492     auto Inserted = PHISet.insert(PN);
1493     if (!Inserted.second) {
1494       // A duplicate. Replace this PHI with its duplicate.
1495       ++NumPHICSEs;
1496       PN->replaceAllUsesWith(*Inserted.first);
1497       ToRemove.insert(PN);
1498       Changed = true;
1499 
1500       // The RAUW can change PHIs that we already visited. Start over from the
1501       // beginning.
1502       PHISet.clear();
1503       I = BB->begin();
1504     }
1505   }
1506 
1507   return Changed;
1508 }
1509 
1510 bool llvm::EliminateDuplicatePHINodes(BasicBlock *BB,
1511                                       SmallPtrSetImpl<PHINode *> &ToRemove) {
1512   if (
1513 #ifndef NDEBUG
1514       !PHICSEDebugHash &&
1515 #endif
1516       hasNItemsOrLess(BB->phis(), PHICSENumPHISmallSize))
1517     return EliminateDuplicatePHINodesNaiveImpl(BB, ToRemove);
1518   return EliminateDuplicatePHINodesSetBasedImpl(BB, ToRemove);
1519 }
1520 
1521 bool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) {
1522   SmallPtrSet<PHINode *, 8> ToRemove;
1523   bool Changed = EliminateDuplicatePHINodes(BB, ToRemove);
1524   for (PHINode *PN : ToRemove)
1525     PN->eraseFromParent();
1526   return Changed;
1527 }
1528 
1529 Align llvm::tryEnforceAlignment(Value *V, Align PrefAlign,
1530                                 const DataLayout &DL) {
1531   V = V->stripPointerCasts();
1532 
1533   if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1534     // TODO: Ideally, this function would not be called if PrefAlign is smaller
1535     // than the current alignment, as the known bits calculation should have
1536     // already taken it into account. However, this is not always the case,
1537     // as computeKnownBits() has a depth limit, while stripPointerCasts()
1538     // doesn't.
1539     Align CurrentAlign = AI->getAlign();
1540     if (PrefAlign <= CurrentAlign)
1541       return CurrentAlign;
1542 
1543     // If the preferred alignment is greater than the natural stack alignment
1544     // then don't round up. This avoids dynamic stack realignment.
1545     MaybeAlign StackAlign = DL.getStackAlignment();
1546     if (StackAlign && PrefAlign > *StackAlign)
1547       return CurrentAlign;
1548     AI->setAlignment(PrefAlign);
1549     return PrefAlign;
1550   }
1551 
1552   if (auto *GO = dyn_cast<GlobalObject>(V)) {
1553     // TODO: as above, this shouldn't be necessary.
1554     Align CurrentAlign = GO->getPointerAlignment(DL);
1555     if (PrefAlign <= CurrentAlign)
1556       return CurrentAlign;
1557 
1558     // If there is a large requested alignment and we can, bump up the alignment
1559     // of the global.  If the memory we set aside for the global may not be the
1560     // memory used by the final program then it is impossible for us to reliably
1561     // enforce the preferred alignment.
1562     if (!GO->canIncreaseAlignment())
1563       return CurrentAlign;
1564 
1565     if (GO->isThreadLocal()) {
1566       unsigned MaxTLSAlign = GO->getParent()->getMaxTLSAlignment() / CHAR_BIT;
1567       if (MaxTLSAlign && PrefAlign > Align(MaxTLSAlign))
1568         PrefAlign = Align(MaxTLSAlign);
1569     }
1570 
1571     GO->setAlignment(PrefAlign);
1572     return PrefAlign;
1573   }
1574 
1575   return Align(1);
1576 }
1577 
1578 Align llvm::getOrEnforceKnownAlignment(Value *V, MaybeAlign PrefAlign,
1579                                        const DataLayout &DL,
1580                                        const Instruction *CxtI,
1581                                        AssumptionCache *AC,
1582                                        const DominatorTree *DT) {
1583   assert(V->getType()->isPointerTy() &&
1584          "getOrEnforceKnownAlignment expects a pointer!");
1585 
1586   KnownBits Known = computeKnownBits(V, DL, 0, AC, CxtI, DT);
1587   unsigned TrailZ = Known.countMinTrailingZeros();
1588 
1589   // Avoid trouble with ridiculously large TrailZ values, such as
1590   // those computed from a null pointer.
1591   // LLVM doesn't support alignments larger than (1 << MaxAlignmentExponent).
1592   TrailZ = std::min(TrailZ, +Value::MaxAlignmentExponent);
1593 
1594   Align Alignment = Align(1ull << std::min(Known.getBitWidth() - 1, TrailZ));
1595 
1596   if (PrefAlign && *PrefAlign > Alignment)
1597     Alignment = std::max(Alignment, tryEnforceAlignment(V, *PrefAlign, DL));
1598 
1599   // We don't need to make any adjustment.
1600   return Alignment;
1601 }
1602 
1603 ///===---------------------------------------------------------------------===//
1604 ///  Dbg Intrinsic utilities
1605 ///
1606 
1607 /// See if there is a dbg.value intrinsic for DIVar for the PHI node.
1608 static bool PhiHasDebugValue(DILocalVariable *DIVar,
1609                              DIExpression *DIExpr,
1610                              PHINode *APN) {
1611   // Since we can't guarantee that the original dbg.declare intrinsic
1612   // is removed by LowerDbgDeclare(), we need to make sure that we are
1613   // not inserting the same dbg.value intrinsic over and over.
1614   SmallVector<DbgValueInst *, 1> DbgValues;
1615   SmallVector<DbgVariableRecord *, 1> DbgVariableRecords;
1616   findDbgValues(DbgValues, APN, &DbgVariableRecords);
1617   for (auto *DVI : DbgValues) {
1618     assert(is_contained(DVI->getValues(), APN));
1619     if ((DVI->getVariable() == DIVar) && (DVI->getExpression() == DIExpr))
1620       return true;
1621   }
1622   for (auto *DVR : DbgVariableRecords) {
1623     assert(is_contained(DVR->location_ops(), APN));
1624     if ((DVR->getVariable() == DIVar) && (DVR->getExpression() == DIExpr))
1625       return true;
1626   }
1627   return false;
1628 }
1629 
1630 /// Check if the alloc size of \p ValTy is large enough to cover the variable
1631 /// (or fragment of the variable) described by \p DII.
1632 ///
1633 /// This is primarily intended as a helper for the different
1634 /// ConvertDebugDeclareToDebugValue functions. The dbg.declare that is converted
1635 /// describes an alloca'd variable, so we need to use the alloc size of the
1636 /// value when doing the comparison. E.g. an i1 value will be identified as
1637 /// covering an n-bit fragment, if the store size of i1 is at least n bits.
1638 static bool valueCoversEntireFragment(Type *ValTy, DbgVariableIntrinsic *DII) {
1639   const DataLayout &DL = DII->getDataLayout();
1640   TypeSize ValueSize = DL.getTypeAllocSizeInBits(ValTy);
1641   if (std::optional<uint64_t> FragmentSize =
1642           DII->getExpression()->getActiveBits(DII->getVariable()))
1643     return TypeSize::isKnownGE(ValueSize, TypeSize::getFixed(*FragmentSize));
1644 
1645   // We can't always calculate the size of the DI variable (e.g. if it is a
1646   // VLA). Try to use the size of the alloca that the dbg intrinsic describes
1647   // instead.
1648   if (DII->isAddressOfVariable()) {
1649     // DII should have exactly 1 location when it is an address.
1650     assert(DII->getNumVariableLocationOps() == 1 &&
1651            "address of variable must have exactly 1 location operand.");
1652     if (auto *AI =
1653             dyn_cast_or_null<AllocaInst>(DII->getVariableLocationOp(0))) {
1654       if (std::optional<TypeSize> FragmentSize =
1655               AI->getAllocationSizeInBits(DL)) {
1656         return TypeSize::isKnownGE(ValueSize, *FragmentSize);
1657       }
1658     }
1659   }
1660   // Could not determine size of variable. Conservatively return false.
1661   return false;
1662 }
1663 // RemoveDIs: duplicate implementation of the above, using DbgVariableRecords,
1664 // the replacement for dbg.values.
1665 static bool valueCoversEntireFragment(Type *ValTy, DbgVariableRecord *DVR) {
1666   const DataLayout &DL = DVR->getModule()->getDataLayout();
1667   TypeSize ValueSize = DL.getTypeAllocSizeInBits(ValTy);
1668   if (std::optional<uint64_t> FragmentSize =
1669           DVR->getExpression()->getActiveBits(DVR->getVariable()))
1670     return TypeSize::isKnownGE(ValueSize, TypeSize::getFixed(*FragmentSize));
1671 
1672   // We can't always calculate the size of the DI variable (e.g. if it is a
1673   // VLA). Try to use the size of the alloca that the dbg intrinsic describes
1674   // instead.
1675   if (DVR->isAddressOfVariable()) {
1676     // DVR should have exactly 1 location when it is an address.
1677     assert(DVR->getNumVariableLocationOps() == 1 &&
1678            "address of variable must have exactly 1 location operand.");
1679     if (auto *AI =
1680             dyn_cast_or_null<AllocaInst>(DVR->getVariableLocationOp(0))) {
1681       if (std::optional<TypeSize> FragmentSize = AI->getAllocationSizeInBits(DL)) {
1682         return TypeSize::isKnownGE(ValueSize, *FragmentSize);
1683       }
1684     }
1685   }
1686   // Could not determine size of variable. Conservatively return false.
1687   return false;
1688 }
1689 
1690 static void insertDbgValueOrDbgVariableRecord(DIBuilder &Builder, Value *DV,
1691                                               DILocalVariable *DIVar,
1692                                               DIExpression *DIExpr,
1693                                               const DebugLoc &NewLoc,
1694                                               BasicBlock::iterator Instr) {
1695   if (!UseNewDbgInfoFormat) {
1696     auto DbgVal = Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, NewLoc,
1697                                                   (Instruction *)nullptr);
1698     cast<Instruction *>(DbgVal)->insertBefore(Instr);
1699   } else {
1700     // RemoveDIs: if we're using the new debug-info format, allocate a
1701     // DbgVariableRecord directly instead of a dbg.value intrinsic.
1702     ValueAsMetadata *DVAM = ValueAsMetadata::get(DV);
1703     DbgVariableRecord *DV =
1704         new DbgVariableRecord(DVAM, DIVar, DIExpr, NewLoc.get());
1705     Instr->getParent()->insertDbgRecordBefore(DV, Instr);
1706   }
1707 }
1708 
1709 static void insertDbgValueOrDbgVariableRecordAfter(
1710     DIBuilder &Builder, Value *DV, DILocalVariable *DIVar, DIExpression *DIExpr,
1711     const DebugLoc &NewLoc, BasicBlock::iterator Instr) {
1712   if (!UseNewDbgInfoFormat) {
1713     auto DbgVal = Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, NewLoc,
1714                                                   (Instruction *)nullptr);
1715     cast<Instruction *>(DbgVal)->insertAfter(&*Instr);
1716   } else {
1717     // RemoveDIs: if we're using the new debug-info format, allocate a
1718     // DbgVariableRecord directly instead of a dbg.value intrinsic.
1719     ValueAsMetadata *DVAM = ValueAsMetadata::get(DV);
1720     DbgVariableRecord *DV =
1721         new DbgVariableRecord(DVAM, DIVar, DIExpr, NewLoc.get());
1722     Instr->getParent()->insertDbgRecordAfter(DV, &*Instr);
1723   }
1724 }
1725 
1726 /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
1727 /// that has an associated llvm.dbg.declare intrinsic.
1728 void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
1729                                            StoreInst *SI, DIBuilder &Builder) {
1730   assert(DII->isAddressOfVariable() || isa<DbgAssignIntrinsic>(DII));
1731   auto *DIVar = DII->getVariable();
1732   assert(DIVar && "Missing variable");
1733   auto *DIExpr = DII->getExpression();
1734   Value *DV = SI->getValueOperand();
1735 
1736   DebugLoc NewLoc = getDebugValueLoc(DII);
1737 
1738   // If the alloca describes the variable itself, i.e. the expression in the
1739   // dbg.declare doesn't start with a dereference, we can perform the
1740   // conversion if the value covers the entire fragment of DII.
1741   // If the alloca describes the *address* of DIVar, i.e. DIExpr is
1742   // *just* a DW_OP_deref, we use DV as is for the dbg.value.
1743   // We conservatively ignore other dereferences, because the following two are
1744   // not equivalent:
1745   //     dbg.declare(alloca, ..., !Expr(deref, plus_uconstant, 2))
1746   //     dbg.value(DV, ..., !Expr(deref, plus_uconstant, 2))
1747   // The former is adding 2 to the address of the variable, whereas the latter
1748   // is adding 2 to the value of the variable. As such, we insist on just a
1749   // deref expression.
1750   bool CanConvert =
1751       DIExpr->isDeref() || (!DIExpr->startsWithDeref() &&
1752                             valueCoversEntireFragment(DV->getType(), DII));
1753   if (CanConvert) {
1754     insertDbgValueOrDbgVariableRecord(Builder, DV, DIVar, DIExpr, NewLoc,
1755                                       SI->getIterator());
1756     return;
1757   }
1758 
1759   // FIXME: If storing to a part of the variable described by the dbg.declare,
1760   // then we want to insert a dbg.value for the corresponding fragment.
1761   LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: " << *DII
1762                     << '\n');
1763   // For now, when there is a store to parts of the variable (but we do not
1764   // know which part) we insert an dbg.value intrinsic to indicate that we
1765   // know nothing about the variable's content.
1766   DV = PoisonValue::get(DV->getType());
1767   insertDbgValueOrDbgVariableRecord(Builder, DV, DIVar, DIExpr, NewLoc,
1768                                     SI->getIterator());
1769 }
1770 
1771 static DIExpression *dropInitialDeref(const DIExpression *DIExpr) {
1772   int NumEltDropped = DIExpr->getElements()[0] == dwarf::DW_OP_LLVM_arg ? 3 : 1;
1773   return DIExpression::get(DIExpr->getContext(),
1774                            DIExpr->getElements().drop_front(NumEltDropped));
1775 }
1776 
1777 void llvm::InsertDebugValueAtStoreLoc(DbgVariableIntrinsic *DII, StoreInst *SI,
1778                                       DIBuilder &Builder) {
1779   auto *DIVar = DII->getVariable();
1780   assert(DIVar && "Missing variable");
1781   auto *DIExpr = DII->getExpression();
1782   DIExpr = dropInitialDeref(DIExpr);
1783   Value *DV = SI->getValueOperand();
1784 
1785   DebugLoc NewLoc = getDebugValueLoc(DII);
1786 
1787   insertDbgValueOrDbgVariableRecord(Builder, DV, DIVar, DIExpr, NewLoc,
1788                                     SI->getIterator());
1789 }
1790 
1791 /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
1792 /// that has an associated llvm.dbg.declare intrinsic.
1793 void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
1794                                            LoadInst *LI, DIBuilder &Builder) {
1795   auto *DIVar = DII->getVariable();
1796   auto *DIExpr = DII->getExpression();
1797   assert(DIVar && "Missing variable");
1798 
1799   if (!valueCoversEntireFragment(LI->getType(), DII)) {
1800     // FIXME: If only referring to a part of the variable described by the
1801     // dbg.declare, then we want to insert a dbg.value for the corresponding
1802     // fragment.
1803     LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: "
1804                       << *DII << '\n');
1805     return;
1806   }
1807 
1808   DebugLoc NewLoc = getDebugValueLoc(DII);
1809 
1810   // We are now tracking the loaded value instead of the address. In the
1811   // future if multi-location support is added to the IR, it might be
1812   // preferable to keep tracking both the loaded value and the original
1813   // address in case the alloca can not be elided.
1814   insertDbgValueOrDbgVariableRecordAfter(Builder, LI, DIVar, DIExpr, NewLoc,
1815                                          LI->getIterator());
1816 }
1817 
1818 void llvm::ConvertDebugDeclareToDebugValue(DbgVariableRecord *DVR,
1819                                            StoreInst *SI, DIBuilder &Builder) {
1820   assert(DVR->isAddressOfVariable() || DVR->isDbgAssign());
1821   auto *DIVar = DVR->getVariable();
1822   assert(DIVar && "Missing variable");
1823   auto *DIExpr = DVR->getExpression();
1824   Value *DV = SI->getValueOperand();
1825 
1826   DebugLoc NewLoc = getDebugValueLoc(DVR);
1827 
1828   // If the alloca describes the variable itself, i.e. the expression in the
1829   // dbg.declare doesn't start with a dereference, we can perform the
1830   // conversion if the value covers the entire fragment of DII.
1831   // If the alloca describes the *address* of DIVar, i.e. DIExpr is
1832   // *just* a DW_OP_deref, we use DV as is for the dbg.value.
1833   // We conservatively ignore other dereferences, because the following two are
1834   // not equivalent:
1835   //     dbg.declare(alloca, ..., !Expr(deref, plus_uconstant, 2))
1836   //     dbg.value(DV, ..., !Expr(deref, plus_uconstant, 2))
1837   // The former is adding 2 to the address of the variable, whereas the latter
1838   // is adding 2 to the value of the variable. As such, we insist on just a
1839   // deref expression.
1840   bool CanConvert =
1841       DIExpr->isDeref() || (!DIExpr->startsWithDeref() &&
1842                             valueCoversEntireFragment(DV->getType(), DVR));
1843   if (CanConvert) {
1844     insertDbgValueOrDbgVariableRecord(Builder, DV, DIVar, DIExpr, NewLoc,
1845                                       SI->getIterator());
1846     return;
1847   }
1848 
1849   // FIXME: If storing to a part of the variable described by the dbg.declare,
1850   // then we want to insert a dbg.value for the corresponding fragment.
1851   LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: " << *DVR
1852                     << '\n');
1853   assert(UseNewDbgInfoFormat);
1854 
1855   // For now, when there is a store to parts of the variable (but we do not
1856   // know which part) we insert an dbg.value intrinsic to indicate that we
1857   // know nothing about the variable's content.
1858   DV = PoisonValue::get(DV->getType());
1859   ValueAsMetadata *DVAM = ValueAsMetadata::get(DV);
1860   DbgVariableRecord *NewDVR =
1861       new DbgVariableRecord(DVAM, DIVar, DIExpr, NewLoc.get());
1862   SI->getParent()->insertDbgRecordBefore(NewDVR, SI->getIterator());
1863 }
1864 
1865 void llvm::InsertDebugValueAtStoreLoc(DbgVariableRecord *DVR, StoreInst *SI,
1866                                       DIBuilder &Builder) {
1867   auto *DIVar = DVR->getVariable();
1868   assert(DIVar && "Missing variable");
1869   auto *DIExpr = DVR->getExpression();
1870   DIExpr = dropInitialDeref(DIExpr);
1871   Value *DV = SI->getValueOperand();
1872 
1873   DebugLoc NewLoc = getDebugValueLoc(DVR);
1874 
1875   insertDbgValueOrDbgVariableRecord(Builder, DV, DIVar, DIExpr, NewLoc,
1876                                     SI->getIterator());
1877 }
1878 
1879 /// Inserts a llvm.dbg.value intrinsic after a phi that has an associated
1880 /// llvm.dbg.declare intrinsic.
1881 void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
1882                                            PHINode *APN, DIBuilder &Builder) {
1883   auto *DIVar = DII->getVariable();
1884   auto *DIExpr = DII->getExpression();
1885   assert(DIVar && "Missing variable");
1886 
1887   if (PhiHasDebugValue(DIVar, DIExpr, APN))
1888     return;
1889 
1890   if (!valueCoversEntireFragment(APN->getType(), DII)) {
1891     // FIXME: If only referring to a part of the variable described by the
1892     // dbg.declare, then we want to insert a dbg.value for the corresponding
1893     // fragment.
1894     LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: "
1895                       << *DII << '\n');
1896     return;
1897   }
1898 
1899   BasicBlock *BB = APN->getParent();
1900   auto InsertionPt = BB->getFirstInsertionPt();
1901 
1902   DebugLoc NewLoc = getDebugValueLoc(DII);
1903 
1904   // The block may be a catchswitch block, which does not have a valid
1905   // insertion point.
1906   // FIXME: Insert dbg.value markers in the successors when appropriate.
1907   if (InsertionPt != BB->end()) {
1908     insertDbgValueOrDbgVariableRecord(Builder, APN, DIVar, DIExpr, NewLoc,
1909                                       InsertionPt);
1910   }
1911 }
1912 
1913 void llvm::ConvertDebugDeclareToDebugValue(DbgVariableRecord *DVR, LoadInst *LI,
1914                                            DIBuilder &Builder) {
1915   auto *DIVar = DVR->getVariable();
1916   auto *DIExpr = DVR->getExpression();
1917   assert(DIVar && "Missing variable");
1918 
1919   if (!valueCoversEntireFragment(LI->getType(), DVR)) {
1920     // FIXME: If only referring to a part of the variable described by the
1921     // dbg.declare, then we want to insert a DbgVariableRecord for the
1922     // corresponding fragment.
1923     LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to DbgVariableRecord: "
1924                       << *DVR << '\n');
1925     return;
1926   }
1927 
1928   DebugLoc NewLoc = getDebugValueLoc(DVR);
1929 
1930   // We are now tracking the loaded value instead of the address. In the
1931   // future if multi-location support is added to the IR, it might be
1932   // preferable to keep tracking both the loaded value and the original
1933   // address in case the alloca can not be elided.
1934   assert(UseNewDbgInfoFormat);
1935 
1936   // Create a DbgVariableRecord directly and insert.
1937   ValueAsMetadata *LIVAM = ValueAsMetadata::get(LI);
1938   DbgVariableRecord *DV =
1939       new DbgVariableRecord(LIVAM, DIVar, DIExpr, NewLoc.get());
1940   LI->getParent()->insertDbgRecordAfter(DV, LI);
1941 }
1942 
1943 /// Determine whether this alloca is either a VLA or an array.
1944 static bool isArray(AllocaInst *AI) {
1945   return AI->isArrayAllocation() ||
1946          (AI->getAllocatedType() && AI->getAllocatedType()->isArrayTy());
1947 }
1948 
1949 /// Determine whether this alloca is a structure.
1950 static bool isStructure(AllocaInst *AI) {
1951   return AI->getAllocatedType() && AI->getAllocatedType()->isStructTy();
1952 }
1953 void llvm::ConvertDebugDeclareToDebugValue(DbgVariableRecord *DVR, PHINode *APN,
1954                                            DIBuilder &Builder) {
1955   auto *DIVar = DVR->getVariable();
1956   auto *DIExpr = DVR->getExpression();
1957   assert(DIVar && "Missing variable");
1958 
1959   if (PhiHasDebugValue(DIVar, DIExpr, APN))
1960     return;
1961 
1962   if (!valueCoversEntireFragment(APN->getType(), DVR)) {
1963     // FIXME: If only referring to a part of the variable described by the
1964     // dbg.declare, then we want to insert a DbgVariableRecord for the
1965     // corresponding fragment.
1966     LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to DbgVariableRecord: "
1967                       << *DVR << '\n');
1968     return;
1969   }
1970 
1971   BasicBlock *BB = APN->getParent();
1972   auto InsertionPt = BB->getFirstInsertionPt();
1973 
1974   DebugLoc NewLoc = getDebugValueLoc(DVR);
1975 
1976   // The block may be a catchswitch block, which does not have a valid
1977   // insertion point.
1978   // FIXME: Insert DbgVariableRecord markers in the successors when appropriate.
1979   if (InsertionPt != BB->end()) {
1980     insertDbgValueOrDbgVariableRecord(Builder, APN, DIVar, DIExpr, NewLoc,
1981                                       InsertionPt);
1982   }
1983 }
1984 
1985 /// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set
1986 /// of llvm.dbg.value intrinsics.
1987 bool llvm::LowerDbgDeclare(Function &F) {
1988   bool Changed = false;
1989   DIBuilder DIB(*F.getParent(), /*AllowUnresolved*/ false);
1990   SmallVector<DbgDeclareInst *, 4> Dbgs;
1991   SmallVector<DbgVariableRecord *> DVRs;
1992   for (auto &FI : F) {
1993     for (Instruction &BI : FI) {
1994       if (auto *DDI = dyn_cast<DbgDeclareInst>(&BI))
1995         Dbgs.push_back(DDI);
1996       for (DbgVariableRecord &DVR : filterDbgVars(BI.getDbgRecordRange())) {
1997         if (DVR.getType() == DbgVariableRecord::LocationType::Declare)
1998           DVRs.push_back(&DVR);
1999       }
2000     }
2001   }
2002 
2003   if (Dbgs.empty() && DVRs.empty())
2004     return Changed;
2005 
2006   auto LowerOne = [&](auto *DDI) {
2007     AllocaInst *AI =
2008         dyn_cast_or_null<AllocaInst>(DDI->getVariableLocationOp(0));
2009     // If this is an alloca for a scalar variable, insert a dbg.value
2010     // at each load and store to the alloca and erase the dbg.declare.
2011     // The dbg.values allow tracking a variable even if it is not
2012     // stored on the stack, while the dbg.declare can only describe
2013     // the stack slot (and at a lexical-scope granularity). Later
2014     // passes will attempt to elide the stack slot.
2015     if (!AI || isArray(AI) || isStructure(AI))
2016       return;
2017 
2018     // A volatile load/store means that the alloca can't be elided anyway.
2019     if (llvm::any_of(AI->users(), [](User *U) -> bool {
2020           if (LoadInst *LI = dyn_cast<LoadInst>(U))
2021             return LI->isVolatile();
2022           if (StoreInst *SI = dyn_cast<StoreInst>(U))
2023             return SI->isVolatile();
2024           return false;
2025         }))
2026       return;
2027 
2028     SmallVector<const Value *, 8> WorkList;
2029     WorkList.push_back(AI);
2030     while (!WorkList.empty()) {
2031       const Value *V = WorkList.pop_back_val();
2032       for (const auto &AIUse : V->uses()) {
2033         User *U = AIUse.getUser();
2034         if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
2035           if (AIUse.getOperandNo() == 1)
2036             ConvertDebugDeclareToDebugValue(DDI, SI, DIB);
2037         } else if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
2038           ConvertDebugDeclareToDebugValue(DDI, LI, DIB);
2039         } else if (CallInst *CI = dyn_cast<CallInst>(U)) {
2040           // This is a call by-value or some other instruction that takes a
2041           // pointer to the variable. Insert a *value* intrinsic that describes
2042           // the variable by dereferencing the alloca.
2043           if (!CI->isLifetimeStartOrEnd()) {
2044             DebugLoc NewLoc = getDebugValueLoc(DDI);
2045             auto *DerefExpr =
2046                 DIExpression::append(DDI->getExpression(), dwarf::DW_OP_deref);
2047             insertDbgValueOrDbgVariableRecord(DIB, AI, DDI->getVariable(),
2048                                               DerefExpr, NewLoc,
2049                                               CI->getIterator());
2050           }
2051         } else if (BitCastInst *BI = dyn_cast<BitCastInst>(U)) {
2052           if (BI->getType()->isPointerTy())
2053             WorkList.push_back(BI);
2054         }
2055       }
2056     }
2057     DDI->eraseFromParent();
2058     Changed = true;
2059   };
2060 
2061   for_each(Dbgs, LowerOne);
2062   for_each(DVRs, LowerOne);
2063 
2064   if (Changed)
2065     for (BasicBlock &BB : F)
2066       RemoveRedundantDbgInstrs(&BB);
2067 
2068   return Changed;
2069 }
2070 
2071 // RemoveDIs: re-implementation of insertDebugValuesForPHIs, but which pulls the
2072 // debug-info out of the block's DbgVariableRecords rather than dbg.value
2073 // intrinsics.
2074 static void
2075 insertDbgVariableRecordsForPHIs(BasicBlock *BB,
2076                                 SmallVectorImpl<PHINode *> &InsertedPHIs) {
2077   assert(BB && "No BasicBlock to clone DbgVariableRecord(s) from.");
2078   if (InsertedPHIs.size() == 0)
2079     return;
2080 
2081   // Map existing PHI nodes to their DbgVariableRecords.
2082   DenseMap<Value *, DbgVariableRecord *> DbgValueMap;
2083   for (auto &I : *BB) {
2084     for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {
2085       for (Value *V : DVR.location_ops())
2086         if (auto *Loc = dyn_cast_or_null<PHINode>(V))
2087           DbgValueMap.insert({Loc, &DVR});
2088     }
2089   }
2090   if (DbgValueMap.size() == 0)
2091     return;
2092 
2093   // Map a pair of the destination BB and old DbgVariableRecord to the new
2094   // DbgVariableRecord, so that if a DbgVariableRecord is being rewritten to use
2095   // more than one of the inserted PHIs in the same destination BB, we can
2096   // update the same DbgVariableRecord with all the new PHIs instead of creating
2097   // one copy for each.
2098   MapVector<std::pair<BasicBlock *, DbgVariableRecord *>, DbgVariableRecord *>
2099       NewDbgValueMap;
2100   // Then iterate through the new PHIs and look to see if they use one of the
2101   // previously mapped PHIs. If so, create a new DbgVariableRecord that will
2102   // propagate the info through the new PHI. If we use more than one new PHI in
2103   // a single destination BB with the same old dbg.value, merge the updates so
2104   // that we get a single new DbgVariableRecord with all the new PHIs.
2105   for (auto PHI : InsertedPHIs) {
2106     BasicBlock *Parent = PHI->getParent();
2107     // Avoid inserting a debug-info record into an EH block.
2108     if (Parent->getFirstNonPHI()->isEHPad())
2109       continue;
2110     for (auto VI : PHI->operand_values()) {
2111       auto V = DbgValueMap.find(VI);
2112       if (V != DbgValueMap.end()) {
2113         DbgVariableRecord *DbgII = cast<DbgVariableRecord>(V->second);
2114         auto NewDI = NewDbgValueMap.find({Parent, DbgII});
2115         if (NewDI == NewDbgValueMap.end()) {
2116           DbgVariableRecord *NewDbgII = DbgII->clone();
2117           NewDI = NewDbgValueMap.insert({{Parent, DbgII}, NewDbgII}).first;
2118         }
2119         DbgVariableRecord *NewDbgII = NewDI->second;
2120         // If PHI contains VI as an operand more than once, we may
2121         // replaced it in NewDbgII; confirm that it is present.
2122         if (is_contained(NewDbgII->location_ops(), VI))
2123           NewDbgII->replaceVariableLocationOp(VI, PHI);
2124       }
2125     }
2126   }
2127   // Insert the new DbgVariableRecords into their destination blocks.
2128   for (auto DI : NewDbgValueMap) {
2129     BasicBlock *Parent = DI.first.first;
2130     DbgVariableRecord *NewDbgII = DI.second;
2131     auto InsertionPt = Parent->getFirstInsertionPt();
2132     assert(InsertionPt != Parent->end() && "Ill-formed basic block");
2133 
2134     Parent->insertDbgRecordBefore(NewDbgII, InsertionPt);
2135   }
2136 }
2137 
2138 /// Propagate dbg.value intrinsics through the newly inserted PHIs.
2139 void llvm::insertDebugValuesForPHIs(BasicBlock *BB,
2140                                     SmallVectorImpl<PHINode *> &InsertedPHIs) {
2141   assert(BB && "No BasicBlock to clone dbg.value(s) from.");
2142   if (InsertedPHIs.size() == 0)
2143     return;
2144 
2145   insertDbgVariableRecordsForPHIs(BB, InsertedPHIs);
2146 
2147   // Map existing PHI nodes to their dbg.values.
2148   ValueToValueMapTy DbgValueMap;
2149   for (auto &I : *BB) {
2150     if (auto DbgII = dyn_cast<DbgVariableIntrinsic>(&I)) {
2151       for (Value *V : DbgII->location_ops())
2152         if (auto *Loc = dyn_cast_or_null<PHINode>(V))
2153           DbgValueMap.insert({Loc, DbgII});
2154     }
2155   }
2156   if (DbgValueMap.size() == 0)
2157     return;
2158 
2159   // Map a pair of the destination BB and old dbg.value to the new dbg.value,
2160   // so that if a dbg.value is being rewritten to use more than one of the
2161   // inserted PHIs in the same destination BB, we can update the same dbg.value
2162   // with all the new PHIs instead of creating one copy for each.
2163   MapVector<std::pair<BasicBlock *, DbgVariableIntrinsic *>,
2164             DbgVariableIntrinsic *>
2165       NewDbgValueMap;
2166   // Then iterate through the new PHIs and look to see if they use one of the
2167   // previously mapped PHIs. If so, create a new dbg.value intrinsic that will
2168   // propagate the info through the new PHI. If we use more than one new PHI in
2169   // a single destination BB with the same old dbg.value, merge the updates so
2170   // that we get a single new dbg.value with all the new PHIs.
2171   for (auto *PHI : InsertedPHIs) {
2172     BasicBlock *Parent = PHI->getParent();
2173     // Avoid inserting an intrinsic into an EH block.
2174     if (Parent->getFirstNonPHI()->isEHPad())
2175       continue;
2176     for (auto *VI : PHI->operand_values()) {
2177       auto V = DbgValueMap.find(VI);
2178       if (V != DbgValueMap.end()) {
2179         auto *DbgII = cast<DbgVariableIntrinsic>(V->second);
2180         auto [NewDI, Inserted] = NewDbgValueMap.try_emplace({Parent, DbgII});
2181         if (Inserted)
2182           NewDI->second = cast<DbgVariableIntrinsic>(DbgII->clone());
2183         DbgVariableIntrinsic *NewDbgII = NewDI->second;
2184         // If PHI contains VI as an operand more than once, we may
2185         // replaced it in NewDbgII; confirm that it is present.
2186         if (is_contained(NewDbgII->location_ops(), VI))
2187           NewDbgII->replaceVariableLocationOp(VI, PHI);
2188       }
2189     }
2190   }
2191   // Insert thew new dbg.values into their destination blocks.
2192   for (auto DI : NewDbgValueMap) {
2193     BasicBlock *Parent = DI.first.first;
2194     auto *NewDbgII = DI.second;
2195     auto InsertionPt = Parent->getFirstInsertionPt();
2196     assert(InsertionPt != Parent->end() && "Ill-formed basic block");
2197     NewDbgII->insertBefore(&*InsertionPt);
2198   }
2199 }
2200 
2201 bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress,
2202                              DIBuilder &Builder, uint8_t DIExprFlags,
2203                              int Offset) {
2204   TinyPtrVector<DbgDeclareInst *> DbgDeclares = findDbgDeclares(Address);
2205   TinyPtrVector<DbgVariableRecord *> DVRDeclares = findDVRDeclares(Address);
2206 
2207   auto ReplaceOne = [&](auto *DII) {
2208     assert(DII->getVariable() && "Missing variable");
2209     auto *DIExpr = DII->getExpression();
2210     DIExpr = DIExpression::prepend(DIExpr, DIExprFlags, Offset);
2211     DII->setExpression(DIExpr);
2212     DII->replaceVariableLocationOp(Address, NewAddress);
2213   };
2214 
2215   for_each(DbgDeclares, ReplaceOne);
2216   for_each(DVRDeclares, ReplaceOne);
2217 
2218   return !DbgDeclares.empty() || !DVRDeclares.empty();
2219 }
2220 
2221 static void updateOneDbgValueForAlloca(const DebugLoc &Loc,
2222                                        DILocalVariable *DIVar,
2223                                        DIExpression *DIExpr, Value *NewAddress,
2224                                        DbgValueInst *DVI,
2225                                        DbgVariableRecord *DVR,
2226                                        DIBuilder &Builder, int Offset) {
2227   assert(DIVar && "Missing variable");
2228 
2229   // This is an alloca-based dbg.value/DbgVariableRecord. The first thing it
2230   // should do with the alloca pointer is dereference it. Otherwise we don't
2231   // know how to handle it and give up.
2232   if (!DIExpr || DIExpr->getNumElements() < 1 ||
2233       DIExpr->getElement(0) != dwarf::DW_OP_deref)
2234     return;
2235 
2236   // Insert the offset before the first deref.
2237   if (Offset)
2238     DIExpr = DIExpression::prepend(DIExpr, 0, Offset);
2239 
2240   if (DVI) {
2241     DVI->setExpression(DIExpr);
2242     DVI->replaceVariableLocationOp(0u, NewAddress);
2243   } else {
2244     assert(DVR);
2245     DVR->setExpression(DIExpr);
2246     DVR->replaceVariableLocationOp(0u, NewAddress);
2247   }
2248 }
2249 
2250 void llvm::replaceDbgValueForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
2251                                     DIBuilder &Builder, int Offset) {
2252   SmallVector<DbgValueInst *, 1> DbgUsers;
2253   SmallVector<DbgVariableRecord *, 1> DPUsers;
2254   findDbgValues(DbgUsers, AI, &DPUsers);
2255 
2256   // Attempt to replace dbg.values that use this alloca.
2257   for (auto *DVI : DbgUsers)
2258     updateOneDbgValueForAlloca(DVI->getDebugLoc(), DVI->getVariable(),
2259                                DVI->getExpression(), NewAllocaAddress, DVI,
2260                                nullptr, Builder, Offset);
2261 
2262   // Replace any DbgVariableRecords that use this alloca.
2263   for (DbgVariableRecord *DVR : DPUsers)
2264     updateOneDbgValueForAlloca(DVR->getDebugLoc(), DVR->getVariable(),
2265                                DVR->getExpression(), NewAllocaAddress, nullptr,
2266                                DVR, Builder, Offset);
2267 }
2268 
2269 /// Where possible to salvage debug information for \p I do so.
2270 /// If not possible mark undef.
2271 void llvm::salvageDebugInfo(Instruction &I) {
2272   SmallVector<DbgVariableIntrinsic *, 1> DbgUsers;
2273   SmallVector<DbgVariableRecord *, 1> DPUsers;
2274   findDbgUsers(DbgUsers, &I, &DPUsers);
2275   salvageDebugInfoForDbgValues(I, DbgUsers, DPUsers);
2276 }
2277 
2278 template <typename T> static void salvageDbgAssignAddress(T *Assign) {
2279   Instruction *I = dyn_cast<Instruction>(Assign->getAddress());
2280   // Only instructions can be salvaged at the moment.
2281   if (!I)
2282     return;
2283 
2284   assert(!Assign->getAddressExpression()->getFragmentInfo().has_value() &&
2285          "address-expression shouldn't have fragment info");
2286 
2287   // The address component of a dbg.assign cannot be variadic.
2288   uint64_t CurrentLocOps = 0;
2289   SmallVector<Value *, 4> AdditionalValues;
2290   SmallVector<uint64_t, 16> Ops;
2291   Value *NewV = salvageDebugInfoImpl(*I, CurrentLocOps, Ops, AdditionalValues);
2292 
2293   // Check if the salvage failed.
2294   if (!NewV)
2295     return;
2296 
2297   DIExpression *SalvagedExpr = DIExpression::appendOpsToArg(
2298       Assign->getAddressExpression(), Ops, 0, /*StackValue=*/false);
2299   assert(!SalvagedExpr->getFragmentInfo().has_value() &&
2300          "address-expression shouldn't have fragment info");
2301 
2302   SalvagedExpr = SalvagedExpr->foldConstantMath();
2303 
2304   // Salvage succeeds if no additional values are required.
2305   if (AdditionalValues.empty()) {
2306     Assign->setAddress(NewV);
2307     Assign->setAddressExpression(SalvagedExpr);
2308   } else {
2309     Assign->setKillAddress();
2310   }
2311 }
2312 
2313 void llvm::salvageDebugInfoForDbgValues(
2314     Instruction &I, ArrayRef<DbgVariableIntrinsic *> DbgUsers,
2315     ArrayRef<DbgVariableRecord *> DPUsers) {
2316   // These are arbitrary chosen limits on the maximum number of values and the
2317   // maximum size of a debug expression we can salvage up to, used for
2318   // performance reasons.
2319   const unsigned MaxDebugArgs = 16;
2320   const unsigned MaxExpressionSize = 128;
2321   bool Salvaged = false;
2322 
2323   for (auto *DII : DbgUsers) {
2324     if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(DII)) {
2325       if (DAI->getAddress() == &I) {
2326         salvageDbgAssignAddress(DAI);
2327         Salvaged = true;
2328       }
2329       if (DAI->getValue() != &I)
2330         continue;
2331     }
2332 
2333     // Do not add DW_OP_stack_value for DbgDeclare, because they are implicitly
2334     // pointing out the value as a DWARF memory location description.
2335     bool StackValue = isa<DbgValueInst>(DII);
2336     auto DIILocation = DII->location_ops();
2337     assert(
2338         is_contained(DIILocation, &I) &&
2339         "DbgVariableIntrinsic must use salvaged instruction as its location");
2340     SmallVector<Value *, 4> AdditionalValues;
2341     // `I` may appear more than once in DII's location ops, and each use of `I`
2342     // must be updated in the DIExpression and potentially have additional
2343     // values added; thus we call salvageDebugInfoImpl for each `I` instance in
2344     // DIILocation.
2345     Value *Op0 = nullptr;
2346     DIExpression *SalvagedExpr = DII->getExpression();
2347     auto LocItr = find(DIILocation, &I);
2348     while (SalvagedExpr && LocItr != DIILocation.end()) {
2349       SmallVector<uint64_t, 16> Ops;
2350       unsigned LocNo = std::distance(DIILocation.begin(), LocItr);
2351       uint64_t CurrentLocOps = SalvagedExpr->getNumLocationOperands();
2352       Op0 = salvageDebugInfoImpl(I, CurrentLocOps, Ops, AdditionalValues);
2353       if (!Op0)
2354         break;
2355       SalvagedExpr =
2356           DIExpression::appendOpsToArg(SalvagedExpr, Ops, LocNo, StackValue);
2357       LocItr = std::find(++LocItr, DIILocation.end(), &I);
2358     }
2359     // salvageDebugInfoImpl should fail on examining the first element of
2360     // DbgUsers, or none of them.
2361     if (!Op0)
2362       break;
2363 
2364     SalvagedExpr = SalvagedExpr->foldConstantMath();
2365     DII->replaceVariableLocationOp(&I, Op0);
2366     bool IsValidSalvageExpr = SalvagedExpr->getNumElements() <= MaxExpressionSize;
2367     if (AdditionalValues.empty() && IsValidSalvageExpr) {
2368       DII->setExpression(SalvagedExpr);
2369     } else if (isa<DbgValueInst>(DII) && IsValidSalvageExpr &&
2370                DII->getNumVariableLocationOps() + AdditionalValues.size() <=
2371                    MaxDebugArgs) {
2372       DII->addVariableLocationOps(AdditionalValues, SalvagedExpr);
2373     } else {
2374       // Do not salvage using DIArgList for dbg.declare, as it is not currently
2375       // supported in those instructions. Also do not salvage if the resulting
2376       // DIArgList would contain an unreasonably large number of values.
2377       DII->setKillLocation();
2378     }
2379     LLVM_DEBUG(dbgs() << "SALVAGE: " << *DII << '\n');
2380     Salvaged = true;
2381   }
2382   // Duplicate of above block for DbgVariableRecords.
2383   for (auto *DVR : DPUsers) {
2384     if (DVR->isDbgAssign()) {
2385       if (DVR->getAddress() == &I) {
2386         salvageDbgAssignAddress(DVR);
2387         Salvaged = true;
2388       }
2389       if (DVR->getValue() != &I)
2390         continue;
2391     }
2392 
2393     // Do not add DW_OP_stack_value for DbgDeclare and DbgAddr, because they
2394     // are implicitly pointing out the value as a DWARF memory location
2395     // description.
2396     bool StackValue =
2397         DVR->getType() != DbgVariableRecord::LocationType::Declare;
2398     auto DVRLocation = DVR->location_ops();
2399     assert(
2400         is_contained(DVRLocation, &I) &&
2401         "DbgVariableIntrinsic must use salvaged instruction as its location");
2402     SmallVector<Value *, 4> AdditionalValues;
2403     // 'I' may appear more than once in DVR's location ops, and each use of 'I'
2404     // must be updated in the DIExpression and potentially have additional
2405     // values added; thus we call salvageDebugInfoImpl for each 'I' instance in
2406     // DVRLocation.
2407     Value *Op0 = nullptr;
2408     DIExpression *SalvagedExpr = DVR->getExpression();
2409     auto LocItr = find(DVRLocation, &I);
2410     while (SalvagedExpr && LocItr != DVRLocation.end()) {
2411       SmallVector<uint64_t, 16> Ops;
2412       unsigned LocNo = std::distance(DVRLocation.begin(), LocItr);
2413       uint64_t CurrentLocOps = SalvagedExpr->getNumLocationOperands();
2414       Op0 = salvageDebugInfoImpl(I, CurrentLocOps, Ops, AdditionalValues);
2415       if (!Op0)
2416         break;
2417       SalvagedExpr =
2418           DIExpression::appendOpsToArg(SalvagedExpr, Ops, LocNo, StackValue);
2419       LocItr = std::find(++LocItr, DVRLocation.end(), &I);
2420     }
2421     // salvageDebugInfoImpl should fail on examining the first element of
2422     // DbgUsers, or none of them.
2423     if (!Op0)
2424       break;
2425 
2426     SalvagedExpr = SalvagedExpr->foldConstantMath();
2427     DVR->replaceVariableLocationOp(&I, Op0);
2428     bool IsValidSalvageExpr =
2429         SalvagedExpr->getNumElements() <= MaxExpressionSize;
2430     if (AdditionalValues.empty() && IsValidSalvageExpr) {
2431       DVR->setExpression(SalvagedExpr);
2432     } else if (DVR->getType() != DbgVariableRecord::LocationType::Declare &&
2433                IsValidSalvageExpr &&
2434                DVR->getNumVariableLocationOps() + AdditionalValues.size() <=
2435                    MaxDebugArgs) {
2436       DVR->addVariableLocationOps(AdditionalValues, SalvagedExpr);
2437     } else {
2438       // Do not salvage using DIArgList for dbg.addr/dbg.declare, as it is
2439       // currently only valid for stack value expressions.
2440       // Also do not salvage if the resulting DIArgList would contain an
2441       // unreasonably large number of values.
2442       DVR->setKillLocation();
2443     }
2444     LLVM_DEBUG(dbgs() << "SALVAGE: " << DVR << '\n');
2445     Salvaged = true;
2446   }
2447 
2448   if (Salvaged)
2449     return;
2450 
2451   for (auto *DII : DbgUsers)
2452     DII->setKillLocation();
2453 
2454   for (auto *DVR : DPUsers)
2455     DVR->setKillLocation();
2456 }
2457 
2458 Value *getSalvageOpsForGEP(GetElementPtrInst *GEP, const DataLayout &DL,
2459                            uint64_t CurrentLocOps,
2460                            SmallVectorImpl<uint64_t> &Opcodes,
2461                            SmallVectorImpl<Value *> &AdditionalValues) {
2462   unsigned BitWidth = DL.getIndexSizeInBits(GEP->getPointerAddressSpace());
2463   // Rewrite a GEP into a DIExpression.
2464   SmallMapVector<Value *, APInt, 4> VariableOffsets;
2465   APInt ConstantOffset(BitWidth, 0);
2466   if (!GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset))
2467     return nullptr;
2468   if (!VariableOffsets.empty() && !CurrentLocOps) {
2469     Opcodes.insert(Opcodes.begin(), {dwarf::DW_OP_LLVM_arg, 0});
2470     CurrentLocOps = 1;
2471   }
2472   for (const auto &Offset : VariableOffsets) {
2473     AdditionalValues.push_back(Offset.first);
2474     assert(Offset.second.isStrictlyPositive() &&
2475            "Expected strictly positive multiplier for offset.");
2476     Opcodes.append({dwarf::DW_OP_LLVM_arg, CurrentLocOps++, dwarf::DW_OP_constu,
2477                     Offset.second.getZExtValue(), dwarf::DW_OP_mul,
2478                     dwarf::DW_OP_plus});
2479   }
2480   DIExpression::appendOffset(Opcodes, ConstantOffset.getSExtValue());
2481   return GEP->getOperand(0);
2482 }
2483 
2484 uint64_t getDwarfOpForBinOp(Instruction::BinaryOps Opcode) {
2485   switch (Opcode) {
2486   case Instruction::Add:
2487     return dwarf::DW_OP_plus;
2488   case Instruction::Sub:
2489     return dwarf::DW_OP_minus;
2490   case Instruction::Mul:
2491     return dwarf::DW_OP_mul;
2492   case Instruction::SDiv:
2493     return dwarf::DW_OP_div;
2494   case Instruction::SRem:
2495     return dwarf::DW_OP_mod;
2496   case Instruction::Or:
2497     return dwarf::DW_OP_or;
2498   case Instruction::And:
2499     return dwarf::DW_OP_and;
2500   case Instruction::Xor:
2501     return dwarf::DW_OP_xor;
2502   case Instruction::Shl:
2503     return dwarf::DW_OP_shl;
2504   case Instruction::LShr:
2505     return dwarf::DW_OP_shr;
2506   case Instruction::AShr:
2507     return dwarf::DW_OP_shra;
2508   default:
2509     // TODO: Salvage from each kind of binop we know about.
2510     return 0;
2511   }
2512 }
2513 
2514 static void handleSSAValueOperands(uint64_t CurrentLocOps,
2515                                    SmallVectorImpl<uint64_t> &Opcodes,
2516                                    SmallVectorImpl<Value *> &AdditionalValues,
2517                                    Instruction *I) {
2518   if (!CurrentLocOps) {
2519     Opcodes.append({dwarf::DW_OP_LLVM_arg, 0});
2520     CurrentLocOps = 1;
2521   }
2522   Opcodes.append({dwarf::DW_OP_LLVM_arg, CurrentLocOps});
2523   AdditionalValues.push_back(I->getOperand(1));
2524 }
2525 
2526 Value *getSalvageOpsForBinOp(BinaryOperator *BI, uint64_t CurrentLocOps,
2527                              SmallVectorImpl<uint64_t> &Opcodes,
2528                              SmallVectorImpl<Value *> &AdditionalValues) {
2529   // Handle binary operations with constant integer operands as a special case.
2530   auto *ConstInt = dyn_cast<ConstantInt>(BI->getOperand(1));
2531   // Values wider than 64 bits cannot be represented within a DIExpression.
2532   if (ConstInt && ConstInt->getBitWidth() > 64)
2533     return nullptr;
2534 
2535   Instruction::BinaryOps BinOpcode = BI->getOpcode();
2536   // Push any Constant Int operand onto the expression stack.
2537   if (ConstInt) {
2538     uint64_t Val = ConstInt->getSExtValue();
2539     // Add or Sub Instructions with a constant operand can potentially be
2540     // simplified.
2541     if (BinOpcode == Instruction::Add || BinOpcode == Instruction::Sub) {
2542       uint64_t Offset = BinOpcode == Instruction::Add ? Val : -int64_t(Val);
2543       DIExpression::appendOffset(Opcodes, Offset);
2544       return BI->getOperand(0);
2545     }
2546     Opcodes.append({dwarf::DW_OP_constu, Val});
2547   } else {
2548     handleSSAValueOperands(CurrentLocOps, Opcodes, AdditionalValues, BI);
2549   }
2550 
2551   // Add salvaged binary operator to expression stack, if it has a valid
2552   // representation in a DIExpression.
2553   uint64_t DwarfBinOp = getDwarfOpForBinOp(BinOpcode);
2554   if (!DwarfBinOp)
2555     return nullptr;
2556   Opcodes.push_back(DwarfBinOp);
2557   return BI->getOperand(0);
2558 }
2559 
2560 uint64_t getDwarfOpForIcmpPred(CmpInst::Predicate Pred) {
2561   // The signedness of the operation is implicit in the typed stack, signed and
2562   // unsigned instructions map to the same DWARF opcode.
2563   switch (Pred) {
2564   case CmpInst::ICMP_EQ:
2565     return dwarf::DW_OP_eq;
2566   case CmpInst::ICMP_NE:
2567     return dwarf::DW_OP_ne;
2568   case CmpInst::ICMP_UGT:
2569   case CmpInst::ICMP_SGT:
2570     return dwarf::DW_OP_gt;
2571   case CmpInst::ICMP_UGE:
2572   case CmpInst::ICMP_SGE:
2573     return dwarf::DW_OP_ge;
2574   case CmpInst::ICMP_ULT:
2575   case CmpInst::ICMP_SLT:
2576     return dwarf::DW_OP_lt;
2577   case CmpInst::ICMP_ULE:
2578   case CmpInst::ICMP_SLE:
2579     return dwarf::DW_OP_le;
2580   default:
2581     return 0;
2582   }
2583 }
2584 
2585 Value *getSalvageOpsForIcmpOp(ICmpInst *Icmp, uint64_t CurrentLocOps,
2586                               SmallVectorImpl<uint64_t> &Opcodes,
2587                               SmallVectorImpl<Value *> &AdditionalValues) {
2588   // Handle icmp operations with constant integer operands as a special case.
2589   auto *ConstInt = dyn_cast<ConstantInt>(Icmp->getOperand(1));
2590   // Values wider than 64 bits cannot be represented within a DIExpression.
2591   if (ConstInt && ConstInt->getBitWidth() > 64)
2592     return nullptr;
2593   // Push any Constant Int operand onto the expression stack.
2594   if (ConstInt) {
2595     if (Icmp->isSigned())
2596       Opcodes.push_back(dwarf::DW_OP_consts);
2597     else
2598       Opcodes.push_back(dwarf::DW_OP_constu);
2599     uint64_t Val = ConstInt->getSExtValue();
2600     Opcodes.push_back(Val);
2601   } else {
2602     handleSSAValueOperands(CurrentLocOps, Opcodes, AdditionalValues, Icmp);
2603   }
2604 
2605   // Add salvaged binary operator to expression stack, if it has a valid
2606   // representation in a DIExpression.
2607   uint64_t DwarfIcmpOp = getDwarfOpForIcmpPred(Icmp->getPredicate());
2608   if (!DwarfIcmpOp)
2609     return nullptr;
2610   Opcodes.push_back(DwarfIcmpOp);
2611   return Icmp->getOperand(0);
2612 }
2613 
2614 Value *llvm::salvageDebugInfoImpl(Instruction &I, uint64_t CurrentLocOps,
2615                                   SmallVectorImpl<uint64_t> &Ops,
2616                                   SmallVectorImpl<Value *> &AdditionalValues) {
2617   auto &M = *I.getModule();
2618   auto &DL = M.getDataLayout();
2619 
2620   if (auto *CI = dyn_cast<CastInst>(&I)) {
2621     Value *FromValue = CI->getOperand(0);
2622     // No-op casts are irrelevant for debug info.
2623     if (CI->isNoopCast(DL)) {
2624       return FromValue;
2625     }
2626 
2627     Type *Type = CI->getType();
2628     if (Type->isPointerTy())
2629       Type = DL.getIntPtrType(Type);
2630     // Casts other than Trunc, SExt, or ZExt to scalar types cannot be salvaged.
2631     if (Type->isVectorTy() ||
2632         !(isa<TruncInst>(&I) || isa<SExtInst>(&I) || isa<ZExtInst>(&I) ||
2633           isa<IntToPtrInst>(&I) || isa<PtrToIntInst>(&I)))
2634       return nullptr;
2635 
2636     llvm::Type *FromType = FromValue->getType();
2637     if (FromType->isPointerTy())
2638       FromType = DL.getIntPtrType(FromType);
2639 
2640     unsigned FromTypeBitSize = FromType->getScalarSizeInBits();
2641     unsigned ToTypeBitSize = Type->getScalarSizeInBits();
2642 
2643     auto ExtOps = DIExpression::getExtOps(FromTypeBitSize, ToTypeBitSize,
2644                                           isa<SExtInst>(&I));
2645     Ops.append(ExtOps.begin(), ExtOps.end());
2646     return FromValue;
2647   }
2648 
2649   if (auto *GEP = dyn_cast<GetElementPtrInst>(&I))
2650     return getSalvageOpsForGEP(GEP, DL, CurrentLocOps, Ops, AdditionalValues);
2651   if (auto *BI = dyn_cast<BinaryOperator>(&I))
2652     return getSalvageOpsForBinOp(BI, CurrentLocOps, Ops, AdditionalValues);
2653   if (auto *IC = dyn_cast<ICmpInst>(&I))
2654     return getSalvageOpsForIcmpOp(IC, CurrentLocOps, Ops, AdditionalValues);
2655 
2656   // *Not* to do: we should not attempt to salvage load instructions,
2657   // because the validity and lifetime of a dbg.value containing
2658   // DW_OP_deref becomes difficult to analyze. See PR40628 for examples.
2659   return nullptr;
2660 }
2661 
2662 /// A replacement for a dbg.value expression.
2663 using DbgValReplacement = std::optional<DIExpression *>;
2664 
2665 /// Point debug users of \p From to \p To using exprs given by \p RewriteExpr,
2666 /// possibly moving/undefing users to prevent use-before-def. Returns true if
2667 /// changes are made.
2668 static bool rewriteDebugUsers(
2669     Instruction &From, Value &To, Instruction &DomPoint, DominatorTree &DT,
2670     function_ref<DbgValReplacement(DbgVariableIntrinsic &DII)> RewriteExpr,
2671     function_ref<DbgValReplacement(DbgVariableRecord &DVR)> RewriteDVRExpr) {
2672   // Find debug users of From.
2673   SmallVector<DbgVariableIntrinsic *, 1> Users;
2674   SmallVector<DbgVariableRecord *, 1> DPUsers;
2675   findDbgUsers(Users, &From, &DPUsers);
2676   if (Users.empty() && DPUsers.empty())
2677     return false;
2678 
2679   // Prevent use-before-def of To.
2680   bool Changed = false;
2681 
2682   SmallPtrSet<DbgVariableIntrinsic *, 1> UndefOrSalvage;
2683   SmallPtrSet<DbgVariableRecord *, 1> UndefOrSalvageDVR;
2684   if (isa<Instruction>(&To)) {
2685     bool DomPointAfterFrom = From.getNextNonDebugInstruction() == &DomPoint;
2686 
2687     for (auto *DII : Users) {
2688       // It's common to see a debug user between From and DomPoint. Move it
2689       // after DomPoint to preserve the variable update without any reordering.
2690       if (DomPointAfterFrom && DII->getNextNonDebugInstruction() == &DomPoint) {
2691         LLVM_DEBUG(dbgs() << "MOVE:  " << *DII << '\n');
2692         DII->moveAfter(&DomPoint);
2693         Changed = true;
2694 
2695       // Users which otherwise aren't dominated by the replacement value must
2696       // be salvaged or deleted.
2697       } else if (!DT.dominates(&DomPoint, DII)) {
2698         UndefOrSalvage.insert(DII);
2699       }
2700     }
2701 
2702     // DbgVariableRecord implementation of the above.
2703     for (auto *DVR : DPUsers) {
2704       Instruction *MarkedInstr = DVR->getMarker()->MarkedInstr;
2705       Instruction *NextNonDebug = MarkedInstr;
2706       // The next instruction might still be a dbg.declare, skip over it.
2707       if (isa<DbgVariableIntrinsic>(NextNonDebug))
2708         NextNonDebug = NextNonDebug->getNextNonDebugInstruction();
2709 
2710       if (DomPointAfterFrom && NextNonDebug == &DomPoint) {
2711         LLVM_DEBUG(dbgs() << "MOVE:  " << *DVR << '\n');
2712         DVR->removeFromParent();
2713         // Ensure there's a marker.
2714         DomPoint.getParent()->insertDbgRecordAfter(DVR, &DomPoint);
2715         Changed = true;
2716       } else if (!DT.dominates(&DomPoint, MarkedInstr)) {
2717         UndefOrSalvageDVR.insert(DVR);
2718       }
2719     }
2720   }
2721 
2722   // Update debug users without use-before-def risk.
2723   for (auto *DII : Users) {
2724     if (UndefOrSalvage.count(DII))
2725       continue;
2726 
2727     DbgValReplacement DVRepl = RewriteExpr(*DII);
2728     if (!DVRepl)
2729       continue;
2730 
2731     DII->replaceVariableLocationOp(&From, &To);
2732     DII->setExpression(*DVRepl);
2733     LLVM_DEBUG(dbgs() << "REWRITE:  " << *DII << '\n');
2734     Changed = true;
2735   }
2736   for (auto *DVR : DPUsers) {
2737     if (UndefOrSalvageDVR.count(DVR))
2738       continue;
2739 
2740     DbgValReplacement DVRepl = RewriteDVRExpr(*DVR);
2741     if (!DVRepl)
2742       continue;
2743 
2744     DVR->replaceVariableLocationOp(&From, &To);
2745     DVR->setExpression(*DVRepl);
2746     LLVM_DEBUG(dbgs() << "REWRITE:  " << DVR << '\n');
2747     Changed = true;
2748   }
2749 
2750   if (!UndefOrSalvage.empty() || !UndefOrSalvageDVR.empty()) {
2751     // Try to salvage the remaining debug users.
2752     salvageDebugInfo(From);
2753     Changed = true;
2754   }
2755 
2756   return Changed;
2757 }
2758 
2759 /// Check if a bitcast between a value of type \p FromTy to type \p ToTy would
2760 /// losslessly preserve the bits and semantics of the value. This predicate is
2761 /// symmetric, i.e swapping \p FromTy and \p ToTy should give the same result.
2762 ///
2763 /// Note that Type::canLosslesslyBitCastTo is not suitable here because it
2764 /// allows semantically unequivalent bitcasts, such as <2 x i64> -> <4 x i32>,
2765 /// and also does not allow lossless pointer <-> integer conversions.
2766 static bool isBitCastSemanticsPreserving(const DataLayout &DL, Type *FromTy,
2767                                          Type *ToTy) {
2768   // Trivially compatible types.
2769   if (FromTy == ToTy)
2770     return true;
2771 
2772   // Handle compatible pointer <-> integer conversions.
2773   if (FromTy->isIntOrPtrTy() && ToTy->isIntOrPtrTy()) {
2774     bool SameSize = DL.getTypeSizeInBits(FromTy) == DL.getTypeSizeInBits(ToTy);
2775     bool LosslessConversion = !DL.isNonIntegralPointerType(FromTy) &&
2776                               !DL.isNonIntegralPointerType(ToTy);
2777     return SameSize && LosslessConversion;
2778   }
2779 
2780   // TODO: This is not exhaustive.
2781   return false;
2782 }
2783 
2784 bool llvm::replaceAllDbgUsesWith(Instruction &From, Value &To,
2785                                  Instruction &DomPoint, DominatorTree &DT) {
2786   // Exit early if From has no debug users.
2787   if (!From.isUsedByMetadata())
2788     return false;
2789 
2790   assert(&From != &To && "Can't replace something with itself");
2791 
2792   Type *FromTy = From.getType();
2793   Type *ToTy = To.getType();
2794 
2795   auto Identity = [&](DbgVariableIntrinsic &DII) -> DbgValReplacement {
2796     return DII.getExpression();
2797   };
2798   auto IdentityDVR = [&](DbgVariableRecord &DVR) -> DbgValReplacement {
2799     return DVR.getExpression();
2800   };
2801 
2802   // Handle no-op conversions.
2803   Module &M = *From.getModule();
2804   const DataLayout &DL = M.getDataLayout();
2805   if (isBitCastSemanticsPreserving(DL, FromTy, ToTy))
2806     return rewriteDebugUsers(From, To, DomPoint, DT, Identity, IdentityDVR);
2807 
2808   // Handle integer-to-integer widening and narrowing.
2809   // FIXME: Use DW_OP_convert when it's available everywhere.
2810   if (FromTy->isIntegerTy() && ToTy->isIntegerTy()) {
2811     uint64_t FromBits = FromTy->getPrimitiveSizeInBits();
2812     uint64_t ToBits = ToTy->getPrimitiveSizeInBits();
2813     assert(FromBits != ToBits && "Unexpected no-op conversion");
2814 
2815     // When the width of the result grows, assume that a debugger will only
2816     // access the low `FromBits` bits when inspecting the source variable.
2817     if (FromBits < ToBits)
2818       return rewriteDebugUsers(From, To, DomPoint, DT, Identity, IdentityDVR);
2819 
2820     // The width of the result has shrunk. Use sign/zero extension to describe
2821     // the source variable's high bits.
2822     auto SignOrZeroExt = [&](DbgVariableIntrinsic &DII) -> DbgValReplacement {
2823       DILocalVariable *Var = DII.getVariable();
2824 
2825       // Without knowing signedness, sign/zero extension isn't possible.
2826       auto Signedness = Var->getSignedness();
2827       if (!Signedness)
2828         return std::nullopt;
2829 
2830       bool Signed = *Signedness == DIBasicType::Signedness::Signed;
2831       return DIExpression::appendExt(DII.getExpression(), ToBits, FromBits,
2832                                      Signed);
2833     };
2834     // RemoveDIs: duplicate implementation working on DbgVariableRecords rather
2835     // than on dbg.value intrinsics.
2836     auto SignOrZeroExtDVR = [&](DbgVariableRecord &DVR) -> DbgValReplacement {
2837       DILocalVariable *Var = DVR.getVariable();
2838 
2839       // Without knowing signedness, sign/zero extension isn't possible.
2840       auto Signedness = Var->getSignedness();
2841       if (!Signedness)
2842         return std::nullopt;
2843 
2844       bool Signed = *Signedness == DIBasicType::Signedness::Signed;
2845       return DIExpression::appendExt(DVR.getExpression(), ToBits, FromBits,
2846                                      Signed);
2847     };
2848     return rewriteDebugUsers(From, To, DomPoint, DT, SignOrZeroExt,
2849                              SignOrZeroExtDVR);
2850   }
2851 
2852   // TODO: Floating-point conversions, vectors.
2853   return false;
2854 }
2855 
2856 bool llvm::handleUnreachableTerminator(
2857     Instruction *I, SmallVectorImpl<Value *> &PoisonedValues) {
2858   bool Changed = false;
2859   // RemoveDIs: erase debug-info on this instruction manually.
2860   I->dropDbgRecords();
2861   for (Use &U : I->operands()) {
2862     Value *Op = U.get();
2863     if (isa<Instruction>(Op) && !Op->getType()->isTokenTy()) {
2864       U.set(PoisonValue::get(Op->getType()));
2865       PoisonedValues.push_back(Op);
2866       Changed = true;
2867     }
2868   }
2869 
2870   return Changed;
2871 }
2872 
2873 std::pair<unsigned, unsigned>
2874 llvm::removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB) {
2875   unsigned NumDeadInst = 0;
2876   unsigned NumDeadDbgInst = 0;
2877   // Delete the instructions backwards, as it has a reduced likelihood of
2878   // having to update as many def-use and use-def chains.
2879   Instruction *EndInst = BB->getTerminator(); // Last not to be deleted.
2880   SmallVector<Value *> Uses;
2881   handleUnreachableTerminator(EndInst, Uses);
2882 
2883   while (EndInst != &BB->front()) {
2884     // Delete the next to last instruction.
2885     Instruction *Inst = &*--EndInst->getIterator();
2886     if (!Inst->use_empty() && !Inst->getType()->isTokenTy())
2887       Inst->replaceAllUsesWith(PoisonValue::get(Inst->getType()));
2888     if (Inst->isEHPad() || Inst->getType()->isTokenTy()) {
2889       // EHPads can't have DbgVariableRecords attached to them, but it might be
2890       // possible for things with token type.
2891       Inst->dropDbgRecords();
2892       EndInst = Inst;
2893       continue;
2894     }
2895     if (isa<DbgInfoIntrinsic>(Inst))
2896       ++NumDeadDbgInst;
2897     else
2898       ++NumDeadInst;
2899     // RemoveDIs: erasing debug-info must be done manually.
2900     Inst->dropDbgRecords();
2901     Inst->eraseFromParent();
2902   }
2903   return {NumDeadInst, NumDeadDbgInst};
2904 }
2905 
2906 unsigned llvm::changeToUnreachable(Instruction *I, bool PreserveLCSSA,
2907                                    DomTreeUpdater *DTU,
2908                                    MemorySSAUpdater *MSSAU) {
2909   BasicBlock *BB = I->getParent();
2910 
2911   if (MSSAU)
2912     MSSAU->changeToUnreachable(I);
2913 
2914   SmallSet<BasicBlock *, 8> UniqueSuccessors;
2915 
2916   // Loop over all of the successors, removing BB's entry from any PHI
2917   // nodes.
2918   for (BasicBlock *Successor : successors(BB)) {
2919     Successor->removePredecessor(BB, PreserveLCSSA);
2920     if (DTU)
2921       UniqueSuccessors.insert(Successor);
2922   }
2923   auto *UI = new UnreachableInst(I->getContext(), I->getIterator());
2924   UI->setDebugLoc(I->getDebugLoc());
2925 
2926   // All instructions after this are dead.
2927   unsigned NumInstrsRemoved = 0;
2928   BasicBlock::iterator BBI = I->getIterator(), BBE = BB->end();
2929   while (BBI != BBE) {
2930     if (!BBI->use_empty())
2931       BBI->replaceAllUsesWith(PoisonValue::get(BBI->getType()));
2932     BBI++->eraseFromParent();
2933     ++NumInstrsRemoved;
2934   }
2935   if (DTU) {
2936     SmallVector<DominatorTree::UpdateType, 8> Updates;
2937     Updates.reserve(UniqueSuccessors.size());
2938     for (BasicBlock *UniqueSuccessor : UniqueSuccessors)
2939       Updates.push_back({DominatorTree::Delete, BB, UniqueSuccessor});
2940     DTU->applyUpdates(Updates);
2941   }
2942   BB->flushTerminatorDbgRecords();
2943   return NumInstrsRemoved;
2944 }
2945 
2946 CallInst *llvm::createCallMatchingInvoke(InvokeInst *II) {
2947   SmallVector<Value *, 8> Args(II->args());
2948   SmallVector<OperandBundleDef, 1> OpBundles;
2949   II->getOperandBundlesAsDefs(OpBundles);
2950   CallInst *NewCall = CallInst::Create(II->getFunctionType(),
2951                                        II->getCalledOperand(), Args, OpBundles);
2952   NewCall->setCallingConv(II->getCallingConv());
2953   NewCall->setAttributes(II->getAttributes());
2954   NewCall->setDebugLoc(II->getDebugLoc());
2955   NewCall->copyMetadata(*II);
2956 
2957   // If the invoke had profile metadata, try converting them for CallInst.
2958   uint64_t TotalWeight;
2959   if (NewCall->extractProfTotalWeight(TotalWeight)) {
2960     // Set the total weight if it fits into i32, otherwise reset.
2961     MDBuilder MDB(NewCall->getContext());
2962     auto NewWeights = uint32_t(TotalWeight) != TotalWeight
2963                           ? nullptr
2964                           : MDB.createBranchWeights({uint32_t(TotalWeight)});
2965     NewCall->setMetadata(LLVMContext::MD_prof, NewWeights);
2966   }
2967 
2968   return NewCall;
2969 }
2970 
2971 // changeToCall - Convert the specified invoke into a normal call.
2972 CallInst *llvm::changeToCall(InvokeInst *II, DomTreeUpdater *DTU) {
2973   CallInst *NewCall = createCallMatchingInvoke(II);
2974   NewCall->takeName(II);
2975   NewCall->insertBefore(II);
2976   II->replaceAllUsesWith(NewCall);
2977 
2978   // Follow the call by a branch to the normal destination.
2979   BasicBlock *NormalDestBB = II->getNormalDest();
2980   BranchInst::Create(NormalDestBB, II->getIterator());
2981 
2982   // Update PHI nodes in the unwind destination
2983   BasicBlock *BB = II->getParent();
2984   BasicBlock *UnwindDestBB = II->getUnwindDest();
2985   UnwindDestBB->removePredecessor(BB);
2986   II->eraseFromParent();
2987   if (DTU)
2988     DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDestBB}});
2989   return NewCall;
2990 }
2991 
2992 BasicBlock *llvm::changeToInvokeAndSplitBasicBlock(CallInst *CI,
2993                                                    BasicBlock *UnwindEdge,
2994                                                    DomTreeUpdater *DTU) {
2995   BasicBlock *BB = CI->getParent();
2996 
2997   // Convert this function call into an invoke instruction.  First, split the
2998   // basic block.
2999   BasicBlock *Split = SplitBlock(BB, CI, DTU, /*LI=*/nullptr, /*MSSAU*/ nullptr,
3000                                  CI->getName() + ".noexc");
3001 
3002   // Delete the unconditional branch inserted by SplitBlock
3003   BB->back().eraseFromParent();
3004 
3005   // Create the new invoke instruction.
3006   SmallVector<Value *, 8> InvokeArgs(CI->args());
3007   SmallVector<OperandBundleDef, 1> OpBundles;
3008 
3009   CI->getOperandBundlesAsDefs(OpBundles);
3010 
3011   // Note: we're round tripping operand bundles through memory here, and that
3012   // can potentially be avoided with a cleverer API design that we do not have
3013   // as of this time.
3014 
3015   InvokeInst *II =
3016       InvokeInst::Create(CI->getFunctionType(), CI->getCalledOperand(), Split,
3017                          UnwindEdge, InvokeArgs, OpBundles, CI->getName(), BB);
3018   II->setDebugLoc(CI->getDebugLoc());
3019   II->setCallingConv(CI->getCallingConv());
3020   II->setAttributes(CI->getAttributes());
3021   II->setMetadata(LLVMContext::MD_prof, CI->getMetadata(LLVMContext::MD_prof));
3022 
3023   if (DTU)
3024     DTU->applyUpdates({{DominatorTree::Insert, BB, UnwindEdge}});
3025 
3026   // Make sure that anything using the call now uses the invoke!  This also
3027   // updates the CallGraph if present, because it uses a WeakTrackingVH.
3028   CI->replaceAllUsesWith(II);
3029 
3030   // Delete the original call
3031   Split->front().eraseFromParent();
3032   return Split;
3033 }
3034 
3035 static bool markAliveBlocks(Function &F,
3036                             SmallPtrSetImpl<BasicBlock *> &Reachable,
3037                             DomTreeUpdater *DTU = nullptr) {
3038   SmallVector<BasicBlock*, 128> Worklist;
3039   BasicBlock *BB = &F.front();
3040   Worklist.push_back(BB);
3041   Reachable.insert(BB);
3042   bool Changed = false;
3043   do {
3044     BB = Worklist.pop_back_val();
3045 
3046     // Do a quick scan of the basic block, turning any obviously unreachable
3047     // instructions into LLVM unreachable insts.  The instruction combining pass
3048     // canonicalizes unreachable insts into stores to null or undef.
3049     for (Instruction &I : *BB) {
3050       if (auto *CI = dyn_cast<CallInst>(&I)) {
3051         Value *Callee = CI->getCalledOperand();
3052         // Handle intrinsic calls.
3053         if (Function *F = dyn_cast<Function>(Callee)) {
3054           auto IntrinsicID = F->getIntrinsicID();
3055           // Assumptions that are known to be false are equivalent to
3056           // unreachable. Also, if the condition is undefined, then we make the
3057           // choice most beneficial to the optimizer, and choose that to also be
3058           // unreachable.
3059           if (IntrinsicID == Intrinsic::assume) {
3060             if (match(CI->getArgOperand(0), m_CombineOr(m_Zero(), m_Undef()))) {
3061               // Don't insert a call to llvm.trap right before the unreachable.
3062               changeToUnreachable(CI, false, DTU);
3063               Changed = true;
3064               break;
3065             }
3066           } else if (IntrinsicID == Intrinsic::experimental_guard) {
3067             // A call to the guard intrinsic bails out of the current
3068             // compilation unit if the predicate passed to it is false. If the
3069             // predicate is a constant false, then we know the guard will bail
3070             // out of the current compile unconditionally, so all code following
3071             // it is dead.
3072             //
3073             // Note: unlike in llvm.assume, it is not "obviously profitable" for
3074             // guards to treat `undef` as `false` since a guard on `undef` can
3075             // still be useful for widening.
3076             if (match(CI->getArgOperand(0), m_Zero()))
3077               if (!isa<UnreachableInst>(CI->getNextNode())) {
3078                 changeToUnreachable(CI->getNextNode(), false, DTU);
3079                 Changed = true;
3080                 break;
3081               }
3082           }
3083         } else if ((isa<ConstantPointerNull>(Callee) &&
3084                     !NullPointerIsDefined(CI->getFunction(),
3085                                           cast<PointerType>(Callee->getType())
3086                                               ->getAddressSpace())) ||
3087                    isa<UndefValue>(Callee)) {
3088           changeToUnreachable(CI, false, DTU);
3089           Changed = true;
3090           break;
3091         }
3092         if (CI->doesNotReturn() && !CI->isMustTailCall()) {
3093           // If we found a call to a no-return function, insert an unreachable
3094           // instruction after it.  Make sure there isn't *already* one there
3095           // though.
3096           if (!isa<UnreachableInst>(CI->getNextNonDebugInstruction())) {
3097             // Don't insert a call to llvm.trap right before the unreachable.
3098             changeToUnreachable(CI->getNextNonDebugInstruction(), false, DTU);
3099             Changed = true;
3100           }
3101           break;
3102         }
3103       } else if (auto *SI = dyn_cast<StoreInst>(&I)) {
3104         // Store to undef and store to null are undefined and used to signal
3105         // that they should be changed to unreachable by passes that can't
3106         // modify the CFG.
3107 
3108         // Don't touch volatile stores.
3109         if (SI->isVolatile()) continue;
3110 
3111         Value *Ptr = SI->getOperand(1);
3112 
3113         if (isa<UndefValue>(Ptr) ||
3114             (isa<ConstantPointerNull>(Ptr) &&
3115              !NullPointerIsDefined(SI->getFunction(),
3116                                    SI->getPointerAddressSpace()))) {
3117           changeToUnreachable(SI, false, DTU);
3118           Changed = true;
3119           break;
3120         }
3121       }
3122     }
3123 
3124     Instruction *Terminator = BB->getTerminator();
3125     if (auto *II = dyn_cast<InvokeInst>(Terminator)) {
3126       // Turn invokes that call 'nounwind' functions into ordinary calls.
3127       Value *Callee = II->getCalledOperand();
3128       if ((isa<ConstantPointerNull>(Callee) &&
3129            !NullPointerIsDefined(BB->getParent())) ||
3130           isa<UndefValue>(Callee)) {
3131         changeToUnreachable(II, false, DTU);
3132         Changed = true;
3133       } else {
3134         if (II->doesNotReturn() &&
3135             !isa<UnreachableInst>(II->getNormalDest()->front())) {
3136           // If we found an invoke of a no-return function,
3137           // create a new empty basic block with an `unreachable` terminator,
3138           // and set it as the normal destination for the invoke,
3139           // unless that is already the case.
3140           // Note that the original normal destination could have other uses.
3141           BasicBlock *OrigNormalDest = II->getNormalDest();
3142           OrigNormalDest->removePredecessor(II->getParent());
3143           LLVMContext &Ctx = II->getContext();
3144           BasicBlock *UnreachableNormalDest = BasicBlock::Create(
3145               Ctx, OrigNormalDest->getName() + ".unreachable",
3146               II->getFunction(), OrigNormalDest);
3147           new UnreachableInst(Ctx, UnreachableNormalDest);
3148           II->setNormalDest(UnreachableNormalDest);
3149           if (DTU)
3150             DTU->applyUpdates(
3151                 {{DominatorTree::Delete, BB, OrigNormalDest},
3152                  {DominatorTree::Insert, BB, UnreachableNormalDest}});
3153           Changed = true;
3154         }
3155         if (II->doesNotThrow() && canSimplifyInvokeNoUnwind(&F)) {
3156           if (II->use_empty() && !II->mayHaveSideEffects()) {
3157             // jump to the normal destination branch.
3158             BasicBlock *NormalDestBB = II->getNormalDest();
3159             BasicBlock *UnwindDestBB = II->getUnwindDest();
3160             BranchInst::Create(NormalDestBB, II->getIterator());
3161             UnwindDestBB->removePredecessor(II->getParent());
3162             II->eraseFromParent();
3163             if (DTU)
3164               DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDestBB}});
3165           } else
3166             changeToCall(II, DTU);
3167           Changed = true;
3168         }
3169       }
3170     } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Terminator)) {
3171       // Remove catchpads which cannot be reached.
3172       struct CatchPadDenseMapInfo {
3173         static CatchPadInst *getEmptyKey() {
3174           return DenseMapInfo<CatchPadInst *>::getEmptyKey();
3175         }
3176 
3177         static CatchPadInst *getTombstoneKey() {
3178           return DenseMapInfo<CatchPadInst *>::getTombstoneKey();
3179         }
3180 
3181         static unsigned getHashValue(CatchPadInst *CatchPad) {
3182           return static_cast<unsigned>(hash_combine_range(
3183               CatchPad->value_op_begin(), CatchPad->value_op_end()));
3184         }
3185 
3186         static bool isEqual(CatchPadInst *LHS, CatchPadInst *RHS) {
3187           if (LHS == getEmptyKey() || LHS == getTombstoneKey() ||
3188               RHS == getEmptyKey() || RHS == getTombstoneKey())
3189             return LHS == RHS;
3190           return LHS->isIdenticalTo(RHS);
3191         }
3192       };
3193 
3194       SmallDenseMap<BasicBlock *, int, 8> NumPerSuccessorCases;
3195       // Set of unique CatchPads.
3196       SmallDenseMap<CatchPadInst *, detail::DenseSetEmpty, 4,
3197                     CatchPadDenseMapInfo, detail::DenseSetPair<CatchPadInst *>>
3198           HandlerSet;
3199       detail::DenseSetEmpty Empty;
3200       for (CatchSwitchInst::handler_iterator I = CatchSwitch->handler_begin(),
3201                                              E = CatchSwitch->handler_end();
3202            I != E; ++I) {
3203         BasicBlock *HandlerBB = *I;
3204         if (DTU)
3205           ++NumPerSuccessorCases[HandlerBB];
3206         auto *CatchPad = cast<CatchPadInst>(HandlerBB->getFirstNonPHI());
3207         if (!HandlerSet.insert({CatchPad, Empty}).second) {
3208           if (DTU)
3209             --NumPerSuccessorCases[HandlerBB];
3210           CatchSwitch->removeHandler(I);
3211           --I;
3212           --E;
3213           Changed = true;
3214         }
3215       }
3216       if (DTU) {
3217         std::vector<DominatorTree::UpdateType> Updates;
3218         for (const std::pair<BasicBlock *, int> &I : NumPerSuccessorCases)
3219           if (I.second == 0)
3220             Updates.push_back({DominatorTree::Delete, BB, I.first});
3221         DTU->applyUpdates(Updates);
3222       }
3223     }
3224 
3225     Changed |= ConstantFoldTerminator(BB, true, nullptr, DTU);
3226     for (BasicBlock *Successor : successors(BB))
3227       if (Reachable.insert(Successor).second)
3228         Worklist.push_back(Successor);
3229   } while (!Worklist.empty());
3230   return Changed;
3231 }
3232 
3233 Instruction *llvm::removeUnwindEdge(BasicBlock *BB, DomTreeUpdater *DTU) {
3234   Instruction *TI = BB->getTerminator();
3235 
3236   if (auto *II = dyn_cast<InvokeInst>(TI))
3237     return changeToCall(II, DTU);
3238 
3239   Instruction *NewTI;
3240   BasicBlock *UnwindDest;
3241 
3242   if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) {
3243     NewTI = CleanupReturnInst::Create(CRI->getCleanupPad(), nullptr, CRI->getIterator());
3244     UnwindDest = CRI->getUnwindDest();
3245   } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(TI)) {
3246     auto *NewCatchSwitch = CatchSwitchInst::Create(
3247         CatchSwitch->getParentPad(), nullptr, CatchSwitch->getNumHandlers(),
3248         CatchSwitch->getName(), CatchSwitch->getIterator());
3249     for (BasicBlock *PadBB : CatchSwitch->handlers())
3250       NewCatchSwitch->addHandler(PadBB);
3251 
3252     NewTI = NewCatchSwitch;
3253     UnwindDest = CatchSwitch->getUnwindDest();
3254   } else {
3255     llvm_unreachable("Could not find unwind successor");
3256   }
3257 
3258   NewTI->takeName(TI);
3259   NewTI->setDebugLoc(TI->getDebugLoc());
3260   UnwindDest->removePredecessor(BB);
3261   TI->replaceAllUsesWith(NewTI);
3262   TI->eraseFromParent();
3263   if (DTU)
3264     DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDest}});
3265   return NewTI;
3266 }
3267 
3268 /// removeUnreachableBlocks - Remove blocks that are not reachable, even
3269 /// if they are in a dead cycle.  Return true if a change was made, false
3270 /// otherwise.
3271 bool llvm::removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU,
3272                                    MemorySSAUpdater *MSSAU) {
3273   SmallPtrSet<BasicBlock *, 16> Reachable;
3274   bool Changed = markAliveBlocks(F, Reachable, DTU);
3275 
3276   // If there are unreachable blocks in the CFG...
3277   if (Reachable.size() == F.size())
3278     return Changed;
3279 
3280   assert(Reachable.size() < F.size());
3281 
3282   // Are there any blocks left to actually delete?
3283   SmallSetVector<BasicBlock *, 8> BlocksToRemove;
3284   for (BasicBlock &BB : F) {
3285     // Skip reachable basic blocks
3286     if (Reachable.count(&BB))
3287       continue;
3288     // Skip already-deleted blocks
3289     if (DTU && DTU->isBBPendingDeletion(&BB))
3290       continue;
3291     BlocksToRemove.insert(&BB);
3292   }
3293 
3294   if (BlocksToRemove.empty())
3295     return Changed;
3296 
3297   Changed = true;
3298   NumRemoved += BlocksToRemove.size();
3299 
3300   if (MSSAU)
3301     MSSAU->removeBlocks(BlocksToRemove);
3302 
3303   DeleteDeadBlocks(BlocksToRemove.takeVector(), DTU);
3304 
3305   return Changed;
3306 }
3307 
3308 void llvm::combineMetadata(Instruction *K, const Instruction *J,
3309                            ArrayRef<unsigned> KnownIDs, bool DoesKMove) {
3310   SmallVector<std::pair<unsigned, MDNode *>, 4> Metadata;
3311   K->dropUnknownNonDebugMetadata(KnownIDs);
3312   K->getAllMetadataOtherThanDebugLoc(Metadata);
3313   for (const auto &MD : Metadata) {
3314     unsigned Kind = MD.first;
3315     MDNode *JMD = J->getMetadata(Kind);
3316     MDNode *KMD = MD.second;
3317 
3318     switch (Kind) {
3319       default:
3320         K->setMetadata(Kind, nullptr); // Remove unknown metadata
3321         break;
3322       case LLVMContext::MD_dbg:
3323         llvm_unreachable("getAllMetadataOtherThanDebugLoc returned a MD_dbg");
3324       case LLVMContext::MD_DIAssignID:
3325         K->mergeDIAssignID(J);
3326         break;
3327       case LLVMContext::MD_tbaa:
3328         if (DoesKMove)
3329           K->setMetadata(Kind, MDNode::getMostGenericTBAA(JMD, KMD));
3330         break;
3331       case LLVMContext::MD_alias_scope:
3332         if (DoesKMove)
3333           K->setMetadata(Kind, MDNode::getMostGenericAliasScope(JMD, KMD));
3334         break;
3335       case LLVMContext::MD_noalias:
3336       case LLVMContext::MD_mem_parallel_loop_access:
3337         if (DoesKMove)
3338           K->setMetadata(Kind, MDNode::intersect(JMD, KMD));
3339         break;
3340       case LLVMContext::MD_access_group:
3341         if (DoesKMove)
3342           K->setMetadata(LLVMContext::MD_access_group,
3343                          intersectAccessGroups(K, J));
3344         break;
3345       case LLVMContext::MD_range:
3346         if (DoesKMove || !K->hasMetadata(LLVMContext::MD_noundef))
3347           K->setMetadata(Kind, MDNode::getMostGenericRange(JMD, KMD));
3348         break;
3349       case LLVMContext::MD_fpmath:
3350         K->setMetadata(Kind, MDNode::getMostGenericFPMath(JMD, KMD));
3351         break;
3352       case LLVMContext::MD_invariant_load:
3353         // If K moves, only set the !invariant.load if it is present in both
3354         // instructions.
3355         if (DoesKMove)
3356           K->setMetadata(Kind, JMD);
3357         break;
3358       case LLVMContext::MD_nonnull:
3359         if (DoesKMove || !K->hasMetadata(LLVMContext::MD_noundef))
3360           K->setMetadata(Kind, JMD);
3361         break;
3362       case LLVMContext::MD_invariant_group:
3363         // Preserve !invariant.group in K.
3364         break;
3365       case LLVMContext::MD_mmra:
3366         // Combine MMRAs
3367         break;
3368       case LLVMContext::MD_align:
3369         if (DoesKMove || !K->hasMetadata(LLVMContext::MD_noundef))
3370           K->setMetadata(
3371               Kind, MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD));
3372         break;
3373       case LLVMContext::MD_dereferenceable:
3374       case LLVMContext::MD_dereferenceable_or_null:
3375         if (DoesKMove)
3376           K->setMetadata(Kind,
3377             MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD));
3378         break;
3379       case LLVMContext::MD_preserve_access_index:
3380         // Preserve !preserve.access.index in K.
3381         break;
3382       case LLVMContext::MD_noundef:
3383         // If K does move, keep noundef if it is present in both instructions.
3384         if (DoesKMove)
3385           K->setMetadata(Kind, JMD);
3386         break;
3387       case LLVMContext::MD_nontemporal:
3388         // Preserve !nontemporal if it is present on both instructions.
3389         K->setMetadata(Kind, JMD);
3390         break;
3391       case LLVMContext::MD_prof:
3392         if (DoesKMove)
3393           K->setMetadata(Kind, MDNode::getMergedProfMetadata(KMD, JMD, K, J));
3394         break;
3395       case LLVMContext::MD_noalias_addrspace:
3396         if (DoesKMove)
3397           K->setMetadata(Kind,
3398                          MDNode::getMostGenericNoaliasAddrspace(JMD, KMD));
3399         break;
3400     }
3401   }
3402   // Set !invariant.group from J if J has it. If both instructions have it
3403   // then we will just pick it from J - even when they are different.
3404   // Also make sure that K is load or store - f.e. combining bitcast with load
3405   // could produce bitcast with invariant.group metadata, which is invalid.
3406   // FIXME: we should try to preserve both invariant.group md if they are
3407   // different, but right now instruction can only have one invariant.group.
3408   if (auto *JMD = J->getMetadata(LLVMContext::MD_invariant_group))
3409     if (isa<LoadInst>(K) || isa<StoreInst>(K))
3410       K->setMetadata(LLVMContext::MD_invariant_group, JMD);
3411 
3412   // Merge MMRAs.
3413   // This is handled separately because we also want to handle cases where K
3414   // doesn't have tags but J does.
3415   auto JMMRA = J->getMetadata(LLVMContext::MD_mmra);
3416   auto KMMRA = K->getMetadata(LLVMContext::MD_mmra);
3417   if (JMMRA || KMMRA) {
3418     K->setMetadata(LLVMContext::MD_mmra,
3419                    MMRAMetadata::combine(K->getContext(), JMMRA, KMMRA));
3420   }
3421 }
3422 
3423 void llvm::combineMetadataForCSE(Instruction *K, const Instruction *J,
3424                                  bool KDominatesJ) {
3425   unsigned KnownIDs[] = {LLVMContext::MD_tbaa,
3426                          LLVMContext::MD_alias_scope,
3427                          LLVMContext::MD_noalias,
3428                          LLVMContext::MD_range,
3429                          LLVMContext::MD_fpmath,
3430                          LLVMContext::MD_invariant_load,
3431                          LLVMContext::MD_nonnull,
3432                          LLVMContext::MD_invariant_group,
3433                          LLVMContext::MD_align,
3434                          LLVMContext::MD_dereferenceable,
3435                          LLVMContext::MD_dereferenceable_or_null,
3436                          LLVMContext::MD_access_group,
3437                          LLVMContext::MD_preserve_access_index,
3438                          LLVMContext::MD_prof,
3439                          LLVMContext::MD_nontemporal,
3440                          LLVMContext::MD_noundef,
3441                          LLVMContext::MD_mmra,
3442                          LLVMContext::MD_noalias_addrspace};
3443   combineMetadata(K, J, KnownIDs, KDominatesJ);
3444 }
3445 
3446 void llvm::copyMetadataForLoad(LoadInst &Dest, const LoadInst &Source) {
3447   SmallVector<std::pair<unsigned, MDNode *>, 8> MD;
3448   Source.getAllMetadata(MD);
3449   MDBuilder MDB(Dest.getContext());
3450   Type *NewType = Dest.getType();
3451   const DataLayout &DL = Source.getDataLayout();
3452   for (const auto &MDPair : MD) {
3453     unsigned ID = MDPair.first;
3454     MDNode *N = MDPair.second;
3455     // Note, essentially every kind of metadata should be preserved here! This
3456     // routine is supposed to clone a load instruction changing *only its type*.
3457     // The only metadata it makes sense to drop is metadata which is invalidated
3458     // when the pointer type changes. This should essentially never be the case
3459     // in LLVM, but we explicitly switch over only known metadata to be
3460     // conservatively correct. If you are adding metadata to LLVM which pertains
3461     // to loads, you almost certainly want to add it here.
3462     switch (ID) {
3463     case LLVMContext::MD_dbg:
3464     case LLVMContext::MD_tbaa:
3465     case LLVMContext::MD_prof:
3466     case LLVMContext::MD_fpmath:
3467     case LLVMContext::MD_tbaa_struct:
3468     case LLVMContext::MD_invariant_load:
3469     case LLVMContext::MD_alias_scope:
3470     case LLVMContext::MD_noalias:
3471     case LLVMContext::MD_nontemporal:
3472     case LLVMContext::MD_mem_parallel_loop_access:
3473     case LLVMContext::MD_access_group:
3474     case LLVMContext::MD_noundef:
3475       // All of these directly apply.
3476       Dest.setMetadata(ID, N);
3477       break;
3478 
3479     case LLVMContext::MD_nonnull:
3480       copyNonnullMetadata(Source, N, Dest);
3481       break;
3482 
3483     case LLVMContext::MD_align:
3484     case LLVMContext::MD_dereferenceable:
3485     case LLVMContext::MD_dereferenceable_or_null:
3486       // These only directly apply if the new type is also a pointer.
3487       if (NewType->isPointerTy())
3488         Dest.setMetadata(ID, N);
3489       break;
3490 
3491     case LLVMContext::MD_range:
3492       copyRangeMetadata(DL, Source, N, Dest);
3493       break;
3494     }
3495   }
3496 }
3497 
3498 void llvm::patchReplacementInstruction(Instruction *I, Value *Repl) {
3499   auto *ReplInst = dyn_cast<Instruction>(Repl);
3500   if (!ReplInst)
3501     return;
3502 
3503   // Patch the replacement so that it is not more restrictive than the value
3504   // being replaced.
3505   WithOverflowInst *UnusedWO;
3506   // When replacing the result of a llvm.*.with.overflow intrinsic with a
3507   // overflowing binary operator, nuw/nsw flags may no longer hold.
3508   if (isa<OverflowingBinaryOperator>(ReplInst) &&
3509       match(I, m_ExtractValue<0>(m_WithOverflowInst(UnusedWO))))
3510     ReplInst->dropPoisonGeneratingFlags();
3511   // Note that if 'I' is a load being replaced by some operation,
3512   // for example, by an arithmetic operation, then andIRFlags()
3513   // would just erase all math flags from the original arithmetic
3514   // operation, which is clearly not wanted and not needed.
3515   else if (!isa<LoadInst>(I))
3516     ReplInst->andIRFlags(I);
3517 
3518   // Handle attributes.
3519   if (auto *CB1 = dyn_cast<CallBase>(ReplInst)) {
3520     if (auto *CB2 = dyn_cast<CallBase>(I)) {
3521       bool Success = CB1->tryIntersectAttributes(CB2);
3522       assert(Success && "We should not be trying to sink callbases "
3523                         "with non-intersectable attributes");
3524       // For NDEBUG Compile.
3525       (void)Success;
3526     }
3527   }
3528 
3529   // FIXME: If both the original and replacement value are part of the
3530   // same control-flow region (meaning that the execution of one
3531   // guarantees the execution of the other), then we can combine the
3532   // noalias scopes here and do better than the general conservative
3533   // answer used in combineMetadata().
3534 
3535   // In general, GVN unifies expressions over different control-flow
3536   // regions, and so we need a conservative combination of the noalias
3537   // scopes.
3538   combineMetadataForCSE(ReplInst, I, false);
3539 }
3540 
3541 template <typename RootType, typename ShouldReplaceFn>
3542 static unsigned replaceDominatedUsesWith(Value *From, Value *To,
3543                                          const RootType &Root,
3544                                          const ShouldReplaceFn &ShouldReplace) {
3545   assert(From->getType() == To->getType());
3546 
3547   unsigned Count = 0;
3548   for (Use &U : llvm::make_early_inc_range(From->uses())) {
3549     auto *II = dyn_cast<IntrinsicInst>(U.getUser());
3550     if (II && II->getIntrinsicID() == Intrinsic::fake_use)
3551       continue;
3552     if (!ShouldReplace(Root, U))
3553       continue;
3554     LLVM_DEBUG(dbgs() << "Replace dominated use of '";
3555                From->printAsOperand(dbgs());
3556                dbgs() << "' with " << *To << " in " << *U.getUser() << "\n");
3557     U.set(To);
3558     ++Count;
3559   }
3560   return Count;
3561 }
3562 
3563 unsigned llvm::replaceNonLocalUsesWith(Instruction *From, Value *To) {
3564    assert(From->getType() == To->getType());
3565    auto *BB = From->getParent();
3566    unsigned Count = 0;
3567 
3568    for (Use &U : llvm::make_early_inc_range(From->uses())) {
3569     auto *I = cast<Instruction>(U.getUser());
3570     if (I->getParent() == BB)
3571       continue;
3572     U.set(To);
3573     ++Count;
3574   }
3575   return Count;
3576 }
3577 
3578 unsigned llvm::replaceDominatedUsesWith(Value *From, Value *To,
3579                                         DominatorTree &DT,
3580                                         const BasicBlockEdge &Root) {
3581   auto Dominates = [&DT](const BasicBlockEdge &Root, const Use &U) {
3582     return DT.dominates(Root, U);
3583   };
3584   return ::replaceDominatedUsesWith(From, To, Root, Dominates);
3585 }
3586 
3587 unsigned llvm::replaceDominatedUsesWith(Value *From, Value *To,
3588                                         DominatorTree &DT,
3589                                         const BasicBlock *BB) {
3590   auto Dominates = [&DT](const BasicBlock *BB, const Use &U) {
3591     return DT.dominates(BB, U);
3592   };
3593   return ::replaceDominatedUsesWith(From, To, BB, Dominates);
3594 }
3595 
3596 unsigned llvm::replaceDominatedUsesWithIf(
3597     Value *From, Value *To, DominatorTree &DT, const BasicBlockEdge &Root,
3598     function_ref<bool(const Use &U, const Value *To)> ShouldReplace) {
3599   auto DominatesAndShouldReplace =
3600       [&DT, &ShouldReplace, To](const BasicBlockEdge &Root, const Use &U) {
3601         return DT.dominates(Root, U) && ShouldReplace(U, To);
3602       };
3603   return ::replaceDominatedUsesWith(From, To, Root, DominatesAndShouldReplace);
3604 }
3605 
3606 unsigned llvm::replaceDominatedUsesWithIf(
3607     Value *From, Value *To, DominatorTree &DT, const BasicBlock *BB,
3608     function_ref<bool(const Use &U, const Value *To)> ShouldReplace) {
3609   auto DominatesAndShouldReplace = [&DT, &ShouldReplace,
3610                                     To](const BasicBlock *BB, const Use &U) {
3611     return DT.dominates(BB, U) && ShouldReplace(U, To);
3612   };
3613   return ::replaceDominatedUsesWith(From, To, BB, DominatesAndShouldReplace);
3614 }
3615 
3616 bool llvm::callsGCLeafFunction(const CallBase *Call,
3617                                const TargetLibraryInfo &TLI) {
3618   // Check if the function is specifically marked as a gc leaf function.
3619   if (Call->hasFnAttr("gc-leaf-function"))
3620     return true;
3621   if (const Function *F = Call->getCalledFunction()) {
3622     if (F->hasFnAttribute("gc-leaf-function"))
3623       return true;
3624 
3625     if (auto IID = F->getIntrinsicID()) {
3626       // Most LLVM intrinsics do not take safepoints.
3627       return IID != Intrinsic::experimental_gc_statepoint &&
3628              IID != Intrinsic::experimental_deoptimize &&
3629              IID != Intrinsic::memcpy_element_unordered_atomic &&
3630              IID != Intrinsic::memmove_element_unordered_atomic;
3631     }
3632   }
3633 
3634   // Lib calls can be materialized by some passes, and won't be
3635   // marked as 'gc-leaf-function.' All available Libcalls are
3636   // GC-leaf.
3637   LibFunc LF;
3638   if (TLI.getLibFunc(*Call, LF)) {
3639     return TLI.has(LF);
3640   }
3641 
3642   return false;
3643 }
3644 
3645 void llvm::copyNonnullMetadata(const LoadInst &OldLI, MDNode *N,
3646                                LoadInst &NewLI) {
3647   auto *NewTy = NewLI.getType();
3648 
3649   // This only directly applies if the new type is also a pointer.
3650   if (NewTy->isPointerTy()) {
3651     NewLI.setMetadata(LLVMContext::MD_nonnull, N);
3652     return;
3653   }
3654 
3655   // The only other translation we can do is to integral loads with !range
3656   // metadata.
3657   if (!NewTy->isIntegerTy())
3658     return;
3659 
3660   MDBuilder MDB(NewLI.getContext());
3661   const Value *Ptr = OldLI.getPointerOperand();
3662   auto *ITy = cast<IntegerType>(NewTy);
3663   auto *NullInt = ConstantExpr::getPtrToInt(
3664       ConstantPointerNull::get(cast<PointerType>(Ptr->getType())), ITy);
3665   auto *NonNullInt = ConstantExpr::getAdd(NullInt, ConstantInt::get(ITy, 1));
3666   NewLI.setMetadata(LLVMContext::MD_range,
3667                     MDB.createRange(NonNullInt, NullInt));
3668 }
3669 
3670 void llvm::copyRangeMetadata(const DataLayout &DL, const LoadInst &OldLI,
3671                              MDNode *N, LoadInst &NewLI) {
3672   auto *NewTy = NewLI.getType();
3673   // Simply copy the metadata if the type did not change.
3674   if (NewTy == OldLI.getType()) {
3675     NewLI.setMetadata(LLVMContext::MD_range, N);
3676     return;
3677   }
3678 
3679   // Give up unless it is converted to a pointer where there is a single very
3680   // valuable mapping we can do reliably.
3681   // FIXME: It would be nice to propagate this in more ways, but the type
3682   // conversions make it hard.
3683   if (!NewTy->isPointerTy())
3684     return;
3685 
3686   unsigned BitWidth = DL.getPointerTypeSizeInBits(NewTy);
3687   if (BitWidth == OldLI.getType()->getScalarSizeInBits() &&
3688       !getConstantRangeFromMetadata(*N).contains(APInt(BitWidth, 0))) {
3689     MDNode *NN = MDNode::get(OldLI.getContext(), {});
3690     NewLI.setMetadata(LLVMContext::MD_nonnull, NN);
3691   }
3692 }
3693 
3694 void llvm::dropDebugUsers(Instruction &I) {
3695   SmallVector<DbgVariableIntrinsic *, 1> DbgUsers;
3696   SmallVector<DbgVariableRecord *, 1> DPUsers;
3697   findDbgUsers(DbgUsers, &I, &DPUsers);
3698   for (auto *DII : DbgUsers)
3699     DII->eraseFromParent();
3700   for (auto *DVR : DPUsers)
3701     DVR->eraseFromParent();
3702 }
3703 
3704 void llvm::hoistAllInstructionsInto(BasicBlock *DomBlock, Instruction *InsertPt,
3705                                     BasicBlock *BB) {
3706   // Since we are moving the instructions out of its basic block, we do not
3707   // retain their original debug locations (DILocations) and debug intrinsic
3708   // instructions.
3709   //
3710   // Doing so would degrade the debugging experience and adversely affect the
3711   // accuracy of profiling information.
3712   //
3713   // Currently, when hoisting the instructions, we take the following actions:
3714   // - Remove their debug intrinsic instructions.
3715   // - Set their debug locations to the values from the insertion point.
3716   //
3717   // As per PR39141 (comment #8), the more fundamental reason why the dbg.values
3718   // need to be deleted, is because there will not be any instructions with a
3719   // DILocation in either branch left after performing the transformation. We
3720   // can only insert a dbg.value after the two branches are joined again.
3721   //
3722   // See PR38762, PR39243 for more details.
3723   //
3724   // TODO: Extend llvm.dbg.value to take more than one SSA Value (PR39141) to
3725   // encode predicated DIExpressions that yield different results on different
3726   // code paths.
3727 
3728   for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;) {
3729     Instruction *I = &*II;
3730     I->dropUBImplyingAttrsAndMetadata();
3731     if (I->isUsedByMetadata())
3732       dropDebugUsers(*I);
3733     // RemoveDIs: drop debug-info too as the following code does.
3734     I->dropDbgRecords();
3735     if (I->isDebugOrPseudoInst()) {
3736       // Remove DbgInfo and pseudo probe Intrinsics.
3737       II = I->eraseFromParent();
3738       continue;
3739     }
3740     I->setDebugLoc(InsertPt->getDebugLoc());
3741     ++II;
3742   }
3743   DomBlock->splice(InsertPt->getIterator(), BB, BB->begin(),
3744                    BB->getTerminator()->getIterator());
3745 }
3746 
3747 DIExpression *llvm::getExpressionForConstant(DIBuilder &DIB, const Constant &C,
3748                                              Type &Ty) {
3749   // Create integer constant expression.
3750   auto createIntegerExpression = [&DIB](const Constant &CV) -> DIExpression * {
3751     const APInt &API = cast<ConstantInt>(&CV)->getValue();
3752     std::optional<int64_t> InitIntOpt = API.trySExtValue();
3753     return InitIntOpt ? DIB.createConstantValueExpression(
3754                             static_cast<uint64_t>(*InitIntOpt))
3755                       : nullptr;
3756   };
3757 
3758   if (isa<ConstantInt>(C))
3759     return createIntegerExpression(C);
3760 
3761   auto *FP = dyn_cast<ConstantFP>(&C);
3762   if (FP && Ty.isFloatingPointTy() && Ty.getScalarSizeInBits() <= 64) {
3763     const APFloat &APF = FP->getValueAPF();
3764     APInt const &API = APF.bitcastToAPInt();
3765     if (auto Temp = API.getZExtValue())
3766       return DIB.createConstantValueExpression(static_cast<uint64_t>(Temp));
3767     return DIB.createConstantValueExpression(*API.getRawData());
3768   }
3769 
3770   if (!Ty.isPointerTy())
3771     return nullptr;
3772 
3773   if (isa<ConstantPointerNull>(C))
3774     return DIB.createConstantValueExpression(0);
3775 
3776   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(&C))
3777     if (CE->getOpcode() == Instruction::IntToPtr) {
3778       const Value *V = CE->getOperand(0);
3779       if (auto CI = dyn_cast_or_null<ConstantInt>(V))
3780         return createIntegerExpression(*CI);
3781     }
3782   return nullptr;
3783 }
3784 
3785 void llvm::remapDebugVariable(ValueToValueMapTy &Mapping, Instruction *Inst) {
3786   auto RemapDebugOperands = [&Mapping](auto *DV, auto Set) {
3787     for (auto *Op : Set) {
3788       auto I = Mapping.find(Op);
3789       if (I != Mapping.end())
3790         DV->replaceVariableLocationOp(Op, I->second, /*AllowEmpty=*/true);
3791     }
3792   };
3793   auto RemapAssignAddress = [&Mapping](auto *DA) {
3794     auto I = Mapping.find(DA->getAddress());
3795     if (I != Mapping.end())
3796       DA->setAddress(I->second);
3797   };
3798   if (auto DVI = dyn_cast<DbgVariableIntrinsic>(Inst))
3799     RemapDebugOperands(DVI, DVI->location_ops());
3800   if (auto DAI = dyn_cast<DbgAssignIntrinsic>(Inst))
3801     RemapAssignAddress(DAI);
3802   for (DbgVariableRecord &DVR : filterDbgVars(Inst->getDbgRecordRange())) {
3803     RemapDebugOperands(&DVR, DVR.location_ops());
3804     if (DVR.isDbgAssign())
3805       RemapAssignAddress(&DVR);
3806   }
3807 }
3808 
3809 namespace {
3810 
3811 /// A potential constituent of a bitreverse or bswap expression. See
3812 /// collectBitParts for a fuller explanation.
3813 struct BitPart {
3814   BitPart(Value *P, unsigned BW) : Provider(P) {
3815     Provenance.resize(BW);
3816   }
3817 
3818   /// The Value that this is a bitreverse/bswap of.
3819   Value *Provider;
3820 
3821   /// The "provenance" of each bit. Provenance[A] = B means that bit A
3822   /// in Provider becomes bit B in the result of this expression.
3823   SmallVector<int8_t, 32> Provenance; // int8_t means max size is i128.
3824 
3825   enum { Unset = -1 };
3826 };
3827 
3828 } // end anonymous namespace
3829 
3830 /// Analyze the specified subexpression and see if it is capable of providing
3831 /// pieces of a bswap or bitreverse. The subexpression provides a potential
3832 /// piece of a bswap or bitreverse if it can be proved that each non-zero bit in
3833 /// the output of the expression came from a corresponding bit in some other
3834 /// value. This function is recursive, and the end result is a mapping of
3835 /// bitnumber to bitnumber. It is the caller's responsibility to validate that
3836 /// the bitnumber to bitnumber mapping is correct for a bswap or bitreverse.
3837 ///
3838 /// For example, if the current subexpression if "(shl i32 %X, 24)" then we know
3839 /// that the expression deposits the low byte of %X into the high byte of the
3840 /// result and that all other bits are zero. This expression is accepted and a
3841 /// BitPart is returned with Provider set to %X and Provenance[24-31] set to
3842 /// [0-7].
3843 ///
3844 /// For vector types, all analysis is performed at the per-element level. No
3845 /// cross-element analysis is supported (shuffle/insertion/reduction), and all
3846 /// constant masks must be splatted across all elements.
3847 ///
3848 /// To avoid revisiting values, the BitPart results are memoized into the
3849 /// provided map. To avoid unnecessary copying of BitParts, BitParts are
3850 /// constructed in-place in the \c BPS map. Because of this \c BPS needs to
3851 /// store BitParts objects, not pointers. As we need the concept of a nullptr
3852 /// BitParts (Value has been analyzed and the analysis failed), we an Optional
3853 /// type instead to provide the same functionality.
3854 ///
3855 /// Because we pass around references into \c BPS, we must use a container that
3856 /// does not invalidate internal references (std::map instead of DenseMap).
3857 static const std::optional<BitPart> &
3858 collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals,
3859                 std::map<Value *, std::optional<BitPart>> &BPS, int Depth,
3860                 bool &FoundRoot) {
3861   auto [I, Inserted] = BPS.try_emplace(V);
3862   if (!Inserted)
3863     return I->second;
3864 
3865   auto &Result = I->second;
3866   auto BitWidth = V->getType()->getScalarSizeInBits();
3867 
3868   // Can't do integer/elements > 128 bits.
3869   if (BitWidth > 128)
3870     return Result;
3871 
3872   // Prevent stack overflow by limiting the recursion depth
3873   if (Depth == BitPartRecursionMaxDepth) {
3874     LLVM_DEBUG(dbgs() << "collectBitParts max recursion depth reached.\n");
3875     return Result;
3876   }
3877 
3878   if (auto *I = dyn_cast<Instruction>(V)) {
3879     Value *X, *Y;
3880     const APInt *C;
3881 
3882     // If this is an or instruction, it may be an inner node of the bswap.
3883     if (match(V, m_Or(m_Value(X), m_Value(Y)))) {
3884       // Check we have both sources and they are from the same provider.
3885       const auto &A = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
3886                                       Depth + 1, FoundRoot);
3887       if (!A || !A->Provider)
3888         return Result;
3889 
3890       const auto &B = collectBitParts(Y, MatchBSwaps, MatchBitReversals, BPS,
3891                                       Depth + 1, FoundRoot);
3892       if (!B || A->Provider != B->Provider)
3893         return Result;
3894 
3895       // Try and merge the two together.
3896       Result = BitPart(A->Provider, BitWidth);
3897       for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx) {
3898         if (A->Provenance[BitIdx] != BitPart::Unset &&
3899             B->Provenance[BitIdx] != BitPart::Unset &&
3900             A->Provenance[BitIdx] != B->Provenance[BitIdx])
3901           return Result = std::nullopt;
3902 
3903         if (A->Provenance[BitIdx] == BitPart::Unset)
3904           Result->Provenance[BitIdx] = B->Provenance[BitIdx];
3905         else
3906           Result->Provenance[BitIdx] = A->Provenance[BitIdx];
3907       }
3908 
3909       return Result;
3910     }
3911 
3912     // If this is a logical shift by a constant, recurse then shift the result.
3913     if (match(V, m_LogicalShift(m_Value(X), m_APInt(C)))) {
3914       const APInt &BitShift = *C;
3915 
3916       // Ensure the shift amount is defined.
3917       if (BitShift.uge(BitWidth))
3918         return Result;
3919 
3920       // For bswap-only, limit shift amounts to whole bytes, for an early exit.
3921       if (!MatchBitReversals && (BitShift.getZExtValue() % 8) != 0)
3922         return Result;
3923 
3924       const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
3925                                         Depth + 1, FoundRoot);
3926       if (!Res)
3927         return Result;
3928       Result = Res;
3929 
3930       // Perform the "shift" on BitProvenance.
3931       auto &P = Result->Provenance;
3932       if (I->getOpcode() == Instruction::Shl) {
3933         P.erase(std::prev(P.end(), BitShift.getZExtValue()), P.end());
3934         P.insert(P.begin(), BitShift.getZExtValue(), BitPart::Unset);
3935       } else {
3936         P.erase(P.begin(), std::next(P.begin(), BitShift.getZExtValue()));
3937         P.insert(P.end(), BitShift.getZExtValue(), BitPart::Unset);
3938       }
3939 
3940       return Result;
3941     }
3942 
3943     // If this is a logical 'and' with a mask that clears bits, recurse then
3944     // unset the appropriate bits.
3945     if (match(V, m_And(m_Value(X), m_APInt(C)))) {
3946       const APInt &AndMask = *C;
3947 
3948       // Check that the mask allows a multiple of 8 bits for a bswap, for an
3949       // early exit.
3950       unsigned NumMaskedBits = AndMask.popcount();
3951       if (!MatchBitReversals && (NumMaskedBits % 8) != 0)
3952         return Result;
3953 
3954       const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
3955                                         Depth + 1, FoundRoot);
3956       if (!Res)
3957         return Result;
3958       Result = Res;
3959 
3960       for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx)
3961         // If the AndMask is zero for this bit, clear the bit.
3962         if (AndMask[BitIdx] == 0)
3963           Result->Provenance[BitIdx] = BitPart::Unset;
3964       return Result;
3965     }
3966 
3967     // If this is a zext instruction zero extend the result.
3968     if (match(V, m_ZExt(m_Value(X)))) {
3969       const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
3970                                         Depth + 1, FoundRoot);
3971       if (!Res)
3972         return Result;
3973 
3974       Result = BitPart(Res->Provider, BitWidth);
3975       auto NarrowBitWidth = X->getType()->getScalarSizeInBits();
3976       for (unsigned BitIdx = 0; BitIdx < NarrowBitWidth; ++BitIdx)
3977         Result->Provenance[BitIdx] = Res->Provenance[BitIdx];
3978       for (unsigned BitIdx = NarrowBitWidth; BitIdx < BitWidth; ++BitIdx)
3979         Result->Provenance[BitIdx] = BitPart::Unset;
3980       return Result;
3981     }
3982 
3983     // If this is a truncate instruction, extract the lower bits.
3984     if (match(V, m_Trunc(m_Value(X)))) {
3985       const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
3986                                         Depth + 1, FoundRoot);
3987       if (!Res)
3988         return Result;
3989 
3990       Result = BitPart(Res->Provider, BitWidth);
3991       for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx)
3992         Result->Provenance[BitIdx] = Res->Provenance[BitIdx];
3993       return Result;
3994     }
3995 
3996     // BITREVERSE - most likely due to us previous matching a partial
3997     // bitreverse.
3998     if (match(V, m_BitReverse(m_Value(X)))) {
3999       const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
4000                                         Depth + 1, FoundRoot);
4001       if (!Res)
4002         return Result;
4003 
4004       Result = BitPart(Res->Provider, BitWidth);
4005       for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx)
4006         Result->Provenance[(BitWidth - 1) - BitIdx] = Res->Provenance[BitIdx];
4007       return Result;
4008     }
4009 
4010     // BSWAP - most likely due to us previous matching a partial bswap.
4011     if (match(V, m_BSwap(m_Value(X)))) {
4012       const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
4013                                         Depth + 1, FoundRoot);
4014       if (!Res)
4015         return Result;
4016 
4017       unsigned ByteWidth = BitWidth / 8;
4018       Result = BitPart(Res->Provider, BitWidth);
4019       for (unsigned ByteIdx = 0; ByteIdx < ByteWidth; ++ByteIdx) {
4020         unsigned ByteBitOfs = ByteIdx * 8;
4021         for (unsigned BitIdx = 0; BitIdx < 8; ++BitIdx)
4022           Result->Provenance[(BitWidth - 8 - ByteBitOfs) + BitIdx] =
4023               Res->Provenance[ByteBitOfs + BitIdx];
4024       }
4025       return Result;
4026     }
4027 
4028     // Funnel 'double' shifts take 3 operands, 2 inputs and the shift
4029     // amount (modulo).
4030     // fshl(X,Y,Z): (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
4031     // fshr(X,Y,Z): (X << (BW - (Z % BW))) | (Y >> (Z % BW))
4032     if (match(V, m_FShl(m_Value(X), m_Value(Y), m_APInt(C))) ||
4033         match(V, m_FShr(m_Value(X), m_Value(Y), m_APInt(C)))) {
4034       // We can treat fshr as a fshl by flipping the modulo amount.
4035       unsigned ModAmt = C->urem(BitWidth);
4036       if (cast<IntrinsicInst>(I)->getIntrinsicID() == Intrinsic::fshr)
4037         ModAmt = BitWidth - ModAmt;
4038 
4039       // For bswap-only, limit shift amounts to whole bytes, for an early exit.
4040       if (!MatchBitReversals && (ModAmt % 8) != 0)
4041         return Result;
4042 
4043       // Check we have both sources and they are from the same provider.
4044       const auto &LHS = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
4045                                         Depth + 1, FoundRoot);
4046       if (!LHS || !LHS->Provider)
4047         return Result;
4048 
4049       const auto &RHS = collectBitParts(Y, MatchBSwaps, MatchBitReversals, BPS,
4050                                         Depth + 1, FoundRoot);
4051       if (!RHS || LHS->Provider != RHS->Provider)
4052         return Result;
4053 
4054       unsigned StartBitRHS = BitWidth - ModAmt;
4055       Result = BitPart(LHS->Provider, BitWidth);
4056       for (unsigned BitIdx = 0; BitIdx < StartBitRHS; ++BitIdx)
4057         Result->Provenance[BitIdx + ModAmt] = LHS->Provenance[BitIdx];
4058       for (unsigned BitIdx = 0; BitIdx < ModAmt; ++BitIdx)
4059         Result->Provenance[BitIdx] = RHS->Provenance[BitIdx + StartBitRHS];
4060       return Result;
4061     }
4062   }
4063 
4064   // If we've already found a root input value then we're never going to merge
4065   // these back together.
4066   if (FoundRoot)
4067     return Result;
4068 
4069   // Okay, we got to something that isn't a shift, 'or', 'and', etc. This must
4070   // be the root input value to the bswap/bitreverse.
4071   FoundRoot = true;
4072   Result = BitPart(V, BitWidth);
4073   for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx)
4074     Result->Provenance[BitIdx] = BitIdx;
4075   return Result;
4076 }
4077 
4078 static bool bitTransformIsCorrectForBSwap(unsigned From, unsigned To,
4079                                           unsigned BitWidth) {
4080   if (From % 8 != To % 8)
4081     return false;
4082   // Convert from bit indices to byte indices and check for a byte reversal.
4083   From >>= 3;
4084   To >>= 3;
4085   BitWidth >>= 3;
4086   return From == BitWidth - To - 1;
4087 }
4088 
4089 static bool bitTransformIsCorrectForBitReverse(unsigned From, unsigned To,
4090                                                unsigned BitWidth) {
4091   return From == BitWidth - To - 1;
4092 }
4093 
4094 bool llvm::recognizeBSwapOrBitReverseIdiom(
4095     Instruction *I, bool MatchBSwaps, bool MatchBitReversals,
4096     SmallVectorImpl<Instruction *> &InsertedInsts) {
4097   if (!match(I, m_Or(m_Value(), m_Value())) &&
4098       !match(I, m_FShl(m_Value(), m_Value(), m_Value())) &&
4099       !match(I, m_FShr(m_Value(), m_Value(), m_Value())) &&
4100       !match(I, m_BSwap(m_Value())))
4101     return false;
4102   if (!MatchBSwaps && !MatchBitReversals)
4103     return false;
4104   Type *ITy = I->getType();
4105   if (!ITy->isIntOrIntVectorTy() || ITy->getScalarSizeInBits() > 128)
4106     return false;  // Can't do integer/elements > 128 bits.
4107 
4108   // Try to find all the pieces corresponding to the bswap.
4109   bool FoundRoot = false;
4110   std::map<Value *, std::optional<BitPart>> BPS;
4111   const auto &Res =
4112       collectBitParts(I, MatchBSwaps, MatchBitReversals, BPS, 0, FoundRoot);
4113   if (!Res)
4114     return false;
4115   ArrayRef<int8_t> BitProvenance = Res->Provenance;
4116   assert(all_of(BitProvenance,
4117                 [](int8_t I) { return I == BitPart::Unset || 0 <= I; }) &&
4118          "Illegal bit provenance index");
4119 
4120   // If the upper bits are zero, then attempt to perform as a truncated op.
4121   Type *DemandedTy = ITy;
4122   if (BitProvenance.back() == BitPart::Unset) {
4123     while (!BitProvenance.empty() && BitProvenance.back() == BitPart::Unset)
4124       BitProvenance = BitProvenance.drop_back();
4125     if (BitProvenance.empty())
4126       return false; // TODO - handle null value?
4127     DemandedTy = Type::getIntNTy(I->getContext(), BitProvenance.size());
4128     if (auto *IVecTy = dyn_cast<VectorType>(ITy))
4129       DemandedTy = VectorType::get(DemandedTy, IVecTy);
4130   }
4131 
4132   // Check BitProvenance hasn't found a source larger than the result type.
4133   unsigned DemandedBW = DemandedTy->getScalarSizeInBits();
4134   if (DemandedBW > ITy->getScalarSizeInBits())
4135     return false;
4136 
4137   // Now, is the bit permutation correct for a bswap or a bitreverse? We can
4138   // only byteswap values with an even number of bytes.
4139   APInt DemandedMask = APInt::getAllOnes(DemandedBW);
4140   bool OKForBSwap = MatchBSwaps && (DemandedBW % 16) == 0;
4141   bool OKForBitReverse = MatchBitReversals;
4142   for (unsigned BitIdx = 0;
4143        (BitIdx < DemandedBW) && (OKForBSwap || OKForBitReverse); ++BitIdx) {
4144     if (BitProvenance[BitIdx] == BitPart::Unset) {
4145       DemandedMask.clearBit(BitIdx);
4146       continue;
4147     }
4148     OKForBSwap &= bitTransformIsCorrectForBSwap(BitProvenance[BitIdx], BitIdx,
4149                                                 DemandedBW);
4150     OKForBitReverse &= bitTransformIsCorrectForBitReverse(BitProvenance[BitIdx],
4151                                                           BitIdx, DemandedBW);
4152   }
4153 
4154   Intrinsic::ID Intrin;
4155   if (OKForBSwap)
4156     Intrin = Intrinsic::bswap;
4157   else if (OKForBitReverse)
4158     Intrin = Intrinsic::bitreverse;
4159   else
4160     return false;
4161 
4162   Function *F =
4163       Intrinsic::getOrInsertDeclaration(I->getModule(), Intrin, DemandedTy);
4164   Value *Provider = Res->Provider;
4165 
4166   // We may need to truncate the provider.
4167   if (DemandedTy != Provider->getType()) {
4168     auto *Trunc =
4169         CastInst::CreateIntegerCast(Provider, DemandedTy, false, "trunc", I->getIterator());
4170     InsertedInsts.push_back(Trunc);
4171     Provider = Trunc;
4172   }
4173 
4174   Instruction *Result = CallInst::Create(F, Provider, "rev", I->getIterator());
4175   InsertedInsts.push_back(Result);
4176 
4177   if (!DemandedMask.isAllOnes()) {
4178     auto *Mask = ConstantInt::get(DemandedTy, DemandedMask);
4179     Result = BinaryOperator::Create(Instruction::And, Result, Mask, "mask", I->getIterator());
4180     InsertedInsts.push_back(Result);
4181   }
4182 
4183   // We may need to zeroextend back to the result type.
4184   if (ITy != Result->getType()) {
4185     auto *ExtInst = CastInst::CreateIntegerCast(Result, ITy, false, "zext", I->getIterator());
4186     InsertedInsts.push_back(ExtInst);
4187   }
4188 
4189   return true;
4190 }
4191 
4192 // CodeGen has special handling for some string functions that may replace
4193 // them with target-specific intrinsics.  Since that'd skip our interceptors
4194 // in ASan/MSan/TSan/DFSan, and thus make us miss some memory accesses,
4195 // we mark affected calls as NoBuiltin, which will disable optimization
4196 // in CodeGen.
4197 void llvm::maybeMarkSanitizerLibraryCallNoBuiltin(
4198     CallInst *CI, const TargetLibraryInfo *TLI) {
4199   Function *F = CI->getCalledFunction();
4200   LibFunc Func;
4201   if (F && !F->hasLocalLinkage() && F->hasName() &&
4202       TLI->getLibFunc(F->getName(), Func) && TLI->hasOptimizedCodeGen(Func) &&
4203       !F->doesNotAccessMemory())
4204     CI->addFnAttr(Attribute::NoBuiltin);
4205 }
4206 
4207 bool llvm::canReplaceOperandWithVariable(const Instruction *I, unsigned OpIdx) {
4208   // We can't have a PHI with a metadata type.
4209   if (I->getOperand(OpIdx)->getType()->isMetadataTy())
4210     return false;
4211 
4212   // Early exit.
4213   if (!isa<Constant>(I->getOperand(OpIdx)))
4214     return true;
4215 
4216   switch (I->getOpcode()) {
4217   default:
4218     return true;
4219   case Instruction::Call:
4220   case Instruction::Invoke: {
4221     const auto &CB = cast<CallBase>(*I);
4222 
4223     // Can't handle inline asm. Skip it.
4224     if (CB.isInlineAsm())
4225       return false;
4226 
4227     // Constant bundle operands may need to retain their constant-ness for
4228     // correctness.
4229     if (CB.isBundleOperand(OpIdx))
4230       return false;
4231 
4232     if (OpIdx < CB.arg_size()) {
4233       // Some variadic intrinsics require constants in the variadic arguments,
4234       // which currently aren't markable as immarg.
4235       if (isa<IntrinsicInst>(CB) &&
4236           OpIdx >= CB.getFunctionType()->getNumParams()) {
4237         // This is known to be OK for stackmap.
4238         return CB.getIntrinsicID() == Intrinsic::experimental_stackmap;
4239       }
4240 
4241       // gcroot is a special case, since it requires a constant argument which
4242       // isn't also required to be a simple ConstantInt.
4243       if (CB.getIntrinsicID() == Intrinsic::gcroot)
4244         return false;
4245 
4246       // Some intrinsic operands are required to be immediates.
4247       return !CB.paramHasAttr(OpIdx, Attribute::ImmArg);
4248     }
4249 
4250     // It is never allowed to replace the call argument to an intrinsic, but it
4251     // may be possible for a call.
4252     return !isa<IntrinsicInst>(CB);
4253   }
4254   case Instruction::ShuffleVector:
4255     // Shufflevector masks are constant.
4256     return OpIdx != 2;
4257   case Instruction::Switch:
4258   case Instruction::ExtractValue:
4259     // All operands apart from the first are constant.
4260     return OpIdx == 0;
4261   case Instruction::InsertValue:
4262     // All operands apart from the first and the second are constant.
4263     return OpIdx < 2;
4264   case Instruction::Alloca:
4265     // Static allocas (constant size in the entry block) are handled by
4266     // prologue/epilogue insertion so they're free anyway. We definitely don't
4267     // want to make them non-constant.
4268     return !cast<AllocaInst>(I)->isStaticAlloca();
4269   case Instruction::GetElementPtr:
4270     if (OpIdx == 0)
4271       return true;
4272     gep_type_iterator It = gep_type_begin(I);
4273     for (auto E = std::next(It, OpIdx); It != E; ++It)
4274       if (It.isStruct())
4275         return false;
4276     return true;
4277   }
4278 }
4279 
4280 Value *llvm::invertCondition(Value *Condition) {
4281   // First: Check if it's a constant
4282   if (Constant *C = dyn_cast<Constant>(Condition))
4283     return ConstantExpr::getNot(C);
4284 
4285   // Second: If the condition is already inverted, return the original value
4286   Value *NotCondition;
4287   if (match(Condition, m_Not(m_Value(NotCondition))))
4288     return NotCondition;
4289 
4290   BasicBlock *Parent = nullptr;
4291   Instruction *Inst = dyn_cast<Instruction>(Condition);
4292   if (Inst)
4293     Parent = Inst->getParent();
4294   else if (Argument *Arg = dyn_cast<Argument>(Condition))
4295     Parent = &Arg->getParent()->getEntryBlock();
4296   assert(Parent && "Unsupported condition to invert");
4297 
4298   // Third: Check all the users for an invert
4299   for (User *U : Condition->users())
4300     if (Instruction *I = dyn_cast<Instruction>(U))
4301       if (I->getParent() == Parent && match(I, m_Not(m_Specific(Condition))))
4302         return I;
4303 
4304   // Last option: Create a new instruction
4305   auto *Inverted =
4306       BinaryOperator::CreateNot(Condition, Condition->getName() + ".inv");
4307   if (Inst && !isa<PHINode>(Inst))
4308     Inverted->insertAfter(Inst);
4309   else
4310     Inverted->insertBefore(&*Parent->getFirstInsertionPt());
4311   return Inverted;
4312 }
4313 
4314 bool llvm::inferAttributesFromOthers(Function &F) {
4315   // Note: We explicitly check for attributes rather than using cover functions
4316   // because some of the cover functions include the logic being implemented.
4317 
4318   bool Changed = false;
4319   // readnone + not convergent implies nosync
4320   if (!F.hasFnAttribute(Attribute::NoSync) &&
4321       F.doesNotAccessMemory() && !F.isConvergent()) {
4322     F.setNoSync();
4323     Changed = true;
4324   }
4325 
4326   // readonly implies nofree
4327   if (!F.hasFnAttribute(Attribute::NoFree) && F.onlyReadsMemory()) {
4328     F.setDoesNotFreeMemory();
4329     Changed = true;
4330   }
4331 
4332   // willreturn implies mustprogress
4333   if (!F.hasFnAttribute(Attribute::MustProgress) && F.willReturn()) {
4334     F.setMustProgress();
4335     Changed = true;
4336   }
4337 
4338   // TODO: There are a bunch of cases of restrictive memory effects we
4339   // can infer by inspecting arguments of argmemonly-ish functions.
4340 
4341   return Changed;
4342 }
4343