LLVM 23.0.0git
GVN.cpp
Go to the documentation of this file.
1//===- GVN.cpp - Eliminate redundant values and loads ---------------------===//
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 pass performs global value numbering to eliminate fully redundant
10// instructions. It also performs simple dead load elimination.
11//
12// Note that this pass does the value numbering itself; it does not use the
13// ValueNumbering analysis passes.
14//
15//===----------------------------------------------------------------------===//
16
18#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/Hashing.h"
21#include "llvm/ADT/MapVector.h"
23#include "llvm/ADT/STLExtras.h"
24#include "llvm/ADT/SetVector.h"
27#include "llvm/ADT/Statistic.h"
31#include "llvm/Analysis/CFG.h"
36#include "llvm/Analysis/Loads.h"
46#include "llvm/IR/Attributes.h"
47#include "llvm/IR/BasicBlock.h"
48#include "llvm/IR/Constant.h"
49#include "llvm/IR/Constants.h"
50#include "llvm/IR/DebugLoc.h"
51#include "llvm/IR/Dominators.h"
52#include "llvm/IR/Function.h"
53#include "llvm/IR/InstrTypes.h"
54#include "llvm/IR/Instruction.h"
57#include "llvm/IR/LLVMContext.h"
58#include "llvm/IR/Metadata.h"
59#include "llvm/IR/Module.h"
60#include "llvm/IR/PassManager.h"
62#include "llvm/IR/Type.h"
63#include "llvm/IR/Use.h"
64#include "llvm/IR/Value.h"
66#include "llvm/Pass.h"
70#include "llvm/Support/Debug.h"
77#include <algorithm>
78#include <cassert>
79#include <cstdint>
80#include <optional>
81#include <utility>
82
83using namespace llvm;
84using namespace llvm::gvn;
85using namespace llvm::VNCoercion;
86using namespace PatternMatch;
87
88#define DEBUG_TYPE "gvn"
89
90STATISTIC(NumGVNInstr, "Number of instructions deleted");
91STATISTIC(NumGVNLoad, "Number of loads deleted");
92STATISTIC(NumGVNPRE, "Number of instructions PRE'd");
93STATISTIC(NumGVNBlocks, "Number of blocks merged");
94STATISTIC(NumGVNSimpl, "Number of instructions simplified");
95STATISTIC(NumGVNEqProp, "Number of equalities propagated");
96STATISTIC(NumPRELoad, "Number of loads PRE'd");
97STATISTIC(NumPRELoopLoad, "Number of loop loads PRE'd");
98STATISTIC(NumPRELoadMoved2CEPred,
99 "Number of loads moved to predecessor of a critical edge in PRE");
100
101STATISTIC(IsValueFullyAvailableInBlockNumSpeculationsMax,
102 "Number of blocks speculated as available in "
103 "IsValueFullyAvailableInBlock(), max");
104STATISTIC(MaxBBSpeculationCutoffReachedTimes,
105 "Number of times we we reached gvn-max-block-speculations cut-off "
106 "preventing further exploration");
107
108static cl::opt<bool> GVNEnableScalarPRE("enable-scalar-pre", cl::init(true),
109 cl::Hidden);
110static cl::opt<bool> GVNEnableLoadPRE("enable-load-pre", cl::init(true));
111static cl::opt<bool> GVNEnableLoadInLoopPRE("enable-load-in-loop-pre",
112 cl::init(true));
113static cl::opt<bool>
114GVNEnableSplitBackedgeInLoadPRE("enable-split-backedge-in-load-pre",
115 cl::init(false));
116static cl::opt<bool> GVNEnableMemDep("enable-gvn-memdep", cl::init(true));
117static cl::opt<bool> GVNEnableMemorySSA("enable-gvn-memoryssa",
118 cl::init(false));
119
121 "gvn-scan-users-limit", cl::Hidden, cl::init(100),
122 cl::desc("The number of memory accesses to scan in a block in reaching "
123 "memory values analysis (default = 100)"));
124
126 "gvn-max-num-deps", cl::Hidden, cl::init(100),
127 cl::desc("Max number of dependences to attempt Load PRE (default = 100)"));
128
129// This is based on IsValueFullyAvailableInBlockNumSpeculationsMax stat.
131 "gvn-max-block-speculations", cl::Hidden, cl::init(600),
132 cl::desc("Max number of blocks we're willing to speculate on (and recurse "
133 "into) when deducing if a value is fully available or not in GVN "
134 "(default = 600)"));
135
137 "gvn-max-num-visited-insts", cl::Hidden, cl::init(100),
138 cl::desc("Max number of visited instructions when trying to find "
139 "dominating value of select dependency (default = 100)"));
140
142 "gvn-max-num-insns", cl::Hidden, cl::init(100),
143 cl::desc("Max number of instructions to scan in each basic block in GVN "
144 "(default = 100)"));
145
148 bool Commutative = false;
149 // The type is not necessarily the result type of the expression, it may be
150 // any additional type needed to disambiguate the expression.
151 Type *Ty = nullptr;
153
154 AttributeList Attrs;
155
157
158 bool operator==(const Expression &Other) const {
159 if (Opcode != Other.Opcode)
160 return false;
161 if (Opcode == ~0U || Opcode == ~1U)
162 return true;
163 if (Ty != Other.Ty)
164 return false;
165 if (VarArgs != Other.VarArgs)
166 return false;
167 if ((!Attrs.isEmpty() || !Other.Attrs.isEmpty()) &&
168 !Attrs.intersectWith(Ty->getContext(), Other.Attrs).has_value())
169 return false;
170 return true;
171 }
172
174 return hash_combine(Value.Opcode, Value.Ty,
175 hash_combine_range(Value.VarArgs));
176 }
177};
178
180 static inline GVNPass::Expression getEmptyKey() { return ~0U; }
181
182 static unsigned getHashValue(const GVNPass::Expression &E) {
183 using llvm::hash_value;
184
185 return static_cast<unsigned>(hash_value(E));
186 }
187
188 static bool isEqual(const GVNPass::Expression &LHS,
189 const GVNPass::Expression &RHS) {
190 return LHS == RHS;
191 }
192};
193
194/// Represents a particular available value that we know how to materialize.
195/// Materialization of an AvailableValue never fails. An AvailableValue is
196/// implicitly associated with a rematerialization point which is the
197/// location of the instruction from which it was formed.
199 enum class ValType {
200 SimpleVal, // A simple offsetted value that is accessed.
201 LoadVal, // A value produced by a load.
202 MemIntrin, // A memory intrinsic which is loaded from.
203 UndefVal, // A UndefValue representing a value from dead block (which
204 // is not yet physically removed from the CFG).
205 SelectVal, // A pointer select which is loaded from and for which the load
206 // can be replace by a value select.
207 };
208
209 /// Val - The value that is live out of the block.
211 /// Kind of the live-out value.
213
214 /// Offset - The byte offset in Val that is interesting for the load query.
215 unsigned Offset = 0;
216 /// V1, V2 - The dominating non-clobbered values of SelectVal.
217 Value *V1 = nullptr, *V2 = nullptr;
218
219 static AvailableValue get(Value *V, unsigned Offset = 0) {
220 AvailableValue Res;
221 Res.Val = V;
223 Res.Offset = Offset;
224 return Res;
225 }
226
227 static AvailableValue getMI(MemIntrinsic *MI, unsigned Offset = 0) {
228 AvailableValue Res;
229 Res.Val = MI;
231 Res.Offset = Offset;
232 return Res;
233 }
234
235 static AvailableValue getLoad(LoadInst *Load, unsigned Offset = 0) {
236 AvailableValue Res;
237 Res.Val = Load;
239 Res.Offset = Offset;
240 return Res;
241 }
242
244 AvailableValue Res;
245 Res.Val = nullptr;
247 Res.Offset = 0;
248 return Res;
249 }
250
252 AvailableValue Res;
253 Res.Val = Sel;
255 Res.Offset = 0;
256 Res.V1 = V1;
257 Res.V2 = V2;
258 return Res;
259 }
260
261 bool isSimpleValue() const { return Kind == ValType::SimpleVal; }
262 bool isCoercedLoadValue() const { return Kind == ValType::LoadVal; }
263 bool isMemIntrinValue() const { return Kind == ValType::MemIntrin; }
264 bool isUndefValue() const { return Kind == ValType::UndefVal; }
265 bool isSelectValue() const { return Kind == ValType::SelectVal; }
266
268 assert(isSimpleValue() && "Wrong accessor");
269 return Val;
270 }
271
273 assert(isCoercedLoadValue() && "Wrong accessor");
274 return cast<LoadInst>(Val);
275 }
276
278 assert(isMemIntrinValue() && "Wrong accessor");
279 return cast<MemIntrinsic>(Val);
280 }
281
283 assert(isSelectValue() && "Wrong accessor");
284 return cast<SelectInst>(Val);
285 }
286
287 /// Emit code at the specified insertion point to adjust the value defined
288 /// here to the specified type. This handles various coercion cases.
289 Value *MaterializeAdjustedValue(LoadInst *Load, Instruction *InsertPt) const;
290};
291
292/// Represents an AvailableValue which can be rematerialized at the end of
293/// the associated BasicBlock.
295 /// BB - The basic block in question.
296 BasicBlock *BB = nullptr;
297
298 /// AV - The actual available value.
300
303 Res.BB = BB;
304 Res.AV = std::move(AV);
305 return Res;
306 }
307
309 unsigned Offset = 0) {
310 return get(BB, AvailableValue::get(V, Offset));
311 }
312
316
318 Value *V1, Value *V2) {
319 return get(BB, AvailableValue::getSelect(Sel, V1, V2));
320 }
321
322 /// Emit code at the end of this block to adjust the value defined here to
323 /// the specified type. This handles various coercion cases.
325 return AV.MaterializeAdjustedValue(Load, BB->getTerminator());
326 }
327};
328
329//===----------------------------------------------------------------------===//
330// ValueTable Internal Functions
331//===----------------------------------------------------------------------===//
332
333GVNPass::Expression GVNPass::ValueTable::createExpr(Instruction *I) {
334 Expression E;
335 E.Ty = I->getType();
336 E.Opcode = I->getOpcode();
337 if (const GCRelocateInst *GCR = dyn_cast<GCRelocateInst>(I)) {
338 // gc.relocate is 'special' call: its second and third operands are
339 // not real values, but indices into statepoint's argument list.
340 // Use the refered to values for purposes of identity.
341 E.VarArgs.push_back(lookupOrAdd(GCR->getOperand(0)));
342 E.VarArgs.push_back(lookupOrAdd(GCR->getBasePtr()));
343 E.VarArgs.push_back(lookupOrAdd(GCR->getDerivedPtr()));
344 } else {
345 for (Use &Op : I->operands())
346 E.VarArgs.push_back(lookupOrAdd(Op));
347 }
348 if (I->isCommutative()) {
349 // Ensure that commutative instructions that only differ by a permutation
350 // of their operands get the same value number by sorting the operand value
351 // numbers. Since commutative operands are the 1st two operands it is more
352 // efficient to sort by hand rather than using, say, std::sort.
353 assert(I->getNumOperands() >= 2 && "Unsupported commutative instruction!");
354 if (E.VarArgs[0] > E.VarArgs[1])
355 std::swap(E.VarArgs[0], E.VarArgs[1]);
356 E.Commutative = true;
357 }
358
359 if (auto *C = dyn_cast<CmpInst>(I)) {
360 // Sort the operand value numbers so x<y and y>x get the same value number.
361 CmpInst::Predicate Predicate = C->getPredicate();
362 if (E.VarArgs[0] > E.VarArgs[1]) {
363 std::swap(E.VarArgs[0], E.VarArgs[1]);
365 }
366 E.Opcode = (C->getOpcode() << 8) | Predicate;
367 E.Commutative = true;
368 } else if (auto *IVI = dyn_cast<InsertValueInst>(I)) {
369 E.VarArgs.append(IVI->idx_begin(), IVI->idx_end());
370 } else if (auto *SVI = dyn_cast<ShuffleVectorInst>(I)) {
371 ArrayRef<int> ShuffleMask = SVI->getShuffleMask();
372 E.VarArgs.append(ShuffleMask.begin(), ShuffleMask.end());
373 } else if (auto *CB = dyn_cast<CallBase>(I)) {
374 E.Attrs = CB->getAttributes();
375 }
376
377 return E;
378}
379
380GVNPass::Expression GVNPass::ValueTable::createCmpExpr(
381 unsigned Opcode, CmpInst::Predicate Predicate, Value *LHS, Value *RHS) {
382 assert((Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) &&
383 "Not a comparison!");
386 E.VarArgs.push_back(lookupOrAdd(LHS));
387 E.VarArgs.push_back(lookupOrAdd(RHS));
388
389 // Sort the operand value numbers so x<y and y>x get the same value number.
390 if (E.VarArgs[0] > E.VarArgs[1]) {
391 std::swap(E.VarArgs[0], E.VarArgs[1]);
393 }
394 E.Opcode = (Opcode << 8) | Predicate;
395 E.Commutative = true;
396 return E;
397}
398
399GVNPass::Expression
400GVNPass::ValueTable::createExtractvalueExpr(ExtractValueInst *EI) {
401 assert(EI && "Not an ExtractValueInst?");
403 E.Ty = EI->getType();
404 E.Opcode = 0;
405
406 WithOverflowInst *WO = dyn_cast<WithOverflowInst>(EI->getAggregateOperand());
407 if (WO != nullptr && EI->getNumIndices() == 1 && *EI->idx_begin() == 0) {
408 // EI is an extract from one of our with.overflow intrinsics. Synthesize
409 // a semantically equivalent expression instead of an extract value
410 // expression.
411 E.Opcode = WO->getBinaryOp();
412 E.VarArgs.push_back(lookupOrAdd(WO->getLHS()));
413 E.VarArgs.push_back(lookupOrAdd(WO->getRHS()));
414 return E;
415 }
416
417 // Not a recognised intrinsic. Fall back to producing an extract value
418 // expression.
419 E.Opcode = EI->getOpcode();
420 for (Use &Op : EI->operands())
421 E.VarArgs.push_back(lookupOrAdd(Op));
422
423 append_range(E.VarArgs, EI->indices());
424
425 return E;
426}
427
428GVNPass::Expression GVNPass::ValueTable::createGEPExpr(GetElementPtrInst *GEP) {
430 Type *PtrTy = GEP->getType()->getScalarType();
431 const DataLayout &DL = GEP->getDataLayout();
432 unsigned BitWidth = DL.getIndexTypeSizeInBits(PtrTy);
433 SmallMapVector<Value *, APInt, 4> VariableOffsets;
434 APInt ConstantOffset(BitWidth, 0);
435 if (GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset)) {
436 // Convert into offset representation, to recognize equivalent address
437 // calculations that use different type encoding.
438 LLVMContext &Context = GEP->getContext();
439 E.Opcode = GEP->getOpcode();
440 E.Ty = nullptr;
441 E.VarArgs.push_back(lookupOrAdd(GEP->getPointerOperand()));
442 for (const auto &[V, Scale] : VariableOffsets) {
443 E.VarArgs.push_back(lookupOrAdd(V));
444 E.VarArgs.push_back(lookupOrAdd(ConstantInt::get(Context, Scale)));
445 }
446 if (!ConstantOffset.isZero())
447 E.VarArgs.push_back(
448 lookupOrAdd(ConstantInt::get(Context, ConstantOffset)));
449 } else {
450 // If converting to offset representation fails (for scalable vectors),
451 // fall back to type-based implementation.
452 E.Opcode = GEP->getOpcode();
453 E.Ty = GEP->getSourceElementType();
454 for (Use &Op : GEP->operands())
455 E.VarArgs.push_back(lookupOrAdd(Op));
456 }
457 return E;
458}
459
460//===----------------------------------------------------------------------===//
461// ValueTable External Functions
462//===----------------------------------------------------------------------===//
463
464GVNPass::ValueTable::ValueTable() = default;
465GVNPass::ValueTable::ValueTable(const ValueTable &) = default;
466GVNPass::ValueTable::ValueTable(ValueTable &&) = default;
467GVNPass::ValueTable::~ValueTable() = default;
468GVNPass::ValueTable &
469GVNPass::ValueTable::operator=(const GVNPass::ValueTable &Arg) = default;
470
471/// add - Insert a value into the table with a specified value number.
472void GVNPass::ValueTable::add(Value *V, uint32_t Num) {
473 ValueNumbering.insert(std::make_pair(V, Num));
474 if (PHINode *PN = dyn_cast<PHINode>(V))
475 NumberingPhi[Num] = PN;
476}
477
478/// Include the incoming memory state into the hash of the expression for the
479/// given instruction. If the incoming memory state is:
480/// * LiveOnEntry, add the value number of the entry block,
481/// * a MemoryPhi, add the value number of the basic block corresponding to that
482/// MemoryPhi,
483/// * a MemoryDef, add the value number of the memory setting instruction.
484void GVNPass::ValueTable::addMemoryStateToExp(Instruction *I, Expression &Exp) {
485 assert(MSSA && "addMemoryStateToExp should not be called without MemorySSA");
486 assert(MSSA->getMemoryAccess(I) && "Instruction does not access memory");
487 MemoryAccess *MA = MSSA->getSkipSelfWalker()->getClobberingMemoryAccess(I);
488 Exp.VarArgs.push_back(lookupOrAdd(MA));
489}
490
491uint32_t GVNPass::ValueTable::lookupOrAddCall(CallInst *C) {
492 // FIXME: Currently the calls which may access the thread id may
493 // be considered as not accessing the memory. But this is
494 // problematic for coroutines, since coroutines may resume in a
495 // different thread. So we disable the optimization here for the
496 // correctness. However, it may block many other correct
497 // optimizations. Revert this one when we detect the memory
498 // accessing kind more precisely.
499 if (C->getFunction()->isPresplitCoroutine()) {
500 ValueNumbering[C] = NextValueNumber;
501 return NextValueNumber++;
502 }
503
504 // Do not combine convergent calls since they implicitly depend on the set of
505 // threads that is currently executing, and they might be in different basic
506 // blocks.
507 if (C->isConvergent()) {
508 ValueNumbering[C] = NextValueNumber;
509 return NextValueNumber++;
510 }
511
512 if (AA->doesNotAccessMemory(C)) {
513 Expression Exp = createExpr(C);
514 uint32_t E = assignExpNewValueNum(Exp).first;
515 ValueNumbering[C] = E;
516 return E;
517 }
518
519 if (MD && AA->onlyReadsMemory(C)) {
520 Expression Exp = createExpr(C);
521 auto [E, IsValNumNew] = assignExpNewValueNum(Exp);
522 if (IsValNumNew) {
523 ValueNumbering[C] = E;
524 return E;
525 }
526
527 MemDepResult LocalDep = MD->getDependency(C);
528
529 if (!LocalDep.isDef() && !LocalDep.isNonLocal()) {
530 ValueNumbering[C] = NextValueNumber;
531 return NextValueNumber++;
532 }
533
534 if (LocalDep.isDef()) {
535 // For masked load/store intrinsics, the local_dep may actually be
536 // a normal load or store instruction.
537 CallInst *LocalDepCall = dyn_cast<CallInst>(LocalDep.getInst());
538
539 if (!LocalDepCall || LocalDepCall->arg_size() != C->arg_size()) {
540 ValueNumbering[C] = NextValueNumber;
541 return NextValueNumber++;
542 }
543
544 for (unsigned I = 0, E = C->arg_size(); I < E; ++I) {
545 uint32_t CVN = lookupOrAdd(C->getArgOperand(I));
546 uint32_t LocalDepCallVN = lookupOrAdd(LocalDepCall->getArgOperand(I));
547 if (CVN != LocalDepCallVN) {
548 ValueNumbering[C] = NextValueNumber;
549 return NextValueNumber++;
550 }
551 }
552
553 uint32_t V = lookupOrAdd(LocalDepCall);
554 ValueNumbering[C] = V;
555 return V;
556 }
557
558 // Non-local case.
560 MD->getNonLocalCallDependency(C);
561 // FIXME: Move the checking logic to MemDep!
562 CallInst *CDep = nullptr;
563
564 // Check to see if we have a single dominating call instruction that is
565 // identical to C.
566 for (const NonLocalDepEntry &I : Deps) {
567 if (I.getResult().isNonLocal())
568 continue;
569
570 // We don't handle non-definitions. If we already have a call, reject
571 // instruction dependencies.
572 if (!I.getResult().isDef() || CDep != nullptr) {
573 CDep = nullptr;
574 break;
575 }
576
577 CallInst *NonLocalDepCall = dyn_cast<CallInst>(I.getResult().getInst());
578 // FIXME: All duplicated with non-local case.
579 if (NonLocalDepCall && DT->properlyDominates(I.getBB(), C->getParent())) {
580 CDep = NonLocalDepCall;
581 continue;
582 }
583
584 CDep = nullptr;
585 break;
586 }
587
588 if (!CDep) {
589 ValueNumbering[C] = NextValueNumber;
590 return NextValueNumber++;
591 }
592
593 if (CDep->arg_size() != C->arg_size()) {
594 ValueNumbering[C] = NextValueNumber;
595 return NextValueNumber++;
596 }
597 for (unsigned I = 0, E = C->arg_size(); I < E; ++I) {
598 uint32_t CVN = lookupOrAdd(C->getArgOperand(I));
599 uint32_t CDepVN = lookupOrAdd(CDep->getArgOperand(I));
600 if (CVN != CDepVN) {
601 ValueNumbering[C] = NextValueNumber;
602 return NextValueNumber++;
603 }
604 }
605
606 uint32_t V = lookupOrAdd(CDep);
607 ValueNumbering[C] = V;
608 return V;
609 }
610
611 if (MSSA && IsMSSAEnabled && AA->onlyReadsMemory(C)) {
612 Expression Exp = createExpr(C);
613 addMemoryStateToExp(C, Exp);
614 auto [V, _] = assignExpNewValueNum(Exp);
615 ValueNumbering[C] = V;
616 return V;
617 }
618
619 ValueNumbering[C] = NextValueNumber;
620 return NextValueNumber++;
621}
622
623/// Returns the value number for the specified load or store instruction.
624uint32_t GVNPass::ValueTable::computeLoadStoreVN(Instruction *I) {
625 if (!MSSA || !IsMSSAEnabled) {
626 ValueNumbering[I] = NextValueNumber;
627 return NextValueNumber++;
628 }
629
631 Exp.Ty = I->getType();
632 Exp.Opcode = I->getOpcode();
633 for (Use &Op : I->operands())
634 Exp.VarArgs.push_back(lookupOrAdd(Op));
635 addMemoryStateToExp(I, Exp);
636
637 auto [V, _] = assignExpNewValueNum(Exp);
638 ValueNumbering[I] = V;
639 return V;
640}
641
642/// Returns true if a value number exists for the specified value.
643bool GVNPass::ValueTable::exists(Value *V) const {
644 return ValueNumbering.contains(V);
645}
646
647uint32_t GVNPass::ValueTable::lookupOrAdd(MemoryAccess *MA) {
648 return MSSA->isLiveOnEntryDef(MA) || isa<MemoryPhi>(MA)
649 ? lookupOrAdd(MA->getBlock())
650 : lookupOrAdd(cast<MemoryUseOrDef>(MA)->getMemoryInst());
651}
652
653/// lookupOrAdd - Returns the value number for the specified value, assigning
654/// it a new number if it did not have one before.
655uint32_t GVNPass::ValueTable::lookupOrAdd(Value *V) {
656 auto VI = ValueNumbering.find(V);
657 if (VI != ValueNumbering.end())
658 return VI->second;
659
660 auto *I = dyn_cast<Instruction>(V);
661 if (!I) {
662 ValueNumbering[V] = NextValueNumber;
663 if (isa<BasicBlock>(V))
664 NumberingBB[NextValueNumber] = cast<BasicBlock>(V);
665 return NextValueNumber++;
666 }
667
668 Expression Exp;
669 switch (I->getOpcode()) {
670 case Instruction::Call:
671 return lookupOrAddCall(cast<CallInst>(I));
672 case Instruction::FNeg:
673 case Instruction::Add:
674 case Instruction::FAdd:
675 case Instruction::Sub:
676 case Instruction::FSub:
677 case Instruction::Mul:
678 case Instruction::FMul:
679 case Instruction::UDiv:
680 case Instruction::SDiv:
681 case Instruction::FDiv:
682 case Instruction::URem:
683 case Instruction::SRem:
684 case Instruction::FRem:
685 case Instruction::Shl:
686 case Instruction::LShr:
687 case Instruction::AShr:
688 case Instruction::And:
689 case Instruction::Or:
690 case Instruction::Xor:
691 case Instruction::ICmp:
692 case Instruction::FCmp:
693 case Instruction::Trunc:
694 case Instruction::ZExt:
695 case Instruction::SExt:
696 case Instruction::FPToUI:
697 case Instruction::FPToSI:
698 case Instruction::UIToFP:
699 case Instruction::SIToFP:
700 case Instruction::FPTrunc:
701 case Instruction::FPExt:
702 case Instruction::PtrToInt:
703 case Instruction::PtrToAddr:
704 case Instruction::IntToPtr:
705 case Instruction::AddrSpaceCast:
706 case Instruction::BitCast:
707 case Instruction::Select:
708 case Instruction::Freeze:
709 case Instruction::ExtractElement:
710 case Instruction::InsertElement:
711 case Instruction::ShuffleVector:
712 case Instruction::InsertValue:
713 Exp = createExpr(I);
714 break;
715 case Instruction::GetElementPtr:
716 Exp = createGEPExpr(cast<GetElementPtrInst>(I));
717 break;
718 case Instruction::ExtractValue:
719 Exp = createExtractvalueExpr(cast<ExtractValueInst>(I));
720 break;
721 case Instruction::PHI:
722 ValueNumbering[V] = NextValueNumber;
723 NumberingPhi[NextValueNumber] = cast<PHINode>(V);
724 return NextValueNumber++;
725 case Instruction::Load:
726 case Instruction::Store:
727 return computeLoadStoreVN(I);
728 default:
729 ValueNumbering[V] = NextValueNumber;
730 return NextValueNumber++;
731 }
732
733 uint32_t E = assignExpNewValueNum(Exp).first;
734 ValueNumbering[V] = E;
735 return E;
736}
737
738/// Returns the value number of the specified value. Fails if
739/// the value has not yet been numbered.
740uint32_t GVNPass::ValueTable::lookup(Value *V, bool Verify) const {
741 auto VI = ValueNumbering.find(V);
742 if (Verify) {
743 assert(VI != ValueNumbering.end() && "Value not numbered?");
744 return VI->second;
745 }
746 return (VI != ValueNumbering.end()) ? VI->second : 0;
747}
748
749/// Returns the value number of the given comparison,
750/// assigning it a new number if it did not have one before. Useful when
751/// we deduced the result of a comparison, but don't immediately have an
752/// instruction realizing that comparison to hand.
753uint32_t GVNPass::ValueTable::lookupOrAddCmp(unsigned Opcode,
754 CmpInst::Predicate Predicate,
755 Value *LHS, Value *RHS) {
756 Expression Exp = createCmpExpr(Opcode, Predicate, LHS, RHS);
757 return assignExpNewValueNum(Exp).first;
758}
759
760/// Remove all entries from the ValueTable.
762 ValueNumbering.clear();
763 ExpressionNumbering.clear();
764 NumberingPhi.clear();
765 NumberingBB.clear();
766 PhiTranslateTable.clear();
767 NextValueNumber = 1;
768 Expressions.clear();
769 ExprIdx.clear();
770 NextExprNumber = 0;
771}
772
773/// Remove a value from the value numbering.
775 uint32_t Num = ValueNumbering.lookup(V);
776 ValueNumbering.erase(V);
777 // If V is PHINode, V <--> value number is an one-to-one mapping.
778 if (isa<PHINode>(V))
779 NumberingPhi.erase(Num);
780 else if (isa<BasicBlock>(V))
781 NumberingBB.erase(Num);
782}
783
784/// verifyRemoved - Verify that the value is removed from all internal data
785/// structures.
786void GVNPass::ValueTable::verifyRemoved(const Value *V) const {
787 assert(!ValueNumbering.contains(V) &&
788 "Inst still occurs in value numbering map!");
789}
790
791//===----------------------------------------------------------------------===//
792// LeaderMap External Functions
793//===----------------------------------------------------------------------===//
794
795/// Push a new Value to the LeaderTable onto the list for its value number.
796void GVNPass::LeaderMap::insert(uint32_t N, Value *V, const BasicBlock *BB) {
797 const auto &[It, Inserted] = NumToLeaders.try_emplace(N, V, BB, nullptr);
798 if (!Inserted) {
799 // Key already exists: insert new node after the head.
800 auto *NewSlot = TableAllocator.Allocate<LeaderListNode>();
801 new (NewSlot) LeaderListNode(V, BB, It->second.Next);
802 It->second.Next = NewSlot;
803 }
804}
805
806/// Scan the list of values corresponding to a given
807/// value number, and remove the given instruction if encountered.
808void GVNPass::LeaderMap::erase(uint32_t N, Instruction *I,
809 const BasicBlock *BB) {
810 auto It = NumToLeaders.find(N);
811 if (It == NumToLeaders.end())
812 return;
813
814 LeaderListNode *Prev = nullptr;
815 LeaderListNode *Curr = &It->second;
816
817 while (Curr && (Curr->Entry.Val != I || Curr->Entry.BB != BB)) {
818 Prev = Curr;
819 Curr = Curr->Next;
820 }
821
822 if (!Curr)
823 return;
824
825 if (Prev) {
826 // Non-head node: unlink and destroy.
827 Prev->Next = Curr->Next;
828 Curr->~LeaderListNode();
829 TableAllocator.Deallocate<LeaderListNode>(Curr);
830 } else {
831 // Head node (stored by value in DenseMap).
832 if (!Curr->Next) {
833 // Only node; erase from map (DenseMap calls the destructor).
834 NumToLeaders.erase(It);
835 } else {
836 // Move second node's data into head, then destroy second node.
837 LeaderListNode *Next = Curr->Next;
838 Curr->Entry.Val = std::move(Next->Entry.Val);
839 Curr->Entry.BB = Next->Entry.BB;
840 Curr->Next = Next->Next;
841 Next->~LeaderListNode();
842 TableAllocator.Deallocate<LeaderListNode>(Next);
843 }
844 }
845}
846
847//===----------------------------------------------------------------------===//
848// GVN Pass
849//===----------------------------------------------------------------------===//
850
852 return Options.AllowScalarPRE.value_or(GVNEnableScalarPRE);
853}
854
856 return Options.AllowLoadPRE.value_or(GVNEnableLoadPRE);
857}
858
860 return Options.AllowLoadInLoopPRE.value_or(GVNEnableLoadInLoopPRE);
861}
862
864 return Options.AllowLoadPRESplitBackedge.value_or(
866}
867
869 return Options.AllowMemDep.value_or(GVNEnableMemDep);
870}
871
873 return Options.AllowMemorySSA.value_or(GVNEnableMemorySSA);
874}
875
877 // FIXME: The order of evaluation of these 'getResult' calls is very
878 // significant! Re-ordering these variables will cause GVN when run alone to
879 // be less effective! We should fix memdep and basic-aa to not exhibit this
880 // behavior, but until then don't change the order here.
881 auto &AC = AM.getResult<AssumptionAnalysis>(F);
882 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
883 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
884 auto &AA = AM.getResult<AAManager>(F);
885 auto *MemDep =
887 auto &LI = AM.getResult<LoopAnalysis>(F);
888 auto *MSSA = AM.getCachedResult<MemorySSAAnalysis>(F);
889 if (isMemorySSAEnabled() && !MSSA) {
890 assert(!MemDep &&
891 "On-demand computation of MemSSA implies that MemDep is disabled!");
892 MSSA = &AM.getResult<MemorySSAAnalysis>(F);
893 }
895 bool Changed = runImpl(F, AC, DT, TLI, AA, MemDep, LI, &ORE,
896 MSSA ? &MSSA->getMSSA() : nullptr);
897 if (!Changed)
898 return PreservedAnalyses::all();
902 if (MSSA)
905 return PA;
906}
907
909 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
910 static_cast<PassInfoMixin<GVNPass> *>(this)->printPipeline(
911 OS, MapClassName2PassName);
912
913 OS << '<';
914 if (Options.AllowScalarPRE != std::nullopt)
915 OS << (*Options.AllowScalarPRE ? "" : "no-") << "scalar-pre;";
916 if (Options.AllowLoadPRE != std::nullopt)
917 OS << (*Options.AllowLoadPRE ? "" : "no-") << "load-pre;";
918 if (Options.AllowLoadPRESplitBackedge != std::nullopt)
919 OS << (*Options.AllowLoadPRESplitBackedge ? "" : "no-")
920 << "split-backedge-load-pre;";
921 if (Options.AllowMemDep != std::nullopt)
922 OS << (*Options.AllowMemDep ? "" : "no-") << "memdep;";
923 if (Options.AllowMemorySSA != std::nullopt)
924 OS << (*Options.AllowMemorySSA ? "" : "no-") << "memoryssa";
925 OS << '>';
926}
927
929 salvageKnowledge(I, AC);
931 removeInstruction(I);
932}
933
934#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
935LLVM_DUMP_METHOD void GVNPass::dump(DenseMap<uint32_t, Value *> &Map) const {
936 errs() << "{\n";
937 for (const auto &[Num, Exp] : Map) {
938 errs() << Num << "\n";
939 Exp->dump();
940 }
941 errs() << "}\n";
942}
943#endif
944
945enum class AvailabilityState : char {
946 /// We know the block *is not* fully available. This is a fixpoint.
948 /// We know the block *is* fully available. This is a fixpoint.
950 /// We do not know whether the block is fully available or not,
951 /// but we are currently speculating that it will be.
952 /// If it would have turned out that the block was, in fact, not fully
953 /// available, this would have been cleaned up into an Unavailable.
955};
956
957/// Return true if we can prove that the value
958/// we're analyzing is fully available in the specified block. As we go, keep
959/// track of which blocks we know are fully alive in FullyAvailableBlocks. This
960/// map is actually a tri-state map with the following values:
961/// 0) we know the block *is not* fully available.
962/// 1) we know the block *is* fully available.
963/// 2) we do not know whether the block is fully available or not, but we are
964/// currently speculating that it will be.
966 BasicBlock *BB,
967 DenseMap<BasicBlock *, AvailabilityState> &FullyAvailableBlocks) {
969 std::optional<BasicBlock *> UnavailableBB;
970
971 // The number of times we didn't find an entry for a block in a map and
972 // optimistically inserted an entry marking block as speculatively available.
973 unsigned NumNewNewSpeculativelyAvailableBBs = 0;
974
975#ifndef NDEBUG
976 SmallPtrSet<BasicBlock *, 32> NewSpeculativelyAvailableBBs;
978#endif
979
980 Worklist.emplace_back(BB);
981 while (!Worklist.empty()) {
982 BasicBlock *CurrBB = Worklist.pop_back_val(); // LoadFO - depth-first!
983 // Optimistically assume that the block is Speculatively Available and check
984 // to see if we already know about this block in one lookup.
985 std::pair<DenseMap<BasicBlock *, AvailabilityState>::iterator, bool> IV =
986 FullyAvailableBlocks.try_emplace(
988 AvailabilityState &State = IV.first->second;
989
990 // Did the entry already exist for this block?
991 if (!IV.second) {
992 if (State == AvailabilityState::Unavailable) {
993 UnavailableBB = CurrBB;
994 break; // Backpropagate unavailability info.
995 }
996
997#ifndef NDEBUG
998 AvailableBBs.emplace_back(CurrBB);
999#endif
1000 continue; // Don't recurse further, but continue processing worklist.
1001 }
1002
1003 // No entry found for block.
1004 ++NumNewNewSpeculativelyAvailableBBs;
1005 bool OutOfBudget = NumNewNewSpeculativelyAvailableBBs > MaxBBSpeculations;
1006
1007 // If we have exhausted our budget, mark this block as unavailable.
1008 // Also, if this block has no predecessors, the value isn't live-in here.
1009 if (OutOfBudget || pred_empty(CurrBB)) {
1010 MaxBBSpeculationCutoffReachedTimes += (int)OutOfBudget;
1012 UnavailableBB = CurrBB;
1013 break; // Backpropagate unavailability info.
1014 }
1015
1016 // Tentatively consider this block as speculatively available.
1017#ifndef NDEBUG
1018 NewSpeculativelyAvailableBBs.insert(CurrBB);
1019#endif
1020 // And further recurse into block's predecessors, in depth-first order!
1021 Worklist.append(pred_begin(CurrBB), pred_end(CurrBB));
1022 }
1023
1024#if LLVM_ENABLE_STATS
1025 IsValueFullyAvailableInBlockNumSpeculationsMax.updateMax(
1026 NumNewNewSpeculativelyAvailableBBs);
1027#endif
1028
1029 // If the block isn't marked as fixpoint yet
1030 // (the Unavailable and Available states are fixpoints).
1031 auto MarkAsFixpointAndEnqueueSuccessors =
1032 [&](BasicBlock *BB, AvailabilityState FixpointState) {
1033 auto It = FullyAvailableBlocks.find(BB);
1034 if (It == FullyAvailableBlocks.end())
1035 return; // Never queried this block, leave as-is.
1036 switch (AvailabilityState &State = It->second) {
1039 return; // Don't backpropagate further, continue processing worklist.
1041 State = FixpointState;
1042#ifndef NDEBUG
1043 assert(NewSpeculativelyAvailableBBs.erase(BB) &&
1044 "Found a speculatively available successor leftover?");
1045#endif
1046 // Queue successors for further processing.
1047 Worklist.append(succ_begin(BB), succ_end(BB));
1048 return;
1049 }
1050 };
1051
1052 if (UnavailableBB) {
1053 // Okay, we have encountered an unavailable block.
1054 // Mark speculatively available blocks reachable from UnavailableBB as
1055 // unavailable as well. Paths are terminated when they reach blocks not in
1056 // FullyAvailableBlocks or they are not marked as speculatively available.
1057 Worklist.clear();
1058 Worklist.append(succ_begin(*UnavailableBB), succ_end(*UnavailableBB));
1059 while (!Worklist.empty())
1060 MarkAsFixpointAndEnqueueSuccessors(Worklist.pop_back_val(),
1062 }
1063
1064#ifndef NDEBUG
1065 Worklist.clear();
1066 for (BasicBlock *AvailableBB : AvailableBBs)
1067 Worklist.append(succ_begin(AvailableBB), succ_end(AvailableBB));
1068 while (!Worklist.empty())
1069 MarkAsFixpointAndEnqueueSuccessors(Worklist.pop_back_val(),
1071
1072 assert(NewSpeculativelyAvailableBBs.empty() &&
1073 "Must have fixed all the new speculatively available blocks.");
1074#endif
1075
1076 return !UnavailableBB;
1077}
1078
1079/// If the specified OldValue exists in ValuesPerBlock, replace its value with
1080/// NewValue.
1082 SmallVectorImpl<AvailableValueInBlock> &ValuesPerBlock, Value *OldValue,
1083 Value *NewValue) {
1084 for (AvailableValueInBlock &V : ValuesPerBlock) {
1085 if (V.AV.Val == OldValue)
1086 V.AV.Val = NewValue;
1087 if (V.AV.isSelectValue()) {
1088 if (V.AV.V1 == OldValue)
1089 V.AV.V1 = NewValue;
1090 if (V.AV.V2 == OldValue)
1091 V.AV.V2 = NewValue;
1092 }
1093 }
1094}
1095
1096/// Given a set of loads specified by ValuesPerBlock,
1097/// construct SSA form, allowing us to eliminate Load. This returns the value
1098/// that should be used at Load's definition site.
1099static Value *
1102 GVNPass &GVN) {
1103 // Check for the fully redundant, dominating load case. In this case, we can
1104 // just use the dominating value directly.
1105 if (ValuesPerBlock.size() == 1 &&
1106 GVN.getDominatorTree().properlyDominates(ValuesPerBlock[0].BB,
1107 Load->getParent())) {
1108 assert(!ValuesPerBlock[0].AV.isUndefValue() &&
1109 "Dead BB dominate this block");
1110 return ValuesPerBlock[0].MaterializeAdjustedValue(Load);
1111 }
1112
1113 // Otherwise, we have to construct SSA form.
1115 SSAUpdater SSAUpdate(&NewPHIs);
1116 SSAUpdate.Initialize(Load->getType(), Load->getName());
1117
1118 for (const AvailableValueInBlock &AV : ValuesPerBlock) {
1119 BasicBlock *BB = AV.BB;
1120
1121 if (AV.AV.isUndefValue())
1122 continue;
1123
1124 if (SSAUpdate.HasValueForBlock(BB))
1125 continue;
1126
1127 // If the value is the load that we will be eliminating, and the block it's
1128 // available in is the block that the load is in, then don't add it as
1129 // SSAUpdater will resolve the value to the relevant phi which may let it
1130 // avoid phi construction entirely if there's actually only one value.
1131 if (BB == Load->getParent() &&
1132 ((AV.AV.isSimpleValue() && AV.AV.getSimpleValue() == Load) ||
1133 (AV.AV.isCoercedLoadValue() && AV.AV.getCoercedLoadValue() == Load)))
1134 continue;
1135
1136 SSAUpdate.AddAvailableValue(BB, AV.MaterializeAdjustedValue(Load));
1137 }
1138
1139 // Perform PHI construction.
1140 return SSAUpdate.GetValueInMiddleOfBlock(Load->getParent());
1141}
1142
1144 Instruction *InsertPt) const {
1145 Value *Res;
1146 Type *LoadTy = Load->getType();
1147 const DataLayout &DL = Load->getDataLayout();
1148 if (isSimpleValue()) {
1149 Res = getSimpleValue();
1150 if (Res->getType() != LoadTy) {
1151 Res = getValueForLoad(Res, Offset, LoadTy, InsertPt, Load->getFunction());
1152
1153 LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL VAL:\nOffset: " << Offset
1154 << " " << *getSimpleValue() << '\n'
1155 << *Res << '\n'
1156 << "\n\n\n");
1157 }
1158 } else if (isCoercedLoadValue()) {
1159 LoadInst *CoercedLoad = getCoercedLoadValue();
1160 if (CoercedLoad->getType() == LoadTy && Offset == 0) {
1161 Res = CoercedLoad;
1162 combineMetadataForCSE(CoercedLoad, Load, false);
1163 } else {
1164 Res = getValueForLoad(CoercedLoad, Offset, LoadTy, InsertPt,
1165 Load->getFunction());
1166 // We are adding a new user for this load, for which the original
1167 // metadata may not hold. Additionally, the new load may have a different
1168 // size and type, so their metadata cannot be combined in any
1169 // straightforward way.
1170 // Drop all metadata that is not known to cause immediate UB on violation,
1171 // unless the load has !noundef, in which case all metadata violations
1172 // will be promoted to UB.
1173 // TODO: We can combine noalias/alias.scope metadata here, because it is
1174 // independent of the load type.
1175 if (!CoercedLoad->hasMetadata(LLVMContext::MD_noundef))
1176 CoercedLoad->dropUnknownNonDebugMetadata(
1177 {LLVMContext::MD_dereferenceable,
1178 LLVMContext::MD_dereferenceable_or_null,
1179 LLVMContext::MD_invariant_load, LLVMContext::MD_invariant_group});
1180 LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL LOAD:\nOffset: " << Offset
1181 << " " << *getCoercedLoadValue() << '\n'
1182 << *Res << '\n'
1183 << "\n\n\n");
1184 }
1185 } else if (isMemIntrinValue()) {
1187 InsertPt, DL);
1188 LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL MEM INTRIN:\nOffset: " << Offset
1189 << " " << *getMemIntrinValue() << '\n'
1190 << *Res << '\n'
1191 << "\n\n\n");
1192 } else if (isSelectValue()) {
1193 // Introduce a new value select for a load from an eligible pointer select.
1194 SelectInst *Sel = getSelectValue();
1195 assert(V1 && V2 && "both value operands of the select must be present");
1196 Res =
1197 SelectInst::Create(Sel->getCondition(), V1, V2, "", Sel->getIterator());
1198 // We use the DebugLoc from the original load here, as this instruction
1199 // materializes the value that would previously have been loaded.
1200 cast<SelectInst>(Res)->setDebugLoc(Load->getDebugLoc());
1201 } else {
1202 llvm_unreachable("Should not materialize value from dead block");
1203 }
1204 assert(Res && "failed to materialize?");
1205 return Res;
1206}
1207
1208static bool isLifetimeStart(const Instruction *Inst) {
1209 if (const IntrinsicInst* II = dyn_cast<IntrinsicInst>(Inst))
1210 return II->getIntrinsicID() == Intrinsic::lifetime_start;
1211 return false;
1212}
1213
1214/// Assuming To can be reached from both From and Between, does Between lie on
1215/// every path from From to To?
1216static bool liesBetween(const Instruction *From, Instruction *Between,
1217 const Instruction *To, const DominatorTree *DT) {
1218 if (From->getParent() == Between->getParent())
1219 return DT->dominates(From, Between);
1221 Exclusion.insert(Between->getParent());
1222 return !isPotentiallyReachable(From, To, &Exclusion, DT);
1223}
1224
1226 const DominatorTree *DT) {
1227 Value *PtrOp = Load->getPointerOperand();
1228 if (!PtrOp->hasUseList())
1229 return nullptr;
1230
1231 Instruction *OtherAccess = nullptr;
1232
1233 for (auto *U : PtrOp->users()) {
1234 if (U != Load && (isa<LoadInst>(U) || isa<StoreInst>(U))) {
1235 auto *I = cast<Instruction>(U);
1236 if (I->getFunction() == Load->getFunction() && DT->dominates(I, Load)) {
1237 // Use the most immediately dominating value.
1238 if (OtherAccess) {
1239 if (DT->dominates(OtherAccess, I))
1240 OtherAccess = I;
1241 else
1242 assert(U == OtherAccess || DT->dominates(I, OtherAccess));
1243 } else
1244 OtherAccess = I;
1245 }
1246 }
1247 }
1248
1249 if (OtherAccess)
1250 return OtherAccess;
1251
1252 // There is no dominating use, check if we can find a closest non-dominating
1253 // use that lies between any other potentially available use and Load.
1254 for (auto *U : PtrOp->users()) {
1255 if (U != Load && (isa<LoadInst>(U) || isa<StoreInst>(U))) {
1256 auto *I = cast<Instruction>(U);
1257 if (I->getFunction() == Load->getFunction() &&
1258 isPotentiallyReachable(I, Load, nullptr, DT)) {
1259 if (OtherAccess) {
1260 if (liesBetween(OtherAccess, I, Load, DT)) {
1261 OtherAccess = I;
1262 } else if (!liesBetween(I, OtherAccess, Load, DT)) {
1263 // These uses are both partially available at Load were it not for
1264 // the clobber, but neither lies strictly after the other.
1265 OtherAccess = nullptr;
1266 break;
1267 } // else: keep current OtherAccess since it lies between U and
1268 // Load.
1269 } else {
1270 OtherAccess = I;
1271 }
1272 }
1273 }
1274 }
1275
1276 return OtherAccess;
1277}
1278
1279/// Try to locate the three instruction involved in a missed
1280/// load-elimination case that is due to an intervening store.
1281static void reportMayClobberedLoad(LoadInst *Load, Instruction *DepInst,
1282 const DominatorTree *DT,
1284 using namespace ore;
1285
1286 OptimizationRemarkMissed R(DEBUG_TYPE, "LoadClobbered", Load);
1287 R << "load of type " << NV("Type", Load->getType()) << " not eliminated"
1288 << setExtraArgs();
1289
1290 const Instruction *OtherAccess = findMayClobberedPtrAccess(Load, DT);
1291 if (OtherAccess)
1292 R << " in favor of " << NV("OtherAccess", OtherAccess);
1293
1294 R << " because it is clobbered by " << NV("ClobberedBy", DepInst);
1295
1296 ORE->emit(R);
1297}
1298
1299// Find a dominating value for Loc memory location in the extended basic block
1300// (chain of basic blocks with single predecessors) starting From instruction.
1301// Returns the value from a matching load or a simple store to the same pointer.
1303 Instruction *From, AAResults *AA) {
1304 uint32_t NumVisitedInsts = 0;
1305 BasicBlock *FromBB = From->getParent();
1306 BatchAAResults BatchAA(*AA);
1307 for (BasicBlock *BB = FromBB; BB; BB = BB->getSinglePredecessor())
1308 for (auto *Inst = BB == FromBB ? From : BB->getTerminator();
1309 Inst != nullptr; Inst = Inst->getPrevNode()) {
1310 // Stop the search if limit is reached.
1311 if (++NumVisitedInsts > MaxNumVisitedInsts)
1312 return nullptr;
1313 if (isModSet(BatchAA.getModRefInfo(Inst, Loc))) {
1314 // A simple store to the exact location can forward its value.
1315 if (auto *SI = dyn_cast<StoreInst>(Inst))
1316 if (SI->isSimple() && SI->getPointerOperand() == Loc.Ptr &&
1317 SI->getValueOperand()->getType() == LoadTy)
1318 return SI->getValueOperand();
1319 return nullptr;
1320 }
1321 if (auto *LI = dyn_cast<LoadInst>(Inst))
1322 if (LI->getPointerOperand() == Loc.Ptr && LI->getType() == LoadTy)
1323 return LI;
1324 }
1325 return nullptr;
1326}
1327
1328std::optional<AvailableValue>
1329GVNPass::AnalyzeLoadAvailability(LoadInst *Load, const ReachingMemVal &Dep,
1330 Value *Address) {
1331 assert(Load->isUnordered() && "rules below are incorrect for ordered access");
1332 assert((Dep.Kind == DepKind::Def || Dep.Kind == DepKind::Clobber) &&
1333 "expected a local dependence");
1334
1335 Instruction *DepInst = Dep.Inst;
1336
1337 const DataLayout &DL = Load->getDataLayout();
1338 if (Dep.Kind == DepKind::Clobber) {
1339 // If the dependence is to a store that writes to a superset of the bits
1340 // read by the load, we can extract the bits we need for the load from the
1341 // stored value.
1342 if (StoreInst *DepSI = dyn_cast<StoreInst>(DepInst)) {
1343 // Can't forward from non-atomic to atomic without violating memory model.
1344 if (Address && Load->isAtomic() <= DepSI->isAtomic()) {
1345 int Offset =
1346 analyzeLoadFromClobberingStore(Load->getType(), Address, DepSI, DL);
1347 if (Offset != -1)
1348 return AvailableValue::get(DepSI->getValueOperand(), Offset);
1349 }
1350 }
1351
1352 // Check to see if we have something like this:
1353 // load i32* P
1354 // load i8* (P+1)
1355 // if we have this, replace the later with an extraction from the former.
1356 if (LoadInst *DepLoad = dyn_cast<LoadInst>(DepInst)) {
1357 // If this is a clobber and L is the first instruction in its block, then
1358 // we have the first instruction in the entry block.
1359 // Can't forward from non-atomic to atomic without violating memory model.
1360 if (DepLoad != Load && Address &&
1361 Load->isAtomic() <= DepLoad->isAtomic()) {
1362 Type *LoadType = Load->getType();
1363 int Offset = Dep.Offset;
1364
1365 if (!isMemorySSAEnabled()) {
1366 // If MD reported clobber, check it was nested.
1367 if (canCoerceMustAliasedValueToLoad(DepLoad, LoadType,
1368 DepLoad->getFunction())) {
1369 const auto ClobberOff = MD->getClobberOffset(DepLoad);
1370 // GVN has no deal with a negative offset.
1371 Offset = (ClobberOff == std::nullopt || *ClobberOff < 0)
1372 ? -1
1373 : *ClobberOff;
1374 }
1375 } else {
1376 if (!canCoerceMustAliasedValueToLoad(DepLoad, LoadType,
1377 DepLoad->getFunction()) ||
1378 Offset < 0)
1379 Offset = -1;
1380 }
1381 if (Offset == -1)
1382 Offset =
1383 analyzeLoadFromClobberingLoad(LoadType, Address, DepLoad, DL);
1384 if (Offset != -1)
1385 return AvailableValue::getLoad(DepLoad, Offset);
1386 }
1387 }
1388
1389 // If the clobbering value is a memset/memcpy/memmove, see if we can
1390 // forward a value on from it.
1391 if (MemIntrinsic *DepMI = dyn_cast<MemIntrinsic>(DepInst)) {
1392 if (Address && !Load->isAtomic()) {
1394 DepMI, DL);
1395 if (Offset != -1)
1396 return AvailableValue::getMI(DepMI, Offset);
1397 }
1398 }
1399
1400 // Nothing known about this clobber, have to be conservative.
1401 LLVM_DEBUG(
1402 // fast print dep, using operator<< on instruction is too slow.
1403 dbgs() << "GVN: load "; Load->printAsOperand(dbgs());
1404 dbgs() << " is clobbered by " << *DepInst << '\n';);
1405 if (ORE->allowExtraAnalysis(DEBUG_TYPE))
1406 reportMayClobberedLoad(Load, DepInst, DT, ORE);
1407
1408 return std::nullopt;
1409 }
1410 assert(Dep.Kind == DepKind::Def && "follows from above");
1411
1412 // Loading the alloca -> undef.
1413 // Loading immediately after lifetime begin -> undef.
1414 if (isa<AllocaInst>(DepInst) || isLifetimeStart(DepInst))
1415 return AvailableValue::get(UndefValue::get(Load->getType()));
1416
1417 if (Constant *InitVal =
1418 getInitialValueOfAllocation(DepInst, TLI, Load->getType()))
1419 return AvailableValue::get(InitVal);
1420
1421 if (StoreInst *S = dyn_cast<StoreInst>(DepInst)) {
1422 // Reject loads and stores that are to the same address but are of
1423 // different types if we have to. If the stored value is convertable to
1424 // the loaded value, we can reuse it.
1425 if (!canCoerceMustAliasedValueToLoad(S->getValueOperand(), Load->getType(),
1426 S->getFunction()))
1427 return std::nullopt;
1428
1429 // Can't forward from non-atomic to atomic without violating memory model.
1430 if (S->isAtomic() < Load->isAtomic())
1431 return std::nullopt;
1432
1433 return AvailableValue::get(S->getValueOperand());
1434 }
1435
1436 if (LoadInst *LD = dyn_cast<LoadInst>(DepInst)) {
1437 // If the types mismatch and we can't handle it, reject reuse of the load.
1438 // If the stored value is larger or equal to the loaded value, we can reuse
1439 // it.
1440 if (!canCoerceMustAliasedValueToLoad(LD, Load->getType(),
1441 LD->getFunction()))
1442 return std::nullopt;
1443
1444 // Can't forward from non-atomic to atomic without violating memory model.
1445 if (LD->isAtomic() < Load->isAtomic())
1446 return std::nullopt;
1447
1448 return AvailableValue::getLoad(LD);
1449 }
1450
1451 // Check if load with Addr dependent from select can be converted to select
1452 // between load values. There must be no instructions between the found
1453 // loads and DepInst that may clobber the loads.
1454 if (auto *Sel = dyn_cast<SelectInst>(DepInst)) {
1455 assert(Sel->getType() == Load->getPointerOperandType());
1456 auto Loc = MemoryLocation::get(Load);
1457 Value *V1 =
1458 findDominatingValue(Loc.getWithNewPtr(Sel->getTrueValue()),
1459 Load->getType(), DepInst, getAliasAnalysis());
1460 if (!V1)
1461 return std::nullopt;
1462 Value *V2 =
1463 findDominatingValue(Loc.getWithNewPtr(Sel->getFalseValue()),
1464 Load->getType(), DepInst, getAliasAnalysis());
1465 if (!V2)
1466 return std::nullopt;
1467 return AvailableValue::getSelect(Sel, V1, V2);
1468 }
1469
1470 // Unknown def - must be conservative.
1471 LLVM_DEBUG(
1472 // fast print dep, using operator<< on instruction is too slow.
1473 dbgs() << "GVN: load "; Load->printAsOperand(dbgs());
1474 dbgs() << " has unknown def " << *DepInst << '\n';);
1475 return std::nullopt;
1476}
1477
1478void GVNPass::AnalyzeLoadAvailability(LoadInst *Load,
1479 SmallVectorImpl<ReachingMemVal> &Deps,
1480 AvailValInBlkVect &ValuesPerBlock,
1481 UnavailBlkVect &UnavailableBlocks) {
1482 // Filter out useless results (non-locals, etc). Keep track of the blocks
1483 // where we have a value available in repl, also keep track of whether we see
1484 // dependencies that produce an unknown value for the load (such as a call
1485 // that could potentially clobber the load).
1486 for (const auto &Dep : Deps) {
1487 BasicBlock *DepBB = Dep.Block;
1488
1489 if (DeadBlocks.count(DepBB)) {
1490 // Dead dependent mem-op disguise as a load evaluating the same value
1491 // as the load in question.
1492 ValuesPerBlock.push_back(AvailableValueInBlock::getUndef(DepBB));
1493 continue;
1494 }
1495
1496 if (Dep.Kind == DepKind::Other) {
1497 UnavailableBlocks.push_back(DepBB);
1498 continue;
1499 }
1500
1501 // The address being loaded in this non-local block may not be the same as
1502 // the pointer operand of the load if PHI translation occurs. Make sure
1503 // to consider the right address.
1504 if (auto AV =
1505 AnalyzeLoadAvailability(Load, Dep, const_cast<Value *>(Dep.Addr))) {
1506 // subtlety: because we know this was a non-local dependency, we know
1507 // it's safe to materialize anywhere between the instruction within
1508 // DepInfo and the end of it's block.
1509 ValuesPerBlock.push_back(
1510 AvailableValueInBlock::get(DepBB, std::move(*AV)));
1511 } else {
1512 UnavailableBlocks.push_back(DepBB);
1513 }
1514 }
1515
1516 assert(Deps.size() == ValuesPerBlock.size() + UnavailableBlocks.size() &&
1517 "post condition violation");
1518}
1519
1520/// Given the following code, v1 is partially available on some edges, but not
1521/// available on the edge from PredBB. This function tries to find if there is
1522/// another identical load in the other successor of PredBB.
1523///
1524/// v0 = load %addr
1525/// br %LoadBB
1526///
1527/// LoadBB:
1528/// v1 = load %addr
1529/// ...
1530///
1531/// PredBB:
1532/// ...
1533/// br %cond, label %LoadBB, label %SuccBB
1534///
1535/// SuccBB:
1536/// v2 = load %addr
1537/// ...
1538///
1539LoadInst *GVNPass::findLoadToHoistIntoPred(BasicBlock *Pred, BasicBlock *LoadBB,
1540 LoadInst *Load) {
1541 // For simplicity we handle a Pred has 2 successors only.
1542 auto *Term = Pred->getTerminator();
1543 if (Term->getNumSuccessors() != 2 || Term->isSpecialTerminator())
1544 return nullptr;
1545 auto *SuccBB = Term->getSuccessor(0);
1546 if (SuccBB == LoadBB)
1547 SuccBB = Term->getSuccessor(1);
1548 if (!SuccBB->getSinglePredecessor())
1549 return nullptr;
1550
1551 unsigned int NumInsts = MaxNumInsnsPerBlock;
1552 for (Instruction &Inst : *SuccBB) {
1553 if (Inst.isDebugOrPseudoInst())
1554 continue;
1555 if (--NumInsts == 0)
1556 return nullptr;
1557
1558 if (!Inst.isIdenticalTo(Load))
1559 continue;
1560
1561 bool HasLocalDep = true;
1562 if (!isMemorySSAEnabled()) {
1563 MemDepResult Dep = MD->getDependency(&Inst);
1564 HasLocalDep = !Dep.isNonLocal();
1565 } else {
1566 auto *MSSA = MSSAU->getMemorySSA();
1567 // Do not hoist if the identical load has ordering constraint.
1568 if (auto *MA = MSSA->getMemoryAccess(&Inst); MA && isa<MemoryUse>(MA)) {
1569 auto *Clobber = MSSA->getWalker()->getClobberingMemoryAccess(MA);
1570 HasLocalDep = Clobber->getBlock() == SuccBB;
1571 }
1572 }
1573
1574 // If an identical load doesn't depends on any local instructions, it can
1575 // be safely moved to PredBB.
1576 // Also check for the implicit control flow instructions. See the comments
1577 // in PerformLoadPRE for details.
1578 if (!HasLocalDep && !ICF->isDominatedByICFIFromSameBlock(&Inst))
1579 return cast<LoadInst>(&Inst);
1580
1581 // Otherwise there is something in the same BB clobbers the memory, we can't
1582 // move this and later load to PredBB.
1583 return nullptr;
1584 }
1585
1586 return nullptr;
1587}
1588
1589void GVNPass::eliminatePartiallyRedundantLoad(
1590 LoadInst *Load, AvailValInBlkVect &ValuesPerBlock,
1591 MapVector<BasicBlock *, Value *> &AvailableLoads,
1592 MapVector<BasicBlock *, LoadInst *> *CriticalEdgePredAndLoad) {
1593 for (const auto &AvailableLoad : AvailableLoads) {
1594 BasicBlock *UnavailableBlock = AvailableLoad.first;
1595 Value *LoadPtr = AvailableLoad.second;
1596
1597 auto *NewLoad = new LoadInst(
1598 Load->getType(), LoadPtr, Load->getName() + ".pre", Load->isVolatile(),
1599 Load->getAlign(), Load->getOrdering(), Load->getSyncScopeID(),
1600 UnavailableBlock->getTerminator()->getIterator());
1601 NewLoad->setDebugLoc(Load->getDebugLoc());
1602 if (MSSAU) {
1603 auto *NewAccess = MSSAU->createMemoryAccessInBB(
1604 NewLoad, nullptr, NewLoad->getParent(), MemorySSA::BeforeTerminator);
1605 if (auto *NewDef = dyn_cast<MemoryDef>(NewAccess))
1606 MSSAU->insertDef(NewDef, /*RenameUses=*/true);
1607 else
1608 MSSAU->insertUse(cast<MemoryUse>(NewAccess), /*RenameUses=*/true);
1609 }
1610
1611 // Transfer the old load's AA tags to the new load.
1612 AAMDNodes Tags = Load->getAAMetadata();
1613 if (Tags)
1614 NewLoad->setAAMetadata(Tags);
1615
1616 if (auto *MD = Load->getMetadata(LLVMContext::MD_invariant_load))
1617 NewLoad->setMetadata(LLVMContext::MD_invariant_load, MD);
1618 if (auto *InvGroupMD = Load->getMetadata(LLVMContext::MD_invariant_group))
1619 NewLoad->setMetadata(LLVMContext::MD_invariant_group, InvGroupMD);
1620 if (auto *RangeMD = Load->getMetadata(LLVMContext::MD_range))
1621 NewLoad->setMetadata(LLVMContext::MD_range, RangeMD);
1622 if (auto *NoFPClassMD = Load->getMetadata(LLVMContext::MD_nofpclass))
1623 NewLoad->setMetadata(LLVMContext::MD_nofpclass, NoFPClassMD);
1624
1625 if (auto *AccessMD = Load->getMetadata(LLVMContext::MD_access_group))
1626 if (LI->getLoopFor(Load->getParent()) == LI->getLoopFor(UnavailableBlock))
1627 NewLoad->setMetadata(LLVMContext::MD_access_group, AccessMD);
1628
1629 // We do not propagate the old load's debug location, because the new
1630 // load now lives in a different BB, and we want to avoid a jumpy line
1631 // table.
1632 // FIXME: How do we retain source locations without causing poor debugging
1633 // behavior?
1634
1635 // Add the newly created load.
1636 ValuesPerBlock.push_back(
1637 AvailableValueInBlock::get(UnavailableBlock, NewLoad));
1638 if (MD)
1639 MD->invalidateCachedPointerInfo(LoadPtr);
1640 LLVM_DEBUG(dbgs() << "GVN INSERTED " << *NewLoad << '\n');
1641
1642 // For PredBB in CriticalEdgePredAndLoad we need to replace the uses of old
1643 // load instruction with the new created load instruction.
1644 if (CriticalEdgePredAndLoad) {
1645 auto It = CriticalEdgePredAndLoad->find(UnavailableBlock);
1646 if (It != CriticalEdgePredAndLoad->end()) {
1647 ++NumPRELoadMoved2CEPred;
1648 ICF->insertInstructionTo(NewLoad, UnavailableBlock);
1649 LoadInst *OldLoad = It->second;
1650 combineMetadataForCSE(NewLoad, OldLoad, /*DoesKMove=*/true);
1651 OldLoad->replaceAllUsesWith(NewLoad);
1652 replaceValuesPerBlockEntry(ValuesPerBlock, OldLoad, NewLoad);
1653 if (uint32_t ValNo = VN.lookup(OldLoad, false))
1654 LeaderTable.erase(ValNo, OldLoad, OldLoad->getParent());
1655 removeInstruction(OldLoad);
1656 }
1657 }
1658 }
1659
1660 // Perform PHI construction.
1661 Value *V = ConstructSSAForLoadSet(Load, ValuesPerBlock, *this);
1662 // ConstructSSAForLoadSet is responsible for combining metadata.
1663 ICF->removeUsersOf(Load);
1664 Load->replaceAllUsesWith(V);
1665 if (isa<PHINode>(V))
1666 V->takeName(Load);
1667 if (Instruction *I = dyn_cast<Instruction>(V))
1668 I->setDebugLoc(Load->getDebugLoc());
1669 if (MD && V->getType()->isPtrOrPtrVectorTy())
1670 MD->invalidateCachedPointerInfo(V);
1671 ORE->emit([&]() {
1672 return OptimizationRemark(DEBUG_TYPE, "LoadPRE", Load)
1673 << "load eliminated by PRE";
1674 });
1676}
1677
1678bool GVNPass::PerformLoadPRE(LoadInst *Load, AvailValInBlkVect &ValuesPerBlock,
1679 UnavailBlkVect &UnavailableBlocks) {
1680 // Okay, we have *some* definitions of the value. This means that the value
1681 // is available in some of our (transitive) predecessors. Lets think about
1682 // doing PRE of this load. This will involve inserting a new load into the
1683 // predecessor when it's not available. We could do this in general, but
1684 // prefer to not increase code size. As such, we only do this when we know
1685 // that we only have to insert *one* load (which means we're basically moving
1686 // the load, not inserting a new one).
1687
1688 SmallPtrSet<BasicBlock *, 4> Blockers(llvm::from_range, UnavailableBlocks);
1689
1690 // Let's find the first basic block with more than one predecessor. Walk
1691 // backwards through predecessors if needed.
1692 BasicBlock *LoadBB = Load->getParent();
1693 BasicBlock *TmpBB = LoadBB;
1694
1695 // Check that there is no implicit control flow instructions above our load in
1696 // its block. If there is an instruction that doesn't always pass the
1697 // execution to the following instruction, then moving through it may become
1698 // invalid. For example:
1699 //
1700 // int arr[LEN];
1701 // int index = ???;
1702 // ...
1703 // guard(0 <= index && index < LEN);
1704 // use(arr[index]);
1705 //
1706 // It is illegal to move the array access to any point above the guard,
1707 // because if the index is out of bounds we should deoptimize rather than
1708 // access the array.
1709 // Check that there is no guard in this block above our instruction.
1710 bool MustEnsureSafetyOfSpeculativeExecution =
1711 ICF->isDominatedByICFIFromSameBlock(Load);
1712
1713 while (TmpBB->getSinglePredecessor()) {
1714 TmpBB = TmpBB->getSinglePredecessor();
1715 if (TmpBB == LoadBB) // Infinite (unreachable) loop.
1716 return false;
1717 if (Blockers.count(TmpBB))
1718 return false;
1719
1720 // If any of these blocks has more than one successor (i.e. if the edge we
1721 // just traversed was critical), then there are other paths through this
1722 // block along which the load may not be anticipated. Hoisting the load
1723 // above this block would be adding the load to execution paths along
1724 // which it was not previously executed.
1725 if (TmpBB->getTerminator()->getNumSuccessors() != 1)
1726 return false;
1727
1728 // Check that there is no implicit control flow in a block above.
1729 MustEnsureSafetyOfSpeculativeExecution =
1730 MustEnsureSafetyOfSpeculativeExecution || ICF->hasICF(TmpBB);
1731 }
1732
1733 assert(TmpBB);
1734 LoadBB = TmpBB;
1735
1736 // Check to see how many predecessors have the loaded value fully
1737 // available.
1738 MapVector<BasicBlock *, Value *> PredLoads;
1739 DenseMap<BasicBlock *, AvailabilityState> FullyAvailableBlocks;
1740 for (const AvailableValueInBlock &AV : ValuesPerBlock)
1741 FullyAvailableBlocks[AV.BB] = AvailabilityState::Available;
1742 for (BasicBlock *UnavailableBB : UnavailableBlocks)
1743 FullyAvailableBlocks[UnavailableBB] = AvailabilityState::Unavailable;
1744
1745 // The edge from Pred to LoadBB is a critical edge will be splitted.
1746 SmallVector<BasicBlock *, 4> CriticalEdgePredSplit;
1747 // The edge from Pred to LoadBB is a critical edge, another successor of Pred
1748 // contains a load can be moved to Pred. This data structure maps the Pred to
1749 // the movable load.
1750 MapVector<BasicBlock *, LoadInst *> CriticalEdgePredAndLoad;
1751 for (BasicBlock *Pred : predecessors(LoadBB)) {
1752 // If any predecessor block is an EH pad that does not allow non-PHI
1753 // instructions before the terminator, we can't PRE the load.
1754 if (Pred->getTerminator()->isEHPad()) {
1755 LLVM_DEBUG(
1756 dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD PREDECESSOR '"
1757 << Pred->getName() << "': " << *Load << '\n');
1758 return false;
1759 }
1760
1761 if (IsValueFullyAvailableInBlock(Pred, FullyAvailableBlocks)) {
1762 continue;
1763 }
1764
1765 if (Pred->getTerminator()->getNumSuccessors() != 1) {
1766 if (isa<IndirectBrInst>(Pred->getTerminator())) {
1767 LLVM_DEBUG(
1768 dbgs() << "COULD NOT PRE LOAD BECAUSE OF INDBR CRITICAL EDGE '"
1769 << Pred->getName() << "': " << *Load << '\n');
1770 return false;
1771 }
1772
1773 if (LoadBB->isEHPad()) {
1774 LLVM_DEBUG(
1775 dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD CRITICAL EDGE '"
1776 << Pred->getName() << "': " << *Load << '\n');
1777 return false;
1778 }
1779
1780 // Do not split backedge as it will break the canonical loop form.
1782 if (DT->dominates(LoadBB, Pred)) {
1783 LLVM_DEBUG(
1784 dbgs()
1785 << "COULD NOT PRE LOAD BECAUSE OF A BACKEDGE CRITICAL EDGE '"
1786 << Pred->getName() << "': " << *Load << '\n');
1787 return false;
1788 }
1789
1790 if (LoadInst *LI = findLoadToHoistIntoPred(Pred, LoadBB, Load))
1791 CriticalEdgePredAndLoad[Pred] = LI;
1792 else
1793 CriticalEdgePredSplit.push_back(Pred);
1794 } else {
1795 // Only add the predecessors that will not be split for now.
1796 PredLoads[Pred] = nullptr;
1797 }
1798 }
1799
1800 // Decide whether PRE is profitable for this load.
1801 unsigned NumInsertPreds = PredLoads.size() + CriticalEdgePredSplit.size();
1802 unsigned NumUnavailablePreds = NumInsertPreds +
1803 CriticalEdgePredAndLoad.size();
1804 assert(NumUnavailablePreds != 0 &&
1805 "Fully available value should already be eliminated!");
1806 (void)NumUnavailablePreds;
1807
1808 // If we need to insert new load in multiple predecessors, reject it.
1809 // FIXME: If we could restructure the CFG, we could make a common pred with
1810 // all the preds that don't have an available Load and insert a new load into
1811 // that one block.
1812 if (NumInsertPreds > 1)
1813 return false;
1814
1815 // Now we know where we will insert load. We must ensure that it is safe
1816 // to speculatively execute the load at that points.
1817 if (MustEnsureSafetyOfSpeculativeExecution) {
1818 if (CriticalEdgePredSplit.size())
1819 if (!isSafeToSpeculativelyExecute(Load, &*LoadBB->getFirstNonPHIIt(), AC,
1820 DT))
1821 return false;
1822 for (auto &PL : PredLoads)
1823 if (!isSafeToSpeculativelyExecute(Load, PL.first->getTerminator(), AC,
1824 DT))
1825 return false;
1826 for (auto &CEP : CriticalEdgePredAndLoad)
1827 if (!isSafeToSpeculativelyExecute(Load, CEP.first->getTerminator(), AC,
1828 DT))
1829 return false;
1830 }
1831
1832 // Split critical edges, and update the unavailable predecessors accordingly.
1833 for (BasicBlock *OrigPred : CriticalEdgePredSplit) {
1834 BasicBlock *NewPred = splitCriticalEdges(OrigPred, LoadBB);
1835 assert(!PredLoads.count(OrigPred) && "Split edges shouldn't be in map!");
1836 PredLoads[NewPred] = nullptr;
1837 LLVM_DEBUG(dbgs() << "Split critical edge " << OrigPred->getName() << "->"
1838 << LoadBB->getName() << '\n');
1839 }
1840
1841 for (auto &CEP : CriticalEdgePredAndLoad)
1842 PredLoads[CEP.first] = nullptr;
1843
1844 // Check if the load can safely be moved to all the unavailable predecessors.
1845 bool CanDoPRE = true;
1846 const DataLayout &DL = Load->getDataLayout();
1847 SmallVector<Instruction*, 8> NewInsts;
1848 for (auto &PredLoad : PredLoads) {
1849 BasicBlock *UnavailablePred = PredLoad.first;
1850
1851 // Do PHI translation to get its value in the predecessor if necessary. The
1852 // returned pointer (if non-null) is guaranteed to dominate UnavailablePred.
1853 // We do the translation for each edge we skipped by going from Load's block
1854 // to LoadBB, otherwise we might miss pieces needing translation.
1855
1856 // If all preds have a single successor, then we know it is safe to insert
1857 // the load on the pred (?!?), so we can insert code to materialize the
1858 // pointer if it is not available.
1859 Value *LoadPtr = Load->getPointerOperand();
1860 BasicBlock *Cur = Load->getParent();
1861 while (Cur != LoadBB) {
1862 PHITransAddr Address(LoadPtr, DL, AC);
1863 LoadPtr = Address.translateWithInsertion(Cur, Cur->getSinglePredecessor(),
1864 *DT, NewInsts);
1865 if (!LoadPtr) {
1866 CanDoPRE = false;
1867 break;
1868 }
1869 Cur = Cur->getSinglePredecessor();
1870 }
1871
1872 if (LoadPtr) {
1873 PHITransAddr Address(LoadPtr, DL, AC);
1874 LoadPtr = Address.translateWithInsertion(LoadBB, UnavailablePred, *DT,
1875 NewInsts);
1876 }
1877 // If we couldn't find or insert a computation of this phi translated value,
1878 // we fail PRE.
1879 if (!LoadPtr) {
1880 LLVM_DEBUG(dbgs() << "COULDN'T INSERT PHI TRANSLATED VALUE OF: "
1881 << *Load->getPointerOperand() << "\n");
1882 CanDoPRE = false;
1883 break;
1884 }
1885
1886 PredLoad.second = LoadPtr;
1887 }
1888
1889 if (!CanDoPRE) {
1890 while (!NewInsts.empty()) {
1891 // Erase instructions generated by the failed PHI translation before
1892 // trying to number them. PHI translation might insert instructions
1893 // in basic blocks other than the current one, and we delete them
1894 // directly, as salvageAndRemoveInstruction only allows removing from the
1895 // current basic block.
1896 NewInsts.pop_back_val()->eraseFromParent();
1897 }
1898 // HINT: Don't revert the edge-splitting as following transformation may
1899 // also need to split these critical edges.
1900 return !CriticalEdgePredSplit.empty();
1901 }
1902
1903 // Okay, we can eliminate this load by inserting a reload in the predecessor
1904 // and using PHI construction to get the value in the other predecessors, do
1905 // it.
1906 LLVM_DEBUG(dbgs() << "GVN REMOVING PRE LOAD: " << *Load << '\n');
1907 LLVM_DEBUG(if (!NewInsts.empty()) dbgs() << "INSERTED " << NewInsts.size()
1908 << " INSTS: " << *NewInsts.back()
1909 << '\n');
1910
1911 // Assign value numbers to the new instructions.
1912 for (Instruction *I : NewInsts) {
1913 // Instructions that have been inserted in predecessor(s) to materialize
1914 // the load address do not retain their original debug locations. Doing
1915 // so could lead to confusing (but correct) source attributions.
1916 I->updateLocationAfterHoist();
1917
1918 // FIXME: We really _ought_ to insert these value numbers into their
1919 // parent's availability map. However, in doing so, we risk getting into
1920 // ordering issues. If a block hasn't been processed yet, we would be
1921 // marking a value as AVAIL-IN, which isn't what we intend.
1922 VN.lookupOrAdd(I);
1923 }
1924
1925 eliminatePartiallyRedundantLoad(Load, ValuesPerBlock, PredLoads,
1926 &CriticalEdgePredAndLoad);
1927 ++NumPRELoad;
1928 return true;
1929}
1930
1931bool GVNPass::performLoopLoadPRE(LoadInst *Load,
1932 AvailValInBlkVect &ValuesPerBlock,
1933 UnavailBlkVect &UnavailableBlocks) {
1934 const Loop *L = LI->getLoopFor(Load->getParent());
1935 // TODO: Generalize to other loop blocks that dominate the latch.
1936 if (!L || L->getHeader() != Load->getParent())
1937 return false;
1938
1939 BasicBlock *Preheader = L->getLoopPreheader();
1940 BasicBlock *Latch = L->getLoopLatch();
1941 if (!Preheader || !Latch)
1942 return false;
1943
1944 Value *LoadPtr = Load->getPointerOperand();
1945 // Must be available in preheader.
1946 if (!L->isLoopInvariant(LoadPtr))
1947 return false;
1948
1949 // We plan to hoist the load to preheader without introducing a new fault.
1950 // In order to do it, we need to prove that we cannot side-exit the loop
1951 // once loop header is first entered before execution of the load.
1952 if (ICF->isDominatedByICFIFromSameBlock(Load))
1953 return false;
1954
1955 BasicBlock *LoopBlock = nullptr;
1956 for (auto *Blocker : UnavailableBlocks) {
1957 // Blockers from outside the loop are handled in preheader.
1958 if (!L->contains(Blocker))
1959 continue;
1960
1961 // Only allow one loop block. Loop header is not less frequently executed
1962 // than each loop block, and likely it is much more frequently executed. But
1963 // in case of multiple loop blocks, we need extra information (such as block
1964 // frequency info) to understand whether it is profitable to PRE into
1965 // multiple loop blocks.
1966 if (LoopBlock)
1967 return false;
1968
1969 // Do not sink into inner loops. This may be non-profitable.
1970 if (L != LI->getLoopFor(Blocker))
1971 return false;
1972
1973 // Blocks that dominate the latch execute on every single iteration, maybe
1974 // except the last one. So PREing into these blocks doesn't make much sense
1975 // in most cases. But the blocks that do not necessarily execute on each
1976 // iteration are sometimes much colder than the header, and this is when
1977 // PRE is potentially profitable.
1978 if (DT->dominates(Blocker, Latch))
1979 return false;
1980
1981 // Make sure that the terminator itself doesn't clobber.
1982 if (Blocker->getTerminator()->mayWriteToMemory())
1983 return false;
1984
1985 LoopBlock = Blocker;
1986 }
1987
1988 if (!LoopBlock)
1989 return false;
1990
1991 // Make sure the memory at this pointer cannot be freed, therefore we can
1992 // safely reload from it after clobber.
1993 if (LoadPtr->canBeFreed())
1994 return false;
1995
1996 // TODO: Support critical edge splitting if blocker has more than 1 successor.
1997 MapVector<BasicBlock *, Value *> AvailableLoads;
1998 AvailableLoads[LoopBlock] = LoadPtr;
1999 AvailableLoads[Preheader] = LoadPtr;
2000
2001 LLVM_DEBUG(dbgs() << "GVN REMOVING PRE LOOP LOAD: " << *Load << '\n');
2002 eliminatePartiallyRedundantLoad(Load, ValuesPerBlock, AvailableLoads,
2003 /*CriticalEdgePredAndLoad*/ nullptr);
2004 ++NumPRELoopLoad;
2005 return true;
2006}
2007
2010 using namespace ore;
2011
2012 ORE->emit([&]() {
2013 return OptimizationRemark(DEBUG_TYPE, "LoadElim", Load)
2014 << "load of type " << NV("Type", Load->getType()) << " eliminated"
2015 << setExtraArgs() << " in favor of "
2016 << NV("InfavorOfValue", AvailableValue);
2017 });
2018}
2019
2020/// Attempt to eliminate a load whose dependencies are
2021/// non-local by performing PHI construction.
2022bool GVNPass::processNonLocalLoad(LoadInst *Load) {
2023 // Non-local speculations are not allowed under asan.
2024 if (Load->getFunction()->hasFnAttribute(Attribute::SanitizeAddress) ||
2025 Load->getFunction()->hasFnAttribute(Attribute::SanitizeHWAddress))
2026 return false;
2027
2028 // Find the non-local dependencies of the load.
2029 LoadDepVect Deps;
2030 MD->getNonLocalPointerDependency(Load, Deps);
2031
2032 // If we had to process more than one hundred blocks to find the
2033 // dependencies, this load isn't worth worrying about. Optimizing
2034 // it will be too expensive.
2035 unsigned NumDeps = Deps.size();
2036 if (NumDeps > MaxNumDeps)
2037 return false;
2038
2040 MemVals.reserve(Deps.size());
2041
2042 for (const NonLocalDepResult &Dep : Deps) {
2043 const auto &R = Dep.getResult();
2044 Value *Address = Dep.getAddress();
2045 BasicBlock *BB = Dep.getBB();
2046 Instruction *Inst = R.getInst();
2047 if (R.isClobber())
2048 MemVals.emplace_back(ReachingMemVal::getClobber(Address, Inst));
2049 else if (R.isDef())
2050 MemVals.emplace_back(ReachingMemVal::getDef(Address, Inst));
2051 else
2052 MemVals.emplace_back(ReachingMemVal::getUnknown(BB, Address, Inst));
2053 }
2054
2055 return processNonLocalLoad(Load, MemVals);
2056}
2057
2058bool GVNPass::processNonLocalLoad(LoadInst *Load,
2059 SmallVectorImpl<ReachingMemVal> &Deps) {
2060 // If we had a phi translation failure, we'll have a single entry which is a
2061 // clobber in the current block. Reject this early.
2062 if (Deps.size() == 1 && Deps[0].Kind == DepKind::Other) {
2063 LLVM_DEBUG(dbgs() << "GVN: non-local load "; Load->printAsOperand(dbgs());
2064 dbgs() << " has unknown dependencies\n";);
2065 return false;
2066 }
2067
2068 bool Changed = false;
2069 // This is a limited form of scalar PRE for load indices. If this load follows
2070 // a GEP, see if we can PRE the indices before analyzing.
2071 if (isScalarPREEnabled()) {
2072 if (GetElementPtrInst *GEP =
2073 dyn_cast<GetElementPtrInst>(Load->getOperand(0))) {
2074 for (Use &U : GEP->indices())
2075 if (Instruction *I = dyn_cast<Instruction>(U.get()))
2076 Changed |= performScalarPRE(I);
2077 }
2078 }
2079
2080 // Step 1: Analyze the availability of the load.
2081 AvailValInBlkVect ValuesPerBlock;
2082 UnavailBlkVect UnavailableBlocks;
2083 AnalyzeLoadAvailability(Load, Deps, ValuesPerBlock, UnavailableBlocks);
2084
2085 // If we have no predecessors that produce a known value for this load, exit
2086 // early.
2087 if (ValuesPerBlock.empty())
2088 return Changed;
2089
2090 // Step 2: Eliminate fully redundancy.
2091 //
2092 // If all of the instructions we depend on produce a known value for this
2093 // load, then it is fully redundant and we can use PHI insertion to compute
2094 // its value. Insert PHIs and remove the fully redundant value now.
2095 if (UnavailableBlocks.empty()) {
2096 LLVM_DEBUG(dbgs() << "GVN REMOVING NONLOCAL LOAD: " << *Load << '\n');
2097
2098 // Perform PHI construction.
2099 Value *V = ConstructSSAForLoadSet(Load, ValuesPerBlock, *this);
2100 // ConstructSSAForLoadSet is responsible for combining metadata.
2101 ICF->removeUsersOf(Load);
2102 Load->replaceAllUsesWith(V);
2103
2104 if (isa<PHINode>(V))
2105 V->takeName(Load);
2106 if (Instruction *I = dyn_cast<Instruction>(V))
2107 // If instruction I has debug info, then we should not update it.
2108 // Also, if I has a null DebugLoc, then it is still potentially incorrect
2109 // to propagate Load's DebugLoc because Load may not post-dominate I.
2110 if (Load->getDebugLoc() && Load->getParent() == I->getParent())
2111 I->setDebugLoc(Load->getDebugLoc());
2112 if (MD && V->getType()->isPtrOrPtrVectorTy())
2113 MD->invalidateCachedPointerInfo(V);
2114 ++NumGVNLoad;
2115 reportLoadElim(Load, V, ORE);
2117 return true;
2118 }
2119
2120 // Step 3: Eliminate partial redundancy.
2121 if (!isLoadPREEnabled())
2122 return Changed;
2123 if (!isLoadInLoopPREEnabled() && LI->getLoopFor(Load->getParent()))
2124 return Changed;
2125
2126 if (performLoopLoadPRE(Load, ValuesPerBlock, UnavailableBlocks) ||
2127 PerformLoadPRE(Load, ValuesPerBlock, UnavailableBlocks))
2128 return true;
2129
2130 return Changed;
2131}
2132
2133bool GVNPass::processAssumeIntrinsic(AssumeInst *IntrinsicI) {
2134 Value *V = IntrinsicI->getArgOperand(0);
2135
2136 if (ConstantInt *Cond = dyn_cast<ConstantInt>(V)) {
2137 if (Cond->isZero()) {
2138 Type *Int8Ty = Type::getInt8Ty(V->getContext());
2139 Type *PtrTy = PointerType::get(V->getContext(), 0);
2140 // Insert a new store to null instruction before the load to indicate that
2141 // this code is not reachable. FIXME: We could insert unreachable
2142 // instruction directly because we can modify the CFG.
2143 auto *NewS =
2144 new StoreInst(PoisonValue::get(Int8Ty), Constant::getNullValue(PtrTy),
2145 IntrinsicI->getIterator());
2146 if (MSSAU) {
2147 const MemoryUseOrDef *FirstNonDom = nullptr;
2148 const auto *AL =
2149 MSSAU->getMemorySSA()->getBlockAccesses(IntrinsicI->getParent());
2150
2151 // If there are accesses in the current basic block, find the first one
2152 // that does not come before NewS. The new memory access is inserted
2153 // after the found access or before the terminator if no such access is
2154 // found.
2155 if (AL) {
2156 for (const auto &Acc : *AL) {
2157 if (auto *Current = dyn_cast<MemoryUseOrDef>(&Acc))
2158 if (!Current->getMemoryInst()->comesBefore(NewS)) {
2159 FirstNonDom = Current;
2160 break;
2161 }
2162 }
2163 }
2164
2165 auto *NewDef =
2166 FirstNonDom ? MSSAU->createMemoryAccessBefore(
2167 NewS, nullptr,
2168 const_cast<MemoryUseOrDef *>(FirstNonDom))
2169 : MSSAU->createMemoryAccessInBB(
2170 NewS, nullptr,
2171 NewS->getParent(), MemorySSA::BeforeTerminator);
2172
2173 MSSAU->insertDef(cast<MemoryDef>(NewDef), /*RenameUses=*/false);
2174 }
2175 }
2176 if (isAssumeWithEmptyBundle(*IntrinsicI)) {
2177 salvageAndRemoveInstruction(IntrinsicI);
2178 return true;
2179 }
2180 return false;
2181 }
2182
2183 if (isa<Constant>(V)) {
2184 // If it's not false, and constant, it must evaluate to true. This means our
2185 // assume is assume(true), and thus, pointless, and we don't want to do
2186 // anything more here.
2187 return false;
2188 }
2189
2190 Constant *True = ConstantInt::getTrue(V->getContext());
2191 return propagateEquality(V, True, IntrinsicI);
2192}
2193
2196 I->replaceAllUsesWith(Repl);
2197}
2198
2199/// If a load has !invariant.group, try to find the most-dominating instruction
2200/// with the same metadata and equivalent pointer (modulo bitcasts and zero
2201/// GEPs). If one is found that dominates the load, its value can be reused.
2203 Value *PointerOperand = L->getPointerOperand()->stripPointerCasts();
2204
2205 // It's not safe to walk the use list of a global value because function
2206 // passes aren't allowed to look outside their functions.
2207 // FIXME: this could be fixed by filtering instructions from outside of
2208 // current function.
2209 if (isa<Constant>(PointerOperand))
2210 return nullptr;
2211
2212 // Queue to process all pointers that are equivalent to load operand.
2213 SmallVector<Value *, 8> PointerUsesQueue;
2214 PointerUsesQueue.push_back(PointerOperand);
2215
2216 Instruction *MostDominatingInstruction = L;
2217
2218 // FIXME: This loop is potentially O(n^2) due to repeated dominates checks.
2219 while (!PointerUsesQueue.empty()) {
2220 Value *Ptr = PointerUsesQueue.pop_back_val();
2221 assert(Ptr && !isa<GlobalValue>(Ptr) &&
2222 "Null or GlobalValue should not be inserted");
2223
2224 for (User *U : Ptr->users()) {
2225 auto *I = dyn_cast<Instruction>(U);
2226 if (!I || I == L || !DT.dominates(I, MostDominatingInstruction))
2227 continue;
2228
2229 // Add bitcasts and zero GEPs to queue.
2230 // TODO: Should drop bitcast?
2231 if (isa<BitCastInst>(I) ||
2233 cast<GetElementPtrInst>(I)->hasAllZeroIndices())) {
2234 PointerUsesQueue.push_back(I);
2235 continue;
2236 }
2237
2238 // If we hit a load/store with an invariant.group metadata and the same
2239 // pointer operand, we can assume that value pointed to by the pointer
2240 // operand didn't change.
2241 if (I->hasMetadata(LLVMContext::MD_invariant_group) &&
2242 Ptr == getLoadStorePointerOperand(I) && !I->isVolatile())
2243 MostDominatingInstruction = I;
2244 }
2245 }
2246
2247 return MostDominatingInstruction != L ? MostDominatingInstruction : nullptr;
2248}
2249
2250/// Return the memory location accessed by the (masked) load/store instruction
2251/// `I`, if the instruction could potentially provide a useful value for
2252/// eliminating the load.
2253static std::optional<MemoryLocation>
2255 const TargetLibraryInfo *TLI) {
2256 if (auto *LI = dyn_cast<LoadInst>(I))
2257 return MemoryLocation::get(LI);
2258
2259 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2260 switch (II->getIntrinsicID()) {
2261 case Intrinsic::masked_load:
2262 return MemoryLocation::getForArgument(II, 0, TLI);
2263 case Intrinsic::masked_store:
2264 if (AllowStores)
2265 return MemoryLocation::getForArgument(II, 1, TLI);
2266 return std::nullopt;
2267 default:
2268 break;
2269 }
2270 }
2271
2272 if (!AllowStores)
2273 return std::nullopt;
2274
2275 if (auto *SI = dyn_cast<StoreInst>(I))
2276 return MemoryLocation::get(SI);
2277 return std::nullopt;
2278}
2279
2280/// Scan the users of each MemoryAccess in `ClobbersList` that belong to `BB`,
2281/// looking for memory reads whose location aliases `Loc` and dominates our
2282/// load.
2283std::optional<GVNPass::ReachingMemVal> GVNPass::scanMemoryAccessesUsers(
2284 const MemoryLocation &Loc, bool IsInvariantLoad, BasicBlock *BB,
2285 const SmallVectorImpl<MemoryAccess *> &ClobbersList, MemorySSA &MSSA,
2286 BatchAAResults &AA, LoadInst *L) {
2287
2288 // Prefer a candidate that is closer to the load within the same block.
2289 auto UpdateChoice = [&](std::optional<ReachingMemVal> &Choice,
2290 AliasResult &AR, Instruction *Candidate) {
2291 if (!Choice) {
2292 if (AR == AliasResult::PartialAlias)
2293 Choice = ReachingMemVal::getClobber(Loc.Ptr, Candidate, AR.getOffset());
2294 else
2295 Choice = ReachingMemVal::getDef(Loc.Ptr, Candidate);
2296 return;
2297 }
2298 if (!MSSA.locallyDominates(MSSA.getMemoryAccess(Choice->Inst),
2299 MSSA.getMemoryAccess(Candidate)))
2300 return;
2301
2302 if (AR == AliasResult::PartialAlias) {
2303 Choice->Kind = DepKind::Clobber;
2304 Choice->Offset = AR.getOffset();
2305 } else {
2306 Choice->Kind = DepKind::Def;
2307 Choice->Offset = -1;
2308 }
2309
2310 Choice->Inst = Candidate;
2311 Choice->Block = Candidate->getParent();
2312 };
2313
2314 std::optional<ReachingMemVal> ReachingVal;
2315 for (MemoryAccess *MA : ClobbersList) {
2316 unsigned Scanned = 0;
2317 for (User *U : MA->users()) {
2318 if (++Scanned >= ScanUsersLimit)
2319 return ReachingMemVal::getUnknown(BB, Loc.Ptr);
2320
2321 auto *UseOrDef = dyn_cast<MemoryUseOrDef>(U);
2322 if (!UseOrDef || UseOrDef->getBlock() != BB)
2323 continue;
2324
2325 Instruction *MemI = UseOrDef->getMemoryInst();
2326 if (MemI == L ||
2327 (L && !MSSA.locallyDominates(UseOrDef, MSSA.getMemoryAccess(L))))
2328 continue;
2329
2330 if (auto MaybeLoc = maybeLoadStoreLocation(MemI, IsInvariantLoad, TLI)) {
2331 AliasResult AR = AA.alias(*MaybeLoc, Loc);
2332 // If the locations do not certainly alias, we cannot possibly infer the
2333 // following load loads the same value.
2335 continue;
2336
2337 // Locations partially overlap, but neither is a subset of the other, or
2338 // the second location is before the first.
2339 if (AR == AliasResult::PartialAlias &&
2340 (!AR.hasOffset() || AR.getOffset() < 0))
2341 continue;
2342
2343 // Found candidate, the new load memory location and the given location
2344 // must alias: precise overlap, or subset with non-negative offset.
2345 UpdateChoice(ReachingVal, AR, MemI);
2346 }
2347 }
2348 if (ReachingVal)
2349 break;
2350 }
2351
2352 return ReachingVal;
2353}
2354
2355/// Check if a given MemoryAccess (usually a MemoryDef) actually modifies a
2356/// given location. Returns a ReachingMemVal describing the dependency.
2357std::optional<GVNPass::ReachingMemVal> GVNPass::accessMayModifyLocation(
2358 MemoryAccess *ClobberMA, const MemoryLocation &Loc, bool IsInvariantLoad,
2359 BasicBlock *BB, MemorySSA &MSSA, BatchAAResults &AA) {
2360 assert(ClobberMA->getBlock() == BB);
2361
2362 // If the clobbering access is the entry memory state, we cannot say anything
2363 // about the content of the memory, except when we are accessing a local
2364 // object, which can be turned later into producing `undef`.
2365 if (MSSA.isLiveOnEntryDef(ClobberMA)) {
2367 if (Alloc->getParent() == BB)
2368 return ReachingMemVal::getDef(Loc.Ptr, const_cast<AllocaInst *>(Alloc));
2369 return ReachingMemVal::getUnknown(BB, Loc.Ptr);
2370 }
2371
2372 // Loads from "constant" memory can't be clobbered.
2373 if (IsInvariantLoad || AA.pointsToConstantMemory(Loc))
2374 return std::nullopt;
2375
2376 auto GetOrdering = [](const Instruction *I) {
2377 if (auto *L = dyn_cast<LoadInst>(I))
2378 return L->getOrdering();
2379 return cast<StoreInst>(I)->getOrdering();
2380 };
2381 Instruction *ClobberI = cast<MemoryDef>(ClobberMA)->getMemoryInst();
2382
2383 // Check if the clobbering access is a load or a store that we can reuse.
2384 if (auto MaybeLoc = maybeLoadStoreLocation(ClobberI, true, TLI)) {
2385 AliasResult AR = AA.alias(*MaybeLoc, Loc);
2386 if (AR == AliasResult::MustAlias)
2387 return ReachingMemVal::getDef(Loc.Ptr, ClobberI);
2388
2389 if (AR == AliasResult::NoAlias) {
2390 // If the locations do not alias we may still be able to skip over the
2391 // clobbering instruction, even if it is atomic.
2392 // The original load is either non-atomic or unordered. We can reorder
2393 // these across non-atomic, unordered or monotonic loads or across any
2394 // store.
2395 if (!ClobberI->isAtomic() ||
2396 !isStrongerThan(GetOrdering(ClobberI), AtomicOrdering::Monotonic) ||
2397 isa<StoreInst>(ClobberI))
2398 return std::nullopt;
2399 return ReachingMemVal::getClobber(Loc.Ptr, ClobberI);
2400 }
2401
2402 // Skip over volatile loads (the original load is non-volatile, non-atomic).
2403 if (!ClobberI->isAtomic() && isa<LoadInst>(ClobberI))
2404 return std::nullopt;
2405
2406 if (AR == AliasResult::MayAlias ||
2408 (!AR.hasOffset() || AR.getOffset() < 0)))
2409 return ReachingMemVal::getClobber(Loc.Ptr, ClobberI);
2410
2411 // The only option left is a store of the superset of the required bits.
2413 AR.getOffset() > 0 &&
2414 "Must be the superset/partial overlap case with positive offset");
2415 return ReachingMemVal::getClobber(Loc.Ptr, ClobberI, AR.getOffset());
2416 }
2417
2418 if (auto *II = dyn_cast<IntrinsicInst>(ClobberI)) {
2420 return std::nullopt;
2421 if (II->getIntrinsicID() == Intrinsic::lifetime_start) {
2422 MemoryLocation IIObjLoc = MemoryLocation::getForArgument(II, 0, TLI);
2423 if (AA.isMustAlias(IIObjLoc, Loc))
2424 return ReachingMemVal::getDef(Loc.Ptr, ClobberI);
2425 return std::nullopt;
2426 }
2427 }
2428
2429 // If we are at a malloc-like function call, we can turn the load into `undef`
2430 // or zero.
2431 if (isNoAliasCall(ClobberI)) {
2432 const Value *Obj = getUnderlyingObject(Loc.Ptr);
2433 if (Obj == ClobberI || AA.isMustAlias(ClobberI, Loc.Ptr))
2434 return ReachingMemVal::getDef(Loc.Ptr, ClobberI);
2435 }
2436
2437 // Can reorder loads across a release fence.
2438 if (auto *FI = dyn_cast<FenceInst>(ClobberI))
2439 if (FI->getOrdering() == AtomicOrdering::Release)
2440 return std::nullopt;
2441
2442 // See if the clobber instruction (e.g., a generic call) may modify the
2443 // location.
2444 ModRefInfo MR = AA.getModRefInfo(ClobberI, Loc);
2445 // If may modify the location, analyze deeper, to exclude accesses to
2446 // non-escaping local allocations.
2447 if (MR == ModRefInfo::NoModRef || MR == ModRefInfo::Ref)
2448 return std::nullopt;
2449
2450 // Conservatively assume the clobbering memory access may overwrite the
2451 // location.
2452 return ReachingMemVal::getClobber(Loc.Ptr, ClobberI);
2453}
2454
2455/// Collect the predecessors of block, while doing phi-translation of the memory
2456/// address and the memory clobber. Return false if the block should be marked
2457/// as clobbering the memory location in an unknown way.
2458bool GVNPass::collectPredecessors(BasicBlock *BB, const PHITransAddr &Addr,
2459 MemoryAccess *ClobberMA,
2460 DependencyBlockSet &Blocks,
2461 SmallVectorImpl<BasicBlock *> &Worklist) {
2462 if (Addr.needsPHITranslationFromBlock(BB) &&
2464 return false;
2465
2466 auto *MPhi =
2467 ClobberMA->getBlock() == BB ? dyn_cast<MemoryPhi>(ClobberMA) : nullptr;
2469 for (BasicBlock *Pred : predecessors(BB)) {
2470 // Skip unreachable predecessors.
2471 if (!DT->isReachableFromEntry(Pred))
2472 continue;
2473
2474 // Skip already visited predecessors.
2475 if (llvm::any_of(Preds, [Pred](const auto &P) { return P.first == Pred; }))
2476 continue;
2477
2478 PHITransAddr TransAddr = Addr;
2479 if (TransAddr.needsPHITranslationFromBlock(BB))
2480 TransAddr.translateValue(BB, Pred, DT, false);
2481
2482 auto It = Blocks.find(Pred);
2483 if (It != Blocks.end()) {
2484 // If we reach a visited block with a different address, set the
2485 // current block as clobbering the memory location in an unknown way
2486 // (by returning false).
2487 if (It->second.Addr.getAddr() != TransAddr.getAddr())
2488 return false;
2489 // Otherwise, just stop the traversal.
2490 continue;
2491 }
2492
2493 Preds.emplace_back(
2494 Pred, DependencyBlockInfo(TransAddr,
2495 MPhi ? MPhi->getIncomingValueForBlock(Pred)
2496 : ClobberMA));
2497 }
2498
2499 // We collected the predecessors and stored them in Preds. Now, populate the
2500 // worklist with the predecessors found, and cache the eventual translated
2501 // address for each block.
2502 for (auto &P : Preds) {
2503 [[maybe_unused]] auto It =
2504 Blocks.try_emplace(P.first, std::move(P.second)).first;
2505 Worklist.push_back(P.first);
2506 }
2507
2508 return true;
2509}
2510
2511/// Build a list of MemoryAccesses whose users could potentially alias the
2512/// memory location being queried. Starts from StartInfo's initial clobber,
2513/// walk the use-def chain to the final clobber. If the chain extends beyond
2514/// `BB`, continue into that block but only if it is in the previously collected
2515/// set.
2516void GVNPass::collectClobberList(SmallVectorImpl<MemoryAccess *> &Clobbers,
2517 BasicBlock *BB,
2518 const DependencyBlockInfo &StartInfo,
2519 const DependencyBlockSet &Blocks,
2520 MemorySSA &MSSA) {
2521 MemoryAccess *MA = StartInfo.InitialClobberMA;
2522 MemoryAccess *LastMA = StartInfo.ClobberMA;
2523
2524 for (;;) {
2525 while (MA != LastMA) {
2526 Clobbers.push_back(MA);
2527 MA = cast<MemoryUseOrDef>(MA)->getDefiningAccess();
2528 }
2529 Clobbers.push_back(MA);
2530
2531 if (MSSA.isLiveOnEntryDef(MA) ||
2532 (MA->getBlock() == BB && !isa<MemoryPhi>(MA)))
2533 break;
2534
2535 // If the final clobber in the current block is a MemoryPhi, go to the
2536 // immediate dominator; otherwise, just get to the block containing the
2537 // final clobber.
2538 if (MA->getBlock() == BB)
2539 BB = DT->getNode(BB)->getIDom()->getBlock();
2540 else
2541 BB = MA->getBlock();
2542
2543 auto It = Blocks.find(BB);
2544 if (It == Blocks.end())
2545 break;
2546
2547 MA = It->second.InitialClobberMA;
2548 LastMA = It->second.ClobberMA;
2549 if (MA == Clobbers.back())
2550 Clobbers.pop_back();
2551 }
2552}
2553
2554/// Entrypoint for the MemorySSA-based redundant load elimination algorithm.
2555/// Given as input a load instruction, the function computes the set of reaching
2556/// memory values, one per predecessor path, that AnalyzeLoadAvailability can
2557/// later use to establish whether the load may be eliminated. A reaching value
2558/// may be of the following descriptor kind:
2559/// * Def: a precise instruction that produces the exact bits the load would
2560/// read (e.g., an equivalent load or a MustAlias store);
2561/// * Clobber: a write that clobbers a superset of the bits the load would read
2562/// (e.g., a memset over a larger region);
2563/// * Other: we know which block defines the memory location in some way, but
2564/// could not identify a precise instruction (e.g., memory already live at
2565/// function entry).
2566bool GVNPass::findReachingValuesForLoad(LoadInst *L,
2567 SmallVectorImpl<ReachingMemVal> &Values,
2568 MemorySSA &MSSA, AAResults &AAR) {
2569 EarliestEscapeAnalysis EA(*DT, LI);
2570 BatchAAResults AA(AAR, &EA);
2571 BasicBlock *StartBlock = L->getParent();
2572 bool IsInvariantLoad = L->hasMetadata(LLVMContext::MD_invariant_load);
2573 // TODO: Simplify later work by just getClobberingMemoryAccess().
2574 MemoryAccess *ClobberMA = MSSA.getMemoryAccess(L)->getDefiningAccess();
2575 const MemoryLocation Loc = MemoryLocation::get(L);
2576
2577 // Fast path for load tagged with !invariant.group.
2578 if (L->hasMetadata(LLVMContext::MD_invariant_group)) {
2579 if (Instruction *G = findInvariantGroupValue(L, *DT)) {
2580 Values.emplace_back(
2581 ReachingMemVal::getDef(getLoadStorePointerOperand(G), G));
2582 return true;
2583 }
2584 }
2585
2586 // Phase 1. First off, look for a local dependency to avoid having to
2587 // disambiguate between before the load and after the load of the starting
2588 // block (as the load may be visited from a backedge).
2589 do {
2590 // Scan users of the clobbering memory access.
2591 if (auto RMV = scanMemoryAccessesUsers(
2592 Loc, IsInvariantLoad, StartBlock,
2593 SmallVector<MemoryAccess *, 1>{ClobberMA}, MSSA, AA, L)) {
2594 Values.emplace_back(*RMV);
2595 return true;
2596 }
2597
2598 // Exit from here, and proceed visiting predecessors if the clobbering
2599 // access is non-local or is a MemoryPhi.
2600 if (ClobberMA->getBlock() != StartBlock || isa<MemoryPhi>(ClobberMA))
2601 break;
2602
2603 // Check if the clobber actually aliases the load location.
2604 if (auto RMV = accessMayModifyLocation(ClobberMA, Loc, IsInvariantLoad,
2605 StartBlock, MSSA, AA)) {
2606 Values.emplace_back(*RMV);
2607 return true;
2608 }
2609
2610 // It may happen that the clobbering memory access does not actually
2611 // clobber our load location, transition to its defining memory access.
2612 ClobberMA = cast<MemoryUseOrDef>(ClobberMA)->getDefiningAccess();
2613 } while (ClobberMA->getBlock() == StartBlock);
2614
2615 // Non-local speculations are not allowed under ASan.
2616 if (L->getFunction()->hasFnAttribute(Attribute::SanitizeAddress) ||
2617 L->getFunction()->hasFnAttribute(Attribute::SanitizeHWAddress))
2618 return false;
2619
2620 // Phase 2. Walk backwards through the CFG, collecting all the blocks that
2621 // contain an instruction that modifies the load memory location, or that lie
2622 // on a path between a clobbering block and our load. Start off by collecting
2623 // the predecessors of `StartBlock`. All the visited blocks are stored in a
2624 // the set `Blocks`. If possible, the memory address maintained for the block
2625 // visited does get phi-translated.
2626 DependencyBlockSet Blocks;
2627 SmallVector<BasicBlock *, 16> InitialWorklist;
2628 const DataLayout &DL = L->getModule()->getDataLayout();
2629 if (!collectPredecessors(StartBlock,
2630 PHITransAddr(L->getPointerOperand(), DL, AC),
2631 ClobberMA, Blocks, InitialWorklist))
2632 return false;
2633
2634 // Do a bottom-up DFS.
2635 auto Worklist = InitialWorklist;
2636 while (!Worklist.empty()) {
2637 auto *BB = Worklist.pop_back_val();
2638 DependencyBlockInfo &Info = Blocks.find(BB)->second;
2639
2640 // Phi-translation may have failed.
2641 if (!Info.Addr.getAddr())
2642 continue;
2643
2644 // If the clobbering memory access is in the current block and it indeed
2645 // clobbers our load location, record the dependency and do not visit the
2646 // predecessors of this block further, continue with the blocks in the
2647 // worklist.
2648 if (Info.ClobberMA->getBlock() == BB && !isa<MemoryPhi>(Info.ClobberMA)) {
2649 if (auto RMV = accessMayModifyLocation(
2650 Info.ClobberMA, Loc.getWithNewPtr(Info.Addr.getAddr()),
2651 IsInvariantLoad, BB, MSSA, AA)) {
2652 Info.MemVal = RMV;
2653 continue;
2654 }
2655 assert(!MSSA.isLiveOnEntryDef(Info.ClobberMA) &&
2656 "LiveOnEntry aliases everything");
2657
2658 // If, however, the clobbering memory access does not actually clobber
2659 // our load location, transition to its defining memory access, but
2660 // keep examining the same basic block.
2661 Info.ClobberMA =
2662 cast<MemoryUseOrDef>(Info.ClobberMA)->getDefiningAccess();
2663 Worklist.emplace_back(BB);
2664 continue;
2665 }
2666
2667 // At this point we know the current block is "transparent", i.e. the memory
2668 // location is not modified when execution goes through this block.
2669 // Continue to its predecessors, unless a predecessor has already been
2670 // visited with a different address. We currently cannot represent such a
2671 // dependency.
2672 if (BB == StartBlock && Info.Addr.getAddr() != L->getPointerOperand()) {
2673 Info.ForceUnknown = true;
2674 continue;
2675 }
2676 if (BB != StartBlock &&
2677 !collectPredecessors(BB, Info.Addr, Info.ClobberMA, Blocks, Worklist))
2678 Info.ForceUnknown = true;
2679 }
2680
2681 // Phase 3. We have collected all the blocks that either write a value to the
2682 // memory location of the load, or there exists a path to the load, along
2683 // which the memory location is not modified. Perform a second DFS to find
2684 // load-to-load dependencies; namely, look at the dominating memory reads,
2685 // that alias our load. These are the MemoryUses that are users of the
2686 // MemoryDefs we previously identified. If no memory read is encountered,
2687 // either confirm the clobbering write found before or set to unknown.
2688 Worklist = InitialWorklist;
2689 for (BasicBlock *BB : Worklist) {
2690 DependencyBlockInfo &Info = Blocks.find(BB)->second;
2691 Info.Visited = true;
2692 }
2693
2695 while (!Worklist.empty()) {
2696 auto *BB = Worklist.pop_back_val();
2697 DependencyBlockInfo &Info = Blocks.find(BB)->second;
2698
2699 // If phi-translation failed, assume the memory location is modified in
2700 // unknown way.
2701 if (!Info.Addr.getAddr()) {
2702 Values.push_back(ReachingMemVal::getUnknown(BB, nullptr));
2703 continue;
2704 }
2705
2706 Clobbers.clear();
2707 collectClobberList(Clobbers, BB, Info, Blocks, MSSA);
2708 if (auto RMV =
2709 scanMemoryAccessesUsers(Loc.getWithNewPtr(Info.Addr.getAddr()),
2710 IsInvariantLoad, BB, Clobbers, MSSA, AA)) {
2711 Values.push_back(*RMV);
2712 continue;
2713 }
2714
2715 // If no reusable memory use was found, and the current block is not
2716 // transparent, use the already established memory def.
2717 if (Info.MemVal) {
2718 Values.push_back(*Info.MemVal);
2719 continue;
2720 }
2721
2722 if (Info.ForceUnknown) {
2723 Values.push_back(ReachingMemVal::getUnknown(BB, Info.Addr.getAddr()));
2724 continue;
2725 }
2726
2727 // If the current block is transparent, continue to its predecessors.
2728 for (BasicBlock *Pred : predecessors(BB)) {
2729 auto It = Blocks.find(Pred);
2730 if (It == Blocks.end())
2731 continue;
2732 DependencyBlockInfo &PredInfo = It->second;
2733 if (PredInfo.Visited)
2734 continue;
2735 PredInfo.Visited = true;
2736 Worklist.push_back(Pred);
2737 }
2738 }
2739
2740 return true;
2741}
2742
2743/// Attempt to eliminate a load, first by eliminating it
2744/// locally, and then attempting non-local elimination if that fails.
2745bool GVNPass::processLoad(LoadInst *L) {
2746 if (!MD && !isMemorySSAEnabled())
2747 return false;
2748
2749 // This code hasn't been audited for ordered or volatile memory access.
2750 if (!L->isUnordered())
2751 return false;
2752
2753 if (L->getType()->isTokenLikeTy())
2754 return false;
2755
2756 if (L->use_empty()) {
2758 return true;
2759 }
2760
2761 ReachingMemVal MemVal = ReachingMemVal::getUnknown(nullptr, nullptr);
2762 if (!isMemorySSAEnabled()) {
2763 // ... to a pointer that has been loaded from before...
2764 MemDepResult Dep = MD->getDependency(L);
2765
2766 // If it is defined in another block, try harder.
2767 if (Dep.isNonLocal())
2768 return processNonLocalLoad(L);
2769
2770 // Only handle the local case below.
2771 if (Dep.isDef())
2772 MemVal = ReachingMemVal::getDef(L->getPointerOperand(), Dep.getInst());
2773 else if (Dep.isClobber())
2774 MemVal =
2775 ReachingMemVal::getClobber(L->getPointerOperand(), Dep.getInst());
2776 } else {
2778 if (!findReachingValuesForLoad(L, MemVals, *MSSAU->getMemorySSA(), *AA))
2779 return false; // Too many dependencies.
2780 assert(MemVals.size() && "Expected at least an unknown value");
2781 if (MemVals.size() > 1 || MemVals[0].Block != L->getParent())
2782 return processNonLocalLoad(L, MemVals);
2783
2784 MemVal = MemVals[0];
2785 }
2786
2787 if (MemVal.Kind == DepKind::Other) {
2788 // This might be a NonFuncLocal or an Unknown.
2789 LLVM_DEBUG(
2790 // fast print dep, using operator<< on instruction is too slow.
2791 dbgs() << "GVN: load "; L->printAsOperand(dbgs());
2792 dbgs() << " has unknown dependence\n";);
2793 return false;
2794 }
2795
2796 auto AV = AnalyzeLoadAvailability(L, MemVal, L->getPointerOperand());
2797 if (!AV)
2798 return false;
2799
2800 Value *AvailableValue = AV->MaterializeAdjustedValue(L, L);
2801
2802 // MaterializeAdjustedValue is responsible for combining metadata.
2803 ICF->removeUsersOf(L);
2804 L->replaceAllUsesWith(AvailableValue);
2805 if (MSSAU)
2806 MSSAU->removeMemoryAccess(L);
2807 ++NumGVNLoad;
2808 reportLoadElim(L, AvailableValue, ORE);
2810 // Tell MDA to reexamine the reused pointer since we might have more
2811 // information after forwarding it.
2812 if (MD && AvailableValue->getType()->isPtrOrPtrVectorTy())
2813 MD->invalidateCachedPointerInfo(AvailableValue);
2814 return true;
2815}
2816
2817// Attempt to process masked loads which have loaded from
2818// masked stores with the same mask
2819bool GVNPass::processMaskedLoad(IntrinsicInst *I) {
2820 if (!MD)
2821 return false;
2822 MemDepResult Dep = MD->getDependency(I);
2823 Instruction *DepInst = Dep.getInst();
2824 if (!DepInst || !Dep.isLocal() || !Dep.isDef())
2825 return false;
2826
2827 Value *Mask = I->getOperand(1);
2828 Value *Passthrough = I->getOperand(2);
2829 Value *StoreVal;
2830 if (!match(DepInst,
2831 m_MaskedStore(m_Value(StoreVal), m_Value(), m_Specific(Mask))) ||
2832 StoreVal->getType() != I->getType())
2833 return false;
2834
2835 // Remove the load but generate a select for the passthrough
2836 Value *OpToForward = llvm::SelectInst::Create(Mask, StoreVal, Passthrough, "",
2837 I->getIterator());
2838
2839 ICF->removeUsersOf(I);
2840 I->replaceAllUsesWith(OpToForward);
2842 ++NumGVNLoad;
2843 return true;
2844}
2845
2846/// Return a pair the first field showing the value number of \p Exp and the
2847/// second field showing whether it is a value number newly created.
2848std::pair<uint32_t, bool>
2849GVNPass::ValueTable::assignExpNewValueNum(Expression &Exp) {
2850 uint32_t &E = ExpressionNumbering[Exp];
2851 bool CreateNewValNum = !E;
2852 if (CreateNewValNum) {
2853 Expressions.push_back(Exp);
2854 if (ExprIdx.size() < NextValueNumber + 1)
2855 ExprIdx.resize(NextValueNumber * 2);
2856 E = NextValueNumber;
2857 ExprIdx[NextValueNumber++] = NextExprNumber++;
2858 }
2859 return {E, CreateNewValNum};
2860}
2861
2862/// Return whether all the values related with the same \p num are
2863/// defined in \p BB.
2864bool GVNPass::ValueTable::areAllValsInBB(uint32_t Num, const BasicBlock *BB,
2865 GVNPass &GVN) {
2866 return all_of(
2867 GVN.LeaderTable.getLeaders(Num),
2868 [=](const LeaderMap::LeaderTableEntry &L) { return L.BB == BB; });
2869}
2870
2871/// Wrap phiTranslateImpl to provide caching functionality.
2872uint32_t GVNPass::ValueTable::phiTranslate(const BasicBlock *Pred,
2873 const BasicBlock *PhiBlock,
2874 uint32_t Num, GVNPass &GVN) {
2875 auto FindRes = PhiTranslateTable.find({Num, Pred});
2876 if (FindRes != PhiTranslateTable.end())
2877 return FindRes->second;
2878 uint32_t NewNum = phiTranslateImpl(Pred, PhiBlock, Num, GVN);
2879 PhiTranslateTable.insert({{Num, Pred}, NewNum});
2880 return NewNum;
2881}
2882
2883// Return true if the value number \p Num and NewNum have equal value.
2884// Return false if the result is unknown.
2885bool GVNPass::ValueTable::areCallValsEqual(uint32_t Num, uint32_t NewNum,
2886 const BasicBlock *Pred,
2887 const BasicBlock *PhiBlock,
2888 GVNPass &GVN) {
2889 CallInst *Call = nullptr;
2890 auto Leaders = GVN.LeaderTable.getLeaders(Num);
2891 for (const auto &Entry : Leaders) {
2892 Call = dyn_cast<CallInst>(&*Entry.Val);
2893 if (Call && Call->getParent() == PhiBlock)
2894 break;
2895 }
2896
2897 if (AA->doesNotAccessMemory(Call))
2898 return true;
2899
2900 if (!MD || !AA->onlyReadsMemory(Call))
2901 return false;
2902
2903 MemDepResult LocalDep = MD->getDependency(Call);
2904 if (!LocalDep.isNonLocal())
2905 return false;
2906
2909
2910 // Check to see if the Call has no function local clobber.
2911 for (const NonLocalDepEntry &D : Deps) {
2912 if (D.getResult().isNonFuncLocal())
2913 return true;
2914 }
2915 return false;
2916}
2917
2918/// Translate value number \p Num using phis, so that it has the values of
2919/// the phis in BB.
2920uint32_t GVNPass::ValueTable::phiTranslateImpl(const BasicBlock *Pred,
2921 const BasicBlock *PhiBlock,
2922 uint32_t Num, GVNPass &GVN) {
2923 // See if we can refine the value number by looking at the PN incoming value
2924 // for the given predecessor.
2925 if (PHINode *PN = NumberingPhi[Num]) {
2926 if (PN->getParent() != PhiBlock)
2927 return Num;
2928 for (unsigned I = 0; I != PN->getNumIncomingValues(); ++I) {
2929 if (PN->getIncomingBlock(I) != Pred)
2930 continue;
2931 if (uint32_t TransVal = lookup(PN->getIncomingValue(I), false))
2932 return TransVal;
2933 }
2934 return Num;
2935 }
2936
2937 if (BasicBlock *BB = NumberingBB[Num]) {
2938 assert(MSSA && "NumberingBB is non-empty only when using MemorySSA");
2939 // Value numbers of basic blocks are used to represent memory state in
2940 // load/store instructions and read-only function calls when said state is
2941 // set by a MemoryPhi.
2942 if (BB != PhiBlock)
2943 return Num;
2944 MemoryPhi *MPhi = MSSA->getMemoryAccess(BB);
2945 for (unsigned i = 0, N = MPhi->getNumIncomingValues(); i != N; ++i) {
2946 if (MPhi->getIncomingBlock(i) != Pred)
2947 continue;
2948 MemoryAccess *MA = MPhi->getIncomingValue(i);
2949 if (auto *PredPhi = dyn_cast<MemoryPhi>(MA))
2950 return lookupOrAdd(PredPhi->getBlock());
2951 if (MSSA->isLiveOnEntryDef(MA))
2952 return lookupOrAdd(&BB->getParent()->getEntryBlock());
2953 return lookupOrAdd(cast<MemoryUseOrDef>(MA)->getMemoryInst());
2954 }
2956 "CFG/MemorySSA mismatch: predecessor not found among incoming blocks");
2957 }
2958
2959 // If there is any value related with Num is defined in a BB other than
2960 // PhiBlock, it cannot depend on a phi in PhiBlock without going through
2961 // a backedge. We can do an early exit in that case to save compile time.
2962 if (!areAllValsInBB(Num, PhiBlock, GVN))
2963 return Num;
2964
2965 if (Num >= ExprIdx.size() || ExprIdx[Num] == 0)
2966 return Num;
2967 Expression Exp = Expressions[ExprIdx[Num]];
2968
2969 for (unsigned I = 0; I < Exp.VarArgs.size(); I++) {
2970 // For InsertValue and ExtractValue, some varargs are index numbers
2971 // instead of value numbers. Those index numbers should not be
2972 // translated.
2973 if ((I > 1 && Exp.Opcode == Instruction::InsertValue) ||
2974 (I > 0 && Exp.Opcode == Instruction::ExtractValue) ||
2975 (I > 1 && Exp.Opcode == Instruction::ShuffleVector))
2976 continue;
2977 Exp.VarArgs[I] = phiTranslate(Pred, PhiBlock, Exp.VarArgs[I], GVN);
2978 }
2979
2980 if (Exp.Commutative) {
2981 assert(Exp.VarArgs.size() >= 2 && "Unsupported commutative instruction!");
2982 if (Exp.VarArgs[0] > Exp.VarArgs[1]) {
2983 std::swap(Exp.VarArgs[0], Exp.VarArgs[1]);
2984 uint32_t Opcode = Exp.Opcode >> 8;
2985 if (Opcode == Instruction::ICmp || Opcode == Instruction::FCmp)
2986 Exp.Opcode = (Opcode << 8) |
2988 static_cast<CmpInst::Predicate>(Exp.Opcode & 255));
2989 }
2990 }
2991
2992 if (uint32_t NewNum = ExpressionNumbering[Exp]) {
2993 if (Exp.Opcode == Instruction::Call && NewNum != Num)
2994 return areCallValsEqual(Num, NewNum, Pred, PhiBlock, GVN) ? NewNum : Num;
2995 return NewNum;
2996 }
2997 return Num;
2998}
2999
3000/// Erase stale entry from phiTranslate cache so phiTranslate can be computed
3001/// again.
3002void GVNPass::ValueTable::eraseTranslateCacheEntry(
3003 uint32_t Num, const BasicBlock &CurrBlock) {
3004 for (const BasicBlock *Pred : predecessors(&CurrBlock))
3005 PhiTranslateTable.erase({Num, Pred});
3006}
3007
3008// In order to find a leader for a given value number at a
3009// specific basic block, we first obtain the list of all Values for that number,
3010// and then scan the list to find one whose block dominates the block in
3011// question. This is fast because dominator tree queries consist of only
3012// a few comparisons of DFS numbers.
3013Value *GVNPass::findLeader(const BasicBlock *BB, uint32_t Num) {
3014 auto Leaders = LeaderTable.getLeaders(Num);
3015 if (Leaders.empty())
3016 return nullptr;
3017
3018 Value *Val = nullptr;
3019 for (const auto &Entry : Leaders) {
3020 if (DT->dominates(Entry.BB, BB)) {
3021 Val = Entry.Val;
3022 if (isa<Constant>(Val))
3023 return Val;
3024 }
3025 }
3026
3027 return Val;
3028}
3029
3030/// There is an edge from 'Src' to 'Dst'. Return
3031/// true if every path from the entry block to 'Dst' passes via this edge. In
3032/// particular 'Dst' must not be reachable via another edge from 'Src'.
3034 DominatorTree *DT) {
3035 // While in theory it is interesting to consider the case in which Dst has
3036 // more than one predecessor, because Dst might be part of a loop which is
3037 // only reachable from Src, in practice it is pointless since at the time
3038 // GVN runs all such loops have preheaders, which means that Dst will have
3039 // been changed to have only one predecessor, namely Src.
3040 const BasicBlock *Pred = E.getEnd()->getSinglePredecessor();
3041 assert((!Pred || Pred == E.getStart()) &&
3042 "No edge between these basic blocks!");
3043 return Pred != nullptr;
3044}
3045
3046void GVNPass::assignBlockRPONumber(Function &F) {
3047 BlockRPONumber.clear();
3048 uint32_t NextBlockNumber = 1;
3049 ReversePostOrderTraversal<Function *> RPOT(&F);
3050 for (BasicBlock *BB : RPOT)
3051 BlockRPONumber[BB] = NextBlockNumber++;
3052 InvalidBlockRPONumbers = false;
3053}
3054
3055/// The given values are known to be equal in every use
3056/// dominated by 'Root'. Exploit this, for example by replacing 'LHS' with
3057/// 'RHS' everywhere in the scope. Returns whether a change was made.
3058/// The Root may either be a basic block edge (for conditions) or an
3059/// instruction (for assumes).
3060bool GVNPass::propagateEquality(
3061 Value *LHS, Value *RHS,
3062 const std::variant<BasicBlockEdge, Instruction *> &Root) {
3064 Worklist.push_back(std::make_pair(LHS, RHS));
3065 bool Changed = false;
3066 SmallVector<const BasicBlock *> DominatedBlocks;
3067 if (const BasicBlockEdge *Edge = std::get_if<BasicBlockEdge>(&Root)) {
3068 // For speed, compute a conservative fast approximation to
3069 // DT->dominates(Root, Root.getEnd());
3071 DominatedBlocks.push_back(Edge->getEnd());
3072 } else {
3073 Instruction *I = std::get<Instruction *>(Root);
3074 for (const auto *Node : DT->getNode(I->getParent())->children())
3075 DominatedBlocks.push_back(Node->getBlock());
3076 }
3077
3078 while (!Worklist.empty()) {
3079 std::pair<Value*, Value*> Item = Worklist.pop_back_val();
3080 LHS = Item.first; RHS = Item.second;
3081
3082 if (LHS == RHS)
3083 continue;
3084 assert(LHS->getType() == RHS->getType() && "Equality but unequal types!");
3085
3086 // Don't try to propagate equalities between constants.
3088 continue;
3089
3090 // Prefer a constant on the right-hand side, or an Argument if no constants.
3092 std::swap(LHS, RHS);
3093 assert((isa<Argument>(LHS) || isa<Instruction>(LHS)) && "Unexpected value!");
3094 const DataLayout &DL =
3096 ? cast<Argument>(LHS)->getParent()->getDataLayout()
3097 : cast<Instruction>(LHS)->getDataLayout();
3098
3099 // If there is no obvious reason to prefer the left-hand side over the
3100 // right-hand side, ensure the longest lived term is on the right-hand side,
3101 // so the shortest lived term will be replaced by the longest lived.
3102 // This tends to expose more simplifications.
3103 uint32_t LVN = VN.lookupOrAdd(LHS);
3104 if ((isa<Argument>(LHS) && isa<Argument>(RHS)) ||
3106 // Move the 'oldest' value to the right-hand side, using the value number
3107 // as a proxy for age.
3108 uint32_t RVN = VN.lookupOrAdd(RHS);
3109 if (LVN < RVN) {
3110 std::swap(LHS, RHS);
3111 LVN = RVN;
3112 }
3113 }
3114
3115 // If value numbering later sees that an instruction in the scope is equal
3116 // to 'LHS' then ensure it will be turned into 'RHS'. In order to preserve
3117 // the invariant that instructions only occur in the leader table for their
3118 // own value number (this is used by removeFromLeaderTable), do not do this
3119 // if RHS is an instruction (if an instruction in the scope is morphed into
3120 // LHS then it will be turned into RHS by the next GVN iteration anyway, so
3121 // using the leader table is about compiling faster, not optimizing better).
3122 // The leader table only tracks basic blocks, not edges. Only add to if we
3123 // have the simple case where the edge dominates the end.
3125 for (const BasicBlock *BB : DominatedBlocks)
3126 LeaderTable.insert(LVN, RHS, BB);
3127
3128 // Replace all occurrences of 'LHS' with 'RHS' everywhere in the scope. As
3129 // LHS always has at least one use that is not dominated by Root, this will
3130 // never do anything if LHS has only one use.
3131 if (!LHS->hasOneUse()) {
3132 // Create a callback that captures the DL.
3133 auto CanReplacePointersCallBack = [&DL](const Use &U, const Value *To) {
3134 return canReplacePointersInUseIfEqual(U, To, DL);
3135 };
3136 unsigned NumReplacements;
3137 if (const BasicBlockEdge *Edge = std::get_if<BasicBlockEdge>(&Root))
3138 NumReplacements = replaceDominatedUsesWithIf(
3139 LHS, RHS, *DT, *Edge, CanReplacePointersCallBack);
3140 else
3141 NumReplacements = replaceDominatedUsesWithIf(
3142 LHS, RHS, *DT, std::get<Instruction *>(Root),
3143 CanReplacePointersCallBack);
3144
3145 if (NumReplacements > 0) {
3146 Changed = true;
3147 NumGVNEqProp += NumReplacements;
3148 // Cached information for anything that uses LHS will be invalid.
3149 if (MD)
3150 MD->invalidateCachedPointerInfo(LHS);
3151 }
3152 }
3153
3154 // Now try to deduce additional equalities from this one. For example, if
3155 // the known equality was "(A != B)" == "false" then it follows that A and B
3156 // are equal in the scope. Only boolean equalities with an explicit true or
3157 // false RHS are currently supported.
3158 if (!RHS->getType()->isIntegerTy(1))
3159 // Not a boolean equality - bail out.
3160 continue;
3161 ConstantInt *CI = dyn_cast<ConstantInt>(RHS);
3162 if (!CI)
3163 // RHS neither 'true' nor 'false' - bail out.
3164 continue;
3165 // Whether RHS equals 'true'. Otherwise it equals 'false'.
3166 bool IsKnownTrue = CI->isMinusOne();
3167 bool IsKnownFalse = !IsKnownTrue;
3168
3169 // If "A && B" is known true then both A and B are known true. If "A || B"
3170 // is known false then both A and B are known false.
3171 Value *A, *B;
3172 if ((IsKnownTrue && match(LHS, m_LogicalAnd(m_Value(A), m_Value(B)))) ||
3173 (IsKnownFalse && match(LHS, m_LogicalOr(m_Value(A), m_Value(B))))) {
3174 Worklist.push_back(std::make_pair(A, RHS));
3175 Worklist.push_back(std::make_pair(B, RHS));
3176 continue;
3177 }
3178
3179 // If we are propagating an equality like "(A == B)" == "true" then also
3180 // propagate the equality A == B. When propagating a comparison such as
3181 // "(A >= B)" == "true", replace all instances of "A < B" with "false".
3182 if (CmpInst *Cmp = dyn_cast<CmpInst>(LHS)) {
3183 Value *Op0 = Cmp->getOperand(0), *Op1 = Cmp->getOperand(1);
3184
3185 // If "A == B" is known true, or "A != B" is known false, then replace
3186 // A with B everywhere in the scope. For floating point operations, we
3187 // have to be careful since equality does not always imply equivalance.
3188 if (Cmp->isEquivalence(IsKnownFalse))
3189 Worklist.push_back(std::make_pair(Op0, Op1));
3190
3191 // If "A >= B" is known true, replace "A < B" with false everywhere.
3192 CmpInst::Predicate NotPred = Cmp->getInversePredicate();
3193 Constant *NotVal = ConstantInt::get(Cmp->getType(), IsKnownFalse);
3194 // Since we don't have the instruction "A < B" immediately to hand, work
3195 // out the value number that it would have and use that to find an
3196 // appropriate instruction (if any).
3197 uint32_t NextNum = VN.getNextUnusedValueNumber();
3198 uint32_t Num = VN.lookupOrAddCmp(Cmp->getOpcode(), NotPred, Op0, Op1);
3199 // If the number we were assigned was brand new then there is no point in
3200 // looking for an instruction realizing it: there cannot be one!
3201 if (Num < NextNum) {
3202 for (const auto &Entry : LeaderTable.getLeaders(Num)) {
3203 // Only look at leaders that either dominate the start of the edge,
3204 // or are dominated by the end. This check is not necessary for
3205 // correctness, it only discards cases for which the following
3206 // use replacement will not work anyway.
3207 if (const BasicBlockEdge *Edge = std::get_if<BasicBlockEdge>(&Root)) {
3208 if (!DT->dominates(Entry.BB, Edge->getStart()) &&
3209 !DT->dominates(Edge->getEnd(), Entry.BB))
3210 continue;
3211 } else {
3212 auto *InstBB = std::get<Instruction *>(Root)->getParent();
3213 if (!DT->dominates(Entry.BB, InstBB) &&
3214 !DT->dominates(InstBB, Entry.BB))
3215 continue;
3216 }
3217
3218 Value *NotCmp = Entry.Val;
3219 if (NotCmp && isa<Instruction>(NotCmp)) {
3220 unsigned NumReplacements;
3221 if (const BasicBlockEdge *Edge = std::get_if<BasicBlockEdge>(&Root))
3222 NumReplacements =
3223 replaceDominatedUsesWith(NotCmp, NotVal, *DT, *Edge);
3224 else
3225 NumReplacements = replaceDominatedUsesWith(
3226 NotCmp, NotVal, *DT, std::get<Instruction *>(Root));
3227 Changed |= NumReplacements > 0;
3228 NumGVNEqProp += NumReplacements;
3229 // Cached information for anything that uses NotCmp will be invalid.
3230 if (MD)
3231 MD->invalidateCachedPointerInfo(NotCmp);
3232 }
3233 }
3234 }
3235 // Ensure that any instruction in scope that gets the "A < B" value number
3236 // is replaced with false.
3237 // The leader table only tracks basic blocks, not edges. Only add to if we
3238 // have the simple case where the edge dominates the end.
3239 for (const BasicBlock *BB : DominatedBlocks)
3240 LeaderTable.insert(Num, NotVal, BB);
3241
3242 continue;
3243 }
3244
3245 // Propagate equalities that results from truncation with no unsigned wrap
3246 // like (trunc nuw i64 %v to i1) == "true" or (trunc nuw i64 %v to i1) ==
3247 // "false"
3248 if (match(LHS, m_NUWTrunc(m_Value(A)))) {
3249 Worklist.emplace_back(A, ConstantInt::get(A->getType(), IsKnownTrue));
3250 continue;
3251 }
3252
3253 if (match(LHS, m_Not(m_Value(A)))) {
3254 Worklist.emplace_back(A, ConstantInt::get(A->getType(), !IsKnownTrue));
3255 continue;
3256 }
3257 }
3258
3259 return Changed;
3260}
3261
3262/// When calculating availability, handle an instruction
3263/// by inserting it into the appropriate sets.
3264bool GVNPass::processInstruction(Instruction *I) {
3265 // If the instruction can be easily simplified then do so now in preference
3266 // to value numbering it. Value numbering often exposes redundancies, for
3267 // example if it determines that %y is equal to %x then the instruction
3268 // "%z = and i32 %x, %y" becomes "%z = and i32 %x, %x" which we now simplify.
3269 const DataLayout &DL = I->getDataLayout();
3270 if (Value *V = simplifyInstruction(I, {DL, TLI, DT, AC})) {
3271 bool Changed = false;
3272 if (!I->use_empty()) {
3273 // Simplification can cause a special instruction to become not special.
3274 // For example, devirtualization to a willreturn function.
3275 ICF->removeUsersOf(I);
3276 I->replaceAllUsesWith(V);
3277 Changed = true;
3278 }
3279 if (isInstructionTriviallyDead(I, TLI)) {
3281 Changed = true;
3282 }
3283 if (Changed) {
3284 if (MD && V->getType()->isPtrOrPtrVectorTy())
3285 MD->invalidateCachedPointerInfo(V);
3286 ++NumGVNSimpl;
3287 return true;
3288 }
3289 }
3290
3291 if (auto *Assume = dyn_cast<AssumeInst>(I))
3292 return processAssumeIntrinsic(Assume);
3293
3294 if (LoadInst *Load = dyn_cast<LoadInst>(I)) {
3295 if (processLoad(Load))
3296 return true;
3297
3298 unsigned Num = VN.lookupOrAdd(Load);
3299 LeaderTable.insert(Num, Load, Load->getParent());
3300 return false;
3301 }
3302
3304 processMaskedLoad(cast<IntrinsicInst>(I)))
3305 return true;
3306
3307 // For conditional branches, we can perform simple conditional propagation on
3308 // the condition value itself.
3309 if (CondBrInst *BI = dyn_cast<CondBrInst>(I)) {
3310 if (isa<Constant>(BI->getCondition()))
3311 return processFoldableCondBr(BI);
3312
3313 Value *BranchCond = BI->getCondition();
3314 BasicBlock *TrueSucc = BI->getSuccessor(0);
3315 BasicBlock *FalseSucc = BI->getSuccessor(1);
3316 // Avoid multiple edges early.
3317 if (TrueSucc == FalseSucc)
3318 return false;
3319
3320 BasicBlock *Parent = BI->getParent();
3321 bool Changed = false;
3322
3324 BasicBlockEdge TrueE(Parent, TrueSucc);
3325 Changed |= propagateEquality(BranchCond, TrueVal, TrueE);
3326
3328 BasicBlockEdge FalseE(Parent, FalseSucc);
3329 Changed |= propagateEquality(BranchCond, FalseVal, FalseE);
3330
3331 return Changed;
3332 }
3333
3334 // For switches, propagate the case values into the case destinations.
3335 if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
3336 Value *SwitchCond = SI->getCondition();
3337 BasicBlock *Parent = SI->getParent();
3338 bool Changed = false;
3339
3340 // Remember how many outgoing edges there are to every successor.
3341 SmallDenseMap<BasicBlock *, unsigned, 16> SwitchEdges;
3342 for (BasicBlock *Succ : successors(Parent))
3343 ++SwitchEdges[Succ];
3344
3345 for (const auto &Case : SI->cases()) {
3346 BasicBlock *Dst = Case.getCaseSuccessor();
3347 // If there is only a single edge, propagate the case value into it.
3348 if (SwitchEdges.lookup(Dst) == 1) {
3349 BasicBlockEdge E(Parent, Dst);
3350 Changed |= propagateEquality(SwitchCond, Case.getCaseValue(), E);
3351 }
3352 }
3353 return Changed;
3354 }
3355
3356 // Instructions with void type don't return a value, so there's
3357 // no point in trying to find redundancies in them.
3358 if (I->getType()->isVoidTy())
3359 return false;
3360
3361 uint32_t NextNum = VN.getNextUnusedValueNumber();
3362 unsigned Num = VN.lookupOrAdd(I);
3363
3364 // Allocations are always uniquely numbered, so we can save time and memory
3365 // by fast failing them.
3366 if (isa<AllocaInst>(I) || I->isTerminator() || isa<PHINode>(I)) {
3367 LeaderTable.insert(Num, I, I->getParent());
3368 return false;
3369 }
3370
3371 // If the number we were assigned was a brand new VN, then we don't
3372 // need to do a lookup to see if the number already exists
3373 // somewhere in the domtree: it can't!
3374 if (Num >= NextNum) {
3375 LeaderTable.insert(Num, I, I->getParent());
3376 return false;
3377 }
3378
3379 // Perform fast-path value-number based elimination of values inherited from
3380 // dominators.
3381 Value *Repl = findLeader(I->getParent(), Num);
3382 if (!Repl) {
3383 // Failure, just remember this instance for future use.
3384 LeaderTable.insert(Num, I, I->getParent());
3385 return false;
3386 }
3387
3388 if (Repl == I) {
3389 // If I was the result of a shortcut PRE, it might already be in the table
3390 // and the best replacement for itself. Nothing to do.
3391 return false;
3392 }
3393
3394 // Remove it!
3396 if (MD && Repl->getType()->isPtrOrPtrVectorTy())
3397 MD->invalidateCachedPointerInfo(Repl);
3399 return true;
3400}
3401
3402/// runOnFunction - This is the main transformation entry point for a function.
3403bool GVNPass::runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
3404 const TargetLibraryInfo &RunTLI, AAResults &RunAA,
3405 MemoryDependenceResults *RunMD, LoopInfo &LI,
3406 OptimizationRemarkEmitter *RunORE, MemorySSA *MSSA) {
3407 AC = &RunAC;
3408 DT = &RunDT;
3409 VN.setDomTree(DT);
3410 TLI = &RunTLI;
3411 AA = &RunAA;
3412 VN.setAliasAnalysis(&RunAA);
3413 MD = RunMD;
3414 ImplicitControlFlowTracking ImplicitCFT;
3415 ICF = &ImplicitCFT;
3416 this->LI = &LI;
3417 VN.setMemDep(MD);
3418 // Propagate the MSSA-enabled flag so the value-numbering paths in
3419 // lookupOrAddCall() and computeLoadStoreVN(), which depends on whether
3420 // IsMSSAEnabled is turned on.
3421 VN.setMemorySSA(MSSA, isMemorySSAEnabled());
3422 ORE = RunORE;
3423 InvalidBlockRPONumbers = true;
3424 MemorySSAUpdater Updater(MSSA);
3425 MSSAU = MSSA ? &Updater : nullptr;
3426
3427 bool Changed = false;
3428 bool ShouldContinue = true;
3429
3430 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
3431 // Merge unconditional branches, allowing PRE to catch more
3432 // optimization opportunities.
3433 for (BasicBlock &BB : make_early_inc_range(F)) {
3434 bool RemovedBlock = MergeBlockIntoPredecessor(&BB, &DTU, &LI, MSSAU, MD);
3435 if (RemovedBlock)
3436 ++NumGVNBlocks;
3437
3438 Changed |= RemovedBlock;
3439 }
3440 DTU.flush();
3441
3442 unsigned Iteration = 0;
3443 while (ShouldContinue) {
3444 LLVM_DEBUG(dbgs() << "GVN iteration: " << Iteration << "\n");
3445 (void) Iteration;
3446 ShouldContinue = iterateOnFunction(F);
3447 Changed |= ShouldContinue;
3448 ++Iteration;
3449 }
3450
3451 if (isScalarPREEnabled()) {
3452 // Fabricate val-num for dead-code in order to suppress assertion in
3453 // performPRE().
3454 assignValNumForDeadCode();
3455 bool PREChanged = true;
3456 while (PREChanged) {
3457 PREChanged = performPRE(F);
3458 Changed |= PREChanged;
3459 }
3460 }
3461
3462 // FIXME: Should perform GVN again after PRE does something. PRE can move
3463 // computations into blocks where they become fully redundant. Note that
3464 // we can't do this until PRE's critical edge splitting updates memdep.
3465 // Actually, when this happens, we should just fully integrate PRE into GVN.
3466
3467 cleanupGlobalSets();
3468 // Do not cleanup DeadBlocks in cleanupGlobalSets() as it's called for each
3469 // iteration.
3470 DeadBlocks.clear();
3471
3472 if (MSSA && VerifyMemorySSA)
3473 MSSA->verifyMemorySSA();
3474
3475 return Changed;
3476}
3477
3478bool GVNPass::processBlock(BasicBlock *BB) {
3479 if (DeadBlocks.count(BB))
3480 return false;
3481
3482 bool ChangedFunction = false;
3483
3484 // Since we may not have visited the input blocks of the phis, we can't
3485 // use our normal hash approach for phis. Instead, simply look for
3486 // obvious duplicates. The first pass of GVN will tend to create
3487 // identical phis, and the second or later passes can eliminate them.
3488 SmallPtrSet<PHINode *, 8> PHINodesToRemove;
3489 ChangedFunction |= EliminateDuplicatePHINodes(BB, PHINodesToRemove);
3490 for (PHINode *PN : PHINodesToRemove) {
3491 removeInstruction(PN);
3492 }
3493 for (Instruction &Inst : make_early_inc_range(*BB))
3494 ChangedFunction |= processInstruction(&Inst);
3495 return ChangedFunction;
3496}
3497
3498// Instantiate an expression in a predecessor that lacked it.
3499bool GVNPass::performScalarPREInsertion(Instruction *Instr, BasicBlock *Pred,
3500 BasicBlock *Curr, unsigned int ValNo) {
3501 // Because we are going top-down through the block, all value numbers
3502 // will be available in the predecessor by the time we need them. Any
3503 // that weren't originally present will have been instantiated earlier
3504 // in this loop.
3505 bool Success = true;
3506 for (unsigned I = 0, E = Instr->getNumOperands(); I != E; ++I) {
3507 Value *Op = Instr->getOperand(I);
3509 continue;
3510 // This could be a newly inserted instruction, in which case, we won't
3511 // find a value number, and should give up before we hurt ourselves.
3512 // FIXME: Rewrite the infrastructure to let it easier to value number
3513 // and process newly inserted instructions.
3514 if (!VN.exists(Op)) {
3515 Success = false;
3516 break;
3517 }
3518 uint32_t TValNo =
3519 VN.phiTranslate(Pred, Curr, VN.lookup(Op), *this);
3520 if (Value *V = findLeader(Pred, TValNo)) {
3521 Instr->setOperand(I, V);
3522 } else {
3523 Success = false;
3524 break;
3525 }
3526 }
3527
3528 // Fail out if we encounter an operand that is not available in
3529 // the PRE predecessor. This is typically because of loads which
3530 // are not value numbered precisely.
3531 if (!Success)
3532 return false;
3533
3534 Instr->insertBefore(Pred->getTerminator()->getIterator());
3535 Instr->setName(Instr->getName() + ".pre");
3536 Instr->setDebugLoc(Instr->getDebugLoc());
3537
3538 ICF->insertInstructionTo(Instr, Pred);
3539
3540 unsigned Num = VN.lookupOrAdd(Instr);
3541 VN.add(Instr, Num);
3542
3543 // Update the availability map to include the new instruction.
3544 LeaderTable.insert(Num, Instr, Pred);
3545 return true;
3546}
3547
3548bool GVNPass::performScalarPRE(Instruction *CurInst) {
3549 if (isa<AllocaInst>(CurInst) || CurInst->isTerminator() ||
3550 isa<PHINode>(CurInst) || CurInst->getType()->isVoidTy() ||
3551 CurInst->mayReadFromMemory() || CurInst->mayHaveSideEffects() ||
3552 CurInst->getType()->isTokenLikeTy())
3553 return false;
3554
3555 // Don't do PRE on compares. The PHI would prevent CodeGenPrepare from
3556 // sinking the compare again, and it would force the code generator to
3557 // move the i1 from processor flags or predicate registers into a general
3558 // purpose register.
3559 if (isa<CmpInst>(CurInst))
3560 return false;
3561
3562 // Don't do PRE on GEPs. The inserted PHI would prevent CodeGenPrepare from
3563 // sinking the addressing mode computation back to its uses. Extending the
3564 // GEP's live range increases the register pressure, and therefore it can
3565 // introduce unnecessary spills.
3566 //
3567 // This doesn't prevent Load PRE. PHI translation will make the GEP available
3568 // to the load by moving it to the predecessor block if necessary.
3569 if (isa<GetElementPtrInst>(CurInst))
3570 return false;
3571
3572 if (auto *CallB = dyn_cast<CallBase>(CurInst)) {
3573 // We don't currently value number ANY inline asm calls.
3574 if (CallB->isInlineAsm())
3575 return false;
3576 }
3577
3578 uint32_t ValNo = VN.lookup(CurInst);
3579
3580 // Look for the predecessors for PRE opportunities. We're
3581 // only trying to solve the basic diamond case, where
3582 // a value is computed in the successor and one predecessor,
3583 // but not the other. We also explicitly disallow cases
3584 // where the successor is its own predecessor, because they're
3585 // more complicated to get right.
3586 unsigned NumWith = 0;
3587 unsigned NumWithout = 0;
3588 BasicBlock *PREPred = nullptr;
3589 BasicBlock *CurrentBlock = CurInst->getParent();
3590
3591 // Update the RPO numbers for this function.
3592 if (InvalidBlockRPONumbers)
3593 assignBlockRPONumber(*CurrentBlock->getParent());
3594
3596 for (BasicBlock *P : predecessors(CurrentBlock)) {
3597 // We're not interested in PRE where blocks with predecessors that are
3598 // not reachable.
3599 if (!DT->isReachableFromEntry(P)) {
3600 NumWithout = 2;
3601 break;
3602 }
3603 // It is not safe to do PRE when P->CurrentBlock is a loop backedge.
3604 assert(BlockRPONumber.count(P) && BlockRPONumber.count(CurrentBlock) &&
3605 "Invalid BlockRPONumber map.");
3606 if (BlockRPONumber[P] >= BlockRPONumber[CurrentBlock]) {
3607 NumWithout = 2;
3608 break;
3609 }
3610
3611 uint32_t TValNo = VN.phiTranslate(P, CurrentBlock, ValNo, *this);
3612 Value *PredV = findLeader(P, TValNo);
3613 if (!PredV) {
3614 PredMap.push_back(std::make_pair(static_cast<Value *>(nullptr), P));
3615 PREPred = P;
3616 ++NumWithout;
3617 } else if (PredV == CurInst) {
3618 // CurInst dominates this predecessor.
3619 NumWithout = 2;
3620 break;
3621 } else {
3622 PredMap.push_back(std::make_pair(PredV, P));
3623 ++NumWith;
3624 }
3625 }
3626
3627 // Don't do PRE when it might increase code size, i.e. when
3628 // we would need to insert instructions in more than one pred.
3629 if (NumWithout > 1 || NumWith == 0)
3630 return false;
3631
3632 // We may have a case where all predecessors have the instruction,
3633 // and we just need to insert a phi node. Otherwise, perform
3634 // insertion.
3635 Instruction *PREInstr = nullptr;
3636
3637 if (NumWithout != 0) {
3638 if (!isSafeToSpeculativelyExecute(CurInst)) {
3639 // It is only valid to insert a new instruction if the current instruction
3640 // is always executed. An instruction with implicit control flow could
3641 // prevent us from doing it. If we cannot speculate the execution, then
3642 // PRE should be prohibited.
3643 if (ICF->isDominatedByICFIFromSameBlock(CurInst))
3644 return false;
3645 }
3646
3647 // Don't do PRE across indirect branch.
3648 if (isa<IndirectBrInst>(PREPred->getTerminator()))
3649 return false;
3650
3651 // We can't do PRE safely on a critical edge, so instead we schedule
3652 // the edge to be split and perform the PRE the next time we iterate
3653 // on the function.
3654 unsigned SuccNum = GetSuccessorNumber(PREPred, CurrentBlock);
3655 if (isCriticalEdge(PREPred->getTerminator(), SuccNum)) {
3656 ToSplit.push_back(std::make_pair(PREPred->getTerminator(), SuccNum));
3657 return false;
3658 }
3659 // We need to insert somewhere, so let's give it a shot.
3660 PREInstr = CurInst->clone();
3661 if (!performScalarPREInsertion(PREInstr, PREPred, CurrentBlock, ValNo)) {
3662 // If we failed insertion, make sure we remove the instruction.
3663#ifndef NDEBUG
3664 verifyRemoved(PREInstr);
3665#endif
3666 PREInstr->deleteValue();
3667 return false;
3668 }
3669 }
3670
3671 // Either we should have filled in the PRE instruction, or we should
3672 // not have needed insertions.
3673 assert(PREInstr != nullptr || NumWithout == 0);
3674
3675 ++NumGVNPRE;
3676
3677 // Create a PHI to make the value available in this block.
3678 PHINode *Phi = PHINode::Create(CurInst->getType(), PredMap.size(),
3679 CurInst->getName() + ".pre-phi");
3680 Phi->insertBefore(CurrentBlock->begin());
3681 for (auto &[V, BB] : PredMap) {
3682 if (V) {
3683 // If we use an existing value in this phi, we have to patch the original
3684 // value because the phi will be used to replace a later value.
3685 patchReplacementInstruction(CurInst, V);
3686 Phi->addIncoming(V, BB);
3687 } else
3688 Phi->addIncoming(PREInstr, PREPred);
3689 }
3690
3691 VN.add(Phi, ValNo);
3692 // After creating a new PHI for ValNo, the phi translate result for ValNo will
3693 // be changed, so erase the related stale entries in phi translate cache.
3694 VN.eraseTranslateCacheEntry(ValNo, *CurrentBlock);
3695 LeaderTable.insert(ValNo, Phi, CurrentBlock);
3696 Phi->setDebugLoc(CurInst->getDebugLoc());
3697 CurInst->replaceAllUsesWith(Phi);
3698 if (MD && Phi->getType()->isPtrOrPtrVectorTy())
3699 MD->invalidateCachedPointerInfo(Phi);
3700 LeaderTable.erase(ValNo, CurInst, CurrentBlock);
3701
3702 LLVM_DEBUG(dbgs() << "GVN PRE removed: " << *CurInst << '\n');
3703 removeInstruction(CurInst);
3704
3705 return true;
3706}
3707
3708/// Perform a purely local form of PRE that looks for diamond
3709/// control flow patterns and attempts to perform simple PRE at the join point.
3710bool GVNPass::performPRE(Function &F) {
3711 bool Changed = false;
3712 for (BasicBlock *CurrentBlock : depth_first(&F.getEntryBlock())) {
3713 // Nothing to PRE in the entry block.
3714 if (CurrentBlock == &F.getEntryBlock())
3715 continue;
3716
3717 // Don't perform PRE on an EH pad.
3718 if (CurrentBlock->isEHPad())
3719 continue;
3720
3721 for (BasicBlock::iterator BI = CurrentBlock->begin(),
3722 BE = CurrentBlock->end();
3723 BI != BE;) {
3724 Instruction *CurInst = &*BI++;
3725 Changed |= performScalarPRE(CurInst);
3726 }
3727 }
3728
3729 if (splitCriticalEdges())
3730 Changed = true;
3731
3732 return Changed;
3733}
3734
3735/// Split the critical edge connecting the given two blocks, and return
3736/// the block inserted to the critical edge.
3737BasicBlock *GVNPass::splitCriticalEdges(BasicBlock *Pred, BasicBlock *Succ) {
3738 // GVN does not require loop-simplify, do not try to preserve it if it is not
3739 // possible.
3741 Pred, Succ,
3742 CriticalEdgeSplittingOptions(DT, LI, MSSAU).unsetPreserveLoopSimplify());
3743 if (BB) {
3744 if (MD)
3745 MD->invalidateCachedPredecessors();
3746 InvalidBlockRPONumbers = true;
3747 }
3748 return BB;
3749}
3750
3751/// Split critical edges found during the previous
3752/// iteration that may enable further optimization.
3753bool GVNPass::splitCriticalEdges() {
3754 if (ToSplit.empty())
3755 return false;
3756
3757 bool Changed = false;
3758 do {
3759 std::pair<Instruction *, unsigned> Edge = ToSplit.pop_back_val();
3760 Changed |= SplitCriticalEdge(Edge.first, Edge.second,
3761 CriticalEdgeSplittingOptions(DT, LI, MSSAU)) !=
3762 nullptr;
3763 } while (!ToSplit.empty());
3764 if (Changed) {
3765 if (MD)
3766 MD->invalidateCachedPredecessors();
3767 InvalidBlockRPONumbers = true;
3768 }
3769 return Changed;
3770}
3771
3772/// Executes one iteration of GVN.
3773bool GVNPass::iterateOnFunction(Function &F) {
3774 cleanupGlobalSets();
3775
3776 // Top-down walk of the dominator tree.
3777 bool Changed = false;
3778 // Needed for value numbering with phi construction to work.
3779 // RPOT walks the graph in its constructor and will not be invalidated during
3780 // processBlock.
3781 ReversePostOrderTraversal<Function *> RPOT(&F);
3782
3783 for (BasicBlock *BB : RPOT)
3784 Changed |= processBlock(BB);
3785
3786 return Changed;
3787}
3788
3789void GVNPass::cleanupGlobalSets() {
3790 VN.clear();
3791 LeaderTable.clear();
3792 BlockRPONumber.clear();
3793 ICF->clear();
3794 InvalidBlockRPONumbers = true;
3795}
3796
3797void GVNPass::removeInstruction(Instruction *I) {
3798 VN.erase(I);
3799 if (MD) MD->removeInstruction(I);
3800 if (MSSAU)
3801 MSSAU->removeMemoryAccess(I);
3802#ifndef NDEBUG
3803 verifyRemoved(I);
3804#endif
3805 ICF->removeInstruction(I);
3806 I->eraseFromParent();
3807 ++NumGVNInstr;
3808}
3809
3810/// Verify that the specified instruction does not occur in our
3811/// internal data structures.
3812void GVNPass::verifyRemoved(const Instruction *Inst) const {
3813 VN.verifyRemoved(Inst);
3814}
3815
3816/// BB is declared dead, which implied other blocks become dead as well. This
3817/// function is to add all these blocks to "DeadBlocks". For the dead blocks'
3818/// live successors, update their phi nodes by replacing the operands
3819/// corresponding to dead blocks with UndefVal.
3820void GVNPass::addDeadBlock(BasicBlock *BB) {
3822 SmallSetVector<BasicBlock *, 4> DF;
3823
3824 NewDead.push_back(BB);
3825 while (!NewDead.empty()) {
3826 BasicBlock *D = NewDead.pop_back_val();
3827 if (DeadBlocks.count(D))
3828 continue;
3829
3830 // All blocks dominated by D are dead.
3831 SmallVector<BasicBlock *, 8> Dom;
3832 DT->getDescendants(D, Dom);
3833 DeadBlocks.insert_range(Dom);
3834
3835 // Figure out the dominance-frontier(D).
3836 for (BasicBlock *B : Dom) {
3837 for (BasicBlock *S : successors(B)) {
3838 if (DeadBlocks.count(S))
3839 continue;
3840
3841 bool AllPredDead = true;
3842 for (BasicBlock *P : predecessors(S))
3843 if (!DeadBlocks.count(P)) {
3844 AllPredDead = false;
3845 break;
3846 }
3847
3848 if (!AllPredDead) {
3849 // S could be proved dead later on. That is why we don't update phi
3850 // operands at this moment.
3851 DF.insert(S);
3852 } else {
3853 // While S is not dominated by D, it is dead by now. This could take
3854 // place if S already have a dead predecessor before D is declared
3855 // dead.
3856 NewDead.push_back(S);
3857 }
3858 }
3859 }
3860 }
3861
3862 // For the dead blocks' live successors, update their phi nodes by replacing
3863 // the operands corresponding to dead blocks with UndefVal.
3864 for (BasicBlock *B : DF) {
3865 if (DeadBlocks.count(B))
3866 continue;
3867
3868 // First, split the critical edges. This might also create additional blocks
3869 // to preserve LoopSimplify form and adjust edges accordingly.
3871 for (BasicBlock *P : Preds) {
3872 if (!DeadBlocks.count(P))
3873 continue;
3874
3875 if (is_contained(successors(P), B) &&
3876 isCriticalEdge(P->getTerminator(), B)) {
3877 if (BasicBlock *S = splitCriticalEdges(P, B))
3878 DeadBlocks.insert(P = S);
3879 }
3880 }
3881
3882 // Now poison the incoming values from the dead predecessors.
3883 for (BasicBlock *P : predecessors(B)) {
3884 if (!DeadBlocks.count(P))
3885 continue;
3886 for (PHINode &Phi : B->phis()) {
3887 Phi.setIncomingValueForBlock(P, PoisonValue::get(Phi.getType()));
3888 if (MD)
3889 MD->invalidateCachedPointerInfo(&Phi);
3890 }
3891 }
3892 }
3893}
3894
3895// If the given branch is recognized as a foldable branch (i.e. conditional
3896// branch with constant condition), it will perform following analyses and
3897// transformation.
3898// 1) If the dead out-coming edge is a critical-edge, split it. Let
3899// R be the target of the dead out-coming edge.
3900// 1) Identify the set of dead blocks implied by the branch's dead outcoming
3901// edge. The result of this step will be {X| X is dominated by R}
3902// 2) Identify those blocks which haves at least one dead predecessor. The
3903// result of this step will be dominance-frontier(R).
3904// 3) Update the PHIs in DF(R) by replacing the operands corresponding to
3905// dead blocks with "UndefVal" in an hope these PHIs will optimized away.
3906//
3907// Return true iff *NEW* dead code are found.
3908bool GVNPass::processFoldableCondBr(CondBrInst *BI) {
3909 // If a branch has two identical successors, we cannot declare either dead.
3910 if (BI->getSuccessor(0) == BI->getSuccessor(1))
3911 return false;
3912
3913 ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
3914 if (!Cond)
3915 return false;
3916
3917 BasicBlock *DeadRoot =
3918 Cond->getZExtValue() ? BI->getSuccessor(1) : BI->getSuccessor(0);
3919 if (DeadBlocks.count(DeadRoot))
3920 return false;
3921
3922 if (!DeadRoot->getSinglePredecessor())
3923 DeadRoot = splitCriticalEdges(BI->getParent(), DeadRoot);
3924
3925 addDeadBlock(DeadRoot);
3926 return true;
3927}
3928
3929// performPRE() will trigger assert if it comes across an instruction without
3930// associated val-num. As it normally has far more live instructions than dead
3931// instructions, it makes more sense just to "fabricate" a val-number for the
3932// dead code than checking if instruction involved is dead or not.
3933void GVNPass::assignValNumForDeadCode() {
3934 for (BasicBlock *BB : DeadBlocks) {
3935 for (Instruction &Inst : *BB) {
3936 unsigned ValNum = VN.lookupOrAdd(&Inst);
3937 LeaderTable.insert(ValNum, &Inst, BB);
3938 }
3939 }
3940}
3941
3943public:
3944 static char ID; // Pass identification, replacement for typeid.
3945
3946 explicit GVNLegacyPass(bool MemDepAnalysis = GVNEnableMemDep,
3947 bool MemSSAAnalysis = GVNEnableMemorySSA,
3948 bool ScalarPRE = true)
3949 : FunctionPass(ID), Impl(GVNOptions()
3950 .setMemDep(MemDepAnalysis)
3951 .setMemorySSA(MemSSAAnalysis)
3952 .setScalarPRE(ScalarPRE)) {
3954 }
3955
3956 bool runOnFunction(Function &F) override {
3957 if (skipFunction(F))
3958 return false;
3959
3961 if (Impl.isMemorySSAEnabled() && !MSSAWP)
3963
3964 return Impl.runImpl(
3965 F, getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F),
3968 getAnalysis<AAResultsWrapperPass>().getAAResults(),
3969 Impl.isMemDepEnabled()
3971 : nullptr,
3972 getAnalysis<LoopInfoWrapperPass>().getLoopInfo(),
3974 MSSAWP ? &MSSAWP->getMSSA() : nullptr);
3975 }
3976
3994
3995private:
3996 GVNPass Impl;
3997};
3998
3999char GVNLegacyPass::ID = 0;
4000
4001INITIALIZE_PASS_BEGIN(GVNLegacyPass, "gvn", "Global Value Numbering", false, false)
4010INITIALIZE_PASS_END(GVNLegacyPass, "gvn", "Global Value Numbering", false, false)
4011
4012// The public interface to this file...
4015 return new GVNLegacyPass(GVNEnableMemDep, GVNEnableMemorySSA, ScalarPRE);
4016}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
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:661
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
This file defines the DenseMap class.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
early cse Early CSE w MemorySSA
static void reportMayClobberedLoad(LoadInst *Load, Instruction *DepInst, const DominatorTree *DT, OptimizationRemarkEmitter *ORE)
Try to locate the three instruction involved in a missed load-elimination case that is due to an inte...
Definition GVN.cpp:1281
static Instruction * findInvariantGroupValue(LoadInst *L, DominatorTree &DT)
If a load has !invariant.group, try to find the most-dominating instruction with the same metadata an...
Definition GVN.cpp:2202
static void reportLoadElim(LoadInst *Load, Value *AvailableValue, OptimizationRemarkEmitter *ORE)
Definition GVN.cpp:2008
static cl::opt< uint32_t > MaxNumInsnsPerBlock("gvn-max-num-insns", cl::Hidden, cl::init(100), cl::desc("Max number of instructions to scan in each basic block in GVN " "(default = 100)"))
static cl::opt< bool > GVNEnableMemDep("enable-gvn-memdep", cl::init(true))
static cl::opt< bool > GVNEnableLoadInLoopPRE("enable-load-in-loop-pre", cl::init(true))
static const Instruction * findMayClobberedPtrAccess(LoadInst *Load, const DominatorTree *DT)
Definition GVN.cpp:1225
static cl::opt< uint32_t > MaxNumDeps("gvn-max-num-deps", cl::Hidden, cl::init(100), cl::desc("Max number of dependences to attempt Load PRE (default = 100)"))
static std::optional< MemoryLocation > maybeLoadStoreLocation(Instruction *I, bool AllowStores, const TargetLibraryInfo *TLI)
Return the memory location accessed by the (masked) load/store instruction I, if the instruction coul...
Definition GVN.cpp:2254
static cl::opt< bool > GVNEnableMemorySSA("enable-gvn-memoryssa", cl::init(false))
static bool isOnlyReachableViaThisEdge(const BasicBlockEdge &E, DominatorTree *DT)
There is an edge from 'Src' to 'Dst'.
Definition GVN.cpp:3033
static bool IsValueFullyAvailableInBlock(BasicBlock *BB, DenseMap< BasicBlock *, AvailabilityState > &FullyAvailableBlocks)
Return true if we can prove that the value we're analyzing is fully available in the specified block.
Definition GVN.cpp:965
static cl::opt< bool > GVNEnableScalarPRE("enable-scalar-pre", cl::init(true), cl::Hidden)
static Value * findDominatingValue(const MemoryLocation &Loc, Type *LoadTy, Instruction *From, AAResults *AA)
Definition GVN.cpp:1302
static bool liesBetween(const Instruction *From, Instruction *Between, const Instruction *To, const DominatorTree *DT)
Assuming To can be reached from both From and Between, does Between lie on every path from From to To...
Definition GVN.cpp:1216
static bool isLifetimeStart(const Instruction *Inst)
Definition GVN.cpp:1208
static cl::opt< bool > GVNEnableSplitBackedgeInLoadPRE("enable-split-backedge-in-load-pre", cl::init(false))
static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl)
Definition GVN.cpp:2194
static void replaceValuesPerBlockEntry(SmallVectorImpl< AvailableValueInBlock > &ValuesPerBlock, Value *OldValue, Value *NewValue)
If the specified OldValue exists in ValuesPerBlock, replace its value with NewValue.
Definition GVN.cpp:1081
static cl::opt< unsigned > ScanUsersLimit("gvn-scan-users-limit", cl::Hidden, cl::init(100), cl::desc("The number of memory accesses to scan in a block in reaching " "memory values analysis (default = 100)"))
static Value * ConstructSSAForLoadSet(LoadInst *Load, SmallVectorImpl< AvailableValueInBlock > &ValuesPerBlock, GVNPass &GVN)
Given a set of loads specified by ValuesPerBlock, construct SSA form, allowing us to eliminate Load.
Definition GVN.cpp:1100
AvailabilityState
Definition GVN.cpp:945
@ Unavailable
We know the block is not fully available. This is a fixpoint.
Definition GVN.cpp:947
@ Available
We know the block is fully available. This is a fixpoint.
Definition GVN.cpp:949
@ SpeculativelyAvailable
We do not know whether the block is fully available or not, but we are currently speculating that it ...
Definition GVN.cpp:954
static cl::opt< uint32_t > MaxNumVisitedInsts("gvn-max-num-visited-insts", cl::Hidden, cl::init(100), cl::desc("Max number of visited instructions when trying to find " "dominating value of select dependency (default = 100)"))
static cl::opt< uint32_t > MaxBBSpeculations("gvn-max-block-speculations", cl::Hidden, cl::init(600), cl::desc("Max number of blocks we're willing to speculate on (and recurse " "into) when deducing if a value is fully available or not in GVN " "(default = 600)"))
static cl::opt< bool > GVNEnableLoadPRE("enable-load-pre", cl::init(true))
This file provides the interface for LLVM's Global Value Numbering pass which eliminates fully redund...
#define DEBUG_TYPE
This is the interface for a simple mod/ref and alias analysis over globals.
Hexagon Common GEP
#define _
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
This defines the Use class.
static bool lookup(const GsymReader &GR, GsymDataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
This file implements a map that provides insertion order iteration.
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
This file contains the declarations for metadata subclasses.
uint64_t IntrinsicInst * II
#define P(N)
ppc ctr loops PowerPC CTR Loops Verify
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
const SmallVectorImpl< MachineOperand > & Cond
static DominatorTree getDomTree(Function &F)
std::pair< BasicBlock *, BasicBlock * > Edge
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
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:119
Value * RHS
Value * LHS
static const uint32_t IV[8]
Definition blake3_impl.h:83
A manager for alias analyses.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
@ MayAlias
The two locations may or may not alias.
@ NoAlias
The two locations do not alias at all.
@ PartialAlias
The two locations alias, but only due to a partial overlap.
@ MustAlias
The two locations precisely alias each other.
constexpr int32_t getOffset() const
constexpr bool hasOffset() const
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
iterator end() const
Definition ArrayRef.h:130
iterator begin() const
Definition ArrayRef.h:129
A function analysis which provides an AssumptionCache.
An immutable pass that tracks lazily created AssumptionCache objects.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
bool isEHPad() const
Return true if this basic block is an exception handling block.
Definition BasicBlock.h:704
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
ModRefInfo getModRefInfo(const Instruction *I, const std::optional< MemoryLocation > &OptLoc)
LLVM_ABI Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
Value * getArgOperand(unsigned i) const
unsigned arg_size() const
This class represents a function call, abstracting a target machine's calling convention.
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:890
Value * getCondition() const
BasicBlock * getSuccessor(unsigned i) const
bool isMinusOne() const
This function will return true iff every bit in this constant is set to true.
Definition Constants.h:231
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
Definition DenseMap.h:252
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:225
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:301
iterator end()
Definition DenseMap.h:143
Analysis pass which computes a DominatorTree.
Definition Dominators.h:274
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
Legacy analysis pass which computes a DominatorTree.
Definition Dominators.h:310
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:155
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Class representing an expression and its matching format.
unsigned getNumIndices() const
iterator_range< idx_iterator > indices() const
idx_iterator idx_begin() const
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
FunctionPass(char &pid)
Definition Pass.h:316
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition Pass.cpp:188
const BasicBlock & getEntryBlock() const
Definition Function.h:809
Represents calls to the gc.relocate intrinsic.
This class holds the mapping between values and value numbers.
Definition GVN.h:164
LLVM_ABI uint32_t lookupOrAdd(MemoryAccess *MA)
Definition GVN.cpp:647
The core GVN pass object.
Definition GVN.h:131
friend class gvn::GVNLegacyPass
Definition GVN.h:250
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Run the pass over the function.
Definition GVN.cpp:876
LLVM_ABI void salvageAndRemoveInstruction(Instruction *I)
This removes the specified instruction from our various maps and marks it for deletion.
Definition GVN.cpp:928
AAResults * getAliasAnalysis() const
Definition GVN.h:151
LLVM_ABI bool isLoadPREEnabled() const
Definition GVN.cpp:855
GVNPass(GVNOptions Options={})
Definition GVN.h:137
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Definition GVN.cpp:908
LLVM_ABI bool isMemorySSAEnabled() const
Definition GVN.cpp:872
DominatorTree & getDominatorTree() const
Definition GVN.h:150
LLVM_ABI bool isLoadInLoopPREEnabled() const
Definition GVN.cpp:859
LLVM_ABI bool isScalarPREEnabled() const
Definition GVN.cpp:851
LLVM_ABI bool isLoadPRESplitBackedgeEnabled() const
Definition GVN.cpp:863
LLVM_ABI bool isMemDepEnabled() const
Definition GVN.cpp:868
Legacy wrapper pass to provide the GlobalsAAResult object.
LLVM_ABI Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
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.
bool hasMetadata() const
Return true if this instruction has any metadata attached to it.
LLVM_ABI bool isAtomic() const LLVM_READONLY
Return true if this instruction has an AtomicOrdering of unordered or higher.
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
LLVM_ABI bool mayHaveSideEffects() const LLVM_READONLY
Return true if the instruction may have side effects.
bool isTerminator() const
LLVM_ABI bool mayReadFromMemory() const LLVM_READONLY
Return true if this instruction may read memory.
LLVM_ABI void dropUnknownNonDebugMetadata(ArrayRef< unsigned > KnownIDs={})
Drop all unknown metadata except for debug locations.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
A wrapper class for inspecting calls to intrinsic functions.
An instruction for reading from memory.
Analysis pass that exposes the LoopInfo for a function.
Definition LoopInfo.h:587
The legacy pass manager's analysis pass to compute loop information.
Definition LoopInfo.h:612
iterator find(const KeyT &Key)
Definition MapVector.h:156
iterator end()
Definition MapVector.h:69
size_type size() const
Definition MapVector.h:58
A memory dependence query can return one of three different answers.
bool isClobber() const
Tests if this MemDepResult represents a query that is an instruction clobber dependency.
bool isNonLocal() const
Tests if this MemDepResult represents a query that is transparent to the start of the block,...
bool isDef() const
Tests if this MemDepResult represents a query that is an instruction definition dependency.
bool isLocal() const
Tests if this MemDepResult represents a valid local query (Clobber/Def).
Instruction * getInst() const
If this is a normal dependency, returns the instruction that is depended on.
This is the common base class for memset/memcpy/memmove.
BasicBlock * getBlock() const
Definition MemorySSA.h:162
An analysis that produces MemoryDependenceResults for a function.
std::vector< NonLocalDepEntry > NonLocalDepInfo
LLVM_ABI MemDepResult getDependency(Instruction *QueryInst)
Returns the instruction on which a memory operation depends.
LLVM_ABI const NonLocalDepInfo & getNonLocalCallDependency(CallBase *QueryCall)
Perform a full dependency query for the specified call, returning the set of blocks that the value is...
A wrapper analysis pass for the legacy pass manager that exposes a MemoryDepnedenceResults instance.
Representation for a specific memory location.
static LLVM_ABI MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
MemoryLocation getWithNewPtr(const Value *NewPtr) const
const Value * Ptr
The address of the start of the location.
static LLVM_ABI MemoryLocation getForArgument(const CallBase *Call, unsigned ArgIdx, const TargetLibraryInfo *TLI)
Return a location representing a particular argument of a call.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
Definition MemorySSA.h:529
BasicBlock * getIncomingBlock(unsigned I) const
Return incoming basic block number i.
Definition MemorySSA.h:542
MemoryAccess * getIncomingValue(unsigned I) const
Return incoming value number x.
Definition MemorySSA.h:532
An analysis that produces MemorySSA for a function.
Definition MemorySSA.h:922
Legacy analysis pass which computes MemorySSA.
Definition MemorySSA.h:975
LLVM_ABI void verifyMemorySSA(VerificationLevel=VerificationLevel::Fast) const
Verify that MemorySSA is self consistent (IE definitions dominate all uses, uses appear in the right ...
MemoryUseOrDef * getMemoryAccess(const Instruction *I) const
Given a memory Mod/Ref'ing instruction, get the MemorySSA access associated with it.
Definition MemorySSA.h:720
LLVM_ABI bool locallyDominates(const MemoryAccess *A, const MemoryAccess *B) const
Given two memory accesses in the same basic block, determine whether MemoryAccess A dominates MemoryA...
bool isLiveOnEntryDef(const MemoryAccess *MA) const
Return true if MA represents the live on entry value.
Definition MemorySSA.h:740
MemoryAccess * getDefiningAccess() const
Get the access that produces the memory state used by this Use.
Definition MemorySSA.h:260
This is an entry in the NonLocalDepInfo cache.
OptimizationRemarkEmitter legacy analysis pass.
The optimization diagnostic interface.
LLVM_ABI void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
Diagnostic information for missed-optimization remarks.
Diagnostic information for applied optimization remarks.
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...
LLVM_ABI Value * translateValue(BasicBlock *CurBB, BasicBlock *PredBB, const DominatorTree *DT, bool MustDominate)
translateValue - PHI translate the current address up the CFG from CurBB to Pred, updating our state ...
LLVM_ABI bool isPotentiallyPHITranslatable() const
isPotentiallyPHITranslatable - If this needs PHI translation, return true if we have some hope of doi...
bool needsPHITranslationFromBlock(BasicBlock *BB) const
needsPHITranslationFromBlock - Return true if moving from the specified BasicBlock to its predecessor...
Value * getAddr() const
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
AnalysisType * getAnalysisIfAvailable() const
getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to get analysis information tha...
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
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 all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
Helper class for SSA formation on a set of values defined in multiple blocks.
Definition SSAUpdater.h:39
LLVM_ABI void Initialize(Type *Ty, StringRef Name)
Reset this object to get ready for a new set of SSA updates with type 'Ty'.
LLVM_ABI Value * GetValueInMiddleOfBlock(BasicBlock *BB)
Construct SSA form, materializing a value that is live in the middle of the specified block.
LLVM_ABI bool HasValueForBlock(BasicBlock *BB) const
Return true if the SSAUpdater already has a value for the specified block.
LLVM_ABI void AddAvailableValue(BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value.
This class represents the LLVM 'select' instruction.
const Value * getCondition() const
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", InsertPosition InsertBefore=nullptr, const Instruction *MDFrom=nullptr)
bool erase(PtrType Ptr)
Remove pointer from the set.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
iterator erase(const_iterator CI)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
iterator insert(iterator I, T &&Elt)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
SmallVector & operator=(const SmallVector &RHS)
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM_ABI bool isTokenLikeTy() const
Returns true if this is 'token' or a token-like target type.s.
Definition Type.cpp:1140
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:307
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition Type.h:285
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:257
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:141
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
op_range operands()
Definition User.h:267
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:552
iterator_range< user_iterator > users()
Definition Value.h:426
bool hasUseList() const
Check if this Value has a use-list.
Definition Value.h:344
LLVM_ABI bool canBeFreed() const
Return true if the memory object referred to by V can by freed in the scope for which the SSA value d...
Definition Value.cpp:827
LLVM_ABI void deleteValue()
Delete a pointer to a generic Value.
Definition Value.cpp:107
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:318
An efficient, type-erasing, non-owning reference to a callable.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition GVN.cpp:3977
GVNLegacyPass(bool MemDepAnalysis=GVNEnableMemDep, bool MemSSAAnalysis=GVNEnableMemorySSA, bool ScalarPRE=true)
Definition GVN.cpp:3946
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Definition GVN.cpp:3956
An opaque object representing a hash code.
Definition Hashing.h:78
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
CallInst * Call
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract Attribute helper functions.
Definition Attributor.h:165
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ Entry
Definition COFF.h:862
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedStore Intrinsic.
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
auto m_Value()
Match an arbitrary value and ignore it.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
NoWrapTrunc_match< OpTy, TruncInst::NoUnsignedWrap > m_NUWTrunc(const OpTy &Op)
Matches trunc nuw.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
LLVM_ABI int analyzeLoadFromClobberingStore(Type *LoadTy, Value *LoadPtr, StoreInst *DepSI, const DataLayout &DL)
This function determines whether a value for the pointer LoadPtr can be extracted from the store at D...
LLVM_ABI Value * getMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset, Type *LoadTy, Instruction *InsertPt, const DataLayout &DL)
If analyzeLoadFromClobberingMemInst returned an offset, this function can be used to actually perform...
LLVM_ABI int analyzeLoadFromClobberingLoad(Type *LoadTy, Value *LoadPtr, LoadInst *DepLI, const DataLayout &DL)
This function determines whether a value for the pointer LoadPtr can be extracted from the load at De...
LLVM_ABI Value * getValueForLoad(Value *SrcVal, unsigned Offset, Type *LoadTy, Instruction *InsertPt, Function *F)
If analyzeLoadFromClobberingStore/Load returned an offset, this function can be used to actually perf...
LLVM_ABI int analyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr, MemIntrinsic *DepMI, const DataLayout &DL)
This function determines whether a value for the pointer LoadPtr can be extracted from the memory int...
LLVM_ABI bool canCoerceMustAliasedValueToLoad(Value *StoredVal, Type *LoadTy, Function *F)
Return true if CoerceAvailableValueToLoadType would succeed if it was called.
initializer< Ty > init(const Ty &Val)
A private "module" namespace for types and utilities used by GVN.
Definition GVN.h:66
Add a small namespace to avoid name clashes with the classes used in the streaming interface.
NodeAddr< InstrNode * > Instr
Definition RDFGraph.h:389
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:390
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:558
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
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:1738
hash_code hash_value(const FixedPointSemantics &Val)
LLVM_ABI Constant * getInitialValueOfAllocation(const Value *V, const TargetLibraryInfo *TLI, Type *Ty)
If this is a call to an allocation function that initializes memory to a fixed value,...
LLVM_ABI unsigned replaceDominatedUsesWithIf(Value *From, Value *To, DominatorTree &DT, const BasicBlockEdge &Edge, function_ref< bool(const Use &U, const Value *To)> ShouldReplace)
Replace each use of 'From' with 'To' if that use is dominated by the given edge and the callback Shou...
Definition Local.cpp:3292
LLVM_ABI unsigned GetSuccessorNumber(const BasicBlock *BB, const BasicBlock *Succ)
Search for the specified successor of basic block BB and return its position in the terminator instru...
Definition CFG.cpp:90
auto pred_end(const MachineBasicBlock *BB)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI FunctionPass * createGVNPass(bool ScalarPRE)
Create a legacy GVN pass.
Definition GVN.cpp:4014
LLVM_ABI void salvageDebugInfo(const MachineRegisterInfo &MRI, MachineInstr &MI)
Assuming the instruction MI is going to be deleted, attempt to salvage debug users of MI by writing t...
Definition Utils.cpp:1687
auto successors(const MachineBasicBlock *BB)
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
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:2207
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:633
LLVM_ABI bool isNoAliasCall(const Value *V)
Return true if this pointer is returned by a noalias function.
LLVM_ABI bool isAssumeWithEmptyBundle(const AssumeInst &Assume)
Return true iff the operand bundles of the provided llvm.assume doesn't contain any valuable informat...
LLVM_ABI bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM_ABI Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
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:1745
LLVM_ABI bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition Local.cpp:403
LLVM_ABI bool canReplacePointersInUseIfEqual(const Use &U, const Value *To, const DataLayout &DL)
Definition Loads.cpp:883
LLVM_ABI bool canReplacePointersIfEqual(const Value *From, const Value *To, const DataLayout &DL)
Returns true if a pointer value From can be replaced with another pointer value \To if they are deeme...
Definition Loads.cpp:903
bool isModSet(const ModRefInfo MRI)
Definition ModRef.h:49
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void patchReplacementInstruction(Instruction *I, Value *Repl)
Patch the replacement so that it is not more restrictive than the value being replaced.
Definition Local.cpp:3192
LLVM_ABI void initializeGVNLegacyPassPass(PassRegistry &)
LLVM_ABI unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT, const BasicBlockEdge &Edge)
Replace each use of 'From' with 'To' if that use is dominated by the given edge.
Definition Local.cpp:3271
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:547
@ Success
The lock was released successfully.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
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:3120
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition ModRef.h:28
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
@ NoModRef
The access neither references nor modifies the value stored in memory.
Definition ModRef.h:30
LLVM_ABI bool VerifyMemorySSA
Enables verification of MemorySSA.
Definition MemorySSA.cpp:84
RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)
LLVM_ABI bool salvageKnowledge(Instruction *I, AssumptionCache *AC=nullptr, DominatorTree *DT=nullptr)
Calls BuildAssumeFromInst and if the resulting llvm.assume is valid insert if before I.
LLVM_ABI bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, MemoryDependenceResults *MemDep=nullptr, bool PredecessorWithTwoSuccessors=false, DominatorTree *DT=nullptr)
Attempts to merge a block into its predecessor, if possible.
LLVM_ABI FunctionPass * createGVNPass()
Definition GVN.cpp:4013
LLVM_ABI bool isPotentiallyReachable(const Instruction *From, const Instruction *To, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet=nullptr, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr, const CycleInfo *CI=nullptr)
Determine whether instruction 'To' is reachable from 'From', without passing through any blocks in Ex...
Definition CFG.cpp:335
FunctionAddr VTableAddr Next
Definition InstrProf.h:141
DWARFExpression::Operation Op
LLVM_ABI BasicBlock * SplitCriticalEdge(Instruction *TI, unsigned SuccNum, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions(), const Twine &BBName="")
If this edge is a critical edge, insert a new node to split the critical edge.
LLVM_ABI bool isCriticalEdge(const Instruction *TI, unsigned SuccNum, bool AllowIdenticalEdges=false)
Return true if the specified edge is a critical edge.
Definition CFG.cpp:106
constexpr unsigned BitWidth
auto pred_begin(const MachineBasicBlock *BB)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1946
bool pred_empty(const BasicBlock *BB)
Definition CFG.h:107
iterator_range< df_iterator< T > > depth_first(const T &G)
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:325
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool EliminateDuplicatePHINodes(BasicBlock *BB)
Check for and eliminate duplicate PHI nodes in this block.
Definition Local.cpp:1518
bool isStrongerThan(AtomicOrdering AO, AtomicOrdering Other)
Returns true if ao is stronger than other as defined by the AtomicOrdering lattice,...
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition Hashing.h:305
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:863
#define N
static bool isEqual(const GVNPass::Expression &LHS, const GVNPass::Expression &RHS)
Definition GVN.cpp:188
static GVNPass::Expression getEmptyKey()
Definition GVN.cpp:180
static unsigned getHashValue(const GVNPass::Expression &E)
Definition GVN.cpp:182
An information struct used to provide DenseMap with the various necessary components for a given valu...
A set of parameters to control various transforms performed by GVN pass.
Definition GVN.h:81
bool operator==(const Expression &Other) const
Definition GVN.cpp:158
friend hash_code hash_value(const Expression &Value)
Definition GVN.cpp:173
SmallVector< uint32_t, 4 > VarArgs
Definition GVN.cpp:152
AttributeList Attrs
Definition GVN.cpp:154
Expression(uint32_t Op=~2U)
Definition GVN.cpp:156
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition PassManager.h:89
Represents an AvailableValue which can be rematerialized at the end of the associated BasicBlock.
Definition GVN.cpp:294
static AvailableValueInBlock get(BasicBlock *BB, Value *V, unsigned Offset=0)
Definition GVN.cpp:308
static AvailableValueInBlock getUndef(BasicBlock *BB)
Definition GVN.cpp:313
static AvailableValueInBlock get(BasicBlock *BB, AvailableValue &&AV)
Definition GVN.cpp:301
AvailableValue AV
AV - The actual available value.
Definition GVN.cpp:299
static AvailableValueInBlock getSelect(BasicBlock *BB, SelectInst *Sel, Value *V1, Value *V2)
Definition GVN.cpp:317
BasicBlock * BB
BB - The basic block in question.
Definition GVN.cpp:296
Value * MaterializeAdjustedValue(LoadInst *Load) const
Emit code at the end of this block to adjust the value defined here to the specified type.
Definition GVN.cpp:324
Represents a particular available value that we know how to materialize.
Definition GVN.cpp:198
unsigned Offset
Offset - The byte offset in Val that is interesting for the load query.
Definition GVN.cpp:215
bool isSimpleValue() const
Definition GVN.cpp:261
static AvailableValue getSelect(SelectInst *Sel, Value *V1, Value *V2)
Definition GVN.cpp:251
bool isCoercedLoadValue() const
Definition GVN.cpp:262
static AvailableValue get(Value *V, unsigned Offset=0)
Definition GVN.cpp:219
ValType Kind
Kind of the live-out value.
Definition GVN.cpp:212
LoadInst * getCoercedLoadValue() const
Definition GVN.cpp:272
static AvailableValue getLoad(LoadInst *Load, unsigned Offset=0)
Definition GVN.cpp:235
static AvailableValue getMI(MemIntrinsic *MI, unsigned Offset=0)
Definition GVN.cpp:227
bool isUndefValue() const
Definition GVN.cpp:264
bool isSelectValue() const
Definition GVN.cpp:265
Value * Val
Val - The value that is live out of the block.
Definition GVN.cpp:210
Value * V1
V1, V2 - The dominating non-clobbered values of SelectVal.
Definition GVN.cpp:217
static AvailableValue getUndef()
Definition GVN.cpp:243
SelectInst * getSelectValue() const
Definition GVN.cpp:282
Value * getSimpleValue() const
Definition GVN.cpp:267
bool isMemIntrinValue() const
Definition GVN.cpp:263
MemIntrinsic * getMemIntrinValue() const
Definition GVN.cpp:277
Value * MaterializeAdjustedValue(LoadInst *Load, Instruction *InsertPt) const
Emit code at the specified insertion point to adjust the value defined here to the specified type.
Definition GVN.cpp:1143