LLVM 22.0.0git
GVNSink.cpp
Go to the documentation of this file.
1//===- GVNSink.cpp - sink expressions into successors ---------------------===//
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/// \file GVNSink.cpp
10/// This pass attempts to sink instructions into successors, reducing static
11/// instruction count and enabling if-conversion.
12///
13/// We use a variant of global value numbering to decide what can be sunk.
14/// Consider:
15///
16/// [ %a1 = add i32 %b, 1 ] [ %c1 = add i32 %d, 1 ]
17/// [ %a2 = xor i32 %a1, 1 ] [ %c2 = xor i32 %c1, 1 ]
18/// \ /
19/// [ %e = phi i32 %a2, %c2 ]
20/// [ add i32 %e, 4 ]
21///
22///
23/// GVN would number %a1 and %c1 differently because they compute different
24/// results - the VN of an instruction is a function of its opcode and the
25/// transitive closure of its operands. This is the key property for hoisting
26/// and CSE.
27///
28/// What we want when sinking however is for a numbering that is a function of
29/// the *uses* of an instruction, which allows us to answer the question "if I
30/// replace %a1 with %c1, will it contribute in an equivalent way to all
31/// successive instructions?". The PostValueTable class in GVN provides this
32/// mapping.
33//
34//===----------------------------------------------------------------------===//
35
36#include "llvm/ADT/ArrayRef.h"
37#include "llvm/ADT/DenseMap.h"
38#include "llvm/ADT/DenseSet.h"
39#include "llvm/ADT/Hashing.h"
41#include "llvm/ADT/STLExtras.h"
44#include "llvm/ADT/Statistic.h"
46#include "llvm/IR/BasicBlock.h"
47#include "llvm/IR/CFG.h"
48#include "llvm/IR/Constants.h"
49#include "llvm/IR/Function.h"
50#include "llvm/IR/InstrTypes.h"
51#include "llvm/IR/Instruction.h"
53#include "llvm/IR/PassManager.h"
54#include "llvm/IR/Type.h"
55#include "llvm/IR/Use.h"
56#include "llvm/IR/Value.h"
62#include "llvm/Support/Debug.h"
69#include <cassert>
70#include <cstddef>
71#include <cstdint>
72#include <iterator>
73#include <utility>
74
75using namespace llvm;
76using namespace llvm::GVNExpression;
77
78#define DEBUG_TYPE "gvn-sink"
79
80STATISTIC(NumRemoved, "Number of instructions removed");
81
83 print(dbgs());
84 dbgs() << "\n";
85}
86
87static bool isMemoryInst(const Instruction *I) {
88 return isa<LoadInst>(I) || isa<StoreInst>(I) ||
89 (isa<InvokeInst>(I) && !cast<InvokeInst>(I)->doesNotAccessMemory()) ||
90 (isa<CallInst>(I) && !cast<CallInst>(I)->doesNotAccessMemory());
91}
92
93//===----------------------------------------------------------------------===//
94
95namespace {
96
97/// Candidate solution for sinking. There may be different ways to
98/// sink instructions, differing in the number of instructions sunk,
99/// the number of predecessors sunk from and the number of PHIs
100/// required.
101struct SinkingInstructionCandidate {
102 unsigned NumBlocks;
103 unsigned NumInstructions;
104 unsigned NumPHIs;
105 unsigned NumMemoryInsts;
106 int Cost = -1;
108
109 void calculateCost(unsigned NumOrigPHIs, unsigned NumOrigBlocks) {
110 unsigned NumExtraPHIs = NumPHIs - NumOrigPHIs;
111 unsigned SplitEdgeCost = (NumOrigBlocks > NumBlocks) ? 2 : 0;
112 Cost = (NumInstructions * (NumBlocks - 1)) -
113 (NumExtraPHIs *
114 NumExtraPHIs) // PHIs are expensive, so make sure they're worth it.
115 - SplitEdgeCost;
116 }
117
118 bool operator>(const SinkingInstructionCandidate &Other) const {
119 return Cost > Other.Cost;
120 }
121};
122
123//===----------------------------------------------------------------------===//
124
125/// Describes a PHI node that may or may not exist. These track the PHIs
126/// that must be created if we sunk a sequence of instructions. It provides
127/// a hash function for efficient equality comparisons.
128class ModelledPHI {
131
132public:
133 ModelledPHI() = default;
134
135 ModelledPHI(const PHINode *PN,
136 const DenseMap<const BasicBlock *, unsigned> &BlockOrder) {
137 // BasicBlock comes first so we sort by basic block pointer order,
138 // then by value pointer order. No need to call `verifyModelledPHI`
139 // As the Values and Blocks are populated in a deterministic order.
140 using OpsType = std::pair<BasicBlock *, Value *>;
142 for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I)
144
145 auto ComesBefore = [BlockOrder](OpsType O1, OpsType O2) {
146 return BlockOrder.lookup(O1.first) < BlockOrder.lookup(O2.first);
147 };
148 // Sort in a deterministic order.
149 llvm::sort(Ops, ComesBefore);
150
151 for (auto &P : Ops) {
152 Blocks.push_back(P.first);
153 Values.push_back(P.second);
154 }
155 }
156
157 /// Create a dummy ModelledPHI that will compare unequal to any other ModelledPHI
158 /// without the same ID.
159 /// \note This is specifically for DenseMapInfo - do not use this!
160 static ModelledPHI createDummy(size_t ID) {
161 ModelledPHI M;
162 M.Values.push_back(reinterpret_cast<Value*>(ID));
163 return M;
164 }
165
166 void
167 verifyModelledPHI(const DenseMap<const BasicBlock *, unsigned> &BlockOrder) {
168 assert(Values.size() > 1 && Blocks.size() > 1 &&
169 "Modelling PHI with less than 2 values");
170 auto ComesBefore = [BlockOrder](const BasicBlock *BB1,
171 const BasicBlock *BB2) {
172 return BlockOrder.lookup(BB1) < BlockOrder.lookup(BB2);
173 };
174 assert(llvm::is_sorted(Blocks, ComesBefore));
175 int C = 0;
176 for (const Value *V : Values) {
177 if (!isa<UndefValue>(V)) {
178 assert(cast<Instruction>(V)->getParent() == Blocks[C]);
179 (void)C;
180 }
181 C++;
182 }
183 }
184 /// Create a PHI from an array of incoming values and incoming blocks.
185 ModelledPHI(SmallVectorImpl<Instruction *> &V,
186 SmallSetVector<BasicBlock *, 4> &B,
187 const DenseMap<const BasicBlock *, unsigned> &BlockOrder) {
188 // The order of Values and Blocks are already ordered by the caller.
189 llvm::append_range(Values, V);
190 llvm::append_range(Blocks, B);
191 verifyModelledPHI(BlockOrder);
192 }
193
194 /// Create a PHI from [I[OpNum] for I in Insts].
195 /// TODO: Figure out a way to verifyModelledPHI in this constructor.
196 ModelledPHI(ArrayRef<Instruction *> Insts, unsigned OpNum,
197 SmallSetVector<BasicBlock *, 4> &B) {
198 llvm::append_range(Blocks, B);
199 for (auto *I : Insts)
200 Values.push_back(I->getOperand(OpNum));
201 }
202
203 /// Restrict the PHI's contents down to only \c NewBlocks.
204 /// \c NewBlocks must be a subset of \c this->Blocks.
205 void restrictToBlocks(const SmallSetVector<BasicBlock *, 4> &NewBlocks) {
206 auto BI = Blocks.begin();
207 auto VI = Values.begin();
208 while (BI != Blocks.end()) {
209 assert(VI != Values.end());
210 if (!NewBlocks.contains(*BI)) {
211 BI = Blocks.erase(BI);
212 VI = Values.erase(VI);
213 } else {
214 ++BI;
215 ++VI;
216 }
217 }
218 assert(Blocks.size() == NewBlocks.size());
219 }
220
221 ArrayRef<Value *> getValues() const { return Values; }
222
223 bool areAllIncomingValuesSame() const {
224 return llvm::all_equal(Values);
225 }
226
227 bool areAllIncomingValuesSameType() const {
228 return llvm::all_of(
229 Values, [&](Value *V) { return V->getType() == Values[0]->getType(); });
230 }
231
232 bool areAnyIncomingValuesConstant() const {
233 return llvm::any_of(Values, [&](Value *V) { return isa<Constant>(V); });
234 }
235
236 // Hash functor
237 unsigned hash() const {
238 // Is deterministic because Values are saved in a specific order.
239 return (unsigned)hash_combine_range(Values);
240 }
241
242 bool operator==(const ModelledPHI &Other) const {
243 return Values == Other.Values && Blocks == Other.Blocks;
244 }
245};
246} // namespace
247
248#ifndef NDEBUG
250 const SinkingInstructionCandidate &C) {
251 OS << "<Candidate Cost=" << C.Cost << " #Blocks=" << C.NumBlocks
252 << " #Insts=" << C.NumInstructions << " #PHIs=" << C.NumPHIs << ">";
253 return OS;
254}
255#endif
256
257template <> struct llvm::DenseMapInfo<ModelledPHI> {
258 static inline ModelledPHI &getEmptyKey() {
259 static ModelledPHI Dummy = ModelledPHI::createDummy(0);
260 return Dummy;
261 }
262
263 static inline ModelledPHI &getTombstoneKey() {
264 static ModelledPHI Dummy = ModelledPHI::createDummy(1);
265 return Dummy;
266 }
267
268 static unsigned getHashValue(const ModelledPHI &V) { return V.hash(); }
269
270 static bool isEqual(const ModelledPHI &LHS, const ModelledPHI &RHS) {
271 return LHS == RHS;
272 }
273};
274
276
277namespace {
278
279//===----------------------------------------------------------------------===//
280// ValueTable
281//===----------------------------------------------------------------------===//
282// This is a value number table where the value number is a function of the
283// *uses* of a value, rather than its operands. Thus, if VN(A) == VN(B) we know
284// that the program would be equivalent if we replaced A with PHI(A, B).
285//===----------------------------------------------------------------------===//
286
287/// A GVN expression describing how an instruction is used. The operands
288/// field of BasicExpression is used to store uses, not operands.
289///
290/// This class also contains fields for discriminators used when determining
291/// equivalence of instructions with sideeffects.
292class InstructionUseExpr : public BasicExpression {
293 unsigned MemoryUseOrder = -1;
294 bool Volatile = false;
295 ArrayRef<int> ShuffleMask;
296
297public:
298 InstructionUseExpr(Instruction *I, ArrayRecycler<Value *> &R,
300 : BasicExpression(I->getNumUses()) {
301 allocateOperands(R, A);
302 setOpcode(I->getOpcode());
303 setType(I->getType());
304
306 ShuffleMask = SVI->getShuffleMask().copy(A);
307
308 for (auto &U : I->uses())
309 op_push_back(U.getUser());
310 llvm::sort(operands());
311 }
312
313 void setMemoryUseOrder(unsigned MUO) { MemoryUseOrder = MUO; }
314 void setVolatile(bool V) { Volatile = V; }
315
316 hash_code getHashValue() const override {
317 return hash_combine(BasicExpression::getHashValue(), MemoryUseOrder,
318 Volatile, ShuffleMask);
319 }
320
321 template <typename Function> hash_code getHashValue(Function MapFn) {
322 hash_code H = hash_combine(getOpcode(), getType(), MemoryUseOrder, Volatile,
323 ShuffleMask);
324 for (auto *V : operands())
325 H = hash_combine(H, MapFn(V));
326 return H;
327 }
328};
329
330using BasicBlocksSet = SmallPtrSet<const BasicBlock *, 32>;
331
332class ValueTable {
333 DenseMap<Value *, uint32_t> ValueNumbering;
334 DenseMap<Expression *, uint32_t> ExpressionNumbering;
335 DenseMap<size_t, uint32_t> HashNumbering;
338 uint32_t nextValueNumber = 1;
339 BasicBlocksSet ReachableBBs;
340
341 /// Create an expression for I based on its opcode and its uses. If I
342 /// touches or reads memory, the expression is also based upon its memory
343 /// order - see \c getMemoryUseOrder().
344 InstructionUseExpr *createExpr(Instruction *I) {
345 InstructionUseExpr *E =
346 new (Allocator) InstructionUseExpr(I, Recycler, Allocator);
347 if (isMemoryInst(I))
348 E->setMemoryUseOrder(getMemoryUseOrder(I));
349
350 if (CmpInst *C = dyn_cast<CmpInst>(I)) {
351 CmpInst::Predicate Predicate = C->getPredicate();
352 E->setOpcode((C->getOpcode() << 8) | Predicate);
353 }
354 return E;
355 }
356
357 /// Helper to compute the value number for a memory instruction
358 /// (LoadInst/StoreInst), including checking the memory ordering and
359 /// volatility.
360 template <class Inst> InstructionUseExpr *createMemoryExpr(Inst *I) {
361 if (isStrongerThanUnordered(I->getOrdering()) || I->isAtomic())
362 return nullptr;
363 InstructionUseExpr *E = createExpr(I);
364 E->setVolatile(I->isVolatile());
365 return E;
366 }
367
368public:
369 ValueTable() = default;
370
371 /// Set basic blocks reachable from entry block.
372 void setReachableBBs(const BasicBlocksSet &ReachableBBs) {
373 this->ReachableBBs = ReachableBBs;
374 }
375
376 /// Returns the value number for the specified value, assigning
377 /// it a new number if it did not have one before.
378 uint32_t lookupOrAdd(Value *V) {
379 auto VI = ValueNumbering.find(V);
380 if (VI != ValueNumbering.end())
381 return VI->second;
382
383 if (!isa<Instruction>(V)) {
384 ValueNumbering[V] = nextValueNumber;
385 return nextValueNumber++;
386 }
387
389 if (!ReachableBBs.contains(I->getParent()))
390 return ~0U;
391
392 InstructionUseExpr *exp = nullptr;
393 switch (I->getOpcode()) {
394 case Instruction::Load:
395 exp = createMemoryExpr(cast<LoadInst>(I));
396 break;
397 case Instruction::Store:
398 exp = createMemoryExpr(cast<StoreInst>(I));
399 break;
400 case Instruction::Call:
401 case Instruction::Invoke:
402 case Instruction::FNeg:
403 case Instruction::Add:
404 case Instruction::FAdd:
405 case Instruction::Sub:
406 case Instruction::FSub:
407 case Instruction::Mul:
408 case Instruction::FMul:
409 case Instruction::UDiv:
410 case Instruction::SDiv:
411 case Instruction::FDiv:
412 case Instruction::URem:
413 case Instruction::SRem:
414 case Instruction::FRem:
415 case Instruction::Shl:
416 case Instruction::LShr:
417 case Instruction::AShr:
418 case Instruction::And:
419 case Instruction::Or:
420 case Instruction::Xor:
421 case Instruction::ICmp:
422 case Instruction::FCmp:
423 case Instruction::Trunc:
424 case Instruction::ZExt:
425 case Instruction::SExt:
426 case Instruction::FPToUI:
427 case Instruction::FPToSI:
428 case Instruction::UIToFP:
429 case Instruction::SIToFP:
430 case Instruction::FPTrunc:
431 case Instruction::FPExt:
432 case Instruction::PtrToInt:
433 case Instruction::IntToPtr:
434 case Instruction::BitCast:
435 case Instruction::AddrSpaceCast:
436 case Instruction::Select:
437 case Instruction::ExtractElement:
438 case Instruction::InsertElement:
439 case Instruction::ShuffleVector:
440 case Instruction::InsertValue:
441 case Instruction::GetElementPtr:
442 exp = createExpr(I);
443 break;
444 default:
445 break;
446 }
447
448 if (!exp) {
449 ValueNumbering[V] = nextValueNumber;
450 return nextValueNumber++;
451 }
452
453 uint32_t e = ExpressionNumbering[exp];
454 if (!e) {
455 hash_code H = exp->getHashValue([=](Value *V) { return lookupOrAdd(V); });
456 auto [I, Inserted] = HashNumbering.try_emplace(H, nextValueNumber);
457 e = I->second;
458 if (Inserted)
459 ExpressionNumbering[exp] = nextValueNumber++;
460 }
461 ValueNumbering[V] = e;
462 return e;
463 }
464
465 /// Returns the value number of the specified value. Fails if the value has
466 /// not yet been numbered.
467 uint32_t lookup(Value *V) const {
468 auto VI = ValueNumbering.find(V);
469 assert(VI != ValueNumbering.end() && "Value not numbered?");
470 return VI->second;
471 }
472
473 /// Removes all value numberings and resets the value table.
474 void clear() {
475 ValueNumbering.clear();
476 ExpressionNumbering.clear();
477 HashNumbering.clear();
479 nextValueNumber = 1;
480 }
481
482 /// \c Inst uses or touches memory. Return an ID describing the memory state
483 /// at \c Inst such that if getMemoryUseOrder(I1) == getMemoryUseOrder(I2),
484 /// the exact same memory operations happen after I1 and I2.
485 ///
486 /// This is a very hard problem in general, so we use domain-specific
487 /// knowledge that we only ever check for equivalence between blocks sharing a
488 /// single immediate successor that is common, and when determining if I1 ==
489 /// I2 we will have already determined that next(I1) == next(I2). This
490 /// inductive property allows us to simply return the value number of the next
491 /// instruction that defines memory.
492 uint32_t getMemoryUseOrder(Instruction *Inst) {
493 auto *BB = Inst->getParent();
494 for (auto I = std::next(Inst->getIterator()), E = BB->end();
495 I != E && !I->isTerminator(); ++I) {
496 if (!isMemoryInst(&*I))
497 continue;
498 if (isa<LoadInst>(&*I))
499 continue;
501 if (CI && CI->onlyReadsMemory())
502 continue;
504 if (II && II->onlyReadsMemory())
505 continue;
506 return lookupOrAdd(&*I);
507 }
508 return 0;
509 }
510};
511
512//===----------------------------------------------------------------------===//
513
514class GVNSink {
515public:
516 GVNSink() {}
517
518 bool run(Function &F) {
519 LLVM_DEBUG(dbgs() << "GVNSink: running on function @" << F.getName()
520 << "\n");
521
522 unsigned NumSunk = 0;
524 VN.setReachableBBs(BasicBlocksSet(llvm::from_range, RPOT));
525 // Populate reverse post-order to order basic blocks in deterministic
526 // order. Any arbitrary ordering will work in this case as long as they are
527 // deterministic. The node ordering of newly created basic blocks
528 // are irrelevant because RPOT(for computing sinkable candidates) is also
529 // obtained ahead of time and only their order are relevant for this pass.
530 unsigned NodeOrdering = 0;
531 RPOTOrder[*RPOT.begin()] = ++NodeOrdering;
532 for (auto *BB : RPOT)
533 if (!pred_empty(BB))
534 RPOTOrder[BB] = ++NodeOrdering;
535 for (auto *N : RPOT)
536 NumSunk += sinkBB(N);
537
538 return NumSunk > 0;
539 }
540
541private:
542 ValueTable VN;
544
545 bool shouldAvoidSinkingInstruction(Instruction *I) {
546 // These instructions may change or break semantics if moved.
547 if (isa<PHINode>(I) || I->isEHPad() || isa<AllocaInst>(I) ||
548 I->getType()->isTokenTy())
549 return true;
550 return false;
551 }
552
553 /// The main heuristic function. Analyze the set of instructions pointed to by
554 /// LRI and return a candidate solution if these instructions can be sunk, or
555 /// std::nullopt otherwise.
556 std::optional<SinkingInstructionCandidate>
557 analyzeInstructionForSinking(LockstepReverseIterator<false> &LRI,
558 unsigned &InstNum, unsigned &MemoryInstNum,
559 ModelledPHISet &NeededPHIs,
560 SmallPtrSetImpl<Value *> &PHIContents);
561
562 /// Create a ModelledPHI for each PHI in BB, adding to PHIs.
563 void analyzeInitialPHIs(BasicBlock *BB, ModelledPHISet &PHIs,
564 SmallPtrSetImpl<Value *> &PHIContents) {
565 for (PHINode &PN : BB->phis()) {
566 auto MPHI = ModelledPHI(&PN, RPOTOrder);
567 PHIs.insert(MPHI);
568 PHIContents.insert_range(MPHI.getValues());
569 }
570 }
571
572 /// The main instruction sinking driver. Set up state and try and sink
573 /// instructions into BBEnd from its predecessors.
574 unsigned sinkBB(BasicBlock *BBEnd);
575
576 /// Perform the actual mechanics of sinking an instruction from Blocks into
577 /// BBEnd, which is their only successor.
579
580 /// Remove PHIs that all have the same incoming value.
581 void foldPointlessPHINodes(BasicBlock *BB) {
582 auto I = BB->begin();
583 while (PHINode *PN = dyn_cast<PHINode>(I++)) {
584 if (!llvm::all_of(PN->incoming_values(), [&](const Value *V) {
585 return V == PN->getIncomingValue(0);
586 }))
587 continue;
588 if (PN->getIncomingValue(0) != PN)
590 else
592 PN->eraseFromParent();
593 }
594 }
595};
596} // namespace
597
598std::optional<SinkingInstructionCandidate>
599GVNSink::analyzeInstructionForSinking(LockstepReverseIterator<false> &LRI,
600 unsigned &InstNum,
601 unsigned &MemoryInstNum,
602 ModelledPHISet &NeededPHIs,
603 SmallPtrSetImpl<Value *> &PHIContents) {
604 auto Insts = *LRI;
605 LLVM_DEBUG(dbgs() << " -- Analyzing instruction set: [\n"; for (auto *I
606 : Insts) {
607 I->dump();
608 } dbgs() << " ]\n";);
609
610 DenseMap<uint32_t, unsigned> VNums;
611 for (auto *I : Insts) {
612 uint32_t N = VN.lookupOrAdd(I);
613 LLVM_DEBUG(dbgs() << " VN=" << Twine::utohexstr(N) << " for" << *I << "\n");
614 if (N == ~0U)
615 return std::nullopt;
616 VNums[N]++;
617 }
618 unsigned VNumToSink = llvm::max_element(VNums, llvm::less_second())->first;
619
620 if (VNums[VNumToSink] == 1)
621 // Can't sink anything!
622 return std::nullopt;
623
624 // Now restrict the number of incoming blocks down to only those with
625 // VNumToSink.
626 auto &ActivePreds = LRI.getActiveBlocks();
627 unsigned InitialActivePredSize = ActivePreds.size();
628 SmallVector<Instruction *, 4> NewInsts;
629 for (auto *I : Insts) {
630 if (VN.lookup(I) != VNumToSink)
631 ActivePreds.remove(I->getParent());
632 else
633 NewInsts.push_back(I);
634 }
635 for (auto *I : NewInsts)
636 if (shouldAvoidSinkingInstruction(I))
637 return std::nullopt;
638
639 // If we've restricted the incoming blocks, restrict all needed PHIs also
640 // to that set.
641 bool RecomputePHIContents = false;
642 if (ActivePreds.size() != InitialActivePredSize) {
643 ModelledPHISet NewNeededPHIs;
644 for (auto P : NeededPHIs) {
645 P.restrictToBlocks(ActivePreds);
646 NewNeededPHIs.insert(P);
647 }
648 NeededPHIs = NewNeededPHIs;
649 LRI.restrictToBlocks(ActivePreds);
650 RecomputePHIContents = true;
651 }
652
653 // The sunk instruction's results.
654 ModelledPHI NewPHI(NewInsts, ActivePreds, RPOTOrder);
655
656 // Does sinking this instruction render previous PHIs redundant?
657 if (NeededPHIs.erase(NewPHI))
658 RecomputePHIContents = true;
659
660 if (RecomputePHIContents) {
661 // The needed PHIs have changed, so recompute the set of all needed
662 // values.
663 PHIContents.clear();
664 for (auto &PHI : NeededPHIs)
665 PHIContents.insert_range(PHI.getValues());
666 }
667
668 // Is this instruction required by a later PHI that doesn't match this PHI?
669 // if so, we can't sink this instruction.
670 for (auto *V : NewPHI.getValues())
671 if (PHIContents.count(V))
672 // V exists in this PHI, but the whole PHI is different to NewPHI
673 // (else it would have been removed earlier). We cannot continue
674 // because this isn't representable.
675 return std::nullopt;
676
677 // Which operands need PHIs?
678 // FIXME: If any of these fail, we should partition up the candidates to
679 // try and continue making progress.
680 Instruction *I0 = NewInsts[0];
681
682 auto isNotSameOperation = [&I0](Instruction *I) {
683 return !I0->isSameOperationAs(I);
684 };
685
686 if (any_of(NewInsts, isNotSameOperation))
687 return std::nullopt;
688
689 for (unsigned OpNum = 0, E = I0->getNumOperands(); OpNum != E; ++OpNum) {
690 ModelledPHI PHI(NewInsts, OpNum, ActivePreds);
691 if (PHI.areAllIncomingValuesSame())
692 continue;
693 if (!canReplaceOperandWithVariable(I0, OpNum))
694 // We can 't create a PHI from this instruction!
695 return std::nullopt;
696 if (NeededPHIs.count(PHI))
697 continue;
698 if (!PHI.areAllIncomingValuesSameType())
699 return std::nullopt;
700 // Don't create indirect calls! The called value is the final operand.
701 if ((isa<CallInst>(I0) || isa<InvokeInst>(I0)) && OpNum == E - 1 &&
702 PHI.areAnyIncomingValuesConstant())
703 return std::nullopt;
704
705 NeededPHIs.reserve(NeededPHIs.size());
706 NeededPHIs.insert(PHI);
707 PHIContents.insert_range(PHI.getValues());
708 }
709
710 if (isMemoryInst(NewInsts[0]))
711 ++MemoryInstNum;
712
713 SinkingInstructionCandidate Cand;
714 Cand.NumInstructions = ++InstNum;
715 Cand.NumMemoryInsts = MemoryInstNum;
716 Cand.NumBlocks = ActivePreds.size();
717 Cand.NumPHIs = NeededPHIs.size();
718 append_range(Cand.Blocks, ActivePreds);
719
720 return Cand;
721}
722
723unsigned GVNSink::sinkBB(BasicBlock *BBEnd) {
724 LLVM_DEBUG(dbgs() << "GVNSink: running on basic block ";
725 BBEnd->printAsOperand(dbgs()); dbgs() << "\n");
727 for (auto *B : predecessors(BBEnd)) {
728 // Bailout on basic blocks without predecessor(PR42346).
729 if (!RPOTOrder.count(B))
730 return 0;
731 auto *T = B->getTerminator();
733 Preds.push_back(B);
734 else
735 return 0;
736 }
737 if (Preds.size() < 2)
738 return 0;
739 auto ComesBefore = [this](const BasicBlock *BB1, const BasicBlock *BB2) {
740 return RPOTOrder.lookup(BB1) < RPOTOrder.lookup(BB2);
741 };
742 // Sort in a deterministic order.
743 llvm::sort(Preds, ComesBefore);
744
745 unsigned NumOrigPreds = Preds.size();
746 // We can only sink instructions through unconditional branches.
747 llvm::erase_if(Preds, [](BasicBlock *BB) {
748 return BB->getTerminator()->getNumSuccessors() != 1;
749 });
750
751 LockstepReverseIterator<false> LRI(Preds);
753 unsigned InstNum = 0, MemoryInstNum = 0;
754 ModelledPHISet NeededPHIs;
755 SmallPtrSet<Value *, 4> PHIContents;
756 analyzeInitialPHIs(BBEnd, NeededPHIs, PHIContents);
757 unsigned NumOrigPHIs = NeededPHIs.size();
758
759 while (LRI.isValid()) {
760 auto Cand = analyzeInstructionForSinking(LRI, InstNum, MemoryInstNum,
761 NeededPHIs, PHIContents);
762 if (!Cand)
763 break;
764 Cand->calculateCost(NumOrigPHIs, Preds.size());
765 Candidates.emplace_back(*Cand);
766 --LRI;
767 }
768
769 llvm::stable_sort(Candidates, std::greater<SinkingInstructionCandidate>());
770 LLVM_DEBUG(dbgs() << " -- Sinking candidates:\n"; for (auto &C
771 : Candidates) dbgs()
772 << " " << C << "\n";);
773
774 // Pick the top candidate, as long it is positive!
775 if (Candidates.empty() || Candidates.front().Cost <= 0)
776 return 0;
777 auto C = Candidates.front();
778
779 LLVM_DEBUG(dbgs() << " -- Sinking: " << C << "\n");
780 BasicBlock *InsertBB = BBEnd;
781 if (C.Blocks.size() < NumOrigPreds) {
782 LLVM_DEBUG(dbgs() << " -- Splitting edge to ";
783 BBEnd->printAsOperand(dbgs()); dbgs() << "\n");
784 InsertBB = SplitBlockPredecessors(BBEnd, C.Blocks, ".gvnsink.split");
785 if (!InsertBB) {
786 LLVM_DEBUG(dbgs() << " -- FAILED to split edge!\n");
787 // Edge couldn't be split.
788 return 0;
789 }
790 }
791
792 for (unsigned I = 0; I < C.NumInstructions; ++I)
793 sinkLastInstruction(C.Blocks, InsertBB);
794
795 return C.NumInstructions;
796}
797
798void GVNSink::sinkLastInstruction(ArrayRef<BasicBlock *> Blocks,
799 BasicBlock *BBEnd) {
800 SmallVector<Instruction *, 4> Insts;
801 for (BasicBlock *BB : Blocks)
802 Insts.push_back(BB->getTerminator()->getPrevNode());
803 Instruction *I0 = Insts.front();
804
805 SmallVector<Value *, 4> NewOperands;
806 for (unsigned O = 0, E = I0->getNumOperands(); O != E; ++O) {
807 bool NeedPHI = llvm::any_of(Insts, [&I0, O](const Instruction *I) {
808 return I->getOperand(O) != I0->getOperand(O);
809 });
810 if (!NeedPHI) {
811 NewOperands.push_back(I0->getOperand(O));
812 continue;
813 }
814
815 // Create a new PHI in the successor block and populate it.
816 auto *Op = I0->getOperand(O);
817 assert(!Op->getType()->isTokenTy() && "Can't PHI tokens!");
818 auto *PN =
819 PHINode::Create(Op->getType(), Insts.size(), Op->getName() + ".sink");
820 PN->insertBefore(BBEnd->begin());
821 for (auto *I : Insts)
822 PN->addIncoming(I->getOperand(O), I->getParent());
823 NewOperands.push_back(PN);
824 }
825
826 // Arbitrarily use I0 as the new "common" instruction; remap its operands
827 // and move it to the start of the successor block.
828 for (unsigned O = 0, E = I0->getNumOperands(); O != E; ++O)
829 I0->getOperandUse(O).set(NewOperands[O]);
830 I0->moveBefore(BBEnd->getFirstInsertionPt());
831
832 // Update metadata and IR flags.
833 for (auto *I : Insts)
834 if (I != I0) {
835 combineMetadataForCSE(I0, I, true);
836 I0->andIRFlags(I);
837 }
838
839 for (auto *I : Insts)
840 if (I != I0) {
841 I->replaceAllUsesWith(I0);
842 I0->applyMergedLocation(I0->getDebugLoc(), I->getDebugLoc());
843 }
844 foldPointlessPHINodes(BBEnd);
845
846 // Finally nuke all instructions apart from the common instruction.
847 for (auto *I : Insts)
848 if (I != I0)
849 I->eraseFromParent();
850
851 NumRemoved += Insts.size() - 1;
852}
853
855 GVNSink G;
856 if (!G.run(F))
857 return PreservedAnalyses::all();
858
860}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Rewrite undef for PHI
This file defines the BumpPtrAllocator interface.
Atomic ordering constants.
static const Function * getParent(const Value *V)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:638
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
The header file for the GVN pass that contains expression handling classes.
static bool isMemoryInst(const Instruction *I)
Definition GVNSink.cpp:87
DenseSet< ModelledPHI > ModelledPHISet
Definition GVNSink.cpp:275
This file provides the interface for LLVM's Global Value Numbering pass which eliminates fully redund...
This is the interface for a simple mod/ref and alias analysis over globals.
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
This header defines various interfaces for pass management in LLVM.
This defines the Use class.
static bool lookup(const GsymReader &GR, DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
#define G(x, y, z)
Definition MD5.cpp:56
#define H(x, y, z)
Definition MD5.cpp:57
#define T
uint64_t IntrinsicInst * II
#define P(N)
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
Basic Register Allocator
This file contains some templates that are useful if you are working with the STL at all.
static void sinkLastInstruction(ArrayRef< BasicBlock * > Blocks)
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
Recycle small arrays allocated from a BumpPtrAllocator.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:459
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition BasicBlock.h:528
LLVM_ABI const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
bool onlyReadsMemory(unsigned OpNo) const
This class represents a function call, abstracting a target machine's calling convention.
This class is the base class for the comparison instructions.
Definition InstrTypes.h:664
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition DenseMap.h:194
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:167
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:237
iterator end()
Definition DenseMap.h:81
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
hash_code getHashValue() const override
LLVM_DUMP_METHOD void dump() const
Definition GVNSink.cpp:82
void print(raw_ostream &OS) const
LLVM_ABI bool isSameOperationAs(const Instruction *I, unsigned flags=0) const LLVM_READONLY
This function determines if the specified instruction executes the same operation as the current one.
LLVM_ABI unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI void andIRFlags(const Value *V)
Logical 'and' of any supported wrapping, exact, and fast-math flags of V and this instruction.
LLVM_ABI void moveBefore(InstListType::iterator InsertPos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
LLVM_ABI void insertBefore(InstListType::iterator InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified position.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI void applyMergedLocation(DebugLoc LocA, DebugLoc LocB)
Merge 2 debug locations and apply it to the Instruction.
Invoke instruction.
Iterates through instructions in a set of blocks in reverse order from the first non-terminator.
void restrictToBlocks(SmallSetVector< BasicBlock *, 4 > &Blocks)
SmallSetVector< BasicBlock *, 4 > & getActiveBlocks()
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
op_range incoming_values()
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Recycler - This class manages a linked-list of deallocated nodes and facilitates reusing deallocated ...
Definition Recycler.h:36
void clear(AllocatorType &Allocator)
clear - Release all the tracked allocations to the allocator.
Definition Recycler.h:74
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:102
bool contains(const key_type &key) const
Check if the SetVector contains the given key.
Definition SetVector.h:251
This instruction constructs a fixed permutation of two input vectors.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
void insert_range(Range &&R)
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
reference emplace_back(ArgTypes &&... Args)
iterator erase(const_iterator CI)
void push_back(const T &Elt)
static Twine utohexstr(uint64_t Val)
Definition Twine.h:385
LLVM_ABI void set(Value *Val)
Definition Value.h:905
const Use & getOperandUse(unsigned i) const
Definition User.h:245
Value * getOperand(unsigned i) const
Definition User.h:232
unsigned getNumOperands() const
Definition User.h:254
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:546
LLVM_ABI void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
size_type size() const
Definition DenseSet.h:87
An opaque object representing a hash code.
Definition Hashing.h:76
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
SmallVector< ValueIDNum, 0 > ValueTable
Type for a table of values in a block.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
raw_ostream & operator<<(raw_ostream &OS, const Expression &E)
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
void stable_sort(R &&Range)
Definition STLExtras.h:2058
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1725
InstructionCost Cost
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:644
constexpr from_range_t from_range
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2136
bool isStrongerThanUnordered(AtomicOrdering AO)
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1732
bool operator>(int64_t V1, const APSInt &V2)
Definition APSInt.h:363
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1622
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
bool is_sorted(R &&Range, Compare C)
Wrapper function around std::is_sorted to check if elements in a range R are sorted with respect to a...
Definition STLExtras.h:1920
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
LLVM_ABI void combineMetadataForCSE(Instruction *K, const Instruction *J, bool DoesKMove)
Combine the metadata of two instructions so that K can replace J.
Definition Local.cpp:3094
LLVM_ABI BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, bool PreserveLCSSA=false)
This method introduces at least one new basic block into the function and moves some of the predecess...
@ Other
Any other memory.
Definition ModRef.h:68
LLVM_ABI bool canReplaceOperandWithVariable(const Instruction *I, unsigned OpIdx)
Given an instruction, is it legal to set operand OpIdx to a non-constant value?
Definition Local.cpp:3871
DWARFExpression::Operation Op
auto max_element(R &&Range)
Provide wrappers to std::max_element which take ranges instead of having to pass begin/end explicitly...
Definition STLExtras.h:2030
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:560
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2120
auto predecessors(const MachineBasicBlock *BB)
bool pred_empty(const BasicBlock *BB)
Definition CFG.h:119
bool all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
Definition STLExtras.h:2108
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition Hashing.h:592
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition Hashing.h:466
#define N
static ModelledPHI & getTombstoneKey()
Definition GVNSink.cpp:263
static ModelledPHI & getEmptyKey()
Definition GVNSink.cpp:258
static bool isEqual(const ModelledPHI &LHS, const ModelledPHI &RHS)
Definition GVNSink.cpp:270
static unsigned getHashValue(const ModelledPHI &V)
Definition GVNSink.cpp:268
An information struct used to provide DenseMap with the various necessary components for a given valu...
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Run the pass over the function.
Definition GVNSink.cpp:854