AssignmentTrackingAnalysis.cpp 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426
  1. #include "llvm/CodeGen/AssignmentTrackingAnalysis.h"
  2. #include "llvm/ADT/DenseMapInfo.h"
  3. #include "llvm/ADT/IntervalMap.h"
  4. #include "llvm/ADT/PostOrderIterator.h"
  5. #include "llvm/ADT/STLExtras.h"
  6. #include "llvm/ADT/SmallSet.h"
  7. #include "llvm/ADT/Statistic.h"
  8. #include "llvm/ADT/UniqueVector.h"
  9. #include "llvm/Analysis/Interval.h"
  10. #include "llvm/BinaryFormat/Dwarf.h"
  11. #include "llvm/IR/BasicBlock.h"
  12. #include "llvm/IR/DataLayout.h"
  13. #include "llvm/IR/DebugInfo.h"
  14. #include "llvm/IR/Function.h"
  15. #include "llvm/IR/Instruction.h"
  16. #include "llvm/IR/IntrinsicInst.h"
  17. #include "llvm/IR/PassManager.h"
  18. #include "llvm/IR/PrintPasses.h"
  19. #include "llvm/InitializePasses.h"
  20. #include "llvm/Support/CommandLine.h"
  21. #include "llvm/Support/ErrorHandling.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  24. #include <assert.h>
  25. #include <cstdint>
  26. #include <optional>
  27. #include <sstream>
  28. #include <unordered_map>
  29. using namespace llvm;
  30. #define DEBUG_TYPE "debug-ata"
  31. STATISTIC(NumDefsScanned, "Number of dbg locs that get scanned for removal");
  32. STATISTIC(NumDefsRemoved, "Number of dbg locs removed");
  33. STATISTIC(NumWedgesScanned, "Number of dbg wedges scanned");
  34. STATISTIC(NumWedgesChanged, "Number of dbg wedges changed");
  35. static cl::opt<unsigned>
  36. MaxNumBlocks("debug-ata-max-blocks", cl::init(10000),
  37. cl::desc("Maximum num basic blocks before debug info dropped"),
  38. cl::Hidden);
  39. /// Option for debugging the pass, determines if the memory location fragment
  40. /// filling happens after generating the variable locations.
  41. static cl::opt<bool> EnableMemLocFragFill("mem-loc-frag-fill", cl::init(true),
  42. cl::Hidden);
  43. /// Print the results of the analysis. Respects -filter-print-funcs.
  44. static cl::opt<bool> PrintResults("print-debug-ata", cl::init(false),
  45. cl::Hidden);
  46. // Implicit conversions are disabled for enum class types, so unfortunately we
  47. // need to create a DenseMapInfo wrapper around the specified underlying type.
  48. template <> struct llvm::DenseMapInfo<VariableID> {
  49. using Wrapped = DenseMapInfo<unsigned>;
  50. static inline VariableID getEmptyKey() {
  51. return static_cast<VariableID>(Wrapped::getEmptyKey());
  52. }
  53. static inline VariableID getTombstoneKey() {
  54. return static_cast<VariableID>(Wrapped::getTombstoneKey());
  55. }
  56. static unsigned getHashValue(const VariableID &Val) {
  57. return Wrapped::getHashValue(static_cast<unsigned>(Val));
  58. }
  59. static bool isEqual(const VariableID &LHS, const VariableID &RHS) {
  60. return LHS == RHS;
  61. }
  62. };
  63. /// Helper class to build FunctionVarLocs, since that class isn't easy to
  64. /// modify. TODO: There's not a great deal of value in the split, it could be
  65. /// worth merging the two classes.
  66. class FunctionVarLocsBuilder {
  67. friend FunctionVarLocs;
  68. UniqueVector<DebugVariable> Variables;
  69. // Use an unordered_map so we don't invalidate iterators after
  70. // insert/modifications.
  71. std::unordered_map<const Instruction *, SmallVector<VarLocInfo>>
  72. VarLocsBeforeInst;
  73. SmallVector<VarLocInfo> SingleLocVars;
  74. public:
  75. /// Find or insert \p V and return the ID.
  76. VariableID insertVariable(DebugVariable V) {
  77. return static_cast<VariableID>(Variables.insert(V));
  78. }
  79. /// Get a variable from its \p ID.
  80. const DebugVariable &getVariable(VariableID ID) const {
  81. return Variables[static_cast<unsigned>(ID)];
  82. }
  83. /// Return ptr to wedge of defs or nullptr if no defs come just before /p
  84. /// Before.
  85. const SmallVectorImpl<VarLocInfo> *getWedge(const Instruction *Before) const {
  86. auto R = VarLocsBeforeInst.find(Before);
  87. if (R == VarLocsBeforeInst.end())
  88. return nullptr;
  89. return &R->second;
  90. }
  91. /// Replace the defs that come just before /p Before with /p Wedge.
  92. void setWedge(const Instruction *Before, SmallVector<VarLocInfo> &&Wedge) {
  93. VarLocsBeforeInst[Before] = std::move(Wedge);
  94. }
  95. /// Add a def for a variable that is valid for its lifetime.
  96. void addSingleLocVar(DebugVariable Var, DIExpression *Expr, DebugLoc DL,
  97. Value *V) {
  98. VarLocInfo VarLoc;
  99. VarLoc.VariableID = insertVariable(Var);
  100. VarLoc.Expr = Expr;
  101. VarLoc.DL = DL;
  102. VarLoc.V = V;
  103. SingleLocVars.emplace_back(VarLoc);
  104. }
  105. /// Add a def to the wedge of defs just before /p Before.
  106. void addVarLoc(Instruction *Before, DebugVariable Var, DIExpression *Expr,
  107. DebugLoc DL, Value *V) {
  108. VarLocInfo VarLoc;
  109. VarLoc.VariableID = insertVariable(Var);
  110. VarLoc.Expr = Expr;
  111. VarLoc.DL = DL;
  112. VarLoc.V = V;
  113. VarLocsBeforeInst[Before].emplace_back(VarLoc);
  114. }
  115. };
  116. void FunctionVarLocs::print(raw_ostream &OS, const Function &Fn) const {
  117. // Print the variable table first. TODO: Sorting by variable could make the
  118. // output more stable?
  119. unsigned Counter = -1;
  120. OS << "=== Variables ===\n";
  121. for (const DebugVariable &V : Variables) {
  122. ++Counter;
  123. // Skip first entry because it is a dummy entry.
  124. if (Counter == 0) {
  125. continue;
  126. }
  127. OS << "[" << Counter << "] " << V.getVariable()->getName();
  128. if (auto F = V.getFragment())
  129. OS << " bits [" << F->OffsetInBits << ", "
  130. << F->OffsetInBits + F->SizeInBits << ")";
  131. if (const auto *IA = V.getInlinedAt())
  132. OS << " inlined-at " << *IA;
  133. OS << "\n";
  134. }
  135. auto PrintLoc = [&OS](const VarLocInfo &Loc) {
  136. OS << "DEF Var=[" << (unsigned)Loc.VariableID << "]"
  137. << " Expr=" << *Loc.Expr << " V=" << *Loc.V << "\n";
  138. };
  139. // Print the single location variables.
  140. OS << "=== Single location vars ===\n";
  141. for (auto It = single_locs_begin(), End = single_locs_end(); It != End;
  142. ++It) {
  143. PrintLoc(*It);
  144. }
  145. // Print the non-single-location defs in line with IR.
  146. OS << "=== In-line variable defs ===";
  147. for (const BasicBlock &BB : Fn) {
  148. OS << "\n" << BB.getName() << ":\n";
  149. for (const Instruction &I : BB) {
  150. for (auto It = locs_begin(&I), End = locs_end(&I); It != End; ++It) {
  151. PrintLoc(*It);
  152. }
  153. OS << I << "\n";
  154. }
  155. }
  156. }
  157. void FunctionVarLocs::init(FunctionVarLocsBuilder &Builder) {
  158. // Add the single-location variables first.
  159. for (const auto &VarLoc : Builder.SingleLocVars)
  160. VarLocRecords.emplace_back(VarLoc);
  161. // Mark the end of the section.
  162. SingleVarLocEnd = VarLocRecords.size();
  163. // Insert a contiguous block of VarLocInfos for each instruction, mapping it
  164. // to the start and end position in the vector with VarLocsBeforeInst.
  165. for (auto &P : Builder.VarLocsBeforeInst) {
  166. unsigned BlockStart = VarLocRecords.size();
  167. for (const VarLocInfo &VarLoc : P.second)
  168. VarLocRecords.emplace_back(VarLoc);
  169. unsigned BlockEnd = VarLocRecords.size();
  170. // Record the start and end indices.
  171. if (BlockEnd != BlockStart)
  172. VarLocsBeforeInst[P.first] = {BlockStart, BlockEnd};
  173. }
  174. // Copy the Variables vector from the builder's UniqueVector.
  175. assert(Variables.empty() && "Expect clear before init");
  176. // UniqueVectors IDs are one-based (which means the VarLocInfo VarID values
  177. // are one-based) so reserve an extra and insert a dummy.
  178. Variables.reserve(Builder.Variables.size() + 1);
  179. Variables.push_back(DebugVariable(nullptr, std::nullopt, nullptr));
  180. Variables.append(Builder.Variables.begin(), Builder.Variables.end());
  181. }
  182. void FunctionVarLocs::clear() {
  183. Variables.clear();
  184. VarLocRecords.clear();
  185. VarLocsBeforeInst.clear();
  186. SingleVarLocEnd = 0;
  187. }
  188. /// Walk backwards along constant GEPs and bitcasts to the base storage from \p
  189. /// Start as far as possible. Prepend \Expression with the offset and append it
  190. /// with a DW_OP_deref that haes been implicit until now. Returns the walked-to
  191. /// value and modified expression.
  192. static std::pair<Value *, DIExpression *>
  193. walkToAllocaAndPrependOffsetDeref(const DataLayout &DL, Value *Start,
  194. DIExpression *Expression) {
  195. APInt OffsetInBytes(DL.getTypeSizeInBits(Start->getType()), false);
  196. Value *End =
  197. Start->stripAndAccumulateInBoundsConstantOffsets(DL, OffsetInBytes);
  198. SmallVector<uint64_t, 3> Ops;
  199. if (OffsetInBytes.getBoolValue()) {
  200. Ops = {dwarf::DW_OP_plus_uconst, OffsetInBytes.getZExtValue()};
  201. Expression = DIExpression::prependOpcodes(
  202. Expression, Ops, /*StackValue=*/false, /*EntryValue=*/false);
  203. }
  204. Expression = DIExpression::append(Expression, {dwarf::DW_OP_deref});
  205. return {End, Expression};
  206. }
  207. /// Extract the offset used in \p DIExpr. Returns std::nullopt if the expression
  208. /// doesn't explicitly describe a memory location with DW_OP_deref or if the
  209. /// expression is too complex to interpret.
  210. static std::optional<int64_t>
  211. getDerefOffsetInBytes(const DIExpression *DIExpr) {
  212. int64_t Offset = 0;
  213. const unsigned NumElements = DIExpr->getNumElements();
  214. const auto Elements = DIExpr->getElements();
  215. unsigned NextElement = 0;
  216. // Extract the offset.
  217. if (NumElements > 2 && Elements[0] == dwarf::DW_OP_plus_uconst) {
  218. Offset = Elements[1];
  219. NextElement = 2;
  220. } else if (NumElements > 3 && Elements[0] == dwarf::DW_OP_constu) {
  221. NextElement = 3;
  222. if (Elements[2] == dwarf::DW_OP_plus)
  223. Offset = Elements[1];
  224. else if (Elements[2] == dwarf::DW_OP_minus)
  225. Offset = -Elements[1];
  226. else
  227. return std::nullopt;
  228. }
  229. // If that's all there is it means there's no deref.
  230. if (NextElement >= NumElements)
  231. return std::nullopt;
  232. // Check the next element is DW_OP_deref - otherwise this is too complex or
  233. // isn't a deref expression.
  234. if (Elements[NextElement] != dwarf::DW_OP_deref)
  235. return std::nullopt;
  236. // Check the final operation is either the DW_OP_deref or is a fragment.
  237. if (NumElements == NextElement + 1)
  238. return Offset; // Ends with deref.
  239. else if (NumElements == NextElement + 3 &&
  240. Elements[NextElement] == dwarf::DW_OP_LLVM_fragment)
  241. return Offset; // Ends with deref + fragment.
  242. // Don't bother trying to interpret anything more complex.
  243. return std::nullopt;
  244. }
  245. /// A whole (unfragmented) source variable.
  246. using DebugAggregate = std::pair<const DILocalVariable *, const DILocation *>;
  247. static DebugAggregate getAggregate(const DbgVariableIntrinsic *DII) {
  248. return DebugAggregate(DII->getVariable(), DII->getDebugLoc().getInlinedAt());
  249. }
  250. static DebugAggregate getAggregate(const DebugVariable &Var) {
  251. return DebugAggregate(Var.getVariable(), Var.getInlinedAt());
  252. }
  253. namespace {
  254. /// In dwarf emission, the following sequence
  255. /// 1. dbg.value ... Fragment(0, 64)
  256. /// 2. dbg.value ... Fragment(0, 32)
  257. /// effectively sets Fragment(32, 32) to undef (each def sets all bits not in
  258. /// the intersection of the fragments to having "no location"). This makes
  259. /// sense for implicit location values because splitting the computed values
  260. /// could be troublesome, and is probably quite uncommon. When we convert
  261. /// dbg.assigns to dbg.value+deref this kind of thing is common, and describing
  262. /// a location (memory) rather than a value means we don't need to worry about
  263. /// splitting any values, so we try to recover the rest of the fragment
  264. /// location here.
  265. /// This class performs a(nother) dataflow analysis over the function, adding
  266. /// variable locations so that any bits of a variable with a memory location
  267. /// have that location explicitly reinstated at each subsequent variable
  268. /// location definition that that doesn't overwrite those bits. i.e. after a
  269. /// variable location def, insert new defs for the memory location with
  270. /// fragments for the difference of "all bits currently in memory" and "the
  271. /// fragment of the second def".
  272. class MemLocFragmentFill {
  273. Function &Fn;
  274. FunctionVarLocsBuilder *FnVarLocs;
  275. const DenseSet<DebugAggregate> *VarsWithStackSlot;
  276. // 0 = no memory location.
  277. using BaseAddress = unsigned;
  278. using OffsetInBitsTy = unsigned;
  279. using FragTraits = IntervalMapHalfOpenInfo<OffsetInBitsTy>;
  280. using FragsInMemMap = IntervalMap<
  281. OffsetInBitsTy, BaseAddress,
  282. IntervalMapImpl::NodeSizer<OffsetInBitsTy, BaseAddress>::LeafSize,
  283. FragTraits>;
  284. FragsInMemMap::Allocator IntervalMapAlloc;
  285. using VarFragMap = DenseMap<unsigned, FragsInMemMap>;
  286. /// IDs for memory location base addresses in maps. Use 0 to indicate that
  287. /// there's no memory location.
  288. UniqueVector<Value *> Bases;
  289. UniqueVector<DebugAggregate> Aggregates;
  290. DenseMap<const BasicBlock *, VarFragMap> LiveIn;
  291. DenseMap<const BasicBlock *, VarFragMap> LiveOut;
  292. struct FragMemLoc {
  293. unsigned Var;
  294. unsigned Base;
  295. unsigned OffsetInBits;
  296. unsigned SizeInBits;
  297. DebugLoc DL;
  298. };
  299. using InsertMap = MapVector<Instruction *, SmallVector<FragMemLoc>>;
  300. /// BBInsertBeforeMap holds a description for the set of location defs to be
  301. /// inserted after the analysis is complete. It is updated during the dataflow
  302. /// and the entry for a block is CLEARED each time it is (re-)visited. After
  303. /// the dataflow is complete, each block entry will contain the set of defs
  304. /// calculated during the final (fixed-point) iteration.
  305. DenseMap<const BasicBlock *, InsertMap> BBInsertBeforeMap;
  306. static bool intervalMapsAreEqual(const FragsInMemMap &A,
  307. const FragsInMemMap &B) {
  308. auto AIt = A.begin(), AEnd = A.end();
  309. auto BIt = B.begin(), BEnd = B.end();
  310. for (; AIt != AEnd; ++AIt, ++BIt) {
  311. if (BIt == BEnd)
  312. return false; // B has fewer elements than A.
  313. if (AIt.start() != BIt.start() || AIt.stop() != BIt.stop())
  314. return false; // Interval is different.
  315. if (*AIt != *BIt)
  316. return false; // Value at interval is different.
  317. }
  318. // AIt == AEnd. Check BIt is also now at end.
  319. return BIt == BEnd;
  320. }
  321. static bool varFragMapsAreEqual(const VarFragMap &A, const VarFragMap &B) {
  322. if (A.size() != B.size())
  323. return false;
  324. for (const auto &APair : A) {
  325. auto BIt = B.find(APair.first);
  326. if (BIt == B.end())
  327. return false;
  328. if (!intervalMapsAreEqual(APair.second, BIt->second))
  329. return false;
  330. }
  331. return true;
  332. }
  333. /// Return a string for the value that \p BaseID represents.
  334. std::string toString(unsigned BaseID) {
  335. if (BaseID)
  336. return Bases[BaseID]->getName().str();
  337. else
  338. return "None";
  339. }
  340. /// Format string describing an FragsInMemMap (IntervalMap) interval.
  341. std::string toString(FragsInMemMap::const_iterator It, bool Newline = true) {
  342. std::string String;
  343. std::stringstream S(String);
  344. if (It.valid()) {
  345. S << "[" << It.start() << ", " << It.stop()
  346. << "): " << toString(It.value());
  347. } else {
  348. S << "invalid iterator (end)";
  349. }
  350. if (Newline)
  351. S << "\n";
  352. return S.str();
  353. };
  354. FragsInMemMap meetFragments(const FragsInMemMap &A, const FragsInMemMap &B) {
  355. FragsInMemMap Result(IntervalMapAlloc);
  356. for (auto AIt = A.begin(), AEnd = A.end(); AIt != AEnd; ++AIt) {
  357. LLVM_DEBUG(dbgs() << "a " << toString(AIt));
  358. // This is basically copied from process() and inverted (process is
  359. // performing something like a union whereas this is more of an
  360. // intersect).
  361. // There's no work to do if interval `a` overlaps no fragments in map `B`.
  362. if (!B.overlaps(AIt.start(), AIt.stop()))
  363. continue;
  364. // Does StartBit intersect an existing fragment?
  365. auto FirstOverlap = B.find(AIt.start());
  366. assert(FirstOverlap != B.end());
  367. bool IntersectStart = FirstOverlap.start() < AIt.start();
  368. LLVM_DEBUG(dbgs() << "- FirstOverlap " << toString(FirstOverlap, false)
  369. << ", IntersectStart: " << IntersectStart << "\n");
  370. // Does EndBit intersect an existing fragment?
  371. auto LastOverlap = B.find(AIt.stop());
  372. bool IntersectEnd =
  373. LastOverlap != B.end() && LastOverlap.start() < AIt.stop();
  374. LLVM_DEBUG(dbgs() << "- LastOverlap " << toString(LastOverlap, false)
  375. << ", IntersectEnd: " << IntersectEnd << "\n");
  376. // Check if both ends of `a` intersect the same interval `b`.
  377. if (IntersectStart && IntersectEnd && FirstOverlap == LastOverlap) {
  378. // Insert `a` (`a` is contained in `b`) if the values match.
  379. // [ a ]
  380. // [ - b - ]
  381. // -
  382. // [ r ]
  383. LLVM_DEBUG(dbgs() << "- a is contained within "
  384. << toString(FirstOverlap));
  385. if (*AIt && *AIt == *FirstOverlap)
  386. Result.insert(AIt.start(), AIt.stop(), *AIt);
  387. } else {
  388. // There's an overlap but `a` is not fully contained within
  389. // `b`. Shorten any end-point intersections.
  390. // [ - a - ]
  391. // [ - b - ]
  392. // -
  393. // [ r ]
  394. auto Next = FirstOverlap;
  395. if (IntersectStart) {
  396. LLVM_DEBUG(dbgs() << "- insert intersection of a and "
  397. << toString(FirstOverlap));
  398. if (*AIt && *AIt == *FirstOverlap)
  399. Result.insert(AIt.start(), FirstOverlap.stop(), *AIt);
  400. ++Next;
  401. }
  402. // [ - a - ]
  403. // [ - b - ]
  404. // -
  405. // [ r ]
  406. if (IntersectEnd) {
  407. LLVM_DEBUG(dbgs() << "- insert intersection of a and "
  408. << toString(LastOverlap));
  409. if (*AIt && *AIt == *LastOverlap)
  410. Result.insert(LastOverlap.start(), AIt.stop(), *AIt);
  411. }
  412. // Insert all intervals in map `B` that are contained within interval
  413. // `a` where the values match.
  414. // [ - - a - - ]
  415. // [ b1 ] [ b2 ]
  416. // -
  417. // [ r1 ] [ r2 ]
  418. while (Next != B.end() && Next.start() < AIt.stop() &&
  419. Next.stop() <= AIt.stop()) {
  420. LLVM_DEBUG(dbgs()
  421. << "- insert intersection of a and " << toString(Next));
  422. if (*AIt && *AIt == *Next)
  423. Result.insert(Next.start(), Next.stop(), *Next);
  424. ++Next;
  425. }
  426. }
  427. }
  428. return Result;
  429. }
  430. /// Meet \p A and \p B, storing the result in \p A.
  431. void meetVars(VarFragMap &A, const VarFragMap &B) {
  432. // Meet A and B.
  433. //
  434. // Result = meet(a, b) for a in A, b in B where Var(a) == Var(b)
  435. for (auto It = A.begin(), End = A.end(); It != End; ++It) {
  436. unsigned AVar = It->first;
  437. FragsInMemMap &AFrags = It->second;
  438. auto BIt = B.find(AVar);
  439. if (BIt == B.end()) {
  440. A.erase(It);
  441. continue; // Var has no bits defined in B.
  442. }
  443. LLVM_DEBUG(dbgs() << "meet fragment maps for "
  444. << Aggregates[AVar].first->getName() << "\n");
  445. AFrags = meetFragments(AFrags, BIt->second);
  446. }
  447. }
  448. bool meet(const BasicBlock &BB,
  449. const SmallPtrSet<BasicBlock *, 16> &Visited) {
  450. LLVM_DEBUG(dbgs() << "meet block info from preds of " << BB.getName()
  451. << "\n");
  452. VarFragMap BBLiveIn;
  453. bool FirstMeet = true;
  454. // LiveIn locs for BB is the meet of the already-processed preds' LiveOut
  455. // locs.
  456. for (auto I = pred_begin(&BB), E = pred_end(&BB); I != E; I++) {
  457. // Ignore preds that haven't been processed yet. This is essentially the
  458. // same as initialising all variables to implicit top value (⊤) which is
  459. // the identity value for the meet operation.
  460. const BasicBlock *Pred = *I;
  461. if (!Visited.count(Pred))
  462. continue;
  463. auto PredLiveOut = LiveOut.find(Pred);
  464. assert(PredLiveOut != LiveOut.end());
  465. if (FirstMeet) {
  466. LLVM_DEBUG(dbgs() << "BBLiveIn = " << Pred->getName() << "\n");
  467. BBLiveIn = PredLiveOut->second;
  468. FirstMeet = false;
  469. } else {
  470. LLVM_DEBUG(dbgs() << "BBLiveIn = meet BBLiveIn, " << Pred->getName()
  471. << "\n");
  472. meetVars(BBLiveIn, PredLiveOut->second);
  473. }
  474. // An empty set is ⊥ for the intersect-like meet operation. If we've
  475. // already got ⊥ there's no need to run the code - we know the result is
  476. // ⊥ since `meet(a, ⊥) = ⊥`.
  477. if (BBLiveIn.size() == 0)
  478. break;
  479. }
  480. auto CurrentLiveInEntry = LiveIn.find(&BB);
  481. // If there's no LiveIn entry for the block yet, add it.
  482. if (CurrentLiveInEntry == LiveIn.end()) {
  483. LLVM_DEBUG(dbgs() << "change=true (first) on meet on " << BB.getName()
  484. << "\n");
  485. LiveIn[&BB] = std::move(BBLiveIn);
  486. return /*Changed=*/true;
  487. }
  488. // If the LiveIn set has changed (expensive check) update it and return
  489. // true.
  490. if (!varFragMapsAreEqual(BBLiveIn, CurrentLiveInEntry->second)) {
  491. LLVM_DEBUG(dbgs() << "change=true on meet on " << BB.getName() << "\n");
  492. CurrentLiveInEntry->second = std::move(BBLiveIn);
  493. return /*Changed=*/true;
  494. }
  495. LLVM_DEBUG(dbgs() << "change=false on meet on " << BB.getName() << "\n");
  496. return /*Changed=*/false;
  497. }
  498. void insertMemLoc(BasicBlock &BB, Instruction &Before, unsigned Var,
  499. unsigned StartBit, unsigned EndBit, unsigned Base,
  500. DebugLoc DL) {
  501. assert(StartBit < EndBit && "Cannot create fragment of size <= 0");
  502. if (!Base)
  503. return;
  504. FragMemLoc Loc;
  505. Loc.Var = Var;
  506. Loc.OffsetInBits = StartBit;
  507. Loc.SizeInBits = EndBit - StartBit;
  508. assert(Base && "Expected a non-zero ID for Base address");
  509. Loc.Base = Base;
  510. Loc.DL = DL;
  511. BBInsertBeforeMap[&BB][&Before].push_back(Loc);
  512. LLVM_DEBUG(dbgs() << "Add mem def for " << Aggregates[Var].first->getName()
  513. << " bits [" << StartBit << ", " << EndBit << ")\n");
  514. }
  515. void addDef(const VarLocInfo &VarLoc, Instruction &Before, BasicBlock &BB,
  516. VarFragMap &LiveSet) {
  517. DebugVariable DbgVar = FnVarLocs->getVariable(VarLoc.VariableID);
  518. if (skipVariable(DbgVar.getVariable()))
  519. return;
  520. // Don't bother doing anything for this variables if we know it's fully
  521. // promoted. We're only interested in variables that (sometimes) live on
  522. // the stack here.
  523. if (!VarsWithStackSlot->count(getAggregate(DbgVar)))
  524. return;
  525. unsigned Var = Aggregates.insert(
  526. DebugAggregate(DbgVar.getVariable(), VarLoc.DL.getInlinedAt()));
  527. // [StartBit: EndBit) are the bits affected by this def.
  528. const DIExpression *DIExpr = VarLoc.Expr;
  529. unsigned StartBit;
  530. unsigned EndBit;
  531. if (auto Frag = DIExpr->getFragmentInfo()) {
  532. StartBit = Frag->OffsetInBits;
  533. EndBit = StartBit + Frag->SizeInBits;
  534. } else {
  535. assert(static_cast<bool>(DbgVar.getVariable()->getSizeInBits()));
  536. StartBit = 0;
  537. EndBit = *DbgVar.getVariable()->getSizeInBits();
  538. }
  539. // We will only fill fragments for simple memory-describing dbg.value
  540. // intrinsics. If the fragment offset is the same as the offset from the
  541. // base pointer, do The Thing, otherwise fall back to normal dbg.value
  542. // behaviour. AssignmentTrackingLowering has generated DIExpressions
  543. // written in terms of the base pointer.
  544. // TODO: Remove this condition since the fragment offset doesn't always
  545. // equal the offset from base pointer (e.g. for a SROA-split variable).
  546. const auto DerefOffsetInBytes = getDerefOffsetInBytes(DIExpr);
  547. const unsigned Base =
  548. DerefOffsetInBytes && *DerefOffsetInBytes * 8 == StartBit
  549. ? Bases.insert(VarLoc.V)
  550. : 0;
  551. LLVM_DEBUG(dbgs() << "DEF " << DbgVar.getVariable()->getName() << " ["
  552. << StartBit << ", " << EndBit << "): " << toString(Base)
  553. << "\n");
  554. // First of all, any locs that use mem that are disrupted need reinstating.
  555. // Unfortunately, IntervalMap doesn't let us insert intervals that overlap
  556. // with existing intervals so this code involves a lot of fiddling around
  557. // with intervals to do that manually.
  558. auto FragIt = LiveSet.find(Var);
  559. // Check if the variable does not exist in the map.
  560. if (FragIt == LiveSet.end()) {
  561. // Add this variable to the BB map.
  562. auto P = LiveSet.try_emplace(Var, FragsInMemMap(IntervalMapAlloc));
  563. assert(P.second && "Var already in map?");
  564. // Add the interval to the fragment map.
  565. P.first->second.insert(StartBit, EndBit, Base);
  566. return;
  567. }
  568. // The variable has an entry in the map.
  569. FragsInMemMap &FragMap = FragIt->second;
  570. // First check the easy case: the new fragment `f` doesn't overlap with any
  571. // intervals.
  572. if (!FragMap.overlaps(StartBit, EndBit)) {
  573. LLVM_DEBUG(dbgs() << "- No overlaps\n");
  574. FragMap.insert(StartBit, EndBit, Base);
  575. return;
  576. }
  577. // There is at least one overlap.
  578. // Does StartBit intersect an existing fragment?
  579. auto FirstOverlap = FragMap.find(StartBit);
  580. assert(FirstOverlap != FragMap.end());
  581. bool IntersectStart = FirstOverlap.start() < StartBit;
  582. // Does EndBit intersect an existing fragment?
  583. auto LastOverlap = FragMap.find(EndBit);
  584. bool IntersectEnd = LastOverlap.valid() && LastOverlap.start() < EndBit;
  585. // Check if both ends of `f` intersect the same interval `i`.
  586. if (IntersectStart && IntersectEnd && FirstOverlap == LastOverlap) {
  587. LLVM_DEBUG(dbgs() << "- Intersect single interval @ both ends\n");
  588. // Shorten `i` so that there's space to insert `f`.
  589. // [ f ]
  590. // [ - i - ]
  591. // +
  592. // [ i ][ f ][ i ]
  593. // Save values for use after inserting a new interval.
  594. auto EndBitOfOverlap = FirstOverlap.stop();
  595. unsigned OverlapValue = FirstOverlap.value();
  596. // Shorten the overlapping interval.
  597. FirstOverlap.setStop(StartBit);
  598. insertMemLoc(BB, Before, Var, FirstOverlap.start(), StartBit,
  599. OverlapValue, VarLoc.DL);
  600. // Insert a new interval to represent the end part.
  601. FragMap.insert(EndBit, EndBitOfOverlap, OverlapValue);
  602. insertMemLoc(BB, Before, Var, EndBit, EndBitOfOverlap, OverlapValue,
  603. VarLoc.DL);
  604. // Insert the new (middle) fragment now there is space.
  605. FragMap.insert(StartBit, EndBit, Base);
  606. } else {
  607. // There's an overlap but `f` may not be fully contained within
  608. // `i`. Shorten any end-point intersections so that we can then
  609. // insert `f`.
  610. // [ - f - ]
  611. // [ - i - ]
  612. // | |
  613. // [ i ]
  614. // Shorten any end-point intersections.
  615. if (IntersectStart) {
  616. LLVM_DEBUG(dbgs() << "- Intersect interval at start\n");
  617. // Split off at the intersection.
  618. FirstOverlap.setStop(StartBit);
  619. insertMemLoc(BB, Before, Var, FirstOverlap.start(), StartBit,
  620. *FirstOverlap, VarLoc.DL);
  621. }
  622. // [ - f - ]
  623. // [ - i - ]
  624. // | |
  625. // [ i ]
  626. if (IntersectEnd) {
  627. LLVM_DEBUG(dbgs() << "- Intersect interval at end\n");
  628. // Split off at the intersection.
  629. LastOverlap.setStart(EndBit);
  630. insertMemLoc(BB, Before, Var, EndBit, LastOverlap.stop(), *LastOverlap,
  631. VarLoc.DL);
  632. }
  633. LLVM_DEBUG(dbgs() << "- Erase intervals contained within\n");
  634. // FirstOverlap and LastOverlap have been shortened such that they're
  635. // no longer overlapping with [StartBit, EndBit). Delete any overlaps
  636. // that remain (these will be fully contained within `f`).
  637. // [ - f - ] }
  638. // [ - i - ] } Intersection shortening that has happened above.
  639. // | | }
  640. // [ i ] }
  641. // -----------------
  642. // [i2 ] } Intervals fully contained within `f` get erased.
  643. // -----------------
  644. // [ - f - ][ i ] } Completed insertion.
  645. auto It = FirstOverlap;
  646. if (IntersectStart)
  647. ++It; // IntersectStart: first overlap has been shortened.
  648. while (It.valid() && It.start() >= StartBit && It.stop() <= EndBit) {
  649. LLVM_DEBUG(dbgs() << "- Erase " << toString(It));
  650. It.erase(); // This increments It after removing the interval.
  651. }
  652. // We've dealt with all the overlaps now!
  653. assert(!FragMap.overlaps(StartBit, EndBit));
  654. LLVM_DEBUG(dbgs() << "- Insert DEF into now-empty space\n");
  655. FragMap.insert(StartBit, EndBit, Base);
  656. }
  657. }
  658. bool skipVariable(const DILocalVariable *V) { return !V->getSizeInBits(); }
  659. void process(BasicBlock &BB, VarFragMap &LiveSet) {
  660. BBInsertBeforeMap[&BB].clear();
  661. for (auto &I : BB) {
  662. if (const auto *Locs = FnVarLocs->getWedge(&I)) {
  663. for (const VarLocInfo &Loc : *Locs) {
  664. addDef(Loc, I, *I.getParent(), LiveSet);
  665. }
  666. }
  667. }
  668. }
  669. public:
  670. MemLocFragmentFill(Function &Fn,
  671. const DenseSet<DebugAggregate> *VarsWithStackSlot)
  672. : Fn(Fn), VarsWithStackSlot(VarsWithStackSlot) {}
  673. /// Add variable locations to \p FnVarLocs so that any bits of a variable
  674. /// with a memory location have that location explicitly reinstated at each
  675. /// subsequent variable location definition that that doesn't overwrite those
  676. /// bits. i.e. after a variable location def, insert new defs for the memory
  677. /// location with fragments for the difference of "all bits currently in
  678. /// memory" and "the fragment of the second def". e.g.
  679. ///
  680. /// Before:
  681. ///
  682. /// var x bits 0 to 63: value in memory
  683. /// more instructions
  684. /// var x bits 0 to 31: value is %0
  685. ///
  686. /// After:
  687. ///
  688. /// var x bits 0 to 63: value in memory
  689. /// more instructions
  690. /// var x bits 0 to 31: value is %0
  691. /// var x bits 32 to 61: value in memory ; <-- new loc def
  692. ///
  693. void run(FunctionVarLocsBuilder *FnVarLocs) {
  694. if (!EnableMemLocFragFill)
  695. return;
  696. this->FnVarLocs = FnVarLocs;
  697. // Prepare for traversal.
  698. //
  699. ReversePostOrderTraversal<Function *> RPOT(&Fn);
  700. std::priority_queue<unsigned int, std::vector<unsigned int>,
  701. std::greater<unsigned int>>
  702. Worklist;
  703. std::priority_queue<unsigned int, std::vector<unsigned int>,
  704. std::greater<unsigned int>>
  705. Pending;
  706. DenseMap<unsigned int, BasicBlock *> OrderToBB;
  707. DenseMap<BasicBlock *, unsigned int> BBToOrder;
  708. { // Init OrderToBB and BBToOrder.
  709. unsigned int RPONumber = 0;
  710. for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) {
  711. OrderToBB[RPONumber] = *RI;
  712. BBToOrder[*RI] = RPONumber;
  713. Worklist.push(RPONumber);
  714. ++RPONumber;
  715. }
  716. LiveIn.init(RPONumber);
  717. LiveOut.init(RPONumber);
  718. }
  719. // Perform the traversal.
  720. //
  721. // This is a standard "intersect of predecessor outs" dataflow problem. To
  722. // solve it, we perform meet() and process() using the two worklist method
  723. // until the LiveIn data for each block becomes unchanging.
  724. //
  725. // This dataflow is essentially working on maps of sets and at each meet we
  726. // intersect the maps and the mapped sets. So, initialized live-in maps
  727. // monotonically decrease in value throughout the dataflow.
  728. SmallPtrSet<BasicBlock *, 16> Visited;
  729. while (!Worklist.empty() || !Pending.empty()) {
  730. // We track what is on the pending worklist to avoid inserting the same
  731. // thing twice. We could avoid this with a custom priority queue, but
  732. // this is probably not worth it.
  733. SmallPtrSet<BasicBlock *, 16> OnPending;
  734. LLVM_DEBUG(dbgs() << "Processing Worklist\n");
  735. while (!Worklist.empty()) {
  736. BasicBlock *BB = OrderToBB[Worklist.top()];
  737. LLVM_DEBUG(dbgs() << "\nPop BB " << BB->getName() << "\n");
  738. Worklist.pop();
  739. bool InChanged = meet(*BB, Visited);
  740. // Always consider LiveIn changed on the first visit.
  741. InChanged |= Visited.insert(BB).second;
  742. if (InChanged) {
  743. LLVM_DEBUG(dbgs()
  744. << BB->getName() << " has new InLocs, process it\n");
  745. // Mutate a copy of LiveIn while processing BB. Once we've processed
  746. // the terminator LiveSet is the LiveOut set for BB.
  747. // This is an expensive copy!
  748. VarFragMap LiveSet = LiveIn[BB];
  749. // Process the instructions in the block.
  750. process(*BB, LiveSet);
  751. // Relatively expensive check: has anything changed in LiveOut for BB?
  752. if (!varFragMapsAreEqual(LiveOut[BB], LiveSet)) {
  753. LLVM_DEBUG(dbgs() << BB->getName()
  754. << " has new OutLocs, add succs to worklist: [ ");
  755. LiveOut[BB] = std::move(LiveSet);
  756. for (auto I = succ_begin(BB), E = succ_end(BB); I != E; I++) {
  757. if (OnPending.insert(*I).second) {
  758. LLVM_DEBUG(dbgs() << I->getName() << " ");
  759. Pending.push(BBToOrder[*I]);
  760. }
  761. }
  762. LLVM_DEBUG(dbgs() << "]\n");
  763. }
  764. }
  765. }
  766. Worklist.swap(Pending);
  767. // At this point, pending must be empty, since it was just the empty
  768. // worklist
  769. assert(Pending.empty() && "Pending should be empty");
  770. }
  771. // Insert new location defs.
  772. for (auto Pair : BBInsertBeforeMap) {
  773. InsertMap &Map = Pair.second;
  774. for (auto Pair : Map) {
  775. Instruction *InsertBefore = Pair.first;
  776. assert(InsertBefore && "should never be null");
  777. auto FragMemLocs = Pair.second;
  778. auto &Ctx = Fn.getContext();
  779. for (auto FragMemLoc : FragMemLocs) {
  780. DIExpression *Expr = DIExpression::get(Ctx, std::nullopt);
  781. Expr = *DIExpression::createFragmentExpression(
  782. Expr, FragMemLoc.OffsetInBits, FragMemLoc.SizeInBits);
  783. Expr = DIExpression::prepend(Expr, DIExpression::DerefAfter,
  784. FragMemLoc.OffsetInBits / 8);
  785. DebugVariable Var(Aggregates[FragMemLoc.Var].first, Expr,
  786. FragMemLoc.DL.getInlinedAt());
  787. FnVarLocs->addVarLoc(InsertBefore, Var, Expr, FragMemLoc.DL,
  788. Bases[FragMemLoc.Base]);
  789. }
  790. }
  791. }
  792. }
  793. };
  794. /// AssignmentTrackingLowering encapsulates a dataflow analysis over a function
  795. /// that interprets assignment tracking debug info metadata and stores in IR to
  796. /// create a map of variable locations.
  797. class AssignmentTrackingLowering {
  798. public:
  799. /// The kind of location in use for a variable, where Mem is the stack home,
  800. /// Val is an SSA value or const, and None means that there is not one single
  801. /// kind (either because there are multiple or because there is none; it may
  802. /// prove useful to split this into two values in the future).
  803. ///
  804. /// LocKind is a join-semilattice with the partial order:
  805. /// None > Mem, Val
  806. ///
  807. /// i.e.
  808. /// join(Mem, Mem) = Mem
  809. /// join(Val, Val) = Val
  810. /// join(Mem, Val) = None
  811. /// join(None, Mem) = None
  812. /// join(None, Val) = None
  813. /// join(None, None) = None
  814. ///
  815. /// Note: the order is not `None > Val > Mem` because we're using DIAssignID
  816. /// to name assignments and are not tracking the actual stored values.
  817. /// Therefore currently there's no way to ensure that Mem values and Val
  818. /// values are the same. This could be a future extension, though it's not
  819. /// clear that many additional locations would be recovered that way in
  820. /// practice as the likelihood of this sitation arising naturally seems
  821. /// incredibly low.
  822. enum class LocKind { Mem, Val, None };
  823. /// An abstraction of the assignment of a value to a variable or memory
  824. /// location.
  825. ///
  826. /// An Assignment is Known or NoneOrPhi. A Known Assignment means we have a
  827. /// DIAssignID ptr that represents it. NoneOrPhi means that we don't (or
  828. /// can't) know the ID of the last assignment that took place.
  829. ///
  830. /// The Status of the Assignment (Known or NoneOrPhi) is another
  831. /// join-semilattice. The partial order is:
  832. /// NoneOrPhi > Known {id_0, id_1, ...id_N}
  833. ///
  834. /// i.e. for all values x and y where x != y:
  835. /// join(x, x) = x
  836. /// join(x, y) = NoneOrPhi
  837. struct Assignment {
  838. enum S { Known, NoneOrPhi } Status;
  839. /// ID of the assignment. nullptr if Status is not Known.
  840. DIAssignID *ID;
  841. /// The dbg.assign that marks this dbg-def. Mem-defs don't use this field.
  842. /// May be nullptr.
  843. DbgAssignIntrinsic *Source;
  844. bool isSameSourceAssignment(const Assignment &Other) const {
  845. // Don't include Source in the equality check. Assignments are
  846. // defined by their ID, not debug intrinsic(s).
  847. return std::tie(Status, ID) == std::tie(Other.Status, Other.ID);
  848. }
  849. void dump(raw_ostream &OS) {
  850. static const char *LUT[] = {"Known", "NoneOrPhi"};
  851. OS << LUT[Status] << "(id=";
  852. if (ID)
  853. OS << ID;
  854. else
  855. OS << "null";
  856. OS << ", s=";
  857. if (Source)
  858. OS << *Source;
  859. else
  860. OS << "null";
  861. OS << ")";
  862. }
  863. static Assignment make(DIAssignID *ID, DbgAssignIntrinsic *Source) {
  864. return Assignment(Known, ID, Source);
  865. }
  866. static Assignment makeFromMemDef(DIAssignID *ID) {
  867. return Assignment(Known, ID, nullptr);
  868. }
  869. static Assignment makeNoneOrPhi() {
  870. return Assignment(NoneOrPhi, nullptr, nullptr);
  871. }
  872. // Again, need a Top value?
  873. Assignment()
  874. : Status(NoneOrPhi), ID(nullptr), Source(nullptr) {
  875. } // Can we delete this?
  876. Assignment(S Status, DIAssignID *ID, DbgAssignIntrinsic *Source)
  877. : Status(Status), ID(ID), Source(Source) {
  878. // If the Status is Known then we expect there to be an assignment ID.
  879. assert(Status == NoneOrPhi || ID);
  880. }
  881. };
  882. using AssignmentMap = DenseMap<VariableID, Assignment>;
  883. using LocMap = DenseMap<VariableID, LocKind>;
  884. using OverlapMap = DenseMap<VariableID, SmallVector<VariableID, 4>>;
  885. using UntaggedStoreAssignmentMap =
  886. DenseMap<const Instruction *,
  887. SmallVector<std::pair<VariableID, at::AssignmentInfo>>>;
  888. private:
  889. /// Map a variable to the set of variables that it fully contains.
  890. OverlapMap VarContains;
  891. /// Map untagged stores to the variable fragments they assign to. Used by
  892. /// processUntaggedInstruction.
  893. UntaggedStoreAssignmentMap UntaggedStoreVars;
  894. // Machinery to defer inserting dbg.values.
  895. using InsertMap = MapVector<Instruction *, SmallVector<VarLocInfo>>;
  896. InsertMap InsertBeforeMap;
  897. /// Clear the location definitions currently cached for insertion after /p
  898. /// After.
  899. void resetInsertionPoint(Instruction &After);
  900. void emitDbgValue(LocKind Kind, const DbgVariableIntrinsic *Source,
  901. Instruction *After);
  902. static bool mapsAreEqual(const AssignmentMap &A, const AssignmentMap &B) {
  903. if (A.size() != B.size())
  904. return false;
  905. for (const auto &Pair : A) {
  906. VariableID Var = Pair.first;
  907. const Assignment &AV = Pair.second;
  908. auto R = B.find(Var);
  909. // Check if this entry exists in B, otherwise ret false.
  910. if (R == B.end())
  911. return false;
  912. // Check that the assignment value is the same.
  913. if (!AV.isSameSourceAssignment(R->second))
  914. return false;
  915. }
  916. return true;
  917. }
  918. /// Represents the stack and debug assignments in a block. Used to describe
  919. /// the live-in and live-out values for blocks, as well as the "current"
  920. /// value as we process each instruction in a block.
  921. struct BlockInfo {
  922. /// Dominating assignment to memory for each variable.
  923. AssignmentMap StackHomeValue;
  924. /// Dominating assignemnt to each variable.
  925. AssignmentMap DebugValue;
  926. /// Location kind for each variable. LiveLoc indicates whether the
  927. /// dominating assignment in StackHomeValue (LocKind::Mem), DebugValue
  928. /// (LocKind::Val), or neither (LocKind::None) is valid, in that order of
  929. /// preference. This cannot be derived by inspecting DebugValue and
  930. /// StackHomeValue due to the fact that there's no distinction in
  931. /// Assignment (the class) between whether an assignment is unknown or a
  932. /// merge of multiple assignments (both are Status::NoneOrPhi). In other
  933. /// words, the memory location may well be valid while both DebugValue and
  934. /// StackHomeValue contain Assignments that have a Status of NoneOrPhi.
  935. LocMap LiveLoc;
  936. /// Compare every element in each map to determine structural equality
  937. /// (slow).
  938. bool operator==(const BlockInfo &Other) const {
  939. return LiveLoc == Other.LiveLoc &&
  940. mapsAreEqual(StackHomeValue, Other.StackHomeValue) &&
  941. mapsAreEqual(DebugValue, Other.DebugValue);
  942. }
  943. bool operator!=(const BlockInfo &Other) const { return !(*this == Other); }
  944. bool isValid() {
  945. return LiveLoc.size() == DebugValue.size() &&
  946. LiveLoc.size() == StackHomeValue.size();
  947. }
  948. };
  949. Function &Fn;
  950. const DataLayout &Layout;
  951. const DenseSet<DebugAggregate> *VarsWithStackSlot;
  952. FunctionVarLocsBuilder *FnVarLocs;
  953. DenseMap<const BasicBlock *, BlockInfo> LiveIn;
  954. DenseMap<const BasicBlock *, BlockInfo> LiveOut;
  955. /// Helper for process methods to track variables touched each frame.
  956. DenseSet<VariableID> VarsTouchedThisFrame;
  957. /// The set of variables that sometimes are not located in their stack home.
  958. DenseSet<DebugAggregate> NotAlwaysStackHomed;
  959. VariableID getVariableID(const DebugVariable &Var) {
  960. return static_cast<VariableID>(FnVarLocs->insertVariable(Var));
  961. }
  962. /// Join the LiveOut values of preds that are contained in \p Visited into
  963. /// LiveIn[BB]. Return True if LiveIn[BB] has changed as a result. LiveIn[BB]
  964. /// values monotonically increase. See the @link joinMethods join methods
  965. /// @endlink documentation for more info.
  966. bool join(const BasicBlock &BB, const SmallPtrSet<BasicBlock *, 16> &Visited);
  967. ///@name joinMethods
  968. /// Functions that implement `join` (the least upper bound) for the
  969. /// join-semilattice types used in the dataflow. There is an explicit bottom
  970. /// value (⊥) for some types and and explicit top value (⊤) for all types.
  971. /// By definition:
  972. ///
  973. /// Join(A, B) >= A && Join(A, B) >= B
  974. /// Join(A, ⊥) = A
  975. /// Join(A, ⊤) = ⊤
  976. ///
  977. /// These invariants are important for monotonicity.
  978. ///
  979. /// For the map-type functions, all unmapped keys in an empty map are
  980. /// associated with a bottom value (⊥). This represents their values being
  981. /// unknown. Unmapped keys in non-empty maps (joining two maps with a key
  982. /// only present in one) represents either a variable going out of scope or
  983. /// dropped debug info. It is assumed the key is associated with a top value
  984. /// (⊤) in this case (unknown location / assignment).
  985. ///@{
  986. static LocKind joinKind(LocKind A, LocKind B);
  987. static LocMap joinLocMap(const LocMap &A, const LocMap &B);
  988. static Assignment joinAssignment(const Assignment &A, const Assignment &B);
  989. static AssignmentMap joinAssignmentMap(const AssignmentMap &A,
  990. const AssignmentMap &B);
  991. static BlockInfo joinBlockInfo(const BlockInfo &A, const BlockInfo &B);
  992. ///@}
  993. /// Process the instructions in \p BB updating \p LiveSet along the way. \p
  994. /// LiveSet must be initialized with the current live-in locations before
  995. /// calling this.
  996. void process(BasicBlock &BB, BlockInfo *LiveSet);
  997. ///@name processMethods
  998. /// Methods to process instructions in order to update the LiveSet (current
  999. /// location information).
  1000. ///@{
  1001. void processNonDbgInstruction(Instruction &I, BlockInfo *LiveSet);
  1002. void processDbgInstruction(Instruction &I, BlockInfo *LiveSet);
  1003. /// Update \p LiveSet after encountering an instruction with a DIAssignID
  1004. /// attachment, \p I.
  1005. void processTaggedInstruction(Instruction &I, BlockInfo *LiveSet);
  1006. /// Update \p LiveSet after encountering an instruciton without a DIAssignID
  1007. /// attachment, \p I.
  1008. void processUntaggedInstruction(Instruction &I, BlockInfo *LiveSet);
  1009. void processDbgAssign(DbgAssignIntrinsic &DAI, BlockInfo *LiveSet);
  1010. void processDbgValue(DbgValueInst &DVI, BlockInfo *LiveSet);
  1011. /// Add an assignment to memory for the variable /p Var.
  1012. void addMemDef(BlockInfo *LiveSet, VariableID Var, const Assignment &AV);
  1013. /// Add an assignment to the variable /p Var.
  1014. void addDbgDef(BlockInfo *LiveSet, VariableID Var, const Assignment &AV);
  1015. ///@}
  1016. /// Set the LocKind for \p Var.
  1017. void setLocKind(BlockInfo *LiveSet, VariableID Var, LocKind K);
  1018. /// Get the live LocKind for a \p Var. Requires addMemDef or addDbgDef to
  1019. /// have been called for \p Var first.
  1020. LocKind getLocKind(BlockInfo *LiveSet, VariableID Var);
  1021. /// Return true if \p Var has an assignment in \p M matching \p AV.
  1022. bool hasVarWithAssignment(VariableID Var, const Assignment &AV,
  1023. const AssignmentMap &M);
  1024. /// Emit info for variables that are fully promoted.
  1025. bool emitPromotedVarLocs(FunctionVarLocsBuilder *FnVarLocs);
  1026. public:
  1027. AssignmentTrackingLowering(Function &Fn, const DataLayout &Layout,
  1028. const DenseSet<DebugAggregate> *VarsWithStackSlot)
  1029. : Fn(Fn), Layout(Layout), VarsWithStackSlot(VarsWithStackSlot) {}
  1030. /// Run the analysis, adding variable location info to \p FnVarLocs. Returns
  1031. /// true if any variable locations have been added to FnVarLocs.
  1032. bool run(FunctionVarLocsBuilder *FnVarLocs);
  1033. };
  1034. } // namespace
  1035. void AssignmentTrackingLowering::setLocKind(BlockInfo *LiveSet, VariableID Var,
  1036. LocKind K) {
  1037. auto SetKind = [this](BlockInfo *LiveSet, VariableID Var, LocKind K) {
  1038. VarsTouchedThisFrame.insert(Var);
  1039. LiveSet->LiveLoc[Var] = K;
  1040. };
  1041. SetKind(LiveSet, Var, K);
  1042. // Update the LocKind for all fragments contained within Var.
  1043. for (VariableID Frag : VarContains[Var])
  1044. SetKind(LiveSet, Frag, K);
  1045. }
  1046. AssignmentTrackingLowering::LocKind
  1047. AssignmentTrackingLowering::getLocKind(BlockInfo *LiveSet, VariableID Var) {
  1048. auto Pair = LiveSet->LiveLoc.find(Var);
  1049. assert(Pair != LiveSet->LiveLoc.end());
  1050. return Pair->second;
  1051. }
  1052. void AssignmentTrackingLowering::addMemDef(BlockInfo *LiveSet, VariableID Var,
  1053. const Assignment &AV) {
  1054. auto AddDef = [](BlockInfo *LiveSet, VariableID Var, Assignment AV) {
  1055. LiveSet->StackHomeValue[Var] = AV;
  1056. // Add default (Var -> ⊤) to DebugValue if Var isn't in DebugValue yet.
  1057. LiveSet->DebugValue.insert({Var, Assignment::makeNoneOrPhi()});
  1058. // Add default (Var -> ⊤) to LiveLocs if Var isn't in LiveLocs yet. Callers
  1059. // of addMemDef will call setLocKind to override.
  1060. LiveSet->LiveLoc.insert({Var, LocKind::None});
  1061. };
  1062. AddDef(LiveSet, Var, AV);
  1063. // Use this assigment for all fragments contained within Var, but do not
  1064. // provide a Source because we cannot convert Var's value to a value for the
  1065. // fragment.
  1066. Assignment FragAV = AV;
  1067. FragAV.Source = nullptr;
  1068. for (VariableID Frag : VarContains[Var])
  1069. AddDef(LiveSet, Frag, FragAV);
  1070. }
  1071. void AssignmentTrackingLowering::addDbgDef(BlockInfo *LiveSet, VariableID Var,
  1072. const Assignment &AV) {
  1073. auto AddDef = [](BlockInfo *LiveSet, VariableID Var, Assignment AV) {
  1074. LiveSet->DebugValue[Var] = AV;
  1075. // Add default (Var -> ⊤) to StackHome if Var isn't in StackHome yet.
  1076. LiveSet->StackHomeValue.insert({Var, Assignment::makeNoneOrPhi()});
  1077. // Add default (Var -> ⊤) to LiveLocs if Var isn't in LiveLocs yet. Callers
  1078. // of addDbgDef will call setLocKind to override.
  1079. LiveSet->LiveLoc.insert({Var, LocKind::None});
  1080. };
  1081. AddDef(LiveSet, Var, AV);
  1082. // Use this assigment for all fragments contained within Var, but do not
  1083. // provide a Source because we cannot convert Var's value to a value for the
  1084. // fragment.
  1085. Assignment FragAV = AV;
  1086. FragAV.Source = nullptr;
  1087. for (VariableID Frag : VarContains[Var])
  1088. AddDef(LiveSet, Frag, FragAV);
  1089. }
  1090. static DIAssignID *getIDFromInst(const Instruction &I) {
  1091. return cast<DIAssignID>(I.getMetadata(LLVMContext::MD_DIAssignID));
  1092. }
  1093. static DIAssignID *getIDFromMarker(const DbgAssignIntrinsic &DAI) {
  1094. return cast<DIAssignID>(DAI.getAssignID());
  1095. }
  1096. /// Return true if \p Var has an assignment in \p M matching \p AV.
  1097. bool AssignmentTrackingLowering::hasVarWithAssignment(VariableID Var,
  1098. const Assignment &AV,
  1099. const AssignmentMap &M) {
  1100. auto AssignmentIsMapped = [](VariableID Var, const Assignment &AV,
  1101. const AssignmentMap &M) {
  1102. auto R = M.find(Var);
  1103. if (R == M.end())
  1104. return false;
  1105. return AV.isSameSourceAssignment(R->second);
  1106. };
  1107. if (!AssignmentIsMapped(Var, AV, M))
  1108. return false;
  1109. // Check all the frags contained within Var as these will have all been
  1110. // mapped to AV at the last store to Var.
  1111. for (VariableID Frag : VarContains[Var])
  1112. if (!AssignmentIsMapped(Frag, AV, M))
  1113. return false;
  1114. return true;
  1115. }
  1116. #ifndef NDEBUG
  1117. const char *locStr(AssignmentTrackingLowering::LocKind Loc) {
  1118. using LocKind = AssignmentTrackingLowering::LocKind;
  1119. switch (Loc) {
  1120. case LocKind::Val:
  1121. return "Val";
  1122. case LocKind::Mem:
  1123. return "Mem";
  1124. case LocKind::None:
  1125. return "None";
  1126. };
  1127. llvm_unreachable("unknown LocKind");
  1128. }
  1129. #endif
  1130. void AssignmentTrackingLowering::emitDbgValue(
  1131. AssignmentTrackingLowering::LocKind Kind,
  1132. const DbgVariableIntrinsic *Source, Instruction *After) {
  1133. DILocation *DL = Source->getDebugLoc();
  1134. auto Emit = [this, Source, After, DL](Value *Val, DIExpression *Expr) {
  1135. assert(Expr);
  1136. if (!Val)
  1137. Val = PoisonValue::get(Type::getInt1Ty(Source->getContext()));
  1138. // Find a suitable insert point.
  1139. Instruction *InsertBefore = After->getNextNode();
  1140. assert(InsertBefore && "Shouldn't be inserting after a terminator");
  1141. VariableID Var = getVariableID(DebugVariable(Source));
  1142. VarLocInfo VarLoc;
  1143. VarLoc.VariableID = static_cast<VariableID>(Var);
  1144. VarLoc.Expr = Expr;
  1145. VarLoc.V = Val;
  1146. VarLoc.DL = DL;
  1147. // Insert it into the map for later.
  1148. InsertBeforeMap[InsertBefore].push_back(VarLoc);
  1149. };
  1150. // NOTE: This block can mutate Kind.
  1151. if (Kind == LocKind::Mem) {
  1152. const auto *DAI = cast<DbgAssignIntrinsic>(Source);
  1153. // Check the address hasn't been dropped (e.g. the debug uses may not have
  1154. // been replaced before deleting a Value).
  1155. if (DAI->isKillAddress()) {
  1156. // The address isn't valid so treat this as a non-memory def.
  1157. Kind = LocKind::Val;
  1158. } else {
  1159. Value *Val = DAI->getAddress();
  1160. DIExpression *Expr = DAI->getAddressExpression();
  1161. assert(!Expr->getFragmentInfo() &&
  1162. "fragment info should be stored in value-expression only");
  1163. // Copy the fragment info over from the value-expression to the new
  1164. // DIExpression.
  1165. if (auto OptFragInfo = Source->getExpression()->getFragmentInfo()) {
  1166. auto FragInfo = *OptFragInfo;
  1167. Expr = *DIExpression::createFragmentExpression(
  1168. Expr, FragInfo.OffsetInBits, FragInfo.SizeInBits);
  1169. }
  1170. // The address-expression has an implicit deref, add it now.
  1171. std::tie(Val, Expr) =
  1172. walkToAllocaAndPrependOffsetDeref(Layout, Val, Expr);
  1173. Emit(Val, Expr);
  1174. return;
  1175. }
  1176. }
  1177. if (Kind == LocKind::Val) {
  1178. /// Get the value component, converting to Undef if it is variadic.
  1179. Value *Val =
  1180. Source->hasArgList() ? nullptr : Source->getVariableLocationOp(0);
  1181. Emit(Val, Source->getExpression());
  1182. return;
  1183. }
  1184. if (Kind == LocKind::None) {
  1185. Emit(nullptr, Source->getExpression());
  1186. return;
  1187. }
  1188. }
  1189. void AssignmentTrackingLowering::processNonDbgInstruction(
  1190. Instruction &I, AssignmentTrackingLowering::BlockInfo *LiveSet) {
  1191. if (I.hasMetadata(LLVMContext::MD_DIAssignID))
  1192. processTaggedInstruction(I, LiveSet);
  1193. else
  1194. processUntaggedInstruction(I, LiveSet);
  1195. }
  1196. void AssignmentTrackingLowering::processUntaggedInstruction(
  1197. Instruction &I, AssignmentTrackingLowering::BlockInfo *LiveSet) {
  1198. // Interpret stack stores that are not tagged as an assignment in memory for
  1199. // the variables associated with that address. These stores may not be tagged
  1200. // because a) the store cannot be represented using dbg.assigns (non-const
  1201. // length or offset) or b) the tag was accidentally dropped during
  1202. // optimisations. For these stores we fall back to assuming that the stack
  1203. // home is a valid location for the variables. The benefit is that this
  1204. // prevents us missing an assignment and therefore incorrectly maintaining
  1205. // earlier location definitions, and in many cases it should be a reasonable
  1206. // assumption. However, this will occasionally lead to slight
  1207. // inaccuracies. The value of a hoisted untagged store will be visible
  1208. // "early", for example.
  1209. assert(!I.hasMetadata(LLVMContext::MD_DIAssignID));
  1210. auto It = UntaggedStoreVars.find(&I);
  1211. if (It == UntaggedStoreVars.end())
  1212. return; // No variables associated with the store destination.
  1213. LLVM_DEBUG(dbgs() << "processUntaggedInstruction on UNTAGGED INST " << I
  1214. << "\n");
  1215. // Iterate over the variables that this store affects, add a NoneOrPhi dbg
  1216. // and mem def, set lockind to Mem, and emit a location def for each.
  1217. for (auto [Var, Info] : It->second) {
  1218. // This instruction is treated as both a debug and memory assignment,
  1219. // meaning the memory location should be used. We don't have an assignment
  1220. // ID though so use Assignment::makeNoneOrPhi() to create an imaginary one.
  1221. addMemDef(LiveSet, Var, Assignment::makeNoneOrPhi());
  1222. addDbgDef(LiveSet, Var, Assignment::makeNoneOrPhi());
  1223. setLocKind(LiveSet, Var, LocKind::Mem);
  1224. LLVM_DEBUG(dbgs() << " setting Stack LocKind to: " << locStr(LocKind::Mem)
  1225. << "\n");
  1226. // Build the dbg location def to insert.
  1227. //
  1228. // DIExpression: Add fragment and offset.
  1229. DebugVariable V = FnVarLocs->getVariable(Var);
  1230. DIExpression *DIE = DIExpression::get(I.getContext(), std::nullopt);
  1231. if (auto Frag = V.getFragment()) {
  1232. auto R = DIExpression::createFragmentExpression(DIE, Frag->OffsetInBits,
  1233. Frag->SizeInBits);
  1234. assert(R && "unexpected createFragmentExpression failure");
  1235. DIE = *R;
  1236. }
  1237. SmallVector<uint64_t, 3> Ops;
  1238. if (Info.OffsetInBits)
  1239. Ops = {dwarf::DW_OP_plus_uconst, Info.OffsetInBits / 8};
  1240. Ops.push_back(dwarf::DW_OP_deref);
  1241. DIE = DIExpression::prependOpcodes(DIE, Ops, /*StackValue=*/false,
  1242. /*EntryValue=*/false);
  1243. // Find a suitable insert point.
  1244. Instruction *InsertBefore = I.getNextNode();
  1245. assert(InsertBefore && "Shouldn't be inserting after a terminator");
  1246. // Get DILocation for this unrecorded assignment.
  1247. DILocation *InlinedAt = const_cast<DILocation *>(V.getInlinedAt());
  1248. const DILocation *DILoc = DILocation::get(
  1249. Fn.getContext(), 0, 0, V.getVariable()->getScope(), InlinedAt);
  1250. VarLocInfo VarLoc;
  1251. VarLoc.VariableID = static_cast<VariableID>(Var);
  1252. VarLoc.Expr = DIE;
  1253. VarLoc.V = const_cast<AllocaInst *>(Info.Base);
  1254. VarLoc.DL = DILoc;
  1255. // 3. Insert it into the map for later.
  1256. InsertBeforeMap[InsertBefore].push_back(VarLoc);
  1257. }
  1258. }
  1259. void AssignmentTrackingLowering::processTaggedInstruction(
  1260. Instruction &I, AssignmentTrackingLowering::BlockInfo *LiveSet) {
  1261. auto Linked = at::getAssignmentMarkers(&I);
  1262. // No dbg.assign intrinsics linked.
  1263. // FIXME: All vars that have a stack slot this store modifies that don't have
  1264. // a dbg.assign linked to it should probably treat this like an untagged
  1265. // store.
  1266. if (Linked.empty())
  1267. return;
  1268. LLVM_DEBUG(dbgs() << "processTaggedInstruction on " << I << "\n");
  1269. for (DbgAssignIntrinsic *DAI : Linked) {
  1270. VariableID Var = getVariableID(DebugVariable(DAI));
  1271. // Something has gone wrong if VarsWithStackSlot doesn't contain a variable
  1272. // that is linked to a store.
  1273. assert(VarsWithStackSlot->count(getAggregate(DAI)) &&
  1274. "expected DAI's variable to have stack slot");
  1275. Assignment AV = Assignment::makeFromMemDef(getIDFromInst(I));
  1276. addMemDef(LiveSet, Var, AV);
  1277. LLVM_DEBUG(dbgs() << " linked to " << *DAI << "\n");
  1278. LLVM_DEBUG(dbgs() << " LiveLoc " << locStr(getLocKind(LiveSet, Var))
  1279. << " -> ");
  1280. // The last assignment to the stack is now AV. Check if the last debug
  1281. // assignment has a matching Assignment.
  1282. if (hasVarWithAssignment(Var, AV, LiveSet->DebugValue)) {
  1283. // The StackHomeValue and DebugValue for this variable match so we can
  1284. // emit a stack home location here.
  1285. LLVM_DEBUG(dbgs() << "Mem, Stack matches Debug program\n";);
  1286. LLVM_DEBUG(dbgs() << " Stack val: "; AV.dump(dbgs()); dbgs() << "\n");
  1287. LLVM_DEBUG(dbgs() << " Debug val: ";
  1288. LiveSet->DebugValue[Var].dump(dbgs()); dbgs() << "\n");
  1289. setLocKind(LiveSet, Var, LocKind::Mem);
  1290. emitDbgValue(LocKind::Mem, DAI, &I);
  1291. continue;
  1292. }
  1293. // The StackHomeValue and DebugValue for this variable do not match. I.e.
  1294. // The value currently stored in the stack is not what we'd expect to
  1295. // see, so we cannot use emit a stack home location here. Now we will
  1296. // look at the live LocKind for the variable and determine an appropriate
  1297. // dbg.value to emit.
  1298. LocKind PrevLoc = getLocKind(LiveSet, Var);
  1299. switch (PrevLoc) {
  1300. case LocKind::Val: {
  1301. // The value in memory in memory has changed but we're not currently
  1302. // using the memory location. Do nothing.
  1303. LLVM_DEBUG(dbgs() << "Val, (unchanged)\n";);
  1304. setLocKind(LiveSet, Var, LocKind::Val);
  1305. } break;
  1306. case LocKind::Mem: {
  1307. // There's been an assignment to memory that we were using as a
  1308. // location for this variable, and the Assignment doesn't match what
  1309. // we'd expect to see in memory.
  1310. if (LiveSet->DebugValue[Var].Status == Assignment::NoneOrPhi) {
  1311. // We need to terminate any previously open location now.
  1312. LLVM_DEBUG(dbgs() << "None, No Debug value available\n";);
  1313. setLocKind(LiveSet, Var, LocKind::None);
  1314. emitDbgValue(LocKind::None, DAI, &I);
  1315. } else {
  1316. // The previous DebugValue Value can be used here.
  1317. LLVM_DEBUG(dbgs() << "Val, Debug value is Known\n";);
  1318. setLocKind(LiveSet, Var, LocKind::Val);
  1319. Assignment PrevAV = LiveSet->DebugValue.lookup(Var);
  1320. if (PrevAV.Source) {
  1321. emitDbgValue(LocKind::Val, PrevAV.Source, &I);
  1322. } else {
  1323. // PrevAV.Source is nullptr so we must emit undef here.
  1324. emitDbgValue(LocKind::None, DAI, &I);
  1325. }
  1326. }
  1327. } break;
  1328. case LocKind::None: {
  1329. // There's been an assignment to memory and we currently are
  1330. // not tracking a location for the variable. Do not emit anything.
  1331. LLVM_DEBUG(dbgs() << "None, (unchanged)\n";);
  1332. setLocKind(LiveSet, Var, LocKind::None);
  1333. } break;
  1334. }
  1335. }
  1336. }
  1337. void AssignmentTrackingLowering::processDbgAssign(DbgAssignIntrinsic &DAI,
  1338. BlockInfo *LiveSet) {
  1339. // Only bother tracking variables that are at some point stack homed. Other
  1340. // variables can be dealt with trivially later.
  1341. if (!VarsWithStackSlot->count(getAggregate(&DAI)))
  1342. return;
  1343. VariableID Var = getVariableID(DebugVariable(&DAI));
  1344. Assignment AV = Assignment::make(getIDFromMarker(DAI), &DAI);
  1345. addDbgDef(LiveSet, Var, AV);
  1346. LLVM_DEBUG(dbgs() << "processDbgAssign on " << DAI << "\n";);
  1347. LLVM_DEBUG(dbgs() << " LiveLoc " << locStr(getLocKind(LiveSet, Var))
  1348. << " -> ");
  1349. // Check if the DebugValue and StackHomeValue both hold the same
  1350. // Assignment.
  1351. if (hasVarWithAssignment(Var, AV, LiveSet->StackHomeValue)) {
  1352. // They match. We can use the stack home because the debug intrinsics state
  1353. // that an assignment happened here, and we know that specific assignment
  1354. // was the last one to take place in memory for this variable.
  1355. LocKind Kind;
  1356. if (DAI.isKillAddress()) {
  1357. LLVM_DEBUG(
  1358. dbgs()
  1359. << "Val, Stack matches Debug program but address is killed\n";);
  1360. Kind = LocKind::Val;
  1361. } else {
  1362. LLVM_DEBUG(dbgs() << "Mem, Stack matches Debug program\n";);
  1363. Kind = LocKind::Mem;
  1364. };
  1365. setLocKind(LiveSet, Var, Kind);
  1366. emitDbgValue(Kind, &DAI, &DAI);
  1367. } else {
  1368. // The last assignment to the memory location isn't the one that we want to
  1369. // show to the user so emit a dbg.value(Value). Value may be undef.
  1370. LLVM_DEBUG(dbgs() << "Val, Stack contents is unknown\n";);
  1371. setLocKind(LiveSet, Var, LocKind::Val);
  1372. emitDbgValue(LocKind::Val, &DAI, &DAI);
  1373. }
  1374. }
  1375. void AssignmentTrackingLowering::processDbgValue(DbgValueInst &DVI,
  1376. BlockInfo *LiveSet) {
  1377. // Only other tracking variables that are at some point stack homed.
  1378. // Other variables can be dealt with trivally later.
  1379. if (!VarsWithStackSlot->count(getAggregate(&DVI)))
  1380. return;
  1381. VariableID Var = getVariableID(DebugVariable(&DVI));
  1382. // We have no ID to create an Assignment with so we mark this assignment as
  1383. // NoneOrPhi. Note that the dbg.value still exists, we just cannot determine
  1384. // the assignment responsible for setting this value.
  1385. // This is fine; dbg.values are essentially interchangable with unlinked
  1386. // dbg.assigns, and some passes such as mem2reg and instcombine add them to
  1387. // PHIs for promoted variables.
  1388. Assignment AV = Assignment::makeNoneOrPhi();
  1389. addDbgDef(LiveSet, Var, AV);
  1390. LLVM_DEBUG(dbgs() << "processDbgValue on " << DVI << "\n";);
  1391. LLVM_DEBUG(dbgs() << " LiveLoc " << locStr(getLocKind(LiveSet, Var))
  1392. << " -> Val, dbg.value override");
  1393. setLocKind(LiveSet, Var, LocKind::Val);
  1394. emitDbgValue(LocKind::Val, &DVI, &DVI);
  1395. }
  1396. void AssignmentTrackingLowering::processDbgInstruction(
  1397. Instruction &I, AssignmentTrackingLowering::BlockInfo *LiveSet) {
  1398. assert(!isa<DbgAddrIntrinsic>(&I) && "unexpected dbg.addr");
  1399. if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(&I))
  1400. processDbgAssign(*DAI, LiveSet);
  1401. else if (auto *DVI = dyn_cast<DbgValueInst>(&I))
  1402. processDbgValue(*DVI, LiveSet);
  1403. }
  1404. void AssignmentTrackingLowering::resetInsertionPoint(Instruction &After) {
  1405. assert(!After.isTerminator() && "Can't insert after a terminator");
  1406. auto R = InsertBeforeMap.find(After.getNextNode());
  1407. if (R == InsertBeforeMap.end())
  1408. return;
  1409. R->second.clear();
  1410. }
  1411. void AssignmentTrackingLowering::process(BasicBlock &BB, BlockInfo *LiveSet) {
  1412. for (auto II = BB.begin(), EI = BB.end(); II != EI;) {
  1413. assert(VarsTouchedThisFrame.empty());
  1414. // Process the instructions in "frames". A "frame" includes a single
  1415. // non-debug instruction followed any debug instructions before the
  1416. // next non-debug instruction.
  1417. if (!isa<DbgInfoIntrinsic>(&*II)) {
  1418. if (II->isTerminator())
  1419. break;
  1420. resetInsertionPoint(*II);
  1421. processNonDbgInstruction(*II, LiveSet);
  1422. assert(LiveSet->isValid());
  1423. ++II;
  1424. }
  1425. while (II != EI) {
  1426. if (!isa<DbgInfoIntrinsic>(&*II))
  1427. break;
  1428. resetInsertionPoint(*II);
  1429. processDbgInstruction(*II, LiveSet);
  1430. assert(LiveSet->isValid());
  1431. ++II;
  1432. }
  1433. // We've processed everything in the "frame". Now determine which variables
  1434. // cannot be represented by a dbg.declare.
  1435. for (auto Var : VarsTouchedThisFrame) {
  1436. LocKind Loc = getLocKind(LiveSet, Var);
  1437. // If a variable's LocKind is anything other than LocKind::Mem then we
  1438. // must note that it cannot be represented with a dbg.declare.
  1439. // Note that this check is enough without having to check the result of
  1440. // joins() because for join to produce anything other than Mem after
  1441. // we've already seen a Mem we'd be joining None or Val with Mem. In that
  1442. // case, we've already hit this codepath when we set the LocKind to Val
  1443. // or None in that block.
  1444. if (Loc != LocKind::Mem) {
  1445. DebugVariable DbgVar = FnVarLocs->getVariable(Var);
  1446. DebugAggregate Aggr{DbgVar.getVariable(), DbgVar.getInlinedAt()};
  1447. NotAlwaysStackHomed.insert(Aggr);
  1448. }
  1449. }
  1450. VarsTouchedThisFrame.clear();
  1451. }
  1452. }
  1453. AssignmentTrackingLowering::LocKind
  1454. AssignmentTrackingLowering::joinKind(LocKind A, LocKind B) {
  1455. // Partial order:
  1456. // None > Mem, Val
  1457. return A == B ? A : LocKind::None;
  1458. }
  1459. AssignmentTrackingLowering::LocMap
  1460. AssignmentTrackingLowering::joinLocMap(const LocMap &A, const LocMap &B) {
  1461. // Join A and B.
  1462. //
  1463. // U = join(a, b) for a in A, b in B where Var(a) == Var(b)
  1464. // D = join(x, ⊤) for x where Var(x) is in A xor B
  1465. // Join = U ∪ D
  1466. //
  1467. // This is achieved by performing a join on elements from A and B with
  1468. // variables common to both A and B (join elements indexed by var intersect),
  1469. // then adding LocKind::None elements for vars in A xor B. The latter part is
  1470. // equivalent to performing join on elements with variables in A xor B with
  1471. // LocKind::None (⊤) since join(x, ⊤) = ⊤.
  1472. LocMap Join;
  1473. SmallVector<VariableID, 16> SymmetricDifference;
  1474. // Insert the join of the elements with common vars into Join. Add the
  1475. // remaining elements to into SymmetricDifference.
  1476. for (const auto &[Var, Loc] : A) {
  1477. // If this Var doesn't exist in B then add it to the symmetric difference
  1478. // set.
  1479. auto R = B.find(Var);
  1480. if (R == B.end()) {
  1481. SymmetricDifference.push_back(Var);
  1482. continue;
  1483. }
  1484. // There is an entry for Var in both, join it.
  1485. Join[Var] = joinKind(Loc, R->second);
  1486. }
  1487. unsigned IntersectSize = Join.size();
  1488. (void)IntersectSize;
  1489. // Add the elements in B with variables that are not in A into
  1490. // SymmetricDifference.
  1491. for (const auto &Pair : B) {
  1492. VariableID Var = Pair.first;
  1493. if (A.count(Var) == 0)
  1494. SymmetricDifference.push_back(Var);
  1495. }
  1496. // Add SymmetricDifference elements to Join and return the result.
  1497. for (const auto &Var : SymmetricDifference)
  1498. Join.insert({Var, LocKind::None});
  1499. assert(Join.size() == (IntersectSize + SymmetricDifference.size()));
  1500. assert(Join.size() >= A.size() && Join.size() >= B.size());
  1501. return Join;
  1502. }
  1503. AssignmentTrackingLowering::Assignment
  1504. AssignmentTrackingLowering::joinAssignment(const Assignment &A,
  1505. const Assignment &B) {
  1506. // Partial order:
  1507. // NoneOrPhi(null, null) > Known(v, ?s)
  1508. // If either are NoneOrPhi the join is NoneOrPhi.
  1509. // If either value is different then the result is
  1510. // NoneOrPhi (joining two values is a Phi).
  1511. if (!A.isSameSourceAssignment(B))
  1512. return Assignment::makeNoneOrPhi();
  1513. if (A.Status == Assignment::NoneOrPhi)
  1514. return Assignment::makeNoneOrPhi();
  1515. // Source is used to lookup the value + expression in the debug program if
  1516. // the stack slot gets assigned a value earlier than expected. Because
  1517. // we're only tracking the one dbg.assign, we can't capture debug PHIs.
  1518. // It's unlikely that we're losing out on much coverage by avoiding that
  1519. // extra work.
  1520. // The Source may differ in this situation:
  1521. // Pred.1:
  1522. // dbg.assign i32 0, ..., !1, ...
  1523. // Pred.2:
  1524. // dbg.assign i32 1, ..., !1, ...
  1525. // Here the same assignment (!1) was performed in both preds in the source,
  1526. // but we can't use either one unless they are identical (e.g. .we don't
  1527. // want to arbitrarily pick between constant values).
  1528. auto JoinSource = [&]() -> DbgAssignIntrinsic * {
  1529. if (A.Source == B.Source)
  1530. return A.Source;
  1531. if (A.Source == nullptr || B.Source == nullptr)
  1532. return nullptr;
  1533. if (A.Source->isIdenticalTo(B.Source))
  1534. return A.Source;
  1535. return nullptr;
  1536. };
  1537. DbgAssignIntrinsic *Source = JoinSource();
  1538. assert(A.Status == B.Status && A.Status == Assignment::Known);
  1539. assert(A.ID == B.ID);
  1540. return Assignment::make(A.ID, Source);
  1541. }
  1542. AssignmentTrackingLowering::AssignmentMap
  1543. AssignmentTrackingLowering::joinAssignmentMap(const AssignmentMap &A,
  1544. const AssignmentMap &B) {
  1545. // Join A and B.
  1546. //
  1547. // U = join(a, b) for a in A, b in B where Var(a) == Var(b)
  1548. // D = join(x, ⊤) for x where Var(x) is in A xor B
  1549. // Join = U ∪ D
  1550. //
  1551. // This is achieved by performing a join on elements from A and B with
  1552. // variables common to both A and B (join elements indexed by var intersect),
  1553. // then adding LocKind::None elements for vars in A xor B. The latter part is
  1554. // equivalent to performing join on elements with variables in A xor B with
  1555. // Status::NoneOrPhi (⊤) since join(x, ⊤) = ⊤.
  1556. AssignmentMap Join;
  1557. SmallVector<VariableID, 16> SymmetricDifference;
  1558. // Insert the join of the elements with common vars into Join. Add the
  1559. // remaining elements to into SymmetricDifference.
  1560. for (const auto &[Var, AV] : A) {
  1561. // If this Var doesn't exist in B then add it to the symmetric difference
  1562. // set.
  1563. auto R = B.find(Var);
  1564. if (R == B.end()) {
  1565. SymmetricDifference.push_back(Var);
  1566. continue;
  1567. }
  1568. // There is an entry for Var in both, join it.
  1569. Join[Var] = joinAssignment(AV, R->second);
  1570. }
  1571. unsigned IntersectSize = Join.size();
  1572. (void)IntersectSize;
  1573. // Add the elements in B with variables that are not in A into
  1574. // SymmetricDifference.
  1575. for (const auto &Pair : B) {
  1576. VariableID Var = Pair.first;
  1577. if (A.count(Var) == 0)
  1578. SymmetricDifference.push_back(Var);
  1579. }
  1580. // Add SymmetricDifference elements to Join and return the result.
  1581. for (auto Var : SymmetricDifference)
  1582. Join.insert({Var, Assignment::makeNoneOrPhi()});
  1583. assert(Join.size() == (IntersectSize + SymmetricDifference.size()));
  1584. assert(Join.size() >= A.size() && Join.size() >= B.size());
  1585. return Join;
  1586. }
  1587. AssignmentTrackingLowering::BlockInfo
  1588. AssignmentTrackingLowering::joinBlockInfo(const BlockInfo &A,
  1589. const BlockInfo &B) {
  1590. BlockInfo Join;
  1591. Join.LiveLoc = joinLocMap(A.LiveLoc, B.LiveLoc);
  1592. Join.StackHomeValue = joinAssignmentMap(A.StackHomeValue, B.StackHomeValue);
  1593. Join.DebugValue = joinAssignmentMap(A.DebugValue, B.DebugValue);
  1594. assert(Join.isValid());
  1595. return Join;
  1596. }
  1597. bool AssignmentTrackingLowering::join(
  1598. const BasicBlock &BB, const SmallPtrSet<BasicBlock *, 16> &Visited) {
  1599. BlockInfo BBLiveIn;
  1600. bool FirstJoin = true;
  1601. // LiveIn locs for BB is the join of the already-processed preds' LiveOut
  1602. // locs.
  1603. for (auto I = pred_begin(&BB), E = pred_end(&BB); I != E; I++) {
  1604. // Ignore backedges if we have not visited the predecessor yet. As the
  1605. // predecessor hasn't yet had locations propagated into it, most locations
  1606. // will not yet be valid, so treat them as all being uninitialized and
  1607. // potentially valid. If a location guessed to be correct here is
  1608. // invalidated later, we will remove it when we revisit this block. This
  1609. // is essentially the same as initialising all LocKinds and Assignments to
  1610. // an implicit ⊥ value which is the identity value for the join operation.
  1611. const BasicBlock *Pred = *I;
  1612. if (!Visited.count(Pred))
  1613. continue;
  1614. auto PredLiveOut = LiveOut.find(Pred);
  1615. // Pred must have been processed already. See comment at start of this loop.
  1616. assert(PredLiveOut != LiveOut.end());
  1617. // Perform the join of BBLiveIn (current live-in info) and PrevLiveOut.
  1618. if (FirstJoin)
  1619. BBLiveIn = PredLiveOut->second;
  1620. else
  1621. BBLiveIn = joinBlockInfo(std::move(BBLiveIn), PredLiveOut->second);
  1622. FirstJoin = false;
  1623. }
  1624. auto CurrentLiveInEntry = LiveIn.find(&BB);
  1625. // Check if there isn't an entry, or there is but the LiveIn set has changed
  1626. // (expensive check).
  1627. if (CurrentLiveInEntry == LiveIn.end() ||
  1628. BBLiveIn != CurrentLiveInEntry->second) {
  1629. LiveIn[&BB] = std::move(BBLiveIn);
  1630. // A change has occured.
  1631. return true;
  1632. }
  1633. // No change.
  1634. return false;
  1635. }
  1636. /// Return true if A fully contains B.
  1637. static bool fullyContains(DIExpression::FragmentInfo A,
  1638. DIExpression::FragmentInfo B) {
  1639. auto ALeft = A.OffsetInBits;
  1640. auto BLeft = B.OffsetInBits;
  1641. if (BLeft < ALeft)
  1642. return false;
  1643. auto ARight = ALeft + A.SizeInBits;
  1644. auto BRight = BLeft + B.SizeInBits;
  1645. if (BRight > ARight)
  1646. return false;
  1647. return true;
  1648. }
  1649. static std::optional<at::AssignmentInfo>
  1650. getUntaggedStoreAssignmentInfo(const Instruction &I, const DataLayout &Layout) {
  1651. // Don't bother checking if this is an AllocaInst. We know this
  1652. // instruction has no tag which means there are no variables associated
  1653. // with it.
  1654. if (const auto *SI = dyn_cast<StoreInst>(&I))
  1655. return at::getAssignmentInfo(Layout, SI);
  1656. if (const auto *MI = dyn_cast<MemIntrinsic>(&I))
  1657. return at::getAssignmentInfo(Layout, MI);
  1658. // Alloca or non-store-like inst.
  1659. return std::nullopt;
  1660. }
  1661. /// Build a map of {Variable x: Variables y} where all variable fragments
  1662. /// contained within the variable fragment x are in set y. This means that
  1663. /// y does not contain all overlaps because partial overlaps are excluded.
  1664. ///
  1665. /// While we're iterating over the function, add single location defs for
  1666. /// dbg.declares to \p FnVarLocs
  1667. ///
  1668. /// Finally, populate UntaggedStoreVars with a mapping of untagged stores to
  1669. /// the stored-to variable fragments.
  1670. ///
  1671. /// These tasks are bundled together to reduce the number of times we need
  1672. /// to iterate over the function as they can be achieved together in one pass.
  1673. static AssignmentTrackingLowering::OverlapMap buildOverlapMapAndRecordDeclares(
  1674. Function &Fn, FunctionVarLocsBuilder *FnVarLocs,
  1675. AssignmentTrackingLowering::UntaggedStoreAssignmentMap &UntaggedStoreVars) {
  1676. DenseSet<DebugVariable> Seen;
  1677. // Map of Variable: [Fragments].
  1678. DenseMap<DebugAggregate, SmallVector<DebugVariable, 8>> FragmentMap;
  1679. // Iterate over all instructions:
  1680. // - dbg.declare -> add single location variable record
  1681. // - dbg.* -> Add fragments to FragmentMap
  1682. // - untagged store -> Add fragments to FragmentMap and update
  1683. // UntaggedStoreVars.
  1684. // We need to add fragments for untagged stores too so that we can correctly
  1685. // clobber overlapped fragment locations later.
  1686. for (auto &BB : Fn) {
  1687. for (auto &I : BB) {
  1688. if (auto *DDI = dyn_cast<DbgDeclareInst>(&I)) {
  1689. FnVarLocs->addSingleLocVar(DebugVariable(DDI), DDI->getExpression(),
  1690. DDI->getDebugLoc(), DDI->getAddress());
  1691. } else if (auto *DII = dyn_cast<DbgVariableIntrinsic>(&I)) {
  1692. DebugVariable DV = DebugVariable(DII);
  1693. DebugAggregate DA = {DV.getVariable(), DV.getInlinedAt()};
  1694. if (Seen.insert(DV).second)
  1695. FragmentMap[DA].push_back(DV);
  1696. } else if (auto Info = getUntaggedStoreAssignmentInfo(
  1697. I, Fn.getParent()->getDataLayout())) {
  1698. // Find markers linked to this alloca.
  1699. for (DbgAssignIntrinsic *DAI : at::getAssignmentMarkers(Info->Base)) {
  1700. // Discard the fragment if it covers the entire variable.
  1701. std::optional<DIExpression::FragmentInfo> FragInfo =
  1702. [&Info, DAI]() -> std::optional<DIExpression::FragmentInfo> {
  1703. DIExpression::FragmentInfo F;
  1704. F.OffsetInBits = Info->OffsetInBits;
  1705. F.SizeInBits = Info->SizeInBits;
  1706. if (auto ExistingFrag = DAI->getExpression()->getFragmentInfo())
  1707. F.OffsetInBits += ExistingFrag->OffsetInBits;
  1708. if (auto Sz = DAI->getVariable()->getSizeInBits()) {
  1709. if (F.OffsetInBits == 0 && F.SizeInBits == *Sz)
  1710. return std::nullopt;
  1711. }
  1712. return F;
  1713. }();
  1714. DebugVariable DV = DebugVariable(DAI->getVariable(), FragInfo,
  1715. DAI->getDebugLoc().getInlinedAt());
  1716. DebugAggregate DA = {DV.getVariable(), DV.getInlinedAt()};
  1717. // Cache this info for later.
  1718. UntaggedStoreVars[&I].push_back(
  1719. {FnVarLocs->insertVariable(DV), *Info});
  1720. if (Seen.insert(DV).second)
  1721. FragmentMap[DA].push_back(DV);
  1722. }
  1723. }
  1724. }
  1725. }
  1726. // Sort the fragment map for each DebugAggregate in non-descending
  1727. // order of fragment size. Assert no entries are duplicates.
  1728. for (auto &Pair : FragmentMap) {
  1729. SmallVector<DebugVariable, 8> &Frags = Pair.second;
  1730. std::sort(
  1731. Frags.begin(), Frags.end(), [](DebugVariable Next, DebugVariable Elmt) {
  1732. assert(!(Elmt.getFragmentOrDefault() == Next.getFragmentOrDefault()));
  1733. return Elmt.getFragmentOrDefault().SizeInBits >
  1734. Next.getFragmentOrDefault().SizeInBits;
  1735. });
  1736. }
  1737. // Build the map.
  1738. AssignmentTrackingLowering::OverlapMap Map;
  1739. for (auto Pair : FragmentMap) {
  1740. auto &Frags = Pair.second;
  1741. for (auto It = Frags.begin(), IEnd = Frags.end(); It != IEnd; ++It) {
  1742. DIExpression::FragmentInfo Frag = It->getFragmentOrDefault();
  1743. // Find the frags that this is contained within.
  1744. //
  1745. // Because Frags is sorted by size and none have the same offset and
  1746. // size, we know that this frag can only be contained by subsequent
  1747. // elements.
  1748. SmallVector<DebugVariable, 8>::iterator OtherIt = It;
  1749. ++OtherIt;
  1750. VariableID ThisVar = FnVarLocs->insertVariable(*It);
  1751. for (; OtherIt != IEnd; ++OtherIt) {
  1752. DIExpression::FragmentInfo OtherFrag = OtherIt->getFragmentOrDefault();
  1753. VariableID OtherVar = FnVarLocs->insertVariable(*OtherIt);
  1754. if (fullyContains(OtherFrag, Frag))
  1755. Map[OtherVar].push_back(ThisVar);
  1756. }
  1757. }
  1758. }
  1759. return Map;
  1760. }
  1761. bool AssignmentTrackingLowering::run(FunctionVarLocsBuilder *FnVarLocsBuilder) {
  1762. if (Fn.size() > MaxNumBlocks) {
  1763. LLVM_DEBUG(dbgs() << "[AT] Dropping var locs in: " << Fn.getName()
  1764. << ": too many blocks (" << Fn.size() << ")\n");
  1765. at::deleteAll(&Fn);
  1766. return false;
  1767. }
  1768. FnVarLocs = FnVarLocsBuilder;
  1769. // The general structure here is inspired by VarLocBasedImpl.cpp
  1770. // (LiveDebugValues).
  1771. // Build the variable fragment overlap map.
  1772. // Note that this pass doesn't handle partial overlaps correctly (FWIW
  1773. // neither does LiveDebugVariables) because that is difficult to do and
  1774. // appears to be rare occurance.
  1775. VarContains =
  1776. buildOverlapMapAndRecordDeclares(Fn, FnVarLocs, UntaggedStoreVars);
  1777. // Prepare for traversal.
  1778. ReversePostOrderTraversal<Function *> RPOT(&Fn);
  1779. std::priority_queue<unsigned int, std::vector<unsigned int>,
  1780. std::greater<unsigned int>>
  1781. Worklist;
  1782. std::priority_queue<unsigned int, std::vector<unsigned int>,
  1783. std::greater<unsigned int>>
  1784. Pending;
  1785. DenseMap<unsigned int, BasicBlock *> OrderToBB;
  1786. DenseMap<BasicBlock *, unsigned int> BBToOrder;
  1787. { // Init OrderToBB and BBToOrder.
  1788. unsigned int RPONumber = 0;
  1789. for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) {
  1790. OrderToBB[RPONumber] = *RI;
  1791. BBToOrder[*RI] = RPONumber;
  1792. Worklist.push(RPONumber);
  1793. ++RPONumber;
  1794. }
  1795. LiveIn.init(RPONumber);
  1796. LiveOut.init(RPONumber);
  1797. }
  1798. // Perform the traversal.
  1799. //
  1800. // This is a standard "union of predecessor outs" dataflow problem. To solve
  1801. // it, we perform join() and process() using the two worklist method until
  1802. // the LiveIn data for each block becomes unchanging. The "proof" that this
  1803. // terminates can be put together by looking at the comments around LocKind,
  1804. // Assignment, and the various join methods, which show that all the elements
  1805. // involved are made up of join-semilattices; LiveIn(n) can only
  1806. // monotonically increase in value throughout the dataflow.
  1807. //
  1808. SmallPtrSet<BasicBlock *, 16> Visited;
  1809. while (!Worklist.empty()) {
  1810. // We track what is on the pending worklist to avoid inserting the same
  1811. // thing twice.
  1812. SmallPtrSet<BasicBlock *, 16> OnPending;
  1813. LLVM_DEBUG(dbgs() << "Processing Worklist\n");
  1814. while (!Worklist.empty()) {
  1815. BasicBlock *BB = OrderToBB[Worklist.top()];
  1816. LLVM_DEBUG(dbgs() << "\nPop BB " << BB->getName() << "\n");
  1817. Worklist.pop();
  1818. bool InChanged = join(*BB, Visited);
  1819. // Always consider LiveIn changed on the first visit.
  1820. InChanged |= Visited.insert(BB).second;
  1821. if (InChanged) {
  1822. LLVM_DEBUG(dbgs() << BB->getName() << " has new InLocs, process it\n");
  1823. // Mutate a copy of LiveIn while processing BB. After calling process
  1824. // LiveSet is the LiveOut set for BB.
  1825. BlockInfo LiveSet = LiveIn[BB];
  1826. // Process the instructions in the block.
  1827. process(*BB, &LiveSet);
  1828. // Relatively expensive check: has anything changed in LiveOut for BB?
  1829. if (LiveOut[BB] != LiveSet) {
  1830. LLVM_DEBUG(dbgs() << BB->getName()
  1831. << " has new OutLocs, add succs to worklist: [ ");
  1832. LiveOut[BB] = std::move(LiveSet);
  1833. for (auto I = succ_begin(BB), E = succ_end(BB); I != E; I++) {
  1834. if (OnPending.insert(*I).second) {
  1835. LLVM_DEBUG(dbgs() << I->getName() << " ");
  1836. Pending.push(BBToOrder[*I]);
  1837. }
  1838. }
  1839. LLVM_DEBUG(dbgs() << "]\n");
  1840. }
  1841. }
  1842. }
  1843. Worklist.swap(Pending);
  1844. // At this point, pending must be empty, since it was just the empty
  1845. // worklist
  1846. assert(Pending.empty() && "Pending should be empty");
  1847. }
  1848. // That's the hard part over. Now we just have some admin to do.
  1849. // Record whether we inserted any intrinsics.
  1850. bool InsertedAnyIntrinsics = false;
  1851. // Identify and add defs for single location variables.
  1852. //
  1853. // Go through all of the defs that we plan to add. If the aggregate variable
  1854. // it's a part of is not in the NotAlwaysStackHomed set we can emit a single
  1855. // location def and omit the rest. Add an entry to AlwaysStackHomed so that
  1856. // we can identify those uneeded defs later.
  1857. DenseSet<DebugAggregate> AlwaysStackHomed;
  1858. for (const auto &Pair : InsertBeforeMap) {
  1859. const auto &Vec = Pair.second;
  1860. for (VarLocInfo VarLoc : Vec) {
  1861. DebugVariable Var = FnVarLocs->getVariable(VarLoc.VariableID);
  1862. DebugAggregate Aggr{Var.getVariable(), Var.getInlinedAt()};
  1863. // Skip this Var if it's not always stack homed.
  1864. if (NotAlwaysStackHomed.contains(Aggr))
  1865. continue;
  1866. // Skip complex cases such as when different fragments of a variable have
  1867. // been split into different allocas. Skipping in this case means falling
  1868. // back to using a list of defs (which could reduce coverage, but is no
  1869. // less correct).
  1870. bool Simple =
  1871. VarLoc.Expr->getNumElements() == 1 && VarLoc.Expr->startsWithDeref();
  1872. if (!Simple) {
  1873. NotAlwaysStackHomed.insert(Aggr);
  1874. continue;
  1875. }
  1876. // All source assignments to this variable remain and all stores to any
  1877. // part of the variable store to the same address (with varying
  1878. // offsets). We can just emit a single location for the whole variable.
  1879. //
  1880. // Unless we've already done so, create the single location def now.
  1881. if (AlwaysStackHomed.insert(Aggr).second) {
  1882. assert(isa<AllocaInst>(VarLoc.V));
  1883. // TODO: When more complex cases are handled VarLoc.Expr should be
  1884. // built appropriately rather than always using an empty DIExpression.
  1885. // The assert below is a reminder.
  1886. assert(Simple);
  1887. VarLoc.Expr = DIExpression::get(Fn.getContext(), std::nullopt);
  1888. DebugVariable Var = FnVarLocs->getVariable(VarLoc.VariableID);
  1889. FnVarLocs->addSingleLocVar(Var, VarLoc.Expr, VarLoc.DL, VarLoc.V);
  1890. InsertedAnyIntrinsics = true;
  1891. }
  1892. }
  1893. }
  1894. // Insert the other DEFs.
  1895. for (const auto &[InsertBefore, Vec] : InsertBeforeMap) {
  1896. SmallVector<VarLocInfo> NewDefs;
  1897. for (const VarLocInfo &VarLoc : Vec) {
  1898. DebugVariable Var = FnVarLocs->getVariable(VarLoc.VariableID);
  1899. DebugAggregate Aggr{Var.getVariable(), Var.getInlinedAt()};
  1900. // If this variable is always stack homed then we have already inserted a
  1901. // dbg.declare and deleted this dbg.value.
  1902. if (AlwaysStackHomed.contains(Aggr))
  1903. continue;
  1904. NewDefs.push_back(VarLoc);
  1905. InsertedAnyIntrinsics = true;
  1906. }
  1907. FnVarLocs->setWedge(InsertBefore, std::move(NewDefs));
  1908. }
  1909. InsertedAnyIntrinsics |= emitPromotedVarLocs(FnVarLocs);
  1910. return InsertedAnyIntrinsics;
  1911. }
  1912. bool AssignmentTrackingLowering::emitPromotedVarLocs(
  1913. FunctionVarLocsBuilder *FnVarLocs) {
  1914. bool InsertedAnyIntrinsics = false;
  1915. // Go through every block, translating debug intrinsics for fully promoted
  1916. // variables into FnVarLocs location defs. No analysis required for these.
  1917. for (auto &BB : Fn) {
  1918. for (auto &I : BB) {
  1919. // Skip instructions other than dbg.values and dbg.assigns.
  1920. auto *DVI = dyn_cast<DbgValueInst>(&I);
  1921. if (!DVI)
  1922. continue;
  1923. // Skip variables that haven't been promoted - we've dealt with those
  1924. // already.
  1925. if (VarsWithStackSlot->contains(getAggregate(DVI)))
  1926. continue;
  1927. // Wrapper to get a single value (or undef) from DVI.
  1928. auto GetValue = [DVI]() -> Value * {
  1929. // We can't handle variadic DIExpressions yet so treat those as
  1930. // kill locations.
  1931. if (DVI->isKillLocation() || DVI->getValue() == nullptr ||
  1932. DVI->hasArgList())
  1933. return PoisonValue::get(Type::getInt32Ty(DVI->getContext()));
  1934. return DVI->getValue();
  1935. };
  1936. Instruction *InsertBefore = I.getNextNode();
  1937. assert(InsertBefore && "Unexpected: debug intrinsics after a terminator");
  1938. FnVarLocs->addVarLoc(InsertBefore, DebugVariable(DVI),
  1939. DVI->getExpression(), DVI->getDebugLoc(),
  1940. GetValue());
  1941. InsertedAnyIntrinsics = true;
  1942. }
  1943. }
  1944. return InsertedAnyIntrinsics;
  1945. }
  1946. /// Remove redundant definitions within sequences of consecutive location defs.
  1947. /// This is done using a backward scan to keep the last def describing a
  1948. /// specific variable/fragment.
  1949. ///
  1950. /// This implements removeRedundantDbgInstrsUsingBackwardScan from
  1951. /// lib/Transforms/Utils/BasicBlockUtils.cpp for locations described with
  1952. /// FunctionVarLocsBuilder instead of with intrinsics.
  1953. static bool
  1954. removeRedundantDbgLocsUsingBackwardScan(const BasicBlock *BB,
  1955. FunctionVarLocsBuilder &FnVarLocs) {
  1956. bool Changed = false;
  1957. SmallDenseSet<DebugVariable> VariableSet;
  1958. // Scan over the entire block, not just over the instructions mapped by
  1959. // FnVarLocs, because wedges in FnVarLocs may only be seperated by debug
  1960. // instructions.
  1961. for (const Instruction &I : reverse(*BB)) {
  1962. if (!isa<DbgVariableIntrinsic>(I)) {
  1963. // Sequence of consecutive defs ended. Clear map for the next one.
  1964. VariableSet.clear();
  1965. }
  1966. // Get the location defs that start just before this instruction.
  1967. const auto *Locs = FnVarLocs.getWedge(&I);
  1968. if (!Locs)
  1969. continue;
  1970. NumWedgesScanned++;
  1971. bool ChangedThisWedge = false;
  1972. // The new pruned set of defs, reversed because we're scanning backwards.
  1973. SmallVector<VarLocInfo> NewDefsReversed;
  1974. // Iterate over the existing defs in reverse.
  1975. for (auto RIt = Locs->rbegin(), REnd = Locs->rend(); RIt != REnd; ++RIt) {
  1976. NumDefsScanned++;
  1977. const DebugVariable &Key = FnVarLocs.getVariable(RIt->VariableID);
  1978. bool FirstDefOfFragment = VariableSet.insert(Key).second;
  1979. // If the same variable fragment is described more than once it is enough
  1980. // to keep the last one (i.e. the first found in this reverse iteration).
  1981. if (FirstDefOfFragment) {
  1982. // New def found: keep it.
  1983. NewDefsReversed.push_back(*RIt);
  1984. } else {
  1985. // Redundant def found: throw it away. Since the wedge of defs is being
  1986. // rebuilt, doing nothing is the same as deleting an entry.
  1987. ChangedThisWedge = true;
  1988. NumDefsRemoved++;
  1989. }
  1990. continue;
  1991. }
  1992. // Un-reverse the defs and replace the wedge with the pruned version.
  1993. if (ChangedThisWedge) {
  1994. std::reverse(NewDefsReversed.begin(), NewDefsReversed.end());
  1995. FnVarLocs.setWedge(&I, std::move(NewDefsReversed));
  1996. NumWedgesChanged++;
  1997. Changed = true;
  1998. }
  1999. }
  2000. return Changed;
  2001. }
  2002. /// Remove redundant location defs using a forward scan. This can remove a
  2003. /// location definition that is redundant due to indicating that a variable has
  2004. /// the same value as is already being indicated by an earlier def.
  2005. ///
  2006. /// This implements removeRedundantDbgInstrsUsingForwardScan from
  2007. /// lib/Transforms/Utils/BasicBlockUtils.cpp for locations described with
  2008. /// FunctionVarLocsBuilder instead of with intrinsics
  2009. static bool
  2010. removeRedundantDbgLocsUsingForwardScan(const BasicBlock *BB,
  2011. FunctionVarLocsBuilder &FnVarLocs) {
  2012. bool Changed = false;
  2013. DenseMap<DebugVariable, std::pair<Value *, DIExpression *>> VariableMap;
  2014. // Scan over the entire block, not just over the instructions mapped by
  2015. // FnVarLocs, because wedges in FnVarLocs may only be seperated by debug
  2016. // instructions.
  2017. for (const Instruction &I : *BB) {
  2018. // Get the defs that come just before this instruction.
  2019. const auto *Locs = FnVarLocs.getWedge(&I);
  2020. if (!Locs)
  2021. continue;
  2022. NumWedgesScanned++;
  2023. bool ChangedThisWedge = false;
  2024. // The new pruned set of defs.
  2025. SmallVector<VarLocInfo> NewDefs;
  2026. // Iterate over the existing defs.
  2027. for (const VarLocInfo &Loc : *Locs) {
  2028. NumDefsScanned++;
  2029. DebugVariable Key(FnVarLocs.getVariable(Loc.VariableID).getVariable(),
  2030. std::nullopt, Loc.DL.getInlinedAt());
  2031. auto VMI = VariableMap.find(Key);
  2032. // Update the map if we found a new value/expression describing the
  2033. // variable, or if the variable wasn't mapped already.
  2034. if (VMI == VariableMap.end() || VMI->second.first != Loc.V ||
  2035. VMI->second.second != Loc.Expr) {
  2036. VariableMap[Key] = {Loc.V, Loc.Expr};
  2037. NewDefs.push_back(Loc);
  2038. continue;
  2039. }
  2040. // Did not insert this Loc, which is the same as removing it.
  2041. ChangedThisWedge = true;
  2042. NumDefsRemoved++;
  2043. }
  2044. // Replace the existing wedge with the pruned version.
  2045. if (ChangedThisWedge) {
  2046. FnVarLocs.setWedge(&I, std::move(NewDefs));
  2047. NumWedgesChanged++;
  2048. Changed = true;
  2049. }
  2050. }
  2051. return Changed;
  2052. }
  2053. static bool
  2054. removeUndefDbgLocsFromEntryBlock(const BasicBlock *BB,
  2055. FunctionVarLocsBuilder &FnVarLocs) {
  2056. assert(BB->isEntryBlock());
  2057. // Do extra work to ensure that we remove semantically unimportant undefs.
  2058. //
  2059. // This is to work around the fact that SelectionDAG will hoist dbg.values
  2060. // using argument values to the top of the entry block. That can move arg
  2061. // dbg.values before undef and constant dbg.values which they previously
  2062. // followed. The easiest thing to do is to just try to feed SelectionDAG
  2063. // input it's happy with.
  2064. //
  2065. // Map of {Variable x: Fragments y} where the fragments y of variable x have
  2066. // have at least one non-undef location defined already. Don't use directly,
  2067. // instead call DefineBits and HasDefinedBits.
  2068. SmallDenseMap<DebugAggregate, SmallDenseSet<DIExpression::FragmentInfo>>
  2069. VarsWithDef;
  2070. // Specify that V (a fragment of A) has a non-undef location.
  2071. auto DefineBits = [&VarsWithDef](DebugAggregate A, DebugVariable V) {
  2072. VarsWithDef[A].insert(V.getFragmentOrDefault());
  2073. };
  2074. // Return true if a non-undef location has been defined for V (a fragment of
  2075. // A). Doesn't imply that the location is currently non-undef, just that a
  2076. // non-undef location has been seen previously.
  2077. auto HasDefinedBits = [&VarsWithDef](DebugAggregate A, DebugVariable V) {
  2078. auto FragsIt = VarsWithDef.find(A);
  2079. if (FragsIt == VarsWithDef.end())
  2080. return false;
  2081. return llvm::any_of(FragsIt->second, [V](auto Frag) {
  2082. return DIExpression::fragmentsOverlap(Frag, V.getFragmentOrDefault());
  2083. });
  2084. };
  2085. bool Changed = false;
  2086. DenseMap<DebugVariable, std::pair<Value *, DIExpression *>> VariableMap;
  2087. // Scan over the entire block, not just over the instructions mapped by
  2088. // FnVarLocs, because wedges in FnVarLocs may only be seperated by debug
  2089. // instructions.
  2090. for (const Instruction &I : *BB) {
  2091. // Get the defs that come just before this instruction.
  2092. const auto *Locs = FnVarLocs.getWedge(&I);
  2093. if (!Locs)
  2094. continue;
  2095. NumWedgesScanned++;
  2096. bool ChangedThisWedge = false;
  2097. // The new pruned set of defs.
  2098. SmallVector<VarLocInfo> NewDefs;
  2099. // Iterate over the existing defs.
  2100. for (const VarLocInfo &Loc : *Locs) {
  2101. NumDefsScanned++;
  2102. DebugAggregate Aggr{FnVarLocs.getVariable(Loc.VariableID).getVariable(),
  2103. Loc.DL.getInlinedAt()};
  2104. DebugVariable Var = FnVarLocs.getVariable(Loc.VariableID);
  2105. // Remove undef entries that are encountered before any non-undef
  2106. // intrinsics from the entry block.
  2107. if (isa<UndefValue>(Loc.V) && !HasDefinedBits(Aggr, Var)) {
  2108. // Did not insert this Loc, which is the same as removing it.
  2109. NumDefsRemoved++;
  2110. ChangedThisWedge = true;
  2111. continue;
  2112. }
  2113. DefineBits(Aggr, Var);
  2114. NewDefs.push_back(Loc);
  2115. }
  2116. // Replace the existing wedge with the pruned version.
  2117. if (ChangedThisWedge) {
  2118. FnVarLocs.setWedge(&I, std::move(NewDefs));
  2119. NumWedgesChanged++;
  2120. Changed = true;
  2121. }
  2122. }
  2123. return Changed;
  2124. }
  2125. static bool removeRedundantDbgLocs(const BasicBlock *BB,
  2126. FunctionVarLocsBuilder &FnVarLocs) {
  2127. bool MadeChanges = false;
  2128. MadeChanges |= removeRedundantDbgLocsUsingBackwardScan(BB, FnVarLocs);
  2129. if (BB->isEntryBlock())
  2130. MadeChanges |= removeUndefDbgLocsFromEntryBlock(BB, FnVarLocs);
  2131. MadeChanges |= removeRedundantDbgLocsUsingForwardScan(BB, FnVarLocs);
  2132. if (MadeChanges)
  2133. LLVM_DEBUG(dbgs() << "Removed redundant dbg locs from: " << BB->getName()
  2134. << "\n");
  2135. return MadeChanges;
  2136. }
  2137. static DenseSet<DebugAggregate> findVarsWithStackSlot(Function &Fn) {
  2138. DenseSet<DebugAggregate> Result;
  2139. for (auto &BB : Fn) {
  2140. for (auto &I : BB) {
  2141. // Any variable linked to an instruction is considered
  2142. // interesting. Ideally we only need to check Allocas, however, a
  2143. // DIAssignID might get dropped from an alloca but not stores. In that
  2144. // case, we need to consider the variable interesting for NFC behaviour
  2145. // with this change. TODO: Consider only looking at allocas.
  2146. for (DbgAssignIntrinsic *DAI : at::getAssignmentMarkers(&I)) {
  2147. Result.insert({DAI->getVariable(), DAI->getDebugLoc().getInlinedAt()});
  2148. }
  2149. }
  2150. }
  2151. return Result;
  2152. }
  2153. static void analyzeFunction(Function &Fn, const DataLayout &Layout,
  2154. FunctionVarLocsBuilder *FnVarLocs) {
  2155. // The analysis will generate location definitions for all variables, but we
  2156. // only need to perform a dataflow on the set of variables which have a stack
  2157. // slot. Find those now.
  2158. DenseSet<DebugAggregate> VarsWithStackSlot = findVarsWithStackSlot(Fn);
  2159. bool Changed = false;
  2160. // Use a scope block to clean up AssignmentTrackingLowering before running
  2161. // MemLocFragmentFill to reduce peak memory consumption.
  2162. {
  2163. AssignmentTrackingLowering Pass(Fn, Layout, &VarsWithStackSlot);
  2164. Changed = Pass.run(FnVarLocs);
  2165. }
  2166. if (Changed) {
  2167. MemLocFragmentFill Pass(Fn, &VarsWithStackSlot);
  2168. Pass.run(FnVarLocs);
  2169. // Remove redundant entries. As well as reducing memory consumption and
  2170. // avoiding waiting cycles later by burning some now, this has another
  2171. // important job. That is to work around some SelectionDAG quirks. See
  2172. // removeRedundantDbgLocsUsingForwardScan comments for more info on that.
  2173. for (auto &BB : Fn)
  2174. removeRedundantDbgLocs(&BB, *FnVarLocs);
  2175. }
  2176. }
  2177. bool AssignmentTrackingAnalysis::runOnFunction(Function &F) {
  2178. if (!isAssignmentTrackingEnabled(*F.getParent()))
  2179. return false;
  2180. LLVM_DEBUG(dbgs() << "AssignmentTrackingAnalysis run on " << F.getName()
  2181. << "\n");
  2182. auto DL = std::make_unique<DataLayout>(F.getParent());
  2183. // Clear previous results.
  2184. Results->clear();
  2185. FunctionVarLocsBuilder Builder;
  2186. analyzeFunction(F, *DL.get(), &Builder);
  2187. // Save these results.
  2188. Results->init(Builder);
  2189. if (PrintResults && isFunctionInPrintList(F.getName()))
  2190. Results->print(errs(), F);
  2191. // Return false because this pass does not modify the function.
  2192. return false;
  2193. }
  2194. AssignmentTrackingAnalysis::AssignmentTrackingAnalysis()
  2195. : FunctionPass(ID), Results(std::make_unique<FunctionVarLocs>()) {}
  2196. char AssignmentTrackingAnalysis::ID = 0;
  2197. INITIALIZE_PASS(AssignmentTrackingAnalysis, DEBUG_TYPE,
  2198. "Assignment Tracking Analysis", false, true)