LLVM 17.0.0git
JumpThreading.cpp
Go to the documentation of this file.
1//===- JumpThreading.cpp - Thread control through conditional blocks ------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Jump Threading pass.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/DenseSet.h"
16#include "llvm/ADT/MapVector.h"
17#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/Statistic.h"
24#include "llvm/Analysis/CFG.h"
30#include "llvm/Analysis/Loads.h"
37#include "llvm/IR/BasicBlock.h"
38#include "llvm/IR/CFG.h"
39#include "llvm/IR/Constant.h"
41#include "llvm/IR/Constants.h"
42#include "llvm/IR/DataLayout.h"
43#include "llvm/IR/DebugInfo.h"
44#include "llvm/IR/Dominators.h"
45#include "llvm/IR/Function.h"
46#include "llvm/IR/InstrTypes.h"
47#include "llvm/IR/Instruction.h"
50#include "llvm/IR/Intrinsics.h"
51#include "llvm/IR/LLVMContext.h"
52#include "llvm/IR/MDBuilder.h"
53#include "llvm/IR/Metadata.h"
54#include "llvm/IR/Module.h"
55#include "llvm/IR/PassManager.h"
58#include "llvm/IR/Type.h"
59#include "llvm/IR/Use.h"
60#include "llvm/IR/Value.h"
62#include "llvm/Pass.h"
67#include "llvm/Support/Debug.h"
75#include <algorithm>
76#include <cassert>
77#include <cstdint>
78#include <iterator>
79#include <memory>
80#include <utility>
81
82using namespace llvm;
83using namespace jumpthreading;
84
85#define DEBUG_TYPE "jump-threading"
86
87STATISTIC(NumThreads, "Number of jumps threaded");
88STATISTIC(NumFolds, "Number of terminators folded");
89STATISTIC(NumDupes, "Number of branch blocks duplicated to eliminate phi");
90
92BBDuplicateThreshold("jump-threading-threshold",
93 cl::desc("Max block size to duplicate for jump threading"),
95
98 "jump-threading-implication-search-threshold",
99 cl::desc("The number of predecessors to search for a stronger "
100 "condition to use to thread over a weaker condition"),
101 cl::init(3), cl::Hidden);
102
104 "jump-threading-phi-threshold",
105 cl::desc("Max PHIs in BB to duplicate for jump threading"), cl::init(76),
106 cl::Hidden);
107
109 "print-lvi-after-jump-threading",
110 cl::desc("Print the LazyValueInfo cache after JumpThreading"), cl::init(false),
111 cl::Hidden);
112
114 "jump-threading-across-loop-headers",
115 cl::desc("Allow JumpThreading to thread across loop headers, for testing"),
116 cl::init(false), cl::Hidden);
117
119 DefaultBBDupThreshold = (T == -1) ? BBDuplicateThreshold : unsigned(T);
120}
121
122// Update branch probability information according to conditional
123// branch probability. This is usually made possible for cloned branches
124// in inline instances by the context specific profile in the caller.
125// For instance,
126//
127// [Block PredBB]
128// [Branch PredBr]
129// if (t) {
130// Block A;
131// } else {
132// Block B;
133// }
134//
135// [Block BB]
136// cond = PN([true, %A], [..., %B]); // PHI node
137// [Branch CondBr]
138// if (cond) {
139// ... // P(cond == true) = 1%
140// }
141//
142// Here we know that when block A is taken, cond must be true, which means
143// P(cond == true | A) = 1
144//
145// Given that P(cond == true) = P(cond == true | A) * P(A) +
146// P(cond == true | B) * P(B)
147// we get:
148// P(cond == true ) = P(A) + P(cond == true | B) * P(B)
149//
150// which gives us:
151// P(A) is less than P(cond == true), i.e.
152// P(t == true) <= P(cond == true)
153//
154// In other words, if we know P(cond == true) is unlikely, we know
155// that P(t == true) is also unlikely.
156//
158 BranchInst *CondBr = dyn_cast<BranchInst>(BB->getTerminator());
159 if (!CondBr)
160 return;
161
162 uint64_t TrueWeight, FalseWeight;
163 if (!extractBranchWeights(*CondBr, TrueWeight, FalseWeight))
164 return;
165
166 if (TrueWeight + FalseWeight == 0)
167 // Zero branch_weights do not give a hint for getting branch probabilities.
168 // Technically it would result in division by zero denominator, which is
169 // TrueWeight + FalseWeight.
170 return;
171
172 // Returns the outgoing edge of the dominating predecessor block
173 // that leads to the PhiNode's incoming block:
174 auto GetPredOutEdge =
175 [](BasicBlock *IncomingBB,
176 BasicBlock *PhiBB) -> std::pair<BasicBlock *, BasicBlock *> {
177 auto *PredBB = IncomingBB;
178 auto *SuccBB = PhiBB;
180 while (true) {
181 BranchInst *PredBr = dyn_cast<BranchInst>(PredBB->getTerminator());
182 if (PredBr && PredBr->isConditional())
183 return {PredBB, SuccBB};
184 Visited.insert(PredBB);
185 auto *SinglePredBB = PredBB->getSinglePredecessor();
186 if (!SinglePredBB)
187 return {nullptr, nullptr};
188
189 // Stop searching when SinglePredBB has been visited. It means we see
190 // an unreachable loop.
191 if (Visited.count(SinglePredBB))
192 return {nullptr, nullptr};
193
194 SuccBB = PredBB;
195 PredBB = SinglePredBB;
196 }
197 };
198
199 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
200 Value *PhiOpnd = PN->getIncomingValue(i);
201 ConstantInt *CI = dyn_cast<ConstantInt>(PhiOpnd);
202
203 if (!CI || !CI->getType()->isIntegerTy(1))
204 continue;
205
208 TrueWeight, TrueWeight + FalseWeight)
210 FalseWeight, TrueWeight + FalseWeight));
211
212 auto PredOutEdge = GetPredOutEdge(PN->getIncomingBlock(i), BB);
213 if (!PredOutEdge.first)
214 return;
215
216 BasicBlock *PredBB = PredOutEdge.first;
217 BranchInst *PredBr = dyn_cast<BranchInst>(PredBB->getTerminator());
218 if (!PredBr)
219 return;
220
221 uint64_t PredTrueWeight, PredFalseWeight;
222 // FIXME: We currently only set the profile data when it is missing.
223 // With PGO, this can be used to refine even existing profile data with
224 // context information. This needs to be done after more performance
225 // testing.
226 if (extractBranchWeights(*PredBr, PredTrueWeight, PredFalseWeight))
227 continue;
228
229 // We can not infer anything useful when BP >= 50%, because BP is the
230 // upper bound probability value.
231 if (BP >= BranchProbability(50, 100))
232 continue;
233
235 if (PredBr->getSuccessor(0) == PredOutEdge.second) {
236 Weights.push_back(BP.getNumerator());
237 Weights.push_back(BP.getCompl().getNumerator());
238 } else {
239 Weights.push_back(BP.getCompl().getNumerator());
240 Weights.push_back(BP.getNumerator());
241 }
242 PredBr->setMetadata(LLVMContext::MD_prof,
243 MDBuilder(PredBr->getParent()->getContext())
244 .createBranchWeights(Weights));
245 }
246}
247
250 auto &TTI = AM.getResult<TargetIRAnalysis>(F);
251 // Jump Threading has no sense for the targets with divergent CF
253 return PreservedAnalyses::all();
254 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
255 auto &LVI = AM.getResult<LazyValueAnalysis>(F);
256 auto &AA = AM.getResult<AAManager>(F);
257 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
258
259 bool Changed =
260 runImpl(F, &AM, &TLI, &TTI, &LVI, &AA,
261 std::make_unique<DomTreeUpdater>(
263 std::nullopt, std::nullopt);
264
266 dbgs() << "LVI for function '" << F.getName() << "':\n";
267 LVI.printLVI(F, getDomTreeUpdater()->getDomTree(), dbgs());
268 }
269
270 if (!Changed)
271 return PreservedAnalyses::all();
272
273
275
276#if defined(EXPENSIVE_CHECKS)
277 assert(getDomTreeUpdater()->getDomTree().verify(
278 DominatorTree::VerificationLevel::Full) &&
279 "DT broken after JumpThreading");
280 assert((!getDomTreeUpdater()->hasPostDomTree() ||
281 getDomTreeUpdater()->getPostDomTree().verify(
283 "PDT broken after JumpThreading");
284#else
285 assert(getDomTreeUpdater()->getDomTree().verify(
286 DominatorTree::VerificationLevel::Fast) &&
287 "DT broken after JumpThreading");
288 assert((!getDomTreeUpdater()->hasPostDomTree() ||
289 getDomTreeUpdater()->getPostDomTree().verify(
291 "PDT broken after JumpThreading");
292#endif
293
294 return getPreservedAnalysis();
295}
296
298 TargetLibraryInfo *TLI_,
300 AliasAnalysis *AA_,
301 std::unique_ptr<DomTreeUpdater> DTU_,
302 std::optional<BlockFrequencyInfo *> BFI_,
303 std::optional<BranchProbabilityInfo *> BPI_) {
304 LLVM_DEBUG(dbgs() << "Jump threading on function '" << F_.getName() << "'\n");
305 F = &F_;
306 FAM = FAM_;
307 TLI = TLI_;
308 TTI = TTI_;
309 LVI = LVI_;
310 AA = AA_;
311 DTU = std::move(DTU_);
312 BFI = BFI_;
313 BPI = BPI_;
314 auto *GuardDecl = F->getParent()->getFunction(
315 Intrinsic::getName(Intrinsic::experimental_guard));
316 HasGuards = GuardDecl && !GuardDecl->use_empty();
317
318 // Reduce the number of instructions duplicated when optimizing strictly for
319 // size.
320 if (BBDuplicateThreshold.getNumOccurrences())
321 BBDupThreshold = BBDuplicateThreshold;
322 else if (F->hasFnAttribute(Attribute::MinSize))
323 BBDupThreshold = 3;
324 else
325 BBDupThreshold = DefaultBBDupThreshold;
326
327 // JumpThreading must not processes blocks unreachable from entry. It's a
328 // waste of compute time and can potentially lead to hangs.
330 assert(DTU && "DTU isn't passed into JumpThreading before using it.");
331 assert(DTU->hasDomTree() && "JumpThreading relies on DomTree to proceed.");
332 DominatorTree &DT = DTU->getDomTree();
333 for (auto &BB : *F)
334 if (!DT.isReachableFromEntry(&BB))
335 Unreachable.insert(&BB);
336
339
340 bool EverChanged = false;
341 bool Changed;
342 do {
343 Changed = false;
344 for (auto &BB : *F) {
345 if (Unreachable.count(&BB))
346 continue;
347 while (processBlock(&BB)) // Thread all of the branches we can over BB.
348 Changed = ChangedSinceLastAnalysisUpdate = true;
349
350 // Jump threading may have introduced redundant debug values into BB
351 // which should be removed.
352 if (Changed)
354
355 // Stop processing BB if it's the entry or is now deleted. The following
356 // routines attempt to eliminate BB and locating a suitable replacement
357 // for the entry is non-trivial.
358 if (&BB == &F->getEntryBlock() || DTU->isBBPendingDeletion(&BB))
359 continue;
360
361 if (pred_empty(&BB)) {
362 // When processBlock makes BB unreachable it doesn't bother to fix up
363 // the instructions in it. We must remove BB to prevent invalid IR.
364 LLVM_DEBUG(dbgs() << " JT: Deleting dead block '" << BB.getName()
365 << "' with terminator: " << *BB.getTerminator()
366 << '\n');
367 LoopHeaders.erase(&BB);
368 LVI->eraseBlock(&BB);
369 DeleteDeadBlock(&BB, DTU.get());
370 Changed = ChangedSinceLastAnalysisUpdate = true;
371 continue;
372 }
373
374 // processBlock doesn't thread BBs with unconditional TIs. However, if BB
375 // is "almost empty", we attempt to merge BB with its sole successor.
376 auto *BI = dyn_cast<BranchInst>(BB.getTerminator());
377 if (BI && BI->isUnconditional()) {
378 BasicBlock *Succ = BI->getSuccessor(0);
379 if (
380 // The terminator must be the only non-phi instruction in BB.
381 BB.getFirstNonPHIOrDbg(true)->isTerminator() &&
382 // Don't alter Loop headers and latches to ensure another pass can
383 // detect and transform nested loops later.
384 !LoopHeaders.count(&BB) && !LoopHeaders.count(Succ) &&
387 // BB is valid for cleanup here because we passed in DTU. F remains
388 // BB's parent until a DTU->getDomTree() event.
389 LVI->eraseBlock(&BB);
390 Changed = ChangedSinceLastAnalysisUpdate = true;
391 }
392 }
393 }
394 EverChanged |= Changed;
395 } while (Changed);
396
397 LoopHeaders.clear();
398 return EverChanged;
399}
400
401// Replace uses of Cond with ToVal when safe to do so. If all uses are
402// replaced, we can remove Cond. We cannot blindly replace all uses of Cond
403// because we may incorrectly replace uses when guards/assumes are uses of
404// of `Cond` and we used the guards/assume to reason about the `Cond` value
405// at the end of block. RAUW unconditionally replaces all uses
406// including the guards/assumes themselves and the uses before the
407// guard/assume.
409 BasicBlock *KnownAtEndOfBB) {
410 bool Changed = false;
411 assert(Cond->getType() == ToVal->getType());
412 // We can unconditionally replace all uses in non-local blocks (i.e. uses
413 // strictly dominated by BB), since LVI information is true from the
414 // terminator of BB.
415 if (Cond->getParent() == KnownAtEndOfBB)
416 Changed |= replaceNonLocalUsesWith(Cond, ToVal);
417 for (Instruction &I : reverse(*KnownAtEndOfBB)) {
418 // Reached the Cond whose uses we are trying to replace, so there are no
419 // more uses.
420 if (&I == Cond)
421 break;
422 // We only replace uses in instructions that are guaranteed to reach the end
423 // of BB, where we know Cond is ToVal.
425 break;
426 Changed |= I.replaceUsesOfWith(Cond, ToVal);
427 }
428 if (Cond->use_empty() && !Cond->mayHaveSideEffects()) {
429 Cond->eraseFromParent();
430 Changed = true;
431 }
432 return Changed;
433}
434
435/// Return the cost of duplicating a piece of this block from first non-phi
436/// and before StopAt instruction to thread across it. Stop scanning the block
437/// when exceeding the threshold. If duplication is impossible, returns ~0U.
439 BasicBlock *BB,
440 Instruction *StopAt,
441 unsigned Threshold) {
442 assert(StopAt->getParent() == BB && "Not an instruction from proper BB?");
443
444 // Do not duplicate the BB if it has a lot of PHI nodes.
445 // If a threadable chain is too long then the number of PHI nodes can add up,
446 // leading to a substantial increase in compile time when rewriting the SSA.
447 unsigned PhiCount = 0;
448 Instruction *FirstNonPHI = nullptr;
449 for (Instruction &I : *BB) {
450 if (!isa<PHINode>(&I)) {
451 FirstNonPHI = &I;
452 break;
453 }
454 if (++PhiCount > PhiDuplicateThreshold)
455 return ~0U;
456 }
457
458 /// Ignore PHI nodes, these will be flattened when duplication happens.
459 BasicBlock::const_iterator I(FirstNonPHI);
460
461 // FIXME: THREADING will delete values that are just used to compute the
462 // branch, so they shouldn't count against the duplication cost.
463
464 unsigned Bonus = 0;
465 if (BB->getTerminator() == StopAt) {
466 // Threading through a switch statement is particularly profitable. If this
467 // block ends in a switch, decrease its cost to make it more likely to
468 // happen.
469 if (isa<SwitchInst>(StopAt))
470 Bonus = 6;
471
472 // The same holds for indirect branches, but slightly more so.
473 if (isa<IndirectBrInst>(StopAt))
474 Bonus = 8;
475 }
476
477 // Bump the threshold up so the early exit from the loop doesn't skip the
478 // terminator-based Size adjustment at the end.
479 Threshold += Bonus;
480
481 // Sum up the cost of each instruction until we get to the terminator. Don't
482 // include the terminator because the copy won't include it.
483 unsigned Size = 0;
484 for (; &*I != StopAt; ++I) {
485
486 // Stop scanning the block if we've reached the threshold.
487 if (Size > Threshold)
488 return Size;
489
490 // Bail out if this instruction gives back a token type, it is not possible
491 // to duplicate it if it is used outside this BB.
492 if (I->getType()->isTokenTy() && I->isUsedOutsideOfBlock(BB))
493 return ~0U;
494
495 // Blocks with NoDuplicate are modelled as having infinite cost, so they
496 // are never duplicated.
497 if (const CallInst *CI = dyn_cast<CallInst>(I))
498 if (CI->cannotDuplicate() || CI->isConvergent())
499 return ~0U;
500
503 continue;
504
505 // All other instructions count for at least one unit.
506 ++Size;
507
508 // Calls are more expensive. If they are non-intrinsic calls, we model them
509 // as having cost of 4. If they are a non-vector intrinsic, we model them
510 // as having cost of 2 total, and if they are a vector intrinsic, we model
511 // them as having cost 1.
512 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
513 if (!isa<IntrinsicInst>(CI))
514 Size += 3;
515 else if (!CI->getType()->isVectorTy())
516 Size += 1;
517 }
518 }
519
520 return Size > Bonus ? Size - Bonus : 0;
521}
522
523/// findLoopHeaders - We do not want jump threading to turn proper loop
524/// structures into irreducible loops. Doing this breaks up the loop nesting
525/// hierarchy and pessimizes later transformations. To prevent this from
526/// happening, we first have to find the loop headers. Here we approximate this
527/// by finding targets of backedges in the CFG.
528///
529/// Note that there definitely are cases when we want to allow threading of
530/// edges across a loop header. For example, threading a jump from outside the
531/// loop (the preheader) to an exit block of the loop is definitely profitable.
532/// It is also almost always profitable to thread backedges from within the loop
533/// to exit blocks, and is often profitable to thread backedges to other blocks
534/// within the loop (forming a nested loop). This simple analysis is not rich
535/// enough to track all of these properties and keep it up-to-date as the CFG
536/// mutates, so we don't allow any of these transformations.
539 FindFunctionBackedges(F, Edges);
540
541 for (const auto &Edge : Edges)
542 LoopHeaders.insert(Edge.second);
543}
544
545/// getKnownConstant - Helper method to determine if we can thread over a
546/// terminator with the given value as its condition, and if so what value to
547/// use for that. What kind of value this is depends on whether we want an
548/// integer or a block address, but an undef is always accepted.
549/// Returns null if Val is null or not an appropriate constant.
551 if (!Val)
552 return nullptr;
553
554 // Undef is "known" enough.
555 if (UndefValue *U = dyn_cast<UndefValue>(Val))
556 return U;
557
558 if (Preference == WantBlockAddress)
559 return dyn_cast<BlockAddress>(Val->stripPointerCasts());
560
561 return dyn_cast<ConstantInt>(Val);
562}
563
564/// computeValueKnownInPredecessors - Given a basic block BB and a value V, see
565/// if we can infer that the value is a known ConstantInt/BlockAddress or undef
566/// in any of our predecessors. If so, return the known list of value and pred
567/// BB in the result vector.
568///
569/// This returns true if there were any known values.
571 Value *V, BasicBlock *BB, PredValueInfo &Result,
572 ConstantPreference Preference, DenseSet<Value *> &RecursionSet,
573 Instruction *CxtI) {
574 // This method walks up use-def chains recursively. Because of this, we could
575 // get into an infinite loop going around loops in the use-def chain. To
576 // prevent this, keep track of what (value, block) pairs we've already visited
577 // and terminate the search if we loop back to them
578 if (!RecursionSet.insert(V).second)
579 return false;
580
581 // If V is a constant, then it is known in all predecessors.
582 if (Constant *KC = getKnownConstant(V, Preference)) {
583 for (BasicBlock *Pred : predecessors(BB))
584 Result.emplace_back(KC, Pred);
585
586 return !Result.empty();
587 }
588
589 // If V is a non-instruction value, or an instruction in a different block,
590 // then it can't be derived from a PHI.
591 Instruction *I = dyn_cast<Instruction>(V);
592 if (!I || I->getParent() != BB) {
593
594 // Okay, if this is a live-in value, see if it has a known value at the any
595 // edge from our predecessors.
596 for (BasicBlock *P : predecessors(BB)) {
597 using namespace PatternMatch;
598 // If the value is known by LazyValueInfo to be a constant in a
599 // predecessor, use that information to try to thread this block.
600 Constant *PredCst = LVI->getConstantOnEdge(V, P, BB, CxtI);
601 // If I is a non-local compare-with-constant instruction, use more-rich
602 // 'getPredicateOnEdge' method. This would be able to handle value
603 // inequalities better, for example if the compare is "X < 4" and "X < 3"
604 // is known true but "X < 4" itself is not available.
606 Value *Val;
607 Constant *Cst;
608 if (!PredCst && match(V, m_Cmp(Pred, m_Value(Val), m_Constant(Cst)))) {
609 auto Res = LVI->getPredicateOnEdge(Pred, Val, Cst, P, BB, CxtI);
610 if (Res != LazyValueInfo::Unknown)
611 PredCst = ConstantInt::getBool(V->getContext(), Res);
612 }
613 if (Constant *KC = getKnownConstant(PredCst, Preference))
614 Result.emplace_back(KC, P);
615 }
616
617 return !Result.empty();
618 }
619
620 /// If I is a PHI node, then we know the incoming values for any constants.
621 if (PHINode *PN = dyn_cast<PHINode>(I)) {
622 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
623 Value *InVal = PN->getIncomingValue(i);
624 if (Constant *KC = getKnownConstant(InVal, Preference)) {
625 Result.emplace_back(KC, PN->getIncomingBlock(i));
626 } else {
627 Constant *CI = LVI->getConstantOnEdge(InVal,
628 PN->getIncomingBlock(i),
629 BB, CxtI);
630 if (Constant *KC = getKnownConstant(CI, Preference))
631 Result.emplace_back(KC, PN->getIncomingBlock(i));
632 }
633 }
634
635 return !Result.empty();
636 }
637
638 // Handle Cast instructions.
639 if (CastInst *CI = dyn_cast<CastInst>(I)) {
640 Value *Source = CI->getOperand(0);
641 computeValueKnownInPredecessorsImpl(Source, BB, Result, Preference,
642 RecursionSet, CxtI);
643 if (Result.empty())
644 return false;
645
646 // Convert the known values.
647 for (auto &R : Result)
648 R.first = ConstantExpr::getCast(CI->getOpcode(), R.first, CI->getType());
649
650 return true;
651 }
652
653 if (FreezeInst *FI = dyn_cast<FreezeInst>(I)) {
654 Value *Source = FI->getOperand(0);
655 computeValueKnownInPredecessorsImpl(Source, BB, Result, Preference,
656 RecursionSet, CxtI);
657
658 erase_if(Result, [](auto &Pair) {
659 return !isGuaranteedNotToBeUndefOrPoison(Pair.first);
660 });
661
662 return !Result.empty();
663 }
664
665 // Handle some boolean conditions.
666 if (I->getType()->getPrimitiveSizeInBits() == 1) {
667 using namespace PatternMatch;
668 if (Preference != WantInteger)
669 return false;
670 // X | true -> true
671 // X & false -> false
672 Value *Op0, *Op1;
673 if (match(I, m_LogicalOr(m_Value(Op0), m_Value(Op1))) ||
674 match(I, m_LogicalAnd(m_Value(Op0), m_Value(Op1)))) {
675 PredValueInfoTy LHSVals, RHSVals;
676
678 RecursionSet, CxtI);
680 RecursionSet, CxtI);
681
682 if (LHSVals.empty() && RHSVals.empty())
683 return false;
684
685 ConstantInt *InterestingVal;
686 if (match(I, m_LogicalOr()))
687 InterestingVal = ConstantInt::getTrue(I->getContext());
688 else
689 InterestingVal = ConstantInt::getFalse(I->getContext());
690
691 SmallPtrSet<BasicBlock*, 4> LHSKnownBBs;
692
693 // Scan for the sentinel. If we find an undef, force it to the
694 // interesting value: x|undef -> true and x&undef -> false.
695 for (const auto &LHSVal : LHSVals)
696 if (LHSVal.first == InterestingVal || isa<UndefValue>(LHSVal.first)) {
697 Result.emplace_back(InterestingVal, LHSVal.second);
698 LHSKnownBBs.insert(LHSVal.second);
699 }
700 for (const auto &RHSVal : RHSVals)
701 if (RHSVal.first == InterestingVal || isa<UndefValue>(RHSVal.first)) {
702 // If we already inferred a value for this block on the LHS, don't
703 // re-add it.
704 if (!LHSKnownBBs.count(RHSVal.second))
705 Result.emplace_back(InterestingVal, RHSVal.second);
706 }
707
708 return !Result.empty();
709 }
710
711 // Handle the NOT form of XOR.
712 if (I->getOpcode() == Instruction::Xor &&
713 isa<ConstantInt>(I->getOperand(1)) &&
714 cast<ConstantInt>(I->getOperand(1))->isOne()) {
715 computeValueKnownInPredecessorsImpl(I->getOperand(0), BB, Result,
716 WantInteger, RecursionSet, CxtI);
717 if (Result.empty())
718 return false;
719
720 // Invert the known values.
721 for (auto &R : Result)
722 R.first = ConstantExpr::getNot(R.first);
723
724 return true;
725 }
726
727 // Try to simplify some other binary operator values.
728 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
729 if (Preference != WantInteger)
730 return false;
731 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
732 const DataLayout &DL = BO->getModule()->getDataLayout();
733 PredValueInfoTy LHSVals;
734 computeValueKnownInPredecessorsImpl(BO->getOperand(0), BB, LHSVals,
735 WantInteger, RecursionSet, CxtI);
736
737 // Try to use constant folding to simplify the binary operator.
738 for (const auto &LHSVal : LHSVals) {
739 Constant *V = LHSVal.first;
740 Constant *Folded =
741 ConstantFoldBinaryOpOperands(BO->getOpcode(), V, CI, DL);
742
743 if (Constant *KC = getKnownConstant(Folded, WantInteger))
744 Result.emplace_back(KC, LHSVal.second);
745 }
746 }
747
748 return !Result.empty();
749 }
750
751 // Handle compare with phi operand, where the PHI is defined in this block.
752 if (CmpInst *Cmp = dyn_cast<CmpInst>(I)) {
753 if (Preference != WantInteger)
754 return false;
755 Type *CmpType = Cmp->getType();
756 Value *CmpLHS = Cmp->getOperand(0);
757 Value *CmpRHS = Cmp->getOperand(1);
758 CmpInst::Predicate Pred = Cmp->getPredicate();
759
760 PHINode *PN = dyn_cast<PHINode>(CmpLHS);
761 if (!PN)
762 PN = dyn_cast<PHINode>(CmpRHS);
763 if (PN && PN->getParent() == BB) {
764 const DataLayout &DL = PN->getModule()->getDataLayout();
765 // We can do this simplification if any comparisons fold to true or false.
766 // See if any do.
767 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
768 BasicBlock *PredBB = PN->getIncomingBlock(i);
769 Value *LHS, *RHS;
770 if (PN == CmpLHS) {
771 LHS = PN->getIncomingValue(i);
772 RHS = CmpRHS->DoPHITranslation(BB, PredBB);
773 } else {
774 LHS = CmpLHS->DoPHITranslation(BB, PredBB);
775 RHS = PN->getIncomingValue(i);
776 }
777 Value *Res = simplifyCmpInst(Pred, LHS, RHS, {DL});
778 if (!Res) {
779 if (!isa<Constant>(RHS))
780 continue;
781
782 // getPredicateOnEdge call will make no sense if LHS is defined in BB.
783 auto LHSInst = dyn_cast<Instruction>(LHS);
784 if (LHSInst && LHSInst->getParent() == BB)
785 continue;
786
788 ResT = LVI->getPredicateOnEdge(Pred, LHS,
789 cast<Constant>(RHS), PredBB, BB,
790 CxtI ? CxtI : Cmp);
791 if (ResT == LazyValueInfo::Unknown)
792 continue;
794 }
795
796 if (Constant *KC = getKnownConstant(Res, WantInteger))
797 Result.emplace_back(KC, PredBB);
798 }
799
800 return !Result.empty();
801 }
802
803 // If comparing a live-in value against a constant, see if we know the
804 // live-in value on any predecessors.
805 if (isa<Constant>(CmpRHS) && !CmpType->isVectorTy()) {
806 Constant *CmpConst = cast<Constant>(CmpRHS);
807
808 if (!isa<Instruction>(CmpLHS) ||
809 cast<Instruction>(CmpLHS)->getParent() != BB) {
810 for (BasicBlock *P : predecessors(BB)) {
811 // If the value is known by LazyValueInfo to be a constant in a
812 // predecessor, use that information to try to thread this block.
814 LVI->getPredicateOnEdge(Pred, CmpLHS,
815 CmpConst, P, BB, CxtI ? CxtI : Cmp);
816 if (Res == LazyValueInfo::Unknown)
817 continue;
818
819 Constant *ResC = ConstantInt::get(CmpType, Res);
820 Result.emplace_back(ResC, P);
821 }
822
823 return !Result.empty();
824 }
825
826 // InstCombine can fold some forms of constant range checks into
827 // (icmp (add (x, C1)), C2). See if we have we have such a thing with
828 // x as a live-in.
829 {
830 using namespace PatternMatch;
831
832 Value *AddLHS;
833 ConstantInt *AddConst;
834 if (isa<ConstantInt>(CmpConst) &&
835 match(CmpLHS, m_Add(m_Value(AddLHS), m_ConstantInt(AddConst)))) {
836 if (!isa<Instruction>(AddLHS) ||
837 cast<Instruction>(AddLHS)->getParent() != BB) {
838 for (BasicBlock *P : predecessors(BB)) {
839 // If the value is known by LazyValueInfo to be a ConstantRange in
840 // a predecessor, use that information to try to thread this
841 // block.
843 AddLHS, P, BB, CxtI ? CxtI : cast<Instruction>(CmpLHS));
844 // Propagate the range through the addition.
845 CR = CR.add(AddConst->getValue());
846
847 // Get the range where the compare returns true.
849 Pred, cast<ConstantInt>(CmpConst)->getValue());
850
851 Constant *ResC;
852 if (CmpRange.contains(CR))
853 ResC = ConstantInt::getTrue(CmpType);
854 else if (CmpRange.inverse().contains(CR))
855 ResC = ConstantInt::getFalse(CmpType);
856 else
857 continue;
858
859 Result.emplace_back(ResC, P);
860 }
861
862 return !Result.empty();
863 }
864 }
865 }
866
867 // Try to find a constant value for the LHS of a comparison,
868 // and evaluate it statically if we can.
869 PredValueInfoTy LHSVals;
870 computeValueKnownInPredecessorsImpl(I->getOperand(0), BB, LHSVals,
871 WantInteger, RecursionSet, CxtI);
872
873 for (const auto &LHSVal : LHSVals) {
874 Constant *V = LHSVal.first;
875 Constant *Folded = ConstantExpr::getCompare(Pred, V, CmpConst);
876 if (Constant *KC = getKnownConstant(Folded, WantInteger))
877 Result.emplace_back(KC, LHSVal.second);
878 }
879
880 return !Result.empty();
881 }
882 }
883
884 if (SelectInst *SI = dyn_cast<SelectInst>(I)) {
885 // Handle select instructions where at least one operand is a known constant
886 // and we can figure out the condition value for any predecessor block.
887 Constant *TrueVal = getKnownConstant(SI->getTrueValue(), Preference);
888 Constant *FalseVal = getKnownConstant(SI->getFalseValue(), Preference);
889 PredValueInfoTy Conds;
890 if ((TrueVal || FalseVal) &&
891 computeValueKnownInPredecessorsImpl(SI->getCondition(), BB, Conds,
892 WantInteger, RecursionSet, CxtI)) {
893 for (auto &C : Conds) {
894 Constant *Cond = C.first;
895
896 // Figure out what value to use for the condition.
897 bool KnownCond;
898 if (ConstantInt *CI = dyn_cast<ConstantInt>(Cond)) {
899 // A known boolean.
900 KnownCond = CI->isOne();
901 } else {
902 assert(isa<UndefValue>(Cond) && "Unexpected condition value");
903 // Either operand will do, so be sure to pick the one that's a known
904 // constant.
905 // FIXME: Do this more cleverly if both values are known constants?
906 KnownCond = (TrueVal != nullptr);
907 }
908
909 // See if the select has a known constant value for this predecessor.
910 if (Constant *Val = KnownCond ? TrueVal : FalseVal)
911 Result.emplace_back(Val, C.second);
912 }
913
914 return !Result.empty();
915 }
916 }
917
918 // If all else fails, see if LVI can figure out a constant value for us.
919 assert(CxtI->getParent() == BB && "CxtI should be in BB");
920 Constant *CI = LVI->getConstant(V, CxtI);
921 if (Constant *KC = getKnownConstant(CI, Preference)) {
922 for (BasicBlock *Pred : predecessors(BB))
923 Result.emplace_back(KC, Pred);
924 }
925
926 return !Result.empty();
927}
928
929/// GetBestDestForBranchOnUndef - If we determine that the specified block ends
930/// in an undefined jump, decide which block is best to revector to.
931///
932/// Since we can pick an arbitrary destination, we pick the successor with the
933/// fewest predecessors. This should reduce the in-degree of the others.
935 Instruction *BBTerm = BB->getTerminator();
936 unsigned MinSucc = 0;
937 BasicBlock *TestBB = BBTerm->getSuccessor(MinSucc);
938 // Compute the successor with the minimum number of predecessors.
939 unsigned MinNumPreds = pred_size(TestBB);
940 for (unsigned i = 1, e = BBTerm->getNumSuccessors(); i != e; ++i) {
941 TestBB = BBTerm->getSuccessor(i);
942 unsigned NumPreds = pred_size(TestBB);
943 if (NumPreds < MinNumPreds) {
944 MinSucc = i;
945 MinNumPreds = NumPreds;
946 }
947 }
948
949 return MinSucc;
950}
951
953 if (!BB->hasAddressTaken()) return false;
954
955 // If the block has its address taken, it may be a tree of dead constants
956 // hanging off of it. These shouldn't keep the block alive.
959 return !BA->use_empty();
960}
961
962/// processBlock - If there are any predecessors whose control can be threaded
963/// through to a successor, transform them now.
965 // If the block is trivially dead, just return and let the caller nuke it.
966 // This simplifies other transformations.
967 if (DTU->isBBPendingDeletion(BB) ||
968 (pred_empty(BB) && BB != &BB->getParent()->getEntryBlock()))
969 return false;
970
971 // If this block has a single predecessor, and if that pred has a single
972 // successor, merge the blocks. This encourages recursive jump threading
973 // because now the condition in this block can be threaded through
974 // predecessors of our predecessor block.
976 return true;
977
979 return true;
980
981 // Look if we can propagate guards to predecessors.
982 if (HasGuards && processGuards(BB))
983 return true;
984
985 // What kind of constant we're looking for.
986 ConstantPreference Preference = WantInteger;
987
988 // Look to see if the terminator is a conditional branch, switch or indirect
989 // branch, if not we can't thread it.
990 Value *Condition;
991 Instruction *Terminator = BB->getTerminator();
992 if (BranchInst *BI = dyn_cast<BranchInst>(Terminator)) {
993 // Can't thread an unconditional jump.
994 if (BI->isUnconditional()) return false;
995 Condition = BI->getCondition();
996 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(Terminator)) {
997 Condition = SI->getCondition();
998 } else if (IndirectBrInst *IB = dyn_cast<IndirectBrInst>(Terminator)) {
999 // Can't thread indirect branch with no successors.
1000 if (IB->getNumSuccessors() == 0) return false;
1001 Condition = IB->getAddress()->stripPointerCasts();
1002 Preference = WantBlockAddress;
1003 } else {
1004 return false; // Must be an invoke or callbr.
1005 }
1006
1007 // Keep track if we constant folded the condition in this invocation.
1008 bool ConstantFolded = false;
1009
1010 // Run constant folding to see if we can reduce the condition to a simple
1011 // constant.
1012 if (Instruction *I = dyn_cast<Instruction>(Condition)) {
1013 Value *SimpleVal =
1015 if (SimpleVal) {
1016 I->replaceAllUsesWith(SimpleVal);
1017 if (isInstructionTriviallyDead(I, TLI))
1018 I->eraseFromParent();
1019 Condition = SimpleVal;
1020 ConstantFolded = true;
1021 }
1022 }
1023
1024 // If the terminator is branching on an undef or freeze undef, we can pick any
1025 // of the successors to branch to. Let getBestDestForJumpOnUndef decide.
1026 auto *FI = dyn_cast<FreezeInst>(Condition);
1027 if (isa<UndefValue>(Condition) ||
1028 (FI && isa<UndefValue>(FI->getOperand(0)) && FI->hasOneUse())) {
1029 unsigned BestSucc = getBestDestForJumpOnUndef(BB);
1030 std::vector<DominatorTree::UpdateType> Updates;
1031
1032 // Fold the branch/switch.
1033 Instruction *BBTerm = BB->getTerminator();
1034 Updates.reserve(BBTerm->getNumSuccessors());
1035 for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i) {
1036 if (i == BestSucc) continue;
1037 BasicBlock *Succ = BBTerm->getSuccessor(i);
1038 Succ->removePredecessor(BB, true);
1039 Updates.push_back({DominatorTree::Delete, BB, Succ});
1040 }
1041
1042 LLVM_DEBUG(dbgs() << " In block '" << BB->getName()
1043 << "' folding undef terminator: " << *BBTerm << '\n');
1044 BranchInst::Create(BBTerm->getSuccessor(BestSucc), BBTerm);
1045 ++NumFolds;
1046 BBTerm->eraseFromParent();
1047 DTU->applyUpdatesPermissive(Updates);
1048 if (FI)
1049 FI->eraseFromParent();
1050 return true;
1051 }
1052
1053 // If the terminator of this block is branching on a constant, simplify the
1054 // terminator to an unconditional branch. This can occur due to threading in
1055 // other blocks.
1056 if (getKnownConstant(Condition, Preference)) {
1057 LLVM_DEBUG(dbgs() << " In block '" << BB->getName()
1058 << "' folding terminator: " << *BB->getTerminator()
1059 << '\n');
1060 ++NumFolds;
1061 ConstantFoldTerminator(BB, true, nullptr, DTU.get());
1062 if (auto *BPI = getBPI())
1063 BPI->eraseBlock(BB);
1064 return true;
1065 }
1066
1067 Instruction *CondInst = dyn_cast<Instruction>(Condition);
1068
1069 // All the rest of our checks depend on the condition being an instruction.
1070 if (!CondInst) {
1071 // FIXME: Unify this with code below.
1072 if (processThreadableEdges(Condition, BB, Preference, Terminator))
1073 return true;
1074 return ConstantFolded;
1075 }
1076
1077 // Some of the following optimization can safely work on the unfrozen cond.
1078 Value *CondWithoutFreeze = CondInst;
1079 if (auto *FI = dyn_cast<FreezeInst>(CondInst))
1080 CondWithoutFreeze = FI->getOperand(0);
1081
1082 if (CmpInst *CondCmp = dyn_cast<CmpInst>(CondWithoutFreeze)) {
1083 // If we're branching on a conditional, LVI might be able to determine
1084 // it's value at the branch instruction. We only handle comparisons
1085 // against a constant at this time.
1086 if (Constant *CondConst = dyn_cast<Constant>(CondCmp->getOperand(1))) {
1088 LVI->getPredicateAt(CondCmp->getPredicate(), CondCmp->getOperand(0),
1089 CondConst, BB->getTerminator(),
1090 /*UseBlockValue=*/false);
1091 if (Ret != LazyValueInfo::Unknown) {
1092 // We can safely replace *some* uses of the CondInst if it has
1093 // exactly one value as returned by LVI. RAUW is incorrect in the
1094 // presence of guards and assumes, that have the `Cond` as the use. This
1095 // is because we use the guards/assume to reason about the `Cond` value
1096 // at the end of block, but RAUW unconditionally replaces all uses
1097 // including the guards/assumes themselves and the uses before the
1098 // guard/assume.
1099 auto *CI = Ret == LazyValueInfo::True ?
1100 ConstantInt::getTrue(CondCmp->getType()) :
1101 ConstantInt::getFalse(CondCmp->getType());
1102 if (replaceFoldableUses(CondCmp, CI, BB))
1103 return true;
1104 }
1105
1106 // We did not manage to simplify this branch, try to see whether
1107 // CondCmp depends on a known phi-select pattern.
1108 if (tryToUnfoldSelect(CondCmp, BB))
1109 return true;
1110 }
1111 }
1112
1113 if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator()))
1114 if (tryToUnfoldSelect(SI, BB))
1115 return true;
1116
1117 // Check for some cases that are worth simplifying. Right now we want to look
1118 // for loads that are used by a switch or by the condition for the branch. If
1119 // we see one, check to see if it's partially redundant. If so, insert a PHI
1120 // which can then be used to thread the values.
1121 Value *SimplifyValue = CondWithoutFreeze;
1122
1123 if (CmpInst *CondCmp = dyn_cast<CmpInst>(SimplifyValue))
1124 if (isa<Constant>(CondCmp->getOperand(1)))
1125 SimplifyValue = CondCmp->getOperand(0);
1126
1127 // TODO: There are other places where load PRE would be profitable, such as
1128 // more complex comparisons.
1129 if (LoadInst *LoadI = dyn_cast<LoadInst>(SimplifyValue))
1131 return true;
1132
1133 // Before threading, try to propagate profile data backwards:
1134 if (PHINode *PN = dyn_cast<PHINode>(CondInst))
1135 if (PN->getParent() == BB && isa<BranchInst>(BB->getTerminator()))
1137
1138 // Handle a variety of cases where we are branching on something derived from
1139 // a PHI node in the current block. If we can prove that any predecessors
1140 // compute a predictable value based on a PHI node, thread those predecessors.
1141 if (processThreadableEdges(CondInst, BB, Preference, Terminator))
1142 return true;
1143
1144 // If this is an otherwise-unfoldable branch on a phi node or freeze(phi) in
1145 // the current block, see if we can simplify.
1146 PHINode *PN = dyn_cast<PHINode>(CondWithoutFreeze);
1147 if (PN && PN->getParent() == BB && isa<BranchInst>(BB->getTerminator()))
1148 return processBranchOnPHI(PN);
1149
1150 // If this is an otherwise-unfoldable branch on a XOR, see if we can simplify.
1151 if (CondInst->getOpcode() == Instruction::Xor &&
1152 CondInst->getParent() == BB && isa<BranchInst>(BB->getTerminator()))
1153 return processBranchOnXOR(cast<BinaryOperator>(CondInst));
1154
1155 // Search for a stronger dominating condition that can be used to simplify a
1156 // conditional branch leaving BB.
1158 return true;
1159
1160 return false;
1161}
1162
1164 auto *BI = dyn_cast<BranchInst>(BB->getTerminator());
1165 if (!BI || !BI->isConditional())
1166 return false;
1167
1168 Value *Cond = BI->getCondition();
1169 // Assuming that predecessor's branch was taken, if pred's branch condition
1170 // (V) implies Cond, Cond can be either true, undef, or poison. In this case,
1171 // freeze(Cond) is either true or a nondeterministic value.
1172 // If freeze(Cond) has only one use, we can freely fold freeze(Cond) to true
1173 // without affecting other instructions.
1174 auto *FICond = dyn_cast<FreezeInst>(Cond);
1175 if (FICond && FICond->hasOneUse())
1176 Cond = FICond->getOperand(0);
1177 else
1178 FICond = nullptr;
1179
1180 BasicBlock *CurrentBB = BB;
1181 BasicBlock *CurrentPred = BB->getSinglePredecessor();
1182 unsigned Iter = 0;
1183
1184 auto &DL = BB->getModule()->getDataLayout();
1185
1186 while (CurrentPred && Iter++ < ImplicationSearchThreshold) {
1187 auto *PBI = dyn_cast<BranchInst>(CurrentPred->getTerminator());
1188 if (!PBI || !PBI->isConditional())
1189 return false;
1190 if (PBI->getSuccessor(0) != CurrentBB && PBI->getSuccessor(1) != CurrentBB)
1191 return false;
1192
1193 bool CondIsTrue = PBI->getSuccessor(0) == CurrentBB;
1194 std::optional<bool> Implication =
1195 isImpliedCondition(PBI->getCondition(), Cond, DL, CondIsTrue);
1196
1197 // If the branch condition of BB (which is Cond) and CurrentPred are
1198 // exactly the same freeze instruction, Cond can be folded into CondIsTrue.
1199 if (!Implication && FICond && isa<FreezeInst>(PBI->getCondition())) {
1200 if (cast<FreezeInst>(PBI->getCondition())->getOperand(0) ==
1201 FICond->getOperand(0))
1202 Implication = CondIsTrue;
1203 }
1204
1205 if (Implication) {
1206 BasicBlock *KeepSucc = BI->getSuccessor(*Implication ? 0 : 1);
1207 BasicBlock *RemoveSucc = BI->getSuccessor(*Implication ? 1 : 0);
1208 RemoveSucc->removePredecessor(BB);
1209 BranchInst *UncondBI = BranchInst::Create(KeepSucc, BI);
1210 UncondBI->setDebugLoc(BI->getDebugLoc());
1211 ++NumFolds;
1212 BI->eraseFromParent();
1213 if (FICond)
1214 FICond->eraseFromParent();
1215
1216 DTU->applyUpdatesPermissive({{DominatorTree::Delete, BB, RemoveSucc}});
1217 if (auto *BPI = getBPI())
1218 BPI->eraseBlock(BB);
1219 return true;
1220 }
1221 CurrentBB = CurrentPred;
1222 CurrentPred = CurrentBB->getSinglePredecessor();
1223 }
1224
1225 return false;
1226}
1227
1228/// Return true if Op is an instruction defined in the given block.
1229static bool isOpDefinedInBlock(Value *Op, BasicBlock *BB) {
1230 if (Instruction *OpInst = dyn_cast<Instruction>(Op))
1231 if (OpInst->getParent() == BB)
1232 return true;
1233 return false;
1234}
1235
1236/// simplifyPartiallyRedundantLoad - If LoadI is an obviously partially
1237/// redundant load instruction, eliminate it by replacing it with a PHI node.
1238/// This is an important optimization that encourages jump threading, and needs
1239/// to be run interlaced with other jump threading tasks.
1241 // Don't hack volatile and ordered loads.
1242 if (!LoadI->isUnordered()) return false;
1243
1244 // If the load is defined in a block with exactly one predecessor, it can't be
1245 // partially redundant.
1246 BasicBlock *LoadBB = LoadI->getParent();
1247 if (LoadBB->getSinglePredecessor())
1248 return false;
1249
1250 // If the load is defined in an EH pad, it can't be partially redundant,
1251 // because the edges between the invoke and the EH pad cannot have other
1252 // instructions between them.
1253 if (LoadBB->isEHPad())
1254 return false;
1255
1256 Value *LoadedPtr = LoadI->getOperand(0);
1257
1258 // If the loaded operand is defined in the LoadBB and its not a phi,
1259 // it can't be available in predecessors.
1260 if (isOpDefinedInBlock(LoadedPtr, LoadBB) && !isa<PHINode>(LoadedPtr))
1261 return false;
1262
1263 // Scan a few instructions up from the load, to see if it is obviously live at
1264 // the entry to its block.
1265 BasicBlock::iterator BBIt(LoadI);
1266 bool IsLoadCSE;
1267 if (Value *AvailableVal = FindAvailableLoadedValue(
1268 LoadI, LoadBB, BBIt, DefMaxInstsToScan, AA, &IsLoadCSE)) {
1269 // If the value of the load is locally available within the block, just use
1270 // it. This frequently occurs for reg2mem'd allocas.
1271
1272 if (IsLoadCSE) {
1273 LoadInst *NLoadI = cast<LoadInst>(AvailableVal);
1274 combineMetadataForCSE(NLoadI, LoadI, false);
1275 };
1276
1277 // If the returned value is the load itself, replace with poison. This can
1278 // only happen in dead loops.
1279 if (AvailableVal == LoadI)
1280 AvailableVal = PoisonValue::get(LoadI->getType());
1281 if (AvailableVal->getType() != LoadI->getType())
1282 AvailableVal = CastInst::CreateBitOrPointerCast(
1283 AvailableVal, LoadI->getType(), "", LoadI);
1284 LoadI->replaceAllUsesWith(AvailableVal);
1285 LoadI->eraseFromParent();
1286 return true;
1287 }
1288
1289 // Otherwise, if we scanned the whole block and got to the top of the block,
1290 // we know the block is locally transparent to the load. If not, something
1291 // might clobber its value.
1292 if (BBIt != LoadBB->begin())
1293 return false;
1294
1295 // If all of the loads and stores that feed the value have the same AA tags,
1296 // then we can propagate them onto any newly inserted loads.
1297 AAMDNodes AATags = LoadI->getAAMetadata();
1298
1299 SmallPtrSet<BasicBlock*, 8> PredsScanned;
1300
1301 using AvailablePredsTy = SmallVector<std::pair<BasicBlock *, Value *>, 8>;
1302
1303 AvailablePredsTy AvailablePreds;
1304 BasicBlock *OneUnavailablePred = nullptr;
1306
1307 // If we got here, the loaded value is transparent through to the start of the
1308 // block. Check to see if it is available in any of the predecessor blocks.
1309 for (BasicBlock *PredBB : predecessors(LoadBB)) {
1310 // If we already scanned this predecessor, skip it.
1311 if (!PredsScanned.insert(PredBB).second)
1312 continue;
1313
1314 BBIt = PredBB->end();
1315 unsigned NumScanedInst = 0;
1316 Value *PredAvailable = nullptr;
1317 // NOTE: We don't CSE load that is volatile or anything stronger than
1318 // unordered, that should have been checked when we entered the function.
1319 assert(LoadI->isUnordered() &&
1320 "Attempting to CSE volatile or atomic loads");
1321 // If this is a load on a phi pointer, phi-translate it and search
1322 // for available load/store to the pointer in predecessors.
1323 Type *AccessTy = LoadI->getType();
1324 const auto &DL = LoadI->getModule()->getDataLayout();
1325 MemoryLocation Loc(LoadedPtr->DoPHITranslation(LoadBB, PredBB),
1326 LocationSize::precise(DL.getTypeStoreSize(AccessTy)),
1327 AATags);
1328 PredAvailable = findAvailablePtrLoadStore(Loc, AccessTy, LoadI->isAtomic(),
1329 PredBB, BBIt, DefMaxInstsToScan,
1330 AA, &IsLoadCSE, &NumScanedInst);
1331
1332 // If PredBB has a single predecessor, continue scanning through the
1333 // single predecessor.
1334 BasicBlock *SinglePredBB = PredBB;
1335 while (!PredAvailable && SinglePredBB && BBIt == SinglePredBB->begin() &&
1336 NumScanedInst < DefMaxInstsToScan) {
1337 SinglePredBB = SinglePredBB->getSinglePredecessor();
1338 if (SinglePredBB) {
1339 BBIt = SinglePredBB->end();
1340 PredAvailable = findAvailablePtrLoadStore(
1341 Loc, AccessTy, LoadI->isAtomic(), SinglePredBB, BBIt,
1342 (DefMaxInstsToScan - NumScanedInst), AA, &IsLoadCSE,
1343 &NumScanedInst);
1344 }
1345 }
1346
1347 if (!PredAvailable) {
1348 OneUnavailablePred = PredBB;
1349 continue;
1350 }
1351
1352 if (IsLoadCSE)
1353 CSELoads.push_back(cast<LoadInst>(PredAvailable));
1354
1355 // If so, this load is partially redundant. Remember this info so that we
1356 // can create a PHI node.
1357 AvailablePreds.emplace_back(PredBB, PredAvailable);
1358 }
1359
1360 // If the loaded value isn't available in any predecessor, it isn't partially
1361 // redundant.
1362 if (AvailablePreds.empty()) return false;
1363
1364 // Okay, the loaded value is available in at least one (and maybe all!)
1365 // predecessors. If the value is unavailable in more than one unique
1366 // predecessor, we want to insert a merge block for those common predecessors.
1367 // This ensures that we only have to insert one reload, thus not increasing
1368 // code size.
1369 BasicBlock *UnavailablePred = nullptr;
1370
1371 // If the value is unavailable in one of predecessors, we will end up
1372 // inserting a new instruction into them. It is only valid if all the
1373 // instructions before LoadI are guaranteed to pass execution to its
1374 // successor, or if LoadI is safe to speculate.
1375 // TODO: If this logic becomes more complex, and we will perform PRE insertion
1376 // farther than to a predecessor, we need to reuse the code from GVN's PRE.
1377 // It requires domination tree analysis, so for this simple case it is an
1378 // overkill.
1379 if (PredsScanned.size() != AvailablePreds.size() &&
1381 for (auto I = LoadBB->begin(); &*I != LoadI; ++I)
1383 return false;
1384
1385 // If there is exactly one predecessor where the value is unavailable, the
1386 // already computed 'OneUnavailablePred' block is it. If it ends in an
1387 // unconditional branch, we know that it isn't a critical edge.
1388 if (PredsScanned.size() == AvailablePreds.size()+1 &&
1389 OneUnavailablePred->getTerminator()->getNumSuccessors() == 1) {
1390 UnavailablePred = OneUnavailablePred;
1391 } else if (PredsScanned.size() != AvailablePreds.size()) {
1392 // Otherwise, we had multiple unavailable predecessors or we had a critical
1393 // edge from the one.
1394 SmallVector<BasicBlock*, 8> PredsToSplit;
1395 SmallPtrSet<BasicBlock*, 8> AvailablePredSet;
1396
1397 for (const auto &AvailablePred : AvailablePreds)
1398 AvailablePredSet.insert(AvailablePred.first);
1399
1400 // Add all the unavailable predecessors to the PredsToSplit list.
1401 for (BasicBlock *P : predecessors(LoadBB)) {
1402 // If the predecessor is an indirect goto, we can't split the edge.
1403 if (isa<IndirectBrInst>(P->getTerminator()))
1404 return false;
1405
1406 if (!AvailablePredSet.count(P))
1407 PredsToSplit.push_back(P);
1408 }
1409
1410 // Split them out to their own block.
1411 UnavailablePred = splitBlockPreds(LoadBB, PredsToSplit, "thread-pre-split");
1412 }
1413
1414 // If the value isn't available in all predecessors, then there will be
1415 // exactly one where it isn't available. Insert a load on that edge and add
1416 // it to the AvailablePreds list.
1417 if (UnavailablePred) {
1418 assert(UnavailablePred->getTerminator()->getNumSuccessors() == 1 &&
1419 "Can't handle critical edge here!");
1420 LoadInst *NewVal = new LoadInst(
1421 LoadI->getType(), LoadedPtr->DoPHITranslation(LoadBB, UnavailablePred),
1422 LoadI->getName() + ".pr", false, LoadI->getAlign(),
1423 LoadI->getOrdering(), LoadI->getSyncScopeID(),
1424 UnavailablePred->getTerminator());
1425 NewVal->setDebugLoc(LoadI->getDebugLoc());
1426 if (AATags)
1427 NewVal->setAAMetadata(AATags);
1428
1429 AvailablePreds.emplace_back(UnavailablePred, NewVal);
1430 }
1431
1432 // Now we know that each predecessor of this block has a value in
1433 // AvailablePreds, sort them for efficient access as we're walking the preds.
1434 array_pod_sort(AvailablePreds.begin(), AvailablePreds.end());
1435
1436 // Create a PHI node at the start of the block for the PRE'd load value.
1437 pred_iterator PB = pred_begin(LoadBB), PE = pred_end(LoadBB);
1438 PHINode *PN = PHINode::Create(LoadI->getType(), std::distance(PB, PE), "",
1439 &LoadBB->front());
1440 PN->takeName(LoadI);
1441 PN->setDebugLoc(LoadI->getDebugLoc());
1442
1443 // Insert new entries into the PHI for each predecessor. A single block may
1444 // have multiple entries here.
1445 for (pred_iterator PI = PB; PI != PE; ++PI) {
1446 BasicBlock *P = *PI;
1447 AvailablePredsTy::iterator I =
1448 llvm::lower_bound(AvailablePreds, std::make_pair(P, (Value *)nullptr));
1449
1450 assert(I != AvailablePreds.end() && I->first == P &&
1451 "Didn't find entry for predecessor!");
1452
1453 // If we have an available predecessor but it requires casting, insert the
1454 // cast in the predecessor and use the cast. Note that we have to update the
1455 // AvailablePreds vector as we go so that all of the PHI entries for this
1456 // predecessor use the same bitcast.
1457 Value *&PredV = I->second;
1458 if (PredV->getType() != LoadI->getType())
1459 PredV = CastInst::CreateBitOrPointerCast(PredV, LoadI->getType(), "",
1460 P->getTerminator());
1461
1462 PN->addIncoming(PredV, I->first);
1463 }
1464
1465 for (LoadInst *PredLoadI : CSELoads) {
1466 combineMetadataForCSE(PredLoadI, LoadI, true);
1467 }
1468
1469 LoadI->replaceAllUsesWith(PN);
1470 LoadI->eraseFromParent();
1471
1472 return true;
1473}
1474
1475/// findMostPopularDest - The specified list contains multiple possible
1476/// threadable destinations. Pick the one that occurs the most frequently in
1477/// the list.
1478static BasicBlock *
1480 const SmallVectorImpl<std::pair<BasicBlock *,
1481 BasicBlock *>> &PredToDestList) {
1482 assert(!PredToDestList.empty());
1483
1484 // Determine popularity. If there are multiple possible destinations, we
1485 // explicitly choose to ignore 'undef' destinations. We prefer to thread
1486 // blocks with known and real destinations to threading undef. We'll handle
1487 // them later if interesting.
1488 MapVector<BasicBlock *, unsigned> DestPopularity;
1489
1490 // Populate DestPopularity with the successors in the order they appear in the
1491 // successor list. This way, we ensure determinism by iterating it in the
1492 // same order in std::max_element below. We map nullptr to 0 so that we can
1493 // return nullptr when PredToDestList contains nullptr only.
1494 DestPopularity[nullptr] = 0;
1495 for (auto *SuccBB : successors(BB))
1496 DestPopularity[SuccBB] = 0;
1497
1498 for (const auto &PredToDest : PredToDestList)
1499 if (PredToDest.second)
1500 DestPopularity[PredToDest.second]++;
1501
1502 // Find the most popular dest.
1503 auto MostPopular = std::max_element(
1504 DestPopularity.begin(), DestPopularity.end(), llvm::less_second());
1505
1506 // Okay, we have finally picked the most popular destination.
1507 return MostPopular->first;
1508}
1509
1510// Try to evaluate the value of V when the control flows from PredPredBB to
1511// BB->getSinglePredecessor() and then on to BB.
1513 BasicBlock *PredPredBB,
1514 Value *V) {
1515 BasicBlock *PredBB = BB->getSinglePredecessor();
1516 assert(PredBB && "Expected a single predecessor");
1517
1518 if (Constant *Cst = dyn_cast<Constant>(V)) {
1519 return Cst;
1520 }
1521
1522 // Consult LVI if V is not an instruction in BB or PredBB.
1523 Instruction *I = dyn_cast<Instruction>(V);
1524 if (!I || (I->getParent() != BB && I->getParent() != PredBB)) {
1525 return LVI->getConstantOnEdge(V, PredPredBB, PredBB, nullptr);
1526 }
1527
1528 // Look into a PHI argument.
1529 if (PHINode *PHI = dyn_cast<PHINode>(V)) {
1530 if (PHI->getParent() == PredBB)
1531 return dyn_cast<Constant>(PHI->getIncomingValueForBlock(PredPredBB));
1532 return nullptr;
1533 }
1534
1535 // If we have a CmpInst, try to fold it for each incoming edge into PredBB.
1536 if (CmpInst *CondCmp = dyn_cast<CmpInst>(V)) {
1537 if (CondCmp->getParent() == BB) {
1538 Constant *Op0 =
1539 evaluateOnPredecessorEdge(BB, PredPredBB, CondCmp->getOperand(0));
1540 Constant *Op1 =
1541 evaluateOnPredecessorEdge(BB, PredPredBB, CondCmp->getOperand(1));
1542 if (Op0 && Op1) {
1543 return ConstantExpr::getCompare(CondCmp->getPredicate(), Op0, Op1);
1544 }
1545 }
1546 return nullptr;
1547 }
1548
1549 return nullptr;
1550}
1551
1553 ConstantPreference Preference,
1554 Instruction *CxtI) {
1555 // If threading this would thread across a loop header, don't even try to
1556 // thread the edge.
1557 if (LoopHeaders.count(BB))
1558 return false;
1559
1560 PredValueInfoTy PredValues;
1561 if (!computeValueKnownInPredecessors(Cond, BB, PredValues, Preference,
1562 CxtI)) {
1563 // We don't have known values in predecessors. See if we can thread through
1564 // BB and its sole predecessor.
1566 }
1567
1568 assert(!PredValues.empty() &&
1569 "computeValueKnownInPredecessors returned true with no values");
1570
1571 LLVM_DEBUG(dbgs() << "IN BB: " << *BB;
1572 for (const auto &PredValue : PredValues) {
1573 dbgs() << " BB '" << BB->getName()
1574 << "': FOUND condition = " << *PredValue.first
1575 << " for pred '" << PredValue.second->getName() << "'.\n";
1576 });
1577
1578 // Decide what we want to thread through. Convert our list of known values to
1579 // a list of known destinations for each pred. This also discards duplicate
1580 // predecessors and keeps track of the undefined inputs (which are represented
1581 // as a null dest in the PredToDestList).
1584
1585 BasicBlock *OnlyDest = nullptr;
1586 BasicBlock *MultipleDestSentinel = (BasicBlock*)(intptr_t)~0ULL;
1587 Constant *OnlyVal = nullptr;
1588 Constant *MultipleVal = (Constant *)(intptr_t)~0ULL;
1589
1590 for (const auto &PredValue : PredValues) {
1591 BasicBlock *Pred = PredValue.second;
1592 if (!SeenPreds.insert(Pred).second)
1593 continue; // Duplicate predecessor entry.
1594
1595 Constant *Val = PredValue.first;
1596
1597 BasicBlock *DestBB;
1598 if (isa<UndefValue>(Val))
1599 DestBB = nullptr;
1600 else if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
1601 assert(isa<ConstantInt>(Val) && "Expecting a constant integer");
1602 DestBB = BI->getSuccessor(cast<ConstantInt>(Val)->isZero());
1603 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
1604 assert(isa<ConstantInt>(Val) && "Expecting a constant integer");
1605 DestBB = SI->findCaseValue(cast<ConstantInt>(Val))->getCaseSuccessor();
1606 } else {
1607 assert(isa<IndirectBrInst>(BB->getTerminator())
1608 && "Unexpected terminator");
1609 assert(isa<BlockAddress>(Val) && "Expecting a constant blockaddress");
1610 DestBB = cast<BlockAddress>(Val)->getBasicBlock();
1611 }
1612
1613 // If we have exactly one destination, remember it for efficiency below.
1614 if (PredToDestList.empty()) {
1615 OnlyDest = DestBB;
1616 OnlyVal = Val;
1617 } else {
1618 if (OnlyDest != DestBB)
1619 OnlyDest = MultipleDestSentinel;
1620 // It possible we have same destination, but different value, e.g. default
1621 // case in switchinst.
1622 if (Val != OnlyVal)
1623 OnlyVal = MultipleVal;
1624 }
1625
1626 // If the predecessor ends with an indirect goto, we can't change its
1627 // destination.
1628 if (isa<IndirectBrInst>(Pred->getTerminator()))
1629 continue;
1630
1631 PredToDestList.emplace_back(Pred, DestBB);
1632 }
1633
1634 // If all edges were unthreadable, we fail.
1635 if (PredToDestList.empty())
1636 return false;
1637
1638 // If all the predecessors go to a single known successor, we want to fold,
1639 // not thread. By doing so, we do not need to duplicate the current block and
1640 // also miss potential opportunities in case we dont/cant duplicate.
1641 if (OnlyDest && OnlyDest != MultipleDestSentinel) {
1642 if (BB->hasNPredecessors(PredToDestList.size())) {
1643 bool SeenFirstBranchToOnlyDest = false;
1644 std::vector <DominatorTree::UpdateType> Updates;
1645 Updates.reserve(BB->getTerminator()->getNumSuccessors() - 1);
1646 for (BasicBlock *SuccBB : successors(BB)) {
1647 if (SuccBB == OnlyDest && !SeenFirstBranchToOnlyDest) {
1648 SeenFirstBranchToOnlyDest = true; // Don't modify the first branch.
1649 } else {
1650 SuccBB->removePredecessor(BB, true); // This is unreachable successor.
1651 Updates.push_back({DominatorTree::Delete, BB, SuccBB});
1652 }
1653 }
1654
1655 // Finally update the terminator.
1656 Instruction *Term = BB->getTerminator();
1657 BranchInst::Create(OnlyDest, Term);
1658 ++NumFolds;
1659 Term->eraseFromParent();
1660 DTU->applyUpdatesPermissive(Updates);
1661 if (auto *BPI = getBPI())
1662 BPI->eraseBlock(BB);
1663
1664 // If the condition is now dead due to the removal of the old terminator,
1665 // erase it.
1666 if (auto *CondInst = dyn_cast<Instruction>(Cond)) {
1667 if (CondInst->use_empty() && !CondInst->mayHaveSideEffects())
1668 CondInst->eraseFromParent();
1669 // We can safely replace *some* uses of the CondInst if it has
1670 // exactly one value as returned by LVI. RAUW is incorrect in the
1671 // presence of guards and assumes, that have the `Cond` as the use. This
1672 // is because we use the guards/assume to reason about the `Cond` value
1673 // at the end of block, but RAUW unconditionally replaces all uses
1674 // including the guards/assumes themselves and the uses before the
1675 // guard/assume.
1676 else if (OnlyVal && OnlyVal != MultipleVal)
1677 replaceFoldableUses(CondInst, OnlyVal, BB);
1678 }
1679 return true;
1680 }
1681 }
1682
1683 // Determine which is the most common successor. If we have many inputs and
1684 // this block is a switch, we want to start by threading the batch that goes
1685 // to the most popular destination first. If we only know about one
1686 // threadable destination (the common case) we can avoid this.
1687 BasicBlock *MostPopularDest = OnlyDest;
1688
1689 if (MostPopularDest == MultipleDestSentinel) {
1690 // Remove any loop headers from the Dest list, threadEdge conservatively
1691 // won't process them, but we might have other destination that are eligible
1692 // and we still want to process.
1693 erase_if(PredToDestList,
1694 [&](const std::pair<BasicBlock *, BasicBlock *> &PredToDest) {
1695 return LoopHeaders.contains(PredToDest.second);
1696 });
1697
1698 if (PredToDestList.empty())
1699 return false;
1700
1701 MostPopularDest = findMostPopularDest(BB, PredToDestList);
1702 }
1703
1704 // Now that we know what the most popular destination is, factor all
1705 // predecessors that will jump to it into a single predecessor.
1706 SmallVector<BasicBlock*, 16> PredsToFactor;
1707 for (const auto &PredToDest : PredToDestList)
1708 if (PredToDest.second == MostPopularDest) {
1709 BasicBlock *Pred = PredToDest.first;
1710
1711 // This predecessor may be a switch or something else that has multiple
1712 // edges to the block. Factor each of these edges by listing them
1713 // according to # occurrences in PredsToFactor.
1714 for (BasicBlock *Succ : successors(Pred))
1715 if (Succ == BB)
1716 PredsToFactor.push_back(Pred);
1717 }
1718
1719 // If the threadable edges are branching on an undefined value, we get to pick
1720 // the destination that these predecessors should get to.
1721 if (!MostPopularDest)
1722 MostPopularDest = BB->getTerminator()->
1723 getSuccessor(getBestDestForJumpOnUndef(BB));
1724
1725 // Ok, try to thread it!
1726 return tryThreadEdge(BB, PredsToFactor, MostPopularDest);
1727}
1728
1729/// processBranchOnPHI - We have an otherwise unthreadable conditional branch on
1730/// a PHI node (or freeze PHI) in the current block. See if there are any
1731/// simplifications we can do based on inputs to the phi node.
1733 BasicBlock *BB = PN->getParent();
1734
1735 // TODO: We could make use of this to do it once for blocks with common PHI
1736 // values.
1738 PredBBs.resize(1);
1739
1740 // If any of the predecessor blocks end in an unconditional branch, we can
1741 // *duplicate* the conditional branch into that block in order to further
1742 // encourage jump threading and to eliminate cases where we have branch on a
1743 // phi of an icmp (branch on icmp is much better).
1744 // This is still beneficial when a frozen phi is used as the branch condition
1745 // because it allows CodeGenPrepare to further canonicalize br(freeze(icmp))
1746 // to br(icmp(freeze ...)).
1747 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1748 BasicBlock *PredBB = PN->getIncomingBlock(i);
1749 if (BranchInst *PredBr = dyn_cast<BranchInst>(PredBB->getTerminator()))
1750 if (PredBr->isUnconditional()) {
1751 PredBBs[0] = PredBB;
1752 // Try to duplicate BB into PredBB.
1753 if (duplicateCondBranchOnPHIIntoPred(BB, PredBBs))
1754 return true;
1755 }
1756 }
1757
1758 return false;
1759}
1760
1761/// processBranchOnXOR - We have an otherwise unthreadable conditional branch on
1762/// a xor instruction in the current block. See if there are any
1763/// simplifications we can do based on inputs to the xor.
1765 BasicBlock *BB = BO->getParent();
1766
1767 // If either the LHS or RHS of the xor is a constant, don't do this
1768 // optimization.
1769 if (isa<ConstantInt>(BO->getOperand(0)) ||
1770 isa<ConstantInt>(BO->getOperand(1)))
1771 return false;
1772
1773 // If the first instruction in BB isn't a phi, we won't be able to infer
1774 // anything special about any particular predecessor.
1775 if (!isa<PHINode>(BB->front()))
1776 return false;
1777
1778 // If this BB is a landing pad, we won't be able to split the edge into it.
1779 if (BB->isEHPad())
1780 return false;
1781
1782 // If we have a xor as the branch input to this block, and we know that the
1783 // LHS or RHS of the xor in any predecessor is true/false, then we can clone
1784 // the condition into the predecessor and fix that value to true, saving some
1785 // logical ops on that path and encouraging other paths to simplify.
1786 //
1787 // This copies something like this:
1788 //
1789 // BB:
1790 // %X = phi i1 [1], [%X']
1791 // %Y = icmp eq i32 %A, %B
1792 // %Z = xor i1 %X, %Y
1793 // br i1 %Z, ...
1794 //
1795 // Into:
1796 // BB':
1797 // %Y = icmp ne i32 %A, %B
1798 // br i1 %Y, ...
1799
1800 PredValueInfoTy XorOpValues;
1801 bool isLHS = true;
1802 if (!computeValueKnownInPredecessors(BO->getOperand(0), BB, XorOpValues,
1803 WantInteger, BO)) {
1804 assert(XorOpValues.empty());
1805 if (!computeValueKnownInPredecessors(BO->getOperand(1), BB, XorOpValues,
1806 WantInteger, BO))
1807 return false;
1808 isLHS = false;
1809 }
1810
1811 assert(!XorOpValues.empty() &&
1812 "computeValueKnownInPredecessors returned true with no values");
1813
1814 // Scan the information to see which is most popular: true or false. The
1815 // predecessors can be of the set true, false, or undef.
1816 unsigned NumTrue = 0, NumFalse = 0;
1817 for (const auto &XorOpValue : XorOpValues) {
1818 if (isa<UndefValue>(XorOpValue.first))
1819 // Ignore undefs for the count.
1820 continue;
1821 if (cast<ConstantInt>(XorOpValue.first)->isZero())
1822 ++NumFalse;
1823 else
1824 ++NumTrue;
1825 }
1826
1827 // Determine which value to split on, true, false, or undef if neither.
1828 ConstantInt *SplitVal = nullptr;
1829 if (NumTrue > NumFalse)
1830 SplitVal = ConstantInt::getTrue(BB->getContext());
1831 else if (NumTrue != 0 || NumFalse != 0)
1832 SplitVal = ConstantInt::getFalse(BB->getContext());
1833
1834 // Collect all of the blocks that this can be folded into so that we can
1835 // factor this once and clone it once.
1836 SmallVector<BasicBlock*, 8> BlocksToFoldInto;
1837 for (const auto &XorOpValue : XorOpValues) {
1838 if (XorOpValue.first != SplitVal && !isa<UndefValue>(XorOpValue.first))
1839 continue;
1840
1841 BlocksToFoldInto.push_back(XorOpValue.second);
1842 }
1843
1844 // If we inferred a value for all of the predecessors, then duplication won't
1845 // help us. However, we can just replace the LHS or RHS with the constant.
1846 if (BlocksToFoldInto.size() ==
1847 cast<PHINode>(BB->front()).getNumIncomingValues()) {
1848 if (!SplitVal) {
1849 // If all preds provide undef, just nuke the xor, because it is undef too.
1851 BO->eraseFromParent();
1852 } else if (SplitVal->isZero() && BO != BO->getOperand(isLHS)) {
1853 // If all preds provide 0, replace the xor with the other input.
1854 BO->replaceAllUsesWith(BO->getOperand(isLHS));
1855 BO->eraseFromParent();
1856 } else {
1857 // If all preds provide 1, set the computed value to 1.
1858 BO->setOperand(!isLHS, SplitVal);
1859 }
1860
1861 return true;
1862 }
1863
1864 // If any of predecessors end with an indirect goto, we can't change its
1865 // destination.
1866 if (any_of(BlocksToFoldInto, [](BasicBlock *Pred) {
1867 return isa<IndirectBrInst>(Pred->getTerminator());
1868 }))
1869 return false;
1870
1871 // Try to duplicate BB into PredBB.
1872 return duplicateCondBranchOnPHIIntoPred(BB, BlocksToFoldInto);
1873}
1874
1875/// addPHINodeEntriesForMappedBlock - We're adding 'NewPred' as a new
1876/// predecessor to the PHIBB block. If it has PHI nodes, add entries for
1877/// NewPred using the entries from OldPred (suitably mapped).
1879 BasicBlock *OldPred,
1880 BasicBlock *NewPred,
1882 for (PHINode &PN : PHIBB->phis()) {
1883 // Ok, we have a PHI node. Figure out what the incoming value was for the
1884 // DestBlock.
1885 Value *IV = PN.getIncomingValueForBlock(OldPred);
1886
1887 // Remap the value if necessary.
1888 if (Instruction *Inst = dyn_cast<Instruction>(IV)) {
1890 if (I != ValueMap.end())
1891 IV = I->second;
1892 }
1893
1894 PN.addIncoming(IV, NewPred);
1895 }
1896}
1897
1898/// Merge basic block BB into its sole predecessor if possible.
1900 BasicBlock *SinglePred = BB->getSinglePredecessor();
1901 if (!SinglePred)
1902 return false;
1903
1904 const Instruction *TI = SinglePred->getTerminator();
1905 if (TI->isExceptionalTerminator() || TI->getNumSuccessors() != 1 ||
1906 SinglePred == BB || hasAddressTakenAndUsed(BB))
1907 return false;
1908
1909 // If SinglePred was a loop header, BB becomes one.
1910 if (LoopHeaders.erase(SinglePred))
1911 LoopHeaders.insert(BB);
1912
1913 LVI->eraseBlock(SinglePred);
1914 MergeBasicBlockIntoOnlyPred(BB, DTU.get());
1915
1916 // Now that BB is merged into SinglePred (i.e. SinglePred code followed by
1917 // BB code within one basic block `BB`), we need to invalidate the LVI
1918 // information associated with BB, because the LVI information need not be
1919 // true for all of BB after the merge. For example,
1920 // Before the merge, LVI info and code is as follows:
1921 // SinglePred: <LVI info1 for %p val>
1922 // %y = use of %p
1923 // call @exit() // need not transfer execution to successor.
1924 // assume(%p) // from this point on %p is true
1925 // br label %BB
1926 // BB: <LVI info2 for %p val, i.e. %p is true>
1927 // %x = use of %p
1928 // br label exit
1929 //
1930 // Note that this LVI info for blocks BB and SinglPred is correct for %p
1931 // (info2 and info1 respectively). After the merge and the deletion of the
1932 // LVI info1 for SinglePred. We have the following code:
1933 // BB: <LVI info2 for %p val>
1934 // %y = use of %p
1935 // call @exit()
1936 // assume(%p)
1937 // %x = use of %p <-- LVI info2 is correct from here onwards.
1938 // br label exit
1939 // LVI info2 for BB is incorrect at the beginning of BB.
1940
1941 // Invalidate LVI information for BB if the LVI is not provably true for
1942 // all of BB.
1944 LVI->eraseBlock(BB);
1945 return true;
1946}
1947
1948/// Update the SSA form. NewBB contains instructions that are copied from BB.
1949/// ValueMapping maps old values in BB to new ones in NewBB.
1951 BasicBlock *BB, BasicBlock *NewBB,
1952 DenseMap<Instruction *, Value *> &ValueMapping) {
1953 // If there were values defined in BB that are used outside the block, then we
1954 // now have to update all uses of the value to use either the original value,
1955 // the cloned value, or some PHI derived value. This can require arbitrary
1956 // PHI insertion, of which we are prepared to do, clean these up now.
1957 SSAUpdater SSAUpdate;
1958 SmallVector<Use *, 16> UsesToRename;
1960
1961 for (Instruction &I : *BB) {
1962 // Scan all uses of this instruction to see if it is used outside of its
1963 // block, and if so, record them in UsesToRename.
1964 for (Use &U : I.uses()) {
1965 Instruction *User = cast<Instruction>(U.getUser());
1966 if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
1967 if (UserPN->getIncomingBlock(U) == BB)
1968 continue;
1969 } else if (User->getParent() == BB)
1970 continue;
1971
1972 UsesToRename.push_back(&U);
1973 }
1974
1975 // Find debug values outside of the block
1976 findDbgValues(DbgValues, &I);
1977 DbgValues.erase(remove_if(DbgValues,
1978 [&](const DbgValueInst *DbgVal) {
1979 return DbgVal->getParent() == BB;
1980 }),
1981 DbgValues.end());
1982
1983 // If there are no uses outside the block, we're done with this instruction.
1984 if (UsesToRename.empty() && DbgValues.empty())
1985 continue;
1986 LLVM_DEBUG(dbgs() << "JT: Renaming non-local uses of: " << I << "\n");
1987
1988 // We found a use of I outside of BB. Rename all uses of I that are outside
1989 // its block to be uses of the appropriate PHI node etc. See ValuesInBlocks
1990 // with the two values we know.
1991 SSAUpdate.Initialize(I.getType(), I.getName());
1992 SSAUpdate.AddAvailableValue(BB, &I);
1993 SSAUpdate.AddAvailableValue(NewBB, ValueMapping[&I]);
1994
1995 while (!UsesToRename.empty())
1996 SSAUpdate.RewriteUse(*UsesToRename.pop_back_val());
1997 if (!DbgValues.empty()) {
1998 SSAUpdate.UpdateDebugValues(&I, DbgValues);
1999 DbgValues.clear();
2000 }
2001
2002 LLVM_DEBUG(dbgs() << "\n");
2003 }
2004}
2005
2006/// Clone instructions in range [BI, BE) to NewBB. For PHI nodes, we only clone
2007/// arguments that come from PredBB. Return the map from the variables in the
2008/// source basic block to the variables in the newly created basic block.
2012 BasicBlock *PredBB) {
2013 // We are going to have to map operands from the source basic block to the new
2014 // copy of the block 'NewBB'. If there are PHI nodes in the source basic
2015 // block, evaluate them to account for entry from PredBB.
2017
2018 // Retargets llvm.dbg.value to any renamed variables.
2019 auto RetargetDbgValueIfPossible = [&](Instruction *NewInst) -> bool {
2020 auto DbgInstruction = dyn_cast<DbgValueInst>(NewInst);
2021 if (!DbgInstruction)
2022 return false;
2023
2024 SmallSet<std::pair<Value *, Value *>, 16> OperandsToRemap;
2025 for (auto DbgOperand : DbgInstruction->location_ops()) {
2026 auto DbgOperandInstruction = dyn_cast<Instruction>(DbgOperand);
2027 if (!DbgOperandInstruction)
2028 continue;
2029
2030 auto I = ValueMapping.find(DbgOperandInstruction);
2031 if (I != ValueMapping.end()) {
2032 OperandsToRemap.insert(
2033 std::pair<Value *, Value *>(DbgOperand, I->second));
2034 }
2035 }
2036
2037 for (auto &[OldOp, MappedOp] : OperandsToRemap)
2038 DbgInstruction->replaceVariableLocationOp(OldOp, MappedOp);
2039 return true;
2040 };
2041
2042 // Clone the phi nodes of the source basic block into NewBB. The resulting
2043 // phi nodes are trivial since NewBB only has one predecessor, but SSAUpdater
2044 // might need to rewrite the operand of the cloned phi.
2045 for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI) {
2046 PHINode *NewPN = PHINode::Create(PN->getType(), 1, PN->getName(), NewBB);
2047 NewPN->addIncoming(PN->getIncomingValueForBlock(PredBB), PredBB);
2048 ValueMapping[PN] = NewPN;
2049 }
2050
2051 // Clone noalias scope declarations in the threaded block. When threading a
2052 // loop exit, we would otherwise end up with two idential scope declarations
2053 // visible at the same time.
2054 SmallVector<MDNode *> NoAliasScopes;
2055 DenseMap<MDNode *, MDNode *> ClonedScopes;
2056 LLVMContext &Context = PredBB->getContext();
2057 identifyNoAliasScopesToClone(BI, BE, NoAliasScopes);
2058 cloneNoAliasScopes(NoAliasScopes, ClonedScopes, "thread", Context);
2059
2060 // Clone the non-phi instructions of the source basic block into NewBB,
2061 // keeping track of the mapping and using it to remap operands in the cloned
2062 // instructions.
2063 for (; BI != BE; ++BI) {
2064 Instruction *New = BI->clone();
2065 New->setName(BI->getName());
2066 New->insertInto(NewBB, NewBB->end());
2067 ValueMapping[&*BI] = New;
2068 adaptNoAliasScopes(New, ClonedScopes, Context);
2069
2070 if (RetargetDbgValueIfPossible(New))
2071 continue;
2072
2073 // Remap operands to patch up intra-block references.
2074 for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
2075 if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
2077 if (I != ValueMapping.end())
2078 New->setOperand(i, I->second);
2079 }
2080 }
2081
2082 return ValueMapping;
2083}
2084
2085/// Attempt to thread through two successive basic blocks.
2087 Value *Cond) {
2088 // Consider:
2089 //
2090 // PredBB:
2091 // %var = phi i32* [ null, %bb1 ], [ @a, %bb2 ]
2092 // %tobool = icmp eq i32 %cond, 0
2093 // br i1 %tobool, label %BB, label ...
2094 //
2095 // BB:
2096 // %cmp = icmp eq i32* %var, null
2097 // br i1 %cmp, label ..., label ...
2098 //
2099 // We don't know the value of %var at BB even if we know which incoming edge
2100 // we take to BB. However, once we duplicate PredBB for each of its incoming
2101 // edges (say, PredBB1 and PredBB2), we know the value of %var in each copy of
2102 // PredBB. Then we can thread edges PredBB1->BB and PredBB2->BB through BB.
2103
2104 // Require that BB end with a Branch for simplicity.
2105 BranchInst *CondBr = dyn_cast<BranchInst>(BB->getTerminator());
2106 if (!CondBr)
2107 return false;
2108
2109 // BB must have exactly one predecessor.
2110 BasicBlock *PredBB = BB->getSinglePredecessor();
2111 if (!PredBB)
2112 return false;
2113
2114 // Require that PredBB end with a conditional Branch. If PredBB ends with an
2115 // unconditional branch, we should be merging PredBB and BB instead. For
2116 // simplicity, we don't deal with a switch.
2117 BranchInst *PredBBBranch = dyn_cast<BranchInst>(PredBB->getTerminator());
2118 if (!PredBBBranch || PredBBBranch->isUnconditional())
2119 return false;
2120
2121 // If PredBB has exactly one incoming edge, we don't gain anything by copying
2122 // PredBB.
2123 if (PredBB->getSinglePredecessor())
2124 return false;
2125
2126 // Don't thread through PredBB if it contains a successor edge to itself, in
2127 // which case we would infinite loop. Suppose we are threading an edge from
2128 // PredPredBB through PredBB and BB to SuccBB with PredBB containing a
2129 // successor edge to itself. If we allowed jump threading in this case, we
2130 // could duplicate PredBB and BB as, say, PredBB.thread and BB.thread. Since
2131 // PredBB.thread has a successor edge to PredBB, we would immediately come up
2132 // with another jump threading opportunity from PredBB.thread through PredBB
2133 // and BB to SuccBB. This jump threading would repeatedly occur. That is, we
2134 // would keep peeling one iteration from PredBB.
2135 if (llvm::is_contained(successors(PredBB), PredBB))
2136 return false;
2137
2138 // Don't thread across a loop header.
2139 if (LoopHeaders.count(PredBB))
2140 return false;
2141
2142 // Avoid complication with duplicating EH pads.
2143 if (PredBB->isEHPad())
2144 return false;
2145
2146 // Find a predecessor that we can thread. For simplicity, we only consider a
2147 // successor edge out of BB to which we thread exactly one incoming edge into
2148 // PredBB.
2149 unsigned ZeroCount = 0;
2150 unsigned OneCount = 0;
2151 BasicBlock *ZeroPred = nullptr;
2152 BasicBlock *OnePred = nullptr;
2153 for (BasicBlock *P : predecessors(PredBB)) {
2154 // If PredPred ends with IndirectBrInst, we can't handle it.
2155 if (isa<IndirectBrInst>(P->getTerminator()))
2156 continue;
2157 if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(
2159 if (CI->isZero()) {
2160 ZeroCount++;
2161 ZeroPred = P;
2162 } else if (CI->isOne()) {
2163 OneCount++;
2164 OnePred = P;
2165 }
2166 }
2167 }
2168
2169 // Disregard complicated cases where we have to thread multiple edges.
2170 BasicBlock *PredPredBB;
2171 if (ZeroCount == 1) {
2172 PredPredBB = ZeroPred;
2173 } else if (OneCount == 1) {
2174 PredPredBB = OnePred;
2175 } else {
2176 return false;
2177 }
2178
2179 BasicBlock *SuccBB = CondBr->getSuccessor(PredPredBB == ZeroPred);
2180
2181 // If threading to the same block as we come from, we would infinite loop.
2182 if (SuccBB == BB) {
2183 LLVM_DEBUG(dbgs() << " Not threading across BB '" << BB->getName()
2184 << "' - would thread to self!\n");
2185 return false;
2186 }
2187
2188 // If threading this would thread across a loop header, don't thread the edge.
2189 // See the comments above findLoopHeaders for justifications and caveats.
2190 if (LoopHeaders.count(BB) || LoopHeaders.count(SuccBB)) {
2191 LLVM_DEBUG({
2192 bool BBIsHeader = LoopHeaders.count(BB);
2193 bool SuccIsHeader = LoopHeaders.count(SuccBB);
2194 dbgs() << " Not threading across "
2195 << (BBIsHeader ? "loop header BB '" : "block BB '")
2196 << BB->getName() << "' to dest "
2197 << (SuccIsHeader ? "loop header BB '" : "block BB '")
2198 << SuccBB->getName()
2199 << "' - it might create an irreducible loop!\n";
2200 });
2201 return false;
2202 }
2203
2204 // Compute the cost of duplicating BB and PredBB.
2205 unsigned BBCost = getJumpThreadDuplicationCost(
2206 TTI, BB, BB->getTerminator(), BBDupThreshold);
2207 unsigned PredBBCost = getJumpThreadDuplicationCost(
2208 TTI, PredBB, PredBB->getTerminator(), BBDupThreshold);
2209
2210 // Give up if costs are too high. We need to check BBCost and PredBBCost
2211 // individually before checking their sum because getJumpThreadDuplicationCost
2212 // return (unsigned)~0 for those basic blocks that cannot be duplicated.
2213 if (BBCost > BBDupThreshold || PredBBCost > BBDupThreshold ||
2214 BBCost + PredBBCost > BBDupThreshold) {
2215 LLVM_DEBUG(dbgs() << " Not threading BB '" << BB->getName()
2216 << "' - Cost is too high: " << PredBBCost
2217 << " for PredBB, " << BBCost << "for BB\n");
2218 return false;
2219 }
2220
2221 // Now we are ready to duplicate PredBB.
2222 threadThroughTwoBasicBlocks(PredPredBB, PredBB, BB, SuccBB);
2223 return true;
2224}
2225
2227 BasicBlock *PredBB,
2228 BasicBlock *BB,
2229 BasicBlock *SuccBB) {
2230 LLVM_DEBUG(dbgs() << " Threading through '" << PredBB->getName() << "' and '"
2231 << BB->getName() << "'\n");
2232
2233 // Build BPI/BFI before any changes are made to IR.
2234 bool HasProfile = doesBlockHaveProfileData(BB);
2235 auto *BFI = getOrCreateBFI(HasProfile);
2236 auto *BPI = getOrCreateBPI(BFI != nullptr);
2237
2238 BranchInst *CondBr = cast<BranchInst>(BB->getTerminator());
2239 BranchInst *PredBBBranch = cast<BranchInst>(PredBB->getTerminator());
2240
2241 BasicBlock *NewBB =
2242 BasicBlock::Create(PredBB->getContext(), PredBB->getName() + ".thread",
2243 PredBB->getParent(), PredBB);
2244 NewBB->moveAfter(PredBB);
2245
2246 // Set the block frequency of NewBB.
2247 if (BFI) {
2248 assert(BPI && "It's expected BPI to exist along with BFI");
2249 auto NewBBFreq = BFI->getBlockFreq(PredPredBB) *
2250 BPI->getEdgeProbability(PredPredBB, PredBB);
2251 BFI->setBlockFreq(NewBB, NewBBFreq.getFrequency());
2252 }
2253
2254 // We are going to have to map operands from the original BB block to the new
2255 // copy of the block 'NewBB'. If there are PHI nodes in PredBB, evaluate them
2256 // to account for entry from PredPredBB.
2258 cloneInstructions(PredBB->begin(), PredBB->end(), NewBB, PredPredBB);
2259
2260 // Copy the edge probabilities from PredBB to NewBB.
2261 if (BPI)
2262 BPI->copyEdgeProbabilities(PredBB, NewBB);
2263
2264 // Update the terminator of PredPredBB to jump to NewBB instead of PredBB.
2265 // This eliminates predecessors from PredPredBB, which requires us to simplify
2266 // any PHI nodes in PredBB.
2267 Instruction *PredPredTerm = PredPredBB->getTerminator();
2268 for (unsigned i = 0, e = PredPredTerm->getNumSuccessors(); i != e; ++i)
2269 if (PredPredTerm->getSuccessor(i) == PredBB) {
2270 PredBB->removePredecessor(PredPredBB, true);
2271 PredPredTerm->setSuccessor(i, NewBB);
2272 }
2273
2274 addPHINodeEntriesForMappedBlock(PredBBBranch->getSuccessor(0), PredBB, NewBB,
2275 ValueMapping);
2276 addPHINodeEntriesForMappedBlock(PredBBBranch->getSuccessor(1), PredBB, NewBB,
2277 ValueMapping);
2278
2279 DTU->applyUpdatesPermissive(
2280 {{DominatorTree::Insert, NewBB, CondBr->getSuccessor(0)},
2281 {DominatorTree::Insert, NewBB, CondBr->getSuccessor(1)},
2282 {DominatorTree::Insert, PredPredBB, NewBB},
2283 {DominatorTree::Delete, PredPredBB, PredBB}});
2284
2285 updateSSA(PredBB, NewBB, ValueMapping);
2286
2287 // Clean up things like PHI nodes with single operands, dead instructions,
2288 // etc.
2289 SimplifyInstructionsInBlock(NewBB, TLI);
2290 SimplifyInstructionsInBlock(PredBB, TLI);
2291
2292 SmallVector<BasicBlock *, 1> PredsToFactor;
2293 PredsToFactor.push_back(NewBB);
2294 threadEdge(BB, PredsToFactor, SuccBB);
2295}
2296
2297/// tryThreadEdge - Thread an edge if it's safe and profitable to do so.
2299 BasicBlock *BB, const SmallVectorImpl<BasicBlock *> &PredBBs,
2300 BasicBlock *SuccBB) {
2301 // If threading to the same block as we come from, we would infinite loop.
2302 if (SuccBB == BB) {
2303 LLVM_DEBUG(dbgs() << " Not threading across BB '" << BB->getName()
2304 << "' - would thread to self!\n");
2305 return false;
2306 }
2307
2308 // If threading this would thread across a loop header, don't thread the edge.
2309 // See the comments above findLoopHeaders for justifications and caveats.
2310 if (LoopHeaders.count(BB) || LoopHeaders.count(SuccBB)) {
2311 LLVM_DEBUG({
2312 bool BBIsHeader = LoopHeaders.count(BB);
2313 bool SuccIsHeader = LoopHeaders.count(SuccBB);
2314 dbgs() << " Not threading across "
2315 << (BBIsHeader ? "loop header BB '" : "block BB '") << BB->getName()
2316 << "' to dest " << (SuccIsHeader ? "loop header BB '" : "block BB '")
2317 << SuccBB->getName() << "' - it might create an irreducible loop!\n";
2318 });
2319 return false;
2320 }
2321
2322 unsigned JumpThreadCost = getJumpThreadDuplicationCost(
2323 TTI, BB, BB->getTerminator(), BBDupThreshold);
2324 if (JumpThreadCost > BBDupThreshold) {
2325 LLVM_DEBUG(dbgs() << " Not threading BB '" << BB->getName()
2326 << "' - Cost is too high: " << JumpThreadCost << "\n");
2327 return false;
2328 }
2329
2330 threadEdge(BB, PredBBs, SuccBB);
2331 return true;
2332}
2333
2334/// threadEdge - We have decided that it is safe and profitable to factor the
2335/// blocks in PredBBs to one predecessor, then thread an edge from it to SuccBB
2336/// across BB. Transform the IR to reflect this change.
2338 const SmallVectorImpl<BasicBlock *> &PredBBs,
2339 BasicBlock *SuccBB) {
2340 assert(SuccBB != BB && "Don't create an infinite loop");
2341
2342 assert(!LoopHeaders.count(BB) && !LoopHeaders.count(SuccBB) &&
2343 "Don't thread across loop headers");
2344
2345 // Build BPI/BFI before any changes are made to IR.
2346 bool HasProfile = doesBlockHaveProfileData(BB);
2347 auto *BFI = getOrCreateBFI(HasProfile);
2348 auto *BPI = getOrCreateBPI(BFI != nullptr);
2349
2350 // And finally, do it! Start by factoring the predecessors if needed.
2351 BasicBlock *PredBB;
2352 if (PredBBs.size() == 1)
2353 PredBB = PredBBs[0];
2354 else {
2355 LLVM_DEBUG(dbgs() << " Factoring out " << PredBBs.size()
2356 << " common predecessors.\n");
2357 PredBB = splitBlockPreds(BB, PredBBs, ".thr_comm");
2358 }
2359
2360 // And finally, do it!
2361 LLVM_DEBUG(dbgs() << " Threading edge from '" << PredBB->getName()
2362 << "' to '" << SuccBB->getName()
2363 << ", across block:\n " << *BB << "\n");
2364
2365 LVI->threadEdge(PredBB, BB, SuccBB);
2366
2368 BB->getName()+".thread",
2369 BB->getParent(), BB);
2370 NewBB->moveAfter(PredBB);
2371
2372 // Set the block frequency of NewBB.
2373 if (BFI) {
2374 assert(BPI && "It's expected BPI to exist along with BFI");
2375 auto NewBBFreq =
2376 BFI->getBlockFreq(PredBB) * BPI->getEdgeProbability(PredBB, BB);
2377 BFI->setBlockFreq(NewBB, NewBBFreq.getFrequency());
2378 }
2379
2380 // Copy all the instructions from BB to NewBB except the terminator.
2382 cloneInstructions(BB->begin(), std::prev(BB->end()), NewBB, PredBB);
2383
2384 // We didn't copy the terminator from BB over to NewBB, because there is now
2385 // an unconditional jump to SuccBB. Insert the unconditional jump.
2386 BranchInst *NewBI = BranchInst::Create(SuccBB, NewBB);
2387 NewBI->setDebugLoc(BB->getTerminator()->getDebugLoc());
2388
2389 // Check to see if SuccBB has PHI nodes. If so, we need to add entries to the
2390 // PHI nodes for NewBB now.
2391 addPHINodeEntriesForMappedBlock(SuccBB, BB, NewBB, ValueMapping);
2392
2393 // Update the terminator of PredBB to jump to NewBB instead of BB. This
2394 // eliminates predecessors from BB, which requires us to simplify any PHI
2395 // nodes in BB.
2396 Instruction *PredTerm = PredBB->getTerminator();
2397 for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i)
2398 if (PredTerm->getSuccessor(i) == BB) {
2399 BB->removePredecessor(PredBB, true);
2400 PredTerm->setSuccessor(i, NewBB);
2401 }
2402
2403 // Enqueue required DT updates.
2404 DTU->applyUpdatesPermissive({{DominatorTree::Insert, NewBB, SuccBB},
2405 {DominatorTree::Insert, PredBB, NewBB},
2406 {DominatorTree::Delete, PredBB, BB}});
2407
2408 updateSSA(BB, NewBB, ValueMapping);
2409
2410 // At this point, the IR is fully up to date and consistent. Do a quick scan
2411 // over the new instructions and zap any that are constants or dead. This
2412 // frequently happens because of phi translation.
2413 SimplifyInstructionsInBlock(NewBB, TLI);
2414
2415 // Update the edge weight from BB to SuccBB, which should be less than before.
2416 updateBlockFreqAndEdgeWeight(PredBB, BB, NewBB, SuccBB, BFI, BPI, HasProfile);
2417
2418 // Threaded an edge!
2419 ++NumThreads;
2420}
2421
2422/// Create a new basic block that will be the predecessor of BB and successor of
2423/// all blocks in Preds. When profile data is available, update the frequency of
2424/// this new block.
2425BasicBlock *JumpThreadingPass::splitBlockPreds(BasicBlock *BB,
2427 const char *Suffix) {
2429
2430 // Collect the frequencies of all predecessors of BB, which will be used to
2431 // update the edge weight of the result of splitting predecessors.
2433 auto *BFI = getBFI();
2434 if (BFI) {
2435 auto *BPI = getOrCreateBPI(true);
2436 for (auto *Pred : Preds)
2437 FreqMap.insert(std::make_pair(
2438 Pred, BFI->getBlockFreq(Pred) * BPI->getEdgeProbability(Pred, BB)));
2439 }
2440
2441 // In the case when BB is a LandingPad block we create 2 new predecessors
2442 // instead of just one.
2443 if (BB->isLandingPad()) {
2444 std::string NewName = std::string(Suffix) + ".split-lp";
2445 SplitLandingPadPredecessors(BB, Preds, Suffix, NewName.c_str(), NewBBs);
2446 } else {
2447 NewBBs.push_back(SplitBlockPredecessors(BB, Preds, Suffix));
2448 }
2449
2450 std::vector<DominatorTree::UpdateType> Updates;
2451 Updates.reserve((2 * Preds.size()) + NewBBs.size());
2452 for (auto *NewBB : NewBBs) {
2453 BlockFrequency NewBBFreq(0);
2454 Updates.push_back({DominatorTree::Insert, NewBB, BB});
2455 for (auto *Pred : predecessors(NewBB)) {
2456 Updates.push_back({DominatorTree::Delete, Pred, BB});
2457 Updates.push_back({DominatorTree::Insert, Pred, NewBB});
2458 if (BFI) // Update frequencies between Pred -> NewBB.
2459 NewBBFreq += FreqMap.lookup(Pred);
2460 }
2461 if (BFI) // Apply the summed frequency to NewBB.
2462 BFI->setBlockFreq(NewBB, NewBBFreq.getFrequency());
2463 }
2464
2465 DTU->applyUpdatesPermissive(Updates);
2466 return NewBBs[0];
2467}
2468
2469bool JumpThreadingPass::doesBlockHaveProfileData(BasicBlock *BB) {
2470 const Instruction *TI = BB->getTerminator();
2471 if (!TI || TI->getNumSuccessors() < 2)
2472 return false;
2473
2474 return hasValidBranchWeightMD(*TI);
2475}
2476
2477/// Update the block frequency of BB and branch weight and the metadata on the
2478/// edge BB->SuccBB. This is done by scaling the weight of BB->SuccBB by 1 -
2479/// Freq(PredBB->BB) / Freq(BB->SuccBB).
2480void JumpThreadingPass::updateBlockFreqAndEdgeWeight(BasicBlock *PredBB,
2481 BasicBlock *BB,
2482 BasicBlock *NewBB,
2483 BasicBlock *SuccBB,
2484 BlockFrequencyInfo *BFI,
2486 bool HasProfile) {
2487 assert(((BFI && BPI) || (!BFI && !BFI)) &&
2488 "Both BFI & BPI should either be set or unset");
2489
2490 if (!BFI) {
2491 assert(!HasProfile &&
2492 "It's expected to have BFI/BPI when profile info exists");
2493 return;
2494 }
2495
2496 // As the edge from PredBB to BB is deleted, we have to update the block
2497 // frequency of BB.
2498 auto BBOrigFreq = BFI->getBlockFreq(BB);
2499 auto NewBBFreq = BFI->getBlockFreq(NewBB);
2500 auto BB2SuccBBFreq = BBOrigFreq * BPI->getEdgeProbability(BB, SuccBB);
2501 auto BBNewFreq = BBOrigFreq - NewBBFreq;
2502 BFI->setBlockFreq(BB, BBNewFreq.getFrequency());
2503
2504 // Collect updated outgoing edges' frequencies from BB and use them to update
2505 // edge probabilities.
2506 SmallVector<uint64_t, 4> BBSuccFreq;
2507 for (BasicBlock *Succ : successors(BB)) {
2508 auto SuccFreq = (Succ == SuccBB)
2509 ? BB2SuccBBFreq - NewBBFreq
2510 : BBOrigFreq * BPI->getEdgeProbability(BB, Succ);
2511 BBSuccFreq.push_back(SuccFreq.getFrequency());
2512 }
2513
2514 uint64_t MaxBBSuccFreq =
2515 *std::max_element(BBSuccFreq.begin(), BBSuccFreq.end());
2516
2518 if (MaxBBSuccFreq == 0)
2519 BBSuccProbs.assign(BBSuccFreq.size(),
2520 {1, static_cast<unsigned>(BBSuccFreq.size())});
2521 else {
2522 for (uint64_t Freq : BBSuccFreq)
2523 BBSuccProbs.push_back(
2524 BranchProbability::getBranchProbability(Freq, MaxBBSuccFreq));
2525 // Normalize edge probabilities so that they sum up to one.
2527 BBSuccProbs.end());
2528 }
2529
2530 // Update edge probabilities in BPI.
2531 BPI->setEdgeProbability(BB, BBSuccProbs);
2532
2533 // Update the profile metadata as well.
2534 //
2535 // Don't do this if the profile of the transformed blocks was statically
2536 // estimated. (This could occur despite the function having an entry
2537 // frequency in completely cold parts of the CFG.)
2538 //
2539 // In this case we don't want to suggest to subsequent passes that the
2540 // calculated weights are fully consistent. Consider this graph:
2541 //
2542 // check_1
2543 // 50% / |
2544 // eq_1 | 50%
2545 // \ |
2546 // check_2
2547 // 50% / |
2548 // eq_2 | 50%
2549 // \ |
2550 // check_3
2551 // 50% / |
2552 // eq_3 | 50%
2553 // \ |
2554 //
2555 // Assuming the blocks check_* all compare the same value against 1, 2 and 3,
2556 // the overall probabilities are inconsistent; the total probability that the
2557 // value is either 1, 2 or 3 is 150%.
2558 //
2559 // As a consequence if we thread eq_1 -> check_2 to check_3, check_2->check_3
2560 // becomes 0%. This is even worse if the edge whose probability becomes 0% is
2561 // the loop exit edge. Then based solely on static estimation we would assume
2562 // the loop was extremely hot.
2563 //
2564 // FIXME this locally as well so that BPI and BFI are consistent as well. We
2565 // shouldn't make edges extremely likely or unlikely based solely on static
2566 // estimation.
2567 if (BBSuccProbs.size() >= 2 && HasProfile) {
2569 for (auto Prob : BBSuccProbs)
2570 Weights.push_back(Prob.getNumerator());
2571
2572 auto TI = BB->getTerminator();
2573 TI->setMetadata(
2574 LLVMContext::MD_prof,
2576 }
2577}
2578
2579/// duplicateCondBranchOnPHIIntoPred - PredBB contains an unconditional branch
2580/// to BB which contains an i1 PHI node and a conditional branch on that PHI.
2581/// If we can duplicate the contents of BB up into PredBB do so now, this
2582/// improves the odds that the branch will be on an analyzable instruction like
2583/// a compare.
2585 BasicBlock *BB, const SmallVectorImpl<BasicBlock *> &PredBBs) {
2586 assert(!PredBBs.empty() && "Can't handle an empty set");
2587
2588 // If BB is a loop header, then duplicating this block outside the loop would
2589 // cause us to transform this into an irreducible loop, don't do this.
2590 // See the comments above findLoopHeaders for justifications and caveats.
2591 if (LoopHeaders.count(BB)) {
2592 LLVM_DEBUG(dbgs() << " Not duplicating loop header '" << BB->getName()
2593 << "' into predecessor block '" << PredBBs[0]->getName()
2594 << "' - it might create an irreducible loop!\n");
2595 return false;
2596 }
2597
2598 unsigned DuplicationCost = getJumpThreadDuplicationCost(
2599 TTI, BB, BB->getTerminator(), BBDupThreshold);
2600 if (DuplicationCost > BBDupThreshold) {
2601 LLVM_DEBUG(dbgs() << " Not duplicating BB '" << BB->getName()
2602 << "' - Cost is too high: " << DuplicationCost << "\n");
2603 return false;
2604 }
2605
2606 // And finally, do it! Start by factoring the predecessors if needed.
2607 std::vector<DominatorTree::UpdateType> Updates;
2608 BasicBlock *PredBB;
2609 if (PredBBs.size() == 1)
2610 PredBB = PredBBs[0];
2611 else {
2612 LLVM_DEBUG(dbgs() << " Factoring out " << PredBBs.size()
2613 << " common predecessors.\n");
2614 PredBB = splitBlockPreds(BB, PredBBs, ".thr_comm");
2615 }
2616 Updates.push_back({DominatorTree::Delete, PredBB, BB});
2617
2618 // Okay, we decided to do this! Clone all the instructions in BB onto the end
2619 // of PredBB.
2620 LLVM_DEBUG(dbgs() << " Duplicating block '" << BB->getName()
2621 << "' into end of '" << PredBB->getName()
2622 << "' to eliminate branch on phi. Cost: "
2623 << DuplicationCost << " block is:" << *BB << "\n");
2624
2625 // Unless PredBB ends with an unconditional branch, split the edge so that we
2626 // can just clone the bits from BB into the end of the new PredBB.
2627 BranchInst *OldPredBranch = dyn_cast<BranchInst>(PredBB->getTerminator());
2628
2629 if (!OldPredBranch || !OldPredBranch->isUnconditional()) {
2630 BasicBlock *OldPredBB = PredBB;
2631 PredBB = SplitEdge(OldPredBB, BB);
2632 Updates.push_back({DominatorTree::Insert, OldPredBB, PredBB});
2633 Updates.push_back({DominatorTree::Insert, PredBB, BB});
2634 Updates.push_back({DominatorTree::Delete, OldPredBB, BB});
2635 OldPredBranch = cast<BranchInst>(PredBB->getTerminator());
2636 }
2637
2638 // We are going to have to map operands from the original BB block into the
2639 // PredBB block. Evaluate PHI nodes in BB.
2640 DenseMap<Instruction*, Value*> ValueMapping;
2641
2642 BasicBlock::iterator BI = BB->begin();
2643 for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
2644 ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
2645 // Clone the non-phi instructions of BB into PredBB, keeping track of the
2646 // mapping and using it to remap operands in the cloned instructions.
2647 for (; BI != BB->end(); ++BI) {
2648 Instruction *New = BI->clone();
2649
2650 // Remap operands to patch up intra-block references.
2651 for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
2652 if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
2653 DenseMap<Instruction*, Value*>::iterator I = ValueMapping.find(Inst);
2654 if (I != ValueMapping.end())
2655 New->setOperand(i, I->second);
2656 }
2657
2658 // If this instruction can be simplified after the operands are updated,
2659 // just use the simplified value instead. This frequently happens due to
2660 // phi translation.
2662 New,
2663 {BB->getModule()->getDataLayout(), TLI, nullptr, nullptr, New})) {
2664 ValueMapping[&*BI] = IV;
2665 if (!New->mayHaveSideEffects()) {
2666 New->deleteValue();
2667 New = nullptr;
2668 }
2669 } else {
2670 ValueMapping[&*BI] = New;
2671 }
2672 if (New) {
2673 // Otherwise, insert the new instruction into the block.
2674 New->setName(BI->getName());
2675 New->insertInto(PredBB, OldPredBranch->getIterator());
2676 // Update Dominance from simplified New instruction operands.
2677 for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
2678 if (BasicBlock *SuccBB = dyn_cast<BasicBlock>(New->getOperand(i)))
2679 Updates.push_back({DominatorTree::Insert, PredBB, SuccBB});
2680 }
2681 }
2682
2683 // Check to see if the targets of the branch had PHI nodes. If so, we need to
2684 // add entries to the PHI nodes for branch from PredBB now.
2685 BranchInst *BBBranch = cast<BranchInst>(BB->getTerminator());
2686 addPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(0), BB, PredBB,
2687 ValueMapping);
2688 addPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(1), BB, PredBB,
2689 ValueMapping);
2690
2691 updateSSA(BB, PredBB, ValueMapping);
2692
2693 // PredBB no longer jumps to BB, remove entries in the PHI node for the edge
2694 // that we nuked.
2695 BB->removePredecessor(PredBB, true);
2696
2697 // Remove the unconditional branch at the end of the PredBB block.
2698 OldPredBranch->eraseFromParent();
2699 if (auto *BPI = getBPI())
2700 BPI->copyEdgeProbabilities(BB, PredBB);
2701 DTU->applyUpdatesPermissive(Updates);
2702
2703 ++NumDupes;
2704 return true;
2705}
2706
2707// Pred is a predecessor of BB with an unconditional branch to BB. SI is
2708// a Select instruction in Pred. BB has other predecessors and SI is used in
2709// a PHI node in BB. SI has no other use.
2710// A new basic block, NewBB, is created and SI is converted to compare and
2711// conditional branch. SI is erased from parent.
2713 SelectInst *SI, PHINode *SIUse,
2714 unsigned Idx) {
2715 // Expand the select.
2716 //
2717 // Pred --
2718 // | v
2719 // | NewBB
2720 // | |
2721 // |-----
2722 // v
2723 // BB
2724 BranchInst *PredTerm = cast<BranchInst>(Pred->getTerminator());
2725 BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "select.unfold",
2726 BB->getParent(), BB);
2727 // Move the unconditional branch to NewBB.
2728 PredTerm->removeFromParent();
2729 PredTerm->insertInto(NewBB, NewBB->end());
2730 // Create a conditional branch and update PHI nodes.
2731 auto *BI = BranchInst::Create(NewBB, BB, SI->getCondition(), Pred);
2732 BI->applyMergedLocation(PredTerm->getDebugLoc(), SI->getDebugLoc());
2733 BI->copyMetadata(*SI, {LLVMContext::MD_prof});
2734 SIUse->setIncomingValue(Idx, SI->getFalseValue());
2735 SIUse->addIncoming(SI->getTrueValue(), NewBB);
2736
2737 uint64_t TrueWeight = 1;
2738 uint64_t FalseWeight = 1;
2739 // Copy probabilities from 'SI' to created conditional branch in 'Pred'.
2740 if (extractBranchWeights(*SI, TrueWeight, FalseWeight) &&
2741 (TrueWeight + FalseWeight) != 0) {
2744 TrueWeight, TrueWeight + FalseWeight));
2746 FalseWeight, TrueWeight + FalseWeight));
2747 // Update BPI if exists.
2748 if (auto *BPI = getBPI())
2749 BPI->setEdgeProbability(Pred, BP);
2750 }
2751 // Set the block frequency of NewBB.
2752 if (auto *BFI = getBFI()) {
2753 if ((TrueWeight + FalseWeight) == 0) {
2754 TrueWeight = 1;
2755 FalseWeight = 1;
2756 }
2758 TrueWeight, TrueWeight + FalseWeight);
2759 auto NewBBFreq = BFI->getBlockFreq(Pred) * PredToNewBBProb;
2760 BFI->setBlockFreq(NewBB, NewBBFreq.getFrequency());
2761 }
2762
2763 // The select is now dead.
2764 SI->eraseFromParent();
2765 DTU->applyUpdatesPermissive({{DominatorTree::Insert, NewBB, BB},
2766 {DominatorTree::Insert, Pred, NewBB}});
2767
2768 // Update any other PHI nodes in BB.
2769 for (BasicBlock::iterator BI = BB->begin();
2770 PHINode *Phi = dyn_cast<PHINode>(BI); ++BI)
2771 if (Phi != SIUse)
2772 Phi->addIncoming(Phi->getIncomingValueForBlock(Pred), NewBB);
2773}
2774
2776 PHINode *CondPHI = dyn_cast<PHINode>(SI->getCondition());
2777
2778 if (!CondPHI || CondPHI->getParent() != BB)
2779 return false;
2780
2781 for (unsigned I = 0, E = CondPHI->getNumIncomingValues(); I != E; ++I) {
2782 BasicBlock *Pred = CondPHI->getIncomingBlock(I);
2783 SelectInst *PredSI = dyn_cast<SelectInst>(CondPHI->getIncomingValue(I));
2784
2785 // The second and third condition can be potentially relaxed. Currently
2786 // the conditions help to simplify the code and allow us to reuse existing
2787 // code, developed for tryToUnfoldSelect(CmpInst *, BasicBlock *)
2788 if (!PredSI || PredSI->getParent() != Pred || !PredSI->hasOneUse())
2789 continue;
2790
2791 BranchInst *PredTerm = dyn_cast<BranchInst>(Pred->getTerminator());
2792 if (!PredTerm || !PredTerm->isUnconditional())
2793 continue;
2794
2795 unfoldSelectInstr(Pred, BB, PredSI, CondPHI, I);
2796 return true;
2797 }
2798 return false;
2799}
2800
2801/// tryToUnfoldSelect - Look for blocks of the form
2802/// bb1:
2803/// %a = select
2804/// br bb2
2805///
2806/// bb2:
2807/// %p = phi [%a, %bb1] ...
2808/// %c = icmp %p
2809/// br i1 %c
2810///
2811/// And expand the select into a branch structure if one of its arms allows %c
2812/// to be folded. This later enables threading from bb1 over bb2.
2814 BranchInst *CondBr = dyn_cast<BranchInst>(BB->getTerminator());
2815 PHINode *CondLHS = dyn_cast<PHINode>(CondCmp->getOperand(0));
2816 Constant *CondRHS = cast<Constant>(CondCmp->getOperand(1));
2817
2818 if (!CondBr || !CondBr->isConditional() || !CondLHS ||
2819 CondLHS->getParent() != BB)
2820 return false;
2821
2822 for (unsigned I = 0, E = CondLHS->getNumIncomingValues(); I != E; ++I) {
2823 BasicBlock *Pred = CondLHS->getIncomingBlock(I);
2824 SelectInst *SI = dyn_cast<SelectInst>(CondLHS->getIncomingValue(I));
2825
2826 // Look if one of the incoming values is a select in the corresponding
2827 // predecessor.
2828 if (!SI || SI->getParent() != Pred || !SI->hasOneUse())
2829 continue;
2830
2831 BranchInst *PredTerm = dyn_cast<BranchInst>(Pred->getTerminator());
2832 if (!PredTerm || !PredTerm->isUnconditional())
2833 continue;
2834
2835 // Now check if one of the select values would allow us to constant fold the
2836 // terminator in BB. We don't do the transform if both sides fold, those
2837 // cases will be threaded in any case.
2838 LazyValueInfo::Tristate LHSFolds =
2839 LVI->getPredicateOnEdge(CondCmp->getPredicate(), SI->getOperand(1),
2840 CondRHS, Pred, BB, CondCmp);
2841 LazyValueInfo::Tristate RHSFolds =
2842 LVI->getPredicateOnEdge(CondCmp->getPredicate(), SI->getOperand(2),
2843 CondRHS, Pred, BB, CondCmp);
2844 if ((LHSFolds != LazyValueInfo::Unknown ||
2845 RHSFolds != LazyValueInfo::Unknown) &&
2846 LHSFolds != RHSFolds) {
2847 unfoldSelectInstr(Pred, BB, SI, CondLHS, I);
2848 return true;
2849 }
2850 }
2851 return false;
2852}
2853
2854/// tryToUnfoldSelectInCurrBB - Look for PHI/Select or PHI/CMP/Select in the
2855/// same BB in the form
2856/// bb:
2857/// %p = phi [false, %bb1], [true, %bb2], [false, %bb3], [true, %bb4], ...
2858/// %s = select %p, trueval, falseval
2859///
2860/// or
2861///
2862/// bb:
2863/// %p = phi [0, %bb1], [1, %bb2], [0, %bb3], [1, %bb4], ...
2864/// %c = cmp %p, 0
2865/// %s = select %c, trueval, falseval
2866///
2867/// And expand the select into a branch structure. This later enables
2868/// jump-threading over bb in this pass.
2869///
2870/// Using the similar approach of SimplifyCFG::FoldCondBranchOnPHI(), unfold
2871/// select if the associated PHI has at least one constant. If the unfolded
2872/// select is not jump-threaded, it will be folded again in the later
2873/// optimizations.
2875 // This transform would reduce the quality of msan diagnostics.
2876 // Disable this transform under MemorySanitizer.
2877 if (BB->getParent()->hasFnAttribute(Attribute::SanitizeMemory))
2878 return false;
2879
2880 // If threading this would thread across a loop header, don't thread the edge.
2881 // See the comments above findLoopHeaders for justifications and caveats.
2882 if (LoopHeaders.count(BB))
2883 return false;
2884
2885 for (BasicBlock::iterator BI = BB->begin();
2886 PHINode *PN = dyn_cast<PHINode>(BI); ++BI) {
2887 // Look for a Phi having at least one constant incoming value.
2888 if (llvm::all_of(PN->incoming_values(),
2889 [](Value *V) { return !isa<ConstantInt>(V); }))
2890 continue;
2891
2892 auto isUnfoldCandidate = [BB](SelectInst *SI, Value *V) {
2893 using namespace PatternMatch;
2894
2895 // Check if SI is in BB and use V as condition.
2896 if (SI->getParent() != BB)
2897 return false;
2898 Value *Cond = SI->getCondition();
2899 bool IsAndOr = match(SI, m_CombineOr(m_LogicalAnd(), m_LogicalOr()));
2900 return Cond && Cond == V && Cond->getType()->isIntegerTy(1) && !IsAndOr;
2901 };
2902
2903 SelectInst *SI = nullptr;
2904 for (Use &U : PN->uses()) {
2905 if (ICmpInst *Cmp = dyn_cast<ICmpInst>(U.getUser())) {
2906 // Look for a ICmp in BB that compares PN with a constant and is the
2907 // condition of a Select.
2908 if (Cmp->getParent() == BB && Cmp->hasOneUse() &&
2909 isa<ConstantInt>(Cmp->getOperand(1 - U.getOperandNo())))
2910 if (SelectInst *SelectI = dyn_cast<SelectInst>(Cmp->user_back()))
2911 if (isUnfoldCandidate(SelectI, Cmp->use_begin()->get())) {
2912 SI = SelectI;
2913 break;
2914 }
2915 } else if (SelectInst *SelectI = dyn_cast<SelectInst>(U.getUser())) {
2916 // Look for a Select in BB that uses PN as condition.
2917 if (isUnfoldCandidate(SelectI, U.get())) {
2918 SI = SelectI;
2919 break;
2920 }
2921 }
2922 }
2923
2924 if (!SI)
2925 continue;
2926 // Expand the select.
2927 Value *Cond = SI->getCondition();
2929 Cond = new FreezeInst(Cond, "cond.fr", SI);
2931 BasicBlock *SplitBB = SI->getParent();
2932 BasicBlock *NewBB = Term->getParent();
2933 PHINode *NewPN = PHINode::Create(SI->getType(), 2, "", SI);
2934 NewPN->addIncoming(SI->getTrueValue(), Term->getParent());
2935 NewPN->addIncoming(SI->getFalseValue(), BB);
2936 SI->replaceAllUsesWith(NewPN);
2937 SI->eraseFromParent();
2938 // NewBB and SplitBB are newly created blocks which require insertion.
2939 std::vector<DominatorTree::UpdateType> Updates;
2940 Updates.reserve((2 * SplitBB->getTerminator()->getNumSuccessors()) + 3);
2941 Updates.push_back({DominatorTree::Insert, BB, SplitBB});
2942 Updates.push_back({DominatorTree::Insert, BB, NewBB});
2943 Updates.push_back({DominatorTree::Insert, NewBB, SplitBB});
2944 // BB's successors were moved to SplitBB, update DTU accordingly.
2945 for (auto *Succ : successors(SplitBB)) {
2946 Updates.push_back({DominatorTree::Delete, BB, Succ});
2947 Updates.push_back({DominatorTree::Insert, SplitBB, Succ});
2948 }
2949 DTU->applyUpdatesPermissive(Updates);
2950 return true;
2951 }
2952 return false;
2953}
2954
2955/// Try to propagate a guard from the current BB into one of its predecessors
2956/// in case if another branch of execution implies that the condition of this
2957/// guard is always true. Currently we only process the simplest case that
2958/// looks like:
2959///
2960/// Start:
2961/// %cond = ...
2962/// br i1 %cond, label %T1, label %F1
2963/// T1:
2964/// br label %Merge
2965/// F1:
2966/// br label %Merge
2967/// Merge:
2968/// %condGuard = ...
2969/// call void(i1, ...) @llvm.experimental.guard( i1 %condGuard )[ "deopt"() ]
2970///
2971/// And cond either implies condGuard or !condGuard. In this case all the
2972/// instructions before the guard can be duplicated in both branches, and the
2973/// guard is then threaded to one of them.
2975 using namespace PatternMatch;
2976
2977 // We only want to deal with two predecessors.
2978 BasicBlock *Pred1, *Pred2;
2979 auto PI = pred_begin(BB), PE = pred_end(BB);
2980 if (PI == PE)
2981 return false;
2982 Pred1 = *PI++;
2983 if (PI == PE)
2984 return false;
2985 Pred2 = *PI++;
2986 if (PI != PE)
2987 return false;
2988 if (Pred1 == Pred2)
2989 return false;
2990
2991 // Try to thread one of the guards of the block.
2992 // TODO: Look up deeper than to immediate predecessor?
2993 auto *Parent = Pred1->getSinglePredecessor();
2994 if (!Parent || Parent != Pred2->getSinglePredecessor())
2995 return false;
2996
2997 if (auto *BI = dyn_cast<BranchInst>(Parent->getTerminator()))
2998 for (auto &I : *BB)
2999 if (isGuard(&I) && threadGuard(BB, cast<IntrinsicInst>(&I), BI))
3000 return true;
3001
3002 return false;
3003}
3004
3005/// Try to propagate the guard from BB which is the lower block of a diamond
3006/// to one of its branches, in case if diamond's condition implies guard's
3007/// condition.
3009 BranchInst *BI) {
3010 assert(BI->getNumSuccessors() == 2 && "Wrong number of successors?");
3011 assert(BI->isConditional() && "Unconditional branch has 2 successors?");
3012 Value *GuardCond = Guard->getArgOperand(0);
3013 Value *BranchCond = BI->getCondition();
3014 BasicBlock *TrueDest = BI->getSuccessor(0);
3015 BasicBlock *FalseDest = BI->getSuccessor(1);
3016
3017 auto &DL = BB->getModule()->getDataLayout();
3018 bool TrueDestIsSafe = false;
3019 bool FalseDestIsSafe = false;
3020
3021 // True dest is safe if BranchCond => GuardCond.
3022 auto Impl = isImpliedCondition(BranchCond, GuardCond, DL);
3023 if (Impl && *Impl)
3024 TrueDestIsSafe = true;
3025 else {
3026 // False dest is safe if !BranchCond => GuardCond.
3027 Impl = isImpliedCondition(BranchCond, GuardCond, DL, /* LHSIsTrue */ false);
3028 if (Impl && *Impl)
3029 FalseDestIsSafe = true;
3030 }
3031
3032 if (!TrueDestIsSafe && !FalseDestIsSafe)
3033 return false;
3034
3035 BasicBlock *PredUnguardedBlock = TrueDestIsSafe ? TrueDest : FalseDest;
3036 BasicBlock *PredGuardedBlock = FalseDestIsSafe ? TrueDest : FalseDest;
3037
3038 ValueToValueMapTy UnguardedMapping, GuardedMapping;
3039 Instruction *AfterGuard = Guard->getNextNode();
3040 unsigned Cost =
3041 getJumpThreadDuplicationCost(TTI, BB, AfterGuard, BBDupThreshold);
3042 if (Cost > BBDupThreshold)
3043 return false;
3044 // Duplicate all instructions before the guard and the guard itself to the
3045 // branch where implication is not proved.
3047 BB, PredGuardedBlock, AfterGuard, GuardedMapping, *DTU);
3048 assert(GuardedBlock && "Could not create the guarded block?");
3049 // Duplicate all instructions before the guard in the unguarded branch.
3050 // Since we have successfully duplicated the guarded block and this block
3051 // has fewer instructions, we expect it to succeed.
3053 BB, PredUnguardedBlock, Guard, UnguardedMapping, *DTU);
3054 assert(UnguardedBlock && "Could not create the unguarded block?");
3055 LLVM_DEBUG(dbgs() << "Moved guard " << *Guard << " to block "
3056 << GuardedBlock->getName() << "\n");
3057 // Some instructions before the guard may still have uses. For them, we need
3058 // to create Phi nodes merging their copies in both guarded and unguarded
3059 // branches. Those instructions that have no uses can be just removed.
3061 for (auto BI = BB->begin(); &*BI != AfterGuard; ++BI)
3062 if (!isa<PHINode>(&*BI))
3063 ToRemove.push_back(&*BI);
3064
3065 Instruction *InsertionPoint = &*BB->getFirstInsertionPt();
3066 assert(InsertionPoint && "Empty block?");
3067 // Substitute with Phis & remove.
3068 for (auto *Inst : reverse(ToRemove)) {
3069 if (!Inst->use_empty()) {
3070 PHINode *NewPN = PHINode::Create(Inst->getType(), 2);
3071 NewPN->addIncoming(UnguardedMapping[Inst], UnguardedBlock);
3072 NewPN->addIncoming(GuardedMapping[Inst], GuardedBlock);
3073 NewPN->insertBefore(InsertionPoint);
3074 Inst->replaceAllUsesWith(NewPN);
3075 }
3076 Inst->eraseFromParent();
3077 }
3078 return true;
3079}
3080
3081PreservedAnalyses JumpThreadingPass::getPreservedAnalysis() const {
3085
3086 // TODO: We would like to preserve BPI/BFI. Enable once all paths update them.
3087 // TODO: Would be nice to verify BPI/BFI consistency as well.
3088 return PA;
3089}
3090
3091template <typename AnalysisT>
3092typename AnalysisT::Result *JumpThreadingPass::runExternalAnalysis() {
3093 assert(FAM && "Can't run external analysis without FunctionAnalysisManager");
3094
3095 // If there were no changes since last call to 'runExternalAnalysis' then all
3096 // analysis is either up to date or explicitly invalidated. Just go ahead and
3097 // run the "external" analysis.
3098 if (!ChangedSinceLastAnalysisUpdate) {
3099 assert(!DTU->hasPendingUpdates() &&
3100 "Lost update of 'ChangedSinceLastAnalysisUpdate'?");
3101 // Run the "external" analysis.
3102 return &FAM->getResult<AnalysisT>(*F);
3103 }
3104 ChangedSinceLastAnalysisUpdate = false;
3105
3106 auto PA = getPreservedAnalysis();
3107 // TODO: This shouldn't be needed once 'getPreservedAnalysis' reports BPI/BFI
3108 // as preserved.
3109 PA.preserve<BranchProbabilityAnalysis>();
3110 PA.preserve<BlockFrequencyAnalysis>();
3111 // Report everything except explicitly preserved as invalid.
3112 FAM->invalidate(*F, PA);
3113 // Update DT/PDT.
3114 DTU->flush();
3115 // Make sure DT/PDT are valid before running "external" analysis.
3116 assert(DTU->getDomTree().verify(DominatorTree::VerificationLevel::Fast));
3117 assert((!DTU->hasPostDomTree() ||
3118 DTU->getPostDomTree().verify(
3120 // Run the "external" analysis.
3121 auto *Result = &FAM->getResult<AnalysisT>(*F);
3122 // Update analysis JumpThreading depends on and not explicitly preserved.
3123 TTI = &FAM->getResult<TargetIRAnalysis>(*F);
3124 TLI = &FAM->getResult<TargetLibraryAnalysis>(*F);
3125 AA = &FAM->getResult<AAManager>(*F);
3126
3127 return Result;
3128}
3129
3130BranchProbabilityInfo *JumpThreadingPass::getBPI() {
3131 if (!BPI) {
3132 assert(FAM && "Can't create BPI without FunctionAnalysisManager");
3134 }
3135 return *BPI;
3136}
3137
3138BlockFrequencyInfo *JumpThreadingPass::getBFI() {
3139 if (!BFI) {
3140 assert(FAM && "Can't create BFI without FunctionAnalysisManager");
3142 }
3143 return *BFI;
3144}
3145
3146// Important note on validity of BPI/BFI. JumpThreading tries to preserve
3147// BPI/BFI as it goes. Thus if cached instance exists it will be updated.
3148// Otherwise, new instance of BPI/BFI is created (up to date by definition).
3149BranchProbabilityInfo *JumpThreadingPass::getOrCreateBPI(bool Force) {
3150 auto *Res = getBPI();
3151 if (Res)
3152 return Res;
3153
3154 if (Force)
3155 BPI = runExternalAnalysis<BranchProbabilityAnalysis>();
3156
3157 return *BPI;
3158}
3159
3160BlockFrequencyInfo *JumpThreadingPass::getOrCreateBFI(bool Force) {
3161 auto *Res = getBFI();
3162 if (Res)
3163 return Res;
3164
3165 if (Force)
3166 BFI = runExternalAnalysis<BlockFrequencyAnalysis>();
3167
3168 return *BFI;
3169}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Rewrite undef for PHI
ReachingDefAnalysis InstSet & ToRemove
static const Function * getParent(const Value *V)
SmallVector< MachineOperand, 4 > Cond
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define LLVM_DEBUG(X)
Definition: Debug.h:101
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
uint64_t Size
This is the interface for a simple mod/ref and alias analysis over globals.
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
static unsigned getBestDestForJumpOnUndef(BasicBlock *BB)
GetBestDestForBranchOnUndef - If we determine that the specified block ends in an undefined jump,...
static cl::opt< unsigned > PhiDuplicateThreshold("jump-threading-phi-threshold", cl::desc("Max PHIs in BB to duplicate for jump threading"), cl::init(76), cl::Hidden)
static bool replaceFoldableUses(Instruction *Cond, Value *ToVal, BasicBlock *KnownAtEndOfBB)
static cl::opt< unsigned > BBDuplicateThreshold("jump-threading-threshold", cl::desc("Max block size to duplicate for jump threading"), cl::init(6), cl::Hidden)
static cl::opt< bool > ThreadAcrossLoopHeaders("jump-threading-across-loop-headers", cl::desc("Allow JumpThreading to thread across loop headers, for testing"), cl::init(false), cl::Hidden)
static unsigned getJumpThreadDuplicationCost(const TargetTransformInfo *TTI, BasicBlock *BB, Instruction *StopAt, unsigned Threshold)
Return the cost of duplicating a piece of this block from first non-phi and before StopAt instruction...
static BasicBlock * findMostPopularDest(BasicBlock *BB, const SmallVectorImpl< std::pair< BasicBlock *, BasicBlock * > > &PredToDestList)
findMostPopularDest - The specified list contains multiple possible threadable destinations.
static Constant * getKnownConstant(Value *Val, ConstantPreference Preference)
getKnownConstant - Helper method to determine if we can thread over a terminator with the given value...
static cl::opt< bool > PrintLVIAfterJumpThreading("print-lvi-after-jump-threading", cl::desc("Print the LazyValueInfo cache after JumpThreading"), cl::init(false), cl::Hidden)
static cl::opt< unsigned > ImplicationSearchThreshold("jump-threading-implication-search-threshold", cl::desc("The number of predecessors to search for a stronger " "condition to use to thread over a weaker condition"), cl::init(3), cl::Hidden)
static void addPHINodeEntriesForMappedBlock(BasicBlock *PHIBB, BasicBlock *OldPred, BasicBlock *NewPred, DenseMap< Instruction *, Value * > &ValueMap)
addPHINodeEntriesForMappedBlock - We're adding 'NewPred' as a new predecessor to the PHIBB block.
static bool isOpDefinedInBlock(Value *Op, BasicBlock *BB)
Return true if Op is an instruction defined in the given block.
static void updatePredecessorProfileMetadata(PHINode *PN, BasicBlock *BB)
static bool hasAddressTakenAndUsed(BasicBlock *BB)
See the comments on JumpThreadingPass.
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:524
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file implements a map that provides insertion order iteration.
This file provides utility analysis objects describing memory locations.
This file contains the declarations for metadata subclasses.
Module.h This file contains the declarations for the Module class.
LLVMContext & Context
#define P(N)
ppc ctr loops verify
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
This header defines various interfaces for pass management in LLVM.
This file contains the declarations for profiling metadata utility functions.
@ SI
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
This pass exposes codegen information to IR-level passes.
This defines the Use class.
Value * RHS
Value * LHS
static const uint32_t IV[8]
Definition: blake3_impl.h:77
A manager for alias analyses.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
void invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Invalidate cached analyses for an IR unit.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
Definition: PassManager.h:793
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:774
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
iterator end()
Definition: BasicBlock.h:316
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:314
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition: BasicBlock.h:372
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:245
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches,...
Definition: BasicBlock.h:495
InstListType::const_iterator const_iterator
Definition: BasicBlock.h:88
const Instruction & front() const
Definition: BasicBlock.h:326
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:105
void moveAfter(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it right after MovePos in the function M...
Definition: BasicBlock.cpp:141
bool hasNPredecessors(unsigned N) const
Return true if this block has exactly N predecessors.
Definition: BasicBlock.cpp:306
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:284
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:112
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:87
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:35
bool isLandingPad() const
Return true if this basic block is a landing pad.
Definition: BasicBlock.cpp:513
bool isEHPad() const
Return true if this basic block is an exception handling block.
Definition: BasicBlock.h:512
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:127
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
Definition: BasicBlock.cpp:146
void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)
Update PHI nodes in this BasicBlock before removal of predecessor Pred.
Definition: BasicBlock.cpp:341
The address of a basic block.
Definition: Constants.h:875
static BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
Definition: Constants.cpp:1777
Analysis pass which computes BlockFrequencyInfo.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Conditional or Unconditional Branch instruction.
bool isConditional() const
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
unsigned getNumSuccessors() const
BasicBlock * getSuccessor(unsigned i) const
bool isUnconditional() const
Value * getCondition() const
Analysis pass which computes BranchProbabilityInfo.
Analysis providing branch probability information.
void setEdgeProbability(const BasicBlock *Src, const SmallVectorImpl< BranchProbability > &Probs)
Set the raw probabilities for all edges from the given block.
BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
void copyEdgeProbabilities(BasicBlock *Src, BasicBlock *Dst)
Copy outgoing edge probabilities from Src to Dst.
static BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)
uint32_t getNumerator() const
BranchProbability getCompl() const
static void normalizeProbabilities(ProbabilityIter Begin, ProbabilityIter End)
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1353
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:428
static CastInst * CreateBitOrPointerCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:708
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:718
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:808
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:1972
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2616
static Constant * getCompare(unsigned short pred, Constant *C1, Constant *C2, bool OnlyIfReduced=false)
Return an ICmp or FCmp comparison operator constant expression.
Definition: Constants.cpp:2400
This is the shared class of boolean and integer constants.
Definition: Constants.h:78
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
Definition: Constants.h:199
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
Definition: Constants.h:172
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:833
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:193
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:888
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:840
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:132
static ConstantInt * getBool(LLVMContext &Context, bool V)
Definition: Constants.cpp:847
This class represents a range of values.
Definition: ConstantRange.h:47
ConstantRange add(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an addition of a value in this ran...
static ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
ConstantRange inverse() const
Return a new range that is the logical not of the current set.
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
This is an important base class in LLVM.
Definition: Constant.h:41
void removeDeadConstantUsers() const
If there are any dead constant users dangling off of this constant, remove them.
Definition: Constants.cpp:708
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
This represents the llvm.dbg.value instruction.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:202
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
iterator end()
Definition: DenseMap.h:84
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:220
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
void flush()
Apply all pending updates to available trees and flush all BasicBlocks awaiting deletion.
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:279
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:166
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:321
This class represents a freeze function that returns random concrete value if an operand is either a ...
const BasicBlock & getEntryBlock() const
Definition: Function.h:740
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.cpp:644
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:652
This instruction compares its operands according to the predicate given to the constructor.
Indirect Branch Instruction.
void removeFromParent()
This method unlinks 'this' from the containing basic block, but does not delete it.
Definition: Instruction.cpp:78
unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction.
Definition: Instruction.cpp:88
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:358
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:70
void setAAMetadata(const AAMDNodes &N)
Sets the AA metadata on this instruction from the AAMDNodes structure.
Definition: Metadata.cpp:1513
bool isAtomic() const LLVM_READONLY
Return true if this instruction has an AtomicOrdering of unordered or higher.
const BasicBlock * getParent() const
Definition: Instruction.h:90
BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1455
bool isExceptionalTerminator() const
Definition: Instruction.h:178
AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
Definition: Metadata.cpp:1499
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:168
SymbolTableList< Instruction >::iterator insertInto(BasicBlock *ParentBB, SymbolTableList< Instruction >::iterator It)
Inserts an unlinked instruction into ParentBB at position It and returns the iterator of the inserted...
Definition: Instruction.cpp:98
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:82
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:355
void setSuccessor(unsigned Idx, BasicBlock *BB)
Update the specified successor to point at the provided block.
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:47
bool simplifyPartiallyRedundantLoad(LoadInst *LI)
simplifyPartiallyRedundantLoad - If LoadI is an obviously partially redundant load instruction,...
bool processBranchOnXOR(BinaryOperator *BO)
processBranchOnXOR - We have an otherwise unthreadable conditional branch on a xor instruction in the...
bool processGuards(BasicBlock *BB)
Try to propagate a guard from the current BB into one of its predecessors in case if another branch o...
DenseMap< Instruction *, Value * > cloneInstructions(BasicBlock::iterator BI, BasicBlock::iterator BE, BasicBlock *NewBB, BasicBlock *PredBB)
Clone instructions in range [BI, BE) to NewBB.
bool computeValueKnownInPredecessors(Value *V, BasicBlock *BB, jumpthreading::PredValueInfo &Result, jumpthreading::ConstantPreference Preference, Instruction *CxtI=nullptr)
void findLoopHeaders(Function &F)
findLoopHeaders - We do not want jump threading to turn proper loop structures into irreducible loops...
bool maybeMergeBasicBlockIntoOnlyPred(BasicBlock *BB)
Merge basic block BB into its sole predecessor if possible.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
bool runImpl(Function &F, FunctionAnalysisManager *FAM, TargetLibraryInfo *TLI, TargetTransformInfo *TTI, LazyValueInfo *LVI, AAResults *AA, std::unique_ptr< DomTreeUpdater > DTU, std::optional< BlockFrequencyInfo * > BFI, std::optional< BranchProbabilityInfo * > BPI)
bool processBranchOnPHI(PHINode *PN)
processBranchOnPHI - We have an otherwise unthreadable conditional branch on a PHI node (or freeze PH...
bool maybethreadThroughTwoBasicBlocks(BasicBlock *BB, Value *Cond)
Attempt to thread through two successive basic blocks.
void unfoldSelectInstr(BasicBlock *Pred, BasicBlock *BB, SelectInst *SI, PHINode *SIUse, unsigned Idx)
DomTreeUpdater * getDomTreeUpdater() const
Constant * evaluateOnPredecessorEdge(BasicBlock *BB, BasicBlock *PredPredBB, Value *cond)
bool processThreadableEdges(Value *Cond, BasicBlock *BB, jumpthreading::ConstantPreference Preference, Instruction *CxtI=nullptr)
bool computeValueKnownInPredecessorsImpl(Value *V, BasicBlock *BB, jumpthreading::PredValueInfo &Result, jumpthreading::ConstantPreference Preference, DenseSet< Value * > &RecursionSet, Instruction *CxtI=nullptr)
computeValueKnownInPredecessors - Given a basic block BB and a value V, see if we can infer that the ...
bool processBlock(BasicBlock *BB)
processBlock - If there are any predecessors whose control can be threaded through to a successor,...
bool processImpliedCondition(BasicBlock *BB)
bool duplicateCondBranchOnPHIIntoPred(BasicBlock *BB, const SmallVectorImpl< BasicBlock * > &PredBBs)
duplicateCondBranchOnPHIIntoPred - PredBB contains an unconditional branch to BB which contains an i1...
void updateSSA(BasicBlock *BB, BasicBlock *NewBB, DenseMap< Instruction *, Value * > &ValueMapping)
Update the SSA form.
void threadThroughTwoBasicBlocks(BasicBlock *PredPredBB, BasicBlock *PredBB, BasicBlock *BB, BasicBlock *SuccBB)
bool tryThreadEdge(BasicBlock *BB, const SmallVectorImpl< BasicBlock * > &PredBBs, BasicBlock *SuccBB)
tryThreadEdge - Thread an edge if it's safe and profitable to do so.
bool tryToUnfoldSelect(CmpInst *CondCmp, BasicBlock *BB)
tryToUnfoldSelect - Look for blocks of the form bb1: a = select br bb2
bool tryToUnfoldSelectInCurrBB(BasicBlock *BB)
tryToUnfoldSelectInCurrBB - Look for PHI/Select or PHI/CMP/Select in the same BB in the form bb: p = ...
void threadEdge(BasicBlock *BB, const SmallVectorImpl< BasicBlock * > &PredBBs, BasicBlock *SuccBB)
threadEdge - We have decided that it is safe and profitable to factor the blocks in PredBBs to one pr...
bool threadGuard(BasicBlock *BB, IntrinsicInst *Guard, BranchInst *BI)
Try to propagate the guard from BB which is the lower block of a diamond to one of its branches,...
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Analysis to compute lazy value information.
This pass computes, caches, and vends lazy value constraint information.
Definition: LazyValueInfo.h:31
void eraseBlock(BasicBlock *BB)
Inform the analysis cache that we have erased a block.
void threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, BasicBlock *NewSucc)
Inform the analysis cache that we have threaded an edge from PredBB to OldSucc to be from PredBB to N...
Tristate
This is used to return true/false/dunno results.
Definition: LazyValueInfo.h:60
Constant * getConstantOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB, Instruction *CxtI=nullptr)
Determine whether the specified value is known to be a constant on the specified edge.
ConstantRange getConstantRangeOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB, Instruction *CxtI=nullptr)
Return the ConstantRage constraint that is known to hold for the specified value on the specified edg...
Tristate getPredicateOnEdge(unsigned Pred, Value *V, Constant *C, BasicBlock *FromBB, BasicBlock *ToBB, Instruction *CxtI=nullptr)
Determine whether the specified value comparison with a constant is known to be true or false on the ...
Tristate getPredicateAt(unsigned Pred, Value *V, Constant *C, Instruction *CxtI, bool UseBlockValue)
Determine whether the specified value comparison with a constant is known to be true or false at the ...
Constant * getConstant(Value *V, Instruction *CxtI)
Determine whether the specified value is known to be a constant at the specified instruction.
An instruction for reading from memory.
Definition: Instructions.h:177
AtomicOrdering getOrdering() const
Returns the ordering constraint of this load instruction.
Definition: Instructions.h:229
bool isUnordered() const
Definition: Instructions.h:258
SyncScope::ID getSyncScopeID() const
Returns the synchronization scope ID of this load instruction.
Definition: Instructions.h:239
Align getAlign() const
Return the alignment of the access that is being performed.
Definition: Instructions.h:220
static LocationSize precise(uint64_t Value)
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight)
Return metadata containing two branch weights.
Definition: MDBuilder.cpp:37
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:37
iterator end()
Definition: MapVector.h:72
iterator begin()
Definition: MapVector.h:70
Representation for a specific memory location.
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:175
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:398
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
void setIncomingValue(unsigned i, Value *V)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1758
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
void preserve()
Mark an analysis as preserved.
Definition: PassManager.h:173
Helper class for SSA formation on a set of values defined in multiple blocks.
Definition: SSAUpdater.h:39
void RewriteUse(Use &U)
Rewrite a use of the symbolic value.
Definition: SSAUpdater.cpp:187
void Initialize(Type *Ty, StringRef Name)
Reset this object to get ready for a new set of SSA updates with type 'Ty'.
Definition: SSAUpdater.cpp:53
void UpdateDebugValues(Instruction *I)
Rewrite debug value intrinsics to conform to a new SSA form.
Definition: SSAUpdater.cpp:199
void AddAvailableValue(BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value.
Definition: SSAUpdater.cpp:70
This class represents the LLVM 'select' instruction.
size_type size() const
Definition: SmallPtrSet.h:93
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:383
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:365
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:450
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:135
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:177
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:708
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:941
iterator erase(const_iterator CI)
Definition: SmallVector.h:741
void resize(size_type N)
Definition: SmallVector.h:642
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
Multiway switch.
Analysis pass providing the TargetTransformInfo.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
bool hasBranchDivergence() const
Return true if branch divergence exists.
@ TCK_SizeAndLatency
The weighted sum of size and latency.
@ TCC_Free
Expected to fold away in lowering.
InstructionCost getInstructionCost(const User *U, ArrayRef< const Value * > Operands, TargetCostKind CostKind) const
Estimate the cost of a given IR user when lowered.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:267
static IntegerType * getInt1Ty(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:231
'undef' values are things that do not have specified contents.
Definition: Constants.h:1371
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1739
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
void setOperand(unsigned i, Value *Val)
Definition: User.h:174
Value * getOperand(unsigned i) const
Definition: User.h:169
See the file comment.
Definition: ValueMap.h:84
iterator find(const KeyT &Val)
Definition: ValueMap.h:155
iterator end()
Definition: ValueMap.h:135
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
const Value * DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) const
Translate PHI node to its predecessor from the given basic block.
Definition: Value.cpp:986
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition: Value.h:434
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:532
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:685
bool use_empty() const
Definition: Value.h:344
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:994
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:308
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:381
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
self_iterator getIterator()
Definition: ilist_node.h:82
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition: ilist_node.h:289
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
Definition: Function.cpp:979
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
Definition: PatternMatch.h:979
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:144
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:147
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
Definition: PatternMatch.h:89
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:76
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition: PatternMatch.h:218
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:445
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool RemoveRedundantDbgInstrs(BasicBlock *BB)
Try to remove redundant dbg.value instructions from given basic block.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1782
bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions=false, const TargetLibraryInfo *TLI=nullptr, DomTreeUpdater *DTU=nullptr)
If a terminator instruction is predicated on a constant value, convert it into an unconditional branc...
Definition: Local.cpp:126
unsigned replaceNonLocalUsesWith(Instruction *From, Value *To)
Definition: Local.cpp:2854
auto successors(const MachineBasicBlock *BB)
void SplitLandingPadPredecessors(BasicBlock *OrigBB, ArrayRef< BasicBlock * > Preds, const char *Suffix, const char *Suffix2, SmallVectorImpl< BasicBlock * > &NewBBs, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, bool PreserveLCSSA=false)
This method transforms the landing pad, OrigBB, by introducing two new basic blocks into the function...
Value * FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan=DefMaxInstsToScan, AAResults *AA=nullptr, bool *IsLoadCSE=nullptr, unsigned *NumScanedInst=nullptr)
Scan backwards to see if we have the value of the given load available locally within a small number ...
Definition: Loads.cpp:428
bool SimplifyInstructionsInBlock(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr)
Scan the specified basic block and try to simplify any instructions in it and recursively delete dead...
Definition: Local.cpp:725
void DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, bool KeepOneInputPHIs=false)
Delete the specified block, which must have no predecessors.
BasicBlock * DuplicateInstructionsInSplitBetween(BasicBlock *BB, BasicBlock *PredBB, Instruction *StopAt, ValueToValueMapTy &ValueMapping, DomTreeUpdater &DTU)
Split edge between BB and PredBB and duplicate all non-Phi instructions from BB between its beginning...
Value * findAvailablePtrLoadStore(const MemoryLocation &Loc, Type *AccessTy, bool AtLeastAtomic, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan, AAResults *AA, bool *IsLoadCSE, unsigned *NumScanedInst)
Scan backwards to see if we have the value of the given pointer available locally within a small numb...
Definition: Loads.cpp:558
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:112
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1789
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition: Local.cpp:398
bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
Definition: GuardUtils.cpp:18
bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB, DomTreeUpdater *DTU=nullptr)
BB is known to contain an unconditional branch, and contains no instructions other than PHI nodes,...
Definition: Local.cpp:1070
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:495
bool hasValidBranchWeightMD(const Instruction &I)
Checks if an instructions has valid Branch Weight Metadata.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:109
void findDbgValues(SmallVectorImpl< DbgValueInst * > &DbgValues, Value *V)
Finds the llvm.dbg.value intrinsics describing a value.
Definition: DebugInfo.cpp:67
void cloneNoAliasScopes(ArrayRef< MDNode * > NoAliasDeclScopes, DenseMap< MDNode *, MDNode * > &ClonedScopes, StringRef Ext, LLVMContext &Context)
Duplicate the specified list of noalias decl scopes.
cl::opt< unsigned > DefMaxInstsToScan
The default number of maximum instructions to scan in the block, used by FindAvailableLoadedValue().
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
void combineMetadataForCSE(Instruction *K, const Instruction *J, bool DoesKMove)
Combine the metadata of two instructions so that K can replace J.
Definition: Local.cpp:2737
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, bool PreserveLCSSA=false)
This method introduces at least one new basic block into the function and moves some of the predecess...
auto remove_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::remove_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1821
void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, DomTreeUpdater *DTU=nullptr)
BB is a block with one predecessor and its predecessor is known to have one successor (BB!...
Definition: Local.cpp:765
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:2001
Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q, OptimizationRemarkEmitter *ORE=nullptr)
See if we can compute a simplified version of this instruction.
Value * simplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a CmpInst, fold the result or return null.
void adaptNoAliasScopes(llvm::Instruction *I, const DenseMap< MDNode *, MDNode * > &ClonedScopes, LLVMContext &Context)
Adapt the metadata for the specified instruction according to the provided mapping.
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if the instruction does not have any effects besides calculating the result and does not ...
bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
bool extractBranchWeights(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)
Extract branch weights from MD_prof metadata.
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition: STLExtras.h:2076
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1939
bool pred_empty(const BasicBlock *BB)
Definition: CFG.h:118
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
Definition: STLExtras.h:1690
void identifyNoAliasScopesToClone(ArrayRef< BasicBlock * > BBs, SmallVectorImpl< MDNode * > &NoAliasDeclScopes)
Find the 'llvm.experimental.noalias.scope.decl' intrinsics in the specified basic blocks and extract ...
BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="")
Split the edge connecting the specified blocks, and return the newly created basic block between From...
Instruction * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights, DominatorTree *DT, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
unsigned pred_size(const MachineBasicBlock *BB)
void FindFunctionBackedges(const Function &F, SmallVectorImpl< std::pair< const BasicBlock *, const BasicBlock * > > &Result)
Analyze the specified function to find all of the loop backedges in the function and return them.
Definition: CFG.cpp:34
std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:651
Function object to check whether the second component of a container supported by std::get (like std:...
Definition: STLExtras.h:1532