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