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->hasNonDebugLocLoopMetadata())
1283 for (BasicBlock *Pred : predecessors(BB))
1284 if (Instruction *PredTI = Pred->getTerminator())
1285 if (PredTI->hasNonDebugLocLoopMetadata())
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 non-debug llvm.loop
1349 // metadata, we add the metadata to the branch instructions in the
1350 // predecessors.
1351 if (Instruction *TI = BB->getTerminator())
1352 if (TI->hasNonDebugLocLoopMetadata()) {
1353 MDNode *LoopMD = TI->getMetadata(LLVMContext::MD_loop);
1354 for (BasicBlock *Pred : predecessors(BB))
1355 Pred->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopMD);
1356 }
1357
1358 if (BBKillable) {
1359 // Everything that jumped to BB now goes to Succ.
1360 BB->replaceAllUsesWith(Succ);
1361
1362 if (!Succ->hasName())
1363 Succ->takeName(BB);
1364
1365 // Clear the successor list of BB to match updates applying to DTU later.
1366 if (BB->getTerminator())
1367 BB->back().eraseFromParent();
1368
1369 new UnreachableInst(BB->getContext(), BB);
1370 assert(succ_empty(BB) && "The successor list of BB isn't empty before "
1371 "applying corresponding DTU updates.");
1372 } else if (BBPhisMergeable) {
1373 // Everything except CommonPred that jumped to BB now goes to Succ.
1374 BB->replaceUsesWithIf(Succ, [BBPreds, CommonPred](Use &U) -> bool {
1375 if (Instruction *UseInst = dyn_cast<Instruction>(U.getUser()))
1376 return UseInst->getParent() != CommonPred &&
1377 BBPreds.contains(UseInst->getParent());
1378 return false;
1379 });
1380 }
1381
1382 if (DTU)
1383 DTU->applyUpdates(Updates);
1384
1385 if (BBKillable)
1386 DeleteDeadBlock(BB, DTU);
1387
1388 return true;
1389}
1390
1391static bool
1394 // This implementation doesn't currently consider undef operands
1395 // specially. Theoretically, two phis which are identical except for
1396 // one having an undef where the other doesn't could be collapsed.
1397
1398 bool Changed = false;
1399
1400 // Examine each PHI.
1401 // Note that increment of I must *NOT* be in the iteration_expression, since
1402 // we don't want to immediately advance when we restart from the beginning.
1403 for (auto I = BB->begin(); PHINode *PN = dyn_cast<PHINode>(I);) {
1404 ++I;
1405 // Is there an identical PHI node in this basic block?
1406 // Note that we only look in the upper square's triangle,
1407 // we already checked that the lower triangle PHI's aren't identical.
1408 for (auto J = I; PHINode *DuplicatePN = dyn_cast<PHINode>(J); ++J) {
1409 if (ToRemove.contains(DuplicatePN))
1410 continue;
1411 if (!DuplicatePN->isIdenticalToWhenDefined(PN))
1412 continue;
1413 // A duplicate. Replace this PHI with the base PHI.
1414 ++NumPHICSEs;
1415 DuplicatePN->replaceAllUsesWith(PN);
1416 ToRemove.insert(DuplicatePN);
1417 Changed = true;
1418
1419 // The RAUW can change PHIs that we already visited.
1420 I = BB->begin();
1421 break; // Start over from the beginning.
1422 }
1423 }
1424 return Changed;
1425}
1426
1427static bool
1430 // This implementation doesn't currently consider undef operands
1431 // specially. Theoretically, two phis which are identical except for
1432 // one having an undef where the other doesn't could be collapsed.
1433
1434 struct PHIDenseMapInfo {
1435 static PHINode *getEmptyKey() {
1437 }
1438
1439 static PHINode *getTombstoneKey() {
1441 }
1442
1443 static bool isSentinel(PHINode *PN) {
1444 return PN == getEmptyKey() || PN == getTombstoneKey();
1445 }
1446
1447 // WARNING: this logic must be kept in sync with
1448 // Instruction::isIdenticalToWhenDefined()!
1449 static unsigned getHashValueImpl(PHINode *PN) {
1450 // Compute a hash value on the operands. Instcombine will likely have
1451 // sorted them, which helps expose duplicates, but we have to check all
1452 // the operands to be safe in case instcombine hasn't run.
1453 return static_cast<unsigned>(hash_combine(
1456 }
1457
1458 static unsigned getHashValue(PHINode *PN) {
1459#ifndef NDEBUG
1460 // If -phicse-debug-hash was specified, return a constant -- this
1461 // will force all hashing to collide, so we'll exhaustively search
1462 // the table for a match, and the assertion in isEqual will fire if
1463 // there's a bug causing equal keys to hash differently.
1464 if (PHICSEDebugHash)
1465 return 0;
1466#endif
1467 return getHashValueImpl(PN);
1468 }
1469
1470 static bool isEqualImpl(PHINode *LHS, PHINode *RHS) {
1471 if (isSentinel(LHS) || isSentinel(RHS))
1472 return LHS == RHS;
1473 return LHS->isIdenticalTo(RHS);
1474 }
1475
1476 static bool isEqual(PHINode *LHS, PHINode *RHS) {
1477 // These comparisons are nontrivial, so assert that equality implies
1478 // hash equality (DenseMap demands this as an invariant).
1479 bool Result = isEqualImpl(LHS, RHS);
1480 assert(!Result || (isSentinel(LHS) && LHS == RHS) ||
1482 return Result;
1483 }
1484 };
1485
1486 // Set of unique PHINodes.
1488 PHISet.reserve(4 * PHICSENumPHISmallSize);
1489
1490 // Examine each PHI.
1491 bool Changed = false;
1492 for (auto I = BB->begin(); PHINode *PN = dyn_cast<PHINode>(I++);) {
1493 if (ToRemove.contains(PN))
1494 continue;
1495 auto Inserted = PHISet.insert(PN);
1496 if (!Inserted.second) {
1497 // A duplicate. Replace this PHI with its duplicate.
1498 ++NumPHICSEs;
1499 PN->replaceAllUsesWith(*Inserted.first);
1500 ToRemove.insert(PN);
1501 Changed = true;
1502
1503 // The RAUW can change PHIs that we already visited. Start over from the
1504 // beginning.
1505 PHISet.clear();
1506 I = BB->begin();
1507 }
1508 }
1509
1510 return Changed;
1511}
1512
1515 if (
1516#ifndef NDEBUG
1517 !PHICSEDebugHash &&
1518#endif
1522}
1523
1526 bool Changed = EliminateDuplicatePHINodes(BB, ToRemove);
1527 for (PHINode *PN : ToRemove)
1528 PN->eraseFromParent();
1529 return Changed;
1530}
1531
1533 const DataLayout &DL) {
1534 V = V->stripPointerCasts();
1535
1536 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1537 // TODO: Ideally, this function would not be called if PrefAlign is smaller
1538 // than the current alignment, as the known bits calculation should have
1539 // already taken it into account. However, this is not always the case,
1540 // as computeKnownBits() has a depth limit, while stripPointerCasts()
1541 // doesn't.
1542 Align CurrentAlign = AI->getAlign();
1543 if (PrefAlign <= CurrentAlign)
1544 return CurrentAlign;
1545
1546 // If the preferred alignment is greater than the natural stack alignment
1547 // then don't round up. This avoids dynamic stack realignment.
1548 MaybeAlign StackAlign = DL.getStackAlignment();
1549 if (StackAlign && PrefAlign > *StackAlign)
1550 return CurrentAlign;
1551 AI->setAlignment(PrefAlign);
1552 return PrefAlign;
1553 }
1554
1555 if (auto *GO = dyn_cast<GlobalObject>(V)) {
1556 // TODO: as above, this shouldn't be necessary.
1557 Align CurrentAlign = GO->getPointerAlignment(DL);
1558 if (PrefAlign <= CurrentAlign)
1559 return CurrentAlign;
1560
1561 // If there is a large requested alignment and we can, bump up the alignment
1562 // of the global. If the memory we set aside for the global may not be the
1563 // memory used by the final program then it is impossible for us to reliably
1564 // enforce the preferred alignment.
1565 if (!GO->canIncreaseAlignment())
1566 return CurrentAlign;
1567
1568 if (GO->isThreadLocal()) {
1569 unsigned MaxTLSAlign = GO->getParent()->getMaxTLSAlignment() / CHAR_BIT;
1570 if (MaxTLSAlign && PrefAlign > Align(MaxTLSAlign))
1571 PrefAlign = Align(MaxTLSAlign);
1572 }
1573
1574 GO->setAlignment(PrefAlign);
1575 return PrefAlign;
1576 }
1577
1578 return Align(1);
1579}
1580
1582 const DataLayout &DL,
1583 const Instruction *CxtI,
1584 AssumptionCache *AC,
1585 const DominatorTree *DT) {
1586 assert(V->getType()->isPointerTy() &&
1587 "getOrEnforceKnownAlignment expects a pointer!");
1588
1589 KnownBits Known = computeKnownBits(V, DL, 0, AC, CxtI, DT);
1590 unsigned TrailZ = Known.countMinTrailingZeros();
1591
1592 // Avoid trouble with ridiculously large TrailZ values, such as
1593 // those computed from a null pointer.
1594 // LLVM doesn't support alignments larger than (1 << MaxAlignmentExponent).
1595 TrailZ = std::min(TrailZ, +Value::MaxAlignmentExponent);
1596
1597 Align Alignment = Align(1ull << std::min(Known.getBitWidth() - 1, TrailZ));
1598
1599 if (PrefAlign && *PrefAlign > Alignment)
1600 Alignment = std::max(Alignment, tryEnforceAlignment(V, *PrefAlign, DL));
1601
1602 // We don't need to make any adjustment.
1603 return Alignment;
1604}
1605
1606///===---------------------------------------------------------------------===//
1607/// Dbg Intrinsic utilities
1608///
1609
1610/// See if there is a dbg.value intrinsic for DIVar for the PHI node.
1612 DIExpression *DIExpr,
1613 PHINode *APN) {
1614 // Since we can't guarantee that the original dbg.declare intrinsic
1615 // is removed by LowerDbgDeclare(), we need to make sure that we are
1616 // not inserting the same dbg.value intrinsic over and over.
1618 SmallVector<DbgVariableRecord *, 1> DbgVariableRecords;
1619 findDbgValues(DbgValues, APN, &DbgVariableRecords);
1620 for (auto *DVI : DbgValues) {
1621 assert(is_contained(DVI->getValues(), APN));
1622 if ((DVI->getVariable() == DIVar) && (DVI->getExpression() == DIExpr))
1623 return true;
1624 }
1625 for (auto *DVR : DbgVariableRecords) {
1626 assert(is_contained(DVR->location_ops(), APN));
1627 if ((DVR->getVariable() == DIVar) && (DVR->getExpression() == DIExpr))
1628 return true;
1629 }
1630 return false;
1631}
1632
1633/// Check if the alloc size of \p ValTy is large enough to cover the variable
1634/// (or fragment of the variable) described by \p DII.
1635///
1636/// This is primarily intended as a helper for the different
1637/// ConvertDebugDeclareToDebugValue functions. The dbg.declare that is converted
1638/// describes an alloca'd variable, so we need to use the alloc size of the
1639/// value when doing the comparison. E.g. an i1 value will be identified as
1640/// covering an n-bit fragment, if the store size of i1 is at least n bits.
1642 const DataLayout &DL = DII->getDataLayout();
1643 TypeSize ValueSize = DL.getTypeAllocSizeInBits(ValTy);
1644 if (std::optional<uint64_t> FragmentSize =
1645 DII->getExpression()->getActiveBits(DII->getVariable()))
1646 return TypeSize::isKnownGE(ValueSize, TypeSize::getFixed(*FragmentSize));
1647
1648 // We can't always calculate the size of the DI variable (e.g. if it is a
1649 // VLA). Try to use the size of the alloca that the dbg intrinsic describes
1650 // instead.
1651 if (DII->isAddressOfVariable()) {
1652 // DII should have exactly 1 location when it is an address.
1653 assert(DII->getNumVariableLocationOps() == 1 &&
1654 "address of variable must have exactly 1 location operand.");
1655 if (auto *AI =
1656 dyn_cast_or_null<AllocaInst>(DII->getVariableLocationOp(0))) {
1657 if (std::optional<TypeSize> FragmentSize =
1658 AI->getAllocationSizeInBits(DL)) {
1659 return TypeSize::isKnownGE(ValueSize, *FragmentSize);
1660 }
1661 }
1662 }
1663 // Could not determine size of variable. Conservatively return false.
1664 return false;
1665}
1666// RemoveDIs: duplicate implementation of the above, using DbgVariableRecords,
1667// the replacement for dbg.values.
1669 const DataLayout &DL = DVR->getModule()->getDataLayout();
1670 TypeSize ValueSize = DL.getTypeAllocSizeInBits(ValTy);
1671 if (std::optional<uint64_t> FragmentSize =
1672 DVR->getExpression()->getActiveBits(DVR->getVariable()))
1673 return TypeSize::isKnownGE(ValueSize, TypeSize::getFixed(*FragmentSize));
1674
1675 // We can't always calculate the size of the DI variable (e.g. if it is a
1676 // VLA). Try to use the size of the alloca that the dbg intrinsic describes
1677 // instead.
1678 if (DVR->isAddressOfVariable()) {
1679 // DVR should have exactly 1 location when it is an address.
1680 assert(DVR->getNumVariableLocationOps() == 1 &&
1681 "address of variable must have exactly 1 location operand.");
1682 if (auto *AI =
1683 dyn_cast_or_null<AllocaInst>(DVR->getVariableLocationOp(0))) {
1684 if (std::optional<TypeSize> FragmentSize = AI->getAllocationSizeInBits(DL)) {
1685 return TypeSize::isKnownGE(ValueSize, *FragmentSize);
1686 }
1687 }
1688 }
1689 // Could not determine size of variable. Conservatively return false.
1690 return false;
1691}
1692
1694 DILocalVariable *DIVar,
1695 DIExpression *DIExpr,
1696 const DebugLoc &NewLoc,
1697 BasicBlock::iterator Instr) {
1698 if (!UseNewDbgInfoFormat) {
1699 auto DbgVal = Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, NewLoc,
1700 (Instruction *)nullptr);
1701 cast<Instruction *>(DbgVal)->insertBefore(Instr);
1702 } else {
1703 // RemoveDIs: if we're using the new debug-info format, allocate a
1704 // DbgVariableRecord directly instead of a dbg.value intrinsic.
1706 DbgVariableRecord *DV =
1707 new DbgVariableRecord(DVAM, DIVar, DIExpr, NewLoc.get());
1708 Instr->getParent()->insertDbgRecordBefore(DV, Instr);
1709 }
1710}
1711
1713 DIBuilder &Builder, Value *DV, DILocalVariable *DIVar, DIExpression *DIExpr,
1714 const DebugLoc &NewLoc, BasicBlock::iterator Instr) {
1715 if (!UseNewDbgInfoFormat) {
1716 auto DbgVal = Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, NewLoc,
1717 (Instruction *)nullptr);
1718 cast<Instruction *>(DbgVal)->insertAfter(&*Instr);
1719 } else {
1720 // RemoveDIs: if we're using the new debug-info format, allocate a
1721 // DbgVariableRecord directly instead of a dbg.value intrinsic.
1723 DbgVariableRecord *DV =
1724 new DbgVariableRecord(DVAM, DIVar, DIExpr, NewLoc.get());
1725 Instr->getParent()->insertDbgRecordAfter(DV, &*Instr);
1726 }
1727}
1728
1729/// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
1730/// that has an associated llvm.dbg.declare intrinsic.
1732 StoreInst *SI, DIBuilder &Builder) {
1733 assert(DII->isAddressOfVariable() || isa<DbgAssignIntrinsic>(DII));
1734 auto *DIVar = DII->getVariable();
1735 assert(DIVar && "Missing variable");
1736 auto *DIExpr = DII->getExpression();
1737 Value *DV = SI->getValueOperand();
1738
1739 DebugLoc NewLoc = getDebugValueLoc(DII);
1740
1741 // If the alloca describes the variable itself, i.e. the expression in the
1742 // dbg.declare doesn't start with a dereference, we can perform the
1743 // conversion if the value covers the entire fragment of DII.
1744 // If the alloca describes the *address* of DIVar, i.e. DIExpr is
1745 // *just* a DW_OP_deref, we use DV as is for the dbg.value.
1746 // We conservatively ignore other dereferences, because the following two are
1747 // not equivalent:
1748 // dbg.declare(alloca, ..., !Expr(deref, plus_uconstant, 2))
1749 // dbg.value(DV, ..., !Expr(deref, plus_uconstant, 2))
1750 // The former is adding 2 to the address of the variable, whereas the latter
1751 // is adding 2 to the value of the variable. As such, we insist on just a
1752 // deref expression.
1753 bool CanConvert =
1754 DIExpr->isDeref() || (!DIExpr->startsWithDeref() &&
1756 if (CanConvert) {
1757 insertDbgValueOrDbgVariableRecord(Builder, DV, DIVar, DIExpr, NewLoc,
1758 SI->getIterator());
1759 return;
1760 }
1761
1762 // FIXME: If storing to a part of the variable described by the dbg.declare,
1763 // then we want to insert a dbg.value for the corresponding fragment.
1764 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: " << *DII
1765 << '\n');
1766 // For now, when there is a store to parts of the variable (but we do not
1767 // know which part) we insert an dbg.value intrinsic to indicate that we
1768 // know nothing about the variable's content.
1769 DV = PoisonValue::get(DV->getType());
1770 insertDbgValueOrDbgVariableRecord(Builder, DV, DIVar, DIExpr, NewLoc,
1771 SI->getIterator());
1772}
1773
1775 int NumEltDropped = DIExpr->getElements()[0] == dwarf::DW_OP_LLVM_arg ? 3 : 1;
1776 return DIExpression::get(DIExpr->getContext(),
1777 DIExpr->getElements().drop_front(NumEltDropped));
1778}
1779
1781 DIBuilder &Builder) {
1782 auto *DIVar = DII->getVariable();
1783 assert(DIVar && "Missing variable");
1784 auto *DIExpr = DII->getExpression();
1785 DIExpr = dropInitialDeref(DIExpr);
1786 Value *DV = SI->getValueOperand();
1787
1788 DebugLoc NewLoc = getDebugValueLoc(DII);
1789
1790 insertDbgValueOrDbgVariableRecord(Builder, DV, DIVar, DIExpr, NewLoc,
1791 SI->getIterator());
1792}
1793
1794/// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
1795/// that has an associated llvm.dbg.declare intrinsic.
1797 LoadInst *LI, DIBuilder &Builder) {
1798 auto *DIVar = DII->getVariable();
1799 auto *DIExpr = DII->getExpression();
1800 assert(DIVar && "Missing variable");
1801
1802 if (!valueCoversEntireFragment(LI->getType(), DII)) {
1803 // FIXME: If only referring to a part of the variable described by the
1804 // dbg.declare, then we want to insert a dbg.value for the corresponding
1805 // fragment.
1806 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: "
1807 << *DII << '\n');
1808 return;
1809 }
1810
1811 DebugLoc NewLoc = getDebugValueLoc(DII);
1812
1813 // We are now tracking the loaded value instead of the address. In the
1814 // future if multi-location support is added to the IR, it might be
1815 // preferable to keep tracking both the loaded value and the original
1816 // address in case the alloca can not be elided.
1817 insertDbgValueOrDbgVariableRecordAfter(Builder, LI, DIVar, DIExpr, NewLoc,
1818 LI->getIterator());
1819}
1820
1822 StoreInst *SI, DIBuilder &Builder) {
1823 assert(DVR->isAddressOfVariable() || DVR->isDbgAssign());
1824 auto *DIVar = DVR->getVariable();
1825 assert(DIVar && "Missing variable");
1826 auto *DIExpr = DVR->getExpression();
1827 Value *DV = SI->getValueOperand();
1828
1829 DebugLoc NewLoc = getDebugValueLoc(DVR);
1830
1831 // If the alloca describes the variable itself, i.e. the expression in the
1832 // dbg.declare doesn't start with a dereference, we can perform the
1833 // conversion if the value covers the entire fragment of DII.
1834 // If the alloca describes the *address* of DIVar, i.e. DIExpr is
1835 // *just* a DW_OP_deref, we use DV as is for the dbg.value.
1836 // We conservatively ignore other dereferences, because the following two are
1837 // not equivalent:
1838 // dbg.declare(alloca, ..., !Expr(deref, plus_uconstant, 2))
1839 // dbg.value(DV, ..., !Expr(deref, plus_uconstant, 2))
1840 // The former is adding 2 to the address of the variable, whereas the latter
1841 // is adding 2 to the value of the variable. As such, we insist on just a
1842 // deref expression.
1843 bool CanConvert =
1844 DIExpr->isDeref() || (!DIExpr->startsWithDeref() &&
1846 if (CanConvert) {
1847 insertDbgValueOrDbgVariableRecord(Builder, DV, DIVar, DIExpr, NewLoc,
1848 SI->getIterator());
1849 return;
1850 }
1851
1852 // FIXME: If storing to a part of the variable described by the dbg.declare,
1853 // then we want to insert a dbg.value for the corresponding fragment.
1854 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: " << *DVR
1855 << '\n');
1857
1858 // For now, when there is a store to parts of the variable (but we do not
1859 // know which part) we insert an dbg.value intrinsic to indicate that we
1860 // know nothing about the variable's content.
1861 DV = PoisonValue::get(DV->getType());
1863 DbgVariableRecord *NewDVR =
1864 new DbgVariableRecord(DVAM, DIVar, DIExpr, NewLoc.get());
1865 SI->getParent()->insertDbgRecordBefore(NewDVR, SI->getIterator());
1866}
1867
1869 DIBuilder &Builder) {
1870 auto *DIVar = DVR->getVariable();
1871 assert(DIVar && "Missing variable");
1872 auto *DIExpr = DVR->getExpression();
1873 DIExpr = dropInitialDeref(DIExpr);
1874 Value *DV = SI->getValueOperand();
1875
1876 DebugLoc NewLoc = getDebugValueLoc(DVR);
1877
1878 insertDbgValueOrDbgVariableRecord(Builder, DV, DIVar, DIExpr, NewLoc,
1879 SI->getIterator());
1880}
1881
1882/// Inserts a llvm.dbg.value intrinsic after a phi that has an associated
1883/// llvm.dbg.declare intrinsic.
1885 PHINode *APN, DIBuilder &Builder) {
1886 auto *DIVar = DII->getVariable();
1887 auto *DIExpr = DII->getExpression();
1888 assert(DIVar && "Missing variable");
1889
1890 if (PhiHasDebugValue(DIVar, DIExpr, APN))
1891 return;
1892
1893 if (!valueCoversEntireFragment(APN->getType(), DII)) {
1894 // FIXME: If only referring to a part of the variable described by the
1895 // dbg.declare, then we want to insert a dbg.value for the corresponding
1896 // fragment.
1897 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: "
1898 << *DII << '\n');
1899 return;
1900 }
1901
1902 BasicBlock *BB = APN->getParent();
1903 auto InsertionPt = BB->getFirstInsertionPt();
1904
1905 DebugLoc NewLoc = getDebugValueLoc(DII);
1906
1907 // The block may be a catchswitch block, which does not have a valid
1908 // insertion point.
1909 // FIXME: Insert dbg.value markers in the successors when appropriate.
1910 if (InsertionPt != BB->end()) {
1911 insertDbgValueOrDbgVariableRecord(Builder, APN, DIVar, DIExpr, NewLoc,
1912 InsertionPt);
1913 }
1914}
1915
1917 DIBuilder &Builder) {
1918 auto *DIVar = DVR->getVariable();
1919 auto *DIExpr = DVR->getExpression();
1920 assert(DIVar && "Missing variable");
1921
1922 if (!valueCoversEntireFragment(LI->getType(), DVR)) {
1923 // FIXME: If only referring to a part of the variable described by the
1924 // dbg.declare, then we want to insert a DbgVariableRecord for the
1925 // corresponding fragment.
1926 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to DbgVariableRecord: "
1927 << *DVR << '\n');
1928 return;
1929 }
1930
1931 DebugLoc NewLoc = getDebugValueLoc(DVR);
1932
1933 // We are now tracking the loaded value instead of the address. In the
1934 // future if multi-location support is added to the IR, it might be
1935 // preferable to keep tracking both the loaded value and the original
1936 // address in case the alloca can not be elided.
1938
1939 // Create a DbgVariableRecord directly and insert.
1941 DbgVariableRecord *DV =
1942 new DbgVariableRecord(LIVAM, DIVar, DIExpr, NewLoc.get());
1943 LI->getParent()->insertDbgRecordAfter(DV, LI);
1944}
1945
1946/// Determine whether this alloca is either a VLA or an array.
1947static bool isArray(AllocaInst *AI) {
1948 return AI->isArrayAllocation() ||
1949 (AI->getAllocatedType() && AI->getAllocatedType()->isArrayTy());
1950}
1951
1952/// Determine whether this alloca is a structure.
1953static bool isStructure(AllocaInst *AI) {
1954 return AI->getAllocatedType() && AI->getAllocatedType()->isStructTy();
1955}
1957 DIBuilder &Builder) {
1958 auto *DIVar = DVR->getVariable();
1959 auto *DIExpr = DVR->getExpression();
1960 assert(DIVar && "Missing variable");
1961
1962 if (PhiHasDebugValue(DIVar, DIExpr, APN))
1963 return;
1964
1965 if (!valueCoversEntireFragment(APN->getType(), DVR)) {
1966 // FIXME: If only referring to a part of the variable described by the
1967 // dbg.declare, then we want to insert a DbgVariableRecord for the
1968 // corresponding fragment.
1969 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to DbgVariableRecord: "
1970 << *DVR << '\n');
1971 return;
1972 }
1973
1974 BasicBlock *BB = APN->getParent();
1975 auto InsertionPt = BB->getFirstInsertionPt();
1976
1977 DebugLoc NewLoc = getDebugValueLoc(DVR);
1978
1979 // The block may be a catchswitch block, which does not have a valid
1980 // insertion point.
1981 // FIXME: Insert DbgVariableRecord markers in the successors when appropriate.
1982 if (InsertionPt != BB->end()) {
1983 insertDbgValueOrDbgVariableRecord(Builder, APN, DIVar, DIExpr, NewLoc,
1984 InsertionPt);
1985 }
1986}
1987
1988/// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set
1989/// of llvm.dbg.value intrinsics.
1991 bool Changed = false;
1992 DIBuilder DIB(*F.getParent(), /*AllowUnresolved*/ false);
1995 for (auto &FI : F) {
1996 for (Instruction &BI : FI) {
1997 if (auto *DDI = dyn_cast<DbgDeclareInst>(&BI))
1998 Dbgs.push_back(DDI);
1999 for (DbgVariableRecord &DVR : filterDbgVars(BI.getDbgRecordRange())) {
2000 if (DVR.getType() == DbgVariableRecord::LocationType::Declare)
2001 DVRs.push_back(&DVR);
2002 }
2003 }
2004 }
2005
2006 if (Dbgs.empty() && DVRs.empty())
2007 return Changed;
2008
2009 auto LowerOne = [&](auto *DDI) {
2010 AllocaInst *AI =
2011 dyn_cast_or_null<AllocaInst>(DDI->getVariableLocationOp(0));
2012 // If this is an alloca for a scalar variable, insert a dbg.value
2013 // at each load and store to the alloca and erase the dbg.declare.
2014 // The dbg.values allow tracking a variable even if it is not
2015 // stored on the stack, while the dbg.declare can only describe
2016 // the stack slot (and at a lexical-scope granularity). Later
2017 // passes will attempt to elide the stack slot.
2018 if (!AI || isArray(AI) || isStructure(AI))
2019 return;
2020
2021 // A volatile load/store means that the alloca can't be elided anyway.
2022 if (llvm::any_of(AI->users(), [](User *U) -> bool {
2023 if (LoadInst *LI = dyn_cast<LoadInst>(U))
2024 return LI->isVolatile();
2025 if (StoreInst *SI = dyn_cast<StoreInst>(U))
2026 return SI->isVolatile();
2027 return false;
2028 }))
2029 return;
2030
2032 WorkList.push_back(AI);
2033 while (!WorkList.empty()) {
2034 const Value *V = WorkList.pop_back_val();
2035 for (const auto &AIUse : V->uses()) {
2036 User *U = AIUse.getUser();
2037 if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
2038 if (AIUse.getOperandNo() == 1)
2039 ConvertDebugDeclareToDebugValue(DDI, SI, DIB);
2040 } else if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
2041 ConvertDebugDeclareToDebugValue(DDI, LI, DIB);
2042 } else if (CallInst *CI = dyn_cast<CallInst>(U)) {
2043 // This is a call by-value or some other instruction that takes a
2044 // pointer to the variable. Insert a *value* intrinsic that describes
2045 // the variable by dereferencing the alloca.
2046 if (!CI->isLifetimeStartOrEnd()) {
2047 DebugLoc NewLoc = getDebugValueLoc(DDI);
2048 auto *DerefExpr =
2049 DIExpression::append(DDI->getExpression(), dwarf::DW_OP_deref);
2050 insertDbgValueOrDbgVariableRecord(DIB, AI, DDI->getVariable(),
2051 DerefExpr, NewLoc,
2052 CI->getIterator());
2053 }
2054 } else if (BitCastInst *BI = dyn_cast<BitCastInst>(U)) {
2055 if (BI->getType()->isPointerTy())
2056 WorkList.push_back(BI);
2057 }
2058 }
2059 }
2060 DDI->eraseFromParent();
2061 Changed = true;
2062 };
2063
2064 for_each(Dbgs, LowerOne);
2065 for_each(DVRs, LowerOne);
2066
2067 if (Changed)
2068 for (BasicBlock &BB : F)
2070
2071 return Changed;
2072}
2073
2074// RemoveDIs: re-implementation of insertDebugValuesForPHIs, but which pulls the
2075// debug-info out of the block's DbgVariableRecords rather than dbg.value
2076// intrinsics.
2077static void
2079 SmallVectorImpl<PHINode *> &InsertedPHIs) {
2080 assert(BB && "No BasicBlock to clone DbgVariableRecord(s) from.");
2081 if (InsertedPHIs.size() == 0)
2082 return;
2083
2084 // Map existing PHI nodes to their DbgVariableRecords.
2086 for (auto &I : *BB) {
2087 for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {
2088 for (Value *V : DVR.location_ops())
2089 if (auto *Loc = dyn_cast_or_null<PHINode>(V))
2090 DbgValueMap.insert({Loc, &DVR});
2091 }
2092 }
2093 if (DbgValueMap.size() == 0)
2094 return;
2095
2096 // Map a pair of the destination BB and old DbgVariableRecord to the new
2097 // DbgVariableRecord, so that if a DbgVariableRecord is being rewritten to use
2098 // more than one of the inserted PHIs in the same destination BB, we can
2099 // update the same DbgVariableRecord with all the new PHIs instead of creating
2100 // one copy for each.
2102 NewDbgValueMap;
2103 // Then iterate through the new PHIs and look to see if they use one of the
2104 // previously mapped PHIs. If so, create a new DbgVariableRecord that will
2105 // propagate the info through the new PHI. If we use more than one new PHI in
2106 // a single destination BB with the same old dbg.value, merge the updates so
2107 // that we get a single new DbgVariableRecord with all the new PHIs.
2108 for (auto PHI : InsertedPHIs) {
2109 BasicBlock *Parent = PHI->getParent();
2110 // Avoid inserting a debug-info record into an EH block.
2111 if (Parent->getFirstNonPHI()->isEHPad())
2112 continue;
2113 for (auto VI : PHI->operand_values()) {
2114 auto V = DbgValueMap.find(VI);
2115 if (V != DbgValueMap.end()) {
2116 DbgVariableRecord *DbgII = cast<DbgVariableRecord>(V->second);
2117 auto NewDI = NewDbgValueMap.find({Parent, DbgII});
2118 if (NewDI == NewDbgValueMap.end()) {
2119 DbgVariableRecord *NewDbgII = DbgII->clone();
2120 NewDI = NewDbgValueMap.insert({{Parent, DbgII}, NewDbgII}).first;
2121 }
2122 DbgVariableRecord *NewDbgII = NewDI->second;
2123 // If PHI contains VI as an operand more than once, we may
2124 // replaced it in NewDbgII; confirm that it is present.
2125 if (is_contained(NewDbgII->location_ops(), VI))
2126 NewDbgII->replaceVariableLocationOp(VI, PHI);
2127 }
2128 }
2129 }
2130 // Insert the new DbgVariableRecords into their destination blocks.
2131 for (auto DI : NewDbgValueMap) {
2132 BasicBlock *Parent = DI.first.first;
2133 DbgVariableRecord *NewDbgII = DI.second;
2134 auto InsertionPt = Parent->getFirstInsertionPt();
2135 assert(InsertionPt != Parent->end() && "Ill-formed basic block");
2136
2137 Parent->insertDbgRecordBefore(NewDbgII, InsertionPt);
2138 }
2139}
2140
2141/// Propagate dbg.value intrinsics through the newly inserted PHIs.
2143 SmallVectorImpl<PHINode *> &InsertedPHIs) {
2144 assert(BB && "No BasicBlock to clone dbg.value(s) from.");
2145 if (InsertedPHIs.size() == 0)
2146 return;
2147
2148 insertDbgVariableRecordsForPHIs(BB, InsertedPHIs);
2149
2150 // Map existing PHI nodes to their dbg.values.
2151 ValueToValueMapTy DbgValueMap;
2152 for (auto &I : *BB) {
2153 if (auto DbgII = dyn_cast<DbgVariableIntrinsic>(&I)) {
2154 for (Value *V : DbgII->location_ops())
2155 if (auto *Loc = dyn_cast_or_null<PHINode>(V))
2156 DbgValueMap.insert({Loc, DbgII});
2157 }
2158 }
2159 if (DbgValueMap.size() == 0)
2160 return;
2161
2162 // Map a pair of the destination BB and old dbg.value to the new dbg.value,
2163 // so that if a dbg.value is being rewritten to use more than one of the
2164 // inserted PHIs in the same destination BB, we can update the same dbg.value
2165 // with all the new PHIs instead of creating one copy for each.
2168 NewDbgValueMap;
2169 // Then iterate through the new PHIs and look to see if they use one of the
2170 // previously mapped PHIs. If so, create a new dbg.value intrinsic that will
2171 // propagate the info through the new PHI. If we use more than one new PHI in
2172 // a single destination BB with the same old dbg.value, merge the updates so
2173 // that we get a single new dbg.value with all the new PHIs.
2174 for (auto *PHI : InsertedPHIs) {
2175 BasicBlock *Parent = PHI->getParent();
2176 // Avoid inserting an intrinsic into an EH block.
2177 if (Parent->getFirstNonPHI()->isEHPad())
2178 continue;
2179 for (auto *VI : PHI->operand_values()) {
2180 auto V = DbgValueMap.find(VI);
2181 if (V != DbgValueMap.end()) {
2182 auto *DbgII = cast<DbgVariableIntrinsic>(V->second);
2183 auto [NewDI, Inserted] = NewDbgValueMap.try_emplace({Parent, DbgII});
2184 if (Inserted)
2185 NewDI->second = cast<DbgVariableIntrinsic>(DbgII->clone());
2186 DbgVariableIntrinsic *NewDbgII = NewDI->second;
2187 // If PHI contains VI as an operand more than once, we may
2188 // replaced it in NewDbgII; confirm that it is present.
2189 if (is_contained(NewDbgII->location_ops(), VI))
2190 NewDbgII->replaceVariableLocationOp(VI, PHI);
2191 }
2192 }
2193 }
2194 // Insert thew new dbg.values into their destination blocks.
2195 for (auto DI : NewDbgValueMap) {
2196 BasicBlock *Parent = DI.first.first;
2197 auto *NewDbgII = DI.second;
2198 auto InsertionPt = Parent->getFirstInsertionPt();
2199 assert(InsertionPt != Parent->end() && "Ill-formed basic block");
2200 NewDbgII->insertBefore(&*InsertionPt);
2201 }
2202}
2203
2204bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress,
2205 DIBuilder &Builder, uint8_t DIExprFlags,
2206 int Offset) {
2209
2210 auto ReplaceOne = [&](auto *DII) {
2211 assert(DII->getVariable() && "Missing variable");
2212 auto *DIExpr = DII->getExpression();
2213 DIExpr = DIExpression::prepend(DIExpr, DIExprFlags, Offset);
2214 DII->setExpression(DIExpr);
2215 DII->replaceVariableLocationOp(Address, NewAddress);
2216 };
2217
2218 for_each(DbgDeclares, ReplaceOne);
2219 for_each(DVRDeclares, ReplaceOne);
2220
2221 return !DbgDeclares.empty() || !DVRDeclares.empty();
2222}
2223
2225 DILocalVariable *DIVar,
2226 DIExpression *DIExpr, Value *NewAddress,
2227 DbgValueInst *DVI,
2228 DbgVariableRecord *DVR,
2229 DIBuilder &Builder, int Offset) {
2230 assert(DIVar && "Missing variable");
2231
2232 // This is an alloca-based dbg.value/DbgVariableRecord. The first thing it
2233 // should do with the alloca pointer is dereference it. Otherwise we don't
2234 // know how to handle it and give up.
2235 if (!DIExpr || DIExpr->getNumElements() < 1 ||
2236 DIExpr->getElement(0) != dwarf::DW_OP_deref)
2237 return;
2238
2239 // Insert the offset before the first deref.
2240 if (Offset)
2241 DIExpr = DIExpression::prepend(DIExpr, 0, Offset);
2242
2243 if (DVI) {
2244 DVI->setExpression(DIExpr);
2245 DVI->replaceVariableLocationOp(0u, NewAddress);
2246 } else {
2247 assert(DVR);
2248 DVR->setExpression(DIExpr);
2249 DVR->replaceVariableLocationOp(0u, NewAddress);
2250 }
2251}
2252
2254 DIBuilder &Builder, int Offset) {
2257 findDbgValues(DbgUsers, AI, &DPUsers);
2258
2259 // Attempt to replace dbg.values that use this alloca.
2260 for (auto *DVI : DbgUsers)
2261 updateOneDbgValueForAlloca(DVI->getDebugLoc(), DVI->getVariable(),
2262 DVI->getExpression(), NewAllocaAddress, DVI,
2263 nullptr, Builder, Offset);
2264
2265 // Replace any DbgVariableRecords that use this alloca.
2266 for (DbgVariableRecord *DVR : DPUsers)
2267 updateOneDbgValueForAlloca(DVR->getDebugLoc(), DVR->getVariable(),
2268 DVR->getExpression(), NewAllocaAddress, nullptr,
2269 DVR, Builder, Offset);
2270}
2271
2272/// Where possible to salvage debug information for \p I do so.
2273/// If not possible mark undef.
2277 findDbgUsers(DbgUsers, &I, &DPUsers);
2278 salvageDebugInfoForDbgValues(I, DbgUsers, DPUsers);
2279}
2280
2281template <typename T> static void salvageDbgAssignAddress(T *Assign) {
2282 Instruction *I = dyn_cast<Instruction>(Assign->getAddress());
2283 // Only instructions can be salvaged at the moment.
2284 if (!I)
2285 return;
2286
2287 assert(!Assign->getAddressExpression()->getFragmentInfo().has_value() &&
2288 "address-expression shouldn't have fragment info");
2289
2290 // The address component of a dbg.assign cannot be variadic.
2291 uint64_t CurrentLocOps = 0;
2292 SmallVector<Value *, 4> AdditionalValues;
2294 Value *NewV = salvageDebugInfoImpl(*I, CurrentLocOps, Ops, AdditionalValues);
2295
2296 // Check if the salvage failed.
2297 if (!NewV)
2298 return;
2299
2301 Assign->getAddressExpression(), Ops, 0, /*StackValue=*/false);
2302 assert(!SalvagedExpr->getFragmentInfo().has_value() &&
2303 "address-expression shouldn't have fragment info");
2304
2305 SalvagedExpr = SalvagedExpr->foldConstantMath();
2306
2307 // Salvage succeeds if no additional values are required.
2308 if (AdditionalValues.empty()) {
2309 Assign->setAddress(NewV);
2310 Assign->setAddressExpression(SalvagedExpr);
2311 } else {
2312 Assign->setKillAddress();
2313 }
2314}
2315
2319 // These are arbitrary chosen limits on the maximum number of values and the
2320 // maximum size of a debug expression we can salvage up to, used for
2321 // performance reasons.
2322 const unsigned MaxDebugArgs = 16;
2323 const unsigned MaxExpressionSize = 128;
2324 bool Salvaged = false;
2325
2326 for (auto *DII : DbgUsers) {
2327 if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(DII)) {
2328 if (DAI->getAddress() == &I) {
2330 Salvaged = true;
2331 }
2332 if (DAI->getValue() != &I)
2333 continue;
2334 }
2335
2336 // Do not add DW_OP_stack_value for DbgDeclare, because they are implicitly
2337 // pointing out the value as a DWARF memory location description.
2338 bool StackValue = isa<DbgValueInst>(DII);
2339 auto DIILocation = DII->location_ops();
2340 assert(
2341 is_contained(DIILocation, &I) &&
2342 "DbgVariableIntrinsic must use salvaged instruction as its location");
2343 SmallVector<Value *, 4> AdditionalValues;
2344 // `I` may appear more than once in DII's location ops, and each use of `I`
2345 // must be updated in the DIExpression and potentially have additional
2346 // values added; thus we call salvageDebugInfoImpl for each `I` instance in
2347 // DIILocation.
2348 Value *Op0 = nullptr;
2349 DIExpression *SalvagedExpr = DII->getExpression();
2350 auto LocItr = find(DIILocation, &I);
2351 while (SalvagedExpr && LocItr != DIILocation.end()) {
2353 unsigned LocNo = std::distance(DIILocation.begin(), LocItr);
2354 uint64_t CurrentLocOps = SalvagedExpr->getNumLocationOperands();
2355 Op0 = salvageDebugInfoImpl(I, CurrentLocOps, Ops, AdditionalValues);
2356 if (!Op0)
2357 break;
2358 SalvagedExpr =
2359 DIExpression::appendOpsToArg(SalvagedExpr, Ops, LocNo, StackValue);
2360 LocItr = std::find(++LocItr, DIILocation.end(), &I);
2361 }
2362 // salvageDebugInfoImpl should fail on examining the first element of
2363 // DbgUsers, or none of them.
2364 if (!Op0)
2365 break;
2366
2367 SalvagedExpr = SalvagedExpr->foldConstantMath();
2368 DII->replaceVariableLocationOp(&I, Op0);
2369 bool IsValidSalvageExpr = SalvagedExpr->getNumElements() <= MaxExpressionSize;
2370 if (AdditionalValues.empty() && IsValidSalvageExpr) {
2371 DII->setExpression(SalvagedExpr);
2372 } else if (isa<DbgValueInst>(DII) && IsValidSalvageExpr &&
2373 DII->getNumVariableLocationOps() + AdditionalValues.size() <=
2374 MaxDebugArgs) {
2375 DII->addVariableLocationOps(AdditionalValues, SalvagedExpr);
2376 } else {
2377 // Do not salvage using DIArgList for dbg.declare, as it is not currently
2378 // supported in those instructions. Also do not salvage if the resulting
2379 // DIArgList would contain an unreasonably large number of values.
2380 DII->setKillLocation();
2381 }
2382 LLVM_DEBUG(dbgs() << "SALVAGE: " << *DII << '\n');
2383 Salvaged = true;
2384 }
2385 // Duplicate of above block for DbgVariableRecords.
2386 for (auto *DVR : DPUsers) {
2387 if (DVR->isDbgAssign()) {
2388 if (DVR->getAddress() == &I) {
2390 Salvaged = true;
2391 }
2392 if (DVR->getValue() != &I)
2393 continue;
2394 }
2395
2396 // Do not add DW_OP_stack_value for DbgDeclare and DbgAddr, because they
2397 // are implicitly pointing out the value as a DWARF memory location
2398 // description.
2399 bool StackValue =
2400 DVR->getType() != DbgVariableRecord::LocationType::Declare;
2401 auto DVRLocation = DVR->location_ops();
2402 assert(
2403 is_contained(DVRLocation, &I) &&
2404 "DbgVariableIntrinsic must use salvaged instruction as its location");
2405 SmallVector<Value *, 4> AdditionalValues;
2406 // 'I' may appear more than once in DVR's location ops, and each use of 'I'
2407 // must be updated in the DIExpression and potentially have additional
2408 // values added; thus we call salvageDebugInfoImpl for each 'I' instance in
2409 // DVRLocation.
2410 Value *Op0 = nullptr;
2411 DIExpression *SalvagedExpr = DVR->getExpression();
2412 auto LocItr = find(DVRLocation, &I);
2413 while (SalvagedExpr && LocItr != DVRLocation.end()) {
2415 unsigned LocNo = std::distance(DVRLocation.begin(), LocItr);
2416 uint64_t CurrentLocOps = SalvagedExpr->getNumLocationOperands();
2417 Op0 = salvageDebugInfoImpl(I, CurrentLocOps, Ops, AdditionalValues);
2418 if (!Op0)
2419 break;
2420 SalvagedExpr =
2421 DIExpression::appendOpsToArg(SalvagedExpr, Ops, LocNo, StackValue);
2422 LocItr = std::find(++LocItr, DVRLocation.end(), &I);
2423 }
2424 // salvageDebugInfoImpl should fail on examining the first element of
2425 // DbgUsers, or none of them.
2426 if (!Op0)
2427 break;
2428
2429 SalvagedExpr = SalvagedExpr->foldConstantMath();
2430 DVR->replaceVariableLocationOp(&I, Op0);
2431 bool IsValidSalvageExpr =
2432 SalvagedExpr->getNumElements() <= MaxExpressionSize;
2433 if (AdditionalValues.empty() && IsValidSalvageExpr) {
2434 DVR->setExpression(SalvagedExpr);
2435 } else if (DVR->getType() != DbgVariableRecord::LocationType::Declare &&
2436 IsValidSalvageExpr &&
2437 DVR->getNumVariableLocationOps() + AdditionalValues.size() <=
2438 MaxDebugArgs) {
2439 DVR->addVariableLocationOps(AdditionalValues, SalvagedExpr);
2440 } else {
2441 // Do not salvage using DIArgList for dbg.addr/dbg.declare, as it is
2442 // currently only valid for stack value expressions.
2443 // Also do not salvage if the resulting DIArgList would contain an
2444 // unreasonably large number of values.
2445 DVR->setKillLocation();
2446 }
2447 LLVM_DEBUG(dbgs() << "SALVAGE: " << DVR << '\n');
2448 Salvaged = true;
2449 }
2450
2451 if (Salvaged)
2452 return;
2453
2454 for (auto *DII : DbgUsers)
2455 DII->setKillLocation();
2456
2457 for (auto *DVR : DPUsers)
2458 DVR->setKillLocation();
2459}
2460
2462 uint64_t CurrentLocOps,
2464 SmallVectorImpl<Value *> &AdditionalValues) {
2465 unsigned BitWidth = DL.getIndexSizeInBits(GEP->getPointerAddressSpace());
2466 // Rewrite a GEP into a DIExpression.
2467 SmallMapVector<Value *, APInt, 4> VariableOffsets;
2468 APInt ConstantOffset(BitWidth, 0);
2469 if (!GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset))
2470 return nullptr;
2471 if (!VariableOffsets.empty() && !CurrentLocOps) {
2472 Opcodes.insert(Opcodes.begin(), {dwarf::DW_OP_LLVM_arg, 0});
2473 CurrentLocOps = 1;
2474 }
2475 for (const auto &Offset : VariableOffsets) {
2476 AdditionalValues.push_back(Offset.first);
2477 assert(Offset.second.isStrictlyPositive() &&
2478 "Expected strictly positive multiplier for offset.");
2479 Opcodes.append({dwarf::DW_OP_LLVM_arg, CurrentLocOps++, dwarf::DW_OP_constu,
2480 Offset.second.getZExtValue(), dwarf::DW_OP_mul,
2481 dwarf::DW_OP_plus});
2482 }
2483 DIExpression::appendOffset(Opcodes, ConstantOffset.getSExtValue());
2484 return GEP->getOperand(0);
2485}
2486
2488 switch (Opcode) {
2489 case Instruction::Add:
2490 return dwarf::DW_OP_plus;
2491 case Instruction::Sub:
2492 return dwarf::DW_OP_minus;
2493 case Instruction::Mul:
2494 return dwarf::DW_OP_mul;
2495 case Instruction::SDiv:
2496 return dwarf::DW_OP_div;
2497 case Instruction::SRem:
2498 return dwarf::DW_OP_mod;
2499 case Instruction::Or:
2500 return dwarf::DW_OP_or;
2501 case Instruction::And:
2502 return dwarf::DW_OP_and;
2503 case Instruction::Xor:
2504 return dwarf::DW_OP_xor;
2505 case Instruction::Shl:
2506 return dwarf::DW_OP_shl;
2507 case Instruction::LShr:
2508 return dwarf::DW_OP_shr;
2509 case Instruction::AShr:
2510 return dwarf::DW_OP_shra;
2511 default:
2512 // TODO: Salvage from each kind of binop we know about.
2513 return 0;
2514 }
2515}
2516
2517static void handleSSAValueOperands(uint64_t CurrentLocOps,
2519 SmallVectorImpl<Value *> &AdditionalValues,
2520 Instruction *I) {
2521 if (!CurrentLocOps) {
2522 Opcodes.append({dwarf::DW_OP_LLVM_arg, 0});
2523 CurrentLocOps = 1;
2524 }
2525 Opcodes.append({dwarf::DW_OP_LLVM_arg, CurrentLocOps});
2526 AdditionalValues.push_back(I->getOperand(1));
2527}
2528
2531 SmallVectorImpl<Value *> &AdditionalValues) {
2532 // Handle binary operations with constant integer operands as a special case.
2533 auto *ConstInt = dyn_cast<ConstantInt>(BI->getOperand(1));
2534 // Values wider than 64 bits cannot be represented within a DIExpression.
2535 if (ConstInt && ConstInt->getBitWidth() > 64)
2536 return nullptr;
2537
2538 Instruction::BinaryOps BinOpcode = BI->getOpcode();
2539 // Push any Constant Int operand onto the expression stack.
2540 if (ConstInt) {
2541 uint64_t Val = ConstInt->getSExtValue();
2542 // Add or Sub Instructions with a constant operand can potentially be
2543 // simplified.
2544 if (BinOpcode == Instruction::Add || BinOpcode == Instruction::Sub) {
2545 uint64_t Offset = BinOpcode == Instruction::Add ? Val : -int64_t(Val);
2547 return BI->getOperand(0);
2548 }
2549 Opcodes.append({dwarf::DW_OP_constu, Val});
2550 } else {
2551 handleSSAValueOperands(CurrentLocOps, Opcodes, AdditionalValues, BI);
2552 }
2553
2554 // Add salvaged binary operator to expression stack, if it has a valid
2555 // representation in a DIExpression.
2556 uint64_t DwarfBinOp = getDwarfOpForBinOp(BinOpcode);
2557 if (!DwarfBinOp)
2558 return nullptr;
2559 Opcodes.push_back(DwarfBinOp);
2560 return BI->getOperand(0);
2561}
2562
2564 // The signedness of the operation is implicit in the typed stack, signed and
2565 // unsigned instructions map to the same DWARF opcode.
2566 switch (Pred) {
2567 case CmpInst::ICMP_EQ:
2568 return dwarf::DW_OP_eq;
2569 case CmpInst::ICMP_NE:
2570 return dwarf::DW_OP_ne;
2571 case CmpInst::ICMP_UGT:
2572 case CmpInst::ICMP_SGT:
2573 return dwarf::DW_OP_gt;
2574 case CmpInst::ICMP_UGE:
2575 case CmpInst::ICMP_SGE:
2576 return dwarf::DW_OP_ge;
2577 case CmpInst::ICMP_ULT:
2578 case CmpInst::ICMP_SLT:
2579 return dwarf::DW_OP_lt;
2580 case CmpInst::ICMP_ULE:
2581 case CmpInst::ICMP_SLE:
2582 return dwarf::DW_OP_le;
2583 default:
2584 return 0;
2585 }
2586}
2587
2590 SmallVectorImpl<Value *> &AdditionalValues) {
2591 // Handle icmp operations with constant integer operands as a special case.
2592 auto *ConstInt = dyn_cast<ConstantInt>(Icmp->getOperand(1));
2593 // Values wider than 64 bits cannot be represented within a DIExpression.
2594 if (ConstInt && ConstInt->getBitWidth() > 64)
2595 return nullptr;
2596 // Push any Constant Int operand onto the expression stack.
2597 if (ConstInt) {
2598 if (Icmp->isSigned())
2599 Opcodes.push_back(dwarf::DW_OP_consts);
2600 else
2601 Opcodes.push_back(dwarf::DW_OP_constu);
2602 uint64_t Val = ConstInt->getSExtValue();
2603 Opcodes.push_back(Val);
2604 } else {
2605 handleSSAValueOperands(CurrentLocOps, Opcodes, AdditionalValues, Icmp);
2606 }
2607
2608 // Add salvaged binary operator to expression stack, if it has a valid
2609 // representation in a DIExpression.
2610 uint64_t DwarfIcmpOp = getDwarfOpForIcmpPred(Icmp->getPredicate());
2611 if (!DwarfIcmpOp)
2612 return nullptr;
2613 Opcodes.push_back(DwarfIcmpOp);
2614 return Icmp->getOperand(0);
2615}
2616
2619 SmallVectorImpl<Value *> &AdditionalValues) {
2620 auto &M = *I.getModule();
2621 auto &DL = M.getDataLayout();
2622
2623 if (auto *CI = dyn_cast<CastInst>(&I)) {
2624 Value *FromValue = CI->getOperand(0);
2625 // No-op casts are irrelevant for debug info.
2626 if (CI->isNoopCast(DL)) {
2627 return FromValue;
2628 }
2629
2630 Type *Type = CI->getType();
2631 if (Type->isPointerTy())
2632 Type = DL.getIntPtrType(Type);
2633 // Casts other than Trunc, SExt, or ZExt to scalar types cannot be salvaged.
2634 if (Type->isVectorTy() ||
2635 !(isa<TruncInst>(&I) || isa<SExtInst>(&I) || isa<ZExtInst>(&I) ||
2636 isa<IntToPtrInst>(&I) || isa<PtrToIntInst>(&I)))
2637 return nullptr;
2638
2639 llvm::Type *FromType = FromValue->getType();
2640 if (FromType->isPointerTy())
2641 FromType = DL.getIntPtrType(FromType);
2642
2643 unsigned FromTypeBitSize = FromType->getScalarSizeInBits();
2644 unsigned ToTypeBitSize = Type->getScalarSizeInBits();
2645
2646 auto ExtOps = DIExpression::getExtOps(FromTypeBitSize, ToTypeBitSize,
2647 isa<SExtInst>(&I));
2648 Ops.append(ExtOps.begin(), ExtOps.end());
2649 return FromValue;
2650 }
2651
2652 if (auto *GEP = dyn_cast<GetElementPtrInst>(&I))
2653 return getSalvageOpsForGEP(GEP, DL, CurrentLocOps, Ops, AdditionalValues);
2654 if (auto *BI = dyn_cast<BinaryOperator>(&I))
2655 return getSalvageOpsForBinOp(BI, CurrentLocOps, Ops, AdditionalValues);
2656 if (auto *IC = dyn_cast<ICmpInst>(&I))
2657 return getSalvageOpsForIcmpOp(IC, CurrentLocOps, Ops, AdditionalValues);
2658
2659 // *Not* to do: we should not attempt to salvage load instructions,
2660 // because the validity and lifetime of a dbg.value containing
2661 // DW_OP_deref becomes difficult to analyze. See PR40628 for examples.
2662 return nullptr;
2663}
2664
2665/// A replacement for a dbg.value expression.
2666using DbgValReplacement = std::optional<DIExpression *>;
2667
2668/// Point debug users of \p From to \p To using exprs given by \p RewriteExpr,
2669/// possibly moving/undefing users to prevent use-before-def. Returns true if
2670/// changes are made.
2672 Instruction &From, Value &To, Instruction &DomPoint, DominatorTree &DT,
2674 function_ref<DbgValReplacement(DbgVariableRecord &DVR)> RewriteDVRExpr) {
2675 // Find debug users of From.
2678 findDbgUsers(Users, &From, &DPUsers);
2679 if (Users.empty() && DPUsers.empty())
2680 return false;
2681
2682 // Prevent use-before-def of To.
2683 bool Changed = false;
2684
2686 SmallPtrSet<DbgVariableRecord *, 1> UndefOrSalvageDVR;
2687 if (isa<Instruction>(&To)) {
2688 bool DomPointAfterFrom = From.getNextNonDebugInstruction() == &DomPoint;
2689
2690 for (auto *DII : Users) {
2691 // It's common to see a debug user between From and DomPoint. Move it
2692 // after DomPoint to preserve the variable update without any reordering.
2693 if (DomPointAfterFrom && DII->getNextNonDebugInstruction() == &DomPoint) {
2694 LLVM_DEBUG(dbgs() << "MOVE: " << *DII << '\n');
2695 DII->moveAfter(&DomPoint);
2696 Changed = true;
2697
2698 // Users which otherwise aren't dominated by the replacement value must
2699 // be salvaged or deleted.
2700 } else if (!DT.dominates(&DomPoint, DII)) {
2701 UndefOrSalvage.insert(DII);
2702 }
2703 }
2704
2705 // DbgVariableRecord implementation of the above.
2706 for (auto *DVR : DPUsers) {
2707 Instruction *MarkedInstr = DVR->getMarker()->MarkedInstr;
2708 Instruction *NextNonDebug = MarkedInstr;
2709 // The next instruction might still be a dbg.declare, skip over it.
2710 if (isa<DbgVariableIntrinsic>(NextNonDebug))
2711 NextNonDebug = NextNonDebug->getNextNonDebugInstruction();
2712
2713 if (DomPointAfterFrom && NextNonDebug == &DomPoint) {
2714 LLVM_DEBUG(dbgs() << "MOVE: " << *DVR << '\n');
2715 DVR->removeFromParent();
2716 // Ensure there's a marker.
2717 DomPoint.getParent()->insertDbgRecordAfter(DVR, &DomPoint);
2718 Changed = true;
2719 } else if (!DT.dominates(&DomPoint, MarkedInstr)) {
2720 UndefOrSalvageDVR.insert(DVR);
2721 }
2722 }
2723 }
2724
2725 // Update debug users without use-before-def risk.
2726 for (auto *DII : Users) {
2727 if (UndefOrSalvage.count(DII))
2728 continue;
2729
2730 DbgValReplacement DVRepl = RewriteExpr(*DII);
2731 if (!DVRepl)
2732 continue;
2733
2734 DII->replaceVariableLocationOp(&From, &To);
2735 DII->setExpression(*DVRepl);
2736 LLVM_DEBUG(dbgs() << "REWRITE: " << *DII << '\n');
2737 Changed = true;
2738 }
2739 for (auto *DVR : DPUsers) {
2740 if (UndefOrSalvageDVR.count(DVR))
2741 continue;
2742
2743 DbgValReplacement DVRepl = RewriteDVRExpr(*DVR);
2744 if (!DVRepl)
2745 continue;
2746
2747 DVR->replaceVariableLocationOp(&From, &To);
2748 DVR->setExpression(*DVRepl);
2749 LLVM_DEBUG(dbgs() << "REWRITE: " << DVR << '\n');
2750 Changed = true;
2751 }
2752
2753 if (!UndefOrSalvage.empty() || !UndefOrSalvageDVR.empty()) {
2754 // Try to salvage the remaining debug users.
2756 Changed = true;
2757 }
2758
2759 return Changed;
2760}
2761
2762/// Check if a bitcast between a value of type \p FromTy to type \p ToTy would
2763/// losslessly preserve the bits and semantics of the value. This predicate is
2764/// symmetric, i.e swapping \p FromTy and \p ToTy should give the same result.
2765///
2766/// Note that Type::canLosslesslyBitCastTo is not suitable here because it
2767/// allows semantically unequivalent bitcasts, such as <2 x i64> -> <4 x i32>,
2768/// and also does not allow lossless pointer <-> integer conversions.
2770 Type *ToTy) {
2771 // Trivially compatible types.
2772 if (FromTy == ToTy)
2773 return true;
2774
2775 // Handle compatible pointer <-> integer conversions.
2776 if (FromTy->isIntOrPtrTy() && ToTy->isIntOrPtrTy()) {
2777 bool SameSize = DL.getTypeSizeInBits(FromTy) == DL.getTypeSizeInBits(ToTy);
2778 bool LosslessConversion = !DL.isNonIntegralPointerType(FromTy) &&
2779 !DL.isNonIntegralPointerType(ToTy);
2780 return SameSize && LosslessConversion;
2781 }
2782
2783 // TODO: This is not exhaustive.
2784 return false;
2785}
2786
2788 Instruction &DomPoint, DominatorTree &DT) {
2789 // Exit early if From has no debug users.
2790 if (!From.isUsedByMetadata())
2791 return false;
2792
2793 assert(&From != &To && "Can't replace something with itself");
2794
2795 Type *FromTy = From.getType();
2796 Type *ToTy = To.getType();
2797
2798 auto Identity = [&](DbgVariableIntrinsic &DII) -> DbgValReplacement {
2799 return DII.getExpression();
2800 };
2801 auto IdentityDVR = [&](DbgVariableRecord &DVR) -> DbgValReplacement {
2802 return DVR.getExpression();
2803 };
2804
2805 // Handle no-op conversions.
2806 Module &M = *From.getModule();
2807 const DataLayout &DL = M.getDataLayout();
2808 if (isBitCastSemanticsPreserving(DL, FromTy, ToTy))
2809 return rewriteDebugUsers(From, To, DomPoint, DT, Identity, IdentityDVR);
2810
2811 // Handle integer-to-integer widening and narrowing.
2812 // FIXME: Use DW_OP_convert when it's available everywhere.
2813 if (FromTy->isIntegerTy() && ToTy->isIntegerTy()) {
2814 uint64_t FromBits = FromTy->getPrimitiveSizeInBits();
2815 uint64_t ToBits = ToTy->getPrimitiveSizeInBits();
2816 assert(FromBits != ToBits && "Unexpected no-op conversion");
2817
2818 // When the width of the result grows, assume that a debugger will only
2819 // access the low `FromBits` bits when inspecting the source variable.
2820 if (FromBits < ToBits)
2821 return rewriteDebugUsers(From, To, DomPoint, DT, Identity, IdentityDVR);
2822
2823 // The width of the result has shrunk. Use sign/zero extension to describe
2824 // the source variable's high bits.
2825 auto SignOrZeroExt = [&](DbgVariableIntrinsic &DII) -> DbgValReplacement {
2826 DILocalVariable *Var = DII.getVariable();
2827
2828 // Without knowing signedness, sign/zero extension isn't possible.
2829 auto Signedness = Var->getSignedness();
2830 if (!Signedness)
2831 return std::nullopt;
2832
2833 bool Signed = *Signedness == DIBasicType::Signedness::Signed;
2834 return DIExpression::appendExt(DII.getExpression(), ToBits, FromBits,
2835 Signed);
2836 };
2837 // RemoveDIs: duplicate implementation working on DbgVariableRecords rather
2838 // than on dbg.value intrinsics.
2839 auto SignOrZeroExtDVR = [&](DbgVariableRecord &DVR) -> DbgValReplacement {
2840 DILocalVariable *Var = DVR.getVariable();
2841
2842 // Without knowing signedness, sign/zero extension isn't possible.
2843 auto Signedness = Var->getSignedness();
2844 if (!Signedness)
2845 return std::nullopt;
2846
2847 bool Signed = *Signedness == DIBasicType::Signedness::Signed;
2848 return DIExpression::appendExt(DVR.getExpression(), ToBits, FromBits,
2849 Signed);
2850 };
2851 return rewriteDebugUsers(From, To, DomPoint, DT, SignOrZeroExt,
2852 SignOrZeroExtDVR);
2853 }
2854
2855 // TODO: Floating-point conversions, vectors.
2856 return false;
2857}
2858
2860 Instruction *I, SmallVectorImpl<Value *> &PoisonedValues) {
2861 bool Changed = false;
2862 // RemoveDIs: erase debug-info on this instruction manually.
2863 I->dropDbgRecords();
2864 for (Use &U : I->operands()) {
2865 Value *Op = U.get();
2866 if (isa<Instruction>(Op) && !Op->getType()->isTokenTy()) {
2867 U.set(PoisonValue::get(Op->getType()));
2868 PoisonedValues.push_back(Op);
2869 Changed = true;
2870 }
2871 }
2872
2873 return Changed;
2874}
2875
2876std::pair<unsigned, unsigned>
2878 unsigned NumDeadInst = 0;
2879 unsigned NumDeadDbgInst = 0;
2880 // Delete the instructions backwards, as it has a reduced likelihood of
2881 // having to update as many def-use and use-def chains.
2882 Instruction *EndInst = BB->getTerminator(); // Last not to be deleted.
2885
2886 while (EndInst != &BB->front()) {
2887 // Delete the next to last instruction.
2888 Instruction *Inst = &*--EndInst->getIterator();
2889 if (!Inst->use_empty() && !Inst->getType()->isTokenTy())
2891 if (Inst->isEHPad() || Inst->getType()->isTokenTy()) {
2892 // EHPads can't have DbgVariableRecords attached to them, but it might be
2893 // possible for things with token type.
2894 Inst->dropDbgRecords();
2895 EndInst = Inst;
2896 continue;
2897 }
2898 if (isa<DbgInfoIntrinsic>(Inst))
2899 ++NumDeadDbgInst;
2900 else
2901 ++NumDeadInst;
2902 // RemoveDIs: erasing debug-info must be done manually.
2903 Inst->dropDbgRecords();
2904 Inst->eraseFromParent();
2905 }
2906 return {NumDeadInst, NumDeadDbgInst};
2907}
2908
2909unsigned llvm::changeToUnreachable(Instruction *I, bool PreserveLCSSA,
2910 DomTreeUpdater *DTU,
2911 MemorySSAUpdater *MSSAU) {
2912 BasicBlock *BB = I->getParent();
2913
2914 if (MSSAU)
2915 MSSAU->changeToUnreachable(I);
2916
2917 SmallSet<BasicBlock *, 8> UniqueSuccessors;
2918
2919 // Loop over all of the successors, removing BB's entry from any PHI
2920 // nodes.
2921 for (BasicBlock *Successor : successors(BB)) {
2922 Successor->removePredecessor(BB, PreserveLCSSA);
2923 if (DTU)
2924 UniqueSuccessors.insert(Successor);
2925 }
2926 auto *UI = new UnreachableInst(I->getContext(), I->getIterator());
2927 UI->setDebugLoc(I->getDebugLoc());
2928
2929 // All instructions after this are dead.
2930 unsigned NumInstrsRemoved = 0;
2931 BasicBlock::iterator BBI = I->getIterator(), BBE = BB->end();
2932 while (BBI != BBE) {
2933 if (!BBI->use_empty())
2934 BBI->replaceAllUsesWith(PoisonValue::get(BBI->getType()));
2935 BBI++->eraseFromParent();
2936 ++NumInstrsRemoved;
2937 }
2938 if (DTU) {
2940 Updates.reserve(UniqueSuccessors.size());
2941 for (BasicBlock *UniqueSuccessor : UniqueSuccessors)
2942 Updates.push_back({DominatorTree::Delete, BB, UniqueSuccessor});
2943 DTU->applyUpdates(Updates);
2944 }
2946 return NumInstrsRemoved;
2947}
2948
2950 SmallVector<Value *, 8> Args(II->args());
2952 II->getOperandBundlesAsDefs(OpBundles);
2953 CallInst *NewCall = CallInst::Create(II->getFunctionType(),
2954 II->getCalledOperand(), Args, OpBundles);
2955 NewCall->setCallingConv(II->getCallingConv());
2956 NewCall->setAttributes(II->getAttributes());
2957 NewCall->setDebugLoc(II->getDebugLoc());
2958 NewCall->copyMetadata(*II);
2959
2960 // If the invoke had profile metadata, try converting them for CallInst.
2961 uint64_t TotalWeight;
2962 if (NewCall->extractProfTotalWeight(TotalWeight)) {
2963 // Set the total weight if it fits into i32, otherwise reset.
2964 MDBuilder MDB(NewCall->getContext());
2965 auto NewWeights = uint32_t(TotalWeight) != TotalWeight
2966 ? nullptr
2967 : MDB.createBranchWeights({uint32_t(TotalWeight)});
2968 NewCall->setMetadata(LLVMContext::MD_prof, NewWeights);
2969 }
2970
2971 return NewCall;
2972}
2973
2974// changeToCall - Convert the specified invoke into a normal call.
2977 NewCall->takeName(II);
2978 NewCall->insertBefore(II);
2979 II->replaceAllUsesWith(NewCall);
2980
2981 // Follow the call by a branch to the normal destination.
2982 BasicBlock *NormalDestBB = II->getNormalDest();
2983 BranchInst::Create(NormalDestBB, II->getIterator());
2984
2985 // Update PHI nodes in the unwind destination
2986 BasicBlock *BB = II->getParent();
2987 BasicBlock *UnwindDestBB = II->getUnwindDest();
2988 UnwindDestBB->removePredecessor(BB);
2989 II->eraseFromParent();
2990 if (DTU)
2991 DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDestBB}});
2992 return NewCall;
2993}
2994
2996 BasicBlock *UnwindEdge,
2997 DomTreeUpdater *DTU) {
2998 BasicBlock *BB = CI->getParent();
2999
3000 // Convert this function call into an invoke instruction. First, split the
3001 // basic block.
3002 BasicBlock *Split = SplitBlock(BB, CI, DTU, /*LI=*/nullptr, /*MSSAU*/ nullptr,
3003 CI->getName() + ".noexc");
3004
3005 // Delete the unconditional branch inserted by SplitBlock
3006 BB->back().eraseFromParent();
3007
3008 // Create the new invoke instruction.
3009 SmallVector<Value *, 8> InvokeArgs(CI->args());
3011
3012 CI->getOperandBundlesAsDefs(OpBundles);
3013
3014 // Note: we're round tripping operand bundles through memory here, and that
3015 // can potentially be avoided with a cleverer API design that we do not have
3016 // as of this time.
3017
3018 InvokeInst *II =
3020 UnwindEdge, InvokeArgs, OpBundles, CI->getName(), BB);
3021 II->setDebugLoc(CI->getDebugLoc());
3022 II->setCallingConv(CI->getCallingConv());
3023 II->setAttributes(CI->getAttributes());
3024 II->setMetadata(LLVMContext::MD_prof, CI->getMetadata(LLVMContext::MD_prof));
3025
3026 if (DTU)
3027 DTU->applyUpdates({{DominatorTree::Insert, BB, UnwindEdge}});
3028
3029 // Make sure that anything using the call now uses the invoke! This also
3030 // updates the CallGraph if present, because it uses a WeakTrackingVH.
3032
3033 // Delete the original call
3034 Split->front().eraseFromParent();
3035 return Split;
3036}
3037
3040 DomTreeUpdater *DTU = nullptr) {
3042 BasicBlock *BB = &F.front();
3043 Worklist.push_back(BB);
3044 Reachable.insert(BB);
3045 bool Changed = false;
3046 do {
3047 BB = Worklist.pop_back_val();
3048
3049 // Do a quick scan of the basic block, turning any obviously unreachable
3050 // instructions into LLVM unreachable insts. The instruction combining pass
3051 // canonicalizes unreachable insts into stores to null or undef.
3052 for (Instruction &I : *BB) {
3053 if (auto *CI = dyn_cast<CallInst>(&I)) {
3054 Value *Callee = CI->getCalledOperand();
3055 // Handle intrinsic calls.
3056 if (Function *F = dyn_cast<Function>(Callee)) {
3057 auto IntrinsicID = F->getIntrinsicID();
3058 // Assumptions that are known to be false are equivalent to
3059 // unreachable. Also, if the condition is undefined, then we make the
3060 // choice most beneficial to the optimizer, and choose that to also be
3061 // unreachable.
3062 if (IntrinsicID == Intrinsic::assume) {
3063 if (match(CI->getArgOperand(0), m_CombineOr(m_Zero(), m_Undef()))) {
3064 // Don't insert a call to llvm.trap right before the unreachable.
3065 changeToUnreachable(CI, false, DTU);
3066 Changed = true;
3067 break;
3068 }
3069 } else if (IntrinsicID == Intrinsic::experimental_guard) {
3070 // A call to the guard intrinsic bails out of the current
3071 // compilation unit if the predicate passed to it is false. If the
3072 // predicate is a constant false, then we know the guard will bail
3073 // out of the current compile unconditionally, so all code following
3074 // it is dead.
3075 //
3076 // Note: unlike in llvm.assume, it is not "obviously profitable" for
3077 // guards to treat `undef` as `false` since a guard on `undef` can
3078 // still be useful for widening.
3079 if (match(CI->getArgOperand(0), m_Zero()))
3080 if (!isa<UnreachableInst>(CI->getNextNode())) {
3081 changeToUnreachable(CI->getNextNode(), false, DTU);
3082 Changed = true;
3083 break;
3084 }
3085 }
3086 } else if ((isa<ConstantPointerNull>(Callee) &&
3087 !NullPointerIsDefined(CI->getFunction(),
3088 cast<PointerType>(Callee->getType())
3089 ->getAddressSpace())) ||
3090 isa<UndefValue>(Callee)) {
3091 changeToUnreachable(CI, false, DTU);
3092 Changed = true;
3093 break;
3094 }
3095 if (CI->doesNotReturn() && !CI->isMustTailCall()) {
3096 // If we found a call to a no-return function, insert an unreachable
3097 // instruction after it. Make sure there isn't *already* one there
3098 // though.
3099 if (!isa<UnreachableInst>(CI->getNextNonDebugInstruction())) {
3100 // Don't insert a call to llvm.trap right before the unreachable.
3101 changeToUnreachable(CI->getNextNonDebugInstruction(), false, DTU);
3102 Changed = true;
3103 }
3104 break;
3105 }
3106 } else if (auto *SI = dyn_cast<StoreInst>(&I)) {
3107 // Store to undef and store to null are undefined and used to signal
3108 // that they should be changed to unreachable by passes that can't
3109 // modify the CFG.
3110
3111 // Don't touch volatile stores.
3112 if (SI->isVolatile()) continue;
3113
3114 Value *Ptr = SI->getOperand(1);
3115
3116 if (isa<UndefValue>(Ptr) ||
3117 (isa<ConstantPointerNull>(Ptr) &&
3118 !NullPointerIsDefined(SI->getFunction(),
3119 SI->getPointerAddressSpace()))) {
3120 changeToUnreachable(SI, false, DTU);
3121 Changed = true;
3122 break;
3123 }
3124 }
3125 }
3126
3127 Instruction *Terminator = BB->getTerminator();
3128 if (auto *II = dyn_cast<InvokeInst>(Terminator)) {
3129 // Turn invokes that call 'nounwind' functions into ordinary calls.
3130 Value *Callee = II->getCalledOperand();
3131 if ((isa<ConstantPointerNull>(Callee) &&
3132 !NullPointerIsDefined(BB->getParent())) ||
3133 isa<UndefValue>(Callee)) {
3134 changeToUnreachable(II, false, DTU);
3135 Changed = true;
3136 } else {
3137 if (II->doesNotReturn() &&
3138 !isa<UnreachableInst>(II->getNormalDest()->front())) {
3139 // If we found an invoke of a no-return function,
3140 // create a new empty basic block with an `unreachable` terminator,
3141 // and set it as the normal destination for the invoke,
3142 // unless that is already the case.
3143 // Note that the original normal destination could have other uses.
3144 BasicBlock *OrigNormalDest = II->getNormalDest();
3145 OrigNormalDest->removePredecessor(II->getParent());
3146 LLVMContext &Ctx = II->getContext();
3147 BasicBlock *UnreachableNormalDest = BasicBlock::Create(
3148 Ctx, OrigNormalDest->getName() + ".unreachable",
3149 II->getFunction(), OrigNormalDest);
3150 new UnreachableInst(Ctx, UnreachableNormalDest);
3151 II->setNormalDest(UnreachableNormalDest);
3152 if (DTU)
3153 DTU->applyUpdates(
3154 {{DominatorTree::Delete, BB, OrigNormalDest},
3155 {DominatorTree::Insert, BB, UnreachableNormalDest}});
3156 Changed = true;
3157 }
3158 if (II->doesNotThrow() && canSimplifyInvokeNoUnwind(&F)) {
3159 if (II->use_empty() && !II->mayHaveSideEffects()) {
3160 // jump to the normal destination branch.
3161 BasicBlock *NormalDestBB = II->getNormalDest();
3162 BasicBlock *UnwindDestBB = II->getUnwindDest();
3163 BranchInst::Create(NormalDestBB, II->getIterator());
3164 UnwindDestBB->removePredecessor(II->getParent());
3165 II->eraseFromParent();
3166 if (DTU)
3167 DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDestBB}});
3168 } else
3169 changeToCall(II, DTU);
3170 Changed = true;
3171 }
3172 }
3173 } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Terminator)) {
3174 // Remove catchpads which cannot be reached.
3175 struct CatchPadDenseMapInfo {
3176 static CatchPadInst *getEmptyKey() {
3178 }
3179
3180 static CatchPadInst *getTombstoneKey() {
3182 }
3183
3184 static unsigned getHashValue(CatchPadInst *CatchPad) {
3185 return static_cast<unsigned>(hash_combine_range(
3186 CatchPad->value_op_begin(), CatchPad->value_op_end()));
3187 }
3188
3189 static bool isEqual(CatchPadInst *LHS, CatchPadInst *RHS) {
3190 if (LHS == getEmptyKey() || LHS == getTombstoneKey() ||
3191 RHS == getEmptyKey() || RHS == getTombstoneKey())
3192 return LHS == RHS;
3193 return LHS->isIdenticalTo(RHS);
3194 }
3195 };
3196
3197 SmallDenseMap<BasicBlock *, int, 8> NumPerSuccessorCases;
3198 // Set of unique CatchPads.
3200 CatchPadDenseMapInfo, detail::DenseSetPair<CatchPadInst *>>
3201 HandlerSet;
3203 for (CatchSwitchInst::handler_iterator I = CatchSwitch->handler_begin(),
3204 E = CatchSwitch->handler_end();
3205 I != E; ++I) {
3206 BasicBlock *HandlerBB = *I;
3207 if (DTU)
3208 ++NumPerSuccessorCases[HandlerBB];
3209 auto *CatchPad = cast<CatchPadInst>(HandlerBB->getFirstNonPHI());
3210 if (!HandlerSet.insert({CatchPad, Empty}).second) {
3211 if (DTU)
3212 --NumPerSuccessorCases[HandlerBB];
3213 CatchSwitch->removeHandler(I);
3214 --I;
3215 --E;
3216 Changed = true;
3217 }
3218 }
3219 if (DTU) {
3220 std::vector<DominatorTree::UpdateType> Updates;
3221 for (const std::pair<BasicBlock *, int> &I : NumPerSuccessorCases)
3222 if (I.second == 0)
3223 Updates.push_back({DominatorTree::Delete, BB, I.first});
3224 DTU->applyUpdates(Updates);
3225 }
3226 }
3227
3228 Changed |= ConstantFoldTerminator(BB, true, nullptr, DTU);
3229 for (BasicBlock *Successor : successors(BB))
3230 if (Reachable.insert(Successor).second)
3231 Worklist.push_back(Successor);
3232 } while (!Worklist.empty());
3233 return Changed;
3234}
3235
3237 Instruction *TI = BB->getTerminator();
3238
3239 if (auto *II = dyn_cast<InvokeInst>(TI))
3240 return changeToCall(II, DTU);
3241
3242 Instruction *NewTI;
3243 BasicBlock *UnwindDest;
3244
3245 if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) {
3246 NewTI = CleanupReturnInst::Create(CRI->getCleanupPad(), nullptr, CRI->getIterator());
3247 UnwindDest = CRI->getUnwindDest();
3248 } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(TI)) {
3249 auto *NewCatchSwitch = CatchSwitchInst::Create(
3250 CatchSwitch->getParentPad(), nullptr, CatchSwitch->getNumHandlers(),
3251 CatchSwitch->getName(), CatchSwitch->getIterator());
3252 for (BasicBlock *PadBB : CatchSwitch->handlers())
3253 NewCatchSwitch->addHandler(PadBB);
3254
3255 NewTI = NewCatchSwitch;
3256 UnwindDest = CatchSwitch->getUnwindDest();
3257 } else {
3258 llvm_unreachable("Could not find unwind successor");
3259 }
3260
3261 NewTI->takeName(TI);
3262 NewTI->setDebugLoc(TI->getDebugLoc());
3263 UnwindDest->removePredecessor(BB);
3264 TI->replaceAllUsesWith(NewTI);
3265 TI->eraseFromParent();
3266 if (DTU)
3267 DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDest}});
3268 return NewTI;
3269}
3270
3271/// removeUnreachableBlocks - Remove blocks that are not reachable, even
3272/// if they are in a dead cycle. Return true if a change was made, false
3273/// otherwise.
3275 MemorySSAUpdater *MSSAU) {
3277 bool Changed = markAliveBlocks(F, Reachable, DTU);
3278
3279 // If there are unreachable blocks in the CFG...
3280 if (Reachable.size() == F.size())
3281 return Changed;
3282
3283 assert(Reachable.size() < F.size());
3284
3285 // Are there any blocks left to actually delete?
3286 SmallSetVector<BasicBlock *, 8> BlocksToRemove;
3287 for (BasicBlock &BB : F) {
3288 // Skip reachable basic blocks
3289 if (Reachable.count(&BB))
3290 continue;
3291 // Skip already-deleted blocks
3292 if (DTU && DTU->isBBPendingDeletion(&BB))
3293 continue;
3294 BlocksToRemove.insert(&BB);
3295 }
3296
3297 if (BlocksToRemove.empty())
3298 return Changed;
3299
3300 Changed = true;
3301 NumRemoved += BlocksToRemove.size();
3302
3303 if (MSSAU)
3304 MSSAU->removeBlocks(BlocksToRemove);
3305
3306 DeleteDeadBlocks(BlocksToRemove.takeVector(), DTU);
3307
3308 return Changed;
3309}
3310
3312 ArrayRef<unsigned> KnownIDs, bool DoesKMove) {
3314 K->dropUnknownNonDebugMetadata(KnownIDs);
3315 K->getAllMetadataOtherThanDebugLoc(Metadata);
3316 for (const auto &MD : Metadata) {
3317 unsigned Kind = MD.first;
3318 MDNode *JMD = J->getMetadata(Kind);
3319 MDNode *KMD = MD.second;
3320
3321 switch (Kind) {
3322 default:
3323 K->setMetadata(Kind, nullptr); // Remove unknown metadata
3324 break;
3325 case LLVMContext::MD_dbg:
3326 llvm_unreachable("getAllMetadataOtherThanDebugLoc returned a MD_dbg");
3327 case LLVMContext::MD_DIAssignID:
3328 K->mergeDIAssignID(J);
3329 break;
3330 case LLVMContext::MD_tbaa:
3331 if (DoesKMove)
3332 K->setMetadata(Kind, MDNode::getMostGenericTBAA(JMD, KMD));
3333 break;
3334 case LLVMContext::MD_alias_scope:
3335 if (DoesKMove)
3336 K->setMetadata(Kind, MDNode::getMostGenericAliasScope(JMD, KMD));
3337 break;
3338 case LLVMContext::MD_noalias:
3339 case LLVMContext::MD_mem_parallel_loop_access:
3340 if (DoesKMove)
3341 K->setMetadata(Kind, MDNode::intersect(JMD, KMD));
3342 break;
3343 case LLVMContext::MD_access_group:
3344 if (DoesKMove)
3345 K->setMetadata(LLVMContext::MD_access_group,
3346 intersectAccessGroups(K, J));
3347 break;
3348 case LLVMContext::MD_range:
3349 if (DoesKMove || !K->hasMetadata(LLVMContext::MD_noundef))
3350 K->setMetadata(Kind, MDNode::getMostGenericRange(JMD, KMD));
3351 break;
3352 case LLVMContext::MD_fpmath:
3353 K->setMetadata(Kind, MDNode::getMostGenericFPMath(JMD, KMD));
3354 break;
3355 case LLVMContext::MD_invariant_load:
3356 // If K moves, only set the !invariant.load if it is present in both
3357 // instructions.
3358 if (DoesKMove)
3359 K->setMetadata(Kind, JMD);
3360 break;
3361 case LLVMContext::MD_nonnull:
3362 if (DoesKMove || !K->hasMetadata(LLVMContext::MD_noundef))
3363 K->setMetadata(Kind, JMD);
3364 break;
3365 case LLVMContext::MD_invariant_group:
3366 // Preserve !invariant.group in K.
3367 break;
3368 case LLVMContext::MD_mmra:
3369 // Combine MMRAs
3370 break;
3371 case LLVMContext::MD_align:
3372 if (DoesKMove || !K->hasMetadata(LLVMContext::MD_noundef))
3373 K->setMetadata(
3375 break;
3376 case LLVMContext::MD_dereferenceable:
3377 case LLVMContext::MD_dereferenceable_or_null:
3378 if (DoesKMove)
3379 K->setMetadata(Kind,
3381 break;
3382 case LLVMContext::MD_preserve_access_index:
3383 // Preserve !preserve.access.index in K.
3384 break;
3385 case LLVMContext::MD_noundef:
3386 // If K does move, keep noundef if it is present in both instructions.
3387 if (DoesKMove)
3388 K->setMetadata(Kind, JMD);
3389 break;
3390 case LLVMContext::MD_nontemporal:
3391 // Preserve !nontemporal if it is present on both instructions.
3392 K->setMetadata(Kind, JMD);
3393 break;
3394 case LLVMContext::MD_prof:
3395 if (DoesKMove)
3396 K->setMetadata(Kind, MDNode::getMergedProfMetadata(KMD, JMD, K, J));
3397 break;
3398 case LLVMContext::MD_noalias_addrspace:
3399 if (DoesKMove)
3400 K->setMetadata(Kind,
3402 break;
3403 }
3404 }
3405 // Set !invariant.group from J if J has it. If both instructions have it
3406 // then we will just pick it from J - even when they are different.
3407 // Also make sure that K is load or store - f.e. combining bitcast with load
3408 // could produce bitcast with invariant.group metadata, which is invalid.
3409 // FIXME: we should try to preserve both invariant.group md if they are
3410 // different, but right now instruction can only have one invariant.group.
3411 if (auto *JMD = J->getMetadata(LLVMContext::MD_invariant_group))
3412 if (isa<LoadInst>(K) || isa<StoreInst>(K))
3413 K->setMetadata(LLVMContext::MD_invariant_group, JMD);
3414
3415 // Merge MMRAs.
3416 // This is handled separately because we also want to handle cases where K
3417 // doesn't have tags but J does.
3418 auto JMMRA = J->getMetadata(LLVMContext::MD_mmra);
3419 auto KMMRA = K->getMetadata(LLVMContext::MD_mmra);
3420 if (JMMRA || KMMRA) {
3421 K->setMetadata(LLVMContext::MD_mmra,
3422 MMRAMetadata::combine(K->getContext(), JMMRA, KMMRA));
3423 }
3424}
3425
3427 bool KDominatesJ) {
3428 unsigned KnownIDs[] = {LLVMContext::MD_tbaa,
3429 LLVMContext::MD_alias_scope,
3430 LLVMContext::MD_noalias,
3431 LLVMContext::MD_range,
3432 LLVMContext::MD_fpmath,
3433 LLVMContext::MD_invariant_load,
3434 LLVMContext::MD_nonnull,
3435 LLVMContext::MD_invariant_group,
3436 LLVMContext::MD_align,
3437 LLVMContext::MD_dereferenceable,
3438 LLVMContext::MD_dereferenceable_or_null,
3439 LLVMContext::MD_access_group,
3440 LLVMContext::MD_preserve_access_index,
3441 LLVMContext::MD_prof,
3442 LLVMContext::MD_nontemporal,
3443 LLVMContext::MD_noundef,
3444 LLVMContext::MD_mmra,
3445 LLVMContext::MD_noalias_addrspace};
3446 combineMetadata(K, J, KnownIDs, KDominatesJ);
3447}
3448
3449void llvm::copyMetadataForLoad(LoadInst &Dest, const LoadInst &Source) {
3451 Source.getAllMetadata(MD);
3452 MDBuilder MDB(Dest.getContext());
3453 Type *NewType = Dest.getType();
3454 const DataLayout &DL = Source.getDataLayout();
3455 for (const auto &MDPair : MD) {
3456 unsigned ID = MDPair.first;
3457 MDNode *N = MDPair.second;
3458 // Note, essentially every kind of metadata should be preserved here! This
3459 // routine is supposed to clone a load instruction changing *only its type*.
3460 // The only metadata it makes sense to drop is metadata which is invalidated
3461 // when the pointer type changes. This should essentially never be the case
3462 // in LLVM, but we explicitly switch over only known metadata to be
3463 // conservatively correct. If you are adding metadata to LLVM which pertains
3464 // to loads, you almost certainly want to add it here.
3465 switch (ID) {
3466 case LLVMContext::MD_dbg:
3467 case LLVMContext::MD_tbaa:
3468 case LLVMContext::MD_prof:
3469 case LLVMContext::MD_fpmath:
3470 case LLVMContext::MD_tbaa_struct:
3471 case LLVMContext::MD_invariant_load:
3472 case LLVMContext::MD_alias_scope:
3473 case LLVMContext::MD_noalias:
3474 case LLVMContext::MD_nontemporal:
3475 case LLVMContext::MD_mem_parallel_loop_access:
3476 case LLVMContext::MD_access_group:
3477 case LLVMContext::MD_noundef:
3478 // All of these directly apply.
3479 Dest.setMetadata(ID, N);
3480 break;
3481
3482 case LLVMContext::MD_nonnull:
3483 copyNonnullMetadata(Source, N, Dest);
3484 break;
3485
3486 case LLVMContext::MD_align:
3487 case LLVMContext::MD_dereferenceable:
3488 case LLVMContext::MD_dereferenceable_or_null:
3489 // These only directly apply if the new type is also a pointer.
3490 if (NewType->isPointerTy())
3491 Dest.setMetadata(ID, N);
3492 break;
3493
3494 case LLVMContext::MD_range:
3495 copyRangeMetadata(DL, Source, N, Dest);
3496 break;
3497 }
3498 }
3499}
3500
3502 auto *ReplInst = dyn_cast<Instruction>(Repl);
3503 if (!ReplInst)
3504 return;
3505
3506 // Patch the replacement so that it is not more restrictive than the value
3507 // being replaced.
3508 WithOverflowInst *UnusedWO;
3509 // When replacing the result of a llvm.*.with.overflow intrinsic with a
3510 // overflowing binary operator, nuw/nsw flags may no longer hold.
3511 if (isa<OverflowingBinaryOperator>(ReplInst) &&
3512 match(I, m_ExtractValue<0>(m_WithOverflowInst(UnusedWO))))
3513 ReplInst->dropPoisonGeneratingFlags();
3514 // Note that if 'I' is a load being replaced by some operation,
3515 // for example, by an arithmetic operation, then andIRFlags()
3516 // would just erase all math flags from the original arithmetic
3517 // operation, which is clearly not wanted and not needed.
3518 else if (!isa<LoadInst>(I))
3519 ReplInst->andIRFlags(I);
3520
3521 // Handle attributes.
3522 if (auto *CB1 = dyn_cast<CallBase>(ReplInst)) {
3523 if (auto *CB2 = dyn_cast<CallBase>(I)) {
3524 bool Success = CB1->tryIntersectAttributes(CB2);
3525 assert(Success && "We should not be trying to sink callbases "
3526 "with non-intersectable attributes");
3527 // For NDEBUG Compile.
3528 (void)Success;
3529 }
3530 }
3531
3532 // FIXME: If both the original and replacement value are part of the
3533 // same control-flow region (meaning that the execution of one
3534 // guarantees the execution of the other), then we can combine the
3535 // noalias scopes here and do better than the general conservative
3536 // answer used in combineMetadata().
3537
3538 // In general, GVN unifies expressions over different control-flow
3539 // regions, and so we need a conservative combination of the noalias
3540 // scopes.
3541 combineMetadataForCSE(ReplInst, I, false);
3542}
3543
3544template <typename RootType, typename ShouldReplaceFn>
3546 const RootType &Root,
3547 const ShouldReplaceFn &ShouldReplace) {
3548 assert(From->getType() == To->getType());
3549
3550 unsigned Count = 0;
3551 for (Use &U : llvm::make_early_inc_range(From->uses())) {
3552 auto *II = dyn_cast<IntrinsicInst>(U.getUser());
3553 if (II && II->getIntrinsicID() == Intrinsic::fake_use)
3554 continue;
3555 if (!ShouldReplace(Root, U))
3556 continue;
3557 LLVM_DEBUG(dbgs() << "Replace dominated use of '";
3558 From->printAsOperand(dbgs());
3559 dbgs() << "' with " << *To << " in " << *U.getUser() << "\n");
3560 U.set(To);
3561 ++Count;
3562 }
3563 return Count;
3564}
3565
3567 assert(From->getType() == To->getType());
3568 auto *BB = From->getParent();
3569 unsigned Count = 0;
3570
3571 for (Use &U : llvm::make_early_inc_range(From->uses())) {
3572 auto *I = cast<Instruction>(U.getUser());
3573 if (I->getParent() == BB)
3574 continue;
3575 U.set(To);
3576 ++Count;
3577 }
3578 return Count;
3579}
3580
3582 DominatorTree &DT,
3583 const BasicBlockEdge &Root) {
3584 auto Dominates = [&DT](const BasicBlockEdge &Root, const Use &U) {
3585 return DT.dominates(Root, U);
3586 };
3587 return ::replaceDominatedUsesWith(From, To, Root, Dominates);
3588}
3589
3591 DominatorTree &DT,
3592 const BasicBlock *BB) {
3593 auto Dominates = [&DT](const BasicBlock *BB, const Use &U) {
3594 return DT.dominates(BB, U);
3595 };
3596 return ::replaceDominatedUsesWith(From, To, BB, Dominates);
3597}
3598
3600 Value *From, Value *To, DominatorTree &DT, const BasicBlockEdge &Root,
3601 function_ref<bool(const Use &U, const Value *To)> ShouldReplace) {
3602 auto DominatesAndShouldReplace =
3603 [&DT, &ShouldReplace, To](const BasicBlockEdge &Root, const Use &U) {
3604 return DT.dominates(Root, U) && ShouldReplace(U, To);
3605 };
3606 return ::replaceDominatedUsesWith(From, To, Root, DominatesAndShouldReplace);
3607}
3608
3610 Value *From, Value *To, DominatorTree &DT, const BasicBlock *BB,
3611 function_ref<bool(const Use &U, const Value *To)> ShouldReplace) {
3612 auto DominatesAndShouldReplace = [&DT, &ShouldReplace,
3613 To](const BasicBlock *BB, const Use &U) {
3614 return DT.dominates(BB, U) && ShouldReplace(U, To);
3615 };
3616 return ::replaceDominatedUsesWith(From, To, BB, DominatesAndShouldReplace);
3617}
3618
3620 const TargetLibraryInfo &TLI) {
3621 // Check if the function is specifically marked as a gc leaf function.
3622 if (Call->hasFnAttr("gc-leaf-function"))
3623 return true;
3624 if (const Function *F = Call->getCalledFunction()) {
3625 if (F->hasFnAttribute("gc-leaf-function"))
3626 return true;
3627
3628 if (auto IID = F->getIntrinsicID()) {
3629 // Most LLVM intrinsics do not take safepoints.
3630 return IID != Intrinsic::experimental_gc_statepoint &&
3631 IID != Intrinsic::experimental_deoptimize &&
3632 IID != Intrinsic::memcpy_element_unordered_atomic &&
3633 IID != Intrinsic::memmove_element_unordered_atomic;
3634 }
3635 }
3636
3637 // Lib calls can be materialized by some passes, and won't be
3638 // marked as 'gc-leaf-function.' All available Libcalls are
3639 // GC-leaf.
3640 LibFunc LF;
3641 if (TLI.getLibFunc(*Call, LF)) {
3642 return TLI.has(LF);
3643 }
3644
3645 return false;
3646}
3647
3649 LoadInst &NewLI) {
3650 auto *NewTy = NewLI.getType();
3651
3652 // This only directly applies if the new type is also a pointer.
3653 if (NewTy->isPointerTy()) {
3654 NewLI.setMetadata(LLVMContext::MD_nonnull, N);
3655 return;
3656 }
3657
3658 // The only other translation we can do is to integral loads with !range
3659 // metadata.
3660 if (!NewTy->isIntegerTy())
3661 return;
3662
3663 MDBuilder MDB(NewLI.getContext());
3664 const Value *Ptr = OldLI.getPointerOperand();
3665 auto *ITy = cast<IntegerType>(NewTy);
3666 auto *NullInt = ConstantExpr::getPtrToInt(
3667 ConstantPointerNull::get(cast<PointerType>(Ptr->getType())), ITy);
3668 auto *NonNullInt = ConstantExpr::getAdd(NullInt, ConstantInt::get(ITy, 1));
3669 NewLI.setMetadata(LLVMContext::MD_range,
3670 MDB.createRange(NonNullInt, NullInt));
3671}
3672
3674 MDNode *N, LoadInst &NewLI) {
3675 auto *NewTy = NewLI.getType();
3676 // Simply copy the metadata if the type did not change.
3677 if (NewTy == OldLI.getType()) {
3678 NewLI.setMetadata(LLVMContext::MD_range, N);
3679 return;
3680 }
3681
3682 // Give up unless it is converted to a pointer where there is a single very
3683 // valuable mapping we can do reliably.
3684 // FIXME: It would be nice to propagate this in more ways, but the type
3685 // conversions make it hard.
3686 if (!NewTy->isPointerTy())
3687 return;
3688
3689 unsigned BitWidth = DL.getPointerTypeSizeInBits(NewTy);
3690 if (BitWidth == OldLI.getType()->getScalarSizeInBits() &&
3692 MDNode *NN = MDNode::get(OldLI.getContext(), {});
3693 NewLI.setMetadata(LLVMContext::MD_nonnull, NN);
3694 }
3695}
3696
3700 findDbgUsers(DbgUsers, &I, &DPUsers);
3701 for (auto *DII : DbgUsers)
3702 DII->eraseFromParent();
3703 for (auto *DVR : DPUsers)
3704 DVR->eraseFromParent();
3705}
3706
3708 BasicBlock *BB) {
3709 // Since we are moving the instructions out of its basic block, we do not
3710 // retain their original debug locations (DILocations) and debug intrinsic
3711 // instructions.
3712 //
3713 // Doing so would degrade the debugging experience and adversely affect the
3714 // accuracy of profiling information.
3715 //
3716 // Currently, when hoisting the instructions, we take the following actions:
3717 // - Remove their debug intrinsic instructions.
3718 // - Set their debug locations to the values from the insertion point.
3719 //
3720 // As per PR39141 (comment #8), the more fundamental reason why the dbg.values
3721 // need to be deleted, is because there will not be any instructions with a
3722 // DILocation in either branch left after performing the transformation. We
3723 // can only insert a dbg.value after the two branches are joined again.
3724 //
3725 // See PR38762, PR39243 for more details.
3726 //
3727 // TODO: Extend llvm.dbg.value to take more than one SSA Value (PR39141) to
3728 // encode predicated DIExpressions that yield different results on different
3729 // code paths.
3730
3731 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;) {
3732 Instruction *I = &*II;
3733 I->dropUBImplyingAttrsAndMetadata();
3734 if (I->isUsedByMetadata())
3735 dropDebugUsers(*I);
3736 // RemoveDIs: drop debug-info too as the following code does.
3737 I->dropDbgRecords();
3738 if (I->isDebugOrPseudoInst()) {
3739 // Remove DbgInfo and pseudo probe Intrinsics.
3740 II = I->eraseFromParent();
3741 continue;
3742 }
3743 I->setDebugLoc(InsertPt->getDebugLoc());
3744 ++II;
3745 }
3746 DomBlock->splice(InsertPt->getIterator(), BB, BB->begin(),
3747 BB->getTerminator()->getIterator());
3748}
3749
3751 Type &Ty) {
3752 // Create integer constant expression.
3753 auto createIntegerExpression = [&DIB](const Constant &CV) -> DIExpression * {
3754 const APInt &API = cast<ConstantInt>(&CV)->getValue();
3755 std::optional<int64_t> InitIntOpt = API.trySExtValue();
3756 return InitIntOpt ? DIB.createConstantValueExpression(
3757 static_cast<uint64_t>(*InitIntOpt))
3758 : nullptr;
3759 };
3760
3761 if (isa<ConstantInt>(C))
3762 return createIntegerExpression(C);
3763
3764 auto *FP = dyn_cast<ConstantFP>(&C);
3765 if (FP && Ty.isFloatingPointTy() && Ty.getScalarSizeInBits() <= 64) {
3766 const APFloat &APF = FP->getValueAPF();
3767 APInt const &API = APF.bitcastToAPInt();
3768 if (auto Temp = API.getZExtValue())
3769 return DIB.createConstantValueExpression(static_cast<uint64_t>(Temp));
3770 return DIB.createConstantValueExpression(*API.getRawData());
3771 }
3772
3773 if (!Ty.isPointerTy())
3774 return nullptr;
3775
3776 if (isa<ConstantPointerNull>(C))
3777 return DIB.createConstantValueExpression(0);
3778
3779 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(&C))
3780 if (CE->getOpcode() == Instruction::IntToPtr) {
3781 const Value *V = CE->getOperand(0);
3782 if (auto CI = dyn_cast_or_null<ConstantInt>(V))
3783 return createIntegerExpression(*CI);
3784 }
3785 return nullptr;
3786}
3787
3789 auto RemapDebugOperands = [&Mapping](auto *DV, auto Set) {
3790 for (auto *Op : Set) {
3791 auto I = Mapping.find(Op);
3792 if (I != Mapping.end())
3793 DV->replaceVariableLocationOp(Op, I->second, /*AllowEmpty=*/true);
3794 }
3795 };
3796 auto RemapAssignAddress = [&Mapping](auto *DA) {
3797 auto I = Mapping.find(DA->getAddress());
3798 if (I != Mapping.end())
3799 DA->setAddress(I->second);
3800 };
3801 if (auto DVI = dyn_cast<DbgVariableIntrinsic>(Inst))
3802 RemapDebugOperands(DVI, DVI->location_ops());
3803 if (auto DAI = dyn_cast<DbgAssignIntrinsic>(Inst))
3804 RemapAssignAddress(DAI);
3805 for (DbgVariableRecord &DVR : filterDbgVars(Inst->getDbgRecordRange())) {
3806 RemapDebugOperands(&DVR, DVR.location_ops());
3807 if (DVR.isDbgAssign())
3808 RemapAssignAddress(&DVR);
3809 }
3810}
3811
3812namespace {
3813
3814/// A potential constituent of a bitreverse or bswap expression. See
3815/// collectBitParts for a fuller explanation.
3816struct BitPart {
3817 BitPart(Value *P, unsigned BW) : Provider(P) {
3818 Provenance.resize(BW);
3819 }
3820
3821 /// The Value that this is a bitreverse/bswap of.
3822 Value *Provider;
3823
3824 /// The "provenance" of each bit. Provenance[A] = B means that bit A
3825 /// in Provider becomes bit B in the result of this expression.
3826 SmallVector<int8_t, 32> Provenance; // int8_t means max size is i128.
3827
3828 enum { Unset = -1 };
3829};
3830
3831} // end anonymous namespace
3832
3833/// Analyze the specified subexpression and see if it is capable of providing
3834/// pieces of a bswap or bitreverse. The subexpression provides a potential
3835/// piece of a bswap or bitreverse if it can be proved that each non-zero bit in
3836/// the output of the expression came from a corresponding bit in some other
3837/// value. This function is recursive, and the end result is a mapping of
3838/// bitnumber to bitnumber. It is the caller's responsibility to validate that
3839/// the bitnumber to bitnumber mapping is correct for a bswap or bitreverse.
3840///
3841/// For example, if the current subexpression if "(shl i32 %X, 24)" then we know
3842/// that the expression deposits the low byte of %X into the high byte of the
3843/// result and that all other bits are zero. This expression is accepted and a
3844/// BitPart is returned with Provider set to %X and Provenance[24-31] set to
3845/// [0-7].
3846///
3847/// For vector types, all analysis is performed at the per-element level. No
3848/// cross-element analysis is supported (shuffle/insertion/reduction), and all
3849/// constant masks must be splatted across all elements.
3850///
3851/// To avoid revisiting values, the BitPart results are memoized into the
3852/// provided map. To avoid unnecessary copying of BitParts, BitParts are
3853/// constructed in-place in the \c BPS map. Because of this \c BPS needs to
3854/// store BitParts objects, not pointers. As we need the concept of a nullptr
3855/// BitParts (Value has been analyzed and the analysis failed), we an Optional
3856/// type instead to provide the same functionality.
3857///
3858/// Because we pass around references into \c BPS, we must use a container that
3859/// does not invalidate internal references (std::map instead of DenseMap).
3860static const std::optional<BitPart> &
3861collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals,
3862 std::map<Value *, std::optional<BitPart>> &BPS, int Depth,
3863 bool &FoundRoot) {
3864 auto [I, Inserted] = BPS.try_emplace(V);
3865 if (!Inserted)
3866 return I->second;
3867
3868 auto &Result = I->second;
3869 auto BitWidth = V->getType()->getScalarSizeInBits();
3870
3871 // Can't do integer/elements > 128 bits.
3872 if (BitWidth > 128)
3873 return Result;
3874
3875 // Prevent stack overflow by limiting the recursion depth
3877 LLVM_DEBUG(dbgs() << "collectBitParts max recursion depth reached.\n");
3878 return Result;
3879 }
3880
3881 if (auto *I = dyn_cast<Instruction>(V)) {
3882 Value *X, *Y;
3883 const APInt *C;
3884
3885 // If this is an or instruction, it may be an inner node of the bswap.
3886 if (match(V, m_Or(m_Value(X), m_Value(Y)))) {
3887 // Check we have both sources and they are from the same provider.
3888 const auto &A = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
3889 Depth + 1, FoundRoot);
3890 if (!A || !A->Provider)
3891 return Result;
3892
3893 const auto &B = collectBitParts(Y, MatchBSwaps, MatchBitReversals, BPS,
3894 Depth + 1, FoundRoot);
3895 if (!B || A->Provider != B->Provider)
3896 return Result;
3897
3898 // Try and merge the two together.
3899 Result = BitPart(A->Provider, BitWidth);
3900 for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx) {
3901 if (A->Provenance[BitIdx] != BitPart::Unset &&
3902 B->Provenance[BitIdx] != BitPart::Unset &&
3903 A->Provenance[BitIdx] != B->Provenance[BitIdx])
3904 return Result = std::nullopt;
3905
3906 if (A->Provenance[BitIdx] == BitPart::Unset)
3907 Result->Provenance[BitIdx] = B->Provenance[BitIdx];
3908 else
3909 Result->Provenance[BitIdx] = A->Provenance[BitIdx];
3910 }
3911
3912 return Result;
3913 }
3914
3915 // If this is a logical shift by a constant, recurse then shift the result.
3916 if (match(V, m_LogicalShift(m_Value(X), m_APInt(C)))) {
3917 const APInt &BitShift = *C;
3918
3919 // Ensure the shift amount is defined.
3920 if (BitShift.uge(BitWidth))
3921 return Result;
3922
3923 // For bswap-only, limit shift amounts to whole bytes, for an early exit.
3924 if (!MatchBitReversals && (BitShift.getZExtValue() % 8) != 0)
3925 return Result;
3926
3927 const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
3928 Depth + 1, FoundRoot);
3929 if (!Res)
3930 return Result;
3931 Result = Res;
3932
3933 // Perform the "shift" on BitProvenance.
3934 auto &P = Result->Provenance;
3935 if (I->getOpcode() == Instruction::Shl) {
3936 P.erase(std::prev(P.end(), BitShift.getZExtValue()), P.end());
3937 P.insert(P.begin(), BitShift.getZExtValue(), BitPart::Unset);
3938 } else {
3939 P.erase(P.begin(), std::next(P.begin(), BitShift.getZExtValue()));
3940 P.insert(P.end(), BitShift.getZExtValue(), BitPart::Unset);
3941 }
3942
3943 return Result;
3944 }
3945
3946 // If this is a logical 'and' with a mask that clears bits, recurse then
3947 // unset the appropriate bits.
3948 if (match(V, m_And(m_Value(X), m_APInt(C)))) {
3949 const APInt &AndMask = *C;
3950
3951 // Check that the mask allows a multiple of 8 bits for a bswap, for an
3952 // early exit.
3953 unsigned NumMaskedBits = AndMask.popcount();
3954 if (!MatchBitReversals && (NumMaskedBits % 8) != 0)
3955 return Result;
3956
3957 const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
3958 Depth + 1, FoundRoot);
3959 if (!Res)
3960 return Result;
3961 Result = Res;
3962
3963 for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx)
3964 // If the AndMask is zero for this bit, clear the bit.
3965 if (AndMask[BitIdx] == 0)
3966 Result->Provenance[BitIdx] = BitPart::Unset;
3967 return Result;
3968 }
3969
3970 // If this is a zext instruction zero extend the result.
3971 if (match(V, m_ZExt(m_Value(X)))) {
3972 const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
3973 Depth + 1, FoundRoot);
3974 if (!Res)
3975 return Result;
3976
3977 Result = BitPart(Res->Provider, BitWidth);
3978 auto NarrowBitWidth = X->getType()->getScalarSizeInBits();
3979 for (unsigned BitIdx = 0; BitIdx < NarrowBitWidth; ++BitIdx)
3980 Result->Provenance[BitIdx] = Res->Provenance[BitIdx];
3981 for (unsigned BitIdx = NarrowBitWidth; BitIdx < BitWidth; ++BitIdx)
3982 Result->Provenance[BitIdx] = BitPart::Unset;
3983 return Result;
3984 }
3985
3986 // If this is a truncate instruction, extract the lower bits.
3987 if (match(V, m_Trunc(m_Value(X)))) {
3988 const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
3989 Depth + 1, FoundRoot);
3990 if (!Res)
3991 return Result;
3992
3993 Result = BitPart(Res->Provider, BitWidth);
3994 for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx)
3995 Result->Provenance[BitIdx] = Res->Provenance[BitIdx];
3996 return Result;
3997 }
3998
3999 // BITREVERSE - most likely due to us previous matching a partial
4000 // bitreverse.
4001 if (match(V, m_BitReverse(m_Value(X)))) {
4002 const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
4003 Depth + 1, FoundRoot);
4004 if (!Res)
4005 return Result;
4006
4007 Result = BitPart(Res->Provider, BitWidth);
4008 for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx)
4009 Result->Provenance[(BitWidth - 1) - BitIdx] = Res->Provenance[BitIdx];
4010 return Result;
4011 }
4012
4013 // BSWAP - most likely due to us previous matching a partial bswap.
4014 if (match(V, m_BSwap(m_Value(X)))) {
4015 const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
4016 Depth + 1, FoundRoot);
4017 if (!Res)
4018 return Result;
4019
4020 unsigned ByteWidth = BitWidth / 8;
4021 Result = BitPart(Res->Provider, BitWidth);
4022 for (unsigned ByteIdx = 0; ByteIdx < ByteWidth; ++ByteIdx) {
4023 unsigned ByteBitOfs = ByteIdx * 8;
4024 for (unsigned BitIdx = 0; BitIdx < 8; ++BitIdx)
4025 Result->Provenance[(BitWidth - 8 - ByteBitOfs) + BitIdx] =
4026 Res->Provenance[ByteBitOfs + BitIdx];
4027 }
4028 return Result;
4029 }
4030
4031 // Funnel 'double' shifts take 3 operands, 2 inputs and the shift
4032 // amount (modulo).
4033 // fshl(X,Y,Z): (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
4034 // fshr(X,Y,Z): (X << (BW - (Z % BW))) | (Y >> (Z % BW))
4035 if (match(V, m_FShl(m_Value(X), m_Value(Y), m_APInt(C))) ||
4036 match(V, m_FShr(m_Value(X), m_Value(Y), m_APInt(C)))) {
4037 // We can treat fshr as a fshl by flipping the modulo amount.
4038 unsigned ModAmt = C->urem(BitWidth);
4039 if (cast<IntrinsicInst>(I)->getIntrinsicID() == Intrinsic::fshr)
4040 ModAmt = BitWidth - ModAmt;
4041
4042 // For bswap-only, limit shift amounts to whole bytes, for an early exit.
4043 if (!MatchBitReversals && (ModAmt % 8) != 0)
4044 return Result;
4045
4046 // Check we have both sources and they are from the same provider.
4047 const auto &LHS = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
4048 Depth + 1, FoundRoot);
4049 if (!LHS || !LHS->Provider)
4050 return Result;
4051
4052 const auto &RHS = collectBitParts(Y, MatchBSwaps, MatchBitReversals, BPS,
4053 Depth + 1, FoundRoot);
4054 if (!RHS || LHS->Provider != RHS->Provider)
4055 return Result;
4056
4057 unsigned StartBitRHS = BitWidth - ModAmt;
4058 Result = BitPart(LHS->Provider, BitWidth);
4059 for (unsigned BitIdx = 0; BitIdx < StartBitRHS; ++BitIdx)
4060 Result->Provenance[BitIdx + ModAmt] = LHS->Provenance[BitIdx];
4061 for (unsigned BitIdx = 0; BitIdx < ModAmt; ++BitIdx)
4062 Result->Provenance[BitIdx] = RHS->Provenance[BitIdx + StartBitRHS];
4063 return Result;
4064 }
4065 }
4066
4067 // If we've already found a root input value then we're never going to merge
4068 // these back together.
4069 if (FoundRoot)
4070 return Result;
4071
4072 // Okay, we got to something that isn't a shift, 'or', 'and', etc. This must
4073 // be the root input value to the bswap/bitreverse.
4074 FoundRoot = true;
4075 Result = BitPart(V, BitWidth);
4076 for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx)
4077 Result->Provenance[BitIdx] = BitIdx;
4078 return Result;
4079}
4080
4081static bool bitTransformIsCorrectForBSwap(unsigned From, unsigned To,
4082 unsigned BitWidth) {
4083 if (From % 8 != To % 8)
4084 return false;
4085 // Convert from bit indices to byte indices and check for a byte reversal.
4086 From >>= 3;
4087 To >>= 3;
4088 BitWidth >>= 3;
4089 return From == BitWidth - To - 1;
4090}
4091
4092static bool bitTransformIsCorrectForBitReverse(unsigned From, unsigned To,
4093 unsigned BitWidth) {
4094 return From == BitWidth - To - 1;
4095}
4096
4098 Instruction *I, bool MatchBSwaps, bool MatchBitReversals,
4099 SmallVectorImpl<Instruction *> &InsertedInsts) {
4100 if (!match(I, m_Or(m_Value(), m_Value())) &&
4101 !match(I, m_FShl(m_Value(), m_Value(), m_Value())) &&
4102 !match(I, m_FShr(m_Value(), m_Value(), m_Value())) &&
4103 !match(I, m_BSwap(m_Value())))
4104 return false;
4105 if (!MatchBSwaps && !MatchBitReversals)
4106 return false;
4107 Type *ITy = I->getType();
4108 if (!ITy->isIntOrIntVectorTy() || ITy->getScalarSizeInBits() > 128)
4109 return false; // Can't do integer/elements > 128 bits.
4110
4111 // Try to find all the pieces corresponding to the bswap.
4112 bool FoundRoot = false;
4113 std::map<Value *, std::optional<BitPart>> BPS;
4114 const auto &Res =
4115 collectBitParts(I, MatchBSwaps, MatchBitReversals, BPS, 0, FoundRoot);
4116 if (!Res)
4117 return false;
4118 ArrayRef<int8_t> BitProvenance = Res->Provenance;
4119 assert(all_of(BitProvenance,
4120 [](int8_t I) { return I == BitPart::Unset || 0 <= I; }) &&
4121 "Illegal bit provenance index");
4122
4123 // If the upper bits are zero, then attempt to perform as a truncated op.
4124 Type *DemandedTy = ITy;
4125 if (BitProvenance.back() == BitPart::Unset) {
4126 while (!BitProvenance.empty() && BitProvenance.back() == BitPart::Unset)
4127 BitProvenance = BitProvenance.drop_back();
4128 if (BitProvenance.empty())
4129 return false; // TODO - handle null value?
4130 DemandedTy = Type::getIntNTy(I->getContext(), BitProvenance.size());
4131 if (auto *IVecTy = dyn_cast<VectorType>(ITy))
4132 DemandedTy = VectorType::get(DemandedTy, IVecTy);
4133 }
4134
4135 // Check BitProvenance hasn't found a source larger than the result type.
4136 unsigned DemandedBW = DemandedTy->getScalarSizeInBits();
4137 if (DemandedBW > ITy->getScalarSizeInBits())
4138 return false;
4139
4140 // Now, is the bit permutation correct for a bswap or a bitreverse? We can
4141 // only byteswap values with an even number of bytes.
4142 APInt DemandedMask = APInt::getAllOnes(DemandedBW);
4143 bool OKForBSwap = MatchBSwaps && (DemandedBW % 16) == 0;
4144 bool OKForBitReverse = MatchBitReversals;
4145 for (unsigned BitIdx = 0;
4146 (BitIdx < DemandedBW) && (OKForBSwap || OKForBitReverse); ++BitIdx) {
4147 if (BitProvenance[BitIdx] == BitPart::Unset) {
4148 DemandedMask.clearBit(BitIdx);
4149 continue;
4150 }
4151 OKForBSwap &= bitTransformIsCorrectForBSwap(BitProvenance[BitIdx], BitIdx,
4152 DemandedBW);
4153 OKForBitReverse &= bitTransformIsCorrectForBitReverse(BitProvenance[BitIdx],
4154 BitIdx, DemandedBW);
4155 }
4156
4157 Intrinsic::ID Intrin;
4158 if (OKForBSwap)
4159 Intrin = Intrinsic::bswap;
4160 else if (OKForBitReverse)
4161 Intrin = Intrinsic::bitreverse;
4162 else
4163 return false;
4164
4165 Function *F =
4166 Intrinsic::getOrInsertDeclaration(I->getModule(), Intrin, DemandedTy);
4167 Value *Provider = Res->Provider;
4168
4169 // We may need to truncate the provider.
4170 if (DemandedTy != Provider->getType()) {
4171 auto *Trunc =
4172 CastInst::CreateIntegerCast(Provider, DemandedTy, false, "trunc", I->getIterator());
4173 InsertedInsts.push_back(Trunc);
4174 Provider = Trunc;
4175 }
4176
4177 Instruction *Result = CallInst::Create(F, Provider, "rev", I->getIterator());
4178 InsertedInsts.push_back(Result);
4179
4180 if (!DemandedMask.isAllOnes()) {
4181 auto *Mask = ConstantInt::get(DemandedTy, DemandedMask);
4182 Result = BinaryOperator::Create(Instruction::And, Result, Mask, "mask", I->getIterator());
4183 InsertedInsts.push_back(Result);
4184 }
4185
4186 // We may need to zeroextend back to the result type.
4187 if (ITy != Result->getType()) {
4188 auto *ExtInst = CastInst::CreateIntegerCast(Result, ITy, false, "zext", I->getIterator());
4189 InsertedInsts.push_back(ExtInst);
4190 }
4191
4192 return true;
4193}
4194
4195// CodeGen has special handling for some string functions that may replace
4196// them with target-specific intrinsics. Since that'd skip our interceptors
4197// in ASan/MSan/TSan/DFSan, and thus make us miss some memory accesses,
4198// we mark affected calls as NoBuiltin, which will disable optimization
4199// in CodeGen.
4201 CallInst *CI, const TargetLibraryInfo *TLI) {
4202 Function *F = CI->getCalledFunction();
4203 LibFunc Func;
4204 if (F && !F->hasLocalLinkage() && F->hasName() &&
4205 TLI->getLibFunc(F->getName(), Func) && TLI->hasOptimizedCodeGen(Func) &&
4206 !F->doesNotAccessMemory())
4207 CI->addFnAttr(Attribute::NoBuiltin);
4208}
4209
4211 // We can't have a PHI with a metadata type.
4212 if (I->getOperand(OpIdx)->getType()->isMetadataTy())
4213 return false;
4214
4215 // Early exit.
4216 if (!isa<Constant>(I->getOperand(OpIdx)))
4217 return true;
4218
4219 switch (I->getOpcode()) {
4220 default:
4221 return true;
4222 case Instruction::Call:
4223 case Instruction::Invoke: {
4224 const auto &CB = cast<CallBase>(*I);
4225
4226 // Can't handle inline asm. Skip it.
4227 if (CB.isInlineAsm())
4228 return false;
4229
4230 // Constant bundle operands may need to retain their constant-ness for
4231 // correctness.
4232 if (CB.isBundleOperand(OpIdx))
4233 return false;
4234
4235 if (OpIdx < CB.arg_size()) {
4236 // Some variadic intrinsics require constants in the variadic arguments,
4237 // which currently aren't markable as immarg.
4238 if (isa<IntrinsicInst>(CB) &&
4239 OpIdx >= CB.getFunctionType()->getNumParams()) {
4240 // This is known to be OK for stackmap.
4241 return CB.getIntrinsicID() == Intrinsic::experimental_stackmap;
4242 }
4243
4244 // gcroot is a special case, since it requires a constant argument which
4245 // isn't also required to be a simple ConstantInt.
4246 if (CB.getIntrinsicID() == Intrinsic::gcroot)
4247 return false;
4248
4249 // Some intrinsic operands are required to be immediates.
4250 return !CB.paramHasAttr(OpIdx, Attribute::ImmArg);
4251 }
4252
4253 // It is never allowed to replace the call argument to an intrinsic, but it
4254 // may be possible for a call.
4255 return !isa<IntrinsicInst>(CB);
4256 }
4257 case Instruction::ShuffleVector:
4258 // Shufflevector masks are constant.
4259 return OpIdx != 2;
4260 case Instruction::Switch:
4261 case Instruction::ExtractValue:
4262 // All operands apart from the first are constant.
4263 return OpIdx == 0;
4264 case Instruction::InsertValue:
4265 // All operands apart from the first and the second are constant.
4266 return OpIdx < 2;
4267 case Instruction::Alloca:
4268 // Static allocas (constant size in the entry block) are handled by
4269 // prologue/epilogue insertion so they're free anyway. We definitely don't
4270 // want to make them non-constant.
4271 return !cast<AllocaInst>(I)->isStaticAlloca();
4272 case Instruction::GetElementPtr:
4273 if (OpIdx == 0)
4274 return true;
4276 for (auto E = std::next(It, OpIdx); It != E; ++It)
4277 if (It.isStruct())
4278 return false;
4279 return true;
4280 }
4281}
4282
4284 // First: Check if it's a constant
4285 if (Constant *C = dyn_cast<Constant>(Condition))
4286 return ConstantExpr::getNot(C);
4287
4288 // Second: If the condition is already inverted, return the original value
4289 Value *NotCondition;
4290 if (match(Condition, m_Not(m_Value(NotCondition))))
4291 return NotCondition;
4292
4293 BasicBlock *Parent = nullptr;
4294 Instruction *Inst = dyn_cast<Instruction>(Condition);
4295 if (Inst)
4296 Parent = Inst->getParent();
4297 else if (Argument *Arg = dyn_cast<Argument>(Condition))
4298 Parent = &Arg->getParent()->getEntryBlock();
4299 assert(Parent && "Unsupported condition to invert");
4300
4301 // Third: Check all the users for an invert
4302 for (User *U : Condition->users())
4303 if (Instruction *I = dyn_cast<Instruction>(U))
4304 if (I->getParent() == Parent && match(I, m_Not(m_Specific(Condition))))
4305 return I;
4306
4307 // Last option: Create a new instruction
4308 auto *Inverted =
4309 BinaryOperator::CreateNot(Condition, Condition->getName() + ".inv");
4310 if (Inst && !isa<PHINode>(Inst))
4311 Inverted->insertAfter(Inst);
4312 else
4313 Inverted->insertBefore(&*Parent->getFirstInsertionPt());
4314 return Inverted;
4315}
4316
4318 // Note: We explicitly check for attributes rather than using cover functions
4319 // because some of the cover functions include the logic being implemented.
4320
4321 bool Changed = false;
4322 // readnone + not convergent implies nosync
4323 if (!F.hasFnAttribute(Attribute::NoSync) &&
4324 F.doesNotAccessMemory() && !F.isConvergent()) {
4325 F.setNoSync();
4326 Changed = true;
4327 }
4328
4329 // readonly implies nofree
4330 if (!F.hasFnAttribute(Attribute::NoFree) && F.onlyReadsMemory()) {
4331 F.setDoesNotFreeMemory();
4332 Changed = true;
4333 }
4334
4335 // willreturn implies mustprogress
4336 if (!F.hasFnAttribute(Attribute::MustProgress) && F.willReturn()) {
4337 F.setMustProgress();
4338 Changed = true;
4339 }
4340
4341 // TODO: There are a bunch of cases of restrictive memory effects we
4342 // can infer by inspecting arguments of argmemonly-ish functions.
4343
4344 return Changed;
4345}
#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:3038
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:1641
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:2769
static bool isStructure(AllocaInst *AI)
Determine whether this alloca is a structure.
Definition: Local.cpp:1953
uint64_t getDwarfOpForBinOp(Instruction::BinaryOps Opcode)
Definition: Local.cpp:2487
static bool PhiHasDebugValue(DILocalVariable *DIVar, DIExpression *DIExpr, PHINode *APN)
===------------------------------------------------------------------—===// Dbg Intrinsic utilities
Definition: Local.cpp:1611
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:2517
std::optional< DIExpression * > DbgValReplacement
A replacement for a dbg.value expression.
Definition: Local.cpp:2666
static void insertDbgValueOrDbgVariableRecordAfter(DIBuilder &Builder, Value *DV, DILocalVariable *DIVar, DIExpression *DIExpr, const DebugLoc &NewLoc, BasicBlock::iterator Instr)
Definition: Local.cpp:1712
Value * getSalvageOpsForBinOp(BinaryOperator *BI, uint64_t CurrentLocOps, SmallVectorImpl< uint64_t > &Opcodes, SmallVectorImpl< Value * > &AdditionalValues)
Definition: Local.cpp:2529
static DIExpression * dropInitialDeref(const DIExpression *DIExpr)
Definition: Local.cpp:1774
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:2461
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:2588
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:1947
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:3545
uint64_t getDwarfOpForIcmpPred(CmpInst::Predicate Pred)
Definition: Local.cpp:2563
static bool bitTransformIsCorrectForBSwap(unsigned From, unsigned To, unsigned BitWidth)
Definition: Local.cpp:4081
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:3861
static bool EliminateDuplicatePHINodesNaiveImpl(BasicBlock *BB, SmallPtrSetImpl< PHINode * > &ToRemove)
Definition: Local.cpp:1392
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:1428
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:2671
static void insertDbgValueOrDbgVariableRecord(DIBuilder &Builder, Value *DV, DILocalVariable *DIVar, DIExpression *DIExpr, const DebugLoc &NewLoc, BasicBlock::iterator Instr)
Definition: Local.cpp:1693
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:2224
static void insertDbgVariableRecordsForPHIs(BasicBlock *BB, SmallVectorImpl< PHINode * > &InsertedPHIs)
Definition: Local.cpp:2078
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:4092
static void salvageDbgAssignAddress(T *Assign)
Definition: Local.cpp:2281
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:99
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:475
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:833
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:94
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:390
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:472
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:76
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:2995
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:3599
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:2877
auto pred_end(const MachineBasicBlock *BB)
unsigned replaceNonLocalUsesWith(Instruction *From, Value *To)
Definition: Local.cpp:3566
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:2316
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:1668
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:2975
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:3449
void InsertDebugValueAtStoreLoc(DbgVariableRecord *DVR, StoreInst *SI, DIBuilder &Builder)
===------------------------------------------------------------------—===// Dbg Intrinsic utilities
Definition: Local.cpp:1868
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:3788
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:2142
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:2859
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:4097
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:1581
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:1990
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:3750
CallInst * createCallMatchingInvoke(InvokeInst *II)
Create a call that matches the invoke II in terms of arguments, attributes, debug information,...
Definition: Local.cpp:2949
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:3236
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:3501
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:1731
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:3581
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:3311
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:2909
bool replaceAllDbgUsesWith(Instruction &From, Value &To, Instruction &DomPoint, DominatorTree &DT)
Point debug users of From to To or salvage them.
Definition: Local.cpp:2787
Value * salvageDebugInfoImpl(Instruction &I, uint64_t CurrentLocOps, SmallVectorImpl< uint64_t > &Ops, SmallVectorImpl< Value * > &AdditionalValues)
Definition: Local.cpp:2617
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:3426
void dropDebugUsers(Instruction &I)
Remove the debug intrinsic instructions for the given instruction.
Definition: Local.cpp:3697
@ 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:3707
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:3673
void copyNonnullMetadata(const LoadInst &OldLI, MDNode *N, LoadInst &NewLI)
Copy a nonnull metadata node to a new load instruction.
Definition: Local.cpp:3648
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:4210
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:2253
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:1532
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:4317
Value * invertCondition(Value *Condition)
Invert the given true/false value, possibly reusing an existing copy.
Definition: Local.cpp:4283
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:4200
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:3274
bool EliminateDuplicatePHINodes(BasicBlock *BB)
Check for and eliminate duplicate PHI nodes in this block.
Definition: Local.cpp:1524
bool callsGCLeafFunction(const CallBase *Call, const TargetLibraryInfo &TLI)
Return true if this call calls a gc leaf function.
Definition: Local.cpp:3619
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:2204
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