1ae6f7882SJeremy Morse //===- InstrRefBasedImpl.cpp - Tracking Debug Value MIs -------------------===// 2ae6f7882SJeremy Morse // 3ae6f7882SJeremy Morse // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4ae6f7882SJeremy Morse // See https://llvm.org/LICENSE.txt for license information. 5ae6f7882SJeremy Morse // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6ae6f7882SJeremy Morse // 7ae6f7882SJeremy Morse //===----------------------------------------------------------------------===// 8ae6f7882SJeremy Morse /// \file InstrRefBasedImpl.cpp 9ae6f7882SJeremy Morse /// 10ae6f7882SJeremy Morse /// This is a separate implementation of LiveDebugValues, see 11ae6f7882SJeremy Morse /// LiveDebugValues.cpp and VarLocBasedImpl.cpp for more information. 12ae6f7882SJeremy Morse /// 13ae6f7882SJeremy Morse /// This pass propagates variable locations between basic blocks, resolving 14ae6f7882SJeremy Morse /// control flow conflicts between them. The problem is much like SSA 15ae6f7882SJeremy Morse /// construction, where each DBG_VALUE instruction assigns the *value* that 16ae6f7882SJeremy Morse /// a variable has, and every instruction where the variable is in scope uses 17ae6f7882SJeremy Morse /// that variable. The resulting map of instruction-to-value is then translated 18ae6f7882SJeremy Morse /// into a register (or spill) location for each variable over each instruction. 19ae6f7882SJeremy Morse /// 20ae6f7882SJeremy Morse /// This pass determines which DBG_VALUE dominates which instructions, or if 21ae6f7882SJeremy Morse /// none do, where values must be merged (like PHI nodes). The added 22ae6f7882SJeremy Morse /// complication is that because codegen has already finished, a PHI node may 23ae6f7882SJeremy Morse /// be needed for a variable location to be correct, but no register or spill 24ae6f7882SJeremy Morse /// slot merges the necessary values. In these circumstances, the variable 25ae6f7882SJeremy Morse /// location is dropped. 26ae6f7882SJeremy Morse /// 27ae6f7882SJeremy Morse /// What makes this analysis non-trivial is loops: we cannot tell in advance 28ae6f7882SJeremy Morse /// whether a variable location is live throughout a loop, or whether its 29ae6f7882SJeremy Morse /// location is clobbered (or redefined by another DBG_VALUE), without 30ae6f7882SJeremy Morse /// exploring all the way through. 31ae6f7882SJeremy Morse /// 32ae6f7882SJeremy Morse /// To make this simpler we perform two kinds of analysis. First, we identify 33ae6f7882SJeremy Morse /// every value defined by every instruction (ignoring those that only move 34ae6f7882SJeremy Morse /// another value), then compute a map of which values are available for each 35ae6f7882SJeremy Morse /// instruction. This is stronger than a reaching-def analysis, as we create 36ae6f7882SJeremy Morse /// PHI values where other values merge. 37ae6f7882SJeremy Morse /// 38ae6f7882SJeremy Morse /// Secondly, for each variable, we effectively re-construct SSA using each 39ae6f7882SJeremy Morse /// DBG_VALUE as a def. The DBG_VALUEs read a value-number computed by the 40ae6f7882SJeremy Morse /// first analysis from the location they refer to. We can then compute the 41ae6f7882SJeremy Morse /// dominance frontiers of where a variable has a value, and create PHI nodes 42ae6f7882SJeremy Morse /// where they merge. 43ae6f7882SJeremy Morse /// This isn't precisely SSA-construction though, because the function shape 44ae6f7882SJeremy Morse /// is pre-defined. If a variable location requires a PHI node, but no 45ae6f7882SJeremy Morse /// PHI for the relevant values is present in the function (as computed by the 46ae6f7882SJeremy Morse /// first analysis), the location must be dropped. 47ae6f7882SJeremy Morse /// 48ae6f7882SJeremy Morse /// Once both are complete, we can pass back over all instructions knowing: 49ae6f7882SJeremy Morse /// * What _value_ each variable should contain, either defined by an 50ae6f7882SJeremy Morse /// instruction or where control flow merges 51ae6f7882SJeremy Morse /// * What the location of that value is (if any). 52ae6f7882SJeremy Morse /// Allowing us to create appropriate live-in DBG_VALUEs, and DBG_VALUEs when 53ae6f7882SJeremy Morse /// a value moves location. After this pass runs, all variable locations within 54ae6f7882SJeremy Morse /// a block should be specified by DBG_VALUEs within that block, allowing 55ae6f7882SJeremy Morse /// DbgEntityHistoryCalculator to focus on individual blocks. 56ae6f7882SJeremy Morse /// 57ae6f7882SJeremy Morse /// This pass is able to go fast because the size of the first 58ae6f7882SJeremy Morse /// reaching-definition analysis is proportional to the working-set size of 59ae6f7882SJeremy Morse /// the function, which the compiler tries to keep small. (It's also 60ae6f7882SJeremy Morse /// proportional to the number of blocks). Additionally, we repeatedly perform 61ae6f7882SJeremy Morse /// the second reaching-definition analysis with only the variables and blocks 62ae6f7882SJeremy Morse /// in a single lexical scope, exploiting their locality. 63ae6f7882SJeremy Morse /// 64ae6f7882SJeremy Morse /// Determining where PHIs happen is trickier with this approach, and it comes 65ae6f7882SJeremy Morse /// to a head in the major problem for LiveDebugValues: is a value live-through 66ae6f7882SJeremy Morse /// a loop, or not? Your garden-variety dataflow analysis aims to build a set of 67ae6f7882SJeremy Morse /// facts about a function, however this analysis needs to generate new value 68ae6f7882SJeremy Morse /// numbers at joins. 69ae6f7882SJeremy Morse /// 70ae6f7882SJeremy Morse /// To do this, consider a lattice of all definition values, from instructions 71ae6f7882SJeremy Morse /// and from PHIs. Each PHI is characterised by the RPO number of the block it 72ae6f7882SJeremy Morse /// occurs in. Each value pair A, B can be ordered by RPO(A) < RPO(B): 73ae6f7882SJeremy Morse /// with non-PHI values at the top, and any PHI value in the last block (by RPO 74ae6f7882SJeremy Morse /// order) at the bottom. 75ae6f7882SJeremy Morse /// 76ae6f7882SJeremy Morse /// (Awkwardly: lower-down-the _lattice_ means a greater RPO _number_. Below, 77ae6f7882SJeremy Morse /// "rank" always refers to the former). 78ae6f7882SJeremy Morse /// 79ae6f7882SJeremy Morse /// At any join, for each register, we consider: 80ae6f7882SJeremy Morse /// * All incoming values, and 81ae6f7882SJeremy Morse /// * The PREVIOUS live-in value at this join. 82ae6f7882SJeremy Morse /// If all incoming values agree: that's the live-in value. If they do not, the 83ae6f7882SJeremy Morse /// incoming values are ranked according to the partial order, and the NEXT 84ae6f7882SJeremy Morse /// LOWEST rank after the PREVIOUS live-in value is picked (multiple values of 85ae6f7882SJeremy Morse /// the same rank are ignored as conflicting). If there are no candidate values, 86ae6f7882SJeremy Morse /// or if the rank of the live-in would be lower than the rank of the current 87ae6f7882SJeremy Morse /// blocks PHIs, create a new PHI value. 88ae6f7882SJeremy Morse /// 89ae6f7882SJeremy Morse /// Intuitively: if it's not immediately obvious what value a join should result 90ae6f7882SJeremy Morse /// in, we iteratively descend from instruction-definitions down through PHI 91ae6f7882SJeremy Morse /// values, getting closer to the current block each time. If the current block 92ae6f7882SJeremy Morse /// is a loop head, this ordering is effectively searching outer levels of 93ae6f7882SJeremy Morse /// loops, to find a value that's live-through the current loop. 94ae6f7882SJeremy Morse /// 95ae6f7882SJeremy Morse /// If there is no value that's live-through this loop, a PHI is created for 96ae6f7882SJeremy Morse /// this location instead. We can't use a lower-ranked PHI because by definition 97ae6f7882SJeremy Morse /// it doesn't dominate the current block. We can't create a PHI value any 98ae6f7882SJeremy Morse /// earlier, because we risk creating a PHI value at a location where values do 99ae6f7882SJeremy Morse /// not in fact merge, thus misrepresenting the truth, and not making the true 100ae6f7882SJeremy Morse /// live-through value for variable locations. 101ae6f7882SJeremy Morse /// 102ae6f7882SJeremy Morse /// This algorithm applies to both calculating the availability of values in 103ae6f7882SJeremy Morse /// the first analysis, and the location of variables in the second. However 104ae6f7882SJeremy Morse /// for the second we add an extra dimension of pain: creating a variable 105ae6f7882SJeremy Morse /// location PHI is only valid if, for each incoming edge, 106ae6f7882SJeremy Morse /// * There is a value for the variable on the incoming edge, and 107ae6f7882SJeremy Morse /// * All the edges have that value in the same register. 108ae6f7882SJeremy Morse /// Or put another way: we can only create a variable-location PHI if there is 109ae6f7882SJeremy Morse /// a matching machine-location PHI, each input to which is the variables value 110ae6f7882SJeremy Morse /// in the predecessor block. 111ae6f7882SJeremy Morse /// 112a64b2c93SDjordje Todorovic /// To accommodate this difference, each point on the lattice is split in 113ae6f7882SJeremy Morse /// two: a "proposed" PHI and "definite" PHI. Any PHI that can immediately 114ae6f7882SJeremy Morse /// have a location determined are "definite" PHIs, and no further work is 115ae6f7882SJeremy Morse /// needed. Otherwise, a location that all non-backedge predecessors agree 116ae6f7882SJeremy Morse /// on is picked and propagated as a "proposed" PHI value. If that PHI value 117ae6f7882SJeremy Morse /// is truly live-through, it'll appear on the loop backedges on the next 118ae6f7882SJeremy Morse /// dataflow iteration, after which the block live-in moves to be a "definite" 119ae6f7882SJeremy Morse /// PHI. If it's not truly live-through, the variable value will be downgraded 120ae6f7882SJeremy Morse /// further as we explore the lattice, or remains "proposed" and is considered 121ae6f7882SJeremy Morse /// invalid once dataflow completes. 122ae6f7882SJeremy Morse /// 123ae6f7882SJeremy Morse /// ### Terminology 124ae6f7882SJeremy Morse /// 125ae6f7882SJeremy Morse /// A machine location is a register or spill slot, a value is something that's 126ae6f7882SJeremy Morse /// defined by an instruction or PHI node, while a variable value is the value 127ae6f7882SJeremy Morse /// assigned to a variable. A variable location is a machine location, that must 128ae6f7882SJeremy Morse /// contain the appropriate variable value. A value that is a PHI node is 129ae6f7882SJeremy Morse /// occasionally called an mphi. 130ae6f7882SJeremy Morse /// 131ae6f7882SJeremy Morse /// The first dataflow problem is the "machine value location" problem, 132ae6f7882SJeremy Morse /// because we're determining which machine locations contain which values. 133ae6f7882SJeremy Morse /// The "locations" are constant: what's unknown is what value they contain. 134ae6f7882SJeremy Morse /// 135ae6f7882SJeremy Morse /// The second dataflow problem (the one for variables) is the "variable value 136ae6f7882SJeremy Morse /// problem", because it's determining what values a variable has, rather than 137ae6f7882SJeremy Morse /// what location those values are placed in. Unfortunately, it's not that 138ae6f7882SJeremy Morse /// simple, because producing a PHI value always involves picking a location. 139ae6f7882SJeremy Morse /// This is an imperfection that we just have to accept, at least for now. 140ae6f7882SJeremy Morse /// 141ae6f7882SJeremy Morse /// TODO: 142ae6f7882SJeremy Morse /// Overlapping fragments 143ae6f7882SJeremy Morse /// Entry values 144ae6f7882SJeremy Morse /// Add back DEBUG statements for debugging this 145ae6f7882SJeremy Morse /// Collect statistics 146ae6f7882SJeremy Morse /// 147ae6f7882SJeremy Morse //===----------------------------------------------------------------------===// 148ae6f7882SJeremy Morse 149ae6f7882SJeremy Morse #include "llvm/ADT/DenseMap.h" 150ae6f7882SJeremy Morse #include "llvm/ADT/PostOrderIterator.h" 151ae6f7882SJeremy Morse #include "llvm/ADT/SmallPtrSet.h" 152ae6f7882SJeremy Morse #include "llvm/ADT/SmallSet.h" 153ae6f7882SJeremy Morse #include "llvm/ADT/SmallVector.h" 154ae6f7882SJeremy Morse #include "llvm/ADT/Statistic.h" 155ae6f7882SJeremy Morse #include "llvm/ADT/UniqueVector.h" 156ae6f7882SJeremy Morse #include "llvm/CodeGen/LexicalScopes.h" 157ae6f7882SJeremy Morse #include "llvm/CodeGen/MachineBasicBlock.h" 158ae6f7882SJeremy Morse #include "llvm/CodeGen/MachineFrameInfo.h" 159ae6f7882SJeremy Morse #include "llvm/CodeGen/MachineFunction.h" 160ae6f7882SJeremy Morse #include "llvm/CodeGen/MachineFunctionPass.h" 161ae6f7882SJeremy Morse #include "llvm/CodeGen/MachineInstr.h" 162ae6f7882SJeremy Morse #include "llvm/CodeGen/MachineInstrBuilder.h" 163ae6f7882SJeremy Morse #include "llvm/CodeGen/MachineMemOperand.h" 164ae6f7882SJeremy Morse #include "llvm/CodeGen/MachineOperand.h" 165ae6f7882SJeremy Morse #include "llvm/CodeGen/PseudoSourceValue.h" 166ae6f7882SJeremy Morse #include "llvm/CodeGen/RegisterScavenging.h" 167ae6f7882SJeremy Morse #include "llvm/CodeGen/TargetFrameLowering.h" 168ae6f7882SJeremy Morse #include "llvm/CodeGen/TargetInstrInfo.h" 169ae6f7882SJeremy Morse #include "llvm/CodeGen/TargetLowering.h" 170ae6f7882SJeremy Morse #include "llvm/CodeGen/TargetPassConfig.h" 171ae6f7882SJeremy Morse #include "llvm/CodeGen/TargetRegisterInfo.h" 172ae6f7882SJeremy Morse #include "llvm/CodeGen/TargetSubtargetInfo.h" 173ae6f7882SJeremy Morse #include "llvm/Config/llvm-config.h" 174ae6f7882SJeremy Morse #include "llvm/IR/DIBuilder.h" 175ae6f7882SJeremy Morse #include "llvm/IR/DebugInfoMetadata.h" 176ae6f7882SJeremy Morse #include "llvm/IR/DebugLoc.h" 177ae6f7882SJeremy Morse #include "llvm/IR/Function.h" 178ae6f7882SJeremy Morse #include "llvm/IR/Module.h" 179ae6f7882SJeremy Morse #include "llvm/InitializePasses.h" 180ae6f7882SJeremy Morse #include "llvm/MC/MCRegisterInfo.h" 181ae6f7882SJeremy Morse #include "llvm/Pass.h" 182ae6f7882SJeremy Morse #include "llvm/Support/Casting.h" 183ae6f7882SJeremy Morse #include "llvm/Support/Compiler.h" 184ae6f7882SJeremy Morse #include "llvm/Support/Debug.h" 185ae6f7882SJeremy Morse #include "llvm/Support/raw_ostream.h" 186ae6f7882SJeremy Morse #include <algorithm> 187ae6f7882SJeremy Morse #include <cassert> 188ae6f7882SJeremy Morse #include <cstdint> 189ae6f7882SJeremy Morse #include <functional> 190ae6f7882SJeremy Morse #include <queue> 191ae6f7882SJeremy Morse #include <tuple> 192ae6f7882SJeremy Morse #include <utility> 193ae6f7882SJeremy Morse #include <vector> 194ae6f7882SJeremy Morse #include <limits.h> 195ae6f7882SJeremy Morse #include <limits> 196ae6f7882SJeremy Morse 197ae6f7882SJeremy Morse #include "LiveDebugValues.h" 198ae6f7882SJeremy Morse 199ae6f7882SJeremy Morse using namespace llvm; 200ae6f7882SJeremy Morse 201ae6f7882SJeremy Morse #define DEBUG_TYPE "livedebugvalues" 202ae6f7882SJeremy Morse 203ae6f7882SJeremy Morse STATISTIC(NumInserted, "Number of DBG_VALUE instructions inserted"); 204ae6f7882SJeremy Morse STATISTIC(NumRemoved, "Number of DBG_VALUE instructions removed"); 205ae6f7882SJeremy Morse 206ae6f7882SJeremy Morse // Act more like the VarLoc implementation, by propagating some locations too 207ae6f7882SJeremy Morse // far and ignoring some transfers. 208ae6f7882SJeremy Morse static cl::opt<bool> EmulateOldLDV("emulate-old-livedebugvalues", cl::Hidden, 209ae6f7882SJeremy Morse cl::desc("Act like old LiveDebugValues did"), 210ae6f7882SJeremy Morse cl::init(false)); 211ae6f7882SJeremy Morse 212ae6f7882SJeremy Morse // Rely on isStoreToStackSlotPostFE and similar to observe all stack spills. 213ae6f7882SJeremy Morse static cl::opt<bool> 214ae6f7882SJeremy Morse ObserveAllStackops("observe-all-stack-ops", cl::Hidden, 215ae6f7882SJeremy Morse cl::desc("Allow non-kill spill and restores"), 216ae6f7882SJeremy Morse cl::init(false)); 217ae6f7882SJeremy Morse 218ae6f7882SJeremy Morse namespace { 219ae6f7882SJeremy Morse 220ae6f7882SJeremy Morse // The location at which a spilled value resides. It consists of a register and 221ae6f7882SJeremy Morse // an offset. 222ae6f7882SJeremy Morse struct SpillLoc { 223ae6f7882SJeremy Morse unsigned SpillBase; 224ae6f7882SJeremy Morse int SpillOffset; 225ae6f7882SJeremy Morse bool operator==(const SpillLoc &Other) const { 226ae6f7882SJeremy Morse return std::tie(SpillBase, SpillOffset) == 227ae6f7882SJeremy Morse std::tie(Other.SpillBase, Other.SpillOffset); 228ae6f7882SJeremy Morse } 229ae6f7882SJeremy Morse bool operator<(const SpillLoc &Other) const { 230ae6f7882SJeremy Morse return std::tie(SpillBase, SpillOffset) < 231ae6f7882SJeremy Morse std::tie(Other.SpillBase, Other.SpillOffset); 232ae6f7882SJeremy Morse } 233ae6f7882SJeremy Morse }; 234ae6f7882SJeremy Morse 235ae6f7882SJeremy Morse class LocIdx { 236ae6f7882SJeremy Morse unsigned Location; 237ae6f7882SJeremy Morse 238ae6f7882SJeremy Morse // Default constructor is private, initializing to an illegal location number. 239ae6f7882SJeremy Morse // Use only for "not an entry" elements in IndexedMaps. 240ae6f7882SJeremy Morse LocIdx() : Location(UINT_MAX) { } 241ae6f7882SJeremy Morse 242ae6f7882SJeremy Morse public: 243ae6f7882SJeremy Morse #define NUM_LOC_BITS 24 244ae6f7882SJeremy Morse LocIdx(unsigned L) : Location(L) { 245ae6f7882SJeremy Morse assert(L < (1 << NUM_LOC_BITS) && "Machine locations must fit in 24 bits"); 246ae6f7882SJeremy Morse } 247ae6f7882SJeremy Morse 248ae6f7882SJeremy Morse static LocIdx MakeIllegalLoc() { 249ae6f7882SJeremy Morse return LocIdx(); 250ae6f7882SJeremy Morse } 251ae6f7882SJeremy Morse 252ae6f7882SJeremy Morse bool isIllegal() const { 253ae6f7882SJeremy Morse return Location == UINT_MAX; 254ae6f7882SJeremy Morse } 255ae6f7882SJeremy Morse 256ae6f7882SJeremy Morse uint64_t asU64() const { 257ae6f7882SJeremy Morse return Location; 258ae6f7882SJeremy Morse } 259ae6f7882SJeremy Morse 260ae6f7882SJeremy Morse bool operator==(unsigned L) const { 261ae6f7882SJeremy Morse return Location == L; 262ae6f7882SJeremy Morse } 263ae6f7882SJeremy Morse 264ae6f7882SJeremy Morse bool operator==(const LocIdx &L) const { 265ae6f7882SJeremy Morse return Location == L.Location; 266ae6f7882SJeremy Morse } 267ae6f7882SJeremy Morse 268ae6f7882SJeremy Morse bool operator!=(unsigned L) const { 269ae6f7882SJeremy Morse return !(*this == L); 270ae6f7882SJeremy Morse } 271ae6f7882SJeremy Morse 272ae6f7882SJeremy Morse bool operator!=(const LocIdx &L) const { 273ae6f7882SJeremy Morse return !(*this == L); 274ae6f7882SJeremy Morse } 275ae6f7882SJeremy Morse 276ae6f7882SJeremy Morse bool operator<(const LocIdx &Other) const { 277ae6f7882SJeremy Morse return Location < Other.Location; 278ae6f7882SJeremy Morse } 279ae6f7882SJeremy Morse }; 280ae6f7882SJeremy Morse 281ae6f7882SJeremy Morse class LocIdxToIndexFunctor { 282ae6f7882SJeremy Morse public: 283ae6f7882SJeremy Morse using argument_type = LocIdx; 284ae6f7882SJeremy Morse unsigned operator()(const LocIdx &L) const { 285ae6f7882SJeremy Morse return L.asU64(); 286ae6f7882SJeremy Morse } 287ae6f7882SJeremy Morse }; 288ae6f7882SJeremy Morse 289ae6f7882SJeremy Morse /// Unique identifier for a value defined by an instruction, as a value type. 290ae6f7882SJeremy Morse /// Casts back and forth to a uint64_t. Probably replacable with something less 291ae6f7882SJeremy Morse /// bit-constrained. Each value identifies the instruction and machine location 292ae6f7882SJeremy Morse /// where the value is defined, although there may be no corresponding machine 293ae6f7882SJeremy Morse /// operand for it (ex: regmasks clobbering values). The instructions are 294ae6f7882SJeremy Morse /// one-based, and definitions that are PHIs have instruction number zero. 295ae6f7882SJeremy Morse /// 296ae6f7882SJeremy Morse /// The obvious limits of a 1M block function or 1M instruction blocks are 297ae6f7882SJeremy Morse /// problematic; but by that point we should probably have bailed out of 298ae6f7882SJeremy Morse /// trying to analyse the function. 299ae6f7882SJeremy Morse class ValueIDNum { 300ae6f7882SJeremy Morse uint64_t BlockNo : 20; /// The block where the def happens. 301ae6f7882SJeremy Morse uint64_t InstNo : 20; /// The Instruction where the def happens. 302ae6f7882SJeremy Morse /// One based, is distance from start of block. 303ae6f7882SJeremy Morse uint64_t LocNo : NUM_LOC_BITS; /// The machine location where the def happens. 304ae6f7882SJeremy Morse 305ae6f7882SJeremy Morse public: 306ae6f7882SJeremy Morse // XXX -- temporarily enabled while the live-in / live-out tables are moved 307ae6f7882SJeremy Morse // to something more type-y 308ae6f7882SJeremy Morse ValueIDNum() : BlockNo(0xFFFFF), 309ae6f7882SJeremy Morse InstNo(0xFFFFF), 310ae6f7882SJeremy Morse LocNo(0xFFFFFF) { } 311ae6f7882SJeremy Morse 312ae6f7882SJeremy Morse ValueIDNum(uint64_t Block, uint64_t Inst, uint64_t Loc) 313ae6f7882SJeremy Morse : BlockNo(Block), InstNo(Inst), LocNo(Loc) { } 314ae6f7882SJeremy Morse 315ae6f7882SJeremy Morse ValueIDNum(uint64_t Block, uint64_t Inst, LocIdx Loc) 316ae6f7882SJeremy Morse : BlockNo(Block), InstNo(Inst), LocNo(Loc.asU64()) { } 317ae6f7882SJeremy Morse 318ae6f7882SJeremy Morse uint64_t getBlock() const { return BlockNo; } 319ae6f7882SJeremy Morse uint64_t getInst() const { return InstNo; } 320ae6f7882SJeremy Morse uint64_t getLoc() const { return LocNo; } 321ae6f7882SJeremy Morse bool isPHI() const { return InstNo == 0; } 322ae6f7882SJeremy Morse 323ae6f7882SJeremy Morse uint64_t asU64() const { 324ae6f7882SJeremy Morse uint64_t TmpBlock = BlockNo; 325ae6f7882SJeremy Morse uint64_t TmpInst = InstNo; 326ae6f7882SJeremy Morse return TmpBlock << 44ull | TmpInst << NUM_LOC_BITS | LocNo; 327ae6f7882SJeremy Morse } 328ae6f7882SJeremy Morse 329ae6f7882SJeremy Morse static ValueIDNum fromU64(uint64_t v) { 330ae6f7882SJeremy Morse uint64_t L = (v & 0x3FFF); 331ae6f7882SJeremy Morse return {v >> 44ull, ((v >> NUM_LOC_BITS) & 0xFFFFF), L}; 332ae6f7882SJeremy Morse } 333ae6f7882SJeremy Morse 334ae6f7882SJeremy Morse bool operator<(const ValueIDNum &Other) const { 335ae6f7882SJeremy Morse return asU64() < Other.asU64(); 336ae6f7882SJeremy Morse } 337ae6f7882SJeremy Morse 338ae6f7882SJeremy Morse bool operator==(const ValueIDNum &Other) const { 339ae6f7882SJeremy Morse return std::tie(BlockNo, InstNo, LocNo) == 340ae6f7882SJeremy Morse std::tie(Other.BlockNo, Other.InstNo, Other.LocNo); 341ae6f7882SJeremy Morse } 342ae6f7882SJeremy Morse 343ae6f7882SJeremy Morse bool operator!=(const ValueIDNum &Other) const { return !(*this == Other); } 344ae6f7882SJeremy Morse 345ae6f7882SJeremy Morse std::string asString(const std::string &mlocname) const { 34663843785SDjordje Todorovic return Twine("Value{bb: ") 34763843785SDjordje Todorovic .concat(Twine(BlockNo).concat( 34863843785SDjordje Todorovic Twine(", inst: ") 34963843785SDjordje Todorovic .concat((InstNo ? Twine(InstNo) : Twine("live-in")) 35063843785SDjordje Todorovic .concat(Twine(", loc: ").concat(Twine(mlocname))) 35163843785SDjordje Todorovic .concat(Twine("}"))))) 352ae6f7882SJeremy Morse .str(); 353ae6f7882SJeremy Morse } 354ae6f7882SJeremy Morse 355ae6f7882SJeremy Morse static ValueIDNum EmptyValue; 356ae6f7882SJeremy Morse }; 357ae6f7882SJeremy Morse 358ae6f7882SJeremy Morse } // end anonymous namespace 359ae6f7882SJeremy Morse 360ae6f7882SJeremy Morse namespace { 361ae6f7882SJeremy Morse 362ae6f7882SJeremy Morse /// Meta qualifiers for a value. Pair of whatever expression is used to qualify 363ae6f7882SJeremy Morse /// the the value, and Boolean of whether or not it's indirect. 364ae6f7882SJeremy Morse class DbgValueProperties { 365ae6f7882SJeremy Morse public: 366ae6f7882SJeremy Morse DbgValueProperties(const DIExpression *DIExpr, bool Indirect) 367ae6f7882SJeremy Morse : DIExpr(DIExpr), Indirect(Indirect) {} 368ae6f7882SJeremy Morse 369ae6f7882SJeremy Morse /// Extract properties from an existing DBG_VALUE instruction. 370ae6f7882SJeremy Morse DbgValueProperties(const MachineInstr &MI) { 371ae6f7882SJeremy Morse assert(MI.isDebugValue()); 372ae6f7882SJeremy Morse DIExpr = MI.getDebugExpression(); 373ae6f7882SJeremy Morse Indirect = MI.getOperand(1).isImm(); 374ae6f7882SJeremy Morse } 375ae6f7882SJeremy Morse 376ae6f7882SJeremy Morse bool operator==(const DbgValueProperties &Other) const { 377ae6f7882SJeremy Morse return std::tie(DIExpr, Indirect) == std::tie(Other.DIExpr, Other.Indirect); 378ae6f7882SJeremy Morse } 379ae6f7882SJeremy Morse 380ae6f7882SJeremy Morse bool operator!=(const DbgValueProperties &Other) const { 381ae6f7882SJeremy Morse return !(*this == Other); 382ae6f7882SJeremy Morse } 383ae6f7882SJeremy Morse 384ae6f7882SJeremy Morse const DIExpression *DIExpr; 385ae6f7882SJeremy Morse bool Indirect; 386ae6f7882SJeremy Morse }; 387ae6f7882SJeremy Morse 388ae6f7882SJeremy Morse /// Tracker for what values are in machine locations. Listens to the Things 389ae6f7882SJeremy Morse /// being Done by various instructions, and maintains a table of what machine 390ae6f7882SJeremy Morse /// locations have what values (as defined by a ValueIDNum). 391ae6f7882SJeremy Morse /// 392ae6f7882SJeremy Morse /// There are potentially a much larger number of machine locations on the 393ae6f7882SJeremy Morse /// target machine than the actual working-set size of the function. On x86 for 394ae6f7882SJeremy Morse /// example, we're extremely unlikely to want to track values through control 395ae6f7882SJeremy Morse /// or debug registers. To avoid doing so, MLocTracker has several layers of 396ae6f7882SJeremy Morse /// indirection going on, with two kinds of ``location'': 397ae6f7882SJeremy Morse /// * A LocID uniquely identifies a register or spill location, with a 398ae6f7882SJeremy Morse /// predictable value. 399ae6f7882SJeremy Morse /// * A LocIdx is a key (in the database sense) for a LocID and a ValueIDNum. 400ae6f7882SJeremy Morse /// Whenever a location is def'd or used by a MachineInstr, we automagically 401ae6f7882SJeremy Morse /// create a new LocIdx for a location, but not otherwise. This ensures we only 402ae6f7882SJeremy Morse /// account for locations that are actually used or defined. The cost is another 403ae6f7882SJeremy Morse /// vector lookup (of LocID -> LocIdx) over any other implementation. This is 404ae6f7882SJeremy Morse /// fairly cheap, and the compiler tries to reduce the working-set at any one 405ae6f7882SJeremy Morse /// time in the function anyway. 406ae6f7882SJeremy Morse /// 407ae6f7882SJeremy Morse /// Register mask operands completely blow this out of the water; I've just 408ae6f7882SJeremy Morse /// piled hacks on top of hacks to get around that. 409ae6f7882SJeremy Morse class MLocTracker { 410ae6f7882SJeremy Morse public: 411ae6f7882SJeremy Morse MachineFunction &MF; 412ae6f7882SJeremy Morse const TargetInstrInfo &TII; 413ae6f7882SJeremy Morse const TargetRegisterInfo &TRI; 414ae6f7882SJeremy Morse const TargetLowering &TLI; 415ae6f7882SJeremy Morse 416ae6f7882SJeremy Morse /// IndexedMap type, mapping from LocIdx to ValueIDNum. 417cca049adSDjordje Todorovic using LocToValueType = IndexedMap<ValueIDNum, LocIdxToIndexFunctor>; 418ae6f7882SJeremy Morse 419ae6f7882SJeremy Morse /// Map of LocIdxes to the ValueIDNums that they store. This is tightly 420ae6f7882SJeremy Morse /// packed, entries only exist for locations that are being tracked. 421ae6f7882SJeremy Morse LocToValueType LocIdxToIDNum; 422ae6f7882SJeremy Morse 423ae6f7882SJeremy Morse /// "Map" of machine location IDs (i.e., raw register or spill number) to the 424ae6f7882SJeremy Morse /// LocIdx key / number for that location. There are always at least as many 425ae6f7882SJeremy Morse /// as the number of registers on the target -- if the value in the register 426ae6f7882SJeremy Morse /// is not being tracked, then the LocIdx value will be zero. New entries are 427ae6f7882SJeremy Morse /// appended if a new spill slot begins being tracked. 428ae6f7882SJeremy Morse /// This, and the corresponding reverse map persist for the analysis of the 429ae6f7882SJeremy Morse /// whole function, and is necessarying for decoding various vectors of 430ae6f7882SJeremy Morse /// values. 431ae6f7882SJeremy Morse std::vector<LocIdx> LocIDToLocIdx; 432ae6f7882SJeremy Morse 433ae6f7882SJeremy Morse /// Inverse map of LocIDToLocIdx. 434ae6f7882SJeremy Morse IndexedMap<unsigned, LocIdxToIndexFunctor> LocIdxToLocID; 435ae6f7882SJeremy Morse 436ae6f7882SJeremy Morse /// Unique-ification of spill slots. Used to number them -- their LocID 437ae6f7882SJeremy Morse /// number is the index in SpillLocs minus one plus NumRegs. 438ae6f7882SJeremy Morse UniqueVector<SpillLoc> SpillLocs; 439ae6f7882SJeremy Morse 440ae6f7882SJeremy Morse // If we discover a new machine location, assign it an mphi with this 441ae6f7882SJeremy Morse // block number. 442ae6f7882SJeremy Morse unsigned CurBB; 443ae6f7882SJeremy Morse 444ae6f7882SJeremy Morse /// Cached local copy of the number of registers the target has. 445ae6f7882SJeremy Morse unsigned NumRegs; 446ae6f7882SJeremy Morse 447ae6f7882SJeremy Morse /// Collection of register mask operands that have been observed. Second part 448ae6f7882SJeremy Morse /// of pair indicates the instruction that they happened in. Used to 449ae6f7882SJeremy Morse /// reconstruct where defs happened if we start tracking a location later 450ae6f7882SJeremy Morse /// on. 451ae6f7882SJeremy Morse SmallVector<std::pair<const MachineOperand *, unsigned>, 32> Masks; 452ae6f7882SJeremy Morse 453ae6f7882SJeremy Morse /// Iterator for locations and the values they contain. Dereferencing 454ae6f7882SJeremy Morse /// produces a struct/pair containing the LocIdx key for this location, 455ae6f7882SJeremy Morse /// and a reference to the value currently stored. Simplifies the process 456ae6f7882SJeremy Morse /// of seeking a particular location. 457ae6f7882SJeremy Morse class MLocIterator { 458ae6f7882SJeremy Morse LocToValueType &ValueMap; 459ae6f7882SJeremy Morse LocIdx Idx; 460ae6f7882SJeremy Morse 461ae6f7882SJeremy Morse public: 462ae6f7882SJeremy Morse class value_type { 463ae6f7882SJeremy Morse public: 464ae6f7882SJeremy Morse value_type(LocIdx Idx, ValueIDNum &Value) : Idx(Idx), Value(Value) { } 465ae6f7882SJeremy Morse const LocIdx Idx; /// Read-only index of this location. 466ae6f7882SJeremy Morse ValueIDNum &Value; /// Reference to the stored value at this location. 467ae6f7882SJeremy Morse }; 468ae6f7882SJeremy Morse 469ae6f7882SJeremy Morse MLocIterator(LocToValueType &ValueMap, LocIdx Idx) 470ae6f7882SJeremy Morse : ValueMap(ValueMap), Idx(Idx) { } 471ae6f7882SJeremy Morse 472ae6f7882SJeremy Morse bool operator==(const MLocIterator &Other) const { 473ae6f7882SJeremy Morse assert(&ValueMap == &Other.ValueMap); 474ae6f7882SJeremy Morse return Idx == Other.Idx; 475ae6f7882SJeremy Morse } 476ae6f7882SJeremy Morse 477ae6f7882SJeremy Morse bool operator!=(const MLocIterator &Other) const { 478ae6f7882SJeremy Morse return !(*this == Other); 479ae6f7882SJeremy Morse } 480ae6f7882SJeremy Morse 481ae6f7882SJeremy Morse void operator++() { 482ae6f7882SJeremy Morse Idx = LocIdx(Idx.asU64() + 1); 483ae6f7882SJeremy Morse } 484ae6f7882SJeremy Morse 485ae6f7882SJeremy Morse value_type operator*() { 486ae6f7882SJeremy Morse return value_type(Idx, ValueMap[LocIdx(Idx)]); 487ae6f7882SJeremy Morse } 488ae6f7882SJeremy Morse }; 489ae6f7882SJeremy Morse 490ae6f7882SJeremy Morse MLocTracker(MachineFunction &MF, const TargetInstrInfo &TII, 491ae6f7882SJeremy Morse const TargetRegisterInfo &TRI, const TargetLowering &TLI) 492ae6f7882SJeremy Morse : MF(MF), TII(TII), TRI(TRI), TLI(TLI), 493ae6f7882SJeremy Morse LocIdxToIDNum(ValueIDNum::EmptyValue), 494ae6f7882SJeremy Morse LocIdxToLocID(0) { 495ae6f7882SJeremy Morse NumRegs = TRI.getNumRegs(); 496ae6f7882SJeremy Morse reset(); 497ae6f7882SJeremy Morse LocIDToLocIdx.resize(NumRegs, LocIdx::MakeIllegalLoc()); 498ae6f7882SJeremy Morse assert(NumRegs < (1u << NUM_LOC_BITS)); // Detect bit packing failure 499ae6f7882SJeremy Morse 500ae6f7882SJeremy Morse // Always track SP. This avoids the implicit clobbering caused by regmasks 501ae6f7882SJeremy Morse // from affectings its values. (LiveDebugValues disbelieves calls and 502ae6f7882SJeremy Morse // regmasks that claim to clobber SP). 5034634ad6cSGaurav Jain Register SP = TLI.getStackPointerRegisterToSaveRestore(); 504ae6f7882SJeremy Morse if (SP) { 505ae6f7882SJeremy Morse unsigned ID = getLocID(SP, false); 506ae6f7882SJeremy Morse (void)lookupOrTrackRegister(ID); 507ae6f7882SJeremy Morse } 508ae6f7882SJeremy Morse } 509ae6f7882SJeremy Morse 510ae6f7882SJeremy Morse /// Produce location ID number for indexing LocIDToLocIdx. Takes the register 511ae6f7882SJeremy Morse /// or spill number, and flag for whether it's a spill or not. 5124634ad6cSGaurav Jain unsigned getLocID(Register RegOrSpill, bool isSpill) { 5134634ad6cSGaurav Jain return (isSpill) ? RegOrSpill.id() + NumRegs - 1 : RegOrSpill.id(); 514ae6f7882SJeremy Morse } 515ae6f7882SJeremy Morse 516ae6f7882SJeremy Morse /// Accessor for reading the value at Idx. 517ae6f7882SJeremy Morse ValueIDNum getNumAtPos(LocIdx Idx) const { 518ae6f7882SJeremy Morse assert(Idx.asU64() < LocIdxToIDNum.size()); 519ae6f7882SJeremy Morse return LocIdxToIDNum[Idx]; 520ae6f7882SJeremy Morse } 521ae6f7882SJeremy Morse 522ae6f7882SJeremy Morse unsigned getNumLocs(void) const { return LocIdxToIDNum.size(); } 523ae6f7882SJeremy Morse 524ae6f7882SJeremy Morse /// Reset all locations to contain a PHI value at the designated block. Used 525ae6f7882SJeremy Morse /// sometimes for actual PHI values, othertimes to indicate the block entry 526ae6f7882SJeremy Morse /// value (before any more information is known). 527ae6f7882SJeremy Morse void setMPhis(unsigned NewCurBB) { 528ae6f7882SJeremy Morse CurBB = NewCurBB; 529ae6f7882SJeremy Morse for (auto Location : locations()) 530ae6f7882SJeremy Morse Location.Value = {CurBB, 0, Location.Idx}; 531ae6f7882SJeremy Morse } 532ae6f7882SJeremy Morse 533ae6f7882SJeremy Morse /// Load values for each location from array of ValueIDNums. Take current 534ae6f7882SJeremy Morse /// bbnum just in case we read a value from a hitherto untouched register. 535ae6f7882SJeremy Morse void loadFromArray(ValueIDNum *Locs, unsigned NewCurBB) { 536ae6f7882SJeremy Morse CurBB = NewCurBB; 537ae6f7882SJeremy Morse // Iterate over all tracked locations, and load each locations live-in 538ae6f7882SJeremy Morse // value into our local index. 539ae6f7882SJeremy Morse for (auto Location : locations()) 540ae6f7882SJeremy Morse Location.Value = Locs[Location.Idx.asU64()]; 541ae6f7882SJeremy Morse } 542ae6f7882SJeremy Morse 543ae6f7882SJeremy Morse /// Wipe any un-necessary location records after traversing a block. 544ae6f7882SJeremy Morse void reset(void) { 545ae6f7882SJeremy Morse // We could reset all the location values too; however either loadFromArray 546ae6f7882SJeremy Morse // or setMPhis should be called before this object is re-used. Just 547ae6f7882SJeremy Morse // clear Masks, they're definitely not needed. 548ae6f7882SJeremy Morse Masks.clear(); 549ae6f7882SJeremy Morse } 550ae6f7882SJeremy Morse 551ae6f7882SJeremy Morse /// Clear all data. Destroys the LocID <=> LocIdx map, which makes most of 552ae6f7882SJeremy Morse /// the information in this pass uninterpretable. 553ae6f7882SJeremy Morse void clear(void) { 554ae6f7882SJeremy Morse reset(); 555ae6f7882SJeremy Morse LocIDToLocIdx.clear(); 556ae6f7882SJeremy Morse LocIdxToLocID.clear(); 557ae6f7882SJeremy Morse LocIdxToIDNum.clear(); 558ae6f7882SJeremy Morse //SpillLocs.reset(); XXX UniqueVector::reset assumes a SpillLoc casts from 0 559ae6f7882SJeremy Morse SpillLocs = decltype(SpillLocs)(); 560ae6f7882SJeremy Morse 561ae6f7882SJeremy Morse LocIDToLocIdx.resize(NumRegs, LocIdx::MakeIllegalLoc()); 562ae6f7882SJeremy Morse } 563ae6f7882SJeremy Morse 564ae6f7882SJeremy Morse /// Set a locaiton to a certain value. 565ae6f7882SJeremy Morse void setMLoc(LocIdx L, ValueIDNum Num) { 566ae6f7882SJeremy Morse assert(L.asU64() < LocIdxToIDNum.size()); 567ae6f7882SJeremy Morse LocIdxToIDNum[L] = Num; 568ae6f7882SJeremy Morse } 569ae6f7882SJeremy Morse 570ae6f7882SJeremy Morse /// Create a LocIdx for an untracked register ID. Initialize it to either an 571ae6f7882SJeremy Morse /// mphi value representing a live-in, or a recent register mask clobber. 572ae6f7882SJeremy Morse LocIdx trackRegister(unsigned ID) { 573ae6f7882SJeremy Morse assert(ID != 0); 574ae6f7882SJeremy Morse LocIdx NewIdx = LocIdx(LocIdxToIDNum.size()); 575ae6f7882SJeremy Morse LocIdxToIDNum.grow(NewIdx); 576ae6f7882SJeremy Morse LocIdxToLocID.grow(NewIdx); 577ae6f7882SJeremy Morse 578ae6f7882SJeremy Morse // Default: it's an mphi. 579ae6f7882SJeremy Morse ValueIDNum ValNum = {CurBB, 0, NewIdx}; 580ae6f7882SJeremy Morse // Was this reg ever touched by a regmask? 581ae6f7882SJeremy Morse for (const auto &MaskPair : reverse(Masks)) { 582ae6f7882SJeremy Morse if (MaskPair.first->clobbersPhysReg(ID)) { 583ae6f7882SJeremy Morse // There was an earlier def we skipped. 584ae6f7882SJeremy Morse ValNum = {CurBB, MaskPair.second, NewIdx}; 585ae6f7882SJeremy Morse break; 586ae6f7882SJeremy Morse } 587ae6f7882SJeremy Morse } 588ae6f7882SJeremy Morse 589ae6f7882SJeremy Morse LocIdxToIDNum[NewIdx] = ValNum; 590ae6f7882SJeremy Morse LocIdxToLocID[NewIdx] = ID; 591ae6f7882SJeremy Morse return NewIdx; 592ae6f7882SJeremy Morse } 593ae6f7882SJeremy Morse 594ae6f7882SJeremy Morse LocIdx lookupOrTrackRegister(unsigned ID) { 595ae6f7882SJeremy Morse LocIdx &Index = LocIDToLocIdx[ID]; 596ae6f7882SJeremy Morse if (Index.isIllegal()) 597ae6f7882SJeremy Morse Index = trackRegister(ID); 598ae6f7882SJeremy Morse return Index; 599ae6f7882SJeremy Morse } 600ae6f7882SJeremy Morse 601ae6f7882SJeremy Morse /// Record a definition of the specified register at the given block / inst. 602ae6f7882SJeremy Morse /// This doesn't take a ValueIDNum, because the definition and its location 603ae6f7882SJeremy Morse /// are synonymous. 604ae6f7882SJeremy Morse void defReg(Register R, unsigned BB, unsigned Inst) { 605ae6f7882SJeremy Morse unsigned ID = getLocID(R, false); 606ae6f7882SJeremy Morse LocIdx Idx = lookupOrTrackRegister(ID); 607ae6f7882SJeremy Morse ValueIDNum ValueID = {BB, Inst, Idx}; 608ae6f7882SJeremy Morse LocIdxToIDNum[Idx] = ValueID; 609ae6f7882SJeremy Morse } 610ae6f7882SJeremy Morse 611ae6f7882SJeremy Morse /// Set a register to a value number. To be used if the value number is 612ae6f7882SJeremy Morse /// known in advance. 613ae6f7882SJeremy Morse void setReg(Register R, ValueIDNum ValueID) { 614ae6f7882SJeremy Morse unsigned ID = getLocID(R, false); 615ae6f7882SJeremy Morse LocIdx Idx = lookupOrTrackRegister(ID); 616ae6f7882SJeremy Morse LocIdxToIDNum[Idx] = ValueID; 617ae6f7882SJeremy Morse } 618ae6f7882SJeremy Morse 619ae6f7882SJeremy Morse ValueIDNum readReg(Register R) { 620ae6f7882SJeremy Morse unsigned ID = getLocID(R, false); 621ae6f7882SJeremy Morse LocIdx Idx = lookupOrTrackRegister(ID); 622ae6f7882SJeremy Morse return LocIdxToIDNum[Idx]; 623ae6f7882SJeremy Morse } 624ae6f7882SJeremy Morse 625ae6f7882SJeremy Morse /// Reset a register value to zero / empty. Needed to replicate the 626ae6f7882SJeremy Morse /// VarLoc implementation where a copy to/from a register effectively 627ae6f7882SJeremy Morse /// clears the contents of the source register. (Values can only have one 628ae6f7882SJeremy Morse /// machine location in VarLocBasedImpl). 629ae6f7882SJeremy Morse void wipeRegister(Register R) { 630ae6f7882SJeremy Morse unsigned ID = getLocID(R, false); 631ae6f7882SJeremy Morse LocIdx Idx = LocIDToLocIdx[ID]; 632ae6f7882SJeremy Morse LocIdxToIDNum[Idx] = ValueIDNum::EmptyValue; 633ae6f7882SJeremy Morse } 634ae6f7882SJeremy Morse 635ae6f7882SJeremy Morse /// Determine the LocIdx of an existing register. 636ae6f7882SJeremy Morse LocIdx getRegMLoc(Register R) { 637ae6f7882SJeremy Morse unsigned ID = getLocID(R, false); 638ae6f7882SJeremy Morse return LocIDToLocIdx[ID]; 639ae6f7882SJeremy Morse } 640ae6f7882SJeremy Morse 641ae6f7882SJeremy Morse /// Record a RegMask operand being executed. Defs any register we currently 642ae6f7882SJeremy Morse /// track, stores a pointer to the mask in case we have to account for it 643ae6f7882SJeremy Morse /// later. 644ae6f7882SJeremy Morse void writeRegMask(const MachineOperand *MO, unsigned CurBB, unsigned InstID) { 645ae6f7882SJeremy Morse // Ensure SP exists, so that we don't override it later. 6464634ad6cSGaurav Jain Register SP = TLI.getStackPointerRegisterToSaveRestore(); 647ae6f7882SJeremy Morse 648ae6f7882SJeremy Morse // Def any register we track have that isn't preserved. The regmask 649ae6f7882SJeremy Morse // terminates the liveness of a register, meaning its value can't be 650ae6f7882SJeremy Morse // relied upon -- we represent this by giving it a new value. 651ae6f7882SJeremy Morse for (auto Location : locations()) { 652ae6f7882SJeremy Morse unsigned ID = LocIdxToLocID[Location.Idx]; 653ae6f7882SJeremy Morse // Don't clobber SP, even if the mask says it's clobbered. 654ae6f7882SJeremy Morse if (ID < NumRegs && ID != SP && MO->clobbersPhysReg(ID)) 655ae6f7882SJeremy Morse defReg(ID, CurBB, InstID); 656ae6f7882SJeremy Morse } 657ae6f7882SJeremy Morse Masks.push_back(std::make_pair(MO, InstID)); 658ae6f7882SJeremy Morse } 659ae6f7882SJeremy Morse 660ae6f7882SJeremy Morse /// Find LocIdx for SpillLoc \p L, creating a new one if it's not tracked. 661ae6f7882SJeremy Morse LocIdx getOrTrackSpillLoc(SpillLoc L) { 662ae6f7882SJeremy Morse unsigned SpillID = SpillLocs.idFor(L); 663ae6f7882SJeremy Morse if (SpillID == 0) { 664ae6f7882SJeremy Morse SpillID = SpillLocs.insert(L); 665ae6f7882SJeremy Morse unsigned L = getLocID(SpillID, true); 666ae6f7882SJeremy Morse LocIdx Idx = LocIdx(LocIdxToIDNum.size()); // New idx 667ae6f7882SJeremy Morse LocIdxToIDNum.grow(Idx); 668ae6f7882SJeremy Morse LocIdxToLocID.grow(Idx); 669ae6f7882SJeremy Morse LocIDToLocIdx.push_back(Idx); 670ae6f7882SJeremy Morse LocIdxToLocID[Idx] = L; 671ae6f7882SJeremy Morse return Idx; 672ae6f7882SJeremy Morse } else { 673ae6f7882SJeremy Morse unsigned L = getLocID(SpillID, true); 674ae6f7882SJeremy Morse LocIdx Idx = LocIDToLocIdx[L]; 675ae6f7882SJeremy Morse return Idx; 676ae6f7882SJeremy Morse } 677ae6f7882SJeremy Morse } 678ae6f7882SJeremy Morse 679ae6f7882SJeremy Morse /// Set the value stored in a spill slot. 680ae6f7882SJeremy Morse void setSpill(SpillLoc L, ValueIDNum ValueID) { 681ae6f7882SJeremy Morse LocIdx Idx = getOrTrackSpillLoc(L); 682ae6f7882SJeremy Morse LocIdxToIDNum[Idx] = ValueID; 683ae6f7882SJeremy Morse } 684ae6f7882SJeremy Morse 685ae6f7882SJeremy Morse /// Read whatever value is in a spill slot, or None if it isn't tracked. 686ae6f7882SJeremy Morse Optional<ValueIDNum> readSpill(SpillLoc L) { 687ae6f7882SJeremy Morse unsigned SpillID = SpillLocs.idFor(L); 688ae6f7882SJeremy Morse if (SpillID == 0) 689ae6f7882SJeremy Morse return None; 690ae6f7882SJeremy Morse 691ae6f7882SJeremy Morse unsigned LocID = getLocID(SpillID, true); 692ae6f7882SJeremy Morse LocIdx Idx = LocIDToLocIdx[LocID]; 693ae6f7882SJeremy Morse return LocIdxToIDNum[Idx]; 694ae6f7882SJeremy Morse } 695ae6f7882SJeremy Morse 696ae6f7882SJeremy Morse /// Determine the LocIdx of a spill slot. Return None if it previously 697ae6f7882SJeremy Morse /// hasn't had a value assigned. 698ae6f7882SJeremy Morse Optional<LocIdx> getSpillMLoc(SpillLoc L) { 699ae6f7882SJeremy Morse unsigned SpillID = SpillLocs.idFor(L); 700ae6f7882SJeremy Morse if (SpillID == 0) 701ae6f7882SJeremy Morse return None; 702ae6f7882SJeremy Morse unsigned LocNo = getLocID(SpillID, true); 703ae6f7882SJeremy Morse return LocIDToLocIdx[LocNo]; 704ae6f7882SJeremy Morse } 705ae6f7882SJeremy Morse 706ae6f7882SJeremy Morse /// Return true if Idx is a spill machine location. 707ae6f7882SJeremy Morse bool isSpill(LocIdx Idx) const { 708ae6f7882SJeremy Morse return LocIdxToLocID[Idx] >= NumRegs; 709ae6f7882SJeremy Morse } 710ae6f7882SJeremy Morse 711ae6f7882SJeremy Morse MLocIterator begin() { 712ae6f7882SJeremy Morse return MLocIterator(LocIdxToIDNum, 0); 713ae6f7882SJeremy Morse } 714ae6f7882SJeremy Morse 715ae6f7882SJeremy Morse MLocIterator end() { 716ae6f7882SJeremy Morse return MLocIterator(LocIdxToIDNum, LocIdxToIDNum.size()); 717ae6f7882SJeremy Morse } 718ae6f7882SJeremy Morse 719ae6f7882SJeremy Morse /// Return a range over all locations currently tracked. 720ae6f7882SJeremy Morse iterator_range<MLocIterator> locations() { 721ae6f7882SJeremy Morse return llvm::make_range(begin(), end()); 722ae6f7882SJeremy Morse } 723ae6f7882SJeremy Morse 724ae6f7882SJeremy Morse std::string LocIdxToName(LocIdx Idx) const { 725ae6f7882SJeremy Morse unsigned ID = LocIdxToLocID[Idx]; 726ae6f7882SJeremy Morse if (ID >= NumRegs) 727ae6f7882SJeremy Morse return Twine("slot ").concat(Twine(ID - NumRegs)).str(); 728ae6f7882SJeremy Morse else 729ae6f7882SJeremy Morse return TRI.getRegAsmName(ID).str(); 730ae6f7882SJeremy Morse } 731ae6f7882SJeremy Morse 732ae6f7882SJeremy Morse std::string IDAsString(const ValueIDNum &Num) const { 733ae6f7882SJeremy Morse std::string DefName = LocIdxToName(Num.getLoc()); 734ae6f7882SJeremy Morse return Num.asString(DefName); 735ae6f7882SJeremy Morse } 736ae6f7882SJeremy Morse 737ae6f7882SJeremy Morse LLVM_DUMP_METHOD 738ae6f7882SJeremy Morse void dump() { 739ae6f7882SJeremy Morse for (auto Location : locations()) { 740ae6f7882SJeremy Morse std::string MLocName = LocIdxToName(Location.Value.getLoc()); 741ae6f7882SJeremy Morse std::string DefName = Location.Value.asString(MLocName); 742ae6f7882SJeremy Morse dbgs() << LocIdxToName(Location.Idx) << " --> " << DefName << "\n"; 743ae6f7882SJeremy Morse } 744ae6f7882SJeremy Morse } 745ae6f7882SJeremy Morse 746ae6f7882SJeremy Morse LLVM_DUMP_METHOD 747ae6f7882SJeremy Morse void dump_mloc_map() { 748ae6f7882SJeremy Morse for (auto Location : locations()) { 749ae6f7882SJeremy Morse std::string foo = LocIdxToName(Location.Idx); 750ae6f7882SJeremy Morse dbgs() << "Idx " << Location.Idx.asU64() << " " << foo << "\n"; 751ae6f7882SJeremy Morse } 752ae6f7882SJeremy Morse } 753ae6f7882SJeremy Morse 754ae6f7882SJeremy Morse /// Create a DBG_VALUE based on machine location \p MLoc. Qualify it with the 755ae6f7882SJeremy Morse /// information in \pProperties, for variable Var. Don't insert it anywhere, 756ae6f7882SJeremy Morse /// just return the builder for it. 757ae6f7882SJeremy Morse MachineInstrBuilder emitLoc(Optional<LocIdx> MLoc, const DebugVariable &Var, 758ae6f7882SJeremy Morse const DbgValueProperties &Properties) { 759*b5ad32efSFangrui Song DebugLoc DL = DILocation::get(Var.getVariable()->getContext(), 0, 0, 760*b5ad32efSFangrui Song Var.getVariable()->getScope(), 761*b5ad32efSFangrui Song const_cast<DILocation *>(Var.getInlinedAt())); 762ae6f7882SJeremy Morse auto MIB = BuildMI(MF, DL, TII.get(TargetOpcode::DBG_VALUE)); 763ae6f7882SJeremy Morse 764ae6f7882SJeremy Morse const DIExpression *Expr = Properties.DIExpr; 765ae6f7882SJeremy Morse if (!MLoc) { 766ae6f7882SJeremy Morse // No location -> DBG_VALUE $noreg 767ae6f7882SJeremy Morse MIB.addReg(0, RegState::Debug); 768ae6f7882SJeremy Morse MIB.addReg(0, RegState::Debug); 769ae6f7882SJeremy Morse } else if (LocIdxToLocID[*MLoc] >= NumRegs) { 770ae6f7882SJeremy Morse unsigned LocID = LocIdxToLocID[*MLoc]; 771ae6f7882SJeremy Morse const SpillLoc &Spill = SpillLocs[LocID - NumRegs + 1]; 772ae6f7882SJeremy Morse Expr = DIExpression::prepend(Expr, DIExpression::ApplyOffset, 773ae6f7882SJeremy Morse Spill.SpillOffset); 774ae6f7882SJeremy Morse unsigned Base = Spill.SpillBase; 775ae6f7882SJeremy Morse MIB.addReg(Base, RegState::Debug); 776ae6f7882SJeremy Morse MIB.addImm(0); 777ae6f7882SJeremy Morse } else { 778ae6f7882SJeremy Morse unsigned LocID = LocIdxToLocID[*MLoc]; 779ae6f7882SJeremy Morse MIB.addReg(LocID, RegState::Debug); 780ae6f7882SJeremy Morse if (Properties.Indirect) 781ae6f7882SJeremy Morse MIB.addImm(0); 782ae6f7882SJeremy Morse else 783ae6f7882SJeremy Morse MIB.addReg(0, RegState::Debug); 784ae6f7882SJeremy Morse } 785ae6f7882SJeremy Morse 786ae6f7882SJeremy Morse MIB.addMetadata(Var.getVariable()); 787ae6f7882SJeremy Morse MIB.addMetadata(Expr); 788ae6f7882SJeremy Morse return MIB; 789ae6f7882SJeremy Morse } 790ae6f7882SJeremy Morse }; 791ae6f7882SJeremy Morse 792ae6f7882SJeremy Morse /// Class recording the (high level) _value_ of a variable. Identifies either 793ae6f7882SJeremy Morse /// the value of the variable as a ValueIDNum, or a constant MachineOperand. 794ae6f7882SJeremy Morse /// This class also stores meta-information about how the value is qualified. 795ae6f7882SJeremy Morse /// Used to reason about variable values when performing the second 796ae6f7882SJeremy Morse /// (DebugVariable specific) dataflow analysis. 797ae6f7882SJeremy Morse class DbgValue { 798ae6f7882SJeremy Morse public: 799ae6f7882SJeremy Morse union { 800ae6f7882SJeremy Morse /// If Kind is Def, the value number that this value is based on. 801ae6f7882SJeremy Morse ValueIDNum ID; 802ae6f7882SJeremy Morse /// If Kind is Const, the MachineOperand defining this value. 803ae6f7882SJeremy Morse MachineOperand MO; 804ae6f7882SJeremy Morse /// For a NoVal DbgValue, which block it was generated in. 805ae6f7882SJeremy Morse unsigned BlockNo; 806ae6f7882SJeremy Morse }; 807ae6f7882SJeremy Morse /// Qualifiers for the ValueIDNum above. 808ae6f7882SJeremy Morse DbgValueProperties Properties; 809ae6f7882SJeremy Morse 810ae6f7882SJeremy Morse typedef enum { 811ae6f7882SJeremy Morse Undef, // Represents a DBG_VALUE $noreg in the transfer function only. 812ae6f7882SJeremy Morse Def, // This value is defined by an inst, or is a PHI value. 813ae6f7882SJeremy Morse Const, // A constant value contained in the MachineOperand field. 814ae6f7882SJeremy Morse Proposed, // This is a tentative PHI value, which may be confirmed or 815ae6f7882SJeremy Morse // invalidated later. 816ae6f7882SJeremy Morse NoVal // Empty DbgValue, generated during dataflow. BlockNo stores 817ae6f7882SJeremy Morse // which block this was generated in. 818ae6f7882SJeremy Morse } KindT; 819ae6f7882SJeremy Morse /// Discriminator for whether this is a constant or an in-program value. 820ae6f7882SJeremy Morse KindT Kind; 821ae6f7882SJeremy Morse 822ae6f7882SJeremy Morse DbgValue(const ValueIDNum &Val, const DbgValueProperties &Prop, KindT Kind) 823ae6f7882SJeremy Morse : ID(Val), Properties(Prop), Kind(Kind) { 824ae6f7882SJeremy Morse assert(Kind == Def || Kind == Proposed); 825ae6f7882SJeremy Morse } 826ae6f7882SJeremy Morse 827ae6f7882SJeremy Morse DbgValue(unsigned BlockNo, const DbgValueProperties &Prop, KindT Kind) 828ae6f7882SJeremy Morse : BlockNo(BlockNo), Properties(Prop), Kind(Kind) { 829ae6f7882SJeremy Morse assert(Kind == NoVal); 830ae6f7882SJeremy Morse } 831ae6f7882SJeremy Morse 832ae6f7882SJeremy Morse DbgValue(const MachineOperand &MO, const DbgValueProperties &Prop, KindT Kind) 833ae6f7882SJeremy Morse : MO(MO), Properties(Prop), Kind(Kind) { 834ae6f7882SJeremy Morse assert(Kind == Const); 835ae6f7882SJeremy Morse } 836ae6f7882SJeremy Morse 837ae6f7882SJeremy Morse DbgValue(const DbgValueProperties &Prop, KindT Kind) 838ae6f7882SJeremy Morse : Properties(Prop), Kind(Kind) { 839ae6f7882SJeremy Morse assert(Kind == Undef && 840ae6f7882SJeremy Morse "Empty DbgValue constructor must pass in Undef kind"); 841ae6f7882SJeremy Morse } 842ae6f7882SJeremy Morse 843ae6f7882SJeremy Morse void dump(const MLocTracker *MTrack) const { 844ae6f7882SJeremy Morse if (Kind == Const) { 845ae6f7882SJeremy Morse MO.dump(); 846ae6f7882SJeremy Morse } else if (Kind == NoVal) { 847ae6f7882SJeremy Morse dbgs() << "NoVal(" << BlockNo << ")"; 848ae6f7882SJeremy Morse } else if (Kind == Proposed) { 849ae6f7882SJeremy Morse dbgs() << "VPHI(" << MTrack->IDAsString(ID) << ")"; 850ae6f7882SJeremy Morse } else { 851ae6f7882SJeremy Morse assert(Kind == Def); 852ae6f7882SJeremy Morse dbgs() << MTrack->IDAsString(ID); 853ae6f7882SJeremy Morse } 854ae6f7882SJeremy Morse if (Properties.Indirect) 855ae6f7882SJeremy Morse dbgs() << " indir"; 856ae6f7882SJeremy Morse if (Properties.DIExpr) 857ae6f7882SJeremy Morse dbgs() << " " << *Properties.DIExpr; 858ae6f7882SJeremy Morse } 859ae6f7882SJeremy Morse 860ae6f7882SJeremy Morse bool operator==(const DbgValue &Other) const { 861ae6f7882SJeremy Morse if (std::tie(Kind, Properties) != std::tie(Other.Kind, Other.Properties)) 862ae6f7882SJeremy Morse return false; 863ae6f7882SJeremy Morse else if (Kind == Proposed && ID != Other.ID) 864ae6f7882SJeremy Morse return false; 865ae6f7882SJeremy Morse else if (Kind == Def && ID != Other.ID) 866ae6f7882SJeremy Morse return false; 867ae6f7882SJeremy Morse else if (Kind == NoVal && BlockNo != Other.BlockNo) 868ae6f7882SJeremy Morse return false; 869ae6f7882SJeremy Morse else if (Kind == Const) 870ae6f7882SJeremy Morse return MO.isIdenticalTo(Other.MO); 871ae6f7882SJeremy Morse 872ae6f7882SJeremy Morse return true; 873ae6f7882SJeremy Morse } 874ae6f7882SJeremy Morse 875ae6f7882SJeremy Morse bool operator!=(const DbgValue &Other) const { return !(*this == Other); } 876ae6f7882SJeremy Morse }; 877ae6f7882SJeremy Morse 878ae6f7882SJeremy Morse /// Types for recording sets of variable fragments that overlap. For a given 879ae6f7882SJeremy Morse /// local variable, we record all other fragments of that variable that could 880ae6f7882SJeremy Morse /// overlap it, to reduce search time. 881ae6f7882SJeremy Morse using FragmentOfVar = 882ae6f7882SJeremy Morse std::pair<const DILocalVariable *, DIExpression::FragmentInfo>; 883ae6f7882SJeremy Morse using OverlapMap = 884ae6f7882SJeremy Morse DenseMap<FragmentOfVar, SmallVector<DIExpression::FragmentInfo, 1>>; 885ae6f7882SJeremy Morse 886ae6f7882SJeremy Morse /// Collection of DBG_VALUEs observed when traversing a block. Records each 887ae6f7882SJeremy Morse /// variable and the value the DBG_VALUE refers to. Requires the machine value 888ae6f7882SJeremy Morse /// location dataflow algorithm to have run already, so that values can be 889ae6f7882SJeremy Morse /// identified. 890ae6f7882SJeremy Morse class VLocTracker { 891ae6f7882SJeremy Morse public: 892ae6f7882SJeremy Morse /// Map DebugVariable to the latest Value it's defined to have. 893ae6f7882SJeremy Morse /// Needs to be a MapVector because we determine order-in-the-input-MIR from 894ae6f7882SJeremy Morse /// the order in this container. 895ae6f7882SJeremy Morse /// We only retain the last DbgValue in each block for each variable, to 896ae6f7882SJeremy Morse /// determine the blocks live-out variable value. The Vars container forms the 897ae6f7882SJeremy Morse /// transfer function for this block, as part of the dataflow analysis. The 898ae6f7882SJeremy Morse /// movement of values between locations inside of a block is handled at a 899ae6f7882SJeremy Morse /// much later stage, in the TransferTracker class. 900ae6f7882SJeremy Morse MapVector<DebugVariable, DbgValue> Vars; 901ae6f7882SJeremy Morse DenseMap<DebugVariable, const DILocation *> Scopes; 902ae6f7882SJeremy Morse MachineBasicBlock *MBB; 903ae6f7882SJeremy Morse 904ae6f7882SJeremy Morse public: 905ae6f7882SJeremy Morse VLocTracker() {} 906ae6f7882SJeremy Morse 90768f47157SJeremy Morse void defVar(const MachineInstr &MI, const DbgValueProperties &Properties, 90868f47157SJeremy Morse Optional<ValueIDNum> ID) { 90968f47157SJeremy Morse assert(MI.isDebugValue() || MI.isDebugRef()); 910ae6f7882SJeremy Morse DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(), 911ae6f7882SJeremy Morse MI.getDebugLoc()->getInlinedAt()); 912ae6f7882SJeremy Morse DbgValue Rec = (ID) ? DbgValue(*ID, Properties, DbgValue::Def) 913ae6f7882SJeremy Morse : DbgValue(Properties, DbgValue::Undef); 914ae6f7882SJeremy Morse 915ae6f7882SJeremy Morse // Attempt insertion; overwrite if it's already mapped. 916ae6f7882SJeremy Morse auto Result = Vars.insert(std::make_pair(Var, Rec)); 917ae6f7882SJeremy Morse if (!Result.second) 918ae6f7882SJeremy Morse Result.first->second = Rec; 919ae6f7882SJeremy Morse Scopes[Var] = MI.getDebugLoc().get(); 920ae6f7882SJeremy Morse } 921ae6f7882SJeremy Morse 922ae6f7882SJeremy Morse void defVar(const MachineInstr &MI, const MachineOperand &MO) { 92368f47157SJeremy Morse // Only DBG_VALUEs can define constant-valued variables. 924ae6f7882SJeremy Morse assert(MI.isDebugValue()); 925ae6f7882SJeremy Morse DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(), 926ae6f7882SJeremy Morse MI.getDebugLoc()->getInlinedAt()); 927ae6f7882SJeremy Morse DbgValueProperties Properties(MI); 928ae6f7882SJeremy Morse DbgValue Rec = DbgValue(MO, Properties, DbgValue::Const); 929ae6f7882SJeremy Morse 930ae6f7882SJeremy Morse // Attempt insertion; overwrite if it's already mapped. 931ae6f7882SJeremy Morse auto Result = Vars.insert(std::make_pair(Var, Rec)); 932ae6f7882SJeremy Morse if (!Result.second) 933ae6f7882SJeremy Morse Result.first->second = Rec; 934ae6f7882SJeremy Morse Scopes[Var] = MI.getDebugLoc().get(); 935ae6f7882SJeremy Morse } 936ae6f7882SJeremy Morse }; 937ae6f7882SJeremy Morse 938ae6f7882SJeremy Morse /// Tracker for converting machine value locations and variable values into 939ae6f7882SJeremy Morse /// variable locations (the output of LiveDebugValues), recorded as DBG_VALUEs 940ae6f7882SJeremy Morse /// specifying block live-in locations and transfers within blocks. 941ae6f7882SJeremy Morse /// 942ae6f7882SJeremy Morse /// Operating on a per-block basis, this class takes a (pre-loaded) MLocTracker 943ae6f7882SJeremy Morse /// and must be initialized with the set of variable values that are live-in to 944ae6f7882SJeremy Morse /// the block. The caller then repeatedly calls process(). TransferTracker picks 945ae6f7882SJeremy Morse /// out variable locations for the live-in variable values (if there _is_ a 946ae6f7882SJeremy Morse /// location) and creates the corresponding DBG_VALUEs. Then, as the block is 947ae6f7882SJeremy Morse /// stepped through, transfers of values between machine locations are 948ae6f7882SJeremy Morse /// identified and if profitable, a DBG_VALUE created. 949ae6f7882SJeremy Morse /// 950ae6f7882SJeremy Morse /// This is where debug use-before-defs would be resolved: a variable with an 951ae6f7882SJeremy Morse /// unavailable value could materialize in the middle of a block, when the 952ae6f7882SJeremy Morse /// value becomes available. Or, we could detect clobbers and re-specify the 953ae6f7882SJeremy Morse /// variable in a backup location. (XXX these are unimplemented). 954ae6f7882SJeremy Morse class TransferTracker { 955ae6f7882SJeremy Morse public: 956ae6f7882SJeremy Morse const TargetInstrInfo *TII; 957ae6f7882SJeremy Morse /// This machine location tracker is assumed to always contain the up-to-date 958ae6f7882SJeremy Morse /// value mapping for all machine locations. TransferTracker only reads 959ae6f7882SJeremy Morse /// information from it. (XXX make it const?) 960ae6f7882SJeremy Morse MLocTracker *MTracker; 961ae6f7882SJeremy Morse MachineFunction &MF; 962ae6f7882SJeremy Morse 963ae6f7882SJeremy Morse /// Record of all changes in variable locations at a block position. Awkwardly 964ae6f7882SJeremy Morse /// we allow inserting either before or after the point: MBB != nullptr 965ae6f7882SJeremy Morse /// indicates it's before, otherwise after. 966ae6f7882SJeremy Morse struct Transfer { 967ae6f7882SJeremy Morse MachineBasicBlock::iterator Pos; /// Position to insert DBG_VALUes 968ae6f7882SJeremy Morse MachineBasicBlock *MBB; /// non-null if we should insert after. 969ae6f7882SJeremy Morse SmallVector<MachineInstr *, 4> Insts; /// Vector of DBG_VALUEs to insert. 970ae6f7882SJeremy Morse }; 971ae6f7882SJeremy Morse 972ae6f7882SJeremy Morse typedef struct { 973ae6f7882SJeremy Morse LocIdx Loc; 974ae6f7882SJeremy Morse DbgValueProperties Properties; 975ae6f7882SJeremy Morse } LocAndProperties; 976ae6f7882SJeremy Morse 977ae6f7882SJeremy Morse /// Collection of transfers (DBG_VALUEs) to be inserted. 978ae6f7882SJeremy Morse SmallVector<Transfer, 32> Transfers; 979ae6f7882SJeremy Morse 980ae6f7882SJeremy Morse /// Local cache of what-value-is-in-what-LocIdx. Used to identify differences 981ae6f7882SJeremy Morse /// between TransferTrackers view of variable locations and MLocTrackers. For 982ae6f7882SJeremy Morse /// example, MLocTracker observes all clobbers, but TransferTracker lazily 983ae6f7882SJeremy Morse /// does not. 984ae6f7882SJeremy Morse std::vector<ValueIDNum> VarLocs; 985ae6f7882SJeremy Morse 986ae6f7882SJeremy Morse /// Map from LocIdxes to which DebugVariables are based that location. 987ae6f7882SJeremy Morse /// Mantained while stepping through the block. Not accurate if 988ae6f7882SJeremy Morse /// VarLocs[Idx] != MTracker->LocIdxToIDNum[Idx]. 989ae6f7882SJeremy Morse std::map<LocIdx, SmallSet<DebugVariable, 4>> ActiveMLocs; 990ae6f7882SJeremy Morse 991ae6f7882SJeremy Morse /// Map from DebugVariable to it's current location and qualifying meta 992ae6f7882SJeremy Morse /// information. To be used in conjunction with ActiveMLocs to construct 993ae6f7882SJeremy Morse /// enough information for the DBG_VALUEs for a particular LocIdx. 994ae6f7882SJeremy Morse DenseMap<DebugVariable, LocAndProperties> ActiveVLocs; 995ae6f7882SJeremy Morse 996ae6f7882SJeremy Morse /// Temporary cache of DBG_VALUEs to be entered into the Transfers collection. 997ae6f7882SJeremy Morse SmallVector<MachineInstr *, 4> PendingDbgValues; 998ae6f7882SJeremy Morse 999b1b2c6abSJeremy Morse /// Record of a use-before-def: created when a value that's live-in to the 1000b1b2c6abSJeremy Morse /// current block isn't available in any machine location, but it will be 1001b1b2c6abSJeremy Morse /// defined in this block. 1002b1b2c6abSJeremy Morse struct UseBeforeDef { 1003b1b2c6abSJeremy Morse /// Value of this variable, def'd in block. 1004b1b2c6abSJeremy Morse ValueIDNum ID; 1005b1b2c6abSJeremy Morse /// Identity of this variable. 1006b1b2c6abSJeremy Morse DebugVariable Var; 1007b1b2c6abSJeremy Morse /// Additional variable properties. 1008b1b2c6abSJeremy Morse DbgValueProperties Properties; 1009b1b2c6abSJeremy Morse }; 1010b1b2c6abSJeremy Morse 1011b1b2c6abSJeremy Morse /// Map from instruction index (within the block) to the set of UseBeforeDefs 1012b1b2c6abSJeremy Morse /// that become defined at that instruction. 1013b1b2c6abSJeremy Morse DenseMap<unsigned, SmallVector<UseBeforeDef, 1>> UseBeforeDefs; 1014b1b2c6abSJeremy Morse 1015b1b2c6abSJeremy Morse /// The set of variables that are in UseBeforeDefs and can become a location 1016b1b2c6abSJeremy Morse /// once the relevant value is defined. An element being erased from this 1017b1b2c6abSJeremy Morse /// collection prevents the use-before-def materializing. 1018b1b2c6abSJeremy Morse DenseSet<DebugVariable> UseBeforeDefVariables; 1019b1b2c6abSJeremy Morse 1020ae6f7882SJeremy Morse const TargetRegisterInfo &TRI; 1021ae6f7882SJeremy Morse const BitVector &CalleeSavedRegs; 1022ae6f7882SJeremy Morse 1023ae6f7882SJeremy Morse TransferTracker(const TargetInstrInfo *TII, MLocTracker *MTracker, 1024ae6f7882SJeremy Morse MachineFunction &MF, const TargetRegisterInfo &TRI, 1025ae6f7882SJeremy Morse const BitVector &CalleeSavedRegs) 1026ae6f7882SJeremy Morse : TII(TII), MTracker(MTracker), MF(MF), TRI(TRI), 1027ae6f7882SJeremy Morse CalleeSavedRegs(CalleeSavedRegs) {} 1028ae6f7882SJeremy Morse 1029ae6f7882SJeremy Morse /// Load object with live-in variable values. \p mlocs contains the live-in 1030ae6f7882SJeremy Morse /// values in each machine location, while \p vlocs the live-in variable 1031ae6f7882SJeremy Morse /// values. This method picks variable locations for the live-in variables, 1032ae6f7882SJeremy Morse /// creates DBG_VALUEs and puts them in #Transfers, then prepares the other 1033ae6f7882SJeremy Morse /// object fields to track variable locations as we step through the block. 1034ae6f7882SJeremy Morse /// FIXME: could just examine mloctracker instead of passing in \p mlocs? 1035ae6f7882SJeremy Morse void loadInlocs(MachineBasicBlock &MBB, ValueIDNum *MLocs, 1036ae6f7882SJeremy Morse SmallVectorImpl<std::pair<DebugVariable, DbgValue>> &VLocs, 1037ae6f7882SJeremy Morse unsigned NumLocs) { 1038ae6f7882SJeremy Morse ActiveMLocs.clear(); 1039ae6f7882SJeremy Morse ActiveVLocs.clear(); 1040ae6f7882SJeremy Morse VarLocs.clear(); 1041ae6f7882SJeremy Morse VarLocs.reserve(NumLocs); 1042b1b2c6abSJeremy Morse UseBeforeDefs.clear(); 1043b1b2c6abSJeremy Morse UseBeforeDefVariables.clear(); 1044ae6f7882SJeremy Morse 1045ae6f7882SJeremy Morse auto isCalleeSaved = [&](LocIdx L) { 1046ae6f7882SJeremy Morse unsigned Reg = MTracker->LocIdxToLocID[L]; 1047ae6f7882SJeremy Morse if (Reg >= MTracker->NumRegs) 1048ae6f7882SJeremy Morse return false; 1049ae6f7882SJeremy Morse for (MCRegAliasIterator RAI(Reg, &TRI, true); RAI.isValid(); ++RAI) 1050ae6f7882SJeremy Morse if (CalleeSavedRegs.test(*RAI)) 1051ae6f7882SJeremy Morse return true; 1052ae6f7882SJeremy Morse return false; 1053ae6f7882SJeremy Morse }; 1054ae6f7882SJeremy Morse 1055ae6f7882SJeremy Morse // Map of the preferred location for each value. 1056ae6f7882SJeremy Morse std::map<ValueIDNum, LocIdx> ValueToLoc; 1057ae6f7882SJeremy Morse 1058ae6f7882SJeremy Morse // Produce a map of value numbers to the current machine locs they live 1059ae6f7882SJeremy Morse // in. When emulating VarLocBasedImpl, there should only be one 1060ae6f7882SJeremy Morse // location; when not, we get to pick. 1061ae6f7882SJeremy Morse for (auto Location : MTracker->locations()) { 1062ae6f7882SJeremy Morse LocIdx Idx = Location.Idx; 1063ae6f7882SJeremy Morse ValueIDNum &VNum = MLocs[Idx.asU64()]; 1064ae6f7882SJeremy Morse VarLocs.push_back(VNum); 1065ae6f7882SJeremy Morse auto it = ValueToLoc.find(VNum); 1066ae6f7882SJeremy Morse // In order of preference, pick: 1067ae6f7882SJeremy Morse // * Callee saved registers, 1068ae6f7882SJeremy Morse // * Other registers, 1069ae6f7882SJeremy Morse // * Spill slots. 1070ae6f7882SJeremy Morse if (it == ValueToLoc.end() || MTracker->isSpill(it->second) || 1071ae6f7882SJeremy Morse (!isCalleeSaved(it->second) && isCalleeSaved(Idx.asU64()))) { 1072ae6f7882SJeremy Morse // Insert, or overwrite if insertion failed. 1073ae6f7882SJeremy Morse auto PrefLocRes = ValueToLoc.insert(std::make_pair(VNum, Idx)); 1074ae6f7882SJeremy Morse if (!PrefLocRes.second) 1075ae6f7882SJeremy Morse PrefLocRes.first->second = Idx; 1076ae6f7882SJeremy Morse } 1077ae6f7882SJeremy Morse } 1078ae6f7882SJeremy Morse 1079ae6f7882SJeremy Morse // Now map variables to their picked LocIdxes. 1080ae6f7882SJeremy Morse for (auto Var : VLocs) { 1081ae6f7882SJeremy Morse if (Var.second.Kind == DbgValue::Const) { 1082ae6f7882SJeremy Morse PendingDbgValues.push_back( 1083ae6f7882SJeremy Morse emitMOLoc(Var.second.MO, Var.first, Var.second.Properties)); 1084ae6f7882SJeremy Morse continue; 1085ae6f7882SJeremy Morse } 1086ae6f7882SJeremy Morse 1087ae6f7882SJeremy Morse // If the value has no location, we can't make a variable location. 1088b1b2c6abSJeremy Morse const ValueIDNum &Num = Var.second.ID; 1089b1b2c6abSJeremy Morse auto ValuesPreferredLoc = ValueToLoc.find(Num); 1090b1b2c6abSJeremy Morse if (ValuesPreferredLoc == ValueToLoc.end()) { 1091b1b2c6abSJeremy Morse // If it's a def that occurs in this block, register it as a 1092b1b2c6abSJeremy Morse // use-before-def to be resolved as we step through the block. 1093b1b2c6abSJeremy Morse if (Num.getBlock() == (unsigned)MBB.getNumber() && !Num.isPHI()) 1094b1b2c6abSJeremy Morse addUseBeforeDef(Var.first, Var.second.Properties, Num); 1095ae6f7882SJeremy Morse continue; 1096b1b2c6abSJeremy Morse } 1097ae6f7882SJeremy Morse 1098ae6f7882SJeremy Morse LocIdx M = ValuesPreferredLoc->second; 1099ae6f7882SJeremy Morse auto NewValue = LocAndProperties{M, Var.second.Properties}; 1100ae6f7882SJeremy Morse auto Result = ActiveVLocs.insert(std::make_pair(Var.first, NewValue)); 1101ae6f7882SJeremy Morse if (!Result.second) 1102ae6f7882SJeremy Morse Result.first->second = NewValue; 1103ae6f7882SJeremy Morse ActiveMLocs[M].insert(Var.first); 1104ae6f7882SJeremy Morse PendingDbgValues.push_back( 1105ae6f7882SJeremy Morse MTracker->emitLoc(M, Var.first, Var.second.Properties)); 1106ae6f7882SJeremy Morse } 1107ae6f7882SJeremy Morse flushDbgValues(MBB.begin(), &MBB); 1108ae6f7882SJeremy Morse } 1109ae6f7882SJeremy Morse 1110b1b2c6abSJeremy Morse /// Record that \p Var has value \p ID, a value that becomes available 1111b1b2c6abSJeremy Morse /// later in the function. 1112b1b2c6abSJeremy Morse void addUseBeforeDef(const DebugVariable &Var, 1113b1b2c6abSJeremy Morse const DbgValueProperties &Properties, ValueIDNum ID) { 1114b1b2c6abSJeremy Morse UseBeforeDef UBD = {ID, Var, Properties}; 1115b1b2c6abSJeremy Morse UseBeforeDefs[ID.getInst()].push_back(UBD); 1116b1b2c6abSJeremy Morse UseBeforeDefVariables.insert(Var); 1117b1b2c6abSJeremy Morse } 1118b1b2c6abSJeremy Morse 1119b1b2c6abSJeremy Morse /// After the instruction at index \p Inst and position \p pos has been 1120b1b2c6abSJeremy Morse /// processed, check whether it defines a variable value in a use-before-def. 1121b1b2c6abSJeremy Morse /// If so, and the variable value hasn't changed since the start of the 1122b1b2c6abSJeremy Morse /// block, create a DBG_VALUE. 1123b1b2c6abSJeremy Morse void checkInstForNewValues(unsigned Inst, MachineBasicBlock::iterator pos) { 1124b1b2c6abSJeremy Morse auto MIt = UseBeforeDefs.find(Inst); 1125b1b2c6abSJeremy Morse if (MIt == UseBeforeDefs.end()) 1126b1b2c6abSJeremy Morse return; 1127b1b2c6abSJeremy Morse 1128b1b2c6abSJeremy Morse for (auto &Use : MIt->second) { 1129b1b2c6abSJeremy Morse LocIdx L = Use.ID.getLoc(); 1130b1b2c6abSJeremy Morse 1131b1b2c6abSJeremy Morse // If something goes very wrong, we might end up labelling a COPY 1132b1b2c6abSJeremy Morse // instruction or similar with an instruction number, where it doesn't 1133b1b2c6abSJeremy Morse // actually define a new value, instead it moves a value. In case this 1134b1b2c6abSJeremy Morse // happens, discard. 1135b1b2c6abSJeremy Morse if (MTracker->LocIdxToIDNum[L] != Use.ID) 1136b1b2c6abSJeremy Morse continue; 1137b1b2c6abSJeremy Morse 1138b1b2c6abSJeremy Morse // If a different debug instruction defined the variable value / location 1139b1b2c6abSJeremy Morse // since the start of the block, don't materialize this use-before-def. 1140b1b2c6abSJeremy Morse if (!UseBeforeDefVariables.count(Use.Var)) 1141b1b2c6abSJeremy Morse continue; 1142b1b2c6abSJeremy Morse 1143b1b2c6abSJeremy Morse PendingDbgValues.push_back(MTracker->emitLoc(L, Use.Var, Use.Properties)); 1144b1b2c6abSJeremy Morse } 1145b1b2c6abSJeremy Morse flushDbgValues(pos, nullptr); 1146b1b2c6abSJeremy Morse } 1147b1b2c6abSJeremy Morse 1148ae6f7882SJeremy Morse /// Helper to move created DBG_VALUEs into Transfers collection. 1149ae6f7882SJeremy Morse void flushDbgValues(MachineBasicBlock::iterator Pos, MachineBasicBlock *MBB) { 1150ae6f7882SJeremy Morse if (PendingDbgValues.size() > 0) { 1151ae6f7882SJeremy Morse Transfers.push_back({Pos, MBB, PendingDbgValues}); 1152ae6f7882SJeremy Morse PendingDbgValues.clear(); 1153ae6f7882SJeremy Morse } 1154ae6f7882SJeremy Morse } 1155ae6f7882SJeremy Morse 115668f47157SJeremy Morse /// Change a variable value after encountering a DBG_VALUE inside a block. 1157ae6f7882SJeremy Morse void redefVar(const MachineInstr &MI) { 1158ae6f7882SJeremy Morse DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(), 1159ae6f7882SJeremy Morse MI.getDebugLoc()->getInlinedAt()); 116068f47157SJeremy Morse DbgValueProperties Properties(MI); 116168f47157SJeremy Morse 1162ae6f7882SJeremy Morse const MachineOperand &MO = MI.getOperand(0); 1163ae6f7882SJeremy Morse 116468f47157SJeremy Morse // Ignore non-register locations, we don't transfer those. 116568f47157SJeremy Morse if (!MO.isReg() || MO.getReg() == 0) { 1166ae6f7882SJeremy Morse auto It = ActiveVLocs.find(Var); 1167ae6f7882SJeremy Morse if (It != ActiveVLocs.end()) { 1168ae6f7882SJeremy Morse ActiveMLocs[It->second.Loc].erase(Var); 1169ae6f7882SJeremy Morse ActiveVLocs.erase(It); 117068f47157SJeremy Morse } 1171b1b2c6abSJeremy Morse // Any use-before-defs no longer apply. 1172b1b2c6abSJeremy Morse UseBeforeDefVariables.erase(Var); 1173ae6f7882SJeremy Morse return; 1174ae6f7882SJeremy Morse } 1175ae6f7882SJeremy Morse 1176ae6f7882SJeremy Morse Register Reg = MO.getReg(); 117768f47157SJeremy Morse LocIdx NewLoc = MTracker->getRegMLoc(Reg); 117868f47157SJeremy Morse redefVar(MI, Properties, NewLoc); 117968f47157SJeremy Morse } 118068f47157SJeremy Morse 118168f47157SJeremy Morse /// Handle a change in variable location within a block. Terminate the 118268f47157SJeremy Morse /// variables current location, and record the value it now refers to, so 118368f47157SJeremy Morse /// that we can detect location transfers later on. 118468f47157SJeremy Morse void redefVar(const MachineInstr &MI, const DbgValueProperties &Properties, 118568f47157SJeremy Morse Optional<LocIdx> OptNewLoc) { 118668f47157SJeremy Morse DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(), 118768f47157SJeremy Morse MI.getDebugLoc()->getInlinedAt()); 1188b1b2c6abSJeremy Morse // Any use-before-defs no longer apply. 1189b1b2c6abSJeremy Morse UseBeforeDefVariables.erase(Var); 119068f47157SJeremy Morse 119168f47157SJeremy Morse // Erase any previous location, 119268f47157SJeremy Morse auto It = ActiveVLocs.find(Var); 119368f47157SJeremy Morse if (It != ActiveVLocs.end()) 119468f47157SJeremy Morse ActiveMLocs[It->second.Loc].erase(Var); 119568f47157SJeremy Morse 119668f47157SJeremy Morse // If there _is_ no new location, all we had to do was erase. 119768f47157SJeremy Morse if (!OptNewLoc) 119868f47157SJeremy Morse return; 119968f47157SJeremy Morse LocIdx NewLoc = *OptNewLoc; 1200ae6f7882SJeremy Morse 1201ae6f7882SJeremy Morse // Check whether our local copy of values-by-location in #VarLocs is out of 1202ae6f7882SJeremy Morse // date. Wipe old tracking data for the location if it's been clobbered in 1203ae6f7882SJeremy Morse // the meantime. 120468f47157SJeremy Morse if (MTracker->getNumAtPos(NewLoc) != VarLocs[NewLoc.asU64()]) { 120568f47157SJeremy Morse for (auto &P : ActiveMLocs[NewLoc]) { 1206ae6f7882SJeremy Morse ActiveVLocs.erase(P); 1207ae6f7882SJeremy Morse } 120868f47157SJeremy Morse ActiveMLocs[NewLoc.asU64()].clear(); 120968f47157SJeremy Morse VarLocs[NewLoc.asU64()] = MTracker->getNumAtPos(NewLoc); 1210ae6f7882SJeremy Morse } 1211ae6f7882SJeremy Morse 121268f47157SJeremy Morse ActiveMLocs[NewLoc].insert(Var); 1213ae6f7882SJeremy Morse if (It == ActiveVLocs.end()) { 121468f47157SJeremy Morse ActiveVLocs.insert( 121568f47157SJeremy Morse std::make_pair(Var, LocAndProperties{NewLoc, Properties})); 1216ae6f7882SJeremy Morse } else { 121768f47157SJeremy Morse It->second.Loc = NewLoc; 1218ae6f7882SJeremy Morse It->second.Properties = Properties; 1219ae6f7882SJeremy Morse } 1220ae6f7882SJeremy Morse } 1221ae6f7882SJeremy Morse 1222ae6f7882SJeremy Morse /// Explicitly terminate variable locations based on \p mloc. Creates undef 1223ae6f7882SJeremy Morse /// DBG_VALUEs for any variables that were located there, and clears 1224ae6f7882SJeremy Morse /// #ActiveMLoc / #ActiveVLoc tracking information for that location. 1225ae6f7882SJeremy Morse void clobberMloc(LocIdx MLoc, MachineBasicBlock::iterator Pos) { 1226ae6f7882SJeremy Morse assert(MTracker->isSpill(MLoc)); 1227ae6f7882SJeremy Morse auto ActiveMLocIt = ActiveMLocs.find(MLoc); 1228ae6f7882SJeremy Morse if (ActiveMLocIt == ActiveMLocs.end()) 1229ae6f7882SJeremy Morse return; 1230ae6f7882SJeremy Morse 1231ae6f7882SJeremy Morse VarLocs[MLoc.asU64()] = ValueIDNum::EmptyValue; 1232ae6f7882SJeremy Morse 1233ae6f7882SJeremy Morse for (auto &Var : ActiveMLocIt->second) { 1234ae6f7882SJeremy Morse auto ActiveVLocIt = ActiveVLocs.find(Var); 1235ae6f7882SJeremy Morse // Create an undef. We can't feed in a nullptr DIExpression alas, 1236ae6f7882SJeremy Morse // so use the variables last expression. Pass None as the location. 1237ae6f7882SJeremy Morse const DIExpression *Expr = ActiveVLocIt->second.Properties.DIExpr; 1238ae6f7882SJeremy Morse DbgValueProperties Properties(Expr, false); 1239ae6f7882SJeremy Morse PendingDbgValues.push_back(MTracker->emitLoc(None, Var, Properties)); 1240ae6f7882SJeremy Morse ActiveVLocs.erase(ActiveVLocIt); 1241ae6f7882SJeremy Morse } 1242ae6f7882SJeremy Morse flushDbgValues(Pos, nullptr); 1243ae6f7882SJeremy Morse 1244ae6f7882SJeremy Morse ActiveMLocIt->second.clear(); 1245ae6f7882SJeremy Morse } 1246ae6f7882SJeremy Morse 1247ae6f7882SJeremy Morse /// Transfer variables based on \p Src to be based on \p Dst. This handles 1248ae6f7882SJeremy Morse /// both register copies as well as spills and restores. Creates DBG_VALUEs 1249ae6f7882SJeremy Morse /// describing the movement. 1250ae6f7882SJeremy Morse void transferMlocs(LocIdx Src, LocIdx Dst, MachineBasicBlock::iterator Pos) { 1251ae6f7882SJeremy Morse // Does Src still contain the value num we expect? If not, it's been 1252ae6f7882SJeremy Morse // clobbered in the meantime, and our variable locations are stale. 1253ae6f7882SJeremy Morse if (VarLocs[Src.asU64()] != MTracker->getNumAtPos(Src)) 1254ae6f7882SJeremy Morse return; 1255ae6f7882SJeremy Morse 1256ae6f7882SJeremy Morse // assert(ActiveMLocs[Dst].size() == 0); 1257ae6f7882SJeremy Morse //^^^ Legitimate scenario on account of un-clobbered slot being assigned to? 1258ae6f7882SJeremy Morse ActiveMLocs[Dst] = ActiveMLocs[Src]; 1259ae6f7882SJeremy Morse VarLocs[Dst.asU64()] = VarLocs[Src.asU64()]; 1260ae6f7882SJeremy Morse 1261ae6f7882SJeremy Morse // For each variable based on Src; create a location at Dst. 1262ae6f7882SJeremy Morse for (auto &Var : ActiveMLocs[Src]) { 1263ae6f7882SJeremy Morse auto ActiveVLocIt = ActiveVLocs.find(Var); 1264ae6f7882SJeremy Morse assert(ActiveVLocIt != ActiveVLocs.end()); 1265ae6f7882SJeremy Morse ActiveVLocIt->second.Loc = Dst; 1266ae6f7882SJeremy Morse 1267ae6f7882SJeremy Morse assert(Dst != 0); 1268ae6f7882SJeremy Morse MachineInstr *MI = 1269ae6f7882SJeremy Morse MTracker->emitLoc(Dst, Var, ActiveVLocIt->second.Properties); 1270ae6f7882SJeremy Morse PendingDbgValues.push_back(MI); 1271ae6f7882SJeremy Morse } 1272ae6f7882SJeremy Morse ActiveMLocs[Src].clear(); 1273ae6f7882SJeremy Morse flushDbgValues(Pos, nullptr); 1274ae6f7882SJeremy Morse 1275ae6f7882SJeremy Morse // XXX XXX XXX "pretend to be old LDV" means dropping all tracking data 1276ae6f7882SJeremy Morse // about the old location. 1277ae6f7882SJeremy Morse if (EmulateOldLDV) 1278ae6f7882SJeremy Morse VarLocs[Src.asU64()] = ValueIDNum::EmptyValue; 1279ae6f7882SJeremy Morse } 1280ae6f7882SJeremy Morse 1281ae6f7882SJeremy Morse MachineInstrBuilder emitMOLoc(const MachineOperand &MO, 1282ae6f7882SJeremy Morse const DebugVariable &Var, 1283ae6f7882SJeremy Morse const DbgValueProperties &Properties) { 1284*b5ad32efSFangrui Song DebugLoc DL = DILocation::get(Var.getVariable()->getContext(), 0, 0, 1285*b5ad32efSFangrui Song Var.getVariable()->getScope(), 1286*b5ad32efSFangrui Song const_cast<DILocation *>(Var.getInlinedAt())); 1287ae6f7882SJeremy Morse auto MIB = BuildMI(MF, DL, TII->get(TargetOpcode::DBG_VALUE)); 1288ae6f7882SJeremy Morse MIB.add(MO); 1289ae6f7882SJeremy Morse if (Properties.Indirect) 1290ae6f7882SJeremy Morse MIB.addImm(0); 1291ae6f7882SJeremy Morse else 1292ae6f7882SJeremy Morse MIB.addReg(0); 1293ae6f7882SJeremy Morse MIB.addMetadata(Var.getVariable()); 1294ae6f7882SJeremy Morse MIB.addMetadata(Properties.DIExpr); 1295ae6f7882SJeremy Morse return MIB; 1296ae6f7882SJeremy Morse } 1297ae6f7882SJeremy Morse }; 1298ae6f7882SJeremy Morse 1299ae6f7882SJeremy Morse class InstrRefBasedLDV : public LDVImpl { 1300ae6f7882SJeremy Morse private: 1301ae6f7882SJeremy Morse using FragmentInfo = DIExpression::FragmentInfo; 1302ae6f7882SJeremy Morse using OptFragmentInfo = Optional<DIExpression::FragmentInfo>; 1303ae6f7882SJeremy Morse 1304ae6f7882SJeremy Morse // Helper while building OverlapMap, a map of all fragments seen for a given 1305ae6f7882SJeremy Morse // DILocalVariable. 1306ae6f7882SJeremy Morse using VarToFragments = 1307ae6f7882SJeremy Morse DenseMap<const DILocalVariable *, SmallSet<FragmentInfo, 4>>; 1308ae6f7882SJeremy Morse 1309ae6f7882SJeremy Morse /// Machine location/value transfer function, a mapping of which locations 1310cca049adSDjordje Todorovic /// are assigned which new values. 1311cca049adSDjordje Todorovic using MLocTransferMap = std::map<LocIdx, ValueIDNum>; 1312ae6f7882SJeremy Morse 1313ae6f7882SJeremy Morse /// Live in/out structure for the variable values: a per-block map of 1314ae6f7882SJeremy Morse /// variables to their values. XXX, better name? 1315cca049adSDjordje Todorovic using LiveIdxT = 1316cca049adSDjordje Todorovic DenseMap<const MachineBasicBlock *, DenseMap<DebugVariable, DbgValue> *>; 1317ae6f7882SJeremy Morse 1318cca049adSDjordje Todorovic using VarAndLoc = std::pair<DebugVariable, DbgValue>; 1319ae6f7882SJeremy Morse 1320ae6f7882SJeremy Morse /// Type for a live-in value: the predecessor block, and its value. 1321cca049adSDjordje Todorovic using InValueT = std::pair<MachineBasicBlock *, DbgValue *>; 1322ae6f7882SJeremy Morse 1323ae6f7882SJeremy Morse /// Vector (per block) of a collection (inner smallvector) of live-ins. 1324ae6f7882SJeremy Morse /// Used as the result type for the variable value dataflow problem. 1325cca049adSDjordje Todorovic using LiveInsT = SmallVector<SmallVector<VarAndLoc, 8>, 8>; 1326ae6f7882SJeremy Morse 1327ae6f7882SJeremy Morse const TargetRegisterInfo *TRI; 1328ae6f7882SJeremy Morse const TargetInstrInfo *TII; 1329ae6f7882SJeremy Morse const TargetFrameLowering *TFI; 1330ae6f7882SJeremy Morse BitVector CalleeSavedRegs; 1331ae6f7882SJeremy Morse LexicalScopes LS; 1332ae6f7882SJeremy Morse TargetPassConfig *TPC; 1333ae6f7882SJeremy Morse 1334ae6f7882SJeremy Morse /// Object to track machine locations as we step through a block. Could 1335ae6f7882SJeremy Morse /// probably be a field rather than a pointer, as it's always used. 1336ae6f7882SJeremy Morse MLocTracker *MTracker; 1337ae6f7882SJeremy Morse 1338ae6f7882SJeremy Morse /// Number of the current block LiveDebugValues is stepping through. 1339ae6f7882SJeremy Morse unsigned CurBB; 1340ae6f7882SJeremy Morse 1341ae6f7882SJeremy Morse /// Number of the current instruction LiveDebugValues is evaluating. 1342ae6f7882SJeremy Morse unsigned CurInst; 1343ae6f7882SJeremy Morse 1344ae6f7882SJeremy Morse /// Variable tracker -- listens to DBG_VALUEs occurring as InstrRefBasedImpl 1345ae6f7882SJeremy Morse /// steps through a block. Reads the values at each location from the 1346ae6f7882SJeremy Morse /// MLocTracker object. 1347ae6f7882SJeremy Morse VLocTracker *VTracker; 1348ae6f7882SJeremy Morse 1349ae6f7882SJeremy Morse /// Tracker for transfers, listens to DBG_VALUEs and transfers of values 1350ae6f7882SJeremy Morse /// between locations during stepping, creates new DBG_VALUEs when values move 1351ae6f7882SJeremy Morse /// location. 1352ae6f7882SJeremy Morse TransferTracker *TTracker; 1353ae6f7882SJeremy Morse 1354ae6f7882SJeremy Morse /// Blocks which are artificial, i.e. blocks which exclusively contain 1355ae6f7882SJeremy Morse /// instructions without DebugLocs, or with line 0 locations. 1356ae6f7882SJeremy Morse SmallPtrSet<const MachineBasicBlock *, 16> ArtificialBlocks; 1357ae6f7882SJeremy Morse 1358ae6f7882SJeremy Morse // Mapping of blocks to and from their RPOT order. 1359ae6f7882SJeremy Morse DenseMap<unsigned int, MachineBasicBlock *> OrderToBB; 1360ae6f7882SJeremy Morse DenseMap<MachineBasicBlock *, unsigned int> BBToOrder; 1361ae6f7882SJeremy Morse DenseMap<unsigned, unsigned> BBNumToRPO; 1362ae6f7882SJeremy Morse 136368f47157SJeremy Morse /// Pair of MachineInstr, and its 1-based offset into the containing block. 1364cca049adSDjordje Todorovic using InstAndNum = std::pair<const MachineInstr *, unsigned>; 136568f47157SJeremy Morse /// Map from debug instruction number to the MachineInstr labelled with that 136668f47157SJeremy Morse /// number, and its location within the function. Used to transform 136768f47157SJeremy Morse /// instruction numbers in DBG_INSTR_REFs into machine value numbers. 136868f47157SJeremy Morse std::map<uint64_t, InstAndNum> DebugInstrNumToInstr; 136968f47157SJeremy Morse 1370ae6f7882SJeremy Morse // Map of overlapping variable fragments. 1371ae6f7882SJeremy Morse OverlapMap OverlapFragments; 1372ae6f7882SJeremy Morse VarToFragments SeenFragments; 1373ae6f7882SJeremy Morse 1374ae6f7882SJeremy Morse /// Tests whether this instruction is a spill to a stack slot. 1375ae6f7882SJeremy Morse bool isSpillInstruction(const MachineInstr &MI, MachineFunction *MF); 1376ae6f7882SJeremy Morse 1377ae6f7882SJeremy Morse /// Decide if @MI is a spill instruction and return true if it is. We use 2 1378ae6f7882SJeremy Morse /// criteria to make this decision: 1379ae6f7882SJeremy Morse /// - Is this instruction a store to a spill slot? 1380ae6f7882SJeremy Morse /// - Is there a register operand that is both used and killed? 1381ae6f7882SJeremy Morse /// TODO: Store optimization can fold spills into other stores (including 1382ae6f7882SJeremy Morse /// other spills). We do not handle this yet (more than one memory operand). 1383ae6f7882SJeremy Morse bool isLocationSpill(const MachineInstr &MI, MachineFunction *MF, 1384ae6f7882SJeremy Morse unsigned &Reg); 1385ae6f7882SJeremy Morse 1386ae6f7882SJeremy Morse /// If a given instruction is identified as a spill, return the spill slot 1387ae6f7882SJeremy Morse /// and set \p Reg to the spilled register. 1388ae6f7882SJeremy Morse Optional<SpillLoc> isRestoreInstruction(const MachineInstr &MI, 1389ae6f7882SJeremy Morse MachineFunction *MF, unsigned &Reg); 1390ae6f7882SJeremy Morse 1391ae6f7882SJeremy Morse /// Given a spill instruction, extract the register and offset used to 1392ae6f7882SJeremy Morse /// address the spill slot in a target independent way. 1393ae6f7882SJeremy Morse SpillLoc extractSpillBaseRegAndOffset(const MachineInstr &MI); 1394ae6f7882SJeremy Morse 1395ae6f7882SJeremy Morse /// Observe a single instruction while stepping through a block. 1396ae6f7882SJeremy Morse void process(MachineInstr &MI); 1397ae6f7882SJeremy Morse 1398ae6f7882SJeremy Morse /// Examines whether \p MI is a DBG_VALUE and notifies trackers. 1399ae6f7882SJeremy Morse /// \returns true if MI was recognized and processed. 1400ae6f7882SJeremy Morse bool transferDebugValue(const MachineInstr &MI); 1401ae6f7882SJeremy Morse 140268f47157SJeremy Morse /// Examines whether \p MI is a DBG_INSTR_REF and notifies trackers. 140368f47157SJeremy Morse /// \returns true if MI was recognized and processed. 140468f47157SJeremy Morse bool transferDebugInstrRef(MachineInstr &MI); 140568f47157SJeremy Morse 1406ae6f7882SJeremy Morse /// Examines whether \p MI is copy instruction, and notifies trackers. 1407ae6f7882SJeremy Morse /// \returns true if MI was recognized and processed. 1408ae6f7882SJeremy Morse bool transferRegisterCopy(MachineInstr &MI); 1409ae6f7882SJeremy Morse 1410ae6f7882SJeremy Morse /// Examines whether \p MI is stack spill or restore instruction, and 1411ae6f7882SJeremy Morse /// notifies trackers. \returns true if MI was recognized and processed. 1412ae6f7882SJeremy Morse bool transferSpillOrRestoreInst(MachineInstr &MI); 1413ae6f7882SJeremy Morse 1414ae6f7882SJeremy Morse /// Examines \p MI for any registers that it defines, and notifies trackers. 1415ae6f7882SJeremy Morse void transferRegisterDef(MachineInstr &MI); 1416ae6f7882SJeremy Morse 1417ae6f7882SJeremy Morse /// Copy one location to the other, accounting for movement of subregisters 1418ae6f7882SJeremy Morse /// too. 1419ae6f7882SJeremy Morse void performCopy(Register Src, Register Dst); 1420ae6f7882SJeremy Morse 1421ae6f7882SJeremy Morse void accumulateFragmentMap(MachineInstr &MI); 1422ae6f7882SJeremy Morse 1423ae6f7882SJeremy Morse /// Step through the function, recording register definitions and movements 1424ae6f7882SJeremy Morse /// in an MLocTracker. Convert the observations into a per-block transfer 1425ae6f7882SJeremy Morse /// function in \p MLocTransfer, suitable for using with the machine value 1426ab93e710SJeremy Morse /// location dataflow problem. 1427ab93e710SJeremy Morse void 1428ab93e710SJeremy Morse produceMLocTransferFunction(MachineFunction &MF, 1429ae6f7882SJeremy Morse SmallVectorImpl<MLocTransferMap> &MLocTransfer, 1430ab93e710SJeremy Morse unsigned MaxNumBlocks); 1431ae6f7882SJeremy Morse 1432ae6f7882SJeremy Morse /// Solve the machine value location dataflow problem. Takes as input the 1433ae6f7882SJeremy Morse /// transfer functions in \p MLocTransfer. Writes the output live-in and 1434ae6f7882SJeremy Morse /// live-out arrays to the (initialized to zero) multidimensional arrays in 1435ae6f7882SJeremy Morse /// \p MInLocs and \p MOutLocs. The outer dimension is indexed by block 1436ae6f7882SJeremy Morse /// number, the inner by LocIdx. 1437ae6f7882SJeremy Morse void mlocDataflow(ValueIDNum **MInLocs, ValueIDNum **MOutLocs, 1438ae6f7882SJeremy Morse SmallVectorImpl<MLocTransferMap> &MLocTransfer); 1439ae6f7882SJeremy Morse 1440ae6f7882SJeremy Morse /// Perform a control flow join (lattice value meet) of the values in machine 1441ae6f7882SJeremy Morse /// locations at \p MBB. Follows the algorithm described in the file-comment, 1442ae6f7882SJeremy Morse /// reading live-outs of predecessors from \p OutLocs, the current live ins 1443ae6f7882SJeremy Morse /// from \p InLocs, and assigning the newly computed live ins back into 1444ae6f7882SJeremy Morse /// \p InLocs. \returns two bools -- the first indicates whether a change 1445ae6f7882SJeremy Morse /// was made, the second whether a lattice downgrade occurred. If the latter 1446ae6f7882SJeremy Morse /// is true, revisiting this block is necessary. 1447ae6f7882SJeremy Morse std::tuple<bool, bool> 1448ae6f7882SJeremy Morse mlocJoin(MachineBasicBlock &MBB, 1449ae6f7882SJeremy Morse SmallPtrSet<const MachineBasicBlock *, 16> &Visited, 1450ae6f7882SJeremy Morse ValueIDNum **OutLocs, ValueIDNum *InLocs); 1451ae6f7882SJeremy Morse 1452ae6f7882SJeremy Morse /// Solve the variable value dataflow problem, for a single lexical scope. 1453ae6f7882SJeremy Morse /// Uses the algorithm from the file comment to resolve control flow joins, 1454ae6f7882SJeremy Morse /// although there are extra hacks, see vlocJoin. Reads the 1455ae6f7882SJeremy Morse /// locations of values from the \p MInLocs and \p MOutLocs arrays (see 1456ae6f7882SJeremy Morse /// mlocDataflow) and reads the variable values transfer function from 1457ae6f7882SJeremy Morse /// \p AllTheVlocs. Live-in and Live-out variable values are stored locally, 1458ae6f7882SJeremy Morse /// with the live-ins permanently stored to \p Output once the fixedpoint is 1459ae6f7882SJeremy Morse /// reached. 1460ae6f7882SJeremy Morse /// \p VarsWeCareAbout contains a collection of the variables in \p Scope 1461ae6f7882SJeremy Morse /// that we should be tracking. 1462ae6f7882SJeremy Morse /// \p AssignBlocks contains the set of blocks that aren't in \p Scope, but 1463ae6f7882SJeremy Morse /// which do contain DBG_VALUEs, which VarLocBasedImpl tracks locations 1464ae6f7882SJeremy Morse /// through. 1465ae6f7882SJeremy Morse void vlocDataflow(const LexicalScope *Scope, const DILocation *DILoc, 1466ae6f7882SJeremy Morse const SmallSet<DebugVariable, 4> &VarsWeCareAbout, 1467ae6f7882SJeremy Morse SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks, 1468ae6f7882SJeremy Morse LiveInsT &Output, ValueIDNum **MOutLocs, 1469ae6f7882SJeremy Morse ValueIDNum **MInLocs, 1470ae6f7882SJeremy Morse SmallVectorImpl<VLocTracker> &AllTheVLocs); 1471ae6f7882SJeremy Morse 1472ae6f7882SJeremy Morse /// Compute the live-ins to a block, considering control flow merges according 1473ae6f7882SJeremy Morse /// to the method in the file comment. Live out and live in variable values 1474ae6f7882SJeremy Morse /// are stored in \p VLOCOutLocs and \p VLOCInLocs. The live-ins for \p MBB 1475ae6f7882SJeremy Morse /// are computed and stored into \p VLOCInLocs. \returns true if the live-ins 1476ae6f7882SJeremy Morse /// are modified. 1477ae6f7882SJeremy Morse /// \p InLocsT Output argument, storage for calculated live-ins. 1478ae6f7882SJeremy Morse /// \returns two bools -- the first indicates whether a change 1479ae6f7882SJeremy Morse /// was made, the second whether a lattice downgrade occurred. If the latter 1480ae6f7882SJeremy Morse /// is true, revisiting this block is necessary. 1481ae6f7882SJeremy Morse std::tuple<bool, bool> 1482ae6f7882SJeremy Morse vlocJoin(MachineBasicBlock &MBB, LiveIdxT &VLOCOutLocs, LiveIdxT &VLOCInLocs, 1483ae6f7882SJeremy Morse SmallPtrSet<const MachineBasicBlock *, 16> *VLOCVisited, 1484ae6f7882SJeremy Morse unsigned BBNum, const SmallSet<DebugVariable, 4> &AllVars, 1485ae6f7882SJeremy Morse ValueIDNum **MOutLocs, ValueIDNum **MInLocs, 1486ae6f7882SJeremy Morse SmallPtrSet<const MachineBasicBlock *, 8> &InScopeBlocks, 1487ae6f7882SJeremy Morse SmallPtrSet<const MachineBasicBlock *, 8> &BlocksToExplore, 1488ae6f7882SJeremy Morse DenseMap<DebugVariable, DbgValue> &InLocsT); 1489ae6f7882SJeremy Morse 1490ae6f7882SJeremy Morse /// Continue exploration of the variable-value lattice, as explained in the 1491ae6f7882SJeremy Morse /// file-level comment. \p OldLiveInLocation contains the current 1492ae6f7882SJeremy Morse /// exploration position, from which we need to descend further. \p Values 1493ae6f7882SJeremy Morse /// contains the set of live-in values, \p CurBlockRPONum the RPO number of 1494ae6f7882SJeremy Morse /// the current block, and \p CandidateLocations a set of locations that 1495ae6f7882SJeremy Morse /// should be considered as PHI locations, if we reach the bottom of the 1496ae6f7882SJeremy Morse /// lattice. \returns true if we should downgrade; the value is the agreeing 1497ae6f7882SJeremy Morse /// value number in a non-backedge predecessor. 1498ae6f7882SJeremy Morse bool vlocDowngradeLattice(const MachineBasicBlock &MBB, 1499ae6f7882SJeremy Morse const DbgValue &OldLiveInLocation, 1500ae6f7882SJeremy Morse const SmallVectorImpl<InValueT> &Values, 1501ae6f7882SJeremy Morse unsigned CurBlockRPONum); 1502ae6f7882SJeremy Morse 1503ae6f7882SJeremy Morse /// For the given block and live-outs feeding into it, try to find a 1504ae6f7882SJeremy Morse /// machine location where they all join. If a solution for all predecessors 1505ae6f7882SJeremy Morse /// can't be found, a location where all non-backedge-predecessors join 1506ae6f7882SJeremy Morse /// will be returned instead. While this method finds a join location, this 1507ae6f7882SJeremy Morse /// says nothing as to whether it should be used. 1508ae6f7882SJeremy Morse /// \returns Pair of value ID if found, and true when the correct value 1509ae6f7882SJeremy Morse /// is available on all predecessor edges, or false if it's only available 1510ae6f7882SJeremy Morse /// for non-backedge predecessors. 1511ae6f7882SJeremy Morse std::tuple<Optional<ValueIDNum>, bool> 1512ae6f7882SJeremy Morse pickVPHILoc(MachineBasicBlock &MBB, const DebugVariable &Var, 1513ae6f7882SJeremy Morse const LiveIdxT &LiveOuts, ValueIDNum **MOutLocs, 1514ae6f7882SJeremy Morse ValueIDNum **MInLocs, 1515ae6f7882SJeremy Morse const SmallVectorImpl<MachineBasicBlock *> &BlockOrders); 1516ae6f7882SJeremy Morse 1517ae6f7882SJeremy Morse /// Given the solutions to the two dataflow problems, machine value locations 1518ae6f7882SJeremy Morse /// in \p MInLocs and live-in variable values in \p SavedLiveIns, runs the 1519ae6f7882SJeremy Morse /// TransferTracker class over the function to produce live-in and transfer 1520ae6f7882SJeremy Morse /// DBG_VALUEs, then inserts them. Groups of DBG_VALUEs are inserted in the 1521ae6f7882SJeremy Morse /// order given by AllVarsNumbering -- this could be any stable order, but 1522ae6f7882SJeremy Morse /// right now "order of appearence in function, when explored in RPO", so 1523ae6f7882SJeremy Morse /// that we can compare explictly against VarLocBasedImpl. 1524ae6f7882SJeremy Morse void emitLocations(MachineFunction &MF, LiveInsT SavedLiveIns, 1525ae6f7882SJeremy Morse ValueIDNum **MInLocs, 1526ae6f7882SJeremy Morse DenseMap<DebugVariable, unsigned> &AllVarsNumbering); 1527ae6f7882SJeremy Morse 1528ae6f7882SJeremy Morse /// Boilerplate computation of some initial sets, artifical blocks and 1529ae6f7882SJeremy Morse /// RPOT block ordering. 1530ae6f7882SJeremy Morse void initialSetup(MachineFunction &MF); 1531ae6f7882SJeremy Morse 1532ae6f7882SJeremy Morse bool ExtendRanges(MachineFunction &MF, TargetPassConfig *TPC) override; 1533ae6f7882SJeremy Morse 1534ae6f7882SJeremy Morse public: 1535ae6f7882SJeremy Morse /// Default construct and initialize the pass. 1536ae6f7882SJeremy Morse InstrRefBasedLDV(); 1537ae6f7882SJeremy Morse 1538ae6f7882SJeremy Morse LLVM_DUMP_METHOD 1539ae6f7882SJeremy Morse void dump_mloc_transfer(const MLocTransferMap &mloc_transfer) const; 1540ae6f7882SJeremy Morse 1541ae6f7882SJeremy Morse bool isCalleeSaved(LocIdx L) { 1542ae6f7882SJeremy Morse unsigned Reg = MTracker->LocIdxToLocID[L]; 1543ae6f7882SJeremy Morse for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI) 1544ae6f7882SJeremy Morse if (CalleeSavedRegs.test(*RAI)) 1545ae6f7882SJeremy Morse return true; 1546ae6f7882SJeremy Morse return false; 1547ae6f7882SJeremy Morse } 1548ae6f7882SJeremy Morse }; 1549ae6f7882SJeremy Morse 1550ae6f7882SJeremy Morse } // end anonymous namespace 1551ae6f7882SJeremy Morse 1552ae6f7882SJeremy Morse //===----------------------------------------------------------------------===// 1553ae6f7882SJeremy Morse // Implementation 1554ae6f7882SJeremy Morse //===----------------------------------------------------------------------===// 1555ae6f7882SJeremy Morse 1556ae6f7882SJeremy Morse ValueIDNum ValueIDNum::EmptyValue = {UINT_MAX, UINT_MAX, UINT_MAX}; 1557ae6f7882SJeremy Morse 1558ae6f7882SJeremy Morse /// Default construct and initialize the pass. 1559ae6f7882SJeremy Morse InstrRefBasedLDV::InstrRefBasedLDV() {} 1560ae6f7882SJeremy Morse 1561ae6f7882SJeremy Morse //===----------------------------------------------------------------------===// 1562ae6f7882SJeremy Morse // Debug Range Extension Implementation 1563ae6f7882SJeremy Morse //===----------------------------------------------------------------------===// 1564ae6f7882SJeremy Morse 1565ae6f7882SJeremy Morse #ifndef NDEBUG 1566ae6f7882SJeremy Morse // Something to restore in the future. 1567ae6f7882SJeremy Morse // void InstrRefBasedLDV::printVarLocInMBB(..) 1568ae6f7882SJeremy Morse #endif 1569ae6f7882SJeremy Morse 1570ae6f7882SJeremy Morse SpillLoc 1571ae6f7882SJeremy Morse InstrRefBasedLDV::extractSpillBaseRegAndOffset(const MachineInstr &MI) { 1572ae6f7882SJeremy Morse assert(MI.hasOneMemOperand() && 1573ae6f7882SJeremy Morse "Spill instruction does not have exactly one memory operand?"); 1574ae6f7882SJeremy Morse auto MMOI = MI.memoperands_begin(); 1575ae6f7882SJeremy Morse const PseudoSourceValue *PVal = (*MMOI)->getPseudoValue(); 1576ae6f7882SJeremy Morse assert(PVal->kind() == PseudoSourceValue::FixedStack && 1577ae6f7882SJeremy Morse "Inconsistent memory operand in spill instruction"); 1578ae6f7882SJeremy Morse int FI = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex(); 1579ae6f7882SJeremy Morse const MachineBasicBlock *MBB = MI.getParent(); 1580ae6f7882SJeremy Morse Register Reg; 1581d57bba7cSSander de Smalen StackOffset Offset = TFI->getFrameIndexReference(*MBB->getParent(), FI, Reg); 1582d57bba7cSSander de Smalen assert(!Offset.getScalable() && 1583d57bba7cSSander de Smalen "Frame offsets with a scalable component are not supported"); 1584d57bba7cSSander de Smalen return {Reg, static_cast<int>(Offset.getFixed())}; 1585ae6f7882SJeremy Morse } 1586ae6f7882SJeremy Morse 1587ae6f7882SJeremy Morse /// End all previous ranges related to @MI and start a new range from @MI 1588ae6f7882SJeremy Morse /// if it is a DBG_VALUE instr. 1589ae6f7882SJeremy Morse bool InstrRefBasedLDV::transferDebugValue(const MachineInstr &MI) { 1590ae6f7882SJeremy Morse if (!MI.isDebugValue()) 1591ae6f7882SJeremy Morse return false; 1592ae6f7882SJeremy Morse 1593ae6f7882SJeremy Morse const DILocalVariable *Var = MI.getDebugVariable(); 1594ae6f7882SJeremy Morse const DIExpression *Expr = MI.getDebugExpression(); 1595ae6f7882SJeremy Morse const DILocation *DebugLoc = MI.getDebugLoc(); 1596ae6f7882SJeremy Morse const DILocation *InlinedAt = DebugLoc->getInlinedAt(); 1597ae6f7882SJeremy Morse assert(Var->isValidLocationForIntrinsic(DebugLoc) && 1598ae6f7882SJeremy Morse "Expected inlined-at fields to agree"); 1599ae6f7882SJeremy Morse 1600ae6f7882SJeremy Morse DebugVariable V(Var, Expr, InlinedAt); 160168f47157SJeremy Morse DbgValueProperties Properties(MI); 1602ae6f7882SJeremy Morse 1603ae6f7882SJeremy Morse // If there are no instructions in this lexical scope, do no location tracking 1604ae6f7882SJeremy Morse // at all, this variable shouldn't get a legitimate location range. 1605ae6f7882SJeremy Morse auto *Scope = LS.findLexicalScope(MI.getDebugLoc().get()); 1606ae6f7882SJeremy Morse if (Scope == nullptr) 1607ae6f7882SJeremy Morse return true; // handled it; by doing nothing 1608ae6f7882SJeremy Morse 1609ae6f7882SJeremy Morse const MachineOperand &MO = MI.getOperand(0); 1610ae6f7882SJeremy Morse 1611ae6f7882SJeremy Morse // MLocTracker needs to know that this register is read, even if it's only 1612ae6f7882SJeremy Morse // read by a debug inst. 1613ae6f7882SJeremy Morse if (MO.isReg() && MO.getReg() != 0) 1614ae6f7882SJeremy Morse (void)MTracker->readReg(MO.getReg()); 1615ae6f7882SJeremy Morse 1616ae6f7882SJeremy Morse // If we're preparing for the second analysis (variables), the machine value 1617ae6f7882SJeremy Morse // locations are already solved, and we report this DBG_VALUE and the value 1618ae6f7882SJeremy Morse // it refers to to VLocTracker. 1619ae6f7882SJeremy Morse if (VTracker) { 1620ae6f7882SJeremy Morse if (MO.isReg()) { 1621ae6f7882SJeremy Morse // Feed defVar the new variable location, or if this is a 1622ae6f7882SJeremy Morse // DBG_VALUE $noreg, feed defVar None. 1623ae6f7882SJeremy Morse if (MO.getReg()) 162468f47157SJeremy Morse VTracker->defVar(MI, Properties, MTracker->readReg(MO.getReg())); 1625ae6f7882SJeremy Morse else 162668f47157SJeremy Morse VTracker->defVar(MI, Properties, None); 1627ae6f7882SJeremy Morse } else if (MI.getOperand(0).isImm() || MI.getOperand(0).isFPImm() || 1628ae6f7882SJeremy Morse MI.getOperand(0).isCImm()) { 1629ae6f7882SJeremy Morse VTracker->defVar(MI, MI.getOperand(0)); 1630ae6f7882SJeremy Morse } 1631ae6f7882SJeremy Morse } 1632ae6f7882SJeremy Morse 1633ae6f7882SJeremy Morse // If performing final tracking of transfers, report this variable definition 1634ae6f7882SJeremy Morse // to the TransferTracker too. 1635ae6f7882SJeremy Morse if (TTracker) 1636ae6f7882SJeremy Morse TTracker->redefVar(MI); 1637ae6f7882SJeremy Morse return true; 1638ae6f7882SJeremy Morse } 1639ae6f7882SJeremy Morse 164068f47157SJeremy Morse bool InstrRefBasedLDV::transferDebugInstrRef(MachineInstr &MI) { 164168f47157SJeremy Morse if (!MI.isDebugRef()) 164268f47157SJeremy Morse return false; 164368f47157SJeremy Morse 164468f47157SJeremy Morse // Only handle this instruction when we are building the variable value 164568f47157SJeremy Morse // transfer function. 164668f47157SJeremy Morse if (!VTracker) 164768f47157SJeremy Morse return false; 164868f47157SJeremy Morse 164968f47157SJeremy Morse unsigned InstNo = MI.getOperand(0).getImm(); 165068f47157SJeremy Morse unsigned OpNo = MI.getOperand(1).getImm(); 165168f47157SJeremy Morse 165268f47157SJeremy Morse const DILocalVariable *Var = MI.getDebugVariable(); 165368f47157SJeremy Morse const DIExpression *Expr = MI.getDebugExpression(); 165468f47157SJeremy Morse const DILocation *DebugLoc = MI.getDebugLoc(); 165568f47157SJeremy Morse const DILocation *InlinedAt = DebugLoc->getInlinedAt(); 165668f47157SJeremy Morse assert(Var->isValidLocationForIntrinsic(DebugLoc) && 165768f47157SJeremy Morse "Expected inlined-at fields to agree"); 165868f47157SJeremy Morse 165968f47157SJeremy Morse DebugVariable V(Var, Expr, InlinedAt); 166068f47157SJeremy Morse 166168f47157SJeremy Morse auto *Scope = LS.findLexicalScope(MI.getDebugLoc().get()); 166268f47157SJeremy Morse if (Scope == nullptr) 166368f47157SJeremy Morse return true; // Handled by doing nothing. This variable is never in scope. 166468f47157SJeremy Morse 166568f47157SJeremy Morse const MachineFunction &MF = *MI.getParent()->getParent(); 166668f47157SJeremy Morse 166768f47157SJeremy Morse // Various optimizations may have happened to the value during codegen, 166868f47157SJeremy Morse // recorded in the value substitution table. Apply any substitutions to 166968f47157SJeremy Morse // the instruction / operand number in this DBG_INSTR_REF. 167068f47157SJeremy Morse auto Sub = MF.DebugValueSubstitutions.find(std::make_pair(InstNo, OpNo)); 167168f47157SJeremy Morse while (Sub != MF.DebugValueSubstitutions.end()) { 167268f47157SJeremy Morse InstNo = Sub->second.first; 167368f47157SJeremy Morse OpNo = Sub->second.second; 167468f47157SJeremy Morse Sub = MF.DebugValueSubstitutions.find(std::make_pair(InstNo, OpNo)); 167568f47157SJeremy Morse } 167668f47157SJeremy Morse 167768f47157SJeremy Morse // Default machine value number is <None> -- if no instruction defines 167868f47157SJeremy Morse // the corresponding value, it must have been optimized out. 167968f47157SJeremy Morse Optional<ValueIDNum> NewID = None; 168068f47157SJeremy Morse 168168f47157SJeremy Morse // Try to lookup the instruction number, and find the machine value number 168268f47157SJeremy Morse // that it defines. 168368f47157SJeremy Morse auto InstrIt = DebugInstrNumToInstr.find(InstNo); 168468f47157SJeremy Morse if (InstrIt != DebugInstrNumToInstr.end()) { 168568f47157SJeremy Morse const MachineInstr &TargetInstr = *InstrIt->second.first; 168668f47157SJeremy Morse uint64_t BlockNo = TargetInstr.getParent()->getNumber(); 168768f47157SJeremy Morse 168868f47157SJeremy Morse // Pick out the designated operand. 168968f47157SJeremy Morse assert(OpNo < TargetInstr.getNumOperands()); 169068f47157SJeremy Morse const MachineOperand &MO = TargetInstr.getOperand(OpNo); 169168f47157SJeremy Morse 169268f47157SJeremy Morse // Today, this can only be a register. 169368f47157SJeremy Morse assert(MO.isReg() && MO.isDef()); 169468f47157SJeremy Morse 169568f47157SJeremy Morse unsigned LocID = MTracker->getLocID(MO.getReg(), false); 169668f47157SJeremy Morse LocIdx L = MTracker->LocIDToLocIdx[LocID]; 169768f47157SJeremy Morse NewID = ValueIDNum(BlockNo, InstrIt->second.second, L); 169868f47157SJeremy Morse } 169968f47157SJeremy Morse 170068f47157SJeremy Morse // We, we have a value number or None. Tell the variable value tracker about 170168f47157SJeremy Morse // it. The rest of this LiveDebugValues implementation acts exactly the same 170268f47157SJeremy Morse // for DBG_INSTR_REFs as DBG_VALUEs (just, the former can refer to values that 170368f47157SJeremy Morse // aren't immediately available). 170468f47157SJeremy Morse DbgValueProperties Properties(Expr, false); 170568f47157SJeremy Morse VTracker->defVar(MI, Properties, NewID); 170668f47157SJeremy Morse 170768f47157SJeremy Morse // If we're on the final pass through the function, decompose this INSTR_REF 170868f47157SJeremy Morse // into a plain DBG_VALUE. 170968f47157SJeremy Morse if (!TTracker) 171068f47157SJeremy Morse return true; 171168f47157SJeremy Morse 171268f47157SJeremy Morse // Pick a location for the machine value number, if such a location exists. 171368f47157SJeremy Morse // (This information could be stored in TransferTracker to make it faster). 171468f47157SJeremy Morse Optional<LocIdx> FoundLoc = None; 171568f47157SJeremy Morse for (auto Location : MTracker->locations()) { 171668f47157SJeremy Morse LocIdx CurL = Location.Idx; 171768f47157SJeremy Morse ValueIDNum ID = MTracker->LocIdxToIDNum[CurL]; 171868f47157SJeremy Morse if (NewID && ID == NewID) { 171968f47157SJeremy Morse // If this is the first location with that value, pick it. Otherwise, 172068f47157SJeremy Morse // consider whether it's a "longer term" location. 172168f47157SJeremy Morse if (!FoundLoc) { 172268f47157SJeremy Morse FoundLoc = CurL; 172368f47157SJeremy Morse continue; 172468f47157SJeremy Morse } 172568f47157SJeremy Morse 172668f47157SJeremy Morse if (MTracker->isSpill(CurL)) 172768f47157SJeremy Morse FoundLoc = CurL; // Spills are a longer term location. 172868f47157SJeremy Morse else if (!MTracker->isSpill(*FoundLoc) && 172968f47157SJeremy Morse !MTracker->isSpill(CurL) && 173068f47157SJeremy Morse !isCalleeSaved(*FoundLoc) && 173168f47157SJeremy Morse isCalleeSaved(CurL)) 173268f47157SJeremy Morse FoundLoc = CurL; // Callee saved regs are longer term than normal. 173368f47157SJeremy Morse } 173468f47157SJeremy Morse } 173568f47157SJeremy Morse 173668f47157SJeremy Morse // Tell transfer tracker that the variable value has changed. 173768f47157SJeremy Morse TTracker->redefVar(MI, Properties, FoundLoc); 173868f47157SJeremy Morse 1739b1b2c6abSJeremy Morse // If there was a value with no location; but the value is defined in a 1740b1b2c6abSJeremy Morse // later instruction in this block, this is a block-local use-before-def. 1741b1b2c6abSJeremy Morse if (!FoundLoc && NewID && NewID->getBlock() == CurBB && 1742b1b2c6abSJeremy Morse NewID->getInst() > CurInst) 1743b1b2c6abSJeremy Morse TTracker->addUseBeforeDef(V, {MI.getDebugExpression(), false}, *NewID); 1744b1b2c6abSJeremy Morse 174568f47157SJeremy Morse // Produce a DBG_VALUE representing what this DBG_INSTR_REF meant. 174668f47157SJeremy Morse // This DBG_VALUE is potentially a $noreg / undefined location, if 174768f47157SJeremy Morse // FoundLoc is None. 174868f47157SJeremy Morse // (XXX -- could morph the DBG_INSTR_REF in the future). 174968f47157SJeremy Morse MachineInstr *DbgMI = MTracker->emitLoc(FoundLoc, V, Properties); 175068f47157SJeremy Morse TTracker->PendingDbgValues.push_back(DbgMI); 175168f47157SJeremy Morse TTracker->flushDbgValues(MI.getIterator(), nullptr); 175268f47157SJeremy Morse 175368f47157SJeremy Morse return true; 175468f47157SJeremy Morse } 175568f47157SJeremy Morse 1756ae6f7882SJeremy Morse void InstrRefBasedLDV::transferRegisterDef(MachineInstr &MI) { 1757ae6f7882SJeremy Morse // Meta Instructions do not affect the debug liveness of any register they 1758ae6f7882SJeremy Morse // define. 1759ae6f7882SJeremy Morse if (MI.isImplicitDef()) { 1760ae6f7882SJeremy Morse // Except when there's an implicit def, and the location it's defining has 1761ae6f7882SJeremy Morse // no value number. The whole point of an implicit def is to announce that 1762ae6f7882SJeremy Morse // the register is live, without be specific about it's value. So define 1763ae6f7882SJeremy Morse // a value if there isn't one already. 1764ae6f7882SJeremy Morse ValueIDNum Num = MTracker->readReg(MI.getOperand(0).getReg()); 1765ae6f7882SJeremy Morse // Has a legitimate value -> ignore the implicit def. 1766ae6f7882SJeremy Morse if (Num.getLoc() != 0) 1767ae6f7882SJeremy Morse return; 1768ae6f7882SJeremy Morse // Otherwise, def it here. 1769ae6f7882SJeremy Morse } else if (MI.isMetaInstruction()) 1770ae6f7882SJeremy Morse return; 1771ae6f7882SJeremy Morse 1772ae6f7882SJeremy Morse MachineFunction *MF = MI.getMF(); 1773ae6f7882SJeremy Morse const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); 17744634ad6cSGaurav Jain Register SP = TLI->getStackPointerRegisterToSaveRestore(); 1775ae6f7882SJeremy Morse 1776ae6f7882SJeremy Morse // Find the regs killed by MI, and find regmasks of preserved regs. 1777ae6f7882SJeremy Morse // Max out the number of statically allocated elements in `DeadRegs`, as this 1778ae6f7882SJeremy Morse // prevents fallback to std::set::count() operations. 1779ae6f7882SJeremy Morse SmallSet<uint32_t, 32> DeadRegs; 1780ae6f7882SJeremy Morse SmallVector<const uint32_t *, 4> RegMasks; 1781ae6f7882SJeremy Morse SmallVector<const MachineOperand *, 4> RegMaskPtrs; 1782ae6f7882SJeremy Morse for (const MachineOperand &MO : MI.operands()) { 1783ae6f7882SJeremy Morse // Determine whether the operand is a register def. 1784ae6f7882SJeremy Morse if (MO.isReg() && MO.isDef() && MO.getReg() && 1785ae6f7882SJeremy Morse Register::isPhysicalRegister(MO.getReg()) && 1786ae6f7882SJeremy Morse !(MI.isCall() && MO.getReg() == SP)) { 1787ae6f7882SJeremy Morse // Remove ranges of all aliased registers. 1788ae6f7882SJeremy Morse for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI) 1789ae6f7882SJeremy Morse // FIXME: Can we break out of this loop early if no insertion occurs? 1790ae6f7882SJeremy Morse DeadRegs.insert(*RAI); 1791ae6f7882SJeremy Morse } else if (MO.isRegMask()) { 1792ae6f7882SJeremy Morse RegMasks.push_back(MO.getRegMask()); 1793ae6f7882SJeremy Morse RegMaskPtrs.push_back(&MO); 1794ae6f7882SJeremy Morse } 1795ae6f7882SJeremy Morse } 1796ae6f7882SJeremy Morse 1797ae6f7882SJeremy Morse // Tell MLocTracker about all definitions, of regmasks and otherwise. 1798ae6f7882SJeremy Morse for (uint32_t DeadReg : DeadRegs) 1799ae6f7882SJeremy Morse MTracker->defReg(DeadReg, CurBB, CurInst); 1800ae6f7882SJeremy Morse 1801ae6f7882SJeremy Morse for (auto *MO : RegMaskPtrs) 1802ae6f7882SJeremy Morse MTracker->writeRegMask(MO, CurBB, CurInst); 1803ae6f7882SJeremy Morse } 1804ae6f7882SJeremy Morse 1805ae6f7882SJeremy Morse void InstrRefBasedLDV::performCopy(Register SrcRegNum, Register DstRegNum) { 1806ae6f7882SJeremy Morse ValueIDNum SrcValue = MTracker->readReg(SrcRegNum); 1807ae6f7882SJeremy Morse 1808ae6f7882SJeremy Morse MTracker->setReg(DstRegNum, SrcValue); 1809ae6f7882SJeremy Morse 1810ae6f7882SJeremy Morse // In all circumstances, re-def the super registers. It's definitely a new 1811ae6f7882SJeremy Morse // value now. This doesn't uniquely identify the composition of subregs, for 1812ae6f7882SJeremy Morse // example, two identical values in subregisters composed in different 1813ae6f7882SJeremy Morse // places would not get equal value numbers. 1814ae6f7882SJeremy Morse for (MCSuperRegIterator SRI(DstRegNum, TRI); SRI.isValid(); ++SRI) 1815ae6f7882SJeremy Morse MTracker->defReg(*SRI, CurBB, CurInst); 1816ae6f7882SJeremy Morse 1817ae6f7882SJeremy Morse // If we're emulating VarLocBasedImpl, just define all the subregisters. 1818ae6f7882SJeremy Morse // DBG_VALUEs of them will expect to be tracked from the DBG_VALUE, not 1819ae6f7882SJeremy Morse // through prior copies. 1820ae6f7882SJeremy Morse if (EmulateOldLDV) { 1821ae6f7882SJeremy Morse for (MCSubRegIndexIterator DRI(DstRegNum, TRI); DRI.isValid(); ++DRI) 1822ae6f7882SJeremy Morse MTracker->defReg(DRI.getSubReg(), CurBB, CurInst); 1823ae6f7882SJeremy Morse return; 1824ae6f7882SJeremy Morse } 1825ae6f7882SJeremy Morse 1826ae6f7882SJeremy Morse // Otherwise, actually copy subregisters from one location to another. 1827ae6f7882SJeremy Morse // XXX: in addition, any subregisters of DstRegNum that don't line up with 1828ae6f7882SJeremy Morse // the source register should be def'd. 1829ae6f7882SJeremy Morse for (MCSubRegIndexIterator SRI(SrcRegNum, TRI); SRI.isValid(); ++SRI) { 1830ae6f7882SJeremy Morse unsigned SrcSubReg = SRI.getSubReg(); 1831ae6f7882SJeremy Morse unsigned SubRegIdx = SRI.getSubRegIndex(); 1832ae6f7882SJeremy Morse unsigned DstSubReg = TRI->getSubReg(DstRegNum, SubRegIdx); 1833ae6f7882SJeremy Morse if (!DstSubReg) 1834ae6f7882SJeremy Morse continue; 1835ae6f7882SJeremy Morse 1836ae6f7882SJeremy Morse // Do copy. There are two matching subregisters, the source value should 1837ae6f7882SJeremy Morse // have been def'd when the super-reg was, the latter might not be tracked 1838ae6f7882SJeremy Morse // yet. 1839ae6f7882SJeremy Morse // This will force SrcSubReg to be tracked, if it isn't yet. 1840ae6f7882SJeremy Morse (void)MTracker->readReg(SrcSubReg); 1841ae6f7882SJeremy Morse LocIdx SrcL = MTracker->getRegMLoc(SrcSubReg); 1842ae6f7882SJeremy Morse assert(SrcL.asU64()); 1843ae6f7882SJeremy Morse (void)MTracker->readReg(DstSubReg); 1844ae6f7882SJeremy Morse LocIdx DstL = MTracker->getRegMLoc(DstSubReg); 1845ae6f7882SJeremy Morse assert(DstL.asU64()); 1846ae6f7882SJeremy Morse (void)DstL; 1847ae6f7882SJeremy Morse ValueIDNum CpyValue = {SrcValue.getBlock(), SrcValue.getInst(), SrcL}; 1848ae6f7882SJeremy Morse 1849ae6f7882SJeremy Morse MTracker->setReg(DstSubReg, CpyValue); 1850ae6f7882SJeremy Morse } 1851ae6f7882SJeremy Morse } 1852ae6f7882SJeremy Morse 1853ae6f7882SJeremy Morse bool InstrRefBasedLDV::isSpillInstruction(const MachineInstr &MI, 1854ae6f7882SJeremy Morse MachineFunction *MF) { 1855ae6f7882SJeremy Morse // TODO: Handle multiple stores folded into one. 1856ae6f7882SJeremy Morse if (!MI.hasOneMemOperand()) 1857ae6f7882SJeremy Morse return false; 1858ae6f7882SJeremy Morse 1859ae6f7882SJeremy Morse if (!MI.getSpillSize(TII) && !MI.getFoldedSpillSize(TII)) 1860ae6f7882SJeremy Morse return false; // This is not a spill instruction, since no valid size was 1861ae6f7882SJeremy Morse // returned from either function. 1862ae6f7882SJeremy Morse 1863ae6f7882SJeremy Morse return true; 1864ae6f7882SJeremy Morse } 1865ae6f7882SJeremy Morse 1866ae6f7882SJeremy Morse bool InstrRefBasedLDV::isLocationSpill(const MachineInstr &MI, 1867ae6f7882SJeremy Morse MachineFunction *MF, unsigned &Reg) { 1868ae6f7882SJeremy Morse if (!isSpillInstruction(MI, MF)) 1869ae6f7882SJeremy Morse return false; 1870ae6f7882SJeremy Morse 1871ae6f7882SJeremy Morse // XXX FIXME: On x86, isStoreToStackSlotPostFE returns '1' instead of an 1872ae6f7882SJeremy Morse // actual register number. 1873ae6f7882SJeremy Morse if (ObserveAllStackops) { 1874ae6f7882SJeremy Morse int FI; 1875ae6f7882SJeremy Morse Reg = TII->isStoreToStackSlotPostFE(MI, FI); 1876ae6f7882SJeremy Morse return Reg != 0; 1877ae6f7882SJeremy Morse } 1878ae6f7882SJeremy Morse 1879ae6f7882SJeremy Morse auto isKilledReg = [&](const MachineOperand MO, unsigned &Reg) { 1880ae6f7882SJeremy Morse if (!MO.isReg() || !MO.isUse()) { 1881ae6f7882SJeremy Morse Reg = 0; 1882ae6f7882SJeremy Morse return false; 1883ae6f7882SJeremy Morse } 1884ae6f7882SJeremy Morse Reg = MO.getReg(); 1885ae6f7882SJeremy Morse return MO.isKill(); 1886ae6f7882SJeremy Morse }; 1887ae6f7882SJeremy Morse 1888ae6f7882SJeremy Morse for (const MachineOperand &MO : MI.operands()) { 1889ae6f7882SJeremy Morse // In a spill instruction generated by the InlineSpiller the spilled 1890ae6f7882SJeremy Morse // register has its kill flag set. 1891ae6f7882SJeremy Morse if (isKilledReg(MO, Reg)) 1892ae6f7882SJeremy Morse return true; 1893ae6f7882SJeremy Morse if (Reg != 0) { 1894ae6f7882SJeremy Morse // Check whether next instruction kills the spilled register. 1895ae6f7882SJeremy Morse // FIXME: Current solution does not cover search for killed register in 1896ae6f7882SJeremy Morse // bundles and instructions further down the chain. 1897ae6f7882SJeremy Morse auto NextI = std::next(MI.getIterator()); 1898ae6f7882SJeremy Morse // Skip next instruction that points to basic block end iterator. 1899ae6f7882SJeremy Morse if (MI.getParent()->end() == NextI) 1900ae6f7882SJeremy Morse continue; 1901ae6f7882SJeremy Morse unsigned RegNext; 1902ae6f7882SJeremy Morse for (const MachineOperand &MONext : NextI->operands()) { 1903ae6f7882SJeremy Morse // Return true if we came across the register from the 1904ae6f7882SJeremy Morse // previous spill instruction that is killed in NextI. 1905ae6f7882SJeremy Morse if (isKilledReg(MONext, RegNext) && RegNext == Reg) 1906ae6f7882SJeremy Morse return true; 1907ae6f7882SJeremy Morse } 1908ae6f7882SJeremy Morse } 1909ae6f7882SJeremy Morse } 1910ae6f7882SJeremy Morse // Return false if we didn't find spilled register. 1911ae6f7882SJeremy Morse return false; 1912ae6f7882SJeremy Morse } 1913ae6f7882SJeremy Morse 1914ae6f7882SJeremy Morse Optional<SpillLoc> 1915ae6f7882SJeremy Morse InstrRefBasedLDV::isRestoreInstruction(const MachineInstr &MI, 1916ae6f7882SJeremy Morse MachineFunction *MF, unsigned &Reg) { 1917ae6f7882SJeremy Morse if (!MI.hasOneMemOperand()) 1918ae6f7882SJeremy Morse return None; 1919ae6f7882SJeremy Morse 1920ae6f7882SJeremy Morse // FIXME: Handle folded restore instructions with more than one memory 1921ae6f7882SJeremy Morse // operand. 1922ae6f7882SJeremy Morse if (MI.getRestoreSize(TII)) { 1923ae6f7882SJeremy Morse Reg = MI.getOperand(0).getReg(); 1924ae6f7882SJeremy Morse return extractSpillBaseRegAndOffset(MI); 1925ae6f7882SJeremy Morse } 1926ae6f7882SJeremy Morse return None; 1927ae6f7882SJeremy Morse } 1928ae6f7882SJeremy Morse 1929ae6f7882SJeremy Morse bool InstrRefBasedLDV::transferSpillOrRestoreInst(MachineInstr &MI) { 1930ae6f7882SJeremy Morse // XXX -- it's too difficult to implement VarLocBasedImpl's stack location 1931ae6f7882SJeremy Morse // limitations under the new model. Therefore, when comparing them, compare 1932ae6f7882SJeremy Morse // versions that don't attempt spills or restores at all. 1933ae6f7882SJeremy Morse if (EmulateOldLDV) 1934ae6f7882SJeremy Morse return false; 1935ae6f7882SJeremy Morse 1936ae6f7882SJeremy Morse MachineFunction *MF = MI.getMF(); 1937ae6f7882SJeremy Morse unsigned Reg; 1938ae6f7882SJeremy Morse Optional<SpillLoc> Loc; 1939ae6f7882SJeremy Morse 1940ae6f7882SJeremy Morse LLVM_DEBUG(dbgs() << "Examining instruction: "; MI.dump();); 1941ae6f7882SJeremy Morse 1942ae6f7882SJeremy Morse // First, if there are any DBG_VALUEs pointing at a spill slot that is 1943ae6f7882SJeremy Morse // written to, terminate that variable location. The value in memory 1944ae6f7882SJeremy Morse // will have changed. DbgEntityHistoryCalculator doesn't try to detect this. 1945ae6f7882SJeremy Morse if (isSpillInstruction(MI, MF)) { 1946ae6f7882SJeremy Morse Loc = extractSpillBaseRegAndOffset(MI); 1947ae6f7882SJeremy Morse 1948ae6f7882SJeremy Morse if (TTracker) { 1949ae6f7882SJeremy Morse Optional<LocIdx> MLoc = MTracker->getSpillMLoc(*Loc); 1950ae6f7882SJeremy Morse if (MLoc) 1951ae6f7882SJeremy Morse TTracker->clobberMloc(*MLoc, MI.getIterator()); 1952ae6f7882SJeremy Morse } 1953ae6f7882SJeremy Morse } 1954ae6f7882SJeremy Morse 1955ae6f7882SJeremy Morse // Try to recognise spill and restore instructions that may transfer a value. 1956ae6f7882SJeremy Morse if (isLocationSpill(MI, MF, Reg)) { 1957ae6f7882SJeremy Morse Loc = extractSpillBaseRegAndOffset(MI); 1958ae6f7882SJeremy Morse auto ValueID = MTracker->readReg(Reg); 1959ae6f7882SJeremy Morse 1960ae6f7882SJeremy Morse // If the location is empty, produce a phi, signify it's the live-in value. 1961ae6f7882SJeremy Morse if (ValueID.getLoc() == 0) 1962ae6f7882SJeremy Morse ValueID = {CurBB, 0, MTracker->getRegMLoc(Reg)}; 1963ae6f7882SJeremy Morse 1964ae6f7882SJeremy Morse MTracker->setSpill(*Loc, ValueID); 1965ae6f7882SJeremy Morse auto OptSpillLocIdx = MTracker->getSpillMLoc(*Loc); 1966ae6f7882SJeremy Morse assert(OptSpillLocIdx && "Spill slot set but has no LocIdx?"); 1967ae6f7882SJeremy Morse LocIdx SpillLocIdx = *OptSpillLocIdx; 1968ae6f7882SJeremy Morse 1969ae6f7882SJeremy Morse // Tell TransferTracker about this spill, produce DBG_VALUEs for it. 1970ae6f7882SJeremy Morse if (TTracker) 1971ae6f7882SJeremy Morse TTracker->transferMlocs(MTracker->getRegMLoc(Reg), SpillLocIdx, 1972ae6f7882SJeremy Morse MI.getIterator()); 1973ae6f7882SJeremy Morse } else { 1974ae6f7882SJeremy Morse if (!(Loc = isRestoreInstruction(MI, MF, Reg))) 1975ae6f7882SJeremy Morse return false; 1976ae6f7882SJeremy Morse 1977ae6f7882SJeremy Morse // Is there a value to be restored? 1978ae6f7882SJeremy Morse auto OptValueID = MTracker->readSpill(*Loc); 1979ae6f7882SJeremy Morse if (OptValueID) { 1980ae6f7882SJeremy Morse ValueIDNum ValueID = *OptValueID; 1981ae6f7882SJeremy Morse LocIdx SpillLocIdx = *MTracker->getSpillMLoc(*Loc); 1982ae6f7882SJeremy Morse // XXX -- can we recover sub-registers of this value? Until we can, first 1983ae6f7882SJeremy Morse // overwrite all defs of the register being restored to. 1984ae6f7882SJeremy Morse for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI) 1985ae6f7882SJeremy Morse MTracker->defReg(*RAI, CurBB, CurInst); 1986ae6f7882SJeremy Morse 1987ae6f7882SJeremy Morse // Now override the reg we're restoring to. 1988ae6f7882SJeremy Morse MTracker->setReg(Reg, ValueID); 1989ae6f7882SJeremy Morse 1990ae6f7882SJeremy Morse // Report this restore to the transfer tracker too. 1991ae6f7882SJeremy Morse if (TTracker) 1992ae6f7882SJeremy Morse TTracker->transferMlocs(SpillLocIdx, MTracker->getRegMLoc(Reg), 1993ae6f7882SJeremy Morse MI.getIterator()); 1994ae6f7882SJeremy Morse } else { 1995ae6f7882SJeremy Morse // There isn't anything in the location; not clear if this is a code path 1996ae6f7882SJeremy Morse // that still runs. Def this register anyway just in case. 1997ae6f7882SJeremy Morse for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI) 1998ae6f7882SJeremy Morse MTracker->defReg(*RAI, CurBB, CurInst); 1999ae6f7882SJeremy Morse 2000ae6f7882SJeremy Morse // Force the spill slot to be tracked. 2001ae6f7882SJeremy Morse LocIdx L = MTracker->getOrTrackSpillLoc(*Loc); 2002ae6f7882SJeremy Morse 2003ae6f7882SJeremy Morse // Set the restored value to be a machine phi number, signifying that it's 2004ae6f7882SJeremy Morse // whatever the spills live-in value is in this block. Definitely has 2005ae6f7882SJeremy Morse // a LocIdx due to the setSpill above. 2006ae6f7882SJeremy Morse ValueIDNum ValueID = {CurBB, 0, L}; 2007ae6f7882SJeremy Morse MTracker->setReg(Reg, ValueID); 2008ae6f7882SJeremy Morse MTracker->setSpill(*Loc, ValueID); 2009ae6f7882SJeremy Morse } 2010ae6f7882SJeremy Morse } 2011ae6f7882SJeremy Morse return true; 2012ae6f7882SJeremy Morse } 2013ae6f7882SJeremy Morse 2014ae6f7882SJeremy Morse bool InstrRefBasedLDV::transferRegisterCopy(MachineInstr &MI) { 2015ae6f7882SJeremy Morse auto DestSrc = TII->isCopyInstr(MI); 2016ae6f7882SJeremy Morse if (!DestSrc) 2017ae6f7882SJeremy Morse return false; 2018ae6f7882SJeremy Morse 2019ae6f7882SJeremy Morse const MachineOperand *DestRegOp = DestSrc->Destination; 2020ae6f7882SJeremy Morse const MachineOperand *SrcRegOp = DestSrc->Source; 2021ae6f7882SJeremy Morse 2022ae6f7882SJeremy Morse auto isCalleeSavedReg = [&](unsigned Reg) { 2023ae6f7882SJeremy Morse for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI) 2024ae6f7882SJeremy Morse if (CalleeSavedRegs.test(*RAI)) 2025ae6f7882SJeremy Morse return true; 2026ae6f7882SJeremy Morse return false; 2027ae6f7882SJeremy Morse }; 2028ae6f7882SJeremy Morse 2029ae6f7882SJeremy Morse Register SrcReg = SrcRegOp->getReg(); 2030ae6f7882SJeremy Morse Register DestReg = DestRegOp->getReg(); 2031ae6f7882SJeremy Morse 2032ae6f7882SJeremy Morse // Ignore identity copies. Yep, these make it as far as LiveDebugValues. 2033ae6f7882SJeremy Morse if (SrcReg == DestReg) 2034ae6f7882SJeremy Morse return true; 2035ae6f7882SJeremy Morse 2036ae6f7882SJeremy Morse // For emulating VarLocBasedImpl: 2037ae6f7882SJeremy Morse // We want to recognize instructions where destination register is callee 2038ae6f7882SJeremy Morse // saved register. If register that could be clobbered by the call is 2039ae6f7882SJeremy Morse // included, there would be a great chance that it is going to be clobbered 2040ae6f7882SJeremy Morse // soon. It is more likely that previous register, which is callee saved, is 2041ae6f7882SJeremy Morse // going to stay unclobbered longer, even if it is killed. 2042ae6f7882SJeremy Morse // 2043ae6f7882SJeremy Morse // For InstrRefBasedImpl, we can track multiple locations per value, so 2044ae6f7882SJeremy Morse // ignore this condition. 2045ae6f7882SJeremy Morse if (EmulateOldLDV && !isCalleeSavedReg(DestReg)) 2046ae6f7882SJeremy Morse return false; 2047ae6f7882SJeremy Morse 2048ae6f7882SJeremy Morse // InstrRefBasedImpl only followed killing copies. 2049ae6f7882SJeremy Morse if (EmulateOldLDV && !SrcRegOp->isKill()) 2050ae6f7882SJeremy Morse return false; 2051ae6f7882SJeremy Morse 2052ae6f7882SJeremy Morse // Copy MTracker info, including subregs if available. 2053ae6f7882SJeremy Morse InstrRefBasedLDV::performCopy(SrcReg, DestReg); 2054ae6f7882SJeremy Morse 2055ae6f7882SJeremy Morse // Only produce a transfer of DBG_VALUE within a block where old LDV 2056ae6f7882SJeremy Morse // would have. We might make use of the additional value tracking in some 2057ae6f7882SJeremy Morse // other way, later. 2058ae6f7882SJeremy Morse if (TTracker && isCalleeSavedReg(DestReg) && SrcRegOp->isKill()) 2059ae6f7882SJeremy Morse TTracker->transferMlocs(MTracker->getRegMLoc(SrcReg), 2060ae6f7882SJeremy Morse MTracker->getRegMLoc(DestReg), MI.getIterator()); 2061ae6f7882SJeremy Morse 2062ae6f7882SJeremy Morse // VarLocBasedImpl would quit tracking the old location after copying. 2063ae6f7882SJeremy Morse if (EmulateOldLDV && SrcReg != DestReg) 2064ae6f7882SJeremy Morse MTracker->defReg(SrcReg, CurBB, CurInst); 2065ae6f7882SJeremy Morse 2066ae6f7882SJeremy Morse return true; 2067ae6f7882SJeremy Morse } 2068ae6f7882SJeremy Morse 2069ae6f7882SJeremy Morse /// Accumulate a mapping between each DILocalVariable fragment and other 2070ae6f7882SJeremy Morse /// fragments of that DILocalVariable which overlap. This reduces work during 2071ae6f7882SJeremy Morse /// the data-flow stage from "Find any overlapping fragments" to "Check if the 2072ae6f7882SJeremy Morse /// known-to-overlap fragments are present". 2073ae6f7882SJeremy Morse /// \param MI A previously unprocessed DEBUG_VALUE instruction to analyze for 2074ae6f7882SJeremy Morse /// fragment usage. 2075ae6f7882SJeremy Morse void InstrRefBasedLDV::accumulateFragmentMap(MachineInstr &MI) { 2076ae6f7882SJeremy Morse DebugVariable MIVar(MI.getDebugVariable(), MI.getDebugExpression(), 2077ae6f7882SJeremy Morse MI.getDebugLoc()->getInlinedAt()); 2078ae6f7882SJeremy Morse FragmentInfo ThisFragment = MIVar.getFragmentOrDefault(); 2079ae6f7882SJeremy Morse 2080ae6f7882SJeremy Morse // If this is the first sighting of this variable, then we are guaranteed 2081ae6f7882SJeremy Morse // there are currently no overlapping fragments either. Initialize the set 2082ae6f7882SJeremy Morse // of seen fragments, record no overlaps for the current one, and return. 2083ae6f7882SJeremy Morse auto SeenIt = SeenFragments.find(MIVar.getVariable()); 2084ae6f7882SJeremy Morse if (SeenIt == SeenFragments.end()) { 2085ae6f7882SJeremy Morse SmallSet<FragmentInfo, 4> OneFragment; 2086ae6f7882SJeremy Morse OneFragment.insert(ThisFragment); 2087ae6f7882SJeremy Morse SeenFragments.insert({MIVar.getVariable(), OneFragment}); 2088ae6f7882SJeremy Morse 2089ae6f7882SJeremy Morse OverlapFragments.insert({{MIVar.getVariable(), ThisFragment}, {}}); 2090ae6f7882SJeremy Morse return; 2091ae6f7882SJeremy Morse } 2092ae6f7882SJeremy Morse 2093ae6f7882SJeremy Morse // If this particular Variable/Fragment pair already exists in the overlap 2094ae6f7882SJeremy Morse // map, it has already been accounted for. 2095ae6f7882SJeremy Morse auto IsInOLapMap = 2096ae6f7882SJeremy Morse OverlapFragments.insert({{MIVar.getVariable(), ThisFragment}, {}}); 2097ae6f7882SJeremy Morse if (!IsInOLapMap.second) 2098ae6f7882SJeremy Morse return; 2099ae6f7882SJeremy Morse 2100ae6f7882SJeremy Morse auto &ThisFragmentsOverlaps = IsInOLapMap.first->second; 2101ae6f7882SJeremy Morse auto &AllSeenFragments = SeenIt->second; 2102ae6f7882SJeremy Morse 2103ae6f7882SJeremy Morse // Otherwise, examine all other seen fragments for this variable, with "this" 2104ae6f7882SJeremy Morse // fragment being a previously unseen fragment. Record any pair of 2105ae6f7882SJeremy Morse // overlapping fragments. 2106ae6f7882SJeremy Morse for (auto &ASeenFragment : AllSeenFragments) { 2107ae6f7882SJeremy Morse // Does this previously seen fragment overlap? 2108ae6f7882SJeremy Morse if (DIExpression::fragmentsOverlap(ThisFragment, ASeenFragment)) { 2109ae6f7882SJeremy Morse // Yes: Mark the current fragment as being overlapped. 2110ae6f7882SJeremy Morse ThisFragmentsOverlaps.push_back(ASeenFragment); 2111ae6f7882SJeremy Morse // Mark the previously seen fragment as being overlapped by the current 2112ae6f7882SJeremy Morse // one. 2113ae6f7882SJeremy Morse auto ASeenFragmentsOverlaps = 2114ae6f7882SJeremy Morse OverlapFragments.find({MIVar.getVariable(), ASeenFragment}); 2115ae6f7882SJeremy Morse assert(ASeenFragmentsOverlaps != OverlapFragments.end() && 2116ae6f7882SJeremy Morse "Previously seen var fragment has no vector of overlaps"); 2117ae6f7882SJeremy Morse ASeenFragmentsOverlaps->second.push_back(ThisFragment); 2118ae6f7882SJeremy Morse } 2119ae6f7882SJeremy Morse } 2120ae6f7882SJeremy Morse 2121ae6f7882SJeremy Morse AllSeenFragments.insert(ThisFragment); 2122ae6f7882SJeremy Morse } 2123ae6f7882SJeremy Morse 2124ae6f7882SJeremy Morse void InstrRefBasedLDV::process(MachineInstr &MI) { 2125ae6f7882SJeremy Morse // Try to interpret an MI as a debug or transfer instruction. Only if it's 2126ae6f7882SJeremy Morse // none of these should we interpret it's register defs as new value 2127ae6f7882SJeremy Morse // definitions. 2128ae6f7882SJeremy Morse if (transferDebugValue(MI)) 2129ae6f7882SJeremy Morse return; 213068f47157SJeremy Morse if (transferDebugInstrRef(MI)) 213168f47157SJeremy Morse return; 2132ae6f7882SJeremy Morse if (transferRegisterCopy(MI)) 2133ae6f7882SJeremy Morse return; 2134ae6f7882SJeremy Morse if (transferSpillOrRestoreInst(MI)) 2135ae6f7882SJeremy Morse return; 2136ae6f7882SJeremy Morse transferRegisterDef(MI); 2137ae6f7882SJeremy Morse } 2138ae6f7882SJeremy Morse 2139ab93e710SJeremy Morse void InstrRefBasedLDV::produceMLocTransferFunction( 2140ae6f7882SJeremy Morse MachineFunction &MF, SmallVectorImpl<MLocTransferMap> &MLocTransfer, 2141ab93e710SJeremy Morse unsigned MaxNumBlocks) { 2142ae6f7882SJeremy Morse // Because we try to optimize around register mask operands by ignoring regs 2143ae6f7882SJeremy Morse // that aren't currently tracked, we set up something ugly for later: RegMask 2144ae6f7882SJeremy Morse // operands that are seen earlier than the first use of a register, still need 2145ae6f7882SJeremy Morse // to clobber that register in the transfer function. But this information 2146ae6f7882SJeremy Morse // isn't actively recorded. Instead, we track each RegMask used in each block, 2147ae6f7882SJeremy Morse // and accumulated the clobbered but untracked registers in each block into 2148ae6f7882SJeremy Morse // the following bitvector. Later, if new values are tracked, we can add 2149ae6f7882SJeremy Morse // appropriate clobbers. 2150ae6f7882SJeremy Morse SmallVector<BitVector, 32> BlockMasks; 2151ae6f7882SJeremy Morse BlockMasks.resize(MaxNumBlocks); 2152ae6f7882SJeremy Morse 2153ae6f7882SJeremy Morse // Reserve one bit per register for the masks described above. 2154ae6f7882SJeremy Morse unsigned BVWords = MachineOperand::getRegMaskSize(TRI->getNumRegs()); 2155ae6f7882SJeremy Morse for (auto &BV : BlockMasks) 2156ae6f7882SJeremy Morse BV.resize(TRI->getNumRegs(), true); 2157ae6f7882SJeremy Morse 2158ae6f7882SJeremy Morse // Step through all instructions and inhale the transfer function. 2159ae6f7882SJeremy Morse for (auto &MBB : MF) { 2160ae6f7882SJeremy Morse // Object fields that are read by trackers to know where we are in the 2161ae6f7882SJeremy Morse // function. 2162ae6f7882SJeremy Morse CurBB = MBB.getNumber(); 2163ae6f7882SJeremy Morse CurInst = 1; 2164ae6f7882SJeremy Morse 2165ae6f7882SJeremy Morse // Set all machine locations to a PHI value. For transfer function 2166ae6f7882SJeremy Morse // production only, this signifies the live-in value to the block. 2167ae6f7882SJeremy Morse MTracker->reset(); 2168ae6f7882SJeremy Morse MTracker->setMPhis(CurBB); 2169ae6f7882SJeremy Morse 2170ae6f7882SJeremy Morse // Step through each instruction in this block. 2171ae6f7882SJeremy Morse for (auto &MI : MBB) { 2172ae6f7882SJeremy Morse process(MI); 2173ae6f7882SJeremy Morse // Also accumulate fragment map. 2174ae6f7882SJeremy Morse if (MI.isDebugValue()) 2175ae6f7882SJeremy Morse accumulateFragmentMap(MI); 217668f47157SJeremy Morse 217768f47157SJeremy Morse // Create a map from the instruction number (if present) to the 217868f47157SJeremy Morse // MachineInstr and its position. 2179cca049adSDjordje Todorovic if (uint64_t InstrNo = MI.peekDebugInstrNum()) { 218068f47157SJeremy Morse auto InstrAndPos = std::make_pair(&MI, CurInst); 218168f47157SJeremy Morse auto InsertResult = 218268f47157SJeremy Morse DebugInstrNumToInstr.insert(std::make_pair(InstrNo, InstrAndPos)); 218368f47157SJeremy Morse 218468f47157SJeremy Morse // There should never be duplicate instruction numbers. 218568f47157SJeremy Morse assert(InsertResult.second); 218668f47157SJeremy Morse (void)InsertResult; 218768f47157SJeremy Morse } 218868f47157SJeremy Morse 2189ae6f7882SJeremy Morse ++CurInst; 2190ae6f7882SJeremy Morse } 2191ae6f7882SJeremy Morse 2192ae6f7882SJeremy Morse // Produce the transfer function, a map of machine location to new value. If 2193ae6f7882SJeremy Morse // any machine location has the live-in phi value from the start of the 2194ae6f7882SJeremy Morse // block, it's live-through and doesn't need recording in the transfer 2195ae6f7882SJeremy Morse // function. 2196ae6f7882SJeremy Morse for (auto Location : MTracker->locations()) { 2197ae6f7882SJeremy Morse LocIdx Idx = Location.Idx; 2198ae6f7882SJeremy Morse ValueIDNum &P = Location.Value; 2199ae6f7882SJeremy Morse if (P.isPHI() && P.getLoc() == Idx.asU64()) 2200ae6f7882SJeremy Morse continue; 2201ae6f7882SJeremy Morse 2202ae6f7882SJeremy Morse // Insert-or-update. 2203ae6f7882SJeremy Morse auto &TransferMap = MLocTransfer[CurBB]; 2204ae6f7882SJeremy Morse auto Result = TransferMap.insert(std::make_pair(Idx.asU64(), P)); 2205ae6f7882SJeremy Morse if (!Result.second) 2206ae6f7882SJeremy Morse Result.first->second = P; 2207ae6f7882SJeremy Morse } 2208ae6f7882SJeremy Morse 2209ae6f7882SJeremy Morse // Accumulate any bitmask operands into the clobberred reg mask for this 2210ae6f7882SJeremy Morse // block. 2211ae6f7882SJeremy Morse for (auto &P : MTracker->Masks) { 2212ae6f7882SJeremy Morse BlockMasks[CurBB].clearBitsNotInMask(P.first->getRegMask(), BVWords); 2213ae6f7882SJeremy Morse } 2214ae6f7882SJeremy Morse } 2215ae6f7882SJeremy Morse 2216ae6f7882SJeremy Morse // Compute a bitvector of all the registers that are tracked in this block. 2217ae6f7882SJeremy Morse const TargetLowering *TLI = MF.getSubtarget().getTargetLowering(); 22184634ad6cSGaurav Jain Register SP = TLI->getStackPointerRegisterToSaveRestore(); 2219ae6f7882SJeremy Morse BitVector UsedRegs(TRI->getNumRegs()); 2220ae6f7882SJeremy Morse for (auto Location : MTracker->locations()) { 2221ae6f7882SJeremy Morse unsigned ID = MTracker->LocIdxToLocID[Location.Idx]; 2222ae6f7882SJeremy Morse if (ID >= TRI->getNumRegs() || ID == SP) 2223ae6f7882SJeremy Morse continue; 2224ae6f7882SJeremy Morse UsedRegs.set(ID); 2225ae6f7882SJeremy Morse } 2226ae6f7882SJeremy Morse 2227ae6f7882SJeremy Morse // Check that any regmask-clobber of a register that gets tracked, is not 2228ae6f7882SJeremy Morse // live-through in the transfer function. It needs to be clobbered at the 2229ae6f7882SJeremy Morse // very least. 2230ae6f7882SJeremy Morse for (unsigned int I = 0; I < MaxNumBlocks; ++I) { 2231ae6f7882SJeremy Morse BitVector &BV = BlockMasks[I]; 2232ae6f7882SJeremy Morse BV.flip(); 2233ae6f7882SJeremy Morse BV &= UsedRegs; 2234ae6f7882SJeremy Morse // This produces all the bits that we clobber, but also use. Check that 2235ae6f7882SJeremy Morse // they're all clobbered or at least set in the designated transfer 2236ae6f7882SJeremy Morse // elem. 2237ae6f7882SJeremy Morse for (unsigned Bit : BV.set_bits()) { 2238ae6f7882SJeremy Morse unsigned ID = MTracker->getLocID(Bit, false); 2239ae6f7882SJeremy Morse LocIdx Idx = MTracker->LocIDToLocIdx[ID]; 2240ae6f7882SJeremy Morse auto &TransferMap = MLocTransfer[I]; 2241ae6f7882SJeremy Morse 2242ae6f7882SJeremy Morse // Install a value representing the fact that this location is effectively 2243ae6f7882SJeremy Morse // written to in this block. As there's no reserved value, instead use 2244ae6f7882SJeremy Morse // a value number that is never generated. Pick the value number for the 2245ae6f7882SJeremy Morse // first instruction in the block, def'ing this location, which we know 2246ae6f7882SJeremy Morse // this block never used anyway. 2247ae6f7882SJeremy Morse ValueIDNum NotGeneratedNum = ValueIDNum(I, 1, Idx); 2248ae6f7882SJeremy Morse auto Result = 2249ae6f7882SJeremy Morse TransferMap.insert(std::make_pair(Idx.asU64(), NotGeneratedNum)); 2250ae6f7882SJeremy Morse if (!Result.second) { 2251ae6f7882SJeremy Morse ValueIDNum &ValueID = Result.first->second; 2252ae6f7882SJeremy Morse if (ValueID.getBlock() == I && ValueID.isPHI()) 2253ae6f7882SJeremy Morse // It was left as live-through. Set it to clobbered. 2254ae6f7882SJeremy Morse ValueID = NotGeneratedNum; 2255ae6f7882SJeremy Morse } 2256ae6f7882SJeremy Morse } 2257ae6f7882SJeremy Morse } 2258ae6f7882SJeremy Morse } 2259ae6f7882SJeremy Morse 2260ae6f7882SJeremy Morse std::tuple<bool, bool> 2261ae6f7882SJeremy Morse InstrRefBasedLDV::mlocJoin(MachineBasicBlock &MBB, 2262ae6f7882SJeremy Morse SmallPtrSet<const MachineBasicBlock *, 16> &Visited, 2263ae6f7882SJeremy Morse ValueIDNum **OutLocs, ValueIDNum *InLocs) { 2264ae6f7882SJeremy Morse LLVM_DEBUG(dbgs() << "join MBB: " << MBB.getNumber() << "\n"); 2265ae6f7882SJeremy Morse bool Changed = false; 2266ae6f7882SJeremy Morse bool DowngradeOccurred = false; 2267ae6f7882SJeremy Morse 2268ae6f7882SJeremy Morse // Collect predecessors that have been visited. Anything that hasn't been 2269ae6f7882SJeremy Morse // visited yet is a backedge on the first iteration, and the meet of it's 2270ae6f7882SJeremy Morse // lattice value for all locations will be unaffected. 2271ae6f7882SJeremy Morse SmallVector<const MachineBasicBlock *, 8> BlockOrders; 2272ae6f7882SJeremy Morse for (auto Pred : MBB.predecessors()) { 2273ae6f7882SJeremy Morse if (Visited.count(Pred)) { 2274ae6f7882SJeremy Morse BlockOrders.push_back(Pred); 2275ae6f7882SJeremy Morse } 2276ae6f7882SJeremy Morse } 2277ae6f7882SJeremy Morse 2278ae6f7882SJeremy Morse // Visit predecessors in RPOT order. 2279ae6f7882SJeremy Morse auto Cmp = [&](const MachineBasicBlock *A, const MachineBasicBlock *B) { 2280ae6f7882SJeremy Morse return BBToOrder.find(A)->second < BBToOrder.find(B)->second; 2281ae6f7882SJeremy Morse }; 2282ae6f7882SJeremy Morse llvm::sort(BlockOrders.begin(), BlockOrders.end(), Cmp); 2283ae6f7882SJeremy Morse 2284ae6f7882SJeremy Morse // Skip entry block. 2285ae6f7882SJeremy Morse if (BlockOrders.size() == 0) 2286ae6f7882SJeremy Morse return std::tuple<bool, bool>(false, false); 2287ae6f7882SJeremy Morse 2288ae6f7882SJeremy Morse // Step through all machine locations, then look at each predecessor and 2289ae6f7882SJeremy Morse // detect disagreements. 2290ae6f7882SJeremy Morse unsigned ThisBlockRPO = BBToOrder.find(&MBB)->second; 2291ae6f7882SJeremy Morse for (auto Location : MTracker->locations()) { 2292ae6f7882SJeremy Morse LocIdx Idx = Location.Idx; 2293ae6f7882SJeremy Morse // Pick out the first predecessors live-out value for this location. It's 2294ae6f7882SJeremy Morse // guaranteed to be not a backedge, as we order by RPO. 2295ae6f7882SJeremy Morse ValueIDNum BaseVal = OutLocs[BlockOrders[0]->getNumber()][Idx.asU64()]; 2296ae6f7882SJeremy Morse 2297ae6f7882SJeremy Morse // Some flags for whether there's a disagreement, and whether it's a 2298ae6f7882SJeremy Morse // disagreement with a backedge or not. 2299ae6f7882SJeremy Morse bool Disagree = false; 2300ae6f7882SJeremy Morse bool NonBackEdgeDisagree = false; 2301ae6f7882SJeremy Morse 2302ae6f7882SJeremy Morse // Loop around everything that wasn't 'base'. 2303ae6f7882SJeremy Morse for (unsigned int I = 1; I < BlockOrders.size(); ++I) { 2304ae6f7882SJeremy Morse auto *MBB = BlockOrders[I]; 2305ae6f7882SJeremy Morse if (BaseVal != OutLocs[MBB->getNumber()][Idx.asU64()]) { 2306ae6f7882SJeremy Morse // Live-out of a predecessor disagrees with the first predecessor. 2307ae6f7882SJeremy Morse Disagree = true; 2308ae6f7882SJeremy Morse 2309ae6f7882SJeremy Morse // Test whether it's a disagreemnt in the backedges or not. 2310ae6f7882SJeremy Morse if (BBToOrder.find(MBB)->second < ThisBlockRPO) // might be self b/e 2311ae6f7882SJeremy Morse NonBackEdgeDisagree = true; 2312ae6f7882SJeremy Morse } 2313ae6f7882SJeremy Morse } 2314ae6f7882SJeremy Morse 2315ae6f7882SJeremy Morse bool OverRide = false; 2316ae6f7882SJeremy Morse if (Disagree && !NonBackEdgeDisagree) { 2317ae6f7882SJeremy Morse // Only the backedges disagree. Consider demoting the livein 2318ae6f7882SJeremy Morse // lattice value, as per the file level comment. The value we consider 2319ae6f7882SJeremy Morse // demoting to is the value that the non-backedge predecessors agree on. 2320ae6f7882SJeremy Morse // The order of values is that non-PHIs are \top, a PHI at this block 2321ae6f7882SJeremy Morse // \bot, and phis between the two are ordered by their RPO number. 2322ae6f7882SJeremy Morse // If there's no agreement, or we've already demoted to this PHI value 2323ae6f7882SJeremy Morse // before, replace with a PHI value at this block. 2324ae6f7882SJeremy Morse 2325ae6f7882SJeremy Morse // Calculate order numbers: zero means normal def, nonzero means RPO 2326ae6f7882SJeremy Morse // number. 2327ae6f7882SJeremy Morse unsigned BaseBlockRPONum = BBNumToRPO[BaseVal.getBlock()] + 1; 2328ae6f7882SJeremy Morse if (!BaseVal.isPHI()) 2329ae6f7882SJeremy Morse BaseBlockRPONum = 0; 2330ae6f7882SJeremy Morse 2331ae6f7882SJeremy Morse ValueIDNum &InLocID = InLocs[Idx.asU64()]; 2332ae6f7882SJeremy Morse unsigned InLocRPONum = BBNumToRPO[InLocID.getBlock()] + 1; 2333ae6f7882SJeremy Morse if (!InLocID.isPHI()) 2334ae6f7882SJeremy Morse InLocRPONum = 0; 2335ae6f7882SJeremy Morse 2336ae6f7882SJeremy Morse // Should we ignore the disagreeing backedges, and override with the 2337ae6f7882SJeremy Morse // value the other predecessors agree on (in "base")? 2338ae6f7882SJeremy Morse unsigned ThisBlockRPONum = BBNumToRPO[MBB.getNumber()] + 1; 2339ae6f7882SJeremy Morse if (BaseBlockRPONum > InLocRPONum && BaseBlockRPONum < ThisBlockRPONum) { 2340ae6f7882SJeremy Morse // Override. 2341ae6f7882SJeremy Morse OverRide = true; 2342ae6f7882SJeremy Morse DowngradeOccurred = true; 2343ae6f7882SJeremy Morse } 2344ae6f7882SJeremy Morse } 2345ae6f7882SJeremy Morse // else: if we disagree in the non-backedges, then this is definitely 2346ae6f7882SJeremy Morse // a control flow merge where different values merge. Make it a PHI. 2347ae6f7882SJeremy Morse 2348ae6f7882SJeremy Morse // Generate a phi... 2349ae6f7882SJeremy Morse ValueIDNum PHI = {(uint64_t)MBB.getNumber(), 0, Idx}; 2350ae6f7882SJeremy Morse ValueIDNum NewVal = (Disagree && !OverRide) ? PHI : BaseVal; 2351ae6f7882SJeremy Morse if (InLocs[Idx.asU64()] != NewVal) { 2352ae6f7882SJeremy Morse Changed |= true; 2353ae6f7882SJeremy Morse InLocs[Idx.asU64()] = NewVal; 2354ae6f7882SJeremy Morse } 2355ae6f7882SJeremy Morse } 2356ae6f7882SJeremy Morse 2357cca049adSDjordje Todorovic // TODO: Reimplement NumInserted and NumRemoved. 2358ae6f7882SJeremy Morse return std::tuple<bool, bool>(Changed, DowngradeOccurred); 2359ae6f7882SJeremy Morse } 2360ae6f7882SJeremy Morse 2361ae6f7882SJeremy Morse void InstrRefBasedLDV::mlocDataflow( 2362ae6f7882SJeremy Morse ValueIDNum **MInLocs, ValueIDNum **MOutLocs, 2363ae6f7882SJeremy Morse SmallVectorImpl<MLocTransferMap> &MLocTransfer) { 2364ae6f7882SJeremy Morse std::priority_queue<unsigned int, std::vector<unsigned int>, 2365ae6f7882SJeremy Morse std::greater<unsigned int>> 2366ae6f7882SJeremy Morse Worklist, Pending; 2367ae6f7882SJeremy Morse 2368ae6f7882SJeremy Morse // We track what is on the current and pending worklist to avoid inserting 2369ae6f7882SJeremy Morse // the same thing twice. We could avoid this with a custom priority queue, 2370ae6f7882SJeremy Morse // but this is probably not worth it. 2371ae6f7882SJeremy Morse SmallPtrSet<MachineBasicBlock *, 16> OnPending, OnWorklist; 2372ae6f7882SJeremy Morse 2373ae6f7882SJeremy Morse // Initialize worklist with every block to be visited. 2374ae6f7882SJeremy Morse for (unsigned int I = 0; I < BBToOrder.size(); ++I) { 2375ae6f7882SJeremy Morse Worklist.push(I); 2376ae6f7882SJeremy Morse OnWorklist.insert(OrderToBB[I]); 2377ae6f7882SJeremy Morse } 2378ae6f7882SJeremy Morse 2379ae6f7882SJeremy Morse MTracker->reset(); 2380ae6f7882SJeremy Morse 2381ae6f7882SJeremy Morse // Set inlocs for entry block -- each as a PHI at the entry block. Represents 2382ae6f7882SJeremy Morse // the incoming value to the function. 2383ae6f7882SJeremy Morse MTracker->setMPhis(0); 2384ae6f7882SJeremy Morse for (auto Location : MTracker->locations()) 2385ae6f7882SJeremy Morse MInLocs[0][Location.Idx.asU64()] = Location.Value; 2386ae6f7882SJeremy Morse 2387ae6f7882SJeremy Morse SmallPtrSet<const MachineBasicBlock *, 16> Visited; 2388ae6f7882SJeremy Morse while (!Worklist.empty() || !Pending.empty()) { 2389ae6f7882SJeremy Morse // Vector for storing the evaluated block transfer function. 2390ae6f7882SJeremy Morse SmallVector<std::pair<LocIdx, ValueIDNum>, 32> ToRemap; 2391ae6f7882SJeremy Morse 2392ae6f7882SJeremy Morse while (!Worklist.empty()) { 2393ae6f7882SJeremy Morse MachineBasicBlock *MBB = OrderToBB[Worklist.top()]; 2394ae6f7882SJeremy Morse CurBB = MBB->getNumber(); 2395ae6f7882SJeremy Morse Worklist.pop(); 2396ae6f7882SJeremy Morse 2397ae6f7882SJeremy Morse // Join the values in all predecessor blocks. 2398ae6f7882SJeremy Morse bool InLocsChanged, DowngradeOccurred; 2399ae6f7882SJeremy Morse std::tie(InLocsChanged, DowngradeOccurred) = 2400ae6f7882SJeremy Morse mlocJoin(*MBB, Visited, MOutLocs, MInLocs[CurBB]); 2401ae6f7882SJeremy Morse InLocsChanged |= Visited.insert(MBB).second; 2402ae6f7882SJeremy Morse 2403ae6f7882SJeremy Morse // If a downgrade occurred, book us in for re-examination on the next 2404ae6f7882SJeremy Morse // iteration. 2405ae6f7882SJeremy Morse if (DowngradeOccurred && OnPending.insert(MBB).second) 2406ae6f7882SJeremy Morse Pending.push(BBToOrder[MBB]); 2407ae6f7882SJeremy Morse 2408ae6f7882SJeremy Morse // Don't examine transfer function if we've visited this loc at least 2409ae6f7882SJeremy Morse // once, and inlocs haven't changed. 2410ae6f7882SJeremy Morse if (!InLocsChanged) 2411ae6f7882SJeremy Morse continue; 2412ae6f7882SJeremy Morse 2413ae6f7882SJeremy Morse // Load the current set of live-ins into MLocTracker. 2414ae6f7882SJeremy Morse MTracker->loadFromArray(MInLocs[CurBB], CurBB); 2415ae6f7882SJeremy Morse 2416ae6f7882SJeremy Morse // Each element of the transfer function can be a new def, or a read of 2417ae6f7882SJeremy Morse // a live-in value. Evaluate each element, and store to "ToRemap". 2418ae6f7882SJeremy Morse ToRemap.clear(); 2419ae6f7882SJeremy Morse for (auto &P : MLocTransfer[CurBB]) { 2420ae6f7882SJeremy Morse if (P.second.getBlock() == CurBB && P.second.isPHI()) { 2421ae6f7882SJeremy Morse // This is a movement of whatever was live in. Read it. 2422ae6f7882SJeremy Morse ValueIDNum NewID = MTracker->getNumAtPos(P.second.getLoc()); 2423ae6f7882SJeremy Morse ToRemap.push_back(std::make_pair(P.first, NewID)); 2424ae6f7882SJeremy Morse } else { 2425ae6f7882SJeremy Morse // It's a def. Just set it. 2426ae6f7882SJeremy Morse assert(P.second.getBlock() == CurBB); 2427ae6f7882SJeremy Morse ToRemap.push_back(std::make_pair(P.first, P.second)); 2428ae6f7882SJeremy Morse } 2429ae6f7882SJeremy Morse } 2430ae6f7882SJeremy Morse 2431ae6f7882SJeremy Morse // Commit the transfer function changes into mloc tracker, which 2432ae6f7882SJeremy Morse // transforms the contents of the MLocTracker into the live-outs. 2433ae6f7882SJeremy Morse for (auto &P : ToRemap) 2434ae6f7882SJeremy Morse MTracker->setMLoc(P.first, P.second); 2435ae6f7882SJeremy Morse 2436ae6f7882SJeremy Morse // Now copy out-locs from mloc tracker into out-loc vector, checking 2437ae6f7882SJeremy Morse // whether changes have occurred. These changes can have come from both 2438ae6f7882SJeremy Morse // the transfer function, and mlocJoin. 2439ae6f7882SJeremy Morse bool OLChanged = false; 2440ae6f7882SJeremy Morse for (auto Location : MTracker->locations()) { 2441ae6f7882SJeremy Morse OLChanged |= MOutLocs[CurBB][Location.Idx.asU64()] != Location.Value; 2442ae6f7882SJeremy Morse MOutLocs[CurBB][Location.Idx.asU64()] = Location.Value; 2443ae6f7882SJeremy Morse } 2444ae6f7882SJeremy Morse 2445ae6f7882SJeremy Morse MTracker->reset(); 2446ae6f7882SJeremy Morse 2447ae6f7882SJeremy Morse // No need to examine successors again if out-locs didn't change. 2448ae6f7882SJeremy Morse if (!OLChanged) 2449ae6f7882SJeremy Morse continue; 2450ae6f7882SJeremy Morse 2451ae6f7882SJeremy Morse // All successors should be visited: put any back-edges on the pending 2452ae6f7882SJeremy Morse // list for the next dataflow iteration, and any other successors to be 2453ae6f7882SJeremy Morse // visited this iteration, if they're not going to be already. 2454ae6f7882SJeremy Morse for (auto s : MBB->successors()) { 2455ae6f7882SJeremy Morse // Does branching to this successor represent a back-edge? 2456ae6f7882SJeremy Morse if (BBToOrder[s] > BBToOrder[MBB]) { 2457ae6f7882SJeremy Morse // No: visit it during this dataflow iteration. 2458ae6f7882SJeremy Morse if (OnWorklist.insert(s).second) 2459ae6f7882SJeremy Morse Worklist.push(BBToOrder[s]); 2460ae6f7882SJeremy Morse } else { 2461ae6f7882SJeremy Morse // Yes: visit it on the next iteration. 2462ae6f7882SJeremy Morse if (OnPending.insert(s).second) 2463ae6f7882SJeremy Morse Pending.push(BBToOrder[s]); 2464ae6f7882SJeremy Morse } 2465ae6f7882SJeremy Morse } 2466ae6f7882SJeremy Morse } 2467ae6f7882SJeremy Morse 2468ae6f7882SJeremy Morse Worklist.swap(Pending); 2469ae6f7882SJeremy Morse std::swap(OnPending, OnWorklist); 2470ae6f7882SJeremy Morse OnPending.clear(); 2471ae6f7882SJeremy Morse // At this point, pending must be empty, since it was just the empty 2472ae6f7882SJeremy Morse // worklist 2473ae6f7882SJeremy Morse assert(Pending.empty() && "Pending should be empty"); 2474ae6f7882SJeremy Morse } 2475ae6f7882SJeremy Morse 2476ae6f7882SJeremy Morse // Once all the live-ins don't change on mlocJoin(), we've reached a 2477ae6f7882SJeremy Morse // fixedpoint. 2478ae6f7882SJeremy Morse } 2479ae6f7882SJeremy Morse 2480ae6f7882SJeremy Morse bool InstrRefBasedLDV::vlocDowngradeLattice( 2481ae6f7882SJeremy Morse const MachineBasicBlock &MBB, const DbgValue &OldLiveInLocation, 2482ae6f7882SJeremy Morse const SmallVectorImpl<InValueT> &Values, unsigned CurBlockRPONum) { 2483ae6f7882SJeremy Morse // Ranking value preference: see file level comment, the highest rank is 2484ae6f7882SJeremy Morse // a plain def, followed by PHI values in reverse post-order. Numerically, 2485ae6f7882SJeremy Morse // we assign all defs the rank '0', all PHIs their blocks RPO number plus 2486ae6f7882SJeremy Morse // one, and consider the lowest value the highest ranked. 2487ae6f7882SJeremy Morse int OldLiveInRank = BBNumToRPO[OldLiveInLocation.ID.getBlock()] + 1; 2488ae6f7882SJeremy Morse if (!OldLiveInLocation.ID.isPHI()) 2489ae6f7882SJeremy Morse OldLiveInRank = 0; 2490ae6f7882SJeremy Morse 2491ae6f7882SJeremy Morse // Allow any unresolvable conflict to be over-ridden. 2492ae6f7882SJeremy Morse if (OldLiveInLocation.Kind == DbgValue::NoVal) { 2493ae6f7882SJeremy Morse // Although if it was an unresolvable conflict from _this_ block, then 2494ae6f7882SJeremy Morse // all other seeking of downgrades and PHIs must have failed before hand. 2495ae6f7882SJeremy Morse if (OldLiveInLocation.BlockNo == (unsigned)MBB.getNumber()) 2496ae6f7882SJeremy Morse return false; 2497ae6f7882SJeremy Morse OldLiveInRank = INT_MIN; 2498ae6f7882SJeremy Morse } 2499ae6f7882SJeremy Morse 2500ae6f7882SJeremy Morse auto &InValue = *Values[0].second; 2501ae6f7882SJeremy Morse 2502ae6f7882SJeremy Morse if (InValue.Kind == DbgValue::Const || InValue.Kind == DbgValue::NoVal) 2503ae6f7882SJeremy Morse return false; 2504ae6f7882SJeremy Morse 2505ae6f7882SJeremy Morse unsigned ThisRPO = BBNumToRPO[InValue.ID.getBlock()]; 2506ae6f7882SJeremy Morse int ThisRank = ThisRPO + 1; 2507ae6f7882SJeremy Morse if (!InValue.ID.isPHI()) 2508ae6f7882SJeremy Morse ThisRank = 0; 2509ae6f7882SJeremy Morse 2510ae6f7882SJeremy Morse // Too far down the lattice? 2511ae6f7882SJeremy Morse if (ThisRPO >= CurBlockRPONum) 2512ae6f7882SJeremy Morse return false; 2513ae6f7882SJeremy Morse 2514ae6f7882SJeremy Morse // Higher in the lattice than what we've already explored? 2515ae6f7882SJeremy Morse if (ThisRank <= OldLiveInRank) 2516ae6f7882SJeremy Morse return false; 2517ae6f7882SJeremy Morse 2518ae6f7882SJeremy Morse return true; 2519ae6f7882SJeremy Morse } 2520ae6f7882SJeremy Morse 2521ae6f7882SJeremy Morse std::tuple<Optional<ValueIDNum>, bool> InstrRefBasedLDV::pickVPHILoc( 2522ae6f7882SJeremy Morse MachineBasicBlock &MBB, const DebugVariable &Var, const LiveIdxT &LiveOuts, 2523ae6f7882SJeremy Morse ValueIDNum **MOutLocs, ValueIDNum **MInLocs, 2524ae6f7882SJeremy Morse const SmallVectorImpl<MachineBasicBlock *> &BlockOrders) { 2525ae6f7882SJeremy Morse // Collect a set of locations from predecessor where its live-out value can 2526ae6f7882SJeremy Morse // be found. 2527ae6f7882SJeremy Morse SmallVector<SmallVector<LocIdx, 4>, 8> Locs; 2528ae6f7882SJeremy Morse unsigned NumLocs = MTracker->getNumLocs(); 2529ae6f7882SJeremy Morse unsigned BackEdgesStart = 0; 2530ae6f7882SJeremy Morse 2531ae6f7882SJeremy Morse for (auto p : BlockOrders) { 2532ae6f7882SJeremy Morse // Pick out where backedges start in the list of predecessors. Relies on 2533ae6f7882SJeremy Morse // BlockOrders being sorted by RPO. 2534ae6f7882SJeremy Morse if (BBToOrder[p] < BBToOrder[&MBB]) 2535ae6f7882SJeremy Morse ++BackEdgesStart; 2536ae6f7882SJeremy Morse 2537ae6f7882SJeremy Morse // For each predecessor, create a new set of locations. 2538ae6f7882SJeremy Morse Locs.resize(Locs.size() + 1); 2539ae6f7882SJeremy Morse unsigned ThisBBNum = p->getNumber(); 2540ae6f7882SJeremy Morse auto LiveOutMap = LiveOuts.find(p); 2541ae6f7882SJeremy Morse if (LiveOutMap == LiveOuts.end()) 2542ae6f7882SJeremy Morse // This predecessor isn't in scope, it must have no live-in/live-out 2543ae6f7882SJeremy Morse // locations. 2544ae6f7882SJeremy Morse continue; 2545ae6f7882SJeremy Morse 2546ae6f7882SJeremy Morse auto It = LiveOutMap->second->find(Var); 2547ae6f7882SJeremy Morse if (It == LiveOutMap->second->end()) 2548ae6f7882SJeremy Morse // There's no value recorded for this variable in this predecessor, 2549ae6f7882SJeremy Morse // leave an empty set of locations. 2550ae6f7882SJeremy Morse continue; 2551ae6f7882SJeremy Morse 2552ae6f7882SJeremy Morse const DbgValue &OutVal = It->second; 2553ae6f7882SJeremy Morse 2554ae6f7882SJeremy Morse if (OutVal.Kind == DbgValue::Const || OutVal.Kind == DbgValue::NoVal) 2555ae6f7882SJeremy Morse // Consts and no-values cannot have locations we can join on. 2556ae6f7882SJeremy Morse continue; 2557ae6f7882SJeremy Morse 2558ae6f7882SJeremy Morse assert(OutVal.Kind == DbgValue::Proposed || OutVal.Kind == DbgValue::Def); 2559ae6f7882SJeremy Morse ValueIDNum ValToLookFor = OutVal.ID; 2560ae6f7882SJeremy Morse 2561ae6f7882SJeremy Morse // Search the live-outs of the predecessor for the specified value. 2562ae6f7882SJeremy Morse for (unsigned int I = 0; I < NumLocs; ++I) { 2563ae6f7882SJeremy Morse if (MOutLocs[ThisBBNum][I] == ValToLookFor) 2564ae6f7882SJeremy Morse Locs.back().push_back(LocIdx(I)); 2565ae6f7882SJeremy Morse } 2566ae6f7882SJeremy Morse } 2567ae6f7882SJeremy Morse 2568ae6f7882SJeremy Morse // If there were no locations at all, return an empty result. 2569ae6f7882SJeremy Morse if (Locs.empty()) 257093af3704SJeremy Morse return std::tuple<Optional<ValueIDNum>, bool>(None, false); 2571ae6f7882SJeremy Morse 2572ae6f7882SJeremy Morse // Lambda for seeking a common location within a range of location-sets. 2573cca049adSDjordje Todorovic using LocsIt = SmallVector<SmallVector<LocIdx, 4>, 8>::iterator; 2574ae6f7882SJeremy Morse auto SeekLocation = 2575ae6f7882SJeremy Morse [&Locs](llvm::iterator_range<LocsIt> SearchRange) -> Optional<LocIdx> { 2576ae6f7882SJeremy Morse // Starting with the first set of locations, take the intersection with 2577ae6f7882SJeremy Morse // subsequent sets. 2578ae6f7882SJeremy Morse SmallVector<LocIdx, 4> base = Locs[0]; 2579ae6f7882SJeremy Morse for (auto &S : SearchRange) { 2580ae6f7882SJeremy Morse SmallVector<LocIdx, 4> new_base; 2581ae6f7882SJeremy Morse std::set_intersection(base.begin(), base.end(), S.begin(), S.end(), 2582ae6f7882SJeremy Morse std::inserter(new_base, new_base.begin())); 2583ae6f7882SJeremy Morse base = new_base; 2584ae6f7882SJeremy Morse } 2585ae6f7882SJeremy Morse if (base.empty()) 2586ae6f7882SJeremy Morse return None; 2587ae6f7882SJeremy Morse 2588ae6f7882SJeremy Morse // We now have a set of LocIdxes that contain the right output value in 2589ae6f7882SJeremy Morse // each of the predecessors. Pick the lowest; if there's a register loc, 2590ae6f7882SJeremy Morse // that'll be it. 2591ae6f7882SJeremy Morse return *base.begin(); 2592ae6f7882SJeremy Morse }; 2593ae6f7882SJeremy Morse 2594ae6f7882SJeremy Morse // Search for a common location for all predecessors. If we can't, then fall 2595ae6f7882SJeremy Morse // back to only finding a common location between non-backedge predecessors. 2596ae6f7882SJeremy Morse bool ValidForAllLocs = true; 2597ae6f7882SJeremy Morse auto TheLoc = SeekLocation(Locs); 2598ae6f7882SJeremy Morse if (!TheLoc) { 2599ae6f7882SJeremy Morse ValidForAllLocs = false; 2600ae6f7882SJeremy Morse TheLoc = 2601ae6f7882SJeremy Morse SeekLocation(make_range(Locs.begin(), Locs.begin() + BackEdgesStart)); 2602ae6f7882SJeremy Morse } 2603ae6f7882SJeremy Morse 2604ae6f7882SJeremy Morse if (!TheLoc) 260593af3704SJeremy Morse return std::tuple<Optional<ValueIDNum>, bool>(None, false); 2606ae6f7882SJeremy Morse 2607ae6f7882SJeremy Morse // Return a PHI-value-number for the found location. 2608ae6f7882SJeremy Morse LocIdx L = *TheLoc; 2609ae6f7882SJeremy Morse ValueIDNum PHIVal = {(unsigned)MBB.getNumber(), 0, L}; 261093af3704SJeremy Morse return std::tuple<Optional<ValueIDNum>, bool>(PHIVal, ValidForAllLocs); 2611ae6f7882SJeremy Morse } 2612ae6f7882SJeremy Morse 2613ae6f7882SJeremy Morse std::tuple<bool, bool> InstrRefBasedLDV::vlocJoin( 2614ae6f7882SJeremy Morse MachineBasicBlock &MBB, LiveIdxT &VLOCOutLocs, LiveIdxT &VLOCInLocs, 2615ae6f7882SJeremy Morse SmallPtrSet<const MachineBasicBlock *, 16> *VLOCVisited, unsigned BBNum, 2616ae6f7882SJeremy Morse const SmallSet<DebugVariable, 4> &AllVars, ValueIDNum **MOutLocs, 2617ae6f7882SJeremy Morse ValueIDNum **MInLocs, 2618ae6f7882SJeremy Morse SmallPtrSet<const MachineBasicBlock *, 8> &InScopeBlocks, 2619ae6f7882SJeremy Morse SmallPtrSet<const MachineBasicBlock *, 8> &BlocksToExplore, 2620ae6f7882SJeremy Morse DenseMap<DebugVariable, DbgValue> &InLocsT) { 2621ae6f7882SJeremy Morse bool DowngradeOccurred = false; 2622ae6f7882SJeremy Morse 2623ae6f7882SJeremy Morse // To emulate VarLocBasedImpl, process this block if it's not in scope but 2624ae6f7882SJeremy Morse // _does_ assign a variable value. No live-ins for this scope are transferred 2625ae6f7882SJeremy Morse // in though, so we can return immediately. 2626ae6f7882SJeremy Morse if (InScopeBlocks.count(&MBB) == 0 && !ArtificialBlocks.count(&MBB)) { 2627ae6f7882SJeremy Morse if (VLOCVisited) 2628ae6f7882SJeremy Morse return std::tuple<bool, bool>(true, false); 2629ae6f7882SJeremy Morse return std::tuple<bool, bool>(false, false); 2630ae6f7882SJeremy Morse } 2631ae6f7882SJeremy Morse 2632ae6f7882SJeremy Morse LLVM_DEBUG(dbgs() << "join MBB: " << MBB.getNumber() << "\n"); 2633ae6f7882SJeremy Morse bool Changed = false; 2634ae6f7882SJeremy Morse 2635ae6f7882SJeremy Morse // Find any live-ins computed in a prior iteration. 2636ae6f7882SJeremy Morse auto ILSIt = VLOCInLocs.find(&MBB); 2637ae6f7882SJeremy Morse assert(ILSIt != VLOCInLocs.end()); 2638ae6f7882SJeremy Morse auto &ILS = *ILSIt->second; 2639ae6f7882SJeremy Morse 2640ae6f7882SJeremy Morse // Order predecessors by RPOT order, for exploring them in that order. 2641ae6f7882SJeremy Morse SmallVector<MachineBasicBlock *, 8> BlockOrders; 2642ae6f7882SJeremy Morse for (auto p : MBB.predecessors()) 2643ae6f7882SJeremy Morse BlockOrders.push_back(p); 2644ae6f7882SJeremy Morse 2645ae6f7882SJeremy Morse auto Cmp = [&](MachineBasicBlock *A, MachineBasicBlock *B) { 2646ae6f7882SJeremy Morse return BBToOrder[A] < BBToOrder[B]; 2647ae6f7882SJeremy Morse }; 2648ae6f7882SJeremy Morse 2649ae6f7882SJeremy Morse llvm::sort(BlockOrders.begin(), BlockOrders.end(), Cmp); 2650ae6f7882SJeremy Morse 2651ae6f7882SJeremy Morse unsigned CurBlockRPONum = BBToOrder[&MBB]; 2652ae6f7882SJeremy Morse 2653ae6f7882SJeremy Morse // Force a re-visit to loop heads in the first dataflow iteration. 2654ae6f7882SJeremy Morse // FIXME: if we could "propose" Const values this wouldn't be needed, 2655ae6f7882SJeremy Morse // because they'd need to be confirmed before being emitted. 2656ae6f7882SJeremy Morse if (!BlockOrders.empty() && 2657ae6f7882SJeremy Morse BBToOrder[BlockOrders[BlockOrders.size() - 1]] >= CurBlockRPONum && 2658ae6f7882SJeremy Morse VLOCVisited) 2659ae6f7882SJeremy Morse DowngradeOccurred = true; 2660ae6f7882SJeremy Morse 2661ae6f7882SJeremy Morse auto ConfirmValue = [&InLocsT](const DebugVariable &DV, DbgValue VR) { 2662ae6f7882SJeremy Morse auto Result = InLocsT.insert(std::make_pair(DV, VR)); 2663ae6f7882SJeremy Morse (void)Result; 2664ae6f7882SJeremy Morse assert(Result.second); 2665ae6f7882SJeremy Morse }; 2666ae6f7882SJeremy Morse 2667ae6f7882SJeremy Morse auto ConfirmNoVal = [&ConfirmValue, &MBB](const DebugVariable &Var, const DbgValueProperties &Properties) { 2668ae6f7882SJeremy Morse DbgValue NoLocPHIVal(MBB.getNumber(), Properties, DbgValue::NoVal); 2669ae6f7882SJeremy Morse 2670ae6f7882SJeremy Morse ConfirmValue(Var, NoLocPHIVal); 2671ae6f7882SJeremy Morse }; 2672ae6f7882SJeremy Morse 2673ae6f7882SJeremy Morse // Attempt to join the values for each variable. 2674ae6f7882SJeremy Morse for (auto &Var : AllVars) { 2675ae6f7882SJeremy Morse // Collect all the DbgValues for this variable. 2676ae6f7882SJeremy Morse SmallVector<InValueT, 8> Values; 2677ae6f7882SJeremy Morse bool Bail = false; 2678ae6f7882SJeremy Morse unsigned BackEdgesStart = 0; 2679ae6f7882SJeremy Morse for (auto p : BlockOrders) { 2680ae6f7882SJeremy Morse // If the predecessor isn't in scope / to be explored, we'll never be 2681ae6f7882SJeremy Morse // able to join any locations. 2682ae6f7882SJeremy Morse if (BlocksToExplore.find(p) == BlocksToExplore.end()) { 2683ae6f7882SJeremy Morse Bail = true; 2684ae6f7882SJeremy Morse break; 2685ae6f7882SJeremy Morse } 2686ae6f7882SJeremy Morse 2687ae6f7882SJeremy Morse // Don't attempt to handle unvisited predecessors: they're implicitly 2688ae6f7882SJeremy Morse // "unknown"s in the lattice. 2689ae6f7882SJeremy Morse if (VLOCVisited && !VLOCVisited->count(p)) 2690ae6f7882SJeremy Morse continue; 2691ae6f7882SJeremy Morse 2692ae6f7882SJeremy Morse // If the predecessors OutLocs is absent, there's not much we can do. 2693ae6f7882SJeremy Morse auto OL = VLOCOutLocs.find(p); 2694ae6f7882SJeremy Morse if (OL == VLOCOutLocs.end()) { 2695ae6f7882SJeremy Morse Bail = true; 2696ae6f7882SJeremy Morse break; 2697ae6f7882SJeremy Morse } 2698ae6f7882SJeremy Morse 2699ae6f7882SJeremy Morse // No live-out value for this predecessor also means we can't produce 2700ae6f7882SJeremy Morse // a joined value. 2701ae6f7882SJeremy Morse auto VIt = OL->second->find(Var); 2702ae6f7882SJeremy Morse if (VIt == OL->second->end()) { 2703ae6f7882SJeremy Morse Bail = true; 2704ae6f7882SJeremy Morse break; 2705ae6f7882SJeremy Morse } 2706ae6f7882SJeremy Morse 2707ae6f7882SJeremy Morse // Keep track of where back-edges begin in the Values vector. Relies on 2708ae6f7882SJeremy Morse // BlockOrders being sorted by RPO. 2709ae6f7882SJeremy Morse unsigned ThisBBRPONum = BBToOrder[p]; 2710ae6f7882SJeremy Morse if (ThisBBRPONum < CurBlockRPONum) 2711ae6f7882SJeremy Morse ++BackEdgesStart; 2712ae6f7882SJeremy Morse 2713ae6f7882SJeremy Morse Values.push_back(std::make_pair(p, &VIt->second)); 2714ae6f7882SJeremy Morse } 2715ae6f7882SJeremy Morse 2716ae6f7882SJeremy Morse // If there were no values, or one of the predecessors couldn't have a 2717ae6f7882SJeremy Morse // value, then give up immediately. It's not safe to produce a live-in 2718ae6f7882SJeremy Morse // value. 2719ae6f7882SJeremy Morse if (Bail || Values.size() == 0) 2720ae6f7882SJeremy Morse continue; 2721ae6f7882SJeremy Morse 2722ae6f7882SJeremy Morse // Enumeration identifying the current state of the predecessors values. 2723ae6f7882SJeremy Morse enum { 2724ae6f7882SJeremy Morse Unset = 0, 2725ae6f7882SJeremy Morse Agreed, // All preds agree on the variable value. 2726ae6f7882SJeremy Morse PropDisagree, // All preds agree, but the value kind is Proposed in some. 2727ae6f7882SJeremy Morse BEDisagree, // Only back-edges disagree on variable value. 2728ae6f7882SJeremy Morse PHINeeded, // Non-back-edge predecessors have conflicing values. 2729ae6f7882SJeremy Morse NoSolution // Conflicting Value metadata makes solution impossible. 2730ae6f7882SJeremy Morse } OurState = Unset; 2731ae6f7882SJeremy Morse 2732ae6f7882SJeremy Morse // All (non-entry) blocks have at least one non-backedge predecessor. 2733ae6f7882SJeremy Morse // Pick the variable value from the first of these, to compare against 2734ae6f7882SJeremy Morse // all others. 2735ae6f7882SJeremy Morse const DbgValue &FirstVal = *Values[0].second; 2736ae6f7882SJeremy Morse const ValueIDNum &FirstID = FirstVal.ID; 2737ae6f7882SJeremy Morse 2738ae6f7882SJeremy Morse // Scan for variable values that can't be resolved: if they have different 2739ae6f7882SJeremy Morse // DIExpressions, different indirectness, or are mixed constants / 2740ae6f7882SJeremy Morse // non-constants. 2741ae6f7882SJeremy Morse for (auto &V : Values) { 2742ae6f7882SJeremy Morse if (V.second->Properties != FirstVal.Properties) 2743ae6f7882SJeremy Morse OurState = NoSolution; 2744ae6f7882SJeremy Morse if (V.second->Kind == DbgValue::Const && FirstVal.Kind != DbgValue::Const) 2745ae6f7882SJeremy Morse OurState = NoSolution; 2746ae6f7882SJeremy Morse } 2747ae6f7882SJeremy Morse 2748ae6f7882SJeremy Morse // Flags diagnosing _how_ the values disagree. 2749ae6f7882SJeremy Morse bool NonBackEdgeDisagree = false; 2750ae6f7882SJeremy Morse bool DisagreeOnPHINess = false; 2751ae6f7882SJeremy Morse bool IDDisagree = false; 2752ae6f7882SJeremy Morse bool Disagree = false; 2753ae6f7882SJeremy Morse if (OurState == Unset) { 2754ae6f7882SJeremy Morse for (auto &V : Values) { 2755ae6f7882SJeremy Morse if (*V.second == FirstVal) 2756ae6f7882SJeremy Morse continue; // No disagreement. 2757ae6f7882SJeremy Morse 2758ae6f7882SJeremy Morse Disagree = true; 2759ae6f7882SJeremy Morse 2760ae6f7882SJeremy Morse // Flag whether the value number actually diagrees. 2761ae6f7882SJeremy Morse if (V.second->ID != FirstID) 2762ae6f7882SJeremy Morse IDDisagree = true; 2763ae6f7882SJeremy Morse 2764ae6f7882SJeremy Morse // Distinguish whether disagreement happens in backedges or not. 2765ae6f7882SJeremy Morse // Relies on Values (and BlockOrders) being sorted by RPO. 2766ae6f7882SJeremy Morse unsigned ThisBBRPONum = BBToOrder[V.first]; 2767ae6f7882SJeremy Morse if (ThisBBRPONum < CurBlockRPONum) 2768ae6f7882SJeremy Morse NonBackEdgeDisagree = true; 2769ae6f7882SJeremy Morse 2770ae6f7882SJeremy Morse // Is there a difference in whether the value is definite or only 2771ae6f7882SJeremy Morse // proposed? 2772ae6f7882SJeremy Morse if (V.second->Kind != FirstVal.Kind && 2773ae6f7882SJeremy Morse (V.second->Kind == DbgValue::Proposed || 2774ae6f7882SJeremy Morse V.second->Kind == DbgValue::Def) && 2775ae6f7882SJeremy Morse (FirstVal.Kind == DbgValue::Proposed || 2776ae6f7882SJeremy Morse FirstVal.Kind == DbgValue::Def)) 2777ae6f7882SJeremy Morse DisagreeOnPHINess = true; 2778ae6f7882SJeremy Morse } 2779ae6f7882SJeremy Morse 2780ae6f7882SJeremy Morse // Collect those flags together and determine an overall state for 2781ae6f7882SJeremy Morse // what extend the predecessors agree on a live-in value. 2782ae6f7882SJeremy Morse if (!Disagree) 2783ae6f7882SJeremy Morse OurState = Agreed; 2784ae6f7882SJeremy Morse else if (!IDDisagree && DisagreeOnPHINess) 2785ae6f7882SJeremy Morse OurState = PropDisagree; 2786ae6f7882SJeremy Morse else if (!NonBackEdgeDisagree) 2787ae6f7882SJeremy Morse OurState = BEDisagree; 2788ae6f7882SJeremy Morse else 2789ae6f7882SJeremy Morse OurState = PHINeeded; 2790ae6f7882SJeremy Morse } 2791ae6f7882SJeremy Morse 2792ae6f7882SJeremy Morse // An extra indicator: if we only disagree on whether the value is a 2793ae6f7882SJeremy Morse // Def, or proposed, then also flag whether that disagreement happens 2794ae6f7882SJeremy Morse // in backedges only. 2795ae6f7882SJeremy Morse bool PropOnlyInBEs = Disagree && !IDDisagree && DisagreeOnPHINess && 2796ae6f7882SJeremy Morse !NonBackEdgeDisagree && FirstVal.Kind == DbgValue::Def; 2797ae6f7882SJeremy Morse 2798ae6f7882SJeremy Morse const auto &Properties = FirstVal.Properties; 2799ae6f7882SJeremy Morse 2800ae6f7882SJeremy Morse auto OldLiveInIt = ILS.find(Var); 2801ae6f7882SJeremy Morse const DbgValue *OldLiveInLocation = 2802ae6f7882SJeremy Morse (OldLiveInIt != ILS.end()) ? &OldLiveInIt->second : nullptr; 2803ae6f7882SJeremy Morse 2804ae6f7882SJeremy Morse bool OverRide = false; 2805ae6f7882SJeremy Morse if (OurState == BEDisagree && OldLiveInLocation) { 2806ae6f7882SJeremy Morse // Only backedges disagree: we can consider downgrading. If there was a 2807ae6f7882SJeremy Morse // previous live-in value, use it to work out whether the current 2808ae6f7882SJeremy Morse // incoming value represents a lattice downgrade or not. 2809ae6f7882SJeremy Morse OverRide = 2810ae6f7882SJeremy Morse vlocDowngradeLattice(MBB, *OldLiveInLocation, Values, CurBlockRPONum); 2811ae6f7882SJeremy Morse } 2812ae6f7882SJeremy Morse 2813ae6f7882SJeremy Morse // Use the current state of predecessor agreement and other flags to work 2814ae6f7882SJeremy Morse // out what to do next. Possibilities include: 2815ae6f7882SJeremy Morse // * Accept a value all predecessors agree on, or accept one that 2816ae6f7882SJeremy Morse // represents a step down the exploration lattice, 2817ae6f7882SJeremy Morse // * Use a PHI value number, if one can be found, 2818ae6f7882SJeremy Morse // * Propose a PHI value number, and see if it gets confirmed later, 2819ae6f7882SJeremy Morse // * Emit a 'NoVal' value, indicating we couldn't resolve anything. 2820ae6f7882SJeremy Morse if (OurState == Agreed) { 2821ae6f7882SJeremy Morse // Easiest solution: all predecessors agree on the variable value. 2822ae6f7882SJeremy Morse ConfirmValue(Var, FirstVal); 2823ae6f7882SJeremy Morse } else if (OurState == BEDisagree && OverRide) { 2824ae6f7882SJeremy Morse // Only backedges disagree, and the other predecessors have produced 2825ae6f7882SJeremy Morse // a new live-in value further down the exploration lattice. 2826ae6f7882SJeremy Morse DowngradeOccurred = true; 2827ae6f7882SJeremy Morse ConfirmValue(Var, FirstVal); 2828ae6f7882SJeremy Morse } else if (OurState == PropDisagree) { 2829ae6f7882SJeremy Morse // Predecessors agree on value, but some say it's only a proposed value. 2830ae6f7882SJeremy Morse // Propagate it as proposed: unless it was proposed in this block, in 2831ae6f7882SJeremy Morse // which case we're able to confirm the value. 2832ae6f7882SJeremy Morse if (FirstID.getBlock() == (uint64_t)MBB.getNumber() && FirstID.isPHI()) { 2833ae6f7882SJeremy Morse ConfirmValue(Var, DbgValue(FirstID, Properties, DbgValue::Def)); 2834ae6f7882SJeremy Morse } else if (PropOnlyInBEs) { 2835ae6f7882SJeremy Morse // If only backedges disagree, a higher (in RPO) block confirmed this 2836ae6f7882SJeremy Morse // location, and we need to propagate it into this loop. 2837ae6f7882SJeremy Morse ConfirmValue(Var, DbgValue(FirstID, Properties, DbgValue::Def)); 2838ae6f7882SJeremy Morse } else { 2839ae6f7882SJeremy Morse // Otherwise; a Def meeting a Proposed is still a Proposed. 2840ae6f7882SJeremy Morse ConfirmValue(Var, DbgValue(FirstID, Properties, DbgValue::Proposed)); 2841ae6f7882SJeremy Morse } 2842ae6f7882SJeremy Morse } else if ((OurState == PHINeeded || OurState == BEDisagree)) { 2843ae6f7882SJeremy Morse // Predecessors disagree and can't be downgraded: this can only be 2844ae6f7882SJeremy Morse // solved with a PHI. Use pickVPHILoc to go look for one. 2845ae6f7882SJeremy Morse Optional<ValueIDNum> VPHI; 2846ae6f7882SJeremy Morse bool AllEdgesVPHI = false; 2847ae6f7882SJeremy Morse std::tie(VPHI, AllEdgesVPHI) = 2848ae6f7882SJeremy Morse pickVPHILoc(MBB, Var, VLOCOutLocs, MOutLocs, MInLocs, BlockOrders); 2849ae6f7882SJeremy Morse 2850ae6f7882SJeremy Morse if (VPHI && AllEdgesVPHI) { 2851ae6f7882SJeremy Morse // There's a PHI value that's valid for all predecessors -- we can use 2852ae6f7882SJeremy Morse // it. If any of the non-backedge predecessors have proposed values 2853ae6f7882SJeremy Morse // though, this PHI is also only proposed, until the predecessors are 2854ae6f7882SJeremy Morse // confirmed. 2855ae6f7882SJeremy Morse DbgValue::KindT K = DbgValue::Def; 2856ae6f7882SJeremy Morse for (unsigned int I = 0; I < BackEdgesStart; ++I) 2857ae6f7882SJeremy Morse if (Values[I].second->Kind == DbgValue::Proposed) 2858ae6f7882SJeremy Morse K = DbgValue::Proposed; 2859ae6f7882SJeremy Morse 2860ae6f7882SJeremy Morse ConfirmValue(Var, DbgValue(*VPHI, Properties, K)); 2861ae6f7882SJeremy Morse } else if (VPHI) { 2862ae6f7882SJeremy Morse // There's a PHI value, but it's only legal for backedges. Leave this 2863ae6f7882SJeremy Morse // as a proposed PHI value: it might come back on the backedges, 2864ae6f7882SJeremy Morse // and allow us to confirm it in the future. 2865ae6f7882SJeremy Morse DbgValue NoBEValue = DbgValue(*VPHI, Properties, DbgValue::Proposed); 2866ae6f7882SJeremy Morse ConfirmValue(Var, NoBEValue); 2867ae6f7882SJeremy Morse } else { 2868ae6f7882SJeremy Morse ConfirmNoVal(Var, Properties); 2869ae6f7882SJeremy Morse } 2870ae6f7882SJeremy Morse } else { 2871ae6f7882SJeremy Morse // Otherwise: we don't know. Emit a "phi but no real loc" phi. 2872ae6f7882SJeremy Morse ConfirmNoVal(Var, Properties); 2873ae6f7882SJeremy Morse } 2874ae6f7882SJeremy Morse } 2875ae6f7882SJeremy Morse 2876ae6f7882SJeremy Morse // Store newly calculated in-locs into VLOCInLocs, if they've changed. 2877ae6f7882SJeremy Morse Changed = ILS != InLocsT; 2878ae6f7882SJeremy Morse if (Changed) 2879ae6f7882SJeremy Morse ILS = InLocsT; 2880ae6f7882SJeremy Morse 2881ae6f7882SJeremy Morse return std::tuple<bool, bool>(Changed, DowngradeOccurred); 2882ae6f7882SJeremy Morse } 2883ae6f7882SJeremy Morse 2884ae6f7882SJeremy Morse void InstrRefBasedLDV::vlocDataflow( 2885ae6f7882SJeremy Morse const LexicalScope *Scope, const DILocation *DILoc, 2886ae6f7882SJeremy Morse const SmallSet<DebugVariable, 4> &VarsWeCareAbout, 2887ae6f7882SJeremy Morse SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks, LiveInsT &Output, 2888ae6f7882SJeremy Morse ValueIDNum **MOutLocs, ValueIDNum **MInLocs, 2889ae6f7882SJeremy Morse SmallVectorImpl<VLocTracker> &AllTheVLocs) { 2890ae6f7882SJeremy Morse // This method is much like mlocDataflow: but focuses on a single 2891ae6f7882SJeremy Morse // LexicalScope at a time. Pick out a set of blocks and variables that are 2892ae6f7882SJeremy Morse // to have their value assignments solved, then run our dataflow algorithm 2893ae6f7882SJeremy Morse // until a fixedpoint is reached. 2894ae6f7882SJeremy Morse std::priority_queue<unsigned int, std::vector<unsigned int>, 2895ae6f7882SJeremy Morse std::greater<unsigned int>> 2896ae6f7882SJeremy Morse Worklist, Pending; 2897ae6f7882SJeremy Morse SmallPtrSet<MachineBasicBlock *, 16> OnWorklist, OnPending; 2898ae6f7882SJeremy Morse 2899ae6f7882SJeremy Morse // The set of blocks we'll be examining. 2900ae6f7882SJeremy Morse SmallPtrSet<const MachineBasicBlock *, 8> BlocksToExplore; 2901ae6f7882SJeremy Morse 2902ae6f7882SJeremy Morse // The order in which to examine them (RPO). 2903ae6f7882SJeremy Morse SmallVector<MachineBasicBlock *, 8> BlockOrders; 2904ae6f7882SJeremy Morse 2905ae6f7882SJeremy Morse // RPO ordering function. 2906ae6f7882SJeremy Morse auto Cmp = [&](MachineBasicBlock *A, MachineBasicBlock *B) { 2907ae6f7882SJeremy Morse return BBToOrder[A] < BBToOrder[B]; 2908ae6f7882SJeremy Morse }; 2909ae6f7882SJeremy Morse 2910ae6f7882SJeremy Morse LS.getMachineBasicBlocks(DILoc, BlocksToExplore); 2911ae6f7882SJeremy Morse 2912ae6f7882SJeremy Morse // A separate container to distinguish "blocks we're exploring" versus 2913ae6f7882SJeremy Morse // "blocks that are potentially in scope. See comment at start of vlocJoin. 2914ae6f7882SJeremy Morse SmallPtrSet<const MachineBasicBlock *, 8> InScopeBlocks = BlocksToExplore; 2915ae6f7882SJeremy Morse 2916ae6f7882SJeremy Morse // Old LiveDebugValues tracks variable locations that come out of blocks 2917ae6f7882SJeremy Morse // not in scope, where DBG_VALUEs occur. This is something we could 2918ae6f7882SJeremy Morse // legitimately ignore, but lets allow it for now. 2919ae6f7882SJeremy Morse if (EmulateOldLDV) 2920ae6f7882SJeremy Morse BlocksToExplore.insert(AssignBlocks.begin(), AssignBlocks.end()); 2921ae6f7882SJeremy Morse 2922ae6f7882SJeremy Morse // We also need to propagate variable values through any artificial blocks 2923ae6f7882SJeremy Morse // that immediately follow blocks in scope. 2924ae6f7882SJeremy Morse DenseSet<const MachineBasicBlock *> ToAdd; 2925ae6f7882SJeremy Morse 2926ae6f7882SJeremy Morse // Helper lambda: For a given block in scope, perform a depth first search 2927ae6f7882SJeremy Morse // of all the artificial successors, adding them to the ToAdd collection. 2928ae6f7882SJeremy Morse auto AccumulateArtificialBlocks = 2929ae6f7882SJeremy Morse [this, &ToAdd, &BlocksToExplore, 2930ae6f7882SJeremy Morse &InScopeBlocks](const MachineBasicBlock *MBB) { 2931ae6f7882SJeremy Morse // Depth-first-search state: each node is a block and which successor 2932ae6f7882SJeremy Morse // we're currently exploring. 2933ae6f7882SJeremy Morse SmallVector<std::pair<const MachineBasicBlock *, 2934ae6f7882SJeremy Morse MachineBasicBlock::const_succ_iterator>, 2935ae6f7882SJeremy Morse 8> 2936ae6f7882SJeremy Morse DFS; 2937ae6f7882SJeremy Morse 2938ae6f7882SJeremy Morse // Find any artificial successors not already tracked. 2939ae6f7882SJeremy Morse for (auto *succ : MBB->successors()) { 2940ae6f7882SJeremy Morse if (BlocksToExplore.count(succ) || InScopeBlocks.count(succ)) 2941ae6f7882SJeremy Morse continue; 2942ae6f7882SJeremy Morse if (!ArtificialBlocks.count(succ)) 2943ae6f7882SJeremy Morse continue; 2944ae6f7882SJeremy Morse DFS.push_back(std::make_pair(succ, succ->succ_begin())); 2945ae6f7882SJeremy Morse ToAdd.insert(succ); 2946ae6f7882SJeremy Morse } 2947ae6f7882SJeremy Morse 2948ae6f7882SJeremy Morse // Search all those blocks, depth first. 2949ae6f7882SJeremy Morse while (!DFS.empty()) { 2950ae6f7882SJeremy Morse const MachineBasicBlock *CurBB = DFS.back().first; 2951ae6f7882SJeremy Morse MachineBasicBlock::const_succ_iterator &CurSucc = DFS.back().second; 2952ae6f7882SJeremy Morse // Walk back if we've explored this blocks successors to the end. 2953ae6f7882SJeremy Morse if (CurSucc == CurBB->succ_end()) { 2954ae6f7882SJeremy Morse DFS.pop_back(); 2955ae6f7882SJeremy Morse continue; 2956ae6f7882SJeremy Morse } 2957ae6f7882SJeremy Morse 2958ae6f7882SJeremy Morse // If the current successor is artificial and unexplored, descend into 2959ae6f7882SJeremy Morse // it. 2960ae6f7882SJeremy Morse if (!ToAdd.count(*CurSucc) && ArtificialBlocks.count(*CurSucc)) { 2961ae6f7882SJeremy Morse DFS.push_back(std::make_pair(*CurSucc, (*CurSucc)->succ_begin())); 2962ae6f7882SJeremy Morse ToAdd.insert(*CurSucc); 2963ae6f7882SJeremy Morse continue; 2964ae6f7882SJeremy Morse } 2965ae6f7882SJeremy Morse 2966ae6f7882SJeremy Morse ++CurSucc; 2967ae6f7882SJeremy Morse } 2968ae6f7882SJeremy Morse }; 2969ae6f7882SJeremy Morse 2970ae6f7882SJeremy Morse // Search in-scope blocks and those containing a DBG_VALUE from this scope 2971ae6f7882SJeremy Morse // for artificial successors. 2972ae6f7882SJeremy Morse for (auto *MBB : BlocksToExplore) 2973ae6f7882SJeremy Morse AccumulateArtificialBlocks(MBB); 2974ae6f7882SJeremy Morse for (auto *MBB : InScopeBlocks) 2975ae6f7882SJeremy Morse AccumulateArtificialBlocks(MBB); 2976ae6f7882SJeremy Morse 2977ae6f7882SJeremy Morse BlocksToExplore.insert(ToAdd.begin(), ToAdd.end()); 2978ae6f7882SJeremy Morse InScopeBlocks.insert(ToAdd.begin(), ToAdd.end()); 2979ae6f7882SJeremy Morse 2980ae6f7882SJeremy Morse // Single block scope: not interesting! No propagation at all. Note that 2981ae6f7882SJeremy Morse // this could probably go above ArtificialBlocks without damage, but 2982ae6f7882SJeremy Morse // that then produces output differences from original-live-debug-values, 2983ae6f7882SJeremy Morse // which propagates from a single block into many artificial ones. 2984ae6f7882SJeremy Morse if (BlocksToExplore.size() == 1) 2985ae6f7882SJeremy Morse return; 2986ae6f7882SJeremy Morse 2987ae6f7882SJeremy Morse // Picks out relevants blocks RPO order and sort them. 2988ae6f7882SJeremy Morse for (auto *MBB : BlocksToExplore) 2989ae6f7882SJeremy Morse BlockOrders.push_back(const_cast<MachineBasicBlock *>(MBB)); 2990ae6f7882SJeremy Morse 2991ae6f7882SJeremy Morse llvm::sort(BlockOrders.begin(), BlockOrders.end(), Cmp); 2992ae6f7882SJeremy Morse unsigned NumBlocks = BlockOrders.size(); 2993ae6f7882SJeremy Morse 2994ae6f7882SJeremy Morse // Allocate some vectors for storing the live ins and live outs. Large. 2995ae6f7882SJeremy Morse SmallVector<DenseMap<DebugVariable, DbgValue>, 32> LiveIns, LiveOuts; 2996ae6f7882SJeremy Morse LiveIns.resize(NumBlocks); 2997ae6f7882SJeremy Morse LiveOuts.resize(NumBlocks); 2998ae6f7882SJeremy Morse 2999ae6f7882SJeremy Morse // Produce by-MBB indexes of live-in/live-outs, to ease lookup within 3000ae6f7882SJeremy Morse // vlocJoin. 3001ae6f7882SJeremy Morse LiveIdxT LiveOutIdx, LiveInIdx; 3002ae6f7882SJeremy Morse LiveOutIdx.reserve(NumBlocks); 3003ae6f7882SJeremy Morse LiveInIdx.reserve(NumBlocks); 3004ae6f7882SJeremy Morse for (unsigned I = 0; I < NumBlocks; ++I) { 3005ae6f7882SJeremy Morse LiveOutIdx[BlockOrders[I]] = &LiveOuts[I]; 3006ae6f7882SJeremy Morse LiveInIdx[BlockOrders[I]] = &LiveIns[I]; 3007ae6f7882SJeremy Morse } 3008ae6f7882SJeremy Morse 3009ae6f7882SJeremy Morse for (auto *MBB : BlockOrders) { 3010ae6f7882SJeremy Morse Worklist.push(BBToOrder[MBB]); 3011ae6f7882SJeremy Morse OnWorklist.insert(MBB); 3012ae6f7882SJeremy Morse } 3013ae6f7882SJeremy Morse 3014ae6f7882SJeremy Morse // Iterate over all the blocks we selected, propagating variable values. 3015ae6f7882SJeremy Morse bool FirstTrip = true; 3016ae6f7882SJeremy Morse SmallPtrSet<const MachineBasicBlock *, 16> VLOCVisited; 3017ae6f7882SJeremy Morse while (!Worklist.empty() || !Pending.empty()) { 3018ae6f7882SJeremy Morse while (!Worklist.empty()) { 3019ae6f7882SJeremy Morse auto *MBB = OrderToBB[Worklist.top()]; 3020ae6f7882SJeremy Morse CurBB = MBB->getNumber(); 3021ae6f7882SJeremy Morse Worklist.pop(); 3022ae6f7882SJeremy Morse 3023ae6f7882SJeremy Morse DenseMap<DebugVariable, DbgValue> JoinedInLocs; 3024ae6f7882SJeremy Morse 3025ae6f7882SJeremy Morse // Join values from predecessors. Updates LiveInIdx, and writes output 3026ae6f7882SJeremy Morse // into JoinedInLocs. 3027ae6f7882SJeremy Morse bool InLocsChanged, DowngradeOccurred; 3028ae6f7882SJeremy Morse std::tie(InLocsChanged, DowngradeOccurred) = vlocJoin( 3029ae6f7882SJeremy Morse *MBB, LiveOutIdx, LiveInIdx, (FirstTrip) ? &VLOCVisited : nullptr, 3030ae6f7882SJeremy Morse CurBB, VarsWeCareAbout, MOutLocs, MInLocs, InScopeBlocks, 3031ae6f7882SJeremy Morse BlocksToExplore, JoinedInLocs); 3032ae6f7882SJeremy Morse 3033ae6f7882SJeremy Morse bool FirstVisit = VLOCVisited.insert(MBB).second; 3034ae6f7882SJeremy Morse 3035ae6f7882SJeremy Morse // Always explore transfer function if inlocs changed, or if we've not 3036ae6f7882SJeremy Morse // visited this block before. 3037ae6f7882SJeremy Morse InLocsChanged |= FirstVisit; 3038ae6f7882SJeremy Morse 3039ae6f7882SJeremy Morse // If a downgrade occurred, book us in for re-examination on the next 3040ae6f7882SJeremy Morse // iteration. 3041ae6f7882SJeremy Morse if (DowngradeOccurred && OnPending.insert(MBB).second) 3042ae6f7882SJeremy Morse Pending.push(BBToOrder[MBB]); 3043ae6f7882SJeremy Morse 3044ae6f7882SJeremy Morse if (!InLocsChanged) 3045ae6f7882SJeremy Morse continue; 3046ae6f7882SJeremy Morse 3047ae6f7882SJeremy Morse // Do transfer function. 3048ab93e710SJeremy Morse auto &VTracker = AllTheVLocs[MBB->getNumber()]; 3049ae6f7882SJeremy Morse for (auto &Transfer : VTracker.Vars) { 3050ae6f7882SJeremy Morse // Is this var we're mangling in this scope? 3051ae6f7882SJeremy Morse if (VarsWeCareAbout.count(Transfer.first)) { 3052ae6f7882SJeremy Morse // Erase on empty transfer (DBG_VALUE $noreg). 3053ae6f7882SJeremy Morse if (Transfer.second.Kind == DbgValue::Undef) { 3054ae6f7882SJeremy Morse JoinedInLocs.erase(Transfer.first); 3055ae6f7882SJeremy Morse } else { 3056ae6f7882SJeremy Morse // Insert new variable value; or overwrite. 3057ae6f7882SJeremy Morse auto NewValuePair = std::make_pair(Transfer.first, Transfer.second); 3058ae6f7882SJeremy Morse auto Result = JoinedInLocs.insert(NewValuePair); 3059ae6f7882SJeremy Morse if (!Result.second) 3060ae6f7882SJeremy Morse Result.first->second = Transfer.second; 3061ae6f7882SJeremy Morse } 3062ae6f7882SJeremy Morse } 3063ae6f7882SJeremy Morse } 3064ae6f7882SJeremy Morse 3065ae6f7882SJeremy Morse // Did the live-out locations change? 3066ae6f7882SJeremy Morse bool OLChanged = JoinedInLocs != *LiveOutIdx[MBB]; 3067ae6f7882SJeremy Morse 3068ae6f7882SJeremy Morse // If they haven't changed, there's no need to explore further. 3069ae6f7882SJeremy Morse if (!OLChanged) 3070ae6f7882SJeremy Morse continue; 3071ae6f7882SJeremy Morse 3072ae6f7882SJeremy Morse // Commit to the live-out record. 3073ae6f7882SJeremy Morse *LiveOutIdx[MBB] = JoinedInLocs; 3074ae6f7882SJeremy Morse 3075ae6f7882SJeremy Morse // We should visit all successors. Ensure we'll visit any non-backedge 3076ae6f7882SJeremy Morse // successors during this dataflow iteration; book backedge successors 3077ae6f7882SJeremy Morse // to be visited next time around. 3078ae6f7882SJeremy Morse for (auto s : MBB->successors()) { 3079ae6f7882SJeremy Morse // Ignore out of scope / not-to-be-explored successors. 3080ae6f7882SJeremy Morse if (LiveInIdx.find(s) == LiveInIdx.end()) 3081ae6f7882SJeremy Morse continue; 3082ae6f7882SJeremy Morse 3083ae6f7882SJeremy Morse if (BBToOrder[s] > BBToOrder[MBB]) { 3084ae6f7882SJeremy Morse if (OnWorklist.insert(s).second) 3085ae6f7882SJeremy Morse Worklist.push(BBToOrder[s]); 3086ae6f7882SJeremy Morse } else if (OnPending.insert(s).second && (FirstTrip || OLChanged)) { 3087ae6f7882SJeremy Morse Pending.push(BBToOrder[s]); 3088ae6f7882SJeremy Morse } 3089ae6f7882SJeremy Morse } 3090ae6f7882SJeremy Morse } 3091ae6f7882SJeremy Morse Worklist.swap(Pending); 3092ae6f7882SJeremy Morse std::swap(OnWorklist, OnPending); 3093ae6f7882SJeremy Morse OnPending.clear(); 3094ae6f7882SJeremy Morse assert(Pending.empty()); 3095ae6f7882SJeremy Morse FirstTrip = false; 3096ae6f7882SJeremy Morse } 3097ae6f7882SJeremy Morse 3098ae6f7882SJeremy Morse // Dataflow done. Now what? Save live-ins. Ignore any that are still marked 3099ae6f7882SJeremy Morse // as being variable-PHIs, because those did not have their machine-PHI 3100ae6f7882SJeremy Morse // value confirmed. Such variable values are places that could have been 3101ae6f7882SJeremy Morse // PHIs, but are not. 3102ae6f7882SJeremy Morse for (auto *MBB : BlockOrders) { 3103ae6f7882SJeremy Morse auto &VarMap = *LiveInIdx[MBB]; 3104ae6f7882SJeremy Morse for (auto &P : VarMap) { 3105ae6f7882SJeremy Morse if (P.second.Kind == DbgValue::Proposed || 3106ae6f7882SJeremy Morse P.second.Kind == DbgValue::NoVal) 3107ae6f7882SJeremy Morse continue; 3108ae6f7882SJeremy Morse Output[MBB->getNumber()].push_back(P); 3109ae6f7882SJeremy Morse } 3110ae6f7882SJeremy Morse } 3111ae6f7882SJeremy Morse 3112ae6f7882SJeremy Morse BlockOrders.clear(); 3113ae6f7882SJeremy Morse BlocksToExplore.clear(); 3114ae6f7882SJeremy Morse } 3115ae6f7882SJeremy Morse 31162ac06241SFangrui Song #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 3117ae6f7882SJeremy Morse void InstrRefBasedLDV::dump_mloc_transfer( 3118ae6f7882SJeremy Morse const MLocTransferMap &mloc_transfer) const { 3119ae6f7882SJeremy Morse for (auto &P : mloc_transfer) { 3120ae6f7882SJeremy Morse std::string foo = MTracker->LocIdxToName(P.first); 3121ae6f7882SJeremy Morse std::string bar = MTracker->IDAsString(P.second); 3122ae6f7882SJeremy Morse dbgs() << "Loc " << foo << " --> " << bar << "\n"; 3123ae6f7882SJeremy Morse } 3124ae6f7882SJeremy Morse } 31252ac06241SFangrui Song #endif 3126ae6f7882SJeremy Morse 3127ae6f7882SJeremy Morse void InstrRefBasedLDV::emitLocations( 3128ae6f7882SJeremy Morse MachineFunction &MF, LiveInsT SavedLiveIns, ValueIDNum **MInLocs, 3129ae6f7882SJeremy Morse DenseMap<DebugVariable, unsigned> &AllVarsNumbering) { 3130ae6f7882SJeremy Morse TTracker = new TransferTracker(TII, MTracker, MF, *TRI, CalleeSavedRegs); 3131ae6f7882SJeremy Morse unsigned NumLocs = MTracker->getNumLocs(); 3132ae6f7882SJeremy Morse 3133ae6f7882SJeremy Morse // For each block, load in the machine value locations and variable value 3134ae6f7882SJeremy Morse // live-ins, then step through each instruction in the block. New DBG_VALUEs 3135ae6f7882SJeremy Morse // to be inserted will be created along the way. 3136ae6f7882SJeremy Morse for (MachineBasicBlock &MBB : MF) { 3137ae6f7882SJeremy Morse unsigned bbnum = MBB.getNumber(); 3138ae6f7882SJeremy Morse MTracker->reset(); 3139ae6f7882SJeremy Morse MTracker->loadFromArray(MInLocs[bbnum], bbnum); 3140ae6f7882SJeremy Morse TTracker->loadInlocs(MBB, MInLocs[bbnum], SavedLiveIns[MBB.getNumber()], 3141ae6f7882SJeremy Morse NumLocs); 3142ae6f7882SJeremy Morse 3143ae6f7882SJeremy Morse CurBB = bbnum; 3144ae6f7882SJeremy Morse CurInst = 1; 3145ae6f7882SJeremy Morse for (auto &MI : MBB) { 3146ae6f7882SJeremy Morse process(MI); 3147b1b2c6abSJeremy Morse TTracker->checkInstForNewValues(CurInst, MI.getIterator()); 3148ae6f7882SJeremy Morse ++CurInst; 3149ae6f7882SJeremy Morse } 3150ae6f7882SJeremy Morse } 3151ae6f7882SJeremy Morse 3152ae6f7882SJeremy Morse // We have to insert DBG_VALUEs in a consistent order, otherwise they appeaer 3153ae6f7882SJeremy Morse // in DWARF in different orders. Use the order that they appear when walking 3154ae6f7882SJeremy Morse // through each block / each instruction, stored in AllVarsNumbering. 3155ae6f7882SJeremy Morse auto OrderDbgValues = [&](const MachineInstr *A, 3156ae6f7882SJeremy Morse const MachineInstr *B) -> bool { 3157ae6f7882SJeremy Morse DebugVariable VarA(A->getDebugVariable(), A->getDebugExpression(), 3158ae6f7882SJeremy Morse A->getDebugLoc()->getInlinedAt()); 3159ae6f7882SJeremy Morse DebugVariable VarB(B->getDebugVariable(), B->getDebugExpression(), 3160ae6f7882SJeremy Morse B->getDebugLoc()->getInlinedAt()); 3161ae6f7882SJeremy Morse return AllVarsNumbering.find(VarA)->second < 3162ae6f7882SJeremy Morse AllVarsNumbering.find(VarB)->second; 3163ae6f7882SJeremy Morse }; 3164ae6f7882SJeremy Morse 3165ae6f7882SJeremy Morse // Go through all the transfers recorded in the TransferTracker -- this is 3166ae6f7882SJeremy Morse // both the live-ins to a block, and any movements of values that happen 3167ae6f7882SJeremy Morse // in the middle. 3168ae6f7882SJeremy Morse for (auto &P : TTracker->Transfers) { 3169ae6f7882SJeremy Morse // Sort them according to appearance order. 3170ae6f7882SJeremy Morse llvm::sort(P.Insts.begin(), P.Insts.end(), OrderDbgValues); 3171ae6f7882SJeremy Morse // Insert either before or after the designated point... 3172ae6f7882SJeremy Morse if (P.MBB) { 3173ae6f7882SJeremy Morse MachineBasicBlock &MBB = *P.MBB; 3174ae6f7882SJeremy Morse for (auto *MI : P.Insts) { 3175ae6f7882SJeremy Morse MBB.insert(P.Pos, MI); 3176ae6f7882SJeremy Morse } 3177ae6f7882SJeremy Morse } else { 3178ae6f7882SJeremy Morse MachineBasicBlock &MBB = *P.Pos->getParent(); 3179ae6f7882SJeremy Morse for (auto *MI : P.Insts) { 3180ae6f7882SJeremy Morse MBB.insertAfter(P.Pos, MI); 3181ae6f7882SJeremy Morse } 3182ae6f7882SJeremy Morse } 3183ae6f7882SJeremy Morse } 3184ae6f7882SJeremy Morse } 3185ae6f7882SJeremy Morse 3186ae6f7882SJeremy Morse void InstrRefBasedLDV::initialSetup(MachineFunction &MF) { 3187ae6f7882SJeremy Morse // Build some useful data structures. 3188ae6f7882SJeremy Morse auto hasNonArtificialLocation = [](const MachineInstr &MI) -> bool { 3189ae6f7882SJeremy Morse if (const DebugLoc &DL = MI.getDebugLoc()) 3190ae6f7882SJeremy Morse return DL.getLine() != 0; 3191ae6f7882SJeremy Morse return false; 3192ae6f7882SJeremy Morse }; 3193ae6f7882SJeremy Morse // Collect a set of all the artificial blocks. 3194ae6f7882SJeremy Morse for (auto &MBB : MF) 3195ae6f7882SJeremy Morse if (none_of(MBB.instrs(), hasNonArtificialLocation)) 3196ae6f7882SJeremy Morse ArtificialBlocks.insert(&MBB); 3197ae6f7882SJeremy Morse 3198ae6f7882SJeremy Morse // Compute mappings of block <=> RPO order. 3199ae6f7882SJeremy Morse ReversePostOrderTraversal<MachineFunction *> RPOT(&MF); 3200ae6f7882SJeremy Morse unsigned int RPONumber = 0; 3201ae6f7882SJeremy Morse for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) { 3202ae6f7882SJeremy Morse OrderToBB[RPONumber] = *RI; 3203ae6f7882SJeremy Morse BBToOrder[*RI] = RPONumber; 3204ae6f7882SJeremy Morse BBNumToRPO[(*RI)->getNumber()] = RPONumber; 3205ae6f7882SJeremy Morse ++RPONumber; 3206ae6f7882SJeremy Morse } 3207ae6f7882SJeremy Morse } 3208ae6f7882SJeremy Morse 3209ae6f7882SJeremy Morse /// Calculate the liveness information for the given machine function and 3210ae6f7882SJeremy Morse /// extend ranges across basic blocks. 3211ae6f7882SJeremy Morse bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF, 3212ae6f7882SJeremy Morse TargetPassConfig *TPC) { 3213ae6f7882SJeremy Morse // No subprogram means this function contains no debuginfo. 3214ae6f7882SJeremy Morse if (!MF.getFunction().getSubprogram()) 3215ae6f7882SJeremy Morse return false; 3216ae6f7882SJeremy Morse 3217ae6f7882SJeremy Morse LLVM_DEBUG(dbgs() << "\nDebug Range Extension\n"); 3218ae6f7882SJeremy Morse this->TPC = TPC; 3219ae6f7882SJeremy Morse 3220ae6f7882SJeremy Morse TRI = MF.getSubtarget().getRegisterInfo(); 3221ae6f7882SJeremy Morse TII = MF.getSubtarget().getInstrInfo(); 3222ae6f7882SJeremy Morse TFI = MF.getSubtarget().getFrameLowering(); 3223ae6f7882SJeremy Morse TFI->getCalleeSaves(MF, CalleeSavedRegs); 3224ae6f7882SJeremy Morse LS.initialize(MF); 3225ae6f7882SJeremy Morse 3226ae6f7882SJeremy Morse MTracker = 3227ae6f7882SJeremy Morse new MLocTracker(MF, *TII, *TRI, *MF.getSubtarget().getTargetLowering()); 3228ae6f7882SJeremy Morse VTracker = nullptr; 3229ae6f7882SJeremy Morse TTracker = nullptr; 3230ae6f7882SJeremy Morse 3231ae6f7882SJeremy Morse SmallVector<MLocTransferMap, 32> MLocTransfer; 3232ae6f7882SJeremy Morse SmallVector<VLocTracker, 8> vlocs; 3233ae6f7882SJeremy Morse LiveInsT SavedLiveIns; 3234ae6f7882SJeremy Morse 3235ae6f7882SJeremy Morse int MaxNumBlocks = -1; 3236ae6f7882SJeremy Morse for (auto &MBB : MF) 3237ae6f7882SJeremy Morse MaxNumBlocks = std::max(MBB.getNumber(), MaxNumBlocks); 3238ae6f7882SJeremy Morse assert(MaxNumBlocks >= 0); 3239ae6f7882SJeremy Morse ++MaxNumBlocks; 3240ae6f7882SJeremy Morse 3241ae6f7882SJeremy Morse MLocTransfer.resize(MaxNumBlocks); 3242ae6f7882SJeremy Morse vlocs.resize(MaxNumBlocks); 3243ae6f7882SJeremy Morse SavedLiveIns.resize(MaxNumBlocks); 3244ae6f7882SJeremy Morse 3245ae6f7882SJeremy Morse initialSetup(MF); 3246ae6f7882SJeremy Morse 3247ab93e710SJeremy Morse produceMLocTransferFunction(MF, MLocTransfer, MaxNumBlocks); 3248ae6f7882SJeremy Morse 3249ae6f7882SJeremy Morse // Allocate and initialize two array-of-arrays for the live-in and live-out 3250ae6f7882SJeremy Morse // machine values. The outer dimension is the block number; while the inner 3251ae6f7882SJeremy Morse // dimension is a LocIdx from MLocTracker. 3252ae6f7882SJeremy Morse ValueIDNum **MOutLocs = new ValueIDNum *[MaxNumBlocks]; 3253ae6f7882SJeremy Morse ValueIDNum **MInLocs = new ValueIDNum *[MaxNumBlocks]; 3254ae6f7882SJeremy Morse unsigned NumLocs = MTracker->getNumLocs(); 3255ae6f7882SJeremy Morse for (int i = 0; i < MaxNumBlocks; ++i) { 3256ae6f7882SJeremy Morse MOutLocs[i] = new ValueIDNum[NumLocs]; 3257ae6f7882SJeremy Morse MInLocs[i] = new ValueIDNum[NumLocs]; 3258ae6f7882SJeremy Morse } 3259ae6f7882SJeremy Morse 3260ae6f7882SJeremy Morse // Solve the machine value dataflow problem using the MLocTransfer function, 3261ae6f7882SJeremy Morse // storing the computed live-ins / live-outs into the array-of-arrays. We use 3262ae6f7882SJeremy Morse // both live-ins and live-outs for decision making in the variable value 3263ae6f7882SJeremy Morse // dataflow problem. 3264ae6f7882SJeremy Morse mlocDataflow(MInLocs, MOutLocs, MLocTransfer); 3265ae6f7882SJeremy Morse 3266ab93e710SJeremy Morse // Walk back through each block / instruction, collecting DBG_VALUE 3267ab93e710SJeremy Morse // instructions and recording what machine value their operands refer to. 3268ab93e710SJeremy Morse for (auto &OrderPair : OrderToBB) { 3269ab93e710SJeremy Morse MachineBasicBlock &MBB = *OrderPair.second; 3270ab93e710SJeremy Morse CurBB = MBB.getNumber(); 3271ab93e710SJeremy Morse VTracker = &vlocs[CurBB]; 3272ab93e710SJeremy Morse VTracker->MBB = &MBB; 3273ab93e710SJeremy Morse MTracker->loadFromArray(MInLocs[CurBB], CurBB); 3274ab93e710SJeremy Morse CurInst = 1; 3275ab93e710SJeremy Morse for (auto &MI : MBB) { 3276ab93e710SJeremy Morse process(MI); 3277ab93e710SJeremy Morse ++CurInst; 3278ab93e710SJeremy Morse } 3279ab93e710SJeremy Morse MTracker->reset(); 3280ab93e710SJeremy Morse } 3281ab93e710SJeremy Morse 3282ae6f7882SJeremy Morse // Number all variables in the order that they appear, to be used as a stable 3283ae6f7882SJeremy Morse // insertion order later. 3284ae6f7882SJeremy Morse DenseMap<DebugVariable, unsigned> AllVarsNumbering; 3285ae6f7882SJeremy Morse 3286ae6f7882SJeremy Morse // Map from one LexicalScope to all the variables in that scope. 3287ae6f7882SJeremy Morse DenseMap<const LexicalScope *, SmallSet<DebugVariable, 4>> ScopeToVars; 3288ae6f7882SJeremy Morse 3289ae6f7882SJeremy Morse // Map from One lexical scope to all blocks in that scope. 3290ae6f7882SJeremy Morse DenseMap<const LexicalScope *, SmallPtrSet<MachineBasicBlock *, 4>> 3291ae6f7882SJeremy Morse ScopeToBlocks; 3292ae6f7882SJeremy Morse 3293ae6f7882SJeremy Morse // Store a DILocation that describes a scope. 3294ae6f7882SJeremy Morse DenseMap<const LexicalScope *, const DILocation *> ScopeToDILocation; 3295ae6f7882SJeremy Morse 3296ae6f7882SJeremy Morse // To mirror old LiveDebugValues, enumerate variables in RPOT order. Otherwise 3297ae6f7882SJeremy Morse // the order is unimportant, it just has to be stable. 3298ae6f7882SJeremy Morse for (unsigned int I = 0; I < OrderToBB.size(); ++I) { 3299ae6f7882SJeremy Morse auto *MBB = OrderToBB[I]; 3300ae6f7882SJeremy Morse auto *VTracker = &vlocs[MBB->getNumber()]; 3301ae6f7882SJeremy Morse // Collect each variable with a DBG_VALUE in this block. 3302ae6f7882SJeremy Morse for (auto &idx : VTracker->Vars) { 3303ae6f7882SJeremy Morse const auto &Var = idx.first; 3304ae6f7882SJeremy Morse const DILocation *ScopeLoc = VTracker->Scopes[Var]; 3305ae6f7882SJeremy Morse assert(ScopeLoc != nullptr); 3306ae6f7882SJeremy Morse auto *Scope = LS.findLexicalScope(ScopeLoc); 3307ae6f7882SJeremy Morse 3308ae6f7882SJeremy Morse // No insts in scope -> shouldn't have been recorded. 3309ae6f7882SJeremy Morse assert(Scope != nullptr); 3310ae6f7882SJeremy Morse 3311ae6f7882SJeremy Morse AllVarsNumbering.insert(std::make_pair(Var, AllVarsNumbering.size())); 3312ae6f7882SJeremy Morse ScopeToVars[Scope].insert(Var); 3313ae6f7882SJeremy Morse ScopeToBlocks[Scope].insert(VTracker->MBB); 3314ae6f7882SJeremy Morse ScopeToDILocation[Scope] = ScopeLoc; 3315ae6f7882SJeremy Morse } 3316ae6f7882SJeremy Morse } 3317ae6f7882SJeremy Morse 3318ae6f7882SJeremy Morse // OK. Iterate over scopes: there might be something to be said for 3319ae6f7882SJeremy Morse // ordering them by size/locality, but that's for the future. For each scope, 3320ae6f7882SJeremy Morse // solve the variable value problem, producing a map of variables to values 3321ae6f7882SJeremy Morse // in SavedLiveIns. 3322ae6f7882SJeremy Morse for (auto &P : ScopeToVars) { 3323ae6f7882SJeremy Morse vlocDataflow(P.first, ScopeToDILocation[P.first], P.second, 3324ae6f7882SJeremy Morse ScopeToBlocks[P.first], SavedLiveIns, MOutLocs, MInLocs, 3325ae6f7882SJeremy Morse vlocs); 3326ae6f7882SJeremy Morse } 3327ae6f7882SJeremy Morse 3328ae6f7882SJeremy Morse // Using the computed value locations and variable values for each block, 3329ae6f7882SJeremy Morse // create the DBG_VALUE instructions representing the extended variable 3330ae6f7882SJeremy Morse // locations. 3331ae6f7882SJeremy Morse emitLocations(MF, SavedLiveIns, MInLocs, AllVarsNumbering); 3332ae6f7882SJeremy Morse 3333ae6f7882SJeremy Morse for (int Idx = 0; Idx < MaxNumBlocks; ++Idx) { 3334ae6f7882SJeremy Morse delete[] MOutLocs[Idx]; 3335ae6f7882SJeremy Morse delete[] MInLocs[Idx]; 3336ae6f7882SJeremy Morse } 3337ae6f7882SJeremy Morse delete[] MOutLocs; 3338ae6f7882SJeremy Morse delete[] MInLocs; 3339ae6f7882SJeremy Morse 3340ae6f7882SJeremy Morse // Did we actually make any changes? If we created any DBG_VALUEs, then yes. 3341ae6f7882SJeremy Morse bool Changed = TTracker->Transfers.size() != 0; 3342ae6f7882SJeremy Morse 3343ae6f7882SJeremy Morse delete MTracker; 33440caeaff1SJeremy Morse delete TTracker; 33450caeaff1SJeremy Morse MTracker = nullptr; 3346ae6f7882SJeremy Morse VTracker = nullptr; 3347ae6f7882SJeremy Morse TTracker = nullptr; 3348ae6f7882SJeremy Morse 3349ae6f7882SJeremy Morse ArtificialBlocks.clear(); 3350ae6f7882SJeremy Morse OrderToBB.clear(); 3351ae6f7882SJeremy Morse BBToOrder.clear(); 3352ae6f7882SJeremy Morse BBNumToRPO.clear(); 335368f47157SJeremy Morse DebugInstrNumToInstr.clear(); 3354ae6f7882SJeremy Morse 3355ae6f7882SJeremy Morse return Changed; 3356ae6f7882SJeremy Morse } 3357ae6f7882SJeremy Morse 3358ae6f7882SJeremy Morse LDVImpl *llvm::makeInstrRefBasedLiveDebugValues() { 3359ae6f7882SJeremy Morse return new InstrRefBasedLDV(); 3360ae6f7882SJeremy Morse } 3361