xref: /llvm-project/llvm/lib/Target/AArch64/AArch64PromoteConstant.cpp (revision a41922ad7530ef5e311afbff2721e69cbf520890)
1 //==- AArch64PromoteConstant.cpp - Promote constant to global for AArch64 --==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the AArch64PromoteConstant pass which promotes constants
10 // to global variables when this is likely to be more efficient. Currently only
11 // types related to constant vector (i.e., constant vector, array of constant
12 // vectors, constant structure with a constant vector field, etc.) are promoted
13 // to global variables. Constant vectors are likely to be lowered in target
14 // constant pool during instruction selection already; therefore, the access
15 // will remain the same (memory load), but the structure types are not split
16 // into different constant pool accesses for each field. A bonus side effect is
17 // that created globals may be merged by the global merge pass.
18 //
19 // FIXME: This pass may be useful for other targets too.
20 //===----------------------------------------------------------------------===//
21 
22 #include "AArch64.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/IR/BasicBlock.h"
27 #include "llvm/IR/Constant.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/Dominators.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/GlobalValue.h"
32 #include "llvm/IR/GlobalVariable.h"
33 #include "llvm/IR/IRBuilder.h"
34 #include "llvm/IR/InstIterator.h"
35 #include "llvm/IR/Instruction.h"
36 #include "llvm/IR/Instructions.h"
37 #include "llvm/IR/IntrinsicInst.h"
38 #include "llvm/IR/Module.h"
39 #include "llvm/IR/Type.h"
40 #include "llvm/InitializePasses.h"
41 #include "llvm/Pass.h"
42 #include "llvm/Support/Casting.h"
43 #include "llvm/Support/CommandLine.h"
44 #include "llvm/Support/Debug.h"
45 #include "llvm/Support/raw_ostream.h"
46 #include <cassert>
47 #include <utility>
48 
49 using namespace llvm;
50 
51 #define DEBUG_TYPE "aarch64-promote-const"
52 
53 // Stress testing mode - disable heuristics.
54 static cl::opt<bool> Stress("aarch64-stress-promote-const", cl::Hidden,
55                             cl::desc("Promote all vector constants"));
56 
57 STATISTIC(NumPromoted, "Number of promoted constants");
58 STATISTIC(NumPromotedUses, "Number of promoted constants uses");
59 
60 //===----------------------------------------------------------------------===//
61 //                       AArch64PromoteConstant
62 //===----------------------------------------------------------------------===//
63 
64 namespace {
65 
66 /// Promotes interesting constant into global variables.
67 /// The motivating example is:
68 /// static const uint16_t TableA[32] = {
69 ///   41944, 40330, 38837, 37450, 36158, 34953, 33826, 32768,
70 ///   31776, 30841, 29960, 29128, 28340, 27595, 26887, 26215,
71 ///   25576, 24967, 24386, 23832, 23302, 22796, 22311, 21846,
72 ///   21400, 20972, 20561, 20165, 19785, 19419, 19066, 18725,
73 /// };
74 ///
75 /// uint8x16x4_t LoadStatic(void) {
76 ///   uint8x16x4_t ret;
77 ///   ret.val[0] = vld1q_u16(TableA +  0);
78 ///   ret.val[1] = vld1q_u16(TableA +  8);
79 ///   ret.val[2] = vld1q_u16(TableA + 16);
80 ///   ret.val[3] = vld1q_u16(TableA + 24);
81 ///   return ret;
82 /// }
83 ///
84 /// The constants in this example are folded into the uses. Thus, 4 different
85 /// constants are created.
86 ///
87 /// As their type is vector the cheapest way to create them is to load them
88 /// for the memory.
89 ///
90 /// Therefore the final assembly final has 4 different loads. With this pass
91 /// enabled, only one load is issued for the constants.
92 class AArch64PromoteConstant : public ModulePass {
93 public:
94   struct PromotedConstant {
95     bool ShouldConvert = false;
96     GlobalVariable *GV = nullptr;
97   };
98   using PromotionCacheTy = SmallDenseMap<Constant *, PromotedConstant, 16>;
99 
100   struct UpdateRecord {
101     Constant *C;
102     Instruction *User;
103     unsigned Op;
104 
105     UpdateRecord(Constant *C, Instruction *User, unsigned Op)
106         : C(C), User(User), Op(Op) {}
107   };
108 
109   static char ID;
110 
111   AArch64PromoteConstant() : ModulePass(ID) {
112     initializeAArch64PromoteConstantPass(*PassRegistry::getPassRegistry());
113   }
114 
115   StringRef getPassName() const override { return "AArch64 Promote Constant"; }
116 
117   /// Iterate over the functions and promote the interesting constants into
118   /// global variables with module scope.
119   bool runOnModule(Module &M) override {
120     LLVM_DEBUG(dbgs() << getPassName() << '\n');
121     if (skipModule(M))
122       return false;
123     bool Changed = false;
124     PromotionCacheTy PromotionCache;
125     for (auto &MF : M) {
126       Changed |= runOnFunction(MF, PromotionCache);
127     }
128     return Changed;
129   }
130 
131 private:
132   /// Look for interesting constants used within the given function.
133   /// Promote them into global variables, load these global variables within
134   /// the related function, so that the number of inserted load is minimal.
135   bool runOnFunction(Function &F, PromotionCacheTy &PromotionCache);
136 
137   // This transformation requires dominator info
138   void getAnalysisUsage(AnalysisUsage &AU) const override {
139     AU.setPreservesCFG();
140     AU.addRequired<DominatorTreeWrapperPass>();
141     AU.addPreserved<DominatorTreeWrapperPass>();
142   }
143 
144   /// Type to store a list of Uses.
145   using Uses = SmallVector<std::pair<Instruction *, unsigned>, 4>;
146   /// Map an insertion point to all the uses it dominates.
147   using InsertionPoints = DenseMap<Instruction *, Uses>;
148 
149   /// Find the closest point that dominates the given Use.
150   Instruction *findInsertionPoint(Instruction &User, unsigned OpNo);
151 
152   /// Check if the given insertion point is dominated by an existing
153   /// insertion point.
154   /// If true, the given use is added to the list of dominated uses for
155   /// the related existing point.
156   /// \param NewPt the insertion point to be checked
157   /// \param User the user of the constant
158   /// \param OpNo the operand number of the use
159   /// \param InsertPts existing insertion points
160   /// \pre NewPt and all instruction in InsertPts belong to the same function
161   /// \return true if one of the insertion point in InsertPts dominates NewPt,
162   ///         false otherwise
163   bool isDominated(Instruction *NewPt, Instruction *User, unsigned OpNo,
164                    InsertionPoints &InsertPts);
165 
166   /// Check if the given insertion point can be merged with an existing
167   /// insertion point in a common dominator.
168   /// If true, the given use is added to the list of the created insertion
169   /// point.
170   /// \param NewPt the insertion point to be checked
171   /// \param User the user of the constant
172   /// \param OpNo the operand number of the use
173   /// \param InsertPts existing insertion points
174   /// \pre NewPt and all instruction in InsertPts belong to the same function
175   /// \pre isDominated returns false for the exact same parameters.
176   /// \return true if it exists an insertion point in InsertPts that could
177   ///         have been merged with NewPt in a common dominator,
178   ///         false otherwise
179   bool tryAndMerge(Instruction *NewPt, Instruction *User, unsigned OpNo,
180                    InsertionPoints &InsertPts);
181 
182   /// Compute the minimal insertion points to dominates all the interesting
183   /// uses of value.
184   /// Insertion points are group per function and each insertion point
185   /// contains a list of all the uses it dominates within the related function
186   /// \param User the user of the constant
187   /// \param OpNo the operand number of the constant
188   /// \param[out] InsertPts output storage of the analysis
189   void computeInsertionPoint(Instruction *User, unsigned OpNo,
190                              InsertionPoints &InsertPts);
191 
192   /// Insert a definition of a new global variable at each point contained in
193   /// InsPtsPerFunc and update the related uses (also contained in
194   /// InsPtsPerFunc).
195   void insertDefinitions(Function &F, GlobalVariable &GV,
196                          InsertionPoints &InsertPts);
197 
198   /// Do the constant promotion indicated by the Updates records, keeping track
199   /// of globals in PromotionCache.
200   void promoteConstants(Function &F, SmallVectorImpl<UpdateRecord> &Updates,
201                         PromotionCacheTy &PromotionCache);
202 
203   /// Transfer the list of dominated uses of IPI to NewPt in InsertPts.
204   /// Append Use to this list and delete the entry of IPI in InsertPts.
205   static void appendAndTransferDominatedUses(Instruction *NewPt,
206                                              Instruction *User, unsigned OpNo,
207                                              InsertionPoints::iterator &IPI,
208                                              InsertionPoints &InsertPts) {
209     // Record the dominated use.
210     IPI->second.emplace_back(User, OpNo);
211     // Transfer the dominated uses of IPI to NewPt
212     // Inserting into the DenseMap may invalidate existing iterator.
213     // Keep a copy of the key to find the iterator to erase.  Keep a copy of the
214     // value so that we don't have to dereference IPI->second.
215     Instruction *OldInstr = IPI->first;
216     Uses OldUses = std::move(IPI->second);
217     InsertPts[NewPt] = std::move(OldUses);
218     // Erase IPI.
219     InsertPts.erase(OldInstr);
220   }
221 };
222 
223 } // end anonymous namespace
224 
225 char AArch64PromoteConstant::ID = 0;
226 
227 INITIALIZE_PASS_BEGIN(AArch64PromoteConstant, "aarch64-promote-const",
228                       "AArch64 Promote Constant Pass", false, false)
229 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
230 INITIALIZE_PASS_END(AArch64PromoteConstant, "aarch64-promote-const",
231                     "AArch64 Promote Constant Pass", false, false)
232 
233 ModulePass *llvm::createAArch64PromoteConstantPass() {
234   return new AArch64PromoteConstant();
235 }
236 
237 /// Check if the given type uses a vector type.
238 static bool isConstantUsingVectorTy(const Type *CstTy) {
239   if (CstTy->isVectorTy())
240     return true;
241   if (CstTy->isStructTy()) {
242     for (unsigned EltIdx = 0, EndEltIdx = CstTy->getStructNumElements();
243          EltIdx < EndEltIdx; ++EltIdx)
244       if (isConstantUsingVectorTy(CstTy->getStructElementType(EltIdx)))
245         return true;
246   } else if (CstTy->isArrayTy())
247     return isConstantUsingVectorTy(CstTy->getArrayElementType());
248   return false;
249 }
250 
251 // Returns true if \p C contains only ConstantData leafs and no global values,
252 // block addresses or constant expressions. Traverses ConstantAggregates.
253 static bool containsOnlyConstantData(const Constant *C) {
254   if (isa<ConstantData>(C))
255     return true;
256 
257   if (isa<GlobalValue>(C) || isa<BlockAddress>(C) || isa<ConstantExpr>(C))
258     return false;
259 
260   return all_of(C->operands(), [](const Use &U) {
261     return containsOnlyConstantData(cast<Constant>(&U));
262   });
263 }
264 
265 /// Check if the given use (Instruction + OpIdx) of Cst should be converted into
266 /// a load of a global variable initialized with Cst.
267 /// A use should be converted if it is legal to do so.
268 /// For instance, it is not legal to turn the mask operand of a shuffle vector
269 /// into a load of a global variable.
270 static bool shouldConvertUse(const Constant *Cst, const Instruction *Instr,
271                              unsigned OpIdx) {
272   // shufflevector instruction expects a const for the mask argument, i.e., the
273   // third argument. Do not promote this use in that case.
274   if (isa<const ShuffleVectorInst>(Instr) && OpIdx == 2)
275     return false;
276 
277   // extractvalue instruction expects a const idx.
278   if (isa<const ExtractValueInst>(Instr) && OpIdx > 0)
279     return false;
280 
281   // extractvalue instruction expects a const idx.
282   if (isa<const InsertValueInst>(Instr) && OpIdx > 1)
283     return false;
284 
285   if (isa<const AllocaInst>(Instr) && OpIdx > 0)
286     return false;
287 
288   // Alignment argument must be constant.
289   if (isa<const LoadInst>(Instr) && OpIdx > 0)
290     return false;
291 
292   // Alignment argument must be constant.
293   if (isa<const StoreInst>(Instr) && OpIdx > 1)
294     return false;
295 
296   // Index must be constant.
297   if (isa<const GetElementPtrInst>(Instr) && OpIdx > 0)
298     return false;
299 
300   // Personality function and filters must be constant.
301   // Give up on that instruction.
302   if (isa<const LandingPadInst>(Instr))
303     return false;
304 
305   // Switch instruction expects constants to compare to.
306   if (isa<const SwitchInst>(Instr))
307     return false;
308 
309   // Expected address must be a constant.
310   if (isa<const IndirectBrInst>(Instr))
311     return false;
312 
313   // Do not mess with intrinsics.
314   if (isa<const IntrinsicInst>(Instr))
315     return false;
316 
317   // Do not mess with inline asm.
318   const CallInst *CI = dyn_cast<const CallInst>(Instr);
319   return !(CI && CI->isInlineAsm());
320 }
321 
322 /// Check if the given Cst should be converted into
323 /// a load of a global variable initialized with Cst.
324 /// A constant should be converted if it is likely that the materialization of
325 /// the constant will be tricky. Thus, we give up on zero or undef values.
326 ///
327 /// \todo Currently, accept only vector related types.
328 /// Also we give up on all simple vector type to keep the existing
329 /// behavior. Otherwise, we should push here all the check of the lowering of
330 /// BUILD_VECTOR. By giving up, we lose the potential benefit of merging
331 /// constant via global merge and the fact that the same constant is stored
332 /// only once with this method (versus, as many function that uses the constant
333 /// for the regular approach, even for float).
334 /// Again, the simplest solution would be to promote every
335 /// constant and rematerialize them when they are actually cheap to create.
336 static bool shouldConvertImpl(const Constant *Cst) {
337   if (isa<const UndefValue>(Cst))
338     return false;
339 
340   // FIXME: In some cases, it may be interesting to promote in memory
341   // a zero initialized constant.
342   // E.g., when the type of Cst require more instructions than the
343   // adrp/add/load sequence or when this sequence can be shared by several
344   // instances of Cst.
345   // Ideally, we could promote this into a global and rematerialize the constant
346   // when it was a bad idea.
347   if (Cst->isZeroValue())
348     return false;
349 
350   if (Stress)
351     return true;
352 
353   // FIXME: see function \todo
354   if (Cst->getType()->isVectorTy())
355     return false;
356   return isConstantUsingVectorTy(Cst->getType());
357 }
358 
359 static bool
360 shouldConvert(Constant &C,
361               AArch64PromoteConstant::PromotionCacheTy &PromotionCache) {
362   auto Converted = PromotionCache.insert(
363       std::make_pair(&C, AArch64PromoteConstant::PromotedConstant()));
364   if (Converted.second)
365     Converted.first->second.ShouldConvert = shouldConvertImpl(&C);
366   return Converted.first->second.ShouldConvert;
367 }
368 
369 Instruction *AArch64PromoteConstant::findInsertionPoint(Instruction &User,
370                                                         unsigned OpNo) {
371   // If this user is a phi, the insertion point is in the related
372   // incoming basic block.
373   if (PHINode *PhiInst = dyn_cast<PHINode>(&User))
374     return PhiInst->getIncomingBlock(OpNo)->getTerminator();
375 
376   return &User;
377 }
378 
379 bool AArch64PromoteConstant::isDominated(Instruction *NewPt, Instruction *User,
380                                          unsigned OpNo,
381                                          InsertionPoints &InsertPts) {
382   DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(
383       *NewPt->getParent()->getParent()).getDomTree();
384 
385   // Traverse all the existing insertion points and check if one is dominating
386   // NewPt. If it is, remember that.
387   for (auto &IPI : InsertPts) {
388     if (NewPt == IPI.first || DT.dominates(IPI.first, NewPt) ||
389         // When IPI.first is a terminator instruction, DT may think that
390         // the result is defined on the edge.
391         // Here we are testing the insertion point, not the definition.
392         (IPI.first->getParent() != NewPt->getParent() &&
393          DT.dominates(IPI.first->getParent(), NewPt->getParent()))) {
394       // No need to insert this point. Just record the dominated use.
395       LLVM_DEBUG(dbgs() << "Insertion point dominated by:\n");
396       LLVM_DEBUG(IPI.first->print(dbgs()));
397       LLVM_DEBUG(dbgs() << '\n');
398       IPI.second.emplace_back(User, OpNo);
399       return true;
400     }
401   }
402   return false;
403 }
404 
405 bool AArch64PromoteConstant::tryAndMerge(Instruction *NewPt, Instruction *User,
406                                          unsigned OpNo,
407                                          InsertionPoints &InsertPts) {
408   DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(
409       *NewPt->getParent()->getParent()).getDomTree();
410   BasicBlock *NewBB = NewPt->getParent();
411 
412   // Traverse all the existing insertion point and check if one is dominated by
413   // NewPt and thus useless or can be combined with NewPt into a common
414   // dominator.
415   for (InsertionPoints::iterator IPI = InsertPts.begin(),
416                                  EndIPI = InsertPts.end();
417        IPI != EndIPI; ++IPI) {
418     BasicBlock *CurBB = IPI->first->getParent();
419     if (NewBB == CurBB) {
420       // Instructions are in the same block.
421       // By construction, NewPt is dominating the other.
422       // Indeed, isDominated returned false with the exact same arguments.
423       LLVM_DEBUG(dbgs() << "Merge insertion point with:\n");
424       LLVM_DEBUG(IPI->first->print(dbgs()));
425       LLVM_DEBUG(dbgs() << "\nat considered insertion point.\n");
426       appendAndTransferDominatedUses(NewPt, User, OpNo, IPI, InsertPts);
427       return true;
428     }
429 
430     // Look for a common dominator
431     BasicBlock *CommonDominator = DT.findNearestCommonDominator(NewBB, CurBB);
432     // If none exists, we cannot merge these two points.
433     if (!CommonDominator)
434       continue;
435 
436     if (CommonDominator != NewBB) {
437       // By construction, the CommonDominator cannot be CurBB.
438       assert(CommonDominator != CurBB &&
439              "Instruction has not been rejected during isDominated check!");
440       // Take the last instruction of the CommonDominator as insertion point
441       NewPt = CommonDominator->getTerminator();
442     }
443     // else, CommonDominator is the block of NewBB, hence NewBB is the last
444     // possible insertion point in that block.
445     LLVM_DEBUG(dbgs() << "Merge insertion point with:\n");
446     LLVM_DEBUG(IPI->first->print(dbgs()));
447     LLVM_DEBUG(dbgs() << '\n');
448     LLVM_DEBUG(NewPt->print(dbgs()));
449     LLVM_DEBUG(dbgs() << '\n');
450     appendAndTransferDominatedUses(NewPt, User, OpNo, IPI, InsertPts);
451     return true;
452   }
453   return false;
454 }
455 
456 void AArch64PromoteConstant::computeInsertionPoint(
457     Instruction *User, unsigned OpNo, InsertionPoints &InsertPts) {
458   LLVM_DEBUG(dbgs() << "Considered use, opidx " << OpNo << ":\n");
459   LLVM_DEBUG(User->print(dbgs()));
460   LLVM_DEBUG(dbgs() << '\n');
461 
462   Instruction *InsertionPoint = findInsertionPoint(*User, OpNo);
463 
464   LLVM_DEBUG(dbgs() << "Considered insertion point:\n");
465   LLVM_DEBUG(InsertionPoint->print(dbgs()));
466   LLVM_DEBUG(dbgs() << '\n');
467 
468   if (isDominated(InsertionPoint, User, OpNo, InsertPts))
469     return;
470   // This insertion point is useful, check if we can merge some insertion
471   // point in a common dominator or if NewPt dominates an existing one.
472   if (tryAndMerge(InsertionPoint, User, OpNo, InsertPts))
473     return;
474 
475   LLVM_DEBUG(dbgs() << "Keep considered insertion point\n");
476 
477   // It is definitely useful by its own
478   InsertPts[InsertionPoint].emplace_back(User, OpNo);
479 }
480 
481 static void ensurePromotedGV(Function &F, Constant &C,
482                              AArch64PromoteConstant::PromotedConstant &PC) {
483   assert(PC.ShouldConvert &&
484          "Expected that we should convert this to a global");
485   if (PC.GV)
486     return;
487   PC.GV = new GlobalVariable(
488       *F.getParent(), C.getType(), true, GlobalValue::InternalLinkage, nullptr,
489       "_PromotedConst", nullptr, GlobalVariable::NotThreadLocal);
490   PC.GV->setInitializer(&C);
491   LLVM_DEBUG(dbgs() << "Global replacement: ");
492   LLVM_DEBUG(PC.GV->print(dbgs()));
493   LLVM_DEBUG(dbgs() << '\n');
494   ++NumPromoted;
495 }
496 
497 void AArch64PromoteConstant::insertDefinitions(Function &F,
498                                                GlobalVariable &PromotedGV,
499                                                InsertionPoints &InsertPts) {
500 #ifndef NDEBUG
501   // Do more checking for debug purposes.
502   DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
503 #endif
504   assert(!InsertPts.empty() && "Empty uses does not need a definition");
505 
506   for (const auto &IPI : InsertPts) {
507     // Create the load of the global variable.
508     IRBuilder<> Builder(IPI.first);
509     LoadInst *LoadedCst =
510         Builder.CreateLoad(PromotedGV.getValueType(), &PromotedGV);
511     LLVM_DEBUG(dbgs() << "**********\n");
512     LLVM_DEBUG(dbgs() << "New def: ");
513     LLVM_DEBUG(LoadedCst->print(dbgs()));
514     LLVM_DEBUG(dbgs() << '\n');
515 
516     // Update the dominated uses.
517     for (auto Use : IPI.second) {
518 #ifndef NDEBUG
519       assert(DT.dominates(LoadedCst,
520                           findInsertionPoint(*Use.first, Use.second)) &&
521              "Inserted definition does not dominate all its uses!");
522 #endif
523       LLVM_DEBUG({
524         dbgs() << "Use to update " << Use.second << ":";
525         Use.first->print(dbgs());
526         dbgs() << '\n';
527       });
528       Use.first->setOperand(Use.second, LoadedCst);
529       ++NumPromotedUses;
530     }
531   }
532 }
533 
534 void AArch64PromoteConstant::promoteConstants(
535     Function &F, SmallVectorImpl<UpdateRecord> &Updates,
536     PromotionCacheTy &PromotionCache) {
537   // Promote the constants.
538   for (auto U = Updates.begin(), E = Updates.end(); U != E;) {
539     LLVM_DEBUG(dbgs() << "** Compute insertion points **\n");
540     auto First = U;
541     Constant *C = First->C;
542     InsertionPoints InsertPts;
543     do {
544       computeInsertionPoint(U->User, U->Op, InsertPts);
545     } while (++U != E && U->C == C);
546 
547     auto &Promotion = PromotionCache[C];
548     ensurePromotedGV(F, *C, Promotion);
549     insertDefinitions(F, *Promotion.GV, InsertPts);
550   }
551 }
552 
553 bool AArch64PromoteConstant::runOnFunction(Function &F,
554                                            PromotionCacheTy &PromotionCache) {
555   // Look for instructions using constant vector. Promote that constant to a
556   // global variable. Create as few loads of this variable as possible and
557   // update the uses accordingly.
558   SmallVector<UpdateRecord, 64> Updates;
559   for (Instruction &I : instructions(&F)) {
560     // Traverse the operand, looking for constant vectors. Replace them by a
561     // load of a global variable of constant vector type.
562     for (Use &U : I.operands()) {
563       Constant *Cst = dyn_cast<Constant>(U);
564       // There is no point in promoting global values as they are already
565       // global. Do not promote constants containing constant expression, global
566       // values or blockaddresses either, as they may require some code
567       // expansion.
568       if (!Cst || isa<GlobalValue>(Cst) || !containsOnlyConstantData(Cst))
569         continue;
570 
571       // Check if this constant is worth promoting.
572       if (!shouldConvert(*Cst, PromotionCache))
573         continue;
574 
575       // Check if this use should be promoted.
576       unsigned OpNo = &U - I.op_begin();
577       if (!shouldConvertUse(Cst, &I, OpNo))
578         continue;
579 
580       Updates.emplace_back(Cst, &I, OpNo);
581     }
582   }
583 
584   if (Updates.empty())
585     return false;
586 
587   promoteConstants(F, Updates, PromotionCache);
588   return true;
589 }
590