1 //===- GVN.cpp - Eliminate redundant values and loads ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass performs global value numbering to eliminate fully redundant
10 // instructions. It also performs simple dead load elimination.
11 //
12 // Note that this pass does the value numbering itself; it does not use the
13 // ValueNumbering analysis passes.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Transforms/Scalar/GVN.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/DepthFirstIterator.h"
20 #include "llvm/ADT/Hashing.h"
21 #include "llvm/ADT/MapVector.h"
22 #include "llvm/ADT/PointerIntPair.h"
23 #include "llvm/ADT/PostOrderIterator.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/SetVector.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/Statistic.h"
29 #include "llvm/Analysis/AliasAnalysis.h"
30 #include "llvm/Analysis/AssumeBundleQueries.h"
31 #include "llvm/Analysis/AssumptionCache.h"
32 #include "llvm/Analysis/CFG.h"
33 #include "llvm/Analysis/DomTreeUpdater.h"
34 #include "llvm/Analysis/GlobalsModRef.h"
35 #include "llvm/Analysis/InstructionSimplify.h"
36 #include "llvm/Analysis/LoopInfo.h"
37 #include "llvm/Analysis/MemoryBuiltins.h"
38 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
39 #include "llvm/Analysis/MemorySSA.h"
40 #include "llvm/Analysis/MemorySSAUpdater.h"
41 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
42 #include "llvm/Analysis/PHITransAddr.h"
43 #include "llvm/Analysis/TargetLibraryInfo.h"
44 #include "llvm/Analysis/ValueTracking.h"
45 #include "llvm/Config/llvm-config.h"
46 #include "llvm/IR/Attributes.h"
47 #include "llvm/IR/BasicBlock.h"
48 #include "llvm/IR/Constant.h"
49 #include "llvm/IR/Constants.h"
50 #include "llvm/IR/DataLayout.h"
51 #include "llvm/IR/DebugLoc.h"
52 #include "llvm/IR/Dominators.h"
53 #include "llvm/IR/Function.h"
54 #include "llvm/IR/InstrTypes.h"
55 #include "llvm/IR/Instruction.h"
56 #include "llvm/IR/Instructions.h"
57 #include "llvm/IR/IntrinsicInst.h"
58 #include "llvm/IR/Intrinsics.h"
59 #include "llvm/IR/LLVMContext.h"
60 #include "llvm/IR/Metadata.h"
61 #include "llvm/IR/Module.h"
62 #include "llvm/IR/Operator.h"
63 #include "llvm/IR/PassManager.h"
64 #include "llvm/IR/PatternMatch.h"
65 #include "llvm/IR/Type.h"
66 #include "llvm/IR/Use.h"
67 #include "llvm/IR/Value.h"
68 #include "llvm/InitializePasses.h"
69 #include "llvm/Pass.h"
70 #include "llvm/Support/Casting.h"
71 #include "llvm/Support/CommandLine.h"
72 #include "llvm/Support/Compiler.h"
73 #include "llvm/Support/Debug.h"
74 #include "llvm/Support/raw_ostream.h"
75 #include "llvm/Transforms/Utils.h"
76 #include "llvm/Transforms/Utils/AssumeBundleBuilder.h"
77 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
78 #include "llvm/Transforms/Utils/Local.h"
79 #include "llvm/Transforms/Utils/SSAUpdater.h"
80 #include "llvm/Transforms/Utils/VNCoercion.h"
81 #include <algorithm>
82 #include <cassert>
83 #include <cstdint>
84 #include <utility>
85 #include <vector>
86
87 using namespace llvm;
88 using namespace llvm::gvn;
89 using namespace llvm::VNCoercion;
90 using namespace PatternMatch;
91
92 #define DEBUG_TYPE "gvn"
93
94 STATISTIC(NumGVNInstr, "Number of instructions deleted");
95 STATISTIC(NumGVNLoad, "Number of loads deleted");
96 STATISTIC(NumGVNPRE, "Number of instructions PRE'd");
97 STATISTIC(NumGVNBlocks, "Number of blocks merged");
98 STATISTIC(NumGVNSimpl, "Number of instructions simplified");
99 STATISTIC(NumGVNEqProp, "Number of equalities propagated");
100 STATISTIC(NumPRELoad, "Number of loads PRE'd");
101
102 STATISTIC(IsValueFullyAvailableInBlockNumSpeculationsMax,
103 "Number of blocks speculated as available in "
104 "IsValueFullyAvailableInBlock(), max");
105 STATISTIC(MaxBBSpeculationCutoffReachedTimes,
106 "Number of times we we reached gvn-max-block-speculations cut-off "
107 "preventing further exploration");
108
109 static cl::opt<bool> GVNEnablePRE("enable-pre", cl::init(true), cl::Hidden);
110 static cl::opt<bool> GVNEnableLoadPRE("enable-load-pre", cl::init(true));
111 static cl::opt<bool> GVNEnableLoadInLoopPRE("enable-load-in-loop-pre",
112 cl::init(true));
113 static cl::opt<bool>
114 GVNEnableSplitBackedgeInLoadPRE("enable-split-backedge-in-load-pre",
115 cl::init(true));
116 static cl::opt<bool> GVNEnableMemDep("enable-gvn-memdep", cl::init(true));
117
118 static cl::opt<uint32_t> MaxNumDeps(
119 "gvn-max-num-deps", cl::Hidden, cl::init(100), cl::ZeroOrMore,
120 cl::desc("Max number of dependences to attempt Load PRE (default = 100)"));
121
122 // This is based on IsValueFullyAvailableInBlockNumSpeculationsMax stat.
123 static cl::opt<uint32_t> MaxBBSpeculations(
124 "gvn-max-block-speculations", cl::Hidden, cl::init(600), cl::ZeroOrMore,
125 cl::desc("Max number of blocks we're willing to speculate on (and recurse "
126 "into) when deducing if a value is fully available or not in GVN "
127 "(default = 600)"));
128
129 struct llvm::GVN::Expression {
130 uint32_t opcode;
131 bool commutative = false;
132 Type *type = nullptr;
133 SmallVector<uint32_t, 4> varargs;
134
Expressionllvm::GVN::Expression135 Expression(uint32_t o = ~2U) : opcode(o) {}
136
operator ==llvm::GVN::Expression137 bool operator==(const Expression &other) const {
138 if (opcode != other.opcode)
139 return false;
140 if (opcode == ~0U || opcode == ~1U)
141 return true;
142 if (type != other.type)
143 return false;
144 if (varargs != other.varargs)
145 return false;
146 return true;
147 }
148
hash_value(const Expression & Value)149 friend hash_code hash_value(const Expression &Value) {
150 return hash_combine(
151 Value.opcode, Value.type,
152 hash_combine_range(Value.varargs.begin(), Value.varargs.end()));
153 }
154 };
155
156 namespace llvm {
157
158 template <> struct DenseMapInfo<GVN::Expression> {
getEmptyKeyllvm::DenseMapInfo159 static inline GVN::Expression getEmptyKey() { return ~0U; }
getTombstoneKeyllvm::DenseMapInfo160 static inline GVN::Expression getTombstoneKey() { return ~1U; }
161
getHashValuellvm::DenseMapInfo162 static unsigned getHashValue(const GVN::Expression &e) {
163 using llvm::hash_value;
164
165 return static_cast<unsigned>(hash_value(e));
166 }
167
isEqualllvm::DenseMapInfo168 static bool isEqual(const GVN::Expression &LHS, const GVN::Expression &RHS) {
169 return LHS == RHS;
170 }
171 };
172
173 } // end namespace llvm
174
175 /// Represents a particular available value that we know how to materialize.
176 /// Materialization of an AvailableValue never fails. An AvailableValue is
177 /// implicitly associated with a rematerialization point which is the
178 /// location of the instruction from which it was formed.
179 struct llvm::gvn::AvailableValue {
180 enum ValType {
181 SimpleVal, // A simple offsetted value that is accessed.
182 LoadVal, // A value produced by a load.
183 MemIntrin, // A memory intrinsic which is loaded from.
184 UndefVal // A UndefValue representing a value from dead block (which
185 // is not yet physically removed from the CFG).
186 };
187
188 /// V - The value that is live out of the block.
189 PointerIntPair<Value *, 2, ValType> Val;
190
191 /// Offset - The byte offset in Val that is interesting for the load query.
192 unsigned Offset = 0;
193
getllvm::gvn::AvailableValue194 static AvailableValue get(Value *V, unsigned Offset = 0) {
195 AvailableValue Res;
196 Res.Val.setPointer(V);
197 Res.Val.setInt(SimpleVal);
198 Res.Offset = Offset;
199 return Res;
200 }
201
getMIllvm::gvn::AvailableValue202 static AvailableValue getMI(MemIntrinsic *MI, unsigned Offset = 0) {
203 AvailableValue Res;
204 Res.Val.setPointer(MI);
205 Res.Val.setInt(MemIntrin);
206 Res.Offset = Offset;
207 return Res;
208 }
209
getLoadllvm::gvn::AvailableValue210 static AvailableValue getLoad(LoadInst *LI, unsigned Offset = 0) {
211 AvailableValue Res;
212 Res.Val.setPointer(LI);
213 Res.Val.setInt(LoadVal);
214 Res.Offset = Offset;
215 return Res;
216 }
217
getUndefllvm::gvn::AvailableValue218 static AvailableValue getUndef() {
219 AvailableValue Res;
220 Res.Val.setPointer(nullptr);
221 Res.Val.setInt(UndefVal);
222 Res.Offset = 0;
223 return Res;
224 }
225
isSimpleValuellvm::gvn::AvailableValue226 bool isSimpleValue() const { return Val.getInt() == SimpleVal; }
isCoercedLoadValuellvm::gvn::AvailableValue227 bool isCoercedLoadValue() const { return Val.getInt() == LoadVal; }
isMemIntrinValuellvm::gvn::AvailableValue228 bool isMemIntrinValue() const { return Val.getInt() == MemIntrin; }
isUndefValuellvm::gvn::AvailableValue229 bool isUndefValue() const { return Val.getInt() == UndefVal; }
230
getSimpleValuellvm::gvn::AvailableValue231 Value *getSimpleValue() const {
232 assert(isSimpleValue() && "Wrong accessor");
233 return Val.getPointer();
234 }
235
getCoercedLoadValuellvm::gvn::AvailableValue236 LoadInst *getCoercedLoadValue() const {
237 assert(isCoercedLoadValue() && "Wrong accessor");
238 return cast<LoadInst>(Val.getPointer());
239 }
240
getMemIntrinValuellvm::gvn::AvailableValue241 MemIntrinsic *getMemIntrinValue() const {
242 assert(isMemIntrinValue() && "Wrong accessor");
243 return cast<MemIntrinsic>(Val.getPointer());
244 }
245
246 /// Emit code at the specified insertion point to adjust the value defined
247 /// here to the specified type. This handles various coercion cases.
248 Value *MaterializeAdjustedValue(LoadInst *LI, Instruction *InsertPt,
249 GVN &gvn) const;
250 };
251
252 /// Represents an AvailableValue which can be rematerialized at the end of
253 /// the associated BasicBlock.
254 struct llvm::gvn::AvailableValueInBlock {
255 /// BB - The basic block in question.
256 BasicBlock *BB = nullptr;
257
258 /// AV - The actual available value
259 AvailableValue AV;
260
getllvm::gvn::AvailableValueInBlock261 static AvailableValueInBlock get(BasicBlock *BB, AvailableValue &&AV) {
262 AvailableValueInBlock Res;
263 Res.BB = BB;
264 Res.AV = std::move(AV);
265 return Res;
266 }
267
getllvm::gvn::AvailableValueInBlock268 static AvailableValueInBlock get(BasicBlock *BB, Value *V,
269 unsigned Offset = 0) {
270 return get(BB, AvailableValue::get(V, Offset));
271 }
272
getUndefllvm::gvn::AvailableValueInBlock273 static AvailableValueInBlock getUndef(BasicBlock *BB) {
274 return get(BB, AvailableValue::getUndef());
275 }
276
277 /// Emit code at the end of this block to adjust the value defined here to
278 /// the specified type. This handles various coercion cases.
MaterializeAdjustedValuellvm::gvn::AvailableValueInBlock279 Value *MaterializeAdjustedValue(LoadInst *LI, GVN &gvn) const {
280 return AV.MaterializeAdjustedValue(LI, BB->getTerminator(), gvn);
281 }
282 };
283
284 //===----------------------------------------------------------------------===//
285 // ValueTable Internal Functions
286 //===----------------------------------------------------------------------===//
287
createExpr(Instruction * I)288 GVN::Expression GVN::ValueTable::createExpr(Instruction *I) {
289 Expression e;
290 e.type = I->getType();
291 e.opcode = I->getOpcode();
292 for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
293 OI != OE; ++OI)
294 e.varargs.push_back(lookupOrAdd(*OI));
295 if (I->isCommutative()) {
296 // Ensure that commutative instructions that only differ by a permutation
297 // of their operands get the same value number by sorting the operand value
298 // numbers. Since commutative operands are the 1st two operands it is more
299 // efficient to sort by hand rather than using, say, std::sort.
300 assert(I->getNumOperands() >= 2 && "Unsupported commutative instruction!");
301 if (e.varargs[0] > e.varargs[1])
302 std::swap(e.varargs[0], e.varargs[1]);
303 e.commutative = true;
304 }
305
306 if (auto *C = dyn_cast<CmpInst>(I)) {
307 // Sort the operand value numbers so x<y and y>x get the same value number.
308 CmpInst::Predicate Predicate = C->getPredicate();
309 if (e.varargs[0] > e.varargs[1]) {
310 std::swap(e.varargs[0], e.varargs[1]);
311 Predicate = CmpInst::getSwappedPredicate(Predicate);
312 }
313 e.opcode = (C->getOpcode() << 8) | Predicate;
314 e.commutative = true;
315 } else if (auto *E = dyn_cast<InsertValueInst>(I)) {
316 e.varargs.append(E->idx_begin(), E->idx_end());
317 } else if (auto *SVI = dyn_cast<ShuffleVectorInst>(I)) {
318 ArrayRef<int> ShuffleMask = SVI->getShuffleMask();
319 e.varargs.append(ShuffleMask.begin(), ShuffleMask.end());
320 }
321
322 return e;
323 }
324
createCmpExpr(unsigned Opcode,CmpInst::Predicate Predicate,Value * LHS,Value * RHS)325 GVN::Expression GVN::ValueTable::createCmpExpr(unsigned Opcode,
326 CmpInst::Predicate Predicate,
327 Value *LHS, Value *RHS) {
328 assert((Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) &&
329 "Not a comparison!");
330 Expression e;
331 e.type = CmpInst::makeCmpResultType(LHS->getType());
332 e.varargs.push_back(lookupOrAdd(LHS));
333 e.varargs.push_back(lookupOrAdd(RHS));
334
335 // Sort the operand value numbers so x<y and y>x get the same value number.
336 if (e.varargs[0] > e.varargs[1]) {
337 std::swap(e.varargs[0], e.varargs[1]);
338 Predicate = CmpInst::getSwappedPredicate(Predicate);
339 }
340 e.opcode = (Opcode << 8) | Predicate;
341 e.commutative = true;
342 return e;
343 }
344
createExtractvalueExpr(ExtractValueInst * EI)345 GVN::Expression GVN::ValueTable::createExtractvalueExpr(ExtractValueInst *EI) {
346 assert(EI && "Not an ExtractValueInst?");
347 Expression e;
348 e.type = EI->getType();
349 e.opcode = 0;
350
351 WithOverflowInst *WO = dyn_cast<WithOverflowInst>(EI->getAggregateOperand());
352 if (WO != nullptr && EI->getNumIndices() == 1 && *EI->idx_begin() == 0) {
353 // EI is an extract from one of our with.overflow intrinsics. Synthesize
354 // a semantically equivalent expression instead of an extract value
355 // expression.
356 e.opcode = WO->getBinaryOp();
357 e.varargs.push_back(lookupOrAdd(WO->getLHS()));
358 e.varargs.push_back(lookupOrAdd(WO->getRHS()));
359 return e;
360 }
361
362 // Not a recognised intrinsic. Fall back to producing an extract value
363 // expression.
364 e.opcode = EI->getOpcode();
365 for (Instruction::op_iterator OI = EI->op_begin(), OE = EI->op_end();
366 OI != OE; ++OI)
367 e.varargs.push_back(lookupOrAdd(*OI));
368
369 for (ExtractValueInst::idx_iterator II = EI->idx_begin(), IE = EI->idx_end();
370 II != IE; ++II)
371 e.varargs.push_back(*II);
372
373 return e;
374 }
375
376 //===----------------------------------------------------------------------===//
377 // ValueTable External Functions
378 //===----------------------------------------------------------------------===//
379
380 GVN::ValueTable::ValueTable() = default;
381 GVN::ValueTable::ValueTable(const ValueTable &) = default;
382 GVN::ValueTable::ValueTable(ValueTable &&) = default;
383 GVN::ValueTable::~ValueTable() = default;
384 GVN::ValueTable &GVN::ValueTable::operator=(const GVN::ValueTable &Arg) = default;
385
386 /// add - Insert a value into the table with a specified value number.
add(Value * V,uint32_t num)387 void GVN::ValueTable::add(Value *V, uint32_t num) {
388 valueNumbering.insert(std::make_pair(V, num));
389 if (PHINode *PN = dyn_cast<PHINode>(V))
390 NumberingPhi[num] = PN;
391 }
392
lookupOrAddCall(CallInst * C)393 uint32_t GVN::ValueTable::lookupOrAddCall(CallInst *C) {
394 if (AA->doesNotAccessMemory(C)) {
395 Expression exp = createExpr(C);
396 uint32_t e = assignExpNewValueNum(exp).first;
397 valueNumbering[C] = e;
398 return e;
399 } else if (MD && AA->onlyReadsMemory(C)) {
400 Expression exp = createExpr(C);
401 auto ValNum = assignExpNewValueNum(exp);
402 if (ValNum.second) {
403 valueNumbering[C] = ValNum.first;
404 return ValNum.first;
405 }
406
407 MemDepResult local_dep = MD->getDependency(C);
408
409 if (!local_dep.isDef() && !local_dep.isNonLocal()) {
410 valueNumbering[C] = nextValueNumber;
411 return nextValueNumber++;
412 }
413
414 if (local_dep.isDef()) {
415 // For masked load/store intrinsics, the local_dep may actully be
416 // a normal load or store instruction.
417 CallInst *local_cdep = dyn_cast<CallInst>(local_dep.getInst());
418
419 if (!local_cdep ||
420 local_cdep->getNumArgOperands() != C->getNumArgOperands()) {
421 valueNumbering[C] = nextValueNumber;
422 return nextValueNumber++;
423 }
424
425 for (unsigned i = 0, e = C->getNumArgOperands(); i < e; ++i) {
426 uint32_t c_vn = lookupOrAdd(C->getArgOperand(i));
427 uint32_t cd_vn = lookupOrAdd(local_cdep->getArgOperand(i));
428 if (c_vn != cd_vn) {
429 valueNumbering[C] = nextValueNumber;
430 return nextValueNumber++;
431 }
432 }
433
434 uint32_t v = lookupOrAdd(local_cdep);
435 valueNumbering[C] = v;
436 return v;
437 }
438
439 // Non-local case.
440 const MemoryDependenceResults::NonLocalDepInfo &deps =
441 MD->getNonLocalCallDependency(C);
442 // FIXME: Move the checking logic to MemDep!
443 CallInst* cdep = nullptr;
444
445 // Check to see if we have a single dominating call instruction that is
446 // identical to C.
447 for (unsigned i = 0, e = deps.size(); i != e; ++i) {
448 const NonLocalDepEntry *I = &deps[i];
449 if (I->getResult().isNonLocal())
450 continue;
451
452 // We don't handle non-definitions. If we already have a call, reject
453 // instruction dependencies.
454 if (!I->getResult().isDef() || cdep != nullptr) {
455 cdep = nullptr;
456 break;
457 }
458
459 CallInst *NonLocalDepCall = dyn_cast<CallInst>(I->getResult().getInst());
460 // FIXME: All duplicated with non-local case.
461 if (NonLocalDepCall && DT->properlyDominates(I->getBB(), C->getParent())){
462 cdep = NonLocalDepCall;
463 continue;
464 }
465
466 cdep = nullptr;
467 break;
468 }
469
470 if (!cdep) {
471 valueNumbering[C] = nextValueNumber;
472 return nextValueNumber++;
473 }
474
475 if (cdep->getNumArgOperands() != C->getNumArgOperands()) {
476 valueNumbering[C] = nextValueNumber;
477 return nextValueNumber++;
478 }
479 for (unsigned i = 0, e = C->getNumArgOperands(); i < e; ++i) {
480 uint32_t c_vn = lookupOrAdd(C->getArgOperand(i));
481 uint32_t cd_vn = lookupOrAdd(cdep->getArgOperand(i));
482 if (c_vn != cd_vn) {
483 valueNumbering[C] = nextValueNumber;
484 return nextValueNumber++;
485 }
486 }
487
488 uint32_t v = lookupOrAdd(cdep);
489 valueNumbering[C] = v;
490 return v;
491 } else {
492 valueNumbering[C] = nextValueNumber;
493 return nextValueNumber++;
494 }
495 }
496
497 /// Returns true if a value number exists for the specified value.
exists(Value * V) const498 bool GVN::ValueTable::exists(Value *V) const { return valueNumbering.count(V) != 0; }
499
500 /// lookup_or_add - Returns the value number for the specified value, assigning
501 /// it a new number if it did not have one before.
lookupOrAdd(Value * V)502 uint32_t GVN::ValueTable::lookupOrAdd(Value *V) {
503 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
504 if (VI != valueNumbering.end())
505 return VI->second;
506
507 if (!isa<Instruction>(V)) {
508 valueNumbering[V] = nextValueNumber;
509 return nextValueNumber++;
510 }
511
512 Instruction* I = cast<Instruction>(V);
513 Expression exp;
514 switch (I->getOpcode()) {
515 case Instruction::Call:
516 return lookupOrAddCall(cast<CallInst>(I));
517 case Instruction::FNeg:
518 case Instruction::Add:
519 case Instruction::FAdd:
520 case Instruction::Sub:
521 case Instruction::FSub:
522 case Instruction::Mul:
523 case Instruction::FMul:
524 case Instruction::UDiv:
525 case Instruction::SDiv:
526 case Instruction::FDiv:
527 case Instruction::URem:
528 case Instruction::SRem:
529 case Instruction::FRem:
530 case Instruction::Shl:
531 case Instruction::LShr:
532 case Instruction::AShr:
533 case Instruction::And:
534 case Instruction::Or:
535 case Instruction::Xor:
536 case Instruction::ICmp:
537 case Instruction::FCmp:
538 case Instruction::Trunc:
539 case Instruction::ZExt:
540 case Instruction::SExt:
541 case Instruction::FPToUI:
542 case Instruction::FPToSI:
543 case Instruction::UIToFP:
544 case Instruction::SIToFP:
545 case Instruction::FPTrunc:
546 case Instruction::FPExt:
547 case Instruction::PtrToInt:
548 case Instruction::IntToPtr:
549 case Instruction::AddrSpaceCast:
550 case Instruction::BitCast:
551 case Instruction::Select:
552 case Instruction::Freeze:
553 case Instruction::ExtractElement:
554 case Instruction::InsertElement:
555 case Instruction::ShuffleVector:
556 case Instruction::InsertValue:
557 case Instruction::GetElementPtr:
558 exp = createExpr(I);
559 break;
560 case Instruction::ExtractValue:
561 exp = createExtractvalueExpr(cast<ExtractValueInst>(I));
562 break;
563 case Instruction::PHI:
564 valueNumbering[V] = nextValueNumber;
565 NumberingPhi[nextValueNumber] = cast<PHINode>(V);
566 return nextValueNumber++;
567 default:
568 valueNumbering[V] = nextValueNumber;
569 return nextValueNumber++;
570 }
571
572 uint32_t e = assignExpNewValueNum(exp).first;
573 valueNumbering[V] = e;
574 return e;
575 }
576
577 /// Returns the value number of the specified value. Fails if
578 /// the value has not yet been numbered.
lookup(Value * V,bool Verify) const579 uint32_t GVN::ValueTable::lookup(Value *V, bool Verify) const {
580 DenseMap<Value*, uint32_t>::const_iterator VI = valueNumbering.find(V);
581 if (Verify) {
582 assert(VI != valueNumbering.end() && "Value not numbered?");
583 return VI->second;
584 }
585 return (VI != valueNumbering.end()) ? VI->second : 0;
586 }
587
588 /// Returns the value number of the given comparison,
589 /// assigning it a new number if it did not have one before. Useful when
590 /// we deduced the result of a comparison, but don't immediately have an
591 /// instruction realizing that comparison to hand.
lookupOrAddCmp(unsigned Opcode,CmpInst::Predicate Predicate,Value * LHS,Value * RHS)592 uint32_t GVN::ValueTable::lookupOrAddCmp(unsigned Opcode,
593 CmpInst::Predicate Predicate,
594 Value *LHS, Value *RHS) {
595 Expression exp = createCmpExpr(Opcode, Predicate, LHS, RHS);
596 return assignExpNewValueNum(exp).first;
597 }
598
599 /// Remove all entries from the ValueTable.
clear()600 void GVN::ValueTable::clear() {
601 valueNumbering.clear();
602 expressionNumbering.clear();
603 NumberingPhi.clear();
604 PhiTranslateTable.clear();
605 nextValueNumber = 1;
606 Expressions.clear();
607 ExprIdx.clear();
608 nextExprNumber = 0;
609 }
610
611 /// Remove a value from the value numbering.
erase(Value * V)612 void GVN::ValueTable::erase(Value *V) {
613 uint32_t Num = valueNumbering.lookup(V);
614 valueNumbering.erase(V);
615 // If V is PHINode, V <--> value number is an one-to-one mapping.
616 if (isa<PHINode>(V))
617 NumberingPhi.erase(Num);
618 }
619
620 /// verifyRemoved - Verify that the value is removed from all internal data
621 /// structures.
verifyRemoved(const Value * V) const622 void GVN::ValueTable::verifyRemoved(const Value *V) const {
623 for (DenseMap<Value*, uint32_t>::const_iterator
624 I = valueNumbering.begin(), E = valueNumbering.end(); I != E; ++I) {
625 assert(I->first != V && "Inst still occurs in value numbering map!");
626 }
627 }
628
629 //===----------------------------------------------------------------------===//
630 // GVN Pass
631 //===----------------------------------------------------------------------===//
632
isPREEnabled() const633 bool GVN::isPREEnabled() const {
634 return Options.AllowPRE.getValueOr(GVNEnablePRE);
635 }
636
isLoadPREEnabled() const637 bool GVN::isLoadPREEnabled() const {
638 return Options.AllowLoadPRE.getValueOr(GVNEnableLoadPRE);
639 }
640
isLoadInLoopPREEnabled() const641 bool GVN::isLoadInLoopPREEnabled() const {
642 return Options.AllowLoadInLoopPRE.getValueOr(GVNEnableLoadInLoopPRE);
643 }
644
isLoadPRESplitBackedgeEnabled() const645 bool GVN::isLoadPRESplitBackedgeEnabled() const {
646 return Options.AllowLoadPRESplitBackedge.getValueOr(
647 GVNEnableSplitBackedgeInLoadPRE);
648 }
649
isMemDepEnabled() const650 bool GVN::isMemDepEnabled() const {
651 return Options.AllowMemDep.getValueOr(GVNEnableMemDep);
652 }
653
run(Function & F,FunctionAnalysisManager & AM)654 PreservedAnalyses GVN::run(Function &F, FunctionAnalysisManager &AM) {
655 // FIXME: The order of evaluation of these 'getResult' calls is very
656 // significant! Re-ordering these variables will cause GVN when run alone to
657 // be less effective! We should fix memdep and basic-aa to not exhibit this
658 // behavior, but until then don't change the order here.
659 auto &AC = AM.getResult<AssumptionAnalysis>(F);
660 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
661 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
662 auto &AA = AM.getResult<AAManager>(F);
663 auto *MemDep =
664 isMemDepEnabled() ? &AM.getResult<MemoryDependenceAnalysis>(F) : nullptr;
665 auto *LI = AM.getCachedResult<LoopAnalysis>(F);
666 auto *MSSA = AM.getCachedResult<MemorySSAAnalysis>(F);
667 auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
668 bool Changed = runImpl(F, AC, DT, TLI, AA, MemDep, LI, &ORE,
669 MSSA ? &MSSA->getMSSA() : nullptr);
670 if (!Changed)
671 return PreservedAnalyses::all();
672 PreservedAnalyses PA;
673 PA.preserve<DominatorTreeAnalysis>();
674 PA.preserve<GlobalsAA>();
675 PA.preserve<TargetLibraryAnalysis>();
676 if (MSSA)
677 PA.preserve<MemorySSAAnalysis>();
678 if (LI)
679 PA.preserve<LoopAnalysis>();
680 return PA;
681 }
682
683 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump(DenseMap<uint32_t,Value * > & d) const684 LLVM_DUMP_METHOD void GVN::dump(DenseMap<uint32_t, Value*>& d) const {
685 errs() << "{\n";
686 for (DenseMap<uint32_t, Value*>::iterator I = d.begin(),
687 E = d.end(); I != E; ++I) {
688 errs() << I->first << "\n";
689 I->second->dump();
690 }
691 errs() << "}\n";
692 }
693 #endif
694
695 enum class AvailabilityState : char {
696 /// We know the block *is not* fully available. This is a fixpoint.
697 Unavailable = 0,
698 /// We know the block *is* fully available. This is a fixpoint.
699 Available = 1,
700 /// We do not know whether the block is fully available or not,
701 /// but we are currently speculating that it will be.
702 /// If it would have turned out that the block was, in fact, not fully
703 /// available, this would have been cleaned up into an Unavailable.
704 SpeculativelyAvailable = 2,
705 };
706
707 /// Return true if we can prove that the value
708 /// we're analyzing is fully available in the specified block. As we go, keep
709 /// track of which blocks we know are fully alive in FullyAvailableBlocks. This
710 /// map is actually a tri-state map with the following values:
711 /// 0) we know the block *is not* fully available.
712 /// 1) we know the block *is* fully available.
713 /// 2) we do not know whether the block is fully available or not, but we are
714 /// currently speculating that it will be.
IsValueFullyAvailableInBlock(BasicBlock * BB,DenseMap<BasicBlock *,AvailabilityState> & FullyAvailableBlocks)715 static bool IsValueFullyAvailableInBlock(
716 BasicBlock *BB,
717 DenseMap<BasicBlock *, AvailabilityState> &FullyAvailableBlocks) {
718 SmallVector<BasicBlock *, 32> Worklist;
719 Optional<BasicBlock *> UnavailableBB;
720
721 // The number of times we didn't find an entry for a block in a map and
722 // optimistically inserted an entry marking block as speculatively available.
723 unsigned NumNewNewSpeculativelyAvailableBBs = 0;
724
725 #ifndef NDEBUG
726 SmallSet<BasicBlock *, 32> NewSpeculativelyAvailableBBs;
727 SmallVector<BasicBlock *, 32> AvailableBBs;
728 #endif
729
730 Worklist.emplace_back(BB);
731 while (!Worklist.empty()) {
732 BasicBlock *CurrBB = Worklist.pop_back_val(); // LIFO - depth-first!
733 // Optimistically assume that the block is Speculatively Available and check
734 // to see if we already know about this block in one lookup.
735 std::pair<DenseMap<BasicBlock *, AvailabilityState>::iterator, bool> IV =
736 FullyAvailableBlocks.try_emplace(
737 CurrBB, AvailabilityState::SpeculativelyAvailable);
738 AvailabilityState &State = IV.first->second;
739
740 // Did the entry already exist for this block?
741 if (!IV.second) {
742 if (State == AvailabilityState::Unavailable) {
743 UnavailableBB = CurrBB;
744 break; // Backpropagate unavailability info.
745 }
746
747 #ifndef NDEBUG
748 AvailableBBs.emplace_back(CurrBB);
749 #endif
750 continue; // Don't recurse further, but continue processing worklist.
751 }
752
753 // No entry found for block.
754 ++NumNewNewSpeculativelyAvailableBBs;
755 bool OutOfBudget = NumNewNewSpeculativelyAvailableBBs > MaxBBSpeculations;
756
757 // If we have exhausted our budget, mark this block as unavailable.
758 // Also, if this block has no predecessors, the value isn't live-in here.
759 if (OutOfBudget || pred_empty(CurrBB)) {
760 MaxBBSpeculationCutoffReachedTimes += (int)OutOfBudget;
761 State = AvailabilityState::Unavailable;
762 UnavailableBB = CurrBB;
763 break; // Backpropagate unavailability info.
764 }
765
766 // Tentatively consider this block as speculatively available.
767 #ifndef NDEBUG
768 NewSpeculativelyAvailableBBs.insert(CurrBB);
769 #endif
770 // And further recurse into block's predecessors, in depth-first order!
771 Worklist.append(pred_begin(CurrBB), pred_end(CurrBB));
772 }
773
774 #if LLVM_ENABLE_STATS
775 IsValueFullyAvailableInBlockNumSpeculationsMax.updateMax(
776 NumNewNewSpeculativelyAvailableBBs);
777 #endif
778
779 // If the block isn't marked as fixpoint yet
780 // (the Unavailable and Available states are fixpoints)
781 auto MarkAsFixpointAndEnqueueSuccessors =
782 [&](BasicBlock *BB, AvailabilityState FixpointState) {
783 auto It = FullyAvailableBlocks.find(BB);
784 if (It == FullyAvailableBlocks.end())
785 return; // Never queried this block, leave as-is.
786 switch (AvailabilityState &State = It->second) {
787 case AvailabilityState::Unavailable:
788 case AvailabilityState::Available:
789 return; // Don't backpropagate further, continue processing worklist.
790 case AvailabilityState::SpeculativelyAvailable: // Fix it!
791 State = FixpointState;
792 #ifndef NDEBUG
793 assert(NewSpeculativelyAvailableBBs.erase(BB) &&
794 "Found a speculatively available successor leftover?");
795 #endif
796 // Queue successors for further processing.
797 Worklist.append(succ_begin(BB), succ_end(BB));
798 return;
799 }
800 };
801
802 if (UnavailableBB) {
803 // Okay, we have encountered an unavailable block.
804 // Mark speculatively available blocks reachable from UnavailableBB as
805 // unavailable as well. Paths are terminated when they reach blocks not in
806 // FullyAvailableBlocks or they are not marked as speculatively available.
807 Worklist.clear();
808 Worklist.append(succ_begin(*UnavailableBB), succ_end(*UnavailableBB));
809 while (!Worklist.empty())
810 MarkAsFixpointAndEnqueueSuccessors(Worklist.pop_back_val(),
811 AvailabilityState::Unavailable);
812 }
813
814 #ifndef NDEBUG
815 Worklist.clear();
816 for (BasicBlock *AvailableBB : AvailableBBs)
817 Worklist.append(succ_begin(AvailableBB), succ_end(AvailableBB));
818 while (!Worklist.empty())
819 MarkAsFixpointAndEnqueueSuccessors(Worklist.pop_back_val(),
820 AvailabilityState::Available);
821
822 assert(NewSpeculativelyAvailableBBs.empty() &&
823 "Must have fixed all the new speculatively available blocks.");
824 #endif
825
826 return !UnavailableBB;
827 }
828
829 /// Given a set of loads specified by ValuesPerBlock,
830 /// construct SSA form, allowing us to eliminate LI. This returns the value
831 /// that should be used at LI's definition site.
ConstructSSAForLoadSet(LoadInst * LI,SmallVectorImpl<AvailableValueInBlock> & ValuesPerBlock,GVN & gvn)832 static Value *ConstructSSAForLoadSet(LoadInst *LI,
833 SmallVectorImpl<AvailableValueInBlock> &ValuesPerBlock,
834 GVN &gvn) {
835 // Check for the fully redundant, dominating load case. In this case, we can
836 // just use the dominating value directly.
837 if (ValuesPerBlock.size() == 1 &&
838 gvn.getDominatorTree().properlyDominates(ValuesPerBlock[0].BB,
839 LI->getParent())) {
840 assert(!ValuesPerBlock[0].AV.isUndefValue() &&
841 "Dead BB dominate this block");
842 return ValuesPerBlock[0].MaterializeAdjustedValue(LI, gvn);
843 }
844
845 // Otherwise, we have to construct SSA form.
846 SmallVector<PHINode*, 8> NewPHIs;
847 SSAUpdater SSAUpdate(&NewPHIs);
848 SSAUpdate.Initialize(LI->getType(), LI->getName());
849
850 for (const AvailableValueInBlock &AV : ValuesPerBlock) {
851 BasicBlock *BB = AV.BB;
852
853 if (SSAUpdate.HasValueForBlock(BB))
854 continue;
855
856 // If the value is the load that we will be eliminating, and the block it's
857 // available in is the block that the load is in, then don't add it as
858 // SSAUpdater will resolve the value to the relevant phi which may let it
859 // avoid phi construction entirely if there's actually only one value.
860 if (BB == LI->getParent() &&
861 ((AV.AV.isSimpleValue() && AV.AV.getSimpleValue() == LI) ||
862 (AV.AV.isCoercedLoadValue() && AV.AV.getCoercedLoadValue() == LI)))
863 continue;
864
865 SSAUpdate.AddAvailableValue(BB, AV.MaterializeAdjustedValue(LI, gvn));
866 }
867
868 // Perform PHI construction.
869 return SSAUpdate.GetValueInMiddleOfBlock(LI->getParent());
870 }
871
MaterializeAdjustedValue(LoadInst * LI,Instruction * InsertPt,GVN & gvn) const872 Value *AvailableValue::MaterializeAdjustedValue(LoadInst *LI,
873 Instruction *InsertPt,
874 GVN &gvn) const {
875 Value *Res;
876 Type *LoadTy = LI->getType();
877 const DataLayout &DL = LI->getModule()->getDataLayout();
878 if (isSimpleValue()) {
879 Res = getSimpleValue();
880 if (Res->getType() != LoadTy) {
881 Res = getStoreValueForLoad(Res, Offset, LoadTy, InsertPt, DL);
882
883 LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL VAL:\nOffset: " << Offset
884 << " " << *getSimpleValue() << '\n'
885 << *Res << '\n'
886 << "\n\n\n");
887 }
888 } else if (isCoercedLoadValue()) {
889 LoadInst *Load = getCoercedLoadValue();
890 if (Load->getType() == LoadTy && Offset == 0) {
891 Res = Load;
892 } else {
893 Res = getLoadValueForLoad(Load, Offset, LoadTy, InsertPt, DL);
894 // We would like to use gvn.markInstructionForDeletion here, but we can't
895 // because the load is already memoized into the leader map table that GVN
896 // tracks. It is potentially possible to remove the load from the table,
897 // but then there all of the operations based on it would need to be
898 // rehashed. Just leave the dead load around.
899 gvn.getMemDep().removeInstruction(Load);
900 LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL LOAD:\nOffset: " << Offset
901 << " " << *getCoercedLoadValue() << '\n'
902 << *Res << '\n'
903 << "\n\n\n");
904 }
905 } else if (isMemIntrinValue()) {
906 Res = getMemInstValueForLoad(getMemIntrinValue(), Offset, LoadTy,
907 InsertPt, DL);
908 LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL MEM INTRIN:\nOffset: " << Offset
909 << " " << *getMemIntrinValue() << '\n'
910 << *Res << '\n'
911 << "\n\n\n");
912 } else {
913 assert(isUndefValue() && "Should be UndefVal");
914 LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL Undef:\n";);
915 return UndefValue::get(LoadTy);
916 }
917 assert(Res && "failed to materialize?");
918 return Res;
919 }
920
isLifetimeStart(const Instruction * Inst)921 static bool isLifetimeStart(const Instruction *Inst) {
922 if (const IntrinsicInst* II = dyn_cast<IntrinsicInst>(Inst))
923 return II->getIntrinsicID() == Intrinsic::lifetime_start;
924 return false;
925 }
926
927 /// Try to locate the three instruction involved in a missed
928 /// load-elimination case that is due to an intervening store.
reportMayClobberedLoad(LoadInst * LI,MemDepResult DepInfo,DominatorTree * DT,OptimizationRemarkEmitter * ORE)929 static void reportMayClobberedLoad(LoadInst *LI, MemDepResult DepInfo,
930 DominatorTree *DT,
931 OptimizationRemarkEmitter *ORE) {
932 using namespace ore;
933
934 User *OtherAccess = nullptr;
935
936 OptimizationRemarkMissed R(DEBUG_TYPE, "LoadClobbered", LI);
937 R << "load of type " << NV("Type", LI->getType()) << " not eliminated"
938 << setExtraArgs();
939
940 for (auto *U : LI->getPointerOperand()->users())
941 if (U != LI && (isa<LoadInst>(U) || isa<StoreInst>(U)) &&
942 DT->dominates(cast<Instruction>(U), LI)) {
943 // FIXME: for now give up if there are multiple memory accesses that
944 // dominate the load. We need further analysis to decide which one is
945 // that we're forwarding from.
946 if (OtherAccess)
947 OtherAccess = nullptr;
948 else
949 OtherAccess = U;
950 }
951
952 if (OtherAccess)
953 R << " in favor of " << NV("OtherAccess", OtherAccess);
954
955 R << " because it is clobbered by " << NV("ClobberedBy", DepInfo.getInst());
956
957 ORE->emit(R);
958 }
959
AnalyzeLoadAvailability(LoadInst * LI,MemDepResult DepInfo,Value * Address,AvailableValue & Res)960 bool GVN::AnalyzeLoadAvailability(LoadInst *LI, MemDepResult DepInfo,
961 Value *Address, AvailableValue &Res) {
962 assert((DepInfo.isDef() || DepInfo.isClobber()) &&
963 "expected a local dependence");
964 assert(LI->isUnordered() && "rules below are incorrect for ordered access");
965
966 const DataLayout &DL = LI->getModule()->getDataLayout();
967
968 Instruction *DepInst = DepInfo.getInst();
969 if (DepInfo.isClobber()) {
970 // If the dependence is to a store that writes to a superset of the bits
971 // read by the load, we can extract the bits we need for the load from the
972 // stored value.
973 if (StoreInst *DepSI = dyn_cast<StoreInst>(DepInst)) {
974 // Can't forward from non-atomic to atomic without violating memory model.
975 if (Address && LI->isAtomic() <= DepSI->isAtomic()) {
976 int Offset =
977 analyzeLoadFromClobberingStore(LI->getType(), Address, DepSI, DL);
978 if (Offset != -1) {
979 Res = AvailableValue::get(DepSI->getValueOperand(), Offset);
980 return true;
981 }
982 }
983 }
984
985 // Check to see if we have something like this:
986 // load i32* P
987 // load i8* (P+1)
988 // if we have this, replace the later with an extraction from the former.
989 if (LoadInst *DepLI = dyn_cast<LoadInst>(DepInst)) {
990 // If this is a clobber and L is the first instruction in its block, then
991 // we have the first instruction in the entry block.
992 // Can't forward from non-atomic to atomic without violating memory model.
993 if (DepLI != LI && Address && LI->isAtomic() <= DepLI->isAtomic()) {
994 int Offset =
995 analyzeLoadFromClobberingLoad(LI->getType(), Address, DepLI, DL);
996
997 if (Offset != -1) {
998 Res = AvailableValue::getLoad(DepLI, Offset);
999 return true;
1000 }
1001 }
1002 }
1003
1004 // If the clobbering value is a memset/memcpy/memmove, see if we can
1005 // forward a value on from it.
1006 if (MemIntrinsic *DepMI = dyn_cast<MemIntrinsic>(DepInst)) {
1007 if (Address && !LI->isAtomic()) {
1008 int Offset = analyzeLoadFromClobberingMemInst(LI->getType(), Address,
1009 DepMI, DL);
1010 if (Offset != -1) {
1011 Res = AvailableValue::getMI(DepMI, Offset);
1012 return true;
1013 }
1014 }
1015 }
1016 // Nothing known about this clobber, have to be conservative
1017 LLVM_DEBUG(
1018 // fast print dep, using operator<< on instruction is too slow.
1019 dbgs() << "GVN: load "; LI->printAsOperand(dbgs());
1020 dbgs() << " is clobbered by " << *DepInst << '\n';);
1021 if (ORE->allowExtraAnalysis(DEBUG_TYPE))
1022 reportMayClobberedLoad(LI, DepInfo, DT, ORE);
1023
1024 return false;
1025 }
1026 assert(DepInfo.isDef() && "follows from above");
1027
1028 // Loading the allocation -> undef.
1029 if (isa<AllocaInst>(DepInst) || isMallocLikeFn(DepInst, TLI) ||
1030 isAlignedAllocLikeFn(DepInst, TLI) ||
1031 // Loading immediately after lifetime begin -> undef.
1032 isLifetimeStart(DepInst)) {
1033 Res = AvailableValue::get(UndefValue::get(LI->getType()));
1034 return true;
1035 }
1036
1037 // Loading from calloc (which zero initializes memory) -> zero
1038 if (isCallocLikeFn(DepInst, TLI)) {
1039 Res = AvailableValue::get(Constant::getNullValue(LI->getType()));
1040 return true;
1041 }
1042
1043 if (StoreInst *S = dyn_cast<StoreInst>(DepInst)) {
1044 // Reject loads and stores that are to the same address but are of
1045 // different types if we have to. If the stored value is convertable to
1046 // the loaded value, we can reuse it.
1047 if (!canCoerceMustAliasedValueToLoad(S->getValueOperand(), LI->getType(),
1048 DL))
1049 return false;
1050
1051 // Can't forward from non-atomic to atomic without violating memory model.
1052 if (S->isAtomic() < LI->isAtomic())
1053 return false;
1054
1055 Res = AvailableValue::get(S->getValueOperand());
1056 return true;
1057 }
1058
1059 if (LoadInst *LD = dyn_cast<LoadInst>(DepInst)) {
1060 // If the types mismatch and we can't handle it, reject reuse of the load.
1061 // If the stored value is larger or equal to the loaded value, we can reuse
1062 // it.
1063 if (!canCoerceMustAliasedValueToLoad(LD, LI->getType(), DL))
1064 return false;
1065
1066 // Can't forward from non-atomic to atomic without violating memory model.
1067 if (LD->isAtomic() < LI->isAtomic())
1068 return false;
1069
1070 Res = AvailableValue::getLoad(LD);
1071 return true;
1072 }
1073
1074 // Unknown def - must be conservative
1075 LLVM_DEBUG(
1076 // fast print dep, using operator<< on instruction is too slow.
1077 dbgs() << "GVN: load "; LI->printAsOperand(dbgs());
1078 dbgs() << " has unknown def " << *DepInst << '\n';);
1079 return false;
1080 }
1081
AnalyzeLoadAvailability(LoadInst * LI,LoadDepVect & Deps,AvailValInBlkVect & ValuesPerBlock,UnavailBlkVect & UnavailableBlocks)1082 void GVN::AnalyzeLoadAvailability(LoadInst *LI, LoadDepVect &Deps,
1083 AvailValInBlkVect &ValuesPerBlock,
1084 UnavailBlkVect &UnavailableBlocks) {
1085 // Filter out useless results (non-locals, etc). Keep track of the blocks
1086 // where we have a value available in repl, also keep track of whether we see
1087 // dependencies that produce an unknown value for the load (such as a call
1088 // that could potentially clobber the load).
1089 unsigned NumDeps = Deps.size();
1090 for (unsigned i = 0, e = NumDeps; i != e; ++i) {
1091 BasicBlock *DepBB = Deps[i].getBB();
1092 MemDepResult DepInfo = Deps[i].getResult();
1093
1094 if (DeadBlocks.count(DepBB)) {
1095 // Dead dependent mem-op disguise as a load evaluating the same value
1096 // as the load in question.
1097 ValuesPerBlock.push_back(AvailableValueInBlock::getUndef(DepBB));
1098 continue;
1099 }
1100
1101 if (!DepInfo.isDef() && !DepInfo.isClobber()) {
1102 UnavailableBlocks.push_back(DepBB);
1103 continue;
1104 }
1105
1106 // The address being loaded in this non-local block may not be the same as
1107 // the pointer operand of the load if PHI translation occurs. Make sure
1108 // to consider the right address.
1109 Value *Address = Deps[i].getAddress();
1110
1111 AvailableValue AV;
1112 if (AnalyzeLoadAvailability(LI, DepInfo, Address, AV)) {
1113 // subtlety: because we know this was a non-local dependency, we know
1114 // it's safe to materialize anywhere between the instruction within
1115 // DepInfo and the end of it's block.
1116 ValuesPerBlock.push_back(AvailableValueInBlock::get(DepBB,
1117 std::move(AV)));
1118 } else {
1119 UnavailableBlocks.push_back(DepBB);
1120 }
1121 }
1122
1123 assert(NumDeps == ValuesPerBlock.size() + UnavailableBlocks.size() &&
1124 "post condition violation");
1125 }
1126
PerformLoadPRE(LoadInst * LI,AvailValInBlkVect & ValuesPerBlock,UnavailBlkVect & UnavailableBlocks)1127 bool GVN::PerformLoadPRE(LoadInst *LI, AvailValInBlkVect &ValuesPerBlock,
1128 UnavailBlkVect &UnavailableBlocks) {
1129 // Okay, we have *some* definitions of the value. This means that the value
1130 // is available in some of our (transitive) predecessors. Lets think about
1131 // doing PRE of this load. This will involve inserting a new load into the
1132 // predecessor when it's not available. We could do this in general, but
1133 // prefer to not increase code size. As such, we only do this when we know
1134 // that we only have to insert *one* load (which means we're basically moving
1135 // the load, not inserting a new one).
1136
1137 SmallPtrSet<BasicBlock *, 4> Blockers(UnavailableBlocks.begin(),
1138 UnavailableBlocks.end());
1139
1140 // Let's find the first basic block with more than one predecessor. Walk
1141 // backwards through predecessors if needed.
1142 BasicBlock *LoadBB = LI->getParent();
1143 BasicBlock *TmpBB = LoadBB;
1144
1145 // Check that there is no implicit control flow instructions above our load in
1146 // its block. If there is an instruction that doesn't always pass the
1147 // execution to the following instruction, then moving through it may become
1148 // invalid. For example:
1149 //
1150 // int arr[LEN];
1151 // int index = ???;
1152 // ...
1153 // guard(0 <= index && index < LEN);
1154 // use(arr[index]);
1155 //
1156 // It is illegal to move the array access to any point above the guard,
1157 // because if the index is out of bounds we should deoptimize rather than
1158 // access the array.
1159 // Check that there is no guard in this block above our instruction.
1160 bool MustEnsureSafetyOfSpeculativeExecution =
1161 ICF->isDominatedByICFIFromSameBlock(LI);
1162
1163 while (TmpBB->getSinglePredecessor()) {
1164 TmpBB = TmpBB->getSinglePredecessor();
1165 if (TmpBB == LoadBB) // Infinite (unreachable) loop.
1166 return false;
1167 if (Blockers.count(TmpBB))
1168 return false;
1169
1170 // If any of these blocks has more than one successor (i.e. if the edge we
1171 // just traversed was critical), then there are other paths through this
1172 // block along which the load may not be anticipated. Hoisting the load
1173 // above this block would be adding the load to execution paths along
1174 // which it was not previously executed.
1175 if (TmpBB->getTerminator()->getNumSuccessors() != 1)
1176 return false;
1177
1178 // Check that there is no implicit control flow in a block above.
1179 MustEnsureSafetyOfSpeculativeExecution =
1180 MustEnsureSafetyOfSpeculativeExecution || ICF->hasICF(TmpBB);
1181 }
1182
1183 assert(TmpBB);
1184 LoadBB = TmpBB;
1185
1186 // Check to see how many predecessors have the loaded value fully
1187 // available.
1188 MapVector<BasicBlock *, Value *> PredLoads;
1189 DenseMap<BasicBlock *, AvailabilityState> FullyAvailableBlocks;
1190 for (const AvailableValueInBlock &AV : ValuesPerBlock)
1191 FullyAvailableBlocks[AV.BB] = AvailabilityState::Available;
1192 for (BasicBlock *UnavailableBB : UnavailableBlocks)
1193 FullyAvailableBlocks[UnavailableBB] = AvailabilityState::Unavailable;
1194
1195 SmallVector<BasicBlock *, 4> CriticalEdgePred;
1196 for (BasicBlock *Pred : predecessors(LoadBB)) {
1197 // If any predecessor block is an EH pad that does not allow non-PHI
1198 // instructions before the terminator, we can't PRE the load.
1199 if (Pred->getTerminator()->isEHPad()) {
1200 LLVM_DEBUG(
1201 dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD PREDECESSOR '"
1202 << Pred->getName() << "': " << *LI << '\n');
1203 return false;
1204 }
1205
1206 if (IsValueFullyAvailableInBlock(Pred, FullyAvailableBlocks)) {
1207 continue;
1208 }
1209
1210 if (Pred->getTerminator()->getNumSuccessors() != 1) {
1211 if (isa<IndirectBrInst>(Pred->getTerminator())) {
1212 LLVM_DEBUG(
1213 dbgs() << "COULD NOT PRE LOAD BECAUSE OF INDBR CRITICAL EDGE '"
1214 << Pred->getName() << "': " << *LI << '\n');
1215 return false;
1216 }
1217
1218 // FIXME: Can we support the fallthrough edge?
1219 if (isa<CallBrInst>(Pred->getTerminator())) {
1220 LLVM_DEBUG(
1221 dbgs() << "COULD NOT PRE LOAD BECAUSE OF CALLBR CRITICAL EDGE '"
1222 << Pred->getName() << "': " << *LI << '\n');
1223 return false;
1224 }
1225
1226 if (LoadBB->isEHPad()) {
1227 LLVM_DEBUG(
1228 dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD CRITICAL EDGE '"
1229 << Pred->getName() << "': " << *LI << '\n');
1230 return false;
1231 }
1232
1233 // Do not split backedge as it will break the canonical loop form.
1234 if (!isLoadPRESplitBackedgeEnabled())
1235 if (DT->dominates(LoadBB, Pred)) {
1236 LLVM_DEBUG(
1237 dbgs()
1238 << "COULD NOT PRE LOAD BECAUSE OF A BACKEDGE CRITICAL EDGE '"
1239 << Pred->getName() << "': " << *LI << '\n');
1240 return false;
1241 }
1242
1243 CriticalEdgePred.push_back(Pred);
1244 } else {
1245 // Only add the predecessors that will not be split for now.
1246 PredLoads[Pred] = nullptr;
1247 }
1248 }
1249
1250 // Decide whether PRE is profitable for this load.
1251 unsigned NumUnavailablePreds = PredLoads.size() + CriticalEdgePred.size();
1252 assert(NumUnavailablePreds != 0 &&
1253 "Fully available value should already be eliminated!");
1254
1255 // If this load is unavailable in multiple predecessors, reject it.
1256 // FIXME: If we could restructure the CFG, we could make a common pred with
1257 // all the preds that don't have an available LI and insert a new load into
1258 // that one block.
1259 if (NumUnavailablePreds != 1)
1260 return false;
1261
1262 // Now we know where we will insert load. We must ensure that it is safe
1263 // to speculatively execute the load at that points.
1264 if (MustEnsureSafetyOfSpeculativeExecution) {
1265 if (CriticalEdgePred.size())
1266 if (!isSafeToSpeculativelyExecute(LI, LoadBB->getFirstNonPHI(), DT))
1267 return false;
1268 for (auto &PL : PredLoads)
1269 if (!isSafeToSpeculativelyExecute(LI, PL.first->getTerminator(), DT))
1270 return false;
1271 }
1272
1273 // Split critical edges, and update the unavailable predecessors accordingly.
1274 for (BasicBlock *OrigPred : CriticalEdgePred) {
1275 BasicBlock *NewPred = splitCriticalEdges(OrigPred, LoadBB);
1276 assert(!PredLoads.count(OrigPred) && "Split edges shouldn't be in map!");
1277 PredLoads[NewPred] = nullptr;
1278 LLVM_DEBUG(dbgs() << "Split critical edge " << OrigPred->getName() << "->"
1279 << LoadBB->getName() << '\n');
1280 }
1281
1282 // Check if the load can safely be moved to all the unavailable predecessors.
1283 bool CanDoPRE = true;
1284 const DataLayout &DL = LI->getModule()->getDataLayout();
1285 SmallVector<Instruction*, 8> NewInsts;
1286 for (auto &PredLoad : PredLoads) {
1287 BasicBlock *UnavailablePred = PredLoad.first;
1288
1289 // Do PHI translation to get its value in the predecessor if necessary. The
1290 // returned pointer (if non-null) is guaranteed to dominate UnavailablePred.
1291 // We do the translation for each edge we skipped by going from LI's block
1292 // to LoadBB, otherwise we might miss pieces needing translation.
1293
1294 // If all preds have a single successor, then we know it is safe to insert
1295 // the load on the pred (?!?), so we can insert code to materialize the
1296 // pointer if it is not available.
1297 Value *LoadPtr = LI->getPointerOperand();
1298 BasicBlock *Cur = LI->getParent();
1299 while (Cur != LoadBB) {
1300 PHITransAddr Address(LoadPtr, DL, AC);
1301 LoadPtr = Address.PHITranslateWithInsertion(
1302 Cur, Cur->getSinglePredecessor(), *DT, NewInsts);
1303 if (!LoadPtr) {
1304 CanDoPRE = false;
1305 break;
1306 }
1307 Cur = Cur->getSinglePredecessor();
1308 }
1309
1310 if (LoadPtr) {
1311 PHITransAddr Address(LoadPtr, DL, AC);
1312 LoadPtr = Address.PHITranslateWithInsertion(LoadBB, UnavailablePred, *DT,
1313 NewInsts);
1314 }
1315 // If we couldn't find or insert a computation of this phi translated value,
1316 // we fail PRE.
1317 if (!LoadPtr) {
1318 LLVM_DEBUG(dbgs() << "COULDN'T INSERT PHI TRANSLATED VALUE OF: "
1319 << *LI->getPointerOperand() << "\n");
1320 CanDoPRE = false;
1321 break;
1322 }
1323
1324 PredLoad.second = LoadPtr;
1325 }
1326
1327 if (!CanDoPRE) {
1328 while (!NewInsts.empty()) {
1329 // Erase instructions generated by the failed PHI translation before
1330 // trying to number them. PHI translation might insert instructions
1331 // in basic blocks other than the current one, and we delete them
1332 // directly, as markInstructionForDeletion only allows removing from the
1333 // current basic block.
1334 NewInsts.pop_back_val()->eraseFromParent();
1335 }
1336 // HINT: Don't revert the edge-splitting as following transformation may
1337 // also need to split these critical edges.
1338 return !CriticalEdgePred.empty();
1339 }
1340
1341 // Okay, we can eliminate this load by inserting a reload in the predecessor
1342 // and using PHI construction to get the value in the other predecessors, do
1343 // it.
1344 LLVM_DEBUG(dbgs() << "GVN REMOVING PRE LOAD: " << *LI << '\n');
1345 LLVM_DEBUG(if (!NewInsts.empty()) dbgs()
1346 << "INSERTED " << NewInsts.size() << " INSTS: " << *NewInsts.back()
1347 << '\n');
1348
1349 // Assign value numbers to the new instructions.
1350 for (Instruction *I : NewInsts) {
1351 // Instructions that have been inserted in predecessor(s) to materialize
1352 // the load address do not retain their original debug locations. Doing
1353 // so could lead to confusing (but correct) source attributions.
1354 I->updateLocationAfterHoist();
1355
1356 // FIXME: We really _ought_ to insert these value numbers into their
1357 // parent's availability map. However, in doing so, we risk getting into
1358 // ordering issues. If a block hasn't been processed yet, we would be
1359 // marking a value as AVAIL-IN, which isn't what we intend.
1360 VN.lookupOrAdd(I);
1361 }
1362
1363 for (const auto &PredLoad : PredLoads) {
1364 BasicBlock *UnavailablePred = PredLoad.first;
1365 Value *LoadPtr = PredLoad.second;
1366
1367 auto *NewLoad = new LoadInst(
1368 LI->getType(), LoadPtr, LI->getName() + ".pre", LI->isVolatile(),
1369 LI->getAlign(), LI->getOrdering(), LI->getSyncScopeID(),
1370 UnavailablePred->getTerminator());
1371 NewLoad->setDebugLoc(LI->getDebugLoc());
1372 if (MSSAU) {
1373 auto *MSSA = MSSAU->getMemorySSA();
1374 // Get the defining access of the original load or use the load if it is a
1375 // MemoryDef (e.g. because it is volatile). The inserted loads are
1376 // guaranteed to load from the same definition.
1377 auto *LIAcc = MSSA->getMemoryAccess(LI);
1378 auto *DefiningAcc =
1379 isa<MemoryDef>(LIAcc) ? LIAcc : LIAcc->getDefiningAccess();
1380 auto *NewAccess = MSSAU->createMemoryAccessInBB(
1381 NewLoad, DefiningAcc, NewLoad->getParent(),
1382 MemorySSA::BeforeTerminator);
1383 if (auto *NewDef = dyn_cast<MemoryDef>(NewAccess))
1384 MSSAU->insertDef(NewDef, /*RenameUses=*/true);
1385 else
1386 MSSAU->insertUse(cast<MemoryUse>(NewAccess), /*RenameUses=*/true);
1387 }
1388
1389 // Transfer the old load's AA tags to the new load.
1390 AAMDNodes Tags;
1391 LI->getAAMetadata(Tags);
1392 if (Tags)
1393 NewLoad->setAAMetadata(Tags);
1394
1395 if (auto *MD = LI->getMetadata(LLVMContext::MD_invariant_load))
1396 NewLoad->setMetadata(LLVMContext::MD_invariant_load, MD);
1397 if (auto *InvGroupMD = LI->getMetadata(LLVMContext::MD_invariant_group))
1398 NewLoad->setMetadata(LLVMContext::MD_invariant_group, InvGroupMD);
1399 if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range))
1400 NewLoad->setMetadata(LLVMContext::MD_range, RangeMD);
1401
1402 // We do not propagate the old load's debug location, because the new
1403 // load now lives in a different BB, and we want to avoid a jumpy line
1404 // table.
1405 // FIXME: How do we retain source locations without causing poor debugging
1406 // behavior?
1407
1408 // Add the newly created load.
1409 ValuesPerBlock.push_back(AvailableValueInBlock::get(UnavailablePred,
1410 NewLoad));
1411 MD->invalidateCachedPointerInfo(LoadPtr);
1412 LLVM_DEBUG(dbgs() << "GVN INSERTED " << *NewLoad << '\n');
1413 }
1414
1415 // Perform PHI construction.
1416 Value *V = ConstructSSAForLoadSet(LI, ValuesPerBlock, *this);
1417 LI->replaceAllUsesWith(V);
1418 if (isa<PHINode>(V))
1419 V->takeName(LI);
1420 if (Instruction *I = dyn_cast<Instruction>(V))
1421 I->setDebugLoc(LI->getDebugLoc());
1422 if (V->getType()->isPtrOrPtrVectorTy())
1423 MD->invalidateCachedPointerInfo(V);
1424 markInstructionForDeletion(LI);
1425 ORE->emit([&]() {
1426 return OptimizationRemark(DEBUG_TYPE, "LoadPRE", LI)
1427 << "load eliminated by PRE";
1428 });
1429 ++NumPRELoad;
1430 return true;
1431 }
1432
reportLoadElim(LoadInst * LI,Value * AvailableValue,OptimizationRemarkEmitter * ORE)1433 static void reportLoadElim(LoadInst *LI, Value *AvailableValue,
1434 OptimizationRemarkEmitter *ORE) {
1435 using namespace ore;
1436
1437 ORE->emit([&]() {
1438 return OptimizationRemark(DEBUG_TYPE, "LoadElim", LI)
1439 << "load of type " << NV("Type", LI->getType()) << " eliminated"
1440 << setExtraArgs() << " in favor of "
1441 << NV("InfavorOfValue", AvailableValue);
1442 });
1443 }
1444
1445 /// Attempt to eliminate a load whose dependencies are
1446 /// non-local by performing PHI construction.
processNonLocalLoad(LoadInst * LI)1447 bool GVN::processNonLocalLoad(LoadInst *LI) {
1448 // non-local speculations are not allowed under asan.
1449 if (LI->getParent()->getParent()->hasFnAttribute(
1450 Attribute::SanitizeAddress) ||
1451 LI->getParent()->getParent()->hasFnAttribute(
1452 Attribute::SanitizeHWAddress))
1453 return false;
1454
1455 // Step 1: Find the non-local dependencies of the load.
1456 LoadDepVect Deps;
1457 MD->getNonLocalPointerDependency(LI, Deps);
1458
1459 // If we had to process more than one hundred blocks to find the
1460 // dependencies, this load isn't worth worrying about. Optimizing
1461 // it will be too expensive.
1462 unsigned NumDeps = Deps.size();
1463 if (NumDeps > MaxNumDeps)
1464 return false;
1465
1466 // If we had a phi translation failure, we'll have a single entry which is a
1467 // clobber in the current block. Reject this early.
1468 if (NumDeps == 1 &&
1469 !Deps[0].getResult().isDef() && !Deps[0].getResult().isClobber()) {
1470 LLVM_DEBUG(dbgs() << "GVN: non-local load "; LI->printAsOperand(dbgs());
1471 dbgs() << " has unknown dependencies\n";);
1472 return false;
1473 }
1474
1475 // If this load follows a GEP, see if we can PRE the indices before analyzing.
1476 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0))) {
1477 for (GetElementPtrInst::op_iterator OI = GEP->idx_begin(),
1478 OE = GEP->idx_end();
1479 OI != OE; ++OI)
1480 if (Instruction *I = dyn_cast<Instruction>(OI->get()))
1481 performScalarPRE(I);
1482 }
1483
1484 // Step 2: Analyze the availability of the load
1485 AvailValInBlkVect ValuesPerBlock;
1486 UnavailBlkVect UnavailableBlocks;
1487 AnalyzeLoadAvailability(LI, Deps, ValuesPerBlock, UnavailableBlocks);
1488
1489 // If we have no predecessors that produce a known value for this load, exit
1490 // early.
1491 if (ValuesPerBlock.empty())
1492 return false;
1493
1494 // Step 3: Eliminate fully redundancy.
1495 //
1496 // If all of the instructions we depend on produce a known value for this
1497 // load, then it is fully redundant and we can use PHI insertion to compute
1498 // its value. Insert PHIs and remove the fully redundant value now.
1499 if (UnavailableBlocks.empty()) {
1500 LLVM_DEBUG(dbgs() << "GVN REMOVING NONLOCAL LOAD: " << *LI << '\n');
1501
1502 // Perform PHI construction.
1503 Value *V = ConstructSSAForLoadSet(LI, ValuesPerBlock, *this);
1504 LI->replaceAllUsesWith(V);
1505
1506 if (isa<PHINode>(V))
1507 V->takeName(LI);
1508 if (Instruction *I = dyn_cast<Instruction>(V))
1509 // If instruction I has debug info, then we should not update it.
1510 // Also, if I has a null DebugLoc, then it is still potentially incorrect
1511 // to propagate LI's DebugLoc because LI may not post-dominate I.
1512 if (LI->getDebugLoc() && LI->getParent() == I->getParent())
1513 I->setDebugLoc(LI->getDebugLoc());
1514 if (V->getType()->isPtrOrPtrVectorTy())
1515 MD->invalidateCachedPointerInfo(V);
1516 markInstructionForDeletion(LI);
1517 ++NumGVNLoad;
1518 reportLoadElim(LI, V, ORE);
1519 return true;
1520 }
1521
1522 // Step 4: Eliminate partial redundancy.
1523 if (!isPREEnabled() || !isLoadPREEnabled())
1524 return false;
1525 if (!isLoadInLoopPREEnabled() && this->LI &&
1526 this->LI->getLoopFor(LI->getParent()))
1527 return false;
1528
1529 return PerformLoadPRE(LI, ValuesPerBlock, UnavailableBlocks);
1530 }
1531
impliesEquivalanceIfTrue(CmpInst * Cmp)1532 static bool impliesEquivalanceIfTrue(CmpInst* Cmp) {
1533 if (Cmp->getPredicate() == CmpInst::Predicate::ICMP_EQ)
1534 return true;
1535
1536 // Floating point comparisons can be equal, but not equivalent. Cases:
1537 // NaNs for unordered operators
1538 // +0.0 vs 0.0 for all operators
1539 if (Cmp->getPredicate() == CmpInst::Predicate::FCMP_OEQ ||
1540 (Cmp->getPredicate() == CmpInst::Predicate::FCMP_UEQ &&
1541 Cmp->getFastMathFlags().noNaNs())) {
1542 Value *LHS = Cmp->getOperand(0);
1543 Value *RHS = Cmp->getOperand(1);
1544 // If we can prove either side non-zero, then equality must imply
1545 // equivalence.
1546 // FIXME: We should do this optimization if 'no signed zeros' is
1547 // applicable via an instruction-level fast-math-flag or some other
1548 // indicator that relaxed FP semantics are being used.
1549 if (isa<ConstantFP>(LHS) && !cast<ConstantFP>(LHS)->isZero())
1550 return true;
1551 if (isa<ConstantFP>(RHS) && !cast<ConstantFP>(RHS)->isZero())
1552 return true;;
1553 // TODO: Handle vector floating point constants
1554 }
1555 return false;
1556 }
1557
impliesEquivalanceIfFalse(CmpInst * Cmp)1558 static bool impliesEquivalanceIfFalse(CmpInst* Cmp) {
1559 if (Cmp->getPredicate() == CmpInst::Predicate::ICMP_NE)
1560 return true;
1561
1562 // Floating point comparisons can be equal, but not equivelent. Cases:
1563 // NaNs for unordered operators
1564 // +0.0 vs 0.0 for all operators
1565 if ((Cmp->getPredicate() == CmpInst::Predicate::FCMP_ONE &&
1566 Cmp->getFastMathFlags().noNaNs()) ||
1567 Cmp->getPredicate() == CmpInst::Predicate::FCMP_UNE) {
1568 Value *LHS = Cmp->getOperand(0);
1569 Value *RHS = Cmp->getOperand(1);
1570 // If we can prove either side non-zero, then equality must imply
1571 // equivalence.
1572 // FIXME: We should do this optimization if 'no signed zeros' is
1573 // applicable via an instruction-level fast-math-flag or some other
1574 // indicator that relaxed FP semantics are being used.
1575 if (isa<ConstantFP>(LHS) && !cast<ConstantFP>(LHS)->isZero())
1576 return true;
1577 if (isa<ConstantFP>(RHS) && !cast<ConstantFP>(RHS)->isZero())
1578 return true;;
1579 // TODO: Handle vector floating point constants
1580 }
1581 return false;
1582 }
1583
1584
hasUsersIn(Value * V,BasicBlock * BB)1585 static bool hasUsersIn(Value *V, BasicBlock *BB) {
1586 for (User *U : V->users())
1587 if (isa<Instruction>(U) &&
1588 cast<Instruction>(U)->getParent() == BB)
1589 return true;
1590 return false;
1591 }
1592
processAssumeIntrinsic(IntrinsicInst * IntrinsicI)1593 bool GVN::processAssumeIntrinsic(IntrinsicInst *IntrinsicI) {
1594 assert(IntrinsicI->getIntrinsicID() == Intrinsic::assume &&
1595 "This function can only be called with llvm.assume intrinsic");
1596 Value *V = IntrinsicI->getArgOperand(0);
1597
1598 if (ConstantInt *Cond = dyn_cast<ConstantInt>(V)) {
1599 if (Cond->isZero()) {
1600 Type *Int8Ty = Type::getInt8Ty(V->getContext());
1601 // Insert a new store to null instruction before the load to indicate that
1602 // this code is not reachable. FIXME: We could insert unreachable
1603 // instruction directly because we can modify the CFG.
1604 auto *NewS = new StoreInst(UndefValue::get(Int8Ty),
1605 Constant::getNullValue(Int8Ty->getPointerTo()),
1606 IntrinsicI);
1607 if (MSSAU) {
1608 const MemoryUseOrDef *FirstNonDom = nullptr;
1609 const auto *AL =
1610 MSSAU->getMemorySSA()->getBlockAccesses(IntrinsicI->getParent());
1611
1612 // If there are accesses in the current basic block, find the first one
1613 // that does not come before NewS. The new memory access is inserted
1614 // after the found access or before the terminator if no such access is
1615 // found.
1616 if (AL) {
1617 for (auto &Acc : *AL) {
1618 if (auto *Current = dyn_cast<MemoryUseOrDef>(&Acc))
1619 if (!Current->getMemoryInst()->comesBefore(NewS)) {
1620 FirstNonDom = Current;
1621 break;
1622 }
1623 }
1624 }
1625
1626 // This added store is to null, so it will never executed and we can
1627 // just use the LiveOnEntry def as defining access.
1628 auto *NewDef =
1629 FirstNonDom ? MSSAU->createMemoryAccessBefore(
1630 NewS, MSSAU->getMemorySSA()->getLiveOnEntryDef(),
1631 const_cast<MemoryUseOrDef *>(FirstNonDom))
1632 : MSSAU->createMemoryAccessInBB(
1633 NewS, MSSAU->getMemorySSA()->getLiveOnEntryDef(),
1634 NewS->getParent(), MemorySSA::BeforeTerminator);
1635
1636 MSSAU->insertDef(cast<MemoryDef>(NewDef), /*RenameUses=*/false);
1637 }
1638 }
1639 if (isAssumeWithEmptyBundle(*IntrinsicI))
1640 markInstructionForDeletion(IntrinsicI);
1641 return false;
1642 } else if (isa<Constant>(V)) {
1643 // If it's not false, and constant, it must evaluate to true. This means our
1644 // assume is assume(true), and thus, pointless, and we don't want to do
1645 // anything more here.
1646 return false;
1647 }
1648
1649 Constant *True = ConstantInt::getTrue(V->getContext());
1650 bool Changed = false;
1651
1652 for (BasicBlock *Successor : successors(IntrinsicI->getParent())) {
1653 BasicBlockEdge Edge(IntrinsicI->getParent(), Successor);
1654
1655 // This property is only true in dominated successors, propagateEquality
1656 // will check dominance for us.
1657 Changed |= propagateEquality(V, True, Edge, false);
1658 }
1659
1660 // We can replace assume value with true, which covers cases like this:
1661 // call void @llvm.assume(i1 %cmp)
1662 // br i1 %cmp, label %bb1, label %bb2 ; will change %cmp to true
1663 ReplaceOperandsWithMap[V] = True;
1664
1665 // Similarly, after assume(!NotV) we know that NotV == false.
1666 Value *NotV;
1667 if (match(V, m_Not(m_Value(NotV))))
1668 ReplaceOperandsWithMap[NotV] = ConstantInt::getFalse(V->getContext());
1669
1670 // If we find an equality fact, canonicalize all dominated uses in this block
1671 // to one of the two values. We heuristically choice the "oldest" of the
1672 // two where age is determined by value number. (Note that propagateEquality
1673 // above handles the cross block case.)
1674 //
1675 // Key case to cover are:
1676 // 1)
1677 // %cmp = fcmp oeq float 3.000000e+00, %0 ; const on lhs could happen
1678 // call void @llvm.assume(i1 %cmp)
1679 // ret float %0 ; will change it to ret float 3.000000e+00
1680 // 2)
1681 // %load = load float, float* %addr
1682 // %cmp = fcmp oeq float %load, %0
1683 // call void @llvm.assume(i1 %cmp)
1684 // ret float %load ; will change it to ret float %0
1685 if (auto *CmpI = dyn_cast<CmpInst>(V)) {
1686 if (impliesEquivalanceIfTrue(CmpI)) {
1687 Value *CmpLHS = CmpI->getOperand(0);
1688 Value *CmpRHS = CmpI->getOperand(1);
1689 // Heuristically pick the better replacement -- the choice of heuristic
1690 // isn't terribly important here, but the fact we canonicalize on some
1691 // replacement is for exposing other simplifications.
1692 // TODO: pull this out as a helper function and reuse w/existing
1693 // (slightly different) logic.
1694 if (isa<Constant>(CmpLHS) && !isa<Constant>(CmpRHS))
1695 std::swap(CmpLHS, CmpRHS);
1696 if (!isa<Instruction>(CmpLHS) && isa<Instruction>(CmpRHS))
1697 std::swap(CmpLHS, CmpRHS);
1698 if ((isa<Argument>(CmpLHS) && isa<Argument>(CmpRHS)) ||
1699 (isa<Instruction>(CmpLHS) && isa<Instruction>(CmpRHS))) {
1700 // Move the 'oldest' value to the right-hand side, using the value
1701 // number as a proxy for age.
1702 uint32_t LVN = VN.lookupOrAdd(CmpLHS);
1703 uint32_t RVN = VN.lookupOrAdd(CmpRHS);
1704 if (LVN < RVN)
1705 std::swap(CmpLHS, CmpRHS);
1706 }
1707
1708 // Handle degenerate case where we either haven't pruned a dead path or a
1709 // removed a trivial assume yet.
1710 if (isa<Constant>(CmpLHS) && isa<Constant>(CmpRHS))
1711 return Changed;
1712
1713 LLVM_DEBUG(dbgs() << "Replacing dominated uses of "
1714 << *CmpLHS << " with "
1715 << *CmpRHS << " in block "
1716 << IntrinsicI->getParent()->getName() << "\n");
1717
1718
1719 // Setup the replacement map - this handles uses within the same block
1720 if (hasUsersIn(CmpLHS, IntrinsicI->getParent()))
1721 ReplaceOperandsWithMap[CmpLHS] = CmpRHS;
1722
1723 // NOTE: The non-block local cases are handled by the call to
1724 // propagateEquality above; this block is just about handling the block
1725 // local cases. TODO: There's a bunch of logic in propagateEqualiy which
1726 // isn't duplicated for the block local case, can we share it somehow?
1727 }
1728 }
1729 return Changed;
1730 }
1731
patchAndReplaceAllUsesWith(Instruction * I,Value * Repl)1732 static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl) {
1733 patchReplacementInstruction(I, Repl);
1734 I->replaceAllUsesWith(Repl);
1735 }
1736
1737 /// Attempt to eliminate a load, first by eliminating it
1738 /// locally, and then attempting non-local elimination if that fails.
processLoad(LoadInst * L)1739 bool GVN::processLoad(LoadInst *L) {
1740 if (!MD)
1741 return false;
1742
1743 // This code hasn't been audited for ordered or volatile memory access
1744 if (!L->isUnordered())
1745 return false;
1746
1747 if (L->use_empty()) {
1748 markInstructionForDeletion(L);
1749 return true;
1750 }
1751
1752 // ... to a pointer that has been loaded from before...
1753 MemDepResult Dep = MD->getDependency(L);
1754
1755 // If it is defined in another block, try harder.
1756 if (Dep.isNonLocal())
1757 return processNonLocalLoad(L);
1758
1759 // Only handle the local case below
1760 if (!Dep.isDef() && !Dep.isClobber()) {
1761 // This might be a NonFuncLocal or an Unknown
1762 LLVM_DEBUG(
1763 // fast print dep, using operator<< on instruction is too slow.
1764 dbgs() << "GVN: load "; L->printAsOperand(dbgs());
1765 dbgs() << " has unknown dependence\n";);
1766 return false;
1767 }
1768
1769 AvailableValue AV;
1770 if (AnalyzeLoadAvailability(L, Dep, L->getPointerOperand(), AV)) {
1771 Value *AvailableValue = AV.MaterializeAdjustedValue(L, L, *this);
1772
1773 // Replace the load!
1774 patchAndReplaceAllUsesWith(L, AvailableValue);
1775 markInstructionForDeletion(L);
1776 if (MSSAU)
1777 MSSAU->removeMemoryAccess(L);
1778 ++NumGVNLoad;
1779 reportLoadElim(L, AvailableValue, ORE);
1780 // Tell MDA to rexamine the reused pointer since we might have more
1781 // information after forwarding it.
1782 if (MD && AvailableValue->getType()->isPtrOrPtrVectorTy())
1783 MD->invalidateCachedPointerInfo(AvailableValue);
1784 return true;
1785 }
1786
1787 return false;
1788 }
1789
1790 /// Return a pair the first field showing the value number of \p Exp and the
1791 /// second field showing whether it is a value number newly created.
1792 std::pair<uint32_t, bool>
assignExpNewValueNum(Expression & Exp)1793 GVN::ValueTable::assignExpNewValueNum(Expression &Exp) {
1794 uint32_t &e = expressionNumbering[Exp];
1795 bool CreateNewValNum = !e;
1796 if (CreateNewValNum) {
1797 Expressions.push_back(Exp);
1798 if (ExprIdx.size() < nextValueNumber + 1)
1799 ExprIdx.resize(nextValueNumber * 2);
1800 e = nextValueNumber;
1801 ExprIdx[nextValueNumber++] = nextExprNumber++;
1802 }
1803 return {e, CreateNewValNum};
1804 }
1805
1806 /// Return whether all the values related with the same \p num are
1807 /// defined in \p BB.
areAllValsInBB(uint32_t Num,const BasicBlock * BB,GVN & Gvn)1808 bool GVN::ValueTable::areAllValsInBB(uint32_t Num, const BasicBlock *BB,
1809 GVN &Gvn) {
1810 LeaderTableEntry *Vals = &Gvn.LeaderTable[Num];
1811 while (Vals && Vals->BB == BB)
1812 Vals = Vals->Next;
1813 return !Vals;
1814 }
1815
1816 /// Wrap phiTranslateImpl to provide caching functionality.
phiTranslate(const BasicBlock * Pred,const BasicBlock * PhiBlock,uint32_t Num,GVN & Gvn)1817 uint32_t GVN::ValueTable::phiTranslate(const BasicBlock *Pred,
1818 const BasicBlock *PhiBlock, uint32_t Num,
1819 GVN &Gvn) {
1820 auto FindRes = PhiTranslateTable.find({Num, Pred});
1821 if (FindRes != PhiTranslateTable.end())
1822 return FindRes->second;
1823 uint32_t NewNum = phiTranslateImpl(Pred, PhiBlock, Num, Gvn);
1824 PhiTranslateTable.insert({{Num, Pred}, NewNum});
1825 return NewNum;
1826 }
1827
1828 // Return true if the value number \p Num and NewNum have equal value.
1829 // Return false if the result is unknown.
areCallValsEqual(uint32_t Num,uint32_t NewNum,const BasicBlock * Pred,const BasicBlock * PhiBlock,GVN & Gvn)1830 bool GVN::ValueTable::areCallValsEqual(uint32_t Num, uint32_t NewNum,
1831 const BasicBlock *Pred,
1832 const BasicBlock *PhiBlock, GVN &Gvn) {
1833 CallInst *Call = nullptr;
1834 LeaderTableEntry *Vals = &Gvn.LeaderTable[Num];
1835 while (Vals) {
1836 Call = dyn_cast<CallInst>(Vals->Val);
1837 if (Call && Call->getParent() == PhiBlock)
1838 break;
1839 Vals = Vals->Next;
1840 }
1841
1842 if (AA->doesNotAccessMemory(Call))
1843 return true;
1844
1845 if (!MD || !AA->onlyReadsMemory(Call))
1846 return false;
1847
1848 MemDepResult local_dep = MD->getDependency(Call);
1849 if (!local_dep.isNonLocal())
1850 return false;
1851
1852 const MemoryDependenceResults::NonLocalDepInfo &deps =
1853 MD->getNonLocalCallDependency(Call);
1854
1855 // Check to see if the Call has no function local clobber.
1856 for (unsigned i = 0; i < deps.size(); i++) {
1857 if (deps[i].getResult().isNonFuncLocal())
1858 return true;
1859 }
1860 return false;
1861 }
1862
1863 /// Translate value number \p Num using phis, so that it has the values of
1864 /// the phis in BB.
phiTranslateImpl(const BasicBlock * Pred,const BasicBlock * PhiBlock,uint32_t Num,GVN & Gvn)1865 uint32_t GVN::ValueTable::phiTranslateImpl(const BasicBlock *Pred,
1866 const BasicBlock *PhiBlock,
1867 uint32_t Num, GVN &Gvn) {
1868 if (PHINode *PN = NumberingPhi[Num]) {
1869 for (unsigned i = 0; i != PN->getNumIncomingValues(); ++i) {
1870 if (PN->getParent() == PhiBlock && PN->getIncomingBlock(i) == Pred)
1871 if (uint32_t TransVal = lookup(PN->getIncomingValue(i), false))
1872 return TransVal;
1873 }
1874 return Num;
1875 }
1876
1877 // If there is any value related with Num is defined in a BB other than
1878 // PhiBlock, it cannot depend on a phi in PhiBlock without going through
1879 // a backedge. We can do an early exit in that case to save compile time.
1880 if (!areAllValsInBB(Num, PhiBlock, Gvn))
1881 return Num;
1882
1883 if (Num >= ExprIdx.size() || ExprIdx[Num] == 0)
1884 return Num;
1885 Expression Exp = Expressions[ExprIdx[Num]];
1886
1887 for (unsigned i = 0; i < Exp.varargs.size(); i++) {
1888 // For InsertValue and ExtractValue, some varargs are index numbers
1889 // instead of value numbers. Those index numbers should not be
1890 // translated.
1891 if ((i > 1 && Exp.opcode == Instruction::InsertValue) ||
1892 (i > 0 && Exp.opcode == Instruction::ExtractValue) ||
1893 (i > 1 && Exp.opcode == Instruction::ShuffleVector))
1894 continue;
1895 Exp.varargs[i] = phiTranslate(Pred, PhiBlock, Exp.varargs[i], Gvn);
1896 }
1897
1898 if (Exp.commutative) {
1899 assert(Exp.varargs.size() >= 2 && "Unsupported commutative instruction!");
1900 if (Exp.varargs[0] > Exp.varargs[1]) {
1901 std::swap(Exp.varargs[0], Exp.varargs[1]);
1902 uint32_t Opcode = Exp.opcode >> 8;
1903 if (Opcode == Instruction::ICmp || Opcode == Instruction::FCmp)
1904 Exp.opcode = (Opcode << 8) |
1905 CmpInst::getSwappedPredicate(
1906 static_cast<CmpInst::Predicate>(Exp.opcode & 255));
1907 }
1908 }
1909
1910 if (uint32_t NewNum = expressionNumbering[Exp]) {
1911 if (Exp.opcode == Instruction::Call && NewNum != Num)
1912 return areCallValsEqual(Num, NewNum, Pred, PhiBlock, Gvn) ? NewNum : Num;
1913 return NewNum;
1914 }
1915 return Num;
1916 }
1917
1918 /// Erase stale entry from phiTranslate cache so phiTranslate can be computed
1919 /// again.
eraseTranslateCacheEntry(uint32_t Num,const BasicBlock & CurrBlock)1920 void GVN::ValueTable::eraseTranslateCacheEntry(uint32_t Num,
1921 const BasicBlock &CurrBlock) {
1922 for (const BasicBlock *Pred : predecessors(&CurrBlock)) {
1923 auto FindRes = PhiTranslateTable.find({Num, Pred});
1924 if (FindRes != PhiTranslateTable.end())
1925 PhiTranslateTable.erase(FindRes);
1926 }
1927 }
1928
1929 // In order to find a leader for a given value number at a
1930 // specific basic block, we first obtain the list of all Values for that number,
1931 // and then scan the list to find one whose block dominates the block in
1932 // question. This is fast because dominator tree queries consist of only
1933 // a few comparisons of DFS numbers.
findLeader(const BasicBlock * BB,uint32_t num)1934 Value *GVN::findLeader(const BasicBlock *BB, uint32_t num) {
1935 LeaderTableEntry Vals = LeaderTable[num];
1936 if (!Vals.Val) return nullptr;
1937
1938 Value *Val = nullptr;
1939 if (DT->dominates(Vals.BB, BB)) {
1940 Val = Vals.Val;
1941 if (isa<Constant>(Val)) return Val;
1942 }
1943
1944 LeaderTableEntry* Next = Vals.Next;
1945 while (Next) {
1946 if (DT->dominates(Next->BB, BB)) {
1947 if (isa<Constant>(Next->Val)) return Next->Val;
1948 if (!Val) Val = Next->Val;
1949 }
1950
1951 Next = Next->Next;
1952 }
1953
1954 return Val;
1955 }
1956
1957 /// There is an edge from 'Src' to 'Dst'. Return
1958 /// true if every path from the entry block to 'Dst' passes via this edge. In
1959 /// particular 'Dst' must not be reachable via another edge from 'Src'.
isOnlyReachableViaThisEdge(const BasicBlockEdge & E,DominatorTree * DT)1960 static bool isOnlyReachableViaThisEdge(const BasicBlockEdge &E,
1961 DominatorTree *DT) {
1962 // While in theory it is interesting to consider the case in which Dst has
1963 // more than one predecessor, because Dst might be part of a loop which is
1964 // only reachable from Src, in practice it is pointless since at the time
1965 // GVN runs all such loops have preheaders, which means that Dst will have
1966 // been changed to have only one predecessor, namely Src.
1967 const BasicBlock *Pred = E.getEnd()->getSinglePredecessor();
1968 assert((!Pred || Pred == E.getStart()) &&
1969 "No edge between these basic blocks!");
1970 return Pred != nullptr;
1971 }
1972
assignBlockRPONumber(Function & F)1973 void GVN::assignBlockRPONumber(Function &F) {
1974 BlockRPONumber.clear();
1975 uint32_t NextBlockNumber = 1;
1976 ReversePostOrderTraversal<Function *> RPOT(&F);
1977 for (BasicBlock *BB : RPOT)
1978 BlockRPONumber[BB] = NextBlockNumber++;
1979 InvalidBlockRPONumbers = false;
1980 }
1981
replaceOperandsForInBlockEquality(Instruction * Instr) const1982 bool GVN::replaceOperandsForInBlockEquality(Instruction *Instr) const {
1983 bool Changed = false;
1984 for (unsigned OpNum = 0; OpNum < Instr->getNumOperands(); ++OpNum) {
1985 Value *Operand = Instr->getOperand(OpNum);
1986 auto it = ReplaceOperandsWithMap.find(Operand);
1987 if (it != ReplaceOperandsWithMap.end()) {
1988 LLVM_DEBUG(dbgs() << "GVN replacing: " << *Operand << " with "
1989 << *it->second << " in instruction " << *Instr << '\n');
1990 Instr->setOperand(OpNum, it->second);
1991 Changed = true;
1992 }
1993 }
1994 return Changed;
1995 }
1996
1997 /// The given values are known to be equal in every block
1998 /// dominated by 'Root'. Exploit this, for example by replacing 'LHS' with
1999 /// 'RHS' everywhere in the scope. Returns whether a change was made.
2000 /// If DominatesByEdge is false, then it means that we will propagate the RHS
2001 /// value starting from the end of Root.Start.
propagateEquality(Value * LHS,Value * RHS,const BasicBlockEdge & Root,bool DominatesByEdge)2002 bool GVN::propagateEquality(Value *LHS, Value *RHS, const BasicBlockEdge &Root,
2003 bool DominatesByEdge) {
2004 SmallVector<std::pair<Value*, Value*>, 4> Worklist;
2005 Worklist.push_back(std::make_pair(LHS, RHS));
2006 bool Changed = false;
2007 // For speed, compute a conservative fast approximation to
2008 // DT->dominates(Root, Root.getEnd());
2009 const bool RootDominatesEnd = isOnlyReachableViaThisEdge(Root, DT);
2010
2011 while (!Worklist.empty()) {
2012 std::pair<Value*, Value*> Item = Worklist.pop_back_val();
2013 LHS = Item.first; RHS = Item.second;
2014
2015 if (LHS == RHS)
2016 continue;
2017 assert(LHS->getType() == RHS->getType() && "Equality but unequal types!");
2018
2019 // Don't try to propagate equalities between constants.
2020 if (isa<Constant>(LHS) && isa<Constant>(RHS))
2021 continue;
2022
2023 // Prefer a constant on the right-hand side, or an Argument if no constants.
2024 if (isa<Constant>(LHS) || (isa<Argument>(LHS) && !isa<Constant>(RHS)))
2025 std::swap(LHS, RHS);
2026 assert((isa<Argument>(LHS) || isa<Instruction>(LHS)) && "Unexpected value!");
2027
2028 // If there is no obvious reason to prefer the left-hand side over the
2029 // right-hand side, ensure the longest lived term is on the right-hand side,
2030 // so the shortest lived term will be replaced by the longest lived.
2031 // This tends to expose more simplifications.
2032 uint32_t LVN = VN.lookupOrAdd(LHS);
2033 if ((isa<Argument>(LHS) && isa<Argument>(RHS)) ||
2034 (isa<Instruction>(LHS) && isa<Instruction>(RHS))) {
2035 // Move the 'oldest' value to the right-hand side, using the value number
2036 // as a proxy for age.
2037 uint32_t RVN = VN.lookupOrAdd(RHS);
2038 if (LVN < RVN) {
2039 std::swap(LHS, RHS);
2040 LVN = RVN;
2041 }
2042 }
2043
2044 // If value numbering later sees that an instruction in the scope is equal
2045 // to 'LHS' then ensure it will be turned into 'RHS'. In order to preserve
2046 // the invariant that instructions only occur in the leader table for their
2047 // own value number (this is used by removeFromLeaderTable), do not do this
2048 // if RHS is an instruction (if an instruction in the scope is morphed into
2049 // LHS then it will be turned into RHS by the next GVN iteration anyway, so
2050 // using the leader table is about compiling faster, not optimizing better).
2051 // The leader table only tracks basic blocks, not edges. Only add to if we
2052 // have the simple case where the edge dominates the end.
2053 if (RootDominatesEnd && !isa<Instruction>(RHS))
2054 addToLeaderTable(LVN, RHS, Root.getEnd());
2055
2056 // Replace all occurrences of 'LHS' with 'RHS' everywhere in the scope. As
2057 // LHS always has at least one use that is not dominated by Root, this will
2058 // never do anything if LHS has only one use.
2059 if (!LHS->hasOneUse()) {
2060 unsigned NumReplacements =
2061 DominatesByEdge
2062 ? replaceDominatedUsesWith(LHS, RHS, *DT, Root)
2063 : replaceDominatedUsesWith(LHS, RHS, *DT, Root.getStart());
2064
2065 Changed |= NumReplacements > 0;
2066 NumGVNEqProp += NumReplacements;
2067 // Cached information for anything that uses LHS will be invalid.
2068 if (MD)
2069 MD->invalidateCachedPointerInfo(LHS);
2070 }
2071
2072 // Now try to deduce additional equalities from this one. For example, if
2073 // the known equality was "(A != B)" == "false" then it follows that A and B
2074 // are equal in the scope. Only boolean equalities with an explicit true or
2075 // false RHS are currently supported.
2076 if (!RHS->getType()->isIntegerTy(1))
2077 // Not a boolean equality - bail out.
2078 continue;
2079 ConstantInt *CI = dyn_cast<ConstantInt>(RHS);
2080 if (!CI)
2081 // RHS neither 'true' nor 'false' - bail out.
2082 continue;
2083 // Whether RHS equals 'true'. Otherwise it equals 'false'.
2084 bool isKnownTrue = CI->isMinusOne();
2085 bool isKnownFalse = !isKnownTrue;
2086
2087 // If "A && B" is known true then both A and B are known true. If "A || B"
2088 // is known false then both A and B are known false.
2089 Value *A, *B;
2090 if ((isKnownTrue && match(LHS, m_And(m_Value(A), m_Value(B)))) ||
2091 (isKnownFalse && match(LHS, m_Or(m_Value(A), m_Value(B))))) {
2092 Worklist.push_back(std::make_pair(A, RHS));
2093 Worklist.push_back(std::make_pair(B, RHS));
2094 continue;
2095 }
2096
2097 // If we are propagating an equality like "(A == B)" == "true" then also
2098 // propagate the equality A == B. When propagating a comparison such as
2099 // "(A >= B)" == "true", replace all instances of "A < B" with "false".
2100 if (CmpInst *Cmp = dyn_cast<CmpInst>(LHS)) {
2101 Value *Op0 = Cmp->getOperand(0), *Op1 = Cmp->getOperand(1);
2102
2103 // If "A == B" is known true, or "A != B" is known false, then replace
2104 // A with B everywhere in the scope. For floating point operations, we
2105 // have to be careful since equality does not always imply equivalance.
2106 if ((isKnownTrue && impliesEquivalanceIfTrue(Cmp)) ||
2107 (isKnownFalse && impliesEquivalanceIfFalse(Cmp)))
2108 Worklist.push_back(std::make_pair(Op0, Op1));
2109
2110 // If "A >= B" is known true, replace "A < B" with false everywhere.
2111 CmpInst::Predicate NotPred = Cmp->getInversePredicate();
2112 Constant *NotVal = ConstantInt::get(Cmp->getType(), isKnownFalse);
2113 // Since we don't have the instruction "A < B" immediately to hand, work
2114 // out the value number that it would have and use that to find an
2115 // appropriate instruction (if any).
2116 uint32_t NextNum = VN.getNextUnusedValueNumber();
2117 uint32_t Num = VN.lookupOrAddCmp(Cmp->getOpcode(), NotPred, Op0, Op1);
2118 // If the number we were assigned was brand new then there is no point in
2119 // looking for an instruction realizing it: there cannot be one!
2120 if (Num < NextNum) {
2121 Value *NotCmp = findLeader(Root.getEnd(), Num);
2122 if (NotCmp && isa<Instruction>(NotCmp)) {
2123 unsigned NumReplacements =
2124 DominatesByEdge
2125 ? replaceDominatedUsesWith(NotCmp, NotVal, *DT, Root)
2126 : replaceDominatedUsesWith(NotCmp, NotVal, *DT,
2127 Root.getStart());
2128 Changed |= NumReplacements > 0;
2129 NumGVNEqProp += NumReplacements;
2130 // Cached information for anything that uses NotCmp will be invalid.
2131 if (MD)
2132 MD->invalidateCachedPointerInfo(NotCmp);
2133 }
2134 }
2135 // Ensure that any instruction in scope that gets the "A < B" value number
2136 // is replaced with false.
2137 // The leader table only tracks basic blocks, not edges. Only add to if we
2138 // have the simple case where the edge dominates the end.
2139 if (RootDominatesEnd)
2140 addToLeaderTable(Num, NotVal, Root.getEnd());
2141
2142 continue;
2143 }
2144 }
2145
2146 return Changed;
2147 }
2148
2149 /// When calculating availability, handle an instruction
2150 /// by inserting it into the appropriate sets
processInstruction(Instruction * I)2151 bool GVN::processInstruction(Instruction *I) {
2152 // Ignore dbg info intrinsics.
2153 if (isa<DbgInfoIntrinsic>(I))
2154 return false;
2155
2156 // If the instruction can be easily simplified then do so now in preference
2157 // to value numbering it. Value numbering often exposes redundancies, for
2158 // example if it determines that %y is equal to %x then the instruction
2159 // "%z = and i32 %x, %y" becomes "%z = and i32 %x, %x" which we now simplify.
2160 const DataLayout &DL = I->getModule()->getDataLayout();
2161 if (Value *V = SimplifyInstruction(I, {DL, TLI, DT, AC})) {
2162 bool Changed = false;
2163 if (!I->use_empty()) {
2164 I->replaceAllUsesWith(V);
2165 Changed = true;
2166 }
2167 if (isInstructionTriviallyDead(I, TLI)) {
2168 markInstructionForDeletion(I);
2169 Changed = true;
2170 }
2171 if (Changed) {
2172 if (MD && V->getType()->isPtrOrPtrVectorTy())
2173 MD->invalidateCachedPointerInfo(V);
2174 ++NumGVNSimpl;
2175 return true;
2176 }
2177 }
2178
2179 if (IntrinsicInst *IntrinsicI = dyn_cast<IntrinsicInst>(I))
2180 if (IntrinsicI->getIntrinsicID() == Intrinsic::assume)
2181 return processAssumeIntrinsic(IntrinsicI);
2182
2183 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
2184 if (processLoad(LI))
2185 return true;
2186
2187 unsigned Num = VN.lookupOrAdd(LI);
2188 addToLeaderTable(Num, LI, LI->getParent());
2189 return false;
2190 }
2191
2192 // For conditional branches, we can perform simple conditional propagation on
2193 // the condition value itself.
2194 if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
2195 if (!BI->isConditional())
2196 return false;
2197
2198 if (isa<Constant>(BI->getCondition()))
2199 return processFoldableCondBr(BI);
2200
2201 Value *BranchCond = BI->getCondition();
2202 BasicBlock *TrueSucc = BI->getSuccessor(0);
2203 BasicBlock *FalseSucc = BI->getSuccessor(1);
2204 // Avoid multiple edges early.
2205 if (TrueSucc == FalseSucc)
2206 return false;
2207
2208 BasicBlock *Parent = BI->getParent();
2209 bool Changed = false;
2210
2211 Value *TrueVal = ConstantInt::getTrue(TrueSucc->getContext());
2212 BasicBlockEdge TrueE(Parent, TrueSucc);
2213 Changed |= propagateEquality(BranchCond, TrueVal, TrueE, true);
2214
2215 Value *FalseVal = ConstantInt::getFalse(FalseSucc->getContext());
2216 BasicBlockEdge FalseE(Parent, FalseSucc);
2217 Changed |= propagateEquality(BranchCond, FalseVal, FalseE, true);
2218
2219 return Changed;
2220 }
2221
2222 // For switches, propagate the case values into the case destinations.
2223 if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
2224 Value *SwitchCond = SI->getCondition();
2225 BasicBlock *Parent = SI->getParent();
2226 bool Changed = false;
2227
2228 // Remember how many outgoing edges there are to every successor.
2229 SmallDenseMap<BasicBlock *, unsigned, 16> SwitchEdges;
2230 for (unsigned i = 0, n = SI->getNumSuccessors(); i != n; ++i)
2231 ++SwitchEdges[SI->getSuccessor(i)];
2232
2233 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
2234 i != e; ++i) {
2235 BasicBlock *Dst = i->getCaseSuccessor();
2236 // If there is only a single edge, propagate the case value into it.
2237 if (SwitchEdges.lookup(Dst) == 1) {
2238 BasicBlockEdge E(Parent, Dst);
2239 Changed |= propagateEquality(SwitchCond, i->getCaseValue(), E, true);
2240 }
2241 }
2242 return Changed;
2243 }
2244
2245 // Instructions with void type don't return a value, so there's
2246 // no point in trying to find redundancies in them.
2247 if (I->getType()->isVoidTy())
2248 return false;
2249
2250 uint32_t NextNum = VN.getNextUnusedValueNumber();
2251 unsigned Num = VN.lookupOrAdd(I);
2252
2253 // Allocations are always uniquely numbered, so we can save time and memory
2254 // by fast failing them.
2255 if (isa<AllocaInst>(I) || I->isTerminator() || isa<PHINode>(I)) {
2256 addToLeaderTable(Num, I, I->getParent());
2257 return false;
2258 }
2259
2260 // If the number we were assigned was a brand new VN, then we don't
2261 // need to do a lookup to see if the number already exists
2262 // somewhere in the domtree: it can't!
2263 if (Num >= NextNum) {
2264 addToLeaderTable(Num, I, I->getParent());
2265 return false;
2266 }
2267
2268 // Perform fast-path value-number based elimination of values inherited from
2269 // dominators.
2270 Value *Repl = findLeader(I->getParent(), Num);
2271 if (!Repl) {
2272 // Failure, just remember this instance for future use.
2273 addToLeaderTable(Num, I, I->getParent());
2274 return false;
2275 } else if (Repl == I) {
2276 // If I was the result of a shortcut PRE, it might already be in the table
2277 // and the best replacement for itself. Nothing to do.
2278 return false;
2279 }
2280
2281 // Remove it!
2282 patchAndReplaceAllUsesWith(I, Repl);
2283 if (MD && Repl->getType()->isPtrOrPtrVectorTy())
2284 MD->invalidateCachedPointerInfo(Repl);
2285 markInstructionForDeletion(I);
2286 return true;
2287 }
2288
2289 /// runOnFunction - This is the main transformation entry point for a function.
runImpl(Function & F,AssumptionCache & RunAC,DominatorTree & RunDT,const TargetLibraryInfo & RunTLI,AAResults & RunAA,MemoryDependenceResults * RunMD,LoopInfo * LI,OptimizationRemarkEmitter * RunORE,MemorySSA * MSSA)2290 bool GVN::runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
2291 const TargetLibraryInfo &RunTLI, AAResults &RunAA,
2292 MemoryDependenceResults *RunMD, LoopInfo *LI,
2293 OptimizationRemarkEmitter *RunORE, MemorySSA *MSSA) {
2294 AC = &RunAC;
2295 DT = &RunDT;
2296 VN.setDomTree(DT);
2297 TLI = &RunTLI;
2298 VN.setAliasAnalysis(&RunAA);
2299 MD = RunMD;
2300 ImplicitControlFlowTracking ImplicitCFT;
2301 ICF = &ImplicitCFT;
2302 this->LI = LI;
2303 VN.setMemDep(MD);
2304 ORE = RunORE;
2305 InvalidBlockRPONumbers = true;
2306 MemorySSAUpdater Updater(MSSA);
2307 MSSAU = MSSA ? &Updater : nullptr;
2308
2309 bool Changed = false;
2310 bool ShouldContinue = true;
2311
2312 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
2313 // Merge unconditional branches, allowing PRE to catch more
2314 // optimization opportunities.
2315 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ) {
2316 BasicBlock *BB = &*FI++;
2317
2318 bool removedBlock = MergeBlockIntoPredecessor(BB, &DTU, LI, MSSAU, MD);
2319 if (removedBlock)
2320 ++NumGVNBlocks;
2321
2322 Changed |= removedBlock;
2323 }
2324
2325 unsigned Iteration = 0;
2326 while (ShouldContinue) {
2327 LLVM_DEBUG(dbgs() << "GVN iteration: " << Iteration << "\n");
2328 ShouldContinue = iterateOnFunction(F);
2329 Changed |= ShouldContinue;
2330 ++Iteration;
2331 }
2332
2333 if (isPREEnabled()) {
2334 // Fabricate val-num for dead-code in order to suppress assertion in
2335 // performPRE().
2336 assignValNumForDeadCode();
2337 bool PREChanged = true;
2338 while (PREChanged) {
2339 PREChanged = performPRE(F);
2340 Changed |= PREChanged;
2341 }
2342 }
2343
2344 // FIXME: Should perform GVN again after PRE does something. PRE can move
2345 // computations into blocks where they become fully redundant. Note that
2346 // we can't do this until PRE's critical edge splitting updates memdep.
2347 // Actually, when this happens, we should just fully integrate PRE into GVN.
2348
2349 cleanupGlobalSets();
2350 // Do not cleanup DeadBlocks in cleanupGlobalSets() as it's called for each
2351 // iteration.
2352 DeadBlocks.clear();
2353
2354 if (MSSA && VerifyMemorySSA)
2355 MSSA->verifyMemorySSA();
2356
2357 return Changed;
2358 }
2359
processBlock(BasicBlock * BB)2360 bool GVN::processBlock(BasicBlock *BB) {
2361 // FIXME: Kill off InstrsToErase by doing erasing eagerly in a helper function
2362 // (and incrementing BI before processing an instruction).
2363 assert(InstrsToErase.empty() &&
2364 "We expect InstrsToErase to be empty across iterations");
2365 if (DeadBlocks.count(BB))
2366 return false;
2367
2368 // Clearing map before every BB because it can be used only for single BB.
2369 ReplaceOperandsWithMap.clear();
2370 bool ChangedFunction = false;
2371
2372 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
2373 BI != BE;) {
2374 if (!ReplaceOperandsWithMap.empty())
2375 ChangedFunction |= replaceOperandsForInBlockEquality(&*BI);
2376 ChangedFunction |= processInstruction(&*BI);
2377
2378 if (InstrsToErase.empty()) {
2379 ++BI;
2380 continue;
2381 }
2382
2383 // If we need some instructions deleted, do it now.
2384 NumGVNInstr += InstrsToErase.size();
2385
2386 // Avoid iterator invalidation.
2387 bool AtStart = BI == BB->begin();
2388 if (!AtStart)
2389 --BI;
2390
2391 for (auto *I : InstrsToErase) {
2392 assert(I->getParent() == BB && "Removing instruction from wrong block?");
2393 LLVM_DEBUG(dbgs() << "GVN removed: " << *I << '\n');
2394 salvageKnowledge(I, AC);
2395 salvageDebugInfo(*I);
2396 if (MD) MD->removeInstruction(I);
2397 if (MSSAU)
2398 MSSAU->removeMemoryAccess(I);
2399 LLVM_DEBUG(verifyRemoved(I));
2400 ICF->removeInstruction(I);
2401 I->eraseFromParent();
2402 }
2403 InstrsToErase.clear();
2404
2405 if (AtStart)
2406 BI = BB->begin();
2407 else
2408 ++BI;
2409 }
2410
2411 return ChangedFunction;
2412 }
2413
2414 // Instantiate an expression in a predecessor that lacked it.
performScalarPREInsertion(Instruction * Instr,BasicBlock * Pred,BasicBlock * Curr,unsigned int ValNo)2415 bool GVN::performScalarPREInsertion(Instruction *Instr, BasicBlock *Pred,
2416 BasicBlock *Curr, unsigned int ValNo) {
2417 // Because we are going top-down through the block, all value numbers
2418 // will be available in the predecessor by the time we need them. Any
2419 // that weren't originally present will have been instantiated earlier
2420 // in this loop.
2421 bool success = true;
2422 for (unsigned i = 0, e = Instr->getNumOperands(); i != e; ++i) {
2423 Value *Op = Instr->getOperand(i);
2424 if (isa<Argument>(Op) || isa<Constant>(Op) || isa<GlobalValue>(Op))
2425 continue;
2426 // This could be a newly inserted instruction, in which case, we won't
2427 // find a value number, and should give up before we hurt ourselves.
2428 // FIXME: Rewrite the infrastructure to let it easier to value number
2429 // and process newly inserted instructions.
2430 if (!VN.exists(Op)) {
2431 success = false;
2432 break;
2433 }
2434 uint32_t TValNo =
2435 VN.phiTranslate(Pred, Curr, VN.lookup(Op), *this);
2436 if (Value *V = findLeader(Pred, TValNo)) {
2437 Instr->setOperand(i, V);
2438 } else {
2439 success = false;
2440 break;
2441 }
2442 }
2443
2444 // Fail out if we encounter an operand that is not available in
2445 // the PRE predecessor. This is typically because of loads which
2446 // are not value numbered precisely.
2447 if (!success)
2448 return false;
2449
2450 Instr->insertBefore(Pred->getTerminator());
2451 Instr->setName(Instr->getName() + ".pre");
2452 Instr->setDebugLoc(Instr->getDebugLoc());
2453
2454 unsigned Num = VN.lookupOrAdd(Instr);
2455 VN.add(Instr, Num);
2456
2457 // Update the availability map to include the new instruction.
2458 addToLeaderTable(Num, Instr, Pred);
2459 return true;
2460 }
2461
performScalarPRE(Instruction * CurInst)2462 bool GVN::performScalarPRE(Instruction *CurInst) {
2463 if (isa<AllocaInst>(CurInst) || CurInst->isTerminator() ||
2464 isa<PHINode>(CurInst) || CurInst->getType()->isVoidTy() ||
2465 CurInst->mayReadFromMemory() || CurInst->mayHaveSideEffects() ||
2466 isa<DbgInfoIntrinsic>(CurInst))
2467 return false;
2468
2469 // Don't do PRE on compares. The PHI would prevent CodeGenPrepare from
2470 // sinking the compare again, and it would force the code generator to
2471 // move the i1 from processor flags or predicate registers into a general
2472 // purpose register.
2473 if (isa<CmpInst>(CurInst))
2474 return false;
2475
2476 // Don't do PRE on GEPs. The inserted PHI would prevent CodeGenPrepare from
2477 // sinking the addressing mode computation back to its uses. Extending the
2478 // GEP's live range increases the register pressure, and therefore it can
2479 // introduce unnecessary spills.
2480 //
2481 // This doesn't prevent Load PRE. PHI translation will make the GEP available
2482 // to the load by moving it to the predecessor block if necessary.
2483 if (isa<GetElementPtrInst>(CurInst))
2484 return false;
2485
2486 if (auto *CallB = dyn_cast<CallBase>(CurInst)) {
2487 // We don't currently value number ANY inline asm calls.
2488 if (CallB->isInlineAsm())
2489 return false;
2490 // Don't do PRE on convergent calls.
2491 if (CallB->isConvergent())
2492 return false;
2493 }
2494
2495 uint32_t ValNo = VN.lookup(CurInst);
2496
2497 // Look for the predecessors for PRE opportunities. We're
2498 // only trying to solve the basic diamond case, where
2499 // a value is computed in the successor and one predecessor,
2500 // but not the other. We also explicitly disallow cases
2501 // where the successor is its own predecessor, because they're
2502 // more complicated to get right.
2503 unsigned NumWith = 0;
2504 unsigned NumWithout = 0;
2505 BasicBlock *PREPred = nullptr;
2506 BasicBlock *CurrentBlock = CurInst->getParent();
2507
2508 // Update the RPO numbers for this function.
2509 if (InvalidBlockRPONumbers)
2510 assignBlockRPONumber(*CurrentBlock->getParent());
2511
2512 SmallVector<std::pair<Value *, BasicBlock *>, 8> predMap;
2513 for (BasicBlock *P : predecessors(CurrentBlock)) {
2514 // We're not interested in PRE where blocks with predecessors that are
2515 // not reachable.
2516 if (!DT->isReachableFromEntry(P)) {
2517 NumWithout = 2;
2518 break;
2519 }
2520 // It is not safe to do PRE when P->CurrentBlock is a loop backedge, and
2521 // when CurInst has operand defined in CurrentBlock (so it may be defined
2522 // by phi in the loop header).
2523 assert(BlockRPONumber.count(P) && BlockRPONumber.count(CurrentBlock) &&
2524 "Invalid BlockRPONumber map.");
2525 if (BlockRPONumber[P] >= BlockRPONumber[CurrentBlock] &&
2526 llvm::any_of(CurInst->operands(), [&](const Use &U) {
2527 if (auto *Inst = dyn_cast<Instruction>(U.get()))
2528 return Inst->getParent() == CurrentBlock;
2529 return false;
2530 })) {
2531 NumWithout = 2;
2532 break;
2533 }
2534
2535 uint32_t TValNo = VN.phiTranslate(P, CurrentBlock, ValNo, *this);
2536 Value *predV = findLeader(P, TValNo);
2537 if (!predV) {
2538 predMap.push_back(std::make_pair(static_cast<Value *>(nullptr), P));
2539 PREPred = P;
2540 ++NumWithout;
2541 } else if (predV == CurInst) {
2542 /* CurInst dominates this predecessor. */
2543 NumWithout = 2;
2544 break;
2545 } else {
2546 predMap.push_back(std::make_pair(predV, P));
2547 ++NumWith;
2548 }
2549 }
2550
2551 // Don't do PRE when it might increase code size, i.e. when
2552 // we would need to insert instructions in more than one pred.
2553 if (NumWithout > 1 || NumWith == 0)
2554 return false;
2555
2556 // We may have a case where all predecessors have the instruction,
2557 // and we just need to insert a phi node. Otherwise, perform
2558 // insertion.
2559 Instruction *PREInstr = nullptr;
2560
2561 if (NumWithout != 0) {
2562 if (!isSafeToSpeculativelyExecute(CurInst)) {
2563 // It is only valid to insert a new instruction if the current instruction
2564 // is always executed. An instruction with implicit control flow could
2565 // prevent us from doing it. If we cannot speculate the execution, then
2566 // PRE should be prohibited.
2567 if (ICF->isDominatedByICFIFromSameBlock(CurInst))
2568 return false;
2569 }
2570
2571 // Don't do PRE across indirect branch.
2572 if (isa<IndirectBrInst>(PREPred->getTerminator()))
2573 return false;
2574
2575 // Don't do PRE across callbr.
2576 // FIXME: Can we do this across the fallthrough edge?
2577 if (isa<CallBrInst>(PREPred->getTerminator()))
2578 return false;
2579
2580 // We can't do PRE safely on a critical edge, so instead we schedule
2581 // the edge to be split and perform the PRE the next time we iterate
2582 // on the function.
2583 unsigned SuccNum = GetSuccessorNumber(PREPred, CurrentBlock);
2584 if (isCriticalEdge(PREPred->getTerminator(), SuccNum)) {
2585 toSplit.push_back(std::make_pair(PREPred->getTerminator(), SuccNum));
2586 return false;
2587 }
2588 // We need to insert somewhere, so let's give it a shot
2589 PREInstr = CurInst->clone();
2590 if (!performScalarPREInsertion(PREInstr, PREPred, CurrentBlock, ValNo)) {
2591 // If we failed insertion, make sure we remove the instruction.
2592 LLVM_DEBUG(verifyRemoved(PREInstr));
2593 PREInstr->deleteValue();
2594 return false;
2595 }
2596 }
2597
2598 // Either we should have filled in the PRE instruction, or we should
2599 // not have needed insertions.
2600 assert(PREInstr != nullptr || NumWithout == 0);
2601
2602 ++NumGVNPRE;
2603
2604 // Create a PHI to make the value available in this block.
2605 PHINode *Phi =
2606 PHINode::Create(CurInst->getType(), predMap.size(),
2607 CurInst->getName() + ".pre-phi", &CurrentBlock->front());
2608 for (unsigned i = 0, e = predMap.size(); i != e; ++i) {
2609 if (Value *V = predMap[i].first) {
2610 // If we use an existing value in this phi, we have to patch the original
2611 // value because the phi will be used to replace a later value.
2612 patchReplacementInstruction(CurInst, V);
2613 Phi->addIncoming(V, predMap[i].second);
2614 } else
2615 Phi->addIncoming(PREInstr, PREPred);
2616 }
2617
2618 VN.add(Phi, ValNo);
2619 // After creating a new PHI for ValNo, the phi translate result for ValNo will
2620 // be changed, so erase the related stale entries in phi translate cache.
2621 VN.eraseTranslateCacheEntry(ValNo, *CurrentBlock);
2622 addToLeaderTable(ValNo, Phi, CurrentBlock);
2623 Phi->setDebugLoc(CurInst->getDebugLoc());
2624 CurInst->replaceAllUsesWith(Phi);
2625 if (MD && Phi->getType()->isPtrOrPtrVectorTy())
2626 MD->invalidateCachedPointerInfo(Phi);
2627 VN.erase(CurInst);
2628 removeFromLeaderTable(ValNo, CurInst, CurrentBlock);
2629
2630 LLVM_DEBUG(dbgs() << "GVN PRE removed: " << *CurInst << '\n');
2631 if (MD)
2632 MD->removeInstruction(CurInst);
2633 if (MSSAU)
2634 MSSAU->removeMemoryAccess(CurInst);
2635 LLVM_DEBUG(verifyRemoved(CurInst));
2636 // FIXME: Intended to be markInstructionForDeletion(CurInst), but it causes
2637 // some assertion failures.
2638 ICF->removeInstruction(CurInst);
2639 CurInst->eraseFromParent();
2640 ++NumGVNInstr;
2641
2642 return true;
2643 }
2644
2645 /// Perform a purely local form of PRE that looks for diamond
2646 /// control flow patterns and attempts to perform simple PRE at the join point.
performPRE(Function & F)2647 bool GVN::performPRE(Function &F) {
2648 bool Changed = false;
2649 for (BasicBlock *CurrentBlock : depth_first(&F.getEntryBlock())) {
2650 // Nothing to PRE in the entry block.
2651 if (CurrentBlock == &F.getEntryBlock())
2652 continue;
2653
2654 // Don't perform PRE on an EH pad.
2655 if (CurrentBlock->isEHPad())
2656 continue;
2657
2658 for (BasicBlock::iterator BI = CurrentBlock->begin(),
2659 BE = CurrentBlock->end();
2660 BI != BE;) {
2661 Instruction *CurInst = &*BI++;
2662 Changed |= performScalarPRE(CurInst);
2663 }
2664 }
2665
2666 if (splitCriticalEdges())
2667 Changed = true;
2668
2669 return Changed;
2670 }
2671
2672 /// Split the critical edge connecting the given two blocks, and return
2673 /// the block inserted to the critical edge.
splitCriticalEdges(BasicBlock * Pred,BasicBlock * Succ)2674 BasicBlock *GVN::splitCriticalEdges(BasicBlock *Pred, BasicBlock *Succ) {
2675 // GVN does not require loop-simplify, do not try to preserve it if it is not
2676 // possible.
2677 BasicBlock *BB = SplitCriticalEdge(
2678 Pred, Succ,
2679 CriticalEdgeSplittingOptions(DT, LI, MSSAU).unsetPreserveLoopSimplify());
2680 if (MD)
2681 MD->invalidateCachedPredecessors();
2682 InvalidBlockRPONumbers = true;
2683 return BB;
2684 }
2685
2686 /// Split critical edges found during the previous
2687 /// iteration that may enable further optimization.
splitCriticalEdges()2688 bool GVN::splitCriticalEdges() {
2689 if (toSplit.empty())
2690 return false;
2691 do {
2692 std::pair<Instruction *, unsigned> Edge = toSplit.pop_back_val();
2693 SplitCriticalEdge(Edge.first, Edge.second,
2694 CriticalEdgeSplittingOptions(DT, LI, MSSAU));
2695 } while (!toSplit.empty());
2696 if (MD) MD->invalidateCachedPredecessors();
2697 InvalidBlockRPONumbers = true;
2698 return true;
2699 }
2700
2701 /// Executes one iteration of GVN
iterateOnFunction(Function & F)2702 bool GVN::iterateOnFunction(Function &F) {
2703 cleanupGlobalSets();
2704
2705 // Top-down walk of the dominator tree
2706 bool Changed = false;
2707 // Needed for value numbering with phi construction to work.
2708 // RPOT walks the graph in its constructor and will not be invalidated during
2709 // processBlock.
2710 ReversePostOrderTraversal<Function *> RPOT(&F);
2711
2712 for (BasicBlock *BB : RPOT)
2713 Changed |= processBlock(BB);
2714
2715 return Changed;
2716 }
2717
cleanupGlobalSets()2718 void GVN::cleanupGlobalSets() {
2719 VN.clear();
2720 LeaderTable.clear();
2721 BlockRPONumber.clear();
2722 TableAllocator.Reset();
2723 ICF->clear();
2724 InvalidBlockRPONumbers = true;
2725 }
2726
2727 /// Verify that the specified instruction does not occur in our
2728 /// internal data structures.
verifyRemoved(const Instruction * Inst) const2729 void GVN::verifyRemoved(const Instruction *Inst) const {
2730 VN.verifyRemoved(Inst);
2731
2732 // Walk through the value number scope to make sure the instruction isn't
2733 // ferreted away in it.
2734 for (DenseMap<uint32_t, LeaderTableEntry>::const_iterator
2735 I = LeaderTable.begin(), E = LeaderTable.end(); I != E; ++I) {
2736 const LeaderTableEntry *Node = &I->second;
2737 assert(Node->Val != Inst && "Inst still in value numbering scope!");
2738
2739 while (Node->Next) {
2740 Node = Node->Next;
2741 assert(Node->Val != Inst && "Inst still in value numbering scope!");
2742 }
2743 }
2744 }
2745
2746 /// BB is declared dead, which implied other blocks become dead as well. This
2747 /// function is to add all these blocks to "DeadBlocks". For the dead blocks'
2748 /// live successors, update their phi nodes by replacing the operands
2749 /// corresponding to dead blocks with UndefVal.
addDeadBlock(BasicBlock * BB)2750 void GVN::addDeadBlock(BasicBlock *BB) {
2751 SmallVector<BasicBlock *, 4> NewDead;
2752 SmallSetVector<BasicBlock *, 4> DF;
2753
2754 NewDead.push_back(BB);
2755 while (!NewDead.empty()) {
2756 BasicBlock *D = NewDead.pop_back_val();
2757 if (DeadBlocks.count(D))
2758 continue;
2759
2760 // All blocks dominated by D are dead.
2761 SmallVector<BasicBlock *, 8> Dom;
2762 DT->getDescendants(D, Dom);
2763 DeadBlocks.insert(Dom.begin(), Dom.end());
2764
2765 // Figure out the dominance-frontier(D).
2766 for (BasicBlock *B : Dom) {
2767 for (BasicBlock *S : successors(B)) {
2768 if (DeadBlocks.count(S))
2769 continue;
2770
2771 bool AllPredDead = true;
2772 for (BasicBlock *P : predecessors(S))
2773 if (!DeadBlocks.count(P)) {
2774 AllPredDead = false;
2775 break;
2776 }
2777
2778 if (!AllPredDead) {
2779 // S could be proved dead later on. That is why we don't update phi
2780 // operands at this moment.
2781 DF.insert(S);
2782 } else {
2783 // While S is not dominated by D, it is dead by now. This could take
2784 // place if S already have a dead predecessor before D is declared
2785 // dead.
2786 NewDead.push_back(S);
2787 }
2788 }
2789 }
2790 }
2791
2792 // For the dead blocks' live successors, update their phi nodes by replacing
2793 // the operands corresponding to dead blocks with UndefVal.
2794 for(SmallSetVector<BasicBlock *, 4>::iterator I = DF.begin(), E = DF.end();
2795 I != E; I++) {
2796 BasicBlock *B = *I;
2797 if (DeadBlocks.count(B))
2798 continue;
2799
2800 // First, split the critical edges. This might also create additional blocks
2801 // to preserve LoopSimplify form and adjust edges accordingly.
2802 SmallVector<BasicBlock *, 4> Preds(pred_begin(B), pred_end(B));
2803 for (BasicBlock *P : Preds) {
2804 if (!DeadBlocks.count(P))
2805 continue;
2806
2807 if (llvm::is_contained(successors(P), B) &&
2808 isCriticalEdge(P->getTerminator(), B)) {
2809 if (BasicBlock *S = splitCriticalEdges(P, B))
2810 DeadBlocks.insert(P = S);
2811 }
2812 }
2813
2814 // Now undef the incoming values from the dead predecessors.
2815 for (BasicBlock *P : predecessors(B)) {
2816 if (!DeadBlocks.count(P))
2817 continue;
2818 for (PHINode &Phi : B->phis()) {
2819 Phi.setIncomingValueForBlock(P, UndefValue::get(Phi.getType()));
2820 if (MD)
2821 MD->invalidateCachedPointerInfo(&Phi);
2822 }
2823 }
2824 }
2825 }
2826
2827 // If the given branch is recognized as a foldable branch (i.e. conditional
2828 // branch with constant condition), it will perform following analyses and
2829 // transformation.
2830 // 1) If the dead out-coming edge is a critical-edge, split it. Let
2831 // R be the target of the dead out-coming edge.
2832 // 1) Identify the set of dead blocks implied by the branch's dead outcoming
2833 // edge. The result of this step will be {X| X is dominated by R}
2834 // 2) Identify those blocks which haves at least one dead predecessor. The
2835 // result of this step will be dominance-frontier(R).
2836 // 3) Update the PHIs in DF(R) by replacing the operands corresponding to
2837 // dead blocks with "UndefVal" in an hope these PHIs will optimized away.
2838 //
2839 // Return true iff *NEW* dead code are found.
processFoldableCondBr(BranchInst * BI)2840 bool GVN::processFoldableCondBr(BranchInst *BI) {
2841 if (!BI || BI->isUnconditional())
2842 return false;
2843
2844 // If a branch has two identical successors, we cannot declare either dead.
2845 if (BI->getSuccessor(0) == BI->getSuccessor(1))
2846 return false;
2847
2848 ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
2849 if (!Cond)
2850 return false;
2851
2852 BasicBlock *DeadRoot =
2853 Cond->getZExtValue() ? BI->getSuccessor(1) : BI->getSuccessor(0);
2854 if (DeadBlocks.count(DeadRoot))
2855 return false;
2856
2857 if (!DeadRoot->getSinglePredecessor())
2858 DeadRoot = splitCriticalEdges(BI->getParent(), DeadRoot);
2859
2860 addDeadBlock(DeadRoot);
2861 return true;
2862 }
2863
2864 // performPRE() will trigger assert if it comes across an instruction without
2865 // associated val-num. As it normally has far more live instructions than dead
2866 // instructions, it makes more sense just to "fabricate" a val-number for the
2867 // dead code than checking if instruction involved is dead or not.
assignValNumForDeadCode()2868 void GVN::assignValNumForDeadCode() {
2869 for (BasicBlock *BB : DeadBlocks) {
2870 for (Instruction &Inst : *BB) {
2871 unsigned ValNum = VN.lookupOrAdd(&Inst);
2872 addToLeaderTable(ValNum, &Inst, BB);
2873 }
2874 }
2875 }
2876
2877 class llvm::gvn::GVNLegacyPass : public FunctionPass {
2878 public:
2879 static char ID; // Pass identification, replacement for typeid
2880
GVNLegacyPass(bool NoMemDepAnalysis=!GVNEnableMemDep)2881 explicit GVNLegacyPass(bool NoMemDepAnalysis = !GVNEnableMemDep)
2882 : FunctionPass(ID), Impl(GVNOptions().setMemDep(!NoMemDepAnalysis)) {
2883 initializeGVNLegacyPassPass(*PassRegistry::getPassRegistry());
2884 }
2885
runOnFunction(Function & F)2886 bool runOnFunction(Function &F) override {
2887 if (skipFunction(F))
2888 return false;
2889
2890 auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
2891
2892 auto *MSSAWP = getAnalysisIfAvailable<MemorySSAWrapperPass>();
2893 return Impl.runImpl(
2894 F, getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F),
2895 getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
2896 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F),
2897 getAnalysis<AAResultsWrapperPass>().getAAResults(),
2898 Impl.isMemDepEnabled()
2899 ? &getAnalysis<MemoryDependenceWrapperPass>().getMemDep()
2900 : nullptr,
2901 LIWP ? &LIWP->getLoopInfo() : nullptr,
2902 &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(),
2903 MSSAWP ? &MSSAWP->getMSSA() : nullptr);
2904 }
2905
getAnalysisUsage(AnalysisUsage & AU) const2906 void getAnalysisUsage(AnalysisUsage &AU) const override {
2907 AU.addRequired<AssumptionCacheTracker>();
2908 AU.addRequired<DominatorTreeWrapperPass>();
2909 AU.addRequired<TargetLibraryInfoWrapperPass>();
2910 AU.addRequired<LoopInfoWrapperPass>();
2911 if (Impl.isMemDepEnabled())
2912 AU.addRequired<MemoryDependenceWrapperPass>();
2913 AU.addRequired<AAResultsWrapperPass>();
2914 AU.addPreserved<DominatorTreeWrapperPass>();
2915 AU.addPreserved<GlobalsAAWrapperPass>();
2916 AU.addPreserved<TargetLibraryInfoWrapperPass>();
2917 AU.addPreserved<LoopInfoWrapperPass>();
2918 AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
2919 AU.addPreserved<MemorySSAWrapperPass>();
2920 }
2921
2922 private:
2923 GVN Impl;
2924 };
2925
2926 char GVNLegacyPass::ID = 0;
2927
2928 INITIALIZE_PASS_BEGIN(GVNLegacyPass, "gvn", "Global Value Numbering", false, false)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)2929 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
2930 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
2931 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
2932 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
2933 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
2934 INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
2935 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
2936 INITIALIZE_PASS_END(GVNLegacyPass, "gvn", "Global Value Numbering", false, false)
2937
2938 // The public interface to this file...
2939 FunctionPass *llvm::createGVNPass(bool NoMemDepAnalysis) {
2940 return new GVNLegacyPass(NoMemDepAnalysis);
2941 }
2942