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