VectorUtils.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. //===----------- VectorUtils.cpp - Vectorizer utility functions -----------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines vectorizer utilities.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/ADT/EquivalenceClasses.h"
  14. #include "llvm/Analysis/DemandedBits.h"
  15. #include "llvm/Analysis/LoopInfo.h"
  16. #include "llvm/Analysis/ScalarEvolutionExpressions.h"
  17. #include "llvm/Analysis/ScalarEvolution.h"
  18. #include "llvm/Analysis/TargetTransformInfo.h"
  19. #include "llvm/Analysis/VectorUtils.h"
  20. #include "llvm/IR/GetElementPtrTypeIterator.h"
  21. #include "llvm/IR/PatternMatch.h"
  22. #include "llvm/IR/Value.h"
  23. #include "llvm/IR/Constants.h"
  24. using namespace llvm;
  25. using namespace llvm::PatternMatch;
  26. /// \brief Identify if the intrinsic is trivially vectorizable.
  27. /// This method returns true if the intrinsic's argument types are all
  28. /// scalars for the scalar form of the intrinsic and all vectors for
  29. /// the vector form of the intrinsic.
  30. bool llvm::isTriviallyVectorizable(Intrinsic::ID ID) {
  31. switch (ID) {
  32. case Intrinsic::sqrt:
  33. case Intrinsic::sin:
  34. case Intrinsic::cos:
  35. case Intrinsic::exp:
  36. case Intrinsic::exp2:
  37. case Intrinsic::log:
  38. case Intrinsic::log10:
  39. case Intrinsic::log2:
  40. case Intrinsic::fabs:
  41. case Intrinsic::minnum:
  42. case Intrinsic::maxnum:
  43. case Intrinsic::copysign:
  44. case Intrinsic::floor:
  45. case Intrinsic::ceil:
  46. case Intrinsic::trunc:
  47. case Intrinsic::rint:
  48. case Intrinsic::nearbyint:
  49. case Intrinsic::round:
  50. case Intrinsic::bswap:
  51. case Intrinsic::ctpop:
  52. case Intrinsic::pow:
  53. case Intrinsic::fma:
  54. case Intrinsic::fmuladd:
  55. case Intrinsic::ctlz:
  56. case Intrinsic::cttz:
  57. case Intrinsic::powi:
  58. return true;
  59. default:
  60. return false;
  61. }
  62. }
  63. /// \brief Identifies if the intrinsic has a scalar operand. It check for
  64. /// ctlz,cttz and powi special intrinsics whose argument is scalar.
  65. bool llvm::hasVectorInstrinsicScalarOpd(Intrinsic::ID ID,
  66. unsigned ScalarOpdIdx) {
  67. switch (ID) {
  68. case Intrinsic::ctlz:
  69. case Intrinsic::cttz:
  70. case Intrinsic::powi:
  71. return (ScalarOpdIdx == 1);
  72. default:
  73. return false;
  74. }
  75. }
  76. /// \brief Check call has a unary float signature
  77. /// It checks following:
  78. /// a) call should have a single argument
  79. /// b) argument type should be floating point type
  80. /// c) call instruction type and argument type should be same
  81. /// d) call should only reads memory.
  82. /// If all these condition is met then return ValidIntrinsicID
  83. /// else return not_intrinsic.
  84. Intrinsic::ID
  85. llvm::checkUnaryFloatSignature(const CallInst &I,
  86. Intrinsic::ID ValidIntrinsicID) {
  87. if (I.getNumArgOperands() != 1 ||
  88. !I.getArgOperand(0)->getType()->isFloatingPointTy() ||
  89. I.getType() != I.getArgOperand(0)->getType() || !I.onlyReadsMemory())
  90. return Intrinsic::not_intrinsic;
  91. return ValidIntrinsicID;
  92. }
  93. /// \brief Check call has a binary float signature
  94. /// It checks following:
  95. /// a) call should have 2 arguments.
  96. /// b) arguments type should be floating point type
  97. /// c) call instruction type and arguments type should be same
  98. /// d) call should only reads memory.
  99. /// If all these condition is met then return ValidIntrinsicID
  100. /// else return not_intrinsic.
  101. Intrinsic::ID
  102. llvm::checkBinaryFloatSignature(const CallInst &I,
  103. Intrinsic::ID ValidIntrinsicID) {
  104. if (I.getNumArgOperands() != 2 ||
  105. !I.getArgOperand(0)->getType()->isFloatingPointTy() ||
  106. !I.getArgOperand(1)->getType()->isFloatingPointTy() ||
  107. I.getType() != I.getArgOperand(0)->getType() ||
  108. I.getType() != I.getArgOperand(1)->getType() || !I.onlyReadsMemory())
  109. return Intrinsic::not_intrinsic;
  110. return ValidIntrinsicID;
  111. }
  112. /// \brief Returns intrinsic ID for call.
  113. /// For the input call instruction it finds mapping intrinsic and returns
  114. /// its ID, in case it does not found it return not_intrinsic.
  115. Intrinsic::ID llvm::getIntrinsicIDForCall(CallInst *CI,
  116. const TargetLibraryInfo *TLI) {
  117. // If we have an intrinsic call, check if it is trivially vectorizable.
  118. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
  119. Intrinsic::ID ID = II->getIntrinsicID();
  120. if (isTriviallyVectorizable(ID) || ID == Intrinsic::lifetime_start ||
  121. ID == Intrinsic::lifetime_end || ID == Intrinsic::assume)
  122. return ID;
  123. return Intrinsic::not_intrinsic;
  124. }
  125. if (!TLI)
  126. return Intrinsic::not_intrinsic;
  127. LibFunc::Func Func;
  128. Function *F = CI->getCalledFunction();
  129. // We're going to make assumptions on the semantics of the functions, check
  130. // that the target knows that it's available in this environment and it does
  131. // not have local linkage.
  132. if (!F || F->hasLocalLinkage() || !TLI->getLibFunc(F->getName(), Func))
  133. return Intrinsic::not_intrinsic;
  134. // Otherwise check if we have a call to a function that can be turned into a
  135. // vector intrinsic.
  136. switch (Func) {
  137. default:
  138. break;
  139. case LibFunc::sin:
  140. case LibFunc::sinf:
  141. case LibFunc::sinl:
  142. return checkUnaryFloatSignature(*CI, Intrinsic::sin);
  143. case LibFunc::cos:
  144. case LibFunc::cosf:
  145. case LibFunc::cosl:
  146. return checkUnaryFloatSignature(*CI, Intrinsic::cos);
  147. case LibFunc::exp:
  148. case LibFunc::expf:
  149. case LibFunc::expl:
  150. return checkUnaryFloatSignature(*CI, Intrinsic::exp);
  151. case LibFunc::exp2:
  152. case LibFunc::exp2f:
  153. case LibFunc::exp2l:
  154. return checkUnaryFloatSignature(*CI, Intrinsic::exp2);
  155. case LibFunc::log:
  156. case LibFunc::logf:
  157. case LibFunc::logl:
  158. return checkUnaryFloatSignature(*CI, Intrinsic::log);
  159. case LibFunc::log10:
  160. case LibFunc::log10f:
  161. case LibFunc::log10l:
  162. return checkUnaryFloatSignature(*CI, Intrinsic::log10);
  163. case LibFunc::log2:
  164. case LibFunc::log2f:
  165. case LibFunc::log2l:
  166. return checkUnaryFloatSignature(*CI, Intrinsic::log2);
  167. case LibFunc::fabs:
  168. case LibFunc::fabsf:
  169. case LibFunc::fabsl:
  170. return checkUnaryFloatSignature(*CI, Intrinsic::fabs);
  171. case LibFunc::fmin:
  172. case LibFunc::fminf:
  173. case LibFunc::fminl:
  174. return checkBinaryFloatSignature(*CI, Intrinsic::minnum);
  175. case LibFunc::fmax:
  176. case LibFunc::fmaxf:
  177. case LibFunc::fmaxl:
  178. return checkBinaryFloatSignature(*CI, Intrinsic::maxnum);
  179. case LibFunc::copysign:
  180. case LibFunc::copysignf:
  181. case LibFunc::copysignl:
  182. return checkBinaryFloatSignature(*CI, Intrinsic::copysign);
  183. case LibFunc::floor:
  184. case LibFunc::floorf:
  185. case LibFunc::floorl:
  186. return checkUnaryFloatSignature(*CI, Intrinsic::floor);
  187. case LibFunc::ceil:
  188. case LibFunc::ceilf:
  189. case LibFunc::ceill:
  190. return checkUnaryFloatSignature(*CI, Intrinsic::ceil);
  191. case LibFunc::trunc:
  192. case LibFunc::truncf:
  193. case LibFunc::truncl:
  194. return checkUnaryFloatSignature(*CI, Intrinsic::trunc);
  195. case LibFunc::rint:
  196. case LibFunc::rintf:
  197. case LibFunc::rintl:
  198. return checkUnaryFloatSignature(*CI, Intrinsic::rint);
  199. case LibFunc::nearbyint:
  200. case LibFunc::nearbyintf:
  201. case LibFunc::nearbyintl:
  202. return checkUnaryFloatSignature(*CI, Intrinsic::nearbyint);
  203. case LibFunc::round:
  204. case LibFunc::roundf:
  205. case LibFunc::roundl:
  206. return checkUnaryFloatSignature(*CI, Intrinsic::round);
  207. case LibFunc::pow:
  208. case LibFunc::powf:
  209. case LibFunc::powl:
  210. return checkBinaryFloatSignature(*CI, Intrinsic::pow);
  211. }
  212. return Intrinsic::not_intrinsic;
  213. }
  214. /// \brief Find the operand of the GEP that should be checked for consecutive
  215. /// stores. This ignores trailing indices that have no effect on the final
  216. /// pointer.
  217. unsigned llvm::getGEPInductionOperand(const GetElementPtrInst *Gep) {
  218. const DataLayout &DL = Gep->getModule()->getDataLayout();
  219. unsigned LastOperand = Gep->getNumOperands() - 1;
  220. unsigned GEPAllocSize = DL.getTypeAllocSize(Gep->getResultElementType());
  221. // Walk backwards and try to peel off zeros.
  222. while (LastOperand > 1 && match(Gep->getOperand(LastOperand), m_Zero())) {
  223. // Find the type we're currently indexing into.
  224. gep_type_iterator GEPTI = gep_type_begin(Gep);
  225. std::advance(GEPTI, LastOperand - 1);
  226. // If it's a type with the same allocation size as the result of the GEP we
  227. // can peel off the zero index.
  228. if (DL.getTypeAllocSize(*GEPTI) != GEPAllocSize)
  229. break;
  230. --LastOperand;
  231. }
  232. return LastOperand;
  233. }
  234. /// \brief If the argument is a GEP, then returns the operand identified by
  235. /// getGEPInductionOperand. However, if there is some other non-loop-invariant
  236. /// operand, it returns that instead.
  237. Value *llvm::stripGetElementPtr(Value *Ptr, ScalarEvolution *SE, Loop *Lp) {
  238. GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr);
  239. if (!GEP)
  240. return Ptr;
  241. unsigned InductionOperand = getGEPInductionOperand(GEP);
  242. // Check that all of the gep indices are uniform except for our induction
  243. // operand.
  244. for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i)
  245. if (i != InductionOperand &&
  246. !SE->isLoopInvariant(SE->getSCEV(GEP->getOperand(i)), Lp))
  247. return Ptr;
  248. return GEP->getOperand(InductionOperand);
  249. }
  250. /// \brief If a value has only one user that is a CastInst, return it.
  251. Value *llvm::getUniqueCastUse(Value *Ptr, Loop *Lp, Type *Ty) {
  252. Value *UniqueCast = nullptr;
  253. for (User *U : Ptr->users()) {
  254. CastInst *CI = dyn_cast<CastInst>(U);
  255. if (CI && CI->getType() == Ty) {
  256. if (!UniqueCast)
  257. UniqueCast = CI;
  258. else
  259. return nullptr;
  260. }
  261. }
  262. return UniqueCast;
  263. }
  264. /// \brief Get the stride of a pointer access in a loop. Looks for symbolic
  265. /// strides "a[i*stride]". Returns the symbolic stride, or null otherwise.
  266. Value *llvm::getStrideFromPointer(Value *Ptr, ScalarEvolution *SE, Loop *Lp) {
  267. auto *PtrTy = dyn_cast<PointerType>(Ptr->getType());
  268. if (!PtrTy || PtrTy->isAggregateType())
  269. return nullptr;
  270. // Try to remove a gep instruction to make the pointer (actually index at this
  271. // point) easier analyzable. If OrigPtr is equal to Ptr we are analzying the
  272. // pointer, otherwise, we are analyzing the index.
  273. Value *OrigPtr = Ptr;
  274. // The size of the pointer access.
  275. int64_t PtrAccessSize = 1;
  276. Ptr = stripGetElementPtr(Ptr, SE, Lp);
  277. const SCEV *V = SE->getSCEV(Ptr);
  278. if (Ptr != OrigPtr)
  279. // Strip off casts.
  280. while (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(V))
  281. V = C->getOperand();
  282. const SCEVAddRecExpr *S = dyn_cast<SCEVAddRecExpr>(V);
  283. if (!S)
  284. return nullptr;
  285. V = S->getStepRecurrence(*SE);
  286. if (!V)
  287. return nullptr;
  288. // Strip off the size of access multiplication if we are still analyzing the
  289. // pointer.
  290. if (OrigPtr == Ptr) {
  291. if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(V)) {
  292. if (M->getOperand(0)->getSCEVType() != scConstant)
  293. return nullptr;
  294. const APInt &APStepVal = cast<SCEVConstant>(M->getOperand(0))->getAPInt();
  295. // Huge step value - give up.
  296. if (APStepVal.getBitWidth() > 64)
  297. return nullptr;
  298. int64_t StepVal = APStepVal.getSExtValue();
  299. if (PtrAccessSize != StepVal)
  300. return nullptr;
  301. V = M->getOperand(1);
  302. }
  303. }
  304. // Strip off casts.
  305. Type *StripedOffRecurrenceCast = nullptr;
  306. if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(V)) {
  307. StripedOffRecurrenceCast = C->getType();
  308. V = C->getOperand();
  309. }
  310. // Look for the loop invariant symbolic value.
  311. const SCEVUnknown *U = dyn_cast<SCEVUnknown>(V);
  312. if (!U)
  313. return nullptr;
  314. Value *Stride = U->getValue();
  315. if (!Lp->isLoopInvariant(Stride))
  316. return nullptr;
  317. // If we have stripped off the recurrence cast we have to make sure that we
  318. // return the value that is used in this loop so that we can replace it later.
  319. if (StripedOffRecurrenceCast)
  320. Stride = getUniqueCastUse(Stride, Lp, StripedOffRecurrenceCast);
  321. return Stride;
  322. }
  323. /// \brief Given a vector and an element number, see if the scalar value is
  324. /// already around as a register, for example if it were inserted then extracted
  325. /// from the vector.
  326. Value *llvm::findScalarElement(Value *V, unsigned EltNo) {
  327. assert(V->getType()->isVectorTy() && "Not looking at a vector?");
  328. VectorType *VTy = cast<VectorType>(V->getType());
  329. unsigned Width = VTy->getNumElements();
  330. if (EltNo >= Width) // Out of range access.
  331. return UndefValue::get(VTy->getElementType());
  332. if (Constant *C = dyn_cast<Constant>(V))
  333. return C->getAggregateElement(EltNo);
  334. if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
  335. // If this is an insert to a variable element, we don't know what it is.
  336. if (!isa<ConstantInt>(III->getOperand(2)))
  337. return nullptr;
  338. unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
  339. // If this is an insert to the element we are looking for, return the
  340. // inserted value.
  341. if (EltNo == IIElt)
  342. return III->getOperand(1);
  343. // Otherwise, the insertelement doesn't modify the value, recurse on its
  344. // vector input.
  345. return findScalarElement(III->getOperand(0), EltNo);
  346. }
  347. if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
  348. unsigned LHSWidth = SVI->getOperand(0)->getType()->getVectorNumElements();
  349. int InEl = SVI->getMaskValue(EltNo);
  350. if (InEl < 0)
  351. return UndefValue::get(VTy->getElementType());
  352. if (InEl < (int)LHSWidth)
  353. return findScalarElement(SVI->getOperand(0), InEl);
  354. return findScalarElement(SVI->getOperand(1), InEl - LHSWidth);
  355. }
  356. // Extract a value from a vector add operation with a constant zero.
  357. Value *Val = nullptr; Constant *Con = nullptr;
  358. if (match(V, m_Add(m_Value(Val), m_Constant(Con))))
  359. if (Constant *Elt = Con->getAggregateElement(EltNo))
  360. if (Elt->isNullValue())
  361. return findScalarElement(Val, EltNo);
  362. // Otherwise, we don't know.
  363. return nullptr;
  364. }
  365. /// \brief Get splat value if the input is a splat vector or return nullptr.
  366. /// This function is not fully general. It checks only 2 cases:
  367. /// the input value is (1) a splat constants vector or (2) a sequence
  368. /// of instructions that broadcast a single value into a vector.
  369. ///
  370. const llvm::Value *llvm::getSplatValue(const Value *V) {
  371. if (auto *C = dyn_cast<Constant>(V))
  372. if (isa<VectorType>(V->getType()))
  373. return C->getSplatValue();
  374. auto *ShuffleInst = dyn_cast<ShuffleVectorInst>(V);
  375. if (!ShuffleInst)
  376. return nullptr;
  377. // All-zero (or undef) shuffle mask elements.
  378. for (int MaskElt : ShuffleInst->getShuffleMask())
  379. if (MaskElt != 0 && MaskElt != -1)
  380. return nullptr;
  381. // The first shuffle source is 'insertelement' with index 0.
  382. auto *InsertEltInst =
  383. dyn_cast<InsertElementInst>(ShuffleInst->getOperand(0));
  384. if (!InsertEltInst || !isa<ConstantInt>(InsertEltInst->getOperand(2)) ||
  385. !cast<ConstantInt>(InsertEltInst->getOperand(2))->isNullValue())
  386. return nullptr;
  387. return InsertEltInst->getOperand(1);
  388. }
  389. MapVector<Instruction *, uint64_t>
  390. llvm::computeMinimumValueSizes(ArrayRef<BasicBlock *> Blocks, DemandedBits &DB,
  391. const TargetTransformInfo *TTI) {
  392. // DemandedBits will give us every value's live-out bits. But we want
  393. // to ensure no extra casts would need to be inserted, so every DAG
  394. // of connected values must have the same minimum bitwidth.
  395. EquivalenceClasses<Value *> ECs;
  396. SmallVector<Value *, 16> Worklist;
  397. SmallPtrSet<Value *, 4> Roots;
  398. SmallPtrSet<Value *, 16> Visited;
  399. DenseMap<Value *, uint64_t> DBits;
  400. SmallPtrSet<Instruction *, 4> InstructionSet;
  401. MapVector<Instruction *, uint64_t> MinBWs;
  402. // Determine the roots. We work bottom-up, from truncs or icmps.
  403. bool SeenExtFromIllegalType = false;
  404. for (auto *BB : Blocks)
  405. for (auto &I : *BB) {
  406. InstructionSet.insert(&I);
  407. if (TTI && (isa<ZExtInst>(&I) || isa<SExtInst>(&I)) &&
  408. !TTI->isTypeLegal(I.getOperand(0)->getType()))
  409. SeenExtFromIllegalType = true;
  410. // Only deal with non-vector integers up to 64-bits wide.
  411. if ((isa<TruncInst>(&I) || isa<ICmpInst>(&I)) &&
  412. !I.getType()->isVectorTy() &&
  413. I.getOperand(0)->getType()->getScalarSizeInBits() <= 64) {
  414. // Don't make work for ourselves. If we know the loaded type is legal,
  415. // don't add it to the worklist.
  416. if (TTI && isa<TruncInst>(&I) && TTI->isTypeLegal(I.getType()))
  417. continue;
  418. Worklist.push_back(&I);
  419. Roots.insert(&I);
  420. }
  421. }
  422. // Early exit.
  423. if (Worklist.empty() || (TTI && !SeenExtFromIllegalType))
  424. return MinBWs;
  425. // Now proceed breadth-first, unioning values together.
  426. while (!Worklist.empty()) {
  427. Value *Val = Worklist.pop_back_val();
  428. Value *Leader = ECs.getOrInsertLeaderValue(Val);
  429. if (Visited.count(Val))
  430. continue;
  431. Visited.insert(Val);
  432. // Non-instructions terminate a chain successfully.
  433. if (!isa<Instruction>(Val))
  434. continue;
  435. Instruction *I = cast<Instruction>(Val);
  436. // If we encounter a type that is larger than 64 bits, we can't represent
  437. // it so bail out.
  438. if (DB.getDemandedBits(I).getBitWidth() > 64)
  439. return MapVector<Instruction *, uint64_t>();
  440. uint64_t V = DB.getDemandedBits(I).getZExtValue();
  441. DBits[Leader] |= V;
  442. // Casts, loads and instructions outside of our range terminate a chain
  443. // successfully.
  444. if (isa<SExtInst>(I) || isa<ZExtInst>(I) || isa<LoadInst>(I) ||
  445. !InstructionSet.count(I))
  446. continue;
  447. // Unsafe casts terminate a chain unsuccessfully. We can't do anything
  448. // useful with bitcasts, ptrtoints or inttoptrs and it'd be unsafe to
  449. // transform anything that relies on them.
  450. if (isa<BitCastInst>(I) || isa<PtrToIntInst>(I) || isa<IntToPtrInst>(I) ||
  451. !I->getType()->isIntegerTy()) {
  452. DBits[Leader] |= ~0ULL;
  453. continue;
  454. }
  455. // We don't modify the types of PHIs. Reductions will already have been
  456. // truncated if possible, and inductions' sizes will have been chosen by
  457. // indvars.
  458. if (isa<PHINode>(I))
  459. continue;
  460. if (DBits[Leader] == ~0ULL)
  461. // All bits demanded, no point continuing.
  462. continue;
  463. for (Value *O : cast<User>(I)->operands()) {
  464. ECs.unionSets(Leader, O);
  465. Worklist.push_back(O);
  466. }
  467. }
  468. // Now we've discovered all values, walk them to see if there are
  469. // any users we didn't see. If there are, we can't optimize that
  470. // chain.
  471. for (auto &I : DBits)
  472. for (auto *U : I.first->users())
  473. if (U->getType()->isIntegerTy() && DBits.count(U) == 0)
  474. DBits[ECs.getOrInsertLeaderValue(I.first)] |= ~0ULL;
  475. for (auto I = ECs.begin(), E = ECs.end(); I != E; ++I) {
  476. uint64_t LeaderDemandedBits = 0;
  477. for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); MI != ME; ++MI)
  478. LeaderDemandedBits |= DBits[*MI];
  479. uint64_t MinBW = (sizeof(LeaderDemandedBits) * 8) -
  480. llvm::countLeadingZeros(LeaderDemandedBits);
  481. // Round up to a power of 2
  482. if (!isPowerOf2_64((uint64_t)MinBW))
  483. MinBW = NextPowerOf2(MinBW);
  484. for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); MI != ME; ++MI) {
  485. if (!isa<Instruction>(*MI))
  486. continue;
  487. Type *Ty = (*MI)->getType();
  488. if (Roots.count(*MI))
  489. Ty = cast<Instruction>(*MI)->getOperand(0)->getType();
  490. if (MinBW < Ty->getScalarSizeInBits())
  491. MinBWs[cast<Instruction>(*MI)] = MinBW;
  492. }
  493. }
  494. return MinBWs;
  495. }