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