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/None.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/SetVector.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/ADT/TinyPtrVector.h"
28 #include "llvm/Analysis/AssumeBundleQueries.h"
29 #include "llvm/Analysis/ConstantFolding.h"
30 #include "llvm/Analysis/DomTreeUpdater.h"
31 #include "llvm/Analysis/EHPersonalities.h"
32 #include "llvm/Analysis/InstructionSimplify.h"
33 #include "llvm/Analysis/LazyValueInfo.h"
34 #include "llvm/Analysis/MemoryBuiltins.h"
35 #include "llvm/Analysis/MemorySSAUpdater.h"
36 #include "llvm/Analysis/TargetLibraryInfo.h"
37 #include "llvm/Analysis/ValueTracking.h"
38 #include "llvm/Analysis/VectorUtils.h"
39 #include "llvm/BinaryFormat/Dwarf.h"
40 #include "llvm/IR/Argument.h"
41 #include "llvm/IR/Attributes.h"
42 #include "llvm/IR/BasicBlock.h"
43 #include "llvm/IR/CFG.h"
44 #include "llvm/IR/Constant.h"
45 #include "llvm/IR/ConstantRange.h"
46 #include "llvm/IR/Constants.h"
47 #include "llvm/IR/DIBuilder.h"
48 #include "llvm/IR/DataLayout.h"
49 #include "llvm/IR/DebugInfoMetadata.h"
50 #include "llvm/IR/DebugLoc.h"
51 #include "llvm/IR/DerivedTypes.h"
52 #include "llvm/IR/Dominators.h"
53 #include "llvm/IR/Function.h"
54 #include "llvm/IR/GetElementPtrTypeIterator.h"
55 #include "llvm/IR/GlobalObject.h"
56 #include "llvm/IR/IRBuilder.h"
57 #include "llvm/IR/InstrTypes.h"
58 #include "llvm/IR/Instruction.h"
59 #include "llvm/IR/Instructions.h"
60 #include "llvm/IR/IntrinsicInst.h"
61 #include "llvm/IR/Intrinsics.h"
62 #include "llvm/IR/LLVMContext.h"
63 #include "llvm/IR/MDBuilder.h"
64 #include "llvm/IR/Metadata.h"
65 #include "llvm/IR/Module.h"
66 #include "llvm/IR/Operator.h"
67 #include "llvm/IR/PatternMatch.h"
68 #include "llvm/IR/Type.h"
69 #include "llvm/IR/Use.h"
70 #include "llvm/IR/User.h"
71 #include "llvm/IR/Value.h"
72 #include "llvm/IR/ValueHandle.h"
73 #include "llvm/Support/Casting.h"
74 #include "llvm/Support/Debug.h"
75 #include "llvm/Support/ErrorHandling.h"
76 #include "llvm/Support/KnownBits.h"
77 #include "llvm/Support/raw_ostream.h"
78 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
79 #include "llvm/Transforms/Utils/ValueMapper.h"
80 #include <algorithm>
81 #include <cassert>
82 #include <climits>
83 #include <cstdint>
84 #include <iterator>
85 #include <map>
86 #include <utility>
87 
88 using namespace llvm;
89 using namespace llvm::PatternMatch;
90 
91 #define DEBUG_TYPE "local"
92 
93 STATISTIC(NumRemoved, "Number of unreachable basic blocks removed");
94 
95 // Max recursion depth for collectBitParts used when detecting bswap and
96 // bitreverse idioms
97 static const unsigned BitPartRecursionMaxDepth = 64;
98 
99 //===----------------------------------------------------------------------===//
100 //  Local constant propagation.
101 //
102 
103 /// ConstantFoldTerminator - If a terminator instruction is predicated on a
104 /// constant value, convert it into an unconditional branch to the constant
105 /// destination.  This is a nontrivial operation because the successors of this
106 /// basic block must have their PHI nodes updated.
107 /// Also calls RecursivelyDeleteTriviallyDeadInstructions() on any branch/switch
108 /// conditions and indirectbr addresses this might make dead if
109 /// DeleteDeadConditions is true.
110 bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions,
111                                   const TargetLibraryInfo *TLI,
112                                   DomTreeUpdater *DTU) {
113   Instruction *T = BB->getTerminator();
114   IRBuilder<> Builder(T);
115 
116   // Branch - See if we are conditional jumping on constant
117   if (auto *BI = dyn_cast<BranchInst>(T)) {
118     if (BI->isUnconditional()) return false;  // Can't optimize uncond branch
119     BasicBlock *Dest1 = BI->getSuccessor(0);
120     BasicBlock *Dest2 = BI->getSuccessor(1);
121 
122     if (auto *Cond = dyn_cast<ConstantInt>(BI->getCondition())) {
123       // Are we branching on constant?
124       // YES.  Change to unconditional branch...
125       BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2;
126       BasicBlock *OldDest     = Cond->getZExtValue() ? Dest2 : Dest1;
127 
128       // Let the basic block know that we are letting go of it.  Based on this,
129       // it will adjust its PHI nodes.
130       SmallVector<WeakTrackingVH, 8> MaybeDeadInstrs;
131       OldDest->removePredecessor(BB, false, &MaybeDeadInstrs);
132       RecursivelyDeleteTriviallyDeadInstructionsPermissive(MaybeDeadInstrs,
133                                                            TLI);
134 
135       // Replace the conditional branch with an unconditional one.
136       Builder.CreateBr(Destination);
137       BI->eraseFromParent();
138       if (DTU)
139         DTU->applyUpdatesPermissive({{DominatorTree::Delete, BB, OldDest}});
140       return true;
141     }
142 
143     if (Dest2 == Dest1) {       // Conditional branch to same location?
144       // This branch matches something like this:
145       //     br bool %cond, label %Dest, label %Dest
146       // and changes it into:  br label %Dest
147 
148       // Let the basic block know that we are letting go of one copy of it.
149       assert(BI->getParent() && "Terminator not inserted in block!");
150       Dest1->removePredecessor(BI->getParent());
151 
152       // Replace the conditional branch with an unconditional one.
153       Builder.CreateBr(Dest1);
154       Value *Cond = BI->getCondition();
155       BI->eraseFromParent();
156       if (DeleteDeadConditions)
157         RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI);
158       return true;
159     }
160     return false;
161   }
162 
163   if (auto *SI = dyn_cast<SwitchInst>(T)) {
164     // If we are switching on a constant, we can convert the switch to an
165     // unconditional branch.
166     auto *CI = dyn_cast<ConstantInt>(SI->getCondition());
167     BasicBlock *DefaultDest = SI->getDefaultDest();
168     BasicBlock *TheOnlyDest = DefaultDest;
169 
170     // If the default is unreachable, ignore it when searching for TheOnlyDest.
171     if (isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg()) &&
172         SI->getNumCases() > 0) {
173       TheOnlyDest = SI->case_begin()->getCaseSuccessor();
174     }
175 
176     // Figure out which case it goes to.
177     for (auto i = SI->case_begin(), e = SI->case_end(); i != e;) {
178       // Found case matching a constant operand?
179       if (i->getCaseValue() == CI) {
180         TheOnlyDest = i->getCaseSuccessor();
181         break;
182       }
183 
184       // Check to see if this branch is going to the same place as the default
185       // dest.  If so, eliminate it as an explicit compare.
186       if (i->getCaseSuccessor() == DefaultDest) {
187         MDNode *MD = SI->getMetadata(LLVMContext::MD_prof);
188         unsigned NCases = SI->getNumCases();
189         // Fold the case metadata into the default if there will be any branches
190         // left, unless the metadata doesn't match the switch.
191         if (NCases > 1 && MD && MD->getNumOperands() == 2 + NCases) {
192           // Collect branch weights into a vector.
193           SmallVector<uint32_t, 8> Weights;
194           for (unsigned MD_i = 1, MD_e = MD->getNumOperands(); MD_i < MD_e;
195                ++MD_i) {
196             auto *CI = mdconst::extract<ConstantInt>(MD->getOperand(MD_i));
197             Weights.push_back(CI->getValue().getZExtValue());
198           }
199           // Merge weight of this case to the default weight.
200           unsigned idx = i->getCaseIndex();
201           Weights[0] += Weights[idx+1];
202           // Remove weight for this case.
203           std::swap(Weights[idx+1], Weights.back());
204           Weights.pop_back();
205           SI->setMetadata(LLVMContext::MD_prof,
206                           MDBuilder(BB->getContext()).
207                           createBranchWeights(Weights));
208         }
209         // Remove this entry.
210         BasicBlock *ParentBB = SI->getParent();
211         DefaultDest->removePredecessor(ParentBB);
212         i = SI->removeCase(i);
213         e = SI->case_end();
214         if (DTU)
215           DTU->applyUpdatesPermissive(
216               {{DominatorTree::Delete, ParentBB, DefaultDest}});
217         continue;
218       }
219 
220       // Otherwise, check to see if the switch only branches to one destination.
221       // We do this by reseting "TheOnlyDest" to null when we find two non-equal
222       // destinations.
223       if (i->getCaseSuccessor() != TheOnlyDest)
224         TheOnlyDest = nullptr;
225 
226       // Increment this iterator as we haven't removed the case.
227       ++i;
228     }
229 
230     if (CI && !TheOnlyDest) {
231       // Branching on a constant, but not any of the cases, go to the default
232       // successor.
233       TheOnlyDest = SI->getDefaultDest();
234     }
235 
236     // If we found a single destination that we can fold the switch into, do so
237     // now.
238     if (TheOnlyDest) {
239       // Insert the new branch.
240       Builder.CreateBr(TheOnlyDest);
241       BasicBlock *BB = SI->getParent();
242       std::vector <DominatorTree::UpdateType> Updates;
243       if (DTU)
244         Updates.reserve(SI->getNumSuccessors() - 1);
245 
246       // Remove entries from PHI nodes which we no longer branch to...
247       for (BasicBlock *Succ : successors(SI)) {
248         // Found case matching a constant operand?
249         if (Succ == TheOnlyDest) {
250           TheOnlyDest = nullptr; // Don't modify the first branch to TheOnlyDest
251         } else {
252           Succ->removePredecessor(BB);
253           if (DTU)
254             Updates.push_back({DominatorTree::Delete, BB, Succ});
255         }
256       }
257 
258       // Delete the old switch.
259       Value *Cond = SI->getCondition();
260       SI->eraseFromParent();
261       if (DeleteDeadConditions)
262         RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI);
263       if (DTU)
264         DTU->applyUpdatesPermissive(Updates);
265       return true;
266     }
267 
268     if (SI->getNumCases() == 1) {
269       // Otherwise, we can fold this switch into a conditional branch
270       // instruction if it has only one non-default destination.
271       auto FirstCase = *SI->case_begin();
272       Value *Cond = Builder.CreateICmpEQ(SI->getCondition(),
273           FirstCase.getCaseValue(), "cond");
274 
275       // Insert the new branch.
276       BranchInst *NewBr = Builder.CreateCondBr(Cond,
277                                                FirstCase.getCaseSuccessor(),
278                                                SI->getDefaultDest());
279       MDNode *MD = SI->getMetadata(LLVMContext::MD_prof);
280       if (MD && MD->getNumOperands() == 3) {
281         ConstantInt *SICase =
282             mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
283         ConstantInt *SIDef =
284             mdconst::dyn_extract<ConstantInt>(MD->getOperand(1));
285         assert(SICase && SIDef);
286         // The TrueWeight should be the weight for the single case of SI.
287         NewBr->setMetadata(LLVMContext::MD_prof,
288                         MDBuilder(BB->getContext()).
289                         createBranchWeights(SICase->getValue().getZExtValue(),
290                                             SIDef->getValue().getZExtValue()));
291       }
292 
293       // Update make.implicit metadata to the newly-created conditional branch.
294       MDNode *MakeImplicitMD = SI->getMetadata(LLVMContext::MD_make_implicit);
295       if (MakeImplicitMD)
296         NewBr->setMetadata(LLVMContext::MD_make_implicit, MakeImplicitMD);
297 
298       // Delete the old switch.
299       SI->eraseFromParent();
300       return true;
301     }
302     return false;
303   }
304 
305   if (auto *IBI = dyn_cast<IndirectBrInst>(T)) {
306     // indirectbr blockaddress(@F, @BB) -> br label @BB
307     if (auto *BA =
308           dyn_cast<BlockAddress>(IBI->getAddress()->stripPointerCasts())) {
309       BasicBlock *TheOnlyDest = BA->getBasicBlock();
310       std::vector <DominatorTree::UpdateType> Updates;
311       if (DTU)
312         Updates.reserve(IBI->getNumDestinations() - 1);
313 
314       // Insert the new branch.
315       Builder.CreateBr(TheOnlyDest);
316 
317       for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
318         if (IBI->getDestination(i) == TheOnlyDest) {
319           TheOnlyDest = nullptr;
320         } else {
321           BasicBlock *ParentBB = IBI->getParent();
322           BasicBlock *DestBB = IBI->getDestination(i);
323           DestBB->removePredecessor(ParentBB);
324           if (DTU)
325             Updates.push_back({DominatorTree::Delete, ParentBB, DestBB});
326         }
327       }
328       Value *Address = IBI->getAddress();
329       IBI->eraseFromParent();
330       if (DeleteDeadConditions)
331         // Delete pointer cast instructions.
332         RecursivelyDeleteTriviallyDeadInstructions(Address, TLI);
333 
334       // Also zap the blockaddress constant if there are no users remaining,
335       // otherwise the destination is still marked as having its address taken.
336       if (BA->use_empty())
337         BA->destroyConstant();
338 
339       // If we didn't find our destination in the IBI successor list, then we
340       // have undefined behavior.  Replace the unconditional branch with an
341       // 'unreachable' instruction.
342       if (TheOnlyDest) {
343         BB->getTerminator()->eraseFromParent();
344         new UnreachableInst(BB->getContext(), BB);
345       }
346 
347       if (DTU)
348         DTU->applyUpdatesPermissive(Updates);
349       return true;
350     }
351   }
352 
353   return false;
354 }
355 
356 //===----------------------------------------------------------------------===//
357 //  Local dead code elimination.
358 //
359 
360 /// isInstructionTriviallyDead - Return true if the result produced by the
361 /// instruction is not used, and the instruction has no side effects.
362 ///
363 bool llvm::isInstructionTriviallyDead(Instruction *I,
364                                       const TargetLibraryInfo *TLI) {
365   if (!I->use_empty())
366     return false;
367   return wouldInstructionBeTriviallyDead(I, TLI);
368 }
369 
370 bool llvm::wouldInstructionBeTriviallyDead(Instruction *I,
371                                            const TargetLibraryInfo *TLI) {
372   if (I->isTerminator())
373     return false;
374 
375   // We don't want the landingpad-like instructions removed by anything this
376   // general.
377   if (I->isEHPad())
378     return false;
379 
380   // We don't want debug info removed by anything this general, unless
381   // debug info is empty.
382   if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
383     if (DDI->getAddress())
384       return false;
385     return true;
386   }
387   if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
388     if (DVI->getValue())
389       return false;
390     return true;
391   }
392   if (DbgLabelInst *DLI = dyn_cast<DbgLabelInst>(I)) {
393     if (DLI->getLabel())
394       return false;
395     return true;
396   }
397 
398   if (!I->mayHaveSideEffects())
399     return true;
400 
401   // Special case intrinsics that "may have side effects" but can be deleted
402   // when dead.
403   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
404     // Safe to delete llvm.stacksave and launder.invariant.group if dead.
405     if (II->getIntrinsicID() == Intrinsic::stacksave ||
406         II->getIntrinsicID() == Intrinsic::launder_invariant_group)
407       return true;
408 
409     if (II->isLifetimeStartOrEnd()) {
410       auto *Arg = II->getArgOperand(1);
411       // Lifetime intrinsics are dead when their right-hand is undef.
412       if (isa<UndefValue>(Arg))
413         return true;
414       // If the right-hand is an alloc, global, or argument and the only uses
415       // are lifetime intrinsics then the intrinsics are dead.
416       if (isa<AllocaInst>(Arg) || isa<GlobalValue>(Arg) || isa<Argument>(Arg))
417         return llvm::all_of(Arg->uses(), [](Use &Use) {
418           if (IntrinsicInst *IntrinsicUse =
419                   dyn_cast<IntrinsicInst>(Use.getUser()))
420             return IntrinsicUse->isLifetimeStartOrEnd();
421           return false;
422         });
423       return false;
424     }
425 
426     // Assumptions are dead if their condition is trivially true.  Guards on
427     // true are operationally no-ops.  In the future we can consider more
428     // sophisticated tradeoffs for guards considering potential for check
429     // widening, but for now we keep things simple.
430     if ((II->getIntrinsicID() == Intrinsic::assume &&
431          isAssumeWithEmptyBundle(*II)) ||
432         II->getIntrinsicID() == Intrinsic::experimental_guard) {
433       if (ConstantInt *Cond = dyn_cast<ConstantInt>(II->getArgOperand(0)))
434         return !Cond->isZero();
435 
436       return false;
437     }
438   }
439 
440   if (isAllocLikeFn(I, TLI))
441     return true;
442 
443   if (CallInst *CI = isFreeCall(I, TLI))
444     if (Constant *C = dyn_cast<Constant>(CI->getArgOperand(0)))
445       return C->isNullValue() || isa<UndefValue>(C);
446 
447   if (auto *Call = dyn_cast<CallBase>(I))
448     if (isMathLibCallNoop(Call, TLI))
449       return true;
450 
451   return false;
452 }
453 
454 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
455 /// trivially dead instruction, delete it.  If that makes any of its operands
456 /// trivially dead, delete them too, recursively.  Return true if any
457 /// instructions were deleted.
458 bool llvm::RecursivelyDeleteTriviallyDeadInstructions(
459     Value *V, const TargetLibraryInfo *TLI, MemorySSAUpdater *MSSAU) {
460   Instruction *I = dyn_cast<Instruction>(V);
461   if (!I || !isInstructionTriviallyDead(I, TLI))
462     return false;
463 
464   SmallVector<WeakTrackingVH, 16> DeadInsts;
465   DeadInsts.push_back(I);
466   RecursivelyDeleteTriviallyDeadInstructions(DeadInsts, TLI, MSSAU);
467 
468   return true;
469 }
470 
471 bool llvm::RecursivelyDeleteTriviallyDeadInstructionsPermissive(
472     SmallVectorImpl<WeakTrackingVH> &DeadInsts, const TargetLibraryInfo *TLI,
473     MemorySSAUpdater *MSSAU) {
474   unsigned S = 0, E = DeadInsts.size(), Alive = 0;
475   for (; S != E; ++S) {
476     auto *I = dyn_cast<Instruction>(DeadInsts[S]);
477     if (!I || !isInstructionTriviallyDead(I)) {
478       DeadInsts[S] = nullptr;
479       ++Alive;
480     }
481   }
482   if (Alive == E)
483     return false;
484   RecursivelyDeleteTriviallyDeadInstructions(DeadInsts, TLI, MSSAU);
485   return true;
486 }
487 
488 void llvm::RecursivelyDeleteTriviallyDeadInstructions(
489     SmallVectorImpl<WeakTrackingVH> &DeadInsts, const TargetLibraryInfo *TLI,
490     MemorySSAUpdater *MSSAU) {
491   // Process the dead instruction list until empty.
492   while (!DeadInsts.empty()) {
493     Value *V = DeadInsts.pop_back_val();
494     Instruction *I = cast_or_null<Instruction>(V);
495     if (!I)
496       continue;
497     assert(isInstructionTriviallyDead(I, TLI) &&
498            "Live instruction found in dead worklist!");
499     assert(I->use_empty() && "Instructions with uses are not dead.");
500 
501     // Don't lose the debug info while deleting the instructions.
502     salvageDebugInfo(*I);
503 
504     // Null out all of the instruction's operands to see if any operand becomes
505     // dead as we go.
506     for (Use &OpU : I->operands()) {
507       Value *OpV = OpU.get();
508       OpU.set(nullptr);
509 
510       if (!OpV->use_empty())
511         continue;
512 
513       // If the operand is an instruction that became dead as we nulled out the
514       // operand, and if it is 'trivially' dead, delete it in a future loop
515       // iteration.
516       if (Instruction *OpI = dyn_cast<Instruction>(OpV))
517         if (isInstructionTriviallyDead(OpI, TLI))
518           DeadInsts.push_back(OpI);
519     }
520     if (MSSAU)
521       MSSAU->removeMemoryAccess(I);
522 
523     I->eraseFromParent();
524   }
525 }
526 
527 bool llvm::replaceDbgUsesWithUndef(Instruction *I) {
528   SmallVector<DbgVariableIntrinsic *, 1> DbgUsers;
529   findDbgUsers(DbgUsers, I);
530   for (auto *DII : DbgUsers) {
531     Value *Undef = UndefValue::get(I->getType());
532     DII->setOperand(0, MetadataAsValue::get(DII->getContext(),
533                                             ValueAsMetadata::get(Undef)));
534   }
535   return !DbgUsers.empty();
536 }
537 
538 /// areAllUsesEqual - Check whether the uses of a value are all the same.
539 /// This is similar to Instruction::hasOneUse() except this will also return
540 /// true when there are no uses or multiple uses that all refer to the same
541 /// value.
542 static bool areAllUsesEqual(Instruction *I) {
543   Value::user_iterator UI = I->user_begin();
544   Value::user_iterator UE = I->user_end();
545   if (UI == UE)
546     return true;
547 
548   User *TheUse = *UI;
549   for (++UI; UI != UE; ++UI) {
550     if (*UI != TheUse)
551       return false;
552   }
553   return true;
554 }
555 
556 /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
557 /// dead PHI node, due to being a def-use chain of single-use nodes that
558 /// either forms a cycle or is terminated by a trivially dead instruction,
559 /// delete it.  If that makes any of its operands trivially dead, delete them
560 /// too, recursively.  Return true if a change was made.
561 bool llvm::RecursivelyDeleteDeadPHINode(PHINode *PN,
562                                         const TargetLibraryInfo *TLI,
563                                         llvm::MemorySSAUpdater *MSSAU) {
564   SmallPtrSet<Instruction*, 4> Visited;
565   for (Instruction *I = PN; areAllUsesEqual(I) && !I->mayHaveSideEffects();
566        I = cast<Instruction>(*I->user_begin())) {
567     if (I->use_empty())
568       return RecursivelyDeleteTriviallyDeadInstructions(I, TLI, MSSAU);
569 
570     // If we find an instruction more than once, we're on a cycle that
571     // won't prove fruitful.
572     if (!Visited.insert(I).second) {
573       // Break the cycle and delete the instruction and its operands.
574       I->replaceAllUsesWith(UndefValue::get(I->getType()));
575       (void)RecursivelyDeleteTriviallyDeadInstructions(I, TLI, MSSAU);
576       return true;
577     }
578   }
579   return false;
580 }
581 
582 static bool
583 simplifyAndDCEInstruction(Instruction *I,
584                           SmallSetVector<Instruction *, 16> &WorkList,
585                           const DataLayout &DL,
586                           const TargetLibraryInfo *TLI) {
587   if (isInstructionTriviallyDead(I, TLI)) {
588     salvageDebugInfo(*I);
589 
590     // Null out all of the instruction's operands to see if any operand becomes
591     // dead as we go.
592     for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
593       Value *OpV = I->getOperand(i);
594       I->setOperand(i, nullptr);
595 
596       if (!OpV->use_empty() || I == OpV)
597         continue;
598 
599       // If the operand is an instruction that became dead as we nulled out the
600       // operand, and if it is 'trivially' dead, delete it in a future loop
601       // iteration.
602       if (Instruction *OpI = dyn_cast<Instruction>(OpV))
603         if (isInstructionTriviallyDead(OpI, TLI))
604           WorkList.insert(OpI);
605     }
606 
607     I->eraseFromParent();
608 
609     return true;
610   }
611 
612   if (Value *SimpleV = SimplifyInstruction(I, DL)) {
613     // Add the users to the worklist. CAREFUL: an instruction can use itself,
614     // in the case of a phi node.
615     for (User *U : I->users()) {
616       if (U != I) {
617         WorkList.insert(cast<Instruction>(U));
618       }
619     }
620 
621     // Replace the instruction with its simplified value.
622     bool Changed = false;
623     if (!I->use_empty()) {
624       I->replaceAllUsesWith(SimpleV);
625       Changed = true;
626     }
627     if (isInstructionTriviallyDead(I, TLI)) {
628       I->eraseFromParent();
629       Changed = true;
630     }
631     return Changed;
632   }
633   return false;
634 }
635 
636 /// SimplifyInstructionsInBlock - Scan the specified basic block and try to
637 /// simplify any instructions in it and recursively delete dead instructions.
638 ///
639 /// This returns true if it changed the code, note that it can delete
640 /// instructions in other blocks as well in this block.
641 bool llvm::SimplifyInstructionsInBlock(BasicBlock *BB,
642                                        const TargetLibraryInfo *TLI) {
643   bool MadeChange = false;
644   const DataLayout &DL = BB->getModule()->getDataLayout();
645 
646 #ifndef NDEBUG
647   // In debug builds, ensure that the terminator of the block is never replaced
648   // or deleted by these simplifications. The idea of simplification is that it
649   // cannot introduce new instructions, and there is no way to replace the
650   // terminator of a block without introducing a new instruction.
651   AssertingVH<Instruction> TerminatorVH(&BB->back());
652 #endif
653 
654   SmallSetVector<Instruction *, 16> WorkList;
655   // Iterate over the original function, only adding insts to the worklist
656   // if they actually need to be revisited. This avoids having to pre-init
657   // the worklist with the entire function's worth of instructions.
658   for (BasicBlock::iterator BI = BB->begin(), E = std::prev(BB->end());
659        BI != E;) {
660     assert(!BI->isTerminator());
661     Instruction *I = &*BI;
662     ++BI;
663 
664     // We're visiting this instruction now, so make sure it's not in the
665     // worklist from an earlier visit.
666     if (!WorkList.count(I))
667       MadeChange |= simplifyAndDCEInstruction(I, WorkList, DL, TLI);
668   }
669 
670   while (!WorkList.empty()) {
671     Instruction *I = WorkList.pop_back_val();
672     MadeChange |= simplifyAndDCEInstruction(I, WorkList, DL, TLI);
673   }
674   return MadeChange;
675 }
676 
677 //===----------------------------------------------------------------------===//
678 //  Control Flow Graph Restructuring.
679 //
680 
681 void llvm::RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
682                                         DomTreeUpdater *DTU) {
683   // This only adjusts blocks with PHI nodes.
684   if (!isa<PHINode>(BB->begin()))
685     return;
686 
687   // Remove the entries for Pred from the PHI nodes in BB, but do not simplify
688   // them down.  This will leave us with single entry phi nodes and other phis
689   // that can be removed.
690   BB->removePredecessor(Pred, true);
691 
692   WeakTrackingVH PhiIt = &BB->front();
693   while (PHINode *PN = dyn_cast<PHINode>(PhiIt)) {
694     PhiIt = &*++BasicBlock::iterator(cast<Instruction>(PhiIt));
695     Value *OldPhiIt = PhiIt;
696 
697     if (!recursivelySimplifyInstruction(PN))
698       continue;
699 
700     // If recursive simplification ended up deleting the next PHI node we would
701     // iterate to, then our iterator is invalid, restart scanning from the top
702     // of the block.
703     if (PhiIt != OldPhiIt) PhiIt = &BB->front();
704   }
705   if (DTU)
706     DTU->applyUpdatesPermissive({{DominatorTree::Delete, Pred, BB}});
707 }
708 
709 void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB,
710                                        DomTreeUpdater *DTU) {
711 
712   // If BB has single-entry PHI nodes, fold them.
713   while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) {
714     Value *NewVal = PN->getIncomingValue(0);
715     // Replace self referencing PHI with undef, it must be dead.
716     if (NewVal == PN) NewVal = UndefValue::get(PN->getType());
717     PN->replaceAllUsesWith(NewVal);
718     PN->eraseFromParent();
719   }
720 
721   BasicBlock *PredBB = DestBB->getSinglePredecessor();
722   assert(PredBB && "Block doesn't have a single predecessor!");
723 
724   bool ReplaceEntryBB = false;
725   if (PredBB == &DestBB->getParent()->getEntryBlock())
726     ReplaceEntryBB = true;
727 
728   // DTU updates: Collect all the edges that enter
729   // PredBB. These dominator edges will be redirected to DestBB.
730   SmallVector<DominatorTree::UpdateType, 32> Updates;
731 
732   if (DTU) {
733     Updates.push_back({DominatorTree::Delete, PredBB, DestBB});
734     for (auto I = pred_begin(PredBB), E = pred_end(PredBB); I != E; ++I) {
735       Updates.push_back({DominatorTree::Delete, *I, PredBB});
736       // This predecessor of PredBB may already have DestBB as a successor.
737       if (llvm::find(successors(*I), DestBB) == succ_end(*I))
738         Updates.push_back({DominatorTree::Insert, *I, DestBB});
739     }
740   }
741 
742   // Zap anything that took the address of DestBB.  Not doing this will give the
743   // address an invalid value.
744   if (DestBB->hasAddressTaken()) {
745     BlockAddress *BA = BlockAddress::get(DestBB);
746     Constant *Replacement =
747       ConstantInt::get(Type::getInt32Ty(BA->getContext()), 1);
748     BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement,
749                                                      BA->getType()));
750     BA->destroyConstant();
751   }
752 
753   // Anything that branched to PredBB now branches to DestBB.
754   PredBB->replaceAllUsesWith(DestBB);
755 
756   // Splice all the instructions from PredBB to DestBB.
757   PredBB->getTerminator()->eraseFromParent();
758   DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList());
759   new UnreachableInst(PredBB->getContext(), PredBB);
760 
761   // If the PredBB is the entry block of the function, move DestBB up to
762   // become the entry block after we erase PredBB.
763   if (ReplaceEntryBB)
764     DestBB->moveAfter(PredBB);
765 
766   if (DTU) {
767     assert(PredBB->getInstList().size() == 1 &&
768            isa<UnreachableInst>(PredBB->getTerminator()) &&
769            "The successor list of PredBB isn't empty before "
770            "applying corresponding DTU updates.");
771     DTU->applyUpdatesPermissive(Updates);
772     DTU->deleteBB(PredBB);
773     // Recalculation of DomTree is needed when updating a forward DomTree and
774     // the Entry BB is replaced.
775     if (ReplaceEntryBB && DTU->hasDomTree()) {
776       // The entry block was removed and there is no external interface for
777       // the dominator tree to be notified of this change. In this corner-case
778       // we recalculate the entire tree.
779       DTU->recalculate(*(DestBB->getParent()));
780     }
781   }
782 
783   else {
784     PredBB->eraseFromParent(); // Nuke BB if DTU is nullptr.
785   }
786 }
787 
788 /// Return true if we can choose one of these values to use in place of the
789 /// other. Note that we will always choose the non-undef value to keep.
790 static bool CanMergeValues(Value *First, Value *Second) {
791   return First == Second || isa<UndefValue>(First) || isa<UndefValue>(Second);
792 }
793 
794 /// Return true if we can fold BB, an almost-empty BB ending in an unconditional
795 /// branch to Succ, into Succ.
796 ///
797 /// Assumption: Succ is the single successor for BB.
798 static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
799   assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");
800 
801   LLVM_DEBUG(dbgs() << "Looking to fold " << BB->getName() << " into "
802                     << Succ->getName() << "\n");
803   // Shortcut, if there is only a single predecessor it must be BB and merging
804   // is always safe
805   if (Succ->getSinglePredecessor()) return true;
806 
807   // Make a list of the predecessors of BB
808   SmallPtrSet<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB));
809 
810   // Look at all the phi nodes in Succ, to see if they present a conflict when
811   // merging these blocks
812   for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
813     PHINode *PN = cast<PHINode>(I);
814 
815     // If the incoming value from BB is again a PHINode in
816     // BB which has the same incoming value for *PI as PN does, we can
817     // merge the phi nodes and then the blocks can still be merged
818     PHINode *BBPN = dyn_cast<PHINode>(PN->getIncomingValueForBlock(BB));
819     if (BBPN && BBPN->getParent() == BB) {
820       for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) {
821         BasicBlock *IBB = PN->getIncomingBlock(PI);
822         if (BBPreds.count(IBB) &&
823             !CanMergeValues(BBPN->getIncomingValueForBlock(IBB),
824                             PN->getIncomingValue(PI))) {
825           LLVM_DEBUG(dbgs()
826                      << "Can't fold, phi node " << PN->getName() << " in "
827                      << Succ->getName() << " is conflicting with "
828                      << BBPN->getName() << " with regard to common predecessor "
829                      << IBB->getName() << "\n");
830           return false;
831         }
832       }
833     } else {
834       Value* Val = PN->getIncomingValueForBlock(BB);
835       for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) {
836         // See if the incoming value for the common predecessor is equal to the
837         // one for BB, in which case this phi node will not prevent the merging
838         // of the block.
839         BasicBlock *IBB = PN->getIncomingBlock(PI);
840         if (BBPreds.count(IBB) &&
841             !CanMergeValues(Val, PN->getIncomingValue(PI))) {
842           LLVM_DEBUG(dbgs() << "Can't fold, phi node " << PN->getName()
843                             << " in " << Succ->getName()
844                             << " is conflicting with regard to common "
845                             << "predecessor " << IBB->getName() << "\n");
846           return false;
847         }
848       }
849     }
850   }
851 
852   return true;
853 }
854 
855 using PredBlockVector = SmallVector<BasicBlock *, 16>;
856 using IncomingValueMap = DenseMap<BasicBlock *, Value *>;
857 
858 /// Determines the value to use as the phi node input for a block.
859 ///
860 /// Select between \p OldVal any value that we know flows from \p BB
861 /// to a particular phi on the basis of which one (if either) is not
862 /// undef. Update IncomingValues based on the selected value.
863 ///
864 /// \param OldVal The value we are considering selecting.
865 /// \param BB The block that the value flows in from.
866 /// \param IncomingValues A map from block-to-value for other phi inputs
867 /// that we have examined.
868 ///
869 /// \returns the selected value.
870 static Value *selectIncomingValueForBlock(Value *OldVal, BasicBlock *BB,
871                                           IncomingValueMap &IncomingValues) {
872   if (!isa<UndefValue>(OldVal)) {
873     assert((!IncomingValues.count(BB) ||
874             IncomingValues.find(BB)->second == OldVal) &&
875            "Expected OldVal to match incoming value from BB!");
876 
877     IncomingValues.insert(std::make_pair(BB, OldVal));
878     return OldVal;
879   }
880 
881   IncomingValueMap::const_iterator It = IncomingValues.find(BB);
882   if (It != IncomingValues.end()) return It->second;
883 
884   return OldVal;
885 }
886 
887 /// Create a map from block to value for the operands of a
888 /// given phi.
889 ///
890 /// Create a map from block to value for each non-undef value flowing
891 /// into \p PN.
892 ///
893 /// \param PN The phi we are collecting the map for.
894 /// \param IncomingValues [out] The map from block to value for this phi.
895 static void gatherIncomingValuesToPhi(PHINode *PN,
896                                       IncomingValueMap &IncomingValues) {
897   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
898     BasicBlock *BB = PN->getIncomingBlock(i);
899     Value *V = PN->getIncomingValue(i);
900 
901     if (!isa<UndefValue>(V))
902       IncomingValues.insert(std::make_pair(BB, V));
903   }
904 }
905 
906 /// Replace the incoming undef values to a phi with the values
907 /// from a block-to-value map.
908 ///
909 /// \param PN The phi we are replacing the undefs in.
910 /// \param IncomingValues A map from block to value.
911 static void replaceUndefValuesInPhi(PHINode *PN,
912                                     const IncomingValueMap &IncomingValues) {
913   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
914     Value *V = PN->getIncomingValue(i);
915 
916     if (!isa<UndefValue>(V)) continue;
917 
918     BasicBlock *BB = PN->getIncomingBlock(i);
919     IncomingValueMap::const_iterator It = IncomingValues.find(BB);
920     if (It == IncomingValues.end()) continue;
921 
922     PN->setIncomingValue(i, It->second);
923   }
924 }
925 
926 /// Replace a value flowing from a block to a phi with
927 /// potentially multiple instances of that value flowing from the
928 /// block's predecessors to the phi.
929 ///
930 /// \param BB The block with the value flowing into the phi.
931 /// \param BBPreds The predecessors of BB.
932 /// \param PN The phi that we are updating.
933 static void redirectValuesFromPredecessorsToPhi(BasicBlock *BB,
934                                                 const PredBlockVector &BBPreds,
935                                                 PHINode *PN) {
936   Value *OldVal = PN->removeIncomingValue(BB, false);
937   assert(OldVal && "No entry in PHI for Pred BB!");
938 
939   IncomingValueMap IncomingValues;
940 
941   // We are merging two blocks - BB, and the block containing PN - and
942   // as a result we need to redirect edges from the predecessors of BB
943   // to go to the block containing PN, and update PN
944   // accordingly. Since we allow merging blocks in the case where the
945   // predecessor and successor blocks both share some predecessors,
946   // and where some of those common predecessors might have undef
947   // values flowing into PN, we want to rewrite those values to be
948   // consistent with the non-undef values.
949 
950   gatherIncomingValuesToPhi(PN, IncomingValues);
951 
952   // If this incoming value is one of the PHI nodes in BB, the new entries
953   // in the PHI node are the entries from the old PHI.
954   if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) {
955     PHINode *OldValPN = cast<PHINode>(OldVal);
956     for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i) {
957       // Note that, since we are merging phi nodes and BB and Succ might
958       // have common predecessors, we could end up with a phi node with
959       // identical incoming branches. This will be cleaned up later (and
960       // will trigger asserts if we try to clean it up now, without also
961       // simplifying the corresponding conditional branch).
962       BasicBlock *PredBB = OldValPN->getIncomingBlock(i);
963       Value *PredVal = OldValPN->getIncomingValue(i);
964       Value *Selected = selectIncomingValueForBlock(PredVal, PredBB,
965                                                     IncomingValues);
966 
967       // And add a new incoming value for this predecessor for the
968       // newly retargeted branch.
969       PN->addIncoming(Selected, PredBB);
970     }
971   } else {
972     for (unsigned i = 0, e = BBPreds.size(); i != e; ++i) {
973       // Update existing incoming values in PN for this
974       // predecessor of BB.
975       BasicBlock *PredBB = BBPreds[i];
976       Value *Selected = selectIncomingValueForBlock(OldVal, PredBB,
977                                                     IncomingValues);
978 
979       // And add a new incoming value for this predecessor for the
980       // newly retargeted branch.
981       PN->addIncoming(Selected, PredBB);
982     }
983   }
984 
985   replaceUndefValuesInPhi(PN, IncomingValues);
986 }
987 
988 bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
989                                                    DomTreeUpdater *DTU) {
990   assert(BB != &BB->getParent()->getEntryBlock() &&
991          "TryToSimplifyUncondBranchFromEmptyBlock called on entry block!");
992 
993   // We can't eliminate infinite loops.
994   BasicBlock *Succ = cast<BranchInst>(BB->getTerminator())->getSuccessor(0);
995   if (BB == Succ) return false;
996 
997   // Check to see if merging these blocks would cause conflicts for any of the
998   // phi nodes in BB or Succ. If not, we can safely merge.
999   if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false;
1000 
1001   // Check for cases where Succ has multiple predecessors and a PHI node in BB
1002   // has uses which will not disappear when the PHI nodes are merged.  It is
1003   // possible to handle such cases, but difficult: it requires checking whether
1004   // BB dominates Succ, which is non-trivial to calculate in the case where
1005   // Succ has multiple predecessors.  Also, it requires checking whether
1006   // constructing the necessary self-referential PHI node doesn't introduce any
1007   // conflicts; this isn't too difficult, but the previous code for doing this
1008   // was incorrect.
1009   //
1010   // Note that if this check finds a live use, BB dominates Succ, so BB is
1011   // something like a loop pre-header (or rarely, a part of an irreducible CFG);
1012   // folding the branch isn't profitable in that case anyway.
1013   if (!Succ->getSinglePredecessor()) {
1014     BasicBlock::iterator BBI = BB->begin();
1015     while (isa<PHINode>(*BBI)) {
1016       for (Use &U : BBI->uses()) {
1017         if (PHINode* PN = dyn_cast<PHINode>(U.getUser())) {
1018           if (PN->getIncomingBlock(U) != BB)
1019             return false;
1020         } else {
1021           return false;
1022         }
1023       }
1024       ++BBI;
1025     }
1026   }
1027 
1028   // We cannot fold the block if it's a branch to an already present callbr
1029   // successor because that creates duplicate successors.
1030   for (auto I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
1031     if (auto *CBI = dyn_cast<CallBrInst>((*I)->getTerminator())) {
1032       if (Succ == CBI->getDefaultDest())
1033         return false;
1034       for (unsigned i = 0, e = CBI->getNumIndirectDests(); i != e; ++i)
1035         if (Succ == CBI->getIndirectDest(i))
1036           return false;
1037     }
1038   }
1039 
1040   LLVM_DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB);
1041 
1042   SmallVector<DominatorTree::UpdateType, 32> Updates;
1043   if (DTU) {
1044     Updates.push_back({DominatorTree::Delete, BB, Succ});
1045     // All predecessors of BB will be moved to Succ.
1046     for (auto I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
1047       Updates.push_back({DominatorTree::Delete, *I, BB});
1048       // This predecessor of BB may already have Succ as a successor.
1049       if (llvm::find(successors(*I), Succ) == succ_end(*I))
1050         Updates.push_back({DominatorTree::Insert, *I, Succ});
1051     }
1052   }
1053 
1054   if (isa<PHINode>(Succ->begin())) {
1055     // If there is more than one pred of succ, and there are PHI nodes in
1056     // the successor, then we need to add incoming edges for the PHI nodes
1057     //
1058     const PredBlockVector BBPreds(pred_begin(BB), pred_end(BB));
1059 
1060     // Loop over all of the PHI nodes in the successor of BB.
1061     for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
1062       PHINode *PN = cast<PHINode>(I);
1063 
1064       redirectValuesFromPredecessorsToPhi(BB, BBPreds, PN);
1065     }
1066   }
1067 
1068   if (Succ->getSinglePredecessor()) {
1069     // BB is the only predecessor of Succ, so Succ will end up with exactly
1070     // the same predecessors BB had.
1071 
1072     // Copy over any phi, debug or lifetime instruction.
1073     BB->getTerminator()->eraseFromParent();
1074     Succ->getInstList().splice(Succ->getFirstNonPHI()->getIterator(),
1075                                BB->getInstList());
1076   } else {
1077     while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) {
1078       // We explicitly check for such uses in CanPropagatePredecessorsForPHIs.
1079       assert(PN->use_empty() && "There shouldn't be any uses here!");
1080       PN->eraseFromParent();
1081     }
1082   }
1083 
1084   // If the unconditional branch we replaced contains llvm.loop metadata, we
1085   // add the metadata to the branch instructions in the predecessors.
1086   unsigned LoopMDKind = BB->getContext().getMDKindID("llvm.loop");
1087   Instruction *TI = BB->getTerminator();
1088   if (TI)
1089     if (MDNode *LoopMD = TI->getMetadata(LoopMDKind))
1090       for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
1091         BasicBlock *Pred = *PI;
1092         Pred->getTerminator()->setMetadata(LoopMDKind, LoopMD);
1093       }
1094 
1095   // Everything that jumped to BB now goes to Succ.
1096   BB->replaceAllUsesWith(Succ);
1097   if (!Succ->hasName()) Succ->takeName(BB);
1098 
1099   // Clear the successor list of BB to match updates applying to DTU later.
1100   if (BB->getTerminator())
1101     BB->getInstList().pop_back();
1102   new UnreachableInst(BB->getContext(), BB);
1103   assert(succ_empty(BB) && "The successor list of BB isn't empty before "
1104                            "applying corresponding DTU updates.");
1105 
1106   if (DTU) {
1107     DTU->applyUpdatesPermissive(Updates);
1108     DTU->deleteBB(BB);
1109   } else {
1110     BB->eraseFromParent(); // Delete the old basic block.
1111   }
1112   return true;
1113 }
1114 
1115 bool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) {
1116   // This implementation doesn't currently consider undef operands
1117   // specially. Theoretically, two phis which are identical except for
1118   // one having an undef where the other doesn't could be collapsed.
1119 
1120   struct PHIDenseMapInfo {
1121     static PHINode *getEmptyKey() {
1122       return DenseMapInfo<PHINode *>::getEmptyKey();
1123     }
1124 
1125     static PHINode *getTombstoneKey() {
1126       return DenseMapInfo<PHINode *>::getTombstoneKey();
1127     }
1128 
1129     static unsigned getHashValue(PHINode *PN) {
1130       // Compute a hash value on the operands. Instcombine will likely have
1131       // sorted them, which helps expose duplicates, but we have to check all
1132       // the operands to be safe in case instcombine hasn't run.
1133       return static_cast<unsigned>(hash_combine(
1134           hash_combine_range(PN->value_op_begin(), PN->value_op_end()),
1135           hash_combine_range(PN->block_begin(), PN->block_end())));
1136     }
1137 
1138     static bool isEqual(PHINode *LHS, PHINode *RHS) {
1139       if (LHS == getEmptyKey() || LHS == getTombstoneKey() ||
1140           RHS == getEmptyKey() || RHS == getTombstoneKey())
1141         return LHS == RHS;
1142       return LHS->isIdenticalTo(RHS);
1143     }
1144   };
1145 
1146   // Set of unique PHINodes.
1147   DenseSet<PHINode *, PHIDenseMapInfo> PHISet;
1148 
1149   // Examine each PHI.
1150   bool Changed = false;
1151   for (auto I = BB->begin(); PHINode *PN = dyn_cast<PHINode>(I++);) {
1152     auto Inserted = PHISet.insert(PN);
1153     if (!Inserted.second) {
1154       // A duplicate. Replace this PHI with its duplicate.
1155       PN->replaceAllUsesWith(*Inserted.first);
1156       PN->eraseFromParent();
1157       Changed = true;
1158 
1159       // The RAUW can change PHIs that we already visited. Start over from the
1160       // beginning.
1161       PHISet.clear();
1162       I = BB->begin();
1163     }
1164   }
1165 
1166   return Changed;
1167 }
1168 
1169 /// enforceKnownAlignment - If the specified pointer points to an object that
1170 /// we control, modify the object's alignment to PrefAlign. This isn't
1171 /// often possible though. If alignment is important, a more reliable approach
1172 /// is to simply align all global variables and allocation instructions to
1173 /// their preferred alignment from the beginning.
1174 static Align enforceKnownAlignment(Value *V, Align Alignment, Align PrefAlign,
1175                                    const DataLayout &DL) {
1176   assert(PrefAlign > Alignment);
1177 
1178   V = V->stripPointerCasts();
1179 
1180   if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1181     // TODO: ideally, computeKnownBits ought to have used
1182     // AllocaInst::getAlignment() in its computation already, making
1183     // the below max redundant. But, as it turns out,
1184     // stripPointerCasts recurses through infinite layers of bitcasts,
1185     // while computeKnownBits is not allowed to traverse more than 6
1186     // levels.
1187     Alignment = std::max(AI->getAlign(), Alignment);
1188     if (PrefAlign <= Alignment)
1189       return Alignment;
1190 
1191     // If the preferred alignment is greater than the natural stack alignment
1192     // then don't round up. This avoids dynamic stack realignment.
1193     if (DL.exceedsNaturalStackAlignment(PrefAlign))
1194       return Alignment;
1195     AI->setAlignment(PrefAlign);
1196     return PrefAlign;
1197   }
1198 
1199   if (auto *GO = dyn_cast<GlobalObject>(V)) {
1200     // TODO: as above, this shouldn't be necessary.
1201     Alignment = max(GO->getAlign(), Alignment);
1202     if (PrefAlign <= Alignment)
1203       return Alignment;
1204 
1205     // If there is a large requested alignment and we can, bump up the alignment
1206     // of the global.  If the memory we set aside for the global may not be the
1207     // memory used by the final program then it is impossible for us to reliably
1208     // enforce the preferred alignment.
1209     if (!GO->canIncreaseAlignment())
1210       return Alignment;
1211 
1212     GO->setAlignment(PrefAlign);
1213     return PrefAlign;
1214   }
1215 
1216   return Alignment;
1217 }
1218 
1219 Align llvm::getOrEnforceKnownAlignment(Value *V, MaybeAlign PrefAlign,
1220                                        const DataLayout &DL,
1221                                        const Instruction *CxtI,
1222                                        AssumptionCache *AC,
1223                                        const DominatorTree *DT) {
1224   assert(V->getType()->isPointerTy() &&
1225          "getOrEnforceKnownAlignment expects a pointer!");
1226 
1227   KnownBits Known = computeKnownBits(V, DL, 0, AC, CxtI, DT);
1228   unsigned TrailZ = Known.countMinTrailingZeros();
1229 
1230   // Avoid trouble with ridiculously large TrailZ values, such as
1231   // those computed from a null pointer.
1232   // LLVM doesn't support alignments larger than (1 << MaxAlignmentExponent).
1233   TrailZ = std::min(TrailZ, +Value::MaxAlignmentExponent);
1234 
1235   Align Alignment = Align(1ull << std::min(Known.getBitWidth() - 1, TrailZ));
1236 
1237   if (PrefAlign && *PrefAlign > Alignment)
1238     Alignment = enforceKnownAlignment(V, Alignment, *PrefAlign, DL);
1239 
1240   // We don't need to make any adjustment.
1241   return Alignment;
1242 }
1243 
1244 ///===---------------------------------------------------------------------===//
1245 ///  Dbg Intrinsic utilities
1246 ///
1247 
1248 /// See if there is a dbg.value intrinsic for DIVar for the PHI node.
1249 static bool PhiHasDebugValue(DILocalVariable *DIVar,
1250                              DIExpression *DIExpr,
1251                              PHINode *APN) {
1252   // Since we can't guarantee that the original dbg.declare instrinsic
1253   // is removed by LowerDbgDeclare(), we need to make sure that we are
1254   // not inserting the same dbg.value intrinsic over and over.
1255   SmallVector<DbgValueInst *, 1> DbgValues;
1256   findDbgValues(DbgValues, APN);
1257   for (auto *DVI : DbgValues) {
1258     assert(DVI->getValue() == APN);
1259     if ((DVI->getVariable() == DIVar) && (DVI->getExpression() == DIExpr))
1260       return true;
1261   }
1262   return false;
1263 }
1264 
1265 /// Check if the alloc size of \p ValTy is large enough to cover the variable
1266 /// (or fragment of the variable) described by \p DII.
1267 ///
1268 /// This is primarily intended as a helper for the different
1269 /// ConvertDebugDeclareToDebugValue functions. The dbg.declare/dbg.addr that is
1270 /// converted describes an alloca'd variable, so we need to use the
1271 /// alloc size of the value when doing the comparison. E.g. an i1 value will be
1272 /// identified as covering an n-bit fragment, if the store size of i1 is at
1273 /// least n bits.
1274 static bool valueCoversEntireFragment(Type *ValTy, DbgVariableIntrinsic *DII) {
1275   const DataLayout &DL = DII->getModule()->getDataLayout();
1276   uint64_t ValueSize = DL.getTypeAllocSizeInBits(ValTy);
1277   if (auto FragmentSize = DII->getFragmentSizeInBits())
1278     return ValueSize >= *FragmentSize;
1279   // We can't always calculate the size of the DI variable (e.g. if it is a
1280   // VLA). Try to use the size of the alloca that the dbg intrinsic describes
1281   // intead.
1282   if (DII->isAddressOfVariable())
1283     if (auto *AI = dyn_cast_or_null<AllocaInst>(DII->getVariableLocation()))
1284       if (auto FragmentSize = AI->getAllocationSizeInBits(DL))
1285         return ValueSize >= *FragmentSize;
1286   // Could not determine size of variable. Conservatively return false.
1287   return false;
1288 }
1289 
1290 /// Produce a DebugLoc to use for each dbg.declare/inst pair that are promoted
1291 /// to a dbg.value. Because no machine insts can come from debug intrinsics,
1292 /// only the scope and inlinedAt is significant. Zero line numbers are used in
1293 /// case this DebugLoc leaks into any adjacent instructions.
1294 static DebugLoc getDebugValueLoc(DbgVariableIntrinsic *DII, Instruction *Src) {
1295   // Original dbg.declare must have a location.
1296   DebugLoc DeclareLoc = DII->getDebugLoc();
1297   MDNode *Scope = DeclareLoc.getScope();
1298   DILocation *InlinedAt = DeclareLoc.getInlinedAt();
1299   // Produce an unknown location with the correct scope / inlinedAt fields.
1300   return DebugLoc::get(0, 0, Scope, InlinedAt);
1301 }
1302 
1303 /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
1304 /// that has an associated llvm.dbg.declare or llvm.dbg.addr intrinsic.
1305 void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
1306                                            StoreInst *SI, DIBuilder &Builder) {
1307   assert(DII->isAddressOfVariable());
1308   auto *DIVar = DII->getVariable();
1309   assert(DIVar && "Missing variable");
1310   auto *DIExpr = DII->getExpression();
1311   Value *DV = SI->getValueOperand();
1312 
1313   DebugLoc NewLoc = getDebugValueLoc(DII, SI);
1314 
1315   if (!valueCoversEntireFragment(DV->getType(), DII)) {
1316     // FIXME: If storing to a part of the variable described by the dbg.declare,
1317     // then we want to insert a dbg.value for the corresponding fragment.
1318     LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: "
1319                       << *DII << '\n');
1320     // For now, when there is a store to parts of the variable (but we do not
1321     // know which part) we insert an dbg.value instrinsic to indicate that we
1322     // know nothing about the variable's content.
1323     DV = UndefValue::get(DV->getType());
1324     Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, NewLoc, SI);
1325     return;
1326   }
1327 
1328   Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, NewLoc, SI);
1329 }
1330 
1331 /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
1332 /// that has an associated llvm.dbg.declare or llvm.dbg.addr intrinsic.
1333 void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
1334                                            LoadInst *LI, DIBuilder &Builder) {
1335   auto *DIVar = DII->getVariable();
1336   auto *DIExpr = DII->getExpression();
1337   assert(DIVar && "Missing variable");
1338 
1339   if (!valueCoversEntireFragment(LI->getType(), DII)) {
1340     // FIXME: If only referring to a part of the variable described by the
1341     // dbg.declare, then we want to insert a dbg.value for the corresponding
1342     // fragment.
1343     LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: "
1344                       << *DII << '\n');
1345     return;
1346   }
1347 
1348   DebugLoc NewLoc = getDebugValueLoc(DII, nullptr);
1349 
1350   // We are now tracking the loaded value instead of the address. In the
1351   // future if multi-location support is added to the IR, it might be
1352   // preferable to keep tracking both the loaded value and the original
1353   // address in case the alloca can not be elided.
1354   Instruction *DbgValue = Builder.insertDbgValueIntrinsic(
1355       LI, DIVar, DIExpr, NewLoc, (Instruction *)nullptr);
1356   DbgValue->insertAfter(LI);
1357 }
1358 
1359 /// Inserts a llvm.dbg.value intrinsic after a phi that has an associated
1360 /// llvm.dbg.declare or llvm.dbg.addr intrinsic.
1361 void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
1362                                            PHINode *APN, DIBuilder &Builder) {
1363   auto *DIVar = DII->getVariable();
1364   auto *DIExpr = DII->getExpression();
1365   assert(DIVar && "Missing variable");
1366 
1367   if (PhiHasDebugValue(DIVar, DIExpr, APN))
1368     return;
1369 
1370   if (!valueCoversEntireFragment(APN->getType(), DII)) {
1371     // FIXME: If only referring to a part of the variable described by the
1372     // dbg.declare, then we want to insert a dbg.value for the corresponding
1373     // fragment.
1374     LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: "
1375                       << *DII << '\n');
1376     return;
1377   }
1378 
1379   BasicBlock *BB = APN->getParent();
1380   auto InsertionPt = BB->getFirstInsertionPt();
1381 
1382   DebugLoc NewLoc = getDebugValueLoc(DII, nullptr);
1383 
1384   // The block may be a catchswitch block, which does not have a valid
1385   // insertion point.
1386   // FIXME: Insert dbg.value markers in the successors when appropriate.
1387   if (InsertionPt != BB->end())
1388     Builder.insertDbgValueIntrinsic(APN, DIVar, DIExpr, NewLoc, &*InsertionPt);
1389 }
1390 
1391 /// Determine whether this alloca is either a VLA or an array.
1392 static bool isArray(AllocaInst *AI) {
1393   return AI->isArrayAllocation() ||
1394          (AI->getAllocatedType() && AI->getAllocatedType()->isArrayTy());
1395 }
1396 
1397 /// Determine whether this alloca is a structure.
1398 static bool isStructure(AllocaInst *AI) {
1399   return AI->getAllocatedType() && AI->getAllocatedType()->isStructTy();
1400 }
1401 
1402 /// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set
1403 /// of llvm.dbg.value intrinsics.
1404 bool llvm::LowerDbgDeclare(Function &F) {
1405   bool Changed = false;
1406   DIBuilder DIB(*F.getParent(), /*AllowUnresolved*/ false);
1407   SmallVector<DbgDeclareInst *, 4> Dbgs;
1408   for (auto &FI : F)
1409     for (Instruction &BI : FI)
1410       if (auto DDI = dyn_cast<DbgDeclareInst>(&BI))
1411         Dbgs.push_back(DDI);
1412 
1413   if (Dbgs.empty())
1414     return Changed;
1415 
1416   for (auto &I : Dbgs) {
1417     DbgDeclareInst *DDI = I;
1418     AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress());
1419     // If this is an alloca for a scalar variable, insert a dbg.value
1420     // at each load and store to the alloca and erase the dbg.declare.
1421     // The dbg.values allow tracking a variable even if it is not
1422     // stored on the stack, while the dbg.declare can only describe
1423     // the stack slot (and at a lexical-scope granularity). Later
1424     // passes will attempt to elide the stack slot.
1425     if (!AI || isArray(AI) || isStructure(AI))
1426       continue;
1427 
1428     // A volatile load/store means that the alloca can't be elided anyway.
1429     if (llvm::any_of(AI->users(), [](User *U) -> bool {
1430           if (LoadInst *LI = dyn_cast<LoadInst>(U))
1431             return LI->isVolatile();
1432           if (StoreInst *SI = dyn_cast<StoreInst>(U))
1433             return SI->isVolatile();
1434           return false;
1435         }))
1436       continue;
1437 
1438     SmallVector<const Value *, 8> WorkList;
1439     WorkList.push_back(AI);
1440     while (!WorkList.empty()) {
1441       const Value *V = WorkList.pop_back_val();
1442       for (auto &AIUse : V->uses()) {
1443         User *U = AIUse.getUser();
1444         if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
1445           if (AIUse.getOperandNo() == 1)
1446             ConvertDebugDeclareToDebugValue(DDI, SI, DIB);
1447         } else if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
1448           ConvertDebugDeclareToDebugValue(DDI, LI, DIB);
1449         } else if (CallInst *CI = dyn_cast<CallInst>(U)) {
1450           // This is a call by-value or some other instruction that takes a
1451           // pointer to the variable. Insert a *value* intrinsic that describes
1452           // the variable by dereferencing the alloca.
1453           if (!CI->isLifetimeStartOrEnd()) {
1454             DebugLoc NewLoc = getDebugValueLoc(DDI, nullptr);
1455             auto *DerefExpr =
1456                 DIExpression::append(DDI->getExpression(), dwarf::DW_OP_deref);
1457             DIB.insertDbgValueIntrinsic(AI, DDI->getVariable(), DerefExpr,
1458                                         NewLoc, CI);
1459           }
1460         } else if (BitCastInst *BI = dyn_cast<BitCastInst>(U)) {
1461           if (BI->getType()->isPointerTy())
1462             WorkList.push_back(BI);
1463         }
1464       }
1465     }
1466     DDI->eraseFromParent();
1467     Changed = true;
1468   }
1469 
1470   if (Changed)
1471   for (BasicBlock &BB : F)
1472     RemoveRedundantDbgInstrs(&BB);
1473 
1474   return Changed;
1475 }
1476 
1477 /// Propagate dbg.value intrinsics through the newly inserted PHIs.
1478 void llvm::insertDebugValuesForPHIs(BasicBlock *BB,
1479                                     SmallVectorImpl<PHINode *> &InsertedPHIs) {
1480   assert(BB && "No BasicBlock to clone dbg.value(s) from.");
1481   if (InsertedPHIs.size() == 0)
1482     return;
1483 
1484   // Map existing PHI nodes to their dbg.values.
1485   ValueToValueMapTy DbgValueMap;
1486   for (auto &I : *BB) {
1487     if (auto DbgII = dyn_cast<DbgVariableIntrinsic>(&I)) {
1488       if (auto *Loc = dyn_cast_or_null<PHINode>(DbgII->getVariableLocation()))
1489         DbgValueMap.insert({Loc, DbgII});
1490     }
1491   }
1492   if (DbgValueMap.size() == 0)
1493     return;
1494 
1495   // Then iterate through the new PHIs and look to see if they use one of the
1496   // previously mapped PHIs. If so, insert a new dbg.value intrinsic that will
1497   // propagate the info through the new PHI.
1498   LLVMContext &C = BB->getContext();
1499   for (auto PHI : InsertedPHIs) {
1500     BasicBlock *Parent = PHI->getParent();
1501     // Avoid inserting an intrinsic into an EH block.
1502     if (Parent->getFirstNonPHI()->isEHPad())
1503       continue;
1504     auto PhiMAV = MetadataAsValue::get(C, ValueAsMetadata::get(PHI));
1505     for (auto VI : PHI->operand_values()) {
1506       auto V = DbgValueMap.find(VI);
1507       if (V != DbgValueMap.end()) {
1508         auto *DbgII = cast<DbgVariableIntrinsic>(V->second);
1509         Instruction *NewDbgII = DbgII->clone();
1510         NewDbgII->setOperand(0, PhiMAV);
1511         auto InsertionPt = Parent->getFirstInsertionPt();
1512         assert(InsertionPt != Parent->end() && "Ill-formed basic block");
1513         NewDbgII->insertBefore(&*InsertionPt);
1514       }
1515     }
1516   }
1517 }
1518 
1519 /// Finds all intrinsics declaring local variables as living in the memory that
1520 /// 'V' points to. This may include a mix of dbg.declare and
1521 /// dbg.addr intrinsics.
1522 TinyPtrVector<DbgVariableIntrinsic *> llvm::FindDbgAddrUses(Value *V) {
1523   // This function is hot. Check whether the value has any metadata to avoid a
1524   // DenseMap lookup.
1525   if (!V->isUsedByMetadata())
1526     return {};
1527   auto *L = LocalAsMetadata::getIfExists(V);
1528   if (!L)
1529     return {};
1530   auto *MDV = MetadataAsValue::getIfExists(V->getContext(), L);
1531   if (!MDV)
1532     return {};
1533 
1534   TinyPtrVector<DbgVariableIntrinsic *> Declares;
1535   for (User *U : MDV->users()) {
1536     if (auto *DII = dyn_cast<DbgVariableIntrinsic>(U))
1537       if (DII->isAddressOfVariable())
1538         Declares.push_back(DII);
1539   }
1540 
1541   return Declares;
1542 }
1543 
1544 TinyPtrVector<DbgDeclareInst *> llvm::FindDbgDeclareUses(Value *V) {
1545   TinyPtrVector<DbgDeclareInst *> DDIs;
1546   for (DbgVariableIntrinsic *DVI : FindDbgAddrUses(V))
1547     if (auto *DDI = dyn_cast<DbgDeclareInst>(DVI))
1548       DDIs.push_back(DDI);
1549   return DDIs;
1550 }
1551 
1552 void llvm::findDbgValues(SmallVectorImpl<DbgValueInst *> &DbgValues, Value *V) {
1553   // This function is hot. Check whether the value has any metadata to avoid a
1554   // DenseMap lookup.
1555   if (!V->isUsedByMetadata())
1556     return;
1557   if (auto *L = LocalAsMetadata::getIfExists(V))
1558     if (auto *MDV = MetadataAsValue::getIfExists(V->getContext(), L))
1559       for (User *U : MDV->users())
1560         if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(U))
1561           DbgValues.push_back(DVI);
1562 }
1563 
1564 void llvm::findDbgUsers(SmallVectorImpl<DbgVariableIntrinsic *> &DbgUsers,
1565                         Value *V) {
1566   // This function is hot. Check whether the value has any metadata to avoid a
1567   // DenseMap lookup.
1568   if (!V->isUsedByMetadata())
1569     return;
1570   if (auto *L = LocalAsMetadata::getIfExists(V))
1571     if (auto *MDV = MetadataAsValue::getIfExists(V->getContext(), L))
1572       for (User *U : MDV->users())
1573         if (DbgVariableIntrinsic *DII = dyn_cast<DbgVariableIntrinsic>(U))
1574           DbgUsers.push_back(DII);
1575 }
1576 
1577 bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress,
1578                              DIBuilder &Builder, uint8_t DIExprFlags,
1579                              int Offset) {
1580   auto DbgAddrs = FindDbgAddrUses(Address);
1581   for (DbgVariableIntrinsic *DII : DbgAddrs) {
1582     DebugLoc Loc = DII->getDebugLoc();
1583     auto *DIVar = DII->getVariable();
1584     auto *DIExpr = DII->getExpression();
1585     assert(DIVar && "Missing variable");
1586     DIExpr = DIExpression::prepend(DIExpr, DIExprFlags, Offset);
1587     // Insert llvm.dbg.declare immediately before DII, and remove old
1588     // llvm.dbg.declare.
1589     Builder.insertDeclare(NewAddress, DIVar, DIExpr, Loc, DII);
1590     DII->eraseFromParent();
1591   }
1592   return !DbgAddrs.empty();
1593 }
1594 
1595 static void replaceOneDbgValueForAlloca(DbgValueInst *DVI, Value *NewAddress,
1596                                         DIBuilder &Builder, int Offset) {
1597   DebugLoc Loc = DVI->getDebugLoc();
1598   auto *DIVar = DVI->getVariable();
1599   auto *DIExpr = DVI->getExpression();
1600   assert(DIVar && "Missing variable");
1601 
1602   // This is an alloca-based llvm.dbg.value. The first thing it should do with
1603   // the alloca pointer is dereference it. Otherwise we don't know how to handle
1604   // it and give up.
1605   if (!DIExpr || DIExpr->getNumElements() < 1 ||
1606       DIExpr->getElement(0) != dwarf::DW_OP_deref)
1607     return;
1608 
1609   // Insert the offset before the first deref.
1610   // We could just change the offset argument of dbg.value, but it's unsigned...
1611   if (Offset)
1612     DIExpr = DIExpression::prepend(DIExpr, 0, Offset);
1613 
1614   Builder.insertDbgValueIntrinsic(NewAddress, DIVar, DIExpr, Loc, DVI);
1615   DVI->eraseFromParent();
1616 }
1617 
1618 void llvm::replaceDbgValueForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
1619                                     DIBuilder &Builder, int Offset) {
1620   if (auto *L = LocalAsMetadata::getIfExists(AI))
1621     if (auto *MDV = MetadataAsValue::getIfExists(AI->getContext(), L))
1622       for (auto UI = MDV->use_begin(), UE = MDV->use_end(); UI != UE;) {
1623         Use &U = *UI++;
1624         if (auto *DVI = dyn_cast<DbgValueInst>(U.getUser()))
1625           replaceOneDbgValueForAlloca(DVI, NewAllocaAddress, Builder, Offset);
1626       }
1627 }
1628 
1629 /// Wrap \p V in a ValueAsMetadata instance.
1630 static MetadataAsValue *wrapValueInMetadata(LLVMContext &C, Value *V) {
1631   return MetadataAsValue::get(C, ValueAsMetadata::get(V));
1632 }
1633 
1634 /// Where possible to salvage debug information for \p I do so
1635 /// and return True. If not possible mark undef and return False.
1636 void llvm::salvageDebugInfo(Instruction &I) {
1637   SmallVector<DbgVariableIntrinsic *, 1> DbgUsers;
1638   findDbgUsers(DbgUsers, &I);
1639   salvageDebugInfoForDbgValues(I, DbgUsers);
1640 }
1641 
1642 void llvm::salvageDebugInfoForDbgValues(
1643     Instruction &I, ArrayRef<DbgVariableIntrinsic *> DbgUsers) {
1644   auto &Ctx = I.getContext();
1645   bool Salvaged = false;
1646   auto wrapMD = [&](Value *V) { return wrapValueInMetadata(Ctx, V); };
1647 
1648   for (auto *DII : DbgUsers) {
1649     // Do not add DW_OP_stack_value for DbgDeclare and DbgAddr, because they
1650     // are implicitly pointing out the value as a DWARF memory location
1651     // description.
1652     bool StackValue = isa<DbgValueInst>(DII);
1653 
1654     DIExpression *DIExpr =
1655         salvageDebugInfoImpl(I, DII->getExpression(), StackValue);
1656 
1657     // salvageDebugInfoImpl should fail on examining the first element of
1658     // DbgUsers, or none of them.
1659     if (!DIExpr)
1660       break;
1661 
1662     DII->setOperand(0, wrapMD(I.getOperand(0)));
1663     DII->setOperand(2, MetadataAsValue::get(Ctx, DIExpr));
1664     LLVM_DEBUG(dbgs() << "SALVAGE: " << *DII << '\n');
1665     Salvaged = true;
1666   }
1667 
1668   if (Salvaged)
1669     return;
1670 
1671   for (auto *DII : DbgUsers) {
1672     Value *Undef = UndefValue::get(I.getType());
1673     DII->setOperand(0, MetadataAsValue::get(DII->getContext(),
1674                                             ValueAsMetadata::get(Undef)));
1675   }
1676 }
1677 
1678 DIExpression *llvm::salvageDebugInfoImpl(Instruction &I,
1679                                          DIExpression *SrcDIExpr,
1680                                          bool WithStackValue) {
1681   auto &M = *I.getModule();
1682   auto &DL = M.getDataLayout();
1683 
1684   // Apply a vector of opcodes to the source DIExpression.
1685   auto doSalvage = [&](SmallVectorImpl<uint64_t> &Ops) -> DIExpression * {
1686     DIExpression *DIExpr = SrcDIExpr;
1687     if (!Ops.empty()) {
1688       DIExpr = DIExpression::prependOpcodes(DIExpr, Ops, WithStackValue);
1689     }
1690     return DIExpr;
1691   };
1692 
1693   // Apply the given offset to the source DIExpression.
1694   auto applyOffset = [&](uint64_t Offset) -> DIExpression * {
1695     SmallVector<uint64_t, 8> Ops;
1696     DIExpression::appendOffset(Ops, Offset);
1697     return doSalvage(Ops);
1698   };
1699 
1700   // initializer-list helper for applying operators to the source DIExpression.
1701   auto applyOps = [&](ArrayRef<uint64_t> Opcodes) -> DIExpression * {
1702     SmallVector<uint64_t, 8> Ops(Opcodes.begin(), Opcodes.end());
1703     return doSalvage(Ops);
1704   };
1705 
1706   if (auto *CI = dyn_cast<CastInst>(&I)) {
1707     // No-op casts are irrelevant for debug info.
1708     if (CI->isNoopCast(DL))
1709       return SrcDIExpr;
1710 
1711     Type *Type = CI->getType();
1712     // Casts other than Trunc, SExt, or ZExt to scalar types cannot be salvaged.
1713     if (Type->isVectorTy() ||
1714         !(isa<TruncInst>(&I) || isa<SExtInst>(&I) || isa<ZExtInst>(&I)))
1715       return nullptr;
1716 
1717     Value *FromValue = CI->getOperand(0);
1718     unsigned FromTypeBitSize = FromValue->getType()->getScalarSizeInBits();
1719     unsigned ToTypeBitSize = Type->getScalarSizeInBits();
1720 
1721     return applyOps(DIExpression::getExtOps(FromTypeBitSize, ToTypeBitSize,
1722                                             isa<SExtInst>(&I)));
1723   }
1724 
1725   if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
1726     unsigned BitWidth =
1727         M.getDataLayout().getIndexSizeInBits(GEP->getPointerAddressSpace());
1728     // Rewrite a constant GEP into a DIExpression.
1729     APInt Offset(BitWidth, 0);
1730     if (GEP->accumulateConstantOffset(M.getDataLayout(), Offset)) {
1731       return applyOffset(Offset.getSExtValue());
1732     } else {
1733       return nullptr;
1734     }
1735   } else if (auto *BI = dyn_cast<BinaryOperator>(&I)) {
1736     // Rewrite binary operations with constant integer operands.
1737     auto *ConstInt = dyn_cast<ConstantInt>(I.getOperand(1));
1738     if (!ConstInt || ConstInt->getBitWidth() > 64)
1739       return nullptr;
1740 
1741     uint64_t Val = ConstInt->getSExtValue();
1742     switch (BI->getOpcode()) {
1743     case Instruction::Add:
1744       return applyOffset(Val);
1745     case Instruction::Sub:
1746       return applyOffset(-int64_t(Val));
1747     case Instruction::Mul:
1748       return applyOps({dwarf::DW_OP_constu, Val, dwarf::DW_OP_mul});
1749     case Instruction::SDiv:
1750       return applyOps({dwarf::DW_OP_constu, Val, dwarf::DW_OP_div});
1751     case Instruction::SRem:
1752       return applyOps({dwarf::DW_OP_constu, Val, dwarf::DW_OP_mod});
1753     case Instruction::Or:
1754       return applyOps({dwarf::DW_OP_constu, Val, dwarf::DW_OP_or});
1755     case Instruction::And:
1756       return applyOps({dwarf::DW_OP_constu, Val, dwarf::DW_OP_and});
1757     case Instruction::Xor:
1758       return applyOps({dwarf::DW_OP_constu, Val, dwarf::DW_OP_xor});
1759     case Instruction::Shl:
1760       return applyOps({dwarf::DW_OP_constu, Val, dwarf::DW_OP_shl});
1761     case Instruction::LShr:
1762       return applyOps({dwarf::DW_OP_constu, Val, dwarf::DW_OP_shr});
1763     case Instruction::AShr:
1764       return applyOps({dwarf::DW_OP_constu, Val, dwarf::DW_OP_shra});
1765     default:
1766       // TODO: Salvage constants from each kind of binop we know about.
1767       return nullptr;
1768     }
1769     // *Not* to do: we should not attempt to salvage load instructions,
1770     // because the validity and lifetime of a dbg.value containing
1771     // DW_OP_deref becomes difficult to analyze. See PR40628 for examples.
1772   }
1773   return nullptr;
1774 }
1775 
1776 /// A replacement for a dbg.value expression.
1777 using DbgValReplacement = Optional<DIExpression *>;
1778 
1779 /// Point debug users of \p From to \p To using exprs given by \p RewriteExpr,
1780 /// possibly moving/undefing users to prevent use-before-def. Returns true if
1781 /// changes are made.
1782 static bool rewriteDebugUsers(
1783     Instruction &From, Value &To, Instruction &DomPoint, DominatorTree &DT,
1784     function_ref<DbgValReplacement(DbgVariableIntrinsic &DII)> RewriteExpr) {
1785   // Find debug users of From.
1786   SmallVector<DbgVariableIntrinsic *, 1> Users;
1787   findDbgUsers(Users, &From);
1788   if (Users.empty())
1789     return false;
1790 
1791   // Prevent use-before-def of To.
1792   bool Changed = false;
1793   SmallPtrSet<DbgVariableIntrinsic *, 1> UndefOrSalvage;
1794   if (isa<Instruction>(&To)) {
1795     bool DomPointAfterFrom = From.getNextNonDebugInstruction() == &DomPoint;
1796 
1797     for (auto *DII : Users) {
1798       // It's common to see a debug user between From and DomPoint. Move it
1799       // after DomPoint to preserve the variable update without any reordering.
1800       if (DomPointAfterFrom && DII->getNextNonDebugInstruction() == &DomPoint) {
1801         LLVM_DEBUG(dbgs() << "MOVE:  " << *DII << '\n');
1802         DII->moveAfter(&DomPoint);
1803         Changed = true;
1804 
1805       // Users which otherwise aren't dominated by the replacement value must
1806       // be salvaged or deleted.
1807       } else if (!DT.dominates(&DomPoint, DII)) {
1808         UndefOrSalvage.insert(DII);
1809       }
1810     }
1811   }
1812 
1813   // Update debug users without use-before-def risk.
1814   for (auto *DII : Users) {
1815     if (UndefOrSalvage.count(DII))
1816       continue;
1817 
1818     LLVMContext &Ctx = DII->getContext();
1819     DbgValReplacement DVR = RewriteExpr(*DII);
1820     if (!DVR)
1821       continue;
1822 
1823     DII->setOperand(0, wrapValueInMetadata(Ctx, &To));
1824     DII->setOperand(2, MetadataAsValue::get(Ctx, *DVR));
1825     LLVM_DEBUG(dbgs() << "REWRITE:  " << *DII << '\n');
1826     Changed = true;
1827   }
1828 
1829   if (!UndefOrSalvage.empty()) {
1830     // Try to salvage the remaining debug users.
1831     salvageDebugInfo(From);
1832     Changed = true;
1833   }
1834 
1835   return Changed;
1836 }
1837 
1838 /// Check if a bitcast between a value of type \p FromTy to type \p ToTy would
1839 /// losslessly preserve the bits and semantics of the value. This predicate is
1840 /// symmetric, i.e swapping \p FromTy and \p ToTy should give the same result.
1841 ///
1842 /// Note that Type::canLosslesslyBitCastTo is not suitable here because it
1843 /// allows semantically unequivalent bitcasts, such as <2 x i64> -> <4 x i32>,
1844 /// and also does not allow lossless pointer <-> integer conversions.
1845 static bool isBitCastSemanticsPreserving(const DataLayout &DL, Type *FromTy,
1846                                          Type *ToTy) {
1847   // Trivially compatible types.
1848   if (FromTy == ToTy)
1849     return true;
1850 
1851   // Handle compatible pointer <-> integer conversions.
1852   if (FromTy->isIntOrPtrTy() && ToTy->isIntOrPtrTy()) {
1853     bool SameSize = DL.getTypeSizeInBits(FromTy) == DL.getTypeSizeInBits(ToTy);
1854     bool LosslessConversion = !DL.isNonIntegralPointerType(FromTy) &&
1855                               !DL.isNonIntegralPointerType(ToTy);
1856     return SameSize && LosslessConversion;
1857   }
1858 
1859   // TODO: This is not exhaustive.
1860   return false;
1861 }
1862 
1863 bool llvm::replaceAllDbgUsesWith(Instruction &From, Value &To,
1864                                  Instruction &DomPoint, DominatorTree &DT) {
1865   // Exit early if From has no debug users.
1866   if (!From.isUsedByMetadata())
1867     return false;
1868 
1869   assert(&From != &To && "Can't replace something with itself");
1870 
1871   Type *FromTy = From.getType();
1872   Type *ToTy = To.getType();
1873 
1874   auto Identity = [&](DbgVariableIntrinsic &DII) -> DbgValReplacement {
1875     return DII.getExpression();
1876   };
1877 
1878   // Handle no-op conversions.
1879   Module &M = *From.getModule();
1880   const DataLayout &DL = M.getDataLayout();
1881   if (isBitCastSemanticsPreserving(DL, FromTy, ToTy))
1882     return rewriteDebugUsers(From, To, DomPoint, DT, Identity);
1883 
1884   // Handle integer-to-integer widening and narrowing.
1885   // FIXME: Use DW_OP_convert when it's available everywhere.
1886   if (FromTy->isIntegerTy() && ToTy->isIntegerTy()) {
1887     uint64_t FromBits = FromTy->getPrimitiveSizeInBits();
1888     uint64_t ToBits = ToTy->getPrimitiveSizeInBits();
1889     assert(FromBits != ToBits && "Unexpected no-op conversion");
1890 
1891     // When the width of the result grows, assume that a debugger will only
1892     // access the low `FromBits` bits when inspecting the source variable.
1893     if (FromBits < ToBits)
1894       return rewriteDebugUsers(From, To, DomPoint, DT, Identity);
1895 
1896     // The width of the result has shrunk. Use sign/zero extension to describe
1897     // the source variable's high bits.
1898     auto SignOrZeroExt = [&](DbgVariableIntrinsic &DII) -> DbgValReplacement {
1899       DILocalVariable *Var = DII.getVariable();
1900 
1901       // Without knowing signedness, sign/zero extension isn't possible.
1902       auto Signedness = Var->getSignedness();
1903       if (!Signedness)
1904         return None;
1905 
1906       bool Signed = *Signedness == DIBasicType::Signedness::Signed;
1907       return DIExpression::appendExt(DII.getExpression(), ToBits, FromBits,
1908                                      Signed);
1909     };
1910     return rewriteDebugUsers(From, To, DomPoint, DT, SignOrZeroExt);
1911   }
1912 
1913   // TODO: Floating-point conversions, vectors.
1914   return false;
1915 }
1916 
1917 unsigned llvm::removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB) {
1918   unsigned NumDeadInst = 0;
1919   // Delete the instructions backwards, as it has a reduced likelihood of
1920   // having to update as many def-use and use-def chains.
1921   Instruction *EndInst = BB->getTerminator(); // Last not to be deleted.
1922   while (EndInst != &BB->front()) {
1923     // Delete the next to last instruction.
1924     Instruction *Inst = &*--EndInst->getIterator();
1925     if (!Inst->use_empty() && !Inst->getType()->isTokenTy())
1926       Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
1927     if (Inst->isEHPad() || Inst->getType()->isTokenTy()) {
1928       EndInst = Inst;
1929       continue;
1930     }
1931     if (!isa<DbgInfoIntrinsic>(Inst))
1932       ++NumDeadInst;
1933     Inst->eraseFromParent();
1934   }
1935   return NumDeadInst;
1936 }
1937 
1938 unsigned llvm::changeToUnreachable(Instruction *I, bool UseLLVMTrap,
1939                                    bool PreserveLCSSA, DomTreeUpdater *DTU,
1940                                    MemorySSAUpdater *MSSAU) {
1941   BasicBlock *BB = I->getParent();
1942   std::vector <DominatorTree::UpdateType> Updates;
1943 
1944   if (MSSAU)
1945     MSSAU->changeToUnreachable(I);
1946 
1947   // Loop over all of the successors, removing BB's entry from any PHI
1948   // nodes.
1949   if (DTU)
1950     Updates.reserve(BB->getTerminator()->getNumSuccessors());
1951   for (BasicBlock *Successor : successors(BB)) {
1952     Successor->removePredecessor(BB, PreserveLCSSA);
1953     if (DTU)
1954       Updates.push_back({DominatorTree::Delete, BB, Successor});
1955   }
1956   // Insert a call to llvm.trap right before this.  This turns the undefined
1957   // behavior into a hard fail instead of falling through into random code.
1958   if (UseLLVMTrap) {
1959     Function *TrapFn =
1960       Intrinsic::getDeclaration(BB->getParent()->getParent(), Intrinsic::trap);
1961     CallInst *CallTrap = CallInst::Create(TrapFn, "", I);
1962     CallTrap->setDebugLoc(I->getDebugLoc());
1963   }
1964   auto *UI = new UnreachableInst(I->getContext(), I);
1965   UI->setDebugLoc(I->getDebugLoc());
1966 
1967   // All instructions after this are dead.
1968   unsigned NumInstrsRemoved = 0;
1969   BasicBlock::iterator BBI = I->getIterator(), BBE = BB->end();
1970   while (BBI != BBE) {
1971     if (!BBI->use_empty())
1972       BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
1973     BB->getInstList().erase(BBI++);
1974     ++NumInstrsRemoved;
1975   }
1976   if (DTU)
1977     DTU->applyUpdatesPermissive(Updates);
1978   return NumInstrsRemoved;
1979 }
1980 
1981 CallInst *llvm::createCallMatchingInvoke(InvokeInst *II) {
1982   SmallVector<Value *, 8> Args(II->arg_begin(), II->arg_end());
1983   SmallVector<OperandBundleDef, 1> OpBundles;
1984   II->getOperandBundlesAsDefs(OpBundles);
1985   CallInst *NewCall = CallInst::Create(II->getFunctionType(),
1986                                        II->getCalledOperand(), Args, OpBundles);
1987   NewCall->setCallingConv(II->getCallingConv());
1988   NewCall->setAttributes(II->getAttributes());
1989   NewCall->setDebugLoc(II->getDebugLoc());
1990   NewCall->copyMetadata(*II);
1991   return NewCall;
1992 }
1993 
1994 /// changeToCall - Convert the specified invoke into a normal call.
1995 void llvm::changeToCall(InvokeInst *II, DomTreeUpdater *DTU) {
1996   CallInst *NewCall = createCallMatchingInvoke(II);
1997   NewCall->takeName(II);
1998   NewCall->insertBefore(II);
1999   II->replaceAllUsesWith(NewCall);
2000 
2001   // Follow the call by a branch to the normal destination.
2002   BasicBlock *NormalDestBB = II->getNormalDest();
2003   BranchInst::Create(NormalDestBB, II);
2004 
2005   // Update PHI nodes in the unwind destination
2006   BasicBlock *BB = II->getParent();
2007   BasicBlock *UnwindDestBB = II->getUnwindDest();
2008   UnwindDestBB->removePredecessor(BB);
2009   II->eraseFromParent();
2010   if (DTU)
2011     DTU->applyUpdatesPermissive({{DominatorTree::Delete, BB, UnwindDestBB}});
2012 }
2013 
2014 BasicBlock *llvm::changeToInvokeAndSplitBasicBlock(CallInst *CI,
2015                                                    BasicBlock *UnwindEdge) {
2016   BasicBlock *BB = CI->getParent();
2017 
2018   // Convert this function call into an invoke instruction.  First, split the
2019   // basic block.
2020   BasicBlock *Split =
2021       BB->splitBasicBlock(CI->getIterator(), CI->getName() + ".noexc");
2022 
2023   // Delete the unconditional branch inserted by splitBasicBlock
2024   BB->getInstList().pop_back();
2025 
2026   // Create the new invoke instruction.
2027   SmallVector<Value *, 8> InvokeArgs(CI->arg_begin(), CI->arg_end());
2028   SmallVector<OperandBundleDef, 1> OpBundles;
2029 
2030   CI->getOperandBundlesAsDefs(OpBundles);
2031 
2032   // Note: we're round tripping operand bundles through memory here, and that
2033   // can potentially be avoided with a cleverer API design that we do not have
2034   // as of this time.
2035 
2036   InvokeInst *II =
2037       InvokeInst::Create(CI->getFunctionType(), CI->getCalledOperand(), Split,
2038                          UnwindEdge, InvokeArgs, OpBundles, CI->getName(), BB);
2039   II->setDebugLoc(CI->getDebugLoc());
2040   II->setCallingConv(CI->getCallingConv());
2041   II->setAttributes(CI->getAttributes());
2042 
2043   // Make sure that anything using the call now uses the invoke!  This also
2044   // updates the CallGraph if present, because it uses a WeakTrackingVH.
2045   CI->replaceAllUsesWith(II);
2046 
2047   // Delete the original call
2048   Split->getInstList().pop_front();
2049   return Split;
2050 }
2051 
2052 static bool markAliveBlocks(Function &F,
2053                             SmallPtrSetImpl<BasicBlock *> &Reachable,
2054                             DomTreeUpdater *DTU = nullptr) {
2055   SmallVector<BasicBlock*, 128> Worklist;
2056   BasicBlock *BB = &F.front();
2057   Worklist.push_back(BB);
2058   Reachable.insert(BB);
2059   bool Changed = false;
2060   do {
2061     BB = Worklist.pop_back_val();
2062 
2063     // Do a quick scan of the basic block, turning any obviously unreachable
2064     // instructions into LLVM unreachable insts.  The instruction combining pass
2065     // canonicalizes unreachable insts into stores to null or undef.
2066     for (Instruction &I : *BB) {
2067       if (auto *CI = dyn_cast<CallInst>(&I)) {
2068         Value *Callee = CI->getCalledOperand();
2069         // Handle intrinsic calls.
2070         if (Function *F = dyn_cast<Function>(Callee)) {
2071           auto IntrinsicID = F->getIntrinsicID();
2072           // Assumptions that are known to be false are equivalent to
2073           // unreachable. Also, if the condition is undefined, then we make the
2074           // choice most beneficial to the optimizer, and choose that to also be
2075           // unreachable.
2076           if (IntrinsicID == Intrinsic::assume) {
2077             if (match(CI->getArgOperand(0), m_CombineOr(m_Zero(), m_Undef()))) {
2078               // Don't insert a call to llvm.trap right before the unreachable.
2079               changeToUnreachable(CI, false, false, DTU);
2080               Changed = true;
2081               break;
2082             }
2083           } else if (IntrinsicID == Intrinsic::experimental_guard) {
2084             // A call to the guard intrinsic bails out of the current
2085             // compilation unit if the predicate passed to it is false. If the
2086             // predicate is a constant false, then we know the guard will bail
2087             // out of the current compile unconditionally, so all code following
2088             // it is dead.
2089             //
2090             // Note: unlike in llvm.assume, it is not "obviously profitable" for
2091             // guards to treat `undef` as `false` since a guard on `undef` can
2092             // still be useful for widening.
2093             if (match(CI->getArgOperand(0), m_Zero()))
2094               if (!isa<UnreachableInst>(CI->getNextNode())) {
2095                 changeToUnreachable(CI->getNextNode(), /*UseLLVMTrap=*/false,
2096                                     false, DTU);
2097                 Changed = true;
2098                 break;
2099               }
2100           }
2101         } else if ((isa<ConstantPointerNull>(Callee) &&
2102                     !NullPointerIsDefined(CI->getFunction())) ||
2103                    isa<UndefValue>(Callee)) {
2104           changeToUnreachable(CI, /*UseLLVMTrap=*/false, false, DTU);
2105           Changed = true;
2106           break;
2107         }
2108         if (CI->doesNotReturn() && !CI->isMustTailCall()) {
2109           // If we found a call to a no-return function, insert an unreachable
2110           // instruction after it.  Make sure there isn't *already* one there
2111           // though.
2112           if (!isa<UnreachableInst>(CI->getNextNode())) {
2113             // Don't insert a call to llvm.trap right before the unreachable.
2114             changeToUnreachable(CI->getNextNode(), false, false, DTU);
2115             Changed = true;
2116           }
2117           break;
2118         }
2119       } else if (auto *SI = dyn_cast<StoreInst>(&I)) {
2120         // Store to undef and store to null are undefined and used to signal
2121         // that they should be changed to unreachable by passes that can't
2122         // modify the CFG.
2123 
2124         // Don't touch volatile stores.
2125         if (SI->isVolatile()) continue;
2126 
2127         Value *Ptr = SI->getOperand(1);
2128 
2129         if (isa<UndefValue>(Ptr) ||
2130             (isa<ConstantPointerNull>(Ptr) &&
2131              !NullPointerIsDefined(SI->getFunction(),
2132                                    SI->getPointerAddressSpace()))) {
2133           changeToUnreachable(SI, true, false, DTU);
2134           Changed = true;
2135           break;
2136         }
2137       }
2138     }
2139 
2140     Instruction *Terminator = BB->getTerminator();
2141     if (auto *II = dyn_cast<InvokeInst>(Terminator)) {
2142       // Turn invokes that call 'nounwind' functions into ordinary calls.
2143       Value *Callee = II->getCalledOperand();
2144       if ((isa<ConstantPointerNull>(Callee) &&
2145            !NullPointerIsDefined(BB->getParent())) ||
2146           isa<UndefValue>(Callee)) {
2147         changeToUnreachable(II, true, false, DTU);
2148         Changed = true;
2149       } else if (II->doesNotThrow() && canSimplifyInvokeNoUnwind(&F)) {
2150         if (II->use_empty() && II->onlyReadsMemory()) {
2151           // jump to the normal destination branch.
2152           BasicBlock *NormalDestBB = II->getNormalDest();
2153           BasicBlock *UnwindDestBB = II->getUnwindDest();
2154           BranchInst::Create(NormalDestBB, II);
2155           UnwindDestBB->removePredecessor(II->getParent());
2156           II->eraseFromParent();
2157           if (DTU)
2158             DTU->applyUpdatesPermissive(
2159                 {{DominatorTree::Delete, BB, UnwindDestBB}});
2160         } else
2161           changeToCall(II, DTU);
2162         Changed = true;
2163       }
2164     } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Terminator)) {
2165       // Remove catchpads which cannot be reached.
2166       struct CatchPadDenseMapInfo {
2167         static CatchPadInst *getEmptyKey() {
2168           return DenseMapInfo<CatchPadInst *>::getEmptyKey();
2169         }
2170 
2171         static CatchPadInst *getTombstoneKey() {
2172           return DenseMapInfo<CatchPadInst *>::getTombstoneKey();
2173         }
2174 
2175         static unsigned getHashValue(CatchPadInst *CatchPad) {
2176           return static_cast<unsigned>(hash_combine_range(
2177               CatchPad->value_op_begin(), CatchPad->value_op_end()));
2178         }
2179 
2180         static bool isEqual(CatchPadInst *LHS, CatchPadInst *RHS) {
2181           if (LHS == getEmptyKey() || LHS == getTombstoneKey() ||
2182               RHS == getEmptyKey() || RHS == getTombstoneKey())
2183             return LHS == RHS;
2184           return LHS->isIdenticalTo(RHS);
2185         }
2186       };
2187 
2188       // Set of unique CatchPads.
2189       SmallDenseMap<CatchPadInst *, detail::DenseSetEmpty, 4,
2190                     CatchPadDenseMapInfo, detail::DenseSetPair<CatchPadInst *>>
2191           HandlerSet;
2192       detail::DenseSetEmpty Empty;
2193       for (CatchSwitchInst::handler_iterator I = CatchSwitch->handler_begin(),
2194                                              E = CatchSwitch->handler_end();
2195            I != E; ++I) {
2196         BasicBlock *HandlerBB = *I;
2197         auto *CatchPad = cast<CatchPadInst>(HandlerBB->getFirstNonPHI());
2198         if (!HandlerSet.insert({CatchPad, Empty}).second) {
2199           CatchSwitch->removeHandler(I);
2200           --I;
2201           --E;
2202           Changed = true;
2203         }
2204       }
2205     }
2206 
2207     Changed |= ConstantFoldTerminator(BB, true, nullptr, DTU);
2208     for (BasicBlock *Successor : successors(BB))
2209       if (Reachable.insert(Successor).second)
2210         Worklist.push_back(Successor);
2211   } while (!Worklist.empty());
2212   return Changed;
2213 }
2214 
2215 void llvm::removeUnwindEdge(BasicBlock *BB, DomTreeUpdater *DTU) {
2216   Instruction *TI = BB->getTerminator();
2217 
2218   if (auto *II = dyn_cast<InvokeInst>(TI)) {
2219     changeToCall(II, DTU);
2220     return;
2221   }
2222 
2223   Instruction *NewTI;
2224   BasicBlock *UnwindDest;
2225 
2226   if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) {
2227     NewTI = CleanupReturnInst::Create(CRI->getCleanupPad(), nullptr, CRI);
2228     UnwindDest = CRI->getUnwindDest();
2229   } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(TI)) {
2230     auto *NewCatchSwitch = CatchSwitchInst::Create(
2231         CatchSwitch->getParentPad(), nullptr, CatchSwitch->getNumHandlers(),
2232         CatchSwitch->getName(), CatchSwitch);
2233     for (BasicBlock *PadBB : CatchSwitch->handlers())
2234       NewCatchSwitch->addHandler(PadBB);
2235 
2236     NewTI = NewCatchSwitch;
2237     UnwindDest = CatchSwitch->getUnwindDest();
2238   } else {
2239     llvm_unreachable("Could not find unwind successor");
2240   }
2241 
2242   NewTI->takeName(TI);
2243   NewTI->setDebugLoc(TI->getDebugLoc());
2244   UnwindDest->removePredecessor(BB);
2245   TI->replaceAllUsesWith(NewTI);
2246   TI->eraseFromParent();
2247   if (DTU)
2248     DTU->applyUpdatesPermissive({{DominatorTree::Delete, BB, UnwindDest}});
2249 }
2250 
2251 /// removeUnreachableBlocks - Remove blocks that are not reachable, even
2252 /// if they are in a dead cycle.  Return true if a change was made, false
2253 /// otherwise.
2254 bool llvm::removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU,
2255                                    MemorySSAUpdater *MSSAU) {
2256   SmallPtrSet<BasicBlock *, 16> Reachable;
2257   bool Changed = markAliveBlocks(F, Reachable, DTU);
2258 
2259   // If there are unreachable blocks in the CFG...
2260   if (Reachable.size() == F.size())
2261     return Changed;
2262 
2263   assert(Reachable.size() < F.size());
2264   NumRemoved += F.size() - Reachable.size();
2265 
2266   SmallSetVector<BasicBlock *, 8> DeadBlockSet;
2267   for (BasicBlock &BB : F) {
2268     // Skip reachable basic blocks
2269     if (Reachable.count(&BB))
2270       continue;
2271     DeadBlockSet.insert(&BB);
2272   }
2273 
2274   if (MSSAU)
2275     MSSAU->removeBlocks(DeadBlockSet);
2276 
2277   // Loop over all of the basic blocks that are not reachable, dropping all of
2278   // their internal references. Update DTU if available.
2279   std::vector<DominatorTree::UpdateType> Updates;
2280   for (auto *BB : DeadBlockSet) {
2281     for (BasicBlock *Successor : successors(BB)) {
2282       if (!DeadBlockSet.count(Successor))
2283         Successor->removePredecessor(BB);
2284       if (DTU)
2285         Updates.push_back({DominatorTree::Delete, BB, Successor});
2286     }
2287     BB->dropAllReferences();
2288     if (DTU) {
2289       Instruction *TI = BB->getTerminator();
2290       assert(TI && "Basic block should have a terminator");
2291       // Terminators like invoke can have users. We have to replace their users,
2292       // before removing them.
2293       if (!TI->use_empty())
2294         TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
2295       TI->eraseFromParent();
2296       new UnreachableInst(BB->getContext(), BB);
2297       assert(succ_empty(BB) && "The successor list of BB isn't empty before "
2298                                "applying corresponding DTU updates.");
2299     }
2300   }
2301 
2302   if (DTU) {
2303     DTU->applyUpdatesPermissive(Updates);
2304     bool Deleted = false;
2305     for (auto *BB : DeadBlockSet) {
2306       if (DTU->isBBPendingDeletion(BB))
2307         --NumRemoved;
2308       else
2309         Deleted = true;
2310       DTU->deleteBB(BB);
2311     }
2312     if (!Deleted)
2313       return false;
2314   } else {
2315     for (auto *BB : DeadBlockSet)
2316       BB->eraseFromParent();
2317   }
2318 
2319   return true;
2320 }
2321 
2322 void llvm::combineMetadata(Instruction *K, const Instruction *J,
2323                            ArrayRef<unsigned> KnownIDs, bool DoesKMove) {
2324   SmallVector<std::pair<unsigned, MDNode *>, 4> Metadata;
2325   K->dropUnknownNonDebugMetadata(KnownIDs);
2326   K->getAllMetadataOtherThanDebugLoc(Metadata);
2327   for (const auto &MD : Metadata) {
2328     unsigned Kind = MD.first;
2329     MDNode *JMD = J->getMetadata(Kind);
2330     MDNode *KMD = MD.second;
2331 
2332     switch (Kind) {
2333       default:
2334         K->setMetadata(Kind, nullptr); // Remove unknown metadata
2335         break;
2336       case LLVMContext::MD_dbg:
2337         llvm_unreachable("getAllMetadataOtherThanDebugLoc returned a MD_dbg");
2338       case LLVMContext::MD_tbaa:
2339         K->setMetadata(Kind, MDNode::getMostGenericTBAA(JMD, KMD));
2340         break;
2341       case LLVMContext::MD_alias_scope:
2342         K->setMetadata(Kind, MDNode::getMostGenericAliasScope(JMD, KMD));
2343         break;
2344       case LLVMContext::MD_noalias:
2345       case LLVMContext::MD_mem_parallel_loop_access:
2346         K->setMetadata(Kind, MDNode::intersect(JMD, KMD));
2347         break;
2348       case LLVMContext::MD_access_group:
2349         K->setMetadata(LLVMContext::MD_access_group,
2350                        intersectAccessGroups(K, J));
2351         break;
2352       case LLVMContext::MD_range:
2353 
2354         // If K does move, use most generic range. Otherwise keep the range of
2355         // K.
2356         if (DoesKMove)
2357           // FIXME: If K does move, we should drop the range info and nonnull.
2358           //        Currently this function is used with DoesKMove in passes
2359           //        doing hoisting/sinking and the current behavior of using the
2360           //        most generic range is correct in those cases.
2361           K->setMetadata(Kind, MDNode::getMostGenericRange(JMD, KMD));
2362         break;
2363       case LLVMContext::MD_fpmath:
2364         K->setMetadata(Kind, MDNode::getMostGenericFPMath(JMD, KMD));
2365         break;
2366       case LLVMContext::MD_invariant_load:
2367         // Only set the !invariant.load if it is present in both instructions.
2368         K->setMetadata(Kind, JMD);
2369         break;
2370       case LLVMContext::MD_nonnull:
2371         // If K does move, keep nonull if it is present in both instructions.
2372         if (DoesKMove)
2373           K->setMetadata(Kind, JMD);
2374         break;
2375       case LLVMContext::MD_invariant_group:
2376         // Preserve !invariant.group in K.
2377         break;
2378       case LLVMContext::MD_align:
2379         K->setMetadata(Kind,
2380           MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD));
2381         break;
2382       case LLVMContext::MD_dereferenceable:
2383       case LLVMContext::MD_dereferenceable_or_null:
2384         K->setMetadata(Kind,
2385           MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD));
2386         break;
2387       case LLVMContext::MD_preserve_access_index:
2388         // Preserve !preserve.access.index in K.
2389         break;
2390     }
2391   }
2392   // Set !invariant.group from J if J has it. If both instructions have it
2393   // then we will just pick it from J - even when they are different.
2394   // Also make sure that K is load or store - f.e. combining bitcast with load
2395   // could produce bitcast with invariant.group metadata, which is invalid.
2396   // FIXME: we should try to preserve both invariant.group md if they are
2397   // different, but right now instruction can only have one invariant.group.
2398   if (auto *JMD = J->getMetadata(LLVMContext::MD_invariant_group))
2399     if (isa<LoadInst>(K) || isa<StoreInst>(K))
2400       K->setMetadata(LLVMContext::MD_invariant_group, JMD);
2401 }
2402 
2403 void llvm::combineMetadataForCSE(Instruction *K, const Instruction *J,
2404                                  bool KDominatesJ) {
2405   unsigned KnownIDs[] = {
2406       LLVMContext::MD_tbaa,            LLVMContext::MD_alias_scope,
2407       LLVMContext::MD_noalias,         LLVMContext::MD_range,
2408       LLVMContext::MD_invariant_load,  LLVMContext::MD_nonnull,
2409       LLVMContext::MD_invariant_group, LLVMContext::MD_align,
2410       LLVMContext::MD_dereferenceable,
2411       LLVMContext::MD_dereferenceable_or_null,
2412       LLVMContext::MD_access_group,    LLVMContext::MD_preserve_access_index};
2413   combineMetadata(K, J, KnownIDs, KDominatesJ);
2414 }
2415 
2416 void llvm::copyMetadataForLoad(LoadInst &Dest, const LoadInst &Source) {
2417   SmallVector<std::pair<unsigned, MDNode *>, 8> MD;
2418   Source.getAllMetadata(MD);
2419   MDBuilder MDB(Dest.getContext());
2420   Type *NewType = Dest.getType();
2421   const DataLayout &DL = Source.getModule()->getDataLayout();
2422   for (const auto &MDPair : MD) {
2423     unsigned ID = MDPair.first;
2424     MDNode *N = MDPair.second;
2425     // Note, essentially every kind of metadata should be preserved here! This
2426     // routine is supposed to clone a load instruction changing *only its type*.
2427     // The only metadata it makes sense to drop is metadata which is invalidated
2428     // when the pointer type changes. This should essentially never be the case
2429     // in LLVM, but we explicitly switch over only known metadata to be
2430     // conservatively correct. If you are adding metadata to LLVM which pertains
2431     // to loads, you almost certainly want to add it here.
2432     switch (ID) {
2433     case LLVMContext::MD_dbg:
2434     case LLVMContext::MD_tbaa:
2435     case LLVMContext::MD_prof:
2436     case LLVMContext::MD_fpmath:
2437     case LLVMContext::MD_tbaa_struct:
2438     case LLVMContext::MD_invariant_load:
2439     case LLVMContext::MD_alias_scope:
2440     case LLVMContext::MD_noalias:
2441     case LLVMContext::MD_nontemporal:
2442     case LLVMContext::MD_mem_parallel_loop_access:
2443     case LLVMContext::MD_access_group:
2444       // All of these directly apply.
2445       Dest.setMetadata(ID, N);
2446       break;
2447 
2448     case LLVMContext::MD_nonnull:
2449       copyNonnullMetadata(Source, N, Dest);
2450       break;
2451 
2452     case LLVMContext::MD_align:
2453     case LLVMContext::MD_dereferenceable:
2454     case LLVMContext::MD_dereferenceable_or_null:
2455       // These only directly apply if the new type is also a pointer.
2456       if (NewType->isPointerTy())
2457         Dest.setMetadata(ID, N);
2458       break;
2459 
2460     case LLVMContext::MD_range:
2461       copyRangeMetadata(DL, Source, N, Dest);
2462       break;
2463     }
2464   }
2465 }
2466 
2467 void llvm::patchReplacementInstruction(Instruction *I, Value *Repl) {
2468   auto *ReplInst = dyn_cast<Instruction>(Repl);
2469   if (!ReplInst)
2470     return;
2471 
2472   // Patch the replacement so that it is not more restrictive than the value
2473   // being replaced.
2474   // Note that if 'I' is a load being replaced by some operation,
2475   // for example, by an arithmetic operation, then andIRFlags()
2476   // would just erase all math flags from the original arithmetic
2477   // operation, which is clearly not wanted and not needed.
2478   if (!isa<LoadInst>(I))
2479     ReplInst->andIRFlags(I);
2480 
2481   // FIXME: If both the original and replacement value are part of the
2482   // same control-flow region (meaning that the execution of one
2483   // guarantees the execution of the other), then we can combine the
2484   // noalias scopes here and do better than the general conservative
2485   // answer used in combineMetadata().
2486 
2487   // In general, GVN unifies expressions over different control-flow
2488   // regions, and so we need a conservative combination of the noalias
2489   // scopes.
2490   static const unsigned KnownIDs[] = {
2491       LLVMContext::MD_tbaa,            LLVMContext::MD_alias_scope,
2492       LLVMContext::MD_noalias,         LLVMContext::MD_range,
2493       LLVMContext::MD_fpmath,          LLVMContext::MD_invariant_load,
2494       LLVMContext::MD_invariant_group, LLVMContext::MD_nonnull,
2495       LLVMContext::MD_access_group,    LLVMContext::MD_preserve_access_index};
2496   combineMetadata(ReplInst, I, KnownIDs, false);
2497 }
2498 
2499 template <typename RootType, typename DominatesFn>
2500 static unsigned replaceDominatedUsesWith(Value *From, Value *To,
2501                                          const RootType &Root,
2502                                          const DominatesFn &Dominates) {
2503   assert(From->getType() == To->getType());
2504 
2505   unsigned Count = 0;
2506   for (Value::use_iterator UI = From->use_begin(), UE = From->use_end();
2507        UI != UE;) {
2508     Use &U = *UI++;
2509     if (!Dominates(Root, U))
2510       continue;
2511     U.set(To);
2512     LLVM_DEBUG(dbgs() << "Replace dominated use of '" << From->getName()
2513                       << "' as " << *To << " in " << *U << "\n");
2514     ++Count;
2515   }
2516   return Count;
2517 }
2518 
2519 unsigned llvm::replaceNonLocalUsesWith(Instruction *From, Value *To) {
2520    assert(From->getType() == To->getType());
2521    auto *BB = From->getParent();
2522    unsigned Count = 0;
2523 
2524   for (Value::use_iterator UI = From->use_begin(), UE = From->use_end();
2525        UI != UE;) {
2526     Use &U = *UI++;
2527     auto *I = cast<Instruction>(U.getUser());
2528     if (I->getParent() == BB)
2529       continue;
2530     U.set(To);
2531     ++Count;
2532   }
2533   return Count;
2534 }
2535 
2536 unsigned llvm::replaceDominatedUsesWith(Value *From, Value *To,
2537                                         DominatorTree &DT,
2538                                         const BasicBlockEdge &Root) {
2539   auto Dominates = [&DT](const BasicBlockEdge &Root, const Use &U) {
2540     return DT.dominates(Root, U);
2541   };
2542   return ::replaceDominatedUsesWith(From, To, Root, Dominates);
2543 }
2544 
2545 unsigned llvm::replaceDominatedUsesWith(Value *From, Value *To,
2546                                         DominatorTree &DT,
2547                                         const BasicBlock *BB) {
2548   auto ProperlyDominates = [&DT](const BasicBlock *BB, const Use &U) {
2549     auto *I = cast<Instruction>(U.getUser())->getParent();
2550     return DT.properlyDominates(BB, I);
2551   };
2552   return ::replaceDominatedUsesWith(From, To, BB, ProperlyDominates);
2553 }
2554 
2555 bool llvm::callsGCLeafFunction(const CallBase *Call,
2556                                const TargetLibraryInfo &TLI) {
2557   // Check if the function is specifically marked as a gc leaf function.
2558   if (Call->hasFnAttr("gc-leaf-function"))
2559     return true;
2560   if (const Function *F = Call->getCalledFunction()) {
2561     if (F->hasFnAttribute("gc-leaf-function"))
2562       return true;
2563 
2564     if (auto IID = F->getIntrinsicID())
2565       // Most LLVM intrinsics do not take safepoints.
2566       return IID != Intrinsic::experimental_gc_statepoint &&
2567              IID != Intrinsic::experimental_deoptimize;
2568   }
2569 
2570   // Lib calls can be materialized by some passes, and won't be
2571   // marked as 'gc-leaf-function.' All available Libcalls are
2572   // GC-leaf.
2573   LibFunc LF;
2574   if (TLI.getLibFunc(*Call, LF)) {
2575     return TLI.has(LF);
2576   }
2577 
2578   return false;
2579 }
2580 
2581 void llvm::copyNonnullMetadata(const LoadInst &OldLI, MDNode *N,
2582                                LoadInst &NewLI) {
2583   auto *NewTy = NewLI.getType();
2584 
2585   // This only directly applies if the new type is also a pointer.
2586   if (NewTy->isPointerTy()) {
2587     NewLI.setMetadata(LLVMContext::MD_nonnull, N);
2588     return;
2589   }
2590 
2591   // The only other translation we can do is to integral loads with !range
2592   // metadata.
2593   if (!NewTy->isIntegerTy())
2594     return;
2595 
2596   MDBuilder MDB(NewLI.getContext());
2597   const Value *Ptr = OldLI.getPointerOperand();
2598   auto *ITy = cast<IntegerType>(NewTy);
2599   auto *NullInt = ConstantExpr::getPtrToInt(
2600       ConstantPointerNull::get(cast<PointerType>(Ptr->getType())), ITy);
2601   auto *NonNullInt = ConstantExpr::getAdd(NullInt, ConstantInt::get(ITy, 1));
2602   NewLI.setMetadata(LLVMContext::MD_range,
2603                     MDB.createRange(NonNullInt, NullInt));
2604 }
2605 
2606 void llvm::copyRangeMetadata(const DataLayout &DL, const LoadInst &OldLI,
2607                              MDNode *N, LoadInst &NewLI) {
2608   auto *NewTy = NewLI.getType();
2609 
2610   // Give up unless it is converted to a pointer where there is a single very
2611   // valuable mapping we can do reliably.
2612   // FIXME: It would be nice to propagate this in more ways, but the type
2613   // conversions make it hard.
2614   if (!NewTy->isPointerTy())
2615     return;
2616 
2617   unsigned BitWidth = DL.getPointerTypeSizeInBits(NewTy);
2618   if (!getConstantRangeFromMetadata(*N).contains(APInt(BitWidth, 0))) {
2619     MDNode *NN = MDNode::get(OldLI.getContext(), None);
2620     NewLI.setMetadata(LLVMContext::MD_nonnull, NN);
2621   }
2622 }
2623 
2624 void llvm::dropDebugUsers(Instruction &I) {
2625   SmallVector<DbgVariableIntrinsic *, 1> DbgUsers;
2626   findDbgUsers(DbgUsers, &I);
2627   for (auto *DII : DbgUsers)
2628     DII->eraseFromParent();
2629 }
2630 
2631 void llvm::hoistAllInstructionsInto(BasicBlock *DomBlock, Instruction *InsertPt,
2632                                     BasicBlock *BB) {
2633   // Since we are moving the instructions out of its basic block, we do not
2634   // retain their original debug locations (DILocations) and debug intrinsic
2635   // instructions.
2636   //
2637   // Doing so would degrade the debugging experience and adversely affect the
2638   // accuracy of profiling information.
2639   //
2640   // Currently, when hoisting the instructions, we take the following actions:
2641   // - Remove their debug intrinsic instructions.
2642   // - Set their debug locations to the values from the insertion point.
2643   //
2644   // As per PR39141 (comment #8), the more fundamental reason why the dbg.values
2645   // need to be deleted, is because there will not be any instructions with a
2646   // DILocation in either branch left after performing the transformation. We
2647   // can only insert a dbg.value after the two branches are joined again.
2648   //
2649   // See PR38762, PR39243 for more details.
2650   //
2651   // TODO: Extend llvm.dbg.value to take more than one SSA Value (PR39141) to
2652   // encode predicated DIExpressions that yield different results on different
2653   // code paths.
2654   for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;) {
2655     Instruction *I = &*II;
2656     I->dropUnknownNonDebugMetadata();
2657     if (I->isUsedByMetadata())
2658       dropDebugUsers(*I);
2659     if (isa<DbgInfoIntrinsic>(I)) {
2660       // Remove DbgInfo Intrinsics.
2661       II = I->eraseFromParent();
2662       continue;
2663     }
2664     I->setDebugLoc(InsertPt->getDebugLoc());
2665     ++II;
2666   }
2667   DomBlock->getInstList().splice(InsertPt->getIterator(), BB->getInstList(),
2668                                  BB->begin(),
2669                                  BB->getTerminator()->getIterator());
2670 }
2671 
2672 namespace {
2673 
2674 /// A potential constituent of a bitreverse or bswap expression. See
2675 /// collectBitParts for a fuller explanation.
2676 struct BitPart {
2677   BitPart(Value *P, unsigned BW) : Provider(P) {
2678     Provenance.resize(BW);
2679   }
2680 
2681   /// The Value that this is a bitreverse/bswap of.
2682   Value *Provider;
2683 
2684   /// The "provenance" of each bit. Provenance[A] = B means that bit A
2685   /// in Provider becomes bit B in the result of this expression.
2686   SmallVector<int8_t, 32> Provenance; // int8_t means max size is i128.
2687 
2688   enum { Unset = -1 };
2689 };
2690 
2691 } // end anonymous namespace
2692 
2693 /// Analyze the specified subexpression and see if it is capable of providing
2694 /// pieces of a bswap or bitreverse. The subexpression provides a potential
2695 /// piece of a bswap or bitreverse if it can be proven that each non-zero bit in
2696 /// the output of the expression came from a corresponding bit in some other
2697 /// value. This function is recursive, and the end result is a mapping of
2698 /// bitnumber to bitnumber. It is the caller's responsibility to validate that
2699 /// the bitnumber to bitnumber mapping is correct for a bswap or bitreverse.
2700 ///
2701 /// For example, if the current subexpression if "(shl i32 %X, 24)" then we know
2702 /// that the expression deposits the low byte of %X into the high byte of the
2703 /// result and that all other bits are zero. This expression is accepted and a
2704 /// BitPart is returned with Provider set to %X and Provenance[24-31] set to
2705 /// [0-7].
2706 ///
2707 /// To avoid revisiting values, the BitPart results are memoized into the
2708 /// provided map. To avoid unnecessary copying of BitParts, BitParts are
2709 /// constructed in-place in the \c BPS map. Because of this \c BPS needs to
2710 /// store BitParts objects, not pointers. As we need the concept of a nullptr
2711 /// BitParts (Value has been analyzed and the analysis failed), we an Optional
2712 /// type instead to provide the same functionality.
2713 ///
2714 /// Because we pass around references into \c BPS, we must use a container that
2715 /// does not invalidate internal references (std::map instead of DenseMap).
2716 static const Optional<BitPart> &
2717 collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals,
2718                 std::map<Value *, Optional<BitPart>> &BPS, int Depth) {
2719   auto I = BPS.find(V);
2720   if (I != BPS.end())
2721     return I->second;
2722 
2723   auto &Result = BPS[V] = None;
2724   auto BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
2725 
2726   // Prevent stack overflow by limiting the recursion depth
2727   if (Depth == BitPartRecursionMaxDepth) {
2728     LLVM_DEBUG(dbgs() << "collectBitParts max recursion depth reached.\n");
2729     return Result;
2730   }
2731 
2732   if (Instruction *I = dyn_cast<Instruction>(V)) {
2733     // If this is an or instruction, it may be an inner node of the bswap.
2734     if (I->getOpcode() == Instruction::Or) {
2735       auto &A = collectBitParts(I->getOperand(0), MatchBSwaps,
2736                                 MatchBitReversals, BPS, Depth + 1);
2737       auto &B = collectBitParts(I->getOperand(1), MatchBSwaps,
2738                                 MatchBitReversals, BPS, Depth + 1);
2739       if (!A || !B)
2740         return Result;
2741 
2742       // Try and merge the two together.
2743       if (!A->Provider || A->Provider != B->Provider)
2744         return Result;
2745 
2746       Result = BitPart(A->Provider, BitWidth);
2747       for (unsigned i = 0; i < A->Provenance.size(); ++i) {
2748         if (A->Provenance[i] != BitPart::Unset &&
2749             B->Provenance[i] != BitPart::Unset &&
2750             A->Provenance[i] != B->Provenance[i])
2751           return Result = None;
2752 
2753         if (A->Provenance[i] == BitPart::Unset)
2754           Result->Provenance[i] = B->Provenance[i];
2755         else
2756           Result->Provenance[i] = A->Provenance[i];
2757       }
2758 
2759       return Result;
2760     }
2761 
2762     // If this is a logical shift by a constant, recurse then shift the result.
2763     if (I->isLogicalShift() && isa<ConstantInt>(I->getOperand(1))) {
2764       unsigned BitShift =
2765           cast<ConstantInt>(I->getOperand(1))->getLimitedValue(~0U);
2766       // Ensure the shift amount is defined.
2767       if (BitShift > BitWidth)
2768         return Result;
2769 
2770       auto &Res = collectBitParts(I->getOperand(0), MatchBSwaps,
2771                                   MatchBitReversals, BPS, Depth + 1);
2772       if (!Res)
2773         return Result;
2774       Result = Res;
2775 
2776       // Perform the "shift" on BitProvenance.
2777       auto &P = Result->Provenance;
2778       if (I->getOpcode() == Instruction::Shl) {
2779         P.erase(std::prev(P.end(), BitShift), P.end());
2780         P.insert(P.begin(), BitShift, BitPart::Unset);
2781       } else {
2782         P.erase(P.begin(), std::next(P.begin(), BitShift));
2783         P.insert(P.end(), BitShift, BitPart::Unset);
2784       }
2785 
2786       return Result;
2787     }
2788 
2789     // If this is a logical 'and' with a mask that clears bits, recurse then
2790     // unset the appropriate bits.
2791     if (I->getOpcode() == Instruction::And &&
2792         isa<ConstantInt>(I->getOperand(1))) {
2793       APInt Bit(I->getType()->getPrimitiveSizeInBits(), 1);
2794       const APInt &AndMask = cast<ConstantInt>(I->getOperand(1))->getValue();
2795 
2796       // Check that the mask allows a multiple of 8 bits for a bswap, for an
2797       // early exit.
2798       unsigned NumMaskedBits = AndMask.countPopulation();
2799       if (!MatchBitReversals && NumMaskedBits % 8 != 0)
2800         return Result;
2801 
2802       auto &Res = collectBitParts(I->getOperand(0), MatchBSwaps,
2803                                   MatchBitReversals, BPS, Depth + 1);
2804       if (!Res)
2805         return Result;
2806       Result = Res;
2807 
2808       for (unsigned i = 0; i < BitWidth; ++i, Bit <<= 1)
2809         // If the AndMask is zero for this bit, clear the bit.
2810         if ((AndMask & Bit) == 0)
2811           Result->Provenance[i] = BitPart::Unset;
2812       return Result;
2813     }
2814 
2815     // If this is a zext instruction zero extend the result.
2816     if (I->getOpcode() == Instruction::ZExt) {
2817       auto &Res = collectBitParts(I->getOperand(0), MatchBSwaps,
2818                                   MatchBitReversals, BPS, Depth + 1);
2819       if (!Res)
2820         return Result;
2821 
2822       Result = BitPart(Res->Provider, BitWidth);
2823       auto NarrowBitWidth =
2824           cast<IntegerType>(cast<ZExtInst>(I)->getSrcTy())->getBitWidth();
2825       for (unsigned i = 0; i < NarrowBitWidth; ++i)
2826         Result->Provenance[i] = Res->Provenance[i];
2827       for (unsigned i = NarrowBitWidth; i < BitWidth; ++i)
2828         Result->Provenance[i] = BitPart::Unset;
2829       return Result;
2830     }
2831   }
2832 
2833   // Okay, we got to something that isn't a shift, 'or' or 'and'.  This must be
2834   // the input value to the bswap/bitreverse.
2835   Result = BitPart(V, BitWidth);
2836   for (unsigned i = 0; i < BitWidth; ++i)
2837     Result->Provenance[i] = i;
2838   return Result;
2839 }
2840 
2841 static bool bitTransformIsCorrectForBSwap(unsigned From, unsigned To,
2842                                           unsigned BitWidth) {
2843   if (From % 8 != To % 8)
2844     return false;
2845   // Convert from bit indices to byte indices and check for a byte reversal.
2846   From >>= 3;
2847   To >>= 3;
2848   BitWidth >>= 3;
2849   return From == BitWidth - To - 1;
2850 }
2851 
2852 static bool bitTransformIsCorrectForBitReverse(unsigned From, unsigned To,
2853                                                unsigned BitWidth) {
2854   return From == BitWidth - To - 1;
2855 }
2856 
2857 bool llvm::recognizeBSwapOrBitReverseIdiom(
2858     Instruction *I, bool MatchBSwaps, bool MatchBitReversals,
2859     SmallVectorImpl<Instruction *> &InsertedInsts) {
2860   if (Operator::getOpcode(I) != Instruction::Or)
2861     return false;
2862   if (!MatchBSwaps && !MatchBitReversals)
2863     return false;
2864   IntegerType *ITy = dyn_cast<IntegerType>(I->getType());
2865   if (!ITy || ITy->getBitWidth() > 128)
2866     return false;   // Can't do vectors or integers > 128 bits.
2867   unsigned BW = ITy->getBitWidth();
2868 
2869   unsigned DemandedBW = BW;
2870   IntegerType *DemandedTy = ITy;
2871   if (I->hasOneUse()) {
2872     if (TruncInst *Trunc = dyn_cast<TruncInst>(I->user_back())) {
2873       DemandedTy = cast<IntegerType>(Trunc->getType());
2874       DemandedBW = DemandedTy->getBitWidth();
2875     }
2876   }
2877 
2878   // Try to find all the pieces corresponding to the bswap.
2879   std::map<Value *, Optional<BitPart>> BPS;
2880   auto Res = collectBitParts(I, MatchBSwaps, MatchBitReversals, BPS, 0);
2881   if (!Res)
2882     return false;
2883   auto &BitProvenance = Res->Provenance;
2884 
2885   // Now, is the bit permutation correct for a bswap or a bitreverse? We can
2886   // only byteswap values with an even number of bytes.
2887   bool OKForBSwap = DemandedBW % 16 == 0, OKForBitReverse = true;
2888   for (unsigned i = 0; i < DemandedBW; ++i) {
2889     OKForBSwap &=
2890         bitTransformIsCorrectForBSwap(BitProvenance[i], i, DemandedBW);
2891     OKForBitReverse &=
2892         bitTransformIsCorrectForBitReverse(BitProvenance[i], i, DemandedBW);
2893   }
2894 
2895   Intrinsic::ID Intrin;
2896   if (OKForBSwap && MatchBSwaps)
2897     Intrin = Intrinsic::bswap;
2898   else if (OKForBitReverse && MatchBitReversals)
2899     Intrin = Intrinsic::bitreverse;
2900   else
2901     return false;
2902 
2903   if (ITy != DemandedTy) {
2904     Function *F = Intrinsic::getDeclaration(I->getModule(), Intrin, DemandedTy);
2905     Value *Provider = Res->Provider;
2906     IntegerType *ProviderTy = cast<IntegerType>(Provider->getType());
2907     // We may need to truncate the provider.
2908     if (DemandedTy != ProviderTy) {
2909       auto *Trunc = CastInst::Create(Instruction::Trunc, Provider, DemandedTy,
2910                                      "trunc", I);
2911       InsertedInsts.push_back(Trunc);
2912       Provider = Trunc;
2913     }
2914     auto *CI = CallInst::Create(F, Provider, "rev", I);
2915     InsertedInsts.push_back(CI);
2916     auto *ExtInst = CastInst::Create(Instruction::ZExt, CI, ITy, "zext", I);
2917     InsertedInsts.push_back(ExtInst);
2918     return true;
2919   }
2920 
2921   Function *F = Intrinsic::getDeclaration(I->getModule(), Intrin, ITy);
2922   InsertedInsts.push_back(CallInst::Create(F, Res->Provider, "rev", I));
2923   return true;
2924 }
2925 
2926 // CodeGen has special handling for some string functions that may replace
2927 // them with target-specific intrinsics.  Since that'd skip our interceptors
2928 // in ASan/MSan/TSan/DFSan, and thus make us miss some memory accesses,
2929 // we mark affected calls as NoBuiltin, which will disable optimization
2930 // in CodeGen.
2931 void llvm::maybeMarkSanitizerLibraryCallNoBuiltin(
2932     CallInst *CI, const TargetLibraryInfo *TLI) {
2933   Function *F = CI->getCalledFunction();
2934   LibFunc Func;
2935   if (F && !F->hasLocalLinkage() && F->hasName() &&
2936       TLI->getLibFunc(F->getName(), Func) && TLI->hasOptimizedCodeGen(Func) &&
2937       !F->doesNotAccessMemory())
2938     CI->addAttribute(AttributeList::FunctionIndex, Attribute::NoBuiltin);
2939 }
2940 
2941 bool llvm::canReplaceOperandWithVariable(const Instruction *I, unsigned OpIdx) {
2942   // We can't have a PHI with a metadata type.
2943   if (I->getOperand(OpIdx)->getType()->isMetadataTy())
2944     return false;
2945 
2946   // Early exit.
2947   if (!isa<Constant>(I->getOperand(OpIdx)))
2948     return true;
2949 
2950   switch (I->getOpcode()) {
2951   default:
2952     return true;
2953   case Instruction::Call:
2954   case Instruction::Invoke: {
2955     const auto &CB = cast<CallBase>(*I);
2956 
2957     // Can't handle inline asm. Skip it.
2958     if (CB.isInlineAsm())
2959       return false;
2960 
2961     // Constant bundle operands may need to retain their constant-ness for
2962     // correctness.
2963     if (CB.isBundleOperand(OpIdx))
2964       return false;
2965 
2966     if (OpIdx < CB.getNumArgOperands()) {
2967       // Some variadic intrinsics require constants in the variadic arguments,
2968       // which currently aren't markable as immarg.
2969       if (isa<IntrinsicInst>(CB) &&
2970           OpIdx >= CB.getFunctionType()->getNumParams()) {
2971         // This is known to be OK for stackmap.
2972         return CB.getIntrinsicID() == Intrinsic::experimental_stackmap;
2973       }
2974 
2975       // gcroot is a special case, since it requires a constant argument which
2976       // isn't also required to be a simple ConstantInt.
2977       if (CB.getIntrinsicID() == Intrinsic::gcroot)
2978         return false;
2979 
2980       // Some intrinsic operands are required to be immediates.
2981       return !CB.paramHasAttr(OpIdx, Attribute::ImmArg);
2982     }
2983 
2984     // It is never allowed to replace the call argument to an intrinsic, but it
2985     // may be possible for a call.
2986     return !isa<IntrinsicInst>(CB);
2987   }
2988   case Instruction::ShuffleVector:
2989     // Shufflevector masks are constant.
2990     return OpIdx != 2;
2991   case Instruction::Switch:
2992   case Instruction::ExtractValue:
2993     // All operands apart from the first are constant.
2994     return OpIdx == 0;
2995   case Instruction::InsertValue:
2996     // All operands apart from the first and the second are constant.
2997     return OpIdx < 2;
2998   case Instruction::Alloca:
2999     // Static allocas (constant size in the entry block) are handled by
3000     // prologue/epilogue insertion so they're free anyway. We definitely don't
3001     // want to make them non-constant.
3002     return !cast<AllocaInst>(I)->isStaticAlloca();
3003   case Instruction::GetElementPtr:
3004     if (OpIdx == 0)
3005       return true;
3006     gep_type_iterator It = gep_type_begin(I);
3007     for (auto E = std::next(It, OpIdx); It != E; ++It)
3008       if (It.isStruct())
3009         return false;
3010     return true;
3011   }
3012 }
3013 
3014 using AllocaForValueMapTy = DenseMap<Value *, AllocaInst *>;
3015 AllocaInst *llvm::findAllocaForValue(Value *V,
3016                                      AllocaForValueMapTy &AllocaForValue) {
3017   if (AllocaInst *AI = dyn_cast<AllocaInst>(V))
3018     return AI;
3019   // See if we've already calculated (or started to calculate) alloca for a
3020   // given value.
3021   AllocaForValueMapTy::iterator I = AllocaForValue.find(V);
3022   if (I != AllocaForValue.end())
3023     return I->second;
3024   // Store 0 while we're calculating alloca for value V to avoid
3025   // infinite recursion if the value references itself.
3026   AllocaForValue[V] = nullptr;
3027   AllocaInst *Res = nullptr;
3028   if (CastInst *CI = dyn_cast<CastInst>(V))
3029     Res = findAllocaForValue(CI->getOperand(0), AllocaForValue);
3030   else if (PHINode *PN = dyn_cast<PHINode>(V)) {
3031     for (Value *IncValue : PN->incoming_values()) {
3032       // Allow self-referencing phi-nodes.
3033       if (IncValue == PN)
3034         continue;
3035       AllocaInst *IncValueAI = findAllocaForValue(IncValue, AllocaForValue);
3036       // AI for incoming values should exist and should all be equal.
3037       if (IncValueAI == nullptr || (Res != nullptr && IncValueAI != Res))
3038         return nullptr;
3039       Res = IncValueAI;
3040     }
3041   } else if (GetElementPtrInst *EP = dyn_cast<GetElementPtrInst>(V)) {
3042     Res = findAllocaForValue(EP->getPointerOperand(), AllocaForValue);
3043   } else {
3044     LLVM_DEBUG(dbgs() << "Alloca search cancelled on unknown instruction: "
3045                       << *V << "\n");
3046   }
3047   if (Res)
3048     AllocaForValue[V] = Res;
3049   return Res;
3050 }
3051 
3052 Value *llvm::invertCondition(Value *Condition) {
3053   // First: Check if it's a constant
3054   if (Constant *C = dyn_cast<Constant>(Condition))
3055     return ConstantExpr::getNot(C);
3056 
3057   // Second: If the condition is already inverted, return the original value
3058   Value *NotCondition;
3059   if (match(Condition, m_Not(m_Value(NotCondition))))
3060     return NotCondition;
3061 
3062   BasicBlock *Parent = nullptr;
3063   Instruction *Inst = dyn_cast<Instruction>(Condition);
3064   if (Inst)
3065     Parent = Inst->getParent();
3066   else if (Argument *Arg = dyn_cast<Argument>(Condition))
3067     Parent = &Arg->getParent()->getEntryBlock();
3068   assert(Parent && "Unsupported condition to invert");
3069 
3070   // Third: Check all the users for an invert
3071   for (User *U : Condition->users())
3072     if (Instruction *I = dyn_cast<Instruction>(U))
3073       if (I->getParent() == Parent && match(I, m_Not(m_Specific(Condition))))
3074         return I;
3075 
3076   // Last option: Create a new instruction
3077   auto *Inverted =
3078       BinaryOperator::CreateNot(Condition, Condition->getName() + ".inv");
3079   if (Inst && !isa<PHINode>(Inst))
3080     Inverted->insertAfter(Inst);
3081   else
3082     Inverted->insertBefore(&*Parent->getFirstInsertionPt());
3083   return Inverted;
3084 }
3085