1 //===- Local.cpp - Functions to perform local transformations -------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This family of functions perform various local transformations to the
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Transforms/Utils/Local.h"
15 #include "llvm/ADT/APInt.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/DenseMapInfo.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"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/Analysis/AssumeBundleQueries.h"
26 #include "llvm/Analysis/ConstantFolding.h"
27 #include "llvm/Analysis/DomTreeUpdater.h"
28 #include "llvm/Analysis/InstructionSimplify.h"
29 #include "llvm/Analysis/MemoryBuiltins.h"
30 #include "llvm/Analysis/MemorySSAUpdater.h"
31 #include "llvm/Analysis/TargetLibraryInfo.h"
32 #include "llvm/Analysis/ValueTracking.h"
33 #include "llvm/Analysis/VectorUtils.h"
34 #include "llvm/BinaryFormat/Dwarf.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"
40 #include "llvm/IR/ConstantRange.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"
45 #include "llvm/IR/DebugInfoMetadata.h"
46 #include "llvm/IR/DebugLoc.h"
47 #include "llvm/IR/DerivedTypes.h"
48 #include "llvm/IR/Dominators.h"
49 #include "llvm/IR/EHPersonalities.h"
50 #include "llvm/IR/Function.h"
51 #include "llvm/IR/GetElementPtrTypeIterator.h"
52 #include "llvm/IR/GlobalObject.h"
53 #include "llvm/IR/IRBuilder.h"
54 #include "llvm/IR/InstrTypes.h"
55 #include "llvm/IR/Instruction.h"
56 #include "llvm/IR/Instructions.h"
57 #include "llvm/IR/IntrinsicInst.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"
62 #include "llvm/IR/Metadata.h"
63 #include "llvm/IR/Module.h"
64 #include "llvm/IR/PatternMatch.h"
65 #include "llvm/IR/ProfDataUtils.h"
66 #include "llvm/IR/Type.h"
67 #include "llvm/IR/Use.h"
68 #include "llvm/IR/User.h"
69 #include "llvm/IR/Value.h"
70 #include "llvm/IR/ValueHandle.h"
71 #include "llvm/Support/Casting.h"
72 #include "llvm/Support/CommandLine.h"
73 #include "llvm/Support/Debug.h"
74 #include "llvm/Support/ErrorHandling.h"
75 #include "llvm/Support/KnownBits.h"
76 #include "llvm/Support/raw_ostream.h"
77 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
78 #include "llvm/Transforms/Utils/ValueMapper.h"
88 using namespace llvm::PatternMatch
;
90 extern cl::opt
<bool> UseNewDbgInfoFormat
;
92 #define DEBUG_TYPE "local"
94 STATISTIC(NumRemoved
, "Number of unreachable basic blocks removed");
95 STATISTIC(NumPHICSEs
, "Number of PHI's that got CSE'd");
97 static cl::opt
<bool> PHICSEDebugHash(
99 #ifdef EXPENSIVE_CHECKS
105 cl::desc("Perform extra assertion checking to verify that PHINodes's hash "
106 "function is well-behaved w.r.t. its isEqual predicate"));
108 static cl::opt
<unsigned> PHICSENumPHISmallSize(
109 "phicse-num-phi-smallsize", cl::init(32), cl::Hidden
,
111 "When the basic block contains not more than this number of PHI nodes, "
112 "perform a (faster!) exhaustive search instead of set-driven one."));
114 // Max recursion depth for collectBitParts used when detecting bswap and
115 // bitreverse idioms.
116 static const unsigned BitPartRecursionMaxDepth
= 48;
118 //===----------------------------------------------------------------------===//
119 // Local constant propagation.
122 /// ConstantFoldTerminator - If a terminator instruction is predicated on a
123 /// constant value, convert it into an unconditional branch to the constant
124 /// destination. This is a nontrivial operation because the successors of this
125 /// basic block must have their PHI nodes updated.
126 /// Also calls RecursivelyDeleteTriviallyDeadInstructions() on any branch/switch
127 /// conditions and indirectbr addresses this might make dead if
128 /// DeleteDeadConditions is true.
129 bool llvm::ConstantFoldTerminator(BasicBlock
*BB
, bool DeleteDeadConditions
,
130 const TargetLibraryInfo
*TLI
,
131 DomTreeUpdater
*DTU
) {
132 Instruction
*T
= BB
->getTerminator();
133 IRBuilder
<> Builder(T
);
135 // Branch - See if we are conditional jumping on constant
136 if (auto *BI
= dyn_cast
<BranchInst
>(T
)) {
137 if (BI
->isUnconditional()) return false; // Can't optimize uncond branch
139 BasicBlock
*Dest1
= BI
->getSuccessor(0);
140 BasicBlock
*Dest2
= BI
->getSuccessor(1);
142 if (Dest2
== Dest1
) { // Conditional branch to same location?
143 // This branch matches something like this:
144 // br bool %cond, label %Dest, label %Dest
145 // and changes it into: br label %Dest
147 // Let the basic block know that we are letting go of one copy of it.
148 assert(BI
->getParent() && "Terminator not inserted in block!");
149 Dest1
->removePredecessor(BI
->getParent());
151 // Replace the conditional branch with an unconditional one.
152 BranchInst
*NewBI
= Builder
.CreateBr(Dest1
);
154 // Transfer the metadata to the new branch instruction.
155 NewBI
->copyMetadata(*BI
, {LLVMContext::MD_loop
, LLVMContext::MD_dbg
,
156 LLVMContext::MD_annotation
});
158 Value
*Cond
= BI
->getCondition();
159 BI
->eraseFromParent();
160 if (DeleteDeadConditions
)
161 RecursivelyDeleteTriviallyDeadInstructions(Cond
, TLI
);
165 if (auto *Cond
= dyn_cast
<ConstantInt
>(BI
->getCondition())) {
166 // Are we branching on constant?
167 // YES. Change to unconditional branch...
168 BasicBlock
*Destination
= Cond
->getZExtValue() ? Dest1
: Dest2
;
169 BasicBlock
*OldDest
= Cond
->getZExtValue() ? Dest2
: Dest1
;
171 // Let the basic block know that we are letting go of it. Based on this,
172 // it will adjust it's PHI nodes.
173 OldDest
->removePredecessor(BB
);
175 // Replace the conditional branch with an unconditional one.
176 BranchInst
*NewBI
= Builder
.CreateBr(Destination
);
178 // Transfer the metadata to the new branch instruction.
179 NewBI
->copyMetadata(*BI
, {LLVMContext::MD_loop
, LLVMContext::MD_dbg
,
180 LLVMContext::MD_annotation
});
182 BI
->eraseFromParent();
184 DTU
->applyUpdates({{DominatorTree::Delete
, BB
, OldDest
}});
191 if (auto *SI
= dyn_cast
<SwitchInst
>(T
)) {
192 // If we are switching on a constant, we can convert the switch to an
193 // unconditional branch.
194 auto *CI
= dyn_cast
<ConstantInt
>(SI
->getCondition());
195 BasicBlock
*DefaultDest
= SI
->getDefaultDest();
196 BasicBlock
*TheOnlyDest
= DefaultDest
;
198 // If the default is unreachable, ignore it when searching for TheOnlyDest.
199 if (isa
<UnreachableInst
>(DefaultDest
->getFirstNonPHIOrDbg()) &&
200 SI
->getNumCases() > 0) {
201 TheOnlyDest
= SI
->case_begin()->getCaseSuccessor();
204 bool Changed
= false;
206 // Figure out which case it goes to.
207 for (auto It
= SI
->case_begin(), End
= SI
->case_end(); It
!= End
;) {
208 // Found case matching a constant operand?
209 if (It
->getCaseValue() == CI
) {
210 TheOnlyDest
= It
->getCaseSuccessor();
214 // Check to see if this branch is going to the same place as the default
215 // dest. If so, eliminate it as an explicit compare.
216 if (It
->getCaseSuccessor() == DefaultDest
) {
217 MDNode
*MD
= getValidBranchWeightMDNode(*SI
);
218 unsigned NCases
= SI
->getNumCases();
219 // Fold the case metadata into the default if there will be any branches
220 // left, unless the metadata doesn't match the switch.
221 if (NCases
> 1 && MD
) {
222 // Collect branch weights into a vector.
223 SmallVector
<uint32_t, 8> Weights
;
224 extractBranchWeights(MD
, Weights
);
226 // Merge weight of this case to the default weight.
227 unsigned Idx
= It
->getCaseIndex();
228 // TODO: Add overflow check.
229 Weights
[0] += Weights
[Idx
+ 1];
230 // Remove weight for this case.
231 std::swap(Weights
[Idx
+ 1], Weights
.back());
233 setBranchWeights(*SI
, Weights
);
235 // Remove this entry.
236 BasicBlock
*ParentBB
= SI
->getParent();
237 DefaultDest
->removePredecessor(ParentBB
);
238 It
= SI
->removeCase(It
);
239 End
= SI
->case_end();
241 // Removing this case may have made the condition constant. In that
242 // case, update CI and restart iteration through the cases.
243 if (auto *NewCI
= dyn_cast
<ConstantInt
>(SI
->getCondition())) {
245 It
= SI
->case_begin();
252 // Otherwise, check to see if the switch only branches to one destination.
253 // We do this by reseting "TheOnlyDest" to null when we find two non-equal
255 if (It
->getCaseSuccessor() != TheOnlyDest
)
256 TheOnlyDest
= nullptr;
258 // Increment this iterator as we haven't removed the case.
262 if (CI
&& !TheOnlyDest
) {
263 // Branching on a constant, but not any of the cases, go to the default
265 TheOnlyDest
= SI
->getDefaultDest();
268 // If we found a single destination that we can fold the switch into, do so
271 // Insert the new branch.
272 Builder
.CreateBr(TheOnlyDest
);
273 BasicBlock
*BB
= SI
->getParent();
275 SmallSet
<BasicBlock
*, 8> RemovedSuccessors
;
277 // Remove entries from PHI nodes which we no longer branch to...
278 BasicBlock
*SuccToKeep
= TheOnlyDest
;
279 for (BasicBlock
*Succ
: successors(SI
)) {
280 if (DTU
&& Succ
!= TheOnlyDest
)
281 RemovedSuccessors
.insert(Succ
);
282 // Found case matching a constant operand?
283 if (Succ
== SuccToKeep
) {
284 SuccToKeep
= nullptr; // Don't modify the first branch to TheOnlyDest
286 Succ
->removePredecessor(BB
);
290 // Delete the old switch.
291 Value
*Cond
= SI
->getCondition();
292 SI
->eraseFromParent();
293 if (DeleteDeadConditions
)
294 RecursivelyDeleteTriviallyDeadInstructions(Cond
, TLI
);
296 std::vector
<DominatorTree::UpdateType
> Updates
;
297 Updates
.reserve(RemovedSuccessors
.size());
298 for (auto *RemovedSuccessor
: RemovedSuccessors
)
299 Updates
.push_back({DominatorTree::Delete
, BB
, RemovedSuccessor
});
300 DTU
->applyUpdates(Updates
);
305 if (SI
->getNumCases() == 1) {
306 // Otherwise, we can fold this switch into a conditional branch
307 // instruction if it has only one non-default destination.
308 auto FirstCase
= *SI
->case_begin();
309 Value
*Cond
= Builder
.CreateICmpEQ(SI
->getCondition(),
310 FirstCase
.getCaseValue(), "cond");
312 // Insert the new branch.
313 BranchInst
*NewBr
= Builder
.CreateCondBr(Cond
,
314 FirstCase
.getCaseSuccessor(),
315 SI
->getDefaultDest());
316 SmallVector
<uint32_t> Weights
;
317 if (extractBranchWeights(*SI
, Weights
) && Weights
.size() == 2) {
318 uint32_t DefWeight
= Weights
[0];
319 uint32_t CaseWeight
= Weights
[1];
320 // The TrueWeight should be the weight for the single case of SI.
321 NewBr
->setMetadata(LLVMContext::MD_prof
,
322 MDBuilder(BB
->getContext())
323 .createBranchWeights(CaseWeight
, DefWeight
));
326 // Update make.implicit metadata to the newly-created conditional branch.
327 MDNode
*MakeImplicitMD
= SI
->getMetadata(LLVMContext::MD_make_implicit
);
329 NewBr
->setMetadata(LLVMContext::MD_make_implicit
, MakeImplicitMD
);
331 // Delete the old switch.
332 SI
->eraseFromParent();
338 if (auto *IBI
= dyn_cast
<IndirectBrInst
>(T
)) {
339 // indirectbr blockaddress(@F, @BB) -> br label @BB
341 dyn_cast
<BlockAddress
>(IBI
->getAddress()->stripPointerCasts())) {
342 BasicBlock
*TheOnlyDest
= BA
->getBasicBlock();
343 SmallSet
<BasicBlock
*, 8> RemovedSuccessors
;
345 // Insert the new branch.
346 Builder
.CreateBr(TheOnlyDest
);
348 BasicBlock
*SuccToKeep
= TheOnlyDest
;
349 for (unsigned i
= 0, e
= IBI
->getNumDestinations(); i
!= e
; ++i
) {
350 BasicBlock
*DestBB
= IBI
->getDestination(i
);
351 if (DTU
&& DestBB
!= TheOnlyDest
)
352 RemovedSuccessors
.insert(DestBB
);
353 if (IBI
->getDestination(i
) == SuccToKeep
) {
354 SuccToKeep
= nullptr;
356 DestBB
->removePredecessor(BB
);
359 Value
*Address
= IBI
->getAddress();
360 IBI
->eraseFromParent();
361 if (DeleteDeadConditions
)
362 // Delete pointer cast instructions.
363 RecursivelyDeleteTriviallyDeadInstructions(Address
, TLI
);
365 // Also zap the blockaddress constant if there are no users remaining,
366 // otherwise the destination is still marked as having its address taken.
368 BA
->destroyConstant();
370 // If we didn't find our destination in the IBI successor list, then we
371 // have undefined behavior. Replace the unconditional branch with an
372 // 'unreachable' instruction.
374 BB
->getTerminator()->eraseFromParent();
375 new UnreachableInst(BB
->getContext(), BB
);
379 std::vector
<DominatorTree::UpdateType
> Updates
;
380 Updates
.reserve(RemovedSuccessors
.size());
381 for (auto *RemovedSuccessor
: RemovedSuccessors
)
382 Updates
.push_back({DominatorTree::Delete
, BB
, RemovedSuccessor
});
383 DTU
->applyUpdates(Updates
);
392 //===----------------------------------------------------------------------===//
393 // Local dead code elimination.
396 /// isInstructionTriviallyDead - Return true if the result produced by the
397 /// instruction is not used, and the instruction has no side effects.
399 bool llvm::isInstructionTriviallyDead(Instruction
*I
,
400 const TargetLibraryInfo
*TLI
) {
403 return wouldInstructionBeTriviallyDead(I
, TLI
);
406 bool llvm::wouldInstructionBeTriviallyDeadOnUnusedPaths(
407 Instruction
*I
, const TargetLibraryInfo
*TLI
) {
408 // Instructions that are "markers" and have implied meaning on code around
409 // them (without explicit uses), are not dead on unused paths.
410 if (IntrinsicInst
*II
= dyn_cast
<IntrinsicInst
>(I
))
411 if (II
->getIntrinsicID() == Intrinsic::stacksave
||
412 II
->getIntrinsicID() == Intrinsic::launder_invariant_group
||
413 II
->isLifetimeStartOrEnd())
415 return wouldInstructionBeTriviallyDead(I
, TLI
);
418 bool llvm::wouldInstructionBeTriviallyDead(const Instruction
*I
,
419 const TargetLibraryInfo
*TLI
) {
420 if (I
->isTerminator())
423 // We don't want the landingpad-like instructions removed by anything this
428 // We don't want debug info removed by anything this general.
429 if (isa
<DbgVariableIntrinsic
>(I
))
432 if (const DbgLabelInst
*DLI
= dyn_cast
<DbgLabelInst
>(I
)) {
438 if (auto *CB
= dyn_cast
<CallBase
>(I
))
439 if (isRemovableAlloc(CB
, TLI
))
442 if (!I
->willReturn()) {
443 auto *II
= dyn_cast
<IntrinsicInst
>(I
);
447 switch (II
->getIntrinsicID()) {
448 case Intrinsic::experimental_guard
: {
449 // Guards on true are operationally no-ops. In the future we can
450 // consider more sophisticated tradeoffs for guards considering potential
451 // for check widening, but for now we keep things simple.
452 auto *Cond
= dyn_cast
<ConstantInt
>(II
->getArgOperand(0));
453 return Cond
&& Cond
->isOne();
455 // TODO: These intrinsics are not safe to remove, because this may remove
456 // a well-defined trap.
457 case Intrinsic::wasm_trunc_signed
:
458 case Intrinsic::wasm_trunc_unsigned
:
459 case Intrinsic::ptrauth_auth
:
460 case Intrinsic::ptrauth_resign
:
467 if (!I
->mayHaveSideEffects())
470 // Special case intrinsics that "may have side effects" but can be deleted
472 if (const IntrinsicInst
*II
= dyn_cast
<IntrinsicInst
>(I
)) {
473 // Safe to delete llvm.stacksave and launder.invariant.group if dead.
474 if (II
->getIntrinsicID() == Intrinsic::stacksave
||
475 II
->getIntrinsicID() == Intrinsic::launder_invariant_group
)
478 if (II
->isLifetimeStartOrEnd()) {
479 auto *Arg
= II
->getArgOperand(1);
480 // Lifetime intrinsics are dead when their right-hand is undef.
481 if (isa
<UndefValue
>(Arg
))
483 // If the right-hand is an alloc, global, or argument and the only uses
484 // are lifetime intrinsics then the intrinsics are dead.
485 if (isa
<AllocaInst
>(Arg
) || isa
<GlobalValue
>(Arg
) || isa
<Argument
>(Arg
))
486 return llvm::all_of(Arg
->uses(), [](Use
&Use
) {
487 if (IntrinsicInst
*IntrinsicUse
=
488 dyn_cast
<IntrinsicInst
>(Use
.getUser()))
489 return IntrinsicUse
->isLifetimeStartOrEnd();
495 // Assumptions are dead if their condition is trivially true.
496 if (II
->getIntrinsicID() == Intrinsic::assume
&&
497 isAssumeWithEmptyBundle(cast
<AssumeInst
>(*II
))) {
498 if (ConstantInt
*Cond
= dyn_cast
<ConstantInt
>(II
->getArgOperand(0)))
499 return !Cond
->isZero();
504 if (auto *FPI
= dyn_cast
<ConstrainedFPIntrinsic
>(I
)) {
505 std::optional
<fp::ExceptionBehavior
> ExBehavior
=
506 FPI
->getExceptionBehavior();
507 return *ExBehavior
!= fp::ebStrict
;
511 if (auto *Call
= dyn_cast
<CallBase
>(I
)) {
512 if (Value
*FreedOp
= getFreedOperand(Call
, TLI
))
513 if (Constant
*C
= dyn_cast
<Constant
>(FreedOp
))
514 return C
->isNullValue() || isa
<UndefValue
>(C
);
515 if (isMathLibCallNoop(Call
, TLI
))
519 // Non-volatile atomic loads from constants can be removed.
520 if (auto *LI
= dyn_cast
<LoadInst
>(I
))
521 if (auto *GV
= dyn_cast
<GlobalVariable
>(
522 LI
->getPointerOperand()->stripPointerCasts()))
523 if (!LI
->isVolatile() && GV
->isConstant())
529 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
530 /// trivially dead instruction, delete it. If that makes any of its operands
531 /// trivially dead, delete them too, recursively. Return true if any
532 /// instructions were deleted.
533 bool llvm::RecursivelyDeleteTriviallyDeadInstructions(
534 Value
*V
, const TargetLibraryInfo
*TLI
, MemorySSAUpdater
*MSSAU
,
535 std::function
<void(Value
*)> AboutToDeleteCallback
) {
536 Instruction
*I
= dyn_cast
<Instruction
>(V
);
537 if (!I
|| !isInstructionTriviallyDead(I
, TLI
))
540 SmallVector
<WeakTrackingVH
, 16> DeadInsts
;
541 DeadInsts
.push_back(I
);
542 RecursivelyDeleteTriviallyDeadInstructions(DeadInsts
, TLI
, MSSAU
,
543 AboutToDeleteCallback
);
548 bool llvm::RecursivelyDeleteTriviallyDeadInstructionsPermissive(
549 SmallVectorImpl
<WeakTrackingVH
> &DeadInsts
, const TargetLibraryInfo
*TLI
,
550 MemorySSAUpdater
*MSSAU
,
551 std::function
<void(Value
*)> AboutToDeleteCallback
) {
552 unsigned S
= 0, E
= DeadInsts
.size(), Alive
= 0;
553 for (; S
!= E
; ++S
) {
554 auto *I
= dyn_cast_or_null
<Instruction
>(DeadInsts
[S
]);
555 if (!I
|| !isInstructionTriviallyDead(I
)) {
556 DeadInsts
[S
] = nullptr;
562 RecursivelyDeleteTriviallyDeadInstructions(DeadInsts
, TLI
, MSSAU
,
563 AboutToDeleteCallback
);
567 void llvm::RecursivelyDeleteTriviallyDeadInstructions(
568 SmallVectorImpl
<WeakTrackingVH
> &DeadInsts
, const TargetLibraryInfo
*TLI
,
569 MemorySSAUpdater
*MSSAU
,
570 std::function
<void(Value
*)> AboutToDeleteCallback
) {
571 // Process the dead instruction list until empty.
572 while (!DeadInsts
.empty()) {
573 Value
*V
= DeadInsts
.pop_back_val();
574 Instruction
*I
= cast_or_null
<Instruction
>(V
);
577 assert(isInstructionTriviallyDead(I
, TLI
) &&
578 "Live instruction found in dead worklist!");
579 assert(I
->use_empty() && "Instructions with uses are not dead.");
581 // Don't lose the debug info while deleting the instructions.
582 salvageDebugInfo(*I
);
584 if (AboutToDeleteCallback
)
585 AboutToDeleteCallback(I
);
587 // Null out all of the instruction's operands to see if any operand becomes
589 for (Use
&OpU
: I
->operands()) {
590 Value
*OpV
= OpU
.get();
593 if (!OpV
->use_empty())
596 // If the operand is an instruction that became dead as we nulled out the
597 // operand, and if it is 'trivially' dead, delete it in a future loop
599 if (Instruction
*OpI
= dyn_cast
<Instruction
>(OpV
))
600 if (isInstructionTriviallyDead(OpI
, TLI
))
601 DeadInsts
.push_back(OpI
);
604 MSSAU
->removeMemoryAccess(I
);
606 I
->eraseFromParent();
610 bool llvm::replaceDbgUsesWithUndef(Instruction
*I
) {
611 SmallVector
<DbgVariableIntrinsic
*, 1> DbgUsers
;
612 SmallVector
<DPValue
*, 1> DPUsers
;
613 findDbgUsers(DbgUsers
, I
, &DPUsers
);
614 for (auto *DII
: DbgUsers
)
615 DII
->setKillLocation();
616 for (auto *DPV
: DPUsers
)
617 DPV
->setKillLocation();
618 return !DbgUsers
.empty() || !DPUsers
.empty();
621 /// areAllUsesEqual - Check whether the uses of a value are all the same.
622 /// This is similar to Instruction::hasOneUse() except this will also return
623 /// true when there are no uses or multiple uses that all refer to the same
625 static bool areAllUsesEqual(Instruction
*I
) {
626 Value::user_iterator UI
= I
->user_begin();
627 Value::user_iterator UE
= I
->user_end();
632 for (++UI
; UI
!= UE
; ++UI
) {
639 /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
640 /// dead PHI node, due to being a def-use chain of single-use nodes that
641 /// either forms a cycle or is terminated by a trivially dead instruction,
642 /// delete it. If that makes any of its operands trivially dead, delete them
643 /// too, recursively. Return true if a change was made.
644 bool llvm::RecursivelyDeleteDeadPHINode(PHINode
*PN
,
645 const TargetLibraryInfo
*TLI
,
646 llvm::MemorySSAUpdater
*MSSAU
) {
647 SmallPtrSet
<Instruction
*, 4> Visited
;
648 for (Instruction
*I
= PN
; areAllUsesEqual(I
) && !I
->mayHaveSideEffects();
649 I
= cast
<Instruction
>(*I
->user_begin())) {
651 return RecursivelyDeleteTriviallyDeadInstructions(I
, TLI
, MSSAU
);
653 // If we find an instruction more than once, we're on a cycle that
654 // won't prove fruitful.
655 if (!Visited
.insert(I
).second
) {
656 // Break the cycle and delete the instruction and its operands.
657 I
->replaceAllUsesWith(PoisonValue::get(I
->getType()));
658 (void)RecursivelyDeleteTriviallyDeadInstructions(I
, TLI
, MSSAU
);
666 simplifyAndDCEInstruction(Instruction
*I
,
667 SmallSetVector
<Instruction
*, 16> &WorkList
,
668 const DataLayout
&DL
,
669 const TargetLibraryInfo
*TLI
) {
670 if (isInstructionTriviallyDead(I
, TLI
)) {
671 salvageDebugInfo(*I
);
673 // Null out all of the instruction's operands to see if any operand becomes
675 for (unsigned i
= 0, e
= I
->getNumOperands(); i
!= e
; ++i
) {
676 Value
*OpV
= I
->getOperand(i
);
677 I
->setOperand(i
, nullptr);
679 if (!OpV
->use_empty() || I
== OpV
)
682 // If the operand is an instruction that became dead as we nulled out the
683 // operand, and if it is 'trivially' dead, delete it in a future loop
685 if (Instruction
*OpI
= dyn_cast
<Instruction
>(OpV
))
686 if (isInstructionTriviallyDead(OpI
, TLI
))
687 WorkList
.insert(OpI
);
690 I
->eraseFromParent();
695 if (Value
*SimpleV
= simplifyInstruction(I
, DL
)) {
696 // Add the users to the worklist. CAREFUL: an instruction can use itself,
697 // in the case of a phi node.
698 for (User
*U
: I
->users()) {
700 WorkList
.insert(cast
<Instruction
>(U
));
704 // Replace the instruction with its simplified value.
705 bool Changed
= false;
706 if (!I
->use_empty()) {
707 I
->replaceAllUsesWith(SimpleV
);
710 if (isInstructionTriviallyDead(I
, TLI
)) {
711 I
->eraseFromParent();
719 /// SimplifyInstructionsInBlock - Scan the specified basic block and try to
720 /// simplify any instructions in it and recursively delete dead instructions.
722 /// This returns true if it changed the code, note that it can delete
723 /// instructions in other blocks as well in this block.
724 bool llvm::SimplifyInstructionsInBlock(BasicBlock
*BB
,
725 const TargetLibraryInfo
*TLI
) {
726 bool MadeChange
= false;
727 const DataLayout
&DL
= BB
->getModule()->getDataLayout();
730 // In debug builds, ensure that the terminator of the block is never replaced
731 // or deleted by these simplifications. The idea of simplification is that it
732 // cannot introduce new instructions, and there is no way to replace the
733 // terminator of a block without introducing a new instruction.
734 AssertingVH
<Instruction
> TerminatorVH(&BB
->back());
737 SmallSetVector
<Instruction
*, 16> WorkList
;
738 // Iterate over the original function, only adding insts to the worklist
739 // if they actually need to be revisited. This avoids having to pre-init
740 // the worklist with the entire function's worth of instructions.
741 for (BasicBlock::iterator BI
= BB
->begin(), E
= std::prev(BB
->end());
743 assert(!BI
->isTerminator());
744 Instruction
*I
= &*BI
;
747 // We're visiting this instruction now, so make sure it's not in the
748 // worklist from an earlier visit.
749 if (!WorkList
.count(I
))
750 MadeChange
|= simplifyAndDCEInstruction(I
, WorkList
, DL
, TLI
);
753 while (!WorkList
.empty()) {
754 Instruction
*I
= WorkList
.pop_back_val();
755 MadeChange
|= simplifyAndDCEInstruction(I
, WorkList
, DL
, TLI
);
760 //===----------------------------------------------------------------------===//
761 // Control Flow Graph Restructuring.
764 void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock
*DestBB
,
765 DomTreeUpdater
*DTU
) {
767 // If BB has single-entry PHI nodes, fold them.
768 while (PHINode
*PN
= dyn_cast
<PHINode
>(DestBB
->begin())) {
769 Value
*NewVal
= PN
->getIncomingValue(0);
770 // Replace self referencing PHI with poison, it must be dead.
771 if (NewVal
== PN
) NewVal
= PoisonValue::get(PN
->getType());
772 PN
->replaceAllUsesWith(NewVal
);
773 PN
->eraseFromParent();
776 BasicBlock
*PredBB
= DestBB
->getSinglePredecessor();
777 assert(PredBB
&& "Block doesn't have a single predecessor!");
779 bool ReplaceEntryBB
= PredBB
->isEntryBlock();
781 // DTU updates: Collect all the edges that enter
782 // PredBB. These dominator edges will be redirected to DestBB.
783 SmallVector
<DominatorTree::UpdateType
, 32> Updates
;
786 // To avoid processing the same predecessor more than once.
787 SmallPtrSet
<BasicBlock
*, 2> SeenPreds
;
788 Updates
.reserve(Updates
.size() + 2 * pred_size(PredBB
) + 1);
789 for (BasicBlock
*PredOfPredBB
: predecessors(PredBB
))
790 // This predecessor of PredBB may already have DestBB as a successor.
791 if (PredOfPredBB
!= PredBB
)
792 if (SeenPreds
.insert(PredOfPredBB
).second
)
793 Updates
.push_back({DominatorTree::Insert
, PredOfPredBB
, DestBB
});
795 for (BasicBlock
*PredOfPredBB
: predecessors(PredBB
))
796 if (SeenPreds
.insert(PredOfPredBB
).second
)
797 Updates
.push_back({DominatorTree::Delete
, PredOfPredBB
, PredBB
});
798 Updates
.push_back({DominatorTree::Delete
, PredBB
, DestBB
});
801 // Zap anything that took the address of DestBB. Not doing this will give the
802 // address an invalid value.
803 if (DestBB
->hasAddressTaken()) {
804 BlockAddress
*BA
= BlockAddress::get(DestBB
);
805 Constant
*Replacement
=
806 ConstantInt::get(Type::getInt32Ty(BA
->getContext()), 1);
807 BA
->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement
,
809 BA
->destroyConstant();
812 // Anything that branched to PredBB now branches to DestBB.
813 PredBB
->replaceAllUsesWith(DestBB
);
815 // Splice all the instructions from PredBB to DestBB.
816 PredBB
->getTerminator()->eraseFromParent();
817 DestBB
->splice(DestBB
->begin(), PredBB
);
818 new UnreachableInst(PredBB
->getContext(), PredBB
);
820 // If the PredBB is the entry block of the function, move DestBB up to
821 // become the entry block after we erase PredBB.
823 DestBB
->moveAfter(PredBB
);
826 assert(PredBB
->size() == 1 &&
827 isa
<UnreachableInst
>(PredBB
->getTerminator()) &&
828 "The successor list of PredBB isn't empty before "
829 "applying corresponding DTU updates.");
830 DTU
->applyUpdatesPermissive(Updates
);
831 DTU
->deleteBB(PredBB
);
832 // Recalculation of DomTree is needed when updating a forward DomTree and
833 // the Entry BB is replaced.
834 if (ReplaceEntryBB
&& DTU
->hasDomTree()) {
835 // The entry block was removed and there is no external interface for
836 // the dominator tree to be notified of this change. In this corner-case
837 // we recalculate the entire tree.
838 DTU
->recalculate(*(DestBB
->getParent()));
843 PredBB
->eraseFromParent(); // Nuke BB if DTU is nullptr.
847 /// Return true if we can choose one of these values to use in place of the
848 /// other. Note that we will always choose the non-undef value to keep.
849 static bool CanMergeValues(Value
*First
, Value
*Second
) {
850 return First
== Second
|| isa
<UndefValue
>(First
) || isa
<UndefValue
>(Second
);
853 /// Return true if we can fold BB, an almost-empty BB ending in an unconditional
854 /// branch to Succ, into Succ.
856 /// Assumption: Succ is the single successor for BB.
858 CanPropagatePredecessorsForPHIs(BasicBlock
*BB
, BasicBlock
*Succ
,
859 const SmallPtrSetImpl
<BasicBlock
*> &BBPreds
) {
860 assert(*succ_begin(BB
) == Succ
&& "Succ is not successor of BB!");
862 LLVM_DEBUG(dbgs() << "Looking to fold " << BB
->getName() << " into "
863 << Succ
->getName() << "\n");
864 // Shortcut, if there is only a single predecessor it must be BB and merging
866 if (Succ
->getSinglePredecessor())
869 // Look at all the phi nodes in Succ, to see if they present a conflict when
870 // merging these blocks
871 for (BasicBlock::iterator I
= Succ
->begin(); isa
<PHINode
>(I
); ++I
) {
872 PHINode
*PN
= cast
<PHINode
>(I
);
874 // If the incoming value from BB is again a PHINode in
875 // BB which has the same incoming value for *PI as PN does, we can
876 // merge the phi nodes and then the blocks can still be merged
877 PHINode
*BBPN
= dyn_cast
<PHINode
>(PN
->getIncomingValueForBlock(BB
));
878 if (BBPN
&& BBPN
->getParent() == BB
) {
879 for (unsigned PI
= 0, PE
= PN
->getNumIncomingValues(); PI
!= PE
; ++PI
) {
880 BasicBlock
*IBB
= PN
->getIncomingBlock(PI
);
881 if (BBPreds
.count(IBB
) &&
882 !CanMergeValues(BBPN
->getIncomingValueForBlock(IBB
),
883 PN
->getIncomingValue(PI
))) {
885 << "Can't fold, phi node " << PN
->getName() << " in "
886 << Succ
->getName() << " is conflicting with "
887 << BBPN
->getName() << " with regard to common predecessor "
888 << IBB
->getName() << "\n");
893 Value
* Val
= PN
->getIncomingValueForBlock(BB
);
894 for (unsigned PI
= 0, PE
= PN
->getNumIncomingValues(); PI
!= PE
; ++PI
) {
895 // See if the incoming value for the common predecessor is equal to the
896 // one for BB, in which case this phi node will not prevent the merging
898 BasicBlock
*IBB
= PN
->getIncomingBlock(PI
);
899 if (BBPreds
.count(IBB
) &&
900 !CanMergeValues(Val
, PN
->getIncomingValue(PI
))) {
901 LLVM_DEBUG(dbgs() << "Can't fold, phi node " << PN
->getName()
902 << " in " << Succ
->getName()
903 << " is conflicting with regard to common "
904 << "predecessor " << IBB
->getName() << "\n");
914 using PredBlockVector
= SmallVector
<BasicBlock
*, 16>;
915 using IncomingValueMap
= DenseMap
<BasicBlock
*, Value
*>;
917 /// Determines the value to use as the phi node input for a block.
919 /// Select between \p OldVal any value that we know flows from \p BB
920 /// to a particular phi on the basis of which one (if either) is not
921 /// undef. Update IncomingValues based on the selected value.
923 /// \param OldVal The value we are considering selecting.
924 /// \param BB The block that the value flows in from.
925 /// \param IncomingValues A map from block-to-value for other phi inputs
926 /// that we have examined.
928 /// \returns the selected value.
929 static Value
*selectIncomingValueForBlock(Value
*OldVal
, BasicBlock
*BB
,
930 IncomingValueMap
&IncomingValues
) {
931 if (!isa
<UndefValue
>(OldVal
)) {
932 assert((!IncomingValues
.count(BB
) ||
933 IncomingValues
.find(BB
)->second
== OldVal
) &&
934 "Expected OldVal to match incoming value from BB!");
936 IncomingValues
.insert(std::make_pair(BB
, OldVal
));
940 IncomingValueMap::const_iterator It
= IncomingValues
.find(BB
);
941 if (It
!= IncomingValues
.end()) return It
->second
;
946 /// Create a map from block to value for the operands of a
949 /// Create a map from block to value for each non-undef value flowing
952 /// \param PN The phi we are collecting the map for.
953 /// \param IncomingValues [out] The map from block to value for this phi.
954 static void gatherIncomingValuesToPhi(PHINode
*PN
,
955 IncomingValueMap
&IncomingValues
) {
956 for (unsigned i
= 0, e
= PN
->getNumIncomingValues(); i
!= e
; ++i
) {
957 BasicBlock
*BB
= PN
->getIncomingBlock(i
);
958 Value
*V
= PN
->getIncomingValue(i
);
960 if (!isa
<UndefValue
>(V
))
961 IncomingValues
.insert(std::make_pair(BB
, V
));
965 /// Replace the incoming undef values to a phi with the values
966 /// from a block-to-value map.
968 /// \param PN The phi we are replacing the undefs in.
969 /// \param IncomingValues A map from block to value.
970 static void replaceUndefValuesInPhi(PHINode
*PN
,
971 const IncomingValueMap
&IncomingValues
) {
972 SmallVector
<unsigned> TrueUndefOps
;
973 for (unsigned i
= 0, e
= PN
->getNumIncomingValues(); i
!= e
; ++i
) {
974 Value
*V
= PN
->getIncomingValue(i
);
976 if (!isa
<UndefValue
>(V
)) continue;
978 BasicBlock
*BB
= PN
->getIncomingBlock(i
);
979 IncomingValueMap::const_iterator It
= IncomingValues
.find(BB
);
981 // Keep track of undef/poison incoming values. Those must match, so we fix
982 // them up below if needed.
983 // Note: this is conservatively correct, but we could try harder and group
984 // the undef values per incoming basic block.
985 if (It
== IncomingValues
.end()) {
986 TrueUndefOps
.push_back(i
);
990 // There is a defined value for this incoming block, so map this undef
991 // incoming value to the defined value.
992 PN
->setIncomingValue(i
, It
->second
);
995 // If there are both undef and poison values incoming, then convert those
996 // values to undef. It is invalid to have different values for the same
998 unsigned PoisonCount
= count_if(TrueUndefOps
, [&](unsigned i
) {
999 return isa
<PoisonValue
>(PN
->getIncomingValue(i
));
1001 if (PoisonCount
!= 0 && PoisonCount
!= TrueUndefOps
.size()) {
1002 for (unsigned i
: TrueUndefOps
)
1003 PN
->setIncomingValue(i
, UndefValue::get(PN
->getType()));
1007 // Only when they shares a single common predecessor, return true.
1008 // Only handles cases when BB can't be merged while its predecessors can be
1011 CanRedirectPredsOfEmptyBBToSucc(BasicBlock
*BB
, BasicBlock
*Succ
,
1012 const SmallPtrSetImpl
<BasicBlock
*> &BBPreds
,
1013 const SmallPtrSetImpl
<BasicBlock
*> &SuccPreds
,
1014 BasicBlock
*&CommonPred
) {
1016 // There must be phis in BB, otherwise BB will be merged into Succ directly
1017 if (BB
->phis().empty() || Succ
->phis().empty())
1020 // BB must have predecessors not shared that can be redirected to Succ
1021 if (!BB
->hasNPredecessorsOrMore(2))
1024 // Get single common predecessors of both BB and Succ
1025 for (BasicBlock
*SuccPred
: SuccPreds
) {
1026 if (BBPreds
.count(SuccPred
)) {
1029 CommonPred
= SuccPred
;
1036 /// Replace a value flowing from a block to a phi with
1037 /// potentially multiple instances of that value flowing from the
1038 /// block's predecessors to the phi.
1040 /// \param BB The block with the value flowing into the phi.
1041 /// \param BBPreds The predecessors of BB.
1042 /// \param PN The phi that we are updating.
1043 /// \param CommonPred The common predecessor of BB and PN's BasicBlock
1044 static void redirectValuesFromPredecessorsToPhi(BasicBlock
*BB
,
1045 const PredBlockVector
&BBPreds
,
1047 BasicBlock
*CommonPred
) {
1048 Value
*OldVal
= PN
->removeIncomingValue(BB
, false);
1049 assert(OldVal
&& "No entry in PHI for Pred BB!");
1051 IncomingValueMap IncomingValues
;
1053 // We are merging two blocks - BB, and the block containing PN - and
1054 // as a result we need to redirect edges from the predecessors of BB
1055 // to go to the block containing PN, and update PN
1056 // accordingly. Since we allow merging blocks in the case where the
1057 // predecessor and successor blocks both share some predecessors,
1058 // and where some of those common predecessors might have undef
1059 // values flowing into PN, we want to rewrite those values to be
1060 // consistent with the non-undef values.
1062 gatherIncomingValuesToPhi(PN
, IncomingValues
);
1064 // If this incoming value is one of the PHI nodes in BB, the new entries
1065 // in the PHI node are the entries from the old PHI.
1066 if (isa
<PHINode
>(OldVal
) && cast
<PHINode
>(OldVal
)->getParent() == BB
) {
1067 PHINode
*OldValPN
= cast
<PHINode
>(OldVal
);
1068 for (unsigned i
= 0, e
= OldValPN
->getNumIncomingValues(); i
!= e
; ++i
) {
1069 // Note that, since we are merging phi nodes and BB and Succ might
1070 // have common predecessors, we could end up with a phi node with
1071 // identical incoming branches. This will be cleaned up later (and
1072 // will trigger asserts if we try to clean it up now, without also
1073 // simplifying the corresponding conditional branch).
1074 BasicBlock
*PredBB
= OldValPN
->getIncomingBlock(i
);
1076 if (PredBB
== CommonPred
)
1079 Value
*PredVal
= OldValPN
->getIncomingValue(i
);
1081 selectIncomingValueForBlock(PredVal
, PredBB
, IncomingValues
);
1083 // And add a new incoming value for this predecessor for the
1084 // newly retargeted branch.
1085 PN
->addIncoming(Selected
, PredBB
);
1088 PN
->addIncoming(OldValPN
->getIncomingValueForBlock(CommonPred
), BB
);
1091 for (unsigned i
= 0, e
= BBPreds
.size(); i
!= e
; ++i
) {
1092 // Update existing incoming values in PN for this
1093 // predecessor of BB.
1094 BasicBlock
*PredBB
= BBPreds
[i
];
1096 if (PredBB
== CommonPred
)
1100 selectIncomingValueForBlock(OldVal
, PredBB
, IncomingValues
);
1102 // And add a new incoming value for this predecessor for the
1103 // newly retargeted branch.
1104 PN
->addIncoming(Selected
, PredBB
);
1107 PN
->addIncoming(OldVal
, BB
);
1110 replaceUndefValuesInPhi(PN
, IncomingValues
);
1113 bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock
*BB
,
1114 DomTreeUpdater
*DTU
) {
1115 assert(BB
!= &BB
->getParent()->getEntryBlock() &&
1116 "TryToSimplifyUncondBranchFromEmptyBlock called on entry block!");
1118 // We can't simplify infinite loops.
1119 BasicBlock
*Succ
= cast
<BranchInst
>(BB
->getTerminator())->getSuccessor(0);
1123 SmallPtrSet
<BasicBlock
*, 16> BBPreds(pred_begin(BB
), pred_end(BB
));
1124 SmallPtrSet
<BasicBlock
*, 16> SuccPreds(pred_begin(Succ
), pred_end(Succ
));
1126 // The single common predecessor of BB and Succ when BB cannot be killed
1127 BasicBlock
*CommonPred
= nullptr;
1129 bool BBKillable
= CanPropagatePredecessorsForPHIs(BB
, Succ
, BBPreds
);
1131 // Even if we can not fold bB into Succ, we may be able to redirect the
1132 // predecessors of BB to Succ.
1133 bool BBPhisMergeable
=
1135 CanRedirectPredsOfEmptyBBToSucc(BB
, Succ
, BBPreds
, SuccPreds
, CommonPred
);
1137 if (!BBKillable
&& !BBPhisMergeable
)
1140 // Check to see if merging these blocks/phis would cause conflicts for any of
1141 // the phi nodes in BB or Succ. If not, we can safely merge.
1143 // Check for cases where Succ has multiple predecessors and a PHI node in BB
1144 // has uses which will not disappear when the PHI nodes are merged. It is
1145 // possible to handle such cases, but difficult: it requires checking whether
1146 // BB dominates Succ, which is non-trivial to calculate in the case where
1147 // Succ has multiple predecessors. Also, it requires checking whether
1148 // constructing the necessary self-referential PHI node doesn't introduce any
1149 // conflicts; this isn't too difficult, but the previous code for doing this
1152 // Note that if this check finds a live use, BB dominates Succ, so BB is
1153 // something like a loop pre-header (or rarely, a part of an irreducible CFG);
1154 // folding the branch isn't profitable in that case anyway.
1155 if (!Succ
->getSinglePredecessor()) {
1156 BasicBlock::iterator BBI
= BB
->begin();
1157 while (isa
<PHINode
>(*BBI
)) {
1158 for (Use
&U
: BBI
->uses()) {
1159 if (PHINode
* PN
= dyn_cast
<PHINode
>(U
.getUser())) {
1160 if (PN
->getIncomingBlock(U
) != BB
)
1170 if (BBPhisMergeable
&& CommonPred
)
1171 LLVM_DEBUG(dbgs() << "Found Common Predecessor between: " << BB
->getName()
1172 << " and " << Succ
->getName() << " : "
1173 << CommonPred
->getName() << "\n");
1175 // 'BB' and 'BB->Pred' are loop latches, bail out to presrve inner loop
1178 // FIXME: This is a stop-gap solution to preserve inner-loop metadata given
1179 // current status (that loop metadata is implemented as metadata attached to
1180 // the branch instruction in the loop latch block). To quote from review
1181 // comments, "the current representation of loop metadata (using a loop latch
1182 // terminator attachment) is known to be fundamentally broken. Loop latches
1183 // are not uniquely associated with loops (both in that a latch can be part of
1184 // multiple loops and a loop may have multiple latches). Loop headers are. The
1185 // solution to this problem is also known: Add support for basic block
1186 // metadata, and attach loop metadata to the loop header."
1189 // In this case, we expect 'BB' is the latch for outer-loop and 'BB->Pred' is
1190 // the latch for inner-loop (see reason below), so bail out to prerserve
1191 // inner-loop metadata rather than eliminating 'BB' and attaching its metadata
1192 // to this inner-loop.
1193 // - The reason we believe 'BB' and 'BB->Pred' have different inner-most
1194 // loops: assuming 'BB' and 'BB->Pred' are from the same inner-most loop L,
1195 // then 'BB' is the header and latch of 'L' and thereby 'L' must consist of
1196 // one self-looping basic block, which is contradictory with the assumption.
1198 // To illustrate how inner-loop metadata is dropped:
1202 // BB is while.cond.exit, attached with loop metdata md2.
1203 // BB->Pred is for.body, attached with loop metadata md1.
1208 // ---> while.cond -------------> while.end
1214 // | for.body <---- (md1)
1217 // | while.cond.exit (md2)
1223 // while.cond1 is the merge of while.cond.exit and while.cond above.
1224 // for.body is attached with md2, and md1 is dropped.
1225 // If LoopSimplify runs later (as a part of loop pass), it could create
1226 // dedicated exits for inner-loop (essentially adding `while.cond.exit`
1227 // back), but won't it won't see 'md1' nor restore it for the inner-loop.
1232 // ---> while.cond1 -------------> while.end
1238 // | for.body <---- (md2)
1239 // |_______| |______|
1240 if (Instruction
*TI
= BB
->getTerminator())
1241 if (TI
->hasMetadata(LLVMContext::MD_loop
))
1242 for (BasicBlock
*Pred
: predecessors(BB
))
1243 if (Instruction
*PredTI
= Pred
->getTerminator())
1244 if (PredTI
->hasMetadata(LLVMContext::MD_loop
))
1248 LLVM_DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB
);
1249 else if (BBPhisMergeable
)
1250 LLVM_DEBUG(dbgs() << "Merge Phis in Trivial BB: \n" << *BB
);
1252 SmallVector
<DominatorTree::UpdateType
, 32> Updates
;
1255 // To avoid processing the same predecessor more than once.
1256 SmallPtrSet
<BasicBlock
*, 8> SeenPreds
;
1257 // All predecessors of BB (except the common predecessor) will be moved to
1259 Updates
.reserve(Updates
.size() + 2 * pred_size(BB
) + 1);
1261 for (auto *PredOfBB
: predecessors(BB
)) {
1262 // Do not modify those common predecessors of BB and Succ
1263 if (!SuccPreds
.contains(PredOfBB
))
1264 if (SeenPreds
.insert(PredOfBB
).second
)
1265 Updates
.push_back({DominatorTree::Insert
, PredOfBB
, Succ
});
1270 for (auto *PredOfBB
: predecessors(BB
))
1271 // When BB cannot be killed, do not remove the edge between BB and
1273 if (SeenPreds
.insert(PredOfBB
).second
&& PredOfBB
!= CommonPred
)
1274 Updates
.push_back({DominatorTree::Delete
, PredOfBB
, BB
});
1277 Updates
.push_back({DominatorTree::Delete
, BB
, Succ
});
1280 if (isa
<PHINode
>(Succ
->begin())) {
1281 // If there is more than one pred of succ, and there are PHI nodes in
1282 // the successor, then we need to add incoming edges for the PHI nodes
1284 const PredBlockVector
BBPreds(predecessors(BB
));
1286 // Loop over all of the PHI nodes in the successor of BB.
1287 for (BasicBlock::iterator I
= Succ
->begin(); isa
<PHINode
>(I
); ++I
) {
1288 PHINode
*PN
= cast
<PHINode
>(I
);
1289 redirectValuesFromPredecessorsToPhi(BB
, BBPreds
, PN
, CommonPred
);
1293 if (Succ
->getSinglePredecessor()) {
1294 // BB is the only predecessor of Succ, so Succ will end up with exactly
1295 // the same predecessors BB had.
1296 // Copy over any phi, debug or lifetime instruction.
1297 BB
->getTerminator()->eraseFromParent();
1298 Succ
->splice(Succ
->getFirstNonPHIIt(), BB
);
1300 while (PHINode
*PN
= dyn_cast
<PHINode
>(&BB
->front())) {
1301 // We explicitly check for such uses for merging phis.
1302 assert(PN
->use_empty() && "There shouldn't be any uses here!");
1303 PN
->eraseFromParent();
1307 // If the unconditional branch we replaced contains llvm.loop metadata, we
1308 // add the metadata to the branch instructions in the predecessors.
1309 if (Instruction
*TI
= BB
->getTerminator())
1310 if (MDNode
*LoopMD
= TI
->getMetadata(LLVMContext::MD_loop
))
1311 for (BasicBlock
*Pred
: predecessors(BB
))
1312 Pred
->getTerminator()->setMetadata(LLVMContext::MD_loop
, LoopMD
);
1315 // Everything that jumped to BB now goes to Succ.
1316 BB
->replaceAllUsesWith(Succ
);
1318 if (!Succ
->hasName())
1321 // Clear the successor list of BB to match updates applying to DTU later.
1322 if (BB
->getTerminator())
1323 BB
->back().eraseFromParent();
1325 new UnreachableInst(BB
->getContext(), BB
);
1326 assert(succ_empty(BB
) && "The successor list of BB isn't empty before "
1327 "applying corresponding DTU updates.");
1328 } else if (BBPhisMergeable
) {
1329 // Everything except CommonPred that jumped to BB now goes to Succ.
1330 BB
->replaceUsesWithIf(Succ
, [BBPreds
, CommonPred
](Use
&U
) -> bool {
1331 if (Instruction
*UseInst
= dyn_cast
<Instruction
>(U
.getUser()))
1332 return UseInst
->getParent() != CommonPred
&&
1333 BBPreds
.contains(UseInst
->getParent());
1339 DTU
->applyUpdates(Updates
);
1342 DeleteDeadBlock(BB
, DTU
);
1348 EliminateDuplicatePHINodesNaiveImpl(BasicBlock
*BB
,
1349 SmallPtrSetImpl
<PHINode
*> &ToRemove
) {
1350 // This implementation doesn't currently consider undef operands
1351 // specially. Theoretically, two phis which are identical except for
1352 // one having an undef where the other doesn't could be collapsed.
1354 bool Changed
= false;
1356 // Examine each PHI.
1357 // Note that increment of I must *NOT* be in the iteration_expression, since
1358 // we don't want to immediately advance when we restart from the beginning.
1359 for (auto I
= BB
->begin(); PHINode
*PN
= dyn_cast
<PHINode
>(I
);) {
1361 // Is there an identical PHI node in this basic block?
1362 // Note that we only look in the upper square's triangle,
1363 // we already checked that the lower triangle PHI's aren't identical.
1364 for (auto J
= I
; PHINode
*DuplicatePN
= dyn_cast
<PHINode
>(J
); ++J
) {
1365 if (ToRemove
.contains(DuplicatePN
))
1367 if (!DuplicatePN
->isIdenticalToWhenDefined(PN
))
1369 // A duplicate. Replace this PHI with the base PHI.
1371 DuplicatePN
->replaceAllUsesWith(PN
);
1372 ToRemove
.insert(DuplicatePN
);
1375 // The RAUW can change PHIs that we already visited.
1377 break; // Start over from the beginning.
1384 EliminateDuplicatePHINodesSetBasedImpl(BasicBlock
*BB
,
1385 SmallPtrSetImpl
<PHINode
*> &ToRemove
) {
1386 // This implementation doesn't currently consider undef operands
1387 // specially. Theoretically, two phis which are identical except for
1388 // one having an undef where the other doesn't could be collapsed.
1390 struct PHIDenseMapInfo
{
1391 static PHINode
*getEmptyKey() {
1392 return DenseMapInfo
<PHINode
*>::getEmptyKey();
1395 static PHINode
*getTombstoneKey() {
1396 return DenseMapInfo
<PHINode
*>::getTombstoneKey();
1399 static bool isSentinel(PHINode
*PN
) {
1400 return PN
== getEmptyKey() || PN
== getTombstoneKey();
1403 // WARNING: this logic must be kept in sync with
1404 // Instruction::isIdenticalToWhenDefined()!
1405 static unsigned getHashValueImpl(PHINode
*PN
) {
1406 // Compute a hash value on the operands. Instcombine will likely have
1407 // sorted them, which helps expose duplicates, but we have to check all
1408 // the operands to be safe in case instcombine hasn't run.
1409 return static_cast<unsigned>(hash_combine(
1410 hash_combine_range(PN
->value_op_begin(), PN
->value_op_end()),
1411 hash_combine_range(PN
->block_begin(), PN
->block_end())));
1414 static unsigned getHashValue(PHINode
*PN
) {
1416 // If -phicse-debug-hash was specified, return a constant -- this
1417 // will force all hashing to collide, so we'll exhaustively search
1418 // the table for a match, and the assertion in isEqual will fire if
1419 // there's a bug causing equal keys to hash differently.
1420 if (PHICSEDebugHash
)
1423 return getHashValueImpl(PN
);
1426 static bool isEqualImpl(PHINode
*LHS
, PHINode
*RHS
) {
1427 if (isSentinel(LHS
) || isSentinel(RHS
))
1429 return LHS
->isIdenticalTo(RHS
);
1432 static bool isEqual(PHINode
*LHS
, PHINode
*RHS
) {
1433 // These comparisons are nontrivial, so assert that equality implies
1434 // hash equality (DenseMap demands this as an invariant).
1435 bool Result
= isEqualImpl(LHS
, RHS
);
1436 assert(!Result
|| (isSentinel(LHS
) && LHS
== RHS
) ||
1437 getHashValueImpl(LHS
) == getHashValueImpl(RHS
));
1442 // Set of unique PHINodes.
1443 DenseSet
<PHINode
*, PHIDenseMapInfo
> PHISet
;
1444 PHISet
.reserve(4 * PHICSENumPHISmallSize
);
1446 // Examine each PHI.
1447 bool Changed
= false;
1448 for (auto I
= BB
->begin(); PHINode
*PN
= dyn_cast
<PHINode
>(I
++);) {
1449 if (ToRemove
.contains(PN
))
1451 auto Inserted
= PHISet
.insert(PN
);
1452 if (!Inserted
.second
) {
1453 // A duplicate. Replace this PHI with its duplicate.
1455 PN
->replaceAllUsesWith(*Inserted
.first
);
1456 ToRemove
.insert(PN
);
1459 // The RAUW can change PHIs that we already visited. Start over from the
1469 bool llvm::EliminateDuplicatePHINodes(BasicBlock
*BB
,
1470 SmallPtrSetImpl
<PHINode
*> &ToRemove
) {
1475 hasNItemsOrLess(BB
->phis(), PHICSENumPHISmallSize
))
1476 return EliminateDuplicatePHINodesNaiveImpl(BB
, ToRemove
);
1477 return EliminateDuplicatePHINodesSetBasedImpl(BB
, ToRemove
);
1480 bool llvm::EliminateDuplicatePHINodes(BasicBlock
*BB
) {
1481 SmallPtrSet
<PHINode
*, 8> ToRemove
;
1482 bool Changed
= EliminateDuplicatePHINodes(BB
, ToRemove
);
1483 for (PHINode
*PN
: ToRemove
)
1484 PN
->eraseFromParent();
1488 Align
llvm::tryEnforceAlignment(Value
*V
, Align PrefAlign
,
1489 const DataLayout
&DL
) {
1490 V
= V
->stripPointerCasts();
1492 if (AllocaInst
*AI
= dyn_cast
<AllocaInst
>(V
)) {
1493 // TODO: Ideally, this function would not be called if PrefAlign is smaller
1494 // than the current alignment, as the known bits calculation should have
1495 // already taken it into account. However, this is not always the case,
1496 // as computeKnownBits() has a depth limit, while stripPointerCasts()
1498 Align CurrentAlign
= AI
->getAlign();
1499 if (PrefAlign
<= CurrentAlign
)
1500 return CurrentAlign
;
1502 // If the preferred alignment is greater than the natural stack alignment
1503 // then don't round up. This avoids dynamic stack realignment.
1504 if (DL
.exceedsNaturalStackAlignment(PrefAlign
))
1505 return CurrentAlign
;
1506 AI
->setAlignment(PrefAlign
);
1510 if (auto *GO
= dyn_cast
<GlobalObject
>(V
)) {
1511 // TODO: as above, this shouldn't be necessary.
1512 Align CurrentAlign
= GO
->getPointerAlignment(DL
);
1513 if (PrefAlign
<= CurrentAlign
)
1514 return CurrentAlign
;
1516 // If there is a large requested alignment and we can, bump up the alignment
1517 // of the global. If the memory we set aside for the global may not be the
1518 // memory used by the final program then it is impossible for us to reliably
1519 // enforce the preferred alignment.
1520 if (!GO
->canIncreaseAlignment())
1521 return CurrentAlign
;
1523 if (GO
->isThreadLocal()) {
1524 unsigned MaxTLSAlign
= GO
->getParent()->getMaxTLSAlignment() / CHAR_BIT
;
1525 if (MaxTLSAlign
&& PrefAlign
> Align(MaxTLSAlign
))
1526 PrefAlign
= Align(MaxTLSAlign
);
1529 GO
->setAlignment(PrefAlign
);
1536 Align
llvm::getOrEnforceKnownAlignment(Value
*V
, MaybeAlign PrefAlign
,
1537 const DataLayout
&DL
,
1538 const Instruction
*CxtI
,
1539 AssumptionCache
*AC
,
1540 const DominatorTree
*DT
) {
1541 assert(V
->getType()->isPointerTy() &&
1542 "getOrEnforceKnownAlignment expects a pointer!");
1544 KnownBits Known
= computeKnownBits(V
, DL
, 0, AC
, CxtI
, DT
);
1545 unsigned TrailZ
= Known
.countMinTrailingZeros();
1547 // Avoid trouble with ridiculously large TrailZ values, such as
1548 // those computed from a null pointer.
1549 // LLVM doesn't support alignments larger than (1 << MaxAlignmentExponent).
1550 TrailZ
= std::min(TrailZ
, +Value::MaxAlignmentExponent
);
1552 Align Alignment
= Align(1ull << std::min(Known
.getBitWidth() - 1, TrailZ
));
1554 if (PrefAlign
&& *PrefAlign
> Alignment
)
1555 Alignment
= std::max(Alignment
, tryEnforceAlignment(V
, *PrefAlign
, DL
));
1557 // We don't need to make any adjustment.
1561 ///===---------------------------------------------------------------------===//
1562 /// Dbg Intrinsic utilities
1565 /// See if there is a dbg.value intrinsic for DIVar for the PHI node.
1566 static bool PhiHasDebugValue(DILocalVariable
*DIVar
,
1567 DIExpression
*DIExpr
,
1569 // Since we can't guarantee that the original dbg.declare intrinsic
1570 // is removed by LowerDbgDeclare(), we need to make sure that we are
1571 // not inserting the same dbg.value intrinsic over and over.
1572 SmallVector
<DbgValueInst
*, 1> DbgValues
;
1573 SmallVector
<DPValue
*, 1> DPValues
;
1574 findDbgValues(DbgValues
, APN
, &DPValues
);
1575 for (auto *DVI
: DbgValues
) {
1576 assert(is_contained(DVI
->getValues(), APN
));
1577 if ((DVI
->getVariable() == DIVar
) && (DVI
->getExpression() == DIExpr
))
1580 for (auto *DPV
: DPValues
) {
1581 assert(is_contained(DPV
->location_ops(), APN
));
1582 if ((DPV
->getVariable() == DIVar
) && (DPV
->getExpression() == DIExpr
))
1588 /// Check if the alloc size of \p ValTy is large enough to cover the variable
1589 /// (or fragment of the variable) described by \p DII.
1591 /// This is primarily intended as a helper for the different
1592 /// ConvertDebugDeclareToDebugValue functions. The dbg.declare that is converted
1593 /// describes an alloca'd variable, so we need to use the alloc size of the
1594 /// value when doing the comparison. E.g. an i1 value will be identified as
1595 /// covering an n-bit fragment, if the store size of i1 is at least n bits.
1596 static bool valueCoversEntireFragment(Type
*ValTy
, DbgVariableIntrinsic
*DII
) {
1597 const DataLayout
&DL
= DII
->getModule()->getDataLayout();
1598 TypeSize ValueSize
= DL
.getTypeAllocSizeInBits(ValTy
);
1599 if (std::optional
<uint64_t> FragmentSize
= DII
->getFragmentSizeInBits())
1600 return TypeSize::isKnownGE(ValueSize
, TypeSize::getFixed(*FragmentSize
));
1602 // We can't always calculate the size of the DI variable (e.g. if it is a
1603 // VLA). Try to use the size of the alloca that the dbg intrinsic describes
1605 if (DII
->isAddressOfVariable()) {
1606 // DII should have exactly 1 location when it is an address.
1607 assert(DII
->getNumVariableLocationOps() == 1 &&
1608 "address of variable must have exactly 1 location operand.");
1610 dyn_cast_or_null
<AllocaInst
>(DII
->getVariableLocationOp(0))) {
1611 if (std::optional
<TypeSize
> FragmentSize
=
1612 AI
->getAllocationSizeInBits(DL
)) {
1613 return TypeSize::isKnownGE(ValueSize
, *FragmentSize
);
1617 // Could not determine size of variable. Conservatively return false.
1620 // RemoveDIs: duplicate implementation of the above, using DPValues, the
1621 // replacement for dbg.values.
1622 static bool valueCoversEntireFragment(Type
*ValTy
, DPValue
*DPV
) {
1623 const DataLayout
&DL
= DPV
->getModule()->getDataLayout();
1624 TypeSize ValueSize
= DL
.getTypeAllocSizeInBits(ValTy
);
1625 if (std::optional
<uint64_t> FragmentSize
= DPV
->getFragmentSizeInBits())
1626 return TypeSize::isKnownGE(ValueSize
, TypeSize::getFixed(*FragmentSize
));
1628 // We can't always calculate the size of the DI variable (e.g. if it is a
1629 // VLA). Try to use the size of the alloca that the dbg intrinsic describes
1631 if (DPV
->isAddressOfVariable()) {
1632 // DPV should have exactly 1 location when it is an address.
1633 assert(DPV
->getNumVariableLocationOps() == 1 &&
1634 "address of variable must have exactly 1 location operand.");
1636 dyn_cast_or_null
<AllocaInst
>(DPV
->getVariableLocationOp(0))) {
1637 if (std::optional
<TypeSize
> FragmentSize
= AI
->getAllocationSizeInBits(DL
)) {
1638 return TypeSize::isKnownGE(ValueSize
, *FragmentSize
);
1642 // Could not determine size of variable. Conservatively return false.
1646 static void insertDbgValueOrDPValue(DIBuilder
&Builder
, Value
*DV
,
1647 DILocalVariable
*DIVar
,
1648 DIExpression
*DIExpr
,
1649 const DebugLoc
&NewLoc
,
1650 BasicBlock::iterator Instr
) {
1651 if (!UseNewDbgInfoFormat
) {
1652 auto *DbgVal
= Builder
.insertDbgValueIntrinsic(DV
, DIVar
, DIExpr
, NewLoc
,
1653 (Instruction
*)nullptr);
1654 DbgVal
->insertBefore(Instr
);
1656 // RemoveDIs: if we're using the new debug-info format, allocate a
1657 // DPValue directly instead of a dbg.value intrinsic.
1658 ValueAsMetadata
*DVAM
= ValueAsMetadata::get(DV
);
1659 DPValue
*DV
= new DPValue(DVAM
, DIVar
, DIExpr
, NewLoc
.get());
1660 Instr
->getParent()->insertDPValueBefore(DV
, Instr
);
1664 static void insertDbgValueOrDPValueAfter(DIBuilder
&Builder
, Value
*DV
,
1665 DILocalVariable
*DIVar
,
1666 DIExpression
*DIExpr
,
1667 const DebugLoc
&NewLoc
,
1668 BasicBlock::iterator Instr
) {
1669 if (!UseNewDbgInfoFormat
) {
1670 auto *DbgVal
= Builder
.insertDbgValueIntrinsic(DV
, DIVar
, DIExpr
, NewLoc
,
1671 (Instruction
*)nullptr);
1672 DbgVal
->insertAfter(&*Instr
);
1674 // RemoveDIs: if we're using the new debug-info format, allocate a
1675 // DPValue directly instead of a dbg.value intrinsic.
1676 ValueAsMetadata
*DVAM
= ValueAsMetadata::get(DV
);
1677 DPValue
*DV
= new DPValue(DVAM
, DIVar
, DIExpr
, NewLoc
.get());
1678 Instr
->getParent()->insertDPValueAfter(DV
, &*Instr
);
1682 /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
1683 /// that has an associated llvm.dbg.declare intrinsic.
1684 void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic
*DII
,
1685 StoreInst
*SI
, DIBuilder
&Builder
) {
1686 assert(DII
->isAddressOfVariable() || isa
<DbgAssignIntrinsic
>(DII
));
1687 auto *DIVar
= DII
->getVariable();
1688 assert(DIVar
&& "Missing variable");
1689 auto *DIExpr
= DII
->getExpression();
1690 Value
*DV
= SI
->getValueOperand();
1692 DebugLoc NewLoc
= getDebugValueLoc(DII
);
1694 // If the alloca describes the variable itself, i.e. the expression in the
1695 // dbg.declare doesn't start with a dereference, we can perform the
1696 // conversion if the value covers the entire fragment of DII.
1697 // If the alloca describes the *address* of DIVar, i.e. DIExpr is
1698 // *just* a DW_OP_deref, we use DV as is for the dbg.value.
1699 // We conservatively ignore other dereferences, because the following two are
1701 // dbg.declare(alloca, ..., !Expr(deref, plus_uconstant, 2))
1702 // dbg.value(DV, ..., !Expr(deref, plus_uconstant, 2))
1703 // The former is adding 2 to the address of the variable, whereas the latter
1704 // is adding 2 to the value of the variable. As such, we insist on just a
1705 // deref expression.
1707 DIExpr
->isDeref() || (!DIExpr
->startsWithDeref() &&
1708 valueCoversEntireFragment(DV
->getType(), DII
));
1710 insertDbgValueOrDPValue(Builder
, DV
, DIVar
, DIExpr
, NewLoc
,
1715 // FIXME: If storing to a part of the variable described by the dbg.declare,
1716 // then we want to insert a dbg.value for the corresponding fragment.
1717 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: " << *DII
1719 // For now, when there is a store to parts of the variable (but we do not
1720 // know which part) we insert an dbg.value intrinsic to indicate that we
1721 // know nothing about the variable's content.
1722 DV
= UndefValue::get(DV
->getType());
1723 insertDbgValueOrDPValue(Builder
, DV
, DIVar
, DIExpr
, NewLoc
,
1728 // RemoveDIs: duplicate the getDebugValueLoc method using DPValues instead of
1729 // dbg.value intrinsics. In llvm namespace so that it overloads the
1730 // DbgVariableIntrinsic version.
1731 static DebugLoc
getDebugValueLoc(DPValue
*DPV
) {
1732 // Original dbg.declare must have a location.
1733 const DebugLoc
&DeclareLoc
= DPV
->getDebugLoc();
1734 MDNode
*Scope
= DeclareLoc
.getScope();
1735 DILocation
*InlinedAt
= DeclareLoc
.getInlinedAt();
1736 // Produce an unknown location with the correct scope / inlinedAt fields.
1737 return DILocation::get(DPV
->getContext(), 0, 0, Scope
, InlinedAt
);
1741 /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
1742 /// that has an associated llvm.dbg.declare intrinsic.
1743 void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic
*DII
,
1744 LoadInst
*LI
, DIBuilder
&Builder
) {
1745 auto *DIVar
= DII
->getVariable();
1746 auto *DIExpr
= DII
->getExpression();
1747 assert(DIVar
&& "Missing variable");
1749 if (!valueCoversEntireFragment(LI
->getType(), DII
)) {
1750 // FIXME: If only referring to a part of the variable described by the
1751 // dbg.declare, then we want to insert a dbg.value for the corresponding
1753 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: "
1758 DebugLoc NewLoc
= getDebugValueLoc(DII
);
1760 // We are now tracking the loaded value instead of the address. In the
1761 // future if multi-location support is added to the IR, it might be
1762 // preferable to keep tracking both the loaded value and the original
1763 // address in case the alloca can not be elided.
1764 insertDbgValueOrDPValueAfter(Builder
, LI
, DIVar
, DIExpr
, NewLoc
,
1768 void llvm::ConvertDebugDeclareToDebugValue(DPValue
*DPV
, StoreInst
*SI
,
1769 DIBuilder
&Builder
) {
1770 assert(DPV
->isAddressOfVariable());
1771 auto *DIVar
= DPV
->getVariable();
1772 assert(DIVar
&& "Missing variable");
1773 auto *DIExpr
= DPV
->getExpression();
1774 Value
*DV
= SI
->getValueOperand();
1776 DebugLoc NewLoc
= getDebugValueLoc(DPV
);
1778 // If the alloca describes the variable itself, i.e. the expression in the
1779 // dbg.declare doesn't start with a dereference, we can perform the
1780 // conversion if the value covers the entire fragment of DII.
1781 // If the alloca describes the *address* of DIVar, i.e. DIExpr is
1782 // *just* a DW_OP_deref, we use DV as is for the dbg.value.
1783 // We conservatively ignore other dereferences, because the following two are
1785 // dbg.declare(alloca, ..., !Expr(deref, plus_uconstant, 2))
1786 // dbg.value(DV, ..., !Expr(deref, plus_uconstant, 2))
1787 // The former is adding 2 to the address of the variable, whereas the latter
1788 // is adding 2 to the value of the variable. As such, we insist on just a
1789 // deref expression.
1791 DIExpr
->isDeref() || (!DIExpr
->startsWithDeref() &&
1792 valueCoversEntireFragment(DV
->getType(), DPV
));
1794 insertDbgValueOrDPValue(Builder
, DV
, DIVar
, DIExpr
, NewLoc
,
1799 // FIXME: If storing to a part of the variable described by the dbg.declare,
1800 // then we want to insert a dbg.value for the corresponding fragment.
1801 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: " << *DPV
1803 assert(UseNewDbgInfoFormat
);
1805 // For now, when there is a store to parts of the variable (but we do not
1806 // know which part) we insert an dbg.value intrinsic to indicate that we
1807 // know nothing about the variable's content.
1808 DV
= UndefValue::get(DV
->getType());
1809 ValueAsMetadata
*DVAM
= ValueAsMetadata::get(DV
);
1810 DPValue
*NewDPV
= new DPValue(DVAM
, DIVar
, DIExpr
, NewLoc
.get());
1811 SI
->getParent()->insertDPValueBefore(NewDPV
, SI
->getIterator());
1814 /// Inserts a llvm.dbg.value intrinsic after a phi that has an associated
1815 /// llvm.dbg.declare intrinsic.
1816 void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic
*DII
,
1817 PHINode
*APN
, DIBuilder
&Builder
) {
1818 auto *DIVar
= DII
->getVariable();
1819 auto *DIExpr
= DII
->getExpression();
1820 assert(DIVar
&& "Missing variable");
1822 if (PhiHasDebugValue(DIVar
, DIExpr
, APN
))
1825 if (!valueCoversEntireFragment(APN
->getType(), DII
)) {
1826 // FIXME: If only referring to a part of the variable described by the
1827 // dbg.declare, then we want to insert a dbg.value for the corresponding
1829 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: "
1834 BasicBlock
*BB
= APN
->getParent();
1835 auto InsertionPt
= BB
->getFirstInsertionPt();
1837 DebugLoc NewLoc
= getDebugValueLoc(DII
);
1839 // The block may be a catchswitch block, which does not have a valid
1841 // FIXME: Insert dbg.value markers in the successors when appropriate.
1842 if (InsertionPt
!= BB
->end()) {
1843 insertDbgValueOrDPValue(Builder
, APN
, DIVar
, DIExpr
, NewLoc
, InsertionPt
);
1847 void llvm::ConvertDebugDeclareToDebugValue(DPValue
*DPV
, LoadInst
*LI
,
1848 DIBuilder
&Builder
) {
1849 auto *DIVar
= DPV
->getVariable();
1850 auto *DIExpr
= DPV
->getExpression();
1851 assert(DIVar
&& "Missing variable");
1853 if (!valueCoversEntireFragment(LI
->getType(), DPV
)) {
1854 // FIXME: If only referring to a part of the variable described by the
1855 // dbg.declare, then we want to insert a DPValue for the corresponding
1857 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to DPValue: " << *DPV
1862 DebugLoc NewLoc
= getDebugValueLoc(DPV
);
1864 // We are now tracking the loaded value instead of the address. In the
1865 // future if multi-location support is added to the IR, it might be
1866 // preferable to keep tracking both the loaded value and the original
1867 // address in case the alloca can not be elided.
1868 assert(UseNewDbgInfoFormat
);
1870 // Create a DPValue directly and insert.
1871 ValueAsMetadata
*LIVAM
= ValueAsMetadata::get(LI
);
1872 DPValue
*DV
= new DPValue(LIVAM
, DIVar
, DIExpr
, NewLoc
.get());
1873 LI
->getParent()->insertDPValueAfter(DV
, LI
);
1876 /// Determine whether this alloca is either a VLA or an array.
1877 static bool isArray(AllocaInst
*AI
) {
1878 return AI
->isArrayAllocation() ||
1879 (AI
->getAllocatedType() && AI
->getAllocatedType()->isArrayTy());
1882 /// Determine whether this alloca is a structure.
1883 static bool isStructure(AllocaInst
*AI
) {
1884 return AI
->getAllocatedType() && AI
->getAllocatedType()->isStructTy();
1886 void llvm::ConvertDebugDeclareToDebugValue(DPValue
*DPV
, PHINode
*APN
,
1887 DIBuilder
&Builder
) {
1888 auto *DIVar
= DPV
->getVariable();
1889 auto *DIExpr
= DPV
->getExpression();
1890 assert(DIVar
&& "Missing variable");
1892 if (PhiHasDebugValue(DIVar
, DIExpr
, APN
))
1895 if (!valueCoversEntireFragment(APN
->getType(), DPV
)) {
1896 // FIXME: If only referring to a part of the variable described by the
1897 // dbg.declare, then we want to insert a DPValue for the corresponding
1899 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to DPValue: " << *DPV
1904 BasicBlock
*BB
= APN
->getParent();
1905 auto InsertionPt
= BB
->getFirstInsertionPt();
1907 DebugLoc NewLoc
= getDebugValueLoc(DPV
);
1909 // The block may be a catchswitch block, which does not have a valid
1911 // FIXME: Insert DPValue markers in the successors when appropriate.
1912 if (InsertionPt
!= BB
->end()) {
1913 insertDbgValueOrDPValue(Builder
, APN
, DIVar
, DIExpr
, NewLoc
, InsertionPt
);
1917 /// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set
1918 /// of llvm.dbg.value intrinsics.
1919 bool llvm::LowerDbgDeclare(Function
&F
) {
1920 bool Changed
= false;
1921 DIBuilder
DIB(*F
.getParent(), /*AllowUnresolved*/ false);
1922 SmallVector
<DbgDeclareInst
*, 4> Dbgs
;
1923 SmallVector
<DPValue
*> DPVs
;
1924 for (auto &FI
: F
) {
1925 for (Instruction
&BI
: FI
) {
1926 if (auto *DDI
= dyn_cast
<DbgDeclareInst
>(&BI
))
1927 Dbgs
.push_back(DDI
);
1928 for (DPValue
&DPV
: BI
.getDbgValueRange()) {
1929 if (DPV
.getType() == DPValue::LocationType::Declare
)
1930 DPVs
.push_back(&DPV
);
1935 if (Dbgs
.empty() && DPVs
.empty())
1938 auto LowerOne
= [&](auto *DDI
) {
1940 dyn_cast_or_null
<AllocaInst
>(DDI
->getVariableLocationOp(0));
1941 // If this is an alloca for a scalar variable, insert a dbg.value
1942 // at each load and store to the alloca and erase the dbg.declare.
1943 // The dbg.values allow tracking a variable even if it is not
1944 // stored on the stack, while the dbg.declare can only describe
1945 // the stack slot (and at a lexical-scope granularity). Later
1946 // passes will attempt to elide the stack slot.
1947 if (!AI
|| isArray(AI
) || isStructure(AI
))
1950 // A volatile load/store means that the alloca can't be elided anyway.
1951 if (llvm::any_of(AI
->users(), [](User
*U
) -> bool {
1952 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(U
))
1953 return LI
->isVolatile();
1954 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(U
))
1955 return SI
->isVolatile();
1960 SmallVector
<const Value
*, 8> WorkList
;
1961 WorkList
.push_back(AI
);
1962 while (!WorkList
.empty()) {
1963 const Value
*V
= WorkList
.pop_back_val();
1964 for (const auto &AIUse
: V
->uses()) {
1965 User
*U
= AIUse
.getUser();
1966 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(U
)) {
1967 if (AIUse
.getOperandNo() == 1)
1968 ConvertDebugDeclareToDebugValue(DDI
, SI
, DIB
);
1969 } else if (LoadInst
*LI
= dyn_cast
<LoadInst
>(U
)) {
1970 ConvertDebugDeclareToDebugValue(DDI
, LI
, DIB
);
1971 } else if (CallInst
*CI
= dyn_cast
<CallInst
>(U
)) {
1972 // This is a call by-value or some other instruction that takes a
1973 // pointer to the variable. Insert a *value* intrinsic that describes
1974 // the variable by dereferencing the alloca.
1975 if (!CI
->isLifetimeStartOrEnd()) {
1976 DebugLoc NewLoc
= getDebugValueLoc(DDI
);
1978 DIExpression::append(DDI
->getExpression(), dwarf::DW_OP_deref
);
1979 insertDbgValueOrDPValue(DIB
, AI
, DDI
->getVariable(), DerefExpr
,
1980 NewLoc
, CI
->getIterator());
1982 } else if (BitCastInst
*BI
= dyn_cast
<BitCastInst
>(U
)) {
1983 if (BI
->getType()->isPointerTy())
1984 WorkList
.push_back(BI
);
1988 DDI
->eraseFromParent();
1992 for_each(Dbgs
, LowerOne
);
1993 for_each(DPVs
, LowerOne
);
1996 for (BasicBlock
&BB
: F
)
1997 RemoveRedundantDbgInstrs(&BB
);
2002 // RemoveDIs: re-implementation of insertDebugValuesForPHIs, but which pulls the
2003 // debug-info out of the block's DPValues rather than dbg.value intrinsics.
2004 static void insertDPValuesForPHIs(BasicBlock
*BB
,
2005 SmallVectorImpl
<PHINode
*> &InsertedPHIs
) {
2006 assert(BB
&& "No BasicBlock to clone DPValue(s) from.");
2007 if (InsertedPHIs
.size() == 0)
2010 // Map existing PHI nodes to their DPValues.
2011 DenseMap
<Value
*, DPValue
*> DbgValueMap
;
2012 for (auto &I
: *BB
) {
2013 for (auto &DPV
: I
.getDbgValueRange()) {
2014 for (Value
*V
: DPV
.location_ops())
2015 if (auto *Loc
= dyn_cast_or_null
<PHINode
>(V
))
2016 DbgValueMap
.insert({Loc
, &DPV
});
2019 if (DbgValueMap
.size() == 0)
2022 // Map a pair of the destination BB and old DPValue to the new DPValue,
2023 // so that if a DPValue is being rewritten to use more than one of the
2024 // inserted PHIs in the same destination BB, we can update the same DPValue
2025 // with all the new PHIs instead of creating one copy for each.
2026 MapVector
<std::pair
<BasicBlock
*, DPValue
*>, DPValue
*> NewDbgValueMap
;
2027 // Then iterate through the new PHIs and look to see if they use one of the
2028 // previously mapped PHIs. If so, create a new DPValue that will propagate
2029 // the info through the new PHI. If we use more than one new PHI in a single
2030 // destination BB with the same old dbg.value, merge the updates so that we
2031 // get a single new DPValue with all the new PHIs.
2032 for (auto PHI
: InsertedPHIs
) {
2033 BasicBlock
*Parent
= PHI
->getParent();
2034 // Avoid inserting a debug-info record into an EH block.
2035 if (Parent
->getFirstNonPHI()->isEHPad())
2037 for (auto VI
: PHI
->operand_values()) {
2038 auto V
= DbgValueMap
.find(VI
);
2039 if (V
!= DbgValueMap
.end()) {
2040 DPValue
*DbgII
= cast
<DPValue
>(V
->second
);
2041 auto NewDI
= NewDbgValueMap
.find({Parent
, DbgII
});
2042 if (NewDI
== NewDbgValueMap
.end()) {
2043 DPValue
*NewDbgII
= DbgII
->clone();
2044 NewDI
= NewDbgValueMap
.insert({{Parent
, DbgII
}, NewDbgII
}).first
;
2046 DPValue
*NewDbgII
= NewDI
->second
;
2047 // If PHI contains VI as an operand more than once, we may
2048 // replaced it in NewDbgII; confirm that it is present.
2049 if (is_contained(NewDbgII
->location_ops(), VI
))
2050 NewDbgII
->replaceVariableLocationOp(VI
, PHI
);
2054 // Insert the new DPValues into their destination blocks.
2055 for (auto DI
: NewDbgValueMap
) {
2056 BasicBlock
*Parent
= DI
.first
.first
;
2057 DPValue
*NewDbgII
= DI
.second
;
2058 auto InsertionPt
= Parent
->getFirstInsertionPt();
2059 assert(InsertionPt
!= Parent
->end() && "Ill-formed basic block");
2061 InsertionPt
->DbgMarker
->insertDPValue(NewDbgII
, true);
2065 /// Propagate dbg.value intrinsics through the newly inserted PHIs.
2066 void llvm::insertDebugValuesForPHIs(BasicBlock
*BB
,
2067 SmallVectorImpl
<PHINode
*> &InsertedPHIs
) {
2068 assert(BB
&& "No BasicBlock to clone dbg.value(s) from.");
2069 if (InsertedPHIs
.size() == 0)
2072 insertDPValuesForPHIs(BB
, InsertedPHIs
);
2074 // Map existing PHI nodes to their dbg.values.
2075 ValueToValueMapTy DbgValueMap
;
2076 for (auto &I
: *BB
) {
2077 if (auto DbgII
= dyn_cast
<DbgVariableIntrinsic
>(&I
)) {
2078 for (Value
*V
: DbgII
->location_ops())
2079 if (auto *Loc
= dyn_cast_or_null
<PHINode
>(V
))
2080 DbgValueMap
.insert({Loc
, DbgII
});
2083 if (DbgValueMap
.size() == 0)
2086 // Map a pair of the destination BB and old dbg.value to the new dbg.value,
2087 // so that if a dbg.value is being rewritten to use more than one of the
2088 // inserted PHIs in the same destination BB, we can update the same dbg.value
2089 // with all the new PHIs instead of creating one copy for each.
2090 MapVector
<std::pair
<BasicBlock
*, DbgVariableIntrinsic
*>,
2091 DbgVariableIntrinsic
*>
2093 // Then iterate through the new PHIs and look to see if they use one of the
2094 // previously mapped PHIs. If so, create a new dbg.value intrinsic that will
2095 // propagate the info through the new PHI. If we use more than one new PHI in
2096 // a single destination BB with the same old dbg.value, merge the updates so
2097 // that we get a single new dbg.value with all the new PHIs.
2098 for (auto *PHI
: InsertedPHIs
) {
2099 BasicBlock
*Parent
= PHI
->getParent();
2100 // Avoid inserting an intrinsic into an EH block.
2101 if (Parent
->getFirstNonPHI()->isEHPad())
2103 for (auto *VI
: PHI
->operand_values()) {
2104 auto V
= DbgValueMap
.find(VI
);
2105 if (V
!= DbgValueMap
.end()) {
2106 auto *DbgII
= cast
<DbgVariableIntrinsic
>(V
->second
);
2107 auto NewDI
= NewDbgValueMap
.find({Parent
, DbgII
});
2108 if (NewDI
== NewDbgValueMap
.end()) {
2109 auto *NewDbgII
= cast
<DbgVariableIntrinsic
>(DbgII
->clone());
2110 NewDI
= NewDbgValueMap
.insert({{Parent
, DbgII
}, NewDbgII
}).first
;
2112 DbgVariableIntrinsic
*NewDbgII
= NewDI
->second
;
2113 // If PHI contains VI as an operand more than once, we may
2114 // replaced it in NewDbgII; confirm that it is present.
2115 if (is_contained(NewDbgII
->location_ops(), VI
))
2116 NewDbgII
->replaceVariableLocationOp(VI
, PHI
);
2120 // Insert thew new dbg.values into their destination blocks.
2121 for (auto DI
: NewDbgValueMap
) {
2122 BasicBlock
*Parent
= DI
.first
.first
;
2123 auto *NewDbgII
= DI
.second
;
2124 auto InsertionPt
= Parent
->getFirstInsertionPt();
2125 assert(InsertionPt
!= Parent
->end() && "Ill-formed basic block");
2126 NewDbgII
->insertBefore(&*InsertionPt
);
2130 bool llvm::replaceDbgDeclare(Value
*Address
, Value
*NewAddress
,
2131 DIBuilder
&Builder
, uint8_t DIExprFlags
,
2133 TinyPtrVector
<DbgDeclareInst
*> DbgDeclares
= findDbgDeclares(Address
);
2134 TinyPtrVector
<DPValue
*> DPVDeclares
= findDPVDeclares(Address
);
2136 auto ReplaceOne
= [&](auto *DII
) {
2137 assert(DII
->getVariable() && "Missing variable");
2138 auto *DIExpr
= DII
->getExpression();
2139 DIExpr
= DIExpression::prepend(DIExpr
, DIExprFlags
, Offset
);
2140 DII
->setExpression(DIExpr
);
2141 DII
->replaceVariableLocationOp(Address
, NewAddress
);
2144 for_each(DbgDeclares
, ReplaceOne
);
2145 for_each(DPVDeclares
, ReplaceOne
);
2147 return !DbgDeclares
.empty() || !DPVDeclares
.empty();
2150 static void updateOneDbgValueForAlloca(const DebugLoc
&Loc
,
2151 DILocalVariable
*DIVar
,
2152 DIExpression
*DIExpr
, Value
*NewAddress
,
2153 DbgValueInst
*DVI
, DPValue
*DPV
,
2154 DIBuilder
&Builder
, int Offset
) {
2155 assert(DIVar
&& "Missing variable");
2157 // This is an alloca-based dbg.value/DPValue. The first thing it should do
2158 // with the alloca pointer is dereference it. Otherwise we don't know how to
2159 // handle it and give up.
2160 if (!DIExpr
|| DIExpr
->getNumElements() < 1 ||
2161 DIExpr
->getElement(0) != dwarf::DW_OP_deref
)
2164 // Insert the offset before the first deref.
2166 DIExpr
= DIExpression::prepend(DIExpr
, 0, Offset
);
2169 DVI
->setExpression(DIExpr
);
2170 DVI
->replaceVariableLocationOp(0u, NewAddress
);
2173 DPV
->setExpression(DIExpr
);
2174 DPV
->replaceVariableLocationOp(0u, NewAddress
);
2178 void llvm::replaceDbgValueForAlloca(AllocaInst
*AI
, Value
*NewAllocaAddress
,
2179 DIBuilder
&Builder
, int Offset
) {
2180 SmallVector
<DbgValueInst
*, 1> DbgUsers
;
2181 SmallVector
<DPValue
*, 1> DPUsers
;
2182 findDbgValues(DbgUsers
, AI
, &DPUsers
);
2184 // Attempt to replace dbg.values that use this alloca.
2185 for (auto *DVI
: DbgUsers
)
2186 updateOneDbgValueForAlloca(DVI
->getDebugLoc(), DVI
->getVariable(),
2187 DVI
->getExpression(), NewAllocaAddress
, DVI
,
2188 nullptr, Builder
, Offset
);
2190 // Replace any DPValues that use this alloca.
2191 for (DPValue
*DPV
: DPUsers
)
2192 updateOneDbgValueForAlloca(DPV
->getDebugLoc(), DPV
->getVariable(),
2193 DPV
->getExpression(), NewAllocaAddress
, nullptr,
2194 DPV
, Builder
, Offset
);
2197 /// Where possible to salvage debug information for \p I do so.
2198 /// If not possible mark undef.
2199 void llvm::salvageDebugInfo(Instruction
&I
) {
2200 SmallVector
<DbgVariableIntrinsic
*, 1> DbgUsers
;
2201 SmallVector
<DPValue
*, 1> DPUsers
;
2202 findDbgUsers(DbgUsers
, &I
, &DPUsers
);
2203 salvageDebugInfoForDbgValues(I
, DbgUsers
, DPUsers
);
2206 /// Salvage the address component of \p DAI.
2207 static void salvageDbgAssignAddress(DbgAssignIntrinsic
*DAI
) {
2208 Instruction
*I
= dyn_cast
<Instruction
>(DAI
->getAddress());
2209 // Only instructions can be salvaged at the moment.
2213 assert(!DAI
->getAddressExpression()->getFragmentInfo().has_value() &&
2214 "address-expression shouldn't have fragment info");
2216 // The address component of a dbg.assign cannot be variadic.
2217 uint64_t CurrentLocOps
= 0;
2218 SmallVector
<Value
*, 4> AdditionalValues
;
2219 SmallVector
<uint64_t, 16> Ops
;
2220 Value
*NewV
= salvageDebugInfoImpl(*I
, CurrentLocOps
, Ops
, AdditionalValues
);
2222 // Check if the salvage failed.
2226 DIExpression
*SalvagedExpr
= DIExpression::appendOpsToArg(
2227 DAI
->getAddressExpression(), Ops
, 0, /*StackValue=*/false);
2228 assert(!SalvagedExpr
->getFragmentInfo().has_value() &&
2229 "address-expression shouldn't have fragment info");
2231 // Salvage succeeds if no additional values are required.
2232 if (AdditionalValues
.empty()) {
2233 DAI
->setAddress(NewV
);
2234 DAI
->setAddressExpression(SalvagedExpr
);
2236 DAI
->setKillAddress();
2240 void llvm::salvageDebugInfoForDbgValues(
2241 Instruction
&I
, ArrayRef
<DbgVariableIntrinsic
*> DbgUsers
,
2242 ArrayRef
<DPValue
*> DPUsers
) {
2243 // These are arbitrary chosen limits on the maximum number of values and the
2244 // maximum size of a debug expression we can salvage up to, used for
2245 // performance reasons.
2246 const unsigned MaxDebugArgs
= 16;
2247 const unsigned MaxExpressionSize
= 128;
2248 bool Salvaged
= false;
2250 for (auto *DII
: DbgUsers
) {
2251 if (auto *DAI
= dyn_cast
<DbgAssignIntrinsic
>(DII
)) {
2252 if (DAI
->getAddress() == &I
) {
2253 salvageDbgAssignAddress(DAI
);
2256 if (DAI
->getValue() != &I
)
2260 // Do not add DW_OP_stack_value for DbgDeclare, because they are implicitly
2261 // pointing out the value as a DWARF memory location description.
2262 bool StackValue
= isa
<DbgValueInst
>(DII
);
2263 auto DIILocation
= DII
->location_ops();
2265 is_contained(DIILocation
, &I
) &&
2266 "DbgVariableIntrinsic must use salvaged instruction as its location");
2267 SmallVector
<Value
*, 4> AdditionalValues
;
2268 // `I` may appear more than once in DII's location ops, and each use of `I`
2269 // must be updated in the DIExpression and potentially have additional
2270 // values added; thus we call salvageDebugInfoImpl for each `I` instance in
2272 Value
*Op0
= nullptr;
2273 DIExpression
*SalvagedExpr
= DII
->getExpression();
2274 auto LocItr
= find(DIILocation
, &I
);
2275 while (SalvagedExpr
&& LocItr
!= DIILocation
.end()) {
2276 SmallVector
<uint64_t, 16> Ops
;
2277 unsigned LocNo
= std::distance(DIILocation
.begin(), LocItr
);
2278 uint64_t CurrentLocOps
= SalvagedExpr
->getNumLocationOperands();
2279 Op0
= salvageDebugInfoImpl(I
, CurrentLocOps
, Ops
, AdditionalValues
);
2283 DIExpression::appendOpsToArg(SalvagedExpr
, Ops
, LocNo
, StackValue
);
2284 LocItr
= std::find(++LocItr
, DIILocation
.end(), &I
);
2286 // salvageDebugInfoImpl should fail on examining the first element of
2287 // DbgUsers, or none of them.
2291 DII
->replaceVariableLocationOp(&I
, Op0
);
2292 bool IsValidSalvageExpr
= SalvagedExpr
->getNumElements() <= MaxExpressionSize
;
2293 if (AdditionalValues
.empty() && IsValidSalvageExpr
) {
2294 DII
->setExpression(SalvagedExpr
);
2295 } else if (isa
<DbgValueInst
>(DII
) && IsValidSalvageExpr
&&
2296 DII
->getNumVariableLocationOps() + AdditionalValues
.size() <=
2298 DII
->addVariableLocationOps(AdditionalValues
, SalvagedExpr
);
2300 // Do not salvage using DIArgList for dbg.declare, as it is not currently
2301 // supported in those instructions. Also do not salvage if the resulting
2302 // DIArgList would contain an unreasonably large number of values.
2303 DII
->setKillLocation();
2305 LLVM_DEBUG(dbgs() << "SALVAGE: " << *DII
<< '\n');
2308 // Duplicate of above block for DPValues.
2309 for (auto *DPV
: DPUsers
) {
2310 // Do not add DW_OP_stack_value for DbgDeclare and DbgAddr, because they
2311 // are implicitly pointing out the value as a DWARF memory location
2313 bool StackValue
= DPV
->getType() == DPValue::LocationType::Value
;
2314 auto DPVLocation
= DPV
->location_ops();
2316 is_contained(DPVLocation
, &I
) &&
2317 "DbgVariableIntrinsic must use salvaged instruction as its location");
2318 SmallVector
<Value
*, 4> AdditionalValues
;
2319 // 'I' may appear more than once in DPV's location ops, and each use of 'I'
2320 // must be updated in the DIExpression and potentially have additional
2321 // values added; thus we call salvageDebugInfoImpl for each 'I' instance in
2323 Value
*Op0
= nullptr;
2324 DIExpression
*SalvagedExpr
= DPV
->getExpression();
2325 auto LocItr
= find(DPVLocation
, &I
);
2326 while (SalvagedExpr
&& LocItr
!= DPVLocation
.end()) {
2327 SmallVector
<uint64_t, 16> Ops
;
2328 unsigned LocNo
= std::distance(DPVLocation
.begin(), LocItr
);
2329 uint64_t CurrentLocOps
= SalvagedExpr
->getNumLocationOperands();
2330 Op0
= salvageDebugInfoImpl(I
, CurrentLocOps
, Ops
, AdditionalValues
);
2334 DIExpression::appendOpsToArg(SalvagedExpr
, Ops
, LocNo
, StackValue
);
2335 LocItr
= std::find(++LocItr
, DPVLocation
.end(), &I
);
2337 // salvageDebugInfoImpl should fail on examining the first element of
2338 // DbgUsers, or none of them.
2342 DPV
->replaceVariableLocationOp(&I
, Op0
);
2343 bool IsValidSalvageExpr
=
2344 SalvagedExpr
->getNumElements() <= MaxExpressionSize
;
2345 if (AdditionalValues
.empty() && IsValidSalvageExpr
) {
2346 DPV
->setExpression(SalvagedExpr
);
2347 } else if (DPV
->getType() == DPValue::LocationType::Value
&&
2348 IsValidSalvageExpr
&&
2349 DPV
->getNumVariableLocationOps() + AdditionalValues
.size() <=
2351 DPV
->addVariableLocationOps(AdditionalValues
, SalvagedExpr
);
2353 // Do not salvage using DIArgList for dbg.addr/dbg.declare, as it is
2354 // currently only valid for stack value expressions.
2355 // Also do not salvage if the resulting DIArgList would contain an
2356 // unreasonably large number of values.
2357 Value
*Undef
= UndefValue::get(I
.getOperand(0)->getType());
2358 DPV
->replaceVariableLocationOp(I
.getOperand(0), Undef
);
2360 LLVM_DEBUG(dbgs() << "SALVAGE: " << DPV
<< '\n');
2367 for (auto *DII
: DbgUsers
)
2368 DII
->setKillLocation();
2370 for (auto *DPV
: DPUsers
)
2371 DPV
->setKillLocation();
2374 Value
*getSalvageOpsForGEP(GetElementPtrInst
*GEP
, const DataLayout
&DL
,
2375 uint64_t CurrentLocOps
,
2376 SmallVectorImpl
<uint64_t> &Opcodes
,
2377 SmallVectorImpl
<Value
*> &AdditionalValues
) {
2378 unsigned BitWidth
= DL
.getIndexSizeInBits(GEP
->getPointerAddressSpace());
2379 // Rewrite a GEP into a DIExpression.
2380 MapVector
<Value
*, APInt
> VariableOffsets
;
2381 APInt
ConstantOffset(BitWidth
, 0);
2382 if (!GEP
->collectOffset(DL
, BitWidth
, VariableOffsets
, ConstantOffset
))
2384 if (!VariableOffsets
.empty() && !CurrentLocOps
) {
2385 Opcodes
.insert(Opcodes
.begin(), {dwarf::DW_OP_LLVM_arg
, 0});
2388 for (const auto &Offset
: VariableOffsets
) {
2389 AdditionalValues
.push_back(Offset
.first
);
2390 assert(Offset
.second
.isStrictlyPositive() &&
2391 "Expected strictly positive multiplier for offset.");
2392 Opcodes
.append({dwarf::DW_OP_LLVM_arg
, CurrentLocOps
++, dwarf::DW_OP_constu
,
2393 Offset
.second
.getZExtValue(), dwarf::DW_OP_mul
,
2394 dwarf::DW_OP_plus
});
2396 DIExpression::appendOffset(Opcodes
, ConstantOffset
.getSExtValue());
2397 return GEP
->getOperand(0);
2400 uint64_t getDwarfOpForBinOp(Instruction::BinaryOps Opcode
) {
2402 case Instruction::Add
:
2403 return dwarf::DW_OP_plus
;
2404 case Instruction::Sub
:
2405 return dwarf::DW_OP_minus
;
2406 case Instruction::Mul
:
2407 return dwarf::DW_OP_mul
;
2408 case Instruction::SDiv
:
2409 return dwarf::DW_OP_div
;
2410 case Instruction::SRem
:
2411 return dwarf::DW_OP_mod
;
2412 case Instruction::Or
:
2413 return dwarf::DW_OP_or
;
2414 case Instruction::And
:
2415 return dwarf::DW_OP_and
;
2416 case Instruction::Xor
:
2417 return dwarf::DW_OP_xor
;
2418 case Instruction::Shl
:
2419 return dwarf::DW_OP_shl
;
2420 case Instruction::LShr
:
2421 return dwarf::DW_OP_shr
;
2422 case Instruction::AShr
:
2423 return dwarf::DW_OP_shra
;
2425 // TODO: Salvage from each kind of binop we know about.
2430 static void handleSSAValueOperands(uint64_t CurrentLocOps
,
2431 SmallVectorImpl
<uint64_t> &Opcodes
,
2432 SmallVectorImpl
<Value
*> &AdditionalValues
,
2434 if (!CurrentLocOps
) {
2435 Opcodes
.append({dwarf::DW_OP_LLVM_arg
, 0});
2438 Opcodes
.append({dwarf::DW_OP_LLVM_arg
, CurrentLocOps
});
2439 AdditionalValues
.push_back(I
->getOperand(1));
2442 Value
*getSalvageOpsForBinOp(BinaryOperator
*BI
, uint64_t CurrentLocOps
,
2443 SmallVectorImpl
<uint64_t> &Opcodes
,
2444 SmallVectorImpl
<Value
*> &AdditionalValues
) {
2445 // Handle binary operations with constant integer operands as a special case.
2446 auto *ConstInt
= dyn_cast
<ConstantInt
>(BI
->getOperand(1));
2447 // Values wider than 64 bits cannot be represented within a DIExpression.
2448 if (ConstInt
&& ConstInt
->getBitWidth() > 64)
2451 Instruction::BinaryOps BinOpcode
= BI
->getOpcode();
2452 // Push any Constant Int operand onto the expression stack.
2454 uint64_t Val
= ConstInt
->getSExtValue();
2455 // Add or Sub Instructions with a constant operand can potentially be
2457 if (BinOpcode
== Instruction::Add
|| BinOpcode
== Instruction::Sub
) {
2458 uint64_t Offset
= BinOpcode
== Instruction::Add
? Val
: -int64_t(Val
);
2459 DIExpression::appendOffset(Opcodes
, Offset
);
2460 return BI
->getOperand(0);
2462 Opcodes
.append({dwarf::DW_OP_constu
, Val
});
2464 handleSSAValueOperands(CurrentLocOps
, Opcodes
, AdditionalValues
, BI
);
2467 // Add salvaged binary operator to expression stack, if it has a valid
2468 // representation in a DIExpression.
2469 uint64_t DwarfBinOp
= getDwarfOpForBinOp(BinOpcode
);
2472 Opcodes
.push_back(DwarfBinOp
);
2473 return BI
->getOperand(0);
2476 uint64_t getDwarfOpForIcmpPred(CmpInst::Predicate Pred
) {
2477 // The signedness of the operation is implicit in the typed stack, signed and
2478 // unsigned instructions map to the same DWARF opcode.
2480 case CmpInst::ICMP_EQ
:
2481 return dwarf::DW_OP_eq
;
2482 case CmpInst::ICMP_NE
:
2483 return dwarf::DW_OP_ne
;
2484 case CmpInst::ICMP_UGT
:
2485 case CmpInst::ICMP_SGT
:
2486 return dwarf::DW_OP_gt
;
2487 case CmpInst::ICMP_UGE
:
2488 case CmpInst::ICMP_SGE
:
2489 return dwarf::DW_OP_ge
;
2490 case CmpInst::ICMP_ULT
:
2491 case CmpInst::ICMP_SLT
:
2492 return dwarf::DW_OP_lt
;
2493 case CmpInst::ICMP_ULE
:
2494 case CmpInst::ICMP_SLE
:
2495 return dwarf::DW_OP_le
;
2501 Value
*getSalvageOpsForIcmpOp(ICmpInst
*Icmp
, uint64_t CurrentLocOps
,
2502 SmallVectorImpl
<uint64_t> &Opcodes
,
2503 SmallVectorImpl
<Value
*> &AdditionalValues
) {
2504 // Handle icmp operations with constant integer operands as a special case.
2505 auto *ConstInt
= dyn_cast
<ConstantInt
>(Icmp
->getOperand(1));
2506 // Values wider than 64 bits cannot be represented within a DIExpression.
2507 if (ConstInt
&& ConstInt
->getBitWidth() > 64)
2509 // Push any Constant Int operand onto the expression stack.
2511 if (Icmp
->isSigned())
2512 Opcodes
.push_back(dwarf::DW_OP_consts
);
2514 Opcodes
.push_back(dwarf::DW_OP_constu
);
2515 uint64_t Val
= ConstInt
->getSExtValue();
2516 Opcodes
.push_back(Val
);
2518 handleSSAValueOperands(CurrentLocOps
, Opcodes
, AdditionalValues
, Icmp
);
2521 // Add salvaged binary operator to expression stack, if it has a valid
2522 // representation in a DIExpression.
2523 uint64_t DwarfIcmpOp
= getDwarfOpForIcmpPred(Icmp
->getPredicate());
2526 Opcodes
.push_back(DwarfIcmpOp
);
2527 return Icmp
->getOperand(0);
2530 Value
*llvm::salvageDebugInfoImpl(Instruction
&I
, uint64_t CurrentLocOps
,
2531 SmallVectorImpl
<uint64_t> &Ops
,
2532 SmallVectorImpl
<Value
*> &AdditionalValues
) {
2533 auto &M
= *I
.getModule();
2534 auto &DL
= M
.getDataLayout();
2536 if (auto *CI
= dyn_cast
<CastInst
>(&I
)) {
2537 Value
*FromValue
= CI
->getOperand(0);
2538 // No-op casts are irrelevant for debug info.
2539 if (CI
->isNoopCast(DL
)) {
2543 Type
*Type
= CI
->getType();
2544 if (Type
->isPointerTy())
2545 Type
= DL
.getIntPtrType(Type
);
2546 // Casts other than Trunc, SExt, or ZExt to scalar types cannot be salvaged.
2547 if (Type
->isVectorTy() ||
2548 !(isa
<TruncInst
>(&I
) || isa
<SExtInst
>(&I
) || isa
<ZExtInst
>(&I
) ||
2549 isa
<IntToPtrInst
>(&I
) || isa
<PtrToIntInst
>(&I
)))
2552 llvm::Type
*FromType
= FromValue
->getType();
2553 if (FromType
->isPointerTy())
2554 FromType
= DL
.getIntPtrType(FromType
);
2556 unsigned FromTypeBitSize
= FromType
->getScalarSizeInBits();
2557 unsigned ToTypeBitSize
= Type
->getScalarSizeInBits();
2559 auto ExtOps
= DIExpression::getExtOps(FromTypeBitSize
, ToTypeBitSize
,
2561 Ops
.append(ExtOps
.begin(), ExtOps
.end());
2565 if (auto *GEP
= dyn_cast
<GetElementPtrInst
>(&I
))
2566 return getSalvageOpsForGEP(GEP
, DL
, CurrentLocOps
, Ops
, AdditionalValues
);
2567 if (auto *BI
= dyn_cast
<BinaryOperator
>(&I
))
2568 return getSalvageOpsForBinOp(BI
, CurrentLocOps
, Ops
, AdditionalValues
);
2569 if (auto *IC
= dyn_cast
<ICmpInst
>(&I
))
2570 return getSalvageOpsForIcmpOp(IC
, CurrentLocOps
, Ops
, AdditionalValues
);
2572 // *Not* to do: we should not attempt to salvage load instructions,
2573 // because the validity and lifetime of a dbg.value containing
2574 // DW_OP_deref becomes difficult to analyze. See PR40628 for examples.
2578 /// A replacement for a dbg.value expression.
2579 using DbgValReplacement
= std::optional
<DIExpression
*>;
2581 /// Point debug users of \p From to \p To using exprs given by \p RewriteExpr,
2582 /// possibly moving/undefing users to prevent use-before-def. Returns true if
2583 /// changes are made.
2584 static bool rewriteDebugUsers(
2585 Instruction
&From
, Value
&To
, Instruction
&DomPoint
, DominatorTree
&DT
,
2586 function_ref
<DbgValReplacement(DbgVariableIntrinsic
&DII
)> RewriteExpr
,
2587 function_ref
<DbgValReplacement(DPValue
&DPV
)> RewriteDPVExpr
) {
2588 // Find debug users of From.
2589 SmallVector
<DbgVariableIntrinsic
*, 1> Users
;
2590 SmallVector
<DPValue
*, 1> DPUsers
;
2591 findDbgUsers(Users
, &From
, &DPUsers
);
2592 if (Users
.empty() && DPUsers
.empty())
2595 // Prevent use-before-def of To.
2596 bool Changed
= false;
2598 SmallPtrSet
<DbgVariableIntrinsic
*, 1> UndefOrSalvage
;
2599 SmallPtrSet
<DPValue
*, 1> UndefOrSalvageDPV
;
2600 if (isa
<Instruction
>(&To
)) {
2601 bool DomPointAfterFrom
= From
.getNextNonDebugInstruction() == &DomPoint
;
2603 for (auto *DII
: Users
) {
2604 // It's common to see a debug user between From and DomPoint. Move it
2605 // after DomPoint to preserve the variable update without any reordering.
2606 if (DomPointAfterFrom
&& DII
->getNextNonDebugInstruction() == &DomPoint
) {
2607 LLVM_DEBUG(dbgs() << "MOVE: " << *DII
<< '\n');
2608 DII
->moveAfter(&DomPoint
);
2611 // Users which otherwise aren't dominated by the replacement value must
2612 // be salvaged or deleted.
2613 } else if (!DT
.dominates(&DomPoint
, DII
)) {
2614 UndefOrSalvage
.insert(DII
);
2618 // DPValue implementation of the above.
2619 for (auto *DPV
: DPUsers
) {
2620 Instruction
*MarkedInstr
= DPV
->getMarker()->MarkedInstr
;
2621 Instruction
*NextNonDebug
= MarkedInstr
;
2622 // The next instruction might still be a dbg.declare, skip over it.
2623 if (isa
<DbgVariableIntrinsic
>(NextNonDebug
))
2624 NextNonDebug
= NextNonDebug
->getNextNonDebugInstruction();
2626 if (DomPointAfterFrom
&& NextNonDebug
== &DomPoint
) {
2627 LLVM_DEBUG(dbgs() << "MOVE: " << *DPV
<< '\n');
2628 DPV
->removeFromParent();
2629 // Ensure there's a marker.
2630 DomPoint
.getParent()->insertDPValueAfter(DPV
, &DomPoint
);
2632 } else if (!DT
.dominates(&DomPoint
, MarkedInstr
)) {
2633 UndefOrSalvageDPV
.insert(DPV
);
2638 // Update debug users without use-before-def risk.
2639 for (auto *DII
: Users
) {
2640 if (UndefOrSalvage
.count(DII
))
2643 DbgValReplacement DVR
= RewriteExpr(*DII
);
2647 DII
->replaceVariableLocationOp(&From
, &To
);
2648 DII
->setExpression(*DVR
);
2649 LLVM_DEBUG(dbgs() << "REWRITE: " << *DII
<< '\n');
2652 for (auto *DPV
: DPUsers
) {
2653 if (UndefOrSalvageDPV
.count(DPV
))
2656 DbgValReplacement DVR
= RewriteDPVExpr(*DPV
);
2660 DPV
->replaceVariableLocationOp(&From
, &To
);
2661 DPV
->setExpression(*DVR
);
2662 LLVM_DEBUG(dbgs() << "REWRITE: " << DPV
<< '\n');
2666 if (!UndefOrSalvage
.empty() || !UndefOrSalvageDPV
.empty()) {
2667 // Try to salvage the remaining debug users.
2668 salvageDebugInfo(From
);
2675 /// Check if a bitcast between a value of type \p FromTy to type \p ToTy would
2676 /// losslessly preserve the bits and semantics of the value. This predicate is
2677 /// symmetric, i.e swapping \p FromTy and \p ToTy should give the same result.
2679 /// Note that Type::canLosslesslyBitCastTo is not suitable here because it
2680 /// allows semantically unequivalent bitcasts, such as <2 x i64> -> <4 x i32>,
2681 /// and also does not allow lossless pointer <-> integer conversions.
2682 static bool isBitCastSemanticsPreserving(const DataLayout
&DL
, Type
*FromTy
,
2684 // Trivially compatible types.
2688 // Handle compatible pointer <-> integer conversions.
2689 if (FromTy
->isIntOrPtrTy() && ToTy
->isIntOrPtrTy()) {
2690 bool SameSize
= DL
.getTypeSizeInBits(FromTy
) == DL
.getTypeSizeInBits(ToTy
);
2691 bool LosslessConversion
= !DL
.isNonIntegralPointerType(FromTy
) &&
2692 !DL
.isNonIntegralPointerType(ToTy
);
2693 return SameSize
&& LosslessConversion
;
2696 // TODO: This is not exhaustive.
2700 bool llvm::replaceAllDbgUsesWith(Instruction
&From
, Value
&To
,
2701 Instruction
&DomPoint
, DominatorTree
&DT
) {
2702 // Exit early if From has no debug users.
2703 if (!From
.isUsedByMetadata())
2706 assert(&From
!= &To
&& "Can't replace something with itself");
2708 Type
*FromTy
= From
.getType();
2709 Type
*ToTy
= To
.getType();
2711 auto Identity
= [&](DbgVariableIntrinsic
&DII
) -> DbgValReplacement
{
2712 return DII
.getExpression();
2714 auto IdentityDPV
= [&](DPValue
&DPV
) -> DbgValReplacement
{
2715 return DPV
.getExpression();
2718 // Handle no-op conversions.
2719 Module
&M
= *From
.getModule();
2720 const DataLayout
&DL
= M
.getDataLayout();
2721 if (isBitCastSemanticsPreserving(DL
, FromTy
, ToTy
))
2722 return rewriteDebugUsers(From
, To
, DomPoint
, DT
, Identity
, IdentityDPV
);
2724 // Handle integer-to-integer widening and narrowing.
2725 // FIXME: Use DW_OP_convert when it's available everywhere.
2726 if (FromTy
->isIntegerTy() && ToTy
->isIntegerTy()) {
2727 uint64_t FromBits
= FromTy
->getPrimitiveSizeInBits();
2728 uint64_t ToBits
= ToTy
->getPrimitiveSizeInBits();
2729 assert(FromBits
!= ToBits
&& "Unexpected no-op conversion");
2731 // When the width of the result grows, assume that a debugger will only
2732 // access the low `FromBits` bits when inspecting the source variable.
2733 if (FromBits
< ToBits
)
2734 return rewriteDebugUsers(From
, To
, DomPoint
, DT
, Identity
, IdentityDPV
);
2736 // The width of the result has shrunk. Use sign/zero extension to describe
2737 // the source variable's high bits.
2738 auto SignOrZeroExt
= [&](DbgVariableIntrinsic
&DII
) -> DbgValReplacement
{
2739 DILocalVariable
*Var
= DII
.getVariable();
2741 // Without knowing signedness, sign/zero extension isn't possible.
2742 auto Signedness
= Var
->getSignedness();
2744 return std::nullopt
;
2746 bool Signed
= *Signedness
== DIBasicType::Signedness::Signed
;
2747 return DIExpression::appendExt(DII
.getExpression(), ToBits
, FromBits
,
2750 // RemoveDIs: duplicate implementation working on DPValues rather than on
2751 // dbg.value intrinsics.
2752 auto SignOrZeroExtDPV
= [&](DPValue
&DPV
) -> DbgValReplacement
{
2753 DILocalVariable
*Var
= DPV
.getVariable();
2755 // Without knowing signedness, sign/zero extension isn't possible.
2756 auto Signedness
= Var
->getSignedness();
2758 return std::nullopt
;
2760 bool Signed
= *Signedness
== DIBasicType::Signedness::Signed
;
2761 return DIExpression::appendExt(DPV
.getExpression(), ToBits
, FromBits
,
2764 return rewriteDebugUsers(From
, To
, DomPoint
, DT
, SignOrZeroExt
,
2768 // TODO: Floating-point conversions, vectors.
2772 std::pair
<unsigned, unsigned>
2773 llvm::removeAllNonTerminatorAndEHPadInstructions(BasicBlock
*BB
) {
2774 unsigned NumDeadInst
= 0;
2775 unsigned NumDeadDbgInst
= 0;
2776 // Delete the instructions backwards, as it has a reduced likelihood of
2777 // having to update as many def-use and use-def chains.
2778 Instruction
*EndInst
= BB
->getTerminator(); // Last not to be deleted.
2779 // RemoveDIs: erasing debug-info must be done manually.
2780 EndInst
->dropDbgValues();
2781 while (EndInst
!= &BB
->front()) {
2782 // Delete the next to last instruction.
2783 Instruction
*Inst
= &*--EndInst
->getIterator();
2784 if (!Inst
->use_empty() && !Inst
->getType()->isTokenTy())
2785 Inst
->replaceAllUsesWith(PoisonValue::get(Inst
->getType()));
2786 if (Inst
->isEHPad() || Inst
->getType()->isTokenTy()) {
2787 // EHPads can't have DPValues attached to them, but it might be possible
2788 // for things with token type.
2789 Inst
->dropDbgValues();
2793 if (isa
<DbgInfoIntrinsic
>(Inst
))
2797 // RemoveDIs: erasing debug-info must be done manually.
2798 Inst
->dropDbgValues();
2799 Inst
->eraseFromParent();
2801 return {NumDeadInst
, NumDeadDbgInst
};
2804 unsigned llvm::changeToUnreachable(Instruction
*I
, bool PreserveLCSSA
,
2805 DomTreeUpdater
*DTU
,
2806 MemorySSAUpdater
*MSSAU
) {
2807 BasicBlock
*BB
= I
->getParent();
2810 MSSAU
->changeToUnreachable(I
);
2812 SmallSet
<BasicBlock
*, 8> UniqueSuccessors
;
2814 // Loop over all of the successors, removing BB's entry from any PHI
2816 for (BasicBlock
*Successor
: successors(BB
)) {
2817 Successor
->removePredecessor(BB
, PreserveLCSSA
);
2819 UniqueSuccessors
.insert(Successor
);
2821 auto *UI
= new UnreachableInst(I
->getContext(), I
);
2822 UI
->setDebugLoc(I
->getDebugLoc());
2824 // All instructions after this are dead.
2825 unsigned NumInstrsRemoved
= 0;
2826 BasicBlock::iterator BBI
= I
->getIterator(), BBE
= BB
->end();
2827 while (BBI
!= BBE
) {
2828 if (!BBI
->use_empty())
2829 BBI
->replaceAllUsesWith(PoisonValue::get(BBI
->getType()));
2830 BBI
++->eraseFromParent();
2834 SmallVector
<DominatorTree::UpdateType
, 8> Updates
;
2835 Updates
.reserve(UniqueSuccessors
.size());
2836 for (BasicBlock
*UniqueSuccessor
: UniqueSuccessors
)
2837 Updates
.push_back({DominatorTree::Delete
, BB
, UniqueSuccessor
});
2838 DTU
->applyUpdates(Updates
);
2840 BB
->flushTerminatorDbgValues();
2841 return NumInstrsRemoved
;
2844 CallInst
*llvm::createCallMatchingInvoke(InvokeInst
*II
) {
2845 SmallVector
<Value
*, 8> Args(II
->args());
2846 SmallVector
<OperandBundleDef
, 1> OpBundles
;
2847 II
->getOperandBundlesAsDefs(OpBundles
);
2848 CallInst
*NewCall
= CallInst::Create(II
->getFunctionType(),
2849 II
->getCalledOperand(), Args
, OpBundles
);
2850 NewCall
->setCallingConv(II
->getCallingConv());
2851 NewCall
->setAttributes(II
->getAttributes());
2852 NewCall
->setDebugLoc(II
->getDebugLoc());
2853 NewCall
->copyMetadata(*II
);
2855 // If the invoke had profile metadata, try converting them for CallInst.
2856 uint64_t TotalWeight
;
2857 if (NewCall
->extractProfTotalWeight(TotalWeight
)) {
2858 // Set the total weight if it fits into i32, otherwise reset.
2859 MDBuilder
MDB(NewCall
->getContext());
2860 auto NewWeights
= uint32_t(TotalWeight
) != TotalWeight
2862 : MDB
.createBranchWeights({uint32_t(TotalWeight
)});
2863 NewCall
->setMetadata(LLVMContext::MD_prof
, NewWeights
);
2869 // changeToCall - Convert the specified invoke into a normal call.
2870 CallInst
*llvm::changeToCall(InvokeInst
*II
, DomTreeUpdater
*DTU
) {
2871 CallInst
*NewCall
= createCallMatchingInvoke(II
);
2872 NewCall
->takeName(II
);
2873 NewCall
->insertBefore(II
);
2874 II
->replaceAllUsesWith(NewCall
);
2876 // Follow the call by a branch to the normal destination.
2877 BasicBlock
*NormalDestBB
= II
->getNormalDest();
2878 BranchInst::Create(NormalDestBB
, II
);
2880 // Update PHI nodes in the unwind destination
2881 BasicBlock
*BB
= II
->getParent();
2882 BasicBlock
*UnwindDestBB
= II
->getUnwindDest();
2883 UnwindDestBB
->removePredecessor(BB
);
2884 II
->eraseFromParent();
2886 DTU
->applyUpdates({{DominatorTree::Delete
, BB
, UnwindDestBB
}});
2890 BasicBlock
*llvm::changeToInvokeAndSplitBasicBlock(CallInst
*CI
,
2891 BasicBlock
*UnwindEdge
,
2892 DomTreeUpdater
*DTU
) {
2893 BasicBlock
*BB
= CI
->getParent();
2895 // Convert this function call into an invoke instruction. First, split the
2897 BasicBlock
*Split
= SplitBlock(BB
, CI
, DTU
, /*LI=*/nullptr, /*MSSAU*/ nullptr,
2898 CI
->getName() + ".noexc");
2900 // Delete the unconditional branch inserted by SplitBlock
2901 BB
->back().eraseFromParent();
2903 // Create the new invoke instruction.
2904 SmallVector
<Value
*, 8> InvokeArgs(CI
->args());
2905 SmallVector
<OperandBundleDef
, 1> OpBundles
;
2907 CI
->getOperandBundlesAsDefs(OpBundles
);
2909 // Note: we're round tripping operand bundles through memory here, and that
2910 // can potentially be avoided with a cleverer API design that we do not have
2914 InvokeInst::Create(CI
->getFunctionType(), CI
->getCalledOperand(), Split
,
2915 UnwindEdge
, InvokeArgs
, OpBundles
, CI
->getName(), BB
);
2916 II
->setDebugLoc(CI
->getDebugLoc());
2917 II
->setCallingConv(CI
->getCallingConv());
2918 II
->setAttributes(CI
->getAttributes());
2919 II
->setMetadata(LLVMContext::MD_prof
, CI
->getMetadata(LLVMContext::MD_prof
));
2922 DTU
->applyUpdates({{DominatorTree::Insert
, BB
, UnwindEdge
}});
2924 // Make sure that anything using the call now uses the invoke! This also
2925 // updates the CallGraph if present, because it uses a WeakTrackingVH.
2926 CI
->replaceAllUsesWith(II
);
2928 // Delete the original call
2929 Split
->front().eraseFromParent();
2933 static bool markAliveBlocks(Function
&F
,
2934 SmallPtrSetImpl
<BasicBlock
*> &Reachable
,
2935 DomTreeUpdater
*DTU
= nullptr) {
2936 SmallVector
<BasicBlock
*, 128> Worklist
;
2937 BasicBlock
*BB
= &F
.front();
2938 Worklist
.push_back(BB
);
2939 Reachable
.insert(BB
);
2940 bool Changed
= false;
2942 BB
= Worklist
.pop_back_val();
2944 // Do a quick scan of the basic block, turning any obviously unreachable
2945 // instructions into LLVM unreachable insts. The instruction combining pass
2946 // canonicalizes unreachable insts into stores to null or undef.
2947 for (Instruction
&I
: *BB
) {
2948 if (auto *CI
= dyn_cast
<CallInst
>(&I
)) {
2949 Value
*Callee
= CI
->getCalledOperand();
2950 // Handle intrinsic calls.
2951 if (Function
*F
= dyn_cast
<Function
>(Callee
)) {
2952 auto IntrinsicID
= F
->getIntrinsicID();
2953 // Assumptions that are known to be false are equivalent to
2954 // unreachable. Also, if the condition is undefined, then we make the
2955 // choice most beneficial to the optimizer, and choose that to also be
2957 if (IntrinsicID
== Intrinsic::assume
) {
2958 if (match(CI
->getArgOperand(0), m_CombineOr(m_Zero(), m_Undef()))) {
2959 // Don't insert a call to llvm.trap right before the unreachable.
2960 changeToUnreachable(CI
, false, DTU
);
2964 } else if (IntrinsicID
== Intrinsic::experimental_guard
) {
2965 // A call to the guard intrinsic bails out of the current
2966 // compilation unit if the predicate passed to it is false. If the
2967 // predicate is a constant false, then we know the guard will bail
2968 // out of the current compile unconditionally, so all code following
2971 // Note: unlike in llvm.assume, it is not "obviously profitable" for
2972 // guards to treat `undef` as `false` since a guard on `undef` can
2973 // still be useful for widening.
2974 if (match(CI
->getArgOperand(0), m_Zero()))
2975 if (!isa
<UnreachableInst
>(CI
->getNextNode())) {
2976 changeToUnreachable(CI
->getNextNode(), false, DTU
);
2981 } else if ((isa
<ConstantPointerNull
>(Callee
) &&
2982 !NullPointerIsDefined(CI
->getFunction(),
2983 cast
<PointerType
>(Callee
->getType())
2984 ->getAddressSpace())) ||
2985 isa
<UndefValue
>(Callee
)) {
2986 changeToUnreachable(CI
, false, DTU
);
2990 if (CI
->doesNotReturn() && !CI
->isMustTailCall()) {
2991 // If we found a call to a no-return function, insert an unreachable
2992 // instruction after it. Make sure there isn't *already* one there
2994 if (!isa
<UnreachableInst
>(CI
->getNextNonDebugInstruction())) {
2995 // Don't insert a call to llvm.trap right before the unreachable.
2996 changeToUnreachable(CI
->getNextNonDebugInstruction(), false, DTU
);
3001 } else if (auto *SI
= dyn_cast
<StoreInst
>(&I
)) {
3002 // Store to undef and store to null are undefined and used to signal
3003 // that they should be changed to unreachable by passes that can't
3006 // Don't touch volatile stores.
3007 if (SI
->isVolatile()) continue;
3009 Value
*Ptr
= SI
->getOperand(1);
3011 if (isa
<UndefValue
>(Ptr
) ||
3012 (isa
<ConstantPointerNull
>(Ptr
) &&
3013 !NullPointerIsDefined(SI
->getFunction(),
3014 SI
->getPointerAddressSpace()))) {
3015 changeToUnreachable(SI
, false, DTU
);
3022 Instruction
*Terminator
= BB
->getTerminator();
3023 if (auto *II
= dyn_cast
<InvokeInst
>(Terminator
)) {
3024 // Turn invokes that call 'nounwind' functions into ordinary calls.
3025 Value
*Callee
= II
->getCalledOperand();
3026 if ((isa
<ConstantPointerNull
>(Callee
) &&
3027 !NullPointerIsDefined(BB
->getParent())) ||
3028 isa
<UndefValue
>(Callee
)) {
3029 changeToUnreachable(II
, false, DTU
);
3032 if (II
->doesNotReturn() &&
3033 !isa
<UnreachableInst
>(II
->getNormalDest()->front())) {
3034 // If we found an invoke of a no-return function,
3035 // create a new empty basic block with an `unreachable` terminator,
3036 // and set it as the normal destination for the invoke,
3037 // unless that is already the case.
3038 // Note that the original normal destination could have other uses.
3039 BasicBlock
*OrigNormalDest
= II
->getNormalDest();
3040 OrigNormalDest
->removePredecessor(II
->getParent());
3041 LLVMContext
&Ctx
= II
->getContext();
3042 BasicBlock
*UnreachableNormalDest
= BasicBlock::Create(
3043 Ctx
, OrigNormalDest
->getName() + ".unreachable",
3044 II
->getFunction(), OrigNormalDest
);
3045 new UnreachableInst(Ctx
, UnreachableNormalDest
);
3046 II
->setNormalDest(UnreachableNormalDest
);
3049 {{DominatorTree::Delete
, BB
, OrigNormalDest
},
3050 {DominatorTree::Insert
, BB
, UnreachableNormalDest
}});
3053 if (II
->doesNotThrow() && canSimplifyInvokeNoUnwind(&F
)) {
3054 if (II
->use_empty() && !II
->mayHaveSideEffects()) {
3055 // jump to the normal destination branch.
3056 BasicBlock
*NormalDestBB
= II
->getNormalDest();
3057 BasicBlock
*UnwindDestBB
= II
->getUnwindDest();
3058 BranchInst::Create(NormalDestBB
, II
);
3059 UnwindDestBB
->removePredecessor(II
->getParent());
3060 II
->eraseFromParent();
3062 DTU
->applyUpdates({{DominatorTree::Delete
, BB
, UnwindDestBB
}});
3064 changeToCall(II
, DTU
);
3068 } else if (auto *CatchSwitch
= dyn_cast
<CatchSwitchInst
>(Terminator
)) {
3069 // Remove catchpads which cannot be reached.
3070 struct CatchPadDenseMapInfo
{
3071 static CatchPadInst
*getEmptyKey() {
3072 return DenseMapInfo
<CatchPadInst
*>::getEmptyKey();
3075 static CatchPadInst
*getTombstoneKey() {
3076 return DenseMapInfo
<CatchPadInst
*>::getTombstoneKey();
3079 static unsigned getHashValue(CatchPadInst
*CatchPad
) {
3080 return static_cast<unsigned>(hash_combine_range(
3081 CatchPad
->value_op_begin(), CatchPad
->value_op_end()));
3084 static bool isEqual(CatchPadInst
*LHS
, CatchPadInst
*RHS
) {
3085 if (LHS
== getEmptyKey() || LHS
== getTombstoneKey() ||
3086 RHS
== getEmptyKey() || RHS
== getTombstoneKey())
3088 return LHS
->isIdenticalTo(RHS
);
3092 SmallDenseMap
<BasicBlock
*, int, 8> NumPerSuccessorCases
;
3093 // Set of unique CatchPads.
3094 SmallDenseMap
<CatchPadInst
*, detail::DenseSetEmpty
, 4,
3095 CatchPadDenseMapInfo
, detail::DenseSetPair
<CatchPadInst
*>>
3097 detail::DenseSetEmpty Empty
;
3098 for (CatchSwitchInst::handler_iterator I
= CatchSwitch
->handler_begin(),
3099 E
= CatchSwitch
->handler_end();
3101 BasicBlock
*HandlerBB
= *I
;
3103 ++NumPerSuccessorCases
[HandlerBB
];
3104 auto *CatchPad
= cast
<CatchPadInst
>(HandlerBB
->getFirstNonPHI());
3105 if (!HandlerSet
.insert({CatchPad
, Empty
}).second
) {
3107 --NumPerSuccessorCases
[HandlerBB
];
3108 CatchSwitch
->removeHandler(I
);
3115 std::vector
<DominatorTree::UpdateType
> Updates
;
3116 for (const std::pair
<BasicBlock
*, int> &I
: NumPerSuccessorCases
)
3118 Updates
.push_back({DominatorTree::Delete
, BB
, I
.first
});
3119 DTU
->applyUpdates(Updates
);
3123 Changed
|= ConstantFoldTerminator(BB
, true, nullptr, DTU
);
3124 for (BasicBlock
*Successor
: successors(BB
))
3125 if (Reachable
.insert(Successor
).second
)
3126 Worklist
.push_back(Successor
);
3127 } while (!Worklist
.empty());
3131 Instruction
*llvm::removeUnwindEdge(BasicBlock
*BB
, DomTreeUpdater
*DTU
) {
3132 Instruction
*TI
= BB
->getTerminator();
3134 if (auto *II
= dyn_cast
<InvokeInst
>(TI
))
3135 return changeToCall(II
, DTU
);
3138 BasicBlock
*UnwindDest
;
3140 if (auto *CRI
= dyn_cast
<CleanupReturnInst
>(TI
)) {
3141 NewTI
= CleanupReturnInst::Create(CRI
->getCleanupPad(), nullptr, CRI
);
3142 UnwindDest
= CRI
->getUnwindDest();
3143 } else if (auto *CatchSwitch
= dyn_cast
<CatchSwitchInst
>(TI
)) {
3144 auto *NewCatchSwitch
= CatchSwitchInst::Create(
3145 CatchSwitch
->getParentPad(), nullptr, CatchSwitch
->getNumHandlers(),
3146 CatchSwitch
->getName(), CatchSwitch
);
3147 for (BasicBlock
*PadBB
: CatchSwitch
->handlers())
3148 NewCatchSwitch
->addHandler(PadBB
);
3150 NewTI
= NewCatchSwitch
;
3151 UnwindDest
= CatchSwitch
->getUnwindDest();
3153 llvm_unreachable("Could not find unwind successor");
3156 NewTI
->takeName(TI
);
3157 NewTI
->setDebugLoc(TI
->getDebugLoc());
3158 UnwindDest
->removePredecessor(BB
);
3159 TI
->replaceAllUsesWith(NewTI
);
3160 TI
->eraseFromParent();
3162 DTU
->applyUpdates({{DominatorTree::Delete
, BB
, UnwindDest
}});
3166 /// removeUnreachableBlocks - Remove blocks that are not reachable, even
3167 /// if they are in a dead cycle. Return true if a change was made, false
3169 bool llvm::removeUnreachableBlocks(Function
&F
, DomTreeUpdater
*DTU
,
3170 MemorySSAUpdater
*MSSAU
) {
3171 SmallPtrSet
<BasicBlock
*, 16> Reachable
;
3172 bool Changed
= markAliveBlocks(F
, Reachable
, DTU
);
3174 // If there are unreachable blocks in the CFG...
3175 if (Reachable
.size() == F
.size())
3178 assert(Reachable
.size() < F
.size());
3180 // Are there any blocks left to actually delete?
3181 SmallSetVector
<BasicBlock
*, 8> BlocksToRemove
;
3182 for (BasicBlock
&BB
: F
) {
3183 // Skip reachable basic blocks
3184 if (Reachable
.count(&BB
))
3186 // Skip already-deleted blocks
3187 if (DTU
&& DTU
->isBBPendingDeletion(&BB
))
3189 BlocksToRemove
.insert(&BB
);
3192 if (BlocksToRemove
.empty())
3196 NumRemoved
+= BlocksToRemove
.size();
3199 MSSAU
->removeBlocks(BlocksToRemove
);
3201 DeleteDeadBlocks(BlocksToRemove
.takeVector(), DTU
);
3206 void llvm::combineMetadata(Instruction
*K
, const Instruction
*J
,
3207 ArrayRef
<unsigned> KnownIDs
, bool DoesKMove
) {
3208 SmallVector
<std::pair
<unsigned, MDNode
*>, 4> Metadata
;
3209 K
->dropUnknownNonDebugMetadata(KnownIDs
);
3210 K
->getAllMetadataOtherThanDebugLoc(Metadata
);
3211 for (const auto &MD
: Metadata
) {
3212 unsigned Kind
= MD
.first
;
3213 MDNode
*JMD
= J
->getMetadata(Kind
);
3214 MDNode
*KMD
= MD
.second
;
3218 K
->setMetadata(Kind
, nullptr); // Remove unknown metadata
3220 case LLVMContext::MD_dbg
:
3221 llvm_unreachable("getAllMetadataOtherThanDebugLoc returned a MD_dbg");
3222 case LLVMContext::MD_DIAssignID
:
3223 K
->mergeDIAssignID(J
);
3225 case LLVMContext::MD_tbaa
:
3226 K
->setMetadata(Kind
, MDNode::getMostGenericTBAA(JMD
, KMD
));
3228 case LLVMContext::MD_alias_scope
:
3229 K
->setMetadata(Kind
, MDNode::getMostGenericAliasScope(JMD
, KMD
));
3231 case LLVMContext::MD_noalias
:
3232 case LLVMContext::MD_mem_parallel_loop_access
:
3233 K
->setMetadata(Kind
, MDNode::intersect(JMD
, KMD
));
3235 case LLVMContext::MD_access_group
:
3236 K
->setMetadata(LLVMContext::MD_access_group
,
3237 intersectAccessGroups(K
, J
));
3239 case LLVMContext::MD_range
:
3240 if (DoesKMove
|| !K
->hasMetadata(LLVMContext::MD_noundef
))
3241 K
->setMetadata(Kind
, MDNode::getMostGenericRange(JMD
, KMD
));
3243 case LLVMContext::MD_fpmath
:
3244 K
->setMetadata(Kind
, MDNode::getMostGenericFPMath(JMD
, KMD
));
3246 case LLVMContext::MD_invariant_load
:
3247 // If K moves, only set the !invariant.load if it is present in both
3250 K
->setMetadata(Kind
, JMD
);
3252 case LLVMContext::MD_nonnull
:
3253 if (DoesKMove
|| !K
->hasMetadata(LLVMContext::MD_noundef
))
3254 K
->setMetadata(Kind
, JMD
);
3256 case LLVMContext::MD_invariant_group
:
3257 // Preserve !invariant.group in K.
3259 case LLVMContext::MD_align
:
3260 if (DoesKMove
|| !K
->hasMetadata(LLVMContext::MD_noundef
))
3262 Kind
, MDNode::getMostGenericAlignmentOrDereferenceable(JMD
, KMD
));
3264 case LLVMContext::MD_dereferenceable
:
3265 case LLVMContext::MD_dereferenceable_or_null
:
3267 K
->setMetadata(Kind
,
3268 MDNode::getMostGenericAlignmentOrDereferenceable(JMD
, KMD
));
3270 case LLVMContext::MD_preserve_access_index
:
3271 // Preserve !preserve.access.index in K.
3273 case LLVMContext::MD_noundef
:
3274 // If K does move, keep noundef if it is present in both instructions.
3276 K
->setMetadata(Kind
, JMD
);
3278 case LLVMContext::MD_nontemporal
:
3279 // Preserve !nontemporal if it is present on both instructions.
3280 K
->setMetadata(Kind
, JMD
);
3282 case LLVMContext::MD_prof
:
3284 K
->setMetadata(Kind
, MDNode::getMergedProfMetadata(KMD
, JMD
, K
, J
));
3288 // Set !invariant.group from J if J has it. If both instructions have it
3289 // then we will just pick it from J - even when they are different.
3290 // Also make sure that K is load or store - f.e. combining bitcast with load
3291 // could produce bitcast with invariant.group metadata, which is invalid.
3292 // FIXME: we should try to preserve both invariant.group md if they are
3293 // different, but right now instruction can only have one invariant.group.
3294 if (auto *JMD
= J
->getMetadata(LLVMContext::MD_invariant_group
))
3295 if (isa
<LoadInst
>(K
) || isa
<StoreInst
>(K
))
3296 K
->setMetadata(LLVMContext::MD_invariant_group
, JMD
);
3299 void llvm::combineMetadataForCSE(Instruction
*K
, const Instruction
*J
,
3301 unsigned KnownIDs
[] = {LLVMContext::MD_tbaa
,
3302 LLVMContext::MD_alias_scope
,
3303 LLVMContext::MD_noalias
,
3304 LLVMContext::MD_range
,
3305 LLVMContext::MD_fpmath
,
3306 LLVMContext::MD_invariant_load
,
3307 LLVMContext::MD_nonnull
,
3308 LLVMContext::MD_invariant_group
,
3309 LLVMContext::MD_align
,
3310 LLVMContext::MD_dereferenceable
,
3311 LLVMContext::MD_dereferenceable_or_null
,
3312 LLVMContext::MD_access_group
,
3313 LLVMContext::MD_preserve_access_index
,
3314 LLVMContext::MD_prof
,
3315 LLVMContext::MD_nontemporal
,
3316 LLVMContext::MD_noundef
};
3317 combineMetadata(K
, J
, KnownIDs
, KDominatesJ
);
3320 void llvm::copyMetadataForLoad(LoadInst
&Dest
, const LoadInst
&Source
) {
3321 SmallVector
<std::pair
<unsigned, MDNode
*>, 8> MD
;
3322 Source
.getAllMetadata(MD
);
3323 MDBuilder
MDB(Dest
.getContext());
3324 Type
*NewType
= Dest
.getType();
3325 const DataLayout
&DL
= Source
.getModule()->getDataLayout();
3326 for (const auto &MDPair
: MD
) {
3327 unsigned ID
= MDPair
.first
;
3328 MDNode
*N
= MDPair
.second
;
3329 // Note, essentially every kind of metadata should be preserved here! This
3330 // routine is supposed to clone a load instruction changing *only its type*.
3331 // The only metadata it makes sense to drop is metadata which is invalidated
3332 // when the pointer type changes. This should essentially never be the case
3333 // in LLVM, but we explicitly switch over only known metadata to be
3334 // conservatively correct. If you are adding metadata to LLVM which pertains
3335 // to loads, you almost certainly want to add it here.
3337 case LLVMContext::MD_dbg
:
3338 case LLVMContext::MD_tbaa
:
3339 case LLVMContext::MD_prof
:
3340 case LLVMContext::MD_fpmath
:
3341 case LLVMContext::MD_tbaa_struct
:
3342 case LLVMContext::MD_invariant_load
:
3343 case LLVMContext::MD_alias_scope
:
3344 case LLVMContext::MD_noalias
:
3345 case LLVMContext::MD_nontemporal
:
3346 case LLVMContext::MD_mem_parallel_loop_access
:
3347 case LLVMContext::MD_access_group
:
3348 case LLVMContext::MD_noundef
:
3349 // All of these directly apply.
3350 Dest
.setMetadata(ID
, N
);
3353 case LLVMContext::MD_nonnull
:
3354 copyNonnullMetadata(Source
, N
, Dest
);
3357 case LLVMContext::MD_align
:
3358 case LLVMContext::MD_dereferenceable
:
3359 case LLVMContext::MD_dereferenceable_or_null
:
3360 // These only directly apply if the new type is also a pointer.
3361 if (NewType
->isPointerTy())
3362 Dest
.setMetadata(ID
, N
);
3365 case LLVMContext::MD_range
:
3366 copyRangeMetadata(DL
, Source
, N
, Dest
);
3372 void llvm::patchReplacementInstruction(Instruction
*I
, Value
*Repl
) {
3373 auto *ReplInst
= dyn_cast
<Instruction
>(Repl
);
3377 // Patch the replacement so that it is not more restrictive than the value
3379 // Note that if 'I' is a load being replaced by some operation,
3380 // for example, by an arithmetic operation, then andIRFlags()
3381 // would just erase all math flags from the original arithmetic
3382 // operation, which is clearly not wanted and not needed.
3383 if (!isa
<LoadInst
>(I
))
3384 ReplInst
->andIRFlags(I
);
3386 // FIXME: If both the original and replacement value are part of the
3387 // same control-flow region (meaning that the execution of one
3388 // guarantees the execution of the other), then we can combine the
3389 // noalias scopes here and do better than the general conservative
3390 // answer used in combineMetadata().
3392 // In general, GVN unifies expressions over different control-flow
3393 // regions, and so we need a conservative combination of the noalias
3395 combineMetadataForCSE(ReplInst
, I
, false);
3398 template <typename RootType
, typename DominatesFn
>
3399 static unsigned replaceDominatedUsesWith(Value
*From
, Value
*To
,
3400 const RootType
&Root
,
3401 const DominatesFn
&Dominates
) {
3402 assert(From
->getType() == To
->getType());
3405 for (Use
&U
: llvm::make_early_inc_range(From
->uses())) {
3406 if (!Dominates(Root
, U
))
3408 LLVM_DEBUG(dbgs() << "Replace dominated use of '";
3409 From
->printAsOperand(dbgs());
3410 dbgs() << "' with " << *To
<< " in " << *U
.getUser() << "\n");
3417 unsigned llvm::replaceNonLocalUsesWith(Instruction
*From
, Value
*To
) {
3418 assert(From
->getType() == To
->getType());
3419 auto *BB
= From
->getParent();
3422 for (Use
&U
: llvm::make_early_inc_range(From
->uses())) {
3423 auto *I
= cast
<Instruction
>(U
.getUser());
3424 if (I
->getParent() == BB
)
3432 unsigned llvm::replaceDominatedUsesWith(Value
*From
, Value
*To
,
3434 const BasicBlockEdge
&Root
) {
3435 auto Dominates
= [&DT
](const BasicBlockEdge
&Root
, const Use
&U
) {
3436 return DT
.dominates(Root
, U
);
3438 return ::replaceDominatedUsesWith(From
, To
, Root
, Dominates
);
3441 unsigned llvm::replaceDominatedUsesWith(Value
*From
, Value
*To
,
3443 const BasicBlock
*BB
) {
3444 auto Dominates
= [&DT
](const BasicBlock
*BB
, const Use
&U
) {
3445 return DT
.dominates(BB
, U
);
3447 return ::replaceDominatedUsesWith(From
, To
, BB
, Dominates
);
3450 bool llvm::callsGCLeafFunction(const CallBase
*Call
,
3451 const TargetLibraryInfo
&TLI
) {
3452 // Check if the function is specifically marked as a gc leaf function.
3453 if (Call
->hasFnAttr("gc-leaf-function"))
3455 if (const Function
*F
= Call
->getCalledFunction()) {
3456 if (F
->hasFnAttribute("gc-leaf-function"))
3459 if (auto IID
= F
->getIntrinsicID()) {
3460 // Most LLVM intrinsics do not take safepoints.
3461 return IID
!= Intrinsic::experimental_gc_statepoint
&&
3462 IID
!= Intrinsic::experimental_deoptimize
&&
3463 IID
!= Intrinsic::memcpy_element_unordered_atomic
&&
3464 IID
!= Intrinsic::memmove_element_unordered_atomic
;
3468 // Lib calls can be materialized by some passes, and won't be
3469 // marked as 'gc-leaf-function.' All available Libcalls are
3472 if (TLI
.getLibFunc(*Call
, LF
)) {
3479 void llvm::copyNonnullMetadata(const LoadInst
&OldLI
, MDNode
*N
,
3481 auto *NewTy
= NewLI
.getType();
3483 // This only directly applies if the new type is also a pointer.
3484 if (NewTy
->isPointerTy()) {
3485 NewLI
.setMetadata(LLVMContext::MD_nonnull
, N
);
3489 // The only other translation we can do is to integral loads with !range
3491 if (!NewTy
->isIntegerTy())
3494 MDBuilder
MDB(NewLI
.getContext());
3495 const Value
*Ptr
= OldLI
.getPointerOperand();
3496 auto *ITy
= cast
<IntegerType
>(NewTy
);
3497 auto *NullInt
= ConstantExpr::getPtrToInt(
3498 ConstantPointerNull::get(cast
<PointerType
>(Ptr
->getType())), ITy
);
3499 auto *NonNullInt
= ConstantExpr::getAdd(NullInt
, ConstantInt::get(ITy
, 1));
3500 NewLI
.setMetadata(LLVMContext::MD_range
,
3501 MDB
.createRange(NonNullInt
, NullInt
));
3504 void llvm::copyRangeMetadata(const DataLayout
&DL
, const LoadInst
&OldLI
,
3505 MDNode
*N
, LoadInst
&NewLI
) {
3506 auto *NewTy
= NewLI
.getType();
3507 // Simply copy the metadata if the type did not change.
3508 if (NewTy
== OldLI
.getType()) {
3509 NewLI
.setMetadata(LLVMContext::MD_range
, N
);
3513 // Give up unless it is converted to a pointer where there is a single very
3514 // valuable mapping we can do reliably.
3515 // FIXME: It would be nice to propagate this in more ways, but the type
3516 // conversions make it hard.
3517 if (!NewTy
->isPointerTy())
3520 unsigned BitWidth
= DL
.getPointerTypeSizeInBits(NewTy
);
3521 if (BitWidth
== OldLI
.getType()->getScalarSizeInBits() &&
3522 !getConstantRangeFromMetadata(*N
).contains(APInt(BitWidth
, 0))) {
3523 MDNode
*NN
= MDNode::get(OldLI
.getContext(), std::nullopt
);
3524 NewLI
.setMetadata(LLVMContext::MD_nonnull
, NN
);
3528 void llvm::dropDebugUsers(Instruction
&I
) {
3529 SmallVector
<DbgVariableIntrinsic
*, 1> DbgUsers
;
3530 SmallVector
<DPValue
*, 1> DPUsers
;
3531 findDbgUsers(DbgUsers
, &I
, &DPUsers
);
3532 for (auto *DII
: DbgUsers
)
3533 DII
->eraseFromParent();
3534 for (auto *DPV
: DPUsers
)
3535 DPV
->eraseFromParent();
3538 void llvm::hoistAllInstructionsInto(BasicBlock
*DomBlock
, Instruction
*InsertPt
,
3540 // Since we are moving the instructions out of its basic block, we do not
3541 // retain their original debug locations (DILocations) and debug intrinsic
3544 // Doing so would degrade the debugging experience and adversely affect the
3545 // accuracy of profiling information.
3547 // Currently, when hoisting the instructions, we take the following actions:
3548 // - Remove their debug intrinsic instructions.
3549 // - Set their debug locations to the values from the insertion point.
3551 // As per PR39141 (comment #8), the more fundamental reason why the dbg.values
3552 // need to be deleted, is because there will not be any instructions with a
3553 // DILocation in either branch left after performing the transformation. We
3554 // can only insert a dbg.value after the two branches are joined again.
3556 // See PR38762, PR39243 for more details.
3558 // TODO: Extend llvm.dbg.value to take more than one SSA Value (PR39141) to
3559 // encode predicated DIExpressions that yield different results on different
3562 for (BasicBlock::iterator II
= BB
->begin(), IE
= BB
->end(); II
!= IE
;) {
3563 Instruction
*I
= &*II
;
3564 I
->dropUBImplyingAttrsAndMetadata();
3565 if (I
->isUsedByMetadata())
3567 // RemoveDIs: drop debug-info too as the following code does.
3569 if (I
->isDebugOrPseudoInst()) {
3570 // Remove DbgInfo and pseudo probe Intrinsics.
3571 II
= I
->eraseFromParent();
3574 I
->setDebugLoc(InsertPt
->getDebugLoc());
3577 DomBlock
->splice(InsertPt
->getIterator(), BB
, BB
->begin(),
3578 BB
->getTerminator()->getIterator());
3581 DIExpression
*llvm::getExpressionForConstant(DIBuilder
&DIB
, const Constant
&C
,
3583 // Create integer constant expression.
3584 auto createIntegerExpression
= [&DIB
](const Constant
&CV
) -> DIExpression
* {
3585 const APInt
&API
= cast
<ConstantInt
>(&CV
)->getValue();
3586 std::optional
<int64_t> InitIntOpt
= API
.trySExtValue();
3587 return InitIntOpt
? DIB
.createConstantValueExpression(
3588 static_cast<uint64_t>(*InitIntOpt
))
3592 if (isa
<ConstantInt
>(C
))
3593 return createIntegerExpression(C
);
3595 auto *FP
= dyn_cast
<ConstantFP
>(&C
);
3596 if (FP
&& (Ty
.isFloatTy() || Ty
.isDoubleTy())) {
3597 const APFloat
&APF
= FP
->getValueAPF();
3598 return DIB
.createConstantValueExpression(
3599 APF
.bitcastToAPInt().getZExtValue());
3602 if (!Ty
.isPointerTy())
3605 if (isa
<ConstantPointerNull
>(C
))
3606 return DIB
.createConstantValueExpression(0);
3608 if (const ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(&C
))
3609 if (CE
->getOpcode() == Instruction::IntToPtr
) {
3610 const Value
*V
= CE
->getOperand(0);
3611 if (auto CI
= dyn_cast_or_null
<ConstantInt
>(V
))
3612 return createIntegerExpression(*CI
);
3619 /// A potential constituent of a bitreverse or bswap expression. See
3620 /// collectBitParts for a fuller explanation.
3622 BitPart(Value
*P
, unsigned BW
) : Provider(P
) {
3623 Provenance
.resize(BW
);
3626 /// The Value that this is a bitreverse/bswap of.
3629 /// The "provenance" of each bit. Provenance[A] = B means that bit A
3630 /// in Provider becomes bit B in the result of this expression.
3631 SmallVector
<int8_t, 32> Provenance
; // int8_t means max size is i128.
3633 enum { Unset
= -1 };
3636 } // end anonymous namespace
3638 /// Analyze the specified subexpression and see if it is capable of providing
3639 /// pieces of a bswap or bitreverse. The subexpression provides a potential
3640 /// piece of a bswap or bitreverse if it can be proved that each non-zero bit in
3641 /// the output of the expression came from a corresponding bit in some other
3642 /// value. This function is recursive, and the end result is a mapping of
3643 /// bitnumber to bitnumber. It is the caller's responsibility to validate that
3644 /// the bitnumber to bitnumber mapping is correct for a bswap or bitreverse.
3646 /// For example, if the current subexpression if "(shl i32 %X, 24)" then we know
3647 /// that the expression deposits the low byte of %X into the high byte of the
3648 /// result and that all other bits are zero. This expression is accepted and a
3649 /// BitPart is returned with Provider set to %X and Provenance[24-31] set to
3652 /// For vector types, all analysis is performed at the per-element level. No
3653 /// cross-element analysis is supported (shuffle/insertion/reduction), and all
3654 /// constant masks must be splatted across all elements.
3656 /// To avoid revisiting values, the BitPart results are memoized into the
3657 /// provided map. To avoid unnecessary copying of BitParts, BitParts are
3658 /// constructed in-place in the \c BPS map. Because of this \c BPS needs to
3659 /// store BitParts objects, not pointers. As we need the concept of a nullptr
3660 /// BitParts (Value has been analyzed and the analysis failed), we an Optional
3661 /// type instead to provide the same functionality.
3663 /// Because we pass around references into \c BPS, we must use a container that
3664 /// does not invalidate internal references (std::map instead of DenseMap).
3665 static const std::optional
<BitPart
> &
3666 collectBitParts(Value
*V
, bool MatchBSwaps
, bool MatchBitReversals
,
3667 std::map
<Value
*, std::optional
<BitPart
>> &BPS
, int Depth
,
3669 auto I
= BPS
.find(V
);
3673 auto &Result
= BPS
[V
] = std::nullopt
;
3674 auto BitWidth
= V
->getType()->getScalarSizeInBits();
3676 // Can't do integer/elements > 128 bits.
3680 // Prevent stack overflow by limiting the recursion depth
3681 if (Depth
== BitPartRecursionMaxDepth
) {
3682 LLVM_DEBUG(dbgs() << "collectBitParts max recursion depth reached.\n");
3686 if (auto *I
= dyn_cast
<Instruction
>(V
)) {
3690 // If this is an or instruction, it may be an inner node of the bswap.
3691 if (match(V
, m_Or(m_Value(X
), m_Value(Y
)))) {
3692 // Check we have both sources and they are from the same provider.
3693 const auto &A
= collectBitParts(X
, MatchBSwaps
, MatchBitReversals
, BPS
,
3694 Depth
+ 1, FoundRoot
);
3695 if (!A
|| !A
->Provider
)
3698 const auto &B
= collectBitParts(Y
, MatchBSwaps
, MatchBitReversals
, BPS
,
3699 Depth
+ 1, FoundRoot
);
3700 if (!B
|| A
->Provider
!= B
->Provider
)
3703 // Try and merge the two together.
3704 Result
= BitPart(A
->Provider
, BitWidth
);
3705 for (unsigned BitIdx
= 0; BitIdx
< BitWidth
; ++BitIdx
) {
3706 if (A
->Provenance
[BitIdx
] != BitPart::Unset
&&
3707 B
->Provenance
[BitIdx
] != BitPart::Unset
&&
3708 A
->Provenance
[BitIdx
] != B
->Provenance
[BitIdx
])
3709 return Result
= std::nullopt
;
3711 if (A
->Provenance
[BitIdx
] == BitPart::Unset
)
3712 Result
->Provenance
[BitIdx
] = B
->Provenance
[BitIdx
];
3714 Result
->Provenance
[BitIdx
] = A
->Provenance
[BitIdx
];
3720 // If this is a logical shift by a constant, recurse then shift the result.
3721 if (match(V
, m_LogicalShift(m_Value(X
), m_APInt(C
)))) {
3722 const APInt
&BitShift
= *C
;
3724 // Ensure the shift amount is defined.
3725 if (BitShift
.uge(BitWidth
))
3728 // For bswap-only, limit shift amounts to whole bytes, for an early exit.
3729 if (!MatchBitReversals
&& (BitShift
.getZExtValue() % 8) != 0)
3732 const auto &Res
= collectBitParts(X
, MatchBSwaps
, MatchBitReversals
, BPS
,
3733 Depth
+ 1, FoundRoot
);
3738 // Perform the "shift" on BitProvenance.
3739 auto &P
= Result
->Provenance
;
3740 if (I
->getOpcode() == Instruction::Shl
) {
3741 P
.erase(std::prev(P
.end(), BitShift
.getZExtValue()), P
.end());
3742 P
.insert(P
.begin(), BitShift
.getZExtValue(), BitPart::Unset
);
3744 P
.erase(P
.begin(), std::next(P
.begin(), BitShift
.getZExtValue()));
3745 P
.insert(P
.end(), BitShift
.getZExtValue(), BitPart::Unset
);
3751 // If this is a logical 'and' with a mask that clears bits, recurse then
3752 // unset the appropriate bits.
3753 if (match(V
, m_And(m_Value(X
), m_APInt(C
)))) {
3754 const APInt
&AndMask
= *C
;
3756 // Check that the mask allows a multiple of 8 bits for a bswap, for an
3758 unsigned NumMaskedBits
= AndMask
.popcount();
3759 if (!MatchBitReversals
&& (NumMaskedBits
% 8) != 0)
3762 const auto &Res
= collectBitParts(X
, MatchBSwaps
, MatchBitReversals
, BPS
,
3763 Depth
+ 1, FoundRoot
);
3768 for (unsigned BitIdx
= 0; BitIdx
< BitWidth
; ++BitIdx
)
3769 // If the AndMask is zero for this bit, clear the bit.
3770 if (AndMask
[BitIdx
] == 0)
3771 Result
->Provenance
[BitIdx
] = BitPart::Unset
;
3775 // If this is a zext instruction zero extend the result.
3776 if (match(V
, m_ZExt(m_Value(X
)))) {
3777 const auto &Res
= collectBitParts(X
, MatchBSwaps
, MatchBitReversals
, BPS
,
3778 Depth
+ 1, FoundRoot
);
3782 Result
= BitPart(Res
->Provider
, BitWidth
);
3783 auto NarrowBitWidth
= X
->getType()->getScalarSizeInBits();
3784 for (unsigned BitIdx
= 0; BitIdx
< NarrowBitWidth
; ++BitIdx
)
3785 Result
->Provenance
[BitIdx
] = Res
->Provenance
[BitIdx
];
3786 for (unsigned BitIdx
= NarrowBitWidth
; BitIdx
< BitWidth
; ++BitIdx
)
3787 Result
->Provenance
[BitIdx
] = BitPart::Unset
;
3791 // If this is a truncate instruction, extract the lower bits.
3792 if (match(V
, m_Trunc(m_Value(X
)))) {
3793 const auto &Res
= collectBitParts(X
, MatchBSwaps
, MatchBitReversals
, BPS
,
3794 Depth
+ 1, FoundRoot
);
3798 Result
= BitPart(Res
->Provider
, BitWidth
);
3799 for (unsigned BitIdx
= 0; BitIdx
< BitWidth
; ++BitIdx
)
3800 Result
->Provenance
[BitIdx
] = Res
->Provenance
[BitIdx
];
3804 // BITREVERSE - most likely due to us previous matching a partial
3806 if (match(V
, m_BitReverse(m_Value(X
)))) {
3807 const auto &Res
= collectBitParts(X
, MatchBSwaps
, MatchBitReversals
, BPS
,
3808 Depth
+ 1, FoundRoot
);
3812 Result
= BitPart(Res
->Provider
, BitWidth
);
3813 for (unsigned BitIdx
= 0; BitIdx
< BitWidth
; ++BitIdx
)
3814 Result
->Provenance
[(BitWidth
- 1) - BitIdx
] = Res
->Provenance
[BitIdx
];
3818 // BSWAP - most likely due to us previous matching a partial bswap.
3819 if (match(V
, m_BSwap(m_Value(X
)))) {
3820 const auto &Res
= collectBitParts(X
, MatchBSwaps
, MatchBitReversals
, BPS
,
3821 Depth
+ 1, FoundRoot
);
3825 unsigned ByteWidth
= BitWidth
/ 8;
3826 Result
= BitPart(Res
->Provider
, BitWidth
);
3827 for (unsigned ByteIdx
= 0; ByteIdx
< ByteWidth
; ++ByteIdx
) {
3828 unsigned ByteBitOfs
= ByteIdx
* 8;
3829 for (unsigned BitIdx
= 0; BitIdx
< 8; ++BitIdx
)
3830 Result
->Provenance
[(BitWidth
- 8 - ByteBitOfs
) + BitIdx
] =
3831 Res
->Provenance
[ByteBitOfs
+ BitIdx
];
3836 // Funnel 'double' shifts take 3 operands, 2 inputs and the shift
3838 // fshl(X,Y,Z): (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3839 // fshr(X,Y,Z): (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3840 if (match(V
, m_FShl(m_Value(X
), m_Value(Y
), m_APInt(C
))) ||
3841 match(V
, m_FShr(m_Value(X
), m_Value(Y
), m_APInt(C
)))) {
3842 // We can treat fshr as a fshl by flipping the modulo amount.
3843 unsigned ModAmt
= C
->urem(BitWidth
);
3844 if (cast
<IntrinsicInst
>(I
)->getIntrinsicID() == Intrinsic::fshr
)
3845 ModAmt
= BitWidth
- ModAmt
;
3847 // For bswap-only, limit shift amounts to whole bytes, for an early exit.
3848 if (!MatchBitReversals
&& (ModAmt
% 8) != 0)
3851 // Check we have both sources and they are from the same provider.
3852 const auto &LHS
= collectBitParts(X
, MatchBSwaps
, MatchBitReversals
, BPS
,
3853 Depth
+ 1, FoundRoot
);
3854 if (!LHS
|| !LHS
->Provider
)
3857 const auto &RHS
= collectBitParts(Y
, MatchBSwaps
, MatchBitReversals
, BPS
,
3858 Depth
+ 1, FoundRoot
);
3859 if (!RHS
|| LHS
->Provider
!= RHS
->Provider
)
3862 unsigned StartBitRHS
= BitWidth
- ModAmt
;
3863 Result
= BitPart(LHS
->Provider
, BitWidth
);
3864 for (unsigned BitIdx
= 0; BitIdx
< StartBitRHS
; ++BitIdx
)
3865 Result
->Provenance
[BitIdx
+ ModAmt
] = LHS
->Provenance
[BitIdx
];
3866 for (unsigned BitIdx
= 0; BitIdx
< ModAmt
; ++BitIdx
)
3867 Result
->Provenance
[BitIdx
] = RHS
->Provenance
[BitIdx
+ StartBitRHS
];
3872 // If we've already found a root input value then we're never going to merge
3873 // these back together.
3877 // Okay, we got to something that isn't a shift, 'or', 'and', etc. This must
3878 // be the root input value to the bswap/bitreverse.
3880 Result
= BitPart(V
, BitWidth
);
3881 for (unsigned BitIdx
= 0; BitIdx
< BitWidth
; ++BitIdx
)
3882 Result
->Provenance
[BitIdx
] = BitIdx
;
3886 static bool bitTransformIsCorrectForBSwap(unsigned From
, unsigned To
,
3887 unsigned BitWidth
) {
3888 if (From
% 8 != To
% 8)
3890 // Convert from bit indices to byte indices and check for a byte reversal.
3894 return From
== BitWidth
- To
- 1;
3897 static bool bitTransformIsCorrectForBitReverse(unsigned From
, unsigned To
,
3898 unsigned BitWidth
) {
3899 return From
== BitWidth
- To
- 1;
3902 bool llvm::recognizeBSwapOrBitReverseIdiom(
3903 Instruction
*I
, bool MatchBSwaps
, bool MatchBitReversals
,
3904 SmallVectorImpl
<Instruction
*> &InsertedInsts
) {
3905 if (!match(I
, m_Or(m_Value(), m_Value())) &&
3906 !match(I
, m_FShl(m_Value(), m_Value(), m_Value())) &&
3907 !match(I
, m_FShr(m_Value(), m_Value(), m_Value())) &&
3908 !match(I
, m_BSwap(m_Value())))
3910 if (!MatchBSwaps
&& !MatchBitReversals
)
3912 Type
*ITy
= I
->getType();
3913 if (!ITy
->isIntOrIntVectorTy() || ITy
->getScalarSizeInBits() > 128)
3914 return false; // Can't do integer/elements > 128 bits.
3916 // Try to find all the pieces corresponding to the bswap.
3917 bool FoundRoot
= false;
3918 std::map
<Value
*, std::optional
<BitPart
>> BPS
;
3920 collectBitParts(I
, MatchBSwaps
, MatchBitReversals
, BPS
, 0, FoundRoot
);
3923 ArrayRef
<int8_t> BitProvenance
= Res
->Provenance
;
3924 assert(all_of(BitProvenance
,
3925 [](int8_t I
) { return I
== BitPart::Unset
|| 0 <= I
; }) &&
3926 "Illegal bit provenance index");
3928 // If the upper bits are zero, then attempt to perform as a truncated op.
3929 Type
*DemandedTy
= ITy
;
3930 if (BitProvenance
.back() == BitPart::Unset
) {
3931 while (!BitProvenance
.empty() && BitProvenance
.back() == BitPart::Unset
)
3932 BitProvenance
= BitProvenance
.drop_back();
3933 if (BitProvenance
.empty())
3934 return false; // TODO - handle null value?
3935 DemandedTy
= Type::getIntNTy(I
->getContext(), BitProvenance
.size());
3936 if (auto *IVecTy
= dyn_cast
<VectorType
>(ITy
))
3937 DemandedTy
= VectorType::get(DemandedTy
, IVecTy
);
3940 // Check BitProvenance hasn't found a source larger than the result type.
3941 unsigned DemandedBW
= DemandedTy
->getScalarSizeInBits();
3942 if (DemandedBW
> ITy
->getScalarSizeInBits())
3945 // Now, is the bit permutation correct for a bswap or a bitreverse? We can
3946 // only byteswap values with an even number of bytes.
3947 APInt DemandedMask
= APInt::getAllOnes(DemandedBW
);
3948 bool OKForBSwap
= MatchBSwaps
&& (DemandedBW
% 16) == 0;
3949 bool OKForBitReverse
= MatchBitReversals
;
3950 for (unsigned BitIdx
= 0;
3951 (BitIdx
< DemandedBW
) && (OKForBSwap
|| OKForBitReverse
); ++BitIdx
) {
3952 if (BitProvenance
[BitIdx
] == BitPart::Unset
) {
3953 DemandedMask
.clearBit(BitIdx
);
3956 OKForBSwap
&= bitTransformIsCorrectForBSwap(BitProvenance
[BitIdx
], BitIdx
,
3958 OKForBitReverse
&= bitTransformIsCorrectForBitReverse(BitProvenance
[BitIdx
],
3959 BitIdx
, DemandedBW
);
3962 Intrinsic::ID Intrin
;
3964 Intrin
= Intrinsic::bswap
;
3965 else if (OKForBitReverse
)
3966 Intrin
= Intrinsic::bitreverse
;
3970 Function
*F
= Intrinsic::getDeclaration(I
->getModule(), Intrin
, DemandedTy
);
3971 Value
*Provider
= Res
->Provider
;
3973 // We may need to truncate the provider.
3974 if (DemandedTy
!= Provider
->getType()) {
3976 CastInst::CreateIntegerCast(Provider
, DemandedTy
, false, "trunc", I
);
3977 InsertedInsts
.push_back(Trunc
);
3981 Instruction
*Result
= CallInst::Create(F
, Provider
, "rev", I
);
3982 InsertedInsts
.push_back(Result
);
3984 if (!DemandedMask
.isAllOnes()) {
3985 auto *Mask
= ConstantInt::get(DemandedTy
, DemandedMask
);
3986 Result
= BinaryOperator::Create(Instruction::And
, Result
, Mask
, "mask", I
);
3987 InsertedInsts
.push_back(Result
);
3990 // We may need to zeroextend back to the result type.
3991 if (ITy
!= Result
->getType()) {
3992 auto *ExtInst
= CastInst::CreateIntegerCast(Result
, ITy
, false, "zext", I
);
3993 InsertedInsts
.push_back(ExtInst
);
3999 // CodeGen has special handling for some string functions that may replace
4000 // them with target-specific intrinsics. Since that'd skip our interceptors
4001 // in ASan/MSan/TSan/DFSan, and thus make us miss some memory accesses,
4002 // we mark affected calls as NoBuiltin, which will disable optimization
4004 void llvm::maybeMarkSanitizerLibraryCallNoBuiltin(
4005 CallInst
*CI
, const TargetLibraryInfo
*TLI
) {
4006 Function
*F
= CI
->getCalledFunction();
4008 if (F
&& !F
->hasLocalLinkage() && F
->hasName() &&
4009 TLI
->getLibFunc(F
->getName(), Func
) && TLI
->hasOptimizedCodeGen(Func
) &&
4010 !F
->doesNotAccessMemory())
4011 CI
->addFnAttr(Attribute::NoBuiltin
);
4014 bool llvm::canReplaceOperandWithVariable(const Instruction
*I
, unsigned OpIdx
) {
4015 // We can't have a PHI with a metadata type.
4016 if (I
->getOperand(OpIdx
)->getType()->isMetadataTy())
4020 if (!isa
<Constant
>(I
->getOperand(OpIdx
)))
4023 switch (I
->getOpcode()) {
4026 case Instruction::Call
:
4027 case Instruction::Invoke
: {
4028 const auto &CB
= cast
<CallBase
>(*I
);
4030 // Can't handle inline asm. Skip it.
4031 if (CB
.isInlineAsm())
4034 // Constant bundle operands may need to retain their constant-ness for
4036 if (CB
.isBundleOperand(OpIdx
))
4039 if (OpIdx
< CB
.arg_size()) {
4040 // Some variadic intrinsics require constants in the variadic arguments,
4041 // which currently aren't markable as immarg.
4042 if (isa
<IntrinsicInst
>(CB
) &&
4043 OpIdx
>= CB
.getFunctionType()->getNumParams()) {
4044 // This is known to be OK for stackmap.
4045 return CB
.getIntrinsicID() == Intrinsic::experimental_stackmap
;
4048 // gcroot is a special case, since it requires a constant argument which
4049 // isn't also required to be a simple ConstantInt.
4050 if (CB
.getIntrinsicID() == Intrinsic::gcroot
)
4053 // Some intrinsic operands are required to be immediates.
4054 return !CB
.paramHasAttr(OpIdx
, Attribute::ImmArg
);
4057 // It is never allowed to replace the call argument to an intrinsic, but it
4058 // may be possible for a call.
4059 return !isa
<IntrinsicInst
>(CB
);
4061 case Instruction::ShuffleVector
:
4062 // Shufflevector masks are constant.
4064 case Instruction::Switch
:
4065 case Instruction::ExtractValue
:
4066 // All operands apart from the first are constant.
4068 case Instruction::InsertValue
:
4069 // All operands apart from the first and the second are constant.
4071 case Instruction::Alloca
:
4072 // Static allocas (constant size in the entry block) are handled by
4073 // prologue/epilogue insertion so they're free anyway. We definitely don't
4074 // want to make them non-constant.
4075 return !cast
<AllocaInst
>(I
)->isStaticAlloca();
4076 case Instruction::GetElementPtr
:
4079 gep_type_iterator It
= gep_type_begin(I
);
4080 for (auto E
= std::next(It
, OpIdx
); It
!= E
; ++It
)
4087 Value
*llvm::invertCondition(Value
*Condition
) {
4088 // First: Check if it's a constant
4089 if (Constant
*C
= dyn_cast
<Constant
>(Condition
))
4090 return ConstantExpr::getNot(C
);
4092 // Second: If the condition is already inverted, return the original value
4093 Value
*NotCondition
;
4094 if (match(Condition
, m_Not(m_Value(NotCondition
))))
4095 return NotCondition
;
4097 BasicBlock
*Parent
= nullptr;
4098 Instruction
*Inst
= dyn_cast
<Instruction
>(Condition
);
4100 Parent
= Inst
->getParent();
4101 else if (Argument
*Arg
= dyn_cast
<Argument
>(Condition
))
4102 Parent
= &Arg
->getParent()->getEntryBlock();
4103 assert(Parent
&& "Unsupported condition to invert");
4105 // Third: Check all the users for an invert
4106 for (User
*U
: Condition
->users())
4107 if (Instruction
*I
= dyn_cast
<Instruction
>(U
))
4108 if (I
->getParent() == Parent
&& match(I
, m_Not(m_Specific(Condition
))))
4111 // Last option: Create a new instruction
4113 BinaryOperator::CreateNot(Condition
, Condition
->getName() + ".inv");
4114 if (Inst
&& !isa
<PHINode
>(Inst
))
4115 Inverted
->insertAfter(Inst
);
4117 Inverted
->insertBefore(&*Parent
->getFirstInsertionPt());
4121 bool llvm::inferAttributesFromOthers(Function
&F
) {
4122 // Note: We explicitly check for attributes rather than using cover functions
4123 // because some of the cover functions include the logic being implemented.
4125 bool Changed
= false;
4126 // readnone + not convergent implies nosync
4127 if (!F
.hasFnAttribute(Attribute::NoSync
) &&
4128 F
.doesNotAccessMemory() && !F
.isConvergent()) {
4133 // readonly implies nofree
4134 if (!F
.hasFnAttribute(Attribute::NoFree
) && F
.onlyReadsMemory()) {
4135 F
.setDoesNotFreeMemory();
4139 // willreturn implies mustprogress
4140 if (!F
.hasFnAttribute(Attribute::MustProgress
) && F
.willReturn()) {
4141 F
.setMustProgress();
4145 // TODO: There are a bunch of cases of restrictive memory effects we
4146 // can infer by inspecting arguments of argmemonly-ish functions.