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-max-num-deps", cl::Hidden, cl::init(100),
122 cl::desc("Max number of dependences to attempt Load PRE (default = 100)"));
123
124// This is based on IsValueFullyAvailableInBlockNumSpeculationsMax stat.
126 "gvn-max-block-speculations", cl::Hidden, cl::init(600),
127 cl::desc("Max number of blocks we're willing to speculate on (and recurse "
128 "into) when deducing if a value is fully available or not in GVN "
129 "(default = 600)"));
130
132 "gvn-max-num-visited-insts", cl::Hidden, cl::init(100),
133 cl::desc("Max number of visited instructions when trying to find "
134 "dominating value of select dependency (default = 100)"));
135
137 "gvn-max-num-insns", cl::Hidden, cl::init(100),
138 cl::desc("Max number of instructions to scan in each basic block in GVN "
139 "(default = 100)"));
140
143 bool Commutative = false;
144 // The type is not necessarily the result type of the expression, it may be
145 // any additional type needed to disambiguate the expression.
146 Type *Ty = nullptr;
148
149 AttributeList Attrs;
150
152
153 bool operator==(const Expression &Other) const {
154 if (Opcode != Other.Opcode)
155 return false;
156 if (Opcode == ~0U || Opcode == ~1U)
157 return true;
158 if (Ty != Other.Ty)
159 return false;
160 if (VarArgs != Other.VarArgs)
161 return false;
162 if ((!Attrs.isEmpty() || !Other.Attrs.isEmpty()) &&
163 !Attrs.intersectWith(Ty->getContext(), Other.Attrs).has_value())
164 return false;
165 return true;
166 }
167
169 return hash_combine(Value.Opcode, Value.Ty,
170 hash_combine_range(Value.VarArgs));
171 }
172};
173
175 static inline GVNPass::Expression getEmptyKey() { return ~0U; }
176 static inline GVNPass::Expression getTombstoneKey() { return ~1U; }
177
178 static unsigned getHashValue(const GVNPass::Expression &E) {
179 using llvm::hash_value;
180
181 return static_cast<unsigned>(hash_value(E));
182 }
183
184 static bool isEqual(const GVNPass::Expression &LHS,
185 const GVNPass::Expression &RHS) {
186 return LHS == RHS;
187 }
188};
189
190/// Represents a particular available value that we know how to materialize.
191/// Materialization of an AvailableValue never fails. An AvailableValue is
192/// implicitly associated with a rematerialization point which is the
193/// location of the instruction from which it was formed.
195 enum class ValType {
196 SimpleVal, // A simple offsetted value that is accessed.
197 LoadVal, // A value produced by a load.
198 MemIntrin, // A memory intrinsic which is loaded from.
199 UndefVal, // A UndefValue representing a value from dead block (which
200 // is not yet physically removed from the CFG).
201 SelectVal, // A pointer select which is loaded from and for which the load
202 // can be replace by a value select.
203 };
204
205 /// Val - The value that is live out of the block.
207 /// Kind of the live-out value.
209
210 /// Offset - The byte offset in Val that is interesting for the load query.
211 unsigned Offset = 0;
212 /// V1, V2 - The dominating non-clobbered values of SelectVal.
213 Value *V1 = nullptr, *V2 = nullptr;
214
215 static AvailableValue get(Value *V, unsigned Offset = 0) {
216 AvailableValue Res;
217 Res.Val = V;
219 Res.Offset = Offset;
220 return Res;
221 }
222
223 static AvailableValue getMI(MemIntrinsic *MI, unsigned Offset = 0) {
224 AvailableValue Res;
225 Res.Val = MI;
227 Res.Offset = Offset;
228 return Res;
229 }
230
231 static AvailableValue getLoad(LoadInst *Load, unsigned Offset = 0) {
232 AvailableValue Res;
233 Res.Val = Load;
235 Res.Offset = Offset;
236 return Res;
237 }
238
240 AvailableValue Res;
241 Res.Val = nullptr;
243 Res.Offset = 0;
244 return Res;
245 }
246
248 AvailableValue Res;
249 Res.Val = Sel;
251 Res.Offset = 0;
252 Res.V1 = V1;
253 Res.V2 = V2;
254 return Res;
255 }
256
257 bool isSimpleValue() const { return Kind == ValType::SimpleVal; }
258 bool isCoercedLoadValue() const { return Kind == ValType::LoadVal; }
259 bool isMemIntrinValue() const { return Kind == ValType::MemIntrin; }
260 bool isUndefValue() const { return Kind == ValType::UndefVal; }
261 bool isSelectValue() const { return Kind == ValType::SelectVal; }
262
264 assert(isSimpleValue() && "Wrong accessor");
265 return Val;
266 }
267
269 assert(isCoercedLoadValue() && "Wrong accessor");
270 return cast<LoadInst>(Val);
271 }
272
274 assert(isMemIntrinValue() && "Wrong accessor");
275 return cast<MemIntrinsic>(Val);
276 }
277
279 assert(isSelectValue() && "Wrong accessor");
280 return cast<SelectInst>(Val);
281 }
282
283 /// Emit code at the specified insertion point to adjust the value defined
284 /// here to the specified type. This handles various coercion cases.
285 Value *MaterializeAdjustedValue(LoadInst *Load, Instruction *InsertPt) const;
286};
287
288/// Represents an AvailableValue which can be rematerialized at the end of
289/// the associated BasicBlock.
291 /// BB - The basic block in question.
292 BasicBlock *BB = nullptr;
293
294 /// AV - The actual available value.
296
299 Res.BB = BB;
300 Res.AV = std::move(AV);
301 return Res;
302 }
303
305 unsigned Offset = 0) {
306 return get(BB, AvailableValue::get(V, Offset));
307 }
308
312
314 Value *V1, Value *V2) {
315 return get(BB, AvailableValue::getSelect(Sel, V1, V2));
316 }
317
318 /// Emit code at the end of this block to adjust the value defined here to
319 /// the specified type. This handles various coercion cases.
321 return AV.MaterializeAdjustedValue(Load, BB->getTerminator());
322 }
323};
324
325//===----------------------------------------------------------------------===//
326// ValueTable Internal Functions
327//===----------------------------------------------------------------------===//
328
329GVNPass::Expression GVNPass::ValueTable::createExpr(Instruction *I) {
330 Expression E;
331 E.Ty = I->getType();
332 E.Opcode = I->getOpcode();
333 if (const GCRelocateInst *GCR = dyn_cast<GCRelocateInst>(I)) {
334 // gc.relocate is 'special' call: its second and third operands are
335 // not real values, but indices into statepoint's argument list.
336 // Use the refered to values for purposes of identity.
337 E.VarArgs.push_back(lookupOrAdd(GCR->getOperand(0)));
338 E.VarArgs.push_back(lookupOrAdd(GCR->getBasePtr()));
339 E.VarArgs.push_back(lookupOrAdd(GCR->getDerivedPtr()));
340 } else {
341 for (Use &Op : I->operands())
342 E.VarArgs.push_back(lookupOrAdd(Op));
343 }
344 if (I->isCommutative()) {
345 // Ensure that commutative instructions that only differ by a permutation
346 // of their operands get the same value number by sorting the operand value
347 // numbers. Since commutative operands are the 1st two operands it is more
348 // efficient to sort by hand rather than using, say, std::sort.
349 assert(I->getNumOperands() >= 2 && "Unsupported commutative instruction!");
350 if (E.VarArgs[0] > E.VarArgs[1])
351 std::swap(E.VarArgs[0], E.VarArgs[1]);
352 E.Commutative = true;
353 }
354
355 if (auto *C = dyn_cast<CmpInst>(I)) {
356 // Sort the operand value numbers so x<y and y>x get the same value number.
357 CmpInst::Predicate Predicate = C->getPredicate();
358 if (E.VarArgs[0] > E.VarArgs[1]) {
359 std::swap(E.VarArgs[0], E.VarArgs[1]);
361 }
362 E.Opcode = (C->getOpcode() << 8) | Predicate;
363 E.Commutative = true;
364 } else if (auto *IVI = dyn_cast<InsertValueInst>(I)) {
365 E.VarArgs.append(IVI->idx_begin(), IVI->idx_end());
366 } else if (auto *SVI = dyn_cast<ShuffleVectorInst>(I)) {
367 ArrayRef<int> ShuffleMask = SVI->getShuffleMask();
368 E.VarArgs.append(ShuffleMask.begin(), ShuffleMask.end());
369 } else if (auto *CB = dyn_cast<CallBase>(I)) {
370 E.Attrs = CB->getAttributes();
371 }
372
373 return E;
374}
375
376GVNPass::Expression GVNPass::ValueTable::createCmpExpr(
377 unsigned Opcode, CmpInst::Predicate Predicate, Value *LHS, Value *RHS) {
378 assert((Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) &&
379 "Not a comparison!");
382 E.VarArgs.push_back(lookupOrAdd(LHS));
383 E.VarArgs.push_back(lookupOrAdd(RHS));
384
385 // Sort the operand value numbers so x<y and y>x get the same value number.
386 if (E.VarArgs[0] > E.VarArgs[1]) {
387 std::swap(E.VarArgs[0], E.VarArgs[1]);
389 }
390 E.Opcode = (Opcode << 8) | Predicate;
391 E.Commutative = true;
392 return E;
393}
394
395GVNPass::Expression
396GVNPass::ValueTable::createExtractvalueExpr(ExtractValueInst *EI) {
397 assert(EI && "Not an ExtractValueInst?");
399 E.Ty = EI->getType();
400 E.Opcode = 0;
401
402 WithOverflowInst *WO = dyn_cast<WithOverflowInst>(EI->getAggregateOperand());
403 if (WO != nullptr && EI->getNumIndices() == 1 && *EI->idx_begin() == 0) {
404 // EI is an extract from one of our with.overflow intrinsics. Synthesize
405 // a semantically equivalent expression instead of an extract value
406 // expression.
407 E.Opcode = WO->getBinaryOp();
408 E.VarArgs.push_back(lookupOrAdd(WO->getLHS()));
409 E.VarArgs.push_back(lookupOrAdd(WO->getRHS()));
410 return E;
411 }
412
413 // Not a recognised intrinsic. Fall back to producing an extract value
414 // expression.
415 E.Opcode = EI->getOpcode();
416 for (Use &Op : EI->operands())
417 E.VarArgs.push_back(lookupOrAdd(Op));
418
419 append_range(E.VarArgs, EI->indices());
420
421 return E;
422}
423
424GVNPass::Expression GVNPass::ValueTable::createGEPExpr(GetElementPtrInst *GEP) {
426 Type *PtrTy = GEP->getType()->getScalarType();
427 const DataLayout &DL = GEP->getDataLayout();
428 unsigned BitWidth = DL.getIndexTypeSizeInBits(PtrTy);
429 SmallMapVector<Value *, APInt, 4> VariableOffsets;
430 APInt ConstantOffset(BitWidth, 0);
431 if (GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset)) {
432 // Convert into offset representation, to recognize equivalent address
433 // calculations that use different type encoding.
434 LLVMContext &Context = GEP->getContext();
435 E.Opcode = GEP->getOpcode();
436 E.Ty = nullptr;
437 E.VarArgs.push_back(lookupOrAdd(GEP->getPointerOperand()));
438 for (const auto &[V, Scale] : VariableOffsets) {
439 E.VarArgs.push_back(lookupOrAdd(V));
440 E.VarArgs.push_back(lookupOrAdd(ConstantInt::get(Context, Scale)));
441 }
442 if (!ConstantOffset.isZero())
443 E.VarArgs.push_back(
444 lookupOrAdd(ConstantInt::get(Context, ConstantOffset)));
445 } else {
446 // If converting to offset representation fails (for scalable vectors),
447 // fall back to type-based implementation.
448 E.Opcode = GEP->getOpcode();
449 E.Ty = GEP->getSourceElementType();
450 for (Use &Op : GEP->operands())
451 E.VarArgs.push_back(lookupOrAdd(Op));
452 }
453 return E;
454}
455
456//===----------------------------------------------------------------------===//
457// ValueTable External Functions
458//===----------------------------------------------------------------------===//
459
460GVNPass::ValueTable::ValueTable() = default;
461GVNPass::ValueTable::ValueTable(const ValueTable &) = default;
462GVNPass::ValueTable::ValueTable(ValueTable &&) = default;
463GVNPass::ValueTable::~ValueTable() = default;
464GVNPass::ValueTable &
465GVNPass::ValueTable::operator=(const GVNPass::ValueTable &Arg) = default;
466
467/// add - Insert a value into the table with a specified value number.
468void GVNPass::ValueTable::add(Value *V, uint32_t Num) {
469 ValueNumbering.insert(std::make_pair(V, Num));
470 if (PHINode *PN = dyn_cast<PHINode>(V))
471 NumberingPhi[Num] = PN;
472}
473
474/// Include the incoming memory state into the hash of the expression for the
475/// given instruction. If the incoming memory state is:
476/// * LiveOnEntry, add the value number of the entry block,
477/// * a MemoryPhi, add the value number of the basic block corresponding to that
478/// MemoryPhi,
479/// * a MemoryDef, add the value number of the memory setting instruction.
480void GVNPass::ValueTable::addMemoryStateToExp(Instruction *I, Expression &Exp) {
481 assert(MSSA && "addMemoryStateToExp should not be called without MemorySSA");
482 assert(MSSA->getMemoryAccess(I) && "Instruction does not access memory");
483 MemoryAccess *MA = MSSA->getSkipSelfWalker()->getClobberingMemoryAccess(I);
484 Exp.VarArgs.push_back(lookupOrAdd(MA));
485}
486
487uint32_t GVNPass::ValueTable::lookupOrAddCall(CallInst *C) {
488 // FIXME: Currently the calls which may access the thread id may
489 // be considered as not accessing the memory. But this is
490 // problematic for coroutines, since coroutines may resume in a
491 // different thread. So we disable the optimization here for the
492 // correctness. However, it may block many other correct
493 // optimizations. Revert this one when we detect the memory
494 // accessing kind more precisely.
495 if (C->getFunction()->isPresplitCoroutine()) {
496 ValueNumbering[C] = NextValueNumber;
497 return NextValueNumber++;
498 }
499
500 // Do not combine convergent calls since they implicitly depend on the set of
501 // threads that is currently executing, and they might be in different basic
502 // blocks.
503 if (C->isConvergent()) {
504 ValueNumbering[C] = NextValueNumber;
505 return NextValueNumber++;
506 }
507
508 if (AA->doesNotAccessMemory(C)) {
509 Expression Exp = createExpr(C);
510 uint32_t E = assignExpNewValueNum(Exp).first;
511 ValueNumbering[C] = E;
512 return E;
513 }
514
515 if (MD && AA->onlyReadsMemory(C)) {
516 Expression Exp = createExpr(C);
517 auto [E, IsValNumNew] = assignExpNewValueNum(Exp);
518 if (IsValNumNew) {
519 ValueNumbering[C] = E;
520 return E;
521 }
522
523 MemDepResult LocalDep = MD->getDependency(C);
524
525 if (!LocalDep.isDef() && !LocalDep.isNonLocal()) {
526 ValueNumbering[C] = NextValueNumber;
527 return NextValueNumber++;
528 }
529
530 if (LocalDep.isDef()) {
531 // For masked load/store intrinsics, the local_dep may actually be
532 // a normal load or store instruction.
533 CallInst *LocalDepCall = dyn_cast<CallInst>(LocalDep.getInst());
534
535 if (!LocalDepCall || LocalDepCall->arg_size() != C->arg_size()) {
536 ValueNumbering[C] = NextValueNumber;
537 return NextValueNumber++;
538 }
539
540 for (unsigned I = 0, E = C->arg_size(); I < E; ++I) {
541 uint32_t CVN = lookupOrAdd(C->getArgOperand(I));
542 uint32_t LocalDepCallVN = lookupOrAdd(LocalDepCall->getArgOperand(I));
543 if (CVN != LocalDepCallVN) {
544 ValueNumbering[C] = NextValueNumber;
545 return NextValueNumber++;
546 }
547 }
548
549 uint32_t V = lookupOrAdd(LocalDepCall);
550 ValueNumbering[C] = V;
551 return V;
552 }
553
554 // Non-local case.
556 MD->getNonLocalCallDependency(C);
557 // FIXME: Move the checking logic to MemDep!
558 CallInst *CDep = nullptr;
559
560 // Check to see if we have a single dominating call instruction that is
561 // identical to C.
562 for (const NonLocalDepEntry &I : Deps) {
563 if (I.getResult().isNonLocal())
564 continue;
565
566 // We don't handle non-definitions. If we already have a call, reject
567 // instruction dependencies.
568 if (!I.getResult().isDef() || CDep != nullptr) {
569 CDep = nullptr;
570 break;
571 }
572
573 CallInst *NonLocalDepCall = dyn_cast<CallInst>(I.getResult().getInst());
574 // FIXME: All duplicated with non-local case.
575 if (NonLocalDepCall && DT->properlyDominates(I.getBB(), C->getParent())) {
576 CDep = NonLocalDepCall;
577 continue;
578 }
579
580 CDep = nullptr;
581 break;
582 }
583
584 if (!CDep) {
585 ValueNumbering[C] = NextValueNumber;
586 return NextValueNumber++;
587 }
588
589 if (CDep->arg_size() != C->arg_size()) {
590 ValueNumbering[C] = NextValueNumber;
591 return NextValueNumber++;
592 }
593 for (unsigned I = 0, E = C->arg_size(); I < E; ++I) {
594 uint32_t CVN = lookupOrAdd(C->getArgOperand(I));
595 uint32_t CDepVN = lookupOrAdd(CDep->getArgOperand(I));
596 if (CVN != CDepVN) {
597 ValueNumbering[C] = NextValueNumber;
598 return NextValueNumber++;
599 }
600 }
601
602 uint32_t V = lookupOrAdd(CDep);
603 ValueNumbering[C] = V;
604 return V;
605 }
606
607 if (MSSA && IsMSSAEnabled && AA->onlyReadsMemory(C)) {
608 Expression Exp = createExpr(C);
609 addMemoryStateToExp(C, Exp);
610 auto [V, _] = assignExpNewValueNum(Exp);
611 ValueNumbering[C] = V;
612 return V;
613 }
614
615 ValueNumbering[C] = NextValueNumber;
616 return NextValueNumber++;
617}
618
619/// Returns the value number for the specified load or store instruction.
620uint32_t GVNPass::ValueTable::computeLoadStoreVN(Instruction *I) {
621 if (!MSSA || !IsMSSAEnabled) {
622 ValueNumbering[I] = NextValueNumber;
623 return NextValueNumber++;
624 }
625
627 Exp.Ty = I->getType();
628 Exp.Opcode = I->getOpcode();
629 for (Use &Op : I->operands())
630 Exp.VarArgs.push_back(lookupOrAdd(Op));
631 addMemoryStateToExp(I, Exp);
632
633 auto [V, _] = assignExpNewValueNum(Exp);
634 ValueNumbering[I] = V;
635 return V;
636}
637
638/// Returns true if a value number exists for the specified value.
639bool GVNPass::ValueTable::exists(Value *V) const {
640 return ValueNumbering.contains(V);
641}
642
643uint32_t GVNPass::ValueTable::lookupOrAdd(MemoryAccess *MA) {
644 return MSSA->isLiveOnEntryDef(MA) || isa<MemoryPhi>(MA)
645 ? lookupOrAdd(MA->getBlock())
646 : lookupOrAdd(cast<MemoryUseOrDef>(MA)->getMemoryInst());
647}
648
649/// lookupOrAdd - Returns the value number for the specified value, assigning
650/// it a new number if it did not have one before.
651uint32_t GVNPass::ValueTable::lookupOrAdd(Value *V) {
652 auto VI = ValueNumbering.find(V);
653 if (VI != ValueNumbering.end())
654 return VI->second;
655
656 auto *I = dyn_cast<Instruction>(V);
657 if (!I) {
658 ValueNumbering[V] = NextValueNumber;
659 if (isa<BasicBlock>(V))
660 NumberingBB[NextValueNumber] = cast<BasicBlock>(V);
661 return NextValueNumber++;
662 }
663
664 Expression Exp;
665 switch (I->getOpcode()) {
666 case Instruction::Call:
667 return lookupOrAddCall(cast<CallInst>(I));
668 case Instruction::FNeg:
669 case Instruction::Add:
670 case Instruction::FAdd:
671 case Instruction::Sub:
672 case Instruction::FSub:
673 case Instruction::Mul:
674 case Instruction::FMul:
675 case Instruction::UDiv:
676 case Instruction::SDiv:
677 case Instruction::FDiv:
678 case Instruction::URem:
679 case Instruction::SRem:
680 case Instruction::FRem:
681 case Instruction::Shl:
682 case Instruction::LShr:
683 case Instruction::AShr:
684 case Instruction::And:
685 case Instruction::Or:
686 case Instruction::Xor:
687 case Instruction::ICmp:
688 case Instruction::FCmp:
689 case Instruction::Trunc:
690 case Instruction::ZExt:
691 case Instruction::SExt:
692 case Instruction::FPToUI:
693 case Instruction::FPToSI:
694 case Instruction::UIToFP:
695 case Instruction::SIToFP:
696 case Instruction::FPTrunc:
697 case Instruction::FPExt:
698 case Instruction::PtrToInt:
699 case Instruction::PtrToAddr:
700 case Instruction::IntToPtr:
701 case Instruction::AddrSpaceCast:
702 case Instruction::BitCast:
703 case Instruction::Select:
704 case Instruction::Freeze:
705 case Instruction::ExtractElement:
706 case Instruction::InsertElement:
707 case Instruction::ShuffleVector:
708 case Instruction::InsertValue:
709 Exp = createExpr(I);
710 break;
711 case Instruction::GetElementPtr:
712 Exp = createGEPExpr(cast<GetElementPtrInst>(I));
713 break;
714 case Instruction::ExtractValue:
715 Exp = createExtractvalueExpr(cast<ExtractValueInst>(I));
716 break;
717 case Instruction::PHI:
718 ValueNumbering[V] = NextValueNumber;
719 NumberingPhi[NextValueNumber] = cast<PHINode>(V);
720 return NextValueNumber++;
721 case Instruction::Load:
722 case Instruction::Store:
723 return computeLoadStoreVN(I);
724 default:
725 ValueNumbering[V] = NextValueNumber;
726 return NextValueNumber++;
727 }
728
729 uint32_t E = assignExpNewValueNum(Exp).first;
730 ValueNumbering[V] = E;
731 return E;
732}
733
734/// Returns the value number of the specified value. Fails if
735/// the value has not yet been numbered.
736uint32_t GVNPass::ValueTable::lookup(Value *V, bool Verify) const {
737 auto VI = ValueNumbering.find(V);
738 if (Verify) {
739 assert(VI != ValueNumbering.end() && "Value not numbered?");
740 return VI->second;
741 }
742 return (VI != ValueNumbering.end()) ? VI->second : 0;
743}
744
745/// Returns the value number of the given comparison,
746/// assigning it a new number if it did not have one before. Useful when
747/// we deduced the result of a comparison, but don't immediately have an
748/// instruction realizing that comparison to hand.
749uint32_t GVNPass::ValueTable::lookupOrAddCmp(unsigned Opcode,
750 CmpInst::Predicate Predicate,
751 Value *LHS, Value *RHS) {
752 Expression Exp = createCmpExpr(Opcode, Predicate, LHS, RHS);
753 return assignExpNewValueNum(Exp).first;
754}
755
756/// Remove all entries from the ValueTable.
758 ValueNumbering.clear();
759 ExpressionNumbering.clear();
760 NumberingPhi.clear();
761 NumberingBB.clear();
762 PhiTranslateTable.clear();
763 NextValueNumber = 1;
764 Expressions.clear();
765 ExprIdx.clear();
766 NextExprNumber = 0;
767}
768
769/// Remove a value from the value numbering.
771 uint32_t Num = ValueNumbering.lookup(V);
772 ValueNumbering.erase(V);
773 // If V is PHINode, V <--> value number is an one-to-one mapping.
774 if (isa<PHINode>(V))
775 NumberingPhi.erase(Num);
776 else if (isa<BasicBlock>(V))
777 NumberingBB.erase(Num);
778}
779
780/// verifyRemoved - Verify that the value is removed from all internal data
781/// structures.
782void GVNPass::ValueTable::verifyRemoved(const Value *V) const {
783 assert(!ValueNumbering.contains(V) &&
784 "Inst still occurs in value numbering map!");
785}
786
787//===----------------------------------------------------------------------===//
788// LeaderMap External Functions
789//===----------------------------------------------------------------------===//
790
791/// Push a new Value to the LeaderTable onto the list for its value number.
792void GVNPass::LeaderMap::insert(uint32_t N, Value *V, const BasicBlock *BB) {
793 const auto &[It, Inserted] = NumToLeaders.try_emplace(N, V, BB, nullptr);
794 if (!Inserted) {
795 // Key already exists: insert new node after the head.
796 auto *NewSlot = TableAllocator.Allocate<LeaderListNode>();
797 new (NewSlot) LeaderListNode(V, BB, It->second.Next);
798 It->second.Next = NewSlot;
799 }
800}
801
802/// Scan the list of values corresponding to a given
803/// value number, and remove the given instruction if encountered.
804void GVNPass::LeaderMap::erase(uint32_t N, Instruction *I,
805 const BasicBlock *BB) {
806 auto It = NumToLeaders.find(N);
807 if (It == NumToLeaders.end())
808 return;
809
810 LeaderListNode *Prev = nullptr;
811 LeaderListNode *Curr = &It->second;
812
813 while (Curr && (Curr->Entry.Val != I || Curr->Entry.BB != BB)) {
814 Prev = Curr;
815 Curr = Curr->Next;
816 }
817
818 if (!Curr)
819 return;
820
821 if (Prev) {
822 // Non-head node: unlink and destroy.
823 Prev->Next = Curr->Next;
824 Curr->~LeaderListNode();
825 TableAllocator.Deallocate<LeaderListNode>(Curr);
826 } else {
827 // Head node (stored by value in DenseMap).
828 if (!Curr->Next) {
829 // Only node; erase from map (DenseMap calls the destructor).
830 NumToLeaders.erase(It);
831 } else {
832 // Move second node's data into head, then destroy second node.
833 LeaderListNode *Next = Curr->Next;
834 Curr->Entry.Val = std::move(Next->Entry.Val);
835 Curr->Entry.BB = Next->Entry.BB;
836 Curr->Next = Next->Next;
837 Next->~LeaderListNode();
838 TableAllocator.Deallocate<LeaderListNode>(Next);
839 }
840 }
841}
842
843//===----------------------------------------------------------------------===//
844// GVN Pass
845//===----------------------------------------------------------------------===//
846
848 return Options.AllowScalarPRE.value_or(GVNEnableScalarPRE);
849}
850
852 return Options.AllowLoadPRE.value_or(GVNEnableLoadPRE);
853}
854
856 return Options.AllowLoadInLoopPRE.value_or(GVNEnableLoadInLoopPRE);
857}
858
860 return Options.AllowLoadPRESplitBackedge.value_or(
862}
863
865 return Options.AllowMemDep.value_or(GVNEnableMemDep);
866}
867
869 return Options.AllowMemorySSA.value_or(GVNEnableMemorySSA);
870}
871
873 // FIXME: The order of evaluation of these 'getResult' calls is very
874 // significant! Re-ordering these variables will cause GVN when run alone to
875 // be less effective! We should fix memdep and basic-aa to not exhibit this
876 // behavior, but until then don't change the order here.
877 auto &AC = AM.getResult<AssumptionAnalysis>(F);
878 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
879 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
880 auto &AA = AM.getResult<AAManager>(F);
881 auto *MemDep =
883 auto &LI = AM.getResult<LoopAnalysis>(F);
884 auto *MSSA = AM.getCachedResult<MemorySSAAnalysis>(F);
885 if (isMemorySSAEnabled() && !MSSA) {
886 assert(!MemDep &&
887 "On-demand computation of MemSSA implies that MemDep is disabled!");
888 MSSA = &AM.getResult<MemorySSAAnalysis>(F);
889 }
891 bool Changed = runImpl(F, AC, DT, TLI, AA, MemDep, LI, &ORE,
892 MSSA ? &MSSA->getMSSA() : nullptr);
893 if (!Changed)
894 return PreservedAnalyses::all();
898 if (MSSA)
901 return PA;
902}
903
905 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
906 static_cast<PassInfoMixin<GVNPass> *>(this)->printPipeline(
907 OS, MapClassName2PassName);
908
909 OS << '<';
910 if (Options.AllowScalarPRE != std::nullopt)
911 OS << (*Options.AllowScalarPRE ? "" : "no-") << "scalar-pre;";
912 if (Options.AllowLoadPRE != std::nullopt)
913 OS << (*Options.AllowLoadPRE ? "" : "no-") << "load-pre;";
914 if (Options.AllowLoadPRESplitBackedge != std::nullopt)
915 OS << (*Options.AllowLoadPRESplitBackedge ? "" : "no-")
916 << "split-backedge-load-pre;";
917 if (Options.AllowMemDep != std::nullopt)
918 OS << (*Options.AllowMemDep ? "" : "no-") << "memdep;";
919 if (Options.AllowMemorySSA != std::nullopt)
920 OS << (*Options.AllowMemorySSA ? "" : "no-") << "memoryssa";
921 OS << '>';
922}
923
925 salvageKnowledge(I, AC);
927 removeInstruction(I);
928}
929
930#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
931LLVM_DUMP_METHOD void GVNPass::dump(DenseMap<uint32_t, Value *> &Map) const {
932 errs() << "{\n";
933 for (const auto &[Num, Exp] : Map) {
934 errs() << Num << "\n";
935 Exp->dump();
936 }
937 errs() << "}\n";
938}
939#endif
940
941enum class AvailabilityState : char {
942 /// We know the block *is not* fully available. This is a fixpoint.
944 /// We know the block *is* fully available. This is a fixpoint.
946 /// We do not know whether the block is fully available or not,
947 /// but we are currently speculating that it will be.
948 /// If it would have turned out that the block was, in fact, not fully
949 /// available, this would have been cleaned up into an Unavailable.
951};
952
953/// Return true if we can prove that the value
954/// we're analyzing is fully available in the specified block. As we go, keep
955/// track of which blocks we know are fully alive in FullyAvailableBlocks. This
956/// map is actually a tri-state map with the following values:
957/// 0) we know the block *is not* fully available.
958/// 1) we know the block *is* fully available.
959/// 2) we do not know whether the block is fully available or not, but we are
960/// currently speculating that it will be.
962 BasicBlock *BB,
963 DenseMap<BasicBlock *, AvailabilityState> &FullyAvailableBlocks) {
965 std::optional<BasicBlock *> UnavailableBB;
966
967 // The number of times we didn't find an entry for a block in a map and
968 // optimistically inserted an entry marking block as speculatively available.
969 unsigned NumNewNewSpeculativelyAvailableBBs = 0;
970
971#ifndef NDEBUG
972 SmallPtrSet<BasicBlock *, 32> NewSpeculativelyAvailableBBs;
974#endif
975
976 Worklist.emplace_back(BB);
977 while (!Worklist.empty()) {
978 BasicBlock *CurrBB = Worklist.pop_back_val(); // LoadFO - depth-first!
979 // Optimistically assume that the block is Speculatively Available and check
980 // to see if we already know about this block in one lookup.
981 std::pair<DenseMap<BasicBlock *, AvailabilityState>::iterator, bool> IV =
982 FullyAvailableBlocks.try_emplace(
984 AvailabilityState &State = IV.first->second;
985
986 // Did the entry already exist for this block?
987 if (!IV.second) {
988 if (State == AvailabilityState::Unavailable) {
989 UnavailableBB = CurrBB;
990 break; // Backpropagate unavailability info.
991 }
992
993#ifndef NDEBUG
994 AvailableBBs.emplace_back(CurrBB);
995#endif
996 continue; // Don't recurse further, but continue processing worklist.
997 }
998
999 // No entry found for block.
1000 ++NumNewNewSpeculativelyAvailableBBs;
1001 bool OutOfBudget = NumNewNewSpeculativelyAvailableBBs > MaxBBSpeculations;
1002
1003 // If we have exhausted our budget, mark this block as unavailable.
1004 // Also, if this block has no predecessors, the value isn't live-in here.
1005 if (OutOfBudget || pred_empty(CurrBB)) {
1006 MaxBBSpeculationCutoffReachedTimes += (int)OutOfBudget;
1008 UnavailableBB = CurrBB;
1009 break; // Backpropagate unavailability info.
1010 }
1011
1012 // Tentatively consider this block as speculatively available.
1013#ifndef NDEBUG
1014 NewSpeculativelyAvailableBBs.insert(CurrBB);
1015#endif
1016 // And further recurse into block's predecessors, in depth-first order!
1017 Worklist.append(pred_begin(CurrBB), pred_end(CurrBB));
1018 }
1019
1020#if LLVM_ENABLE_STATS
1021 IsValueFullyAvailableInBlockNumSpeculationsMax.updateMax(
1022 NumNewNewSpeculativelyAvailableBBs);
1023#endif
1024
1025 // If the block isn't marked as fixpoint yet
1026 // (the Unavailable and Available states are fixpoints).
1027 auto MarkAsFixpointAndEnqueueSuccessors =
1028 [&](BasicBlock *BB, AvailabilityState FixpointState) {
1029 auto It = FullyAvailableBlocks.find(BB);
1030 if (It == FullyAvailableBlocks.end())
1031 return; // Never queried this block, leave as-is.
1032 switch (AvailabilityState &State = It->second) {
1035 return; // Don't backpropagate further, continue processing worklist.
1037 State = FixpointState;
1038#ifndef NDEBUG
1039 assert(NewSpeculativelyAvailableBBs.erase(BB) &&
1040 "Found a speculatively available successor leftover?");
1041#endif
1042 // Queue successors for further processing.
1043 Worklist.append(succ_begin(BB), succ_end(BB));
1044 return;
1045 }
1046 };
1047
1048 if (UnavailableBB) {
1049 // Okay, we have encountered an unavailable block.
1050 // Mark speculatively available blocks reachable from UnavailableBB as
1051 // unavailable as well. Paths are terminated when they reach blocks not in
1052 // FullyAvailableBlocks or they are not marked as speculatively available.
1053 Worklist.clear();
1054 Worklist.append(succ_begin(*UnavailableBB), succ_end(*UnavailableBB));
1055 while (!Worklist.empty())
1056 MarkAsFixpointAndEnqueueSuccessors(Worklist.pop_back_val(),
1058 }
1059
1060#ifndef NDEBUG
1061 Worklist.clear();
1062 for (BasicBlock *AvailableBB : AvailableBBs)
1063 Worklist.append(succ_begin(AvailableBB), succ_end(AvailableBB));
1064 while (!Worklist.empty())
1065 MarkAsFixpointAndEnqueueSuccessors(Worklist.pop_back_val(),
1067
1068 assert(NewSpeculativelyAvailableBBs.empty() &&
1069 "Must have fixed all the new speculatively available blocks.");
1070#endif
1071
1072 return !UnavailableBB;
1073}
1074
1075/// If the specified OldValue exists in ValuesPerBlock, replace its value with
1076/// NewValue.
1078 SmallVectorImpl<AvailableValueInBlock> &ValuesPerBlock, Value *OldValue,
1079 Value *NewValue) {
1080 for (AvailableValueInBlock &V : ValuesPerBlock) {
1081 if (V.AV.Val == OldValue)
1082 V.AV.Val = NewValue;
1083 if (V.AV.isSelectValue()) {
1084 if (V.AV.V1 == OldValue)
1085 V.AV.V1 = NewValue;
1086 if (V.AV.V2 == OldValue)
1087 V.AV.V2 = NewValue;
1088 }
1089 }
1090}
1091
1092/// Given a set of loads specified by ValuesPerBlock,
1093/// construct SSA form, allowing us to eliminate Load. This returns the value
1094/// that should be used at Load's definition site.
1095static Value *
1098 GVNPass &GVN) {
1099 // Check for the fully redundant, dominating load case. In this case, we can
1100 // just use the dominating value directly.
1101 if (ValuesPerBlock.size() == 1 &&
1102 GVN.getDominatorTree().properlyDominates(ValuesPerBlock[0].BB,
1103 Load->getParent())) {
1104 assert(!ValuesPerBlock[0].AV.isUndefValue() &&
1105 "Dead BB dominate this block");
1106 return ValuesPerBlock[0].MaterializeAdjustedValue(Load);
1107 }
1108
1109 // Otherwise, we have to construct SSA form.
1111 SSAUpdater SSAUpdate(&NewPHIs);
1112 SSAUpdate.Initialize(Load->getType(), Load->getName());
1113
1114 for (const AvailableValueInBlock &AV : ValuesPerBlock) {
1115 BasicBlock *BB = AV.BB;
1116
1117 if (AV.AV.isUndefValue())
1118 continue;
1119
1120 if (SSAUpdate.HasValueForBlock(BB))
1121 continue;
1122
1123 // If the value is the load that we will be eliminating, and the block it's
1124 // available in is the block that the load is in, then don't add it as
1125 // SSAUpdater will resolve the value to the relevant phi which may let it
1126 // avoid phi construction entirely if there's actually only one value.
1127 if (BB == Load->getParent() &&
1128 ((AV.AV.isSimpleValue() && AV.AV.getSimpleValue() == Load) ||
1129 (AV.AV.isCoercedLoadValue() && AV.AV.getCoercedLoadValue() == Load)))
1130 continue;
1131
1132 SSAUpdate.AddAvailableValue(BB, AV.MaterializeAdjustedValue(Load));
1133 }
1134
1135 // Perform PHI construction.
1136 return SSAUpdate.GetValueInMiddleOfBlock(Load->getParent());
1137}
1138
1140 Instruction *InsertPt) const {
1141 Value *Res;
1142 Type *LoadTy = Load->getType();
1143 const DataLayout &DL = Load->getDataLayout();
1144 if (isSimpleValue()) {
1145 Res = getSimpleValue();
1146 if (Res->getType() != LoadTy) {
1147 Res = getValueForLoad(Res, Offset, LoadTy, InsertPt, Load->getFunction());
1148
1149 LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL VAL:\nOffset: " << Offset
1150 << " " << *getSimpleValue() << '\n'
1151 << *Res << '\n'
1152 << "\n\n\n");
1153 }
1154 } else if (isCoercedLoadValue()) {
1155 LoadInst *CoercedLoad = getCoercedLoadValue();
1156 if (CoercedLoad->getType() == LoadTy && Offset == 0) {
1157 Res = CoercedLoad;
1158 combineMetadataForCSE(CoercedLoad, Load, false);
1159 } else {
1160 Res = getValueForLoad(CoercedLoad, Offset, LoadTy, InsertPt,
1161 Load->getFunction());
1162 // We are adding a new user for this load, for which the original
1163 // metadata may not hold. Additionally, the new load may have a different
1164 // size and type, so their metadata cannot be combined in any
1165 // straightforward way.
1166 // Drop all metadata that is not known to cause immediate UB on violation,
1167 // unless the load has !noundef, in which case all metadata violations
1168 // will be promoted to UB.
1169 // TODO: We can combine noalias/alias.scope metadata here, because it is
1170 // independent of the load type.
1171 if (!CoercedLoad->hasMetadata(LLVMContext::MD_noundef))
1172 CoercedLoad->dropUnknownNonDebugMetadata(
1173 {LLVMContext::MD_dereferenceable,
1174 LLVMContext::MD_dereferenceable_or_null,
1175 LLVMContext::MD_invariant_load, LLVMContext::MD_invariant_group});
1176 LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL LOAD:\nOffset: " << Offset
1177 << " " << *getCoercedLoadValue() << '\n'
1178 << *Res << '\n'
1179 << "\n\n\n");
1180 }
1181 } else if (isMemIntrinValue()) {
1183 InsertPt, DL);
1184 LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL MEM INTRIN:\nOffset: " << Offset
1185 << " " << *getMemIntrinValue() << '\n'
1186 << *Res << '\n'
1187 << "\n\n\n");
1188 } else if (isSelectValue()) {
1189 // Introduce a new value select for a load from an eligible pointer select.
1190 SelectInst *Sel = getSelectValue();
1191 assert(V1 && V2 && "both value operands of the select must be present");
1192 Res =
1193 SelectInst::Create(Sel->getCondition(), V1, V2, "", Sel->getIterator());
1194 // We use the DebugLoc from the original load here, as this instruction
1195 // materializes the value that would previously have been loaded.
1196 cast<SelectInst>(Res)->setDebugLoc(Load->getDebugLoc());
1197 } else {
1198 llvm_unreachable("Should not materialize value from dead block");
1199 }
1200 assert(Res && "failed to materialize?");
1201 return Res;
1202}
1203
1204static bool isLifetimeStart(const Instruction *Inst) {
1205 if (const IntrinsicInst* II = dyn_cast<IntrinsicInst>(Inst))
1206 return II->getIntrinsicID() == Intrinsic::lifetime_start;
1207 return false;
1208}
1209
1210/// Assuming To can be reached from both From and Between, does Between lie on
1211/// every path from From to To?
1212static bool liesBetween(const Instruction *From, Instruction *Between,
1213 const Instruction *To, const DominatorTree *DT) {
1214 if (From->getParent() == Between->getParent())
1215 return DT->dominates(From, Between);
1217 Exclusion.insert(Between->getParent());
1218 return !isPotentiallyReachable(From, To, &Exclusion, DT);
1219}
1220
1222 const DominatorTree *DT) {
1223 Value *PtrOp = Load->getPointerOperand();
1224 if (!PtrOp->hasUseList())
1225 return nullptr;
1226
1227 Instruction *OtherAccess = nullptr;
1228
1229 for (auto *U : PtrOp->users()) {
1230 if (U != Load && (isa<LoadInst>(U) || isa<StoreInst>(U))) {
1231 auto *I = cast<Instruction>(U);
1232 if (I->getFunction() == Load->getFunction() && DT->dominates(I, Load)) {
1233 // Use the most immediately dominating value.
1234 if (OtherAccess) {
1235 if (DT->dominates(OtherAccess, I))
1236 OtherAccess = I;
1237 else
1238 assert(U == OtherAccess || DT->dominates(I, OtherAccess));
1239 } else
1240 OtherAccess = I;
1241 }
1242 }
1243 }
1244
1245 if (OtherAccess)
1246 return OtherAccess;
1247
1248 // There is no dominating use, check if we can find a closest non-dominating
1249 // use that lies between any other potentially available use and Load.
1250 for (auto *U : PtrOp->users()) {
1251 if (U != Load && (isa<LoadInst>(U) || isa<StoreInst>(U))) {
1252 auto *I = cast<Instruction>(U);
1253 if (I->getFunction() == Load->getFunction() &&
1254 isPotentiallyReachable(I, Load, nullptr, DT)) {
1255 if (OtherAccess) {
1256 if (liesBetween(OtherAccess, I, Load, DT)) {
1257 OtherAccess = I;
1258 } else if (!liesBetween(I, OtherAccess, Load, DT)) {
1259 // These uses are both partially available at Load were it not for
1260 // the clobber, but neither lies strictly after the other.
1261 OtherAccess = nullptr;
1262 break;
1263 } // else: keep current OtherAccess since it lies between U and
1264 // Load.
1265 } else {
1266 OtherAccess = I;
1267 }
1268 }
1269 }
1270 }
1271
1272 return OtherAccess;
1273}
1274
1275/// Try to locate the three instruction involved in a missed
1276/// load-elimination case that is due to an intervening store.
1277static void reportMayClobberedLoad(LoadInst *Load, Instruction *DepInst,
1278 const DominatorTree *DT,
1280 using namespace ore;
1281
1282 OptimizationRemarkMissed R(DEBUG_TYPE, "LoadClobbered", Load);
1283 R << "load of type " << NV("Type", Load->getType()) << " not eliminated"
1284 << setExtraArgs();
1285
1286 const Instruction *OtherAccess = findMayClobberedPtrAccess(Load, DT);
1287 if (OtherAccess)
1288 R << " in favor of " << NV("OtherAccess", OtherAccess);
1289
1290 R << " because it is clobbered by " << NV("ClobberedBy", DepInst);
1291
1292 ORE->emit(R);
1293}
1294
1295// Find a dominating value for Loc memory location in the extended basic block
1296// (chain of basic blocks with single predecessors) starting From instruction.
1297// Returns the value from a matching load or a simple store to the same pointer.
1299 Instruction *From, AAResults *AA) {
1300 uint32_t NumVisitedInsts = 0;
1301 BasicBlock *FromBB = From->getParent();
1302 BatchAAResults BatchAA(*AA);
1303 for (BasicBlock *BB = FromBB; BB; BB = BB->getSinglePredecessor())
1304 for (auto *Inst = BB == FromBB ? From : BB->getTerminator();
1305 Inst != nullptr; Inst = Inst->getPrevNode()) {
1306 // Stop the search if limit is reached.
1307 if (++NumVisitedInsts > MaxNumVisitedInsts)
1308 return nullptr;
1309 if (isModSet(BatchAA.getModRefInfo(Inst, Loc))) {
1310 // A simple store to the exact location can forward its value.
1311 if (auto *SI = dyn_cast<StoreInst>(Inst))
1312 if (SI->isSimple() && SI->getPointerOperand() == Loc.Ptr &&
1313 SI->getValueOperand()->getType() == LoadTy)
1314 return SI->getValueOperand();
1315 return nullptr;
1316 }
1317 if (auto *LI = dyn_cast<LoadInst>(Inst))
1318 if (LI->getPointerOperand() == Loc.Ptr && LI->getType() == LoadTy)
1319 return LI;
1320 }
1321 return nullptr;
1322}
1323
1324std::optional<AvailableValue>
1325GVNPass::AnalyzeLoadAvailability(LoadInst *Load, MemDepResult DepInfo,
1326 Value *Address) {
1327 assert(Load->isUnordered() && "rules below are incorrect for ordered access");
1328 assert(DepInfo.isLocal() && "expected a local dependence");
1329
1330 Instruction *DepInst = DepInfo.getInst();
1331
1332 const DataLayout &DL = Load->getDataLayout();
1333 if (DepInfo.isClobber()) {
1334 // If the dependence is to a store that writes to a superset of the bits
1335 // read by the load, we can extract the bits we need for the load from the
1336 // stored value.
1337 if (StoreInst *DepSI = dyn_cast<StoreInst>(DepInst)) {
1338 // Can't forward from non-atomic to atomic without violating memory model.
1339 if (Address && Load->isAtomic() <= DepSI->isAtomic()) {
1340 int Offset =
1341 analyzeLoadFromClobberingStore(Load->getType(), Address, DepSI, DL);
1342 if (Offset != -1)
1343 return AvailableValue::get(DepSI->getValueOperand(), Offset);
1344 }
1345 }
1346
1347 // Check to see if we have something like this:
1348 // load i32* P
1349 // load i8* (P+1)
1350 // if we have this, replace the later with an extraction from the former.
1351 if (LoadInst *DepLoad = dyn_cast<LoadInst>(DepInst)) {
1352 // If this is a clobber and L is the first instruction in its block, then
1353 // we have the first instruction in the entry block.
1354 // Can't forward from non-atomic to atomic without violating memory model.
1355 if (DepLoad != Load && Address &&
1356 Load->isAtomic() <= DepLoad->isAtomic()) {
1357 Type *LoadType = Load->getType();
1358 int Offset = -1;
1359
1360 // If MD reported clobber, check it was nested.
1361 if (DepInfo.isClobber() &&
1362 canCoerceMustAliasedValueToLoad(DepLoad, LoadType,
1363 DepLoad->getFunction())) {
1364 const auto ClobberOff = MD->getClobberOffset(DepLoad);
1365 // GVN has no deal with a negative offset.
1366 Offset = (ClobberOff == std::nullopt || *ClobberOff < 0)
1367 ? -1
1368 : *ClobberOff;
1369 }
1370 if (Offset == -1)
1371 Offset =
1372 analyzeLoadFromClobberingLoad(LoadType, Address, DepLoad, DL);
1373 if (Offset != -1)
1374 return AvailableValue::getLoad(DepLoad, Offset);
1375 }
1376 }
1377
1378 // If the clobbering value is a memset/memcpy/memmove, see if we can
1379 // forward a value on from it.
1380 if (MemIntrinsic *DepMI = dyn_cast<MemIntrinsic>(DepInst)) {
1381 if (Address && !Load->isAtomic()) {
1383 DepMI, DL);
1384 if (Offset != -1)
1385 return AvailableValue::getMI(DepMI, Offset);
1386 }
1387 }
1388
1389 // Nothing known about this clobber, have to be conservative.
1390 LLVM_DEBUG(
1391 // fast print dep, using operator<< on instruction is too slow.
1392 dbgs() << "GVN: load "; Load->printAsOperand(dbgs());
1393 dbgs() << " is clobbered by " << *DepInst << '\n';);
1394 if (ORE->allowExtraAnalysis(DEBUG_TYPE))
1395 reportMayClobberedLoad(Load, DepInst, DT, ORE);
1396
1397 return std::nullopt;
1398 }
1399 assert(DepInfo.isDef() && "follows from above");
1400
1401 // Loading the alloca -> undef.
1402 // Loading immediately after lifetime begin -> undef.
1403 if (isa<AllocaInst>(DepInst) || isLifetimeStart(DepInst))
1404 return AvailableValue::get(UndefValue::get(Load->getType()));
1405
1406 if (Constant *InitVal =
1407 getInitialValueOfAllocation(DepInst, TLI, Load->getType()))
1408 return AvailableValue::get(InitVal);
1409
1410 if (StoreInst *S = dyn_cast<StoreInst>(DepInst)) {
1411 // Reject loads and stores that are to the same address but are of
1412 // different types if we have to. If the stored value is convertable to
1413 // the loaded value, we can reuse it.
1414 if (!canCoerceMustAliasedValueToLoad(S->getValueOperand(), Load->getType(),
1415 S->getFunction()))
1416 return std::nullopt;
1417
1418 // Can't forward from non-atomic to atomic without violating memory model.
1419 if (S->isAtomic() < Load->isAtomic())
1420 return std::nullopt;
1421
1422 return AvailableValue::get(S->getValueOperand());
1423 }
1424
1425 if (LoadInst *LD = dyn_cast<LoadInst>(DepInst)) {
1426 // If the types mismatch and we can't handle it, reject reuse of the load.
1427 // If the stored value is larger or equal to the loaded value, we can reuse
1428 // it.
1429 if (!canCoerceMustAliasedValueToLoad(LD, Load->getType(),
1430 LD->getFunction()))
1431 return std::nullopt;
1432
1433 // Can't forward from non-atomic to atomic without violating memory model.
1434 if (LD->isAtomic() < Load->isAtomic())
1435 return std::nullopt;
1436
1437 return AvailableValue::getLoad(LD);
1438 }
1439
1440 // Check if load with Addr dependent from select can be converted to select
1441 // between load values. There must be no instructions between the found
1442 // loads and DepInst that may clobber the loads.
1443 if (auto *Sel = dyn_cast<SelectInst>(DepInst)) {
1444 assert(Sel->getType() == Load->getPointerOperandType());
1445 auto Loc = MemoryLocation::get(Load);
1446 Value *V1 =
1447 findDominatingValue(Loc.getWithNewPtr(Sel->getTrueValue()),
1448 Load->getType(), DepInst, getAliasAnalysis());
1449 if (!V1)
1450 return std::nullopt;
1451 Value *V2 =
1452 findDominatingValue(Loc.getWithNewPtr(Sel->getFalseValue()),
1453 Load->getType(), DepInst, getAliasAnalysis());
1454 if (!V2)
1455 return std::nullopt;
1456 return AvailableValue::getSelect(Sel, V1, V2);
1457 }
1458
1459 // Unknown def - must be conservative.
1460 LLVM_DEBUG(
1461 // fast print dep, using operator<< on instruction is too slow.
1462 dbgs() << "GVN: load "; Load->printAsOperand(dbgs());
1463 dbgs() << " has unknown def " << *DepInst << '\n';);
1464 return std::nullopt;
1465}
1466
1467void GVNPass::AnalyzeLoadAvailability(LoadInst *Load, LoadDepVect &Deps,
1468 AvailValInBlkVect &ValuesPerBlock,
1469 UnavailBlkVect &UnavailableBlocks) {
1470 // Filter out useless results (non-locals, etc). Keep track of the blocks
1471 // where we have a value available in repl, also keep track of whether we see
1472 // dependencies that produce an unknown value for the load (such as a call
1473 // that could potentially clobber the load).
1474 for (const auto &Dep : Deps) {
1475 BasicBlock *DepBB = Dep.getBB();
1476 MemDepResult DepInfo = Dep.getResult();
1477
1478 if (DeadBlocks.count(DepBB)) {
1479 // Dead dependent mem-op disguise as a load evaluating the same value
1480 // as the load in question.
1481 ValuesPerBlock.push_back(AvailableValueInBlock::getUndef(DepBB));
1482 continue;
1483 }
1484
1485 if (!DepInfo.isLocal()) {
1486 UnavailableBlocks.push_back(DepBB);
1487 continue;
1488 }
1489
1490 // The address being loaded in this non-local block may not be the same as
1491 // the pointer operand of the load if PHI translation occurs. Make sure
1492 // to consider the right address.
1493 if (auto AV = AnalyzeLoadAvailability(Load, DepInfo, Dep.getAddress())) {
1494 // subtlety: because we know this was a non-local dependency, we know
1495 // it's safe to materialize anywhere between the instruction within
1496 // DepInfo and the end of it's block.
1497 ValuesPerBlock.push_back(
1498 AvailableValueInBlock::get(DepBB, std::move(*AV)));
1499 } else {
1500 UnavailableBlocks.push_back(DepBB);
1501 }
1502 }
1503
1504 assert(Deps.size() == ValuesPerBlock.size() + UnavailableBlocks.size() &&
1505 "post condition violation");
1506}
1507
1508/// Given the following code, v1 is partially available on some edges, but not
1509/// available on the edge from PredBB. This function tries to find if there is
1510/// another identical load in the other successor of PredBB.
1511///
1512/// v0 = load %addr
1513/// br %LoadBB
1514///
1515/// LoadBB:
1516/// v1 = load %addr
1517/// ...
1518///
1519/// PredBB:
1520/// ...
1521/// br %cond, label %LoadBB, label %SuccBB
1522///
1523/// SuccBB:
1524/// v2 = load %addr
1525/// ...
1526///
1527LoadInst *GVNPass::findLoadToHoistIntoPred(BasicBlock *Pred, BasicBlock *LoadBB,
1528 LoadInst *Load) {
1529 // For simplicity we handle a Pred has 2 successors only.
1530 auto *Term = Pred->getTerminator();
1531 if (Term->getNumSuccessors() != 2 || Term->isSpecialTerminator())
1532 return nullptr;
1533 auto *SuccBB = Term->getSuccessor(0);
1534 if (SuccBB == LoadBB)
1535 SuccBB = Term->getSuccessor(1);
1536 if (!SuccBB->getSinglePredecessor())
1537 return nullptr;
1538
1539 unsigned int NumInsts = MaxNumInsnsPerBlock;
1540 for (Instruction &Inst : *SuccBB) {
1541 if (Inst.isDebugOrPseudoInst())
1542 continue;
1543 if (--NumInsts == 0)
1544 return nullptr;
1545
1546 if (!Inst.isIdenticalTo(Load))
1547 continue;
1548
1549 MemDepResult Dep = MD->getDependency(&Inst);
1550 // If an identical load doesn't depends on any local instructions, it can
1551 // be safely moved to PredBB.
1552 // Also check for the implicit control flow instructions. See the comments
1553 // in PerformLoadPRE for details.
1554 if (Dep.isNonLocal() && !ICF->isDominatedByICFIFromSameBlock(&Inst))
1555 return cast<LoadInst>(&Inst);
1556
1557 // Otherwise there is something in the same BB clobbers the memory, we can't
1558 // move this and later load to PredBB.
1559 return nullptr;
1560 }
1561
1562 return nullptr;
1563}
1564
1565void GVNPass::eliminatePartiallyRedundantLoad(
1566 LoadInst *Load, AvailValInBlkVect &ValuesPerBlock,
1567 MapVector<BasicBlock *, Value *> &AvailableLoads,
1568 MapVector<BasicBlock *, LoadInst *> *CriticalEdgePredAndLoad) {
1569 for (const auto &AvailableLoad : AvailableLoads) {
1570 BasicBlock *UnavailableBlock = AvailableLoad.first;
1571 Value *LoadPtr = AvailableLoad.second;
1572
1573 auto *NewLoad = new LoadInst(
1574 Load->getType(), LoadPtr, Load->getName() + ".pre", Load->isVolatile(),
1575 Load->getAlign(), Load->getOrdering(), Load->getSyncScopeID(),
1576 UnavailableBlock->getTerminator()->getIterator());
1577 NewLoad->setDebugLoc(Load->getDebugLoc());
1578 if (MSSAU) {
1579 auto *NewAccess = MSSAU->createMemoryAccessInBB(
1580 NewLoad, nullptr, NewLoad->getParent(), MemorySSA::BeforeTerminator);
1581 if (auto *NewDef = dyn_cast<MemoryDef>(NewAccess))
1582 MSSAU->insertDef(NewDef, /*RenameUses=*/true);
1583 else
1584 MSSAU->insertUse(cast<MemoryUse>(NewAccess), /*RenameUses=*/true);
1585 }
1586
1587 // Transfer the old load's AA tags to the new load.
1588 AAMDNodes Tags = Load->getAAMetadata();
1589 if (Tags)
1590 NewLoad->setAAMetadata(Tags);
1591
1592 if (auto *MD = Load->getMetadata(LLVMContext::MD_invariant_load))
1593 NewLoad->setMetadata(LLVMContext::MD_invariant_load, MD);
1594 if (auto *InvGroupMD = Load->getMetadata(LLVMContext::MD_invariant_group))
1595 NewLoad->setMetadata(LLVMContext::MD_invariant_group, InvGroupMD);
1596 if (auto *RangeMD = Load->getMetadata(LLVMContext::MD_range))
1597 NewLoad->setMetadata(LLVMContext::MD_range, RangeMD);
1598 if (auto *NoFPClassMD = Load->getMetadata(LLVMContext::MD_nofpclass))
1599 NewLoad->setMetadata(LLVMContext::MD_nofpclass, NoFPClassMD);
1600
1601 if (auto *AccessMD = Load->getMetadata(LLVMContext::MD_access_group))
1602 if (LI->getLoopFor(Load->getParent()) == LI->getLoopFor(UnavailableBlock))
1603 NewLoad->setMetadata(LLVMContext::MD_access_group, AccessMD);
1604
1605 // We do not propagate the old load's debug location, because the new
1606 // load now lives in a different BB, and we want to avoid a jumpy line
1607 // table.
1608 // FIXME: How do we retain source locations without causing poor debugging
1609 // behavior?
1610
1611 // Add the newly created load.
1612 ValuesPerBlock.push_back(
1613 AvailableValueInBlock::get(UnavailableBlock, NewLoad));
1614 MD->invalidateCachedPointerInfo(LoadPtr);
1615 LLVM_DEBUG(dbgs() << "GVN INSERTED " << *NewLoad << '\n');
1616
1617 // For PredBB in CriticalEdgePredAndLoad we need to replace the uses of old
1618 // load instruction with the new created load instruction.
1619 if (CriticalEdgePredAndLoad) {
1620 auto It = CriticalEdgePredAndLoad->find(UnavailableBlock);
1621 if (It != CriticalEdgePredAndLoad->end()) {
1622 ++NumPRELoadMoved2CEPred;
1623 ICF->insertInstructionTo(NewLoad, UnavailableBlock);
1624 LoadInst *OldLoad = It->second;
1625 combineMetadataForCSE(NewLoad, OldLoad, false);
1626 OldLoad->replaceAllUsesWith(NewLoad);
1627 replaceValuesPerBlockEntry(ValuesPerBlock, OldLoad, NewLoad);
1628 if (uint32_t ValNo = VN.lookup(OldLoad, false))
1629 LeaderTable.erase(ValNo, OldLoad, OldLoad->getParent());
1630 removeInstruction(OldLoad);
1631 }
1632 }
1633 }
1634
1635 // Perform PHI construction.
1636 Value *V = ConstructSSAForLoadSet(Load, ValuesPerBlock, *this);
1637 // ConstructSSAForLoadSet is responsible for combining metadata.
1638 ICF->removeUsersOf(Load);
1639 Load->replaceAllUsesWith(V);
1640 if (isa<PHINode>(V))
1641 V->takeName(Load);
1642 if (Instruction *I = dyn_cast<Instruction>(V))
1643 I->setDebugLoc(Load->getDebugLoc());
1644 if (V->getType()->isPtrOrPtrVectorTy())
1645 MD->invalidateCachedPointerInfo(V);
1646 ORE->emit([&]() {
1647 return OptimizationRemark(DEBUG_TYPE, "LoadPRE", Load)
1648 << "load eliminated by PRE";
1649 });
1651}
1652
1653bool GVNPass::PerformLoadPRE(LoadInst *Load, AvailValInBlkVect &ValuesPerBlock,
1654 UnavailBlkVect &UnavailableBlocks) {
1655 // Okay, we have *some* definitions of the value. This means that the value
1656 // is available in some of our (transitive) predecessors. Lets think about
1657 // doing PRE of this load. This will involve inserting a new load into the
1658 // predecessor when it's not available. We could do this in general, but
1659 // prefer to not increase code size. As such, we only do this when we know
1660 // that we only have to insert *one* load (which means we're basically moving
1661 // the load, not inserting a new one).
1662
1663 SmallPtrSet<BasicBlock *, 4> Blockers(llvm::from_range, UnavailableBlocks);
1664
1665 // Let's find the first basic block with more than one predecessor. Walk
1666 // backwards through predecessors if needed.
1667 BasicBlock *LoadBB = Load->getParent();
1668 BasicBlock *TmpBB = LoadBB;
1669
1670 // Check that there is no implicit control flow instructions above our load in
1671 // its block. If there is an instruction that doesn't always pass the
1672 // execution to the following instruction, then moving through it may become
1673 // invalid. For example:
1674 //
1675 // int arr[LEN];
1676 // int index = ???;
1677 // ...
1678 // guard(0 <= index && index < LEN);
1679 // use(arr[index]);
1680 //
1681 // It is illegal to move the array access to any point above the guard,
1682 // because if the index is out of bounds we should deoptimize rather than
1683 // access the array.
1684 // Check that there is no guard in this block above our instruction.
1685 bool MustEnsureSafetyOfSpeculativeExecution =
1686 ICF->isDominatedByICFIFromSameBlock(Load);
1687
1688 while (TmpBB->getSinglePredecessor()) {
1689 TmpBB = TmpBB->getSinglePredecessor();
1690 if (TmpBB == LoadBB) // Infinite (unreachable) loop.
1691 return false;
1692 if (Blockers.count(TmpBB))
1693 return false;
1694
1695 // If any of these blocks has more than one successor (i.e. if the edge we
1696 // just traversed was critical), then there are other paths through this
1697 // block along which the load may not be anticipated. Hoisting the load
1698 // above this block would be adding the load to execution paths along
1699 // which it was not previously executed.
1700 if (TmpBB->getTerminator()->getNumSuccessors() != 1)
1701 return false;
1702
1703 // Check that there is no implicit control flow in a block above.
1704 MustEnsureSafetyOfSpeculativeExecution =
1705 MustEnsureSafetyOfSpeculativeExecution || ICF->hasICF(TmpBB);
1706 }
1707
1708 assert(TmpBB);
1709 LoadBB = TmpBB;
1710
1711 // Check to see how many predecessors have the loaded value fully
1712 // available.
1713 MapVector<BasicBlock *, Value *> PredLoads;
1714 DenseMap<BasicBlock *, AvailabilityState> FullyAvailableBlocks;
1715 for (const AvailableValueInBlock &AV : ValuesPerBlock)
1716 FullyAvailableBlocks[AV.BB] = AvailabilityState::Available;
1717 for (BasicBlock *UnavailableBB : UnavailableBlocks)
1718 FullyAvailableBlocks[UnavailableBB] = AvailabilityState::Unavailable;
1719
1720 // The edge from Pred to LoadBB is a critical edge will be splitted.
1721 SmallVector<BasicBlock *, 4> CriticalEdgePredSplit;
1722 // The edge from Pred to LoadBB is a critical edge, another successor of Pred
1723 // contains a load can be moved to Pred. This data structure maps the Pred to
1724 // the movable load.
1725 MapVector<BasicBlock *, LoadInst *> CriticalEdgePredAndLoad;
1726 for (BasicBlock *Pred : predecessors(LoadBB)) {
1727 // If any predecessor block is an EH pad that does not allow non-PHI
1728 // instructions before the terminator, we can't PRE the load.
1729 if (Pred->getTerminator()->isEHPad()) {
1730 LLVM_DEBUG(
1731 dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD PREDECESSOR '"
1732 << Pred->getName() << "': " << *Load << '\n');
1733 return false;
1734 }
1735
1736 if (IsValueFullyAvailableInBlock(Pred, FullyAvailableBlocks)) {
1737 continue;
1738 }
1739
1740 if (Pred->getTerminator()->getNumSuccessors() != 1) {
1741 if (isa<IndirectBrInst>(Pred->getTerminator())) {
1742 LLVM_DEBUG(
1743 dbgs() << "COULD NOT PRE LOAD BECAUSE OF INDBR CRITICAL EDGE '"
1744 << Pred->getName() << "': " << *Load << '\n');
1745 return false;
1746 }
1747
1748 if (LoadBB->isEHPad()) {
1749 LLVM_DEBUG(
1750 dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD CRITICAL EDGE '"
1751 << Pred->getName() << "': " << *Load << '\n');
1752 return false;
1753 }
1754
1755 // Do not split backedge as it will break the canonical loop form.
1757 if (DT->dominates(LoadBB, Pred)) {
1758 LLVM_DEBUG(
1759 dbgs()
1760 << "COULD NOT PRE LOAD BECAUSE OF A BACKEDGE CRITICAL EDGE '"
1761 << Pred->getName() << "': " << *Load << '\n');
1762 return false;
1763 }
1764
1765 if (LoadInst *LI = findLoadToHoistIntoPred(Pred, LoadBB, Load))
1766 CriticalEdgePredAndLoad[Pred] = LI;
1767 else
1768 CriticalEdgePredSplit.push_back(Pred);
1769 } else {
1770 // Only add the predecessors that will not be split for now.
1771 PredLoads[Pred] = nullptr;
1772 }
1773 }
1774
1775 // Decide whether PRE is profitable for this load.
1776 unsigned NumInsertPreds = PredLoads.size() + CriticalEdgePredSplit.size();
1777 unsigned NumUnavailablePreds = NumInsertPreds +
1778 CriticalEdgePredAndLoad.size();
1779 assert(NumUnavailablePreds != 0 &&
1780 "Fully available value should already be eliminated!");
1781 (void)NumUnavailablePreds;
1782
1783 // If we need to insert new load in multiple predecessors, reject it.
1784 // FIXME: If we could restructure the CFG, we could make a common pred with
1785 // all the preds that don't have an available Load and insert a new load into
1786 // that one block.
1787 if (NumInsertPreds > 1)
1788 return false;
1789
1790 // Now we know where we will insert load. We must ensure that it is safe
1791 // to speculatively execute the load at that points.
1792 if (MustEnsureSafetyOfSpeculativeExecution) {
1793 if (CriticalEdgePredSplit.size())
1794 if (!isSafeToSpeculativelyExecute(Load, &*LoadBB->getFirstNonPHIIt(), AC,
1795 DT))
1796 return false;
1797 for (auto &PL : PredLoads)
1798 if (!isSafeToSpeculativelyExecute(Load, PL.first->getTerminator(), AC,
1799 DT))
1800 return false;
1801 for (auto &CEP : CriticalEdgePredAndLoad)
1802 if (!isSafeToSpeculativelyExecute(Load, CEP.first->getTerminator(), AC,
1803 DT))
1804 return false;
1805 }
1806
1807 // Split critical edges, and update the unavailable predecessors accordingly.
1808 for (BasicBlock *OrigPred : CriticalEdgePredSplit) {
1809 BasicBlock *NewPred = splitCriticalEdges(OrigPred, LoadBB);
1810 assert(!PredLoads.count(OrigPred) && "Split edges shouldn't be in map!");
1811 PredLoads[NewPred] = nullptr;
1812 LLVM_DEBUG(dbgs() << "Split critical edge " << OrigPred->getName() << "->"
1813 << LoadBB->getName() << '\n');
1814 }
1815
1816 for (auto &CEP : CriticalEdgePredAndLoad)
1817 PredLoads[CEP.first] = nullptr;
1818
1819 // Check if the load can safely be moved to all the unavailable predecessors.
1820 bool CanDoPRE = true;
1821 const DataLayout &DL = Load->getDataLayout();
1822 SmallVector<Instruction*, 8> NewInsts;
1823 for (auto &PredLoad : PredLoads) {
1824 BasicBlock *UnavailablePred = PredLoad.first;
1825
1826 // Do PHI translation to get its value in the predecessor if necessary. The
1827 // returned pointer (if non-null) is guaranteed to dominate UnavailablePred.
1828 // We do the translation for each edge we skipped by going from Load's block
1829 // to LoadBB, otherwise we might miss pieces needing translation.
1830
1831 // If all preds have a single successor, then we know it is safe to insert
1832 // the load on the pred (?!?), so we can insert code to materialize the
1833 // pointer if it is not available.
1834 Value *LoadPtr = Load->getPointerOperand();
1835 BasicBlock *Cur = Load->getParent();
1836 while (Cur != LoadBB) {
1837 PHITransAddr Address(LoadPtr, DL, AC);
1838 LoadPtr = Address.translateWithInsertion(Cur, Cur->getSinglePredecessor(),
1839 *DT, NewInsts);
1840 if (!LoadPtr) {
1841 CanDoPRE = false;
1842 break;
1843 }
1844 Cur = Cur->getSinglePredecessor();
1845 }
1846
1847 if (LoadPtr) {
1848 PHITransAddr Address(LoadPtr, DL, AC);
1849 LoadPtr = Address.translateWithInsertion(LoadBB, UnavailablePred, *DT,
1850 NewInsts);
1851 }
1852 // If we couldn't find or insert a computation of this phi translated value,
1853 // we fail PRE.
1854 if (!LoadPtr) {
1855 LLVM_DEBUG(dbgs() << "COULDN'T INSERT PHI TRANSLATED VALUE OF: "
1856 << *Load->getPointerOperand() << "\n");
1857 CanDoPRE = false;
1858 break;
1859 }
1860
1861 PredLoad.second = LoadPtr;
1862 }
1863
1864 if (!CanDoPRE) {
1865 while (!NewInsts.empty()) {
1866 // Erase instructions generated by the failed PHI translation before
1867 // trying to number them. PHI translation might insert instructions
1868 // in basic blocks other than the current one, and we delete them
1869 // directly, as salvageAndRemoveInstruction only allows removing from the
1870 // current basic block.
1871 NewInsts.pop_back_val()->eraseFromParent();
1872 }
1873 // HINT: Don't revert the edge-splitting as following transformation may
1874 // also need to split these critical edges.
1875 return !CriticalEdgePredSplit.empty();
1876 }
1877
1878 // Okay, we can eliminate this load by inserting a reload in the predecessor
1879 // and using PHI construction to get the value in the other predecessors, do
1880 // it.
1881 LLVM_DEBUG(dbgs() << "GVN REMOVING PRE LOAD: " << *Load << '\n');
1882 LLVM_DEBUG(if (!NewInsts.empty()) dbgs() << "INSERTED " << NewInsts.size()
1883 << " INSTS: " << *NewInsts.back()
1884 << '\n');
1885
1886 // Assign value numbers to the new instructions.
1887 for (Instruction *I : NewInsts) {
1888 // Instructions that have been inserted in predecessor(s) to materialize
1889 // the load address do not retain their original debug locations. Doing
1890 // so could lead to confusing (but correct) source attributions.
1891 I->updateLocationAfterHoist();
1892
1893 // FIXME: We really _ought_ to insert these value numbers into their
1894 // parent's availability map. However, in doing so, we risk getting into
1895 // ordering issues. If a block hasn't been processed yet, we would be
1896 // marking a value as AVAIL-IN, which isn't what we intend.
1897 VN.lookupOrAdd(I);
1898 }
1899
1900 eliminatePartiallyRedundantLoad(Load, ValuesPerBlock, PredLoads,
1901 &CriticalEdgePredAndLoad);
1902 ++NumPRELoad;
1903 return true;
1904}
1905
1906bool GVNPass::performLoopLoadPRE(LoadInst *Load,
1907 AvailValInBlkVect &ValuesPerBlock,
1908 UnavailBlkVect &UnavailableBlocks) {
1909 const Loop *L = LI->getLoopFor(Load->getParent());
1910 // TODO: Generalize to other loop blocks that dominate the latch.
1911 if (!L || L->getHeader() != Load->getParent())
1912 return false;
1913
1914 BasicBlock *Preheader = L->getLoopPreheader();
1915 BasicBlock *Latch = L->getLoopLatch();
1916 if (!Preheader || !Latch)
1917 return false;
1918
1919 Value *LoadPtr = Load->getPointerOperand();
1920 // Must be available in preheader.
1921 if (!L->isLoopInvariant(LoadPtr))
1922 return false;
1923
1924 // We plan to hoist the load to preheader without introducing a new fault.
1925 // In order to do it, we need to prove that we cannot side-exit the loop
1926 // once loop header is first entered before execution of the load.
1927 if (ICF->isDominatedByICFIFromSameBlock(Load))
1928 return false;
1929
1930 BasicBlock *LoopBlock = nullptr;
1931 for (auto *Blocker : UnavailableBlocks) {
1932 // Blockers from outside the loop are handled in preheader.
1933 if (!L->contains(Blocker))
1934 continue;
1935
1936 // Only allow one loop block. Loop header is not less frequently executed
1937 // than each loop block, and likely it is much more frequently executed. But
1938 // in case of multiple loop blocks, we need extra information (such as block
1939 // frequency info) to understand whether it is profitable to PRE into
1940 // multiple loop blocks.
1941 if (LoopBlock)
1942 return false;
1943
1944 // Do not sink into inner loops. This may be non-profitable.
1945 if (L != LI->getLoopFor(Blocker))
1946 return false;
1947
1948 // Blocks that dominate the latch execute on every single iteration, maybe
1949 // except the last one. So PREing into these blocks doesn't make much sense
1950 // in most cases. But the blocks that do not necessarily execute on each
1951 // iteration are sometimes much colder than the header, and this is when
1952 // PRE is potentially profitable.
1953 if (DT->dominates(Blocker, Latch))
1954 return false;
1955
1956 // Make sure that the terminator itself doesn't clobber.
1957 if (Blocker->getTerminator()->mayWriteToMemory())
1958 return false;
1959
1960 LoopBlock = Blocker;
1961 }
1962
1963 if (!LoopBlock)
1964 return false;
1965
1966 // Make sure the memory at this pointer cannot be freed, therefore we can
1967 // safely reload from it after clobber.
1968 if (LoadPtr->canBeFreed())
1969 return false;
1970
1971 // TODO: Support critical edge splitting if blocker has more than 1 successor.
1972 MapVector<BasicBlock *, Value *> AvailableLoads;
1973 AvailableLoads[LoopBlock] = LoadPtr;
1974 AvailableLoads[Preheader] = LoadPtr;
1975
1976 LLVM_DEBUG(dbgs() << "GVN REMOVING PRE LOOP LOAD: " << *Load << '\n');
1977 eliminatePartiallyRedundantLoad(Load, ValuesPerBlock, AvailableLoads,
1978 /*CriticalEdgePredAndLoad*/ nullptr);
1979 ++NumPRELoopLoad;
1980 return true;
1981}
1982
1985 using namespace ore;
1986
1987 ORE->emit([&]() {
1988 return OptimizationRemark(DEBUG_TYPE, "LoadElim", Load)
1989 << "load of type " << NV("Type", Load->getType()) << " eliminated"
1990 << setExtraArgs() << " in favor of "
1991 << NV("InfavorOfValue", AvailableValue);
1992 });
1993}
1994
1995/// Attempt to eliminate a load whose dependencies are
1996/// non-local by performing PHI construction.
1997bool GVNPass::processNonLocalLoad(LoadInst *Load) {
1998 // Non-local speculations are not allowed under asan.
1999 if (Load->getParent()->getParent()->hasFnAttribute(
2000 Attribute::SanitizeAddress) ||
2001 Load->getParent()->getParent()->hasFnAttribute(
2002 Attribute::SanitizeHWAddress))
2003 return false;
2004
2005 // Step 1: Find the non-local dependencies of the load.
2006 LoadDepVect Deps;
2007 MD->getNonLocalPointerDependency(Load, Deps);
2008
2009 // If we had to process more than one hundred blocks to find the
2010 // dependencies, this load isn't worth worrying about. Optimizing
2011 // it will be too expensive.
2012 unsigned NumDeps = Deps.size();
2013 if (NumDeps > MaxNumDeps)
2014 return false;
2015
2016 // If we had a phi translation failure, we'll have a single entry which is a
2017 // clobber in the current block. Reject this early.
2018 if (NumDeps == 1 &&
2019 !Deps[0].getResult().isDef() && !Deps[0].getResult().isClobber()) {
2020 LLVM_DEBUG(dbgs() << "GVN: non-local load "; Load->printAsOperand(dbgs());
2021 dbgs() << " has unknown dependencies\n";);
2022 return false;
2023 }
2024
2025 bool Changed = false;
2026 // This is a limited form of scalar PRE for load indices. If this load follows
2027 // a GEP, see if we can PRE the indices before analyzing.
2028 if (isScalarPREEnabled()) {
2029 if (GetElementPtrInst *GEP =
2030 dyn_cast<GetElementPtrInst>(Load->getOperand(0))) {
2031 for (Use &U : GEP->indices())
2032 if (Instruction *I = dyn_cast<Instruction>(U.get()))
2033 Changed |= performScalarPRE(I);
2034 }
2035 }
2036
2037 // Step 2: Analyze the availability of the load.
2038 AvailValInBlkVect ValuesPerBlock;
2039 UnavailBlkVect UnavailableBlocks;
2040 AnalyzeLoadAvailability(Load, Deps, ValuesPerBlock, UnavailableBlocks);
2041
2042 // If we have no predecessors that produce a known value for this load, exit
2043 // early.
2044 if (ValuesPerBlock.empty())
2045 return Changed;
2046
2047 // Step 3: Eliminate fully redundancy.
2048 //
2049 // If all of the instructions we depend on produce a known value for this
2050 // load, then it is fully redundant and we can use PHI insertion to compute
2051 // its value. Insert PHIs and remove the fully redundant value now.
2052 if (UnavailableBlocks.empty()) {
2053 LLVM_DEBUG(dbgs() << "GVN REMOVING NONLOCAL LOAD: " << *Load << '\n');
2054
2055 // Perform PHI construction.
2056 Value *V = ConstructSSAForLoadSet(Load, ValuesPerBlock, *this);
2057 // ConstructSSAForLoadSet is responsible for combining metadata.
2058 ICF->removeUsersOf(Load);
2059 Load->replaceAllUsesWith(V);
2060
2061 if (isa<PHINode>(V))
2062 V->takeName(Load);
2063 if (Instruction *I = dyn_cast<Instruction>(V))
2064 // If instruction I has debug info, then we should not update it.
2065 // Also, if I has a null DebugLoc, then it is still potentially incorrect
2066 // to propagate Load's DebugLoc because Load may not post-dominate I.
2067 if (Load->getDebugLoc() && Load->getParent() == I->getParent())
2068 I->setDebugLoc(Load->getDebugLoc());
2069 if (V->getType()->isPtrOrPtrVectorTy())
2070 MD->invalidateCachedPointerInfo(V);
2071 ++NumGVNLoad;
2072 reportLoadElim(Load, V, ORE);
2074 return true;
2075 }
2076
2077 // Step 4: Eliminate partial redundancy.
2078 if (!isLoadPREEnabled())
2079 return Changed;
2080 if (!isLoadInLoopPREEnabled() && LI->getLoopFor(Load->getParent()))
2081 return Changed;
2082
2083 if (performLoopLoadPRE(Load, ValuesPerBlock, UnavailableBlocks) ||
2084 PerformLoadPRE(Load, ValuesPerBlock, UnavailableBlocks))
2085 return true;
2086
2087 return Changed;
2088}
2089
2090bool GVNPass::processAssumeIntrinsic(AssumeInst *IntrinsicI) {
2091 Value *V = IntrinsicI->getArgOperand(0);
2092
2093 if (ConstantInt *Cond = dyn_cast<ConstantInt>(V)) {
2094 if (Cond->isZero()) {
2095 Type *Int8Ty = Type::getInt8Ty(V->getContext());
2096 Type *PtrTy = PointerType::get(V->getContext(), 0);
2097 // Insert a new store to null instruction before the load to indicate that
2098 // this code is not reachable. FIXME: We could insert unreachable
2099 // instruction directly because we can modify the CFG.
2100 auto *NewS =
2101 new StoreInst(PoisonValue::get(Int8Ty), Constant::getNullValue(PtrTy),
2102 IntrinsicI->getIterator());
2103 if (MSSAU) {
2104 const MemoryUseOrDef *FirstNonDom = nullptr;
2105 const auto *AL =
2106 MSSAU->getMemorySSA()->getBlockAccesses(IntrinsicI->getParent());
2107
2108 // If there are accesses in the current basic block, find the first one
2109 // that does not come before NewS. The new memory access is inserted
2110 // after the found access or before the terminator if no such access is
2111 // found.
2112 if (AL) {
2113 for (const auto &Acc : *AL) {
2114 if (auto *Current = dyn_cast<MemoryUseOrDef>(&Acc))
2115 if (!Current->getMemoryInst()->comesBefore(NewS)) {
2116 FirstNonDom = Current;
2117 break;
2118 }
2119 }
2120 }
2121
2122 auto *NewDef =
2123 FirstNonDom ? MSSAU->createMemoryAccessBefore(
2124 NewS, nullptr,
2125 const_cast<MemoryUseOrDef *>(FirstNonDom))
2126 : MSSAU->createMemoryAccessInBB(
2127 NewS, nullptr,
2128 NewS->getParent(), MemorySSA::BeforeTerminator);
2129
2130 MSSAU->insertDef(cast<MemoryDef>(NewDef), /*RenameUses=*/false);
2131 }
2132 }
2133 if (isAssumeWithEmptyBundle(*IntrinsicI)) {
2134 salvageAndRemoveInstruction(IntrinsicI);
2135 return true;
2136 }
2137 return false;
2138 }
2139
2140 if (isa<Constant>(V)) {
2141 // If it's not false, and constant, it must evaluate to true. This means our
2142 // assume is assume(true), and thus, pointless, and we don't want to do
2143 // anything more here.
2144 return false;
2145 }
2146
2147 Constant *True = ConstantInt::getTrue(V->getContext());
2148 return propagateEquality(V, True, IntrinsicI);
2149}
2150
2153 I->replaceAllUsesWith(Repl);
2154}
2155
2156/// Attempt to eliminate a load, first by eliminating it
2157/// locally, and then attempting non-local elimination if that fails.
2158bool GVNPass::processLoad(LoadInst *L) {
2159 if (!MD)
2160 return false;
2161
2162 // This code hasn't been audited for ordered or volatile memory access.
2163 if (!L->isUnordered())
2164 return false;
2165
2166 if (L->getType()->isTokenLikeTy())
2167 return false;
2168
2169 if (L->use_empty()) {
2171 return true;
2172 }
2173
2174 // ... to a pointer that has been loaded from before...
2175 MemDepResult Dep = MD->getDependency(L);
2176
2177 // If it is defined in another block, try harder.
2178 if (Dep.isNonLocal())
2179 return processNonLocalLoad(L);
2180
2181 // Only handle the local case below.
2182 if (!Dep.isLocal()) {
2183 // This might be a NonFuncLocal or an Unknown.
2184 LLVM_DEBUG(
2185 // fast print dep, using operator<< on instruction is too slow.
2186 dbgs() << "GVN: load "; L->printAsOperand(dbgs());
2187 dbgs() << " has unknown dependence\n";);
2188 return false;
2189 }
2190
2191 auto AV = AnalyzeLoadAvailability(L, Dep, L->getPointerOperand());
2192 if (!AV)
2193 return false;
2194
2195 Value *AvailableValue = AV->MaterializeAdjustedValue(L, L);
2196
2197 // MaterializeAdjustedValue is responsible for combining metadata.
2198 ICF->removeUsersOf(L);
2199 L->replaceAllUsesWith(AvailableValue);
2200 if (MSSAU)
2201 MSSAU->removeMemoryAccess(L);
2202 ++NumGVNLoad;
2203 reportLoadElim(L, AvailableValue, ORE);
2205 // Tell MDA to reexamine the reused pointer since we might have more
2206 // information after forwarding it.
2207 if (MD && AvailableValue->getType()->isPtrOrPtrVectorTy())
2208 MD->invalidateCachedPointerInfo(AvailableValue);
2209 return true;
2210}
2211
2212// Attempt to process masked loads which have loaded from
2213// masked stores with the same mask
2214bool GVNPass::processMaskedLoad(IntrinsicInst *I) {
2215 if (!MD)
2216 return false;
2217 MemDepResult Dep = MD->getDependency(I);
2218 Instruction *DepInst = Dep.getInst();
2219 if (!DepInst || !Dep.isLocal() || !Dep.isDef())
2220 return false;
2221
2222 Value *Mask = I->getOperand(1);
2223 Value *Passthrough = I->getOperand(2);
2224 Value *StoreVal;
2225 if (!match(DepInst,
2226 m_MaskedStore(m_Value(StoreVal), m_Value(), m_Specific(Mask))) ||
2227 StoreVal->getType() != I->getType())
2228 return false;
2229
2230 // Remove the load but generate a select for the passthrough
2231 Value *OpToForward = llvm::SelectInst::Create(Mask, StoreVal, Passthrough, "",
2232 I->getIterator());
2233
2234 ICF->removeUsersOf(I);
2235 I->replaceAllUsesWith(OpToForward);
2237 ++NumGVNLoad;
2238 return true;
2239}
2240
2241/// Return a pair the first field showing the value number of \p Exp and the
2242/// second field showing whether it is a value number newly created.
2243std::pair<uint32_t, bool>
2244GVNPass::ValueTable::assignExpNewValueNum(Expression &Exp) {
2245 uint32_t &E = ExpressionNumbering[Exp];
2246 bool CreateNewValNum = !E;
2247 if (CreateNewValNum) {
2248 Expressions.push_back(Exp);
2249 if (ExprIdx.size() < NextValueNumber + 1)
2250 ExprIdx.resize(NextValueNumber * 2);
2251 E = NextValueNumber;
2252 ExprIdx[NextValueNumber++] = NextExprNumber++;
2253 }
2254 return {E, CreateNewValNum};
2255}
2256
2257/// Return whether all the values related with the same \p num are
2258/// defined in \p BB.
2259bool GVNPass::ValueTable::areAllValsInBB(uint32_t Num, const BasicBlock *BB,
2260 GVNPass &GVN) {
2261 return all_of(
2262 GVN.LeaderTable.getLeaders(Num),
2263 [=](const LeaderMap::LeaderTableEntry &L) { return L.BB == BB; });
2264}
2265
2266/// Wrap phiTranslateImpl to provide caching functionality.
2267uint32_t GVNPass::ValueTable::phiTranslate(const BasicBlock *Pred,
2268 const BasicBlock *PhiBlock,
2269 uint32_t Num, GVNPass &GVN) {
2270 auto FindRes = PhiTranslateTable.find({Num, Pred});
2271 if (FindRes != PhiTranslateTable.end())
2272 return FindRes->second;
2273 uint32_t NewNum = phiTranslateImpl(Pred, PhiBlock, Num, GVN);
2274 PhiTranslateTable.insert({{Num, Pred}, NewNum});
2275 return NewNum;
2276}
2277
2278// Return true if the value number \p Num and NewNum have equal value.
2279// Return false if the result is unknown.
2280bool GVNPass::ValueTable::areCallValsEqual(uint32_t Num, uint32_t NewNum,
2281 const BasicBlock *Pred,
2282 const BasicBlock *PhiBlock,
2283 GVNPass &GVN) {
2284 CallInst *Call = nullptr;
2285 auto Leaders = GVN.LeaderTable.getLeaders(Num);
2286 for (const auto &Entry : Leaders) {
2287 Call = dyn_cast<CallInst>(&*Entry.Val);
2288 if (Call && Call->getParent() == PhiBlock)
2289 break;
2290 }
2291
2292 if (AA->doesNotAccessMemory(Call))
2293 return true;
2294
2295 if (!MD || !AA->onlyReadsMemory(Call))
2296 return false;
2297
2298 MemDepResult LocalDep = MD->getDependency(Call);
2299 if (!LocalDep.isNonLocal())
2300 return false;
2301
2304
2305 // Check to see if the Call has no function local clobber.
2306 for (const NonLocalDepEntry &D : Deps) {
2307 if (D.getResult().isNonFuncLocal())
2308 return true;
2309 }
2310 return false;
2311}
2312
2313/// Translate value number \p Num using phis, so that it has the values of
2314/// the phis in BB.
2315uint32_t GVNPass::ValueTable::phiTranslateImpl(const BasicBlock *Pred,
2316 const BasicBlock *PhiBlock,
2317 uint32_t Num, GVNPass &GVN) {
2318 // See if we can refine the value number by looking at the PN incoming value
2319 // for the given predecessor.
2320 if (PHINode *PN = NumberingPhi[Num]) {
2321 if (PN->getParent() != PhiBlock)
2322 return Num;
2323 for (unsigned I = 0; I != PN->getNumIncomingValues(); ++I) {
2324 if (PN->getIncomingBlock(I) != Pred)
2325 continue;
2326 if (uint32_t TransVal = lookup(PN->getIncomingValue(I), false))
2327 return TransVal;
2328 }
2329 return Num;
2330 }
2331
2332 if (BasicBlock *BB = NumberingBB[Num]) {
2333 assert(MSSA && "NumberingBB is non-empty only when using MemorySSA");
2334 // Value numbers of basic blocks are used to represent memory state in
2335 // load/store instructions and read-only function calls when said state is
2336 // set by a MemoryPhi.
2337 if (BB != PhiBlock)
2338 return Num;
2339 MemoryPhi *MPhi = MSSA->getMemoryAccess(BB);
2340 for (unsigned i = 0, N = MPhi->getNumIncomingValues(); i != N; ++i) {
2341 if (MPhi->getIncomingBlock(i) != Pred)
2342 continue;
2343 MemoryAccess *MA = MPhi->getIncomingValue(i);
2344 if (auto *PredPhi = dyn_cast<MemoryPhi>(MA))
2345 return lookupOrAdd(PredPhi->getBlock());
2346 if (MSSA->isLiveOnEntryDef(MA))
2347 return lookupOrAdd(&BB->getParent()->getEntryBlock());
2348 return lookupOrAdd(cast<MemoryUseOrDef>(MA)->getMemoryInst());
2349 }
2351 "CFG/MemorySSA mismatch: predecessor not found among incoming blocks");
2352 }
2353
2354 // If there is any value related with Num is defined in a BB other than
2355 // PhiBlock, it cannot depend on a phi in PhiBlock without going through
2356 // a backedge. We can do an early exit in that case to save compile time.
2357 if (!areAllValsInBB(Num, PhiBlock, GVN))
2358 return Num;
2359
2360 if (Num >= ExprIdx.size() || ExprIdx[Num] == 0)
2361 return Num;
2362 Expression Exp = Expressions[ExprIdx[Num]];
2363
2364 for (unsigned I = 0; I < Exp.VarArgs.size(); I++) {
2365 // For InsertValue and ExtractValue, some varargs are index numbers
2366 // instead of value numbers. Those index numbers should not be
2367 // translated.
2368 if ((I > 1 && Exp.Opcode == Instruction::InsertValue) ||
2369 (I > 0 && Exp.Opcode == Instruction::ExtractValue) ||
2370 (I > 1 && Exp.Opcode == Instruction::ShuffleVector))
2371 continue;
2372 Exp.VarArgs[I] = phiTranslate(Pred, PhiBlock, Exp.VarArgs[I], GVN);
2373 }
2374
2375 if (Exp.Commutative) {
2376 assert(Exp.VarArgs.size() >= 2 && "Unsupported commutative instruction!");
2377 if (Exp.VarArgs[0] > Exp.VarArgs[1]) {
2378 std::swap(Exp.VarArgs[0], Exp.VarArgs[1]);
2379 uint32_t Opcode = Exp.Opcode >> 8;
2380 if (Opcode == Instruction::ICmp || Opcode == Instruction::FCmp)
2381 Exp.Opcode = (Opcode << 8) |
2383 static_cast<CmpInst::Predicate>(Exp.Opcode & 255));
2384 }
2385 }
2386
2387 if (uint32_t NewNum = ExpressionNumbering[Exp]) {
2388 if (Exp.Opcode == Instruction::Call && NewNum != Num)
2389 return areCallValsEqual(Num, NewNum, Pred, PhiBlock, GVN) ? NewNum : Num;
2390 return NewNum;
2391 }
2392 return Num;
2393}
2394
2395/// Erase stale entry from phiTranslate cache so phiTranslate can be computed
2396/// again.
2397void GVNPass::ValueTable::eraseTranslateCacheEntry(
2398 uint32_t Num, const BasicBlock &CurrBlock) {
2399 for (const BasicBlock *Pred : predecessors(&CurrBlock))
2400 PhiTranslateTable.erase({Num, Pred});
2401}
2402
2403// In order to find a leader for a given value number at a
2404// specific basic block, we first obtain the list of all Values for that number,
2405// and then scan the list to find one whose block dominates the block in
2406// question. This is fast because dominator tree queries consist of only
2407// a few comparisons of DFS numbers.
2408Value *GVNPass::findLeader(const BasicBlock *BB, uint32_t Num) {
2409 auto Leaders = LeaderTable.getLeaders(Num);
2410 if (Leaders.empty())
2411 return nullptr;
2412
2413 Value *Val = nullptr;
2414 for (const auto &Entry : Leaders) {
2415 if (DT->dominates(Entry.BB, BB)) {
2416 Val = Entry.Val;
2417 if (isa<Constant>(Val))
2418 return Val;
2419 }
2420 }
2421
2422 return Val;
2423}
2424
2425/// There is an edge from 'Src' to 'Dst'. Return
2426/// true if every path from the entry block to 'Dst' passes via this edge. In
2427/// particular 'Dst' must not be reachable via another edge from 'Src'.
2429 DominatorTree *DT) {
2430 // While in theory it is interesting to consider the case in which Dst has
2431 // more than one predecessor, because Dst might be part of a loop which is
2432 // only reachable from Src, in practice it is pointless since at the time
2433 // GVN runs all such loops have preheaders, which means that Dst will have
2434 // been changed to have only one predecessor, namely Src.
2435 const BasicBlock *Pred = E.getEnd()->getSinglePredecessor();
2436 assert((!Pred || Pred == E.getStart()) &&
2437 "No edge between these basic blocks!");
2438 return Pred != nullptr;
2439}
2440
2441void GVNPass::assignBlockRPONumber(Function &F) {
2442 BlockRPONumber.clear();
2443 uint32_t NextBlockNumber = 1;
2444 ReversePostOrderTraversal<Function *> RPOT(&F);
2445 for (BasicBlock *BB : RPOT)
2446 BlockRPONumber[BB] = NextBlockNumber++;
2447 InvalidBlockRPONumbers = false;
2448}
2449
2450/// The given values are known to be equal in every use
2451/// dominated by 'Root'. Exploit this, for example by replacing 'LHS' with
2452/// 'RHS' everywhere in the scope. Returns whether a change was made.
2453/// The Root may either be a basic block edge (for conditions) or an
2454/// instruction (for assumes).
2455bool GVNPass::propagateEquality(
2456 Value *LHS, Value *RHS,
2457 const std::variant<BasicBlockEdge, Instruction *> &Root) {
2459 Worklist.push_back(std::make_pair(LHS, RHS));
2460 bool Changed = false;
2461 SmallVector<const BasicBlock *> DominatedBlocks;
2462 if (const BasicBlockEdge *Edge = std::get_if<BasicBlockEdge>(&Root)) {
2463 // For speed, compute a conservative fast approximation to
2464 // DT->dominates(Root, Root.getEnd());
2466 DominatedBlocks.push_back(Edge->getEnd());
2467 } else {
2468 Instruction *I = std::get<Instruction *>(Root);
2469 for (const auto *Node : DT->getNode(I->getParent())->children())
2470 DominatedBlocks.push_back(Node->getBlock());
2471 }
2472
2473 while (!Worklist.empty()) {
2474 std::pair<Value*, Value*> Item = Worklist.pop_back_val();
2475 LHS = Item.first; RHS = Item.second;
2476
2477 if (LHS == RHS)
2478 continue;
2479 assert(LHS->getType() == RHS->getType() && "Equality but unequal types!");
2480
2481 // Don't try to propagate equalities between constants.
2483 continue;
2484
2485 // Prefer a constant on the right-hand side, or an Argument if no constants.
2487 std::swap(LHS, RHS);
2488 assert((isa<Argument>(LHS) || isa<Instruction>(LHS)) && "Unexpected value!");
2489 const DataLayout &DL =
2491 ? cast<Argument>(LHS)->getParent()->getDataLayout()
2492 : cast<Instruction>(LHS)->getDataLayout();
2493
2494 // If there is no obvious reason to prefer the left-hand side over the
2495 // right-hand side, ensure the longest lived term is on the right-hand side,
2496 // so the shortest lived term will be replaced by the longest lived.
2497 // This tends to expose more simplifications.
2498 uint32_t LVN = VN.lookupOrAdd(LHS);
2499 if ((isa<Argument>(LHS) && isa<Argument>(RHS)) ||
2501 // Move the 'oldest' value to the right-hand side, using the value number
2502 // as a proxy for age.
2503 uint32_t RVN = VN.lookupOrAdd(RHS);
2504 if (LVN < RVN) {
2505 std::swap(LHS, RHS);
2506 LVN = RVN;
2507 }
2508 }
2509
2510 // If value numbering later sees that an instruction in the scope is equal
2511 // to 'LHS' then ensure it will be turned into 'RHS'. In order to preserve
2512 // the invariant that instructions only occur in the leader table for their
2513 // own value number (this is used by removeFromLeaderTable), do not do this
2514 // if RHS is an instruction (if an instruction in the scope is morphed into
2515 // LHS then it will be turned into RHS by the next GVN iteration anyway, so
2516 // using the leader table is about compiling faster, not optimizing better).
2517 // The leader table only tracks basic blocks, not edges. Only add to if we
2518 // have the simple case where the edge dominates the end.
2520 for (const BasicBlock *BB : DominatedBlocks)
2521 LeaderTable.insert(LVN, RHS, BB);
2522
2523 // Replace all occurrences of 'LHS' with 'RHS' everywhere in the scope. As
2524 // LHS always has at least one use that is not dominated by Root, this will
2525 // never do anything if LHS has only one use.
2526 if (!LHS->hasOneUse()) {
2527 // Create a callback that captures the DL.
2528 auto CanReplacePointersCallBack = [&DL](const Use &U, const Value *To) {
2529 return canReplacePointersInUseIfEqual(U, To, DL);
2530 };
2531 unsigned NumReplacements;
2532 if (const BasicBlockEdge *Edge = std::get_if<BasicBlockEdge>(&Root))
2533 NumReplacements = replaceDominatedUsesWithIf(
2534 LHS, RHS, *DT, *Edge, CanReplacePointersCallBack);
2535 else
2536 NumReplacements = replaceDominatedUsesWithIf(
2537 LHS, RHS, *DT, std::get<Instruction *>(Root),
2538 CanReplacePointersCallBack);
2539
2540 if (NumReplacements > 0) {
2541 Changed = true;
2542 NumGVNEqProp += NumReplacements;
2543 // Cached information for anything that uses LHS will be invalid.
2544 if (MD)
2545 MD->invalidateCachedPointerInfo(LHS);
2546 }
2547 }
2548
2549 // Now try to deduce additional equalities from this one. For example, if
2550 // the known equality was "(A != B)" == "false" then it follows that A and B
2551 // are equal in the scope. Only boolean equalities with an explicit true or
2552 // false RHS are currently supported.
2553 if (!RHS->getType()->isIntegerTy(1))
2554 // Not a boolean equality - bail out.
2555 continue;
2556 ConstantInt *CI = dyn_cast<ConstantInt>(RHS);
2557 if (!CI)
2558 // RHS neither 'true' nor 'false' - bail out.
2559 continue;
2560 // Whether RHS equals 'true'. Otherwise it equals 'false'.
2561 bool IsKnownTrue = CI->isMinusOne();
2562 bool IsKnownFalse = !IsKnownTrue;
2563
2564 // If "A && B" is known true then both A and B are known true. If "A || B"
2565 // is known false then both A and B are known false.
2566 Value *A, *B;
2567 if ((IsKnownTrue && match(LHS, m_LogicalAnd(m_Value(A), m_Value(B)))) ||
2568 (IsKnownFalse && match(LHS, m_LogicalOr(m_Value(A), m_Value(B))))) {
2569 Worklist.push_back(std::make_pair(A, RHS));
2570 Worklist.push_back(std::make_pair(B, RHS));
2571 continue;
2572 }
2573
2574 // If we are propagating an equality like "(A == B)" == "true" then also
2575 // propagate the equality A == B. When propagating a comparison such as
2576 // "(A >= B)" == "true", replace all instances of "A < B" with "false".
2577 if (CmpInst *Cmp = dyn_cast<CmpInst>(LHS)) {
2578 Value *Op0 = Cmp->getOperand(0), *Op1 = Cmp->getOperand(1);
2579
2580 // If "A == B" is known true, or "A != B" is known false, then replace
2581 // A with B everywhere in the scope. For floating point operations, we
2582 // have to be careful since equality does not always imply equivalance.
2583 if (Cmp->isEquivalence(IsKnownFalse))
2584 Worklist.push_back(std::make_pair(Op0, Op1));
2585
2586 // If "A >= B" is known true, replace "A < B" with false everywhere.
2587 CmpInst::Predicate NotPred = Cmp->getInversePredicate();
2588 Constant *NotVal = ConstantInt::get(Cmp->getType(), IsKnownFalse);
2589 // Since we don't have the instruction "A < B" immediately to hand, work
2590 // out the value number that it would have and use that to find an
2591 // appropriate instruction (if any).
2592 uint32_t NextNum = VN.getNextUnusedValueNumber();
2593 uint32_t Num = VN.lookupOrAddCmp(Cmp->getOpcode(), NotPred, Op0, Op1);
2594 // If the number we were assigned was brand new then there is no point in
2595 // looking for an instruction realizing it: there cannot be one!
2596 if (Num < NextNum) {
2597 for (const auto &Entry : LeaderTable.getLeaders(Num)) {
2598 // Only look at leaders that either dominate the start of the edge,
2599 // or are dominated by the end. This check is not necessary for
2600 // correctness, it only discards cases for which the following
2601 // use replacement will not work anyway.
2602 if (const BasicBlockEdge *Edge = std::get_if<BasicBlockEdge>(&Root)) {
2603 if (!DT->dominates(Entry.BB, Edge->getStart()) &&
2604 !DT->dominates(Edge->getEnd(), Entry.BB))
2605 continue;
2606 } else {
2607 auto *InstBB = std::get<Instruction *>(Root)->getParent();
2608 if (!DT->dominates(Entry.BB, InstBB) &&
2609 !DT->dominates(InstBB, Entry.BB))
2610 continue;
2611 }
2612
2613 Value *NotCmp = Entry.Val;
2614 if (NotCmp && isa<Instruction>(NotCmp)) {
2615 unsigned NumReplacements;
2616 if (const BasicBlockEdge *Edge = std::get_if<BasicBlockEdge>(&Root))
2617 NumReplacements =
2618 replaceDominatedUsesWith(NotCmp, NotVal, *DT, *Edge);
2619 else
2620 NumReplacements = replaceDominatedUsesWith(
2621 NotCmp, NotVal, *DT, std::get<Instruction *>(Root));
2622 Changed |= NumReplacements > 0;
2623 NumGVNEqProp += NumReplacements;
2624 // Cached information for anything that uses NotCmp will be invalid.
2625 if (MD)
2626 MD->invalidateCachedPointerInfo(NotCmp);
2627 }
2628 }
2629 }
2630 // Ensure that any instruction in scope that gets the "A < B" value number
2631 // is replaced with false.
2632 // The leader table only tracks basic blocks, not edges. Only add to if we
2633 // have the simple case where the edge dominates the end.
2634 for (const BasicBlock *BB : DominatedBlocks)
2635 LeaderTable.insert(Num, NotVal, BB);
2636
2637 continue;
2638 }
2639
2640 // Propagate equalities that results from truncation with no unsigned wrap
2641 // like (trunc nuw i64 %v to i1) == "true" or (trunc nuw i64 %v to i1) ==
2642 // "false"
2643 if (match(LHS, m_NUWTrunc(m_Value(A)))) {
2644 Worklist.emplace_back(A, ConstantInt::get(A->getType(), IsKnownTrue));
2645 continue;
2646 }
2647
2648 if (match(LHS, m_Not(m_Value(A)))) {
2649 Worklist.emplace_back(A, ConstantInt::get(A->getType(), !IsKnownTrue));
2650 continue;
2651 }
2652 }
2653
2654 return Changed;
2655}
2656
2657/// When calculating availability, handle an instruction
2658/// by inserting it into the appropriate sets.
2659bool GVNPass::processInstruction(Instruction *I) {
2660 // If the instruction can be easily simplified then do so now in preference
2661 // to value numbering it. Value numbering often exposes redundancies, for
2662 // example if it determines that %y is equal to %x then the instruction
2663 // "%z = and i32 %x, %y" becomes "%z = and i32 %x, %x" which we now simplify.
2664 const DataLayout &DL = I->getDataLayout();
2665 if (Value *V = simplifyInstruction(I, {DL, TLI, DT, AC})) {
2666 bool Changed = false;
2667 if (!I->use_empty()) {
2668 // Simplification can cause a special instruction to become not special.
2669 // For example, devirtualization to a willreturn function.
2670 ICF->removeUsersOf(I);
2671 I->replaceAllUsesWith(V);
2672 Changed = true;
2673 }
2674 if (isInstructionTriviallyDead(I, TLI)) {
2676 Changed = true;
2677 }
2678 if (Changed) {
2679 if (MD && V->getType()->isPtrOrPtrVectorTy())
2680 MD->invalidateCachedPointerInfo(V);
2681 ++NumGVNSimpl;
2682 return true;
2683 }
2684 }
2685
2686 if (auto *Assume = dyn_cast<AssumeInst>(I))
2687 return processAssumeIntrinsic(Assume);
2688
2689 if (LoadInst *Load = dyn_cast<LoadInst>(I)) {
2690 if (processLoad(Load))
2691 return true;
2692
2693 unsigned Num = VN.lookupOrAdd(Load);
2694 LeaderTable.insert(Num, Load, Load->getParent());
2695 return false;
2696 }
2697
2699 processMaskedLoad(cast<IntrinsicInst>(I)))
2700 return true;
2701
2702 // For conditional branches, we can perform simple conditional propagation on
2703 // the condition value itself.
2704 if (CondBrInst *BI = dyn_cast<CondBrInst>(I)) {
2705 if (isa<Constant>(BI->getCondition()))
2706 return processFoldableCondBr(BI);
2707
2708 Value *BranchCond = BI->getCondition();
2709 BasicBlock *TrueSucc = BI->getSuccessor(0);
2710 BasicBlock *FalseSucc = BI->getSuccessor(1);
2711 // Avoid multiple edges early.
2712 if (TrueSucc == FalseSucc)
2713 return false;
2714
2715 BasicBlock *Parent = BI->getParent();
2716 bool Changed = false;
2717
2719 BasicBlockEdge TrueE(Parent, TrueSucc);
2720 Changed |= propagateEquality(BranchCond, TrueVal, TrueE);
2721
2723 BasicBlockEdge FalseE(Parent, FalseSucc);
2724 Changed |= propagateEquality(BranchCond, FalseVal, FalseE);
2725
2726 return Changed;
2727 }
2728
2729 // For switches, propagate the case values into the case destinations.
2730 if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
2731 Value *SwitchCond = SI->getCondition();
2732 BasicBlock *Parent = SI->getParent();
2733 bool Changed = false;
2734
2735 // Remember how many outgoing edges there are to every successor.
2736 SmallDenseMap<BasicBlock *, unsigned, 16> SwitchEdges;
2737 for (BasicBlock *Succ : successors(Parent))
2738 ++SwitchEdges[Succ];
2739
2740 for (const auto &Case : SI->cases()) {
2741 BasicBlock *Dst = Case.getCaseSuccessor();
2742 // If there is only a single edge, propagate the case value into it.
2743 if (SwitchEdges.lookup(Dst) == 1) {
2744 BasicBlockEdge E(Parent, Dst);
2745 Changed |= propagateEquality(SwitchCond, Case.getCaseValue(), E);
2746 }
2747 }
2748 return Changed;
2749 }
2750
2751 // Instructions with void type don't return a value, so there's
2752 // no point in trying to find redundancies in them.
2753 if (I->getType()->isVoidTy())
2754 return false;
2755
2756 uint32_t NextNum = VN.getNextUnusedValueNumber();
2757 unsigned Num = VN.lookupOrAdd(I);
2758
2759 // Allocations are always uniquely numbered, so we can save time and memory
2760 // by fast failing them.
2761 if (isa<AllocaInst>(I) || I->isTerminator() || isa<PHINode>(I)) {
2762 LeaderTable.insert(Num, I, I->getParent());
2763 return false;
2764 }
2765
2766 // If the number we were assigned was a brand new VN, then we don't
2767 // need to do a lookup to see if the number already exists
2768 // somewhere in the domtree: it can't!
2769 if (Num >= NextNum) {
2770 LeaderTable.insert(Num, I, I->getParent());
2771 return false;
2772 }
2773
2774 // Perform fast-path value-number based elimination of values inherited from
2775 // dominators.
2776 Value *Repl = findLeader(I->getParent(), Num);
2777 if (!Repl) {
2778 // Failure, just remember this instance for future use.
2779 LeaderTable.insert(Num, I, I->getParent());
2780 return false;
2781 }
2782
2783 if (Repl == I) {
2784 // If I was the result of a shortcut PRE, it might already be in the table
2785 // and the best replacement for itself. Nothing to do.
2786 return false;
2787 }
2788
2789 // Remove it!
2791 if (MD && Repl->getType()->isPtrOrPtrVectorTy())
2792 MD->invalidateCachedPointerInfo(Repl);
2794 return true;
2795}
2796
2797/// runOnFunction - This is the main transformation entry point for a function.
2798bool GVNPass::runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
2799 const TargetLibraryInfo &RunTLI, AAResults &RunAA,
2800 MemoryDependenceResults *RunMD, LoopInfo &LI,
2801 OptimizationRemarkEmitter *RunORE, MemorySSA *MSSA) {
2802 AC = &RunAC;
2803 DT = &RunDT;
2804 VN.setDomTree(DT);
2805 TLI = &RunTLI;
2806 VN.setAliasAnalysis(&RunAA);
2807 MD = RunMD;
2808 ImplicitControlFlowTracking ImplicitCFT;
2809 ICF = &ImplicitCFT;
2810 this->LI = &LI;
2811 VN.setMemDep(MD);
2812 // Propagate the MSSA-enabled flag so the value-numbering paths in
2813 // lookupOrAddCall() and computeLoadStoreVN(), which depends on whether
2814 // IsMSSAEnabled is turned on.
2815 VN.setMemorySSA(MSSA, isMemorySSAEnabled());
2816 ORE = RunORE;
2817 InvalidBlockRPONumbers = true;
2818 MemorySSAUpdater Updater(MSSA);
2819 MSSAU = MSSA ? &Updater : nullptr;
2820
2821 bool Changed = false;
2822 bool ShouldContinue = true;
2823
2824 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
2825 // Merge unconditional branches, allowing PRE to catch more
2826 // optimization opportunities.
2827 for (BasicBlock &BB : make_early_inc_range(F)) {
2828 bool RemovedBlock = MergeBlockIntoPredecessor(&BB, &DTU, &LI, MSSAU, MD);
2829 if (RemovedBlock)
2830 ++NumGVNBlocks;
2831
2832 Changed |= RemovedBlock;
2833 }
2834 DTU.flush();
2835
2836 unsigned Iteration = 0;
2837 while (ShouldContinue) {
2838 LLVM_DEBUG(dbgs() << "GVN iteration: " << Iteration << "\n");
2839 (void) Iteration;
2840 ShouldContinue = iterateOnFunction(F);
2841 Changed |= ShouldContinue;
2842 ++Iteration;
2843 }
2844
2845 if (isScalarPREEnabled()) {
2846 // Fabricate val-num for dead-code in order to suppress assertion in
2847 // performPRE().
2848 assignValNumForDeadCode();
2849 bool PREChanged = true;
2850 while (PREChanged) {
2851 PREChanged = performPRE(F);
2852 Changed |= PREChanged;
2853 }
2854 }
2855
2856 // FIXME: Should perform GVN again after PRE does something. PRE can move
2857 // computations into blocks where they become fully redundant. Note that
2858 // we can't do this until PRE's critical edge splitting updates memdep.
2859 // Actually, when this happens, we should just fully integrate PRE into GVN.
2860
2861 cleanupGlobalSets();
2862 // Do not cleanup DeadBlocks in cleanupGlobalSets() as it's called for each
2863 // iteration.
2864 DeadBlocks.clear();
2865
2866 if (MSSA && VerifyMemorySSA)
2867 MSSA->verifyMemorySSA();
2868
2869 return Changed;
2870}
2871
2872bool GVNPass::processBlock(BasicBlock *BB) {
2873 if (DeadBlocks.count(BB))
2874 return false;
2875
2876 bool ChangedFunction = false;
2877
2878 // Since we may not have visited the input blocks of the phis, we can't
2879 // use our normal hash approach for phis. Instead, simply look for
2880 // obvious duplicates. The first pass of GVN will tend to create
2881 // identical phis, and the second or later passes can eliminate them.
2882 SmallPtrSet<PHINode *, 8> PHINodesToRemove;
2883 ChangedFunction |= EliminateDuplicatePHINodes(BB, PHINodesToRemove);
2884 for (PHINode *PN : PHINodesToRemove) {
2885 removeInstruction(PN);
2886 }
2887 for (Instruction &Inst : make_early_inc_range(*BB))
2888 ChangedFunction |= processInstruction(&Inst);
2889 return ChangedFunction;
2890}
2891
2892// Instantiate an expression in a predecessor that lacked it.
2893bool GVNPass::performScalarPREInsertion(Instruction *Instr, BasicBlock *Pred,
2894 BasicBlock *Curr, unsigned int ValNo) {
2895 // Because we are going top-down through the block, all value numbers
2896 // will be available in the predecessor by the time we need them. Any
2897 // that weren't originally present will have been instantiated earlier
2898 // in this loop.
2899 bool Success = true;
2900 for (unsigned I = 0, E = Instr->getNumOperands(); I != E; ++I) {
2901 Value *Op = Instr->getOperand(I);
2903 continue;
2904 // This could be a newly inserted instruction, in which case, we won't
2905 // find a value number, and should give up before we hurt ourselves.
2906 // FIXME: Rewrite the infrastructure to let it easier to value number
2907 // and process newly inserted instructions.
2908 if (!VN.exists(Op)) {
2909 Success = false;
2910 break;
2911 }
2912 uint32_t TValNo =
2913 VN.phiTranslate(Pred, Curr, VN.lookup(Op), *this);
2914 if (Value *V = findLeader(Pred, TValNo)) {
2915 Instr->setOperand(I, V);
2916 } else {
2917 Success = false;
2918 break;
2919 }
2920 }
2921
2922 // Fail out if we encounter an operand that is not available in
2923 // the PRE predecessor. This is typically because of loads which
2924 // are not value numbered precisely.
2925 if (!Success)
2926 return false;
2927
2928 Instr->insertBefore(Pred->getTerminator()->getIterator());
2929 Instr->setName(Instr->getName() + ".pre");
2930 Instr->setDebugLoc(Instr->getDebugLoc());
2931
2932 ICF->insertInstructionTo(Instr, Pred);
2933
2934 unsigned Num = VN.lookupOrAdd(Instr);
2935 VN.add(Instr, Num);
2936
2937 // Update the availability map to include the new instruction.
2938 LeaderTable.insert(Num, Instr, Pred);
2939 return true;
2940}
2941
2942bool GVNPass::performScalarPRE(Instruction *CurInst) {
2943 if (isa<AllocaInst>(CurInst) || CurInst->isTerminator() ||
2944 isa<PHINode>(CurInst) || CurInst->getType()->isVoidTy() ||
2945 CurInst->mayReadFromMemory() || CurInst->mayHaveSideEffects() ||
2946 CurInst->getType()->isTokenLikeTy())
2947 return false;
2948
2949 // Don't do PRE on compares. The PHI would prevent CodeGenPrepare from
2950 // sinking the compare again, and it would force the code generator to
2951 // move the i1 from processor flags or predicate registers into a general
2952 // purpose register.
2953 if (isa<CmpInst>(CurInst))
2954 return false;
2955
2956 // Don't do PRE on GEPs. The inserted PHI would prevent CodeGenPrepare from
2957 // sinking the addressing mode computation back to its uses. Extending the
2958 // GEP's live range increases the register pressure, and therefore it can
2959 // introduce unnecessary spills.
2960 //
2961 // This doesn't prevent Load PRE. PHI translation will make the GEP available
2962 // to the load by moving it to the predecessor block if necessary.
2963 if (isa<GetElementPtrInst>(CurInst))
2964 return false;
2965
2966 if (auto *CallB = dyn_cast<CallBase>(CurInst)) {
2967 // We don't currently value number ANY inline asm calls.
2968 if (CallB->isInlineAsm())
2969 return false;
2970 }
2971
2972 uint32_t ValNo = VN.lookup(CurInst);
2973
2974 // Look for the predecessors for PRE opportunities. We're
2975 // only trying to solve the basic diamond case, where
2976 // a value is computed in the successor and one predecessor,
2977 // but not the other. We also explicitly disallow cases
2978 // where the successor is its own predecessor, because they're
2979 // more complicated to get right.
2980 unsigned NumWith = 0;
2981 unsigned NumWithout = 0;
2982 BasicBlock *PREPred = nullptr;
2983 BasicBlock *CurrentBlock = CurInst->getParent();
2984
2985 // Update the RPO numbers for this function.
2986 if (InvalidBlockRPONumbers)
2987 assignBlockRPONumber(*CurrentBlock->getParent());
2988
2990 for (BasicBlock *P : predecessors(CurrentBlock)) {
2991 // We're not interested in PRE where blocks with predecessors that are
2992 // not reachable.
2993 if (!DT->isReachableFromEntry(P)) {
2994 NumWithout = 2;
2995 break;
2996 }
2997 // It is not safe to do PRE when P->CurrentBlock is a loop backedge.
2998 assert(BlockRPONumber.count(P) && BlockRPONumber.count(CurrentBlock) &&
2999 "Invalid BlockRPONumber map.");
3000 if (BlockRPONumber[P] >= BlockRPONumber[CurrentBlock]) {
3001 NumWithout = 2;
3002 break;
3003 }
3004
3005 uint32_t TValNo = VN.phiTranslate(P, CurrentBlock, ValNo, *this);
3006 Value *PredV = findLeader(P, TValNo);
3007 if (!PredV) {
3008 PredMap.push_back(std::make_pair(static_cast<Value *>(nullptr), P));
3009 PREPred = P;
3010 ++NumWithout;
3011 } else if (PredV == CurInst) {
3012 // CurInst dominates this predecessor.
3013 NumWithout = 2;
3014 break;
3015 } else {
3016 PredMap.push_back(std::make_pair(PredV, P));
3017 ++NumWith;
3018 }
3019 }
3020
3021 // Don't do PRE when it might increase code size, i.e. when
3022 // we would need to insert instructions in more than one pred.
3023 if (NumWithout > 1 || NumWith == 0)
3024 return false;
3025
3026 // We may have a case where all predecessors have the instruction,
3027 // and we just need to insert a phi node. Otherwise, perform
3028 // insertion.
3029 Instruction *PREInstr = nullptr;
3030
3031 if (NumWithout != 0) {
3032 if (!isSafeToSpeculativelyExecute(CurInst)) {
3033 // It is only valid to insert a new instruction if the current instruction
3034 // is always executed. An instruction with implicit control flow could
3035 // prevent us from doing it. If we cannot speculate the execution, then
3036 // PRE should be prohibited.
3037 if (ICF->isDominatedByICFIFromSameBlock(CurInst))
3038 return false;
3039 }
3040
3041 // Don't do PRE across indirect branch.
3042 if (isa<IndirectBrInst>(PREPred->getTerminator()))
3043 return false;
3044
3045 // We can't do PRE safely on a critical edge, so instead we schedule
3046 // the edge to be split and perform the PRE the next time we iterate
3047 // on the function.
3048 unsigned SuccNum = GetSuccessorNumber(PREPred, CurrentBlock);
3049 if (isCriticalEdge(PREPred->getTerminator(), SuccNum)) {
3050 ToSplit.push_back(std::make_pair(PREPred->getTerminator(), SuccNum));
3051 return false;
3052 }
3053 // We need to insert somewhere, so let's give it a shot.
3054 PREInstr = CurInst->clone();
3055 if (!performScalarPREInsertion(PREInstr, PREPred, CurrentBlock, ValNo)) {
3056 // If we failed insertion, make sure we remove the instruction.
3057#ifndef NDEBUG
3058 verifyRemoved(PREInstr);
3059#endif
3060 PREInstr->deleteValue();
3061 return false;
3062 }
3063 }
3064
3065 // Either we should have filled in the PRE instruction, or we should
3066 // not have needed insertions.
3067 assert(PREInstr != nullptr || NumWithout == 0);
3068
3069 ++NumGVNPRE;
3070
3071 // Create a PHI to make the value available in this block.
3072 PHINode *Phi = PHINode::Create(CurInst->getType(), PredMap.size(),
3073 CurInst->getName() + ".pre-phi");
3074 Phi->insertBefore(CurrentBlock->begin());
3075 for (auto &[V, BB] : PredMap) {
3076 if (V) {
3077 // If we use an existing value in this phi, we have to patch the original
3078 // value because the phi will be used to replace a later value.
3079 patchReplacementInstruction(CurInst, V);
3080 Phi->addIncoming(V, BB);
3081 } else
3082 Phi->addIncoming(PREInstr, PREPred);
3083 }
3084
3085 VN.add(Phi, ValNo);
3086 // After creating a new PHI for ValNo, the phi translate result for ValNo will
3087 // be changed, so erase the related stale entries in phi translate cache.
3088 VN.eraseTranslateCacheEntry(ValNo, *CurrentBlock);
3089 LeaderTable.insert(ValNo, Phi, CurrentBlock);
3090 Phi->setDebugLoc(CurInst->getDebugLoc());
3091 CurInst->replaceAllUsesWith(Phi);
3092 if (MD && Phi->getType()->isPtrOrPtrVectorTy())
3093 MD->invalidateCachedPointerInfo(Phi);
3094 LeaderTable.erase(ValNo, CurInst, CurrentBlock);
3095
3096 LLVM_DEBUG(dbgs() << "GVN PRE removed: " << *CurInst << '\n');
3097 removeInstruction(CurInst);
3098
3099 return true;
3100}
3101
3102/// Perform a purely local form of PRE that looks for diamond
3103/// control flow patterns and attempts to perform simple PRE at the join point.
3104bool GVNPass::performPRE(Function &F) {
3105 bool Changed = false;
3106 for (BasicBlock *CurrentBlock : depth_first(&F.getEntryBlock())) {
3107 // Nothing to PRE in the entry block.
3108 if (CurrentBlock == &F.getEntryBlock())
3109 continue;
3110
3111 // Don't perform PRE on an EH pad.
3112 if (CurrentBlock->isEHPad())
3113 continue;
3114
3115 for (BasicBlock::iterator BI = CurrentBlock->begin(),
3116 BE = CurrentBlock->end();
3117 BI != BE;) {
3118 Instruction *CurInst = &*BI++;
3119 Changed |= performScalarPRE(CurInst);
3120 }
3121 }
3122
3123 if (splitCriticalEdges())
3124 Changed = true;
3125
3126 return Changed;
3127}
3128
3129/// Split the critical edge connecting the given two blocks, and return
3130/// the block inserted to the critical edge.
3131BasicBlock *GVNPass::splitCriticalEdges(BasicBlock *Pred, BasicBlock *Succ) {
3132 // GVN does not require loop-simplify, do not try to preserve it if it is not
3133 // possible.
3135 Pred, Succ,
3136 CriticalEdgeSplittingOptions(DT, LI, MSSAU).unsetPreserveLoopSimplify());
3137 if (BB) {
3138 if (MD)
3139 MD->invalidateCachedPredecessors();
3140 InvalidBlockRPONumbers = true;
3141 }
3142 return BB;
3143}
3144
3145/// Split critical edges found during the previous
3146/// iteration that may enable further optimization.
3147bool GVNPass::splitCriticalEdges() {
3148 if (ToSplit.empty())
3149 return false;
3150
3151 bool Changed = false;
3152 do {
3153 std::pair<Instruction *, unsigned> Edge = ToSplit.pop_back_val();
3154 Changed |= SplitCriticalEdge(Edge.first, Edge.second,
3155 CriticalEdgeSplittingOptions(DT, LI, MSSAU)) !=
3156 nullptr;
3157 } while (!ToSplit.empty());
3158 if (Changed) {
3159 if (MD)
3160 MD->invalidateCachedPredecessors();
3161 InvalidBlockRPONumbers = true;
3162 }
3163 return Changed;
3164}
3165
3166/// Executes one iteration of GVN.
3167bool GVNPass::iterateOnFunction(Function &F) {
3168 cleanupGlobalSets();
3169
3170 // Top-down walk of the dominator tree.
3171 bool Changed = false;
3172 // Needed for value numbering with phi construction to work.
3173 // RPOT walks the graph in its constructor and will not be invalidated during
3174 // processBlock.
3175 ReversePostOrderTraversal<Function *> RPOT(&F);
3176
3177 for (BasicBlock *BB : RPOT)
3178 Changed |= processBlock(BB);
3179
3180 return Changed;
3181}
3182
3183void GVNPass::cleanupGlobalSets() {
3184 VN.clear();
3185 LeaderTable.clear();
3186 BlockRPONumber.clear();
3187 ICF->clear();
3188 InvalidBlockRPONumbers = true;
3189}
3190
3191void GVNPass::removeInstruction(Instruction *I) {
3192 VN.erase(I);
3193 if (MD) MD->removeInstruction(I);
3194 if (MSSAU)
3195 MSSAU->removeMemoryAccess(I);
3196#ifndef NDEBUG
3197 verifyRemoved(I);
3198#endif
3199 ICF->removeInstruction(I);
3200 I->eraseFromParent();
3201 ++NumGVNInstr;
3202}
3203
3204/// Verify that the specified instruction does not occur in our
3205/// internal data structures.
3206void GVNPass::verifyRemoved(const Instruction *Inst) const {
3207 VN.verifyRemoved(Inst);
3208}
3209
3210/// BB is declared dead, which implied other blocks become dead as well. This
3211/// function is to add all these blocks to "DeadBlocks". For the dead blocks'
3212/// live successors, update their phi nodes by replacing the operands
3213/// corresponding to dead blocks with UndefVal.
3214void GVNPass::addDeadBlock(BasicBlock *BB) {
3216 SmallSetVector<BasicBlock *, 4> DF;
3217
3218 NewDead.push_back(BB);
3219 while (!NewDead.empty()) {
3220 BasicBlock *D = NewDead.pop_back_val();
3221 if (DeadBlocks.count(D))
3222 continue;
3223
3224 // All blocks dominated by D are dead.
3225 SmallVector<BasicBlock *, 8> Dom;
3226 DT->getDescendants(D, Dom);
3227 DeadBlocks.insert_range(Dom);
3228
3229 // Figure out the dominance-frontier(D).
3230 for (BasicBlock *B : Dom) {
3231 for (BasicBlock *S : successors(B)) {
3232 if (DeadBlocks.count(S))
3233 continue;
3234
3235 bool AllPredDead = true;
3236 for (BasicBlock *P : predecessors(S))
3237 if (!DeadBlocks.count(P)) {
3238 AllPredDead = false;
3239 break;
3240 }
3241
3242 if (!AllPredDead) {
3243 // S could be proved dead later on. That is why we don't update phi
3244 // operands at this moment.
3245 DF.insert(S);
3246 } else {
3247 // While S is not dominated by D, it is dead by now. This could take
3248 // place if S already have a dead predecessor before D is declared
3249 // dead.
3250 NewDead.push_back(S);
3251 }
3252 }
3253 }
3254 }
3255
3256 // For the dead blocks' live successors, update their phi nodes by replacing
3257 // the operands corresponding to dead blocks with UndefVal.
3258 for (BasicBlock *B : DF) {
3259 if (DeadBlocks.count(B))
3260 continue;
3261
3262 // First, split the critical edges. This might also create additional blocks
3263 // to preserve LoopSimplify form and adjust edges accordingly.
3265 for (BasicBlock *P : Preds) {
3266 if (!DeadBlocks.count(P))
3267 continue;
3268
3269 if (is_contained(successors(P), B) &&
3270 isCriticalEdge(P->getTerminator(), B)) {
3271 if (BasicBlock *S = splitCriticalEdges(P, B))
3272 DeadBlocks.insert(P = S);
3273 }
3274 }
3275
3276 // Now poison the incoming values from the dead predecessors.
3277 for (BasicBlock *P : predecessors(B)) {
3278 if (!DeadBlocks.count(P))
3279 continue;
3280 for (PHINode &Phi : B->phis()) {
3281 Phi.setIncomingValueForBlock(P, PoisonValue::get(Phi.getType()));
3282 if (MD)
3283 MD->invalidateCachedPointerInfo(&Phi);
3284 }
3285 }
3286 }
3287}
3288
3289// If the given branch is recognized as a foldable branch (i.e. conditional
3290// branch with constant condition), it will perform following analyses and
3291// transformation.
3292// 1) If the dead out-coming edge is a critical-edge, split it. Let
3293// R be the target of the dead out-coming edge.
3294// 1) Identify the set of dead blocks implied by the branch's dead outcoming
3295// edge. The result of this step will be {X| X is dominated by R}
3296// 2) Identify those blocks which haves at least one dead predecessor. The
3297// result of this step will be dominance-frontier(R).
3298// 3) Update the PHIs in DF(R) by replacing the operands corresponding to
3299// dead blocks with "UndefVal" in an hope these PHIs will optimized away.
3300//
3301// Return true iff *NEW* dead code are found.
3302bool GVNPass::processFoldableCondBr(CondBrInst *BI) {
3303 // If a branch has two identical successors, we cannot declare either dead.
3304 if (BI->getSuccessor(0) == BI->getSuccessor(1))
3305 return false;
3306
3307 ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
3308 if (!Cond)
3309 return false;
3310
3311 BasicBlock *DeadRoot =
3312 Cond->getZExtValue() ? BI->getSuccessor(1) : BI->getSuccessor(0);
3313 if (DeadBlocks.count(DeadRoot))
3314 return false;
3315
3316 if (!DeadRoot->getSinglePredecessor())
3317 DeadRoot = splitCriticalEdges(BI->getParent(), DeadRoot);
3318
3319 addDeadBlock(DeadRoot);
3320 return true;
3321}
3322
3323// performPRE() will trigger assert if it comes across an instruction without
3324// associated val-num. As it normally has far more live instructions than dead
3325// instructions, it makes more sense just to "fabricate" a val-number for the
3326// dead code than checking if instruction involved is dead or not.
3327void GVNPass::assignValNumForDeadCode() {
3328 for (BasicBlock *BB : DeadBlocks) {
3329 for (Instruction &Inst : *BB) {
3330 unsigned ValNum = VN.lookupOrAdd(&Inst);
3331 LeaderTable.insert(ValNum, &Inst, BB);
3332 }
3333 }
3334}
3335
3337public:
3338 static char ID; // Pass identification, replacement for typeid.
3339
3340 explicit GVNLegacyPass(bool MemDepAnalysis = GVNEnableMemDep,
3341 bool MemSSAAnalysis = GVNEnableMemorySSA,
3342 bool ScalarPRE = true)
3343 : FunctionPass(ID), Impl(GVNOptions()
3344 .setMemDep(MemDepAnalysis)
3345 .setMemorySSA(MemSSAAnalysis)
3346 .setScalarPRE(ScalarPRE)) {
3348 }
3349
3350 bool runOnFunction(Function &F) override {
3351 if (skipFunction(F))
3352 return false;
3353
3355 if (Impl.isMemorySSAEnabled() && !MSSAWP)
3357
3358 return Impl.runImpl(
3359 F, getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F),
3362 getAnalysis<AAResultsWrapperPass>().getAAResults(),
3363 Impl.isMemDepEnabled()
3365 : nullptr,
3366 getAnalysis<LoopInfoWrapperPass>().getLoopInfo(),
3368 MSSAWP ? &MSSAWP->getMSSA() : nullptr);
3369 }
3370
3388
3389private:
3390 GVNPass Impl;
3391};
3392
3393char GVNLegacyPass::ID = 0;
3394
3395INITIALIZE_PASS_BEGIN(GVNLegacyPass, "gvn", "Global Value Numbering", false, false)
3404INITIALIZE_PASS_END(GVNLegacyPass, "gvn", "Global Value Numbering", false, false)
3405
3406// The public interface to this file...
3409 return new GVNLegacyPass(GVNEnableMemDep, GVNEnableMemorySSA, ScalarPRE);
3410}
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:1277
static void reportLoadElim(LoadInst *Load, Value *AvailableValue, OptimizationRemarkEmitter *ORE)
Definition GVN.cpp:1983
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:1221
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 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:2428
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:961
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:1298
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:1212
static bool isLifetimeStart(const Instruction *Inst)
Definition GVN.cpp:1204
static cl::opt< bool > GVNEnableSplitBackedgeInLoadPRE("enable-split-backedge-in-load-pre", cl::init(false))
static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl)
Definition GVN.cpp:2151
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:1077
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:1096
AvailabilityState
Definition GVN.cpp:941
@ Unavailable
We know the block is not fully available. This is a fixpoint.
Definition GVN.cpp:943
@ Available
We know the block is fully available. This is a fixpoint.
Definition GVN.cpp:945
@ SpeculativelyAvailable
We do not know whether the block is fully available or not, but we are currently speculating that it ...
Definition GVN.cpp:950
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
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.
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.
Definition InstrTypes.h:986
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:827
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:205
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:254
iterator end()
Definition DenseMap.h:81
Analysis pass which computes a DominatorTree.
Definition Dominators.h:278
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:314
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:159
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:161
LLVM_ABI uint32_t lookupOrAdd(MemoryAccess *MA)
Definition GVN.cpp:643
The core GVN pass object.
Definition GVN.h:128
friend class gvn::GVNLegacyPass
Definition GVN.h:247
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Run the pass over the function.
Definition GVN.cpp:872
LLVM_ABI void salvageAndRemoveInstruction(Instruction *I)
This removes the specified instruction from our various maps and marks it for deletion.
Definition GVN.cpp:924
AAResults * getAliasAnalysis() const
Definition GVN.h:148
LLVM_ABI bool isLoadPREEnabled() const
Definition GVN.cpp:851
GVNPass(GVNOptions Options={})
Definition GVN.h:134
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Definition GVN.cpp:904
LLVM_ABI bool isMemorySSAEnabled() const
Definition GVN.cpp:868
DominatorTree & getDominatorTree() const
Definition GVN.h:147
LLVM_ABI bool isLoadInLoopPREEnabled() const
Definition GVN.cpp:855
LLVM_ABI bool isScalarPREEnabled() const
Definition GVN.cpp:847
LLVM_ABI bool isLoadPRESplitBackedgeEnabled() const
Definition GVN.cpp:859
LLVM_ABI bool isMemDepEnabled() const
Definition GVN.cpp:864
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.
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
MemDepResult getDependency(Instruction *QueryInst)
Returns the instruction on which a memory operation depends.
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.
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 ...
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...
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
void Initialize(Type *Ty, StringRef Name)
Reset this object to get ready for a new set of SSA updates with type 'Ty'.
Value * GetValueInMiddleOfBlock(BasicBlock *BB)
Construct SSA form, materializing a value that is live in the middle of the specified block.
bool HasValueForBlock(BasicBlock *BB) const
Return true if the SSAUpdater already has a value for the specified block.
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)
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.
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:1136
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:311
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition Type.h:287
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:549
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:824
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:3371
GVNLegacyPass(bool MemDepAnalysis=GVNEnableMemDep, bool MemSSAAnalysis=GVNEnableMemorySSA, bool ScalarPRE=true)
Definition GVN.cpp:3340
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Definition GVN.cpp:3350
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.
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...
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...
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...
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...
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...
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:63
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:557
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:3280
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:3408
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:1683
auto successors(const MachineBasicBlock *BB)
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 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.
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:861
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:881
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:3180
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:3259
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:3110
@ Other
Any other memory.
Definition ModRef.h:68
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:3407
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:119
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 bool EliminateDuplicatePHINodes(BasicBlock *BB)
Check for and eliminate duplicate PHI nodes in this block.
Definition Local.cpp:1524
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:876
#define N
static GVNPass::Expression getTombstoneKey()
Definition GVN.cpp:176
static bool isEqual(const GVNPass::Expression &LHS, const GVNPass::Expression &RHS)
Definition GVN.cpp:184
static GVNPass::Expression getEmptyKey()
Definition GVN.cpp:175
static unsigned getHashValue(const GVNPass::Expression &E)
Definition GVN.cpp:178
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:78
bool operator==(const Expression &Other) const
Definition GVN.cpp:153
friend hash_code hash_value(const Expression &Value)
Definition GVN.cpp:168
SmallVector< uint32_t, 4 > VarArgs
Definition GVN.cpp:147
AttributeList Attrs
Definition GVN.cpp:149
Expression(uint32_t Op=~2U)
Definition GVN.cpp:151
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:290
static AvailableValueInBlock get(BasicBlock *BB, Value *V, unsigned Offset=0)
Definition GVN.cpp:304
static AvailableValueInBlock getUndef(BasicBlock *BB)
Definition GVN.cpp:309
static AvailableValueInBlock get(BasicBlock *BB, AvailableValue &&AV)
Definition GVN.cpp:297
AvailableValue AV
AV - The actual available value.
Definition GVN.cpp:295
static AvailableValueInBlock getSelect(BasicBlock *BB, SelectInst *Sel, Value *V1, Value *V2)
Definition GVN.cpp:313
BasicBlock * BB
BB - The basic block in question.
Definition GVN.cpp:292
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:320
Represents a particular available value that we know how to materialize.
Definition GVN.cpp:194
unsigned Offset
Offset - The byte offset in Val that is interesting for the load query.
Definition GVN.cpp:211
bool isSimpleValue() const
Definition GVN.cpp:257
static AvailableValue getSelect(SelectInst *Sel, Value *V1, Value *V2)
Definition GVN.cpp:247
bool isCoercedLoadValue() const
Definition GVN.cpp:258
static AvailableValue get(Value *V, unsigned Offset=0)
Definition GVN.cpp:215
ValType Kind
Kind of the live-out value.
Definition GVN.cpp:208
LoadInst * getCoercedLoadValue() const
Definition GVN.cpp:268
static AvailableValue getLoad(LoadInst *Load, unsigned Offset=0)
Definition GVN.cpp:231
static AvailableValue getMI(MemIntrinsic *MI, unsigned Offset=0)
Definition GVN.cpp:223
bool isUndefValue() const
Definition GVN.cpp:260
bool isSelectValue() const
Definition GVN.cpp:261
Value * Val
Val - The value that is live out of the block.
Definition GVN.cpp:206
Value * V1
V1, V2 - The dominating non-clobbered values of SelectVal.
Definition GVN.cpp:213
static AvailableValue getUndef()
Definition GVN.cpp:239
SelectInst * getSelectValue() const
Definition GVN.cpp:278
Value * getSimpleValue() const
Definition GVN.cpp:263
bool isMemIntrinValue() const
Definition GVN.cpp:259
MemIntrinsic * getMemIntrinValue() const
Definition GVN.cpp:273
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:1139