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/Cloning.h"
77#include "llvm/Transforms/Utils/Local.h"
78#include "llvm/Transforms/Utils/LockstepReverseIterator.h"
79#include "llvm/Transforms/Utils/ValueMapper.h"
80#include <algorithm>
81#include <cassert>
82#include <climits>
83#include <cmath>
84#include <cstddef>
85#include <cstdint>
86#include <iterator>
87#include <map>
88#include <optional>
89#include <set>
90#include <tuple>
91#include <utility>
92#include <vector>
93
94using namespace llvm;
95using namespace PatternMatch;
96
97#define DEBUG_TYPE "simplifycfg"
98
99namespace llvm {
100
101cl::opt<bool> RequireAndPreserveDomTree(
102 "simplifycfg-require-and-preserve-domtree", cl::Hidden,
103
104 cl::desc(
105 "Temporary development switch used to gradually uplift SimplifyCFG "
106 "into preserving DomTree,"));
107
108// Chosen as 2 so as to be cheap, but still to have enough power to fold
109// a select, so the "clamp" idiom (of a min followed by a max) will be caught.
110// To catch this, we need to fold a compare and a select, hence '2' being the
111// minimum reasonable default.
112static cl::opt<unsigned> PHINodeFoldingThreshold(
113 "phi-node-folding-threshold", cl::Hidden, cl::init(Val: 2),
114 cl::desc(
115 "Control the amount of phi node folding to perform (default = 2)"));
116
117static cl::opt<unsigned> TwoEntryPHINodeFoldingThreshold(
118 "two-entry-phi-node-folding-threshold", cl::Hidden, cl::init(Val: 4),
119 cl::desc("Control the maximal total instruction cost that we are willing "
120 "to speculatively execute to fold a 2-entry PHI node into a "
121 "select (default = 4)"));
122
123static cl::opt<bool>
124 HoistCommon("simplifycfg-hoist-common", cl::Hidden, cl::init(Val: true),
125 cl::desc("Hoist common instructions up to the parent block"));
126
127static cl::opt<bool> HoistLoadsWithCondFaulting(
128 "simplifycfg-hoist-loads-with-cond-faulting", cl::Hidden, cl::init(Val: true),
129 cl::desc("Hoist loads if the target supports conditional faulting"));
130
131static cl::opt<bool> HoistStoresWithCondFaulting(
132 "simplifycfg-hoist-stores-with-cond-faulting", cl::Hidden, cl::init(Val: true),
133 cl::desc("Hoist stores if the target supports conditional faulting"));
134
135static cl::opt<unsigned> HoistLoadsStoresWithCondFaultingThreshold(
136 "hoist-loads-stores-with-cond-faulting-threshold", cl::Hidden, cl::init(Val: 6),
137 cl::desc("Control the maximal conditional load/store that we are willing "
138 "to speculatively execute to eliminate conditional branch "
139 "(default = 6)"));
140
141static cl::opt<unsigned>
142 HoistCommonSkipLimit("simplifycfg-hoist-common-skip-limit", cl::Hidden,
143 cl::init(Val: 20),
144 cl::desc("Allow reordering across at most this many "
145 "instructions when hoisting"));
146
147static cl::opt<bool>
148 SinkCommon("simplifycfg-sink-common", cl::Hidden, cl::init(Val: true),
149 cl::desc("Sink common instructions down to the end block"));
150
151static cl::opt<bool> HoistCondStores(
152 "simplifycfg-hoist-cond-stores", cl::Hidden, cl::init(Val: true),
153 cl::desc("Hoist conditional stores if an unconditional store precedes"));
154
155static cl::opt<bool> MergeCondStores(
156 "simplifycfg-merge-cond-stores", cl::Hidden, cl::init(Val: true),
157 cl::desc("Hoist conditional stores even if an unconditional store does not "
158 "precede - hoist multiple conditional stores into a single "
159 "predicated store"));
160
161static cl::opt<bool> MergeCondStoresAggressively(
162 "simplifycfg-merge-cond-stores-aggressively", cl::Hidden, cl::init(Val: false),
163 cl::desc("When merging conditional stores, do so even if the resultant "
164 "basic blocks are unlikely to be if-converted as a result"));
165
166static cl::opt<bool> SpeculateOneExpensiveInst(
167 "speculate-one-expensive-inst", cl::Hidden, cl::init(Val: true),
168 cl::desc("Allow exactly one expensive instruction to be speculatively "
169 "executed"));
170
171static cl::opt<unsigned> MaxSpeculationDepth(
172 "max-speculation-depth", cl::Hidden, cl::init(Val: 10),
173 cl::desc("Limit maximum recursion depth when calculating costs of "
174 "speculatively executed instructions"));
175
176static cl::opt<int>
177 MaxSmallBlockSize("simplifycfg-max-small-block-size", cl::Hidden,
178 cl::init(Val: 10),
179 cl::desc("Max size of a block which is still considered "
180 "small enough to thread through"));
181
182// Two is chosen to allow one negation and a logical combine.
183static cl::opt<unsigned>
184 BranchFoldThreshold("simplifycfg-branch-fold-threshold", cl::Hidden,
185 cl::init(Val: 2),
186 cl::desc("Maximum cost of combining conditions when "
187 "folding branches"));
188
189static cl::opt<unsigned> BranchFoldToCommonDestVectorMultiplier(
190 "simplifycfg-branch-fold-common-dest-vector-multiplier", cl::Hidden,
191 cl::init(Val: 2),
192 cl::desc("Multiplier to apply to threshold when determining whether or not "
193 "to fold branch to common destination when vector operations are "
194 "present"));
195
196static cl::opt<bool> EnableMergeCompatibleInvokes(
197 "simplifycfg-merge-compatible-invokes", cl::Hidden, cl::init(Val: true),
198 cl::desc("Allow SimplifyCFG to merge invokes together when appropriate"));
199
200static cl::opt<unsigned> MaxSwitchCasesPerResult(
201 "max-switch-cases-per-result", cl::Hidden, cl::init(Val: 16),
202 cl::desc("Limit cases to analyze when converting a switch to select"));
203
204static cl::opt<unsigned> MaxJumpThreadingLiveBlocks(
205 "max-jump-threading-live-blocks", cl::Hidden, cl::init(Val: 24),
206 cl::desc("Limit number of blocks a define in a threaded block is allowed "
207 "to be live in"));
208
209extern cl::opt<bool> ProfcheckDisableMetadataFixes;
210
211} // end namespace llvm
212
213STATISTIC(NumBitMaps, "Number of switch instructions turned into bitmaps");
214STATISTIC(NumLinearMaps,
215 "Number of switch instructions turned into linear mapping");
216STATISTIC(NumLookupTables,
217 "Number of switch instructions turned into lookup tables");
218STATISTIC(
219 NumLookupTablesHoles,
220 "Number of switch instructions turned into lookup tables (holes checked)");
221STATISTIC(NumTableCmpReuses, "Number of reused switch table lookup compares");
222STATISTIC(NumFoldValueComparisonIntoPredecessors,
223 "Number of value comparisons folded into predecessor basic blocks");
224STATISTIC(NumFoldBranchToCommonDest,
225 "Number of branches folded into predecessor basic block");
226STATISTIC(
227 NumHoistCommonCode,
228 "Number of common instruction 'blocks' hoisted up to the begin block");
229STATISTIC(NumHoistCommonInstrs,
230 "Number of common instructions hoisted up to the begin block");
231STATISTIC(NumSinkCommonCode,
232 "Number of common instruction 'blocks' sunk down to the end block");
233STATISTIC(NumSinkCommonInstrs,
234 "Number of common instructions sunk down to the end block");
235STATISTIC(NumSpeculations, "Number of speculative executed instructions");
236STATISTIC(NumInvokes,
237 "Number of invokes with empty resume blocks simplified into calls");
238STATISTIC(NumInvokesMerged, "Number of invokes that were merged together");
239STATISTIC(NumInvokeSetsFormed, "Number of invoke sets that were formed");
240
241namespace {
242
243// The first field contains the value that the switch produces when a certain
244// case group is selected, and the second field is a vector containing the
245// cases composing the case group.
246using SwitchCaseResultVectorTy =
247 SmallVector<std::pair<Constant *, SmallVector<ConstantInt *, 4>>, 2>;
248
249// The first field contains the phi node that generates a result of the switch
250// and the second field contains the value generated for a certain case in the
251// switch for that PHI.
252using SwitchCaseResultsTy = SmallVector<std::pair<PHINode *, Constant *>, 4>;
253
254/// ValueEqualityComparisonCase - Represents a case of a switch.
255struct ValueEqualityComparisonCase {
256 ConstantInt *Value;
257 BasicBlock *Dest;
258
259 ValueEqualityComparisonCase(ConstantInt *Value, BasicBlock *Dest)
260 : Value(Value), Dest(Dest) {}
261
262 bool operator<(ValueEqualityComparisonCase RHS) const {
263 // Comparing pointers is ok as we only rely on the order for uniquing.
264 return Value < RHS.Value;
265 }
266
267 bool operator==(BasicBlock *RHSDest) const { return Dest == RHSDest; }
268};
269
270class SimplifyCFGOpt {
271 const TargetTransformInfo &TTI;
272 DomTreeUpdater *DTU;
273 const DataLayout &DL;
274 ArrayRef<WeakVH> LoopHeaders;
275 const SimplifyCFGOptions &Options;
276 bool Resimplify;
277
278 Value *isValueEqualityComparison(Instruction *TI);
279 BasicBlock *getValueEqualityComparisonCases(
280 Instruction *TI, std::vector<ValueEqualityComparisonCase> &Cases);
281 bool simplifyEqualityComparisonWithOnlyPredecessor(Instruction *TI,
282 BasicBlock *Pred,
283 IRBuilder<> &Builder);
284 bool performValueComparisonIntoPredecessorFolding(Instruction *TI, Value *&CV,
285 Instruction *PTI,
286 IRBuilder<> &Builder);
287 bool foldValueComparisonIntoPredecessors(Instruction *TI,
288 IRBuilder<> &Builder);
289
290 bool simplifyResume(ResumeInst *RI, IRBuilder<> &Builder);
291 bool simplifySingleResume(ResumeInst *RI);
292 bool simplifyCommonResume(ResumeInst *RI);
293 bool simplifyCleanupReturn(CleanupReturnInst *RI);
294 bool simplifyUnreachable(UnreachableInst *UI);
295 bool simplifySwitch(SwitchInst *SI, IRBuilder<> &Builder);
296 bool simplifyDuplicateSwitchArms(SwitchInst *SI, DomTreeUpdater *DTU);
297 bool simplifyIndirectBr(IndirectBrInst *IBI);
298 bool simplifyUncondBranch(UncondBrInst *BI, IRBuilder<> &Builder);
299 bool simplifyCondBranch(CondBrInst *BI, IRBuilder<> &Builder);
300 bool foldCondBranchOnValueKnownInPredecessor(CondBrInst *BI);
301
302 bool tryToSimplifyUncondBranchWithICmpInIt(ICmpInst *ICI,
303 IRBuilder<> &Builder);
304 bool tryToSimplifyUncondBranchWithICmpSelectInIt(ICmpInst *ICI,
305 SelectInst *Select,
306 IRBuilder<> &Builder);
307 bool hoistCommonCodeFromSuccessors(Instruction *TI, bool AllInstsEqOnly);
308 bool hoistSuccIdenticalTerminatorToSwitchOrIf(
309 Instruction *TI, Instruction *I1,
310 SmallVectorImpl<Instruction *> &OtherSuccTIs,
311 ArrayRef<BasicBlock *> UniqueSuccessors);
312 bool speculativelyExecuteBB(CondBrInst *BI, BasicBlock *ThenBB);
313 bool simplifyTerminatorOnSelect(Instruction *OldTerm, Value *Cond,
314 BasicBlock *TrueBB, BasicBlock *FalseBB,
315 uint32_t TrueWeight, uint32_t FalseWeight);
316 bool simplifyBranchOnICmpChain(CondBrInst *BI, IRBuilder<> &Builder,
317 const DataLayout &DL);
318 bool simplifySwitchOnSelect(SwitchInst *SI, SelectInst *Select);
319 bool simplifyIndirectBrOnSelect(IndirectBrInst *IBI, SelectInst *SI);
320 bool turnSwitchRangeIntoICmp(SwitchInst *SI, IRBuilder<> &Builder);
321 bool simplifyDuplicatePredecessors(BasicBlock *Succ, DomTreeUpdater *DTU);
322
323public:
324 SimplifyCFGOpt(const TargetTransformInfo &TTI, DomTreeUpdater *DTU,
325 const DataLayout &DL, ArrayRef<WeakVH> LoopHeaders,
326 const SimplifyCFGOptions &Opts)
327 : TTI(TTI), DTU(DTU), DL(DL), LoopHeaders(LoopHeaders), Options(Opts) {
328 assert((!DTU || !DTU->hasPostDomTree()) &&
329 "SimplifyCFG is not yet capable of maintaining validity of a "
330 "PostDomTree, so don't ask for it.");
331 }
332
333 bool simplifyOnce(BasicBlock *BB);
334 bool run(BasicBlock *BB);
335
336 // Helper to set Resimplify and return change indication.
337 bool requestResimplify() {
338 Resimplify = true;
339 return true;
340 }
341};
342
343// we synthesize a || b as select a, true, b
344// we synthesize a && b as select a, b, false
345// this function determines if SI is playing one of those roles.
346[[maybe_unused]] bool
347isSelectInRoleOfConjunctionOrDisjunction(const SelectInst *SI) {
348 return ((isa<ConstantInt>(Val: SI->getTrueValue()) &&
349 (dyn_cast<ConstantInt>(Val: SI->getTrueValue())->isOne())) ||
350 (isa<ConstantInt>(Val: SI->getFalseValue()) &&
351 (dyn_cast<ConstantInt>(Val: SI->getFalseValue())->isNullValue())));
352}
353
354} // end anonymous namespace
355
356/// Return true if all the PHI nodes in the basic block \p BB
357/// receive compatible (identical) incoming values when coming from
358/// all of the predecessor blocks that are specified in \p IncomingBlocks.
359///
360/// Note that if the values aren't exactly identical, but \p EquivalenceSet
361/// is provided, and *both* of the values are present in the set,
362/// then they are considered equal.
363static bool incomingValuesAreCompatible(
364 BasicBlock *BB, ArrayRef<BasicBlock *> IncomingBlocks,
365 SmallPtrSetImpl<Value *> *EquivalenceSet = nullptr) {
366 assert(IncomingBlocks.size() == 2 &&
367 "Only for a pair of incoming blocks at the time!");
368
369 // FIXME: it is okay if one of the incoming values is an `undef` value,
370 // iff the other incoming value is guaranteed to be a non-poison value.
371 // FIXME: it is okay if one of the incoming values is a `poison` value.
372 return all_of(Range: BB->phis(), P: [IncomingBlocks, EquivalenceSet](PHINode &PN) {
373 Value *IV0 = PN.getIncomingValueForBlock(BB: IncomingBlocks[0]);
374 Value *IV1 = PN.getIncomingValueForBlock(BB: IncomingBlocks[1]);
375 if (IV0 == IV1)
376 return true;
377 if (EquivalenceSet && EquivalenceSet->contains(Ptr: IV0) &&
378 EquivalenceSet->contains(Ptr: IV1))
379 return true;
380 return false;
381 });
382}
383
384/// Return true if it is safe to merge these two
385/// terminator instructions together.
386static bool
387safeToMergeTerminators(Instruction *SI1, Instruction *SI2,
388 SmallSetVector<BasicBlock *, 4> *FailBlocks = nullptr) {
389 if (SI1 == SI2)
390 return false; // Can't merge with self!
391
392 // It is not safe to merge these two switch instructions if they have a common
393 // successor, and if that successor has a PHI node, and if *that* PHI node has
394 // conflicting incoming values from the two switch blocks.
395 BasicBlock *SI1BB = SI1->getParent();
396 BasicBlock *SI2BB = SI2->getParent();
397
398 SmallPtrSet<BasicBlock *, 16> SI1Succs(llvm::from_range, successors(BB: SI1BB));
399 bool Fail = false;
400 for (BasicBlock *Succ : successors(BB: SI2BB)) {
401 if (!SI1Succs.count(Ptr: Succ))
402 continue;
403 if (incomingValuesAreCompatible(BB: Succ, IncomingBlocks: {SI1BB, SI2BB}))
404 continue;
405 Fail = true;
406 if (FailBlocks)
407 FailBlocks->insert(X: Succ);
408 else
409 break;
410 }
411
412 return !Fail;
413}
414
415/// Update PHI nodes in Succ to indicate that there will now be entries in it
416/// from the 'NewPred' block. The values that will be flowing into the PHI nodes
417/// will be the same as those coming in from ExistPred, an existing predecessor
418/// of Succ.
419static void addPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred,
420 BasicBlock *ExistPred,
421 MemorySSAUpdater *MSSAU = nullptr) {
422 for (PHINode &PN : Succ->phis())
423 PN.addIncoming(V: PN.getIncomingValueForBlock(BB: ExistPred), BB: NewPred);
424 if (MSSAU)
425 if (auto *MPhi = MSSAU->getMemorySSA()->getMemoryAccess(BB: Succ))
426 MPhi->addIncoming(V: MPhi->getIncomingValueForBlock(BB: ExistPred), BB: NewPred);
427}
428
429/// Compute an abstract "cost" of speculating the given instruction,
430/// which is assumed to be safe to speculate. TCC_Free means cheap,
431/// TCC_Basic means less cheap, and TCC_Expensive means prohibitively
432/// expensive.
433static InstructionCost computeSpeculationCost(const User *I,
434 const TargetTransformInfo &TTI) {
435 return TTI.getInstructionCost(U: I, CostKind: TargetTransformInfo::TCK_SizeAndLatency);
436}
437
438/// If we have a merge point of an "if condition" as accepted above,
439/// return true if the specified value dominates the block. We don't handle
440/// the true generality of domination here, just a special case which works
441/// well enough for us.
442///
443/// If AggressiveInsts is non-null, and if V does not dominate BB, we check to
444/// see if V (which must be an instruction) and its recursive operands
445/// that do not dominate BB have a combined cost lower than Budget and
446/// are non-trapping. If both are true, the instruction is inserted into the
447/// set and true is returned.
448///
449/// The cost for most non-trapping instructions is defined as 1 except for
450/// Select whose cost is 2.
451///
452/// After this function returns, Cost is increased by the cost of
453/// V plus its non-dominating operands. If that cost is greater than
454/// Budget, false is returned and Cost is undefined.
455static bool dominatesMergePoint(
456 Value *V, BasicBlock *BB, Instruction *InsertPt,
457 SmallPtrSetImpl<Instruction *> &AggressiveInsts, InstructionCost &Cost,
458 InstructionCost Budget, const TargetTransformInfo &TTI, AssumptionCache *AC,
459 SmallPtrSetImpl<Instruction *> &ZeroCostInstructions, unsigned Depth = 0) {
460 // It is possible to hit a zero-cost cycle (phi/gep instructions for example),
461 // so limit the recursion depth.
462 // TODO: While this recursion limit does prevent pathological behavior, it
463 // would be better to track visited instructions to avoid cycles.
464 if (Depth == MaxSpeculationDepth)
465 return false;
466
467 Instruction *I = dyn_cast<Instruction>(Val: V);
468 if (!I) {
469 // Non-instructions dominate all instructions and can be executed
470 // unconditionally.
471 return true;
472 }
473 BasicBlock *PBB = I->getParent();
474
475 // We don't want to allow weird loops that might have the "if condition" in
476 // the bottom of this block.
477 if (PBB == BB)
478 return false;
479
480 // If this instruction is defined in a block that contains an unconditional
481 // branch to BB, then it must be in the 'conditional' part of the "if
482 // statement". If not, it definitely dominates the region.
483 UncondBrInst *BI = dyn_cast<UncondBrInst>(Val: PBB->getTerminator());
484 if (!BI || BI->getSuccessor() != BB)
485 return true;
486
487 // If we have seen this instruction before, don't count it again.
488 if (AggressiveInsts.count(Ptr: I))
489 return true;
490
491 // Okay, it looks like the instruction IS in the "condition". Check to
492 // see if it's a cheap instruction to unconditionally compute, and if it
493 // only uses stuff defined outside of the condition. If so, hoist it out.
494 if (!isSafeToSpeculativelyExecute(I, CtxI: InsertPt, AC))
495 return false;
496
497 // Overflow arithmetic instruction plus extract value are usually generated
498 // when a division is being replaced. But, in this case, the zero check may
499 // still be kept in the code. In that case it would be worth to hoist these
500 // two instruction out of the basic block. Let's treat this pattern as one
501 // single cheap instruction here!
502 WithOverflowInst *OverflowInst;
503 if (match(V: I, P: m_ExtractValue<1>(V: m_OneUse(SubPattern: m_WithOverflowInst(I&: OverflowInst))))) {
504 ZeroCostInstructions.insert(Ptr: OverflowInst);
505 Cost += 1;
506 } else if (!ZeroCostInstructions.contains(Ptr: I))
507 Cost += computeSpeculationCost(I, TTI);
508
509 // Allow exactly one instruction to be speculated regardless of its cost
510 // (as long as it is safe to do so).
511 // This is intended to flatten the CFG even if the instruction is a division
512 // or other expensive operation. The speculation of an expensive instruction
513 // is expected to be undone in CodeGenPrepare if the speculation has not
514 // enabled further IR optimizations.
515 if (Cost > Budget &&
516 (!SpeculateOneExpensiveInst || !AggressiveInsts.empty() || Depth > 0 ||
517 !Cost.isValid()))
518 return false;
519
520 // Okay, we can only really hoist these out if their operands do
521 // not take us over the cost threshold.
522 for (Use &Op : I->operands())
523 if (!dominatesMergePoint(V: Op, BB, InsertPt, AggressiveInsts, Cost, Budget,
524 TTI, AC, ZeroCostInstructions, Depth: Depth + 1))
525 return false;
526 // Okay, it's safe to do this! Remember this instruction.
527 AggressiveInsts.insert(Ptr: I);
528 return true;
529}
530
531/// Extract ConstantInt from value, looking through IntToPtr
532/// and PointerNullValue. Return NULL if value is not a constant int.
533static ConstantInt *getConstantInt(Value *V, const DataLayout &DL) {
534 // Normal constant int.
535 ConstantInt *CI = dyn_cast<ConstantInt>(Val: V);
536 if (CI || !isa<Constant>(Val: V) || !V->getType()->isPointerTy())
537 return CI;
538
539 // It is not safe to look through inttoptr or ptrtoint when using unstable
540 // pointer types.
541 if (DL.hasUnstableRepresentation(Ty: V->getType()))
542 return nullptr;
543
544 // This is some kind of pointer constant. Turn it into a pointer-sized
545 // ConstantInt if possible.
546 IntegerType *IntPtrTy = cast<IntegerType>(Val: DL.getIntPtrType(V->getType()));
547
548 // Null pointer means 0, see SelectionDAGBuilder::getValue(const Value*).
549 if (isa<ConstantPointerNull>(Val: V))
550 return ConstantInt::get(Ty: IntPtrTy, V: 0);
551
552 // IntToPtr const int, we can look through this if the semantics of
553 // inttoptr for this address space are a simple (truncating) bitcast.
554 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Val: V))
555 if (CE->getOpcode() == Instruction::IntToPtr)
556 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val: CE->getOperand(i_nocapture: 0))) {
557 // The constant is very likely to have the right type already.
558 if (CI->getType() == IntPtrTy)
559 return CI;
560 else
561 return cast<ConstantInt>(
562 Val: ConstantFoldIntegerCast(C: CI, DestTy: IntPtrTy, /*isSigned=*/IsSigned: false, DL));
563 }
564 return nullptr;
565}
566
567namespace {
568
569/// Given a chain of or (||) or and (&&) comparison of a value against a
570/// constant, this will try to recover the information required for a switch
571/// structure.
572/// It will depth-first traverse the chain of comparison, seeking for patterns
573/// like %a == 12 or %a < 4 and combine them to produce a set of integer
574/// representing the different cases for the switch.
575/// Note that if the chain is composed of '||' it will build the set of elements
576/// that matches the comparisons (i.e. any of this value validate the chain)
577/// while for a chain of '&&' it will build the set elements that make the test
578/// fail.
579struct ConstantComparesGatherer {
580 const DataLayout &DL;
581
582 /// Value found for the switch comparison
583 Value *CompValue = nullptr;
584
585 /// Extra clause to be checked before the switch
586 Value *Extra = nullptr;
587
588 /// Set of integers to match in switch
589 SmallVector<ConstantInt *, 8> Vals;
590
591 /// Number of comparisons matched in the and/or chain
592 unsigned UsedICmps = 0;
593
594 /// If the elements in Vals matches the comparisons
595 bool IsEq = false;
596
597 // Used to check if the first matched CompValue shall be the Extra check.
598 bool IgnoreFirstMatch = false;
599 bool MultipleMatches = false;
600
601 /// Construct and compute the result for the comparison instruction Cond
602 ConstantComparesGatherer(Instruction *Cond, const DataLayout &DL) : DL(DL) {
603 gather(V: Cond);
604 if (CompValue || !MultipleMatches)
605 return;
606 Extra = nullptr;
607 Vals.clear();
608 UsedICmps = 0;
609 IgnoreFirstMatch = true;
610 gather(V: Cond);
611 }
612
613 ConstantComparesGatherer(const ConstantComparesGatherer &) = delete;
614 ConstantComparesGatherer &
615 operator=(const ConstantComparesGatherer &) = delete;
616
617private:
618 /// Try to set the current value used for the comparison, it succeeds only if
619 /// it wasn't set before or if the new value is the same as the old one
620 bool setValueOnce(Value *NewVal) {
621 if (IgnoreFirstMatch) {
622 IgnoreFirstMatch = false;
623 return false;
624 }
625 if (CompValue && CompValue != NewVal) {
626 MultipleMatches = true;
627 return false;
628 }
629 CompValue = NewVal;
630 return true;
631 }
632
633 /// Try to match Instruction "I" as a comparison against a constant and
634 /// populates the array Vals with the set of values that match (or do not
635 /// match depending on isEQ).
636 /// Return false on failure. On success, the Value the comparison matched
637 /// against is placed in CompValue.
638 /// If CompValue is already set, the function is expected to fail if a match
639 /// is found but the value compared to is different.
640 bool matchInstruction(Instruction *I, bool isEQ) {
641 if (match(V: I, P: m_Not(V: m_Instruction(I))))
642 isEQ = !isEQ;
643
644 Value *Val;
645 if (match(V: I, P: m_NUWTrunc(Op: m_Value(V&: Val)))) {
646 // If we already have a value for the switch, it has to match!
647 if (!setValueOnce(Val))
648 return false;
649 UsedICmps++;
650 Vals.push_back(Elt: ConstantInt::get(Ty: cast<IntegerType>(Val: Val->getType()), V: isEQ));
651 return true;
652 }
653 // If this is an icmp against a constant, handle this as one of the cases.
654 ICmpInst *ICI;
655 ConstantInt *C;
656 if (!((ICI = dyn_cast<ICmpInst>(Val: I)) &&
657 (C = getConstantInt(V: I->getOperand(i: 1), DL)))) {
658 return false;
659 }
660
661 Value *RHSVal;
662 const APInt *RHSC;
663
664 // Pattern match a special case
665 // (x & ~2^z) == y --> x == y || x == y|2^z
666 // This undoes a transformation done by instcombine to fuse 2 compares.
667 if (ICI->getPredicate() == (isEQ ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE)) {
668 // It's a little bit hard to see why the following transformations are
669 // correct. Here is a CVC3 program to verify them for 64-bit values:
670
671 /*
672 ONE : BITVECTOR(64) = BVZEROEXTEND(0bin1, 63);
673 x : BITVECTOR(64);
674 y : BITVECTOR(64);
675 z : BITVECTOR(64);
676 mask : BITVECTOR(64) = BVSHL(ONE, z);
677 QUERY( (y & ~mask = y) =>
678 ((x & ~mask = y) <=> (x = y OR x = (y | mask)))
679 );
680 QUERY( (y | mask = y) =>
681 ((x | mask = y) <=> (x = y OR x = (y & ~mask)))
682 );
683 */
684
685 // Please note that each pattern must be a dual implication (<--> or
686 // iff). One directional implication can create spurious matches. If the
687 // implication is only one-way, an unsatisfiable condition on the left
688 // side can imply a satisfiable condition on the right side. Dual
689 // implication ensures that satisfiable conditions are transformed to
690 // other satisfiable conditions and unsatisfiable conditions are
691 // transformed to other unsatisfiable conditions.
692
693 // Here is a concrete example of a unsatisfiable condition on the left
694 // implying a satisfiable condition on the right:
695 //
696 // mask = (1 << z)
697 // (x & ~mask) == y --> (x == y || x == (y | mask))
698 //
699 // Substituting y = 3, z = 0 yields:
700 // (x & -2) == 3 --> (x == 3 || x == 2)
701
702 // Pattern match a special case:
703 /*
704 QUERY( (y & ~mask = y) =>
705 ((x & ~mask = y) <=> (x = y OR x = (y | mask)))
706 );
707 */
708 if (match(V: ICI->getOperand(i_nocapture: 0),
709 P: m_And(L: m_Value(V&: RHSVal), R: m_APInt(Res&: RHSC)))) {
710 APInt Mask = ~*RHSC;
711 if (Mask.isPowerOf2() && (C->getValue() & ~Mask) == C->getValue()) {
712 // If we already have a value for the switch, it has to match!
713 if (!setValueOnce(RHSVal))
714 return false;
715
716 Vals.push_back(Elt: C);
717 Vals.push_back(
718 Elt: ConstantInt::get(Context&: C->getContext(),
719 V: C->getValue() | Mask));
720 UsedICmps++;
721 return true;
722 }
723 }
724
725 // Pattern match a special case:
726 /*
727 QUERY( (y | mask = y) =>
728 ((x | mask = y) <=> (x = y OR x = (y & ~mask)))
729 );
730 */
731 if (match(V: ICI->getOperand(i_nocapture: 0),
732 P: m_Or(L: m_Value(V&: RHSVal), R: m_APInt(Res&: RHSC)))) {
733 APInt Mask = *RHSC;
734 if (Mask.isPowerOf2() && (C->getValue() | Mask) == C->getValue()) {
735 // If we already have a value for the switch, it has to match!
736 if (!setValueOnce(RHSVal))
737 return false;
738
739 Vals.push_back(Elt: C);
740 Vals.push_back(Elt: ConstantInt::get(Context&: C->getContext(),
741 V: C->getValue() & ~Mask));
742 UsedICmps++;
743 return true;
744 }
745 }
746
747 // If we already have a value for the switch, it has to match!
748 if (!setValueOnce(ICI->getOperand(i_nocapture: 0)))
749 return false;
750
751 UsedICmps++;
752 Vals.push_back(Elt: C);
753 return true;
754 }
755
756 // If we have "x ult 3", for example, then we can add 0,1,2 to the set.
757 ConstantRange Span =
758 ConstantRange::makeExactICmpRegion(Pred: ICI->getPredicate(), Other: C->getValue());
759
760 // Shift the range if the compare is fed by an add. This is the range
761 // compare idiom as emitted by instcombine.
762 Value *CandidateVal = I->getOperand(i: 0);
763 if (match(V: I->getOperand(i: 0), P: m_Add(L: m_Value(V&: RHSVal), R: m_APInt(Res&: RHSC)))) {
764 Span = Span.subtract(CI: *RHSC);
765 CandidateVal = RHSVal;
766 }
767
768 // If this is an and/!= check, then we are looking to build the set of
769 // value that *don't* pass the and chain. I.e. to turn "x ugt 2" into
770 // x != 0 && x != 1.
771 if (!isEQ)
772 Span = Span.inverse();
773
774 // If there are a ton of values, we don't want to make a ginormous switch.
775 if (Span.isSizeLargerThan(MaxSize: 8) || Span.isEmptySet()) {
776 return false;
777 }
778
779 // If we already have a value for the switch, it has to match!
780 if (!setValueOnce(CandidateVal))
781 return false;
782
783 // Add all values from the range to the set
784 APInt Tmp = Span.getLower();
785 do
786 Vals.push_back(Elt: ConstantInt::get(Context&: I->getContext(), V: Tmp));
787 while (++Tmp != Span.getUpper());
788
789 UsedICmps++;
790 return true;
791 }
792
793 /// Given a potentially 'or'd or 'and'd together collection of icmp
794 /// eq/ne/lt/gt instructions that compare a value against a constant, extract
795 /// the value being compared, and stick the list constants into the Vals
796 /// vector.
797 /// One "Extra" case is allowed to differ from the other.
798 void gather(Value *V) {
799 Value *Op0, *Op1;
800 if (match(V, P: m_LogicalOr(L: m_Value(V&: Op0), R: m_Value(V&: Op1))))
801 IsEq = true;
802 else if (match(V, P: m_LogicalAnd(L: m_Value(V&: Op0), R: m_Value(V&: Op1))))
803 IsEq = false;
804 else
805 return;
806 // Keep a stack (SmallVector for efficiency) for depth-first traversal
807 SmallVector<Value *, 8> DFT{Op0, Op1};
808 SmallPtrSet<Value *, 8> Visited{V, Op0, Op1};
809
810 while (!DFT.empty()) {
811 V = DFT.pop_back_val();
812
813 if (Instruction *I = dyn_cast<Instruction>(Val: V)) {
814 // If it is a || (or && depending on isEQ), process the operands.
815 if (IsEq ? match(V: I, P: m_LogicalOr(L: m_Value(V&: Op0), R: m_Value(V&: Op1)))
816 : match(V: I, P: m_LogicalAnd(L: m_Value(V&: Op0), R: m_Value(V&: Op1)))) {
817 if (Visited.insert(Ptr: Op1).second)
818 DFT.push_back(Elt: Op1);
819 if (Visited.insert(Ptr: Op0).second)
820 DFT.push_back(Elt: Op0);
821
822 continue;
823 }
824
825 // Try to match the current instruction
826 if (matchInstruction(I, isEQ: IsEq))
827 // Match succeed, continue the loop
828 continue;
829 }
830
831 // One element of the sequence of || (or &&) could not be match as a
832 // comparison against the same value as the others.
833 // We allow only one "Extra" case to be checked before the switch
834 if (!Extra) {
835 Extra = V;
836 continue;
837 }
838 // Failed to parse a proper sequence, abort now
839 CompValue = nullptr;
840 break;
841 }
842 }
843};
844
845} // end anonymous namespace
846
847static void eraseTerminatorAndDCECond(Instruction *TI,
848 MemorySSAUpdater *MSSAU = nullptr) {
849 Instruction *Cond = nullptr;
850 if (SwitchInst *SI = dyn_cast<SwitchInst>(Val: TI)) {
851 Cond = dyn_cast<Instruction>(Val: SI->getCondition());
852 } else if (CondBrInst *BI = dyn_cast<CondBrInst>(Val: TI)) {
853 Cond = dyn_cast<Instruction>(Val: BI->getCondition());
854 } else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(Val: TI)) {
855 Cond = dyn_cast<Instruction>(Val: IBI->getAddress());
856 }
857
858 TI->eraseFromParent();
859 if (Cond)
860 RecursivelyDeleteTriviallyDeadInstructions(V: Cond, TLI: nullptr, MSSAU);
861}
862
863/// Return true if the specified terminator checks
864/// to see if a value is equal to constant integer value.
865Value *SimplifyCFGOpt::isValueEqualityComparison(Instruction *TI) {
866 Value *CV = nullptr;
867 if (SwitchInst *SI = dyn_cast<SwitchInst>(Val: TI)) {
868 // Do not permit merging of large switch instructions into their
869 // predecessors unless there is only one predecessor.
870 if (!SI->getParent()->hasNPredecessorsOrMore(N: 128 / SI->getNumSuccessors()))
871 CV = SI->getCondition();
872 } else if (CondBrInst *BI = dyn_cast<CondBrInst>(Val: TI))
873 if (BI->getCondition()->hasOneUse()) {
874 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Val: BI->getCondition())) {
875 if (ICI->isEquality() && getConstantInt(V: ICI->getOperand(i_nocapture: 1), DL))
876 CV = ICI->getOperand(i_nocapture: 0);
877 } else if (auto *Trunc = dyn_cast<TruncInst>(Val: BI->getCondition())) {
878 if (Trunc->hasNoUnsignedWrap())
879 CV = Trunc->getOperand(i_nocapture: 0);
880 }
881 }
882
883 // Unwrap any lossless ptrtoint cast (except for unstable pointers).
884 if (CV) {
885 if (PtrToIntInst *PTII = dyn_cast<PtrToIntInst>(Val: CV)) {
886 Value *Ptr = PTII->getPointerOperand();
887 if (DL.hasUnstableRepresentation(Ty: Ptr->getType()))
888 return CV;
889 if (PTII->getType() == DL.getIntPtrType(Ptr->getType()))
890 CV = Ptr;
891 }
892 }
893 return CV;
894}
895
896/// Given a value comparison instruction,
897/// decode all of the 'cases' that it represents and return the 'default' block.
898BasicBlock *SimplifyCFGOpt::getValueEqualityComparisonCases(
899 Instruction *TI, std::vector<ValueEqualityComparisonCase> &Cases) {
900 if (SwitchInst *SI = dyn_cast<SwitchInst>(Val: TI)) {
901 Cases.reserve(n: SI->getNumCases());
902 for (auto Case : SI->cases())
903 Cases.push_back(x: ValueEqualityComparisonCase(Case.getCaseValue(),
904 Case.getCaseSuccessor()));
905 return SI->getDefaultDest();
906 }
907
908 CondBrInst *BI = cast<CondBrInst>(Val: TI);
909 Value *Cond = BI->getCondition();
910 ICmpInst::Predicate Pred;
911 ConstantInt *C;
912 if (auto *ICI = dyn_cast<ICmpInst>(Val: Cond)) {
913 Pred = ICI->getPredicate();
914 C = getConstantInt(V: ICI->getOperand(i_nocapture: 1), DL);
915 } else {
916 Pred = ICmpInst::ICMP_NE;
917 auto *Trunc = cast<TruncInst>(Val: Cond);
918 C = ConstantInt::get(Ty: cast<IntegerType>(Val: Trunc->getOperand(i_nocapture: 0)->getType()), V: 0);
919 }
920 BasicBlock *Succ = BI->getSuccessor(i: Pred == ICmpInst::ICMP_NE);
921 Cases.push_back(x: ValueEqualityComparisonCase(C, Succ));
922 return BI->getSuccessor(i: Pred == ICmpInst::ICMP_EQ);
923}
924
925/// Given a vector of bb/value pairs, remove any entries
926/// in the list that match the specified block.
927static void
928eliminateBlockCases(BasicBlock *BB,
929 std::vector<ValueEqualityComparisonCase> &Cases) {
930 llvm::erase(C&: Cases, V: BB);
931}
932
933/// Return true if there are any keys in C1 that exist in C2 as well.
934static bool valuesOverlap(std::vector<ValueEqualityComparisonCase> &C1,
935 std::vector<ValueEqualityComparisonCase> &C2) {
936 std::vector<ValueEqualityComparisonCase> *V1 = &C1, *V2 = &C2;
937
938 // Make V1 be smaller than V2.
939 if (V1->size() > V2->size())
940 std::swap(a&: V1, b&: V2);
941
942 if (V1->empty())
943 return false;
944 if (V1->size() == 1) {
945 // Just scan V2.
946 ConstantInt *TheVal = (*V1)[0].Value;
947 for (const ValueEqualityComparisonCase &VECC : *V2)
948 if (TheVal == VECC.Value)
949 return true;
950 }
951
952 // Otherwise, just sort both lists and compare element by element.
953 array_pod_sort(Start: V1->begin(), End: V1->end());
954 array_pod_sort(Start: V2->begin(), End: V2->end());
955 unsigned i1 = 0, i2 = 0, e1 = V1->size(), e2 = V2->size();
956 while (i1 != e1 && i2 != e2) {
957 if ((*V1)[i1].Value == (*V2)[i2].Value)
958 return true;
959 if ((*V1)[i1].Value < (*V2)[i2].Value)
960 ++i1;
961 else
962 ++i2;
963 }
964 return false;
965}
966
967/// If TI is known to be a terminator instruction and its block is known to
968/// only have a single predecessor block, check to see if that predecessor is
969/// also a value comparison with the same value, and if that comparison
970/// determines the outcome of this comparison. If so, simplify TI. This does a
971/// very limited form of jump threading.
972bool SimplifyCFGOpt::simplifyEqualityComparisonWithOnlyPredecessor(
973 Instruction *TI, BasicBlock *Pred, IRBuilder<> &Builder) {
974 Value *PredVal = isValueEqualityComparison(TI: Pred->getTerminator());
975 if (!PredVal)
976 return false; // Not a value comparison in predecessor.
977
978 Value *ThisVal = isValueEqualityComparison(TI);
979 assert(ThisVal && "This isn't a value comparison!!");
980 if (ThisVal != PredVal)
981 return false; // Different predicates.
982
983 // TODO: Preserve branch weight metadata, similarly to how
984 // foldValueComparisonIntoPredecessors preserves it.
985
986 // Find out information about when control will move from Pred to TI's block.
987 std::vector<ValueEqualityComparisonCase> PredCases;
988 BasicBlock *PredDef =
989 getValueEqualityComparisonCases(TI: Pred->getTerminator(), Cases&: PredCases);
990 eliminateBlockCases(BB: PredDef, Cases&: PredCases); // Remove default from cases.
991
992 // Find information about how control leaves this block.
993 std::vector<ValueEqualityComparisonCase> ThisCases;
994 BasicBlock *ThisDef = getValueEqualityComparisonCases(TI, Cases&: ThisCases);
995 eliminateBlockCases(BB: ThisDef, Cases&: ThisCases); // Remove default from cases.
996
997 // If TI's block is the default block from Pred's comparison, potentially
998 // simplify TI based on this knowledge.
999 if (PredDef == TI->getParent()) {
1000 // If we are here, we know that the value is none of those cases listed in
1001 // PredCases. If there are any cases in ThisCases that are in PredCases, we
1002 // can simplify TI.
1003 if (!valuesOverlap(C1&: PredCases, C2&: ThisCases))
1004 return false;
1005
1006 if (isa<CondBrInst>(Val: TI)) {
1007 // Okay, one of the successors of this condbr is dead. Convert it to a
1008 // uncond br.
1009 assert(ThisCases.size() == 1 && "Branch can only have one case!");
1010 // Insert the new branch.
1011 Instruction *NI = Builder.CreateBr(Dest: ThisDef);
1012 (void)NI;
1013
1014 // Remove PHI node entries for the dead edge.
1015 ThisCases[0].Dest->removePredecessor(Pred: PredDef);
1016
1017 LLVM_DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
1018 << "Through successor TI: " << *TI << "Leaving: " << *NI
1019 << "\n");
1020
1021 eraseTerminatorAndDCECond(TI);
1022
1023 if (DTU)
1024 DTU->applyUpdates(
1025 Updates: {{DominatorTree::Delete, PredDef, ThisCases[0].Dest}});
1026
1027 return true;
1028 }
1029
1030 SwitchInstProfUpdateWrapper SI = *cast<SwitchInst>(Val: TI);
1031 // Okay, TI has cases that are statically dead, prune them away.
1032 SmallPtrSet<Constant *, 16> DeadCases;
1033 for (const ValueEqualityComparisonCase &Case : PredCases)
1034 DeadCases.insert(Ptr: Case.Value);
1035
1036 LLVM_DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
1037 << "Through successor TI: " << *TI);
1038
1039 SmallDenseMap<BasicBlock *, int, 8> NumPerSuccessorCases;
1040 for (SwitchInst::CaseIt i = SI->case_end(), e = SI->case_begin(); i != e;) {
1041 --i;
1042 auto *Successor = i->getCaseSuccessor();
1043 if (DTU)
1044 ++NumPerSuccessorCases[Successor];
1045 if (DeadCases.count(Ptr: i->getCaseValue())) {
1046 Successor->removePredecessor(Pred: PredDef);
1047 SI.removeCase(I: i);
1048 if (DTU)
1049 --NumPerSuccessorCases[Successor];
1050 }
1051 }
1052
1053 if (DTU) {
1054 std::vector<DominatorTree::UpdateType> Updates;
1055 for (const std::pair<BasicBlock *, int> &I : NumPerSuccessorCases)
1056 if (I.second == 0)
1057 Updates.push_back(x: {DominatorTree::Delete, PredDef, I.first});
1058 DTU->applyUpdates(Updates);
1059 }
1060
1061 LLVM_DEBUG(dbgs() << "Leaving: " << *TI << "\n");
1062 return true;
1063 }
1064
1065 // Otherwise, TI's block must correspond to some matched value. Find out
1066 // which value (or set of values) this is.
1067 ConstantInt *TIV = nullptr;
1068 BasicBlock *TIBB = TI->getParent();
1069 for (const auto &[Value, Dest] : PredCases)
1070 if (Dest == TIBB) {
1071 if (TIV)
1072 return false; // Cannot handle multiple values coming to this block.
1073 TIV = Value;
1074 }
1075 assert(TIV && "No edge from pred to succ?");
1076
1077 // Okay, we found the one constant that our value can be if we get into TI's
1078 // BB. Find out which successor will unconditionally be branched to.
1079 BasicBlock *TheRealDest = nullptr;
1080 for (const auto &[Value, Dest] : ThisCases)
1081 if (Value == TIV) {
1082 TheRealDest = Dest;
1083 break;
1084 }
1085
1086 // If not handled by any explicit cases, it is handled by the default case.
1087 if (!TheRealDest)
1088 TheRealDest = ThisDef;
1089
1090 SmallPtrSet<BasicBlock *, 2> RemovedSuccs;
1091
1092 // Remove PHI node entries for dead edges.
1093 BasicBlock *CheckEdge = TheRealDest;
1094 for (BasicBlock *Succ : successors(BB: TIBB))
1095 if (Succ != CheckEdge) {
1096 if (Succ != TheRealDest)
1097 RemovedSuccs.insert(Ptr: Succ);
1098 Succ->removePredecessor(Pred: TIBB);
1099 } else
1100 CheckEdge = nullptr;
1101
1102 // Insert the new branch.
1103 Instruction *NI = Builder.CreateBr(Dest: TheRealDest);
1104 (void)NI;
1105
1106 LLVM_DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
1107 << "Through successor TI: " << *TI << "Leaving: " << *NI
1108 << "\n");
1109
1110 eraseTerminatorAndDCECond(TI);
1111 if (DTU) {
1112 SmallVector<DominatorTree::UpdateType, 2> Updates;
1113 Updates.reserve(N: RemovedSuccs.size());
1114 for (auto *RemovedSucc : RemovedSuccs)
1115 Updates.push_back(Elt: {DominatorTree::Delete, TIBB, RemovedSucc});
1116 DTU->applyUpdates(Updates);
1117 }
1118 return true;
1119}
1120
1121namespace {
1122
1123/// This class implements a stable ordering of constant
1124/// integers that does not depend on their address. This is important for
1125/// applications that sort ConstantInt's to ensure uniqueness.
1126struct ConstantIntOrdering {
1127 bool operator()(const ConstantInt *LHS, const ConstantInt *RHS) const {
1128 return LHS->getValue().ult(RHS: RHS->getValue());
1129 }
1130};
1131
1132} // end anonymous namespace
1133
1134static int constantIntSortPredicate(ConstantInt *const *P1,
1135 ConstantInt *const *P2) {
1136 const ConstantInt *LHS = *P1;
1137 const ConstantInt *RHS = *P2;
1138 if (LHS == RHS)
1139 return 0;
1140 return LHS->getValue().ult(RHS: RHS->getValue()) ? 1 : -1;
1141}
1142
1143/// Get Weights of a given terminator, the default weight is at the front
1144/// of the vector. If TI is a conditional eq, we need to swap the branch-weight
1145/// metadata.
1146static void getBranchWeights(Instruction *TI,
1147 SmallVectorImpl<uint64_t> &Weights) {
1148 MDNode *MD = TI->getMetadata(KindID: LLVMContext::MD_prof);
1149 assert(MD && "Invalid branch-weight metadata");
1150 extractFromBranchWeightMD64(ProfileData: MD, Weights);
1151
1152 // If TI is a conditional eq, the default case is the false case,
1153 // and the corresponding branch-weight data is at index 2. We swap the
1154 // default weight to be the first entry.
1155 if (CondBrInst *BI = dyn_cast<CondBrInst>(Val: TI)) {
1156 assert(Weights.size() == 2);
1157 auto *ICI = dyn_cast<ICmpInst>(Val: BI->getCondition());
1158 if (!ICI)
1159 return;
1160
1161 if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
1162 std::swap(a&: Weights.front(), b&: Weights.back());
1163 }
1164}
1165
1166static void cloneInstructionsIntoPredecessorBlockAndUpdateSSAUses(
1167 BasicBlock *BB, BasicBlock *PredBlock, ValueToValueMapTy &VMap) {
1168 Instruction *PTI = PredBlock->getTerminator();
1169
1170 // If we have bonus instructions, clone them into the predecessor block.
1171 // Note that there may be multiple predecessor blocks, so we cannot move
1172 // bonus instructions to a predecessor block.
1173 for (Instruction &BonusInst : *BB) {
1174 if (BonusInst.isTerminator())
1175 continue;
1176
1177 // Skip cloning pseudo probes into the predecessor, as it would overcount
1178 // otherwise.
1179 if (isa<PseudoProbeInst>(Val: BonusInst))
1180 continue;
1181
1182 Instruction *NewBonusInst = BonusInst.clone();
1183
1184 if (!NewBonusInst->getDebugLoc().isSameSourceLocation(Other: PTI->getDebugLoc())) {
1185 // Unless the instruction has the same !dbg location as the original
1186 // branch, drop it. When we fold the bonus instructions we want to make
1187 // sure we reset their debug locations in order to avoid stepping on
1188 // dead code caused by folding dead branches.
1189 NewBonusInst->setDebugLoc(DebugLoc::getDropped());
1190 } else if (const DebugLoc &DL = NewBonusInst->getDebugLoc()) {
1191 mapAtomInstance(DL, VMap);
1192 }
1193
1194 RemapInstruction(I: NewBonusInst, VM&: VMap,
1195 Flags: RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
1196
1197 // If we speculated an instruction, we need to drop any metadata that may
1198 // result in undefined behavior, as the metadata might have been valid
1199 // only given the branch precondition.
1200 // Similarly strip attributes on call parameters that may cause UB in
1201 // location the call is moved to.
1202 NewBonusInst->dropUBImplyingAttrsAndMetadata();
1203
1204 NewBonusInst->insertInto(ParentBB: PredBlock, It: PTI->getIterator());
1205 auto Range = NewBonusInst->cloneDebugInfoFrom(From: &BonusInst);
1206 RemapDbgRecordRange(M: NewBonusInst->getModule(), Range, VM&: VMap,
1207 Flags: RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
1208
1209 NewBonusInst->takeName(V: &BonusInst);
1210 BonusInst.setName(NewBonusInst->getName() + ".old");
1211 VMap[&BonusInst] = NewBonusInst;
1212
1213 // Update (liveout) uses of bonus instructions,
1214 // now that the bonus instruction has been cloned into predecessor.
1215 // Note that we expect to be in a block-closed SSA form for this to work!
1216 for (Use &U : make_early_inc_range(Range: BonusInst.uses())) {
1217 auto *UI = cast<Instruction>(Val: U.getUser());
1218 auto *PN = dyn_cast<PHINode>(Val: UI);
1219 if (!PN) {
1220 assert(UI->getParent() == BB && BonusInst.comesBefore(UI) &&
1221 "If the user is not a PHI node, then it should be in the same "
1222 "block as, and come after, the original bonus instruction.");
1223 continue; // Keep using the original bonus instruction.
1224 }
1225 // Is this the block-closed SSA form PHI node?
1226 if (PN->getIncomingBlock(U) == BB)
1227 continue; // Great, keep using the original bonus instruction.
1228 // The only other alternative is an "use" when coming from
1229 // the predecessor block - here we should refer to the cloned bonus instr.
1230 assert(PN->getIncomingBlock(U) == PredBlock &&
1231 "Not in block-closed SSA form?");
1232 U.set(NewBonusInst);
1233 }
1234 }
1235
1236 // Key Instructions: We may have propagated atom info into the pred. If the
1237 // pred's terminator already has atom info do nothing as merging would drop
1238 // one atom group anyway. If it doesn't, propagte the remapped atom group
1239 // from BB's terminator.
1240 if (auto &PredDL = PTI->getDebugLoc()) {
1241 auto &DL = BB->getTerminator()->getDebugLoc();
1242 if (!PredDL->getAtomGroup() && DL && DL->getAtomGroup() &&
1243 PredDL.isSameSourceLocation(Other: DL)) {
1244 PTI->setDebugLoc(DL);
1245 RemapSourceAtom(I: PTI, VM&: VMap);
1246 }
1247 }
1248}
1249
1250bool SimplifyCFGOpt::performValueComparisonIntoPredecessorFolding(
1251 Instruction *TI, Value *&CV, Instruction *PTI, IRBuilder<> &Builder) {
1252 BasicBlock *BB = TI->getParent();
1253 BasicBlock *Pred = PTI->getParent();
1254
1255 SmallVector<DominatorTree::UpdateType, 32> Updates;
1256
1257 // Figure out which 'cases' to copy from SI to PSI.
1258 std::vector<ValueEqualityComparisonCase> BBCases;
1259 BasicBlock *BBDefault = getValueEqualityComparisonCases(TI, Cases&: BBCases);
1260
1261 std::vector<ValueEqualityComparisonCase> PredCases;
1262 BasicBlock *PredDefault = getValueEqualityComparisonCases(TI: PTI, Cases&: PredCases);
1263
1264 // Based on whether the default edge from PTI goes to BB or not, fill in
1265 // PredCases and PredDefault with the new switch cases we would like to
1266 // build.
1267 SmallMapVector<BasicBlock *, int, 8> NewSuccessors;
1268
1269 // Update the branch weight metadata along the way
1270 SmallVector<uint64_t, 8> Weights;
1271 bool PredHasWeights = hasBranchWeightMD(I: *PTI);
1272 bool SuccHasWeights = hasBranchWeightMD(I: *TI);
1273
1274 if (PredHasWeights) {
1275 getBranchWeights(TI: PTI, Weights);
1276 // branch-weight metadata is inconsistent here.
1277 if (Weights.size() != 1 + PredCases.size())
1278 PredHasWeights = SuccHasWeights = false;
1279 } else if (SuccHasWeights)
1280 // If there are no predecessor weights but there are successor weights,
1281 // populate Weights with 1, which will later be scaled to the sum of
1282 // successor's weights
1283 Weights.assign(NumElts: 1 + PredCases.size(), Elt: 1);
1284
1285 SmallVector<uint64_t, 8> SuccWeights;
1286 if (SuccHasWeights) {
1287 getBranchWeights(TI, Weights&: SuccWeights);
1288 // branch-weight metadata is inconsistent here.
1289 if (SuccWeights.size() != 1 + BBCases.size())
1290 PredHasWeights = SuccHasWeights = false;
1291 } else if (PredHasWeights)
1292 SuccWeights.assign(NumElts: 1 + BBCases.size(), Elt: 1);
1293
1294 if (PredDefault == BB) {
1295 // If this is the default destination from PTI, only the edges in TI
1296 // that don't occur in PTI, or that branch to BB will be activated.
1297 std::set<ConstantInt *, ConstantIntOrdering> PTIHandled;
1298 for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
1299 if (PredCases[i].Dest != BB)
1300 PTIHandled.insert(x: PredCases[i].Value);
1301 else {
1302 // The default destination is BB, we don't need explicit targets.
1303 std::swap(a&: PredCases[i], b&: PredCases.back());
1304
1305 if (PredHasWeights || SuccHasWeights) {
1306 // Increase weight for the default case.
1307 Weights[0] += Weights[i + 1];
1308 std::swap(a&: Weights[i + 1], b&: Weights.back());
1309 Weights.pop_back();
1310 }
1311
1312 PredCases.pop_back();
1313 --i;
1314 --e;
1315 }
1316
1317 // Reconstruct the new switch statement we will be building.
1318 if (PredDefault != BBDefault) {
1319 PredDefault->removePredecessor(Pred);
1320 if (DTU && PredDefault != BB)
1321 Updates.push_back(Elt: {DominatorTree::Delete, Pred, PredDefault});
1322 PredDefault = BBDefault;
1323 ++NewSuccessors[BBDefault];
1324 }
1325
1326 unsigned CasesFromPred = Weights.size();
1327 uint64_t ValidTotalSuccWeight = 0;
1328 for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
1329 if (!PTIHandled.count(x: BBCases[i].Value) && BBCases[i].Dest != BBDefault) {
1330 PredCases.push_back(x: BBCases[i]);
1331 ++NewSuccessors[BBCases[i].Dest];
1332 if (SuccHasWeights || PredHasWeights) {
1333 // The default weight is at index 0, so weight for the ith case
1334 // should be at index i+1. Scale the cases from successor by
1335 // PredDefaultWeight (Weights[0]).
1336 Weights.push_back(Elt: Weights[0] * SuccWeights[i + 1]);
1337 ValidTotalSuccWeight += SuccWeights[i + 1];
1338 }
1339 }
1340
1341 if (SuccHasWeights || PredHasWeights) {
1342 ValidTotalSuccWeight += SuccWeights[0];
1343 // Scale the cases from predecessor by ValidTotalSuccWeight.
1344 for (unsigned i = 1; i < CasesFromPred; ++i)
1345 Weights[i] *= ValidTotalSuccWeight;
1346 // Scale the default weight by SuccDefaultWeight (SuccWeights[0]).
1347 Weights[0] *= SuccWeights[0];
1348 }
1349 } else {
1350 // If this is not the default destination from PSI, only the edges
1351 // in SI that occur in PSI with a destination of BB will be
1352 // activated.
1353 std::set<ConstantInt *, ConstantIntOrdering> PTIHandled;
1354 std::map<ConstantInt *, uint64_t> WeightsForHandled;
1355 for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
1356 if (PredCases[i].Dest == BB) {
1357 PTIHandled.insert(x: PredCases[i].Value);
1358
1359 if (PredHasWeights || SuccHasWeights) {
1360 WeightsForHandled[PredCases[i].Value] = Weights[i + 1];
1361 std::swap(a&: Weights[i + 1], b&: Weights.back());
1362 Weights.pop_back();
1363 }
1364
1365 std::swap(a&: PredCases[i], b&: PredCases.back());
1366 PredCases.pop_back();
1367 --i;
1368 --e;
1369 }
1370
1371 // Okay, now we know which constants were sent to BB from the
1372 // predecessor. Figure out where they will all go now.
1373 for (const ValueEqualityComparisonCase &Case : BBCases)
1374 if (PTIHandled.count(x: Case.Value)) {
1375 // If this is one we are capable of getting...
1376 if (PredHasWeights || SuccHasWeights)
1377 Weights.push_back(Elt: WeightsForHandled[Case.Value]);
1378 PredCases.push_back(x: Case);
1379 ++NewSuccessors[Case.Dest];
1380 PTIHandled.erase(x: Case.Value); // This constant is taken care of
1381 }
1382
1383 // If there are any constants vectored to BB that TI doesn't handle,
1384 // they must go to the default destination of TI.
1385 for (ConstantInt *I : PTIHandled) {
1386 if (PredHasWeights || SuccHasWeights)
1387 Weights.push_back(Elt: WeightsForHandled[I]);
1388 PredCases.push_back(x: ValueEqualityComparisonCase(I, BBDefault));
1389 ++NewSuccessors[BBDefault];
1390 }
1391 }
1392
1393 // Okay, at this point, we know which new successor Pred will get. Make
1394 // sure we update the number of entries in the PHI nodes for these
1395 // successors.
1396 SmallPtrSet<BasicBlock *, 2> SuccsOfPred;
1397 if (DTU) {
1398 SuccsOfPred = {llvm::from_range, successors(BB: Pred)};
1399 Updates.reserve(N: Updates.size() + NewSuccessors.size());
1400 }
1401 for (const std::pair<BasicBlock *, int /*Num*/> &NewSuccessor :
1402 NewSuccessors) {
1403 for (auto I : seq(Size: NewSuccessor.second)) {
1404 (void)I;
1405 addPredecessorToBlock(Succ: NewSuccessor.first, NewPred: Pred, ExistPred: BB);
1406 }
1407 if (DTU && !SuccsOfPred.contains(Ptr: NewSuccessor.first))
1408 Updates.push_back(Elt: {DominatorTree::Insert, Pred, NewSuccessor.first});
1409 }
1410
1411 Builder.SetInsertPoint(PTI);
1412 // Convert pointer to int before we switch.
1413 if (CV->getType()->isPointerTy()) {
1414 assert(!DL.hasUnstableRepresentation(CV->getType()) &&
1415 "Should not end up here with unstable pointers");
1416 CV =
1417 Builder.CreatePtrToInt(V: CV, DestTy: DL.getIntPtrType(CV->getType()), Name: "magicptr");
1418 }
1419
1420 // Now that the successors are updated, create the new Switch instruction.
1421 SwitchInst *NewSI = Builder.CreateSwitch(V: CV, Dest: PredDefault, NumCases: PredCases.size());
1422 NewSI->setDebugLoc(PTI->getDebugLoc());
1423 for (ValueEqualityComparisonCase &V : PredCases)
1424 NewSI->addCase(OnVal: V.Value, Dest: V.Dest);
1425
1426 if (PredHasWeights || SuccHasWeights)
1427 setFittedBranchWeights(I&: *NewSI, Weights, /*IsExpected=*/false,
1428 /*ElideAllZero=*/true);
1429
1430 eraseTerminatorAndDCECond(TI: PTI);
1431
1432 // Okay, last check. If BB is still a successor of PSI, then we must
1433 // have an infinite loop case. If so, add an infinitely looping block
1434 // to handle the case to preserve the behavior of the code.
1435 BasicBlock *InfLoopBlock = nullptr;
1436 for (unsigned i = 0, e = NewSI->getNumSuccessors(); i != e; ++i)
1437 if (NewSI->getSuccessor(idx: i) == BB) {
1438 if (!InfLoopBlock) {
1439 // Insert it at the end of the function, because it's either code,
1440 // or it won't matter if it's hot. :)
1441 InfLoopBlock =
1442 BasicBlock::Create(Context&: BB->getContext(), Name: "infloop", Parent: BB->getParent());
1443 UncondBrInst::Create(Target: InfLoopBlock, InsertBefore: InfLoopBlock);
1444 if (DTU)
1445 Updates.push_back(
1446 Elt: {DominatorTree::Insert, InfLoopBlock, InfLoopBlock});
1447 }
1448 NewSI->setSuccessor(idx: i, NewSucc: InfLoopBlock);
1449 }
1450
1451 if (DTU) {
1452 if (InfLoopBlock)
1453 Updates.push_back(Elt: {DominatorTree::Insert, Pred, InfLoopBlock});
1454
1455 Updates.push_back(Elt: {DominatorTree::Delete, Pred, BB});
1456
1457 DTU->applyUpdates(Updates);
1458 }
1459
1460 ++NumFoldValueComparisonIntoPredecessors;
1461 return true;
1462}
1463
1464/// The specified terminator is a value equality comparison instruction
1465/// (either a switch or a branch on "X == c").
1466/// See if any of the predecessors of the terminator block are value comparisons
1467/// on the same value. If so, and if safe to do so, fold them together.
1468bool SimplifyCFGOpt::foldValueComparisonIntoPredecessors(Instruction *TI,
1469 IRBuilder<> &Builder) {
1470 BasicBlock *BB = TI->getParent();
1471 Value *CV = isValueEqualityComparison(TI); // CondVal
1472 assert(CV && "Not a comparison?");
1473
1474 bool Changed = false;
1475
1476 SmallSetVector<BasicBlock *, 16> Preds(pred_begin(BB), pred_end(BB));
1477 while (!Preds.empty()) {
1478 BasicBlock *Pred = Preds.pop_back_val();
1479 Instruction *PTI = Pred->getTerminator();
1480
1481 // Don't try to fold into itself.
1482 if (Pred == BB)
1483 continue;
1484
1485 // See if the predecessor is a comparison with the same value.
1486 Value *PCV = isValueEqualityComparison(TI: PTI); // PredCondVal
1487 if (PCV != CV)
1488 continue;
1489
1490 SmallSetVector<BasicBlock *, 4> FailBlocks;
1491 if (!safeToMergeTerminators(SI1: TI, SI2: PTI, FailBlocks: &FailBlocks)) {
1492 for (auto *Succ : FailBlocks) {
1493 if (!SplitBlockPredecessors(BB: Succ, Preds: TI->getParent(), Suffix: ".fold.split", DTU))
1494 return false;
1495 }
1496 }
1497
1498 performValueComparisonIntoPredecessorFolding(TI, CV, PTI, Builder);
1499 Changed = true;
1500 }
1501 return Changed;
1502}
1503
1504// If we would need to insert a select that uses the value of this invoke
1505// (comments in hoistSuccIdenticalTerminatorToSwitchOrIf explain why we would
1506// need to do this), we can't hoist the invoke, as there is nowhere to put the
1507// select in this case.
1508static bool isSafeToHoistInvoke(BasicBlock *BB1, BasicBlock *BB2,
1509 Instruction *I1, Instruction *I2) {
1510 for (BasicBlock *Succ : successors(BB: BB1)) {
1511 for (const PHINode &PN : Succ->phis()) {
1512 Value *BB1V = PN.getIncomingValueForBlock(BB: BB1);
1513 Value *BB2V = PN.getIncomingValueForBlock(BB: BB2);
1514 if (BB1V != BB2V && (BB1V == I1 || BB2V == I2)) {
1515 return false;
1516 }
1517 }
1518 }
1519 return true;
1520}
1521
1522// Get interesting characteristics of instructions that
1523// `hoistCommonCodeFromSuccessors` didn't hoist. They restrict what kind of
1524// instructions can be reordered across.
1525enum SkipFlags {
1526 SkipReadMem = 1,
1527 SkipSideEffect = 2,
1528 SkipImplicitControlFlow = 4
1529};
1530
1531static unsigned skippedInstrFlags(Instruction *I) {
1532 // Pseudo probes don't constrain reordering of other instructions.
1533 if (isa<PseudoProbeInst>(Val: I))
1534 return 0;
1535 unsigned Flags = 0;
1536 if (I->mayReadFromMemory())
1537 Flags |= SkipReadMem;
1538 // We can't arbitrarily move around allocas, e.g. moving allocas (especially
1539 // inalloca) across stacksave/stackrestore boundaries.
1540 if (I->mayHaveSideEffects() || isa<AllocaInst>(Val: I))
1541 Flags |= SkipSideEffect;
1542 if (!isGuaranteedToTransferExecutionToSuccessor(I))
1543 Flags |= SkipImplicitControlFlow;
1544 return Flags;
1545}
1546
1547// Returns true if it is safe to reorder an instruction across preceding
1548// instructions in a basic block.
1549static bool isSafeToHoistInstr(Instruction *I, unsigned Flags) {
1550 // Don't reorder a store over a load.
1551 if ((Flags & SkipReadMem) && I->mayWriteToMemory())
1552 return false;
1553
1554 // If we have seen an instruction with side effects, it's unsafe to reorder an
1555 // instruction which reads memory or itself has side effects.
1556 if ((Flags & SkipSideEffect) &&
1557 (I->mayReadFromMemory() || I->mayHaveSideEffects() || isa<AllocaInst>(Val: I)))
1558 return false;
1559
1560 // Reordering across an instruction which does not necessarily transfer
1561 // control to the next instruction is speculation.
1562 if ((Flags & SkipImplicitControlFlow) && !isSafeToSpeculativelyExecute(I))
1563 return false;
1564
1565 // Hoisting of llvm.deoptimize is only legal together with the next return
1566 // instruction, which this pass is not always able to do.
1567 if (auto *CB = dyn_cast<CallBase>(Val: I))
1568 if (CB->getIntrinsicID() == Intrinsic::experimental_deoptimize)
1569 return false;
1570
1571 // It's also unsafe/illegal to hoist an instruction above its instruction
1572 // operands
1573 BasicBlock *BB = I->getParent();
1574 for (Value *Op : I->operands()) {
1575 if (auto *J = dyn_cast<Instruction>(Val: Op))
1576 if (J->getParent() == BB)
1577 return false;
1578 }
1579
1580 return true;
1581}
1582
1583static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I, bool PtrValueMayBeModified = false);
1584
1585/// Helper function for hoistCommonCodeFromSuccessors. Return true if identical
1586/// instructions \p I1 and \p I2 can and should be hoisted.
1587static bool shouldHoistCommonInstructions(Instruction *I1, Instruction *I2,
1588 const TargetTransformInfo &TTI) {
1589 // If we're going to hoist a call, make sure that the two instructions
1590 // we're commoning/hoisting are both marked with musttail, or neither of
1591 // them is marked as such. Otherwise, we might end up in a situation where
1592 // we hoist from a block where the terminator is a `ret` to a block where
1593 // the terminator is a `br`, and `musttail` calls expect to be followed by
1594 // a return.
1595 auto *C1 = dyn_cast<CallInst>(Val: I1);
1596 auto *C2 = dyn_cast<CallInst>(Val: I2);
1597 if (C1 && C2)
1598 if (C1->isMustTailCall() != C2->isMustTailCall())
1599 return false;
1600
1601 if (!TTI.isProfitableToHoist(I: I1) || !TTI.isProfitableToHoist(I: I2))
1602 return false;
1603
1604 // If any of the two call sites has nomerge or convergent attribute, stop
1605 // hoisting.
1606 if (const auto *CB1 = dyn_cast<CallBase>(Val: I1))
1607 if (CB1->cannotMerge() || CB1->isConvergent())
1608 return false;
1609 if (const auto *CB2 = dyn_cast<CallBase>(Val: I2))
1610 if (CB2->cannotMerge() || CB2->isConvergent())
1611 return false;
1612
1613 return true;
1614}
1615
1616/// Hoists DbgVariableRecords from \p I1 and \p OtherInstrs that are identical
1617/// in lock-step to \p TI. This matches how dbg.* intrinsics are hoisting in
1618/// hoistCommonCodeFromSuccessors. e.g. The input:
1619/// I1 DVRs: { x, z },
1620/// OtherInsts: { I2 DVRs: { x, y, z } }
1621/// would result in hoisting only DbgVariableRecord x.
1622static void hoistLockstepIdenticalDbgVariableRecords(
1623 Instruction *TI, Instruction *I1,
1624 SmallVectorImpl<Instruction *> &OtherInsts) {
1625 if (!I1->hasDbgRecords())
1626 return;
1627 using CurrentAndEndIt =
1628 std::pair<DbgRecord::self_iterator, DbgRecord::self_iterator>;
1629 // Vector of {Current, End} iterators.
1630 SmallVector<CurrentAndEndIt> Itrs;
1631 Itrs.reserve(N: OtherInsts.size() + 1);
1632 // Helper lambdas for lock-step checks:
1633 // Return true if this Current == End.
1634 auto atEnd = [](const CurrentAndEndIt &Pair) {
1635 return Pair.first == Pair.second;
1636 };
1637 // Return true if all Current are identical.
1638 auto allIdentical = [](const SmallVector<CurrentAndEndIt> &Itrs) {
1639 return all_of(Range: make_first_range(c: ArrayRef(Itrs).drop_front()),
1640 P: [&](DbgRecord::self_iterator I) {
1641 return Itrs[0].first->isIdenticalToWhenDefined(R: *I);
1642 });
1643 };
1644
1645 // Collect the iterators.
1646 Itrs.push_back(
1647 Elt: {I1->getDbgRecordRange().begin(), I1->getDbgRecordRange().end()});
1648 for (Instruction *Other : OtherInsts) {
1649 if (!Other->hasDbgRecords())
1650 return;
1651 Itrs.push_back(
1652 Elt: {Other->getDbgRecordRange().begin(), Other->getDbgRecordRange().end()});
1653 }
1654
1655 // Iterate in lock-step until any of the DbgRecord lists are exausted. If
1656 // the lock-step DbgRecord are identical, hoist all of them to TI.
1657 // This replicates the dbg.* intrinsic behaviour in
1658 // hoistCommonCodeFromSuccessors.
1659 while (none_of(Range&: Itrs, P: atEnd)) {
1660 bool HoistDVRs = allIdentical(Itrs);
1661 for (CurrentAndEndIt &Pair : Itrs) {
1662 // Increment Current iterator now as we may be about to move the
1663 // DbgRecord.
1664 DbgRecord &DR = *Pair.first++;
1665 if (HoistDVRs) {
1666 DR.removeFromParent();
1667 TI->getParent()->insertDbgRecordBefore(DR: &DR, Here: TI->getIterator());
1668 }
1669 }
1670 }
1671}
1672
1673static bool areIdenticalUpToCommutativity(const Instruction *I1,
1674 const Instruction *I2) {
1675 if (I1->isIdenticalToWhenDefined(I: I2, /*IntersectAttrs=*/true))
1676 return true;
1677
1678 if (auto *Cmp1 = dyn_cast<CmpInst>(Val: I1))
1679 if (auto *Cmp2 = dyn_cast<CmpInst>(Val: I2))
1680 return Cmp1->getPredicate() == Cmp2->getSwappedPredicate() &&
1681 Cmp1->getOperand(i_nocapture: 0) == Cmp2->getOperand(i_nocapture: 1) &&
1682 Cmp1->getOperand(i_nocapture: 1) == Cmp2->getOperand(i_nocapture: 0);
1683
1684 if (I1->isCommutative() && I1->isSameOperationAs(I: I2)) {
1685 return I1->getOperand(i: 0) == I2->getOperand(i: 1) &&
1686 I1->getOperand(i: 1) == I2->getOperand(i: 0) &&
1687 equal(LRange: drop_begin(RangeOrContainer: I1->operands(), N: 2), RRange: drop_begin(RangeOrContainer: I2->operands(), N: 2));
1688 }
1689
1690 return false;
1691}
1692
1693/// If the target supports conditional faulting,
1694/// we look for the following pattern:
1695/// \code
1696/// BB:
1697/// ...
1698/// %cond = icmp ult %x, %y
1699/// br i1 %cond, label %TrueBB, label %FalseBB
1700/// FalseBB:
1701/// store i32 1, ptr %q, align 4
1702/// ...
1703/// TrueBB:
1704/// %maskedloadstore = load i32, ptr %b, align 4
1705/// store i32 %maskedloadstore, ptr %p, align 4
1706/// ...
1707/// \endcode
1708///
1709/// and transform it into:
1710///
1711/// \code
1712/// BB:
1713/// ...
1714/// %cond = icmp ult %x, %y
1715/// %maskedloadstore = cload i32, ptr %b, %cond
1716/// cstore i32 %maskedloadstore, ptr %p, %cond
1717/// cstore i32 1, ptr %q, ~%cond
1718/// br i1 %cond, label %TrueBB, label %FalseBB
1719/// FalseBB:
1720/// ...
1721/// TrueBB:
1722/// ...
1723/// \endcode
1724///
1725/// where cload/cstore are represented by llvm.masked.load/store intrinsics,
1726/// e.g.
1727///
1728/// \code
1729/// %vcond = bitcast i1 %cond to <1 x i1>
1730/// %v0 = call <1 x i32> @llvm.masked.load.v1i32.p0
1731/// (ptr %b, i32 4, <1 x i1> %vcond, <1 x i32> poison)
1732/// %maskedloadstore = bitcast <1 x i32> %v0 to i32
1733/// call void @llvm.masked.store.v1i32.p0
1734/// (<1 x i32> %v0, ptr %p, i32 4, <1 x i1> %vcond)
1735/// %cond.not = xor i1 %cond, true
1736/// %vcond.not = bitcast i1 %cond.not to <1 x i>
1737/// call void @llvm.masked.store.v1i32.p0
1738/// (<1 x i32> <i32 1>, ptr %q, i32 4, <1x i1> %vcond.not)
1739/// \endcode
1740///
1741/// So we need to turn hoisted load/store into cload/cstore.
1742///
1743/// \param BI The branch instruction.
1744/// \param SpeculatedConditionalLoadsStores The load/store instructions that
1745/// will be speculated.
1746/// \param Invert indicates if speculates FalseBB. Only used in triangle CFG.
1747static void hoistConditionalLoadsStores(
1748 CondBrInst *BI,
1749 SmallVectorImpl<Instruction *> &SpeculatedConditionalLoadsStores,
1750 std::optional<bool> Invert, Instruction *Sel) {
1751 auto &Context = BI->getParent()->getContext();
1752 auto *VCondTy = FixedVectorType::get(ElementType: Type::getInt1Ty(C&: Context), NumElts: 1);
1753 auto *Cond = BI->getCondition();
1754 // Construct the condition if needed.
1755 BasicBlock *BB = BI->getParent();
1756 Value *Mask = nullptr;
1757 Value *MaskFalse = nullptr;
1758 Value *MaskTrue = nullptr;
1759 if (Invert.has_value()) {
1760 IRBuilder<> Builder(Sel ? Sel : SpeculatedConditionalLoadsStores.back());
1761 Mask = Builder.CreateBitCast(
1762 V: *Invert ? Builder.CreateXor(LHS: Cond, RHS: ConstantInt::getTrue(Context)) : Cond,
1763 DestTy: VCondTy);
1764 } else {
1765 IRBuilder<> Builder(BI);
1766 MaskFalse = Builder.CreateBitCast(
1767 V: Builder.CreateXor(LHS: Cond, RHS: ConstantInt::getTrue(Context)), DestTy: VCondTy);
1768 MaskTrue = Builder.CreateBitCast(V: Cond, DestTy: VCondTy);
1769 }
1770 auto PeekThroughBitcasts = [](Value *V) {
1771 while (auto *BitCast = dyn_cast<BitCastInst>(Val: V))
1772 V = BitCast->getOperand(i_nocapture: 0);
1773 return V;
1774 };
1775 for (auto *I : SpeculatedConditionalLoadsStores) {
1776 IRBuilder<> Builder(Invert.has_value() ? I : BI);
1777 if (!Invert.has_value())
1778 Mask = I->getParent() == BI->getSuccessor(i: 0) ? MaskTrue : MaskFalse;
1779 // We currently assume conditional faulting load/store is supported for
1780 // scalar types only when creating new instructions. This can be easily
1781 // extended for vector types in the future.
1782 assert(!getLoadStoreType(I)->isVectorTy() && "not implemented");
1783 auto *Op0 = I->getOperand(i: 0);
1784 CallInst *MaskedLoadStore = nullptr;
1785 if (auto *LI = dyn_cast<LoadInst>(Val: I)) {
1786 // Handle Load.
1787 auto *Ty = I->getType();
1788 PHINode *PN = nullptr;
1789 Value *PassThru = nullptr;
1790 if (Invert.has_value())
1791 for (User *U : I->users()) {
1792 if ((PN = dyn_cast<PHINode>(Val: U))) {
1793 PassThru = Builder.CreateBitCast(
1794 V: PeekThroughBitcasts(PN->getIncomingValueForBlock(BB)),
1795 DestTy: FixedVectorType::get(ElementType: Ty, NumElts: 1));
1796 } else if (auto *Ins = cast<Instruction>(Val: U);
1797 Sel && Ins->getParent() == BB) {
1798 // This happens when store or/and a speculative instruction between
1799 // load and store were hoisted to the BB. Make sure the masked load
1800 // inserted before its use.
1801 // We assume there's one of such use.
1802 Builder.SetInsertPoint(Ins);
1803 }
1804 }
1805 MaskedLoadStore = Builder.CreateMaskedLoad(
1806 Ty: FixedVectorType::get(ElementType: Ty, NumElts: 1), Ptr: Op0, Alignment: LI->getAlign(), Mask, PassThru);
1807 Value *NewLoadStore = Builder.CreateBitCast(V: MaskedLoadStore, DestTy: Ty);
1808 if (PN)
1809 PN->setIncomingValue(i: PN->getBasicBlockIndex(BB), V: NewLoadStore);
1810 I->replaceAllUsesWith(V: NewLoadStore);
1811 } else {
1812 // Handle Store.
1813 auto *StoredVal = Builder.CreateBitCast(
1814 V: PeekThroughBitcasts(Op0), DestTy: FixedVectorType::get(ElementType: Op0->getType(), NumElts: 1));
1815 MaskedLoadStore = Builder.CreateMaskedStore(
1816 Val: StoredVal, Ptr: I->getOperand(i: 1), Alignment: cast<StoreInst>(Val: I)->getAlign(), Mask);
1817 }
1818 // For non-debug metadata, only !annotation, !range, !nonnull and !align are
1819 // kept when hoisting (see Instruction::dropUBImplyingAttrsAndMetadata).
1820 //
1821 // !nonnull, !align : Not support pointer type, no need to keep.
1822 // !range: Load type is changed from scalar to vector, but the metadata on
1823 // vector specifies a per-element range, so the semantics stay the
1824 // same. Keep it.
1825 // !annotation: Not impact semantics. Keep it.
1826 if (const MDNode *Ranges = I->getMetadata(KindID: LLVMContext::MD_range))
1827 MaskedLoadStore->addRangeRetAttr(CR: getConstantRangeFromMetadata(RangeMD: *Ranges));
1828 I->dropUBImplyingAttrsAndUnknownMetadata(KnownIDs: {LLVMContext::MD_annotation});
1829 // FIXME: DIAssignID is not supported for masked store yet.
1830 // (Verifier::visitDIAssignIDMetadata)
1831 at::deleteAssignmentMarkers(Inst: I);
1832 I->eraseMetadataIf(Pred: [](unsigned MDKind, MDNode *Node) {
1833 return Node->getMetadataID() == Metadata::DIAssignIDKind;
1834 });
1835 MaskedLoadStore->copyMetadata(SrcInst: *I);
1836 I->eraseFromParent();
1837 }
1838}
1839
1840static bool isSafeCheapLoadStore(const Instruction *I,
1841 const TargetTransformInfo &TTI) {
1842 // Not handle volatile or atomic.
1843 bool IsStore = false;
1844 if (auto *L = dyn_cast<LoadInst>(Val: I)) {
1845 if (!L->isSimple() || !HoistLoadsWithCondFaulting)
1846 return false;
1847 } else if (auto *S = dyn_cast<StoreInst>(Val: I)) {
1848 if (!S->isSimple() || !HoistStoresWithCondFaulting)
1849 return false;
1850 IsStore = true;
1851 } else
1852 return false;
1853
1854 // llvm.masked.load/store use i32 for alignment while load/store use i64.
1855 // That's why we have the alignment limitation.
1856 // FIXME: Update the prototype of the intrinsics?
1857 return TTI.hasConditionalLoadStoreForType(Ty: getLoadStoreType(I), IsStore) &&
1858 getLoadStoreAlignment(I) < Value::MaximumAlignment;
1859}
1860
1861/// Hoist any common code in the successor blocks up into the block. This
1862/// function guarantees that BB dominates all successors. If AllInstsEqOnly is
1863/// given, only perform hoisting in case all successors blocks contain matching
1864/// instructions only. In that case, all instructions can be hoisted and the
1865/// original branch will be replaced and selects for PHIs are added.
1866bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(Instruction *TI,
1867 bool AllInstsEqOnly) {
1868 // This does very trivial matching, with limited scanning, to find identical
1869 // instructions in the two blocks. In particular, we don't want to get into
1870 // O(N1*N2*...) situations here where Ni are the sizes of these successors. As
1871 // such, we currently just scan for obviously identical instructions in an
1872 // identical order, possibly separated by the same number of non-identical
1873 // instructions.
1874 BasicBlock *BB = TI->getParent();
1875 unsigned int SuccSize = succ_size(BB);
1876 if (SuccSize < 2)
1877 return false;
1878
1879 // If either of the blocks has it's address taken, then we can't do this fold,
1880 // because the code we'd hoist would no longer run when we jump into the block
1881 // by it's address.
1882 SmallSetVector<BasicBlock *, 4> UniqueSuccessors(from_range, successors(BB));
1883 for (auto *Succ : UniqueSuccessors) {
1884 if (Succ->hasAddressTaken())
1885 return false;
1886 // Use getUniquePredecessor instead of getSinglePredecessor to support
1887 // multi-cases successors in switch.
1888 if (Succ->getUniquePredecessor())
1889 continue;
1890 // If Succ has >1 predecessors, continue to check if the Succ contains only
1891 // one `unreachable` inst. Since executing `unreachable` inst is an UB, we
1892 // can relax the condition based on the assumptiom that the program would
1893 // never enter Succ and trigger such an UB.
1894 if (isa<UnreachableInst>(Val: *Succ->begin()))
1895 continue;
1896 return false;
1897 }
1898 // The second of pair is a SkipFlags bitmask.
1899 using SuccIterPair = std::pair<BasicBlock::iterator, unsigned>;
1900 SmallVector<SuccIterPair, 8> SuccIterPairs;
1901 for (auto *Succ : UniqueSuccessors) {
1902 BasicBlock::iterator SuccItr = Succ->begin();
1903 if (isa<PHINode>(Val: *SuccItr))
1904 return false;
1905 SuccIterPairs.push_back(Elt: SuccIterPair(SuccItr, 0));
1906 }
1907
1908 if (AllInstsEqOnly) {
1909 // Check if all instructions in the successor blocks match. This allows
1910 // hoisting all instructions and removing the blocks we are hoisting from,
1911 // so does not add any new instructions.
1912
1913 // Check if sizes and terminators of all successors match.
1914 unsigned Size0 = UniqueSuccessors[0]->size();
1915 Instruction *Term0 = UniqueSuccessors[0]->getTerminator();
1916 bool AllSame =
1917 all_of(Range: drop_begin(RangeOrContainer&: UniqueSuccessors), P: [Term0, Size0](BasicBlock *Succ) {
1918 return Succ->getTerminator()->isIdenticalTo(I: Term0) &&
1919 Succ->size() == Size0;
1920 });
1921 if (!AllSame)
1922 return false;
1923 LockstepReverseIterator<true> LRI(UniqueSuccessors.getArrayRef());
1924 while (LRI.isValid()) {
1925 Instruction *I0 = (*LRI)[0];
1926 if (any_of(Range: *LRI, P: [I0](Instruction *I) {
1927 return !areIdenticalUpToCommutativity(I1: I0, I2: I);
1928 })) {
1929 return false;
1930 }
1931 --LRI;
1932 }
1933 // Now we know that all instructions in all successors can be hoisted. Let
1934 // the loop below handle the hoisting.
1935 }
1936
1937 // Count how many instructions were not hoisted so far. There's a limit on how
1938 // many instructions we skip, serving as a compilation time control as well as
1939 // preventing excessive increase of life ranges.
1940 unsigned NumSkipped = 0;
1941 // If we find an unreachable instruction at the beginning of a basic block, we
1942 // can still hoist instructions from the rest of the basic blocks.
1943 if (SuccIterPairs.size() > 2) {
1944 erase_if(C&: SuccIterPairs,
1945 P: [](const auto &Pair) { return isa<UnreachableInst>(Pair.first); });
1946 if (SuccIterPairs.size() < 2)
1947 return false;
1948 }
1949
1950 bool Changed = false;
1951
1952 for (;;) {
1953 auto *SuccIterPairBegin = SuccIterPairs.begin();
1954 auto &BB1ItrPair = *SuccIterPairBegin++;
1955 auto OtherSuccIterPairRange =
1956 iterator_range(SuccIterPairBegin, SuccIterPairs.end());
1957 auto OtherSuccIterRange = make_first_range(c&: OtherSuccIterPairRange);
1958
1959 Instruction *I1 = &*BB1ItrPair.first;
1960
1961 bool AllInstsAreIdentical = true;
1962 bool HasTerminator = I1->isTerminator();
1963 for (auto &SuccIter : OtherSuccIterRange) {
1964 Instruction *I2 = &*SuccIter;
1965 HasTerminator |= I2->isTerminator();
1966 if (AllInstsAreIdentical && (!areIdenticalUpToCommutativity(I1, I2) ||
1967 MMRAMetadata(*I1) != MMRAMetadata(*I2)))
1968 AllInstsAreIdentical = false;
1969 }
1970
1971 SmallVector<Instruction *, 8> OtherInsts;
1972 for (auto &SuccIter : OtherSuccIterRange)
1973 OtherInsts.push_back(Elt: &*SuccIter);
1974
1975 // If we are hoisting the terminator instruction, don't move one (making a
1976 // broken BB), instead clone it, and remove BI.
1977 if (HasTerminator) {
1978 // Even if BB, which contains only one unreachable instruction, is ignored
1979 // at the beginning of the loop, we can hoist the terminator instruction.
1980 // If any instructions remain in the block, we cannot hoist terminators.
1981 if (NumSkipped || !AllInstsAreIdentical) {
1982 hoistLockstepIdenticalDbgVariableRecords(TI, I1, OtherInsts);
1983 return Changed;
1984 }
1985
1986 return hoistSuccIdenticalTerminatorToSwitchOrIf(
1987 TI, I1, OtherSuccTIs&: OtherInsts, UniqueSuccessors: UniqueSuccessors.getArrayRef()) ||
1988 Changed;
1989 }
1990
1991 if (AllInstsAreIdentical) {
1992 unsigned SkipFlagsBB1 = BB1ItrPair.second;
1993 AllInstsAreIdentical =
1994 isSafeToHoistInstr(I: I1, Flags: SkipFlagsBB1) &&
1995 all_of(Range&: OtherSuccIterPairRange, P: [=](const auto &Pair) {
1996 Instruction *I2 = &*Pair.first;
1997 unsigned SkipFlagsBB2 = Pair.second;
1998 // Even if the instructions are identical, it may not
1999 // be safe to hoist them if we have skipped over
2000 // instructions with side effects or their operands
2001 // weren't hoisted.
2002 return isSafeToHoistInstr(I: I2, Flags: SkipFlagsBB2) &&
2003 shouldHoistCommonInstructions(I1, I2, TTI);
2004 });
2005 }
2006
2007 // A musttail call must be immediately followed by a ret, so hoisting is
2008 // only legal if its ret is hoisted with it on the next iteration. That is,
2009 // no instruction has been skipped (the entire successor can be hoisted into
2010 // the predecessor) and the call is directly followed by a ret.
2011 if (auto *CI = dyn_cast<CallInst>(Val: I1);
2012 AllInstsAreIdentical && CI && CI->isMustTailCall()) {
2013 AllInstsAreIdentical =
2014 NumSkipped == 0 && all_of(Range&: SuccIterPairs, P: [](const SuccIterPair &P) {
2015 return isa<ReturnInst>(Val: *std::next(x: P.first));
2016 });
2017 }
2018
2019 if (AllInstsAreIdentical) {
2020 BB1ItrPair.first++;
2021 // For a normal instruction, we just move one to right before the
2022 // branch, then replace all uses of the other with the first. Finally,
2023 // we remove the now redundant second instruction.
2024 hoistLockstepIdenticalDbgVariableRecords(TI, I1, OtherInsts);
2025 // We've just hoisted DbgVariableRecords; move I1 after them (before TI)
2026 // and leave any that were not hoisted behind (by calling moveBefore
2027 // rather than moveBeforePreserving).
2028 I1->moveBefore(InsertPos: TI->getIterator());
2029 for (auto &SuccIter : OtherSuccIterRange) {
2030 Instruction *I2 = &*SuccIter++;
2031 assert(I2 != I1);
2032 if (!I2->use_empty())
2033 I2->replaceAllUsesWith(V: I1);
2034 I1->andIRFlags(V: I2);
2035 if (auto *CB = dyn_cast<CallBase>(Val: I1)) {
2036 bool Success = CB->tryIntersectAttributes(Other: cast<CallBase>(Val: I2));
2037 assert(Success && "We should not be trying to hoist callbases "
2038 "with non-intersectable attributes");
2039 // For NDEBUG Compile.
2040 (void)Success;
2041 }
2042
2043 combineMetadataForCSE(K: I1, J: I2, DoesKMove: true);
2044 // I1 and I2 are being combined into a single instruction. Its debug
2045 // location is the merged locations of the original instructions.
2046 I1->applyMergedLocation(LocA: I1->getDebugLoc(), LocB: I2->getDebugLoc());
2047 I2->eraseFromParent();
2048 }
2049 if (!Changed)
2050 NumHoistCommonCode += SuccIterPairs.size();
2051 Changed = true;
2052 NumHoistCommonInstrs += SuccIterPairs.size();
2053 } else {
2054 if (NumSkipped >= HoistCommonSkipLimit) {
2055 hoistLockstepIdenticalDbgVariableRecords(TI, I1, OtherInsts);
2056 return Changed;
2057 }
2058 // We are about to skip over a pair of non-identical instructions. Record
2059 // if any have characteristics that would prevent reordering instructions
2060 // across them.
2061 for (auto &SuccIterPair : SuccIterPairs) {
2062 Instruction *I = &*SuccIterPair.first++;
2063 SuccIterPair.second |= skippedInstrFlags(I);
2064 }
2065 ++NumSkipped;
2066 }
2067 }
2068}
2069
2070bool SimplifyCFGOpt::hoistSuccIdenticalTerminatorToSwitchOrIf(
2071 Instruction *TI, Instruction *I1,
2072 SmallVectorImpl<Instruction *> &OtherSuccTIs,
2073 ArrayRef<BasicBlock *> UniqueSuccessors) {
2074
2075 auto *BI = dyn_cast<CondBrInst>(Val: TI);
2076
2077 bool Changed = false;
2078 BasicBlock *TIParent = TI->getParent();
2079 BasicBlock *BB1 = I1->getParent();
2080
2081 // Use only for an if statement.
2082 auto *I2 = *OtherSuccTIs.begin();
2083 auto *BB2 = I2->getParent();
2084 if (BI) {
2085 assert(OtherSuccTIs.size() == 1);
2086 assert(BI->getSuccessor(0) == I1->getParent());
2087 assert(BI->getSuccessor(1) == I2->getParent());
2088 }
2089
2090 // In the case of an if statement, we try to hoist an invoke.
2091 // FIXME: Can we define a safety predicate for CallBr?
2092 // FIXME: Test case llvm/test/Transforms/SimplifyCFG/2009-06-15-InvokeCrash.ll
2093 // removed in 4c923b3b3fd0ac1edebf0603265ca3ba51724937 commit?
2094 if (isa<InvokeInst>(Val: I1) && (!BI || !isSafeToHoistInvoke(BB1, BB2, I1, I2)))
2095 return false;
2096
2097 // TODO: callbr hoisting currently disabled pending further study.
2098 if (isa<CallBrInst>(Val: I1))
2099 return false;
2100
2101 for (BasicBlock *Succ : successors(BB: BB1)) {
2102 for (PHINode &PN : Succ->phis()) {
2103 Value *BB1V = PN.getIncomingValueForBlock(BB: BB1);
2104 for (Instruction *OtherSuccTI : OtherSuccTIs) {
2105 Value *BB2V = PN.getIncomingValueForBlock(BB: OtherSuccTI->getParent());
2106 if (BB1V == BB2V)
2107 continue;
2108
2109 // In the case of an if statement, check for
2110 // passingValueIsAlwaysUndefined here because we would rather eliminate
2111 // undefined control flow then converting it to a select.
2112 if (!BI || passingValueIsAlwaysUndefined(V: BB1V, I: &PN) ||
2113 passingValueIsAlwaysUndefined(V: BB2V, I: &PN))
2114 return false;
2115 }
2116 }
2117 }
2118
2119 // Hoist DbgVariableRecords attached to the terminator to match dbg.*
2120 // intrinsic hoisting behaviour in hoistCommonCodeFromSuccessors.
2121 hoistLockstepIdenticalDbgVariableRecords(TI, I1, OtherInsts&: OtherSuccTIs);
2122 // Clone the terminator and hoist it into the pred, without any debug info.
2123 Instruction *NT = I1->clone();
2124 NT->insertInto(ParentBB: TIParent, It: TI->getIterator());
2125 if (!NT->getType()->isVoidTy()) {
2126 I1->replaceAllUsesWith(V: NT);
2127 for (Instruction *OtherSuccTI : OtherSuccTIs)
2128 OtherSuccTI->replaceAllUsesWith(V: NT);
2129 NT->takeName(V: I1);
2130 }
2131 Changed = true;
2132 NumHoistCommonInstrs += OtherSuccTIs.size() + 1;
2133
2134 // Ensure terminator gets a debug location, even an unknown one, in case
2135 // it involves inlinable calls.
2136 SmallVector<DebugLoc, 4> Locs;
2137 Locs.push_back(Elt: I1->getDebugLoc());
2138 for (auto *OtherSuccTI : OtherSuccTIs)
2139 Locs.push_back(Elt: OtherSuccTI->getDebugLoc());
2140 NT->setDebugLoc(DebugLoc::getMergedLocations(Locs));
2141
2142 // PHIs created below will adopt NT's merged DebugLoc.
2143 IRBuilder<NoFolder> Builder(NT);
2144
2145 // In the case of an if statement, hoisting one of the terminators from our
2146 // successor is a great thing. Unfortunately, the successors of the if/else
2147 // blocks may have PHI nodes in them. If they do, all PHI entries for BB1/BB2
2148 // must agree for all PHI nodes, so we insert select instruction to compute
2149 // the final result.
2150 if (BI) {
2151 std::map<std::pair<Value *, Value *>, SelectInst *> InsertedSelects;
2152 for (BasicBlock *Succ : successors(BB: BB1)) {
2153 for (PHINode &PN : Succ->phis()) {
2154 Value *BB1V = PN.getIncomingValueForBlock(BB: BB1);
2155 Value *BB2V = PN.getIncomingValueForBlock(BB: BB2);
2156 if (BB1V == BB2V)
2157 continue;
2158
2159 // These values do not agree. Insert a select instruction before NT
2160 // that determines the right value.
2161 SelectInst *&SI = InsertedSelects[std::make_pair(x&: BB1V, y&: BB2V)];
2162 if (!SI) {
2163 // Propagate fast-math-flags from phi node to its replacement select.
2164 SI = cast<SelectInst>(Val: Builder.CreateSelectFMF(
2165 C: BI->getCondition(), True: BB1V, False: BB2V,
2166 FMFSource: isa<FPMathOperator>(Val: PN) ? &PN : nullptr,
2167 Name: BB1V->getName() + "." + BB2V->getName(), MDFrom: BI));
2168 }
2169
2170 // Make the PHI node use the select for all incoming values for BB1/BB2
2171 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
2172 if (PN.getIncomingBlock(i) == BB1 || PN.getIncomingBlock(i) == BB2)
2173 PN.setIncomingValue(i, V: SI);
2174 }
2175 }
2176 }
2177
2178 SmallVector<DominatorTree::UpdateType, 4> Updates;
2179
2180 // Update any PHI nodes in our new successors.
2181 for (BasicBlock *Succ : successors(BB: BB1)) {
2182 addPredecessorToBlock(Succ, NewPred: TIParent, ExistPred: BB1);
2183 if (DTU)
2184 Updates.push_back(Elt: {DominatorTree::Insert, TIParent, Succ});
2185 }
2186
2187 if (DTU) {
2188 // TI might be a switch with multi-cases destination, so we need to care for
2189 // the duplication of successors.
2190 for (BasicBlock *Succ : UniqueSuccessors)
2191 Updates.push_back(Elt: {DominatorTree::Delete, TIParent, Succ});
2192 }
2193
2194 eraseTerminatorAndDCECond(TI);
2195 if (DTU)
2196 DTU->applyUpdates(Updates);
2197 return Changed;
2198}
2199
2200// TODO: Refine this. This should avoid cases like turning constant memcpy sizes
2201// into variables.
2202static bool replacingOperandWithVariableIsCheap(const Instruction *I,
2203 int OpIdx) {
2204 // Divide/Remainder by constant is typically much cheaper than by variable.
2205 if (I->isIntDivRem())
2206 return OpIdx != 1;
2207 return !isa<IntrinsicInst>(Val: I);
2208}
2209
2210// All instructions in Insts belong to different blocks that all unconditionally
2211// branch to a common successor. Analyze each instruction and return true if it
2212// would be possible to sink them into their successor, creating one common
2213// instruction instead. For every value that would be required to be provided by
2214// PHI node (because an operand varies in each input block), add to PHIOperands.
2215static bool canSinkInstructions(
2216 ArrayRef<Instruction *> Insts,
2217 DenseMap<const Use *, SmallVector<Value *, 4>> &PHIOperands) {
2218 // Prune out obviously bad instructions to move. Each instruction must have
2219 // the same number of uses, and we check later that the uses are consistent.
2220 std::optional<unsigned> NumUses;
2221 for (auto *I : Insts) {
2222 // These instructions may change or break semantics if moved.
2223 if (isa<PHINode>(Val: I) || I->isEHPad() || isa<AllocaInst>(Val: I) ||
2224 I->getType()->isTokenTy())
2225 return false;
2226
2227 // Do not try to sink an instruction in an infinite loop - it can cause
2228 // this algorithm to infinite loop.
2229 if (I->getParent()->getSingleSuccessor() == I->getParent())
2230 return false;
2231
2232 // Conservatively return false if I is an inline-asm instruction. Sinking
2233 // and merging inline-asm instructions can potentially create arguments
2234 // that cannot satisfy the inline-asm constraints.
2235 // If the instruction has nomerge or convergent attribute, return false.
2236 if (const auto *C = dyn_cast<CallBase>(Val: I))
2237 if (C->isInlineAsm() || C->cannotMerge() || C->isConvergent())
2238 return false;
2239
2240 if (!NumUses)
2241 NumUses = I->getNumUses();
2242 else if (NumUses != I->getNumUses())
2243 return false;
2244 }
2245
2246 const Instruction *I0 = Insts.front();
2247 const auto I0MMRA = MMRAMetadata(*I0);
2248 for (auto *I : Insts) {
2249 if (!I->isSameOperationAs(I: I0, flags: Instruction::CompareUsingIntersectedAttrs))
2250 return false;
2251
2252 // Treat MMRAs conservatively. This pass can be quite aggressive and
2253 // could drop a lot of MMRAs otherwise.
2254 if (MMRAMetadata(*I) != I0MMRA)
2255 return false;
2256 }
2257
2258 // Uses must be consistent: If I0 is used in a phi node in the sink target,
2259 // then the other phi operands must match the instructions from Insts. This
2260 // also has to hold true for any phi nodes that would be created as a result
2261 // of sinking. Both of these cases are represented by PhiOperands.
2262 for (const Use &U : I0->uses()) {
2263 auto It = PHIOperands.find(Val: &U);
2264 if (It == PHIOperands.end())
2265 // There may be uses in other blocks when sinking into a loop header.
2266 return false;
2267 if (!equal(LRange&: Insts, RRange&: It->second))
2268 return false;
2269 }
2270
2271 // For calls to be sinkable, they must all be indirect, or have same callee.
2272 // I.e. if we have two direct calls to different callees, we don't want to
2273 // turn that into an indirect call. Likewise, if we have an indirect call,
2274 // and a direct call, we don't actually want to have a single indirect call.
2275 if (isa<CallBase>(Val: I0)) {
2276 auto IsIndirectCall = [](const Instruction *I) {
2277 return cast<CallBase>(Val: I)->isIndirectCall();
2278 };
2279 bool HaveIndirectCalls = any_of(Range&: Insts, P: IsIndirectCall);
2280 bool AllCallsAreIndirect = all_of(Range&: Insts, P: IsIndirectCall);
2281 if (HaveIndirectCalls) {
2282 if (!AllCallsAreIndirect)
2283 return false;
2284 } else {
2285 // All callees must be identical.
2286 Value *Callee = nullptr;
2287 for (const Instruction *I : Insts) {
2288 Value *CurrCallee = cast<CallBase>(Val: I)->getCalledOperand();
2289 if (!Callee)
2290 Callee = CurrCallee;
2291 else if (Callee != CurrCallee)
2292 return false;
2293 }
2294 }
2295 }
2296
2297 for (unsigned OI = 0, OE = I0->getNumOperands(); OI != OE; ++OI) {
2298 Value *Op = I0->getOperand(i: OI);
2299 auto SameAsI0 = [&I0, OI](const Instruction *I) {
2300 assert(I->getNumOperands() == I0->getNumOperands());
2301 return I->getOperand(i: OI) == I0->getOperand(i: OI);
2302 };
2303 if (!all_of(Range&: Insts, P: SameAsI0)) {
2304 if ((isa<Constant>(Val: Op) && !replacingOperandWithVariableIsCheap(I: I0, OpIdx: OI)) ||
2305 !canReplaceOperandWithVariable(I: I0, OpIdx: OI))
2306 // We can't create a PHI from this GEP.
2307 return false;
2308 auto &Ops = PHIOperands[&I0->getOperandUse(i: OI)];
2309 for (auto *I : Insts)
2310 Ops.push_back(Elt: I->getOperand(i: OI));
2311 }
2312 }
2313 return true;
2314}
2315
2316// Assuming canSinkInstructions(Blocks) has returned true, sink the last
2317// instruction of every block in Blocks to their common successor, commoning
2318// into one instruction.
2319static void sinkLastInstruction(ArrayRef<BasicBlock*> Blocks) {
2320 auto *BBEnd = Blocks[0]->getTerminator()->getSuccessor(Idx: 0);
2321
2322 // canSinkInstructions returning true guarantees that every block has at
2323 // least one non-terminator instruction.
2324 SmallVector<Instruction*,4> Insts;
2325 for (auto *BB : Blocks) {
2326 Instruction *I = BB->getTerminator();
2327 I = I->getPrevNode();
2328 Insts.push_back(Elt: I);
2329 }
2330
2331 // We don't need to do any more checking here; canSinkInstructions should
2332 // have done it all for us.
2333 SmallVector<Value*, 4> NewOperands;
2334 Instruction *I0 = Insts.front();
2335 for (unsigned O = 0, E = I0->getNumOperands(); O != E; ++O) {
2336 // This check is different to that in canSinkInstructions. There, we
2337 // cared about the global view once simplifycfg (and instcombine) have
2338 // completed - it takes into account PHIs that become trivially
2339 // simplifiable. However here we need a more local view; if an operand
2340 // differs we create a PHI and rely on instcombine to clean up the very
2341 // small mess we may make.
2342 bool NeedPHI = any_of(Range&: Insts, P: [&I0, O](const Instruction *I) {
2343 return I->getOperand(i: O) != I0->getOperand(i: O);
2344 });
2345 if (!NeedPHI) {
2346 NewOperands.push_back(Elt: I0->getOperand(i: O));
2347 continue;
2348 }
2349
2350 // Create a new PHI in the successor block and populate it.
2351 auto *Op = I0->getOperand(i: O);
2352 assert(!Op->getType()->isTokenTy() && "Can't PHI tokens!");
2353 auto *PN =
2354 PHINode::Create(Ty: Op->getType(), NumReservedValues: Insts.size(), NameStr: Op->getName() + ".sink");
2355 PN->insertBefore(InsertPos: BBEnd->begin());
2356 for (auto *I : Insts)
2357 PN->addIncoming(V: I->getOperand(i: O), BB: I->getParent());
2358 NewOperands.push_back(Elt: PN);
2359 }
2360
2361 // Arbitrarily use I0 as the new "common" instruction; remap its operands
2362 // and move it to the start of the successor block.
2363 for (unsigned O = 0, E = I0->getNumOperands(); O != E; ++O)
2364 I0->getOperandUse(i: O).set(NewOperands[O]);
2365
2366 I0->moveBefore(BB&: *BBEnd, I: BBEnd->getFirstInsertionPt());
2367
2368 // Update metadata and IR flags, and merge debug locations.
2369 for (auto *I : Insts)
2370 if (I != I0) {
2371 // The debug location for the "common" instruction is the merged locations
2372 // of all the commoned instructions. We start with the original location
2373 // of the "common" instruction and iteratively merge each location in the
2374 // loop below.
2375 // This is an N-way merge, which will be inefficient if I0 is a CallInst.
2376 // However, as N-way merge for CallInst is rare, so we use simplified API
2377 // instead of using complex API for N-way merge.
2378 I0->applyMergedLocation(LocA: I0->getDebugLoc(), LocB: I->getDebugLoc());
2379 combineMetadataForCSE(K: I0, J: I, DoesKMove: true);
2380 I0->andIRFlags(V: I);
2381 if (auto *CB = dyn_cast<CallBase>(Val: I0)) {
2382 bool Success = CB->tryIntersectAttributes(Other: cast<CallBase>(Val: I));
2383 assert(Success && "We should not be trying to sink callbases "
2384 "with non-intersectable attributes");
2385 // For NDEBUG Compile.
2386 (void)Success;
2387 }
2388 }
2389
2390 for (User *U : make_early_inc_range(Range: I0->users())) {
2391 // canSinkLastInstruction checked that all instructions are only used by
2392 // phi nodes in a way that allows replacing the phi node with the common
2393 // instruction.
2394 auto *PN = cast<PHINode>(Val: U);
2395 PN->replaceAllUsesWith(V: I0);
2396 PN->eraseFromParent();
2397 }
2398
2399 // Finally nuke all instructions apart from the common instruction.
2400 for (auto *I : Insts) {
2401 if (I == I0)
2402 continue;
2403 // The remaining uses are debug users, replace those with the common inst.
2404 // In most (all?) cases this just introduces a use-before-def.
2405 assert(I->user_empty() && "Inst unexpectedly still has non-dbg users");
2406 I->replaceAllUsesWith(V: I0);
2407 I->eraseFromParent();
2408 }
2409}
2410
2411/// Check whether BB's predecessors end with unconditional branches. If it is
2412/// true, sink any common code from the predecessors to BB.
2413static bool sinkCommonCodeFromPredecessors(BasicBlock *BB,
2414 DomTreeUpdater *DTU) {
2415 // We support two situations:
2416 // (1) all incoming arcs are unconditional
2417 // (2) there are non-unconditional incoming arcs
2418 //
2419 // (2) is very common in switch defaults and
2420 // else-if patterns;
2421 //
2422 // if (a) f(1);
2423 // else if (b) f(2);
2424 //
2425 // produces:
2426 //
2427 // [if]
2428 // / \
2429 // [f(1)] [if]
2430 // | | \
2431 // | | |
2432 // | [f(2)]|
2433 // \ | /
2434 // [ end ]
2435 //
2436 // [end] has two unconditional predecessor arcs and one conditional. The
2437 // conditional refers to the implicit empty 'else' arc. This conditional
2438 // arc can also be caused by an empty default block in a switch.
2439 //
2440 // In this case, we attempt to sink code from all *unconditional* arcs.
2441 // If we can sink instructions from these arcs (determined during the scan
2442 // phase below) we insert a common successor for all unconditional arcs and
2443 // connect that to [end], to enable sinking:
2444 //
2445 // [if]
2446 // / \
2447 // [x(1)] [if]
2448 // | | \
2449 // | | \
2450 // | [x(2)] |
2451 // \ / |
2452 // [sink.split] |
2453 // \ /
2454 // [ end ]
2455 //
2456 SmallVector<BasicBlock*,4> UnconditionalPreds;
2457 bool HaveNonUnconditionalPredecessors = false;
2458 for (auto *PredBB : predecessors(BB)) {
2459 auto *PredBr = dyn_cast<UncondBrInst>(Val: PredBB->getTerminator());
2460 if (PredBr)
2461 UnconditionalPreds.push_back(Elt: PredBB);
2462 else
2463 HaveNonUnconditionalPredecessors = true;
2464 }
2465 if (UnconditionalPreds.size() < 2)
2466 return false;
2467
2468 // We take a two-step approach to tail sinking. First we scan from the end of
2469 // each block upwards in lockstep. If the n'th instruction from the end of each
2470 // block can be sunk, those instructions are added to ValuesToSink and we
2471 // carry on. If we can sink an instruction but need to PHI-merge some operands
2472 // (because they're not identical in each instruction) we add these to
2473 // PHIOperands.
2474 // We prepopulate PHIOperands with the phis that already exist in BB.
2475 DenseMap<const Use *, SmallVector<Value *, 4>> PHIOperands;
2476 for (PHINode &PN : BB->phis()) {
2477 SmallDenseMap<BasicBlock *, const Use *, 4> IncomingVals;
2478 for (const Use &U : PN.incoming_values())
2479 IncomingVals.insert(KV: {PN.getIncomingBlock(U), &U});
2480 auto &Ops = PHIOperands[IncomingVals[UnconditionalPreds[0]]];
2481 for (BasicBlock *Pred : UnconditionalPreds)
2482 Ops.push_back(Elt: *IncomingVals[Pred]);
2483 }
2484
2485 int ScanIdx = 0;
2486 SmallPtrSet<Value*,4> InstructionsToSink;
2487 LockstepReverseIterator<true> LRI(UnconditionalPreds);
2488 while (LRI.isValid() &&
2489 canSinkInstructions(Insts: *LRI, PHIOperands)) {
2490 LLVM_DEBUG(dbgs() << "SINK: instruction can be sunk: " << *(*LRI)[0]
2491 << "\n");
2492 InstructionsToSink.insert_range(R: *LRI);
2493 ++ScanIdx;
2494 --LRI;
2495 }
2496
2497 // If no instructions can be sunk, early-return.
2498 if (ScanIdx == 0)
2499 return false;
2500
2501 bool followedByDeoptOrUnreachable = IsBlockFollowedByDeoptOrUnreachable(BB);
2502
2503 if (!followedByDeoptOrUnreachable) {
2504 // Check whether this is the pointer operand of a load/store.
2505 auto IsMemOperand = [](Use &U) {
2506 auto *I = cast<Instruction>(Val: U.getUser());
2507 if (isa<LoadInst>(Val: I))
2508 return U.getOperandNo() == LoadInst::getPointerOperandIndex();
2509 if (isa<StoreInst>(Val: I))
2510 return U.getOperandNo() == StoreInst::getPointerOperandIndex();
2511 return false;
2512 };
2513
2514 // Okay, we *could* sink last ScanIdx instructions. But how many can we
2515 // actually sink before encountering instruction that is unprofitable to
2516 // sink?
2517 auto ProfitableToSinkInstruction = [&](LockstepReverseIterator<true> &LRI) {
2518 unsigned NumPHIInsts = 0;
2519 for (Use &U : (*LRI)[0]->operands()) {
2520 auto It = PHIOperands.find(Val: &U);
2521 if (It != PHIOperands.end() && !all_of(Range&: It->second, P: [&](Value *V) {
2522 return InstructionsToSink.contains(Ptr: V);
2523 })) {
2524 ++NumPHIInsts;
2525 // Do not separate a load/store from the gep producing the address.
2526 // The gep can likely be folded into the load/store as an addressing
2527 // mode. Additionally, a load of a gep is easier to analyze than a
2528 // load of a phi.
2529 if (IsMemOperand(U) &&
2530 any_of(Range&: It->second, P: [](Value *V) { return isa<GEPOperator>(Val: V); }))
2531 return false;
2532 // FIXME: this check is overly optimistic. We may end up not sinking
2533 // said instruction, due to the very same profitability check.
2534 // See @creating_too_many_phis in sink-common-code.ll.
2535 }
2536 }
2537 LLVM_DEBUG(dbgs() << "SINK: #phi insts: " << NumPHIInsts << "\n");
2538 return NumPHIInsts <= 1;
2539 };
2540
2541 // We've determined that we are going to sink last ScanIdx instructions,
2542 // and recorded them in InstructionsToSink. Now, some instructions may be
2543 // unprofitable to sink. But that determination depends on the instructions
2544 // that we are going to sink.
2545
2546 // First, forward scan: find the first instruction unprofitable to sink,
2547 // recording all the ones that are profitable to sink.
2548 // FIXME: would it be better, after we detect that not all are profitable.
2549 // to either record the profitable ones, or erase the unprofitable ones?
2550 // Maybe we need to choose (at runtime) the one that will touch least
2551 // instrs?
2552 LRI.reset();
2553 int Idx = 0;
2554 SmallPtrSet<Value *, 4> InstructionsProfitableToSink;
2555 while (Idx < ScanIdx) {
2556 if (!ProfitableToSinkInstruction(LRI)) {
2557 // Too many PHIs would be created.
2558 LLVM_DEBUG(
2559 dbgs() << "SINK: stopping here, too many PHIs would be created!\n");
2560 break;
2561 }
2562 InstructionsProfitableToSink.insert_range(R: *LRI);
2563 --LRI;
2564 ++Idx;
2565 }
2566
2567 // If no instructions can be sunk, early-return.
2568 if (Idx == 0)
2569 return false;
2570
2571 // Did we determine that (only) some instructions are unprofitable to sink?
2572 if (Idx < ScanIdx) {
2573 // Okay, some instructions are unprofitable.
2574 ScanIdx = Idx;
2575 InstructionsToSink = InstructionsProfitableToSink;
2576
2577 // But, that may make other instructions unprofitable, too.
2578 // So, do a backward scan, do any earlier instructions become
2579 // unprofitable?
2580 assert(
2581 !ProfitableToSinkInstruction(LRI) &&
2582 "We already know that the last instruction is unprofitable to sink");
2583 ++LRI;
2584 --Idx;
2585 while (Idx >= 0) {
2586 // If we detect that an instruction becomes unprofitable to sink,
2587 // all earlier instructions won't be sunk either,
2588 // so preemptively keep InstructionsProfitableToSink in sync.
2589 // FIXME: is this the most performant approach?
2590 for (auto *I : *LRI)
2591 InstructionsProfitableToSink.erase(Ptr: I);
2592 if (!ProfitableToSinkInstruction(LRI)) {
2593 // Everything starting with this instruction won't be sunk.
2594 ScanIdx = Idx;
2595 InstructionsToSink = InstructionsProfitableToSink;
2596 }
2597 ++LRI;
2598 --Idx;
2599 }
2600 }
2601
2602 // If no instructions can be sunk, early-return.
2603 if (ScanIdx == 0)
2604 return false;
2605 }
2606
2607 bool Changed = false;
2608
2609 if (HaveNonUnconditionalPredecessors) {
2610 if (!followedByDeoptOrUnreachable) {
2611 // It is always legal to sink common instructions from unconditional
2612 // predecessors. However, if not all predecessors are unconditional,
2613 // this transformation might be pessimizing. So as a rule of thumb,
2614 // don't do it unless we'd sink at least one non-speculatable instruction.
2615 // See https://bugs.llvm.org/show_bug.cgi?id=30244
2616 LRI.reset();
2617 int Idx = 0;
2618 bool Profitable = false;
2619 while (Idx < ScanIdx) {
2620 if (!isSafeToSpeculativelyExecute(I: (*LRI)[0])) {
2621 Profitable = true;
2622 break;
2623 }
2624 --LRI;
2625 ++Idx;
2626 }
2627 if (!Profitable)
2628 return false;
2629 }
2630
2631 LLVM_DEBUG(dbgs() << "SINK: Splitting edge\n");
2632 // We have a conditional edge and we're going to sink some instructions.
2633 // Insert a new block postdominating all blocks we're going to sink from.
2634 if (!SplitBlockPredecessors(BB, Preds: UnconditionalPreds, Suffix: ".sink.split", DTU))
2635 // Edges couldn't be split.
2636 return false;
2637 Changed = true;
2638 }
2639
2640 // Now that we've analyzed all potential sinking candidates, perform the
2641 // actual sink. We iteratively sink the last non-terminator of the source
2642 // blocks into their common successor unless doing so would require too
2643 // many PHI instructions to be generated (currently only one PHI is allowed
2644 // per sunk instruction).
2645 //
2646 // We can use InstructionsToSink to discount values needing PHI-merging that will
2647 // actually be sunk in a later iteration. This allows us to be more
2648 // aggressive in what we sink. This does allow a false positive where we
2649 // sink presuming a later value will also be sunk, but stop half way through
2650 // and never actually sink it which means we produce more PHIs than intended.
2651 // This is unlikely in practice though.
2652 int SinkIdx = 0;
2653 for (; SinkIdx != ScanIdx; ++SinkIdx) {
2654 LLVM_DEBUG(dbgs() << "SINK: Sink: "
2655 << *UnconditionalPreds[0]->getTerminator()->getPrevNode()
2656 << "\n");
2657
2658 // Because we've sunk every instruction in turn, the current instruction to
2659 // sink is always at index 0.
2660 LRI.reset();
2661
2662 sinkLastInstruction(Blocks: UnconditionalPreds);
2663 NumSinkCommonInstrs++;
2664 Changed = true;
2665 }
2666 if (SinkIdx != 0)
2667 ++NumSinkCommonCode;
2668 return Changed;
2669}
2670
2671namespace {
2672
2673struct CompatibleSets {
2674 using SetTy = SmallVector<InvokeInst *, 2>;
2675
2676 SmallVector<SetTy, 1> Sets;
2677
2678 static bool shouldBelongToSameSet(ArrayRef<InvokeInst *> Invokes);
2679
2680 SetTy &getCompatibleSet(InvokeInst *II);
2681
2682 void insert(InvokeInst *II);
2683};
2684
2685CompatibleSets::SetTy &CompatibleSets::getCompatibleSet(InvokeInst *II) {
2686 // Perform a linear scan over all the existing sets, see if the new `invoke`
2687 // is compatible with any particular set. Since we know that all the `invokes`
2688 // within a set are compatible, only check the first `invoke` in each set.
2689 // WARNING: at worst, this has quadratic complexity.
2690 for (CompatibleSets::SetTy &Set : Sets) {
2691 if (CompatibleSets::shouldBelongToSameSet(Invokes: {Set.front(), II}))
2692 return Set;
2693 }
2694
2695 // Otherwise, we either had no sets yet, or this invoke forms a new set.
2696 return Sets.emplace_back();
2697}
2698
2699void CompatibleSets::insert(InvokeInst *II) {
2700 getCompatibleSet(II).emplace_back(Args&: II);
2701}
2702
2703bool CompatibleSets::shouldBelongToSameSet(ArrayRef<InvokeInst *> Invokes) {
2704 assert(Invokes.size() == 2 && "Always called with exactly two candidates.");
2705
2706 // Can we theoretically merge these `invoke`s?
2707 auto IsIllegalToMerge = [](InvokeInst *II) {
2708 return II->cannotMerge() || II->isInlineAsm();
2709 };
2710 if (any_of(Range&: Invokes, P: IsIllegalToMerge))
2711 return false;
2712
2713 // Either both `invoke`s must be direct,
2714 // or both `invoke`s must be indirect.
2715 auto IsIndirectCall = [](InvokeInst *II) { return II->isIndirectCall(); };
2716 bool HaveIndirectCalls = any_of(Range&: Invokes, P: IsIndirectCall);
2717 bool AllCallsAreIndirect = all_of(Range&: Invokes, P: IsIndirectCall);
2718 if (HaveIndirectCalls) {
2719 if (!AllCallsAreIndirect)
2720 return false;
2721 } else {
2722 // All callees must be identical.
2723 Value *Callee = nullptr;
2724 for (InvokeInst *II : Invokes) {
2725 Value *CurrCallee = II->getCalledOperand();
2726 assert(CurrCallee && "There is always a called operand.");
2727 if (!Callee)
2728 Callee = CurrCallee;
2729 else if (Callee != CurrCallee)
2730 return false;
2731 }
2732 }
2733
2734 // Either both `invoke`s must not have a normal destination,
2735 // or both `invoke`s must have a normal destination,
2736 auto HasNormalDest = [](InvokeInst *II) {
2737 return !isa<UnreachableInst>(Val: II->getNormalDest()->getFirstNonPHIOrDbg());
2738 };
2739 if (any_of(Range&: Invokes, P: HasNormalDest)) {
2740 // Do not merge `invoke` that does not have a normal destination with one
2741 // that does have a normal destination, even though doing so would be legal.
2742 if (!all_of(Range&: Invokes, P: HasNormalDest))
2743 return false;
2744
2745 // All normal destinations must be identical.
2746 BasicBlock *NormalBB = nullptr;
2747 for (InvokeInst *II : Invokes) {
2748 BasicBlock *CurrNormalBB = II->getNormalDest();
2749 assert(CurrNormalBB && "There is always a 'continue to' basic block.");
2750 if (!NormalBB)
2751 NormalBB = CurrNormalBB;
2752 else if (NormalBB != CurrNormalBB)
2753 return false;
2754 }
2755
2756 // In the normal destination, the incoming values for these two `invoke`s
2757 // must be compatible.
2758 SmallPtrSet<Value *, 16> EquivalenceSet(llvm::from_range, Invokes);
2759 if (!incomingValuesAreCompatible(
2760 BB: NormalBB, IncomingBlocks: {Invokes[0]->getParent(), Invokes[1]->getParent()},
2761 EquivalenceSet: &EquivalenceSet))
2762 return false;
2763 }
2764
2765#ifndef NDEBUG
2766 // All unwind destinations must be identical.
2767 // We know that because we have started from said unwind destination.
2768 BasicBlock *UnwindBB = nullptr;
2769 for (InvokeInst *II : Invokes) {
2770 BasicBlock *CurrUnwindBB = II->getUnwindDest();
2771 assert(CurrUnwindBB && "There is always an 'unwind to' basic block.");
2772 if (!UnwindBB)
2773 UnwindBB = CurrUnwindBB;
2774 else
2775 assert(UnwindBB == CurrUnwindBB && "Unexpected unwind destination.");
2776 }
2777#endif
2778
2779 // In the unwind destination, the incoming values for these two `invoke`s
2780 // must be compatible.
2781 if (!incomingValuesAreCompatible(
2782 BB: Invokes.front()->getUnwindDest(),
2783 IncomingBlocks: {Invokes[0]->getParent(), Invokes[1]->getParent()}))
2784 return false;
2785
2786 // Ignoring arguments, these `invoke`s must be identical,
2787 // including operand bundles.
2788 const InvokeInst *II0 = Invokes.front();
2789 for (auto *II : Invokes.drop_front())
2790 if (!II->isSameOperationAs(I: II0, flags: Instruction::CompareUsingIntersectedAttrs))
2791 return false;
2792
2793 // Can we theoretically form the data operands for the merged `invoke`?
2794 auto IsIllegalToMergeArguments = [](auto Ops) {
2795 Use &U0 = std::get<0>(Ops);
2796 Use &U1 = std::get<1>(Ops);
2797 if (U0 == U1)
2798 return false;
2799 return !canReplaceOperandWithVariable(I: cast<Instruction>(Val: U0.getUser()),
2800 OpIdx: U0.getOperandNo());
2801 };
2802 assert(Invokes.size() == 2 && "Always called with exactly two candidates.");
2803 if (any_of(Range: zip(t: Invokes[0]->data_ops(), u: Invokes[1]->data_ops()),
2804 P: IsIllegalToMergeArguments))
2805 return false;
2806
2807 return true;
2808}
2809
2810} // namespace
2811
2812// Merge all invokes in the provided set, all of which are compatible
2813// as per the `CompatibleSets::shouldBelongToSameSet()`.
2814static void mergeCompatibleInvokesImpl(ArrayRef<InvokeInst *> Invokes,
2815 DomTreeUpdater *DTU) {
2816 assert(Invokes.size() >= 2 && "Must have at least two invokes to merge.");
2817
2818 SmallVector<DominatorTree::UpdateType, 8> Updates;
2819 if (DTU)
2820 Updates.reserve(N: 2 + 3 * Invokes.size());
2821
2822 bool HasNormalDest =
2823 !isa<UnreachableInst>(Val: Invokes[0]->getNormalDest()->getFirstNonPHIOrDbg());
2824
2825 // Clone one of the invokes into a new basic block.
2826 // Since they are all compatible, it doesn't matter which invoke is cloned.
2827 InvokeInst *MergedInvoke = [&Invokes, HasNormalDest]() {
2828 InvokeInst *II0 = Invokes.front();
2829 BasicBlock *II0BB = II0->getParent();
2830 BasicBlock *InsertBeforeBlock =
2831 II0->getParent()->getIterator()->getNextNode();
2832 Function *Func = II0BB->getParent();
2833 LLVMContext &Ctx = II0->getContext();
2834
2835 BasicBlock *MergedInvokeBB = BasicBlock::Create(
2836 Context&: Ctx, Name: II0BB->getName() + ".invoke", Parent: Func, InsertBefore: InsertBeforeBlock);
2837
2838 auto *MergedInvoke = cast<InvokeInst>(Val: II0->clone());
2839 // NOTE: all invokes have the same attributes, so no handling needed.
2840 MergedInvoke->insertInto(ParentBB: MergedInvokeBB, It: MergedInvokeBB->end());
2841
2842 if (!HasNormalDest) {
2843 // This set does not have a normal destination,
2844 // so just form a new block with unreachable terminator.
2845 BasicBlock *MergedNormalDest = BasicBlock::Create(
2846 Context&: Ctx, Name: II0BB->getName() + ".cont", Parent: Func, InsertBefore: InsertBeforeBlock);
2847 auto *UI = new UnreachableInst(Ctx, MergedNormalDest);
2848 UI->setDebugLoc(DebugLoc::getTemporary());
2849 MergedInvoke->setNormalDest(MergedNormalDest);
2850 }
2851
2852 // The unwind destination, however, remainds identical for all invokes here.
2853
2854 return MergedInvoke;
2855 }();
2856
2857 if (DTU) {
2858 // Predecessor blocks that contained these invokes will now branch to
2859 // the new block that contains the merged invoke, ...
2860 for (InvokeInst *II : Invokes)
2861 Updates.push_back(
2862 Elt: {DominatorTree::Insert, II->getParent(), MergedInvoke->getParent()});
2863
2864 // ... which has the new `unreachable` block as normal destination,
2865 // or unwinds to the (same for all `invoke`s in this set) `landingpad`,
2866 for (BasicBlock *SuccBBOfMergedInvoke : successors(I: MergedInvoke))
2867 Updates.push_back(Elt: {DominatorTree::Insert, MergedInvoke->getParent(),
2868 SuccBBOfMergedInvoke});
2869
2870 // Since predecessor blocks now unconditionally branch to a new block,
2871 // they no longer branch to their original successors.
2872 for (InvokeInst *II : Invokes)
2873 for (BasicBlock *SuccOfPredBB : successors(BB: II->getParent()))
2874 Updates.push_back(
2875 Elt: {DominatorTree::Delete, II->getParent(), SuccOfPredBB});
2876 }
2877
2878 bool IsIndirectCall = Invokes[0]->isIndirectCall();
2879
2880 // Form the merged operands for the merged invoke.
2881 for (Use &U : MergedInvoke->operands()) {
2882 // Only PHI together the indirect callees and data operands.
2883 if (MergedInvoke->isCallee(U: &U)) {
2884 if (!IsIndirectCall)
2885 continue;
2886 } else if (!MergedInvoke->isDataOperand(U: &U))
2887 continue;
2888
2889 // Don't create trivial PHI's with all-identical incoming values.
2890 bool NeedPHI = any_of(Range&: Invokes, P: [&U](InvokeInst *II) {
2891 return II->getOperand(i_nocapture: U.getOperandNo()) != U.get();
2892 });
2893 if (!NeedPHI)
2894 continue;
2895
2896 // Form a PHI out of all the data ops under this index.
2897 PHINode *PN = PHINode::Create(
2898 Ty: U->getType(), /*NumReservedValues=*/Invokes.size(), NameStr: "", InsertBefore: MergedInvoke->getIterator());
2899 for (InvokeInst *II : Invokes)
2900 PN->addIncoming(V: II->getOperand(i_nocapture: U.getOperandNo()), BB: II->getParent());
2901
2902 U.set(PN);
2903 }
2904
2905 // We've ensured that each PHI node has compatible (identical) incoming values
2906 // when coming from each of the `invoke`s in the current merge set,
2907 // so update the PHI nodes accordingly.
2908 for (BasicBlock *Succ : successors(I: MergedInvoke))
2909 addPredecessorToBlock(Succ, /*NewPred=*/MergedInvoke->getParent(),
2910 /*ExistPred=*/Invokes.front()->getParent());
2911
2912 // And finally, replace the original `invoke`s with an unconditional branch
2913 // to the block with the merged `invoke`. Also, give that merged `invoke`
2914 // the merged debugloc of all the original `invoke`s.
2915 DILocation *MergedDebugLoc = nullptr;
2916 for (InvokeInst *II : Invokes) {
2917 // Compute the debug location common to all the original `invoke`s.
2918 if (!MergedDebugLoc)
2919 MergedDebugLoc = II->getDebugLoc();
2920 else
2921 MergedDebugLoc =
2922 DebugLoc::getMergedLocation(LocA: MergedDebugLoc, LocB: II->getDebugLoc());
2923
2924 // And replace the old `invoke` with an unconditionally branch
2925 // to the block with the merged `invoke`.
2926 for (BasicBlock *OrigSuccBB : successors(BB: II->getParent()))
2927 OrigSuccBB->removePredecessor(Pred: II->getParent());
2928 auto *BI = UncondBrInst::Create(Target: MergedInvoke->getParent(), InsertBefore: II->getParent());
2929 // The unconditional branch is part of the replacement for the original
2930 // invoke, so should use its DebugLoc.
2931 BI->setDebugLoc(II->getDebugLoc());
2932 bool Success = MergedInvoke->tryIntersectAttributes(Other: II);
2933 assert(Success && "Merged invokes with incompatible attributes");
2934 // For NDEBUG Compile
2935 (void)Success;
2936 II->replaceAllUsesWith(V: MergedInvoke);
2937 II->eraseFromParent();
2938 ++NumInvokesMerged;
2939 }
2940 MergedInvoke->setDebugLoc(MergedDebugLoc);
2941 ++NumInvokeSetsFormed;
2942
2943 if (DTU)
2944 DTU->applyUpdates(Updates);
2945}
2946
2947/// If this block is a `landingpad` exception handling block, categorize all
2948/// the predecessor `invoke`s into sets, with all `invoke`s in each set
2949/// being "mergeable" together, and then merge invokes in each set together.
2950///
2951/// This is a weird mix of hoisting and sinking. Visually, it goes from:
2952/// [...] [...]
2953/// | |
2954/// [invoke0] [invoke1]
2955/// / \ / \
2956/// [cont0] [landingpad] [cont1]
2957/// to:
2958/// [...] [...]
2959/// \ /
2960/// [invoke]
2961/// / \
2962/// [cont] [landingpad]
2963///
2964/// But of course we can only do that if the invokes share the `landingpad`,
2965/// edges invoke0->cont0 and invoke1->cont1 are "compatible",
2966/// and the invoked functions are "compatible".
2967static bool mergeCompatibleInvokes(BasicBlock *BB, DomTreeUpdater *DTU) {
2968 if (!EnableMergeCompatibleInvokes)
2969 return false;
2970
2971 bool Changed = false;
2972
2973 // FIXME: generalize to all exception handling blocks?
2974 if (!BB->isLandingPad())
2975 return Changed;
2976
2977 CompatibleSets Grouper;
2978
2979 // Record all the predecessors of this `landingpad`. As per verifier,
2980 // the only allowed predecessor is the unwind edge of an `invoke`.
2981 // We want to group "compatible" `invokes` into the same set to be merged.
2982 for (BasicBlock *PredBB : predecessors(BB))
2983 Grouper.insert(II: cast<InvokeInst>(Val: PredBB->getTerminator()));
2984
2985 // And now, merge `invoke`s that were grouped togeter.
2986 for (ArrayRef<InvokeInst *> Invokes : Grouper.Sets) {
2987 if (Invokes.size() < 2)
2988 continue;
2989 Changed = true;
2990 mergeCompatibleInvokesImpl(Invokes, DTU);
2991 }
2992
2993 return Changed;
2994}
2995
2996namespace {
2997/// Track ephemeral values, which should be ignored for cost-modelling
2998/// purposes. Requires walking instructions in reverse order.
2999class EphemeralValueTracker {
3000 SmallPtrSet<const Instruction *, 32> EphValues;
3001
3002 bool isEphemeral(const Instruction *I) {
3003 if (isa<AssumeInst>(Val: I))
3004 return true;
3005 return !I->mayHaveSideEffects() && !I->isTerminator() &&
3006 all_of(Range: I->users(), P: [&](const User *U) {
3007 return EphValues.count(Ptr: cast<Instruction>(Val: U));
3008 });
3009 }
3010
3011public:
3012 bool track(const Instruction *I) {
3013 if (isEphemeral(I)) {
3014 EphValues.insert(Ptr: I);
3015 return true;
3016 }
3017 return false;
3018 }
3019
3020 bool contains(const Instruction *I) const { return EphValues.contains(Ptr: I); }
3021};
3022} // namespace
3023
3024/// Determine if we can hoist sink a sole store instruction out of a
3025/// conditional block.
3026///
3027/// We are looking for code like the following:
3028/// BrBB:
3029/// store i32 %add, i32* %arrayidx2
3030/// ... // No other stores or function calls (we could be calling a memory
3031/// ... // function).
3032/// %cmp = icmp ult %x, %y
3033/// br i1 %cmp, label %EndBB, label %ThenBB
3034/// ThenBB:
3035/// store i32 %add5, i32* %arrayidx2
3036/// br label EndBB
3037/// EndBB:
3038/// ...
3039/// We are going to transform this into:
3040/// BrBB:
3041/// store i32 %add, i32* %arrayidx2
3042/// ... //
3043/// %cmp = icmp ult %x, %y
3044/// %add.add5 = select i1 %cmp, i32 %add, %add5
3045/// store i32 %add.add5, i32* %arrayidx2
3046/// ...
3047///
3048/// \return The pointer to the value of the previous store if the store can be
3049/// hoisted into the predecessor block. 0 otherwise.
3050static Value *isSafeToSpeculateStore(Instruction *I, BasicBlock *BrBB,
3051 BasicBlock *StoreBB, BasicBlock *EndBB) {
3052 StoreInst *StoreToHoist = dyn_cast<StoreInst>(Val: I);
3053 if (!StoreToHoist)
3054 return nullptr;
3055
3056 // Volatile or atomic.
3057 if (!StoreToHoist->isSimple())
3058 return nullptr;
3059
3060 Value *StorePtr = StoreToHoist->getPointerOperand();
3061 Type *StoreTy = StoreToHoist->getValueOperand()->getType();
3062
3063 // Look for a store to the same pointer in BrBB.
3064 unsigned MaxNumInstToLookAt = 9;
3065 // Skip pseudo probe intrinsic calls which are not really killing any memory
3066 // accesses.
3067 for (Instruction &CurI : reverse(C&: *BrBB)) {
3068 if (!MaxNumInstToLookAt)
3069 break;
3070 --MaxNumInstToLookAt;
3071
3072 if (isa<PseudoProbeInst>(Val: CurI))
3073 continue;
3074
3075 // Could be calling an instruction that affects memory like free().
3076 if (CurI.mayWriteToMemory() && !isa<StoreInst>(Val: CurI))
3077 return nullptr;
3078
3079 if (auto *SI = dyn_cast<StoreInst>(Val: &CurI)) {
3080 // Found the previous store to same location and type. Make sure it is
3081 // simple, to avoid introducing a spurious non-atomic write after an
3082 // atomic write.
3083 if (SI->getPointerOperand() == StorePtr &&
3084 SI->getValueOperand()->getType() == StoreTy && SI->isSimple() &&
3085 SI->getAlign() >= StoreToHoist->getAlign())
3086 // Found the previous store, return its value operand.
3087 return SI->getValueOperand();
3088 return nullptr; // Unknown store.
3089 }
3090
3091 if (auto *LI = dyn_cast<LoadInst>(Val: &CurI)) {
3092 if (LI->getPointerOperand() == StorePtr && LI->getType() == StoreTy &&
3093 LI->isSimple() && LI->getAlign() >= StoreToHoist->getAlign()) {
3094 Value *Obj = getUnderlyingObject(V: StorePtr);
3095 bool ExplicitlyDereferenceableOnly;
3096 // The dereferenceability query here is only required to satisfy the
3097 // writable contract, actual dereferenceability is proven by the
3098 // presence of an access. As such, we can ignore frees.
3099 if (isWritableObject(Object: Obj, ExplicitlyDereferenceableOnly) &&
3100 capturesNothing(
3101 CC: PointerMayBeCaptured(V: Obj, Mask: CaptureComponents::Provenance)
3102 .WithoutRet) &&
3103 (!ExplicitlyDereferenceableOnly ||
3104 isDereferenceablePointer(V: StorePtr, Ty: StoreTy, Q: LI->getDataLayout(),
3105 /*IgnoreFree=*/true))) {
3106 // Found a previous load, return it.
3107 return LI;
3108 }
3109 }
3110 // The load didn't work out, but we may still find a store.
3111 }
3112 }
3113
3114 return nullptr;
3115}
3116
3117/// Estimate the cost of the insertion(s) and check that the PHI nodes can be
3118/// converted to selects.
3119static bool validateAndCostRequiredSelects(BasicBlock *BB, BasicBlock *ThenBB,
3120 BasicBlock *EndBB,
3121 unsigned &SpeculatedInstructions,
3122 InstructionCost &Cost,
3123 const TargetTransformInfo &TTI) {
3124 TargetTransformInfo::TargetCostKind CostKind =
3125 BB->getParent()->hasMinSize()
3126 ? TargetTransformInfo::TCK_CodeSize
3127 : TargetTransformInfo::TCK_SizeAndLatency;
3128
3129 bool HaveRewritablePHIs = false;
3130 for (PHINode &PN : EndBB->phis()) {
3131 Value *OrigV = PN.getIncomingValueForBlock(BB);
3132 Value *ThenV = PN.getIncomingValueForBlock(BB: ThenBB);
3133
3134 // FIXME: Try to remove some of the duplication with
3135 // hoistCommonCodeFromSuccessors. Skip PHIs which are trivial.
3136 if (ThenV == OrigV)
3137 continue;
3138
3139 Cost += TTI.getCmpSelInstrCost(Opcode: Instruction::Select, ValTy: PN.getType(),
3140 CondTy: CmpInst::makeCmpResultType(opnd_type: PN.getType()),
3141 VecPred: CmpInst::BAD_ICMP_PREDICATE, CostKind);
3142
3143 // Don't convert to selects if we could remove undefined behavior instead.
3144 if (passingValueIsAlwaysUndefined(V: OrigV, I: &PN) ||
3145 passingValueIsAlwaysUndefined(V: ThenV, I: &PN))
3146 return false;
3147
3148 HaveRewritablePHIs = true;
3149 ConstantExpr *OrigCE = dyn_cast<ConstantExpr>(Val: OrigV);
3150 ConstantExpr *ThenCE = dyn_cast<ConstantExpr>(Val: ThenV);
3151 if (!OrigCE && !ThenCE)
3152 continue; // Known cheap (FIXME: Maybe not true for aggregates).
3153
3154 InstructionCost OrigCost = OrigCE ? computeSpeculationCost(I: OrigCE, TTI) : 0;
3155 InstructionCost ThenCost = ThenCE ? computeSpeculationCost(I: ThenCE, TTI) : 0;
3156 InstructionCost MaxCost =
3157 2 * PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic;
3158 if (OrigCost + ThenCost > MaxCost)
3159 return false;
3160
3161 // Account for the cost of an unfolded ConstantExpr which could end up
3162 // getting expanded into Instructions.
3163 // FIXME: This doesn't account for how many operations are combined in the
3164 // constant expression.
3165 ++SpeculatedInstructions;
3166 if (SpeculatedInstructions > 1)
3167 return false;
3168 }
3169
3170 return HaveRewritablePHIs;
3171}
3172
3173static bool isProfitableToSpeculate(const CondBrInst *BI,
3174 std::optional<bool> Invert,
3175 const TargetTransformInfo &TTI) {
3176 // If the branch is non-unpredictable, and is predicted to *not* branch to
3177 // the `then` block, then avoid speculating it.
3178 if (BI->getMetadata(KindID: LLVMContext::MD_unpredictable))
3179 return true;
3180
3181 uint64_t TWeight, FWeight;
3182 if (!extractBranchWeights(I: *BI, TrueVal&: TWeight, FalseVal&: FWeight) || (TWeight + FWeight) == 0)
3183 return true;
3184
3185 if (!Invert.has_value())
3186 return false;
3187
3188 uint64_t EndWeight = *Invert ? TWeight : FWeight;
3189 BranchProbability BIEndProb =
3190 BranchProbability::getBranchProbability(Numerator: EndWeight, Denominator: TWeight + FWeight);
3191 BranchProbability Likely = TTI.getPredictableBranchThreshold();
3192 return BIEndProb < Likely;
3193}
3194
3195/// Speculate a conditional basic block flattening the CFG.
3196///
3197/// Note that this is a very risky transform currently. Speculating
3198/// instructions like this is most often not desirable. Instead, there is an MI
3199/// pass which can do it with full awareness of the resource constraints.
3200/// However, some cases are "obvious" and we should do directly. An example of
3201/// this is speculating a single, reasonably cheap instruction.
3202///
3203/// There is only one distinct advantage to flattening the CFG at the IR level:
3204/// it makes very common but simplistic optimizations such as are common in
3205/// instcombine and the DAG combiner more powerful by removing CFG edges and
3206/// modeling their effects with easier to reason about SSA value graphs.
3207///
3208///
3209/// An illustration of this transform is turning this IR:
3210/// \code
3211/// BB:
3212/// %cmp = icmp ult %x, %y
3213/// br i1 %cmp, label %EndBB, label %ThenBB
3214/// ThenBB:
3215/// %sub = sub %x, %y
3216/// br label BB2
3217/// EndBB:
3218/// %phi = phi [ %sub, %ThenBB ], [ 0, %BB ]
3219/// ...
3220/// \endcode
3221///
3222/// Into this IR:
3223/// \code
3224/// BB:
3225/// %cmp = icmp ult %x, %y
3226/// %sub = sub %x, %y
3227/// %cond = select i1 %cmp, 0, %sub
3228/// ...
3229/// \endcode
3230///
3231/// \returns true if the conditional block is removed.
3232bool SimplifyCFGOpt::speculativelyExecuteBB(CondBrInst *BI,
3233 BasicBlock *ThenBB) {
3234 if (!Options.SpeculateBlocks)
3235 return false;
3236
3237 // Be conservative for now. FP select instruction can often be expensive.
3238 Value *BrCond = BI->getCondition();
3239 if (isa<FCmpInst>(Val: BrCond))
3240 return false;
3241
3242 BasicBlock *BB = BI->getParent();
3243 BasicBlock *EndBB = ThenBB->getTerminator()->getSuccessor(Idx: 0);
3244 InstructionCost Budget =
3245 PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic;
3246
3247 // If ThenBB is actually on the false edge of the conditional branch, remember
3248 // to swap the select operands later.
3249 bool Invert = false;
3250 if (ThenBB != BI->getSuccessor(i: 0)) {
3251 assert(ThenBB == BI->getSuccessor(1) && "No edge from 'if' block?");
3252 Invert = true;
3253 }
3254 assert(EndBB == BI->getSuccessor(!Invert) && "No edge from to end block");
3255
3256 if (!isProfitableToSpeculate(BI, Invert, TTI))
3257 return false;
3258
3259 // Keep a count of how many times instructions are used within ThenBB when
3260 // they are candidates for sinking into ThenBB. Specifically:
3261 // - They are defined in BB, and
3262 // - They have no side effects, and
3263 // - All of their uses are in ThenBB.
3264 SmallDenseMap<Instruction *, unsigned, 4> SinkCandidateUseCounts;
3265
3266 SmallVector<Instruction *, 4> SpeculatedPseudoProbes;
3267
3268 unsigned SpeculatedInstructions = 0;
3269 bool HoistLoadsStores = Options.HoistLoadsStoresWithCondFaulting;
3270 SmallVector<Instruction *, 2> SpeculatedConditionalLoadsStores;
3271 Value *SpeculatedStoreValue = nullptr;
3272 StoreInst *SpeculatedStore = nullptr;
3273 EphemeralValueTracker EphTracker;
3274 for (Instruction &I : reverse(C: drop_end(RangeOrContainer&: *ThenBB))) {
3275 // Skip pseudo probes. The consequence is we lose track of the branch
3276 // probability for ThenBB, which is fine since the optimization here takes
3277 // place regardless of the branch probability.
3278 if (isa<PseudoProbeInst>(Val: I)) {
3279 // The probe should be deleted so that it will not be over-counted when
3280 // the samples collected on the non-conditional path are counted towards
3281 // the conditional path. We leave it for the counts inference algorithm to
3282 // figure out a proper count for an unknown probe.
3283 SpeculatedPseudoProbes.push_back(Elt: &I);
3284 continue;
3285 }
3286
3287 // Ignore ephemeral values, they will be dropped by the transform.
3288 if (EphTracker.track(I: &I))
3289 continue;
3290
3291 // Only speculatively execute a single instruction (not counting the
3292 // terminator) for now.
3293 bool IsSafeCheapLoadStore = HoistLoadsStores &&
3294 isSafeCheapLoadStore(I: &I, TTI) &&
3295 SpeculatedConditionalLoadsStores.size() <
3296 HoistLoadsStoresWithCondFaultingThreshold;
3297 // Not count load/store into cost if target supports conditional faulting
3298 // b/c it's cheap to speculate it.
3299 if (IsSafeCheapLoadStore)
3300 SpeculatedConditionalLoadsStores.push_back(Elt: &I);
3301 else
3302 ++SpeculatedInstructions;
3303
3304 if (SpeculatedInstructions > 1)
3305 return false;
3306
3307 // Don't hoist the instruction if it's unsafe or expensive.
3308 if (!IsSafeCheapLoadStore &&
3309 !isSafeToSpeculativelyExecute(I: &I, CtxI: BI, AC: Options.AC) &&
3310 !(HoistCondStores && !SpeculatedStoreValue &&
3311 (SpeculatedStoreValue =
3312 isSafeToSpeculateStore(I: &I, BrBB: BB, StoreBB: ThenBB, EndBB))))
3313 return false;
3314 if (!IsSafeCheapLoadStore && !SpeculatedStoreValue &&
3315 computeSpeculationCost(I: &I, TTI) >
3316 PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic)
3317 return false;
3318
3319 // Store the store speculation candidate.
3320 if (!SpeculatedStore && SpeculatedStoreValue)
3321 SpeculatedStore = cast<StoreInst>(Val: &I);
3322
3323 // Do not hoist the instruction if any of its operands are defined but not
3324 // used in BB. The transformation will prevent the operand from
3325 // being sunk into the use block.
3326 for (Use &Op : I.operands()) {
3327 Instruction *OpI = dyn_cast<Instruction>(Val&: Op);
3328 if (!OpI || OpI->getParent() != BB || OpI->mayHaveSideEffects())
3329 continue; // Not a candidate for sinking.
3330
3331 ++SinkCandidateUseCounts[OpI];
3332 }
3333 }
3334
3335 // Consider any sink candidates which are only used in ThenBB as costs for
3336 // speculation. Note, while we iterate over a DenseMap here, we are summing
3337 // and so iteration order isn't significant.
3338 for (const auto &[Inst, Count] : SinkCandidateUseCounts)
3339 if (Inst->hasNUses(N: Count)) {
3340 ++SpeculatedInstructions;
3341 if (SpeculatedInstructions > 1)
3342 return false;
3343 }
3344
3345 // Check that we can insert the selects and that it's not too expensive to do
3346 // so.
3347 bool Convert =
3348 SpeculatedStore != nullptr || !SpeculatedConditionalLoadsStores.empty();
3349 InstructionCost Cost = 0;
3350 Convert |= validateAndCostRequiredSelects(BB, ThenBB, EndBB,
3351 SpeculatedInstructions, Cost, TTI);
3352 if (!Convert || Cost > Budget)
3353 return false;
3354
3355 // If we get here, we can hoist the instruction and if-convert.
3356 LLVM_DEBUG(dbgs() << "SPECULATIVELY EXECUTING BB" << *ThenBB << "\n";);
3357
3358 Instruction *Sel = nullptr;
3359 // Insert a select of the value of the speculated store.
3360 if (SpeculatedStoreValue) {
3361 IRBuilder<NoFolder> Builder(BI);
3362 Value *OrigV = SpeculatedStore->getValueOperand();
3363 Value *TrueV = SpeculatedStore->getValueOperand();
3364 Value *FalseV = SpeculatedStoreValue;
3365 if (Invert)
3366 std::swap(a&: TrueV, b&: FalseV);
3367 Value *S = Builder.CreateSelect(
3368 C: BrCond, True: TrueV, False: FalseV, Name: "spec.store.select", MDFrom: BI);
3369 Sel = cast<Instruction>(Val: S);
3370 SpeculatedStore->setOperand(i_nocapture: 0, Val_nocapture: S);
3371 SpeculatedStore->applyMergedLocation(LocA: BI->getDebugLoc(),
3372 LocB: SpeculatedStore->getDebugLoc());
3373 // The value stored is still conditional, but the store itself is now
3374 // unconditionally executed, so we must be sure that any linked dbg.assign
3375 // intrinsics are tracking the new stored value (the result of the
3376 // select). If we don't, and the store were to be removed by another pass
3377 // (e.g. DSE), then we'd eventually end up emitting a location describing
3378 // the conditional value, unconditionally.
3379 //
3380 // === Before this transformation ===
3381 // pred:
3382 // store %one, %x.dest, !DIAssignID !1
3383 // dbg.assign %one, "x", ..., !1, ...
3384 // br %cond if.then
3385 //
3386 // if.then:
3387 // store %two, %x.dest, !DIAssignID !2
3388 // dbg.assign %two, "x", ..., !2, ...
3389 //
3390 // === After this transformation ===
3391 // pred:
3392 // store %one, %x.dest, !DIAssignID !1
3393 // dbg.assign %one, "x", ..., !1
3394 /// ...
3395 // %merge = select %cond, %two, %one
3396 // store %merge, %x.dest, !DIAssignID !2
3397 // dbg.assign %merge, "x", ..., !2
3398 for (DbgVariableRecord *DbgAssign :
3399 at::getDVRAssignmentMarkers(Inst: SpeculatedStore))
3400 if (llvm::is_contained(Range: DbgAssign->location_ops(), Element: OrigV))
3401 DbgAssign->replaceVariableLocationOp(OldValue: OrigV, NewValue: S);
3402 }
3403
3404 // Metadata can be dependent on the condition we are hoisting above.
3405 // Strip all UB-implying metadata on the instruction. Drop the debug loc
3406 // to avoid making it appear as if the condition is a constant, which would
3407 // be misleading while debugging.
3408 // Similarly strip attributes that maybe dependent on condition we are
3409 // hoisting above.
3410 for (auto &I : make_early_inc_range(Range&: *ThenBB)) {
3411 if (!SpeculatedStoreValue || &I != SpeculatedStore) {
3412 I.dropLocation();
3413 }
3414 I.dropUBImplyingAttrsAndMetadata();
3415
3416 // Drop ephemeral values.
3417 if (EphTracker.contains(I: &I)) {
3418 I.replaceAllUsesWith(V: PoisonValue::get(T: I.getType()));
3419 I.eraseFromParent();
3420 }
3421 }
3422
3423 // Hoist the instructions.
3424 // Drop DbgVariableRecords attached to these instructions.
3425 for (auto &It : *ThenBB)
3426 for (DbgRecord &DR : make_early_inc_range(Range: It.getDbgRecordRange()))
3427 // Drop all records except assign-kind DbgVariableRecords (dbg.assign
3428 // equivalent).
3429 if (DbgVariableRecord *DVR = dyn_cast<DbgVariableRecord>(Val: &DR);
3430 !DVR || !DVR->isDbgAssign())
3431 It.dropOneDbgRecord(I: &DR);
3432 BB->splice(ToIt: BI->getIterator(), FromBB: ThenBB, FromBeginIt: ThenBB->begin(),
3433 FromEndIt: std::prev(x: ThenBB->end()));
3434
3435 if (!SpeculatedConditionalLoadsStores.empty())
3436 hoistConditionalLoadsStores(BI, SpeculatedConditionalLoadsStores, Invert,
3437 Sel);
3438
3439 // Insert selects and rewrite the PHI operands.
3440 IRBuilder<NoFolder> Builder(BI);
3441 for (PHINode &PN : EndBB->phis()) {
3442 unsigned OrigI = PN.getBasicBlockIndex(BB);
3443 unsigned ThenI = PN.getBasicBlockIndex(BB: ThenBB);
3444 Value *OrigV = PN.getIncomingValue(i: OrigI);
3445 Value *ThenV = PN.getIncomingValue(i: ThenI);
3446
3447 // Skip PHIs which are trivial.
3448 if (OrigV == ThenV)
3449 continue;
3450
3451 // Create a select whose true value is the speculatively executed value and
3452 // false value is the pre-existing value. Swap them if the branch
3453 // destinations were inverted.
3454 Value *TrueV = ThenV, *FalseV = OrigV;
3455 if (Invert)
3456 std::swap(a&: TrueV, b&: FalseV);
3457 Value *V = Builder.CreateSelect(C: BrCond, True: TrueV, False: FalseV, Name: "spec.select", MDFrom: BI);
3458 PN.setIncomingValue(i: OrigI, V);
3459 PN.setIncomingValue(i: ThenI, V);
3460 }
3461
3462 // Remove speculated pseudo probes.
3463 for (Instruction *I : SpeculatedPseudoProbes)
3464 I->eraseFromParent();
3465
3466 ++NumSpeculations;
3467 return true;
3468}
3469
3470using BlocksSet = SmallPtrSet<BasicBlock *, 8>;
3471
3472// Return false if number of blocks searched is too much.
3473static bool findReaching(BasicBlock *BB, BasicBlock *DefBB,
3474 BlocksSet &ReachesNonLocalUses) {
3475 if (BB == DefBB)
3476 return true;
3477 if (!ReachesNonLocalUses.insert(Ptr: BB).second)
3478 return true;
3479
3480 if (ReachesNonLocalUses.size() > MaxJumpThreadingLiveBlocks)
3481 return false;
3482 for (BasicBlock *Pred : predecessors(BB))
3483 if (!findReaching(BB: Pred, DefBB, ReachesNonLocalUses))
3484 return false;
3485 return true;
3486}
3487
3488/// Return true if we can thread a branch across this block.
3489static bool blockIsSimpleEnoughToThreadThrough(BasicBlock *BB,
3490 BlocksSet &NonLocalUseBlocks) {
3491 int Size = 0;
3492 EphemeralValueTracker EphTracker;
3493
3494 // Walk the loop in reverse so that we can identify ephemeral values properly
3495 // (values only feeding assumes).
3496 for (Instruction &I : reverse(C&: *BB)) {
3497 // Can't fold blocks that contain noduplicate or convergent calls.
3498 if (CallInst *CI = dyn_cast<CallInst>(Val: &I))
3499 if (CI->cannotDuplicate() || CI->isConvergent())
3500 return false;
3501
3502 // Ignore ephemeral values which are deleted during codegen.
3503 // We will delete Phis while threading, so Phis should not be accounted in
3504 // block's size.
3505 if (!EphTracker.track(I: &I) && !isa<PHINode>(Val: I)) {
3506 if (Size++ > MaxSmallBlockSize)
3507 return false; // Don't clone large BB's.
3508 }
3509
3510 // Record blocks with non-local uses of values defined in the current basic
3511 // block.
3512 for (User *U : I.users()) {
3513 Instruction *UI = cast<Instruction>(Val: U);
3514 BasicBlock *UsedInBB = UI->getParent();
3515 if (UsedInBB == BB) {
3516 if (isa<PHINode>(Val: UI))
3517 return false;
3518 } else
3519 NonLocalUseBlocks.insert(Ptr: UsedInBB);
3520 }
3521
3522 // Looks ok, continue checking.
3523 }
3524
3525 return true;
3526}
3527
3528static ConstantInt *getKnownValueOnEdge(Value *V, BasicBlock *From,
3529 BasicBlock *To) {
3530 // Don't look past the block defining the value, we might get the value from
3531 // a previous loop iteration.
3532 auto *I = dyn_cast<Instruction>(Val: V);
3533 if (I && I->getParent() == To)
3534 return nullptr;
3535
3536 // We know the value if the From block branches on it.
3537 auto *BI = dyn_cast<CondBrInst>(Val: From->getTerminator());
3538 if (BI && BI->getCondition() == V &&
3539 BI->getSuccessor(i: 0) != BI->getSuccessor(i: 1))
3540 return BI->getSuccessor(i: 0) == To ? ConstantInt::getTrue(Context&: BI->getContext())
3541 : ConstantInt::getFalse(Context&: BI->getContext());
3542
3543 return nullptr;
3544}
3545
3546/// If we have a conditional branch on something for which we know the constant
3547/// value in predecessors (e.g. a phi node in the current block), thread edges
3548/// from the predecessor to their ultimate destination.
3549static std::optional<bool>
3550foldCondBranchOnValueKnownInPredecessorImpl(CondBrInst *BI, DomTreeUpdater *DTU,
3551 const DataLayout &DL,
3552 AssumptionCache *AC) {
3553 SmallMapVector<ConstantInt *, SmallSetVector<BasicBlock *, 2>, 2> KnownValues;
3554 BasicBlock *BB = BI->getParent();
3555 Value *Cond = BI->getCondition();
3556 PHINode *PN = dyn_cast<PHINode>(Val: Cond);
3557 if (PN && PN->getParent() == BB) {
3558 // Degenerate case of a single entry PHI.
3559 if (PN->getNumIncomingValues() == 1) {
3560 FoldSingleEntryPHINodes(BB: PN->getParent());
3561 return true;
3562 }
3563
3564 for (Use &U : PN->incoming_values())
3565 if (auto *CB = dyn_cast<ConstantInt>(Val&: U))
3566 KnownValues[CB].insert(X: PN->getIncomingBlock(U));
3567 } else {
3568 for (BasicBlock *Pred : predecessors(BB)) {
3569 if (ConstantInt *CB = getKnownValueOnEdge(V: Cond, From: Pred, To: BB))
3570 KnownValues[CB].insert(X: Pred);
3571 }
3572 }
3573
3574 if (KnownValues.empty())
3575 return false;
3576
3577 // Now we know that this block has multiple preds and two succs.
3578 // Check that the block is small enough and record which non-local blocks use
3579 // values defined in the block.
3580
3581 BlocksSet NonLocalUseBlocks;
3582 BlocksSet ReachesNonLocalUseBlocks;
3583 if (!blockIsSimpleEnoughToThreadThrough(BB, NonLocalUseBlocks))
3584 return false;
3585
3586 // Jump-threading can only be done to destinations where no values defined
3587 // in BB are live.
3588
3589 // Quickly check if both destinations have uses. If so, jump-threading cannot
3590 // be done.
3591 if (NonLocalUseBlocks.contains(Ptr: BI->getSuccessor(i: 0)) &&
3592 NonLocalUseBlocks.contains(Ptr: BI->getSuccessor(i: 1)))
3593 return false;
3594
3595 // Search backward from NonLocalUseBlocks to find which blocks
3596 // reach non-local uses.
3597 for (BasicBlock *UseBB : NonLocalUseBlocks)
3598 // Give up if too many blocks are searched.
3599 if (!findReaching(BB: UseBB, DefBB: BB, ReachesNonLocalUses&: ReachesNonLocalUseBlocks))
3600 return false;
3601
3602 for (const auto &Pair : KnownValues) {
3603 ConstantInt *CB = Pair.first;
3604 ArrayRef<BasicBlock *> PredBBs = Pair.second.getArrayRef();
3605 BasicBlock *RealDest = BI->getSuccessor(i: !CB->getZExtValue());
3606
3607 // Okay, we now know that all edges from PredBB should be revectored to
3608 // branch to RealDest.
3609 if (RealDest == BB)
3610 continue; // Skip self loops.
3611
3612 // Skip if the predecessor's terminator is an indirect branch.
3613 if (any_of(Range&: PredBBs, P: [](BasicBlock *PredBB) {
3614 return isa<IndirectBrInst>(Val: PredBB->getTerminator());
3615 }))
3616 continue;
3617
3618 // Only revector to RealDest if no values defined in BB are live.
3619 if (ReachesNonLocalUseBlocks.contains(Ptr: RealDest))
3620 continue;
3621
3622 LLVM_DEBUG({
3623 dbgs() << "Condition " << *Cond << " in " << BB->getName()
3624 << " has value " << *Pair.first << " in predecessors:\n";
3625 for (const BasicBlock *PredBB : Pair.second)
3626 dbgs() << " " << PredBB->getName() << "\n";
3627 dbgs() << "Threading to destination " << RealDest->getName() << ".\n";
3628 });
3629
3630 // Split the predecessors we are threading into a new edge block. We'll
3631 // clone the instructions into this block, and then redirect it to RealDest.
3632 BasicBlock *EdgeBB = SplitBlockPredecessors(BB, Preds: PredBBs, Suffix: ".critedge", DTU);
3633 if (!EdgeBB)
3634 continue;
3635
3636 // TODO: These just exist to reduce test diff, we can drop them if we like.
3637 EdgeBB->setName(RealDest->getName() + ".critedge");
3638 EdgeBB->moveBefore(MovePos: RealDest);
3639
3640 // Update PHI nodes.
3641 addPredecessorToBlock(Succ: RealDest, NewPred: EdgeBB, ExistPred: BB);
3642
3643 // BB may have instructions that are being threaded over. Clone these
3644 // instructions into EdgeBB. We know that there will be no uses of the
3645 // cloned instructions outside of EdgeBB.
3646 BasicBlock::iterator InsertPt = EdgeBB->getFirstInsertionPt();
3647 ValueToValueMapTy TranslateMap; // Track translated values.
3648 TranslateMap[Cond] = CB;
3649
3650 // RemoveDIs: track instructions that we optimise away while folding, so
3651 // that we can copy DbgVariableRecords from them later.
3652 BasicBlock::iterator SrcDbgCursor = BB->begin();
3653 for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI) {
3654 if (PHINode *PN = dyn_cast<PHINode>(Val&: BBI)) {
3655 TranslateMap[PN] = PN->getIncomingValueForBlock(BB: EdgeBB);
3656 continue;
3657 }
3658 // Clone the instruction.
3659 Instruction *N = BBI->clone();
3660 // Insert the new instruction into its new home.
3661 N->insertInto(ParentBB: EdgeBB, It: InsertPt);
3662
3663 if (BBI->hasName())
3664 N->setName(BBI->getName() + ".c");
3665
3666 // Update operands due to translation.
3667 // Key Instructions: Remap all the atom groups.
3668 if (const DebugLoc &DL = BBI->getDebugLoc())
3669 mapAtomInstance(DL, VMap&: TranslateMap);
3670 RemapInstruction(I: N, VM&: TranslateMap,
3671 Flags: RF_IgnoreMissingLocals | RF_NoModuleLevelChanges);
3672
3673 // Check for trivial simplification.
3674 if (Value *V = simplifyInstruction(I: N, Q: {DL, nullptr, nullptr, AC})) {
3675 if (!BBI->use_empty())
3676 TranslateMap[&*BBI] = V;
3677 if (!N->mayHaveSideEffects()) {
3678 N->eraseFromParent(); // Instruction folded away, don't need actual
3679 // inst
3680 N = nullptr;
3681 }
3682 } else {
3683 if (!BBI->use_empty())
3684 TranslateMap[&*BBI] = N;
3685 }
3686 if (N) {
3687 // Copy all debug-info attached to instructions from the last we
3688 // successfully clone, up to this instruction (they might have been
3689 // folded away).
3690 for (; SrcDbgCursor != BBI; ++SrcDbgCursor)
3691 N->cloneDebugInfoFrom(From: &*SrcDbgCursor);
3692 SrcDbgCursor = std::next(x: BBI);
3693 // Clone debug-info on this instruction too.
3694 N->cloneDebugInfoFrom(From: &*BBI);
3695
3696 // Register the new instruction with the assumption cache if necessary.
3697 if (auto *Assume = dyn_cast<AssumeInst>(Val: N))
3698 if (AC)
3699 AC->registerAssumption(CI: Assume);
3700 }
3701 }
3702
3703 for (; &*SrcDbgCursor != BI; ++SrcDbgCursor)
3704 InsertPt->cloneDebugInfoFrom(From: &*SrcDbgCursor);
3705 InsertPt->cloneDebugInfoFrom(From: BI);
3706
3707 BB->removePredecessor(Pred: EdgeBB);
3708 UncondBrInst *EdgeBI = cast<UncondBrInst>(Val: EdgeBB->getTerminator());
3709 EdgeBI->setSuccessor(idx: 0, NewSucc: RealDest);
3710 EdgeBI->setDebugLoc(BI->getDebugLoc());
3711
3712 if (DTU) {
3713 SmallVector<DominatorTree::UpdateType, 2> Updates;
3714 Updates.push_back(Elt: {DominatorTree::Delete, EdgeBB, BB});
3715 Updates.push_back(Elt: {DominatorTree::Insert, EdgeBB, RealDest});
3716 DTU->applyUpdates(Updates);
3717 }
3718
3719 // For simplicity, we created a separate basic block for the edge. Merge
3720 // it back into the predecessor if possible. This not only avoids
3721 // unnecessary SimplifyCFG iterations, but also makes sure that we don't
3722 // bypass the check for trivial cycles above.
3723 MergeBlockIntoPredecessor(BB: EdgeBB, DTU);
3724
3725 // Signal repeat, simplifying any other constants.
3726 return std::nullopt;
3727 }
3728
3729 return false;
3730}
3731
3732bool SimplifyCFGOpt::foldCondBranchOnValueKnownInPredecessor(CondBrInst *BI) {
3733 // Note: If BB is a loop header then there is a risk that threading introduces
3734 // a non-canonical loop by moving a back edge. So we avoid this optimization
3735 // for loop headers if NeedCanonicalLoop is set.
3736 if (Options.NeedCanonicalLoop && is_contained(Range&: LoopHeaders, Element: BI->getParent()))
3737 return false;
3738
3739 std::optional<bool> Result;
3740 bool EverChanged = false;
3741 do {
3742 // Note that None means "we changed things, but recurse further."
3743 Result =
3744 foldCondBranchOnValueKnownInPredecessorImpl(BI, DTU, DL, AC: Options.AC);
3745 EverChanged |= Result == std::nullopt || *Result;
3746 } while (Result == std::nullopt);
3747 return EverChanged;
3748}
3749
3750/// Given a BB that starts with the specified two-entry PHI node,
3751/// see if we can eliminate it.
3752static bool foldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
3753 DomTreeUpdater *DTU, AssumptionCache *AC,
3754 const DataLayout &DL,
3755 bool SpeculateUnpredictables) {
3756 // Ok, this is a two entry PHI node. Check to see if this is a simple "if
3757 // statement", which has a very simple dominance structure. Basically, we
3758 // are trying to find the condition that is being branched on, which
3759 // subsequently causes this merge to happen. We really want control
3760 // dependence information for this check, but simplifycfg can't keep it up
3761 // to date, and this catches most of the cases we care about anyway.
3762 BasicBlock *BB = PN->getParent();
3763
3764 BasicBlock *IfTrue, *IfFalse;
3765 CondBrInst *DomBI = GetIfCondition(BB, IfTrue, IfFalse);
3766 if (!DomBI)
3767 return false;
3768 Value *IfCond = DomBI->getCondition();
3769 // Don't bother if the branch will be constant folded trivially.
3770 if (isa<ConstantInt>(Val: IfCond))
3771 return false;
3772
3773 BasicBlock *DomBlock = DomBI->getParent();
3774 SmallVector<BasicBlock *, 2> IfBlocks;
3775 llvm::copy_if(Range: PN->blocks(), Out: std::back_inserter(x&: IfBlocks),
3776 P: [](BasicBlock *IfBlock) {
3777 return isa<UncondBrInst>(Val: IfBlock->getTerminator());
3778 });
3779 assert((IfBlocks.size() == 1 || IfBlocks.size() == 2) &&
3780 "Will have either one or two blocks to speculate.");
3781
3782 // If the branch is non-unpredictable, see if we either predictably jump to
3783 // the merge bb (if we have only a single 'then' block), or if we predictably
3784 // jump to one specific 'then' block (if we have two of them).
3785 // It isn't beneficial to speculatively execute the code
3786 // from the block that we know is predictably not entered.
3787 bool IsUnpredictable = DomBI->getMetadata(KindID: LLVMContext::MD_unpredictable);
3788 if (!IsUnpredictable) {
3789 uint64_t TWeight, FWeight;
3790 if (extractBranchWeights(I: *DomBI, TrueVal&: TWeight, FalseVal&: FWeight) &&
3791 (TWeight + FWeight) != 0) {
3792 BranchProbability BITrueProb =
3793 BranchProbability::getBranchProbability(Numerator: TWeight, Denominator: TWeight + FWeight);
3794 BranchProbability Likely = TTI.getPredictableBranchThreshold();
3795 BranchProbability BIFalseProb = BITrueProb.getCompl();
3796 if (IfBlocks.size() == 1) {
3797 BranchProbability BIBBProb =
3798 DomBI->getSuccessor(i: 0) == BB ? BITrueProb : BIFalseProb;
3799 if (BIBBProb >= Likely)
3800 return false;
3801 } else {
3802 if (BITrueProb >= Likely || BIFalseProb >= Likely)
3803 return false;
3804 }
3805 }
3806 }
3807
3808 // Don't try to fold an unreachable block. For example, the phi node itself
3809 // can't be the candidate if-condition for a select that we want to form.
3810 if (auto *IfCondPhiInst = dyn_cast<PHINode>(Val: IfCond))
3811 if (IfCondPhiInst->getParent() == BB)
3812 return false;
3813
3814 // Okay, we found that we can merge this two-entry phi node into a select.
3815 // Doing so would require us to fold *all* two entry phi nodes in this block.
3816 // At some point this becomes non-profitable (particularly if the target
3817 // doesn't support cmov's). Only do this transformation if there are two or
3818 // fewer PHI nodes in this block.
3819 unsigned NumPhis = 0;
3820 for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(Val: I); ++NumPhis, ++I)
3821 if (NumPhis > 2)
3822 return false;
3823
3824 // Loop over the PHI's seeing if we can promote them all to select
3825 // instructions. While we are at it, keep track of the instructions
3826 // that need to be moved to the dominating block.
3827 SmallPtrSet<Instruction *, 4> AggressiveInsts;
3828 SmallPtrSet<Instruction *, 2> ZeroCostInstructions;
3829 InstructionCost Cost = 0;
3830 InstructionCost Budget =
3831 TwoEntryPHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic;
3832 if (SpeculateUnpredictables && IsUnpredictable)
3833 Budget += TTI.getBranchMispredictPenalty();
3834
3835 bool Changed = false;
3836 for (BasicBlock::iterator II = BB->begin(); isa<PHINode>(Val: II);) {
3837 PHINode *PN = cast<PHINode>(Val: II++);
3838 if (Value *V = simplifyInstruction(I: PN, Q: {DL, PN})) {
3839 PN->replaceAllUsesWith(V);
3840 PN->eraseFromParent();
3841 Changed = true;
3842 continue;
3843 }
3844
3845 if (!dominatesMergePoint(V: PN->getIncomingValue(i: 0), BB, InsertPt: DomBI,
3846 AggressiveInsts, Cost, Budget, TTI, AC,
3847 ZeroCostInstructions) ||
3848 !dominatesMergePoint(V: PN->getIncomingValue(i: 1), BB, InsertPt: DomBI,
3849 AggressiveInsts, Cost, Budget, TTI, AC,
3850 ZeroCostInstructions))
3851 return Changed;
3852 }
3853
3854 // If we folded the first phi, PN dangles at this point. Refresh it. If
3855 // we ran out of PHIs then we simplified them all.
3856 PN = dyn_cast<PHINode>(Val: BB->begin());
3857 if (!PN)
3858 return true;
3859
3860 // Don't fold i1 branches on PHIs which contain binary operators or
3861 // (possibly inverted) select form of or/ands if their parameters are
3862 // an equality test.
3863 auto IsBinOpOrAndEq = [](Value *V) {
3864 CmpPredicate Pred;
3865 if (match(V, P: m_CombineOr(
3866 Ps: m_CombineOr(
3867 Ps: m_BinOp(L: m_Cmp(Pred, L: m_Value(), R: m_Value()), R: m_Value()),
3868 Ps: m_BinOp(L: m_Value(), R: m_Cmp(Pred, L: m_Value(), R: m_Value()))),
3869 Ps: m_c_Select(L: m_ImmConstant(),
3870 R: m_Cmp(Pred, L: m_Value(), R: m_Value()))))) {
3871 return CmpInst::isEquality(pred: Pred);
3872 }
3873 return false;
3874 };
3875 if (PN->getType()->isIntegerTy(BitWidth: 1) &&
3876 (IsBinOpOrAndEq(PN->getIncomingValue(i: 0)) ||
3877 IsBinOpOrAndEq(PN->getIncomingValue(i: 1)) || IsBinOpOrAndEq(IfCond)))
3878 return Changed;
3879
3880 // If all PHI nodes are promotable, check to make sure that all instructions
3881 // in the predecessor blocks can be promoted as well. If not, we won't be able
3882 // to get rid of the control flow, so it's not worth promoting to select
3883 // instructions.
3884 for (BasicBlock *IfBlock : IfBlocks)
3885 for (BasicBlock::iterator I = IfBlock->begin(); !I->isTerminator(); ++I)
3886 if (!AggressiveInsts.count(Ptr: &*I) && !I->isDebugOrPseudoInst()) {
3887 // This is not an aggressive instruction that we can promote.
3888 // Because of this, we won't be able to get rid of the control flow, so
3889 // the xform is not worth it.
3890 return Changed;
3891 }
3892
3893 // If either of the blocks has it's address taken, we can't do this fold.
3894 if (any_of(Range&: IfBlocks,
3895 P: [](BasicBlock *IfBlock) { return IfBlock->hasAddressTaken(); }))
3896 return Changed;
3897
3898 LLVM_DEBUG(dbgs() << "FOUND IF CONDITION! " << *IfCond;
3899 if (IsUnpredictable) dbgs() << " (unpredictable)";
3900 dbgs() << " T: " << IfTrue->getName()
3901 << " F: " << IfFalse->getName() << "\n");
3902
3903 // If we can still promote the PHI nodes after this gauntlet of tests,
3904 // do all of the PHI's now.
3905
3906 // Move all 'aggressive' instructions, which are defined in the
3907 // conditional parts of the if's up to the dominating block.
3908 for (BasicBlock *IfBlock : IfBlocks)
3909 hoistAllInstructionsInto(DomBlock, InsertPt: DomBI, BB: IfBlock);
3910
3911 IRBuilder<NoFolder> Builder(DomBI);
3912 // Propagate fast-math-flags from phi nodes to replacement selects.
3913 while (PHINode *PN = dyn_cast<PHINode>(Val: BB->begin())) {
3914 // Change the PHI node into a select instruction.
3915 Value *TrueVal = PN->getIncomingValueForBlock(BB: IfTrue);
3916 Value *FalseVal = PN->getIncomingValueForBlock(BB: IfFalse);
3917
3918 Value *Sel = Builder.CreateSelectFMF(C: IfCond, True: TrueVal, False: FalseVal,
3919 FMFSource: isa<FPMathOperator>(Val: PN) ? PN : nullptr,
3920 Name: "", MDFrom: DomBI);
3921 PN->replaceAllUsesWith(V: Sel);
3922 Sel->takeName(V: PN);
3923 PN->eraseFromParent();
3924 }
3925
3926 // At this point, all IfBlocks are empty, so our if statement
3927 // has been flattened. Change DomBlock to jump directly to our new block to
3928 // avoid other simplifycfg's kicking in on the diamond.
3929 Builder.CreateBr(Dest: BB);
3930
3931 SmallVector<DominatorTree::UpdateType, 3> Updates;
3932 if (DTU) {
3933 Updates.push_back(Elt: {DominatorTree::Insert, DomBlock, BB});
3934 for (auto *Successor : successors(BB: DomBlock))
3935 Updates.push_back(Elt: {DominatorTree::Delete, DomBlock, Successor});
3936 }
3937
3938 DomBI->eraseFromParent();
3939 if (DTU)
3940 DTU->applyUpdates(Updates);
3941
3942 return true;
3943}
3944
3945static Value *createLogicalOp(IRBuilderBase &Builder,
3946 Instruction::BinaryOps Opc, Value *LHS,
3947 Value *RHS, const Twine &Name = "") {
3948 // Try to relax logical op to binary op.
3949 if (impliesPoison(ValAssumedPoison: RHS, V: LHS))
3950 return Builder.CreateBinOp(Opc, LHS, RHS, Name);
3951 if (Opc == Instruction::And)
3952 return Builder.CreateLogicalAnd(Cond1: LHS, Cond2: RHS, Name);
3953 if (Opc == Instruction::Or)
3954 return Builder.CreateLogicalOr(Cond1: LHS, Cond2: RHS, Name);
3955 llvm_unreachable("Invalid logical opcode");
3956}
3957
3958/// Return true if either PBI or BI has branch weight available, and store
3959/// the weights in {Pred|Succ}{True|False}Weight. If one of PBI and BI does
3960/// not have branch weight, use 1:1 as its weight.
3961static bool extractPredSuccWeights(CondBrInst *PBI, CondBrInst *BI,
3962 uint64_t &PredTrueWeight,
3963 uint64_t &PredFalseWeight,
3964 uint64_t &SuccTrueWeight,
3965 uint64_t &SuccFalseWeight) {
3966 bool PredHasWeights =
3967 extractBranchWeights(I: *PBI, TrueVal&: PredTrueWeight, FalseVal&: PredFalseWeight);
3968 bool SuccHasWeights =
3969 extractBranchWeights(I: *BI, TrueVal&: SuccTrueWeight, FalseVal&: SuccFalseWeight);
3970 if (PredHasWeights || SuccHasWeights) {
3971 if (!PredHasWeights)
3972 PredTrueWeight = PredFalseWeight = 1;
3973 if (!SuccHasWeights)
3974 SuccTrueWeight = SuccFalseWeight = 1;
3975 return true;
3976 } else {
3977 return false;
3978 }
3979}
3980
3981/// Determine if the two branches share a common destination and deduce a glue
3982/// that joins the branches' conditions to arrive at the common destination if
3983/// that would be profitable.
3984static std::optional<std::tuple<BasicBlock *, Instruction::BinaryOps, bool>>
3985shouldFoldCondBranchesToCommonDestination(CondBrInst *BI, CondBrInst *PBI,
3986 const TargetTransformInfo *TTI) {
3987 assert(BI && PBI && "Both blocks must end with a conditional branches.");
3988 assert(is_contained(predecessors(BI->getParent()), PBI->getParent()) &&
3989 "PredBB must be a predecessor of BB.");
3990
3991 // We have the potential to fold the conditions together, but if the
3992 // predecessor branch is predictable, we may not want to merge them.
3993 uint64_t PTWeight, PFWeight;
3994 BranchProbability PBITrueProb, Likely;
3995 if (TTI && !PBI->getMetadata(KindID: LLVMContext::MD_unpredictable) &&
3996 extractBranchWeights(I: *PBI, TrueVal&: PTWeight, FalseVal&: PFWeight) &&
3997 (PTWeight + PFWeight) != 0) {
3998 PBITrueProb =
3999 BranchProbability::getBranchProbability(Numerator: PTWeight, Denominator: PTWeight + PFWeight);
4000 Likely = TTI->getPredictableBranchThreshold();
4001 }
4002
4003 if (PBI->getSuccessor(i: 0) == BI->getSuccessor(i: 0)) {
4004 // Speculate the 2nd condition unless the 1st is probably true.
4005 if (PBITrueProb.isUnknown() || PBITrueProb < Likely)
4006 return {{BI->getSuccessor(i: 0), Instruction::Or, false}};
4007 } else if (PBI->getSuccessor(i: 1) == BI->getSuccessor(i: 1)) {
4008 // Speculate the 2nd condition unless the 1st is probably false.
4009 if (PBITrueProb.isUnknown() || PBITrueProb.getCompl() < Likely)
4010 return {{BI->getSuccessor(i: 1), Instruction::And, false}};
4011 } else if (PBI->getSuccessor(i: 0) == BI->getSuccessor(i: 1)) {
4012 // Speculate the 2nd condition unless the 1st is probably true.
4013 if (PBITrueProb.isUnknown() || PBITrueProb < Likely)
4014 return {{BI->getSuccessor(i: 1), Instruction::And, true}};
4015 } else if (PBI->getSuccessor(i: 1) == BI->getSuccessor(i: 0)) {
4016 // Speculate the 2nd condition unless the 1st is probably false.
4017 if (PBITrueProb.isUnknown() || PBITrueProb.getCompl() < Likely)
4018 return {{BI->getSuccessor(i: 0), Instruction::Or, true}};
4019 }
4020 return std::nullopt;
4021}
4022
4023static bool performBranchToCommonDestFolding(CondBrInst *BI, CondBrInst *PBI,
4024 DomTreeUpdater *DTU,
4025 MemorySSAUpdater *MSSAU,
4026 const TargetTransformInfo *TTI) {
4027 BasicBlock *BB = BI->getParent();
4028 BasicBlock *PredBlock = PBI->getParent();
4029
4030 // Determine if the two branches share a common destination.
4031 BasicBlock *CommonSucc;
4032 Instruction::BinaryOps Opc;
4033 bool InvertPredCond;
4034 std::tie(args&: CommonSucc, args&: Opc, args&: InvertPredCond) =
4035 *shouldFoldCondBranchesToCommonDestination(BI, PBI, TTI);
4036
4037 LLVM_DEBUG(dbgs() << "FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB);
4038
4039 IRBuilder<ConstantFolder, IRBuilderCallbackInserter> Builder(
4040 BB->getContext(), ConstantFolder{},
4041 IRBuilderCallbackInserter([&BB](Instruction *I) {
4042 // The builder is used to create instructions to eliminate the branch in
4043 // BB. If BB's terminator has !annotation metadata, add it to the new
4044 // instructions.
4045 I->copyMetadata(SrcInst: *BB->getTerminator(), WL: LLVMContext::MD_annotation);
4046 }));
4047 Builder.SetInsertPoint(PBI);
4048
4049 // If we need to invert the condition in the pred block to match, do so now.
4050 if (InvertPredCond) {
4051 InvertBranch(PBI, Builder);
4052 }
4053
4054 BasicBlock *UniqueSucc =
4055 PBI->getSuccessor(i: 0) == BB ? BI->getSuccessor(i: 0) : BI->getSuccessor(i: 1);
4056
4057 // Before cloning instructions, notify the successor basic block that it
4058 // is about to have a new predecessor. This will update PHI nodes,
4059 // which will allow us to update live-out uses of bonus instructions.
4060 addPredecessorToBlock(Succ: UniqueSucc, NewPred: PredBlock, ExistPred: BB, MSSAU);
4061
4062 // Try to update branch weights.
4063 uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
4064 SmallVector<uint64_t, 2> MDWeights;
4065 if (extractPredSuccWeights(PBI, BI, PredTrueWeight, PredFalseWeight,
4066 SuccTrueWeight, SuccFalseWeight)) {
4067
4068 if (PBI->getSuccessor(i: 0) == BB) {
4069 // PBI: br i1 %x, BB, FalseDest
4070 // BI: br i1 %y, UniqueSucc, FalseDest
4071 // TrueWeight is TrueWeight for PBI * TrueWeight for BI.
4072 MDWeights.push_back(Elt: PredTrueWeight * SuccTrueWeight);
4073 // FalseWeight is FalseWeight for PBI * TotalWeight for BI +
4074 // TrueWeight for PBI * FalseWeight for BI.
4075 // We assume that total weights of a CondBrInst can fit into 32 bits.
4076 // Therefore, we will not have overflow using 64-bit arithmetic.
4077 MDWeights.push_back(Elt: PredFalseWeight * (SuccFalseWeight + SuccTrueWeight) +
4078 PredTrueWeight * SuccFalseWeight);
4079 } else {
4080 // PBI: br i1 %x, TrueDest, BB
4081 // BI: br i1 %y, TrueDest, UniqueSucc
4082 // TrueWeight is TrueWeight for PBI * TotalWeight for BI +
4083 // FalseWeight for PBI * TrueWeight for BI.
4084 MDWeights.push_back(Elt: PredTrueWeight * (SuccFalseWeight + SuccTrueWeight) +
4085 PredFalseWeight * SuccTrueWeight);
4086 // FalseWeight is FalseWeight for PBI * FalseWeight for BI.
4087 MDWeights.push_back(Elt: PredFalseWeight * SuccFalseWeight);
4088 }
4089
4090 setFittedBranchWeights(I&: *PBI, Weights: MDWeights, /*IsExpected=*/false,
4091 /*ElideAllZero=*/true);
4092
4093 // TODO: If BB is reachable from all paths through PredBlock, then we
4094 // could replace PBI's branch probabilities with BI's.
4095 } else
4096 PBI->setMetadata(KindID: LLVMContext::MD_prof, Node: nullptr);
4097
4098 // Now, update the CFG.
4099 PBI->setSuccessor(idx: PBI->getSuccessor(i: 0) != BB, NewSucc: UniqueSucc);
4100
4101 if (DTU)
4102 DTU->applyUpdates(Updates: {{DominatorTree::Insert, PredBlock, UniqueSucc},
4103 {DominatorTree::Delete, PredBlock, BB}});
4104
4105 // If BI was a loop latch, it may have had associated loop metadata.
4106 // We need to copy it to the new latch, that is, PBI.
4107 if (MDNode *LoopMD = BI->getMetadata(KindID: LLVMContext::MD_loop))
4108 PBI->setMetadata(KindID: LLVMContext::MD_loop, Node: LoopMD);
4109
4110 ValueToValueMapTy VMap; // maps original values to cloned values
4111 cloneInstructionsIntoPredecessorBlockAndUpdateSSAUses(BB, PredBlock, VMap);
4112
4113 Module *M = BB->getModule();
4114
4115 PredBlock->getTerminator()->cloneDebugInfoFrom(From: BB->getTerminator());
4116 for (DbgVariableRecord &DVR :
4117 filterDbgVars(R: PredBlock->getTerminator()->getDbgRecordRange())) {
4118 RemapDbgRecord(M, DR: &DVR, VM&: VMap,
4119 Flags: RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
4120 }
4121
4122 // Now that the Cond was cloned into the predecessor basic block,
4123 // or/and the two conditions together.
4124 Value *BICond = VMap[BI->getCondition()];
4125 PBI->setCondition(
4126 createLogicalOp(Builder, Opc, LHS: PBI->getCondition(), RHS: BICond, Name: "or.cond"));
4127 if (!ProfcheckDisableMetadataFixes)
4128 if (auto *SI = dyn_cast<SelectInst>(Val: PBI->getCondition()))
4129 if (!MDWeights.empty()) {
4130 assert(isSelectInRoleOfConjunctionOrDisjunction(SI));
4131 setFittedBranchWeights(I&: *SI, Weights: {MDWeights[0], MDWeights[1]},
4132 /*IsExpected=*/false, /*ElideAllZero=*/true);
4133 }
4134
4135 ++NumFoldBranchToCommonDest;
4136 return true;
4137}
4138
4139/// Return if an instruction's type or any of its operands' types are a vector
4140/// type.
4141static bool isVectorOp(Instruction &I) {
4142 return I.getType()->isVectorTy() || any_of(Range: I.operands(), P: [](Use &U) {
4143 return U->getType()->isVectorTy();
4144 });
4145}
4146
4147/// If this basic block is simple enough, and if a predecessor branches to us
4148/// and one of our successors, fold the block into the predecessor and use
4149/// logical operations to pick the right destination.
4150bool llvm::foldBranchToCommonDest(CondBrInst *BI, DomTreeUpdater *DTU,
4151 MemorySSAUpdater *MSSAU,
4152 const TargetTransformInfo *TTI,
4153 AssumptionCache *AC,
4154 unsigned BonusInstThreshold) {
4155 BasicBlock *BB = BI->getParent();
4156 TargetTransformInfo::TargetCostKind CostKind =
4157 BB->getParent()->hasMinSize() ? TargetTransformInfo::TCK_CodeSize
4158 : TargetTransformInfo::TCK_SizeAndLatency;
4159
4160 Instruction *Cond = dyn_cast<Instruction>(Val: BI->getCondition());
4161
4162 if (!Cond || !isa<CmpInst, BinaryOperator, SelectInst, TruncInst>(Val: Cond) ||
4163 Cond->getParent() != BB || !Cond->hasOneUse())
4164 return false;
4165
4166 // Finally, don't infinitely unroll conditional loops.
4167 if (is_contained(Range: successors(BB), Element: BB))
4168 return false;
4169
4170 // With which predecessors will we want to deal with?
4171 SmallVector<BasicBlock *, 8> Preds;
4172 for (BasicBlock *PredBlock : predecessors(BB)) {
4173 CondBrInst *PBI = dyn_cast<CondBrInst>(Val: PredBlock->getTerminator());
4174
4175 // Check that we have two conditional branches. If there is a PHI node in
4176 // the common successor, verify that the same value flows in from both
4177 // blocks.
4178 if (!PBI || !safeToMergeTerminators(SI1: BI, SI2: PBI))
4179 continue;
4180
4181 // Determine if the two branches share a common destination.
4182 BasicBlock *CommonSucc;
4183 Instruction::BinaryOps Opc;
4184 bool InvertPredCond;
4185 if (auto Recipe = shouldFoldCondBranchesToCommonDestination(BI, PBI, TTI))
4186 std::tie(args&: CommonSucc, args&: Opc, args&: InvertPredCond) = *Recipe;
4187 else
4188 continue;
4189
4190 // Check the cost of inserting the necessary logic before performing the
4191 // transformation.
4192 if (TTI) {
4193 Type *Ty = BI->getCondition()->getType();
4194 InstructionCost Cost = TTI->getArithmeticInstrCost(Opcode: Opc, Ty, CostKind);
4195 if (InvertPredCond && (!PBI->getCondition()->hasOneUse() ||
4196 !isa<CmpInst>(Val: PBI->getCondition())))
4197 Cost += TTI->getArithmeticInstrCost(Opcode: Instruction::Xor, Ty, CostKind);
4198
4199 if (Cost > BranchFoldThreshold)
4200 continue;
4201 }
4202
4203 // Ok, we do want to deal with this predecessor. Record it.
4204 Preds.emplace_back(Args&: PredBlock);
4205 }
4206
4207 // If there aren't any predecessors into which we can fold,
4208 // don't bother checking the cost.
4209 if (Preds.empty())
4210 return false;
4211
4212 // Only allow this transformation if computing the condition doesn't involve
4213 // too many instructions and these involved instructions can be executed
4214 // unconditionally. We denote all involved instructions except the condition
4215 // as "bonus instructions", and only allow this transformation when the
4216 // number of the bonus instructions we'll need to create when cloning into
4217 // each predecessor does not exceed a certain threshold.
4218 unsigned NumBonusInsts = 0;
4219 bool SawVectorOp = false;
4220 const unsigned PredCount = Preds.size();
4221 // Speculated instructions will be inserted before the terminator of the
4222 // predecessor. Only handle the simple case of one predecessor.
4223 const Instruction *CxtI =
4224 PredCount == 1 ? Preds[0]->getTerminator() : nullptr;
4225 for (Instruction &I : *BB) {
4226 // Don't check the branch condition comparison itself.
4227 if (&I == Cond)
4228 continue;
4229 // Ignore the terminator.
4230 if (isa<UncondBrInst, CondBrInst>(Val: I))
4231 continue;
4232 // Pseudo probes aren't speculatable but can be dropped on fold.
4233 if (isa<PseudoProbeInst>(Val: I))
4234 continue;
4235 // I must be safe to execute unconditionally.
4236 if (!isSafeToSpeculativelyExecute(I: &I, CtxI: CxtI, AC))
4237 return false;
4238 SawVectorOp |= isVectorOp(I);
4239
4240 // Account for the cost of duplicating this instruction into each
4241 // predecessor. Ignore free instructions.
4242 if (!TTI || TTI->getInstructionCost(U: &I, CostKind) !=
4243 TargetTransformInfo::TCC_Free) {
4244 NumBonusInsts += PredCount;
4245
4246 // Early exits once we reach the limit.
4247 if (NumBonusInsts >
4248 BonusInstThreshold * BranchFoldToCommonDestVectorMultiplier)
4249 return false;
4250 }
4251
4252 auto IsBCSSAUse = [BB, &I](Use &U) {
4253 auto *UI = cast<Instruction>(Val: U.getUser());
4254 if (auto *PN = dyn_cast<PHINode>(Val: UI))
4255 return PN->getIncomingBlock(U) == BB;
4256 return UI->getParent() == BB && I.comesBefore(Other: UI);
4257 };
4258
4259 // Does this instruction require rewriting of uses?
4260 if (!all_of(Range: I.uses(), P: IsBCSSAUse))
4261 return false;
4262 }
4263 if (NumBonusInsts >
4264 BonusInstThreshold *
4265 (SawVectorOp ? BranchFoldToCommonDestVectorMultiplier : 1))
4266 return false;
4267
4268 // Ok, we have the budget. Perform the transformation.
4269 for (BasicBlock *PredBlock : Preds) {
4270 auto *PBI = cast<CondBrInst>(Val: PredBlock->getTerminator());
4271 return performBranchToCommonDestFolding(BI, PBI, DTU, MSSAU, TTI);
4272 }
4273 return false;
4274}
4275
4276// If there is only one store in BB1 and BB2, return it, otherwise return
4277// nullptr.
4278static StoreInst *findUniqueStoreInBlocks(BasicBlock *BB1, BasicBlock *BB2) {
4279 StoreInst *S = nullptr;
4280 for (auto *BB : {BB1, BB2}) {
4281 if (!BB)
4282 continue;
4283 for (auto &I : *BB)
4284 if (auto *SI = dyn_cast<StoreInst>(Val: &I)) {
4285 if (S)
4286 // Multiple stores seen.
4287 return nullptr;
4288 else
4289 S = SI;
4290 }
4291 }
4292 return S;
4293}
4294
4295static Value *ensureValueAvailableInSuccessor(Value *V, BasicBlock *BB,
4296 Value *AlternativeV = nullptr) {
4297 // PHI is going to be a PHI node that allows the value V that is defined in
4298 // BB to be referenced in BB's only successor.
4299 //
4300 // If AlternativeV is nullptr, the only value we care about in PHI is V. It
4301 // doesn't matter to us what the other operand is (it'll never get used). We
4302 // could just create a new PHI with an undef incoming value, but that could
4303 // increase register pressure if EarlyCSE/InstCombine can't fold it with some
4304 // other PHI. So here we directly look for some PHI in BB's successor with V
4305 // as an incoming operand. If we find one, we use it, else we create a new
4306 // one.
4307 //
4308 // If AlternativeV is not nullptr, we care about both incoming values in PHI.
4309 // PHI must be exactly: phi <ty> [ %BB, %V ], [ %OtherBB, %AlternativeV]
4310 // where OtherBB is the single other predecessor of BB's only successor.
4311 PHINode *PHI = nullptr;
4312 BasicBlock *Succ = BB->getSingleSuccessor();
4313
4314 for (auto I = Succ->begin(); isa<PHINode>(Val: I); ++I)
4315 if (cast<PHINode>(Val&: I)->getIncomingValueForBlock(BB) == V) {
4316 PHI = cast<PHINode>(Val&: I);
4317 if (!AlternativeV)
4318 break;
4319
4320 assert(Succ->hasNPredecessors(2));
4321 auto PredI = pred_begin(BB: Succ);
4322 BasicBlock *OtherPredBB = *PredI == BB ? *++PredI : *PredI;
4323 if (PHI->getIncomingValueForBlock(BB: OtherPredBB) == AlternativeV)
4324 break;
4325 PHI = nullptr;
4326 }
4327 if (PHI)
4328 return PHI;
4329
4330 // If V is not an instruction defined in BB, just return it.
4331 if (!AlternativeV &&
4332 (!isa<Instruction>(Val: V) || cast<Instruction>(Val: V)->getParent() != BB))
4333 return V;
4334
4335 PHI = PHINode::Create(Ty: V->getType(), NumReservedValues: 2, NameStr: "simplifycfg.merge");
4336 PHI->insertBefore(InsertPos: Succ->begin());
4337 PHI->addIncoming(V, BB);
4338 for (BasicBlock *PredBB : predecessors(BB: Succ))
4339 if (PredBB != BB)
4340 PHI->addIncoming(
4341 V: AlternativeV ? AlternativeV : PoisonValue::get(T: V->getType()), BB: PredBB);
4342 return PHI;
4343}
4344
4345static bool mergeConditionalStoreToAddress(
4346 BasicBlock *PTB, BasicBlock *PFB, BasicBlock *QTB, BasicBlock *QFB,
4347 BasicBlock *PostBB, Value *Address, bool InvertPCond, bool InvertQCond,
4348 DomTreeUpdater *DTU, const DataLayout &DL, const TargetTransformInfo &TTI) {
4349 // For every pointer, there must be exactly two stores, one coming from
4350 // PTB or PFB, and the other from QTB or QFB. We don't support more than one
4351 // store (to any address) in PTB,PFB or QTB,QFB.
4352 // FIXME: We could relax this restriction with a bit more work and performance
4353 // testing.
4354 StoreInst *PStore = findUniqueStoreInBlocks(BB1: PTB, BB2: PFB);
4355 StoreInst *QStore = findUniqueStoreInBlocks(BB1: QTB, BB2: QFB);
4356 if (!PStore || !QStore)
4357 return false;
4358
4359 // Now check the stores are compatible.
4360 if (!QStore->isUnordered() || !PStore->isUnordered() ||
4361 PStore->getOrdering() != QStore->getOrdering() ||
4362 PStore->getSyncScopeID() != QStore->getSyncScopeID() ||
4363 PStore->getValueOperand()->getType() !=
4364 QStore->getValueOperand()->getType())
4365 return false;
4366
4367 // Check that sinking the store won't cause program behavior changes. Sinking
4368 // the store out of the Q blocks won't change any behavior as we're sinking
4369 // from a block to its unconditional successor. But we're moving a store from
4370 // the P blocks down through the middle block (QBI) and past both QFB and QTB.
4371 // So we need to check that there are no aliasing loads or stores in
4372 // QBI, QTB and QFB. We also need to check there are no conflicting memory
4373 // operations between PStore and the end of its parent block.
4374 //
4375 // The ideal way to do this is to query AliasAnalysis, but we don't
4376 // preserve AA currently so that is dangerous. Be super safe and just
4377 // check there are no other memory operations at all.
4378 for (auto &I : *QFB->getSinglePredecessor())
4379 if (I.mayReadOrWriteMemory())
4380 return false;
4381 for (auto &I : *QFB)
4382 if (&I != QStore && I.mayReadOrWriteMemory())
4383 return false;
4384 if (QTB)
4385 for (auto &I : *QTB)
4386 if (&I != QStore && I.mayReadOrWriteMemory())
4387 return false;
4388 for (auto I = BasicBlock::iterator(PStore), E = PStore->getParent()->end();
4389 I != E; ++I)
4390 if (&*I != PStore && I->mayReadOrWriteMemory())
4391 return false;
4392
4393 // If we're not in aggressive mode, we only optimize if we have some
4394 // confidence that by optimizing we'll allow P and/or Q to be if-converted.
4395 auto IsWorthwhile = [&](BasicBlock *BB, ArrayRef<StoreInst *> FreeStores) {
4396 if (!BB)
4397 return true;
4398 // Heuristic: if the block can be if-converted/phi-folded and the
4399 // instructions inside are all cheap (arithmetic/GEPs), it's worthwhile to
4400 // thread this store.
4401 InstructionCost Cost = 0;
4402 InstructionCost Budget =
4403 PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic;
4404 for (auto &I : *BB) {
4405 // Consider terminator instruction to be free.
4406 if (I.isTerminator())
4407 continue;
4408 // If this is one the stores that we want to speculate out of this BB,
4409 // then don't count it's cost, consider it to be free.
4410 if (auto *S = dyn_cast<StoreInst>(Val: &I))
4411 if (llvm::find(Range&: FreeStores, Val: S))
4412 continue;
4413 // Else, we have a white-list of instructions that we are ak speculating.
4414 if (!isa<BinaryOperator>(Val: I) && !isa<GetElementPtrInst>(Val: I))
4415 return false; // Not in white-list - not worthwhile folding.
4416 // And finally, if this is a non-free instruction that we are okay
4417 // speculating, ensure that we consider the speculation budget.
4418 Cost +=
4419 TTI.getInstructionCost(U: &I, CostKind: TargetTransformInfo::TCK_SizeAndLatency);
4420 if (Cost > Budget)
4421 return false; // Eagerly refuse to fold as soon as we're out of budget.
4422 }
4423 assert(Cost <= Budget &&
4424 "When we run out of budget we will eagerly return from within the "
4425 "per-instruction loop.");
4426 return true;
4427 };
4428
4429 const std::array<StoreInst *, 2> FreeStores = {PStore, QStore};
4430 if (!MergeCondStoresAggressively &&
4431 (!IsWorthwhile(PTB, FreeStores) || !IsWorthwhile(PFB, FreeStores) ||
4432 !IsWorthwhile(QTB, FreeStores) || !IsWorthwhile(QFB, FreeStores)))
4433 return false;
4434
4435 // If PostBB has more than two predecessors, we need to split it so we can
4436 // sink the store.
4437 if (std::next(x: pred_begin(BB: PostBB), n: 2) != pred_end(BB: PostBB)) {
4438 // We know that QFB's only successor is PostBB. And QFB has a single
4439 // predecessor. If QTB exists, then its only successor is also PostBB.
4440 // If QTB does not exist, then QFB's only predecessor has a conditional
4441 // branch to QFB and PostBB.
4442 BasicBlock *TruePred = QTB ? QTB : QFB->getSinglePredecessor();
4443 BasicBlock *NewBB =
4444 SplitBlockPredecessors(BB: PostBB, Preds: {QFB, TruePred}, Suffix: "condstore.split", DTU);
4445 if (!NewBB)
4446 return false;
4447 PostBB = NewBB;
4448 }
4449
4450 // OK, we're going to sink the stores to PostBB. The store has to be
4451 // conditional though, so first create the predicate.
4452 CondBrInst *PBranch =
4453 cast<CondBrInst>(Val: PFB->getSinglePredecessor()->getTerminator());
4454 CondBrInst *QBranch =
4455 cast<CondBrInst>(Val: QFB->getSinglePredecessor()->getTerminator());
4456 Value *PCond = PBranch->getCondition();
4457 Value *QCond = QBranch->getCondition();
4458
4459 Value *PPHI = ensureValueAvailableInSuccessor(V: PStore->getValueOperand(),
4460 BB: PStore->getParent());
4461 Value *QPHI = ensureValueAvailableInSuccessor(V: QStore->getValueOperand(),
4462 BB: QStore->getParent(), AlternativeV: PPHI);
4463
4464 BasicBlock::iterator PostBBFirst = PostBB->getFirstInsertionPt();
4465 IRBuilder<> QB(PostBB, PostBBFirst);
4466 QB.SetCurrentDebugLocation(PostBBFirst->getStableDebugLoc());
4467
4468 InvertPCond ^= (PStore->getParent() != PTB);
4469 InvertQCond ^= (QStore->getParent() != QTB);
4470 Value *PPred = InvertPCond ? QB.CreateNot(V: PCond) : PCond;
4471 Value *QPred = InvertQCond ? QB.CreateNot(V: QCond) : QCond;
4472
4473 Value *CombinedPred = QB.CreateOr(LHS: PPred, RHS: QPred);
4474
4475 BasicBlock::iterator InsertPt = QB.GetInsertPoint();
4476 auto *T = SplitBlockAndInsertIfThen(Cond: CombinedPred, SplitBefore: InsertPt,
4477 /*Unreachable=*/false,
4478 /*BranchWeights=*/nullptr, DTU);
4479 if (hasBranchWeightMD(I: *PBranch) && hasBranchWeightMD(I: *QBranch) &&
4480 !ProfcheckDisableMetadataFixes) {
4481 SmallVector<uint32_t, 2> PWeights, QWeights;
4482 extractBranchWeights(I: *PBranch, Weights&: PWeights);
4483 extractBranchWeights(I: *QBranch, Weights&: QWeights);
4484 if (InvertPCond)
4485 std::swap(a&: PWeights[0], b&: PWeights[1]);
4486 if (InvertQCond)
4487 std::swap(a&: QWeights[0], b&: QWeights[1]);
4488 auto CombinedWeights = getDisjunctionWeights(B1: PWeights, B2: QWeights);
4489 setFittedBranchWeights(I&: *PostBB->getTerminator(),
4490 Weights: {CombinedWeights[0], CombinedWeights[1]},
4491 /*IsExpected=*/false, /*ElideAllZero=*/true);
4492 }
4493
4494 QB.SetInsertPoint(T);
4495 StoreInst *SI = cast<StoreInst>(Val: QB.CreateStore(Val: QPHI, Ptr: Address));
4496 combineMetadataForCSE(K: QStore, J: PStore, DoesKMove: true);
4497 SI->copyMetadata(SrcInst: *QStore);
4498 // Update any dbg.assign intrinsics to track the merged value (QPHI) instead
4499 // of the original constant values, likely making these identical.
4500 for (auto *DbgAssign : at::getDVRAssignmentMarkers(Inst: SI)) {
4501 if (llvm::is_contained(Range: DbgAssign->location_ops(),
4502 Element: PStore->getValueOperand()))
4503 DbgAssign->replaceVariableLocationOp(OldValue: PStore->getValueOperand(), NewValue: QPHI);
4504 if (llvm::is_contained(Range: DbgAssign->location_ops(),
4505 Element: QStore->getValueOperand()))
4506 DbgAssign->replaceVariableLocationOp(OldValue: QStore->getValueOperand(), NewValue: QPHI);
4507 }
4508
4509 // Choose the minimum alignment. If we could prove both stores execute, we
4510 // could use biggest one. In this case, though, we only know that one of the
4511 // stores executes. And we don't know it's safe to take the alignment from a
4512 // store that doesn't execute.
4513 SI->setAlignment(std::min(a: PStore->getAlign(), b: QStore->getAlign()));
4514
4515 if (QStore->isAtomic())
4516 SI->setAtomic(Ordering: QStore->getOrdering(), SSID: QStore->getSyncScopeID());
4517
4518 QStore->eraseFromParent();
4519 PStore->eraseFromParent();
4520
4521 return true;
4522}
4523
4524static bool mergeConditionalStores(CondBrInst *PBI, CondBrInst *QBI,
4525 DomTreeUpdater *DTU, const DataLayout &DL,
4526 const TargetTransformInfo &TTI) {
4527 // The intention here is to find diamonds or triangles (see below) where each
4528 // conditional block contains a store to the same address. Both of these
4529 // stores are conditional, so they can't be unconditionally sunk. But it may
4530 // be profitable to speculatively sink the stores into one merged store at the
4531 // end, and predicate the merged store on the union of the two conditions of
4532 // PBI and QBI.
4533 //
4534 // This can reduce the number of stores executed if both of the conditions are
4535 // true, and can allow the blocks to become small enough to be if-converted.
4536 // This optimization will also chain, so that ladders of test-and-set
4537 // sequences can be if-converted away.
4538 //
4539 // We only deal with simple diamonds or triangles:
4540 //
4541 // PBI or PBI or a combination of the two
4542 // / \ | \
4543 // PTB PFB | PFB
4544 // \ / | /
4545 // QBI QBI
4546 // / \ | \
4547 // QTB QFB | QFB
4548 // \ / | /
4549 // PostBB PostBB
4550 //
4551 // We model triangles as a type of diamond with a nullptr "true" block.
4552 // Triangles are canonicalized so that the fallthrough edge is represented by
4553 // a true condition, as in the diagram above.
4554 BasicBlock *PTB = PBI->getSuccessor(i: 0);
4555 BasicBlock *PFB = PBI->getSuccessor(i: 1);
4556 BasicBlock *QTB = QBI->getSuccessor(i: 0);
4557 BasicBlock *QFB = QBI->getSuccessor(i: 1);
4558 BasicBlock *PostBB = QFB->getSingleSuccessor();
4559
4560 // Make sure we have a good guess for PostBB. If QTB's only successor is
4561 // QFB, then QFB is a better PostBB.
4562 if (QTB->getSingleSuccessor() == QFB)
4563 PostBB = QFB;
4564
4565 // If we couldn't find a good PostBB, stop.
4566 if (!PostBB)
4567 return false;
4568
4569 bool InvertPCond = false, InvertQCond = false;
4570 // Canonicalize fallthroughs to the true branches.
4571 if (PFB == QBI->getParent()) {
4572 std::swap(a&: PFB, b&: PTB);
4573 InvertPCond = true;
4574 }
4575 if (QFB == PostBB) {
4576 std::swap(a&: QFB, b&: QTB);
4577 InvertQCond = true;
4578 }
4579
4580 // From this point on we can assume PTB or QTB may be fallthroughs but PFB
4581 // and QFB may not. Model fallthroughs as a nullptr block.
4582 if (PTB == QBI->getParent())
4583 PTB = nullptr;
4584 if (QTB == PostBB)
4585 QTB = nullptr;
4586
4587 // Legality bailouts. We must have at least the non-fallthrough blocks and
4588 // the post-dominating block, and the non-fallthroughs must only have one
4589 // predecessor.
4590 auto HasOnePredAndOneSucc = [](BasicBlock *BB, BasicBlock *P, BasicBlock *S) {
4591 return BB->getSinglePredecessor() == P && BB->getSingleSuccessor() == S;
4592 };
4593 if (!HasOnePredAndOneSucc(PFB, PBI->getParent(), QBI->getParent()) ||
4594 !HasOnePredAndOneSucc(QFB, QBI->getParent(), PostBB))
4595 return false;
4596 if ((PTB && !HasOnePredAndOneSucc(PTB, PBI->getParent(), QBI->getParent())) ||
4597 (QTB && !HasOnePredAndOneSucc(QTB, QBI->getParent(), PostBB)))
4598 return false;
4599 if (!QBI->getParent()->hasNUses(N: 2))
4600 return false;
4601
4602 // OK, this is a sequence of two diamonds or triangles.
4603 // Check if there are stores in PTB or PFB that are repeated in QTB or QFB.
4604 SmallPtrSet<Value *, 4> PStoreAddresses, QStoreAddresses;
4605 for (auto *BB : {PTB, PFB}) {
4606 if (!BB)
4607 continue;
4608 for (auto &I : *BB)
4609 if (StoreInst *SI = dyn_cast<StoreInst>(Val: &I))
4610 PStoreAddresses.insert(Ptr: SI->getPointerOperand());
4611 }
4612 for (auto *BB : {QTB, QFB}) {
4613 if (!BB)
4614 continue;
4615 for (auto &I : *BB)
4616 if (StoreInst *SI = dyn_cast<StoreInst>(Val: &I))
4617 QStoreAddresses.insert(Ptr: SI->getPointerOperand());
4618 }
4619
4620 set_intersect(S1&: PStoreAddresses, S2: QStoreAddresses);
4621 // set_intersect mutates PStoreAddresses in place. Rename it here to make it
4622 // clear what it contains.
4623 auto &CommonAddresses = PStoreAddresses;
4624
4625 bool Changed = false;
4626 for (auto *Address : CommonAddresses)
4627 Changed |=
4628 mergeConditionalStoreToAddress(PTB, PFB, QTB, QFB, PostBB, Address,
4629 InvertPCond, InvertQCond, DTU, DL, TTI);
4630 return Changed;
4631}
4632
4633/// If the previous block ended with a widenable branch, determine if reusing
4634/// the target block is profitable and legal. This will have the effect of
4635/// "widening" PBI, but doesn't require us to reason about hosting safety.
4636static bool tryWidenCondBranchToCondBranch(CondBrInst *PBI, CondBrInst *BI,
4637 DomTreeUpdater *DTU) {
4638 // TODO: This can be generalized in two important ways:
4639 // 1) We can allow phi nodes in IfFalseBB and simply reuse all the input
4640 // values from the PBI edge.
4641 // 2) We can sink side effecting instructions into BI's fallthrough
4642 // successor provided they doesn't contribute to computation of
4643 // BI's condition.
4644 BasicBlock *IfTrueBB = PBI->getSuccessor(i: 0);
4645 BasicBlock *IfFalseBB = PBI->getSuccessor(i: 1);
4646 if (!isWidenableBranch(U: PBI) || IfTrueBB != BI->getParent() ||
4647 !BI->getParent()->getSinglePredecessor())
4648 return false;
4649 if (!IfFalseBB->phis().empty())
4650 return false; // TODO
4651 // This helps avoid infinite loop with SimplifyCondBranchToCondBranch which
4652 // may undo the transform done here.
4653 // TODO: There might be a more fine-grained solution to this.
4654 if (!llvm::succ_empty(BB: IfFalseBB))
4655 return false;
4656 // Use lambda to lazily compute expensive condition after cheap ones.
4657 auto NoSideEffects = [](BasicBlock &BB) {
4658 return llvm::none_of(Range&: BB, P: [](const Instruction &I) {
4659 return I.mayWriteToMemory() || I.mayHaveSideEffects();
4660 });
4661 };
4662 if (BI->getSuccessor(i: 1) != IfFalseBB && // no inf looping
4663 BI->getSuccessor(i: 1)->getTerminatingDeoptimizeCall() && // profitability
4664 NoSideEffects(*BI->getParent())) {
4665 auto *OldSuccessor = BI->getSuccessor(i: 1);
4666 OldSuccessor->removePredecessor(Pred: BI->getParent());
4667 BI->setSuccessor(idx: 1, NewSucc: IfFalseBB);
4668 if (DTU)
4669 DTU->applyUpdates(
4670 Updates: {{DominatorTree::Insert, BI->getParent(), IfFalseBB},
4671 {DominatorTree::Delete, BI->getParent(), OldSuccessor}});
4672 return true;
4673 }
4674 if (BI->getSuccessor(i: 0) != IfFalseBB && // no inf looping
4675 BI->getSuccessor(i: 0)->getTerminatingDeoptimizeCall() && // profitability
4676 NoSideEffects(*BI->getParent())) {
4677 auto *OldSuccessor = BI->getSuccessor(i: 0);
4678 OldSuccessor->removePredecessor(Pred: BI->getParent());
4679 BI->setSuccessor(idx: 0, NewSucc: IfFalseBB);
4680 if (DTU)
4681 DTU->applyUpdates(
4682 Updates: {{DominatorTree::Insert, BI->getParent(), IfFalseBB},
4683 {DominatorTree::Delete, BI->getParent(), OldSuccessor}});
4684 return true;
4685 }
4686 return false;
4687}
4688
4689/// If we have a conditional branch as a predecessor of another block,
4690/// this function tries to simplify it. We know
4691/// that PBI and BI are both conditional branches, and BI is in one of the
4692/// successor blocks of PBI - PBI branches to BI.
4693static bool SimplifyCondBranchToCondBranch(CondBrInst *PBI, CondBrInst *BI,
4694 DomTreeUpdater *DTU,
4695 const DataLayout &DL,
4696 const TargetTransformInfo &TTI) {
4697 BasicBlock *BB = BI->getParent();
4698
4699 // If this block ends with a branch instruction, and if there is a
4700 // predecessor that ends on a branch of the same condition, make
4701 // this conditional branch redundant.
4702 if (PBI->getCondition() == BI->getCondition() &&
4703 PBI->getSuccessor(i: 0) != PBI->getSuccessor(i: 1)) {
4704 // Okay, the outcome of this conditional branch is statically
4705 // knowable. If this block had a single pred, handle specially, otherwise
4706 // foldCondBranchOnValueKnownInPredecessor() will handle it.
4707 if (BB->getSinglePredecessor()) {
4708 // Turn this into a branch on constant.
4709 bool CondIsTrue = PBI->getSuccessor(i: 0) == BB;
4710 BI->setCondition(
4711 ConstantInt::get(Ty: Type::getInt1Ty(C&: BB->getContext()), V: CondIsTrue));
4712 return true; // Nuke the branch on constant.
4713 }
4714 }
4715
4716 // If the previous block ended with a widenable branch, determine if reusing
4717 // the target block is profitable and legal. This will have the effect of
4718 // "widening" PBI, but doesn't require us to reason about hosting safety.
4719 if (tryWidenCondBranchToCondBranch(PBI, BI, DTU))
4720 return true;
4721
4722 // If both branches are conditional and both contain stores to the same
4723 // address, remove the stores from the conditionals and create a conditional
4724 // merged store at the end.
4725 if (MergeCondStores && mergeConditionalStores(PBI, QBI: BI, DTU, DL, TTI))
4726 return true;
4727
4728 // If this is a conditional branch in an empty block, and if any
4729 // predecessors are a conditional branch to one of our destinations,
4730 // fold the conditions into logical ops and one cond br.
4731
4732 // Ignore dbg intrinsics.
4733 if (&*BB->begin() != BI)
4734 return false;
4735
4736 int PBIOp, BIOp;
4737 if (PBI->getSuccessor(i: 0) == BI->getSuccessor(i: 0)) {
4738 PBIOp = 0;
4739 BIOp = 0;
4740 } else if (PBI->getSuccessor(i: 0) == BI->getSuccessor(i: 1)) {
4741 PBIOp = 0;
4742 BIOp = 1;
4743 } else if (PBI->getSuccessor(i: 1) == BI->getSuccessor(i: 0)) {
4744 PBIOp = 1;
4745 BIOp = 0;
4746 } else if (PBI->getSuccessor(i: 1) == BI->getSuccessor(i: 1)) {
4747 PBIOp = 1;
4748 BIOp = 1;
4749 } else {
4750 return false;
4751 }
4752
4753 // Check to make sure that the other destination of this branch
4754 // isn't BB itself. If so, this is an infinite loop that will
4755 // keep getting unwound.
4756 if (PBI->getSuccessor(i: PBIOp) == BB)
4757 return false;
4758
4759 // If predecessor's branch probability to BB is too low don't merge branches.
4760 SmallVector<uint32_t, 2> PredWeights;
4761 if (!PBI->getMetadata(KindID: LLVMContext::MD_unpredictable) &&
4762 extractBranchWeights(I: *PBI, Weights&: PredWeights) &&
4763 (static_cast<uint64_t>(PredWeights[0]) + PredWeights[1]) != 0) {
4764
4765 BranchProbability CommonDestProb = BranchProbability::getBranchProbability(
4766 Numerator: PredWeights[PBIOp],
4767 Denominator: static_cast<uint64_t>(PredWeights[0]) + PredWeights[1]);
4768
4769 BranchProbability Likely = TTI.getPredictableBranchThreshold();
4770 if (CommonDestProb >= Likely)
4771 return false;
4772 }
4773
4774 // Do not perform this transformation if it would require
4775 // insertion of a large number of select instructions. For targets
4776 // without predication/cmovs, this is a big pessimization.
4777
4778 BasicBlock *CommonDest = PBI->getSuccessor(i: PBIOp);
4779 BasicBlock *RemovedDest = PBI->getSuccessor(i: PBIOp ^ 1);
4780 unsigned NumPhis = 0;
4781 for (BasicBlock::iterator II = CommonDest->begin(); isa<PHINode>(Val: II);
4782 ++II, ++NumPhis) {
4783 if (NumPhis > 2) // Disable this xform.
4784 return false;
4785 }
4786
4787 // Finally, if everything is ok, fold the branches to logical ops.
4788 BasicBlock *OtherDest = BI->getSuccessor(i: BIOp ^ 1);
4789
4790 LLVM_DEBUG(dbgs() << "FOLDING BRs:" << *PBI->getParent()
4791 << "AND: " << *BI->getParent());
4792
4793 SmallVector<DominatorTree::UpdateType, 5> Updates;
4794
4795 // If OtherDest *is* BB, then BB is a basic block with a single conditional
4796 // branch in it, where one edge (OtherDest) goes back to itself but the other
4797 // exits. We don't *know* that the program avoids the infinite loop
4798 // (even though that seems likely). If we do this xform naively, we'll end up
4799 // recursively unpeeling the loop. Since we know that (after the xform is
4800 // done) that the block *is* infinite if reached, we just make it an obviously
4801 // infinite loop with no cond branch.
4802 if (OtherDest == BB) {
4803 // Insert it at the end of the function, because it's either code,
4804 // or it won't matter if it's hot. :)
4805 BasicBlock *InfLoopBlock =
4806 BasicBlock::Create(Context&: BB->getContext(), Name: "infloop", Parent: BB->getParent());
4807 UncondBrInst::Create(Target: InfLoopBlock, InsertBefore: InfLoopBlock);
4808 if (DTU)
4809 Updates.push_back(Elt: {DominatorTree::Insert, InfLoopBlock, InfLoopBlock});
4810 OtherDest = InfLoopBlock;
4811 }
4812
4813 LLVM_DEBUG(dbgs() << *PBI->getParent()->getParent());
4814
4815 // BI may have other predecessors. Because of this, we leave
4816 // it alone, but modify PBI.
4817
4818 // Make sure we get to CommonDest on True&True directions.
4819 Value *PBICond = PBI->getCondition();
4820 IRBuilder<NoFolder> Builder(PBI);
4821 if (PBIOp)
4822 PBICond = Builder.CreateNot(V: PBICond, Name: PBICond->getName() + ".not");
4823
4824 Value *BICond = BI->getCondition();
4825 if (BIOp)
4826 BICond = Builder.CreateNot(V: BICond, Name: BICond->getName() + ".not");
4827
4828 // Merge the conditions.
4829 Value *Cond =
4830 createLogicalOp(Builder, Opc: Instruction::Or, LHS: PBICond, RHS: BICond, Name: "brmerge");
4831
4832 // Modify PBI to branch on the new condition to the new dests.
4833 PBI->setCondition(Cond);
4834 PBI->setSuccessor(idx: 0, NewSucc: CommonDest);
4835 PBI->setSuccessor(idx: 1, NewSucc: OtherDest);
4836
4837 if (DTU) {
4838 Updates.push_back(Elt: {DominatorTree::Insert, PBI->getParent(), OtherDest});
4839 Updates.push_back(Elt: {DominatorTree::Delete, PBI->getParent(), RemovedDest});
4840
4841 DTU->applyUpdates(Updates);
4842 }
4843
4844 // Update branch weight for PBI.
4845 uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
4846 uint64_t PredCommon, PredOther, SuccCommon, SuccOther;
4847 bool HasWeights =
4848 extractPredSuccWeights(PBI, BI, PredTrueWeight, PredFalseWeight,
4849 SuccTrueWeight, SuccFalseWeight);
4850 if (HasWeights) {
4851 PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
4852 PredOther = PBIOp ? PredTrueWeight : PredFalseWeight;
4853 SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
4854 SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
4855 // The weight to CommonDest should be PredCommon * SuccTotal +
4856 // PredOther * SuccCommon.
4857 // The weight to OtherDest should be PredOther * SuccOther.
4858 uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther) +
4859 PredOther * SuccCommon,
4860 PredOther * SuccOther};
4861
4862 setFittedBranchWeights(I&: *PBI, Weights: NewWeights, /*IsExpected=*/false,
4863 /*ElideAllZero=*/true);
4864 // Cond may be a select instruction with the first operand set to "true", or
4865 // the second to "false" (see how createLogicalOp works for `and` and `or`)
4866 if (!ProfcheckDisableMetadataFixes)
4867 if (auto *SI = dyn_cast<SelectInst>(Val: Cond)) {
4868 assert(isSelectInRoleOfConjunctionOrDisjunction(SI));
4869 // The select is predicated on PBICond
4870 assert(SI->getCondition() == PBICond);
4871 // The corresponding probabilities are what was referred to above as
4872 // PredCommon and PredOther.
4873 setFittedBranchWeights(I&: *SI, Weights: {PredCommon, PredOther},
4874 /*IsExpected=*/false, /*ElideAllZero=*/true);
4875 }
4876 }
4877
4878 // OtherDest may have phi nodes. If so, add an entry from PBI's
4879 // block that are identical to the entries for BI's block.
4880 addPredecessorToBlock(Succ: OtherDest, NewPred: PBI->getParent(), ExistPred: BB);
4881
4882 // We know that the CommonDest already had an edge from PBI to
4883 // it. If it has PHIs though, the PHIs may have different
4884 // entries for BB and PBI's BB. If so, insert a select to make
4885 // them agree.
4886 for (PHINode &PN : CommonDest->phis()) {
4887 Value *BIV = PN.getIncomingValueForBlock(BB);
4888 unsigned PBBIdx = PN.getBasicBlockIndex(BB: PBI->getParent());
4889 Value *PBIV = PN.getIncomingValue(i: PBBIdx);
4890 if (BIV != PBIV) {
4891 // Insert a select in PBI to pick the right value.
4892 SelectInst *NV = cast<SelectInst>(
4893 Val: Builder.CreateSelect(C: PBICond, True: PBIV, False: BIV, Name: PBIV->getName() + ".mux"));
4894 PN.setIncomingValue(i: PBBIdx, V: NV);
4895 // The select has the same condition as PBI, in the same BB. The
4896 // probabilities don't change.
4897 if (HasWeights) {
4898 uint64_t TrueWeight = PBIOp ? PredFalseWeight : PredTrueWeight;
4899 uint64_t FalseWeight = PBIOp ? PredTrueWeight : PredFalseWeight;
4900 setFittedBranchWeights(I&: *NV, Weights: {TrueWeight, FalseWeight},
4901 /*IsExpected=*/false, /*ElideAllZero=*/true);
4902 }
4903 }
4904 }
4905
4906 LLVM_DEBUG(dbgs() << "INTO: " << *PBI->getParent());
4907 LLVM_DEBUG(dbgs() << *PBI->getParent()->getParent());
4908
4909 // This basic block is probably dead. We know it has at least
4910 // one fewer predecessor.
4911 return true;
4912}
4913
4914// Simplifies a terminator by replacing it with a branch to TrueBB if Cond is
4915// true or to FalseBB if Cond is false.
4916// Takes care of updating the successors and removing the old terminator.
4917// Also makes sure not to introduce new successors by assuming that edges to
4918// non-successor TrueBBs and FalseBBs aren't reachable.
4919bool SimplifyCFGOpt::simplifyTerminatorOnSelect(Instruction *OldTerm,
4920 Value *Cond, BasicBlock *TrueBB,
4921 BasicBlock *FalseBB,
4922 uint32_t TrueWeight,
4923 uint32_t FalseWeight) {
4924 auto *BB = OldTerm->getParent();
4925 // Remove any superfluous successor edges from the CFG.
4926 // First, figure out which successors to preserve.
4927 // If TrueBB and FalseBB are equal, only try to preserve one copy of that
4928 // successor.
4929 BasicBlock *KeepEdge1 = TrueBB;
4930 BasicBlock *KeepEdge2 = TrueBB != FalseBB ? FalseBB : nullptr;
4931
4932 SmallSetVector<BasicBlock *, 2> RemovedSuccessors;
4933
4934 // Then remove the rest.
4935 for (BasicBlock *Succ : successors(I: OldTerm)) {
4936 // Make sure only to keep exactly one copy of each edge.
4937 if (Succ == KeepEdge1)
4938 KeepEdge1 = nullptr;
4939 else if (Succ == KeepEdge2)
4940 KeepEdge2 = nullptr;
4941 else {
4942 Succ->removePredecessor(Pred: BB,
4943 /*KeepOneInputPHIs=*/true);
4944
4945 if (Succ != TrueBB && Succ != FalseBB)
4946 RemovedSuccessors.insert(X: Succ);
4947 }
4948 }
4949
4950 IRBuilder<> Builder(OldTerm);
4951 Builder.SetCurrentDebugLocation(OldTerm->getDebugLoc());
4952
4953 // Insert an appropriate new terminator.
4954 if (!KeepEdge1 && !KeepEdge2) {
4955 if (TrueBB == FalseBB) {
4956 // We were only looking for one successor, and it was present.
4957 // Create an unconditional branch to it.
4958 Builder.CreateBr(Dest: TrueBB);
4959 } else {
4960 // We found both of the successors we were looking for.
4961 // Create a conditional branch sharing the condition of the select.
4962 CondBrInst *NewBI = Builder.CreateCondBr(Cond, True: TrueBB, False: FalseBB);
4963 setBranchWeights(I&: *NewBI, Weights: {TrueWeight, FalseWeight},
4964 /*IsExpected=*/false, /*ElideAllZero=*/true);
4965 }
4966 } else if (KeepEdge1 && (KeepEdge2 || TrueBB == FalseBB)) {
4967 // Neither of the selected blocks were successors, so this
4968 // terminator must be unreachable.
4969 new UnreachableInst(OldTerm->getContext(), OldTerm->getIterator());
4970 } else {
4971 // One of the selected values was a successor, but the other wasn't.
4972 // Insert an unconditional branch to the one that was found;
4973 // the edge to the one that wasn't must be unreachable.
4974 if (!KeepEdge1) {
4975 // Only TrueBB was found.
4976 Builder.CreateBr(Dest: TrueBB);
4977 } else {
4978 // Only FalseBB was found.
4979 Builder.CreateBr(Dest: FalseBB);
4980 }
4981 }
4982
4983 eraseTerminatorAndDCECond(TI: OldTerm);
4984
4985 if (DTU) {
4986 SmallVector<DominatorTree::UpdateType, 2> Updates;
4987 Updates.reserve(N: RemovedSuccessors.size());
4988 for (auto *RemovedSuccessor : RemovedSuccessors)
4989 Updates.push_back(Elt: {DominatorTree::Delete, BB, RemovedSuccessor});
4990 DTU->applyUpdates(Updates);
4991 }
4992
4993 return true;
4994}
4995
4996// Replaces
4997// (switch (select cond, X, Y)) on constant X, Y
4998// with a branch - conditional if X and Y lead to distinct BBs,
4999// unconditional otherwise.
5000bool SimplifyCFGOpt::simplifySwitchOnSelect(SwitchInst *SI,
5001 SelectInst *Select) {
5002 // Check for constant integer values in the select.
5003 ConstantInt *TrueVal = dyn_cast<ConstantInt>(Val: Select->getTrueValue());
5004 ConstantInt *FalseVal = dyn_cast<ConstantInt>(Val: Select->getFalseValue());
5005 if (!TrueVal || !FalseVal)
5006 return false;
5007
5008 // Find the relevant condition and destinations.
5009 Value *Condition = Select->getCondition();
5010 BasicBlock *TrueBB = SI->findCaseValue(C: TrueVal)->getCaseSuccessor();
5011 BasicBlock *FalseBB = SI->findCaseValue(C: FalseVal)->getCaseSuccessor();
5012
5013 // Get weight for TrueBB and FalseBB.
5014 uint32_t TrueWeight = 0, FalseWeight = 0;
5015 SmallVector<uint64_t, 8> Weights;
5016 bool HasWeights = hasBranchWeightMD(I: *SI);
5017 if (HasWeights) {
5018 getBranchWeights(TI: SI, Weights);
5019 if (Weights.size() == 1 + SI->getNumCases()) {
5020 TrueWeight =
5021 (uint32_t)Weights[SI->findCaseValue(C: TrueVal)->getSuccessorIndex()];
5022 FalseWeight =
5023 (uint32_t)Weights[SI->findCaseValue(C: FalseVal)->getSuccessorIndex()];
5024 }
5025 }
5026
5027 // Perform the actual simplification.
5028 return simplifyTerminatorOnSelect(OldTerm: SI, Cond: Condition, TrueBB, FalseBB, TrueWeight,
5029 FalseWeight);
5030}
5031
5032// Replaces
5033// (indirectbr (select cond, blockaddress(@fn, BlockA),
5034// blockaddress(@fn, BlockB)))
5035// with
5036// (br cond, BlockA, BlockB).
5037bool SimplifyCFGOpt::simplifyIndirectBrOnSelect(IndirectBrInst *IBI,
5038 SelectInst *SI) {
5039 // Check that both operands of the select are block addresses.
5040 BlockAddress *TBA = dyn_cast<BlockAddress>(Val: SI->getTrueValue());
5041 BlockAddress *FBA = dyn_cast<BlockAddress>(Val: SI->getFalseValue());
5042 if (!TBA || !FBA)
5043 return false;
5044
5045 // Extract the actual blocks.
5046 BasicBlock *TrueBB = TBA->getBasicBlock();
5047 BasicBlock *FalseBB = FBA->getBasicBlock();
5048
5049 // The select's profile becomes the profile of the conditional branch that
5050 // replaces the indirect branch.
5051 SmallVector<uint32_t> SelectBranchWeights(2);
5052 if (!ProfcheckDisableMetadataFixes)
5053 extractBranchWeights(I: *SI, Weights&: SelectBranchWeights);
5054 // Perform the actual simplification.
5055 return simplifyTerminatorOnSelect(OldTerm: IBI, Cond: SI->getCondition(), TrueBB, FalseBB,
5056 TrueWeight: SelectBranchWeights[0],
5057 FalseWeight: SelectBranchWeights[1]);
5058}
5059
5060/// This is called when we find an icmp instruction
5061/// (a seteq/setne with a constant) as the only instruction in a
5062/// block that ends with an uncond branch. We are looking for a very specific
5063/// pattern that occurs when "A == 1 || A == 2 || A == 3" gets simplified. In
5064/// this case, we merge the first two "or's of icmp" into a switch, but then the
5065/// default value goes to an uncond block with a seteq in it, we get something
5066/// like:
5067///
5068/// switch i8 %A, label %DEFAULT [ i8 1, label %end i8 2, label %end ]
5069/// DEFAULT:
5070/// %tmp = icmp eq i8 %A, 92
5071/// br label %end
5072/// end:
5073/// ... = phi i1 [ true, %entry ], [ %tmp, %DEFAULT ], [ true, %entry ]
5074///
5075/// We prefer to split the edge to 'end' so that there is a true/false entry to
5076/// the PHI, merging the third icmp into the switch.
5077bool SimplifyCFGOpt::tryToSimplifyUncondBranchWithICmpInIt(
5078 ICmpInst *ICI, IRBuilder<> &Builder) {
5079 // Select == nullptr means we assume that there is a hidden no-op select
5080 // instruction of `_ = select %icmp, true, false` after `%icmp = icmp ...`
5081 return tryToSimplifyUncondBranchWithICmpSelectInIt(ICI, Select: nullptr, Builder);
5082}
5083
5084/// Similar to tryToSimplifyUncondBranchWithICmpInIt, but handle a more generic
5085/// case. This is called when we find an icmp instruction (a seteq/setne with a
5086/// constant) and its following select instruction as the only TWO instructions
5087/// in a block that ends with an uncond branch. We are looking for a very
5088/// specific pattern that occurs when "
5089/// if (A == 1) return C1;
5090/// if (A == 2) return C2;
5091/// if (A < 3) return C3;
5092/// return C4;
5093/// " gets simplified. In this case, we merge the first two "branches of icmp"
5094/// into a switch, but then the default value goes to an uncond block with a lt
5095/// icmp and select in it, as InstCombine can not simplify "A < 3" as "A == 2".
5096/// After SimplifyCFG and other subsequent optimizations (e.g., SCCP), we might
5097/// get something like:
5098///
5099/// case1:
5100/// switch i8 %A, label %DEFAULT [ i8 0, label %end i8 1, label %case2 ]
5101/// case2:
5102/// br label %end
5103/// DEFAULT:
5104/// %tmp = icmp eq i8 %A, 2
5105/// %val = select i1 %tmp, i8 C3, i8 C4
5106/// br label %end
5107/// end:
5108/// _ = phi i8 [ C1, %case1 ], [ C2, %case2 ], [ %val, %DEFAULT ]
5109///
5110/// We prefer to split the edge to 'end' so that there are TWO entries of V3/V4
5111/// to the PHI, merging the icmp & select into the switch, as follows:
5112///
5113/// case1:
5114/// switch i8 %A, label %DEFAULT [
5115/// i8 0, label %end
5116/// i8 1, label %case2
5117/// i8 2, label %case3
5118/// ]
5119/// case2:
5120/// br label %end
5121/// case3:
5122/// br label %end
5123/// DEFAULT:
5124/// br label %end
5125/// end:
5126/// _ = phi i8 [ C1, %case1 ], [ C2, %case2 ], [ C3, %case2 ], [ C4, %DEFAULT]
5127bool SimplifyCFGOpt::tryToSimplifyUncondBranchWithICmpSelectInIt(
5128 ICmpInst *ICI, SelectInst *Select, IRBuilder<> &Builder) {
5129 BasicBlock *BB = ICI->getParent();
5130
5131 // If the block has any PHIs in it or the icmp/select has multiple uses, it is
5132 // too complex.
5133 /// TODO: support multi-phis in succ BB of select's BB.
5134 if (isa<PHINode>(Val: BB->begin()) || !ICI->hasOneUse() ||
5135 (Select && !Select->hasOneUse()))
5136 return false;
5137
5138 // The pattern we're looking for is where our only predecessor is a switch on
5139 // 'V' and this block is the default case for the switch. In this case we can
5140 // fold the compared value into the switch to simplify things.
5141 BasicBlock *Pred = BB->getSinglePredecessor();
5142 if (!Pred || !isa<SwitchInst>(Val: Pred->getTerminator()))
5143 return false;
5144
5145 Value *IcmpCond;
5146 ConstantInt *NewCaseVal;
5147 CmpPredicate Predicate;
5148
5149 // Match icmp X, C
5150 if (!match(V: ICI,
5151 P: m_ICmp(Pred&: Predicate, L: m_Value(V&: IcmpCond), R: m_ConstantInt(CI&: NewCaseVal))))
5152 return false;
5153
5154 Value *SelectCond, *SelectTrueVal, *SelectFalseVal;
5155 Instruction *User;
5156 if (!Select) {
5157 // If Select == nullptr, we can assume that there is a hidden no-op select
5158 // just after icmp
5159 SelectCond = ICI;
5160 SelectTrueVal = Builder.getTrue();
5161 SelectFalseVal = Builder.getFalse();
5162 User = ICI->user_back();
5163 } else {
5164 SelectCond = Select->getCondition();
5165 // Check if the select condition is the same as the icmp condition.
5166 if (SelectCond != ICI)
5167 return false;
5168 SelectTrueVal = Select->getTrueValue();
5169 SelectFalseVal = Select->getFalseValue();
5170 User = Select->user_back();
5171 }
5172
5173 SwitchInst *SI = cast<SwitchInst>(Val: Pred->getTerminator());
5174 if (SI->getCondition() != IcmpCond)
5175 return false;
5176
5177 // If BB is reachable on a non-default case, then we simply know the value of
5178 // V in this block. Substitute it and constant fold the icmp instruction
5179 // away.
5180 if (SI->getDefaultDest() != BB) {
5181 ConstantInt *VVal = SI->findCaseDest(BB);
5182 assert(VVal && "Should have a unique destination value");
5183 ICI->setOperand(i_nocapture: 0, Val_nocapture: VVal);
5184
5185 if (Value *V = simplifyInstruction(I: ICI, Q: {DL, ICI})) {
5186 ICI->replaceAllUsesWith(V);
5187 ICI->eraseFromParent();
5188 }
5189 // BB is now empty, so it is likely to simplify away.
5190 return requestResimplify();
5191 }
5192
5193 // Ok, the block is reachable from the default dest. If the constant we're
5194 // comparing exists in one of the other edges, then we can constant fold ICI
5195 // and zap it.
5196 if (SI->findCaseValue(C: NewCaseVal) != SI->case_default()) {
5197 Value *V;
5198 if (Predicate == ICmpInst::ICMP_EQ)
5199 V = ConstantInt::getFalse(Context&: BB->getContext());
5200 else
5201 V = ConstantInt::getTrue(Context&: BB->getContext());
5202
5203 ICI->replaceAllUsesWith(V);
5204 ICI->eraseFromParent();
5205 // BB is now empty, so it is likely to simplify away.
5206 return requestResimplify();
5207 }
5208
5209 // The use of the select has to be in the 'end' block, by the only PHI node in
5210 // the block.
5211 BasicBlock *SuccBlock = BB->getTerminator()->getSuccessor(Idx: 0);
5212 PHINode *PHIUse = dyn_cast<PHINode>(Val: User);
5213 if (PHIUse == nullptr || PHIUse != &SuccBlock->front() ||
5214 isa<PHINode>(Val: ++BasicBlock::iterator(PHIUse)))
5215 return false;
5216
5217 // If the icmp is a SETEQ, then the default dest gets SelectFalseVal, the new
5218 // edge gets SelectTrueVal in the PHI.
5219 Value *DefaultCst = SelectFalseVal;
5220 Value *NewCst = SelectTrueVal;
5221
5222 if (ICI->getPredicate() == ICmpInst::ICMP_NE)
5223 std::swap(a&: DefaultCst, b&: NewCst);
5224
5225 // Replace Select (which is used by the PHI for the default value) with
5226 // SelectFalseVal or SelectTrueVal depending on if ICI is EQ or NE.
5227 if (Select) {
5228 Select->replaceAllUsesWith(V: DefaultCst);
5229 Select->eraseFromParent();
5230 } else {
5231 ICI->replaceAllUsesWith(V: DefaultCst);
5232 }
5233 ICI->eraseFromParent();
5234
5235 SmallVector<DominatorTree::UpdateType, 2> Updates;
5236
5237 // Okay, the switch goes to this block on a default value. Add an edge from
5238 // the switch to the merge point on the compared value.
5239 BasicBlock *NewBB =
5240 BasicBlock::Create(Context&: BB->getContext(), Name: "switch.edge", Parent: BB->getParent(), InsertBefore: BB);
5241 {
5242 SwitchInstProfUpdateWrapper SIW(*SI);
5243 auto W0 = SIW.getSuccessorWeight(idx: 0);
5244 SwitchInstProfUpdateWrapper::CaseWeightOpt NewW;
5245 if (W0) {
5246 NewW = ((uint64_t(*W0) + 1) >> 1);
5247 SIW.setSuccessorWeight(idx: 0, W: *NewW);
5248 }
5249 SIW.addCase(OnVal: NewCaseVal, Dest: NewBB, W: NewW);
5250 if (DTU)
5251 Updates.push_back(Elt: {DominatorTree::Insert, Pred, NewBB});
5252 }
5253
5254 // NewBB branches to the phi block, add the uncond branch and the phi entry.
5255 Builder.SetInsertPoint(NewBB);
5256 Builder.SetCurrentDebugLocation(SI->getDebugLoc());
5257 Builder.CreateBr(Dest: SuccBlock);
5258 PHIUse->addIncoming(V: NewCst, BB: NewBB);
5259 if (DTU) {
5260 Updates.push_back(Elt: {DominatorTree::Insert, NewBB, SuccBlock});
5261 DTU->applyUpdates(Updates);
5262 }
5263 return true;
5264}
5265
5266/// Check to see if it is branching on an or/and chain of icmp instructions, and
5267/// fold it into a switch instruction if so.
5268bool SimplifyCFGOpt::simplifyBranchOnICmpChain(CondBrInst *BI,
5269 IRBuilder<> &Builder,
5270 const DataLayout &DL) {
5271 Instruction *Cond = dyn_cast<Instruction>(Val: BI->getCondition());
5272 if (!Cond)
5273 return false;
5274
5275 // Change br (X == 0 | X == 1), T, F into a switch instruction.
5276 // If this is a bunch of seteq's or'd together, or if it's a bunch of
5277 // 'setne's and'ed together, collect them.
5278
5279 // Try to gather values from a chain of and/or to be turned into a switch
5280 ConstantComparesGatherer ConstantCompare(Cond, DL);
5281 // Unpack the result
5282 SmallVectorImpl<ConstantInt *> &Values = ConstantCompare.Vals;
5283 Value *CompVal = ConstantCompare.CompValue;
5284 unsigned UsedICmps = ConstantCompare.UsedICmps;
5285 Value *ExtraCase = ConstantCompare.Extra;
5286 bool TrueWhenEqual = ConstantCompare.IsEq;
5287
5288 // If we didn't have a multiply compared value, fail.
5289 if (!CompVal)
5290 return false;
5291
5292 // Avoid turning single icmps into a switch.
5293 if (UsedICmps <= 1)
5294 return false;
5295
5296 // There might be duplicate constants in the list, which the switch
5297 // instruction can't handle, remove them now.
5298 array_pod_sort(Start: Values.begin(), End: Values.end(), Compare: constantIntSortPredicate);
5299 Values.erase(CS: llvm::unique(R&: Values), CE: Values.end());
5300
5301 // If Extra was used, we require at least two switch values to do the
5302 // transformation. A switch with one value is just a conditional branch.
5303 if (ExtraCase && Values.size() < 2)
5304 return false;
5305
5306 SmallVector<uint32_t> BranchWeights;
5307 const bool HasProfile = !ProfcheckDisableMetadataFixes &&
5308 extractBranchWeights(I: *BI, Weights&: BranchWeights);
5309
5310 // Figure out which block is which destination.
5311 BasicBlock *DefaultBB = BI->getSuccessor(i: 1);
5312 BasicBlock *EdgeBB = BI->getSuccessor(i: 0);
5313 if (!TrueWhenEqual) {
5314 std::swap(a&: DefaultBB, b&: EdgeBB);
5315 if (HasProfile)
5316 std::swap(a&: BranchWeights[0], b&: BranchWeights[1]);
5317 }
5318
5319 BasicBlock *BB = BI->getParent();
5320
5321 LLVM_DEBUG(dbgs() << "Converting 'icmp' chain with " << Values.size()
5322 << " cases into SWITCH. BB is:\n"
5323 << *BB);
5324
5325 SmallVector<DominatorTree::UpdateType, 2> Updates;
5326
5327 // If there are any extra values that couldn't be folded into the switch
5328 // then we evaluate them with an explicit branch first. Split the block
5329 // right before the condbr to handle it.
5330 if (ExtraCase) {
5331 BasicBlock *NewBB = SplitBlock(Old: BB, SplitPt: BI, DTU, /*LI=*/nullptr,
5332 /*MSSAU=*/nullptr, BBName: "switch.early.test");
5333
5334 // Remove the uncond branch added to the old block.
5335 Instruction *OldTI = BB->getTerminator();
5336 Builder.SetInsertPoint(OldTI);
5337
5338 // There can be an unintended UB if extra values are Poison. Before the
5339 // transformation, extra values may not be evaluated according to the
5340 // condition, and it will not raise UB. But after transformation, we are
5341 // evaluating extra values before checking the condition, and it will raise
5342 // UB. It can be solved by adding freeze instruction to extra values.
5343 AssumptionCache *AC = Options.AC;
5344
5345 if (!isGuaranteedNotToBeUndefOrPoison(V: ExtraCase, AC, CtxI: BI, DT: nullptr))
5346 ExtraCase = Builder.CreateFreeze(V: ExtraCase);
5347
5348 // We don't have any info about this condition.
5349 auto *Br = TrueWhenEqual ? Builder.CreateCondBr(Cond: ExtraCase, True: EdgeBB, False: NewBB)
5350 : Builder.CreateCondBr(Cond: ExtraCase, True: NewBB, False: EdgeBB);
5351 setExplicitlyUnknownBranchWeightsIfProfiled(I&: *Br, DEBUG_TYPE);
5352
5353 OldTI->eraseFromParent();
5354
5355 if (DTU)
5356 Updates.push_back(Elt: {DominatorTree::Insert, BB, EdgeBB});
5357
5358 // If there are PHI nodes in EdgeBB, then we need to add a new entry to them
5359 // for the edge we just added.
5360 addPredecessorToBlock(Succ: EdgeBB, NewPred: BB, ExistPred: NewBB);
5361
5362 LLVM_DEBUG(dbgs() << " ** 'icmp' chain unhandled condition: " << *ExtraCase
5363 << "\nEXTRABB = " << *BB);
5364 BB = NewBB;
5365 }
5366
5367 Builder.SetInsertPoint(BI);
5368 // Convert pointer to int before we switch.
5369 if (CompVal->getType()->isPointerTy()) {
5370 assert(!DL.hasUnstableRepresentation(CompVal->getType()) &&
5371 "Should not end up here with unstable pointers");
5372 CompVal = Builder.CreatePtrToInt(
5373 V: CompVal, DestTy: DL.getIntPtrType(CompVal->getType()), Name: "magicptr");
5374 }
5375
5376 // Check if we can represent the values as a contiguous range. If so, we use a
5377 // range check + conditional branch instead of a switch.
5378 if (Values.front()->getValue() - Values.back()->getValue() ==
5379 Values.size() - 1) {
5380 ConstantRange RangeToCheck = ConstantRange::getNonEmpty(
5381 Lower: Values.back()->getValue(), Upper: Values.front()->getValue() + 1);
5382 APInt Offset, RHS;
5383 ICmpInst::Predicate Pred;
5384 RangeToCheck.getEquivalentICmp(Pred, RHS, Offset);
5385 Value *X = CompVal;
5386 if (!Offset.isZero())
5387 X = Builder.CreateAdd(LHS: X, RHS: ConstantInt::get(Ty: CompVal->getType(), V: Offset));
5388 Value *Cond =
5389 Builder.CreateICmp(P: Pred, LHS: X, RHS: ConstantInt::get(Ty: CompVal->getType(), V: RHS));
5390 CondBrInst *NewBI = Builder.CreateCondBr(Cond, True: EdgeBB, False: DefaultBB);
5391 if (HasProfile)
5392 setBranchWeights(I&: *NewBI, Weights: BranchWeights, /*IsExpected=*/false);
5393 // We don't need to update PHI nodes since we don't add any new edges.
5394 } else {
5395 // Create the new switch instruction now.
5396 SwitchInst *New = Builder.CreateSwitch(V: CompVal, Dest: DefaultBB, NumCases: Values.size());
5397 if (HasProfile) {
5398 // We know the weight of the default case. We don't know the weight of the
5399 // other cases, but rather than completely lose profiling info, we split
5400 // the remaining probability equally over them.
5401 SmallVector<uint32_t> NewWeights(Values.size() + 1);
5402 NewWeights[0] = BranchWeights[1]; // this is the default, and we swapped
5403 // if TrueWhenEqual.
5404 for (auto &V : drop_begin(RangeOrContainer&: NewWeights))
5405 V = BranchWeights[0] / Values.size();
5406 setBranchWeights(I&: *New, Weights: NewWeights, /*IsExpected=*/false);
5407 }
5408
5409 // Add all of the 'cases' to the switch instruction.
5410 for (ConstantInt *Val : Values)
5411 New->addCase(OnVal: Val, Dest: EdgeBB);
5412
5413 // We added edges from PI to the EdgeBB. As such, if there were any
5414 // PHI nodes in EdgeBB, they need entries to be added corresponding to
5415 // the number of edges added.
5416 for (BasicBlock::iterator BBI = EdgeBB->begin(); isa<PHINode>(Val: BBI); ++BBI) {
5417 PHINode *PN = cast<PHINode>(Val&: BBI);
5418 Value *InVal = PN->getIncomingValueForBlock(BB);
5419 for (unsigned i = 0, e = Values.size() - 1; i != e; ++i)
5420 PN->addIncoming(V: InVal, BB);
5421 }
5422 }
5423
5424 // Erase the old branch instruction.
5425 eraseTerminatorAndDCECond(TI: BI);
5426 if (DTU)
5427 DTU->applyUpdates(Updates);
5428
5429 LLVM_DEBUG(dbgs() << " ** 'icmp' chain result is:\n" << *BB << '\n');
5430 return true;
5431}
5432
5433bool SimplifyCFGOpt::simplifyResume(ResumeInst *RI, IRBuilder<> &Builder) {
5434 if (isa<PHINode>(Val: RI->getValue()))
5435 return simplifyCommonResume(RI);
5436 else if (isa<LandingPadInst>(Val: RI->getParent()->getFirstNonPHIIt()) &&
5437 RI->getValue() == &*RI->getParent()->getFirstNonPHIIt())
5438 // The resume must unwind the exception that caused control to branch here.
5439 return simplifySingleResume(RI);
5440
5441 return false;
5442}
5443
5444// Check if cleanup block is empty
5445static bool isCleanupBlockEmpty(iterator_range<BasicBlock::iterator> R) {
5446 for (Instruction &I : R) {
5447 auto *II = dyn_cast<IntrinsicInst>(Val: &I);
5448 if (!II)
5449 return false;
5450
5451 Intrinsic::ID IntrinsicID = II->getIntrinsicID();
5452 switch (IntrinsicID) {
5453 case Intrinsic::dbg_declare:
5454 case Intrinsic::dbg_value:
5455 case Intrinsic::dbg_label:
5456 case Intrinsic::lifetime_end:
5457 break;
5458 default:
5459 return false;
5460 }
5461 }
5462 return true;
5463}
5464
5465// Simplify resume that is shared by several landing pads (phi of landing pad).
5466bool SimplifyCFGOpt::simplifyCommonResume(ResumeInst *RI) {
5467 BasicBlock *BB = RI->getParent();
5468
5469 // Check that there are no other instructions except for debug and lifetime
5470 // intrinsics between the phi's and resume instruction.
5471 if (!isCleanupBlockEmpty(R: make_range(x: RI->getParent()->getFirstNonPHIIt(),
5472 y: BB->getTerminator()->getIterator())))
5473 return false;
5474
5475 SmallSetVector<BasicBlock *, 4> TrivialUnwindBlocks;
5476 auto *PhiLPInst = cast<PHINode>(Val: RI->getValue());
5477
5478 // Check incoming blocks to see if any of them are trivial.
5479 for (unsigned Idx = 0, End = PhiLPInst->getNumIncomingValues(); Idx != End;
5480 Idx++) {
5481 auto *IncomingBB = PhiLPInst->getIncomingBlock(i: Idx);
5482 auto *IncomingValue = PhiLPInst->getIncomingValue(i: Idx);
5483
5484 // If the block has other successors, we can not delete it because
5485 // it has other dependents.
5486 if (IncomingBB->getUniqueSuccessor() != BB)
5487 continue;
5488
5489 auto *LandingPad = dyn_cast<LandingPadInst>(Val: IncomingBB->getFirstNonPHIIt());
5490 // Not the landing pad that caused the control to branch here.
5491 if (IncomingValue != LandingPad)
5492 continue;
5493
5494 if (isCleanupBlockEmpty(
5495 R: make_range(x: LandingPad->getNextNode(), y: IncomingBB->getTerminator())))
5496 TrivialUnwindBlocks.insert(X: IncomingBB);
5497 }
5498
5499 // If no trivial unwind blocks, don't do any simplifications.
5500 if (TrivialUnwindBlocks.empty())
5501 return false;
5502
5503 // Turn all invokes that unwind here into calls.
5504 for (auto *TrivialBB : TrivialUnwindBlocks) {
5505 // Blocks that will be simplified should be removed from the phi node.
5506 // Note there could be multiple edges to the resume block, and we need
5507 // to remove them all.
5508 while (PhiLPInst->getBasicBlockIndex(BB: TrivialBB) != -1)
5509 BB->removePredecessor(Pred: TrivialBB, KeepOneInputPHIs: true);
5510
5511 for (BasicBlock *Pred :
5512 llvm::make_early_inc_range(Range: predecessors(BB: TrivialBB))) {
5513 removeUnwindEdge(BB: Pred, DTU);
5514 ++NumInvokes;
5515 }
5516
5517 // In each SimplifyCFG run, only the current processed block can be erased.
5518 // Otherwise, it will break the iteration of SimplifyCFG pass. So instead
5519 // of erasing TrivialBB, we only remove the branch to the common resume
5520 // block so that we can later erase the resume block since it has no
5521 // predecessors.
5522 TrivialBB->getTerminator()->eraseFromParent();
5523 new UnreachableInst(RI->getContext(), TrivialBB);
5524 if (DTU)
5525 DTU->applyUpdates(Updates: {{DominatorTree::Delete, TrivialBB, BB}});
5526 }
5527
5528 // Delete the resume block if all its predecessors have been removed.
5529 if (pred_empty(BB))
5530 DeleteDeadBlock(BB, DTU);
5531
5532 return !TrivialUnwindBlocks.empty();
5533}
5534
5535// Simplify resume that is only used by a single (non-phi) landing pad.
5536bool SimplifyCFGOpt::simplifySingleResume(ResumeInst *RI) {
5537 BasicBlock *BB = RI->getParent();
5538 auto *LPInst = cast<LandingPadInst>(Val: BB->getFirstNonPHIIt());
5539 assert(RI->getValue() == LPInst &&
5540 "Resume must unwind the exception that caused control to here");
5541
5542 // Check that there are no other instructions except for debug intrinsics.
5543 if (!isCleanupBlockEmpty(
5544 R: make_range<Instruction *>(x: LPInst->getNextNode(), y: RI)))
5545 return false;
5546
5547 // Turn all invokes that unwind here into calls and delete the basic block.
5548 for (BasicBlock *Pred : llvm::make_early_inc_range(Range: predecessors(BB))) {
5549 removeUnwindEdge(BB: Pred, DTU);
5550 ++NumInvokes;
5551 }
5552
5553 // The landingpad is now unreachable. Zap it.
5554 DeleteDeadBlock(BB, DTU);
5555 return true;
5556}
5557
5558static bool removeEmptyCleanup(CleanupReturnInst *RI, DomTreeUpdater *DTU) {
5559 // If this is a trivial cleanup pad that executes no instructions, it can be
5560 // eliminated. If the cleanup pad continues to the caller, any predecessor
5561 // that is an EH pad will be updated to continue to the caller and any
5562 // predecessor that terminates with an invoke instruction will have its invoke
5563 // instruction converted to a call instruction. If the cleanup pad being
5564 // simplified does not continue to the caller, each predecessor will be
5565 // updated to continue to the unwind destination of the cleanup pad being
5566 // simplified.
5567 BasicBlock *BB = RI->getParent();
5568 CleanupPadInst *CPInst = RI->getCleanupPad();
5569 if (CPInst->getParent() != BB)
5570 // This isn't an empty cleanup.
5571 return false;
5572
5573 // We cannot kill the pad if it has multiple uses. This typically arises
5574 // from unreachable basic blocks.
5575 if (!CPInst->hasOneUse())
5576 return false;
5577
5578 // Check that there are no other instructions except for benign intrinsics.
5579 if (!isCleanupBlockEmpty(
5580 R: make_range<Instruction *>(x: CPInst->getNextNode(), y: RI)))
5581 return false;
5582
5583 // If the cleanup return we are simplifying unwinds to the caller, this will
5584 // set UnwindDest to nullptr.
5585 BasicBlock *UnwindDest = RI->getUnwindDest();
5586
5587 // We're about to remove BB from the control flow. Before we do, sink any
5588 // PHINodes into the unwind destination. Doing this before changing the
5589 // control flow avoids some potentially slow checks, since we can currently
5590 // be certain that UnwindDest and BB have no common predecessors (since they
5591 // are both EH pads).
5592 if (UnwindDest) {
5593 // First, go through the PHI nodes in UnwindDest and update any nodes that
5594 // reference the block we are removing
5595 for (PHINode &DestPN : UnwindDest->phis()) {
5596 int Idx = DestPN.getBasicBlockIndex(BB);
5597 // Since BB unwinds to UnwindDest, it has to be in the PHI node.
5598 assert(Idx != -1);
5599 // This PHI node has an incoming value that corresponds to a control
5600 // path through the cleanup pad we are removing. If the incoming
5601 // value is in the cleanup pad, it must be a PHINode (because we
5602 // verified above that the block is otherwise empty). Otherwise, the
5603 // value is either a constant or a value that dominates the cleanup
5604 // pad being removed.
5605 //
5606 // Because BB and UnwindDest are both EH pads, all of their
5607 // predecessors must unwind to these blocks, and since no instruction
5608 // can have multiple unwind destinations, there will be no overlap in
5609 // incoming blocks between SrcPN and DestPN.
5610 Value *SrcVal = DestPN.getIncomingValue(i: Idx);
5611 PHINode *SrcPN = dyn_cast<PHINode>(Val: SrcVal);
5612
5613 bool NeedPHITranslation = SrcPN && SrcPN->getParent() == BB;
5614 for (auto *Pred : predecessors(BB)) {
5615 Value *Incoming =
5616 NeedPHITranslation ? SrcPN->getIncomingValueForBlock(BB: Pred) : SrcVal;
5617 DestPN.addIncoming(V: Incoming, BB: Pred);
5618 }
5619 }
5620
5621 // Sink any remaining PHI nodes directly into UnwindDest.
5622 BasicBlock::iterator InsertPt = UnwindDest->getFirstNonPHIIt();
5623 for (PHINode &PN : make_early_inc_range(Range: BB->phis())) {
5624 if (PN.use_empty() || !PN.isUsedOutsideOfBlock(BB))
5625 // If the PHI node has no uses or all of its uses are in this basic
5626 // block (meaning they are debug or lifetime intrinsics), just leave
5627 // it. It will be erased when we erase BB below.
5628 continue;
5629
5630 // Otherwise, sink this PHI node into UnwindDest.
5631 // Any predecessors to UnwindDest which are not already represented
5632 // must be back edges which inherit the value from the path through
5633 // BB. In this case, the PHI value must reference itself.
5634 for (auto *pred : predecessors(BB: UnwindDest))
5635 if (pred != BB)
5636 PN.addIncoming(V: &PN, BB: pred);
5637 PN.moveBefore(InsertPos: InsertPt);
5638 // Also, add a dummy incoming value for the original BB itself,
5639 // so that the PHI is well-formed until we drop said predecessor.
5640 PN.addIncoming(V: PoisonValue::get(T: PN.getType()), BB);
5641 }
5642 }
5643
5644 std::vector<DominatorTree::UpdateType> Updates;
5645
5646 // We use make_early_inc_range here because we will remove all predecessors.
5647 for (BasicBlock *PredBB : llvm::make_early_inc_range(Range: predecessors(BB))) {
5648 if (UnwindDest == nullptr) {
5649 if (DTU) {
5650 DTU->applyUpdates(Updates);
5651 Updates.clear();
5652 }
5653 removeUnwindEdge(BB: PredBB, DTU);
5654 ++NumInvokes;
5655 } else {
5656 BB->removePredecessor(Pred: PredBB);
5657 Instruction *TI = PredBB->getTerminator();
5658 TI->replaceUsesOfWith(From: BB, To: UnwindDest);
5659 if (DTU) {
5660 Updates.push_back(x: {DominatorTree::Insert, PredBB, UnwindDest});
5661 Updates.push_back(x: {DominatorTree::Delete, PredBB, BB});
5662 }
5663 }
5664 }
5665
5666 if (DTU)
5667 DTU->applyUpdates(Updates);
5668
5669 DeleteDeadBlock(BB, DTU);
5670
5671 return true;
5672}
5673
5674// Try to merge two cleanuppads together.
5675static bool mergeCleanupPad(CleanupReturnInst *RI) {
5676 // Skip any cleanuprets which unwind to caller, there is nothing to merge
5677 // with.
5678 BasicBlock *UnwindDest = RI->getUnwindDest();
5679 if (!UnwindDest)
5680 return false;
5681
5682 // This cleanupret isn't the only predecessor of this cleanuppad, it wouldn't
5683 // be safe to merge without code duplication.
5684 if (UnwindDest->getSinglePredecessor() != RI->getParent())
5685 return false;
5686
5687 // Verify that our cleanuppad's unwind destination is another cleanuppad.
5688 auto *SuccessorCleanupPad = dyn_cast<CleanupPadInst>(Val: &UnwindDest->front());
5689 if (!SuccessorCleanupPad)
5690 return false;
5691
5692 CleanupPadInst *PredecessorCleanupPad = RI->getCleanupPad();
5693 // Replace any uses of the successor cleanupad with the predecessor pad
5694 // The only cleanuppad uses should be this cleanupret, it's cleanupret and
5695 // funclet bundle operands.
5696 SuccessorCleanupPad->replaceAllUsesWith(V: PredecessorCleanupPad);
5697 // Remove the old cleanuppad.
5698 SuccessorCleanupPad->eraseFromParent();
5699 // Now, we simply replace the cleanupret with a branch to the unwind
5700 // destination.
5701 UncondBrInst::Create(Target: UnwindDest, InsertBefore: RI->getParent());
5702 RI->eraseFromParent();
5703
5704 return true;
5705}
5706
5707bool SimplifyCFGOpt::simplifyCleanupReturn(CleanupReturnInst *RI) {
5708 // It is possible to transiantly have an undef cleanuppad operand because we
5709 // have deleted some, but not all, dead blocks.
5710 // Eventually, this block will be deleted.
5711 if (isa<UndefValue>(Val: RI->getOperand(i_nocapture: 0)))
5712 return false;
5713
5714 if (mergeCleanupPad(RI))
5715 return true;
5716
5717 if (removeEmptyCleanup(RI, DTU))
5718 return true;
5719
5720 return false;
5721}
5722
5723// WARNING: keep in sync with InstCombinerImpl::visitUnreachableInst()!
5724bool SimplifyCFGOpt::simplifyUnreachable(UnreachableInst *UI) {
5725 BasicBlock *BB = UI->getParent();
5726
5727 bool Changed = false;
5728
5729 // Ensure that any debug-info records that used to occur after the Unreachable
5730 // are moved to in front of it -- otherwise they'll "dangle" at the end of
5731 // the block.
5732 BB->flushTerminatorDbgRecords();
5733
5734 // Debug-info records on the unreachable inst itself should be deleted, as
5735 // below we delete everything past the final executable instruction.
5736 UI->dropDbgRecords();
5737
5738 // If there are any instructions immediately before the unreachable that can
5739 // be removed, do so.
5740 while (UI->getIterator() != BB->begin()) {
5741 BasicBlock::iterator BBI = UI->getIterator();
5742 --BBI;
5743
5744 if (!isGuaranteedToTransferExecutionToSuccessor(I: &*BBI))
5745 break; // Can not drop any more instructions. We're done here.
5746 // Otherwise, this instruction can be freely erased,
5747 // even if it is not side-effect free.
5748
5749 // Note that deleting EH's here is in fact okay, although it involves a bit
5750 // of subtle reasoning. If this inst is an EH, all the predecessors of this
5751 // block will be the unwind edges of Invoke/CatchSwitch/CleanupReturn,
5752 // and we can therefore guarantee this block will be erased.
5753
5754 // If we're deleting this, we're deleting any subsequent debug info, so
5755 // delete DbgRecords.
5756 BBI->dropDbgRecords();
5757
5758 // Delete this instruction (any uses are guaranteed to be dead)
5759 BBI->replaceAllUsesWith(V: PoisonValue::get(T: BBI->getType()));
5760 BBI->eraseFromParent();
5761 Changed = true;
5762 }
5763
5764 // If the unreachable instruction is the first in the block, take a gander
5765 // at all of the predecessors of this instruction, and simplify them.
5766 if (&BB->front() != UI)
5767 return Changed;
5768
5769 std::vector<DominatorTree::UpdateType> Updates;
5770
5771 SmallSetVector<BasicBlock *, 8> Preds(pred_begin(BB), pred_end(BB));
5772 for (BasicBlock *Predecessor : Preds) {
5773 Instruction *TI = Predecessor->getTerminator();
5774 IRBuilder<> Builder(TI);
5775 if (isa<UncondBrInst>(Val: TI)) {
5776 new UnreachableInst(TI->getContext(), TI->getIterator());
5777 TI->eraseFromParent();
5778 Changed = true;
5779 if (DTU)
5780 Updates.push_back(x: {DominatorTree::Delete, Predecessor, BB});
5781 } else if (auto *BI = dyn_cast<CondBrInst>(Val: TI)) {
5782 // We could either have a proper unconditional branch,
5783 // or a degenerate conditional branch with matching destinations.
5784 if (BI->getSuccessor(i: 0) == BI->getSuccessor(i: 1)) {
5785 new UnreachableInst(TI->getContext(), TI->getIterator());
5786 TI->eraseFromParent();
5787 Changed = true;
5788 } else {
5789 Value* Cond = BI->getCondition();
5790 assert(BI->getSuccessor(0) != BI->getSuccessor(1) &&
5791 "The destinations are guaranteed to be different here.");
5792 CallInst *Assumption;
5793 if (BI->getSuccessor(i: 0) == BB) {
5794 Assumption = Builder.CreateAssumption(Cond: Builder.CreateNot(V: Cond));
5795 Builder.CreateBr(Dest: BI->getSuccessor(i: 1));
5796 } else {
5797 assert(BI->getSuccessor(1) == BB && "Incorrect CFG");
5798 Assumption = Builder.CreateAssumption(Cond);
5799 Builder.CreateBr(Dest: BI->getSuccessor(i: 0));
5800 }
5801 if (Options.AC)
5802 Options.AC->registerAssumption(CI: cast<AssumeInst>(Val: Assumption));
5803
5804 eraseTerminatorAndDCECond(TI: BI);
5805 Changed = true;
5806 }
5807 if (DTU)
5808 Updates.push_back(x: {DominatorTree::Delete, Predecessor, BB});
5809 } else if (auto *SI = dyn_cast<SwitchInst>(Val: TI)) {
5810 SwitchInstProfUpdateWrapper SU(*SI);
5811 for (auto i = SU->case_begin(), e = SU->case_end(); i != e;) {
5812 if (i->getCaseSuccessor() != BB) {
5813 ++i;
5814 continue;
5815 }
5816 BB->removePredecessor(Pred: SU->getParent());
5817 i = SU.removeCase(I: i);
5818 e = SU->case_end();
5819 Changed = true;
5820 }
5821 // Note that the default destination can't be removed!
5822 if (DTU && SI->getDefaultDest() != BB)
5823 Updates.push_back(x: {DominatorTree::Delete, Predecessor, BB});
5824 } else if (auto *II = dyn_cast<InvokeInst>(Val: TI)) {
5825 if (II->getUnwindDest() == BB) {
5826 if (DTU) {
5827 DTU->applyUpdates(Updates);
5828 Updates.clear();
5829 }
5830 auto *CI = cast<CallInst>(Val: removeUnwindEdge(BB: TI->getParent(), DTU));
5831 if (!CI->doesNotThrow())
5832 CI->setDoesNotThrow();
5833 Changed = true;
5834 }
5835 } else if (auto *CSI = dyn_cast<CatchSwitchInst>(Val: TI)) {
5836 if (CSI->getUnwindDest() == BB) {
5837 if (DTU) {
5838 DTU->applyUpdates(Updates);
5839 Updates.clear();
5840 }
5841 removeUnwindEdge(BB: TI->getParent(), DTU);
5842 Changed = true;
5843 continue;
5844 }
5845
5846 for (CatchSwitchInst::handler_iterator I = CSI->handler_begin(),
5847 E = CSI->handler_end();
5848 I != E; ++I) {
5849 if (*I == BB) {
5850 CSI->removeHandler(HI: I);
5851 --I;
5852 --E;
5853 Changed = true;
5854 }
5855 }
5856 if (DTU)
5857 Updates.push_back(x: {DominatorTree::Delete, Predecessor, BB});
5858 if (CSI->getNumHandlers() == 0) {
5859 if (CSI->hasUnwindDest()) {
5860 // Redirect all predecessors of the block containing CatchSwitchInst
5861 // to instead branch to the CatchSwitchInst's unwind destination.
5862 if (DTU) {
5863 for (auto *PredecessorOfPredecessor : predecessors(BB: Predecessor)) {
5864 Updates.push_back(x: {DominatorTree::Insert,
5865 PredecessorOfPredecessor,
5866 CSI->getUnwindDest()});
5867 Updates.push_back(x: {DominatorTree::Delete,
5868 PredecessorOfPredecessor, Predecessor});
5869 }
5870 }
5871 Predecessor->replaceAllUsesWith(V: CSI->getUnwindDest());
5872 } else {
5873 // Rewrite all preds to unwind to caller (or from invoke to call).
5874 if (DTU) {
5875 DTU->applyUpdates(Updates);
5876 Updates.clear();
5877 }
5878 SmallVector<BasicBlock *, 8> EHPreds(predecessors(BB: Predecessor));
5879 for (BasicBlock *EHPred : EHPreds)
5880 removeUnwindEdge(BB: EHPred, DTU);
5881 }
5882 // The catchswitch is no longer reachable.
5883 new UnreachableInst(CSI->getContext(), CSI->getIterator());
5884 CSI->eraseFromParent();
5885 Changed = true;
5886 }
5887 } else if (auto *CRI = dyn_cast<CleanupReturnInst>(Val: TI)) {
5888 (void)CRI;
5889 assert(CRI->hasUnwindDest() && CRI->getUnwindDest() == BB &&
5890 "Expected to always have an unwind to BB.");
5891 if (DTU)
5892 Updates.push_back(x: {DominatorTree::Delete, Predecessor, BB});
5893 new UnreachableInst(TI->getContext(), TI->getIterator());
5894 TI->eraseFromParent();
5895 Changed = true;
5896 }
5897 }
5898
5899 if (DTU)
5900 DTU->applyUpdates(Updates);
5901
5902 // If this block is now dead, remove it.
5903 if (pred_empty(BB) && BB != &BB->getParent()->getEntryBlock()) {
5904 DeleteDeadBlock(BB, DTU);
5905 return true;
5906 }
5907
5908 return Changed;
5909}
5910
5911struct ContiguousCasesResult {
5912 ConstantInt *Min;
5913 ConstantInt *Max;
5914 BasicBlock *Dest;
5915 BasicBlock *OtherDest;
5916 SmallVectorImpl<ConstantInt *> *Cases;
5917 SmallVectorImpl<ConstantInt *> *OtherCases;
5918};
5919
5920static std::optional<ContiguousCasesResult>
5921findContiguousCases(Value *Condition, SmallVectorImpl<ConstantInt *> &Cases,
5922 SmallVectorImpl<ConstantInt *> &OtherCases,
5923 BasicBlock *Dest, BasicBlock *OtherDest) {
5924 assert(Cases.size() >= 1);
5925
5926 array_pod_sort(Start: Cases.begin(), End: Cases.end(), Compare: constantIntSortPredicate);
5927 const APInt &Min = Cases.back()->getValue();
5928 const APInt &Max = Cases.front()->getValue();
5929 APInt Offset = Max - Min;
5930 size_t ContiguousOffset = Cases.size() - 1;
5931 if (Offset == ContiguousOffset) {
5932 return ContiguousCasesResult{
5933 /*Min=*/Cases.back(),
5934 /*Max=*/Cases.front(),
5935 /*Dest=*/Dest,
5936 /*OtherDest=*/OtherDest,
5937 /*Cases=*/&Cases,
5938 /*OtherCases=*/&OtherCases,
5939 };
5940 }
5941 ConstantRange CR = computeConstantRange(V: Condition, /*ForSigned=*/false,
5942 SQ: SimplifyQuery(Dest->getDataLayout()));
5943 // If this is a wrapping contiguous range, that is, [Min, OtherMin] +
5944 // [OtherMax, Max] (also [OtherMax, OtherMin]), [OtherMin+1, OtherMax-1] is a
5945 // contiguous range for the other destination. N.B. If CR is not a full range,
5946 // Max+1 is not equal to Min. It's not continuous in arithmetic.
5947 if (Max == CR.getUnsignedMax() && Min == CR.getUnsignedMin()) {
5948 assert(Cases.size() >= 2);
5949 auto *It =
5950 std::adjacent_find(first: Cases.begin(), last: Cases.end(), binary_pred: [](auto L, auto R) {
5951 return L->getValue() != R->getValue() + 1;
5952 });
5953 if (It == Cases.end())
5954 return std::nullopt;
5955 auto [OtherMax, OtherMin] = std::make_pair(x&: *It, y&: *std::next(x: It));
5956 if ((Max - OtherMax->getValue()) + (OtherMin->getValue() - Min) ==
5957 Cases.size() - 2) {
5958 return ContiguousCasesResult{
5959 /*Min=*/cast<ConstantInt>(
5960 Val: ConstantInt::get(Ty: OtherMin->getType(), V: OtherMin->getValue() + 1)),
5961 /*Max=*/
5962 cast<ConstantInt>(
5963 Val: ConstantInt::get(Ty: OtherMax->getType(), V: OtherMax->getValue() - 1)),
5964 /*Dest=*/OtherDest,
5965 /*OtherDest=*/Dest,
5966 /*Cases=*/&OtherCases,
5967 /*OtherCases=*/&Cases,
5968 };
5969 }
5970 }
5971 return std::nullopt;
5972}
5973
5974static void createUnreachableSwitchDefault(SwitchInst *Switch,
5975 DomTreeUpdater *DTU,
5976 bool RemoveOrigDefaultBlock = true) {
5977 LLVM_DEBUG(dbgs() << "SimplifyCFG: switch default is dead.\n");
5978 auto *BB = Switch->getParent();
5979 auto *OrigDefaultBlock = Switch->getDefaultDest();
5980 if (RemoveOrigDefaultBlock)
5981 OrigDefaultBlock->removePredecessor(Pred: BB);
5982 BasicBlock *NewDefaultBlock = BasicBlock::Create(
5983 Context&: BB->getContext(), Name: BB->getName() + ".unreachabledefault", Parent: BB->getParent(),
5984 InsertBefore: OrigDefaultBlock);
5985 auto *UI = new UnreachableInst(Switch->getContext(), NewDefaultBlock);
5986 UI->setDebugLoc(DebugLoc::getTemporary());
5987 Switch->setDefaultDest(&*NewDefaultBlock);
5988 if (DTU) {
5989 SmallVector<DominatorTree::UpdateType, 2> Updates;
5990 Updates.push_back(Elt: {DominatorTree::Insert, BB, &*NewDefaultBlock});
5991 if (RemoveOrigDefaultBlock &&
5992 !is_contained(Range: successors(BB), Element: OrigDefaultBlock))
5993 Updates.push_back(Elt: {DominatorTree::Delete, BB, &*OrigDefaultBlock});
5994 DTU->applyUpdates(Updates);
5995 }
5996}
5997
5998/// Turn a switch into an integer range comparison and branch.
5999/// Switches with more than 2 destinations are ignored.
6000/// Switches with 1 destination are also ignored.
6001bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
6002 IRBuilder<> &Builder) {
6003 assert(SI->getNumCases() > 1 && "Degenerate switch?");
6004
6005 bool HasDefault = !SI->defaultDestUnreachable();
6006
6007 auto *BB = SI->getParent();
6008 // Partition the cases into two sets with different destinations.
6009 BasicBlock *DestA = HasDefault ? SI->getDefaultDest() : nullptr;
6010 BasicBlock *DestB = nullptr;
6011 SmallVector<ConstantInt *, 16> CasesA;
6012 SmallVector<ConstantInt *, 16> CasesB;
6013
6014 for (auto Case : SI->cases()) {
6015 BasicBlock *Dest = Case.getCaseSuccessor();
6016 if (!DestA)
6017 DestA = Dest;
6018 if (Dest == DestA) {
6019 CasesA.push_back(Elt: Case.getCaseValue());
6020 continue;
6021 }
6022 if (!DestB)
6023 DestB = Dest;
6024 if (Dest == DestB) {
6025 CasesB.push_back(Elt: Case.getCaseValue());
6026 continue;
6027 }
6028 return false; // More than two destinations.
6029 }
6030 if (!DestB)
6031 return false; // All destinations are the same and the default is unreachable
6032
6033 assert(DestA && DestB &&
6034 "Single-destination switch should have been folded.");
6035 assert(DestA != DestB);
6036 assert(DestB != SI->getDefaultDest());
6037 assert(!CasesB.empty() && "There must be non-default cases.");
6038 assert(!CasesA.empty() || HasDefault);
6039
6040 // Figure out if one of the sets of cases form a contiguous range.
6041 std::optional<ContiguousCasesResult> ContiguousCases;
6042
6043 // Only one icmp is needed when there is only one case.
6044 if (!HasDefault && CasesA.size() == 1)
6045 ContiguousCases = ContiguousCasesResult{
6046 /*Min=*/CasesA[0],
6047 /*Max=*/CasesA[0],
6048 /*Dest=*/DestA,
6049 /*OtherDest=*/DestB,
6050 /*Cases=*/&CasesA,
6051 /*OtherCases=*/&CasesB,
6052 };
6053 else if (CasesB.size() == 1)
6054 ContiguousCases = ContiguousCasesResult{
6055 /*Min=*/CasesB[0],
6056 /*Max=*/CasesB[0],
6057 /*Dest=*/DestB,
6058 /*OtherDest=*/DestA,
6059 /*Cases=*/&CasesB,
6060 /*OtherCases=*/&CasesA,
6061 };
6062 // Correctness: Cases to the default destination cannot be contiguous cases.
6063 else if (!HasDefault)
6064 ContiguousCases =
6065 findContiguousCases(Condition: SI->getCondition(), Cases&: CasesA, OtherCases&: CasesB, Dest: DestA, OtherDest: DestB);
6066
6067 if (!ContiguousCases)
6068 ContiguousCases =
6069 findContiguousCases(Condition: SI->getCondition(), Cases&: CasesB, OtherCases&: CasesA, Dest: DestB, OtherDest: DestA);
6070
6071 if (!ContiguousCases)
6072 return false;
6073
6074 auto [Min, Max, Dest, OtherDest, Cases, OtherCases] = *ContiguousCases;
6075
6076 // Start building the compare and branch.
6077
6078 Constant *Offset = ConstantExpr::getNeg(C: Min);
6079 Constant *NumCases = ConstantInt::get(Ty: Offset->getType(),
6080 V: Max->getValue() - Min->getValue() + 1);
6081 Instruction *NewBI;
6082 if (NumCases->isOneValue()) {
6083 assert(Max->getValue() == Min->getValue());
6084 Value *Cmp = Builder.CreateICmpEQ(LHS: SI->getCondition(), RHS: Min);
6085 NewBI = Builder.CreateCondBr(Cond: Cmp, True: Dest, False: OtherDest);
6086 }
6087 // If NumCases overflowed, then all possible values jump to the successor.
6088 else if (NumCases->isNullValue() && !Cases->empty()) {
6089 NewBI = Builder.CreateBr(Dest);
6090 } else {
6091 Value *Sub = SI->getCondition();
6092 if (!Offset->isNullValue())
6093 Sub = Builder.CreateAdd(LHS: Sub, RHS: Offset, Name: Sub->getName() + ".off");
6094 Value *Cmp = Builder.CreateICmpULT(LHS: Sub, RHS: NumCases, Name: "switch");
6095 NewBI = Builder.CreateCondBr(Cond: Cmp, True: Dest, False: OtherDest);
6096 }
6097
6098 // Update weight for the newly-created conditional branch.
6099 if (hasBranchWeightMD(I: *SI) && isa<CondBrInst>(Val: NewBI)) {
6100 SmallVector<uint64_t, 8> Weights;
6101 getBranchWeights(TI: SI, Weights);
6102 if (Weights.size() == 1 + SI->getNumCases()) {
6103 uint64_t TrueWeight = 0;
6104 uint64_t FalseWeight = 0;
6105 for (size_t I = 0, E = Weights.size(); I != E; ++I) {
6106 if (SI->getSuccessor(idx: I) == Dest)
6107 TrueWeight += Weights[I];
6108 else
6109 FalseWeight += Weights[I];
6110 }
6111 while (TrueWeight > UINT32_MAX || FalseWeight > UINT32_MAX) {
6112 TrueWeight /= 2;
6113 FalseWeight /= 2;
6114 }
6115 setFittedBranchWeights(I&: *NewBI, Weights: {TrueWeight, FalseWeight},
6116 /*IsExpected=*/false, /*ElideAllZero=*/true);
6117 }
6118 }
6119
6120 // Prune obsolete incoming values off the successors' PHI nodes.
6121 for (auto &PHI : make_early_inc_range(Range: Dest->phis())) {
6122 unsigned PreviousEdges = Cases->size();
6123 if (Dest == SI->getDefaultDest())
6124 ++PreviousEdges;
6125 for (unsigned I = 0, E = PreviousEdges - 1; I != E; ++I)
6126 PHI.removeIncomingValue(BB: SI->getParent());
6127 }
6128 for (auto &PHI : make_early_inc_range(Range: OtherDest->phis())) {
6129 unsigned PreviousEdges = OtherCases->size();
6130 if (OtherDest == SI->getDefaultDest())
6131 ++PreviousEdges;
6132 unsigned E = PreviousEdges - 1;
6133 // Remove all incoming values from OtherDest if OtherDest is unreachable.
6134 if (isa<UncondBrInst>(Val: NewBI))
6135 ++E;
6136 for (unsigned I = 0; I != E; ++I)
6137 PHI.removeIncomingValue(BB: SI->getParent());
6138 }
6139
6140 // Clean up the default block - it may have phis or other instructions before
6141 // the unreachable terminator.
6142 if (!HasDefault)
6143 createUnreachableSwitchDefault(Switch: SI, DTU);
6144
6145 auto *UnreachableDefault = SI->getDefaultDest();
6146
6147 // Drop the switch.
6148 SI->eraseFromParent();
6149
6150 if (!HasDefault && DTU)
6151 DTU->applyUpdates(Updates: {{DominatorTree::Delete, BB, UnreachableDefault}});
6152
6153 return true;
6154}
6155
6156/// Compute masked bits for the condition of a switch
6157/// and use it to remove dead cases.
6158static bool eliminateDeadSwitchCases(SwitchInst *SI, DomTreeUpdater *DTU,
6159 AssumptionCache *AC,
6160 const DataLayout &DL) {
6161 Value *Cond = SI->getCondition();
6162 KnownBits Known = computeKnownBits(V: Cond, DL, AC, CxtI: SI);
6163 SmallPtrSet<const Constant *, 4> KnownValues;
6164 bool IsKnownValuesValid = collectPossibleValues(V: Cond, Constants&: KnownValues, MaxCount: 4);
6165
6166 // We can also eliminate cases by determining that their values are outside of
6167 // the limited range of the condition based on how many significant (non-sign)
6168 // bits are in the condition value.
6169 unsigned MaxSignificantBitsInCond =
6170 ComputeMaxSignificantBits(Op: Cond, DL, AC, CxtI: SI);
6171
6172 // Gather dead cases.
6173 SmallVector<ConstantInt *, 8> DeadCases;
6174 SmallDenseMap<BasicBlock *, int, 8> NumPerSuccessorCases;
6175 SmallVector<BasicBlock *, 8> UniqueSuccessors;
6176 for (const auto &Case : SI->cases()) {
6177 auto *Successor = Case.getCaseSuccessor();
6178 if (DTU) {
6179 auto [It, Inserted] = NumPerSuccessorCases.try_emplace(Key: Successor);
6180 if (Inserted)
6181 UniqueSuccessors.push_back(Elt: Successor);
6182 ++It->second;
6183 }
6184 ConstantInt *CaseC = Case.getCaseValue();
6185 const APInt &CaseVal = CaseC->getValue();
6186 if (Known.Zero.intersects(RHS: CaseVal) || !Known.One.isSubsetOf(RHS: CaseVal) ||
6187 (CaseVal.getSignificantBits() > MaxSignificantBitsInCond) ||
6188 (IsKnownValuesValid && !KnownValues.contains(Ptr: CaseC))) {
6189 DeadCases.push_back(Elt: CaseC);
6190 if (DTU)
6191 --NumPerSuccessorCases[Successor];
6192 LLVM_DEBUG(dbgs() << "SimplifyCFG: switch case " << CaseVal
6193 << " is dead.\n");
6194 } else if (IsKnownValuesValid)
6195 KnownValues.erase(Ptr: CaseC);
6196 }
6197
6198 // If we can prove that the cases must cover all possible values, the
6199 // default destination becomes dead and we can remove it. If we know some
6200 // of the bits in the value, we can use that to more precisely compute the
6201 // number of possible unique case values.
6202 bool HasDefault = !SI->defaultDestUnreachable();
6203 const unsigned NumUnknownBits =
6204 Known.getBitWidth() - (Known.Zero | Known.One).popcount();
6205 assert(NumUnknownBits <= Known.getBitWidth());
6206 if (HasDefault && DeadCases.empty()) {
6207 if (IsKnownValuesValid && all_of(Range&: KnownValues, P: IsaPred<UndefValue>)) {
6208 createUnreachableSwitchDefault(Switch: SI, DTU);
6209 return true;
6210 }
6211
6212 if (NumUnknownBits < 64 /* avoid overflow */) {
6213 uint64_t AllNumCases = 1ULL << NumUnknownBits;
6214 if (SI->getNumCases() == AllNumCases) {
6215 createUnreachableSwitchDefault(Switch: SI, DTU);
6216 return true;
6217 }
6218 // When only one case value is missing, replace default with that case.
6219 // Eliminating the default branch will provide more opportunities for
6220 // optimization, such as lookup tables.
6221 if (SI->getNumCases() == AllNumCases - 1) {
6222 assert(NumUnknownBits > 1 && "Should be canonicalized to a branch");
6223 IntegerType *CondTy = cast<IntegerType>(Val: Cond->getType());
6224 if (CondTy->getIntegerBitWidth() > 64 ||
6225 !DL.fitsInLegalInteger(Width: CondTy->getIntegerBitWidth()))
6226 return false;
6227
6228 uint64_t MissingCaseVal = 0;
6229 for (const auto &Case : SI->cases())
6230 MissingCaseVal ^= Case.getCaseValue()->getValue().getLimitedValue();
6231 auto *MissingCase = cast<ConstantInt>(
6232 Val: ConstantInt::get(Ty: Cond->getType(), V: MissingCaseVal));
6233 SwitchInstProfUpdateWrapper SIW(*SI);
6234 SIW.addCase(OnVal: MissingCase, Dest: SI->getDefaultDest(),
6235 W: SIW.getSuccessorWeight(idx: 0));
6236 createUnreachableSwitchDefault(Switch: SI, DTU,
6237 /*RemoveOrigDefaultBlock*/ false);
6238 SIW.setSuccessorWeight(idx: 0, W: 0);
6239 return true;
6240 }
6241 }
6242 }
6243
6244 if (DeadCases.empty())
6245 return false;
6246
6247 SwitchInstProfUpdateWrapper SIW(*SI);
6248 for (ConstantInt *DeadCase : DeadCases) {
6249 SwitchInst::CaseIt CaseI = SI->findCaseValue(C: DeadCase);
6250 assert(CaseI != SI->case_default() &&
6251 "Case was not found. Probably mistake in DeadCases forming.");
6252 // Prune unused values from PHI nodes.
6253 CaseI->getCaseSuccessor()->removePredecessor(Pred: SI->getParent());
6254 SIW.removeCase(I: CaseI);
6255 }
6256
6257 if (DTU) {
6258 std::vector<DominatorTree::UpdateType> Updates;
6259 for (auto *Successor : UniqueSuccessors)
6260 if (NumPerSuccessorCases[Successor] == 0)
6261 Updates.push_back(x: {DominatorTree::Delete, SI->getParent(), Successor});
6262 DTU->applyUpdates(Updates);
6263 }
6264
6265 return true;
6266}
6267
6268/// If BB would be eligible for simplification by
6269/// TryToSimplifyUncondBranchFromEmptyBlock (i.e. it is empty and terminated
6270/// by an unconditional branch), look at the phi node for BB in the successor
6271/// block and see if the incoming value is equal to CaseValue. If so, return
6272/// the phi node, and set PhiIndex to BB's index in the phi node.
6273static PHINode *findPHIForConditionForwarding(ConstantInt *CaseValue,
6274 BasicBlock *BB, int *PhiIndex) {
6275 if (&*BB->getFirstNonPHIIt() != BB->getTerminator())
6276 return nullptr; // BB must be empty to be a candidate for simplification.
6277 if (!BB->getSinglePredecessor())
6278 return nullptr; // BB must be dominated by the switch.
6279
6280 UncondBrInst *Branch = dyn_cast<UncondBrInst>(Val: BB->getTerminator());
6281 if (!Branch)
6282 return nullptr; // Terminator must be unconditional branch.
6283
6284 BasicBlock *Succ = Branch->getSuccessor();
6285
6286 for (PHINode &PHI : Succ->phis()) {
6287 int Idx = PHI.getBasicBlockIndex(BB);
6288 assert(Idx >= 0 && "PHI has no entry for predecessor?");
6289
6290 Value *InValue = PHI.getIncomingValue(i: Idx);
6291 if (InValue != CaseValue)
6292 continue;
6293
6294 *PhiIndex = Idx;
6295 return &PHI;
6296 }
6297
6298 return nullptr;
6299}
6300
6301/// Try to forward the condition of a switch instruction to a phi node
6302/// dominated by the switch, if that would mean that some of the destination
6303/// blocks of the switch can be folded away. Return true if a change is made.
6304static bool forwardSwitchConditionToPHI(SwitchInst *SI) {
6305 using ForwardingNodesMap = DenseMap<PHINode *, SmallVector<int, 4>>;
6306
6307 ForwardingNodesMap ForwardingNodes;
6308 BasicBlock *SwitchBlock = SI->getParent();
6309 bool Changed = false;
6310 for (const auto &Case : SI->cases()) {
6311 ConstantInt *CaseValue = Case.getCaseValue();
6312 BasicBlock *CaseDest = Case.getCaseSuccessor();
6313
6314 // Replace phi operands in successor blocks that are using the constant case
6315 // value rather than the switch condition variable:
6316 // switchbb:
6317 // switch i32 %x, label %default [
6318 // i32 17, label %succ
6319 // ...
6320 // succ:
6321 // %r = phi i32 ... [ 17, %switchbb ] ...
6322 // -->
6323 // %r = phi i32 ... [ %x, %switchbb ] ...
6324
6325 for (PHINode &Phi : CaseDest->phis()) {
6326 // This only works if there is exactly 1 incoming edge from the switch to
6327 // a phi. If there is >1, that means multiple cases of the switch map to 1
6328 // value in the phi, and that phi value is not the switch condition. Thus,
6329 // this transform would not make sense (the phi would be invalid because
6330 // a phi can't have different incoming values from the same block).
6331 int SwitchBBIdx = Phi.getBasicBlockIndex(BB: SwitchBlock);
6332 if (Phi.getIncomingValue(i: SwitchBBIdx) == CaseValue &&
6333 count(Range: Phi.blocks(), Element: SwitchBlock) == 1) {
6334 Phi.setIncomingValue(i: SwitchBBIdx, V: SI->getCondition());
6335 Changed = true;
6336 }
6337 }
6338
6339 // Collect phi nodes that are indirectly using this switch's case constants.
6340 int PhiIdx;
6341 if (auto *Phi = findPHIForConditionForwarding(CaseValue, BB: CaseDest, PhiIndex: &PhiIdx))
6342 ForwardingNodes[Phi].push_back(Elt: PhiIdx);
6343 }
6344
6345 for (auto &ForwardingNode : ForwardingNodes) {
6346 PHINode *Phi = ForwardingNode.first;
6347 SmallVectorImpl<int> &Indexes = ForwardingNode.second;
6348 // Check if it helps to fold PHI.
6349 if (Indexes.size() < 2 && !llvm::is_contained(Range: Phi->incoming_values(), Element: SI->getCondition()))
6350 continue;
6351
6352 for (int Index : Indexes)
6353 Phi->setIncomingValue(i: Index, V: SI->getCondition());
6354 Changed = true;
6355 }
6356
6357 return Changed;
6358}
6359
6360/// Return true if the backend will be able to handle
6361/// initializing an array of constants like C.
6362static bool validLookupTableConstant(Constant *C, const TargetTransformInfo &TTI) {
6363 if (C->isThreadDependent())
6364 return false;
6365 if (C->isDLLImportDependent())
6366 return false;
6367
6368 if (!isa<ConstantDataVector, ConstantExpr, ConstantFP, ConstantInt,
6369 ConstantPointerNull, GlobalValue, UndefValue>(Val: C))
6370 return false;
6371
6372 // Globals cannot contain scalable types.
6373 if (C->getType()->isScalableTy())
6374 return false;
6375
6376 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Val: C)) {
6377 // Pointer casts and in-bounds GEPs will not prohibit the backend from
6378 // materializing the array of constants.
6379 Constant *StrippedC = cast<Constant>(Val: CE->stripInBoundsConstantOffsets());
6380 if (StrippedC == C || !validLookupTableConstant(C: StrippedC, TTI))
6381 return false;
6382 }
6383
6384 if (!TTI.shouldBuildLookupTablesForConstant(C))
6385 return false;
6386
6387 return true;
6388}
6389
6390/// If V is a Constant, return it. Otherwise, try to look up
6391/// its constant value in ConstantPool, returning 0 if it's not there.
6392static Constant *
6393lookupConstant(Value *V,
6394 const SmallDenseMap<Value *, Constant *> &ConstantPool) {
6395 if (Constant *C = dyn_cast<Constant>(Val: V))
6396 return C;
6397 return ConstantPool.lookup(Val: V);
6398}
6399
6400/// Try to fold instruction I into a constant. This works for
6401/// simple instructions such as binary operations where both operands are
6402/// constant or can be replaced by constants from the ConstantPool. Returns the
6403/// resulting constant on success, 0 otherwise.
6404static Constant *
6405constantFold(Instruction *I, const DataLayout &DL,
6406 const SmallDenseMap<Value *, Constant *> &ConstantPool) {
6407 if (SelectInst *Select = dyn_cast<SelectInst>(Val: I)) {
6408 Constant *A = lookupConstant(V: Select->getCondition(), ConstantPool);
6409 if (!A)
6410 return nullptr;
6411 if (A->isAllOnesValue())
6412 return lookupConstant(V: Select->getTrueValue(), ConstantPool);
6413 if (A->isNullValue())
6414 return lookupConstant(V: Select->getFalseValue(), ConstantPool);
6415 return nullptr;
6416 }
6417
6418 SmallVector<Constant *, 4> COps;
6419 for (unsigned N = 0, E = I->getNumOperands(); N != E; ++N) {
6420 if (Constant *A = lookupConstant(V: I->getOperand(i: N), ConstantPool))
6421 COps.push_back(Elt: A);
6422 else
6423 return nullptr;
6424 }
6425
6426 return ConstantFoldInstOperands(I, Ops: COps, DL);
6427}
6428
6429/// Try to determine the resulting constant values in phi nodes
6430/// at the common destination basic block, *CommonDest, for one of the case
6431/// destinations CaseDest corresponding to value CaseVal (nullptr for the
6432/// default case), of a switch instruction SI.
6433static bool
6434getCaseResults(SwitchInst *SI, ConstantInt *CaseVal, BasicBlock *CaseDest,
6435 BasicBlock **CommonDest,
6436 SmallVectorImpl<std::pair<PHINode *, Constant *>> &Res,
6437 const DataLayout &DL, const TargetTransformInfo &TTI) {
6438 // The block from which we enter the common destination.
6439 BasicBlock *Pred = SI->getParent();
6440
6441 // If CaseDest is empty except for some side-effect free instructions through
6442 // which we can constant-propagate the CaseVal, continue to its successor.
6443 SmallDenseMap<Value *, Constant *> ConstantPool;
6444 ConstantPool.insert(KV: std::make_pair(x: SI->getCondition(), y&: CaseVal));
6445 for (Instruction &I : *CaseDest) {
6446 if (I.isTerminator()) {
6447 // If the terminator is a simple branch, continue to the next block.
6448 if (I.getNumSuccessors() != 1 || I.isSpecialTerminator())
6449 return false;
6450 Pred = CaseDest;
6451 CaseDest = I.getSuccessor(Idx: 0);
6452 } else if (Constant *C = constantFold(I: &I, DL, ConstantPool)) {
6453 // Instruction is side-effect free and constant.
6454
6455 // If the instruction has uses outside this block or a phi node slot for
6456 // the block, it is not safe to bypass the instruction since it would then
6457 // no longer dominate all its uses.
6458 for (auto &Use : I.uses()) {
6459 User *User = Use.getUser();
6460 if (Instruction *I = dyn_cast<Instruction>(Val: User))
6461 if (I->getParent() == CaseDest)
6462 continue;
6463 if (PHINode *Phi = dyn_cast<PHINode>(Val: User))
6464 if (Phi->getIncomingBlock(U: Use) == CaseDest)
6465 continue;
6466 return false;
6467 }
6468
6469 ConstantPool.insert(KV: std::make_pair(x: &I, y&: C));
6470 } else {
6471 break;
6472 }
6473 }
6474
6475 // If we did not have a CommonDest before, use the current one.
6476 if (!*CommonDest)
6477 *CommonDest = CaseDest;
6478 // If the destination isn't the common one, abort.
6479 if (CaseDest != *CommonDest)
6480 return false;
6481
6482 // Get the values for this case from phi nodes in the destination block.
6483 for (PHINode &PHI : (*CommonDest)->phis()) {
6484 int Idx = PHI.getBasicBlockIndex(BB: Pred);
6485 if (Idx == -1)
6486 continue;
6487
6488 Constant *ConstVal =
6489 lookupConstant(V: PHI.getIncomingValue(i: Idx), ConstantPool);
6490 if (!ConstVal)
6491 return false;
6492
6493 // Be conservative about which kinds of constants we support.
6494 if (!validLookupTableConstant(C: ConstVal, TTI))
6495 return false;
6496
6497 Res.push_back(Elt: std::make_pair(x: &PHI, y&: ConstVal));
6498 }
6499
6500 return Res.size() > 0;
6501}
6502
6503// Helper function used to add CaseVal to the list of cases that generate
6504// Result. Returns the updated number of cases that generate this result.
6505static size_t mapCaseToResult(ConstantInt *CaseVal,
6506 SwitchCaseResultVectorTy &UniqueResults,
6507 Constant *Result) {
6508 for (auto &I : UniqueResults) {
6509 if (I.first == Result) {
6510 I.second.push_back(Elt: CaseVal);
6511 return I.second.size();
6512 }
6513 }
6514 UniqueResults.push_back(
6515 Elt: std::make_pair(x&: Result, y: SmallVector<ConstantInt *, 4>(1, CaseVal)));
6516 return 1;
6517}
6518
6519// Helper function that initializes a map containing
6520// results for the PHI node of the common destination block for a switch
6521// instruction. Returns false if multiple PHI nodes have been found or if
6522// there is not a common destination block for the switch.
6523static bool initializeUniqueCases(SwitchInst *SI, PHINode *&PHI,
6524 BasicBlock *&CommonDest,
6525 SwitchCaseResultVectorTy &UniqueResults,
6526 Constant *&DefaultResult,
6527 const DataLayout &DL,
6528 const TargetTransformInfo &TTI,
6529 uintptr_t MaxUniqueResults) {
6530 for (const auto &I : SI->cases()) {
6531 ConstantInt *CaseVal = I.getCaseValue();
6532
6533 // Resulting value at phi nodes for this case value.
6534 SwitchCaseResultsTy Results;
6535 if (!getCaseResults(SI, CaseVal, CaseDest: I.getCaseSuccessor(), CommonDest: &CommonDest, Res&: Results,
6536 DL, TTI))
6537 return false;
6538
6539 // Only one value per case is permitted.
6540 if (Results.size() > 1)
6541 return false;
6542
6543 // Add the case->result mapping to UniqueResults.
6544 const size_t NumCasesForResult =
6545 mapCaseToResult(CaseVal, UniqueResults, Result: Results.begin()->second);
6546
6547 // Early out if there are too many cases for this result.
6548 if (NumCasesForResult > MaxSwitchCasesPerResult)
6549 return false;
6550
6551 // Early out if there are too many unique results.
6552 if (UniqueResults.size() > MaxUniqueResults)
6553 return false;
6554
6555 // Check the PHI consistency.
6556 if (!PHI)
6557 PHI = Results[0].first;
6558 else if (PHI != Results[0].first)
6559 return false;
6560 }
6561 // Find the default result value.
6562 SmallVector<std::pair<PHINode *, Constant *>, 1> DefaultResults;
6563 getCaseResults(SI, CaseVal: nullptr, CaseDest: SI->getDefaultDest(), CommonDest: &CommonDest, Res&: DefaultResults,
6564 DL, TTI);
6565 // If the default value is not found abort unless the default destination
6566 // is unreachable.
6567 DefaultResult =
6568 DefaultResults.size() == 1 ? DefaultResults.begin()->second : nullptr;
6569
6570 return DefaultResult || SI->defaultDestUnreachable();
6571}
6572
6573// Helper function that checks if it is possible to transform a switch with only
6574// two cases (or two cases + default) that produces a result into a select.
6575// TODO: Handle switches with more than 2 cases that map to the same result.
6576// The branch weights correspond to the provided Condition (i.e. if Condition is
6577// modified from the original SwitchInst, the caller must adjust the weights)
6578static Value *foldSwitchToSelect(const SwitchCaseResultVectorTy &ResultVector,
6579 Constant *DefaultResult, Value *Condition,
6580 IRBuilder<> &Builder, const DataLayout &DL,
6581 ArrayRef<uint32_t> BranchWeights) {
6582 // If we are selecting between only two cases transform into a simple
6583 // select or a two-way select if default is possible.
6584 // Example:
6585 // switch (a) { %0 = icmp eq i32 %a, 10
6586 // case 10: return 42; %1 = select i1 %0, i32 42, i32 4
6587 // case 20: return 2; ----> %2 = icmp eq i32 %a, 20
6588 // default: return 4; %3 = select i1 %2, i32 2, i32 %1
6589 // }
6590
6591 const bool HasBranchWeights =
6592 !BranchWeights.empty() && !ProfcheckDisableMetadataFixes;
6593
6594 if (ResultVector.size() == 2 && ResultVector[0].second.size() == 1 &&
6595 ResultVector[1].second.size() == 1) {
6596 ConstantInt *FirstCase = ResultVector[0].second[0];
6597 ConstantInt *SecondCase = ResultVector[1].second[0];
6598 Value *SelectValue = ResultVector[1].first;
6599 if (DefaultResult) {
6600 Value *ValueCompare =
6601 Builder.CreateICmpEQ(LHS: Condition, RHS: SecondCase, Name: "switch.selectcmp");
6602 SelectValue = Builder.CreateSelect(C: ValueCompare, True: ResultVector[1].first,
6603 False: DefaultResult, Name: "switch.select");
6604 if (auto *SI = dyn_cast<SelectInst>(Val: SelectValue);
6605 SI && HasBranchWeights) {
6606 // We start with 3 probabilities, where the numerator is the
6607 // corresponding BranchWeights[i], and the denominator is the sum over
6608 // BranchWeights. We want the probability and negative probability of
6609 // Condition == SecondCase.
6610 assert(BranchWeights.size() == 3);
6611 setBranchWeights(
6612 I&: *SI, Weights: {BranchWeights[2], BranchWeights[0] + BranchWeights[1]},
6613 /*IsExpected=*/false, /*ElideAllZero=*/true);
6614 }
6615 }
6616 Value *ValueCompare =
6617 Builder.CreateICmpEQ(LHS: Condition, RHS: FirstCase, Name: "switch.selectcmp");
6618 Value *Ret = Builder.CreateSelect(C: ValueCompare, True: ResultVector[0].first,
6619 False: SelectValue, Name: "switch.select");
6620 if (auto *SI = dyn_cast<SelectInst>(Val: Ret); SI && HasBranchWeights) {
6621 // We may have had a DefaultResult. Base the position of the first and
6622 // second's branch weights accordingly. Also the proability that Condition
6623 // != FirstCase needs to take that into account.
6624 assert(BranchWeights.size() >= 2);
6625 size_t FirstCasePos = (Condition != nullptr);
6626 size_t SecondCasePos = FirstCasePos + 1;
6627 uint32_t DefaultCase = (Condition != nullptr) ? BranchWeights[0] : 0;
6628 setBranchWeights(I&: *SI,
6629 Weights: {BranchWeights[FirstCasePos],
6630 DefaultCase + BranchWeights[SecondCasePos]},
6631 /*IsExpected=*/false, /*ElideAllZero=*/true);
6632 }
6633 return Ret;
6634 }
6635
6636 // Handle the degenerate case where two cases have the same result value.
6637 if (ResultVector.size() == 1 && DefaultResult) {
6638 ArrayRef<ConstantInt *> CaseValues = ResultVector[0].second;
6639 unsigned CaseCount = CaseValues.size();
6640 // n bits group cases map to the same result:
6641 // case 0,4 -> Cond & 0b1..1011 == 0 ? result : default
6642 // case 0,2,4,6 -> Cond & 0b1..1001 == 0 ? result : default
6643 // case 0,2,8,10 -> Cond & 0b1..0101 == 0 ? result : default
6644 if (isPowerOf2_32(Value: CaseCount)) {
6645 ConstantInt *MinCaseVal = CaseValues[0];
6646 // If there are bits that are set exclusively by CaseValues, we
6647 // can transform the switch into a select if the conjunction of
6648 // all the values uniquely identify CaseValues.
6649 APInt AndMask = APInt::getAllOnes(numBits: MinCaseVal->getBitWidth());
6650
6651 // Find the minimum value and compute the and of all the case values.
6652 for (auto *Case : CaseValues) {
6653 if (Case->getValue().slt(RHS: MinCaseVal->getValue()))
6654 MinCaseVal = Case;
6655 AndMask &= Case->getValue();
6656 }
6657 KnownBits Known = computeKnownBits(V: Condition, DL);
6658
6659 if (!AndMask.isZero() && Known.getMaxValue().uge(RHS: AndMask)) {
6660 // Compute the number of bits that are free to vary.
6661 unsigned FreeBits = Known.countMaxActiveBits() - AndMask.popcount();
6662
6663 // Check if the number of values covered by the mask is equal
6664 // to the number of cases.
6665 if (FreeBits == Log2_32(Value: CaseCount)) {
6666 Value *And = Builder.CreateAnd(LHS: Condition, RHS: AndMask);
6667 Value *Cmp = Builder.CreateICmpEQ(
6668 LHS: And, RHS: Constant::getIntegerValue(Ty: And->getType(), V: AndMask));
6669 Value *Ret =
6670 Builder.CreateSelect(C: Cmp, True: ResultVector[0].first, False: DefaultResult);
6671 if (auto *SI = dyn_cast<SelectInst>(Val: Ret); SI && HasBranchWeights) {
6672 // We know there's a Default case. We base the resulting branch
6673 // weights off its probability.
6674 assert(BranchWeights.size() >= 2);
6675 setBranchWeights(
6676 I&: *SI,
6677 Weights: {accumulate(Range: drop_begin(RangeOrContainer&: BranchWeights), Init: 0U), BranchWeights[0]},
6678 /*IsExpected=*/false, /*ElideAllZero=*/true);
6679 }
6680 return Ret;
6681 }
6682 }
6683
6684 // Mark the bits case number touched.
6685 APInt BitMask = APInt::getZero(numBits: MinCaseVal->getBitWidth());
6686 for (auto *Case : CaseValues)
6687 BitMask |= (Case->getValue() - MinCaseVal->getValue());
6688
6689 // Check if cases with the same result can cover all number
6690 // in touched bits.
6691 if (BitMask.popcount() == Log2_32(Value: CaseCount)) {
6692 if (!MinCaseVal->isNullValue())
6693 Condition = Builder.CreateSub(LHS: Condition, RHS: MinCaseVal);
6694 Value *And = Builder.CreateAnd(LHS: Condition, RHS: ~BitMask, Name: "switch.and");
6695 Value *Cmp = Builder.CreateICmpEQ(
6696 LHS: And, RHS: Constant::getNullValue(Ty: And->getType()), Name: "switch.selectcmp");
6697 Value *Ret =
6698 Builder.CreateSelect(C: Cmp, True: ResultVector[0].first, False: DefaultResult);
6699 if (auto *SI = dyn_cast<SelectInst>(Val: Ret); SI && HasBranchWeights) {
6700 assert(BranchWeights.size() >= 2);
6701 setBranchWeights(
6702 I&: *SI,
6703 Weights: {accumulate(Range: drop_begin(RangeOrContainer&: BranchWeights), Init: 0U), BranchWeights[0]},
6704 /*IsExpected=*/false, /*ElideAllZero=*/true);
6705 }
6706 return Ret;
6707 }
6708 }
6709
6710 // Handle the degenerate case where two cases have the same value.
6711 if (CaseValues.size() == 2) {
6712 Value *Cmp1 = Builder.CreateICmpEQ(LHS: Condition, RHS: CaseValues[0],
6713 Name: "switch.selectcmp.case1");
6714 Value *Cmp2 = Builder.CreateICmpEQ(LHS: Condition, RHS: CaseValues[1],
6715 Name: "switch.selectcmp.case2");
6716 Value *Cmp = Builder.CreateOr(LHS: Cmp1, RHS: Cmp2, Name: "switch.selectcmp");
6717 Value *Ret =
6718 Builder.CreateSelect(C: Cmp, True: ResultVector[0].first, False: DefaultResult);
6719 if (auto *SI = dyn_cast<SelectInst>(Val: Ret); SI && HasBranchWeights) {
6720 assert(BranchWeights.size() >= 2);
6721 setBranchWeights(
6722 I&: *SI, Weights: {accumulate(Range: drop_begin(RangeOrContainer&: BranchWeights), Init: 0U), BranchWeights[0]},
6723 /*IsExpected=*/false, /*ElideAllZero=*/true);
6724 }
6725 return Ret;
6726 }
6727 }
6728
6729 return nullptr;
6730}
6731
6732// Helper function to cleanup a switch instruction that has been converted into
6733// a select, fixing up PHI nodes and basic blocks.
6734static void removeSwitchAfterSelectFold(SwitchInst *SI, PHINode *PHI,
6735 Value *SelectValue,
6736 IRBuilder<> &Builder,
6737 DomTreeUpdater *DTU) {
6738 std::vector<DominatorTree::UpdateType> Updates;
6739
6740 BasicBlock *SelectBB = SI->getParent();
6741 BasicBlock *DestBB = PHI->getParent();
6742
6743 if (DTU && !is_contained(Range: predecessors(BB: DestBB), Element: SelectBB))
6744 Updates.push_back(x: {DominatorTree::Insert, SelectBB, DestBB});
6745 Builder.CreateBr(Dest: DestBB);
6746
6747 // Remove the switch.
6748
6749 PHI->removeIncomingValueIf(
6750 Predicate: [&](unsigned Idx) { return PHI->getIncomingBlock(i: Idx) == SelectBB; });
6751 PHI->addIncoming(V: SelectValue, BB: SelectBB);
6752
6753 SmallPtrSet<BasicBlock *, 4> RemovedSuccessors;
6754 for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) {
6755 BasicBlock *Succ = SI->getSuccessor(idx: i);
6756
6757 if (Succ == DestBB)
6758 continue;
6759 Succ->removePredecessor(Pred: SelectBB);
6760 if (DTU && RemovedSuccessors.insert(Ptr: Succ).second)
6761 Updates.push_back(x: {DominatorTree::Delete, SelectBB, Succ});
6762 }
6763 SI->eraseFromParent();
6764 if (DTU)
6765 DTU->applyUpdates(Updates);
6766}
6767
6768/// If a switch is only used to initialize one or more phi nodes in a common
6769/// successor block with only two different constant values, try to replace the
6770/// switch with a select. Returns true if the fold was made.
6771static bool trySwitchToSelect(SwitchInst *SI, IRBuilder<> &Builder,
6772 DomTreeUpdater *DTU, const DataLayout &DL,
6773 const TargetTransformInfo &TTI) {
6774 Value *const Cond = SI->getCondition();
6775 PHINode *PHI = nullptr;
6776 BasicBlock *CommonDest = nullptr;
6777 Constant *DefaultResult;
6778 SwitchCaseResultVectorTy UniqueResults;
6779 // Collect all the cases that will deliver the same value from the switch.
6780 if (!initializeUniqueCases(SI, PHI, CommonDest, UniqueResults, DefaultResult,
6781 DL, TTI, /*MaxUniqueResults*/ 2))
6782 return false;
6783
6784 assert(PHI != nullptr && "PHI for value select not found");
6785 Builder.SetInsertPoint(SI);
6786 SmallVector<uint32_t, 4> BranchWeights;
6787 if (!ProfcheckDisableMetadataFixes) {
6788 [[maybe_unused]] auto HasWeights =
6789 extractBranchWeights(ProfileData: getBranchWeightMDNode(I: *SI), Weights&: BranchWeights);
6790 assert(!HasWeights == (BranchWeights.empty()));
6791 }
6792 assert(BranchWeights.empty() ||
6793 (BranchWeights.size() >=
6794 UniqueResults.size() + (DefaultResult != nullptr)));
6795
6796 Value *SelectValue = foldSwitchToSelect(ResultVector: UniqueResults, DefaultResult, Condition: Cond,
6797 Builder, DL, BranchWeights);
6798 if (!SelectValue)
6799 return false;
6800
6801 removeSwitchAfterSelectFold(SI, PHI, SelectValue, Builder, DTU);
6802 return true;
6803}
6804
6805namespace {
6806
6807/// This class finds alternatives for switches to ultimately
6808/// replace the switch.
6809class SwitchReplacement {
6810public:
6811 /// Create a helper for optimizations to use as a switch replacement.
6812 /// Find a better representation for the content of Values,
6813 /// using DefaultValue to fill any holes in the table.
6814 SwitchReplacement(
6815 Module &M, uint64_t TableSize, ConstantInt *Offset,
6816 const SmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values,
6817 Constant *DefaultValue, const DataLayout &DL,
6818 const TargetTransformInfo &TTI, const StringRef &FuncName);
6819
6820 /// Build instructions with Builder to retrieve values using Index
6821 /// and replace the switch.
6822 Value *replaceSwitch(Value *Index, IRBuilder<> &Builder, const DataLayout &DL,
6823 Function *Func);
6824
6825 /// Return true if a table with TableSize elements of
6826 /// type ElementType would fit in a target-legal register.
6827 static bool wouldFitInRegister(const DataLayout &DL, uint64_t TableSize,
6828 Type *ElementType);
6829
6830 /// Return the default value of the switch.
6831 Constant *getDefaultValue();
6832
6833 /// Return true if the replacement is a lookup table.
6834 bool isLookupTable();
6835
6836 /// Return true if the replacement is a bit map.
6837 bool isBitMap();
6838
6839private:
6840 // Depending on the switch, there are different alternatives.
6841 enum {
6842 // For switches where each case contains the same value, we just have to
6843 // store that single value and return it for each lookup.
6844 SingleValueKind,
6845
6846 // For switches where there is a linear relationship between table index
6847 // and values. We calculate the result with a simple multiplication
6848 // and addition instead of a table lookup.
6849 LinearMapKind,
6850
6851 // For small tables with integer elements, we can pack them into a bitmap
6852 // that fits into a target-legal register. Values are retrieved by
6853 // shift and mask operations.
6854 BitMapKind,
6855
6856 // The table is stored as an array of values. Values are retrieved by load
6857 // instructions from the table.
6858 LookupTableKind
6859 } Kind;
6860
6861 // The default value of the switch.
6862 Constant *DefaultValue;
6863
6864 // The type of the output values.
6865 Type *ValueType;
6866
6867 // For SingleValueKind, this is the single value.
6868 Constant *SingleValue = nullptr;
6869
6870 // For BitMapKind, this is the bitmap.
6871 ConstantInt *BitMap = nullptr;
6872 IntegerType *BitMapElementTy = nullptr;
6873
6874 // For LinearMapKind, these are the constants used to derive the value.
6875 ConstantInt *LinearOffset = nullptr;
6876 ConstantInt *LinearMultiplier = nullptr;
6877 bool LinearMapValWrapped = false;
6878
6879 // For LookupTableKind, this is the table.
6880 Constant *Initializer = nullptr;
6881};
6882
6883} // end anonymous namespace
6884
6885SwitchReplacement::SwitchReplacement(
6886 Module &M, uint64_t TableSize, ConstantInt *Offset,
6887 const SmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values,
6888 Constant *DefaultValue, const DataLayout &DL,
6889 const TargetTransformInfo &TTI, const StringRef &FuncName)
6890 : DefaultValue(DefaultValue) {
6891 assert(Values.size() && "Can't build lookup table without values!");
6892 assert(TableSize >= Values.size() && "Can't fit values in table!");
6893
6894 // If all values in the table are equal, this is that value.
6895 SingleValue = Values.begin()->second;
6896
6897 ValueType = Values.begin()->second->getType();
6898
6899 // Build up the table contents.
6900 SmallVector<Constant *, 64> TableContents(TableSize);
6901 for (const auto &[CaseVal, CaseRes] : Values) {
6902 assert(CaseRes->getType() == ValueType);
6903
6904 uint64_t Idx = (CaseVal->getValue() - Offset->getValue()).getLimitedValue();
6905 TableContents[Idx] = CaseRes;
6906
6907 if (SingleValue && !isa<PoisonValue>(Val: CaseRes) && CaseRes != SingleValue)
6908 SingleValue = isa<PoisonValue>(Val: SingleValue) ? CaseRes : nullptr;
6909 }
6910
6911 // Fill in any holes in the table with the default result.
6912 if (Values.size() < TableSize) {
6913 assert(DefaultValue &&
6914 "Need a default value to fill the lookup table holes.");
6915 assert(DefaultValue->getType() == ValueType);
6916 for (uint64_t I = 0; I < TableSize; ++I) {
6917 if (!TableContents[I])
6918 TableContents[I] = DefaultValue;
6919 }
6920
6921 // If the default value is poison, all the holes are poison.
6922 bool DefaultValueIsPoison = isa<PoisonValue>(Val: DefaultValue);
6923
6924 if (DefaultValue != SingleValue && !DefaultValueIsPoison)
6925 SingleValue = nullptr;
6926 }
6927
6928 // If each element in the table contains the same value, we only need to store
6929 // that single value.
6930 if (SingleValue) {
6931 Kind = SingleValueKind;
6932 return;
6933 }
6934
6935 // Check if we can derive the value with a linear transformation from the
6936 // table index.
6937 if (isa<IntegerType>(Val: ValueType)) {
6938 bool LinearMappingPossible = true;
6939 APInt PrevVal;
6940 APInt DistToPrev;
6941 // When linear map is monotonic and signed overflow doesn't happen on
6942 // maximum index, we can attach nsw on Add and Mul.
6943 bool NonMonotonic = false;
6944 assert(TableSize >= 2 && "Should be a SingleValue table.");
6945 // Check if there is the same distance between two consecutive values.
6946 for (uint64_t I = 0; I < TableSize; ++I) {
6947 ConstantInt *ConstVal = dyn_cast<ConstantInt>(Val: TableContents[I]);
6948
6949 if (!ConstVal && isa<PoisonValue>(Val: TableContents[I])) {
6950 // This is an poison, so it's (probably) a lookup table hole.
6951 // To prevent any regressions from before we switched to using poison as
6952 // the default value, holes will fall back to using the first value.
6953 // This can be removed once we add proper handling for poisons in lookup
6954 // tables.
6955 ConstVal = dyn_cast<ConstantInt>(Val: Values[0].second);
6956 }
6957
6958 if (!ConstVal) {
6959 // This is an undef. We could deal with it, but undefs in lookup tables
6960 // are very seldom. It's probably not worth the additional complexity.
6961 LinearMappingPossible = false;
6962 break;
6963 }
6964 const APInt &Val = ConstVal->getValue();
6965 if (I != 0) {
6966 APInt Dist = Val - PrevVal;
6967 if (I == 1) {
6968 DistToPrev = Dist;
6969 } else if (Dist != DistToPrev) {
6970 LinearMappingPossible = false;
6971 break;
6972 }
6973 NonMonotonic |=
6974 Dist.isStrictlyPositive() ? Val.sle(RHS: PrevVal) : Val.sgt(RHS: PrevVal);
6975 }
6976 PrevVal = Val;
6977 }
6978 if (LinearMappingPossible) {
6979 LinearOffset = cast<ConstantInt>(Val: TableContents[0]);
6980 LinearMultiplier = ConstantInt::get(Context&: M.getContext(), V: DistToPrev);
6981 APInt M = LinearMultiplier->getValue();
6982 bool MayWrap = true;
6983 if (isIntN(N: M.getBitWidth(), x: TableSize - 1))
6984 (void)M.smul_ov(RHS: APInt(M.getBitWidth(), TableSize - 1), Overflow&: MayWrap);
6985 LinearMapValWrapped = NonMonotonic || MayWrap;
6986 Kind = LinearMapKind;
6987 return;
6988 }
6989 }
6990
6991 // If the type is integer and the table fits in a register, build a bitmap.
6992 if (wouldFitInRegister(DL, TableSize, ElementType: ValueType)) {
6993 IntegerType *IT = cast<IntegerType>(Val: ValueType);
6994 APInt TableInt(TableSize * IT->getBitWidth(), 0);
6995 for (uint64_t I = TableSize; I > 0; --I) {
6996 TableInt <<= IT->getBitWidth();
6997 // Insert values into the bitmap. Undef values are set to zero.
6998 if (!isa<UndefValue>(Val: TableContents[I - 1])) {
6999 ConstantInt *Val = cast<ConstantInt>(Val: TableContents[I - 1]);
7000 TableInt |= Val->getValue().zext(width: TableInt.getBitWidth());
7001 }
7002 }
7003 BitMap = ConstantInt::get(Context&: M.getContext(), V: TableInt);
7004 BitMapElementTy = IT;
7005 Kind = BitMapKind;
7006 return;
7007 }
7008
7009 if (auto *IT = dyn_cast<IntegerType>(Val: ValueType)) {
7010 ConstantRange Range(IT->getBitWidth(), false);
7011 for (Constant *Value : TableContents)
7012 if (!isa<UndefValue>(Val: Value))
7013 Range = Range.unionWith(CR: cast<ConstantInt>(Val: Value)->getValue());
7014 // TODO: handle sign extension as well?
7015 unsigned NeededBitWidth =
7016 std::max(a: TTI.getMinimumLookupTableEntryBitWidth(),
7017 b: unsigned(PowerOf2Ceil(A: Range.getActiveBits())));
7018 if (NeededBitWidth < IT->getBitWidth()) {
7019 IntegerType *DstTy = IntegerType::get(C&: IT->getContext(), NumBits: NeededBitWidth);
7020 for (Constant *&Value : TableContents)
7021 Value = ConstantFoldCastInstruction(opcode: Instruction::Trunc, V: Value, DestTy: DstTy);
7022 }
7023 }
7024
7025 // Store the table in an array.
7026 auto *TableTy = ArrayType::get(ElementType: TableContents[0]->getType(), NumElements: TableSize);
7027 Initializer = ConstantArray::get(T: TableTy, V: TableContents);
7028
7029 Kind = LookupTableKind;
7030}
7031
7032Value *SwitchReplacement::replaceSwitch(Value *Index, IRBuilder<> &Builder,
7033 const DataLayout &DL, Function *Func) {
7034 switch (Kind) {
7035 case SingleValueKind:
7036 return SingleValue;
7037 case LinearMapKind: {
7038 ++NumLinearMaps;
7039 // Derive the result value from the input value.
7040 Value *Result = Builder.CreateIntCast(V: Index, DestTy: LinearMultiplier->getType(),
7041 isSigned: false, Name: "switch.idx.cast");
7042 if (!LinearMultiplier->isOne())
7043 Result = Builder.CreateMul(LHS: Result, RHS: LinearMultiplier, Name: "switch.idx.mult",
7044 /*HasNUW = */ false,
7045 /*HasNSW = */ !LinearMapValWrapped);
7046
7047 if (!LinearOffset->isZero())
7048 Result = Builder.CreateAdd(LHS: Result, RHS: LinearOffset, Name: "switch.offset",
7049 /*HasNUW = */ false,
7050 /*HasNSW = */ !LinearMapValWrapped);
7051 return Result;
7052 }
7053 case BitMapKind: {
7054 ++NumBitMaps;
7055 // Type of the bitmap (e.g. i59).
7056 IntegerType *MapTy = BitMap->getIntegerType();
7057
7058 // Cast Index to the same type as the bitmap.
7059 // Note: The Index is <= the number of elements in the table, so
7060 // truncating it to the width of the bitmask is safe.
7061 Value *ShiftAmt = Builder.CreateZExtOrTrunc(V: Index, DestTy: MapTy, Name: "switch.cast");
7062
7063 // Multiply the shift amount by the element width. NUW/NSW can always be
7064 // set, because wouldFitInRegister guarantees Index * ShiftAmt is in
7065 // BitMap's bit width.
7066 ShiftAmt = Builder.CreateMul(
7067 LHS: ShiftAmt, RHS: ConstantInt::get(Ty: MapTy, V: BitMapElementTy->getBitWidth()),
7068 Name: "switch.shiftamt",/*HasNUW =*/true,/*HasNSW =*/true);
7069
7070 // Shift down.
7071 Value *DownShifted =
7072 Builder.CreateLShr(LHS: BitMap, RHS: ShiftAmt, Name: "switch.downshift");
7073 // Mask off.
7074 return Builder.CreateTrunc(V: DownShifted, DestTy: BitMapElementTy, Name: "switch.masked");
7075 }
7076 case LookupTableKind: {
7077 ++NumLookupTables;
7078 auto *Table =
7079 new GlobalVariable(*Func->getParent(), Initializer->getType(),
7080 /*isConstant=*/true, GlobalVariable::PrivateLinkage,
7081 Initializer, "switch.table." + Func->getName());
7082 Table->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
7083 // Set the alignment to that of an array items. We will be only loading one
7084 // value out of it.
7085 Table->setAlignment(DL.getPrefTypeAlign(Ty: ValueType));
7086 Type *IndexTy = DL.getIndexType(PtrTy: Table->getType());
7087 auto *ArrayTy = cast<ArrayType>(Val: Table->getValueType());
7088
7089 if (Index->getType() != IndexTy) {
7090 unsigned OldBitWidth = Index->getType()->getIntegerBitWidth();
7091 Index = Builder.CreateZExtOrTrunc(V: Index, DestTy: IndexTy);
7092 if (auto *Zext = dyn_cast<ZExtInst>(Val: Index))
7093 Zext->setNonNeg(
7094 isUIntN(N: OldBitWidth - 1, x: ArrayTy->getNumElements() - 1));
7095 }
7096
7097 Value *GEPIndices[] = {ConstantInt::get(Ty: IndexTy, V: 0), Index};
7098 Value *GEP =
7099 Builder.CreateInBoundsGEP(Ty: ArrayTy, Ptr: Table, IdxList: GEPIndices, Name: "switch.gep");
7100 Value *Load =
7101 Builder.CreateLoad(Ty: ArrayTy->getElementType(), Ptr: GEP, Name: "switch.load");
7102 if (Load->getType() == ValueType)
7103 return Load;
7104 return Builder.CreateZExt(V: Load, DestTy: ValueType, Name: "switch.ext");
7105 }
7106 }
7107 llvm_unreachable("Unknown helper kind!");
7108}
7109
7110bool SwitchReplacement::wouldFitInRegister(const DataLayout &DL,
7111 uint64_t TableSize,
7112 Type *ElementType) {
7113 auto *IT = dyn_cast<IntegerType>(Val: ElementType);
7114 if (!IT)
7115 return false;
7116 // FIXME: If the type is wider than it needs to be, e.g. i8 but all values
7117 // are <= 15, we could try to narrow the type.
7118
7119 // Avoid overflow, fitsInLegalInteger uses unsigned int for the width.
7120 if (TableSize >= UINT_MAX / IT->getBitWidth())
7121 return false;
7122 return DL.fitsInLegalInteger(Width: TableSize * IT->getBitWidth());
7123}
7124
7125static bool isTypeLegalForLookupTable(Type *Ty, const TargetTransformInfo &TTI,
7126 const DataLayout &DL) {
7127 // Allow any legal type.
7128 if (TTI.isTypeLegal(Ty))
7129 return true;
7130
7131 auto *IT = dyn_cast<IntegerType>(Val: Ty);
7132 if (!IT)
7133 return false;
7134
7135 // Also allow power of 2 integer types that have at least 8 bits and fit in
7136 // a register. These types are common in frontend languages and targets
7137 // usually support loads of these types.
7138 // TODO: We could relax this to any integer that fits in a register and rely
7139 // on ABI alignment and padding in the table to allow the load to be widened.
7140 // Or we could widen the constants and truncate the load.
7141 unsigned BitWidth = IT->getBitWidth();
7142 return BitWidth >= 8 && isPowerOf2_32(Value: BitWidth) &&
7143 DL.fitsInLegalInteger(Width: IT->getBitWidth());
7144}
7145
7146Constant *SwitchReplacement::getDefaultValue() { return DefaultValue; }
7147
7148bool SwitchReplacement::isLookupTable() { return Kind == LookupTableKind; }
7149
7150bool SwitchReplacement::isBitMap() { return Kind == BitMapKind; }
7151
7152static bool isSwitchDense(uint64_t NumCases, uint64_t CaseRange, bool OptSize) {
7153 // 40% is the default density for building a jump table in optsize/minsize
7154 // mode, 10% is the default density for jump tables. See also
7155 // TargetLoweringBase::isSuitableForJumpTable(), which this function was based
7156 // on.
7157 const uint64_t MinDensity = OptSize ? 40 : 10;
7158
7159 if (CaseRange >= UINT64_MAX / 100)
7160 return false; // Avoid multiplication overflows below.
7161
7162 return NumCases * 100 >= CaseRange * MinDensity;
7163}
7164
7165static bool isSwitchDense(ArrayRef<int64_t> Values, bool OptSize) {
7166 uint64_t Diff = (uint64_t)Values.back() - (uint64_t)Values.front();
7167 uint64_t Range = Diff + 1;
7168 if (Range < Diff)
7169 return false; // Overflow.
7170
7171 return isSwitchDense(NumCases: Values.size(), CaseRange: Range, OptSize);
7172}
7173
7174static std::optional<unsigned>
7175getDenseSwitchRangeReductionShift(ArrayRef<int64_t> Values, int64_t Base,
7176 bool OptSize) {
7177 assert(Values.size() > 1 && "expected multiple switch cases");
7178 if (!llvm::all_of(Range&: Values, P: [Base](int64_t V) { return V >= Base; }))
7179 return std::nullopt;
7180
7181 // First, transform the values by subtracting Base.
7182 SmallVector<int64_t, 4> ReducedValues(Values);
7183 uint64_t ReducedValuesOr = 0;
7184 for (auto &V : ReducedValues) {
7185 uint64_t Reduced = (uint64_t)V - (uint64_t)Base;
7186 ReducedValuesOr |= Reduced;
7187 V = (int64_t)Reduced;
7188 }
7189
7190 // Conceptually, the reduced values are non-negative distances from Base.
7191 // Since the rest of the transform is bitwise only, treat them as unsigned
7192 // bit patterns from here.
7193
7194 // countr_zero(0) returns 64. As Values is guaranteed to have more than
7195 // one element and LLVM disallows duplicate cases, ReducedValuesOr will
7196 // have at least one bit set, so Shift will be less than 64.
7197 unsigned Shift = llvm::countr_zero(Val: ReducedValuesOr);
7198 assert(Shift < 64);
7199 if (Shift > 0)
7200 for (auto &V : ReducedValues)
7201 V = (int64_t)((uint64_t)V >> Shift);
7202
7203 if (!isSwitchDense(Values: ReducedValues, OptSize))
7204 return std::nullopt;
7205
7206 return Shift;
7207}
7208
7209/// Determine whether a lookup table should be built for this switch, based on
7210/// the number of cases, size of the table, and the types of the results.
7211// TODO: We could support larger than legal types by limiting based on the
7212// number of loads required and/or table size. If the constants are small we
7213// could use smaller table entries and extend after the load.
7214static bool shouldBuildLookupTable(SwitchInst *SI, uint64_t TableSize,
7215 const TargetTransformInfo &TTI,
7216 const DataLayout &DL,
7217 const SmallVector<Type *> &ResultTypes) {
7218 if (SI->getNumCases() > TableSize)
7219 return false; // TableSize overflowed.
7220
7221 bool AllTablesFitInRegister = true;
7222 bool HasIllegalType = false;
7223 for (const auto &Ty : ResultTypes) {
7224 // Saturate this flag to true.
7225 HasIllegalType = HasIllegalType || !isTypeLegalForLookupTable(Ty, TTI, DL);
7226
7227 // Saturate this flag to false.
7228 AllTablesFitInRegister =
7229 AllTablesFitInRegister &&
7230 SwitchReplacement::wouldFitInRegister(DL, TableSize, ElementType: Ty);
7231
7232 // If both flags saturate, we're done. NOTE: This *only* works with
7233 // saturating flags, and all flags have to saturate first due to the
7234 // non-deterministic behavior of iterating over a dense map.
7235 if (HasIllegalType && !AllTablesFitInRegister)
7236 break;
7237 }
7238
7239 // If each table would fit in a register, we should build it anyway.
7240 if (AllTablesFitInRegister)
7241 return true;
7242
7243 // Don't build a table that doesn't fit in-register if it has illegal types.
7244 if (HasIllegalType)
7245 return false;
7246
7247 return isSwitchDense(NumCases: SI->getNumCases(), CaseRange: TableSize,
7248 OptSize: SI->getFunction()->hasOptSize());
7249}
7250
7251static bool shouldUseSwitchConditionAsTableIndex(
7252 ConstantInt &MinCaseVal, const ConstantInt &MaxCaseVal,
7253 bool HasDefaultResults, const SmallVector<Type *> &ResultTypes,
7254 const DataLayout &DL, const TargetTransformInfo &TTI) {
7255 if (MinCaseVal.isNullValue())
7256 return true;
7257 if (MinCaseVal.isNegative() ||
7258 MaxCaseVal.getLimitedValue() == std::numeric_limits<uint64_t>::max() ||
7259 !HasDefaultResults)
7260 return false;
7261 return all_of(Range: ResultTypes, P: [&](const auto &ResultType) {
7262 return SwitchReplacement::wouldFitInRegister(
7263 DL, TableSize: MaxCaseVal.getLimitedValue() + 1 /* TableSize */, ElementType: ResultType);
7264 });
7265}
7266
7267/// Try to reuse the switch table index compare. Following pattern:
7268/// \code
7269/// if (idx < tablesize)
7270/// r = table[idx]; // table does not contain default_value
7271/// else
7272/// r = default_value;
7273/// if (r != default_value)
7274/// ...
7275/// \endcode
7276/// Is optimized to:
7277/// \code
7278/// cond = idx < tablesize;
7279/// if (cond)
7280/// r = table[idx];
7281/// else
7282/// r = default_value;
7283/// if (cond)
7284/// ...
7285/// \endcode
7286/// Jump threading will then eliminate the second if(cond).
7287static void reuseTableCompare(
7288 User *PhiUser, BasicBlock *PhiBlock, CondBrInst *RangeCheckBranch,
7289 Constant *DefaultValue,
7290 const SmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values) {
7291 ICmpInst *CmpInst = dyn_cast<ICmpInst>(Val: PhiUser);
7292 if (!CmpInst)
7293 return;
7294
7295 // We require that the compare is in the same block as the phi so that jump
7296 // threading can do its work afterwards.
7297 if (CmpInst->getParent() != PhiBlock)
7298 return;
7299
7300 Constant *CmpOp1 = dyn_cast<Constant>(Val: CmpInst->getOperand(i_nocapture: 1));
7301 if (!CmpOp1)
7302 return;
7303
7304 Value *RangeCmp = RangeCheckBranch->getCondition();
7305 Constant *TrueConst = ConstantInt::getTrue(Ty: RangeCmp->getType());
7306 Constant *FalseConst = ConstantInt::getFalse(Ty: RangeCmp->getType());
7307
7308 // Check if the compare with the default value is constant true or false.
7309 const DataLayout &DL = PhiBlock->getDataLayout();
7310 Constant *DefaultConst = ConstantFoldCompareInstOperands(
7311 Predicate: CmpInst->getPredicate(), LHS: DefaultValue, RHS: CmpOp1, DL);
7312 if (DefaultConst != TrueConst && DefaultConst != FalseConst)
7313 return;
7314
7315 // Check if the compare with the case values is distinct from the default
7316 // compare result.
7317 for (auto ValuePair : Values) {
7318 Constant *CaseConst = ConstantFoldCompareInstOperands(
7319 Predicate: CmpInst->getPredicate(), LHS: ValuePair.second, RHS: CmpOp1, DL);
7320 if (!CaseConst || CaseConst == DefaultConst ||
7321 (CaseConst != TrueConst && CaseConst != FalseConst))
7322 return;
7323 }
7324
7325 // Check if the branch instruction dominates the phi node. It's a simple
7326 // dominance check, but sufficient for our needs.
7327 // Although this check is invariant in the calling loops, it's better to do it
7328 // at this late stage. Practically we do it at most once for a switch.
7329 BasicBlock *BranchBlock = RangeCheckBranch->getParent();
7330 for (BasicBlock *Pred : predecessors(BB: PhiBlock)) {
7331 if (Pred != BranchBlock && Pred->getUniquePredecessor() != BranchBlock)
7332 return;
7333 }
7334
7335 if (DefaultConst == FalseConst) {
7336 // The compare yields the same result. We can replace it.
7337 CmpInst->replaceAllUsesWith(V: RangeCmp);
7338 ++NumTableCmpReuses;
7339 } else {
7340 // The compare yields the same result, just inverted. We can replace it.
7341 Value *InvertedTableCmp = BinaryOperator::CreateXor(
7342 V1: RangeCmp, V2: ConstantInt::get(Ty: RangeCmp->getType(), V: 1), Name: "inverted.cmp",
7343 InsertBefore: RangeCheckBranch->getIterator());
7344 CmpInst->replaceAllUsesWith(V: InvertedTableCmp);
7345 ++NumTableCmpReuses;
7346 }
7347}
7348
7349/// If the switch is only used to initialize one or more phi nodes in a common
7350/// successor block with different constant values, replace the switch with
7351/// lookup tables.
7352static bool simplifySwitchLookup(SwitchInst *SI, IRBuilder<> &Builder,
7353 DomTreeUpdater *DTU, const DataLayout &DL,
7354 const TargetTransformInfo &TTI,
7355 bool ConvertSwitchToLookupTable) {
7356 assert(SI->getNumCases() > 1 && "Degenerate switch?");
7357
7358 BasicBlock *BB = SI->getParent();
7359 Function *Fn = BB->getParent();
7360
7361 // FIXME: If the switch is too sparse for a lookup table, perhaps we could
7362 // split off a dense part and build a lookup table for that.
7363
7364 // FIXME: This creates arrays of GEPs to constant strings, which means each
7365 // GEP needs a runtime relocation in PIC code. We should just build one big
7366 // string and lookup indices into that.
7367
7368 // Ignore switches with less than three cases. Lookup tables will not make
7369 // them faster, so we don't analyze them.
7370 if (SI->getNumCases() < 3)
7371 return false;
7372
7373 // Figure out the corresponding result for each case value and phi node in the
7374 // common destination, as well as the min and max case values.
7375 assert(!SI->cases().empty());
7376 SwitchInst::CaseIt CI = SI->case_begin();
7377 ConstantInt *MinCaseVal = CI->getCaseValue();
7378 ConstantInt *MaxCaseVal = CI->getCaseValue();
7379
7380 BasicBlock *CommonDest = nullptr;
7381
7382 using ResultListTy = SmallVector<std::pair<ConstantInt *, Constant *>, 4>;
7383 SmallDenseMap<PHINode *, ResultListTy> ResultLists;
7384
7385 SmallDenseMap<PHINode *, Constant *> DefaultResults;
7386 SmallVector<Type *> ResultTypes;
7387 SmallVector<PHINode *, 4> PHIs;
7388
7389 for (SwitchInst::CaseIt E = SI->case_end(); CI != E; ++CI) {
7390 ConstantInt *CaseVal = CI->getCaseValue();
7391 if (CaseVal->getValue().slt(RHS: MinCaseVal->getValue()))
7392 MinCaseVal = CaseVal;
7393 if (CaseVal->getValue().sgt(RHS: MaxCaseVal->getValue()))
7394 MaxCaseVal = CaseVal;
7395
7396 // Resulting value at phi nodes for this case value.
7397 using ResultsTy = SmallVector<std::pair<PHINode *, Constant *>, 4>;
7398 ResultsTy Results;
7399 if (!getCaseResults(SI, CaseVal, CaseDest: CI->getCaseSuccessor(), CommonDest: &CommonDest,
7400 Res&: Results, DL, TTI))
7401 return false;
7402
7403 // Append the result and result types from this case to the list for each
7404 // phi.
7405 for (const auto &I : Results) {
7406 PHINode *PHI = I.first;
7407 Constant *Value = I.second;
7408 auto [It, Inserted] = ResultLists.try_emplace(Key: PHI);
7409 if (Inserted)
7410 PHIs.push_back(Elt: PHI);
7411 It->second.push_back(Elt: std::make_pair(x&: CaseVal, y&: Value));
7412 ResultTypes.push_back(Elt: PHI->getType());
7413 }
7414 }
7415
7416 // If the table has holes, we need a constant result for the default case
7417 // or a bitmask that fits in a register.
7418 SmallVector<std::pair<PHINode *, Constant *>, 4> DefaultResultsList;
7419 bool HasDefaultResults =
7420 getCaseResults(SI, CaseVal: nullptr, CaseDest: SI->getDefaultDest(), CommonDest: &CommonDest,
7421 Res&: DefaultResultsList, DL, TTI);
7422 for (const auto &I : DefaultResultsList) {
7423 PHINode *PHI = I.first;
7424 Constant *Result = I.second;
7425 DefaultResults[PHI] = Result;
7426 }
7427
7428 bool UseSwitchConditionAsTableIndex = shouldUseSwitchConditionAsTableIndex(
7429 MinCaseVal&: *MinCaseVal, MaxCaseVal: *MaxCaseVal, HasDefaultResults, ResultTypes, DL, TTI);
7430 uint64_t TableSize;
7431 ConstantInt *TableIndexOffset;
7432 if (UseSwitchConditionAsTableIndex) {
7433 TableSize = MaxCaseVal->getLimitedValue() + 1;
7434 TableIndexOffset = ConstantInt::get(Ty: MaxCaseVal->getIntegerType(), V: 0);
7435 } else {
7436 TableSize =
7437 (MaxCaseVal->getValue() - MinCaseVal->getValue()).getLimitedValue() + 1;
7438
7439 TableIndexOffset = MinCaseVal;
7440 }
7441
7442 // If the default destination is unreachable, or if the lookup table covers
7443 // all values of the conditional variable, branch directly to the lookup table
7444 // BB. Otherwise, check that the condition is within the case range.
7445 uint64_t NumResults = ResultLists[PHIs[0]].size();
7446 bool DefaultIsReachable = !SI->defaultDestUnreachable();
7447
7448 bool TableHasHoles = (NumResults < TableSize);
7449
7450 // If the table has holes but the default destination doesn't produce any
7451 // constant results, the lookup table entries corresponding to the holes will
7452 // contain poison.
7453 bool AllHolesArePoison = TableHasHoles && !HasDefaultResults;
7454
7455 // If the default destination doesn't produce a constant result but is still
7456 // reachable, and the lookup table has holes, we need to use a mask to
7457 // determine if the current index should load from the lookup table or jump
7458 // to the default case.
7459 // The mask is unnecessary if the table has holes but the default destination
7460 // is unreachable, as in that case the holes must also be unreachable.
7461 bool NeedMask = AllHolesArePoison && DefaultIsReachable;
7462 if (NeedMask) {
7463 // As an extra penalty for the validity test we require more cases.
7464 if (SI->getNumCases() < 4) // FIXME: Find best threshold value (benchmark).
7465 return false;
7466 if (!DL.fitsInLegalInteger(Width: TableSize))
7467 return false;
7468 }
7469
7470 if (!shouldBuildLookupTable(SI, TableSize, TTI, DL, ResultTypes))
7471 return false;
7472
7473 // Compute the table index value.
7474 Value *TableIndex;
7475 if (UseSwitchConditionAsTableIndex) {
7476 TableIndex = SI->getCondition();
7477 if (HasDefaultResults) {
7478 // Grow the table to cover all possible index values to avoid the range
7479 // check. It will use the default result to fill in the table hole later,
7480 // so make sure it exist.
7481 ConstantRange CR = computeConstantRange(V: TableIndex, /*ForSigned=*/false,
7482 SQ: SimplifyQuery(DL));
7483 // Grow the table shouldn't have any size impact by checking
7484 // wouldFitInRegister.
7485 // TODO: Consider growing the table also when it doesn't fit in a register
7486 // if no optsize is specified.
7487 const uint64_t UpperBound = CR.getUpper().getLimitedValue();
7488 if (!CR.isUpperWrapped() &&
7489 all_of(Range&: ResultTypes, P: [&](const auto &ResultType) {
7490 return SwitchReplacement::wouldFitInRegister(DL, TableSize: UpperBound,
7491 ElementType: ResultType);
7492 })) {
7493 // There may be some case index larger than the UpperBound (unreachable
7494 // case), so make sure the table size does not get smaller.
7495 TableSize = std::max(a: UpperBound, b: TableSize);
7496 // The default branch is unreachable after we enlarge the lookup table.
7497 // Adjust DefaultIsReachable to reuse code path.
7498 DefaultIsReachable = false;
7499 }
7500 }
7501 }
7502
7503 // Keep track of the switch replacement for each phi
7504 SmallDenseMap<PHINode *, SwitchReplacement> PhiToReplacementMap;
7505 for (PHINode *PHI : PHIs) {
7506 const auto &ResultList = ResultLists[PHI];
7507
7508 Type *ResultType = ResultList.begin()->second->getType();
7509 // Use any value to fill the lookup table holes.
7510 Constant *DefaultVal =
7511 AllHolesArePoison ? PoisonValue::get(T: ResultType) : DefaultResults[PHI];
7512 StringRef FuncName = Fn->getName();
7513 SwitchReplacement Replacement(*Fn->getParent(), TableSize, TableIndexOffset,
7514 ResultList, DefaultVal, DL, TTI, FuncName);
7515 PhiToReplacementMap.insert(KV: {PHI, Replacement});
7516 }
7517
7518 bool AnyLookupTables = any_of(
7519 Range&: PhiToReplacementMap, P: [](auto &KV) { return KV.second.isLookupTable(); });
7520 bool AnyBitMaps = any_of(Range&: PhiToReplacementMap,
7521 P: [](auto &KV) { return KV.second.isBitMap(); });
7522
7523 // A few conditions prevent the generation of lookup tables:
7524 // 1. The target does not support lookup tables.
7525 // 2. The "no-jump-tables" function attribute is set.
7526 // However, these objections do not apply to other switch replacements, like
7527 // the bitmap, so we only stop here if any of these conditions are met and we
7528 // want to create a LUT. Otherwise, continue with the switch replacement.
7529 if (AnyLookupTables &&
7530 (!TTI.shouldBuildLookupTables() ||
7531 Fn->getFnAttribute(Kind: "no-jump-tables").getValueAsBool()))
7532 return false;
7533
7534 // In the early optimization pipeline, disable formation of lookup tables,
7535 // bit maps and mask checks, as they may inhibit further optimization.
7536 if (!ConvertSwitchToLookupTable &&
7537 (AnyLookupTables || AnyBitMaps || NeedMask))
7538 return false;
7539
7540 Builder.SetInsertPoint(SI);
7541 // TableIndex is the switch condition - TableIndexOffset if we don't
7542 // use the condition directly
7543 if (!UseSwitchConditionAsTableIndex) {
7544 // If the default is unreachable, all case values are s>= MinCaseVal. Then
7545 // we can try to attach nsw.
7546 bool MayWrap = true;
7547 if (!DefaultIsReachable) {
7548 APInt Res =
7549 MaxCaseVal->getValue().ssub_ov(RHS: MinCaseVal->getValue(), Overflow&: MayWrap);
7550 (void)Res;
7551 }
7552 TableIndex = Builder.CreateSub(LHS: SI->getCondition(), RHS: TableIndexOffset,
7553 Name: "switch.tableidx", /*HasNUW =*/false,
7554 /*HasNSW =*/!MayWrap);
7555 }
7556
7557 std::vector<DominatorTree::UpdateType> Updates;
7558
7559 // Compute the maximum table size representable by the integer type we are
7560 // switching upon.
7561 unsigned CaseSize = MinCaseVal->getType()->getPrimitiveSizeInBits();
7562 uint64_t MaxTableSize = CaseSize > 63 ? UINT64_MAX : 1ULL << CaseSize;
7563 assert(MaxTableSize >= TableSize &&
7564 "It is impossible for a switch to have more entries than the max "
7565 "representable value of its input integer type's size.");
7566
7567 // Create the BB that does the lookups.
7568 Module &Mod = *CommonDest->getParent()->getParent();
7569 BasicBlock *LookupBB = BasicBlock::Create(
7570 Context&: Mod.getContext(), Name: "switch.lookup", Parent: CommonDest->getParent(), InsertBefore: CommonDest);
7571
7572 CondBrInst *RangeCheckBranch = nullptr;
7573 CondBrInst *CondBranch = nullptr;
7574
7575 Builder.SetInsertPoint(SI);
7576 const bool GeneratingCoveredLookupTable = (MaxTableSize == TableSize);
7577 if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
7578 Builder.CreateBr(Dest: LookupBB);
7579 if (DTU)
7580 Updates.push_back(x: {DominatorTree::Insert, BB, LookupBB});
7581 // Note: We call removeProdecessor later since we need to be able to get the
7582 // PHI value for the default case in case we're using a bit mask.
7583 } else {
7584 Value *Cmp = Builder.CreateICmpULT(
7585 LHS: TableIndex, RHS: ConstantInt::get(Ty: MinCaseVal->getType(), V: TableSize));
7586 RangeCheckBranch =
7587 Builder.CreateCondBr(Cond: Cmp, True: LookupBB, False: SI->getDefaultDest());
7588 CondBranch = RangeCheckBranch;
7589 if (DTU)
7590 Updates.push_back(x: {DominatorTree::Insert, BB, LookupBB});
7591 }
7592
7593 // Populate the BB that does the lookups.
7594 Builder.SetInsertPoint(LookupBB);
7595
7596 if (NeedMask) {
7597 // Before doing the lookup, we do the hole check. The LookupBB is therefore
7598 // re-purposed to do the hole check, and we create a new LookupBB.
7599 BasicBlock *MaskBB = LookupBB;
7600 MaskBB->setName("switch.hole_check");
7601 LookupBB = BasicBlock::Create(Context&: Mod.getContext(), Name: "switch.lookup",
7602 Parent: CommonDest->getParent(), InsertBefore: CommonDest);
7603
7604 // Make the mask's bitwidth at least 8-bit and a power-of-2 to avoid
7605 // unnecessary illegal types.
7606 uint64_t TableSizePowOf2 = NextPowerOf2(A: std::max(a: 7ULL, b: TableSize - 1ULL));
7607 APInt MaskInt(TableSizePowOf2, 0);
7608 APInt One(TableSizePowOf2, 1);
7609 // Build bitmask; fill in a 1 bit for every case.
7610 const ResultListTy &ResultList = ResultLists[PHIs[0]];
7611 for (const auto &Result : ResultList) {
7612 uint64_t Idx = (Result.first->getValue() - TableIndexOffset->getValue())
7613 .getLimitedValue();
7614 MaskInt |= One << Idx;
7615 }
7616 ConstantInt *TableMask = ConstantInt::get(Context&: Mod.getContext(), V: MaskInt);
7617
7618 // Get the TableIndex'th bit of the bitmask.
7619 // If this bit is 0 (meaning hole) jump to the default destination,
7620 // else continue with table lookup.
7621 IntegerType *MapTy = TableMask->getIntegerType();
7622 Value *MaskIndex =
7623 Builder.CreateZExtOrTrunc(V: TableIndex, DestTy: MapTy, Name: "switch.maskindex");
7624 Value *Shifted = Builder.CreateLShr(LHS: TableMask, RHS: MaskIndex, Name: "switch.shifted");
7625 Value *LoBit = Builder.CreateTrunc(
7626 V: Shifted, DestTy: Type::getInt1Ty(C&: Mod.getContext()), Name: "switch.lobit");
7627 CondBranch = Builder.CreateCondBr(Cond: LoBit, True: LookupBB, False: SI->getDefaultDest());
7628 if (DTU) {
7629 Updates.push_back(x: {DominatorTree::Insert, MaskBB, LookupBB});
7630 Updates.push_back(x: {DominatorTree::Insert, MaskBB, SI->getDefaultDest()});
7631 }
7632 Builder.SetInsertPoint(LookupBB);
7633 addPredecessorToBlock(Succ: SI->getDefaultDest(), NewPred: MaskBB, ExistPred: BB);
7634 }
7635
7636 if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
7637 // We cached PHINodes in PHIs. To avoid accessing deleted PHINodes later,
7638 // do not delete PHINodes here.
7639 SI->getDefaultDest()->removePredecessor(Pred: BB,
7640 /*KeepOneInputPHIs=*/true);
7641 if (DTU)
7642 Updates.push_back(x: {DominatorTree::Delete, BB, SI->getDefaultDest()});
7643 }
7644
7645 for (PHINode *PHI : PHIs) {
7646 const ResultListTy &ResultList = ResultLists[PHI];
7647 auto Replacement = PhiToReplacementMap.at(Val: PHI);
7648 auto *Result = Replacement.replaceSwitch(Index: TableIndex, Builder, DL, Func: Fn);
7649 // Do a small peephole optimization: re-use the switch table compare if
7650 // possible.
7651 if (!TableHasHoles && HasDefaultResults && RangeCheckBranch) {
7652 BasicBlock *PhiBlock = PHI->getParent();
7653 // Search for compare instructions which use the phi.
7654 for (auto *User : PHI->users()) {
7655 reuseTableCompare(PhiUser: User, PhiBlock, RangeCheckBranch,
7656 DefaultValue: Replacement.getDefaultValue(), Values: ResultList);
7657 }
7658 }
7659
7660 PHI->addIncoming(V: Result, BB: LookupBB);
7661 }
7662
7663 Builder.CreateBr(Dest: CommonDest);
7664 if (DTU)
7665 Updates.push_back(x: {DominatorTree::Insert, LookupBB, CommonDest});
7666
7667 SmallVector<uint32_t> BranchWeights;
7668 const bool HasBranchWeights = CondBranch && !ProfcheckDisableMetadataFixes &&
7669 extractBranchWeights(I: *SI, Weights&: BranchWeights);
7670 uint64_t ToLookupWeight = 0;
7671 uint64_t ToDefaultWeight = 0;
7672
7673 // Remove the switch.
7674 SmallPtrSet<BasicBlock *, 8> RemovedSuccessors;
7675 for (unsigned I = 0, E = SI->getNumSuccessors(); I < E; ++I) {
7676 BasicBlock *Succ = SI->getSuccessor(idx: I);
7677
7678 if (Succ == SI->getDefaultDest()) {
7679 if (HasBranchWeights)
7680 ToDefaultWeight += BranchWeights[I];
7681 continue;
7682 }
7683 Succ->removePredecessor(Pred: BB);
7684 if (DTU && RemovedSuccessors.insert(Ptr: Succ).second)
7685 Updates.push_back(x: {DominatorTree::Delete, BB, Succ});
7686 if (HasBranchWeights)
7687 ToLookupWeight += BranchWeights[I];
7688 }
7689 SI->eraseFromParent();
7690 if (HasBranchWeights)
7691 setFittedBranchWeights(I&: *CondBranch, Weights: {ToLookupWeight, ToDefaultWeight},
7692 /*IsExpected=*/false);
7693 if (DTU)
7694 DTU->applyUpdates(Updates);
7695
7696 if (NeedMask)
7697 ++NumLookupTablesHoles;
7698 return true;
7699}
7700
7701/// Try to transform a switch that has "holes" in it to a contiguous sequence
7702/// of cases.
7703///
7704/// A switch such as: switch(i) {case 5: case 9: case 13: case 17:} can be
7705/// range-reduced to: switch ((i-5) / 4) {case 0: case 1: case 2: case 3:}.
7706///
7707/// This converts a sparse switch into a dense switch which allows better
7708/// lowering and could also allow transforming into a lookup table.
7709static bool reduceSwitchRange(SwitchInst *SI, IRBuilder<> &Builder,
7710 const DataLayout &DL,
7711 const TargetTransformInfo &TTI) {
7712 auto *CondTy = cast<IntegerType>(Val: SI->getCondition()->getType());
7713 if (CondTy->getIntegerBitWidth() > 64 ||
7714 !DL.fitsInLegalInteger(Width: CondTy->getIntegerBitWidth()))
7715 return false;
7716 // Only bother with this optimization if there are more than 3 switch cases;
7717 // SDAG will only bother creating jump tables for 4 or more cases.
7718 if (SI->getNumCases() < 4)
7719 return false;
7720
7721 // This transform is agnostic to the signedness of the input or case values. We
7722 // can treat the case values as signed or unsigned. We can optimize more common
7723 // cases such as a sequence crossing zero {-4,0,4,8} if we interpret case values
7724 // as signed.
7725 SmallVector<int64_t,4> Values;
7726 for (const auto &C : SI->cases())
7727 Values.push_back(Elt: C.getCaseValue()->getValue().getSExtValue());
7728 llvm::sort(C&: Values);
7729
7730 // If the switch is already dense, there's nothing useful to do here.
7731 bool OptSize = SI->getFunction()->hasOptSize();
7732 if (isSwitchDense(Values, OptSize))
7733 return false;
7734
7735 // Find a Base and corresponding Shift that results in a dense switch range.
7736 // Values[0] is the local minimum.
7737 int64_t Base = Values[0];
7738 std::optional<unsigned> Shift;
7739 // Prefer Base=0 when shifting out common low zero bits still produces a dense
7740 // range, as this avoids an unnecessary `(condition - local_min)` expression.
7741 // However, avoiding the subtract can leave a wider reduced range than using
7742 // the local minimum, so require Base=0 to satisfy the stricter optsize
7743 // density threshold before falling back to the normal density policy for
7744 // local-min.
7745 if ((Shift = getDenseSwitchRangeReductionShift(Values, /*Base=*/0,
7746 /*OptSize=*/true)))
7747 Base = 0;
7748 else if (Base != 0)
7749 Shift = getDenseSwitchRangeReductionShift(Values, Base, OptSize);
7750
7751 if (!Shift)
7752 return false;
7753
7754 // The obvious transform is to shift the switch condition right and emit a
7755 // check that the condition actually cleanly divided by GCD, i.e.
7756 // C & (1 << Shift - 1) == 0
7757 // inserting a new CFG edge to handle the case where it didn't divide cleanly.
7758 //
7759 // A cheaper way of doing this is a simple ROTR(C, Shift). This performs the
7760 // shift and puts the shifted-off bits in the uppermost bits. If any of these
7761 // are nonzero then the switch condition will be very large and will hit the
7762 // default case.
7763 //
7764 // This transform can be done speculatively because it is so cheap - it
7765 // results in a single rotate operation being inserted.
7766
7767 auto *Ty = cast<IntegerType>(Val: SI->getCondition()->getType());
7768 Builder.SetInsertPoint(SI);
7769 Value *Sub = SI->getCondition();
7770 if (Base != 0)
7771 Sub = Builder.CreateSub(LHS: Sub, RHS: ConstantInt::getSigned(Ty, V: Base));
7772 Value *Rot = Builder.CreateIntrinsic(
7773 RetTy: Ty, ID: Intrinsic::fshl,
7774 Args: {Sub, Sub, ConstantInt::get(Ty, V: Ty->getBitWidth() - *Shift)});
7775 SI->replaceUsesOfWith(From: SI->getCondition(), To: Rot);
7776
7777 for (auto Case : SI->cases()) {
7778 auto *Orig = Case.getCaseValue();
7779 auto Sub = Orig->getValue() - APInt(Ty->getBitWidth(), Base, true);
7780 Case.setValue(cast<ConstantInt>(Val: ConstantInt::get(Ty, V: Sub.lshr(shiftAmt: *Shift))));
7781 }
7782 return true;
7783}
7784
7785/// Tries to transform the switch when the condition is umin with a constant.
7786/// In that case, the default branch can be replaced by the constant's branch.
7787/// This method also removes dead cases when the simplification cannot replace
7788/// the default branch.
7789///
7790/// For example:
7791/// switch(umin(a, 3)) {
7792/// case 0:
7793/// case 1:
7794/// case 2:
7795/// case 3:
7796/// case 4:
7797/// // ...
7798/// default:
7799/// unreachable
7800/// }
7801///
7802/// Transforms into:
7803///
7804/// switch(a) {
7805/// case 0:
7806/// case 1:
7807/// case 2:
7808/// default:
7809/// // This is case 3
7810/// }
7811static bool simplifySwitchWhenUMin(SwitchInst *SI, DomTreeUpdater *DTU) {
7812 Value *A;
7813 ConstantInt *Constant;
7814
7815 if (!match(V: SI->getCondition(), P: m_UMin(Op0: m_Value(V&: A), Op1: m_ConstantInt(CI&: Constant))))
7816 return false;
7817
7818 SmallVector<DominatorTree::UpdateType> Updates;
7819 SwitchInstProfUpdateWrapper SIW(*SI);
7820 BasicBlock *BB = SIW->getParent();
7821
7822 // Dead cases are removed even when the simplification fails.
7823 // A case is dead when its value is higher than the Constant.
7824 for (auto I = SI->case_begin(), E = SI->case_end(); I != E;) {
7825 if (!I->getCaseValue()->getValue().ugt(RHS: Constant->getValue())) {
7826 ++I;
7827 continue;
7828 }
7829 BasicBlock *DeadCaseBB = I->getCaseSuccessor();
7830 DeadCaseBB->removePredecessor(Pred: BB);
7831 Updates.push_back(Elt: {DominatorTree::Delete, BB, DeadCaseBB});
7832 I = SIW.removeCase(I);
7833 E = SIW->case_end();
7834 }
7835
7836 auto Case = SI->findCaseValue(C: Constant);
7837 // If the case value is not found, `findCaseValue` returns the default case.
7838 // In this scenario, since there is no explicit `case 3:`, the simplification
7839 // fails. The simplification also fails when the switch’s default destination
7840 // is reachable.
7841 if (!SI->defaultDestUnreachable() || Case == SI->case_default()) {
7842 if (DTU)
7843 DTU->applyUpdates(Updates);
7844 return !Updates.empty();
7845 }
7846
7847 BasicBlock *Unreachable = SI->getDefaultDest();
7848 SIW.replaceDefaultDest(I: Case);
7849 SIW.removeCase(I: Case);
7850 SIW->setCondition(A);
7851
7852 Updates.push_back(Elt: {DominatorTree::Delete, BB, Unreachable});
7853
7854 if (DTU)
7855 DTU->applyUpdates(Updates);
7856
7857 return true;
7858}
7859
7860/// Tries to transform switch of powers of two to reduce switch range.
7861/// For example, switch like:
7862/// switch (C) { case 1: case 2: case 64: case 128: }
7863/// will be transformed to:
7864/// switch (count_trailing_zeros(C)) { case 0: case 1: case 6: case 7: }
7865///
7866/// This transformation allows better lowering and may transform the switch
7867/// instruction into a sequence of bit manipulation and a smaller
7868/// log2(C)-indexed value table (instead of traditionally emitting a load of the
7869/// address of the jump target, and indirectly jump to it).
7870static bool simplifySwitchOfPowersOfTwo(SwitchInst *SI, IRBuilder<> &Builder,
7871 DomTreeUpdater *DTU,
7872 const DataLayout &DL,
7873 const TargetTransformInfo &TTI) {
7874 Value *Condition = SI->getCondition();
7875 LLVMContext &Context = SI->getContext();
7876 auto *CondTy = cast<IntegerType>(Val: Condition->getType());
7877
7878 if (CondTy->getIntegerBitWidth() > 64 ||
7879 !DL.fitsInLegalInteger(Width: CondTy->getIntegerBitWidth()))
7880 return false;
7881
7882 // Ensure trailing zeroes count intrinsic emission is not too expensive.
7883 IntrinsicCostAttributes Attrs(Intrinsic::cttz, CondTy,
7884 {Condition, ConstantInt::getTrue(Context)});
7885 if (TTI.getIntrinsicInstrCost(ICA: Attrs, CostKind: TTI::TCK_SizeAndLatency) >
7886 TTI::TCC_Basic * 2)
7887 return false;
7888
7889 // Only bother with this optimization if there are more than 3 switch cases.
7890 // SDAG will start emitting jump tables for 4 or more cases.
7891 if (SI->getNumCases() < 4)
7892 return false;
7893
7894 // Check that switch cases are powers of two.
7895 SmallVector<uint64_t, 4> Values;
7896 for (const auto &Case : SI->cases()) {
7897 uint64_t CaseValue = Case.getCaseValue()->getValue().getZExtValue();
7898 if (llvm::has_single_bit(Value: CaseValue))
7899 Values.push_back(Elt: CaseValue);
7900 else
7901 return false;
7902 }
7903
7904 // isSwichDense requires case values to be sorted.
7905 llvm::sort(C&: Values);
7906 if (!isSwitchDense(NumCases: Values.size(),
7907 CaseRange: llvm::countr_zero(Val: Values.back()) -
7908 llvm::countr_zero(Val: Values.front()) + 1,
7909 OptSize: SI->getFunction()->hasOptSize()))
7910 // Transform is unable to generate dense switch.
7911 return false;
7912
7913 Builder.SetInsertPoint(SI);
7914
7915 if (!SI->defaultDestUnreachable()) {
7916 // Let non-power-of-two inputs jump to the default case, when the latter is
7917 // reachable.
7918 auto *PopC = Builder.CreateUnaryIntrinsic(ID: Intrinsic::ctpop, Op: Condition);
7919 auto *IsPow2 = Builder.CreateICmpEQ(LHS: PopC, RHS: ConstantInt::get(Ty: CondTy, V: 1));
7920
7921 auto *OrigBB = SI->getParent();
7922 auto *DefaultCaseBB = SI->getDefaultDest();
7923 BasicBlock *SplitBB = SplitBlock(Old: OrigBB, SplitPt: SI, DTU);
7924 auto It = OrigBB->getTerminator()->getIterator();
7925 SmallVector<uint32_t> Weights;
7926 auto HasWeights =
7927 !ProfcheckDisableMetadataFixes && extractBranchWeights(I: *SI, Weights);
7928 auto *BI = CondBrInst::Create(Cond: IsPow2, IfTrue: SplitBB, IfFalse: DefaultCaseBB, InsertBefore: It);
7929 if (HasWeights && any_of(Range&: Weights, P: not_equal_to(Arg: 0))) {
7930 // IsPow2 covers a subset of the cases in which we'd go to the default
7931 // label. The other is those powers of 2 that don't appear in the case
7932 // statement. We don't know the distribution of the values coming in, so
7933 // the safest is to split 50-50 the original probability to `default`.
7934 uint64_t OrigDenominator =
7935 sum_of(Range: map_range(C&: Weights, F: StaticCastTo<uint64_t>));
7936 SmallVector<uint64_t> NewWeights(2);
7937 NewWeights[1] = Weights[0] / 2;
7938 NewWeights[0] = OrigDenominator - NewWeights[1];
7939 setFittedBranchWeights(I&: *BI, Weights: NewWeights, /*IsExpected=*/false);
7940 // The probability of executing the default block stays constant. It was
7941 // p_d = Weights[0] / OrigDenominator
7942 // we rewrite as W/D
7943 // We want to find the probability of the default branch of the switch
7944 // statement. Let's call it X. We have W/D = W/2D + X * (1-W/2D)
7945 // i.e. the original probability is the probability we go to the default
7946 // branch from the BI branch, or we take the default branch on the SI.
7947 // Meaning X = W / (2D - W), or (W/2) / (D - W/2)
7948 // This matches using W/2 for the default branch probability numerator and
7949 // D-W/2 as the denominator.
7950 Weights[0] = NewWeights[1];
7951 uint64_t CasesDenominator = OrigDenominator - Weights[0];
7952 for (auto &W : drop_begin(RangeOrContainer&: Weights))
7953 W = NewWeights[0] * static_cast<double>(W) / CasesDenominator;
7954
7955 setBranchWeights(I&: *SI, Weights, /*IsExpected=*/false);
7956 }
7957 // BI is handling the default case for SI, and so should share its DebugLoc.
7958 BI->setDebugLoc(SI->getDebugLoc());
7959 It->eraseFromParent();
7960
7961 addPredecessorToBlock(Succ: DefaultCaseBB, NewPred: OrigBB, ExistPred: SplitBB);
7962 if (DTU)
7963 DTU->applyUpdates(Updates: {{DominatorTree::Insert, OrigBB, DefaultCaseBB}});
7964 }
7965
7966 // Replace each case with its trailing zeros number.
7967 for (auto &Case : SI->cases()) {
7968 auto *OrigValue = Case.getCaseValue();
7969 Case.setValue(ConstantInt::get(Ty: OrigValue->getIntegerType(),
7970 V: OrigValue->getValue().countr_zero()));
7971 }
7972
7973 // Replace condition with its trailing zeros number.
7974 auto *ConditionTrailingZeros = Builder.CreateIntrinsic(
7975 ID: Intrinsic::cttz, OverloadTypes: {CondTy}, Args: {Condition, ConstantInt::getTrue(Context)});
7976
7977 SI->setCondition(ConditionTrailingZeros);
7978
7979 return true;
7980}
7981
7982/// Fold switch over ucmp/scmp intrinsic to br if two of the switch arms have
7983/// the same destination.
7984static bool simplifySwitchOfCmpIntrinsic(SwitchInst *SI, IRBuilderBase &Builder,
7985 DomTreeUpdater *DTU) {
7986 auto *Cmp = dyn_cast<CmpIntrinsic>(Val: SI->getCondition());
7987 if (!Cmp || !Cmp->hasOneUse())
7988 return false;
7989
7990 SmallVector<uint32_t, 4> Weights;
7991 bool HasWeights = extractBranchWeights(ProfileData: getBranchWeightMDNode(I: *SI), Weights);
7992 if (!HasWeights)
7993 Weights.resize(N: 4); // Avoid checking HasWeights everywhere.
7994
7995 // Normalize to [us]cmp == Res ? Succ : OtherSucc.
7996 int64_t Res;
7997 BasicBlock *Succ, *OtherSucc;
7998 uint32_t SuccWeight = 0, OtherSuccWeight = 0;
7999 BasicBlock *Unreachable = nullptr;
8000
8001 if (SI->getNumCases() == 2) {
8002 // Find which of 1, 0 or -1 is missing (handled by default dest).
8003 SmallSet<int64_t, 3> Missing;
8004 Missing.insert(V: 1);
8005 Missing.insert(V: 0);
8006 Missing.insert(V: -1);
8007
8008 Succ = SI->getDefaultDest();
8009 SuccWeight = Weights[0];
8010 OtherSucc = nullptr;
8011 for (auto &Case : SI->cases()) {
8012 std::optional<int64_t> Val =
8013 Case.getCaseValue()->getValue().trySExtValue();
8014 if (!Val)
8015 return false;
8016 if (!Missing.erase(V: *Val))
8017 return false;
8018 if (OtherSucc && OtherSucc != Case.getCaseSuccessor())
8019 return false;
8020 OtherSucc = Case.getCaseSuccessor();
8021 OtherSuccWeight += Weights[Case.getSuccessorIndex()];
8022 }
8023
8024 assert(Missing.size() == 1 && "Should have one case left");
8025 Res = *Missing.begin();
8026 } else if (SI->getNumCases() == 3 && SI->defaultDestUnreachable()) {
8027 // Normalize so that Succ is taken once and OtherSucc twice.
8028 Unreachable = SI->getDefaultDest();
8029 Succ = OtherSucc = nullptr;
8030 for (auto &Case : SI->cases()) {
8031 BasicBlock *NewSucc = Case.getCaseSuccessor();
8032 uint32_t Weight = Weights[Case.getSuccessorIndex()];
8033 if (!OtherSucc || OtherSucc == NewSucc) {
8034 OtherSucc = NewSucc;
8035 OtherSuccWeight += Weight;
8036 } else if (!Succ) {
8037 Succ = NewSucc;
8038 SuccWeight = Weight;
8039 } else if (Succ == NewSucc) {
8040 std::swap(a&: Succ, b&: OtherSucc);
8041 std::swap(a&: SuccWeight, b&: OtherSuccWeight);
8042 } else
8043 return false;
8044 }
8045 for (auto &Case : SI->cases()) {
8046 std::optional<int64_t> Val =
8047 Case.getCaseValue()->getValue().trySExtValue();
8048 if (!Val || (Val != 1 && Val != 0 && Val != -1))
8049 return false;
8050 if (Case.getCaseSuccessor() == Succ) {
8051 Res = *Val;
8052 break;
8053 }
8054 }
8055 } else {
8056 return false;
8057 }
8058
8059 // Determine predicate for the missing case.
8060 ICmpInst::Predicate Pred;
8061 switch (Res) {
8062 case 1:
8063 Pred = ICmpInst::ICMP_UGT;
8064 break;
8065 case 0:
8066 Pred = ICmpInst::ICMP_EQ;
8067 break;
8068 case -1:
8069 Pred = ICmpInst::ICMP_ULT;
8070 break;
8071 }
8072 if (Cmp->isSigned())
8073 Pred = ICmpInst::getSignedPredicate(Pred);
8074
8075 MDNode *NewWeights = nullptr;
8076 if (HasWeights)
8077 NewWeights = MDBuilder(SI->getContext())
8078 .createBranchWeights(TrueWeight: SuccWeight, FalseWeight: OtherSuccWeight);
8079
8080 BasicBlock *BB = SI->getParent();
8081 Builder.SetInsertPoint(SI->getIterator());
8082 Value *ICmp = Builder.CreateICmp(P: Pred, LHS: Cmp->getLHS(), RHS: Cmp->getRHS());
8083 Builder.CreateCondBr(Cond: ICmp, True: Succ, False: OtherSucc, BranchWeights: NewWeights,
8084 Unpredictable: SI->getMetadata(KindID: LLVMContext::MD_unpredictable));
8085 OtherSucc->removePredecessor(Pred: BB);
8086 if (Unreachable)
8087 Unreachable->removePredecessor(Pred: BB);
8088 SI->eraseFromParent();
8089 Cmp->eraseFromParent();
8090 if (DTU && Unreachable)
8091 DTU->applyUpdates(Updates: {{DominatorTree::Delete, BB, Unreachable}});
8092 return true;
8093}
8094
8095/// Checking whether two BBs are equal depends on the contents of the
8096/// BasicBlock and the incoming values of their successor PHINodes.
8097/// PHINode::getIncomingValueForBlock is O(|Preds|), so we'd like to avoid
8098/// calling this function on each BasicBlock every time isEqual is called,
8099/// especially since the same BasicBlock may be passed as an argument multiple
8100/// times. To do this, we can precompute a map of PHINode -> Pred BasicBlock ->
8101/// IncomingValue and add it in the Wrapper so isEqual can do O(1) checking
8102/// of the incoming values.
8103struct EqualBBWrapper {
8104 BasicBlock *BB;
8105
8106 // One Phi usually has < 8 incoming values.
8107 using BB2ValueMap = SmallDenseMap<BasicBlock *, Value *, 8>;
8108 using Phi2IVsMap = DenseMap<PHINode *, BB2ValueMap>;
8109 Phi2IVsMap *PhiPredIVs;
8110
8111 // We only merge the identical non-entry BBs with
8112 // - terminator unconditional br to Succ (pending relaxation),
8113 // - does not have address taken / weird control.
8114 static bool canBeMerged(const BasicBlock *BB) {
8115 assert(BB && "Expected non-null BB");
8116 // Entry block cannot be eliminated or have predecessors.
8117 if (BB->isEntryBlock())
8118 return false;
8119
8120 // Single successor and must be Succ.
8121 // FIXME: Relax that the terminator is a BranchInst by checking for equality
8122 // on other kinds of terminators. We decide to only support unconditional
8123 // branches for now for compile time reasons.
8124 auto *BI = dyn_cast<UncondBrInst>(Val: BB->getTerminator());
8125 if (!BI)
8126 return false;
8127
8128 // Avoid blocks that are "address-taken" (blockaddress) or have unusual
8129 // uses.
8130 if (BB->hasAddressTaken() || BB->isEHPad())
8131 return false;
8132
8133 // TODO: relax this condition to merge equal blocks with >1 instructions?
8134 // Here, we use a O(1) form of the O(n) comparison of `size() != 1`.
8135 if (&BB->front() != &BB->back())
8136 return false;
8137
8138 // The BB must have at least one predecessor.
8139 if (pred_empty(BB))
8140 return false;
8141
8142 return true;
8143 }
8144};
8145
8146template <> struct llvm::DenseMapInfo<const EqualBBWrapper *> {
8147 static unsigned getHashValue(const EqualBBWrapper *EBW) {
8148 BasicBlock *BB = EBW->BB;
8149 UncondBrInst *BI = cast<UncondBrInst>(Val: BB->getTerminator());
8150 assert(BB->size() == 1 && "Expected just a single branch in the BB");
8151
8152 // Since we assume the BB is just a single UncondBrInst with a single
8153 // successor, we hash as the BB and the incoming Values of its successor
8154 // PHIs. Initially, we tried to just use the successor BB as the hash, but
8155 // including the incoming PHI values leads to better performance.
8156 // We also tried to build a map from BB -> Succs.IncomingValues ahead of
8157 // time and passing it in EqualBBWrapper, but this slowed down the average
8158 // compile time without having any impact on the worst case compile time.
8159 BasicBlock *Succ = BI->getSuccessor();
8160 auto PhiValsForBB = map_range(C: Succ->phis(), F: [&](PHINode &Phi) {
8161 return (*EBW->PhiPredIVs)[&Phi][BB];
8162 });
8163 return hash_combine(args: Succ, args: hash_combine_range(R&: PhiValsForBB));
8164 }
8165 static bool isEqual(const EqualBBWrapper *LHS, const EqualBBWrapper *RHS) {
8166 BasicBlock *A = LHS->BB;
8167 BasicBlock *B = RHS->BB;
8168
8169 // FIXME: we checked that the size of A and B are both 1 in
8170 // mergeIdenticalUncondBBs to make the Case list smaller to
8171 // improve performance. If we decide to support BasicBlocks with more
8172 // than just a single instruction, we need to check that A.size() ==
8173 // B.size() here, and we need to check more than just the BranchInsts
8174 // for equality.
8175
8176 UncondBrInst *ABI = cast<UncondBrInst>(Val: A->getTerminator());
8177 UncondBrInst *BBI = cast<UncondBrInst>(Val: B->getTerminator());
8178 if (ABI->getSuccessor() != BBI->getSuccessor())
8179 return false;
8180
8181 // Need to check that PHIs in successor have matching values.
8182 BasicBlock *Succ = ABI->getSuccessor();
8183 auto IfPhiIVMatch = [&](PHINode &Phi) {
8184 // Replace O(|Pred|) Phi.getIncomingValueForBlock with this O(1) hashmap
8185 // query.
8186 auto &PredIVs = (*LHS->PhiPredIVs)[&Phi];
8187 return PredIVs[A] == PredIVs[B];
8188 };
8189 return all_of(Range: Succ->phis(), P: IfPhiIVMatch);
8190 }
8191};
8192
8193// Merge identical BBs into one of them.
8194static bool mergeIdenticalBBs(ArrayRef<BasicBlock *> Candidates,
8195 DomTreeUpdater *DTU) {
8196 if (Candidates.size() < 2)
8197 return false;
8198
8199 // Build Cases. Skip BBs that are not candidates for simplification. Mark
8200 // PHINodes which need to be processed into PhiPredIVs. We decide to process
8201 // an entire PHI at once after the loop, opposed to calling
8202 // getIncomingValueForBlock inside this loop, since each call to
8203 // getIncomingValueForBlock is O(|Preds|).
8204 EqualBBWrapper::Phi2IVsMap PhiPredIVs;
8205 SmallVector<EqualBBWrapper> BBs2Merge;
8206 BBs2Merge.reserve(N: Candidates.size());
8207 SmallSetVector<PHINode *, 8> Phis;
8208
8209 for (BasicBlock *BB : Candidates) {
8210 BasicBlock *Succ = BB->getSingleSuccessor();
8211 assert(Succ && "Expected unconditional BB");
8212 BBs2Merge.emplace_back(Args: EqualBBWrapper{.BB: BB, .PhiPredIVs: &PhiPredIVs});
8213 Phis.insert_range(R: make_pointer_range(Range: Succ->phis()));
8214 }
8215
8216 // Precompute a data structure to improve performance of isEqual for
8217 // EqualBBWrapper.
8218 PhiPredIVs.reserve(NumEntries: Phis.size());
8219 for (PHINode *Phi : Phis) {
8220 auto &IVs =
8221 PhiPredIVs.try_emplace(Key: Phi, Args: Phi->getNumIncomingValues()).first->second;
8222 // Pre-fill all incoming for O(1) lookup as Phi.getIncomingValueForBlock is
8223 // O(|Pred|).
8224 for (auto &IV : Phi->incoming_values())
8225 IVs.insert(KV: {Phi->getIncomingBlock(U: IV), IV.get()});
8226 }
8227
8228 // Group duplicates using DenseSet with custom equality/hashing.
8229 // Build a set such that if the EqualBBWrapper exists in the set and another
8230 // EqualBBWrapper isEqual, then the equivalent EqualBBWrapper which is not in
8231 // the set should be replaced with the one in the set. If the EqualBBWrapper
8232 // is not in the set, then it should be added to the set so other
8233 // EqualBBWrapper can check against it in the same manner. We use
8234 // EqualBBWrapper instead of just BasicBlock because we'd like to pass around
8235 // information to isEquality, getHashValue, and when doing the replacement
8236 // with better performance.
8237 DenseSet<const EqualBBWrapper *> Keep;
8238 Keep.reserve(Size: BBs2Merge.size());
8239
8240 SmallVector<DominatorTree::UpdateType> Updates;
8241 Updates.reserve(N: BBs2Merge.size() * 2);
8242
8243 bool MadeChange = false;
8244
8245 // Helper: redirect all edges X -> DeadPred to X -> LivePred.
8246 auto RedirectIncomingEdges = [&](BasicBlock *Dead, BasicBlock *Live) {
8247 SmallSetVector<BasicBlock *, 8> DeadPreds(llvm::from_range,
8248 predecessors(BB: Dead));
8249 if (DTU) {
8250 // All predecessors of DeadPred (except the common predecessor) will be
8251 // moved to LivePred.
8252 Updates.reserve(N: Updates.size() + DeadPreds.size() * 2);
8253 SmallPtrSet<BasicBlock *, 16> LivePreds(llvm::from_range,
8254 predecessors(BB: Live));
8255 for (BasicBlock *PredOfDead : DeadPreds) {
8256 // Do not modify those common predecessors of DeadPred and LivePred.
8257 if (!LivePreds.contains(Ptr: PredOfDead))
8258 Updates.push_back(Elt: {DominatorTree::Insert, PredOfDead, Live});
8259 Updates.push_back(Elt: {DominatorTree::Delete, PredOfDead, Dead});
8260 }
8261 }
8262 LLVM_DEBUG(dbgs() << "Replacing duplicate pred BB ";
8263 Dead->printAsOperand(dbgs()); dbgs() << " with pred ";
8264 Live->printAsOperand(dbgs()); dbgs() << " for ";
8265 Live->getSingleSuccessor()->printAsOperand(dbgs());
8266 dbgs() << "\n");
8267 // Replace successors in all predecessors of DeadPred.
8268 for (BasicBlock *PredOfDead : DeadPreds) {
8269 Instruction *T = PredOfDead->getTerminator();
8270 T->replaceSuccessorWith(OldBB: Dead, NewBB: Live);
8271 }
8272 };
8273
8274 // Try to eliminate duplicate predecessors.
8275 for (const auto &EBW : BBs2Merge) {
8276 // EBW is a candidate for simplification. If we find a duplicate BB,
8277 // replace it.
8278 const auto &[It, Inserted] = Keep.insert(V: &EBW);
8279 if (Inserted)
8280 continue;
8281
8282 // Found duplicate: merge P into canonical predecessor It->Pred.
8283 BasicBlock *KeepBB = (*It)->BB;
8284 BasicBlock *DeadBB = EBW.BB;
8285
8286 // Avoid merging a BB with itself.
8287 if (KeepBB == DeadBB)
8288 continue;
8289
8290 // Redirect all edges into DeadPred to KeepPred.
8291 RedirectIncomingEdges(DeadBB, KeepBB);
8292
8293 // Now DeadBB should become unreachable; leave DCE to later,
8294 // but we can try to simplify it if it only branches to Succ.
8295 // (We won't erase here to keep the routine simple and DT-safe.)
8296 assert(pred_empty(DeadBB) && "DeadBB should be unreachable.");
8297 MadeChange = true;
8298 }
8299
8300 if (DTU && !Updates.empty())
8301 DTU->applyUpdates(Updates);
8302
8303 return MadeChange;
8304}
8305
8306bool SimplifyCFGOpt::simplifyDuplicateSwitchArms(SwitchInst *SI,
8307 DomTreeUpdater *DTU) {
8308 // Collect candidate switch-arms top-down.
8309 SmallSetVector<BasicBlock *, 16> FilteredArms(
8310 llvm::from_range,
8311 make_filter_range(Range: successors(I: SI), Pred: EqualBBWrapper::canBeMerged));
8312 return mergeIdenticalBBs(Candidates: FilteredArms.getArrayRef(), DTU);
8313}
8314
8315bool SimplifyCFGOpt::simplifyDuplicatePredecessors(BasicBlock *BB,
8316 DomTreeUpdater *DTU) {
8317 // Need at least 2 predecessors to do anything.
8318 if (!BB || !BB->hasNPredecessorsOrMore(N: 2))
8319 return false;
8320
8321 // Compilation time consideration: retain the canonical loop, otherwise, we
8322 // require more time in the later loop canonicalization.
8323 if (Options.NeedCanonicalLoop && is_contained(Range&: LoopHeaders, Element: BB))
8324 return false;
8325
8326 // Collect candidate predecessors bottom-up.
8327 SmallSetVector<BasicBlock *, 8> FilteredPreds(
8328 llvm::from_range,
8329 make_filter_range(Range: predecessors(BB), Pred: EqualBBWrapper::canBeMerged));
8330 return mergeIdenticalBBs(Candidates: FilteredPreds.getArrayRef(), DTU);
8331}
8332
8333bool SimplifyCFGOpt::simplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) {
8334 BasicBlock *BB = SI->getParent();
8335
8336 if (isValueEqualityComparison(TI: SI)) {
8337 // If we only have one predecessor, and if it is a branch on this value,
8338 // see if that predecessor totally determines the outcome of this switch.
8339 if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
8340 if (simplifyEqualityComparisonWithOnlyPredecessor(TI: SI, Pred: OnlyPred, Builder))
8341 return requestResimplify();
8342
8343 Value *Cond = SI->getCondition();
8344 if (SelectInst *Select = dyn_cast<SelectInst>(Val: Cond))
8345 if (simplifySwitchOnSelect(SI, Select))
8346 return requestResimplify();
8347
8348 // If the block only contains the switch, see if we can fold the block
8349 // away into any preds.
8350 if (SI == &*BB->begin())
8351 if (foldValueComparisonIntoPredecessors(TI: SI, Builder))
8352 return requestResimplify();
8353 }
8354
8355 // Try to transform the switch into an icmp and a branch.
8356 // The conversion from switch to comparison may lose information on
8357 // impossible switch values, so disable it early in the pipeline.
8358 if (Options.ConvertSwitchRangeToICmp && turnSwitchRangeIntoICmp(SI, Builder))
8359 return requestResimplify();
8360
8361 // Remove unreachable cases.
8362 if (eliminateDeadSwitchCases(SI, DTU, AC: Options.AC, DL))
8363 return requestResimplify();
8364
8365 if (simplifySwitchOfCmpIntrinsic(SI, Builder, DTU))
8366 return requestResimplify();
8367
8368 if (trySwitchToSelect(SI, Builder, DTU, DL, TTI))
8369 return requestResimplify();
8370
8371 if (Options.ForwardSwitchCondToPhi && forwardSwitchConditionToPHI(SI))
8372 return requestResimplify();
8373
8374 // The conversion of switches to arithmetic or lookup table is disabled in
8375 // the early optimization pipeline, as it may lose information or make the
8376 // resulting code harder to analyze.
8377 if (Options.ConvertSwitchToArithmetic || Options.ConvertSwitchToLookupTable)
8378 if (simplifySwitchLookup(SI, Builder, DTU, DL, TTI,
8379 ConvertSwitchToLookupTable: Options.ConvertSwitchToLookupTable))
8380 return requestResimplify();
8381
8382 if (simplifySwitchOfPowersOfTwo(SI, Builder, DTU, DL, TTI))
8383 return requestResimplify();
8384
8385 if (reduceSwitchRange(SI, Builder, DL, TTI))
8386 return requestResimplify();
8387
8388 if (HoistCommon &&
8389 hoistCommonCodeFromSuccessors(TI: SI, AllInstsEqOnly: !Options.HoistCommonInsts))
8390 return requestResimplify();
8391
8392 // We can merge identical switch arms early to enhance more aggressive
8393 // optimization on switch.
8394 if (simplifyDuplicateSwitchArms(SI, DTU))
8395 return requestResimplify();
8396
8397 if (simplifySwitchWhenUMin(SI, DTU))
8398 return requestResimplify();
8399
8400 return false;
8401}
8402
8403bool SimplifyCFGOpt::simplifyIndirectBr(IndirectBrInst *IBI) {
8404 BasicBlock *BB = IBI->getParent();
8405 bool Changed = false;
8406 SmallVector<uint32_t> BranchWeights;
8407 const bool HasBranchWeights = !ProfcheckDisableMetadataFixes &&
8408 extractBranchWeights(I: *IBI, Weights&: BranchWeights);
8409
8410 DenseMap<const BasicBlock *, uint64_t> TargetWeight;
8411 if (HasBranchWeights)
8412 for (size_t I = 0, E = IBI->getNumDestinations(); I < E; ++I)
8413 TargetWeight[IBI->getDestination(i: I)] += BranchWeights[I];
8414
8415 // Eliminate redundant destinations.
8416 SmallPtrSet<Value *, 8> Succs;
8417 SmallSetVector<BasicBlock *, 8> RemovedSuccs;
8418 for (unsigned I = 0, E = IBI->getNumDestinations(); I != E; ++I) {
8419 BasicBlock *Dest = IBI->getDestination(i: I);
8420 if (!Dest->hasAddressTaken() || !Succs.insert(Ptr: Dest).second) {
8421 if (!Dest->hasAddressTaken())
8422 RemovedSuccs.insert(X: Dest);
8423 Dest->removePredecessor(Pred: BB);
8424 IBI->removeDestination(i: I);
8425 --I;
8426 --E;
8427 Changed = true;
8428 }
8429 }
8430
8431 if (DTU) {
8432 std::vector<DominatorTree::UpdateType> Updates;
8433 Updates.reserve(n: RemovedSuccs.size());
8434 for (auto *RemovedSucc : RemovedSuccs)
8435 Updates.push_back(x: {DominatorTree::Delete, BB, RemovedSucc});
8436 DTU->applyUpdates(Updates);
8437 }
8438
8439 if (IBI->getNumDestinations() == 0) {
8440 // If the indirectbr has no successors, change it to unreachable.
8441 new UnreachableInst(IBI->getContext(), IBI->getIterator());
8442 eraseTerminatorAndDCECond(TI: IBI);
8443 return true;
8444 }
8445
8446 if (IBI->getNumDestinations() == 1) {
8447 // If the indirectbr has one successor, change it to a direct branch.
8448 UncondBrInst::Create(Target: IBI->getDestination(i: 0), InsertBefore: IBI->getIterator());
8449 eraseTerminatorAndDCECond(TI: IBI);
8450 return true;
8451 }
8452 if (HasBranchWeights) {
8453 SmallVector<uint64_t> NewBranchWeights(IBI->getNumDestinations());
8454 for (size_t I = 0, E = IBI->getNumDestinations(); I < E; ++I)
8455 NewBranchWeights[I] += TargetWeight.find(Val: IBI->getDestination(i: I))->second;
8456 setFittedBranchWeights(I&: *IBI, Weights: NewBranchWeights, /*IsExpected=*/false);
8457 }
8458 if (SelectInst *SI = dyn_cast<SelectInst>(Val: IBI->getAddress())) {
8459 if (simplifyIndirectBrOnSelect(IBI, SI))
8460 return requestResimplify();
8461 }
8462 return Changed;
8463}
8464
8465/// Given an block with only a single landing pad and a unconditional branch
8466/// try to find another basic block which this one can be merged with. This
8467/// handles cases where we have multiple invokes with unique landing pads, but
8468/// a shared handler.
8469///
8470/// We specifically choose to not worry about merging non-empty blocks
8471/// here. That is a PRE/scheduling problem and is best solved elsewhere. In
8472/// practice, the optimizer produces empty landing pad blocks quite frequently
8473/// when dealing with exception dense code. (see: instcombine, gvn, if-else
8474/// sinking in this file)
8475///
8476/// This is primarily a code size optimization. We need to avoid performing
8477/// any transform which might inhibit optimization (such as our ability to
8478/// specialize a particular handler via tail commoning). We do this by not
8479/// merging any blocks which require us to introduce a phi. Since the same
8480/// values are flowing through both blocks, we don't lose any ability to
8481/// specialize. If anything, we make such specialization more likely.
8482///
8483/// TODO - This transformation could remove entries from a phi in the target
8484/// block when the inputs in the phi are the same for the two blocks being
8485/// merged. In some cases, this could result in removal of the PHI entirely.
8486static bool tryToMergeLandingPad(LandingPadInst *LPad, UncondBrInst *BI,
8487 BasicBlock *BB, DomTreeUpdater *DTU) {
8488 auto Succ = BB->getUniqueSuccessor();
8489 assert(Succ);
8490 // If there's a phi in the successor block, we'd likely have to introduce
8491 // a phi into the merged landing pad block.
8492 if (isa<PHINode>(Val: *Succ->begin()))
8493 return false;
8494
8495 for (BasicBlock *OtherPred : predecessors(BB: Succ)) {
8496 if (BB == OtherPred)
8497 continue;
8498 BasicBlock::iterator I = OtherPred->begin();
8499 LandingPadInst *LPad2 = dyn_cast<LandingPadInst>(Val&: I);
8500 if (!LPad2 || !LPad2->isIdenticalTo(I: LPad))
8501 continue;
8502 ++I;
8503 UncondBrInst *BI2 = dyn_cast<UncondBrInst>(Val&: I);
8504 if (!BI2 || !BI2->isIdenticalTo(I: BI))
8505 continue;
8506
8507 std::vector<DominatorTree::UpdateType> Updates;
8508
8509 // We've found an identical block. Update our predecessors to take that
8510 // path instead and make ourselves dead.
8511 SmallSetVector<BasicBlock *, 16> UniquePreds(pred_begin(BB), pred_end(BB));
8512 for (BasicBlock *Pred : UniquePreds) {
8513 InvokeInst *II = cast<InvokeInst>(Val: Pred->getTerminator());
8514 assert(II->getNormalDest() != BB && II->getUnwindDest() == BB &&
8515 "unexpected successor");
8516 II->setUnwindDest(OtherPred);
8517 if (DTU) {
8518 Updates.push_back(x: {DominatorTree::Insert, Pred, OtherPred});
8519 Updates.push_back(x: {DominatorTree::Delete, Pred, BB});
8520 }
8521 }
8522
8523 SmallSetVector<BasicBlock *, 16> UniqueSuccs(succ_begin(BB), succ_end(BB));
8524 for (BasicBlock *Succ : UniqueSuccs) {
8525 Succ->removePredecessor(Pred: BB);
8526 if (DTU)
8527 Updates.push_back(x: {DominatorTree::Delete, BB, Succ});
8528 }
8529
8530 IRBuilder<> Builder(BI);
8531 Builder.CreateUnreachable();
8532 BI->eraseFromParent();
8533 if (DTU)
8534 DTU->applyUpdates(Updates);
8535 return true;
8536 }
8537 return false;
8538}
8539
8540bool SimplifyCFGOpt::simplifyUncondBranch(UncondBrInst *BI,
8541 IRBuilder<> &Builder) {
8542 BasicBlock *BB = BI->getParent();
8543 BasicBlock *Succ = BI->getSuccessor(i: 0);
8544
8545 // If the Terminator is the only non-phi instruction, simplify the block.
8546 // If LoopHeader is provided, check if the block or its successor is a loop
8547 // header. (This is for early invocations before loop simplify and
8548 // vectorization to keep canonical loop forms for nested loops. These blocks
8549 // can be eliminated when the pass is invoked later in the back-end.)
8550 // Note that if BB has only one predecessor then we do not introduce new
8551 // backedge, so we can eliminate BB.
8552 bool NeedCanonicalLoop =
8553 Options.NeedCanonicalLoop &&
8554 (!LoopHeaders.empty() && BB->hasNPredecessorsOrMore(N: 2) &&
8555 (is_contained(Range&: LoopHeaders, Element: BB) || is_contained(Range&: LoopHeaders, Element: Succ)));
8556 BasicBlock::iterator I = BB->getFirstNonPHIOrDbg();
8557 if (I->isTerminator() && BB != &BB->getParent()->getEntryBlock() &&
8558 !NeedCanonicalLoop && TryToSimplifyUncondBranchFromEmptyBlock(BB, DTU))
8559 return true;
8560
8561 // If the only instruction in the block is a seteq/setne comparison against a
8562 // constant, try to simplify the block.
8563 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Val&: I)) {
8564 if (ICI->isEquality() && isa<ConstantInt>(Val: ICI->getOperand(i_nocapture: 1))) {
8565 ++I;
8566 if (I->isTerminator() &&
8567 tryToSimplifyUncondBranchWithICmpInIt(ICI, Builder))
8568 return true;
8569 if (isa<SelectInst>(Val: I) && I->getNextNode()->isTerminator() &&
8570 tryToSimplifyUncondBranchWithICmpSelectInIt(ICI, Select: cast<SelectInst>(Val&: I),
8571 Builder))
8572 return true;
8573 }
8574 }
8575
8576 // See if we can merge an empty landing pad block with another which is
8577 // equivalent.
8578 if (LandingPadInst *LPad = dyn_cast<LandingPadInst>(Val&: I)) {
8579 ++I;
8580 if (I->isTerminator() && tryToMergeLandingPad(LPad, BI, BB, DTU))
8581 return true;
8582 }
8583
8584 return false;
8585}
8586
8587static BasicBlock *allPredecessorsComeFromSameSource(BasicBlock *BB) {
8588 BasicBlock *PredPred = nullptr;
8589 for (auto *P : predecessors(BB)) {
8590 BasicBlock *PPred = P->getSinglePredecessor();
8591 if (!PPred || (PredPred && PredPred != PPred))
8592 return nullptr;
8593 PredPred = PPred;
8594 }
8595 return PredPred;
8596}
8597
8598/// Fold the following pattern:
8599/// bb0:
8600/// br i1 %cond1, label %bb1, label %bb2
8601/// bb1:
8602/// br i1 %cond2, label %bb3, label %bb4
8603/// bb2:
8604/// br i1 %cond2, label %bb4, label %bb3
8605/// bb3:
8606/// ...
8607/// bb4:
8608/// ...
8609/// into
8610/// bb0:
8611/// %cond = xor i1 %cond1, %cond2
8612/// br i1 %cond, label %bb4, label %bb3
8613/// bb3:
8614/// ...
8615/// bb4:
8616/// ...
8617/// NOTE: %cond2 always dominates the terminator of bb0.
8618static bool mergeNestedCondBranch(CondBrInst *BI, DomTreeUpdater *DTU) {
8619 BasicBlock *BB = BI->getParent();
8620 BasicBlock *BB1 = BI->getSuccessor(i: 0);
8621 BasicBlock *BB2 = BI->getSuccessor(i: 1);
8622 auto IsSimpleSuccessor = [BB](BasicBlock *Succ, CondBrInst *&SuccBI) {
8623 if (Succ == BB)
8624 return false;
8625 if (&Succ->front() != Succ->getTerminator())
8626 return false;
8627 SuccBI = dyn_cast<CondBrInst>(Val: Succ->getTerminator());
8628 if (!SuccBI)
8629 return false;
8630 BasicBlock *Succ1 = SuccBI->getSuccessor(i: 0);
8631 BasicBlock *Succ2 = SuccBI->getSuccessor(i: 1);
8632 return Succ1 != Succ && Succ2 != Succ && Succ1 != BB && Succ2 != BB &&
8633 !isa<PHINode>(Val: Succ1->front()) && !isa<PHINode>(Val: Succ2->front());
8634 };
8635 CondBrInst *BB1BI, *BB2BI;
8636 if (!IsSimpleSuccessor(BB1, BB1BI) || !IsSimpleSuccessor(BB2, BB2BI))
8637 return false;
8638
8639 if (BB1BI->getCondition() != BB2BI->getCondition() ||
8640 BB1BI->getSuccessor(i: 0) != BB2BI->getSuccessor(i: 1) ||
8641 BB1BI->getSuccessor(i: 1) != BB2BI->getSuccessor(i: 0))
8642 return false;
8643
8644 BasicBlock *BB3 = BB1BI->getSuccessor(i: 0);
8645 BasicBlock *BB4 = BB1BI->getSuccessor(i: 1);
8646 IRBuilder<> Builder(BI);
8647 BI->setCondition(
8648 Builder.CreateXor(LHS: BI->getCondition(), RHS: BB1BI->getCondition()));
8649 BB1->removePredecessor(Pred: BB);
8650 BI->setSuccessor(idx: 0, NewSucc: BB4);
8651 BB2->removePredecessor(Pred: BB);
8652 BI->setSuccessor(idx: 1, NewSucc: BB3);
8653 if (DTU) {
8654 SmallVector<DominatorTree::UpdateType, 4> Updates;
8655 Updates.push_back(Elt: {DominatorTree::Delete, BB, BB1});
8656 Updates.push_back(Elt: {DominatorTree::Insert, BB, BB4});
8657 Updates.push_back(Elt: {DominatorTree::Delete, BB, BB2});
8658 Updates.push_back(Elt: {DominatorTree::Insert, BB, BB3});
8659
8660 DTU->applyUpdates(Updates);
8661 }
8662 bool HasWeight = false;
8663 uint64_t BBTWeight, BBFWeight;
8664 if (extractBranchWeights(I: *BI, TrueVal&: BBTWeight, FalseVal&: BBFWeight))
8665 HasWeight = true;
8666 else
8667 BBTWeight = BBFWeight = 1;
8668 uint64_t BB1TWeight, BB1FWeight;
8669 if (extractBranchWeights(I: *BB1BI, TrueVal&: BB1TWeight, FalseVal&: BB1FWeight))
8670 HasWeight = true;
8671 else
8672 BB1TWeight = BB1FWeight = 1;
8673 uint64_t BB2TWeight, BB2FWeight;
8674 if (extractBranchWeights(I: *BB2BI, TrueVal&: BB2TWeight, FalseVal&: BB2FWeight))
8675 HasWeight = true;
8676 else
8677 BB2TWeight = BB2FWeight = 1;
8678 if (HasWeight) {
8679 uint64_t Weights[2] = {BBTWeight * BB1FWeight + BBFWeight * BB2TWeight,
8680 BBTWeight * BB1TWeight + BBFWeight * BB2FWeight};
8681 setFittedBranchWeights(I&: *BI, Weights, /*IsExpected=*/false,
8682 /*ElideAllZero=*/true);
8683 }
8684 return true;
8685}
8686
8687bool SimplifyCFGOpt::simplifyCondBranch(CondBrInst *BI, IRBuilder<> &Builder) {
8688 assert(
8689 !isa<ConstantInt>(BI->getCondition()) &&
8690 BI->getSuccessor(0) != BI->getSuccessor(1) &&
8691 "Tautological conditional branch should have been eliminated already.");
8692
8693 BasicBlock *BB = BI->getParent();
8694 if (!Options.SimplifyCondBranch ||
8695 BI->getFunction()->hasFnAttribute(Kind: Attribute::OptForFuzzing))
8696 return false;
8697
8698 // Conditional branch
8699 if (isValueEqualityComparison(TI: BI)) {
8700 // If we only have one predecessor, and if it is a branch on this value,
8701 // see if that predecessor totally determines the outcome of this
8702 // switch.
8703 if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
8704 if (simplifyEqualityComparisonWithOnlyPredecessor(TI: BI, Pred: OnlyPred, Builder))
8705 return requestResimplify();
8706
8707 // This block must be empty, except for the setcond inst, if it exists.
8708 // Ignore pseudo intrinsics.
8709 for (auto &I : *BB) {
8710 if (isa<PseudoProbeInst>(Val: I) ||
8711 &I == cast<Instruction>(Val: BI->getCondition()))
8712 continue;
8713 if (&I == BI)
8714 if (foldValueComparisonIntoPredecessors(TI: BI, Builder))
8715 return requestResimplify();
8716 break;
8717 }
8718 }
8719
8720 // Try to turn "br (X == 0 | X == 1), T, F" into a switch instruction.
8721 if (simplifyBranchOnICmpChain(BI, Builder, DL))
8722 return true;
8723
8724 // If this basic block has dominating predecessor blocks and the dominating
8725 // blocks' conditions imply BI's condition, we know the direction of BI.
8726 std::optional<bool> Imp = isImpliedByDomCondition(Cond: BI->getCondition(), ContextI: BI, DL);
8727 if (Imp) {
8728 // Turn this into a branch on constant.
8729 auto *OldCond = BI->getCondition();
8730 ConstantInt *TorF = *Imp ? ConstantInt::getTrue(Context&: BB->getContext())
8731 : ConstantInt::getFalse(Context&: BB->getContext());
8732 BI->setCondition(TorF);
8733 RecursivelyDeleteTriviallyDeadInstructions(V: OldCond);
8734 return requestResimplify();
8735 }
8736
8737 // If this basic block is ONLY a compare and a branch, and if a predecessor
8738 // branches to us and one of our successors, fold the comparison into the
8739 // predecessor and use logical operations to pick the right destination.
8740 if (Options.SpeculateBlocks &&
8741 foldBranchToCommonDest(BI, DTU, /*MSSAU=*/nullptr, TTI: &TTI, AC: Options.AC,
8742 BonusInstThreshold: Options.BonusInstThreshold))
8743 return requestResimplify();
8744
8745 // We have a conditional branch to two blocks that are only reachable
8746 // from BI. We know that the condbr dominates the two blocks, so see if
8747 // there is any identical code in the "then" and "else" blocks. If so, we
8748 // can hoist it up to the branching block.
8749 if (BI->getSuccessor(i: 0)->getSinglePredecessor()) {
8750 if (BI->getSuccessor(i: 1)->getSinglePredecessor()) {
8751 if (HoistCommon &&
8752 hoistCommonCodeFromSuccessors(TI: BI, AllInstsEqOnly: !Options.HoistCommonInsts))
8753 return requestResimplify();
8754
8755 if (BI && Options.HoistLoadsStoresWithCondFaulting &&
8756 isProfitableToSpeculate(BI, Invert: std::nullopt, TTI)) {
8757 SmallVector<Instruction *, 2> SpeculatedConditionalLoadsStores;
8758 auto CanSpeculateConditionalLoadsStores = [&]() {
8759 for (auto *Succ : successors(BB)) {
8760 for (Instruction &I : *Succ) {
8761 if (I.isTerminator()) {
8762 if (I.getNumSuccessors() > 1)
8763 return false;
8764 continue;
8765 } else if (!isSafeCheapLoadStore(I: &I, TTI) ||
8766 SpeculatedConditionalLoadsStores.size() ==
8767 HoistLoadsStoresWithCondFaultingThreshold) {
8768 return false;
8769 }
8770 SpeculatedConditionalLoadsStores.push_back(Elt: &I);
8771 }
8772 }
8773 return !SpeculatedConditionalLoadsStores.empty();
8774 };
8775
8776 if (CanSpeculateConditionalLoadsStores()) {
8777 hoistConditionalLoadsStores(BI, SpeculatedConditionalLoadsStores,
8778 Invert: std::nullopt, Sel: nullptr);
8779 return requestResimplify();
8780 }
8781 }
8782 } else {
8783 // If Successor #1 has multiple preds, we may be able to conditionally
8784 // execute Successor #0 if it branches to Successor #1.
8785 Instruction *Succ0TI = BI->getSuccessor(i: 0)->getTerminator();
8786 if (Succ0TI->getNumSuccessors() == 1 &&
8787 Succ0TI->getSuccessor(Idx: 0) == BI->getSuccessor(i: 1))
8788 if (speculativelyExecuteBB(BI, ThenBB: BI->getSuccessor(i: 0)))
8789 return requestResimplify();
8790 }
8791 } else if (BI->getSuccessor(i: 1)->getSinglePredecessor()) {
8792 // If Successor #0 has multiple preds, we may be able to conditionally
8793 // execute Successor #1 if it branches to Successor #0.
8794 Instruction *Succ1TI = BI->getSuccessor(i: 1)->getTerminator();
8795 if (Succ1TI->getNumSuccessors() == 1 &&
8796 Succ1TI->getSuccessor(Idx: 0) == BI->getSuccessor(i: 0))
8797 if (speculativelyExecuteBB(BI, ThenBB: BI->getSuccessor(i: 1)))
8798 return requestResimplify();
8799 }
8800
8801 // If this is a branch on something for which we know the constant value in
8802 // predecessors (e.g. a phi node in the current block), thread control
8803 // through this block.
8804 if (foldCondBranchOnValueKnownInPredecessor(BI))
8805 return requestResimplify();
8806
8807 // Scan predecessor blocks for conditional branches.
8808 for (BasicBlock *Pred : predecessors(BB))
8809 if (CondBrInst *PBI = dyn_cast<CondBrInst>(Val: Pred->getTerminator()))
8810 if (PBI != BI)
8811 if (SimplifyCondBranchToCondBranch(PBI, BI, DTU, DL, TTI))
8812 return requestResimplify();
8813
8814 // Look for diamond patterns.
8815 if (MergeCondStores)
8816 if (BasicBlock *PrevBB = allPredecessorsComeFromSameSource(BB))
8817 if (CondBrInst *PBI = dyn_cast<CondBrInst>(Val: PrevBB->getTerminator()))
8818 if (PBI != BI)
8819 if (mergeConditionalStores(PBI, QBI: BI, DTU, DL, TTI))
8820 return requestResimplify();
8821
8822 // Look for nested conditional branches.
8823 if (mergeNestedCondBranch(BI, DTU))
8824 return requestResimplify();
8825
8826 return false;
8827}
8828
8829/// Check if passing a value to an instruction will cause undefined behavior.
8830static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I, bool PtrValueMayBeModified) {
8831 assert(V->getType() == I->getType() && "Mismatched types");
8832 Constant *C = dyn_cast<Constant>(Val: V);
8833 if (!C)
8834 return false;
8835
8836 if (I->use_empty())
8837 return false;
8838
8839 if (C->isNullValue() || isa<UndefValue>(Val: C)) {
8840 // Find the first same-block use with a UB-triggering opcode, skipping
8841 // cross-block or before-I uses.
8842 auto FindUse = llvm::find_if(Range: I->uses(), P: [I](auto &U) {
8843 auto *Use = cast<Instruction>(U.getUser());
8844 // Only same-block uses after I can witness UB at I's program point.
8845 // Self-uses and before-I uses can occur when I is a PHI node.
8846 if (Use->getParent() != I->getParent() || Use == I || Use->comesBefore(I))
8847 return false;
8848 // Change this list when we want to add new instructions.
8849 switch (Use->getOpcode()) {
8850 default:
8851 return false;
8852 case Instruction::GetElementPtr:
8853 case Instruction::Ret:
8854 case Instruction::BitCast:
8855 case Instruction::Load:
8856 case Instruction::Store:
8857 case Instruction::Call:
8858 case Instruction::CallBr:
8859 case Instruction::Invoke:
8860 case Instruction::UDiv:
8861 case Instruction::URem:
8862 // Note: signed div/rem of INT_MIN / -1 is also immediate UB, not
8863 // implemented to avoid code complexity as it is unclear how useful such
8864 // logic is.
8865 case Instruction::SDiv:
8866 case Instruction::SRem:
8867 return true;
8868 }
8869 });
8870 if (FindUse == I->use_end())
8871 return false;
8872 auto &Use = *FindUse;
8873 auto *User = cast<Instruction>(Val: Use.getUser());
8874
8875 // Now make sure that there are no instructions in between that can alter
8876 // control flow (eg. calls)
8877 auto InstrRange =
8878 make_range(x: std::next(x: I->getIterator()), y: User->getIterator());
8879 if (any_of(Range&: InstrRange, P: [](Instruction &I) {
8880 return !isGuaranteedToTransferExecutionToSuccessor(I: &I);
8881 }))
8882 return false;
8883
8884 // Look through GEPs. A load from a GEP derived from NULL is still undefined
8885 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Val: User))
8886 if (GEP->getPointerOperand() == I) {
8887 // The type of GEP may differ from the type of base pointer.
8888 // Bail out on vector GEPs, as they are not handled by other checks.
8889 if (GEP->getType()->isVectorTy())
8890 return false;
8891 // The current base address is null, there are four cases to consider:
8892 // getelementptr (TY, null, 0) -> null
8893 // getelementptr (TY, null, not zero) -> may be modified
8894 // getelementptr inbounds (TY, null, 0) -> null
8895 // getelementptr inbounds (TY, null, not zero) -> poison iff null is
8896 // undefined?
8897 if (!GEP->hasAllZeroIndices() &&
8898 (!GEP->isInBounds() ||
8899 NullPointerIsDefined(F: GEP->getFunction(),
8900 AS: GEP->getPointerAddressSpace())))
8901 PtrValueMayBeModified = true;
8902 return passingValueIsAlwaysUndefined(V, I: GEP, PtrValueMayBeModified);
8903 }
8904
8905 // Look through return.
8906 if (ReturnInst *Ret = dyn_cast<ReturnInst>(Val: User)) {
8907 bool HasNoUndefAttr =
8908 Ret->getFunction()->hasRetAttribute(Kind: Attribute::NoUndef);
8909 // Return undefined to a noundef return value is undefined.
8910 if (isa<UndefValue>(Val: C) && HasNoUndefAttr)
8911 return true;
8912 // Return null to a nonnull+noundef return value is undefined.
8913 if (C->isNullValue() && HasNoUndefAttr &&
8914 Ret->getFunction()->hasRetAttribute(Kind: Attribute::NonNull)) {
8915 return !PtrValueMayBeModified;
8916 }
8917 }
8918
8919 // Load from null is undefined.
8920 if (LoadInst *LI = dyn_cast<LoadInst>(Val: User))
8921 if (!LI->isVolatile())
8922 return !NullPointerIsDefined(F: LI->getFunction(),
8923 AS: LI->getPointerAddressSpace());
8924
8925 // Store to null is undefined.
8926 if (StoreInst *SI = dyn_cast<StoreInst>(Val: User))
8927 if (!SI->isVolatile())
8928 return (!NullPointerIsDefined(F: SI->getFunction(),
8929 AS: SI->getPointerAddressSpace())) &&
8930 SI->getPointerOperand() == I;
8931
8932 // llvm.assume(false/undef) always triggers immediate UB.
8933 if (auto *Assume = dyn_cast<AssumeInst>(Val: User)) {
8934 // Ignore assume operand bundles.
8935 if (I == Assume->getArgOperand(i: 0))
8936 return true;
8937 }
8938
8939 if (auto *CB = dyn_cast<CallBase>(Val: User)) {
8940 if (C->isNullValue() && NullPointerIsDefined(F: CB->getFunction()))
8941 return false;
8942 // A call to null is undefined.
8943 if (CB->getCalledOperand() == I)
8944 return true;
8945
8946 if (CB->isArgOperand(U: &Use)) {
8947 unsigned ArgIdx = CB->getArgOperandNo(U: &Use);
8948 // Passing null to a nonnnull+noundef argument is undefined.
8949 if (isa<ConstantPointerNull>(Val: C) && C->getType()->isPointerTy() &&
8950 CB->paramHasNonNullAttr(ArgNo: ArgIdx, /*AllowUndefOrPoison=*/false))
8951 return !PtrValueMayBeModified;
8952 // Passing undef to a noundef argument is undefined.
8953 if (isa<UndefValue>(Val: C) && CB->isPassingUndefUB(ArgNo: ArgIdx))
8954 return true;
8955 }
8956 }
8957 // Div/Rem by zero is immediate UB
8958 if (match(V: User, P: m_BinOp(L: m_Value(), R: m_Specific(V: I))) && User->isIntDivRem())
8959 return true;
8960 }
8961 return false;
8962}
8963
8964/// If BB has an incoming value that will always trigger undefined behavior
8965/// (eg. null pointer dereference), remove the branch leading here.
8966static bool removeUndefIntroducingPredecessor(BasicBlock *BB,
8967 DomTreeUpdater *DTU,
8968 AssumptionCache *AC) {
8969 for (PHINode &PHI : BB->phis())
8970 for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i)
8971 if (passingValueIsAlwaysUndefined(V: PHI.getIncomingValue(i), I: &PHI)) {
8972 BasicBlock *Predecessor = PHI.getIncomingBlock(i);
8973 Instruction *T = Predecessor->getTerminator();
8974 IRBuilder<> Builder(T);
8975 if (isa<UncondBrInst>(Val: T)) {
8976 BB->removePredecessor(Pred: Predecessor);
8977 // Turn unconditional branches into unreachables.
8978 Builder.CreateUnreachable();
8979 T->eraseFromParent();
8980 if (DTU)
8981 DTU->applyUpdates(Updates: {{DominatorTree::Delete, Predecessor, BB}});
8982 return true;
8983 } else if (CondBrInst *BI = dyn_cast<CondBrInst>(Val: T)) {
8984 BB->removePredecessor(Pred: Predecessor);
8985 // Preserve guarding condition in assume, because it might not be
8986 // inferrable from any dominating condition.
8987 Value *Cond = BI->getCondition();
8988 CallInst *Assumption;
8989 if (BI->getSuccessor(i: 0) == BB)
8990 Assumption = Builder.CreateAssumption(Cond: Builder.CreateNot(V: Cond));
8991 else
8992 Assumption = Builder.CreateAssumption(Cond);
8993 if (AC)
8994 AC->registerAssumption(CI: cast<AssumeInst>(Val: Assumption));
8995 Builder.CreateBr(Dest: BI->getSuccessor(i: 0) == BB ? BI->getSuccessor(i: 1)
8996 : BI->getSuccessor(i: 0));
8997 BI->eraseFromParent();
8998 if (DTU)
8999 DTU->applyUpdates(Updates: {{DominatorTree::Delete, Predecessor, BB}});
9000 return true;
9001 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(Val: T)) {
9002 // Redirect all branches leading to UB into
9003 // a newly created unreachable block.
9004 BasicBlock *Unreachable = BasicBlock::Create(
9005 Context&: Predecessor->getContext(), Name: "unreachable", Parent: BB->getParent(), InsertBefore: BB);
9006 Builder.SetInsertPoint(Unreachable);
9007 // The new block contains only one instruction: Unreachable
9008 Builder.CreateUnreachable();
9009 for (const auto &Case : SI->cases())
9010 if (Case.getCaseSuccessor() == BB) {
9011 BB->removePredecessor(Pred: Predecessor);
9012 Case.setSuccessor(Unreachable);
9013 }
9014 if (SI->getDefaultDest() == BB) {
9015 BB->removePredecessor(Pred: Predecessor);
9016 SI->setDefaultDest(Unreachable);
9017 }
9018
9019 if (DTU)
9020 DTU->applyUpdates(
9021 Updates: { { DominatorTree::Insert, Predecessor, Unreachable },
9022 { DominatorTree::Delete, Predecessor, BB } });
9023 return true;
9024 }
9025 }
9026
9027 return false;
9028}
9029
9030bool SimplifyCFGOpt::simplifyOnce(BasicBlock *BB) {
9031 bool Changed = false;
9032
9033 assert(BB && BB->getParent() && "Block not embedded in function!");
9034 assert(BB->getTerminator() && "Degenerate basic block encountered!");
9035
9036 // Remove basic blocks that have no predecessors (except the entry block)...
9037 // or that just have themself as a predecessor. These are unreachable.
9038 if ((pred_empty(BB) && BB != &BB->getParent()->getEntryBlock()) ||
9039 BB->getSinglePredecessor() == BB) {
9040 LLVM_DEBUG(dbgs() << "Removing BB: \n" << *BB);
9041 DeleteDeadBlock(BB, DTU);
9042 return true;
9043 }
9044
9045 // Check to see if we can constant propagate this terminator instruction
9046 // away...
9047 Changed |= ConstantFoldTerminator(BB, /*DeleteDeadConditions=*/true,
9048 /*TLI=*/nullptr, DTU);
9049
9050 // Check for and eliminate duplicate PHI nodes in this block.
9051 Changed |= EliminateDuplicatePHINodes(BB);
9052
9053 // Check for and remove branches that will always cause undefined behavior.
9054 if (removeUndefIntroducingPredecessor(BB, DTU, AC: Options.AC))
9055 return requestResimplify();
9056
9057 // Merge basic blocks into their predecessor if there is only one distinct
9058 // pred, and if there is only one distinct successor of the predecessor, and
9059 // if there are no PHI nodes.
9060 if (MergeBlockIntoPredecessor(BB, DTU))
9061 return true;
9062
9063 if (SinkCommon && Options.SinkCommonInsts) {
9064 if (sinkCommonCodeFromPredecessors(BB, DTU) ||
9065 mergeCompatibleInvokes(BB, DTU)) {
9066 // sinkCommonCodeFromPredecessors() does not automatically CSE PHI's,
9067 // so we may now how duplicate PHI's.
9068 // Let's rerun EliminateDuplicatePHINodes() first,
9069 // before foldTwoEntryPHINode() potentially converts them into select's,
9070 // after which we'd need a whole EarlyCSE pass run to cleanup them.
9071 return true;
9072 }
9073 // Merge identical predecessors of this block.
9074 if (simplifyDuplicatePredecessors(BB, DTU))
9075 return true;
9076 }
9077
9078 if (Options.SpeculateBlocks &&
9079 !BB->getParent()->hasFnAttribute(Kind: Attribute::OptForFuzzing)) {
9080 // If there is a trivial two-entry PHI node in this basic block, and we can
9081 // eliminate it, do so now.
9082 if (auto *PN = dyn_cast<PHINode>(Val: BB->begin()))
9083 if (PN->getNumIncomingValues() == 2)
9084 if (foldTwoEntryPHINode(PN, TTI, DTU, AC: Options.AC, DL,
9085 SpeculateUnpredictables: Options.SpeculateUnpredictables))
9086 return true;
9087 }
9088
9089 IRBuilder<> Builder(BB);
9090 Instruction *Terminator = BB->getTerminator();
9091 Builder.SetInsertPoint(Terminator);
9092 switch (Terminator->getOpcode()) {
9093 case Instruction::UncondBr:
9094 Changed |= simplifyUncondBranch(BI: cast<UncondBrInst>(Val: Terminator), Builder);
9095 break;
9096 case Instruction::CondBr:
9097 Changed |= simplifyCondBranch(BI: cast<CondBrInst>(Val: Terminator), Builder);
9098 break;
9099 case Instruction::Resume:
9100 Changed |= simplifyResume(RI: cast<ResumeInst>(Val: Terminator), Builder);
9101 break;
9102 case Instruction::CleanupRet:
9103 Changed |= simplifyCleanupReturn(RI: cast<CleanupReturnInst>(Val: Terminator));
9104 break;
9105 case Instruction::Switch:
9106 Changed |= simplifySwitch(SI: cast<SwitchInst>(Val: Terminator), Builder);
9107 break;
9108 case Instruction::Unreachable:
9109 Changed |= simplifyUnreachable(UI: cast<UnreachableInst>(Val: Terminator));
9110 break;
9111 case Instruction::IndirectBr:
9112 Changed |= simplifyIndirectBr(IBI: cast<IndirectBrInst>(Val: Terminator));
9113 break;
9114 }
9115
9116 return Changed;
9117}
9118
9119bool SimplifyCFGOpt::run(BasicBlock *BB) {
9120 bool Changed = false;
9121
9122 // Repeated simplify BB as long as resimplification is requested.
9123 do {
9124 Resimplify = false;
9125
9126 // Perform one round of simplifcation. Resimplify flag will be set if
9127 // another iteration is requested.
9128 Changed |= simplifyOnce(BB);
9129 } while (Resimplify);
9130
9131 return Changed;
9132}
9133
9134bool llvm::simplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,
9135 DomTreeUpdater *DTU, const SimplifyCFGOptions &Options,
9136 ArrayRef<WeakVH> LoopHeaders) {
9137 return SimplifyCFGOpt(TTI, DTU, BB->getDataLayout(), LoopHeaders,
9138 Options)
9139 .run(BB);
9140}
9141