| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1 | //===---- 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 | /// |
| Daniel Berlin | db3c7be | 2017-01-26 21:39:49 +0000 | [diff] [blame] | 20 | /// A brief overview of the algorithm: The algorithm is essentially the same as |
| 21 | /// the standard RPO value numbering algorithm (a good reference is the paper |
| 22 | /// "SCC based value numbering" by L. Taylor Simpson) with one major difference: |
| 23 | /// The RPO algorithm proceeds, on every iteration, to process every reachable |
| 24 | /// block and every instruction in that block. This is because the standard RPO |
| 25 | /// algorithm does not track what things have the same value number, it only |
| 26 | /// tracks what the value number of a given operation is (the mapping is |
| 27 | /// operation -> value number). Thus, when a value number of an operation |
| 28 | /// changes, it must reprocess everything to ensure all uses of a value number |
| 29 | /// get updated properly. In constrast, the sparse algorithm we use *also* |
| 30 | /// tracks what operations have a given value number (IE it also tracks the |
| 31 | /// reverse mapping from value number -> operations with that value number), so |
| 32 | /// that it only needs to reprocess the instructions that are affected when |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 33 | /// something's value number changes. The vast majority of complexity and code |
| 34 | /// in this file is devoted to tracking what value numbers could change for what |
| 35 | /// instructions when various things happen. The rest of the algorithm is |
| 36 | /// devoted to performing symbolic evaluation, forward propagation, and |
| 37 | /// simplification of operations based on the value numbers deduced so far |
| 38 | /// |
| 39 | /// In order to make the GVN mostly-complete, we use a technique derived from |
| 40 | /// "Detection of Redundant Expressions: A Complete and Polynomial-time |
| 41 | /// Algorithm in SSA" by R.R. Pai. The source of incompleteness in most SSA |
| 42 | /// based GVN algorithms is related to their inability to detect equivalence |
| 43 | /// between phi of ops (IE phi(a+b, c+d)) and op of phis (phi(a,c) + phi(b, d)). |
| 44 | /// We resolve this issue by generating the equivalent "phi of ops" form for |
| 45 | /// each op of phis we see, in a way that only takes polynomial time to resolve. |
| Daniel Berlin | db3c7be | 2017-01-26 21:39:49 +0000 | [diff] [blame] | 46 | /// |
| 47 | /// We also do not perform elimination by using any published algorithm. All |
| 48 | /// published algorithms are O(Instructions). Instead, we use a technique that |
| 49 | /// is O(number of operations with the same value number), enabling us to skip |
| 50 | /// trying to eliminate things that have unique value numbers. |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
| 52 | |
| 53 | #include "llvm/Transforms/Scalar/NewGVN.h" |
| 54 | #include "llvm/ADT/BitVector.h" |
| 55 | #include "llvm/ADT/DenseMap.h" |
| 56 | #include "llvm/ADT/DenseSet.h" |
| 57 | #include "llvm/ADT/DepthFirstIterator.h" |
| 58 | #include "llvm/ADT/Hashing.h" |
| 59 | #include "llvm/ADT/MapVector.h" |
| 60 | #include "llvm/ADT/PostOrderIterator.h" |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 61 | #include "llvm/ADT/STLExtras.h" |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 62 | #include "llvm/ADT/SmallPtrSet.h" |
| 63 | #include "llvm/ADT/SmallSet.h" |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 64 | #include "llvm/ADT/Statistic.h" |
| 65 | #include "llvm/ADT/TinyPtrVector.h" |
| 66 | #include "llvm/Analysis/AliasAnalysis.h" |
| 67 | #include "llvm/Analysis/AssumptionCache.h" |
| 68 | #include "llvm/Analysis/CFG.h" |
| 69 | #include "llvm/Analysis/CFGPrinter.h" |
| 70 | #include "llvm/Analysis/ConstantFolding.h" |
| 71 | #include "llvm/Analysis/GlobalsModRef.h" |
| 72 | #include "llvm/Analysis/InstructionSimplify.h" |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 73 | #include "llvm/Analysis/MemoryBuiltins.h" |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 74 | #include "llvm/Analysis/MemoryLocation.h" |
| Daniel Berlin | 2f72b19 | 2017-04-14 02:53:37 +0000 | [diff] [blame] | 75 | #include "llvm/Analysis/MemorySSA.h" |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 76 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 77 | #include "llvm/IR/DataLayout.h" |
| 78 | #include "llvm/IR/Dominators.h" |
| 79 | #include "llvm/IR/GlobalVariable.h" |
| 80 | #include "llvm/IR/IRBuilder.h" |
| 81 | #include "llvm/IR/IntrinsicInst.h" |
| 82 | #include "llvm/IR/LLVMContext.h" |
| 83 | #include "llvm/IR/Metadata.h" |
| 84 | #include "llvm/IR/PatternMatch.h" |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 85 | #include "llvm/IR/Type.h" |
| 86 | #include "llvm/Support/Allocator.h" |
| 87 | #include "llvm/Support/CommandLine.h" |
| 88 | #include "llvm/Support/Debug.h" |
| Daniel Berlin | 283a608 | 2017-03-01 19:59:26 +0000 | [diff] [blame] | 89 | #include "llvm/Support/DebugCounter.h" |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 90 | #include "llvm/Transforms/Scalar.h" |
| 91 | #include "llvm/Transforms/Scalar/GVNExpression.h" |
| 92 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 93 | #include "llvm/Transforms/Utils/Local.h" |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 94 | #include "llvm/Transforms/Utils/PredicateInfo.h" |
| Daniel Berlin | 07daac8 | 2017-04-02 13:23:44 +0000 | [diff] [blame] | 95 | #include "llvm/Transforms/Utils/VNCoercion.h" |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 96 | #include <numeric> |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 97 | #include <unordered_map> |
| 98 | #include <utility> |
| 99 | #include <vector> |
| 100 | using namespace llvm; |
| 101 | using namespace PatternMatch; |
| 102 | using namespace llvm::GVNExpression; |
| Daniel Berlin | 07daac8 | 2017-04-02 13:23:44 +0000 | [diff] [blame] | 103 | using namespace llvm::VNCoercion; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 104 | #define DEBUG_TYPE "newgvn" |
| 105 | |
| 106 | STATISTIC(NumGVNInstrDeleted, "Number of instructions deleted"); |
| 107 | STATISTIC(NumGVNBlocksDeleted, "Number of blocks deleted"); |
| 108 | STATISTIC(NumGVNOpsSimplified, "Number of Expressions simplified"); |
| 109 | STATISTIC(NumGVNPhisAllSame, "Number of PHIs whos arguments are all the same"); |
| Daniel Berlin | 0444343 | 2017-01-07 03:23:47 +0000 | [diff] [blame] | 110 | STATISTIC(NumGVNMaxIterations, |
| 111 | "Maximum Number of iterations it took to converge GVN"); |
| Daniel Berlin | c0431fd | 2017-01-13 22:40:01 +0000 | [diff] [blame] | 112 | STATISTIC(NumGVNLeaderChanges, "Number of leader changes"); |
| 113 | STATISTIC(NumGVNSortedLeaderChanges, "Number of sorted leader changes"); |
| 114 | STATISTIC(NumGVNAvoidedSortedLeaderChanges, |
| 115 | "Number of avoided sorted leader changes"); |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 116 | STATISTIC(NumGVNDeadStores, "Number of redundant/dead stores eliminated"); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 117 | STATISTIC(NumGVNPHIOfOpsCreated, "Number of PHI of ops created"); |
| 118 | STATISTIC(NumGVNPHIOfOpsEliminations, |
| 119 | "Number of things eliminated using PHI of ops"); |
| Daniel Berlin | 283a608 | 2017-03-01 19:59:26 +0000 | [diff] [blame] | 120 | DEBUG_COUNTER(VNCounter, "newgvn-vn", |
| Craig Topper | 9cd976d | 2017-08-10 17:48:11 +0000 | [diff] [blame] | 121 | "Controls which instructions are value numbered"); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 122 | DEBUG_COUNTER(PHIOfOpsCounter, "newgvn-phi", |
| Craig Topper | 9cd976d | 2017-08-10 17:48:11 +0000 | [diff] [blame] | 123 | "Controls which instructions we create phi of ops for"); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 124 | // Currently store defining access refinement is too slow due to basicaa being |
| 125 | // egregiously slow. This flag lets us keep it working while we work on this |
| 126 | // issue. |
| 127 | static cl::opt<bool> EnableStoreRefinement("enable-store-refinement", |
| 128 | cl::init(false), cl::Hidden); |
| 129 | |
| Chad Rosier | a5508e3 | 2017-08-10 14:12:57 +0000 | [diff] [blame] | 130 | /// Currently, the generation "phi of ops" can result in correctness issues. |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 131 | static cl::opt<bool> EnablePhiOfOps("enable-phi-of-ops", cl::init(true), |
| Chad Rosier | a5508e3 | 2017-08-10 14:12:57 +0000 | [diff] [blame] | 132 | cl::Hidden); |
| 133 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 134 | //===----------------------------------------------------------------------===// |
| 135 | // GVN Pass |
| 136 | //===----------------------------------------------------------------------===// |
| 137 | |
| 138 | // Anchor methods. |
| 139 | namespace llvm { |
| 140 | namespace GVNExpression { |
| Daniel Berlin | 85f91b0 | 2016-12-26 20:06:58 +0000 | [diff] [blame] | 141 | Expression::~Expression() = default; |
| 142 | BasicExpression::~BasicExpression() = default; |
| 143 | CallExpression::~CallExpression() = default; |
| 144 | LoadExpression::~LoadExpression() = default; |
| 145 | StoreExpression::~StoreExpression() = default; |
| 146 | AggregateValueExpression::~AggregateValueExpression() = default; |
| 147 | PHIExpression::~PHIExpression() = default; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| Benjamin Kramer | 49a49fe | 2017-08-20 13:03:48 +0000 | [diff] [blame] | 151 | namespace { |
| Daniel Berlin | 2f72b19 | 2017-04-14 02:53:37 +0000 | [diff] [blame] | 152 | // Tarjan's SCC finding algorithm with Nuutila's improvements |
| 153 | // SCCIterator is actually fairly complex for the simple thing we want. |
| 154 | // It also wants to hand us SCC's that are unrelated to the phi node we ask |
| 155 | // about, and have us process them there or risk redoing work. |
| 156 | // Graph traits over a filter iterator also doesn't work that well here. |
| Daniel Berlin | 9d0042b | 2017-04-18 20:15:47 +0000 | [diff] [blame] | 157 | // This SCC finder is specialized to walk use-def chains, and only follows |
| 158 | // instructions, |
| Daniel Berlin | 2f72b19 | 2017-04-14 02:53:37 +0000 | [diff] [blame] | 159 | // not generic values (arguments, etc). |
| 160 | struct TarjanSCC { |
| 161 | |
| 162 | TarjanSCC() : Components(1) {} |
| 163 | |
| 164 | void Start(const Instruction *Start) { |
| 165 | if (Root.lookup(Start) == 0) |
| 166 | FindSCC(Start); |
| 167 | } |
| 168 | |
| 169 | const SmallPtrSetImpl<const Value *> &getComponentFor(const Value *V) const { |
| 170 | unsigned ComponentID = ValueToComponent.lookup(V); |
| 171 | |
| 172 | assert(ComponentID > 0 && |
| 173 | "Asking for a component for a value we never processed"); |
| 174 | return Components[ComponentID]; |
| 175 | } |
| 176 | |
| 177 | private: |
| 178 | void FindSCC(const Instruction *I) { |
| 179 | Root[I] = ++DFSNum; |
| 180 | // Store the DFS Number we had before it possibly gets incremented. |
| 181 | unsigned int OurDFS = DFSNum; |
| 182 | for (auto &Op : I->operands()) { |
| 183 | if (auto *InstOp = dyn_cast<Instruction>(Op)) { |
| 184 | if (Root.lookup(Op) == 0) |
| 185 | FindSCC(InstOp); |
| 186 | if (!InComponent.count(Op)) |
| 187 | Root[I] = std::min(Root.lookup(I), Root.lookup(Op)); |
| 188 | } |
| 189 | } |
| Daniel Berlin | 9d0042b | 2017-04-18 20:15:47 +0000 | [diff] [blame] | 190 | // See if we really were the root of a component, by seeing if we still have |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 191 | // our DFSNumber. If we do, we are the root of the component, and we have |
| 192 | // completed a component. If we do not, we are not the root of a component, |
| 193 | // and belong on the component stack. |
| Daniel Berlin | 2f72b19 | 2017-04-14 02:53:37 +0000 | [diff] [blame] | 194 | if (Root.lookup(I) == OurDFS) { |
| 195 | unsigned ComponentID = Components.size(); |
| 196 | Components.resize(Components.size() + 1); |
| 197 | auto &Component = Components.back(); |
| 198 | Component.insert(I); |
| 199 | DEBUG(dbgs() << "Component root is " << *I << "\n"); |
| 200 | InComponent.insert(I); |
| 201 | ValueToComponent[I] = ComponentID; |
| 202 | // Pop a component off the stack and label it. |
| 203 | while (!Stack.empty() && Root.lookup(Stack.back()) >= OurDFS) { |
| 204 | auto *Member = Stack.back(); |
| 205 | DEBUG(dbgs() << "Component member is " << *Member << "\n"); |
| 206 | Component.insert(Member); |
| 207 | InComponent.insert(Member); |
| 208 | ValueToComponent[Member] = ComponentID; |
| 209 | Stack.pop_back(); |
| 210 | } |
| 211 | } else { |
| 212 | // Part of a component, push to stack |
| 213 | Stack.push_back(I); |
| 214 | } |
| 215 | } |
| 216 | unsigned int DFSNum = 1; |
| 217 | SmallPtrSet<const Value *, 8> InComponent; |
| 218 | DenseMap<const Value *, unsigned int> Root; |
| 219 | SmallVector<const Value *, 8> Stack; |
| 220 | // Store the components as vector of ptr sets, because we need the topo order |
| 221 | // of SCC's, but not individual member order |
| 222 | SmallVector<SmallPtrSet<const Value *, 8>, 8> Components; |
| 223 | DenseMap<const Value *, unsigned> ValueToComponent; |
| 224 | }; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 225 | // Congruence classes represent the set of expressions/instructions |
| 226 | // that are all the same *during some scope in the function*. |
| 227 | // That is, because of the way we perform equality propagation, and |
| 228 | // because of memory value numbering, it is not correct to assume |
| 229 | // you can willy-nilly replace any member with any other at any |
| 230 | // point in the function. |
| 231 | // |
| 232 | // For any Value in the Member set, it is valid to replace any dominated member |
| 233 | // with that Value. |
| 234 | // |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 235 | // Every congruence class has a leader, and the leader is used to symbolize |
| 236 | // instructions in a canonical way (IE every operand of an instruction that is a |
| 237 | // member of the same congruence class will always be replaced with leader |
| 238 | // during symbolization). To simplify symbolization, we keep the leader as a |
| 239 | // constant if class can be proved to be a constant value. Otherwise, the |
| 240 | // leader is the member of the value set with the smallest DFS number. Each |
| 241 | // congruence class also has a defining expression, though the expression may be |
| 242 | // null. If it exists, it can be used for forward propagation and reassociation |
| 243 | // of values. |
| 244 | |
| 245 | // For memory, we also track a representative MemoryAccess, and a set of memory |
| 246 | // members for MemoryPhis (which have no real instructions). Note that for |
| 247 | // memory, it seems tempting to try to split the memory members into a |
| 248 | // MemoryCongruenceClass or something. Unfortunately, this does not work |
| 249 | // easily. The value numbering of a given memory expression depends on the |
| 250 | // leader of the memory congruence class, and the leader of memory congruence |
| 251 | // class depends on the value numbering of a given memory expression. This |
| 252 | // leads to wasted propagation, and in some cases, missed optimization. For |
| 253 | // example: If we had value numbered two stores together before, but now do not, |
| 254 | // we move them to a new value congruence class. This in turn will move at one |
| 255 | // of the memorydefs to a new memory congruence class. Which in turn, affects |
| 256 | // the value numbering of the stores we just value numbered (because the memory |
| 257 | // congruence class is part of the value number). So while theoretically |
| 258 | // possible to split them up, it turns out to be *incredibly* complicated to get |
| 259 | // it to work right, because of the interdependency. While structurally |
| 260 | // slightly messier, it is algorithmically much simpler and faster to do what we |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 261 | // do here, and track them both at once in the same class. |
| 262 | // Note: The default iterators for this class iterate over values |
| 263 | class CongruenceClass { |
| 264 | public: |
| 265 | using MemberType = Value; |
| 266 | using MemberSet = SmallPtrSet<MemberType *, 4>; |
| 267 | using MemoryMemberType = MemoryPhi; |
| 268 | using MemoryMemberSet = SmallPtrSet<const MemoryMemberType *, 2>; |
| 269 | |
| 270 | explicit CongruenceClass(unsigned ID) : ID(ID) {} |
| 271 | CongruenceClass(unsigned ID, Value *Leader, const Expression *E) |
| 272 | : ID(ID), RepLeader(Leader), DefiningExpr(E) {} |
| 273 | unsigned getID() const { return ID; } |
| 274 | // True if this class has no members left. This is mainly used for assertion |
| 275 | // purposes, and for skipping empty classes. |
| 276 | bool isDead() const { |
| 277 | // If it's both dead from a value perspective, and dead from a memory |
| 278 | // perspective, it's really dead. |
| 279 | return empty() && memory_empty(); |
| 280 | } |
| 281 | // Leader functions |
| 282 | Value *getLeader() const { return RepLeader; } |
| 283 | void setLeader(Value *Leader) { RepLeader = Leader; } |
| 284 | const std::pair<Value *, unsigned int> &getNextLeader() const { |
| 285 | return NextLeader; |
| 286 | } |
| 287 | void resetNextLeader() { NextLeader = {nullptr, ~0}; } |
| 288 | |
| 289 | void addPossibleNextLeader(std::pair<Value *, unsigned int> LeaderPair) { |
| 290 | if (LeaderPair.second < NextLeader.second) |
| 291 | NextLeader = LeaderPair; |
| 292 | } |
| 293 | |
| 294 | Value *getStoredValue() const { return RepStoredValue; } |
| 295 | void setStoredValue(Value *Leader) { RepStoredValue = Leader; } |
| 296 | const MemoryAccess *getMemoryLeader() const { return RepMemoryAccess; } |
| 297 | void setMemoryLeader(const MemoryAccess *Leader) { RepMemoryAccess = Leader; } |
| 298 | |
| 299 | // Forward propagation info |
| 300 | const Expression *getDefiningExpr() const { return DefiningExpr; } |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 301 | |
| 302 | // Value member set |
| 303 | bool empty() const { return Members.empty(); } |
| 304 | unsigned size() const { return Members.size(); } |
| 305 | MemberSet::const_iterator begin() const { return Members.begin(); } |
| 306 | MemberSet::const_iterator end() const { return Members.end(); } |
| 307 | void insert(MemberType *M) { Members.insert(M); } |
| 308 | void erase(MemberType *M) { Members.erase(M); } |
| 309 | void swap(MemberSet &Other) { Members.swap(Other); } |
| 310 | |
| 311 | // Memory member set |
| 312 | bool memory_empty() const { return MemoryMembers.empty(); } |
| 313 | unsigned memory_size() const { return MemoryMembers.size(); } |
| 314 | MemoryMemberSet::const_iterator memory_begin() const { |
| 315 | return MemoryMembers.begin(); |
| 316 | } |
| 317 | MemoryMemberSet::const_iterator memory_end() const { |
| 318 | return MemoryMembers.end(); |
| 319 | } |
| 320 | iterator_range<MemoryMemberSet::const_iterator> memory() const { |
| 321 | return make_range(memory_begin(), memory_end()); |
| 322 | } |
| 323 | void memory_insert(const MemoryMemberType *M) { MemoryMembers.insert(M); } |
| 324 | void memory_erase(const MemoryMemberType *M) { MemoryMembers.erase(M); } |
| 325 | |
| 326 | // Store count |
| 327 | unsigned getStoreCount() const { return StoreCount; } |
| 328 | void incStoreCount() { ++StoreCount; } |
| 329 | void decStoreCount() { |
| 330 | assert(StoreCount != 0 && "Store count went negative"); |
| 331 | --StoreCount; |
| 332 | } |
| 333 | |
| Davide Italiano | dc43532 | 2017-05-10 19:57:43 +0000 | [diff] [blame] | 334 | // True if this class has no memory members. |
| 335 | bool definesNoMemory() const { return StoreCount == 0 && memory_empty(); } |
| 336 | |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 337 | // Return true if two congruence classes are equivalent to each other. This |
| 338 | // means |
| 339 | // that every field but the ID number and the dead field are equivalent. |
| 340 | bool isEquivalentTo(const CongruenceClass *Other) const { |
| 341 | if (!Other) |
| 342 | return false; |
| 343 | if (this == Other) |
| 344 | return true; |
| 345 | |
| 346 | if (std::tie(StoreCount, RepLeader, RepStoredValue, RepMemoryAccess) != |
| 347 | std::tie(Other->StoreCount, Other->RepLeader, Other->RepStoredValue, |
| 348 | Other->RepMemoryAccess)) |
| 349 | return false; |
| 350 | if (DefiningExpr != Other->DefiningExpr) |
| 351 | if (!DefiningExpr || !Other->DefiningExpr || |
| 352 | *DefiningExpr != *Other->DefiningExpr) |
| 353 | return false; |
| 354 | // We need some ordered set |
| 355 | std::set<Value *> AMembers(Members.begin(), Members.end()); |
| 356 | std::set<Value *> BMembers(Members.begin(), Members.end()); |
| 357 | return AMembers == BMembers; |
| 358 | } |
| 359 | |
| 360 | private: |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 361 | unsigned ID; |
| 362 | // Representative leader. |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 363 | Value *RepLeader = nullptr; |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 364 | // The most dominating leader after our current leader, because the member set |
| 365 | // is not sorted and is expensive to keep sorted all the time. |
| 366 | std::pair<Value *, unsigned int> NextLeader = {nullptr, ~0U}; |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 367 | // If this is represented by a store, the value of the store. |
| Daniel Berlin | 26addef | 2017-01-20 21:04:30 +0000 | [diff] [blame] | 368 | Value *RepStoredValue = nullptr; |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 369 | // If this class contains MemoryDefs or MemoryPhis, this is the leading memory |
| 370 | // access. |
| 371 | const MemoryAccess *RepMemoryAccess = nullptr; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 372 | // Defining Expression. |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 373 | const Expression *DefiningExpr = nullptr; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 374 | // Actual members of this class. |
| 375 | MemberSet Members; |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 376 | // This is the set of MemoryPhis that exist in the class. MemoryDefs and |
| 377 | // MemoryUses have real instructions representing them, so we only need to |
| 378 | // track MemoryPhis here. |
| 379 | MemoryMemberSet MemoryMembers; |
| Daniel Berlin | f6eba4b | 2017-01-11 20:22:36 +0000 | [diff] [blame] | 380 | // Number of stores in this congruence class. |
| 381 | // This is used so we can detect store equivalence changes properly. |
| Davide Italiano | eac05f6 | 2017-01-11 23:41:24 +0000 | [diff] [blame] | 382 | int StoreCount = 0; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 383 | }; |
| Benjamin Kramer | 49a49fe | 2017-08-20 13:03:48 +0000 | [diff] [blame] | 384 | } // namespace |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 385 | |
| 386 | namespace llvm { |
| Daniel Berlin | eafdd86 | 2017-06-06 17:15:28 +0000 | [diff] [blame] | 387 | struct ExactEqualsExpression { |
| 388 | const Expression &E; |
| 389 | explicit ExactEqualsExpression(const Expression &E) : E(E) {} |
| 390 | hash_code getComputedHash() const { return E.getComputedHash(); } |
| 391 | bool operator==(const Expression &Other) const { |
| 392 | return E.exactlyEquals(Other); |
| 393 | } |
| 394 | }; |
| 395 | |
| Daniel Berlin | 85f91b0 | 2016-12-26 20:06:58 +0000 | [diff] [blame] | 396 | template <> struct DenseMapInfo<const Expression *> { |
| 397 | static const Expression *getEmptyKey() { |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 398 | auto Val = static_cast<uintptr_t>(-1); |
| Daniel Berlin | 85f91b0 | 2016-12-26 20:06:58 +0000 | [diff] [blame] | 399 | Val <<= PointerLikeTypeTraits<const Expression *>::NumLowBitsAvailable; |
| 400 | return reinterpret_cast<const Expression *>(Val); |
| 401 | } |
| 402 | static const Expression *getTombstoneKey() { |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 403 | auto Val = static_cast<uintptr_t>(~1U); |
| Daniel Berlin | 85f91b0 | 2016-12-26 20:06:58 +0000 | [diff] [blame] | 404 | Val <<= PointerLikeTypeTraits<const Expression *>::NumLowBitsAvailable; |
| 405 | return reinterpret_cast<const Expression *>(Val); |
| 406 | } |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 407 | static unsigned getHashValue(const Expression *E) { |
| Daniel Berlin | eafdd86 | 2017-06-06 17:15:28 +0000 | [diff] [blame] | 408 | return E->getComputedHash(); |
| Daniel Berlin | 85f91b0 | 2016-12-26 20:06:58 +0000 | [diff] [blame] | 409 | } |
| Daniel Berlin | eafdd86 | 2017-06-06 17:15:28 +0000 | [diff] [blame] | 410 | static unsigned getHashValue(const ExactEqualsExpression &E) { |
| 411 | return E.getComputedHash(); |
| 412 | } |
| 413 | static bool isEqual(const ExactEqualsExpression &LHS, const Expression *RHS) { |
| 414 | if (RHS == getTombstoneKey() || RHS == getEmptyKey()) |
| 415 | return false; |
| 416 | return LHS == *RHS; |
| 417 | } |
| 418 | |
| Daniel Berlin | 85f91b0 | 2016-12-26 20:06:58 +0000 | [diff] [blame] | 419 | static bool isEqual(const Expression *LHS, const Expression *RHS) { |
| 420 | if (LHS == RHS) |
| 421 | return true; |
| 422 | if (LHS == getTombstoneKey() || RHS == getTombstoneKey() || |
| 423 | LHS == getEmptyKey() || RHS == getEmptyKey()) |
| 424 | return false; |
| Daniel Berlin | 2aa5dc1 | 2017-05-30 06:58:18 +0000 | [diff] [blame] | 425 | // Compare hashes before equality. This is *not* what the hashtable does, |
| 426 | // since it is computing it modulo the number of buckets, whereas we are |
| 427 | // using the full hash keyspace. Since the hashes are precomputed, this |
| 428 | // check is *much* faster than equality. |
| 429 | if (LHS->getComputedHash() != RHS->getComputedHash()) |
| 430 | return false; |
| Daniel Berlin | 85f91b0 | 2016-12-26 20:06:58 +0000 | [diff] [blame] | 431 | return *LHS == *RHS; |
| 432 | } |
| 433 | }; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 434 | } // end namespace llvm |
| 435 | |
| Benjamin Kramer | efcf06f | 2017-02-11 11:06:55 +0000 | [diff] [blame] | 436 | namespace { |
| Daniel Berlin | 64e6899 | 2017-03-12 04:46:45 +0000 | [diff] [blame] | 437 | class NewGVN { |
| 438 | Function &F; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 439 | DominatorTree *DT; |
| Daniel Berlin | 64e6899 | 2017-03-12 04:46:45 +0000 | [diff] [blame] | 440 | const TargetLibraryInfo *TLI; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 441 | AliasAnalysis *AA; |
| 442 | MemorySSA *MSSA; |
| 443 | MemorySSAWalker *MSSAWalker; |
| Daniel Berlin | 64e6899 | 2017-03-12 04:46:45 +0000 | [diff] [blame] | 444 | const DataLayout &DL; |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 445 | std::unique_ptr<PredicateInfo> PredInfo; |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 446 | |
| 447 | // These are the only two things the create* functions should have |
| 448 | // side-effects on due to allocating memory. |
| 449 | mutable BumpPtrAllocator ExpressionAllocator; |
| 450 | mutable ArrayRecycler<Value *> ArgRecycler; |
| 451 | mutable TarjanSCC SCCFinder; |
| Daniel Berlin | ede130d | 2017-04-26 20:56:14 +0000 | [diff] [blame] | 452 | const SimplifyQuery SQ; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 453 | |
| Daniel Berlin | 1c08767 | 2017-02-11 15:07:01 +0000 | [diff] [blame] | 454 | // Number of function arguments, used by ranking |
| 455 | unsigned int NumFuncArgs; |
| 456 | |
| Daniel Berlin | 2f72b19 | 2017-04-14 02:53:37 +0000 | [diff] [blame] | 457 | // RPOOrdering of basic blocks |
| 458 | DenseMap<const DomTreeNode *, unsigned> RPOOrdering; |
| 459 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 460 | // Congruence class info. |
| Daniel Berlin | b79f536 | 2017-02-11 12:48:50 +0000 | [diff] [blame] | 461 | |
| 462 | // This class is called INITIAL in the paper. It is the class everything |
| 463 | // startsout in, and represents any value. Being an optimistic analysis, |
| Daniel Berlin | 5c338ff | 2017-03-10 19:05:04 +0000 | [diff] [blame] | 464 | // anything in the TOP class has the value TOP, which is indeterminate and |
| Daniel Berlin | b79f536 | 2017-02-11 12:48:50 +0000 | [diff] [blame] | 465 | // equivalent to everything. |
| Daniel Berlin | 5c338ff | 2017-03-10 19:05:04 +0000 | [diff] [blame] | 466 | CongruenceClass *TOPClass; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 467 | std::vector<CongruenceClass *> CongruenceClasses; |
| 468 | unsigned NextCongruenceNum; |
| 469 | |
| 470 | // Value Mappings. |
| 471 | DenseMap<Value *, CongruenceClass *> ValueToClass; |
| 472 | DenseMap<Value *, const Expression *> ValueToExpression; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 473 | // Value PHI handling, used to make equivalence between phi(op, op) and |
| 474 | // op(phi, phi). |
| 475 | // These mappings just store various data that would normally be part of the |
| 476 | // IR. |
| 477 | DenseSet<const Instruction *> PHINodeUses; |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 478 | DenseMap<const Value *, bool> OpSafeForPHIOfOps; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 479 | // Map a temporary instruction we created to a parent block. |
| 480 | DenseMap<const Value *, BasicBlock *> TempToBlock; |
| Davide Italiano | 5974c31 | 2017-08-03 21:17:49 +0000 | [diff] [blame] | 481 | // Map between the already in-program instructions and the temporary phis we |
| 482 | // created that they are known equivalent to. |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 483 | DenseMap<const Value *, PHINode *> RealToTemp; |
| 484 | // In order to know when we should re-process instructions that have |
| 485 | // phi-of-ops, we track the set of expressions that they needed as |
| 486 | // leaders. When we discover new leaders for those expressions, we process the |
| 487 | // associated phi-of-op instructions again in case they have changed. The |
| 488 | // other way they may change is if they had leaders, and those leaders |
| 489 | // disappear. However, at the point they have leaders, there are uses of the |
| 490 | // relevant operands in the created phi node, and so they will get reprocessed |
| 491 | // through the normal user marking we perform. |
| 492 | mutable DenseMap<const Value *, SmallPtrSet<Value *, 2>> AdditionalUsers; |
| 493 | DenseMap<const Expression *, SmallPtrSet<Instruction *, 2>> |
| 494 | ExpressionToPhiOfOps; |
| 495 | // Map from basic block to the temporary operations we created |
| Davide Italiano | 5974c31 | 2017-08-03 21:17:49 +0000 | [diff] [blame] | 496 | DenseMap<const BasicBlock *, SmallPtrSet<PHINode *, 2>> PHIOfOpsPHIs; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 497 | // Map from temporary operation to MemoryAccess. |
| 498 | DenseMap<const Instruction *, MemoryUseOrDef *> TempToMemory; |
| 499 | // Set of all temporary instructions we created. |
| Davide Italiano | 5974c31 | 2017-08-03 21:17:49 +0000 | [diff] [blame] | 500 | // Note: This will include instructions that were just created during value |
| 501 | // numbering. The way to test if something is using them is to check |
| 502 | // RealToTemp. |
| 503 | |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 504 | DenseSet<Instruction *> AllTempInstructions; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 505 | |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 506 | // Mapping from predicate info we used to the instructions we used it with. |
| 507 | // In order to correctly ensure propagation, we must keep track of what |
| 508 | // comparisons we used, so that when the values of the comparisons change, we |
| 509 | // propagate the information to the places we used the comparison. |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 510 | mutable DenseMap<const Value *, SmallPtrSet<Instruction *, 2>> |
| 511 | PredicateToUsers; |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 512 | // the same reasoning as PredicateToUsers. When we skip MemoryAccesses for |
| 513 | // stores, we no longer can rely solely on the def-use chains of MemorySSA. |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 514 | mutable DenseMap<const MemoryAccess *, SmallPtrSet<MemoryAccess *, 2>> |
| 515 | MemoryToUsers; |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 516 | |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 517 | // A table storing which memorydefs/phis represent a memory state provably |
| 518 | // equivalent to another memory state. |
| 519 | // We could use the congruence class machinery, but the MemoryAccess's are |
| 520 | // abstract memory states, so they can only ever be equivalent to each other, |
| 521 | // and not to constants, etc. |
| Daniel Berlin | 1ea5f32 | 2017-01-26 22:21:48 +0000 | [diff] [blame] | 522 | DenseMap<const MemoryAccess *, CongruenceClass *> MemoryAccessToClass; |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 523 | |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 524 | // We could, if we wanted, build MemoryPhiExpressions and |
| 525 | // MemoryVariableExpressions, etc, and value number them the same way we value |
| 526 | // number phi expressions. For the moment, this seems like overkill. They |
| 527 | // can only exist in one of three states: they can be TOP (equal to |
| 528 | // everything), Equivalent to something else, or unique. Because we do not |
| 529 | // create expressions for them, we need to simulate leader change not just |
| 530 | // when they change class, but when they change state. Note: We can do the |
| 531 | // same thing for phis, and avoid having phi expressions if we wanted, We |
| 532 | // should eventually unify in one direction or the other, so this is a little |
| 533 | // bit of an experiment in which turns out easier to maintain. |
| 534 | enum MemoryPhiState { MPS_Invalid, MPS_TOP, MPS_Equivalent, MPS_Unique }; |
| 535 | DenseMap<const MemoryPhi *, MemoryPhiState> MemoryPhiState; |
| 536 | |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 537 | enum InstCycleState { ICS_Unknown, ICS_CycleFree, ICS_Cycle }; |
| 538 | mutable DenseMap<const Instruction *, InstCycleState> InstCycleState; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 539 | // Expression to class mapping. |
| Piotr Padlewski | e4047b8 | 2016-12-28 19:29:26 +0000 | [diff] [blame] | 540 | using ExpressionClassMap = DenseMap<const Expression *, CongruenceClass *>; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 541 | ExpressionClassMap ExpressionToClass; |
| 542 | |
| Daniel Berlin | e021d2d | 2017-05-19 20:22:20 +0000 | [diff] [blame] | 543 | // We have a single expression that represents currently DeadExpressions. |
| 544 | // For dead expressions we can prove will stay dead, we mark them with |
| 545 | // DFS number zero. However, it's possible in the case of phi nodes |
| 546 | // for us to assume/prove all arguments are dead during fixpointing. |
| 547 | // We use DeadExpression for that case. |
| 548 | DeadExpression *SingletonDeadExpression = nullptr; |
| 549 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 550 | // Which values have changed as a result of leader changes. |
| Daniel Berlin | 3a1bd02 | 2017-01-11 20:22:05 +0000 | [diff] [blame] | 551 | SmallPtrSet<Value *, 8> LeaderChanges; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 552 | |
| 553 | // Reachability info. |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 554 | using BlockEdge = BasicBlockEdge; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 555 | DenseSet<BlockEdge> ReachableEdges; |
| 556 | SmallPtrSet<const BasicBlock *, 8> ReachableBlocks; |
| 557 | |
| 558 | // This is a bitvector because, on larger functions, we may have |
| 559 | // thousands of touched instructions at once (entire blocks, |
| 560 | // instructions with hundreds of uses, etc). Even with optimization |
| 561 | // for when we mark whole blocks as touched, when this was a |
| 562 | // SmallPtrSet or DenseSet, for some functions, we spent >20% of all |
| 563 | // the time in GVN just managing this list. The bitvector, on the |
| 564 | // other hand, efficiently supports test/set/clear of both |
| 565 | // individual and ranges, as well as "find next element" This |
| 566 | // enables us to use it as a worklist with essentially 0 cost. |
| 567 | BitVector TouchedInstructions; |
| 568 | |
| 569 | DenseMap<const BasicBlock *, std::pair<unsigned, unsigned>> BlockInstRange; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 570 | |
| 571 | #ifndef NDEBUG |
| 572 | // Debugging for how many times each block and instruction got processed. |
| 573 | DenseMap<const Value *, unsigned> ProcessedCount; |
| 574 | #endif |
| 575 | |
| 576 | // DFS info. |
| Davide Italiano | 71f2d9c | 2017-01-20 23:29:28 +0000 | [diff] [blame] | 577 | // This contains a mapping from Instructions to DFS numbers. |
| 578 | // The numbering starts at 1. An instruction with DFS number zero |
| 579 | // means that the instruction is dead. |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 580 | DenseMap<const Value *, unsigned> InstrDFS; |
| Davide Italiano | 71f2d9c | 2017-01-20 23:29:28 +0000 | [diff] [blame] | 581 | |
| 582 | // This contains the mapping DFS numbers to instructions. |
| Daniel Berlin | 1f31fe52 | 2016-12-27 09:20:36 +0000 | [diff] [blame] | 583 | SmallVector<Value *, 32> DFSToInstr; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 584 | |
| 585 | // Deletion info. |
| 586 | SmallPtrSet<Instruction *, 8> InstructionsToErase; |
| 587 | |
| 588 | public: |
| Daniel Berlin | 64e6899 | 2017-03-12 04:46:45 +0000 | [diff] [blame] | 589 | NewGVN(Function &F, DominatorTree *DT, AssumptionCache *AC, |
| 590 | TargetLibraryInfo *TLI, AliasAnalysis *AA, MemorySSA *MSSA, |
| 591 | const DataLayout &DL) |
| Daniel Berlin | 4d0fe64 | 2017-04-28 19:55:38 +0000 | [diff] [blame] | 592 | : F(F), DT(DT), TLI(TLI), AA(AA), MSSA(MSSA), DL(DL), |
| Daniel Berlin | ede130d | 2017-04-26 20:56:14 +0000 | [diff] [blame] | 593 | PredInfo(make_unique<PredicateInfo>(F, *DT, *AC)), SQ(DL, TLI, DT, AC) { |
| 594 | } |
| Daniel Berlin | 64e6899 | 2017-03-12 04:46:45 +0000 | [diff] [blame] | 595 | bool runGVN(); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 596 | |
| 597 | private: |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 598 | // Expression handling. |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 599 | const Expression *createExpression(Instruction *) const; |
| Daniel Berlin | 54a92fc | 2017-09-05 02:17:42 +0000 | [diff] [blame] | 600 | const Expression *createBinaryExpression(unsigned, Type *, Value *, Value *, |
| 601 | Instruction *) const; |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 602 | PHIExpression *createPHIExpression(Instruction *, bool &HasBackEdge, |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 603 | bool &OriginalOpsConstant) const; |
| Daniel Berlin | e021d2d | 2017-05-19 20:22:20 +0000 | [diff] [blame] | 604 | const DeadExpression *createDeadExpression() const; |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 605 | const VariableExpression *createVariableExpression(Value *) const; |
| 606 | const ConstantExpression *createConstantExpression(Constant *) const; |
| 607 | const Expression *createVariableOrConstant(Value *V) const; |
| 608 | const UnknownExpression *createUnknownExpression(Instruction *) const; |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 609 | const StoreExpression *createStoreExpression(StoreInst *, |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 610 | const MemoryAccess *) const; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 611 | LoadExpression *createLoadExpression(Type *, Value *, LoadInst *, |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 612 | const MemoryAccess *) const; |
| 613 | const CallExpression *createCallExpression(CallInst *, |
| 614 | const MemoryAccess *) const; |
| 615 | const AggregateValueExpression * |
| 616 | createAggregateValueExpression(Instruction *) const; |
| 617 | bool setBasicExpressionInfo(Instruction *, BasicExpression *) const; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 618 | |
| 619 | // Congruence class handling. |
| 620 | CongruenceClass *createCongruenceClass(Value *Leader, const Expression *E) { |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 621 | auto *result = new CongruenceClass(NextCongruenceNum++, Leader, E); |
| Piotr Padlewski | 6c37d29 | 2016-12-28 23:24:02 +0000 | [diff] [blame] | 622 | CongruenceClasses.emplace_back(result); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 623 | return result; |
| 624 | } |
| 625 | |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 626 | CongruenceClass *createMemoryClass(MemoryAccess *MA) { |
| 627 | auto *CC = createCongruenceClass(nullptr, nullptr); |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 628 | CC->setMemoryLeader(MA); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 629 | return CC; |
| 630 | } |
| 631 | CongruenceClass *ensureLeaderOfMemoryClass(MemoryAccess *MA) { |
| 632 | auto *CC = getMemoryClass(MA); |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 633 | if (CC->getMemoryLeader() != MA) |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 634 | CC = createMemoryClass(MA); |
| 635 | return CC; |
| 636 | } |
| 637 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 638 | CongruenceClass *createSingletonCongruenceClass(Value *Member) { |
| Davide Italiano | 0e71480 | 2016-12-28 14:00:11 +0000 | [diff] [blame] | 639 | CongruenceClass *CClass = createCongruenceClass(Member, nullptr); |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 640 | CClass->insert(Member); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 641 | ValueToClass[Member] = CClass; |
| 642 | return CClass; |
| 643 | } |
| 644 | void initializeCongruenceClasses(Function &F); |
| Daniel Berlin | be3e7ba | 2017-05-31 01:47:32 +0000 | [diff] [blame] | 645 | const Expression *makePossiblePhiOfOps(Instruction *, |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 646 | SmallPtrSetImpl<Value *> &); |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 647 | Value *findLeaderForInst(Instruction *ValueOp, |
| 648 | SmallPtrSetImpl<Value *> &Visited, |
| 649 | MemoryAccess *MemAccess, Instruction *OrigInst, |
| 650 | BasicBlock *PredBB); |
| 651 | |
| 652 | bool OpIsSafeForPHIOfOps(Value *Op, Instruction *OrigInst, |
| 653 | const BasicBlock *PHIBlock, |
| 654 | SmallPtrSetImpl<const Value *> &); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 655 | void addPhiOfOps(PHINode *Op, BasicBlock *BB, Instruction *ExistingValue); |
| Davide Italiano | 5974c31 | 2017-08-03 21:17:49 +0000 | [diff] [blame] | 656 | void removePhiOfOps(Instruction *I, PHINode *PHITemp); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 657 | |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 658 | // Value number an Instruction or MemoryPhi. |
| 659 | void valueNumberMemoryPhi(MemoryPhi *); |
| 660 | void valueNumberInstruction(Instruction *); |
| 661 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 662 | // Symbolic evaluation. |
| 663 | const Expression *checkSimplificationResults(Expression *, Instruction *, |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 664 | Value *) const; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 665 | const Expression *performSymbolicEvaluation(Value *, |
| 666 | SmallPtrSetImpl<Value *> &) const; |
| Daniel Berlin | 07daac8 | 2017-04-02 13:23:44 +0000 | [diff] [blame] | 667 | const Expression *performSymbolicLoadCoercion(Type *, Value *, LoadInst *, |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 668 | Instruction *, |
| 669 | MemoryAccess *) const; |
| 670 | const Expression *performSymbolicLoadEvaluation(Instruction *) const; |
| 671 | const Expression *performSymbolicStoreEvaluation(Instruction *) const; |
| 672 | const Expression *performSymbolicCallEvaluation(Instruction *) const; |
| 673 | const Expression *performSymbolicPHIEvaluation(Instruction *) const; |
| 674 | const Expression *performSymbolicAggrValueEvaluation(Instruction *) const; |
| 675 | const Expression *performSymbolicCmpEvaluation(Instruction *) const; |
| 676 | const Expression *performSymbolicPredicateInfoEvaluation(Instruction *) const; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 677 | |
| 678 | // Congruence finding. |
| Daniel Berlin | 9d0796e | 2017-03-24 05:30:34 +0000 | [diff] [blame] | 679 | bool someEquivalentDominates(const Instruction *, const Instruction *) const; |
| Daniel Berlin | 203f47b | 2017-01-31 22:31:53 +0000 | [diff] [blame] | 680 | Value *lookupOperandLeader(Value *) const; |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 681 | CongruenceClass *getClassForExpression(const Expression *E) const; |
| Daniel Berlin | c0431fd | 2017-01-13 22:40:01 +0000 | [diff] [blame] | 682 | void performCongruenceFinding(Instruction *, const Expression *); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 683 | void moveValueToNewCongruenceClass(Instruction *, const Expression *, |
| 684 | CongruenceClass *, CongruenceClass *); |
| 685 | void moveMemoryToNewCongruenceClass(Instruction *, MemoryAccess *, |
| 686 | CongruenceClass *, CongruenceClass *); |
| 687 | Value *getNextValueLeader(CongruenceClass *) const; |
| 688 | const MemoryAccess *getNextMemoryLeader(CongruenceClass *) const; |
| 689 | bool setMemoryClass(const MemoryAccess *From, CongruenceClass *To); |
| 690 | CongruenceClass *getMemoryClass(const MemoryAccess *MA) const; |
| 691 | const MemoryAccess *lookupMemoryLeader(const MemoryAccess *) const; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 692 | bool isMemoryAccessTOP(const MemoryAccess *) const; |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 693 | |
| Daniel Berlin | 1c08767 | 2017-02-11 15:07:01 +0000 | [diff] [blame] | 694 | // Ranking |
| 695 | unsigned int getRank(const Value *) const; |
| 696 | bool shouldSwapOperands(const Value *, const Value *) const; |
| 697 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 698 | // Reachability handling. |
| 699 | void updateReachableEdge(BasicBlock *, BasicBlock *); |
| 700 | void processOutgoingEdges(TerminatorInst *, BasicBlock *); |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 701 | Value *findConditionEquivalence(Value *) const; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 702 | |
| 703 | // Elimination. |
| 704 | struct ValueDFS; |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 705 | void convertClassToDFSOrdered(const CongruenceClass &, |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 706 | SmallVectorImpl<ValueDFS> &, |
| 707 | DenseMap<const Value *, unsigned int> &, |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 708 | SmallPtrSetImpl<Instruction *> &) const; |
| 709 | void convertClassToLoadsAndStores(const CongruenceClass &, |
| 710 | SmallVectorImpl<ValueDFS> &) const; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 711 | |
| 712 | bool eliminateInstructions(Function &); |
| 713 | void replaceInstruction(Instruction *, Value *); |
| 714 | void markInstructionForDeletion(Instruction *); |
| 715 | void deleteInstructionsInBlock(BasicBlock *); |
| Daniel Berlin | 4ad7e8d | 2017-09-05 02:17:40 +0000 | [diff] [blame] | 716 | Value *findPHIOfOpsLeader(const Expression *, const Instruction *, |
| 717 | const BasicBlock *) const; |
| 718 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 719 | // New instruction creation. |
| 720 | void handleNewInstruction(Instruction *){}; |
| Daniel Berlin | 32f8d56 | 2017-01-07 16:55:14 +0000 | [diff] [blame] | 721 | |
| 722 | // Various instruction touch utilities |
| Daniel Berlin | 0207cca | 2017-05-21 23:41:56 +0000 | [diff] [blame] | 723 | template <typename Map, typename KeyType, typename Func> |
| 724 | void for_each_found(Map &, const KeyType &, Func); |
| 725 | template <typename Map, typename KeyType> |
| 726 | void touchAndErase(Map &, const KeyType &); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 727 | void markUsersTouched(Value *); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 728 | void markMemoryUsersTouched(const MemoryAccess *); |
| 729 | void markMemoryDefTouched(const MemoryAccess *); |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 730 | void markPredicateUsersTouched(Instruction *); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 731 | void markValueLeaderChangeTouched(CongruenceClass *CC); |
| 732 | void markMemoryLeaderChangeTouched(CongruenceClass *CC); |
| Daniel Berlin | 2aa5dc1 | 2017-05-30 06:58:18 +0000 | [diff] [blame] | 733 | void markPhiOfOpsChanged(const Expression *E); |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 734 | void addPredicateUsers(const PredicateBase *, Instruction *) const; |
| 735 | void addMemoryUsers(const MemoryAccess *To, MemoryAccess *U) const; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 736 | void addAdditionalUsers(Value *To, Value *User) const; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 737 | |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 738 | // Main loop of value numbering |
| 739 | void iterateTouchedInstructions(); |
| 740 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 741 | // Utilities. |
| 742 | void cleanupTables(); |
| 743 | std::pair<unsigned, unsigned> assignDFSNumbers(BasicBlock *, unsigned); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 744 | void updateProcessedCount(const Value *V); |
| Daniel Berlin | f6eba4b | 2017-01-11 20:22:36 +0000 | [diff] [blame] | 745 | void verifyMemoryCongruency() const; |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 746 | void verifyIterationSettled(Function &F); |
| Daniel Berlin | 4540357 | 2017-05-16 19:58:47 +0000 | [diff] [blame] | 747 | void verifyStoreExpressions() const; |
| Davide Italiano | eab0de2 | 2017-05-18 23:22:44 +0000 | [diff] [blame] | 748 | bool singleReachablePHIPath(SmallPtrSet<const MemoryAccess *, 8> &, |
| 749 | const MemoryAccess *, const MemoryAccess *) const; |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 750 | BasicBlock *getBlockForValue(Value *V) const; |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 751 | void deleteExpression(const Expression *E) const; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 752 | MemoryUseOrDef *getMemoryAccess(const Instruction *) const; |
| 753 | MemoryAccess *getDefiningAccess(const MemoryAccess *) const; |
| 754 | MemoryPhi *getMemoryAccess(const BasicBlock *) const; |
| 755 | template <class T, class Range> T *getMinDFSOfRange(const Range &) const; |
| Daniel Berlin | 21279bd | 2017-04-06 18:52:58 +0000 | [diff] [blame] | 756 | unsigned InstrToDFSNum(const Value *V) const { |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 757 | assert(isa<Instruction>(V) && "This should not be used for MemoryAccesses"); |
| 758 | return InstrDFS.lookup(V); |
| 759 | } |
| 760 | |
| Daniel Berlin | 21279bd | 2017-04-06 18:52:58 +0000 | [diff] [blame] | 761 | unsigned InstrToDFSNum(const MemoryAccess *MA) const { |
| 762 | return MemoryToDFSNum(MA); |
| 763 | } |
| 764 | Value *InstrFromDFSNum(unsigned DFSNum) { return DFSToInstr[DFSNum]; } |
| 765 | // Given a MemoryAccess, return the relevant instruction DFS number. Note: |
| 766 | // This deliberately takes a value so it can be used with Use's, which will |
| 767 | // auto-convert to Value's but not to MemoryAccess's. |
| 768 | unsigned MemoryToDFSNum(const Value *MA) const { |
| 769 | assert(isa<MemoryAccess>(MA) && |
| 770 | "This should not be used with instructions"); |
| 771 | return isa<MemoryUseOrDef>(MA) |
| 772 | ? InstrToDFSNum(cast<MemoryUseOrDef>(MA)->getMemoryInst()) |
| 773 | : InstrDFS.lookup(MA); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 774 | } |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 775 | bool isCycleFree(const Instruction *) const; |
| 776 | bool isBackedge(BasicBlock *From, BasicBlock *To) const; |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 777 | // Debug counter info. When verifying, we have to reset the value numbering |
| 778 | // debug counter to the same state it started in to get the same results. |
| 779 | std::pair<int, int> StartingVNCounter; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 780 | }; |
| Benjamin Kramer | efcf06f | 2017-02-11 11:06:55 +0000 | [diff] [blame] | 781 | } // end anonymous namespace |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 782 | |
| Davide Italiano | b111409 | 2016-12-28 13:37:17 +0000 | [diff] [blame] | 783 | template <typename T> |
| 784 | static bool equalsLoadStoreHelper(const T &LHS, const Expression &RHS) { |
| Daniel Berlin | 9b49849 | 2017-04-01 09:44:29 +0000 | [diff] [blame] | 785 | if (!isa<LoadExpression>(RHS) && !isa<StoreExpression>(RHS)) |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 786 | return false; |
| Daniel Berlin | 9b49849 | 2017-04-01 09:44:29 +0000 | [diff] [blame] | 787 | return LHS.MemoryExpression::equals(RHS); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| Davide Italiano | b111409 | 2016-12-28 13:37:17 +0000 | [diff] [blame] | 790 | bool LoadExpression::equals(const Expression &Other) const { |
| 791 | return equalsLoadStoreHelper(*this, Other); |
| 792 | } |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 793 | |
| Davide Italiano | b111409 | 2016-12-28 13:37:17 +0000 | [diff] [blame] | 794 | bool StoreExpression::equals(const Expression &Other) const { |
| Daniel Berlin | 9b49849 | 2017-04-01 09:44:29 +0000 | [diff] [blame] | 795 | if (!equalsLoadStoreHelper(*this, Other)) |
| 796 | return false; |
| Daniel Berlin | 26addef | 2017-01-20 21:04:30 +0000 | [diff] [blame] | 797 | // Make sure that store vs store includes the value operand. |
| Daniel Berlin | 9b49849 | 2017-04-01 09:44:29 +0000 | [diff] [blame] | 798 | if (const auto *S = dyn_cast<StoreExpression>(&Other)) |
| 799 | if (getStoredValue() != S->getStoredValue()) |
| 800 | return false; |
| 801 | return true; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 804 | // Determine if the edge From->To is a backedge |
| 805 | bool NewGVN::isBackedge(BasicBlock *From, BasicBlock *To) const { |
| Davide Italiano | c2f73b7 | 2017-08-02 04:05:49 +0000 | [diff] [blame] | 806 | return From == To || |
| 807 | RPOOrdering.lookup(DT->getNode(From)) >= |
| 808 | RPOOrdering.lookup(DT->getNode(To)); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 811 | #ifndef NDEBUG |
| 812 | static std::string getBlockName(const BasicBlock *B) { |
| Davide Italiano | 0e71480 | 2016-12-28 14:00:11 +0000 | [diff] [blame] | 813 | return DOTGraphTraits<const Function *>::getSimpleNodeLabel(B, nullptr); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 814 | } |
| 815 | #endif |
| 816 | |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 817 | // Get a MemoryAccess for an instruction, fake or real. |
| 818 | MemoryUseOrDef *NewGVN::getMemoryAccess(const Instruction *I) const { |
| 819 | auto *Result = MSSA->getMemoryAccess(I); |
| 820 | return Result ? Result : TempToMemory.lookup(I); |
| 821 | } |
| 822 | |
| 823 | // Get a MemoryPhi for a basic block. These are all real. |
| 824 | MemoryPhi *NewGVN::getMemoryAccess(const BasicBlock *BB) const { |
| 825 | return MSSA->getMemoryAccess(BB); |
| 826 | } |
| 827 | |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 828 | // Get the basic block from an instruction/memory value. |
| 829 | BasicBlock *NewGVN::getBlockForValue(Value *V) const { |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 830 | if (auto *I = dyn_cast<Instruction>(V)) { |
| 831 | auto *Parent = I->getParent(); |
| 832 | if (Parent) |
| 833 | return Parent; |
| 834 | Parent = TempToBlock.lookup(V); |
| 835 | assert(Parent && "Every fake instruction should have a block"); |
| 836 | return Parent; |
| 837 | } |
| 838 | |
| 839 | auto *MP = dyn_cast<MemoryPhi>(V); |
| 840 | assert(MP && "Should have been an instruction or a MemoryPhi"); |
| 841 | return MP->getBlock(); |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| Daniel Berlin | 0e90011 | 2017-03-24 06:33:48 +0000 | [diff] [blame] | 844 | // Delete a definitely dead expression, so it can be reused by the expression |
| 845 | // allocator. Some of these are not in creation functions, so we have to accept |
| 846 | // const versions. |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 847 | void NewGVN::deleteExpression(const Expression *E) const { |
| Daniel Berlin | 0e90011 | 2017-03-24 06:33:48 +0000 | [diff] [blame] | 848 | assert(isa<BasicExpression>(E)); |
| 849 | auto *BE = cast<BasicExpression>(E); |
| 850 | const_cast<BasicExpression *>(BE)->deallocateOperands(ArgRecycler); |
| 851 | ExpressionAllocator.Deallocate(E); |
| 852 | } |
| Daniel Berlin | 1a58258 | 2017-09-05 02:17:41 +0000 | [diff] [blame] | 853 | |
| Daniel Berlin | f9c9455 | 2017-09-05 02:17:43 +0000 | [diff] [blame^] | 854 | // If V is a predicateinfo copy, get the thing it is a copy of. |
| 855 | static Value *getCopyOf(const Value *V) { |
| Daniel Berlin | 1a58258 | 2017-09-05 02:17:41 +0000 | [diff] [blame] | 856 | if (auto *II = dyn_cast<IntrinsicInst>(V)) |
| Daniel Berlin | f9c9455 | 2017-09-05 02:17:43 +0000 | [diff] [blame^] | 857 | if (II->getIntrinsicID() == Intrinsic::ssa_copy) |
| 858 | return II->getOperand(0); |
| 859 | return nullptr; |
| 860 | } |
| 861 | |
| 862 | // Return true if V is really PN, even accounting for predicateinfo copies. |
| 863 | static bool isCopyOfPHI(const Value *V, const PHINode *PN) { |
| 864 | return V == PN || getCopyOf(V) == PN; |
| 865 | } |
| 866 | |
| 867 | static bool isCopyOfAPHI(const Value *V) { |
| 868 | auto *CO = getCopyOf(V); |
| 869 | return CO && isa<PHINode>(CO); |
| Daniel Berlin | 1a58258 | 2017-09-05 02:17:41 +0000 | [diff] [blame] | 870 | } |
| 871 | |
| Daniel Berlin | 2f72b19 | 2017-04-14 02:53:37 +0000 | [diff] [blame] | 872 | PHIExpression *NewGVN::createPHIExpression(Instruction *I, bool &HasBackedge, |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 873 | bool &OriginalOpsConstant) const { |
| 874 | BasicBlock *PHIBlock = getBlockForValue(I); |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 875 | auto *PN = cast<PHINode>(I); |
| Daniel Berlin | d92e7f9 | 2017-01-07 00:01:42 +0000 | [diff] [blame] | 876 | auto *E = |
| 877 | new (ExpressionAllocator) PHIExpression(PN->getNumOperands(), PHIBlock); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 878 | |
| 879 | E->allocateOperands(ArgRecycler, ExpressionAllocator); |
| 880 | E->setType(I->getType()); |
| 881 | E->setOpcode(I->getOpcode()); |
| Daniel Berlin | 85cbc8c | 2016-12-26 19:57:25 +0000 | [diff] [blame] | 882 | |
| Davide Italiano | d6bb8ca | 2017-05-09 16:58:28 +0000 | [diff] [blame] | 883 | // NewGVN assumes the operands of a PHI node are in a consistent order across |
| 884 | // PHIs. LLVM doesn't seem to always guarantee this. While we need to fix |
| 885 | // this in LLVM at some point we don't want GVN to find wrong congruences. |
| 886 | // Therefore, here we sort uses in predecessor order. |
| Davide Italiano | 63998ec | 2017-05-09 18:29:37 +0000 | [diff] [blame] | 887 | // We're sorting the values by pointer. In theory this might be cause of |
| 888 | // non-determinism, but here we don't rely on the ordering for anything |
| 889 | // significant, e.g. we don't create new instructions based on it so we're |
| 890 | // fine. |
| Davide Italiano | d6bb8ca | 2017-05-09 16:58:28 +0000 | [diff] [blame] | 891 | SmallVector<const Use *, 4> PHIOperands; |
| 892 | for (const Use &U : PN->operands()) |
| 893 | PHIOperands.push_back(&U); |
| 894 | std::sort(PHIOperands.begin(), PHIOperands.end(), |
| 895 | [&](const Use *U1, const Use *U2) { |
| 896 | return PN->getIncomingBlock(*U1) < PN->getIncomingBlock(*U2); |
| 897 | }); |
| 898 | |
| Davide Italiano | b3886dd | 2017-01-25 23:37:49 +0000 | [diff] [blame] | 899 | // Filter out unreachable phi operands. |
| Davide Italiano | d6bb8ca | 2017-05-09 16:58:28 +0000 | [diff] [blame] | 900 | auto Filtered = make_filter_range(PHIOperands, [&](const Use *U) { |
| Daniel Berlin | f9c9455 | 2017-09-05 02:17:43 +0000 | [diff] [blame^] | 901 | auto *BB = PN->getIncomingBlock(*U); |
| 902 | if (isCopyOfPHI(*U, PN)) |
| Daniel Berlin | e67c322 | 2017-05-25 15:44:20 +0000 | [diff] [blame] | 903 | return false; |
| Daniel Berlin | f9c9455 | 2017-09-05 02:17:43 +0000 | [diff] [blame^] | 904 | if (!ReachableEdges.count({BB, PHIBlock})) |
| Daniel Berlin | e67c322 | 2017-05-25 15:44:20 +0000 | [diff] [blame] | 905 | return false; |
| 906 | // Things in TOPClass are equivalent to everything. |
| 907 | if (ValueToClass.lookup(*U) == TOPClass) |
| 908 | return false; |
| Daniel Berlin | f9c9455 | 2017-09-05 02:17:43 +0000 | [diff] [blame^] | 909 | OriginalOpsConstant = OriginalOpsConstant && isa<Constant>(*U); |
| 910 | HasBackedge = HasBackedge || isBackedge(BB, PHIBlock); |
| Davide Italiano | a7a7754 | 2017-07-10 20:45:00 +0000 | [diff] [blame] | 911 | return lookupOperandLeader(*U) != PN; |
| Davide Italiano | b3886dd | 2017-01-25 23:37:49 +0000 | [diff] [blame] | 912 | }); |
| Daniel Berlin | f9c9455 | 2017-09-05 02:17:43 +0000 | [diff] [blame^] | 913 | std::transform( |
| 914 | Filtered.begin(), Filtered.end(), op_inserter(E), |
| 915 | [&](const Use *U) -> Value * { return lookupOperandLeader(*U); }); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 916 | return E; |
| 917 | } |
| 918 | |
| 919 | // Set basic expression info (Arguments, type, opcode) for Expression |
| 920 | // E from Instruction I in block B. |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 921 | bool NewGVN::setBasicExpressionInfo(Instruction *I, BasicExpression *E) const { |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 922 | bool AllConstant = true; |
| 923 | if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) |
| 924 | E->setType(GEP->getSourceElementType()); |
| 925 | else |
| 926 | E->setType(I->getType()); |
| 927 | E->setOpcode(I->getOpcode()); |
| 928 | E->allocateOperands(ArgRecycler, ExpressionAllocator); |
| 929 | |
| Daniel Berlin | 85cbc8c | 2016-12-26 19:57:25 +0000 | [diff] [blame] | 930 | // Transform the operand array into an operand leader array, and keep track of |
| 931 | // whether all members are constant. |
| 932 | std::transform(I->op_begin(), I->op_end(), op_inserter(E), [&](Value *O) { |
| Daniel Berlin | 203f47b | 2017-01-31 22:31:53 +0000 | [diff] [blame] | 933 | auto Operand = lookupOperandLeader(O); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 934 | AllConstant = AllConstant && isa<Constant>(Operand); |
| Daniel Berlin | 85cbc8c | 2016-12-26 19:57:25 +0000 | [diff] [blame] | 935 | return Operand; |
| 936 | }); |
| 937 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 938 | return AllConstant; |
| 939 | } |
| 940 | |
| 941 | const Expression *NewGVN::createBinaryExpression(unsigned Opcode, Type *T, |
| Daniel Berlin | 54a92fc | 2017-09-05 02:17:42 +0000 | [diff] [blame] | 942 | Value *Arg1, Value *Arg2, |
| 943 | Instruction *I) const { |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 944 | auto *E = new (ExpressionAllocator) BasicExpression(2); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 945 | |
| 946 | E->setType(T); |
| 947 | E->setOpcode(Opcode); |
| 948 | E->allocateOperands(ArgRecycler, ExpressionAllocator); |
| 949 | if (Instruction::isCommutative(Opcode)) { |
| 950 | // Ensure that commutative instructions that only differ by a permutation |
| 951 | // of their operands get the same value number by sorting the operand value |
| 952 | // numbers. Since all commutative instructions have two operands it is more |
| 953 | // efficient to sort by hand rather than using, say, std::sort. |
| Daniel Berlin | 1c08767 | 2017-02-11 15:07:01 +0000 | [diff] [blame] | 954 | if (shouldSwapOperands(Arg1, Arg2)) |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 955 | std::swap(Arg1, Arg2); |
| 956 | } |
| Daniel Berlin | 203f47b | 2017-01-31 22:31:53 +0000 | [diff] [blame] | 957 | E->op_push_back(lookupOperandLeader(Arg1)); |
| 958 | E->op_push_back(lookupOperandLeader(Arg2)); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 959 | |
| Daniel Berlin | ede130d | 2017-04-26 20:56:14 +0000 | [diff] [blame] | 960 | Value *V = SimplifyBinOp(Opcode, E->getOperand(0), E->getOperand(1), SQ); |
| Daniel Berlin | 54a92fc | 2017-09-05 02:17:42 +0000 | [diff] [blame] | 961 | if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V)) |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 962 | return SimplifiedE; |
| 963 | return E; |
| 964 | } |
| 965 | |
| 966 | // Take a Value returned by simplification of Expression E/Instruction |
| 967 | // I, and see if it resulted in a simpler expression. If so, return |
| 968 | // that expression. |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 969 | const Expression *NewGVN::checkSimplificationResults(Expression *E, |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 970 | Instruction *I, |
| 971 | Value *V) const { |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 972 | if (!V) |
| 973 | return nullptr; |
| 974 | if (auto *C = dyn_cast<Constant>(V)) { |
| 975 | if (I) |
| 976 | DEBUG(dbgs() << "Simplified " << *I << " to " |
| 977 | << " constant " << *C << "\n"); |
| 978 | NumGVNOpsSimplified++; |
| 979 | assert(isa<BasicExpression>(E) && |
| 980 | "We should always have had a basic expression here"); |
| Daniel Berlin | 0e90011 | 2017-03-24 06:33:48 +0000 | [diff] [blame] | 981 | deleteExpression(E); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 982 | return createConstantExpression(C); |
| 983 | } else if (isa<Argument>(V) || isa<GlobalVariable>(V)) { |
| 984 | if (I) |
| 985 | DEBUG(dbgs() << "Simplified " << *I << " to " |
| 986 | << " variable " << *V << "\n"); |
| Daniel Berlin | 0e90011 | 2017-03-24 06:33:48 +0000 | [diff] [blame] | 987 | deleteExpression(E); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 988 | return createVariableExpression(V); |
| 989 | } |
| 990 | |
| 991 | CongruenceClass *CC = ValueToClass.lookup(V); |
| Daniel Berlin | 7ef26da | 2017-08-30 19:52:39 +0000 | [diff] [blame] | 992 | if (CC) { |
| 993 | if (CC->getLeader() && CC->getLeader() != I) { |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 994 | // Don't add temporary instructions to the user lists. |
| 995 | if (!AllTempInstructions.count(I)) |
| 996 | addAdditionalUsers(V, I); |
| Daniel Berlin | 7ef26da | 2017-08-30 19:52:39 +0000 | [diff] [blame] | 997 | return createVariableOrConstant(CC->getLeader()); |
| Daniel Berlin | c8ed404 | 2017-05-30 06:42:29 +0000 | [diff] [blame] | 998 | } |
| Daniel Berlin | 7ef26da | 2017-08-30 19:52:39 +0000 | [diff] [blame] | 999 | if (CC->getDefiningExpr()) { |
| 1000 | // If we simplified to something else, we need to communicate |
| 1001 | // that we're users of the value we simplified to. |
| 1002 | if (I != V) { |
| 1003 | // Don't add temporary instructions to the user lists. |
| 1004 | if (!AllTempInstructions.count(I)) |
| 1005 | addAdditionalUsers(V, I); |
| 1006 | } |
| 1007 | |
| 1008 | if (I) |
| 1009 | DEBUG(dbgs() << "Simplified " << *I << " to " |
| 1010 | << " expression " << *CC->getDefiningExpr() << "\n"); |
| 1011 | NumGVNOpsSimplified++; |
| 1012 | deleteExpression(E); |
| 1013 | return CC->getDefiningExpr(); |
| 1014 | } |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1015 | } |
| Daniel Berlin | 7ef26da | 2017-08-30 19:52:39 +0000 | [diff] [blame] | 1016 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1017 | return nullptr; |
| 1018 | } |
| 1019 | |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 1020 | // Create a value expression from the instruction I, replacing operands with |
| 1021 | // their leaders. |
| 1022 | |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 1023 | const Expression *NewGVN::createExpression(Instruction *I) const { |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 1024 | auto *E = new (ExpressionAllocator) BasicExpression(I->getNumOperands()); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1025 | |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 1026 | bool AllConstant = setBasicExpressionInfo(I, E); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1027 | |
| 1028 | if (I->isCommutative()) { |
| 1029 | // Ensure that commutative instructions that only differ by a permutation |
| 1030 | // of their operands get the same value number by sorting the operand value |
| 1031 | // numbers. Since all commutative instructions have two operands it is more |
| 1032 | // efficient to sort by hand rather than using, say, std::sort. |
| 1033 | assert(I->getNumOperands() == 2 && "Unsupported commutative instruction!"); |
| Daniel Berlin | 508a1de | 2017-02-12 23:24:42 +0000 | [diff] [blame] | 1034 | if (shouldSwapOperands(E->getOperand(0), E->getOperand(1))) |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1035 | E->swapOperands(0, 1); |
| 1036 | } |
| Sanjay Patel | 7cf745c | 2017-08-03 15:18:27 +0000 | [diff] [blame] | 1037 | // Perform simplification. |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1038 | if (auto *CI = dyn_cast<CmpInst>(I)) { |
| 1039 | // Sort the operand value numbers so x<y and y>x get the same value |
| 1040 | // number. |
| 1041 | CmpInst::Predicate Predicate = CI->getPredicate(); |
| Daniel Berlin | 1c08767 | 2017-02-11 15:07:01 +0000 | [diff] [blame] | 1042 | if (shouldSwapOperands(E->getOperand(0), E->getOperand(1))) { |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1043 | E->swapOperands(0, 1); |
| 1044 | Predicate = CmpInst::getSwappedPredicate(Predicate); |
| 1045 | } |
| 1046 | E->setOpcode((CI->getOpcode() << 8) | Predicate); |
| 1047 | // TODO: 25% of our time is spent in SimplifyCmpInst with pointer operands |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1048 | assert(I->getOperand(0)->getType() == I->getOperand(1)->getType() && |
| 1049 | "Wrong types on cmp instruction"); |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 1050 | assert((E->getOperand(0)->getType() == I->getOperand(0)->getType() && |
| 1051 | E->getOperand(1)->getType() == I->getOperand(1)->getType())); |
| Daniel Berlin | ede130d | 2017-04-26 20:56:14 +0000 | [diff] [blame] | 1052 | Value *V = |
| 1053 | SimplifyCmpInst(Predicate, E->getOperand(0), E->getOperand(1), SQ); |
| Daniel Berlin | ff12c92 | 2017-01-31 22:32:01 +0000 | [diff] [blame] | 1054 | if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V)) |
| 1055 | return SimplifiedE; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1056 | } else if (isa<SelectInst>(I)) { |
| 1057 | if (isa<Constant>(E->getOperand(0)) || |
| Daniel Berlin | f948603 | 2017-08-24 02:43:17 +0000 | [diff] [blame] | 1058 | E->getOperand(1) == E->getOperand(2)) { |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 1059 | assert(E->getOperand(1)->getType() == I->getOperand(1)->getType() && |
| 1060 | E->getOperand(2)->getType() == I->getOperand(2)->getType()); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1061 | Value *V = SimplifySelectInst(E->getOperand(0), E->getOperand(1), |
| Daniel Berlin | ede130d | 2017-04-26 20:56:14 +0000 | [diff] [blame] | 1062 | E->getOperand(2), SQ); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1063 | if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V)) |
| 1064 | return SimplifiedE; |
| 1065 | } |
| 1066 | } else if (I->isBinaryOp()) { |
| Daniel Berlin | ede130d | 2017-04-26 20:56:14 +0000 | [diff] [blame] | 1067 | Value *V = |
| 1068 | SimplifyBinOp(E->getOpcode(), E->getOperand(0), E->getOperand(1), SQ); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1069 | if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V)) |
| 1070 | return SimplifiedE; |
| 1071 | } else if (auto *BI = dyn_cast<BitCastInst>(I)) { |
| Daniel Berlin | 4d0fe64 | 2017-04-28 19:55:38 +0000 | [diff] [blame] | 1072 | Value *V = |
| 1073 | SimplifyCastInst(BI->getOpcode(), BI->getOperand(0), BI->getType(), SQ); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1074 | if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V)) |
| 1075 | return SimplifiedE; |
| 1076 | } else if (isa<GetElementPtrInst>(I)) { |
| Daniel Berlin | ede130d | 2017-04-26 20:56:14 +0000 | [diff] [blame] | 1077 | Value *V = SimplifyGEPInst( |
| 1078 | E->getType(), ArrayRef<Value *>(E->op_begin(), E->op_end()), SQ); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1079 | if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V)) |
| 1080 | return SimplifiedE; |
| 1081 | } else if (AllConstant) { |
| 1082 | // We don't bother trying to simplify unless all of the operands |
| 1083 | // were constant. |
| 1084 | // TODO: There are a lot of Simplify*'s we could call here, if we |
| 1085 | // wanted to. The original motivating case for this code was a |
| 1086 | // zext i1 false to i8, which we don't have an interface to |
| 1087 | // simplify (IE there is no SimplifyZExt). |
| 1088 | |
| 1089 | SmallVector<Constant *, 8> C; |
| 1090 | for (Value *Arg : E->operands()) |
| Piotr Padlewski | 6c37d29 | 2016-12-28 23:24:02 +0000 | [diff] [blame] | 1091 | C.emplace_back(cast<Constant>(Arg)); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1092 | |
| Daniel Berlin | 64e6899 | 2017-03-12 04:46:45 +0000 | [diff] [blame] | 1093 | if (Value *V = ConstantFoldInstOperands(I, C, DL, TLI)) |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1094 | if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V)) |
| 1095 | return SimplifiedE; |
| 1096 | } |
| 1097 | return E; |
| 1098 | } |
| 1099 | |
| 1100 | const AggregateValueExpression * |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 1101 | NewGVN::createAggregateValueExpression(Instruction *I) const { |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1102 | if (auto *II = dyn_cast<InsertValueInst>(I)) { |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 1103 | auto *E = new (ExpressionAllocator) |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1104 | AggregateValueExpression(I->getNumOperands(), II->getNumIndices()); |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 1105 | setBasicExpressionInfo(I, E); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1106 | E->allocateIntOperands(ExpressionAllocator); |
| Daniel Berlin | 85cbc8c | 2016-12-26 19:57:25 +0000 | [diff] [blame] | 1107 | std::copy(II->idx_begin(), II->idx_end(), int_op_inserter(E)); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1108 | return E; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1109 | } else if (auto *EI = dyn_cast<ExtractValueInst>(I)) { |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 1110 | auto *E = new (ExpressionAllocator) |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1111 | AggregateValueExpression(I->getNumOperands(), EI->getNumIndices()); |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 1112 | setBasicExpressionInfo(EI, E); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1113 | E->allocateIntOperands(ExpressionAllocator); |
| Daniel Berlin | 85cbc8c | 2016-12-26 19:57:25 +0000 | [diff] [blame] | 1114 | std::copy(EI->idx_begin(), EI->idx_end(), int_op_inserter(E)); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1115 | return E; |
| 1116 | } |
| 1117 | llvm_unreachable("Unhandled type of aggregate value operation"); |
| 1118 | } |
| 1119 | |
| Daniel Berlin | e021d2d | 2017-05-19 20:22:20 +0000 | [diff] [blame] | 1120 | const DeadExpression *NewGVN::createDeadExpression() const { |
| 1121 | // DeadExpression has no arguments and all DeadExpression's are the same, |
| 1122 | // so we only need one of them. |
| 1123 | return SingletonDeadExpression; |
| 1124 | } |
| 1125 | |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 1126 | const VariableExpression *NewGVN::createVariableExpression(Value *V) const { |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 1127 | auto *E = new (ExpressionAllocator) VariableExpression(V); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1128 | E->setOpcode(V->getValueID()); |
| 1129 | return E; |
| 1130 | } |
| 1131 | |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 1132 | const Expression *NewGVN::createVariableOrConstant(Value *V) const { |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1133 | if (auto *C = dyn_cast<Constant>(V)) |
| 1134 | return createConstantExpression(C); |
| 1135 | return createVariableExpression(V); |
| 1136 | } |
| 1137 | |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 1138 | const ConstantExpression *NewGVN::createConstantExpression(Constant *C) const { |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 1139 | auto *E = new (ExpressionAllocator) ConstantExpression(C); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1140 | E->setOpcode(C->getValueID()); |
| 1141 | return E; |
| 1142 | } |
| 1143 | |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 1144 | const UnknownExpression *NewGVN::createUnknownExpression(Instruction *I) const { |
| Daniel Berlin | 02c6b17 | 2017-01-02 18:00:53 +0000 | [diff] [blame] | 1145 | auto *E = new (ExpressionAllocator) UnknownExpression(I); |
| 1146 | E->setOpcode(I->getOpcode()); |
| 1147 | return E; |
| 1148 | } |
| 1149 | |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 1150 | const CallExpression * |
| 1151 | NewGVN::createCallExpression(CallInst *CI, const MemoryAccess *MA) const { |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1152 | // FIXME: Add operand bundles for calls. |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 1153 | auto *E = |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1154 | new (ExpressionAllocator) CallExpression(CI->getNumOperands(), CI, MA); |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 1155 | setBasicExpressionInfo(CI, E); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1156 | return E; |
| 1157 | } |
| 1158 | |
| Daniel Berlin | 9d0796e | 2017-03-24 05:30:34 +0000 | [diff] [blame] | 1159 | // Return true if some equivalent of instruction Inst dominates instruction U. |
| 1160 | bool NewGVN::someEquivalentDominates(const Instruction *Inst, |
| 1161 | const Instruction *U) const { |
| 1162 | auto *CC = ValueToClass.lookup(Inst); |
| Daniel Berlin | ffc3078 | 2017-03-24 06:33:51 +0000 | [diff] [blame] | 1163 | // This must be an instruction because we are only called from phi nodes |
| 1164 | // in the case that the value it needs to check against is an instruction. |
| 1165 | |
| 1166 | // The most likely candiates for dominance are the leader and the next leader. |
| 1167 | // The leader or nextleader will dominate in all cases where there is an |
| 1168 | // equivalent that is higher up in the dom tree. |
| 1169 | // We can't *only* check them, however, because the |
| 1170 | // dominator tree could have an infinite number of non-dominating siblings |
| 1171 | // with instructions that are in the right congruence class. |
| 1172 | // A |
| 1173 | // B C D E F G |
| 1174 | // | |
| 1175 | // H |
| 1176 | // Instruction U could be in H, with equivalents in every other sibling. |
| 1177 | // Depending on the rpo order picked, the leader could be the equivalent in |
| 1178 | // any of these siblings. |
| 1179 | if (!CC) |
| 1180 | return false; |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 1181 | if (DT->dominates(cast<Instruction>(CC->getLeader()), U)) |
| Daniel Berlin | ffc3078 | 2017-03-24 06:33:51 +0000 | [diff] [blame] | 1182 | return true; |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 1183 | if (CC->getNextLeader().first && |
| 1184 | DT->dominates(cast<Instruction>(CC->getNextLeader().first), U)) |
| Daniel Berlin | ffc3078 | 2017-03-24 06:33:51 +0000 | [diff] [blame] | 1185 | return true; |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 1186 | return llvm::any_of(*CC, [&](const Value *Member) { |
| 1187 | return Member != CC->getLeader() && |
| Daniel Berlin | ffc3078 | 2017-03-24 06:33:51 +0000 | [diff] [blame] | 1188 | DT->dominates(cast<Instruction>(Member), U); |
| 1189 | }); |
| Daniel Berlin | 9d0796e | 2017-03-24 05:30:34 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1192 | // See if we have a congruence class and leader for this operand, and if so, |
| 1193 | // return it. Otherwise, return the operand itself. |
| Daniel Berlin | 203f47b | 2017-01-31 22:31:53 +0000 | [diff] [blame] | 1194 | Value *NewGVN::lookupOperandLeader(Value *V) const { |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1195 | CongruenceClass *CC = ValueToClass.lookup(V); |
| Daniel Berlin | b79f536 | 2017-02-11 12:48:50 +0000 | [diff] [blame] | 1196 | if (CC) { |
| Daniel Berlin | e021d2d | 2017-05-19 20:22:20 +0000 | [diff] [blame] | 1197 | // Everything in TOP is represented by undef, as it can be any value. |
| Daniel Berlin | b79f536 | 2017-02-11 12:48:50 +0000 | [diff] [blame] | 1198 | // We do have to make sure we get the type right though, so we can't set the |
| 1199 | // RepLeader to undef. |
| Daniel Berlin | 5c338ff | 2017-03-10 19:05:04 +0000 | [diff] [blame] | 1200 | if (CC == TOPClass) |
| Daniel Berlin | b79f536 | 2017-02-11 12:48:50 +0000 | [diff] [blame] | 1201 | return UndefValue::get(V->getType()); |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 1202 | return CC->getStoredValue() ? CC->getStoredValue() : CC->getLeader(); |
| Daniel Berlin | b79f536 | 2017-02-11 12:48:50 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1205 | return V; |
| 1206 | } |
| 1207 | |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1208 | const MemoryAccess *NewGVN::lookupMemoryLeader(const MemoryAccess *MA) const { |
| 1209 | auto *CC = getMemoryClass(MA); |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 1210 | assert(CC->getMemoryLeader() && |
| Davide Italiano | b60f6e0 | 2017-05-12 15:25:56 +0000 | [diff] [blame] | 1211 | "Every MemoryAccess should be mapped to a congruence class with a " |
| 1212 | "representative memory access"); |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 1213 | return CC->getMemoryLeader(); |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 1216 | // Return true if the MemoryAccess is really equivalent to everything. This is |
| 1217 | // equivalent to the lattice value "TOP" in most lattices. This is the initial |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1218 | // state of all MemoryAccesses. |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 1219 | bool NewGVN::isMemoryAccessTOP(const MemoryAccess *MA) const { |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1220 | return getMemoryClass(MA) == TOPClass; |
| 1221 | } |
| 1222 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1223 | LoadExpression *NewGVN::createLoadExpression(Type *LoadType, Value *PointerOp, |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1224 | LoadInst *LI, |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 1225 | const MemoryAccess *MA) const { |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1226 | auto *E = |
| 1227 | new (ExpressionAllocator) LoadExpression(1, LI, lookupMemoryLeader(MA)); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1228 | E->allocateOperands(ArgRecycler, ExpressionAllocator); |
| 1229 | E->setType(LoadType); |
| 1230 | |
| 1231 | // Give store and loads same opcode so they value number together. |
| 1232 | E->setOpcode(0); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1233 | E->op_push_back(PointerOp); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1234 | if (LI) |
| 1235 | E->setAlignment(LI->getAlignment()); |
| 1236 | |
| 1237 | // TODO: Value number heap versions. We may be able to discover |
| 1238 | // things alias analysis can't on it's own (IE that a store and a |
| 1239 | // load have the same value, and thus, it isn't clobbering the load). |
| 1240 | return E; |
| 1241 | } |
| 1242 | |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 1243 | const StoreExpression * |
| 1244 | NewGVN::createStoreExpression(StoreInst *SI, const MemoryAccess *MA) const { |
| Daniel Berlin | 203f47b | 2017-01-31 22:31:53 +0000 | [diff] [blame] | 1245 | auto *StoredValueLeader = lookupOperandLeader(SI->getValueOperand()); |
| Daniel Berlin | 26addef | 2017-01-20 21:04:30 +0000 | [diff] [blame] | 1246 | auto *E = new (ExpressionAllocator) |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1247 | StoreExpression(SI->getNumOperands(), SI, StoredValueLeader, MA); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1248 | E->allocateOperands(ArgRecycler, ExpressionAllocator); |
| 1249 | E->setType(SI->getValueOperand()->getType()); |
| 1250 | |
| 1251 | // Give store and loads same opcode so they value number together. |
| 1252 | E->setOpcode(0); |
| Daniel Berlin | 203f47b | 2017-01-31 22:31:53 +0000 | [diff] [blame] | 1253 | E->op_push_back(lookupOperandLeader(SI->getPointerOperand())); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1254 | |
| 1255 | // TODO: Value number heap versions. We may be able to discover |
| 1256 | // things alias analysis can't on it's own (IE that a store and a |
| 1257 | // load have the same value, and thus, it isn't clobbering the load). |
| 1258 | return E; |
| 1259 | } |
| 1260 | |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 1261 | const Expression *NewGVN::performSymbolicStoreEvaluation(Instruction *I) const { |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 1262 | // Unlike loads, we never try to eliminate stores, so we do not check if they |
| 1263 | // are simple and avoid value numbering them. |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 1264 | auto *SI = cast<StoreInst>(I); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 1265 | auto *StoreAccess = getMemoryAccess(SI); |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 1266 | // Get the expression, if any, for the RHS of the MemoryDef. |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1267 | const MemoryAccess *StoreRHS = StoreAccess->getDefiningAccess(); |
| 1268 | if (EnableStoreRefinement) |
| 1269 | StoreRHS = MSSAWalker->getClobberingMemoryAccess(StoreAccess); |
| 1270 | // If we bypassed the use-def chains, make sure we add a use. |
| Daniel Berlin | de269f4 | 2017-08-26 07:37:11 +0000 | [diff] [blame] | 1271 | StoreRHS = lookupMemoryLeader(StoreRHS); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1272 | if (StoreRHS != StoreAccess->getDefiningAccess()) |
| 1273 | addMemoryUsers(StoreRHS, StoreAccess); |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 1274 | // If we are defined by ourselves, use the live on entry def. |
| 1275 | if (StoreRHS == StoreAccess) |
| 1276 | StoreRHS = MSSA->getLiveOnEntryDef(); |
| 1277 | |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 1278 | if (SI->isSimple()) { |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 1279 | // See if we are defined by a previous store expression, it already has a |
| 1280 | // value, and it's the same value as our current store. FIXME: Right now, we |
| 1281 | // only do this for simple stores, we should expand to cover memcpys, etc. |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1282 | const auto *LastStore = createStoreExpression(SI, StoreRHS); |
| 1283 | const auto *LastCC = ExpressionToClass.lookup(LastStore); |
| Daniel Berlin | 36b08b2 | 2017-06-19 00:24:00 +0000 | [diff] [blame] | 1284 | // We really want to check whether the expression we matched was a store. No |
| 1285 | // easy way to do that. However, we can check that the class we found has a |
| 1286 | // store, which, assuming the value numbering state is not corrupt, is |
| 1287 | // sufficient, because we must also be equivalent to that store's expression |
| 1288 | // for it to be in the same class as the load. |
| 1289 | if (LastCC && LastCC->getStoredValue() == LastStore->getStoredValue()) |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1290 | return LastStore; |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 1291 | // Also check if our value operand is defined by a load of the same memory |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1292 | // location, and the memory state is the same as it was then (otherwise, it |
| 1293 | // could have been overwritten later. See test32 in |
| 1294 | // transforms/DeadStoreElimination/simple.ll). |
| Daniel Berlin | 36b08b2 | 2017-06-19 00:24:00 +0000 | [diff] [blame] | 1295 | if (auto *LI = dyn_cast<LoadInst>(LastStore->getStoredValue())) |
| Daniel Berlin | 203f47b | 2017-01-31 22:31:53 +0000 | [diff] [blame] | 1296 | if ((lookupOperandLeader(LI->getPointerOperand()) == |
| Daniel Berlin | 36b08b2 | 2017-06-19 00:24:00 +0000 | [diff] [blame] | 1297 | LastStore->getOperand(0)) && |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 1298 | (lookupMemoryLeader(getMemoryAccess(LI)->getDefiningAccess()) == |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1299 | StoreRHS)) |
| Daniel Berlin | 36b08b2 | 2017-06-19 00:24:00 +0000 | [diff] [blame] | 1300 | return LastStore; |
| 1301 | deleteExpression(LastStore); |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 1302 | } |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1303 | |
| 1304 | // If the store is not equivalent to anything, value number it as a store that |
| 1305 | // produces a unique memory state (instead of using it's MemoryUse, we use |
| 1306 | // it's MemoryDef). |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 1307 | return createStoreExpression(SI, StoreAccess); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| Daniel Berlin | 07daac8 | 2017-04-02 13:23:44 +0000 | [diff] [blame] | 1310 | // See if we can extract the value of a loaded pointer from a load, a store, or |
| 1311 | // a memory instruction. |
| 1312 | const Expression * |
| 1313 | NewGVN::performSymbolicLoadCoercion(Type *LoadType, Value *LoadPtr, |
| 1314 | LoadInst *LI, Instruction *DepInst, |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 1315 | MemoryAccess *DefiningAccess) const { |
| Daniel Berlin | 07daac8 | 2017-04-02 13:23:44 +0000 | [diff] [blame] | 1316 | assert((!LI || LI->isSimple()) && "Not a simple load"); |
| 1317 | if (auto *DepSI = dyn_cast<StoreInst>(DepInst)) { |
| 1318 | // Can't forward from non-atomic to atomic without violating memory model. |
| 1319 | // Also don't need to coerce if they are the same type, we will just |
| Sanjay Patel | 7cf745c | 2017-08-03 15:18:27 +0000 | [diff] [blame] | 1320 | // propagate. |
| Daniel Berlin | 07daac8 | 2017-04-02 13:23:44 +0000 | [diff] [blame] | 1321 | if (LI->isAtomic() > DepSI->isAtomic() || |
| 1322 | LoadType == DepSI->getValueOperand()->getType()) |
| 1323 | return nullptr; |
| 1324 | int Offset = analyzeLoadFromClobberingStore(LoadType, LoadPtr, DepSI, DL); |
| 1325 | if (Offset >= 0) { |
| 1326 | if (auto *C = dyn_cast<Constant>( |
| 1327 | lookupOperandLeader(DepSI->getValueOperand()))) { |
| 1328 | DEBUG(dbgs() << "Coercing load from store " << *DepSI << " to constant " |
| 1329 | << *C << "\n"); |
| 1330 | return createConstantExpression( |
| 1331 | getConstantStoreValueForLoad(C, Offset, LoadType, DL)); |
| 1332 | } |
| 1333 | } |
| 1334 | |
| Davide Italiano | 9bdccb3 | 2017-08-26 22:31:10 +0000 | [diff] [blame] | 1335 | } else if (auto *DepLI = dyn_cast<LoadInst>(DepInst)) { |
| Daniel Berlin | 07daac8 | 2017-04-02 13:23:44 +0000 | [diff] [blame] | 1336 | // Can't forward from non-atomic to atomic without violating memory model. |
| 1337 | if (LI->isAtomic() > DepLI->isAtomic()) |
| 1338 | return nullptr; |
| 1339 | int Offset = analyzeLoadFromClobberingLoad(LoadType, LoadPtr, DepLI, DL); |
| 1340 | if (Offset >= 0) { |
| Sanjay Patel | 7cf745c | 2017-08-03 15:18:27 +0000 | [diff] [blame] | 1341 | // We can coerce a constant load into a load. |
| Daniel Berlin | 07daac8 | 2017-04-02 13:23:44 +0000 | [diff] [blame] | 1342 | if (auto *C = dyn_cast<Constant>(lookupOperandLeader(DepLI))) |
| 1343 | if (auto *PossibleConstant = |
| 1344 | getConstantLoadValueForLoad(C, Offset, LoadType, DL)) { |
| 1345 | DEBUG(dbgs() << "Coercing load from load " << *LI << " to constant " |
| 1346 | << *PossibleConstant << "\n"); |
| 1347 | return createConstantExpression(PossibleConstant); |
| 1348 | } |
| 1349 | } |
| 1350 | |
| Davide Italiano | 9bdccb3 | 2017-08-26 22:31:10 +0000 | [diff] [blame] | 1351 | } else if (auto *DepMI = dyn_cast<MemIntrinsic>(DepInst)) { |
| Daniel Berlin | 07daac8 | 2017-04-02 13:23:44 +0000 | [diff] [blame] | 1352 | int Offset = analyzeLoadFromClobberingMemInst(LoadType, LoadPtr, DepMI, DL); |
| 1353 | if (Offset >= 0) { |
| 1354 | if (auto *PossibleConstant = |
| 1355 | getConstantMemInstValueForLoad(DepMI, Offset, LoadType, DL)) { |
| 1356 | DEBUG(dbgs() << "Coercing load from meminst " << *DepMI |
| 1357 | << " to constant " << *PossibleConstant << "\n"); |
| 1358 | return createConstantExpression(PossibleConstant); |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | // All of the below are only true if the loaded pointer is produced |
| 1364 | // by the dependent instruction. |
| 1365 | if (LoadPtr != lookupOperandLeader(DepInst) && |
| 1366 | !AA->isMustAlias(LoadPtr, DepInst)) |
| 1367 | return nullptr; |
| 1368 | // If this load really doesn't depend on anything, then we must be loading an |
| 1369 | // undef value. This can happen when loading for a fresh allocation with no |
| 1370 | // intervening stores, for example. Note that this is only true in the case |
| 1371 | // that the result of the allocation is pointer equal to the load ptr. |
| 1372 | if (isa<AllocaInst>(DepInst) || isMallocLikeFn(DepInst, TLI)) { |
| 1373 | return createConstantExpression(UndefValue::get(LoadType)); |
| 1374 | } |
| 1375 | // If this load occurs either right after a lifetime begin, |
| 1376 | // then the loaded value is undefined. |
| 1377 | else if (auto *II = dyn_cast<IntrinsicInst>(DepInst)) { |
| 1378 | if (II->getIntrinsicID() == Intrinsic::lifetime_start) |
| 1379 | return createConstantExpression(UndefValue::get(LoadType)); |
| 1380 | } |
| 1381 | // If this load follows a calloc (which zero initializes memory), |
| 1382 | // then the loaded value is zero |
| 1383 | else if (isCallocLikeFn(DepInst, TLI)) { |
| 1384 | return createConstantExpression(Constant::getNullValue(LoadType)); |
| 1385 | } |
| 1386 | |
| 1387 | return nullptr; |
| 1388 | } |
| 1389 | |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 1390 | const Expression *NewGVN::performSymbolicLoadEvaluation(Instruction *I) const { |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 1391 | auto *LI = cast<LoadInst>(I); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1392 | |
| 1393 | // We can eliminate in favor of non-simple loads, but we won't be able to |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 1394 | // eliminate the loads themselves. |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1395 | if (!LI->isSimple()) |
| 1396 | return nullptr; |
| 1397 | |
| Daniel Berlin | 203f47b | 2017-01-31 22:31:53 +0000 | [diff] [blame] | 1398 | Value *LoadAddressLeader = lookupOperandLeader(LI->getPointerOperand()); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1399 | // Load of undef is undef. |
| 1400 | if (isa<UndefValue>(LoadAddressLeader)) |
| 1401 | return createConstantExpression(UndefValue::get(LI->getType())); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 1402 | MemoryAccess *OriginalAccess = getMemoryAccess(I); |
| 1403 | MemoryAccess *DefiningAccess = |
| 1404 | MSSAWalker->getClobberingMemoryAccess(OriginalAccess); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1405 | |
| 1406 | if (!MSSA->isLiveOnEntryDef(DefiningAccess)) { |
| 1407 | if (auto *MD = dyn_cast<MemoryDef>(DefiningAccess)) { |
| 1408 | Instruction *DefiningInst = MD->getMemoryInst(); |
| 1409 | // If the defining instruction is not reachable, replace with undef. |
| 1410 | if (!ReachableBlocks.count(DefiningInst->getParent())) |
| 1411 | return createConstantExpression(UndefValue::get(LI->getType())); |
| Daniel Berlin | 07daac8 | 2017-04-02 13:23:44 +0000 | [diff] [blame] | 1412 | // This will handle stores and memory insts. We only do if it the |
| 1413 | // defining access has a different type, or it is a pointer produced by |
| 1414 | // certain memory operations that cause the memory to have a fixed value |
| 1415 | // (IE things like calloc). |
| Daniel Berlin | 5845e05 | 2017-04-06 18:52:53 +0000 | [diff] [blame] | 1416 | if (const auto *CoercionResult = |
| 1417 | performSymbolicLoadCoercion(LI->getType(), LoadAddressLeader, LI, |
| 1418 | DefiningInst, DefiningAccess)) |
| Daniel Berlin | 07daac8 | 2017-04-02 13:23:44 +0000 | [diff] [blame] | 1419 | return CoercionResult; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1420 | } |
| 1421 | } |
| 1422 | |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 1423 | const auto *LE = createLoadExpression(LI->getType(), LoadAddressLeader, LI, |
| 1424 | DefiningAccess); |
| Daniel Berlin | de269f4 | 2017-08-26 07:37:11 +0000 | [diff] [blame] | 1425 | // If our MemoryLeader is not our defining access, add a use to the |
| 1426 | // MemoryLeader, so that we get reprocessed when it changes. |
| 1427 | if (LE->getMemoryLeader() != DefiningAccess) |
| 1428 | addMemoryUsers(LE->getMemoryLeader(), OriginalAccess); |
| 1429 | return LE; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1432 | const Expression * |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 1433 | NewGVN::performSymbolicPredicateInfoEvaluation(Instruction *I) const { |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1434 | auto *PI = PredInfo->getPredicateInfoFor(I); |
| 1435 | if (!PI) |
| 1436 | return nullptr; |
| 1437 | |
| 1438 | DEBUG(dbgs() << "Found predicate info from instruction !\n"); |
| Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 1439 | |
| 1440 | auto *PWC = dyn_cast<PredicateWithCondition>(PI); |
| 1441 | if (!PWC) |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1442 | return nullptr; |
| 1443 | |
| Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 1444 | auto *CopyOf = I->getOperand(0); |
| 1445 | auto *Cond = PWC->Condition; |
| 1446 | |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1447 | // If this a copy of the condition, it must be either true or false depending |
| Sanjay Patel | 7cf745c | 2017-08-03 15:18:27 +0000 | [diff] [blame] | 1448 | // on the predicate info type and edge. |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1449 | if (CopyOf == Cond) { |
| Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 1450 | // We should not need to add predicate users because the predicate info is |
| 1451 | // already a use of this operand. |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1452 | if (isa<PredicateAssume>(PI)) |
| 1453 | return createConstantExpression(ConstantInt::getTrue(Cond->getType())); |
| 1454 | if (auto *PBranch = dyn_cast<PredicateBranch>(PI)) { |
| 1455 | if (PBranch->TrueEdge) |
| 1456 | return createConstantExpression(ConstantInt::getTrue(Cond->getType())); |
| 1457 | return createConstantExpression(ConstantInt::getFalse(Cond->getType())); |
| 1458 | } |
| Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 1459 | if (auto *PSwitch = dyn_cast<PredicateSwitch>(PI)) |
| 1460 | return createConstantExpression(cast<Constant>(PSwitch->CaseValue)); |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1461 | } |
| Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 1462 | |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1463 | // Not a copy of the condition, so see what the predicates tell us about this |
| 1464 | // value. First, though, we check to make sure the value is actually a copy |
| 1465 | // of one of the condition operands. It's possible, in certain cases, for it |
| 1466 | // to be a copy of a predicateinfo copy. In particular, if two branch |
| 1467 | // operations use the same condition, and one branch dominates the other, we |
| 1468 | // will end up with a copy of a copy. This is currently a small deficiency in |
| Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 1469 | // predicateinfo. What will end up happening here is that we will value |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1470 | // number both copies the same anyway. |
| Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 1471 | |
| 1472 | // Everything below relies on the condition being a comparison. |
| 1473 | auto *Cmp = dyn_cast<CmpInst>(Cond); |
| 1474 | if (!Cmp) |
| 1475 | return nullptr; |
| 1476 | |
| 1477 | if (CopyOf != Cmp->getOperand(0) && CopyOf != Cmp->getOperand(1)) { |
| Davide Italiano | c43a9f8 | 2017-05-12 15:28:12 +0000 | [diff] [blame] | 1478 | DEBUG(dbgs() << "Copy is not of any condition operands!\n"); |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1479 | return nullptr; |
| 1480 | } |
| Daniel Berlin | fccbda9 | 2017-02-22 22:20:58 +0000 | [diff] [blame] | 1481 | Value *FirstOp = lookupOperandLeader(Cmp->getOperand(0)); |
| 1482 | Value *SecondOp = lookupOperandLeader(Cmp->getOperand(1)); |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1483 | bool SwappedOps = false; |
| Sanjay Patel | 7cf745c | 2017-08-03 15:18:27 +0000 | [diff] [blame] | 1484 | // Sort the ops. |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1485 | if (shouldSwapOperands(FirstOp, SecondOp)) { |
| 1486 | std::swap(FirstOp, SecondOp); |
| 1487 | SwappedOps = true; |
| 1488 | } |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1489 | CmpInst::Predicate Predicate = |
| 1490 | SwappedOps ? Cmp->getSwappedPredicate() : Cmp->getPredicate(); |
| 1491 | |
| 1492 | if (isa<PredicateAssume>(PI)) { |
| 1493 | // If the comparison is true when the operands are equal, then we know the |
| 1494 | // operands are equal, because assumes must always be true. |
| 1495 | if (CmpInst::isTrueWhenEqual(Predicate)) { |
| 1496 | addPredicateUsers(PI, I); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 1497 | addAdditionalUsers(Cmp->getOperand(0), I); |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1498 | return createVariableOrConstant(FirstOp); |
| 1499 | } |
| 1500 | } |
| 1501 | if (const auto *PBranch = dyn_cast<PredicateBranch>(PI)) { |
| 1502 | // If we are *not* a copy of the comparison, we may equal to the other |
| 1503 | // operand when the predicate implies something about equality of |
| 1504 | // operations. In particular, if the comparison is true/false when the |
| 1505 | // operands are equal, and we are on the right edge, we know this operation |
| 1506 | // is equal to something. |
| 1507 | if ((PBranch->TrueEdge && Predicate == CmpInst::ICMP_EQ) || |
| 1508 | (!PBranch->TrueEdge && Predicate == CmpInst::ICMP_NE)) { |
| 1509 | addPredicateUsers(PI, I); |
| Daniel Berlin | 23fec57 | 2017-08-30 19:53:23 +0000 | [diff] [blame] | 1510 | addAdditionalUsers(SwappedOps ? Cmp->getOperand(1) : Cmp->getOperand(0), |
| 1511 | I); |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1512 | return createVariableOrConstant(FirstOp); |
| 1513 | } |
| 1514 | // Handle the special case of floating point. |
| 1515 | if (((PBranch->TrueEdge && Predicate == CmpInst::FCMP_OEQ) || |
| 1516 | (!PBranch->TrueEdge && Predicate == CmpInst::FCMP_UNE)) && |
| 1517 | isa<ConstantFP>(FirstOp) && !cast<ConstantFP>(FirstOp)->isZero()) { |
| 1518 | addPredicateUsers(PI, I); |
| Daniel Berlin | 23fec57 | 2017-08-30 19:53:23 +0000 | [diff] [blame] | 1519 | addAdditionalUsers(SwappedOps ? Cmp->getOperand(1) : Cmp->getOperand(0), |
| 1520 | I); |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1521 | return createConstantExpression(cast<Constant>(FirstOp)); |
| 1522 | } |
| 1523 | } |
| 1524 | return nullptr; |
| 1525 | } |
| 1526 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1527 | // Evaluate read only and pure calls, and create an expression result. |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 1528 | const Expression *NewGVN::performSymbolicCallEvaluation(Instruction *I) const { |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 1529 | auto *CI = cast<CallInst>(I); |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1530 | if (auto *II = dyn_cast<IntrinsicInst>(I)) { |
| 1531 | // Instrinsics with the returned attribute are copies of arguments. |
| 1532 | if (auto *ReturnedValue = II->getReturnedArgOperand()) { |
| 1533 | if (II->getIntrinsicID() == Intrinsic::ssa_copy) |
| 1534 | if (const auto *Result = performSymbolicPredicateInfoEvaluation(I)) |
| 1535 | return Result; |
| 1536 | return createVariableOrConstant(ReturnedValue); |
| 1537 | } |
| 1538 | } |
| 1539 | if (AA->doesNotAccessMemory(CI)) { |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 1540 | return createCallExpression(CI, TOPClass->getMemoryLeader()); |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1541 | } else if (AA->onlyReadsMemory(CI)) { |
| Daniel Berlin | 85cbc8c | 2016-12-26 19:57:25 +0000 | [diff] [blame] | 1542 | MemoryAccess *DefiningAccess = MSSAWalker->getClobberingMemoryAccess(CI); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1543 | return createCallExpression(CI, DefiningAccess); |
| Davide Italiano | b222549 | 2016-12-27 18:15:39 +0000 | [diff] [blame] | 1544 | } |
| 1545 | return nullptr; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1548 | // Retrieve the memory class for a given MemoryAccess. |
| 1549 | CongruenceClass *NewGVN::getMemoryClass(const MemoryAccess *MA) const { |
| 1550 | |
| 1551 | auto *Result = MemoryAccessToClass.lookup(MA); |
| 1552 | assert(Result && "Should have found memory class"); |
| 1553 | return Result; |
| 1554 | } |
| 1555 | |
| 1556 | // Update the MemoryAccess equivalence table to say that From is equal to To, |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 1557 | // and return true if this is different from what already existed in the table. |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1558 | bool NewGVN::setMemoryClass(const MemoryAccess *From, |
| 1559 | CongruenceClass *NewClass) { |
| 1560 | assert(NewClass && |
| 1561 | "Every MemoryAccess should be getting mapped to a non-null class"); |
| Daniel Berlin | 1ea5f32 | 2017-01-26 22:21:48 +0000 | [diff] [blame] | 1562 | DEBUG(dbgs() << "Setting " << *From); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1563 | DEBUG(dbgs() << " equivalent to congruence class "); |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 1564 | DEBUG(dbgs() << NewClass->getID() << " with current MemoryAccess leader "); |
| Davide Italiano | b7a6698 | 2017-05-09 20:02:48 +0000 | [diff] [blame] | 1565 | DEBUG(dbgs() << *NewClass->getMemoryLeader() << "\n"); |
| Daniel Berlin | 1ea5f32 | 2017-01-26 22:21:48 +0000 | [diff] [blame] | 1566 | |
| 1567 | auto LookupResult = MemoryAccessToClass.find(From); |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 1568 | bool Changed = false; |
| 1569 | // If it's already in the table, see if the value changed. |
| Daniel Berlin | 1ea5f32 | 2017-01-26 22:21:48 +0000 | [diff] [blame] | 1570 | if (LookupResult != MemoryAccessToClass.end()) { |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1571 | auto *OldClass = LookupResult->second; |
| 1572 | if (OldClass != NewClass) { |
| 1573 | // If this is a phi, we have to handle memory member updates. |
| 1574 | if (auto *MP = dyn_cast<MemoryPhi>(From)) { |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 1575 | OldClass->memory_erase(MP); |
| 1576 | NewClass->memory_insert(MP); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1577 | // This may have killed the class if it had no non-memory members |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 1578 | if (OldClass->getMemoryLeader() == From) { |
| Davide Italiano | 41f5c7b | 2017-05-12 15:22:45 +0000 | [diff] [blame] | 1579 | if (OldClass->definesNoMemory()) { |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 1580 | OldClass->setMemoryLeader(nullptr); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1581 | } else { |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 1582 | OldClass->setMemoryLeader(getNextMemoryLeader(OldClass)); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1583 | DEBUG(dbgs() << "Memory class leader change for class " |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 1584 | << OldClass->getID() << " to " |
| 1585 | << *OldClass->getMemoryLeader() |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1586 | << " due to removal of a memory member " << *From |
| 1587 | << "\n"); |
| 1588 | markMemoryLeaderChangeTouched(OldClass); |
| 1589 | } |
| 1590 | } |
| 1591 | } |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 1592 | // It wasn't equivalent before, and now it is. |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1593 | LookupResult->second = NewClass; |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 1594 | Changed = true; |
| 1595 | } |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 1596 | } |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 1597 | |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 1598 | return Changed; |
| 1599 | } |
| Daniel Berlin | 0e90011 | 2017-03-24 06:33:48 +0000 | [diff] [blame] | 1600 | |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 1601 | // Determine if a instruction is cycle-free. That means the values in the |
| 1602 | // instruction don't depend on any expressions that can change value as a result |
| 1603 | // of the instruction. For example, a non-cycle free instruction would be v = |
| 1604 | // phi(0, v+1). |
| 1605 | bool NewGVN::isCycleFree(const Instruction *I) const { |
| 1606 | // In order to compute cycle-freeness, we do SCC finding on the instruction, |
| 1607 | // and see what kind of SCC it ends up in. If it is a singleton, it is |
| 1608 | // cycle-free. If it is not in a singleton, it is only cycle free if the |
| 1609 | // other members are all phi nodes (as they do not compute anything, they are |
| 1610 | // copies). |
| 1611 | auto ICS = InstCycleState.lookup(I); |
| 1612 | if (ICS == ICS_Unknown) { |
| 1613 | SCCFinder.Start(I); |
| 1614 | auto &SCC = SCCFinder.getComponentFor(I); |
| Daniel Berlin | 2f72b19 | 2017-04-14 02:53:37 +0000 | [diff] [blame] | 1615 | // It's cycle free if it's size 1 or or the SCC is *only* phi nodes. |
| 1616 | if (SCC.size() == 1) |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 1617 | InstCycleState.insert({I, ICS_CycleFree}); |
| Daniel Berlin | 2f72b19 | 2017-04-14 02:53:37 +0000 | [diff] [blame] | 1618 | else { |
| Daniel Berlin | f9c9455 | 2017-09-05 02:17:43 +0000 | [diff] [blame^] | 1619 | bool AllPhis = llvm::all_of(SCC, [](const Value *V) { |
| 1620 | return isa<PHINode>(V) || isCopyOfAPHI(V); |
| 1621 | }); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 1622 | ICS = AllPhis ? ICS_CycleFree : ICS_Cycle; |
| Daniel Berlin | 2f72b19 | 2017-04-14 02:53:37 +0000 | [diff] [blame] | 1623 | for (auto *Member : SCC) |
| 1624 | if (auto *MemberPhi = dyn_cast<PHINode>(Member)) |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 1625 | InstCycleState.insert({MemberPhi, ICS}); |
| Daniel Berlin | 2f72b19 | 2017-04-14 02:53:37 +0000 | [diff] [blame] | 1626 | } |
| 1627 | } |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 1628 | if (ICS == ICS_Cycle) |
| Daniel Berlin | 2f72b19 | 2017-04-14 02:53:37 +0000 | [diff] [blame] | 1629 | return false; |
| 1630 | return true; |
| 1631 | } |
| 1632 | |
| Sanjay Patel | 7cf745c | 2017-08-03 15:18:27 +0000 | [diff] [blame] | 1633 | // Evaluate PHI nodes symbolically and create an expression result. |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 1634 | const Expression *NewGVN::performSymbolicPHIEvaluation(Instruction *I) const { |
| Daniel Berlin | 2f72b19 | 2017-04-14 02:53:37 +0000 | [diff] [blame] | 1635 | // True if one of the incoming phi edges is a backedge. |
| 1636 | bool HasBackedge = false; |
| 1637 | // All constant tracks the state of whether all the *original* phi operands |
| Daniel Berlin | e021d2d | 2017-05-19 20:22:20 +0000 | [diff] [blame] | 1638 | // This is really shorthand for "this phi cannot cycle due to forward |
| 1639 | // change in value of the phi is guaranteed not to later change the value of |
| 1640 | // the phi. IE it can't be v = phi(undef, v+1) |
| Daniel Berlin | f9c9455 | 2017-09-05 02:17:43 +0000 | [diff] [blame^] | 1641 | bool OriginalOpsConstant = true; |
| 1642 | auto *E = cast<PHIExpression>( |
| 1643 | createPHIExpression(I, HasBackedge, OriginalOpsConstant)); |
| Daniel Berlin | d92e7f9 | 2017-01-07 00:01:42 +0000 | [diff] [blame] | 1644 | // We match the semantics of SimplifyPhiNode from InstructionSimplify here. |
| Davide Italiano | 839c7e6 | 2017-05-02 21:11:40 +0000 | [diff] [blame] | 1645 | // See if all arguments are the same. |
| Daniel Berlin | d92e7f9 | 2017-01-07 00:01:42 +0000 | [diff] [blame] | 1646 | // We track if any were undef because they need special handling. |
| 1647 | bool HasUndef = false; |
| Daniel Berlin | e021d2d | 2017-05-19 20:22:20 +0000 | [diff] [blame] | 1648 | auto Filtered = make_filter_range(E->operands(), [&](Value *Arg) { |
| Daniel Berlin | d92e7f9 | 2017-01-07 00:01:42 +0000 | [diff] [blame] | 1649 | if (isa<UndefValue>(Arg)) { |
| 1650 | HasUndef = true; |
| 1651 | return false; |
| 1652 | } |
| 1653 | return true; |
| 1654 | }); |
| Daniel Berlin | e021d2d | 2017-05-19 20:22:20 +0000 | [diff] [blame] | 1655 | // If we are left with no operands, it's dead. |
| Daniel Berlin | d92e7f9 | 2017-01-07 00:01:42 +0000 | [diff] [blame] | 1656 | if (Filtered.begin() == Filtered.end()) { |
| Daniel Berlin | e67c322 | 2017-05-25 15:44:20 +0000 | [diff] [blame] | 1657 | // If it has undef at this point, it means there are no-non-undef arguments, |
| 1658 | // and thus, the value of the phi node must be undef. |
| 1659 | if (HasUndef) { |
| 1660 | DEBUG(dbgs() << "PHI Node " << *I |
| 1661 | << " has no non-undef arguments, valuing it as undef\n"); |
| 1662 | return createConstantExpression(UndefValue::get(I->getType())); |
| 1663 | } |
| 1664 | |
| Daniel Berlin | e021d2d | 2017-05-19 20:22:20 +0000 | [diff] [blame] | 1665 | DEBUG(dbgs() << "No arguments of PHI node " << *I << " are live\n"); |
| Daniel Berlin | 0e90011 | 2017-03-24 06:33:48 +0000 | [diff] [blame] | 1666 | deleteExpression(E); |
| Daniel Berlin | e021d2d | 2017-05-19 20:22:20 +0000 | [diff] [blame] | 1667 | return createDeadExpression(); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1668 | } |
| Daniel Berlin | d92e7f9 | 2017-01-07 00:01:42 +0000 | [diff] [blame] | 1669 | Value *AllSameValue = *(Filtered.begin()); |
| 1670 | ++Filtered.begin(); |
| 1671 | // Can't use std::equal here, sadly, because filter.begin moves. |
| Daniel Berlin | f9c9455 | 2017-09-05 02:17:43 +0000 | [diff] [blame^] | 1672 | if (llvm::all_of(Filtered, [&](Value *Arg) { return Arg == AllSameValue; })) { |
| Daniel Berlin | d92e7f9 | 2017-01-07 00:01:42 +0000 | [diff] [blame] | 1673 | // In LLVM's non-standard representation of phi nodes, it's possible to have |
| 1674 | // phi nodes with cycles (IE dependent on other phis that are .... dependent |
| 1675 | // on the original phi node), especially in weird CFG's where some arguments |
| 1676 | // are unreachable, or uninitialized along certain paths. This can cause |
| 1677 | // infinite loops during evaluation. We work around this by not trying to |
| 1678 | // really evaluate them independently, but instead using a variable |
| 1679 | // expression to say if one is equivalent to the other. |
| 1680 | // We also special case undef, so that if we have an undef, we can't use the |
| 1681 | // common value unless it dominates the phi block. |
| 1682 | if (HasUndef) { |
| Daniel Berlin | 2f72b19 | 2017-04-14 02:53:37 +0000 | [diff] [blame] | 1683 | // If we have undef and at least one other value, this is really a |
| 1684 | // multivalued phi, and we need to know if it's cycle free in order to |
| 1685 | // evaluate whether we can ignore the undef. The other parts of this are |
| 1686 | // just shortcuts. If there is no backedge, or all operands are |
| Daniel Berlin | f9c9455 | 2017-09-05 02:17:43 +0000 | [diff] [blame^] | 1687 | // constants, it also must be cycle free. |
| 1688 | if (HasBackedge && !OriginalOpsConstant && |
| Daniel Berlin | e67c322 | 2017-05-25 15:44:20 +0000 | [diff] [blame] | 1689 | !isa<UndefValue>(AllSameValue) && !isCycleFree(I)) |
| Daniel Berlin | 2f72b19 | 2017-04-14 02:53:37 +0000 | [diff] [blame] | 1690 | return E; |
| 1691 | |
| Daniel Berlin | d92e7f9 | 2017-01-07 00:01:42 +0000 | [diff] [blame] | 1692 | // Only have to check for instructions |
| Davide Italiano | 1b97fc3 | 2017-01-07 02:05:50 +0000 | [diff] [blame] | 1693 | if (auto *AllSameInst = dyn_cast<Instruction>(AllSameValue)) |
| Daniel Berlin | 9d0796e | 2017-03-24 05:30:34 +0000 | [diff] [blame] | 1694 | if (!someEquivalentDominates(AllSameInst, I)) |
| Daniel Berlin | d92e7f9 | 2017-01-07 00:01:42 +0000 | [diff] [blame] | 1695 | return E; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1696 | } |
| Daniel Berlin | eafdd86 | 2017-06-06 17:15:28 +0000 | [diff] [blame] | 1697 | // Can't simplify to something that comes later in the iteration. |
| 1698 | // Otherwise, when and if it changes congruence class, we will never catch |
| 1699 | // up. We will always be a class behind it. |
| 1700 | if (isa<Instruction>(AllSameValue) && |
| 1701 | InstrToDFSNum(AllSameValue) > InstrToDFSNum(I)) |
| 1702 | return E; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1703 | NumGVNPhisAllSame++; |
| 1704 | DEBUG(dbgs() << "Simplified PHI node " << *I << " to " << *AllSameValue |
| 1705 | << "\n"); |
| Daniel Berlin | 0e90011 | 2017-03-24 06:33:48 +0000 | [diff] [blame] | 1706 | deleteExpression(E); |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1707 | return createVariableOrConstant(AllSameValue); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1708 | } |
| 1709 | return E; |
| 1710 | } |
| 1711 | |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 1712 | const Expression * |
| 1713 | NewGVN::performSymbolicAggrValueEvaluation(Instruction *I) const { |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1714 | if (auto *EI = dyn_cast<ExtractValueInst>(I)) { |
| 1715 | auto *II = dyn_cast<IntrinsicInst>(EI->getAggregateOperand()); |
| 1716 | if (II && EI->getNumIndices() == 1 && *EI->idx_begin() == 0) { |
| 1717 | unsigned Opcode = 0; |
| 1718 | // EI might be an extract from one of our recognised intrinsics. If it |
| 1719 | // is we'll synthesize a semantically equivalent expression instead on |
| 1720 | // an extract value expression. |
| 1721 | switch (II->getIntrinsicID()) { |
| 1722 | case Intrinsic::sadd_with_overflow: |
| 1723 | case Intrinsic::uadd_with_overflow: |
| 1724 | Opcode = Instruction::Add; |
| 1725 | break; |
| 1726 | case Intrinsic::ssub_with_overflow: |
| 1727 | case Intrinsic::usub_with_overflow: |
| 1728 | Opcode = Instruction::Sub; |
| 1729 | break; |
| 1730 | case Intrinsic::smul_with_overflow: |
| 1731 | case Intrinsic::umul_with_overflow: |
| 1732 | Opcode = Instruction::Mul; |
| 1733 | break; |
| 1734 | default: |
| 1735 | break; |
| 1736 | } |
| 1737 | |
| 1738 | if (Opcode != 0) { |
| 1739 | // Intrinsic recognized. Grab its args to finish building the |
| 1740 | // expression. |
| 1741 | assert(II->getNumArgOperands() == 2 && |
| 1742 | "Expect two args for recognised intrinsics."); |
| Daniel Berlin | 54a92fc | 2017-09-05 02:17:42 +0000 | [diff] [blame] | 1743 | return createBinaryExpression(Opcode, EI->getType(), |
| 1744 | II->getArgOperand(0), |
| 1745 | II->getArgOperand(1), I); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1746 | } |
| 1747 | } |
| 1748 | } |
| 1749 | |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 1750 | return createAggregateValueExpression(I); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1751 | } |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 1752 | const Expression *NewGVN::performSymbolicCmpEvaluation(Instruction *I) const { |
| Chad Rosier | 4d85259 | 2017-08-08 18:41:49 +0000 | [diff] [blame] | 1753 | assert(isa<CmpInst>(I) && "Expected a cmp instruction."); |
| 1754 | |
| 1755 | auto *CI = cast<CmpInst>(I); |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1756 | // See if our operands are equal to those of a previous predicate, and if so, |
| 1757 | // if it implies true or false. |
| Daniel Berlin | c22aafe | 2017-01-31 22:31:58 +0000 | [diff] [blame] | 1758 | auto Op0 = lookupOperandLeader(CI->getOperand(0)); |
| 1759 | auto Op1 = lookupOperandLeader(CI->getOperand(1)); |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1760 | auto OurPredicate = CI->getPredicate(); |
| Daniel Berlin | 0350a87 | 2017-03-04 00:44:43 +0000 | [diff] [blame] | 1761 | if (shouldSwapOperands(Op0, Op1)) { |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1762 | std::swap(Op0, Op1); |
| 1763 | OurPredicate = CI->getSwappedPredicate(); |
| 1764 | } |
| 1765 | |
| Sanjay Patel | 7cf745c | 2017-08-03 15:18:27 +0000 | [diff] [blame] | 1766 | // Avoid processing the same info twice. |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1767 | const PredicateBase *LastPredInfo = nullptr; |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1768 | // See if we know something about the comparison itself, like it is the target |
| 1769 | // of an assume. |
| 1770 | auto *CmpPI = PredInfo->getPredicateInfoFor(I); |
| 1771 | if (dyn_cast_or_null<PredicateAssume>(CmpPI)) |
| 1772 | return createConstantExpression(ConstantInt::getTrue(CI->getType())); |
| 1773 | |
| Daniel Berlin | c22aafe | 2017-01-31 22:31:58 +0000 | [diff] [blame] | 1774 | if (Op0 == Op1) { |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1775 | // This condition does not depend on predicates, no need to add users |
| Daniel Berlin | c22aafe | 2017-01-31 22:31:58 +0000 | [diff] [blame] | 1776 | if (CI->isTrueWhenEqual()) |
| 1777 | return createConstantExpression(ConstantInt::getTrue(CI->getType())); |
| 1778 | else if (CI->isFalseWhenEqual()) |
| 1779 | return createConstantExpression(ConstantInt::getFalse(CI->getType())); |
| 1780 | } |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1781 | |
| 1782 | // NOTE: Because we are comparing both operands here and below, and using |
| 1783 | // previous comparisons, we rely on fact that predicateinfo knows to mark |
| 1784 | // comparisons that use renamed operands as users of the earlier comparisons. |
| 1785 | // It is *not* enough to just mark predicateinfo renamed operands as users of |
| 1786 | // the earlier comparisons, because the *other* operand may have changed in a |
| 1787 | // previous iteration. |
| 1788 | // Example: |
| 1789 | // icmp slt %a, %b |
| 1790 | // %b.0 = ssa.copy(%b) |
| 1791 | // false branch: |
| 1792 | // icmp slt %c, %b.0 |
| 1793 | |
| 1794 | // %c and %a may start out equal, and thus, the code below will say the second |
| 1795 | // %icmp is false. c may become equal to something else, and in that case the |
| 1796 | // %second icmp *must* be reexamined, but would not if only the renamed |
| 1797 | // %operands are considered users of the icmp. |
| 1798 | |
| 1799 | // *Currently* we only check one level of comparisons back, and only mark one |
| Sanjay Patel | 7cf745c | 2017-08-03 15:18:27 +0000 | [diff] [blame] | 1800 | // level back as touched when changes happen. If you modify this code to look |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1801 | // back farther through comparisons, you *must* mark the appropriate |
| 1802 | // comparisons as users in PredicateInfo.cpp, or you will cause bugs. See if |
| 1803 | // we know something just from the operands themselves |
| 1804 | |
| 1805 | // See if our operands have predicate info, so that we may be able to derive |
| 1806 | // something from a previous comparison. |
| 1807 | for (const auto &Op : CI->operands()) { |
| 1808 | auto *PI = PredInfo->getPredicateInfoFor(Op); |
| 1809 | if (const auto *PBranch = dyn_cast_or_null<PredicateBranch>(PI)) { |
| 1810 | if (PI == LastPredInfo) |
| 1811 | continue; |
| 1812 | LastPredInfo = PI; |
| Daniel Berlin | 8693210 | 2017-09-01 19:20:18 +0000 | [diff] [blame] | 1813 | // In phi of ops cases, we may have predicate info that we are evaluating |
| 1814 | // in a different context. |
| 1815 | if (!DT->dominates(PBranch->To, getBlockForValue(I))) |
| 1816 | continue; |
| 1817 | // TODO: Along the false edge, we may know more things too, like |
| 1818 | // icmp of |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1819 | // same operands is false. |
| Daniel Berlin | 8693210 | 2017-09-01 19:20:18 +0000 | [diff] [blame] | 1820 | // TODO: We only handle actual comparison conditions below, not |
| 1821 | // and/or. |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1822 | auto *BranchCond = dyn_cast<CmpInst>(PBranch->Condition); |
| 1823 | if (!BranchCond) |
| 1824 | continue; |
| 1825 | auto *BranchOp0 = lookupOperandLeader(BranchCond->getOperand(0)); |
| 1826 | auto *BranchOp1 = lookupOperandLeader(BranchCond->getOperand(1)); |
| 1827 | auto BranchPredicate = BranchCond->getPredicate(); |
| Daniel Berlin | 0350a87 | 2017-03-04 00:44:43 +0000 | [diff] [blame] | 1828 | if (shouldSwapOperands(BranchOp0, BranchOp1)) { |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1829 | std::swap(BranchOp0, BranchOp1); |
| 1830 | BranchPredicate = BranchCond->getSwappedPredicate(); |
| 1831 | } |
| 1832 | if (BranchOp0 == Op0 && BranchOp1 == Op1) { |
| 1833 | if (PBranch->TrueEdge) { |
| 1834 | // If we know the previous predicate is true and we are in the true |
| 1835 | // edge then we may be implied true or false. |
| Davide Italiano | 2dfd46b | 2017-05-01 22:26:28 +0000 | [diff] [blame] | 1836 | if (CmpInst::isImpliedTrueByMatchingCmp(BranchPredicate, |
| 1837 | OurPredicate)) { |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1838 | addPredicateUsers(PI, I); |
| 1839 | return createConstantExpression( |
| 1840 | ConstantInt::getTrue(CI->getType())); |
| 1841 | } |
| 1842 | |
| Davide Italiano | 2dfd46b | 2017-05-01 22:26:28 +0000 | [diff] [blame] | 1843 | if (CmpInst::isImpliedFalseByMatchingCmp(BranchPredicate, |
| 1844 | OurPredicate)) { |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1845 | addPredicateUsers(PI, I); |
| 1846 | return createConstantExpression( |
| 1847 | ConstantInt::getFalse(CI->getType())); |
| 1848 | } |
| 1849 | |
| 1850 | } else { |
| 1851 | // Just handle the ne and eq cases, where if we have the same |
| 1852 | // operands, we may know something. |
| 1853 | if (BranchPredicate == OurPredicate) { |
| 1854 | addPredicateUsers(PI, I); |
| 1855 | // Same predicate, same ops,we know it was false, so this is false. |
| 1856 | return createConstantExpression( |
| 1857 | ConstantInt::getFalse(CI->getType())); |
| 1858 | } else if (BranchPredicate == |
| 1859 | CmpInst::getInversePredicate(OurPredicate)) { |
| 1860 | addPredicateUsers(PI, I); |
| 1861 | // Inverse predicate, we know the other was false, so this is true. |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 1862 | return createConstantExpression( |
| 1863 | ConstantInt::getTrue(CI->getType())); |
| 1864 | } |
| 1865 | } |
| 1866 | } |
| 1867 | } |
| 1868 | } |
| 1869 | // Create expression will take care of simplifyCmpInst |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 1870 | return createExpression(I); |
| Daniel Berlin | c22aafe | 2017-01-31 22:31:58 +0000 | [diff] [blame] | 1871 | } |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1872 | |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 1873 | // Return true if V is a value that will always be available (IE can |
| 1874 | // be placed anywhere) in the function. We don't do globals here |
| 1875 | // because they are often worse to put in place. |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 1876 | static bool alwaysAvailable(Value *V) { |
| 1877 | return isa<Constant>(V) || isa<Argument>(V); |
| 1878 | } |
| 1879 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1880 | // Substitute and symbolize the value before value numbering. |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 1881 | const Expression * |
| 1882 | NewGVN::performSymbolicEvaluation(Value *V, |
| 1883 | SmallPtrSetImpl<Value *> &Visited) const { |
| Davide Italiano | 0e71480 | 2016-12-28 14:00:11 +0000 | [diff] [blame] | 1884 | const Expression *E = nullptr; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1885 | if (auto *C = dyn_cast<Constant>(V)) |
| 1886 | E = createConstantExpression(C); |
| 1887 | else if (isa<Argument>(V) || isa<GlobalVariable>(V)) { |
| 1888 | E = createVariableExpression(V); |
| 1889 | } else { |
| 1890 | // TODO: memory intrinsics. |
| 1891 | // TODO: Some day, we should do the forward propagation and reassociation |
| 1892 | // parts of the algorithm. |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 1893 | auto *I = cast<Instruction>(V); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1894 | switch (I->getOpcode()) { |
| 1895 | case Instruction::ExtractValue: |
| 1896 | case Instruction::InsertValue: |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 1897 | E = performSymbolicAggrValueEvaluation(I); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1898 | break; |
| 1899 | case Instruction::PHI: |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 1900 | E = performSymbolicPHIEvaluation(I); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1901 | break; |
| 1902 | case Instruction::Call: |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 1903 | E = performSymbolicCallEvaluation(I); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1904 | break; |
| 1905 | case Instruction::Store: |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 1906 | E = performSymbolicStoreEvaluation(I); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1907 | break; |
| 1908 | case Instruction::Load: |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 1909 | E = performSymbolicLoadEvaluation(I); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1910 | break; |
| 1911 | case Instruction::BitCast: { |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 1912 | E = createExpression(I); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1913 | } break; |
| Daniel Berlin | c22aafe | 2017-01-31 22:31:58 +0000 | [diff] [blame] | 1914 | case Instruction::ICmp: |
| 1915 | case Instruction::FCmp: { |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 1916 | E = performSymbolicCmpEvaluation(I); |
| Daniel Berlin | c22aafe | 2017-01-31 22:31:58 +0000 | [diff] [blame] | 1917 | } break; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1918 | case Instruction::Add: |
| 1919 | case Instruction::FAdd: |
| 1920 | case Instruction::Sub: |
| 1921 | case Instruction::FSub: |
| 1922 | case Instruction::Mul: |
| 1923 | case Instruction::FMul: |
| 1924 | case Instruction::UDiv: |
| 1925 | case Instruction::SDiv: |
| 1926 | case Instruction::FDiv: |
| 1927 | case Instruction::URem: |
| 1928 | case Instruction::SRem: |
| 1929 | case Instruction::FRem: |
| 1930 | case Instruction::Shl: |
| 1931 | case Instruction::LShr: |
| 1932 | case Instruction::AShr: |
| 1933 | case Instruction::And: |
| 1934 | case Instruction::Or: |
| 1935 | case Instruction::Xor: |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1936 | case Instruction::Trunc: |
| 1937 | case Instruction::ZExt: |
| 1938 | case Instruction::SExt: |
| 1939 | case Instruction::FPToUI: |
| 1940 | case Instruction::FPToSI: |
| 1941 | case Instruction::UIToFP: |
| 1942 | case Instruction::SIToFP: |
| 1943 | case Instruction::FPTrunc: |
| 1944 | case Instruction::FPExt: |
| 1945 | case Instruction::PtrToInt: |
| 1946 | case Instruction::IntToPtr: |
| 1947 | case Instruction::Select: |
| 1948 | case Instruction::ExtractElement: |
| 1949 | case Instruction::InsertElement: |
| 1950 | case Instruction::ShuffleVector: |
| 1951 | case Instruction::GetElementPtr: |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 1952 | E = createExpression(I); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1953 | break; |
| 1954 | default: |
| 1955 | return nullptr; |
| 1956 | } |
| 1957 | } |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1958 | return E; |
| 1959 | } |
| 1960 | |
| Daniel Berlin | 0207cca | 2017-05-21 23:41:56 +0000 | [diff] [blame] | 1961 | // Look up a container in a map, and then call a function for each thing in the |
| 1962 | // found container. |
| 1963 | template <typename Map, typename KeyType, typename Func> |
| 1964 | void NewGVN::for_each_found(Map &M, const KeyType &Key, Func F) { |
| 1965 | const auto Result = M.find_as(Key); |
| 1966 | if (Result != M.end()) |
| 1967 | for (typename Map::mapped_type::value_type Mapped : Result->second) |
| 1968 | F(Mapped); |
| 1969 | } |
| 1970 | |
| 1971 | // Look up a container of values/instructions in a map, and touch all the |
| 1972 | // instructions in the container. Then erase value from the map. |
| 1973 | template <typename Map, typename KeyType> |
| 1974 | void NewGVN::touchAndErase(Map &M, const KeyType &Key) { |
| 1975 | const auto Result = M.find_as(Key); |
| 1976 | if (Result != M.end()) { |
| 1977 | for (const typename Map::mapped_type::value_type Mapped : Result->second) |
| 1978 | TouchedInstructions.set(InstrToDFSNum(Mapped)); |
| 1979 | M.erase(Result); |
| 1980 | } |
| 1981 | } |
| 1982 | |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 1983 | void NewGVN::addAdditionalUsers(Value *To, Value *User) const { |
| Daniel Berlin | 54a92fc | 2017-09-05 02:17:42 +0000 | [diff] [blame] | 1984 | assert(User && To != User); |
| Daniel Berlin | be3e7ba | 2017-05-31 01:47:32 +0000 | [diff] [blame] | 1985 | if (isa<Instruction>(To)) |
| 1986 | AdditionalUsers[To].insert(User); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1989 | void NewGVN::markUsersTouched(Value *V) { |
| 1990 | // Now mark the users as touched. |
| Daniel Berlin | e0bd37e | 2016-12-29 22:15:12 +0000 | [diff] [blame] | 1991 | for (auto *User : V->users()) { |
| 1992 | assert(isa<Instruction>(User) && "Use of value not within an instruction?"); |
| Daniel Berlin | 21279bd | 2017-04-06 18:52:58 +0000 | [diff] [blame] | 1993 | TouchedInstructions.set(InstrToDFSNum(User)); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1994 | } |
| Daniel Berlin | 0207cca | 2017-05-21 23:41:56 +0000 | [diff] [blame] | 1995 | touchAndErase(AdditionalUsers, V); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 1998 | void NewGVN::addMemoryUsers(const MemoryAccess *To, MemoryAccess *U) const { |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 1999 | DEBUG(dbgs() << "Adding memory user " << *U << " to " << *To << "\n"); |
| 2000 | MemoryToUsers[To].insert(U); |
| 2001 | } |
| 2002 | |
| 2003 | void NewGVN::markMemoryDefTouched(const MemoryAccess *MA) { |
| Daniel Berlin | 21279bd | 2017-04-06 18:52:58 +0000 | [diff] [blame] | 2004 | TouchedInstructions.set(MemoryToDFSNum(MA)); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
| 2007 | void NewGVN::markMemoryUsersTouched(const MemoryAccess *MA) { |
| 2008 | if (isa<MemoryUse>(MA)) |
| 2009 | return; |
| 2010 | for (auto U : MA->users()) |
| Daniel Berlin | 21279bd | 2017-04-06 18:52:58 +0000 | [diff] [blame] | 2011 | TouchedInstructions.set(MemoryToDFSNum(U)); |
| Daniel Berlin | 0207cca | 2017-05-21 23:41:56 +0000 | [diff] [blame] | 2012 | touchAndErase(MemoryToUsers, MA); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 2015 | // Add I to the set of users of a given predicate. |
| Daniel Berlin | 6604a2f | 2017-05-09 16:40:04 +0000 | [diff] [blame] | 2016 | void NewGVN::addPredicateUsers(const PredicateBase *PB, Instruction *I) const { |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2017 | // Don't add temporary instructions to the user lists. |
| 2018 | if (AllTempInstructions.count(I)) |
| 2019 | return; |
| 2020 | |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 2021 | if (auto *PBranch = dyn_cast<PredicateBranch>(PB)) |
| 2022 | PredicateToUsers[PBranch->Condition].insert(I); |
| 2023 | else if (auto *PAssume = dyn_cast<PredicateBranch>(PB)) |
| 2024 | PredicateToUsers[PAssume->Condition].insert(I); |
| 2025 | } |
| 2026 | |
| 2027 | // Touch all the predicates that depend on this instruction. |
| 2028 | void NewGVN::markPredicateUsersTouched(Instruction *I) { |
| Daniel Berlin | 0207cca | 2017-05-21 23:41:56 +0000 | [diff] [blame] | 2029 | touchAndErase(PredicateToUsers, I); |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2032 | // Mark users affected by a memory leader change. |
| 2033 | void NewGVN::markMemoryLeaderChangeTouched(CongruenceClass *CC) { |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2034 | for (auto M : CC->memory()) |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2035 | markMemoryDefTouched(M); |
| 2036 | } |
| 2037 | |
| Daniel Berlin | 32f8d56 | 2017-01-07 16:55:14 +0000 | [diff] [blame] | 2038 | // Touch the instructions that need to be updated after a congruence class has a |
| 2039 | // leader change, and mark changed values. |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2040 | void NewGVN::markValueLeaderChangeTouched(CongruenceClass *CC) { |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2041 | for (auto M : *CC) { |
| Daniel Berlin | 32f8d56 | 2017-01-07 16:55:14 +0000 | [diff] [blame] | 2042 | if (auto *I = dyn_cast<Instruction>(M)) |
| Daniel Berlin | 21279bd | 2017-04-06 18:52:58 +0000 | [diff] [blame] | 2043 | TouchedInstructions.set(InstrToDFSNum(I)); |
| Daniel Berlin | 3a1bd02 | 2017-01-11 20:22:05 +0000 | [diff] [blame] | 2044 | LeaderChanges.insert(M); |
| 2045 | } |
| 2046 | } |
| 2047 | |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2048 | // Give a range of things that have instruction DFS numbers, this will return |
| 2049 | // the member of the range with the smallest dfs number. |
| 2050 | template <class T, class Range> |
| 2051 | T *NewGVN::getMinDFSOfRange(const Range &R) const { |
| 2052 | std::pair<T *, unsigned> MinDFS = {nullptr, ~0U}; |
| 2053 | for (const auto X : R) { |
| Daniel Berlin | 21279bd | 2017-04-06 18:52:58 +0000 | [diff] [blame] | 2054 | auto DFSNum = InstrToDFSNum(X); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2055 | if (DFSNum < MinDFS.second) |
| 2056 | MinDFS = {X, DFSNum}; |
| 2057 | } |
| 2058 | return MinDFS.first; |
| 2059 | } |
| 2060 | |
| 2061 | // This function returns the MemoryAccess that should be the next leader of |
| 2062 | // congruence class CC, under the assumption that the current leader is going to |
| 2063 | // disappear. |
| 2064 | const MemoryAccess *NewGVN::getNextMemoryLeader(CongruenceClass *CC) const { |
| 2065 | // TODO: If this ends up to slow, we can maintain a next memory leader like we |
| 2066 | // do for regular leaders. |
| Daniel Berlin | de269f4 | 2017-08-26 07:37:11 +0000 | [diff] [blame] | 2067 | // Make sure there will be a leader to find. |
| Davide Italiano | dc43532 | 2017-05-10 19:57:43 +0000 | [diff] [blame] | 2068 | assert(!CC->definesNoMemory() && "Can't get next leader if there is none"); |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2069 | if (CC->getStoreCount() > 0) { |
| 2070 | if (auto *NL = dyn_cast_or_null<StoreInst>(CC->getNextLeader().first)) |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2071 | return getMemoryAccess(NL); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2072 | // Find the store with the minimum DFS number. |
| 2073 | auto *V = getMinDFSOfRange<Value>(make_filter_range( |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2074 | *CC, [&](const Value *V) { return isa<StoreInst>(V); })); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2075 | return getMemoryAccess(cast<StoreInst>(V)); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2076 | } |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2077 | assert(CC->getStoreCount() == 0); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2078 | |
| 2079 | // Given our assertion, hitting this part must mean |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2080 | // !OldClass->memory_empty() |
| 2081 | if (CC->memory_size() == 1) |
| 2082 | return *CC->memory_begin(); |
| 2083 | return getMinDFSOfRange<const MemoryPhi>(CC->memory()); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | // This function returns the next value leader of a congruence class, under the |
| 2087 | // assumption that the current leader is going away. This should end up being |
| 2088 | // the next most dominating member. |
| 2089 | Value *NewGVN::getNextValueLeader(CongruenceClass *CC) const { |
| 2090 | // We don't need to sort members if there is only 1, and we don't care about |
| 2091 | // sorting the TOP class because everything either gets out of it or is |
| 2092 | // unreachable. |
| 2093 | |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2094 | if (CC->size() == 1 || CC == TOPClass) { |
| 2095 | return *(CC->begin()); |
| 2096 | } else if (CC->getNextLeader().first) { |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2097 | ++NumGVNAvoidedSortedLeaderChanges; |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2098 | return CC->getNextLeader().first; |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2099 | } else { |
| 2100 | ++NumGVNSortedLeaderChanges; |
| 2101 | // NOTE: If this ends up to slow, we can maintain a dual structure for |
| 2102 | // member testing/insertion, or keep things mostly sorted, and sort only |
| 2103 | // here, or use SparseBitVector or .... |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2104 | return getMinDFSOfRange<Value>(*CC); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2105 | } |
| 2106 | } |
| 2107 | |
| 2108 | // Move a MemoryAccess, currently in OldClass, to NewClass, including updates to |
| 2109 | // the memory members, etc for the move. |
| 2110 | // |
| 2111 | // The invariants of this function are: |
| 2112 | // |
| Davide Italiano | fb4544c | 2017-07-11 19:15:36 +0000 | [diff] [blame] | 2113 | // - I must be moving to NewClass from OldClass |
| 2114 | // - The StoreCount of OldClass and NewClass is expected to have been updated |
| 2115 | // for I already if it is is a store. |
| 2116 | // - The OldClass memory leader has not been updated yet if I was the leader. |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2117 | void NewGVN::moveMemoryToNewCongruenceClass(Instruction *I, |
| 2118 | MemoryAccess *InstMA, |
| 2119 | CongruenceClass *OldClass, |
| 2120 | CongruenceClass *NewClass) { |
| 2121 | // If the leader is I, and we had a represenative MemoryAccess, it should |
| 2122 | // be the MemoryAccess of OldClass. |
| Davide Italiano | f58a3023 | 2017-04-10 23:08:35 +0000 | [diff] [blame] | 2123 | assert((!InstMA || !OldClass->getMemoryLeader() || |
| 2124 | OldClass->getLeader() != I || |
| Davide Italiano | ee1c821 | 2017-07-11 19:49:12 +0000 | [diff] [blame] | 2125 | MemoryAccessToClass.lookup(OldClass->getMemoryLeader()) == |
| 2126 | MemoryAccessToClass.lookup(InstMA)) && |
| Davide Italiano | f58a3023 | 2017-04-10 23:08:35 +0000 | [diff] [blame] | 2127 | "Representative MemoryAccess mismatch"); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2128 | // First, see what happens to the new class |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2129 | if (!NewClass->getMemoryLeader()) { |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2130 | // Should be a new class, or a store becoming a leader of a new class. |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2131 | assert(NewClass->size() == 1 || |
| 2132 | (isa<StoreInst>(I) && NewClass->getStoreCount() == 1)); |
| 2133 | NewClass->setMemoryLeader(InstMA); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2134 | // Mark it touched if we didn't just create a singleton |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2135 | DEBUG(dbgs() << "Memory class leader change for class " << NewClass->getID() |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2136 | << " due to new memory instruction becoming leader\n"); |
| 2137 | markMemoryLeaderChangeTouched(NewClass); |
| 2138 | } |
| 2139 | setMemoryClass(InstMA, NewClass); |
| 2140 | // Now, fixup the old class if necessary |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2141 | if (OldClass->getMemoryLeader() == InstMA) { |
| Davide Italiano | dc43532 | 2017-05-10 19:57:43 +0000 | [diff] [blame] | 2142 | if (!OldClass->definesNoMemory()) { |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2143 | OldClass->setMemoryLeader(getNextMemoryLeader(OldClass)); |
| 2144 | DEBUG(dbgs() << "Memory class leader change for class " |
| 2145 | << OldClass->getID() << " to " |
| 2146 | << *OldClass->getMemoryLeader() |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2147 | << " due to removal of old leader " << *InstMA << "\n"); |
| 2148 | markMemoryLeaderChangeTouched(OldClass); |
| 2149 | } else |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2150 | OldClass->setMemoryLeader(nullptr); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2151 | } |
| 2152 | } |
| 2153 | |
| Daniel Berlin | 3a1bd02 | 2017-01-11 20:22:05 +0000 | [diff] [blame] | 2154 | // Move a value, currently in OldClass, to be part of NewClass |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2155 | // Update OldClass and NewClass for the move (including changing leaders, etc). |
| 2156 | void NewGVN::moveValueToNewCongruenceClass(Instruction *I, const Expression *E, |
| Daniel Berlin | c0431fd | 2017-01-13 22:40:01 +0000 | [diff] [blame] | 2157 | CongruenceClass *OldClass, |
| Daniel Berlin | 3a1bd02 | 2017-01-11 20:22:05 +0000 | [diff] [blame] | 2158 | CongruenceClass *NewClass) { |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2159 | if (I == OldClass->getNextLeader().first) |
| 2160 | OldClass->resetNextLeader(); |
| Daniel Berlin | c0431fd | 2017-01-13 22:40:01 +0000 | [diff] [blame] | 2161 | |
| Daniel Berlin | ff15200 | 2017-05-19 19:01:24 +0000 | [diff] [blame] | 2162 | OldClass->erase(I); |
| 2163 | NewClass->insert(I); |
| Daniel Berlin | c0431fd | 2017-01-13 22:40:01 +0000 | [diff] [blame] | 2164 | |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2165 | if (NewClass->getLeader() != I) |
| 2166 | NewClass->addPossibleNextLeader({I, InstrToDFSNum(I)}); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2167 | // Handle our special casing of stores. |
| Daniel Berlin | 1ea5f32 | 2017-01-26 22:21:48 +0000 | [diff] [blame] | 2168 | if (auto *SI = dyn_cast<StoreInst>(I)) { |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2169 | OldClass->decStoreCount(); |
| 2170 | // Okay, so when do we want to make a store a leader of a class? |
| 2171 | // If we have a store defined by an earlier load, we want the earlier load |
| 2172 | // to lead the class. |
| 2173 | // If we have a store defined by something else, we want the store to lead |
| 2174 | // the class so everything else gets the "something else" as a value. |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2175 | // If we have a store as the single member of the class, we want the store |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2176 | // as the leader |
| 2177 | if (NewClass->getStoreCount() == 0 && !NewClass->getStoredValue()) { |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2178 | // If it's a store expression we are using, it means we are not equivalent |
| 2179 | // to something earlier. |
| Daniel Berlin | 629e1ff | 2017-05-16 06:06:15 +0000 | [diff] [blame] | 2180 | if (auto *SE = dyn_cast<StoreExpression>(E)) { |
| Daniel Berlin | 629e1ff | 2017-05-16 06:06:15 +0000 | [diff] [blame] | 2181 | NewClass->setStoredValue(SE->getStoredValue()); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2182 | markValueLeaderChangeTouched(NewClass); |
| 2183 | // Shift the new class leader to be the store |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2184 | DEBUG(dbgs() << "Changing leader of congruence class " |
| 2185 | << NewClass->getID() << " from " << *NewClass->getLeader() |
| 2186 | << " to " << *SI << " because store joined class\n"); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2187 | // If we changed the leader, we have to mark it changed because we don't |
| Davide Italiano | 67b0e53 | 2017-07-11 19:19:45 +0000 | [diff] [blame] | 2188 | // know what it will do to symbolic evaluation. |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2189 | NewClass->setLeader(SI); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2190 | } |
| 2191 | // We rely on the code below handling the MemoryAccess change. |
| 2192 | } |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2193 | NewClass->incStoreCount(); |
| Daniel Berlin | 3a1bd02 | 2017-01-11 20:22:05 +0000 | [diff] [blame] | 2194 | } |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2195 | // True if there is no memory instructions left in a class that had memory |
| 2196 | // instructions before. |
| Daniel Berlin | 3a1bd02 | 2017-01-11 20:22:05 +0000 | [diff] [blame] | 2197 | |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2198 | // If it's not a memory use, set the MemoryAccess equivalence |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2199 | auto *InstMA = dyn_cast_or_null<MemoryDef>(getMemoryAccess(I)); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2200 | if (InstMA) |
| 2201 | moveMemoryToNewCongruenceClass(I, InstMA, OldClass, NewClass); |
| Daniel Berlin | c0431fd | 2017-01-13 22:40:01 +0000 | [diff] [blame] | 2202 | ValueToClass[I] = NewClass; |
| Daniel Berlin | 3a1bd02 | 2017-01-11 20:22:05 +0000 | [diff] [blame] | 2203 | // See if we destroyed the class or need to swap leaders. |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2204 | if (OldClass->empty() && OldClass != TOPClass) { |
| 2205 | if (OldClass->getDefiningExpr()) { |
| Daniel Berlin | 629e1ff | 2017-05-16 06:06:15 +0000 | [diff] [blame] | 2206 | DEBUG(dbgs() << "Erasing expression " << *OldClass->getDefiningExpr() |
| Daniel Berlin | 3a1bd02 | 2017-01-11 20:22:05 +0000 | [diff] [blame] | 2207 | << " from table\n"); |
| Daniel Berlin | eafdd86 | 2017-06-06 17:15:28 +0000 | [diff] [blame] | 2208 | // We erase it as an exact expression to make sure we don't just erase an |
| 2209 | // equivalent one. |
| 2210 | auto Iter = ExpressionToClass.find_as( |
| 2211 | ExactEqualsExpression(*OldClass->getDefiningExpr())); |
| 2212 | if (Iter != ExpressionToClass.end()) |
| 2213 | ExpressionToClass.erase(Iter); |
| 2214 | #ifdef EXPENSIVE_CHECKS |
| 2215 | assert( |
| 2216 | (*OldClass->getDefiningExpr() != *E || ExpressionToClass.lookup(E)) && |
| 2217 | "We erased the expression we just inserted, which should not happen"); |
| 2218 | #endif |
| Daniel Berlin | 3a1bd02 | 2017-01-11 20:22:05 +0000 | [diff] [blame] | 2219 | } |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2220 | } else if (OldClass->getLeader() == I) { |
| Daniel Berlin | 3a1bd02 | 2017-01-11 20:22:05 +0000 | [diff] [blame] | 2221 | // When the leader changes, the value numbering of |
| 2222 | // everything may change due to symbolization changes, so we need to |
| 2223 | // reprocess. |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2224 | DEBUG(dbgs() << "Value class leader change for class " << OldClass->getID() |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2225 | << "\n"); |
| Daniel Berlin | c0431fd | 2017-01-13 22:40:01 +0000 | [diff] [blame] | 2226 | ++NumGVNLeaderChanges; |
| Daniel Berlin | 26addef | 2017-01-20 21:04:30 +0000 | [diff] [blame] | 2227 | // Destroy the stored value if there are no more stores to represent it. |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2228 | // Note that this is basically clean up for the expression removal that |
| 2229 | // happens below. If we remove stores from a class, we may leave it as a |
| 2230 | // class of equivalent memory phis. |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2231 | if (OldClass->getStoreCount() == 0) { |
| 2232 | if (OldClass->getStoredValue()) |
| 2233 | OldClass->setStoredValue(nullptr); |
| Daniel Berlin | 1ea5f32 | 2017-01-26 22:21:48 +0000 | [diff] [blame] | 2234 | } |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2235 | OldClass->setLeader(getNextValueLeader(OldClass)); |
| 2236 | OldClass->resetNextLeader(); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2237 | markValueLeaderChangeTouched(OldClass); |
| Daniel Berlin | 32f8d56 | 2017-01-07 16:55:14 +0000 | [diff] [blame] | 2238 | } |
| 2239 | } |
| 2240 | |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2241 | // For a given expression, mark the phi of ops instructions that could have |
| 2242 | // changed as a result. |
| Daniel Berlin | 2aa5dc1 | 2017-05-30 06:58:18 +0000 | [diff] [blame] | 2243 | void NewGVN::markPhiOfOpsChanged(const Expression *E) { |
| Daniel Berlin | 51e878e | 2017-06-14 21:19:28 +0000 | [diff] [blame] | 2244 | touchAndErase(ExpressionToPhiOfOps, ExactEqualsExpression(*E)); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2245 | } |
| Daniel Berlin | 0207cca | 2017-05-21 23:41:56 +0000 | [diff] [blame] | 2246 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2247 | // Perform congruence finding on a given value numbering expression. |
| Daniel Berlin | c0431fd | 2017-01-13 22:40:01 +0000 | [diff] [blame] | 2248 | void NewGVN::performCongruenceFinding(Instruction *I, const Expression *E) { |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2249 | // This is guaranteed to return something, since it will at least find |
| Daniel Berlin | b79f536 | 2017-02-11 12:48:50 +0000 | [diff] [blame] | 2250 | // TOP. |
| Daniel Berlin | e021d2d | 2017-05-19 20:22:20 +0000 | [diff] [blame] | 2251 | |
| 2252 | CongruenceClass *IClass = ValueToClass.lookup(I); |
| Daniel Berlin | c0431fd | 2017-01-13 22:40:01 +0000 | [diff] [blame] | 2253 | assert(IClass && "Should have found a IClass"); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2254 | // Dead classes should have been eliminated from the mapping. |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2255 | assert(!IClass->isDead() && "Found a dead class"); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2256 | |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2257 | CongruenceClass *EClass = nullptr; |
| Daniel Berlin | 02c6b17 | 2017-01-02 18:00:53 +0000 | [diff] [blame] | 2258 | if (const auto *VE = dyn_cast<VariableExpression>(E)) { |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2259 | EClass = ValueToClass.lookup(VE->getVariableValue()); |
| Daniel Berlin | e021d2d | 2017-05-19 20:22:20 +0000 | [diff] [blame] | 2260 | } else if (isa<DeadExpression>(E)) { |
| 2261 | EClass = TOPClass; |
| 2262 | } |
| 2263 | if (!EClass) { |
| Daniel Berlin | 2aa5dc1 | 2017-05-30 06:58:18 +0000 | [diff] [blame] | 2264 | auto lookupResult = ExpressionToClass.insert({E, nullptr}); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2265 | |
| 2266 | // If it's not in the value table, create a new congruence class. |
| 2267 | if (lookupResult.second) { |
| Davide Italiano | 0e71480 | 2016-12-28 14:00:11 +0000 | [diff] [blame] | 2268 | CongruenceClass *NewClass = createCongruenceClass(nullptr, E); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2269 | auto place = lookupResult.first; |
| 2270 | place->second = NewClass; |
| 2271 | |
| 2272 | // Constants and variables should always be made the leader. |
| Daniel Berlin | 32f8d56 | 2017-01-07 16:55:14 +0000 | [diff] [blame] | 2273 | if (const auto *CE = dyn_cast<ConstantExpression>(E)) { |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2274 | NewClass->setLeader(CE->getConstantValue()); |
| Daniel Berlin | 32f8d56 | 2017-01-07 16:55:14 +0000 | [diff] [blame] | 2275 | } else if (const auto *SE = dyn_cast<StoreExpression>(E)) { |
| 2276 | StoreInst *SI = SE->getStoreInst(); |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2277 | NewClass->setLeader(SI); |
| Daniel Berlin | 629e1ff | 2017-05-16 06:06:15 +0000 | [diff] [blame] | 2278 | NewClass->setStoredValue(SE->getStoredValue()); |
| Daniel Berlin | 1ea5f32 | 2017-01-26 22:21:48 +0000 | [diff] [blame] | 2279 | // The RepMemoryAccess field will be filled in properly by the |
| 2280 | // moveValueToNewCongruenceClass call. |
| Daniel Berlin | 32f8d56 | 2017-01-07 16:55:14 +0000 | [diff] [blame] | 2281 | } else { |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2282 | NewClass->setLeader(I); |
| Daniel Berlin | 32f8d56 | 2017-01-07 16:55:14 +0000 | [diff] [blame] | 2283 | } |
| 2284 | assert(!isa<VariableExpression>(E) && |
| 2285 | "VariableExpression should have been handled already"); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2286 | |
| 2287 | EClass = NewClass; |
| Daniel Berlin | c0431fd | 2017-01-13 22:40:01 +0000 | [diff] [blame] | 2288 | DEBUG(dbgs() << "Created new congruence class for " << *I |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2289 | << " using expression " << *E << " at " << NewClass->getID() |
| 2290 | << " and leader " << *(NewClass->getLeader())); |
| 2291 | if (NewClass->getStoredValue()) |
| 2292 | DEBUG(dbgs() << " and stored value " << *(NewClass->getStoredValue())); |
| Daniel Berlin | 26addef | 2017-01-20 21:04:30 +0000 | [diff] [blame] | 2293 | DEBUG(dbgs() << "\n"); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2294 | } else { |
| 2295 | EClass = lookupResult.first->second; |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 2296 | if (isa<ConstantExpression>(E)) |
| Davide Italiano | f58a3023 | 2017-04-10 23:08:35 +0000 | [diff] [blame] | 2297 | assert((isa<Constant>(EClass->getLeader()) || |
| 2298 | (EClass->getStoredValue() && |
| 2299 | isa<Constant>(EClass->getStoredValue()))) && |
| 2300 | "Any class with a constant expression should have a " |
| 2301 | "constant leader"); |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 2302 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2303 | assert(EClass && "Somehow don't have an eclass"); |
| 2304 | |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2305 | assert(!EClass->isDead() && "We accidentally looked up a dead class"); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2306 | } |
| 2307 | } |
| Daniel Berlin | c0431fd | 2017-01-13 22:40:01 +0000 | [diff] [blame] | 2308 | bool ClassChanged = IClass != EClass; |
| 2309 | bool LeaderChanged = LeaderChanges.erase(I); |
| Daniel Berlin | 3a1bd02 | 2017-01-11 20:22:05 +0000 | [diff] [blame] | 2310 | if (ClassChanged || LeaderChanged) { |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2311 | DEBUG(dbgs() << "New class " << EClass->getID() << " for expression " << *E |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2312 | << "\n"); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2313 | if (ClassChanged) { |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2314 | moveValueToNewCongruenceClass(I, E, IClass, EClass); |
| Daniel Berlin | 2aa5dc1 | 2017-05-30 06:58:18 +0000 | [diff] [blame] | 2315 | markPhiOfOpsChanged(E); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
| Daniel Berlin | c0431fd | 2017-01-13 22:40:01 +0000 | [diff] [blame] | 2318 | markUsersTouched(I); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2319 | if (MemoryAccess *MA = getMemoryAccess(I)) |
| Daniel Berlin | c0431fd | 2017-01-13 22:40:01 +0000 | [diff] [blame] | 2320 | markMemoryUsersTouched(MA); |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 2321 | if (auto *CI = dyn_cast<CmpInst>(I)) |
| 2322 | markPredicateUsersTouched(CI); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2323 | } |
| Daniel Berlin | 4540357 | 2017-05-16 19:58:47 +0000 | [diff] [blame] | 2324 | // If we changed the class of the store, we want to ensure nothing finds the |
| 2325 | // old store expression. In particular, loads do not compare against stored |
| 2326 | // value, so they will find old store expressions (and associated class |
| 2327 | // mappings) if we leave them in the table. |
| Davide Italiano | ee49f49 | 2017-05-19 04:06:10 +0000 | [diff] [blame] | 2328 | if (ClassChanged && isa<StoreInst>(I)) { |
| Daniel Berlin | 4540357 | 2017-05-16 19:58:47 +0000 | [diff] [blame] | 2329 | auto *OldE = ValueToExpression.lookup(I); |
| 2330 | // It could just be that the old class died. We don't want to erase it if we |
| 2331 | // just moved classes. |
| Daniel Berlin | eafdd86 | 2017-06-06 17:15:28 +0000 | [diff] [blame] | 2332 | if (OldE && isa<StoreExpression>(OldE) && *E != *OldE) { |
| 2333 | // Erase this as an exact expression to ensure we don't erase expressions |
| 2334 | // equivalent to it. |
| 2335 | auto Iter = ExpressionToClass.find_as(ExactEqualsExpression(*OldE)); |
| 2336 | if (Iter != ExpressionToClass.end()) |
| 2337 | ExpressionToClass.erase(Iter); |
| 2338 | } |
| Daniel Berlin | 4540357 | 2017-05-16 19:58:47 +0000 | [diff] [blame] | 2339 | } |
| 2340 | ValueToExpression[I] = E; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2341 | } |
| 2342 | |
| 2343 | // Process the fact that Edge (from, to) is reachable, including marking |
| 2344 | // any newly reachable blocks and instructions for processing. |
| 2345 | void NewGVN::updateReachableEdge(BasicBlock *From, BasicBlock *To) { |
| 2346 | // Check if the Edge was reachable before. |
| 2347 | if (ReachableEdges.insert({From, To}).second) { |
| 2348 | // If this block wasn't reachable before, all instructions are touched. |
| 2349 | if (ReachableBlocks.insert(To).second) { |
| 2350 | DEBUG(dbgs() << "Block " << getBlockName(To) << " marked reachable\n"); |
| 2351 | const auto &InstRange = BlockInstRange.lookup(To); |
| 2352 | TouchedInstructions.set(InstRange.first, InstRange.second); |
| 2353 | } else { |
| 2354 | DEBUG(dbgs() << "Block " << getBlockName(To) |
| 2355 | << " was reachable, but new edge {" << getBlockName(From) |
| 2356 | << "," << getBlockName(To) << "} to it found\n"); |
| 2357 | |
| 2358 | // We've made an edge reachable to an existing block, which may |
| 2359 | // impact predicates. Otherwise, only mark the phi nodes as touched, as |
| 2360 | // they are the only thing that depend on new edges. Anything using their |
| 2361 | // values will get propagated to if necessary. |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2362 | if (MemoryAccess *MemPhi = getMemoryAccess(To)) |
| Daniel Berlin | 21279bd | 2017-04-06 18:52:58 +0000 | [diff] [blame] | 2363 | TouchedInstructions.set(InstrToDFSNum(MemPhi)); |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 2364 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2365 | auto BI = To->begin(); |
| 2366 | while (isa<PHINode>(BI)) { |
| Daniel Berlin | 21279bd | 2017-04-06 18:52:58 +0000 | [diff] [blame] | 2367 | TouchedInstructions.set(InstrToDFSNum(&*BI)); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2368 | ++BI; |
| 2369 | } |
| Daniel Berlin | 0207cca | 2017-05-21 23:41:56 +0000 | [diff] [blame] | 2370 | for_each_found(PHIOfOpsPHIs, To, [&](const PHINode *I) { |
| 2371 | TouchedInstructions.set(InstrToDFSNum(I)); |
| 2372 | }); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2373 | } |
| 2374 | } |
| 2375 | } |
| 2376 | |
| 2377 | // Given a predicate condition (from a switch, cmp, or whatever) and a block, |
| 2378 | // see if we know some constant value for it already. |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 2379 | Value *NewGVN::findConditionEquivalence(Value *Cond) const { |
| Daniel Berlin | 203f47b | 2017-01-31 22:31:53 +0000 | [diff] [blame] | 2380 | auto Result = lookupOperandLeader(Cond); |
| Davide Italiano | daa9c0e | 2017-06-19 16:46:15 +0000 | [diff] [blame] | 2381 | return isa<Constant>(Result) ? Result : nullptr; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | // Process the outgoing edges of a block for reachability. |
| 2385 | void NewGVN::processOutgoingEdges(TerminatorInst *TI, BasicBlock *B) { |
| 2386 | // Evaluate reachability of terminator instruction. |
| 2387 | BranchInst *BR; |
| 2388 | if ((BR = dyn_cast<BranchInst>(TI)) && BR->isConditional()) { |
| 2389 | Value *Cond = BR->getCondition(); |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 2390 | Value *CondEvaluated = findConditionEquivalence(Cond); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2391 | if (!CondEvaluated) { |
| 2392 | if (auto *I = dyn_cast<Instruction>(Cond)) { |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 2393 | const Expression *E = createExpression(I); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2394 | if (const auto *CE = dyn_cast<ConstantExpression>(E)) { |
| 2395 | CondEvaluated = CE->getConstantValue(); |
| 2396 | } |
| 2397 | } else if (isa<ConstantInt>(Cond)) { |
| 2398 | CondEvaluated = Cond; |
| 2399 | } |
| 2400 | } |
| 2401 | ConstantInt *CI; |
| 2402 | BasicBlock *TrueSucc = BR->getSuccessor(0); |
| 2403 | BasicBlock *FalseSucc = BR->getSuccessor(1); |
| 2404 | if (CondEvaluated && (CI = dyn_cast<ConstantInt>(CondEvaluated))) { |
| 2405 | if (CI->isOne()) { |
| 2406 | DEBUG(dbgs() << "Condition for Terminator " << *TI |
| 2407 | << " evaluated to true\n"); |
| 2408 | updateReachableEdge(B, TrueSucc); |
| 2409 | } else if (CI->isZero()) { |
| 2410 | DEBUG(dbgs() << "Condition for Terminator " << *TI |
| 2411 | << " evaluated to false\n"); |
| 2412 | updateReachableEdge(B, FalseSucc); |
| 2413 | } |
| 2414 | } else { |
| 2415 | updateReachableEdge(B, TrueSucc); |
| 2416 | updateReachableEdge(B, FalseSucc); |
| 2417 | } |
| 2418 | } else if (auto *SI = dyn_cast<SwitchInst>(TI)) { |
| 2419 | // For switches, propagate the case values into the case |
| 2420 | // destinations. |
| 2421 | |
| 2422 | // Remember how many outgoing edges there are to every successor. |
| 2423 | SmallDenseMap<BasicBlock *, unsigned, 16> SwitchEdges; |
| 2424 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2425 | Value *SwitchCond = SI->getCondition(); |
| Daniel Berlin | 97718e6 | 2017-01-31 22:32:03 +0000 | [diff] [blame] | 2426 | Value *CondEvaluated = findConditionEquivalence(SwitchCond); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2427 | // See if we were able to turn this switch statement into a constant. |
| 2428 | if (CondEvaluated && isa<ConstantInt>(CondEvaluated)) { |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 2429 | auto *CondVal = cast<ConstantInt>(CondEvaluated); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2430 | // We should be able to get case value for this. |
| Chandler Carruth | 927d8e6 | 2017-04-12 07:27:28 +0000 | [diff] [blame] | 2431 | auto Case = *SI->findCaseValue(CondVal); |
| 2432 | if (Case.getCaseSuccessor() == SI->getDefaultDest()) { |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2433 | // We proved the value is outside of the range of the case. |
| 2434 | // We can't do anything other than mark the default dest as reachable, |
| 2435 | // and go home. |
| 2436 | updateReachableEdge(B, SI->getDefaultDest()); |
| 2437 | return; |
| 2438 | } |
| 2439 | // Now get where it goes and mark it reachable. |
| Chandler Carruth | 927d8e6 | 2017-04-12 07:27:28 +0000 | [diff] [blame] | 2440 | BasicBlock *TargetBlock = Case.getCaseSuccessor(); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2441 | updateReachableEdge(B, TargetBlock); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2442 | } else { |
| 2443 | for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) { |
| 2444 | BasicBlock *TargetBlock = SI->getSuccessor(i); |
| 2445 | ++SwitchEdges[TargetBlock]; |
| 2446 | updateReachableEdge(B, TargetBlock); |
| 2447 | } |
| 2448 | } |
| 2449 | } else { |
| 2450 | // Otherwise this is either unconditional, or a type we have no |
| 2451 | // idea about. Just mark successors as reachable. |
| 2452 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) { |
| 2453 | BasicBlock *TargetBlock = TI->getSuccessor(i); |
| 2454 | updateReachableEdge(B, TargetBlock); |
| 2455 | } |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 2456 | |
| 2457 | // This also may be a memory defining terminator, in which case, set it |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2458 | // equivalent only to itself. |
| 2459 | // |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2460 | auto *MA = getMemoryAccess(TI); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2461 | if (MA && !isa<MemoryUse>(MA)) { |
| 2462 | auto *CC = ensureLeaderOfMemoryClass(MA); |
| 2463 | if (setMemoryClass(MA, CC)) |
| 2464 | markMemoryUsersTouched(MA); |
| 2465 | } |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2466 | } |
| 2467 | } |
| 2468 | |
| Davide Italiano | 5974c31 | 2017-08-03 21:17:49 +0000 | [diff] [blame] | 2469 | // Remove the PHI of Ops PHI for I |
| 2470 | void NewGVN::removePhiOfOps(Instruction *I, PHINode *PHITemp) { |
| 2471 | InstrDFS.erase(PHITemp); |
| 2472 | // It's still a temp instruction. We keep it in the array so it gets erased. |
| 2473 | // However, it's no longer used by I, or in the block/ |
| 2474 | PHIOfOpsPHIs[getBlockForValue(PHITemp)].erase(PHITemp); |
| 2475 | TempToBlock.erase(PHITemp); |
| 2476 | RealToTemp.erase(I); |
| 2477 | } |
| 2478 | |
| 2479 | // Add PHI Op in BB as a PHI of operations version of ExistingValue. |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2480 | void NewGVN::addPhiOfOps(PHINode *Op, BasicBlock *BB, |
| 2481 | Instruction *ExistingValue) { |
| 2482 | InstrDFS[Op] = InstrToDFSNum(ExistingValue); |
| 2483 | AllTempInstructions.insert(Op); |
| Davide Italiano | 5974c31 | 2017-08-03 21:17:49 +0000 | [diff] [blame] | 2484 | PHIOfOpsPHIs[BB].insert(Op); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2485 | TempToBlock[Op] = BB; |
| Daniel Berlin | b779db7 | 2017-06-29 17:01:10 +0000 | [diff] [blame] | 2486 | RealToTemp[ExistingValue] = Op; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | static bool okayForPHIOfOps(const Instruction *I) { |
| Chad Rosier | a5508e3 | 2017-08-10 14:12:57 +0000 | [diff] [blame] | 2490 | if (!EnablePhiOfOps) |
| 2491 | return false; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2492 | return isa<BinaryOperator>(I) || isa<SelectInst>(I) || isa<CmpInst>(I) || |
| 2493 | isa<LoadInst>(I); |
| 2494 | } |
| 2495 | |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 2496 | // Return true if this operand will be safe to use for phi of ops. |
| 2497 | // |
| 2498 | // The reason some operands are unsafe is that we are not trying to recursively |
| 2499 | // translate everything back through phi nodes. We actually expect some lookups |
| 2500 | // of expressions to fail. In particular, a lookup where the expression cannot |
| 2501 | // exist in the predecessor. This is true even if the expression, as shown, can |
| 2502 | // be determined to be constant. |
| 2503 | bool NewGVN::OpIsSafeForPHIOfOps(Value *V, Instruction *OrigInst, |
| 2504 | const BasicBlock *PHIBlock, |
| 2505 | SmallPtrSetImpl<const Value *> &Visited) { |
| 2506 | if (!isa<Instruction>(V)) |
| 2507 | return true; |
| 2508 | auto OISIt = OpSafeForPHIOfOps.find(V); |
| 2509 | if (OISIt != OpSafeForPHIOfOps.end()) |
| 2510 | return OISIt->second; |
| 2511 | // Keep walking until we either dominate the phi block, or hit a phi, or run |
| 2512 | // out of things to check. |
| 2513 | if (DT->properlyDominates(getBlockForValue(V), PHIBlock)) { |
| 2514 | OpSafeForPHIOfOps.insert({V, true}); |
| 2515 | return true; |
| 2516 | } |
| 2517 | // PHI in the same block. |
| 2518 | if (isa<PHINode>(V) && getBlockForValue(V) == PHIBlock) { |
| 2519 | OpSafeForPHIOfOps.insert({V, false}); |
| 2520 | return false; |
| 2521 | } |
| 2522 | for (auto Op : cast<Instruction>(V)->operand_values()) { |
| 2523 | if (!isa<Instruction>(Op)) |
| 2524 | continue; |
| 2525 | // See if we already know the answer for this node. |
| 2526 | auto OISIt = OpSafeForPHIOfOps.find(Op); |
| 2527 | if (OISIt != OpSafeForPHIOfOps.end()) { |
| 2528 | if (!OISIt->second) { |
| 2529 | OpSafeForPHIOfOps.insert({V, false}); |
| 2530 | return false; |
| 2531 | } |
| 2532 | } |
| 2533 | if (!Visited.insert(Op).second) |
| 2534 | continue; |
| 2535 | if (!OpIsSafeForPHIOfOps(Op, OrigInst, PHIBlock, Visited)) { |
| 2536 | OpSafeForPHIOfOps.insert({V, false}); |
| 2537 | return false; |
| 2538 | } |
| 2539 | } |
| 2540 | OpSafeForPHIOfOps.insert({V, true}); |
| 2541 | return true; |
| 2542 | } |
| 2543 | |
| 2544 | // Try to find a leader for instruction TransInst, which is a phi translated |
| 2545 | // version of something in our original program. Visited is used to ensure we |
| 2546 | // don't infinite loop during translations of cycles. OrigInst is the |
| 2547 | // instruction in the original program, and PredBB is the predecessor we |
| 2548 | // translated it through. |
| 2549 | Value *NewGVN::findLeaderForInst(Instruction *TransInst, |
| 2550 | SmallPtrSetImpl<Value *> &Visited, |
| 2551 | MemoryAccess *MemAccess, Instruction *OrigInst, |
| 2552 | BasicBlock *PredBB) { |
| 2553 | unsigned IDFSNum = InstrToDFSNum(OrigInst); |
| 2554 | // Make sure it's marked as a temporary instruction. |
| 2555 | AllTempInstructions.insert(TransInst); |
| 2556 | // and make sure anything that tries to add it's DFS number is |
| 2557 | // redirected to the instruction we are making a phi of ops |
| 2558 | // for. |
| 2559 | TempToBlock.insert({TransInst, PredBB}); |
| 2560 | InstrDFS.insert({TransInst, IDFSNum}); |
| 2561 | |
| 2562 | const Expression *E = performSymbolicEvaluation(TransInst, Visited); |
| 2563 | InstrDFS.erase(TransInst); |
| 2564 | AllTempInstructions.erase(TransInst); |
| 2565 | TempToBlock.erase(TransInst); |
| 2566 | if (MemAccess) |
| 2567 | TempToMemory.erase(TransInst); |
| 2568 | if (!E) |
| 2569 | return nullptr; |
| Daniel Berlin | 4ad7e8d | 2017-09-05 02:17:40 +0000 | [diff] [blame] | 2570 | auto *FoundVal = findPHIOfOpsLeader(E, OrigInst, PredBB); |
| 2571 | if (!FoundVal) { |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 2572 | ExpressionToPhiOfOps[E].insert(OrigInst); |
| 2573 | DEBUG(dbgs() << "Cannot find phi of ops operand for " << *TransInst |
| 2574 | << " in block " << getBlockName(PredBB) << "\n"); |
| 2575 | return nullptr; |
| 2576 | } |
| 2577 | if (auto *SI = dyn_cast<StoreInst>(FoundVal)) |
| 2578 | FoundVal = SI->getValueOperand(); |
| 2579 | return FoundVal; |
| 2580 | } |
| 2581 | |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2582 | // When we see an instruction that is an op of phis, generate the equivalent phi |
| 2583 | // of ops form. |
| 2584 | const Expression * |
| Daniel Berlin | be3e7ba | 2017-05-31 01:47:32 +0000 | [diff] [blame] | 2585 | NewGVN::makePossiblePhiOfOps(Instruction *I, |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2586 | SmallPtrSetImpl<Value *> &Visited) { |
| 2587 | if (!okayForPHIOfOps(I)) |
| 2588 | return nullptr; |
| 2589 | |
| 2590 | if (!Visited.insert(I).second) |
| 2591 | return nullptr; |
| 2592 | // For now, we require the instruction be cycle free because we don't |
| 2593 | // *always* create a phi of ops for instructions that could be done as phi |
| 2594 | // of ops, we only do it if we think it is useful. If we did do it all the |
| 2595 | // time, we could remove the cycle free check. |
| 2596 | if (!isCycleFree(I)) |
| 2597 | return nullptr; |
| 2598 | |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2599 | SmallPtrSet<const Value *, 8> ProcessedPHIs; |
| 2600 | // TODO: We don't do phi translation on memory accesses because it's |
| 2601 | // complicated. For a load, we'd need to be able to simulate a new memoryuse, |
| 2602 | // which we don't have a good way of doing ATM. |
| 2603 | auto *MemAccess = getMemoryAccess(I); |
| 2604 | // If the memory operation is defined by a memory operation this block that |
| 2605 | // isn't a MemoryPhi, transforming the pointer backwards through a scalar phi |
| 2606 | // can't help, as it would still be killed by that memory operation. |
| 2607 | if (MemAccess && !isa<MemoryPhi>(MemAccess->getDefiningAccess()) && |
| 2608 | MemAccess->getDefiningAccess()->getBlock() == I->getParent()) |
| 2609 | return nullptr; |
| 2610 | |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 2611 | SmallPtrSet<const Value *, 10> VisitedOps; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2612 | // Convert op of phis to phi of ops |
| 2613 | for (auto &Op : I->operands()) { |
| 2614 | if (!isa<PHINode>(Op)) |
| 2615 | continue; |
| 2616 | auto *OpPHI = cast<PHINode>(Op); |
| 2617 | // No point in doing this for one-operand phis. |
| 2618 | if (OpPHI->getNumOperands() == 1) |
| 2619 | continue; |
| 2620 | if (!DebugCounter::shouldExecute(PHIOfOpsCounter)) |
| 2621 | return nullptr; |
| 2622 | SmallVector<std::pair<Value *, BasicBlock *>, 4> Ops; |
| 2623 | auto *PHIBlock = getBlockForValue(OpPHI); |
| 2624 | for (auto PredBB : OpPHI->blocks()) { |
| 2625 | Value *FoundVal = nullptr; |
| 2626 | // We could just skip unreachable edges entirely but it's tricky to do |
| 2627 | // with rewriting existing phi nodes. |
| 2628 | if (ReachableEdges.count({PredBB, PHIBlock})) { |
| 2629 | // Clone the instruction, create an expression from it, and see if we |
| 2630 | // have a leader. |
| 2631 | Instruction *ValueOp = I->clone(); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2632 | if (MemAccess) |
| Daniel Berlin | c8ed404 | 2017-05-30 06:42:29 +0000 | [diff] [blame] | 2633 | TempToMemory.insert({ValueOp, MemAccess}); |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 2634 | bool SafeForPHIOfOps = true; |
| 2635 | VisitedOps.clear(); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2636 | for (auto &Op : ValueOp->operands()) { |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 2637 | auto *OrigOp = &*Op; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2638 | Op = Op->DoPHITranslation(PHIBlock, PredBB); |
| 2639 | // When this operand changes, it could change whether there is a |
| 2640 | // leader for us or not. |
| 2641 | addAdditionalUsers(Op, I); |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 2642 | // If we phi-translated the op, it must be safe. |
| 2643 | SafeForPHIOfOps = SafeForPHIOfOps && |
| 2644 | (Op != OrigOp || |
| 2645 | OpIsSafeForPHIOfOps(Op, I, PHIBlock, VisitedOps)); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2646 | } |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 2647 | // FIXME: For those things that are not safe We could generate |
| 2648 | // expressions all the way down, and see if this comes out to a |
| 2649 | // constant. For anything where that is true, and unsafe, we should |
| 2650 | // have made a phi-of-ops (or value numbered it equivalent to something) |
| 2651 | // for the pieces already. |
| 2652 | FoundVal = !SafeForPHIOfOps ? nullptr |
| 2653 | : findLeaderForInst(ValueOp, Visited, |
| 2654 | MemAccess, I, PredBB); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2655 | ValueOp->deleteValue(); |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 2656 | if (!FoundVal) |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2657 | return nullptr; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2658 | } else { |
| 2659 | DEBUG(dbgs() << "Skipping phi of ops operand for incoming block " |
| 2660 | << getBlockName(PredBB) |
| 2661 | << " because the block is unreachable\n"); |
| 2662 | FoundVal = UndefValue::get(I->getType()); |
| 2663 | } |
| 2664 | |
| 2665 | Ops.push_back({FoundVal, PredBB}); |
| 2666 | DEBUG(dbgs() << "Found phi of ops operand " << *FoundVal << " in " |
| 2667 | << getBlockName(PredBB) << "\n"); |
| 2668 | } |
| 2669 | auto *ValuePHI = RealToTemp.lookup(I); |
| 2670 | bool NewPHI = false; |
| 2671 | if (!ValuePHI) { |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 2672 | ValuePHI = |
| 2673 | PHINode::Create(I->getType(), OpPHI->getNumOperands(), "phiofops"); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2674 | addPhiOfOps(ValuePHI, PHIBlock, I); |
| 2675 | NewPHI = true; |
| 2676 | NumGVNPHIOfOpsCreated++; |
| 2677 | } |
| 2678 | if (NewPHI) { |
| 2679 | for (auto PHIOp : Ops) |
| 2680 | ValuePHI->addIncoming(PHIOp.first, PHIOp.second); |
| 2681 | } else { |
| 2682 | unsigned int i = 0; |
| 2683 | for (auto PHIOp : Ops) { |
| 2684 | ValuePHI->setIncomingValue(i, PHIOp.first); |
| 2685 | ValuePHI->setIncomingBlock(i, PHIOp.second); |
| 2686 | ++i; |
| 2687 | } |
| 2688 | } |
| 2689 | |
| 2690 | DEBUG(dbgs() << "Created phi of ops " << *ValuePHI << " for " << *I |
| 2691 | << "\n"); |
| 2692 | return performSymbolicEvaluation(ValuePHI, Visited); |
| 2693 | } |
| 2694 | return nullptr; |
| 2695 | } |
| 2696 | |
| Daniel Berlin | 5c338ff | 2017-03-10 19:05:04 +0000 | [diff] [blame] | 2697 | // The algorithm initially places the values of the routine in the TOP |
| 2698 | // congruence class. The leader of TOP is the undetermined value `undef`. |
| 2699 | // When the algorithm has finished, values still in TOP are unreachable. |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2700 | void NewGVN::initializeCongruenceClasses(Function &F) { |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2701 | NextCongruenceNum = 0; |
| 2702 | |
| 2703 | // Note that even though we use the live on entry def as a representative |
| 2704 | // MemoryAccess, it is *not* the same as the actual live on entry def. We |
| 2705 | // have no real equivalemnt to undef for MemoryAccesses, and so we really |
| 2706 | // should be checking whether the MemoryAccess is top if we want to know if it |
| 2707 | // is equivalent to everything. Otherwise, what this really signifies is that |
| 2708 | // the access "it reaches all the way back to the beginning of the function" |
| 2709 | |
| Daniel Berlin | 5c338ff | 2017-03-10 19:05:04 +0000 | [diff] [blame] | 2710 | // Initialize all other instructions to be in TOP class. |
| Daniel Berlin | 5c338ff | 2017-03-10 19:05:04 +0000 | [diff] [blame] | 2711 | TOPClass = createCongruenceClass(nullptr, nullptr); |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2712 | TOPClass->setMemoryLeader(MSSA->getLiveOnEntryDef()); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2713 | // The live on entry def gets put into it's own class |
| 2714 | MemoryAccessToClass[MSSA->getLiveOnEntryDef()] = |
| 2715 | createMemoryClass(MSSA->getLiveOnEntryDef()); |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 2716 | |
| Daniel Berlin | ec9deb7 | 2017-04-18 17:06:11 +0000 | [diff] [blame] | 2717 | for (auto DTN : nodes(DT)) { |
| 2718 | BasicBlock *BB = DTN->getBlock(); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2719 | // All MemoryAccesses are equivalent to live on entry to start. They must |
| 2720 | // be initialized to something so that initial changes are noticed. For |
| 2721 | // the maximal answer, we initialize them all to be the same as |
| 2722 | // liveOnEntry. |
| Daniel Berlin | ec9deb7 | 2017-04-18 17:06:11 +0000 | [diff] [blame] | 2723 | auto *MemoryBlockDefs = MSSA->getBlockDefs(BB); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2724 | if (MemoryBlockDefs) |
| 2725 | for (const auto &Def : *MemoryBlockDefs) { |
| 2726 | MemoryAccessToClass[&Def] = TOPClass; |
| 2727 | auto *MD = dyn_cast<MemoryDef>(&Def); |
| 2728 | // Insert the memory phis into the member list. |
| 2729 | if (!MD) { |
| 2730 | const MemoryPhi *MP = cast<MemoryPhi>(&Def); |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2731 | TOPClass->memory_insert(MP); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2732 | MemoryPhiState.insert({MP, MPS_TOP}); |
| 2733 | } |
| 2734 | |
| 2735 | if (MD && isa<StoreInst>(MD->getMemoryInst())) |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2736 | TOPClass->incStoreCount(); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2737 | } |
| Daniel Berlin | ec9deb7 | 2017-04-18 17:06:11 +0000 | [diff] [blame] | 2738 | for (auto &I : *BB) { |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2739 | // TODO: Move to helper |
| 2740 | if (isa<PHINode>(&I)) |
| 2741 | for (auto *U : I.users()) |
| 2742 | if (auto *UInst = dyn_cast<Instruction>(U)) |
| 2743 | if (InstrToDFSNum(UInst) != 0 && okayForPHIOfOps(UInst)) |
| 2744 | PHINodeUses.insert(UInst); |
| Daniel Berlin | 22a4a01 | 2017-02-11 15:20:15 +0000 | [diff] [blame] | 2745 | // Don't insert void terminators into the class. We don't value number |
| Daniel Berlin | 5c338ff | 2017-03-10 19:05:04 +0000 | [diff] [blame] | 2746 | // them, and they just end up sitting in TOP. |
| Daniel Berlin | 22a4a01 | 2017-02-11 15:20:15 +0000 | [diff] [blame] | 2747 | if (isa<TerminatorInst>(I) && I.getType()->isVoidTy()) |
| 2748 | continue; |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2749 | TOPClass->insert(&I); |
| Daniel Berlin | 5c338ff | 2017-03-10 19:05:04 +0000 | [diff] [blame] | 2750 | ValueToClass[&I] = TOPClass; |
| Daniel Berlin | 85cbc8c | 2016-12-26 19:57:25 +0000 | [diff] [blame] | 2751 | } |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 2752 | } |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2753 | |
| 2754 | // Initialize arguments to be in their own unique congruence classes |
| 2755 | for (auto &FA : F.args()) |
| 2756 | createSingletonCongruenceClass(&FA); |
| 2757 | } |
| 2758 | |
| 2759 | void NewGVN::cleanupTables() { |
| 2760 | for (unsigned i = 0, e = CongruenceClasses.size(); i != e; ++i) { |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 2761 | DEBUG(dbgs() << "Congruence class " << CongruenceClasses[i]->getID() |
| 2762 | << " has " << CongruenceClasses[i]->size() << " members\n"); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2763 | // Make sure we delete the congruence class (probably worth switching to |
| 2764 | // a unique_ptr at some point. |
| 2765 | delete CongruenceClasses[i]; |
| Davide Italiano | 0e71480 | 2016-12-28 14:00:11 +0000 | [diff] [blame] | 2766 | CongruenceClasses[i] = nullptr; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2769 | // Destroy the value expressions |
| 2770 | SmallVector<Instruction *, 8> TempInst(AllTempInstructions.begin(), |
| 2771 | AllTempInstructions.end()); |
| 2772 | AllTempInstructions.clear(); |
| 2773 | |
| 2774 | // We have to drop all references for everything first, so there are no uses |
| 2775 | // left as we delete them. |
| 2776 | for (auto *I : TempInst) { |
| 2777 | I->dropAllReferences(); |
| 2778 | } |
| 2779 | |
| 2780 | while (!TempInst.empty()) { |
| 2781 | auto *I = TempInst.back(); |
| 2782 | TempInst.pop_back(); |
| 2783 | I->deleteValue(); |
| 2784 | } |
| 2785 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2786 | ValueToClass.clear(); |
| 2787 | ArgRecycler.clear(ExpressionAllocator); |
| 2788 | ExpressionAllocator.Reset(); |
| 2789 | CongruenceClasses.clear(); |
| 2790 | ExpressionToClass.clear(); |
| 2791 | ValueToExpression.clear(); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2792 | RealToTemp.clear(); |
| 2793 | AdditionalUsers.clear(); |
| 2794 | ExpressionToPhiOfOps.clear(); |
| 2795 | TempToBlock.clear(); |
| 2796 | TempToMemory.clear(); |
| 2797 | PHIOfOpsPHIs.clear(); |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 2798 | PHINodeUses.clear(); |
| 2799 | OpSafeForPHIOfOps.clear(); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2800 | ReachableBlocks.clear(); |
| 2801 | ReachableEdges.clear(); |
| 2802 | #ifndef NDEBUG |
| 2803 | ProcessedCount.clear(); |
| 2804 | #endif |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2805 | InstrDFS.clear(); |
| 2806 | InstructionsToErase.clear(); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2807 | DFSToInstr.clear(); |
| 2808 | BlockInstRange.clear(); |
| 2809 | TouchedInstructions.clear(); |
| Daniel Berlin | 1ea5f32 | 2017-01-26 22:21:48 +0000 | [diff] [blame] | 2810 | MemoryAccessToClass.clear(); |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 2811 | PredicateToUsers.clear(); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2812 | MemoryToUsers.clear(); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2815 | // Assign local DFS number mapping to instructions, and leave space for Value |
| 2816 | // PHI's. |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2817 | std::pair<unsigned, unsigned> NewGVN::assignDFSNumbers(BasicBlock *B, |
| 2818 | unsigned Start) { |
| 2819 | unsigned End = Start; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2820 | if (MemoryAccess *MemPhi = getMemoryAccess(B)) { |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 2821 | InstrDFS[MemPhi] = End++; |
| Piotr Padlewski | 6c37d29 | 2016-12-28 23:24:02 +0000 | [diff] [blame] | 2822 | DFSToInstr.emplace_back(MemPhi); |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2825 | // Then the real block goes next. |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2826 | for (auto &I : *B) { |
| Daniel Berlin | 856fa14 | 2017-03-06 18:42:27 +0000 | [diff] [blame] | 2827 | // There's no need to call isInstructionTriviallyDead more than once on |
| 2828 | // an instruction. Therefore, once we know that an instruction is dead |
| 2829 | // we change its DFS number so that it doesn't get value numbered. |
| 2830 | if (isInstructionTriviallyDead(&I, TLI)) { |
| 2831 | InstrDFS[&I] = 0; |
| 2832 | DEBUG(dbgs() << "Skipping trivially dead instruction " << I << "\n"); |
| 2833 | markInstructionForDeletion(&I); |
| 2834 | continue; |
| 2835 | } |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2836 | InstrDFS[&I] = End++; |
| Piotr Padlewski | 6c37d29 | 2016-12-28 23:24:02 +0000 | [diff] [blame] | 2837 | DFSToInstr.emplace_back(&I); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | // All of the range functions taken half-open ranges (open on the end side). |
| 2841 | // So we do not subtract one from count, because at this point it is one |
| 2842 | // greater than the last instruction. |
| 2843 | return std::make_pair(Start, End); |
| 2844 | } |
| 2845 | |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2846 | void NewGVN::updateProcessedCount(const Value *V) { |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2847 | #ifndef NDEBUG |
| 2848 | if (ProcessedCount.count(V) == 0) { |
| 2849 | ProcessedCount.insert({V, 1}); |
| 2850 | } else { |
| Davide Italiano | 7cf29dc | 2017-01-14 20:13:18 +0000 | [diff] [blame] | 2851 | ++ProcessedCount[V]; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2852 | assert(ProcessedCount[V] < 100 && |
| Davide Italiano | 75e39f9 | 2016-12-30 15:01:17 +0000 | [diff] [blame] | 2853 | "Seem to have processed the same Value a lot"); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2854 | } |
| 2855 | #endif |
| 2856 | } |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 2857 | // Evaluate MemoryPhi nodes symbolically, just like PHI nodes |
| 2858 | void NewGVN::valueNumberMemoryPhi(MemoryPhi *MP) { |
| 2859 | // If all the arguments are the same, the MemoryPhi has the same value as the |
| Daniel Berlin | d130b6c | 2017-05-21 23:41:58 +0000 | [diff] [blame] | 2860 | // argument. Filter out unreachable blocks and self phis from our operands. |
| 2861 | // TODO: We could do cycle-checking on the memory phis to allow valueizing for |
| 2862 | // self-phi checking. |
| Daniel Berlin | 41b3916 | 2017-03-18 15:41:36 +0000 | [diff] [blame] | 2863 | const BasicBlock *PHIBlock = MP->getBlock(); |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 2864 | auto Filtered = make_filter_range(MP->operands(), [&](const Use &U) { |
| Daniel Berlin | d130b6c | 2017-05-21 23:41:58 +0000 | [diff] [blame] | 2865 | return cast<MemoryAccess>(U) != MP && |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2866 | !isMemoryAccessTOP(cast<MemoryAccess>(U)) && |
| Daniel Berlin | 41b3916 | 2017-03-18 15:41:36 +0000 | [diff] [blame] | 2867 | ReachableEdges.count({MP->getIncomingBlock(U), PHIBlock}); |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 2868 | }); |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 2869 | // If all that is left is nothing, our memoryphi is undef. We keep it as |
| 2870 | // InitialClass. Note: The only case this should happen is if we have at |
| 2871 | // least one self-argument. |
| 2872 | if (Filtered.begin() == Filtered.end()) { |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2873 | if (setMemoryClass(MP, TOPClass)) |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 2874 | markMemoryUsersTouched(MP); |
| 2875 | return; |
| 2876 | } |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 2877 | |
| 2878 | // Transform the remaining operands into operand leaders. |
| 2879 | // FIXME: mapped_iterator should have a range version. |
| 2880 | auto LookupFunc = [&](const Use &U) { |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2881 | return lookupMemoryLeader(cast<MemoryAccess>(U)); |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 2882 | }; |
| 2883 | auto MappedBegin = map_iterator(Filtered.begin(), LookupFunc); |
| 2884 | auto MappedEnd = map_iterator(Filtered.end(), LookupFunc); |
| 2885 | |
| 2886 | // and now check if all the elements are equal. |
| 2887 | // Sadly, we can't use std::equals since these are random access iterators. |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2888 | const auto *AllSameValue = *MappedBegin; |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 2889 | ++MappedBegin; |
| 2890 | bool AllEqual = std::all_of( |
| 2891 | MappedBegin, MappedEnd, |
| 2892 | [&AllSameValue](const MemoryAccess *V) { return V == AllSameValue; }); |
| 2893 | |
| 2894 | if (AllEqual) |
| 2895 | DEBUG(dbgs() << "Memory Phi value numbered to " << *AllSameValue << "\n"); |
| 2896 | else |
| 2897 | DEBUG(dbgs() << "Memory Phi value numbered to itself\n"); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2898 | // If it's equal to something, it's in that class. Otherwise, it has to be in |
| 2899 | // a class where it is the leader (other things may be equivalent to it, but |
| 2900 | // it needs to start off in its own class, which means it must have been the |
| 2901 | // leader, and it can't have stopped being the leader because it was never |
| 2902 | // removed). |
| 2903 | CongruenceClass *CC = |
| 2904 | AllEqual ? getMemoryClass(AllSameValue) : ensureLeaderOfMemoryClass(MP); |
| 2905 | auto OldState = MemoryPhiState.lookup(MP); |
| 2906 | assert(OldState != MPS_Invalid && "Invalid memory phi state"); |
| 2907 | auto NewState = AllEqual ? MPS_Equivalent : MPS_Unique; |
| 2908 | MemoryPhiState[MP] = NewState; |
| 2909 | if (setMemoryClass(MP, CC) || OldState != NewState) |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 2910 | markMemoryUsersTouched(MP); |
| 2911 | } |
| 2912 | |
| 2913 | // Value number a single instruction, symbolically evaluating, performing |
| 2914 | // congruence finding, and updating mappings. |
| 2915 | void NewGVN::valueNumberInstruction(Instruction *I) { |
| 2916 | DEBUG(dbgs() << "Processing instruction " << *I << "\n"); |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 2917 | if (!I->isTerminator()) { |
| Daniel Berlin | 283a608 | 2017-03-01 19:59:26 +0000 | [diff] [blame] | 2918 | const Expression *Symbolized = nullptr; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2919 | SmallPtrSet<Value *, 2> Visited; |
| Daniel Berlin | 283a608 | 2017-03-01 19:59:26 +0000 | [diff] [blame] | 2920 | if (DebugCounter::shouldExecute(VNCounter)) { |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2921 | Symbolized = performSymbolicEvaluation(I, Visited); |
| 2922 | // Make a phi of ops if necessary |
| 2923 | if (Symbolized && !isa<ConstantExpression>(Symbolized) && |
| 2924 | !isa<VariableExpression>(Symbolized) && PHINodeUses.count(I)) { |
| Daniel Berlin | be3e7ba | 2017-05-31 01:47:32 +0000 | [diff] [blame] | 2925 | auto *PHIE = makePossiblePhiOfOps(I, Visited); |
| Davide Italiano | 5974c31 | 2017-08-03 21:17:49 +0000 | [diff] [blame] | 2926 | // If we created a phi of ops, use it. |
| 2927 | // If we couldn't create one, make sure we don't leave one lying around |
| 2928 | if (PHIE) { |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2929 | Symbolized = PHIE; |
| Davide Italiano | 5974c31 | 2017-08-03 21:17:49 +0000 | [diff] [blame] | 2930 | } else if (auto *Op = RealToTemp.lookup(I)) { |
| 2931 | removePhiOfOps(I, Op); |
| 2932 | } |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
| Daniel Berlin | 283a608 | 2017-03-01 19:59:26 +0000 | [diff] [blame] | 2935 | } else { |
| Daniel Berlin | 343576a | 2017-03-06 18:42:39 +0000 | [diff] [blame] | 2936 | // Mark the instruction as unused so we don't value number it again. |
| 2937 | InstrDFS[I] = 0; |
| Daniel Berlin | 283a608 | 2017-03-01 19:59:26 +0000 | [diff] [blame] | 2938 | } |
| Daniel Berlin | 02c6b17 | 2017-01-02 18:00:53 +0000 | [diff] [blame] | 2939 | // If we couldn't come up with a symbolic expression, use the unknown |
| 2940 | // expression |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 2941 | if (Symbolized == nullptr) |
| Daniel Berlin | 02c6b17 | 2017-01-02 18:00:53 +0000 | [diff] [blame] | 2942 | Symbolized = createUnknownExpression(I); |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 2943 | performCongruenceFinding(I, Symbolized); |
| 2944 | } else { |
| Daniel Berlin | 02c6b17 | 2017-01-02 18:00:53 +0000 | [diff] [blame] | 2945 | // Handle terminators that return values. All of them produce values we |
| Daniel Berlin | b79f536 | 2017-02-11 12:48:50 +0000 | [diff] [blame] | 2946 | // don't currently understand. We don't place non-value producing |
| 2947 | // terminators in a class. |
| Daniel Berlin | 25f05b0 | 2017-01-02 18:22:38 +0000 | [diff] [blame] | 2948 | if (!I->getType()->isVoidTy()) { |
| Daniel Berlin | 02c6b17 | 2017-01-02 18:00:53 +0000 | [diff] [blame] | 2949 | auto *Symbolized = createUnknownExpression(I); |
| 2950 | performCongruenceFinding(I, Symbolized); |
| 2951 | } |
| Daniel Berlin | d7c12ee | 2016-12-25 22:23:49 +0000 | [diff] [blame] | 2952 | processOutgoingEdges(dyn_cast<TerminatorInst>(I), I->getParent()); |
| 2953 | } |
| 2954 | } |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 2955 | |
| Daniel Berlin | f6eba4b | 2017-01-11 20:22:36 +0000 | [diff] [blame] | 2956 | // Check if there is a path, using single or equal argument phi nodes, from |
| 2957 | // First to Second. |
| Davide Italiano | eab0de2 | 2017-05-18 23:22:44 +0000 | [diff] [blame] | 2958 | bool NewGVN::singleReachablePHIPath( |
| 2959 | SmallPtrSet<const MemoryAccess *, 8> &Visited, const MemoryAccess *First, |
| 2960 | const MemoryAccess *Second) const { |
| Daniel Berlin | f6eba4b | 2017-01-11 20:22:36 +0000 | [diff] [blame] | 2961 | if (First == Second) |
| 2962 | return true; |
| Daniel Berlin | 871ecd9 | 2017-04-01 09:44:24 +0000 | [diff] [blame] | 2963 | if (MSSA->isLiveOnEntryDef(First)) |
| Daniel Berlin | f6eba4b | 2017-01-11 20:22:36 +0000 | [diff] [blame] | 2964 | return false; |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 2965 | |
| Davide Italiano | eab0de2 | 2017-05-18 23:22:44 +0000 | [diff] [blame] | 2966 | // This is not perfect, but as we're just verifying here, we can live with |
| 2967 | // the loss of precision. The real solution would be that of doing strongly |
| 2968 | // connected component finding in this routine, and it's probably not worth |
| 2969 | // the complexity for the time being. So, we just keep a set of visited |
| 2970 | // MemoryAccess and return true when we hit a cycle. |
| 2971 | if (Visited.count(First)) |
| 2972 | return true; |
| 2973 | Visited.insert(First); |
| 2974 | |
| Daniel Berlin | 871ecd9 | 2017-04-01 09:44:24 +0000 | [diff] [blame] | 2975 | const auto *EndDef = First; |
| Daniel Berlin | 3082b8e | 2017-04-05 17:26:25 +0000 | [diff] [blame] | 2976 | for (auto *ChainDef : optimized_def_chain(First)) { |
| Daniel Berlin | 871ecd9 | 2017-04-01 09:44:24 +0000 | [diff] [blame] | 2977 | if (ChainDef == Second) |
| 2978 | return true; |
| 2979 | if (MSSA->isLiveOnEntryDef(ChainDef)) |
| 2980 | return false; |
| 2981 | EndDef = ChainDef; |
| Daniel Berlin | f6eba4b | 2017-01-11 20:22:36 +0000 | [diff] [blame] | 2982 | } |
| Daniel Berlin | 871ecd9 | 2017-04-01 09:44:24 +0000 | [diff] [blame] | 2983 | auto *MP = cast<MemoryPhi>(EndDef); |
| 2984 | auto ReachableOperandPred = [&](const Use &U) { |
| 2985 | return ReachableEdges.count({MP->getIncomingBlock(U), MP->getBlock()}); |
| 2986 | }; |
| 2987 | auto FilteredPhiArgs = |
| 2988 | make_filter_range(MP->operands(), ReachableOperandPred); |
| 2989 | SmallVector<const Value *, 32> OperandList; |
| 2990 | std::copy(FilteredPhiArgs.begin(), FilteredPhiArgs.end(), |
| 2991 | std::back_inserter(OperandList)); |
| 2992 | bool Okay = OperandList.size() == 1; |
| 2993 | if (!Okay) |
| 2994 | Okay = |
| 2995 | std::equal(OperandList.begin(), OperandList.end(), OperandList.begin()); |
| 2996 | if (Okay) |
| Davide Italiano | eab0de2 | 2017-05-18 23:22:44 +0000 | [diff] [blame] | 2997 | return singleReachablePHIPath(Visited, cast<MemoryAccess>(OperandList[0]), |
| 2998 | Second); |
| Daniel Berlin | 871ecd9 | 2017-04-01 09:44:24 +0000 | [diff] [blame] | 2999 | return false; |
| Daniel Berlin | f6eba4b | 2017-01-11 20:22:36 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 3002 | // Verify the that the memory equivalence table makes sense relative to the |
| Daniel Berlin | f6eba4b | 2017-01-11 20:22:36 +0000 | [diff] [blame] | 3003 | // congruence classes. Note that this checking is not perfect, and is currently |
| Davide Italiano | ed67f19 | 2017-01-14 20:15:04 +0000 | [diff] [blame] | 3004 | // subject to very rare false negatives. It is only useful for |
| 3005 | // testing/debugging. |
| Daniel Berlin | f6eba4b | 2017-01-11 20:22:36 +0000 | [diff] [blame] | 3006 | void NewGVN::verifyMemoryCongruency() const { |
| Davide Italiano | e9781e7 | 2017-03-25 02:40:02 +0000 | [diff] [blame] | 3007 | #ifndef NDEBUG |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 3008 | // Verify that the memory table equivalence and memory member set match |
| 3009 | for (const auto *CC : CongruenceClasses) { |
| 3010 | if (CC == TOPClass || CC->isDead()) |
| 3011 | continue; |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 3012 | if (CC->getStoreCount() != 0) { |
| Davide Italiano | f58a3023 | 2017-04-10 23:08:35 +0000 | [diff] [blame] | 3013 | assert((CC->getStoredValue() || !isa<StoreInst>(CC->getLeader())) && |
| Davide Italiano | 94bf784 | 2017-05-04 17:26:15 +0000 | [diff] [blame] | 3014 | "Any class with a store as a leader should have a " |
| 3015 | "representative stored value"); |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 3016 | assert(CC->getMemoryLeader() && |
| Davide Italiano | 94bf784 | 2017-05-04 17:26:15 +0000 | [diff] [blame] | 3017 | "Any congruence class with a store should have a " |
| 3018 | "representative access"); |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 3021 | if (CC->getMemoryLeader()) |
| 3022 | assert(MemoryAccessToClass.lookup(CC->getMemoryLeader()) == CC && |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 3023 | "Representative MemoryAccess does not appear to be reverse " |
| 3024 | "mapped properly"); |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 3025 | for (auto M : CC->memory()) |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 3026 | assert(MemoryAccessToClass.lookup(M) == CC && |
| 3027 | "Memory member does not appear to be reverse mapped properly"); |
| 3028 | } |
| 3029 | |
| 3030 | // Anything equivalent in the MemoryAccess table should be in the same |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 3031 | // congruence class. |
| 3032 | |
| 3033 | // Filter out the unreachable and trivially dead entries, because they may |
| 3034 | // never have been updated if the instructions were not processed. |
| 3035 | auto ReachableAccessPred = |
| Daniel Berlin | 1ea5f32 | 2017-01-26 22:21:48 +0000 | [diff] [blame] | 3036 | [&](const std::pair<const MemoryAccess *, CongruenceClass *> Pair) { |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 3037 | bool Result = ReachableBlocks.count(Pair.first->getBlock()); |
| Daniel Berlin | 9d0042b | 2017-04-18 20:15:47 +0000 | [diff] [blame] | 3038 | if (!Result || MSSA->isLiveOnEntryDef(Pair.first) || |
| 3039 | MemoryToDFSNum(Pair.first) == 0) |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 3040 | return false; |
| 3041 | if (auto *MemDef = dyn_cast<MemoryDef>(Pair.first)) |
| 3042 | return !isInstructionTriviallyDead(MemDef->getMemoryInst()); |
| Davide Italiano | 6e7a212 | 2017-05-15 18:50:53 +0000 | [diff] [blame] | 3043 | |
| 3044 | // We could have phi nodes which operands are all trivially dead, |
| 3045 | // so we don't process them. |
| 3046 | if (auto *MemPHI = dyn_cast<MemoryPhi>(Pair.first)) { |
| 3047 | for (auto &U : MemPHI->incoming_values()) { |
| 3048 | if (Instruction *I = dyn_cast<Instruction>(U.get())) { |
| 3049 | if (!isInstructionTriviallyDead(I)) |
| 3050 | return true; |
| 3051 | } |
| 3052 | } |
| 3053 | return false; |
| 3054 | } |
| 3055 | |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 3056 | return true; |
| 3057 | }; |
| 3058 | |
| Daniel Berlin | 1ea5f32 | 2017-01-26 22:21:48 +0000 | [diff] [blame] | 3059 | auto Filtered = make_filter_range(MemoryAccessToClass, ReachableAccessPred); |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 3060 | for (auto KV : Filtered) { |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 3061 | if (auto *FirstMUD = dyn_cast<MemoryUseOrDef>(KV.first)) { |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 3062 | auto *SecondMUD = dyn_cast<MemoryUseOrDef>(KV.second->getMemoryLeader()); |
| Davide Italiano | eab0de2 | 2017-05-18 23:22:44 +0000 | [diff] [blame] | 3063 | if (FirstMUD && SecondMUD) { |
| 3064 | SmallPtrSet<const MemoryAccess *, 8> VisitedMAS; |
| 3065 | assert((singleReachablePHIPath(VisitedMAS, FirstMUD, SecondMUD) || |
| Davide Italiano | ed67f19 | 2017-01-14 20:15:04 +0000 | [diff] [blame] | 3066 | ValueToClass.lookup(FirstMUD->getMemoryInst()) == |
| 3067 | ValueToClass.lookup(SecondMUD->getMemoryInst())) && |
| 3068 | "The instructions for these memory operations should have " |
| 3069 | "been in the same congruence class or reachable through" |
| 3070 | "a single argument phi"); |
| Davide Italiano | eab0de2 | 2017-05-18 23:22:44 +0000 | [diff] [blame] | 3071 | } |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 3072 | } else if (auto *FirstMP = dyn_cast<MemoryPhi>(KV.first)) { |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 3073 | // We can only sanely verify that MemoryDefs in the operand list all have |
| 3074 | // the same class. |
| 3075 | auto ReachableOperandPred = [&](const Use &U) { |
| Daniel Berlin | 41b3916 | 2017-03-18 15:41:36 +0000 | [diff] [blame] | 3076 | return ReachableEdges.count( |
| 3077 | {FirstMP->getIncomingBlock(U), FirstMP->getBlock()}) && |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 3078 | isa<MemoryDef>(U); |
| 3079 | |
| 3080 | }; |
| 3081 | // All arguments should in the same class, ignoring unreachable arguments |
| 3082 | auto FilteredPhiArgs = |
| 3083 | make_filter_range(FirstMP->operands(), ReachableOperandPred); |
| 3084 | SmallVector<const CongruenceClass *, 16> PhiOpClasses; |
| 3085 | std::transform(FilteredPhiArgs.begin(), FilteredPhiArgs.end(), |
| 3086 | std::back_inserter(PhiOpClasses), [&](const Use &U) { |
| 3087 | const MemoryDef *MD = cast<MemoryDef>(U); |
| 3088 | return ValueToClass.lookup(MD->getMemoryInst()); |
| 3089 | }); |
| 3090 | assert(std::equal(PhiOpClasses.begin(), PhiOpClasses.end(), |
| 3091 | PhiOpClasses.begin()) && |
| 3092 | "All MemoryPhi arguments should be in the same class"); |
| 3093 | } |
| 3094 | } |
| Davide Italiano | e9781e7 | 2017-03-25 02:40:02 +0000 | [diff] [blame] | 3095 | #endif |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 3096 | } |
| 3097 | |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 3098 | // Verify that the sparse propagation we did actually found the maximal fixpoint |
| 3099 | // We do this by storing the value to class mapping, touching all instructions, |
| 3100 | // and redoing the iteration to see if anything changed. |
| 3101 | void NewGVN::verifyIterationSettled(Function &F) { |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 3102 | #ifndef NDEBUG |
| Daniel Berlin | 1316a94 | 2017-04-06 18:52:50 +0000 | [diff] [blame] | 3103 | DEBUG(dbgs() << "Beginning iteration verification\n"); |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 3104 | if (DebugCounter::isCounterSet(VNCounter)) |
| 3105 | DebugCounter::setCounterValue(VNCounter, StartingVNCounter); |
| 3106 | |
| 3107 | // Note that we have to store the actual classes, as we may change existing |
| 3108 | // classes during iteration. This is because our memory iteration propagation |
| 3109 | // is not perfect, and so may waste a little work. But it should generate |
| 3110 | // exactly the same congruence classes we have now, with different IDs. |
| 3111 | std::map<const Value *, CongruenceClass> BeforeIteration; |
| 3112 | |
| 3113 | for (auto &KV : ValueToClass) { |
| 3114 | if (auto *I = dyn_cast<Instruction>(KV.first)) |
| 3115 | // Skip unused/dead instructions. |
| Daniel Berlin | 21279bd | 2017-04-06 18:52:58 +0000 | [diff] [blame] | 3116 | if (InstrToDFSNum(I) == 0) |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 3117 | continue; |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 3118 | BeforeIteration.insert({KV.first, *KV.second}); |
| 3119 | } |
| 3120 | |
| 3121 | TouchedInstructions.set(); |
| 3122 | TouchedInstructions.reset(0); |
| 3123 | iterateTouchedInstructions(); |
| 3124 | DenseSet<std::pair<const CongruenceClass *, const CongruenceClass *>> |
| 3125 | EqualClasses; |
| 3126 | for (const auto &KV : ValueToClass) { |
| 3127 | if (auto *I = dyn_cast<Instruction>(KV.first)) |
| 3128 | // Skip unused/dead instructions. |
| Daniel Berlin | 21279bd | 2017-04-06 18:52:58 +0000 | [diff] [blame] | 3129 | if (InstrToDFSNum(I) == 0) |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 3130 | continue; |
| 3131 | // We could sink these uses, but i think this adds a bit of clarity here as |
| 3132 | // to what we are comparing. |
| 3133 | auto *BeforeCC = &BeforeIteration.find(KV.first)->second; |
| 3134 | auto *AfterCC = KV.second; |
| 3135 | // Note that the classes can't change at this point, so we memoize the set |
| 3136 | // that are equal. |
| 3137 | if (!EqualClasses.count({BeforeCC, AfterCC})) { |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 3138 | assert(BeforeCC->isEquivalentTo(AfterCC) && |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 3139 | "Value number changed after main loop completed!"); |
| 3140 | EqualClasses.insert({BeforeCC, AfterCC}); |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 3141 | } |
| 3142 | } |
| 3143 | #endif |
| 3144 | } |
| 3145 | |
| Daniel Berlin | 4540357 | 2017-05-16 19:58:47 +0000 | [diff] [blame] | 3146 | // Verify that for each store expression in the expression to class mapping, |
| 3147 | // only the latest appears, and multiple ones do not appear. |
| 3148 | // Because loads do not use the stored value when doing equality with stores, |
| 3149 | // if we don't erase the old store expressions from the table, a load can find |
| 3150 | // a no-longer valid StoreExpression. |
| 3151 | void NewGVN::verifyStoreExpressions() const { |
| Daniel Berlin | 6c66e9a | 2017-05-16 20:02:45 +0000 | [diff] [blame] | 3152 | #ifndef NDEBUG |
| Daniel Berlin | 36b08b2 | 2017-06-19 00:24:00 +0000 | [diff] [blame] | 3153 | // This is the only use of this, and it's not worth defining a complicated |
| 3154 | // densemapinfo hash/equality function for it. |
| 3155 | std::set< |
| 3156 | std::pair<const Value *, |
| 3157 | std::tuple<const Value *, const CongruenceClass *, Value *>>> |
| 3158 | StoreExpressionSet; |
| Daniel Berlin | 4540357 | 2017-05-16 19:58:47 +0000 | [diff] [blame] | 3159 | for (const auto &KV : ExpressionToClass) { |
| 3160 | if (auto *SE = dyn_cast<StoreExpression>(KV.first)) { |
| 3161 | // Make sure a version that will conflict with loads is not already there |
| Daniel Berlin | 36b08b2 | 2017-06-19 00:24:00 +0000 | [diff] [blame] | 3162 | auto Res = StoreExpressionSet.insert( |
| 3163 | {SE->getOperand(0), std::make_tuple(SE->getMemoryLeader(), KV.second, |
| 3164 | SE->getStoredValue())}); |
| 3165 | bool Okay = Res.second; |
| 3166 | // It's okay to have the same expression already in there if it is |
| 3167 | // identical in nature. |
| 3168 | // This can happen when the leader of the stored value changes over time. |
| Davide Italiano | 0ec715b | 2017-06-20 22:57:40 +0000 | [diff] [blame] | 3169 | if (!Okay) |
| 3170 | Okay = (std::get<1>(Res.first->second) == KV.second) && |
| 3171 | (lookupOperandLeader(std::get<2>(Res.first->second)) == |
| 3172 | lookupOperandLeader(SE->getStoredValue())); |
| Daniel Berlin | 36b08b2 | 2017-06-19 00:24:00 +0000 | [diff] [blame] | 3173 | assert(Okay && "Stored expression conflict exists in expression table"); |
| Daniel Berlin | 4540357 | 2017-05-16 19:58:47 +0000 | [diff] [blame] | 3174 | auto *ValueExpr = ValueToExpression.lookup(SE->getStoreInst()); |
| 3175 | assert(ValueExpr && ValueExpr->equals(*SE) && |
| 3176 | "StoreExpression in ExpressionToClass is not latest " |
| 3177 | "StoreExpression for value"); |
| 3178 | } |
| 3179 | } |
| Daniel Berlin | 6c66e9a | 2017-05-16 20:02:45 +0000 | [diff] [blame] | 3180 | #endif |
| Daniel Berlin | 4540357 | 2017-05-16 19:58:47 +0000 | [diff] [blame] | 3181 | } |
| 3182 | |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 3183 | // This is the main value numbering loop, it iterates over the initial touched |
| 3184 | // instruction set, propagating value numbers, marking things touched, etc, |
| 3185 | // until the set of touched instructions is completely empty. |
| 3186 | void NewGVN::iterateTouchedInstructions() { |
| 3187 | unsigned int Iterations = 0; |
| 3188 | // Figure out where touchedinstructions starts |
| 3189 | int FirstInstr = TouchedInstructions.find_first(); |
| 3190 | // Nothing set, nothing to iterate, just return. |
| 3191 | if (FirstInstr == -1) |
| 3192 | return; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 3193 | const BasicBlock *LastBlock = getBlockForValue(InstrFromDFSNum(FirstInstr)); |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 3194 | while (TouchedInstructions.any()) { |
| 3195 | ++Iterations; |
| 3196 | // Walk through all the instructions in all the blocks in RPO. |
| 3197 | // TODO: As we hit a new block, we should push and pop equalities into a |
| 3198 | // table lookupOperandLeader can use, to catch things PredicateInfo |
| 3199 | // might miss, like edge-only equivalences. |
| Francis Visoiu Mistrih | b52e036 | 2017-05-17 01:07:53 +0000 | [diff] [blame] | 3200 | for (unsigned InstrNum : TouchedInstructions.set_bits()) { |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 3201 | |
| 3202 | // This instruction was found to be dead. We don't bother looking |
| 3203 | // at it again. |
| 3204 | if (InstrNum == 0) { |
| 3205 | TouchedInstructions.reset(InstrNum); |
| 3206 | continue; |
| 3207 | } |
| 3208 | |
| Daniel Berlin | 21279bd | 2017-04-06 18:52:58 +0000 | [diff] [blame] | 3209 | Value *V = InstrFromDFSNum(InstrNum); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 3210 | const BasicBlock *CurrBlock = getBlockForValue(V); |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 3211 | |
| 3212 | // If we hit a new block, do reachability processing. |
| 3213 | if (CurrBlock != LastBlock) { |
| 3214 | LastBlock = CurrBlock; |
| 3215 | bool BlockReachable = ReachableBlocks.count(CurrBlock); |
| 3216 | const auto &CurrInstRange = BlockInstRange.lookup(CurrBlock); |
| 3217 | |
| 3218 | // If it's not reachable, erase any touched instructions and move on. |
| 3219 | if (!BlockReachable) { |
| 3220 | TouchedInstructions.reset(CurrInstRange.first, CurrInstRange.second); |
| 3221 | DEBUG(dbgs() << "Skipping instructions in block " |
| 3222 | << getBlockName(CurrBlock) |
| 3223 | << " because it is unreachable\n"); |
| 3224 | continue; |
| 3225 | } |
| 3226 | updateProcessedCount(CurrBlock); |
| 3227 | } |
| Daniel Berlin | eafdd86 | 2017-06-06 17:15:28 +0000 | [diff] [blame] | 3228 | // Reset after processing (because we may mark ourselves as touched when |
| 3229 | // we propagate equalities). |
| 3230 | TouchedInstructions.reset(InstrNum); |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 3231 | |
| 3232 | if (auto *MP = dyn_cast<MemoryPhi>(V)) { |
| 3233 | DEBUG(dbgs() << "Processing MemoryPhi " << *MP << "\n"); |
| 3234 | valueNumberMemoryPhi(MP); |
| 3235 | } else if (auto *I = dyn_cast<Instruction>(V)) { |
| 3236 | valueNumberInstruction(I); |
| 3237 | } else { |
| 3238 | llvm_unreachable("Should have been a MemoryPhi or Instruction"); |
| 3239 | } |
| 3240 | updateProcessedCount(V); |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 3241 | } |
| 3242 | } |
| 3243 | NumGVNMaxIterations = std::max(NumGVNMaxIterations.getValue(), Iterations); |
| 3244 | } |
| 3245 | |
| Daniel Berlin | 85f91b0 | 2016-12-26 20:06:58 +0000 | [diff] [blame] | 3246 | // This is the main transformation entry point. |
| Daniel Berlin | 64e6899 | 2017-03-12 04:46:45 +0000 | [diff] [blame] | 3247 | bool NewGVN::runGVN() { |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 3248 | if (DebugCounter::isCounterSet(VNCounter)) |
| 3249 | StartingVNCounter = DebugCounter::getCounterValue(VNCounter); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3250 | bool Changed = false; |
| Daniel Berlin | 1529bb9 | 2017-02-11 15:13:49 +0000 | [diff] [blame] | 3251 | NumFuncArgs = F.arg_size(); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3252 | MSSAWalker = MSSA->getWalker(); |
| Daniel Berlin | e021d2d | 2017-05-19 20:22:20 +0000 | [diff] [blame] | 3253 | SingletonDeadExpression = new (ExpressionAllocator) DeadExpression(); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3254 | |
| 3255 | // Count number of instructions for sizing of hash tables, and come |
| 3256 | // up with a global dfs numbering for instructions. |
| Daniel Berlin | e0bd37e | 2016-12-29 22:15:12 +0000 | [diff] [blame] | 3257 | unsigned ICount = 1; |
| 3258 | // Add an empty instruction to account for the fact that we start at 1 |
| 3259 | DFSToInstr.emplace_back(nullptr); |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 3260 | // Note: We want ideal RPO traversal of the blocks, which is not quite the |
| 3261 | // same as dominator tree order, particularly with regard whether backedges |
| 3262 | // get visited first or second, given a block with multiple successors. |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3263 | // If we visit in the wrong order, we will end up performing N times as many |
| 3264 | // iterations. |
| Daniel Berlin | 6658cc9 | 2016-12-29 01:12:36 +0000 | [diff] [blame] | 3265 | // The dominator tree does guarantee that, for a given dom tree node, it's |
| 3266 | // parent must occur before it in the RPO ordering. Thus, we only need to sort |
| 3267 | // the siblings. |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3268 | ReversePostOrderTraversal<Function *> RPOT(&F); |
| Daniel Berlin | 6658cc9 | 2016-12-29 01:12:36 +0000 | [diff] [blame] | 3269 | unsigned Counter = 0; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3270 | for (auto &B : RPOT) { |
| Daniel Berlin | 6658cc9 | 2016-12-29 01:12:36 +0000 | [diff] [blame] | 3271 | auto *Node = DT->getNode(B); |
| 3272 | assert(Node && "RPO and Dominator tree should have same reachability"); |
| 3273 | RPOOrdering[Node] = ++Counter; |
| 3274 | } |
| 3275 | // Sort dominator tree children arrays into RPO. |
| 3276 | for (auto &B : RPOT) { |
| 3277 | auto *Node = DT->getNode(B); |
| 3278 | if (Node->getChildren().size() > 1) |
| 3279 | std::sort(Node->begin(), Node->end(), |
| Daniel Berlin | 2f72b19 | 2017-04-14 02:53:37 +0000 | [diff] [blame] | 3280 | [&](const DomTreeNode *A, const DomTreeNode *B) { |
| Daniel Berlin | 6658cc9 | 2016-12-29 01:12:36 +0000 | [diff] [blame] | 3281 | return RPOOrdering[A] < RPOOrdering[B]; |
| 3282 | }); |
| 3283 | } |
| 3284 | |
| 3285 | // Now a standard depth first ordering of the domtree is equivalent to RPO. |
| Daniel Berlin | ec9deb7 | 2017-04-18 17:06:11 +0000 | [diff] [blame] | 3286 | for (auto DTN : depth_first(DT->getRootNode())) { |
| 3287 | BasicBlock *B = DTN->getBlock(); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3288 | const auto &BlockRange = assignDFSNumbers(B, ICount); |
| 3289 | BlockInstRange.insert({B, BlockRange}); |
| 3290 | ICount += BlockRange.second - BlockRange.first; |
| 3291 | } |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 3292 | initializeCongruenceClasses(F); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3293 | |
| Daniel Berlin | e0bd37e | 2016-12-29 22:15:12 +0000 | [diff] [blame] | 3294 | TouchedInstructions.resize(ICount); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3295 | // Ensure we don't end up resizing the expressionToClass map, as |
| 3296 | // that can be quite expensive. At most, we have one expression per |
| 3297 | // instruction. |
| Daniel Berlin | e0bd37e | 2016-12-29 22:15:12 +0000 | [diff] [blame] | 3298 | ExpressionToClass.reserve(ICount); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3299 | |
| 3300 | // Initialize the touched instructions to include the entry block. |
| 3301 | const auto &InstRange = BlockInstRange.lookup(&F.getEntryBlock()); |
| 3302 | TouchedInstructions.set(InstRange.first, InstRange.second); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 3303 | DEBUG(dbgs() << "Block " << getBlockName(&F.getEntryBlock()) |
| 3304 | << " marked reachable\n"); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3305 | ReachableBlocks.insert(&F.getEntryBlock()); |
| 3306 | |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 3307 | iterateTouchedInstructions(); |
| Daniel Berlin | 589cecc | 2017-01-02 18:00:46 +0000 | [diff] [blame] | 3308 | verifyMemoryCongruency(); |
| Daniel Berlin | 06329a9 | 2017-03-18 15:41:40 +0000 | [diff] [blame] | 3309 | verifyIterationSettled(F); |
| Daniel Berlin | 4540357 | 2017-05-16 19:58:47 +0000 | [diff] [blame] | 3310 | verifyStoreExpressions(); |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 3311 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3312 | Changed |= eliminateInstructions(F); |
| 3313 | |
| 3314 | // Delete all instructions marked for deletion. |
| 3315 | for (Instruction *ToErase : InstructionsToErase) { |
| 3316 | if (!ToErase->use_empty()) |
| 3317 | ToErase->replaceAllUsesWith(UndefValue::get(ToErase->getType())); |
| 3318 | |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 3319 | if (ToErase->getParent()) |
| 3320 | ToErase->eraseFromParent(); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3321 | } |
| 3322 | |
| 3323 | // Delete all unreachable blocks. |
| Daniel Berlin | 85f91b0 | 2016-12-26 20:06:58 +0000 | [diff] [blame] | 3324 | auto UnreachableBlockPred = [&](const BasicBlock &BB) { |
| 3325 | return !ReachableBlocks.count(&BB); |
| 3326 | }; |
| Daniel Berlin | 85cbc8c | 2016-12-26 19:57:25 +0000 | [diff] [blame] | 3327 | |
| 3328 | for (auto &BB : make_filter_range(F, UnreachableBlockPred)) { |
| 3329 | DEBUG(dbgs() << "We believe block " << getBlockName(&BB) |
| Daniel Berlin | 85f91b0 | 2016-12-26 20:06:58 +0000 | [diff] [blame] | 3330 | << " is unreachable\n"); |
| Daniel Berlin | 85cbc8c | 2016-12-26 19:57:25 +0000 | [diff] [blame] | 3331 | deleteInstructionsInBlock(&BB); |
| 3332 | Changed = true; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | cleanupTables(); |
| 3336 | return Changed; |
| 3337 | } |
| 3338 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3339 | struct NewGVN::ValueDFS { |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 3340 | int DFSIn = 0; |
| 3341 | int DFSOut = 0; |
| 3342 | int LocalNum = 0; |
| Daniel Berlin | c0e008d | 2017-03-10 00:32:26 +0000 | [diff] [blame] | 3343 | // Only one of Def and U will be set. |
| Daniel Berlin | 9a9c9ff | 2017-04-01 09:44:33 +0000 | [diff] [blame] | 3344 | // The bool in the Def tells us whether the Def is the stored value of a |
| 3345 | // store. |
| 3346 | PointerIntPair<Value *, 1, bool> Def; |
| Piotr Padlewski | fc5727b | 2016-12-28 19:17:17 +0000 | [diff] [blame] | 3347 | Use *U = nullptr; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3348 | bool operator<(const ValueDFS &Other) const { |
| 3349 | // It's not enough that any given field be less than - we have sets |
| 3350 | // of fields that need to be evaluated together to give a proper ordering. |
| 3351 | // For example, if you have; |
| 3352 | // DFS (1, 3) |
| 3353 | // Val 0 |
| 3354 | // DFS (1, 2) |
| 3355 | // Val 50 |
| 3356 | // We want the second to be less than the first, but if we just go field |
| 3357 | // by field, we will get to Val 0 < Val 50 and say the first is less than |
| 3358 | // the second. We only want it to be less than if the DFS orders are equal. |
| 3359 | // |
| 3360 | // Each LLVM instruction only produces one value, and thus the lowest-level |
| 3361 | // differentiator that really matters for the stack (and what we use as as a |
| 3362 | // replacement) is the local dfs number. |
| Daniel Berlin | 85f91b0 | 2016-12-26 20:06:58 +0000 | [diff] [blame] | 3363 | // Everything else in the structure is instruction level, and only affects |
| 3364 | // the order in which we will replace operands of a given instruction. |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3365 | // |
| 3366 | // For a given instruction (IE things with equal dfsin, dfsout, localnum), |
| 3367 | // the order of replacement of uses does not matter. |
| 3368 | // IE given, |
| 3369 | // a = 5 |
| 3370 | // b = a + a |
| Daniel Berlin | 85f91b0 | 2016-12-26 20:06:58 +0000 | [diff] [blame] | 3371 | // When you hit b, you will have two valuedfs with the same dfsin, out, and |
| 3372 | // localnum. |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3373 | // The .val will be the same as well. |
| 3374 | // The .u's will be different. |
| Daniel Berlin | 85f91b0 | 2016-12-26 20:06:58 +0000 | [diff] [blame] | 3375 | // You will replace both, and it does not matter what order you replace them |
| 3376 | // in (IE whether you replace operand 2, then operand 1, or operand 1, then |
| 3377 | // operand 2). |
| 3378 | // Similarly for the case of same dfsin, dfsout, localnum, but different |
| 3379 | // .val's |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3380 | // a = 5 |
| 3381 | // b = 6 |
| 3382 | // c = a + b |
| Daniel Berlin | 85f91b0 | 2016-12-26 20:06:58 +0000 | [diff] [blame] | 3383 | // in c, we will a valuedfs for a, and one for b,with everything the same |
| 3384 | // but .val and .u. |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3385 | // It does not matter what order we replace these operands in. |
| 3386 | // You will always end up with the same IR, and this is guaranteed. |
| Daniel Berlin | c0e008d | 2017-03-10 00:32:26 +0000 | [diff] [blame] | 3387 | return std::tie(DFSIn, DFSOut, LocalNum, Def, U) < |
| 3388 | std::tie(Other.DFSIn, Other.DFSOut, Other.LocalNum, Other.Def, |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3389 | Other.U); |
| 3390 | } |
| 3391 | }; |
| 3392 | |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 3393 | // This function converts the set of members for a congruence class from values, |
| Daniel Berlin | c0e008d | 2017-03-10 00:32:26 +0000 | [diff] [blame] | 3394 | // to sets of defs and uses with associated DFS info. The total number of |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3395 | // reachable uses for each value is stored in UseCount, and instructions that |
| 3396 | // seem |
| 3397 | // dead (have no non-dead uses) are stored in ProbablyDead. |
| 3398 | void NewGVN::convertClassToDFSOrdered( |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 3399 | const CongruenceClass &Dense, SmallVectorImpl<ValueDFS> &DFSOrderedSet, |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3400 | DenseMap<const Value *, unsigned int> &UseCounts, |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 3401 | SmallPtrSetImpl<Instruction *> &ProbablyDead) const { |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3402 | for (auto D : Dense) { |
| 3403 | // First add the value. |
| 3404 | BasicBlock *BB = getBlockForValue(D); |
| 3405 | // Constants are handled prior to ever calling this function, so |
| 3406 | // we should only be left with instructions as members. |
| Chandler Carruth | ee08676 | 2016-12-23 01:38:06 +0000 | [diff] [blame] | 3407 | assert(BB && "Should have figured out a basic block for value"); |
| Daniel Berlin | c0e008d | 2017-03-10 00:32:26 +0000 | [diff] [blame] | 3408 | ValueDFS VDDef; |
| Daniel Berlin | b66164c | 2017-01-14 00:24:23 +0000 | [diff] [blame] | 3409 | DomTreeNode *DomNode = DT->getNode(BB); |
| Daniel Berlin | c0e008d | 2017-03-10 00:32:26 +0000 | [diff] [blame] | 3410 | VDDef.DFSIn = DomNode->getDFSNumIn(); |
| 3411 | VDDef.DFSOut = DomNode->getDFSNumOut(); |
| Daniel Berlin | 9a9c9ff | 2017-04-01 09:44:33 +0000 | [diff] [blame] | 3412 | // If it's a store, use the leader of the value operand, if it's always |
| 3413 | // available, or the value operand. TODO: We could do dominance checks to |
| 3414 | // find a dominating leader, but not worth it ATM. |
| Daniel Berlin | 26addef | 2017-01-20 21:04:30 +0000 | [diff] [blame] | 3415 | if (auto *SI = dyn_cast<StoreInst>(D)) { |
| Daniel Berlin | 808e3ff | 2017-01-31 22:31:56 +0000 | [diff] [blame] | 3416 | auto Leader = lookupOperandLeader(SI->getValueOperand()); |
| Daniel Berlin | 9a9c9ff | 2017-04-01 09:44:33 +0000 | [diff] [blame] | 3417 | if (alwaysAvailable(Leader)) { |
| 3418 | VDDef.Def.setPointer(Leader); |
| 3419 | } else { |
| 3420 | VDDef.Def.setPointer(SI->getValueOperand()); |
| 3421 | VDDef.Def.setInt(true); |
| 3422 | } |
| Daniel Berlin | 26addef | 2017-01-20 21:04:30 +0000 | [diff] [blame] | 3423 | } else { |
| Daniel Berlin | 9a9c9ff | 2017-04-01 09:44:33 +0000 | [diff] [blame] | 3424 | VDDef.Def.setPointer(D); |
| Daniel Berlin | 26addef | 2017-01-20 21:04:30 +0000 | [diff] [blame] | 3425 | } |
| Daniel Berlin | c0e008d | 2017-03-10 00:32:26 +0000 | [diff] [blame] | 3426 | assert(isa<Instruction>(D) && |
| 3427 | "The dense set member should always be an instruction"); |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3428 | Instruction *Def = cast<Instruction>(D); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 3429 | VDDef.LocalNum = InstrToDFSNum(D); |
| 3430 | DFSOrderedSet.push_back(VDDef); |
| 3431 | // If there is a phi node equivalent, add it |
| 3432 | if (auto *PN = RealToTemp.lookup(Def)) { |
| 3433 | auto *PHIE = |
| 3434 | dyn_cast_or_null<PHIExpression>(ValueToExpression.lookup(Def)); |
| 3435 | if (PHIE) { |
| 3436 | VDDef.Def.setInt(false); |
| 3437 | VDDef.Def.setPointer(PN); |
| 3438 | VDDef.LocalNum = 0; |
| 3439 | DFSOrderedSet.push_back(VDDef); |
| 3440 | } |
| 3441 | } |
| 3442 | |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3443 | unsigned int UseCount = 0; |
| Daniel Berlin | b66164c | 2017-01-14 00:24:23 +0000 | [diff] [blame] | 3444 | // Now add the uses. |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3445 | for (auto &U : Def->uses()) { |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3446 | if (auto *I = dyn_cast<Instruction>(U.getUser())) { |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3447 | // Don't try to replace into dead uses |
| 3448 | if (InstructionsToErase.count(I)) |
| 3449 | continue; |
| Daniel Berlin | c0e008d | 2017-03-10 00:32:26 +0000 | [diff] [blame] | 3450 | ValueDFS VDUse; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3451 | // Put the phi node uses in the incoming block. |
| 3452 | BasicBlock *IBlock; |
| 3453 | if (auto *P = dyn_cast<PHINode>(I)) { |
| 3454 | IBlock = P->getIncomingBlock(U); |
| 3455 | // Make phi node users appear last in the incoming block |
| 3456 | // they are from. |
| Daniel Berlin | c0e008d | 2017-03-10 00:32:26 +0000 | [diff] [blame] | 3457 | VDUse.LocalNum = InstrDFS.size() + 1; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3458 | } else { |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 3459 | IBlock = getBlockForValue(I); |
| Daniel Berlin | 21279bd | 2017-04-06 18:52:58 +0000 | [diff] [blame] | 3460 | VDUse.LocalNum = InstrToDFSNum(I); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3461 | } |
| Davide Italiano | ccbbc83 | 2017-01-26 00:42:42 +0000 | [diff] [blame] | 3462 | |
| 3463 | // Skip uses in unreachable blocks, as we're going |
| 3464 | // to delete them. |
| 3465 | if (ReachableBlocks.count(IBlock) == 0) |
| 3466 | continue; |
| 3467 | |
| Daniel Berlin | b66164c | 2017-01-14 00:24:23 +0000 | [diff] [blame] | 3468 | DomTreeNode *DomNode = DT->getNode(IBlock); |
| Daniel Berlin | c0e008d | 2017-03-10 00:32:26 +0000 | [diff] [blame] | 3469 | VDUse.DFSIn = DomNode->getDFSNumIn(); |
| 3470 | VDUse.DFSOut = DomNode->getDFSNumOut(); |
| 3471 | VDUse.U = &U; |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3472 | ++UseCount; |
| Daniel Berlin | c0e008d | 2017-03-10 00:32:26 +0000 | [diff] [blame] | 3473 | DFSOrderedSet.emplace_back(VDUse); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3474 | } |
| 3475 | } |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3476 | |
| 3477 | // If there are no uses, it's probably dead (but it may have side-effects, |
| 3478 | // so not definitely dead. Otherwise, store the number of uses so we can |
| 3479 | // track if it becomes dead later). |
| 3480 | if (UseCount == 0) |
| 3481 | ProbablyDead.insert(Def); |
| 3482 | else |
| 3483 | UseCounts[Def] = UseCount; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3484 | } |
| 3485 | } |
| 3486 | |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 3487 | // This function converts the set of members for a congruence class from values, |
| 3488 | // to the set of defs for loads and stores, with associated DFS info. |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3489 | void NewGVN::convertClassToLoadsAndStores( |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 3490 | const CongruenceClass &Dense, |
| 3491 | SmallVectorImpl<ValueDFS> &LoadsAndStores) const { |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 3492 | for (auto D : Dense) { |
| 3493 | if (!isa<LoadInst>(D) && !isa<StoreInst>(D)) |
| 3494 | continue; |
| 3495 | |
| 3496 | BasicBlock *BB = getBlockForValue(D); |
| 3497 | ValueDFS VD; |
| 3498 | DomTreeNode *DomNode = DT->getNode(BB); |
| 3499 | VD.DFSIn = DomNode->getDFSNumIn(); |
| 3500 | VD.DFSOut = DomNode->getDFSNumOut(); |
| Daniel Berlin | 9a9c9ff | 2017-04-01 09:44:33 +0000 | [diff] [blame] | 3501 | VD.Def.setPointer(D); |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 3502 | |
| 3503 | // If it's an instruction, use the real local dfs number. |
| 3504 | if (auto *I = dyn_cast<Instruction>(D)) |
| Daniel Berlin | 21279bd | 2017-04-06 18:52:58 +0000 | [diff] [blame] | 3505 | VD.LocalNum = InstrToDFSNum(I); |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 3506 | else |
| 3507 | llvm_unreachable("Should have been an instruction"); |
| 3508 | |
| 3509 | LoadsAndStores.emplace_back(VD); |
| 3510 | } |
| 3511 | } |
| 3512 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3513 | static void patchReplacementInstruction(Instruction *I, Value *Repl) { |
| Daniel Berlin | 4d54796 | 2017-02-12 23:24:45 +0000 | [diff] [blame] | 3514 | auto *ReplInst = dyn_cast<Instruction>(Repl); |
| Daniel Berlin | 86eab15 | 2017-02-12 22:25:20 +0000 | [diff] [blame] | 3515 | if (!ReplInst) |
| 3516 | return; |
| 3517 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3518 | // Patch the replacement so that it is not more restrictive than the value |
| 3519 | // being replaced. |
| Daniel Berlin | 86eab15 | 2017-02-12 22:25:20 +0000 | [diff] [blame] | 3520 | // Note that if 'I' is a load being replaced by some operation, |
| 3521 | // for example, by an arithmetic operation, then andIRFlags() |
| 3522 | // would just erase all math flags from the original arithmetic |
| 3523 | // operation, which is clearly not wanted and not needed. |
| 3524 | if (!isa<LoadInst>(I)) |
| 3525 | ReplInst->andIRFlags(I); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3526 | |
| Daniel Berlin | 86eab15 | 2017-02-12 22:25:20 +0000 | [diff] [blame] | 3527 | // FIXME: If both the original and replacement value are part of the |
| 3528 | // same control-flow region (meaning that the execution of one |
| 3529 | // guarantees the execution of the other), then we can combine the |
| 3530 | // noalias scopes here and do better than the general conservative |
| 3531 | // answer used in combineMetadata(). |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3532 | |
| Daniel Berlin | 86eab15 | 2017-02-12 22:25:20 +0000 | [diff] [blame] | 3533 | // In general, GVN unifies expressions over different control-flow |
| 3534 | // regions, and so we need a conservative combination of the noalias |
| 3535 | // scopes. |
| 3536 | static const unsigned KnownIDs[] = { |
| 3537 | LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope, |
| 3538 | LLVMContext::MD_noalias, LLVMContext::MD_range, |
| 3539 | LLVMContext::MD_fpmath, LLVMContext::MD_invariant_load, |
| 3540 | LLVMContext::MD_invariant_group}; |
| 3541 | combineMetadata(ReplInst, I, KnownIDs); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
| 3544 | static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl) { |
| 3545 | patchReplacementInstruction(I, Repl); |
| 3546 | I->replaceAllUsesWith(Repl); |
| 3547 | } |
| 3548 | |
| 3549 | void NewGVN::deleteInstructionsInBlock(BasicBlock *BB) { |
| 3550 | DEBUG(dbgs() << " BasicBlock Dead:" << *BB); |
| 3551 | ++NumGVNBlocksDeleted; |
| 3552 | |
| Daniel Berlin | e19f0e0 | 2017-01-30 17:06:55 +0000 | [diff] [blame] | 3553 | // Delete the instructions backwards, as it has a reduced likelihood of having |
| 3554 | // to update as many def-use and use-def chains. Start after the terminator. |
| 3555 | auto StartPoint = BB->rbegin(); |
| 3556 | ++StartPoint; |
| 3557 | // Note that we explicitly recalculate BB->rend() on each iteration, |
| 3558 | // as it may change when we remove the first instruction. |
| 3559 | for (BasicBlock::reverse_iterator I(StartPoint); I != BB->rend();) { |
| 3560 | Instruction &Inst = *I++; |
| 3561 | if (!Inst.use_empty()) |
| 3562 | Inst.replaceAllUsesWith(UndefValue::get(Inst.getType())); |
| 3563 | if (isa<LandingPadInst>(Inst)) |
| 3564 | continue; |
| 3565 | |
| 3566 | Inst.eraseFromParent(); |
| 3567 | ++NumGVNInstrDeleted; |
| 3568 | } |
| Daniel Berlin | a53a722 | 2017-01-30 18:12:56 +0000 | [diff] [blame] | 3569 | // Now insert something that simplifycfg will turn into an unreachable. |
| 3570 | Type *Int8Ty = Type::getInt8Ty(BB->getContext()); |
| 3571 | new StoreInst(UndefValue::get(Int8Ty), |
| 3572 | Constant::getNullValue(Int8Ty->getPointerTo()), |
| 3573 | BB->getTerminator()); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
| 3576 | void NewGVN::markInstructionForDeletion(Instruction *I) { |
| 3577 | DEBUG(dbgs() << "Marking " << *I << " for deletion\n"); |
| 3578 | InstructionsToErase.insert(I); |
| 3579 | } |
| 3580 | |
| 3581 | void NewGVN::replaceInstruction(Instruction *I, Value *V) { |
| 3582 | |
| 3583 | DEBUG(dbgs() << "Replacing " << *I << " with " << *V << "\n"); |
| 3584 | patchAndReplaceAllUsesWith(I, V); |
| 3585 | // We save the actual erasing to avoid invalidating memory |
| 3586 | // dependencies until we are done with everything. |
| 3587 | markInstructionForDeletion(I); |
| 3588 | } |
| 3589 | |
| 3590 | namespace { |
| 3591 | |
| 3592 | // This is a stack that contains both the value and dfs info of where |
| 3593 | // that value is valid. |
| 3594 | class ValueDFSStack { |
| 3595 | public: |
| 3596 | Value *back() const { return ValueStack.back(); } |
| 3597 | std::pair<int, int> dfs_back() const { return DFSStack.back(); } |
| 3598 | |
| 3599 | void push_back(Value *V, int DFSIn, int DFSOut) { |
| Piotr Padlewski | 6c37d29 | 2016-12-28 23:24:02 +0000 | [diff] [blame] | 3600 | ValueStack.emplace_back(V); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3601 | DFSStack.emplace_back(DFSIn, DFSOut); |
| 3602 | } |
| 3603 | bool empty() const { return DFSStack.empty(); } |
| 3604 | bool isInScope(int DFSIn, int DFSOut) const { |
| 3605 | if (empty()) |
| 3606 | return false; |
| 3607 | return DFSIn >= DFSStack.back().first && DFSOut <= DFSStack.back().second; |
| 3608 | } |
| 3609 | |
| 3610 | void popUntilDFSScope(int DFSIn, int DFSOut) { |
| 3611 | |
| 3612 | // These two should always be in sync at this point. |
| 3613 | assert(ValueStack.size() == DFSStack.size() && |
| 3614 | "Mismatch between ValueStack and DFSStack"); |
| 3615 | while ( |
| 3616 | !DFSStack.empty() && |
| 3617 | !(DFSIn >= DFSStack.back().first && DFSOut <= DFSStack.back().second)) { |
| 3618 | DFSStack.pop_back(); |
| 3619 | ValueStack.pop_back(); |
| 3620 | } |
| 3621 | } |
| 3622 | |
| 3623 | private: |
| 3624 | SmallVector<Value *, 8> ValueStack; |
| 3625 | SmallVector<std::pair<int, int>, 8> DFSStack; |
| 3626 | }; |
| 3627 | } |
| Daniel Berlin | 0444343 | 2017-01-07 03:23:47 +0000 | [diff] [blame] | 3628 | |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 3629 | // Given an expression, get the congruence class for it. |
| 3630 | CongruenceClass *NewGVN::getClassForExpression(const Expression *E) const { |
| 3631 | if (auto *VE = dyn_cast<VariableExpression>(E)) |
| 3632 | return ValueToClass.lookup(VE->getVariableValue()); |
| 3633 | else if (isa<DeadExpression>(E)) |
| 3634 | return TOPClass; |
| 3635 | return ExpressionToClass.lookup(E); |
| 3636 | } |
| 3637 | |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 3638 | // Given a value and a basic block we are trying to see if it is available in, |
| 3639 | // see if the value has a leader available in that block. |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 3640 | Value *NewGVN::findPHIOfOpsLeader(const Expression *E, |
| Daniel Berlin | 4ad7e8d | 2017-09-05 02:17:40 +0000 | [diff] [blame] | 3641 | const Instruction *OrigInst, |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 3642 | const BasicBlock *BB) const { |
| 3643 | // It would already be constant if we could make it constant |
| 3644 | if (auto *CE = dyn_cast<ConstantExpression>(E)) |
| 3645 | return CE->getConstantValue(); |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 3646 | if (auto *VE = dyn_cast<VariableExpression>(E)) { |
| 3647 | auto *V = VE->getVariableValue(); |
| 3648 | if (alwaysAvailable(V) || DT->dominates(getBlockForValue(V), BB)) |
| 3649 | return VE->getVariableValue(); |
| 3650 | } |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 3651 | |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 3652 | auto *CC = getClassForExpression(E); |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 3653 | if (!CC) |
| 3654 | return nullptr; |
| 3655 | if (alwaysAvailable(CC->getLeader())) |
| 3656 | return CC->getLeader(); |
| 3657 | |
| 3658 | for (auto Member : *CC) { |
| 3659 | auto *MemberInst = dyn_cast<Instruction>(Member); |
| Daniel Berlin | 4ad7e8d | 2017-09-05 02:17:40 +0000 | [diff] [blame] | 3660 | if (MemberInst == OrigInst) |
| 3661 | continue; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 3662 | // Anything that isn't an instruction is always available. |
| 3663 | if (!MemberInst) |
| 3664 | return Member; |
| Daniel Berlin | 94090dd | 2017-09-02 02:18:44 +0000 | [diff] [blame] | 3665 | if (DT->dominates(getBlockForValue(MemberInst), BB)) |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 3666 | return Member; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 3667 | } |
| 3668 | return nullptr; |
| 3669 | } |
| 3670 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3671 | bool NewGVN::eliminateInstructions(Function &F) { |
| 3672 | // This is a non-standard eliminator. The normal way to eliminate is |
| 3673 | // to walk the dominator tree in order, keeping track of available |
| 3674 | // values, and eliminating them. However, this is mildly |
| 3675 | // pointless. It requires doing lookups on every instruction, |
| 3676 | // regardless of whether we will ever eliminate it. For |
| Daniel Berlin | 85cbc8c | 2016-12-26 19:57:25 +0000 | [diff] [blame] | 3677 | // instructions part of most singleton congruence classes, we know we |
| 3678 | // will never eliminate them. |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3679 | |
| 3680 | // Instead, this eliminator looks at the congruence classes directly, sorts |
| 3681 | // them into a DFS ordering of the dominator tree, and then we just |
| Daniel Berlin | 85cbc8c | 2016-12-26 19:57:25 +0000 | [diff] [blame] | 3682 | // perform elimination straight on the sets by walking the congruence |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3683 | // class member uses in order, and eliminate the ones dominated by the |
| Daniel Berlin | 85cbc8c | 2016-12-26 19:57:25 +0000 | [diff] [blame] | 3684 | // last member. This is worst case O(E log E) where E = number of |
| 3685 | // instructions in a single congruence class. In theory, this is all |
| 3686 | // instructions. In practice, it is much faster, as most instructions are |
| 3687 | // either in singleton congruence classes or can't possibly be eliminated |
| 3688 | // anyway (if there are no overlapping DFS ranges in class). |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3689 | // When we find something not dominated, it becomes the new leader |
| Daniel Berlin | 85cbc8c | 2016-12-26 19:57:25 +0000 | [diff] [blame] | 3690 | // for elimination purposes. |
| 3691 | // TODO: If we wanted to be faster, We could remove any members with no |
| 3692 | // overlapping ranges while sorting, as we will never eliminate anything |
| 3693 | // with those members, as they don't dominate anything else in our set. |
| 3694 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3695 | bool AnythingReplaced = false; |
| 3696 | |
| 3697 | // Since we are going to walk the domtree anyway, and we can't guarantee the |
| 3698 | // DFS numbers are updated, we compute some ourselves. |
| 3699 | DT->updateDFSNumbers(); |
| 3700 | |
| Daniel Berlin | 0207cca | 2017-05-21 23:41:56 +0000 | [diff] [blame] | 3701 | // Go through all of our phi nodes, and kill the arguments associated with |
| 3702 | // unreachable edges. |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 3703 | auto ReplaceUnreachablePHIArgs = [&](PHINode &PHI, BasicBlock *BB) { |
| 3704 | for (auto &Operand : PHI.incoming_values()) |
| 3705 | if (!ReachableEdges.count({PHI.getIncomingBlock(Operand), BB})) { |
| 3706 | DEBUG(dbgs() << "Replacing incoming value of " << PHI << " for block " |
| 3707 | << getBlockName(PHI.getIncomingBlock(Operand)) |
| 3708 | << " with undef due to it being unreachable\n"); |
| 3709 | Operand.set(UndefValue::get(PHI.getType())); |
| 3710 | } |
| 3711 | }; |
| 3712 | SmallPtrSet<BasicBlock *, 8> BlocksWithPhis; |
| 3713 | for (auto &B : F) |
| 3714 | if ((!B.empty() && isa<PHINode>(*B.begin())) || |
| 3715 | (PHIOfOpsPHIs.find(&B) != PHIOfOpsPHIs.end())) |
| 3716 | BlocksWithPhis.insert(&B); |
| 3717 | DenseMap<const BasicBlock *, unsigned> ReachablePredCount; |
| 3718 | for (auto KV : ReachableEdges) |
| 3719 | ReachablePredCount[KV.getEnd()]++; |
| 3720 | for (auto *BB : BlocksWithPhis) |
| 3721 | // TODO: It would be faster to use getNumIncomingBlocks() on a phi node in |
| 3722 | // the block and subtract the pred count, but it's more complicated. |
| 3723 | if (ReachablePredCount.lookup(BB) != |
| George Burgess IV | f613749 | 2017-06-13 01:28:49 +0000 | [diff] [blame] | 3724 | unsigned(std::distance(pred_begin(BB), pred_end(BB)))) { |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 3725 | for (auto II = BB->begin(); isa<PHINode>(II); ++II) { |
| 3726 | auto &PHI = cast<PHINode>(*II); |
| 3727 | ReplaceUnreachablePHIArgs(PHI, BB); |
| 3728 | } |
| Daniel Berlin | 0207cca | 2017-05-21 23:41:56 +0000 | [diff] [blame] | 3729 | for_each_found(PHIOfOpsPHIs, BB, [&](PHINode *PHI) { |
| 3730 | ReplaceUnreachablePHIArgs(*PHI, BB); |
| 3731 | }); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3732 | } |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3733 | |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3734 | // Map to store the use counts |
| 3735 | DenseMap<const Value *, unsigned int> UseCounts; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 3736 | for (auto *CC : reverse(CongruenceClasses)) { |
| Daniel Berlin | e67c322 | 2017-05-25 15:44:20 +0000 | [diff] [blame] | 3737 | DEBUG(dbgs() << "Eliminating in congruence class " << CC->getID() << "\n"); |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 3738 | // Track the equivalent store info so we can decide whether to try |
| 3739 | // dead store elimination. |
| 3740 | SmallVector<ValueDFS, 8> PossibleDeadStores; |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3741 | SmallPtrSet<Instruction *, 8> ProbablyDead; |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 3742 | if (CC->isDead() || CC->empty()) |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3743 | continue; |
| Daniel Berlin | 5c338ff | 2017-03-10 19:05:04 +0000 | [diff] [blame] | 3744 | // Everything still in the TOP class is unreachable or dead. |
| 3745 | if (CC == TOPClass) { |
| Daniel Berlin | e021d2d | 2017-05-19 20:22:20 +0000 | [diff] [blame] | 3746 | for (auto M : *CC) { |
| 3747 | auto *VTE = ValueToExpression.lookup(M); |
| 3748 | if (VTE && isa<DeadExpression>(VTE)) |
| 3749 | markInstructionForDeletion(cast<Instruction>(M)); |
| Daniel Berlin | b79f536 | 2017-02-11 12:48:50 +0000 | [diff] [blame] | 3750 | assert((!ReachableBlocks.count(cast<Instruction>(M)->getParent()) || |
| 3751 | InstructionsToErase.count(cast<Instruction>(M))) && |
| Daniel Berlin | 5c338ff | 2017-03-10 19:05:04 +0000 | [diff] [blame] | 3752 | "Everything in TOP should be unreachable or dead at this " |
| Daniel Berlin | b79f536 | 2017-02-11 12:48:50 +0000 | [diff] [blame] | 3753 | "point"); |
| Daniel Berlin | e021d2d | 2017-05-19 20:22:20 +0000 | [diff] [blame] | 3754 | } |
| Daniel Berlin | b79f536 | 2017-02-11 12:48:50 +0000 | [diff] [blame] | 3755 | continue; |
| 3756 | } |
| 3757 | |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 3758 | assert(CC->getLeader() && "We should have had a leader"); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3759 | // If this is a leader that is always available, and it's a |
| 3760 | // constant or has no equivalences, just replace everything with |
| 3761 | // it. We then update the congruence class with whatever members |
| 3762 | // are left. |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 3763 | Value *Leader = |
| 3764 | CC->getStoredValue() ? CC->getStoredValue() : CC->getLeader(); |
| Daniel Berlin | 26addef | 2017-01-20 21:04:30 +0000 | [diff] [blame] | 3765 | if (alwaysAvailable(Leader)) { |
| Daniel Berlin | 08fe6e0 | 2017-04-06 18:52:55 +0000 | [diff] [blame] | 3766 | CongruenceClass::MemberSet MembersLeft; |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 3767 | for (auto M : *CC) { |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3768 | Value *Member = M; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3769 | // Void things have no uses we can replace. |
| Daniel Berlin | 08fe6e0 | 2017-04-06 18:52:55 +0000 | [diff] [blame] | 3770 | if (Member == Leader || !isa<Instruction>(Member) || |
| 3771 | Member->getType()->isVoidTy()) { |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3772 | MembersLeft.insert(Member); |
| 3773 | continue; |
| 3774 | } |
| Daniel Berlin | 26addef | 2017-01-20 21:04:30 +0000 | [diff] [blame] | 3775 | DEBUG(dbgs() << "Found replacement " << *(Leader) << " for " << *Member |
| 3776 | << "\n"); |
| Daniel Berlin | 08fe6e0 | 2017-04-06 18:52:55 +0000 | [diff] [blame] | 3777 | auto *I = cast<Instruction>(Member); |
| 3778 | assert(Leader != I && "About to accidentally remove our leader"); |
| 3779 | replaceInstruction(I, Leader); |
| 3780 | AnythingReplaced = true; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3781 | } |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 3782 | CC->swap(MembersLeft); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3783 | } else { |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3784 | // If this is a singleton, we can skip it. |
| Davide Italiano | 5974c31 | 2017-08-03 21:17:49 +0000 | [diff] [blame] | 3785 | if (CC->size() != 1 || RealToTemp.count(Leader)) { |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3786 | // This is a stack because equality replacement/etc may place |
| 3787 | // constants in the middle of the member list, and we want to use |
| 3788 | // those constant values in preference to the current leader, over |
| 3789 | // the scope of those constants. |
| 3790 | ValueDFSStack EliminationStack; |
| 3791 | |
| 3792 | // Convert the members to DFS ordered sets and then merge them. |
| Daniel Berlin | 2f1fbcc | 2017-01-09 05:34:19 +0000 | [diff] [blame] | 3793 | SmallVector<ValueDFS, 8> DFSOrderedSet; |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 3794 | convertClassToDFSOrdered(*CC, DFSOrderedSet, UseCounts, ProbablyDead); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3795 | |
| 3796 | // Sort the whole thing. |
| Daniel Berlin | 2f1fbcc | 2017-01-09 05:34:19 +0000 | [diff] [blame] | 3797 | std::sort(DFSOrderedSet.begin(), DFSOrderedSet.end()); |
| Daniel Berlin | 2f1fbcc | 2017-01-09 05:34:19 +0000 | [diff] [blame] | 3798 | for (auto &VD : DFSOrderedSet) { |
| 3799 | int MemberDFSIn = VD.DFSIn; |
| 3800 | int MemberDFSOut = VD.DFSOut; |
| Daniel Berlin | 9a9c9ff | 2017-04-01 09:44:33 +0000 | [diff] [blame] | 3801 | Value *Def = VD.Def.getPointer(); |
| 3802 | bool FromStore = VD.Def.getInt(); |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3803 | Use *U = VD.U; |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 3804 | // We ignore void things because we can't get a value from them. |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3805 | if (Def && Def->getType()->isVoidTy()) |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 3806 | continue; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 3807 | auto *DefInst = dyn_cast_or_null<Instruction>(Def); |
| 3808 | if (DefInst && AllTempInstructions.count(DefInst)) { |
| 3809 | auto *PN = cast<PHINode>(DefInst); |
| 3810 | |
| 3811 | // If this is a value phi and that's the expression we used, insert |
| 3812 | // it into the program |
| 3813 | // remove from temp instruction list. |
| 3814 | AllTempInstructions.erase(PN); |
| 3815 | auto *DefBlock = getBlockForValue(Def); |
| 3816 | DEBUG(dbgs() << "Inserting fully real phi of ops" << *Def |
| 3817 | << " into block " |
| 3818 | << getBlockName(getBlockForValue(Def)) << "\n"); |
| 3819 | PN->insertBefore(&DefBlock->front()); |
| 3820 | Def = PN; |
| 3821 | NumGVNPHIOfOpsEliminations++; |
| 3822 | } |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3823 | |
| 3824 | if (EliminationStack.empty()) { |
| 3825 | DEBUG(dbgs() << "Elimination Stack is empty\n"); |
| 3826 | } else { |
| 3827 | DEBUG(dbgs() << "Elimination Stack Top DFS numbers are (" |
| 3828 | << EliminationStack.dfs_back().first << "," |
| 3829 | << EliminationStack.dfs_back().second << ")\n"); |
| 3830 | } |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3831 | |
| 3832 | DEBUG(dbgs() << "Current DFS numbers are (" << MemberDFSIn << "," |
| 3833 | << MemberDFSOut << ")\n"); |
| 3834 | // First, we see if we are out of scope or empty. If so, |
| 3835 | // and there equivalences, we try to replace the top of |
| 3836 | // stack with equivalences (if it's on the stack, it must |
| 3837 | // not have been eliminated yet). |
| 3838 | // Then we synchronize to our current scope, by |
| 3839 | // popping until we are back within a DFS scope that |
| 3840 | // dominates the current member. |
| 3841 | // Then, what happens depends on a few factors |
| 3842 | // If the stack is now empty, we need to push |
| 3843 | // If we have a constant or a local equivalence we want to |
| 3844 | // start using, we also push. |
| 3845 | // Otherwise, we walk along, processing members who are |
| 3846 | // dominated by this scope, and eliminate them. |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3847 | bool ShouldPush = Def && EliminationStack.empty(); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3848 | bool OutOfScope = |
| 3849 | !EliminationStack.isInScope(MemberDFSIn, MemberDFSOut); |
| 3850 | |
| 3851 | if (OutOfScope || ShouldPush) { |
| 3852 | // Sync to our current scope. |
| 3853 | EliminationStack.popUntilDFSScope(MemberDFSIn, MemberDFSOut); |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3854 | bool ShouldPush = Def && EliminationStack.empty(); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3855 | if (ShouldPush) { |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3856 | EliminationStack.push_back(Def, MemberDFSIn, MemberDFSOut); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3857 | } |
| 3858 | } |
| 3859 | |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3860 | // Skip the Def's, we only want to eliminate on their uses. But mark |
| 3861 | // dominated defs as dead. |
| 3862 | if (Def) { |
| 3863 | // For anything in this case, what and how we value number |
| 3864 | // guarantees that any side-effets that would have occurred (ie |
| 3865 | // throwing, etc) can be proven to either still occur (because it's |
| 3866 | // dominated by something that has the same side-effects), or never |
| 3867 | // occur. Otherwise, we would not have been able to prove it value |
| 3868 | // equivalent to something else. For these things, we can just mark |
| 3869 | // it all dead. Note that this is different from the "ProbablyDead" |
| 3870 | // set, which may not be dominated by anything, and thus, are only |
| Daniel Berlin | 9a9c9ff | 2017-04-01 09:44:33 +0000 | [diff] [blame] | 3871 | // easy to prove dead if they are also side-effect free. Note that |
| 3872 | // because stores are put in terms of the stored value, we skip |
| 3873 | // stored values here. If the stored value is really dead, it will |
| 3874 | // still be marked for deletion when we process it in its own class. |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3875 | if (!EliminationStack.empty() && Def != EliminationStack.back() && |
| Daniel Berlin | 9a9c9ff | 2017-04-01 09:44:33 +0000 | [diff] [blame] | 3876 | isa<Instruction>(Def) && !FromStore) |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3877 | markInstructionForDeletion(cast<Instruction>(Def)); |
| 3878 | continue; |
| 3879 | } |
| 3880 | // At this point, we know it is a Use we are trying to possibly |
| 3881 | // replace. |
| 3882 | |
| 3883 | assert(isa<Instruction>(U->get()) && |
| 3884 | "Current def should have been an instruction"); |
| 3885 | assert(isa<Instruction>(U->getUser()) && |
| 3886 | "Current user should have been an instruction"); |
| 3887 | |
| 3888 | // If the thing we are replacing into is already marked to be dead, |
| 3889 | // this use is dead. Note that this is true regardless of whether |
| 3890 | // we have anything dominating the use or not. We do this here |
| 3891 | // because we are already walking all the uses anyway. |
| 3892 | Instruction *InstUse = cast<Instruction>(U->getUser()); |
| 3893 | if (InstructionsToErase.count(InstUse)) { |
| 3894 | auto &UseCount = UseCounts[U->get()]; |
| 3895 | if (--UseCount == 0) { |
| 3896 | ProbablyDead.insert(cast<Instruction>(U->get())); |
| 3897 | } |
| Daniel Berlin | c0e008d | 2017-03-10 00:32:26 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3900 | // If we get to this point, and the stack is empty we must have a use |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3901 | // with nothing we can use to eliminate this use, so just skip it. |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3902 | if (EliminationStack.empty()) |
| 3903 | continue; |
| 3904 | |
| Daniel Berlin | c0e008d | 2017-03-10 00:32:26 +0000 | [diff] [blame] | 3905 | Value *DominatingLeader = EliminationStack.back(); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3906 | |
| Davide Italiano | a76e5fa | 2017-05-18 21:43:23 +0000 | [diff] [blame] | 3907 | auto *II = dyn_cast<IntrinsicInst>(DominatingLeader); |
| 3908 | if (II && II->getIntrinsicID() == Intrinsic::ssa_copy) |
| 3909 | DominatingLeader = II->getOperand(0); |
| 3910 | |
| Daniel Berlin | d92e7f9 | 2017-01-07 00:01:42 +0000 | [diff] [blame] | 3911 | // Don't replace our existing users with ourselves. |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3912 | if (U->get() == DominatingLeader) |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3913 | continue; |
| Daniel Berlin | c0e008d | 2017-03-10 00:32:26 +0000 | [diff] [blame] | 3914 | DEBUG(dbgs() << "Found replacement " << *DominatingLeader << " for " |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3915 | << *U->get() << " in " << *(U->getUser()) << "\n"); |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3916 | |
| 3917 | // If we replaced something in an instruction, handle the patching of |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3918 | // metadata. Skip this if we are replacing predicateinfo with its |
| 3919 | // original operand, as we already know we can just drop it. |
| 3920 | auto *ReplacedInst = cast<Instruction>(U->get()); |
| Daniel Berlin | c0e008d | 2017-03-10 00:32:26 +0000 | [diff] [blame] | 3921 | auto *PI = PredInfo->getPredicateInfoFor(ReplacedInst); |
| 3922 | if (!PI || DominatingLeader != PI->OriginalOp) |
| 3923 | patchReplacementInstruction(ReplacedInst, DominatingLeader); |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3924 | U->set(DominatingLeader); |
| 3925 | // This is now a use of the dominating leader, which means if the |
| 3926 | // dominating leader was dead, it's now live! |
| 3927 | auto &LeaderUseCount = UseCounts[DominatingLeader]; |
| 3928 | // It's about to be alive again. |
| 3929 | if (LeaderUseCount == 0 && isa<Instruction>(DominatingLeader)) |
| 3930 | ProbablyDead.erase(cast<Instruction>(DominatingLeader)); |
| Davide Italiano | a76e5fa | 2017-05-18 21:43:23 +0000 | [diff] [blame] | 3931 | if (LeaderUseCount == 0 && II) |
| 3932 | ProbablyDead.insert(II); |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3933 | ++LeaderUseCount; |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3934 | AnythingReplaced = true; |
| 3935 | } |
| 3936 | } |
| 3937 | } |
| 3938 | |
| Daniel Berlin | e3e69e1 | 2017-03-10 00:32:33 +0000 | [diff] [blame] | 3939 | // At this point, anything still in the ProbablyDead set is actually dead if |
| 3940 | // would be trivially dead. |
| 3941 | for (auto *I : ProbablyDead) |
| 3942 | if (wouldInstructionBeTriviallyDead(I)) |
| 3943 | markInstructionForDeletion(I); |
| 3944 | |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3945 | // Cleanup the congruence class. |
| Daniel Berlin | 08fe6e0 | 2017-04-06 18:52:55 +0000 | [diff] [blame] | 3946 | CongruenceClass::MemberSet MembersLeft; |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 3947 | for (auto *Member : *CC) |
| Daniel Berlin | 08fe6e0 | 2017-04-06 18:52:55 +0000 | [diff] [blame] | 3948 | if (!isa<Instruction>(Member) || |
| 3949 | !InstructionsToErase.count(cast<Instruction>(Member))) |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3950 | MembersLeft.insert(Member); |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 3951 | CC->swap(MembersLeft); |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 3952 | |
| 3953 | // If we have possible dead stores to look at, try to eliminate them. |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 3954 | if (CC->getStoreCount() > 0) { |
| 3955 | convertClassToLoadsAndStores(*CC, PossibleDeadStores); |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 3956 | std::sort(PossibleDeadStores.begin(), PossibleDeadStores.end()); |
| 3957 | ValueDFSStack EliminationStack; |
| 3958 | for (auto &VD : PossibleDeadStores) { |
| 3959 | int MemberDFSIn = VD.DFSIn; |
| 3960 | int MemberDFSOut = VD.DFSOut; |
| Daniel Berlin | 9a9c9ff | 2017-04-01 09:44:33 +0000 | [diff] [blame] | 3961 | Instruction *Member = cast<Instruction>(VD.Def.getPointer()); |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 3962 | if (EliminationStack.empty() || |
| 3963 | !EliminationStack.isInScope(MemberDFSIn, MemberDFSOut)) { |
| 3964 | // Sync to our current scope. |
| 3965 | EliminationStack.popUntilDFSScope(MemberDFSIn, MemberDFSOut); |
| 3966 | if (EliminationStack.empty()) { |
| 3967 | EliminationStack.push_back(Member, MemberDFSIn, MemberDFSOut); |
| 3968 | continue; |
| 3969 | } |
| 3970 | } |
| 3971 | // We already did load elimination, so nothing to do here. |
| 3972 | if (isa<LoadInst>(Member)) |
| 3973 | continue; |
| 3974 | assert(!EliminationStack.empty()); |
| 3975 | Instruction *Leader = cast<Instruction>(EliminationStack.back()); |
| Richard Trieu | 0b79aa3 | 2017-01-27 06:06:05 +0000 | [diff] [blame] | 3976 | (void)Leader; |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 3977 | assert(DT->dominates(Leader->getParent(), Member->getParent())); |
| 3978 | // Member is dominater by Leader, and thus dead |
| 3979 | DEBUG(dbgs() << "Marking dead store " << *Member |
| 3980 | << " that is dominated by " << *Leader << "\n"); |
| 3981 | markInstructionForDeletion(Member); |
| Daniel Berlin | a823656 | 2017-04-07 18:38:09 +0000 | [diff] [blame] | 3982 | CC->erase(Member); |
| Daniel Berlin | c479686 | 2017-01-27 02:37:11 +0000 | [diff] [blame] | 3983 | ++NumGVNDeadStores; |
| 3984 | } |
| 3985 | } |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3986 | } |
| Davide Italiano | 7e274e0 | 2016-12-22 16:03:48 +0000 | [diff] [blame] | 3987 | return AnythingReplaced; |
| 3988 | } |
| Daniel Berlin | 1c08767 | 2017-02-11 15:07:01 +0000 | [diff] [blame] | 3989 | |
| 3990 | // This function provides global ranking of operations so that we can place them |
| 3991 | // in a canonical order. Note that rank alone is not necessarily enough for a |
| 3992 | // complete ordering, as constants all have the same rank. However, generally, |
| 3993 | // we will simplify an operation with all constants so that it doesn't matter |
| 3994 | // what order they appear in. |
| 3995 | unsigned int NewGVN::getRank(const Value *V) const { |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 3996 | // Prefer constants to undef to anything else |
| 3997 | // Undef is a constant, have to check it first. |
| 3998 | // Prefer smaller constants to constantexprs |
| 3999 | if (isa<ConstantExpr>(V)) |
| 4000 | return 2; |
| Daniel Berlin | b355c4f | 2017-02-18 23:06:47 +0000 | [diff] [blame] | 4001 | if (isa<UndefValue>(V)) |
| Daniel Berlin | b355c4f | 2017-02-18 23:06:47 +0000 | [diff] [blame] | 4002 | return 1; |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 4003 | if (isa<Constant>(V)) |
| 4004 | return 0; |
| Daniel Berlin | 1c08767 | 2017-02-11 15:07:01 +0000 | [diff] [blame] | 4005 | else if (auto *A = dyn_cast<Argument>(V)) |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 4006 | return 3 + A->getArgNo(); |
| Daniel Berlin | 1c08767 | 2017-02-11 15:07:01 +0000 | [diff] [blame] | 4007 | |
| Daniel Berlin | b355c4f | 2017-02-18 23:06:47 +0000 | [diff] [blame] | 4008 | // Need to shift the instruction DFS by number of arguments + 3 to account for |
| Daniel Berlin | 1c08767 | 2017-02-11 15:07:01 +0000 | [diff] [blame] | 4009 | // the constant and argument ranking above. |
| Daniel Berlin | 21279bd | 2017-04-06 18:52:58 +0000 | [diff] [blame] | 4010 | unsigned Result = InstrToDFSNum(V); |
| Daniel Berlin | 1c08767 | 2017-02-11 15:07:01 +0000 | [diff] [blame] | 4011 | if (Result > 0) |
| Daniel Berlin | b527b2c | 2017-05-19 19:01:27 +0000 | [diff] [blame] | 4012 | return 4 + NumFuncArgs + Result; |
| Daniel Berlin | 1c08767 | 2017-02-11 15:07:01 +0000 | [diff] [blame] | 4013 | // Unreachable or something else, just return a really large number. |
| 4014 | return ~0; |
| 4015 | } |
| 4016 | |
| 4017 | // This is a function that says whether two commutative operations should |
| 4018 | // have their order swapped when canonicalizing. |
| 4019 | bool NewGVN::shouldSwapOperands(const Value *A, const Value *B) const { |
| 4020 | // Because we only care about a total ordering, and don't rewrite expressions |
| 4021 | // in this order, we order by rank, which will give a strict weak ordering to |
| Daniel Berlin | b355c4f | 2017-02-18 23:06:47 +0000 | [diff] [blame] | 4022 | // everything but constants, and then we order by pointer address. |
| Daniel Berlin | f7d9580 | 2017-02-18 23:06:50 +0000 | [diff] [blame] | 4023 | return std::make_pair(getRank(A), A) > std::make_pair(getRank(B), B); |
| Daniel Berlin | 1c08767 | 2017-02-11 15:07:01 +0000 | [diff] [blame] | 4024 | } |
| Daniel Berlin | 64e6899 | 2017-03-12 04:46:45 +0000 | [diff] [blame] | 4025 | |
| Benjamin Kramer | debb3c3 | 2017-05-26 20:09:00 +0000 | [diff] [blame] | 4026 | namespace { |
| Daniel Berlin | 64e6899 | 2017-03-12 04:46:45 +0000 | [diff] [blame] | 4027 | class NewGVNLegacyPass : public FunctionPass { |
| 4028 | public: |
| 4029 | static char ID; // Pass identification, replacement for typeid. |
| 4030 | NewGVNLegacyPass() : FunctionPass(ID) { |
| 4031 | initializeNewGVNLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 4032 | } |
| 4033 | bool runOnFunction(Function &F) override; |
| 4034 | |
| 4035 | private: |
| 4036 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 4037 | AU.addRequired<AssumptionCacheTracker>(); |
| 4038 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 4039 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
| 4040 | AU.addRequired<MemorySSAWrapperPass>(); |
| 4041 | AU.addRequired<AAResultsWrapperPass>(); |
| 4042 | AU.addPreserved<DominatorTreeWrapperPass>(); |
| 4043 | AU.addPreserved<GlobalsAAWrapperPass>(); |
| 4044 | } |
| 4045 | }; |
| Benjamin Kramer | debb3c3 | 2017-05-26 20:09:00 +0000 | [diff] [blame] | 4046 | } // namespace |
| Daniel Berlin | 64e6899 | 2017-03-12 04:46:45 +0000 | [diff] [blame] | 4047 | |
| 4048 | bool NewGVNLegacyPass::runOnFunction(Function &F) { |
| 4049 | if (skipFunction(F)) |
| 4050 | return false; |
| 4051 | return NewGVN(F, &getAnalysis<DominatorTreeWrapperPass>().getDomTree(), |
| 4052 | &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F), |
| 4053 | &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(), |
| 4054 | &getAnalysis<AAResultsWrapperPass>().getAAResults(), |
| 4055 | &getAnalysis<MemorySSAWrapperPass>().getMSSA(), |
| 4056 | F.getParent()->getDataLayout()) |
| 4057 | .runGVN(); |
| 4058 | } |
| 4059 | |
| 4060 | INITIALIZE_PASS_BEGIN(NewGVNLegacyPass, "newgvn", "Global Value Numbering", |
| 4061 | false, false) |
| 4062 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
| 4063 | INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass) |
| 4064 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 4065 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 4066 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
| 4067 | INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass) |
| 4068 | INITIALIZE_PASS_END(NewGVNLegacyPass, "newgvn", "Global Value Numbering", false, |
| 4069 | false) |
| 4070 | |
| 4071 | char NewGVNLegacyPass::ID = 0; |
| 4072 | |
| 4073 | // createGVNPass - The public interface to this file. |
| 4074 | FunctionPass *llvm::createNewGVNPass() { return new NewGVNLegacyPass(); } |
| 4075 | |
| 4076 | PreservedAnalyses NewGVNPass::run(Function &F, AnalysisManager<Function> &AM) { |
| 4077 | // Apparently the order in which we get these results matter for |
| 4078 | // the old GVN (see Chandler's comment in GVN.cpp). I'll keep |
| 4079 | // the same order here, just in case. |
| 4080 | auto &AC = AM.getResult<AssumptionAnalysis>(F); |
| 4081 | auto &DT = AM.getResult<DominatorTreeAnalysis>(F); |
| 4082 | auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); |
| 4083 | auto &AA = AM.getResult<AAManager>(F); |
| 4084 | auto &MSSA = AM.getResult<MemorySSAAnalysis>(F).getMSSA(); |
| 4085 | bool Changed = |
| 4086 | NewGVN(F, &DT, &AC, &TLI, &AA, &MSSA, F.getParent()->getDataLayout()) |
| 4087 | .runGVN(); |
| 4088 | if (!Changed) |
| 4089 | return PreservedAnalyses::all(); |
| 4090 | PreservedAnalyses PA; |
| 4091 | PA.preserve<DominatorTreeAnalysis>(); |
| 4092 | PA.preserve<GlobalsAA>(); |
| 4093 | return PA; |
| 4094 | } |