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