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