Movatterモバイル変換


[0]ホーム

URL:


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

Generated on Fri Jul 18 2025 15:56:05 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp