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