Skip to content

Commit 45e3787

Browse files
artagnonIanWood1
authored andcommitted
[VectorUtils] Improve computeMinimumValueSizes (NFC) (llvm#137692)
The Worklist used by computeMinimumValueSizes is wastefully a Value-vector, when the top of the loop attempts to cast the popped value to an Instruction anyway. Improve the algorithm by cutting this wasteful work, refining the type of Worklist, Visited, and Roots from Value-vectors to Instruction-vectors.
1 parent 30641f5 commit 45e3787

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

llvm/lib/Analysis/VectorUtils.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -750,9 +750,9 @@ llvm::computeMinimumValueSizes(ArrayRef<BasicBlock *> Blocks, DemandedBits &DB,
750750
// to ensure no extra casts would need to be inserted, so every DAG
751751
// of connected values must have the same minimum bitwidth.
752752
EquivalenceClasses<Value *> ECs;
753-
SmallVector<Value *, 16> Worklist;
754-
SmallPtrSet<Value *, 4> Roots;
755-
SmallPtrSet<Value *, 16> Visited;
753+
SmallVector<Instruction *, 16> Worklist;
754+
SmallPtrSet<Instruction *, 4> Roots;
755+
SmallPtrSet<Instruction *, 16> Visited;
756756
DenseMap<Value *, uint64_t> DBits;
757757
SmallPtrSet<Instruction *, 4> InstructionSet;
758758
MapVector<Instruction *, uint64_t> MinBWs;
@@ -786,17 +786,12 @@ llvm::computeMinimumValueSizes(ArrayRef<BasicBlock *> Blocks, DemandedBits &DB,
786786

787787
// Now proceed breadth-first, unioning values together.
788788
while (!Worklist.empty()) {
789-
Value *Val = Worklist.pop_back_val();
790-
Value *Leader = ECs.getOrInsertLeaderValue(Val);
789+
Instruction *I = Worklist.pop_back_val();
790+
Value *Leader = ECs.getOrInsertLeaderValue(I);
791791

792-
if (!Visited.insert(Val).second)
792+
if (!Visited.insert(I).second)
793793
continue;
794794

795-
// Non-instructions terminate a chain successfully.
796-
if (!isa<Instruction>(Val))
797-
continue;
798-
Instruction *I = cast<Instruction>(Val);
799-
800795
// If we encounter a type that is larger than 64 bits, we can't represent
801796
// it so bail out.
802797
if (DB.getDemandedBits(I).getBitWidth() > 64)
@@ -836,9 +831,10 @@ llvm::computeMinimumValueSizes(ArrayRef<BasicBlock *> Blocks, DemandedBits &DB,
836831
// All bits demanded, no point continuing.
837832
continue;
838833

839-
for (Value *O : cast<User>(I)->operands()) {
834+
for (Value *O : I->operands()) {
840835
ECs.unionSets(Leader, O);
841-
Worklist.push_back(O);
836+
if (auto *OI = dyn_cast<Instruction>(O))
837+
Worklist.push_back(OI);
842838
}
843839
}
844840

@@ -879,7 +875,7 @@ llvm::computeMinimumValueSizes(ArrayRef<BasicBlock *> Blocks, DemandedBits &DB,
879875
if (!MI)
880876
continue;
881877
Type *Ty = M->getType();
882-
if (Roots.count(M))
878+
if (Roots.count(MI))
883879
Ty = MI->getOperand(0)->getType();
884880

885881
if (MinBW >= Ty->getScalarSizeInBits())

0 commit comments

Comments
 (0)