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