blob: 91ccf2ee051469e6152597a46697b19cb0414cfe [file] [log] [blame]
Davide Italiano7e274e02016-12-22 16:03:48 +00001//===---- NewGVN.cpp - Global Value Numbering Pass --------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// This file implements the new LLVM's Global Value Numbering pass.
11/// GVN partitions values computed by a function into congruence classes.
12/// Values ending up in the same congruence class are guaranteed to be the same
13/// for every execution of the program. In that respect, congruency is a
14/// compile-time approximation of equivalence of values at runtime.
15/// The algorithm implemented here uses a sparse formulation and it's based
16/// on the ideas described in the paper:
17/// "A Sparse Algorithm for Predicated Global Value Numbering" from
18/// Karthik Gargi.
19///
20//===----------------------------------------------------------------------===//
21
22#include "llvm/Transforms/Scalar/NewGVN.h"
23#include "llvm/ADT/BitVector.h"
24#include "llvm/ADT/DenseMap.h"
25#include "llvm/ADT/DenseSet.h"
26#include "llvm/ADT/DepthFirstIterator.h"
27#include "llvm/ADT/Hashing.h"
28#include "llvm/ADT/MapVector.h"
29#include "llvm/ADT/PostOrderIterator.h"
Daniel Berlind7c12ee2016-12-25 22:23:49 +000030#include "llvm/ADT/STLExtras.h"
Davide Italiano7e274e02016-12-22 16:03:48 +000031#include "llvm/ADT/SmallPtrSet.h"
32#include "llvm/ADT/SmallSet.h"
33#include "llvm/ADT/SparseBitVector.h"
34#include "llvm/ADT/Statistic.h"
35#include "llvm/ADT/TinyPtrVector.h"
36#include "llvm/Analysis/AliasAnalysis.h"
37#include "llvm/Analysis/AssumptionCache.h"
38#include "llvm/Analysis/CFG.h"
39#include "llvm/Analysis/CFGPrinter.h"
40#include "llvm/Analysis/ConstantFolding.h"
41#include "llvm/Analysis/GlobalsModRef.h"
42#include "llvm/Analysis/InstructionSimplify.h"
43#include "llvm/Analysis/Loads.h"
44#include "llvm/Analysis/MemoryBuiltins.h"
45#include "llvm/Analysis/MemoryDependenceAnalysis.h"
46#include "llvm/Analysis/MemoryLocation.h"
47#include "llvm/Analysis/PHITransAddr.h"
48#include "llvm/Analysis/TargetLibraryInfo.h"
49#include "llvm/Analysis/ValueTracking.h"
50#include "llvm/IR/DataLayout.h"
51#include "llvm/IR/Dominators.h"
52#include "llvm/IR/GlobalVariable.h"
53#include "llvm/IR/IRBuilder.h"
54#include "llvm/IR/IntrinsicInst.h"
55#include "llvm/IR/LLVMContext.h"
56#include "llvm/IR/Metadata.h"
57#include "llvm/IR/PatternMatch.h"
58#include "llvm/IR/PredIteratorCache.h"
59#include "llvm/IR/Type.h"
60#include "llvm/Support/Allocator.h"
61#include "llvm/Support/CommandLine.h"
62#include "llvm/Support/Debug.h"
63#include "llvm/Transforms/Scalar.h"
64#include "llvm/Transforms/Scalar/GVNExpression.h"
65#include "llvm/Transforms/Utils/BasicBlockUtils.h"
66#include "llvm/Transforms/Utils/Local.h"
67#include "llvm/Transforms/Utils/MemorySSA.h"
68#include "llvm/Transforms/Utils/SSAUpdater.h"
69#include <unordered_map>
70#include <utility>
71#include <vector>
72using namespace llvm;
73using namespace PatternMatch;
74using namespace llvm::GVNExpression;
75
76#define DEBUG_TYPE "newgvn"
77
78STATISTIC(NumGVNInstrDeleted, "Number of instructions deleted");
79STATISTIC(NumGVNBlocksDeleted, "Number of blocks deleted");
80STATISTIC(NumGVNOpsSimplified, "Number of Expressions simplified");
81STATISTIC(NumGVNPhisAllSame, "Number of PHIs whos arguments are all the same");
Daniel Berlin04443432017-01-07 03:23:47 +000082STATISTIC(NumGVNMaxIterations,
83 "Maximum Number of iterations it took to converge GVN");
Daniel Berlinc0431fd2017-01-13 22:40:01 +000084STATISTIC(NumGVNLeaderChanges, "Number of leader changes");
85STATISTIC(NumGVNSortedLeaderChanges, "Number of sorted leader changes");
86STATISTIC(NumGVNAvoidedSortedLeaderChanges,
87 "Number of avoided sorted leader changes");
Daniel Berlin89fea6f2017-01-20 06:38:41 +000088STATISTIC(NumGVNNotMostDominatingLeader,
89 "Number of times a member dominated it's new classes' leader");
Davide Italiano7e274e02016-12-22 16:03:48 +000090
91//===----------------------------------------------------------------------===//
92// GVN Pass
93//===----------------------------------------------------------------------===//
94
95// Anchor methods.
96namespace llvm {
97namespace GVNExpression {
Daniel Berlin85f91b02016-12-26 20:06:58 +000098Expression::~Expression() = default;
99BasicExpression::~BasicExpression() = default;
100CallExpression::~CallExpression() = default;
101LoadExpression::~LoadExpression() = default;
102StoreExpression::~StoreExpression() = default;
103AggregateValueExpression::~AggregateValueExpression() = default;
104PHIExpression::~PHIExpression() = default;
Davide Italiano7e274e02016-12-22 16:03:48 +0000105}
106}
107
108// Congruence classes represent the set of expressions/instructions
109// that are all the same *during some scope in the function*.
110// That is, because of the way we perform equality propagation, and
111// because of memory value numbering, it is not correct to assume
112// you can willy-nilly replace any member with any other at any
113// point in the function.
114//
115// For any Value in the Member set, it is valid to replace any dominated member
116// with that Value.
117//
118// Every congruence class has a leader, and the leader is used to
119// symbolize instructions in a canonical way (IE every operand of an
120// instruction that is a member of the same congruence class will
121// always be replaced with leader during symbolization).
122// To simplify symbolization, we keep the leader as a constant if class can be
123// proved to be a constant value.
124// Otherwise, the leader is a randomly chosen member of the value set, it does
125// not matter which one is chosen.
126// Each congruence class also has a defining expression,
127// though the expression may be null. If it exists, it can be used for forward
128// propagation and reassociation of values.
129//
130struct CongruenceClass {
Piotr Padlewskie4047b82016-12-28 19:29:26 +0000131 using MemberSet = SmallPtrSet<Value *, 4>;
Davide Italiano7e274e02016-12-22 16:03:48 +0000132 unsigned ID;
133 // Representative leader.
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000134 Value *RepLeader = nullptr;
Davide Italiano7e274e02016-12-22 16:03:48 +0000135 // Defining Expression.
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000136 const Expression *DefiningExpr = nullptr;
Davide Italiano7e274e02016-12-22 16:03:48 +0000137 // Actual members of this class.
138 MemberSet Members;
139
140 // True if this class has no members left. This is mainly used for assertion
141 // purposes, and for skipping empty classes.
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000142 bool Dead = false;
Davide Italiano7e274e02016-12-22 16:03:48 +0000143
Daniel Berlinf6eba4b2017-01-11 20:22:36 +0000144 // Number of stores in this congruence class.
145 // This is used so we can detect store equivalence changes properly.
Davide Italianoeac05f62017-01-11 23:41:24 +0000146 int StoreCount = 0;
Daniel Berlinf6eba4b2017-01-11 20:22:36 +0000147
Daniel Berlinc0431fd2017-01-13 22:40:01 +0000148 // The most dominating leader after our current leader, because the member set
149 // is not sorted and is expensive to keep sorted all the time.
150 std::pair<Value *, unsigned int> NextLeader = {nullptr, ~0U};
151
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000152 explicit CongruenceClass(unsigned ID) : ID(ID) {}
Davide Italiano7e274e02016-12-22 16:03:48 +0000153 CongruenceClass(unsigned ID, Value *Leader, const Expression *E)
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000154 : ID(ID), RepLeader(Leader), DefiningExpr(E) {}
Davide Italiano7e274e02016-12-22 16:03:48 +0000155};
156
157namespace llvm {
Daniel Berlin85f91b02016-12-26 20:06:58 +0000158template <> struct DenseMapInfo<const Expression *> {
159 static const Expression *getEmptyKey() {
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000160 auto Val = static_cast<uintptr_t>(-1);
Daniel Berlin85f91b02016-12-26 20:06:58 +0000161 Val <<= PointerLikeTypeTraits<const Expression *>::NumLowBitsAvailable;
162 return reinterpret_cast<const Expression *>(Val);
163 }
164 static const Expression *getTombstoneKey() {
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000165 auto Val = static_cast<uintptr_t>(~1U);
Daniel Berlin85f91b02016-12-26 20:06:58 +0000166 Val <<= PointerLikeTypeTraits<const Expression *>::NumLowBitsAvailable;
167 return reinterpret_cast<const Expression *>(Val);
168 }
169 static unsigned getHashValue(const Expression *V) {
170 return static_cast<unsigned>(V->getHashValue());
171 }
172 static bool isEqual(const Expression *LHS, const Expression *RHS) {
173 if (LHS == RHS)
174 return true;
175 if (LHS == getTombstoneKey() || RHS == getTombstoneKey() ||
176 LHS == getEmptyKey() || RHS == getEmptyKey())
177 return false;
178 return *LHS == *RHS;
179 }
180};
Davide Italiano7e274e02016-12-22 16:03:48 +0000181} // end namespace llvm
182
183class NewGVN : public FunctionPass {
184 DominatorTree *DT;
185 const DataLayout *DL;
186 const TargetLibraryInfo *TLI;
187 AssumptionCache *AC;
188 AliasAnalysis *AA;
189 MemorySSA *MSSA;
190 MemorySSAWalker *MSSAWalker;
191 BumpPtrAllocator ExpressionAllocator;
192 ArrayRecycler<Value *> ArgRecycler;
193
194 // Congruence class info.
195 CongruenceClass *InitialClass;
196 std::vector<CongruenceClass *> CongruenceClasses;
197 unsigned NextCongruenceNum;
198
199 // Value Mappings.
200 DenseMap<Value *, CongruenceClass *> ValueToClass;
201 DenseMap<Value *, const Expression *> ValueToExpression;
202
Daniel Berlind7c12ee2016-12-25 22:23:49 +0000203 // A table storing which memorydefs/phis represent a memory state provably
204 // equivalent to another memory state.
205 // We could use the congruence class machinery, but the MemoryAccess's are
206 // abstract memory states, so they can only ever be equivalent to each other,
207 // and not to constants, etc.
Daniel Berlin7ad1ea02016-12-29 00:49:32 +0000208 DenseMap<const MemoryAccess *, MemoryAccess *> MemoryAccessEquiv;
Daniel Berlind7c12ee2016-12-25 22:23:49 +0000209
Davide Italiano7e274e02016-12-22 16:03:48 +0000210 // Expression to class mapping.
Piotr Padlewskie4047b82016-12-28 19:29:26 +0000211 using ExpressionClassMap = DenseMap<const Expression *, CongruenceClass *>;
Davide Italiano7e274e02016-12-22 16:03:48 +0000212 ExpressionClassMap ExpressionToClass;
213
214 // Which values have changed as a result of leader changes.
Daniel Berlin3a1bd022017-01-11 20:22:05 +0000215 SmallPtrSet<Value *, 8> LeaderChanges;
Davide Italiano7e274e02016-12-22 16:03:48 +0000216
217 // Reachability info.
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000218 using BlockEdge = BasicBlockEdge;
Davide Italiano7e274e02016-12-22 16:03:48 +0000219 DenseSet<BlockEdge> ReachableEdges;
220 SmallPtrSet<const BasicBlock *, 8> ReachableBlocks;
221
222 // This is a bitvector because, on larger functions, we may have
223 // thousands of touched instructions at once (entire blocks,
224 // instructions with hundreds of uses, etc). Even with optimization
225 // for when we mark whole blocks as touched, when this was a
226 // SmallPtrSet or DenseSet, for some functions, we spent >20% of all
227 // the time in GVN just managing this list. The bitvector, on the
228 // other hand, efficiently supports test/set/clear of both
229 // individual and ranges, as well as "find next element" This
230 // enables us to use it as a worklist with essentially 0 cost.
231 BitVector TouchedInstructions;
232
233 DenseMap<const BasicBlock *, std::pair<unsigned, unsigned>> BlockInstRange;
234 DenseMap<const DomTreeNode *, std::pair<unsigned, unsigned>>
235 DominatedInstRange;
236
237#ifndef NDEBUG
238 // Debugging for how many times each block and instruction got processed.
239 DenseMap<const Value *, unsigned> ProcessedCount;
240#endif
241
242 // DFS info.
Davide Italiano7e274e02016-12-22 16:03:48 +0000243 DenseMap<const Value *, unsigned> InstrDFS;
Daniel Berlin1f31fe522016-12-27 09:20:36 +0000244 SmallVector<Value *, 32> DFSToInstr;
Davide Italiano7e274e02016-12-22 16:03:48 +0000245
246 // Deletion info.
247 SmallPtrSet<Instruction *, 8> InstructionsToErase;
248
249public:
250 static char ID; // Pass identification, replacement for typeid.
251 NewGVN() : FunctionPass(ID) {
252 initializeNewGVNPass(*PassRegistry::getPassRegistry());
253 }
254
255 bool runOnFunction(Function &F) override;
256 bool runGVN(Function &F, DominatorTree *DT, AssumptionCache *AC,
Daniel Berlin85f91b02016-12-26 20:06:58 +0000257 TargetLibraryInfo *TLI, AliasAnalysis *AA, MemorySSA *MSSA);
Davide Italiano7e274e02016-12-22 16:03:48 +0000258
259private:
Davide Italiano7e274e02016-12-22 16:03:48 +0000260 void getAnalysisUsage(AnalysisUsage &AU) const override {
261 AU.addRequired<AssumptionCacheTracker>();
262 AU.addRequired<DominatorTreeWrapperPass>();
263 AU.addRequired<TargetLibraryInfoWrapperPass>();
264 AU.addRequired<MemorySSAWrapperPass>();
265 AU.addRequired<AAResultsWrapperPass>();
266
267 AU.addPreserved<DominatorTreeWrapperPass>();
268 AU.addPreserved<GlobalsAAWrapperPass>();
269 }
270
271 // Expression handling.
272 const Expression *createExpression(Instruction *, const BasicBlock *);
273 const Expression *createBinaryExpression(unsigned, Type *, Value *, Value *,
274 const BasicBlock *);
275 PHIExpression *createPHIExpression(Instruction *);
276 const VariableExpression *createVariableExpression(Value *);
277 const ConstantExpression *createConstantExpression(Constant *);
278 const Expression *createVariableOrConstant(Value *V, const BasicBlock *B);
Daniel Berlin02c6b172017-01-02 18:00:53 +0000279 const UnknownExpression *createUnknownExpression(Instruction *);
Davide Italiano7e274e02016-12-22 16:03:48 +0000280 const StoreExpression *createStoreExpression(StoreInst *, MemoryAccess *,
281 const BasicBlock *);
282 LoadExpression *createLoadExpression(Type *, Value *, LoadInst *,
283 MemoryAccess *, const BasicBlock *);
284
285 const CallExpression *createCallExpression(CallInst *, MemoryAccess *,
286 const BasicBlock *);
287 const AggregateValueExpression *
288 createAggregateValueExpression(Instruction *, const BasicBlock *);
289 bool setBasicExpressionInfo(Instruction *, BasicExpression *,
290 const BasicBlock *);
291
292 // Congruence class handling.
293 CongruenceClass *createCongruenceClass(Value *Leader, const Expression *E) {
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000294 auto *result = new CongruenceClass(NextCongruenceNum++, Leader, E);
Piotr Padlewski6c37d292016-12-28 23:24:02 +0000295 CongruenceClasses.emplace_back(result);
Davide Italiano7e274e02016-12-22 16:03:48 +0000296 return result;
297 }
298
299 CongruenceClass *createSingletonCongruenceClass(Value *Member) {
Davide Italiano0e714802016-12-28 14:00:11 +0000300 CongruenceClass *CClass = createCongruenceClass(Member, nullptr);
Davide Italiano7e274e02016-12-22 16:03:48 +0000301 CClass->Members.insert(Member);
302 ValueToClass[Member] = CClass;
303 return CClass;
304 }
305 void initializeCongruenceClasses(Function &F);
306
Daniel Berlind7c12ee2016-12-25 22:23:49 +0000307 // Value number an Instruction or MemoryPhi.
308 void valueNumberMemoryPhi(MemoryPhi *);
309 void valueNumberInstruction(Instruction *);
310
Davide Italiano7e274e02016-12-22 16:03:48 +0000311 // Symbolic evaluation.
312 const Expression *checkSimplificationResults(Expression *, Instruction *,
313 Value *);
314 const Expression *performSymbolicEvaluation(Value *, const BasicBlock *);
315 const Expression *performSymbolicLoadEvaluation(Instruction *,
316 const BasicBlock *);
317 const Expression *performSymbolicStoreEvaluation(Instruction *,
318 const BasicBlock *);
319 const Expression *performSymbolicCallEvaluation(Instruction *,
320 const BasicBlock *);
321 const Expression *performSymbolicPHIEvaluation(Instruction *,
322 const BasicBlock *);
Daniel Berlind7c12ee2016-12-25 22:23:49 +0000323 bool setMemoryAccessEquivTo(MemoryAccess *From, MemoryAccess *To);
Davide Italiano7e274e02016-12-22 16:03:48 +0000324 const Expression *performSymbolicAggrValueEvaluation(Instruction *,
325 const BasicBlock *);
326
327 // Congruence finding.
328 // Templated to allow them to work both on BB's and BB-edges.
329 template <class T>
330 Value *lookupOperandLeader(Value *, const User *, const T &) const;
Daniel Berlinc0431fd2017-01-13 22:40:01 +0000331 void performCongruenceFinding(Instruction *, const Expression *);
332 void moveValueToNewCongruenceClass(Instruction *, CongruenceClass *,
Daniel Berlin3a1bd022017-01-11 20:22:05 +0000333 CongruenceClass *);
Davide Italiano7e274e02016-12-22 16:03:48 +0000334 // Reachability handling.
335 void updateReachableEdge(BasicBlock *, BasicBlock *);
336 void processOutgoingEdges(TerminatorInst *, BasicBlock *);
Daniel Berlin8a6a8612016-12-24 00:04:07 +0000337 bool isOnlyReachableViaThisEdge(const BasicBlockEdge &) const;
Davide Italiano7e274e02016-12-22 16:03:48 +0000338 Value *findConditionEquivalence(Value *, BasicBlock *) const;
Daniel Berlind7c12ee2016-12-25 22:23:49 +0000339 MemoryAccess *lookupMemoryAccessEquiv(MemoryAccess *) const;
Davide Italiano7e274e02016-12-22 16:03:48 +0000340
341 // Elimination.
342 struct ValueDFS;
343 void convertDenseToDFSOrdered(CongruenceClass::MemberSet &,
Daniel Berlin2f1fbcc2017-01-09 05:34:19 +0000344 SmallVectorImpl<ValueDFS> &);
Davide Italiano7e274e02016-12-22 16:03:48 +0000345
346 bool eliminateInstructions(Function &);
347 void replaceInstruction(Instruction *, Value *);
348 void markInstructionForDeletion(Instruction *);
349 void deleteInstructionsInBlock(BasicBlock *);
350
351 // New instruction creation.
352 void handleNewInstruction(Instruction *){};
Daniel Berlin32f8d562017-01-07 16:55:14 +0000353
354 // Various instruction touch utilities
Davide Italiano7e274e02016-12-22 16:03:48 +0000355 void markUsersTouched(Value *);
356 void markMemoryUsersTouched(MemoryAccess *);
Daniel Berlin32f8d562017-01-07 16:55:14 +0000357 void markLeaderChangeTouched(CongruenceClass *CC);
Davide Italiano7e274e02016-12-22 16:03:48 +0000358
359 // Utilities.
360 void cleanupTables();
361 std::pair<unsigned, unsigned> assignDFSNumbers(BasicBlock *, unsigned);
362 void updateProcessedCount(Value *V);
Daniel Berlinf6eba4b2017-01-11 20:22:36 +0000363 void verifyMemoryCongruency() const;
364 bool singleReachablePHIPath(const MemoryAccess *, const MemoryAccess *) const;
Davide Italiano7e274e02016-12-22 16:03:48 +0000365};
366
367char NewGVN::ID = 0;
368
369// createGVNPass - The public interface to this file.
370FunctionPass *llvm::createNewGVNPass() { return new NewGVN(); }
371
Davide Italianob1114092016-12-28 13:37:17 +0000372template <typename T>
373static bool equalsLoadStoreHelper(const T &LHS, const Expression &RHS) {
374 if ((!isa<LoadExpression>(RHS) && !isa<StoreExpression>(RHS)) ||
Daniel Berlin7ad1ea02016-12-29 00:49:32 +0000375 !LHS.BasicExpression::equals(RHS)) {
Davide Italiano7e274e02016-12-22 16:03:48 +0000376 return false;
Daniel Berlin7ad1ea02016-12-29 00:49:32 +0000377 } else if (const auto *L = dyn_cast<LoadExpression>(&RHS)) {
Davide Italianob1114092016-12-28 13:37:17 +0000378 if (LHS.getDefiningAccess() != L->getDefiningAccess())
Davide Italiano7e274e02016-12-22 16:03:48 +0000379 return false;
Daniel Berlin7ad1ea02016-12-29 00:49:32 +0000380 } else if (const auto *S = dyn_cast<StoreExpression>(&RHS)) {
Davide Italianob1114092016-12-28 13:37:17 +0000381 if (LHS.getDefiningAccess() != S->getDefiningAccess())
Davide Italiano7e274e02016-12-22 16:03:48 +0000382 return false;
Daniel Berlin7ad1ea02016-12-29 00:49:32 +0000383 }
Davide Italiano7e274e02016-12-22 16:03:48 +0000384 return true;
385}
386
Davide Italianob1114092016-12-28 13:37:17 +0000387bool LoadExpression::equals(const Expression &Other) const {
388 return equalsLoadStoreHelper(*this, Other);
389}
Davide Italiano7e274e02016-12-22 16:03:48 +0000390
Davide Italianob1114092016-12-28 13:37:17 +0000391bool StoreExpression::equals(const Expression &Other) const {
392 return equalsLoadStoreHelper(*this, Other);
Davide Italiano7e274e02016-12-22 16:03:48 +0000393}
394
395#ifndef NDEBUG
396static std::string getBlockName(const BasicBlock *B) {
Davide Italiano0e714802016-12-28 14:00:11 +0000397 return DOTGraphTraits<const Function *>::getSimpleNodeLabel(B, nullptr);
Davide Italiano7e274e02016-12-22 16:03:48 +0000398}
399#endif
400
401INITIALIZE_PASS_BEGIN(NewGVN, "newgvn", "Global Value Numbering", false, false)
402INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
403INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
404INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
405INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
406INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
407INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
408INITIALIZE_PASS_END(NewGVN, "newgvn", "Global Value Numbering", false, false)
409
410PHIExpression *NewGVN::createPHIExpression(Instruction *I) {
Daniel Berlind92e7f92017-01-07 00:01:42 +0000411 BasicBlock *PHIBlock = I->getParent();
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000412 auto *PN = cast<PHINode>(I);
Daniel Berlind92e7f92017-01-07 00:01:42 +0000413 auto *E =
414 new (ExpressionAllocator) PHIExpression(PN->getNumOperands(), PHIBlock);
Davide Italiano7e274e02016-12-22 16:03:48 +0000415
416 E->allocateOperands(ArgRecycler, ExpressionAllocator);
417 E->setType(I->getType());
418 E->setOpcode(I->getOpcode());
Daniel Berlin85cbc8c2016-12-26 19:57:25 +0000419
420 auto ReachablePhiArg = [&](const Use &U) {
421 return ReachableBlocks.count(PN->getIncomingBlock(U));
422 };
423
424 // Filter out unreachable operands
425 auto Filtered = make_filter_range(PN->operands(), ReachablePhiArg);
426
427 std::transform(Filtered.begin(), Filtered.end(), op_inserter(E),
428 [&](const Use &U) -> Value * {
Daniel Berlind92e7f92017-01-07 00:01:42 +0000429 // Don't try to transform self-defined phis.
Daniel Berlin85cbc8c2016-12-26 19:57:25 +0000430 if (U == PN)
431 return PN;
Daniel Berlind92e7f92017-01-07 00:01:42 +0000432 const BasicBlockEdge BBE(PN->getIncomingBlock(U), PHIBlock);
Daniel Berlin85cbc8c2016-12-26 19:57:25 +0000433 return lookupOperandLeader(U, I, BBE);
434 });
Davide Italiano7e274e02016-12-22 16:03:48 +0000435 return E;
436}
437
438// Set basic expression info (Arguments, type, opcode) for Expression
439// E from Instruction I in block B.
440bool NewGVN::setBasicExpressionInfo(Instruction *I, BasicExpression *E,
441 const BasicBlock *B) {
442 bool AllConstant = true;
443 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
444 E->setType(GEP->getSourceElementType());
445 else
446 E->setType(I->getType());
447 E->setOpcode(I->getOpcode());
448 E->allocateOperands(ArgRecycler, ExpressionAllocator);
449
Daniel Berlin85cbc8c2016-12-26 19:57:25 +0000450 // Transform the operand array into an operand leader array, and keep track of
451 // whether all members are constant.
452 std::transform(I->op_begin(), I->op_end(), op_inserter(E), [&](Value *O) {
Davide Italiano7e274e02016-12-22 16:03:48 +0000453 auto Operand = lookupOperandLeader(O, I, B);
Daniel Berlin85cbc8c2016-12-26 19:57:25 +0000454 AllConstant &= isa<Constant>(Operand);
455 return Operand;
456 });
457
Davide Italiano7e274e02016-12-22 16:03:48 +0000458 return AllConstant;
459}
460
461const Expression *NewGVN::createBinaryExpression(unsigned Opcode, Type *T,
462 Value *Arg1, Value *Arg2,
463 const BasicBlock *B) {
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000464 auto *E = new (ExpressionAllocator) BasicExpression(2);
Davide Italiano7e274e02016-12-22 16:03:48 +0000465
466 E->setType(T);
467 E->setOpcode(Opcode);
468 E->allocateOperands(ArgRecycler, ExpressionAllocator);
469 if (Instruction::isCommutative(Opcode)) {
470 // Ensure that commutative instructions that only differ by a permutation
471 // of their operands get the same value number by sorting the operand value
472 // numbers. Since all commutative instructions have two operands it is more
473 // efficient to sort by hand rather than using, say, std::sort.
474 if (Arg1 > Arg2)
475 std::swap(Arg1, Arg2);
476 }
Daniel Berlin65f5f0d2016-12-25 22:10:37 +0000477 E->op_push_back(lookupOperandLeader(Arg1, nullptr, B));
478 E->op_push_back(lookupOperandLeader(Arg2, nullptr, B));
Davide Italiano7e274e02016-12-22 16:03:48 +0000479
480 Value *V = SimplifyBinOp(Opcode, E->getOperand(0), E->getOperand(1), *DL, TLI,
481 DT, AC);
482 if (const Expression *SimplifiedE = checkSimplificationResults(E, nullptr, V))
483 return SimplifiedE;
484 return E;
485}
486
487// Take a Value returned by simplification of Expression E/Instruction
488// I, and see if it resulted in a simpler expression. If so, return
489// that expression.
490// TODO: Once finished, this should not take an Instruction, we only
491// use it for printing.
492const Expression *NewGVN::checkSimplificationResults(Expression *E,
493 Instruction *I, Value *V) {
494 if (!V)
495 return nullptr;
496 if (auto *C = dyn_cast<Constant>(V)) {
497 if (I)
498 DEBUG(dbgs() << "Simplified " << *I << " to "
499 << " constant " << *C << "\n");
500 NumGVNOpsSimplified++;
501 assert(isa<BasicExpression>(E) &&
502 "We should always have had a basic expression here");
503
504 cast<BasicExpression>(E)->deallocateOperands(ArgRecycler);
505 ExpressionAllocator.Deallocate(E);
506 return createConstantExpression(C);
507 } else if (isa<Argument>(V) || isa<GlobalVariable>(V)) {
508 if (I)
509 DEBUG(dbgs() << "Simplified " << *I << " to "
510 << " variable " << *V << "\n");
511 cast<BasicExpression>(E)->deallocateOperands(ArgRecycler);
512 ExpressionAllocator.Deallocate(E);
513 return createVariableExpression(V);
514 }
515
516 CongruenceClass *CC = ValueToClass.lookup(V);
517 if (CC && CC->DefiningExpr) {
518 if (I)
519 DEBUG(dbgs() << "Simplified " << *I << " to "
520 << " expression " << *V << "\n");
521 NumGVNOpsSimplified++;
522 assert(isa<BasicExpression>(E) &&
523 "We should always have had a basic expression here");
524 cast<BasicExpression>(E)->deallocateOperands(ArgRecycler);
525 ExpressionAllocator.Deallocate(E);
526 return CC->DefiningExpr;
527 }
528 return nullptr;
529}
530
531const Expression *NewGVN::createExpression(Instruction *I,
532 const BasicBlock *B) {
533
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000534 auto *E = new (ExpressionAllocator) BasicExpression(I->getNumOperands());
Davide Italiano7e274e02016-12-22 16:03:48 +0000535
536 bool AllConstant = setBasicExpressionInfo(I, E, B);
537
538 if (I->isCommutative()) {
539 // Ensure that commutative instructions that only differ by a permutation
540 // of their operands get the same value number by sorting the operand value
541 // numbers. Since all commutative instructions have two operands it is more
542 // efficient to sort by hand rather than using, say, std::sort.
543 assert(I->getNumOperands() == 2 && "Unsupported commutative instruction!");
544 if (E->getOperand(0) > E->getOperand(1))
545 E->swapOperands(0, 1);
546 }
547
548 // Perform simplificaiton
549 // TODO: Right now we only check to see if we get a constant result.
550 // We may get a less than constant, but still better, result for
551 // some operations.
552 // IE
553 // add 0, x -> x
554 // and x, x -> x
555 // We should handle this by simply rewriting the expression.
556 if (auto *CI = dyn_cast<CmpInst>(I)) {
557 // Sort the operand value numbers so x<y and y>x get the same value
558 // number.
559 CmpInst::Predicate Predicate = CI->getPredicate();
560 if (E->getOperand(0) > E->getOperand(1)) {
561 E->swapOperands(0, 1);
562 Predicate = CmpInst::getSwappedPredicate(Predicate);
563 }
564 E->setOpcode((CI->getOpcode() << 8) | Predicate);
565 // TODO: 25% of our time is spent in SimplifyCmpInst with pointer operands
566 // TODO: Since we noop bitcasts, we may need to check types before
567 // simplifying, so that we don't end up simplifying based on a wrong
568 // type assumption. We should clean this up so we can use constants of the
569 // wrong type
570
571 assert(I->getOperand(0)->getType() == I->getOperand(1)->getType() &&
572 "Wrong types on cmp instruction");
573 if ((E->getOperand(0)->getType() == I->getOperand(0)->getType() &&
574 E->getOperand(1)->getType() == I->getOperand(1)->getType())) {
575 Value *V = SimplifyCmpInst(Predicate, E->getOperand(0), E->getOperand(1),
576 *DL, TLI, DT, AC);
577 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
578 return SimplifiedE;
579 }
580 } else if (isa<SelectInst>(I)) {
581 if (isa<Constant>(E->getOperand(0)) ||
582 (E->getOperand(1)->getType() == I->getOperand(1)->getType() &&
583 E->getOperand(2)->getType() == I->getOperand(2)->getType())) {
584 Value *V = SimplifySelectInst(E->getOperand(0), E->getOperand(1),
585 E->getOperand(2), *DL, TLI, DT, AC);
586 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
587 return SimplifiedE;
588 }
589 } else if (I->isBinaryOp()) {
590 Value *V = SimplifyBinOp(E->getOpcode(), E->getOperand(0), E->getOperand(1),
591 *DL, TLI, DT, AC);
592 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
593 return SimplifiedE;
594 } else if (auto *BI = dyn_cast<BitCastInst>(I)) {
595 Value *V = SimplifyInstruction(BI, *DL, TLI, DT, AC);
596 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
597 return SimplifiedE;
598 } else if (isa<GetElementPtrInst>(I)) {
599 Value *V = SimplifyGEPInst(E->getType(),
Daniel Berlin65f5f0d2016-12-25 22:10:37 +0000600 ArrayRef<Value *>(E->op_begin(), E->op_end()),
Davide Italiano7e274e02016-12-22 16:03:48 +0000601 *DL, TLI, DT, AC);
602 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
603 return SimplifiedE;
604 } else if (AllConstant) {
605 // We don't bother trying to simplify unless all of the operands
606 // were constant.
607 // TODO: There are a lot of Simplify*'s we could call here, if we
608 // wanted to. The original motivating case for this code was a
609 // zext i1 false to i8, which we don't have an interface to
610 // simplify (IE there is no SimplifyZExt).
611
612 SmallVector<Constant *, 8> C;
613 for (Value *Arg : E->operands())
Piotr Padlewski6c37d292016-12-28 23:24:02 +0000614 C.emplace_back(cast<Constant>(Arg));
Davide Italiano7e274e02016-12-22 16:03:48 +0000615
616 if (Value *V = ConstantFoldInstOperands(I, C, *DL, TLI))
617 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
618 return SimplifiedE;
619 }
620 return E;
621}
622
623const AggregateValueExpression *
624NewGVN::createAggregateValueExpression(Instruction *I, const BasicBlock *B) {
625 if (auto *II = dyn_cast<InsertValueInst>(I)) {
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000626 auto *E = new (ExpressionAllocator)
Davide Italiano7e274e02016-12-22 16:03:48 +0000627 AggregateValueExpression(I->getNumOperands(), II->getNumIndices());
628 setBasicExpressionInfo(I, E, B);
629 E->allocateIntOperands(ExpressionAllocator);
Daniel Berlin85cbc8c2016-12-26 19:57:25 +0000630 std::copy(II->idx_begin(), II->idx_end(), int_op_inserter(E));
Davide Italiano7e274e02016-12-22 16:03:48 +0000631 return E;
Davide Italiano7e274e02016-12-22 16:03:48 +0000632 } else if (auto *EI = dyn_cast<ExtractValueInst>(I)) {
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000633 auto *E = new (ExpressionAllocator)
Davide Italiano7e274e02016-12-22 16:03:48 +0000634 AggregateValueExpression(I->getNumOperands(), EI->getNumIndices());
635 setBasicExpressionInfo(EI, E, B);
636 E->allocateIntOperands(ExpressionAllocator);
Daniel Berlin85cbc8c2016-12-26 19:57:25 +0000637 std::copy(EI->idx_begin(), EI->idx_end(), int_op_inserter(E));
Davide Italiano7e274e02016-12-22 16:03:48 +0000638 return E;
639 }
640 llvm_unreachable("Unhandled type of aggregate value operation");
641}
642
Daniel Berlin85f91b02016-12-26 20:06:58 +0000643const VariableExpression *NewGVN::createVariableExpression(Value *V) {
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000644 auto *E = new (ExpressionAllocator) VariableExpression(V);
Davide Italiano7e274e02016-12-22 16:03:48 +0000645 E->setOpcode(V->getValueID());
646 return E;
647}
648
649const Expression *NewGVN::createVariableOrConstant(Value *V,
650 const BasicBlock *B) {
651 auto Leader = lookupOperandLeader(V, nullptr, B);
652 if (auto *C = dyn_cast<Constant>(Leader))
653 return createConstantExpression(C);
654 return createVariableExpression(Leader);
655}
656
Daniel Berlin85f91b02016-12-26 20:06:58 +0000657const ConstantExpression *NewGVN::createConstantExpression(Constant *C) {
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000658 auto *E = new (ExpressionAllocator) ConstantExpression(C);
Davide Italiano7e274e02016-12-22 16:03:48 +0000659 E->setOpcode(C->getValueID());
660 return E;
661}
662
Daniel Berlin02c6b172017-01-02 18:00:53 +0000663const UnknownExpression *NewGVN::createUnknownExpression(Instruction *I) {
664 auto *E = new (ExpressionAllocator) UnknownExpression(I);
665 E->setOpcode(I->getOpcode());
666 return E;
667}
668
Davide Italiano7e274e02016-12-22 16:03:48 +0000669const CallExpression *NewGVN::createCallExpression(CallInst *CI,
670 MemoryAccess *HV,
671 const BasicBlock *B) {
672 // FIXME: Add operand bundles for calls.
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000673 auto *E =
Davide Italiano7e274e02016-12-22 16:03:48 +0000674 new (ExpressionAllocator) CallExpression(CI->getNumOperands(), CI, HV);
675 setBasicExpressionInfo(CI, E, B);
676 return E;
677}
678
679// See if we have a congruence class and leader for this operand, and if so,
680// return it. Otherwise, return the operand itself.
681template <class T>
Daniel Berlin85f91b02016-12-26 20:06:58 +0000682Value *NewGVN::lookupOperandLeader(Value *V, const User *U, const T &B) const {
Davide Italiano7e274e02016-12-22 16:03:48 +0000683 CongruenceClass *CC = ValueToClass.lookup(V);
684 if (CC && (CC != InitialClass))
685 return CC->RepLeader;
686 return V;
687}
688
Daniel Berlind7c12ee2016-12-25 22:23:49 +0000689MemoryAccess *NewGVN::lookupMemoryAccessEquiv(MemoryAccess *MA) const {
690 MemoryAccess *Result = MemoryAccessEquiv.lookup(MA);
691 return Result ? Result : MA;
692}
693
Davide Italiano7e274e02016-12-22 16:03:48 +0000694LoadExpression *NewGVN::createLoadExpression(Type *LoadType, Value *PointerOp,
695 LoadInst *LI, MemoryAccess *DA,
696 const BasicBlock *B) {
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000697 auto *E = new (ExpressionAllocator) LoadExpression(1, LI, DA);
Davide Italiano7e274e02016-12-22 16:03:48 +0000698 E->allocateOperands(ArgRecycler, ExpressionAllocator);
699 E->setType(LoadType);
700
701 // Give store and loads same opcode so they value number together.
702 E->setOpcode(0);
Davide Italianoa312ca82016-12-26 16:19:34 +0000703 E->op_push_back(lookupOperandLeader(PointerOp, LI, B));
Davide Italiano7e274e02016-12-22 16:03:48 +0000704 if (LI)
705 E->setAlignment(LI->getAlignment());
706
707 // TODO: Value number heap versions. We may be able to discover
708 // things alias analysis can't on it's own (IE that a store and a
709 // load have the same value, and thus, it isn't clobbering the load).
710 return E;
711}
712
713const StoreExpression *NewGVN::createStoreExpression(StoreInst *SI,
714 MemoryAccess *DA,
715 const BasicBlock *B) {
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000716 auto *E =
Davide Italiano7e274e02016-12-22 16:03:48 +0000717 new (ExpressionAllocator) StoreExpression(SI->getNumOperands(), SI, DA);
718 E->allocateOperands(ArgRecycler, ExpressionAllocator);
719 E->setType(SI->getValueOperand()->getType());
720
721 // Give store and loads same opcode so they value number together.
722 E->setOpcode(0);
Daniel Berlin65f5f0d2016-12-25 22:10:37 +0000723 E->op_push_back(lookupOperandLeader(SI->getPointerOperand(), SI, B));
Davide Italiano7e274e02016-12-22 16:03:48 +0000724
725 // TODO: Value number heap versions. We may be able to discover
726 // things alias analysis can't on it's own (IE that a store and a
727 // load have the same value, and thus, it isn't clobbering the load).
728 return E;
729}
730
Daniel Berlinb755aea2017-01-09 05:34:29 +0000731// Utility function to check whether the congruence class has a member other
732// than the given instruction.
733bool hasMemberOtherThanUs(const CongruenceClass *CC, Instruction *I) {
Daniel Berlinf6eba4b2017-01-11 20:22:36 +0000734 // Either it has more than one store, in which case it must contain something
735 // other than us (because it's indexed by value), or if it only has one store
Daniel Berlinb755aea2017-01-09 05:34:29 +0000736 // right now, that member should not be us.
Daniel Berlinf6eba4b2017-01-11 20:22:36 +0000737 return CC->StoreCount > 1 || CC->Members.count(I) == 0;
Daniel Berlinb755aea2017-01-09 05:34:29 +0000738}
739
Davide Italiano7e274e02016-12-22 16:03:48 +0000740const Expression *NewGVN::performSymbolicStoreEvaluation(Instruction *I,
741 const BasicBlock *B) {
Daniel Berlin589cecc2017-01-02 18:00:46 +0000742 // Unlike loads, we never try to eliminate stores, so we do not check if they
743 // are simple and avoid value numbering them.
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000744 auto *SI = cast<StoreInst>(I);
Daniel Berlind7c12ee2016-12-25 22:23:49 +0000745 MemoryAccess *StoreAccess = MSSA->getMemoryAccess(SI);
Daniel Berlinde43ef92017-01-02 19:49:17 +0000746 // See if we are defined by a previous store expression, it already has a
747 // value, and it's the same value as our current store. FIXME: Right now, we
748 // only do this for simple stores, we should expand to cover memcpys, etc.
Daniel Berlin589cecc2017-01-02 18:00:46 +0000749 if (SI->isSimple()) {
Daniel Berlinde43ef92017-01-02 19:49:17 +0000750 // Get the expression, if any, for the RHS of the MemoryDef.
751 MemoryAccess *StoreRHS = lookupMemoryAccessEquiv(
752 cast<MemoryDef>(StoreAccess)->getDefiningAccess());
753 const Expression *OldStore = createStoreExpression(SI, StoreRHS, B);
Daniel Berlin589cecc2017-01-02 18:00:46 +0000754 CongruenceClass *CC = ExpressionToClass.lookup(OldStore);
Daniel Berlinb755aea2017-01-09 05:34:29 +0000755 // Basically, check if the congruence class the store is in is defined by a
756 // store that isn't us, and has the same value. MemorySSA takes care of
757 // ensuring the store has the same memory state as us already.
Daniel Berlin589cecc2017-01-02 18:00:46 +0000758 if (CC && CC->DefiningExpr && isa<StoreExpression>(CC->DefiningExpr) &&
Daniel Berlinb755aea2017-01-09 05:34:29 +0000759 CC->RepLeader == lookupOperandLeader(SI->getValueOperand(), SI, B) &&
760 hasMemberOtherThanUs(CC, I))
Daniel Berlin589cecc2017-01-02 18:00:46 +0000761 return createStoreExpression(SI, StoreRHS, B);
Daniel Berlind7c12ee2016-12-25 22:23:49 +0000762 }
Daniel Berlin589cecc2017-01-02 18:00:46 +0000763
Daniel Berlind7c12ee2016-12-25 22:23:49 +0000764 return createStoreExpression(SI, StoreAccess, B);
Davide Italiano7e274e02016-12-22 16:03:48 +0000765}
766
767const Expression *NewGVN::performSymbolicLoadEvaluation(Instruction *I,
768 const BasicBlock *B) {
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000769 auto *LI = cast<LoadInst>(I);
Davide Italiano7e274e02016-12-22 16:03:48 +0000770
771 // We can eliminate in favor of non-simple loads, but we won't be able to
Daniel Berlin589cecc2017-01-02 18:00:46 +0000772 // eliminate the loads themselves.
Davide Italiano7e274e02016-12-22 16:03:48 +0000773 if (!LI->isSimple())
774 return nullptr;
775
Daniel Berlin85f91b02016-12-26 20:06:58 +0000776 Value *LoadAddressLeader = lookupOperandLeader(LI->getPointerOperand(), I, B);
Davide Italiano7e274e02016-12-22 16:03:48 +0000777 // Load of undef is undef.
778 if (isa<UndefValue>(LoadAddressLeader))
779 return createConstantExpression(UndefValue::get(LI->getType()));
780
781 MemoryAccess *DefiningAccess = MSSAWalker->getClobberingMemoryAccess(I);
782
783 if (!MSSA->isLiveOnEntryDef(DefiningAccess)) {
784 if (auto *MD = dyn_cast<MemoryDef>(DefiningAccess)) {
785 Instruction *DefiningInst = MD->getMemoryInst();
786 // If the defining instruction is not reachable, replace with undef.
787 if (!ReachableBlocks.count(DefiningInst->getParent()))
788 return createConstantExpression(UndefValue::get(LI->getType()));
789 }
790 }
791
Daniel Berlind7c12ee2016-12-25 22:23:49 +0000792 const Expression *E =
793 createLoadExpression(LI->getType(), LI->getPointerOperand(), LI,
794 lookupMemoryAccessEquiv(DefiningAccess), B);
Davide Italiano7e274e02016-12-22 16:03:48 +0000795 return E;
796}
797
798// Evaluate read only and pure calls, and create an expression result.
799const Expression *NewGVN::performSymbolicCallEvaluation(Instruction *I,
800 const BasicBlock *B) {
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000801 auto *CI = cast<CallInst>(I);
Davide Italiano7e274e02016-12-22 16:03:48 +0000802 if (AA->doesNotAccessMemory(CI))
803 return createCallExpression(CI, nullptr, B);
Davide Italianob2225492016-12-27 18:15:39 +0000804 if (AA->onlyReadsMemory(CI)) {
Daniel Berlin85cbc8c2016-12-26 19:57:25 +0000805 MemoryAccess *DefiningAccess = MSSAWalker->getClobberingMemoryAccess(CI);
Daniel Berlin85f91b02016-12-26 20:06:58 +0000806 return createCallExpression(CI, lookupMemoryAccessEquiv(DefiningAccess), B);
Davide Italianob2225492016-12-27 18:15:39 +0000807 }
808 return nullptr;
Davide Italiano7e274e02016-12-22 16:03:48 +0000809}
810
Daniel Berlind7c12ee2016-12-25 22:23:49 +0000811// Update the memory access equivalence table to say that From is equal to To,
812// and return true if this is different from what already existed in the table.
813bool NewGVN::setMemoryAccessEquivTo(MemoryAccess *From, MemoryAccess *To) {
Davide Italiano84126162017-01-02 18:41:34 +0000814 DEBUG(dbgs() << "Setting " << *From << " equivalent to ");
815 if (!To)
816 DEBUG(dbgs() << "itself");
817 else
818 DEBUG(dbgs() << *To);
819 DEBUG(dbgs() << "\n");
Daniel Berlin589cecc2017-01-02 18:00:46 +0000820 auto LookupResult = MemoryAccessEquiv.find(From);
Daniel Berlind7c12ee2016-12-25 22:23:49 +0000821 bool Changed = false;
822 // If it's already in the table, see if the value changed.
Daniel Berlin589cecc2017-01-02 18:00:46 +0000823 if (LookupResult != MemoryAccessEquiv.end()) {
824 if (To && LookupResult->second != To) {
Daniel Berlind7c12ee2016-12-25 22:23:49 +0000825 // It wasn't equivalent before, and now it is.
Daniel Berlin589cecc2017-01-02 18:00:46 +0000826 LookupResult->second = To;
Daniel Berlind7c12ee2016-12-25 22:23:49 +0000827 Changed = true;
828 } else if (!To) {
829 // It used to be equivalent to something, and now it's not.
Daniel Berlin589cecc2017-01-02 18:00:46 +0000830 MemoryAccessEquiv.erase(LookupResult);
Daniel Berlind7c12ee2016-12-25 22:23:49 +0000831 Changed = true;
832 }
Daniel Berlin589cecc2017-01-02 18:00:46 +0000833 } else {
834 assert(!To &&
835 "Memory equivalence should never change from nothing to something");
Daniel Berlind7c12ee2016-12-25 22:23:49 +0000836 }
Daniel Berlin589cecc2017-01-02 18:00:46 +0000837
Daniel Berlind7c12ee2016-12-25 22:23:49 +0000838 return Changed;
839}
Davide Italiano7e274e02016-12-22 16:03:48 +0000840// Evaluate PHI nodes symbolically, and create an expression result.
841const Expression *NewGVN::performSymbolicPHIEvaluation(Instruction *I,
842 const BasicBlock *B) {
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000843 auto *E = cast<PHIExpression>(createPHIExpression(I));
Daniel Berlind92e7f92017-01-07 00:01:42 +0000844 // We match the semantics of SimplifyPhiNode from InstructionSimplify here.
845
846 // See if all arguaments are the same.
847 // We track if any were undef because they need special handling.
848 bool HasUndef = false;
849 auto Filtered = make_filter_range(E->operands(), [&](const Value *Arg) {
850 if (Arg == I)
851 return false;
852 if (isa<UndefValue>(Arg)) {
853 HasUndef = true;
854 return false;
855 }
856 return true;
857 });
858 // If we are left with no operands, it's undef
859 if (Filtered.begin() == Filtered.end()) {
Davide Italiano7e274e02016-12-22 16:03:48 +0000860 DEBUG(dbgs() << "Simplified PHI node " << *I << " to undef"
861 << "\n");
862 E->deallocateOperands(ArgRecycler);
863 ExpressionAllocator.Deallocate(E);
864 return createConstantExpression(UndefValue::get(I->getType()));
865 }
Daniel Berlind92e7f92017-01-07 00:01:42 +0000866 Value *AllSameValue = *(Filtered.begin());
867 ++Filtered.begin();
868 // Can't use std::equal here, sadly, because filter.begin moves.
869 if (llvm::all_of(Filtered, [AllSameValue](const Value *V) {
870 return V == AllSameValue;
871 })) {
872 // In LLVM's non-standard representation of phi nodes, it's possible to have
873 // phi nodes with cycles (IE dependent on other phis that are .... dependent
874 // on the original phi node), especially in weird CFG's where some arguments
875 // are unreachable, or uninitialized along certain paths. This can cause
876 // infinite loops during evaluation. We work around this by not trying to
877 // really evaluate them independently, but instead using a variable
878 // expression to say if one is equivalent to the other.
879 // We also special case undef, so that if we have an undef, we can't use the
880 // common value unless it dominates the phi block.
881 if (HasUndef) {
882 // Only have to check for instructions
Davide Italiano1b97fc32017-01-07 02:05:50 +0000883 if (auto *AllSameInst = dyn_cast<Instruction>(AllSameValue))
Daniel Berlind92e7f92017-01-07 00:01:42 +0000884 if (!DT->dominates(AllSameInst, I))
885 return E;
Davide Italiano7e274e02016-12-22 16:03:48 +0000886 }
887
Davide Italiano7e274e02016-12-22 16:03:48 +0000888 NumGVNPhisAllSame++;
889 DEBUG(dbgs() << "Simplified PHI node " << *I << " to " << *AllSameValue
890 << "\n");
891 E->deallocateOperands(ArgRecycler);
892 ExpressionAllocator.Deallocate(E);
893 if (auto *C = dyn_cast<Constant>(AllSameValue))
894 return createConstantExpression(C);
895 return createVariableExpression(AllSameValue);
896 }
897 return E;
898}
899
900const Expression *
901NewGVN::performSymbolicAggrValueEvaluation(Instruction *I,
902 const BasicBlock *B) {
903 if (auto *EI = dyn_cast<ExtractValueInst>(I)) {
904 auto *II = dyn_cast<IntrinsicInst>(EI->getAggregateOperand());
905 if (II && EI->getNumIndices() == 1 && *EI->idx_begin() == 0) {
906 unsigned Opcode = 0;
907 // EI might be an extract from one of our recognised intrinsics. If it
908 // is we'll synthesize a semantically equivalent expression instead on
909 // an extract value expression.
910 switch (II->getIntrinsicID()) {
911 case Intrinsic::sadd_with_overflow:
912 case Intrinsic::uadd_with_overflow:
913 Opcode = Instruction::Add;
914 break;
915 case Intrinsic::ssub_with_overflow:
916 case Intrinsic::usub_with_overflow:
917 Opcode = Instruction::Sub;
918 break;
919 case Intrinsic::smul_with_overflow:
920 case Intrinsic::umul_with_overflow:
921 Opcode = Instruction::Mul;
922 break;
923 default:
924 break;
925 }
926
927 if (Opcode != 0) {
928 // Intrinsic recognized. Grab its args to finish building the
929 // expression.
930 assert(II->getNumArgOperands() == 2 &&
931 "Expect two args for recognised intrinsics.");
932 return createBinaryExpression(Opcode, EI->getType(),
933 II->getArgOperand(0),
934 II->getArgOperand(1), B);
935 }
936 }
937 }
938
939 return createAggregateValueExpression(I, B);
940}
941
942// Substitute and symbolize the value before value numbering.
943const Expression *NewGVN::performSymbolicEvaluation(Value *V,
944 const BasicBlock *B) {
Davide Italiano0e714802016-12-28 14:00:11 +0000945 const Expression *E = nullptr;
Davide Italiano7e274e02016-12-22 16:03:48 +0000946 if (auto *C = dyn_cast<Constant>(V))
947 E = createConstantExpression(C);
948 else if (isa<Argument>(V) || isa<GlobalVariable>(V)) {
949 E = createVariableExpression(V);
950 } else {
951 // TODO: memory intrinsics.
952 // TODO: Some day, we should do the forward propagation and reassociation
953 // parts of the algorithm.
Piotr Padlewskifc5727b2016-12-28 19:17:17 +0000954 auto *I = cast<Instruction>(V);
Davide Italiano7e274e02016-12-22 16:03:48 +0000955 switch (I->getOpcode()) {
956 case Instruction::ExtractValue:
957 case Instruction::InsertValue:
958 E = performSymbolicAggrValueEvaluation(I, B);
959 break;
960 case Instruction::PHI:
961 E = performSymbolicPHIEvaluation(I, B);
962 break;
963 case Instruction::Call:
964 E = performSymbolicCallEvaluation(I, B);
965 break;
966 case Instruction::Store:
967 E = performSymbolicStoreEvaluation(I, B);
968 break;
969 case Instruction::Load:
970 E = performSymbolicLoadEvaluation(I, B);
971 break;
972 case Instruction::BitCast: {
973 E = createExpression(I, B);
974 } break;
975
976 case Instruction::Add:
977 case Instruction::FAdd:
978 case Instruction::Sub:
979 case Instruction::FSub:
980 case Instruction::Mul:
981 case Instruction::FMul:
982 case Instruction::UDiv:
983 case Instruction::SDiv:
984 case Instruction::FDiv:
985 case Instruction::URem:
986 case Instruction::SRem:
987 case Instruction::FRem:
988 case Instruction::Shl:
989 case Instruction::LShr:
990 case Instruction::AShr:
991 case Instruction::And:
992 case Instruction::Or:
993 case Instruction::Xor:
994 case Instruction::ICmp:
995 case Instruction::FCmp:
996 case Instruction::Trunc:
997 case Instruction::ZExt:
998 case Instruction::SExt:
999 case Instruction::FPToUI:
1000 case Instruction::FPToSI:
1001 case Instruction::UIToFP:
1002 case Instruction::SIToFP:
1003 case Instruction::FPTrunc:
1004 case Instruction::FPExt:
1005 case Instruction::PtrToInt:
1006 case Instruction::IntToPtr:
1007 case Instruction::Select:
1008 case Instruction::ExtractElement:
1009 case Instruction::InsertElement:
1010 case Instruction::ShuffleVector:
1011 case Instruction::GetElementPtr:
1012 E = createExpression(I, B);
1013 break;
1014 default:
1015 return nullptr;
1016 }
1017 }
Davide Italiano7e274e02016-12-22 16:03:48 +00001018 return E;
1019}
1020
1021// There is an edge from 'Src' to 'Dst'. Return true if every path from
1022// the entry block to 'Dst' passes via this edge. In particular 'Dst'
1023// must not be reachable via another edge from 'Src'.
Daniel Berlin8a6a8612016-12-24 00:04:07 +00001024bool NewGVN::isOnlyReachableViaThisEdge(const BasicBlockEdge &E) const {
Davide Italiano7e274e02016-12-22 16:03:48 +00001025
1026 // While in theory it is interesting to consider the case in which Dst has
1027 // more than one predecessor, because Dst might be part of a loop which is
1028 // only reachable from Src, in practice it is pointless since at the time
1029 // GVN runs all such loops have preheaders, which means that Dst will have
1030 // been changed to have only one predecessor, namely Src.
1031 const BasicBlock *Pred = E.getEnd()->getSinglePredecessor();
1032 const BasicBlock *Src = E.getStart();
1033 assert((!Pred || Pred == Src) && "No edge between these basic blocks!");
1034 (void)Src;
1035 return Pred != nullptr;
1036}
1037
1038void NewGVN::markUsersTouched(Value *V) {
1039 // Now mark the users as touched.
Daniel Berline0bd37e2016-12-29 22:15:12 +00001040 for (auto *User : V->users()) {
1041 assert(isa<Instruction>(User) && "Use of value not within an instruction?");
Daniel Berlinaac56842017-01-15 09:18:41 +00001042 TouchedInstructions.set(InstrDFS.lookup(User));
Davide Italiano7e274e02016-12-22 16:03:48 +00001043 }
1044}
1045
1046void NewGVN::markMemoryUsersTouched(MemoryAccess *MA) {
1047 for (auto U : MA->users()) {
1048 if (auto *MUD = dyn_cast<MemoryUseOrDef>(U))
Daniel Berlinaac56842017-01-15 09:18:41 +00001049 TouchedInstructions.set(InstrDFS.lookup(MUD->getMemoryInst()));
Davide Italiano7e274e02016-12-22 16:03:48 +00001050 else
Daniel Berlinaac56842017-01-15 09:18:41 +00001051 TouchedInstructions.set(InstrDFS.lookup(U));
Davide Italiano7e274e02016-12-22 16:03:48 +00001052 }
1053}
1054
Daniel Berlin32f8d562017-01-07 16:55:14 +00001055// Touch the instructions that need to be updated after a congruence class has a
1056// leader change, and mark changed values.
1057void NewGVN::markLeaderChangeTouched(CongruenceClass *CC) {
1058 for (auto M : CC->Members) {
1059 if (auto *I = dyn_cast<Instruction>(M))
Daniel Berlinaac56842017-01-15 09:18:41 +00001060 TouchedInstructions.set(InstrDFS.lookup(I));
Daniel Berlin3a1bd022017-01-11 20:22:05 +00001061 LeaderChanges.insert(M);
1062 }
1063}
1064
1065// Move a value, currently in OldClass, to be part of NewClass
1066// Update OldClass for the move (including changing leaders, etc)
Daniel Berlinc0431fd2017-01-13 22:40:01 +00001067void NewGVN::moveValueToNewCongruenceClass(Instruction *I,
1068 CongruenceClass *OldClass,
Daniel Berlin3a1bd022017-01-11 20:22:05 +00001069 CongruenceClass *NewClass) {
Daniel Berlinc0431fd2017-01-13 22:40:01 +00001070 DEBUG(dbgs() << "New congruence class for " << I << " is " << NewClass->ID
Daniel Berlin3a1bd022017-01-11 20:22:05 +00001071 << "\n");
Daniel Berlinc0431fd2017-01-13 22:40:01 +00001072
1073 if (I == OldClass->NextLeader.first)
1074 OldClass->NextLeader = {nullptr, ~0U};
1075
Daniel Berlin89fea6f2017-01-20 06:38:41 +00001076 // It's possible, though unlikely, for us to discover equivalences such
1077 // that the current leader does not dominate the old one.
1078 // This statistic tracks how often this happens.
1079 // We assert on phi nodes when this happens, currently, for debugging, because
1080 // we want to make sure we name phi node cycles properly.
1081 if (isa<Instruction>(NewClass->RepLeader) && NewClass->RepLeader &&
1082 I != NewClass->RepLeader &&
1083 DT->properlyDominates(
1084 I->getParent(),
1085 cast<Instruction>(NewClass->RepLeader)->getParent())) {
1086 ++NumGVNNotMostDominatingLeader;
1087 assert(!isa<PHINode>(I) &&
1088 "New class for instruction should not be dominated by instruction");
1089 }
Daniel Berlinc0431fd2017-01-13 22:40:01 +00001090
1091 if (NewClass->RepLeader != I) {
1092 auto DFSNum = InstrDFS.lookup(I);
1093 if (DFSNum < NewClass->NextLeader.second)
1094 NewClass->NextLeader = {I, DFSNum};
1095 }
1096
1097 OldClass->Members.erase(I);
1098 NewClass->Members.insert(I);
1099 if (isa<StoreInst>(I)) {
Daniel Berlin3a1bd022017-01-11 20:22:05 +00001100 --OldClass->StoreCount;
Davide Italiano0dc68bf2017-01-11 22:00:29 +00001101 assert(OldClass->StoreCount >= 0);
Daniel Berlin3a1bd022017-01-11 20:22:05 +00001102 ++NewClass->StoreCount;
Davide Italianoeac05f62017-01-11 23:41:24 +00001103 assert(NewClass->StoreCount > 0);
Daniel Berlin3a1bd022017-01-11 20:22:05 +00001104 }
1105
Daniel Berlinc0431fd2017-01-13 22:40:01 +00001106 ValueToClass[I] = NewClass;
Daniel Berlin3a1bd022017-01-11 20:22:05 +00001107 // See if we destroyed the class or need to swap leaders.
1108 if (OldClass->Members.empty() && OldClass != InitialClass) {
1109 if (OldClass->DefiningExpr) {
1110 OldClass->Dead = true;
1111 DEBUG(dbgs() << "Erasing expression " << OldClass->DefiningExpr
1112 << " from table\n");
1113 ExpressionToClass.erase(OldClass->DefiningExpr);
1114 }
Daniel Berlinc0431fd2017-01-13 22:40:01 +00001115 } else if (OldClass->RepLeader == I) {
Daniel Berlin3a1bd022017-01-11 20:22:05 +00001116 // When the leader changes, the value numbering of
1117 // everything may change due to symbolization changes, so we need to
1118 // reprocess.
Daniel Berlinc0431fd2017-01-13 22:40:01 +00001119 DEBUG(dbgs() << "Leader change!\n");
1120 ++NumGVNLeaderChanges;
1121 // We don't need to sort members if there is only 1, and we don't care about
1122 // sorting the initial class because everything either gets out of it or is
1123 // unreachable.
1124 if (OldClass->Members.size() == 1 || OldClass == InitialClass) {
1125 OldClass->RepLeader = *(OldClass->Members.begin());
1126 } else if (OldClass->NextLeader.first) {
1127 ++NumGVNAvoidedSortedLeaderChanges;
1128 OldClass->RepLeader = OldClass->NextLeader.first;
1129 OldClass->NextLeader = {nullptr, ~0U};
1130 } else {
1131 ++NumGVNSortedLeaderChanges;
1132 // TODO: If this ends up to slow, we can maintain a dual structure for
1133 // member testing/insertion, or keep things mostly sorted, and sort only
1134 // here, or ....
1135 std::pair<Value *, unsigned> MinDFS = {nullptr, ~0U};
1136 for (const auto X : OldClass->Members) {
1137 auto DFSNum = InstrDFS.lookup(X);
1138 if (DFSNum < MinDFS.second)
1139 MinDFS = {X, DFSNum};
1140 }
1141 OldClass->RepLeader = MinDFS.first;
1142 }
Daniel Berlin3a1bd022017-01-11 20:22:05 +00001143 markLeaderChangeTouched(OldClass);
Daniel Berlin32f8d562017-01-07 16:55:14 +00001144 }
1145}
1146
Davide Italiano7e274e02016-12-22 16:03:48 +00001147// Perform congruence finding on a given value numbering expression.
Daniel Berlinc0431fd2017-01-13 22:40:01 +00001148void NewGVN::performCongruenceFinding(Instruction *I, const Expression *E) {
1149 ValueToExpression[I] = E;
Davide Italiano7e274e02016-12-22 16:03:48 +00001150 // This is guaranteed to return something, since it will at least find
1151 // INITIAL.
Daniel Berlin32f8d562017-01-07 16:55:14 +00001152
Daniel Berlinc0431fd2017-01-13 22:40:01 +00001153 CongruenceClass *IClass = ValueToClass[I];
1154 assert(IClass && "Should have found a IClass");
Davide Italiano7e274e02016-12-22 16:03:48 +00001155 // Dead classes should have been eliminated from the mapping.
Daniel Berlinc0431fd2017-01-13 22:40:01 +00001156 assert(!IClass->Dead && "Found a dead class");
Davide Italiano7e274e02016-12-22 16:03:48 +00001157
1158 CongruenceClass *EClass;
Daniel Berlin02c6b172017-01-02 18:00:53 +00001159 if (const auto *VE = dyn_cast<VariableExpression>(E)) {
Davide Italiano7e274e02016-12-22 16:03:48 +00001160 EClass = ValueToClass[VE->getVariableValue()];
1161 } else {
1162 auto lookupResult = ExpressionToClass.insert({E, nullptr});
1163
1164 // If it's not in the value table, create a new congruence class.
1165 if (lookupResult.second) {
Davide Italiano0e714802016-12-28 14:00:11 +00001166 CongruenceClass *NewClass = createCongruenceClass(nullptr, E);
Davide Italiano7e274e02016-12-22 16:03:48 +00001167 auto place = lookupResult.first;
1168 place->second = NewClass;
1169
1170 // Constants and variables should always be made the leader.
Daniel Berlin32f8d562017-01-07 16:55:14 +00001171 if (const auto *CE = dyn_cast<ConstantExpression>(E)) {
Davide Italiano7e274e02016-12-22 16:03:48 +00001172 NewClass->RepLeader = CE->getConstantValue();
Daniel Berlin32f8d562017-01-07 16:55:14 +00001173 } else if (const auto *SE = dyn_cast<StoreExpression>(E)) {
1174 StoreInst *SI = SE->getStoreInst();
1175 NewClass->RepLeader =
1176 lookupOperandLeader(SI->getValueOperand(), SI, SI->getParent());
1177 } else {
Daniel Berlinc0431fd2017-01-13 22:40:01 +00001178 NewClass->RepLeader = I;
Daniel Berlin32f8d562017-01-07 16:55:14 +00001179 }
1180 assert(!isa<VariableExpression>(E) &&
1181 "VariableExpression should have been handled already");
Davide Italiano7e274e02016-12-22 16:03:48 +00001182
1183 EClass = NewClass;
Daniel Berlinc0431fd2017-01-13 22:40:01 +00001184 DEBUG(dbgs() << "Created new congruence class for " << *I
Davide Italiano7e274e02016-12-22 16:03:48 +00001185 << " using expression " << *E << " at " << NewClass->ID
Daniel Berlin589cecc2017-01-02 18:00:46 +00001186 << " and leader " << *(NewClass->RepLeader) << "\n");
Davide Italiano7e274e02016-12-22 16:03:48 +00001187 DEBUG(dbgs() << "Hash value was " << E->getHashValue() << "\n");
1188 } else {
1189 EClass = lookupResult.first->second;
Daniel Berlin589cecc2017-01-02 18:00:46 +00001190 if (isa<ConstantExpression>(E))
1191 assert(isa<Constant>(EClass->RepLeader) &&
1192 "Any class with a constant expression should have a "
1193 "constant leader");
1194
Davide Italiano7e274e02016-12-22 16:03:48 +00001195 assert(EClass && "Somehow don't have an eclass");
1196
1197 assert(!EClass->Dead && "We accidentally looked up a dead class");
1198 }
1199 }
Daniel Berlinc0431fd2017-01-13 22:40:01 +00001200 bool ClassChanged = IClass != EClass;
1201 bool LeaderChanged = LeaderChanges.erase(I);
Daniel Berlin3a1bd022017-01-11 20:22:05 +00001202 if (ClassChanged || LeaderChanged) {
Davide Italiano7e274e02016-12-22 16:03:48 +00001203 DEBUG(dbgs() << "Found class " << EClass->ID << " for expression " << E
1204 << "\n");
1205
Daniel Berlin3a1bd022017-01-11 20:22:05 +00001206 if (ClassChanged)
Daniel Berlinc0431fd2017-01-13 22:40:01 +00001207 moveValueToNewCongruenceClass(I, IClass, EClass);
1208 markUsersTouched(I);
1209 if (MemoryAccess *MA = MSSA->getMemoryAccess(I)) {
1210 // If this is a MemoryDef, we need to update the equivalence table. If
1211 // we determined the expression is congruent to a different memory
1212 // state, use that different memory state. If we determined it didn't,
1213 // we update that as well. Right now, we only support store
1214 // expressions.
1215 if (!isa<MemoryUse>(MA) && isa<StoreExpression>(E) &&
1216 EClass->Members.size() != 1) {
1217 auto *DefAccess = cast<StoreExpression>(E)->getDefiningAccess();
1218 setMemoryAccessEquivTo(MA, DefAccess != MA ? DefAccess : nullptr);
1219 } else {
1220 setMemoryAccessEquivTo(MA, nullptr);
Daniel Berlin589cecc2017-01-02 18:00:46 +00001221 }
Daniel Berlinc0431fd2017-01-13 22:40:01 +00001222 markMemoryUsersTouched(MA);
Daniel Berlin589cecc2017-01-02 18:00:46 +00001223 }
Daniel Berlinc0431fd2017-01-13 22:40:01 +00001224 } else if (auto *SI = dyn_cast<StoreInst>(I)) {
Daniel Berlin32f8d562017-01-07 16:55:14 +00001225 // There is, sadly, one complicating thing for stores. Stores do not
1226 // produce values, only consume them. However, in order to make loads and
1227 // stores value number the same, we ignore the value operand of the store.
1228 // But the value operand will still be the leader of our class, and thus, it
1229 // may change. Because the store is a use, the store will get reprocessed,
1230 // but nothing will change about it, and so nothing above will catch it
1231 // (since the class will not change). In order to make sure everything ends
1232 // up okay, we need to recheck the leader of the class. Since stores of
1233 // different values value number differently due to different memorydefs, we
1234 // are guaranteed the leader is always the same between stores in the same
1235 // class.
1236 DEBUG(dbgs() << "Checking store leader\n");
1237 auto ProperLeader =
1238 lookupOperandLeader(SI->getValueOperand(), SI, SI->getParent());
1239 if (EClass->RepLeader != ProperLeader) {
1240 DEBUG(dbgs() << "Store leader changed, fixing\n");
1241 EClass->RepLeader = ProperLeader;
1242 markLeaderChangeTouched(EClass);
1243 markMemoryUsersTouched(MSSA->getMemoryAccess(SI));
1244 }
Davide Italiano7e274e02016-12-22 16:03:48 +00001245 }
1246}
1247
1248// Process the fact that Edge (from, to) is reachable, including marking
1249// any newly reachable blocks and instructions for processing.
1250void NewGVN::updateReachableEdge(BasicBlock *From, BasicBlock *To) {
1251 // Check if the Edge was reachable before.
1252 if (ReachableEdges.insert({From, To}).second) {
1253 // If this block wasn't reachable before, all instructions are touched.
1254 if (ReachableBlocks.insert(To).second) {
1255 DEBUG(dbgs() << "Block " << getBlockName(To) << " marked reachable\n");
1256 const auto &InstRange = BlockInstRange.lookup(To);
1257 TouchedInstructions.set(InstRange.first, InstRange.second);
1258 } else {
1259 DEBUG(dbgs() << "Block " << getBlockName(To)
1260 << " was reachable, but new edge {" << getBlockName(From)
1261 << "," << getBlockName(To) << "} to it found\n");
1262
1263 // We've made an edge reachable to an existing block, which may
1264 // impact predicates. Otherwise, only mark the phi nodes as touched, as
1265 // they are the only thing that depend on new edges. Anything using their
1266 // values will get propagated to if necessary.
Daniel Berlin589cecc2017-01-02 18:00:46 +00001267 if (MemoryAccess *MemPhi = MSSA->getMemoryAccess(To))
Daniel Berlinaac56842017-01-15 09:18:41 +00001268 TouchedInstructions.set(InstrDFS.lookup(MemPhi));
Daniel Berlin589cecc2017-01-02 18:00:46 +00001269
Davide Italiano7e274e02016-12-22 16:03:48 +00001270 auto BI = To->begin();
1271 while (isa<PHINode>(BI)) {
Daniel Berlinaac56842017-01-15 09:18:41 +00001272 TouchedInstructions.set(InstrDFS.lookup(&*BI));
Davide Italiano7e274e02016-12-22 16:03:48 +00001273 ++BI;
1274 }
1275 }
1276 }
1277}
1278
1279// Given a predicate condition (from a switch, cmp, or whatever) and a block,
1280// see if we know some constant value for it already.
1281Value *NewGVN::findConditionEquivalence(Value *Cond, BasicBlock *B) const {
1282 auto Result = lookupOperandLeader(Cond, nullptr, B);
1283 if (isa<Constant>(Result))
1284 return Result;
1285 return nullptr;
1286}
1287
1288// Process the outgoing edges of a block for reachability.
1289void NewGVN::processOutgoingEdges(TerminatorInst *TI, BasicBlock *B) {
1290 // Evaluate reachability of terminator instruction.
1291 BranchInst *BR;
1292 if ((BR = dyn_cast<BranchInst>(TI)) && BR->isConditional()) {
1293 Value *Cond = BR->getCondition();
1294 Value *CondEvaluated = findConditionEquivalence(Cond, B);
1295 if (!CondEvaluated) {
1296 if (auto *I = dyn_cast<Instruction>(Cond)) {
1297 const Expression *E = createExpression(I, B);
1298 if (const auto *CE = dyn_cast<ConstantExpression>(E)) {
1299 CondEvaluated = CE->getConstantValue();
1300 }
1301 } else if (isa<ConstantInt>(Cond)) {
1302 CondEvaluated = Cond;
1303 }
1304 }
1305 ConstantInt *CI;
1306 BasicBlock *TrueSucc = BR->getSuccessor(0);
1307 BasicBlock *FalseSucc = BR->getSuccessor(1);
1308 if (CondEvaluated && (CI = dyn_cast<ConstantInt>(CondEvaluated))) {
1309 if (CI->isOne()) {
1310 DEBUG(dbgs() << "Condition for Terminator " << *TI
1311 << " evaluated to true\n");
1312 updateReachableEdge(B, TrueSucc);
1313 } else if (CI->isZero()) {
1314 DEBUG(dbgs() << "Condition for Terminator " << *TI
1315 << " evaluated to false\n");
1316 updateReachableEdge(B, FalseSucc);
1317 }
1318 } else {
1319 updateReachableEdge(B, TrueSucc);
1320 updateReachableEdge(B, FalseSucc);
1321 }
1322 } else if (auto *SI = dyn_cast<SwitchInst>(TI)) {
1323 // For switches, propagate the case values into the case
1324 // destinations.
1325
1326 // Remember how many outgoing edges there are to every successor.
1327 SmallDenseMap<BasicBlock *, unsigned, 16> SwitchEdges;
1328
Davide Italiano7e274e02016-12-22 16:03:48 +00001329 Value *SwitchCond = SI->getCondition();
1330 Value *CondEvaluated = findConditionEquivalence(SwitchCond, B);
1331 // See if we were able to turn this switch statement into a constant.
1332 if (CondEvaluated && isa<ConstantInt>(CondEvaluated)) {
Piotr Padlewskifc5727b2016-12-28 19:17:17 +00001333 auto *CondVal = cast<ConstantInt>(CondEvaluated);
Davide Italiano7e274e02016-12-22 16:03:48 +00001334 // We should be able to get case value for this.
1335 auto CaseVal = SI->findCaseValue(CondVal);
1336 if (CaseVal.getCaseSuccessor() == SI->getDefaultDest()) {
1337 // We proved the value is outside of the range of the case.
1338 // We can't do anything other than mark the default dest as reachable,
1339 // and go home.
1340 updateReachableEdge(B, SI->getDefaultDest());
1341 return;
1342 }
1343 // Now get where it goes and mark it reachable.
1344 BasicBlock *TargetBlock = CaseVal.getCaseSuccessor();
1345 updateReachableEdge(B, TargetBlock);
Davide Italiano7e274e02016-12-22 16:03:48 +00001346 } else {
1347 for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) {
1348 BasicBlock *TargetBlock = SI->getSuccessor(i);
1349 ++SwitchEdges[TargetBlock];
1350 updateReachableEdge(B, TargetBlock);
1351 }
1352 }
1353 } else {
1354 // Otherwise this is either unconditional, or a type we have no
1355 // idea about. Just mark successors as reachable.
1356 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
1357 BasicBlock *TargetBlock = TI->getSuccessor(i);
1358 updateReachableEdge(B, TargetBlock);
1359 }
Daniel Berlin589cecc2017-01-02 18:00:46 +00001360
1361 // This also may be a memory defining terminator, in which case, set it
1362 // equivalent to nothing.
1363 if (MemoryAccess *MA = MSSA->getMemoryAccess(TI))
1364 setMemoryAccessEquivTo(MA, nullptr);
Davide Italiano7e274e02016-12-22 16:03:48 +00001365 }
1366}
1367
Daniel Berlin85f91b02016-12-26 20:06:58 +00001368// The algorithm initially places the values of the routine in the INITIAL
1369// congruence
Davide Italiano7e274e02016-12-22 16:03:48 +00001370// class. The leader of INITIAL is the undetermined value `TOP`.
1371// When the algorithm has finished, values still in INITIAL are unreachable.
1372void NewGVN::initializeCongruenceClasses(Function &F) {
1373 // FIXME now i can't remember why this is 2
1374 NextCongruenceNum = 2;
1375 // Initialize all other instructions to be in INITIAL class.
1376 CongruenceClass::MemberSet InitialValues;
Davide Italiano0e714802016-12-28 14:00:11 +00001377 InitialClass = createCongruenceClass(nullptr, nullptr);
Daniel Berlin589cecc2017-01-02 18:00:46 +00001378 for (auto &B : F) {
1379 if (auto *MP = MSSA->getMemoryAccess(&B))
1380 MemoryAccessEquiv.insert({MP, MSSA->getLiveOnEntryDef()});
1381
Daniel Berlin85cbc8c2016-12-26 19:57:25 +00001382 for (auto &I : B) {
1383 InitialValues.insert(&I);
1384 ValueToClass[&I] = InitialClass;
Daniel Berlin589cecc2017-01-02 18:00:46 +00001385 // All memory accesses are equivalent to live on entry to start. They must
1386 // be initialized to something so that initial changes are noticed. For
1387 // the maximal answer, we initialize them all to be the same as
1388 // liveOnEntry. Note that to save time, we only initialize the
1389 // MemoryDef's for stores and all MemoryPhis to be equal. Right now, no
1390 // other expression can generate a memory equivalence. If we start
1391 // handling memcpy/etc, we can expand this.
Davide Italianoeac05f62017-01-11 23:41:24 +00001392 if (isa<StoreInst>(&I)) {
Daniel Berlin589cecc2017-01-02 18:00:46 +00001393 MemoryAccessEquiv.insert(
1394 {MSSA->getMemoryAccess(&I), MSSA->getLiveOnEntryDef()});
Davide Italianoeac05f62017-01-11 23:41:24 +00001395 ++InitialClass->StoreCount;
1396 assert(InitialClass->StoreCount > 0);
1397 }
Daniel Berlin85cbc8c2016-12-26 19:57:25 +00001398 }
Daniel Berlin589cecc2017-01-02 18:00:46 +00001399 }
Davide Italiano7e274e02016-12-22 16:03:48 +00001400 InitialClass->Members.swap(InitialValues);
1401
1402 // Initialize arguments to be in their own unique congruence classes
1403 for (auto &FA : F.args())
1404 createSingletonCongruenceClass(&FA);
1405}
1406
1407void NewGVN::cleanupTables() {
1408 for (unsigned i = 0, e = CongruenceClasses.size(); i != e; ++i) {
1409 DEBUG(dbgs() << "Congruence class " << CongruenceClasses[i]->ID << " has "
1410 << CongruenceClasses[i]->Members.size() << " members\n");
1411 // Make sure we delete the congruence class (probably worth switching to
1412 // a unique_ptr at some point.
1413 delete CongruenceClasses[i];
Davide Italiano0e714802016-12-28 14:00:11 +00001414 CongruenceClasses[i] = nullptr;
Davide Italiano7e274e02016-12-22 16:03:48 +00001415 }
1416
1417 ValueToClass.clear();
1418 ArgRecycler.clear(ExpressionAllocator);
1419 ExpressionAllocator.Reset();
1420 CongruenceClasses.clear();
1421 ExpressionToClass.clear();
1422 ValueToExpression.clear();
1423 ReachableBlocks.clear();
1424 ReachableEdges.clear();
1425#ifndef NDEBUG
1426 ProcessedCount.clear();
1427#endif
Davide Italiano7e274e02016-12-22 16:03:48 +00001428 InstrDFS.clear();
1429 InstructionsToErase.clear();
1430
1431 DFSToInstr.clear();
1432 BlockInstRange.clear();
1433 TouchedInstructions.clear();
1434 DominatedInstRange.clear();
Daniel Berlind7c12ee2016-12-25 22:23:49 +00001435 MemoryAccessEquiv.clear();
Davide Italiano7e274e02016-12-22 16:03:48 +00001436}
1437
1438std::pair<unsigned, unsigned> NewGVN::assignDFSNumbers(BasicBlock *B,
1439 unsigned Start) {
1440 unsigned End = Start;
Daniel Berlind7c12ee2016-12-25 22:23:49 +00001441 if (MemoryAccess *MemPhi = MSSA->getMemoryAccess(B)) {
1442 InstrDFS[MemPhi] = End++;
Piotr Padlewski6c37d292016-12-28 23:24:02 +00001443 DFSToInstr.emplace_back(MemPhi);
Daniel Berlind7c12ee2016-12-25 22:23:49 +00001444 }
1445
Davide Italiano7e274e02016-12-22 16:03:48 +00001446 for (auto &I : *B) {
1447 InstrDFS[&I] = End++;
Piotr Padlewski6c37d292016-12-28 23:24:02 +00001448 DFSToInstr.emplace_back(&I);
Davide Italiano7e274e02016-12-22 16:03:48 +00001449 }
1450
1451 // All of the range functions taken half-open ranges (open on the end side).
1452 // So we do not subtract one from count, because at this point it is one
1453 // greater than the last instruction.
1454 return std::make_pair(Start, End);
1455}
1456
1457void NewGVN::updateProcessedCount(Value *V) {
1458#ifndef NDEBUG
1459 if (ProcessedCount.count(V) == 0) {
1460 ProcessedCount.insert({V, 1});
1461 } else {
Davide Italiano7cf29dc2017-01-14 20:13:18 +00001462 ++ProcessedCount[V];
Davide Italiano7e274e02016-12-22 16:03:48 +00001463 assert(ProcessedCount[V] < 100 &&
Davide Italiano75e39f92016-12-30 15:01:17 +00001464 "Seem to have processed the same Value a lot");
Davide Italiano7e274e02016-12-22 16:03:48 +00001465 }
1466#endif
1467}
Daniel Berlind7c12ee2016-12-25 22:23:49 +00001468// Evaluate MemoryPhi nodes symbolically, just like PHI nodes
1469void NewGVN::valueNumberMemoryPhi(MemoryPhi *MP) {
1470 // If all the arguments are the same, the MemoryPhi has the same value as the
1471 // argument.
1472 // Filter out unreachable blocks from our operands.
1473 auto Filtered = make_filter_range(MP->operands(), [&](const Use &U) {
1474 return ReachableBlocks.count(MP->getIncomingBlock(U));
1475 });
1476
1477 assert(Filtered.begin() != Filtered.end() &&
1478 "We should not be processing a MemoryPhi in a completely "
1479 "unreachable block");
1480
1481 // Transform the remaining operands into operand leaders.
1482 // FIXME: mapped_iterator should have a range version.
1483 auto LookupFunc = [&](const Use &U) {
1484 return lookupMemoryAccessEquiv(cast<MemoryAccess>(U));
1485 };
1486 auto MappedBegin = map_iterator(Filtered.begin(), LookupFunc);
1487 auto MappedEnd = map_iterator(Filtered.end(), LookupFunc);
1488
1489 // and now check if all the elements are equal.
1490 // Sadly, we can't use std::equals since these are random access iterators.
1491 MemoryAccess *AllSameValue = *MappedBegin;
1492 ++MappedBegin;
1493 bool AllEqual = std::all_of(
1494 MappedBegin, MappedEnd,
1495 [&AllSameValue](const MemoryAccess *V) { return V == AllSameValue; });
1496
1497 if (AllEqual)
1498 DEBUG(dbgs() << "Memory Phi value numbered to " << *AllSameValue << "\n");
1499 else
1500 DEBUG(dbgs() << "Memory Phi value numbered to itself\n");
1501
1502 if (setMemoryAccessEquivTo(MP, AllEqual ? AllSameValue : nullptr))
1503 markMemoryUsersTouched(MP);
1504}
1505
1506// Value number a single instruction, symbolically evaluating, performing
1507// congruence finding, and updating mappings.
1508void NewGVN::valueNumberInstruction(Instruction *I) {
1509 DEBUG(dbgs() << "Processing instruction " << *I << "\n");
Daniel Berlind59e8012016-12-26 18:44:36 +00001510 if (isInstructionTriviallyDead(I, TLI)) {
Daniel Berlind7c12ee2016-12-25 22:23:49 +00001511 DEBUG(dbgs() << "Skipping unused instruction\n");
Daniel Berlind59e8012016-12-26 18:44:36 +00001512 markInstructionForDeletion(I);
Daniel Berlind7c12ee2016-12-25 22:23:49 +00001513 return;
1514 }
1515 if (!I->isTerminator()) {
Daniel Berlin02c6b172017-01-02 18:00:53 +00001516 const auto *Symbolized = performSymbolicEvaluation(I, I->getParent());
1517 // If we couldn't come up with a symbolic expression, use the unknown
1518 // expression
1519 if (Symbolized == nullptr)
1520 Symbolized = createUnknownExpression(I);
Daniel Berlind7c12ee2016-12-25 22:23:49 +00001521 performCongruenceFinding(I, Symbolized);
1522 } else {
Daniel Berlin02c6b172017-01-02 18:00:53 +00001523 // Handle terminators that return values. All of them produce values we
1524 // don't currently understand.
Daniel Berlin25f05b02017-01-02 18:22:38 +00001525 if (!I->getType()->isVoidTy()) {
Daniel Berlin02c6b172017-01-02 18:00:53 +00001526 auto *Symbolized = createUnknownExpression(I);
1527 performCongruenceFinding(I, Symbolized);
1528 }
Daniel Berlind7c12ee2016-12-25 22:23:49 +00001529 processOutgoingEdges(dyn_cast<TerminatorInst>(I), I->getParent());
1530 }
1531}
Davide Italiano7e274e02016-12-22 16:03:48 +00001532
Daniel Berlinf6eba4b2017-01-11 20:22:36 +00001533// Check if there is a path, using single or equal argument phi nodes, from
1534// First to Second.
1535bool NewGVN::singleReachablePHIPath(const MemoryAccess *First,
1536 const MemoryAccess *Second) const {
1537 if (First == Second)
1538 return true;
1539
1540 if (auto *FirstDef = dyn_cast<MemoryUseOrDef>(First)) {
1541 auto *DefAccess = FirstDef->getDefiningAccess();
1542 return singleReachablePHIPath(DefAccess, Second);
1543 } else {
1544 auto *MP = cast<MemoryPhi>(First);
1545 auto ReachableOperandPred = [&](const Use &U) {
1546 return ReachableBlocks.count(MP->getIncomingBlock(U));
1547 };
1548 auto FilteredPhiArgs =
1549 make_filter_range(MP->operands(), ReachableOperandPred);
1550 SmallVector<const Value *, 32> OperandList;
1551 std::copy(FilteredPhiArgs.begin(), FilteredPhiArgs.end(),
1552 std::back_inserter(OperandList));
1553 bool Okay = OperandList.size() == 1;
1554 if (!Okay)
1555 Okay = std::equal(OperandList.begin(), OperandList.end(),
1556 OperandList.begin());
1557 if (Okay)
1558 return singleReachablePHIPath(cast<MemoryAccess>(OperandList[0]), Second);
1559 return false;
1560 }
1561}
1562
Daniel Berlin589cecc2017-01-02 18:00:46 +00001563// Verify the that the memory equivalence table makes sense relative to the
Daniel Berlinf6eba4b2017-01-11 20:22:36 +00001564// congruence classes. Note that this checking is not perfect, and is currently
Davide Italianoed67f192017-01-14 20:15:04 +00001565// subject to very rare false negatives. It is only useful for
1566// testing/debugging.
Daniel Berlinf6eba4b2017-01-11 20:22:36 +00001567void NewGVN::verifyMemoryCongruency() const {
Daniel Berlin589cecc2017-01-02 18:00:46 +00001568 // Anything equivalent in the memory access table should be in the same
1569 // congruence class.
1570
1571 // Filter out the unreachable and trivially dead entries, because they may
1572 // never have been updated if the instructions were not processed.
1573 auto ReachableAccessPred =
1574 [&](const std::pair<const MemoryAccess *, MemoryAccess *> Pair) {
1575 bool Result = ReachableBlocks.count(Pair.first->getBlock());
1576 if (!Result)
1577 return false;
1578 if (auto *MemDef = dyn_cast<MemoryDef>(Pair.first))
1579 return !isInstructionTriviallyDead(MemDef->getMemoryInst());
1580 return true;
1581 };
1582
1583 auto Filtered = make_filter_range(MemoryAccessEquiv, ReachableAccessPred);
1584 for (auto KV : Filtered) {
1585 assert(KV.first != KV.second &&
1586 "We added a useless equivalence to the memory equivalence table");
1587 // Unreachable instructions may not have changed because we never process
1588 // them.
1589 if (!ReachableBlocks.count(KV.first->getBlock()))
1590 continue;
1591 if (auto *FirstMUD = dyn_cast<MemoryUseOrDef>(KV.first)) {
1592 auto *SecondMUD = dyn_cast<MemoryUseOrDef>(KV.second);
Davide Italiano67ada752017-01-02 19:03:16 +00001593 if (FirstMUD && SecondMUD)
Davide Italianoff694052017-01-11 21:58:42 +00001594 assert((singleReachablePHIPath(FirstMUD, SecondMUD) ||
Davide Italianoed67f192017-01-14 20:15:04 +00001595 ValueToClass.lookup(FirstMUD->getMemoryInst()) ==
1596 ValueToClass.lookup(SecondMUD->getMemoryInst())) &&
1597 "The instructions for these memory operations should have "
1598 "been in the same congruence class or reachable through"
1599 "a single argument phi");
Daniel Berlin589cecc2017-01-02 18:00:46 +00001600 } else if (auto *FirstMP = dyn_cast<MemoryPhi>(KV.first)) {
1601
1602 // We can only sanely verify that MemoryDefs in the operand list all have
1603 // the same class.
1604 auto ReachableOperandPred = [&](const Use &U) {
1605 return ReachableBlocks.count(FirstMP->getIncomingBlock(U)) &&
1606 isa<MemoryDef>(U);
1607
1608 };
1609 // All arguments should in the same class, ignoring unreachable arguments
1610 auto FilteredPhiArgs =
1611 make_filter_range(FirstMP->operands(), ReachableOperandPred);
1612 SmallVector<const CongruenceClass *, 16> PhiOpClasses;
1613 std::transform(FilteredPhiArgs.begin(), FilteredPhiArgs.end(),
1614 std::back_inserter(PhiOpClasses), [&](const Use &U) {
1615 const MemoryDef *MD = cast<MemoryDef>(U);
1616 return ValueToClass.lookup(MD->getMemoryInst());
1617 });
1618 assert(std::equal(PhiOpClasses.begin(), PhiOpClasses.end(),
1619 PhiOpClasses.begin()) &&
1620 "All MemoryPhi arguments should be in the same class");
1621 }
1622 }
1623}
1624
Daniel Berlin85f91b02016-12-26 20:06:58 +00001625// This is the main transformation entry point.
Davide Italiano7e274e02016-12-22 16:03:48 +00001626bool NewGVN::runGVN(Function &F, DominatorTree *_DT, AssumptionCache *_AC,
Daniel Berlin85f91b02016-12-26 20:06:58 +00001627 TargetLibraryInfo *_TLI, AliasAnalysis *_AA,
1628 MemorySSA *_MSSA) {
Davide Italiano7e274e02016-12-22 16:03:48 +00001629 bool Changed = false;
1630 DT = _DT;
1631 AC = _AC;
1632 TLI = _TLI;
1633 AA = _AA;
1634 MSSA = _MSSA;
1635 DL = &F.getParent()->getDataLayout();
1636 MSSAWalker = MSSA->getWalker();
1637
1638 // Count number of instructions for sizing of hash tables, and come
1639 // up with a global dfs numbering for instructions.
Daniel Berline0bd37e2016-12-29 22:15:12 +00001640 unsigned ICount = 1;
1641 // Add an empty instruction to account for the fact that we start at 1
1642 DFSToInstr.emplace_back(nullptr);
Davide Italiano7e274e02016-12-22 16:03:48 +00001643 // Note: We want RPO traversal of the blocks, which is not quite the same as
1644 // dominator tree order, particularly with regard whether backedges get
1645 // visited first or second, given a block with multiple successors.
1646 // If we visit in the wrong order, we will end up performing N times as many
1647 // iterations.
Daniel Berlin6658cc92016-12-29 01:12:36 +00001648 // The dominator tree does guarantee that, for a given dom tree node, it's
1649 // parent must occur before it in the RPO ordering. Thus, we only need to sort
1650 // the siblings.
1651 DenseMap<const DomTreeNode *, unsigned> RPOOrdering;
Davide Italiano7e274e02016-12-22 16:03:48 +00001652 ReversePostOrderTraversal<Function *> RPOT(&F);
Daniel Berlin6658cc92016-12-29 01:12:36 +00001653 unsigned Counter = 0;
Davide Italiano7e274e02016-12-22 16:03:48 +00001654 for (auto &B : RPOT) {
Daniel Berlin6658cc92016-12-29 01:12:36 +00001655 auto *Node = DT->getNode(B);
1656 assert(Node && "RPO and Dominator tree should have same reachability");
1657 RPOOrdering[Node] = ++Counter;
1658 }
1659 // Sort dominator tree children arrays into RPO.
1660 for (auto &B : RPOT) {
1661 auto *Node = DT->getNode(B);
1662 if (Node->getChildren().size() > 1)
1663 std::sort(Node->begin(), Node->end(),
1664 [&RPOOrdering](const DomTreeNode *A, const DomTreeNode *B) {
1665 return RPOOrdering[A] < RPOOrdering[B];
1666 });
1667 }
1668
1669 // Now a standard depth first ordering of the domtree is equivalent to RPO.
1670 auto DFI = df_begin(DT->getRootNode());
1671 for (auto DFE = df_end(DT->getRootNode()); DFI != DFE; ++DFI) {
1672 BasicBlock *B = DFI->getBlock();
Davide Italiano7e274e02016-12-22 16:03:48 +00001673 const auto &BlockRange = assignDFSNumbers(B, ICount);
1674 BlockInstRange.insert({B, BlockRange});
1675 ICount += BlockRange.second - BlockRange.first;
1676 }
1677
1678 // Handle forward unreachable blocks and figure out which blocks
1679 // have single preds.
1680 for (auto &B : F) {
1681 // Assign numbers to unreachable blocks.
Daniel Berlin6658cc92016-12-29 01:12:36 +00001682 if (!DFI.nodeVisited(DT->getNode(&B))) {
Davide Italiano7e274e02016-12-22 16:03:48 +00001683 const auto &BlockRange = assignDFSNumbers(&B, ICount);
1684 BlockInstRange.insert({&B, BlockRange});
1685 ICount += BlockRange.second - BlockRange.first;
1686 }
1687 }
1688
Daniel Berline0bd37e2016-12-29 22:15:12 +00001689 TouchedInstructions.resize(ICount);
Davide Italiano7e274e02016-12-22 16:03:48 +00001690 DominatedInstRange.reserve(F.size());
1691 // Ensure we don't end up resizing the expressionToClass map, as
1692 // that can be quite expensive. At most, we have one expression per
1693 // instruction.
Daniel Berline0bd37e2016-12-29 22:15:12 +00001694 ExpressionToClass.reserve(ICount);
Davide Italiano7e274e02016-12-22 16:03:48 +00001695
1696 // Initialize the touched instructions to include the entry block.
1697 const auto &InstRange = BlockInstRange.lookup(&F.getEntryBlock());
1698 TouchedInstructions.set(InstRange.first, InstRange.second);
1699 ReachableBlocks.insert(&F.getEntryBlock());
1700
1701 initializeCongruenceClasses(F);
1702
Daniel Berlin6cc5e442017-01-04 21:01:02 +00001703 unsigned int Iterations = 0;
Davide Italiano7e274e02016-12-22 16:03:48 +00001704 // We start out in the entry block.
1705 BasicBlock *LastBlock = &F.getEntryBlock();
1706 while (TouchedInstructions.any()) {
Daniel Berlin6cc5e442017-01-04 21:01:02 +00001707 ++Iterations;
Davide Italiano7e274e02016-12-22 16:03:48 +00001708 // Walk through all the instructions in all the blocks in RPO.
1709 for (int InstrNum = TouchedInstructions.find_first(); InstrNum != -1;
1710 InstrNum = TouchedInstructions.find_next(InstrNum)) {
Daniel Berline0bd37e2016-12-29 22:15:12 +00001711 assert(InstrNum != 0 && "Bit 0 should never be set, something touched an "
1712 "instruction not in the lookup table");
Daniel Berlind7c12ee2016-12-25 22:23:49 +00001713 Value *V = DFSToInstr[InstrNum];
1714 BasicBlock *CurrBlock = nullptr;
1715
Piotr Padlewskifc5727b2016-12-28 19:17:17 +00001716 if (auto *I = dyn_cast<Instruction>(V))
Daniel Berlind7c12ee2016-12-25 22:23:49 +00001717 CurrBlock = I->getParent();
Piotr Padlewskifc5727b2016-12-28 19:17:17 +00001718 else if (auto *MP = dyn_cast<MemoryPhi>(V))
Daniel Berlind7c12ee2016-12-25 22:23:49 +00001719 CurrBlock = MP->getBlock();
1720 else
1721 llvm_unreachable("DFSToInstr gave us an unknown type of instruction");
Davide Italiano7e274e02016-12-22 16:03:48 +00001722
1723 // If we hit a new block, do reachability processing.
1724 if (CurrBlock != LastBlock) {
1725 LastBlock = CurrBlock;
1726 bool BlockReachable = ReachableBlocks.count(CurrBlock);
1727 const auto &CurrInstRange = BlockInstRange.lookup(CurrBlock);
1728
1729 // If it's not reachable, erase any touched instructions and move on.
1730 if (!BlockReachable) {
1731 TouchedInstructions.reset(CurrInstRange.first, CurrInstRange.second);
1732 DEBUG(dbgs() << "Skipping instructions in block "
1733 << getBlockName(CurrBlock)
1734 << " because it is unreachable\n");
1735 continue;
1736 }
1737 updateProcessedCount(CurrBlock);
1738 }
Davide Italiano7e274e02016-12-22 16:03:48 +00001739
Piotr Padlewskifc5727b2016-12-28 19:17:17 +00001740 if (auto *MP = dyn_cast<MemoryPhi>(V)) {
Daniel Berlind7c12ee2016-12-25 22:23:49 +00001741 DEBUG(dbgs() << "Processing MemoryPhi " << *MP << "\n");
1742 valueNumberMemoryPhi(MP);
Piotr Padlewskifc5727b2016-12-28 19:17:17 +00001743 } else if (auto *I = dyn_cast<Instruction>(V)) {
Daniel Berlind7c12ee2016-12-25 22:23:49 +00001744 valueNumberInstruction(I);
Davide Italiano7e274e02016-12-22 16:03:48 +00001745 } else {
Daniel Berlind7c12ee2016-12-25 22:23:49 +00001746 llvm_unreachable("Should have been a MemoryPhi or Instruction");
Davide Italiano7e274e02016-12-22 16:03:48 +00001747 }
Daniel Berlind7c12ee2016-12-25 22:23:49 +00001748 updateProcessedCount(V);
Davide Italiano7e274e02016-12-22 16:03:48 +00001749 // Reset after processing (because we may mark ourselves as touched when
1750 // we propagate equalities).
1751 TouchedInstructions.reset(InstrNum);
1752 }
1753 }
Daniel Berlin6cc5e442017-01-04 21:01:02 +00001754 NumGVNMaxIterations = std::max(NumGVNMaxIterations.getValue(), Iterations);
Daniel Berlin589cecc2017-01-02 18:00:46 +00001755#ifndef NDEBUG
1756 verifyMemoryCongruency();
1757#endif
Davide Italiano7e274e02016-12-22 16:03:48 +00001758 Changed |= eliminateInstructions(F);
1759
1760 // Delete all instructions marked for deletion.
1761 for (Instruction *ToErase : InstructionsToErase) {
1762 if (!ToErase->use_empty())
1763 ToErase->replaceAllUsesWith(UndefValue::get(ToErase->getType()));
1764
1765 ToErase->eraseFromParent();
1766 }
1767
1768 // Delete all unreachable blocks.
Daniel Berlin85f91b02016-12-26 20:06:58 +00001769 auto UnreachableBlockPred = [&](const BasicBlock &BB) {
1770 return !ReachableBlocks.count(&BB);
1771 };
Daniel Berlin85cbc8c2016-12-26 19:57:25 +00001772
1773 for (auto &BB : make_filter_range(F, UnreachableBlockPred)) {
1774 DEBUG(dbgs() << "We believe block " << getBlockName(&BB)
Daniel Berlin85f91b02016-12-26 20:06:58 +00001775 << " is unreachable\n");
Daniel Berlin85cbc8c2016-12-26 19:57:25 +00001776 deleteInstructionsInBlock(&BB);
1777 Changed = true;
Davide Italiano7e274e02016-12-22 16:03:48 +00001778 }
1779
1780 cleanupTables();
1781 return Changed;
1782}
1783
1784bool NewGVN::runOnFunction(Function &F) {
1785 if (skipFunction(F))
1786 return false;
1787 return runGVN(F, &getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
1788 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F),
1789 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(),
1790 &getAnalysis<AAResultsWrapperPass>().getAAResults(),
1791 &getAnalysis<MemorySSAWrapperPass>().getMSSA());
1792}
1793
Daniel Berlin85f91b02016-12-26 20:06:58 +00001794PreservedAnalyses NewGVNPass::run(Function &F, AnalysisManager<Function> &AM) {
Davide Italiano7e274e02016-12-22 16:03:48 +00001795 NewGVN Impl;
1796
1797 // Apparently the order in which we get these results matter for
1798 // the old GVN (see Chandler's comment in GVN.cpp). I'll keep
1799 // the same order here, just in case.
1800 auto &AC = AM.getResult<AssumptionAnalysis>(F);
1801 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
1802 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
1803 auto &AA = AM.getResult<AAManager>(F);
1804 auto &MSSA = AM.getResult<MemorySSAAnalysis>(F).getMSSA();
1805 bool Changed = Impl.runGVN(F, &DT, &AC, &TLI, &AA, &MSSA);
1806 if (!Changed)
1807 return PreservedAnalyses::all();
1808 PreservedAnalyses PA;
1809 PA.preserve<DominatorTreeAnalysis>();
1810 PA.preserve<GlobalsAA>();
1811 return PA;
1812}
1813
1814// Return true if V is a value that will always be available (IE can
1815// be placed anywhere) in the function. We don't do globals here
1816// because they are often worse to put in place.
1817// TODO: Separate cost from availability
1818static bool alwaysAvailable(Value *V) {
1819 return isa<Constant>(V) || isa<Argument>(V);
1820}
1821
1822// Get the basic block from an instruction/value.
1823static BasicBlock *getBlockForValue(Value *V) {
1824 if (auto *I = dyn_cast<Instruction>(V))
1825 return I->getParent();
1826 return nullptr;
1827}
1828
1829struct NewGVN::ValueDFS {
Piotr Padlewskifc5727b2016-12-28 19:17:17 +00001830 int DFSIn = 0;
1831 int DFSOut = 0;
1832 int LocalNum = 0;
Davide Italiano7e274e02016-12-22 16:03:48 +00001833 // Only one of these will be set.
Piotr Padlewskifc5727b2016-12-28 19:17:17 +00001834 Value *Val = nullptr;
1835 Use *U = nullptr;
Davide Italiano7e274e02016-12-22 16:03:48 +00001836
1837 bool operator<(const ValueDFS &Other) const {
1838 // It's not enough that any given field be less than - we have sets
1839 // of fields that need to be evaluated together to give a proper ordering.
1840 // For example, if you have;
1841 // DFS (1, 3)
1842 // Val 0
1843 // DFS (1, 2)
1844 // Val 50
1845 // We want the second to be less than the first, but if we just go field
1846 // by field, we will get to Val 0 < Val 50 and say the first is less than
1847 // the second. We only want it to be less than if the DFS orders are equal.
1848 //
1849 // Each LLVM instruction only produces one value, and thus the lowest-level
1850 // differentiator that really matters for the stack (and what we use as as a
1851 // replacement) is the local dfs number.
Daniel Berlin85f91b02016-12-26 20:06:58 +00001852 // Everything else in the structure is instruction level, and only affects
1853 // the order in which we will replace operands of a given instruction.
Davide Italiano7e274e02016-12-22 16:03:48 +00001854 //
1855 // For a given instruction (IE things with equal dfsin, dfsout, localnum),
1856 // the order of replacement of uses does not matter.
1857 // IE given,
1858 // a = 5
1859 // b = a + a
Daniel Berlin85f91b02016-12-26 20:06:58 +00001860 // When you hit b, you will have two valuedfs with the same dfsin, out, and
1861 // localnum.
Davide Italiano7e274e02016-12-22 16:03:48 +00001862 // The .val will be the same as well.
1863 // The .u's will be different.
Daniel Berlin85f91b02016-12-26 20:06:58 +00001864 // You will replace both, and it does not matter what order you replace them
1865 // in (IE whether you replace operand 2, then operand 1, or operand 1, then
1866 // operand 2).
1867 // Similarly for the case of same dfsin, dfsout, localnum, but different
1868 // .val's
Davide Italiano7e274e02016-12-22 16:03:48 +00001869 // a = 5
1870 // b = 6
1871 // c = a + b
Daniel Berlin85f91b02016-12-26 20:06:58 +00001872 // in c, we will a valuedfs for a, and one for b,with everything the same
1873 // but .val and .u.
Davide Italiano7e274e02016-12-22 16:03:48 +00001874 // It does not matter what order we replace these operands in.
1875 // You will always end up with the same IR, and this is guaranteed.
1876 return std::tie(DFSIn, DFSOut, LocalNum, Val, U) <
1877 std::tie(Other.DFSIn, Other.DFSOut, Other.LocalNum, Other.Val,
1878 Other.U);
1879 }
1880};
1881
Daniel Berlin2f1fbcc2017-01-09 05:34:19 +00001882void NewGVN::convertDenseToDFSOrdered(
1883 CongruenceClass::MemberSet &Dense,
1884 SmallVectorImpl<ValueDFS> &DFSOrderedSet) {
Davide Italiano7e274e02016-12-22 16:03:48 +00001885 for (auto D : Dense) {
1886 // First add the value.
1887 BasicBlock *BB = getBlockForValue(D);
1888 // Constants are handled prior to ever calling this function, so
1889 // we should only be left with instructions as members.
Chandler Carruthee086762016-12-23 01:38:06 +00001890 assert(BB && "Should have figured out a basic block for value");
Davide Italiano7e274e02016-12-22 16:03:48 +00001891 ValueDFS VD;
1892
Daniel Berlinb66164c2017-01-14 00:24:23 +00001893 DomTreeNode *DomNode = DT->getNode(BB);
1894 VD.DFSIn = DomNode->getDFSNumIn();
1895 VD.DFSOut = DomNode->getDFSNumOut();
Davide Italiano7e274e02016-12-22 16:03:48 +00001896 VD.Val = D;
1897 // If it's an instruction, use the real local dfs number.
1898 if (auto *I = dyn_cast<Instruction>(D))
Daniel Berlinaac56842017-01-15 09:18:41 +00001899 VD.LocalNum = InstrDFS.lookup(I);
Davide Italiano7e274e02016-12-22 16:03:48 +00001900 else
1901 llvm_unreachable("Should have been an instruction");
1902
Piotr Padlewski6c37d292016-12-28 23:24:02 +00001903 DFSOrderedSet.emplace_back(VD);
Davide Italiano7e274e02016-12-22 16:03:48 +00001904
Daniel Berlinb66164c2017-01-14 00:24:23 +00001905 // Now add the uses.
Davide Italiano7e274e02016-12-22 16:03:48 +00001906 for (auto &U : D->uses()) {
1907 if (auto *I = dyn_cast<Instruction>(U.getUser())) {
1908 ValueDFS VD;
1909 // Put the phi node uses in the incoming block.
1910 BasicBlock *IBlock;
1911 if (auto *P = dyn_cast<PHINode>(I)) {
1912 IBlock = P->getIncomingBlock(U);
1913 // Make phi node users appear last in the incoming block
1914 // they are from.
1915 VD.LocalNum = InstrDFS.size() + 1;
1916 } else {
1917 IBlock = I->getParent();
Daniel Berlinaac56842017-01-15 09:18:41 +00001918 VD.LocalNum = InstrDFS.lookup(I);
Davide Italiano7e274e02016-12-22 16:03:48 +00001919 }
Daniel Berlinb66164c2017-01-14 00:24:23 +00001920 DomTreeNode *DomNode = DT->getNode(IBlock);
1921 VD.DFSIn = DomNode->getDFSNumIn();
1922 VD.DFSOut = DomNode->getDFSNumOut();
Davide Italiano7e274e02016-12-22 16:03:48 +00001923 VD.U = &U;
Piotr Padlewski6c37d292016-12-28 23:24:02 +00001924 DFSOrderedSet.emplace_back(VD);
Davide Italiano7e274e02016-12-22 16:03:48 +00001925 }
1926 }
1927 }
1928}
1929
1930static void patchReplacementInstruction(Instruction *I, Value *Repl) {
1931 // Patch the replacement so that it is not more restrictive than the value
1932 // being replaced.
1933 auto *Op = dyn_cast<BinaryOperator>(I);
1934 auto *ReplOp = dyn_cast<BinaryOperator>(Repl);
1935
1936 if (Op && ReplOp)
1937 ReplOp->andIRFlags(Op);
1938
1939 if (auto *ReplInst = dyn_cast<Instruction>(Repl)) {
1940 // FIXME: If both the original and replacement value are part of the
1941 // same control-flow region (meaning that the execution of one
1942 // guarentees the executation of the other), then we can combine the
1943 // noalias scopes here and do better than the general conservative
1944 // answer used in combineMetadata().
1945
1946 // In general, GVN unifies expressions over different control-flow
1947 // regions, and so we need a conservative combination of the noalias
1948 // scopes.
1949 unsigned KnownIDs[] = {
1950 LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
1951 LLVMContext::MD_noalias, LLVMContext::MD_range,
1952 LLVMContext::MD_fpmath, LLVMContext::MD_invariant_load,
1953 LLVMContext::MD_invariant_group};
1954 combineMetadata(ReplInst, I, KnownIDs);
1955 }
1956}
1957
1958static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl) {
1959 patchReplacementInstruction(I, Repl);
1960 I->replaceAllUsesWith(Repl);
1961}
1962
1963void NewGVN::deleteInstructionsInBlock(BasicBlock *BB) {
1964 DEBUG(dbgs() << " BasicBlock Dead:" << *BB);
1965 ++NumGVNBlocksDeleted;
1966
1967 // Check to see if there are non-terminating instructions to delete.
1968 if (isa<TerminatorInst>(BB->begin()))
1969 return;
1970
1971 // Delete the instructions backwards, as it has a reduced likelihood of having
1972 // to update as many def-use and use-def chains. Start after the terminator.
1973 auto StartPoint = BB->rbegin();
1974 ++StartPoint;
1975 // Note that we explicitly recalculate BB->rend() on each iteration,
1976 // as it may change when we remove the first instruction.
1977 for (BasicBlock::reverse_iterator I(StartPoint); I != BB->rend();) {
1978 Instruction &Inst = *I++;
1979 if (!Inst.use_empty())
1980 Inst.replaceAllUsesWith(UndefValue::get(Inst.getType()));
1981 if (isa<LandingPadInst>(Inst))
1982 continue;
1983
1984 Inst.eraseFromParent();
1985 ++NumGVNInstrDeleted;
1986 }
1987}
1988
1989void NewGVN::markInstructionForDeletion(Instruction *I) {
1990 DEBUG(dbgs() << "Marking " << *I << " for deletion\n");
1991 InstructionsToErase.insert(I);
1992}
1993
1994void NewGVN::replaceInstruction(Instruction *I, Value *V) {
1995
1996 DEBUG(dbgs() << "Replacing " << *I << " with " << *V << "\n");
1997 patchAndReplaceAllUsesWith(I, V);
1998 // We save the actual erasing to avoid invalidating memory
1999 // dependencies until we are done with everything.
2000 markInstructionForDeletion(I);
2001}
2002
2003namespace {
2004
2005// This is a stack that contains both the value and dfs info of where
2006// that value is valid.
2007class ValueDFSStack {
2008public:
2009 Value *back() const { return ValueStack.back(); }
2010 std::pair<int, int> dfs_back() const { return DFSStack.back(); }
2011
2012 void push_back(Value *V, int DFSIn, int DFSOut) {
Piotr Padlewski6c37d292016-12-28 23:24:02 +00002013 ValueStack.emplace_back(V);
Davide Italiano7e274e02016-12-22 16:03:48 +00002014 DFSStack.emplace_back(DFSIn, DFSOut);
2015 }
2016 bool empty() const { return DFSStack.empty(); }
2017 bool isInScope(int DFSIn, int DFSOut) const {
2018 if (empty())
2019 return false;
2020 return DFSIn >= DFSStack.back().first && DFSOut <= DFSStack.back().second;
2021 }
2022
2023 void popUntilDFSScope(int DFSIn, int DFSOut) {
2024
2025 // These two should always be in sync at this point.
2026 assert(ValueStack.size() == DFSStack.size() &&
2027 "Mismatch between ValueStack and DFSStack");
2028 while (
2029 !DFSStack.empty() &&
2030 !(DFSIn >= DFSStack.back().first && DFSOut <= DFSStack.back().second)) {
2031 DFSStack.pop_back();
2032 ValueStack.pop_back();
2033 }
2034 }
2035
2036private:
2037 SmallVector<Value *, 8> ValueStack;
2038 SmallVector<std::pair<int, int>, 8> DFSStack;
2039};
2040}
Daniel Berlin04443432017-01-07 03:23:47 +00002041
Davide Italiano7e274e02016-12-22 16:03:48 +00002042bool NewGVN::eliminateInstructions(Function &F) {
2043 // This is a non-standard eliminator. The normal way to eliminate is
2044 // to walk the dominator tree in order, keeping track of available
2045 // values, and eliminating them. However, this is mildly
2046 // pointless. It requires doing lookups on every instruction,
2047 // regardless of whether we will ever eliminate it. For
Daniel Berlin85cbc8c2016-12-26 19:57:25 +00002048 // instructions part of most singleton congruence classes, we know we
2049 // will never eliminate them.
Davide Italiano7e274e02016-12-22 16:03:48 +00002050
2051 // Instead, this eliminator looks at the congruence classes directly, sorts
2052 // them into a DFS ordering of the dominator tree, and then we just
Daniel Berlin85cbc8c2016-12-26 19:57:25 +00002053 // perform elimination straight on the sets by walking the congruence
Davide Italiano7e274e02016-12-22 16:03:48 +00002054 // class member uses in order, and eliminate the ones dominated by the
Daniel Berlin85cbc8c2016-12-26 19:57:25 +00002055 // last member. This is worst case O(E log E) where E = number of
2056 // instructions in a single congruence class. In theory, this is all
2057 // instructions. In practice, it is much faster, as most instructions are
2058 // either in singleton congruence classes or can't possibly be eliminated
2059 // anyway (if there are no overlapping DFS ranges in class).
Davide Italiano7e274e02016-12-22 16:03:48 +00002060 // When we find something not dominated, it becomes the new leader
Daniel Berlin85cbc8c2016-12-26 19:57:25 +00002061 // for elimination purposes.
2062 // TODO: If we wanted to be faster, We could remove any members with no
2063 // overlapping ranges while sorting, as we will never eliminate anything
2064 // with those members, as they don't dominate anything else in our set.
2065
Davide Italiano7e274e02016-12-22 16:03:48 +00002066 bool AnythingReplaced = false;
2067
2068 // Since we are going to walk the domtree anyway, and we can't guarantee the
2069 // DFS numbers are updated, we compute some ourselves.
2070 DT->updateDFSNumbers();
2071
2072 for (auto &B : F) {
2073 if (!ReachableBlocks.count(&B)) {
2074 for (const auto S : successors(&B)) {
2075 for (auto II = S->begin(); isa<PHINode>(II); ++II) {
Piotr Padlewskifc5727b2016-12-28 19:17:17 +00002076 auto &Phi = cast<PHINode>(*II);
Davide Italiano7e274e02016-12-22 16:03:48 +00002077 DEBUG(dbgs() << "Replacing incoming value of " << *II << " for block "
2078 << getBlockName(&B)
2079 << " with undef due to it being unreachable\n");
2080 for (auto &Operand : Phi.incoming_values())
2081 if (Phi.getIncomingBlock(Operand) == &B)
2082 Operand.set(UndefValue::get(Phi.getType()));
2083 }
2084 }
2085 }
Davide Italiano7e274e02016-12-22 16:03:48 +00002086 }
2087
2088 for (CongruenceClass *CC : CongruenceClasses) {
2089 // FIXME: We should eventually be able to replace everything still
2090 // in the initial class with undef, as they should be unreachable.
2091 // Right now, initial still contains some things we skip value
2092 // numbering of (UNREACHABLE's, for example).
2093 if (CC == InitialClass || CC->Dead)
2094 continue;
2095 assert(CC->RepLeader && "We should have had a leader");
2096
2097 // If this is a leader that is always available, and it's a
2098 // constant or has no equivalences, just replace everything with
2099 // it. We then update the congruence class with whatever members
2100 // are left.
2101 if (alwaysAvailable(CC->RepLeader)) {
2102 SmallPtrSet<Value *, 4> MembersLeft;
2103 for (auto M : CC->Members) {
2104
2105 Value *Member = M;
2106
2107 // Void things have no uses we can replace.
2108 if (Member == CC->RepLeader || Member->getType()->isVoidTy()) {
2109 MembersLeft.insert(Member);
2110 continue;
2111 }
2112
2113 DEBUG(dbgs() << "Found replacement " << *(CC->RepLeader) << " for "
2114 << *Member << "\n");
2115 // Due to equality propagation, these may not always be
2116 // instructions, they may be real values. We don't really
2117 // care about trying to replace the non-instructions.
2118 if (auto *I = dyn_cast<Instruction>(Member)) {
2119 assert(CC->RepLeader != I &&
2120 "About to accidentally remove our leader");
2121 replaceInstruction(I, CC->RepLeader);
2122 AnythingReplaced = true;
2123
2124 continue;
2125 } else {
2126 MembersLeft.insert(I);
2127 }
2128 }
2129 CC->Members.swap(MembersLeft);
2130
2131 } else {
2132 DEBUG(dbgs() << "Eliminating in congruence class " << CC->ID << "\n");
2133 // If this is a singleton, we can skip it.
2134 if (CC->Members.size() != 1) {
2135
2136 // This is a stack because equality replacement/etc may place
2137 // constants in the middle of the member list, and we want to use
2138 // those constant values in preference to the current leader, over
2139 // the scope of those constants.
2140 ValueDFSStack EliminationStack;
2141
2142 // Convert the members to DFS ordered sets and then merge them.
Daniel Berlin2f1fbcc2017-01-09 05:34:19 +00002143 SmallVector<ValueDFS, 8> DFSOrderedSet;
Davide Italiano7e274e02016-12-22 16:03:48 +00002144 convertDenseToDFSOrdered(CC->Members, DFSOrderedSet);
2145
2146 // Sort the whole thing.
Daniel Berlin2f1fbcc2017-01-09 05:34:19 +00002147 std::sort(DFSOrderedSet.begin(), DFSOrderedSet.end());
Davide Italiano7e274e02016-12-22 16:03:48 +00002148
Daniel Berlin2f1fbcc2017-01-09 05:34:19 +00002149 for (auto &VD : DFSOrderedSet) {
2150 int MemberDFSIn = VD.DFSIn;
2151 int MemberDFSOut = VD.DFSOut;
2152 Value *Member = VD.Val;
2153 Use *MemberUse = VD.U;
Davide Italiano7e274e02016-12-22 16:03:48 +00002154
Daniel Berlind92e7f92017-01-07 00:01:42 +00002155 if (Member) {
2156 // We ignore void things because we can't get a value from them.
2157 // FIXME: We could actually use this to kill dead stores that are
2158 // dominated by equivalent earlier stores.
2159 if (Member->getType()->isVoidTy())
2160 continue;
2161 }
Davide Italiano7e274e02016-12-22 16:03:48 +00002162
2163 if (EliminationStack.empty()) {
2164 DEBUG(dbgs() << "Elimination Stack is empty\n");
2165 } else {
2166 DEBUG(dbgs() << "Elimination Stack Top DFS numbers are ("
2167 << EliminationStack.dfs_back().first << ","
2168 << EliminationStack.dfs_back().second << ")\n");
2169 }
Davide Italiano7e274e02016-12-22 16:03:48 +00002170
2171 DEBUG(dbgs() << "Current DFS numbers are (" << MemberDFSIn << ","
2172 << MemberDFSOut << ")\n");
2173 // First, we see if we are out of scope or empty. If so,
2174 // and there equivalences, we try to replace the top of
2175 // stack with equivalences (if it's on the stack, it must
2176 // not have been eliminated yet).
2177 // Then we synchronize to our current scope, by
2178 // popping until we are back within a DFS scope that
2179 // dominates the current member.
2180 // Then, what happens depends on a few factors
2181 // If the stack is now empty, we need to push
2182 // If we have a constant or a local equivalence we want to
2183 // start using, we also push.
2184 // Otherwise, we walk along, processing members who are
2185 // dominated by this scope, and eliminate them.
2186 bool ShouldPush =
2187 Member && (EliminationStack.empty() || isa<Constant>(Member));
2188 bool OutOfScope =
2189 !EliminationStack.isInScope(MemberDFSIn, MemberDFSOut);
2190
2191 if (OutOfScope || ShouldPush) {
2192 // Sync to our current scope.
2193 EliminationStack.popUntilDFSScope(MemberDFSIn, MemberDFSOut);
2194 ShouldPush |= Member && EliminationStack.empty();
2195 if (ShouldPush) {
2196 EliminationStack.push_back(Member, MemberDFSIn, MemberDFSOut);
2197 }
2198 }
2199
2200 // If we get to this point, and the stack is empty we must have a use
2201 // with nothing we can use to eliminate it, just skip it.
2202 if (EliminationStack.empty())
2203 continue;
2204
2205 // Skip the Value's, we only want to eliminate on their uses.
2206 if (Member)
2207 continue;
2208 Value *Result = EliminationStack.back();
2209
Daniel Berlind92e7f92017-01-07 00:01:42 +00002210 // Don't replace our existing users with ourselves.
2211 if (MemberUse->get() == Result)
Davide Italiano7e274e02016-12-22 16:03:48 +00002212 continue;
2213
2214 DEBUG(dbgs() << "Found replacement " << *Result << " for "
2215 << *MemberUse->get() << " in " << *(MemberUse->getUser())
2216 << "\n");
2217
2218 // If we replaced something in an instruction, handle the patching of
2219 // metadata.
Daniel Berlin85f91b02016-12-26 20:06:58 +00002220 if (auto *ReplacedInst = dyn_cast<Instruction>(MemberUse->get()))
Davide Italiano7e274e02016-12-22 16:03:48 +00002221 patchReplacementInstruction(ReplacedInst, Result);
2222
2223 assert(isa<Instruction>(MemberUse->getUser()));
2224 MemberUse->set(Result);
2225 AnythingReplaced = true;
2226 }
2227 }
2228 }
2229
2230 // Cleanup the congruence class.
2231 SmallPtrSet<Value *, 4> MembersLeft;
Daniel Berlin25f05b02017-01-02 18:22:38 +00002232 for (Value *Member : CC->Members) {
Davide Italiano7e274e02016-12-22 16:03:48 +00002233 if (Member->getType()->isVoidTy()) {
2234 MembersLeft.insert(Member);
2235 continue;
2236 }
2237
2238 if (auto *MemberInst = dyn_cast<Instruction>(Member)) {
2239 if (isInstructionTriviallyDead(MemberInst)) {
2240 // TODO: Don't mark loads of undefs.
2241 markInstructionForDeletion(MemberInst);
2242 continue;
2243 }
2244 }
2245 MembersLeft.insert(Member);
2246 }
2247 CC->Members.swap(MembersLeft);
2248 }
2249
2250 return AnythingReplaced;
2251}