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