Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
SimplifyCFG.cpp
Go to the documentation of this file.
1//===- SimplifyCFG.cpp - Code to perform CFG simplification ---------------===//
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// Peephole optimize the CFG.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/ADT/APInt.h"
14#include "llvm/ADT/ArrayRef.h"
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/MapVector.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/Sequence.h"
19#include "llvm/ADT/SetOperations.h"
20#include "llvm/ADT/SetVector.h"
21#include "llvm/ADT/SmallPtrSet.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/Statistic.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/Analysis/AssumptionCache.h"
26#include "llvm/Analysis/CaptureTracking.h"
27#include "llvm/Analysis/ConstantFolding.h"
28#include "llvm/Analysis/DomTreeUpdater.h"
29#include "llvm/Analysis/GuardUtils.h"
30#include "llvm/Analysis/InstructionSimplify.h"
31#include "llvm/Analysis/Loads.h"
32#include "llvm/Analysis/MemorySSA.h"
33#include "llvm/Analysis/MemorySSAUpdater.h"
34#include "llvm/Analysis/TargetTransformInfo.h"
35#include "llvm/Analysis/ValueTracking.h"
36#include "llvm/IR/Attributes.h"
37#include "llvm/IR/BasicBlock.h"
38#include "llvm/IR/CFG.h"
39#include "llvm/IR/Constant.h"
40#include "llvm/IR/ConstantRange.h"
41#include "llvm/IR/Constants.h"
42#include "llvm/IR/DataLayout.h"
43#include "llvm/IR/DebugInfo.h"
44#include "llvm/IR/DerivedTypes.h"
45#include "llvm/IR/Function.h"
46#include "llvm/IR/GlobalValue.h"
47#include "llvm/IR/GlobalVariable.h"
48#include "llvm/IR/IRBuilder.h"
49#include "llvm/IR/InstrTypes.h"
50#include "llvm/IR/Instruction.h"
51#include "llvm/IR/Instructions.h"
52#include "llvm/IR/IntrinsicInst.h"
53#include "llvm/IR/LLVMContext.h"
54#include "llvm/IR/MDBuilder.h"
55#include "llvm/IR/MemoryModelRelaxationAnnotations.h"
56#include "llvm/IR/Metadata.h"
57#include "llvm/IR/Module.h"
58#include "llvm/IR/NoFolder.h"
59#include "llvm/IR/Operator.h"
60#include "llvm/IR/PatternMatch.h"
61#include "llvm/IR/ProfDataUtils.h"
62#include "llvm/IR/Type.h"
63#include "llvm/IR/Use.h"
64#include "llvm/IR/User.h"
65#include "llvm/IR/Value.h"
66#include "llvm/IR/ValueHandle.h"
67#include "llvm/Support/BranchProbability.h"
68#include "llvm/Support/Casting.h"
69#include "llvm/Support/CommandLine.h"
70#include "llvm/Support/Debug.h"
71#include "llvm/Support/ErrorHandling.h"
72#include "llvm/Support/KnownBits.h"
73#include "llvm/Support/MathExtras.h"
74#include "llvm/Support/raw_ostream.h"
75#include "llvm/Transforms/Utils/BasicBlockUtils.h"
76#include "llvm/Transforms/Utils/Local.h"
77#include "llvm/Transforms/Utils/ValueMapper.h"
78#include <algorithm>
79#include <cassert>
80#include <climits>
81#include <cstddef>
82#include <cstdint>
83#include <iterator>
84#include <map>
85#include <optional>
86#include <set>
87#include <tuple>
88#include <utility>
89#include <vector>
90
91using namespacellvm;
92using namespacePatternMatch;
93
94#define DEBUG_TYPE "simplifycfg"
95
96cl::opt<bool>llvm::RequireAndPreserveDomTree(
97"simplifycfg-require-and-preserve-domtree",cl::Hidden,
98
99cl::desc(
100"Temporary development switch used to gradually uplift SimplifyCFG "
101"into preserving DomTree,"));
102
103// Chosen as 2 so as to be cheap, but still to have enough power to fold
104// a select, so the "clamp" idiom (of a min followed by a max) will be caught.
105// To catch this, we need to fold a compare and a select, hence '2' being the
106// minimum reasonable default.
107staticcl::opt<unsigned>PHINodeFoldingThreshold(
108"phi-node-folding-threshold",cl::Hidden,cl::init(2),
109cl::desc(
110"Control the amount of phi node folding to perform (default = 2)"));
111
112staticcl::opt<unsigned>TwoEntryPHINodeFoldingThreshold(
113"two-entry-phi-node-folding-threshold",cl::Hidden,cl::init(4),
114cl::desc("Control the maximal total instruction cost that we are willing "
115"to speculatively execute to fold a 2-entry PHI node into a "
116"select (default = 4)"));
117
118staticcl::opt<bool>
119HoistCommon("simplifycfg-hoist-common",cl::Hidden,cl::init(true),
120cl::desc("Hoist common instructions up to the parent block"));
121
122staticcl::opt<bool>HoistLoadsStoresWithCondFaulting(
123"simplifycfg-hoist-loads-stores-with-cond-faulting",cl::Hidden,
124cl::init(true),
125cl::desc("Hoist loads/stores if the target supports "
126"conditional faulting"));
127
128staticcl::opt<unsigned>HoistLoadsStoresWithCondFaultingThreshold(
129"hoist-loads-stores-with-cond-faulting-threshold",cl::Hidden,cl::init(6),
130cl::desc("Control the maximal conditional load/store that we are willing "
131"to speculatively execute to eliminate conditional branch "
132"(default = 6)"));
133
134staticcl::opt<unsigned>
135HoistCommonSkipLimit("simplifycfg-hoist-common-skip-limit",cl::Hidden,
136cl::init(20),
137cl::desc("Allow reordering across at most this many "
138"instructions when hoisting"));
139
140staticcl::opt<bool>
141SinkCommon("simplifycfg-sink-common",cl::Hidden,cl::init(true),
142cl::desc("Sink common instructions down to the end block"));
143
144staticcl::opt<bool>HoistCondStores(
145"simplifycfg-hoist-cond-stores",cl::Hidden,cl::init(true),
146cl::desc("Hoist conditional stores if an unconditional store precedes"));
147
148staticcl::opt<bool>MergeCondStores(
149"simplifycfg-merge-cond-stores",cl::Hidden,cl::init(true),
150cl::desc("Hoist conditional stores even if an unconditional store does not "
151"precede - hoist multiple conditional stores into a single "
152"predicated store"));
153
154staticcl::opt<bool>MergeCondStoresAggressively(
155"simplifycfg-merge-cond-stores-aggressively",cl::Hidden,cl::init(false),
156cl::desc("When merging conditional stores, do so even if the resultant "
157"basic blocks are unlikely to be if-converted as a result"));
158
159staticcl::opt<bool>SpeculateOneExpensiveInst(
160"speculate-one-expensive-inst",cl::Hidden,cl::init(true),
161cl::desc("Allow exactly one expensive instruction to be speculatively "
162"executed"));
163
164staticcl::opt<unsigned>MaxSpeculationDepth(
165"max-speculation-depth",cl::Hidden,cl::init(10),
166cl::desc("Limit maximum recursion depth when calculating costs of "
167"speculatively executed instructions"));
168
169staticcl::opt<int>
170MaxSmallBlockSize("simplifycfg-max-small-block-size",cl::Hidden,
171cl::init(10),
172cl::desc("Max size of a block which is still considered "
173"small enough to thread through"));
174
175// Two is chosen to allow one negation and a logical combine.
176staticcl::opt<unsigned>
177BranchFoldThreshold("simplifycfg-branch-fold-threshold",cl::Hidden,
178cl::init(2),
179cl::desc("Maximum cost of combining conditions when "
180"folding branches"));
181
182staticcl::opt<unsigned>BranchFoldToCommonDestVectorMultiplier(
183"simplifycfg-branch-fold-common-dest-vector-multiplier",cl::Hidden,
184cl::init(2),
185cl::desc("Multiplier to apply to threshold when determining whether or not "
186"to fold branch to common destination when vector operations are "
187"present"));
188
189staticcl::opt<bool>EnableMergeCompatibleInvokes(
190"simplifycfg-merge-compatible-invokes",cl::Hidden,cl::init(true),
191cl::desc("Allow SimplifyCFG to merge invokes together when appropriate"));
192
193staticcl::opt<unsigned>MaxSwitchCasesPerResult(
194"max-switch-cases-per-result",cl::Hidden,cl::init(16),
195cl::desc("Limit cases to analyze when converting a switch to select"));
196
197STATISTIC(NumBitMaps,"Number of switch instructions turned into bitmaps");
198STATISTIC(NumLinearMaps,
199"Number of switch instructions turned into linear mapping");
200STATISTIC(NumLookupTables,
201"Number of switch instructions turned into lookup tables");
202STATISTIC(
203 NumLookupTablesHoles,
204"Number of switch instructions turned into lookup tables (holes checked)");
205STATISTIC(NumTableCmpReuses,"Number of reused switch table lookup compares");
206STATISTIC(NumFoldValueComparisonIntoPredecessors,
207"Number of value comparisons folded into predecessor basic blocks");
208STATISTIC(NumFoldBranchToCommonDest,
209"Number of branches folded into predecessor basic block");
210STATISTIC(
211 NumHoistCommonCode,
212"Number of common instruction 'blocks' hoisted up to the begin block");
213STATISTIC(NumHoistCommonInstrs,
214"Number of common instructions hoisted up to the begin block");
215STATISTIC(NumSinkCommonCode,
216"Number of common instruction 'blocks' sunk down to the end block");
217STATISTIC(NumSinkCommonInstrs,
218"Number of common instructions sunk down to the end block");
219STATISTIC(NumSpeculations,"Number of speculative executed instructions");
220STATISTIC(NumInvokes,
221"Number of invokes with empty resume blocks simplified into calls");
222STATISTIC(NumInvokesMerged,"Number of invokes that were merged together");
223STATISTIC(NumInvokeSetsFormed,"Number of invoke sets that were formed");
224
225namespace{
226
227// The first field contains the value that the switch produces when a certain
228// case group is selected, and the second field is a vector containing the
229// cases composing the case group.
230usingSwitchCaseResultVectorTy =
231SmallVector<std::pair<Constant *, SmallVector<ConstantInt *, 4>>, 2>;
232
233// The first field contains the phi node that generates a result of the switch
234// and the second field contains the value generated for a certain case in the
235// switch for that PHI.
236usingSwitchCaseResultsTy =SmallVector<std::pair<PHINode *, Constant *>, 4>;
237
238/// ValueEqualityComparisonCase - Represents a case of a switch.
239structValueEqualityComparisonCase {
240ConstantInt *Value;
241BasicBlock *Dest;
242
243 ValueEqualityComparisonCase(ConstantInt *Value,BasicBlock *Dest)
244 :Value(Value), Dest(Dest) {}
245
246booloperator<(ValueEqualityComparisonCase RHS) const{
247// Comparing pointers is ok as we only rely on the order for uniquing.
248returnValue <RHS.Value;
249 }
250
251booloperator==(BasicBlock *RHSDest) const{return Dest == RHSDest; }
252};
253
254classSimplifyCFGOpt {
255constTargetTransformInfo &TTI;
256DomTreeUpdater *DTU;
257constDataLayout &DL;
258ArrayRef<WeakVH> LoopHeaders;
259constSimplifyCFGOptions &Options;
260bool Resimplify;
261
262Value *isValueEqualityComparison(Instruction *TI);
263BasicBlock *getValueEqualityComparisonCases(
264Instruction *TI, std::vector<ValueEqualityComparisonCase> &Cases);
265bool simplifyEqualityComparisonWithOnlyPredecessor(Instruction *TI,
266BasicBlock *Pred,
267IRBuilder<> &Builder);
268bool performValueComparisonIntoPredecessorFolding(Instruction *TI,Value *&CV,
269Instruction *PTI,
270IRBuilder<> &Builder);
271bool foldValueComparisonIntoPredecessors(Instruction *TI,
272IRBuilder<> &Builder);
273
274bool simplifyResume(ResumeInst *RI,IRBuilder<> &Builder);
275bool simplifySingleResume(ResumeInst *RI);
276bool simplifyCommonResume(ResumeInst *RI);
277bool simplifyCleanupReturn(CleanupReturnInst *RI);
278bool simplifyUnreachable(UnreachableInst *UI);
279bool simplifySwitch(SwitchInst *SI,IRBuilder<> &Builder);
280bool simplifyDuplicateSwitchArms(SwitchInst *SI,DomTreeUpdater *DTU);
281bool simplifyIndirectBr(IndirectBrInst *IBI);
282bool simplifyBranch(BranchInst *Branch,IRBuilder<> &Builder);
283bool simplifyUncondBranch(BranchInst *BI,IRBuilder<> &Builder);
284bool simplifyCondBranch(BranchInst *BI,IRBuilder<> &Builder);
285
286bool tryToSimplifyUncondBranchWithICmpInIt(ICmpInst *ICI,
287IRBuilder<> &Builder);
288
289bool hoistCommonCodeFromSuccessors(Instruction *TI,bool AllInstsEqOnly);
290bool hoistSuccIdenticalTerminatorToSwitchOrIf(
291Instruction *TI,Instruction *I1,
292SmallVectorImpl<Instruction *> &OtherSuccTIs);
293bool speculativelyExecuteBB(BranchInst *BI,BasicBlock *ThenBB);
294bool simplifyTerminatorOnSelect(Instruction *OldTerm,Value *Cond,
295BasicBlock *TrueBB,BasicBlock *FalseBB,
296uint32_t TrueWeight,uint32_t FalseWeight);
297bool simplifyBranchOnICmpChain(BranchInst *BI,IRBuilder<> &Builder,
298constDataLayout &DL);
299bool simplifySwitchOnSelect(SwitchInst *SI,SelectInst *Select);
300bool simplifyIndirectBrOnSelect(IndirectBrInst *IBI,SelectInst *SI);
301bool turnSwitchRangeIntoICmp(SwitchInst *SI,IRBuilder<> &Builder);
302
303public:
304 SimplifyCFGOpt(constTargetTransformInfo &TTI,DomTreeUpdater *DTU,
305constDataLayout &DL,ArrayRef<WeakVH> LoopHeaders,
306constSimplifyCFGOptions &Opts)
307 :TTI(TTI), DTU(DTU),DL(DL), LoopHeaders(LoopHeaders), Options(Opts) {
308assert((!DTU || !DTU->hasPostDomTree()) &&
309"SimplifyCFG is not yet capable of maintaining validity of a "
310"PostDomTree, so don't ask for it.");
311 }
312
313bool simplifyOnce(BasicBlock *BB);
314boolrun(BasicBlock *BB);
315
316// Helper to set Resimplify and return change indication.
317bool requestResimplify() {
318 Resimplify =true;
319returntrue;
320 }
321};
322
323}// end anonymous namespace
324
325/// Return true if all the PHI nodes in the basic block \p BB
326/// receive compatible (identical) incoming values when coming from
327/// all of the predecessor blocks that are specified in \p IncomingBlocks.
328///
329/// Note that if the values aren't exactly identical, but \p EquivalenceSet
330/// is provided, and *both* of the values are present in the set,
331/// then they are considered equal.
332staticboolincomingValuesAreCompatible(
333BasicBlock *BB,ArrayRef<BasicBlock *> IncomingBlocks,
334SmallPtrSetImpl<Value *> *EquivalenceSet =nullptr) {
335assert(IncomingBlocks.size() == 2 &&
336"Only for a pair of incoming blocks at the time!");
337
338// FIXME: it is okay if one of the incoming values is an `undef` value,
339// iff the other incoming value is guaranteed to be a non-poison value.
340// FIXME: it is okay if one of the incoming values is a `poison` value.
341returnall_of(BB->phis(), [IncomingBlocks, EquivalenceSet](PHINode &PN) {
342 Value *IV0 = PN.getIncomingValueForBlock(IncomingBlocks[0]);
343 Value *IV1 = PN.getIncomingValueForBlock(IncomingBlocks[1]);
344 if (IV0 == IV1)
345 return true;
346 if (EquivalenceSet && EquivalenceSet->contains(IV0) &&
347 EquivalenceSet->contains(IV1))
348 return true;
349 return false;
350 });
351}
352
353/// Return true if it is safe to merge these two
354/// terminator instructions together.
355staticbool
356safeToMergeTerminators(Instruction *SI1,Instruction *SI2,
357SmallSetVector<BasicBlock *, 4> *FailBlocks =nullptr) {
358if (SI1 == SI2)
359returnfalse;// Can't merge with self!
360
361// It is not safe to merge these two switch instructions if they have a common
362// successor, and if that successor has a PHI node, and if *that* PHI node has
363// conflicting incoming values from the two switch blocks.
364BasicBlock *SI1BB = SI1->getParent();
365BasicBlock *SI2BB = SI2->getParent();
366
367SmallPtrSet<BasicBlock *, 16> SI1Succs(succ_begin(SI1BB),succ_end(SI1BB));
368boolFail =false;
369for (BasicBlock *Succ :successors(SI2BB)) {
370if (!SI1Succs.count(Succ))
371continue;
372if (incomingValuesAreCompatible(Succ, {SI1BB, SI2BB}))
373continue;
374Fail =true;
375if (FailBlocks)
376 FailBlocks->insert(Succ);
377else
378break;
379 }
380
381return !Fail;
382}
383
384/// Update PHI nodes in Succ to indicate that there will now be entries in it
385/// from the 'NewPred' block. The values that will be flowing into the PHI nodes
386/// will be the same as those coming in from ExistPred, an existing predecessor
387/// of Succ.
388staticvoidaddPredecessorToBlock(BasicBlock *Succ,BasicBlock *NewPred,
389BasicBlock *ExistPred,
390MemorySSAUpdater *MSSAU =nullptr) {
391for (PHINode &PN : Succ->phis())
392 PN.addIncoming(PN.getIncomingValueForBlock(ExistPred), NewPred);
393if (MSSAU)
394if (auto *MPhi = MSSAU->getMemorySSA()->getMemoryAccess(Succ))
395 MPhi->addIncoming(MPhi->getIncomingValueForBlock(ExistPred), NewPred);
396}
397
398/// Compute an abstract "cost" of speculating the given instruction,
399/// which is assumed to be safe to speculate. TCC_Free means cheap,
400/// TCC_Basic means less cheap, and TCC_Expensive means prohibitively
401/// expensive.
402staticInstructionCostcomputeSpeculationCost(constUser *I,
403constTargetTransformInfo &TTI) {
404returnTTI.getInstructionCost(I,TargetTransformInfo::TCK_SizeAndLatency);
405}
406
407/// If we have a merge point of an "if condition" as accepted above,
408/// return true if the specified value dominates the block. We don't handle
409/// the true generality of domination here, just a special case which works
410/// well enough for us.
411///
412/// If AggressiveInsts is non-null, and if V does not dominate BB, we check to
413/// see if V (which must be an instruction) and its recursive operands
414/// that do not dominate BB have a combined cost lower than Budget and
415/// are non-trapping. If both are true, the instruction is inserted into the
416/// set and true is returned.
417///
418/// The cost for most non-trapping instructions is defined as 1 except for
419/// Select whose cost is 2.
420///
421/// After this function returns, Cost is increased by the cost of
422/// V plus its non-dominating operands. If that cost is greater than
423/// Budget, false is returned and Cost is undefined.
424staticbooldominatesMergePoint(Value *V,BasicBlock *BB,Instruction *InsertPt,
425SmallPtrSetImpl<Instruction *> &AggressiveInsts,
426InstructionCost &Cost,InstructionCost Budget,
427constTargetTransformInfo &TTI,
428AssumptionCache *AC,unsignedDepth = 0) {
429// It is possible to hit a zero-cost cycle (phi/gep instructions for example),
430// so limit the recursion depth.
431// TODO: While this recursion limit does prevent pathological behavior, it
432// would be better to track visited instructions to avoid cycles.
433if (Depth ==MaxSpeculationDepth)
434returnfalse;
435
436Instruction *I = dyn_cast<Instruction>(V);
437if (!I) {
438// Non-instructions dominate all instructions and can be executed
439// unconditionally.
440returntrue;
441 }
442BasicBlock *PBB =I->getParent();
443
444// We don't want to allow weird loops that might have the "if condition" in
445// the bottom of this block.
446if (PBB == BB)
447returnfalse;
448
449// If this instruction is defined in a block that contains an unconditional
450// branch to BB, then it must be in the 'conditional' part of the "if
451// statement". If not, it definitely dominates the region.
452BranchInst *BI = dyn_cast<BranchInst>(PBB->getTerminator());
453if (!BI || BI->isConditional() || BI->getSuccessor(0) != BB)
454returntrue;
455
456// If we have seen this instruction before, don't count it again.
457if (AggressiveInsts.count(I))
458returntrue;
459
460// Okay, it looks like the instruction IS in the "condition". Check to
461// see if it's a cheap instruction to unconditionally compute, and if it
462// only uses stuff defined outside of the condition. If so, hoist it out.
463if (!isSafeToSpeculativelyExecute(I, InsertPt, AC))
464returnfalse;
465
466Cost +=computeSpeculationCost(I,TTI);
467
468// Allow exactly one instruction to be speculated regardless of its cost
469// (as long as it is safe to do so).
470// This is intended to flatten the CFG even if the instruction is a division
471// or other expensive operation. The speculation of an expensive instruction
472// is expected to be undone in CodeGenPrepare if the speculation has not
473// enabled further IR optimizations.
474if (Cost > Budget &&
475 (!SpeculateOneExpensiveInst || !AggressiveInsts.empty() ||Depth > 0 ||
476 !Cost.isValid()))
477returnfalse;
478
479// Okay, we can only really hoist these out if their operands do
480// not take us over the cost threshold.
481for (Use &Op :I->operands())
482if (!dominatesMergePoint(Op, BB, InsertPt, AggressiveInsts,Cost, Budget,
483TTI, AC,Depth + 1))
484returnfalse;
485// Okay, it's safe to do this! Remember this instruction.
486 AggressiveInsts.insert(I);
487returntrue;
488}
489
490/// Extract ConstantInt from value, looking through IntToPtr
491/// and PointerNullValue. Return NULL if value is not a constant int.
492staticConstantInt *getConstantInt(Value *V,constDataLayout &DL) {
493// Normal constant int.
494ConstantInt *CI = dyn_cast<ConstantInt>(V);
495if (CI || !isa<Constant>(V) || !V->getType()->isPointerTy() ||
496DL.isNonIntegralPointerType(V->getType()))
497return CI;
498
499// This is some kind of pointer constant. Turn it into a pointer-sized
500// ConstantInt if possible.
501IntegerType *PtrTy = cast<IntegerType>(DL.getIntPtrType(V->getType()));
502
503// Null pointer means 0, see SelectionDAGBuilder::getValue(const Value*).
504if (isa<ConstantPointerNull>(V))
505return ConstantInt::get(PtrTy, 0);
506
507// IntToPtr const int.
508if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
509if (CE->getOpcode() == Instruction::IntToPtr)
510if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(0))) {
511// The constant is very likely to have the right type already.
512if (CI->getType() == PtrTy)
513return CI;
514else
515return cast<ConstantInt>(
516ConstantFoldIntegerCast(CI, PtrTy,/*isSigned=*/false,DL));
517 }
518returnnullptr;
519}
520
521namespace{
522
523/// Given a chain of or (||) or and (&&) comparison of a value against a
524/// constant, this will try to recover the information required for a switch
525/// structure.
526/// It will depth-first traverse the chain of comparison, seeking for patterns
527/// like %a == 12 or %a < 4 and combine them to produce a set of integer
528/// representing the different cases for the switch.
529/// Note that if the chain is composed of '||' it will build the set of elements
530/// that matches the comparisons (i.e. any of this value validate the chain)
531/// while for a chain of '&&' it will build the set elements that make the test
532/// fail.
533structConstantComparesGatherer {
534constDataLayout &DL;
535
536 /// Value found for the switch comparison
537Value *CompValue =nullptr;
538
539 /// Extra clause to be checked before the switch
540Value *Extra =nullptr;
541
542 /// Set of integers to match in switch
543SmallVector<ConstantInt *, 8> Vals;
544
545 /// Number of comparisons matched in the and/or chain
546unsigned UsedICmps = 0;
547
548 /// Construct and compute the result for the comparison instruction Cond
549 ConstantComparesGatherer(Instruction *Cond,constDataLayout &DL) :DL(DL) {
550 gather(Cond);
551 }
552
553 ConstantComparesGatherer(const ConstantComparesGatherer &) =delete;
554 ConstantComparesGatherer &
555 operator=(const ConstantComparesGatherer &) =delete;
556
557private:
558 /// Try to set the current value used for the comparison, it succeeds only if
559 /// it wasn't set before or if the new value is the same as the old one
560bool setValueOnce(Value *NewVal) {
561if (CompValue && CompValue != NewVal)
562returnfalse;
563 CompValue = NewVal;
564return (CompValue !=nullptr);
565 }
566
567 /// Try to match Instruction "I" as a comparison against a constant and
568 /// populates the array Vals with the set of values that match (or do not
569 /// match depending on isEQ).
570 /// Return false on failure. On success, the Value the comparison matched
571 /// against is placed in CompValue.
572 /// If CompValue is already set, the function is expected to fail if a match
573 /// is found but the value compared to is different.
574bool matchInstruction(Instruction *I,bool isEQ) {
575// If this is an icmp against a constant, handle this as one of the cases.
576ICmpInst *ICI;
577ConstantInt *C;
578if (!((ICI = dyn_cast<ICmpInst>(I)) &&
579 (C =getConstantInt(I->getOperand(1), DL)))) {
580returnfalse;
581 }
582
583Value *RHSVal;
584constAPInt *RHSC;
585
586// Pattern match a special case
587// (x & ~2^z) == y --> x == y || x == y|2^z
588// This undoes a transformation done by instcombine to fuse 2 compares.
589if (ICI->getPredicate() == (isEQ ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE)) {
590// It's a little bit hard to see why the following transformations are
591// correct. Here is a CVC3 program to verify them for 64-bit values:
592
593/*
594 ONE : BITVECTOR(64) = BVZEROEXTEND(0bin1, 63);
595 x : BITVECTOR(64);
596 y : BITVECTOR(64);
597 z : BITVECTOR(64);
598 mask : BITVECTOR(64) = BVSHL(ONE, z);
599 QUERY( (y & ~mask = y) =>
600 ((x & ~mask = y) <=> (x = y OR x = (y | mask)))
601 );
602 QUERY( (y | mask = y) =>
603 ((x | mask = y) <=> (x = y OR x = (y & ~mask)))
604 );
605 */
606
607// Please note that each pattern must be a dual implication (<--> or
608// iff). One directional implication can create spurious matches. If the
609// implication is only one-way, an unsatisfiable condition on the left
610// side can imply a satisfiable condition on the right side. Dual
611// implication ensures that satisfiable conditions are transformed to
612// other satisfiable conditions and unsatisfiable conditions are
613// transformed to other unsatisfiable conditions.
614
615// Here is a concrete example of a unsatisfiable condition on the left
616// implying a satisfiable condition on the right:
617//
618// mask = (1 << z)
619// (x & ~mask) == y --> (x == y || x == (y | mask))
620//
621// Substituting y = 3, z = 0 yields:
622// (x & -2) == 3 --> (x == 3 || x == 2)
623
624// Pattern match a special case:
625/*
626 QUERY( (y & ~mask = y) =>
627 ((x & ~mask = y) <=> (x = y OR x = (y | mask)))
628 );
629 */
630if (match(ICI->getOperand(0),
631m_And(m_Value(RHSVal),m_APInt(RHSC)))) {
632APIntMask = ~*RHSC;
633if (Mask.isPowerOf2() && (C->getValue() & ~Mask) ==C->getValue()) {
634// If we already have a value for the switch, it has to match!
635if (!setValueOnce(RHSVal))
636returnfalse;
637
638 Vals.push_back(C);
639 Vals.push_back(
640 ConstantInt::get(C->getContext(),
641C->getValue() | Mask));
642 UsedICmps++;
643returntrue;
644 }
645 }
646
647// Pattern match a special case:
648/*
649 QUERY( (y | mask = y) =>
650 ((x | mask = y) <=> (x = y OR x = (y & ~mask)))
651 );
652 */
653if (match(ICI->getOperand(0),
654m_Or(m_Value(RHSVal),m_APInt(RHSC)))) {
655APIntMask = *RHSC;
656if (Mask.isPowerOf2() && (C->getValue() | Mask) ==C->getValue()) {
657// If we already have a value for the switch, it has to match!
658if (!setValueOnce(RHSVal))
659returnfalse;
660
661 Vals.push_back(C);
662 Vals.push_back(ConstantInt::get(C->getContext(),
663C->getValue() & ~Mask));
664 UsedICmps++;
665returntrue;
666 }
667 }
668
669// If we already have a value for the switch, it has to match!
670if (!setValueOnce(ICI->getOperand(0)))
671returnfalse;
672
673 UsedICmps++;
674 Vals.push_back(C);
675return ICI->getOperand(0);
676 }
677
678// If we have "x ult 3", for example, then we can add 0,1,2 to the set.
679ConstantRange Span =
680ConstantRange::makeExactICmpRegion(ICI->getPredicate(),C->getValue());
681
682// Shift the range if the compare is fed by an add. This is the range
683// compare idiom as emitted by instcombine.
684Value *CandidateVal =I->getOperand(0);
685if (match(I->getOperand(0),m_Add(m_Value(RHSVal),m_APInt(RHSC)))) {
686 Span = Span.subtract(*RHSC);
687 CandidateVal = RHSVal;
688 }
689
690// If this is an and/!= check, then we are looking to build the set of
691// value that *don't* pass the and chain. I.e. to turn "x ugt 2" into
692// x != 0 && x != 1.
693if (!isEQ)
694 Span = Span.inverse();
695
696// If there are a ton of values, we don't want to make a ginormous switch.
697if (Span.isSizeLargerThan(8) || Span.isEmptySet()) {
698returnfalse;
699 }
700
701// If we already have a value for the switch, it has to match!
702if (!setValueOnce(CandidateVal))
703returnfalse;
704
705// Add all values from the range to the set
706for (APInt Tmp = Span.getLower(); Tmp != Span.getUpper(); ++Tmp)
707 Vals.push_back(ConstantInt::get(I->getContext(), Tmp));
708
709 UsedICmps++;
710returntrue;
711 }
712
713 /// Given a potentially 'or'd or 'and'd together collection of icmp
714 /// eq/ne/lt/gt instructions that compare a value against a constant, extract
715 /// the value being compared, and stick the list constants into the Vals
716 /// vector.
717 /// One "Extra" case is allowed to differ from the other.
718void gather(Value *V) {
719bool isEQ =match(V,m_LogicalOr(m_Value(),m_Value()));
720
721// Keep a stack (SmallVector for efficiency) for depth-first traversal
722SmallVector<Value *, 8> DFT;
723SmallPtrSet<Value *, 8> Visited;
724
725// Initialize
726 Visited.insert(V);
727 DFT.push_back(V);
728
729while (!DFT.empty()) {
730V = DFT.pop_back_val();
731
732if (Instruction *I = dyn_cast<Instruction>(V)) {
733// If it is a || (or && depending on isEQ), process the operands.
734Value *Op0, *Op1;
735if (isEQ ?match(I,m_LogicalOr(m_Value(Op0),m_Value(Op1)))
736 :match(I,m_LogicalAnd(m_Value(Op0),m_Value(Op1)))) {
737if (Visited.insert(Op1).second)
738 DFT.push_back(Op1);
739if (Visited.insert(Op0).second)
740 DFT.push_back(Op0);
741
742continue;
743 }
744
745// Try to match the current instruction
746if (matchInstruction(I, isEQ))
747// Match succeed, continue the loop
748continue;
749 }
750
751// One element of the sequence of || (or &&) could not be match as a
752// comparison against the same value as the others.
753// We allow only one "Extra" case to be checked before the switch
754if (!Extra) {
755 Extra =V;
756continue;
757 }
758// Failed to parse a proper sequence, abort now
759 CompValue =nullptr;
760break;
761 }
762 }
763};
764
765}// end anonymous namespace
766
767staticvoideraseTerminatorAndDCECond(Instruction *TI,
768MemorySSAUpdater *MSSAU =nullptr) {
769Instruction *Cond =nullptr;
770if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
771Cond = dyn_cast<Instruction>(SI->getCondition());
772 }elseif (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
773if (BI->isConditional())
774Cond = dyn_cast<Instruction>(BI->getCondition());
775 }elseif (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(TI)) {
776Cond = dyn_cast<Instruction>(IBI->getAddress());
777 }
778
779 TI->eraseFromParent();
780if (Cond)
781RecursivelyDeleteTriviallyDeadInstructions(Cond,nullptr, MSSAU);
782}
783
784/// Return true if the specified terminator checks
785/// to see if a value is equal to constant integer value.
786Value *SimplifyCFGOpt::isValueEqualityComparison(Instruction *TI) {
787Value *CV =nullptr;
788if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
789// Do not permit merging of large switch instructions into their
790// predecessors unless there is only one predecessor.
791if (!SI->getParent()->hasNPredecessorsOrMore(128 /SI->getNumSuccessors()))
792 CV =SI->getCondition();
793 }elseif (BranchInst *BI = dyn_cast<BranchInst>(TI))
794if (BI->isConditional() && BI->getCondition()->hasOneUse())
795if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) {
796if (ICI->isEquality() &&getConstantInt(ICI->getOperand(1),DL))
797 CV = ICI->getOperand(0);
798 }
799
800// Unwrap any lossless ptrtoint cast.
801if (CV) {
802if (PtrToIntInst *PTII = dyn_cast<PtrToIntInst>(CV)) {
803Value *Ptr = PTII->getPointerOperand();
804if (PTII->getType() ==DL.getIntPtrType(Ptr->getType()))
805 CV =Ptr;
806 }
807 }
808return CV;
809}
810
811/// Given a value comparison instruction,
812/// decode all of the 'cases' that it represents and return the 'default' block.
813BasicBlock *SimplifyCFGOpt::getValueEqualityComparisonCases(
814Instruction *TI, std::vector<ValueEqualityComparisonCase> &Cases) {
815if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
816 Cases.reserve(SI->getNumCases());
817for (auto Case :SI->cases())
818 Cases.push_back(ValueEqualityComparisonCase(Case.getCaseValue(),
819 Case.getCaseSuccessor()));
820returnSI->getDefaultDest();
821 }
822
823BranchInst *BI = cast<BranchInst>(TI);
824ICmpInst *ICI = cast<ICmpInst>(BI->getCondition());
825BasicBlock *Succ = BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_NE);
826 Cases.push_back(ValueEqualityComparisonCase(
827getConstantInt(ICI->getOperand(1),DL), Succ));
828return BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_EQ);
829}
830
831/// Given a vector of bb/value pairs, remove any entries
832/// in the list that match the specified block.
833staticvoid
834eliminateBlockCases(BasicBlock *BB,
835 std::vector<ValueEqualityComparisonCase> &Cases) {
836llvm::erase(Cases, BB);
837}
838
839/// Return true if there are any keys in C1 that exist in C2 as well.
840staticboolvaluesOverlap(std::vector<ValueEqualityComparisonCase> &C1,
841 std::vector<ValueEqualityComparisonCase> &C2) {
842 std::vector<ValueEqualityComparisonCase> *V1 = &C1, *V2 = &C2;
843
844// Make V1 be smaller than V2.
845if (V1->size() > V2->size())
846std::swap(V1, V2);
847
848if (V1->empty())
849returnfalse;
850if (V1->size() == 1) {
851// Just scan V2.
852ConstantInt *TheVal = (*V1)[0].Value;
853for (const ValueEqualityComparisonCase &VECC : *V2)
854if (TheVal == VECC.Value)
855returntrue;
856 }
857
858// Otherwise, just sort both lists and compare element by element.
859array_pod_sort(V1->begin(), V1->end());
860array_pod_sort(V2->begin(), V2->end());
861unsigned i1 = 0, i2 = 0, e1 = V1->size(), e2 = V2->size();
862while (i1 != e1 && i2 != e2) {
863if ((*V1)[i1].Value == (*V2)[i2].Value)
864returntrue;
865if ((*V1)[i1].Value < (*V2)[i2].Value)
866 ++i1;
867else
868 ++i2;
869 }
870returnfalse;
871}
872
873// Set branch weights on SwitchInst. This sets the metadata if there is at
874// least one non-zero weight.
875staticvoidsetBranchWeights(SwitchInst *SI,ArrayRef<uint32_t> Weights,
876bool IsExpected) {
877// Check that there is at least one non-zero weight. Otherwise, pass
878// nullptr to setMetadata which will erase the existing metadata.
879MDNode *N =nullptr;
880if (llvm::any_of(Weights, [](uint32_t W) {return W != 0; }))
881N =MDBuilder(SI->getParent()->getContext())
882 .createBranchWeights(Weights, IsExpected);
883 SI->setMetadata(LLVMContext::MD_prof,N);
884}
885
886// Similar to the above, but for branch and select instructions that take
887// exactly 2 weights.
888staticvoidsetBranchWeights(Instruction *I,uint32_t TrueWeight,
889uint32_t FalseWeight,bool IsExpected) {
890assert(isa<BranchInst>(I) || isa<SelectInst>(I));
891// Check that there is at least one non-zero weight. Otherwise, pass
892// nullptr to setMetadata which will erase the existing metadata.
893MDNode *N =nullptr;
894if (TrueWeight || FalseWeight)
895N =MDBuilder(I->getParent()->getContext())
896 .createBranchWeights(TrueWeight, FalseWeight, IsExpected);
897I->setMetadata(LLVMContext::MD_prof,N);
898}
899
900/// If TI is known to be a terminator instruction and its block is known to
901/// only have a single predecessor block, check to see if that predecessor is
902/// also a value comparison with the same value, and if that comparison
903/// determines the outcome of this comparison. If so, simplify TI. This does a
904/// very limited form of jump threading.
905bool SimplifyCFGOpt::simplifyEqualityComparisonWithOnlyPredecessor(
906Instruction *TI,BasicBlock *Pred,IRBuilder<> &Builder) {
907Value *PredVal = isValueEqualityComparison(Pred->getTerminator());
908if (!PredVal)
909returnfalse;// Not a value comparison in predecessor.
910
911Value *ThisVal = isValueEqualityComparison(TI);
912assert(ThisVal &&"This isn't a value comparison!!");
913if (ThisVal != PredVal)
914returnfalse;// Different predicates.
915
916// TODO: Preserve branch weight metadata, similarly to how
917// foldValueComparisonIntoPredecessors preserves it.
918
919// Find out information about when control will move from Pred to TI's block.
920 std::vector<ValueEqualityComparisonCase> PredCases;
921BasicBlock *PredDef =
922 getValueEqualityComparisonCases(Pred->getTerminator(), PredCases);
923eliminateBlockCases(PredDef, PredCases);// Remove default from cases.
924
925// Find information about how control leaves this block.
926 std::vector<ValueEqualityComparisonCase> ThisCases;
927BasicBlock *ThisDef = getValueEqualityComparisonCases(TI, ThisCases);
928eliminateBlockCases(ThisDef, ThisCases);// Remove default from cases.
929
930// If TI's block is the default block from Pred's comparison, potentially
931// simplify TI based on this knowledge.
932if (PredDef == TI->getParent()) {
933// If we are here, we know that the value is none of those cases listed in
934// PredCases. If there are any cases in ThisCases that are in PredCases, we
935// can simplify TI.
936if (!valuesOverlap(PredCases, ThisCases))
937returnfalse;
938
939if (isa<BranchInst>(TI)) {
940// Okay, one of the successors of this condbr is dead. Convert it to a
941// uncond br.
942assert(ThisCases.size() == 1 &&"Branch can only have one case!");
943// Insert the new branch.
944Instruction *NI = Builder.CreateBr(ThisDef);
945 (void)NI;
946
947// Remove PHI node entries for the dead edge.
948 ThisCases[0].Dest->removePredecessor(PredDef);
949
950LLVM_DEBUG(dbgs() <<"Threading pred instr: " << *Pred->getTerminator()
951 <<"Through successor TI: " << *TI <<"Leaving: " << *NI
952 <<"\n");
953
954eraseTerminatorAndDCECond(TI);
955
956if (DTU)
957 DTU->applyUpdates(
958 {{DominatorTree::Delete, PredDef, ThisCases[0].Dest}});
959
960returntrue;
961 }
962
963SwitchInstProfUpdateWrapperSI = *cast<SwitchInst>(TI);
964// Okay, TI has cases that are statically dead, prune them away.
965SmallPtrSet<Constant *, 16> DeadCases;
966for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
967 DeadCases.insert(PredCases[i].Value);
968
969LLVM_DEBUG(dbgs() <<"Threading pred instr: " << *Pred->getTerminator()
970 <<"Through successor TI: " << *TI);
971
972SmallDenseMap<BasicBlock *, int, 8> NumPerSuccessorCases;
973for (SwitchInst::CaseIt i =SI->case_end(), e =SI->case_begin(); i != e;) {
974 --i;
975auto *Successor = i->getCaseSuccessor();
976if (DTU)
977 ++NumPerSuccessorCases[Successor];
978if (DeadCases.count(i->getCaseValue())) {
979Successor->removePredecessor(PredDef);
980SI.removeCase(i);
981if (DTU)
982 --NumPerSuccessorCases[Successor];
983 }
984 }
985
986if (DTU) {
987 std::vector<DominatorTree::UpdateType> Updates;
988for (const std::pair<BasicBlock *, int> &I : NumPerSuccessorCases)
989if (I.second == 0)
990 Updates.push_back({DominatorTree::Delete, PredDef,I.first});
991 DTU->applyUpdates(Updates);
992 }
993
994LLVM_DEBUG(dbgs() <<"Leaving: " << *TI <<"\n");
995returntrue;
996 }
997
998// Otherwise, TI's block must correspond to some matched value. Find out
999// which value (or set of values) this is.
1000ConstantInt *TIV =nullptr;
1001BasicBlock *TIBB = TI->getParent();
1002for (constauto &[Value, Dest] : PredCases)
1003if (Dest == TIBB) {
1004if (TIV)
1005returnfalse;// Cannot handle multiple values coming to this block.
1006 TIV =Value;
1007 }
1008assert(TIV &&"No edge from pred to succ?");
1009
1010// Okay, we found the one constant that our value can be if we get into TI's
1011// BB. Find out which successor will unconditionally be branched to.
1012BasicBlock *TheRealDest =nullptr;
1013for (constauto &[Value, Dest] : ThisCases)
1014if (Value == TIV) {
1015 TheRealDest = Dest;
1016break;
1017 }
1018
1019// If not handled by any explicit cases, it is handled by the default case.
1020if (!TheRealDest)
1021 TheRealDest = ThisDef;
1022
1023SmallPtrSet<BasicBlock *, 2> RemovedSuccs;
1024
1025// Remove PHI node entries for dead edges.
1026BasicBlock *CheckEdge = TheRealDest;
1027for (BasicBlock *Succ :successors(TIBB))
1028if (Succ != CheckEdge) {
1029if (Succ != TheRealDest)
1030 RemovedSuccs.insert(Succ);
1031 Succ->removePredecessor(TIBB);
1032 }else
1033 CheckEdge =nullptr;
1034
1035// Insert the new branch.
1036Instruction *NI = Builder.CreateBr(TheRealDest);
1037 (void)NI;
1038
1039LLVM_DEBUG(dbgs() <<"Threading pred instr: " << *Pred->getTerminator()
1040 <<"Through successor TI: " << *TI <<"Leaving: " << *NI
1041 <<"\n");
1042
1043eraseTerminatorAndDCECond(TI);
1044if (DTU) {
1045SmallVector<DominatorTree::UpdateType, 2> Updates;
1046 Updates.reserve(RemovedSuccs.size());
1047for (auto *RemovedSucc : RemovedSuccs)
1048 Updates.push_back({DominatorTree::Delete, TIBB, RemovedSucc});
1049 DTU->applyUpdates(Updates);
1050 }
1051returntrue;
1052}
1053
1054namespace{
1055
1056/// This class implements a stable ordering of constant
1057/// integers that does not depend on their address. This is important for
1058/// applications that sort ConstantInt's to ensure uniqueness.
1059structConstantIntOrdering {
1060bool operator()(constConstantInt *LHS,constConstantInt *RHS) const{
1061returnLHS->getValue().ult(RHS->getValue());
1062 }
1063};
1064
1065}// end anonymous namespace
1066
1067staticintconstantIntSortPredicate(ConstantInt *const *P1,
1068ConstantInt *const *P2) {
1069constConstantInt *LHS = *P1;
1070constConstantInt *RHS = *P2;
1071if (LHS ==RHS)
1072return 0;
1073returnLHS->getValue().ult(RHS->getValue()) ? 1 : -1;
1074}
1075
1076/// Get Weights of a given terminator, the default weight is at the front
1077/// of the vector. If TI is a conditional eq, we need to swap the branch-weight
1078/// metadata.
1079staticvoidgetBranchWeights(Instruction *TI,
1080SmallVectorImpl<uint64_t> &Weights) {
1081MDNode *MD = TI->getMetadata(LLVMContext::MD_prof);
1082assert(MD &&"Invalid branch-weight metadata");
1083extractFromBranchWeightMD64(MD, Weights);
1084
1085// If TI is a conditional eq, the default case is the false case,
1086// and the corresponding branch-weight data is at index 2. We swap the
1087// default weight to be the first entry.
1088if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
1089assert(Weights.size() == 2);
1090ICmpInst *ICI = cast<ICmpInst>(BI->getCondition());
1091if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
1092std::swap(Weights.front(), Weights.back());
1093 }
1094}
1095
1096/// Keep halving the weights until all can fit in uint32_t.
1097staticvoidfitWeights(MutableArrayRef<uint64_t> Weights) {
1098uint64_t Max = *llvm::max_element(Weights);
1099if (Max > UINT_MAX) {
1100unsignedOffset = 32 -llvm::countl_zero(Max);
1101for (uint64_t &I : Weights)
1102I >>=Offset;
1103 }
1104}
1105
1106staticvoidcloneInstructionsIntoPredecessorBlockAndUpdateSSAUses(
1107BasicBlock *BB,BasicBlock *PredBlock,ValueToValueMapTy &VMap) {
1108Instruction *PTI = PredBlock->getTerminator();
1109
1110// If we have bonus instructions, clone them into the predecessor block.
1111// Note that there may be multiple predecessor blocks, so we cannot move
1112// bonus instructions to a predecessor block.
1113for (Instruction &BonusInst : *BB) {
1114if (BonusInst.isTerminator())
1115continue;
1116
1117Instruction *NewBonusInst = BonusInst.clone();
1118
1119if (!isa<DbgInfoIntrinsic>(BonusInst) &&
1120 PTI->getDebugLoc() != NewBonusInst->getDebugLoc()) {
1121// Unless the instruction has the same !dbg location as the original
1122// branch, drop it. When we fold the bonus instructions we want to make
1123// sure we reset their debug locations in order to avoid stepping on
1124// dead code caused by folding dead branches.
1125 NewBonusInst->setDebugLoc(DebugLoc());
1126 }
1127
1128RemapInstruction(NewBonusInst, VMap,
1129RF_NoModuleLevelChanges |RF_IgnoreMissingLocals);
1130
1131// If we speculated an instruction, we need to drop any metadata that may
1132// result in undefined behavior, as the metadata might have been valid
1133// only given the branch precondition.
1134// Similarly strip attributes on call parameters that may cause UB in
1135// location the call is moved to.
1136 NewBonusInst->dropUBImplyingAttrsAndMetadata();
1137
1138 NewBonusInst->insertInto(PredBlock, PTI->getIterator());
1139autoRange = NewBonusInst->cloneDebugInfoFrom(&BonusInst);
1140RemapDbgRecordRange(NewBonusInst->getModule(),Range, VMap,
1141RF_NoModuleLevelChanges |RF_IgnoreMissingLocals);
1142
1143if (isa<DbgInfoIntrinsic>(BonusInst))
1144continue;
1145
1146 NewBonusInst->takeName(&BonusInst);
1147 BonusInst.setName(NewBonusInst->getName() +".old");
1148 VMap[&BonusInst] = NewBonusInst;
1149
1150// Update (liveout) uses of bonus instructions,
1151// now that the bonus instruction has been cloned into predecessor.
1152// Note that we expect to be in a block-closed SSA form for this to work!
1153for (Use &U :make_early_inc_range(BonusInst.uses())) {
1154auto *UI = cast<Instruction>(U.getUser());
1155auto *PN = dyn_cast<PHINode>(UI);
1156if (!PN) {
1157assert(UI->getParent() == BB && BonusInst.comesBefore(UI) &&
1158"If the user is not a PHI node, then it should be in the same "
1159"block as, and come after, the original bonus instruction.");
1160continue;// Keep using the original bonus instruction.
1161 }
1162// Is this the block-closed SSA form PHI node?
1163if (PN->getIncomingBlock(U) == BB)
1164continue;// Great, keep using the original bonus instruction.
1165// The only other alternative is an "use" when coming from
1166// the predecessor block - here we should refer to the cloned bonus instr.
1167assert(PN->getIncomingBlock(U) == PredBlock &&
1168"Not in block-closed SSA form?");
1169 U.set(NewBonusInst);
1170 }
1171 }
1172}
1173
1174bool SimplifyCFGOpt::performValueComparisonIntoPredecessorFolding(
1175Instruction *TI,Value *&CV,Instruction *PTI,IRBuilder<> &Builder) {
1176BasicBlock *BB = TI->getParent();
1177BasicBlock *Pred = PTI->getParent();
1178
1179SmallVector<DominatorTree::UpdateType, 32> Updates;
1180
1181// Figure out which 'cases' to copy from SI to PSI.
1182 std::vector<ValueEqualityComparisonCase> BBCases;
1183BasicBlock *BBDefault = getValueEqualityComparisonCases(TI, BBCases);
1184
1185 std::vector<ValueEqualityComparisonCase> PredCases;
1186BasicBlock *PredDefault = getValueEqualityComparisonCases(PTI, PredCases);
1187
1188// Based on whether the default edge from PTI goes to BB or not, fill in
1189// PredCases and PredDefault with the new switch cases we would like to
1190// build.
1191SmallMapVector<BasicBlock *, int, 8> NewSuccessors;
1192
1193// Update the branch weight metadata along the way
1194SmallVector<uint64_t, 8> Weights;
1195bool PredHasWeights =hasBranchWeightMD(*PTI);
1196bool SuccHasWeights =hasBranchWeightMD(*TI);
1197
1198if (PredHasWeights) {
1199getBranchWeights(PTI, Weights);
1200// branch-weight metadata is inconsistent here.
1201if (Weights.size() != 1 + PredCases.size())
1202 PredHasWeights = SuccHasWeights =false;
1203 }elseif (SuccHasWeights)
1204// If there are no predecessor weights but there are successor weights,
1205// populate Weights with 1, which will later be scaled to the sum of
1206// successor's weights
1207 Weights.assign(1 + PredCases.size(), 1);
1208
1209SmallVector<uint64_t, 8> SuccWeights;
1210if (SuccHasWeights) {
1211getBranchWeights(TI, SuccWeights);
1212// branch-weight metadata is inconsistent here.
1213if (SuccWeights.size() != 1 + BBCases.size())
1214 PredHasWeights = SuccHasWeights =false;
1215 }elseif (PredHasWeights)
1216 SuccWeights.assign(1 + BBCases.size(), 1);
1217
1218if (PredDefault == BB) {
1219// If this is the default destination from PTI, only the edges in TI
1220// that don't occur in PTI, or that branch to BB will be activated.
1221 std::set<ConstantInt *, ConstantIntOrdering> PTIHandled;
1222for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
1223if (PredCases[i].Dest != BB)
1224 PTIHandled.insert(PredCases[i].Value);
1225else {
1226// The default destination is BB, we don't need explicit targets.
1227std::swap(PredCases[i], PredCases.back());
1228
1229if (PredHasWeights || SuccHasWeights) {
1230// Increase weight for the default case.
1231 Weights[0] += Weights[i + 1];
1232std::swap(Weights[i + 1], Weights.back());
1233 Weights.pop_back();
1234 }
1235
1236 PredCases.pop_back();
1237 --i;
1238 --e;
1239 }
1240
1241// Reconstruct the new switch statement we will be building.
1242if (PredDefault != BBDefault) {
1243 PredDefault->removePredecessor(Pred);
1244if (DTU && PredDefault != BB)
1245 Updates.push_back({DominatorTree::Delete, Pred, PredDefault});
1246 PredDefault = BBDefault;
1247 ++NewSuccessors[BBDefault];
1248 }
1249
1250unsigned CasesFromPred = Weights.size();
1251uint64_t ValidTotalSuccWeight = 0;
1252for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
1253if (!PTIHandled.count(BBCases[i].Value) && BBCases[i].Dest != BBDefault) {
1254 PredCases.push_back(BBCases[i]);
1255 ++NewSuccessors[BBCases[i].Dest];
1256if (SuccHasWeights || PredHasWeights) {
1257// The default weight is at index 0, so weight for the ith case
1258// should be at index i+1. Scale the cases from successor by
1259// PredDefaultWeight (Weights[0]).
1260 Weights.push_back(Weights[0] * SuccWeights[i + 1]);
1261 ValidTotalSuccWeight += SuccWeights[i + 1];
1262 }
1263 }
1264
1265if (SuccHasWeights || PredHasWeights) {
1266 ValidTotalSuccWeight += SuccWeights[0];
1267// Scale the cases from predecessor by ValidTotalSuccWeight.
1268for (unsigned i = 1; i < CasesFromPred; ++i)
1269 Weights[i] *= ValidTotalSuccWeight;
1270// Scale the default weight by SuccDefaultWeight (SuccWeights[0]).
1271 Weights[0] *= SuccWeights[0];
1272 }
1273 }else {
1274// If this is not the default destination from PSI, only the edges
1275// in SI that occur in PSI with a destination of BB will be
1276// activated.
1277 std::set<ConstantInt *, ConstantIntOrdering> PTIHandled;
1278 std::map<ConstantInt *, uint64_t> WeightsForHandled;
1279for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
1280if (PredCases[i].Dest == BB) {
1281 PTIHandled.insert(PredCases[i].Value);
1282
1283if (PredHasWeights || SuccHasWeights) {
1284 WeightsForHandled[PredCases[i].Value] = Weights[i + 1];
1285std::swap(Weights[i + 1], Weights.back());
1286 Weights.pop_back();
1287 }
1288
1289std::swap(PredCases[i], PredCases.back());
1290 PredCases.pop_back();
1291 --i;
1292 --e;
1293 }
1294
1295// Okay, now we know which constants were sent to BB from the
1296// predecessor. Figure out where they will all go now.
1297for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
1298if (PTIHandled.count(BBCases[i].Value)) {
1299// If this is one we are capable of getting...
1300if (PredHasWeights || SuccHasWeights)
1301 Weights.push_back(WeightsForHandled[BBCases[i].Value]);
1302 PredCases.push_back(BBCases[i]);
1303 ++NewSuccessors[BBCases[i].Dest];
1304 PTIHandled.erase(BBCases[i].Value);// This constant is taken care of
1305 }
1306
1307// If there are any constants vectored to BB that TI doesn't handle,
1308// they must go to the default destination of TI.
1309for (ConstantInt *I : PTIHandled) {
1310if (PredHasWeights || SuccHasWeights)
1311 Weights.push_back(WeightsForHandled[I]);
1312 PredCases.push_back(ValueEqualityComparisonCase(I, BBDefault));
1313 ++NewSuccessors[BBDefault];
1314 }
1315 }
1316
1317// Okay, at this point, we know which new successor Pred will get. Make
1318// sure we update the number of entries in the PHI nodes for these
1319// successors.
1320SmallPtrSet<BasicBlock *, 2> SuccsOfPred;
1321if (DTU) {
1322 SuccsOfPred = {succ_begin(Pred),succ_end(Pred)};
1323 Updates.reserve(Updates.size() + NewSuccessors.size());
1324 }
1325for (const std::pair<BasicBlock *, int /*Num*/> &NewSuccessor :
1326 NewSuccessors) {
1327for (autoI :seq(NewSuccessor.second)) {
1328 (void)I;
1329addPredecessorToBlock(NewSuccessor.first, Pred, BB);
1330 }
1331if (DTU && !SuccsOfPred.contains(NewSuccessor.first))
1332 Updates.push_back({DominatorTree::Insert, Pred, NewSuccessor.first});
1333 }
1334
1335 Builder.SetInsertPoint(PTI);
1336// Convert pointer to int before we switch.
1337if (CV->getType()->isPointerTy()) {
1338 CV =
1339 Builder.CreatePtrToInt(CV,DL.getIntPtrType(CV->getType()),"magicptr");
1340 }
1341
1342// Now that the successors are updated, create the new Switch instruction.
1343SwitchInst *NewSI = Builder.CreateSwitch(CV, PredDefault, PredCases.size());
1344 NewSI->setDebugLoc(PTI->getDebugLoc());
1345for (ValueEqualityComparisonCase &V : PredCases)
1346 NewSI->addCase(V.Value,V.Dest);
1347
1348if (PredHasWeights || SuccHasWeights) {
1349// Halve the weights if any of them cannot fit in an uint32_t
1350fitWeights(Weights);
1351
1352SmallVector<uint32_t, 8> MDWeights(Weights.begin(), Weights.end());
1353
1354setBranchWeights(NewSI, MDWeights,/*IsExpected=*/false);
1355 }
1356
1357eraseTerminatorAndDCECond(PTI);
1358
1359// Okay, last check. If BB is still a successor of PSI, then we must
1360// have an infinite loop case. If so, add an infinitely looping block
1361// to handle the case to preserve the behavior of the code.
1362BasicBlock *InfLoopBlock =nullptr;
1363for (unsigned i = 0, e = NewSI->getNumSuccessors(); i != e; ++i)
1364if (NewSI->getSuccessor(i) == BB) {
1365if (!InfLoopBlock) {
1366// Insert it at the end of the function, because it's either code,
1367// or it won't matter if it's hot. :)
1368 InfLoopBlock =
1369BasicBlock::Create(BB->getContext(),"infloop", BB->getParent());
1370BranchInst::Create(InfLoopBlock, InfLoopBlock);
1371if (DTU)
1372 Updates.push_back(
1373 {DominatorTree::Insert, InfLoopBlock, InfLoopBlock});
1374 }
1375 NewSI->setSuccessor(i, InfLoopBlock);
1376 }
1377
1378if (DTU) {
1379if (InfLoopBlock)
1380 Updates.push_back({DominatorTree::Insert, Pred, InfLoopBlock});
1381
1382 Updates.push_back({DominatorTree::Delete, Pred, BB});
1383
1384 DTU->applyUpdates(Updates);
1385 }
1386
1387 ++NumFoldValueComparisonIntoPredecessors;
1388returntrue;
1389}
1390
1391/// The specified terminator is a value equality comparison instruction
1392/// (either a switch or a branch on "X == c").
1393/// See if any of the predecessors of the terminator block are value comparisons
1394/// on the same value. If so, and if safe to do so, fold them together.
1395bool SimplifyCFGOpt::foldValueComparisonIntoPredecessors(Instruction *TI,
1396IRBuilder<> &Builder) {
1397BasicBlock *BB = TI->getParent();
1398Value *CV = isValueEqualityComparison(TI);// CondVal
1399assert(CV &&"Not a comparison?");
1400
1401bool Changed =false;
1402
1403SmallSetVector<BasicBlock *, 16> Preds(pred_begin(BB),pred_end(BB));
1404while (!Preds.empty()) {
1405BasicBlock *Pred = Preds.pop_back_val();
1406Instruction *PTI = Pred->getTerminator();
1407
1408// Don't try to fold into itself.
1409if (Pred == BB)
1410continue;
1411
1412// See if the predecessor is a comparison with the same value.
1413Value *PCV = isValueEqualityComparison(PTI);// PredCondVal
1414if (PCV != CV)
1415continue;
1416
1417SmallSetVector<BasicBlock *, 4> FailBlocks;
1418if (!safeToMergeTerminators(TI, PTI, &FailBlocks)) {
1419for (auto *Succ : FailBlocks) {
1420if (!SplitBlockPredecessors(Succ, TI->getParent(),".fold.split", DTU))
1421returnfalse;
1422 }
1423 }
1424
1425 performValueComparisonIntoPredecessorFolding(TI, CV, PTI, Builder);
1426 Changed =true;
1427 }
1428return Changed;
1429}
1430
1431// If we would need to insert a select that uses the value of this invoke
1432// (comments in hoistSuccIdenticalTerminatorToSwitchOrIf explain why we would
1433// need to do this), we can't hoist the invoke, as there is nowhere to put the
1434// select in this case.
1435staticboolisSafeToHoistInvoke(BasicBlock *BB1,BasicBlock *BB2,
1436Instruction *I1,Instruction *I2) {
1437for (BasicBlock *Succ :successors(BB1)) {
1438for (constPHINode &PN : Succ->phis()) {
1439Value *BB1V = PN.getIncomingValueForBlock(BB1);
1440Value *BB2V = PN.getIncomingValueForBlock(BB2);
1441if (BB1V != BB2V && (BB1V == I1 || BB2V == I2)) {
1442returnfalse;
1443 }
1444 }
1445 }
1446returntrue;
1447}
1448
1449// Get interesting characteristics of instructions that
1450// `hoistCommonCodeFromSuccessors` didn't hoist. They restrict what kind of
1451// instructions can be reordered across.
1452enumSkipFlags {
1453SkipReadMem = 1,
1454SkipSideEffect = 2,
1455SkipImplicitControlFlow = 4
1456};
1457
1458staticunsignedskippedInstrFlags(Instruction *I) {
1459unsigned Flags = 0;
1460if (I->mayReadFromMemory())
1461 Flags |=SkipReadMem;
1462// We can't arbitrarily move around allocas, e.g. moving allocas (especially
1463// inalloca) across stacksave/stackrestore boundaries.
1464if (I->mayHaveSideEffects() || isa<AllocaInst>(I))
1465 Flags |=SkipSideEffect;
1466if (!isGuaranteedToTransferExecutionToSuccessor(I))
1467 Flags |=SkipImplicitControlFlow;
1468return Flags;
1469}
1470
1471// Returns true if it is safe to reorder an instruction across preceding
1472// instructions in a basic block.
1473staticboolisSafeToHoistInstr(Instruction *I,unsigned Flags) {
1474// Don't reorder a store over a load.
1475if ((Flags &SkipReadMem) &&I->mayWriteToMemory())
1476returnfalse;
1477
1478// If we have seen an instruction with side effects, it's unsafe to reorder an
1479// instruction which reads memory or itself has side effects.
1480if ((Flags &SkipSideEffect) &&
1481 (I->mayReadFromMemory() ||I->mayHaveSideEffects() || isa<AllocaInst>(I)))
1482returnfalse;
1483
1484// Reordering across an instruction which does not necessarily transfer
1485// control to the next instruction is speculation.
1486if ((Flags &SkipImplicitControlFlow) && !isSafeToSpeculativelyExecute(I))
1487returnfalse;
1488
1489// Hoisting of llvm.deoptimize is only legal together with the next return
1490// instruction, which this pass is not always able to do.
1491if (auto *CB = dyn_cast<CallBase>(I))
1492if (CB->getIntrinsicID() == Intrinsic::experimental_deoptimize)
1493returnfalse;
1494
1495// It's also unsafe/illegal to hoist an instruction above its instruction
1496// operands
1497BasicBlock *BB =I->getParent();
1498for (Value *Op :I->operands()) {
1499if (auto *J = dyn_cast<Instruction>(Op))
1500if (J->getParent() == BB)
1501returnfalse;
1502 }
1503
1504returntrue;
1505}
1506
1507staticboolpassingValueIsAlwaysUndefined(Value *V,Instruction *I,bool PtrValueMayBeModified =false);
1508
1509/// Helper function for hoistCommonCodeFromSuccessors. Return true if identical
1510/// instructions \p I1 and \p I2 can and should be hoisted.
1511staticboolshouldHoistCommonInstructions(Instruction *I1,Instruction *I2,
1512constTargetTransformInfo &TTI) {
1513// If we're going to hoist a call, make sure that the two instructions
1514// we're commoning/hoisting are both marked with musttail, or neither of
1515// them is marked as such. Otherwise, we might end up in a situation where
1516// we hoist from a block where the terminator is a `ret` to a block where
1517// the terminator is a `br`, and `musttail` calls expect to be followed by
1518// a return.
1519auto *C1 = dyn_cast<CallInst>(I1);
1520auto *C2 = dyn_cast<CallInst>(I2);
1521if (C1 && C2)
1522if (C1->isMustTailCall() != C2->isMustTailCall())
1523returnfalse;
1524
1525if (!TTI.isProfitableToHoist(I1) || !TTI.isProfitableToHoist(I2))
1526returnfalse;
1527
1528// If any of the two call sites has nomerge or convergent attribute, stop
1529// hoisting.
1530if (constauto *CB1 = dyn_cast<CallBase>(I1))
1531if (CB1->cannotMerge() || CB1->isConvergent())
1532returnfalse;
1533if (constauto *CB2 = dyn_cast<CallBase>(I2))
1534if (CB2->cannotMerge() || CB2->isConvergent())
1535returnfalse;
1536
1537returntrue;
1538}
1539
1540/// Hoists DbgVariableRecords from \p I1 and \p OtherInstrs that are identical
1541/// in lock-step to \p TI. This matches how dbg.* intrinsics are hoisting in
1542/// hoistCommonCodeFromSuccessors. e.g. The input:
1543/// I1 DVRs: { x, z },
1544/// OtherInsts: { I2 DVRs: { x, y, z } }
1545/// would result in hoisting only DbgVariableRecord x.
1546staticvoidhoistLockstepIdenticalDbgVariableRecords(
1547Instruction *TI,Instruction *I1,
1548SmallVectorImpl<Instruction *> &OtherInsts) {
1549if (!I1->hasDbgRecords())
1550return;
1551usingCurrentAndEndIt =
1552 std::pair<DbgRecord::self_iterator, DbgRecord::self_iterator>;
1553// Vector of {Current, End} iterators.
1554SmallVector<CurrentAndEndIt> Itrs;
1555 Itrs.reserve(OtherInsts.size() + 1);
1556// Helper lambdas for lock-step checks:
1557// Return true if this Current == End.
1558auto atEnd = [](const CurrentAndEndIt &Pair) {
1559return Pair.first == Pair.second;
1560 };
1561// Return true if all Current are identical.
1562auto allIdentical = [](constSmallVector<CurrentAndEndIt> &Itrs) {
1563returnall_of(make_first_range(ArrayRef(Itrs).drop_front()),
1564 [&](DbgRecord::self_iteratorI) {
1565return Itrs[0].first->isIdenticalToWhenDefined(*I);
1566 });
1567 };
1568
1569// Collect the iterators.
1570 Itrs.push_back(
1571 {I1->getDbgRecordRange().begin(), I1->getDbgRecordRange().end()});
1572for (Instruction *Other : OtherInsts) {
1573if (!Other->hasDbgRecords())
1574return;
1575 Itrs.push_back(
1576 {Other->getDbgRecordRange().begin(),Other->getDbgRecordRange().end()});
1577 }
1578
1579// Iterate in lock-step until any of the DbgRecord lists are exausted. If
1580// the lock-step DbgRecord are identical, hoist all of them to TI.
1581// This replicates the dbg.* intrinsic behaviour in
1582// hoistCommonCodeFromSuccessors.
1583while (none_of(Itrs, atEnd)) {
1584bool HoistDVRs = allIdentical(Itrs);
1585for (CurrentAndEndIt &Pair : Itrs) {
1586// Increment Current iterator now as we may be about to move the
1587// DbgRecord.
1588DbgRecord &DR = *Pair.first++;
1589if (HoistDVRs) {
1590 DR.removeFromParent();
1591 TI->getParent()->insertDbgRecordBefore(&DR, TI->getIterator());
1592 }
1593 }
1594 }
1595}
1596
1597staticboolareIdenticalUpToCommutativity(constInstruction *I1,
1598constInstruction *I2) {
1599if (I1->isIdenticalToWhenDefined(I2,/*IntersectAttrs=*/true))
1600returntrue;
1601
1602if (auto *Cmp1 = dyn_cast<CmpInst>(I1))
1603if (auto *Cmp2 = dyn_cast<CmpInst>(I2))
1604return Cmp1->getPredicate() == Cmp2->getSwappedPredicate() &&
1605 Cmp1->getOperand(0) == Cmp2->getOperand(1) &&
1606 Cmp1->getOperand(1) == Cmp2->getOperand(0);
1607
1608if (I1->isCommutative() && I1->isSameOperationAs(I2)) {
1609return I1->getOperand(0) == I2->getOperand(1) &&
1610 I1->getOperand(1) == I2->getOperand(0) &&
1611equal(drop_begin(I1->operands(), 2),drop_begin(I2->operands(), 2));
1612 }
1613
1614returnfalse;
1615}
1616
1617/// If the target supports conditional faulting,
1618/// we look for the following pattern:
1619/// \code
1620/// BB:
1621/// ...
1622/// %cond = icmp ult %x, %y
1623/// br i1 %cond, label %TrueBB, label %FalseBB
1624/// FalseBB:
1625/// store i32 1, ptr %q, align 4
1626/// ...
1627/// TrueBB:
1628/// %maskedloadstore = load i32, ptr %b, align 4
1629/// store i32 %maskedloadstore, ptr %p, align 4
1630/// ...
1631/// \endcode
1632///
1633/// and transform it into:
1634///
1635/// \code
1636/// BB:
1637/// ...
1638/// %cond = icmp ult %x, %y
1639/// %maskedloadstore = cload i32, ptr %b, %cond
1640/// cstore i32 %maskedloadstore, ptr %p, %cond
1641/// cstore i32 1, ptr %q, ~%cond
1642/// br i1 %cond, label %TrueBB, label %FalseBB
1643/// FalseBB:
1644/// ...
1645/// TrueBB:
1646/// ...
1647/// \endcode
1648///
1649/// where cload/cstore are represented by llvm.masked.load/store intrinsics,
1650/// e.g.
1651///
1652/// \code
1653/// %vcond = bitcast i1 %cond to <1 x i1>
1654/// %v0 = call <1 x i32> @llvm.masked.load.v1i32.p0
1655/// (ptr %b, i32 4, <1 x i1> %vcond, <1 x i32> poison)
1656/// %maskedloadstore = bitcast <1 x i32> %v0 to i32
1657/// call void @llvm.masked.store.v1i32.p0
1658/// (<1 x i32> %v0, ptr %p, i32 4, <1 x i1> %vcond)
1659/// %cond.not = xor i1 %cond, true
1660/// %vcond.not = bitcast i1 %cond.not to <1 x i>
1661/// call void @llvm.masked.store.v1i32.p0
1662/// (<1 x i32> <i32 1>, ptr %q, i32 4, <1x i1> %vcond.not)
1663/// \endcode
1664///
1665/// So we need to turn hoisted load/store into cload/cstore.
1666///
1667/// \param BI The branch instruction.
1668/// \param SpeculatedConditionalLoadsStores The load/store instructions that
1669/// will be speculated.
1670/// \param Invert indicates if speculates FalseBB. Only used in triangle CFG.
1671staticvoidhoistConditionalLoadsStores(
1672BranchInst *BI,
1673SmallVectorImpl<Instruction *> &SpeculatedConditionalLoadsStores,
1674 std::optional<bool> Invert) {
1675auto &Context = BI->getParent()->getContext();
1676auto *VCondTy =FixedVectorType::get(Type::getInt1Ty(Context), 1);
1677auto *Cond = BI->getOperand(0);
1678// Construct the condition if needed.
1679BasicBlock *BB = BI->getParent();
1680IRBuilder<> Builder(
1681 Invert.has_value() ? SpeculatedConditionalLoadsStores.back() : BI);
1682Value *Mask =nullptr;
1683Value *MaskFalse =nullptr;
1684Value *MaskTrue =nullptr;
1685if (Invert.has_value()) {
1686 Mask = Builder.CreateBitCast(
1687 *Invert ? Builder.CreateXor(Cond,ConstantInt::getTrue(Context)) :Cond,
1688 VCondTy);
1689 }else {
1690 MaskFalse = Builder.CreateBitCast(
1691 Builder.CreateXor(Cond,ConstantInt::getTrue(Context)), VCondTy);
1692 MaskTrue = Builder.CreateBitCast(Cond, VCondTy);
1693 }
1694auto PeekThroughBitcasts = [](Value *V) {
1695while (auto *BitCast = dyn_cast<BitCastInst>(V))
1696 V = BitCast->getOperand(0);
1697return V;
1698 };
1699for (auto *I : SpeculatedConditionalLoadsStores) {
1700IRBuilder<> Builder(Invert.has_value() ?I : BI);
1701if (!Invert.has_value())
1702 Mask =I->getParent() == BI->getSuccessor(0) ? MaskTrue : MaskFalse;
1703// We currently assume conditional faulting load/store is supported for
1704// scalar types only when creating new instructions. This can be easily
1705// extended for vector types in the future.
1706assert(!getLoadStoreType(I)->isVectorTy() &&"not implemented");
1707auto *Op0 =I->getOperand(0);
1708CallInst *MaskedLoadStore =nullptr;
1709if (auto *LI = dyn_cast<LoadInst>(I)) {
1710// Handle Load.
1711auto *Ty =I->getType();
1712PHINode *PN =nullptr;
1713Value *PassThru =nullptr;
1714if (Invert.has_value())
1715for (User *U :I->users())
1716if ((PN = dyn_cast<PHINode>(U))) {
1717 PassThru = Builder.CreateBitCast(
1718 PeekThroughBitcasts(PN->getIncomingValueForBlock(BB)),
1719FixedVectorType::get(Ty, 1));
1720break;
1721 }
1722 MaskedLoadStore = Builder.CreateMaskedLoad(
1723FixedVectorType::get(Ty, 1), Op0, LI->getAlign(), Mask, PassThru);
1724Value *NewLoadStore = Builder.CreateBitCast(MaskedLoadStore, Ty);
1725if (PN)
1726 PN->setIncomingValue(PN->getBasicBlockIndex(BB), NewLoadStore);
1727I->replaceAllUsesWith(NewLoadStore);
1728 }else {
1729// Handle Store.
1730auto *StoredVal = Builder.CreateBitCast(
1731 PeekThroughBitcasts(Op0),FixedVectorType::get(Op0->getType(), 1));
1732 MaskedLoadStore = Builder.CreateMaskedStore(
1733 StoredVal,I->getOperand(1), cast<StoreInst>(I)->getAlign(), Mask);
1734 }
1735// For non-debug metadata, only !annotation, !range, !nonnull and !align are
1736// kept when hoisting (see Instruction::dropUBImplyingAttrsAndMetadata).
1737//
1738// !nonnull, !align : Not support pointer type, no need to keep.
1739// !range: Load type is changed from scalar to vector, but the metadata on
1740// vector specifies a per-element range, so the semantics stay the
1741// same. Keep it.
1742// !annotation: Not impact semantics. Keep it.
1743if (constMDNode *Ranges =I->getMetadata(LLVMContext::MD_range))
1744 MaskedLoadStore->addRangeRetAttr(getConstantRangeFromMetadata(*Ranges));
1745I->dropUBImplyingAttrsAndUnknownMetadata({LLVMContext::MD_annotation});
1746// FIXME: DIAssignID is not supported for masked store yet.
1747// (Verifier::visitDIAssignIDMetadata)
1748at::deleteAssignmentMarkers(I);
1749I->eraseMetadataIf([](unsigned MDKind,MDNode *Node) {
1750returnNode->getMetadataID() == Metadata::DIAssignIDKind;
1751 });
1752 MaskedLoadStore->copyMetadata(*I);
1753I->eraseFromParent();
1754 }
1755}
1756
1757staticboolisSafeCheapLoadStore(constInstruction *I,
1758constTargetTransformInfo &TTI) {
1759// Not handle volatile or atomic.
1760if (auto *L = dyn_cast<LoadInst>(I)) {
1761if (!L->isSimple())
1762returnfalse;
1763 }elseif (auto *S = dyn_cast<StoreInst>(I)) {
1764if (!S->isSimple())
1765returnfalse;
1766 }else
1767returnfalse;
1768
1769// llvm.masked.load/store use i32 for alignment while load/store use i64.
1770// That's why we have the alignment limitation.
1771// FIXME: Update the prototype of the intrinsics?
1772returnTTI.hasConditionalLoadStoreForType(getLoadStoreType(I)) &&
1773getLoadStoreAlignment(I) <Value::MaximumAlignment;
1774}
1775
1776namespace{
1777
1778// LockstepReverseIterator - Iterates through instructions
1779// in a set of blocks in reverse order from the first non-terminator.
1780// For example (assume all blocks have size n):
1781// LockstepReverseIterator I([B1, B2, B3]);
1782// *I-- = [B1[n], B2[n], B3[n]];
1783// *I-- = [B1[n-1], B2[n-1], B3[n-1]];
1784// *I-- = [B1[n-2], B2[n-2], B3[n-2]];
1785// ...
1786classLockstepReverseIterator {
1787ArrayRef<BasicBlock *>Blocks;
1788SmallVector<Instruction *, 4> Insts;
1789boolFail;
1790
1791public:
1792 LockstepReverseIterator(ArrayRef<BasicBlock *> Blocks) :Blocks(Blocks) {
1793 reset();
1794 }
1795
1796void reset() {
1797Fail =false;
1798 Insts.clear();
1799for (auto *BB : Blocks) {
1800Instruction *Inst = BB->getTerminator();
1801for (Inst = Inst->getPrevNode(); Inst && isa<DbgInfoIntrinsic>(Inst);)
1802 Inst = Inst->getPrevNode();
1803if (!Inst) {
1804// Block wasn't big enough.
1805Fail =true;
1806return;
1807 }
1808 Insts.push_back(Inst);
1809 }
1810 }
1811
1812boolisValid() const{return !Fail; }
1813
1814voidoperator--() {
1815if (Fail)
1816return;
1817for (auto *&Inst : Insts) {
1818for (Inst = Inst->getPrevNode(); Inst && isa<DbgInfoIntrinsic>(Inst);)
1819 Inst = Inst->getPrevNode();
1820// Already at beginning of block.
1821if (!Inst) {
1822Fail =true;
1823return;
1824 }
1825 }
1826 }
1827
1828void operator++() {
1829if (Fail)
1830return;
1831for (auto *&Inst : Insts) {
1832for (Inst = Inst->getNextNode(); Inst && isa<DbgInfoIntrinsic>(Inst);)
1833 Inst = Inst->getNextNode();
1834// Already at end of block.
1835if (!Inst) {
1836Fail =true;
1837return;
1838 }
1839 }
1840 }
1841
1842ArrayRef<Instruction *>operator*() const{return Insts; }
1843};
1844
1845}// end anonymous namespace
1846
1847/// Hoist any common code in the successor blocks up into the block. This
1848/// function guarantees that BB dominates all successors. If AllInstsEqOnly is
1849/// given, only perform hoisting in case all successors blocks contain matching
1850/// instructions only. In that case, all instructions can be hoisted and the
1851/// original branch will be replaced and selects for PHIs are added.
1852bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(Instruction *TI,
1853bool AllInstsEqOnly) {
1854// This does very trivial matching, with limited scanning, to find identical
1855// instructions in the two blocks. In particular, we don't want to get into
1856// O(N1*N2*...) situations here where Ni are the sizes of these successors. As
1857// such, we currently just scan for obviously identical instructions in an
1858// identical order, possibly separated by the same number of non-identical
1859// instructions.
1860BasicBlock *BB = TI->getParent();
1861unsignedint SuccSize =succ_size(BB);
1862if (SuccSize < 2)
1863returnfalse;
1864
1865// If either of the blocks has it's address taken, then we can't do this fold,
1866// because the code we'd hoist would no longer run when we jump into the block
1867// by it's address.
1868for (auto *Succ :successors(BB))
1869if (Succ->hasAddressTaken() || !Succ->getSinglePredecessor())
1870returnfalse;
1871
1872// The second of pair is a SkipFlags bitmask.
1873usingSuccIterPair = std::pair<BasicBlock::iterator, unsigned>;
1874SmallVector<SuccIterPair, 8> SuccIterPairs;
1875for (auto *Succ :successors(BB)) {
1876BasicBlock::iterator SuccItr = Succ->begin();
1877if (isa<PHINode>(*SuccItr))
1878returnfalse;
1879 SuccIterPairs.push_back(SuccIterPair(SuccItr, 0));
1880 }
1881
1882if (AllInstsEqOnly) {
1883// Check if all instructions in the successor blocks match. This allows
1884// hoisting all instructions and removing the blocks we are hoisting from,
1885// so does not add any new instructions.
1886SmallVector<BasicBlock *> Succs =to_vector(successors(BB));
1887// Check if sizes and terminators of all successors match.
1888bool AllSame =none_of(Succs, [&Succs](BasicBlock *Succ) {
1889Instruction *Term0 = Succs[0]->getTerminator();
1890Instruction *Term = Succ->getTerminator();
1891return !Term->isSameOperationAs(Term0) ||
1892 !equal(Term->operands(), Term0->operands()) ||
1893 Succs[0]->size() != Succ->size();
1894 });
1895if (!AllSame)
1896returnfalse;
1897if (AllSame) {
1898 LockstepReverseIterator LRI(Succs);
1899while (LRI.isValid()) {
1900Instruction *I0 = (*LRI)[0];
1901if (any_of(*LRI, [I0](Instruction *I) {
1902return !areIdenticalUpToCommutativity(I0,I);
1903 })) {
1904returnfalse;
1905 }
1906 --LRI;
1907 }
1908 }
1909// Now we know that all instructions in all successors can be hoisted. Let
1910// the loop below handle the hoisting.
1911 }
1912
1913// Count how many instructions were not hoisted so far. There's a limit on how
1914// many instructions we skip, serving as a compilation time control as well as
1915// preventing excessive increase of life ranges.
1916unsigned NumSkipped = 0;
1917// If we find an unreachable instruction at the beginning of a basic block, we
1918// can still hoist instructions from the rest of the basic blocks.
1919if (SuccIterPairs.size() > 2) {
1920erase_if(SuccIterPairs,
1921 [](constauto &Pair) {return isa<UnreachableInst>(Pair.first); });
1922if (SuccIterPairs.size() < 2)
1923returnfalse;
1924 }
1925
1926bool Changed =false;
1927
1928for (;;) {
1929auto *SuccIterPairBegin = SuccIterPairs.begin();
1930auto &BB1ItrPair = *SuccIterPairBegin++;
1931auto OtherSuccIterPairRange =
1932iterator_range(SuccIterPairBegin, SuccIterPairs.end());
1933auto OtherSuccIterRange =make_first_range(OtherSuccIterPairRange);
1934
1935Instruction *I1 = &*BB1ItrPair.first;
1936
1937// Skip debug info if it is not identical.
1938bool AllDbgInstsAreIdentical =all_of(OtherSuccIterRange, [I1](auto &Iter) {
1939Instruction *I2 = &*Iter;
1940returnI1->isIdenticalToWhenDefined(I2);
1941 });
1942if (!AllDbgInstsAreIdentical) {
1943while (isa<DbgInfoIntrinsic>(I1))
1944I1 = &*++BB1ItrPair.first;
1945for (auto &SuccIter : OtherSuccIterRange) {
1946Instruction *I2 = &*SuccIter;
1947while (isa<DbgInfoIntrinsic>(I2))
1948 I2 = &*++SuccIter;
1949 }
1950 }
1951
1952bool AllInstsAreIdentical =true;
1953bool HasTerminator =I1->isTerminator();
1954for (auto &SuccIter : OtherSuccIterRange) {
1955Instruction *I2 = &*SuccIter;
1956 HasTerminator |= I2->isTerminator();
1957if (AllInstsAreIdentical && (!areIdenticalUpToCommutativity(I1, I2) ||
1958MMRAMetadata(*I1) !=MMRAMetadata(*I2)))
1959 AllInstsAreIdentical =false;
1960 }
1961
1962SmallVector<Instruction *, 8> OtherInsts;
1963for (auto &SuccIter : OtherSuccIterRange)
1964 OtherInsts.push_back(&*SuccIter);
1965
1966// If we are hoisting the terminator instruction, don't move one (making a
1967// broken BB), instead clone it, and remove BI.
1968if (HasTerminator) {
1969// Even if BB, which contains only one unreachable instruction, is ignored
1970// at the beginning of the loop, we can hoist the terminator instruction.
1971// If any instructions remain in the block, we cannot hoist terminators.
1972if (NumSkipped || !AllInstsAreIdentical) {
1973hoistLockstepIdenticalDbgVariableRecords(TI, I1, OtherInsts);
1974return Changed;
1975 }
1976
1977return hoistSuccIdenticalTerminatorToSwitchOrIf(TI, I1, OtherInsts) ||
1978 Changed;
1979 }
1980
1981if (AllInstsAreIdentical) {
1982unsigned SkipFlagsBB1 = BB1ItrPair.second;
1983 AllInstsAreIdentical =
1984isSafeToHoistInstr(I1, SkipFlagsBB1) &&
1985all_of(OtherSuccIterPairRange, [=](constauto &Pair) {
1986Instruction *I2 = &*Pair.first;
1987unsigned SkipFlagsBB2 = Pair.second;
1988// Even if the instructions are identical, it may not
1989// be safe to hoist them if we have skipped over
1990// instructions with side effects or their operands
1991// weren't hoisted.
1992returnisSafeToHoistInstr(I2, SkipFlagsBB2) &&
1993shouldHoistCommonInstructions(I1, I2,TTI);
1994 });
1995 }
1996
1997if (AllInstsAreIdentical) {
1998 BB1ItrPair.first++;
1999if (isa<DbgInfoIntrinsic>(I1)) {
2000// The debug location is an integral part of a debug info intrinsic
2001// and can't be separated from it or replaced. Instead of attempting
2002// to merge locations, simply hoist both copies of the intrinsic.
2003hoistLockstepIdenticalDbgVariableRecords(TI, I1, OtherInsts);
2004// We've just hoisted DbgVariableRecords; move I1 after them (before TI)
2005// and leave any that were not hoisted behind (by calling moveBefore
2006// rather than moveBeforePreserving).
2007I1->moveBefore(TI->getIterator());
2008for (auto &SuccIter : OtherSuccIterRange) {
2009auto *I2 = &*SuccIter++;
2010assert(isa<DbgInfoIntrinsic>(I2));
2011 I2->moveBefore(TI->getIterator());
2012 }
2013 }else {
2014// For a normal instruction, we just move one to right before the
2015// branch, then replace all uses of the other with the first. Finally,
2016// we remove the now redundant second instruction.
2017hoistLockstepIdenticalDbgVariableRecords(TI, I1, OtherInsts);
2018// We've just hoisted DbgVariableRecords; move I1 after them (before TI)
2019// and leave any that were not hoisted behind (by calling moveBefore
2020// rather than moveBeforePreserving).
2021I1->moveBefore(TI->getIterator());
2022for (auto &SuccIter : OtherSuccIterRange) {
2023Instruction *I2 = &*SuccIter++;
2024assert(I2 != I1);
2025if (!I2->use_empty())
2026 I2->replaceAllUsesWith(I1);
2027I1->andIRFlags(I2);
2028if (auto *CB = dyn_cast<CallBase>(I1)) {
2029boolSuccess = CB->tryIntersectAttributes(cast<CallBase>(I2));
2030assert(Success &&"We should not be trying to hoist callbases "
2031"with non-intersectable attributes");
2032// For NDEBUG Compile.
2033 (void)Success;
2034 }
2035
2036combineMetadataForCSE(I1, I2,true);
2037// I1 and I2 are being combined into a single instruction. Its debug
2038// location is the merged locations of the original instructions.
2039I1->applyMergedLocation(I1->getDebugLoc(), I2->getDebugLoc());
2040 I2->eraseFromParent();
2041 }
2042 }
2043if (!Changed)
2044 NumHoistCommonCode += SuccIterPairs.size();
2045 Changed =true;
2046 NumHoistCommonInstrs += SuccIterPairs.size();
2047 }else {
2048if (NumSkipped >=HoistCommonSkipLimit) {
2049hoistLockstepIdenticalDbgVariableRecords(TI, I1, OtherInsts);
2050return Changed;
2051 }
2052// We are about to skip over a pair of non-identical instructions. Record
2053// if any have characteristics that would prevent reordering instructions
2054// across them.
2055for (auto &SuccIterPair : SuccIterPairs) {
2056Instruction *I = &*SuccIterPair.first++;
2057 SuccIterPair.second |=skippedInstrFlags(I);
2058 }
2059 ++NumSkipped;
2060 }
2061 }
2062}
2063
2064bool SimplifyCFGOpt::hoistSuccIdenticalTerminatorToSwitchOrIf(
2065Instruction *TI,Instruction *I1,
2066SmallVectorImpl<Instruction *> &OtherSuccTIs) {
2067
2068auto *BI = dyn_cast<BranchInst>(TI);
2069
2070bool Changed =false;
2071BasicBlock *TIParent = TI->getParent();
2072BasicBlock *BB1 =I1->getParent();
2073
2074// Use only for an if statement.
2075auto *I2 = *OtherSuccTIs.begin();
2076auto *BB2 = I2->getParent();
2077if (BI) {
2078assert(OtherSuccTIs.size() == 1);
2079assert(BI->getSuccessor(0) ==I1->getParent());
2080assert(BI->getSuccessor(1) == I2->getParent());
2081 }
2082
2083// In the case of an if statement, we try to hoist an invoke.
2084// FIXME: Can we define a safety predicate for CallBr?
2085// FIXME: Test case llvm/test/Transforms/SimplifyCFG/2009-06-15-InvokeCrash.ll
2086// removed in 4c923b3b3fd0ac1edebf0603265ca3ba51724937 commit?
2087if (isa<InvokeInst>(I1) && (!BI || !isSafeToHoistInvoke(BB1, BB2, I1, I2)))
2088returnfalse;
2089
2090// TODO: callbr hoisting currently disabled pending further study.
2091if (isa<CallBrInst>(I1))
2092returnfalse;
2093
2094for (BasicBlock *Succ :successors(BB1)) {
2095for (PHINode &PN : Succ->phis()) {
2096Value *BB1V = PN.getIncomingValueForBlock(BB1);
2097for (Instruction *OtherSuccTI : OtherSuccTIs) {
2098Value *BB2V = PN.getIncomingValueForBlock(OtherSuccTI->getParent());
2099if (BB1V == BB2V)
2100continue;
2101
2102// In the case of an if statement, check for
2103// passingValueIsAlwaysUndefined here because we would rather eliminate
2104// undefined control flow then converting it to a select.
2105if (!BI ||passingValueIsAlwaysUndefined(BB1V, &PN) ||
2106passingValueIsAlwaysUndefined(BB2V, &PN))
2107returnfalse;
2108 }
2109 }
2110 }
2111
2112// Hoist DbgVariableRecords attached to the terminator to match dbg.*
2113// intrinsic hoisting behaviour in hoistCommonCodeFromSuccessors.
2114hoistLockstepIdenticalDbgVariableRecords(TI, I1, OtherSuccTIs);
2115// Clone the terminator and hoist it into the pred, without any debug info.
2116Instruction *NT =I1->clone();
2117NT->insertInto(TIParent, TI->getIterator());
2118if (!NT->getType()->isVoidTy()) {
2119I1->replaceAllUsesWith(NT);
2120for (Instruction *OtherSuccTI : OtherSuccTIs)
2121 OtherSuccTI->replaceAllUsesWith(NT);
2122NT->takeName(I1);
2123 }
2124 Changed =true;
2125 NumHoistCommonInstrs += OtherSuccTIs.size() + 1;
2126
2127// Ensure terminator gets a debug location, even an unknown one, in case
2128// it involves inlinable calls.
2129SmallVector<DILocation *, 4> Locs;
2130 Locs.push_back(I1->getDebugLoc());
2131for (auto *OtherSuccTI : OtherSuccTIs)
2132 Locs.push_back(OtherSuccTI->getDebugLoc());
2133NT->setDebugLoc(DILocation::getMergedLocations(Locs));
2134
2135// PHIs created below will adopt NT's merged DebugLoc.
2136IRBuilder<NoFolder> Builder(NT);
2137
2138// In the case of an if statement, hoisting one of the terminators from our
2139// successor is a great thing. Unfortunately, the successors of the if/else
2140// blocks may have PHI nodes in them. If they do, all PHI entries for BB1/BB2
2141// must agree for all PHI nodes, so we insert select instruction to compute
2142// the final result.
2143if (BI) {
2144 std::map<std::pair<Value *, Value *>,SelectInst *> InsertedSelects;
2145for (BasicBlock *Succ :successors(BB1)) {
2146for (PHINode &PN : Succ->phis()) {
2147Value *BB1V = PN.getIncomingValueForBlock(BB1);
2148Value *BB2V = PN.getIncomingValueForBlock(BB2);
2149if (BB1V == BB2V)
2150continue;
2151
2152// These values do not agree. Insert a select instruction before NT
2153// that determines the right value.
2154SelectInst *&SI = InsertedSelects[std::make_pair(BB1V, BB2V)];
2155if (!SI) {
2156// Propagate fast-math-flags from phi node to its replacement select.
2157SI = cast<SelectInst>(Builder.CreateSelectFMF(
2158 BI->getCondition(), BB1V, BB2V,
2159 isa<FPMathOperator>(PN) ? &PN :nullptr,
2160 BB1V->getName() +"." + BB2V->getName(), BI));
2161 }
2162
2163// Make the PHI node use the select for all incoming values for BB1/BB2
2164for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
2165if (PN.getIncomingBlock(i) == BB1 || PN.getIncomingBlock(i) == BB2)
2166 PN.setIncomingValue(i, SI);
2167 }
2168 }
2169 }
2170
2171SmallVector<DominatorTree::UpdateType, 4> Updates;
2172
2173// Update any PHI nodes in our new successors.
2174for (BasicBlock *Succ :successors(BB1)) {
2175addPredecessorToBlock(Succ, TIParent, BB1);
2176if (DTU)
2177 Updates.push_back({DominatorTree::Insert, TIParent, Succ});
2178 }
2179
2180if (DTU)
2181for (BasicBlock *Succ :successors(TI))
2182 Updates.push_back({DominatorTree::Delete, TIParent, Succ});
2183
2184eraseTerminatorAndDCECond(TI);
2185if (DTU)
2186 DTU->applyUpdates(Updates);
2187return Changed;
2188}
2189
2190// Check lifetime markers.
2191staticboolisLifeTimeMarker(constInstruction *I) {
2192if (autoII = dyn_cast<IntrinsicInst>(I)) {
2193switch (II->getIntrinsicID()) {
2194default:
2195break;
2196case Intrinsic::lifetime_start:
2197case Intrinsic::lifetime_end:
2198returntrue;
2199 }
2200 }
2201returnfalse;
2202}
2203
2204// TODO: Refine this. This should avoid cases like turning constant memcpy sizes
2205// into variables.
2206staticboolreplacingOperandWithVariableIsCheap(constInstruction *I,
2207int OpIdx) {
2208// Divide/Remainder by constant is typically much cheaper than by variable.
2209if (I->isIntDivRem())
2210return OpIdx != 1;
2211return !isa<IntrinsicInst>(I);
2212}
2213
2214// All instructions in Insts belong to different blocks that all unconditionally
2215// branch to a common successor. Analyze each instruction and return true if it
2216// would be possible to sink them into their successor, creating one common
2217// instruction instead. For every value that would be required to be provided by
2218// PHI node (because an operand varies in each input block), add to PHIOperands.
2219staticboolcanSinkInstructions(
2220ArrayRef<Instruction *> Insts,
2221DenseMap<constUse *,SmallVector<Value *, 4>> &PHIOperands) {
2222// Prune out obviously bad instructions to move. Each instruction must have
2223// the same number of uses, and we check later that the uses are consistent.
2224 std::optional<unsigned> NumUses;
2225for (auto *I : Insts) {
2226// These instructions may change or break semantics if moved.
2227if (isa<PHINode>(I) ||I->isEHPad() || isa<AllocaInst>(I) ||
2228I->getType()->isTokenTy())
2229returnfalse;
2230
2231// Do not try to sink an instruction in an infinite loop - it can cause
2232// this algorithm to infinite loop.
2233if (I->getParent()->getSingleSuccessor() ==I->getParent())
2234returnfalse;
2235
2236// Conservatively return false if I is an inline-asm instruction. Sinking
2237// and merging inline-asm instructions can potentially create arguments
2238// that cannot satisfy the inline-asm constraints.
2239// If the instruction has nomerge or convergent attribute, return false.
2240if (constauto *C = dyn_cast<CallBase>(I))
2241if (C->isInlineAsm() ||C->cannotMerge() ||C->isConvergent())
2242returnfalse;
2243
2244if (!NumUses)
2245 NumUses =I->getNumUses();
2246elseif (NumUses !=I->getNumUses())
2247returnfalse;
2248 }
2249
2250constInstruction *I0 = Insts.front();
2251constauto I0MMRA =MMRAMetadata(*I0);
2252for (auto *I : Insts) {
2253if (!I->isSameOperationAs(I0,Instruction::CompareUsingIntersectedAttrs))
2254returnfalse;
2255
2256// swifterror pointers can only be used by a load or store; sinking a load
2257// or store would require introducing a select for the pointer operand,
2258// which isn't allowed for swifterror pointers.
2259if (isa<StoreInst>(I) &&I->getOperand(1)->isSwiftError())
2260returnfalse;
2261if (isa<LoadInst>(I) &&I->getOperand(0)->isSwiftError())
2262returnfalse;
2263
2264// Treat MMRAs conservatively. This pass can be quite aggressive and
2265// could drop a lot of MMRAs otherwise.
2266if (MMRAMetadata(*I) != I0MMRA)
2267returnfalse;
2268 }
2269
2270// Uses must be consistent: If I0 is used in a phi node in the sink target,
2271// then the other phi operands must match the instructions from Insts. This
2272// also has to hold true for any phi nodes that would be created as a result
2273// of sinking. Both of these cases are represented by PhiOperands.
2274for (constUse &U : I0->uses()) {
2275auto It = PHIOperands.find(&U);
2276if (It == PHIOperands.end())
2277// There may be uses in other blocks when sinking into a loop header.
2278returnfalse;
2279if (!equal(Insts, It->second))
2280returnfalse;
2281 }
2282
2283// For calls to be sinkable, they must all be indirect, or have same callee.
2284// I.e. if we have two direct calls to different callees, we don't want to
2285// turn that into an indirect call. Likewise, if we have an indirect call,
2286// and a direct call, we don't actually want to have a single indirect call.
2287if (isa<CallBase>(I0)) {
2288auto IsIndirectCall = [](constInstruction *I) {
2289return cast<CallBase>(I)->isIndirectCall();
2290 };
2291bool HaveIndirectCalls =any_of(Insts, IsIndirectCall);
2292bool AllCallsAreIndirect =all_of(Insts, IsIndirectCall);
2293if (HaveIndirectCalls) {
2294if (!AllCallsAreIndirect)
2295returnfalse;
2296 }else {
2297// All callees must be identical.
2298Value *Callee =nullptr;
2299for (constInstruction *I : Insts) {
2300Value *CurrCallee = cast<CallBase>(I)->getCalledOperand();
2301if (!Callee)
2302 Callee = CurrCallee;
2303elseif (Callee != CurrCallee)
2304returnfalse;
2305 }
2306 }
2307 }
2308
2309for (unsigned OI = 0, OE = I0->getNumOperands(); OI != OE; ++OI) {
2310Value *Op = I0->getOperand(OI);
2311if (Op->getType()->isTokenTy())
2312// Don't touch any operand of token type.
2313returnfalse;
2314
2315auto SameAsI0 = [&I0, OI](constInstruction *I) {
2316assert(I->getNumOperands() == I0->getNumOperands());
2317returnI->getOperand(OI) == I0->getOperand(OI);
2318 };
2319if (!all_of(Insts, SameAsI0)) {
2320// SROA can't speculate lifetime markers of selects/phis, and the
2321// backend may handle such lifetimes incorrectly as well (#104776).
2322// Don't sink lifetimes if it would introduce a phi on the pointer
2323// argument.
2324if (isLifeTimeMarker(I0) && OI == 1 &&
2325any_of(Insts, [](constInstruction *I) {
2326return isa<AllocaInst>(I->getOperand(1)->stripPointerCasts());
2327 }))
2328returnfalse;
2329
2330if ((isa<Constant>(Op) && !replacingOperandWithVariableIsCheap(I0, OI)) ||
2331 !canReplaceOperandWithVariable(I0, OI))
2332// We can't create a PHI from this GEP.
2333returnfalse;
2334auto &Ops = PHIOperands[&I0->getOperandUse(OI)];
2335for (auto *I : Insts)
2336 Ops.push_back(I->getOperand(OI));
2337 }
2338 }
2339returntrue;
2340}
2341
2342// Assuming canSinkInstructions(Blocks) has returned true, sink the last
2343// instruction of every block in Blocks to their common successor, commoning
2344// into one instruction.
2345staticvoidsinkLastInstruction(ArrayRef<BasicBlock*>Blocks) {
2346auto *BBEnd =Blocks[0]->getTerminator()->getSuccessor(0);
2347
2348// canSinkInstructions returning true guarantees that every block has at
2349// least one non-terminator instruction.
2350SmallVector<Instruction*,4> Insts;
2351for (auto *BB :Blocks) {
2352Instruction *I = BB->getTerminator();
2353do {
2354I =I->getPrevNode();
2355 }while (isa<DbgInfoIntrinsic>(I) &&I != &BB->front());
2356if (!isa<DbgInfoIntrinsic>(I))
2357 Insts.push_back(I);
2358 }
2359
2360// We don't need to do any more checking here; canSinkInstructions should
2361// have done it all for us.
2362SmallVector<Value*, 4> NewOperands;
2363Instruction *I0 = Insts.front();
2364for (unsigned O = 0, E = I0->getNumOperands(); O != E; ++O) {
2365// This check is different to that in canSinkInstructions. There, we
2366// cared about the global view once simplifycfg (and instcombine) have
2367// completed - it takes into account PHIs that become trivially
2368// simplifiable. However here we need a more local view; if an operand
2369// differs we create a PHI and rely on instcombine to clean up the very
2370// small mess we may make.
2371bool NeedPHI =any_of(Insts, [&I0, O](constInstruction *I) {
2372returnI->getOperand(O) != I0->getOperand(O);
2373 });
2374if (!NeedPHI) {
2375 NewOperands.push_back(I0->getOperand(O));
2376continue;
2377 }
2378
2379// Create a new PHI in the successor block and populate it.
2380auto *Op = I0->getOperand(O);
2381assert(!Op->getType()->isTokenTy() &&"Can't PHI tokens!");
2382auto *PN =
2383PHINode::Create(Op->getType(), Insts.size(),Op->getName() +".sink");
2384 PN->insertBefore(BBEnd->begin());
2385for (auto *I : Insts)
2386 PN->addIncoming(I->getOperand(O),I->getParent());
2387 NewOperands.push_back(PN);
2388 }
2389
2390// Arbitrarily use I0 as the new "common" instruction; remap its operands
2391// and move it to the start of the successor block.
2392for (unsigned O = 0, E = I0->getNumOperands(); O != E; ++O)
2393 I0->getOperandUse(O).set(NewOperands[O]);
2394
2395 I0->moveBefore(*BBEnd, BBEnd->getFirstInsertionPt());
2396
2397// Update metadata and IR flags, and merge debug locations.
2398for (auto *I : Insts)
2399if (I != I0) {
2400// The debug location for the "common" instruction is the merged locations
2401// of all the commoned instructions. We start with the original location
2402// of the "common" instruction and iteratively merge each location in the
2403// loop below.
2404// This is an N-way merge, which will be inefficient if I0 is a CallInst.
2405// However, as N-way merge for CallInst is rare, so we use simplified API
2406// instead of using complex API for N-way merge.
2407 I0->applyMergedLocation(I0->getDebugLoc(),I->getDebugLoc());
2408combineMetadataForCSE(I0,I,true);
2409 I0->andIRFlags(I);
2410if (auto *CB = dyn_cast<CallBase>(I0)) {
2411boolSuccess = CB->tryIntersectAttributes(cast<CallBase>(I));
2412assert(Success &&"We should not be trying to sink callbases "
2413"with non-intersectable attributes");
2414// For NDEBUG Compile.
2415 (void)Success;
2416 }
2417 }
2418
2419for (User *U :make_early_inc_range(I0->users())) {
2420// canSinkLastInstruction checked that all instructions are only used by
2421// phi nodes in a way that allows replacing the phi node with the common
2422// instruction.
2423auto *PN = cast<PHINode>(U);
2424 PN->replaceAllUsesWith(I0);
2425 PN->eraseFromParent();
2426 }
2427
2428// Finally nuke all instructions apart from the common instruction.
2429for (auto *I : Insts) {
2430if (I == I0)
2431continue;
2432// The remaining uses are debug users, replace those with the common inst.
2433// In most (all?) cases this just introduces a use-before-def.
2434assert(I->user_empty() &&"Inst unexpectedly still has non-dbg users");
2435I->replaceAllUsesWith(I0);
2436I->eraseFromParent();
2437 }
2438}
2439
2440/// Check whether BB's predecessors end with unconditional branches. If it is
2441/// true, sink any common code from the predecessors to BB.
2442staticboolsinkCommonCodeFromPredecessors(BasicBlock *BB,
2443DomTreeUpdater *DTU) {
2444// We support two situations:
2445// (1) all incoming arcs are unconditional
2446// (2) there are non-unconditional incoming arcs
2447//
2448// (2) is very common in switch defaults and
2449// else-if patterns;
2450//
2451// if (a) f(1);
2452// else if (b) f(2);
2453//
2454// produces:
2455//
2456// [if]
2457// / \
2458 // [f(1)] [if]
2459// | | \
2460 // | | |
2461// | [f(2)]|
2462// \ | /
2463// [ end ]
2464//
2465// [end] has two unconditional predecessor arcs and one conditional. The
2466// conditional refers to the implicit empty 'else' arc. This conditional
2467// arc can also be caused by an empty default block in a switch.
2468//
2469// In this case, we attempt to sink code from all *unconditional* arcs.
2470// If we can sink instructions from these arcs (determined during the scan
2471// phase below) we insert a common successor for all unconditional arcs and
2472// connect that to [end], to enable sinking:
2473//
2474// [if]
2475// / \
2476 // [x(1)] [if]
2477// | | \
2478 // | | \
2479 // | [x(2)] |
2480// \ / |
2481// [sink.split] |
2482// \ /
2483// [ end ]
2484//
2485SmallVector<BasicBlock*,4> UnconditionalPreds;
2486bool HaveNonUnconditionalPredecessors =false;
2487for (auto *PredBB :predecessors(BB)) {
2488auto *PredBr = dyn_cast<BranchInst>(PredBB->getTerminator());
2489if (PredBr && PredBr->isUnconditional())
2490 UnconditionalPreds.push_back(PredBB);
2491else
2492 HaveNonUnconditionalPredecessors =true;
2493 }
2494if (UnconditionalPreds.size() < 2)
2495returnfalse;
2496
2497// We take a two-step approach to tail sinking. First we scan from the end of
2498// each block upwards in lockstep. If the n'th instruction from the end of each
2499// block can be sunk, those instructions are added to ValuesToSink and we
2500// carry on. If we can sink an instruction but need to PHI-merge some operands
2501// (because they're not identical in each instruction) we add these to
2502// PHIOperands.
2503// We prepopulate PHIOperands with the phis that already exist in BB.
2504DenseMap<const Use *, SmallVector<Value *, 4>> PHIOperands;
2505for (PHINode &PN : BB->phis()) {
2506SmallDenseMap<BasicBlock *, const Use *, 4> IncomingVals;
2507for (constUse &U : PN.incoming_values())
2508 IncomingVals.insert({PN.getIncomingBlock(U), &U});
2509auto &Ops = PHIOperands[IncomingVals[UnconditionalPreds[0]]];
2510for (BasicBlock *Pred : UnconditionalPreds)
2511 Ops.push_back(*IncomingVals[Pred]);
2512 }
2513
2514int ScanIdx = 0;
2515SmallPtrSet<Value*,4> InstructionsToSink;
2516 LockstepReverseIterator LRI(UnconditionalPreds);
2517while (LRI.isValid() &&
2518canSinkInstructions(*LRI, PHIOperands)) {
2519LLVM_DEBUG(dbgs() <<"SINK: instruction can be sunk: " << *(*LRI)[0]
2520 <<"\n");
2521 InstructionsToSink.insert((*LRI).begin(), (*LRI).end());
2522 ++ScanIdx;
2523 --LRI;
2524 }
2525
2526// If no instructions can be sunk, early-return.
2527if (ScanIdx == 0)
2528returnfalse;
2529
2530bool followedByDeoptOrUnreachable =IsBlockFollowedByDeoptOrUnreachable(BB);
2531
2532if (!followedByDeoptOrUnreachable) {
2533// Check whether this is the pointer operand of a load/store.
2534auto IsMemOperand = [](Use &U) {
2535auto *I = cast<Instruction>(U.getUser());
2536if (isa<LoadInst>(I))
2537return U.getOperandNo() ==LoadInst::getPointerOperandIndex();
2538if (isa<StoreInst>(I))
2539return U.getOperandNo() ==StoreInst::getPointerOperandIndex();
2540returnfalse;
2541 };
2542
2543// Okay, we *could* sink last ScanIdx instructions. But how many can we
2544// actually sink before encountering instruction that is unprofitable to
2545// sink?
2546auto ProfitableToSinkInstruction = [&](LockstepReverseIterator &LRI) {
2547unsigned NumPHIInsts = 0;
2548for (Use &U : (*LRI)[0]->operands()) {
2549auto It = PHIOperands.find(&U);
2550if (It != PHIOperands.end() && !all_of(It->second, [&](Value *V) {
2551 return InstructionsToSink.contains(V);
2552 })) {
2553 ++NumPHIInsts;
2554// Do not separate a load/store from the gep producing the address.
2555// The gep can likely be folded into the load/store as an addressing
2556// mode. Additionally, a load of a gep is easier to analyze than a
2557// load of a phi.
2558if (IsMemOperand(U) &&
2559any_of(It->second, [](Value *V) { return isa<GEPOperator>(V); }))
2560returnfalse;
2561// FIXME: this check is overly optimistic. We may end up not sinking
2562// said instruction, due to the very same profitability check.
2563// See @creating_too_many_phis in sink-common-code.ll.
2564 }
2565 }
2566LLVM_DEBUG(dbgs() <<"SINK: #phi insts: " << NumPHIInsts <<"\n");
2567return NumPHIInsts <= 1;
2568 };
2569
2570// We've determined that we are going to sink last ScanIdx instructions,
2571// and recorded them in InstructionsToSink. Now, some instructions may be
2572// unprofitable to sink. But that determination depends on the instructions
2573// that we are going to sink.
2574
2575// First, forward scan: find the first instruction unprofitable to sink,
2576// recording all the ones that are profitable to sink.
2577// FIXME: would it be better, after we detect that not all are profitable.
2578// to either record the profitable ones, or erase the unprofitable ones?
2579// Maybe we need to choose (at runtime) the one that will touch least
2580// instrs?
2581 LRI.reset();
2582intIdx = 0;
2583SmallPtrSet<Value *, 4> InstructionsProfitableToSink;
2584while (Idx < ScanIdx) {
2585if (!ProfitableToSinkInstruction(LRI)) {
2586// Too many PHIs would be created.
2587LLVM_DEBUG(
2588dbgs() <<"SINK: stopping here, too many PHIs would be created!\n");
2589break;
2590 }
2591 InstructionsProfitableToSink.insert((*LRI).begin(), (*LRI).end());
2592 --LRI;
2593 ++Idx;
2594 }
2595
2596// If no instructions can be sunk, early-return.
2597if (Idx == 0)
2598returnfalse;
2599
2600// Did we determine that (only) some instructions are unprofitable to sink?
2601if (Idx < ScanIdx) {
2602// Okay, some instructions are unprofitable.
2603 ScanIdx =Idx;
2604 InstructionsToSink = InstructionsProfitableToSink;
2605
2606// But, that may make other instructions unprofitable, too.
2607// So, do a backward scan, do any earlier instructions become
2608// unprofitable?
2609assert(
2610 !ProfitableToSinkInstruction(LRI) &&
2611"We already know that the last instruction is unprofitable to sink");
2612 ++LRI;
2613 --Idx;
2614while (Idx >= 0) {
2615// If we detect that an instruction becomes unprofitable to sink,
2616// all earlier instructions won't be sunk either,
2617// so preemptively keep InstructionsProfitableToSink in sync.
2618// FIXME: is this the most performant approach?
2619for (auto *I : *LRI)
2620 InstructionsProfitableToSink.erase(I);
2621if (!ProfitableToSinkInstruction(LRI)) {
2622// Everything starting with this instruction won't be sunk.
2623 ScanIdx =Idx;
2624 InstructionsToSink = InstructionsProfitableToSink;
2625 }
2626 ++LRI;
2627 --Idx;
2628 }
2629 }
2630
2631// If no instructions can be sunk, early-return.
2632if (ScanIdx == 0)
2633returnfalse;
2634 }
2635
2636bool Changed =false;
2637
2638if (HaveNonUnconditionalPredecessors) {
2639if (!followedByDeoptOrUnreachable) {
2640// It is always legal to sink common instructions from unconditional
2641// predecessors. However, if not all predecessors are unconditional,
2642// this transformation might be pessimizing. So as a rule of thumb,
2643// don't do it unless we'd sink at least one non-speculatable instruction.
2644// See https://bugs.llvm.org/show_bug.cgi?id=30244
2645 LRI.reset();
2646intIdx = 0;
2647bool Profitable =false;
2648while (Idx < ScanIdx) {
2649if (!isSafeToSpeculativelyExecute((*LRI)[0])) {
2650 Profitable =true;
2651break;
2652 }
2653 --LRI;
2654 ++Idx;
2655 }
2656if (!Profitable)
2657returnfalse;
2658 }
2659
2660LLVM_DEBUG(dbgs() <<"SINK: Splitting edge\n");
2661// We have a conditional edge and we're going to sink some instructions.
2662// Insert a new block postdominating all blocks we're going to sink from.
2663if (!SplitBlockPredecessors(BB, UnconditionalPreds,".sink.split", DTU))
2664// Edges couldn't be split.
2665returnfalse;
2666 Changed =true;
2667 }
2668
2669// Now that we've analyzed all potential sinking candidates, perform the
2670// actual sink. We iteratively sink the last non-terminator of the source
2671// blocks into their common successor unless doing so would require too
2672// many PHI instructions to be generated (currently only one PHI is allowed
2673// per sunk instruction).
2674//
2675// We can use InstructionsToSink to discount values needing PHI-merging that will
2676// actually be sunk in a later iteration. This allows us to be more
2677// aggressive in what we sink. This does allow a false positive where we
2678// sink presuming a later value will also be sunk, but stop half way through
2679// and never actually sink it which means we produce more PHIs than intended.
2680// This is unlikely in practice though.
2681int SinkIdx = 0;
2682for (; SinkIdx != ScanIdx; ++SinkIdx) {
2683LLVM_DEBUG(dbgs() <<"SINK: Sink: "
2684 << *UnconditionalPreds[0]->getTerminator()->getPrevNode()
2685 <<"\n");
2686
2687// Because we've sunk every instruction in turn, the current instruction to
2688// sink is always at index 0.
2689 LRI.reset();
2690
2691sinkLastInstruction(UnconditionalPreds);
2692 NumSinkCommonInstrs++;
2693 Changed =true;
2694 }
2695if (SinkIdx != 0)
2696 ++NumSinkCommonCode;
2697return Changed;
2698}
2699
2700namespace{
2701
2702structCompatibleSets {
2703usingSetTy =SmallVector<InvokeInst *, 2>;
2704
2705SmallVector<SetTy, 1> Sets;
2706
2707staticbool shouldBelongToSameSet(ArrayRef<InvokeInst *> Invokes);
2708
2709 SetTy &getCompatibleSet(InvokeInst *II);
2710
2711void insert(InvokeInst *II);
2712};
2713
2714CompatibleSets::SetTy &CompatibleSets::getCompatibleSet(InvokeInst *II) {
2715// Perform a linear scan over all the existing sets, see if the new `invoke`
2716// is compatible with any particular set. Since we know that all the `invokes`
2717// within a set are compatible, only check the first `invoke` in each set.
2718// WARNING: at worst, this has quadratic complexity.
2719for (CompatibleSets::SetTy &Set : Sets) {
2720if (CompatibleSets::shouldBelongToSameSet({Set.front(),II}))
2721returnSet;
2722 }
2723
2724// Otherwise, we either had no sets yet, or this invoke forms a new set.
2725return Sets.emplace_back();
2726}
2727
2728void CompatibleSets::insert(InvokeInst *II) {
2729 getCompatibleSet(II).emplace_back(II);
2730}
2731
2732bool CompatibleSets::shouldBelongToSameSet(ArrayRef<InvokeInst *> Invokes) {
2733assert(Invokes.size() == 2 &&"Always called with exactly two candidates.");
2734
2735// Can we theoretically merge these `invoke`s?
2736auto IsIllegalToMerge = [](InvokeInst *II) {
2737returnII->cannotMerge() ||II->isInlineAsm();
2738 };
2739if (any_of(Invokes, IsIllegalToMerge))
2740returnfalse;
2741
2742// Either both `invoke`s must be direct,
2743// or both `invoke`s must be indirect.
2744auto IsIndirectCall = [](InvokeInst *II) {returnII->isIndirectCall(); };
2745bool HaveIndirectCalls =any_of(Invokes, IsIndirectCall);
2746bool AllCallsAreIndirect =all_of(Invokes, IsIndirectCall);
2747if (HaveIndirectCalls) {
2748if (!AllCallsAreIndirect)
2749returnfalse;
2750 }else {
2751// All callees must be identical.
2752Value *Callee =nullptr;
2753for (InvokeInst *II : Invokes) {
2754Value *CurrCallee =II->getCalledOperand();
2755assert(CurrCallee &&"There is always a called operand.");
2756if (!Callee)
2757Callee = CurrCallee;
2758elseif (Callee != CurrCallee)
2759returnfalse;
2760 }
2761 }
2762
2763// Either both `invoke`s must not have a normal destination,
2764// or both `invoke`s must have a normal destination,
2765auto HasNormalDest = [](InvokeInst *II) {
2766return !isa<UnreachableInst>(II->getNormalDest()->getFirstNonPHIOrDbg());
2767 };
2768if (any_of(Invokes, HasNormalDest)) {
2769// Do not merge `invoke` that does not have a normal destination with one
2770// that does have a normal destination, even though doing so would be legal.
2771if (!all_of(Invokes, HasNormalDest))
2772returnfalse;
2773
2774// All normal destinations must be identical.
2775BasicBlock *NormalBB =nullptr;
2776for (InvokeInst *II : Invokes) {
2777BasicBlock *CurrNormalBB =II->getNormalDest();
2778assert(CurrNormalBB &&"There is always a 'continue to' basic block.");
2779if (!NormalBB)
2780 NormalBB = CurrNormalBB;
2781elseif (NormalBB != CurrNormalBB)
2782returnfalse;
2783 }
2784
2785// In the normal destination, the incoming values for these two `invoke`s
2786// must be compatible.
2787SmallPtrSet<Value *, 16> EquivalenceSet(Invokes.begin(), Invokes.end());
2788if (!incomingValuesAreCompatible(
2789 NormalBB, {Invokes[0]->getParent(), Invokes[1]->getParent()},
2790 &EquivalenceSet))
2791returnfalse;
2792 }
2793
2794#ifndef NDEBUG
2795// All unwind destinations must be identical.
2796// We know that because we have started from said unwind destination.
2797BasicBlock *UnwindBB =nullptr;
2798for (InvokeInst *II : Invokes) {
2799BasicBlock *CurrUnwindBB =II->getUnwindDest();
2800assert(CurrUnwindBB &&"There is always an 'unwind to' basic block.");
2801if (!UnwindBB)
2802 UnwindBB = CurrUnwindBB;
2803else
2804assert(UnwindBB == CurrUnwindBB &&"Unexpected unwind destination.");
2805 }
2806#endif
2807
2808// In the unwind destination, the incoming values for these two `invoke`s
2809// must be compatible.
2810if (!incomingValuesAreCompatible(
2811 Invokes.front()->getUnwindDest(),
2812 {Invokes[0]->getParent(), Invokes[1]->getParent()}))
2813returnfalse;
2814
2815// Ignoring arguments, these `invoke`s must be identical,
2816// including operand bundles.
2817constInvokeInst *II0 = Invokes.front();
2818for (auto *II : Invokes.drop_front())
2819if (!II->isSameOperationAs(II0,Instruction::CompareUsingIntersectedAttrs))
2820returnfalse;
2821
2822// Can we theoretically form the data operands for the merged `invoke`?
2823auto IsIllegalToMergeArguments = [](auto Ops) {
2824Use &U0 = std::get<0>(Ops);
2825Use &U1 = std::get<1>(Ops);
2826if (U0 == U1)
2827returnfalse;
2828return U0->getType()->isTokenTy() ||
2829 !canReplaceOperandWithVariable(cast<Instruction>(U0.getUser()),
2830 U0.getOperandNo());
2831 };
2832assert(Invokes.size() == 2 &&"Always called with exactly two candidates.");
2833if (any_of(zip(Invokes[0]->data_ops(), Invokes[1]->data_ops()),
2834 IsIllegalToMergeArguments))
2835returnfalse;
2836
2837returntrue;
2838}
2839
2840}// namespace
2841
2842// Merge all invokes in the provided set, all of which are compatible
2843// as per the `CompatibleSets::shouldBelongToSameSet()`.
2844staticvoidmergeCompatibleInvokesImpl(ArrayRef<InvokeInst *> Invokes,
2845DomTreeUpdater *DTU) {
2846assert(Invokes.size() >= 2 &&"Must have at least two invokes to merge.");
2847
2848SmallVector<DominatorTree::UpdateType, 8> Updates;
2849if (DTU)
2850 Updates.reserve(2 + 3 * Invokes.size());
2851
2852bool HasNormalDest =
2853 !isa<UnreachableInst>(Invokes[0]->getNormalDest()->getFirstNonPHIOrDbg());
2854
2855// Clone one of the invokes into a new basic block.
2856// Since they are all compatible, it doesn't matter which invoke is cloned.
2857InvokeInst *MergedInvoke = [&Invokes, HasNormalDest]() {
2858InvokeInst *II0 = Invokes.front();
2859BasicBlock *II0BB = II0->getParent();
2860BasicBlock *InsertBeforeBlock =
2861 II0->getParent()->getIterator()->getNextNode();
2862Function *Func = II0BB->getParent();
2863LLVMContext &Ctx = II0->getContext();
2864
2865BasicBlock *MergedInvokeBB =BasicBlock::Create(
2866 Ctx, II0BB->getName() +".invoke", Func, InsertBeforeBlock);
2867
2868auto *MergedInvoke = cast<InvokeInst>(II0->clone());
2869// NOTE: all invokes have the same attributes, so no handling needed.
2870 MergedInvoke->insertInto(MergedInvokeBB, MergedInvokeBB->end());
2871
2872if (!HasNormalDest) {
2873// This set does not have a normal destination,
2874// so just form a new block with unreachable terminator.
2875BasicBlock *MergedNormalDest =BasicBlock::Create(
2876 Ctx, II0BB->getName() +".cont", Func, InsertBeforeBlock);
2877newUnreachableInst(Ctx, MergedNormalDest);
2878 MergedInvoke->setNormalDest(MergedNormalDest);
2879 }
2880
2881// The unwind destination, however, remainds identical for all invokes here.
2882
2883return MergedInvoke;
2884 }();
2885
2886if (DTU) {
2887// Predecessor blocks that contained these invokes will now branch to
2888// the new block that contains the merged invoke, ...
2889for (InvokeInst *II : Invokes)
2890 Updates.push_back(
2891 {DominatorTree::Insert,II->getParent(), MergedInvoke->getParent()});
2892
2893// ... which has the new `unreachable` block as normal destination,
2894// or unwinds to the (same for all `invoke`s in this set) `landingpad`,
2895for (BasicBlock *SuccBBOfMergedInvoke :successors(MergedInvoke))
2896 Updates.push_back({DominatorTree::Insert, MergedInvoke->getParent(),
2897 SuccBBOfMergedInvoke});
2898
2899// Since predecessor blocks now unconditionally branch to a new block,
2900// they no longer branch to their original successors.
2901for (InvokeInst *II : Invokes)
2902for (BasicBlock *SuccOfPredBB :successors(II->getParent()))
2903 Updates.push_back(
2904 {DominatorTree::Delete,II->getParent(), SuccOfPredBB});
2905 }
2906
2907bool IsIndirectCall = Invokes[0]->isIndirectCall();
2908
2909// Form the merged operands for the merged invoke.
2910for (Use &U : MergedInvoke->operands()) {
2911// Only PHI together the indirect callees and data operands.
2912if (MergedInvoke->isCallee(&U)) {
2913if (!IsIndirectCall)
2914continue;
2915 }elseif (!MergedInvoke->isDataOperand(&U))
2916continue;
2917
2918// Don't create trivial PHI's with all-identical incoming values.
2919bool NeedPHI =any_of(Invokes, [&U](InvokeInst *II) {
2920returnII->getOperand(U.getOperandNo()) !=U.get();
2921 });
2922if (!NeedPHI)
2923continue;
2924
2925// Form a PHI out of all the data ops under this index.
2926PHINode *PN =PHINode::Create(
2927U->getType(),/*NumReservedValues=*/Invokes.size(),"", MergedInvoke->getIterator());
2928for (InvokeInst *II : Invokes)
2929 PN->addIncoming(II->getOperand(U.getOperandNo()),II->getParent());
2930
2931U.set(PN);
2932 }
2933
2934// We've ensured that each PHI node has compatible (identical) incoming values
2935// when coming from each of the `invoke`s in the current merge set,
2936// so update the PHI nodes accordingly.
2937for (BasicBlock *Succ :successors(MergedInvoke))
2938addPredecessorToBlock(Succ,/*NewPred=*/MergedInvoke->getParent(),
2939/*ExistPred=*/Invokes.front()->getParent());
2940
2941// And finally, replace the original `invoke`s with an unconditional branch
2942// to the block with the merged `invoke`. Also, give that merged `invoke`
2943// the merged debugloc of all the original `invoke`s.
2944DILocation *MergedDebugLoc =nullptr;
2945for (InvokeInst *II : Invokes) {
2946// Compute the debug location common to all the original `invoke`s.
2947if (!MergedDebugLoc)
2948 MergedDebugLoc =II->getDebugLoc();
2949else
2950 MergedDebugLoc =
2951DILocation::getMergedLocation(MergedDebugLoc,II->getDebugLoc());
2952
2953// And replace the old `invoke` with an unconditionally branch
2954// to the block with the merged `invoke`.
2955for (BasicBlock *OrigSuccBB :successors(II->getParent()))
2956 OrigSuccBB->removePredecessor(II->getParent());
2957auto *BI =BranchInst::Create(MergedInvoke->getParent(),II->getParent());
2958// The unconditional branch is part of the replacement for the original
2959// invoke, so should use its DebugLoc.
2960 BI->setDebugLoc(II->getDebugLoc());
2961boolSuccess = MergedInvoke->tryIntersectAttributes(II);
2962assert(Success &&"Merged invokes with incompatible attributes");
2963// For NDEBUG Compile
2964 (void)Success;
2965II->replaceAllUsesWith(MergedInvoke);
2966II->eraseFromParent();
2967 ++NumInvokesMerged;
2968 }
2969 MergedInvoke->setDebugLoc(MergedDebugLoc);
2970 ++NumInvokeSetsFormed;
2971
2972if (DTU)
2973 DTU->applyUpdates(Updates);
2974}
2975
2976/// If this block is a `landingpad` exception handling block, categorize all
2977/// the predecessor `invoke`s into sets, with all `invoke`s in each set
2978/// being "mergeable" together, and then merge invokes in each set together.
2979///
2980/// This is a weird mix of hoisting and sinking. Visually, it goes from:
2981/// [...] [...]
2982/// | |
2983/// [invoke0] [invoke1]
2984/// / \ / \
2985/// [cont0] [landingpad] [cont1]
2986/// to:
2987/// [...] [...]
2988/// \ /
2989/// [invoke]
2990/// / \
2991/// [cont] [landingpad]
2992///
2993/// But of course we can only do that if the invokes share the `landingpad`,
2994/// edges invoke0->cont0 and invoke1->cont1 are "compatible",
2995/// and the invoked functions are "compatible".
2996staticboolmergeCompatibleInvokes(BasicBlock *BB,DomTreeUpdater *DTU) {
2997if (!EnableMergeCompatibleInvokes)
2998returnfalse;
2999
3000bool Changed =false;
3001
3002// FIXME: generalize to all exception handling blocks?
3003if (!BB->isLandingPad())
3004return Changed;
3005
3006 CompatibleSets Grouper;
3007
3008// Record all the predecessors of this `landingpad`. As per verifier,
3009// the only allowed predecessor is the unwind edge of an `invoke`.
3010// We want to group "compatible" `invokes` into the same set to be merged.
3011for (BasicBlock *PredBB :predecessors(BB))
3012 Grouper.insert(cast<InvokeInst>(PredBB->getTerminator()));
3013
3014// And now, merge `invoke`s that were grouped togeter.
3015for (ArrayRef<InvokeInst *> Invokes : Grouper.Sets) {
3016if (Invokes.size() < 2)
3017continue;
3018 Changed =true;
3019mergeCompatibleInvokesImpl(Invokes, DTU);
3020 }
3021
3022return Changed;
3023}
3024
3025namespace{
3026/// Track ephemeral values, which should be ignored for cost-modelling
3027/// purposes. Requires walking instructions in reverse order.
3028classEphemeralValueTracker {
3029SmallPtrSet<const Instruction *, 32> EphValues;
3030
3031bool isEphemeral(constInstruction *I) {
3032if (isa<AssumeInst>(I))
3033returntrue;
3034return !I->mayHaveSideEffects() && !I->isTerminator() &&
3035all_of(I->users(), [&](constUser *U) {
3036 return EphValues.count(cast<Instruction>(U));
3037 });
3038 }
3039
3040public:
3041bool track(constInstruction *I) {
3042if (isEphemeral(I)) {
3043 EphValues.insert(I);
3044returntrue;
3045 }
3046returnfalse;
3047 }
3048
3049boolcontains(constInstruction *I) const{return EphValues.contains(I); }
3050};
3051}// namespace
3052
3053/// Determine if we can hoist sink a sole store instruction out of a
3054/// conditional block.
3055///
3056/// We are looking for code like the following:
3057/// BrBB:
3058/// store i32 %add, i32* %arrayidx2
3059/// ... // No other stores or function calls (we could be calling a memory
3060/// ... // function).
3061/// %cmp = icmp ult %x, %y
3062/// br i1 %cmp, label %EndBB, label %ThenBB
3063/// ThenBB:
3064/// store i32 %add5, i32* %arrayidx2
3065/// br label EndBB
3066/// EndBB:
3067/// ...
3068/// We are going to transform this into:
3069/// BrBB:
3070/// store i32 %add, i32* %arrayidx2
3071/// ... //
3072/// %cmp = icmp ult %x, %y
3073/// %add.add5 = select i1 %cmp, i32 %add, %add5
3074/// store i32 %add.add5, i32* %arrayidx2
3075/// ...
3076///
3077/// \return The pointer to the value of the previous store if the store can be
3078/// hoisted into the predecessor block. 0 otherwise.
3079staticValue *isSafeToSpeculateStore(Instruction *I,BasicBlock *BrBB,
3080BasicBlock *StoreBB,BasicBlock *EndBB) {
3081StoreInst *StoreToHoist = dyn_cast<StoreInst>(I);
3082if (!StoreToHoist)
3083returnnullptr;
3084
3085// Volatile or atomic.
3086if (!StoreToHoist->isSimple())
3087returnnullptr;
3088
3089Value *StorePtr = StoreToHoist->getPointerOperand();
3090Type *StoreTy = StoreToHoist->getValueOperand()->getType();
3091
3092// Look for a store to the same pointer in BrBB.
3093unsigned MaxNumInstToLookAt = 9;
3094// Skip pseudo probe intrinsic calls which are not really killing any memory
3095// accesses.
3096for (Instruction &CurI :reverse(BrBB->instructionsWithoutDebug(true))) {
3097if (!MaxNumInstToLookAt)
3098break;
3099 --MaxNumInstToLookAt;
3100
3101// Could be calling an instruction that affects memory like free().
3102if (CurI.mayWriteToMemory() && !isa<StoreInst>(CurI))
3103returnnullptr;
3104
3105if (auto *SI = dyn_cast<StoreInst>(&CurI)) {
3106// Found the previous store to same location and type. Make sure it is
3107// simple, to avoid introducing a spurious non-atomic write after an
3108// atomic write.
3109if (SI->getPointerOperand() == StorePtr &&
3110 SI->getValueOperand()->getType() == StoreTy && SI->isSimple() &&
3111 SI->getAlign() >= StoreToHoist->getAlign())
3112// Found the previous store, return its value operand.
3113return SI->getValueOperand();
3114returnnullptr;// Unknown store.
3115 }
3116
3117if (auto *LI = dyn_cast<LoadInst>(&CurI)) {
3118if (LI->getPointerOperand() == StorePtr && LI->getType() == StoreTy &&
3119 LI->isSimple() && LI->getAlign() >= StoreToHoist->getAlign()) {
3120Value *Obj =getUnderlyingObject(StorePtr);
3121bool ExplicitlyDereferenceableOnly;
3122if (isWritableObject(Obj, ExplicitlyDereferenceableOnly) &&
3123 !PointerMayBeCaptured(Obj,/*ReturnCaptures=*/false,
3124/*StoreCaptures=*/true) &&
3125 (!ExplicitlyDereferenceableOnly ||
3126isDereferenceablePointer(StorePtr, StoreTy,
3127 LI->getDataLayout()))) {
3128// Found a previous load, return it.
3129return LI;
3130 }
3131 }
3132// The load didn't work out, but we may still find a store.
3133 }
3134 }
3135
3136returnnullptr;
3137}
3138
3139/// Estimate the cost of the insertion(s) and check that the PHI nodes can be
3140/// converted to selects.
3141staticboolvalidateAndCostRequiredSelects(BasicBlock *BB,BasicBlock *ThenBB,
3142BasicBlock *EndBB,
3143unsigned &SpeculatedInstructions,
3144InstructionCost &Cost,
3145constTargetTransformInfo &TTI) {
3146TargetTransformInfo::TargetCostKindCostKind =
3147 BB->getParent()->hasMinSize()
3148 ?TargetTransformInfo::TCK_CodeSize
3149 :TargetTransformInfo::TCK_SizeAndLatency;
3150
3151bool HaveRewritablePHIs =false;
3152for (PHINode &PN : EndBB->phis()) {
3153Value *OrigV = PN.getIncomingValueForBlock(BB);
3154Value *ThenV = PN.getIncomingValueForBlock(ThenBB);
3155
3156// FIXME: Try to remove some of the duplication with
3157// hoistCommonCodeFromSuccessors. Skip PHIs which are trivial.
3158if (ThenV == OrigV)
3159continue;
3160
3161Cost +=TTI.getCmpSelInstrCost(Instruction::Select, PN.getType(),nullptr,
3162CmpInst::BAD_ICMP_PREDICATE,CostKind);
3163
3164// Don't convert to selects if we could remove undefined behavior instead.
3165if (passingValueIsAlwaysUndefined(OrigV, &PN) ||
3166passingValueIsAlwaysUndefined(ThenV, &PN))
3167returnfalse;
3168
3169 HaveRewritablePHIs =true;
3170ConstantExpr *OrigCE = dyn_cast<ConstantExpr>(OrigV);
3171ConstantExpr *ThenCE = dyn_cast<ConstantExpr>(ThenV);
3172if (!OrigCE && !ThenCE)
3173continue;// Known cheap (FIXME: Maybe not true for aggregates).
3174
3175InstructionCost OrigCost = OrigCE ?computeSpeculationCost(OrigCE,TTI) : 0;
3176InstructionCost ThenCost = ThenCE ?computeSpeculationCost(ThenCE,TTI) : 0;
3177InstructionCost MaxCost =
3178 2 *PHINodeFoldingThreshold *TargetTransformInfo::TCC_Basic;
3179if (OrigCost + ThenCost > MaxCost)
3180returnfalse;
3181
3182// Account for the cost of an unfolded ConstantExpr which could end up
3183// getting expanded into Instructions.
3184// FIXME: This doesn't account for how many operations are combined in the
3185// constant expression.
3186 ++SpeculatedInstructions;
3187if (SpeculatedInstructions > 1)
3188returnfalse;
3189 }
3190
3191return HaveRewritablePHIs;
3192}
3193
3194staticboolisProfitableToSpeculate(constBranchInst *BI,
3195 std::optional<bool> Invert,
3196constTargetTransformInfo &TTI) {
3197// If the branch is non-unpredictable, and is predicted to *not* branch to
3198// the `then` block, then avoid speculating it.
3199if (BI->getMetadata(LLVMContext::MD_unpredictable))
3200returntrue;
3201
3202uint64_t TWeight, FWeight;
3203if (!extractBranchWeights(*BI, TWeight, FWeight) || (TWeight + FWeight) == 0)
3204returntrue;
3205
3206if (!Invert.has_value())
3207returnfalse;
3208
3209uint64_t EndWeight = *Invert ? TWeight : FWeight;
3210BranchProbability BIEndProb =
3211BranchProbability::getBranchProbability(EndWeight, TWeight + FWeight);
3212BranchProbability Likely =TTI.getPredictableBranchThreshold();
3213return BIEndProb < Likely;
3214}
3215
3216/// Speculate a conditional basic block flattening the CFG.
3217///
3218/// Note that this is a very risky transform currently. Speculating
3219/// instructions like this is most often not desirable. Instead, there is an MI
3220/// pass which can do it with full awareness of the resource constraints.
3221/// However, some cases are "obvious" and we should do directly. An example of
3222/// this is speculating a single, reasonably cheap instruction.
3223///
3224/// There is only one distinct advantage to flattening the CFG at the IR level:
3225/// it makes very common but simplistic optimizations such as are common in
3226/// instcombine and the DAG combiner more powerful by removing CFG edges and
3227/// modeling their effects with easier to reason about SSA value graphs.
3228///
3229///
3230/// An illustration of this transform is turning this IR:
3231/// \code
3232/// BB:
3233/// %cmp = icmp ult %x, %y
3234/// br i1 %cmp, label %EndBB, label %ThenBB
3235/// ThenBB:
3236/// %sub = sub %x, %y
3237/// br label BB2
3238/// EndBB:
3239/// %phi = phi [ %sub, %ThenBB ], [ 0, %BB ]
3240/// ...
3241/// \endcode
3242///
3243/// Into this IR:
3244/// \code
3245/// BB:
3246/// %cmp = icmp ult %x, %y
3247/// %sub = sub %x, %y
3248/// %cond = select i1 %cmp, 0, %sub
3249/// ...
3250/// \endcode
3251///
3252/// \returns true if the conditional block is removed.
3253bool SimplifyCFGOpt::speculativelyExecuteBB(BranchInst *BI,
3254BasicBlock *ThenBB) {
3255if (!Options.SpeculateBlocks)
3256returnfalse;
3257
3258// Be conservative for now. FP select instruction can often be expensive.
3259Value *BrCond = BI->getCondition();
3260if (isa<FCmpInst>(BrCond))
3261returnfalse;
3262
3263BasicBlock *BB = BI->getParent();
3264BasicBlock *EndBB = ThenBB->getTerminator()->getSuccessor(0);
3265InstructionCost Budget =
3266PHINodeFoldingThreshold *TargetTransformInfo::TCC_Basic;
3267
3268// If ThenBB is actually on the false edge of the conditional branch, remember
3269// to swap the select operands later.
3270bool Invert =false;
3271if (ThenBB != BI->getSuccessor(0)) {
3272assert(ThenBB == BI->getSuccessor(1) &&"No edge from 'if' block?");
3273 Invert =true;
3274 }
3275assert(EndBB == BI->getSuccessor(!Invert) &&"No edge from to end block");
3276
3277if (!isProfitableToSpeculate(BI, Invert,TTI))
3278returnfalse;
3279
3280// Keep a count of how many times instructions are used within ThenBB when
3281// they are candidates for sinking into ThenBB. Specifically:
3282// - They are defined in BB, and
3283// - They have no side effects, and
3284// - All of their uses are in ThenBB.
3285SmallDenseMap<Instruction *, unsigned, 4> SinkCandidateUseCounts;
3286
3287SmallVector<Instruction *, 4> SpeculatedDbgIntrinsics;
3288
3289unsigned SpeculatedInstructions = 0;
3290bool HoistLoadsStores =HoistLoadsStoresWithCondFaulting &&
3291Options.HoistLoadsStoresWithCondFaulting;
3292SmallVector<Instruction *, 2> SpeculatedConditionalLoadsStores;
3293Value *SpeculatedStoreValue =nullptr;
3294StoreInst *SpeculatedStore =nullptr;
3295 EphemeralValueTracker EphTracker;
3296for (Instruction &I :reverse(drop_end(*ThenBB))) {
3297// Skip debug info.
3298if (isa<DbgInfoIntrinsic>(I)) {
3299 SpeculatedDbgIntrinsics.push_back(&I);
3300continue;
3301 }
3302
3303// Skip pseudo probes. The consequence is we lose track of the branch
3304// probability for ThenBB, which is fine since the optimization here takes
3305// place regardless of the branch probability.
3306if (isa<PseudoProbeInst>(I)) {
3307// The probe should be deleted so that it will not be over-counted when
3308// the samples collected on the non-conditional path are counted towards
3309// the conditional path. We leave it for the counts inference algorithm to
3310// figure out a proper count for an unknown probe.
3311 SpeculatedDbgIntrinsics.push_back(&I);
3312continue;
3313 }
3314
3315// Ignore ephemeral values, they will be dropped by the transform.
3316if (EphTracker.track(&I))
3317continue;
3318
3319// Only speculatively execute a single instruction (not counting the
3320// terminator) for now.
3321bool IsSafeCheapLoadStore = HoistLoadsStores &&
3322isSafeCheapLoadStore(&I,TTI) &&
3323 SpeculatedConditionalLoadsStores.size() <
3324HoistLoadsStoresWithCondFaultingThreshold;
3325// Not count load/store into cost if target supports conditional faulting
3326// b/c it's cheap to speculate it.
3327if (IsSafeCheapLoadStore)
3328 SpeculatedConditionalLoadsStores.push_back(&I);
3329else
3330 ++SpeculatedInstructions;
3331
3332if (SpeculatedInstructions > 1)
3333returnfalse;
3334
3335// Don't hoist the instruction if it's unsafe or expensive.
3336if (!IsSafeCheapLoadStore &&
3337 !isSafeToSpeculativelyExecute(&I, BI,Options.AC) &&
3338 !(HoistCondStores && !SpeculatedStoreValue &&
3339 (SpeculatedStoreValue =
3340isSafeToSpeculateStore(&I, BB, ThenBB, EndBB))))
3341returnfalse;
3342if (!IsSafeCheapLoadStore && !SpeculatedStoreValue &&
3343computeSpeculationCost(&I,TTI) >
3344PHINodeFoldingThreshold *TargetTransformInfo::TCC_Basic)
3345returnfalse;
3346
3347// Store the store speculation candidate.
3348if (!SpeculatedStore && SpeculatedStoreValue)
3349 SpeculatedStore = cast<StoreInst>(&I);
3350
3351// Do not hoist the instruction if any of its operands are defined but not
3352// used in BB. The transformation will prevent the operand from
3353// being sunk into the use block.
3354for (Use &Op :I.operands()) {
3355Instruction *OpI = dyn_cast<Instruction>(Op);
3356if (!OpI || OpI->getParent() != BB || OpI->mayHaveSideEffects())
3357continue;// Not a candidate for sinking.
3358
3359 ++SinkCandidateUseCounts[OpI];
3360 }
3361 }
3362
3363// Consider any sink candidates which are only used in ThenBB as costs for
3364// speculation. Note, while we iterate over a DenseMap here, we are summing
3365// and so iteration order isn't significant.
3366for (constauto &[Inst, Count] : SinkCandidateUseCounts)
3367if (Inst->hasNUses(Count)) {
3368 ++SpeculatedInstructions;
3369if (SpeculatedInstructions > 1)
3370returnfalse;
3371 }
3372
3373// Check that we can insert the selects and that it's not too expensive to do
3374// so.
3375bool Convert =
3376 SpeculatedStore !=nullptr || !SpeculatedConditionalLoadsStores.empty();
3377InstructionCostCost = 0;
3378 Convert |=validateAndCostRequiredSelects(BB, ThenBB, EndBB,
3379 SpeculatedInstructions,Cost,TTI);
3380if (!Convert ||Cost > Budget)
3381returnfalse;
3382
3383// If we get here, we can hoist the instruction and if-convert.
3384LLVM_DEBUG(dbgs() <<"SPECULATIVELY EXECUTING BB" << *ThenBB <<"\n";);
3385
3386// Insert a select of the value of the speculated store.
3387if (SpeculatedStoreValue) {
3388IRBuilder<NoFolder> Builder(BI);
3389Value *OrigV = SpeculatedStore->getValueOperand();
3390Value *TrueV = SpeculatedStore->getValueOperand();
3391Value *FalseV = SpeculatedStoreValue;
3392if (Invert)
3393std::swap(TrueV, FalseV);
3394Value *S = Builder.CreateSelect(
3395 BrCond, TrueV, FalseV,"spec.store.select", BI);
3396 SpeculatedStore->setOperand(0, S);
3397 SpeculatedStore->applyMergedLocation(BI->getDebugLoc(),
3398 SpeculatedStore->getDebugLoc());
3399// The value stored is still conditional, but the store itself is now
3400// unconditonally executed, so we must be sure that any linked dbg.assign
3401// intrinsics are tracking the new stored value (the result of the
3402// select). If we don't, and the store were to be removed by another pass
3403// (e.g. DSE), then we'd eventually end up emitting a location describing
3404// the conditional value, unconditionally.
3405//
3406// === Before this transformation ===
3407// pred:
3408// store %one, %x.dest, !DIAssignID !1
3409// dbg.assign %one, "x", ..., !1, ...
3410// br %cond if.then
3411//
3412// if.then:
3413// store %two, %x.dest, !DIAssignID !2
3414// dbg.assign %two, "x", ..., !2, ...
3415//
3416// === After this transformation ===
3417// pred:
3418// store %one, %x.dest, !DIAssignID !1
3419// dbg.assign %one, "x", ..., !1
3420 /// ...
3421// %merge = select %cond, %two, %one
3422// store %merge, %x.dest, !DIAssignID !2
3423// dbg.assign %merge, "x", ..., !2
3424auto replaceVariable = [OrigV, S](auto *DbgAssign) {
3425if (llvm::is_contained(DbgAssign->location_ops(), OrigV))
3426 DbgAssign->replaceVariableLocationOp(OrigV, S);
3427 };
3428for_each(at::getAssignmentMarkers(SpeculatedStore), replaceVariable);
3429for_each(at::getDVRAssignmentMarkers(SpeculatedStore), replaceVariable);
3430 }
3431
3432// Metadata can be dependent on the condition we are hoisting above.
3433// Strip all UB-implying metadata on the instruction. Drop the debug loc
3434// to avoid making it appear as if the condition is a constant, which would
3435// be misleading while debugging.
3436// Similarly strip attributes that maybe dependent on condition we are
3437// hoisting above.
3438for (auto &I :make_early_inc_range(*ThenBB)) {
3439if (!SpeculatedStoreValue || &I != SpeculatedStore) {
3440// Don't update the DILocation of dbg.assign intrinsics.
3441if (!isa<DbgAssignIntrinsic>(&I))
3442I.setDebugLoc(DebugLoc());
3443 }
3444I.dropUBImplyingAttrsAndMetadata();
3445
3446// Drop ephemeral values.
3447if (EphTracker.contains(&I)) {
3448I.replaceAllUsesWith(PoisonValue::get(I.getType()));
3449I.eraseFromParent();
3450 }
3451 }
3452
3453// Hoist the instructions.
3454// In "RemoveDIs" non-instr debug-info mode, drop DbgVariableRecords attached
3455// to these instructions, in the same way that dbg.value intrinsics are
3456// dropped at the end of this block.
3457for (auto &It :make_range(ThenBB->begin(), ThenBB->end()))
3458for (DbgRecord &DR :make_early_inc_range(It.getDbgRecordRange()))
3459// Drop all records except assign-kind DbgVariableRecords (dbg.assign
3460// equivalent).
3461if (DbgVariableRecord *DVR = dyn_cast<DbgVariableRecord>(&DR);
3462 !DVR || !DVR->isDbgAssign())
3463 It.dropOneDbgRecord(&DR);
3464 BB->splice(BI->getIterator(), ThenBB, ThenBB->begin(),
3465 std::prev(ThenBB->end()));
3466
3467if (!SpeculatedConditionalLoadsStores.empty())
3468hoistConditionalLoadsStores(BI, SpeculatedConditionalLoadsStores, Invert);
3469
3470// Insert selects and rewrite the PHI operands.
3471IRBuilder<NoFolder> Builder(BI);
3472for (PHINode &PN : EndBB->phis()) {
3473unsigned OrigI = PN.getBasicBlockIndex(BB);
3474unsigned ThenI = PN.getBasicBlockIndex(ThenBB);
3475Value *OrigV = PN.getIncomingValue(OrigI);
3476Value *ThenV = PN.getIncomingValue(ThenI);
3477
3478// Skip PHIs which are trivial.
3479if (OrigV == ThenV)
3480continue;
3481
3482// Create a select whose true value is the speculatively executed value and
3483// false value is the pre-existing value. Swap them if the branch
3484// destinations were inverted.
3485Value *TrueV = ThenV, *FalseV = OrigV;
3486if (Invert)
3487std::swap(TrueV, FalseV);
3488Value *V = Builder.CreateSelect(BrCond, TrueV, FalseV,"spec.select", BI);
3489 PN.setIncomingValue(OrigI, V);
3490 PN.setIncomingValue(ThenI, V);
3491 }
3492
3493// Remove speculated dbg intrinsics.
3494// FIXME: Is it possible to do this in a more elegant way? Moving/merging the
3495// dbg value for the different flows and inserting it after the select.
3496for (Instruction *I : SpeculatedDbgIntrinsics) {
3497// We still want to know that an assignment took place so don't remove
3498// dbg.assign intrinsics.
3499if (!isa<DbgAssignIntrinsic>(I))
3500I->eraseFromParent();
3501 }
3502
3503 ++NumSpeculations;
3504returntrue;
3505}
3506
3507/// Return true if we can thread a branch across this block.
3508staticboolblockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
3509intSize = 0;
3510 EphemeralValueTracker EphTracker;
3511
3512// Walk the loop in reverse so that we can identify ephemeral values properly
3513// (values only feeding assumes).
3514for (Instruction &I :reverse(BB->instructionsWithoutDebug(false))) {
3515// Can't fold blocks that contain noduplicate or convergent calls.
3516if (CallInst *CI = dyn_cast<CallInst>(&I))
3517if (CI->cannotDuplicate() || CI->isConvergent())
3518returnfalse;
3519
3520// Ignore ephemeral values which are deleted during codegen.
3521// We will delete Phis while threading, so Phis should not be accounted in
3522// block's size.
3523if (!EphTracker.track(&I) && !isa<PHINode>(I)) {
3524if (Size++ >MaxSmallBlockSize)
3525returnfalse;// Don't clone large BB's.
3526 }
3527
3528// We can only support instructions that do not define values that are
3529// live outside of the current basic block.
3530for (User *U :I.users()) {
3531Instruction *UI = cast<Instruction>(U);
3532if (UI->getParent() != BB || isa<PHINode>(UI))
3533returnfalse;
3534 }
3535
3536// Looks ok, continue checking.
3537 }
3538
3539returntrue;
3540}
3541
3542staticConstantInt *getKnownValueOnEdge(Value *V,BasicBlock *From,
3543BasicBlock *To) {
3544// Don't look past the block defining the value, we might get the value from
3545// a previous loop iteration.
3546auto *I = dyn_cast<Instruction>(V);
3547if (I &&I->getParent() == To)
3548returnnullptr;
3549
3550// We know the value if the From block branches on it.
3551auto *BI = dyn_cast<BranchInst>(From->getTerminator());
3552if (BI && BI->isConditional() && BI->getCondition() == V &&
3553 BI->getSuccessor(0) != BI->getSuccessor(1))
3554return BI->getSuccessor(0) == To ?ConstantInt::getTrue(BI->getContext())
3555 :ConstantInt::getFalse(BI->getContext());
3556
3557returnnullptr;
3558}
3559
3560/// If we have a conditional branch on something for which we know the constant
3561/// value in predecessors (e.g. a phi node in the current block), thread edges
3562/// from the predecessor to their ultimate destination.
3563static std::optional<bool>
3564foldCondBranchOnValueKnownInPredecessorImpl(BranchInst *BI,DomTreeUpdater *DTU,
3565constDataLayout &DL,
3566AssumptionCache *AC) {
3567SmallMapVector<ConstantInt *, SmallSetVector<BasicBlock *, 2>, 2> KnownValues;
3568BasicBlock *BB = BI->getParent();
3569Value *Cond = BI->getCondition();
3570PHINode *PN = dyn_cast<PHINode>(Cond);
3571if (PN && PN->getParent() == BB) {
3572// Degenerate case of a single entry PHI.
3573if (PN->getNumIncomingValues() == 1) {
3574FoldSingleEntryPHINodes(PN->getParent());
3575returntrue;
3576 }
3577
3578for (Use &U : PN->incoming_values())
3579if (auto *CB = dyn_cast<ConstantInt>(U))
3580 KnownValues[CB].insert(PN->getIncomingBlock(U));
3581 }else {
3582for (BasicBlock *Pred :predecessors(BB)) {
3583if (ConstantInt *CB =getKnownValueOnEdge(Cond, Pred, BB))
3584 KnownValues[CB].insert(Pred);
3585 }
3586 }
3587
3588if (KnownValues.empty())
3589returnfalse;
3590
3591// Now we know that this block has multiple preds and two succs.
3592// Check that the block is small enough and values defined in the block are
3593// not used outside of it.
3594if (!blockIsSimpleEnoughToThreadThrough(BB))
3595returnfalse;
3596
3597for (constauto &Pair : KnownValues) {
3598// Okay, we now know that all edges from PredBB should be revectored to
3599// branch to RealDest.
3600ConstantInt *CB = Pair.first;
3601ArrayRef<BasicBlock *> PredBBs = Pair.second.getArrayRef();
3602BasicBlock *RealDest = BI->getSuccessor(!CB->getZExtValue());
3603
3604if (RealDest == BB)
3605continue;// Skip self loops.
3606
3607// Skip if the predecessor's terminator is an indirect branch.
3608if (any_of(PredBBs, [](BasicBlock *PredBB) {
3609return isa<IndirectBrInst>(PredBB->getTerminator());
3610 }))
3611continue;
3612
3613LLVM_DEBUG({
3614dbgs() <<"Condition " << *Cond <<" in " << BB->getName()
3615 <<" has value " << *Pair.first <<" in predecessors:\n";
3616for (constBasicBlock *PredBB : Pair.second)
3617dbgs() <<" " << PredBB->getName() <<"\n";
3618dbgs() <<"Threading to destination " << RealDest->getName() <<".\n";
3619 });
3620
3621// Split the predecessors we are threading into a new edge block. We'll
3622// clone the instructions into this block, and then redirect it to RealDest.
3623BasicBlock *EdgeBB =SplitBlockPredecessors(BB, PredBBs,".critedge", DTU);
3624
3625// TODO: These just exist to reduce test diff, we can drop them if we like.
3626 EdgeBB->setName(RealDest->getName() +".critedge");
3627 EdgeBB->moveBefore(RealDest);
3628
3629// Update PHI nodes.
3630addPredecessorToBlock(RealDest, EdgeBB, BB);
3631
3632// BB may have instructions that are being threaded over. Clone these
3633// instructions into EdgeBB. We know that there will be no uses of the
3634// cloned instructions outside of EdgeBB.
3635BasicBlock::iterator InsertPt = EdgeBB->getFirstInsertionPt();
3636DenseMap<Value *, Value *> TranslateMap;// Track translated values.
3637 TranslateMap[Cond] = CB;
3638
3639// RemoveDIs: track instructions that we optimise away while folding, so
3640// that we can copy DbgVariableRecords from them later.
3641BasicBlock::iterator SrcDbgCursor = BB->begin();
3642for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI) {
3643if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
3644 TranslateMap[PN] = PN->getIncomingValueForBlock(EdgeBB);
3645continue;
3646 }
3647// Clone the instruction.
3648Instruction *N = BBI->clone();
3649// Insert the new instruction into its new home.
3650N->insertInto(EdgeBB, InsertPt);
3651
3652if (BBI->hasName())
3653N->setName(BBI->getName() +".c");
3654
3655// Update operands due to translation.
3656for (Use &Op :N->operands()) {
3657DenseMap<Value *, Value *>::iterator PI = TranslateMap.find(Op);
3658if (PI != TranslateMap.end())
3659Op = PI->second;
3660 }
3661
3662// Check for trivial simplification.
3663if (Value *V =simplifyInstruction(N, {DL,nullptr,nullptr, AC})) {
3664if (!BBI->use_empty())
3665 TranslateMap[&*BBI] = V;
3666if (!N->mayHaveSideEffects()) {
3667N->eraseFromParent();// Instruction folded away, don't need actual
3668// inst
3669N =nullptr;
3670 }
3671 }else {
3672if (!BBI->use_empty())
3673 TranslateMap[&*BBI] =N;
3674 }
3675if (N) {
3676// Copy all debug-info attached to instructions from the last we
3677// successfully clone, up to this instruction (they might have been
3678// folded away).
3679for (; SrcDbgCursor != BBI; ++SrcDbgCursor)
3680N->cloneDebugInfoFrom(&*SrcDbgCursor);
3681 SrcDbgCursor = std::next(BBI);
3682// Clone debug-info on this instruction too.
3683N->cloneDebugInfoFrom(&*BBI);
3684
3685// Register the new instruction with the assumption cache if necessary.
3686if (auto *Assume = dyn_cast<AssumeInst>(N))
3687if (AC)
3688 AC->registerAssumption(Assume);
3689 }
3690 }
3691
3692for (; &*SrcDbgCursor != BI; ++SrcDbgCursor)
3693 InsertPt->cloneDebugInfoFrom(&*SrcDbgCursor);
3694 InsertPt->cloneDebugInfoFrom(BI);
3695
3696 BB->removePredecessor(EdgeBB);
3697BranchInst *EdgeBI = cast<BranchInst>(EdgeBB->getTerminator());
3698 EdgeBI->setSuccessor(0, RealDest);
3699 EdgeBI->setDebugLoc(BI->getDebugLoc());
3700
3701if (DTU) {
3702SmallVector<DominatorTree::UpdateType, 2> Updates;
3703 Updates.push_back({DominatorTree::Delete, EdgeBB, BB});
3704 Updates.push_back({DominatorTree::Insert, EdgeBB, RealDest});
3705 DTU->applyUpdates(Updates);
3706 }
3707
3708// For simplicity, we created a separate basic block for the edge. Merge
3709// it back into the predecessor if possible. This not only avoids
3710// unnecessary SimplifyCFG iterations, but also makes sure that we don't
3711// bypass the check for trivial cycles above.
3712MergeBlockIntoPredecessor(EdgeBB, DTU);
3713
3714// Signal repeat, simplifying any other constants.
3715return std::nullopt;
3716 }
3717
3718returnfalse;
3719}
3720
3721staticboolfoldCondBranchOnValueKnownInPredecessor(BranchInst *BI,
3722DomTreeUpdater *DTU,
3723constDataLayout &DL,
3724AssumptionCache *AC) {
3725 std::optional<bool> Result;
3726bool EverChanged =false;
3727do {
3728// Note that None means "we changed things, but recurse further."
3729 Result =foldCondBranchOnValueKnownInPredecessorImpl(BI, DTU,DL, AC);
3730 EverChanged |= Result == std::nullopt || *Result;
3731 }while (Result == std::nullopt);
3732return EverChanged;
3733}
3734
3735/// Given a BB that starts with the specified two-entry PHI node,
3736/// see if we can eliminate it.
3737staticboolfoldTwoEntryPHINode(PHINode *PN,constTargetTransformInfo &TTI,
3738DomTreeUpdater *DTU,AssumptionCache *AC,
3739constDataLayout &DL,
3740bool SpeculateUnpredictables) {
3741// Ok, this is a two entry PHI node. Check to see if this is a simple "if
3742// statement", which has a very simple dominance structure. Basically, we
3743// are trying to find the condition that is being branched on, which
3744// subsequently causes this merge to happen. We really want control
3745// dependence information for this check, but simplifycfg can't keep it up
3746// to date, and this catches most of the cases we care about anyway.
3747BasicBlock *BB = PN->getParent();
3748
3749BasicBlock *IfTrue, *IfFalse;
3750BranchInst *DomBI =GetIfCondition(BB, IfTrue, IfFalse);
3751if (!DomBI)
3752returnfalse;
3753Value *IfCond = DomBI->getCondition();
3754// Don't bother if the branch will be constant folded trivially.
3755if (isa<ConstantInt>(IfCond))
3756returnfalse;
3757
3758BasicBlock *DomBlock = DomBI->getParent();
3759SmallVector<BasicBlock *, 2> IfBlocks;
3760llvm::copy_if(
3761 PN->blocks(), std::back_inserter(IfBlocks), [](BasicBlock *IfBlock) {
3762 return cast<BranchInst>(IfBlock->getTerminator())->isUnconditional();
3763 });
3764assert((IfBlocks.size() == 1 || IfBlocks.size() == 2) &&
3765"Will have either one or two blocks to speculate.");
3766
3767// If the branch is non-unpredictable, see if we either predictably jump to
3768// the merge bb (if we have only a single 'then' block), or if we predictably
3769// jump to one specific 'then' block (if we have two of them).
3770// It isn't beneficial to speculatively execute the code
3771// from the block that we know is predictably not entered.
3772bool IsUnpredictable = DomBI->getMetadata(LLVMContext::MD_unpredictable);
3773if (!IsUnpredictable) {
3774uint64_t TWeight, FWeight;
3775if (extractBranchWeights(*DomBI, TWeight, FWeight) &&
3776 (TWeight + FWeight) != 0) {
3777BranchProbability BITrueProb =
3778BranchProbability::getBranchProbability(TWeight, TWeight + FWeight);
3779BranchProbability Likely =TTI.getPredictableBranchThreshold();
3780BranchProbability BIFalseProb = BITrueProb.getCompl();
3781if (IfBlocks.size() == 1) {
3782BranchProbability BIBBProb =
3783 DomBI->getSuccessor(0) == BB ? BITrueProb : BIFalseProb;
3784if (BIBBProb >= Likely)
3785returnfalse;
3786 }else {
3787if (BITrueProb >= Likely || BIFalseProb >= Likely)
3788returnfalse;
3789 }
3790 }
3791 }
3792
3793// Don't try to fold an unreachable block. For example, the phi node itself
3794// can't be the candidate if-condition for a select that we want to form.
3795if (auto *IfCondPhiInst = dyn_cast<PHINode>(IfCond))
3796if (IfCondPhiInst->getParent() == BB)
3797returnfalse;
3798
3799// Okay, we found that we can merge this two-entry phi node into a select.
3800// Doing so would require us to fold *all* two entry phi nodes in this block.
3801// At some point this becomes non-profitable (particularly if the target
3802// doesn't support cmov's). Only do this transformation if there are two or
3803// fewer PHI nodes in this block.
3804unsigned NumPhis = 0;
3805for (BasicBlock::iteratorI = BB->begin(); isa<PHINode>(I); ++NumPhis, ++I)
3806if (NumPhis > 2)
3807returnfalse;
3808
3809// Loop over the PHI's seeing if we can promote them all to select
3810// instructions. While we are at it, keep track of the instructions
3811// that need to be moved to the dominating block.
3812SmallPtrSet<Instruction *, 4> AggressiveInsts;
3813InstructionCostCost = 0;
3814InstructionCost Budget =
3815TwoEntryPHINodeFoldingThreshold *TargetTransformInfo::TCC_Basic;
3816if (SpeculateUnpredictables && IsUnpredictable)
3817 Budget +=TTI.getBranchMispredictPenalty();
3818
3819bool Changed =false;
3820for (BasicBlock::iteratorII = BB->begin(); isa<PHINode>(II);) {
3821PHINode *PN = cast<PHINode>(II++);
3822if (Value *V =simplifyInstruction(PN, {DL, PN})) {
3823 PN->replaceAllUsesWith(V);
3824 PN->eraseFromParent();
3825 Changed =true;
3826continue;
3827 }
3828
3829if (!dominatesMergePoint(PN->getIncomingValue(0), BB, DomBI,
3830 AggressiveInsts,Cost, Budget,TTI, AC) ||
3831 !dominatesMergePoint(PN->getIncomingValue(1), BB, DomBI,
3832 AggressiveInsts,Cost, Budget,TTI, AC))
3833return Changed;
3834 }
3835
3836// If we folded the first phi, PN dangles at this point. Refresh it. If
3837// we ran out of PHIs then we simplified them all.
3838 PN = dyn_cast<PHINode>(BB->begin());
3839if (!PN)
3840returntrue;
3841
3842// Return true if at least one of these is a 'not', and another is either
3843// a 'not' too, or a constant.
3844auto CanHoistNotFromBothValues = [](Value *V0,Value *V1) {
3845if (!match(V0,m_Not(m_Value())))
3846std::swap(V0, V1);
3847auto Invertible =m_CombineOr(m_Not(m_Value()),m_AnyIntegralConstant());
3848returnmatch(V0,m_Not(m_Value())) &&match(V1, Invertible);
3849 };
3850
3851// Don't fold i1 branches on PHIs which contain binary operators or
3852// (possibly inverted) select form of or/ands, unless one of
3853// the incoming values is an 'not' and another one is freely invertible.
3854// These can often be turned into switches and other things.
3855auto IsBinOpOrAnd = [](Value *V) {
3856returnmatch(
3857 V,m_CombineOr(m_BinOp(),m_c_Select(m_ImmConstant(),m_Value())));
3858 };
3859if (PN->getType()->isIntegerTy(1) &&
3860 (IsBinOpOrAnd(PN->getIncomingValue(0)) ||
3861 IsBinOpOrAnd(PN->getIncomingValue(1)) || IsBinOpOrAnd(IfCond)) &&
3862 !CanHoistNotFromBothValues(PN->getIncomingValue(0),
3863 PN->getIncomingValue(1)))
3864return Changed;
3865
3866// If all PHI nodes are promotable, check to make sure that all instructions
3867// in the predecessor blocks can be promoted as well. If not, we won't be able
3868// to get rid of the control flow, so it's not worth promoting to select
3869// instructions.
3870for (BasicBlock *IfBlock : IfBlocks)
3871for (BasicBlock::iteratorI = IfBlock->begin(); !I->isTerminator(); ++I)
3872if (!AggressiveInsts.count(&*I) && !I->isDebugOrPseudoInst()) {
3873// This is not an aggressive instruction that we can promote.
3874// Because of this, we won't be able to get rid of the control flow, so
3875// the xform is not worth it.
3876return Changed;
3877 }
3878
3879// If either of the blocks has it's address taken, we can't do this fold.
3880if (any_of(IfBlocks,
3881 [](BasicBlock *IfBlock) {return IfBlock->hasAddressTaken(); }))
3882return Changed;
3883
3884LLVM_DEBUG(dbgs() <<"FOUND IF CONDITION! " << *IfCond;
3885if (IsUnpredictable)dbgs() <<" (unpredictable)";
3886dbgs() <<" T: " << IfTrue->getName()
3887 <<" F: " << IfFalse->getName() <<"\n");
3888
3889// If we can still promote the PHI nodes after this gauntlet of tests,
3890// do all of the PHI's now.
3891
3892// Move all 'aggressive' instructions, which are defined in the
3893// conditional parts of the if's up to the dominating block.
3894for (BasicBlock *IfBlock : IfBlocks)
3895hoistAllInstructionsInto(DomBlock, DomBI, IfBlock);
3896
3897IRBuilder<NoFolder> Builder(DomBI);
3898// Propagate fast-math-flags from phi nodes to replacement selects.
3899while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
3900// Change the PHI node into a select instruction.
3901Value *TrueVal = PN->getIncomingValueForBlock(IfTrue);
3902Value *FalseVal = PN->getIncomingValueForBlock(IfFalse);
3903
3904Value *Sel = Builder.CreateSelectFMF(IfCond, TrueVal, FalseVal,
3905 isa<FPMathOperator>(PN) ? PN :nullptr,
3906"", DomBI);
3907 PN->replaceAllUsesWith(Sel);
3908 Sel->takeName(PN);
3909 PN->eraseFromParent();
3910 }
3911
3912// At this point, all IfBlocks are empty, so our if statement
3913// has been flattened. Change DomBlock to jump directly to our new block to
3914// avoid other simplifycfg's kicking in on the diamond.
3915 Builder.CreateBr(BB);
3916
3917SmallVector<DominatorTree::UpdateType, 3> Updates;
3918if (DTU) {
3919 Updates.push_back({DominatorTree::Insert, DomBlock, BB});
3920for (auto *Successor :successors(DomBlock))
3921 Updates.push_back({DominatorTree::Delete, DomBlock,Successor});
3922 }
3923
3924 DomBI->eraseFromParent();
3925if (DTU)
3926 DTU->applyUpdates(Updates);
3927
3928returntrue;
3929}
3930
3931staticValue *createLogicalOp(IRBuilderBase &Builder,
3932Instruction::BinaryOps Opc,Value *LHS,
3933Value *RHS,constTwine &Name ="") {
3934// Try to relax logical op to binary op.
3935if (impliesPoison(RHS,LHS))
3936return Builder.CreateBinOp(Opc,LHS,RHS,Name);
3937if (Opc == Instruction::And)
3938return Builder.CreateLogicalAnd(LHS,RHS,Name);
3939if (Opc == Instruction::Or)
3940return Builder.CreateLogicalOr(LHS,RHS,Name);
3941llvm_unreachable("Invalid logical opcode");
3942}
3943
3944/// Return true if either PBI or BI has branch weight available, and store
3945/// the weights in {Pred|Succ}{True|False}Weight. If one of PBI and BI does
3946/// not have branch weight, use 1:1 as its weight.
3947staticboolextractPredSuccWeights(BranchInst *PBI,BranchInst *BI,
3948uint64_t &PredTrueWeight,
3949uint64_t &PredFalseWeight,
3950uint64_t &SuccTrueWeight,
3951uint64_t &SuccFalseWeight) {
3952bool PredHasWeights =
3953extractBranchWeights(*PBI, PredTrueWeight, PredFalseWeight);
3954bool SuccHasWeights =
3955extractBranchWeights(*BI, SuccTrueWeight, SuccFalseWeight);
3956if (PredHasWeights || SuccHasWeights) {
3957if (!PredHasWeights)
3958 PredTrueWeight = PredFalseWeight = 1;
3959if (!SuccHasWeights)
3960 SuccTrueWeight = SuccFalseWeight = 1;
3961returntrue;
3962 }else {
3963returnfalse;
3964 }
3965}
3966
3967/// Determine if the two branches share a common destination and deduce a glue
3968/// that joins the branches' conditions to arrive at the common destination if
3969/// that would be profitable.
3970static std::optional<std::tuple<BasicBlock *, Instruction::BinaryOps, bool>>
3971shouldFoldCondBranchesToCommonDestination(BranchInst *BI,BranchInst *PBI,
3972constTargetTransformInfo *TTI) {
3973assert(BI && PBI && BI->isConditional() && PBI->isConditional() &&
3974"Both blocks must end with a conditional branches.");
3975assert(is_contained(predecessors(BI->getParent()), PBI->getParent()) &&
3976"PredBB must be a predecessor of BB.");
3977
3978// We have the potential to fold the conditions together, but if the
3979// predecessor branch is predictable, we may not want to merge them.
3980uint64_t PTWeight, PFWeight;
3981BranchProbability PBITrueProb, Likely;
3982if (TTI && !PBI->getMetadata(LLVMContext::MD_unpredictable) &&
3983extractBranchWeights(*PBI, PTWeight, PFWeight) &&
3984 (PTWeight + PFWeight) != 0) {
3985 PBITrueProb =
3986BranchProbability::getBranchProbability(PTWeight, PTWeight + PFWeight);
3987 Likely =TTI->getPredictableBranchThreshold();
3988 }
3989
3990if (PBI->getSuccessor(0) == BI->getSuccessor(0)) {
3991// Speculate the 2nd condition unless the 1st is probably true.
3992if (PBITrueProb.isUnknown() || PBITrueProb < Likely)
3993return {{BI->getSuccessor(0), Instruction::Or,false}};
3994 }elseif (PBI->getSuccessor(1) == BI->getSuccessor(1)) {
3995// Speculate the 2nd condition unless the 1st is probably false.
3996if (PBITrueProb.isUnknown() || PBITrueProb.getCompl() < Likely)
3997return {{BI->getSuccessor(1), Instruction::And,false}};
3998 }elseif (PBI->getSuccessor(0) == BI->getSuccessor(1)) {
3999// Speculate the 2nd condition unless the 1st is probably true.
4000if (PBITrueProb.isUnknown() || PBITrueProb < Likely)
4001return {{BI->getSuccessor(1), Instruction::And,true}};
4002 }elseif (PBI->getSuccessor(1) == BI->getSuccessor(0)) {
4003// Speculate the 2nd condition unless the 1st is probably false.
4004if (PBITrueProb.isUnknown() || PBITrueProb.getCompl() < Likely)
4005return {{BI->getSuccessor(0), Instruction::Or,true}};
4006 }
4007return std::nullopt;
4008}
4009
4010staticboolperformBranchToCommonDestFolding(BranchInst *BI,BranchInst *PBI,
4011DomTreeUpdater *DTU,
4012MemorySSAUpdater *MSSAU,
4013constTargetTransformInfo *TTI) {
4014BasicBlock *BB = BI->getParent();
4015BasicBlock *PredBlock = PBI->getParent();
4016
4017// Determine if the two branches share a common destination.
4018BasicBlock *CommonSucc;
4019Instruction::BinaryOps Opc;
4020bool InvertPredCond;
4021 std::tie(CommonSucc, Opc, InvertPredCond) =
4022 *shouldFoldCondBranchesToCommonDestination(BI, PBI,TTI);
4023
4024LLVM_DEBUG(dbgs() <<"FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB);
4025
4026IRBuilder<> Builder(PBI);
4027// The builder is used to create instructions to eliminate the branch in BB.
4028// If BB's terminator has !annotation metadata, add it to the new
4029// instructions.
4030 Builder.CollectMetadataToCopy(BB->getTerminator(),
4031 {LLVMContext::MD_annotation});
4032
4033// If we need to invert the condition in the pred block to match, do so now.
4034if (InvertPredCond) {
4035InvertBranch(PBI, Builder);
4036 }
4037
4038BasicBlock *UniqueSucc =
4039 PBI->getSuccessor(0) == BB ? BI->getSuccessor(0) : BI->getSuccessor(1);
4040
4041// Before cloning instructions, notify the successor basic block that it
4042// is about to have a new predecessor. This will update PHI nodes,
4043// which will allow us to update live-out uses of bonus instructions.
4044addPredecessorToBlock(UniqueSucc, PredBlock, BB, MSSAU);
4045
4046// Try to update branch weights.
4047uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
4048if (extractPredSuccWeights(PBI, BI, PredTrueWeight, PredFalseWeight,
4049 SuccTrueWeight, SuccFalseWeight)) {
4050SmallVector<uint64_t, 8> NewWeights;
4051
4052if (PBI->getSuccessor(0) == BB) {
4053// PBI: br i1 %x, BB, FalseDest
4054// BI: br i1 %y, UniqueSucc, FalseDest
4055// TrueWeight is TrueWeight for PBI * TrueWeight for BI.
4056 NewWeights.push_back(PredTrueWeight * SuccTrueWeight);
4057// FalseWeight is FalseWeight for PBI * TotalWeight for BI +
4058// TrueWeight for PBI * FalseWeight for BI.
4059// We assume that total weights of a BranchInst can fit into 32 bits.
4060// Therefore, we will not have overflow using 64-bit arithmetic.
4061 NewWeights.push_back(PredFalseWeight *
4062 (SuccFalseWeight + SuccTrueWeight) +
4063 PredTrueWeight * SuccFalseWeight);
4064 }else {
4065// PBI: br i1 %x, TrueDest, BB
4066// BI: br i1 %y, TrueDest, UniqueSucc
4067// TrueWeight is TrueWeight for PBI * TotalWeight for BI +
4068// FalseWeight for PBI * TrueWeight for BI.
4069 NewWeights.push_back(PredTrueWeight * (SuccFalseWeight + SuccTrueWeight) +
4070 PredFalseWeight * SuccTrueWeight);
4071// FalseWeight is FalseWeight for PBI * FalseWeight for BI.
4072 NewWeights.push_back(PredFalseWeight * SuccFalseWeight);
4073 }
4074
4075// Halve the weights if any of them cannot fit in an uint32_t
4076fitWeights(NewWeights);
4077
4078SmallVector<uint32_t, 8> MDWeights(NewWeights.begin(), NewWeights.end());
4079setBranchWeights(PBI, MDWeights[0], MDWeights[1],/*IsExpected=*/false);
4080
4081// TODO: If BB is reachable from all paths through PredBlock, then we
4082// could replace PBI's branch probabilities with BI's.
4083 }else
4084 PBI->setMetadata(LLVMContext::MD_prof,nullptr);
4085
4086// Now, update the CFG.
4087 PBI->setSuccessor(PBI->getSuccessor(0) != BB, UniqueSucc);
4088
4089if (DTU)
4090 DTU->applyUpdates({{DominatorTree::Insert, PredBlock, UniqueSucc},
4091 {DominatorTree::Delete, PredBlock, BB}});
4092
4093// If BI was a loop latch, it may have had associated loop metadata.
4094// We need to copy it to the new latch, that is, PBI.
4095if (MDNode *LoopMD = BI->getMetadata(LLVMContext::MD_loop))
4096 PBI->setMetadata(LLVMContext::MD_loop, LoopMD);
4097
4098ValueToValueMapTy VMap;// maps original values to cloned values
4099cloneInstructionsIntoPredecessorBlockAndUpdateSSAUses(BB, PredBlock, VMap);
4100
4101Module *M = BB->getModule();
4102
4103if (PredBlock->IsNewDbgInfoFormat) {
4104 PredBlock->getTerminator()->cloneDebugInfoFrom(BB->getTerminator());
4105for (DbgVariableRecord &DVR :
4106filterDbgVars(PredBlock->getTerminator()->getDbgRecordRange())) {
4107RemapDbgRecord(M, &DVR, VMap,
4108RF_NoModuleLevelChanges |RF_IgnoreMissingLocals);
4109 }
4110 }
4111
4112// Now that the Cond was cloned into the predecessor basic block,
4113// or/and the two conditions together.
4114Value *BICond = VMap[BI->getCondition()];
4115 PBI->setCondition(
4116createLogicalOp(Builder, Opc, PBI->getCondition(), BICond,"or.cond"));
4117
4118 ++NumFoldBranchToCommonDest;
4119returntrue;
4120}
4121
4122/// Return if an instruction's type or any of its operands' types are a vector
4123/// type.
4124staticboolisVectorOp(Instruction &I) {
4125returnI.getType()->isVectorTy() ||any_of(I.operands(), [](Use &U) {
4126 return U->getType()->isVectorTy();
4127 });
4128}
4129
4130/// If this basic block is simple enough, and if a predecessor branches to us
4131/// and one of our successors, fold the block into the predecessor and use
4132/// logical operations to pick the right destination.
4133boolllvm::foldBranchToCommonDest(BranchInst *BI,DomTreeUpdater *DTU,
4134MemorySSAUpdater *MSSAU,
4135constTargetTransformInfo *TTI,
4136unsigned BonusInstThreshold) {
4137// If this block ends with an unconditional branch,
4138// let speculativelyExecuteBB() deal with it.
4139if (!BI->isConditional())
4140returnfalse;
4141
4142BasicBlock *BB = BI->getParent();
4143TargetTransformInfo::TargetCostKindCostKind =
4144 BB->getParent()->hasMinSize() ?TargetTransformInfo::TCK_CodeSize
4145 :TargetTransformInfo::TCK_SizeAndLatency;
4146
4147Instruction *Cond = dyn_cast<Instruction>(BI->getCondition());
4148
4149if (!Cond ||
4150 (!isa<CmpInst>(Cond) && !isa<BinaryOperator>(Cond) &&
4151 !isa<SelectInst>(Cond)) ||
4152Cond->getParent() != BB || !Cond->hasOneUse())
4153returnfalse;
4154
4155// Finally, don't infinitely unroll conditional loops.
4156if (is_contained(successors(BB), BB))
4157returnfalse;
4158
4159// With which predecessors will we want to deal with?
4160SmallVector<BasicBlock *, 8> Preds;
4161for (BasicBlock *PredBlock :predecessors(BB)) {
4162BranchInst *PBI = dyn_cast<BranchInst>(PredBlock->getTerminator());
4163
4164// Check that we have two conditional branches. If there is a PHI node in
4165// the common successor, verify that the same value flows in from both
4166// blocks.
4167if (!PBI || PBI->isUnconditional() || !safeToMergeTerminators(BI, PBI))
4168continue;
4169
4170// Determine if the two branches share a common destination.
4171BasicBlock *CommonSucc;
4172Instruction::BinaryOps Opc;
4173bool InvertPredCond;
4174if (auto Recipe =shouldFoldCondBranchesToCommonDestination(BI, PBI,TTI))
4175 std::tie(CommonSucc, Opc, InvertPredCond) = *Recipe;
4176else
4177continue;
4178
4179// Check the cost of inserting the necessary logic before performing the
4180// transformation.
4181if (TTI) {
4182Type *Ty = BI->getCondition()->getType();
4183InstructionCostCost =TTI->getArithmeticInstrCost(Opc, Ty,CostKind);
4184if (InvertPredCond && (!PBI->getCondition()->hasOneUse() ||
4185 !isa<CmpInst>(PBI->getCondition())))
4186Cost +=TTI->getArithmeticInstrCost(Instruction::Xor, Ty,CostKind);
4187
4188if (Cost >BranchFoldThreshold)
4189continue;
4190 }
4191
4192// Ok, we do want to deal with this predecessor. Record it.
4193 Preds.emplace_back(PredBlock);
4194 }
4195
4196// If there aren't any predecessors into which we can fold,
4197// don't bother checking the cost.
4198if (Preds.empty())
4199returnfalse;
4200
4201// Only allow this transformation if computing the condition doesn't involve
4202// too many instructions and these involved instructions can be executed
4203// unconditionally. We denote all involved instructions except the condition
4204// as "bonus instructions", and only allow this transformation when the
4205// number of the bonus instructions we'll need to create when cloning into
4206// each predecessor does not exceed a certain threshold.
4207unsigned NumBonusInsts = 0;
4208bool SawVectorOp =false;
4209constunsigned PredCount = Preds.size();
4210for (Instruction &I : *BB) {
4211// Don't check the branch condition comparison itself.
4212if (&I ==Cond)
4213continue;
4214// Ignore dbg intrinsics, and the terminator.
4215if (isa<DbgInfoIntrinsic>(I) || isa<BranchInst>(I))
4216continue;
4217// I must be safe to execute unconditionally.
4218if (!isSafeToSpeculativelyExecute(&I))
4219returnfalse;
4220 SawVectorOp |=isVectorOp(I);
4221
4222// Account for the cost of duplicating this instruction into each
4223// predecessor. Ignore free instructions.
4224if (!TTI ||TTI->getInstructionCost(&I,CostKind) !=
4225TargetTransformInfo::TCC_Free) {
4226 NumBonusInsts += PredCount;
4227
4228// Early exits once we reach the limit.
4229if (NumBonusInsts >
4230 BonusInstThreshold *BranchFoldToCommonDestVectorMultiplier)
4231returnfalse;
4232 }
4233
4234auto IsBCSSAUse = [BB, &I](Use &U) {
4235auto *UI = cast<Instruction>(U.getUser());
4236if (auto *PN = dyn_cast<PHINode>(UI))
4237return PN->getIncomingBlock(U) == BB;
4238return UI->getParent() == BB &&I.comesBefore(UI);
4239 };
4240
4241// Does this instruction require rewriting of uses?
4242if (!all_of(I.uses(), IsBCSSAUse))
4243returnfalse;
4244 }
4245if (NumBonusInsts >
4246 BonusInstThreshold *
4247 (SawVectorOp ?BranchFoldToCommonDestVectorMultiplier : 1))
4248returnfalse;
4249
4250// Ok, we have the budget. Perform the transformation.
4251for (BasicBlock *PredBlock : Preds) {
4252auto *PBI = cast<BranchInst>(PredBlock->getTerminator());
4253returnperformBranchToCommonDestFolding(BI, PBI, DTU, MSSAU,TTI);
4254 }
4255returnfalse;
4256}
4257
4258// If there is only one store in BB1 and BB2, return it, otherwise return
4259// nullptr.
4260staticStoreInst *findUniqueStoreInBlocks(BasicBlock *BB1,BasicBlock *BB2) {
4261StoreInst *S =nullptr;
4262for (auto *BB : {BB1, BB2}) {
4263if (!BB)
4264continue;
4265for (auto &I : *BB)
4266if (auto *SI = dyn_cast<StoreInst>(&I)) {
4267if (S)
4268// Multiple stores seen.
4269returnnullptr;
4270else
4271 S = SI;
4272 }
4273 }
4274return S;
4275}
4276
4277staticValue *ensureValueAvailableInSuccessor(Value *V,BasicBlock *BB,
4278Value *AlternativeV =nullptr) {
4279// PHI is going to be a PHI node that allows the value V that is defined in
4280// BB to be referenced in BB's only successor.
4281//
4282// If AlternativeV is nullptr, the only value we care about in PHI is V. It
4283// doesn't matter to us what the other operand is (it'll never get used). We
4284// could just create a new PHI with an undef incoming value, but that could
4285// increase register pressure if EarlyCSE/InstCombine can't fold it with some
4286// other PHI. So here we directly look for some PHI in BB's successor with V
4287// as an incoming operand. If we find one, we use it, else we create a new
4288// one.
4289//
4290// If AlternativeV is not nullptr, we care about both incoming values in PHI.
4291// PHI must be exactly: phi <ty> [ %BB, %V ], [ %OtherBB, %AlternativeV]
4292// where OtherBB is the single other predecessor of BB's only successor.
4293PHINode *PHI =nullptr;
4294BasicBlock *Succ = BB->getSingleSuccessor();
4295
4296for (autoI = Succ->begin(); isa<PHINode>(I); ++I)
4297if (cast<PHINode>(I)->getIncomingValueForBlock(BB) == V) {
4298PHI = cast<PHINode>(I);
4299if (!AlternativeV)
4300break;
4301
4302assert(Succ->hasNPredecessors(2));
4303auto PredI =pred_begin(Succ);
4304BasicBlock *OtherPredBB = *PredI == BB ? *++PredI : *PredI;
4305if (PHI->getIncomingValueForBlock(OtherPredBB) == AlternativeV)
4306break;
4307PHI =nullptr;
4308 }
4309if (PHI)
4310returnPHI;
4311
4312// If V is not an instruction defined in BB, just return it.
4313if (!AlternativeV &&
4314 (!isa<Instruction>(V) || cast<Instruction>(V)->getParent() != BB))
4315return V;
4316
4317PHI =PHINode::Create(V->getType(), 2,"simplifycfg.merge");
4318PHI->insertBefore(Succ->begin());
4319PHI->addIncoming(V, BB);
4320for (BasicBlock *PredBB :predecessors(Succ))
4321if (PredBB != BB)
4322PHI->addIncoming(
4323 AlternativeV ? AlternativeV :PoisonValue::get(V->getType()), PredBB);
4324returnPHI;
4325}
4326
4327staticboolmergeConditionalStoreToAddress(
4328BasicBlock *PTB,BasicBlock *PFB,BasicBlock *QTB,BasicBlock *QFB,
4329BasicBlock *PostBB,Value *Address,bool InvertPCond,bool InvertQCond,
4330DomTreeUpdater *DTU,constDataLayout &DL,constTargetTransformInfo &TTI) {
4331// For every pointer, there must be exactly two stores, one coming from
4332// PTB or PFB, and the other from QTB or QFB. We don't support more than one
4333// store (to any address) in PTB,PFB or QTB,QFB.
4334// FIXME: We could relax this restriction with a bit more work and performance
4335// testing.
4336StoreInst *PStore =findUniqueStoreInBlocks(PTB, PFB);
4337StoreInst *QStore =findUniqueStoreInBlocks(QTB, QFB);
4338if (!PStore || !QStore)
4339returnfalse;
4340
4341// Now check the stores are compatible.
4342if (!QStore->isUnordered() || !PStore->isUnordered() ||
4343 PStore->getValueOperand()->getType() !=
4344 QStore->getValueOperand()->getType())
4345returnfalse;
4346
4347// Check that sinking the store won't cause program behavior changes. Sinking
4348// the store out of the Q blocks won't change any behavior as we're sinking
4349// from a block to its unconditional successor. But we're moving a store from
4350// the P blocks down through the middle block (QBI) and past both QFB and QTB.
4351// So we need to check that there are no aliasing loads or stores in
4352// QBI, QTB and QFB. We also need to check there are no conflicting memory
4353// operations between PStore and the end of its parent block.
4354//
4355// The ideal way to do this is to query AliasAnalysis, but we don't
4356// preserve AA currently so that is dangerous. Be super safe and just
4357// check there are no other memory operations at all.
4358for (auto &I : *QFB->getSinglePredecessor())
4359if (I.mayReadOrWriteMemory())
4360returnfalse;
4361for (auto &I : *QFB)
4362if (&I != QStore &&I.mayReadOrWriteMemory())
4363returnfalse;
4364if (QTB)
4365for (auto &I : *QTB)
4366if (&I != QStore &&I.mayReadOrWriteMemory())
4367returnfalse;
4368for (autoI =BasicBlock::iterator(PStore), E = PStore->getParent()->end();
4369I != E; ++I)
4370if (&*I != PStore &&I->mayReadOrWriteMemory())
4371returnfalse;
4372
4373// If we're not in aggressive mode, we only optimize if we have some
4374// confidence that by optimizing we'll allow P and/or Q to be if-converted.
4375auto IsWorthwhile = [&](BasicBlock *BB,ArrayRef<StoreInst *> FreeStores) {
4376if (!BB)
4377returntrue;
4378// Heuristic: if the block can be if-converted/phi-folded and the
4379// instructions inside are all cheap (arithmetic/GEPs), it's worthwhile to
4380// thread this store.
4381InstructionCostCost = 0;
4382InstructionCost Budget =
4383PHINodeFoldingThreshold *TargetTransformInfo::TCC_Basic;
4384for (auto &I : BB->instructionsWithoutDebug(false)) {
4385// Consider terminator instruction to be free.
4386if (I.isTerminator())
4387continue;
4388// If this is one the stores that we want to speculate out of this BB,
4389// then don't count it's cost, consider it to be free.
4390if (auto *S = dyn_cast<StoreInst>(&I))
4391if (llvm::find(FreeStores, S))
4392continue;
4393// Else, we have a white-list of instructions that we are ak speculating.
4394if (!isa<BinaryOperator>(I) && !isa<GetElementPtrInst>(I))
4395returnfalse;// Not in white-list - not worthwhile folding.
4396// And finally, if this is a non-free instruction that we are okay
4397// speculating, ensure that we consider the speculation budget.
4398Cost +=
4399TTI.getInstructionCost(&I,TargetTransformInfo::TCK_SizeAndLatency);
4400if (Cost > Budget)
4401returnfalse;// Eagerly refuse to fold as soon as we're out of budget.
4402 }
4403assert(Cost <= Budget &&
4404"When we run out of budget we will eagerly return from within the "
4405"per-instruction loop.");
4406returntrue;
4407 };
4408
4409const std::array<StoreInst *, 2> FreeStores = {PStore, QStore};
4410if (!MergeCondStoresAggressively &&
4411 (!IsWorthwhile(PTB, FreeStores) || !IsWorthwhile(PFB, FreeStores) ||
4412 !IsWorthwhile(QTB, FreeStores) || !IsWorthwhile(QFB, FreeStores)))
4413returnfalse;
4414
4415// If PostBB has more than two predecessors, we need to split it so we can
4416// sink the store.
4417if (std::next(pred_begin(PostBB), 2) !=pred_end(PostBB)) {
4418// We know that QFB's only successor is PostBB. And QFB has a single
4419// predecessor. If QTB exists, then its only successor is also PostBB.
4420// If QTB does not exist, then QFB's only predecessor has a conditional
4421// branch to QFB and PostBB.
4422BasicBlock *TruePred = QTB ? QTB : QFB->getSinglePredecessor();
4423BasicBlock *NewBB =
4424SplitBlockPredecessors(PostBB, {QFB, TruePred},"condstore.split", DTU);
4425if (!NewBB)
4426returnfalse;
4427 PostBB = NewBB;
4428 }
4429
4430// OK, we're going to sink the stores to PostBB. The store has to be
4431// conditional though, so first create the predicate.
4432Value *PCond = cast<BranchInst>(PFB->getSinglePredecessor()->getTerminator())
4433 ->getCondition();
4434Value *QCond = cast<BranchInst>(QFB->getSinglePredecessor()->getTerminator())
4435 ->getCondition();
4436
4437Value *PPHI =ensureValueAvailableInSuccessor(PStore->getValueOperand(),
4438 PStore->getParent());
4439Value *QPHI =ensureValueAvailableInSuccessor(QStore->getValueOperand(),
4440 QStore->getParent(), PPHI);
4441
4442BasicBlock::iterator PostBBFirst = PostBB->getFirstInsertionPt();
4443IRBuilder<> QB(PostBB, PostBBFirst);
4444 QB.SetCurrentDebugLocation(PostBBFirst->getStableDebugLoc());
4445
4446Value *PPred = PStore->getParent() == PTB ? PCond : QB.CreateNot(PCond);
4447Value *QPred = QStore->getParent() == QTB ? QCond : QB.CreateNot(QCond);
4448
4449if (InvertPCond)
4450 PPred = QB.CreateNot(PPred);
4451if (InvertQCond)
4452 QPred = QB.CreateNot(QPred);
4453Value *CombinedPred = QB.CreateOr(PPred, QPred);
4454
4455BasicBlock::iterator InsertPt = QB.GetInsertPoint();
4456auto *T =SplitBlockAndInsertIfThen(CombinedPred, InsertPt,
4457/*Unreachable=*/false,
4458/*BranchWeights=*/nullptr, DTU);
4459
4460 QB.SetInsertPoint(T);
4461StoreInst *SI = cast<StoreInst>(QB.CreateStore(QPHI,Address));
4462 SI->setAAMetadata(PStore->getAAMetadata().merge(QStore->getAAMetadata()));
4463// Choose the minimum alignment. If we could prove both stores execute, we
4464// could use biggest one. In this case, though, we only know that one of the
4465// stores executes. And we don't know it's safe to take the alignment from a
4466// store that doesn't execute.
4467 SI->setAlignment(std::min(PStore->getAlign(), QStore->getAlign()));
4468
4469 QStore->eraseFromParent();
4470 PStore->eraseFromParent();
4471
4472returntrue;
4473}
4474
4475staticboolmergeConditionalStores(BranchInst *PBI,BranchInst *QBI,
4476DomTreeUpdater *DTU,constDataLayout &DL,
4477constTargetTransformInfo &TTI) {
4478// The intention here is to find diamonds or triangles (see below) where each
4479// conditional block contains a store to the same address. Both of these
4480// stores are conditional, so they can't be unconditionally sunk. But it may
4481// be profitable to speculatively sink the stores into one merged store at the
4482// end, and predicate the merged store on the union of the two conditions of
4483// PBI and QBI.
4484//
4485// This can reduce the number of stores executed if both of the conditions are
4486// true, and can allow the blocks to become small enough to be if-converted.
4487// This optimization will also chain, so that ladders of test-and-set
4488// sequences can be if-converted away.
4489//
4490// We only deal with simple diamonds or triangles:
4491//
4492// PBI or PBI or a combination of the two
4493// / \ | \
4494 // PTB PFB | PFB
4495// \ / | /
4496// QBI QBI
4497// / \ | \
4498 // QTB QFB | QFB
4499// \ / | /
4500// PostBB PostBB
4501//
4502// We model triangles as a type of diamond with a nullptr "true" block.
4503// Triangles are canonicalized so that the fallthrough edge is represented by
4504// a true condition, as in the diagram above.
4505BasicBlock *PTB = PBI->getSuccessor(0);
4506BasicBlock *PFB = PBI->getSuccessor(1);
4507BasicBlock *QTB = QBI->getSuccessor(0);
4508BasicBlock *QFB = QBI->getSuccessor(1);
4509BasicBlock *PostBB = QFB->getSingleSuccessor();
4510
4511// Make sure we have a good guess for PostBB. If QTB's only successor is
4512// QFB, then QFB is a better PostBB.
4513if (QTB->getSingleSuccessor() == QFB)
4514 PostBB = QFB;
4515
4516// If we couldn't find a good PostBB, stop.
4517if (!PostBB)
4518returnfalse;
4519
4520bool InvertPCond =false, InvertQCond =false;
4521// Canonicalize fallthroughs to the true branches.
4522if (PFB == QBI->getParent()) {
4523std::swap(PFB, PTB);
4524 InvertPCond =true;
4525 }
4526if (QFB == PostBB) {
4527std::swap(QFB, QTB);
4528 InvertQCond =true;
4529 }
4530
4531// From this point on we can assume PTB or QTB may be fallthroughs but PFB
4532// and QFB may not. Model fallthroughs as a nullptr block.
4533if (PTB == QBI->getParent())
4534 PTB =nullptr;
4535if (QTB == PostBB)
4536 QTB =nullptr;
4537
4538// Legality bailouts. We must have at least the non-fallthrough blocks and
4539// the post-dominating block, and the non-fallthroughs must only have one
4540// predecessor.
4541auto HasOnePredAndOneSucc = [](BasicBlock *BB,BasicBlock *P,BasicBlock *S) {
4542return BB->getSinglePredecessor() ==P && BB->getSingleSuccessor() == S;
4543 };
4544if (!HasOnePredAndOneSucc(PFB, PBI->getParent(), QBI->getParent()) ||
4545 !HasOnePredAndOneSucc(QFB, QBI->getParent(), PostBB))
4546returnfalse;
4547if ((PTB && !HasOnePredAndOneSucc(PTB, PBI->getParent(), QBI->getParent())) ||
4548 (QTB && !HasOnePredAndOneSucc(QTB, QBI->getParent(), PostBB)))
4549returnfalse;
4550if (!QBI->getParent()->hasNUses(2))
4551returnfalse;
4552
4553// OK, this is a sequence of two diamonds or triangles.
4554// Check if there are stores in PTB or PFB that are repeated in QTB or QFB.
4555SmallPtrSet<Value *, 4> PStoreAddresses, QStoreAddresses;
4556for (auto *BB : {PTB, PFB}) {
4557if (!BB)
4558continue;
4559for (auto &I : *BB)
4560if (StoreInst *SI = dyn_cast<StoreInst>(&I))
4561 PStoreAddresses.insert(SI->getPointerOperand());
4562 }
4563for (auto *BB : {QTB, QFB}) {
4564if (!BB)
4565continue;
4566for (auto &I : *BB)
4567if (StoreInst *SI = dyn_cast<StoreInst>(&I))
4568 QStoreAddresses.insert(SI->getPointerOperand());
4569 }
4570
4571set_intersect(PStoreAddresses, QStoreAddresses);
4572// set_intersect mutates PStoreAddresses in place. Rename it here to make it
4573// clear what it contains.
4574auto &CommonAddresses = PStoreAddresses;
4575
4576bool Changed =false;
4577for (auto *Address : CommonAddresses)
4578 Changed |=
4579mergeConditionalStoreToAddress(PTB, PFB, QTB, QFB, PostBB,Address,
4580 InvertPCond, InvertQCond, DTU,DL,TTI);
4581return Changed;
4582}
4583
4584/// If the previous block ended with a widenable branch, determine if reusing
4585/// the target block is profitable and legal. This will have the effect of
4586/// "widening" PBI, but doesn't require us to reason about hosting safety.
4587staticbooltryWidenCondBranchToCondBranch(BranchInst *PBI,BranchInst *BI,
4588DomTreeUpdater *DTU) {
4589// TODO: This can be generalized in two important ways:
4590// 1) We can allow phi nodes in IfFalseBB and simply reuse all the input
4591// values from the PBI edge.
4592// 2) We can sink side effecting instructions into BI's fallthrough
4593// successor provided they doesn't contribute to computation of
4594// BI's condition.
4595BasicBlock *IfTrueBB = PBI->getSuccessor(0);
4596BasicBlock *IfFalseBB = PBI->getSuccessor(1);
4597if (!isWidenableBranch(PBI) || IfTrueBB != BI->getParent() ||
4598 !BI->getParent()->getSinglePredecessor())
4599returnfalse;
4600if (!IfFalseBB->phis().empty())
4601returnfalse;// TODO
4602// This helps avoid infinite loop with SimplifyCondBranchToCondBranch which
4603// may undo the transform done here.
4604// TODO: There might be a more fine-grained solution to this.
4605if (!llvm::succ_empty(IfFalseBB))
4606returnfalse;
4607// Use lambda to lazily compute expensive condition after cheap ones.
4608auto NoSideEffects = [](BasicBlock &BB) {
4609returnllvm::none_of(BB, [](constInstruction &I) {
4610returnI.mayWriteToMemory() ||I.mayHaveSideEffects();
4611 });
4612 };
4613if (BI->getSuccessor(1) != IfFalseBB &&// no inf looping
4614 BI->getSuccessor(1)->getTerminatingDeoptimizeCall() &&// profitability
4615 NoSideEffects(*BI->getParent())) {
4616auto *OldSuccessor = BI->getSuccessor(1);
4617 OldSuccessor->removePredecessor(BI->getParent());
4618 BI->setSuccessor(1, IfFalseBB);
4619if (DTU)
4620 DTU->applyUpdates(
4621 {{DominatorTree::Insert, BI->getParent(), IfFalseBB},
4622 {DominatorTree::Delete, BI->getParent(), OldSuccessor}});
4623returntrue;
4624 }
4625if (BI->getSuccessor(0) != IfFalseBB &&// no inf looping
4626 BI->getSuccessor(0)->getTerminatingDeoptimizeCall() &&// profitability
4627 NoSideEffects(*BI->getParent())) {
4628auto *OldSuccessor = BI->getSuccessor(0);
4629 OldSuccessor->removePredecessor(BI->getParent());
4630 BI->setSuccessor(0, IfFalseBB);
4631if (DTU)
4632 DTU->applyUpdates(
4633 {{DominatorTree::Insert, BI->getParent(), IfFalseBB},
4634 {DominatorTree::Delete, BI->getParent(), OldSuccessor}});
4635returntrue;
4636 }
4637returnfalse;
4638}
4639
4640/// If we have a conditional branch as a predecessor of another block,
4641/// this function tries to simplify it. We know
4642/// that PBI and BI are both conditional branches, and BI is in one of the
4643/// successor blocks of PBI - PBI branches to BI.
4644staticboolSimplifyCondBranchToCondBranch(BranchInst *PBI,BranchInst *BI,
4645DomTreeUpdater *DTU,
4646constDataLayout &DL,
4647constTargetTransformInfo &TTI) {
4648assert(PBI->isConditional() && BI->isConditional());
4649BasicBlock *BB = BI->getParent();
4650
4651// If this block ends with a branch instruction, and if there is a
4652// predecessor that ends on a branch of the same condition, make
4653// this conditional branch redundant.
4654if (PBI->getCondition() == BI->getCondition() &&
4655 PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
4656// Okay, the outcome of this conditional branch is statically
4657// knowable. If this block had a single pred, handle specially, otherwise
4658// foldCondBranchOnValueKnownInPredecessor() will handle it.
4659if (BB->getSinglePredecessor()) {
4660// Turn this into a branch on constant.
4661bool CondIsTrue = PBI->getSuccessor(0) == BB;
4662 BI->setCondition(
4663 ConstantInt::get(Type::getInt1Ty(BB->getContext()), CondIsTrue));
4664returntrue;// Nuke the branch on constant.
4665 }
4666 }
4667
4668// If the previous block ended with a widenable branch, determine if reusing
4669// the target block is profitable and legal. This will have the effect of
4670// "widening" PBI, but doesn't require us to reason about hosting safety.
4671if (tryWidenCondBranchToCondBranch(PBI, BI, DTU))
4672returntrue;
4673
4674// If both branches are conditional and both contain stores to the same
4675// address, remove the stores from the conditionals and create a conditional
4676// merged store at the end.
4677if (MergeCondStores &&mergeConditionalStores(PBI, BI, DTU,DL,TTI))
4678returntrue;
4679
4680// If this is a conditional branch in an empty block, and if any
4681// predecessors are a conditional branch to one of our destinations,
4682// fold the conditions into logical ops and one cond br.
4683
4684// Ignore dbg intrinsics.
4685if (&*BB->instructionsWithoutDebug(false).begin() != BI)
4686returnfalse;
4687
4688int PBIOp, BIOp;
4689if (PBI->getSuccessor(0) == BI->getSuccessor(0)) {
4690 PBIOp = 0;
4691 BIOp = 0;
4692 }elseif (PBI->getSuccessor(0) == BI->getSuccessor(1)) {
4693 PBIOp = 0;
4694 BIOp = 1;
4695 }elseif (PBI->getSuccessor(1) == BI->getSuccessor(0)) {
4696 PBIOp = 1;
4697 BIOp = 0;
4698 }elseif (PBI->getSuccessor(1) == BI->getSuccessor(1)) {
4699 PBIOp = 1;
4700 BIOp = 1;
4701 }else {
4702returnfalse;
4703 }
4704
4705// Check to make sure that the other destination of this branch
4706// isn't BB itself. If so, this is an infinite loop that will
4707// keep getting unwound.
4708if (PBI->getSuccessor(PBIOp) == BB)
4709returnfalse;
4710
4711// If predecessor's branch probability to BB is too low don't merge branches.
4712SmallVector<uint32_t, 2> PredWeights;
4713if (!PBI->getMetadata(LLVMContext::MD_unpredictable) &&
4714extractBranchWeights(*PBI, PredWeights) &&
4715 (static_cast<uint64_t>(PredWeights[0]) + PredWeights[1]) != 0) {
4716
4717BranchProbability CommonDestProb =BranchProbability::getBranchProbability(
4718 PredWeights[PBIOp],
4719static_cast<uint64_t>(PredWeights[0]) + PredWeights[1]);
4720
4721BranchProbability Likely =TTI.getPredictableBranchThreshold();
4722if (CommonDestProb >= Likely)
4723returnfalse;
4724 }
4725
4726// Do not perform this transformation if it would require
4727// insertion of a large number of select instructions. For targets
4728// without predication/cmovs, this is a big pessimization.
4729
4730BasicBlock *CommonDest = PBI->getSuccessor(PBIOp);
4731BasicBlock *RemovedDest = PBI->getSuccessor(PBIOp ^ 1);
4732unsigned NumPhis = 0;
4733for (BasicBlock::iteratorII = CommonDest->begin(); isa<PHINode>(II);
4734 ++II, ++NumPhis) {
4735if (NumPhis > 2)// Disable this xform.
4736returnfalse;
4737 }
4738
4739// Finally, if everything is ok, fold the branches to logical ops.
4740BasicBlock *OtherDest = BI->getSuccessor(BIOp ^ 1);
4741
4742LLVM_DEBUG(dbgs() <<"FOLDING BRs:" << *PBI->getParent()
4743 <<"AND: " << *BI->getParent());
4744
4745SmallVector<DominatorTree::UpdateType, 5> Updates;
4746
4747// If OtherDest *is* BB, then BB is a basic block with a single conditional
4748// branch in it, where one edge (OtherDest) goes back to itself but the other
4749// exits. We don't *know* that the program avoids the infinite loop
4750// (even though that seems likely). If we do this xform naively, we'll end up
4751// recursively unpeeling the loop. Since we know that (after the xform is
4752// done) that the block *is* infinite if reached, we just make it an obviously
4753// infinite loop with no cond branch.
4754if (OtherDest == BB) {
4755// Insert it at the end of the function, because it's either code,
4756// or it won't matter if it's hot. :)
4757BasicBlock *InfLoopBlock =
4758BasicBlock::Create(BB->getContext(),"infloop", BB->getParent());
4759BranchInst::Create(InfLoopBlock, InfLoopBlock);
4760if (DTU)
4761 Updates.push_back({DominatorTree::Insert, InfLoopBlock, InfLoopBlock});
4762 OtherDest = InfLoopBlock;
4763 }
4764
4765LLVM_DEBUG(dbgs() << *PBI->getParent()->getParent());
4766
4767// BI may have other predecessors. Because of this, we leave
4768// it alone, but modify PBI.
4769
4770// Make sure we get to CommonDest on True&True directions.
4771Value *PBICond = PBI->getCondition();
4772IRBuilder<NoFolder> Builder(PBI);
4773if (PBIOp)
4774 PBICond = Builder.CreateNot(PBICond, PBICond->getName() +".not");
4775
4776Value *BICond = BI->getCondition();
4777if (BIOp)
4778 BICond = Builder.CreateNot(BICond, BICond->getName() +".not");
4779
4780// Merge the conditions.
4781Value *Cond =
4782createLogicalOp(Builder, Instruction::Or, PBICond, BICond,"brmerge");
4783
4784// Modify PBI to branch on the new condition to the new dests.
4785 PBI->setCondition(Cond);
4786 PBI->setSuccessor(0, CommonDest);
4787 PBI->setSuccessor(1, OtherDest);
4788
4789if (DTU) {
4790 Updates.push_back({DominatorTree::Insert, PBI->getParent(), OtherDest});
4791 Updates.push_back({DominatorTree::Delete, PBI->getParent(), RemovedDest});
4792
4793 DTU->applyUpdates(Updates);
4794 }
4795
4796// Update branch weight for PBI.
4797uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
4798uint64_t PredCommon, PredOther, SuccCommon, SuccOther;
4799bool HasWeights =
4800extractPredSuccWeights(PBI, BI, PredTrueWeight, PredFalseWeight,
4801 SuccTrueWeight, SuccFalseWeight);
4802if (HasWeights) {
4803 PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
4804 PredOther = PBIOp ? PredTrueWeight : PredFalseWeight;
4805 SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
4806 SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
4807// The weight to CommonDest should be PredCommon * SuccTotal +
4808// PredOther * SuccCommon.
4809// The weight to OtherDest should be PredOther * SuccOther.
4810uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther) +
4811 PredOther * SuccCommon,
4812 PredOther * SuccOther};
4813// Halve the weights if any of them cannot fit in an uint32_t
4814fitWeights(NewWeights);
4815
4816setBranchWeights(PBI, NewWeights[0], NewWeights[1],/*IsExpected=*/false);
4817 }
4818
4819// OtherDest may have phi nodes. If so, add an entry from PBI's
4820// block that are identical to the entries for BI's block.
4821addPredecessorToBlock(OtherDest, PBI->getParent(), BB);
4822
4823// We know that the CommonDest already had an edge from PBI to
4824// it. If it has PHIs though, the PHIs may have different
4825// entries for BB and PBI's BB. If so, insert a select to make
4826// them agree.
4827for (PHINode &PN : CommonDest->phis()) {
4828Value *BIV = PN.getIncomingValueForBlock(BB);
4829unsigned PBBIdx = PN.getBasicBlockIndex(PBI->getParent());
4830Value *PBIV = PN.getIncomingValue(PBBIdx);
4831if (BIV != PBIV) {
4832// Insert a select in PBI to pick the right value.
4833SelectInst *NV = cast<SelectInst>(
4834 Builder.CreateSelect(PBICond, PBIV, BIV, PBIV->getName() +".mux"));
4835 PN.setIncomingValue(PBBIdx, NV);
4836// Although the select has the same condition as PBI, the original branch
4837// weights for PBI do not apply to the new select because the select's
4838// 'logical' edges are incoming edges of the phi that is eliminated, not
4839// the outgoing edges of PBI.
4840if (HasWeights) {
4841uint64_t PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
4842uint64_t PredOther = PBIOp ? PredTrueWeight : PredFalseWeight;
4843uint64_t SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
4844uint64_t SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
4845// The weight to PredCommonDest should be PredCommon * SuccTotal.
4846// The weight to PredOtherDest should be PredOther * SuccCommon.
4847uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther),
4848 PredOther * SuccCommon};
4849
4850fitWeights(NewWeights);
4851
4852setBranchWeights(NV, NewWeights[0], NewWeights[1],
4853/*IsExpected=*/false);
4854 }
4855 }
4856 }
4857
4858LLVM_DEBUG(dbgs() <<"INTO: " << *PBI->getParent());
4859LLVM_DEBUG(dbgs() << *PBI->getParent()->getParent());
4860
4861// This basic block is probably dead. We know it has at least
4862// one fewer predecessor.
4863returntrue;
4864}
4865
4866// Simplifies a terminator by replacing it with a branch to TrueBB if Cond is
4867// true or to FalseBB if Cond is false.
4868// Takes care of updating the successors and removing the old terminator.
4869// Also makes sure not to introduce new successors by assuming that edges to
4870// non-successor TrueBBs and FalseBBs aren't reachable.
4871bool SimplifyCFGOpt::simplifyTerminatorOnSelect(Instruction *OldTerm,
4872Value *Cond,BasicBlock *TrueBB,
4873BasicBlock *FalseBB,
4874uint32_t TrueWeight,
4875uint32_t FalseWeight) {
4876auto *BB = OldTerm->getParent();
4877// Remove any superfluous successor edges from the CFG.
4878// First, figure out which successors to preserve.
4879// If TrueBB and FalseBB are equal, only try to preserve one copy of that
4880// successor.
4881BasicBlock *KeepEdge1 = TrueBB;
4882BasicBlock *KeepEdge2 = TrueBB != FalseBB ? FalseBB :nullptr;
4883
4884SmallSetVector<BasicBlock *, 2> RemovedSuccessors;
4885
4886// Then remove the rest.
4887for (BasicBlock *Succ :successors(OldTerm)) {
4888// Make sure only to keep exactly one copy of each edge.
4889if (Succ == KeepEdge1)
4890 KeepEdge1 =nullptr;
4891elseif (Succ == KeepEdge2)
4892 KeepEdge2 =nullptr;
4893else {
4894 Succ->removePredecessor(BB,
4895/*KeepOneInputPHIs=*/true);
4896
4897if (Succ != TrueBB && Succ != FalseBB)
4898 RemovedSuccessors.insert(Succ);
4899 }
4900 }
4901
4902IRBuilder<> Builder(OldTerm);
4903 Builder.SetCurrentDebugLocation(OldTerm->getDebugLoc());
4904
4905// Insert an appropriate new terminator.
4906if (!KeepEdge1 && !KeepEdge2) {
4907if (TrueBB == FalseBB) {
4908// We were only looking for one successor, and it was present.
4909// Create an unconditional branch to it.
4910 Builder.CreateBr(TrueBB);
4911 }else {
4912// We found both of the successors we were looking for.
4913// Create a conditional branch sharing the condition of the select.
4914BranchInst *NewBI = Builder.CreateCondBr(Cond, TrueBB, FalseBB);
4915if (TrueWeight != FalseWeight)
4916setBranchWeights(NewBI, TrueWeight, FalseWeight,/*IsExpected=*/false);
4917 }
4918 }elseif (KeepEdge1 && (KeepEdge2 || TrueBB == FalseBB)) {
4919// Neither of the selected blocks were successors, so this
4920// terminator must be unreachable.
4921newUnreachableInst(OldTerm->getContext(), OldTerm->getIterator());
4922 }else {
4923// One of the selected values was a successor, but the other wasn't.
4924// Insert an unconditional branch to the one that was found;
4925// the edge to the one that wasn't must be unreachable.
4926if (!KeepEdge1) {
4927// Only TrueBB was found.
4928 Builder.CreateBr(TrueBB);
4929 }else {
4930// Only FalseBB was found.
4931 Builder.CreateBr(FalseBB);
4932 }
4933 }
4934
4935eraseTerminatorAndDCECond(OldTerm);
4936
4937if (DTU) {
4938SmallVector<DominatorTree::UpdateType, 2> Updates;
4939 Updates.reserve(RemovedSuccessors.size());
4940for (auto *RemovedSuccessor : RemovedSuccessors)
4941 Updates.push_back({DominatorTree::Delete, BB, RemovedSuccessor});
4942 DTU->applyUpdates(Updates);
4943 }
4944
4945returntrue;
4946}
4947
4948// Replaces
4949// (switch (select cond, X, Y)) on constant X, Y
4950// with a branch - conditional if X and Y lead to distinct BBs,
4951// unconditional otherwise.
4952bool SimplifyCFGOpt::simplifySwitchOnSelect(SwitchInst *SI,
4953SelectInst *Select) {
4954// Check for constant integer values in the select.
4955ConstantInt *TrueVal = dyn_cast<ConstantInt>(Select->getTrueValue());
4956ConstantInt *FalseVal = dyn_cast<ConstantInt>(Select->getFalseValue());
4957if (!TrueVal || !FalseVal)
4958returnfalse;
4959
4960// Find the relevant condition and destinations.
4961Value *Condition =Select->getCondition();
4962BasicBlock *TrueBB =SI->findCaseValue(TrueVal)->getCaseSuccessor();
4963BasicBlock *FalseBB =SI->findCaseValue(FalseVal)->getCaseSuccessor();
4964
4965// Get weight for TrueBB and FalseBB.
4966uint32_t TrueWeight = 0, FalseWeight = 0;
4967SmallVector<uint64_t, 8> Weights;
4968bool HasWeights =hasBranchWeightMD(*SI);
4969if (HasWeights) {
4970getBranchWeights(SI, Weights);
4971if (Weights.size() == 1 +SI->getNumCases()) {
4972 TrueWeight =
4973 (uint32_t)Weights[SI->findCaseValue(TrueVal)->getSuccessorIndex()];
4974 FalseWeight =
4975 (uint32_t)Weights[SI->findCaseValue(FalseVal)->getSuccessorIndex()];
4976 }
4977 }
4978
4979// Perform the actual simplification.
4980return simplifyTerminatorOnSelect(SI, Condition, TrueBB, FalseBB, TrueWeight,
4981 FalseWeight);
4982}
4983
4984// Replaces
4985// (indirectbr (select cond, blockaddress(@fn, BlockA),
4986// blockaddress(@fn, BlockB)))
4987// with
4988// (br cond, BlockA, BlockB).
4989bool SimplifyCFGOpt::simplifyIndirectBrOnSelect(IndirectBrInst *IBI,
4990SelectInst *SI) {
4991// Check that both operands of the select are block addresses.
4992BlockAddress *TBA = dyn_cast<BlockAddress>(SI->getTrueValue());
4993BlockAddress *FBA = dyn_cast<BlockAddress>(SI->getFalseValue());
4994if (!TBA || !FBA)
4995returnfalse;
4996
4997// Extract the actual blocks.
4998BasicBlock *TrueBB = TBA->getBasicBlock();
4999BasicBlock *FalseBB = FBA->getBasicBlock();
5000
5001// Perform the actual simplification.
5002return simplifyTerminatorOnSelect(IBI,SI->getCondition(), TrueBB, FalseBB, 0,
5003 0);
5004}
5005
5006/// This is called when we find an icmp instruction
5007/// (a seteq/setne with a constant) as the only instruction in a
5008/// block that ends with an uncond branch. We are looking for a very specific
5009/// pattern that occurs when "A == 1 || A == 2 || A == 3" gets simplified. In
5010/// this case, we merge the first two "or's of icmp" into a switch, but then the
5011/// default value goes to an uncond block with a seteq in it, we get something
5012/// like:
5013///
5014/// switch i8 %A, label %DEFAULT [ i8 1, label %end i8 2, label %end ]
5015/// DEFAULT:
5016/// %tmp = icmp eq i8 %A, 92
5017/// br label %end
5018/// end:
5019/// ... = phi i1 [ true, %entry ], [ %tmp, %DEFAULT ], [ true, %entry ]
5020///
5021/// We prefer to split the edge to 'end' so that there is a true/false entry to
5022/// the PHI, merging the third icmp into the switch.
5023bool SimplifyCFGOpt::tryToSimplifyUncondBranchWithICmpInIt(
5024ICmpInst *ICI,IRBuilder<> &Builder) {
5025BasicBlock *BB = ICI->getParent();
5026
5027// If the block has any PHIs in it or the icmp has multiple uses, it is too
5028// complex.
5029if (isa<PHINode>(BB->begin()) || !ICI->hasOneUse())
5030returnfalse;
5031
5032Value *V = ICI->getOperand(0);
5033ConstantInt *Cst = cast<ConstantInt>(ICI->getOperand(1));
5034
5035// The pattern we're looking for is where our only predecessor is a switch on
5036// 'V' and this block is the default case for the switch. In this case we can
5037// fold the compared value into the switch to simplify things.
5038BasicBlock *Pred = BB->getSinglePredecessor();
5039if (!Pred || !isa<SwitchInst>(Pred->getTerminator()))
5040returnfalse;
5041
5042SwitchInst *SI = cast<SwitchInst>(Pred->getTerminator());
5043if (SI->getCondition() != V)
5044returnfalse;
5045
5046// If BB is reachable on a non-default case, then we simply know the value of
5047// V in this block. Substitute it and constant fold the icmp instruction
5048// away.
5049if (SI->getDefaultDest() != BB) {
5050ConstantInt *VVal =SI->findCaseDest(BB);
5051assert(VVal &&"Should have a unique destination value");
5052 ICI->setOperand(0, VVal);
5053
5054if (Value *V =simplifyInstruction(ICI, {DL, ICI})) {
5055 ICI->replaceAllUsesWith(V);
5056 ICI->eraseFromParent();
5057 }
5058// BB is now empty, so it is likely to simplify away.
5059return requestResimplify();
5060 }
5061
5062// Ok, the block is reachable from the default dest. If the constant we're
5063// comparing exists in one of the other edges, then we can constant fold ICI
5064// and zap it.
5065if (SI->findCaseValue(Cst) !=SI->case_default()) {
5066Value *V;
5067if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
5068V =ConstantInt::getFalse(BB->getContext());
5069else
5070V =ConstantInt::getTrue(BB->getContext());
5071
5072 ICI->replaceAllUsesWith(V);
5073 ICI->eraseFromParent();
5074// BB is now empty, so it is likely to simplify away.
5075return requestResimplify();
5076 }
5077
5078// The use of the icmp has to be in the 'end' block, by the only PHI node in
5079// the block.
5080BasicBlock *SuccBlock = BB->getTerminator()->getSuccessor(0);
5081PHINode *PHIUse = dyn_cast<PHINode>(ICI->user_back());
5082if (PHIUse ==nullptr || PHIUse != &SuccBlock->front() ||
5083 isa<PHINode>(++BasicBlock::iterator(PHIUse)))
5084returnfalse;
5085
5086// If the icmp is a SETEQ, then the default dest gets false, the new edge gets
5087// true in the PHI.
5088Constant *DefaultCst =ConstantInt::getTrue(BB->getContext());
5089Constant *NewCst =ConstantInt::getFalse(BB->getContext());
5090
5091if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
5092std::swap(DefaultCst, NewCst);
5093
5094// Replace ICI (which is used by the PHI for the default value) with true or
5095// false depending on if it is EQ or NE.
5096 ICI->replaceAllUsesWith(DefaultCst);
5097 ICI->eraseFromParent();
5098
5099SmallVector<DominatorTree::UpdateType, 2> Updates;
5100
5101// Okay, the switch goes to this block on a default value. Add an edge from
5102// the switch to the merge point on the compared value.
5103BasicBlock *NewBB =
5104BasicBlock::Create(BB->getContext(),"switch.edge", BB->getParent(), BB);
5105 {
5106SwitchInstProfUpdateWrapper SIW(*SI);
5107auto W0 = SIW.getSuccessorWeight(0);
5108SwitchInstProfUpdateWrapper::CaseWeightOpt NewW;
5109if (W0) {
5110 NewW = ((uint64_t(*W0) + 1) >> 1);
5111 SIW.setSuccessorWeight(0, *NewW);
5112 }
5113 SIW.addCase(Cst, NewBB, NewW);
5114if (DTU)
5115 Updates.push_back({DominatorTree::Insert, Pred, NewBB});
5116 }
5117
5118// NewBB branches to the phi block, add the uncond branch and the phi entry.
5119 Builder.SetInsertPoint(NewBB);
5120 Builder.SetCurrentDebugLocation(SI->getDebugLoc());
5121 Builder.CreateBr(SuccBlock);
5122 PHIUse->addIncoming(NewCst, NewBB);
5123if (DTU) {
5124 Updates.push_back({DominatorTree::Insert, NewBB, SuccBlock});
5125 DTU->applyUpdates(Updates);
5126 }
5127returntrue;
5128}
5129
5130/// The specified branch is a conditional branch.
5131/// Check to see if it is branching on an or/and chain of icmp instructions, and
5132/// fold it into a switch instruction if so.
5133bool SimplifyCFGOpt::simplifyBranchOnICmpChain(BranchInst *BI,
5134IRBuilder<> &Builder,
5135constDataLayout &DL) {
5136Instruction *Cond = dyn_cast<Instruction>(BI->getCondition());
5137if (!Cond)
5138returnfalse;
5139
5140// Change br (X == 0 | X == 1), T, F into a switch instruction.
5141// If this is a bunch of seteq's or'd together, or if it's a bunch of
5142// 'setne's and'ed together, collect them.
5143
5144// Try to gather values from a chain of and/or to be turned into a switch
5145 ConstantComparesGatherer ConstantCompare(Cond,DL);
5146// Unpack the result
5147SmallVectorImpl<ConstantInt *> &Values = ConstantCompare.Vals;
5148Value *CompVal = ConstantCompare.CompValue;
5149unsigned UsedICmps = ConstantCompare.UsedICmps;
5150Value *ExtraCase = ConstantCompare.Extra;
5151
5152// If we didn't have a multiply compared value, fail.
5153if (!CompVal)
5154returnfalse;
5155
5156// Avoid turning single icmps into a switch.
5157if (UsedICmps <= 1)
5158returnfalse;
5159
5160bool TrueWhenEqual =match(Cond,m_LogicalOr(m_Value(),m_Value()));
5161
5162// There might be duplicate constants in the list, which the switch
5163// instruction can't handle, remove them now.
5164array_pod_sort(Values.begin(), Values.end(),constantIntSortPredicate);
5165 Values.erase(llvm::unique(Values), Values.end());
5166
5167// If Extra was used, we require at least two switch values to do the
5168// transformation. A switch with one value is just a conditional branch.
5169if (ExtraCase && Values.size() < 2)
5170returnfalse;
5171
5172// TODO: Preserve branch weight metadata, similarly to how
5173// foldValueComparisonIntoPredecessors preserves it.
5174
5175// Figure out which block is which destination.
5176BasicBlock *DefaultBB = BI->getSuccessor(1);
5177BasicBlock *EdgeBB = BI->getSuccessor(0);
5178if (!TrueWhenEqual)
5179std::swap(DefaultBB, EdgeBB);
5180
5181BasicBlock *BB = BI->getParent();
5182
5183LLVM_DEBUG(dbgs() <<"Converting 'icmp' chain with " << Values.size()
5184 <<" cases into SWITCH. BB is:\n"
5185 << *BB);
5186
5187SmallVector<DominatorTree::UpdateType, 2> Updates;
5188
5189// If there are any extra values that couldn't be folded into the switch
5190// then we evaluate them with an explicit branch first. Split the block
5191// right before the condbr to handle it.
5192if (ExtraCase) {
5193BasicBlock *NewBB =SplitBlock(BB, BI, DTU,/*LI=*/nullptr,
5194/*MSSAU=*/nullptr,"switch.early.test");
5195
5196// Remove the uncond branch added to the old block.
5197Instruction *OldTI = BB->getTerminator();
5198 Builder.SetInsertPoint(OldTI);
5199
5200// There can be an unintended UB if extra values are Poison. Before the
5201// transformation, extra values may not be evaluated according to the
5202// condition, and it will not raise UB. But after transformation, we are
5203// evaluating extra values before checking the condition, and it will raise
5204// UB. It can be solved by adding freeze instruction to extra values.
5205AssumptionCache *AC =Options.AC;
5206
5207if (!isGuaranteedNotToBeUndefOrPoison(ExtraCase, AC, BI,nullptr))
5208 ExtraCase = Builder.CreateFreeze(ExtraCase);
5209
5210if (TrueWhenEqual)
5211 Builder.CreateCondBr(ExtraCase, EdgeBB, NewBB);
5212else
5213 Builder.CreateCondBr(ExtraCase, NewBB, EdgeBB);
5214
5215 OldTI->eraseFromParent();
5216
5217if (DTU)
5218 Updates.push_back({DominatorTree::Insert, BB, EdgeBB});
5219
5220// If there are PHI nodes in EdgeBB, then we need to add a new entry to them
5221// for the edge we just added.
5222addPredecessorToBlock(EdgeBB, BB, NewBB);
5223
5224LLVM_DEBUG(dbgs() <<" ** 'icmp' chain unhandled condition: " << *ExtraCase
5225 <<"\nEXTRABB = " << *BB);
5226 BB = NewBB;
5227 }
5228
5229 Builder.SetInsertPoint(BI);
5230// Convert pointer to int before we switch.
5231if (CompVal->getType()->isPointerTy()) {
5232 CompVal = Builder.CreatePtrToInt(
5233 CompVal,DL.getIntPtrType(CompVal->getType()),"magicptr");
5234 }
5235
5236// Create the new switch instruction now.
5237SwitchInst *New = Builder.CreateSwitch(CompVal, DefaultBB, Values.size());
5238
5239// Add all of the 'cases' to the switch instruction.
5240for (unsigned i = 0, e = Values.size(); i != e; ++i)
5241New->addCase(Values[i], EdgeBB);
5242
5243// We added edges from PI to the EdgeBB. As such, if there were any
5244// PHI nodes in EdgeBB, they need entries to be added corresponding to
5245// the number of edges added.
5246for (BasicBlock::iterator BBI = EdgeBB->begin(); isa<PHINode>(BBI); ++BBI) {
5247PHINode *PN = cast<PHINode>(BBI);
5248Value *InVal = PN->getIncomingValueForBlock(BB);
5249for (unsigned i = 0, e = Values.size() - 1; i != e; ++i)
5250 PN->addIncoming(InVal, BB);
5251 }
5252
5253// Erase the old branch instruction.
5254eraseTerminatorAndDCECond(BI);
5255if (DTU)
5256 DTU->applyUpdates(Updates);
5257
5258LLVM_DEBUG(dbgs() <<" ** 'icmp' chain result is:\n" << *BB <<'\n');
5259returntrue;
5260}
5261
5262bool SimplifyCFGOpt::simplifyResume(ResumeInst *RI,IRBuilder<> &Builder) {
5263if (isa<PHINode>(RI->getValue()))
5264return simplifyCommonResume(RI);
5265elseif (isa<LandingPadInst>(RI->getParent()->getFirstNonPHIIt()) &&
5266 RI->getValue() == &*RI->getParent()->getFirstNonPHIIt())
5267// The resume must unwind the exception that caused control to branch here.
5268return simplifySingleResume(RI);
5269
5270returnfalse;
5271}
5272
5273// Check if cleanup block is empty
5274staticboolisCleanupBlockEmpty(iterator_range<BasicBlock::iterator> R) {
5275for (Instruction &I : R) {
5276auto *II = dyn_cast<IntrinsicInst>(&I);
5277if (!II)
5278returnfalse;
5279
5280Intrinsic::ID IntrinsicID =II->getIntrinsicID();
5281switch (IntrinsicID) {
5282case Intrinsic::dbg_declare:
5283case Intrinsic::dbg_value:
5284case Intrinsic::dbg_label:
5285case Intrinsic::lifetime_end:
5286break;
5287default:
5288returnfalse;
5289 }
5290 }
5291returntrue;
5292}
5293
5294// Simplify resume that is shared by several landing pads (phi of landing pad).
5295bool SimplifyCFGOpt::simplifyCommonResume(ResumeInst *RI) {
5296BasicBlock *BB = RI->getParent();
5297
5298// Check that there are no other instructions except for debug and lifetime
5299// intrinsics between the phi's and resume instruction.
5300if (!isCleanupBlockEmpty(make_range(RI->getParent()->getFirstNonPHIIt(),
5301 BB->getTerminator()->getIterator())))
5302returnfalse;
5303
5304SmallSetVector<BasicBlock *, 4> TrivialUnwindBlocks;
5305auto *PhiLPInst = cast<PHINode>(RI->getValue());
5306
5307// Check incoming blocks to see if any of them are trivial.
5308for (unsignedIdx = 0,End = PhiLPInst->getNumIncomingValues();Idx !=End;
5309Idx++) {
5310auto *IncomingBB = PhiLPInst->getIncomingBlock(Idx);
5311auto *IncomingValue = PhiLPInst->getIncomingValue(Idx);
5312
5313// If the block has other successors, we can not delete it because
5314// it has other dependents.
5315if (IncomingBB->getUniqueSuccessor() != BB)
5316continue;
5317
5318auto *LandingPad = dyn_cast<LandingPadInst>(IncomingBB->getFirstNonPHIIt());
5319// Not the landing pad that caused the control to branch here.
5320if (IncomingValue != LandingPad)
5321continue;
5322
5323if (isCleanupBlockEmpty(
5324make_range(LandingPad->getNextNode(), IncomingBB->getTerminator())))
5325 TrivialUnwindBlocks.insert(IncomingBB);
5326 }
5327
5328// If no trivial unwind blocks, don't do any simplifications.
5329if (TrivialUnwindBlocks.empty())
5330returnfalse;
5331
5332// Turn all invokes that unwind here into calls.
5333for (auto *TrivialBB : TrivialUnwindBlocks) {
5334// Blocks that will be simplified should be removed from the phi node.
5335// Note there could be multiple edges to the resume block, and we need
5336// to remove them all.
5337while (PhiLPInst->getBasicBlockIndex(TrivialBB) != -1)
5338 BB->removePredecessor(TrivialBB,true);
5339
5340for (BasicBlock *Pred :
5341llvm::make_early_inc_range(predecessors(TrivialBB))) {
5342removeUnwindEdge(Pred, DTU);
5343 ++NumInvokes;
5344 }
5345
5346// In each SimplifyCFG run, only the current processed block can be erased.
5347// Otherwise, it will break the iteration of SimplifyCFG pass. So instead
5348// of erasing TrivialBB, we only remove the branch to the common resume
5349// block so that we can later erase the resume block since it has no
5350// predecessors.
5351 TrivialBB->getTerminator()->eraseFromParent();
5352newUnreachableInst(RI->getContext(), TrivialBB);
5353if (DTU)
5354 DTU->applyUpdates({{DominatorTree::Delete, TrivialBB, BB}});
5355 }
5356
5357// Delete the resume block if all its predecessors have been removed.
5358if (pred_empty(BB))
5359DeleteDeadBlock(BB, DTU);
5360
5361return !TrivialUnwindBlocks.empty();
5362}
5363
5364// Simplify resume that is only used by a single (non-phi) landing pad.
5365bool SimplifyCFGOpt::simplifySingleResume(ResumeInst *RI) {
5366BasicBlock *BB = RI->getParent();
5367auto *LPInst = cast<LandingPadInst>(BB->getFirstNonPHIIt());
5368assert(RI->getValue() == LPInst &&
5369"Resume must unwind the exception that caused control to here");
5370
5371// Check that there are no other instructions except for debug intrinsics.
5372if (!isCleanupBlockEmpty(
5373 make_range<Instruction *>(LPInst->getNextNode(), RI)))
5374returnfalse;
5375
5376// Turn all invokes that unwind here into calls and delete the basic block.
5377for (BasicBlock *Pred :llvm::make_early_inc_range(predecessors(BB))) {
5378removeUnwindEdge(Pred, DTU);
5379 ++NumInvokes;
5380 }
5381
5382// The landingpad is now unreachable. Zap it.
5383DeleteDeadBlock(BB, DTU);
5384returntrue;
5385}
5386
5387staticboolremoveEmptyCleanup(CleanupReturnInst *RI,DomTreeUpdater *DTU) {
5388// If this is a trivial cleanup pad that executes no instructions, it can be
5389// eliminated. If the cleanup pad continues to the caller, any predecessor
5390// that is an EH pad will be updated to continue to the caller and any
5391// predecessor that terminates with an invoke instruction will have its invoke
5392// instruction converted to a call instruction. If the cleanup pad being
5393// simplified does not continue to the caller, each predecessor will be
5394// updated to continue to the unwind destination of the cleanup pad being
5395// simplified.
5396BasicBlock *BB = RI->getParent();
5397CleanupPadInst *CPInst = RI->getCleanupPad();
5398if (CPInst->getParent() != BB)
5399// This isn't an empty cleanup.
5400returnfalse;
5401
5402// We cannot kill the pad if it has multiple uses. This typically arises
5403// from unreachable basic blocks.
5404if (!CPInst->hasOneUse())
5405returnfalse;
5406
5407// Check that there are no other instructions except for benign intrinsics.
5408if (!isCleanupBlockEmpty(
5409 make_range<Instruction *>(CPInst->getNextNode(), RI)))
5410returnfalse;
5411
5412// If the cleanup return we are simplifying unwinds to the caller, this will
5413// set UnwindDest to nullptr.
5414BasicBlock *UnwindDest = RI->getUnwindDest();
5415
5416// We're about to remove BB from the control flow. Before we do, sink any
5417// PHINodes into the unwind destination. Doing this before changing the
5418// control flow avoids some potentially slow checks, since we can currently
5419// be certain that UnwindDest and BB have no common predecessors (since they
5420// are both EH pads).
5421if (UnwindDest) {
5422// First, go through the PHI nodes in UnwindDest and update any nodes that
5423// reference the block we are removing
5424for (PHINode &DestPN : UnwindDest->phis()) {
5425intIdx = DestPN.getBasicBlockIndex(BB);
5426// Since BB unwinds to UnwindDest, it has to be in the PHI node.
5427assert(Idx != -1);
5428// This PHI node has an incoming value that corresponds to a control
5429// path through the cleanup pad we are removing. If the incoming
5430// value is in the cleanup pad, it must be a PHINode (because we
5431// verified above that the block is otherwise empty). Otherwise, the
5432// value is either a constant or a value that dominates the cleanup
5433// pad being removed.
5434//
5435// Because BB and UnwindDest are both EH pads, all of their
5436// predecessors must unwind to these blocks, and since no instruction
5437// can have multiple unwind destinations, there will be no overlap in
5438// incoming blocks between SrcPN and DestPN.
5439Value *SrcVal = DestPN.getIncomingValue(Idx);
5440PHINode *SrcPN = dyn_cast<PHINode>(SrcVal);
5441
5442bool NeedPHITranslation = SrcPN && SrcPN->getParent() == BB;
5443for (auto *Pred :predecessors(BB)) {
5444Value *Incoming =
5445 NeedPHITranslation ? SrcPN->getIncomingValueForBlock(Pred) : SrcVal;
5446 DestPN.addIncoming(Incoming, Pred);
5447 }
5448 }
5449
5450// Sink any remaining PHI nodes directly into UnwindDest.
5451BasicBlock::iterator InsertPt = UnwindDest->getFirstNonPHIIt();
5452for (PHINode &PN :make_early_inc_range(BB->phis())) {
5453if (PN.use_empty() || !PN.isUsedOutsideOfBlock(BB))
5454// If the PHI node has no uses or all of its uses are in this basic
5455// block (meaning they are debug or lifetime intrinsics), just leave
5456// it. It will be erased when we erase BB below.
5457continue;
5458
5459// Otherwise, sink this PHI node into UnwindDest.
5460// Any predecessors to UnwindDest which are not already represented
5461// must be back edges which inherit the value from the path through
5462// BB. In this case, the PHI value must reference itself.
5463for (auto *pred :predecessors(UnwindDest))
5464if (pred != BB)
5465 PN.addIncoming(&PN,pred);
5466 PN.moveBefore(InsertPt);
5467// Also, add a dummy incoming value for the original BB itself,
5468// so that the PHI is well-formed until we drop said predecessor.
5469 PN.addIncoming(PoisonValue::get(PN.getType()), BB);
5470 }
5471 }
5472
5473 std::vector<DominatorTree::UpdateType> Updates;
5474
5475// We use make_early_inc_range here because we will remove all predecessors.
5476for (BasicBlock *PredBB :llvm::make_early_inc_range(predecessors(BB))) {
5477if (UnwindDest ==nullptr) {
5478if (DTU) {
5479 DTU->applyUpdates(Updates);
5480 Updates.clear();
5481 }
5482removeUnwindEdge(PredBB, DTU);
5483 ++NumInvokes;
5484 }else {
5485 BB->removePredecessor(PredBB);
5486Instruction *TI = PredBB->getTerminator();
5487 TI->replaceUsesOfWith(BB, UnwindDest);
5488if (DTU) {
5489 Updates.push_back({DominatorTree::Insert, PredBB, UnwindDest});
5490 Updates.push_back({DominatorTree::Delete, PredBB, BB});
5491 }
5492 }
5493 }
5494
5495if (DTU)
5496 DTU->applyUpdates(Updates);
5497
5498DeleteDeadBlock(BB, DTU);
5499
5500returntrue;
5501}
5502
5503// Try to merge two cleanuppads together.
5504staticboolmergeCleanupPad(CleanupReturnInst *RI) {
5505// Skip any cleanuprets which unwind to caller, there is nothing to merge
5506// with.
5507BasicBlock *UnwindDest = RI->getUnwindDest();
5508if (!UnwindDest)
5509returnfalse;
5510
5511// This cleanupret isn't the only predecessor of this cleanuppad, it wouldn't
5512// be safe to merge without code duplication.
5513if (UnwindDest->getSinglePredecessor() != RI->getParent())
5514returnfalse;
5515
5516// Verify that our cleanuppad's unwind destination is another cleanuppad.
5517auto *SuccessorCleanupPad = dyn_cast<CleanupPadInst>(&UnwindDest->front());
5518if (!SuccessorCleanupPad)
5519returnfalse;
5520
5521CleanupPadInst *PredecessorCleanupPad = RI->getCleanupPad();
5522// Replace any uses of the successor cleanupad with the predecessor pad
5523// The only cleanuppad uses should be this cleanupret, it's cleanupret and
5524// funclet bundle operands.
5525 SuccessorCleanupPad->replaceAllUsesWith(PredecessorCleanupPad);
5526// Remove the old cleanuppad.
5527 SuccessorCleanupPad->eraseFromParent();
5528// Now, we simply replace the cleanupret with a branch to the unwind
5529// destination.
5530BranchInst::Create(UnwindDest, RI->getParent());
5531 RI->eraseFromParent();
5532
5533returntrue;
5534}
5535
5536bool SimplifyCFGOpt::simplifyCleanupReturn(CleanupReturnInst *RI) {
5537// It is possible to transiantly have an undef cleanuppad operand because we
5538// have deleted some, but not all, dead blocks.
5539// Eventually, this block will be deleted.
5540if (isa<UndefValue>(RI->getOperand(0)))
5541returnfalse;
5542
5543if (mergeCleanupPad(RI))
5544returntrue;
5545
5546if (removeEmptyCleanup(RI, DTU))
5547returntrue;
5548
5549returnfalse;
5550}
5551
5552// WARNING: keep in sync with InstCombinerImpl::visitUnreachableInst()!
5553bool SimplifyCFGOpt::simplifyUnreachable(UnreachableInst *UI) {
5554BasicBlock *BB = UI->getParent();
5555
5556bool Changed =false;
5557
5558// Ensure that any debug-info records that used to occur after the Unreachable
5559// are moved to in front of it -- otherwise they'll "dangle" at the end of
5560// the block.
5561 BB->flushTerminatorDbgRecords();
5562
5563// Debug-info records on the unreachable inst itself should be deleted, as
5564// below we delete everything past the final executable instruction.
5565 UI->dropDbgRecords();
5566
5567// If there are any instructions immediately before the unreachable that can
5568// be removed, do so.
5569while (UI->getIterator() != BB->begin()) {
5570BasicBlock::iterator BBI = UI->getIterator();
5571 --BBI;
5572
5573if (!isGuaranteedToTransferExecutionToSuccessor(&*BBI))
5574break;// Can not drop any more instructions. We're done here.
5575// Otherwise, this instruction can be freely erased,
5576// even if it is not side-effect free.
5577
5578// Note that deleting EH's here is in fact okay, although it involves a bit
5579// of subtle reasoning. If this inst is an EH, all the predecessors of this
5580// block will be the unwind edges of Invoke/CatchSwitch/CleanupReturn,
5581// and we can therefore guarantee this block will be erased.
5582
5583// If we're deleting this, we're deleting any subsequent debug info, so
5584// delete DbgRecords.
5585 BBI->dropDbgRecords();
5586
5587// Delete this instruction (any uses are guaranteed to be dead)
5588 BBI->replaceAllUsesWith(PoisonValue::get(BBI->getType()));
5589 BBI->eraseFromParent();
5590 Changed =true;
5591 }
5592
5593// If the unreachable instruction is the first in the block, take a gander
5594// at all of the predecessors of this instruction, and simplify them.
5595if (&BB->front() != UI)
5596return Changed;
5597
5598 std::vector<DominatorTree::UpdateType> Updates;
5599
5600SmallSetVector<BasicBlock *, 8> Preds(pred_begin(BB),pred_end(BB));
5601for (BasicBlock *Predecessor : Preds) {
5602Instruction *TI = Predecessor->getTerminator();
5603IRBuilder<> Builder(TI);
5604if (auto *BI = dyn_cast<BranchInst>(TI)) {
5605// We could either have a proper unconditional branch,
5606// or a degenerate conditional branch with matching destinations.
5607if (all_of(BI->successors(),
5608 [BB](auto *Successor) { return Successor == BB; })) {
5609newUnreachableInst(TI->getContext(), TI->getIterator());
5610 TI->eraseFromParent();
5611 Changed =true;
5612 }else {
5613assert(BI->isConditional() &&"Can't get here with an uncond branch.");
5614Value*Cond = BI->getCondition();
5615assert(BI->getSuccessor(0) != BI->getSuccessor(1) &&
5616"The destinations are guaranteed to be different here.");
5617CallInst *Assumption;
5618if (BI->getSuccessor(0) == BB) {
5619 Assumption = Builder.CreateAssumption(Builder.CreateNot(Cond));
5620 Builder.CreateBr(BI->getSuccessor(1));
5621 }else {
5622assert(BI->getSuccessor(1) == BB &&"Incorrect CFG");
5623 Assumption = Builder.CreateAssumption(Cond);
5624 Builder.CreateBr(BI->getSuccessor(0));
5625 }
5626if (Options.AC)
5627Options.AC->registerAssumption(cast<AssumeInst>(Assumption));
5628
5629eraseTerminatorAndDCECond(BI);
5630 Changed =true;
5631 }
5632if (DTU)
5633 Updates.push_back({DominatorTree::Delete, Predecessor, BB});
5634 }elseif (auto *SI = dyn_cast<SwitchInst>(TI)) {
5635SwitchInstProfUpdateWrapper SU(*SI);
5636for (auto i = SU->case_begin(), e = SU->case_end(); i != e;) {
5637if (i->getCaseSuccessor() != BB) {
5638 ++i;
5639continue;
5640 }
5641 BB->removePredecessor(SU->getParent());
5642 i = SU.removeCase(i);
5643e = SU->case_end();
5644 Changed =true;
5645 }
5646// Note that the default destination can't be removed!
5647if (DTU &&SI->getDefaultDest() != BB)
5648 Updates.push_back({DominatorTree::Delete, Predecessor, BB});
5649 }elseif (auto *II = dyn_cast<InvokeInst>(TI)) {
5650if (II->getUnwindDest() == BB) {
5651if (DTU) {
5652 DTU->applyUpdates(Updates);
5653 Updates.clear();
5654 }
5655auto *CI = cast<CallInst>(removeUnwindEdge(TI->getParent(), DTU));
5656if (!CI->doesNotThrow())
5657 CI->setDoesNotThrow();
5658 Changed =true;
5659 }
5660 }elseif (auto *CSI = dyn_cast<CatchSwitchInst>(TI)) {
5661if (CSI->getUnwindDest() == BB) {
5662if (DTU) {
5663 DTU->applyUpdates(Updates);
5664 Updates.clear();
5665 }
5666removeUnwindEdge(TI->getParent(), DTU);
5667 Changed =true;
5668continue;
5669 }
5670
5671for (CatchSwitchInst::handler_iteratorI = CSI->handler_begin(),
5672 E = CSI->handler_end();
5673I != E; ++I) {
5674if (*I == BB) {
5675 CSI->removeHandler(I);
5676 --I;
5677 --E;
5678 Changed =true;
5679 }
5680 }
5681if (DTU)
5682 Updates.push_back({DominatorTree::Delete, Predecessor, BB});
5683if (CSI->getNumHandlers() == 0) {
5684if (CSI->hasUnwindDest()) {
5685// Redirect all predecessors of the block containing CatchSwitchInst
5686// to instead branch to the CatchSwitchInst's unwind destination.
5687if (DTU) {
5688for (auto *PredecessorOfPredecessor :predecessors(Predecessor)) {
5689 Updates.push_back({DominatorTree::Insert,
5690 PredecessorOfPredecessor,
5691 CSI->getUnwindDest()});
5692 Updates.push_back({DominatorTree::Delete,
5693 PredecessorOfPredecessor, Predecessor});
5694 }
5695 }
5696 Predecessor->replaceAllUsesWith(CSI->getUnwindDest());
5697 }else {
5698// Rewrite all preds to unwind to caller (or from invoke to call).
5699if (DTU) {
5700 DTU->applyUpdates(Updates);
5701 Updates.clear();
5702 }
5703SmallVector<BasicBlock *, 8> EHPreds(predecessors(Predecessor));
5704for (BasicBlock *EHPred : EHPreds)
5705removeUnwindEdge(EHPred, DTU);
5706 }
5707// The catchswitch is no longer reachable.
5708newUnreachableInst(CSI->getContext(), CSI->getIterator());
5709 CSI->eraseFromParent();
5710 Changed =true;
5711 }
5712 }elseif (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) {
5713 (void)CRI;
5714assert(CRI->hasUnwindDest() && CRI->getUnwindDest() == BB &&
5715"Expected to always have an unwind to BB.");
5716if (DTU)
5717 Updates.push_back({DominatorTree::Delete, Predecessor, BB});
5718newUnreachableInst(TI->getContext(), TI->getIterator());
5719 TI->eraseFromParent();
5720 Changed =true;
5721 }
5722 }
5723
5724if (DTU)
5725 DTU->applyUpdates(Updates);
5726
5727// If this block is now dead, remove it.
5728if (pred_empty(BB) && BB != &BB->getParent()->getEntryBlock()) {
5729DeleteDeadBlock(BB, DTU);
5730returntrue;
5731 }
5732
5733return Changed;
5734}
5735
5736staticboolcasesAreContiguous(SmallVectorImpl<ConstantInt *> &Cases) {
5737assert(Cases.size() >= 1);
5738
5739array_pod_sort(Cases.begin(), Cases.end(),constantIntSortPredicate);
5740for (size_tI = 1, E = Cases.size();I != E; ++I) {
5741if (Cases[I - 1]->getValue() != Cases[I]->getValue() + 1)
5742returnfalse;
5743 }
5744returntrue;
5745}
5746
5747staticvoidcreateUnreachableSwitchDefault(SwitchInst *Switch,
5748DomTreeUpdater *DTU,
5749bool RemoveOrigDefaultBlock =true) {
5750LLVM_DEBUG(dbgs() <<"SimplifyCFG: switch default is dead.\n");
5751auto *BB = Switch->getParent();
5752auto *OrigDefaultBlock = Switch->getDefaultDest();
5753if (RemoveOrigDefaultBlock)
5754 OrigDefaultBlock->removePredecessor(BB);
5755BasicBlock *NewDefaultBlock =BasicBlock::Create(
5756 BB->getContext(), BB->getName() +".unreachabledefault", BB->getParent(),
5757 OrigDefaultBlock);
5758newUnreachableInst(Switch->getContext(), NewDefaultBlock);
5759 Switch->setDefaultDest(&*NewDefaultBlock);
5760if (DTU) {
5761SmallVector<DominatorTree::UpdateType, 2> Updates;
5762 Updates.push_back({DominatorTree::Insert, BB, &*NewDefaultBlock});
5763if (RemoveOrigDefaultBlock &&
5764 !is_contained(successors(BB), OrigDefaultBlock))
5765 Updates.push_back({DominatorTree::Delete, BB, &*OrigDefaultBlock});
5766 DTU->applyUpdates(Updates);
5767 }
5768}
5769
5770/// Turn a switch into an integer range comparison and branch.
5771/// Switches with more than 2 destinations are ignored.
5772/// Switches with 1 destination are also ignored.
5773bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
5774IRBuilder<> &Builder) {
5775assert(SI->getNumCases() > 1 &&"Degenerate switch?");
5776
5777bool HasDefault =
5778 !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
5779
5780auto *BB =SI->getParent();
5781
5782// Partition the cases into two sets with different destinations.
5783BasicBlock *DestA = HasDefault ?SI->getDefaultDest() :nullptr;
5784BasicBlock *DestB =nullptr;
5785SmallVector<ConstantInt *, 16> CasesA;
5786SmallVector<ConstantInt *, 16> CasesB;
5787
5788for (auto Case :SI->cases()) {
5789BasicBlock *Dest = Case.getCaseSuccessor();
5790if (!DestA)
5791 DestA = Dest;
5792if (Dest == DestA) {
5793 CasesA.push_back(Case.getCaseValue());
5794continue;
5795 }
5796if (!DestB)
5797 DestB = Dest;
5798if (Dest == DestB) {
5799 CasesB.push_back(Case.getCaseValue());
5800continue;
5801 }
5802returnfalse;// More than two destinations.
5803 }
5804if (!DestB)
5805returnfalse;// All destinations are the same and the default is unreachable
5806
5807assert(DestA && DestB &&
5808"Single-destination switch should have been folded.");
5809assert(DestA != DestB);
5810assert(DestB !=SI->getDefaultDest());
5811assert(!CasesB.empty() &&"There must be non-default cases.");
5812assert(!CasesA.empty() || HasDefault);
5813
5814// Figure out if one of the sets of cases form a contiguous range.
5815SmallVectorImpl<ConstantInt *> *ContiguousCases =nullptr;
5816BasicBlock *ContiguousDest =nullptr;
5817BasicBlock *OtherDest =nullptr;
5818if (!CasesA.empty() &&casesAreContiguous(CasesA)) {
5819 ContiguousCases = &CasesA;
5820 ContiguousDest = DestA;
5821 OtherDest = DestB;
5822 }elseif (casesAreContiguous(CasesB)) {
5823 ContiguousCases = &CasesB;
5824 ContiguousDest = DestB;
5825 OtherDest = DestA;
5826 }else
5827returnfalse;
5828
5829// Start building the compare and branch.
5830
5831Constant *Offset =ConstantExpr::getNeg(ContiguousCases->back());
5832Constant *NumCases =
5833 ConstantInt::get(Offset->getType(), ContiguousCases->size());
5834
5835Value *Sub =SI->getCondition();
5836if (!Offset->isNullValue())
5837 Sub = Builder.CreateAdd(Sub,Offset, Sub->getName() +".off");
5838
5839Value *Cmp;
5840// If NumCases overflowed, then all possible values jump to the successor.
5841if (NumCases->isNullValue() && !ContiguousCases->empty())
5842Cmp =ConstantInt::getTrue(SI->getContext());
5843else
5844Cmp = Builder.CreateICmpULT(Sub, NumCases,"switch");
5845BranchInst *NewBI = Builder.CreateCondBr(Cmp, ContiguousDest, OtherDest);
5846
5847// Update weight for the newly-created conditional branch.
5848if (hasBranchWeightMD(*SI)) {
5849SmallVector<uint64_t, 8> Weights;
5850getBranchWeights(SI, Weights);
5851if (Weights.size() == 1 +SI->getNumCases()) {
5852uint64_t TrueWeight = 0;
5853uint64_t FalseWeight = 0;
5854for (size_tI = 0, E = Weights.size();I != E; ++I) {
5855if (SI->getSuccessor(I) == ContiguousDest)
5856 TrueWeight += Weights[I];
5857else
5858 FalseWeight += Weights[I];
5859 }
5860while (TrueWeight > UINT32_MAX || FalseWeight > UINT32_MAX) {
5861 TrueWeight /= 2;
5862 FalseWeight /= 2;
5863 }
5864setBranchWeights(NewBI, TrueWeight, FalseWeight,/*IsExpected=*/false);
5865 }
5866 }
5867
5868// Prune obsolete incoming values off the successors' PHI nodes.
5869for (auto BBI = ContiguousDest->begin(); isa<PHINode>(BBI); ++BBI) {
5870unsigned PreviousEdges = ContiguousCases->size();
5871if (ContiguousDest ==SI->getDefaultDest())
5872 ++PreviousEdges;
5873for (unsignedI = 0, E = PreviousEdges - 1;I != E; ++I)
5874 cast<PHINode>(BBI)->removeIncomingValue(SI->getParent());
5875 }
5876for (auto BBI = OtherDest->begin(); isa<PHINode>(BBI); ++BBI) {
5877unsigned PreviousEdges =SI->getNumCases() - ContiguousCases->size();
5878if (OtherDest ==SI->getDefaultDest())
5879 ++PreviousEdges;
5880for (unsignedI = 0, E = PreviousEdges - 1;I != E; ++I)
5881 cast<PHINode>(BBI)->removeIncomingValue(SI->getParent());
5882 }
5883
5884// Clean up the default block - it may have phis or other instructions before
5885// the unreachable terminator.
5886if (!HasDefault)
5887createUnreachableSwitchDefault(SI, DTU);
5888
5889auto *UnreachableDefault =SI->getDefaultDest();
5890
5891// Drop the switch.
5892SI->eraseFromParent();
5893
5894if (!HasDefault && DTU)
5895 DTU->applyUpdates({{DominatorTree::Delete, BB, UnreachableDefault}});
5896
5897returntrue;
5898}
5899
5900/// Compute masked bits for the condition of a switch
5901/// and use it to remove dead cases.
5902staticbooleliminateDeadSwitchCases(SwitchInst *SI,DomTreeUpdater *DTU,
5903AssumptionCache *AC,
5904constDataLayout &DL) {
5905Value *Cond = SI->getCondition();
5906KnownBits Known =computeKnownBits(Cond,DL, 0, AC, SI);
5907
5908// We can also eliminate cases by determining that their values are outside of
5909// the limited range of the condition based on how many significant (non-sign)
5910// bits are in the condition value.
5911unsigned MaxSignificantBitsInCond =
5912ComputeMaxSignificantBits(Cond,DL, 0, AC, SI);
5913
5914// Gather dead cases.
5915SmallVector<ConstantInt *, 8> DeadCases;
5916SmallDenseMap<BasicBlock *, int, 8> NumPerSuccessorCases;
5917SmallVector<BasicBlock *, 8> UniqueSuccessors;
5918for (constauto &Case : SI->cases()) {
5919auto *Successor = Case.getCaseSuccessor();
5920if (DTU) {
5921if (!NumPerSuccessorCases.count(Successor))
5922 UniqueSuccessors.push_back(Successor);
5923 ++NumPerSuccessorCases[Successor];
5924 }
5925constAPInt &CaseVal = Case.getCaseValue()->getValue();
5926if (Known.Zero.intersects(CaseVal) || !Known.One.isSubsetOf(CaseVal) ||
5927 (CaseVal.getSignificantBits() > MaxSignificantBitsInCond)) {
5928 DeadCases.push_back(Case.getCaseValue());
5929if (DTU)
5930 --NumPerSuccessorCases[Successor];
5931LLVM_DEBUG(dbgs() <<"SimplifyCFG: switch case " << CaseVal
5932 <<" is dead.\n");
5933 }
5934 }
5935
5936// If we can prove that the cases must cover all possible values, the
5937// default destination becomes dead and we can remove it. If we know some
5938// of the bits in the value, we can use that to more precisely compute the
5939// number of possible unique case values.
5940bool HasDefault =
5941 !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
5942constunsigned NumUnknownBits =
5943 Known.getBitWidth() - (Known.Zero | Known.One).popcount();
5944assert(NumUnknownBits <= Known.getBitWidth());
5945if (HasDefault && DeadCases.empty() &&
5946 NumUnknownBits < 64/* avoid overflow */) {
5947uint64_t AllNumCases = 1ULL << NumUnknownBits;
5948if (SI->getNumCases() == AllNumCases) {
5949createUnreachableSwitchDefault(SI, DTU);
5950returntrue;
5951 }
5952// When only one case value is missing, replace default with that case.
5953// Eliminating the default branch will provide more opportunities for
5954// optimization, such as lookup tables.
5955if (SI->getNumCases() == AllNumCases - 1) {
5956assert(NumUnknownBits > 1 &&"Should be canonicalized to a branch");
5957IntegerType *CondTy = cast<IntegerType>(Cond->getType());
5958if (CondTy->getIntegerBitWidth() > 64 ||
5959 !DL.fitsInLegalInteger(CondTy->getIntegerBitWidth()))
5960returnfalse;
5961
5962uint64_t MissingCaseVal = 0;
5963for (constauto &Case : SI->cases())
5964 MissingCaseVal ^= Case.getCaseValue()->getValue().getLimitedValue();
5965auto *MissingCase =
5966 cast<ConstantInt>(ConstantInt::get(Cond->getType(), MissingCaseVal));
5967SwitchInstProfUpdateWrapper SIW(*SI);
5968 SIW.addCase(MissingCase, SI->getDefaultDest(), SIW.getSuccessorWeight(0));
5969createUnreachableSwitchDefault(SI, DTU,/*RemoveOrigDefaultBlock*/false);
5970 SIW.setSuccessorWeight(0, 0);
5971returntrue;
5972 }
5973 }
5974
5975if (DeadCases.empty())
5976returnfalse;
5977
5978SwitchInstProfUpdateWrapper SIW(*SI);
5979for (ConstantInt *DeadCase : DeadCases) {
5980SwitchInst::CaseIt CaseI = SI->findCaseValue(DeadCase);
5981assert(CaseI != SI->case_default() &&
5982"Case was not found. Probably mistake in DeadCases forming.");
5983// Prune unused values from PHI nodes.
5984 CaseI->getCaseSuccessor()->removePredecessor(SI->getParent());
5985 SIW.removeCase(CaseI);
5986 }
5987
5988if (DTU) {
5989 std::vector<DominatorTree::UpdateType> Updates;
5990for (auto *Successor : UniqueSuccessors)
5991if (NumPerSuccessorCases[Successor] == 0)
5992 Updates.push_back({DominatorTree::Delete, SI->getParent(),Successor});
5993 DTU->applyUpdates(Updates);
5994 }
5995
5996returntrue;
5997}
5998
5999/// If BB would be eligible for simplification by
6000/// TryToSimplifyUncondBranchFromEmptyBlock (i.e. it is empty and terminated
6001/// by an unconditional branch), look at the phi node for BB in the successor
6002/// block and see if the incoming value is equal to CaseValue. If so, return
6003/// the phi node, and set PhiIndex to BB's index in the phi node.
6004staticPHINode *findPHIForConditionForwarding(ConstantInt *CaseValue,
6005BasicBlock *BB,int *PhiIndex) {
6006if (&*BB->getFirstNonPHIIt() != BB->getTerminator())
6007returnnullptr;// BB must be empty to be a candidate for simplification.
6008if (!BB->getSinglePredecessor())
6009returnnullptr;// BB must be dominated by the switch.
6010
6011BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator());
6012if (!Branch || !Branch->isUnconditional())
6013returnnullptr;// Terminator must be unconditional branch.
6014
6015BasicBlock *Succ = Branch->getSuccessor(0);
6016
6017for (PHINode &PHI : Succ->phis()) {
6018intIdx =PHI.getBasicBlockIndex(BB);
6019assert(Idx >= 0 &&"PHI has no entry for predecessor?");
6020
6021Value *InValue =PHI.getIncomingValue(Idx);
6022if (InValue != CaseValue)
6023continue;
6024
6025 *PhiIndex =Idx;
6026return &PHI;
6027 }
6028
6029returnnullptr;
6030}
6031
6032/// Try to forward the condition of a switch instruction to a phi node
6033/// dominated by the switch, if that would mean that some of the destination
6034/// blocks of the switch can be folded away. Return true if a change is made.
6035staticboolforwardSwitchConditionToPHI(SwitchInst *SI) {
6036usingForwardingNodesMap =DenseMap<PHINode *, SmallVector<int, 4>>;
6037
6038 ForwardingNodesMap ForwardingNodes;
6039BasicBlock *SwitchBlock = SI->getParent();
6040bool Changed =false;
6041for (constauto &Case : SI->cases()) {
6042ConstantInt *CaseValue = Case.getCaseValue();
6043BasicBlock *CaseDest = Case.getCaseSuccessor();
6044
6045// Replace phi operands in successor blocks that are using the constant case
6046// value rather than the switch condition variable:
6047// switchbb:
6048// switch i32 %x, label %default [
6049// i32 17, label %succ
6050// ...
6051// succ:
6052// %r = phi i32 ... [ 17, %switchbb ] ...
6053// -->
6054// %r = phi i32 ... [ %x, %switchbb ] ...
6055
6056for (PHINode &Phi : CaseDest->phis()) {
6057// This only works if there is exactly 1 incoming edge from the switch to
6058// a phi. If there is >1, that means multiple cases of the switch map to 1
6059// value in the phi, and that phi value is not the switch condition. Thus,
6060// this transform would not make sense (the phi would be invalid because
6061// a phi can't have different incoming values from the same block).
6062int SwitchBBIdx = Phi.getBasicBlockIndex(SwitchBlock);
6063if (Phi.getIncomingValue(SwitchBBIdx) == CaseValue &&
6064count(Phi.blocks(), SwitchBlock) == 1) {
6065 Phi.setIncomingValue(SwitchBBIdx, SI->getCondition());
6066 Changed =true;
6067 }
6068 }
6069
6070// Collect phi nodes that are indirectly using this switch's case constants.
6071int PhiIdx;
6072if (auto *Phi =findPHIForConditionForwarding(CaseValue, CaseDest, &PhiIdx))
6073 ForwardingNodes[Phi].push_back(PhiIdx);
6074 }
6075
6076for (auto &ForwardingNode : ForwardingNodes) {
6077PHINode *Phi = ForwardingNode.first;
6078SmallVectorImpl<int> &Indexes = ForwardingNode.second;
6079// Check if it helps to fold PHI.
6080if (Indexes.size() < 2 && !llvm::is_contained(Phi->incoming_values(), SI->getCondition()))
6081continue;
6082
6083for (int Index : Indexes)
6084 Phi->setIncomingValue(Index, SI->getCondition());
6085 Changed =true;
6086 }
6087
6088return Changed;
6089}
6090
6091/// Return true if the backend will be able to handle
6092/// initializing an array of constants like C.
6093staticboolvalidLookupTableConstant(Constant *C,constTargetTransformInfo &TTI) {
6094if (C->isThreadDependent())
6095returnfalse;
6096if (C->isDLLImportDependent())
6097returnfalse;
6098
6099if (!isa<ConstantFP>(C) && !isa<ConstantInt>(C) &&
6100 !isa<ConstantPointerNull>(C) && !isa<GlobalValue>(C) &&
6101 !isa<UndefValue>(C) && !isa<ConstantExpr>(C))
6102returnfalse;
6103
6104if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
6105// Pointer casts and in-bounds GEPs will not prohibit the backend from
6106// materializing the array of constants.
6107Constant *StrippedC = cast<Constant>(CE->stripInBoundsConstantOffsets());
6108if (StrippedC ==C || !validLookupTableConstant(StrippedC,TTI))
6109returnfalse;
6110 }
6111
6112if (!TTI.shouldBuildLookupTablesForConstant(C))
6113returnfalse;
6114
6115returntrue;
6116}
6117
6118/// If V is a Constant, return it. Otherwise, try to look up
6119/// its constant value in ConstantPool, returning 0 if it's not there.
6120staticConstant *
6121lookupConstant(Value *V,
6122constSmallDenseMap<Value *, Constant *> &ConstantPool) {
6123if (Constant *C = dyn_cast<Constant>(V))
6124returnC;
6125returnConstantPool.lookup(V);
6126}
6127
6128/// Try to fold instruction I into a constant. This works for
6129/// simple instructions such as binary operations where both operands are
6130/// constant or can be replaced by constants from the ConstantPool. Returns the
6131/// resulting constant on success, 0 otherwise.
6132staticConstant *
6133constantFold(Instruction *I,constDataLayout &DL,
6134constSmallDenseMap<Value *, Constant *> &ConstantPool) {
6135if (SelectInst *Select = dyn_cast<SelectInst>(I)) {
6136Constant *A =lookupConstant(Select->getCondition(),ConstantPool);
6137if (!A)
6138returnnullptr;
6139if (A->isAllOnesValue())
6140returnlookupConstant(Select->getTrueValue(),ConstantPool);
6141if (A->isNullValue())
6142returnlookupConstant(Select->getFalseValue(),ConstantPool);
6143returnnullptr;
6144 }
6145
6146SmallVector<Constant *, 4> COps;
6147for (unsignedN = 0, E =I->getNumOperands();N != E; ++N) {
6148if (Constant *A =lookupConstant(I->getOperand(N),ConstantPool))
6149 COps.push_back(A);
6150else
6151returnnullptr;
6152 }
6153
6154returnConstantFoldInstOperands(I, COps,DL);
6155}
6156
6157/// Try to determine the resulting constant values in phi nodes
6158/// at the common destination basic block, *CommonDest, for one of the case
6159/// destionations CaseDest corresponding to value CaseVal (0 for the default
6160/// case), of a switch instruction SI.
6161staticbool
6162getCaseResults(SwitchInst *SI,ConstantInt *CaseVal,BasicBlock *CaseDest,
6163BasicBlock **CommonDest,
6164SmallVectorImpl<std::pair<PHINode *, Constant *>> &Res,
6165constDataLayout &DL,constTargetTransformInfo &TTI) {
6166// The block from which we enter the common destination.
6167BasicBlock *Pred = SI->getParent();
6168
6169// If CaseDest is empty except for some side-effect free instructions through
6170// which we can constant-propagate the CaseVal, continue to its successor.
6171SmallDenseMap<Value *, Constant *>ConstantPool;
6172ConstantPool.insert(std::make_pair(SI->getCondition(), CaseVal));
6173for (Instruction &I : CaseDest->instructionsWithoutDebug(false)) {
6174if (I.isTerminator()) {
6175// If the terminator is a simple branch, continue to the next block.
6176if (I.getNumSuccessors() != 1 ||I.isSpecialTerminator())
6177returnfalse;
6178 Pred = CaseDest;
6179 CaseDest =I.getSuccessor(0);
6180 }elseif (Constant *C =constantFold(&I,DL,ConstantPool)) {
6181// Instruction is side-effect free and constant.
6182
6183// If the instruction has uses outside this block or a phi node slot for
6184// the block, it is not safe to bypass the instruction since it would then
6185// no longer dominate all its uses.
6186for (auto &Use :I.uses()) {
6187User *User =Use.getUser();
6188if (Instruction *I = dyn_cast<Instruction>(User))
6189if (I->getParent() == CaseDest)
6190continue;
6191if (PHINode *Phi = dyn_cast<PHINode>(User))
6192if (Phi->getIncomingBlock(Use) == CaseDest)
6193continue;
6194returnfalse;
6195 }
6196
6197ConstantPool.insert(std::make_pair(&I,C));
6198 }else {
6199break;
6200 }
6201 }
6202
6203// If we did not have a CommonDest before, use the current one.
6204if (!*CommonDest)
6205 *CommonDest = CaseDest;
6206// If the destination isn't the common one, abort.
6207if (CaseDest != *CommonDest)
6208returnfalse;
6209
6210// Get the values for this case from phi nodes in the destination block.
6211for (PHINode &PHI : (*CommonDest)->phis()) {
6212intIdx =PHI.getBasicBlockIndex(Pred);
6213if (Idx == -1)
6214continue;
6215
6216Constant *ConstVal =
6217lookupConstant(PHI.getIncomingValue(Idx),ConstantPool);
6218if (!ConstVal)
6219returnfalse;
6220
6221// Be conservative about which kinds of constants we support.
6222if (!validLookupTableConstant(ConstVal,TTI))
6223returnfalse;
6224
6225 Res.push_back(std::make_pair(&PHI, ConstVal));
6226 }
6227
6228return Res.size() > 0;
6229}
6230
6231// Helper function used to add CaseVal to the list of cases that generate
6232// Result. Returns the updated number of cases that generate this result.
6233staticsize_tmapCaseToResult(ConstantInt *CaseVal,
6234 SwitchCaseResultVectorTy &UniqueResults,
6235Constant *Result) {
6236for (auto &I : UniqueResults) {
6237if (I.first == Result) {
6238I.second.push_back(CaseVal);
6239returnI.second.size();
6240 }
6241 }
6242 UniqueResults.push_back(
6243 std::make_pair(Result,SmallVector<ConstantInt *, 4>(1, CaseVal)));
6244return 1;
6245}
6246
6247// Helper function that initializes a map containing
6248// results for the PHI node of the common destination block for a switch
6249// instruction. Returns false if multiple PHI nodes have been found or if
6250// there is not a common destination block for the switch.
6251staticboolinitializeUniqueCases(SwitchInst *SI,PHINode *&PHI,
6252BasicBlock *&CommonDest,
6253 SwitchCaseResultVectorTy &UniqueResults,
6254Constant *&DefaultResult,
6255constDataLayout &DL,
6256constTargetTransformInfo &TTI,
6257 uintptr_t MaxUniqueResults) {
6258for (constauto &I : SI->cases()) {
6259ConstantInt *CaseVal =I.getCaseValue();
6260
6261// Resulting value at phi nodes for this case value.
6262 SwitchCaseResultsTyResults;
6263if (!getCaseResults(SI, CaseVal,I.getCaseSuccessor(), &CommonDest,Results,
6264DL,TTI))
6265returnfalse;
6266
6267// Only one value per case is permitted.
6268if (Results.size() > 1)
6269returnfalse;
6270
6271// Add the case->result mapping to UniqueResults.
6272constsize_t NumCasesForResult =
6273mapCaseToResult(CaseVal, UniqueResults,Results.begin()->second);
6274
6275// Early out if there are too many cases for this result.
6276if (NumCasesForResult >MaxSwitchCasesPerResult)
6277returnfalse;
6278
6279// Early out if there are too many unique results.
6280if (UniqueResults.size() > MaxUniqueResults)
6281returnfalse;
6282
6283// Check the PHI consistency.
6284if (!PHI)
6285PHI =Results[0].first;
6286elseif (PHI !=Results[0].first)
6287returnfalse;
6288 }
6289// Find the default result value.
6290SmallVector<std::pair<PHINode *, Constant *>, 1> DefaultResults;
6291BasicBlock *DefaultDest = SI->getDefaultDest();
6292getCaseResults(SI,nullptr, SI->getDefaultDest(), &CommonDest, DefaultResults,
6293DL,TTI);
6294// If the default value is not found abort unless the default destination
6295// is unreachable.
6296 DefaultResult =
6297 DefaultResults.size() == 1 ? DefaultResults.begin()->second :nullptr;
6298if ((!DefaultResult &&
6299 !isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg())))
6300returnfalse;
6301
6302returntrue;
6303}
6304
6305// Helper function that checks if it is possible to transform a switch with only
6306// two cases (or two cases + default) that produces a result into a select.
6307// TODO: Handle switches with more than 2 cases that map to the same result.
6308staticValue *foldSwitchToSelect(const SwitchCaseResultVectorTy &ResultVector,
6309Constant *DefaultResult,Value *Condition,
6310IRBuilder<> &Builder) {
6311// If we are selecting between only two cases transform into a simple
6312// select or a two-way select if default is possible.
6313// Example:
6314// switch (a) { %0 = icmp eq i32 %a, 10
6315// case 10: return 42; %1 = select i1 %0, i32 42, i32 4
6316// case 20: return 2; ----> %2 = icmp eq i32 %a, 20
6317// default: return 4; %3 = select i1 %2, i32 2, i32 %1
6318// }
6319if (ResultVector.size() == 2 && ResultVector[0].second.size() == 1 &&
6320 ResultVector[1].second.size() == 1) {
6321ConstantInt *FirstCase = ResultVector[0].second[0];
6322ConstantInt *SecondCase = ResultVector[1].second[0];
6323Value *SelectValue = ResultVector[1].first;
6324if (DefaultResult) {
6325Value *ValueCompare =
6326 Builder.CreateICmpEQ(Condition, SecondCase,"switch.selectcmp");
6327 SelectValue = Builder.CreateSelect(ValueCompare, ResultVector[1].first,
6328 DefaultResult,"switch.select");
6329 }
6330Value *ValueCompare =
6331 Builder.CreateICmpEQ(Condition, FirstCase,"switch.selectcmp");
6332return Builder.CreateSelect(ValueCompare, ResultVector[0].first,
6333 SelectValue,"switch.select");
6334 }
6335
6336// Handle the degenerate case where two cases have the same result value.
6337if (ResultVector.size() == 1 && DefaultResult) {
6338ArrayRef<ConstantInt *> CaseValues = ResultVector[0].second;
6339unsigned CaseCount = CaseValues.size();
6340// n bits group cases map to the same result:
6341// case 0,4 -> Cond & 0b1..1011 == 0 ? result : default
6342// case 0,2,4,6 -> Cond & 0b1..1001 == 0 ? result : default
6343// case 0,2,8,10 -> Cond & 0b1..0101 == 0 ? result : default
6344if (isPowerOf2_32(CaseCount)) {
6345ConstantInt *MinCaseVal = CaseValues[0];
6346// Find mininal value.
6347for (auto *Case : CaseValues)
6348if (Case->getValue().slt(MinCaseVal->getValue()))
6349 MinCaseVal = Case;
6350
6351// Mark the bits case number touched.
6352APInt BitMask =APInt::getZero(MinCaseVal->getBitWidth());
6353for (auto *Case : CaseValues)
6354 BitMask |= (Case->getValue() - MinCaseVal->getValue());
6355
6356// Check if cases with the same result can cover all number
6357// in touched bits.
6358if (BitMask.popcount() ==Log2_32(CaseCount)) {
6359if (!MinCaseVal->isNullValue())
6360 Condition = Builder.CreateSub(Condition, MinCaseVal);
6361Value *And = Builder.CreateAnd(Condition, ~BitMask,"switch.and");
6362Value *Cmp = Builder.CreateICmpEQ(
6363And,Constant::getNullValue(And->getType()),"switch.selectcmp");
6364return Builder.CreateSelect(Cmp, ResultVector[0].first, DefaultResult);
6365 }
6366 }
6367
6368// Handle the degenerate case where two cases have the same value.
6369if (CaseValues.size() == 2) {
6370Value *Cmp1 = Builder.CreateICmpEQ(Condition, CaseValues[0],
6371"switch.selectcmp.case1");
6372Value *Cmp2 = Builder.CreateICmpEQ(Condition, CaseValues[1],
6373"switch.selectcmp.case2");
6374Value *Cmp = Builder.CreateOr(Cmp1, Cmp2,"switch.selectcmp");
6375return Builder.CreateSelect(Cmp, ResultVector[0].first, DefaultResult);
6376 }
6377 }
6378
6379returnnullptr;
6380}
6381
6382// Helper function to cleanup a switch instruction that has been converted into
6383// a select, fixing up PHI nodes and basic blocks.
6384staticvoidremoveSwitchAfterSelectFold(SwitchInst *SI,PHINode *PHI,
6385Value *SelectValue,
6386IRBuilder<> &Builder,
6387DomTreeUpdater *DTU) {
6388 std::vector<DominatorTree::UpdateType> Updates;
6389
6390BasicBlock *SelectBB = SI->getParent();
6391BasicBlock *DestBB =PHI->getParent();
6392
6393if (DTU && !is_contained(predecessors(DestBB), SelectBB))
6394 Updates.push_back({DominatorTree::Insert, SelectBB, DestBB});
6395 Builder.CreateBr(DestBB);
6396
6397// Remove the switch.
6398
6399PHI->removeIncomingValueIf(
6400 [&](unsignedIdx) {returnPHI->getIncomingBlock(Idx) == SelectBB; });
6401PHI->addIncoming(SelectValue, SelectBB);
6402
6403SmallPtrSet<BasicBlock *, 4> RemovedSuccessors;
6404for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) {
6405BasicBlock *Succ = SI->getSuccessor(i);
6406
6407if (Succ == DestBB)
6408continue;
6409 Succ->removePredecessor(SelectBB);
6410if (DTU && RemovedSuccessors.insert(Succ).second)
6411 Updates.push_back({DominatorTree::Delete, SelectBB, Succ});
6412 }
6413 SI->eraseFromParent();
6414if (DTU)
6415 DTU->applyUpdates(Updates);
6416}
6417
6418/// If a switch is only used to initialize one or more phi nodes in a common
6419/// successor block with only two different constant values, try to replace the
6420/// switch with a select. Returns true if the fold was made.
6421staticbooltrySwitchToSelect(SwitchInst *SI,IRBuilder<> &Builder,
6422DomTreeUpdater *DTU,constDataLayout &DL,
6423constTargetTransformInfo &TTI) {
6424Value *constCond = SI->getCondition();
6425PHINode *PHI =nullptr;
6426BasicBlock *CommonDest =nullptr;
6427Constant *DefaultResult;
6428 SwitchCaseResultVectorTy UniqueResults;
6429// Collect all the cases that will deliver the same value from the switch.
6430if (!initializeUniqueCases(SI,PHI, CommonDest, UniqueResults, DefaultResult,
6431DL,TTI,/*MaxUniqueResults*/ 2))
6432returnfalse;
6433
6434assert(PHI !=nullptr &&"PHI for value select not found");
6435 Builder.SetInsertPoint(SI);
6436Value *SelectValue =
6437foldSwitchToSelect(UniqueResults, DefaultResult,Cond, Builder);
6438if (!SelectValue)
6439returnfalse;
6440
6441removeSwitchAfterSelectFold(SI,PHI, SelectValue, Builder, DTU);
6442returntrue;
6443}
6444
6445namespace{
6446
6447/// This class represents a lookup table that can be used to replace a switch.
6448classSwitchLookupTable {
6449public:
6450 /// Create a lookup table to use as a switch replacement with the contents
6451 /// of Values, using DefaultValue to fill any holes in the table.
6452 SwitchLookupTable(
6453Module &M,uint64_t TableSize,ConstantInt *Offset,
6454constSmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values,
6455Constant *DefaultValue,constDataLayout &DL,constStringRef &FuncName);
6456
6457 /// Build instructions with Builder to retrieve the value at
6458 /// the position given by Index in the lookup table.
6459Value *buildLookup(Value *Index,IRBuilder<> &Builder);
6460
6461 /// Return true if a table with TableSize elements of
6462 /// type ElementType would fit in a target-legal register.
6463staticbool wouldFitInRegister(constDataLayout &DL,uint64_t TableSize,
6464Type *ElementType);
6465
6466private:
6467// Depending on the contents of the table, it can be represented in
6468// different ways.
6469enum {
6470// For tables where each element contains the same value, we just have to
6471// store that single value and return it for each lookup.
6472 SingleValueKind,
6473
6474// For tables where there is a linear relationship between table index
6475// and values. We calculate the result with a simple multiplication
6476// and addition instead of a table lookup.
6477 LinearMapKind,
6478
6479// For small tables with integer elements, we can pack them into a bitmap
6480// that fits into a target-legal register. Values are retrieved by
6481// shift and mask operations.
6482 BitMapKind,
6483
6484// The table is stored as an array of values. Values are retrieved by load
6485// instructions from the table.
6486 ArrayKind
6487 }Kind;
6488
6489// For SingleValueKind, this is the single value.
6490Constant *SingleValue =nullptr;
6491
6492// For BitMapKind, this is the bitmap.
6493ConstantInt *BitMap =nullptr;
6494IntegerType *BitMapElementTy =nullptr;
6495
6496// For LinearMapKind, these are the constants used to derive the value.
6497ConstantInt *LinearOffset =nullptr;
6498ConstantInt *LinearMultiplier =nullptr;
6499bool LinearMapValWrapped =false;
6500
6501// For ArrayKind, this is the array.
6502GlobalVariable *Array =nullptr;
6503};
6504
6505}// end anonymous namespace
6506
6507SwitchLookupTable::SwitchLookupTable(
6508Module &M,uint64_t TableSize,ConstantInt *Offset,
6509constSmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values,
6510Constant *DefaultValue,constDataLayout &DL,constStringRef &FuncName) {
6511assert(Values.size() &&"Can't build lookup table without values!");
6512assert(TableSize >= Values.size() &&"Can't fit values in table!");
6513
6514// If all values in the table are equal, this is that value.
6515 SingleValue = Values.begin()->second;
6516
6517Type *ValueType = Values.begin()->second->getType();
6518
6519// Build up the table contents.
6520SmallVector<Constant *, 64> TableContents(TableSize);
6521for (size_tI = 0, E = Values.size();I != E; ++I) {
6522ConstantInt *CaseVal = Values[I].first;
6523Constant *CaseRes = Values[I].second;
6524assert(CaseRes->getType() ==ValueType);
6525
6526uint64_tIdx = (CaseVal->getValue() -Offset->getValue()).getLimitedValue();
6527 TableContents[Idx] = CaseRes;
6528
6529if (SingleValue && !isa<PoisonValue>(CaseRes) && CaseRes != SingleValue)
6530 SingleValue = isa<PoisonValue>(SingleValue) ? CaseRes :nullptr;
6531 }
6532
6533// Fill in any holes in the table with the default result.
6534if (Values.size() < TableSize) {
6535assert(DefaultValue &&
6536"Need a default value to fill the lookup table holes.");
6537assert(DefaultValue->getType() ==ValueType);
6538for (uint64_tI = 0;I < TableSize; ++I) {
6539if (!TableContents[I])
6540 TableContents[I] = DefaultValue;
6541 }
6542
6543// If the default value is poison, all the holes are poison.
6544bool DefaultValueIsPoison = isa<PoisonValue>(DefaultValue);
6545
6546if (DefaultValue != SingleValue && !DefaultValueIsPoison)
6547 SingleValue =nullptr;
6548 }
6549
6550// If each element in the table contains the same value, we only need to store
6551// that single value.
6552if (SingleValue) {
6553Kind = SingleValueKind;
6554return;
6555 }
6556
6557// Check if we can derive the value with a linear transformation from the
6558// table index.
6559if (isa<IntegerType>(ValueType)) {
6560bool LinearMappingPossible =true;
6561APInt PrevVal;
6562APInt DistToPrev;
6563// When linear map is monotonic and signed overflow doesn't happen on
6564// maximum index, we can attach nsw on Add and Mul.
6565bool NonMonotonic =false;
6566assert(TableSize >= 2 &&"Should be a SingleValue table.");
6567// Check if there is the same distance between two consecutive values.
6568for (uint64_tI = 0;I < TableSize; ++I) {
6569ConstantInt *ConstVal = dyn_cast<ConstantInt>(TableContents[I]);
6570
6571if (!ConstVal && isa<PoisonValue>(TableContents[I])) {
6572// This is an poison, so it's (probably) a lookup table hole.
6573// To prevent any regressions from before we switched to using poison as
6574// the default value, holes will fall back to using the first value.
6575// This can be removed once we add proper handling for poisons in lookup
6576// tables.
6577 ConstVal = dyn_cast<ConstantInt>(Values[0].second);
6578 }
6579
6580if (!ConstVal) {
6581// This is an undef. We could deal with it, but undefs in lookup tables
6582// are very seldom. It's probably not worth the additional complexity.
6583 LinearMappingPossible =false;
6584break;
6585 }
6586constAPInt &Val = ConstVal->getValue();
6587if (I != 0) {
6588APInt Dist = Val - PrevVal;
6589if (I == 1) {
6590 DistToPrev = Dist;
6591 }elseif (Dist != DistToPrev) {
6592 LinearMappingPossible =false;
6593break;
6594 }
6595 NonMonotonic |=
6596 Dist.isStrictlyPositive() ? Val.sle(PrevVal) : Val.sgt(PrevVal);
6597 }
6598 PrevVal = Val;
6599 }
6600if (LinearMappingPossible) {
6601 LinearOffset = cast<ConstantInt>(TableContents[0]);
6602 LinearMultiplier = ConstantInt::get(M.getContext(), DistToPrev);
6603APIntM = LinearMultiplier->getValue();
6604bool MayWrap =true;
6605if (isIntN(M.getBitWidth(), TableSize - 1))
6606 (void)M.smul_ov(APInt(M.getBitWidth(), TableSize - 1), MayWrap);
6607 LinearMapValWrapped = NonMonotonic || MayWrap;
6608Kind = LinearMapKind;
6609 ++NumLinearMaps;
6610return;
6611 }
6612 }
6613
6614// If the type is integer and the table fits in a register, build a bitmap.
6615if (wouldFitInRegister(DL, TableSize,ValueType)) {
6616IntegerType *IT = cast<IntegerType>(ValueType);
6617APInt TableInt(TableSize *IT->getBitWidth(), 0);
6618for (uint64_tI = TableSize;I > 0; --I) {
6619 TableInt <<=IT->getBitWidth();
6620// Insert values into the bitmap. Undef values are set to zero.
6621if (!isa<UndefValue>(TableContents[I - 1])) {
6622ConstantInt *Val = cast<ConstantInt>(TableContents[I - 1]);
6623 TableInt |= Val->getValue().zext(TableInt.getBitWidth());
6624 }
6625 }
6626 BitMap = ConstantInt::get(M.getContext(), TableInt);
6627 BitMapElementTy =IT;
6628Kind = BitMapKind;
6629 ++NumBitMaps;
6630return;
6631 }
6632
6633// Store the table in an array.
6634ArrayType *ArrayTy = ArrayType::get(ValueType, TableSize);
6635Constant *Initializer =ConstantArray::get(ArrayTy, TableContents);
6636
6637Array =newGlobalVariable(M, ArrayTy,/*isConstant=*/true,
6638 GlobalVariable::PrivateLinkage, Initializer,
6639"switch.table." + FuncName);
6640Array->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
6641// Set the alignment to that of an array items. We will be only loading one
6642// value out of it.
6643Array->setAlignment(DL.getPrefTypeAlign(ValueType));
6644Kind = ArrayKind;
6645}
6646
6647Value *SwitchLookupTable::buildLookup(Value *Index,IRBuilder<> &Builder) {
6648switch (Kind) {
6649case SingleValueKind:
6650return SingleValue;
6651case LinearMapKind: {
6652// Derive the result value from the input value.
6653Value *Result = Builder.CreateIntCast(Index, LinearMultiplier->getType(),
6654false,"switch.idx.cast");
6655if (!LinearMultiplier->isOne())
6656Result = Builder.CreateMul(Result, LinearMultiplier,"switch.idx.mult",
6657/*HasNUW = */false,
6658/*HasNSW = */ !LinearMapValWrapped);
6659
6660if (!LinearOffset->isZero())
6661Result = Builder.CreateAdd(Result, LinearOffset,"switch.offset",
6662/*HasNUW = */false,
6663/*HasNSW = */ !LinearMapValWrapped);
6664returnResult;
6665 }
6666case BitMapKind: {
6667// Type of the bitmap (e.g. i59).
6668IntegerType *MapTy = BitMap->getIntegerType();
6669
6670// Cast Index to the same type as the bitmap.
6671// Note: The Index is <= the number of elements in the table, so
6672// truncating it to the width of the bitmask is safe.
6673Value *ShiftAmt = Builder.CreateZExtOrTrunc(Index, MapTy,"switch.cast");
6674
6675// Multiply the shift amount by the element width. NUW/NSW can always be
6676// set, because wouldFitInRegister guarantees Index * ShiftAmt is in
6677// BitMap's bit width.
6678 ShiftAmt = Builder.CreateMul(
6679 ShiftAmt, ConstantInt::get(MapTy, BitMapElementTy->getBitWidth()),
6680"switch.shiftamt",/*HasNUW =*/true,/*HasNSW =*/true);
6681
6682// Shift down.
6683Value *DownShifted =
6684 Builder.CreateLShr(BitMap, ShiftAmt,"switch.downshift");
6685// Mask off.
6686return Builder.CreateTrunc(DownShifted, BitMapElementTy,"switch.masked");
6687 }
6688case ArrayKind: {
6689// Make sure the table index will not overflow when treated as signed.
6690IntegerType *IT = cast<IntegerType>(Index->getType());
6691uint64_t TableSize =
6692Array->getInitializer()->getType()->getArrayNumElements();
6693if (TableSize > (1ULL << std::min(IT->getBitWidth() - 1, 63u)))
6694Index = Builder.CreateZExt(
6695 Index,IntegerType::get(IT->getContext(),IT->getBitWidth() + 1),
6696"switch.tableidx.zext");
6697
6698Value *GEPIndices[] = {Builder.getInt32(0),Index};
6699Value *GEP = Builder.CreateInBoundsGEP(Array->getValueType(), Array,
6700 GEPIndices,"switch.gep");
6701return Builder.CreateLoad(
6702 cast<ArrayType>(Array->getValueType())->getElementType(),GEP,
6703"switch.load");
6704 }
6705 }
6706llvm_unreachable("Unknown lookup table kind!");
6707}
6708
6709bool SwitchLookupTable::wouldFitInRegister(constDataLayout &DL,
6710uint64_t TableSize,
6711Type *ElementType) {
6712auto *IT = dyn_cast<IntegerType>(ElementType);
6713if (!IT)
6714returnfalse;
6715// FIXME: If the type is wider than it needs to be, e.g. i8 but all values
6716// are <= 15, we could try to narrow the type.
6717
6718// Avoid overflow, fitsInLegalInteger uses unsigned int for the width.
6719if (TableSize >= UINT_MAX /IT->getBitWidth())
6720returnfalse;
6721returnDL.fitsInLegalInteger(TableSize *IT->getBitWidth());
6722}
6723
6724staticboolisTypeLegalForLookupTable(Type *Ty,constTargetTransformInfo &TTI,
6725constDataLayout &DL) {
6726// Allow any legal type.
6727if (TTI.isTypeLegal(Ty))
6728returntrue;
6729
6730auto *IT = dyn_cast<IntegerType>(Ty);
6731if (!IT)
6732returnfalse;
6733
6734// Also allow power of 2 integer types that have at least 8 bits and fit in
6735// a register. These types are common in frontend languages and targets
6736// usually support loads of these types.
6737// TODO: We could relax this to any integer that fits in a register and rely
6738// on ABI alignment and padding in the table to allow the load to be widened.
6739// Or we could widen the constants and truncate the load.
6740unsignedBitWidth =IT->getBitWidth();
6741returnBitWidth >= 8 &&isPowerOf2_32(BitWidth) &&
6742DL.fitsInLegalInteger(IT->getBitWidth());
6743}
6744
6745staticboolisSwitchDense(uint64_t NumCases,uint64_t CaseRange) {
6746// 40% is the default density for building a jump table in optsize/minsize
6747// mode. See also TargetLoweringBase::isSuitableForJumpTable(), which this
6748// function was based on.
6749constuint64_t MinDensity = 40;
6750
6751if (CaseRange >=UINT64_MAX / 100)
6752returnfalse;// Avoid multiplication overflows below.
6753
6754return NumCases * 100 >= CaseRange * MinDensity;
6755}
6756
6757staticboolisSwitchDense(ArrayRef<int64_t> Values) {
6758uint64_t Diff = (uint64_t)Values.back() - (uint64_t)Values.front();
6759uint64_tRange = Diff + 1;
6760if (Range < Diff)
6761returnfalse;// Overflow.
6762
6763returnisSwitchDense(Values.size(),Range);
6764}
6765
6766/// Determine whether a lookup table should be built for this switch, based on
6767/// the number of cases, size of the table, and the types of the results.
6768// TODO: We could support larger than legal types by limiting based on the
6769// number of loads required and/or table size. If the constants are small we
6770// could use smaller table entries and extend after the load.
6771staticbool
6772shouldBuildLookupTable(SwitchInst *SI,uint64_t TableSize,
6773constTargetTransformInfo &TTI,constDataLayout &DL,
6774constSmallDenseMap<PHINode *, Type *> &ResultTypes) {
6775if (SI->getNumCases() > TableSize)
6776returnfalse;// TableSize overflowed.
6777
6778bool AllTablesFitInRegister =true;
6779bool HasIllegalType =false;
6780for (constauto &I : ResultTypes) {
6781Type *Ty =I.second;
6782
6783// Saturate this flag to true.
6784 HasIllegalType = HasIllegalType || !isTypeLegalForLookupTable(Ty,TTI,DL);
6785
6786// Saturate this flag to false.
6787 AllTablesFitInRegister =
6788 AllTablesFitInRegister &&
6789 SwitchLookupTable::wouldFitInRegister(DL, TableSize, Ty);
6790
6791// If both flags saturate, we're done. NOTE: This *only* works with
6792// saturating flags, and all flags have to saturate first due to the
6793// non-deterministic behavior of iterating over a dense map.
6794if (HasIllegalType && !AllTablesFitInRegister)
6795break;
6796 }
6797
6798// If each table would fit in a register, we should build it anyway.
6799if (AllTablesFitInRegister)
6800returntrue;
6801
6802// Don't build a table that doesn't fit in-register if it has illegal types.
6803if (HasIllegalType)
6804returnfalse;
6805
6806returnisSwitchDense(SI->getNumCases(), TableSize);
6807}
6808
6809staticboolshouldUseSwitchConditionAsTableIndex(
6810ConstantInt &MinCaseVal,constConstantInt &MaxCaseVal,
6811bool HasDefaultResults,constSmallDenseMap<PHINode *, Type *> &ResultTypes,
6812constDataLayout &DL,constTargetTransformInfo &TTI) {
6813if (MinCaseVal.isNullValue())
6814returntrue;
6815if (MinCaseVal.isNegative() ||
6816 MaxCaseVal.getLimitedValue() == std::numeric_limits<uint64_t>::max() ||
6817 !HasDefaultResults)
6818returnfalse;
6819returnall_of(ResultTypes, [&](constauto &KV) {
6820return SwitchLookupTable::wouldFitInRegister(
6821DL, MaxCaseVal.getLimitedValue() + 1/* TableSize */,
6822 KV.second/* ResultType */);
6823 });
6824}
6825
6826/// Try to reuse the switch table index compare. Following pattern:
6827/// \code
6828/// if (idx < tablesize)
6829/// r = table[idx]; // table does not contain default_value
6830/// else
6831/// r = default_value;
6832/// if (r != default_value)
6833/// ...
6834/// \endcode
6835/// Is optimized to:
6836/// \code
6837/// cond = idx < tablesize;
6838/// if (cond)
6839/// r = table[idx];
6840/// else
6841/// r = default_value;
6842/// if (cond)
6843/// ...
6844/// \endcode
6845/// Jump threading will then eliminate the second if(cond).
6846staticvoidreuseTableCompare(
6847User *PhiUser,BasicBlock *PhiBlock,BranchInst *RangeCheckBranch,
6848Constant *DefaultValue,
6849constSmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values) {
6850ICmpInst *CmpInst = dyn_cast<ICmpInst>(PhiUser);
6851if (!CmpInst)
6852return;
6853
6854// We require that the compare is in the same block as the phi so that jump
6855// threading can do its work afterwards.
6856if (CmpInst->getParent() != PhiBlock)
6857return;
6858
6859Constant *CmpOp1 = dyn_cast<Constant>(CmpInst->getOperand(1));
6860if (!CmpOp1)
6861return;
6862
6863Value *RangeCmp = RangeCheckBranch->getCondition();
6864Constant *TrueConst =ConstantInt::getTrue(RangeCmp->getType());
6865Constant *FalseConst =ConstantInt::getFalse(RangeCmp->getType());
6866
6867// Check if the compare with the default value is constant true or false.
6868constDataLayout &DL = PhiBlock->getDataLayout();
6869Constant *DefaultConst =ConstantFoldCompareInstOperands(
6870CmpInst->getPredicate(), DefaultValue, CmpOp1,DL);
6871if (DefaultConst != TrueConst && DefaultConst != FalseConst)
6872return;
6873
6874// Check if the compare with the case values is distinct from the default
6875// compare result.
6876for (auto ValuePair : Values) {
6877Constant *CaseConst =ConstantFoldCompareInstOperands(
6878CmpInst->getPredicate(), ValuePair.second, CmpOp1,DL);
6879if (!CaseConst || CaseConst == DefaultConst ||
6880 (CaseConst != TrueConst && CaseConst != FalseConst))
6881return;
6882 }
6883
6884// Check if the branch instruction dominates the phi node. It's a simple
6885// dominance check, but sufficient for our needs.
6886// Although this check is invariant in the calling loops, it's better to do it
6887// at this late stage. Practically we do it at most once for a switch.
6888BasicBlock *BranchBlock = RangeCheckBranch->getParent();
6889for (BasicBlock *Pred :predecessors(PhiBlock)) {
6890if (Pred != BranchBlock && Pred->getUniquePredecessor() != BranchBlock)
6891return;
6892 }
6893
6894if (DefaultConst == FalseConst) {
6895// The compare yields the same result. We can replace it.
6896CmpInst->replaceAllUsesWith(RangeCmp);
6897 ++NumTableCmpReuses;
6898 }else {
6899// The compare yields the same result, just inverted. We can replace it.
6900Value *InvertedTableCmp = BinaryOperator::CreateXor(
6901 RangeCmp, ConstantInt::get(RangeCmp->getType(), 1),"inverted.cmp",
6902 RangeCheckBranch->getIterator());
6903CmpInst->replaceAllUsesWith(InvertedTableCmp);
6904 ++NumTableCmpReuses;
6905 }
6906}
6907
6908/// If the switch is only used to initialize one or more phi nodes in a common
6909/// successor block with different constant values, replace the switch with
6910/// lookup tables.
6911staticboolswitchToLookupTable(SwitchInst *SI,IRBuilder<> &Builder,
6912DomTreeUpdater *DTU,constDataLayout &DL,
6913constTargetTransformInfo &TTI) {
6914assert(SI->getNumCases() > 1 &&"Degenerate switch?");
6915
6916BasicBlock *BB = SI->getParent();
6917Function *Fn = BB->getParent();
6918// Only build lookup table when we have a target that supports it or the
6919// attribute is not set.
6920if (!TTI.shouldBuildLookupTables() ||
6921 (Fn->getFnAttribute("no-jump-tables").getValueAsBool()))
6922returnfalse;
6923
6924// FIXME: If the switch is too sparse for a lookup table, perhaps we could
6925// split off a dense part and build a lookup table for that.
6926
6927// FIXME: This creates arrays of GEPs to constant strings, which means each
6928// GEP needs a runtime relocation in PIC code. We should just build one big
6929// string and lookup indices into that.
6930
6931// Ignore switches with less than three cases. Lookup tables will not make
6932// them faster, so we don't analyze them.
6933if (SI->getNumCases() < 3)
6934returnfalse;
6935
6936// Figure out the corresponding result for each case value and phi node in the
6937// common destination, as well as the min and max case values.
6938assert(!SI->cases().empty());
6939SwitchInst::CaseIt CI = SI->case_begin();
6940ConstantInt *MinCaseVal = CI->getCaseValue();
6941ConstantInt *MaxCaseVal = CI->getCaseValue();
6942
6943BasicBlock *CommonDest =nullptr;
6944
6945usingResultListTy =SmallVector<std::pair<ConstantInt *, Constant *>, 4>;
6946SmallDenseMap<PHINode *, ResultListTy> ResultLists;
6947
6948SmallDenseMap<PHINode *, Constant *> DefaultResults;
6949SmallDenseMap<PHINode *, Type *> ResultTypes;
6950SmallVector<PHINode *, 4> PHIs;
6951
6952for (SwitchInst::CaseIt E = SI->case_end(); CI != E; ++CI) {
6953ConstantInt *CaseVal = CI->getCaseValue();
6954if (CaseVal->getValue().slt(MinCaseVal->getValue()))
6955 MinCaseVal = CaseVal;
6956if (CaseVal->getValue().sgt(MaxCaseVal->getValue()))
6957 MaxCaseVal = CaseVal;
6958
6959// Resulting value at phi nodes for this case value.
6960usingResultsTy =SmallVector<std::pair<PHINode *, Constant *>, 4>;
6961 ResultsTyResults;
6962if (!getCaseResults(SI, CaseVal, CI->getCaseSuccessor(), &CommonDest,
6963Results,DL,TTI))
6964returnfalse;
6965
6966// Append the result from this case to the list for each phi.
6967for (constauto &I :Results) {
6968PHINode *PHI =I.first;
6969Constant *Value =I.second;
6970if (!ResultLists.count(PHI))
6971 PHIs.push_back(PHI);
6972 ResultLists[PHI].push_back(std::make_pair(CaseVal,Value));
6973 }
6974 }
6975
6976// Keep track of the result types.
6977for (PHINode *PHI : PHIs) {
6978 ResultTypes[PHI] = ResultLists[PHI][0].second->getType();
6979 }
6980
6981uint64_t NumResults = ResultLists[PHIs[0]].size();
6982
6983// If the table has holes, we need a constant result for the default case
6984// or a bitmask that fits in a register.
6985SmallVector<std::pair<PHINode *, Constant *>, 4> DefaultResultsList;
6986bool HasDefaultResults =
6987getCaseResults(SI,nullptr, SI->getDefaultDest(), &CommonDest,
6988 DefaultResultsList,DL,TTI);
6989
6990for (constauto &I : DefaultResultsList) {
6991PHINode *PHI =I.first;
6992Constant *Result =I.second;
6993 DefaultResults[PHI] = Result;
6994 }
6995
6996bool UseSwitchConditionAsTableIndex =shouldUseSwitchConditionAsTableIndex(
6997 *MinCaseVal, *MaxCaseVal, HasDefaultResults, ResultTypes,DL,TTI);
6998uint64_t TableSize;
6999if (UseSwitchConditionAsTableIndex)
7000 TableSize = MaxCaseVal->getLimitedValue() + 1;
7001else
7002 TableSize =
7003 (MaxCaseVal->getValue() - MinCaseVal->getValue()).getLimitedValue() + 1;
7004
7005// If the default destination is unreachable, or if the lookup table covers
7006// all values of the conditional variable, branch directly to the lookup table
7007// BB. Otherwise, check that the condition is within the case range.
7008bool DefaultIsReachable = !SI->defaultDestUndefined();
7009
7010bool TableHasHoles = (NumResults < TableSize);
7011
7012// If the table has holes but the default destination doesn't produce any
7013// constant results, the lookup table entries corresponding to the holes will
7014// contain poison.
7015bool AllHolesArePoison = TableHasHoles && !HasDefaultResults;
7016
7017// If the default destination doesn't produce a constant result but is still
7018// reachable, and the lookup table has holes, we need to use a mask to
7019// determine if the current index should load from the lookup table or jump
7020// to the default case.
7021// The mask is unnecessary if the table has holes but the default destination
7022// is unreachable, as in that case the holes must also be unreachable.
7023bool NeedMask = AllHolesArePoison && DefaultIsReachable;
7024if (NeedMask) {
7025// As an extra penalty for the validity test we require more cases.
7026if (SI->getNumCases() < 4)// FIXME: Find best threshold value (benchmark).
7027returnfalse;
7028if (!DL.fitsInLegalInteger(TableSize))
7029returnfalse;
7030 }
7031
7032if (!shouldBuildLookupTable(SI, TableSize,TTI,DL, ResultTypes))
7033returnfalse;
7034
7035 std::vector<DominatorTree::UpdateType> Updates;
7036
7037// Compute the maximum table size representable by the integer type we are
7038// switching upon.
7039unsigned CaseSize = MinCaseVal->getType()->getPrimitiveSizeInBits();
7040uint64_t MaxTableSize = CaseSize > 63 ?UINT64_MAX : 1ULL << CaseSize;
7041assert(MaxTableSize >= TableSize &&
7042"It is impossible for a switch to have more entries than the max "
7043"representable value of its input integer type's size.");
7044
7045// Create the BB that does the lookups.
7046Module &Mod = *CommonDest->getParent()->getParent();
7047BasicBlock *LookupBB =BasicBlock::Create(
7048Mod.getContext(),"switch.lookup", CommonDest->getParent(), CommonDest);
7049
7050// Compute the table index value.
7051 Builder.SetInsertPoint(SI);
7052Value *TableIndex;
7053ConstantInt *TableIndexOffset;
7054if (UseSwitchConditionAsTableIndex) {
7055 TableIndexOffset = ConstantInt::get(MaxCaseVal->getIntegerType(), 0);
7056 TableIndex = SI->getCondition();
7057 }else {
7058 TableIndexOffset = MinCaseVal;
7059// If the default is unreachable, all case values are s>= MinCaseVal. Then
7060// we can try to attach nsw.
7061bool MayWrap =true;
7062if (!DefaultIsReachable) {
7063APInt Res = MaxCaseVal->getValue().ssub_ov(MinCaseVal->getValue(), MayWrap);
7064 (void)Res;
7065 }
7066
7067 TableIndex = Builder.CreateSub(SI->getCondition(), TableIndexOffset,
7068"switch.tableidx",/*HasNUW =*/false,
7069/*HasNSW =*/!MayWrap);
7070 }
7071
7072BranchInst *RangeCheckBranch =nullptr;
7073
7074// Grow the table to cover all possible index values to avoid the range check.
7075// It will use the default result to fill in the table hole later, so make
7076// sure it exist.
7077if (UseSwitchConditionAsTableIndex && HasDefaultResults) {
7078ConstantRange CR =computeConstantRange(TableIndex,/* ForSigned */false);
7079// Grow the table shouldn't have any size impact by checking
7080// wouldFitInRegister.
7081// TODO: Consider growing the table also when it doesn't fit in a register
7082// if no optsize is specified.
7083constuint64_t UpperBound = CR.getUpper().getLimitedValue();
7084if (!CR.isUpperWrapped() &&all_of(ResultTypes, [&](constauto &KV) {
7085return SwitchLookupTable::wouldFitInRegister(
7086DL, UpperBound, KV.second/* ResultType */);
7087 })) {
7088// There may be some case index larger than the UpperBound (unreachable
7089// case), so make sure the table size does not get smaller.
7090 TableSize = std::max(UpperBound, TableSize);
7091// The default branch is unreachable after we enlarge the lookup table.
7092// Adjust DefaultIsReachable to reuse code path.
7093 DefaultIsReachable =false;
7094 }
7095 }
7096
7097constbool GeneratingCoveredLookupTable = (MaxTableSize == TableSize);
7098if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
7099 Builder.CreateBr(LookupBB);
7100if (DTU)
7101 Updates.push_back({DominatorTree::Insert, BB, LookupBB});
7102// Note: We call removeProdecessor later since we need to be able to get the
7103// PHI value for the default case in case we're using a bit mask.
7104 }else {
7105Value *Cmp = Builder.CreateICmpULT(
7106 TableIndex, ConstantInt::get(MinCaseVal->getType(), TableSize));
7107 RangeCheckBranch =
7108 Builder.CreateCondBr(Cmp, LookupBB, SI->getDefaultDest());
7109if (DTU)
7110 Updates.push_back({DominatorTree::Insert, BB, LookupBB});
7111 }
7112
7113// Populate the BB that does the lookups.
7114 Builder.SetInsertPoint(LookupBB);
7115
7116if (NeedMask) {
7117// Before doing the lookup, we do the hole check. The LookupBB is therefore
7118// re-purposed to do the hole check, and we create a new LookupBB.
7119BasicBlock *MaskBB = LookupBB;
7120 MaskBB->setName("switch.hole_check");
7121 LookupBB =BasicBlock::Create(Mod.getContext(),"switch.lookup",
7122 CommonDest->getParent(), CommonDest);
7123
7124// Make the mask's bitwidth at least 8-bit and a power-of-2 to avoid
7125// unnecessary illegal types.
7126uint64_t TableSizePowOf2 =NextPowerOf2(std::max(7ULL, TableSize - 1ULL));
7127APInt MaskInt(TableSizePowOf2, 0);
7128APInt One(TableSizePowOf2, 1);
7129// Build bitmask; fill in a 1 bit for every case.
7130const ResultListTy &ResultList = ResultLists[PHIs[0]];
7131for (size_tI = 0, E = ResultList.size();I != E; ++I) {
7132uint64_tIdx = (ResultList[I].first->getValue() - TableIndexOffset->getValue())
7133 .getLimitedValue();
7134 MaskInt |= One <<Idx;
7135 }
7136ConstantInt *TableMask = ConstantInt::get(Mod.getContext(), MaskInt);
7137
7138// Get the TableIndex'th bit of the bitmask.
7139// If this bit is 0 (meaning hole) jump to the default destination,
7140// else continue with table lookup.
7141IntegerType *MapTy = TableMask->getIntegerType();
7142Value *MaskIndex =
7143 Builder.CreateZExtOrTrunc(TableIndex, MapTy,"switch.maskindex");
7144Value *Shifted = Builder.CreateLShr(TableMask, MaskIndex,"switch.shifted");
7145Value *LoBit = Builder.CreateTrunc(
7146 Shifted,Type::getInt1Ty(Mod.getContext()),"switch.lobit");
7147 Builder.CreateCondBr(LoBit, LookupBB, SI->getDefaultDest());
7148if (DTU) {
7149 Updates.push_back({DominatorTree::Insert, MaskBB, LookupBB});
7150 Updates.push_back({DominatorTree::Insert, MaskBB, SI->getDefaultDest()});
7151 }
7152 Builder.SetInsertPoint(LookupBB);
7153addPredecessorToBlock(SI->getDefaultDest(), MaskBB, BB);
7154 }
7155
7156if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
7157// We cached PHINodes in PHIs. To avoid accessing deleted PHINodes later,
7158// do not delete PHINodes here.
7159 SI->getDefaultDest()->removePredecessor(BB,
7160/*KeepOneInputPHIs=*/true);
7161if (DTU)
7162 Updates.push_back({DominatorTree::Delete, BB, SI->getDefaultDest()});
7163 }
7164
7165for (PHINode *PHI : PHIs) {
7166const ResultListTy &ResultList = ResultLists[PHI];
7167
7168Type *ResultType = ResultList.begin()->second->getType();
7169
7170// Use any value to fill the lookup table holes.
7171Constant *DV =
7172 AllHolesArePoison ?PoisonValue::get(ResultType) : DefaultResults[PHI];
7173StringRef FuncName = Fn->getName();
7174 SwitchLookupTable Table(Mod, TableSize, TableIndexOffset, ResultList, DV,
7175DL, FuncName);
7176
7177Value *Result = Table.buildLookup(TableIndex, Builder);
7178
7179// Do a small peephole optimization: re-use the switch table compare if
7180// possible.
7181if (!TableHasHoles && HasDefaultResults && RangeCheckBranch) {
7182BasicBlock *PhiBlock =PHI->getParent();
7183// Search for compare instructions which use the phi.
7184for (auto *User :PHI->users()) {
7185reuseTableCompare(User, PhiBlock, RangeCheckBranch, DV, ResultList);
7186 }
7187 }
7188
7189PHI->addIncoming(Result, LookupBB);
7190 }
7191
7192 Builder.CreateBr(CommonDest);
7193if (DTU)
7194 Updates.push_back({DominatorTree::Insert, LookupBB, CommonDest});
7195
7196// Remove the switch.
7197SmallPtrSet<BasicBlock *, 8> RemovedSuccessors;
7198for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) {
7199BasicBlock *Succ = SI->getSuccessor(i);
7200
7201if (Succ == SI->getDefaultDest())
7202continue;
7203 Succ->removePredecessor(BB);
7204if (DTU && RemovedSuccessors.insert(Succ).second)
7205 Updates.push_back({DominatorTree::Delete, BB, Succ});
7206 }
7207 SI->eraseFromParent();
7208
7209if (DTU)
7210 DTU->applyUpdates(Updates);
7211
7212 ++NumLookupTables;
7213if (NeedMask)
7214 ++NumLookupTablesHoles;
7215returntrue;
7216}
7217
7218/// Try to transform a switch that has "holes" in it to a contiguous sequence
7219/// of cases.
7220///
7221/// A switch such as: switch(i) {case 5: case 9: case 13: case 17:} can be
7222/// range-reduced to: switch ((i-5) / 4) {case 0: case 1: case 2: case 3:}.
7223///
7224/// This converts a sparse switch into a dense switch which allows better
7225/// lowering and could also allow transforming into a lookup table.
7226staticboolreduceSwitchRange(SwitchInst *SI,IRBuilder<> &Builder,
7227constDataLayout &DL,
7228constTargetTransformInfo &TTI) {
7229auto *CondTy = cast<IntegerType>(SI->getCondition()->getType());
7230if (CondTy->getIntegerBitWidth() > 64 ||
7231 !DL.fitsInLegalInteger(CondTy->getIntegerBitWidth()))
7232returnfalse;
7233// Only bother with this optimization if there are more than 3 switch cases;
7234// SDAG will only bother creating jump tables for 4 or more cases.
7235if (SI->getNumCases() < 4)
7236returnfalse;
7237
7238// This transform is agnostic to the signedness of the input or case values. We
7239// can treat the case values as signed or unsigned. We can optimize more common
7240// cases such as a sequence crossing zero {-4,0,4,8} if we interpret case values
7241// as signed.
7242SmallVector<int64_t,4> Values;
7243for (constauto &C : SI->cases())
7244 Values.push_back(C.getCaseValue()->getValue().getSExtValue());
7245llvm::sort(Values);
7246
7247// If the switch is already dense, there's nothing useful to do here.
7248if (isSwitchDense(Values))
7249returnfalse;
7250
7251// First, transform the values such that they start at zero and ascend.
7252 int64_tBase = Values[0];
7253for (auto &V : Values)
7254 V -= (uint64_t)(Base);
7255
7256// Now we have signed numbers that have been shifted so that, given enough
7257// precision, there are no negative values. Since the rest of the transform
7258// is bitwise only, we switch now to an unsigned representation.
7259
7260// This transform can be done speculatively because it is so cheap - it
7261// results in a single rotate operation being inserted.
7262
7263// countTrailingZeros(0) returns 64. As Values is guaranteed to have more than
7264// one element and LLVM disallows duplicate cases, Shift is guaranteed to be
7265// less than 64.
7266unsigned Shift = 64;
7267for (auto &V : Values)
7268 Shift = std::min(Shift, (unsigned)llvm::countr_zero((uint64_t)V));
7269assert(Shift < 64);
7270if (Shift > 0)
7271for (auto &V : Values)
7272 V = (int64_t)((uint64_t)V >> Shift);
7273
7274if (!isSwitchDense(Values))
7275// Transform didn't create a dense switch.
7276returnfalse;
7277
7278// The obvious transform is to shift the switch condition right and emit a
7279// check that the condition actually cleanly divided by GCD, i.e.
7280// C & (1 << Shift - 1) == 0
7281// inserting a new CFG edge to handle the case where it didn't divide cleanly.
7282//
7283// A cheaper way of doing this is a simple ROTR(C, Shift). This performs the
7284// shift and puts the shifted-off bits in the uppermost bits. If any of these
7285// are nonzero then the switch condition will be very large and will hit the
7286// default case.
7287
7288auto *Ty = cast<IntegerType>(SI->getCondition()->getType());
7289 Builder.SetInsertPoint(SI);
7290Value *Sub =
7291 Builder.CreateSub(SI->getCondition(), ConstantInt::get(Ty,Base));
7292Value *Rot = Builder.CreateIntrinsic(
7293 Ty, Intrinsic::fshl,
7294 {Sub, Sub, ConstantInt::get(Ty, Ty->getBitWidth() - Shift)});
7295 SI->replaceUsesOfWith(SI->getCondition(), Rot);
7296
7297for (auto Case : SI->cases()) {
7298auto *Orig = Case.getCaseValue();
7299auto Sub = Orig->getValue() -APInt(Ty->getBitWidth(),Base,true);
7300 Case.setValue(cast<ConstantInt>(ConstantInt::get(Ty, Sub.lshr(Shift))));
7301 }
7302returntrue;
7303}
7304
7305/// Tries to transform switch of powers of two to reduce switch range.
7306/// For example, switch like:
7307/// switch (C) { case 1: case 2: case 64: case 128: }
7308/// will be transformed to:
7309/// switch (count_trailing_zeros(C)) { case 0: case 1: case 6: case 7: }
7310///
7311/// This transformation allows better lowering and could allow transforming into
7312/// a lookup table.
7313staticboolsimplifySwitchOfPowersOfTwo(SwitchInst *SI,IRBuilder<> &Builder,
7314constDataLayout &DL,
7315constTargetTransformInfo &TTI) {
7316Value *Condition = SI->getCondition();
7317LLVMContext &Context = SI->getContext();
7318auto *CondTy = cast<IntegerType>(Condition->getType());
7319
7320if (CondTy->getIntegerBitWidth() > 64 ||
7321 !DL.fitsInLegalInteger(CondTy->getIntegerBitWidth()))
7322returnfalse;
7323
7324constauto CttzIntrinsicCost =TTI.getIntrinsicInstrCost(
7325IntrinsicCostAttributes(Intrinsic::cttz, CondTy,
7326 {Condition,ConstantInt::getTrue(Context)}),
7327TTI::TCK_SizeAndLatency);
7328
7329if (CttzIntrinsicCost >TTI::TCC_Basic)
7330// Inserting intrinsic is too expensive.
7331returnfalse;
7332
7333// Only bother with this optimization if there are more than 3 switch cases.
7334// SDAG will only bother creating jump tables for 4 or more cases.
7335if (SI->getNumCases() < 4)
7336returnfalse;
7337
7338// We perform this optimization only for switches with
7339// unreachable default case.
7340// This assumtion will save us from checking if `Condition` is a power of two.
7341if (!isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg()))
7342returnfalse;
7343
7344// Check that switch cases are powers of two.
7345SmallVector<uint64_t, 4> Values;
7346for (constauto &Case : SI->cases()) {
7347uint64_t CaseValue = Case.getCaseValue()->getValue().getZExtValue();
7348if (llvm::has_single_bit(CaseValue))
7349 Values.push_back(CaseValue);
7350else
7351returnfalse;
7352 }
7353
7354// isSwichDense requires case values to be sorted.
7355llvm::sort(Values);
7356if (!isSwitchDense(Values.size(),llvm::countr_zero(Values.back()) -
7357llvm::countr_zero(Values.front()) + 1))
7358// Transform is unable to generate dense switch.
7359returnfalse;
7360
7361 Builder.SetInsertPoint(SI);
7362
7363// Replace each case with its trailing zeros number.
7364for (auto &Case : SI->cases()) {
7365auto *OrigValue = Case.getCaseValue();
7366 Case.setValue(ConstantInt::get(OrigValue->getIntegerType(),
7367 OrigValue->getValue().countr_zero()));
7368 }
7369
7370// Replace condition with its trailing zeros number.
7371auto *ConditionTrailingZeros = Builder.CreateIntrinsic(
7372 Intrinsic::cttz, {CondTy}, {Condition,ConstantInt::getTrue(Context)});
7373
7374 SI->setCondition(ConditionTrailingZeros);
7375
7376returntrue;
7377}
7378
7379/// Fold switch over ucmp/scmp intrinsic to br if two of the switch arms have
7380/// the same destination.
7381staticboolsimplifySwitchOfCmpIntrinsic(SwitchInst *SI,IRBuilderBase &Builder,
7382DomTreeUpdater *DTU) {
7383auto *Cmp = dyn_cast<CmpIntrinsic>(SI->getCondition());
7384if (!Cmp || !Cmp->hasOneUse())
7385returnfalse;
7386
7387SmallVector<uint32_t, 4> Weights;
7388bool HasWeights =extractBranchWeights(getBranchWeightMDNode(*SI), Weights);
7389if (!HasWeights)
7390 Weights.resize(4);// Avoid checking HasWeights everywhere.
7391
7392// Normalize to [us]cmp == Res ? Succ : OtherSucc.
7393 int64_t Res;
7394BasicBlock *Succ, *OtherSucc;
7395uint32_t SuccWeight = 0, OtherSuccWeight = 0;
7396BasicBlock *Unreachable =nullptr;
7397
7398if (SI->getNumCases() == 2) {
7399// Find which of 1, 0 or -1 is missing (handled by default dest).
7400SmallSet<int64_t, 3> Missing;
7401 Missing.insert(1);
7402 Missing.insert(0);
7403 Missing.insert(-1);
7404
7405 Succ = SI->getDefaultDest();
7406 SuccWeight = Weights[0];
7407OtherSucc =nullptr;
7408for (auto &Case : SI->cases()) {
7409 std::optional<int64_t> Val =
7410 Case.getCaseValue()->getValue().trySExtValue();
7411if (!Val)
7412returnfalse;
7413if (!Missing.erase(*Val))
7414returnfalse;
7415if (OtherSucc &&OtherSucc != Case.getCaseSuccessor())
7416returnfalse;
7417OtherSucc = Case.getCaseSuccessor();
7418 OtherSuccWeight += Weights[Case.getSuccessorIndex()];
7419 }
7420
7421assert(Missing.size() == 1 &&"Should have one case left");
7422 Res = *Missing.begin();
7423 }elseif (SI->getNumCases() == 3 && SI->defaultDestUndefined()) {
7424// Normalize so that Succ is taken once and OtherSucc twice.
7425 Unreachable = SI->getDefaultDest();
7426 Succ =OtherSucc =nullptr;
7427for (auto &Case : SI->cases()) {
7428BasicBlock *NewSucc = Case.getCaseSuccessor();
7429uint32_t Weight = Weights[Case.getSuccessorIndex()];
7430if (!OtherSucc ||OtherSucc == NewSucc) {
7431OtherSucc = NewSucc;
7432 OtherSuccWeight += Weight;
7433 }elseif (!Succ) {
7434 Succ = NewSucc;
7435 SuccWeight = Weight;
7436 }elseif (Succ == NewSucc) {
7437std::swap(Succ,OtherSucc);
7438std::swap(SuccWeight, OtherSuccWeight);
7439 }else
7440returnfalse;
7441 }
7442for (auto &Case : SI->cases()) {
7443 std::optional<int64_t> Val =
7444 Case.getCaseValue()->getValue().trySExtValue();
7445if (!Val || (Val != 1 && Val != 0 && Val != -1))
7446returnfalse;
7447if (Case.getCaseSuccessor() == Succ) {
7448 Res = *Val;
7449break;
7450 }
7451 }
7452 }else {
7453returnfalse;
7454 }
7455
7456// Determine predicate for the missing case.
7457ICmpInst::Predicate Pred;
7458switch (Res) {
7459case 1:
7460 Pred = ICmpInst::ICMP_UGT;
7461break;
7462case 0:
7463 Pred = ICmpInst::ICMP_EQ;
7464break;
7465case -1:
7466 Pred = ICmpInst::ICMP_ULT;
7467break;
7468 }
7469if (Cmp->isSigned())
7470 Pred =ICmpInst::getSignedPredicate(Pred);
7471
7472MDNode *NewWeights =nullptr;
7473if (HasWeights)
7474 NewWeights =MDBuilder(SI->getContext())
7475 .createBranchWeights(SuccWeight, OtherSuccWeight);
7476
7477BasicBlock *BB = SI->getParent();
7478 Builder.SetInsertPoint(SI->getIterator());
7479Value *ICmp = Builder.CreateICmp(Pred, Cmp->getLHS(), Cmp->getRHS());
7480 Builder.CreateCondBr(ICmp, Succ,OtherSucc, NewWeights,
7481 SI->getMetadata(LLVMContext::MD_unpredictable));
7482OtherSucc->removePredecessor(BB);
7483if (Unreachable)
7484 Unreachable->removePredecessor(BB);
7485 SI->eraseFromParent();
7486 Cmp->eraseFromParent();
7487if (DTU && Unreachable)
7488 DTU->applyUpdates({{DominatorTree::Delete, BB, Unreachable}});
7489returntrue;
7490}
7491
7492/// Checking whether two cases of SI are equal depends on the contents of the
7493/// BasicBlock and the incoming values of their successor PHINodes.
7494/// PHINode::getIncomingValueForBlock is O(|Preds|), so we'd like to avoid
7495/// calling this function on each BasicBlock every time isEqual is called,
7496/// especially since the same BasicBlock may be passed as an argument multiple
7497/// times. To do this, we can precompute a map of PHINode -> Pred BasicBlock ->
7498/// IncomingValue and add it in the Wrapper so isEqual can do O(1) checking
7499/// of the incoming values.
7500structSwitchSuccWrapper {
7501BasicBlock *Dest;
7502DenseMap<PHINode *, SmallDenseMap<BasicBlock *, Value *, 8>> *PhiPredIVs;
7503};
7504
7505namespacellvm {
7506template <>structDenseMapInfo<constSwitchSuccWrapper *> {
7507staticconstSwitchSuccWrapper *getEmptyKey() {
7508returnstatic_cast<SwitchSuccWrapper *>(
7509DenseMapInfo<void *>::getEmptyKey());
7510 }
7511staticconstSwitchSuccWrapper *getTombstoneKey() {
7512returnstatic_cast<SwitchSuccWrapper *>(
7513DenseMapInfo<void *>::getTombstoneKey());
7514 }
7515staticunsignedgetHashValue(constSwitchSuccWrapper *SSW) {
7516BasicBlock *Succ = SSW->Dest;
7517BranchInst *BI = cast<BranchInst>(Succ->getTerminator());
7518assert(BI->isUnconditional() &&
7519"Only supporting unconditional branches for now");
7520assert(BI->getNumSuccessors() == 1 &&
7521"Expected unconditional branches to have one successor");
7522assert(Succ->size() == 1 &&"Expected just a single branch in the BB");
7523
7524// Since we assume the BB is just a single BranchInst with a single
7525// successor, we hash as the BB and the incoming Values of its successor
7526// PHIs. Initially, we tried to just use the successor BB as the hash, but
7527// including the incoming PHI values leads to better performance.
7528// We also tried to build a map from BB -> Succs.IncomingValues ahead of
7529// time and passing it in SwitchSuccWrapper, but this slowed down the
7530// average compile time without having any impact on the worst case compile
7531// time.
7532BasicBlock *BB = BI->getSuccessor(0);
7533SmallVector<Value *> PhiValsForBB;
7534for (PHINode &Phi : BB->phis())
7535 PhiValsForBB.emplace_back((*SSW->PhiPredIVs)[&Phi][BB]);
7536
7537returnhash_combine(
7538 BB,hash_combine_range(PhiValsForBB.begin(), PhiValsForBB.end()));
7539 }
7540staticboolisEqual(constSwitchSuccWrapper *LHS,
7541constSwitchSuccWrapper *RHS) {
7542auto EKey =DenseMapInfo<SwitchSuccWrapper *>::getEmptyKey();
7543auto TKey =DenseMapInfo<SwitchSuccWrapper *>::getTombstoneKey();
7544if (LHS == EKey ||RHS == EKey ||LHS == TKey ||RHS == TKey)
7545returnLHS ==RHS;
7546
7547BasicBlock *A =LHS->Dest;
7548BasicBlock *B =RHS->Dest;
7549
7550// FIXME: we checked that the size of A and B are both 1 in
7551// simplifyDuplicateSwitchArms to make the Case list smaller to
7552// improve performance. If we decide to support BasicBlocks with more
7553// than just a single instruction, we need to check that A.size() ==
7554// B.size() here, and we need to check more than just the BranchInsts
7555// for equality.
7556
7557BranchInst *ABI = cast<BranchInst>(A->getTerminator());
7558BranchInst *BBI = cast<BranchInst>(B->getTerminator());
7559assert(ABI->isUnconditional() && BBI->isUnconditional() &&
7560"Only supporting unconditional branches for now");
7561if (ABI->getSuccessor(0) != BBI->getSuccessor(0))
7562returnfalse;
7563
7564// Need to check that PHIs in successor have matching values
7565BasicBlock *Succ = ABI->getSuccessor(0);
7566for (PHINode &Phi : Succ->phis()) {
7567auto &PredIVs = (*LHS->PhiPredIVs)[&Phi];
7568if (PredIVs[A] != PredIVs[B])
7569returnfalse;
7570 }
7571
7572returntrue;
7573 }
7574};
7575}// namespace llvm
7576
7577bool SimplifyCFGOpt::simplifyDuplicateSwitchArms(SwitchInst *SI,
7578DomTreeUpdater *DTU) {
7579// Build Cases. Skip BBs that are not candidates for simplification. Mark
7580// PHINodes which need to be processed into PhiPredIVs. We decide to process
7581// an entire PHI at once after the loop, opposed to calling
7582// getIncomingValueForBlock inside this loop, since each call to
7583// getIncomingValueForBlock is O(|Preds|).
7584SmallPtrSet<PHINode *, 8> Phis;
7585SmallPtrSet<BasicBlock *, 8> Seen;
7586DenseMap<PHINode *, SmallDenseMap<BasicBlock *, Value *, 8>> PhiPredIVs;
7587DenseMap<BasicBlock *, SmallVector<unsigned, 4>> BBToSuccessorIndexes;
7588SmallVector<SwitchSuccWrapper> Cases;
7589 Cases.reserve(SI->getNumSuccessors());
7590
7591for (unsignedI = 0;I <SI->getNumSuccessors(); ++I) {
7592BasicBlock *BB =SI->getSuccessor(I);
7593
7594// FIXME: Support more than just a single BranchInst. One way we could do
7595// this is by taking a hashing approach of all insts in BB.
7596if (BB->size() != 1)
7597continue;
7598
7599// FIXME: This case needs some extra care because the terminators other than
7600// SI need to be updated. For now, consider only backedges to the SI.
7601if (BB->hasNPredecessorsOrMore(4) ||
7602 BB->getUniquePredecessor() !=SI->getParent())
7603continue;
7604
7605// FIXME: Relax that the terminator is a BranchInst by checking for equality
7606// on other kinds of terminators. We decide to only support unconditional
7607// branches for now for compile time reasons.
7608auto *BI = dyn_cast<BranchInst>(BB->getTerminator());
7609if (!BI || BI->isConditional())
7610continue;
7611
7612if (Seen.insert(BB).second) {
7613// Keep track of which PHIs we need as keys in PhiPredIVs below.
7614for (BasicBlock *Succ : BI->successors())
7615for (PHINode &Phi : Succ->phis())
7616 Phis.insert(&Phi);
7617// Add the successor only if not previously visited.
7618 Cases.emplace_back(SwitchSuccWrapper{BB, &PhiPredIVs});
7619 }
7620
7621 BBToSuccessorIndexes[BB].emplace_back(I);
7622 }
7623
7624// Precompute a data structure to improve performance of isEqual for
7625// SwitchSuccWrapper.
7626 PhiPredIVs.reserve(Phis.size());
7627for (PHINode *Phi : Phis) {
7628 PhiPredIVs[Phi] =
7629SmallDenseMap<BasicBlock *, Value *, 8>(Phi->getNumIncomingValues());
7630for (auto &IV :Phi->incoming_values())
7631 PhiPredIVs[Phi].insert({Phi->getIncomingBlock(IV),IV.get()});
7632 }
7633
7634// Build a set such that if the SwitchSuccWrapper exists in the set and
7635// another SwitchSuccWrapper isEqual, then the equivalent SwitchSuccWrapper
7636// which is not in the set should be replaced with the one in the set. If the
7637// SwitchSuccWrapper is not in the set, then it should be added to the set so
7638// other SwitchSuccWrappers can check against it in the same manner. We use
7639// SwitchSuccWrapper instead of just BasicBlock because we'd like to pass
7640// around information to isEquality, getHashValue, and when doing the
7641// replacement with better performance.
7642DenseSet<const SwitchSuccWrapper *> ReplaceWith;
7643 ReplaceWith.reserve(Cases.size());
7644
7645SmallVector<DominatorTree::UpdateType> Updates;
7646 Updates.reserve(ReplaceWith.size());
7647bool MadeChange =false;
7648for (auto &SSW : Cases) {
7649// SSW is a candidate for simplification. If we find a duplicate BB,
7650// replace it.
7651constauto [It,Inserted] = ReplaceWith.insert(&SSW);
7652if (!Inserted) {
7653// We know that SI's parent BB no longer dominates the old case successor
7654// since we are making it dead.
7655 Updates.push_back({DominatorTree::Delete,SI->getParent(), SSW.Dest});
7656constauto &Successors = BBToSuccessorIndexes.at(SSW.Dest);
7657for (unsignedIdx : Successors)
7658SI->setSuccessor(Idx, (*It)->Dest);
7659 MadeChange =true;
7660 }
7661 }
7662
7663if (DTU)
7664 DTU->applyUpdates(Updates);
7665
7666return MadeChange;
7667}
7668
7669bool SimplifyCFGOpt::simplifySwitch(SwitchInst *SI,IRBuilder<> &Builder) {
7670BasicBlock *BB =SI->getParent();
7671
7672if (isValueEqualityComparison(SI)) {
7673// If we only have one predecessor, and if it is a branch on this value,
7674// see if that predecessor totally determines the outcome of this switch.
7675if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
7676if (simplifyEqualityComparisonWithOnlyPredecessor(SI, OnlyPred, Builder))
7677return requestResimplify();
7678
7679Value *Cond =SI->getCondition();
7680if (SelectInst *Select = dyn_cast<SelectInst>(Cond))
7681if (simplifySwitchOnSelect(SI,Select))
7682return requestResimplify();
7683
7684// If the block only contains the switch, see if we can fold the block
7685// away into any preds.
7686if (SI == &*BB->instructionsWithoutDebug(false).begin())
7687if (foldValueComparisonIntoPredecessors(SI, Builder))
7688return requestResimplify();
7689 }
7690
7691// Try to transform the switch into an icmp and a branch.
7692// The conversion from switch to comparison may lose information on
7693// impossible switch values, so disable it early in the pipeline.
7694if (Options.ConvertSwitchRangeToICmp && turnSwitchRangeIntoICmp(SI, Builder))
7695return requestResimplify();
7696
7697// Remove unreachable cases.
7698if (eliminateDeadSwitchCases(SI, DTU,Options.AC,DL))
7699return requestResimplify();
7700
7701if (simplifySwitchOfCmpIntrinsic(SI, Builder, DTU))
7702return requestResimplify();
7703
7704if (trySwitchToSelect(SI, Builder, DTU,DL,TTI))
7705return requestResimplify();
7706
7707if (Options.ForwardSwitchCondToPhi &&forwardSwitchConditionToPHI(SI))
7708return requestResimplify();
7709
7710// The conversion from switch to lookup tables results in difficult-to-analyze
7711// code and makes pruning branches much harder. This is a problem if the
7712// switch expression itself can still be restricted as a result of inlining or
7713// CVP. Therefore, only apply this transformation during late stages of the
7714// optimisation pipeline.
7715if (Options.ConvertSwitchToLookupTable &&
7716switchToLookupTable(SI, Builder, DTU,DL,TTI))
7717return requestResimplify();
7718
7719if (simplifySwitchOfPowersOfTwo(SI, Builder,DL,TTI))
7720return requestResimplify();
7721
7722if (reduceSwitchRange(SI, Builder,DL,TTI))
7723return requestResimplify();
7724
7725if (HoistCommon &&
7726 hoistCommonCodeFromSuccessors(SI, !Options.HoistCommonInsts))
7727return requestResimplify();
7728
7729if (simplifyDuplicateSwitchArms(SI, DTU))
7730return requestResimplify();
7731
7732returnfalse;
7733}
7734
7735bool SimplifyCFGOpt::simplifyIndirectBr(IndirectBrInst *IBI) {
7736BasicBlock *BB = IBI->getParent();
7737bool Changed =false;
7738
7739// Eliminate redundant destinations.
7740SmallPtrSet<Value *, 8> Succs;
7741SmallSetVector<BasicBlock *, 8> RemovedSuccs;
7742for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
7743BasicBlock *Dest = IBI->getDestination(i);
7744if (!Dest->hasAddressTaken() || !Succs.insert(Dest).second) {
7745if (!Dest->hasAddressTaken())
7746 RemovedSuccs.insert(Dest);
7747 Dest->removePredecessor(BB);
7748 IBI->removeDestination(i);
7749 --i;
7750 --e;
7751 Changed =true;
7752 }
7753 }
7754
7755if (DTU) {
7756 std::vector<DominatorTree::UpdateType> Updates;
7757 Updates.reserve(RemovedSuccs.size());
7758for (auto *RemovedSucc : RemovedSuccs)
7759 Updates.push_back({DominatorTree::Delete, BB, RemovedSucc});
7760 DTU->applyUpdates(Updates);
7761 }
7762
7763if (IBI->getNumDestinations() == 0) {
7764// If the indirectbr has no successors, change it to unreachable.
7765newUnreachableInst(IBI->getContext(), IBI->getIterator());
7766eraseTerminatorAndDCECond(IBI);
7767returntrue;
7768 }
7769
7770if (IBI->getNumDestinations() == 1) {
7771// If the indirectbr has one successor, change it to a direct branch.
7772BranchInst::Create(IBI->getDestination(0), IBI->getIterator());
7773eraseTerminatorAndDCECond(IBI);
7774returntrue;
7775 }
7776
7777if (SelectInst *SI = dyn_cast<SelectInst>(IBI->getAddress())) {
7778if (simplifyIndirectBrOnSelect(IBI, SI))
7779return requestResimplify();
7780 }
7781return Changed;
7782}
7783
7784/// Given an block with only a single landing pad and a unconditional branch
7785/// try to find another basic block which this one can be merged with. This
7786/// handles cases where we have multiple invokes with unique landing pads, but
7787/// a shared handler.
7788///
7789/// We specifically choose to not worry about merging non-empty blocks
7790/// here. That is a PRE/scheduling problem and is best solved elsewhere. In
7791/// practice, the optimizer produces empty landing pad blocks quite frequently
7792/// when dealing with exception dense code. (see: instcombine, gvn, if-else
7793/// sinking in this file)
7794///
7795/// This is primarily a code size optimization. We need to avoid performing
7796/// any transform which might inhibit optimization (such as our ability to
7797/// specialize a particular handler via tail commoning). We do this by not
7798/// merging any blocks which require us to introduce a phi. Since the same
7799/// values are flowing through both blocks, we don't lose any ability to
7800/// specialize. If anything, we make such specialization more likely.
7801///
7802/// TODO - This transformation could remove entries from a phi in the target
7803/// block when the inputs in the phi are the same for the two blocks being
7804/// merged. In some cases, this could result in removal of the PHI entirely.
7805staticbooltryToMergeLandingPad(LandingPadInst *LPad,BranchInst *BI,
7806BasicBlock *BB,DomTreeUpdater *DTU) {
7807auto Succ = BB->getUniqueSuccessor();
7808assert(Succ);
7809// If there's a phi in the successor block, we'd likely have to introduce
7810// a phi into the merged landing pad block.
7811if (isa<PHINode>(*Succ->begin()))
7812returnfalse;
7813
7814for (BasicBlock *OtherPred :predecessors(Succ)) {
7815if (BB == OtherPred)
7816continue;
7817BasicBlock::iteratorI = OtherPred->begin();
7818LandingPadInst *LPad2 = dyn_cast<LandingPadInst>(I);
7819if (!LPad2 || !LPad2->isIdenticalTo(LPad))
7820continue;
7821for (++I; isa<DbgInfoIntrinsic>(I); ++I)
7822 ;
7823BranchInst *BI2 = dyn_cast<BranchInst>(I);
7824if (!BI2 || !BI2->isIdenticalTo(BI))
7825continue;
7826
7827 std::vector<DominatorTree::UpdateType> Updates;
7828
7829// We've found an identical block. Update our predecessors to take that
7830// path instead and make ourselves dead.
7831SmallSetVector<BasicBlock *, 16> UniquePreds(pred_begin(BB),pred_end(BB));
7832for (BasicBlock *Pred : UniquePreds) {
7833InvokeInst *II = cast<InvokeInst>(Pred->getTerminator());
7834assert(II->getNormalDest() != BB &&II->getUnwindDest() == BB &&
7835"unexpected successor");
7836II->setUnwindDest(OtherPred);
7837if (DTU) {
7838 Updates.push_back({DominatorTree::Insert, Pred, OtherPred});
7839 Updates.push_back({DominatorTree::Delete, Pred, BB});
7840 }
7841 }
7842
7843// The debug info in OtherPred doesn't cover the merged control flow that
7844// used to go through BB. We need to delete it or update it.
7845for (Instruction &Inst :llvm::make_early_inc_range(*OtherPred))
7846if (isa<DbgInfoIntrinsic>(Inst))
7847 Inst.eraseFromParent();
7848
7849SmallSetVector<BasicBlock *, 16> UniqueSuccs(succ_begin(BB),succ_end(BB));
7850for (BasicBlock *Succ : UniqueSuccs) {
7851 Succ->removePredecessor(BB);
7852if (DTU)
7853 Updates.push_back({DominatorTree::Delete, BB, Succ});
7854 }
7855
7856IRBuilder<> Builder(BI);
7857 Builder.CreateUnreachable();
7858 BI->eraseFromParent();
7859if (DTU)
7860 DTU->applyUpdates(Updates);
7861returntrue;
7862 }
7863returnfalse;
7864}
7865
7866bool SimplifyCFGOpt::simplifyBranch(BranchInst *Branch,IRBuilder<> &Builder) {
7867returnBranch->isUnconditional() ? simplifyUncondBranch(Branch, Builder)
7868 : simplifyCondBranch(Branch, Builder);
7869}
7870
7871bool SimplifyCFGOpt::simplifyUncondBranch(BranchInst *BI,
7872IRBuilder<> &Builder) {
7873BasicBlock *BB = BI->getParent();
7874BasicBlock *Succ = BI->getSuccessor(0);
7875
7876// If the Terminator is the only non-phi instruction, simplify the block.
7877// If LoopHeader is provided, check if the block or its successor is a loop
7878// header. (This is for early invocations before loop simplify and
7879// vectorization to keep canonical loop forms for nested loops. These blocks
7880// can be eliminated when the pass is invoked later in the back-end.)
7881// Note that if BB has only one predecessor then we do not introduce new
7882// backedge, so we can eliminate BB.
7883bool NeedCanonicalLoop =
7884Options.NeedCanonicalLoop &&
7885 (!LoopHeaders.empty() && BB->hasNPredecessorsOrMore(2) &&
7886 (is_contained(LoopHeaders, BB) ||is_contained(LoopHeaders, Succ)));
7887BasicBlock::iteratorI = BB->getFirstNonPHIOrDbg();
7888if (I->isTerminator() && BB != &BB->getParent()->getEntryBlock() &&
7889 !NeedCanonicalLoop &&TryToSimplifyUncondBranchFromEmptyBlock(BB, DTU))
7890returntrue;
7891
7892// If the only instruction in the block is a seteq/setne comparison against a
7893// constant, try to simplify the block.
7894if (ICmpInst *ICI = dyn_cast<ICmpInst>(I))
7895if (ICI->isEquality() && isa<ConstantInt>(ICI->getOperand(1))) {
7896for (++I; isa<DbgInfoIntrinsic>(I); ++I)
7897 ;
7898if (I->isTerminator() &&
7899 tryToSimplifyUncondBranchWithICmpInIt(ICI, Builder))
7900returntrue;
7901 }
7902
7903// See if we can merge an empty landing pad block with another which is
7904// equivalent.
7905if (LandingPadInst *LPad = dyn_cast<LandingPadInst>(I)) {
7906for (++I; isa<DbgInfoIntrinsic>(I); ++I)
7907 ;
7908if (I->isTerminator() &&tryToMergeLandingPad(LPad, BI, BB, DTU))
7909returntrue;
7910 }
7911
7912// If this basic block is ONLY a compare and a branch, and if a predecessor
7913// branches to us and our successor, fold the comparison into the
7914// predecessor and use logical operations to update the incoming value
7915// for PHI nodes in common successor.
7916if (Options.SpeculateBlocks &&
7917foldBranchToCommonDest(BI, DTU,/*MSSAU=*/nullptr, &TTI,
7918Options.BonusInstThreshold))
7919return requestResimplify();
7920returnfalse;
7921}
7922
7923staticBasicBlock *allPredecessorsComeFromSameSource(BasicBlock *BB) {
7924BasicBlock *PredPred =nullptr;
7925for (auto *P :predecessors(BB)) {
7926BasicBlock *PPred =P->getSinglePredecessor();
7927if (!PPred || (PredPred && PredPred != PPred))
7928returnnullptr;
7929 PredPred = PPred;
7930 }
7931return PredPred;
7932}
7933
7934/// Fold the following pattern:
7935/// bb0:
7936/// br i1 %cond1, label %bb1, label %bb2
7937/// bb1:
7938/// br i1 %cond2, label %bb3, label %bb4
7939/// bb2:
7940/// br i1 %cond2, label %bb4, label %bb3
7941/// bb3:
7942/// ...
7943/// bb4:
7944/// ...
7945/// into
7946/// bb0:
7947/// %cond = xor i1 %cond1, %cond2
7948/// br i1 %cond, label %bb4, label %bb3
7949/// bb3:
7950/// ...
7951/// bb4:
7952/// ...
7953/// NOTE: %cond2 always dominates the terminator of bb0.
7954staticboolmergeNestedCondBranch(BranchInst *BI,DomTreeUpdater *DTU) {
7955BasicBlock *BB = BI->getParent();
7956BasicBlock *BB1 = BI->getSuccessor(0);
7957BasicBlock *BB2 = BI->getSuccessor(1);
7958auto IsSimpleSuccessor = [BB](BasicBlock *Succ,BranchInst *&SuccBI) {
7959if (Succ == BB)
7960returnfalse;
7961if (&Succ->front() != Succ->getTerminator())
7962returnfalse;
7963 SuccBI = dyn_cast<BranchInst>(Succ->getTerminator());
7964if (!SuccBI || !SuccBI->isConditional())
7965returnfalse;
7966BasicBlock *Succ1 = SuccBI->getSuccessor(0);
7967BasicBlock *Succ2 = SuccBI->getSuccessor(1);
7968return Succ1 != Succ && Succ2 != Succ && Succ1 != BB && Succ2 != BB &&
7969 !isa<PHINode>(Succ1->front()) && !isa<PHINode>(Succ2->front());
7970 };
7971BranchInst *BB1BI, *BB2BI;
7972if (!IsSimpleSuccessor(BB1, BB1BI) || !IsSimpleSuccessor(BB2, BB2BI))
7973returnfalse;
7974
7975if (BB1BI->getCondition() != BB2BI->getCondition() ||
7976 BB1BI->getSuccessor(0) != BB2BI->getSuccessor(1) ||
7977 BB1BI->getSuccessor(1) != BB2BI->getSuccessor(0))
7978returnfalse;
7979
7980BasicBlock *BB3 = BB1BI->getSuccessor(0);
7981BasicBlock *BB4 = BB1BI->getSuccessor(1);
7982IRBuilder<> Builder(BI);
7983 BI->setCondition(
7984 Builder.CreateXor(BI->getCondition(), BB1BI->getCondition()));
7985 BB1->removePredecessor(BB);
7986 BI->setSuccessor(0, BB4);
7987 BB2->removePredecessor(BB);
7988 BI->setSuccessor(1, BB3);
7989if (DTU) {
7990SmallVector<DominatorTree::UpdateType, 4> Updates;
7991 Updates.push_back({DominatorTree::Delete, BB, BB1});
7992 Updates.push_back({DominatorTree::Insert, BB, BB4});
7993 Updates.push_back({DominatorTree::Delete, BB, BB2});
7994 Updates.push_back({DominatorTree::Insert, BB, BB3});
7995
7996 DTU->applyUpdates(Updates);
7997 }
7998bool HasWeight =false;
7999uint64_t BBTWeight, BBFWeight;
8000if (extractBranchWeights(*BI, BBTWeight, BBFWeight))
8001 HasWeight =true;
8002else
8003 BBTWeight = BBFWeight = 1;
8004uint64_t BB1TWeight, BB1FWeight;
8005if (extractBranchWeights(*BB1BI, BB1TWeight, BB1FWeight))
8006 HasWeight =true;
8007else
8008 BB1TWeight = BB1FWeight = 1;
8009uint64_t BB2TWeight, BB2FWeight;
8010if (extractBranchWeights(*BB2BI, BB2TWeight, BB2FWeight))
8011 HasWeight =true;
8012else
8013 BB2TWeight = BB2FWeight = 1;
8014if (HasWeight) {
8015uint64_t Weights[2] = {BBTWeight * BB1FWeight + BBFWeight * BB2TWeight,
8016 BBTWeight * BB1TWeight + BBFWeight * BB2FWeight};
8017fitWeights(Weights);
8018setBranchWeights(BI, Weights[0], Weights[1],/*IsExpected=*/false);
8019 }
8020returntrue;
8021}
8022
8023bool SimplifyCFGOpt::simplifyCondBranch(BranchInst *BI,IRBuilder<> &Builder) {
8024assert(
8025 !isa<ConstantInt>(BI->getCondition()) &&
8026 BI->getSuccessor(0) != BI->getSuccessor(1) &&
8027"Tautological conditional branch should have been eliminated already.");
8028
8029BasicBlock *BB = BI->getParent();
8030if (!Options.SimplifyCondBranch ||
8031 BI->getFunction()->hasFnAttribute(Attribute::OptForFuzzing))
8032returnfalse;
8033
8034// Conditional branch
8035if (isValueEqualityComparison(BI)) {
8036// If we only have one predecessor, and if it is a branch on this value,
8037// see if that predecessor totally determines the outcome of this
8038// switch.
8039if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
8040if (simplifyEqualityComparisonWithOnlyPredecessor(BI, OnlyPred, Builder))
8041return requestResimplify();
8042
8043// This block must be empty, except for the setcond inst, if it exists.
8044// Ignore dbg and pseudo intrinsics.
8045autoI = BB->instructionsWithoutDebug(true).begin();
8046if (&*I == BI) {
8047if (foldValueComparisonIntoPredecessors(BI, Builder))
8048return requestResimplify();
8049 }elseif (&*I == cast<Instruction>(BI->getCondition())) {
8050 ++I;
8051if (&*I == BI && foldValueComparisonIntoPredecessors(BI, Builder))
8052return requestResimplify();
8053 }
8054 }
8055
8056// Try to turn "br (X == 0 | X == 1), T, F" into a switch instruction.
8057if (simplifyBranchOnICmpChain(BI, Builder,DL))
8058returntrue;
8059
8060// If this basic block has dominating predecessor blocks and the dominating
8061// blocks' conditions imply BI's condition, we know the direction of BI.
8062 std::optional<bool> Imp =isImpliedByDomCondition(BI->getCondition(), BI,DL);
8063if (Imp) {
8064// Turn this into a branch on constant.
8065auto *OldCond = BI->getCondition();
8066ConstantInt *TorF = *Imp ?ConstantInt::getTrue(BB->getContext())
8067 :ConstantInt::getFalse(BB->getContext());
8068 BI->setCondition(TorF);
8069RecursivelyDeleteTriviallyDeadInstructions(OldCond);
8070return requestResimplify();
8071 }
8072
8073// If this basic block is ONLY a compare and a branch, and if a predecessor
8074// branches to us and one of our successors, fold the comparison into the
8075// predecessor and use logical operations to pick the right destination.
8076if (Options.SpeculateBlocks &&
8077foldBranchToCommonDest(BI, DTU,/*MSSAU=*/nullptr, &TTI,
8078Options.BonusInstThreshold))
8079return requestResimplify();
8080
8081// We have a conditional branch to two blocks that are only reachable
8082// from BI. We know that the condbr dominates the two blocks, so see if
8083// there is any identical code in the "then" and "else" blocks. If so, we
8084// can hoist it up to the branching block.
8085if (BI->getSuccessor(0)->getSinglePredecessor()) {
8086if (BI->getSuccessor(1)->getSinglePredecessor()) {
8087if (HoistCommon &&
8088 hoistCommonCodeFromSuccessors(BI, !Options.HoistCommonInsts))
8089return requestResimplify();
8090
8091if (BI &&HoistLoadsStoresWithCondFaulting &&
8092Options.HoistLoadsStoresWithCondFaulting &&
8093isProfitableToSpeculate(BI, std::nullopt,TTI)) {
8094SmallVector<Instruction *, 2> SpeculatedConditionalLoadsStores;
8095auto CanSpeculateConditionalLoadsStores = [&]() {
8096for (auto *Succ :successors(BB)) {
8097for (Instruction &I : *Succ) {
8098if (I.isTerminator()) {
8099if (I.getNumSuccessors() > 1)
8100returnfalse;
8101continue;
8102 }elseif (!isSafeCheapLoadStore(&I,TTI) ||
8103 SpeculatedConditionalLoadsStores.size() ==
8104HoistLoadsStoresWithCondFaultingThreshold) {
8105returnfalse;
8106 }
8107 SpeculatedConditionalLoadsStores.push_back(&I);
8108 }
8109 }
8110return !SpeculatedConditionalLoadsStores.empty();
8111 };
8112
8113if (CanSpeculateConditionalLoadsStores()) {
8114hoistConditionalLoadsStores(BI, SpeculatedConditionalLoadsStores,
8115 std::nullopt);
8116return requestResimplify();
8117 }
8118 }
8119 }else {
8120// If Successor #1 has multiple preds, we may be able to conditionally
8121// execute Successor #0 if it branches to Successor #1.
8122Instruction *Succ0TI = BI->getSuccessor(0)->getTerminator();
8123if (Succ0TI->getNumSuccessors() == 1 &&
8124 Succ0TI->getSuccessor(0) == BI->getSuccessor(1))
8125if (speculativelyExecuteBB(BI, BI->getSuccessor(0)))
8126return requestResimplify();
8127 }
8128 }elseif (BI->getSuccessor(1)->getSinglePredecessor()) {
8129// If Successor #0 has multiple preds, we may be able to conditionally
8130// execute Successor #1 if it branches to Successor #0.
8131Instruction *Succ1TI = BI->getSuccessor(1)->getTerminator();
8132if (Succ1TI->getNumSuccessors() == 1 &&
8133 Succ1TI->getSuccessor(0) == BI->getSuccessor(0))
8134if (speculativelyExecuteBB(BI, BI->getSuccessor(1)))
8135return requestResimplify();
8136 }
8137
8138// If this is a branch on something for which we know the constant value in
8139// predecessors (e.g. a phi node in the current block), thread control
8140// through this block.
8141if (foldCondBranchOnValueKnownInPredecessor(BI, DTU,DL,Options.AC))
8142return requestResimplify();
8143
8144// Scan predecessor blocks for conditional branches.
8145for (BasicBlock *Pred :predecessors(BB))
8146if (BranchInst *PBI = dyn_cast<BranchInst>(Pred->getTerminator()))
8147if (PBI != BI && PBI->isConditional())
8148if (SimplifyCondBranchToCondBranch(PBI, BI, DTU,DL,TTI))
8149return requestResimplify();
8150
8151// Look for diamond patterns.
8152if (MergeCondStores)
8153if (BasicBlock *PrevBB =allPredecessorsComeFromSameSource(BB))
8154if (BranchInst *PBI = dyn_cast<BranchInst>(PrevBB->getTerminator()))
8155if (PBI != BI && PBI->isConditional())
8156if (mergeConditionalStores(PBI, BI, DTU,DL,TTI))
8157return requestResimplify();
8158
8159// Look for nested conditional branches.
8160if (mergeNestedCondBranch(BI, DTU))
8161return requestResimplify();
8162
8163returnfalse;
8164}
8165
8166/// Check if passing a value to an instruction will cause undefined behavior.
8167staticboolpassingValueIsAlwaysUndefined(Value *V,Instruction *I,bool PtrValueMayBeModified) {
8168Constant *C = dyn_cast<Constant>(V);
8169if (!C)
8170returnfalse;
8171
8172if (I->use_empty())
8173returnfalse;
8174
8175if (C->isNullValue() || isa<UndefValue>(C)) {
8176// Only look at the first use we can handle, avoid hurting compile time with
8177// long uselists
8178auto FindUse =llvm::find_if(I->users(), [](auto *U) {
8179 auto *Use = cast<Instruction>(U);
8180// Change this list when we want to add new instructions.
8181 switch (Use->getOpcode()) {
8182 default:
8183 return false;
8184 case Instruction::GetElementPtr:
8185 case Instruction::Ret:
8186 case Instruction::BitCast:
8187 case Instruction::Load:
8188 case Instruction::Store:
8189 case Instruction::Call:
8190 case Instruction::CallBr:
8191 case Instruction::Invoke:
8192 case Instruction::UDiv:
8193 case Instruction::URem:
8194// Note: signed div/rem of INT_MIN / -1 is also immediate UB, not
8195// implemented to avoid code complexity as it is unclear how useful such
8196// logic is.
8197 case Instruction::SDiv:
8198 case Instruction::SRem:
8199 return true;
8200 }
8201 });
8202if (FindUse ==I->user_end())
8203returnfalse;
8204auto *Use = cast<Instruction>(*FindUse);
8205// Bail out if Use is not in the same BB as I or Use == I or Use comes
8206// before I in the block. The latter two can be the case if Use is a
8207// PHI node.
8208if (Use->getParent() !=I->getParent() ||Use ==I ||Use->comesBefore(I))
8209returnfalse;
8210
8211// Now make sure that there are no instructions in between that can alter
8212// control flow (eg. calls)
8213auto InstrRange =
8214make_range(std::next(I->getIterator()),Use->getIterator());
8215if (any_of(InstrRange, [](Instruction &I) {
8216return !isGuaranteedToTransferExecutionToSuccessor(&I);
8217 }))
8218returnfalse;
8219
8220// Look through GEPs. A load from a GEP derived from NULL is still undefined
8221if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Use))
8222if (GEP->getPointerOperand() ==I) {
8223// The current base address is null, there are four cases to consider:
8224// getelementptr (TY, null, 0) -> null
8225// getelementptr (TY, null, not zero) -> may be modified
8226// getelementptr inbounds (TY, null, 0) -> null
8227// getelementptr inbounds (TY, null, not zero) -> poison iff null is
8228// undefined?
8229if (!GEP->hasAllZeroIndices() &&
8230 (!GEP->isInBounds() ||
8231NullPointerIsDefined(GEP->getFunction(),
8232GEP->getPointerAddressSpace())))
8233 PtrValueMayBeModified =true;
8234returnpassingValueIsAlwaysUndefined(V,GEP, PtrValueMayBeModified);
8235 }
8236
8237// Look through return.
8238if (ReturnInst *Ret = dyn_cast<ReturnInst>(Use)) {
8239bool HasNoUndefAttr =
8240 Ret->getFunction()->hasRetAttribute(Attribute::NoUndef);
8241// Return undefined to a noundef return value is undefined.
8242if (isa<UndefValue>(C) && HasNoUndefAttr)
8243returntrue;
8244// Return null to a nonnull+noundef return value is undefined.
8245if (C->isNullValue() && HasNoUndefAttr &&
8246 Ret->getFunction()->hasRetAttribute(Attribute::NonNull)) {
8247return !PtrValueMayBeModified;
8248 }
8249 }
8250
8251// Load from null is undefined.
8252if (LoadInst *LI = dyn_cast<LoadInst>(Use))
8253if (!LI->isVolatile())
8254return !NullPointerIsDefined(LI->getFunction(),
8255 LI->getPointerAddressSpace());
8256
8257// Store to null is undefined.
8258if (StoreInst *SI = dyn_cast<StoreInst>(Use))
8259if (!SI->isVolatile())
8260return (!NullPointerIsDefined(SI->getFunction(),
8261 SI->getPointerAddressSpace())) &&
8262 SI->getPointerOperand() ==I;
8263
8264// llvm.assume(false/undef) always triggers immediate UB.
8265if (auto *Assume = dyn_cast<AssumeInst>(Use)) {
8266// Ignore assume operand bundles.
8267if (I == Assume->getArgOperand(0))
8268returntrue;
8269 }
8270
8271if (auto *CB = dyn_cast<CallBase>(Use)) {
8272if (C->isNullValue() &&NullPointerIsDefined(CB->getFunction()))
8273returnfalse;
8274// A call to null is undefined.
8275if (CB->getCalledOperand() ==I)
8276returntrue;
8277
8278if (C->isNullValue()) {
8279for (constllvm::Use &Arg : CB->args())
8280if (Arg ==I) {
8281unsigned ArgIdx = CB->getArgOperandNo(&Arg);
8282if (CB->isPassingUndefUB(ArgIdx) &&
8283 CB->paramHasAttr(ArgIdx, Attribute::NonNull)) {
8284// Passing null to a nonnnull+noundef argument is undefined.
8285return !PtrValueMayBeModified;
8286 }
8287 }
8288 }elseif (isa<UndefValue>(C)) {
8289// Passing undef to a noundef argument is undefined.
8290for (constllvm::Use &Arg : CB->args())
8291if (Arg ==I) {
8292unsigned ArgIdx = CB->getArgOperandNo(&Arg);
8293if (CB->isPassingUndefUB(ArgIdx)) {
8294// Passing undef to a noundef argument is undefined.
8295returntrue;
8296 }
8297 }
8298 }
8299 }
8300// Div/Rem by zero is immediate UB
8301if (match(Use,m_BinOp(m_Value(),m_Specific(I))) &&Use->isIntDivRem())
8302returntrue;
8303 }
8304returnfalse;
8305}
8306
8307/// If BB has an incoming value that will always trigger undefined behavior
8308/// (eg. null pointer dereference), remove the branch leading here.
8309staticboolremoveUndefIntroducingPredecessor(BasicBlock *BB,
8310DomTreeUpdater *DTU,
8311AssumptionCache *AC) {
8312for (PHINode &PHI : BB->phis())
8313for (unsigned i = 0, e =PHI.getNumIncomingValues(); i != e; ++i)
8314if (passingValueIsAlwaysUndefined(PHI.getIncomingValue(i), &PHI)) {
8315BasicBlock *Predecessor =PHI.getIncomingBlock(i);
8316Instruction *T = Predecessor->getTerminator();
8317IRBuilder<> Builder(T);
8318if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
8319 BB->removePredecessor(Predecessor);
8320// Turn unconditional branches into unreachables and remove the dead
8321// destination from conditional branches.
8322if (BI->isUnconditional())
8323 Builder.CreateUnreachable();
8324else {
8325// Preserve guarding condition in assume, because it might not be
8326// inferrable from any dominating condition.
8327Value *Cond = BI->getCondition();
8328CallInst *Assumption;
8329if (BI->getSuccessor(0) == BB)
8330 Assumption = Builder.CreateAssumption(Builder.CreateNot(Cond));
8331else
8332 Assumption = Builder.CreateAssumption(Cond);
8333if (AC)
8334 AC->registerAssumption(cast<AssumeInst>(Assumption));
8335 Builder.CreateBr(BI->getSuccessor(0) == BB ? BI->getSuccessor(1)
8336 : BI->getSuccessor(0));
8337 }
8338 BI->eraseFromParent();
8339if (DTU)
8340 DTU->applyUpdates({{DominatorTree::Delete, Predecessor, BB}});
8341returntrue;
8342 }elseif (SwitchInst *SI = dyn_cast<SwitchInst>(T)) {
8343// Redirect all branches leading to UB into
8344// a newly created unreachable block.
8345BasicBlock *Unreachable =BasicBlock::Create(
8346 Predecessor->getContext(),"unreachable", BB->getParent(), BB);
8347 Builder.SetInsertPoint(Unreachable);
8348// The new block contains only one instruction: Unreachable
8349 Builder.CreateUnreachable();
8350for (constauto &Case : SI->cases())
8351if (Case.getCaseSuccessor() == BB) {
8352 BB->removePredecessor(Predecessor);
8353 Case.setSuccessor(Unreachable);
8354 }
8355if (SI->getDefaultDest() == BB) {
8356 BB->removePredecessor(Predecessor);
8357 SI->setDefaultDest(Unreachable);
8358 }
8359
8360if (DTU)
8361 DTU->applyUpdates(
8362 { {DominatorTree::Insert, Predecessor, Unreachable },
8363 {DominatorTree::Delete, Predecessor, BB } });
8364returntrue;
8365 }
8366 }
8367
8368returnfalse;
8369}
8370
8371bool SimplifyCFGOpt::simplifyOnce(BasicBlock *BB) {
8372bool Changed =false;
8373
8374assert(BB && BB->getParent() &&"Block not embedded in function!");
8375assert(BB->getTerminator() &&"Degenerate basic block encountered!");
8376
8377// Remove basic blocks that have no predecessors (except the entry block)...
8378// or that just have themself as a predecessor. These are unreachable.
8379if ((pred_empty(BB) && BB != &BB->getParent()->getEntryBlock()) ||
8380 BB->getSinglePredecessor() == BB) {
8381LLVM_DEBUG(dbgs() <<"Removing BB: \n" << *BB);
8382DeleteDeadBlock(BB, DTU);
8383returntrue;
8384 }
8385
8386// Check to see if we can constant propagate this terminator instruction
8387// away...
8388 Changed |=ConstantFoldTerminator(BB,/*DeleteDeadConditions=*/true,
8389/*TLI=*/nullptr, DTU);
8390
8391// Check for and eliminate duplicate PHI nodes in this block.
8392 Changed |=EliminateDuplicatePHINodes(BB);
8393
8394// Check for and remove branches that will always cause undefined behavior.
8395if (removeUndefIntroducingPredecessor(BB, DTU,Options.AC))
8396return requestResimplify();
8397
8398// Merge basic blocks into their predecessor if there is only one distinct
8399// pred, and if there is only one distinct successor of the predecessor, and
8400// if there are no PHI nodes.
8401if (MergeBlockIntoPredecessor(BB, DTU))
8402returntrue;
8403
8404if (SinkCommon &&Options.SinkCommonInsts)
8405if (sinkCommonCodeFromPredecessors(BB, DTU) ||
8406mergeCompatibleInvokes(BB, DTU)) {
8407// sinkCommonCodeFromPredecessors() does not automatically CSE PHI's,
8408// so we may now how duplicate PHI's.
8409// Let's rerun EliminateDuplicatePHINodes() first,
8410// before foldTwoEntryPHINode() potentially converts them into select's,
8411// after which we'd need a whole EarlyCSE pass run to cleanup them.
8412returntrue;
8413 }
8414
8415IRBuilder<> Builder(BB);
8416
8417if (Options.SpeculateBlocks &&
8418 !BB->getParent()->hasFnAttribute(Attribute::OptForFuzzing)) {
8419// If there is a trivial two-entry PHI node in this basic block, and we can
8420// eliminate it, do so now.
8421if (auto *PN = dyn_cast<PHINode>(BB->begin()))
8422if (PN->getNumIncomingValues() == 2)
8423if (foldTwoEntryPHINode(PN,TTI, DTU,Options.AC,DL,
8424Options.SpeculateUnpredictables))
8425returntrue;
8426 }
8427
8428Instruction *Terminator = BB->getTerminator();
8429 Builder.SetInsertPoint(Terminator);
8430switch (Terminator->getOpcode()) {
8431case Instruction::Br:
8432 Changed |= simplifyBranch(cast<BranchInst>(Terminator), Builder);
8433break;
8434case Instruction::Resume:
8435 Changed |= simplifyResume(cast<ResumeInst>(Terminator), Builder);
8436break;
8437case Instruction::CleanupRet:
8438 Changed |= simplifyCleanupReturn(cast<CleanupReturnInst>(Terminator));
8439break;
8440case Instruction::Switch:
8441 Changed |= simplifySwitch(cast<SwitchInst>(Terminator), Builder);
8442break;
8443case Instruction::Unreachable:
8444 Changed |= simplifyUnreachable(cast<UnreachableInst>(Terminator));
8445break;
8446case Instruction::IndirectBr:
8447 Changed |= simplifyIndirectBr(cast<IndirectBrInst>(Terminator));
8448break;
8449 }
8450
8451return Changed;
8452}
8453
8454bool SimplifyCFGOpt::run(BasicBlock *BB) {
8455bool Changed =false;
8456
8457// Repeated simplify BB as long as resimplification is requested.
8458do {
8459 Resimplify =false;
8460
8461// Perform one round of simplifcation. Resimplify flag will be set if
8462// another iteration is requested.
8463 Changed |= simplifyOnce(BB);
8464 }while (Resimplify);
8465
8466return Changed;
8467}
8468
8469boolllvm::simplifyCFG(BasicBlock *BB,constTargetTransformInfo &TTI,
8470DomTreeUpdater *DTU,constSimplifyCFGOptions &Options,
8471ArrayRef<WeakVH> LoopHeaders) {
8472return SimplifyCFGOpt(TTI, DTU, BB->getDataLayout(), LoopHeaders,
8473Options)
8474 .run(BB);
8475}
Fail
#define Fail
Definition:AArch64Disassembler.cpp:221
Success
#define Success
Definition:AArch64Disassembler.cpp:220
const
aarch64 promote const
Definition:AArch64PromoteConstant.cpp:230
Select
AMDGPU Register Bank Select
Definition:AMDGPURegBankSelect.cpp:71
PHI
Rewrite undef for PHI
Definition:AMDGPURewriteUndefForPHI.cpp:100
APInt.h
This file implements a class to represent arbitrary precision integral constant values and operations...
OtherSucc
static MachineBasicBlock * OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ)
Definition:ARMISelLowering.cpp:11379
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition:ARMSLSHardening.cpp:73
IT
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::values(clEnumValN(DefaultIT, "arm-default-it", "Generate any type of IT block"), clEnumValN(RestrictedIT, "arm-restrict-it", "Disallow complex IT blocks")))
Results
Function Alias Analysis Results
Definition:AliasAnalysis.cpp:731
GuardUtils.h
ArrayRef.h
AssumptionCache.h
Attributes.h
This file contains the simple types necessary to represent the attributes associated with functions a...
getParent
static const Function * getParent(const Value *V)
Definition:BasicAliasAnalysis.cpp:863
BasicBlockUtils.h
From
BlockVerifier::State From
Definition:BlockVerifier.cpp:57
BranchProbability.h
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
A
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
CaptureTracking.h
Casting.h
CommandLine.h
ConstantFolding.h
ConstantRange.h
Constants.h
This file contains the declarations for the subclasses of Constant, which represent the different fla...
CostKind
static cl::opt< TargetTransformInfo::TargetCostKind > CostKind("cost-kind", cl::desc("Target cost kind"), cl::init(TargetTransformInfo::TCK_RecipThroughput), cl::values(clEnumValN(TargetTransformInfo::TCK_RecipThroughput, "throughput", "Reciprocal throughput"), clEnumValN(TargetTransformInfo::TCK_Latency, "latency", "Instruction latency"), clEnumValN(TargetTransformInfo::TCK_CodeSize, "code-size", "Code size"), clEnumValN(TargetTransformInfo::TCK_SizeAndLatency, "size-latency", "Code size and latency")))
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.
DerivedTypes.h
DomTreeUpdater.h
Name
std::string Name
Definition:ELFObjHandler.cpp:77
Size
uint64_t Size
Definition:ELFObjHandler.cpp:81
Other
std::optional< std::vector< StOtherPiece > > Other
Definition:ELFYAML.cpp:1315
End
bool End
Definition:ELF_riscv.cpp:480
Blocks
DenseMap< Block *, BlockRelaxAux > Blocks
Definition:ELF_riscv.cpp:507
GlobalValue.h
GlobalVariable.h
GEP
Hexagon Common GEP
Definition:HexagonCommonGEP.cpp:170
pred
hexagon gen pred
Definition:HexagonGenPredicate.cpp:134
IRBuilder.h
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.
Operator.h
Type.h
Use.h
This defines the Use class.
User.h
Value.h
InstrTypes.h
getFalse
static Constant * getFalse(Type *Ty)
For a boolean type or a vector of boolean type, return false or a vector with every element false.
Definition:InstructionSimplify.cpp:86
InstructionSimplify.h
Instructions.h
KnownBits.h
LLVMContext.h
Options
static LVOptions Options
Definition:LVOptions.cpp:25
Loads.h
I
#define I(x, y, z)
Definition:MD5.cpp:58
MDBuilder.h
MapVector.h
This file implements a map that provides insertion order iteration.
MathExtras.h
MemoryModelRelaxationAnnotations.h
This file provides utility for Memory Model Relaxation Annotations (MMRAs).
MemorySSAUpdater.h
MemorySSA.h
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
Metadata.h
This file contains the declarations for metadata subclasses.
Range
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
II
uint64_t IntrinsicInst * II
Definition:NVVMIntrRange.cpp:51
NoFolder.h
P
#define P(N)
Mod
if(auto Err=PB.parsePassPipeline(MPM, Passes)) return wrap(std MPM run * Mod
Definition:PassBuilderBindings.cpp:95
PatternMatch.h
ProfDataUtils.h
This file contains the declarations for profiling metadata utility functions.
Cond
const SmallVectorImpl< MachineOperand > & Cond
Definition:RISCVRedundantCopyElimination.cpp:75
isValid
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
Definition:RustDemangle.cpp:181
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Address
@ Address
Definition:SPIRVEmitNonSemanticDI.cpp:68
STLExtras.h
This file contains some templates that are useful if you are working with the STL at all.
contains
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition:Value.cpp:469
Sequence.h
Provides some synthesis utilities to produce sequences of values.
SetOperations.h
This file defines generic set operations that may be used on set's of different types,...
SetVector.h
This file implements a set that has insertion order iteration characteristics.
HoistLoadsStoresWithCondFaulting
static cl::opt< bool > HoistLoadsStoresWithCondFaulting("simplifycfg-hoist-loads-stores-with-cond-faulting", cl::Hidden, cl::init(true), cl::desc("Hoist loads/stores if the target supports " "conditional faulting"))
addPredecessorToBlock
static void addPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred, BasicBlock *ExistPred, MemorySSAUpdater *MSSAU=nullptr)
Update PHI nodes in Succ to indicate that there will now be entries in it from the 'NewPred' block.
Definition:SimplifyCFG.cpp:388
validLookupTableConstant
static bool validLookupTableConstant(Constant *C, const TargetTransformInfo &TTI)
Return true if the backend will be able to handle initializing an array of constants like C.
Definition:SimplifyCFG.cpp:6093
findUniqueStoreInBlocks
static StoreInst * findUniqueStoreInBlocks(BasicBlock *BB1, BasicBlock *BB2)
Definition:SimplifyCFG.cpp:4260
isProfitableToSpeculate
static bool isProfitableToSpeculate(const BranchInst *BI, std::optional< bool > Invert, const TargetTransformInfo &TTI)
Definition:SimplifyCFG.cpp:3194
validateAndCostRequiredSelects
static bool validateAndCostRequiredSelects(BasicBlock *BB, BasicBlock *ThenBB, BasicBlock *EndBB, unsigned &SpeculatedInstructions, InstructionCost &Cost, const TargetTransformInfo &TTI)
Estimate the cost of the insertion(s) and check that the PHI nodes can be converted to selects.
Definition:SimplifyCFG.cpp:3141
SinkCommon
static cl::opt< bool > SinkCommon("simplifycfg-sink-common", cl::Hidden, cl::init(true), cl::desc("Sink common instructions down to the end block"))
removeSwitchAfterSelectFold
static void removeSwitchAfterSelectFold(SwitchInst *SI, PHINode *PHI, Value *SelectValue, IRBuilder<> &Builder, DomTreeUpdater *DTU)
Definition:SimplifyCFG.cpp:6384
valuesOverlap
static bool valuesOverlap(std::vector< ValueEqualityComparisonCase > &C1, std::vector< ValueEqualityComparisonCase > &C2)
Return true if there are any keys in C1 that exist in C2 as well.
Definition:SimplifyCFG.cpp:840
mergeConditionalStoreToAddress
static bool mergeConditionalStoreToAddress(BasicBlock *PTB, BasicBlock *PFB, BasicBlock *QTB, BasicBlock *QFB, BasicBlock *PostBB, Value *Address, bool InvertPCond, bool InvertQCond, DomTreeUpdater *DTU, const DataLayout &DL, const TargetTransformInfo &TTI)
Definition:SimplifyCFG.cpp:4327
MaxSpeculationDepth
static cl::opt< unsigned > MaxSpeculationDepth("max-speculation-depth", cl::Hidden, cl::init(10), cl::desc("Limit maximum recursion depth when calculating costs of " "speculatively executed instructions"))
shouldFoldCondBranchesToCommonDestination
static std::optional< std::tuple< BasicBlock *, Instruction::BinaryOps, bool > > shouldFoldCondBranchesToCommonDestination(BranchInst *BI, BranchInst *PBI, const TargetTransformInfo *TTI)
Determine if the two branches share a common destination and deduce a glue that joins the branches' c...
Definition:SimplifyCFG.cpp:3971
mergeCleanupPad
static bool mergeCleanupPad(CleanupReturnInst *RI)
Definition:SimplifyCFG.cpp:5504
isVectorOp
static bool isVectorOp(Instruction &I)
Return if an instruction's type or any of its operands' types are a vector type.
Definition:SimplifyCFG.cpp:4124
MaxSwitchCasesPerResult
static cl::opt< unsigned > MaxSwitchCasesPerResult("max-switch-cases-per-result", cl::Hidden, cl::init(16), cl::desc("Limit cases to analyze when converting a switch to select"))
allPredecessorsComeFromSameSource
static BasicBlock * allPredecessorsComeFromSameSource(BasicBlock *BB)
Definition:SimplifyCFG.cpp:7923
cloneInstructionsIntoPredecessorBlockAndUpdateSSAUses
static void cloneInstructionsIntoPredecessorBlockAndUpdateSSAUses(BasicBlock *BB, BasicBlock *PredBlock, ValueToValueMapTy &VMap)
Definition:SimplifyCFG.cpp:1106
constantIntSortPredicate
static int constantIntSortPredicate(ConstantInt *const *P1, ConstantInt *const *P2)
Definition:SimplifyCFG.cpp:1067
getCaseResults
static bool getCaseResults(SwitchInst *SI, ConstantInt *CaseVal, BasicBlock *CaseDest, BasicBlock **CommonDest, SmallVectorImpl< std::pair< PHINode *, Constant * > > &Res, const DataLayout &DL, const TargetTransformInfo &TTI)
Try to determine the resulting constant values in phi nodes at the common destination basic block,...
Definition:SimplifyCFG.cpp:6162
performBranchToCommonDestFolding
static bool performBranchToCommonDestFolding(BranchInst *BI, BranchInst *PBI, DomTreeUpdater *DTU, MemorySSAUpdater *MSSAU, const TargetTransformInfo *TTI)
Definition:SimplifyCFG.cpp:4010
passingValueIsAlwaysUndefined
static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I, bool PtrValueMayBeModified=false)
Check if passing a value to an instruction will cause undefined behavior.
Definition:SimplifyCFG.cpp:8167
isSafeToHoistInstr
static bool isSafeToHoistInstr(Instruction *I, unsigned Flags)
Definition:SimplifyCFG.cpp:1473
isSafeToHoistInvoke
static bool isSafeToHoistInvoke(BasicBlock *BB1, BasicBlock *BB2, Instruction *I1, Instruction *I2)
Definition:SimplifyCFG.cpp:1435
getConstantInt
static ConstantInt * getConstantInt(Value *V, const DataLayout &DL)
Extract ConstantInt from value, looking through IntToPtr and PointerNullValue.
Definition:SimplifyCFG.cpp:492
MergeCondStoresAggressively
static cl::opt< bool > MergeCondStoresAggressively("simplifycfg-merge-cond-stores-aggressively", cl::Hidden, cl::init(false), cl::desc("When merging conditional stores, do so even if the resultant " "basic blocks are unlikely to be if-converted as a result"))
simplifySwitchOfCmpIntrinsic
static bool simplifySwitchOfCmpIntrinsic(SwitchInst *SI, IRBuilderBase &Builder, DomTreeUpdater *DTU)
Fold switch over ucmp/scmp intrinsic to br if two of the switch arms have the same destination.
Definition:SimplifyCFG.cpp:7381
foldCondBranchOnValueKnownInPredecessor
static bool foldCondBranchOnValueKnownInPredecessor(BranchInst *BI, DomTreeUpdater *DTU, const DataLayout &DL, AssumptionCache *AC)
Definition:SimplifyCFG.cpp:3721
extractPredSuccWeights
static bool extractPredSuccWeights(BranchInst *PBI, BranchInst *BI, uint64_t &PredTrueWeight, uint64_t &PredFalseWeight, uint64_t &SuccTrueWeight, uint64_t &SuccFalseWeight)
Return true if either PBI or BI has branch weight available, and store the weights in {Pred|Succ}{Tru...
Definition:SimplifyCFG.cpp:3947
TwoEntryPHINodeFoldingThreshold
static cl::opt< unsigned > TwoEntryPHINodeFoldingThreshold("two-entry-phi-node-folding-threshold", cl::Hidden, cl::init(4), cl::desc("Control the maximal total instruction cost that we are willing " "to speculatively execute to fold a 2-entry PHI node into a " "select (default = 4)"))
constantFold
static Constant * constantFold(Instruction *I, const DataLayout &DL, const SmallDenseMap< Value *, Constant * > &ConstantPool)
Try to fold instruction I into a constant.
Definition:SimplifyCFG.cpp:6133
SimplifyCondBranchToCondBranch
static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI, DomTreeUpdater *DTU, const DataLayout &DL, const TargetTransformInfo &TTI)
If we have a conditional branch as a predecessor of another block, this function tries to simplify it...
Definition:SimplifyCFG.cpp:4644
tryToMergeLandingPad
static bool tryToMergeLandingPad(LandingPadInst *LPad, BranchInst *BI, BasicBlock *BB, DomTreeUpdater *DTU)
Given an block with only a single landing pad and a unconditional branch try to find another basic bl...
Definition:SimplifyCFG.cpp:7805
SpeculateOneExpensiveInst
static cl::opt< bool > SpeculateOneExpensiveInst("speculate-one-expensive-inst", cl::Hidden, cl::init(true), cl::desc("Allow exactly one expensive instruction to be speculatively " "executed"))
areIdenticalUpToCommutativity
static bool areIdenticalUpToCommutativity(const Instruction *I1, const Instruction *I2)
Definition:SimplifyCFG.cpp:1597
MaxSmallBlockSize
static cl::opt< int > MaxSmallBlockSize("simplifycfg-max-small-block-size", cl::Hidden, cl::init(10), cl::desc("Max size of a block which is still considered " "small enough to thread through"))
forwardSwitchConditionToPHI
static bool forwardSwitchConditionToPHI(SwitchInst *SI)
Try to forward the condition of a switch instruction to a phi node dominated by the switch,...
Definition:SimplifyCFG.cpp:6035
findPHIForConditionForwarding
static PHINode * findPHIForConditionForwarding(ConstantInt *CaseValue, BasicBlock *BB, int *PhiIndex)
If BB would be eligible for simplification by TryToSimplifyUncondBranchFromEmptyBlock (i....
Definition:SimplifyCFG.cpp:6004
switchToLookupTable
static bool switchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder, DomTreeUpdater *DTU, const DataLayout &DL, const TargetTransformInfo &TTI)
If the switch is only used to initialize one or more phi nodes in a common successor block with diffe...
Definition:SimplifyCFG.cpp:6911
setBranchWeights
static void setBranchWeights(SwitchInst *SI, ArrayRef< uint32_t > Weights, bool IsExpected)
Definition:SimplifyCFG.cpp:875
foldSwitchToSelect
static Value * foldSwitchToSelect(const SwitchCaseResultVectorTy &ResultVector, Constant *DefaultResult, Value *Condition, IRBuilder<> &Builder)
Definition:SimplifyCFG.cpp:6308
isCleanupBlockEmpty
static bool isCleanupBlockEmpty(iterator_range< BasicBlock::iterator > R)
Definition:SimplifyCFG.cpp:5274
ensureValueAvailableInSuccessor
static Value * ensureValueAvailableInSuccessor(Value *V, BasicBlock *BB, Value *AlternativeV=nullptr)
Definition:SimplifyCFG.cpp:4277
shouldBuildLookupTable
static bool shouldBuildLookupTable(SwitchInst *SI, uint64_t TableSize, const TargetTransformInfo &TTI, const DataLayout &DL, const SmallDenseMap< PHINode *, Type * > &ResultTypes)
Determine whether a lookup table should be built for this switch, based on the number of cases,...
Definition:SimplifyCFG.cpp:6772
createLogicalOp
static Value * createLogicalOp(IRBuilderBase &Builder, Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="")
Definition:SimplifyCFG.cpp:3931
shouldHoistCommonInstructions
static bool shouldHoistCommonInstructions(Instruction *I1, Instruction *I2, const TargetTransformInfo &TTI)
Helper function for hoistCommonCodeFromSuccessors.
Definition:SimplifyCFG.cpp:1511
reduceSwitchRange
static bool reduceSwitchRange(SwitchInst *SI, IRBuilder<> &Builder, const DataLayout &DL, const TargetTransformInfo &TTI)
Try to transform a switch that has "holes" in it to a contiguous sequence of cases.
Definition:SimplifyCFG.cpp:7226
mergeConditionalStores
static bool mergeConditionalStores(BranchInst *PBI, BranchInst *QBI, DomTreeUpdater *DTU, const DataLayout &DL, const TargetTransformInfo &TTI)
Definition:SimplifyCFG.cpp:4475
safeToMergeTerminators
static bool safeToMergeTerminators(Instruction *SI1, Instruction *SI2, SmallSetVector< BasicBlock *, 4 > *FailBlocks=nullptr)
Return true if it is safe to merge these two terminator instructions together.
Definition:SimplifyCFG.cpp:356
SkipFlags
SkipFlags
Definition:SimplifyCFG.cpp:1452
SkipReadMem
@ SkipReadMem
Definition:SimplifyCFG.cpp:1453
SkipSideEffect
@ SkipSideEffect
Definition:SimplifyCFG.cpp:1454
SkipImplicitControlFlow
@ SkipImplicitControlFlow
Definition:SimplifyCFG.cpp:1455
EnableMergeCompatibleInvokes
static cl::opt< bool > EnableMergeCompatibleInvokes("simplifycfg-merge-compatible-invokes", cl::Hidden, cl::init(true), cl::desc("Allow SimplifyCFG to merge invokes together when appropriate"))
incomingValuesAreCompatible
static bool incomingValuesAreCompatible(BasicBlock *BB, ArrayRef< BasicBlock * > IncomingBlocks, SmallPtrSetImpl< Value * > *EquivalenceSet=nullptr)
Return true if all the PHI nodes in the basic block BB receive compatible (identical) incoming values...
Definition:SimplifyCFG.cpp:332
trySwitchToSelect
static bool trySwitchToSelect(SwitchInst *SI, IRBuilder<> &Builder, DomTreeUpdater *DTU, const DataLayout &DL, const TargetTransformInfo &TTI)
If a switch is only used to initialize one or more phi nodes in a common successor block with only tw...
Definition:SimplifyCFG.cpp:6421
BranchFoldThreshold
static cl::opt< unsigned > BranchFoldThreshold("simplifycfg-branch-fold-threshold", cl::Hidden, cl::init(2), cl::desc("Maximum cost of combining conditions when " "folding branches"))
createUnreachableSwitchDefault
static void createUnreachableSwitchDefault(SwitchInst *Switch, DomTreeUpdater *DTU, bool RemoveOrigDefaultBlock=true)
Definition:SimplifyCFG.cpp:5747
fitWeights
static void fitWeights(MutableArrayRef< uint64_t > Weights)
Keep halving the weights until all can fit in uint32_t.
Definition:SimplifyCFG.cpp:1097
isSwitchDense
static bool isSwitchDense(uint64_t NumCases, uint64_t CaseRange)
Definition:SimplifyCFG.cpp:6745
sinkCommonCodeFromPredecessors
static bool sinkCommonCodeFromPredecessors(BasicBlock *BB, DomTreeUpdater *DTU)
Check whether BB's predecessors end with unconditional branches.
Definition:SimplifyCFG.cpp:2442
casesAreContiguous
static bool casesAreContiguous(SmallVectorImpl< ConstantInt * > &Cases)
Definition:SimplifyCFG.cpp:5736
isTypeLegalForLookupTable
static bool isTypeLegalForLookupTable(Type *Ty, const TargetTransformInfo &TTI, const DataLayout &DL)
Definition:SimplifyCFG.cpp:6724
eliminateDeadSwitchCases
static bool eliminateDeadSwitchCases(SwitchInst *SI, DomTreeUpdater *DTU, AssumptionCache *AC, const DataLayout &DL)
Compute masked bits for the condition of a switch and use it to remove dead cases.
Definition:SimplifyCFG.cpp:5902
isSafeToSpeculateStore
static Value * isSafeToSpeculateStore(Instruction *I, BasicBlock *BrBB, BasicBlock *StoreBB, BasicBlock *EndBB)
Determine if we can hoist sink a sole store instruction out of a conditional block.
Definition:SimplifyCFG.cpp:3079
HoistCommon
static cl::opt< bool > HoistCommon("simplifycfg-hoist-common", cl::Hidden, cl::init(true), cl::desc("Hoist common instructions up to the parent block"))
foldTwoEntryPHINode
static bool foldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, DomTreeUpdater *DTU, AssumptionCache *AC, const DataLayout &DL, bool SpeculateUnpredictables)
Given a BB that starts with the specified two-entry PHI node, see if we can eliminate it.
Definition:SimplifyCFG.cpp:3737
initializeUniqueCases
static bool initializeUniqueCases(SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest, SwitchCaseResultVectorTy &UniqueResults, Constant *&DefaultResult, const DataLayout &DL, const TargetTransformInfo &TTI, uintptr_t MaxUniqueResults)
Definition:SimplifyCFG.cpp:6251
HoistCondStores
static cl::opt< bool > HoistCondStores("simplifycfg-hoist-cond-stores", cl::Hidden, cl::init(true), cl::desc("Hoist conditional stores if an unconditional store precedes"))
computeSpeculationCost
static InstructionCost computeSpeculationCost(const User *I, const TargetTransformInfo &TTI)
Compute an abstract "cost" of speculating the given instruction, which is assumed to be safe to specu...
Definition:SimplifyCFG.cpp:402
shouldUseSwitchConditionAsTableIndex
static bool shouldUseSwitchConditionAsTableIndex(ConstantInt &MinCaseVal, const ConstantInt &MaxCaseVal, bool HasDefaultResults, const SmallDenseMap< PHINode *, Type * > &ResultTypes, const DataLayout &DL, const TargetTransformInfo &TTI)
Definition:SimplifyCFG.cpp:6809
skippedInstrFlags
static unsigned skippedInstrFlags(Instruction *I)
Definition:SimplifyCFG.cpp:1458
mergeCompatibleInvokes
static bool mergeCompatibleInvokes(BasicBlock *BB, DomTreeUpdater *DTU)
If this block is a landingpad exception handling block, categorize all the predecessor invokes into s...
Definition:SimplifyCFG.cpp:2996
replacingOperandWithVariableIsCheap
static bool replacingOperandWithVariableIsCheap(const Instruction *I, int OpIdx)
Definition:SimplifyCFG.cpp:2206
eraseTerminatorAndDCECond
static void eraseTerminatorAndDCECond(Instruction *TI, MemorySSAUpdater *MSSAU=nullptr)
Definition:SimplifyCFG.cpp:767
eliminateBlockCases
static void eliminateBlockCases(BasicBlock *BB, std::vector< ValueEqualityComparisonCase > &Cases)
Given a vector of bb/value pairs, remove any entries in the list that match the specified block.
Definition:SimplifyCFG.cpp:834
hoistConditionalLoadsStores
static void hoistConditionalLoadsStores(BranchInst *BI, SmallVectorImpl< Instruction * > &SpeculatedConditionalLoadsStores, std::optional< bool > Invert)
If the target supports conditional faulting, we look for the following pattern:
Definition:SimplifyCFG.cpp:1671
sinkLastInstruction
static void sinkLastInstruction(ArrayRef< BasicBlock * > Blocks)
Definition:SimplifyCFG.cpp:2345
foldCondBranchOnValueKnownInPredecessorImpl
static std::optional< bool > foldCondBranchOnValueKnownInPredecessorImpl(BranchInst *BI, DomTreeUpdater *DTU, const DataLayout &DL, AssumptionCache *AC)
If we have a conditional branch on something for which we know the constant value in predecessors (e....
Definition:SimplifyCFG.cpp:3564
mapCaseToResult
static size_t mapCaseToResult(ConstantInt *CaseVal, SwitchCaseResultVectorTy &UniqueResults, Constant *Result)
Definition:SimplifyCFG.cpp:6233
mergeCompatibleInvokesImpl
static void mergeCompatibleInvokesImpl(ArrayRef< InvokeInst * > Invokes, DomTreeUpdater *DTU)
Definition:SimplifyCFG.cpp:2844
getBranchWeights
static void getBranchWeights(Instruction *TI, SmallVectorImpl< uint64_t > &Weights)
Get Weights of a given terminator, the default weight is at the front of the vector.
Definition:SimplifyCFG.cpp:1079
reuseTableCompare
static void reuseTableCompare(User *PhiUser, BasicBlock *PhiBlock, BranchInst *RangeCheckBranch, Constant *DefaultValue, const SmallVectorImpl< std::pair< ConstantInt *, Constant * > > &Values)
Try to reuse the switch table index compare.
Definition:SimplifyCFG.cpp:6846
tryWidenCondBranchToCondBranch
static bool tryWidenCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI, DomTreeUpdater *DTU)
If the previous block ended with a widenable branch, determine if reusing the target block is profita...
Definition:SimplifyCFG.cpp:4587
mergeNestedCondBranch
static bool mergeNestedCondBranch(BranchInst *BI, DomTreeUpdater *DTU)
Fold the following pattern: bb0: br i1 cond1, label bb1, label bb2 bb1: br i1 cond2,...
Definition:SimplifyCFG.cpp:7954
simplifySwitchOfPowersOfTwo
static bool simplifySwitchOfPowersOfTwo(SwitchInst *SI, IRBuilder<> &Builder, const DataLayout &DL, const TargetTransformInfo &TTI)
Tries to transform switch of powers of two to reduce switch range.
Definition:SimplifyCFG.cpp:7313
lookupConstant
static Constant * lookupConstant(Value *V, const SmallDenseMap< Value *, Constant * > &ConstantPool)
If V is a Constant, return it.
Definition:SimplifyCFG.cpp:6121
MergeCondStores
static cl::opt< bool > MergeCondStores("simplifycfg-merge-cond-stores", cl::Hidden, cl::init(true), cl::desc("Hoist conditional stores even if an unconditional store does not " "precede - hoist multiple conditional stores into a single " "predicated store"))
isLifeTimeMarker
static bool isLifeTimeMarker(const Instruction *I)
Definition:SimplifyCFG.cpp:2191
canSinkInstructions
static bool canSinkInstructions(ArrayRef< Instruction * > Insts, DenseMap< const Use *, SmallVector< Value *, 4 > > &PHIOperands)
Definition:SimplifyCFG.cpp:2219
BranchFoldToCommonDestVectorMultiplier
static cl::opt< unsigned > BranchFoldToCommonDestVectorMultiplier("simplifycfg-branch-fold-common-dest-vector-multiplier", cl::Hidden, cl::init(2), cl::desc("Multiplier to apply to threshold when determining whether or not " "to fold branch to common destination when vector operations are " "present"))
hoistLockstepIdenticalDbgVariableRecords
static void hoistLockstepIdenticalDbgVariableRecords(Instruction *TI, Instruction *I1, SmallVectorImpl< Instruction * > &OtherInsts)
Hoists DbgVariableRecords from I1 and OtherInstrs that are identical in lock-step to TI.
Definition:SimplifyCFG.cpp:1546
HoistCommonSkipLimit
static cl::opt< unsigned > HoistCommonSkipLimit("simplifycfg-hoist-common-skip-limit", cl::Hidden, cl::init(20), cl::desc("Allow reordering across at most this many " "instructions when hoisting"))
removeEmptyCleanup
static bool removeEmptyCleanup(CleanupReturnInst *RI, DomTreeUpdater *DTU)
Definition:SimplifyCFG.cpp:5387
blockIsSimpleEnoughToThreadThrough
static bool blockIsSimpleEnoughToThreadThrough(BasicBlock *BB)
Return true if we can thread a branch across this block.
Definition:SimplifyCFG.cpp:3508
PHINodeFoldingThreshold
static cl::opt< unsigned > PHINodeFoldingThreshold("phi-node-folding-threshold", cl::Hidden, cl::init(2), cl::desc("Control the amount of phi node folding to perform (default = 2)"))
removeUndefIntroducingPredecessor
static bool removeUndefIntroducingPredecessor(BasicBlock *BB, DomTreeUpdater *DTU, AssumptionCache *AC)
If BB has an incoming value that will always trigger undefined behavior (eg.
Definition:SimplifyCFG.cpp:8309
dominatesMergePoint
static bool dominatesMergePoint(Value *V, BasicBlock *BB, Instruction *InsertPt, SmallPtrSetImpl< Instruction * > &AggressiveInsts, InstructionCost &Cost, InstructionCost Budget, const TargetTransformInfo &TTI, AssumptionCache *AC, unsigned Depth=0)
If we have a merge point of an "if condition" as accepted above, return true if the specified value d...
Definition:SimplifyCFG.cpp:424
isSafeCheapLoadStore
static bool isSafeCheapLoadStore(const Instruction *I, const TargetTransformInfo &TTI)
Definition:SimplifyCFG.cpp:1757
getKnownValueOnEdge
static ConstantInt * getKnownValueOnEdge(Value *V, BasicBlock *From, BasicBlock *To)
Definition:SimplifyCFG.cpp:3542
HoistLoadsStoresWithCondFaultingThreshold
static cl::opt< unsigned > HoistLoadsStoresWithCondFaultingThreshold("hoist-loads-stores-with-cond-faulting-threshold", cl::Hidden, cl::init(6), cl::desc("Control the maximal conditional load/store that we are willing " "to speculatively execute to eliminate conditional branch " "(default = 6)"))
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
StringRef.h
Ptr
@ Ptr
Definition:TargetLibraryInfo.cpp:77
TargetTransformInfo.h
This pass exposes codegen information to IR-level passes.
Local.h
ValueHandle.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
ArrayType
Definition:ItaniumDemangle.h:785
Node
Definition:ItaniumDemangle.h:163
T
llvm::APInt
Class for arbitrary precision integers.
Definition:APInt.h:78
llvm::APInt::zext
APInt zext(unsigned width) const
Zero extend to a new width.
Definition:APInt.cpp:986
llvm::APInt::popcount
unsigned popcount() const
Count the number of bits set.
Definition:APInt.h:1649
llvm::APInt::sgt
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition:APInt.h:1201
llvm::APInt::intersects
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition:APInt.h:1249
llvm::APInt::sle
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition:APInt.h:1166
llvm::APInt::getSignificantBits
unsigned getSignificantBits() const
Get the minimum bit size for this signed APInt.
Definition:APInt.h:1511
llvm::APInt::isStrictlyPositive
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition:APInt.h:356
llvm::APInt::getLimitedValue
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
Definition:APInt.h:475
llvm::APInt::isSubsetOf
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition:APInt.h:1257
llvm::APInt::slt
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition:APInt.h:1130
llvm::APInt::getZero
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition:APInt.h:200
llvm::APInt::trySExtValue
std::optional< int64_t > trySExtValue() const
Get sign extended value if possible.
Definition:APInt.h:1554
llvm::APInt::ssub_ov
APInt ssub_ov(const APInt &RHS, bool &Overflow) const
Definition:APInt.cpp:1915
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition:ArrayRef.h:41
llvm::ArrayRef::back
const T & back() const
back - Get the last element.
Definition:ArrayRef.h:177
llvm::ArrayRef::front
const T & front() const
front - Get the first element.
Definition:ArrayRef.h:171
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition:ArrayRef.h:168
llvm::AssumptionCache
A cache of @llvm.assume calls within a function.
Definition:AssumptionCache.h:42
llvm::AssumptionCache::registerAssumption
void registerAssumption(AssumeInst *CI)
Add an @llvm.assume intrinsic to this function's cache.
Definition:AssumptionCache.cpp:185
llvm::Attribute::getValueAsBool
bool getValueAsBool() const
Return the attribute's value as a boolean.
Definition:Attributes.cpp:378
llvm::BasicBlock
LLVM Basic Block Representation.
Definition:BasicBlock.h:61
llvm::BasicBlock::end
iterator end()
Definition:BasicBlock.h:464
llvm::BasicBlock::begin
iterator begin()
Instruction iterator methods.
Definition:BasicBlock.h:451
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:520
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:426
llvm::BasicBlock::instructionsWithoutDebug
iterator_range< filter_iterator< BasicBlock::const_iterator, std::function< bool(const Instruction &)> > > instructionsWithoutDebug(bool SkipPseudoOp=true) const
Return a const iterator range over the instructions in the block, skipping any debug instructions.
Definition:BasicBlock.cpp:250
llvm::BasicBlock::hasAddressTaken
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches,...
Definition:BasicBlock.h:661
llvm::BasicBlock::getFirstNonPHIIt
InstListType::const_iterator getFirstNonPHIIt() const
Iterator returning form of getFirstNonPHI.
Definition:BasicBlock.cpp:374
llvm::BasicBlock::front
const Instruction & front() const
Definition:BasicBlock.h:474
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::getFirstNonPHIOrDbg
InstListType::const_iterator getFirstNonPHIOrDbg(bool SkipPseudoOp=true) const
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic,...
Definition:BasicBlock.cpp:387
llvm::BasicBlock::hasNPredecessors
bool hasNPredecessors(unsigned N) const
Return true if this block has exactly N predecessors.
Definition:BasicBlock.cpp:493
llvm::BasicBlock::getUniqueSuccessor
const BasicBlock * getUniqueSuccessor() const
Return the successor of this block if it has a unique successor.
Definition:BasicBlock.cpp:509
llvm::BasicBlock::getSinglePredecessor
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition:BasicBlock.cpp:471
llvm::BasicBlock::getTerminatingDeoptimizeCall
const CallInst * getTerminatingDeoptimizeCall() const
Returns the call instruction calling @llvm.experimental.deoptimize prior to the terminating return in...
Definition:BasicBlock.cpp:331
llvm::BasicBlock::getUniquePredecessor
const BasicBlock * getUniquePredecessor() const
Return the predecessor of this block if it has a unique predecessor block.
Definition:BasicBlock.cpp:479
llvm::BasicBlock::getSingleSuccessor
const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
Definition:BasicBlock.cpp:501
llvm::BasicBlock::flushTerminatorDbgRecords
void flushTerminatorDbgRecords()
Eject any debug-info trailing at the end of a block.
Definition:BasicBlock.cpp:729
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::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::IsNewDbgInfoFormat
bool IsNewDbgInfoFormat
Flag recording whether or not this block stores debug-info in the form of intrinsic instructions (fal...
Definition:BasicBlock.h:67
llvm::BasicBlock::size
size_t size() const
Definition:BasicBlock.h:472
llvm::BasicBlock::isLandingPad
bool isLandingPad() const
Return true if this basic block is a landing pad.
Definition:BasicBlock.cpp:689
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::hasNPredecessorsOrMore
bool hasNPredecessorsOrMore(unsigned N) const
Return true if this block has N predecessors or more.
Definition:BasicBlock.cpp:497
llvm::BasicBlock::splice
void splice(BasicBlock::iterator ToIt, BasicBlock *FromBB)
Transfer all instructions from FromBB to this basic block at ToIt.
Definition:BasicBlock.h:634
llvm::BasicBlock::getModule
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
Definition:BasicBlock.cpp:292
llvm::BasicBlock::removePredecessor
void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)
Update PHI nodes in this BasicBlock before removal of predecessor Pred.
Definition:BasicBlock.cpp:528
llvm::BlockAddress
The address of a basic block.
Definition:Constants.h:893
llvm::BlockAddress::getBasicBlock
BasicBlock * getBasicBlock() const
Definition:Constants.h:924
llvm::BranchInst
Conditional or Unconditional Branch instruction.
Definition:Instructions.h:3016
llvm::BranchInst::successors
iterator_range< succ_op_iterator > successors()
Definition:Instructions.h:3121
llvm::BranchInst::setCondition
void setCondition(Value *V)
Definition:Instructions.h:3097
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::setSuccessor
void setSuccessor(unsigned idx, BasicBlock *NewSucc)
Definition:Instructions.h:3109
llvm::BranchInst::getCondition
Value * getCondition() const
Definition:Instructions.h:3092
llvm::BranchProbability
Definition:BranchProbability.h:30
llvm::BranchProbability::getBranchProbability
static BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)
Definition:BranchProbability.cpp:53
llvm::BranchProbability::isUnknown
bool isUnknown() const
Definition:BranchProbability.h:47
llvm::BranchProbability::getCompl
BranchProbability getCompl() const
Definition:BranchProbability.h:69
llvm::CallBase::addRangeRetAttr
void addRangeRetAttr(const ConstantRange &CR)
adds the range attribute to the list of attributes.
Definition:InstrTypes.h:1568
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition:Instructions.h:1479
llvm::CleanupPadInst
Definition:Instructions.h:4221
llvm::CleanupReturnInst
Definition:Instructions.h:4364
llvm::CleanupReturnInst::getCleanupPad
CleanupPadInst * getCleanupPad() const
Convenience accessor.
Definition:Instructions.h:4400
llvm::CleanupReturnInst::getUnwindDest
BasicBlock * getUnwindDest() const
Definition:Instructions.h:4410
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::BAD_ICMP_PREDICATE
@ BAD_ICMP_PREDICATE
Definition:InstrTypes.h:706
llvm::CmpInst::getPredicate
Predicate getPredicate() const
Return the predicate for this instruction.
Definition:InstrTypes.h:763
llvm::ConstantArray::get
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition:Constants.cpp:1312
llvm::ConstantExpr
A constant value that is initialized with an expression using other constant values.
Definition:Constants.h:1108
llvm::ConstantExpr::getNeg
static Constant * getNeg(Constant *C, bool HasNSW=false)
Definition:Constants.cpp:2626
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition:Constants.h:83
llvm::ConstantInt::isNegative
bool isNegative() const
Definition:Constants.h:203
llvm::ConstantInt::getLimitedValue
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
getLimitedValue - If the value is smaller than the specified limit, return it, otherwise return the l...
Definition:Constants.h:258
llvm::ConstantInt::getIntegerType
IntegerType * getIntegerType() const
Variant of the getType() method to always return an IntegerType, which reduces the amount of casting ...
Definition:Constants.h:187
llvm::ConstantInt::getTrue
static ConstantInt * getTrue(LLVMContext &Context)
Definition:Constants.cpp:866
llvm::ConstantInt::getFalse
static ConstantInt * getFalse(LLVMContext &Context)
Definition:Constants.cpp:873
llvm::ConstantInt::getBitWidth
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition:Constants.h:151
llvm::ConstantInt::getZExtValue
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition:Constants.h:157
llvm::ConstantInt::getValue
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition:Constants.h:148
llvm::ConstantPool
Definition:ConstantPools.h:43
llvm::ConstantRange
This class represents a range of values.
Definition:ConstantRange.h:47
llvm::ConstantRange::subtract
ConstantRange subtract(const APInt &CI) const
Subtract the specified constant from the endpoints of this constant range.
Definition:ConstantRange.cpp:549
llvm::ConstantRange::getLower
const APInt & getLower() const
Return the lower value for this range.
Definition:ConstantRange.h:203
llvm::ConstantRange::isEmptySet
bool isEmptySet() const
Return true if this set contains no members.
Definition:ConstantRange.cpp:418
llvm::ConstantRange::isSizeLargerThan
bool isSizeLargerThan(uint64_t MaxSize) const
Compare set size of this range with Value.
Definition:ConstantRange.cpp:449
llvm::ConstantRange::getUpper
const APInt & getUpper() const
Return the upper value for this range.
Definition:ConstantRange.h:206
llvm::ConstantRange::isUpperWrapped
bool isUpperWrapped() const
Return true if the exclusive upper bound wraps around the unsigned domain.
Definition:ConstantRange.cpp:426
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::Constant
This is an important base class in LLVM.
Definition:Constant.h:42
llvm::Constant::getNullValue
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition:Constants.cpp:373
llvm::Constant::isNullValue
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition:Constants.cpp:90
llvm::DILocation
Debug location.
Definition:DebugInfoMetadata.h:1988
llvm::DILocation::getMergedLocations
static DILocation * getMergedLocations(ArrayRef< DILocation * > Locs)
Try to combine the vector of locations passed as input in a single one.
Definition:DebugInfoMetadata.cpp:107
llvm::DILocation::getMergedLocation
static DILocation * getMergedLocation(DILocation *LocA, DILocation *LocB)
When two instructions are combined into a single instruction we also need to combine the original loc...
Definition:DebugInfoMetadata.cpp:121
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::DbgRecord
Base class for non-instruction debug metadata records that have positions within IR.
Definition:DebugProgramInstruction.h:134
llvm::DbgRecord::removeFromParent
void removeFromParent()
Definition:DebugProgramInstruction.cpp:674
llvm::DbgRecord::self_iterator
simple_ilist< DbgRecord >::iterator self_iterator
Definition:DebugProgramInstruction.h:213
llvm::DbgVariableRecord
Record of a variable value-assignment, aka a non instruction representation of the dbg....
Definition:DebugProgramInstruction.h:270
llvm::DbgVariableRecord::isDbgAssign
bool isDbgAssign() const
Definition:DebugProgramInstruction.h:506
llvm::DebugLoc
A debug info location.
Definition:DebugLoc.h:33
llvm::DenseMapBase::find
iterator find(const_arg_type_t< KeyT > Val)
Definition:DenseMap.h:156
llvm::DenseMapBase::size
unsigned size() const
Definition:DenseMap.h:99
llvm::DenseMapBase::count
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition:DenseMap.h:152
llvm::DenseMapBase::end
iterator end()
Definition:DenseMap.h:84
llvm::DenseMapBase::at
const ValueT & at(const_arg_type_t< KeyT > Val) const
at - Return the entry for the specified key, or abort if no such entry exists.
Definition:DenseMap.h:202
llvm::DenseMapBase::insert
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition:DenseMap.h:211
llvm::DenseMapBase::reserve
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition:DenseMap.h:103
llvm::DenseMap
Definition:DenseMap.h:727
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition:DenseSet.h:278
llvm::DomTreeUpdater
Definition:DomTreeUpdater.h:30
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::FixedVectorType::get
static FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition:Type.cpp:791
llvm::Function
Definition:Function.h:63
llvm::Function::getEntryBlock
const BasicBlock & getEntryBlock() const
Definition:Function.h:809
llvm::Function::getFnAttribute
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition:Function.cpp:766
llvm::Function::hasMinSize
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition:Function.h:704
llvm::Function::begin
iterator begin()
Definition:Function.h:853
llvm::Function::size
size_t size() const
Definition:Function.h:858
llvm::Function::hasFnAttribute
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition:Function.cpp:731
llvm::GenericDomTreeUpdater::applyUpdates
void applyUpdates(ArrayRef< UpdateT > Updates)
Submit updates to all available trees.
Definition:GenericDomTreeUpdaterImpl.h:59
llvm::GenericDomTreeUpdater::hasPostDomTree
bool hasPostDomTree() const
Returns true if it holds a PostDomTreeT.
Definition:GenericDomTreeUpdater.h:68
llvm::GetElementPtrInst
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition:Instructions.h:933
llvm::GlobalValue::getParent
Module * getParent()
Get the module that this global value is contained inside of...
Definition:GlobalValue.h:657
llvm::GlobalVariable
Definition:GlobalVariable.h:39
llvm::ICmpInst
This instruction compares its operands according to the predicate given to the constructor.
Definition:Instructions.h:1158
llvm::ICmpInst::getSignedPredicate
Predicate getSignedPredicate() const
For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
Definition:Instructions.h:1238
llvm::ICmpInst::isEquality
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
Definition:Instructions.h:1285
llvm::IRBuilderBase
Common base class shared among various IRBuilders.
Definition:IRBuilder.h:113
llvm::IRBuilderBase::CreateICmpULT
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition:IRBuilder.h:2286
llvm::IRBuilderBase::CreateZExtOrTrunc
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
Definition:IRBuilder.h:2051
llvm::IRBuilderBase::CreateUnreachable
UnreachableInst * CreateUnreachable()
Definition:IRBuilder.h:1306
llvm::IRBuilderBase::CreateSelectFMF
Value * CreateSelectFMF(Value *C, Value *True, Value *False, FMFSource FMFSource, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition:IRBuilder.cpp:1058
llvm::IRBuilderBase::CreateAssumption
CallInst * CreateAssumption(Value *Cond, ArrayRef< OperandBundleDef > OpBundles={})
Create an assume intrinsic call that allows the optimizer to assume that the provided condition will ...
Definition:IRBuilder.cpp:521
llvm::IRBuilderBase::CreateMaskedLoad
CallInst * CreateMaskedLoad(Type *Ty, Value *Ptr, Align Alignment, Value *Mask, Value *PassThru=nullptr, const Twine &Name="")
Create a call to Masked Load intrinsic.
Definition:IRBuilder.cpp:546
llvm::IRBuilderBase::CreateSelect
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition:IRBuilder.cpp:1053
llvm::IRBuilderBase::GetInsertPoint
BasicBlock::iterator GetInsertPoint() const
Definition:IRBuilder.h:194
llvm::IRBuilderBase::CreateFreeze
Value * CreateFreeze(Value *V, const Twine &Name="")
Definition:IRBuilder.h:2574
llvm::IRBuilderBase::CreateLShr
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition:IRBuilder.h:1480
llvm::IRBuilderBase::SetCurrentDebugLocation
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
Definition:IRBuilder.h:239
llvm::IRBuilderBase::CreateInBoundsGEP
Value * CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Definition:IRBuilder.h:1882
llvm::IRBuilderBase::CollectMetadataToCopy
void CollectMetadataToCopy(Instruction *Src, ArrayRef< unsigned > MetadataKinds)
Collect metadata with IDs MetadataKinds from Src which should be added to all created instructions.
Definition:IRBuilder.h:252
llvm::IRBuilderBase::CreateIntrinsic
CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
Definition:IRBuilder.cpp:900
llvm::IRBuilderBase::getInt32
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition:IRBuilder.h:505
llvm::IRBuilderBase::CreateNot
Value * CreateNot(Value *V, const Twine &Name="")
Definition:IRBuilder.h:1757
llvm::IRBuilderBase::CreateSwitch
SwitchInst * CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases=10, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a switch instruction with the specified value, default dest, and with a hint for the number of...
Definition:IRBuilder.h:1187
llvm::IRBuilderBase::CreateICmpEQ
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition:IRBuilder.h:2270
llvm::IRBuilderBase::CreateSub
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition:IRBuilder.h:1387
llvm::IRBuilderBase::CreateBitCast
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition:IRBuilder.h:2152
llvm::IRBuilderBase::CreateCondBr
BranchInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a conditional 'br Cond, TrueDest, FalseDest' instruction.
Definition:IRBuilder.h:1164
llvm::IRBuilderBase::CreateLoad
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
Definition:IRBuilder.h:1798
llvm::IRBuilderBase::CreateZExt
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="", bool IsNonNeg=false)
Definition:IRBuilder.h:2033
llvm::IRBuilderBase::CreateAnd
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition:IRBuilder.h:1518
llvm::IRBuilderBase::CreateStore
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition:IRBuilder.h:1811
llvm::IRBuilderBase::CreateMaskedStore
CallInst * CreateMaskedStore(Value *Val, Value *Ptr, Align Alignment, Value *Mask)
Create a call to Masked Store intrinsic.
Definition:IRBuilder.cpp:566
llvm::IRBuilderBase::CreateAdd
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition:IRBuilder.h:1370
llvm::IRBuilderBase::CreatePtrToInt
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
Definition:IRBuilder.h:2142
llvm::IRBuilderBase::CreateTrunc
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="", bool IsNUW=false, bool IsNSW=false)
Definition:IRBuilder.h:2019
llvm::IRBuilderBase::CreateOr
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition:IRBuilder.h:1540
llvm::IRBuilderBase::CreateBinOp
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition:IRBuilder.h:1671
llvm::IRBuilderBase::CreateBr
BranchInst * CreateBr(BasicBlock *Dest)
Create an unconditional 'br label X' instruction.
Definition:IRBuilder.h:1158
llvm::IRBuilderBase::CreateLogicalAnd
Value * CreateLogicalAnd(Value *Cond1, Value *Cond2, const Twine &Name="")
Definition:IRBuilder.h:1688
llvm::IRBuilderBase::CreateIntCast
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition:IRBuilder.h:2225
llvm::IRBuilderBase::SetInsertPoint
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition:IRBuilder.h:199
llvm::IRBuilderBase::CreateXor
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition:IRBuilder.h:1562
llvm::IRBuilderBase::CreateICmp
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition:IRBuilder.h:2380
llvm::IRBuilderBase::CreateLogicalOr
Value * CreateLogicalOr(Value *Cond1, Value *Cond2, const Twine &Name="")
Definition:IRBuilder.h:1694
llvm::IRBuilderBase::CreateMul
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition:IRBuilder.h:1404
llvm::IRBuilder
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition:IRBuilder.h:2705
llvm::IndirectBrInst
Indirect Branch Instruction.
Definition:Instructions.h:3544
llvm::IndirectBrInst::getDestination
BasicBlock * getDestination(unsigned i)
Return the specified destination.
Definition:Instructions.h:3620
llvm::IndirectBrInst::getAddress
Value * getAddress()
Definition:Instructions.h:3611
llvm::IndirectBrInst::getNumDestinations
unsigned getNumDestinations() const
return the number of possible destinations in this indirectbr instruction.
Definition:Instructions.h:3617
llvm::IndirectBrInst::removeDestination
void removeDestination(unsigned i)
This method removes the specified successor from the indirectbr instruction.
Definition:Instructions.cpp:4229
llvm::InstructionCost
Definition:InstructionCost.h:29
llvm::InstructionCost::isValid
bool isValid() const
Definition:InstructionCost.h:79
llvm::Instruction
Definition:Instruction.h:68
llvm::Instruction::clone
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
Definition:Instruction.cpp:1364
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::getDbgRecordRange
iterator_range< simple_ilist< DbgRecord >::iterator > getDbgRecordRange() const
Return a range over the DbgRecords attached to this instruction.
Definition:Instruction.h:104
llvm::Instruction::getDebugLoc
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition:Instruction.h:492
llvm::Instruction::getModule
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition:Instruction.cpp:68
llvm::Instruction::andIRFlags
void andIRFlags(const Value *V)
Logical 'and' of any supported wrapping, exact, and fast-math flags of V and this instruction.
Definition:Instruction.cpp:704
llvm::Instruction::eraseFromParent
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition:Instruction.cpp:94
llvm::Instruction::user_back
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
Definition:Instruction.h:169
llvm::Instruction::getFunction
const Function * getFunction() const
Return the function this instruction belongs to.
Definition:Instruction.cpp:72
llvm::Instruction::getMetadata
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition:Instruction.h:407
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::mayHaveSideEffects
bool mayHaveSideEffects() const LLVM_READONLY
Return true if the instruction may have side effects.
Definition:Instruction.cpp:1185
llvm::Instruction::isTerminator
bool isTerminator() const
Definition:Instruction.h:294
llvm::Instruction::dropUBImplyingAttrsAndMetadata
void dropUBImplyingAttrsAndMetadata()
Drop any attributes or metadata that can cause immediate undefined behavior.
Definition:Instruction.cpp:547
llvm::Instruction::isUsedOutsideOfBlock
bool isUsedOutsideOfBlock(const BasicBlock *BB) const LLVM_READONLY
Return true if there are any uses of this instruction in blocks other than the specified block.
Definition:Instruction.cpp:973
llvm::Instruction::setMetadata
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition:Metadata.cpp:1679
llvm::Instruction::CompareUsingIntersectedAttrs
@ CompareUsingIntersectedAttrs
Check for equivalence with intersected callbase attrs.
Definition:Instruction.h:923
llvm::Instruction::getAAMetadata
AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
Definition:Metadata.cpp:1750
llvm::Instruction::BinaryOps
BinaryOps
Definition:Instruction.h:989
llvm::Instruction::isIdenticalTo
bool isIdenticalTo(const Instruction *I) const LLVM_READONLY
Return true if the specified instruction is exactly identical to the current one.
Definition:Instruction.cpp:914
llvm::Instruction::applyMergedLocation
void applyMergedLocation(DILocation *LocA, DILocation *LocB)
Merge 2 debug locations and apply it to the Instruction.
Definition:DebugInfo.cpp:949
llvm::Instruction::setDebugLoc
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition:Instruction.h:489
llvm::Instruction::copyMetadata
void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())
Copy metadata from SrcInst to this instruction.
Definition:Instruction.cpp:1345
llvm::Instruction::dropDbgRecords
void dropDbgRecords()
Erase any DbgRecords attached to this instruction.
Definition:Instruction.cpp:325
llvm::Instruction::moveBefore
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
Definition:Instruction.cpp:175
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::IntegerType
Class to represent integer types.
Definition:DerivedTypes.h:42
llvm::IntegerType::get
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition:Type.cpp:311
llvm::IntrinsicCostAttributes
Definition:TargetTransformInfo.h:119
llvm::InvokeInst
Invoke instruction.
Definition:Instructions.h:3670
llvm::InvokeInst::setNormalDest
void setNormalDest(BasicBlock *B)
Definition:Instructions.h:3767
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition:LLVMContext.h:67
llvm::LandingPadInst
The landingpad instruction holds all of the information necessary to generate correct exception handl...
Definition:Instructions.h:2840
llvm::LoadInst
An instruction for reading from memory.
Definition:Instructions.h:176
llvm::LoadInst::getPointerOperandIndex
static unsigned getPointerOperandIndex()
Definition:Instructions.h:257
llvm::MDBuilder
Definition:MDBuilder.h:36
llvm::MDBuilder::createBranchWeights
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight, bool IsExpected=false)
Return metadata containing two branch weights.
Definition:MDBuilder.cpp:37
llvm::MDNode
Metadata node.
Definition:Metadata.h:1073
llvm::MMRAMetadata
Helper class to manipulate !mmra metadata nodes.
Definition:MemoryModelRelaxationAnnotations.h:46
llvm::MapVector::erase
VectorType::iterator erase(typename VectorType::iterator Iterator)
Remove the element given by Iterator.
Definition:MapVector.h:193
llvm::MapVector::empty
bool empty() const
Definition:MapVector.h:79
llvm::MapVector::insert
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition:MapVector.h:141
llvm::MapVector::size
size_type size() const
Definition:MapVector.h:60
llvm::MemorySSAUpdater
Definition:MemorySSAUpdater.h:54
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition:Module.h:65
llvm::MutableArrayRef
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition:ArrayRef.h:310
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::blocks
iterator_range< const_block_iterator > blocks() const
Definition:Instructions.h:2661
llvm::PHINode::incoming_values
op_range incoming_values()
Definition:Instructions.h:2665
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::getBasicBlockIndex
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
Definition:Instructions.h:2768
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::PointerUnion< const Value *, const PseudoSourceValue * >
llvm::PoisonValue::get
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition:Constants.cpp:1878
llvm::PtrToIntInst
This class represents a cast from a pointer to an integer.
Definition:Instructions.h:4851
llvm::ResumeInst
Resume the propagation of an exception.
Definition:Instructions.h:4002
llvm::ResumeInst::getValue
Value * getValue() const
Convenience accessor.
Definition:Instructions.h:4024
llvm::ReturnInst
Return a value (possibly void), from a function.
Definition:Instructions.h:2938
llvm::SelectInst
This class represents the LLVM 'select' instruction.
Definition:Instructions.h:1657
llvm::SetVector::size
size_type size() const
Determine the number of elements in the SetVector.
Definition:SetVector.h:98
llvm::SetVector::empty
bool empty() const
Determine if the SetVector is empty or not.
Definition:SetVector.h:93
llvm::SetVector::insert
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition:SetVector.h:162
llvm::SmallDenseMap
Definition:DenseMap.h:883
llvm::SmallPtrSetImplBase::size
size_type size() const
Definition:SmallPtrSet.h:94
llvm::SmallPtrSetImplBase::empty
bool empty() const
Definition:SmallPtrSet.h:93
llvm::SmallPtrSetImpl
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition:SmallPtrSet.h:363
llvm::SmallPtrSetImpl::erase
bool erase(PtrType Ptr)
Remove pointer from the set.
Definition:SmallPtrSet.h:401
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::SmallPtrSetImpl::contains
bool contains(ConstPtrType Ptr) const
Definition:SmallPtrSet.h:458
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition:SmallPtrSet.h:519
llvm::SmallSetVector
A SetVector that performs no allocations if smaller than a certain size.
Definition:SetVector.h:370
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::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::reserve
void reserve(size_type N)
Definition:SmallVector.h:663
llvm::SmallVectorImpl::erase
iterator erase(const_iterator CI)
Definition:SmallVector.h:737
llvm::SmallVectorImpl::insert
iterator insert(iterator I, T &&Elt)
Definition:SmallVector.h:805
llvm::SmallVectorImpl::clear
void clear()
Definition:SmallVector.h:610
llvm::SmallVectorImpl::resize
void resize(size_type N)
Definition:SmallVector.h:638
llvm::SmallVectorTemplateBase::pop_back
void pop_back()
Definition:SmallVector.h:425
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::front
reference front()
Definition:SmallVector.h:299
llvm::SmallVectorTemplateCommon::begin
iterator begin()
Definition:SmallVector.h:267
llvm::SmallVectorTemplateCommon::back
reference back()
Definition:SmallVector.h:308
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::StoreInst
An instruction for storing to memory.
Definition:Instructions.h:292
llvm::StoreInst::getAlign
Align getAlign() const
Definition:Instructions.h:333
llvm::StoreInst::isSimple
bool isSimple() const
Definition:Instructions.h:370
llvm::StoreInst::getValueOperand
Value * getValueOperand()
Definition:Instructions.h:378
llvm::StoreInst::isUnordered
bool isUnordered() const
Definition:Instructions.h:372
llvm::StoreInst::getPointerOperandIndex
static unsigned getPointerOperandIndex()
Definition:Instructions.h:383
llvm::StoreInst::getPointerOperand
Value * getPointerOperand()
Definition:Instructions.h:381
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::SwitchInstProfUpdateWrapper
A wrapper class to simplify modification of SwitchInst cases along with their prof branch_weights met...
Definition:Instructions.h:3492
llvm::SwitchInstProfUpdateWrapper::setSuccessorWeight
void setSuccessorWeight(unsigned idx, CaseWeightOpt W)
Definition:Instructions.cpp:4141
llvm::SwitchInstProfUpdateWrapper::addCase
void addCase(ConstantInt *OnVal, BasicBlock *Dest, CaseWeightOpt W)
Delegate the call to the underlying SwitchInst::addCase() and set the specified branch weight for the...
Definition:Instructions.cpp:4107
llvm::SwitchInstProfUpdateWrapper::getSuccessorWeight
CaseWeightOpt getSuccessorWeight(unsigned idx)
Definition:Instructions.cpp:4135
llvm::SwitchInstProfUpdateWrapper::CaseWeightOpt
std::optional< uint32_t > CaseWeightOpt
Definition:Instructions.h:3503
llvm::SwitchInstProfUpdateWrapper::removeCase
SwitchInst::CaseIt removeCase(SwitchInst::CaseIt I)
Delegate the call to the underlying SwitchInst::removeCase() and remove correspondent branch weight.
Definition:Instructions.cpp:4093
llvm::SwitchInst::CaseIteratorImpl
Definition:Instructions.h:3273
llvm::SwitchInst
Multiway switch.
Definition:Instructions.h:3154
llvm::SwitchInst::getSuccessor
BasicBlock * getSuccessor(unsigned idx) const
Definition:Instructions.h:3472
llvm::SwitchInst::addCase
void addCase(ConstantInt *OnVal, BasicBlock *Dest)
Add an entry to the switch instruction.
Definition:Instructions.cpp:4011
llvm::SwitchInst::setSuccessor
void setSuccessor(unsigned idx, BasicBlock *NewSucc)
Definition:Instructions.h:3476
llvm::SwitchInst::getNumSuccessors
unsigned getNumSuccessors() const
Definition:Instructions.h:3471
llvm::TargetTransformInfo
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Definition:TargetTransformInfo.h:212
llvm::TargetTransformInfo::shouldBuildLookupTables
bool shouldBuildLookupTables() const
Return true if switches should be turned into lookup tables for the target.
Definition:TargetTransformInfo.cpp:591
llvm::TargetTransformInfo::getCmpSelInstrCost
InstructionCost getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy, CmpInst::Predicate VecPred, TTI::TargetCostKind CostKind=TTI::TCK_RecipThroughput, OperandValueInfo Op1Info={OK_AnyValue, OP_None}, OperandValueInfo Op2Info={OK_AnyValue, OP_None}, const Instruction *I=nullptr) const
Definition:TargetTransformInfo.cpp:1067
llvm::TargetTransformInfo::shouldBuildLookupTablesForConstant
bool shouldBuildLookupTablesForConstant(Constant *C) const
Return true if switches should be turned into lookup tables containing this constant value for the ta...
Definition:TargetTransformInfo.cpp:595
llvm::TargetTransformInfo::getIntrinsicInstrCost
InstructionCost getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA, TTI::TargetCostKind CostKind) const
Definition:TargetTransformInfo.cpp:1177
llvm::TargetTransformInfo::TargetCostKind
TargetCostKind
The kind of cost model.
Definition:TargetTransformInfo.h:263
llvm::TargetTransformInfo::TCK_CodeSize
@ TCK_CodeSize
Instruction code size.
Definition:TargetTransformInfo.h:266
llvm::TargetTransformInfo::TCK_SizeAndLatency
@ TCK_SizeAndLatency
The weighted sum of size and latency.
Definition:TargetTransformInfo.h:267
llvm::TargetTransformInfo::getArithmeticInstrCost
InstructionCost getArithmeticInstrCost(unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind=TTI::TCK_RecipThroughput, TTI::OperandValueInfo Opd1Info={TTI::OK_AnyValue, TTI::OP_None}, TTI::OperandValueInfo Opd2Info={TTI::OK_AnyValue, TTI::OP_None}, ArrayRef< const Value * > Args={}, const Instruction *CxtI=nullptr, const TargetLibraryInfo *TLibInfo=nullptr) const
This is an approximation of reciprocal throughput of a math/logic op.
Definition:TargetTransformInfo.cpp:940
llvm::TargetTransformInfo::isTypeLegal
bool isTypeLegal(Type *Ty) const
Return true if this type is legal.
Definition:TargetTransformInfo.cpp:583
llvm::TargetTransformInfo::getPredictableBranchThreshold
BranchProbability getPredictableBranchThreshold() const
If a branch or a select condition is skewed in one direction by more than this factor,...
Definition:TargetTransformInfo.cpp:279
llvm::TargetTransformInfo::getBranchMispredictPenalty
InstructionCost getBranchMispredictPenalty() const
Returns estimated penalty of a branch misprediction in latency.
Definition:TargetTransformInfo.cpp:285
llvm::TargetTransformInfo::isProfitableToHoist
bool isProfitableToHoist(Instruction *I) const
Return true if it is profitable to hoist instruction in the then/else to before if.
Definition:TargetTransformInfo.cpp:577
llvm::TargetTransformInfo::TCC_Free
@ TCC_Free
Expected to fold away in lowering.
Definition:TargetTransformInfo.h:289
llvm::TargetTransformInfo::TCC_Basic
@ TCC_Basic
The cost of a typical 'add' instruction.
Definition:TargetTransformInfo.h:290
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::TargetTransformInfo::hasConditionalLoadStoreForType
bool hasConditionalLoadStoreForType(Type *Ty=nullptr) const
Definition:TargetTransformInfo.cpp:763
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition:Twine.h:81
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition:Type.h:45
llvm::Type::getIntegerBitWidth
unsigned getIntegerBitWidth() const
llvm::Type::isPointerTy
bool isPointerTy() const
True if this is an instance of PointerType.
Definition:Type.h:264
llvm::Type::getInt1Ty
static IntegerType * getInt1Ty(LLVMContext &C)
llvm::Type::isIntegerTy
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition:Type.h:237
llvm::Type::isTokenTy
bool isTokenTy() const
Return true if this is 'token'.
Definition:Type.h:234
llvm::Type::getPrimitiveSizeInBits
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
llvm::UnreachableInst
This function has undefined behavior.
Definition:Instructions.h:4461
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition:Use.h:43
llvm::Use::set
void set(Value *Val)
Definition:Value.h:886
llvm::Use::getUser
User * getUser() const
Returns the User that contains this Use.
Definition:Use.h:72
llvm::Use::getOperandNo
unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition:Use.cpp:31
llvm::User
Definition:User.h:44
llvm::User::operands
op_range operands()
Definition:User.h:288
llvm::User::replaceUsesOfWith
bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition:User.cpp:21
llvm::User::getOperandUse
const Use & getOperandUse(unsigned i) const
Definition:User.h:241
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::User::getNumOperands
unsigned getNumOperands() const
Definition:User.h:250
llvm::ValueMap< const Value *, WeakTrackingVH >
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::MaximumAlignment
static constexpr uint64_t MaximumAlignment
Definition:Value.h:811
llvm::Value::Value
Value(Type *Ty, unsigned scid)
Definition:Value.cpp:53
llvm::Value::setName
void setName(const Twine &Name)
Change the name of the value.
Definition:Value.cpp:377
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::users
iterator_range< user_iterator > users()
Definition:Value.h:421
llvm::Value::hasNUses
bool hasNUses(unsigned N) const
Return true if this Value has exactly N uses.
Definition:Value.cpp:149
llvm::Value::use_empty
bool use_empty() const
Definition:Value.h:344
llvm::Value::getContext
LLVMContext & getContext() const
All values hold a context through their type.
Definition:Value.cpp:1075
llvm::Value::uses
iterator_range< use_iterator > uses()
Definition:Value.h:376
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::detail::DenseSetImpl::insert
std::pair< iterator, bool > insert(const ValueT &V)
Definition:DenseSet.h:213
llvm::detail::DenseSetImpl::reserve
void reserve(size_t Size)
Grow the DenseSet so that it can contain at least NumEntries items before resizing again.
Definition:DenseSet.h:90
llvm::detail::DenseSetImpl::size
size_type size() const
Definition:DenseSet.h:81
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::getPrevNode
NodeTy * getPrevNode()
Definition:ilist_node.h:339
llvm::ilist_node_with_parent::getNextNode
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition:ilist_node.h:353
llvm::iterator_range
A range adaptor for a pair of iterators.
Definition:iterator_range.h:42
llvm::mapped_iterator
Definition:STLExtras.h:353
uint32_t
uint64_t
unsigned
UINT64_MAX
#define UINT64_MAX
Definition:DataTypes.h:77
DebugInfo.h
ErrorHandling.h
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition:ErrorHandling.h:143
llvm::AMDGPU::CPol::NT
@ NT
Definition:SIDefines.h:387
llvm::ARM::ProfileKind::M
@ M
llvm::ARM::operator--
ArchKind & operator--(ArchKind &Kind)
Definition:ARMTargetParser.h:210
llvm::BitmaskEnumDetail::Mask
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition:BitmaskEnum.h:125
llvm::CallingConv::C
@ C
The default llvm calling convention, compatible with C.
Definition:CallingConv.h:34
llvm::M68kBeads::Term
@ Term
Definition:M68kBaseInfo.h:116
llvm::M68k::MemAddrModeKind::U
@ U
llvm::M68k::MemAddrModeKind::V
@ V
llvm::MCID::Branch
@ Branch
Definition:MCInstrDesc.h:159
llvm::MCID::Terminator
@ Terminator
Definition:MCInstrDesc.h:158
llvm::PatternMatch::m_And
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1216
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_BinOp
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition:PatternMatch.h:100
llvm::PatternMatch::match
bool match(Val *V, const Pattern &P)
Definition:PatternMatch.h:49
llvm::PatternMatch::m_Specific
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition:PatternMatch.h:885
llvm::PatternMatch::m_AnyIntegralConstant
cst_pred_ty< is_any_apint > m_AnyIntegralConstant()
Match an integer or vector with any integral constant.
Definition:PatternMatch.h:507
llvm::PatternMatch::m_LogicalOr
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
Definition:PatternMatch.h:3099
llvm::PatternMatch::m_ImmConstant
match_combine_and< class_match< Constant >, match_unless< constantexpr_match > > m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
Definition:PatternMatch.h:864
llvm::PatternMatch::m_c_Select
ThreeOps_match< decltype(m_Value()), LHS, RHS, Instruction::Select, true > m_c_Select(const LHS &L, const RHS &R)
Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
Definition:PatternMatch.h:1815
llvm::PatternMatch::m_APInt
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition:PatternMatch.h:299
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_Not
BinaryOp_match< cst_pred_ty< is_all_ones >, ValTy, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
Definition:PatternMatch.h:2467
llvm::PatternMatch::m_Or
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1222
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::SIEncodingFamily::SI
@ SI
Definition:SIDefines.h:36
llvm::X86::FirstMacroFusionInstKind::Cmp
@ Cmp
llvm::at::getAssignmentMarkers
AssignmentMarkerRange getAssignmentMarkers(DIAssignID *ID)
Return a range of dbg.assign intrinsics which use \ID as an operand.
Definition:DebugInfo.cpp:1867
llvm::at::getDVRAssignmentMarkers
SmallVector< DbgVariableRecord * > getDVRAssignmentMarkers(const Instruction *Inst)
Definition:DebugInfo.h:240
llvm::at::deleteAssignmentMarkers
void deleteAssignmentMarkers(const Instruction *Inst)
Delete the llvm.dbg.assign intrinsics linked to Inst.
Definition:DebugInfo.cpp:1881
llvm::cl::Hidden
@ Hidden
Definition:CommandLine.h:137
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition:CommandLine.h:443
llvm::dwarf::Index
Index
Definition:Dwarf.h:882
llvm::dxil::PointerTypeAnalysis::run
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
Definition:PointerTypeAnalysis.cpp:191
llvm::dxil::ElementType::I1
@ I1
llvm::lltok::Kind
Kind
Definition:LLToken.h:18
llvm::logicalview::LVAttributeKind::Inserted
@ Inserted
llvm::ms_demangle::IntrinsicFunctionKind::New
@ New
llvm::ms_demangle::QualifierMangleMode::Result
@ Result
llvm::msgpack::Type::Array
@ Array
llvm::numbers::e
constexpr double e
Definition:MathExtras.h:47
llvm::objcopy::AdjustKind::Set
@ Set
llvm::pdb::PDB_SymType::Callee
@ Callee
llvm::rdf::Phi
NodeAddr< PhiNode * > Phi
Definition:RDFGraph.h:390
llvm::sampleprof::Base
@ Base
Definition:Discriminator.h:58
llvm::tgtok::TrueVal
@ TrueVal
Definition:TGLexer.h:58
llvm::tgtok::FalseVal
@ FalseVal
Definition:TGLexer.h:59
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::drop_begin
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition:STLExtras.h:329
llvm::Offset
@ Offset
Definition:DWP.cpp:480
llvm::zip
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
Definition:STLExtras.h:854
llvm::operator<
bool operator<(int64_t V1, const APSInt &V2)
Definition:APSInt.h:361
llvm::find
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition:STLExtras.h:1759
llvm::for_each
UnaryFunction for_each(R &&Range, UnaryFunction F)
Provide wrappers to std::for_each which take ranges instead of having to pass begin/end explicitly.
Definition:STLExtras.h:1732
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::popcount
int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition:bit.h:385
llvm::RecursivelyDeleteTriviallyDeadInstructions
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
If the specified value is a trivially dead instruction, delete it.
Definition:Local.cpp:546
llvm::succ_empty
bool succ_empty(const Instruction *I)
Definition:CFG.h:255
llvm::IsBlockFollowedByDeoptOrUnreachable
bool IsBlockFollowedByDeoptOrUnreachable(const BasicBlock *BB)
Check if we can prove that all paths starting from this block converge to a block that either has a @...
Definition:BasicBlockUtils.cpp:743
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::GetIfCondition
BranchInst * GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, BasicBlock *&IfFalse)
Check whether BB is the merge point of a if-region.
Definition:BasicBlockUtils.cpp:1804
llvm::Successor
@ Successor
Definition:SIMachineScheduler.h:35
llvm::Depth
@ Depth
Definition:SIMachineScheduler.h:36
llvm::pred_end
auto pred_end(const MachineBasicBlock *BB)
Definition:MachineBasicBlock.h:1385
llvm::set_intersect
void set_intersect(S1Ty &S1, const S2Ty &S2)
set_intersect(A, B) - Compute A := A ^ B Identical to set_intersection, except that it works on set<>...
Definition:SetOperations.h:58
llvm::operator*
APInt operator*(APInt a, uint64_t RHS)
Definition:APInt.h:2204
llvm::successors
auto successors(const MachineBasicBlock *BB)
Definition:MachineBasicBlock.h:1376
llvm::make_range
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Definition:iterator_range.h:77
llvm::getBranchWeightMDNode
MDNode * getBranchWeightMDNode(const Instruction &I)
Get the branch weights metadata node.
Definition:ProfDataUtils.cpp:146
llvm::getUnderlyingObject
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
Definition:ValueTracking.cpp:6768
llvm::RemapDbgRecordRange
void RemapDbgRecordRange(Module *M, iterator_range< DbgRecordIterator > Range, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataSetTy *IdentityMD=nullptr)
Remap the Values used in the DbgRecords Range using the value map VM.
Definition:ValueMapper.h:299
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::make_early_inc_range
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition:STLExtras.h:657
llvm::getLoadStoreAlignment
Align getLoadStoreAlignment(const Value *I)
A helper function that returns the alignment of load or store instruction.
Definition:Instructions.h:5010
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::unique
auto unique(Range &&R, Predicate P)
Definition:STLExtras.h:2055
llvm::copy_if
OutputIt copy_if(R &&Range, OutputIt Out, UnaryPredicate P)
Provide wrappers to std::copy_if which take ranges instead of having to pass begin/end explicitly.
Definition:STLExtras.h:1785
llvm::operator==
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
Definition:AddressRanges.h:153
llvm::getConstantRangeFromMetadata
ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
Definition:ConstantRange.cpp:2264
llvm::computeConstantRange
ConstantRange computeConstantRange(const Value *V, bool ForSigned, bool UseInstrInfo=true, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
Definition:ValueTracking.cpp:10078
llvm::countr_zero
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition:bit.h:215
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::erase
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
Definition:STLExtras.h:2107
llvm::has_single_bit
constexpr bool has_single_bit(T Value) noexcept
Definition:bit.h:146
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::Log2_32
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition:MathExtras.h:341
llvm::countl_zero
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition:bit.h:281
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::isPowerOf2_32
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition:MathExtras.h:292
llvm::InvertBranch
void InvertBranch(BranchInst *PBI, IRBuilderBase &Builder)
Definition:BasicBlockUtils.cpp:1896
llvm::impliesPoison
bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
Definition:ValueTracking.cpp:7690
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition:STLExtras.h:1664
llvm::RF_IgnoreMissingLocals
@ RF_IgnoreMissingLocals
If this flag is set, the remapper ignores missing function-local entries (Argument,...
Definition:ValueMapper.h:96
llvm::RF_NoModuleLevelChanges
@ RF_NoModuleLevelChanges
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
Definition:ValueMapper.h:78
llvm::PointerMayBeCaptured
bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures, bool StoreCaptures, unsigned MaxUsesToExplore=0)
PointerMayBeCaptured - Return true if this pointer value may be captured by the enclosing function (w...
Definition:CaptureTracking.cpp:204
llvm::NullPointerIsDefined
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
Definition:Function.cpp:1187
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition:Debug.cpp:163
llvm::RemapInstruction
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataSetTy *IdentityMD=nullptr)
Convert the instruction operands from referencing the current values into those specified by VM.
Definition:ValueMapper.h:277
llvm::none_of
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition:STLExtras.h:1753
llvm::make_first_range
auto make_first_range(ContainerTy &&c)
Given a container of pairs, return a range over the first elements.
Definition:STLExtras.h:1439
llvm::RemapDbgRecord
void RemapDbgRecord(Module *M, DbgRecord *DR, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataSetTy *IdentityMD=nullptr)
Remap the Values used in the DbgRecord DR using the value map VM.
Definition:ValueMapper.h:288
llvm::removeUnwindEdge
Instruction * removeUnwindEdge(BasicBlock *BB, DomTreeUpdater *DTU=nullptr)
Replace 'BB's terminator with one that does not have an unwind successor block.
Definition:Local.cpp:3236
llvm::succ_size
auto succ_size(const MachineBasicBlock *BB)
Definition:MachineBasicBlock.h:1380
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::to_vector
SmallVector< ValueTypeFromRangeType< R >, Size > to_vector(R &&Range)
Given a range of type R, iterate the entire range and return a SmallVector with elements of the vecto...
Definition:SmallVector.h:1299
llvm::ConstantFoldInstOperands
Constant * ConstantFoldInstOperands(Instruction *I, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands.
Definition:ConstantFolding.cpp:1177
llvm::RequireAndPreserveDomTree
cl::opt< bool > RequireAndPreserveDomTree
This function is used to do simplification of a CFG.
llvm::succ_begin
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
Definition:RegionIterator.h:249
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::drop_end
auto drop_end(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the last N elements excluded.
Definition:STLExtras.h:336
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::isWidenableBranch
bool isWidenableBranch(const User *U)
Returns true iff U is a widenable branch (that is, extractWidenableCondition returns widenable condit...
Definition:GuardUtils.cpp:26
llvm::succ_end
RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)
Definition:RegionIterator.h:254
llvm::MergeBlockIntoPredecessor
bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, MemoryDependenceResults *MemDep=nullptr, bool PredecessorWithTwoSuccessors=false, DominatorTree *DT=nullptr)
Attempts to merge a block into its predecessor, if possible.
Definition:BasicBlockUtils.cpp:180
llvm::hoistAllInstructionsInto
void hoistAllInstructionsInto(BasicBlock *DomBlock, Instruction *InsertPt, BasicBlock *BB)
Hoist all of the instructions in the IfBlock to the dominant block DomBlock, by moving its instructio...
Definition:Local.cpp:3706
llvm::RecurKind::And
@ And
Bitwise or logical AND of integers.
llvm::isIntN
bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition:MathExtras.h:261
llvm::count
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition:STLExtras.h:1938
llvm::computeKnownBits
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
Definition:ValueTracking.cpp:164
llvm::canReplaceOperandWithVariable
bool canReplaceOperandWithVariable(const Instruction *I, unsigned OpIdx)
Given an instruction, is it legal to set operand OpIdx to a non-constant value?
Definition:Local.cpp:4209
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::FoldSingleEntryPHINodes
bool FoldSingleEntryPHINodes(BasicBlock *BB, MemoryDependenceResults *MemDep=nullptr)
We know that BB has one predecessor.
Definition:BasicBlockUtils.cpp:145
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::BitWidth
constexpr unsigned BitWidth
Definition:BitmaskEnum.h:217
llvm::isDereferenceablePointer
bool isDereferenceablePointer(const Value *V, Type *Ty, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if this is always a dereferenceable pointer.
Definition:Loads.cpp:237
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::simplifyCFG
bool simplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI, DomTreeUpdater *DTU=nullptr, const SimplifyCFGOptions &Options={}, ArrayRef< WeakVH > LoopHeaders={})
Definition:SimplifyCFG.cpp:8469
llvm::pred_begin
auto pred_begin(const MachineBasicBlock *BB)
Definition:MachineBasicBlock.h:1383
llvm::SplitBlock
BasicBlock * SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="", bool Before=false)
Split the specified block at the specified instruction.
Definition:BasicBlockUtils.cpp:1084
llvm::find_if
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition:STLExtras.h:1766
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::getLoadStoreType
Type * getLoadStoreType(const Value *I)
A helper function that returns the type of a load or store instruction.
Definition:Instructions.h:5039
llvm::foldBranchToCommonDest
bool foldBranchToCommonDest(BranchInst *BI, llvm::DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr, const TargetTransformInfo *TTI=nullptr, unsigned BonusInstThreshold=1)
If this basic block is ONLY a setcc and a branch, and if a predecessor branches to us and one of our ...
Definition:SimplifyCFG.cpp:4133
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::isImpliedByDomCondition
std::optional< bool > isImpliedByDomCondition(const Value *Cond, const Instruction *ContextI, const DataLayout &DL)
Return the boolean condition value in the context of the given instruction if it is known based on do...
Definition:ValueTracking.cpp:9680
llvm::seq
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition:Sequence.h:305
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::hasBranchWeightMD
bool hasBranchWeightMD(const Instruction &I)
Checks if an instructions has Branch Weight Metadata.
Definition:ProfDataUtils.cpp:103
llvm::hash_combine
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition:Hashing.h:590
llvm::equal
bool equal(L &&LRange, R &&RRange)
Wrapper function around std::equal to detect if pair-wise elements between two ranges are the same.
Definition:STLExtras.h:2067
llvm::ConstantFoldIntegerCast
Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
Definition:ConstantFolding.cpp:1549
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::ComputeMaxSignificantBits
unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Get the upper bound on bit size for this Value Op as a signed integer.
Definition:ValueTracking.cpp:359
llvm::EliminateDuplicatePHINodes
bool EliminateDuplicatePHINodes(BasicBlock *BB)
Check for and eliminate duplicate PHI nodes in this block.
Definition:Local.cpp:1524
llvm::hash_combine_range
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition:Hashing.h:468
llvm::isWritableObject
bool isWritableObject(const Value *Object, bool &ExplicitlyDereferenceableOnly)
Return true if the Object is writable, in the sense that any location based on this pointer that can ...
Definition:AliasAnalysis.cpp:889
llvm::NextPowerOf2
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition:MathExtras.h:383
llvm::extractFromBranchWeightMD64
void extractFromBranchWeightMD64(const MDNode *ProfileData, SmallVectorImpl< uint64_t > &Weights)
Faster version of extractBranchWeights() that skips checks and must only be called with "branch_weigh...
Definition:ProfDataUtils.cpp:165
std::swap
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition:BitVector.h:860
raw_ostream.h
N
#define N
SwitchSuccWrapper
Checking whether two cases of SI are equal depends on the contents of the BasicBlock and the incoming...
Definition:SimplifyCFG.cpp:7500
SwitchSuccWrapper::Dest
BasicBlock * Dest
Definition:SimplifyCFG.cpp:7501
SwitchSuccWrapper::PhiPredIVs
DenseMap< PHINode *, SmallDenseMap< BasicBlock *, Value *, 8 > > * PhiPredIVs
Definition:SimplifyCFG.cpp:7502
llvm::AAMDNodes::merge
AAMDNodes merge(const AAMDNodes &Other) const
Given two sets of AAMDNodes applying to potentially different locations, determine the best AAMDNodes...
Definition:TypeBasedAliasAnalysis.cpp:522
llvm::DenseMapInfo< const SwitchSuccWrapper * >::getEmptyKey
static const SwitchSuccWrapper * getEmptyKey()
Definition:SimplifyCFG.cpp:7507
llvm::DenseMapInfo< const SwitchSuccWrapper * >::getTombstoneKey
static const SwitchSuccWrapper * getTombstoneKey()
Definition:SimplifyCFG.cpp:7511
llvm::DenseMapInfo< const SwitchSuccWrapper * >::getHashValue
static unsigned getHashValue(const SwitchSuccWrapper *SSW)
Definition:SimplifyCFG.cpp:7515
llvm::DenseMapInfo< const SwitchSuccWrapper * >::isEqual
static bool isEqual(const SwitchSuccWrapper *LHS, const SwitchSuccWrapper *RHS)
Definition:SimplifyCFG.cpp:7540
llvm::DenseMapInfo
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition:DenseMapInfo.h:52
llvm::Incoming
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...
Definition:SILowerI1Copies.h:25
llvm::KnownBits
Definition:KnownBits.h:23
llvm::KnownBits::getBitWidth
unsigned getBitWidth() const
Get the bit width of this value.
Definition:KnownBits.h:43
llvm::KnownBits::One
APInt One
Definition:KnownBits.h:25
llvm::KnownBits::Zero
APInt Zero
Definition:KnownBits.h:24
llvm::SimplifyCFGOptions
Definition:SimplifyCFGOptions.h:23
llvm::SmallMapVector
A MapVector that performs no allocations if smaller than a certain size.
Definition:MapVector.h:254
llvm::cl::desc
Definition:CommandLine.h:409

Generated on Thu Jul 17 2025 17:52:51 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp