LLVM  4.0.0
GVN.cpp
Go to the documentation of this file.
1 //===- GVN.cpp - Eliminate redundant values and loads ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass performs global value numbering to eliminate fully redundant
11 // instructions. It also performs simple dead load elimination.
12 //
13 // Note that this pass does the value numbering itself; it does not use the
14 // ValueNumbering analysis passes.
15 //
16 //===----------------------------------------------------------------------===//
17 
19 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/Hashing.h"
22 #include "llvm/ADT/MapVector.h"
24 #include "llvm/ADT/SetVector.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/ADT/Statistic.h"
29 #include "llvm/Analysis/CFG.h"
33 #include "llvm/Analysis/Loads.h"
40 #include "llvm/IR/DataLayout.h"
41 #include "llvm/IR/Dominators.h"
42 #include "llvm/IR/GlobalVariable.h"
43 #include "llvm/IR/IRBuilder.h"
44 #include "llvm/IR/IntrinsicInst.h"
45 #include "llvm/IR/LLVMContext.h"
46 #include "llvm/IR/Metadata.h"
47 #include "llvm/IR/PatternMatch.h"
49 #include "llvm/Support/Debug.h"
54 #include <vector>
55 using namespace llvm;
56 using namespace llvm::gvn;
57 using namespace PatternMatch;
58 
59 #define DEBUG_TYPE "gvn"
60 
61 STATISTIC(NumGVNInstr, "Number of instructions deleted");
62 STATISTIC(NumGVNLoad, "Number of loads deleted");
63 STATISTIC(NumGVNPRE, "Number of instructions PRE'd");
64 STATISTIC(NumGVNBlocks, "Number of blocks merged");
65 STATISTIC(NumGVNSimpl, "Number of instructions simplified");
66 STATISTIC(NumGVNEqProp, "Number of equalities propagated");
67 STATISTIC(NumPRELoad, "Number of loads PRE'd");
68 
69 static cl::opt<bool> EnablePRE("enable-pre",
70  cl::init(true), cl::Hidden);
71 static cl::opt<bool> EnableLoadPRE("enable-load-pre", cl::init(true));
72 
73 // Maximum allowed recursion depth.
74 static cl::opt<uint32_t>
75 MaxRecurseDepth("max-recurse-depth", cl::Hidden, cl::init(1000), cl::ZeroOrMore,
76  cl::desc("Max recurse depth (default = 1000)"));
77 
82 
83  Expression(uint32_t o = ~2U) : opcode(o) {}
84 
85  bool operator==(const Expression &other) const {
86  if (opcode != other.opcode)
87  return false;
88  if (opcode == ~0U || opcode == ~1U)
89  return true;
90  if (type != other.type)
91  return false;
92  if (varargs != other.varargs)
93  return false;
94  return true;
95  }
96 
98  return hash_combine(
99  Value.opcode, Value.type,
100  hash_combine_range(Value.varargs.begin(), Value.varargs.end()));
101  }
102 };
103 
104 namespace llvm {
105 template <> struct DenseMapInfo<GVN::Expression> {
106  static inline GVN::Expression getEmptyKey() { return ~0U; }
107 
108  static inline GVN::Expression getTombstoneKey() { return ~1U; }
109 
110  static unsigned getHashValue(const GVN::Expression &e) {
111  using llvm::hash_value;
112  return static_cast<unsigned>(hash_value(e));
113  }
114  static bool isEqual(const GVN::Expression &LHS, const GVN::Expression &RHS) {
115  return LHS == RHS;
116  }
117 };
118 } // End llvm namespace.
119 
120 /// Represents a particular available value that we know how to materialize.
121 /// Materialization of an AvailableValue never fails. An AvailableValue is
122 /// implicitly associated with a rematerialization point which is the
123 /// location of the instruction from which it was formed.
125  enum ValType {
126  SimpleVal, // A simple offsetted value that is accessed.
127  LoadVal, // A value produced by a load.
128  MemIntrin, // A memory intrinsic which is loaded from.
129  UndefVal // A UndefValue representing a value from dead block (which
130  // is not yet physically removed from the CFG).
131  };
132 
133  /// V - The value that is live out of the block.
135 
136  /// Offset - The byte offset in Val that is interesting for the load query.
137  unsigned Offset;
138 
139  static AvailableValue get(Value *V, unsigned Offset = 0) {
140  AvailableValue Res;
141  Res.Val.setPointer(V);
142  Res.Val.setInt(SimpleVal);
143  Res.Offset = Offset;
144  return Res;
145  }
146 
147  static AvailableValue getMI(MemIntrinsic *MI, unsigned Offset = 0) {
148  AvailableValue Res;
149  Res.Val.setPointer(MI);
150  Res.Val.setInt(MemIntrin);
151  Res.Offset = Offset;
152  return Res;
153  }
154 
155  static AvailableValue getLoad(LoadInst *LI, unsigned Offset = 0) {
156  AvailableValue Res;
157  Res.Val.setPointer(LI);
158  Res.Val.setInt(LoadVal);
159  Res.Offset = Offset;
160  return Res;
161  }
162 
164  AvailableValue Res;
165  Res.Val.setPointer(nullptr);
166  Res.Val.setInt(UndefVal);
167  Res.Offset = 0;
168  return Res;
169  }
170 
171  bool isSimpleValue() const { return Val.getInt() == SimpleVal; }
172  bool isCoercedLoadValue() const { return Val.getInt() == LoadVal; }
173  bool isMemIntrinValue() const { return Val.getInt() == MemIntrin; }
174  bool isUndefValue() const { return Val.getInt() == UndefVal; }
175 
177  assert(isSimpleValue() && "Wrong accessor");
178  return Val.getPointer();
179  }
180 
182  assert(isCoercedLoadValue() && "Wrong accessor");
183  return cast<LoadInst>(Val.getPointer());
184  }
185 
187  assert(isMemIntrinValue() && "Wrong accessor");
188  return cast<MemIntrinsic>(Val.getPointer());
189  }
190 
191  /// Emit code at the specified insertion point to adjust the value defined
192  /// here to the specified type. This handles various coercion cases.
193  Value *MaterializeAdjustedValue(LoadInst *LI, Instruction *InsertPt,
194  GVN &gvn) const;
195 };
196 
197 /// Represents an AvailableValue which can be rematerialized at the end of
198 /// the associated BasicBlock.
200  /// BB - The basic block in question.
202 
203  /// AV - The actual available value
205 
208  Res.BB = BB;
209  Res.AV = std::move(AV);
210  return Res;
211  }
212 
214  unsigned Offset = 0) {
215  return get(BB, AvailableValue::get(V, Offset));
216  }
218  return get(BB, AvailableValue::getUndef());
219  }
220 
221  /// Emit code at the end of this block to adjust the value defined here to
222  /// the specified type. This handles various coercion cases.
224  return AV.MaterializeAdjustedValue(LI, BB->getTerminator(), gvn);
225  }
226 };
227 
228 //===----------------------------------------------------------------------===//
229 // ValueTable Internal Functions
230 //===----------------------------------------------------------------------===//
231 
232 GVN::Expression GVN::ValueTable::createExpr(Instruction *I) {
233  Expression e;
234  e.type = I->getType();
235  e.opcode = I->getOpcode();
236  for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
237  OI != OE; ++OI)
238  e.varargs.push_back(lookupOrAdd(*OI));
239  if (I->isCommutative()) {
240  // Ensure that commutative instructions that only differ by a permutation
241  // of their operands get the same value number by sorting the operand value
242  // numbers. Since all commutative instructions have two operands it is more
243  // efficient to sort by hand rather than using, say, std::sort.
244  assert(I->getNumOperands() == 2 && "Unsupported commutative instruction!");
245  if (e.varargs[0] > e.varargs[1])
246  std::swap(e.varargs[0], e.varargs[1]);
247  }
248 
249  if (CmpInst *C = dyn_cast<CmpInst>(I)) {
250  // Sort the operand value numbers so x<y and y>x get the same value number.
251  CmpInst::Predicate Predicate = C->getPredicate();
252  if (e.varargs[0] > e.varargs[1]) {
253  std::swap(e.varargs[0], e.varargs[1]);
254  Predicate = CmpInst::getSwappedPredicate(Predicate);
255  }
256  e.opcode = (C->getOpcode() << 8) | Predicate;
257  } else if (InsertValueInst *E = dyn_cast<InsertValueInst>(I)) {
258  for (InsertValueInst::idx_iterator II = E->idx_begin(), IE = E->idx_end();
259  II != IE; ++II)
260  e.varargs.push_back(*II);
261  }
262 
263  return e;
264 }
265 
266 GVN::Expression GVN::ValueTable::createCmpExpr(unsigned Opcode,
267  CmpInst::Predicate Predicate,
268  Value *LHS, Value *RHS) {
269  assert((Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) &&
270  "Not a comparison!");
271  Expression e;
272  e.type = CmpInst::makeCmpResultType(LHS->getType());
273  e.varargs.push_back(lookupOrAdd(LHS));
274  e.varargs.push_back(lookupOrAdd(RHS));
275 
276  // Sort the operand value numbers so x<y and y>x get the same value number.
277  if (e.varargs[0] > e.varargs[1]) {
278  std::swap(e.varargs[0], e.varargs[1]);
279  Predicate = CmpInst::getSwappedPredicate(Predicate);
280  }
281  e.opcode = (Opcode << 8) | Predicate;
282  return e;
283 }
284 
285 GVN::Expression GVN::ValueTable::createExtractvalueExpr(ExtractValueInst *EI) {
286  assert(EI && "Not an ExtractValueInst?");
287  Expression e;
288  e.type = EI->getType();
289  e.opcode = 0;
290 
292  if (I != nullptr && EI->getNumIndices() == 1 && *EI->idx_begin() == 0 ) {
293  // EI might be an extract from one of our recognised intrinsics. If it
294  // is we'll synthesize a semantically equivalent expression instead on
295  // an extract value expression.
296  switch (I->getIntrinsicID()) {
297  case Intrinsic::sadd_with_overflow:
298  case Intrinsic::uadd_with_overflow:
299  e.opcode = Instruction::Add;
300  break;
301  case Intrinsic::ssub_with_overflow:
302  case Intrinsic::usub_with_overflow:
303  e.opcode = Instruction::Sub;
304  break;
305  case Intrinsic::smul_with_overflow:
306  case Intrinsic::umul_with_overflow:
307  e.opcode = Instruction::Mul;
308  break;
309  default:
310  break;
311  }
312 
313  if (e.opcode != 0) {
314  // Intrinsic recognized. Grab its args to finish building the expression.
315  assert(I->getNumArgOperands() == 2 &&
316  "Expect two args for recognised intrinsics.");
317  e.varargs.push_back(lookupOrAdd(I->getArgOperand(0)));
318  e.varargs.push_back(lookupOrAdd(I->getArgOperand(1)));
319  return e;
320  }
321  }
322 
323  // Not a recognised intrinsic. Fall back to producing an extract value
324  // expression.
325  e.opcode = EI->getOpcode();
326  for (Instruction::op_iterator OI = EI->op_begin(), OE = EI->op_end();
327  OI != OE; ++OI)
328  e.varargs.push_back(lookupOrAdd(*OI));
329 
330  for (ExtractValueInst::idx_iterator II = EI->idx_begin(), IE = EI->idx_end();
331  II != IE; ++II)
332  e.varargs.push_back(*II);
333 
334  return e;
335 }
336 
337 //===----------------------------------------------------------------------===//
338 // ValueTable External Functions
339 //===----------------------------------------------------------------------===//
340 
341 GVN::ValueTable::ValueTable() : nextValueNumber(1) {}
342 GVN::ValueTable::ValueTable(const ValueTable &) = default;
344 GVN::ValueTable::~ValueTable() = default;
345 
346 /// add - Insert a value into the table with a specified value number.
348  valueNumbering.insert(std::make_pair(V, num));
349 }
350 
351 uint32_t GVN::ValueTable::lookupOrAddCall(CallInst *C) {
352  if (AA->doesNotAccessMemory(C)) {
353  Expression exp = createExpr(C);
354  uint32_t &e = expressionNumbering[exp];
355  if (!e) e = nextValueNumber++;
356  valueNumbering[C] = e;
357  return e;
358  } else if (AA->onlyReadsMemory(C)) {
359  Expression exp = createExpr(C);
360  uint32_t &e = expressionNumbering[exp];
361  if (!e) {
362  e = nextValueNumber++;
363  valueNumbering[C] = e;
364  return e;
365  }
366  if (!MD) {
367  e = nextValueNumber++;
368  valueNumbering[C] = e;
369  return e;
370  }
371 
372  MemDepResult local_dep = MD->getDependency(C);
373 
374  if (!local_dep.isDef() && !local_dep.isNonLocal()) {
375  valueNumbering[C] = nextValueNumber;
376  return nextValueNumber++;
377  }
378 
379  if (local_dep.isDef()) {
380  CallInst* local_cdep = cast<CallInst>(local_dep.getInst());
381 
382  if (local_cdep->getNumArgOperands() != C->getNumArgOperands()) {
383  valueNumbering[C] = nextValueNumber;
384  return nextValueNumber++;
385  }
386 
387  for (unsigned i = 0, e = C->getNumArgOperands(); i < e; ++i) {
388  uint32_t c_vn = lookupOrAdd(C->getArgOperand(i));
389  uint32_t cd_vn = lookupOrAdd(local_cdep->getArgOperand(i));
390  if (c_vn != cd_vn) {
391  valueNumbering[C] = nextValueNumber;
392  return nextValueNumber++;
393  }
394  }
395 
396  uint32_t v = lookupOrAdd(local_cdep);
397  valueNumbering[C] = v;
398  return v;
399  }
400 
401  // Non-local case.
404  // FIXME: Move the checking logic to MemDep!
405  CallInst* cdep = nullptr;
406 
407  // Check to see if we have a single dominating call instruction that is
408  // identical to C.
409  for (unsigned i = 0, e = deps.size(); i != e; ++i) {
410  const NonLocalDepEntry *I = &deps[i];
411  if (I->getResult().isNonLocal())
412  continue;
413 
414  // We don't handle non-definitions. If we already have a call, reject
415  // instruction dependencies.
416  if (!I->getResult().isDef() || cdep != nullptr) {
417  cdep = nullptr;
418  break;
419  }
420 
421  CallInst *NonLocalDepCall = dyn_cast<CallInst>(I->getResult().getInst());
422  // FIXME: All duplicated with non-local case.
423  if (NonLocalDepCall && DT->properlyDominates(I->getBB(), C->getParent())){
424  cdep = NonLocalDepCall;
425  continue;
426  }
427 
428  cdep = nullptr;
429  break;
430  }
431 
432  if (!cdep) {
433  valueNumbering[C] = nextValueNumber;
434  return nextValueNumber++;
435  }
436 
437  if (cdep->getNumArgOperands() != C->getNumArgOperands()) {
438  valueNumbering[C] = nextValueNumber;
439  return nextValueNumber++;
440  }
441  for (unsigned i = 0, e = C->getNumArgOperands(); i < e; ++i) {
442  uint32_t c_vn = lookupOrAdd(C->getArgOperand(i));
443  uint32_t cd_vn = lookupOrAdd(cdep->getArgOperand(i));
444  if (c_vn != cd_vn) {
445  valueNumbering[C] = nextValueNumber;
446  return nextValueNumber++;
447  }
448  }
449 
450  uint32_t v = lookupOrAdd(cdep);
451  valueNumbering[C] = v;
452  return v;
453 
454  } else {
455  valueNumbering[C] = nextValueNumber;
456  return nextValueNumber++;
457  }
458 }
459 
460 /// Returns true if a value number exists for the specified value.
461 bool GVN::ValueTable::exists(Value *V) const { return valueNumbering.count(V) != 0; }
462 
463 /// lookup_or_add - Returns the value number for the specified value, assigning
464 /// it a new number if it did not have one before.
466  DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
467  if (VI != valueNumbering.end())
468  return VI->second;
469 
470  if (!isa<Instruction>(V)) {
471  valueNumbering[V] = nextValueNumber;
472  return nextValueNumber++;
473  }
474 
475  Instruction* I = cast<Instruction>(V);
476  Expression exp;
477  switch (I->getOpcode()) {
478  case Instruction::Call:
479  return lookupOrAddCall(cast<CallInst>(I));
480  case Instruction::Add:
481  case Instruction::FAdd:
482  case Instruction::Sub:
483  case Instruction::FSub:
484  case Instruction::Mul:
485  case Instruction::FMul:
486  case Instruction::UDiv:
487  case Instruction::SDiv:
488  case Instruction::FDiv:
489  case Instruction::URem:
490  case Instruction::SRem:
491  case Instruction::FRem:
492  case Instruction::Shl:
493  case Instruction::LShr:
494  case Instruction::AShr:
495  case Instruction::And:
496  case Instruction::Or:
497  case Instruction::Xor:
498  case Instruction::ICmp:
499  case Instruction::FCmp:
500  case Instruction::Trunc:
501  case Instruction::ZExt:
502  case Instruction::SExt:
503  case Instruction::FPToUI:
504  case Instruction::FPToSI:
505  case Instruction::UIToFP:
506  case Instruction::SIToFP:
507  case Instruction::FPTrunc:
508  case Instruction::FPExt:
509  case Instruction::PtrToInt:
510  case Instruction::IntToPtr:
511  case Instruction::BitCast:
512  case Instruction::Select:
513  case Instruction::ExtractElement:
514  case Instruction::InsertElement:
515  case Instruction::ShuffleVector:
516  case Instruction::InsertValue:
517  case Instruction::GetElementPtr:
518  exp = createExpr(I);
519  break;
520  case Instruction::ExtractValue:
521  exp = createExtractvalueExpr(cast<ExtractValueInst>(I));
522  break;
523  default:
524  valueNumbering[V] = nextValueNumber;
525  return nextValueNumber++;
526  }
527 
528  uint32_t& e = expressionNumbering[exp];
529  if (!e) e = nextValueNumber++;
530  valueNumbering[V] = e;
531  return e;
532 }
533 
534 /// Returns the value number of the specified value. Fails if
535 /// the value has not yet been numbered.
537  DenseMap<Value*, uint32_t>::const_iterator VI = valueNumbering.find(V);
538  assert(VI != valueNumbering.end() && "Value not numbered?");
539  return VI->second;
540 }
541 
542 /// Returns the value number of the given comparison,
543 /// assigning it a new number if it did not have one before. Useful when
544 /// we deduced the result of a comparison, but don't immediately have an
545 /// instruction realizing that comparison to hand.
547  CmpInst::Predicate Predicate,
548  Value *LHS, Value *RHS) {
549  Expression exp = createCmpExpr(Opcode, Predicate, LHS, RHS);
550  uint32_t& e = expressionNumbering[exp];
551  if (!e) e = nextValueNumber++;
552  return e;
553 }
554 
555 /// Remove all entries from the ValueTable.
557  valueNumbering.clear();
558  expressionNumbering.clear();
559  nextValueNumber = 1;
560 }
561 
562 /// Remove a value from the value numbering.
564  valueNumbering.erase(V);
565 }
566 
567 /// verifyRemoved - Verify that the value is removed from all internal data
568 /// structures.
569 void GVN::ValueTable::verifyRemoved(const Value *V) const {
571  I = valueNumbering.begin(), E = valueNumbering.end(); I != E; ++I) {
572  assert(I->first != V && "Inst still occurs in value numbering map!");
573  }
574 }
575 
576 //===----------------------------------------------------------------------===//
577 // GVN Pass
578 //===----------------------------------------------------------------------===//
579 
581  // FIXME: The order of evaluation of these 'getResult' calls is very
582  // significant! Re-ordering these variables will cause GVN when run alone to
583  // be less effective! We should fix memdep and basic-aa to not exhibit this
584  // behavior, but until then don't change the order here.
585  auto &AC = AM.getResult<AssumptionAnalysis>(F);
586  auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
587  auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
588  auto &AA = AM.getResult<AAManager>(F);
589  auto &MemDep = AM.getResult<MemoryDependenceAnalysis>(F);
590  auto *LI = AM.getCachedResult<LoopAnalysis>(F);
592  bool Changed = runImpl(F, AC, DT, TLI, AA, &MemDep, LI, &ORE);
593  if (!Changed)
594  return PreservedAnalyses::all();
597  PA.preserve<GlobalsAA>();
598  return PA;
599 }
600 
602 void GVN::dump(DenseMap<uint32_t, Value*>& d) {
603  errs() << "{\n";
605  E = d.end(); I != E; ++I) {
606  errs() << I->first << "\n";
607  I->second->dump();
608  }
609  errs() << "}\n";
610 }
611 
612 /// Return true if we can prove that the value
613 /// we're analyzing is fully available in the specified block. As we go, keep
614 /// track of which blocks we know are fully alive in FullyAvailableBlocks. This
615 /// map is actually a tri-state map with the following values:
616 /// 0) we know the block *is not* fully available.
617 /// 1) we know the block *is* fully available.
618 /// 2) we do not know whether the block is fully available or not, but we are
619 /// currently speculating that it will be.
620 /// 3) we are speculating for this block and have used that to speculate for
621 /// other blocks.
623  DenseMap<BasicBlock*, char> &FullyAvailableBlocks,
624  uint32_t RecurseDepth) {
625  if (RecurseDepth > MaxRecurseDepth)
626  return false;
627 
628  // Optimistically assume that the block is fully available and check to see
629  // if we already know about this block in one lookup.
630  std::pair<DenseMap<BasicBlock*, char>::iterator, char> IV =
631  FullyAvailableBlocks.insert(std::make_pair(BB, 2));
632 
633  // If the entry already existed for this block, return the precomputed value.
634  if (!IV.second) {
635  // If this is a speculative "available" value, mark it as being used for
636  // speculation of other blocks.
637  if (IV.first->second == 2)
638  IV.first->second = 3;
639  return IV.first->second != 0;
640  }
641 
642  // Otherwise, see if it is fully available in all predecessors.
643  pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
644 
645  // If this block has no predecessors, it isn't live-in here.
646  if (PI == PE)
647  goto SpeculationFailure;
648 
649  for (; PI != PE; ++PI)
650  // If the value isn't fully available in one of our predecessors, then it
651  // isn't fully available in this block either. Undo our previous
652  // optimistic assumption and bail out.
653  if (!IsValueFullyAvailableInBlock(*PI, FullyAvailableBlocks,RecurseDepth+1))
654  goto SpeculationFailure;
655 
656  return true;
657 
658 // If we get here, we found out that this is not, after
659 // all, a fully-available block. We have a problem if we speculated on this and
660 // used the speculation to mark other blocks as available.
661 SpeculationFailure:
662  char &BBVal = FullyAvailableBlocks[BB];
663 
664  // If we didn't speculate on this, just return with it set to false.
665  if (BBVal == 2) {
666  BBVal = 0;
667  return false;
668  }
669 
670  // If we did speculate on this value, we could have blocks set to 1 that are
671  // incorrect. Walk the (transitive) successors of this block and mark them as
672  // 0 if set to one.
673  SmallVector<BasicBlock*, 32> BBWorklist;
674  BBWorklist.push_back(BB);
675 
676  do {
677  BasicBlock *Entry = BBWorklist.pop_back_val();
678  // Note that this sets blocks to 0 (unavailable) if they happen to not
679  // already be in FullyAvailableBlocks. This is safe.
680  char &EntryVal = FullyAvailableBlocks[Entry];
681  if (EntryVal == 0) continue; // Already unavailable.
682 
683  // Mark as unavailable.
684  EntryVal = 0;
685 
686  BBWorklist.append(succ_begin(Entry), succ_end(Entry));
687  } while (!BBWorklist.empty());
688 
689  return false;
690 }
691 
692 
693 /// Return true if CoerceAvailableValueToLoadType will succeed.
694 static bool CanCoerceMustAliasedValueToLoad(Value *StoredVal,
695  Type *LoadTy,
696  const DataLayout &DL) {
697  // If the loaded or stored value is an first class array or struct, don't try
698  // to transform them. We need to be able to bitcast to integer.
699  if (LoadTy->isStructTy() || LoadTy->isArrayTy() ||
700  StoredVal->getType()->isStructTy() ||
701  StoredVal->getType()->isArrayTy())
702  return false;
703 
704  // The store has to be at least as big as the load.
705  if (DL.getTypeSizeInBits(StoredVal->getType()) <
706  DL.getTypeSizeInBits(LoadTy))
707  return false;
708 
709  return true;
710 }
711 
712 /// If we saw a store of a value to memory, and
713 /// then a load from a must-aliased pointer of a different type, try to coerce
714 /// the stored value. LoadedTy is the type of the load we want to replace.
715 /// IRB is IRBuilder used to insert new instructions.
716 ///
717 /// If we can't do it, return null.
718 static Value *CoerceAvailableValueToLoadType(Value *StoredVal, Type *LoadedTy,
719  IRBuilder<> &IRB,
720  const DataLayout &DL) {
721  assert(CanCoerceMustAliasedValueToLoad(StoredVal, LoadedTy, DL) &&
722  "precondition violation - materialization can't fail");
723 
724  if (auto *C = dyn_cast<Constant>(StoredVal))
725  if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL))
726  StoredVal = FoldedStoredVal;
727 
728  // If this is already the right type, just return it.
729  Type *StoredValTy = StoredVal->getType();
730 
731  uint64_t StoredValSize = DL.getTypeSizeInBits(StoredValTy);
732  uint64_t LoadedValSize = DL.getTypeSizeInBits(LoadedTy);
733 
734  // If the store and reload are the same size, we can always reuse it.
735  if (StoredValSize == LoadedValSize) {
736  // Pointer to Pointer -> use bitcast.
737  if (StoredValTy->getScalarType()->isPointerTy() &&
738  LoadedTy->getScalarType()->isPointerTy()) {
739  StoredVal = IRB.CreateBitCast(StoredVal, LoadedTy);
740  } else {
741  // Convert source pointers to integers, which can be bitcast.
742  if (StoredValTy->getScalarType()->isPointerTy()) {
743  StoredValTy = DL.getIntPtrType(StoredValTy);
744  StoredVal = IRB.CreatePtrToInt(StoredVal, StoredValTy);
745  }
746 
747  Type *TypeToCastTo = LoadedTy;
748  if (TypeToCastTo->getScalarType()->isPointerTy())
749  TypeToCastTo = DL.getIntPtrType(TypeToCastTo);
750 
751  if (StoredValTy != TypeToCastTo)
752  StoredVal = IRB.CreateBitCast(StoredVal, TypeToCastTo);
753 
754  // Cast to pointer if the load needs a pointer type.
755  if (LoadedTy->getScalarType()->isPointerTy())
756  StoredVal = IRB.CreateIntToPtr(StoredVal, LoadedTy);
757  }
758 
759  if (auto *C = dyn_cast<ConstantExpr>(StoredVal))
760  if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL))
761  StoredVal = FoldedStoredVal;
762 
763  return StoredVal;
764  }
765 
766  // If the loaded value is smaller than the available value, then we can
767  // extract out a piece from it. If the available value is too small, then we
768  // can't do anything.
769  assert(StoredValSize >= LoadedValSize &&
770  "CanCoerceMustAliasedValueToLoad fail");
771 
772  // Convert source pointers to integers, which can be manipulated.
773  if (StoredValTy->getScalarType()->isPointerTy()) {
774  StoredValTy = DL.getIntPtrType(StoredValTy);
775  StoredVal = IRB.CreatePtrToInt(StoredVal, StoredValTy);
776  }
777 
778  // Convert vectors and fp to integer, which can be manipulated.
779  if (!StoredValTy->isIntegerTy()) {
780  StoredValTy = IntegerType::get(StoredValTy->getContext(), StoredValSize);
781  StoredVal = IRB.CreateBitCast(StoredVal, StoredValTy);
782  }
783 
784  // If this is a big-endian system, we need to shift the value down to the low
785  // bits so that a truncate will work.
786  if (DL.isBigEndian()) {
787  uint64_t ShiftAmt = DL.getTypeStoreSizeInBits(StoredValTy) -
788  DL.getTypeStoreSizeInBits(LoadedTy);
789  StoredVal = IRB.CreateLShr(StoredVal, ShiftAmt, "tmp");
790  }
791 
792  // Truncate the integer to the right size now.
793  Type *NewIntTy = IntegerType::get(StoredValTy->getContext(), LoadedValSize);
794  StoredVal = IRB.CreateTrunc(StoredVal, NewIntTy, "trunc");
795 
796  if (LoadedTy != NewIntTy) {
797  // If the result is a pointer, inttoptr.
798  if (LoadedTy->getScalarType()->isPointerTy())
799  StoredVal = IRB.CreateIntToPtr(StoredVal, LoadedTy, "inttoptr");
800  else
801  // Otherwise, bitcast.
802  StoredVal = IRB.CreateBitCast(StoredVal, LoadedTy, "bitcast");
803  }
804 
805  if (auto *C = dyn_cast<Constant>(StoredVal))
806  if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL))
807  StoredVal = FoldedStoredVal;
808 
809  return StoredVal;
810 }
811 
812 /// This function is called when we have a
813 /// memdep query of a load that ends up being a clobbering memory write (store,
814 /// memset, memcpy, memmove). This means that the write *may* provide bits used
815 /// by the load but we can't be sure because the pointers don't mustalias.
816 ///
817 /// Check this case to see if there is anything more we can do before we give
818 /// up. This returns -1 if we have to give up, or a byte number in the stored
819 /// value of the piece that feeds the load.
820 static int AnalyzeLoadFromClobberingWrite(Type *LoadTy, Value *LoadPtr,
821  Value *WritePtr,
822  uint64_t WriteSizeInBits,
823  const DataLayout &DL) {
824  // If the loaded or stored value is a first class array or struct, don't try
825  // to transform them. We need to be able to bitcast to integer.
826  if (LoadTy->isStructTy() || LoadTy->isArrayTy())
827  return -1;
828 
829  int64_t StoreOffset = 0, LoadOffset = 0;
830  Value *StoreBase =
831  GetPointerBaseWithConstantOffset(WritePtr, StoreOffset, DL);
832  Value *LoadBase = GetPointerBaseWithConstantOffset(LoadPtr, LoadOffset, DL);
833  if (StoreBase != LoadBase)
834  return -1;
835 
836  // If the load and store are to the exact same address, they should have been
837  // a must alias. AA must have gotten confused.
838  // FIXME: Study to see if/when this happens. One case is forwarding a memset
839  // to a load from the base of the memset.
840 
841  // If the load and store don't overlap at all, the store doesn't provide
842  // anything to the load. In this case, they really don't alias at all, AA
843  // must have gotten confused.
844  uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy);
845 
846  if ((WriteSizeInBits & 7) | (LoadSize & 7))
847  return -1;
848  uint64_t StoreSize = WriteSizeInBits / 8; // Convert to bytes.
849  LoadSize /= 8;
850 
851 
852  bool isAAFailure = false;
853  if (StoreOffset < LoadOffset)
854  isAAFailure = StoreOffset+int64_t(StoreSize) <= LoadOffset;
855  else
856  isAAFailure = LoadOffset+int64_t(LoadSize) <= StoreOffset;
857 
858  if (isAAFailure)
859  return -1;
860 
861  // If the Load isn't completely contained within the stored bits, we don't
862  // have all the bits to feed it. We could do something crazy in the future
863  // (issue a smaller load then merge the bits in) but this seems unlikely to be
864  // valuable.
865  if (StoreOffset > LoadOffset ||
866  StoreOffset+StoreSize < LoadOffset+LoadSize)
867  return -1;
868 
869  // Okay, we can do this transformation. Return the number of bytes into the
870  // store that the load is.
871  return LoadOffset-StoreOffset;
872 }
873 
874 /// This function is called when we have a
875 /// memdep query of a load that ends up being a clobbering store.
876 static int AnalyzeLoadFromClobberingStore(Type *LoadTy, Value *LoadPtr,
877  StoreInst *DepSI) {
878  // Cannot handle reading from store of first-class aggregate yet.
879  if (DepSI->getValueOperand()->getType()->isStructTy() ||
880  DepSI->getValueOperand()->getType()->isArrayTy())
881  return -1;
882 
883  const DataLayout &DL = DepSI->getModule()->getDataLayout();
884  Value *StorePtr = DepSI->getPointerOperand();
885  uint64_t StoreSize =DL.getTypeSizeInBits(DepSI->getValueOperand()->getType());
886  return AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr,
887  StorePtr, StoreSize, DL);
888 }
889 
890 /// This function is called when we have a
891 /// memdep query of a load that ends up being clobbered by another load. See if
892 /// the other load can feed into the second load.
893 static int AnalyzeLoadFromClobberingLoad(Type *LoadTy, Value *LoadPtr,
894  LoadInst *DepLI, const DataLayout &DL){
895  // Cannot handle reading from store of first-class aggregate yet.
896  if (DepLI->getType()->isStructTy() || DepLI->getType()->isArrayTy())
897  return -1;
898 
899  Value *DepPtr = DepLI->getPointerOperand();
900  uint64_t DepSize = DL.getTypeSizeInBits(DepLI->getType());
901  int R = AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr, DepPtr, DepSize, DL);
902  if (R != -1) return R;
903 
904  // If we have a load/load clobber an DepLI can be widened to cover this load,
905  // then we should widen it!
906  int64_t LoadOffs = 0;
907  const Value *LoadBase =
908  GetPointerBaseWithConstantOffset(LoadPtr, LoadOffs, DL);
909  unsigned LoadSize = DL.getTypeStoreSize(LoadTy);
910 
912  LoadBase, LoadOffs, LoadSize, DepLI);
913  if (Size == 0) return -1;
914 
915  // Check non-obvious conditions enforced by MDA which we rely on for being
916  // able to materialize this potentially available value
917  assert(DepLI->isSimple() && "Cannot widen volatile/atomic load!");
918  assert(DepLI->getType()->isIntegerTy() && "Can't widen non-integer load");
919 
920  return AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr, DepPtr, Size*8, DL);
921 }
922 
923 
924 
925 static int AnalyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr,
926  MemIntrinsic *MI,
927  const DataLayout &DL) {
928  // If the mem operation is a non-constant size, we can't handle it.
929  ConstantInt *SizeCst = dyn_cast<ConstantInt>(MI->getLength());
930  if (!SizeCst) return -1;
931  uint64_t MemSizeInBits = SizeCst->getZExtValue()*8;
932 
933  // If this is memset, we just need to see if the offset is valid in the size
934  // of the memset..
935  if (MI->getIntrinsicID() == Intrinsic::memset)
936  return AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr, MI->getDest(),
937  MemSizeInBits, DL);
938 
939  // If we have a memcpy/memmove, the only case we can handle is if this is a
940  // copy from constant memory. In that case, we can read directly from the
941  // constant memory.
942  MemTransferInst *MTI = cast<MemTransferInst>(MI);
943 
944  Constant *Src = dyn_cast<Constant>(MTI->getSource());
945  if (!Src) return -1;
946 
948  if (!GV || !GV->isConstant()) return -1;
949 
950  // See if the access is within the bounds of the transfer.
951  int Offset = AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr,
952  MI->getDest(), MemSizeInBits, DL);
953  if (Offset == -1)
954  return Offset;
955 
956  unsigned AS = Src->getType()->getPointerAddressSpace();
957  // Otherwise, see if we can constant fold a load from the constant with the
958  // offset applied as appropriate.
959  Src = ConstantExpr::getBitCast(Src,
960  Type::getInt8PtrTy(Src->getContext(), AS));
961  Constant *OffsetCst =
962  ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset);
963  Src = ConstantExpr::getGetElementPtr(Type::getInt8Ty(Src->getContext()), Src,
964  OffsetCst);
965  Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS));
966  if (ConstantFoldLoadFromConstPtr(Src, LoadTy, DL))
967  return Offset;
968  return -1;
969 }
970 
971 
972 /// This function is called when we have a
973 /// memdep query of a load that ends up being a clobbering store. This means
974 /// that the store provides bits used by the load but we the pointers don't
975 /// mustalias. Check this case to see if there is anything more we can do
976 /// before we give up.
977 static Value *GetStoreValueForLoad(Value *SrcVal, unsigned Offset,
978  Type *LoadTy,
979  Instruction *InsertPt, const DataLayout &DL){
980  LLVMContext &Ctx = SrcVal->getType()->getContext();
981 
982  uint64_t StoreSize = (DL.getTypeSizeInBits(SrcVal->getType()) + 7) / 8;
983  uint64_t LoadSize = (DL.getTypeSizeInBits(LoadTy) + 7) / 8;
984 
985  IRBuilder<> Builder(InsertPt);
986 
987  // Compute which bits of the stored value are being used by the load. Convert
988  // to an integer type to start with.
989  if (SrcVal->getType()->getScalarType()->isPointerTy())
990  SrcVal = Builder.CreatePtrToInt(SrcVal,
991  DL.getIntPtrType(SrcVal->getType()));
992  if (!SrcVal->getType()->isIntegerTy())
993  SrcVal = Builder.CreateBitCast(SrcVal, IntegerType::get(Ctx, StoreSize*8));
994 
995  // Shift the bits to the least significant depending on endianness.
996  unsigned ShiftAmt;
997  if (DL.isLittleEndian())
998  ShiftAmt = Offset*8;
999  else
1000  ShiftAmt = (StoreSize-LoadSize-Offset)*8;
1001 
1002  if (ShiftAmt)
1003  SrcVal = Builder.CreateLShr(SrcVal, ShiftAmt);
1004 
1005  if (LoadSize != StoreSize)
1006  SrcVal = Builder.CreateTrunc(SrcVal, IntegerType::get(Ctx, LoadSize*8));
1007 
1008  return CoerceAvailableValueToLoadType(SrcVal, LoadTy, Builder, DL);
1009 }
1010 
1011 /// This function is called when we have a
1012 /// memdep query of a load that ends up being a clobbering load. This means
1013 /// that the load *may* provide bits used by the load but we can't be sure
1014 /// because the pointers don't mustalias. Check this case to see if there is
1015 /// anything more we can do before we give up.
1016 static Value *GetLoadValueForLoad(LoadInst *SrcVal, unsigned Offset,
1017  Type *LoadTy, Instruction *InsertPt,
1018  GVN &gvn) {
1019  const DataLayout &DL = SrcVal->getModule()->getDataLayout();
1020  // If Offset+LoadTy exceeds the size of SrcVal, then we must be wanting to
1021  // widen SrcVal out to a larger load.
1022  unsigned SrcValStoreSize = DL.getTypeStoreSize(SrcVal->getType());
1023  unsigned LoadSize = DL.getTypeStoreSize(LoadTy);
1024  if (Offset+LoadSize > SrcValStoreSize) {
1025  assert(SrcVal->isSimple() && "Cannot widen volatile/atomic load!");
1026  assert(SrcVal->getType()->isIntegerTy() && "Can't widen non-integer load");
1027  // If we have a load/load clobber an DepLI can be widened to cover this
1028  // load, then we should widen it to the next power of 2 size big enough!
1029  unsigned NewLoadSize = Offset+LoadSize;
1030  if (!isPowerOf2_32(NewLoadSize))
1031  NewLoadSize = NextPowerOf2(NewLoadSize);
1032 
1033  Value *PtrVal = SrcVal->getPointerOperand();
1034 
1035  // Insert the new load after the old load. This ensures that subsequent
1036  // memdep queries will find the new load. We can't easily remove the old
1037  // load completely because it is already in the value numbering table.
1038  IRBuilder<> Builder(SrcVal->getParent(), ++BasicBlock::iterator(SrcVal));
1039  Type *DestPTy =
1040  IntegerType::get(LoadTy->getContext(), NewLoadSize*8);
1041  DestPTy = PointerType::get(DestPTy,
1042  PtrVal->getType()->getPointerAddressSpace());
1043  Builder.SetCurrentDebugLocation(SrcVal->getDebugLoc());
1044  PtrVal = Builder.CreateBitCast(PtrVal, DestPTy);
1045  LoadInst *NewLoad = Builder.CreateLoad(PtrVal);
1046  NewLoad->takeName(SrcVal);
1047  NewLoad->setAlignment(SrcVal->getAlignment());
1048 
1049  DEBUG(dbgs() << "GVN WIDENED LOAD: " << *SrcVal << "\n");
1050  DEBUG(dbgs() << "TO: " << *NewLoad << "\n");
1051 
1052  // Replace uses of the original load with the wider load. On a big endian
1053  // system, we need to shift down to get the relevant bits.
1054  Value *RV = NewLoad;
1055  if (DL.isBigEndian())
1056  RV = Builder.CreateLShr(RV, (NewLoadSize - SrcValStoreSize) * 8);
1057  RV = Builder.CreateTrunc(RV, SrcVal->getType());
1058  SrcVal->replaceAllUsesWith(RV);
1059 
1060  // We would like to use gvn.markInstructionForDeletion here, but we can't
1061  // because the load is already memoized into the leader map table that GVN
1062  // tracks. It is potentially possible to remove the load from the table,
1063  // but then there all of the operations based on it would need to be
1064  // rehashed. Just leave the dead load around.
1065  gvn.getMemDep().removeInstruction(SrcVal);
1066  SrcVal = NewLoad;
1067  }
1068 
1069  return GetStoreValueForLoad(SrcVal, Offset, LoadTy, InsertPt, DL);
1070 }
1071 
1072 
1073 /// This function is called when we have a
1074 /// memdep query of a load that ends up being a clobbering mem intrinsic.
1076  Type *LoadTy, Instruction *InsertPt,
1077  const DataLayout &DL){
1078  LLVMContext &Ctx = LoadTy->getContext();
1079  uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy)/8;
1080 
1081  IRBuilder<> Builder(InsertPt);
1082 
1083  // We know that this method is only called when the mem transfer fully
1084  // provides the bits for the load.
1085  if (MemSetInst *MSI = dyn_cast<MemSetInst>(SrcInst)) {
1086  // memset(P, 'x', 1234) -> splat('x'), even if x is a variable, and
1087  // independently of what the offset is.
1088  Value *Val = MSI->getValue();
1089  if (LoadSize != 1)
1090  Val = Builder.CreateZExt(Val, IntegerType::get(Ctx, LoadSize*8));
1091 
1092  Value *OneElt = Val;
1093 
1094  // Splat the value out to the right number of bits.
1095  for (unsigned NumBytesSet = 1; NumBytesSet != LoadSize; ) {
1096  // If we can double the number of bytes set, do it.
1097  if (NumBytesSet*2 <= LoadSize) {
1098  Value *ShVal = Builder.CreateShl(Val, NumBytesSet*8);
1099  Val = Builder.CreateOr(Val, ShVal);
1100  NumBytesSet <<= 1;
1101  continue;
1102  }
1103 
1104  // Otherwise insert one byte at a time.
1105  Value *ShVal = Builder.CreateShl(Val, 1*8);
1106  Val = Builder.CreateOr(OneElt, ShVal);
1107  ++NumBytesSet;
1108  }
1109 
1110  return CoerceAvailableValueToLoadType(Val, LoadTy, Builder, DL);
1111  }
1112 
1113  // Otherwise, this is a memcpy/memmove from a constant global.
1114  MemTransferInst *MTI = cast<MemTransferInst>(SrcInst);
1115  Constant *Src = cast<Constant>(MTI->getSource());
1116  unsigned AS = Src->getType()->getPointerAddressSpace();
1117 
1118  // Otherwise, see if we can constant fold a load from the constant with the
1119  // offset applied as appropriate.
1120  Src = ConstantExpr::getBitCast(Src,
1121  Type::getInt8PtrTy(Src->getContext(), AS));
1122  Constant *OffsetCst =
1123  ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset);
1124  Src = ConstantExpr::getGetElementPtr(Type::getInt8Ty(Src->getContext()), Src,
1125  OffsetCst);
1126  Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS));
1127  return ConstantFoldLoadFromConstPtr(Src, LoadTy, DL);
1128 }
1129 
1130 
1131 /// Given a set of loads specified by ValuesPerBlock,
1132 /// construct SSA form, allowing us to eliminate LI. This returns the value
1133 /// that should be used at LI's definition site.
1136  GVN &gvn) {
1137  // Check for the fully redundant, dominating load case. In this case, we can
1138  // just use the dominating value directly.
1139  if (ValuesPerBlock.size() == 1 &&
1140  gvn.getDominatorTree().properlyDominates(ValuesPerBlock[0].BB,
1141  LI->getParent())) {
1142  assert(!ValuesPerBlock[0].AV.isUndefValue() &&
1143  "Dead BB dominate this block");
1144  return ValuesPerBlock[0].MaterializeAdjustedValue(LI, gvn);
1145  }
1146 
1147  // Otherwise, we have to construct SSA form.
1148  SmallVector<PHINode*, 8> NewPHIs;
1149  SSAUpdater SSAUpdate(&NewPHIs);
1150  SSAUpdate.Initialize(LI->getType(), LI->getName());
1151 
1152  for (const AvailableValueInBlock &AV : ValuesPerBlock) {
1153  BasicBlock *BB = AV.BB;
1154 
1155  if (SSAUpdate.HasValueForBlock(BB))
1156  continue;
1157 
1158  SSAUpdate.AddAvailableValue(BB, AV.MaterializeAdjustedValue(LI, gvn));
1159  }
1160 
1161  // Perform PHI construction.
1162  return SSAUpdate.GetValueInMiddleOfBlock(LI->getParent());
1163 }
1164 
1165 Value *AvailableValue::MaterializeAdjustedValue(LoadInst *LI,
1166  Instruction *InsertPt,
1167  GVN &gvn) const {
1168  Value *Res;
1169  Type *LoadTy = LI->getType();
1170  const DataLayout &DL = LI->getModule()->getDataLayout();
1171  if (isSimpleValue()) {
1172  Res = getSimpleValue();
1173  if (Res->getType() != LoadTy) {
1174  Res = GetStoreValueForLoad(Res, Offset, LoadTy, InsertPt, DL);
1175 
1176  DEBUG(dbgs() << "GVN COERCED NONLOCAL VAL:\nOffset: " << Offset << " "
1177  << *getSimpleValue() << '\n'
1178  << *Res << '\n' << "\n\n\n");
1179  }
1180  } else if (isCoercedLoadValue()) {
1181  LoadInst *Load = getCoercedLoadValue();
1182  if (Load->getType() == LoadTy && Offset == 0) {
1183  Res = Load;
1184  } else {
1185  Res = GetLoadValueForLoad(Load, Offset, LoadTy, InsertPt, gvn);
1186 
1187  DEBUG(dbgs() << "GVN COERCED NONLOCAL LOAD:\nOffset: " << Offset << " "
1188  << *getCoercedLoadValue() << '\n'
1189  << *Res << '\n' << "\n\n\n");
1190  }
1191  } else if (isMemIntrinValue()) {
1192  Res = GetMemInstValueForLoad(getMemIntrinValue(), Offset, LoadTy,
1193  InsertPt, DL);
1194  DEBUG(dbgs() << "GVN COERCED NONLOCAL MEM INTRIN:\nOffset: " << Offset
1195  << " " << *getMemIntrinValue() << '\n'
1196  << *Res << '\n' << "\n\n\n");
1197  } else {
1198  assert(isUndefValue() && "Should be UndefVal");
1199  DEBUG(dbgs() << "GVN COERCED NONLOCAL Undef:\n";);
1200  return UndefValue::get(LoadTy);
1201  }
1202  assert(Res && "failed to materialize?");
1203  return Res;
1204 }
1205 
1206 static bool isLifetimeStart(const Instruction *Inst) {
1207  if (const IntrinsicInst* II = dyn_cast<IntrinsicInst>(Inst))
1208  return II->getIntrinsicID() == Intrinsic::lifetime_start;
1209  return false;
1210 }
1211 
1212 /// \brief Try to locate the three instruction involved in a missed
1213 /// load-elimination case that is due to an intervening store.
1215  DominatorTree *DT,
1217  using namespace ore;
1218  User *OtherAccess = nullptr;
1219 
1220  OptimizationRemarkMissed R(DEBUG_TYPE, "LoadClobbered", LI);
1221  R << "load of type " << NV("Type", LI->getType()) << " not eliminated"
1222  << setExtraArgs();
1223 
1224  for (auto *U : LI->getPointerOperand()->users())
1225  if (U != LI && (isa<LoadInst>(U) || isa<StoreInst>(U)) &&
1226  DT->dominates(cast<Instruction>(U), LI)) {
1227  // FIXME: for now give up if there are multiple memory accesses that
1228  // dominate the load. We need further analysis to decide which one is
1229  // that we're forwarding from.
1230  if (OtherAccess)
1231  OtherAccess = nullptr;
1232  else
1233  OtherAccess = U;
1234  }
1235 
1236  if (OtherAccess)
1237  R << " in favor of " << NV("OtherAccess", OtherAccess);
1238 
1239  R << " because it is clobbered by " << NV("ClobberedBy", DepInfo.getInst());
1240 
1241  ORE->emit(R);
1242 }
1243 
1244 bool GVN::AnalyzeLoadAvailability(LoadInst *LI, MemDepResult DepInfo,
1245  Value *Address, AvailableValue &Res) {
1246 
1247  assert((DepInfo.isDef() || DepInfo.isClobber()) &&
1248  "expected a local dependence");
1249  assert(LI->isUnordered() && "rules below are incorrect for ordered access");
1250 
1251  const DataLayout &DL = LI->getModule()->getDataLayout();
1252 
1253  if (DepInfo.isClobber()) {
1254  // If the dependence is to a store that writes to a superset of the bits
1255  // read by the load, we can extract the bits we need for the load from the
1256  // stored value.
1257  if (StoreInst *DepSI = dyn_cast<StoreInst>(DepInfo.getInst())) {
1258  // Can't forward from non-atomic to atomic without violating memory model.
1259  if (Address && LI->isAtomic() <= DepSI->isAtomic()) {
1260  int Offset =
1262  if (Offset != -1) {
1263  Res = AvailableValue::get(DepSI->getValueOperand(), Offset);
1264  return true;
1265  }
1266  }
1267  }
1268 
1269  // Check to see if we have something like this:
1270  // load i32* P
1271  // load i8* (P+1)
1272  // if we have this, replace the later with an extraction from the former.
1273  if (LoadInst *DepLI = dyn_cast<LoadInst>(DepInfo.getInst())) {
1274  // If this is a clobber and L is the first instruction in its block, then
1275  // we have the first instruction in the entry block.
1276  // Can't forward from non-atomic to atomic without violating memory model.
1277  if (DepLI != LI && Address && LI->isAtomic() <= DepLI->isAtomic()) {
1278  int Offset =
1279  AnalyzeLoadFromClobberingLoad(LI->getType(), Address, DepLI, DL);
1280 
1281  if (Offset != -1) {
1282  Res = AvailableValue::getLoad(DepLI, Offset);
1283  return true;
1284  }
1285  }
1286  }
1287 
1288  // If the clobbering value is a memset/memcpy/memmove, see if we can
1289  // forward a value on from it.
1290  if (MemIntrinsic *DepMI = dyn_cast<MemIntrinsic>(DepInfo.getInst())) {
1291  if (Address && !LI->isAtomic()) {
1292  int Offset = AnalyzeLoadFromClobberingMemInst(LI->getType(), Address,
1293  DepMI, DL);
1294  if (Offset != -1) {
1295  Res = AvailableValue::getMI(DepMI, Offset);
1296  return true;
1297  }
1298  }
1299  }
1300  // Nothing known about this clobber, have to be conservative
1301  DEBUG(
1302  // fast print dep, using operator<< on instruction is too slow.
1303  dbgs() << "GVN: load ";
1304  LI->printAsOperand(dbgs());
1305  Instruction *I = DepInfo.getInst();
1306  dbgs() << " is clobbered by " << *I << '\n';
1307  );
1308 
1309  if (ORE->allowExtraAnalysis())
1310  reportMayClobberedLoad(LI, DepInfo, DT, ORE);
1311 
1312  return false;
1313  }
1314  assert(DepInfo.isDef() && "follows from above");
1315 
1316  Instruction *DepInst = DepInfo.getInst();
1317 
1318  // Loading the allocation -> undef.
1319  if (isa<AllocaInst>(DepInst) || isMallocLikeFn(DepInst, TLI) ||
1320  // Loading immediately after lifetime begin -> undef.
1321  isLifetimeStart(DepInst)) {
1322  Res = AvailableValue::get(UndefValue::get(LI->getType()));
1323  return true;
1324  }
1325 
1326  // Loading from calloc (which zero initializes memory) -> zero
1327  if (isCallocLikeFn(DepInst, TLI)) {
1328  Res = AvailableValue::get(Constant::getNullValue(LI->getType()));
1329  return true;
1330  }
1331 
1332  if (StoreInst *S = dyn_cast<StoreInst>(DepInst)) {
1333  // Reject loads and stores that are to the same address but are of
1334  // different types if we have to. If the stored value is larger or equal to
1335  // the loaded value, we can reuse it.
1336  if (S->getValueOperand()->getType() != LI->getType() &&
1337  !CanCoerceMustAliasedValueToLoad(S->getValueOperand(),
1338  LI->getType(), DL))
1339  return false;
1340 
1341  // Can't forward from non-atomic to atomic without violating memory model.
1342  if (S->isAtomic() < LI->isAtomic())
1343  return false;
1344 
1345  Res = AvailableValue::get(S->getValueOperand());
1346  return true;
1347  }
1348 
1349  if (LoadInst *LD = dyn_cast<LoadInst>(DepInst)) {
1350  // If the types mismatch and we can't handle it, reject reuse of the load.
1351  // If the stored value is larger or equal to the loaded value, we can reuse
1352  // it.
1353  if (LD->getType() != LI->getType() &&
1355  return false;
1356 
1357  // Can't forward from non-atomic to atomic without violating memory model.
1358  if (LD->isAtomic() < LI->isAtomic())
1359  return false;
1360 
1361  Res = AvailableValue::getLoad(LD);
1362  return true;
1363  }
1364 
1365  // Unknown def - must be conservative
1366  DEBUG(
1367  // fast print dep, using operator<< on instruction is too slow.
1368  dbgs() << "GVN: load ";
1369  LI->printAsOperand(dbgs());
1370  dbgs() << " has unknown def " << *DepInst << '\n';
1371  );
1372  return false;
1373 }
1374 
1375 void GVN::AnalyzeLoadAvailability(LoadInst *LI, LoadDepVect &Deps,
1376  AvailValInBlkVect &ValuesPerBlock,
1377  UnavailBlkVect &UnavailableBlocks) {
1378 
1379  // Filter out useless results (non-locals, etc). Keep track of the blocks
1380  // where we have a value available in repl, also keep track of whether we see
1381  // dependencies that produce an unknown value for the load (such as a call
1382  // that could potentially clobber the load).
1383  unsigned NumDeps = Deps.size();
1384  for (unsigned i = 0, e = NumDeps; i != e; ++i) {
1385  BasicBlock *DepBB = Deps[i].getBB();
1386  MemDepResult DepInfo = Deps[i].getResult();
1387 
1388  if (DeadBlocks.count(DepBB)) {
1389  // Dead dependent mem-op disguise as a load evaluating the same value
1390  // as the load in question.
1391  ValuesPerBlock.push_back(AvailableValueInBlock::getUndef(DepBB));
1392  continue;
1393  }
1394 
1395  if (!DepInfo.isDef() && !DepInfo.isClobber()) {
1396  UnavailableBlocks.push_back(DepBB);
1397  continue;
1398  }
1399 
1400  // The address being loaded in this non-local block may not be the same as
1401  // the pointer operand of the load if PHI translation occurs. Make sure
1402  // to consider the right address.
1403  Value *Address = Deps[i].getAddress();
1404 
1405  AvailableValue AV;
1406  if (AnalyzeLoadAvailability(LI, DepInfo, Address, AV)) {
1407  // subtlety: because we know this was a non-local dependency, we know
1408  // it's safe to materialize anywhere between the instruction within
1409  // DepInfo and the end of it's block.
1410  ValuesPerBlock.push_back(AvailableValueInBlock::get(DepBB,
1411  std::move(AV)));
1412  } else {
1413  UnavailableBlocks.push_back(DepBB);
1414  }
1415  }
1416 
1417  assert(NumDeps == ValuesPerBlock.size() + UnavailableBlocks.size() &&
1418  "post condition violation");
1419 }
1420 
1421 bool GVN::PerformLoadPRE(LoadInst *LI, AvailValInBlkVect &ValuesPerBlock,
1422  UnavailBlkVect &UnavailableBlocks) {
1423  // Okay, we have *some* definitions of the value. This means that the value
1424  // is available in some of our (transitive) predecessors. Lets think about
1425  // doing PRE of this load. This will involve inserting a new load into the
1426  // predecessor when it's not available. We could do this in general, but
1427  // prefer to not increase code size. As such, we only do this when we know
1428  // that we only have to insert *one* load (which means we're basically moving
1429  // the load, not inserting a new one).
1430 
1431  SmallPtrSet<BasicBlock *, 4> Blockers(UnavailableBlocks.begin(),
1432  UnavailableBlocks.end());
1433 
1434  // Let's find the first basic block with more than one predecessor. Walk
1435  // backwards through predecessors if needed.
1436  BasicBlock *LoadBB = LI->getParent();
1437  BasicBlock *TmpBB = LoadBB;
1438 
1439  while (TmpBB->getSinglePredecessor()) {
1440  TmpBB = TmpBB->getSinglePredecessor();
1441  if (TmpBB == LoadBB) // Infinite (unreachable) loop.
1442  return false;
1443  if (Blockers.count(TmpBB))
1444  return false;
1445 
1446  // If any of these blocks has more than one successor (i.e. if the edge we
1447  // just traversed was critical), then there are other paths through this
1448  // block along which the load may not be anticipated. Hoisting the load
1449  // above this block would be adding the load to execution paths along
1450  // which it was not previously executed.
1451  if (TmpBB->getTerminator()->getNumSuccessors() != 1)
1452  return false;
1453  }
1454 
1455  assert(TmpBB);
1456  LoadBB = TmpBB;
1457 
1458  // Check to see how many predecessors have the loaded value fully
1459  // available.
1461  DenseMap<BasicBlock*, char> FullyAvailableBlocks;
1462  for (const AvailableValueInBlock &AV : ValuesPerBlock)
1463  FullyAvailableBlocks[AV.BB] = true;
1464  for (BasicBlock *UnavailableBB : UnavailableBlocks)
1465  FullyAvailableBlocks[UnavailableBB] = false;
1466 
1467  SmallVector<BasicBlock *, 4> CriticalEdgePred;
1468  for (BasicBlock *Pred : predecessors(LoadBB)) {
1469  // If any predecessor block is an EH pad that does not allow non-PHI
1470  // instructions before the terminator, we can't PRE the load.
1471  if (Pred->getTerminator()->isEHPad()) {
1472  DEBUG(dbgs()
1473  << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD PREDECESSOR '"
1474  << Pred->getName() << "': " << *LI << '\n');
1475  return false;
1476  }
1477 
1478  if (IsValueFullyAvailableInBlock(Pred, FullyAvailableBlocks, 0)) {
1479  continue;
1480  }
1481 
1482  if (Pred->getTerminator()->getNumSuccessors() != 1) {
1483  if (isa<IndirectBrInst>(Pred->getTerminator())) {
1484  DEBUG(dbgs() << "COULD NOT PRE LOAD BECAUSE OF INDBR CRITICAL EDGE '"
1485  << Pred->getName() << "': " << *LI << '\n');
1486  return false;
1487  }
1488 
1489  if (LoadBB->isEHPad()) {
1490  DEBUG(dbgs()
1491  << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD CRITICAL EDGE '"
1492  << Pred->getName() << "': " << *LI << '\n');
1493  return false;
1494  }
1495 
1496  CriticalEdgePred.push_back(Pred);
1497  } else {
1498  // Only add the predecessors that will not be split for now.
1499  PredLoads[Pred] = nullptr;
1500  }
1501  }
1502 
1503  // Decide whether PRE is profitable for this load.
1504  unsigned NumUnavailablePreds = PredLoads.size() + CriticalEdgePred.size();
1505  assert(NumUnavailablePreds != 0 &&
1506  "Fully available value should already be eliminated!");
1507 
1508  // If this load is unavailable in multiple predecessors, reject it.
1509  // FIXME: If we could restructure the CFG, we could make a common pred with
1510  // all the preds that don't have an available LI and insert a new load into
1511  // that one block.
1512  if (NumUnavailablePreds != 1)
1513  return false;
1514 
1515  // Split critical edges, and update the unavailable predecessors accordingly.
1516  for (BasicBlock *OrigPred : CriticalEdgePred) {
1517  BasicBlock *NewPred = splitCriticalEdges(OrigPred, LoadBB);
1518  assert(!PredLoads.count(OrigPred) && "Split edges shouldn't be in map!");
1519  PredLoads[NewPred] = nullptr;
1520  DEBUG(dbgs() << "Split critical edge " << OrigPred->getName() << "->"
1521  << LoadBB->getName() << '\n');
1522  }
1523 
1524  // Check if the load can safely be moved to all the unavailable predecessors.
1525  bool CanDoPRE = true;
1526  const DataLayout &DL = LI->getModule()->getDataLayout();
1528  for (auto &PredLoad : PredLoads) {
1529  BasicBlock *UnavailablePred = PredLoad.first;
1530 
1531  // Do PHI translation to get its value in the predecessor if necessary. The
1532  // returned pointer (if non-null) is guaranteed to dominate UnavailablePred.
1533 
1534  // If all preds have a single successor, then we know it is safe to insert
1535  // the load on the pred (?!?), so we can insert code to materialize the
1536  // pointer if it is not available.
1537  PHITransAddr Address(LI->getPointerOperand(), DL, AC);
1538  Value *LoadPtr = nullptr;
1539  LoadPtr = Address.PHITranslateWithInsertion(LoadBB, UnavailablePred,
1540  *DT, NewInsts);
1541 
1542  // If we couldn't find or insert a computation of this phi translated value,
1543  // we fail PRE.
1544  if (!LoadPtr) {
1545  DEBUG(dbgs() << "COULDN'T INSERT PHI TRANSLATED VALUE OF: "
1546  << *LI->getPointerOperand() << "\n");
1547  CanDoPRE = false;
1548  break;
1549  }
1550 
1551  PredLoad.second = LoadPtr;
1552  }
1553 
1554  if (!CanDoPRE) {
1555  while (!NewInsts.empty()) {
1556  Instruction *I = NewInsts.pop_back_val();
1557  if (MD) MD->removeInstruction(I);
1558  I->eraseFromParent();
1559  }
1560  // HINT: Don't revert the edge-splitting as following transformation may
1561  // also need to split these critical edges.
1562  return !CriticalEdgePred.empty();
1563  }
1564 
1565  // Okay, we can eliminate this load by inserting a reload in the predecessor
1566  // and using PHI construction to get the value in the other predecessors, do
1567  // it.
1568  DEBUG(dbgs() << "GVN REMOVING PRE LOAD: " << *LI << '\n');
1569  DEBUG(if (!NewInsts.empty())
1570  dbgs() << "INSERTED " << NewInsts.size() << " INSTS: "
1571  << *NewInsts.back() << '\n');
1572 
1573  // Assign value numbers to the new instructions.
1574  for (Instruction *I : NewInsts) {
1575  // Instructions that have been inserted in predecessor(s) to materialize
1576  // the load address do not retain their original debug locations. Doing
1577  // so could lead to confusing (but correct) source attributions.
1578  // FIXME: How do we retain source locations without causing poor debugging
1579  // behavior?
1580  I->setDebugLoc(DebugLoc());
1581 
1582  // FIXME: We really _ought_ to insert these value numbers into their
1583  // parent's availability map. However, in doing so, we risk getting into
1584  // ordering issues. If a block hasn't been processed yet, we would be
1585  // marking a value as AVAIL-IN, which isn't what we intend.
1586  VN.lookupOrAdd(I);
1587  }
1588 
1589  for (const auto &PredLoad : PredLoads) {
1590  BasicBlock *UnavailablePred = PredLoad.first;
1591  Value *LoadPtr = PredLoad.second;
1592 
1593  auto *NewLoad = new LoadInst(LoadPtr, LI->getName()+".pre",
1594  LI->isVolatile(), LI->getAlignment(),
1595  LI->getOrdering(), LI->getSynchScope(),
1596  UnavailablePred->getTerminator());
1597 
1598  // Transfer the old load's AA tags to the new load.
1599  AAMDNodes Tags;
1600  LI->getAAMetadata(Tags);
1601  if (Tags)
1602  NewLoad->setAAMetadata(Tags);
1603 
1604  if (auto *MD = LI->getMetadata(LLVMContext::MD_invariant_load))
1605  NewLoad->setMetadata(LLVMContext::MD_invariant_load, MD);
1606  if (auto *InvGroupMD = LI->getMetadata(LLVMContext::MD_invariant_group))
1607  NewLoad->setMetadata(LLVMContext::MD_invariant_group, InvGroupMD);
1608  if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range))
1609  NewLoad->setMetadata(LLVMContext::MD_range, RangeMD);
1610 
1611  // We do not propagate the old load's debug location, because the new
1612  // load now lives in a different BB, and we want to avoid a jumpy line
1613  // table.
1614  // FIXME: How do we retain source locations without causing poor debugging
1615  // behavior?
1616 
1617  // Add the newly created load.
1618  ValuesPerBlock.push_back(AvailableValueInBlock::get(UnavailablePred,
1619  NewLoad));
1620  MD->invalidateCachedPointerInfo(LoadPtr);
1621  DEBUG(dbgs() << "GVN INSERTED " << *NewLoad << '\n');
1622  }
1623 
1624  // Perform PHI construction.
1625  Value *V = ConstructSSAForLoadSet(LI, ValuesPerBlock, *this);
1626  LI->replaceAllUsesWith(V);
1627  if (isa<PHINode>(V))
1628  V->takeName(LI);
1629  if (Instruction *I = dyn_cast<Instruction>(V))
1630  I->setDebugLoc(LI->getDebugLoc());
1631  if (V->getType()->getScalarType()->isPointerTy())
1634  ORE->emit(OptimizationRemark(DEBUG_TYPE, "LoadPRE", LI)
1635  << "load eliminated by PRE");
1636  ++NumPRELoad;
1637  return true;
1638 }
1639 
1642  using namespace ore;
1643  ORE->emit(OptimizationRemark(DEBUG_TYPE, "LoadElim", LI)
1644  << "load of type " << NV("Type", LI->getType()) << " eliminated"
1645  << setExtraArgs() << " in favor of "
1646  << NV("InfavorOfValue", AvailableValue));
1647 }
1648 
1649 /// Attempt to eliminate a load whose dependencies are
1650 /// non-local by performing PHI construction.
1651 bool GVN::processNonLocalLoad(LoadInst *LI) {
1652  // non-local speculations are not allowed under asan.
1653  if (LI->getParent()->getParent()->hasFnAttribute(Attribute::SanitizeAddress))
1654  return false;
1655 
1656  // Step 1: Find the non-local dependencies of the load.
1657  LoadDepVect Deps;
1658  MD->getNonLocalPointerDependency(LI, Deps);
1659 
1660  // If we had to process more than one hundred blocks to find the
1661  // dependencies, this load isn't worth worrying about. Optimizing
1662  // it will be too expensive.
1663  unsigned NumDeps = Deps.size();
1664  if (NumDeps > 100)
1665  return false;
1666 
1667  // If we had a phi translation failure, we'll have a single entry which is a
1668  // clobber in the current block. Reject this early.
1669  if (NumDeps == 1 &&
1670  !Deps[0].getResult().isDef() && !Deps[0].getResult().isClobber()) {
1671  DEBUG(
1672  dbgs() << "GVN: non-local load ";
1673  LI->printAsOperand(dbgs());
1674  dbgs() << " has unknown dependencies\n";
1675  );
1676  return false;
1677  }
1678 
1679  // If this load follows a GEP, see if we can PRE the indices before analyzing.
1680  if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0))) {
1681  for (GetElementPtrInst::op_iterator OI = GEP->idx_begin(),
1682  OE = GEP->idx_end();
1683  OI != OE; ++OI)
1684  if (Instruction *I = dyn_cast<Instruction>(OI->get()))
1685  performScalarPRE(I);
1686  }
1687 
1688  // Step 2: Analyze the availability of the load
1689  AvailValInBlkVect ValuesPerBlock;
1690  UnavailBlkVect UnavailableBlocks;
1691  AnalyzeLoadAvailability(LI, Deps, ValuesPerBlock, UnavailableBlocks);
1692 
1693  // If we have no predecessors that produce a known value for this load, exit
1694  // early.
1695  if (ValuesPerBlock.empty())
1696  return false;
1697 
1698  // Step 3: Eliminate fully redundancy.
1699  //
1700  // If all of the instructions we depend on produce a known value for this
1701  // load, then it is fully redundant and we can use PHI insertion to compute
1702  // its value. Insert PHIs and remove the fully redundant value now.
1703  if (UnavailableBlocks.empty()) {
1704  DEBUG(dbgs() << "GVN REMOVING NONLOCAL LOAD: " << *LI << '\n');
1705 
1706  // Perform PHI construction.
1707  Value *V = ConstructSSAForLoadSet(LI, ValuesPerBlock, *this);
1708  LI->replaceAllUsesWith(V);
1709 
1710  if (isa<PHINode>(V))
1711  V->takeName(LI);
1712  if (Instruction *I = dyn_cast<Instruction>(V))
1713  // If instruction I has debug info, then we should not update it.
1714  // Also, if I has a null DebugLoc, then it is still potentially incorrect
1715  // to propagate LI's DebugLoc because LI may not post-dominate I.
1716  if (LI->getDebugLoc() && ValuesPerBlock.size() != 1)
1717  I->setDebugLoc(LI->getDebugLoc());
1718  if (V->getType()->getScalarType()->isPointerTy())
1721  ++NumGVNLoad;
1722  reportLoadElim(LI, V, ORE);
1723  return true;
1724  }
1725 
1726  // Step 4: Eliminate partial redundancy.
1727  if (!EnablePRE || !EnableLoadPRE)
1728  return false;
1729 
1730  return PerformLoadPRE(LI, ValuesPerBlock, UnavailableBlocks);
1731 }
1732 
1733 bool GVN::processAssumeIntrinsic(IntrinsicInst *IntrinsicI) {
1734  assert(IntrinsicI->getIntrinsicID() == Intrinsic::assume &&
1735  "This function can only be called with llvm.assume intrinsic");
1736  Value *V = IntrinsicI->getArgOperand(0);
1737 
1738  if (ConstantInt *Cond = dyn_cast<ConstantInt>(V)) {
1739  if (Cond->isZero()) {
1740  Type *Int8Ty = Type::getInt8Ty(V->getContext());
1741  // Insert a new store to null instruction before the load to indicate that
1742  // this code is not reachable. FIXME: We could insert unreachable
1743  // instruction directly because we can modify the CFG.
1744  new StoreInst(UndefValue::get(Int8Ty),
1746  IntrinsicI);
1747  }
1748  markInstructionForDeletion(IntrinsicI);
1749  return false;
1750  }
1751 
1752  Constant *True = ConstantInt::getTrue(V->getContext());
1753  bool Changed = false;
1754 
1755  for (BasicBlock *Successor : successors(IntrinsicI->getParent())) {
1756  BasicBlockEdge Edge(IntrinsicI->getParent(), Successor);
1757 
1758  // This property is only true in dominated successors, propagateEquality
1759  // will check dominance for us.
1760  Changed |= propagateEquality(V, True, Edge, false);
1761  }
1762 
1763  // We can replace assume value with true, which covers cases like this:
1764  // call void @llvm.assume(i1 %cmp)
1765  // br i1 %cmp, label %bb1, label %bb2 ; will change %cmp to true
1766  ReplaceWithConstMap[V] = True;
1767 
1768  // If one of *cmp *eq operand is const, adding it to map will cover this:
1769  // %cmp = fcmp oeq float 3.000000e+00, %0 ; const on lhs could happen
1770  // call void @llvm.assume(i1 %cmp)
1771  // ret float %0 ; will change it to ret float 3.000000e+00
1772  if (auto *CmpI = dyn_cast<CmpInst>(V)) {
1773  if (CmpI->getPredicate() == CmpInst::Predicate::ICMP_EQ ||
1774  CmpI->getPredicate() == CmpInst::Predicate::FCMP_OEQ ||
1775  (CmpI->getPredicate() == CmpInst::Predicate::FCMP_UEQ &&
1776  CmpI->getFastMathFlags().noNaNs())) {
1777  Value *CmpLHS = CmpI->getOperand(0);
1778  Value *CmpRHS = CmpI->getOperand(1);
1779  if (isa<Constant>(CmpLHS))
1780  std::swap(CmpLHS, CmpRHS);
1781  auto *RHSConst = dyn_cast<Constant>(CmpRHS);
1782 
1783  // If only one operand is constant.
1784  if (RHSConst != nullptr && !isa<Constant>(CmpLHS))
1785  ReplaceWithConstMap[CmpLHS] = RHSConst;
1786  }
1787  }
1788  return Changed;
1789 }
1790 
1792  auto *ReplInst = dyn_cast<Instruction>(Repl);
1793  if (!ReplInst)
1794  return;
1795 
1796  // Patch the replacement so that it is not more restrictive than the value
1797  // being replaced.
1798  // Note that if 'I' is a load being replaced by some operation,
1799  // for example, by an arithmetic operation, then andIRFlags()
1800  // would just erase all math flags from the original arithmetic
1801  // operation, which is clearly not wanted and not needed.
1802  if (!isa<LoadInst>(I))
1803  ReplInst->andIRFlags(I);
1804 
1805  // FIXME: If both the original and replacement value are part of the
1806  // same control-flow region (meaning that the execution of one
1807  // guarantees the execution of the other), then we can combine the
1808  // noalias scopes here and do better than the general conservative
1809  // answer used in combineMetadata().
1810 
1811  // In general, GVN unifies expressions over different control-flow
1812  // regions, and so we need a conservative combination of the noalias
1813  // scopes.
1814  static const unsigned KnownIDs[] = {
1819  combineMetadata(ReplInst, I, KnownIDs);
1820 }
1821 
1823  patchReplacementInstruction(I, Repl);
1824  I->replaceAllUsesWith(Repl);
1825 }
1826 
1827 /// Attempt to eliminate a load, first by eliminating it
1828 /// locally, and then attempting non-local elimination if that fails.
1829 bool GVN::processLoad(LoadInst *L) {
1830  if (!MD)
1831  return false;
1832 
1833  // This code hasn't been audited for ordered or volatile memory access
1834  if (!L->isUnordered())
1835  return false;
1836 
1837  if (L->use_empty()) {
1839  return true;
1840  }
1841 
1842  // ... to a pointer that has been loaded from before...
1843  MemDepResult Dep = MD->getDependency(L);
1844 
1845  // If it is defined in another block, try harder.
1846  if (Dep.isNonLocal())
1847  return processNonLocalLoad(L);
1848 
1849  // Only handle the local case below
1850  if (!Dep.isDef() && !Dep.isClobber()) {
1851  // This might be a NonFuncLocal or an Unknown
1852  DEBUG(
1853  // fast print dep, using operator<< on instruction is too slow.
1854  dbgs() << "GVN: load ";
1855  L->printAsOperand(dbgs());
1856  dbgs() << " has unknown dependence\n";
1857  );
1858  return false;
1859  }
1860 
1861  AvailableValue AV;
1862  if (AnalyzeLoadAvailability(L, Dep, L->getPointerOperand(), AV)) {
1863  Value *AvailableValue = AV.MaterializeAdjustedValue(L, L, *this);
1864 
1865  // Replace the load!
1866  patchAndReplaceAllUsesWith(L, AvailableValue);
1868  ++NumGVNLoad;
1869  reportLoadElim(L, AvailableValue, ORE);
1870  // Tell MDA to rexamine the reused pointer since we might have more
1871  // information after forwarding it.
1872  if (MD && AvailableValue->getType()->getScalarType()->isPointerTy())
1873  MD->invalidateCachedPointerInfo(AvailableValue);
1874  return true;
1875  }
1876 
1877  return false;
1878 }
1879 
1880 // In order to find a leader for a given value number at a
1881 // specific basic block, we first obtain the list of all Values for that number,
1882 // and then scan the list to find one whose block dominates the block in
1883 // question. This is fast because dominator tree queries consist of only
1884 // a few comparisons of DFS numbers.
1885 Value *GVN::findLeader(const BasicBlock *BB, uint32_t num) {
1886  LeaderTableEntry Vals = LeaderTable[num];
1887  if (!Vals.Val) return nullptr;
1888 
1889  Value *Val = nullptr;
1890  if (DT->dominates(Vals.BB, BB)) {
1891  Val = Vals.Val;
1892  if (isa<Constant>(Val)) return Val;
1893  }
1894 
1895  LeaderTableEntry* Next = Vals.Next;
1896  while (Next) {
1897  if (DT->dominates(Next->BB, BB)) {
1898  if (isa<Constant>(Next->Val)) return Next->Val;
1899  if (!Val) Val = Next->Val;
1900  }
1901 
1902  Next = Next->Next;
1903  }
1904 
1905  return Val;
1906 }
1907 
1908 /// There is an edge from 'Src' to 'Dst'. Return
1909 /// true if every path from the entry block to 'Dst' passes via this edge. In
1910 /// particular 'Dst' must not be reachable via another edge from 'Src'.
1912  DominatorTree *DT) {
1913  // While in theory it is interesting to consider the case in which Dst has
1914  // more than one predecessor, because Dst might be part of a loop which is
1915  // only reachable from Src, in practice it is pointless since at the time
1916  // GVN runs all such loops have preheaders, which means that Dst will have
1917  // been changed to have only one predecessor, namely Src.
1918  const BasicBlock *Pred = E.getEnd()->getSinglePredecessor();
1919  assert((!Pred || Pred == E.getStart()) &&
1920  "No edge between these basic blocks!");
1921  return Pred != nullptr;
1922 }
1923 
1924 // Tries to replace instruction with const, using information from
1925 // ReplaceWithConstMap.
1926 bool GVN::replaceOperandsWithConsts(Instruction *Instr) const {
1927  bool Changed = false;
1928  for (unsigned OpNum = 0; OpNum < Instr->getNumOperands(); ++OpNum) {
1929  Value *Operand = Instr->getOperand(OpNum);
1930  auto it = ReplaceWithConstMap.find(Operand);
1931  if (it != ReplaceWithConstMap.end()) {
1932  assert(!isa<Constant>(Operand) &&
1933  "Replacing constants with constants is invalid");
1934  DEBUG(dbgs() << "GVN replacing: " << *Operand << " with " << *it->second
1935  << " in instruction " << *Instr << '\n');
1936  Instr->setOperand(OpNum, it->second);
1937  Changed = true;
1938  }
1939  }
1940  return Changed;
1941 }
1942 
1943 /// The given values are known to be equal in every block
1944 /// dominated by 'Root'. Exploit this, for example by replacing 'LHS' with
1945 /// 'RHS' everywhere in the scope. Returns whether a change was made.
1946 /// If DominatesByEdge is false, then it means that we will propagate the RHS
1947 /// value starting from the end of Root.Start.
1948 bool GVN::propagateEquality(Value *LHS, Value *RHS, const BasicBlockEdge &Root,
1949  bool DominatesByEdge) {
1951  Worklist.push_back(std::make_pair(LHS, RHS));
1952  bool Changed = false;
1953  // For speed, compute a conservative fast approximation to
1954  // DT->dominates(Root, Root.getEnd());
1955  const bool RootDominatesEnd = isOnlyReachableViaThisEdge(Root, DT);
1956 
1957  while (!Worklist.empty()) {
1958  std::pair<Value*, Value*> Item = Worklist.pop_back_val();
1959  LHS = Item.first; RHS = Item.second;
1960 
1961  if (LHS == RHS)
1962  continue;
1963  assert(LHS->getType() == RHS->getType() && "Equality but unequal types!");
1964 
1965  // Don't try to propagate equalities between constants.
1966  if (isa<Constant>(LHS) && isa<Constant>(RHS))
1967  continue;
1968 
1969  // Prefer a constant on the right-hand side, or an Argument if no constants.
1970  if (isa<Constant>(LHS) || (isa<Argument>(LHS) && !isa<Constant>(RHS)))
1971  std::swap(LHS, RHS);
1972  assert((isa<Argument>(LHS) || isa<Instruction>(LHS)) && "Unexpected value!");
1973 
1974  // If there is no obvious reason to prefer the left-hand side over the
1975  // right-hand side, ensure the longest lived term is on the right-hand side,
1976  // so the shortest lived term will be replaced by the longest lived.
1977  // This tends to expose more simplifications.
1978  uint32_t LVN = VN.lookupOrAdd(LHS);
1979  if ((isa<Argument>(LHS) && isa<Argument>(RHS)) ||
1980  (isa<Instruction>(LHS) && isa<Instruction>(RHS))) {
1981  // Move the 'oldest' value to the right-hand side, using the value number
1982  // as a proxy for age.
1983  uint32_t RVN = VN.lookupOrAdd(RHS);
1984  if (LVN < RVN) {
1985  std::swap(LHS, RHS);
1986  LVN = RVN;
1987  }
1988  }
1989 
1990  // If value numbering later sees that an instruction in the scope is equal
1991  // to 'LHS' then ensure it will be turned into 'RHS'. In order to preserve
1992  // the invariant that instructions only occur in the leader table for their
1993  // own value number (this is used by removeFromLeaderTable), do not do this
1994  // if RHS is an instruction (if an instruction in the scope is morphed into
1995  // LHS then it will be turned into RHS by the next GVN iteration anyway, so
1996  // using the leader table is about compiling faster, not optimizing better).
1997  // The leader table only tracks basic blocks, not edges. Only add to if we
1998  // have the simple case where the edge dominates the end.
1999  if (RootDominatesEnd && !isa<Instruction>(RHS))
2000  addToLeaderTable(LVN, RHS, Root.getEnd());
2001 
2002  // Replace all occurrences of 'LHS' with 'RHS' everywhere in the scope. As
2003  // LHS always has at least one use that is not dominated by Root, this will
2004  // never do anything if LHS has only one use.
2005  if (!LHS->hasOneUse()) {
2006  unsigned NumReplacements =
2007  DominatesByEdge
2008  ? replaceDominatedUsesWith(LHS, RHS, *DT, Root)
2009  : replaceDominatedUsesWith(LHS, RHS, *DT, Root.getStart());
2010 
2011  Changed |= NumReplacements > 0;
2012  NumGVNEqProp += NumReplacements;
2013  }
2014 
2015  // Now try to deduce additional equalities from this one. For example, if
2016  // the known equality was "(A != B)" == "false" then it follows that A and B
2017  // are equal in the scope. Only boolean equalities with an explicit true or
2018  // false RHS are currently supported.
2019  if (!RHS->getType()->isIntegerTy(1))
2020  // Not a boolean equality - bail out.
2021  continue;
2022  ConstantInt *CI = dyn_cast<ConstantInt>(RHS);
2023  if (!CI)
2024  // RHS neither 'true' nor 'false' - bail out.
2025  continue;
2026  // Whether RHS equals 'true'. Otherwise it equals 'false'.
2027  bool isKnownTrue = CI->isAllOnesValue();
2028  bool isKnownFalse = !isKnownTrue;
2029 
2030  // If "A && B" is known true then both A and B are known true. If "A || B"
2031  // is known false then both A and B are known false.
2032  Value *A, *B;
2033  if ((isKnownTrue && match(LHS, m_And(m_Value(A), m_Value(B)))) ||
2034  (isKnownFalse && match(LHS, m_Or(m_Value(A), m_Value(B))))) {
2035  Worklist.push_back(std::make_pair(A, RHS));
2036  Worklist.push_back(std::make_pair(B, RHS));
2037  continue;
2038  }
2039 
2040  // If we are propagating an equality like "(A == B)" == "true" then also
2041  // propagate the equality A == B. When propagating a comparison such as
2042  // "(A >= B)" == "true", replace all instances of "A < B" with "false".
2043  if (CmpInst *Cmp = dyn_cast<CmpInst>(LHS)) {
2044  Value *Op0 = Cmp->getOperand(0), *Op1 = Cmp->getOperand(1);
2045 
2046  // If "A == B" is known true, or "A != B" is known false, then replace
2047  // A with B everywhere in the scope.
2048  if ((isKnownTrue && Cmp->getPredicate() == CmpInst::ICMP_EQ) ||
2049  (isKnownFalse && Cmp->getPredicate() == CmpInst::ICMP_NE))
2050  Worklist.push_back(std::make_pair(Op0, Op1));
2051 
2052  // Handle the floating point versions of equality comparisons too.
2053  if ((isKnownTrue && Cmp->getPredicate() == CmpInst::FCMP_OEQ) ||
2054  (isKnownFalse && Cmp->getPredicate() == CmpInst::FCMP_UNE)) {
2055 
2056  // Floating point -0.0 and 0.0 compare equal, so we can only
2057  // propagate values if we know that we have a constant and that
2058  // its value is non-zero.
2059 
2060  // FIXME: We should do this optimization if 'no signed zeros' is
2061  // applicable via an instruction-level fast-math-flag or some other
2062  // indicator that relaxed FP semantics are being used.
2063 
2064  if (isa<ConstantFP>(Op1) && !cast<ConstantFP>(Op1)->isZero())
2065  Worklist.push_back(std::make_pair(Op0, Op1));
2066  }
2067 
2068  // If "A >= B" is known true, replace "A < B" with false everywhere.
2069  CmpInst::Predicate NotPred = Cmp->getInversePredicate();
2070  Constant *NotVal = ConstantInt::get(Cmp->getType(), isKnownFalse);
2071  // Since we don't have the instruction "A < B" immediately to hand, work
2072  // out the value number that it would have and use that to find an
2073  // appropriate instruction (if any).
2074  uint32_t NextNum = VN.getNextUnusedValueNumber();
2075  uint32_t Num = VN.lookupOrAddCmp(Cmp->getOpcode(), NotPred, Op0, Op1);
2076  // If the number we were assigned was brand new then there is no point in
2077  // looking for an instruction realizing it: there cannot be one!
2078  if (Num < NextNum) {
2079  Value *NotCmp = findLeader(Root.getEnd(), Num);
2080  if (NotCmp && isa<Instruction>(NotCmp)) {
2081  unsigned NumReplacements =
2082  DominatesByEdge
2083  ? replaceDominatedUsesWith(NotCmp, NotVal, *DT, Root)
2084  : replaceDominatedUsesWith(NotCmp, NotVal, *DT,
2085  Root.getStart());
2086  Changed |= NumReplacements > 0;
2087  NumGVNEqProp += NumReplacements;
2088  }
2089  }
2090  // Ensure that any instruction in scope that gets the "A < B" value number
2091  // is replaced with false.
2092  // The leader table only tracks basic blocks, not edges. Only add to if we
2093  // have the simple case where the edge dominates the end.
2094  if (RootDominatesEnd)
2095  addToLeaderTable(Num, NotVal, Root.getEnd());
2096 
2097  continue;
2098  }
2099  }
2100 
2101  return Changed;
2102 }
2103 
2104 /// When calculating availability, handle an instruction
2105 /// by inserting it into the appropriate sets
2106 bool GVN::processInstruction(Instruction *I) {
2107  // Ignore dbg info intrinsics.
2108  if (isa<DbgInfoIntrinsic>(I))
2109  return false;
2110 
2111  // If the instruction can be easily simplified then do so now in preference
2112  // to value numbering it. Value numbering often exposes redundancies, for
2113  // example if it determines that %y is equal to %x then the instruction
2114  // "%z = and i32 %x, %y" becomes "%z = and i32 %x, %x" which we now simplify.
2115  const DataLayout &DL = I->getModule()->getDataLayout();
2116  if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) {
2117  bool Changed = false;
2118  if (!I->use_empty()) {
2119  I->replaceAllUsesWith(V);
2120  Changed = true;
2121  }
2122  if (isInstructionTriviallyDead(I, TLI)) {
2124  Changed = true;
2125  }
2126  if (Changed) {
2127  if (MD && V->getType()->getScalarType()->isPointerTy())
2129  ++NumGVNSimpl;
2130  return true;
2131  }
2132  }
2133 
2134  if (IntrinsicInst *IntrinsicI = dyn_cast<IntrinsicInst>(I))
2135  if (IntrinsicI->getIntrinsicID() == Intrinsic::assume)
2136  return processAssumeIntrinsic(IntrinsicI);
2137 
2138  if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
2139  if (processLoad(LI))
2140  return true;
2141 
2142  unsigned Num = VN.lookupOrAdd(LI);
2143  addToLeaderTable(Num, LI, LI->getParent());
2144  return false;
2145  }
2146 
2147  // For conditional branches, we can perform simple conditional propagation on
2148  // the condition value itself.
2149  if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
2150  if (!BI->isConditional())
2151  return false;
2152 
2153  if (isa<Constant>(BI->getCondition()))
2154  return processFoldableCondBr(BI);
2155 
2156  Value *BranchCond = BI->getCondition();
2157  BasicBlock *TrueSucc = BI->getSuccessor(0);
2158  BasicBlock *FalseSucc = BI->getSuccessor(1);
2159  // Avoid multiple edges early.
2160  if (TrueSucc == FalseSucc)
2161  return false;
2162 
2163  BasicBlock *Parent = BI->getParent();
2164  bool Changed = false;
2165 
2166  Value *TrueVal = ConstantInt::getTrue(TrueSucc->getContext());
2167  BasicBlockEdge TrueE(Parent, TrueSucc);
2168  Changed |= propagateEquality(BranchCond, TrueVal, TrueE, true);
2169 
2170  Value *FalseVal = ConstantInt::getFalse(FalseSucc->getContext());
2171  BasicBlockEdge FalseE(Parent, FalseSucc);
2172  Changed |= propagateEquality(BranchCond, FalseVal, FalseE, true);
2173 
2174  return Changed;
2175  }
2176 
2177  // For switches, propagate the case values into the case destinations.
2178  if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
2179  Value *SwitchCond = SI->getCondition();
2180  BasicBlock *Parent = SI->getParent();
2181  bool Changed = false;
2182 
2183  // Remember how many outgoing edges there are to every successor.
2185  for (unsigned i = 0, n = SI->getNumSuccessors(); i != n; ++i)
2186  ++SwitchEdges[SI->getSuccessor(i)];
2187 
2188  for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
2189  i != e; ++i) {
2190  BasicBlock *Dst = i.getCaseSuccessor();
2191  // If there is only a single edge, propagate the case value into it.
2192  if (SwitchEdges.lookup(Dst) == 1) {
2193  BasicBlockEdge E(Parent, Dst);
2194  Changed |= propagateEquality(SwitchCond, i.getCaseValue(), E, true);
2195  }
2196  }
2197  return Changed;
2198  }
2199 
2200  // Instructions with void type don't return a value, so there's
2201  // no point in trying to find redundancies in them.
2202  if (I->getType()->isVoidTy())
2203  return false;
2204 
2205  uint32_t NextNum = VN.getNextUnusedValueNumber();
2206  unsigned Num = VN.lookupOrAdd(I);
2207 
2208  // Allocations are always uniquely numbered, so we can save time and memory
2209  // by fast failing them.
2210  if (isa<AllocaInst>(I) || isa<TerminatorInst>(I) || isa<PHINode>(I)) {
2211  addToLeaderTable(Num, I, I->getParent());
2212  return false;
2213  }
2214 
2215  // If the number we were assigned was a brand new VN, then we don't
2216  // need to do a lookup to see if the number already exists
2217  // somewhere in the domtree: it can't!
2218  if (Num >= NextNum) {
2219  addToLeaderTable(Num, I, I->getParent());
2220  return false;
2221  }
2222 
2223  // Perform fast-path value-number based elimination of values inherited from
2224  // dominators.
2225  Value *Repl = findLeader(I->getParent(), Num);
2226  if (!Repl) {
2227  // Failure, just remember this instance for future use.
2228  addToLeaderTable(Num, I, I->getParent());
2229  return false;
2230  } else if (Repl == I) {
2231  // If I was the result of a shortcut PRE, it might already be in the table
2232  // and the best replacement for itself. Nothing to do.
2233  return false;
2234  }
2235 
2236  // Remove it!
2237  patchAndReplaceAllUsesWith(I, Repl);
2238  if (MD && Repl->getType()->getScalarType()->isPointerTy())
2239  MD->invalidateCachedPointerInfo(Repl);
2241  return true;
2242 }
2243 
2244 /// runOnFunction - This is the main transformation entry point for a function.
2245 bool GVN::runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
2246  const TargetLibraryInfo &RunTLI, AAResults &RunAA,
2247  MemoryDependenceResults *RunMD, LoopInfo *LI,
2248  OptimizationRemarkEmitter *RunORE) {
2249  AC = &RunAC;
2250  DT = &RunDT;
2251  VN.setDomTree(DT);
2252  TLI = &RunTLI;
2253  VN.setAliasAnalysis(&RunAA);
2254  MD = RunMD;
2255  VN.setMemDep(MD);
2256  ORE = RunORE;
2257 
2258  bool Changed = false;
2259  bool ShouldContinue = true;
2260 
2261  // Merge unconditional branches, allowing PRE to catch more
2262  // optimization opportunities.
2263  for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ) {
2264  BasicBlock *BB = &*FI++;
2265 
2266  bool removedBlock = MergeBlockIntoPredecessor(BB, DT, LI, MD);
2267  if (removedBlock)
2268  ++NumGVNBlocks;
2269 
2270  Changed |= removedBlock;
2271  }
2272 
2273  unsigned Iteration = 0;
2274  while (ShouldContinue) {
2275  DEBUG(dbgs() << "GVN iteration: " << Iteration << "\n");
2276  ShouldContinue = iterateOnFunction(F);
2277  Changed |= ShouldContinue;
2278  ++Iteration;
2279  }
2280 
2281  if (EnablePRE) {
2282  // Fabricate val-num for dead-code in order to suppress assertion in
2283  // performPRE().
2284  assignValNumForDeadCode();
2285  bool PREChanged = true;
2286  while (PREChanged) {
2287  PREChanged = performPRE(F);
2288  Changed |= PREChanged;
2289  }
2290  }
2291 
2292  // FIXME: Should perform GVN again after PRE does something. PRE can move
2293  // computations into blocks where they become fully redundant. Note that
2294  // we can't do this until PRE's critical edge splitting updates memdep.
2295  // Actually, when this happens, we should just fully integrate PRE into GVN.
2296 
2297  cleanupGlobalSets();
2298  // Do not cleanup DeadBlocks in cleanupGlobalSets() as it's called for each
2299  // iteration.
2300  DeadBlocks.clear();
2301 
2302  return Changed;
2303 }
2304 
2305 bool GVN::processBlock(BasicBlock *BB) {
2306  // FIXME: Kill off InstrsToErase by doing erasing eagerly in a helper function
2307  // (and incrementing BI before processing an instruction).
2308  assert(InstrsToErase.empty() &&
2309  "We expect InstrsToErase to be empty across iterations");
2310  if (DeadBlocks.count(BB))
2311  return false;
2312 
2313  // Clearing map before every BB because it can be used only for single BB.
2314  ReplaceWithConstMap.clear();
2315  bool ChangedFunction = false;
2316 
2317  for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
2318  BI != BE;) {
2319  if (!ReplaceWithConstMap.empty())
2320  ChangedFunction |= replaceOperandsWithConsts(&*BI);
2321  ChangedFunction |= processInstruction(&*BI);
2322 
2323  if (InstrsToErase.empty()) {
2324  ++BI;
2325  continue;
2326  }
2327 
2328  // If we need some instructions deleted, do it now.
2329  NumGVNInstr += InstrsToErase.size();
2330 
2331  // Avoid iterator invalidation.
2332  bool AtStart = BI == BB->begin();
2333  if (!AtStart)
2334  --BI;
2335 
2336  for (SmallVectorImpl<Instruction *>::iterator I = InstrsToErase.begin(),
2337  E = InstrsToErase.end(); I != E; ++I) {
2338  DEBUG(dbgs() << "GVN removed: " << **I << '\n');
2339  if (MD) MD->removeInstruction(*I);
2340  DEBUG(verifyRemoved(*I));
2341  (*I)->eraseFromParent();
2342  }
2343  InstrsToErase.clear();
2344 
2345  if (AtStart)
2346  BI = BB->begin();
2347  else
2348  ++BI;
2349  }
2350 
2351  return ChangedFunction;
2352 }
2353 
2354 // Instantiate an expression in a predecessor that lacked it.
2355 bool GVN::performScalarPREInsertion(Instruction *Instr, BasicBlock *Pred,
2356  unsigned int ValNo) {
2357  // Because we are going top-down through the block, all value numbers
2358  // will be available in the predecessor by the time we need them. Any
2359  // that weren't originally present will have been instantiated earlier
2360  // in this loop.
2361  bool success = true;
2362  for (unsigned i = 0, e = Instr->getNumOperands(); i != e; ++i) {
2363  Value *Op = Instr->getOperand(i);
2364  if (isa<Argument>(Op) || isa<Constant>(Op) || isa<GlobalValue>(Op))
2365  continue;
2366  // This could be a newly inserted instruction, in which case, we won't
2367  // find a value number, and should give up before we hurt ourselves.
2368  // FIXME: Rewrite the infrastructure to let it easier to value number
2369  // and process newly inserted instructions.
2370  if (!VN.exists(Op)) {
2371  success = false;
2372  break;
2373  }
2374  if (Value *V = findLeader(Pred, VN.lookup(Op))) {
2375  Instr->setOperand(i, V);
2376  } else {
2377  success = false;
2378  break;
2379  }
2380  }
2381 
2382  // Fail out if we encounter an operand that is not available in
2383  // the PRE predecessor. This is typically because of loads which
2384  // are not value numbered precisely.
2385  if (!success)
2386  return false;
2387 
2388  Instr->insertBefore(Pred->getTerminator());
2389  Instr->setName(Instr->getName() + ".pre");
2390  Instr->setDebugLoc(Instr->getDebugLoc());
2391  VN.add(Instr, ValNo);
2392 
2393  // Update the availability map to include the new instruction.
2394  addToLeaderTable(ValNo, Instr, Pred);
2395  return true;
2396 }
2397 
2398 bool GVN::performScalarPRE(Instruction *CurInst) {
2399  if (isa<AllocaInst>(CurInst) || isa<TerminatorInst>(CurInst) ||
2400  isa<PHINode>(CurInst) || CurInst->getType()->isVoidTy() ||
2401  CurInst->mayReadFromMemory() || CurInst->mayHaveSideEffects() ||
2402  isa<DbgInfoIntrinsic>(CurInst))
2403  return false;
2404 
2405  // Don't do PRE on compares. The PHI would prevent CodeGenPrepare from
2406  // sinking the compare again, and it would force the code generator to
2407  // move the i1 from processor flags or predicate registers into a general
2408  // purpose register.
2409  if (isa<CmpInst>(CurInst))
2410  return false;
2411 
2412  // We don't currently value number ANY inline asm calls.
2413  if (CallInst *CallI = dyn_cast<CallInst>(CurInst))
2414  if (CallI->isInlineAsm())
2415  return false;
2416 
2417  uint32_t ValNo = VN.lookup(CurInst);
2418 
2419  // Look for the predecessors for PRE opportunities. We're
2420  // only trying to solve the basic diamond case, where
2421  // a value is computed in the successor and one predecessor,
2422  // but not the other. We also explicitly disallow cases
2423  // where the successor is its own predecessor, because they're
2424  // more complicated to get right.
2425  unsigned NumWith = 0;
2426  unsigned NumWithout = 0;
2427  BasicBlock *PREPred = nullptr;
2428  BasicBlock *CurrentBlock = CurInst->getParent();
2429 
2431  for (BasicBlock *P : predecessors(CurrentBlock)) {
2432  // We're not interested in PRE where the block is its
2433  // own predecessor, or in blocks with predecessors
2434  // that are not reachable.
2435  if (P == CurrentBlock) {
2436  NumWithout = 2;
2437  break;
2438  } else if (!DT->isReachableFromEntry(P)) {
2439  NumWithout = 2;
2440  break;
2441  }
2442 
2443  Value *predV = findLeader(P, ValNo);
2444  if (!predV) {
2445  predMap.push_back(std::make_pair(static_cast<Value *>(nullptr), P));
2446  PREPred = P;
2447  ++NumWithout;
2448  } else if (predV == CurInst) {
2449  /* CurInst dominates this predecessor. */
2450  NumWithout = 2;
2451  break;
2452  } else {
2453  predMap.push_back(std::make_pair(predV, P));
2454  ++NumWith;
2455  }
2456  }
2457 
2458  // Don't do PRE when it might increase code size, i.e. when
2459  // we would need to insert instructions in more than one pred.
2460  if (NumWithout > 1 || NumWith == 0)
2461  return false;
2462 
2463  // We may have a case where all predecessors have the instruction,
2464  // and we just need to insert a phi node. Otherwise, perform
2465  // insertion.
2466  Instruction *PREInstr = nullptr;
2467 
2468  if (NumWithout != 0) {
2469  // Don't do PRE across indirect branch.
2470  if (isa<IndirectBrInst>(PREPred->getTerminator()))
2471  return false;
2472 
2473  // We can't do PRE safely on a critical edge, so instead we schedule
2474  // the edge to be split and perform the PRE the next time we iterate
2475  // on the function.
2476  unsigned SuccNum = GetSuccessorNumber(PREPred, CurrentBlock);
2477  if (isCriticalEdge(PREPred->getTerminator(), SuccNum)) {
2478  toSplit.push_back(std::make_pair(PREPred->getTerminator(), SuccNum));
2479  return false;
2480  }
2481  // We need to insert somewhere, so let's give it a shot
2482  PREInstr = CurInst->clone();
2483  if (!performScalarPREInsertion(PREInstr, PREPred, ValNo)) {
2484  // If we failed insertion, make sure we remove the instruction.
2485  DEBUG(verifyRemoved(PREInstr));
2486  delete PREInstr;
2487  return false;
2488  }
2489  }
2490 
2491  // Either we should have filled in the PRE instruction, or we should
2492  // not have needed insertions.
2493  assert (PREInstr != nullptr || NumWithout == 0);
2494 
2495  ++NumGVNPRE;
2496 
2497  // Create a PHI to make the value available in this block.
2498  PHINode *Phi =
2499  PHINode::Create(CurInst->getType(), predMap.size(),
2500  CurInst->getName() + ".pre-phi", &CurrentBlock->front());
2501  for (unsigned i = 0, e = predMap.size(); i != e; ++i) {
2502  if (Value *V = predMap[i].first)
2503  Phi->addIncoming(V, predMap[i].second);
2504  else
2505  Phi->addIncoming(PREInstr, PREPred);
2506  }
2507 
2508  VN.add(Phi, ValNo);
2509  addToLeaderTable(ValNo, Phi, CurrentBlock);
2510  Phi->setDebugLoc(CurInst->getDebugLoc());
2511  CurInst->replaceAllUsesWith(Phi);
2512  if (MD && Phi->getType()->getScalarType()->isPointerTy())
2513  MD->invalidateCachedPointerInfo(Phi);
2514  VN.erase(CurInst);
2515  removeFromLeaderTable(ValNo, CurInst, CurrentBlock);
2516 
2517  DEBUG(dbgs() << "GVN PRE removed: " << *CurInst << '\n');
2518  if (MD)
2519  MD->removeInstruction(CurInst);
2520  DEBUG(verifyRemoved(CurInst));
2521  CurInst->eraseFromParent();
2522  ++NumGVNInstr;
2523 
2524  return true;
2525 }
2526 
2527 /// Perform a purely local form of PRE that looks for diamond
2528 /// control flow patterns and attempts to perform simple PRE at the join point.
2529 bool GVN::performPRE(Function &F) {
2530  bool Changed = false;
2531  for (BasicBlock *CurrentBlock : depth_first(&F.getEntryBlock())) {
2532  // Nothing to PRE in the entry block.
2533  if (CurrentBlock == &F.getEntryBlock())
2534  continue;
2535 
2536  // Don't perform PRE on an EH pad.
2537  if (CurrentBlock->isEHPad())
2538  continue;
2539 
2540  for (BasicBlock::iterator BI = CurrentBlock->begin(),
2541  BE = CurrentBlock->end();
2542  BI != BE;) {
2543  Instruction *CurInst = &*BI++;
2544  Changed |= performScalarPRE(CurInst);
2545  }
2546  }
2547 
2548  if (splitCriticalEdges())
2549  Changed = true;
2550 
2551  return Changed;
2552 }
2553 
2554 /// Split the critical edge connecting the given two blocks, and return
2555 /// the block inserted to the critical edge.
2556 BasicBlock *GVN::splitCriticalEdges(BasicBlock *Pred, BasicBlock *Succ) {
2557  BasicBlock *BB =
2559  if (MD)
2561  return BB;
2562 }
2563 
2564 /// Split critical edges found during the previous
2565 /// iteration that may enable further optimization.
2566 bool GVN::splitCriticalEdges() {
2567  if (toSplit.empty())
2568  return false;
2569  do {
2570  std::pair<TerminatorInst*, unsigned> Edge = toSplit.pop_back_val();
2571  SplitCriticalEdge(Edge.first, Edge.second,
2573  } while (!toSplit.empty());
2574  if (MD) MD->invalidateCachedPredecessors();
2575  return true;
2576 }
2577 
2578 /// Executes one iteration of GVN
2579 bool GVN::iterateOnFunction(Function &F) {
2580  cleanupGlobalSets();
2581 
2582  // Top-down walk of the dominator tree
2583  bool Changed = false;
2584  // Save the blocks this function have before transformation begins. GVN may
2585  // split critical edge, and hence may invalidate the RPO/DT iterator.
2586  //
2587  std::vector<BasicBlock *> BBVect;
2588  BBVect.reserve(256);
2589  // Needed for value numbering with phi construction to work.
2592  RE = RPOT.end();
2593  RI != RE; ++RI)
2594  BBVect.push_back(*RI);
2595 
2596  for (std::vector<BasicBlock *>::iterator I = BBVect.begin(), E = BBVect.end();
2597  I != E; I++)
2598  Changed |= processBlock(*I);
2599 
2600  return Changed;
2601 }
2602 
2603 void GVN::cleanupGlobalSets() {
2604  VN.clear();
2605  LeaderTable.clear();
2606  TableAllocator.Reset();
2607 }
2608 
2609 /// Verify that the specified instruction does not occur in our
2610 /// internal data structures.
2611 void GVN::verifyRemoved(const Instruction *Inst) const {
2612  VN.verifyRemoved(Inst);
2613 
2614  // Walk through the value number scope to make sure the instruction isn't
2615  // ferreted away in it.
2617  I = LeaderTable.begin(), E = LeaderTable.end(); I != E; ++I) {
2618  const LeaderTableEntry *Node = &I->second;
2619  assert(Node->Val != Inst && "Inst still in value numbering scope!");
2620 
2621  while (Node->Next) {
2622  Node = Node->Next;
2623  assert(Node->Val != Inst && "Inst still in value numbering scope!");
2624  }
2625  }
2626 }
2627 
2628 /// BB is declared dead, which implied other blocks become dead as well. This
2629 /// function is to add all these blocks to "DeadBlocks". For the dead blocks'
2630 /// live successors, update their phi nodes by replacing the operands
2631 /// corresponding to dead blocks with UndefVal.
2632 void GVN::addDeadBlock(BasicBlock *BB) {
2635 
2636  NewDead.push_back(BB);
2637  while (!NewDead.empty()) {
2638  BasicBlock *D = NewDead.pop_back_val();
2639  if (DeadBlocks.count(D))
2640  continue;
2641 
2642  // All blocks dominated by D are dead.
2644  DT->getDescendants(D, Dom);
2645  DeadBlocks.insert(Dom.begin(), Dom.end());
2646 
2647  // Figure out the dominance-frontier(D).
2648  for (BasicBlock *B : Dom) {
2649  for (BasicBlock *S : successors(B)) {
2650  if (DeadBlocks.count(S))
2651  continue;
2652 
2653  bool AllPredDead = true;
2654  for (BasicBlock *P : predecessors(S))
2655  if (!DeadBlocks.count(P)) {
2656  AllPredDead = false;
2657  break;
2658  }
2659 
2660  if (!AllPredDead) {
2661  // S could be proved dead later on. That is why we don't update phi
2662  // operands at this moment.
2663  DF.insert(S);
2664  } else {
2665  // While S is not dominated by D, it is dead by now. This could take
2666  // place if S already have a dead predecessor before D is declared
2667  // dead.
2668  NewDead.push_back(S);
2669  }
2670  }
2671  }
2672  }
2673 
2674  // For the dead blocks' live successors, update their phi nodes by replacing
2675  // the operands corresponding to dead blocks with UndefVal.
2677  I != E; I++) {
2678  BasicBlock *B = *I;
2679  if (DeadBlocks.count(B))
2680  continue;
2681 
2683  for (BasicBlock *P : Preds) {
2684  if (!DeadBlocks.count(P))
2685  continue;
2686 
2687  if (isCriticalEdge(P->getTerminator(), GetSuccessorNumber(P, B))) {
2688  if (BasicBlock *S = splitCriticalEdges(P, B))
2689  DeadBlocks.insert(P = S);
2690  }
2691 
2692  for (BasicBlock::iterator II = B->begin(); isa<PHINode>(II); ++II) {
2693  PHINode &Phi = cast<PHINode>(*II);
2695  UndefValue::get(Phi.getType()));
2696  }
2697  }
2698  }
2699 }
2700 
2701 // If the given branch is recognized as a foldable branch (i.e. conditional
2702 // branch with constant condition), it will perform following analyses and
2703 // transformation.
2704 // 1) If the dead out-coming edge is a critical-edge, split it. Let
2705 // R be the target of the dead out-coming edge.
2706 // 1) Identify the set of dead blocks implied by the branch's dead outcoming
2707 // edge. The result of this step will be {X| X is dominated by R}
2708 // 2) Identify those blocks which haves at least one dead predecessor. The
2709 // result of this step will be dominance-frontier(R).
2710 // 3) Update the PHIs in DF(R) by replacing the operands corresponding to
2711 // dead blocks with "UndefVal" in an hope these PHIs will optimized away.
2712 //
2713 // Return true iff *NEW* dead code are found.
2714 bool GVN::processFoldableCondBr(BranchInst *BI) {
2715  if (!BI || BI->isUnconditional())
2716  return false;
2717 
2718  // If a branch has two identical successors, we cannot declare either dead.
2719  if (BI->getSuccessor(0) == BI->getSuccessor(1))
2720  return false;
2721 
2722  ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
2723  if (!Cond)
2724  return false;
2725 
2726  BasicBlock *DeadRoot =
2727  Cond->getZExtValue() ? BI->getSuccessor(1) : BI->getSuccessor(0);
2728  if (DeadBlocks.count(DeadRoot))
2729  return false;
2730 
2731  if (!DeadRoot->getSinglePredecessor())
2732  DeadRoot = splitCriticalEdges(BI->getParent(), DeadRoot);
2733 
2734  addDeadBlock(DeadRoot);
2735  return true;
2736 }
2737 
2738 // performPRE() will trigger assert if it comes across an instruction without
2739 // associated val-num. As it normally has far more live instructions than dead
2740 // instructions, it makes more sense just to "fabricate" a val-number for the
2741 // dead code than checking if instruction involved is dead or not.
2742 void GVN::assignValNumForDeadCode() {
2743  for (BasicBlock *BB : DeadBlocks) {
2744  for (Instruction &Inst : *BB) {
2745  unsigned ValNum = VN.lookupOrAdd(&Inst);
2746  addToLeaderTable(ValNum, &Inst, BB);
2747  }
2748  }
2749 }
2750 
2752 public:
2753  static char ID; // Pass identification, replacement for typeid
2754  explicit GVNLegacyPass(bool NoLoads = false)
2755  : FunctionPass(ID), NoLoads(NoLoads) {
2757  }
2758 
2759  bool runOnFunction(Function &F) override {
2760  if (skipFunction(F))
2761  return false;
2762 
2763  auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
2764 
2765  return Impl.runImpl(
2766  F, getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F),
2767  getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
2768  getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(),
2769  getAnalysis<AAResultsWrapperPass>().getAAResults(),
2770  NoLoads ? nullptr
2771  : &getAnalysis<MemoryDependenceWrapperPass>().getMemDep(),
2772  LIWP ? &LIWP->getLoopInfo() : nullptr,
2773  &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE());
2774  }
2775 
2776  void getAnalysisUsage(AnalysisUsage &AU) const override {
2780  if (!NoLoads)
2783 
2787  }
2788 
2789 private:
2790  bool NoLoads;
2791  GVN Impl;
2792 };
2793 
2794 char GVNLegacyPass::ID = 0;
2795 
2796 // The public interface to this file...
2798  return new GVNLegacyPass(NoLoads);
2799 }
2800 
2801 INITIALIZE_PASS_BEGIN(GVNLegacyPass, "gvn", "Global Value Numbering", false, false)
2809 INITIALIZE_PASS_END(GVNLegacyPass, "gvn", "Global Value Numbering", false, false)
Legacy wrapper pass to provide the GlobalsAAResult object.
MachineLoop * L
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition: PatternMatch.h:506
void setDomTree(DominatorTree *D)
Definition: GVN.h:100
Value * getValueOperand()
Definition: Instructions.h:391
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:76
FunctionPass * createGVNPass(bool NoLoads=false)
Create a legacy GVN pass.
Definition: GVN.cpp:2797
static cl::opt< bool > EnableLoadPRE("enable-load-pre", cl::init(true))
void invalidateCachedPointerInfo(Value *Ptr)
Invalidates cached information about the specified pointer, because it may be too conservative in mem...
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:102
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:513
void invalidateCachedPredecessors()
Clears the PredIteratorCache info.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:64
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:870
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Helper class for SSA formation on a set of values defined in multiple blocks.
Definition: SSAUpdater.h:38
Diagnostic information for missed-optimization remarks.
Provides a lazy, caching interface for making common memory aliasing information queries, backed by LLVM's alias analysis passes.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
This instruction extracts a struct member or array element value from an aggregate value...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
static AvailableValue getMI(MemIntrinsic *MI, unsigned Offset=0)
Definition: GVN.cpp:147
unsigned Offset
Offset - The byte offset in Val that is interesting for the load query.
Definition: GVN.cpp:137
DiagnosticInfoOptimizationBase::Argument NV
bool isDef() const
Tests if this MemDepResult represents a query that is an instruction definition dependency.
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition: InstrTypes.h:1112
static int AnalyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr, MemIntrinsic *MI, const DataLayout &DL)
Definition: GVN.cpp:925
STATISTIC(NumFunctions,"Total number of functions")
size_t i
ValueT lookup(const KeyT &Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:162
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:450
Value * MaterializeAdjustedValue(LoadInst *LI, Instruction *InsertPt, GVN &gvn) const
Emit code at the specified insertion point to adjust the value defined here to the specified type...
Definition: GVN.cpp:1165
bool isUndefValue() const
Definition: GVN.cpp:174
This is the interface for a simple mod/ref and alias analysis over globals.
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:45
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:546
iterator end()
Definition: Function.h:537
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:51
void getDescendants(NodeT *R, SmallVectorImpl< NodeT * > &Result) const
Get all nodes dominated by R, including R itself.
const BasicBlock * getStart() const
Definition: Dominators.h:49
unsigned getNumOperands() const
Definition: User.h:167
void AddAvailableValue(BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value...
Definition: SSAUpdater.cpp:58
This class represents a function call, abstracting a target machine's calling convention.
This file contains the declarations for metadata subclasses.
An immutable pass that tracks lazily created AssumptionCache objects.
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
Definition: Type.cpp:655
LoadInst * getCoercedLoadValue() const
Definition: GVN.cpp:181
bool mayHaveSideEffects() const
Return true if the instruction may have side effects.
Definition: Instruction.h:450
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1126
A cache of .assume calls within a function.
bool exists(Value *V) const
Returns true if a value number exists for the specified value.
Definition: GVN.cpp:461
void setAliasAnalysis(AliasAnalysis *A)
Definition: GVN.h:97
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:896
static int AnalyzeLoadFromClobberingWrite(Type *LoadTy, Value *LoadPtr, Value *WritePtr, uint64_t WriteSizeInBits, const DataLayout &DL)
This function is called when we have a memdep query of a load that ends up being a clobbering memory ...
Definition: GVN.cpp:820
This class wraps the llvm.memset intrinsic.
This class implements a map that also provides access to all stored values in a deterministic order...
Definition: MapVector.h:32
bool isEHPad() const
Return true if this basic block is an exception handling block.
Definition: BasicBlock.h:315
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:100
A debug info location.
Definition: DebugLoc.h:34
const Instruction & front() const
Definition: BasicBlock.h:240
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:189
An instruction for reading from memory.
Definition: Instructions.h:164
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:170
Hexagon Common GEP
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:1758
bool isSimple() const
Definition: Instructions.h:263
iterator end()
Get an iterator to the end of the SetVector.
Definition: SetVector.h:93
op_iterator op_begin()
Definition: User.h:205
bool isClobber() const
Tests if this MemDepResult represents a query that is an instruction clobber dependency.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:195
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:191
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:228
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:172
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
Definition: PassManager.h:670
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:41
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:53
bool isUnconditional() const
static cl::opt< uint32_t > MaxRecurseDepth("max-recurse-depth", cl::Hidden, cl::init(1000), cl::ZeroOrMore, cl::desc("Max recurse depth (default = 1000)"))
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: GVN.cpp:2776
static cl::opt< bool > EnablePRE("enable-pre", cl::init(true), cl::Hidden)
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
Definition: GVN.cpp:2759
Option class for critical edge splitting.
void clear()
Remove all entries from the ValueTable.
Definition: GVN.cpp:556
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it...
Definition: Allocator.h:192
bool isUnordered() const
Definition: Instructions.h:264
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
static unsigned getLoadLoadClobberFullWidthSize(const Value *MemLocBase, int64_t MemLocOffs, unsigned MemLocSize, const LoadInst *LI)
Looks at a memory location for a load (specified by MemLocBase, Offs, and Size) and compares it again...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:32
static GCRegistry::Add< StatepointGC > D("statepoint-example","an example strategy for statepoint")
unsigned getNumArgOperands() const
Return the number of call arguments.
void getNonLocalPointerDependency(Instruction *QueryInst, SmallVectorImpl< NonLocalDepResult > &Result)
Perform a full dependency query for an access to the QueryInst's specified memory location...
static int AnalyzeLoadFromClobberingLoad(Type *LoadTy, Value *LoadPtr, LoadInst *DepLI, const DataLayout &DL)
This function is called when we have a memdep query of a load that ends up being clobbered by another...
Definition: GVN.cpp:893
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:588
unsigned getNumIndices() const
An analysis that produces MemoryDependenceResults for a function.
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:257
std::vector< NonLocalDepEntry > NonLocalDepInfo
Analysis pass that exposes the LoopInfo for a function.
Definition: LoopInfo.h:806
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following: ...
SynchronizationScope getSynchScope() const
Definition: Instructions.h:245
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:60
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:106
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:154
Value * CreateIntToPtr(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1358
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Attempt to fold the constant using the specified DataLayout.
static void patchReplacementInstruction(Instruction *I, Value *Repl)
Definition: GVN.cpp:1791
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1362
#define F(x, y, z)
Definition: MD5.cpp:51
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:136
bool mayReadFromMemory() const
Return true if this instruction may read memory.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
bool isSimpleValue() const
Definition: GVN.cpp:171
The core GVN pass object.
Definition: GVN.h:46
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
Definition: AsmWriter.cpp:3473
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL)
Analyze the specified pointer to see if it can be expressed as a base pointer plus a constant offset...
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:269
Expression(uint32_t o=~2U)
Definition: GVN.cpp:83
void andIRFlags(const Value *V)
Logical 'and' of any supported wrapping, exact, and fast-math flags of V and this instruction...
Function Alias Analysis false
BasicBlock * getSuccessor(unsigned i) const
#define DEBUG_TYPE
Definition: GVN.cpp:59
iterator begin()
Get an iterator to the beginning of the SetVector.
Definition: SetVector.h:83
DiagnosticInfoOptimizationBase::setExtraArgs setExtraArgs
static AvailableValue getLoad(LoadInst *LI, unsigned Offset=0)
Definition: GVN.cpp:155
hash_code hash_value(const APFloat &Arg)
See friend declarations above.
Definition: APFloat.cpp:4132
static Value * GetMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset, Type *LoadTy, Instruction *InsertPt, const DataLayout &DL)
This function is called when we have a memdep query of a load that ends up being a clobbering mem int...
Definition: GVN.cpp:1075
bool isLittleEndian() const
Layout endianness...
Definition: DataLayout.h:220
static GVN::Expression getEmptyKey()
Definition: GVN.cpp:106
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
An instruction for storing to memory.
Definition: Instructions.h:300
void add(Value *V, uint32_t num)
add - Insert a value into the table with a specified value number.
Definition: GVN.cpp:347
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:401
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition: Type.h:210
Value * getSimpleValue() const
Definition: GVN.cpp:176
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1301
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:263
iterator begin()
Definition: Function.h:535
static unsigned getHashValue(const GVN::Expression &e)
Definition: GVN.cpp:110
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:96
Type * getScalarType() const LLVM_READONLY
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.cpp:44
static Value * GetLoadValueForLoad(LoadInst *SrcVal, unsigned Offset, Type *LoadTy, Instruction *InsertPt, GVN &gvn)
This function is called when we have a memdep query of a load that ends up being a clobbering load...
Definition: GVN.cpp:1016
bool isAtomic() const
Return true if this instruction has an AtomicOrdering of unordered or higher.
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:109
void initializeGVNLegacyPassPass(PassRegistry &)
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1003
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1695
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
Definition: InstrTypes.h:74
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
Definition: Instructions.h:830
BasicBlock * SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
If this edge is a critical edge, insert a new node to split the critical edge.
#define P(N)
bool allowExtraAnalysis() const
Whether we allow for extra compile-time budget to perform more analysis to produce fewer false positi...
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:395
Value * GetValueInMiddleOfBlock(BasicBlock *BB)
Construct SSA form, materializing a value that is live in the middle of the specified block...
Definition: SSAUpdater.cpp:86
SmallVector< uint32_t, 4 > varargs
Definition: GVN.cpp:81
bool operator==(const Expression &other) const
Definition: GVN.cpp:85
const NonLocalDepInfo & getNonLocalCallDependency(CallSite QueryCS)
Perform a full dependency query for the specified call, returning the set of blocks that the value is...
static Value * GetStoreValueForLoad(Value *SrcVal, unsigned Offset, Type *LoadTy, Instruction *InsertPt, const DataLayout &DL)
This function is called when we have a memdep query of a load that ends up being a clobbering store...
Definition: GVN.cpp:977
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:107
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:256
constexpr bool isPowerOf2_32(uint32_t Value)
isPowerOf2_32 - This function returns true if the argument is a power of two > 0. ...
Definition: MathExtras.h:399
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:653
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction...
Definition: Instruction.cpp:82
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
PointerIntPair - This class implements a pair of a pointer and small integer.
PHITransAddr - An address value which tracks and handles phi translation.
Definition: PHITransAddr.h:36
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
Definition: PatternMatch.h:512
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:48
uint64_t getTypeStoreSizeInBits(Type *Ty) const
Returns the maximum number of bits that may be overwritten by storing the specified type; always a mu...
Definition: DataLayout.h:399
This class holds the mapping between values and value numbers.
Definition: GVN.h:68
Conditional or Unconditional Branch instruction.
This file provides the interface for LLVM's Global Value Numbering pass which eliminates fully redund...
static GVN::Expression getTombstoneKey()
Definition: GVN.cpp:108
static Value * ConstructSSAForLoadSet(LoadInst *LI, SmallVectorImpl< AvailableValueInBlock > &ValuesPerBlock, GVN &gvn)
Given a set of loads specified by ValuesPerBlock, construct SSA form, allowing us to eliminate LI...
Definition: GVN.cpp:1134
This is an important base class in LLVM.
Definition: Constant.h:42
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
static bool isEqual(const GVN::Expression &LHS, const GVN::Expression &RHS)
Definition: GVN.cpp:114
A manager for alias analyses.
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Definition: APInt.h:1947
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Definition: APInt.h:1952
Diagnostic information for applied optimization remarks.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:116
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:259
Represent the analysis usage information of a pass.
op_iterator op_end()
Definition: User.h:207
uint32_t Offset
std::vector< NodeRef >::reverse_iterator rpo_iterator
size_type size() const
Definition: MapVector.h:51
Analysis pass providing a never-invalidated alias analysis result.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:880
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
PointerIntPair< Value *, 2, ValType > Val
V - The value that is live out of the block.
Definition: GVN.cpp:134
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
Value * getOperand(unsigned i) const
Definition: User.h:145
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:119
Value * getPointerOperand()
Definition: Instructions.h:270
bool isCommutative() const
Return true if the instruction is commutative:
Definition: Instruction.h:385
const MemDepResult & getResult() const
bool HasValueForBlock(BasicBlock *BB) const
Return true if the SSAUpdater already has a value for the specified block.
Definition: SSAUpdater.cpp:54
GVNLegacyPass(bool NoLoads=false)
Definition: GVN.cpp:2754
void setAlignment(unsigned Align)
Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL)
ConstantFoldLoadFromConstPtr - Return the value that a load from C would produce if it is constant an...
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:392
static void reportLoadElim(LoadInst *LI, Value *AvailableValue, OptimizationRemarkEmitter *ORE)
Definition: GVN.cpp:1640
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:213
DominatorTree & getDominatorTree() const
Definition: GVN.h:59
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1337
uint64_t NextPowerOf2(uint64_t A)
NextPowerOf2 - Returns the next power of two (in 64-bits) that is strictly greater than A...
Definition: MathExtras.h:619
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:113
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:213
A wrapper analysis pass for the legacy pass manager that exposes a MemoryDepnedenceResults instance...
Value * GetUnderlyingObject(Value *V, const DataLayout &DL, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value...
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1298
A memory dependence query can return one of three different answers.
const BasicBlock * getEnd() const
Definition: Dominators.h:52
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Definition: Dominators.cpp:218
static void reportMayClobberedLoad(LoadInst *LI, MemDepResult DepInfo, 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:1214
void markInstructionForDeletion(Instruction *I)
This removes the specified instruction from our various maps and marks it for deletion.
Definition: GVN.h:54
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
Definition: DataLayout.cpp:709
A function analysis which provides an AssumptionCache.
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:234
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:292
uint64_t * Vals
This is the common base class for memset/memcpy/memmove.
Iterator for intrusive lists based on ilist_node.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:425
This is the shared class of boolean and integer constants.
Definition: Constants.h:88
void emit(DiagnosticInfoOptimizationBase &OptDiag)
The new interface to emit remarks.
static bool CanCoerceMustAliasedValueToLoad(Value *StoredVal, Type *LoadTy, const DataLayout &DL)
Return true if CoerceAvailableValueToLoadType will succeed.
Definition: GVN.cpp:694
Value * getDest() const
This is just like getRawDest, but it strips off any cast instructions that feed it, giving the original input.
iterator end()
Definition: BasicBlock.h:230
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:58
void verifyRemoved(const Value *) const
verifyRemoved - Verify that the value is removed from all internal data structures.
Definition: GVN.cpp:569
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:230
Provides information about what library functions are available for the current target.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:175
bool isVolatile() const
Return true if this is a load from a volatile memory location.
Definition: Instructions.h:218
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:50
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:625
Value * getLength() const
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:382
bool isNonLocal() const
Tests if this MemDepResult represents a query that is transparent to the start of the block...
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition: InstrTypes.h:1000
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:558
AtomicOrdering getOrdering() const
Returns the ordering effect of this fence.
Definition: Instructions.h:234
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
pred_range predecessors(BasicBlock *BB)
Definition: IR/CFG.h:110
const BasicBlock & getEntryBlock() const
Definition: Function.h:519
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:506
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
void setOperand(unsigned i, Value *Val)
Definition: User.h:150
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
bool isAllOnesValue() const
Return true if this is the value that would be returned by getAllOnesValue.
Definition: Constants.cpp:105
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:586
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
size_type count(const KeyT &Key) const
Definition: MapVector.h:126
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:602
Represents an AvailableValue which can be rematerialized at the end of the associated BasicBlock...
Definition: GVN.cpp:199
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:195
iterator_range< user_iterator > users()
Definition: Value.h:370
BasicBlock * getSinglePredecessor()
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:226
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:932
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition: Hashing.h:480
idx_iterator idx_end() const
An opaque object representing a hash code.
Definition: Hashing.h:72
bool isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates uninitialized memory (such ...
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1942
bool isStructTy() const
True if this is an instance of StructType.
Definition: Type.h:207
void erase(Value *v)
Remove a value from the value numbering.
Definition: GVN.cpp:563
bool isCoercedLoadValue() const
Definition: GVN.cpp:172
Value * getSource() const
This is just like getRawSource, but it strips off any cast instructions that feed it...
static bool isLifetimeStart(const Instruction *Inst)
Definition: GVN.cpp:1206
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:528
This class wraps the llvm.memcpy/memmove intrinsics.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:384
Instruction * getInst() const
If this is a normal dependency, returns the instruction that is depended on.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
Value * getCondition() const
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:72
bool MergeBlockIntoPredecessor(BasicBlock *BB, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemoryDependenceResults *MemDep=nullptr)
Attempts to merge a block into its predecessor, if possible.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:226
unsigned getAlignment() const
Return the alignment of the access that is being performed.
Definition: Instructions.h:227
void getAAMetadata(AAMDNodes &N, bool Merge=false) const
Fills the AAMDNodes structure with AA metadata from this instruction.
bool isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates zero-filled memory (such as...
#define I(x, y, z)
Definition: MD5.cpp:54
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
bool hasOneUse() const
Return true if there is exactly one user of this value.
Definition: Value.h:383
static AvailableValue get(Value *V, unsigned Offset=0)
Definition: GVN.cpp:139
uint32_t opcode
Definition: GVN.cpp:79
void setMemDep(MemoryDependenceResults *M)
Definition: GVN.h:99
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:287
void preserve()
Mark an analysis as preserved.
Definition: PassManager.h:120
uint64_t getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
Definition: DataLayout.h:391
friend hash_code hash_value(const Expression &Value)
Definition: GVN.cpp:97
uint32_t lookupOrAdd(Value *V)
lookup_or_add - Returns the value number for the specified value, assigning it a new number if it did...
Definition: GVN.cpp:465
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1354
Analysis pass providing the TargetLibraryInfo.
bool isMemIntrinValue() const
Definition: GVN.cpp:173
iterator_range< df_iterator< T > > depth_first(const T &G)
Multiway switch.
uint32_t lookup(Value *V) const
Returns the value number of the specified value.
Definition: GVN.cpp:536
bool use_empty() const
Definition: Value.h:299
static int AnalyzeLoadFromClobberingStore(Type *LoadTy, Value *LoadPtr, StoreInst *DepSI)
This function is called when we have a memdep query of a load that ends up being a clobbering store...
Definition: GVN.cpp:876
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Use * op_iterator
Definition: User.h:200
Represents a particular available value that we know how to materialize.
Definition: GVN.cpp:124
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:33
uint32_t getNextUnusedValueNumber()
Definition: GVN.h:101
static bool IsValueFullyAvailableInBlock(BasicBlock *BB, DenseMap< BasicBlock *, char > &FullyAvailableBlocks, uint32_t RecurseDepth)
Return true if we can prove that the value we're analyzing is fully available in the specified block...
Definition: GVN.cpp:622
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:883
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction has no side ef...
Definition: Local.cpp:288
LLVM Value Representation.
Definition: Value.h:71
succ_range successors(BasicBlock *BB)
Definition: IR/CFG.h:143
static AvailableValueInBlock getUndef(BasicBlock *BB)
Definition: GVN.cpp:217
void removeInstruction(Instruction *InstToRemove)
Removes an instruction from the dependence analysis, updating the dependence of instructions that pre...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:111
OptimizationRemarkEmitter legacy analysis pass.
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:951
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:533
#define DEBUG(X)
Definition: Debug.h:100
static Value * CoerceAvailableValueToLoadType(Value *StoredVal, Type *LoadedTy, IRBuilder<> &IRB, const DataLayout &DL)
If we saw a store of a value to memory, and then a load from a must-aliased pointer of a different ty...
Definition: GVN.cpp:718
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
Definition: Type.cpp:678
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Run the pass over the function.
Definition: GVN.cpp:580
IRTranslator LLVM IR MI
This is an entry in the NonLocalDepInfo cache.
A container for analyses that lazily runs them and caches their results.
BasicBlock * BB
BB - The basic block in question.
Definition: GVN.cpp:201
Value * SimplifyInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
See if we can compute a simplified version of this instruction.
static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl)
Definition: GVN.cpp:1822
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:217
idx_iterator idx_begin() const
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
Value * MaterializeAdjustedValue(LoadInst *LI, GVN &gvn) const
Emit code at the end of this block to adjust the value defined here to the specified type...
Definition: GVN.cpp:223
void setIncomingValue(unsigned i, Value *V)
AvailableValue AV
AV - The actual available value.
Definition: GVN.cpp:204
bool isBigEndian() const
Definition: DataLayout.h:221
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:479
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
Value * getPointerOperand()
Definition: Instructions.h:394
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:167
void combineMetadata(Instruction *K, const Instruction *J, ArrayRef< unsigned > KnownIDs)
Combine the metadata of two instructions so that K can replace J.
Definition: Local.cpp:1682
The optimization diagnostic interface.
const BasicBlock * getParent() const
Definition: Instruction.h:62
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:83
static AvailableValue getUndef()
Definition: GVN.cpp:163
static bool isOnlyReachableViaThisEdge(const BasicBlockEdge &E, DominatorTree *DT)
There is an edge from 'Src' to 'Dst'.
Definition: GVN.cpp:1911
MemIntrinsic * getMemIntrinValue() const
Definition: GVN.cpp:186
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:44
bool isVoidTy() const
Return true if this is 'void'.
Definition: Type.h:139
This instruction inserts a struct field of array element value into an aggregate value.
MemoryDependenceResults & getMemDep() const
Definition: GVN.h:61
bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum, bool AllowIdenticalEdges=false)
Return true if the specified edge is a critical edge.
Definition: CFG.cpp:88
MemDepResult getDependency(Instruction *QueryInst)
Returns the instruction on which a memory operation depends.