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