xref: /llvm-project/llvm/lib/CodeGen/SelectOptimize.cpp (revision e909c0ccd40e6d6aa2d10e0b60e8b992f3cde35b)
1 //===--- SelectOptimize.cpp - Convert select to branches if profitable ---===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass converts selects to conditional jumps when profitable.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/CodeGen/SelectOptimize.h"
14 #include "llvm/ADT/SetVector.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/Analysis/BlockFrequencyInfo.h"
18 #include "llvm/Analysis/BranchProbabilityInfo.h"
19 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
21 #include "llvm/Analysis/ProfileSummaryInfo.h"
22 #include "llvm/Analysis/TargetTransformInfo.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/CodeGen/TargetLowering.h"
25 #include "llvm/CodeGen/TargetPassConfig.h"
26 #include "llvm/CodeGen/TargetSchedule.h"
27 #include "llvm/CodeGen/TargetSubtargetInfo.h"
28 #include "llvm/IR/BasicBlock.h"
29 #include "llvm/IR/Dominators.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/IRBuilder.h"
32 #include "llvm/IR/Instruction.h"
33 #include "llvm/IR/PatternMatch.h"
34 #include "llvm/IR/ProfDataUtils.h"
35 #include "llvm/InitializePasses.h"
36 #include "llvm/Pass.h"
37 #include "llvm/Support/ScaledNumber.h"
38 #include "llvm/Target/TargetMachine.h"
39 #include "llvm/Transforms/Utils/SizeOpts.h"
40 #include <algorithm>
41 #include <queue>
42 #include <stack>
43 
44 using namespace llvm;
45 using namespace llvm::PatternMatch;
46 
47 #define DEBUG_TYPE "select-optimize"
48 
49 STATISTIC(NumSelectOptAnalyzed,
50           "Number of select groups considered for conversion to branch");
51 STATISTIC(NumSelectConvertedExpColdOperand,
52           "Number of select groups converted due to expensive cold operand");
53 STATISTIC(NumSelectConvertedHighPred,
54           "Number of select groups converted due to high-predictability");
55 STATISTIC(NumSelectUnPred,
56           "Number of select groups not converted due to unpredictability");
57 STATISTIC(NumSelectColdBB,
58           "Number of select groups not converted due to cold basic block");
59 STATISTIC(NumSelectConvertedLoop,
60           "Number of select groups converted due to loop-level analysis");
61 STATISTIC(NumSelectsConverted, "Number of selects converted");
62 
63 static cl::opt<unsigned> ColdOperandThreshold(
64     "cold-operand-threshold",
65     cl::desc("Maximum frequency of path for an operand to be considered cold."),
66     cl::init(20), cl::Hidden);
67 
68 static cl::opt<unsigned> ColdOperandMaxCostMultiplier(
69     "cold-operand-max-cost-multiplier",
70     cl::desc("Maximum cost multiplier of TCC_expensive for the dependence "
71              "slice of a cold operand to be considered inexpensive."),
72     cl::init(1), cl::Hidden);
73 
74 static cl::opt<unsigned>
75     GainGradientThreshold("select-opti-loop-gradient-gain-threshold",
76                           cl::desc("Gradient gain threshold (%)."),
77                           cl::init(25), cl::Hidden);
78 
79 static cl::opt<unsigned>
80     GainCycleThreshold("select-opti-loop-cycle-gain-threshold",
81                        cl::desc("Minimum gain per loop (in cycles) threshold."),
82                        cl::init(4), cl::Hidden);
83 
84 static cl::opt<unsigned> GainRelativeThreshold(
85     "select-opti-loop-relative-gain-threshold",
86     cl::desc(
87         "Minimum relative gain per loop threshold (1/X). Defaults to 12.5%"),
88     cl::init(8), cl::Hidden);
89 
90 static cl::opt<unsigned> MispredictDefaultRate(
91     "mispredict-default-rate", cl::Hidden, cl::init(25),
92     cl::desc("Default mispredict rate (initialized to 25%)."));
93 
94 static cl::opt<bool>
95     DisableLoopLevelHeuristics("disable-loop-level-heuristics", cl::Hidden,
96                                cl::init(false),
97                                cl::desc("Disable loop-level heuristics."));
98 
99 namespace {
100 
101 class SelectOptimizeImpl {
102   const TargetMachine *TM = nullptr;
103   const TargetSubtargetInfo *TSI = nullptr;
104   const TargetLowering *TLI = nullptr;
105   const TargetTransformInfo *TTI = nullptr;
106   const LoopInfo *LI = nullptr;
107   BlockFrequencyInfo *BFI;
108   ProfileSummaryInfo *PSI = nullptr;
109   OptimizationRemarkEmitter *ORE = nullptr;
110   TargetSchedModel TSchedModel;
111 
112 public:
113   SelectOptimizeImpl() = default;
114   SelectOptimizeImpl(const TargetMachine *TM) : TM(TM){};
115   PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
116   bool runOnFunction(Function &F, Pass &P);
117 
118   using Scaled64 = ScaledNumber<uint64_t>;
119 
120   struct CostInfo {
121     /// Predicated cost (with selects as conditional moves).
122     Scaled64 PredCost;
123     /// Non-predicated cost (with selects converted to branches).
124     Scaled64 NonPredCost;
125   };
126 
127   /// SelectLike is an abstraction over SelectInst and other operations that can
128   /// act like selects. For example Or(Zext(icmp), X) can be treated like
129   /// select(icmp, X|1, X).
130   class SelectLike {
131     /// The select (/or) instruction.
132     Instruction *I;
133     /// Whether this select is inverted, "not(cond), FalseVal, TrueVal", as
134     /// opposed to the original condition.
135     bool Inverted = false;
136 
137     /// The index of the operand that depends on condition. Only for select-like
138     /// instruction such as Or/Add.
139     unsigned CondIdx;
140 
141   public:
142     SelectLike(Instruction *I, bool Inverted = false, unsigned CondIdx = 0)
143         : I(I), Inverted(Inverted), CondIdx(CondIdx) {}
144 
145     Instruction *getI() { return I; }
146     const Instruction *getI() const { return I; }
147 
148     Type *getType() const { return I->getType(); }
149 
150     unsigned getConditionOpIndex() { return CondIdx; };
151 
152     /// Return the true value for the SelectLike instruction. Note this may not
153     /// exist for all SelectLike instructions. For example, for `or(zext(c), x)`
154     /// the true value would be `or(x,1)`. As this value does not exist, nullptr
155     /// is returned.
156     Value *getTrueValue(bool HonorInverts = true) const {
157       if (Inverted && HonorInverts)
158         return getFalseValue(/*HonorInverts=*/false);
159       if (auto *Sel = dyn_cast<SelectInst>(I))
160         return Sel->getTrueValue();
161       // Or(zext) case - The true value is Or(X), so return nullptr as the value
162       // does not yet exist.
163       if (isa<BinaryOperator>(I))
164         return nullptr;
165 
166       llvm_unreachable("Unhandled case in getTrueValue");
167     }
168 
169     /// Return the false value for the SelectLike instruction. For example the
170     /// getFalseValue of a select or `x` in `or(zext(c), x)` (which is
171     /// `select(c, x|1, x)`)
172     Value *getFalseValue(bool HonorInverts = true) const {
173       if (Inverted && HonorInverts)
174         return getTrueValue(/*HonorInverts=*/false);
175       if (auto *Sel = dyn_cast<SelectInst>(I))
176         return Sel->getFalseValue();
177       // We are on the branch where the condition is zero, which means BinOp
178       // does not perform any computation, and we can simply return the operand
179       // that is not related to the condition
180       if (auto *BO = dyn_cast<BinaryOperator>(I))
181         return BO->getOperand(1 - CondIdx);
182 
183       llvm_unreachable("Unhandled case in getFalseValue");
184     }
185 
186     /// Return the NonPredCost cost of the op on \p isTrue branch, given the
187     /// costs in \p InstCostMap. This may need to be generated for select-like
188     /// instructions.
189     Scaled64 getOpCostOnBranch(
190         bool IsTrue, const DenseMap<const Instruction *, CostInfo> &InstCostMap,
191         const TargetTransformInfo *TTI) {
192       auto *V = IsTrue ? getTrueValue() : getFalseValue();
193       if (V) {
194         if (auto *IV = dyn_cast<Instruction>(V)) {
195           auto It = InstCostMap.find(IV);
196           return It != InstCostMap.end() ? It->second.NonPredCost
197                                          : Scaled64::getZero();
198         }
199         return Scaled64::getZero();
200       }
201       // If getTrue(False)Value() return nullptr, it means we are dealing with
202       // select-like instructions on the branch where the actual computation is
203       // happening. In that case the cost is equal to the cost of computation +
204       // cost of non-dependant on condition operand
205       InstructionCost Cost = TTI->getArithmeticInstrCost(
206           getI()->getOpcode(), I->getType(), TargetTransformInfo::TCK_Latency,
207           {TargetTransformInfo::OK_AnyValue, TargetTransformInfo::OP_None},
208           {TTI::OK_UniformConstantValue, TTI::OP_PowerOf2});
209       auto TotalCost = Scaled64::get(*Cost.getValue());
210       if (auto *OpI = dyn_cast<Instruction>(I->getOperand(1 - CondIdx))) {
211         auto It = InstCostMap.find(OpI);
212         if (It != InstCostMap.end())
213           TotalCost += It->second.NonPredCost;
214       }
215       return TotalCost;
216     }
217   };
218 
219 private:
220   // Select groups consist of consecutive select-like instructions with the same
221   // condition. Between select-likes could be any number of auxiliary
222   // instructions related to the condition like not, zext, ashr/lshr
223   struct SelectGroup {
224     Value *Condition;
225     SmallVector<SelectLike, 2> Selects;
226   };
227   using SelectGroups = SmallVector<SelectGroup, 2>;
228 
229   // Converts select instructions of a function to conditional jumps when deemed
230   // profitable. Returns true if at least one select was converted.
231   bool optimizeSelects(Function &F);
232 
233   // Heuristics for determining which select instructions can be profitably
234   // conveted to branches. Separate heuristics for selects in inner-most loops
235   // and the rest of code regions (base heuristics for non-inner-most loop
236   // regions).
237   void optimizeSelectsBase(Function &F, SelectGroups &ProfSIGroups);
238   void optimizeSelectsInnerLoops(Function &F, SelectGroups &ProfSIGroups);
239 
240   // Converts to branches the select groups that were deemed
241   // profitable-to-convert.
242   void convertProfitableSIGroups(SelectGroups &ProfSIGroups);
243 
244   // Splits selects of a given basic block into select groups.
245   void collectSelectGroups(BasicBlock &BB, SelectGroups &SIGroups);
246 
247   // Determines for which select groups it is profitable converting to branches
248   // (base and inner-most-loop heuristics).
249   void findProfitableSIGroupsBase(SelectGroups &SIGroups,
250                                   SelectGroups &ProfSIGroups);
251   void findProfitableSIGroupsInnerLoops(const Loop *L, SelectGroups &SIGroups,
252                                         SelectGroups &ProfSIGroups);
253 
254   // Determines if a select group should be converted to a branch (base
255   // heuristics).
256   bool isConvertToBranchProfitableBase(const SelectGroup &ASI);
257 
258   // Returns true if there are expensive instructions in the cold value
259   // operand's (if any) dependence slice of any of the selects of the given
260   // group.
261   bool hasExpensiveColdOperand(const SelectGroup &ASI);
262 
263   // For a given source instruction, collect its backwards dependence slice
264   // consisting of instructions exclusively computed for producing the operands
265   // of the source instruction.
266   void getExclBackwardsSlice(Instruction *I, std::stack<Instruction *> &Slice,
267                              Instruction *SI, bool ForSinking = false);
268 
269   // Returns true if the condition of the select is highly predictable.
270   bool isSelectHighlyPredictable(const SelectLike SI);
271 
272   // Loop-level checks to determine if a non-predicated version (with branches)
273   // of the given loop is more profitable than its predicated version.
274   bool checkLoopHeuristics(const Loop *L, const CostInfo LoopDepth[2]);
275 
276   // Computes instruction and loop-critical-path costs for both the predicated
277   // and non-predicated version of the given loop.
278   bool computeLoopCosts(const Loop *L, const SelectGroups &SIGroups,
279                         DenseMap<const Instruction *, CostInfo> &InstCostMap,
280                         CostInfo *LoopCost);
281 
282   // Returns a set of all the select instructions in the given select groups.
283   SmallDenseMap<const Instruction *, SelectLike, 2>
284   getSImap(const SelectGroups &SIGroups);
285 
286   // Returns a map from select-like instructions to the corresponding select
287   // group.
288   SmallDenseMap<const Instruction *, const SelectGroup *, 2>
289   getSGmap(const SelectGroups &SIGroups);
290 
291   // Returns the latency cost of a given instruction.
292   std::optional<uint64_t> computeInstCost(const Instruction *I);
293 
294   // Returns the misprediction cost of a given select when converted to branch.
295   Scaled64 getMispredictionCost(const SelectLike SI, const Scaled64 CondCost);
296 
297   // Returns the cost of a branch when the prediction is correct.
298   Scaled64 getPredictedPathCost(Scaled64 TrueCost, Scaled64 FalseCost,
299                                 const SelectLike SI);
300 
301   // Returns true if the target architecture supports lowering a given select.
302   bool isSelectKindSupported(const SelectLike SI);
303 };
304 
305 class SelectOptimize : public FunctionPass {
306   SelectOptimizeImpl Impl;
307 
308 public:
309   static char ID;
310 
311   SelectOptimize() : FunctionPass(ID) {
312     initializeSelectOptimizePass(*PassRegistry::getPassRegistry());
313   }
314 
315   bool runOnFunction(Function &F) override {
316     return Impl.runOnFunction(F, *this);
317   }
318 
319   void getAnalysisUsage(AnalysisUsage &AU) const override {
320     AU.addRequired<ProfileSummaryInfoWrapperPass>();
321     AU.addRequired<TargetPassConfig>();
322     AU.addRequired<TargetTransformInfoWrapperPass>();
323     AU.addRequired<LoopInfoWrapperPass>();
324     AU.addRequired<BlockFrequencyInfoWrapperPass>();
325     AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
326   }
327 };
328 
329 } // namespace
330 
331 PreservedAnalyses SelectOptimizePass::run(Function &F,
332                                           FunctionAnalysisManager &FAM) {
333   SelectOptimizeImpl Impl(TM);
334   return Impl.run(F, FAM);
335 }
336 
337 char SelectOptimize::ID = 0;
338 
339 INITIALIZE_PASS_BEGIN(SelectOptimize, DEBUG_TYPE, "Optimize selects", false,
340                       false)
341 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
342 INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
343 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
344 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
345 INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
346 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
347 INITIALIZE_PASS_END(SelectOptimize, DEBUG_TYPE, "Optimize selects", false,
348                     false)
349 
350 FunctionPass *llvm::createSelectOptimizePass() { return new SelectOptimize(); }
351 
352 PreservedAnalyses SelectOptimizeImpl::run(Function &F,
353                                           FunctionAnalysisManager &FAM) {
354   TSI = TM->getSubtargetImpl(F);
355   TLI = TSI->getTargetLowering();
356 
357   // If none of the select types are supported then skip this pass.
358   // This is an optimization pass. Legality issues will be handled by
359   // instruction selection.
360   if (!TLI->isSelectSupported(TargetLowering::ScalarValSelect) &&
361       !TLI->isSelectSupported(TargetLowering::ScalarCondVectorVal) &&
362       !TLI->isSelectSupported(TargetLowering::VectorMaskSelect))
363     return PreservedAnalyses::all();
364 
365   TTI = &FAM.getResult<TargetIRAnalysis>(F);
366   if (!TTI->enableSelectOptimize())
367     return PreservedAnalyses::all();
368 
369   PSI = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F)
370             .getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
371   assert(PSI && "This pass requires module analysis pass `profile-summary`!");
372   BFI = &FAM.getResult<BlockFrequencyAnalysis>(F);
373 
374   // When optimizing for size, selects are preferable over branches.
375   if (llvm::shouldOptimizeForSize(&F, PSI, BFI))
376     return PreservedAnalyses::all();
377 
378   LI = &FAM.getResult<LoopAnalysis>(F);
379   ORE = &FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
380   TSchedModel.init(TSI);
381 
382   bool Changed = optimizeSelects(F);
383   return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
384 }
385 
386 bool SelectOptimizeImpl::runOnFunction(Function &F, Pass &P) {
387   TM = &P.getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
388   TSI = TM->getSubtargetImpl(F);
389   TLI = TSI->getTargetLowering();
390 
391   // If none of the select types are supported then skip this pass.
392   // This is an optimization pass. Legality issues will be handled by
393   // instruction selection.
394   if (!TLI->isSelectSupported(TargetLowering::ScalarValSelect) &&
395       !TLI->isSelectSupported(TargetLowering::ScalarCondVectorVal) &&
396       !TLI->isSelectSupported(TargetLowering::VectorMaskSelect))
397     return false;
398 
399   TTI = &P.getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
400 
401   if (!TTI->enableSelectOptimize())
402     return false;
403 
404   LI = &P.getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
405   BFI = &P.getAnalysis<BlockFrequencyInfoWrapperPass>().getBFI();
406   PSI = &P.getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
407   ORE = &P.getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
408   TSchedModel.init(TSI);
409 
410   // When optimizing for size, selects are preferable over branches.
411   if (llvm::shouldOptimizeForSize(&F, PSI, BFI))
412     return false;
413 
414   return optimizeSelects(F);
415 }
416 
417 bool SelectOptimizeImpl::optimizeSelects(Function &F) {
418   // Determine for which select groups it is profitable converting to branches.
419   SelectGroups ProfSIGroups;
420   // Base heuristics apply only to non-loops and outer loops.
421   optimizeSelectsBase(F, ProfSIGroups);
422   // Separate heuristics for inner-most loops.
423   optimizeSelectsInnerLoops(F, ProfSIGroups);
424 
425   // Convert to branches the select groups that were deemed
426   // profitable-to-convert.
427   convertProfitableSIGroups(ProfSIGroups);
428 
429   // Code modified if at least one select group was converted.
430   return !ProfSIGroups.empty();
431 }
432 
433 void SelectOptimizeImpl::optimizeSelectsBase(Function &F,
434                                              SelectGroups &ProfSIGroups) {
435   // Collect all the select groups.
436   SelectGroups SIGroups;
437   for (BasicBlock &BB : F) {
438     // Base heuristics apply only to non-loops and outer loops.
439     Loop *L = LI->getLoopFor(&BB);
440     if (L && L->isInnermost())
441       continue;
442     collectSelectGroups(BB, SIGroups);
443   }
444 
445   // Determine for which select groups it is profitable converting to branches.
446   findProfitableSIGroupsBase(SIGroups, ProfSIGroups);
447 }
448 
449 void SelectOptimizeImpl::optimizeSelectsInnerLoops(Function &F,
450                                                    SelectGroups &ProfSIGroups) {
451   SmallVector<Loop *, 4> Loops(LI->begin(), LI->end());
452   // Need to check size on each iteration as we accumulate child loops.
453   for (unsigned long i = 0; i < Loops.size(); ++i)
454     for (Loop *ChildL : Loops[i]->getSubLoops())
455       Loops.push_back(ChildL);
456 
457   for (Loop *L : Loops) {
458     if (!L->isInnermost())
459       continue;
460 
461     SelectGroups SIGroups;
462     for (BasicBlock *BB : L->getBlocks())
463       collectSelectGroups(*BB, SIGroups);
464 
465     findProfitableSIGroupsInnerLoops(L, SIGroups, ProfSIGroups);
466   }
467 }
468 
469 /// Returns optimised value on \p IsTrue branch. For SelectInst that would be
470 /// either True or False value. For (BinaryOperator) instructions, where the
471 /// condition may be skipped, the operation will use a non-conditional operand.
472 /// For example, for `or(V,zext(cond))` this function would return V.
473 /// However, if the conditional operand on \p IsTrue branch matters, we create a
474 /// clone of instruction at the end of that branch \p B and replace the
475 /// condition operand with a constant.
476 ///
477 /// Also /p OptSelects contains previously optimised select-like instructions.
478 /// If the current value uses one of the optimised values, we can optimise it
479 /// further by replacing it with the corresponding value on the given branch
480 static Value *getTrueOrFalseValue(
481     SelectOptimizeImpl::SelectLike &SI, bool isTrue,
482     SmallDenseMap<Instruction *, std::pair<Value *, Value *>, 2> &OptSelects,
483     BasicBlock *B) {
484   Value *V = isTrue ? SI.getTrueValue() : SI.getFalseValue();
485   if (V) {
486     auto *IV = dyn_cast<Instruction>(V);
487     if (IV && OptSelects.count(IV))
488       return isTrue ? OptSelects[IV].first : OptSelects[IV].second;
489     return V;
490   }
491 
492   auto *BO = cast<BinaryOperator>(SI.getI());
493   assert((BO->getOpcode() == Instruction::Add ||
494           BO->getOpcode() == Instruction::Or ||
495           BO->getOpcode() == Instruction::Sub) &&
496          "Only currently handling Add, Or and Sub binary operators.");
497 
498   auto *CBO = BO->clone();
499   auto CondIdx = SI.getConditionOpIndex();
500   auto *AuxI = cast<Instruction>(CBO->getOperand(CondIdx));
501   if (isa<ZExtInst>(AuxI) || isa<LShrOperator>(AuxI)) {
502     CBO->setOperand(CondIdx, ConstantInt::get(CBO->getType(), 1));
503   } else {
504     assert(isa<AShrOperator>(AuxI) && "Unexpected opcode");
505     CBO->setOperand(CondIdx, ConstantInt::get(CBO->getType(), -1));
506   }
507 
508   unsigned OtherIdx = 1 - CondIdx;
509   if (auto *IV = dyn_cast<Instruction>(CBO->getOperand(OtherIdx))) {
510     if (OptSelects.count(IV))
511       CBO->setOperand(OtherIdx,
512                       isTrue ? OptSelects[IV].first : OptSelects[IV].second);
513   }
514   CBO->insertBefore(B->getTerminator());
515   return CBO;
516 }
517 
518 void SelectOptimizeImpl::convertProfitableSIGroups(SelectGroups &ProfSIGroups) {
519   for (SelectGroup &ASI : ProfSIGroups) {
520     // The code transformation here is a modified version of the sinking
521     // transformation in CodeGenPrepare::optimizeSelectInst with a more
522     // aggressive strategy of which instructions to sink.
523     //
524     // TODO: eliminate the redundancy of logic transforming selects to branches
525     // by removing CodeGenPrepare::optimizeSelectInst and optimizing here
526     // selects for all cases (with and without profile information).
527 
528     // Transform a sequence like this:
529     //    start:
530     //       %cmp = cmp uge i32 %a, %b
531     //       %sel = select i1 %cmp, i32 %c, i32 %d
532     //
533     // Into:
534     //    start:
535     //       %cmp = cmp uge i32 %a, %b
536     //       %cmp.frozen = freeze %cmp
537     //       br i1 %cmp.frozen, label %select.true, label %select.false
538     //    select.true:
539     //       br label %select.end
540     //    select.false:
541     //       br label %select.end
542     //    select.end:
543     //       %sel = phi i32 [ %c, %select.true ], [ %d, %select.false ]
544     //
545     // %cmp should be frozen, otherwise it may introduce undefined behavior.
546     // In addition, we may sink instructions that produce %c or %d into the
547     // destination(s) of the new branch.
548     // If the true or false blocks do not contain a sunken instruction, that
549     // block and its branch may be optimized away. In that case, one side of the
550     // first branch will point directly to select.end, and the corresponding PHI
551     // predecessor block will be the start block.
552 
553     // Find all the instructions that can be soundly sunk to the true/false
554     // blocks. These are instructions that are computed solely for producing the
555     // operands of the select instructions in the group and can be sunk without
556     // breaking the semantics of the LLVM IR (e.g., cannot sink instructions
557     // with side effects).
558     SmallVector<std::stack<Instruction *>, 2> TrueSlices, FalseSlices;
559     typedef std::stack<Instruction *>::size_type StackSizeType;
560     StackSizeType maxTrueSliceLen = 0, maxFalseSliceLen = 0;
561     for (SelectLike &SI : ASI.Selects) {
562       if (!isa<SelectInst>(SI.getI()))
563         continue;
564       // For each select, compute the sinkable dependence chains of the true and
565       // false operands.
566       if (auto *TI = dyn_cast_or_null<Instruction>(SI.getTrueValue())) {
567         std::stack<Instruction *> TrueSlice;
568         getExclBackwardsSlice(TI, TrueSlice, SI.getI(), true);
569         maxTrueSliceLen = std::max(maxTrueSliceLen, TrueSlice.size());
570         TrueSlices.push_back(TrueSlice);
571       }
572       if (auto *FI = dyn_cast_or_null<Instruction>(SI.getFalseValue())) {
573         if (isa<SelectInst>(SI.getI()) || !FI->hasOneUse()) {
574           std::stack<Instruction *> FalseSlice;
575           getExclBackwardsSlice(FI, FalseSlice, SI.getI(), true);
576           maxFalseSliceLen = std::max(maxFalseSliceLen, FalseSlice.size());
577           FalseSlices.push_back(FalseSlice);
578         }
579       }
580     }
581     // In the case of multiple select instructions in the same group, the order
582     // of non-dependent instructions (instructions of different dependence
583     // slices) in the true/false blocks appears to affect performance.
584     // Interleaving the slices seems to experimentally be the optimal approach.
585     // This interleaving scheduling allows for more ILP (with a natural downside
586     // of increasing a bit register pressure) compared to a simple ordering of
587     // one whole chain after another. One would expect that this ordering would
588     // not matter since the scheduling in the backend of the compiler  would
589     // take care of it, but apparently the scheduler fails to deliver optimal
590     // ILP with a naive ordering here.
591     SmallVector<Instruction *, 2> TrueSlicesInterleaved, FalseSlicesInterleaved;
592     for (StackSizeType IS = 0; IS < maxTrueSliceLen; ++IS) {
593       for (auto &S : TrueSlices) {
594         if (!S.empty()) {
595           TrueSlicesInterleaved.push_back(S.top());
596           S.pop();
597         }
598       }
599     }
600     for (StackSizeType IS = 0; IS < maxFalseSliceLen; ++IS) {
601       for (auto &S : FalseSlices) {
602         if (!S.empty()) {
603           FalseSlicesInterleaved.push_back(S.top());
604           S.pop();
605         }
606       }
607     }
608 
609     // We split the block containing the select(s) into two blocks.
610     SelectLike &SI = ASI.Selects.front();
611     SelectLike &LastSI = ASI.Selects.back();
612     BasicBlock *StartBlock = SI.getI()->getParent();
613     BasicBlock::iterator SplitPt = ++(BasicBlock::iterator(LastSI.getI()));
614     // With RemoveDIs turned off, SplitPt can be a dbg.* intrinsic. With
615     // RemoveDIs turned on, SplitPt would instead point to the next
616     // instruction. To match existing dbg.* intrinsic behaviour with RemoveDIs,
617     // tell splitBasicBlock that we want to include any DbgVariableRecords
618     // attached to SplitPt in the splice.
619     SplitPt.setHeadBit(true);
620     BasicBlock *EndBlock = StartBlock->splitBasicBlock(SplitPt, "select.end");
621     BFI->setBlockFreq(EndBlock, BFI->getBlockFreq(StartBlock));
622     // Delete the unconditional branch that was just created by the split.
623     StartBlock->getTerminator()->eraseFromParent();
624 
625     // Move any debug/pseudo and auxiliary instructions that were in-between the
626     // select group to the newly-created end block.
627     SmallVector<Instruction *, 2> SinkInstrs;
628     auto DIt = SI.getI()->getIterator();
629     auto NIt = ASI.Selects.begin();
630     while (&*DIt != LastSI.getI()) {
631       if (NIt != ASI.Selects.end() && &*DIt == NIt->getI())
632         ++NIt;
633       else
634         SinkInstrs.push_back(&*DIt);
635       DIt++;
636     }
637     auto InsertionPoint = EndBlock->getFirstInsertionPt();
638     for (auto *DI : SinkInstrs)
639       DI->moveBeforePreserving(&*InsertionPoint);
640 
641     // Duplicate implementation for DbgRecords, the non-instruction debug-info
642     // format. Helper lambda for moving DbgRecords to the end block.
643     auto TransferDbgRecords = [&](Instruction &I) {
644       for (auto &DbgRecord :
645            llvm::make_early_inc_range(I.getDbgRecordRange())) {
646         DbgRecord.removeFromParent();
647         EndBlock->insertDbgRecordBefore(&DbgRecord,
648                                         EndBlock->getFirstInsertionPt());
649       }
650     };
651 
652     // Iterate over all instructions in between SI and LastSI, not including
653     // SI itself. These are all the variable assignments that happen "in the
654     // middle" of the select group.
655     auto R = make_range(std::next(SI.getI()->getIterator()),
656                         std::next(LastSI.getI()->getIterator()));
657     llvm::for_each(R, TransferDbgRecords);
658 
659     // These are the new basic blocks for the conditional branch.
660     // At least one will become an actual new basic block.
661     BasicBlock *TrueBlock = nullptr, *FalseBlock = nullptr;
662     BranchInst *TrueBranch = nullptr, *FalseBranch = nullptr;
663     // Checks if select-like instruction would materialise on the given branch
664     auto HasSelectLike = [](SelectGroup &SG, bool IsTrue) {
665       for (auto &SL : SG.Selects) {
666         if ((IsTrue ? SL.getTrueValue() : SL.getFalseValue()) == nullptr)
667           return true;
668       }
669       return false;
670     };
671     if (!TrueSlicesInterleaved.empty() || HasSelectLike(ASI, true)) {
672       TrueBlock = BasicBlock::Create(EndBlock->getContext(), "select.true.sink",
673                                      EndBlock->getParent(), EndBlock);
674       TrueBranch = BranchInst::Create(EndBlock, TrueBlock);
675       TrueBranch->setDebugLoc(LastSI.getI()->getDebugLoc());
676       for (Instruction *TrueInst : TrueSlicesInterleaved)
677         TrueInst->moveBefore(TrueBranch);
678     }
679     if (!FalseSlicesInterleaved.empty() || HasSelectLike(ASI, false)) {
680       FalseBlock =
681           BasicBlock::Create(EndBlock->getContext(), "select.false.sink",
682                              EndBlock->getParent(), EndBlock);
683       FalseBranch = BranchInst::Create(EndBlock, FalseBlock);
684       FalseBranch->setDebugLoc(LastSI.getI()->getDebugLoc());
685       for (Instruction *FalseInst : FalseSlicesInterleaved)
686         FalseInst->moveBefore(FalseBranch);
687     }
688     // If there was nothing to sink, then arbitrarily choose the 'false' side
689     // for a new input value to the PHI.
690     if (TrueBlock == FalseBlock) {
691       assert(TrueBlock == nullptr &&
692              "Unexpected basic block transform while optimizing select");
693 
694       FalseBlock = BasicBlock::Create(StartBlock->getContext(), "select.false",
695                                       EndBlock->getParent(), EndBlock);
696       auto *FalseBranch = BranchInst::Create(EndBlock, FalseBlock);
697       FalseBranch->setDebugLoc(SI.getI()->getDebugLoc());
698     }
699 
700     // Insert the real conditional branch based on the original condition.
701     // If we did not create a new block for one of the 'true' or 'false' paths
702     // of the condition, it means that side of the branch goes to the end block
703     // directly and the path originates from the start block from the point of
704     // view of the new PHI.
705     BasicBlock *TT, *FT;
706     if (TrueBlock == nullptr) {
707       TT = EndBlock;
708       FT = FalseBlock;
709       TrueBlock = StartBlock;
710     } else if (FalseBlock == nullptr) {
711       TT = TrueBlock;
712       FT = EndBlock;
713       FalseBlock = StartBlock;
714     } else {
715       TT = TrueBlock;
716       FT = FalseBlock;
717     }
718     IRBuilder<> IB(SI.getI());
719     auto *CondFr =
720         IB.CreateFreeze(ASI.Condition, ASI.Condition->getName() + ".frozen");
721 
722     SmallDenseMap<Instruction *, std::pair<Value *, Value *>, 2> INS;
723 
724     // Use reverse iterator because later select may use the value of the
725     // earlier select, and we need to propagate value through earlier select
726     // to get the PHI operand.
727     InsertionPoint = EndBlock->begin();
728     for (SelectLike &SI : ASI.Selects) {
729       // The select itself is replaced with a PHI Node.
730       PHINode *PN = PHINode::Create(SI.getType(), 2, "");
731       PN->insertBefore(InsertionPoint);
732       PN->takeName(SI.getI());
733       // Current instruction might be a condition of some other group, so we
734       // need to replace it there to avoid dangling pointer
735       if (PN->getType()->isIntegerTy(1)) {
736         for (auto &SG : ProfSIGroups) {
737           if (SG.Condition == SI.getI())
738             SG.Condition = PN;
739         }
740       }
741       SI.getI()->replaceAllUsesWith(PN);
742       auto *TV = getTrueOrFalseValue(SI, true, INS, TrueBlock);
743       auto *FV = getTrueOrFalseValue(SI, false, INS, FalseBlock);
744       INS[PN] = {TV, FV};
745       PN->addIncoming(TV, TrueBlock);
746       PN->addIncoming(FV, FalseBlock);
747       PN->setDebugLoc(SI.getI()->getDebugLoc());
748       ++NumSelectsConverted;
749     }
750     IB.CreateCondBr(CondFr, TT, FT, SI.getI());
751 
752     // Remove the old select instructions, now that they are not longer used.
753     for (SelectLike &SI : ASI.Selects)
754       SI.getI()->eraseFromParent();
755   }
756 }
757 
758 void SelectOptimizeImpl::collectSelectGroups(BasicBlock &BB,
759                                              SelectGroups &SIGroups) {
760   // Represents something that can be considered as select instruction.
761   // Auxiliary instruction are instructions that depends on a condition and have
762   // zero or some constant value on True/False branch, such as:
763   // * ZExt(1bit)
764   // * Not(1bit)
765   // * A(L)Shr(Val), ValBitSize - 1, where there is a condition like `Val <= 0`
766   // earlier in the BB. For conditions that check the sign of the Val compiler
767   // may generate shifts instead of ZExt/SExt.
768   struct SelectLikeInfo {
769     Value *Cond;
770     bool IsAuxiliary;
771     bool IsInverted;
772     unsigned ConditionIdx;
773   };
774 
775   DenseMap<Value *, SelectLikeInfo> SelectInfo;
776   // Keeps visited comparisons to help identify AShr/LShr variants of auxiliary
777   // instructions.
778   SmallSetVector<CmpInst *, 4> SeenCmp;
779 
780   // Check if the instruction is SelectLike or might be part of SelectLike
781   // expression, put information into SelectInfo and return the iterator to the
782   // inserted position.
783   auto ProcessSelectInfo = [&SelectInfo, &SeenCmp](Instruction *I) {
784     if (auto *Cmp = dyn_cast<CmpInst>(I)) {
785       SeenCmp.insert(Cmp);
786       return SelectInfo.end();
787     }
788 
789     Value *Cond;
790     if (match(I, m_OneUse(m_ZExt(m_Value(Cond)))) &&
791         Cond->getType()->isIntegerTy(1)) {
792       bool Inverted = match(Cond, m_Not(m_Value(Cond)));
793       return SelectInfo.insert({I, {Cond, true, Inverted, 0}}).first;
794     }
795 
796     if (match(I, m_Not(m_Value(Cond)))) {
797       return SelectInfo.insert({I, {Cond, true, true, 0}}).first;
798     }
799 
800     // Select instruction are what we are usually looking for.
801     if (match(I, m_Select(m_Value(Cond), m_Value(), m_Value()))) {
802       bool Inverted = match(Cond, m_Not(m_Value(Cond)));
803       return SelectInfo.insert({I, {Cond, false, Inverted, 0}}).first;
804     }
805     Value *Val;
806     ConstantInt *Shift;
807     if (match(I, m_Shr(m_Value(Val), m_ConstantInt(Shift))) &&
808         I->getType()->getIntegerBitWidth() == Shift->getZExtValue() + 1) {
809       for (auto *CmpI : SeenCmp) {
810         auto Pred = CmpI->getPredicate();
811         if (Val != CmpI->getOperand(0))
812           continue;
813         if ((Pred == CmpInst::ICMP_SGT &&
814              match(CmpI->getOperand(1), m_ConstantInt<-1>())) ||
815             (Pred == CmpInst::ICMP_SGE &&
816              match(CmpI->getOperand(1), m_Zero())) ||
817             (Pred == CmpInst::ICMP_SLT &&
818              match(CmpI->getOperand(1), m_Zero())) ||
819             (Pred == CmpInst::ICMP_SLE &&
820              match(CmpI->getOperand(1), m_ConstantInt<-1>()))) {
821           bool Inverted =
822               Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
823           return SelectInfo.insert({I, {CmpI, true, Inverted, 0}}).first;
824         }
825       }
826       return SelectInfo.end();
827     }
828 
829     // An BinOp(Aux(X), Y) can also be treated like a select, with condition X
830     // and values Y|1 and Y.
831     // `Aux` can be either `ZExt(1bit)` or `XShr(Val), ValBitSize - 1`
832     // `BinOp` can be Add, Sub, Or
833     Value *X;
834     auto MatchZExtPattern = m_c_BinOp(m_Value(), m_OneUse(m_ZExt(m_Value(X))));
835     auto MatchShiftPattern =
836         m_c_BinOp(m_Value(), m_OneUse(m_Shr(m_Value(X), m_ConstantInt(Shift))));
837 
838     // This check is unnecessary, but it prevents costly access to the
839     // SelectInfo map.
840     if ((match(I, MatchZExtPattern) && X->getType()->isIntegerTy(1)) ||
841         (match(I, MatchShiftPattern) &&
842          X->getType()->getIntegerBitWidth() == Shift->getZExtValue() + 1)) {
843       if (I->getOpcode() != Instruction::Add &&
844           I->getOpcode() != Instruction::Sub &&
845           I->getOpcode() != Instruction::Or)
846         return SelectInfo.end();
847 
848       if (I->getOpcode() == Instruction::Or && I->getType()->isIntegerTy(1))
849         return SelectInfo.end();
850 
851       // Iterate through operands and find dependant on recognised sign
852       // extending auxiliary select-like instructions. The operand index does
853       // not matter for Add and Or. However, for Sub, we can only safely
854       // transform when the operand is second.
855       unsigned Idx = I->getOpcode() == Instruction::Sub ? 1 : 0;
856       for (; Idx < 2; Idx++) {
857         auto *Op = I->getOperand(Idx);
858         auto It = SelectInfo.find(Op);
859         if (It != SelectInfo.end() && It->second.IsAuxiliary) {
860           Cond = It->second.Cond;
861           bool Inverted = It->second.IsInverted;
862           return SelectInfo.insert({I, {Cond, false, Inverted, Idx}}).first;
863         }
864       }
865     }
866     return SelectInfo.end();
867   };
868 
869   bool AlreadyProcessed = false;
870   BasicBlock::iterator BBIt = BB.begin();
871   DenseMap<Value *, SelectLikeInfo>::iterator It;
872   while (BBIt != BB.end()) {
873     Instruction *I = &*BBIt++;
874     if (I->isDebugOrPseudoInst())
875       continue;
876 
877     if (!AlreadyProcessed)
878       It = ProcessSelectInfo(I);
879     else
880       AlreadyProcessed = false;
881 
882     if (It == SelectInfo.end() || It->second.IsAuxiliary)
883       continue;
884 
885     if (!TTI->shouldTreatInstructionLikeSelect(I))
886       continue;
887 
888     Value *Cond = It->second.Cond;
889     // Vector conditions are not supported.
890     if (!Cond->getType()->isIntegerTy(1))
891       continue;
892 
893     SelectGroup SIGroup = {Cond, {}};
894     SIGroup.Selects.emplace_back(I, It->second.IsInverted,
895                                  It->second.ConditionIdx);
896 
897     // If the select type is not supported, no point optimizing it.
898     // Instruction selection will take care of it.
899     if (!isSelectKindSupported(SIGroup.Selects.front()))
900       continue;
901 
902     while (BBIt != BB.end()) {
903       Instruction *NI = &*BBIt;
904       // Debug/pseudo instructions should be skipped and not prevent the
905       // formation of a select group.
906       if (NI->isDebugOrPseudoInst()) {
907         ++BBIt;
908         continue;
909       }
910 
911       It = ProcessSelectInfo(NI);
912       if (It == SelectInfo.end()) {
913         AlreadyProcessed = true;
914         break;
915       }
916 
917       // Auxiliary with same condition
918       auto [CurrCond, IsAux, IsRev, CondIdx] = It->second;
919       if (Cond != CurrCond) {
920         AlreadyProcessed = true;
921         break;
922       }
923 
924       if (!IsAux)
925         SIGroup.Selects.emplace_back(NI, IsRev, CondIdx);
926       ++BBIt;
927     }
928     LLVM_DEBUG({
929       dbgs() << "New Select group (" << SIGroup.Selects.size() << ") with\n";
930       for (auto &SI : SIGroup.Selects)
931         dbgs() << "  " << *SI.getI() << "\n";
932     });
933 
934     SIGroups.push_back(SIGroup);
935   }
936 }
937 
938 void SelectOptimizeImpl::findProfitableSIGroupsBase(
939     SelectGroups &SIGroups, SelectGroups &ProfSIGroups) {
940   for (SelectGroup &ASI : SIGroups) {
941     ++NumSelectOptAnalyzed;
942     if (isConvertToBranchProfitableBase(ASI))
943       ProfSIGroups.push_back(ASI);
944   }
945 }
946 
947 static void EmitAndPrintRemark(OptimizationRemarkEmitter *ORE,
948                                DiagnosticInfoOptimizationBase &Rem) {
949   LLVM_DEBUG(dbgs() << Rem.getMsg() << "\n");
950   ORE->emit(Rem);
951 }
952 
953 void SelectOptimizeImpl::findProfitableSIGroupsInnerLoops(
954     const Loop *L, SelectGroups &SIGroups, SelectGroups &ProfSIGroups) {
955   NumSelectOptAnalyzed += SIGroups.size();
956   // For each select group in an inner-most loop,
957   // a branch is more preferable than a select/conditional-move if:
958   // i) conversion to branches for all the select groups of the loop satisfies
959   //    loop-level heuristics including reducing the loop's critical path by
960   //    some threshold (see SelectOptimizeImpl::checkLoopHeuristics); and
961   // ii) the total cost of the select group is cheaper with a branch compared
962   //     to its predicated version. The cost is in terms of latency and the cost
963   //     of a select group is the cost of its most expensive select instruction
964   //     (assuming infinite resources and thus fully leveraging available ILP).
965 
966   DenseMap<const Instruction *, CostInfo> InstCostMap;
967   CostInfo LoopCost[2] = {{Scaled64::getZero(), Scaled64::getZero()},
968                           {Scaled64::getZero(), Scaled64::getZero()}};
969   if (!computeLoopCosts(L, SIGroups, InstCostMap, LoopCost) ||
970       !checkLoopHeuristics(L, LoopCost)) {
971     return;
972   }
973 
974   for (SelectGroup &ASI : SIGroups) {
975     // Assuming infinite resources, the cost of a group of instructions is the
976     // cost of the most expensive instruction of the group.
977     Scaled64 SelectCost = Scaled64::getZero(), BranchCost = Scaled64::getZero();
978     for (SelectLike &SI : ASI.Selects) {
979       SelectCost = std::max(SelectCost, InstCostMap[SI.getI()].PredCost);
980       BranchCost = std::max(BranchCost, InstCostMap[SI.getI()].NonPredCost);
981     }
982     if (BranchCost < SelectCost) {
983       OptimizationRemark OR(DEBUG_TYPE, "SelectOpti",
984                             ASI.Selects.front().getI());
985       OR << "Profitable to convert to branch (loop analysis). BranchCost="
986          << BranchCost.toString() << ", SelectCost=" << SelectCost.toString()
987          << ". ";
988       EmitAndPrintRemark(ORE, OR);
989       ++NumSelectConvertedLoop;
990       ProfSIGroups.push_back(ASI);
991     } else {
992       OptimizationRemarkMissed ORmiss(DEBUG_TYPE, "SelectOpti",
993                                       ASI.Selects.front().getI());
994       ORmiss << "Select is more profitable (loop analysis). BranchCost="
995              << BranchCost.toString()
996              << ", SelectCost=" << SelectCost.toString() << ". ";
997       EmitAndPrintRemark(ORE, ORmiss);
998     }
999   }
1000 }
1001 
1002 bool SelectOptimizeImpl::isConvertToBranchProfitableBase(
1003     const SelectGroup &ASI) {
1004   const SelectLike &SI = ASI.Selects.front();
1005   LLVM_DEBUG(dbgs() << "Analyzing select group containing " << *SI.getI()
1006                     << "\n");
1007   OptimizationRemark OR(DEBUG_TYPE, "SelectOpti", SI.getI());
1008   OptimizationRemarkMissed ORmiss(DEBUG_TYPE, "SelectOpti", SI.getI());
1009 
1010   // Skip cold basic blocks. Better to optimize for size for cold blocks.
1011   if (PSI->isColdBlock(SI.getI()->getParent(), BFI)) {
1012     ++NumSelectColdBB;
1013     ORmiss << "Not converted to branch because of cold basic block. ";
1014     EmitAndPrintRemark(ORE, ORmiss);
1015     return false;
1016   }
1017 
1018   // If unpredictable, branch form is less profitable.
1019   if (SI.getI()->getMetadata(LLVMContext::MD_unpredictable)) {
1020     ++NumSelectUnPred;
1021     ORmiss << "Not converted to branch because of unpredictable branch. ";
1022     EmitAndPrintRemark(ORE, ORmiss);
1023     return false;
1024   }
1025 
1026   // If highly predictable, branch form is more profitable, unless a
1027   // predictable select is inexpensive in the target architecture.
1028   if (isSelectHighlyPredictable(SI) && TLI->isPredictableSelectExpensive()) {
1029     ++NumSelectConvertedHighPred;
1030     OR << "Converted to branch because of highly predictable branch. ";
1031     EmitAndPrintRemark(ORE, OR);
1032     return true;
1033   }
1034 
1035   // Look for expensive instructions in the cold operand's (if any) dependence
1036   // slice of any of the selects in the group.
1037   if (hasExpensiveColdOperand(ASI)) {
1038     ++NumSelectConvertedExpColdOperand;
1039     OR << "Converted to branch because of expensive cold operand.";
1040     EmitAndPrintRemark(ORE, OR);
1041     return true;
1042   }
1043 
1044   ORmiss << "Not profitable to convert to branch (base heuristic).";
1045   EmitAndPrintRemark(ORE, ORmiss);
1046   return false;
1047 }
1048 
1049 static InstructionCost divideNearest(InstructionCost Numerator,
1050                                      uint64_t Denominator) {
1051   return (Numerator + (Denominator / 2)) / Denominator;
1052 }
1053 
1054 static bool extractBranchWeights(const SelectOptimizeImpl::SelectLike SI,
1055                                  uint64_t &TrueVal, uint64_t &FalseVal) {
1056   if (isa<SelectInst>(SI.getI()))
1057     return extractBranchWeights(*SI.getI(), TrueVal, FalseVal);
1058   return false;
1059 }
1060 
1061 bool SelectOptimizeImpl::hasExpensiveColdOperand(const SelectGroup &ASI) {
1062   bool ColdOperand = false;
1063   uint64_t TrueWeight, FalseWeight, TotalWeight;
1064   if (extractBranchWeights(ASI.Selects.front(), TrueWeight, FalseWeight)) {
1065     uint64_t MinWeight = std::min(TrueWeight, FalseWeight);
1066     TotalWeight = TrueWeight + FalseWeight;
1067     // Is there a path with frequency <ColdOperandThreshold% (default:20%) ?
1068     ColdOperand = TotalWeight * ColdOperandThreshold > 100 * MinWeight;
1069   } else if (PSI->hasProfileSummary()) {
1070     OptimizationRemarkMissed ORmiss(DEBUG_TYPE, "SelectOpti",
1071                                     ASI.Selects.front().getI());
1072     ORmiss << "Profile data available but missing branch-weights metadata for "
1073               "select instruction. ";
1074     EmitAndPrintRemark(ORE, ORmiss);
1075   }
1076   if (!ColdOperand)
1077     return false;
1078   // Check if the cold path's dependence slice is expensive for any of the
1079   // selects of the group.
1080   for (SelectLike SI : ASI.Selects) {
1081     Instruction *ColdI = nullptr;
1082     uint64_t HotWeight;
1083     if (TrueWeight < FalseWeight) {
1084       ColdI = dyn_cast_or_null<Instruction>(SI.getTrueValue());
1085       HotWeight = FalseWeight;
1086     } else {
1087       ColdI = dyn_cast_or_null<Instruction>(SI.getFalseValue());
1088       HotWeight = TrueWeight;
1089     }
1090     if (ColdI) {
1091       std::stack<Instruction *> ColdSlice;
1092       getExclBackwardsSlice(ColdI, ColdSlice, SI.getI());
1093       InstructionCost SliceCost = 0;
1094       while (!ColdSlice.empty()) {
1095         SliceCost += TTI->getInstructionCost(ColdSlice.top(),
1096                                              TargetTransformInfo::TCK_Latency);
1097         ColdSlice.pop();
1098       }
1099       // The colder the cold value operand of the select is the more expensive
1100       // the cmov becomes for computing the cold value operand every time. Thus,
1101       // the colder the cold operand is the more its cost counts.
1102       // Get nearest integer cost adjusted for coldness.
1103       InstructionCost AdjSliceCost =
1104           divideNearest(SliceCost * HotWeight, TotalWeight);
1105       if (AdjSliceCost >=
1106           ColdOperandMaxCostMultiplier * TargetTransformInfo::TCC_Expensive)
1107         return true;
1108     }
1109   }
1110   return false;
1111 }
1112 
1113 // Check if it is safe to move LoadI next to the SI.
1114 // Conservatively assume it is safe only if there is no instruction
1115 // modifying memory in-between the load and the select instruction.
1116 static bool isSafeToSinkLoad(Instruction *LoadI, Instruction *SI) {
1117   // Assume loads from different basic blocks are unsafe to move.
1118   if (LoadI->getParent() != SI->getParent())
1119     return false;
1120   auto It = LoadI->getIterator();
1121   while (&*It != SI) {
1122     if (It->mayWriteToMemory())
1123       return false;
1124     It++;
1125   }
1126   return true;
1127 }
1128 
1129 // For a given source instruction, collect its backwards dependence slice
1130 // consisting of instructions exclusively computed for the purpose of producing
1131 // the operands of the source instruction. As an approximation
1132 // (sufficiently-accurate in practice), we populate this set with the
1133 // instructions of the backwards dependence slice that only have one-use and
1134 // form an one-use chain that leads to the source instruction.
1135 void SelectOptimizeImpl::getExclBackwardsSlice(Instruction *I,
1136                                                std::stack<Instruction *> &Slice,
1137                                                Instruction *SI,
1138                                                bool ForSinking) {
1139   SmallPtrSet<Instruction *, 2> Visited;
1140   std::queue<Instruction *> Worklist;
1141   Worklist.push(I);
1142   while (!Worklist.empty()) {
1143     Instruction *II = Worklist.front();
1144     Worklist.pop();
1145 
1146     // Avoid cycles.
1147     if (!Visited.insert(II).second)
1148       continue;
1149 
1150     if (!II->hasOneUse())
1151       continue;
1152 
1153     // Cannot soundly sink instructions with side-effects.
1154     // Terminator or phi instructions cannot be sunk.
1155     // Avoid sinking other select instructions (should be handled separetely).
1156     if (ForSinking && (II->isTerminator() || II->mayHaveSideEffects() ||
1157                        isa<SelectInst>(II) || isa<PHINode>(II)))
1158       continue;
1159 
1160     // Avoid sinking loads in order not to skip state-modifying instructions,
1161     // that may alias with the loaded address.
1162     // Only allow sinking of loads within the same basic block that are
1163     // conservatively proven to be safe.
1164     if (ForSinking && II->mayReadFromMemory() && !isSafeToSinkLoad(II, SI))
1165       continue;
1166 
1167     // Avoid considering instructions with less frequency than the source
1168     // instruction (i.e., avoid colder code regions of the dependence slice).
1169     if (BFI->getBlockFreq(II->getParent()) < BFI->getBlockFreq(I->getParent()))
1170       continue;
1171 
1172     // Eligible one-use instruction added to the dependence slice.
1173     Slice.push(II);
1174 
1175     // Explore all the operands of the current instruction to expand the slice.
1176     for (Value *Op : II->operand_values())
1177       if (auto *OpI = dyn_cast<Instruction>(Op))
1178         Worklist.push(OpI);
1179   }
1180 }
1181 
1182 bool SelectOptimizeImpl::isSelectHighlyPredictable(const SelectLike SI) {
1183   uint64_t TrueWeight, FalseWeight;
1184   if (extractBranchWeights(SI, TrueWeight, FalseWeight)) {
1185     uint64_t Max = std::max(TrueWeight, FalseWeight);
1186     uint64_t Sum = TrueWeight + FalseWeight;
1187     if (Sum != 0) {
1188       auto Probability = BranchProbability::getBranchProbability(Max, Sum);
1189       if (Probability > TTI->getPredictableBranchThreshold())
1190         return true;
1191     }
1192   }
1193   return false;
1194 }
1195 
1196 bool SelectOptimizeImpl::checkLoopHeuristics(const Loop *L,
1197                                              const CostInfo LoopCost[2]) {
1198   // Loop-level checks to determine if a non-predicated version (with branches)
1199   // of the loop is more profitable than its predicated version.
1200 
1201   if (DisableLoopLevelHeuristics)
1202     return true;
1203 
1204   OptimizationRemarkMissed ORmissL(DEBUG_TYPE, "SelectOpti",
1205                                    L->getHeader()->getFirstNonPHI());
1206 
1207   if (LoopCost[0].NonPredCost > LoopCost[0].PredCost ||
1208       LoopCost[1].NonPredCost >= LoopCost[1].PredCost) {
1209     ORmissL << "No select conversion in the loop due to no reduction of loop's "
1210                "critical path. ";
1211     EmitAndPrintRemark(ORE, ORmissL);
1212     return false;
1213   }
1214 
1215   Scaled64 Gain[2] = {LoopCost[0].PredCost - LoopCost[0].NonPredCost,
1216                       LoopCost[1].PredCost - LoopCost[1].NonPredCost};
1217 
1218   // Profitably converting to branches need to reduce the loop's critical path
1219   // by at least some threshold (absolute gain of GainCycleThreshold cycles and
1220   // relative gain of 12.5%).
1221   if (Gain[1] < Scaled64::get(GainCycleThreshold) ||
1222       Gain[1] * Scaled64::get(GainRelativeThreshold) < LoopCost[1].PredCost) {
1223     Scaled64 RelativeGain = Scaled64::get(100) * Gain[1] / LoopCost[1].PredCost;
1224     ORmissL << "No select conversion in the loop due to small reduction of "
1225                "loop's critical path. Gain="
1226             << Gain[1].toString()
1227             << ", RelativeGain=" << RelativeGain.toString() << "%. ";
1228     EmitAndPrintRemark(ORE, ORmissL);
1229     return false;
1230   }
1231 
1232   // If the loop's critical path involves loop-carried dependences, the gradient
1233   // of the gain needs to be at least GainGradientThreshold% (defaults to 25%).
1234   // This check ensures that the latency reduction for the loop's critical path
1235   // keeps decreasing with sufficient rate beyond the two analyzed loop
1236   // iterations.
1237   if (Gain[1] > Gain[0]) {
1238     Scaled64 GradientGain = Scaled64::get(100) * (Gain[1] - Gain[0]) /
1239                             (LoopCost[1].PredCost - LoopCost[0].PredCost);
1240     if (GradientGain < Scaled64::get(GainGradientThreshold)) {
1241       ORmissL << "No select conversion in the loop due to small gradient gain. "
1242                  "GradientGain="
1243               << GradientGain.toString() << "%. ";
1244       EmitAndPrintRemark(ORE, ORmissL);
1245       return false;
1246     }
1247   }
1248   // If the gain decreases it is not profitable to convert.
1249   else if (Gain[1] < Gain[0]) {
1250     ORmissL
1251         << "No select conversion in the loop due to negative gradient gain. ";
1252     EmitAndPrintRemark(ORE, ORmissL);
1253     return false;
1254   }
1255 
1256   // Non-predicated version of the loop is more profitable than its
1257   // predicated version.
1258   return true;
1259 }
1260 
1261 // Computes instruction and loop-critical-path costs for both the predicated
1262 // and non-predicated version of the given loop.
1263 // Returns false if unable to compute these costs due to invalid cost of loop
1264 // instruction(s).
1265 bool SelectOptimizeImpl::computeLoopCosts(
1266     const Loop *L, const SelectGroups &SIGroups,
1267     DenseMap<const Instruction *, CostInfo> &InstCostMap, CostInfo *LoopCost) {
1268   LLVM_DEBUG(dbgs() << "Calculating Latency / IPredCost / INonPredCost of loop "
1269                     << L->getHeader()->getName() << "\n");
1270   const auto SImap = getSImap(SIGroups);
1271   const auto SGmap = getSGmap(SIGroups);
1272   // Compute instruction and loop-critical-path costs across two iterations for
1273   // both predicated and non-predicated version.
1274   const unsigned Iterations = 2;
1275   for (unsigned Iter = 0; Iter < Iterations; ++Iter) {
1276     // Cost of the loop's critical path.
1277     CostInfo &MaxCost = LoopCost[Iter];
1278     for (BasicBlock *BB : L->getBlocks()) {
1279       for (const Instruction &I : *BB) {
1280         if (I.isDebugOrPseudoInst())
1281           continue;
1282         // Compute the predicated and non-predicated cost of the instruction.
1283         Scaled64 IPredCost = Scaled64::getZero(),
1284                  INonPredCost = Scaled64::getZero();
1285 
1286         // Assume infinite resources that allow to fully exploit the available
1287         // instruction-level parallelism.
1288         // InstCost = InstLatency + max(Op1Cost, Op2Cost, … OpNCost)
1289         for (const Use &U : I.operands()) {
1290           auto UI = dyn_cast<Instruction>(U.get());
1291           if (!UI)
1292             continue;
1293           if (InstCostMap.count(UI)) {
1294             IPredCost = std::max(IPredCost, InstCostMap[UI].PredCost);
1295             INonPredCost = std::max(INonPredCost, InstCostMap[UI].NonPredCost);
1296           }
1297         }
1298         auto ILatency = computeInstCost(&I);
1299         if (!ILatency) {
1300           OptimizationRemarkMissed ORmissL(DEBUG_TYPE, "SelectOpti", &I);
1301           ORmissL << "Invalid instruction cost preventing analysis and "
1302                      "optimization of the inner-most loop containing this "
1303                      "instruction. ";
1304           EmitAndPrintRemark(ORE, ORmissL);
1305           return false;
1306         }
1307         IPredCost += Scaled64::get(*ILatency);
1308         INonPredCost += Scaled64::get(*ILatency);
1309 
1310         // For a select that can be converted to branch,
1311         // compute its cost as a branch (non-predicated cost).
1312         //
1313         // BranchCost = PredictedPathCost + MispredictCost
1314         // PredictedPathCost = TrueOpCost * TrueProb + FalseOpCost * FalseProb
1315         // MispredictCost = max(MispredictPenalty, CondCost) * MispredictRate
1316         if (SImap.contains(&I)) {
1317           auto SI = SImap.at(&I);
1318           const auto *SG = SGmap.at(&I);
1319           Scaled64 TrueOpCost = SI.getOpCostOnBranch(true, InstCostMap, TTI);
1320           Scaled64 FalseOpCost = SI.getOpCostOnBranch(false, InstCostMap, TTI);
1321           Scaled64 PredictedPathCost =
1322               getPredictedPathCost(TrueOpCost, FalseOpCost, SI);
1323 
1324           Scaled64 CondCost = Scaled64::getZero();
1325           if (auto *CI = dyn_cast<Instruction>(SG->Condition))
1326             if (InstCostMap.count(CI))
1327               CondCost = InstCostMap[CI].NonPredCost;
1328           Scaled64 MispredictCost = getMispredictionCost(SI, CondCost);
1329 
1330           INonPredCost = PredictedPathCost + MispredictCost;
1331         }
1332         LLVM_DEBUG(dbgs() << " " << ILatency << "/" << IPredCost << "/"
1333                           << INonPredCost << " for " << I << "\n");
1334 
1335         InstCostMap[&I] = {IPredCost, INonPredCost};
1336         MaxCost.PredCost = std::max(MaxCost.PredCost, IPredCost);
1337         MaxCost.NonPredCost = std::max(MaxCost.NonPredCost, INonPredCost);
1338       }
1339     }
1340     LLVM_DEBUG(dbgs() << "Iteration " << Iter + 1
1341                       << " MaxCost = " << MaxCost.PredCost << " "
1342                       << MaxCost.NonPredCost << "\n");
1343   }
1344   return true;
1345 }
1346 
1347 SmallDenseMap<const Instruction *, SelectOptimizeImpl::SelectLike, 2>
1348 SelectOptimizeImpl::getSImap(const SelectGroups &SIGroups) {
1349   SmallDenseMap<const Instruction *, SelectLike, 2> SImap;
1350   for (const SelectGroup &ASI : SIGroups)
1351     for (const SelectLike &SI : ASI.Selects)
1352       SImap.try_emplace(SI.getI(), SI);
1353   return SImap;
1354 }
1355 
1356 SmallDenseMap<const Instruction *, const SelectOptimizeImpl::SelectGroup *, 2>
1357 SelectOptimizeImpl::getSGmap(const SelectGroups &SIGroups) {
1358   SmallDenseMap<const Instruction *, const SelectGroup *, 2> SImap;
1359   for (const SelectGroup &ASI : SIGroups)
1360     for (const SelectLike &SI : ASI.Selects)
1361       SImap.try_emplace(SI.getI(), &ASI);
1362   return SImap;
1363 }
1364 
1365 std::optional<uint64_t>
1366 SelectOptimizeImpl::computeInstCost(const Instruction *I) {
1367   InstructionCost ICost =
1368       TTI->getInstructionCost(I, TargetTransformInfo::TCK_Latency);
1369   if (auto OC = ICost.getValue())
1370     return std::optional<uint64_t>(*OC);
1371   return std::nullopt;
1372 }
1373 
1374 ScaledNumber<uint64_t>
1375 SelectOptimizeImpl::getMispredictionCost(const SelectLike SI,
1376                                          const Scaled64 CondCost) {
1377   uint64_t MispredictPenalty = TSchedModel.getMCSchedModel()->MispredictPenalty;
1378 
1379   // Account for the default misprediction rate when using a branch
1380   // (conservatively set to 25% by default).
1381   uint64_t MispredictRate = MispredictDefaultRate;
1382   // If the select condition is obviously predictable, then the misprediction
1383   // rate is zero.
1384   if (isSelectHighlyPredictable(SI))
1385     MispredictRate = 0;
1386 
1387   // CondCost is included to account for cases where the computation of the
1388   // condition is part of a long dependence chain (potentially loop-carried)
1389   // that would delay detection of a misprediction and increase its cost.
1390   Scaled64 MispredictCost =
1391       std::max(Scaled64::get(MispredictPenalty), CondCost) *
1392       Scaled64::get(MispredictRate);
1393   MispredictCost /= Scaled64::get(100);
1394 
1395   return MispredictCost;
1396 }
1397 
1398 // Returns the cost of a branch when the prediction is correct.
1399 // TrueCost * TrueProbability + FalseCost * FalseProbability.
1400 ScaledNumber<uint64_t>
1401 SelectOptimizeImpl::getPredictedPathCost(Scaled64 TrueCost, Scaled64 FalseCost,
1402                                          const SelectLike SI) {
1403   Scaled64 PredPathCost;
1404   uint64_t TrueWeight, FalseWeight;
1405   if (extractBranchWeights(SI, TrueWeight, FalseWeight)) {
1406     uint64_t SumWeight = TrueWeight + FalseWeight;
1407     if (SumWeight != 0) {
1408       PredPathCost = TrueCost * Scaled64::get(TrueWeight) +
1409                      FalseCost * Scaled64::get(FalseWeight);
1410       PredPathCost /= Scaled64::get(SumWeight);
1411       return PredPathCost;
1412     }
1413   }
1414   // Without branch weight metadata, we assume 75% for the one path and 25% for
1415   // the other, and pick the result with the biggest cost.
1416   PredPathCost = std::max(TrueCost * Scaled64::get(3) + FalseCost,
1417                           FalseCost * Scaled64::get(3) + TrueCost);
1418   PredPathCost /= Scaled64::get(4);
1419   return PredPathCost;
1420 }
1421 
1422 bool SelectOptimizeImpl::isSelectKindSupported(const SelectLike SI) {
1423   TargetLowering::SelectSupportKind SelectKind;
1424   if (SI.getType()->isVectorTy())
1425     SelectKind = TargetLowering::ScalarCondVectorVal;
1426   else
1427     SelectKind = TargetLowering::ScalarValSelect;
1428   return TLI->isSelectSupported(SelectKind);
1429 }
1430