1e8d8bef9SDimitry Andric //===- InstrRefBasedImpl.cpp - Tracking Debug Value MIs -------------------===// 2e8d8bef9SDimitry Andric // 3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e8d8bef9SDimitry Andric // 7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 8e8d8bef9SDimitry Andric /// \file InstrRefBasedImpl.cpp 9e8d8bef9SDimitry Andric /// 10e8d8bef9SDimitry Andric /// This is a separate implementation of LiveDebugValues, see 11e8d8bef9SDimitry Andric /// LiveDebugValues.cpp and VarLocBasedImpl.cpp for more information. 12e8d8bef9SDimitry Andric /// 13e8d8bef9SDimitry Andric /// This pass propagates variable locations between basic blocks, resolving 14349cc55cSDimitry Andric /// control flow conflicts between them. The problem is SSA construction, where 15349cc55cSDimitry Andric /// each debug instruction assigns the *value* that a variable has, and every 16349cc55cSDimitry Andric /// instruction where the variable is in scope uses that variable. The resulting 17349cc55cSDimitry Andric /// map of instruction-to-value is then translated into a register (or spill) 18349cc55cSDimitry Andric /// location for each variable over each instruction. 19e8d8bef9SDimitry Andric /// 20349cc55cSDimitry Andric /// The primary difference from normal SSA construction is that we cannot 21349cc55cSDimitry Andric /// _create_ PHI values that contain variable values. CodeGen has already 22349cc55cSDimitry Andric /// completed, and we can't alter it just to make debug-info complete. Thus: 23349cc55cSDimitry Andric /// we can identify function positions where we would like a PHI value for a 24349cc55cSDimitry Andric /// variable, but must search the MachineFunction to see whether such a PHI is 25349cc55cSDimitry Andric /// available. If no such PHI exists, the variable location must be dropped. 26e8d8bef9SDimitry Andric /// 27349cc55cSDimitry Andric /// To achieve this, we perform two kinds of analysis. First, we identify 28e8d8bef9SDimitry Andric /// every value defined by every instruction (ignoring those that only move 29349cc55cSDimitry Andric /// another value), then re-compute an SSA-form representation of the 30349cc55cSDimitry Andric /// MachineFunction, using value propagation to eliminate any un-necessary 31349cc55cSDimitry Andric /// PHI values. This gives us a map of every value computed in the function, 32349cc55cSDimitry Andric /// and its location within the register file / stack. 33e8d8bef9SDimitry Andric /// 34349cc55cSDimitry Andric /// Secondly, for each variable we perform the same analysis, where each debug 35349cc55cSDimitry Andric /// instruction is considered a def, and every instruction where the variable 36349cc55cSDimitry Andric /// is in lexical scope as a use. Value propagation is used again to eliminate 37349cc55cSDimitry Andric /// any un-necessary PHIs. This gives us a map of each variable to the value 38349cc55cSDimitry Andric /// it should have in a block. 39e8d8bef9SDimitry Andric /// 40349cc55cSDimitry Andric /// Once both are complete, we have two maps for each block: 41349cc55cSDimitry Andric /// * Variables to the values they should have, 42349cc55cSDimitry Andric /// * Values to the register / spill slot they are located in. 43349cc55cSDimitry Andric /// After which we can marry-up variable values with a location, and emit 44349cc55cSDimitry Andric /// DBG_VALUE instructions specifying those locations. Variable locations may 45349cc55cSDimitry Andric /// be dropped in this process due to the desired variable value not being 46349cc55cSDimitry Andric /// resident in any machine location, or because there is no PHI value in any 47349cc55cSDimitry Andric /// location that accurately represents the desired value. The building of 48349cc55cSDimitry Andric /// location lists for each block is left to DbgEntityHistoryCalculator. 49e8d8bef9SDimitry Andric /// 50349cc55cSDimitry Andric /// This pass is kept efficient because the size of the first SSA problem 51349cc55cSDimitry Andric /// is proportional to the working-set size of the function, which the compiler 52349cc55cSDimitry Andric /// tries to keep small. (It's also proportional to the number of blocks). 53349cc55cSDimitry Andric /// Additionally, we repeatedly perform the second SSA problem analysis with 54349cc55cSDimitry Andric /// only the variables and blocks in a single lexical scope, exploiting their 55349cc55cSDimitry Andric /// locality. 56e8d8bef9SDimitry Andric /// 57e8d8bef9SDimitry Andric /// ### Terminology 58e8d8bef9SDimitry Andric /// 59e8d8bef9SDimitry Andric /// A machine location is a register or spill slot, a value is something that's 60e8d8bef9SDimitry Andric /// defined by an instruction or PHI node, while a variable value is the value 61e8d8bef9SDimitry Andric /// assigned to a variable. A variable location is a machine location, that must 62e8d8bef9SDimitry Andric /// contain the appropriate variable value. A value that is a PHI node is 63e8d8bef9SDimitry Andric /// occasionally called an mphi. 64e8d8bef9SDimitry Andric /// 65349cc55cSDimitry Andric /// The first SSA problem is the "machine value location" problem, 66e8d8bef9SDimitry Andric /// because we're determining which machine locations contain which values. 67e8d8bef9SDimitry Andric /// The "locations" are constant: what's unknown is what value they contain. 68e8d8bef9SDimitry Andric /// 69349cc55cSDimitry Andric /// The second SSA problem (the one for variables) is the "variable value 70e8d8bef9SDimitry Andric /// problem", because it's determining what values a variable has, rather than 71349cc55cSDimitry Andric /// what location those values are placed in. 72e8d8bef9SDimitry Andric /// 73e8d8bef9SDimitry Andric /// TODO: 74e8d8bef9SDimitry Andric /// Overlapping fragments 75e8d8bef9SDimitry Andric /// Entry values 76e8d8bef9SDimitry Andric /// Add back DEBUG statements for debugging this 77e8d8bef9SDimitry Andric /// Collect statistics 78e8d8bef9SDimitry Andric /// 79e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 80e8d8bef9SDimitry Andric 81e8d8bef9SDimitry Andric #include "llvm/ADT/DenseMap.h" 82e8d8bef9SDimitry Andric #include "llvm/ADT/PostOrderIterator.h" 83fe6060f1SDimitry Andric #include "llvm/ADT/STLExtras.h" 84e8d8bef9SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 85e8d8bef9SDimitry Andric #include "llvm/ADT/SmallSet.h" 86e8d8bef9SDimitry Andric #include "llvm/ADT/SmallVector.h" 87e8d8bef9SDimitry Andric #include "llvm/ADT/Statistic.h" 88349cc55cSDimitry Andric #include "llvm/Analysis/IteratedDominanceFrontier.h" 89e8d8bef9SDimitry Andric #include "llvm/CodeGen/LexicalScopes.h" 90e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 91349cc55cSDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 92e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 93e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 94e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 95e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 96e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 97fe6060f1SDimitry Andric #include "llvm/CodeGen/MachineInstrBundle.h" 98e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h" 99e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 100e8d8bef9SDimitry Andric #include "llvm/CodeGen/PseudoSourceValue.h" 101e8d8bef9SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h" 102e8d8bef9SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h" 103e8d8bef9SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 104e8d8bef9SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 105e8d8bef9SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h" 106e8d8bef9SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 107e8d8bef9SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 108e8d8bef9SDimitry Andric #include "llvm/Config/llvm-config.h" 109e8d8bef9SDimitry Andric #include "llvm/IR/DIBuilder.h" 110e8d8bef9SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 111e8d8bef9SDimitry Andric #include "llvm/IR/DebugLoc.h" 112e8d8bef9SDimitry Andric #include "llvm/IR/Function.h" 113e8d8bef9SDimitry Andric #include "llvm/IR/Module.h" 114e8d8bef9SDimitry Andric #include "llvm/InitializePasses.h" 115e8d8bef9SDimitry Andric #include "llvm/MC/MCRegisterInfo.h" 116e8d8bef9SDimitry Andric #include "llvm/Pass.h" 117e8d8bef9SDimitry Andric #include "llvm/Support/Casting.h" 118e8d8bef9SDimitry Andric #include "llvm/Support/Compiler.h" 119e8d8bef9SDimitry Andric #include "llvm/Support/Debug.h" 120e8d8bef9SDimitry Andric #include "llvm/Support/TypeSize.h" 121e8d8bef9SDimitry Andric #include "llvm/Support/raw_ostream.h" 122fe6060f1SDimitry Andric #include "llvm/Target/TargetMachine.h" 123fe6060f1SDimitry Andric #include "llvm/Transforms/Utils/SSAUpdaterImpl.h" 124e8d8bef9SDimitry Andric #include <algorithm> 125e8d8bef9SDimitry Andric #include <cassert> 126e8d8bef9SDimitry Andric #include <cstdint> 127e8d8bef9SDimitry Andric #include <functional> 128349cc55cSDimitry Andric #include <limits.h> 129349cc55cSDimitry Andric #include <limits> 130e8d8bef9SDimitry Andric #include <queue> 131e8d8bef9SDimitry Andric #include <tuple> 132e8d8bef9SDimitry Andric #include <utility> 133e8d8bef9SDimitry Andric #include <vector> 134e8d8bef9SDimitry Andric 135349cc55cSDimitry Andric #include "InstrRefBasedImpl.h" 136e8d8bef9SDimitry Andric #include "LiveDebugValues.h" 137e8d8bef9SDimitry Andric 138e8d8bef9SDimitry Andric using namespace llvm; 139349cc55cSDimitry Andric using namespace LiveDebugValues; 140e8d8bef9SDimitry Andric 141fe6060f1SDimitry Andric // SSAUpdaterImple sets DEBUG_TYPE, change it. 142fe6060f1SDimitry Andric #undef DEBUG_TYPE 143e8d8bef9SDimitry Andric #define DEBUG_TYPE "livedebugvalues" 144e8d8bef9SDimitry Andric 145e8d8bef9SDimitry Andric // Act more like the VarLoc implementation, by propagating some locations too 146e8d8bef9SDimitry Andric // far and ignoring some transfers. 147e8d8bef9SDimitry Andric static cl::opt<bool> EmulateOldLDV("emulate-old-livedebugvalues", cl::Hidden, 148e8d8bef9SDimitry Andric cl::desc("Act like old LiveDebugValues did"), 149e8d8bef9SDimitry Andric cl::init(false)); 150e8d8bef9SDimitry Andric 151e8d8bef9SDimitry Andric /// Tracker for converting machine value locations and variable values into 152e8d8bef9SDimitry Andric /// variable locations (the output of LiveDebugValues), recorded as DBG_VALUEs 153e8d8bef9SDimitry Andric /// specifying block live-in locations and transfers within blocks. 154e8d8bef9SDimitry Andric /// 155e8d8bef9SDimitry Andric /// Operating on a per-block basis, this class takes a (pre-loaded) MLocTracker 156e8d8bef9SDimitry Andric /// and must be initialized with the set of variable values that are live-in to 157e8d8bef9SDimitry Andric /// the block. The caller then repeatedly calls process(). TransferTracker picks 158e8d8bef9SDimitry Andric /// out variable locations for the live-in variable values (if there _is_ a 159e8d8bef9SDimitry Andric /// location) and creates the corresponding DBG_VALUEs. Then, as the block is 160e8d8bef9SDimitry Andric /// stepped through, transfers of values between machine locations are 161e8d8bef9SDimitry Andric /// identified and if profitable, a DBG_VALUE created. 162e8d8bef9SDimitry Andric /// 163e8d8bef9SDimitry Andric /// This is where debug use-before-defs would be resolved: a variable with an 164e8d8bef9SDimitry Andric /// unavailable value could materialize in the middle of a block, when the 165e8d8bef9SDimitry Andric /// value becomes available. Or, we could detect clobbers and re-specify the 166e8d8bef9SDimitry Andric /// variable in a backup location. (XXX these are unimplemented). 167e8d8bef9SDimitry Andric class TransferTracker { 168e8d8bef9SDimitry Andric public: 169e8d8bef9SDimitry Andric const TargetInstrInfo *TII; 170fe6060f1SDimitry Andric const TargetLowering *TLI; 171e8d8bef9SDimitry Andric /// This machine location tracker is assumed to always contain the up-to-date 172e8d8bef9SDimitry Andric /// value mapping for all machine locations. TransferTracker only reads 173e8d8bef9SDimitry Andric /// information from it. (XXX make it const?) 174e8d8bef9SDimitry Andric MLocTracker *MTracker; 175e8d8bef9SDimitry Andric MachineFunction &MF; 176fe6060f1SDimitry Andric bool ShouldEmitDebugEntryValues; 177e8d8bef9SDimitry Andric 178e8d8bef9SDimitry Andric /// Record of all changes in variable locations at a block position. Awkwardly 179e8d8bef9SDimitry Andric /// we allow inserting either before or after the point: MBB != nullptr 180e8d8bef9SDimitry Andric /// indicates it's before, otherwise after. 181e8d8bef9SDimitry Andric struct Transfer { 182fe6060f1SDimitry Andric MachineBasicBlock::instr_iterator Pos; /// Position to insert DBG_VALUes 183e8d8bef9SDimitry Andric MachineBasicBlock *MBB; /// non-null if we should insert after. 184e8d8bef9SDimitry Andric SmallVector<MachineInstr *, 4> Insts; /// Vector of DBG_VALUEs to insert. 185e8d8bef9SDimitry Andric }; 186e8d8bef9SDimitry Andric 187fe6060f1SDimitry Andric struct LocAndProperties { 188e8d8bef9SDimitry Andric LocIdx Loc; 189e8d8bef9SDimitry Andric DbgValueProperties Properties; 190fe6060f1SDimitry Andric }; 191e8d8bef9SDimitry Andric 192e8d8bef9SDimitry Andric /// Collection of transfers (DBG_VALUEs) to be inserted. 193e8d8bef9SDimitry Andric SmallVector<Transfer, 32> Transfers; 194e8d8bef9SDimitry Andric 195e8d8bef9SDimitry Andric /// Local cache of what-value-is-in-what-LocIdx. Used to identify differences 196e8d8bef9SDimitry Andric /// between TransferTrackers view of variable locations and MLocTrackers. For 197e8d8bef9SDimitry Andric /// example, MLocTracker observes all clobbers, but TransferTracker lazily 198e8d8bef9SDimitry Andric /// does not. 199349cc55cSDimitry Andric SmallVector<ValueIDNum, 32> VarLocs; 200e8d8bef9SDimitry Andric 201e8d8bef9SDimitry Andric /// Map from LocIdxes to which DebugVariables are based that location. 202e8d8bef9SDimitry Andric /// Mantained while stepping through the block. Not accurate if 203e8d8bef9SDimitry Andric /// VarLocs[Idx] != MTracker->LocIdxToIDNum[Idx]. 204349cc55cSDimitry Andric DenseMap<LocIdx, SmallSet<DebugVariable, 4>> ActiveMLocs; 205e8d8bef9SDimitry Andric 206e8d8bef9SDimitry Andric /// Map from DebugVariable to it's current location and qualifying meta 207e8d8bef9SDimitry Andric /// information. To be used in conjunction with ActiveMLocs to construct 208e8d8bef9SDimitry Andric /// enough information for the DBG_VALUEs for a particular LocIdx. 209e8d8bef9SDimitry Andric DenseMap<DebugVariable, LocAndProperties> ActiveVLocs; 210e8d8bef9SDimitry Andric 211e8d8bef9SDimitry Andric /// Temporary cache of DBG_VALUEs to be entered into the Transfers collection. 212e8d8bef9SDimitry Andric SmallVector<MachineInstr *, 4> PendingDbgValues; 213e8d8bef9SDimitry Andric 214e8d8bef9SDimitry Andric /// Record of a use-before-def: created when a value that's live-in to the 215e8d8bef9SDimitry Andric /// current block isn't available in any machine location, but it will be 216e8d8bef9SDimitry Andric /// defined in this block. 217e8d8bef9SDimitry Andric struct UseBeforeDef { 218e8d8bef9SDimitry Andric /// Value of this variable, def'd in block. 219e8d8bef9SDimitry Andric ValueIDNum ID; 220e8d8bef9SDimitry Andric /// Identity of this variable. 221e8d8bef9SDimitry Andric DebugVariable Var; 222e8d8bef9SDimitry Andric /// Additional variable properties. 223e8d8bef9SDimitry Andric DbgValueProperties Properties; 224e8d8bef9SDimitry Andric }; 225e8d8bef9SDimitry Andric 226e8d8bef9SDimitry Andric /// Map from instruction index (within the block) to the set of UseBeforeDefs 227e8d8bef9SDimitry Andric /// that become defined at that instruction. 228e8d8bef9SDimitry Andric DenseMap<unsigned, SmallVector<UseBeforeDef, 1>> UseBeforeDefs; 229e8d8bef9SDimitry Andric 230e8d8bef9SDimitry Andric /// The set of variables that are in UseBeforeDefs and can become a location 231e8d8bef9SDimitry Andric /// once the relevant value is defined. An element being erased from this 232e8d8bef9SDimitry Andric /// collection prevents the use-before-def materializing. 233e8d8bef9SDimitry Andric DenseSet<DebugVariable> UseBeforeDefVariables; 234e8d8bef9SDimitry Andric 235e8d8bef9SDimitry Andric const TargetRegisterInfo &TRI; 236e8d8bef9SDimitry Andric const BitVector &CalleeSavedRegs; 237e8d8bef9SDimitry Andric 238e8d8bef9SDimitry Andric TransferTracker(const TargetInstrInfo *TII, MLocTracker *MTracker, 239e8d8bef9SDimitry Andric MachineFunction &MF, const TargetRegisterInfo &TRI, 240fe6060f1SDimitry Andric const BitVector &CalleeSavedRegs, const TargetPassConfig &TPC) 241e8d8bef9SDimitry Andric : TII(TII), MTracker(MTracker), MF(MF), TRI(TRI), 242fe6060f1SDimitry Andric CalleeSavedRegs(CalleeSavedRegs) { 243fe6060f1SDimitry Andric TLI = MF.getSubtarget().getTargetLowering(); 244fe6060f1SDimitry Andric auto &TM = TPC.getTM<TargetMachine>(); 245fe6060f1SDimitry Andric ShouldEmitDebugEntryValues = TM.Options.ShouldEmitDebugEntryValues(); 246fe6060f1SDimitry Andric } 247e8d8bef9SDimitry Andric 248e8d8bef9SDimitry Andric /// Load object with live-in variable values. \p mlocs contains the live-in 249e8d8bef9SDimitry Andric /// values in each machine location, while \p vlocs the live-in variable 250e8d8bef9SDimitry Andric /// values. This method picks variable locations for the live-in variables, 251e8d8bef9SDimitry Andric /// creates DBG_VALUEs and puts them in #Transfers, then prepares the other 252e8d8bef9SDimitry Andric /// object fields to track variable locations as we step through the block. 253e8d8bef9SDimitry Andric /// FIXME: could just examine mloctracker instead of passing in \p mlocs? 25404eeddc0SDimitry Andric void 25504eeddc0SDimitry Andric loadInlocs(MachineBasicBlock &MBB, ValueIDNum *MLocs, 25604eeddc0SDimitry Andric const SmallVectorImpl<std::pair<DebugVariable, DbgValue>> &VLocs, 257e8d8bef9SDimitry Andric unsigned NumLocs) { 258e8d8bef9SDimitry Andric ActiveMLocs.clear(); 259e8d8bef9SDimitry Andric ActiveVLocs.clear(); 260e8d8bef9SDimitry Andric VarLocs.clear(); 261e8d8bef9SDimitry Andric VarLocs.reserve(NumLocs); 262e8d8bef9SDimitry Andric UseBeforeDefs.clear(); 263e8d8bef9SDimitry Andric UseBeforeDefVariables.clear(); 264e8d8bef9SDimitry Andric 265e8d8bef9SDimitry Andric auto isCalleeSaved = [&](LocIdx L) { 266e8d8bef9SDimitry Andric unsigned Reg = MTracker->LocIdxToLocID[L]; 267e8d8bef9SDimitry Andric if (Reg >= MTracker->NumRegs) 268e8d8bef9SDimitry Andric return false; 269e8d8bef9SDimitry Andric for (MCRegAliasIterator RAI(Reg, &TRI, true); RAI.isValid(); ++RAI) 270e8d8bef9SDimitry Andric if (CalleeSavedRegs.test(*RAI)) 271e8d8bef9SDimitry Andric return true; 272e8d8bef9SDimitry Andric return false; 273e8d8bef9SDimitry Andric }; 274e8d8bef9SDimitry Andric 275e8d8bef9SDimitry Andric // Map of the preferred location for each value. 27604eeddc0SDimitry Andric DenseMap<ValueIDNum, LocIdx> ValueToLoc; 277*1fd87a68SDimitry Andric 278*1fd87a68SDimitry Andric // Initialized the preferred-location map with illegal locations, to be 279*1fd87a68SDimitry Andric // filled in later. 280*1fd87a68SDimitry Andric for (auto &VLoc : VLocs) 281*1fd87a68SDimitry Andric if (VLoc.second.Kind == DbgValue::Def) 282*1fd87a68SDimitry Andric ValueToLoc.insert({VLoc.second.ID, LocIdx::MakeIllegalLoc()}); 283*1fd87a68SDimitry Andric 284349cc55cSDimitry Andric ActiveMLocs.reserve(VLocs.size()); 285349cc55cSDimitry Andric ActiveVLocs.reserve(VLocs.size()); 286e8d8bef9SDimitry Andric 287e8d8bef9SDimitry Andric // Produce a map of value numbers to the current machine locs they live 288e8d8bef9SDimitry Andric // in. When emulating VarLocBasedImpl, there should only be one 289e8d8bef9SDimitry Andric // location; when not, we get to pick. 290e8d8bef9SDimitry Andric for (auto Location : MTracker->locations()) { 291e8d8bef9SDimitry Andric LocIdx Idx = Location.Idx; 292e8d8bef9SDimitry Andric ValueIDNum &VNum = MLocs[Idx.asU64()]; 293e8d8bef9SDimitry Andric VarLocs.push_back(VNum); 29404eeddc0SDimitry Andric 295*1fd87a68SDimitry Andric // Is there a variable that wants a location for this value? If not, skip. 296*1fd87a68SDimitry Andric auto VIt = ValueToLoc.find(VNum); 297*1fd87a68SDimitry Andric if (VIt == ValueToLoc.end()) 29804eeddc0SDimitry Andric continue; 29904eeddc0SDimitry Andric 300*1fd87a68SDimitry Andric LocIdx CurLoc = VIt->second; 301e8d8bef9SDimitry Andric // In order of preference, pick: 302e8d8bef9SDimitry Andric // * Callee saved registers, 303e8d8bef9SDimitry Andric // * Other registers, 304e8d8bef9SDimitry Andric // * Spill slots. 305*1fd87a68SDimitry Andric if (CurLoc.isIllegal() || MTracker->isSpill(CurLoc) || 306*1fd87a68SDimitry Andric (!isCalleeSaved(CurLoc) && isCalleeSaved(Idx.asU64()))) { 307e8d8bef9SDimitry Andric // Insert, or overwrite if insertion failed. 308*1fd87a68SDimitry Andric VIt->second = Idx; 309e8d8bef9SDimitry Andric } 310e8d8bef9SDimitry Andric } 311e8d8bef9SDimitry Andric 312e8d8bef9SDimitry Andric // Now map variables to their picked LocIdxes. 31304eeddc0SDimitry Andric for (const auto &Var : VLocs) { 314e8d8bef9SDimitry Andric if (Var.second.Kind == DbgValue::Const) { 315e8d8bef9SDimitry Andric PendingDbgValues.push_back( 316349cc55cSDimitry Andric emitMOLoc(*Var.second.MO, Var.first, Var.second.Properties)); 317e8d8bef9SDimitry Andric continue; 318e8d8bef9SDimitry Andric } 319e8d8bef9SDimitry Andric 320e8d8bef9SDimitry Andric // If the value has no location, we can't make a variable location. 321e8d8bef9SDimitry Andric const ValueIDNum &Num = Var.second.ID; 322e8d8bef9SDimitry Andric auto ValuesPreferredLoc = ValueToLoc.find(Num); 323*1fd87a68SDimitry Andric if (ValuesPreferredLoc->second.isIllegal()) { 324e8d8bef9SDimitry Andric // If it's a def that occurs in this block, register it as a 325e8d8bef9SDimitry Andric // use-before-def to be resolved as we step through the block. 326e8d8bef9SDimitry Andric if (Num.getBlock() == (unsigned)MBB.getNumber() && !Num.isPHI()) 327e8d8bef9SDimitry Andric addUseBeforeDef(Var.first, Var.second.Properties, Num); 328fe6060f1SDimitry Andric else 329fe6060f1SDimitry Andric recoverAsEntryValue(Var.first, Var.second.Properties, Num); 330e8d8bef9SDimitry Andric continue; 331e8d8bef9SDimitry Andric } 332e8d8bef9SDimitry Andric 333e8d8bef9SDimitry Andric LocIdx M = ValuesPreferredLoc->second; 334e8d8bef9SDimitry Andric auto NewValue = LocAndProperties{M, Var.second.Properties}; 335e8d8bef9SDimitry Andric auto Result = ActiveVLocs.insert(std::make_pair(Var.first, NewValue)); 336e8d8bef9SDimitry Andric if (!Result.second) 337e8d8bef9SDimitry Andric Result.first->second = NewValue; 338e8d8bef9SDimitry Andric ActiveMLocs[M].insert(Var.first); 339e8d8bef9SDimitry Andric PendingDbgValues.push_back( 340e8d8bef9SDimitry Andric MTracker->emitLoc(M, Var.first, Var.second.Properties)); 341e8d8bef9SDimitry Andric } 342e8d8bef9SDimitry Andric flushDbgValues(MBB.begin(), &MBB); 343e8d8bef9SDimitry Andric } 344e8d8bef9SDimitry Andric 345e8d8bef9SDimitry Andric /// Record that \p Var has value \p ID, a value that becomes available 346e8d8bef9SDimitry Andric /// later in the function. 347e8d8bef9SDimitry Andric void addUseBeforeDef(const DebugVariable &Var, 348e8d8bef9SDimitry Andric const DbgValueProperties &Properties, ValueIDNum ID) { 349e8d8bef9SDimitry Andric UseBeforeDef UBD = {ID, Var, Properties}; 350e8d8bef9SDimitry Andric UseBeforeDefs[ID.getInst()].push_back(UBD); 351e8d8bef9SDimitry Andric UseBeforeDefVariables.insert(Var); 352e8d8bef9SDimitry Andric } 353e8d8bef9SDimitry Andric 354e8d8bef9SDimitry Andric /// After the instruction at index \p Inst and position \p pos has been 355e8d8bef9SDimitry Andric /// processed, check whether it defines a variable value in a use-before-def. 356e8d8bef9SDimitry Andric /// If so, and the variable value hasn't changed since the start of the 357e8d8bef9SDimitry Andric /// block, create a DBG_VALUE. 358e8d8bef9SDimitry Andric void checkInstForNewValues(unsigned Inst, MachineBasicBlock::iterator pos) { 359e8d8bef9SDimitry Andric auto MIt = UseBeforeDefs.find(Inst); 360e8d8bef9SDimitry Andric if (MIt == UseBeforeDefs.end()) 361e8d8bef9SDimitry Andric return; 362e8d8bef9SDimitry Andric 363e8d8bef9SDimitry Andric for (auto &Use : MIt->second) { 364e8d8bef9SDimitry Andric LocIdx L = Use.ID.getLoc(); 365e8d8bef9SDimitry Andric 366e8d8bef9SDimitry Andric // If something goes very wrong, we might end up labelling a COPY 367e8d8bef9SDimitry Andric // instruction or similar with an instruction number, where it doesn't 368e8d8bef9SDimitry Andric // actually define a new value, instead it moves a value. In case this 369e8d8bef9SDimitry Andric // happens, discard. 370349cc55cSDimitry Andric if (MTracker->readMLoc(L) != Use.ID) 371e8d8bef9SDimitry Andric continue; 372e8d8bef9SDimitry Andric 373e8d8bef9SDimitry Andric // If a different debug instruction defined the variable value / location 374e8d8bef9SDimitry Andric // since the start of the block, don't materialize this use-before-def. 375e8d8bef9SDimitry Andric if (!UseBeforeDefVariables.count(Use.Var)) 376e8d8bef9SDimitry Andric continue; 377e8d8bef9SDimitry Andric 378e8d8bef9SDimitry Andric PendingDbgValues.push_back(MTracker->emitLoc(L, Use.Var, Use.Properties)); 379e8d8bef9SDimitry Andric } 380e8d8bef9SDimitry Andric flushDbgValues(pos, nullptr); 381e8d8bef9SDimitry Andric } 382e8d8bef9SDimitry Andric 383e8d8bef9SDimitry Andric /// Helper to move created DBG_VALUEs into Transfers collection. 384e8d8bef9SDimitry Andric void flushDbgValues(MachineBasicBlock::iterator Pos, MachineBasicBlock *MBB) { 385fe6060f1SDimitry Andric if (PendingDbgValues.size() == 0) 386fe6060f1SDimitry Andric return; 387fe6060f1SDimitry Andric 388fe6060f1SDimitry Andric // Pick out the instruction start position. 389fe6060f1SDimitry Andric MachineBasicBlock::instr_iterator BundleStart; 390fe6060f1SDimitry Andric if (MBB && Pos == MBB->begin()) 391fe6060f1SDimitry Andric BundleStart = MBB->instr_begin(); 392fe6060f1SDimitry Andric else 393fe6060f1SDimitry Andric BundleStart = getBundleStart(Pos->getIterator()); 394fe6060f1SDimitry Andric 395fe6060f1SDimitry Andric Transfers.push_back({BundleStart, MBB, PendingDbgValues}); 396e8d8bef9SDimitry Andric PendingDbgValues.clear(); 397e8d8bef9SDimitry Andric } 398fe6060f1SDimitry Andric 399fe6060f1SDimitry Andric bool isEntryValueVariable(const DebugVariable &Var, 400fe6060f1SDimitry Andric const DIExpression *Expr) const { 401fe6060f1SDimitry Andric if (!Var.getVariable()->isParameter()) 402fe6060f1SDimitry Andric return false; 403fe6060f1SDimitry Andric 404fe6060f1SDimitry Andric if (Var.getInlinedAt()) 405fe6060f1SDimitry Andric return false; 406fe6060f1SDimitry Andric 407fe6060f1SDimitry Andric if (Expr->getNumElements() > 0) 408fe6060f1SDimitry Andric return false; 409fe6060f1SDimitry Andric 410fe6060f1SDimitry Andric return true; 411fe6060f1SDimitry Andric } 412fe6060f1SDimitry Andric 413fe6060f1SDimitry Andric bool isEntryValueValue(const ValueIDNum &Val) const { 414fe6060f1SDimitry Andric // Must be in entry block (block number zero), and be a PHI / live-in value. 415fe6060f1SDimitry Andric if (Val.getBlock() || !Val.isPHI()) 416fe6060f1SDimitry Andric return false; 417fe6060f1SDimitry Andric 418fe6060f1SDimitry Andric // Entry values must enter in a register. 419fe6060f1SDimitry Andric if (MTracker->isSpill(Val.getLoc())) 420fe6060f1SDimitry Andric return false; 421fe6060f1SDimitry Andric 422fe6060f1SDimitry Andric Register SP = TLI->getStackPointerRegisterToSaveRestore(); 423fe6060f1SDimitry Andric Register FP = TRI.getFrameRegister(MF); 424fe6060f1SDimitry Andric Register Reg = MTracker->LocIdxToLocID[Val.getLoc()]; 425fe6060f1SDimitry Andric return Reg != SP && Reg != FP; 426fe6060f1SDimitry Andric } 427fe6060f1SDimitry Andric 42804eeddc0SDimitry Andric bool recoverAsEntryValue(const DebugVariable &Var, 42904eeddc0SDimitry Andric const DbgValueProperties &Prop, 430fe6060f1SDimitry Andric const ValueIDNum &Num) { 431fe6060f1SDimitry Andric // Is this variable location a candidate to be an entry value. First, 432fe6060f1SDimitry Andric // should we be trying this at all? 433fe6060f1SDimitry Andric if (!ShouldEmitDebugEntryValues) 434fe6060f1SDimitry Andric return false; 435fe6060f1SDimitry Andric 436fe6060f1SDimitry Andric // Is the variable appropriate for entry values (i.e., is a parameter). 437fe6060f1SDimitry Andric if (!isEntryValueVariable(Var, Prop.DIExpr)) 438fe6060f1SDimitry Andric return false; 439fe6060f1SDimitry Andric 440fe6060f1SDimitry Andric // Is the value assigned to this variable still the entry value? 441fe6060f1SDimitry Andric if (!isEntryValueValue(Num)) 442fe6060f1SDimitry Andric return false; 443fe6060f1SDimitry Andric 444fe6060f1SDimitry Andric // Emit a variable location using an entry value expression. 445fe6060f1SDimitry Andric DIExpression *NewExpr = 446fe6060f1SDimitry Andric DIExpression::prepend(Prop.DIExpr, DIExpression::EntryValue); 447fe6060f1SDimitry Andric Register Reg = MTracker->LocIdxToLocID[Num.getLoc()]; 448fe6060f1SDimitry Andric MachineOperand MO = MachineOperand::CreateReg(Reg, false); 449fe6060f1SDimitry Andric 450fe6060f1SDimitry Andric PendingDbgValues.push_back(emitMOLoc(MO, Var, {NewExpr, Prop.Indirect})); 451fe6060f1SDimitry Andric return true; 452e8d8bef9SDimitry Andric } 453e8d8bef9SDimitry Andric 454e8d8bef9SDimitry Andric /// Change a variable value after encountering a DBG_VALUE inside a block. 455e8d8bef9SDimitry Andric void redefVar(const MachineInstr &MI) { 456e8d8bef9SDimitry Andric DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(), 457e8d8bef9SDimitry Andric MI.getDebugLoc()->getInlinedAt()); 458e8d8bef9SDimitry Andric DbgValueProperties Properties(MI); 459e8d8bef9SDimitry Andric 460e8d8bef9SDimitry Andric const MachineOperand &MO = MI.getOperand(0); 461e8d8bef9SDimitry Andric 462e8d8bef9SDimitry Andric // Ignore non-register locations, we don't transfer those. 463e8d8bef9SDimitry Andric if (!MO.isReg() || MO.getReg() == 0) { 464e8d8bef9SDimitry Andric auto It = ActiveVLocs.find(Var); 465e8d8bef9SDimitry Andric if (It != ActiveVLocs.end()) { 466e8d8bef9SDimitry Andric ActiveMLocs[It->second.Loc].erase(Var); 467e8d8bef9SDimitry Andric ActiveVLocs.erase(It); 468e8d8bef9SDimitry Andric } 469e8d8bef9SDimitry Andric // Any use-before-defs no longer apply. 470e8d8bef9SDimitry Andric UseBeforeDefVariables.erase(Var); 471e8d8bef9SDimitry Andric return; 472e8d8bef9SDimitry Andric } 473e8d8bef9SDimitry Andric 474e8d8bef9SDimitry Andric Register Reg = MO.getReg(); 475e8d8bef9SDimitry Andric LocIdx NewLoc = MTracker->getRegMLoc(Reg); 476e8d8bef9SDimitry Andric redefVar(MI, Properties, NewLoc); 477e8d8bef9SDimitry Andric } 478e8d8bef9SDimitry Andric 479e8d8bef9SDimitry Andric /// Handle a change in variable location within a block. Terminate the 480e8d8bef9SDimitry Andric /// variables current location, and record the value it now refers to, so 481e8d8bef9SDimitry Andric /// that we can detect location transfers later on. 482e8d8bef9SDimitry Andric void redefVar(const MachineInstr &MI, const DbgValueProperties &Properties, 483e8d8bef9SDimitry Andric Optional<LocIdx> OptNewLoc) { 484e8d8bef9SDimitry Andric DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(), 485e8d8bef9SDimitry Andric MI.getDebugLoc()->getInlinedAt()); 486e8d8bef9SDimitry Andric // Any use-before-defs no longer apply. 487e8d8bef9SDimitry Andric UseBeforeDefVariables.erase(Var); 488e8d8bef9SDimitry Andric 489e8d8bef9SDimitry Andric // Erase any previous location, 490e8d8bef9SDimitry Andric auto It = ActiveVLocs.find(Var); 491e8d8bef9SDimitry Andric if (It != ActiveVLocs.end()) 492e8d8bef9SDimitry Andric ActiveMLocs[It->second.Loc].erase(Var); 493e8d8bef9SDimitry Andric 494e8d8bef9SDimitry Andric // If there _is_ no new location, all we had to do was erase. 495e8d8bef9SDimitry Andric if (!OptNewLoc) 496e8d8bef9SDimitry Andric return; 497e8d8bef9SDimitry Andric LocIdx NewLoc = *OptNewLoc; 498e8d8bef9SDimitry Andric 499e8d8bef9SDimitry Andric // Check whether our local copy of values-by-location in #VarLocs is out of 500e8d8bef9SDimitry Andric // date. Wipe old tracking data for the location if it's been clobbered in 501e8d8bef9SDimitry Andric // the meantime. 502349cc55cSDimitry Andric if (MTracker->readMLoc(NewLoc) != VarLocs[NewLoc.asU64()]) { 503e8d8bef9SDimitry Andric for (auto &P : ActiveMLocs[NewLoc]) { 504e8d8bef9SDimitry Andric ActiveVLocs.erase(P); 505e8d8bef9SDimitry Andric } 506e8d8bef9SDimitry Andric ActiveMLocs[NewLoc.asU64()].clear(); 507349cc55cSDimitry Andric VarLocs[NewLoc.asU64()] = MTracker->readMLoc(NewLoc); 508e8d8bef9SDimitry Andric } 509e8d8bef9SDimitry Andric 510e8d8bef9SDimitry Andric ActiveMLocs[NewLoc].insert(Var); 511e8d8bef9SDimitry Andric if (It == ActiveVLocs.end()) { 512e8d8bef9SDimitry Andric ActiveVLocs.insert( 513e8d8bef9SDimitry Andric std::make_pair(Var, LocAndProperties{NewLoc, Properties})); 514e8d8bef9SDimitry Andric } else { 515e8d8bef9SDimitry Andric It->second.Loc = NewLoc; 516e8d8bef9SDimitry Andric It->second.Properties = Properties; 517e8d8bef9SDimitry Andric } 518e8d8bef9SDimitry Andric } 519e8d8bef9SDimitry Andric 520fe6060f1SDimitry Andric /// Account for a location \p mloc being clobbered. Examine the variable 521fe6060f1SDimitry Andric /// locations that will be terminated: and try to recover them by using 522fe6060f1SDimitry Andric /// another location. Optionally, given \p MakeUndef, emit a DBG_VALUE to 523fe6060f1SDimitry Andric /// explicitly terminate a location if it can't be recovered. 524fe6060f1SDimitry Andric void clobberMloc(LocIdx MLoc, MachineBasicBlock::iterator Pos, 525fe6060f1SDimitry Andric bool MakeUndef = true) { 526e8d8bef9SDimitry Andric auto ActiveMLocIt = ActiveMLocs.find(MLoc); 527e8d8bef9SDimitry Andric if (ActiveMLocIt == ActiveMLocs.end()) 528e8d8bef9SDimitry Andric return; 529e8d8bef9SDimitry Andric 530fe6060f1SDimitry Andric // What was the old variable value? 531fe6060f1SDimitry Andric ValueIDNum OldValue = VarLocs[MLoc.asU64()]; 532e8d8bef9SDimitry Andric VarLocs[MLoc.asU64()] = ValueIDNum::EmptyValue; 533e8d8bef9SDimitry Andric 534fe6060f1SDimitry Andric // Examine the remaining variable locations: if we can find the same value 535fe6060f1SDimitry Andric // again, we can recover the location. 536fe6060f1SDimitry Andric Optional<LocIdx> NewLoc = None; 537fe6060f1SDimitry Andric for (auto Loc : MTracker->locations()) 538fe6060f1SDimitry Andric if (Loc.Value == OldValue) 539fe6060f1SDimitry Andric NewLoc = Loc.Idx; 540fe6060f1SDimitry Andric 541fe6060f1SDimitry Andric // If there is no location, and we weren't asked to make the variable 542fe6060f1SDimitry Andric // explicitly undef, then stop here. 543fe6060f1SDimitry Andric if (!NewLoc && !MakeUndef) { 544fe6060f1SDimitry Andric // Try and recover a few more locations with entry values. 545fe6060f1SDimitry Andric for (auto &Var : ActiveMLocIt->second) { 546fe6060f1SDimitry Andric auto &Prop = ActiveVLocs.find(Var)->second.Properties; 547fe6060f1SDimitry Andric recoverAsEntryValue(Var, Prop, OldValue); 548fe6060f1SDimitry Andric } 549fe6060f1SDimitry Andric flushDbgValues(Pos, nullptr); 550fe6060f1SDimitry Andric return; 551fe6060f1SDimitry Andric } 552fe6060f1SDimitry Andric 553fe6060f1SDimitry Andric // Examine all the variables based on this location. 554fe6060f1SDimitry Andric DenseSet<DebugVariable> NewMLocs; 555e8d8bef9SDimitry Andric for (auto &Var : ActiveMLocIt->second) { 556e8d8bef9SDimitry Andric auto ActiveVLocIt = ActiveVLocs.find(Var); 557fe6060f1SDimitry Andric // Re-state the variable location: if there's no replacement then NewLoc 558fe6060f1SDimitry Andric // is None and a $noreg DBG_VALUE will be created. Otherwise, a DBG_VALUE 559fe6060f1SDimitry Andric // identifying the alternative location will be emitted. 5604824e7fdSDimitry Andric const DbgValueProperties &Properties = ActiveVLocIt->second.Properties; 561fe6060f1SDimitry Andric PendingDbgValues.push_back(MTracker->emitLoc(NewLoc, Var, Properties)); 562fe6060f1SDimitry Andric 563fe6060f1SDimitry Andric // Update machine locations <=> variable locations maps. Defer updating 564fe6060f1SDimitry Andric // ActiveMLocs to avoid invalidaing the ActiveMLocIt iterator. 565fe6060f1SDimitry Andric if (!NewLoc) { 566e8d8bef9SDimitry Andric ActiveVLocs.erase(ActiveVLocIt); 567fe6060f1SDimitry Andric } else { 568fe6060f1SDimitry Andric ActiveVLocIt->second.Loc = *NewLoc; 569fe6060f1SDimitry Andric NewMLocs.insert(Var); 570e8d8bef9SDimitry Andric } 571fe6060f1SDimitry Andric } 572fe6060f1SDimitry Andric 573fe6060f1SDimitry Andric // Commit any deferred ActiveMLoc changes. 574fe6060f1SDimitry Andric if (!NewMLocs.empty()) 575fe6060f1SDimitry Andric for (auto &Var : NewMLocs) 576fe6060f1SDimitry Andric ActiveMLocs[*NewLoc].insert(Var); 577fe6060f1SDimitry Andric 578fe6060f1SDimitry Andric // We lazily track what locations have which values; if we've found a new 579fe6060f1SDimitry Andric // location for the clobbered value, remember it. 580fe6060f1SDimitry Andric if (NewLoc) 581fe6060f1SDimitry Andric VarLocs[NewLoc->asU64()] = OldValue; 582fe6060f1SDimitry Andric 583e8d8bef9SDimitry Andric flushDbgValues(Pos, nullptr); 584e8d8bef9SDimitry Andric 585349cc55cSDimitry Andric // Re-find ActiveMLocIt, iterator could have been invalidated. 586349cc55cSDimitry Andric ActiveMLocIt = ActiveMLocs.find(MLoc); 587e8d8bef9SDimitry Andric ActiveMLocIt->second.clear(); 588e8d8bef9SDimitry Andric } 589e8d8bef9SDimitry Andric 590e8d8bef9SDimitry Andric /// Transfer variables based on \p Src to be based on \p Dst. This handles 591e8d8bef9SDimitry Andric /// both register copies as well as spills and restores. Creates DBG_VALUEs 592e8d8bef9SDimitry Andric /// describing the movement. 593e8d8bef9SDimitry Andric void transferMlocs(LocIdx Src, LocIdx Dst, MachineBasicBlock::iterator Pos) { 594e8d8bef9SDimitry Andric // Does Src still contain the value num we expect? If not, it's been 595e8d8bef9SDimitry Andric // clobbered in the meantime, and our variable locations are stale. 596349cc55cSDimitry Andric if (VarLocs[Src.asU64()] != MTracker->readMLoc(Src)) 597e8d8bef9SDimitry Andric return; 598e8d8bef9SDimitry Andric 599e8d8bef9SDimitry Andric // assert(ActiveMLocs[Dst].size() == 0); 600e8d8bef9SDimitry Andric //^^^ Legitimate scenario on account of un-clobbered slot being assigned to? 601349cc55cSDimitry Andric 602349cc55cSDimitry Andric // Move set of active variables from one location to another. 603349cc55cSDimitry Andric auto MovingVars = ActiveMLocs[Src]; 604349cc55cSDimitry Andric ActiveMLocs[Dst] = MovingVars; 605e8d8bef9SDimitry Andric VarLocs[Dst.asU64()] = VarLocs[Src.asU64()]; 606e8d8bef9SDimitry Andric 607e8d8bef9SDimitry Andric // For each variable based on Src; create a location at Dst. 608349cc55cSDimitry Andric for (auto &Var : MovingVars) { 609e8d8bef9SDimitry Andric auto ActiveVLocIt = ActiveVLocs.find(Var); 610e8d8bef9SDimitry Andric assert(ActiveVLocIt != ActiveVLocs.end()); 611e8d8bef9SDimitry Andric ActiveVLocIt->second.Loc = Dst; 612e8d8bef9SDimitry Andric 613e8d8bef9SDimitry Andric MachineInstr *MI = 614e8d8bef9SDimitry Andric MTracker->emitLoc(Dst, Var, ActiveVLocIt->second.Properties); 615e8d8bef9SDimitry Andric PendingDbgValues.push_back(MI); 616e8d8bef9SDimitry Andric } 617e8d8bef9SDimitry Andric ActiveMLocs[Src].clear(); 618e8d8bef9SDimitry Andric flushDbgValues(Pos, nullptr); 619e8d8bef9SDimitry Andric 620e8d8bef9SDimitry Andric // XXX XXX XXX "pretend to be old LDV" means dropping all tracking data 621e8d8bef9SDimitry Andric // about the old location. 622e8d8bef9SDimitry Andric if (EmulateOldLDV) 623e8d8bef9SDimitry Andric VarLocs[Src.asU64()] = ValueIDNum::EmptyValue; 624e8d8bef9SDimitry Andric } 625e8d8bef9SDimitry Andric 626e8d8bef9SDimitry Andric MachineInstrBuilder emitMOLoc(const MachineOperand &MO, 627e8d8bef9SDimitry Andric const DebugVariable &Var, 628e8d8bef9SDimitry Andric const DbgValueProperties &Properties) { 629e8d8bef9SDimitry Andric DebugLoc DL = DILocation::get(Var.getVariable()->getContext(), 0, 0, 630e8d8bef9SDimitry Andric Var.getVariable()->getScope(), 631e8d8bef9SDimitry Andric const_cast<DILocation *>(Var.getInlinedAt())); 632e8d8bef9SDimitry Andric auto MIB = BuildMI(MF, DL, TII->get(TargetOpcode::DBG_VALUE)); 633e8d8bef9SDimitry Andric MIB.add(MO); 634e8d8bef9SDimitry Andric if (Properties.Indirect) 635e8d8bef9SDimitry Andric MIB.addImm(0); 636e8d8bef9SDimitry Andric else 637e8d8bef9SDimitry Andric MIB.addReg(0); 638e8d8bef9SDimitry Andric MIB.addMetadata(Var.getVariable()); 639e8d8bef9SDimitry Andric MIB.addMetadata(Properties.DIExpr); 640e8d8bef9SDimitry Andric return MIB; 641e8d8bef9SDimitry Andric } 642e8d8bef9SDimitry Andric }; 643e8d8bef9SDimitry Andric 644349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 645349cc55cSDimitry Andric // Implementation 646349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 647e8d8bef9SDimitry Andric 648349cc55cSDimitry Andric ValueIDNum ValueIDNum::EmptyValue = {UINT_MAX, UINT_MAX, UINT_MAX}; 649349cc55cSDimitry Andric ValueIDNum ValueIDNum::TombstoneValue = {UINT_MAX, UINT_MAX, UINT_MAX - 1}; 650e8d8bef9SDimitry Andric 651349cc55cSDimitry Andric #ifndef NDEBUG 652349cc55cSDimitry Andric void DbgValue::dump(const MLocTracker *MTrack) const { 653349cc55cSDimitry Andric if (Kind == Const) { 654349cc55cSDimitry Andric MO->dump(); 655349cc55cSDimitry Andric } else if (Kind == NoVal) { 656349cc55cSDimitry Andric dbgs() << "NoVal(" << BlockNo << ")"; 657349cc55cSDimitry Andric } else if (Kind == VPHI) { 658349cc55cSDimitry Andric dbgs() << "VPHI(" << BlockNo << "," << MTrack->IDAsString(ID) << ")"; 659349cc55cSDimitry Andric } else { 660349cc55cSDimitry Andric assert(Kind == Def); 661349cc55cSDimitry Andric dbgs() << MTrack->IDAsString(ID); 662349cc55cSDimitry Andric } 663349cc55cSDimitry Andric if (Properties.Indirect) 664349cc55cSDimitry Andric dbgs() << " indir"; 665349cc55cSDimitry Andric if (Properties.DIExpr) 666349cc55cSDimitry Andric dbgs() << " " << *Properties.DIExpr; 667349cc55cSDimitry Andric } 668349cc55cSDimitry Andric #endif 669e8d8bef9SDimitry Andric 670349cc55cSDimitry Andric MLocTracker::MLocTracker(MachineFunction &MF, const TargetInstrInfo &TII, 671349cc55cSDimitry Andric const TargetRegisterInfo &TRI, 672349cc55cSDimitry Andric const TargetLowering &TLI) 673349cc55cSDimitry Andric : MF(MF), TII(TII), TRI(TRI), TLI(TLI), 674349cc55cSDimitry Andric LocIdxToIDNum(ValueIDNum::EmptyValue), LocIdxToLocID(0) { 675349cc55cSDimitry Andric NumRegs = TRI.getNumRegs(); 676349cc55cSDimitry Andric reset(); 677349cc55cSDimitry Andric LocIDToLocIdx.resize(NumRegs, LocIdx::MakeIllegalLoc()); 678349cc55cSDimitry Andric assert(NumRegs < (1u << NUM_LOC_BITS)); // Detect bit packing failure 679e8d8bef9SDimitry Andric 680349cc55cSDimitry Andric // Always track SP. This avoids the implicit clobbering caused by regmasks 681349cc55cSDimitry Andric // from affectings its values. (LiveDebugValues disbelieves calls and 682349cc55cSDimitry Andric // regmasks that claim to clobber SP). 683349cc55cSDimitry Andric Register SP = TLI.getStackPointerRegisterToSaveRestore(); 684349cc55cSDimitry Andric if (SP) { 685349cc55cSDimitry Andric unsigned ID = getLocID(SP); 686349cc55cSDimitry Andric (void)lookupOrTrackRegister(ID); 687e8d8bef9SDimitry Andric 688349cc55cSDimitry Andric for (MCRegAliasIterator RAI(SP, &TRI, true); RAI.isValid(); ++RAI) 689349cc55cSDimitry Andric SPAliases.insert(*RAI); 690349cc55cSDimitry Andric } 691e8d8bef9SDimitry Andric 692349cc55cSDimitry Andric // Build some common stack positions -- full registers being spilt to the 693349cc55cSDimitry Andric // stack. 694349cc55cSDimitry Andric StackSlotIdxes.insert({{8, 0}, 0}); 695349cc55cSDimitry Andric StackSlotIdxes.insert({{16, 0}, 1}); 696349cc55cSDimitry Andric StackSlotIdxes.insert({{32, 0}, 2}); 697349cc55cSDimitry Andric StackSlotIdxes.insert({{64, 0}, 3}); 698349cc55cSDimitry Andric StackSlotIdxes.insert({{128, 0}, 4}); 699349cc55cSDimitry Andric StackSlotIdxes.insert({{256, 0}, 5}); 700349cc55cSDimitry Andric StackSlotIdxes.insert({{512, 0}, 6}); 701e8d8bef9SDimitry Andric 702349cc55cSDimitry Andric // Traverse all the subregister idxes, and ensure there's an index for them. 703349cc55cSDimitry Andric // Duplicates are no problem: we're interested in their position in the 704349cc55cSDimitry Andric // stack slot, we don't want to type the slot. 705349cc55cSDimitry Andric for (unsigned int I = 1; I < TRI.getNumSubRegIndices(); ++I) { 706349cc55cSDimitry Andric unsigned Size = TRI.getSubRegIdxSize(I); 707349cc55cSDimitry Andric unsigned Offs = TRI.getSubRegIdxOffset(I); 708349cc55cSDimitry Andric unsigned Idx = StackSlotIdxes.size(); 709e8d8bef9SDimitry Andric 710349cc55cSDimitry Andric // Some subregs have -1, -2 and so forth fed into their fields, to mean 711349cc55cSDimitry Andric // special backend things. Ignore those. 712349cc55cSDimitry Andric if (Size > 60000 || Offs > 60000) 713349cc55cSDimitry Andric continue; 714e8d8bef9SDimitry Andric 715349cc55cSDimitry Andric StackSlotIdxes.insert({{Size, Offs}, Idx}); 716349cc55cSDimitry Andric } 717e8d8bef9SDimitry Andric 718349cc55cSDimitry Andric for (auto &Idx : StackSlotIdxes) 719349cc55cSDimitry Andric StackIdxesToPos[Idx.second] = Idx.first; 720e8d8bef9SDimitry Andric 721349cc55cSDimitry Andric NumSlotIdxes = StackSlotIdxes.size(); 722349cc55cSDimitry Andric } 723e8d8bef9SDimitry Andric 724349cc55cSDimitry Andric LocIdx MLocTracker::trackRegister(unsigned ID) { 725349cc55cSDimitry Andric assert(ID != 0); 726349cc55cSDimitry Andric LocIdx NewIdx = LocIdx(LocIdxToIDNum.size()); 727349cc55cSDimitry Andric LocIdxToIDNum.grow(NewIdx); 728349cc55cSDimitry Andric LocIdxToLocID.grow(NewIdx); 729e8d8bef9SDimitry Andric 730349cc55cSDimitry Andric // Default: it's an mphi. 731349cc55cSDimitry Andric ValueIDNum ValNum = {CurBB, 0, NewIdx}; 732349cc55cSDimitry Andric // Was this reg ever touched by a regmask? 733349cc55cSDimitry Andric for (const auto &MaskPair : reverse(Masks)) { 734349cc55cSDimitry Andric if (MaskPair.first->clobbersPhysReg(ID)) { 735349cc55cSDimitry Andric // There was an earlier def we skipped. 736349cc55cSDimitry Andric ValNum = {CurBB, MaskPair.second, NewIdx}; 737349cc55cSDimitry Andric break; 738349cc55cSDimitry Andric } 739349cc55cSDimitry Andric } 740e8d8bef9SDimitry Andric 741349cc55cSDimitry Andric LocIdxToIDNum[NewIdx] = ValNum; 742349cc55cSDimitry Andric LocIdxToLocID[NewIdx] = ID; 743349cc55cSDimitry Andric return NewIdx; 744349cc55cSDimitry Andric } 745e8d8bef9SDimitry Andric 746349cc55cSDimitry Andric void MLocTracker::writeRegMask(const MachineOperand *MO, unsigned CurBB, 747349cc55cSDimitry Andric unsigned InstID) { 748349cc55cSDimitry Andric // Def any register we track have that isn't preserved. The regmask 749349cc55cSDimitry Andric // terminates the liveness of a register, meaning its value can't be 750349cc55cSDimitry Andric // relied upon -- we represent this by giving it a new value. 751349cc55cSDimitry Andric for (auto Location : locations()) { 752349cc55cSDimitry Andric unsigned ID = LocIdxToLocID[Location.Idx]; 753349cc55cSDimitry Andric // Don't clobber SP, even if the mask says it's clobbered. 754349cc55cSDimitry Andric if (ID < NumRegs && !SPAliases.count(ID) && MO->clobbersPhysReg(ID)) 755349cc55cSDimitry Andric defReg(ID, CurBB, InstID); 756349cc55cSDimitry Andric } 757349cc55cSDimitry Andric Masks.push_back(std::make_pair(MO, InstID)); 758349cc55cSDimitry Andric } 759e8d8bef9SDimitry Andric 760349cc55cSDimitry Andric SpillLocationNo MLocTracker::getOrTrackSpillLoc(SpillLoc L) { 761349cc55cSDimitry Andric SpillLocationNo SpillID(SpillLocs.idFor(L)); 762349cc55cSDimitry Andric if (SpillID.id() == 0) { 763349cc55cSDimitry Andric // Spill location is untracked: create record for this one, and all 764349cc55cSDimitry Andric // subregister slots too. 765349cc55cSDimitry Andric SpillID = SpillLocationNo(SpillLocs.insert(L)); 766349cc55cSDimitry Andric for (unsigned StackIdx = 0; StackIdx < NumSlotIdxes; ++StackIdx) { 767349cc55cSDimitry Andric unsigned L = getSpillIDWithIdx(SpillID, StackIdx); 768349cc55cSDimitry Andric LocIdx Idx = LocIdx(LocIdxToIDNum.size()); // New idx 769349cc55cSDimitry Andric LocIdxToIDNum.grow(Idx); 770349cc55cSDimitry Andric LocIdxToLocID.grow(Idx); 771349cc55cSDimitry Andric LocIDToLocIdx.push_back(Idx); 772349cc55cSDimitry Andric LocIdxToLocID[Idx] = L; 773349cc55cSDimitry Andric // Initialize to PHI value; corresponds to the location's live-in value 774349cc55cSDimitry Andric // during transfer function construction. 775349cc55cSDimitry Andric LocIdxToIDNum[Idx] = ValueIDNum(CurBB, 0, Idx); 776349cc55cSDimitry Andric } 777349cc55cSDimitry Andric } 778349cc55cSDimitry Andric return SpillID; 779349cc55cSDimitry Andric } 780fe6060f1SDimitry Andric 781349cc55cSDimitry Andric std::string MLocTracker::LocIdxToName(LocIdx Idx) const { 782349cc55cSDimitry Andric unsigned ID = LocIdxToLocID[Idx]; 783349cc55cSDimitry Andric if (ID >= NumRegs) { 784349cc55cSDimitry Andric StackSlotPos Pos = locIDToSpillIdx(ID); 785349cc55cSDimitry Andric ID -= NumRegs; 786349cc55cSDimitry Andric unsigned Slot = ID / NumSlotIdxes; 787349cc55cSDimitry Andric return Twine("slot ") 788349cc55cSDimitry Andric .concat(Twine(Slot).concat(Twine(" sz ").concat(Twine(Pos.first) 789349cc55cSDimitry Andric .concat(Twine(" offs ").concat(Twine(Pos.second)))))) 790349cc55cSDimitry Andric .str(); 791349cc55cSDimitry Andric } else { 792349cc55cSDimitry Andric return TRI.getRegAsmName(ID).str(); 793349cc55cSDimitry Andric } 794349cc55cSDimitry Andric } 795fe6060f1SDimitry Andric 796349cc55cSDimitry Andric std::string MLocTracker::IDAsString(const ValueIDNum &Num) const { 797349cc55cSDimitry Andric std::string DefName = LocIdxToName(Num.getLoc()); 798349cc55cSDimitry Andric return Num.asString(DefName); 799349cc55cSDimitry Andric } 800fe6060f1SDimitry Andric 801349cc55cSDimitry Andric #ifndef NDEBUG 802349cc55cSDimitry Andric LLVM_DUMP_METHOD void MLocTracker::dump() { 803349cc55cSDimitry Andric for (auto Location : locations()) { 804349cc55cSDimitry Andric std::string MLocName = LocIdxToName(Location.Value.getLoc()); 805349cc55cSDimitry Andric std::string DefName = Location.Value.asString(MLocName); 806349cc55cSDimitry Andric dbgs() << LocIdxToName(Location.Idx) << " --> " << DefName << "\n"; 807349cc55cSDimitry Andric } 808349cc55cSDimitry Andric } 809e8d8bef9SDimitry Andric 810349cc55cSDimitry Andric LLVM_DUMP_METHOD void MLocTracker::dump_mloc_map() { 811349cc55cSDimitry Andric for (auto Location : locations()) { 812349cc55cSDimitry Andric std::string foo = LocIdxToName(Location.Idx); 813349cc55cSDimitry Andric dbgs() << "Idx " << Location.Idx.asU64() << " " << foo << "\n"; 814349cc55cSDimitry Andric } 815349cc55cSDimitry Andric } 816349cc55cSDimitry Andric #endif 817e8d8bef9SDimitry Andric 818349cc55cSDimitry Andric MachineInstrBuilder MLocTracker::emitLoc(Optional<LocIdx> MLoc, 819349cc55cSDimitry Andric const DebugVariable &Var, 820349cc55cSDimitry Andric const DbgValueProperties &Properties) { 821349cc55cSDimitry Andric DebugLoc DL = DILocation::get(Var.getVariable()->getContext(), 0, 0, 822349cc55cSDimitry Andric Var.getVariable()->getScope(), 823349cc55cSDimitry Andric const_cast<DILocation *>(Var.getInlinedAt())); 824349cc55cSDimitry Andric auto MIB = BuildMI(MF, DL, TII.get(TargetOpcode::DBG_VALUE)); 825e8d8bef9SDimitry Andric 826349cc55cSDimitry Andric const DIExpression *Expr = Properties.DIExpr; 827349cc55cSDimitry Andric if (!MLoc) { 828349cc55cSDimitry Andric // No location -> DBG_VALUE $noreg 829349cc55cSDimitry Andric MIB.addReg(0); 830349cc55cSDimitry Andric MIB.addReg(0); 831349cc55cSDimitry Andric } else if (LocIdxToLocID[*MLoc] >= NumRegs) { 832349cc55cSDimitry Andric unsigned LocID = LocIdxToLocID[*MLoc]; 833349cc55cSDimitry Andric SpillLocationNo SpillID = locIDToSpill(LocID); 834349cc55cSDimitry Andric StackSlotPos StackIdx = locIDToSpillIdx(LocID); 835349cc55cSDimitry Andric unsigned short Offset = StackIdx.second; 836e8d8bef9SDimitry Andric 837349cc55cSDimitry Andric // TODO: support variables that are located in spill slots, with non-zero 838349cc55cSDimitry Andric // offsets from the start of the spill slot. It would require some more 839349cc55cSDimitry Andric // complex DIExpression calculations. This doesn't seem to be produced by 840349cc55cSDimitry Andric // LLVM right now, so don't try and support it. 841349cc55cSDimitry Andric // Accept no-subregister slots and subregisters where the offset is zero. 842349cc55cSDimitry Andric // The consumer should already have type information to work out how large 843349cc55cSDimitry Andric // the variable is. 844349cc55cSDimitry Andric if (Offset == 0) { 845349cc55cSDimitry Andric const SpillLoc &Spill = SpillLocs[SpillID.id()]; 846349cc55cSDimitry Andric Expr = TRI.prependOffsetExpression(Expr, DIExpression::ApplyOffset, 847349cc55cSDimitry Andric Spill.SpillOffset); 848349cc55cSDimitry Andric unsigned Base = Spill.SpillBase; 849349cc55cSDimitry Andric MIB.addReg(Base); 850349cc55cSDimitry Andric MIB.addImm(0); 8514824e7fdSDimitry Andric 8524824e7fdSDimitry Andric // Being on the stack makes this location indirect; if it was _already_ 8534824e7fdSDimitry Andric // indirect though, we need to add extra indirection. See this test for 8544824e7fdSDimitry Andric // a scenario where this happens: 8554824e7fdSDimitry Andric // llvm/test/DebugInfo/X86/spill-nontrivial-param.ll 8564824e7fdSDimitry Andric if (Properties.Indirect) { 8574824e7fdSDimitry Andric std::vector<uint64_t> Elts = {dwarf::DW_OP_deref}; 8584824e7fdSDimitry Andric Expr = DIExpression::append(Expr, Elts); 8594824e7fdSDimitry Andric } 860349cc55cSDimitry Andric } else { 861349cc55cSDimitry Andric // This is a stack location with a weird subregister offset: emit an undef 862349cc55cSDimitry Andric // DBG_VALUE instead. 863349cc55cSDimitry Andric MIB.addReg(0); 864349cc55cSDimitry Andric MIB.addReg(0); 865349cc55cSDimitry Andric } 866349cc55cSDimitry Andric } else { 867349cc55cSDimitry Andric // Non-empty, non-stack slot, must be a plain register. 868349cc55cSDimitry Andric unsigned LocID = LocIdxToLocID[*MLoc]; 869349cc55cSDimitry Andric MIB.addReg(LocID); 870349cc55cSDimitry Andric if (Properties.Indirect) 871349cc55cSDimitry Andric MIB.addImm(0); 872349cc55cSDimitry Andric else 873349cc55cSDimitry Andric MIB.addReg(0); 874349cc55cSDimitry Andric } 875e8d8bef9SDimitry Andric 876349cc55cSDimitry Andric MIB.addMetadata(Var.getVariable()); 877349cc55cSDimitry Andric MIB.addMetadata(Expr); 878349cc55cSDimitry Andric return MIB; 879349cc55cSDimitry Andric } 880e8d8bef9SDimitry Andric 881e8d8bef9SDimitry Andric /// Default construct and initialize the pass. 882349cc55cSDimitry Andric InstrRefBasedLDV::InstrRefBasedLDV() {} 883e8d8bef9SDimitry Andric 884349cc55cSDimitry Andric bool InstrRefBasedLDV::isCalleeSaved(LocIdx L) const { 885e8d8bef9SDimitry Andric unsigned Reg = MTracker->LocIdxToLocID[L]; 886e8d8bef9SDimitry Andric for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI) 887e8d8bef9SDimitry Andric if (CalleeSavedRegs.test(*RAI)) 888e8d8bef9SDimitry Andric return true; 889e8d8bef9SDimitry Andric return false; 890e8d8bef9SDimitry Andric } 891e8d8bef9SDimitry Andric 892e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 893e8d8bef9SDimitry Andric // Debug Range Extension Implementation 894e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 895e8d8bef9SDimitry Andric 896e8d8bef9SDimitry Andric #ifndef NDEBUG 897e8d8bef9SDimitry Andric // Something to restore in the future. 898e8d8bef9SDimitry Andric // void InstrRefBasedLDV::printVarLocInMBB(..) 899e8d8bef9SDimitry Andric #endif 900e8d8bef9SDimitry Andric 901349cc55cSDimitry Andric SpillLocationNo 902e8d8bef9SDimitry Andric InstrRefBasedLDV::extractSpillBaseRegAndOffset(const MachineInstr &MI) { 903e8d8bef9SDimitry Andric assert(MI.hasOneMemOperand() && 904e8d8bef9SDimitry Andric "Spill instruction does not have exactly one memory operand?"); 905e8d8bef9SDimitry Andric auto MMOI = MI.memoperands_begin(); 906e8d8bef9SDimitry Andric const PseudoSourceValue *PVal = (*MMOI)->getPseudoValue(); 907e8d8bef9SDimitry Andric assert(PVal->kind() == PseudoSourceValue::FixedStack && 908e8d8bef9SDimitry Andric "Inconsistent memory operand in spill instruction"); 909e8d8bef9SDimitry Andric int FI = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex(); 910e8d8bef9SDimitry Andric const MachineBasicBlock *MBB = MI.getParent(); 911e8d8bef9SDimitry Andric Register Reg; 912e8d8bef9SDimitry Andric StackOffset Offset = TFI->getFrameIndexReference(*MBB->getParent(), FI, Reg); 913349cc55cSDimitry Andric return MTracker->getOrTrackSpillLoc({Reg, Offset}); 914349cc55cSDimitry Andric } 915349cc55cSDimitry Andric 916349cc55cSDimitry Andric Optional<LocIdx> InstrRefBasedLDV::findLocationForMemOperand(const MachineInstr &MI) { 917349cc55cSDimitry Andric SpillLocationNo SpillLoc = extractSpillBaseRegAndOffset(MI); 918349cc55cSDimitry Andric 919349cc55cSDimitry Andric // Where in the stack slot is this value defined -- i.e., what size of value 920349cc55cSDimitry Andric // is this? An important question, because it could be loaded into a register 921349cc55cSDimitry Andric // from the stack at some point. Happily the memory operand will tell us 922349cc55cSDimitry Andric // the size written to the stack. 923349cc55cSDimitry Andric auto *MemOperand = *MI.memoperands_begin(); 924349cc55cSDimitry Andric unsigned SizeInBits = MemOperand->getSizeInBits(); 925349cc55cSDimitry Andric 926349cc55cSDimitry Andric // Find that position in the stack indexes we're tracking. 927349cc55cSDimitry Andric auto IdxIt = MTracker->StackSlotIdxes.find({SizeInBits, 0}); 928349cc55cSDimitry Andric if (IdxIt == MTracker->StackSlotIdxes.end()) 929349cc55cSDimitry Andric // That index is not tracked. This is suprising, and unlikely to ever 930349cc55cSDimitry Andric // occur, but the safe action is to indicate the variable is optimised out. 931349cc55cSDimitry Andric return None; 932349cc55cSDimitry Andric 933349cc55cSDimitry Andric unsigned SpillID = MTracker->getSpillIDWithIdx(SpillLoc, IdxIt->second); 934349cc55cSDimitry Andric return MTracker->getSpillMLoc(SpillID); 935e8d8bef9SDimitry Andric } 936e8d8bef9SDimitry Andric 937e8d8bef9SDimitry Andric /// End all previous ranges related to @MI and start a new range from @MI 938e8d8bef9SDimitry Andric /// if it is a DBG_VALUE instr. 939e8d8bef9SDimitry Andric bool InstrRefBasedLDV::transferDebugValue(const MachineInstr &MI) { 940e8d8bef9SDimitry Andric if (!MI.isDebugValue()) 941e8d8bef9SDimitry Andric return false; 942e8d8bef9SDimitry Andric 943e8d8bef9SDimitry Andric const DILocalVariable *Var = MI.getDebugVariable(); 944e8d8bef9SDimitry Andric const DIExpression *Expr = MI.getDebugExpression(); 945e8d8bef9SDimitry Andric const DILocation *DebugLoc = MI.getDebugLoc(); 946e8d8bef9SDimitry Andric const DILocation *InlinedAt = DebugLoc->getInlinedAt(); 947e8d8bef9SDimitry Andric assert(Var->isValidLocationForIntrinsic(DebugLoc) && 948e8d8bef9SDimitry Andric "Expected inlined-at fields to agree"); 949e8d8bef9SDimitry Andric 950e8d8bef9SDimitry Andric DebugVariable V(Var, Expr, InlinedAt); 951e8d8bef9SDimitry Andric DbgValueProperties Properties(MI); 952e8d8bef9SDimitry Andric 953e8d8bef9SDimitry Andric // If there are no instructions in this lexical scope, do no location tracking 954e8d8bef9SDimitry Andric // at all, this variable shouldn't get a legitimate location range. 955e8d8bef9SDimitry Andric auto *Scope = LS.findLexicalScope(MI.getDebugLoc().get()); 956e8d8bef9SDimitry Andric if (Scope == nullptr) 957e8d8bef9SDimitry Andric return true; // handled it; by doing nothing 958e8d8bef9SDimitry Andric 959349cc55cSDimitry Andric // For now, ignore DBG_VALUE_LISTs when extending ranges. Allow it to 960349cc55cSDimitry Andric // contribute to locations in this block, but don't propagate further. 961349cc55cSDimitry Andric // Interpret it like a DBG_VALUE $noreg. 962349cc55cSDimitry Andric if (MI.isDebugValueList()) { 963349cc55cSDimitry Andric if (VTracker) 964349cc55cSDimitry Andric VTracker->defVar(MI, Properties, None); 965349cc55cSDimitry Andric if (TTracker) 966349cc55cSDimitry Andric TTracker->redefVar(MI, Properties, None); 967349cc55cSDimitry Andric return true; 968349cc55cSDimitry Andric } 969349cc55cSDimitry Andric 970e8d8bef9SDimitry Andric const MachineOperand &MO = MI.getOperand(0); 971e8d8bef9SDimitry Andric 972e8d8bef9SDimitry Andric // MLocTracker needs to know that this register is read, even if it's only 973e8d8bef9SDimitry Andric // read by a debug inst. 974e8d8bef9SDimitry Andric if (MO.isReg() && MO.getReg() != 0) 975e8d8bef9SDimitry Andric (void)MTracker->readReg(MO.getReg()); 976e8d8bef9SDimitry Andric 977e8d8bef9SDimitry Andric // If we're preparing for the second analysis (variables), the machine value 978e8d8bef9SDimitry Andric // locations are already solved, and we report this DBG_VALUE and the value 979e8d8bef9SDimitry Andric // it refers to to VLocTracker. 980e8d8bef9SDimitry Andric if (VTracker) { 981e8d8bef9SDimitry Andric if (MO.isReg()) { 982e8d8bef9SDimitry Andric // Feed defVar the new variable location, or if this is a 983e8d8bef9SDimitry Andric // DBG_VALUE $noreg, feed defVar None. 984e8d8bef9SDimitry Andric if (MO.getReg()) 985e8d8bef9SDimitry Andric VTracker->defVar(MI, Properties, MTracker->readReg(MO.getReg())); 986e8d8bef9SDimitry Andric else 987e8d8bef9SDimitry Andric VTracker->defVar(MI, Properties, None); 988e8d8bef9SDimitry Andric } else if (MI.getOperand(0).isImm() || MI.getOperand(0).isFPImm() || 989e8d8bef9SDimitry Andric MI.getOperand(0).isCImm()) { 990e8d8bef9SDimitry Andric VTracker->defVar(MI, MI.getOperand(0)); 991e8d8bef9SDimitry Andric } 992e8d8bef9SDimitry Andric } 993e8d8bef9SDimitry Andric 994e8d8bef9SDimitry Andric // If performing final tracking of transfers, report this variable definition 995e8d8bef9SDimitry Andric // to the TransferTracker too. 996e8d8bef9SDimitry Andric if (TTracker) 997e8d8bef9SDimitry Andric TTracker->redefVar(MI); 998e8d8bef9SDimitry Andric return true; 999e8d8bef9SDimitry Andric } 1000e8d8bef9SDimitry Andric 1001fe6060f1SDimitry Andric bool InstrRefBasedLDV::transferDebugInstrRef(MachineInstr &MI, 1002fe6060f1SDimitry Andric ValueIDNum **MLiveOuts, 1003fe6060f1SDimitry Andric ValueIDNum **MLiveIns) { 1004e8d8bef9SDimitry Andric if (!MI.isDebugRef()) 1005e8d8bef9SDimitry Andric return false; 1006e8d8bef9SDimitry Andric 1007e8d8bef9SDimitry Andric // Only handle this instruction when we are building the variable value 1008e8d8bef9SDimitry Andric // transfer function. 1009e8d8bef9SDimitry Andric if (!VTracker) 1010e8d8bef9SDimitry Andric return false; 1011e8d8bef9SDimitry Andric 1012e8d8bef9SDimitry Andric unsigned InstNo = MI.getOperand(0).getImm(); 1013e8d8bef9SDimitry Andric unsigned OpNo = MI.getOperand(1).getImm(); 1014e8d8bef9SDimitry Andric 1015e8d8bef9SDimitry Andric const DILocalVariable *Var = MI.getDebugVariable(); 1016e8d8bef9SDimitry Andric const DIExpression *Expr = MI.getDebugExpression(); 1017e8d8bef9SDimitry Andric const DILocation *DebugLoc = MI.getDebugLoc(); 1018e8d8bef9SDimitry Andric const DILocation *InlinedAt = DebugLoc->getInlinedAt(); 1019e8d8bef9SDimitry Andric assert(Var->isValidLocationForIntrinsic(DebugLoc) && 1020e8d8bef9SDimitry Andric "Expected inlined-at fields to agree"); 1021e8d8bef9SDimitry Andric 1022e8d8bef9SDimitry Andric DebugVariable V(Var, Expr, InlinedAt); 1023e8d8bef9SDimitry Andric 1024e8d8bef9SDimitry Andric auto *Scope = LS.findLexicalScope(MI.getDebugLoc().get()); 1025e8d8bef9SDimitry Andric if (Scope == nullptr) 1026e8d8bef9SDimitry Andric return true; // Handled by doing nothing. This variable is never in scope. 1027e8d8bef9SDimitry Andric 1028e8d8bef9SDimitry Andric const MachineFunction &MF = *MI.getParent()->getParent(); 1029e8d8bef9SDimitry Andric 1030e8d8bef9SDimitry Andric // Various optimizations may have happened to the value during codegen, 1031e8d8bef9SDimitry Andric // recorded in the value substitution table. Apply any substitutions to 1032fe6060f1SDimitry Andric // the instruction / operand number in this DBG_INSTR_REF, and collect 1033fe6060f1SDimitry Andric // any subregister extractions performed during optimization. 1034fe6060f1SDimitry Andric 1035fe6060f1SDimitry Andric // Create dummy substitution with Src set, for lookup. 1036fe6060f1SDimitry Andric auto SoughtSub = 1037fe6060f1SDimitry Andric MachineFunction::DebugSubstitution({InstNo, OpNo}, {0, 0}, 0); 1038fe6060f1SDimitry Andric 1039fe6060f1SDimitry Andric SmallVector<unsigned, 4> SeenSubregs; 1040fe6060f1SDimitry Andric auto LowerBoundIt = llvm::lower_bound(MF.DebugValueSubstitutions, SoughtSub); 1041fe6060f1SDimitry Andric while (LowerBoundIt != MF.DebugValueSubstitutions.end() && 1042fe6060f1SDimitry Andric LowerBoundIt->Src == SoughtSub.Src) { 1043fe6060f1SDimitry Andric std::tie(InstNo, OpNo) = LowerBoundIt->Dest; 1044fe6060f1SDimitry Andric SoughtSub.Src = LowerBoundIt->Dest; 1045fe6060f1SDimitry Andric if (unsigned Subreg = LowerBoundIt->Subreg) 1046fe6060f1SDimitry Andric SeenSubregs.push_back(Subreg); 1047fe6060f1SDimitry Andric LowerBoundIt = llvm::lower_bound(MF.DebugValueSubstitutions, SoughtSub); 1048e8d8bef9SDimitry Andric } 1049e8d8bef9SDimitry Andric 1050e8d8bef9SDimitry Andric // Default machine value number is <None> -- if no instruction defines 1051e8d8bef9SDimitry Andric // the corresponding value, it must have been optimized out. 1052e8d8bef9SDimitry Andric Optional<ValueIDNum> NewID = None; 1053e8d8bef9SDimitry Andric 1054e8d8bef9SDimitry Andric // Try to lookup the instruction number, and find the machine value number 1055fe6060f1SDimitry Andric // that it defines. It could be an instruction, or a PHI. 1056e8d8bef9SDimitry Andric auto InstrIt = DebugInstrNumToInstr.find(InstNo); 1057fe6060f1SDimitry Andric auto PHIIt = std::lower_bound(DebugPHINumToValue.begin(), 1058fe6060f1SDimitry Andric DebugPHINumToValue.end(), InstNo); 1059e8d8bef9SDimitry Andric if (InstrIt != DebugInstrNumToInstr.end()) { 1060e8d8bef9SDimitry Andric const MachineInstr &TargetInstr = *InstrIt->second.first; 1061e8d8bef9SDimitry Andric uint64_t BlockNo = TargetInstr.getParent()->getNumber(); 1062e8d8bef9SDimitry Andric 1063349cc55cSDimitry Andric // Pick out the designated operand. It might be a memory reference, if 1064349cc55cSDimitry Andric // a register def was folded into a stack store. 1065349cc55cSDimitry Andric if (OpNo == MachineFunction::DebugOperandMemNumber && 1066349cc55cSDimitry Andric TargetInstr.hasOneMemOperand()) { 1067349cc55cSDimitry Andric Optional<LocIdx> L = findLocationForMemOperand(TargetInstr); 1068349cc55cSDimitry Andric if (L) 1069349cc55cSDimitry Andric NewID = ValueIDNum(BlockNo, InstrIt->second.second, *L); 1070349cc55cSDimitry Andric } else if (OpNo != MachineFunction::DebugOperandMemNumber) { 1071e8d8bef9SDimitry Andric assert(OpNo < TargetInstr.getNumOperands()); 1072e8d8bef9SDimitry Andric const MachineOperand &MO = TargetInstr.getOperand(OpNo); 1073e8d8bef9SDimitry Andric 1074e8d8bef9SDimitry Andric // Today, this can only be a register. 1075e8d8bef9SDimitry Andric assert(MO.isReg() && MO.isDef()); 1076e8d8bef9SDimitry Andric 1077349cc55cSDimitry Andric unsigned LocID = MTracker->getLocID(MO.getReg()); 1078e8d8bef9SDimitry Andric LocIdx L = MTracker->LocIDToLocIdx[LocID]; 1079e8d8bef9SDimitry Andric NewID = ValueIDNum(BlockNo, InstrIt->second.second, L); 1080349cc55cSDimitry Andric } 1081349cc55cSDimitry Andric // else: NewID is left as None. 1082fe6060f1SDimitry Andric } else if (PHIIt != DebugPHINumToValue.end() && PHIIt->InstrNum == InstNo) { 1083fe6060f1SDimitry Andric // It's actually a PHI value. Which value it is might not be obvious, use 1084fe6060f1SDimitry Andric // the resolver helper to find out. 1085fe6060f1SDimitry Andric NewID = resolveDbgPHIs(*MI.getParent()->getParent(), MLiveOuts, MLiveIns, 1086fe6060f1SDimitry Andric MI, InstNo); 1087fe6060f1SDimitry Andric } 1088fe6060f1SDimitry Andric 1089fe6060f1SDimitry Andric // Apply any subregister extractions, in reverse. We might have seen code 1090fe6060f1SDimitry Andric // like this: 1091fe6060f1SDimitry Andric // CALL64 @foo, implicit-def $rax 1092fe6060f1SDimitry Andric // %0:gr64 = COPY $rax 1093fe6060f1SDimitry Andric // %1:gr32 = COPY %0.sub_32bit 1094fe6060f1SDimitry Andric // %2:gr16 = COPY %1.sub_16bit 1095fe6060f1SDimitry Andric // %3:gr8 = COPY %2.sub_8bit 1096fe6060f1SDimitry Andric // In which case each copy would have been recorded as a substitution with 1097fe6060f1SDimitry Andric // a subregister qualifier. Apply those qualifiers now. 1098fe6060f1SDimitry Andric if (NewID && !SeenSubregs.empty()) { 1099fe6060f1SDimitry Andric unsigned Offset = 0; 1100fe6060f1SDimitry Andric unsigned Size = 0; 1101fe6060f1SDimitry Andric 1102fe6060f1SDimitry Andric // Look at each subregister that we passed through, and progressively 1103fe6060f1SDimitry Andric // narrow in, accumulating any offsets that occur. Substitutions should 1104fe6060f1SDimitry Andric // only ever be the same or narrower width than what they read from; 1105fe6060f1SDimitry Andric // iterate in reverse order so that we go from wide to small. 1106fe6060f1SDimitry Andric for (unsigned Subreg : reverse(SeenSubregs)) { 1107fe6060f1SDimitry Andric unsigned ThisSize = TRI->getSubRegIdxSize(Subreg); 1108fe6060f1SDimitry Andric unsigned ThisOffset = TRI->getSubRegIdxOffset(Subreg); 1109fe6060f1SDimitry Andric Offset += ThisOffset; 1110fe6060f1SDimitry Andric Size = (Size == 0) ? ThisSize : std::min(Size, ThisSize); 1111fe6060f1SDimitry Andric } 1112fe6060f1SDimitry Andric 1113fe6060f1SDimitry Andric // If that worked, look for an appropriate subregister with the register 1114fe6060f1SDimitry Andric // where the define happens. Don't look at values that were defined during 1115fe6060f1SDimitry Andric // a stack write: we can't currently express register locations within 1116fe6060f1SDimitry Andric // spills. 1117fe6060f1SDimitry Andric LocIdx L = NewID->getLoc(); 1118fe6060f1SDimitry Andric if (NewID && !MTracker->isSpill(L)) { 1119fe6060f1SDimitry Andric // Find the register class for the register where this def happened. 1120fe6060f1SDimitry Andric // FIXME: no index for this? 1121fe6060f1SDimitry Andric Register Reg = MTracker->LocIdxToLocID[L]; 1122fe6060f1SDimitry Andric const TargetRegisterClass *TRC = nullptr; 1123fe6060f1SDimitry Andric for (auto *TRCI : TRI->regclasses()) 1124fe6060f1SDimitry Andric if (TRCI->contains(Reg)) 1125fe6060f1SDimitry Andric TRC = TRCI; 1126fe6060f1SDimitry Andric assert(TRC && "Couldn't find target register class?"); 1127fe6060f1SDimitry Andric 1128fe6060f1SDimitry Andric // If the register we have isn't the right size or in the right place, 1129fe6060f1SDimitry Andric // Try to find a subregister inside it. 1130fe6060f1SDimitry Andric unsigned MainRegSize = TRI->getRegSizeInBits(*TRC); 1131fe6060f1SDimitry Andric if (Size != MainRegSize || Offset) { 1132fe6060f1SDimitry Andric // Enumerate all subregisters, searching. 1133fe6060f1SDimitry Andric Register NewReg = 0; 1134fe6060f1SDimitry Andric for (MCSubRegIterator SRI(Reg, TRI, false); SRI.isValid(); ++SRI) { 1135fe6060f1SDimitry Andric unsigned Subreg = TRI->getSubRegIndex(Reg, *SRI); 1136fe6060f1SDimitry Andric unsigned SubregSize = TRI->getSubRegIdxSize(Subreg); 1137fe6060f1SDimitry Andric unsigned SubregOffset = TRI->getSubRegIdxOffset(Subreg); 1138fe6060f1SDimitry Andric if (SubregSize == Size && SubregOffset == Offset) { 1139fe6060f1SDimitry Andric NewReg = *SRI; 1140fe6060f1SDimitry Andric break; 1141fe6060f1SDimitry Andric } 1142fe6060f1SDimitry Andric } 1143fe6060f1SDimitry Andric 1144fe6060f1SDimitry Andric // If we didn't find anything: there's no way to express our value. 1145fe6060f1SDimitry Andric if (!NewReg) { 1146fe6060f1SDimitry Andric NewID = None; 1147fe6060f1SDimitry Andric } else { 1148fe6060f1SDimitry Andric // Re-state the value as being defined within the subregister 1149fe6060f1SDimitry Andric // that we found. 1150fe6060f1SDimitry Andric LocIdx NewLoc = MTracker->lookupOrTrackRegister(NewReg); 1151fe6060f1SDimitry Andric NewID = ValueIDNum(NewID->getBlock(), NewID->getInst(), NewLoc); 1152fe6060f1SDimitry Andric } 1153fe6060f1SDimitry Andric } 1154fe6060f1SDimitry Andric } else { 1155fe6060f1SDimitry Andric // If we can't handle subregisters, unset the new value. 1156fe6060f1SDimitry Andric NewID = None; 1157fe6060f1SDimitry Andric } 1158e8d8bef9SDimitry Andric } 1159e8d8bef9SDimitry Andric 1160e8d8bef9SDimitry Andric // We, we have a value number or None. Tell the variable value tracker about 1161e8d8bef9SDimitry Andric // it. The rest of this LiveDebugValues implementation acts exactly the same 1162e8d8bef9SDimitry Andric // for DBG_INSTR_REFs as DBG_VALUEs (just, the former can refer to values that 1163e8d8bef9SDimitry Andric // aren't immediately available). 1164e8d8bef9SDimitry Andric DbgValueProperties Properties(Expr, false); 1165e8d8bef9SDimitry Andric VTracker->defVar(MI, Properties, NewID); 1166e8d8bef9SDimitry Andric 1167e8d8bef9SDimitry Andric // If we're on the final pass through the function, decompose this INSTR_REF 1168e8d8bef9SDimitry Andric // into a plain DBG_VALUE. 1169e8d8bef9SDimitry Andric if (!TTracker) 1170e8d8bef9SDimitry Andric return true; 1171e8d8bef9SDimitry Andric 1172e8d8bef9SDimitry Andric // Pick a location for the machine value number, if such a location exists. 1173e8d8bef9SDimitry Andric // (This information could be stored in TransferTracker to make it faster). 1174e8d8bef9SDimitry Andric Optional<LocIdx> FoundLoc = None; 1175e8d8bef9SDimitry Andric for (auto Location : MTracker->locations()) { 1176e8d8bef9SDimitry Andric LocIdx CurL = Location.Idx; 1177349cc55cSDimitry Andric ValueIDNum ID = MTracker->readMLoc(CurL); 1178e8d8bef9SDimitry Andric if (NewID && ID == NewID) { 1179e8d8bef9SDimitry Andric // If this is the first location with that value, pick it. Otherwise, 1180e8d8bef9SDimitry Andric // consider whether it's a "longer term" location. 1181e8d8bef9SDimitry Andric if (!FoundLoc) { 1182e8d8bef9SDimitry Andric FoundLoc = CurL; 1183e8d8bef9SDimitry Andric continue; 1184e8d8bef9SDimitry Andric } 1185e8d8bef9SDimitry Andric 1186e8d8bef9SDimitry Andric if (MTracker->isSpill(CurL)) 1187e8d8bef9SDimitry Andric FoundLoc = CurL; // Spills are a longer term location. 1188e8d8bef9SDimitry Andric else if (!MTracker->isSpill(*FoundLoc) && 1189e8d8bef9SDimitry Andric !MTracker->isSpill(CurL) && 1190e8d8bef9SDimitry Andric !isCalleeSaved(*FoundLoc) && 1191e8d8bef9SDimitry Andric isCalleeSaved(CurL)) 1192e8d8bef9SDimitry Andric FoundLoc = CurL; // Callee saved regs are longer term than normal. 1193e8d8bef9SDimitry Andric } 1194e8d8bef9SDimitry Andric } 1195e8d8bef9SDimitry Andric 1196e8d8bef9SDimitry Andric // Tell transfer tracker that the variable value has changed. 1197e8d8bef9SDimitry Andric TTracker->redefVar(MI, Properties, FoundLoc); 1198e8d8bef9SDimitry Andric 1199e8d8bef9SDimitry Andric // If there was a value with no location; but the value is defined in a 1200e8d8bef9SDimitry Andric // later instruction in this block, this is a block-local use-before-def. 1201e8d8bef9SDimitry Andric if (!FoundLoc && NewID && NewID->getBlock() == CurBB && 1202e8d8bef9SDimitry Andric NewID->getInst() > CurInst) 1203e8d8bef9SDimitry Andric TTracker->addUseBeforeDef(V, {MI.getDebugExpression(), false}, *NewID); 1204e8d8bef9SDimitry Andric 1205e8d8bef9SDimitry Andric // Produce a DBG_VALUE representing what this DBG_INSTR_REF meant. 1206e8d8bef9SDimitry Andric // This DBG_VALUE is potentially a $noreg / undefined location, if 1207e8d8bef9SDimitry Andric // FoundLoc is None. 1208e8d8bef9SDimitry Andric // (XXX -- could morph the DBG_INSTR_REF in the future). 1209e8d8bef9SDimitry Andric MachineInstr *DbgMI = MTracker->emitLoc(FoundLoc, V, Properties); 1210e8d8bef9SDimitry Andric TTracker->PendingDbgValues.push_back(DbgMI); 1211e8d8bef9SDimitry Andric TTracker->flushDbgValues(MI.getIterator(), nullptr); 1212fe6060f1SDimitry Andric return true; 1213fe6060f1SDimitry Andric } 1214fe6060f1SDimitry Andric 1215fe6060f1SDimitry Andric bool InstrRefBasedLDV::transferDebugPHI(MachineInstr &MI) { 1216fe6060f1SDimitry Andric if (!MI.isDebugPHI()) 1217fe6060f1SDimitry Andric return false; 1218fe6060f1SDimitry Andric 1219fe6060f1SDimitry Andric // Analyse these only when solving the machine value location problem. 1220fe6060f1SDimitry Andric if (VTracker || TTracker) 1221fe6060f1SDimitry Andric return true; 1222fe6060f1SDimitry Andric 1223fe6060f1SDimitry Andric // First operand is the value location, either a stack slot or register. 1224fe6060f1SDimitry Andric // Second is the debug instruction number of the original PHI. 1225fe6060f1SDimitry Andric const MachineOperand &MO = MI.getOperand(0); 1226fe6060f1SDimitry Andric unsigned InstrNum = MI.getOperand(1).getImm(); 1227fe6060f1SDimitry Andric 1228fe6060f1SDimitry Andric if (MO.isReg()) { 1229fe6060f1SDimitry Andric // The value is whatever's currently in the register. Read and record it, 1230fe6060f1SDimitry Andric // to be analysed later. 1231fe6060f1SDimitry Andric Register Reg = MO.getReg(); 1232fe6060f1SDimitry Andric ValueIDNum Num = MTracker->readReg(Reg); 1233fe6060f1SDimitry Andric auto PHIRec = DebugPHIRecord( 1234fe6060f1SDimitry Andric {InstrNum, MI.getParent(), Num, MTracker->lookupOrTrackRegister(Reg)}); 1235fe6060f1SDimitry Andric DebugPHINumToValue.push_back(PHIRec); 1236349cc55cSDimitry Andric 1237349cc55cSDimitry Andric // Ensure this register is tracked. 1238349cc55cSDimitry Andric for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI) 1239349cc55cSDimitry Andric MTracker->lookupOrTrackRegister(*RAI); 1240fe6060f1SDimitry Andric } else { 1241fe6060f1SDimitry Andric // The value is whatever's in this stack slot. 1242fe6060f1SDimitry Andric assert(MO.isFI()); 1243fe6060f1SDimitry Andric unsigned FI = MO.getIndex(); 1244fe6060f1SDimitry Andric 1245fe6060f1SDimitry Andric // If the stack slot is dead, then this was optimized away. 1246fe6060f1SDimitry Andric // FIXME: stack slot colouring should account for slots that get merged. 1247fe6060f1SDimitry Andric if (MFI->isDeadObjectIndex(FI)) 1248fe6060f1SDimitry Andric return true; 1249fe6060f1SDimitry Andric 1250349cc55cSDimitry Andric // Identify this spill slot, ensure it's tracked. 1251fe6060f1SDimitry Andric Register Base; 1252fe6060f1SDimitry Andric StackOffset Offs = TFI->getFrameIndexReference(*MI.getMF(), FI, Base); 1253fe6060f1SDimitry Andric SpillLoc SL = {Base, Offs}; 1254349cc55cSDimitry Andric SpillLocationNo SpillNo = MTracker->getOrTrackSpillLoc(SL); 1255fe6060f1SDimitry Andric 1256349cc55cSDimitry Andric // Problem: what value should we extract from the stack? LLVM does not 1257349cc55cSDimitry Andric // record what size the last store to the slot was, and it would become 1258349cc55cSDimitry Andric // sketchy after stack slot colouring anyway. Take a look at what values 1259349cc55cSDimitry Andric // are stored on the stack, and pick the largest one that wasn't def'd 1260349cc55cSDimitry Andric // by a spill (i.e., the value most likely to have been def'd in a register 1261349cc55cSDimitry Andric // and then spilt. 1262349cc55cSDimitry Andric std::array<unsigned, 4> CandidateSizes = {64, 32, 16, 8}; 1263349cc55cSDimitry Andric Optional<ValueIDNum> Result = None; 1264349cc55cSDimitry Andric Optional<LocIdx> SpillLoc = None; 12650eae32dcSDimitry Andric for (unsigned CS : CandidateSizes) { 12660eae32dcSDimitry Andric unsigned SpillID = MTracker->getLocID(SpillNo, {CS, 0}); 1267349cc55cSDimitry Andric SpillLoc = MTracker->getSpillMLoc(SpillID); 1268349cc55cSDimitry Andric ValueIDNum Val = MTracker->readMLoc(*SpillLoc); 1269349cc55cSDimitry Andric // If this value was defined in it's own position, then it was probably 1270349cc55cSDimitry Andric // an aliasing index of a small value that was spilt. 1271349cc55cSDimitry Andric if (Val.getLoc() != SpillLoc->asU64()) { 1272349cc55cSDimitry Andric Result = Val; 1273349cc55cSDimitry Andric break; 1274349cc55cSDimitry Andric } 1275349cc55cSDimitry Andric } 1276349cc55cSDimitry Andric 1277349cc55cSDimitry Andric // If we didn't find anything, we're probably looking at a PHI, or a memory 1278349cc55cSDimitry Andric // store folded into an instruction. FIXME: Take a guess that's it's 64 1279349cc55cSDimitry Andric // bits. This isn't ideal, but tracking the size that the spill is 1280349cc55cSDimitry Andric // "supposed" to be is more complex, and benefits a small number of 1281349cc55cSDimitry Andric // locations. 1282349cc55cSDimitry Andric if (!Result) { 1283349cc55cSDimitry Andric unsigned SpillID = MTracker->getLocID(SpillNo, {64, 0}); 1284349cc55cSDimitry Andric SpillLoc = MTracker->getSpillMLoc(SpillID); 1285349cc55cSDimitry Andric Result = MTracker->readMLoc(*SpillLoc); 1286349cc55cSDimitry Andric } 1287fe6060f1SDimitry Andric 1288fe6060f1SDimitry Andric // Record this DBG_PHI for later analysis. 1289349cc55cSDimitry Andric auto DbgPHI = DebugPHIRecord({InstrNum, MI.getParent(), *Result, *SpillLoc}); 1290fe6060f1SDimitry Andric DebugPHINumToValue.push_back(DbgPHI); 1291fe6060f1SDimitry Andric } 1292e8d8bef9SDimitry Andric 1293e8d8bef9SDimitry Andric return true; 1294e8d8bef9SDimitry Andric } 1295e8d8bef9SDimitry Andric 1296e8d8bef9SDimitry Andric void InstrRefBasedLDV::transferRegisterDef(MachineInstr &MI) { 1297e8d8bef9SDimitry Andric // Meta Instructions do not affect the debug liveness of any register they 1298e8d8bef9SDimitry Andric // define. 1299e8d8bef9SDimitry Andric if (MI.isImplicitDef()) { 1300e8d8bef9SDimitry Andric // Except when there's an implicit def, and the location it's defining has 1301e8d8bef9SDimitry Andric // no value number. The whole point of an implicit def is to announce that 1302e8d8bef9SDimitry Andric // the register is live, without be specific about it's value. So define 1303e8d8bef9SDimitry Andric // a value if there isn't one already. 1304e8d8bef9SDimitry Andric ValueIDNum Num = MTracker->readReg(MI.getOperand(0).getReg()); 1305e8d8bef9SDimitry Andric // Has a legitimate value -> ignore the implicit def. 1306e8d8bef9SDimitry Andric if (Num.getLoc() != 0) 1307e8d8bef9SDimitry Andric return; 1308e8d8bef9SDimitry Andric // Otherwise, def it here. 1309e8d8bef9SDimitry Andric } else if (MI.isMetaInstruction()) 1310e8d8bef9SDimitry Andric return; 1311e8d8bef9SDimitry Andric 13124824e7fdSDimitry Andric // We always ignore SP defines on call instructions, they don't actually 13134824e7fdSDimitry Andric // change the value of the stack pointer... except for win32's _chkstk. This 13144824e7fdSDimitry Andric // is rare: filter quickly for the common case (no stack adjustments, not a 13154824e7fdSDimitry Andric // call, etc). If it is a call that modifies SP, recognise the SP register 13164824e7fdSDimitry Andric // defs. 13174824e7fdSDimitry Andric bool CallChangesSP = false; 13184824e7fdSDimitry Andric if (AdjustsStackInCalls && MI.isCall() && MI.getOperand(0).isSymbol() && 13194824e7fdSDimitry Andric !strcmp(MI.getOperand(0).getSymbolName(), StackProbeSymbolName.data())) 13204824e7fdSDimitry Andric CallChangesSP = true; 13214824e7fdSDimitry Andric 13224824e7fdSDimitry Andric // Test whether we should ignore a def of this register due to it being part 13234824e7fdSDimitry Andric // of the stack pointer. 13244824e7fdSDimitry Andric auto IgnoreSPAlias = [this, &MI, CallChangesSP](Register R) -> bool { 13254824e7fdSDimitry Andric if (CallChangesSP) 13264824e7fdSDimitry Andric return false; 13274824e7fdSDimitry Andric return MI.isCall() && MTracker->SPAliases.count(R); 13284824e7fdSDimitry Andric }; 13294824e7fdSDimitry Andric 1330e8d8bef9SDimitry Andric // Find the regs killed by MI, and find regmasks of preserved regs. 1331e8d8bef9SDimitry Andric // Max out the number of statically allocated elements in `DeadRegs`, as this 1332e8d8bef9SDimitry Andric // prevents fallback to std::set::count() operations. 1333e8d8bef9SDimitry Andric SmallSet<uint32_t, 32> DeadRegs; 1334e8d8bef9SDimitry Andric SmallVector<const uint32_t *, 4> RegMasks; 1335e8d8bef9SDimitry Andric SmallVector<const MachineOperand *, 4> RegMaskPtrs; 1336e8d8bef9SDimitry Andric for (const MachineOperand &MO : MI.operands()) { 1337e8d8bef9SDimitry Andric // Determine whether the operand is a register def. 1338e8d8bef9SDimitry Andric if (MO.isReg() && MO.isDef() && MO.getReg() && 1339e8d8bef9SDimitry Andric Register::isPhysicalRegister(MO.getReg()) && 13404824e7fdSDimitry Andric !IgnoreSPAlias(MO.getReg())) { 1341e8d8bef9SDimitry Andric // Remove ranges of all aliased registers. 1342e8d8bef9SDimitry Andric for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI) 1343e8d8bef9SDimitry Andric // FIXME: Can we break out of this loop early if no insertion occurs? 1344e8d8bef9SDimitry Andric DeadRegs.insert(*RAI); 1345e8d8bef9SDimitry Andric } else if (MO.isRegMask()) { 1346e8d8bef9SDimitry Andric RegMasks.push_back(MO.getRegMask()); 1347e8d8bef9SDimitry Andric RegMaskPtrs.push_back(&MO); 1348e8d8bef9SDimitry Andric } 1349e8d8bef9SDimitry Andric } 1350e8d8bef9SDimitry Andric 1351e8d8bef9SDimitry Andric // Tell MLocTracker about all definitions, of regmasks and otherwise. 1352e8d8bef9SDimitry Andric for (uint32_t DeadReg : DeadRegs) 1353e8d8bef9SDimitry Andric MTracker->defReg(DeadReg, CurBB, CurInst); 1354e8d8bef9SDimitry Andric 1355e8d8bef9SDimitry Andric for (auto *MO : RegMaskPtrs) 1356e8d8bef9SDimitry Andric MTracker->writeRegMask(MO, CurBB, CurInst); 1357fe6060f1SDimitry Andric 1358349cc55cSDimitry Andric // If this instruction writes to a spill slot, def that slot. 1359349cc55cSDimitry Andric if (hasFoldedStackStore(MI)) { 1360349cc55cSDimitry Andric SpillLocationNo SpillNo = extractSpillBaseRegAndOffset(MI); 1361349cc55cSDimitry Andric for (unsigned int I = 0; I < MTracker->NumSlotIdxes; ++I) { 1362349cc55cSDimitry Andric unsigned SpillID = MTracker->getSpillIDWithIdx(SpillNo, I); 1363349cc55cSDimitry Andric LocIdx L = MTracker->getSpillMLoc(SpillID); 1364349cc55cSDimitry Andric MTracker->setMLoc(L, ValueIDNum(CurBB, CurInst, L)); 1365349cc55cSDimitry Andric } 1366349cc55cSDimitry Andric } 1367349cc55cSDimitry Andric 1368fe6060f1SDimitry Andric if (!TTracker) 1369fe6060f1SDimitry Andric return; 1370fe6060f1SDimitry Andric 1371fe6060f1SDimitry Andric // When committing variable values to locations: tell transfer tracker that 1372fe6060f1SDimitry Andric // we've clobbered things. It may be able to recover the variable from a 1373fe6060f1SDimitry Andric // different location. 1374fe6060f1SDimitry Andric 1375fe6060f1SDimitry Andric // Inform TTracker about any direct clobbers. 1376fe6060f1SDimitry Andric for (uint32_t DeadReg : DeadRegs) { 1377fe6060f1SDimitry Andric LocIdx Loc = MTracker->lookupOrTrackRegister(DeadReg); 1378fe6060f1SDimitry Andric TTracker->clobberMloc(Loc, MI.getIterator(), false); 1379fe6060f1SDimitry Andric } 1380fe6060f1SDimitry Andric 1381fe6060f1SDimitry Andric // Look for any clobbers performed by a register mask. Only test locations 1382fe6060f1SDimitry Andric // that are actually being tracked. 1383*1fd87a68SDimitry Andric if (!RegMaskPtrs.empty()) { 1384fe6060f1SDimitry Andric for (auto L : MTracker->locations()) { 1385fe6060f1SDimitry Andric // Stack locations can't be clobbered by regmasks. 1386fe6060f1SDimitry Andric if (MTracker->isSpill(L.Idx)) 1387fe6060f1SDimitry Andric continue; 1388fe6060f1SDimitry Andric 1389fe6060f1SDimitry Andric Register Reg = MTracker->LocIdxToLocID[L.Idx]; 13904824e7fdSDimitry Andric if (IgnoreSPAlias(Reg)) 13914824e7fdSDimitry Andric continue; 13924824e7fdSDimitry Andric 1393fe6060f1SDimitry Andric for (auto *MO : RegMaskPtrs) 1394fe6060f1SDimitry Andric if (MO->clobbersPhysReg(Reg)) 1395fe6060f1SDimitry Andric TTracker->clobberMloc(L.Idx, MI.getIterator(), false); 1396fe6060f1SDimitry Andric } 1397*1fd87a68SDimitry Andric } 1398349cc55cSDimitry Andric 1399349cc55cSDimitry Andric // Tell TTracker about any folded stack store. 1400349cc55cSDimitry Andric if (hasFoldedStackStore(MI)) { 1401349cc55cSDimitry Andric SpillLocationNo SpillNo = extractSpillBaseRegAndOffset(MI); 1402349cc55cSDimitry Andric for (unsigned int I = 0; I < MTracker->NumSlotIdxes; ++I) { 1403349cc55cSDimitry Andric unsigned SpillID = MTracker->getSpillIDWithIdx(SpillNo, I); 1404349cc55cSDimitry Andric LocIdx L = MTracker->getSpillMLoc(SpillID); 1405349cc55cSDimitry Andric TTracker->clobberMloc(L, MI.getIterator(), true); 1406349cc55cSDimitry Andric } 1407349cc55cSDimitry Andric } 1408e8d8bef9SDimitry Andric } 1409e8d8bef9SDimitry Andric 1410e8d8bef9SDimitry Andric void InstrRefBasedLDV::performCopy(Register SrcRegNum, Register DstRegNum) { 1411349cc55cSDimitry Andric // In all circumstances, re-def all aliases. It's definitely a new value now. 1412349cc55cSDimitry Andric for (MCRegAliasIterator RAI(DstRegNum, TRI, true); RAI.isValid(); ++RAI) 1413349cc55cSDimitry Andric MTracker->defReg(*RAI, CurBB, CurInst); 1414e8d8bef9SDimitry Andric 1415349cc55cSDimitry Andric ValueIDNum SrcValue = MTracker->readReg(SrcRegNum); 1416e8d8bef9SDimitry Andric MTracker->setReg(DstRegNum, SrcValue); 1417e8d8bef9SDimitry Andric 1418349cc55cSDimitry Andric // Copy subregisters from one location to another. 1419e8d8bef9SDimitry Andric for (MCSubRegIndexIterator SRI(SrcRegNum, TRI); SRI.isValid(); ++SRI) { 1420e8d8bef9SDimitry Andric unsigned SrcSubReg = SRI.getSubReg(); 1421e8d8bef9SDimitry Andric unsigned SubRegIdx = SRI.getSubRegIndex(); 1422e8d8bef9SDimitry Andric unsigned DstSubReg = TRI->getSubReg(DstRegNum, SubRegIdx); 1423e8d8bef9SDimitry Andric if (!DstSubReg) 1424e8d8bef9SDimitry Andric continue; 1425e8d8bef9SDimitry Andric 1426e8d8bef9SDimitry Andric // Do copy. There are two matching subregisters, the source value should 1427e8d8bef9SDimitry Andric // have been def'd when the super-reg was, the latter might not be tracked 1428e8d8bef9SDimitry Andric // yet. 1429349cc55cSDimitry Andric // This will force SrcSubReg to be tracked, if it isn't yet. Will read 1430349cc55cSDimitry Andric // mphi values if it wasn't tracked. 1431349cc55cSDimitry Andric LocIdx SrcL = MTracker->lookupOrTrackRegister(SrcSubReg); 1432349cc55cSDimitry Andric LocIdx DstL = MTracker->lookupOrTrackRegister(DstSubReg); 1433349cc55cSDimitry Andric (void)SrcL; 1434e8d8bef9SDimitry Andric (void)DstL; 1435349cc55cSDimitry Andric ValueIDNum CpyValue = MTracker->readReg(SrcSubReg); 1436e8d8bef9SDimitry Andric 1437e8d8bef9SDimitry Andric MTracker->setReg(DstSubReg, CpyValue); 1438e8d8bef9SDimitry Andric } 1439e8d8bef9SDimitry Andric } 1440e8d8bef9SDimitry Andric 1441e8d8bef9SDimitry Andric bool InstrRefBasedLDV::isSpillInstruction(const MachineInstr &MI, 1442e8d8bef9SDimitry Andric MachineFunction *MF) { 1443e8d8bef9SDimitry Andric // TODO: Handle multiple stores folded into one. 1444e8d8bef9SDimitry Andric if (!MI.hasOneMemOperand()) 1445e8d8bef9SDimitry Andric return false; 1446e8d8bef9SDimitry Andric 1447349cc55cSDimitry Andric // Reject any memory operand that's aliased -- we can't guarantee its value. 1448349cc55cSDimitry Andric auto MMOI = MI.memoperands_begin(); 1449349cc55cSDimitry Andric const PseudoSourceValue *PVal = (*MMOI)->getPseudoValue(); 1450349cc55cSDimitry Andric if (PVal->isAliased(MFI)) 1451349cc55cSDimitry Andric return false; 1452349cc55cSDimitry Andric 1453e8d8bef9SDimitry Andric if (!MI.getSpillSize(TII) && !MI.getFoldedSpillSize(TII)) 1454e8d8bef9SDimitry Andric return false; // This is not a spill instruction, since no valid size was 1455e8d8bef9SDimitry Andric // returned from either function. 1456e8d8bef9SDimitry Andric 1457e8d8bef9SDimitry Andric return true; 1458e8d8bef9SDimitry Andric } 1459e8d8bef9SDimitry Andric 1460e8d8bef9SDimitry Andric bool InstrRefBasedLDV::isLocationSpill(const MachineInstr &MI, 1461e8d8bef9SDimitry Andric MachineFunction *MF, unsigned &Reg) { 1462e8d8bef9SDimitry Andric if (!isSpillInstruction(MI, MF)) 1463e8d8bef9SDimitry Andric return false; 1464e8d8bef9SDimitry Andric 1465e8d8bef9SDimitry Andric int FI; 1466e8d8bef9SDimitry Andric Reg = TII->isStoreToStackSlotPostFE(MI, FI); 1467e8d8bef9SDimitry Andric return Reg != 0; 1468e8d8bef9SDimitry Andric } 1469e8d8bef9SDimitry Andric 1470349cc55cSDimitry Andric Optional<SpillLocationNo> 1471e8d8bef9SDimitry Andric InstrRefBasedLDV::isRestoreInstruction(const MachineInstr &MI, 1472e8d8bef9SDimitry Andric MachineFunction *MF, unsigned &Reg) { 1473e8d8bef9SDimitry Andric if (!MI.hasOneMemOperand()) 1474e8d8bef9SDimitry Andric return None; 1475e8d8bef9SDimitry Andric 1476e8d8bef9SDimitry Andric // FIXME: Handle folded restore instructions with more than one memory 1477e8d8bef9SDimitry Andric // operand. 1478e8d8bef9SDimitry Andric if (MI.getRestoreSize(TII)) { 1479e8d8bef9SDimitry Andric Reg = MI.getOperand(0).getReg(); 1480e8d8bef9SDimitry Andric return extractSpillBaseRegAndOffset(MI); 1481e8d8bef9SDimitry Andric } 1482e8d8bef9SDimitry Andric return None; 1483e8d8bef9SDimitry Andric } 1484e8d8bef9SDimitry Andric 1485e8d8bef9SDimitry Andric bool InstrRefBasedLDV::transferSpillOrRestoreInst(MachineInstr &MI) { 1486e8d8bef9SDimitry Andric // XXX -- it's too difficult to implement VarLocBasedImpl's stack location 1487e8d8bef9SDimitry Andric // limitations under the new model. Therefore, when comparing them, compare 1488e8d8bef9SDimitry Andric // versions that don't attempt spills or restores at all. 1489e8d8bef9SDimitry Andric if (EmulateOldLDV) 1490e8d8bef9SDimitry Andric return false; 1491e8d8bef9SDimitry Andric 1492349cc55cSDimitry Andric // Strictly limit ourselves to plain loads and stores, not all instructions 1493349cc55cSDimitry Andric // that can access the stack. 1494349cc55cSDimitry Andric int DummyFI = -1; 1495349cc55cSDimitry Andric if (!TII->isStoreToStackSlotPostFE(MI, DummyFI) && 1496349cc55cSDimitry Andric !TII->isLoadFromStackSlotPostFE(MI, DummyFI)) 1497349cc55cSDimitry Andric return false; 1498349cc55cSDimitry Andric 1499e8d8bef9SDimitry Andric MachineFunction *MF = MI.getMF(); 1500e8d8bef9SDimitry Andric unsigned Reg; 1501e8d8bef9SDimitry Andric 1502e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Examining instruction: "; MI.dump();); 1503e8d8bef9SDimitry Andric 1504349cc55cSDimitry Andric // Strictly limit ourselves to plain loads and stores, not all instructions 1505349cc55cSDimitry Andric // that can access the stack. 1506349cc55cSDimitry Andric int FIDummy; 1507349cc55cSDimitry Andric if (!TII->isStoreToStackSlotPostFE(MI, FIDummy) && 1508349cc55cSDimitry Andric !TII->isLoadFromStackSlotPostFE(MI, FIDummy)) 1509349cc55cSDimitry Andric return false; 1510349cc55cSDimitry Andric 1511e8d8bef9SDimitry Andric // First, if there are any DBG_VALUEs pointing at a spill slot that is 1512e8d8bef9SDimitry Andric // written to, terminate that variable location. The value in memory 1513e8d8bef9SDimitry Andric // will have changed. DbgEntityHistoryCalculator doesn't try to detect this. 1514e8d8bef9SDimitry Andric if (isSpillInstruction(MI, MF)) { 1515349cc55cSDimitry Andric SpillLocationNo Loc = extractSpillBaseRegAndOffset(MI); 1516e8d8bef9SDimitry Andric 1517349cc55cSDimitry Andric // Un-set this location and clobber, so that earlier locations don't 1518349cc55cSDimitry Andric // continue past this store. 1519349cc55cSDimitry Andric for (unsigned SlotIdx = 0; SlotIdx < MTracker->NumSlotIdxes; ++SlotIdx) { 1520349cc55cSDimitry Andric unsigned SpillID = MTracker->getSpillIDWithIdx(Loc, SlotIdx); 1521349cc55cSDimitry Andric Optional<LocIdx> MLoc = MTracker->getSpillMLoc(SpillID); 1522349cc55cSDimitry Andric if (!MLoc) 1523349cc55cSDimitry Andric continue; 1524349cc55cSDimitry Andric 1525349cc55cSDimitry Andric // We need to over-write the stack slot with something (here, a def at 1526349cc55cSDimitry Andric // this instruction) to ensure no values are preserved in this stack slot 1527349cc55cSDimitry Andric // after the spill. It also prevents TTracker from trying to recover the 1528349cc55cSDimitry Andric // location and re-installing it in the same place. 1529349cc55cSDimitry Andric ValueIDNum Def(CurBB, CurInst, *MLoc); 1530349cc55cSDimitry Andric MTracker->setMLoc(*MLoc, Def); 1531349cc55cSDimitry Andric if (TTracker) 1532e8d8bef9SDimitry Andric TTracker->clobberMloc(*MLoc, MI.getIterator()); 1533e8d8bef9SDimitry Andric } 1534e8d8bef9SDimitry Andric } 1535e8d8bef9SDimitry Andric 1536e8d8bef9SDimitry Andric // Try to recognise spill and restore instructions that may transfer a value. 1537e8d8bef9SDimitry Andric if (isLocationSpill(MI, MF, Reg)) { 1538349cc55cSDimitry Andric SpillLocationNo Loc = extractSpillBaseRegAndOffset(MI); 1539e8d8bef9SDimitry Andric 1540349cc55cSDimitry Andric auto DoTransfer = [&](Register SrcReg, unsigned SpillID) { 1541349cc55cSDimitry Andric auto ReadValue = MTracker->readReg(SrcReg); 1542349cc55cSDimitry Andric LocIdx DstLoc = MTracker->getSpillMLoc(SpillID); 1543349cc55cSDimitry Andric MTracker->setMLoc(DstLoc, ReadValue); 1544e8d8bef9SDimitry Andric 1545349cc55cSDimitry Andric if (TTracker) { 1546349cc55cSDimitry Andric LocIdx SrcLoc = MTracker->getRegMLoc(SrcReg); 1547349cc55cSDimitry Andric TTracker->transferMlocs(SrcLoc, DstLoc, MI.getIterator()); 1548e8d8bef9SDimitry Andric } 1549349cc55cSDimitry Andric }; 1550349cc55cSDimitry Andric 1551349cc55cSDimitry Andric // Then, transfer subreg bits. 1552349cc55cSDimitry Andric for (MCSubRegIterator SRI(Reg, TRI, false); SRI.isValid(); ++SRI) { 1553349cc55cSDimitry Andric // Ensure this reg is tracked, 1554349cc55cSDimitry Andric (void)MTracker->lookupOrTrackRegister(*SRI); 1555349cc55cSDimitry Andric unsigned SubregIdx = TRI->getSubRegIndex(Reg, *SRI); 1556349cc55cSDimitry Andric unsigned SpillID = MTracker->getLocID(Loc, SubregIdx); 1557349cc55cSDimitry Andric DoTransfer(*SRI, SpillID); 1558349cc55cSDimitry Andric } 1559349cc55cSDimitry Andric 1560349cc55cSDimitry Andric // Directly lookup size of main source reg, and transfer. 1561349cc55cSDimitry Andric unsigned Size = TRI->getRegSizeInBits(Reg, *MRI); 1562349cc55cSDimitry Andric unsigned SpillID = MTracker->getLocID(Loc, {Size, 0}); 1563349cc55cSDimitry Andric DoTransfer(Reg, SpillID); 1564349cc55cSDimitry Andric } else { 1565349cc55cSDimitry Andric Optional<SpillLocationNo> OptLoc = isRestoreInstruction(MI, MF, Reg); 1566349cc55cSDimitry Andric if (!OptLoc) 1567349cc55cSDimitry Andric return false; 1568349cc55cSDimitry Andric SpillLocationNo Loc = *OptLoc; 1569349cc55cSDimitry Andric 1570349cc55cSDimitry Andric // Assumption: we're reading from the base of the stack slot, not some 1571349cc55cSDimitry Andric // offset into it. It seems very unlikely LLVM would ever generate 1572349cc55cSDimitry Andric // restores where this wasn't true. This then becomes a question of what 1573349cc55cSDimitry Andric // subregisters in the destination register line up with positions in the 1574349cc55cSDimitry Andric // stack slot. 1575349cc55cSDimitry Andric 1576349cc55cSDimitry Andric // Def all registers that alias the destination. 1577349cc55cSDimitry Andric for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI) 1578349cc55cSDimitry Andric MTracker->defReg(*RAI, CurBB, CurInst); 1579349cc55cSDimitry Andric 1580349cc55cSDimitry Andric // Now find subregisters within the destination register, and load values 1581349cc55cSDimitry Andric // from stack slot positions. 1582349cc55cSDimitry Andric auto DoTransfer = [&](Register DestReg, unsigned SpillID) { 1583349cc55cSDimitry Andric LocIdx SrcIdx = MTracker->getSpillMLoc(SpillID); 1584349cc55cSDimitry Andric auto ReadValue = MTracker->readMLoc(SrcIdx); 1585349cc55cSDimitry Andric MTracker->setReg(DestReg, ReadValue); 1586349cc55cSDimitry Andric 1587349cc55cSDimitry Andric if (TTracker) { 1588349cc55cSDimitry Andric LocIdx DstLoc = MTracker->getRegMLoc(DestReg); 1589349cc55cSDimitry Andric TTracker->transferMlocs(SrcIdx, DstLoc, MI.getIterator()); 1590349cc55cSDimitry Andric } 1591349cc55cSDimitry Andric }; 1592349cc55cSDimitry Andric 1593349cc55cSDimitry Andric for (MCSubRegIterator SRI(Reg, TRI, false); SRI.isValid(); ++SRI) { 1594349cc55cSDimitry Andric unsigned Subreg = TRI->getSubRegIndex(Reg, *SRI); 1595349cc55cSDimitry Andric unsigned SpillID = MTracker->getLocID(Loc, Subreg); 1596349cc55cSDimitry Andric DoTransfer(*SRI, SpillID); 1597349cc55cSDimitry Andric } 1598349cc55cSDimitry Andric 1599349cc55cSDimitry Andric // Directly look up this registers slot idx by size, and transfer. 1600349cc55cSDimitry Andric unsigned Size = TRI->getRegSizeInBits(Reg, *MRI); 1601349cc55cSDimitry Andric unsigned SpillID = MTracker->getLocID(Loc, {Size, 0}); 1602349cc55cSDimitry Andric DoTransfer(Reg, SpillID); 1603e8d8bef9SDimitry Andric } 1604e8d8bef9SDimitry Andric return true; 1605e8d8bef9SDimitry Andric } 1606e8d8bef9SDimitry Andric 1607e8d8bef9SDimitry Andric bool InstrRefBasedLDV::transferRegisterCopy(MachineInstr &MI) { 1608e8d8bef9SDimitry Andric auto DestSrc = TII->isCopyInstr(MI); 1609e8d8bef9SDimitry Andric if (!DestSrc) 1610e8d8bef9SDimitry Andric return false; 1611e8d8bef9SDimitry Andric 1612e8d8bef9SDimitry Andric const MachineOperand *DestRegOp = DestSrc->Destination; 1613e8d8bef9SDimitry Andric const MachineOperand *SrcRegOp = DestSrc->Source; 1614e8d8bef9SDimitry Andric 1615e8d8bef9SDimitry Andric auto isCalleeSavedReg = [&](unsigned Reg) { 1616e8d8bef9SDimitry Andric for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI) 1617e8d8bef9SDimitry Andric if (CalleeSavedRegs.test(*RAI)) 1618e8d8bef9SDimitry Andric return true; 1619e8d8bef9SDimitry Andric return false; 1620e8d8bef9SDimitry Andric }; 1621e8d8bef9SDimitry Andric 1622e8d8bef9SDimitry Andric Register SrcReg = SrcRegOp->getReg(); 1623e8d8bef9SDimitry Andric Register DestReg = DestRegOp->getReg(); 1624e8d8bef9SDimitry Andric 1625e8d8bef9SDimitry Andric // Ignore identity copies. Yep, these make it as far as LiveDebugValues. 1626e8d8bef9SDimitry Andric if (SrcReg == DestReg) 1627e8d8bef9SDimitry Andric return true; 1628e8d8bef9SDimitry Andric 1629e8d8bef9SDimitry Andric // For emulating VarLocBasedImpl: 1630e8d8bef9SDimitry Andric // We want to recognize instructions where destination register is callee 1631e8d8bef9SDimitry Andric // saved register. If register that could be clobbered by the call is 1632e8d8bef9SDimitry Andric // included, there would be a great chance that it is going to be clobbered 1633e8d8bef9SDimitry Andric // soon. It is more likely that previous register, which is callee saved, is 1634e8d8bef9SDimitry Andric // going to stay unclobbered longer, even if it is killed. 1635e8d8bef9SDimitry Andric // 1636e8d8bef9SDimitry Andric // For InstrRefBasedImpl, we can track multiple locations per value, so 1637e8d8bef9SDimitry Andric // ignore this condition. 1638e8d8bef9SDimitry Andric if (EmulateOldLDV && !isCalleeSavedReg(DestReg)) 1639e8d8bef9SDimitry Andric return false; 1640e8d8bef9SDimitry Andric 1641e8d8bef9SDimitry Andric // InstrRefBasedImpl only followed killing copies. 1642e8d8bef9SDimitry Andric if (EmulateOldLDV && !SrcRegOp->isKill()) 1643e8d8bef9SDimitry Andric return false; 1644e8d8bef9SDimitry Andric 1645e8d8bef9SDimitry Andric // Copy MTracker info, including subregs if available. 1646e8d8bef9SDimitry Andric InstrRefBasedLDV::performCopy(SrcReg, DestReg); 1647e8d8bef9SDimitry Andric 1648e8d8bef9SDimitry Andric // Only produce a transfer of DBG_VALUE within a block where old LDV 1649e8d8bef9SDimitry Andric // would have. We might make use of the additional value tracking in some 1650e8d8bef9SDimitry Andric // other way, later. 1651e8d8bef9SDimitry Andric if (TTracker && isCalleeSavedReg(DestReg) && SrcRegOp->isKill()) 1652e8d8bef9SDimitry Andric TTracker->transferMlocs(MTracker->getRegMLoc(SrcReg), 1653e8d8bef9SDimitry Andric MTracker->getRegMLoc(DestReg), MI.getIterator()); 1654e8d8bef9SDimitry Andric 1655e8d8bef9SDimitry Andric // VarLocBasedImpl would quit tracking the old location after copying. 1656e8d8bef9SDimitry Andric if (EmulateOldLDV && SrcReg != DestReg) 1657e8d8bef9SDimitry Andric MTracker->defReg(SrcReg, CurBB, CurInst); 1658e8d8bef9SDimitry Andric 1659fe6060f1SDimitry Andric // Finally, the copy might have clobbered variables based on the destination 1660fe6060f1SDimitry Andric // register. Tell TTracker about it, in case a backup location exists. 1661fe6060f1SDimitry Andric if (TTracker) { 1662fe6060f1SDimitry Andric for (MCRegAliasIterator RAI(DestReg, TRI, true); RAI.isValid(); ++RAI) { 1663fe6060f1SDimitry Andric LocIdx ClobberedLoc = MTracker->getRegMLoc(*RAI); 1664fe6060f1SDimitry Andric TTracker->clobberMloc(ClobberedLoc, MI.getIterator(), false); 1665fe6060f1SDimitry Andric } 1666fe6060f1SDimitry Andric } 1667fe6060f1SDimitry Andric 1668e8d8bef9SDimitry Andric return true; 1669e8d8bef9SDimitry Andric } 1670e8d8bef9SDimitry Andric 1671e8d8bef9SDimitry Andric /// Accumulate a mapping between each DILocalVariable fragment and other 1672e8d8bef9SDimitry Andric /// fragments of that DILocalVariable which overlap. This reduces work during 1673e8d8bef9SDimitry Andric /// the data-flow stage from "Find any overlapping fragments" to "Check if the 1674e8d8bef9SDimitry Andric /// known-to-overlap fragments are present". 16754824e7fdSDimitry Andric /// \param MI A previously unprocessed debug instruction to analyze for 1676e8d8bef9SDimitry Andric /// fragment usage. 1677e8d8bef9SDimitry Andric void InstrRefBasedLDV::accumulateFragmentMap(MachineInstr &MI) { 16784824e7fdSDimitry Andric assert(MI.isDebugValue() || MI.isDebugRef()); 1679e8d8bef9SDimitry Andric DebugVariable MIVar(MI.getDebugVariable(), MI.getDebugExpression(), 1680e8d8bef9SDimitry Andric MI.getDebugLoc()->getInlinedAt()); 1681e8d8bef9SDimitry Andric FragmentInfo ThisFragment = MIVar.getFragmentOrDefault(); 1682e8d8bef9SDimitry Andric 1683e8d8bef9SDimitry Andric // If this is the first sighting of this variable, then we are guaranteed 1684e8d8bef9SDimitry Andric // there are currently no overlapping fragments either. Initialize the set 1685e8d8bef9SDimitry Andric // of seen fragments, record no overlaps for the current one, and return. 1686e8d8bef9SDimitry Andric auto SeenIt = SeenFragments.find(MIVar.getVariable()); 1687e8d8bef9SDimitry Andric if (SeenIt == SeenFragments.end()) { 1688e8d8bef9SDimitry Andric SmallSet<FragmentInfo, 4> OneFragment; 1689e8d8bef9SDimitry Andric OneFragment.insert(ThisFragment); 1690e8d8bef9SDimitry Andric SeenFragments.insert({MIVar.getVariable(), OneFragment}); 1691e8d8bef9SDimitry Andric 1692e8d8bef9SDimitry Andric OverlapFragments.insert({{MIVar.getVariable(), ThisFragment}, {}}); 1693e8d8bef9SDimitry Andric return; 1694e8d8bef9SDimitry Andric } 1695e8d8bef9SDimitry Andric 1696e8d8bef9SDimitry Andric // If this particular Variable/Fragment pair already exists in the overlap 1697e8d8bef9SDimitry Andric // map, it has already been accounted for. 1698e8d8bef9SDimitry Andric auto IsInOLapMap = 1699e8d8bef9SDimitry Andric OverlapFragments.insert({{MIVar.getVariable(), ThisFragment}, {}}); 1700e8d8bef9SDimitry Andric if (!IsInOLapMap.second) 1701e8d8bef9SDimitry Andric return; 1702e8d8bef9SDimitry Andric 1703e8d8bef9SDimitry Andric auto &ThisFragmentsOverlaps = IsInOLapMap.first->second; 1704e8d8bef9SDimitry Andric auto &AllSeenFragments = SeenIt->second; 1705e8d8bef9SDimitry Andric 1706e8d8bef9SDimitry Andric // Otherwise, examine all other seen fragments for this variable, with "this" 1707e8d8bef9SDimitry Andric // fragment being a previously unseen fragment. Record any pair of 1708e8d8bef9SDimitry Andric // overlapping fragments. 1709e8d8bef9SDimitry Andric for (auto &ASeenFragment : AllSeenFragments) { 1710e8d8bef9SDimitry Andric // Does this previously seen fragment overlap? 1711e8d8bef9SDimitry Andric if (DIExpression::fragmentsOverlap(ThisFragment, ASeenFragment)) { 1712e8d8bef9SDimitry Andric // Yes: Mark the current fragment as being overlapped. 1713e8d8bef9SDimitry Andric ThisFragmentsOverlaps.push_back(ASeenFragment); 1714e8d8bef9SDimitry Andric // Mark the previously seen fragment as being overlapped by the current 1715e8d8bef9SDimitry Andric // one. 1716e8d8bef9SDimitry Andric auto ASeenFragmentsOverlaps = 1717e8d8bef9SDimitry Andric OverlapFragments.find({MIVar.getVariable(), ASeenFragment}); 1718e8d8bef9SDimitry Andric assert(ASeenFragmentsOverlaps != OverlapFragments.end() && 1719e8d8bef9SDimitry Andric "Previously seen var fragment has no vector of overlaps"); 1720e8d8bef9SDimitry Andric ASeenFragmentsOverlaps->second.push_back(ThisFragment); 1721e8d8bef9SDimitry Andric } 1722e8d8bef9SDimitry Andric } 1723e8d8bef9SDimitry Andric 1724e8d8bef9SDimitry Andric AllSeenFragments.insert(ThisFragment); 1725e8d8bef9SDimitry Andric } 1726e8d8bef9SDimitry Andric 1727fe6060f1SDimitry Andric void InstrRefBasedLDV::process(MachineInstr &MI, ValueIDNum **MLiveOuts, 1728fe6060f1SDimitry Andric ValueIDNum **MLiveIns) { 1729e8d8bef9SDimitry Andric // Try to interpret an MI as a debug or transfer instruction. Only if it's 1730e8d8bef9SDimitry Andric // none of these should we interpret it's register defs as new value 1731e8d8bef9SDimitry Andric // definitions. 1732e8d8bef9SDimitry Andric if (transferDebugValue(MI)) 1733e8d8bef9SDimitry Andric return; 1734fe6060f1SDimitry Andric if (transferDebugInstrRef(MI, MLiveOuts, MLiveIns)) 1735fe6060f1SDimitry Andric return; 1736fe6060f1SDimitry Andric if (transferDebugPHI(MI)) 1737e8d8bef9SDimitry Andric return; 1738e8d8bef9SDimitry Andric if (transferRegisterCopy(MI)) 1739e8d8bef9SDimitry Andric return; 1740e8d8bef9SDimitry Andric if (transferSpillOrRestoreInst(MI)) 1741e8d8bef9SDimitry Andric return; 1742e8d8bef9SDimitry Andric transferRegisterDef(MI); 1743e8d8bef9SDimitry Andric } 1744e8d8bef9SDimitry Andric 1745e8d8bef9SDimitry Andric void InstrRefBasedLDV::produceMLocTransferFunction( 1746e8d8bef9SDimitry Andric MachineFunction &MF, SmallVectorImpl<MLocTransferMap> &MLocTransfer, 1747e8d8bef9SDimitry Andric unsigned MaxNumBlocks) { 1748e8d8bef9SDimitry Andric // Because we try to optimize around register mask operands by ignoring regs 1749e8d8bef9SDimitry Andric // that aren't currently tracked, we set up something ugly for later: RegMask 1750e8d8bef9SDimitry Andric // operands that are seen earlier than the first use of a register, still need 1751e8d8bef9SDimitry Andric // to clobber that register in the transfer function. But this information 1752e8d8bef9SDimitry Andric // isn't actively recorded. Instead, we track each RegMask used in each block, 1753e8d8bef9SDimitry Andric // and accumulated the clobbered but untracked registers in each block into 1754e8d8bef9SDimitry Andric // the following bitvector. Later, if new values are tracked, we can add 1755e8d8bef9SDimitry Andric // appropriate clobbers. 1756e8d8bef9SDimitry Andric SmallVector<BitVector, 32> BlockMasks; 1757e8d8bef9SDimitry Andric BlockMasks.resize(MaxNumBlocks); 1758e8d8bef9SDimitry Andric 1759e8d8bef9SDimitry Andric // Reserve one bit per register for the masks described above. 1760e8d8bef9SDimitry Andric unsigned BVWords = MachineOperand::getRegMaskSize(TRI->getNumRegs()); 1761e8d8bef9SDimitry Andric for (auto &BV : BlockMasks) 1762e8d8bef9SDimitry Andric BV.resize(TRI->getNumRegs(), true); 1763e8d8bef9SDimitry Andric 1764e8d8bef9SDimitry Andric // Step through all instructions and inhale the transfer function. 1765e8d8bef9SDimitry Andric for (auto &MBB : MF) { 1766e8d8bef9SDimitry Andric // Object fields that are read by trackers to know where we are in the 1767e8d8bef9SDimitry Andric // function. 1768e8d8bef9SDimitry Andric CurBB = MBB.getNumber(); 1769e8d8bef9SDimitry Andric CurInst = 1; 1770e8d8bef9SDimitry Andric 1771e8d8bef9SDimitry Andric // Set all machine locations to a PHI value. For transfer function 1772e8d8bef9SDimitry Andric // production only, this signifies the live-in value to the block. 1773e8d8bef9SDimitry Andric MTracker->reset(); 1774e8d8bef9SDimitry Andric MTracker->setMPhis(CurBB); 1775e8d8bef9SDimitry Andric 1776e8d8bef9SDimitry Andric // Step through each instruction in this block. 1777e8d8bef9SDimitry Andric for (auto &MI : MBB) { 1778e8d8bef9SDimitry Andric process(MI); 1779e8d8bef9SDimitry Andric // Also accumulate fragment map. 17804824e7fdSDimitry Andric if (MI.isDebugValue() || MI.isDebugRef()) 1781e8d8bef9SDimitry Andric accumulateFragmentMap(MI); 1782e8d8bef9SDimitry Andric 1783e8d8bef9SDimitry Andric // Create a map from the instruction number (if present) to the 1784e8d8bef9SDimitry Andric // MachineInstr and its position. 1785e8d8bef9SDimitry Andric if (uint64_t InstrNo = MI.peekDebugInstrNum()) { 1786e8d8bef9SDimitry Andric auto InstrAndPos = std::make_pair(&MI, CurInst); 1787e8d8bef9SDimitry Andric auto InsertResult = 1788e8d8bef9SDimitry Andric DebugInstrNumToInstr.insert(std::make_pair(InstrNo, InstrAndPos)); 1789e8d8bef9SDimitry Andric 1790e8d8bef9SDimitry Andric // There should never be duplicate instruction numbers. 1791e8d8bef9SDimitry Andric assert(InsertResult.second); 1792e8d8bef9SDimitry Andric (void)InsertResult; 1793e8d8bef9SDimitry Andric } 1794e8d8bef9SDimitry Andric 1795e8d8bef9SDimitry Andric ++CurInst; 1796e8d8bef9SDimitry Andric } 1797e8d8bef9SDimitry Andric 1798e8d8bef9SDimitry Andric // Produce the transfer function, a map of machine location to new value. If 1799e8d8bef9SDimitry Andric // any machine location has the live-in phi value from the start of the 1800e8d8bef9SDimitry Andric // block, it's live-through and doesn't need recording in the transfer 1801e8d8bef9SDimitry Andric // function. 1802e8d8bef9SDimitry Andric for (auto Location : MTracker->locations()) { 1803e8d8bef9SDimitry Andric LocIdx Idx = Location.Idx; 1804e8d8bef9SDimitry Andric ValueIDNum &P = Location.Value; 1805e8d8bef9SDimitry Andric if (P.isPHI() && P.getLoc() == Idx.asU64()) 1806e8d8bef9SDimitry Andric continue; 1807e8d8bef9SDimitry Andric 1808e8d8bef9SDimitry Andric // Insert-or-update. 1809e8d8bef9SDimitry Andric auto &TransferMap = MLocTransfer[CurBB]; 1810e8d8bef9SDimitry Andric auto Result = TransferMap.insert(std::make_pair(Idx.asU64(), P)); 1811e8d8bef9SDimitry Andric if (!Result.second) 1812e8d8bef9SDimitry Andric Result.first->second = P; 1813e8d8bef9SDimitry Andric } 1814e8d8bef9SDimitry Andric 1815e8d8bef9SDimitry Andric // Accumulate any bitmask operands into the clobberred reg mask for this 1816e8d8bef9SDimitry Andric // block. 1817e8d8bef9SDimitry Andric for (auto &P : MTracker->Masks) { 1818e8d8bef9SDimitry Andric BlockMasks[CurBB].clearBitsNotInMask(P.first->getRegMask(), BVWords); 1819e8d8bef9SDimitry Andric } 1820e8d8bef9SDimitry Andric } 1821e8d8bef9SDimitry Andric 1822e8d8bef9SDimitry Andric // Compute a bitvector of all the registers that are tracked in this block. 1823e8d8bef9SDimitry Andric BitVector UsedRegs(TRI->getNumRegs()); 1824e8d8bef9SDimitry Andric for (auto Location : MTracker->locations()) { 1825e8d8bef9SDimitry Andric unsigned ID = MTracker->LocIdxToLocID[Location.Idx]; 1826349cc55cSDimitry Andric // Ignore stack slots, and aliases of the stack pointer. 1827349cc55cSDimitry Andric if (ID >= TRI->getNumRegs() || MTracker->SPAliases.count(ID)) 1828e8d8bef9SDimitry Andric continue; 1829e8d8bef9SDimitry Andric UsedRegs.set(ID); 1830e8d8bef9SDimitry Andric } 1831e8d8bef9SDimitry Andric 1832e8d8bef9SDimitry Andric // Check that any regmask-clobber of a register that gets tracked, is not 1833e8d8bef9SDimitry Andric // live-through in the transfer function. It needs to be clobbered at the 1834e8d8bef9SDimitry Andric // very least. 1835e8d8bef9SDimitry Andric for (unsigned int I = 0; I < MaxNumBlocks; ++I) { 1836e8d8bef9SDimitry Andric BitVector &BV = BlockMasks[I]; 1837e8d8bef9SDimitry Andric BV.flip(); 1838e8d8bef9SDimitry Andric BV &= UsedRegs; 1839e8d8bef9SDimitry Andric // This produces all the bits that we clobber, but also use. Check that 1840e8d8bef9SDimitry Andric // they're all clobbered or at least set in the designated transfer 1841e8d8bef9SDimitry Andric // elem. 1842e8d8bef9SDimitry Andric for (unsigned Bit : BV.set_bits()) { 1843349cc55cSDimitry Andric unsigned ID = MTracker->getLocID(Bit); 1844e8d8bef9SDimitry Andric LocIdx Idx = MTracker->LocIDToLocIdx[ID]; 1845e8d8bef9SDimitry Andric auto &TransferMap = MLocTransfer[I]; 1846e8d8bef9SDimitry Andric 1847e8d8bef9SDimitry Andric // Install a value representing the fact that this location is effectively 1848e8d8bef9SDimitry Andric // written to in this block. As there's no reserved value, instead use 1849e8d8bef9SDimitry Andric // a value number that is never generated. Pick the value number for the 1850e8d8bef9SDimitry Andric // first instruction in the block, def'ing this location, which we know 1851e8d8bef9SDimitry Andric // this block never used anyway. 1852e8d8bef9SDimitry Andric ValueIDNum NotGeneratedNum = ValueIDNum(I, 1, Idx); 1853e8d8bef9SDimitry Andric auto Result = 1854e8d8bef9SDimitry Andric TransferMap.insert(std::make_pair(Idx.asU64(), NotGeneratedNum)); 1855e8d8bef9SDimitry Andric if (!Result.second) { 1856e8d8bef9SDimitry Andric ValueIDNum &ValueID = Result.first->second; 1857e8d8bef9SDimitry Andric if (ValueID.getBlock() == I && ValueID.isPHI()) 1858e8d8bef9SDimitry Andric // It was left as live-through. Set it to clobbered. 1859e8d8bef9SDimitry Andric ValueID = NotGeneratedNum; 1860e8d8bef9SDimitry Andric } 1861e8d8bef9SDimitry Andric } 1862e8d8bef9SDimitry Andric } 1863e8d8bef9SDimitry Andric } 1864e8d8bef9SDimitry Andric 1865349cc55cSDimitry Andric bool InstrRefBasedLDV::mlocJoin( 1866349cc55cSDimitry Andric MachineBasicBlock &MBB, SmallPtrSet<const MachineBasicBlock *, 16> &Visited, 1867e8d8bef9SDimitry Andric ValueIDNum **OutLocs, ValueIDNum *InLocs) { 1868e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "join MBB: " << MBB.getNumber() << "\n"); 1869e8d8bef9SDimitry Andric bool Changed = false; 1870e8d8bef9SDimitry Andric 1871349cc55cSDimitry Andric // Handle value-propagation when control flow merges on entry to a block. For 1872349cc55cSDimitry Andric // any location without a PHI already placed, the location has the same value 1873349cc55cSDimitry Andric // as its predecessors. If a PHI is placed, test to see whether it's now a 1874349cc55cSDimitry Andric // redundant PHI that we can eliminate. 1875349cc55cSDimitry Andric 1876e8d8bef9SDimitry Andric SmallVector<const MachineBasicBlock *, 8> BlockOrders; 1877349cc55cSDimitry Andric for (auto Pred : MBB.predecessors()) 1878e8d8bef9SDimitry Andric BlockOrders.push_back(Pred); 1879e8d8bef9SDimitry Andric 1880e8d8bef9SDimitry Andric // Visit predecessors in RPOT order. 1881e8d8bef9SDimitry Andric auto Cmp = [&](const MachineBasicBlock *A, const MachineBasicBlock *B) { 1882e8d8bef9SDimitry Andric return BBToOrder.find(A)->second < BBToOrder.find(B)->second; 1883e8d8bef9SDimitry Andric }; 1884e8d8bef9SDimitry Andric llvm::sort(BlockOrders, Cmp); 1885e8d8bef9SDimitry Andric 1886e8d8bef9SDimitry Andric // Skip entry block. 1887e8d8bef9SDimitry Andric if (BlockOrders.size() == 0) 1888349cc55cSDimitry Andric return false; 1889e8d8bef9SDimitry Andric 1890349cc55cSDimitry Andric // Step through all machine locations, look at each predecessor and test 1891349cc55cSDimitry Andric // whether we can eliminate redundant PHIs. 1892e8d8bef9SDimitry Andric for (auto Location : MTracker->locations()) { 1893e8d8bef9SDimitry Andric LocIdx Idx = Location.Idx; 1894349cc55cSDimitry Andric 1895e8d8bef9SDimitry Andric // Pick out the first predecessors live-out value for this location. It's 1896349cc55cSDimitry Andric // guaranteed to not be a backedge, as we order by RPO. 1897349cc55cSDimitry Andric ValueIDNum FirstVal = OutLocs[BlockOrders[0]->getNumber()][Idx.asU64()]; 1898e8d8bef9SDimitry Andric 1899349cc55cSDimitry Andric // If we've already eliminated a PHI here, do no further checking, just 1900349cc55cSDimitry Andric // propagate the first live-in value into this block. 1901349cc55cSDimitry Andric if (InLocs[Idx.asU64()] != ValueIDNum(MBB.getNumber(), 0, Idx)) { 1902349cc55cSDimitry Andric if (InLocs[Idx.asU64()] != FirstVal) { 1903349cc55cSDimitry Andric InLocs[Idx.asU64()] = FirstVal; 1904349cc55cSDimitry Andric Changed |= true; 1905349cc55cSDimitry Andric } 1906349cc55cSDimitry Andric continue; 1907349cc55cSDimitry Andric } 1908349cc55cSDimitry Andric 1909349cc55cSDimitry Andric // We're now examining a PHI to see whether it's un-necessary. Loop around 1910349cc55cSDimitry Andric // the other live-in values and test whether they're all the same. 1911e8d8bef9SDimitry Andric bool Disagree = false; 1912e8d8bef9SDimitry Andric for (unsigned int I = 1; I < BlockOrders.size(); ++I) { 1913349cc55cSDimitry Andric const MachineBasicBlock *PredMBB = BlockOrders[I]; 1914349cc55cSDimitry Andric const ValueIDNum &PredLiveOut = 1915349cc55cSDimitry Andric OutLocs[PredMBB->getNumber()][Idx.asU64()]; 1916349cc55cSDimitry Andric 1917349cc55cSDimitry Andric // Incoming values agree, continue trying to eliminate this PHI. 1918349cc55cSDimitry Andric if (FirstVal == PredLiveOut) 1919349cc55cSDimitry Andric continue; 1920349cc55cSDimitry Andric 1921349cc55cSDimitry Andric // We can also accept a PHI value that feeds back into itself. 1922349cc55cSDimitry Andric if (PredLiveOut == ValueIDNum(MBB.getNumber(), 0, Idx)) 1923349cc55cSDimitry Andric continue; 1924349cc55cSDimitry Andric 1925e8d8bef9SDimitry Andric // Live-out of a predecessor disagrees with the first predecessor. 1926e8d8bef9SDimitry Andric Disagree = true; 1927e8d8bef9SDimitry Andric } 1928e8d8bef9SDimitry Andric 1929349cc55cSDimitry Andric // No disagreement? No PHI. Otherwise, leave the PHI in live-ins. 1930349cc55cSDimitry Andric if (!Disagree) { 1931349cc55cSDimitry Andric InLocs[Idx.asU64()] = FirstVal; 1932e8d8bef9SDimitry Andric Changed |= true; 1933e8d8bef9SDimitry Andric } 1934e8d8bef9SDimitry Andric } 1935e8d8bef9SDimitry Andric 1936e8d8bef9SDimitry Andric // TODO: Reimplement NumInserted and NumRemoved. 1937349cc55cSDimitry Andric return Changed; 1938e8d8bef9SDimitry Andric } 1939e8d8bef9SDimitry Andric 1940349cc55cSDimitry Andric void InstrRefBasedLDV::findStackIndexInterference( 1941349cc55cSDimitry Andric SmallVectorImpl<unsigned> &Slots) { 1942349cc55cSDimitry Andric // We could spend a bit of time finding the exact, minimal, set of stack 1943349cc55cSDimitry Andric // indexes that interfere with each other, much like reg units. Or, we can 1944349cc55cSDimitry Andric // rely on the fact that: 1945349cc55cSDimitry Andric // * The smallest / lowest index will interfere with everything at zero 1946349cc55cSDimitry Andric // offset, which will be the largest set of registers, 1947349cc55cSDimitry Andric // * Most indexes with non-zero offset will end up being interference units 1948349cc55cSDimitry Andric // anyway. 1949349cc55cSDimitry Andric // So just pick those out and return them. 1950349cc55cSDimitry Andric 1951349cc55cSDimitry Andric // We can rely on a single-byte stack index existing already, because we 1952349cc55cSDimitry Andric // initialize them in MLocTracker. 1953349cc55cSDimitry Andric auto It = MTracker->StackSlotIdxes.find({8, 0}); 1954349cc55cSDimitry Andric assert(It != MTracker->StackSlotIdxes.end()); 1955349cc55cSDimitry Andric Slots.push_back(It->second); 1956349cc55cSDimitry Andric 1957349cc55cSDimitry Andric // Find anything that has a non-zero offset and add that too. 1958349cc55cSDimitry Andric for (auto &Pair : MTracker->StackSlotIdxes) { 1959349cc55cSDimitry Andric // Is offset zero? If so, ignore. 1960349cc55cSDimitry Andric if (!Pair.first.second) 1961349cc55cSDimitry Andric continue; 1962349cc55cSDimitry Andric Slots.push_back(Pair.second); 1963349cc55cSDimitry Andric } 1964349cc55cSDimitry Andric } 1965349cc55cSDimitry Andric 1966349cc55cSDimitry Andric void InstrRefBasedLDV::placeMLocPHIs( 1967349cc55cSDimitry Andric MachineFunction &MF, SmallPtrSetImpl<MachineBasicBlock *> &AllBlocks, 1968349cc55cSDimitry Andric ValueIDNum **MInLocs, SmallVectorImpl<MLocTransferMap> &MLocTransfer) { 1969349cc55cSDimitry Andric SmallVector<unsigned, 4> StackUnits; 1970349cc55cSDimitry Andric findStackIndexInterference(StackUnits); 1971349cc55cSDimitry Andric 1972349cc55cSDimitry Andric // To avoid repeatedly running the PHI placement algorithm, leverage the 1973349cc55cSDimitry Andric // fact that a def of register MUST also def its register units. Find the 1974349cc55cSDimitry Andric // units for registers, place PHIs for them, and then replicate them for 1975349cc55cSDimitry Andric // aliasing registers. Some inputs that are never def'd (DBG_PHIs of 1976349cc55cSDimitry Andric // arguments) don't lead to register units being tracked, just place PHIs for 1977349cc55cSDimitry Andric // those registers directly. Stack slots have their own form of "unit", 1978349cc55cSDimitry Andric // store them to one side. 1979349cc55cSDimitry Andric SmallSet<Register, 32> RegUnitsToPHIUp; 1980349cc55cSDimitry Andric SmallSet<LocIdx, 32> NormalLocsToPHI; 1981349cc55cSDimitry Andric SmallSet<SpillLocationNo, 32> StackSlots; 1982349cc55cSDimitry Andric for (auto Location : MTracker->locations()) { 1983349cc55cSDimitry Andric LocIdx L = Location.Idx; 1984349cc55cSDimitry Andric if (MTracker->isSpill(L)) { 1985349cc55cSDimitry Andric StackSlots.insert(MTracker->locIDToSpill(MTracker->LocIdxToLocID[L])); 1986349cc55cSDimitry Andric continue; 1987349cc55cSDimitry Andric } 1988349cc55cSDimitry Andric 1989349cc55cSDimitry Andric Register R = MTracker->LocIdxToLocID[L]; 1990349cc55cSDimitry Andric SmallSet<Register, 8> FoundRegUnits; 1991349cc55cSDimitry Andric bool AnyIllegal = false; 1992349cc55cSDimitry Andric for (MCRegUnitIterator RUI(R.asMCReg(), TRI); RUI.isValid(); ++RUI) { 1993349cc55cSDimitry Andric for (MCRegUnitRootIterator URoot(*RUI, TRI); URoot.isValid(); ++URoot){ 1994349cc55cSDimitry Andric if (!MTracker->isRegisterTracked(*URoot)) { 1995349cc55cSDimitry Andric // Not all roots were loaded into the tracking map: this register 1996349cc55cSDimitry Andric // isn't actually def'd anywhere, we only read from it. Generate PHIs 1997349cc55cSDimitry Andric // for this reg, but don't iterate units. 1998349cc55cSDimitry Andric AnyIllegal = true; 1999349cc55cSDimitry Andric } else { 2000349cc55cSDimitry Andric FoundRegUnits.insert(*URoot); 2001349cc55cSDimitry Andric } 2002349cc55cSDimitry Andric } 2003349cc55cSDimitry Andric } 2004349cc55cSDimitry Andric 2005349cc55cSDimitry Andric if (AnyIllegal) { 2006349cc55cSDimitry Andric NormalLocsToPHI.insert(L); 2007349cc55cSDimitry Andric continue; 2008349cc55cSDimitry Andric } 2009349cc55cSDimitry Andric 2010349cc55cSDimitry Andric RegUnitsToPHIUp.insert(FoundRegUnits.begin(), FoundRegUnits.end()); 2011349cc55cSDimitry Andric } 2012349cc55cSDimitry Andric 2013349cc55cSDimitry Andric // Lambda to fetch PHIs for a given location, and write into the PHIBlocks 2014349cc55cSDimitry Andric // collection. 2015349cc55cSDimitry Andric SmallVector<MachineBasicBlock *, 32> PHIBlocks; 2016349cc55cSDimitry Andric auto CollectPHIsForLoc = [&](LocIdx L) { 2017349cc55cSDimitry Andric // Collect the set of defs. 2018349cc55cSDimitry Andric SmallPtrSet<MachineBasicBlock *, 32> DefBlocks; 2019349cc55cSDimitry Andric for (unsigned int I = 0; I < OrderToBB.size(); ++I) { 2020349cc55cSDimitry Andric MachineBasicBlock *MBB = OrderToBB[I]; 2021349cc55cSDimitry Andric const auto &TransferFunc = MLocTransfer[MBB->getNumber()]; 2022349cc55cSDimitry Andric if (TransferFunc.find(L) != TransferFunc.end()) 2023349cc55cSDimitry Andric DefBlocks.insert(MBB); 2024349cc55cSDimitry Andric } 2025349cc55cSDimitry Andric 2026349cc55cSDimitry Andric // The entry block defs the location too: it's the live-in / argument value. 2027349cc55cSDimitry Andric // Only insert if there are other defs though; everything is trivially live 2028349cc55cSDimitry Andric // through otherwise. 2029349cc55cSDimitry Andric if (!DefBlocks.empty()) 2030349cc55cSDimitry Andric DefBlocks.insert(&*MF.begin()); 2031349cc55cSDimitry Andric 2032349cc55cSDimitry Andric // Ask the SSA construction algorithm where we should put PHIs. Clear 2033349cc55cSDimitry Andric // anything that might have been hanging around from earlier. 2034349cc55cSDimitry Andric PHIBlocks.clear(); 2035349cc55cSDimitry Andric BlockPHIPlacement(AllBlocks, DefBlocks, PHIBlocks); 2036349cc55cSDimitry Andric }; 2037349cc55cSDimitry Andric 2038349cc55cSDimitry Andric auto InstallPHIsAtLoc = [&PHIBlocks, &MInLocs](LocIdx L) { 2039349cc55cSDimitry Andric for (const MachineBasicBlock *MBB : PHIBlocks) 2040349cc55cSDimitry Andric MInLocs[MBB->getNumber()][L.asU64()] = ValueIDNum(MBB->getNumber(), 0, L); 2041349cc55cSDimitry Andric }; 2042349cc55cSDimitry Andric 2043349cc55cSDimitry Andric // For locations with no reg units, just place PHIs. 2044349cc55cSDimitry Andric for (LocIdx L : NormalLocsToPHI) { 2045349cc55cSDimitry Andric CollectPHIsForLoc(L); 2046349cc55cSDimitry Andric // Install those PHI values into the live-in value array. 2047349cc55cSDimitry Andric InstallPHIsAtLoc(L); 2048349cc55cSDimitry Andric } 2049349cc55cSDimitry Andric 2050349cc55cSDimitry Andric // For stack slots, calculate PHIs for the equivalent of the units, then 2051349cc55cSDimitry Andric // install for each index. 2052349cc55cSDimitry Andric for (SpillLocationNo Slot : StackSlots) { 2053349cc55cSDimitry Andric for (unsigned Idx : StackUnits) { 2054349cc55cSDimitry Andric unsigned SpillID = MTracker->getSpillIDWithIdx(Slot, Idx); 2055349cc55cSDimitry Andric LocIdx L = MTracker->getSpillMLoc(SpillID); 2056349cc55cSDimitry Andric CollectPHIsForLoc(L); 2057349cc55cSDimitry Andric InstallPHIsAtLoc(L); 2058349cc55cSDimitry Andric 2059349cc55cSDimitry Andric // Find anything that aliases this stack index, install PHIs for it too. 2060349cc55cSDimitry Andric unsigned Size, Offset; 2061349cc55cSDimitry Andric std::tie(Size, Offset) = MTracker->StackIdxesToPos[Idx]; 2062349cc55cSDimitry Andric for (auto &Pair : MTracker->StackSlotIdxes) { 2063349cc55cSDimitry Andric unsigned ThisSize, ThisOffset; 2064349cc55cSDimitry Andric std::tie(ThisSize, ThisOffset) = Pair.first; 2065349cc55cSDimitry Andric if (ThisSize + ThisOffset <= Offset || Size + Offset <= ThisOffset) 2066349cc55cSDimitry Andric continue; 2067349cc55cSDimitry Andric 2068349cc55cSDimitry Andric unsigned ThisID = MTracker->getSpillIDWithIdx(Slot, Pair.second); 2069349cc55cSDimitry Andric LocIdx ThisL = MTracker->getSpillMLoc(ThisID); 2070349cc55cSDimitry Andric InstallPHIsAtLoc(ThisL); 2071349cc55cSDimitry Andric } 2072349cc55cSDimitry Andric } 2073349cc55cSDimitry Andric } 2074349cc55cSDimitry Andric 2075349cc55cSDimitry Andric // For reg units, place PHIs, and then place them for any aliasing registers. 2076349cc55cSDimitry Andric for (Register R : RegUnitsToPHIUp) { 2077349cc55cSDimitry Andric LocIdx L = MTracker->lookupOrTrackRegister(R); 2078349cc55cSDimitry Andric CollectPHIsForLoc(L); 2079349cc55cSDimitry Andric 2080349cc55cSDimitry Andric // Install those PHI values into the live-in value array. 2081349cc55cSDimitry Andric InstallPHIsAtLoc(L); 2082349cc55cSDimitry Andric 2083349cc55cSDimitry Andric // Now find aliases and install PHIs for those. 2084349cc55cSDimitry Andric for (MCRegAliasIterator RAI(R, TRI, true); RAI.isValid(); ++RAI) { 2085349cc55cSDimitry Andric // Super-registers that are "above" the largest register read/written by 2086349cc55cSDimitry Andric // the function will alias, but will not be tracked. 2087349cc55cSDimitry Andric if (!MTracker->isRegisterTracked(*RAI)) 2088349cc55cSDimitry Andric continue; 2089349cc55cSDimitry Andric 2090349cc55cSDimitry Andric LocIdx AliasLoc = MTracker->lookupOrTrackRegister(*RAI); 2091349cc55cSDimitry Andric InstallPHIsAtLoc(AliasLoc); 2092349cc55cSDimitry Andric } 2093349cc55cSDimitry Andric } 2094349cc55cSDimitry Andric } 2095349cc55cSDimitry Andric 2096349cc55cSDimitry Andric void InstrRefBasedLDV::buildMLocValueMap( 2097349cc55cSDimitry Andric MachineFunction &MF, ValueIDNum **MInLocs, ValueIDNum **MOutLocs, 2098e8d8bef9SDimitry Andric SmallVectorImpl<MLocTransferMap> &MLocTransfer) { 2099e8d8bef9SDimitry Andric std::priority_queue<unsigned int, std::vector<unsigned int>, 2100e8d8bef9SDimitry Andric std::greater<unsigned int>> 2101e8d8bef9SDimitry Andric Worklist, Pending; 2102e8d8bef9SDimitry Andric 2103e8d8bef9SDimitry Andric // We track what is on the current and pending worklist to avoid inserting 2104e8d8bef9SDimitry Andric // the same thing twice. We could avoid this with a custom priority queue, 2105e8d8bef9SDimitry Andric // but this is probably not worth it. 2106e8d8bef9SDimitry Andric SmallPtrSet<MachineBasicBlock *, 16> OnPending, OnWorklist; 2107e8d8bef9SDimitry Andric 2108349cc55cSDimitry Andric // Initialize worklist with every block to be visited. Also produce list of 2109349cc55cSDimitry Andric // all blocks. 2110349cc55cSDimitry Andric SmallPtrSet<MachineBasicBlock *, 32> AllBlocks; 2111e8d8bef9SDimitry Andric for (unsigned int I = 0; I < BBToOrder.size(); ++I) { 2112e8d8bef9SDimitry Andric Worklist.push(I); 2113e8d8bef9SDimitry Andric OnWorklist.insert(OrderToBB[I]); 2114349cc55cSDimitry Andric AllBlocks.insert(OrderToBB[I]); 2115e8d8bef9SDimitry Andric } 2116e8d8bef9SDimitry Andric 2117349cc55cSDimitry Andric // Initialize entry block to PHIs. These represent arguments. 2118349cc55cSDimitry Andric for (auto Location : MTracker->locations()) 2119349cc55cSDimitry Andric MInLocs[0][Location.Idx.asU64()] = ValueIDNum(0, 0, Location.Idx); 2120349cc55cSDimitry Andric 2121e8d8bef9SDimitry Andric MTracker->reset(); 2122e8d8bef9SDimitry Andric 2123349cc55cSDimitry Andric // Start by placing PHIs, using the usual SSA constructor algorithm. Consider 2124349cc55cSDimitry Andric // any machine-location that isn't live-through a block to be def'd in that 2125349cc55cSDimitry Andric // block. 2126349cc55cSDimitry Andric placeMLocPHIs(MF, AllBlocks, MInLocs, MLocTransfer); 2127e8d8bef9SDimitry Andric 2128349cc55cSDimitry Andric // Propagate values to eliminate redundant PHIs. At the same time, this 2129349cc55cSDimitry Andric // produces the table of Block x Location => Value for the entry to each 2130349cc55cSDimitry Andric // block. 2131349cc55cSDimitry Andric // The kind of PHIs we can eliminate are, for example, where one path in a 2132349cc55cSDimitry Andric // conditional spills and restores a register, and the register still has 2133349cc55cSDimitry Andric // the same value once control flow joins, unbeknowns to the PHI placement 2134349cc55cSDimitry Andric // code. Propagating values allows us to identify such un-necessary PHIs and 2135349cc55cSDimitry Andric // remove them. 2136e8d8bef9SDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> Visited; 2137e8d8bef9SDimitry Andric while (!Worklist.empty() || !Pending.empty()) { 2138e8d8bef9SDimitry Andric // Vector for storing the evaluated block transfer function. 2139e8d8bef9SDimitry Andric SmallVector<std::pair<LocIdx, ValueIDNum>, 32> ToRemap; 2140e8d8bef9SDimitry Andric 2141e8d8bef9SDimitry Andric while (!Worklist.empty()) { 2142e8d8bef9SDimitry Andric MachineBasicBlock *MBB = OrderToBB[Worklist.top()]; 2143e8d8bef9SDimitry Andric CurBB = MBB->getNumber(); 2144e8d8bef9SDimitry Andric Worklist.pop(); 2145e8d8bef9SDimitry Andric 2146e8d8bef9SDimitry Andric // Join the values in all predecessor blocks. 2147349cc55cSDimitry Andric bool InLocsChanged; 2148349cc55cSDimitry Andric InLocsChanged = mlocJoin(*MBB, Visited, MOutLocs, MInLocs[CurBB]); 2149e8d8bef9SDimitry Andric InLocsChanged |= Visited.insert(MBB).second; 2150e8d8bef9SDimitry Andric 2151e8d8bef9SDimitry Andric // Don't examine transfer function if we've visited this loc at least 2152e8d8bef9SDimitry Andric // once, and inlocs haven't changed. 2153e8d8bef9SDimitry Andric if (!InLocsChanged) 2154e8d8bef9SDimitry Andric continue; 2155e8d8bef9SDimitry Andric 2156e8d8bef9SDimitry Andric // Load the current set of live-ins into MLocTracker. 2157e8d8bef9SDimitry Andric MTracker->loadFromArray(MInLocs[CurBB], CurBB); 2158e8d8bef9SDimitry Andric 2159e8d8bef9SDimitry Andric // Each element of the transfer function can be a new def, or a read of 2160e8d8bef9SDimitry Andric // a live-in value. Evaluate each element, and store to "ToRemap". 2161e8d8bef9SDimitry Andric ToRemap.clear(); 2162e8d8bef9SDimitry Andric for (auto &P : MLocTransfer[CurBB]) { 2163e8d8bef9SDimitry Andric if (P.second.getBlock() == CurBB && P.second.isPHI()) { 2164e8d8bef9SDimitry Andric // This is a movement of whatever was live in. Read it. 2165349cc55cSDimitry Andric ValueIDNum NewID = MTracker->readMLoc(P.second.getLoc()); 2166e8d8bef9SDimitry Andric ToRemap.push_back(std::make_pair(P.first, NewID)); 2167e8d8bef9SDimitry Andric } else { 2168e8d8bef9SDimitry Andric // It's a def. Just set it. 2169e8d8bef9SDimitry Andric assert(P.second.getBlock() == CurBB); 2170e8d8bef9SDimitry Andric ToRemap.push_back(std::make_pair(P.first, P.second)); 2171e8d8bef9SDimitry Andric } 2172e8d8bef9SDimitry Andric } 2173e8d8bef9SDimitry Andric 2174e8d8bef9SDimitry Andric // Commit the transfer function changes into mloc tracker, which 2175e8d8bef9SDimitry Andric // transforms the contents of the MLocTracker into the live-outs. 2176e8d8bef9SDimitry Andric for (auto &P : ToRemap) 2177e8d8bef9SDimitry Andric MTracker->setMLoc(P.first, P.second); 2178e8d8bef9SDimitry Andric 2179e8d8bef9SDimitry Andric // Now copy out-locs from mloc tracker into out-loc vector, checking 2180e8d8bef9SDimitry Andric // whether changes have occurred. These changes can have come from both 2181e8d8bef9SDimitry Andric // the transfer function, and mlocJoin. 2182e8d8bef9SDimitry Andric bool OLChanged = false; 2183e8d8bef9SDimitry Andric for (auto Location : MTracker->locations()) { 2184e8d8bef9SDimitry Andric OLChanged |= MOutLocs[CurBB][Location.Idx.asU64()] != Location.Value; 2185e8d8bef9SDimitry Andric MOutLocs[CurBB][Location.Idx.asU64()] = Location.Value; 2186e8d8bef9SDimitry Andric } 2187e8d8bef9SDimitry Andric 2188e8d8bef9SDimitry Andric MTracker->reset(); 2189e8d8bef9SDimitry Andric 2190e8d8bef9SDimitry Andric // No need to examine successors again if out-locs didn't change. 2191e8d8bef9SDimitry Andric if (!OLChanged) 2192e8d8bef9SDimitry Andric continue; 2193e8d8bef9SDimitry Andric 2194e8d8bef9SDimitry Andric // All successors should be visited: put any back-edges on the pending 2195349cc55cSDimitry Andric // list for the next pass-through, and any other successors to be 2196349cc55cSDimitry Andric // visited this pass, if they're not going to be already. 2197e8d8bef9SDimitry Andric for (auto s : MBB->successors()) { 2198e8d8bef9SDimitry Andric // Does branching to this successor represent a back-edge? 2199e8d8bef9SDimitry Andric if (BBToOrder[s] > BBToOrder[MBB]) { 2200e8d8bef9SDimitry Andric // No: visit it during this dataflow iteration. 2201e8d8bef9SDimitry Andric if (OnWorklist.insert(s).second) 2202e8d8bef9SDimitry Andric Worklist.push(BBToOrder[s]); 2203e8d8bef9SDimitry Andric } else { 2204e8d8bef9SDimitry Andric // Yes: visit it on the next iteration. 2205e8d8bef9SDimitry Andric if (OnPending.insert(s).second) 2206e8d8bef9SDimitry Andric Pending.push(BBToOrder[s]); 2207e8d8bef9SDimitry Andric } 2208e8d8bef9SDimitry Andric } 2209e8d8bef9SDimitry Andric } 2210e8d8bef9SDimitry Andric 2211e8d8bef9SDimitry Andric Worklist.swap(Pending); 2212e8d8bef9SDimitry Andric std::swap(OnPending, OnWorklist); 2213e8d8bef9SDimitry Andric OnPending.clear(); 2214e8d8bef9SDimitry Andric // At this point, pending must be empty, since it was just the empty 2215e8d8bef9SDimitry Andric // worklist 2216e8d8bef9SDimitry Andric assert(Pending.empty() && "Pending should be empty"); 2217e8d8bef9SDimitry Andric } 2218e8d8bef9SDimitry Andric 2219349cc55cSDimitry Andric // Once all the live-ins don't change on mlocJoin(), we've eliminated all 2220349cc55cSDimitry Andric // redundant PHIs. 2221e8d8bef9SDimitry Andric } 2222e8d8bef9SDimitry Andric 2223349cc55cSDimitry Andric void InstrRefBasedLDV::BlockPHIPlacement( 2224349cc55cSDimitry Andric const SmallPtrSetImpl<MachineBasicBlock *> &AllBlocks, 2225349cc55cSDimitry Andric const SmallPtrSetImpl<MachineBasicBlock *> &DefBlocks, 2226349cc55cSDimitry Andric SmallVectorImpl<MachineBasicBlock *> &PHIBlocks) { 2227349cc55cSDimitry Andric // Apply IDF calculator to the designated set of location defs, storing 2228349cc55cSDimitry Andric // required PHIs into PHIBlocks. Uses the dominator tree stored in the 2229349cc55cSDimitry Andric // InstrRefBasedLDV object. 2230*1fd87a68SDimitry Andric IDFCalculatorBase<MachineBasicBlock, false> IDF(DomTree->getBase()); 2231349cc55cSDimitry Andric 2232349cc55cSDimitry Andric IDF.setLiveInBlocks(AllBlocks); 2233349cc55cSDimitry Andric IDF.setDefiningBlocks(DefBlocks); 2234349cc55cSDimitry Andric IDF.calculate(PHIBlocks); 2235e8d8bef9SDimitry Andric } 2236e8d8bef9SDimitry Andric 2237349cc55cSDimitry Andric Optional<ValueIDNum> InstrRefBasedLDV::pickVPHILoc( 2238349cc55cSDimitry Andric const MachineBasicBlock &MBB, const DebugVariable &Var, 2239349cc55cSDimitry Andric const LiveIdxT &LiveOuts, ValueIDNum **MOutLocs, 2240349cc55cSDimitry Andric const SmallVectorImpl<const MachineBasicBlock *> &BlockOrders) { 2241e8d8bef9SDimitry Andric // Collect a set of locations from predecessor where its live-out value can 2242e8d8bef9SDimitry Andric // be found. 2243e8d8bef9SDimitry Andric SmallVector<SmallVector<LocIdx, 4>, 8> Locs; 2244349cc55cSDimitry Andric SmallVector<const DbgValueProperties *, 4> Properties; 2245e8d8bef9SDimitry Andric unsigned NumLocs = MTracker->getNumLocs(); 2246349cc55cSDimitry Andric 2247349cc55cSDimitry Andric // No predecessors means no PHIs. 2248349cc55cSDimitry Andric if (BlockOrders.empty()) 2249349cc55cSDimitry Andric return None; 2250e8d8bef9SDimitry Andric 2251e8d8bef9SDimitry Andric for (auto p : BlockOrders) { 2252e8d8bef9SDimitry Andric unsigned ThisBBNum = p->getNumber(); 2253349cc55cSDimitry Andric auto OutValIt = LiveOuts.find(p); 2254349cc55cSDimitry Andric if (OutValIt == LiveOuts.end()) 2255349cc55cSDimitry Andric // If we have a predecessor not in scope, we'll never find a PHI position. 2256349cc55cSDimitry Andric return None; 2257349cc55cSDimitry Andric const DbgValue &OutVal = *OutValIt->second; 2258e8d8bef9SDimitry Andric 2259e8d8bef9SDimitry Andric if (OutVal.Kind == DbgValue::Const || OutVal.Kind == DbgValue::NoVal) 2260e8d8bef9SDimitry Andric // Consts and no-values cannot have locations we can join on. 2261349cc55cSDimitry Andric return None; 2262e8d8bef9SDimitry Andric 2263349cc55cSDimitry Andric Properties.push_back(&OutVal.Properties); 2264349cc55cSDimitry Andric 2265349cc55cSDimitry Andric // Create new empty vector of locations. 2266349cc55cSDimitry Andric Locs.resize(Locs.size() + 1); 2267349cc55cSDimitry Andric 2268349cc55cSDimitry Andric // If the live-in value is a def, find the locations where that value is 2269349cc55cSDimitry Andric // present. Do the same for VPHIs where we know the VPHI value. 2270349cc55cSDimitry Andric if (OutVal.Kind == DbgValue::Def || 2271349cc55cSDimitry Andric (OutVal.Kind == DbgValue::VPHI && OutVal.BlockNo != MBB.getNumber() && 2272349cc55cSDimitry Andric OutVal.ID != ValueIDNum::EmptyValue)) { 2273e8d8bef9SDimitry Andric ValueIDNum ValToLookFor = OutVal.ID; 2274e8d8bef9SDimitry Andric // Search the live-outs of the predecessor for the specified value. 2275e8d8bef9SDimitry Andric for (unsigned int I = 0; I < NumLocs; ++I) { 2276e8d8bef9SDimitry Andric if (MOutLocs[ThisBBNum][I] == ValToLookFor) 2277e8d8bef9SDimitry Andric Locs.back().push_back(LocIdx(I)); 2278e8d8bef9SDimitry Andric } 2279349cc55cSDimitry Andric } else { 2280349cc55cSDimitry Andric assert(OutVal.Kind == DbgValue::VPHI); 2281349cc55cSDimitry Andric // For VPHIs where we don't know the location, we definitely can't find 2282349cc55cSDimitry Andric // a join loc. 2283349cc55cSDimitry Andric if (OutVal.BlockNo != MBB.getNumber()) 2284349cc55cSDimitry Andric return None; 2285349cc55cSDimitry Andric 2286349cc55cSDimitry Andric // Otherwise: this is a VPHI on a backedge feeding back into itself, i.e. 2287349cc55cSDimitry Andric // a value that's live-through the whole loop. (It has to be a backedge, 2288349cc55cSDimitry Andric // because a block can't dominate itself). We can accept as a PHI location 2289349cc55cSDimitry Andric // any location where the other predecessors agree, _and_ the machine 2290349cc55cSDimitry Andric // locations feed back into themselves. Therefore, add all self-looping 2291349cc55cSDimitry Andric // machine-value PHI locations. 2292349cc55cSDimitry Andric for (unsigned int I = 0; I < NumLocs; ++I) { 2293349cc55cSDimitry Andric ValueIDNum MPHI(MBB.getNumber(), 0, LocIdx(I)); 2294349cc55cSDimitry Andric if (MOutLocs[ThisBBNum][I] == MPHI) 2295349cc55cSDimitry Andric Locs.back().push_back(LocIdx(I)); 2296349cc55cSDimitry Andric } 2297349cc55cSDimitry Andric } 2298e8d8bef9SDimitry Andric } 2299e8d8bef9SDimitry Andric 2300349cc55cSDimitry Andric // We should have found locations for all predecessors, or returned. 2301349cc55cSDimitry Andric assert(Locs.size() == BlockOrders.size()); 2302e8d8bef9SDimitry Andric 2303349cc55cSDimitry Andric // Check that all properties are the same. We can't pick a location if they're 2304349cc55cSDimitry Andric // not. 2305349cc55cSDimitry Andric const DbgValueProperties *Properties0 = Properties[0]; 2306349cc55cSDimitry Andric for (auto *Prop : Properties) 2307349cc55cSDimitry Andric if (*Prop != *Properties0) 2308349cc55cSDimitry Andric return None; 2309349cc55cSDimitry Andric 2310e8d8bef9SDimitry Andric // Starting with the first set of locations, take the intersection with 2311e8d8bef9SDimitry Andric // subsequent sets. 2312349cc55cSDimitry Andric SmallVector<LocIdx, 4> CandidateLocs = Locs[0]; 2313349cc55cSDimitry Andric for (unsigned int I = 1; I < Locs.size(); ++I) { 2314349cc55cSDimitry Andric auto &LocVec = Locs[I]; 2315349cc55cSDimitry Andric SmallVector<LocIdx, 4> NewCandidates; 2316349cc55cSDimitry Andric std::set_intersection(CandidateLocs.begin(), CandidateLocs.end(), 2317349cc55cSDimitry Andric LocVec.begin(), LocVec.end(), std::inserter(NewCandidates, NewCandidates.begin())); 2318349cc55cSDimitry Andric CandidateLocs = NewCandidates; 2319e8d8bef9SDimitry Andric } 2320349cc55cSDimitry Andric if (CandidateLocs.empty()) 2321e8d8bef9SDimitry Andric return None; 2322e8d8bef9SDimitry Andric 2323e8d8bef9SDimitry Andric // We now have a set of LocIdxes that contain the right output value in 2324e8d8bef9SDimitry Andric // each of the predecessors. Pick the lowest; if there's a register loc, 2325e8d8bef9SDimitry Andric // that'll be it. 2326349cc55cSDimitry Andric LocIdx L = *CandidateLocs.begin(); 2327e8d8bef9SDimitry Andric 2328e8d8bef9SDimitry Andric // Return a PHI-value-number for the found location. 2329e8d8bef9SDimitry Andric ValueIDNum PHIVal = {(unsigned)MBB.getNumber(), 0, L}; 2330349cc55cSDimitry Andric return PHIVal; 2331e8d8bef9SDimitry Andric } 2332e8d8bef9SDimitry Andric 2333349cc55cSDimitry Andric bool InstrRefBasedLDV::vlocJoin( 2334349cc55cSDimitry Andric MachineBasicBlock &MBB, LiveIdxT &VLOCOutLocs, 2335e8d8bef9SDimitry Andric SmallPtrSet<const MachineBasicBlock *, 8> &BlocksToExplore, 2336349cc55cSDimitry Andric DbgValue &LiveIn) { 2337e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "join MBB: " << MBB.getNumber() << "\n"); 2338e8d8bef9SDimitry Andric bool Changed = false; 2339e8d8bef9SDimitry Andric 2340e8d8bef9SDimitry Andric // Order predecessors by RPOT order, for exploring them in that order. 2341fe6060f1SDimitry Andric SmallVector<MachineBasicBlock *, 8> BlockOrders(MBB.predecessors()); 2342e8d8bef9SDimitry Andric 2343e8d8bef9SDimitry Andric auto Cmp = [&](MachineBasicBlock *A, MachineBasicBlock *B) { 2344e8d8bef9SDimitry Andric return BBToOrder[A] < BBToOrder[B]; 2345e8d8bef9SDimitry Andric }; 2346e8d8bef9SDimitry Andric 2347e8d8bef9SDimitry Andric llvm::sort(BlockOrders, Cmp); 2348e8d8bef9SDimitry Andric 2349e8d8bef9SDimitry Andric unsigned CurBlockRPONum = BBToOrder[&MBB]; 2350e8d8bef9SDimitry Andric 2351349cc55cSDimitry Andric // Collect all the incoming DbgValues for this variable, from predecessor 2352349cc55cSDimitry Andric // live-out values. 2353e8d8bef9SDimitry Andric SmallVector<InValueT, 8> Values; 2354e8d8bef9SDimitry Andric bool Bail = false; 2355349cc55cSDimitry Andric int BackEdgesStart = 0; 2356e8d8bef9SDimitry Andric for (auto p : BlockOrders) { 2357e8d8bef9SDimitry Andric // If the predecessor isn't in scope / to be explored, we'll never be 2358e8d8bef9SDimitry Andric // able to join any locations. 2359e8d8bef9SDimitry Andric if (!BlocksToExplore.contains(p)) { 2360e8d8bef9SDimitry Andric Bail = true; 2361e8d8bef9SDimitry Andric break; 2362e8d8bef9SDimitry Andric } 2363e8d8bef9SDimitry Andric 2364349cc55cSDimitry Andric // All Live-outs will have been initialized. 2365349cc55cSDimitry Andric DbgValue &OutLoc = *VLOCOutLocs.find(p)->second; 2366e8d8bef9SDimitry Andric 2367e8d8bef9SDimitry Andric // Keep track of where back-edges begin in the Values vector. Relies on 2368e8d8bef9SDimitry Andric // BlockOrders being sorted by RPO. 2369e8d8bef9SDimitry Andric unsigned ThisBBRPONum = BBToOrder[p]; 2370e8d8bef9SDimitry Andric if (ThisBBRPONum < CurBlockRPONum) 2371e8d8bef9SDimitry Andric ++BackEdgesStart; 2372e8d8bef9SDimitry Andric 2373349cc55cSDimitry Andric Values.push_back(std::make_pair(p, &OutLoc)); 2374e8d8bef9SDimitry Andric } 2375e8d8bef9SDimitry Andric 2376e8d8bef9SDimitry Andric // If there were no values, or one of the predecessors couldn't have a 2377e8d8bef9SDimitry Andric // value, then give up immediately. It's not safe to produce a live-in 2378349cc55cSDimitry Andric // value. Leave as whatever it was before. 2379e8d8bef9SDimitry Andric if (Bail || Values.size() == 0) 2380349cc55cSDimitry Andric return false; 2381e8d8bef9SDimitry Andric 2382e8d8bef9SDimitry Andric // All (non-entry) blocks have at least one non-backedge predecessor. 2383e8d8bef9SDimitry Andric // Pick the variable value from the first of these, to compare against 2384e8d8bef9SDimitry Andric // all others. 2385e8d8bef9SDimitry Andric const DbgValue &FirstVal = *Values[0].second; 2386e8d8bef9SDimitry Andric 2387349cc55cSDimitry Andric // If the old live-in value is not a PHI then either a) no PHI is needed 2388349cc55cSDimitry Andric // here, or b) we eliminated the PHI that was here. If so, we can just 2389349cc55cSDimitry Andric // propagate in the first parent's incoming value. 2390349cc55cSDimitry Andric if (LiveIn.Kind != DbgValue::VPHI || LiveIn.BlockNo != MBB.getNumber()) { 2391349cc55cSDimitry Andric Changed = LiveIn != FirstVal; 2392349cc55cSDimitry Andric if (Changed) 2393349cc55cSDimitry Andric LiveIn = FirstVal; 2394349cc55cSDimitry Andric return Changed; 2395349cc55cSDimitry Andric } 2396349cc55cSDimitry Andric 2397349cc55cSDimitry Andric // Scan for variable values that can never be resolved: if they have 2398349cc55cSDimitry Andric // different DIExpressions, different indirectness, or are mixed constants / 2399e8d8bef9SDimitry Andric // non-constants. 2400e8d8bef9SDimitry Andric for (auto &V : Values) { 2401e8d8bef9SDimitry Andric if (V.second->Properties != FirstVal.Properties) 2402349cc55cSDimitry Andric return false; 2403349cc55cSDimitry Andric if (V.second->Kind == DbgValue::NoVal) 2404349cc55cSDimitry Andric return false; 2405e8d8bef9SDimitry Andric if (V.second->Kind == DbgValue::Const && FirstVal.Kind != DbgValue::Const) 2406349cc55cSDimitry Andric return false; 2407e8d8bef9SDimitry Andric } 2408e8d8bef9SDimitry Andric 2409349cc55cSDimitry Andric // Try to eliminate this PHI. Do the incoming values all agree? 2410e8d8bef9SDimitry Andric bool Disagree = false; 2411e8d8bef9SDimitry Andric for (auto &V : Values) { 2412e8d8bef9SDimitry Andric if (*V.second == FirstVal) 2413e8d8bef9SDimitry Andric continue; // No disagreement. 2414e8d8bef9SDimitry Andric 2415349cc55cSDimitry Andric // Eliminate if a backedge feeds a VPHI back into itself. 2416349cc55cSDimitry Andric if (V.second->Kind == DbgValue::VPHI && 2417349cc55cSDimitry Andric V.second->BlockNo == MBB.getNumber() && 2418349cc55cSDimitry Andric // Is this a backedge? 2419349cc55cSDimitry Andric std::distance(Values.begin(), &V) >= BackEdgesStart) 2420349cc55cSDimitry Andric continue; 2421349cc55cSDimitry Andric 2422e8d8bef9SDimitry Andric Disagree = true; 2423e8d8bef9SDimitry Andric } 2424e8d8bef9SDimitry Andric 2425349cc55cSDimitry Andric // No disagreement -> live-through value. 2426349cc55cSDimitry Andric if (!Disagree) { 2427349cc55cSDimitry Andric Changed = LiveIn != FirstVal; 2428e8d8bef9SDimitry Andric if (Changed) 2429349cc55cSDimitry Andric LiveIn = FirstVal; 2430349cc55cSDimitry Andric return Changed; 2431349cc55cSDimitry Andric } else { 2432349cc55cSDimitry Andric // Otherwise use a VPHI. 2433349cc55cSDimitry Andric DbgValue VPHI(MBB.getNumber(), FirstVal.Properties, DbgValue::VPHI); 2434349cc55cSDimitry Andric Changed = LiveIn != VPHI; 2435349cc55cSDimitry Andric if (Changed) 2436349cc55cSDimitry Andric LiveIn = VPHI; 2437349cc55cSDimitry Andric return Changed; 2438349cc55cSDimitry Andric } 2439e8d8bef9SDimitry Andric } 2440e8d8bef9SDimitry Andric 2441*1fd87a68SDimitry Andric void InstrRefBasedLDV::getBlocksForScope( 2442*1fd87a68SDimitry Andric const DILocation *DILoc, 2443*1fd87a68SDimitry Andric SmallPtrSetImpl<const MachineBasicBlock *> &BlocksToExplore, 2444*1fd87a68SDimitry Andric const SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks) { 2445*1fd87a68SDimitry Andric // Get the set of "normal" in-lexical-scope blocks. 2446*1fd87a68SDimitry Andric LS.getMachineBasicBlocks(DILoc, BlocksToExplore); 2447*1fd87a68SDimitry Andric 2448*1fd87a68SDimitry Andric // VarLoc LiveDebugValues tracks variable locations that are defined in 2449*1fd87a68SDimitry Andric // blocks not in scope. This is something we could legitimately ignore, but 2450*1fd87a68SDimitry Andric // lets allow it for now for the sake of coverage. 2451*1fd87a68SDimitry Andric BlocksToExplore.insert(AssignBlocks.begin(), AssignBlocks.end()); 2452*1fd87a68SDimitry Andric 2453*1fd87a68SDimitry Andric // Storage for artificial blocks we intend to add to BlocksToExplore. 2454*1fd87a68SDimitry Andric DenseSet<const MachineBasicBlock *> ToAdd; 2455*1fd87a68SDimitry Andric 2456*1fd87a68SDimitry Andric // To avoid needlessly dropping large volumes of variable locations, propagate 2457*1fd87a68SDimitry Andric // variables through aritifical blocks, i.e. those that don't have any 2458*1fd87a68SDimitry Andric // instructions in scope at all. To accurately replicate VarLoc 2459*1fd87a68SDimitry Andric // LiveDebugValues, this means exploring all artificial successors too. 2460*1fd87a68SDimitry Andric // Perform a depth-first-search to enumerate those blocks. 2461*1fd87a68SDimitry Andric for (auto *MBB : BlocksToExplore) { 2462*1fd87a68SDimitry Andric // Depth-first-search state: each node is a block and which successor 2463*1fd87a68SDimitry Andric // we're currently exploring. 2464*1fd87a68SDimitry Andric SmallVector<std::pair<const MachineBasicBlock *, 2465*1fd87a68SDimitry Andric MachineBasicBlock::const_succ_iterator>, 2466*1fd87a68SDimitry Andric 8> 2467*1fd87a68SDimitry Andric DFS; 2468*1fd87a68SDimitry Andric 2469*1fd87a68SDimitry Andric // Find any artificial successors not already tracked. 2470*1fd87a68SDimitry Andric for (auto *succ : MBB->successors()) { 2471*1fd87a68SDimitry Andric if (BlocksToExplore.count(succ)) 2472*1fd87a68SDimitry Andric continue; 2473*1fd87a68SDimitry Andric if (!ArtificialBlocks.count(succ)) 2474*1fd87a68SDimitry Andric continue; 2475*1fd87a68SDimitry Andric ToAdd.insert(succ); 2476*1fd87a68SDimitry Andric DFS.push_back({succ, succ->succ_begin()}); 2477*1fd87a68SDimitry Andric } 2478*1fd87a68SDimitry Andric 2479*1fd87a68SDimitry Andric // Search all those blocks, depth first. 2480*1fd87a68SDimitry Andric while (!DFS.empty()) { 2481*1fd87a68SDimitry Andric const MachineBasicBlock *CurBB = DFS.back().first; 2482*1fd87a68SDimitry Andric MachineBasicBlock::const_succ_iterator &CurSucc = DFS.back().second; 2483*1fd87a68SDimitry Andric // Walk back if we've explored this blocks successors to the end. 2484*1fd87a68SDimitry Andric if (CurSucc == CurBB->succ_end()) { 2485*1fd87a68SDimitry Andric DFS.pop_back(); 2486*1fd87a68SDimitry Andric continue; 2487*1fd87a68SDimitry Andric } 2488*1fd87a68SDimitry Andric 2489*1fd87a68SDimitry Andric // If the current successor is artificial and unexplored, descend into 2490*1fd87a68SDimitry Andric // it. 2491*1fd87a68SDimitry Andric if (!ToAdd.count(*CurSucc) && ArtificialBlocks.count(*CurSucc)) { 2492*1fd87a68SDimitry Andric ToAdd.insert(*CurSucc); 2493*1fd87a68SDimitry Andric DFS.push_back({*CurSucc, (*CurSucc)->succ_begin()}); 2494*1fd87a68SDimitry Andric continue; 2495*1fd87a68SDimitry Andric } 2496*1fd87a68SDimitry Andric 2497*1fd87a68SDimitry Andric ++CurSucc; 2498*1fd87a68SDimitry Andric } 2499*1fd87a68SDimitry Andric }; 2500*1fd87a68SDimitry Andric 2501*1fd87a68SDimitry Andric BlocksToExplore.insert(ToAdd.begin(), ToAdd.end()); 2502*1fd87a68SDimitry Andric } 2503*1fd87a68SDimitry Andric 2504*1fd87a68SDimitry Andric void InstrRefBasedLDV::buildVLocValueMap( 2505*1fd87a68SDimitry Andric const DILocation *DILoc, const SmallSet<DebugVariable, 4> &VarsWeCareAbout, 2506e8d8bef9SDimitry Andric SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks, LiveInsT &Output, 2507e8d8bef9SDimitry Andric ValueIDNum **MOutLocs, ValueIDNum **MInLocs, 2508e8d8bef9SDimitry Andric SmallVectorImpl<VLocTracker> &AllTheVLocs) { 2509349cc55cSDimitry Andric // This method is much like buildMLocValueMap: but focuses on a single 2510e8d8bef9SDimitry Andric // LexicalScope at a time. Pick out a set of blocks and variables that are 2511e8d8bef9SDimitry Andric // to have their value assignments solved, then run our dataflow algorithm 2512e8d8bef9SDimitry Andric // until a fixedpoint is reached. 2513e8d8bef9SDimitry Andric std::priority_queue<unsigned int, std::vector<unsigned int>, 2514e8d8bef9SDimitry Andric std::greater<unsigned int>> 2515e8d8bef9SDimitry Andric Worklist, Pending; 2516e8d8bef9SDimitry Andric SmallPtrSet<MachineBasicBlock *, 16> OnWorklist, OnPending; 2517e8d8bef9SDimitry Andric 2518e8d8bef9SDimitry Andric // The set of blocks we'll be examining. 2519e8d8bef9SDimitry Andric SmallPtrSet<const MachineBasicBlock *, 8> BlocksToExplore; 2520e8d8bef9SDimitry Andric 2521e8d8bef9SDimitry Andric // The order in which to examine them (RPO). 2522e8d8bef9SDimitry Andric SmallVector<MachineBasicBlock *, 8> BlockOrders; 2523e8d8bef9SDimitry Andric 2524e8d8bef9SDimitry Andric // RPO ordering function. 2525e8d8bef9SDimitry Andric auto Cmp = [&](MachineBasicBlock *A, MachineBasicBlock *B) { 2526e8d8bef9SDimitry Andric return BBToOrder[A] < BBToOrder[B]; 2527e8d8bef9SDimitry Andric }; 2528e8d8bef9SDimitry Andric 2529*1fd87a68SDimitry Andric getBlocksForScope(DILoc, BlocksToExplore, AssignBlocks); 2530e8d8bef9SDimitry Andric 2531e8d8bef9SDimitry Andric // Single block scope: not interesting! No propagation at all. Note that 2532e8d8bef9SDimitry Andric // this could probably go above ArtificialBlocks without damage, but 2533e8d8bef9SDimitry Andric // that then produces output differences from original-live-debug-values, 2534e8d8bef9SDimitry Andric // which propagates from a single block into many artificial ones. 2535e8d8bef9SDimitry Andric if (BlocksToExplore.size() == 1) 2536e8d8bef9SDimitry Andric return; 2537e8d8bef9SDimitry Andric 2538349cc55cSDimitry Andric // Convert a const set to a non-const set. LexicalScopes 2539349cc55cSDimitry Andric // getMachineBasicBlocks returns const MBB pointers, IDF wants mutable ones. 2540349cc55cSDimitry Andric // (Neither of them mutate anything). 2541349cc55cSDimitry Andric SmallPtrSet<MachineBasicBlock *, 8> MutBlocksToExplore; 2542349cc55cSDimitry Andric for (const auto *MBB : BlocksToExplore) 2543349cc55cSDimitry Andric MutBlocksToExplore.insert(const_cast<MachineBasicBlock *>(MBB)); 2544349cc55cSDimitry Andric 2545e8d8bef9SDimitry Andric // Picks out relevants blocks RPO order and sort them. 2546e8d8bef9SDimitry Andric for (auto *MBB : BlocksToExplore) 2547e8d8bef9SDimitry Andric BlockOrders.push_back(const_cast<MachineBasicBlock *>(MBB)); 2548e8d8bef9SDimitry Andric 2549e8d8bef9SDimitry Andric llvm::sort(BlockOrders, Cmp); 2550e8d8bef9SDimitry Andric unsigned NumBlocks = BlockOrders.size(); 2551e8d8bef9SDimitry Andric 2552e8d8bef9SDimitry Andric // Allocate some vectors for storing the live ins and live outs. Large. 2553349cc55cSDimitry Andric SmallVector<DbgValue, 32> LiveIns, LiveOuts; 2554349cc55cSDimitry Andric LiveIns.reserve(NumBlocks); 2555349cc55cSDimitry Andric LiveOuts.reserve(NumBlocks); 2556349cc55cSDimitry Andric 2557349cc55cSDimitry Andric // Initialize all values to start as NoVals. This signifies "it's live 2558349cc55cSDimitry Andric // through, but we don't know what it is". 2559349cc55cSDimitry Andric DbgValueProperties EmptyProperties(EmptyExpr, false); 2560349cc55cSDimitry Andric for (unsigned int I = 0; I < NumBlocks; ++I) { 2561349cc55cSDimitry Andric DbgValue EmptyDbgValue(I, EmptyProperties, DbgValue::NoVal); 2562349cc55cSDimitry Andric LiveIns.push_back(EmptyDbgValue); 2563349cc55cSDimitry Andric LiveOuts.push_back(EmptyDbgValue); 2564349cc55cSDimitry Andric } 2565e8d8bef9SDimitry Andric 2566e8d8bef9SDimitry Andric // Produce by-MBB indexes of live-in/live-outs, to ease lookup within 2567e8d8bef9SDimitry Andric // vlocJoin. 2568e8d8bef9SDimitry Andric LiveIdxT LiveOutIdx, LiveInIdx; 2569e8d8bef9SDimitry Andric LiveOutIdx.reserve(NumBlocks); 2570e8d8bef9SDimitry Andric LiveInIdx.reserve(NumBlocks); 2571e8d8bef9SDimitry Andric for (unsigned I = 0; I < NumBlocks; ++I) { 2572e8d8bef9SDimitry Andric LiveOutIdx[BlockOrders[I]] = &LiveOuts[I]; 2573e8d8bef9SDimitry Andric LiveInIdx[BlockOrders[I]] = &LiveIns[I]; 2574e8d8bef9SDimitry Andric } 2575e8d8bef9SDimitry Andric 2576349cc55cSDimitry Andric // Loop over each variable and place PHIs for it, then propagate values 2577349cc55cSDimitry Andric // between blocks. This keeps the locality of working on one lexical scope at 2578349cc55cSDimitry Andric // at time, but avoids re-processing variable values because some other 2579349cc55cSDimitry Andric // variable has been assigned. 2580349cc55cSDimitry Andric for (auto &Var : VarsWeCareAbout) { 2581349cc55cSDimitry Andric // Re-initialize live-ins and live-outs, to clear the remains of previous 2582349cc55cSDimitry Andric // variables live-ins / live-outs. 2583349cc55cSDimitry Andric for (unsigned int I = 0; I < NumBlocks; ++I) { 2584349cc55cSDimitry Andric DbgValue EmptyDbgValue(I, EmptyProperties, DbgValue::NoVal); 2585349cc55cSDimitry Andric LiveIns[I] = EmptyDbgValue; 2586349cc55cSDimitry Andric LiveOuts[I] = EmptyDbgValue; 2587349cc55cSDimitry Andric } 2588349cc55cSDimitry Andric 2589349cc55cSDimitry Andric // Place PHIs for variable values, using the LLVM IDF calculator. 2590349cc55cSDimitry Andric // Collect the set of blocks where variables are def'd. 2591349cc55cSDimitry Andric SmallPtrSet<MachineBasicBlock *, 32> DefBlocks; 2592349cc55cSDimitry Andric for (const MachineBasicBlock *ExpMBB : BlocksToExplore) { 2593349cc55cSDimitry Andric auto &TransferFunc = AllTheVLocs[ExpMBB->getNumber()].Vars; 2594349cc55cSDimitry Andric if (TransferFunc.find(Var) != TransferFunc.end()) 2595349cc55cSDimitry Andric DefBlocks.insert(const_cast<MachineBasicBlock *>(ExpMBB)); 2596349cc55cSDimitry Andric } 2597349cc55cSDimitry Andric 2598349cc55cSDimitry Andric SmallVector<MachineBasicBlock *, 32> PHIBlocks; 2599349cc55cSDimitry Andric 2600*1fd87a68SDimitry Andric // Request the set of PHIs we should insert for this variable. If there's 2601*1fd87a68SDimitry Andric // only one value definition, things are very simple. 2602*1fd87a68SDimitry Andric if (DefBlocks.size() == 1) { 2603*1fd87a68SDimitry Andric placePHIsForSingleVarDefinition(MutBlocksToExplore, *DefBlocks.begin(), 2604*1fd87a68SDimitry Andric AllTheVLocs, Var, Output); 2605*1fd87a68SDimitry Andric continue; 2606*1fd87a68SDimitry Andric } 2607*1fd87a68SDimitry Andric 2608*1fd87a68SDimitry Andric // Otherwise: we need to place PHIs through SSA and propagate values. 2609349cc55cSDimitry Andric BlockPHIPlacement(MutBlocksToExplore, DefBlocks, PHIBlocks); 2610349cc55cSDimitry Andric 2611349cc55cSDimitry Andric // Insert PHIs into the per-block live-in tables for this variable. 2612349cc55cSDimitry Andric for (MachineBasicBlock *PHIMBB : PHIBlocks) { 2613349cc55cSDimitry Andric unsigned BlockNo = PHIMBB->getNumber(); 2614349cc55cSDimitry Andric DbgValue *LiveIn = LiveInIdx[PHIMBB]; 2615349cc55cSDimitry Andric *LiveIn = DbgValue(BlockNo, EmptyProperties, DbgValue::VPHI); 2616349cc55cSDimitry Andric } 2617349cc55cSDimitry Andric 2618e8d8bef9SDimitry Andric for (auto *MBB : BlockOrders) { 2619e8d8bef9SDimitry Andric Worklist.push(BBToOrder[MBB]); 2620e8d8bef9SDimitry Andric OnWorklist.insert(MBB); 2621e8d8bef9SDimitry Andric } 2622e8d8bef9SDimitry Andric 2623349cc55cSDimitry Andric // Iterate over all the blocks we selected, propagating the variables value. 2624349cc55cSDimitry Andric // This loop does two things: 2625349cc55cSDimitry Andric // * Eliminates un-necessary VPHIs in vlocJoin, 2626349cc55cSDimitry Andric // * Evaluates the blocks transfer function (i.e. variable assignments) and 2627349cc55cSDimitry Andric // stores the result to the blocks live-outs. 2628349cc55cSDimitry Andric // Always evaluate the transfer function on the first iteration, and when 2629349cc55cSDimitry Andric // the live-ins change thereafter. 2630e8d8bef9SDimitry Andric bool FirstTrip = true; 2631e8d8bef9SDimitry Andric while (!Worklist.empty() || !Pending.empty()) { 2632e8d8bef9SDimitry Andric while (!Worklist.empty()) { 2633e8d8bef9SDimitry Andric auto *MBB = OrderToBB[Worklist.top()]; 2634e8d8bef9SDimitry Andric CurBB = MBB->getNumber(); 2635e8d8bef9SDimitry Andric Worklist.pop(); 2636e8d8bef9SDimitry Andric 2637349cc55cSDimitry Andric auto LiveInsIt = LiveInIdx.find(MBB); 2638349cc55cSDimitry Andric assert(LiveInsIt != LiveInIdx.end()); 2639349cc55cSDimitry Andric DbgValue *LiveIn = LiveInsIt->second; 2640e8d8bef9SDimitry Andric 2641e8d8bef9SDimitry Andric // Join values from predecessors. Updates LiveInIdx, and writes output 2642e8d8bef9SDimitry Andric // into JoinedInLocs. 2643349cc55cSDimitry Andric bool InLocsChanged = 26444824e7fdSDimitry Andric vlocJoin(*MBB, LiveOutIdx, BlocksToExplore, *LiveIn); 2645e8d8bef9SDimitry Andric 2646349cc55cSDimitry Andric SmallVector<const MachineBasicBlock *, 8> Preds; 2647349cc55cSDimitry Andric for (const auto *Pred : MBB->predecessors()) 2648349cc55cSDimitry Andric Preds.push_back(Pred); 2649e8d8bef9SDimitry Andric 2650349cc55cSDimitry Andric // If this block's live-in value is a VPHI, try to pick a machine-value 2651349cc55cSDimitry Andric // for it. This makes the machine-value available and propagated 2652349cc55cSDimitry Andric // through all blocks by the time value propagation finishes. We can't 2653349cc55cSDimitry Andric // do this any earlier as it needs to read the block live-outs. 2654349cc55cSDimitry Andric if (LiveIn->Kind == DbgValue::VPHI && LiveIn->BlockNo == (int)CurBB) { 2655349cc55cSDimitry Andric // There's a small possibility that on a preceeding path, a VPHI is 2656349cc55cSDimitry Andric // eliminated and transitions from VPHI-with-location to 2657349cc55cSDimitry Andric // live-through-value. As a result, the selected location of any VPHI 2658349cc55cSDimitry Andric // might change, so we need to re-compute it on each iteration. 2659349cc55cSDimitry Andric Optional<ValueIDNum> ValueNum = 2660349cc55cSDimitry Andric pickVPHILoc(*MBB, Var, LiveOutIdx, MOutLocs, Preds); 2661e8d8bef9SDimitry Andric 2662349cc55cSDimitry Andric if (ValueNum) { 2663349cc55cSDimitry Andric InLocsChanged |= LiveIn->ID != *ValueNum; 2664349cc55cSDimitry Andric LiveIn->ID = *ValueNum; 2665349cc55cSDimitry Andric } 2666349cc55cSDimitry Andric } 2667e8d8bef9SDimitry Andric 2668349cc55cSDimitry Andric if (!InLocsChanged && !FirstTrip) 2669e8d8bef9SDimitry Andric continue; 2670e8d8bef9SDimitry Andric 2671349cc55cSDimitry Andric DbgValue *LiveOut = LiveOutIdx[MBB]; 2672349cc55cSDimitry Andric bool OLChanged = false; 2673349cc55cSDimitry Andric 2674e8d8bef9SDimitry Andric // Do transfer function. 2675e8d8bef9SDimitry Andric auto &VTracker = AllTheVLocs[MBB->getNumber()]; 2676349cc55cSDimitry Andric auto TransferIt = VTracker.Vars.find(Var); 2677349cc55cSDimitry Andric if (TransferIt != VTracker.Vars.end()) { 2678e8d8bef9SDimitry Andric // Erase on empty transfer (DBG_VALUE $noreg). 2679349cc55cSDimitry Andric if (TransferIt->second.Kind == DbgValue::Undef) { 2680349cc55cSDimitry Andric DbgValue NewVal(MBB->getNumber(), EmptyProperties, DbgValue::NoVal); 2681349cc55cSDimitry Andric if (*LiveOut != NewVal) { 2682349cc55cSDimitry Andric *LiveOut = NewVal; 2683349cc55cSDimitry Andric OLChanged = true; 2684349cc55cSDimitry Andric } 2685e8d8bef9SDimitry Andric } else { 2686e8d8bef9SDimitry Andric // Insert new variable value; or overwrite. 2687349cc55cSDimitry Andric if (*LiveOut != TransferIt->second) { 2688349cc55cSDimitry Andric *LiveOut = TransferIt->second; 2689349cc55cSDimitry Andric OLChanged = true; 2690e8d8bef9SDimitry Andric } 2691e8d8bef9SDimitry Andric } 2692349cc55cSDimitry Andric } else { 2693349cc55cSDimitry Andric // Just copy live-ins to live-outs, for anything not transferred. 2694349cc55cSDimitry Andric if (*LiveOut != *LiveIn) { 2695349cc55cSDimitry Andric *LiveOut = *LiveIn; 2696349cc55cSDimitry Andric OLChanged = true; 2697349cc55cSDimitry Andric } 2698e8d8bef9SDimitry Andric } 2699e8d8bef9SDimitry Andric 2700349cc55cSDimitry Andric // If no live-out value changed, there's no need to explore further. 2701e8d8bef9SDimitry Andric if (!OLChanged) 2702e8d8bef9SDimitry Andric continue; 2703e8d8bef9SDimitry Andric 2704e8d8bef9SDimitry Andric // We should visit all successors. Ensure we'll visit any non-backedge 2705e8d8bef9SDimitry Andric // successors during this dataflow iteration; book backedge successors 2706e8d8bef9SDimitry Andric // to be visited next time around. 2707e8d8bef9SDimitry Andric for (auto s : MBB->successors()) { 2708e8d8bef9SDimitry Andric // Ignore out of scope / not-to-be-explored successors. 2709e8d8bef9SDimitry Andric if (LiveInIdx.find(s) == LiveInIdx.end()) 2710e8d8bef9SDimitry Andric continue; 2711e8d8bef9SDimitry Andric 2712e8d8bef9SDimitry Andric if (BBToOrder[s] > BBToOrder[MBB]) { 2713e8d8bef9SDimitry Andric if (OnWorklist.insert(s).second) 2714e8d8bef9SDimitry Andric Worklist.push(BBToOrder[s]); 2715e8d8bef9SDimitry Andric } else if (OnPending.insert(s).second && (FirstTrip || OLChanged)) { 2716e8d8bef9SDimitry Andric Pending.push(BBToOrder[s]); 2717e8d8bef9SDimitry Andric } 2718e8d8bef9SDimitry Andric } 2719e8d8bef9SDimitry Andric } 2720e8d8bef9SDimitry Andric Worklist.swap(Pending); 2721e8d8bef9SDimitry Andric std::swap(OnWorklist, OnPending); 2722e8d8bef9SDimitry Andric OnPending.clear(); 2723e8d8bef9SDimitry Andric assert(Pending.empty()); 2724e8d8bef9SDimitry Andric FirstTrip = false; 2725e8d8bef9SDimitry Andric } 2726e8d8bef9SDimitry Andric 2727349cc55cSDimitry Andric // Save live-ins to output vector. Ignore any that are still marked as being 2728349cc55cSDimitry Andric // VPHIs with no location -- those are variables that we know the value of, 2729349cc55cSDimitry Andric // but are not actually available in the register file. 2730e8d8bef9SDimitry Andric for (auto *MBB : BlockOrders) { 2731349cc55cSDimitry Andric DbgValue *BlockLiveIn = LiveInIdx[MBB]; 2732349cc55cSDimitry Andric if (BlockLiveIn->Kind == DbgValue::NoVal) 2733e8d8bef9SDimitry Andric continue; 2734349cc55cSDimitry Andric if (BlockLiveIn->Kind == DbgValue::VPHI && 2735349cc55cSDimitry Andric BlockLiveIn->ID == ValueIDNum::EmptyValue) 2736349cc55cSDimitry Andric continue; 2737349cc55cSDimitry Andric if (BlockLiveIn->Kind == DbgValue::VPHI) 2738349cc55cSDimitry Andric BlockLiveIn->Kind = DbgValue::Def; 27394824e7fdSDimitry Andric assert(BlockLiveIn->Properties.DIExpr->getFragmentInfo() == 27404824e7fdSDimitry Andric Var.getFragment() && "Fragment info missing during value prop"); 2741349cc55cSDimitry Andric Output[MBB->getNumber()].push_back(std::make_pair(Var, *BlockLiveIn)); 2742e8d8bef9SDimitry Andric } 2743349cc55cSDimitry Andric } // Per-variable loop. 2744e8d8bef9SDimitry Andric 2745e8d8bef9SDimitry Andric BlockOrders.clear(); 2746e8d8bef9SDimitry Andric BlocksToExplore.clear(); 2747e8d8bef9SDimitry Andric } 2748e8d8bef9SDimitry Andric 2749*1fd87a68SDimitry Andric void InstrRefBasedLDV::placePHIsForSingleVarDefinition( 2750*1fd87a68SDimitry Andric const SmallPtrSetImpl<MachineBasicBlock *> &InScopeBlocks, 2751*1fd87a68SDimitry Andric MachineBasicBlock *AssignMBB, SmallVectorImpl<VLocTracker> &AllTheVLocs, 2752*1fd87a68SDimitry Andric const DebugVariable &Var, LiveInsT &Output) { 2753*1fd87a68SDimitry Andric // If there is a single definition of the variable, then working out it's 2754*1fd87a68SDimitry Andric // value everywhere is very simple: it's every block dominated by the 2755*1fd87a68SDimitry Andric // definition. At the dominance frontier, the usual algorithm would: 2756*1fd87a68SDimitry Andric // * Place PHIs, 2757*1fd87a68SDimitry Andric // * Propagate values into them, 2758*1fd87a68SDimitry Andric // * Find there's no incoming variable value from the other incoming branches 2759*1fd87a68SDimitry Andric // of the dominance frontier, 2760*1fd87a68SDimitry Andric // * Specify there's no variable value in blocks past the frontier. 2761*1fd87a68SDimitry Andric // This is a common case, hence it's worth special-casing it. 2762*1fd87a68SDimitry Andric 2763*1fd87a68SDimitry Andric // Pick out the variables value from the block transfer function. 2764*1fd87a68SDimitry Andric VLocTracker &VLocs = AllTheVLocs[AssignMBB->getNumber()]; 2765*1fd87a68SDimitry Andric auto ValueIt = VLocs.Vars.find(Var); 2766*1fd87a68SDimitry Andric const DbgValue &Value = ValueIt->second; 2767*1fd87a68SDimitry Andric 2768*1fd87a68SDimitry Andric // Assign the variable value to entry to each dominated block that's in scope. 2769*1fd87a68SDimitry Andric // Skip the definition block -- it's assigned the variable value in the middle 2770*1fd87a68SDimitry Andric // of the block somewhere. 2771*1fd87a68SDimitry Andric for (auto *ScopeBlock : InScopeBlocks) { 2772*1fd87a68SDimitry Andric if (!DomTree->properlyDominates(AssignMBB, ScopeBlock)) 2773*1fd87a68SDimitry Andric continue; 2774*1fd87a68SDimitry Andric 2775*1fd87a68SDimitry Andric Output[ScopeBlock->getNumber()].push_back({Var, Value}); 2776*1fd87a68SDimitry Andric } 2777*1fd87a68SDimitry Andric 2778*1fd87a68SDimitry Andric // All blocks that aren't dominated have no live-in value, thus no variable 2779*1fd87a68SDimitry Andric // value will be given to them. 2780*1fd87a68SDimitry Andric } 2781*1fd87a68SDimitry Andric 2782e8d8bef9SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 2783e8d8bef9SDimitry Andric void InstrRefBasedLDV::dump_mloc_transfer( 2784e8d8bef9SDimitry Andric const MLocTransferMap &mloc_transfer) const { 2785e8d8bef9SDimitry Andric for (auto &P : mloc_transfer) { 2786e8d8bef9SDimitry Andric std::string foo = MTracker->LocIdxToName(P.first); 2787e8d8bef9SDimitry Andric std::string bar = MTracker->IDAsString(P.second); 2788e8d8bef9SDimitry Andric dbgs() << "Loc " << foo << " --> " << bar << "\n"; 2789e8d8bef9SDimitry Andric } 2790e8d8bef9SDimitry Andric } 2791e8d8bef9SDimitry Andric #endif 2792e8d8bef9SDimitry Andric 2793e8d8bef9SDimitry Andric void InstrRefBasedLDV::emitLocations( 2794fe6060f1SDimitry Andric MachineFunction &MF, LiveInsT SavedLiveIns, ValueIDNum **MOutLocs, 2795fe6060f1SDimitry Andric ValueIDNum **MInLocs, DenseMap<DebugVariable, unsigned> &AllVarsNumbering, 2796fe6060f1SDimitry Andric const TargetPassConfig &TPC) { 2797fe6060f1SDimitry Andric TTracker = new TransferTracker(TII, MTracker, MF, *TRI, CalleeSavedRegs, TPC); 2798e8d8bef9SDimitry Andric unsigned NumLocs = MTracker->getNumLocs(); 2799e8d8bef9SDimitry Andric 2800e8d8bef9SDimitry Andric // For each block, load in the machine value locations and variable value 2801e8d8bef9SDimitry Andric // live-ins, then step through each instruction in the block. New DBG_VALUEs 2802e8d8bef9SDimitry Andric // to be inserted will be created along the way. 2803e8d8bef9SDimitry Andric for (MachineBasicBlock &MBB : MF) { 2804e8d8bef9SDimitry Andric unsigned bbnum = MBB.getNumber(); 2805e8d8bef9SDimitry Andric MTracker->reset(); 2806e8d8bef9SDimitry Andric MTracker->loadFromArray(MInLocs[bbnum], bbnum); 2807e8d8bef9SDimitry Andric TTracker->loadInlocs(MBB, MInLocs[bbnum], SavedLiveIns[MBB.getNumber()], 2808e8d8bef9SDimitry Andric NumLocs); 2809e8d8bef9SDimitry Andric 2810e8d8bef9SDimitry Andric CurBB = bbnum; 2811e8d8bef9SDimitry Andric CurInst = 1; 2812e8d8bef9SDimitry Andric for (auto &MI : MBB) { 2813fe6060f1SDimitry Andric process(MI, MOutLocs, MInLocs); 2814e8d8bef9SDimitry Andric TTracker->checkInstForNewValues(CurInst, MI.getIterator()); 2815e8d8bef9SDimitry Andric ++CurInst; 2816e8d8bef9SDimitry Andric } 2817e8d8bef9SDimitry Andric } 2818e8d8bef9SDimitry Andric 2819*1fd87a68SDimitry Andric emitTransfers(AllVarsNumbering); 2820e8d8bef9SDimitry Andric } 2821e8d8bef9SDimitry Andric 2822e8d8bef9SDimitry Andric void InstrRefBasedLDV::initialSetup(MachineFunction &MF) { 2823e8d8bef9SDimitry Andric // Build some useful data structures. 2824349cc55cSDimitry Andric 2825349cc55cSDimitry Andric LLVMContext &Context = MF.getFunction().getContext(); 2826349cc55cSDimitry Andric EmptyExpr = DIExpression::get(Context, {}); 2827349cc55cSDimitry Andric 2828e8d8bef9SDimitry Andric auto hasNonArtificialLocation = [](const MachineInstr &MI) -> bool { 2829e8d8bef9SDimitry Andric if (const DebugLoc &DL = MI.getDebugLoc()) 2830e8d8bef9SDimitry Andric return DL.getLine() != 0; 2831e8d8bef9SDimitry Andric return false; 2832e8d8bef9SDimitry Andric }; 2833e8d8bef9SDimitry Andric // Collect a set of all the artificial blocks. 2834e8d8bef9SDimitry Andric for (auto &MBB : MF) 2835e8d8bef9SDimitry Andric if (none_of(MBB.instrs(), hasNonArtificialLocation)) 2836e8d8bef9SDimitry Andric ArtificialBlocks.insert(&MBB); 2837e8d8bef9SDimitry Andric 2838e8d8bef9SDimitry Andric // Compute mappings of block <=> RPO order. 2839e8d8bef9SDimitry Andric ReversePostOrderTraversal<MachineFunction *> RPOT(&MF); 2840e8d8bef9SDimitry Andric unsigned int RPONumber = 0; 2841fe6060f1SDimitry Andric for (MachineBasicBlock *MBB : RPOT) { 2842fe6060f1SDimitry Andric OrderToBB[RPONumber] = MBB; 2843fe6060f1SDimitry Andric BBToOrder[MBB] = RPONumber; 2844fe6060f1SDimitry Andric BBNumToRPO[MBB->getNumber()] = RPONumber; 2845e8d8bef9SDimitry Andric ++RPONumber; 2846e8d8bef9SDimitry Andric } 2847fe6060f1SDimitry Andric 2848fe6060f1SDimitry Andric // Order value substitutions by their "source" operand pair, for quick lookup. 2849fe6060f1SDimitry Andric llvm::sort(MF.DebugValueSubstitutions); 2850fe6060f1SDimitry Andric 2851fe6060f1SDimitry Andric #ifdef EXPENSIVE_CHECKS 2852fe6060f1SDimitry Andric // As an expensive check, test whether there are any duplicate substitution 2853fe6060f1SDimitry Andric // sources in the collection. 2854fe6060f1SDimitry Andric if (MF.DebugValueSubstitutions.size() > 2) { 2855fe6060f1SDimitry Andric for (auto It = MF.DebugValueSubstitutions.begin(); 2856fe6060f1SDimitry Andric It != std::prev(MF.DebugValueSubstitutions.end()); ++It) { 2857fe6060f1SDimitry Andric assert(It->Src != std::next(It)->Src && "Duplicate variable location " 2858fe6060f1SDimitry Andric "substitution seen"); 2859fe6060f1SDimitry Andric } 2860fe6060f1SDimitry Andric } 2861fe6060f1SDimitry Andric #endif 2862e8d8bef9SDimitry Andric } 2863e8d8bef9SDimitry Andric 2864*1fd87a68SDimitry Andric bool InstrRefBasedLDV::emitTransfers( 2865*1fd87a68SDimitry Andric DenseMap<DebugVariable, unsigned> &AllVarsNumbering) { 2866*1fd87a68SDimitry Andric // Go through all the transfers recorded in the TransferTracker -- this is 2867*1fd87a68SDimitry Andric // both the live-ins to a block, and any movements of values that happen 2868*1fd87a68SDimitry Andric // in the middle. 2869*1fd87a68SDimitry Andric for (const auto &P : TTracker->Transfers) { 2870*1fd87a68SDimitry Andric // We have to insert DBG_VALUEs in a consistent order, otherwise they 2871*1fd87a68SDimitry Andric // appear in DWARF in different orders. Use the order that they appear 2872*1fd87a68SDimitry Andric // when walking through each block / each instruction, stored in 2873*1fd87a68SDimitry Andric // AllVarsNumbering. 2874*1fd87a68SDimitry Andric SmallVector<std::pair<unsigned, MachineInstr *>> Insts; 2875*1fd87a68SDimitry Andric for (MachineInstr *MI : P.Insts) { 2876*1fd87a68SDimitry Andric DebugVariable Var(MI->getDebugVariable(), MI->getDebugExpression(), 2877*1fd87a68SDimitry Andric MI->getDebugLoc()->getInlinedAt()); 2878*1fd87a68SDimitry Andric Insts.emplace_back(AllVarsNumbering.find(Var)->second, MI); 2879*1fd87a68SDimitry Andric } 2880*1fd87a68SDimitry Andric llvm::sort(Insts, 2881*1fd87a68SDimitry Andric [](const auto &A, const auto &B) { return A.first < B.first; }); 2882*1fd87a68SDimitry Andric 2883*1fd87a68SDimitry Andric // Insert either before or after the designated point... 2884*1fd87a68SDimitry Andric if (P.MBB) { 2885*1fd87a68SDimitry Andric MachineBasicBlock &MBB = *P.MBB; 2886*1fd87a68SDimitry Andric for (const auto &Pair : Insts) 2887*1fd87a68SDimitry Andric MBB.insert(P.Pos, Pair.second); 2888*1fd87a68SDimitry Andric } else { 2889*1fd87a68SDimitry Andric // Terminators, like tail calls, can clobber things. Don't try and place 2890*1fd87a68SDimitry Andric // transfers after them. 2891*1fd87a68SDimitry Andric if (P.Pos->isTerminator()) 2892*1fd87a68SDimitry Andric continue; 2893*1fd87a68SDimitry Andric 2894*1fd87a68SDimitry Andric MachineBasicBlock &MBB = *P.Pos->getParent(); 2895*1fd87a68SDimitry Andric for (const auto &Pair : Insts) 2896*1fd87a68SDimitry Andric MBB.insertAfterBundle(P.Pos, Pair.second); 2897*1fd87a68SDimitry Andric } 2898*1fd87a68SDimitry Andric } 2899*1fd87a68SDimitry Andric 2900*1fd87a68SDimitry Andric return TTracker->Transfers.size() != 0; 2901*1fd87a68SDimitry Andric } 2902*1fd87a68SDimitry Andric 2903e8d8bef9SDimitry Andric /// Calculate the liveness information for the given machine function and 2904e8d8bef9SDimitry Andric /// extend ranges across basic blocks. 2905e8d8bef9SDimitry Andric bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF, 2906349cc55cSDimitry Andric MachineDominatorTree *DomTree, 2907349cc55cSDimitry Andric TargetPassConfig *TPC, 2908349cc55cSDimitry Andric unsigned InputBBLimit, 2909349cc55cSDimitry Andric unsigned InputDbgValLimit) { 2910e8d8bef9SDimitry Andric // No subprogram means this function contains no debuginfo. 2911e8d8bef9SDimitry Andric if (!MF.getFunction().getSubprogram()) 2912e8d8bef9SDimitry Andric return false; 2913e8d8bef9SDimitry Andric 2914e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "\nDebug Range Extension\n"); 2915e8d8bef9SDimitry Andric this->TPC = TPC; 2916e8d8bef9SDimitry Andric 2917349cc55cSDimitry Andric this->DomTree = DomTree; 2918e8d8bef9SDimitry Andric TRI = MF.getSubtarget().getRegisterInfo(); 2919349cc55cSDimitry Andric MRI = &MF.getRegInfo(); 2920e8d8bef9SDimitry Andric TII = MF.getSubtarget().getInstrInfo(); 2921e8d8bef9SDimitry Andric TFI = MF.getSubtarget().getFrameLowering(); 2922e8d8bef9SDimitry Andric TFI->getCalleeSaves(MF, CalleeSavedRegs); 2923fe6060f1SDimitry Andric MFI = &MF.getFrameInfo(); 2924e8d8bef9SDimitry Andric LS.initialize(MF); 2925e8d8bef9SDimitry Andric 29264824e7fdSDimitry Andric const auto &STI = MF.getSubtarget(); 29274824e7fdSDimitry Andric AdjustsStackInCalls = MFI->adjustsStack() && 29284824e7fdSDimitry Andric STI.getFrameLowering()->stackProbeFunctionModifiesSP(); 29294824e7fdSDimitry Andric if (AdjustsStackInCalls) 29304824e7fdSDimitry Andric StackProbeSymbolName = STI.getTargetLowering()->getStackProbeSymbolName(MF); 29314824e7fdSDimitry Andric 2932e8d8bef9SDimitry Andric MTracker = 2933e8d8bef9SDimitry Andric new MLocTracker(MF, *TII, *TRI, *MF.getSubtarget().getTargetLowering()); 2934e8d8bef9SDimitry Andric VTracker = nullptr; 2935e8d8bef9SDimitry Andric TTracker = nullptr; 2936e8d8bef9SDimitry Andric 2937e8d8bef9SDimitry Andric SmallVector<MLocTransferMap, 32> MLocTransfer; 2938e8d8bef9SDimitry Andric SmallVector<VLocTracker, 8> vlocs; 2939e8d8bef9SDimitry Andric LiveInsT SavedLiveIns; 2940e8d8bef9SDimitry Andric 2941e8d8bef9SDimitry Andric int MaxNumBlocks = -1; 2942e8d8bef9SDimitry Andric for (auto &MBB : MF) 2943e8d8bef9SDimitry Andric MaxNumBlocks = std::max(MBB.getNumber(), MaxNumBlocks); 2944e8d8bef9SDimitry Andric assert(MaxNumBlocks >= 0); 2945e8d8bef9SDimitry Andric ++MaxNumBlocks; 2946e8d8bef9SDimitry Andric 2947e8d8bef9SDimitry Andric MLocTransfer.resize(MaxNumBlocks); 29484824e7fdSDimitry Andric vlocs.resize(MaxNumBlocks, VLocTracker(OverlapFragments, EmptyExpr)); 2949e8d8bef9SDimitry Andric SavedLiveIns.resize(MaxNumBlocks); 2950e8d8bef9SDimitry Andric 2951e8d8bef9SDimitry Andric initialSetup(MF); 2952e8d8bef9SDimitry Andric 2953e8d8bef9SDimitry Andric produceMLocTransferFunction(MF, MLocTransfer, MaxNumBlocks); 2954e8d8bef9SDimitry Andric 2955e8d8bef9SDimitry Andric // Allocate and initialize two array-of-arrays for the live-in and live-out 2956e8d8bef9SDimitry Andric // machine values. The outer dimension is the block number; while the inner 2957e8d8bef9SDimitry Andric // dimension is a LocIdx from MLocTracker. 2958e8d8bef9SDimitry Andric ValueIDNum **MOutLocs = new ValueIDNum *[MaxNumBlocks]; 2959e8d8bef9SDimitry Andric ValueIDNum **MInLocs = new ValueIDNum *[MaxNumBlocks]; 2960e8d8bef9SDimitry Andric unsigned NumLocs = MTracker->getNumLocs(); 2961e8d8bef9SDimitry Andric for (int i = 0; i < MaxNumBlocks; ++i) { 2962349cc55cSDimitry Andric // These all auto-initialize to ValueIDNum::EmptyValue 2963e8d8bef9SDimitry Andric MOutLocs[i] = new ValueIDNum[NumLocs]; 2964e8d8bef9SDimitry Andric MInLocs[i] = new ValueIDNum[NumLocs]; 2965e8d8bef9SDimitry Andric } 2966e8d8bef9SDimitry Andric 2967e8d8bef9SDimitry Andric // Solve the machine value dataflow problem using the MLocTransfer function, 2968e8d8bef9SDimitry Andric // storing the computed live-ins / live-outs into the array-of-arrays. We use 2969e8d8bef9SDimitry Andric // both live-ins and live-outs for decision making in the variable value 2970e8d8bef9SDimitry Andric // dataflow problem. 2971349cc55cSDimitry Andric buildMLocValueMap(MF, MInLocs, MOutLocs, MLocTransfer); 2972e8d8bef9SDimitry Andric 2973fe6060f1SDimitry Andric // Patch up debug phi numbers, turning unknown block-live-in values into 2974fe6060f1SDimitry Andric // either live-through machine values, or PHIs. 2975fe6060f1SDimitry Andric for (auto &DBG_PHI : DebugPHINumToValue) { 2976fe6060f1SDimitry Andric // Identify unresolved block-live-ins. 2977fe6060f1SDimitry Andric ValueIDNum &Num = DBG_PHI.ValueRead; 2978fe6060f1SDimitry Andric if (!Num.isPHI()) 2979fe6060f1SDimitry Andric continue; 2980fe6060f1SDimitry Andric 2981fe6060f1SDimitry Andric unsigned BlockNo = Num.getBlock(); 2982fe6060f1SDimitry Andric LocIdx LocNo = Num.getLoc(); 2983fe6060f1SDimitry Andric Num = MInLocs[BlockNo][LocNo.asU64()]; 2984fe6060f1SDimitry Andric } 2985fe6060f1SDimitry Andric // Later, we'll be looking up ranges of instruction numbers. 2986fe6060f1SDimitry Andric llvm::sort(DebugPHINumToValue); 2987fe6060f1SDimitry Andric 2988e8d8bef9SDimitry Andric // Walk back through each block / instruction, collecting DBG_VALUE 2989e8d8bef9SDimitry Andric // instructions and recording what machine value their operands refer to. 2990e8d8bef9SDimitry Andric for (auto &OrderPair : OrderToBB) { 2991e8d8bef9SDimitry Andric MachineBasicBlock &MBB = *OrderPair.second; 2992e8d8bef9SDimitry Andric CurBB = MBB.getNumber(); 2993e8d8bef9SDimitry Andric VTracker = &vlocs[CurBB]; 2994e8d8bef9SDimitry Andric VTracker->MBB = &MBB; 2995e8d8bef9SDimitry Andric MTracker->loadFromArray(MInLocs[CurBB], CurBB); 2996e8d8bef9SDimitry Andric CurInst = 1; 2997e8d8bef9SDimitry Andric for (auto &MI : MBB) { 2998fe6060f1SDimitry Andric process(MI, MOutLocs, MInLocs); 2999e8d8bef9SDimitry Andric ++CurInst; 3000e8d8bef9SDimitry Andric } 3001e8d8bef9SDimitry Andric MTracker->reset(); 3002e8d8bef9SDimitry Andric } 3003e8d8bef9SDimitry Andric 3004e8d8bef9SDimitry Andric // Number all variables in the order that they appear, to be used as a stable 3005e8d8bef9SDimitry Andric // insertion order later. 3006e8d8bef9SDimitry Andric DenseMap<DebugVariable, unsigned> AllVarsNumbering; 3007e8d8bef9SDimitry Andric 3008e8d8bef9SDimitry Andric // Map from one LexicalScope to all the variables in that scope. 3009*1fd87a68SDimitry Andric ScopeToVarsT ScopeToVars; 3010e8d8bef9SDimitry Andric 3011*1fd87a68SDimitry Andric // Map from One lexical scope to all blocks where assignments happen for 3012*1fd87a68SDimitry Andric // that scope. 3013*1fd87a68SDimitry Andric ScopeToAssignBlocksT ScopeToAssignBlocks; 3014e8d8bef9SDimitry Andric 3015*1fd87a68SDimitry Andric // Store map of DILocations that describes scopes. 3016*1fd87a68SDimitry Andric ScopeToDILocT ScopeToDILocation; 3017e8d8bef9SDimitry Andric 3018e8d8bef9SDimitry Andric // To mirror old LiveDebugValues, enumerate variables in RPOT order. Otherwise 3019e8d8bef9SDimitry Andric // the order is unimportant, it just has to be stable. 3020349cc55cSDimitry Andric unsigned VarAssignCount = 0; 3021e8d8bef9SDimitry Andric for (unsigned int I = 0; I < OrderToBB.size(); ++I) { 3022e8d8bef9SDimitry Andric auto *MBB = OrderToBB[I]; 3023e8d8bef9SDimitry Andric auto *VTracker = &vlocs[MBB->getNumber()]; 3024e8d8bef9SDimitry Andric // Collect each variable with a DBG_VALUE in this block. 3025e8d8bef9SDimitry Andric for (auto &idx : VTracker->Vars) { 3026e8d8bef9SDimitry Andric const auto &Var = idx.first; 3027e8d8bef9SDimitry Andric const DILocation *ScopeLoc = VTracker->Scopes[Var]; 3028e8d8bef9SDimitry Andric assert(ScopeLoc != nullptr); 3029e8d8bef9SDimitry Andric auto *Scope = LS.findLexicalScope(ScopeLoc); 3030e8d8bef9SDimitry Andric 3031e8d8bef9SDimitry Andric // No insts in scope -> shouldn't have been recorded. 3032e8d8bef9SDimitry Andric assert(Scope != nullptr); 3033e8d8bef9SDimitry Andric 3034e8d8bef9SDimitry Andric AllVarsNumbering.insert(std::make_pair(Var, AllVarsNumbering.size())); 3035e8d8bef9SDimitry Andric ScopeToVars[Scope].insert(Var); 3036*1fd87a68SDimitry Andric ScopeToAssignBlocks[Scope].insert(VTracker->MBB); 3037e8d8bef9SDimitry Andric ScopeToDILocation[Scope] = ScopeLoc; 3038349cc55cSDimitry Andric ++VarAssignCount; 3039e8d8bef9SDimitry Andric } 3040e8d8bef9SDimitry Andric } 3041e8d8bef9SDimitry Andric 3042349cc55cSDimitry Andric bool Changed = false; 3043349cc55cSDimitry Andric 3044349cc55cSDimitry Andric // If we have an extremely large number of variable assignments and blocks, 3045349cc55cSDimitry Andric // bail out at this point. We've burnt some time doing analysis already, 3046349cc55cSDimitry Andric // however we should cut our losses. 3047349cc55cSDimitry Andric if ((unsigned)MaxNumBlocks > InputBBLimit && 3048349cc55cSDimitry Andric VarAssignCount > InputDbgValLimit) { 3049349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << "Disabling InstrRefBasedLDV: " << MF.getName() 3050349cc55cSDimitry Andric << " has " << MaxNumBlocks << " basic blocks and " 3051349cc55cSDimitry Andric << VarAssignCount 3052349cc55cSDimitry Andric << " variable assignments, exceeding limits.\n"); 3053349cc55cSDimitry Andric } else { 3054349cc55cSDimitry Andric // Compute the extended ranges, iterating over scopes. There might be 3055349cc55cSDimitry Andric // something to be said for ordering them by size/locality, but that's for 3056349cc55cSDimitry Andric // the future. For each scope, solve the variable value problem, producing 3057349cc55cSDimitry Andric // a map of variables to values in SavedLiveIns. 3058e8d8bef9SDimitry Andric for (auto &P : ScopeToVars) { 3059349cc55cSDimitry Andric buildVLocValueMap(ScopeToDILocation[P.first], P.second, 3060*1fd87a68SDimitry Andric ScopeToAssignBlocks[P.first], SavedLiveIns, MOutLocs, MInLocs, 3061e8d8bef9SDimitry Andric vlocs); 3062e8d8bef9SDimitry Andric } 3063e8d8bef9SDimitry Andric 3064e8d8bef9SDimitry Andric // Using the computed value locations and variable values for each block, 3065e8d8bef9SDimitry Andric // create the DBG_VALUE instructions representing the extended variable 3066e8d8bef9SDimitry Andric // locations. 3067fe6060f1SDimitry Andric emitLocations(MF, SavedLiveIns, MOutLocs, MInLocs, AllVarsNumbering, *TPC); 3068e8d8bef9SDimitry Andric 3069349cc55cSDimitry Andric // Did we actually make any changes? If we created any DBG_VALUEs, then yes. 3070349cc55cSDimitry Andric Changed = TTracker->Transfers.size() != 0; 3071349cc55cSDimitry Andric } 3072349cc55cSDimitry Andric 3073349cc55cSDimitry Andric // Common clean-up of memory. 3074e8d8bef9SDimitry Andric for (int Idx = 0; Idx < MaxNumBlocks; ++Idx) { 3075e8d8bef9SDimitry Andric delete[] MOutLocs[Idx]; 3076e8d8bef9SDimitry Andric delete[] MInLocs[Idx]; 3077e8d8bef9SDimitry Andric } 3078e8d8bef9SDimitry Andric delete[] MOutLocs; 3079e8d8bef9SDimitry Andric delete[] MInLocs; 3080e8d8bef9SDimitry Andric 3081e8d8bef9SDimitry Andric delete MTracker; 3082e8d8bef9SDimitry Andric delete TTracker; 3083e8d8bef9SDimitry Andric MTracker = nullptr; 3084e8d8bef9SDimitry Andric VTracker = nullptr; 3085e8d8bef9SDimitry Andric TTracker = nullptr; 3086e8d8bef9SDimitry Andric 3087e8d8bef9SDimitry Andric ArtificialBlocks.clear(); 3088e8d8bef9SDimitry Andric OrderToBB.clear(); 3089e8d8bef9SDimitry Andric BBToOrder.clear(); 3090e8d8bef9SDimitry Andric BBNumToRPO.clear(); 3091e8d8bef9SDimitry Andric DebugInstrNumToInstr.clear(); 3092fe6060f1SDimitry Andric DebugPHINumToValue.clear(); 30934824e7fdSDimitry Andric OverlapFragments.clear(); 30944824e7fdSDimitry Andric SeenFragments.clear(); 3095e8d8bef9SDimitry Andric 3096e8d8bef9SDimitry Andric return Changed; 3097e8d8bef9SDimitry Andric } 3098e8d8bef9SDimitry Andric 3099e8d8bef9SDimitry Andric LDVImpl *llvm::makeInstrRefBasedLiveDebugValues() { 3100e8d8bef9SDimitry Andric return new InstrRefBasedLDV(); 3101e8d8bef9SDimitry Andric } 3102fe6060f1SDimitry Andric 3103fe6060f1SDimitry Andric namespace { 3104fe6060f1SDimitry Andric class LDVSSABlock; 3105fe6060f1SDimitry Andric class LDVSSAUpdater; 3106fe6060f1SDimitry Andric 3107fe6060f1SDimitry Andric // Pick a type to identify incoming block values as we construct SSA. We 3108fe6060f1SDimitry Andric // can't use anything more robust than an integer unfortunately, as SSAUpdater 3109fe6060f1SDimitry Andric // expects to zero-initialize the type. 3110fe6060f1SDimitry Andric typedef uint64_t BlockValueNum; 3111fe6060f1SDimitry Andric 3112fe6060f1SDimitry Andric /// Represents an SSA PHI node for the SSA updater class. Contains the block 3113fe6060f1SDimitry Andric /// this PHI is in, the value number it would have, and the expected incoming 3114fe6060f1SDimitry Andric /// values from parent blocks. 3115fe6060f1SDimitry Andric class LDVSSAPhi { 3116fe6060f1SDimitry Andric public: 3117fe6060f1SDimitry Andric SmallVector<std::pair<LDVSSABlock *, BlockValueNum>, 4> IncomingValues; 3118fe6060f1SDimitry Andric LDVSSABlock *ParentBlock; 3119fe6060f1SDimitry Andric BlockValueNum PHIValNum; 3120fe6060f1SDimitry Andric LDVSSAPhi(BlockValueNum PHIValNum, LDVSSABlock *ParentBlock) 3121fe6060f1SDimitry Andric : ParentBlock(ParentBlock), PHIValNum(PHIValNum) {} 3122fe6060f1SDimitry Andric 3123fe6060f1SDimitry Andric LDVSSABlock *getParent() { return ParentBlock; } 3124fe6060f1SDimitry Andric }; 3125fe6060f1SDimitry Andric 3126fe6060f1SDimitry Andric /// Thin wrapper around a block predecessor iterator. Only difference from a 3127fe6060f1SDimitry Andric /// normal block iterator is that it dereferences to an LDVSSABlock. 3128fe6060f1SDimitry Andric class LDVSSABlockIterator { 3129fe6060f1SDimitry Andric public: 3130fe6060f1SDimitry Andric MachineBasicBlock::pred_iterator PredIt; 3131fe6060f1SDimitry Andric LDVSSAUpdater &Updater; 3132fe6060f1SDimitry Andric 3133fe6060f1SDimitry Andric LDVSSABlockIterator(MachineBasicBlock::pred_iterator PredIt, 3134fe6060f1SDimitry Andric LDVSSAUpdater &Updater) 3135fe6060f1SDimitry Andric : PredIt(PredIt), Updater(Updater) {} 3136fe6060f1SDimitry Andric 3137fe6060f1SDimitry Andric bool operator!=(const LDVSSABlockIterator &OtherIt) const { 3138fe6060f1SDimitry Andric return OtherIt.PredIt != PredIt; 3139fe6060f1SDimitry Andric } 3140fe6060f1SDimitry Andric 3141fe6060f1SDimitry Andric LDVSSABlockIterator &operator++() { 3142fe6060f1SDimitry Andric ++PredIt; 3143fe6060f1SDimitry Andric return *this; 3144fe6060f1SDimitry Andric } 3145fe6060f1SDimitry Andric 3146fe6060f1SDimitry Andric LDVSSABlock *operator*(); 3147fe6060f1SDimitry Andric }; 3148fe6060f1SDimitry Andric 3149fe6060f1SDimitry Andric /// Thin wrapper around a block for SSA Updater interface. Necessary because 3150fe6060f1SDimitry Andric /// we need to track the PHI value(s) that we may have observed as necessary 3151fe6060f1SDimitry Andric /// in this block. 3152fe6060f1SDimitry Andric class LDVSSABlock { 3153fe6060f1SDimitry Andric public: 3154fe6060f1SDimitry Andric MachineBasicBlock &BB; 3155fe6060f1SDimitry Andric LDVSSAUpdater &Updater; 3156fe6060f1SDimitry Andric using PHIListT = SmallVector<LDVSSAPhi, 1>; 3157fe6060f1SDimitry Andric /// List of PHIs in this block. There should only ever be one. 3158fe6060f1SDimitry Andric PHIListT PHIList; 3159fe6060f1SDimitry Andric 3160fe6060f1SDimitry Andric LDVSSABlock(MachineBasicBlock &BB, LDVSSAUpdater &Updater) 3161fe6060f1SDimitry Andric : BB(BB), Updater(Updater) {} 3162fe6060f1SDimitry Andric 3163fe6060f1SDimitry Andric LDVSSABlockIterator succ_begin() { 3164fe6060f1SDimitry Andric return LDVSSABlockIterator(BB.succ_begin(), Updater); 3165fe6060f1SDimitry Andric } 3166fe6060f1SDimitry Andric 3167fe6060f1SDimitry Andric LDVSSABlockIterator succ_end() { 3168fe6060f1SDimitry Andric return LDVSSABlockIterator(BB.succ_end(), Updater); 3169fe6060f1SDimitry Andric } 3170fe6060f1SDimitry Andric 3171fe6060f1SDimitry Andric /// SSAUpdater has requested a PHI: create that within this block record. 3172fe6060f1SDimitry Andric LDVSSAPhi *newPHI(BlockValueNum Value) { 3173fe6060f1SDimitry Andric PHIList.emplace_back(Value, this); 3174fe6060f1SDimitry Andric return &PHIList.back(); 3175fe6060f1SDimitry Andric } 3176fe6060f1SDimitry Andric 3177fe6060f1SDimitry Andric /// SSAUpdater wishes to know what PHIs already exist in this block. 3178fe6060f1SDimitry Andric PHIListT &phis() { return PHIList; } 3179fe6060f1SDimitry Andric }; 3180fe6060f1SDimitry Andric 3181fe6060f1SDimitry Andric /// Utility class for the SSAUpdater interface: tracks blocks, PHIs and values 3182fe6060f1SDimitry Andric /// while SSAUpdater is exploring the CFG. It's passed as a handle / baton to 3183fe6060f1SDimitry Andric // SSAUpdaterTraits<LDVSSAUpdater>. 3184fe6060f1SDimitry Andric class LDVSSAUpdater { 3185fe6060f1SDimitry Andric public: 3186fe6060f1SDimitry Andric /// Map of value numbers to PHI records. 3187fe6060f1SDimitry Andric DenseMap<BlockValueNum, LDVSSAPhi *> PHIs; 3188fe6060f1SDimitry Andric /// Map of which blocks generate Undef values -- blocks that are not 3189fe6060f1SDimitry Andric /// dominated by any Def. 3190fe6060f1SDimitry Andric DenseMap<MachineBasicBlock *, BlockValueNum> UndefMap; 3191fe6060f1SDimitry Andric /// Map of machine blocks to our own records of them. 3192fe6060f1SDimitry Andric DenseMap<MachineBasicBlock *, LDVSSABlock *> BlockMap; 3193fe6060f1SDimitry Andric /// Machine location where any PHI must occur. 3194fe6060f1SDimitry Andric LocIdx Loc; 3195fe6060f1SDimitry Andric /// Table of live-in machine value numbers for blocks / locations. 3196fe6060f1SDimitry Andric ValueIDNum **MLiveIns; 3197fe6060f1SDimitry Andric 3198fe6060f1SDimitry Andric LDVSSAUpdater(LocIdx L, ValueIDNum **MLiveIns) : Loc(L), MLiveIns(MLiveIns) {} 3199fe6060f1SDimitry Andric 3200fe6060f1SDimitry Andric void reset() { 3201fe6060f1SDimitry Andric for (auto &Block : BlockMap) 3202fe6060f1SDimitry Andric delete Block.second; 3203fe6060f1SDimitry Andric 3204fe6060f1SDimitry Andric PHIs.clear(); 3205fe6060f1SDimitry Andric UndefMap.clear(); 3206fe6060f1SDimitry Andric BlockMap.clear(); 3207fe6060f1SDimitry Andric } 3208fe6060f1SDimitry Andric 3209fe6060f1SDimitry Andric ~LDVSSAUpdater() { reset(); } 3210fe6060f1SDimitry Andric 3211fe6060f1SDimitry Andric /// For a given MBB, create a wrapper block for it. Stores it in the 3212fe6060f1SDimitry Andric /// LDVSSAUpdater block map. 3213fe6060f1SDimitry Andric LDVSSABlock *getSSALDVBlock(MachineBasicBlock *BB) { 3214fe6060f1SDimitry Andric auto it = BlockMap.find(BB); 3215fe6060f1SDimitry Andric if (it == BlockMap.end()) { 3216fe6060f1SDimitry Andric BlockMap[BB] = new LDVSSABlock(*BB, *this); 3217fe6060f1SDimitry Andric it = BlockMap.find(BB); 3218fe6060f1SDimitry Andric } 3219fe6060f1SDimitry Andric return it->second; 3220fe6060f1SDimitry Andric } 3221fe6060f1SDimitry Andric 3222fe6060f1SDimitry Andric /// Find the live-in value number for the given block. Looks up the value at 3223fe6060f1SDimitry Andric /// the PHI location on entry. 3224fe6060f1SDimitry Andric BlockValueNum getValue(LDVSSABlock *LDVBB) { 3225fe6060f1SDimitry Andric return MLiveIns[LDVBB->BB.getNumber()][Loc.asU64()].asU64(); 3226fe6060f1SDimitry Andric } 3227fe6060f1SDimitry Andric }; 3228fe6060f1SDimitry Andric 3229fe6060f1SDimitry Andric LDVSSABlock *LDVSSABlockIterator::operator*() { 3230fe6060f1SDimitry Andric return Updater.getSSALDVBlock(*PredIt); 3231fe6060f1SDimitry Andric } 3232fe6060f1SDimitry Andric 3233fe6060f1SDimitry Andric #ifndef NDEBUG 3234fe6060f1SDimitry Andric 3235fe6060f1SDimitry Andric raw_ostream &operator<<(raw_ostream &out, const LDVSSAPhi &PHI) { 3236fe6060f1SDimitry Andric out << "SSALDVPHI " << PHI.PHIValNum; 3237fe6060f1SDimitry Andric return out; 3238fe6060f1SDimitry Andric } 3239fe6060f1SDimitry Andric 3240fe6060f1SDimitry Andric #endif 3241fe6060f1SDimitry Andric 3242fe6060f1SDimitry Andric } // namespace 3243fe6060f1SDimitry Andric 3244fe6060f1SDimitry Andric namespace llvm { 3245fe6060f1SDimitry Andric 3246fe6060f1SDimitry Andric /// Template specialization to give SSAUpdater access to CFG and value 3247fe6060f1SDimitry Andric /// information. SSAUpdater calls methods in these traits, passing in the 3248fe6060f1SDimitry Andric /// LDVSSAUpdater object, to learn about blocks and the values they define. 3249fe6060f1SDimitry Andric /// It also provides methods to create PHI nodes and track them. 3250fe6060f1SDimitry Andric template <> class SSAUpdaterTraits<LDVSSAUpdater> { 3251fe6060f1SDimitry Andric public: 3252fe6060f1SDimitry Andric using BlkT = LDVSSABlock; 3253fe6060f1SDimitry Andric using ValT = BlockValueNum; 3254fe6060f1SDimitry Andric using PhiT = LDVSSAPhi; 3255fe6060f1SDimitry Andric using BlkSucc_iterator = LDVSSABlockIterator; 3256fe6060f1SDimitry Andric 3257fe6060f1SDimitry Andric // Methods to access block successors -- dereferencing to our wrapper class. 3258fe6060f1SDimitry Andric static BlkSucc_iterator BlkSucc_begin(BlkT *BB) { return BB->succ_begin(); } 3259fe6060f1SDimitry Andric static BlkSucc_iterator BlkSucc_end(BlkT *BB) { return BB->succ_end(); } 3260fe6060f1SDimitry Andric 3261fe6060f1SDimitry Andric /// Iterator for PHI operands. 3262fe6060f1SDimitry Andric class PHI_iterator { 3263fe6060f1SDimitry Andric private: 3264fe6060f1SDimitry Andric LDVSSAPhi *PHI; 3265fe6060f1SDimitry Andric unsigned Idx; 3266fe6060f1SDimitry Andric 3267fe6060f1SDimitry Andric public: 3268fe6060f1SDimitry Andric explicit PHI_iterator(LDVSSAPhi *P) // begin iterator 3269fe6060f1SDimitry Andric : PHI(P), Idx(0) {} 3270fe6060f1SDimitry Andric PHI_iterator(LDVSSAPhi *P, bool) // end iterator 3271fe6060f1SDimitry Andric : PHI(P), Idx(PHI->IncomingValues.size()) {} 3272fe6060f1SDimitry Andric 3273fe6060f1SDimitry Andric PHI_iterator &operator++() { 3274fe6060f1SDimitry Andric Idx++; 3275fe6060f1SDimitry Andric return *this; 3276fe6060f1SDimitry Andric } 3277fe6060f1SDimitry Andric bool operator==(const PHI_iterator &X) const { return Idx == X.Idx; } 3278fe6060f1SDimitry Andric bool operator!=(const PHI_iterator &X) const { return !operator==(X); } 3279fe6060f1SDimitry Andric 3280fe6060f1SDimitry Andric BlockValueNum getIncomingValue() { return PHI->IncomingValues[Idx].second; } 3281fe6060f1SDimitry Andric 3282fe6060f1SDimitry Andric LDVSSABlock *getIncomingBlock() { return PHI->IncomingValues[Idx].first; } 3283fe6060f1SDimitry Andric }; 3284fe6060f1SDimitry Andric 3285fe6060f1SDimitry Andric static inline PHI_iterator PHI_begin(PhiT *PHI) { return PHI_iterator(PHI); } 3286fe6060f1SDimitry Andric 3287fe6060f1SDimitry Andric static inline PHI_iterator PHI_end(PhiT *PHI) { 3288fe6060f1SDimitry Andric return PHI_iterator(PHI, true); 3289fe6060f1SDimitry Andric } 3290fe6060f1SDimitry Andric 3291fe6060f1SDimitry Andric /// FindPredecessorBlocks - Put the predecessors of BB into the Preds 3292fe6060f1SDimitry Andric /// vector. 3293fe6060f1SDimitry Andric static void FindPredecessorBlocks(LDVSSABlock *BB, 3294fe6060f1SDimitry Andric SmallVectorImpl<LDVSSABlock *> *Preds) { 3295349cc55cSDimitry Andric for (MachineBasicBlock *Pred : BB->BB.predecessors()) 3296349cc55cSDimitry Andric Preds->push_back(BB->Updater.getSSALDVBlock(Pred)); 3297fe6060f1SDimitry Andric } 3298fe6060f1SDimitry Andric 3299fe6060f1SDimitry Andric /// GetUndefVal - Normally creates an IMPLICIT_DEF instruction with a new 3300fe6060f1SDimitry Andric /// register. For LiveDebugValues, represents a block identified as not having 3301fe6060f1SDimitry Andric /// any DBG_PHI predecessors. 3302fe6060f1SDimitry Andric static BlockValueNum GetUndefVal(LDVSSABlock *BB, LDVSSAUpdater *Updater) { 3303fe6060f1SDimitry Andric // Create a value number for this block -- it needs to be unique and in the 3304fe6060f1SDimitry Andric // "undef" collection, so that we know it's not real. Use a number 3305fe6060f1SDimitry Andric // representing a PHI into this block. 3306fe6060f1SDimitry Andric BlockValueNum Num = ValueIDNum(BB->BB.getNumber(), 0, Updater->Loc).asU64(); 3307fe6060f1SDimitry Andric Updater->UndefMap[&BB->BB] = Num; 3308fe6060f1SDimitry Andric return Num; 3309fe6060f1SDimitry Andric } 3310fe6060f1SDimitry Andric 3311fe6060f1SDimitry Andric /// CreateEmptyPHI - Create a (representation of a) PHI in the given block. 3312fe6060f1SDimitry Andric /// SSAUpdater will populate it with information about incoming values. The 3313fe6060f1SDimitry Andric /// value number of this PHI is whatever the machine value number problem 3314fe6060f1SDimitry Andric /// solution determined it to be. This includes non-phi values if SSAUpdater 3315fe6060f1SDimitry Andric /// tries to create a PHI where the incoming values are identical. 3316fe6060f1SDimitry Andric static BlockValueNum CreateEmptyPHI(LDVSSABlock *BB, unsigned NumPreds, 3317fe6060f1SDimitry Andric LDVSSAUpdater *Updater) { 3318fe6060f1SDimitry Andric BlockValueNum PHIValNum = Updater->getValue(BB); 3319fe6060f1SDimitry Andric LDVSSAPhi *PHI = BB->newPHI(PHIValNum); 3320fe6060f1SDimitry Andric Updater->PHIs[PHIValNum] = PHI; 3321fe6060f1SDimitry Andric return PHIValNum; 3322fe6060f1SDimitry Andric } 3323fe6060f1SDimitry Andric 3324fe6060f1SDimitry Andric /// AddPHIOperand - Add the specified value as an operand of the PHI for 3325fe6060f1SDimitry Andric /// the specified predecessor block. 3326fe6060f1SDimitry Andric static void AddPHIOperand(LDVSSAPhi *PHI, BlockValueNum Val, LDVSSABlock *Pred) { 3327fe6060f1SDimitry Andric PHI->IncomingValues.push_back(std::make_pair(Pred, Val)); 3328fe6060f1SDimitry Andric } 3329fe6060f1SDimitry Andric 3330fe6060f1SDimitry Andric /// ValueIsPHI - Check if the instruction that defines the specified value 3331fe6060f1SDimitry Andric /// is a PHI instruction. 3332fe6060f1SDimitry Andric static LDVSSAPhi *ValueIsPHI(BlockValueNum Val, LDVSSAUpdater *Updater) { 3333fe6060f1SDimitry Andric auto PHIIt = Updater->PHIs.find(Val); 3334fe6060f1SDimitry Andric if (PHIIt == Updater->PHIs.end()) 3335fe6060f1SDimitry Andric return nullptr; 3336fe6060f1SDimitry Andric return PHIIt->second; 3337fe6060f1SDimitry Andric } 3338fe6060f1SDimitry Andric 3339fe6060f1SDimitry Andric /// ValueIsNewPHI - Like ValueIsPHI but also check if the PHI has no source 3340fe6060f1SDimitry Andric /// operands, i.e., it was just added. 3341fe6060f1SDimitry Andric static LDVSSAPhi *ValueIsNewPHI(BlockValueNum Val, LDVSSAUpdater *Updater) { 3342fe6060f1SDimitry Andric LDVSSAPhi *PHI = ValueIsPHI(Val, Updater); 3343fe6060f1SDimitry Andric if (PHI && PHI->IncomingValues.size() == 0) 3344fe6060f1SDimitry Andric return PHI; 3345fe6060f1SDimitry Andric return nullptr; 3346fe6060f1SDimitry Andric } 3347fe6060f1SDimitry Andric 3348fe6060f1SDimitry Andric /// GetPHIValue - For the specified PHI instruction, return the value 3349fe6060f1SDimitry Andric /// that it defines. 3350fe6060f1SDimitry Andric static BlockValueNum GetPHIValue(LDVSSAPhi *PHI) { return PHI->PHIValNum; } 3351fe6060f1SDimitry Andric }; 3352fe6060f1SDimitry Andric 3353fe6060f1SDimitry Andric } // end namespace llvm 3354fe6060f1SDimitry Andric 3355fe6060f1SDimitry Andric Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIs(MachineFunction &MF, 3356fe6060f1SDimitry Andric ValueIDNum **MLiveOuts, 3357fe6060f1SDimitry Andric ValueIDNum **MLiveIns, 3358fe6060f1SDimitry Andric MachineInstr &Here, 3359fe6060f1SDimitry Andric uint64_t InstrNum) { 3360fe6060f1SDimitry Andric // Pick out records of DBG_PHI instructions that have been observed. If there 3361fe6060f1SDimitry Andric // are none, then we cannot compute a value number. 3362fe6060f1SDimitry Andric auto RangePair = std::equal_range(DebugPHINumToValue.begin(), 3363fe6060f1SDimitry Andric DebugPHINumToValue.end(), InstrNum); 3364fe6060f1SDimitry Andric auto LowerIt = RangePair.first; 3365fe6060f1SDimitry Andric auto UpperIt = RangePair.second; 3366fe6060f1SDimitry Andric 3367fe6060f1SDimitry Andric // No DBG_PHI means there can be no location. 3368fe6060f1SDimitry Andric if (LowerIt == UpperIt) 3369fe6060f1SDimitry Andric return None; 3370fe6060f1SDimitry Andric 3371fe6060f1SDimitry Andric // If there's only one DBG_PHI, then that is our value number. 3372fe6060f1SDimitry Andric if (std::distance(LowerIt, UpperIt) == 1) 3373fe6060f1SDimitry Andric return LowerIt->ValueRead; 3374fe6060f1SDimitry Andric 3375fe6060f1SDimitry Andric auto DBGPHIRange = make_range(LowerIt, UpperIt); 3376fe6060f1SDimitry Andric 3377fe6060f1SDimitry Andric // Pick out the location (physreg, slot) where any PHIs must occur. It's 3378fe6060f1SDimitry Andric // technically possible for us to merge values in different registers in each 3379fe6060f1SDimitry Andric // block, but highly unlikely that LLVM will generate such code after register 3380fe6060f1SDimitry Andric // allocation. 3381fe6060f1SDimitry Andric LocIdx Loc = LowerIt->ReadLoc; 3382fe6060f1SDimitry Andric 3383fe6060f1SDimitry Andric // We have several DBG_PHIs, and a use position (the Here inst). All each 3384fe6060f1SDimitry Andric // DBG_PHI does is identify a value at a program position. We can treat each 3385fe6060f1SDimitry Andric // DBG_PHI like it's a Def of a value, and the use position is a Use of a 3386fe6060f1SDimitry Andric // value, just like SSA. We use the bulk-standard LLVM SSA updater class to 3387fe6060f1SDimitry Andric // determine which Def is used at the Use, and any PHIs that happen along 3388fe6060f1SDimitry Andric // the way. 3389fe6060f1SDimitry Andric // Adapted LLVM SSA Updater: 3390fe6060f1SDimitry Andric LDVSSAUpdater Updater(Loc, MLiveIns); 3391fe6060f1SDimitry Andric // Map of which Def or PHI is the current value in each block. 3392fe6060f1SDimitry Andric DenseMap<LDVSSABlock *, BlockValueNum> AvailableValues; 3393fe6060f1SDimitry Andric // Set of PHIs that we have created along the way. 3394fe6060f1SDimitry Andric SmallVector<LDVSSAPhi *, 8> CreatedPHIs; 3395fe6060f1SDimitry Andric 3396fe6060f1SDimitry Andric // Each existing DBG_PHI is a Def'd value under this model. Record these Defs 3397fe6060f1SDimitry Andric // for the SSAUpdater. 3398fe6060f1SDimitry Andric for (const auto &DBG_PHI : DBGPHIRange) { 3399fe6060f1SDimitry Andric LDVSSABlock *Block = Updater.getSSALDVBlock(DBG_PHI.MBB); 3400fe6060f1SDimitry Andric const ValueIDNum &Num = DBG_PHI.ValueRead; 3401fe6060f1SDimitry Andric AvailableValues.insert(std::make_pair(Block, Num.asU64())); 3402fe6060f1SDimitry Andric } 3403fe6060f1SDimitry Andric 3404fe6060f1SDimitry Andric LDVSSABlock *HereBlock = Updater.getSSALDVBlock(Here.getParent()); 3405fe6060f1SDimitry Andric const auto &AvailIt = AvailableValues.find(HereBlock); 3406fe6060f1SDimitry Andric if (AvailIt != AvailableValues.end()) { 3407fe6060f1SDimitry Andric // Actually, we already know what the value is -- the Use is in the same 3408fe6060f1SDimitry Andric // block as the Def. 3409fe6060f1SDimitry Andric return ValueIDNum::fromU64(AvailIt->second); 3410fe6060f1SDimitry Andric } 3411fe6060f1SDimitry Andric 3412fe6060f1SDimitry Andric // Otherwise, we must use the SSA Updater. It will identify the value number 3413fe6060f1SDimitry Andric // that we are to use, and the PHIs that must happen along the way. 3414fe6060f1SDimitry Andric SSAUpdaterImpl<LDVSSAUpdater> Impl(&Updater, &AvailableValues, &CreatedPHIs); 3415fe6060f1SDimitry Andric BlockValueNum ResultInt = Impl.GetValue(Updater.getSSALDVBlock(Here.getParent())); 3416fe6060f1SDimitry Andric ValueIDNum Result = ValueIDNum::fromU64(ResultInt); 3417fe6060f1SDimitry Andric 3418fe6060f1SDimitry Andric // We have the number for a PHI, or possibly live-through value, to be used 3419fe6060f1SDimitry Andric // at this Use. There are a number of things we have to check about it though: 3420fe6060f1SDimitry Andric // * Does any PHI use an 'Undef' (like an IMPLICIT_DEF) value? If so, this 3421fe6060f1SDimitry Andric // Use was not completely dominated by DBG_PHIs and we should abort. 3422fe6060f1SDimitry Andric // * Are the Defs or PHIs clobbered in a block? SSAUpdater isn't aware that 3423fe6060f1SDimitry Andric // we've left SSA form. Validate that the inputs to each PHI are the 3424fe6060f1SDimitry Andric // expected values. 3425fe6060f1SDimitry Andric // * Is a PHI we've created actually a merging of values, or are all the 3426fe6060f1SDimitry Andric // predecessor values the same, leading to a non-PHI machine value number? 3427fe6060f1SDimitry Andric // (SSAUpdater doesn't know that either). Remap validated PHIs into the 3428fe6060f1SDimitry Andric // the ValidatedValues collection below to sort this out. 3429fe6060f1SDimitry Andric DenseMap<LDVSSABlock *, ValueIDNum> ValidatedValues; 3430fe6060f1SDimitry Andric 3431fe6060f1SDimitry Andric // Define all the input DBG_PHI values in ValidatedValues. 3432fe6060f1SDimitry Andric for (const auto &DBG_PHI : DBGPHIRange) { 3433fe6060f1SDimitry Andric LDVSSABlock *Block = Updater.getSSALDVBlock(DBG_PHI.MBB); 3434fe6060f1SDimitry Andric const ValueIDNum &Num = DBG_PHI.ValueRead; 3435fe6060f1SDimitry Andric ValidatedValues.insert(std::make_pair(Block, Num)); 3436fe6060f1SDimitry Andric } 3437fe6060f1SDimitry Andric 3438fe6060f1SDimitry Andric // Sort PHIs to validate into RPO-order. 3439fe6060f1SDimitry Andric SmallVector<LDVSSAPhi *, 8> SortedPHIs; 3440fe6060f1SDimitry Andric for (auto &PHI : CreatedPHIs) 3441fe6060f1SDimitry Andric SortedPHIs.push_back(PHI); 3442fe6060f1SDimitry Andric 3443fe6060f1SDimitry Andric std::sort( 3444fe6060f1SDimitry Andric SortedPHIs.begin(), SortedPHIs.end(), [&](LDVSSAPhi *A, LDVSSAPhi *B) { 3445fe6060f1SDimitry Andric return BBToOrder[&A->getParent()->BB] < BBToOrder[&B->getParent()->BB]; 3446fe6060f1SDimitry Andric }); 3447fe6060f1SDimitry Andric 3448fe6060f1SDimitry Andric for (auto &PHI : SortedPHIs) { 3449fe6060f1SDimitry Andric ValueIDNum ThisBlockValueNum = 3450fe6060f1SDimitry Andric MLiveIns[PHI->ParentBlock->BB.getNumber()][Loc.asU64()]; 3451fe6060f1SDimitry Andric 3452fe6060f1SDimitry Andric // Are all these things actually defined? 3453fe6060f1SDimitry Andric for (auto &PHIIt : PHI->IncomingValues) { 3454fe6060f1SDimitry Andric // Any undef input means DBG_PHIs didn't dominate the use point. 3455fe6060f1SDimitry Andric if (Updater.UndefMap.find(&PHIIt.first->BB) != Updater.UndefMap.end()) 3456fe6060f1SDimitry Andric return None; 3457fe6060f1SDimitry Andric 3458fe6060f1SDimitry Andric ValueIDNum ValueToCheck; 3459fe6060f1SDimitry Andric ValueIDNum *BlockLiveOuts = MLiveOuts[PHIIt.first->BB.getNumber()]; 3460fe6060f1SDimitry Andric 3461fe6060f1SDimitry Andric auto VVal = ValidatedValues.find(PHIIt.first); 3462fe6060f1SDimitry Andric if (VVal == ValidatedValues.end()) { 3463fe6060f1SDimitry Andric // We cross a loop, and this is a backedge. LLVMs tail duplication 3464fe6060f1SDimitry Andric // happens so late that DBG_PHI instructions should not be able to 3465fe6060f1SDimitry Andric // migrate into loops -- meaning we can only be live-through this 3466fe6060f1SDimitry Andric // loop. 3467fe6060f1SDimitry Andric ValueToCheck = ThisBlockValueNum; 3468fe6060f1SDimitry Andric } else { 3469fe6060f1SDimitry Andric // Does the block have as a live-out, in the location we're examining, 3470fe6060f1SDimitry Andric // the value that we expect? If not, it's been moved or clobbered. 3471fe6060f1SDimitry Andric ValueToCheck = VVal->second; 3472fe6060f1SDimitry Andric } 3473fe6060f1SDimitry Andric 3474fe6060f1SDimitry Andric if (BlockLiveOuts[Loc.asU64()] != ValueToCheck) 3475fe6060f1SDimitry Andric return None; 3476fe6060f1SDimitry Andric } 3477fe6060f1SDimitry Andric 3478fe6060f1SDimitry Andric // Record this value as validated. 3479fe6060f1SDimitry Andric ValidatedValues.insert({PHI->ParentBlock, ThisBlockValueNum}); 3480fe6060f1SDimitry Andric } 3481fe6060f1SDimitry Andric 3482fe6060f1SDimitry Andric // All the PHIs are valid: we can return what the SSAUpdater said our value 3483fe6060f1SDimitry Andric // number was. 3484fe6060f1SDimitry Andric return Result; 3485fe6060f1SDimitry Andric } 3486