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