xref: /llvm-project/llvm/lib/Transforms/Vectorize/SandboxVectorizer/SeedCollector.cpp (revision 3f5039323c43af22896aeb2c34f6b302768d1fab)
1 //===- SeedCollector.cpp  -0000000-----------------------------------------===//
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 #include "llvm/Transforms/Vectorize/SandboxVectorizer/SeedCollector.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/Analysis/LoopAccessAnalysis.h"
12 #include "llvm/Analysis/ValueTracking.h"
13 #include "llvm/IR/BasicBlock.h"
14 #include "llvm/IR/Type.h"
15 #include "llvm/SandboxIR/Instruction.h"
16 #include "llvm/SandboxIR/Utils.h"
17 #include "llvm/Support/Debug.h"
18 
19 using namespace llvm;
20 namespace llvm::sandboxir {
21 
22 MutableArrayRef<Instruction *> SeedBundle::getSlice(unsigned StartIdx,
23                                                     unsigned MaxVecRegBits,
24                                                     bool ForcePowerOf2) {
25   // Use uint32_t here for compatibility with IsPowerOf2_32
26 
27   // BitCount tracks the size of the working slice. From that we can tell
28   // when the working slice's size is a power-of-two and when it exceeds
29   // the legal size in MaxVecBits.
30   uint32_t BitCount = 0;
31   uint32_t NumElements = 0;
32   // Tracks the most recent slice where NumElements gave a power-of-2 BitCount
33   uint32_t NumElementsPowerOfTwo = 0;
34   uint32_t BitCountPowerOfTwo = 0;
35   // Can't start a slice with a used instruction.
36   assert(!isUsed(StartIdx) && "Expected unused at StartIdx");
37   for (auto S : make_range(Seeds.begin() + StartIdx, Seeds.end())) {
38     uint32_t InstBits = Utils::getNumBits(S);
39     // Stop if this instruction is used, or if adding it puts the slice over
40     // the limit.
41     if (isUsed(StartIdx + NumElements) || BitCount + InstBits > MaxVecRegBits)
42       break;
43     NumElements++;
44     BitCount += InstBits;
45     if (ForcePowerOf2 && isPowerOf2_32(BitCount)) {
46       NumElementsPowerOfTwo = NumElements;
47       BitCountPowerOfTwo = BitCount;
48     }
49   }
50   if (ForcePowerOf2) {
51     NumElements = NumElementsPowerOfTwo;
52     BitCount = BitCountPowerOfTwo;
53   }
54 
55   assert((!ForcePowerOf2 || isPowerOf2_32(BitCount)) &&
56          "Must be a power of two");
57   // Return any non-empty slice
58   if (NumElements > 1)
59     return MutableArrayRef<Instruction *>(&Seeds[StartIdx], NumElements);
60   else
61     return {};
62 }
63 
64 } // namespace llvm::sandboxir
65