RewriteStatepointsForGC.cpp 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673
  1. //===- RewriteStatepointsForGC.cpp - Make GC relocations explicit ---------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Rewrite an existing set of gc.statepoints such that they make potential
  11. // relocations performed by the garbage collector explicit in the IR.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Pass.h"
  15. #include "llvm/Analysis/CFG.h"
  16. #include "llvm/Analysis/TargetTransformInfo.h"
  17. #include "llvm/ADT/SetOperations.h"
  18. #include "llvm/ADT/Statistic.h"
  19. #include "llvm/ADT/DenseSet.h"
  20. #include "llvm/ADT/SetVector.h"
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/IR/BasicBlock.h"
  23. #include "llvm/IR/CallSite.h"
  24. #include "llvm/IR/Dominators.h"
  25. #include "llvm/IR/Function.h"
  26. #include "llvm/IR/IRBuilder.h"
  27. #include "llvm/IR/InstIterator.h"
  28. #include "llvm/IR/Instructions.h"
  29. #include "llvm/IR/Intrinsics.h"
  30. #include "llvm/IR/IntrinsicInst.h"
  31. #include "llvm/IR/Module.h"
  32. #include "llvm/IR/MDBuilder.h"
  33. #include "llvm/IR/Statepoint.h"
  34. #include "llvm/IR/Value.h"
  35. #include "llvm/IR/Verifier.h"
  36. #include "llvm/Support/Debug.h"
  37. #include "llvm/Support/CommandLine.h"
  38. #include "llvm/Transforms/Scalar.h"
  39. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  40. #include "llvm/Transforms/Utils/Cloning.h"
  41. #include "llvm/Transforms/Utils/Local.h"
  42. #include "llvm/Transforms/Utils/PromoteMemToReg.h"
  43. #define DEBUG_TYPE "rewrite-statepoints-for-gc"
  44. using namespace llvm;
  45. // Print tracing output
  46. static cl::opt<bool> TraceLSP("trace-rewrite-statepoints", cl::Hidden,
  47. cl::init(false));
  48. // Print the liveset found at the insert location
  49. static cl::opt<bool> PrintLiveSet("spp-print-liveset", cl::Hidden,
  50. cl::init(false));
  51. static cl::opt<bool> PrintLiveSetSize("spp-print-liveset-size", cl::Hidden,
  52. cl::init(false));
  53. // Print out the base pointers for debugging
  54. static cl::opt<bool> PrintBasePointers("spp-print-base-pointers", cl::Hidden,
  55. cl::init(false));
  56. // Cost threshold measuring when it is profitable to rematerialize value instead
  57. // of relocating it
  58. static cl::opt<unsigned>
  59. RematerializationThreshold("spp-rematerialization-threshold", cl::Hidden,
  60. cl::init(6));
  61. #ifdef XDEBUG
  62. static bool ClobberNonLive = true;
  63. #else
  64. static bool ClobberNonLive = false;
  65. #endif
  66. static cl::opt<bool, true> ClobberNonLiveOverride("rs4gc-clobber-non-live",
  67. cl::location(ClobberNonLive),
  68. cl::Hidden);
  69. namespace {
  70. struct RewriteStatepointsForGC : public ModulePass {
  71. static char ID; // Pass identification, replacement for typeid
  72. RewriteStatepointsForGC() : ModulePass(ID) {
  73. initializeRewriteStatepointsForGCPass(*PassRegistry::getPassRegistry());
  74. }
  75. bool runOnFunction(Function &F);
  76. bool runOnModule(Module &M) override {
  77. bool Changed = false;
  78. for (Function &F : M)
  79. Changed |= runOnFunction(F);
  80. if (Changed) {
  81. // stripDereferenceabilityInfo asserts that shouldRewriteStatepointsIn
  82. // returns true for at least one function in the module. Since at least
  83. // one function changed, we know that the precondition is satisfied.
  84. stripDereferenceabilityInfo(M);
  85. }
  86. return Changed;
  87. }
  88. void getAnalysisUsage(AnalysisUsage &AU) const override {
  89. // We add and rewrite a bunch of instructions, but don't really do much
  90. // else. We could in theory preserve a lot more analyses here.
  91. AU.addRequired<DominatorTreeWrapperPass>();
  92. AU.addRequired<TargetTransformInfoWrapperPass>();
  93. }
  94. /// The IR fed into RewriteStatepointsForGC may have had attributes implying
  95. /// dereferenceability that are no longer valid/correct after
  96. /// RewriteStatepointsForGC has run. This is because semantically, after
  97. /// RewriteStatepointsForGC runs, all calls to gc.statepoint "free" the entire
  98. /// heap. stripDereferenceabilityInfo (conservatively) restores correctness
  99. /// by erasing all attributes in the module that externally imply
  100. /// dereferenceability.
  101. ///
  102. void stripDereferenceabilityInfo(Module &M);
  103. // Helpers for stripDereferenceabilityInfo
  104. void stripDereferenceabilityInfoFromBody(Function &F);
  105. void stripDereferenceabilityInfoFromPrototype(Function &F);
  106. };
  107. } // namespace
  108. char RewriteStatepointsForGC::ID = 0;
  109. ModulePass *llvm::createRewriteStatepointsForGCPass() {
  110. return new RewriteStatepointsForGC();
  111. }
  112. INITIALIZE_PASS_BEGIN(RewriteStatepointsForGC, "rewrite-statepoints-for-gc",
  113. "Make relocations explicit at statepoints", false, false)
  114. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  115. INITIALIZE_PASS_END(RewriteStatepointsForGC, "rewrite-statepoints-for-gc",
  116. "Make relocations explicit at statepoints", false, false)
  117. namespace {
  118. struct GCPtrLivenessData {
  119. /// Values defined in this block.
  120. DenseMap<BasicBlock *, DenseSet<Value *>> KillSet;
  121. /// Values used in this block (and thus live); does not included values
  122. /// killed within this block.
  123. DenseMap<BasicBlock *, DenseSet<Value *>> LiveSet;
  124. /// Values live into this basic block (i.e. used by any
  125. /// instruction in this basic block or ones reachable from here)
  126. DenseMap<BasicBlock *, DenseSet<Value *>> LiveIn;
  127. /// Values live out of this basic block (i.e. live into
  128. /// any successor block)
  129. DenseMap<BasicBlock *, DenseSet<Value *>> LiveOut;
  130. };
  131. // The type of the internal cache used inside the findBasePointers family
  132. // of functions. From the callers perspective, this is an opaque type and
  133. // should not be inspected.
  134. //
  135. // In the actual implementation this caches two relations:
  136. // - The base relation itself (i.e. this pointer is based on that one)
  137. // - The base defining value relation (i.e. before base_phi insertion)
  138. // Generally, after the execution of a full findBasePointer call, only the
  139. // base relation will remain. Internally, we add a mixture of the two
  140. // types, then update all the second type to the first type
  141. typedef DenseMap<Value *, Value *> DefiningValueMapTy;
  142. typedef DenseSet<llvm::Value *> StatepointLiveSetTy;
  143. typedef DenseMap<Instruction *, Value *> RematerializedValueMapTy;
  144. struct PartiallyConstructedSafepointRecord {
  145. /// The set of values known to be live accross this safepoint
  146. StatepointLiveSetTy liveset;
  147. /// Mapping from live pointers to a base-defining-value
  148. DenseMap<llvm::Value *, llvm::Value *> PointerToBase;
  149. /// The *new* gc.statepoint instruction itself. This produces the token
  150. /// that normal path gc.relocates and the gc.result are tied to.
  151. Instruction *StatepointToken;
  152. /// Instruction to which exceptional gc relocates are attached
  153. /// Makes it easier to iterate through them during relocationViaAlloca.
  154. Instruction *UnwindToken;
  155. /// Record live values we are rematerialized instead of relocating.
  156. /// They are not included into 'liveset' field.
  157. /// Maps rematerialized copy to it's original value.
  158. RematerializedValueMapTy RematerializedValues;
  159. };
  160. }
  161. /// Compute the live-in set for every basic block in the function
  162. static void computeLiveInValues(DominatorTree &DT, Function &F,
  163. GCPtrLivenessData &Data);
  164. /// Given results from the dataflow liveness computation, find the set of live
  165. /// Values at a particular instruction.
  166. static void findLiveSetAtInst(Instruction *inst, GCPtrLivenessData &Data,
  167. StatepointLiveSetTy &out);
  168. // TODO: Once we can get to the GCStrategy, this becomes
  169. // Optional<bool> isGCManagedPointer(const Value *V) const override {
  170. static bool isGCPointerType(const Type *T) {
  171. if (const PointerType *PT = dyn_cast<PointerType>(T))
  172. // For the sake of this example GC, we arbitrarily pick addrspace(1) as our
  173. // GC managed heap. We know that a pointer into this heap needs to be
  174. // updated and that no other pointer does.
  175. return (1 == PT->getAddressSpace());
  176. return false;
  177. }
  178. // Return true if this type is one which a) is a gc pointer or contains a GC
  179. // pointer and b) is of a type this code expects to encounter as a live value.
  180. // (The insertion code will assert that a type which matches (a) and not (b)
  181. // is not encountered.)
  182. static bool isHandledGCPointerType(Type *T) {
  183. // We fully support gc pointers
  184. if (isGCPointerType(T))
  185. return true;
  186. // We partially support vectors of gc pointers. The code will assert if it
  187. // can't handle something.
  188. if (auto VT = dyn_cast<VectorType>(T))
  189. if (isGCPointerType(VT->getElementType()))
  190. return true;
  191. return false;
  192. }
  193. #ifndef NDEBUG
  194. /// Returns true if this type contains a gc pointer whether we know how to
  195. /// handle that type or not.
  196. static bool containsGCPtrType(Type *Ty) {
  197. if (isGCPointerType(Ty))
  198. return true;
  199. if (VectorType *VT = dyn_cast<VectorType>(Ty))
  200. return isGCPointerType(VT->getScalarType());
  201. if (ArrayType *AT = dyn_cast<ArrayType>(Ty))
  202. return containsGCPtrType(AT->getElementType());
  203. if (StructType *ST = dyn_cast<StructType>(Ty))
  204. return std::any_of(
  205. ST->subtypes().begin(), ST->subtypes().end(),
  206. [](Type *SubType) { return containsGCPtrType(SubType); });
  207. return false;
  208. }
  209. // Returns true if this is a type which a) is a gc pointer or contains a GC
  210. // pointer and b) is of a type which the code doesn't expect (i.e. first class
  211. // aggregates). Used to trip assertions.
  212. static bool isUnhandledGCPointerType(Type *Ty) {
  213. return containsGCPtrType(Ty) && !isHandledGCPointerType(Ty);
  214. }
  215. #endif
  216. static bool order_by_name(llvm::Value *a, llvm::Value *b) {
  217. if (a->hasName() && b->hasName()) {
  218. return -1 == a->getName().compare(b->getName());
  219. } else if (a->hasName() && !b->hasName()) {
  220. return true;
  221. } else if (!a->hasName() && b->hasName()) {
  222. return false;
  223. } else {
  224. // Better than nothing, but not stable
  225. return a < b;
  226. }
  227. }
  228. // Conservatively identifies any definitions which might be live at the
  229. // given instruction. The analysis is performed immediately before the
  230. // given instruction. Values defined by that instruction are not considered
  231. // live. Values used by that instruction are considered live.
  232. static void analyzeParsePointLiveness(
  233. DominatorTree &DT, GCPtrLivenessData &OriginalLivenessData,
  234. const CallSite &CS, PartiallyConstructedSafepointRecord &result) {
  235. Instruction *inst = CS.getInstruction();
  236. StatepointLiveSetTy liveset;
  237. findLiveSetAtInst(inst, OriginalLivenessData, liveset);
  238. if (PrintLiveSet) {
  239. // Note: This output is used by several of the test cases
  240. // The order of elemtns in a set is not stable, put them in a vec and sort
  241. // by name
  242. SmallVector<Value *, 64> temp;
  243. temp.insert(temp.end(), liveset.begin(), liveset.end());
  244. std::sort(temp.begin(), temp.end(), order_by_name);
  245. errs() << "Live Variables:\n";
  246. for (Value *V : temp) {
  247. errs() << " " << V->getName(); // no newline
  248. V->dump();
  249. }
  250. }
  251. if (PrintLiveSetSize) {
  252. errs() << "Safepoint For: " << CS.getCalledValue()->getName() << "\n";
  253. errs() << "Number live values: " << liveset.size() << "\n";
  254. }
  255. result.liveset = liveset;
  256. }
  257. static Value *findBaseDefiningValue(Value *I);
  258. /// Return a base defining value for the 'Index' element of the given vector
  259. /// instruction 'I'. If Index is null, returns a BDV for the entire vector
  260. /// 'I'. As an optimization, this method will try to determine when the
  261. /// element is known to already be a base pointer. If this can be established,
  262. /// the second value in the returned pair will be true. Note that either a
  263. /// vector or a pointer typed value can be returned. For the former, the
  264. /// vector returned is a BDV (and possibly a base) of the entire vector 'I'.
  265. /// If the later, the return pointer is a BDV (or possibly a base) for the
  266. /// particular element in 'I'.
  267. static std::pair<Value *, bool>
  268. findBaseDefiningValueOfVector(Value *I, Value *Index = nullptr) {
  269. assert(I->getType()->isVectorTy() &&
  270. cast<VectorType>(I->getType())->getElementType()->isPointerTy() &&
  271. "Illegal to ask for the base pointer of a non-pointer type");
  272. // Each case parallels findBaseDefiningValue below, see that code for
  273. // detailed motivation.
  274. if (isa<Argument>(I))
  275. // An incoming argument to the function is a base pointer
  276. return std::make_pair(I, true);
  277. // We shouldn't see the address of a global as a vector value?
  278. assert(!isa<GlobalVariable>(I) &&
  279. "unexpected global variable found in base of vector");
  280. // inlining could possibly introduce phi node that contains
  281. // undef if callee has multiple returns
  282. if (isa<UndefValue>(I))
  283. // utterly meaningless, but useful for dealing with partially optimized
  284. // code.
  285. return std::make_pair(I, true);
  286. // Due to inheritance, this must be _after_ the global variable and undef
  287. // checks
  288. if (Constant *Con = dyn_cast<Constant>(I)) {
  289. assert(!isa<GlobalVariable>(I) && !isa<UndefValue>(I) &&
  290. "order of checks wrong!");
  291. assert(Con->isNullValue() && "null is the only case which makes sense");
  292. return std::make_pair(Con, true);
  293. }
  294. if (isa<LoadInst>(I))
  295. return std::make_pair(I, true);
  296. // For an insert element, we might be able to look through it if we know
  297. // something about the indexes.
  298. if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(I)) {
  299. if (Index) {
  300. Value *InsertIndex = IEI->getOperand(2);
  301. // This index is inserting the value, look for its BDV
  302. if (InsertIndex == Index)
  303. return std::make_pair(findBaseDefiningValue(IEI->getOperand(1)), false);
  304. // Both constant, and can't be equal per above. This insert is definitely
  305. // not relevant, look back at the rest of the vector and keep trying.
  306. if (isa<ConstantInt>(Index) && isa<ConstantInt>(InsertIndex))
  307. return findBaseDefiningValueOfVector(IEI->getOperand(0), Index);
  308. }
  309. // We don't know whether this vector contains entirely base pointers or
  310. // not. To be conservatively correct, we treat it as a BDV and will
  311. // duplicate code as needed to construct a parallel vector of bases.
  312. return std::make_pair(IEI, false);
  313. }
  314. if (isa<ShuffleVectorInst>(I))
  315. // We don't know whether this vector contains entirely base pointers or
  316. // not. To be conservatively correct, we treat it as a BDV and will
  317. // duplicate code as needed to construct a parallel vector of bases.
  318. // TODO: There a number of local optimizations which could be applied here
  319. // for particular sufflevector patterns.
  320. return std::make_pair(I, false);
  321. // A PHI or Select is a base defining value. The outer findBasePointer
  322. // algorithm is responsible for constructing a base value for this BDV.
  323. assert((isa<SelectInst>(I) || isa<PHINode>(I)) &&
  324. "unknown vector instruction - no base found for vector element");
  325. return std::make_pair(I, false);
  326. }
  327. static bool isKnownBaseResult(Value *V);
  328. /// Helper function for findBasePointer - Will return a value which either a)
  329. /// defines the base pointer for the input or b) blocks the simple search
  330. /// (i.e. a PHI or Select of two derived pointers)
  331. static Value *findBaseDefiningValue(Value *I) {
  332. if (I->getType()->isVectorTy())
  333. return findBaseDefiningValueOfVector(I).first;
  334. assert(I->getType()->isPointerTy() &&
  335. "Illegal to ask for the base pointer of a non-pointer type");
  336. // This case is a bit of a hack - it only handles extracts from vectors which
  337. // trivially contain only base pointers or cases where we can directly match
  338. // the index of the original extract element to an insertion into the vector.
  339. // See note inside the function for how to improve this.
  340. if (auto *EEI = dyn_cast<ExtractElementInst>(I)) {
  341. Value *VectorOperand = EEI->getVectorOperand();
  342. Value *Index = EEI->getIndexOperand();
  343. std::pair<Value *, bool> pair =
  344. findBaseDefiningValueOfVector(VectorOperand, Index);
  345. Value *VectorBase = pair.first;
  346. if (VectorBase->getType()->isPointerTy())
  347. // We found a BDV for this specific element with the vector. This is an
  348. // optimization, but in practice it covers most of the useful cases
  349. // created via scalarization.
  350. return VectorBase;
  351. else {
  352. assert(VectorBase->getType()->isVectorTy());
  353. if (pair.second)
  354. // If the entire vector returned is known to be entirely base pointers,
  355. // then the extractelement is valid base for this value.
  356. return EEI;
  357. else {
  358. // Otherwise, we have an instruction which potentially produces a
  359. // derived pointer and we need findBasePointers to clone code for us
  360. // such that we can create an instruction which produces the
  361. // accompanying base pointer.
  362. // Note: This code is currently rather incomplete. We don't currently
  363. // support the general form of shufflevector of insertelement.
  364. // Conceptually, these are just 'base defining values' of the same
  365. // variety as phi or select instructions. We need to update the
  366. // findBasePointers algorithm to insert new 'base-only' versions of the
  367. // original instructions. This is relative straight forward to do, but
  368. // the case which would motivate the work hasn't shown up in real
  369. // workloads yet.
  370. assert((isa<PHINode>(VectorBase) || isa<SelectInst>(VectorBase)) &&
  371. "need to extend findBasePointers for generic vector"
  372. "instruction cases");
  373. return VectorBase;
  374. }
  375. }
  376. }
  377. if (isa<Argument>(I))
  378. // An incoming argument to the function is a base pointer
  379. // We should have never reached here if this argument isn't an gc value
  380. return I;
  381. if (isa<GlobalVariable>(I))
  382. // base case
  383. return I;
  384. // inlining could possibly introduce phi node that contains
  385. // undef if callee has multiple returns
  386. if (isa<UndefValue>(I))
  387. // utterly meaningless, but useful for dealing with
  388. // partially optimized code.
  389. return I;
  390. // Due to inheritance, this must be _after_ the global variable and undef
  391. // checks
  392. if (Constant *Con = dyn_cast<Constant>(I)) {
  393. assert(!isa<GlobalVariable>(I) && !isa<UndefValue>(I) &&
  394. "order of checks wrong!");
  395. // Note: Finding a constant base for something marked for relocation
  396. // doesn't really make sense. The most likely case is either a) some
  397. // screwed up the address space usage or b) your validating against
  398. // compiled C++ code w/o the proper separation. The only real exception
  399. // is a null pointer. You could have generic code written to index of
  400. // off a potentially null value and have proven it null. We also use
  401. // null pointers in dead paths of relocation phis (which we might later
  402. // want to find a base pointer for).
  403. assert(isa<ConstantPointerNull>(Con) &&
  404. "null is the only case which makes sense");
  405. return Con;
  406. }
  407. if (CastInst *CI = dyn_cast<CastInst>(I)) {
  408. Value *Def = CI->stripPointerCasts();
  409. // If we find a cast instruction here, it means we've found a cast which is
  410. // not simply a pointer cast (i.e. an inttoptr). We don't know how to
  411. // handle int->ptr conversion.
  412. assert(!isa<CastInst>(Def) && "shouldn't find another cast here");
  413. return findBaseDefiningValue(Def);
  414. }
  415. if (isa<LoadInst>(I))
  416. return I; // The value loaded is an gc base itself
  417. if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I))
  418. // The base of this GEP is the base
  419. return findBaseDefiningValue(GEP->getPointerOperand());
  420. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
  421. switch (II->getIntrinsicID()) {
  422. case Intrinsic::experimental_gc_result_ptr:
  423. default:
  424. // fall through to general call handling
  425. break;
  426. case Intrinsic::experimental_gc_statepoint:
  427. case Intrinsic::experimental_gc_result_float:
  428. case Intrinsic::experimental_gc_result_int:
  429. llvm_unreachable("these don't produce pointers");
  430. case Intrinsic::experimental_gc_relocate: {
  431. // Rerunning safepoint insertion after safepoints are already
  432. // inserted is not supported. It could probably be made to work,
  433. // but why are you doing this? There's no good reason.
  434. llvm_unreachable("repeat safepoint insertion is not supported");
  435. }
  436. case Intrinsic::gcroot:
  437. // Currently, this mechanism hasn't been extended to work with gcroot.
  438. // There's no reason it couldn't be, but I haven't thought about the
  439. // implications much.
  440. llvm_unreachable(
  441. "interaction with the gcroot mechanism is not supported");
  442. }
  443. }
  444. // We assume that functions in the source language only return base
  445. // pointers. This should probably be generalized via attributes to support
  446. // both source language and internal functions.
  447. if (isa<CallInst>(I) || isa<InvokeInst>(I))
  448. return I;
  449. // I have absolutely no idea how to implement this part yet. It's not
  450. // neccessarily hard, I just haven't really looked at it yet.
  451. assert(!isa<LandingPadInst>(I) && "Landing Pad is unimplemented");
  452. if (isa<AtomicCmpXchgInst>(I))
  453. // A CAS is effectively a atomic store and load combined under a
  454. // predicate. From the perspective of base pointers, we just treat it
  455. // like a load.
  456. return I;
  457. assert(!isa<AtomicRMWInst>(I) && "Xchg handled above, all others are "
  458. "binary ops which don't apply to pointers");
  459. // The aggregate ops. Aggregates can either be in the heap or on the
  460. // stack, but in either case, this is simply a field load. As a result,
  461. // this is a defining definition of the base just like a load is.
  462. if (isa<ExtractValueInst>(I))
  463. return I;
  464. // We should never see an insert vector since that would require we be
  465. // tracing back a struct value not a pointer value.
  466. assert(!isa<InsertValueInst>(I) &&
  467. "Base pointer for a struct is meaningless");
  468. // The last two cases here don't return a base pointer. Instead, they
  469. // return a value which dynamically selects from amoung several base
  470. // derived pointers (each with it's own base potentially). It's the job of
  471. // the caller to resolve these.
  472. assert((isa<SelectInst>(I) || isa<PHINode>(I)) &&
  473. "missing instruction case in findBaseDefiningValing");
  474. return I;
  475. }
  476. /// Returns the base defining value for this value.
  477. static Value *findBaseDefiningValueCached(Value *I, DefiningValueMapTy &Cache) {
  478. Value *&Cached = Cache[I];
  479. if (!Cached) {
  480. Cached = findBaseDefiningValue(I);
  481. }
  482. assert(Cache[I] != nullptr);
  483. if (TraceLSP) {
  484. dbgs() << "fBDV-cached: " << I->getName() << " -> " << Cached->getName()
  485. << "\n";
  486. }
  487. return Cached;
  488. }
  489. /// Return a base pointer for this value if known. Otherwise, return it's
  490. /// base defining value.
  491. static Value *findBaseOrBDV(Value *I, DefiningValueMapTy &Cache) {
  492. Value *Def = findBaseDefiningValueCached(I, Cache);
  493. auto Found = Cache.find(Def);
  494. if (Found != Cache.end()) {
  495. // Either a base-of relation, or a self reference. Caller must check.
  496. return Found->second;
  497. }
  498. // Only a BDV available
  499. return Def;
  500. }
  501. /// Given the result of a call to findBaseDefiningValue, or findBaseOrBDV,
  502. /// is it known to be a base pointer? Or do we need to continue searching.
  503. static bool isKnownBaseResult(Value *V) {
  504. if (!isa<PHINode>(V) && !isa<SelectInst>(V)) {
  505. // no recursion possible
  506. return true;
  507. }
  508. if (isa<Instruction>(V) &&
  509. cast<Instruction>(V)->getMetadata("is_base_value")) {
  510. // This is a previously inserted base phi or select. We know
  511. // that this is a base value.
  512. return true;
  513. }
  514. // We need to keep searching
  515. return false;
  516. }
  517. // TODO: find a better name for this
  518. namespace {
  519. class PhiState {
  520. public:
  521. enum Status { Unknown, Base, Conflict };
  522. PhiState(Status s, Value *b = nullptr) : status(s), base(b) {
  523. assert(status != Base || b);
  524. }
  525. PhiState(Value *b) : status(Base), base(b) {}
  526. PhiState() : status(Unknown), base(nullptr) {}
  527. Status getStatus() const { return status; }
  528. Value *getBase() const { return base; }
  529. bool isBase() const { return getStatus() == Base; }
  530. bool isUnknown() const { return getStatus() == Unknown; }
  531. bool isConflict() const { return getStatus() == Conflict; }
  532. bool operator==(const PhiState &other) const {
  533. return base == other.base && status == other.status;
  534. }
  535. bool operator!=(const PhiState &other) const { return !(*this == other); }
  536. void dump() {
  537. errs() << status << " (" << base << " - "
  538. << (base ? base->getName() : "nullptr") << "): ";
  539. }
  540. private:
  541. Status status;
  542. Value *base; // non null only if status == base
  543. };
  544. typedef DenseMap<Value *, PhiState> ConflictStateMapTy;
  545. // Values of type PhiState form a lattice, and this is a helper
  546. // class that implementes the meet operation. The meat of the meet
  547. // operation is implemented in MeetPhiStates::pureMeet
  548. class MeetPhiStates {
  549. public:
  550. // phiStates is a mapping from PHINodes and SelectInst's to PhiStates.
  551. explicit MeetPhiStates(const ConflictStateMapTy &phiStates)
  552. : phiStates(phiStates) {}
  553. // Destructively meet the current result with the base V. V can
  554. // either be a merge instruction (SelectInst / PHINode), in which
  555. // case its status is looked up in the phiStates map; or a regular
  556. // SSA value, in which case it is assumed to be a base.
  557. void meetWith(Value *V) {
  558. PhiState otherState = getStateForBDV(V);
  559. assert((MeetPhiStates::pureMeet(otherState, currentResult) ==
  560. MeetPhiStates::pureMeet(currentResult, otherState)) &&
  561. "math is wrong: meet does not commute!");
  562. currentResult = MeetPhiStates::pureMeet(otherState, currentResult);
  563. }
  564. PhiState getResult() const { return currentResult; }
  565. private:
  566. const ConflictStateMapTy &phiStates;
  567. PhiState currentResult;
  568. /// Return a phi state for a base defining value. We'll generate a new
  569. /// base state for known bases and expect to find a cached state otherwise
  570. PhiState getStateForBDV(Value *baseValue) {
  571. if (isKnownBaseResult(baseValue)) {
  572. return PhiState(baseValue);
  573. } else {
  574. return lookupFromMap(baseValue);
  575. }
  576. }
  577. PhiState lookupFromMap(Value *V) {
  578. auto I = phiStates.find(V);
  579. assert(I != phiStates.end() && "lookup failed!");
  580. return I->second;
  581. }
  582. static PhiState pureMeet(const PhiState &stateA, const PhiState &stateB) {
  583. switch (stateA.getStatus()) {
  584. case PhiState::Unknown:
  585. return stateB;
  586. case PhiState::Base:
  587. assert(stateA.getBase() && "can't be null");
  588. if (stateB.isUnknown())
  589. return stateA;
  590. if (stateB.isBase()) {
  591. if (stateA.getBase() == stateB.getBase()) {
  592. assert(stateA == stateB && "equality broken!");
  593. return stateA;
  594. }
  595. return PhiState(PhiState::Conflict);
  596. }
  597. assert(stateB.isConflict() && "only three states!");
  598. return PhiState(PhiState::Conflict);
  599. case PhiState::Conflict:
  600. return stateA;
  601. }
  602. llvm_unreachable("only three states!");
  603. }
  604. };
  605. }
  606. /// For a given value or instruction, figure out what base ptr it's derived
  607. /// from. For gc objects, this is simply itself. On success, returns a value
  608. /// which is the base pointer. (This is reliable and can be used for
  609. /// relocation.) On failure, returns nullptr.
  610. static Value *findBasePointer(Value *I, DefiningValueMapTy &cache) {
  611. Value *def = findBaseOrBDV(I, cache);
  612. if (isKnownBaseResult(def)) {
  613. return def;
  614. }
  615. // Here's the rough algorithm:
  616. // - For every SSA value, construct a mapping to either an actual base
  617. // pointer or a PHI which obscures the base pointer.
  618. // - Construct a mapping from PHI to unknown TOP state. Use an
  619. // optimistic algorithm to propagate base pointer information. Lattice
  620. // looks like:
  621. // UNKNOWN
  622. // b1 b2 b3 b4
  623. // CONFLICT
  624. // When algorithm terminates, all PHIs will either have a single concrete
  625. // base or be in a conflict state.
  626. // - For every conflict, insert a dummy PHI node without arguments. Add
  627. // these to the base[Instruction] = BasePtr mapping. For every
  628. // non-conflict, add the actual base.
  629. // - For every conflict, add arguments for the base[a] of each input
  630. // arguments.
  631. //
  632. // Note: A simpler form of this would be to add the conflict form of all
  633. // PHIs without running the optimistic algorithm. This would be
  634. // analougous to pessimistic data flow and would likely lead to an
  635. // overall worse solution.
  636. ConflictStateMapTy states;
  637. states[def] = PhiState();
  638. // Recursively fill in all phis & selects reachable from the initial one
  639. // for which we don't already know a definite base value for
  640. // TODO: This should be rewritten with a worklist
  641. bool done = false;
  642. while (!done) {
  643. done = true;
  644. // Since we're adding elements to 'states' as we run, we can't keep
  645. // iterators into the set.
  646. SmallVector<Value *, 16> Keys;
  647. Keys.reserve(states.size());
  648. for (auto Pair : states) {
  649. Value *V = Pair.first;
  650. Keys.push_back(V);
  651. }
  652. for (Value *v : Keys) {
  653. assert(!isKnownBaseResult(v) && "why did it get added?");
  654. if (PHINode *phi = dyn_cast<PHINode>(v)) {
  655. assert(phi->getNumIncomingValues() > 0 &&
  656. "zero input phis are illegal");
  657. for (Value *InVal : phi->incoming_values()) {
  658. Value *local = findBaseOrBDV(InVal, cache);
  659. if (!isKnownBaseResult(local) && states.find(local) == states.end()) {
  660. states[local] = PhiState();
  661. done = false;
  662. }
  663. }
  664. } else if (SelectInst *sel = dyn_cast<SelectInst>(v)) {
  665. Value *local = findBaseOrBDV(sel->getTrueValue(), cache);
  666. if (!isKnownBaseResult(local) && states.find(local) == states.end()) {
  667. states[local] = PhiState();
  668. done = false;
  669. }
  670. local = findBaseOrBDV(sel->getFalseValue(), cache);
  671. if (!isKnownBaseResult(local) && states.find(local) == states.end()) {
  672. states[local] = PhiState();
  673. done = false;
  674. }
  675. }
  676. }
  677. }
  678. if (TraceLSP) {
  679. errs() << "States after initialization:\n";
  680. for (auto Pair : states) {
  681. Instruction *v = cast<Instruction>(Pair.first);
  682. PhiState state = Pair.second;
  683. state.dump();
  684. v->dump();
  685. }
  686. }
  687. // TODO: come back and revisit the state transitions around inputs which
  688. // have reached conflict state. The current version seems too conservative.
  689. bool progress = true;
  690. while (progress) {
  691. #ifndef NDEBUG
  692. size_t oldSize = states.size();
  693. #endif
  694. progress = false;
  695. // We're only changing keys in this loop, thus safe to keep iterators
  696. for (auto Pair : states) {
  697. MeetPhiStates calculateMeet(states);
  698. Value *v = Pair.first;
  699. assert(!isKnownBaseResult(v) && "why did it get added?");
  700. if (SelectInst *select = dyn_cast<SelectInst>(v)) {
  701. calculateMeet.meetWith(findBaseOrBDV(select->getTrueValue(), cache));
  702. calculateMeet.meetWith(findBaseOrBDV(select->getFalseValue(), cache));
  703. } else
  704. for (Value *Val : cast<PHINode>(v)->incoming_values())
  705. calculateMeet.meetWith(findBaseOrBDV(Val, cache));
  706. PhiState oldState = states[v];
  707. PhiState newState = calculateMeet.getResult();
  708. if (oldState != newState) {
  709. progress = true;
  710. states[v] = newState;
  711. }
  712. }
  713. assert(oldSize <= states.size());
  714. assert(oldSize == states.size() || progress);
  715. }
  716. if (TraceLSP) {
  717. errs() << "States after meet iteration:\n";
  718. for (auto Pair : states) {
  719. Instruction *v = cast<Instruction>(Pair.first);
  720. PhiState state = Pair.second;
  721. state.dump();
  722. v->dump();
  723. }
  724. }
  725. // Insert Phis for all conflicts
  726. // We want to keep naming deterministic in the loop that follows, so
  727. // sort the keys before iteration. This is useful in allowing us to
  728. // write stable tests. Note that there is no invalidation issue here.
  729. SmallVector<Value *, 16> Keys;
  730. Keys.reserve(states.size());
  731. for (auto Pair : states) {
  732. Value *V = Pair.first;
  733. Keys.push_back(V);
  734. }
  735. std::sort(Keys.begin(), Keys.end(), order_by_name);
  736. // TODO: adjust naming patterns to avoid this order of iteration dependency
  737. for (Value *V : Keys) {
  738. Instruction *v = cast<Instruction>(V);
  739. PhiState state = states[V];
  740. assert(!isKnownBaseResult(v) && "why did it get added?");
  741. assert(!state.isUnknown() && "Optimistic algorithm didn't complete!");
  742. if (!state.isConflict())
  743. continue;
  744. if (isa<PHINode>(v)) {
  745. int num_preds =
  746. std::distance(pred_begin(v->getParent()), pred_end(v->getParent()));
  747. assert(num_preds > 0 && "how did we reach here");
  748. PHINode *phi = PHINode::Create(v->getType(), num_preds, "base_phi", v);
  749. // Add metadata marking this as a base value
  750. auto *const_1 = ConstantInt::get(
  751. Type::getInt32Ty(
  752. v->getParent()->getParent()->getParent()->getContext()),
  753. 1);
  754. auto MDConst = ConstantAsMetadata::get(const_1);
  755. MDNode *md = MDNode::get(
  756. v->getParent()->getParent()->getParent()->getContext(), MDConst);
  757. phi->setMetadata("is_base_value", md);
  758. states[v] = PhiState(PhiState::Conflict, phi);
  759. } else {
  760. SelectInst *sel = cast<SelectInst>(v);
  761. // The undef will be replaced later
  762. UndefValue *undef = UndefValue::get(sel->getType());
  763. SelectInst *basesel = SelectInst::Create(sel->getCondition(), undef,
  764. undef, "base_select", sel);
  765. // Add metadata marking this as a base value
  766. auto *const_1 = ConstantInt::get(
  767. Type::getInt32Ty(
  768. v->getParent()->getParent()->getParent()->getContext()),
  769. 1);
  770. auto MDConst = ConstantAsMetadata::get(const_1);
  771. MDNode *md = MDNode::get(
  772. v->getParent()->getParent()->getParent()->getContext(), MDConst);
  773. basesel->setMetadata("is_base_value", md);
  774. states[v] = PhiState(PhiState::Conflict, basesel);
  775. }
  776. }
  777. // Fixup all the inputs of the new PHIs
  778. for (auto Pair : states) {
  779. Instruction *v = cast<Instruction>(Pair.first);
  780. PhiState state = Pair.second;
  781. assert(!isKnownBaseResult(v) && "why did it get added?");
  782. assert(!state.isUnknown() && "Optimistic algorithm didn't complete!");
  783. if (!state.isConflict())
  784. continue;
  785. if (PHINode *basephi = dyn_cast<PHINode>(state.getBase())) {
  786. PHINode *phi = cast<PHINode>(v);
  787. unsigned NumPHIValues = phi->getNumIncomingValues();
  788. for (unsigned i = 0; i < NumPHIValues; i++) {
  789. Value *InVal = phi->getIncomingValue(i);
  790. BasicBlock *InBB = phi->getIncomingBlock(i);
  791. // If we've already seen InBB, add the same incoming value
  792. // we added for it earlier. The IR verifier requires phi
  793. // nodes with multiple entries from the same basic block
  794. // to have the same incoming value for each of those
  795. // entries. If we don't do this check here and basephi
  796. // has a different type than base, we'll end up adding two
  797. // bitcasts (and hence two distinct values) as incoming
  798. // values for the same basic block.
  799. int blockIndex = basephi->getBasicBlockIndex(InBB);
  800. if (blockIndex != -1) {
  801. Value *oldBase = basephi->getIncomingValue(blockIndex);
  802. basephi->addIncoming(oldBase, InBB);
  803. #ifndef NDEBUG
  804. Value *base = findBaseOrBDV(InVal, cache);
  805. if (!isKnownBaseResult(base)) {
  806. // Either conflict or base.
  807. assert(states.count(base));
  808. base = states[base].getBase();
  809. assert(base != nullptr && "unknown PhiState!");
  810. }
  811. // In essense this assert states: the only way two
  812. // values incoming from the same basic block may be
  813. // different is by being different bitcasts of the same
  814. // value. A cleanup that remains TODO is changing
  815. // findBaseOrBDV to return an llvm::Value of the correct
  816. // type (and still remain pure). This will remove the
  817. // need to add bitcasts.
  818. assert(base->stripPointerCasts() == oldBase->stripPointerCasts() &&
  819. "sanity -- findBaseOrBDV should be pure!");
  820. #endif
  821. continue;
  822. }
  823. // Find either the defining value for the PHI or the normal base for
  824. // a non-phi node
  825. Value *base = findBaseOrBDV(InVal, cache);
  826. if (!isKnownBaseResult(base)) {
  827. // Either conflict or base.
  828. assert(states.count(base));
  829. base = states[base].getBase();
  830. assert(base != nullptr && "unknown PhiState!");
  831. }
  832. assert(base && "can't be null");
  833. // Must use original input BB since base may not be Instruction
  834. // The cast is needed since base traversal may strip away bitcasts
  835. if (base->getType() != basephi->getType()) {
  836. base = new BitCastInst(base, basephi->getType(), "cast",
  837. InBB->getTerminator());
  838. }
  839. basephi->addIncoming(base, InBB);
  840. }
  841. assert(basephi->getNumIncomingValues() == NumPHIValues);
  842. } else {
  843. SelectInst *basesel = cast<SelectInst>(state.getBase());
  844. SelectInst *sel = cast<SelectInst>(v);
  845. // Operand 1 & 2 are true, false path respectively. TODO: refactor to
  846. // something more safe and less hacky.
  847. for (int i = 1; i <= 2; i++) {
  848. Value *InVal = sel->getOperand(i);
  849. // Find either the defining value for the PHI or the normal base for
  850. // a non-phi node
  851. Value *base = findBaseOrBDV(InVal, cache);
  852. if (!isKnownBaseResult(base)) {
  853. // Either conflict or base.
  854. assert(states.count(base));
  855. base = states[base].getBase();
  856. assert(base != nullptr && "unknown PhiState!");
  857. }
  858. assert(base && "can't be null");
  859. // Must use original input BB since base may not be Instruction
  860. // The cast is needed since base traversal may strip away bitcasts
  861. if (base->getType() != basesel->getType()) {
  862. base = new BitCastInst(base, basesel->getType(), "cast", basesel);
  863. }
  864. basesel->setOperand(i, base);
  865. }
  866. }
  867. }
  868. // Cache all of our results so we can cheaply reuse them
  869. // NOTE: This is actually two caches: one of the base defining value
  870. // relation and one of the base pointer relation! FIXME
  871. for (auto item : states) {
  872. Value *v = item.first;
  873. Value *base = item.second.getBase();
  874. assert(v && base);
  875. assert(!isKnownBaseResult(v) && "why did it get added?");
  876. if (TraceLSP) {
  877. std::string fromstr =
  878. cache.count(v) ? (cache[v]->hasName() ? cache[v]->getName() : "")
  879. : "none";
  880. errs() << "Updating base value cache"
  881. << " for: " << (v->hasName() ? v->getName() : "")
  882. << " from: " << fromstr
  883. << " to: " << (base->hasName() ? base->getName() : "") << "\n";
  884. }
  885. assert(isKnownBaseResult(base) &&
  886. "must be something we 'know' is a base pointer");
  887. if (cache.count(v)) {
  888. // Once we transition from the BDV relation being store in the cache to
  889. // the base relation being stored, it must be stable
  890. assert((!isKnownBaseResult(cache[v]) || cache[v] == base) &&
  891. "base relation should be stable");
  892. }
  893. cache[v] = base;
  894. }
  895. assert(cache.find(def) != cache.end());
  896. return cache[def];
  897. }
  898. // For a set of live pointers (base and/or derived), identify the base
  899. // pointer of the object which they are derived from. This routine will
  900. // mutate the IR graph as needed to make the 'base' pointer live at the
  901. // definition site of 'derived'. This ensures that any use of 'derived' can
  902. // also use 'base'. This may involve the insertion of a number of
  903. // additional PHI nodes.
  904. //
  905. // preconditions: live is a set of pointer type Values
  906. //
  907. // side effects: may insert PHI nodes into the existing CFG, will preserve
  908. // CFG, will not remove or mutate any existing nodes
  909. //
  910. // post condition: PointerToBase contains one (derived, base) pair for every
  911. // pointer in live. Note that derived can be equal to base if the original
  912. // pointer was a base pointer.
  913. static void
  914. findBasePointers(const StatepointLiveSetTy &live,
  915. DenseMap<llvm::Value *, llvm::Value *> &PointerToBase,
  916. DominatorTree *DT, DefiningValueMapTy &DVCache) {
  917. // For the naming of values inserted to be deterministic - which makes for
  918. // much cleaner and more stable tests - we need to assign an order to the
  919. // live values. DenseSets do not provide a deterministic order across runs.
  920. SmallVector<Value *, 64> Temp;
  921. Temp.insert(Temp.end(), live.begin(), live.end());
  922. std::sort(Temp.begin(), Temp.end(), order_by_name);
  923. for (Value *ptr : Temp) {
  924. Value *base = findBasePointer(ptr, DVCache);
  925. assert(base && "failed to find base pointer");
  926. PointerToBase[ptr] = base;
  927. assert((!isa<Instruction>(base) || !isa<Instruction>(ptr) ||
  928. DT->dominates(cast<Instruction>(base)->getParent(),
  929. cast<Instruction>(ptr)->getParent())) &&
  930. "The base we found better dominate the derived pointer");
  931. // If you see this trip and like to live really dangerously, the code should
  932. // be correct, just with idioms the verifier can't handle. You can try
  933. // disabling the verifier at your own substaintial risk.
  934. assert(!isa<ConstantPointerNull>(base) &&
  935. "the relocation code needs adjustment to handle the relocation of "
  936. "a null pointer constant without causing false positives in the "
  937. "safepoint ir verifier.");
  938. }
  939. }
  940. /// Find the required based pointers (and adjust the live set) for the given
  941. /// parse point.
  942. static void findBasePointers(DominatorTree &DT, DefiningValueMapTy &DVCache,
  943. const CallSite &CS,
  944. PartiallyConstructedSafepointRecord &result) {
  945. DenseMap<llvm::Value *, llvm::Value *> PointerToBase;
  946. findBasePointers(result.liveset, PointerToBase, &DT, DVCache);
  947. if (PrintBasePointers) {
  948. // Note: Need to print these in a stable order since this is checked in
  949. // some tests.
  950. errs() << "Base Pairs (w/o Relocation):\n";
  951. SmallVector<Value *, 64> Temp;
  952. Temp.reserve(PointerToBase.size());
  953. for (auto Pair : PointerToBase) {
  954. Temp.push_back(Pair.first);
  955. }
  956. std::sort(Temp.begin(), Temp.end(), order_by_name);
  957. for (Value *Ptr : Temp) {
  958. Value *Base = PointerToBase[Ptr];
  959. errs() << " derived %" << Ptr->getName() << " base %" << Base->getName()
  960. << "\n";
  961. }
  962. }
  963. result.PointerToBase = PointerToBase;
  964. }
  965. /// Given an updated version of the dataflow liveness results, update the
  966. /// liveset and base pointer maps for the call site CS.
  967. static void recomputeLiveInValues(GCPtrLivenessData &RevisedLivenessData,
  968. const CallSite &CS,
  969. PartiallyConstructedSafepointRecord &result);
  970. static void recomputeLiveInValues(
  971. Function &F, DominatorTree &DT, Pass *P, ArrayRef<CallSite> toUpdate,
  972. MutableArrayRef<struct PartiallyConstructedSafepointRecord> records) {
  973. // TODO-PERF: reuse the original liveness, then simply run the dataflow
  974. // again. The old values are still live and will help it stablize quickly.
  975. GCPtrLivenessData RevisedLivenessData;
  976. computeLiveInValues(DT, F, RevisedLivenessData);
  977. for (size_t i = 0; i < records.size(); i++) {
  978. struct PartiallyConstructedSafepointRecord &info = records[i];
  979. const CallSite &CS = toUpdate[i];
  980. recomputeLiveInValues(RevisedLivenessData, CS, info);
  981. }
  982. }
  983. // When inserting gc.relocate calls, we need to ensure there are no uses
  984. // of the original value between the gc.statepoint and the gc.relocate call.
  985. // One case which can arise is a phi node starting one of the successor blocks.
  986. // We also need to be able to insert the gc.relocates only on the path which
  987. // goes through the statepoint. We might need to split an edge to make this
  988. // possible.
  989. static BasicBlock *
  990. normalizeForInvokeSafepoint(BasicBlock *BB, BasicBlock *InvokeParent,
  991. DominatorTree &DT) {
  992. BasicBlock *Ret = BB;
  993. if (!BB->getUniquePredecessor()) {
  994. Ret = SplitBlockPredecessors(BB, InvokeParent, "", nullptr, &DT);
  995. }
  996. // Now that 'ret' has unique predecessor we can safely remove all phi nodes
  997. // from it
  998. FoldSingleEntryPHINodes(Ret);
  999. assert(!isa<PHINode>(Ret->begin()));
  1000. // At this point, we can safely insert a gc.relocate as the first instruction
  1001. // in Ret if needed.
  1002. return Ret;
  1003. }
  1004. static int find_index(ArrayRef<Value *> livevec, Value *val) {
  1005. auto itr = std::find(livevec.begin(), livevec.end(), val);
  1006. assert(livevec.end() != itr);
  1007. size_t index = std::distance(livevec.begin(), itr);
  1008. assert(index < livevec.size());
  1009. return index;
  1010. }
  1011. // Create new attribute set containing only attributes which can be transfered
  1012. // from original call to the safepoint.
  1013. static AttributeSet legalizeCallAttributes(AttributeSet AS) {
  1014. AttributeSet ret;
  1015. for (unsigned Slot = 0; Slot < AS.getNumSlots(); Slot++) {
  1016. unsigned index = AS.getSlotIndex(Slot);
  1017. if (index == AttributeSet::ReturnIndex ||
  1018. index == AttributeSet::FunctionIndex) {
  1019. for (auto it = AS.begin(Slot), it_end = AS.end(Slot); it != it_end;
  1020. ++it) {
  1021. Attribute attr = *it;
  1022. // Do not allow certain attributes - just skip them
  1023. // Safepoint can not be read only or read none.
  1024. if (attr.hasAttribute(Attribute::ReadNone) ||
  1025. attr.hasAttribute(Attribute::ReadOnly))
  1026. continue;
  1027. ret = ret.addAttributes(
  1028. AS.getContext(), index,
  1029. AttributeSet::get(AS.getContext(), index, AttrBuilder(attr)));
  1030. }
  1031. }
  1032. // Just skip parameter attributes for now
  1033. }
  1034. return ret;
  1035. }
  1036. /// Helper function to place all gc relocates necessary for the given
  1037. /// statepoint.
  1038. /// Inputs:
  1039. /// liveVariables - list of variables to be relocated.
  1040. /// liveStart - index of the first live variable.
  1041. /// basePtrs - base pointers.
  1042. /// statepointToken - statepoint instruction to which relocates should be
  1043. /// bound.
  1044. /// Builder - Llvm IR builder to be used to construct new calls.
  1045. static void CreateGCRelocates(ArrayRef<llvm::Value *> LiveVariables,
  1046. const int LiveStart,
  1047. ArrayRef<llvm::Value *> BasePtrs,
  1048. Instruction *StatepointToken,
  1049. IRBuilder<> Builder) {
  1050. SmallVector<Instruction *, 64> NewDefs;
  1051. NewDefs.reserve(LiveVariables.size());
  1052. Module *M = StatepointToken->getParent()->getParent()->getParent();
  1053. for (unsigned i = 0; i < LiveVariables.size(); i++) {
  1054. // We generate a (potentially) unique declaration for every pointer type
  1055. // combination. This results is some blow up the function declarations in
  1056. // the IR, but removes the need for argument bitcasts which shrinks the IR
  1057. // greatly and makes it much more readable.
  1058. SmallVector<Type *, 1> Types; // one per 'any' type
  1059. // All gc_relocate are set to i8 addrspace(1)* type. This could help avoid
  1060. // cases where the actual value's type mangling is not supported by llvm. A
  1061. // bitcast is added later to convert gc_relocate to the actual value's type.
  1062. Types.push_back(Type::getInt8PtrTy(M->getContext(), 1));
  1063. Value *GCRelocateDecl = Intrinsic::getDeclaration(
  1064. M, Intrinsic::experimental_gc_relocate, Types);
  1065. // Generate the gc.relocate call and save the result
  1066. Value *BaseIdx =
  1067. ConstantInt::get(Type::getInt32Ty(M->getContext()),
  1068. LiveStart + find_index(LiveVariables, BasePtrs[i]));
  1069. Value *LiveIdx = ConstantInt::get(
  1070. Type::getInt32Ty(M->getContext()),
  1071. LiveStart + find_index(LiveVariables, LiveVariables[i]));
  1072. // only specify a debug name if we can give a useful one
  1073. Value *Reloc = Builder.CreateCall(
  1074. GCRelocateDecl, {StatepointToken, BaseIdx, LiveIdx},
  1075. LiveVariables[i]->hasName() ? LiveVariables[i]->getName() + ".relocated"
  1076. : "");
  1077. // Trick CodeGen into thinking there are lots of free registers at this
  1078. // fake call.
  1079. cast<CallInst>(Reloc)->setCallingConv(CallingConv::Cold);
  1080. NewDefs.push_back(cast<Instruction>(Reloc));
  1081. }
  1082. assert(NewDefs.size() == LiveVariables.size() &&
  1083. "missing or extra redefinition at safepoint");
  1084. }
  1085. static void
  1086. makeStatepointExplicitImpl(const CallSite &CS, /* to replace */
  1087. const SmallVectorImpl<llvm::Value *> &basePtrs,
  1088. const SmallVectorImpl<llvm::Value *> &liveVariables,
  1089. Pass *P,
  1090. PartiallyConstructedSafepointRecord &result) {
  1091. assert(basePtrs.size() == liveVariables.size());
  1092. assert(isStatepoint(CS) &&
  1093. "This method expects to be rewriting a statepoint");
  1094. BasicBlock *BB = CS.getInstruction()->getParent();
  1095. assert(BB);
  1096. Function *F = BB->getParent();
  1097. assert(F && "must be set");
  1098. Module *M = F->getParent();
  1099. (void)M;
  1100. assert(M && "must be set");
  1101. // We're not changing the function signature of the statepoint since the gc
  1102. // arguments go into the var args section.
  1103. Function *gc_statepoint_decl = CS.getCalledFunction();
  1104. // Then go ahead and use the builder do actually do the inserts. We insert
  1105. // immediately before the previous instruction under the assumption that all
  1106. // arguments will be available here. We can't insert afterwards since we may
  1107. // be replacing a terminator.
  1108. Instruction *insertBefore = CS.getInstruction();
  1109. IRBuilder<> Builder(insertBefore);
  1110. // Copy all of the arguments from the original statepoint - this includes the
  1111. // target, call args, and deopt args
  1112. SmallVector<llvm::Value *, 64> args;
  1113. args.insert(args.end(), CS.arg_begin(), CS.arg_end());
  1114. // TODO: Clear the 'needs rewrite' flag
  1115. // add all the pointers to be relocated (gc arguments)
  1116. // Capture the start of the live variable list for use in the gc_relocates
  1117. const int live_start = args.size();
  1118. args.insert(args.end(), liveVariables.begin(), liveVariables.end());
  1119. // Create the statepoint given all the arguments
  1120. Instruction *token = nullptr;
  1121. AttributeSet return_attributes;
  1122. if (CS.isCall()) {
  1123. CallInst *toReplace = cast<CallInst>(CS.getInstruction());
  1124. CallInst *call =
  1125. Builder.CreateCall(gc_statepoint_decl, args, "safepoint_token");
  1126. call->setTailCall(toReplace->isTailCall());
  1127. call->setCallingConv(toReplace->getCallingConv());
  1128. // Currently we will fail on parameter attributes and on certain
  1129. // function attributes.
  1130. AttributeSet new_attrs = legalizeCallAttributes(toReplace->getAttributes());
  1131. // In case if we can handle this set of sttributes - set up function attrs
  1132. // directly on statepoint and return attrs later for gc_result intrinsic.
  1133. call->setAttributes(new_attrs.getFnAttributes());
  1134. return_attributes = new_attrs.getRetAttributes();
  1135. token = call;
  1136. // Put the following gc_result and gc_relocate calls immediately after the
  1137. // the old call (which we're about to delete)
  1138. BasicBlock::iterator next(toReplace);
  1139. assert(BB->end() != next && "not a terminator, must have next");
  1140. next++;
  1141. Instruction *IP = &*(next);
  1142. Builder.SetInsertPoint(IP);
  1143. Builder.SetCurrentDebugLocation(IP->getDebugLoc());
  1144. } else {
  1145. InvokeInst *toReplace = cast<InvokeInst>(CS.getInstruction());
  1146. // Insert the new invoke into the old block. We'll remove the old one in a
  1147. // moment at which point this will become the new terminator for the
  1148. // original block.
  1149. InvokeInst *invoke = InvokeInst::Create(
  1150. gc_statepoint_decl, toReplace->getNormalDest(),
  1151. toReplace->getUnwindDest(), args, "", toReplace->getParent());
  1152. invoke->setCallingConv(toReplace->getCallingConv());
  1153. // Currently we will fail on parameter attributes and on certain
  1154. // function attributes.
  1155. AttributeSet new_attrs = legalizeCallAttributes(toReplace->getAttributes());
  1156. // In case if we can handle this set of sttributes - set up function attrs
  1157. // directly on statepoint and return attrs later for gc_result intrinsic.
  1158. invoke->setAttributes(new_attrs.getFnAttributes());
  1159. return_attributes = new_attrs.getRetAttributes();
  1160. token = invoke;
  1161. // Generate gc relocates in exceptional path
  1162. BasicBlock *unwindBlock = toReplace->getUnwindDest();
  1163. assert(!isa<PHINode>(unwindBlock->begin()) &&
  1164. unwindBlock->getUniquePredecessor() &&
  1165. "can't safely insert in this block!");
  1166. Instruction *IP = &*(unwindBlock->getFirstInsertionPt());
  1167. Builder.SetInsertPoint(IP);
  1168. Builder.SetCurrentDebugLocation(toReplace->getDebugLoc());
  1169. // Extract second element from landingpad return value. We will attach
  1170. // exceptional gc relocates to it.
  1171. const unsigned idx = 1;
  1172. Instruction *exceptional_token =
  1173. cast<Instruction>(Builder.CreateExtractValue(
  1174. unwindBlock->getLandingPadInst(), idx, "relocate_token"));
  1175. result.UnwindToken = exceptional_token;
  1176. // Just throw away return value. We will use the one we got for normal
  1177. // block.
  1178. (void)CreateGCRelocates(liveVariables, live_start, basePtrs,
  1179. exceptional_token, Builder);
  1180. // Generate gc relocates and returns for normal block
  1181. BasicBlock *normalDest = toReplace->getNormalDest();
  1182. assert(!isa<PHINode>(normalDest->begin()) &&
  1183. normalDest->getUniquePredecessor() &&
  1184. "can't safely insert in this block!");
  1185. IP = &*(normalDest->getFirstInsertionPt());
  1186. Builder.SetInsertPoint(IP);
  1187. // gc relocates will be generated later as if it were regular call
  1188. // statepoint
  1189. }
  1190. assert(token);
  1191. // Take the name of the original value call if it had one.
  1192. token->takeName(CS.getInstruction());
  1193. // The GCResult is already inserted, we just need to find it
  1194. #ifndef NDEBUG
  1195. Instruction *toReplace = CS.getInstruction();
  1196. assert((toReplace->hasNUses(0) || toReplace->hasNUses(1)) &&
  1197. "only valid use before rewrite is gc.result");
  1198. assert(!toReplace->hasOneUse() ||
  1199. isGCResult(cast<Instruction>(*toReplace->user_begin())));
  1200. #endif
  1201. // Update the gc.result of the original statepoint (if any) to use the newly
  1202. // inserted statepoint. This is safe to do here since the token can't be
  1203. // considered a live reference.
  1204. CS.getInstruction()->replaceAllUsesWith(token);
  1205. result.StatepointToken = token;
  1206. // Second, create a gc.relocate for every live variable
  1207. CreateGCRelocates(liveVariables, live_start, basePtrs, token, Builder);
  1208. }
  1209. namespace {
  1210. struct name_ordering {
  1211. Value *base;
  1212. Value *derived;
  1213. bool operator()(name_ordering const &a, name_ordering const &b) {
  1214. return -1 == a.derived->getName().compare(b.derived->getName());
  1215. }
  1216. };
  1217. }
  1218. static void stablize_order(SmallVectorImpl<Value *> &basevec,
  1219. SmallVectorImpl<Value *> &livevec) {
  1220. assert(basevec.size() == livevec.size());
  1221. SmallVector<name_ordering, 64> temp;
  1222. for (size_t i = 0; i < basevec.size(); i++) {
  1223. name_ordering v;
  1224. v.base = basevec[i];
  1225. v.derived = livevec[i];
  1226. temp.push_back(v);
  1227. }
  1228. std::sort(temp.begin(), temp.end(), name_ordering());
  1229. for (size_t i = 0; i < basevec.size(); i++) {
  1230. basevec[i] = temp[i].base;
  1231. livevec[i] = temp[i].derived;
  1232. }
  1233. }
  1234. // Replace an existing gc.statepoint with a new one and a set of gc.relocates
  1235. // which make the relocations happening at this safepoint explicit.
  1236. //
  1237. // WARNING: Does not do any fixup to adjust users of the original live
  1238. // values. That's the callers responsibility.
  1239. static void
  1240. makeStatepointExplicit(DominatorTree &DT, const CallSite &CS, Pass *P,
  1241. PartiallyConstructedSafepointRecord &result) {
  1242. auto liveset = result.liveset;
  1243. auto PointerToBase = result.PointerToBase;
  1244. // Convert to vector for efficient cross referencing.
  1245. SmallVector<Value *, 64> basevec, livevec;
  1246. livevec.reserve(liveset.size());
  1247. basevec.reserve(liveset.size());
  1248. for (Value *L : liveset) {
  1249. livevec.push_back(L);
  1250. assert(PointerToBase.find(L) != PointerToBase.end());
  1251. Value *base = PointerToBase[L];
  1252. basevec.push_back(base);
  1253. }
  1254. assert(livevec.size() == basevec.size());
  1255. // To make the output IR slightly more stable (for use in diffs), ensure a
  1256. // fixed order of the values in the safepoint (by sorting the value name).
  1257. // The order is otherwise meaningless.
  1258. stablize_order(basevec, livevec);
  1259. // Do the actual rewriting and delete the old statepoint
  1260. makeStatepointExplicitImpl(CS, basevec, livevec, P, result);
  1261. CS.getInstruction()->eraseFromParent();
  1262. }
  1263. // Helper function for the relocationViaAlloca.
  1264. // It receives iterator to the statepoint gc relocates and emits store to the
  1265. // assigned
  1266. // location (via allocaMap) for the each one of them.
  1267. // Add visited values into the visitedLiveValues set we will later use them
  1268. // for sanity check.
  1269. static void
  1270. insertRelocationStores(iterator_range<Value::user_iterator> GCRelocs,
  1271. DenseMap<Value *, Value *> &AllocaMap,
  1272. DenseSet<Value *> &VisitedLiveValues) {
  1273. for (User *U : GCRelocs) {
  1274. if (!isa<IntrinsicInst>(U))
  1275. continue;
  1276. IntrinsicInst *RelocatedValue = cast<IntrinsicInst>(U);
  1277. // We only care about relocates
  1278. if (RelocatedValue->getIntrinsicID() !=
  1279. Intrinsic::experimental_gc_relocate) {
  1280. continue;
  1281. }
  1282. GCRelocateOperands RelocateOperands(RelocatedValue);
  1283. Value *OriginalValue =
  1284. const_cast<Value *>(RelocateOperands.getDerivedPtr());
  1285. assert(AllocaMap.count(OriginalValue));
  1286. Value *Alloca = AllocaMap[OriginalValue];
  1287. // Emit store into the related alloca
  1288. // All gc_relocate are i8 addrspace(1)* typed, and it must be bitcasted to
  1289. // the correct type according to alloca.
  1290. assert(RelocatedValue->getNextNode() && "Should always have one since it's not a terminator");
  1291. IRBuilder<> Builder(RelocatedValue->getNextNode());
  1292. Value *CastedRelocatedValue =
  1293. Builder.CreateBitCast(RelocatedValue, cast<AllocaInst>(Alloca)->getAllocatedType(),
  1294. RelocatedValue->hasName() ? RelocatedValue->getName() + ".casted" : "");
  1295. StoreInst *Store = new StoreInst(CastedRelocatedValue, Alloca);
  1296. Store->insertAfter(cast<Instruction>(CastedRelocatedValue));
  1297. #ifndef NDEBUG
  1298. VisitedLiveValues.insert(OriginalValue);
  1299. #endif
  1300. }
  1301. }
  1302. // Helper function for the "relocationViaAlloca". Similar to the
  1303. // "insertRelocationStores" but works for rematerialized values.
  1304. static void
  1305. insertRematerializationStores(
  1306. RematerializedValueMapTy RematerializedValues,
  1307. DenseMap<Value *, Value *> &AllocaMap,
  1308. DenseSet<Value *> &VisitedLiveValues) {
  1309. for (auto RematerializedValuePair: RematerializedValues) {
  1310. Instruction *RematerializedValue = RematerializedValuePair.first;
  1311. Value *OriginalValue = RematerializedValuePair.second;
  1312. assert(AllocaMap.count(OriginalValue) &&
  1313. "Can not find alloca for rematerialized value");
  1314. Value *Alloca = AllocaMap[OriginalValue];
  1315. StoreInst *Store = new StoreInst(RematerializedValue, Alloca);
  1316. Store->insertAfter(RematerializedValue);
  1317. #ifndef NDEBUG
  1318. VisitedLiveValues.insert(OriginalValue);
  1319. #endif
  1320. }
  1321. }
  1322. /// do all the relocation update via allocas and mem2reg
  1323. static void relocationViaAlloca(
  1324. Function &F, DominatorTree &DT, ArrayRef<Value *> Live,
  1325. ArrayRef<struct PartiallyConstructedSafepointRecord> Records) {
  1326. #ifndef NDEBUG
  1327. // record initial number of (static) allocas; we'll check we have the same
  1328. // number when we get done.
  1329. int InitialAllocaNum = 0;
  1330. for (auto I = F.getEntryBlock().begin(), E = F.getEntryBlock().end(); I != E;
  1331. I++)
  1332. if (isa<AllocaInst>(*I))
  1333. InitialAllocaNum++;
  1334. #endif
  1335. // TODO-PERF: change data structures, reserve
  1336. DenseMap<Value *, Value *> AllocaMap;
  1337. SmallVector<AllocaInst *, 200> PromotableAllocas;
  1338. // Used later to chack that we have enough allocas to store all values
  1339. std::size_t NumRematerializedValues = 0;
  1340. PromotableAllocas.reserve(Live.size());
  1341. // Emit alloca for "LiveValue" and record it in "allocaMap" and
  1342. // "PromotableAllocas"
  1343. auto emitAllocaFor = [&](Value *LiveValue) {
  1344. AllocaInst *Alloca = new AllocaInst(LiveValue->getType(), "",
  1345. F.getEntryBlock().getFirstNonPHI());
  1346. AllocaMap[LiveValue] = Alloca;
  1347. PromotableAllocas.push_back(Alloca);
  1348. };
  1349. // emit alloca for each live gc pointer
  1350. for (unsigned i = 0; i < Live.size(); i++) {
  1351. emitAllocaFor(Live[i]);
  1352. }
  1353. // emit allocas for rematerialized values
  1354. for (size_t i = 0; i < Records.size(); i++) {
  1355. const struct PartiallyConstructedSafepointRecord &Info = Records[i];
  1356. for (auto RematerializedValuePair : Info.RematerializedValues) {
  1357. Value *OriginalValue = RematerializedValuePair.second;
  1358. if (AllocaMap.count(OriginalValue) != 0)
  1359. continue;
  1360. emitAllocaFor(OriginalValue);
  1361. ++NumRematerializedValues;
  1362. }
  1363. }
  1364. // The next two loops are part of the same conceptual operation. We need to
  1365. // insert a store to the alloca after the original def and at each
  1366. // redefinition. We need to insert a load before each use. These are split
  1367. // into distinct loops for performance reasons.
  1368. // update gc pointer after each statepoint
  1369. // either store a relocated value or null (if no relocated value found for
  1370. // this gc pointer and it is not a gc_result)
  1371. // this must happen before we update the statepoint with load of alloca
  1372. // otherwise we lose the link between statepoint and old def
  1373. for (size_t i = 0; i < Records.size(); i++) {
  1374. const struct PartiallyConstructedSafepointRecord &Info = Records[i];
  1375. Value *Statepoint = Info.StatepointToken;
  1376. // This will be used for consistency check
  1377. DenseSet<Value *> VisitedLiveValues;
  1378. // Insert stores for normal statepoint gc relocates
  1379. insertRelocationStores(Statepoint->users(), AllocaMap, VisitedLiveValues);
  1380. // In case if it was invoke statepoint
  1381. // we will insert stores for exceptional path gc relocates.
  1382. if (isa<InvokeInst>(Statepoint)) {
  1383. insertRelocationStores(Info.UnwindToken->users(), AllocaMap,
  1384. VisitedLiveValues);
  1385. }
  1386. // Do similar thing with rematerialized values
  1387. insertRematerializationStores(Info.RematerializedValues, AllocaMap,
  1388. VisitedLiveValues);
  1389. if (ClobberNonLive) {
  1390. // As a debuging aid, pretend that an unrelocated pointer becomes null at
  1391. // the gc.statepoint. This will turn some subtle GC problems into
  1392. // slightly easier to debug SEGVs. Note that on large IR files with
  1393. // lots of gc.statepoints this is extremely costly both memory and time
  1394. // wise.
  1395. SmallVector<AllocaInst *, 64> ToClobber;
  1396. for (auto Pair : AllocaMap) {
  1397. Value *Def = Pair.first;
  1398. AllocaInst *Alloca = cast<AllocaInst>(Pair.second);
  1399. // This value was relocated
  1400. if (VisitedLiveValues.count(Def)) {
  1401. continue;
  1402. }
  1403. ToClobber.push_back(Alloca);
  1404. }
  1405. auto InsertClobbersAt = [&](Instruction *IP) {
  1406. for (auto *AI : ToClobber) {
  1407. auto AIType = cast<PointerType>(AI->getType());
  1408. auto PT = cast<PointerType>(AIType->getElementType());
  1409. Constant *CPN = ConstantPointerNull::get(PT);
  1410. StoreInst *Store = new StoreInst(CPN, AI);
  1411. Store->insertBefore(IP);
  1412. }
  1413. };
  1414. // Insert the clobbering stores. These may get intermixed with the
  1415. // gc.results and gc.relocates, but that's fine.
  1416. if (auto II = dyn_cast<InvokeInst>(Statepoint)) {
  1417. InsertClobbersAt(II->getNormalDest()->getFirstInsertionPt());
  1418. InsertClobbersAt(II->getUnwindDest()->getFirstInsertionPt());
  1419. } else {
  1420. BasicBlock::iterator Next(cast<CallInst>(Statepoint));
  1421. Next++;
  1422. InsertClobbersAt(Next);
  1423. }
  1424. }
  1425. }
  1426. // update use with load allocas and add store for gc_relocated
  1427. for (auto Pair : AllocaMap) {
  1428. Value *Def = Pair.first;
  1429. Value *Alloca = Pair.second;
  1430. // we pre-record the uses of allocas so that we dont have to worry about
  1431. // later update
  1432. // that change the user information.
  1433. SmallVector<Instruction *, 20> Uses;
  1434. // PERF: trade a linear scan for repeated reallocation
  1435. Uses.reserve(std::distance(Def->user_begin(), Def->user_end()));
  1436. for (User *U : Def->users()) {
  1437. if (!isa<ConstantExpr>(U)) {
  1438. // If the def has a ConstantExpr use, then the def is either a
  1439. // ConstantExpr use itself or null. In either case
  1440. // (recursively in the first, directly in the second), the oop
  1441. // it is ultimately dependent on is null and this particular
  1442. // use does not need to be fixed up.
  1443. Uses.push_back(cast<Instruction>(U));
  1444. }
  1445. }
  1446. std::sort(Uses.begin(), Uses.end());
  1447. auto Last = std::unique(Uses.begin(), Uses.end());
  1448. Uses.erase(Last, Uses.end());
  1449. for (Instruction *Use : Uses) {
  1450. if (isa<PHINode>(Use)) {
  1451. PHINode *Phi = cast<PHINode>(Use);
  1452. for (unsigned i = 0; i < Phi->getNumIncomingValues(); i++) {
  1453. if (Def == Phi->getIncomingValue(i)) {
  1454. LoadInst *Load = new LoadInst(
  1455. Alloca, "", Phi->getIncomingBlock(i)->getTerminator());
  1456. Phi->setIncomingValue(i, Load);
  1457. }
  1458. }
  1459. } else {
  1460. LoadInst *Load = new LoadInst(Alloca, "", Use);
  1461. Use->replaceUsesOfWith(Def, Load);
  1462. }
  1463. }
  1464. // emit store for the initial gc value
  1465. // store must be inserted after load, otherwise store will be in alloca's
  1466. // use list and an extra load will be inserted before it
  1467. StoreInst *Store = new StoreInst(Def, Alloca);
  1468. if (Instruction *Inst = dyn_cast<Instruction>(Def)) {
  1469. if (InvokeInst *Invoke = dyn_cast<InvokeInst>(Inst)) {
  1470. // InvokeInst is a TerminatorInst so the store need to be inserted
  1471. // into its normal destination block.
  1472. BasicBlock *NormalDest = Invoke->getNormalDest();
  1473. Store->insertBefore(NormalDest->getFirstNonPHI());
  1474. } else {
  1475. assert(!Inst->isTerminator() &&
  1476. "The only TerminatorInst that can produce a value is "
  1477. "InvokeInst which is handled above.");
  1478. Store->insertAfter(Inst);
  1479. }
  1480. } else {
  1481. assert(isa<Argument>(Def));
  1482. Store->insertAfter(cast<Instruction>(Alloca));
  1483. }
  1484. }
  1485. assert(PromotableAllocas.size() == Live.size() + NumRematerializedValues &&
  1486. "we must have the same allocas with lives");
  1487. if (!PromotableAllocas.empty()) {
  1488. // apply mem2reg to promote alloca to SSA
  1489. PromoteMemToReg(PromotableAllocas, DT);
  1490. }
  1491. #ifndef NDEBUG
  1492. for (auto I = F.getEntryBlock().begin(), E = F.getEntryBlock().end(); I != E;
  1493. I++)
  1494. if (isa<AllocaInst>(*I))
  1495. InitialAllocaNum--;
  1496. assert(InitialAllocaNum == 0 && "We must not introduce any extra allocas");
  1497. #endif
  1498. }
  1499. /// Implement a unique function which doesn't require we sort the input
  1500. /// vector. Doing so has the effect of changing the output of a couple of
  1501. /// tests in ways which make them less useful in testing fused safepoints.
  1502. template <typename T> static void unique_unsorted(SmallVectorImpl<T> &Vec) {
  1503. SmallSet<T, 8> Seen;
  1504. Vec.erase(std::remove_if(Vec.begin(), Vec.end(), [&](const T &V) {
  1505. return !Seen.insert(V).second;
  1506. }), Vec.end());
  1507. }
  1508. /// Insert holders so that each Value is obviously live through the entire
  1509. /// lifetime of the call.
  1510. static void insertUseHolderAfter(CallSite &CS, const ArrayRef<Value *> Values,
  1511. SmallVectorImpl<CallInst *> &Holders) {
  1512. if (Values.empty())
  1513. // No values to hold live, might as well not insert the empty holder
  1514. return;
  1515. Module *M = CS.getInstruction()->getParent()->getParent()->getParent();
  1516. // Use a dummy vararg function to actually hold the values live
  1517. Function *Func = cast<Function>(M->getOrInsertFunction(
  1518. "__tmp_use", FunctionType::get(Type::getVoidTy(M->getContext()), true)));
  1519. if (CS.isCall()) {
  1520. // For call safepoints insert dummy calls right after safepoint
  1521. BasicBlock::iterator Next(CS.getInstruction());
  1522. Next++;
  1523. Holders.push_back(CallInst::Create(Func, Values, "", Next));
  1524. return;
  1525. }
  1526. // For invoke safepooints insert dummy calls both in normal and
  1527. // exceptional destination blocks
  1528. auto *II = cast<InvokeInst>(CS.getInstruction());
  1529. Holders.push_back(CallInst::Create(
  1530. Func, Values, "", II->getNormalDest()->getFirstInsertionPt()));
  1531. Holders.push_back(CallInst::Create(
  1532. Func, Values, "", II->getUnwindDest()->getFirstInsertionPt()));
  1533. }
  1534. static void findLiveReferences(
  1535. Function &F, DominatorTree &DT, Pass *P, ArrayRef<CallSite> toUpdate,
  1536. MutableArrayRef<struct PartiallyConstructedSafepointRecord> records) {
  1537. GCPtrLivenessData OriginalLivenessData;
  1538. computeLiveInValues(DT, F, OriginalLivenessData);
  1539. for (size_t i = 0; i < records.size(); i++) {
  1540. struct PartiallyConstructedSafepointRecord &info = records[i];
  1541. const CallSite &CS = toUpdate[i];
  1542. analyzeParsePointLiveness(DT, OriginalLivenessData, CS, info);
  1543. }
  1544. }
  1545. /// Remove any vector of pointers from the liveset by scalarizing them over the
  1546. /// statepoint instruction. Adds the scalarized pieces to the liveset. It
  1547. /// would be preferrable to include the vector in the statepoint itself, but
  1548. /// the lowering code currently does not handle that. Extending it would be
  1549. /// slightly non-trivial since it requires a format change. Given how rare
  1550. /// such cases are (for the moment?) scalarizing is an acceptable comprimise.
  1551. static void splitVectorValues(Instruction *StatepointInst,
  1552. StatepointLiveSetTy &LiveSet,
  1553. DenseMap<Value *, Value *>& PointerToBase,
  1554. DominatorTree &DT) {
  1555. SmallVector<Value *, 16> ToSplit;
  1556. for (Value *V : LiveSet)
  1557. if (isa<VectorType>(V->getType()))
  1558. ToSplit.push_back(V);
  1559. if (ToSplit.empty())
  1560. return;
  1561. DenseMap<Value *, SmallVector<Value *, 16>> ElementMapping;
  1562. Function &F = *(StatepointInst->getParent()->getParent());
  1563. DenseMap<Value *, AllocaInst *> AllocaMap;
  1564. // First is normal return, second is exceptional return (invoke only)
  1565. DenseMap<Value *, std::pair<Value *, Value *>> Replacements;
  1566. for (Value *V : ToSplit) {
  1567. AllocaInst *Alloca =
  1568. new AllocaInst(V->getType(), "", F.getEntryBlock().getFirstNonPHI());
  1569. AllocaMap[V] = Alloca;
  1570. VectorType *VT = cast<VectorType>(V->getType());
  1571. IRBuilder<> Builder(StatepointInst);
  1572. SmallVector<Value *, 16> Elements;
  1573. for (unsigned i = 0; i < VT->getNumElements(); i++)
  1574. Elements.push_back(Builder.CreateExtractElement(V, Builder.getInt32(i)));
  1575. ElementMapping[V] = Elements;
  1576. auto InsertVectorReform = [&](Instruction *IP) {
  1577. Builder.SetInsertPoint(IP);
  1578. Builder.SetCurrentDebugLocation(IP->getDebugLoc());
  1579. Value *ResultVec = UndefValue::get(VT);
  1580. for (unsigned i = 0; i < VT->getNumElements(); i++)
  1581. ResultVec = Builder.CreateInsertElement(ResultVec, Elements[i],
  1582. Builder.getInt32(i));
  1583. return ResultVec;
  1584. };
  1585. if (isa<CallInst>(StatepointInst)) {
  1586. BasicBlock::iterator Next(StatepointInst);
  1587. Next++;
  1588. Instruction *IP = &*(Next);
  1589. Replacements[V].first = InsertVectorReform(IP);
  1590. Replacements[V].second = nullptr;
  1591. } else {
  1592. InvokeInst *Invoke = cast<InvokeInst>(StatepointInst);
  1593. // We've already normalized - check that we don't have shared destination
  1594. // blocks
  1595. BasicBlock *NormalDest = Invoke->getNormalDest();
  1596. assert(!isa<PHINode>(NormalDest->begin()));
  1597. BasicBlock *UnwindDest = Invoke->getUnwindDest();
  1598. assert(!isa<PHINode>(UnwindDest->begin()));
  1599. // Insert insert element sequences in both successors
  1600. Instruction *IP = &*(NormalDest->getFirstInsertionPt());
  1601. Replacements[V].first = InsertVectorReform(IP);
  1602. IP = &*(UnwindDest->getFirstInsertionPt());
  1603. Replacements[V].second = InsertVectorReform(IP);
  1604. }
  1605. }
  1606. for (Value *V : ToSplit) {
  1607. AllocaInst *Alloca = AllocaMap[V];
  1608. // Capture all users before we start mutating use lists
  1609. SmallVector<Instruction *, 16> Users;
  1610. for (User *U : V->users())
  1611. Users.push_back(cast<Instruction>(U));
  1612. for (Instruction *I : Users) {
  1613. if (auto Phi = dyn_cast<PHINode>(I)) {
  1614. for (unsigned i = 0; i < Phi->getNumIncomingValues(); i++)
  1615. if (V == Phi->getIncomingValue(i)) {
  1616. LoadInst *Load = new LoadInst(
  1617. Alloca, "", Phi->getIncomingBlock(i)->getTerminator());
  1618. Phi->setIncomingValue(i, Load);
  1619. }
  1620. } else {
  1621. LoadInst *Load = new LoadInst(Alloca, "", I);
  1622. I->replaceUsesOfWith(V, Load);
  1623. }
  1624. }
  1625. // Store the original value and the replacement value into the alloca
  1626. StoreInst *Store = new StoreInst(V, Alloca);
  1627. if (auto I = dyn_cast<Instruction>(V))
  1628. Store->insertAfter(I);
  1629. else
  1630. Store->insertAfter(Alloca);
  1631. // Normal return for invoke, or call return
  1632. Instruction *Replacement = cast<Instruction>(Replacements[V].first);
  1633. (new StoreInst(Replacement, Alloca))->insertAfter(Replacement);
  1634. // Unwind return for invoke only
  1635. Replacement = cast_or_null<Instruction>(Replacements[V].second);
  1636. if (Replacement)
  1637. (new StoreInst(Replacement, Alloca))->insertAfter(Replacement);
  1638. }
  1639. // apply mem2reg to promote alloca to SSA
  1640. SmallVector<AllocaInst *, 16> Allocas;
  1641. for (Value *V : ToSplit)
  1642. Allocas.push_back(AllocaMap[V]);
  1643. PromoteMemToReg(Allocas, DT);
  1644. // Update our tracking of live pointers and base mappings to account for the
  1645. // changes we just made.
  1646. for (Value *V : ToSplit) {
  1647. auto &Elements = ElementMapping[V];
  1648. LiveSet.erase(V);
  1649. LiveSet.insert(Elements.begin(), Elements.end());
  1650. // We need to update the base mapping as well.
  1651. assert(PointerToBase.count(V));
  1652. Value *OldBase = PointerToBase[V];
  1653. auto &BaseElements = ElementMapping[OldBase];
  1654. PointerToBase.erase(V);
  1655. assert(Elements.size() == BaseElements.size());
  1656. for (unsigned i = 0; i < Elements.size(); i++) {
  1657. Value *Elem = Elements[i];
  1658. PointerToBase[Elem] = BaseElements[i];
  1659. }
  1660. }
  1661. }
  1662. // Helper function for the "rematerializeLiveValues". It walks use chain
  1663. // starting from the "CurrentValue" until it meets "BaseValue". Only "simple"
  1664. // values are visited (currently it is GEP's and casts). Returns true if it
  1665. // sucessfully reached "BaseValue" and false otherwise.
  1666. // Fills "ChainToBase" array with all visited values. "BaseValue" is not
  1667. // recorded.
  1668. static bool findRematerializableChainToBasePointer(
  1669. SmallVectorImpl<Instruction*> &ChainToBase,
  1670. Value *CurrentValue, Value *BaseValue) {
  1671. // We have found a base value
  1672. if (CurrentValue == BaseValue) {
  1673. return true;
  1674. }
  1675. if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(CurrentValue)) {
  1676. ChainToBase.push_back(GEP);
  1677. return findRematerializableChainToBasePointer(ChainToBase,
  1678. GEP->getPointerOperand(),
  1679. BaseValue);
  1680. }
  1681. if (CastInst *CI = dyn_cast<CastInst>(CurrentValue)) {
  1682. Value *Def = CI->stripPointerCasts();
  1683. // This two checks are basically similar. First one is here for the
  1684. // consistency with findBasePointers logic.
  1685. assert(!isa<CastInst>(Def) && "not a pointer cast found");
  1686. if (!CI->isNoopCast(CI->getModule()->getDataLayout()))
  1687. return false;
  1688. ChainToBase.push_back(CI);
  1689. return findRematerializableChainToBasePointer(ChainToBase, Def, BaseValue);
  1690. }
  1691. // Not supported instruction in the chain
  1692. return false;
  1693. }
  1694. // Helper function for the "rematerializeLiveValues". Compute cost of the use
  1695. // chain we are going to rematerialize.
  1696. static unsigned
  1697. chainToBasePointerCost(SmallVectorImpl<Instruction*> &Chain,
  1698. TargetTransformInfo &TTI) {
  1699. unsigned Cost = 0;
  1700. for (Instruction *Instr : Chain) {
  1701. if (CastInst *CI = dyn_cast<CastInst>(Instr)) {
  1702. assert(CI->isNoopCast(CI->getModule()->getDataLayout()) &&
  1703. "non noop cast is found during rematerialization");
  1704. Type *SrcTy = CI->getOperand(0)->getType();
  1705. Cost += TTI.getCastInstrCost(CI->getOpcode(), CI->getType(), SrcTy);
  1706. } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Instr)) {
  1707. // Cost of the address calculation
  1708. Type *ValTy = GEP->getPointerOperandType()->getPointerElementType();
  1709. Cost += TTI.getAddressComputationCost(ValTy);
  1710. // And cost of the GEP itself
  1711. // TODO: Use TTI->getGEPCost here (it exists, but appears to be not
  1712. // allowed for the external usage)
  1713. if (!GEP->hasAllConstantIndices())
  1714. Cost += 2;
  1715. } else {
  1716. llvm_unreachable("unsupported instruciton type during rematerialization");
  1717. }
  1718. }
  1719. return Cost;
  1720. }
  1721. // From the statepoint liveset pick values that are cheaper to recompute then to
  1722. // relocate. Remove this values from the liveset, rematerialize them after
  1723. // statepoint and record them in "Info" structure. Note that similar to
  1724. // relocated values we don't do any user adjustments here.
  1725. static void rematerializeLiveValues(CallSite CS,
  1726. PartiallyConstructedSafepointRecord &Info,
  1727. TargetTransformInfo &TTI) {
  1728. const unsigned int ChainLengthThreshold = 10;
  1729. // Record values we are going to delete from this statepoint live set.
  1730. // We can not di this in following loop due to iterator invalidation.
  1731. SmallVector<Value *, 32> LiveValuesToBeDeleted;
  1732. for (Value *LiveValue: Info.liveset) {
  1733. // For each live pointer find it's defining chain
  1734. SmallVector<Instruction *, 3> ChainToBase;
  1735. assert(Info.PointerToBase.find(LiveValue) != Info.PointerToBase.end());
  1736. bool FoundChain =
  1737. findRematerializableChainToBasePointer(ChainToBase,
  1738. LiveValue,
  1739. Info.PointerToBase[LiveValue]);
  1740. // Nothing to do, or chain is too long
  1741. if (!FoundChain ||
  1742. ChainToBase.size() == 0 ||
  1743. ChainToBase.size() > ChainLengthThreshold)
  1744. continue;
  1745. // Compute cost of this chain
  1746. unsigned Cost = chainToBasePointerCost(ChainToBase, TTI);
  1747. // TODO: We can also account for cases when we will be able to remove some
  1748. // of the rematerialized values by later optimization passes. I.e if
  1749. // we rematerialized several intersecting chains. Or if original values
  1750. // don't have any uses besides this statepoint.
  1751. // For invokes we need to rematerialize each chain twice - for normal and
  1752. // for unwind basic blocks. Model this by multiplying cost by two.
  1753. if (CS.isInvoke()) {
  1754. Cost *= 2;
  1755. }
  1756. // If it's too expensive - skip it
  1757. if (Cost >= RematerializationThreshold)
  1758. continue;
  1759. // Remove value from the live set
  1760. LiveValuesToBeDeleted.push_back(LiveValue);
  1761. // Clone instructions and record them inside "Info" structure
  1762. // Walk backwards to visit top-most instructions first
  1763. std::reverse(ChainToBase.begin(), ChainToBase.end());
  1764. // Utility function which clones all instructions from "ChainToBase"
  1765. // and inserts them before "InsertBefore". Returns rematerialized value
  1766. // which should be used after statepoint.
  1767. auto rematerializeChain = [&ChainToBase](Instruction *InsertBefore) {
  1768. Instruction *LastClonedValue = nullptr;
  1769. Instruction *LastValue = nullptr;
  1770. for (Instruction *Instr: ChainToBase) {
  1771. // Only GEP's and casts are suported as we need to be careful to not
  1772. // introduce any new uses of pointers not in the liveset.
  1773. // Note that it's fine to introduce new uses of pointers which were
  1774. // otherwise not used after this statepoint.
  1775. assert(isa<GetElementPtrInst>(Instr) || isa<CastInst>(Instr));
  1776. Instruction *ClonedValue = Instr->clone();
  1777. ClonedValue->insertBefore(InsertBefore);
  1778. ClonedValue->setName(Instr->getName() + ".remat");
  1779. // If it is not first instruction in the chain then it uses previously
  1780. // cloned value. We should update it to use cloned value.
  1781. if (LastClonedValue) {
  1782. assert(LastValue);
  1783. ClonedValue->replaceUsesOfWith(LastValue, LastClonedValue);
  1784. #ifndef NDEBUG
  1785. // Assert that cloned instruction does not use any instructions from
  1786. // this chain other than LastClonedValue
  1787. for (auto OpValue : ClonedValue->operand_values()) {
  1788. assert(std::find(ChainToBase.begin(), ChainToBase.end(), OpValue) ==
  1789. ChainToBase.end() &&
  1790. "incorrect use in rematerialization chain");
  1791. }
  1792. #endif
  1793. }
  1794. LastClonedValue = ClonedValue;
  1795. LastValue = Instr;
  1796. }
  1797. assert(LastClonedValue);
  1798. return LastClonedValue;
  1799. };
  1800. // Different cases for calls and invokes. For invokes we need to clone
  1801. // instructions both on normal and unwind path.
  1802. if (CS.isCall()) {
  1803. Instruction *InsertBefore = CS.getInstruction()->getNextNode();
  1804. assert(InsertBefore);
  1805. Instruction *RematerializedValue = rematerializeChain(InsertBefore);
  1806. Info.RematerializedValues[RematerializedValue] = LiveValue;
  1807. } else {
  1808. InvokeInst *Invoke = cast<InvokeInst>(CS.getInstruction());
  1809. Instruction *NormalInsertBefore =
  1810. Invoke->getNormalDest()->getFirstInsertionPt();
  1811. Instruction *UnwindInsertBefore =
  1812. Invoke->getUnwindDest()->getFirstInsertionPt();
  1813. Instruction *NormalRematerializedValue =
  1814. rematerializeChain(NormalInsertBefore);
  1815. Instruction *UnwindRematerializedValue =
  1816. rematerializeChain(UnwindInsertBefore);
  1817. Info.RematerializedValues[NormalRematerializedValue] = LiveValue;
  1818. Info.RematerializedValues[UnwindRematerializedValue] = LiveValue;
  1819. }
  1820. }
  1821. // Remove rematerializaed values from the live set
  1822. for (auto LiveValue: LiveValuesToBeDeleted) {
  1823. Info.liveset.erase(LiveValue);
  1824. }
  1825. }
  1826. static bool insertParsePoints(Function &F, DominatorTree &DT, Pass *P,
  1827. SmallVectorImpl<CallSite> &toUpdate) {
  1828. #ifndef NDEBUG
  1829. // sanity check the input
  1830. std::set<CallSite> uniqued;
  1831. uniqued.insert(toUpdate.begin(), toUpdate.end());
  1832. assert(uniqued.size() == toUpdate.size() && "no duplicates please!");
  1833. for (size_t i = 0; i < toUpdate.size(); i++) {
  1834. CallSite &CS = toUpdate[i];
  1835. assert(CS.getInstruction()->getParent()->getParent() == &F);
  1836. assert(isStatepoint(CS) && "expected to already be a deopt statepoint");
  1837. }
  1838. #endif
  1839. // When inserting gc.relocates for invokes, we need to be able to insert at
  1840. // the top of the successor blocks. See the comment on
  1841. // normalForInvokeSafepoint on exactly what is needed. Note that this step
  1842. // may restructure the CFG.
  1843. for (CallSite CS : toUpdate) {
  1844. if (!CS.isInvoke())
  1845. continue;
  1846. InvokeInst *invoke = cast<InvokeInst>(CS.getInstruction());
  1847. normalizeForInvokeSafepoint(invoke->getNormalDest(), invoke->getParent(),
  1848. DT);
  1849. normalizeForInvokeSafepoint(invoke->getUnwindDest(), invoke->getParent(),
  1850. DT);
  1851. }
  1852. // A list of dummy calls added to the IR to keep various values obviously
  1853. // live in the IR. We'll remove all of these when done.
  1854. SmallVector<CallInst *, 64> holders;
  1855. // Insert a dummy call with all of the arguments to the vm_state we'll need
  1856. // for the actual safepoint insertion. This ensures reference arguments in
  1857. // the deopt argument list are considered live through the safepoint (and
  1858. // thus makes sure they get relocated.)
  1859. for (size_t i = 0; i < toUpdate.size(); i++) {
  1860. CallSite &CS = toUpdate[i];
  1861. Statepoint StatepointCS(CS);
  1862. SmallVector<Value *, 64> DeoptValues;
  1863. for (Use &U : StatepointCS.vm_state_args()) {
  1864. Value *Arg = cast<Value>(&U);
  1865. assert(!isUnhandledGCPointerType(Arg->getType()) &&
  1866. "support for FCA unimplemented");
  1867. if (isHandledGCPointerType(Arg->getType()))
  1868. DeoptValues.push_back(Arg);
  1869. }
  1870. insertUseHolderAfter(CS, DeoptValues, holders);
  1871. }
  1872. SmallVector<struct PartiallyConstructedSafepointRecord, 64> records;
  1873. records.reserve(toUpdate.size());
  1874. for (size_t i = 0; i < toUpdate.size(); i++) {
  1875. struct PartiallyConstructedSafepointRecord info;
  1876. records.push_back(info);
  1877. }
  1878. assert(records.size() == toUpdate.size());
  1879. // A) Identify all gc pointers which are staticly live at the given call
  1880. // site.
  1881. findLiveReferences(F, DT, P, toUpdate, records);
  1882. // B) Find the base pointers for each live pointer
  1883. /* scope for caching */ {
  1884. // Cache the 'defining value' relation used in the computation and
  1885. // insertion of base phis and selects. This ensures that we don't insert
  1886. // large numbers of duplicate base_phis.
  1887. DefiningValueMapTy DVCache;
  1888. for (size_t i = 0; i < records.size(); i++) {
  1889. struct PartiallyConstructedSafepointRecord &info = records[i];
  1890. CallSite &CS = toUpdate[i];
  1891. findBasePointers(DT, DVCache, CS, info);
  1892. }
  1893. } // end of cache scope
  1894. // The base phi insertion logic (for any safepoint) may have inserted new
  1895. // instructions which are now live at some safepoint. The simplest such
  1896. // example is:
  1897. // loop:
  1898. // phi a <-- will be a new base_phi here
  1899. // safepoint 1 <-- that needs to be live here
  1900. // gep a + 1
  1901. // safepoint 2
  1902. // br loop
  1903. // We insert some dummy calls after each safepoint to definitely hold live
  1904. // the base pointers which were identified for that safepoint. We'll then
  1905. // ask liveness for _every_ base inserted to see what is now live. Then we
  1906. // remove the dummy calls.
  1907. holders.reserve(holders.size() + records.size());
  1908. for (size_t i = 0; i < records.size(); i++) {
  1909. struct PartiallyConstructedSafepointRecord &info = records[i];
  1910. CallSite &CS = toUpdate[i];
  1911. SmallVector<Value *, 128> Bases;
  1912. for (auto Pair : info.PointerToBase) {
  1913. Bases.push_back(Pair.second);
  1914. }
  1915. insertUseHolderAfter(CS, Bases, holders);
  1916. }
  1917. // By selecting base pointers, we've effectively inserted new uses. Thus, we
  1918. // need to rerun liveness. We may *also* have inserted new defs, but that's
  1919. // not the key issue.
  1920. recomputeLiveInValues(F, DT, P, toUpdate, records);
  1921. if (PrintBasePointers) {
  1922. for (size_t i = 0; i < records.size(); i++) {
  1923. struct PartiallyConstructedSafepointRecord &info = records[i];
  1924. errs() << "Base Pairs: (w/Relocation)\n";
  1925. for (auto Pair : info.PointerToBase) {
  1926. errs() << " derived %" << Pair.first->getName() << " base %"
  1927. << Pair.second->getName() << "\n";
  1928. }
  1929. }
  1930. }
  1931. for (size_t i = 0; i < holders.size(); i++) {
  1932. holders[i]->eraseFromParent();
  1933. holders[i] = nullptr;
  1934. }
  1935. holders.clear();
  1936. // Do a limited scalarization of any live at safepoint vector values which
  1937. // contain pointers. This enables this pass to run after vectorization at
  1938. // the cost of some possible performance loss. TODO: it would be nice to
  1939. // natively support vectors all the way through the backend so we don't need
  1940. // to scalarize here.
  1941. for (size_t i = 0; i < records.size(); i++) {
  1942. struct PartiallyConstructedSafepointRecord &info = records[i];
  1943. Instruction *statepoint = toUpdate[i].getInstruction();
  1944. splitVectorValues(cast<Instruction>(statepoint), info.liveset,
  1945. info.PointerToBase, DT);
  1946. }
  1947. // In order to reduce live set of statepoint we might choose to rematerialize
  1948. // some values instead of relocating them. This is purelly an optimization and
  1949. // does not influence correctness.
  1950. TargetTransformInfo &TTI =
  1951. P->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
  1952. for (size_t i = 0; i < records.size(); i++) {
  1953. struct PartiallyConstructedSafepointRecord &info = records[i];
  1954. CallSite &CS = toUpdate[i];
  1955. rematerializeLiveValues(CS, info, TTI);
  1956. }
  1957. // Now run through and replace the existing statepoints with new ones with
  1958. // the live variables listed. We do not yet update uses of the values being
  1959. // relocated. We have references to live variables that need to
  1960. // survive to the last iteration of this loop. (By construction, the
  1961. // previous statepoint can not be a live variable, thus we can and remove
  1962. // the old statepoint calls as we go.)
  1963. for (size_t i = 0; i < records.size(); i++) {
  1964. struct PartiallyConstructedSafepointRecord &info = records[i];
  1965. CallSite &CS = toUpdate[i];
  1966. makeStatepointExplicit(DT, CS, P, info);
  1967. }
  1968. toUpdate.clear(); // prevent accident use of invalid CallSites
  1969. // Do all the fixups of the original live variables to their relocated selves
  1970. SmallVector<Value *, 128> live;
  1971. for (size_t i = 0; i < records.size(); i++) {
  1972. struct PartiallyConstructedSafepointRecord &info = records[i];
  1973. // We can't simply save the live set from the original insertion. One of
  1974. // the live values might be the result of a call which needs a safepoint.
  1975. // That Value* no longer exists and we need to use the new gc_result.
  1976. // Thankfully, the liveset is embedded in the statepoint (and updated), so
  1977. // we just grab that.
  1978. Statepoint statepoint(info.StatepointToken);
  1979. live.insert(live.end(), statepoint.gc_args_begin(),
  1980. statepoint.gc_args_end());
  1981. #ifndef NDEBUG
  1982. // Do some basic sanity checks on our liveness results before performing
  1983. // relocation. Relocation can and will turn mistakes in liveness results
  1984. // into non-sensical code which is must harder to debug.
  1985. // TODO: It would be nice to test consistency as well
  1986. assert(DT.isReachableFromEntry(info.StatepointToken->getParent()) &&
  1987. "statepoint must be reachable or liveness is meaningless");
  1988. for (Value *V : statepoint.gc_args()) {
  1989. if (!isa<Instruction>(V))
  1990. // Non-instruction values trivial dominate all possible uses
  1991. continue;
  1992. auto LiveInst = cast<Instruction>(V);
  1993. assert(DT.isReachableFromEntry(LiveInst->getParent()) &&
  1994. "unreachable values should never be live");
  1995. assert(DT.dominates(LiveInst, info.StatepointToken) &&
  1996. "basic SSA liveness expectation violated by liveness analysis");
  1997. }
  1998. #endif
  1999. }
  2000. unique_unsorted(live);
  2001. #ifndef NDEBUG
  2002. // sanity check
  2003. for (auto ptr : live) {
  2004. assert(isGCPointerType(ptr->getType()) && "must be a gc pointer type");
  2005. }
  2006. #endif
  2007. relocationViaAlloca(F, DT, live, records);
  2008. return !records.empty();
  2009. }
  2010. // Handles both return values and arguments for Functions and CallSites.
  2011. template <typename AttrHolder>
  2012. static void RemoveDerefAttrAtIndex(LLVMContext &Ctx, AttrHolder &AH,
  2013. unsigned Index) {
  2014. AttrBuilder R;
  2015. if (AH.getDereferenceableBytes(Index))
  2016. R.addAttribute(Attribute::get(Ctx, Attribute::Dereferenceable,
  2017. AH.getDereferenceableBytes(Index)));
  2018. if (AH.getDereferenceableOrNullBytes(Index))
  2019. R.addAttribute(Attribute::get(Ctx, Attribute::DereferenceableOrNull,
  2020. AH.getDereferenceableOrNullBytes(Index)));
  2021. if (!R.empty())
  2022. AH.setAttributes(AH.getAttributes().removeAttributes(
  2023. Ctx, Index, AttributeSet::get(Ctx, Index, R)));
  2024. }
  2025. void
  2026. RewriteStatepointsForGC::stripDereferenceabilityInfoFromPrototype(Function &F) {
  2027. LLVMContext &Ctx = F.getContext();
  2028. for (Argument &A : F.args())
  2029. if (isa<PointerType>(A.getType()))
  2030. RemoveDerefAttrAtIndex(Ctx, F, A.getArgNo() + 1);
  2031. if (isa<PointerType>(F.getReturnType()))
  2032. RemoveDerefAttrAtIndex(Ctx, F, AttributeSet::ReturnIndex);
  2033. }
  2034. void RewriteStatepointsForGC::stripDereferenceabilityInfoFromBody(Function &F) {
  2035. if (F.empty())
  2036. return;
  2037. LLVMContext &Ctx = F.getContext();
  2038. MDBuilder Builder(Ctx);
  2039. for (Instruction &I : inst_range(F)) {
  2040. if (const MDNode *MD = I.getMetadata(LLVMContext::MD_tbaa)) {
  2041. assert(MD->getNumOperands() < 5 && "unrecognized metadata shape!");
  2042. bool IsImmutableTBAA =
  2043. MD->getNumOperands() == 4 &&
  2044. mdconst::extract<ConstantInt>(MD->getOperand(3))->getValue() == 1;
  2045. if (!IsImmutableTBAA)
  2046. continue; // no work to do, MD_tbaa is already marked mutable
  2047. MDNode *Base = cast<MDNode>(MD->getOperand(0));
  2048. MDNode *Access = cast<MDNode>(MD->getOperand(1));
  2049. uint64_t Offset =
  2050. mdconst::extract<ConstantInt>(MD->getOperand(2))->getZExtValue();
  2051. MDNode *MutableTBAA =
  2052. Builder.createTBAAStructTagNode(Base, Access, Offset);
  2053. I.setMetadata(LLVMContext::MD_tbaa, MutableTBAA);
  2054. }
  2055. if (CallSite CS = CallSite(&I)) {
  2056. for (int i = 0, e = CS.arg_size(); i != e; i++)
  2057. if (isa<PointerType>(CS.getArgument(i)->getType()))
  2058. RemoveDerefAttrAtIndex(Ctx, CS, i + 1);
  2059. if (isa<PointerType>(CS.getType()))
  2060. RemoveDerefAttrAtIndex(Ctx, CS, AttributeSet::ReturnIndex);
  2061. }
  2062. }
  2063. }
  2064. /// Returns true if this function should be rewritten by this pass. The main
  2065. /// point of this function is as an extension point for custom logic.
  2066. static bool shouldRewriteStatepointsIn(Function &F) {
  2067. // TODO: This should check the GCStrategy
  2068. if (F.hasGC()) {
  2069. const char *FunctionGCName = F.getGC();
  2070. const StringRef StatepointExampleName("statepoint-example");
  2071. const StringRef CoreCLRName("coreclr");
  2072. return (StatepointExampleName == FunctionGCName) ||
  2073. (CoreCLRName == FunctionGCName);
  2074. } else
  2075. return false;
  2076. }
  2077. void RewriteStatepointsForGC::stripDereferenceabilityInfo(Module &M) {
  2078. #ifndef NDEBUG
  2079. assert(std::any_of(M.begin(), M.end(), shouldRewriteStatepointsIn) &&
  2080. "precondition!");
  2081. #endif
  2082. for (Function &F : M)
  2083. stripDereferenceabilityInfoFromPrototype(F);
  2084. for (Function &F : M)
  2085. stripDereferenceabilityInfoFromBody(F);
  2086. }
  2087. bool RewriteStatepointsForGC::runOnFunction(Function &F) {
  2088. // Nothing to do for declarations.
  2089. if (F.isDeclaration() || F.empty())
  2090. return false;
  2091. // Policy choice says not to rewrite - the most common reason is that we're
  2092. // compiling code without a GCStrategy.
  2093. if (!shouldRewriteStatepointsIn(F))
  2094. return false;
  2095. DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
  2096. // Gather all the statepoints which need rewritten. Be careful to only
  2097. // consider those in reachable code since we need to ask dominance queries
  2098. // when rewriting. We'll delete the unreachable ones in a moment.
  2099. SmallVector<CallSite, 64> ParsePointNeeded;
  2100. bool HasUnreachableStatepoint = false;
  2101. for (Instruction &I : inst_range(F)) {
  2102. // TODO: only the ones with the flag set!
  2103. if (isStatepoint(I)) {
  2104. if (DT.isReachableFromEntry(I.getParent()))
  2105. ParsePointNeeded.push_back(CallSite(&I));
  2106. else
  2107. HasUnreachableStatepoint = true;
  2108. }
  2109. }
  2110. bool MadeChange = false;
  2111. // Delete any unreachable statepoints so that we don't have unrewritten
  2112. // statepoints surviving this pass. This makes testing easier and the
  2113. // resulting IR less confusing to human readers. Rather than be fancy, we
  2114. // just reuse a utility function which removes the unreachable blocks.
  2115. if (HasUnreachableStatepoint)
  2116. MadeChange |= removeUnreachableBlocks(F);
  2117. // Return early if no work to do.
  2118. if (ParsePointNeeded.empty())
  2119. return MadeChange;
  2120. // As a prepass, go ahead and aggressively destroy single entry phi nodes.
  2121. // These are created by LCSSA. They have the effect of increasing the size
  2122. // of liveness sets for no good reason. It may be harder to do this post
  2123. // insertion since relocations and base phis can confuse things.
  2124. for (BasicBlock &BB : F)
  2125. if (BB.getUniquePredecessor()) {
  2126. MadeChange = true;
  2127. FoldSingleEntryPHINodes(&BB);
  2128. }
  2129. MadeChange |= insertParsePoints(F, DT, this, ParsePointNeeded);
  2130. return MadeChange;
  2131. }
  2132. // liveness computation via standard dataflow
  2133. // -------------------------------------------------------------------
  2134. // TODO: Consider using bitvectors for liveness, the set of potentially
  2135. // interesting values should be small and easy to pre-compute.
  2136. /// Compute the live-in set for the location rbegin starting from
  2137. /// the live-out set of the basic block
  2138. static void computeLiveInValues(BasicBlock::reverse_iterator rbegin,
  2139. BasicBlock::reverse_iterator rend,
  2140. DenseSet<Value *> &LiveTmp) {
  2141. for (BasicBlock::reverse_iterator ritr = rbegin; ritr != rend; ritr++) {
  2142. Instruction *I = &*ritr;
  2143. // KILL/Def - Remove this definition from LiveIn
  2144. LiveTmp.erase(I);
  2145. // Don't consider *uses* in PHI nodes, we handle their contribution to
  2146. // predecessor blocks when we seed the LiveOut sets
  2147. if (isa<PHINode>(I))
  2148. continue;
  2149. // USE - Add to the LiveIn set for this instruction
  2150. for (Value *V : I->operands()) {
  2151. assert(!isUnhandledGCPointerType(V->getType()) &&
  2152. "support for FCA unimplemented");
  2153. if (isHandledGCPointerType(V->getType()) && !isa<Constant>(V)) {
  2154. // The choice to exclude all things constant here is slightly subtle.
  2155. // There are two idependent reasons:
  2156. // - We assume that things which are constant (from LLVM's definition)
  2157. // do not move at runtime. For example, the address of a global
  2158. // variable is fixed, even though it's contents may not be.
  2159. // - Second, we can't disallow arbitrary inttoptr constants even
  2160. // if the language frontend does. Optimization passes are free to
  2161. // locally exploit facts without respect to global reachability. This
  2162. // can create sections of code which are dynamically unreachable and
  2163. // contain just about anything. (see constants.ll in tests)
  2164. LiveTmp.insert(V);
  2165. }
  2166. }
  2167. }
  2168. }
  2169. static void computeLiveOutSeed(BasicBlock *BB, DenseSet<Value *> &LiveTmp) {
  2170. for (BasicBlock *Succ : successors(BB)) {
  2171. const BasicBlock::iterator E(Succ->getFirstNonPHI());
  2172. for (BasicBlock::iterator I = Succ->begin(); I != E; I++) {
  2173. PHINode *Phi = cast<PHINode>(&*I);
  2174. Value *V = Phi->getIncomingValueForBlock(BB);
  2175. assert(!isUnhandledGCPointerType(V->getType()) &&
  2176. "support for FCA unimplemented");
  2177. if (isHandledGCPointerType(V->getType()) && !isa<Constant>(V)) {
  2178. LiveTmp.insert(V);
  2179. }
  2180. }
  2181. }
  2182. }
  2183. static DenseSet<Value *> computeKillSet(BasicBlock *BB) {
  2184. DenseSet<Value *> KillSet;
  2185. for (Instruction &I : *BB)
  2186. if (isHandledGCPointerType(I.getType()))
  2187. KillSet.insert(&I);
  2188. return KillSet;
  2189. }
  2190. #ifndef NDEBUG
  2191. /// Check that the items in 'Live' dominate 'TI'. This is used as a basic
  2192. /// sanity check for the liveness computation.
  2193. static void checkBasicSSA(DominatorTree &DT, DenseSet<Value *> &Live,
  2194. TerminatorInst *TI, bool TermOkay = false) {
  2195. for (Value *V : Live) {
  2196. if (auto *I = dyn_cast<Instruction>(V)) {
  2197. // The terminator can be a member of the LiveOut set. LLVM's definition
  2198. // of instruction dominance states that V does not dominate itself. As
  2199. // such, we need to special case this to allow it.
  2200. if (TermOkay && TI == I)
  2201. continue;
  2202. assert(DT.dominates(I, TI) &&
  2203. "basic SSA liveness expectation violated by liveness analysis");
  2204. }
  2205. }
  2206. }
  2207. /// Check that all the liveness sets used during the computation of liveness
  2208. /// obey basic SSA properties. This is useful for finding cases where we miss
  2209. /// a def.
  2210. static void checkBasicSSA(DominatorTree &DT, GCPtrLivenessData &Data,
  2211. BasicBlock &BB) {
  2212. checkBasicSSA(DT, Data.LiveSet[&BB], BB.getTerminator());
  2213. checkBasicSSA(DT, Data.LiveOut[&BB], BB.getTerminator(), true);
  2214. checkBasicSSA(DT, Data.LiveIn[&BB], BB.getTerminator());
  2215. }
  2216. #endif
  2217. static void computeLiveInValues(DominatorTree &DT, Function &F,
  2218. GCPtrLivenessData &Data) {
  2219. SmallSetVector<BasicBlock *, 200> Worklist;
  2220. auto AddPredsToWorklist = [&](BasicBlock *BB) {
  2221. // We use a SetVector so that we don't have duplicates in the worklist.
  2222. Worklist.insert(pred_begin(BB), pred_end(BB));
  2223. };
  2224. auto NextItem = [&]() {
  2225. BasicBlock *BB = Worklist.back();
  2226. Worklist.pop_back();
  2227. return BB;
  2228. };
  2229. // Seed the liveness for each individual block
  2230. for (BasicBlock &BB : F) {
  2231. Data.KillSet[&BB] = computeKillSet(&BB);
  2232. Data.LiveSet[&BB].clear();
  2233. computeLiveInValues(BB.rbegin(), BB.rend(), Data.LiveSet[&BB]);
  2234. #ifndef NDEBUG
  2235. for (Value *Kill : Data.KillSet[&BB])
  2236. assert(!Data.LiveSet[&BB].count(Kill) && "live set contains kill");
  2237. #endif
  2238. Data.LiveOut[&BB] = DenseSet<Value *>();
  2239. computeLiveOutSeed(&BB, Data.LiveOut[&BB]);
  2240. Data.LiveIn[&BB] = Data.LiveSet[&BB];
  2241. set_union(Data.LiveIn[&BB], Data.LiveOut[&BB]);
  2242. set_subtract(Data.LiveIn[&BB], Data.KillSet[&BB]);
  2243. if (!Data.LiveIn[&BB].empty())
  2244. AddPredsToWorklist(&BB);
  2245. }
  2246. // Propagate that liveness until stable
  2247. while (!Worklist.empty()) {
  2248. BasicBlock *BB = NextItem();
  2249. // Compute our new liveout set, then exit early if it hasn't changed
  2250. // despite the contribution of our successor.
  2251. DenseSet<Value *> LiveOut = Data.LiveOut[BB];
  2252. const auto OldLiveOutSize = LiveOut.size();
  2253. for (BasicBlock *Succ : successors(BB)) {
  2254. assert(Data.LiveIn.count(Succ));
  2255. set_union(LiveOut, Data.LiveIn[Succ]);
  2256. }
  2257. // assert OutLiveOut is a subset of LiveOut
  2258. if (OldLiveOutSize == LiveOut.size()) {
  2259. // If the sets are the same size, then we didn't actually add anything
  2260. // when unioning our successors LiveIn Thus, the LiveIn of this block
  2261. // hasn't changed.
  2262. continue;
  2263. }
  2264. Data.LiveOut[BB] = LiveOut;
  2265. // Apply the effects of this basic block
  2266. DenseSet<Value *> LiveTmp = LiveOut;
  2267. set_union(LiveTmp, Data.LiveSet[BB]);
  2268. set_subtract(LiveTmp, Data.KillSet[BB]);
  2269. assert(Data.LiveIn.count(BB));
  2270. const DenseSet<Value *> &OldLiveIn = Data.LiveIn[BB];
  2271. // assert: OldLiveIn is a subset of LiveTmp
  2272. if (OldLiveIn.size() != LiveTmp.size()) {
  2273. Data.LiveIn[BB] = LiveTmp;
  2274. AddPredsToWorklist(BB);
  2275. }
  2276. } // while( !worklist.empty() )
  2277. #ifndef NDEBUG
  2278. // Sanity check our ouput against SSA properties. This helps catch any
  2279. // missing kills during the above iteration.
  2280. for (BasicBlock &BB : F) {
  2281. checkBasicSSA(DT, Data, BB);
  2282. }
  2283. #endif
  2284. }
  2285. static void findLiveSetAtInst(Instruction *Inst, GCPtrLivenessData &Data,
  2286. StatepointLiveSetTy &Out) {
  2287. BasicBlock *BB = Inst->getParent();
  2288. // Note: The copy is intentional and required
  2289. assert(Data.LiveOut.count(BB));
  2290. DenseSet<Value *> LiveOut = Data.LiveOut[BB];
  2291. // We want to handle the statepoint itself oddly. It's
  2292. // call result is not live (normal), nor are it's arguments
  2293. // (unless they're used again later). This adjustment is
  2294. // specifically what we need to relocate
  2295. BasicBlock::reverse_iterator rend(Inst);
  2296. computeLiveInValues(BB->rbegin(), rend, LiveOut);
  2297. LiveOut.erase(Inst);
  2298. Out.insert(LiveOut.begin(), LiveOut.end());
  2299. }
  2300. static void recomputeLiveInValues(GCPtrLivenessData &RevisedLivenessData,
  2301. const CallSite &CS,
  2302. PartiallyConstructedSafepointRecord &Info) {
  2303. Instruction *Inst = CS.getInstruction();
  2304. StatepointLiveSetTy Updated;
  2305. findLiveSetAtInst(Inst, RevisedLivenessData, Updated);
  2306. #ifndef NDEBUG
  2307. DenseSet<Value *> Bases;
  2308. for (auto KVPair : Info.PointerToBase) {
  2309. Bases.insert(KVPair.second);
  2310. }
  2311. #endif
  2312. // We may have base pointers which are now live that weren't before. We need
  2313. // to update the PointerToBase structure to reflect this.
  2314. for (auto V : Updated)
  2315. if (!Info.PointerToBase.count(V)) {
  2316. assert(Bases.count(V) && "can't find base for unexpected live value");
  2317. Info.PointerToBase[V] = V;
  2318. continue;
  2319. }
  2320. #ifndef NDEBUG
  2321. for (auto V : Updated) {
  2322. assert(Info.PointerToBase.count(V) &&
  2323. "must be able to find base for live value");
  2324. }
  2325. #endif
  2326. // Remove any stale base mappings - this can happen since our liveness is
  2327. // more precise then the one inherent in the base pointer analysis
  2328. DenseSet<Value *> ToErase;
  2329. for (auto KVPair : Info.PointerToBase)
  2330. if (!Updated.count(KVPair.first))
  2331. ToErase.insert(KVPair.first);
  2332. for (auto V : ToErase)
  2333. Info.PointerToBase.erase(V);
  2334. #ifndef NDEBUG
  2335. for (auto KVPair : Info.PointerToBase)
  2336. assert(Updated.count(KVPair.first) && "record for non-live value");
  2337. #endif
  2338. Info.liveset = Updated;
  2339. }