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