SafepointIRVerifier.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. //===-- SafepointIRVerifier.cpp - Verify gc.statepoint invariants ---------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Run a basic correctness check on the IR to ensure that Safepoints - if
  10. // they've been inserted - were inserted correctly. In particular, look for use
  11. // of non-relocated values after a safepoint. It's primary use is to check the
  12. // correctness of safepoint insertion immediately after insertion, but it can
  13. // also be used to verify that later transforms have not found a way to break
  14. // safepoint semenatics.
  15. //
  16. // In its current form, this verify checks a property which is sufficient, but
  17. // not neccessary for correctness. There are some cases where an unrelocated
  18. // pointer can be used after the safepoint. Consider this example:
  19. //
  20. // a = ...
  21. // b = ...
  22. // (a',b') = safepoint(a,b)
  23. // c = cmp eq a b
  24. // br c, ..., ....
  25. //
  26. // Because it is valid to reorder 'c' above the safepoint, this is legal. In
  27. // practice, this is a somewhat uncommon transform, but CodeGenPrep does create
  28. // idioms like this. The verifier knows about these cases and avoids reporting
  29. // false positives.
  30. //
  31. //===----------------------------------------------------------------------===//
  32. #include "llvm/IR/SafepointIRVerifier.h"
  33. #include "llvm/ADT/DenseSet.h"
  34. #include "llvm/ADT/PostOrderIterator.h"
  35. #include "llvm/ADT/SetOperations.h"
  36. #include "llvm/ADT/SetVector.h"
  37. #include "llvm/IR/BasicBlock.h"
  38. #include "llvm/IR/Dominators.h"
  39. #include "llvm/IR/Function.h"
  40. #include "llvm/IR/InstrTypes.h"
  41. #include "llvm/IR/Instructions.h"
  42. #include "llvm/IR/Statepoint.h"
  43. #include "llvm/IR/Value.h"
  44. #include "llvm/InitializePasses.h"
  45. #include "llvm/Support/Allocator.h"
  46. #include "llvm/Support/CommandLine.h"
  47. #include "llvm/Support/Debug.h"
  48. #include "llvm/Support/raw_ostream.h"
  49. #define DEBUG_TYPE "safepoint-ir-verifier"
  50. using namespace llvm;
  51. /// This option is used for writing test cases. Instead of crashing the program
  52. /// when verification fails, report a message to the console (for FileCheck
  53. /// usage) and continue execution as if nothing happened.
  54. static cl::opt<bool> PrintOnly("safepoint-ir-verifier-print-only",
  55. cl::init(false));
  56. namespace {
  57. /// This CFG Deadness finds dead blocks and edges. Algorithm starts with a set
  58. /// of blocks unreachable from entry then propagates deadness using foldable
  59. /// conditional branches without modifying CFG. So GVN does but it changes CFG
  60. /// by splitting critical edges. In most cases passes rely on SimplifyCFG to
  61. /// clean up dead blocks, but in some cases, like verification or loop passes
  62. /// it's not possible.
  63. class CFGDeadness {
  64. const DominatorTree *DT = nullptr;
  65. SetVector<const BasicBlock *> DeadBlocks;
  66. SetVector<const Use *> DeadEdges; // Contains all dead edges from live blocks.
  67. public:
  68. /// Return the edge that coresponds to the predecessor.
  69. static const Use& getEdge(const_pred_iterator &PredIt) {
  70. auto &PU = PredIt.getUse();
  71. return PU.getUser()->getOperandUse(PU.getOperandNo());
  72. }
  73. /// Return true if there is at least one live edge that corresponds to the
  74. /// basic block InBB listed in the phi node.
  75. bool hasLiveIncomingEdge(const PHINode *PN, const BasicBlock *InBB) const {
  76. assert(!isDeadBlock(InBB) && "block must be live");
  77. const BasicBlock* BB = PN->getParent();
  78. bool Listed = false;
  79. for (const_pred_iterator PredIt(BB), End(BB, true); PredIt != End; ++PredIt) {
  80. if (InBB == *PredIt) {
  81. if (!isDeadEdge(&getEdge(PredIt)))
  82. return true;
  83. Listed = true;
  84. }
  85. }
  86. (void)Listed;
  87. assert(Listed && "basic block is not found among incoming blocks");
  88. return false;
  89. }
  90. bool isDeadBlock(const BasicBlock *BB) const {
  91. return DeadBlocks.count(BB);
  92. }
  93. bool isDeadEdge(const Use *U) const {
  94. assert(cast<Instruction>(U->getUser())->isTerminator() &&
  95. "edge must be operand of terminator");
  96. assert(cast_or_null<BasicBlock>(U->get()) &&
  97. "edge must refer to basic block");
  98. assert(!isDeadBlock(cast<Instruction>(U->getUser())->getParent()) &&
  99. "isDeadEdge() must be applied to edge from live block");
  100. return DeadEdges.count(U);
  101. }
  102. bool hasLiveIncomingEdges(const BasicBlock *BB) const {
  103. // Check if all incoming edges are dead.
  104. for (const_pred_iterator PredIt(BB), End(BB, true); PredIt != End; ++PredIt) {
  105. auto &PU = PredIt.getUse();
  106. const Use &U = PU.getUser()->getOperandUse(PU.getOperandNo());
  107. if (!isDeadBlock(*PredIt) && !isDeadEdge(&U))
  108. return true; // Found a live edge.
  109. }
  110. return false;
  111. }
  112. void processFunction(const Function &F, const DominatorTree &DT) {
  113. this->DT = &DT;
  114. // Start with all blocks unreachable from entry.
  115. for (const BasicBlock &BB : F)
  116. if (!DT.isReachableFromEntry(&BB))
  117. DeadBlocks.insert(&BB);
  118. // Top-down walk of the dominator tree
  119. ReversePostOrderTraversal<const Function *> RPOT(&F);
  120. for (const BasicBlock *BB : RPOT) {
  121. const Instruction *TI = BB->getTerminator();
  122. assert(TI && "blocks must be well formed");
  123. // For conditional branches, we can perform simple conditional propagation on
  124. // the condition value itself.
  125. const BranchInst *BI = dyn_cast<BranchInst>(TI);
  126. if (!BI || !BI->isConditional() || !isa<Constant>(BI->getCondition()))
  127. continue;
  128. // If a branch has two identical successors, we cannot declare either dead.
  129. if (BI->getSuccessor(0) == BI->getSuccessor(1))
  130. continue;
  131. ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
  132. if (!Cond)
  133. continue;
  134. addDeadEdge(BI->getOperandUse(Cond->getZExtValue() ? 1 : 2));
  135. }
  136. }
  137. protected:
  138. void addDeadBlock(const BasicBlock *BB) {
  139. SmallVector<const BasicBlock *, 4> NewDead;
  140. SmallSetVector<const BasicBlock *, 4> DF;
  141. NewDead.push_back(BB);
  142. while (!NewDead.empty()) {
  143. const BasicBlock *D = NewDead.pop_back_val();
  144. if (isDeadBlock(D))
  145. continue;
  146. // All blocks dominated by D are dead.
  147. SmallVector<BasicBlock *, 8> Dom;
  148. DT->getDescendants(const_cast<BasicBlock*>(D), Dom);
  149. // Do not need to mark all in and out edges dead
  150. // because BB is marked dead and this is enough
  151. // to run further.
  152. DeadBlocks.insert(Dom.begin(), Dom.end());
  153. // Figure out the dominance-frontier(D).
  154. for (BasicBlock *B : Dom)
  155. for (BasicBlock *S : successors(B))
  156. if (!isDeadBlock(S) && !hasLiveIncomingEdges(S))
  157. NewDead.push_back(S);
  158. }
  159. }
  160. void addDeadEdge(const Use &DeadEdge) {
  161. if (!DeadEdges.insert(&DeadEdge))
  162. return;
  163. BasicBlock *BB = cast_or_null<BasicBlock>(DeadEdge.get());
  164. if (hasLiveIncomingEdges(BB))
  165. return;
  166. addDeadBlock(BB);
  167. }
  168. };
  169. } // namespace
  170. static void Verify(const Function &F, const DominatorTree &DT,
  171. const CFGDeadness &CD);
  172. namespace llvm {
  173. PreservedAnalyses SafepointIRVerifierPass::run(Function &F,
  174. FunctionAnalysisManager &AM) {
  175. const auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
  176. CFGDeadness CD;
  177. CD.processFunction(F, DT);
  178. Verify(F, DT, CD);
  179. return PreservedAnalyses::all();
  180. }
  181. } // namespace llvm
  182. namespace {
  183. struct SafepointIRVerifier : public FunctionPass {
  184. static char ID; // Pass identification, replacement for typeid
  185. SafepointIRVerifier() : FunctionPass(ID) {
  186. initializeSafepointIRVerifierPass(*PassRegistry::getPassRegistry());
  187. }
  188. bool runOnFunction(Function &F) override {
  189. auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  190. CFGDeadness CD;
  191. CD.processFunction(F, DT);
  192. Verify(F, DT, CD);
  193. return false; // no modifications
  194. }
  195. void getAnalysisUsage(AnalysisUsage &AU) const override {
  196. AU.addRequiredID(DominatorTreeWrapperPass::ID);
  197. AU.setPreservesAll();
  198. }
  199. StringRef getPassName() const override { return "safepoint verifier"; }
  200. };
  201. } // namespace
  202. void llvm::verifySafepointIR(Function &F) {
  203. SafepointIRVerifier pass;
  204. pass.runOnFunction(F);
  205. }
  206. char SafepointIRVerifier::ID = 0;
  207. FunctionPass *llvm::createSafepointIRVerifierPass() {
  208. return new SafepointIRVerifier();
  209. }
  210. INITIALIZE_PASS_BEGIN(SafepointIRVerifier, "verify-safepoint-ir",
  211. "Safepoint IR Verifier", false, false)
  212. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  213. INITIALIZE_PASS_END(SafepointIRVerifier, "verify-safepoint-ir",
  214. "Safepoint IR Verifier", false, false)
  215. static bool isGCPointerType(Type *T) {
  216. if (auto *PT = dyn_cast<PointerType>(T))
  217. // For the sake of this example GC, we arbitrarily pick addrspace(1) as our
  218. // GC managed heap. We know that a pointer into this heap needs to be
  219. // updated and that no other pointer does.
  220. return (1 == PT->getAddressSpace());
  221. return false;
  222. }
  223. static bool containsGCPtrType(Type *Ty) {
  224. if (isGCPointerType(Ty))
  225. return true;
  226. if (VectorType *VT = dyn_cast<VectorType>(Ty))
  227. return isGCPointerType(VT->getScalarType());
  228. if (ArrayType *AT = dyn_cast<ArrayType>(Ty))
  229. return containsGCPtrType(AT->getElementType());
  230. if (StructType *ST = dyn_cast<StructType>(Ty))
  231. return llvm::any_of(ST->elements(), containsGCPtrType);
  232. return false;
  233. }
  234. // Debugging aid -- prints a [Begin, End) range of values.
  235. template<typename IteratorTy>
  236. static void PrintValueSet(raw_ostream &OS, IteratorTy Begin, IteratorTy End) {
  237. OS << "[ ";
  238. while (Begin != End) {
  239. OS << **Begin << " ";
  240. ++Begin;
  241. }
  242. OS << "]";
  243. }
  244. /// The verifier algorithm is phrased in terms of availability. The set of
  245. /// values "available" at a given point in the control flow graph is the set of
  246. /// correctly relocated value at that point, and is a subset of the set of
  247. /// definitions dominating that point.
  248. using AvailableValueSet = DenseSet<const Value *>;
  249. /// State we compute and track per basic block.
  250. struct BasicBlockState {
  251. // Set of values available coming in, before the phi nodes
  252. AvailableValueSet AvailableIn;
  253. // Set of values available going out
  254. AvailableValueSet AvailableOut;
  255. // AvailableOut minus AvailableIn.
  256. // All elements are Instructions
  257. AvailableValueSet Contribution;
  258. // True if this block contains a safepoint and thus AvailableIn does not
  259. // contribute to AvailableOut.
  260. bool Cleared = false;
  261. };
  262. /// A given derived pointer can have multiple base pointers through phi/selects.
  263. /// This type indicates when the base pointer is exclusively constant
  264. /// (ExclusivelySomeConstant), and if that constant is proven to be exclusively
  265. /// null, we record that as ExclusivelyNull. In all other cases, the BaseType is
  266. /// NonConstant.
  267. enum BaseType {
  268. NonConstant = 1, // Base pointers is not exclusively constant.
  269. ExclusivelyNull,
  270. ExclusivelySomeConstant // Base pointers for a given derived pointer is from a
  271. // set of constants, but they are not exclusively
  272. // null.
  273. };
  274. /// Return the baseType for Val which states whether Val is exclusively
  275. /// derived from constant/null, or not exclusively derived from constant.
  276. /// Val is exclusively derived off a constant base when all operands of phi and
  277. /// selects are derived off a constant base.
  278. static enum BaseType getBaseType(const Value *Val) {
  279. SmallVector<const Value *, 32> Worklist;
  280. DenseSet<const Value *> Visited;
  281. bool isExclusivelyDerivedFromNull = true;
  282. Worklist.push_back(Val);
  283. // Strip through all the bitcasts and geps to get base pointer. Also check for
  284. // the exclusive value when there can be multiple base pointers (through phis
  285. // or selects).
  286. while(!Worklist.empty()) {
  287. const Value *V = Worklist.pop_back_val();
  288. if (!Visited.insert(V).second)
  289. continue;
  290. if (const auto *CI = dyn_cast<CastInst>(V)) {
  291. Worklist.push_back(CI->stripPointerCasts());
  292. continue;
  293. }
  294. if (const auto *GEP = dyn_cast<GetElementPtrInst>(V)) {
  295. Worklist.push_back(GEP->getPointerOperand());
  296. continue;
  297. }
  298. // Push all the incoming values of phi node into the worklist for
  299. // processing.
  300. if (const auto *PN = dyn_cast<PHINode>(V)) {
  301. append_range(Worklist, PN->incoming_values());
  302. continue;
  303. }
  304. if (const auto *SI = dyn_cast<SelectInst>(V)) {
  305. // Push in the true and false values
  306. Worklist.push_back(SI->getTrueValue());
  307. Worklist.push_back(SI->getFalseValue());
  308. continue;
  309. }
  310. if (isa<Constant>(V)) {
  311. // We found at least one base pointer which is non-null, so this derived
  312. // pointer is not exclusively derived from null.
  313. if (V != Constant::getNullValue(V->getType()))
  314. isExclusivelyDerivedFromNull = false;
  315. // Continue processing the remaining values to make sure it's exclusively
  316. // constant.
  317. continue;
  318. }
  319. // At this point, we know that the base pointer is not exclusively
  320. // constant.
  321. return BaseType::NonConstant;
  322. }
  323. // Now, we know that the base pointer is exclusively constant, but we need to
  324. // differentiate between exclusive null constant and non-null constant.
  325. return isExclusivelyDerivedFromNull ? BaseType::ExclusivelyNull
  326. : BaseType::ExclusivelySomeConstant;
  327. }
  328. static bool isNotExclusivelyConstantDerived(const Value *V) {
  329. return getBaseType(V) == BaseType::NonConstant;
  330. }
  331. namespace {
  332. class InstructionVerifier;
  333. /// Builds BasicBlockState for each BB of the function.
  334. /// It can traverse function for verification and provides all required
  335. /// information.
  336. ///
  337. /// GC pointer may be in one of three states: relocated, unrelocated and
  338. /// poisoned.
  339. /// Relocated pointer may be used without any restrictions.
  340. /// Unrelocated pointer cannot be dereferenced, passed as argument to any call
  341. /// or returned. Unrelocated pointer may be safely compared against another
  342. /// unrelocated pointer or against a pointer exclusively derived from null.
  343. /// Poisoned pointers are produced when we somehow derive pointer from relocated
  344. /// and unrelocated pointers (e.g. phi, select). This pointers may be safely
  345. /// used in a very limited number of situations. Currently the only way to use
  346. /// it is comparison against constant exclusively derived from null. All
  347. /// limitations arise due to their undefined state: this pointers should be
  348. /// treated as relocated and unrelocated simultaneously.
  349. /// Rules of deriving:
  350. /// R + U = P - that's where the poisoned pointers come from
  351. /// P + X = P
  352. /// U + U = U
  353. /// R + R = R
  354. /// X + C = X
  355. /// Where "+" - any operation that somehow derive pointer, U - unrelocated,
  356. /// R - relocated and P - poisoned, C - constant, X - U or R or P or C or
  357. /// nothing (in case when "+" is unary operation).
  358. /// Deriving of pointers by itself is always safe.
  359. /// NOTE: when we are making decision on the status of instruction's result:
  360. /// a) for phi we need to check status of each input *at the end of
  361. /// corresponding predecessor BB*.
  362. /// b) for other instructions we need to check status of each input *at the
  363. /// current point*.
  364. ///
  365. /// FIXME: This works fairly well except one case
  366. /// bb1:
  367. /// p = *some GC-ptr def*
  368. /// p1 = gep p, offset
  369. /// / |
  370. /// / |
  371. /// bb2: |
  372. /// safepoint |
  373. /// \ |
  374. /// \ |
  375. /// bb3:
  376. /// p2 = phi [p, bb2] [p1, bb1]
  377. /// p3 = phi [p, bb2] [p, bb1]
  378. /// here p and p1 is unrelocated
  379. /// p2 and p3 is poisoned (though they shouldn't be)
  380. ///
  381. /// This leads to some weird results:
  382. /// cmp eq p, p2 - illegal instruction (false-positive)
  383. /// cmp eq p1, p2 - illegal instruction (false-positive)
  384. /// cmp eq p, p3 - illegal instruction (false-positive)
  385. /// cmp eq p, p1 - ok
  386. /// To fix this we need to introduce conception of generations and be able to
  387. /// check if two values belong to one generation or not. This way p2 will be
  388. /// considered to be unrelocated and no false alarm will happen.
  389. class GCPtrTracker {
  390. const Function &F;
  391. const CFGDeadness &CD;
  392. SpecificBumpPtrAllocator<BasicBlockState> BSAllocator;
  393. DenseMap<const BasicBlock *, BasicBlockState *> BlockMap;
  394. // This set contains defs of unrelocated pointers that are proved to be legal
  395. // and don't need verification.
  396. DenseSet<const Instruction *> ValidUnrelocatedDefs;
  397. // This set contains poisoned defs. They can be safely ignored during
  398. // verification too.
  399. DenseSet<const Value *> PoisonedDefs;
  400. public:
  401. GCPtrTracker(const Function &F, const DominatorTree &DT,
  402. const CFGDeadness &CD);
  403. bool hasLiveIncomingEdge(const PHINode *PN, const BasicBlock *InBB) const {
  404. return CD.hasLiveIncomingEdge(PN, InBB);
  405. }
  406. BasicBlockState *getBasicBlockState(const BasicBlock *BB);
  407. const BasicBlockState *getBasicBlockState(const BasicBlock *BB) const;
  408. bool isValuePoisoned(const Value *V) const { return PoisonedDefs.count(V); }
  409. /// Traverse each BB of the function and call
  410. /// InstructionVerifier::verifyInstruction for each possibly invalid
  411. /// instruction.
  412. /// It destructively modifies GCPtrTracker so it's passed via rvalue reference
  413. /// in order to prohibit further usages of GCPtrTracker as it'll be in
  414. /// inconsistent state.
  415. static void verifyFunction(GCPtrTracker &&Tracker,
  416. InstructionVerifier &Verifier);
  417. /// Returns true for reachable and live blocks.
  418. bool isMapped(const BasicBlock *BB) const {
  419. return BlockMap.find(BB) != BlockMap.end();
  420. }
  421. private:
  422. /// Returns true if the instruction may be safely skipped during verification.
  423. bool instructionMayBeSkipped(const Instruction *I) const;
  424. /// Iterates over all BBs from BlockMap and recalculates AvailableIn/Out for
  425. /// each of them until it converges.
  426. void recalculateBBsStates();
  427. /// Remove from Contribution all defs that legally produce unrelocated
  428. /// pointers and saves them to ValidUnrelocatedDefs.
  429. /// Though Contribution should belong to BBS it is passed separately with
  430. /// different const-modifier in order to emphasize (and guarantee) that only
  431. /// Contribution will be changed.
  432. /// Returns true if Contribution was changed otherwise false.
  433. bool removeValidUnrelocatedDefs(const BasicBlock *BB,
  434. const BasicBlockState *BBS,
  435. AvailableValueSet &Contribution);
  436. /// Gather all the definitions dominating the start of BB into Result. This is
  437. /// simply the defs introduced by every dominating basic block and the
  438. /// function arguments.
  439. void gatherDominatingDefs(const BasicBlock *BB, AvailableValueSet &Result,
  440. const DominatorTree &DT);
  441. /// Compute the AvailableOut set for BB, based on the BasicBlockState BBS,
  442. /// which is the BasicBlockState for BB.
  443. /// ContributionChanged is set when the verifier runs for the first time
  444. /// (in this case Contribution was changed from 'empty' to its initial state)
  445. /// or when Contribution of this BB was changed since last computation.
  446. static void transferBlock(const BasicBlock *BB, BasicBlockState &BBS,
  447. bool ContributionChanged);
  448. /// Model the effect of an instruction on the set of available values.
  449. static void transferInstruction(const Instruction &I, bool &Cleared,
  450. AvailableValueSet &Available);
  451. };
  452. /// It is a visitor for GCPtrTracker::verifyFunction. It decides if the
  453. /// instruction (which uses heap reference) is legal or not, given our safepoint
  454. /// semantics.
  455. class InstructionVerifier {
  456. bool AnyInvalidUses = false;
  457. public:
  458. void verifyInstruction(const GCPtrTracker *Tracker, const Instruction &I,
  459. const AvailableValueSet &AvailableSet);
  460. bool hasAnyInvalidUses() const { return AnyInvalidUses; }
  461. private:
  462. void reportInvalidUse(const Value &V, const Instruction &I);
  463. };
  464. } // end anonymous namespace
  465. GCPtrTracker::GCPtrTracker(const Function &F, const DominatorTree &DT,
  466. const CFGDeadness &CD) : F(F), CD(CD) {
  467. // Calculate Contribution of each live BB.
  468. // Allocate BB states for live blocks.
  469. for (const BasicBlock &BB : F)
  470. if (!CD.isDeadBlock(&BB)) {
  471. BasicBlockState *BBS = new (BSAllocator.Allocate()) BasicBlockState;
  472. for (const auto &I : BB)
  473. transferInstruction(I, BBS->Cleared, BBS->Contribution);
  474. BlockMap[&BB] = BBS;
  475. }
  476. // Initialize AvailableIn/Out sets of each BB using only information about
  477. // dominating BBs.
  478. for (auto &BBI : BlockMap) {
  479. gatherDominatingDefs(BBI.first, BBI.second->AvailableIn, DT);
  480. transferBlock(BBI.first, *BBI.second, true);
  481. }
  482. // Simulate the flow of defs through the CFG and recalculate AvailableIn/Out
  483. // sets of each BB until it converges. If any def is proved to be an
  484. // unrelocated pointer, it will be removed from all BBSs.
  485. recalculateBBsStates();
  486. }
  487. BasicBlockState *GCPtrTracker::getBasicBlockState(const BasicBlock *BB) {
  488. return BlockMap.lookup(BB);
  489. }
  490. const BasicBlockState *GCPtrTracker::getBasicBlockState(
  491. const BasicBlock *BB) const {
  492. return const_cast<GCPtrTracker *>(this)->getBasicBlockState(BB);
  493. }
  494. bool GCPtrTracker::instructionMayBeSkipped(const Instruction *I) const {
  495. // Poisoned defs are skipped since they are always safe by itself by
  496. // definition (for details see comment to this class).
  497. return ValidUnrelocatedDefs.count(I) || PoisonedDefs.count(I);
  498. }
  499. void GCPtrTracker::verifyFunction(GCPtrTracker &&Tracker,
  500. InstructionVerifier &Verifier) {
  501. // We need RPO here to a) report always the first error b) report errors in
  502. // same order from run to run.
  503. ReversePostOrderTraversal<const Function *> RPOT(&Tracker.F);
  504. for (const BasicBlock *BB : RPOT) {
  505. BasicBlockState *BBS = Tracker.getBasicBlockState(BB);
  506. if (!BBS)
  507. continue;
  508. // We destructively modify AvailableIn as we traverse the block instruction
  509. // by instruction.
  510. AvailableValueSet &AvailableSet = BBS->AvailableIn;
  511. for (const Instruction &I : *BB) {
  512. if (Tracker.instructionMayBeSkipped(&I))
  513. continue; // This instruction shouldn't be added to AvailableSet.
  514. Verifier.verifyInstruction(&Tracker, I, AvailableSet);
  515. // Model the effect of current instruction on AvailableSet to keep the set
  516. // relevant at each point of BB.
  517. bool Cleared = false;
  518. transferInstruction(I, Cleared, AvailableSet);
  519. (void)Cleared;
  520. }
  521. }
  522. }
  523. void GCPtrTracker::recalculateBBsStates() {
  524. SetVector<const BasicBlock *> Worklist;
  525. // TODO: This order is suboptimal, it's better to replace it with priority
  526. // queue where priority is RPO number of BB.
  527. for (auto &BBI : BlockMap)
  528. Worklist.insert(BBI.first);
  529. // This loop iterates the AvailableIn/Out sets until it converges.
  530. // The AvailableIn and AvailableOut sets decrease as we iterate.
  531. while (!Worklist.empty()) {
  532. const BasicBlock *BB = Worklist.pop_back_val();
  533. BasicBlockState *BBS = getBasicBlockState(BB);
  534. if (!BBS)
  535. continue; // Ignore dead successors.
  536. size_t OldInCount = BBS->AvailableIn.size();
  537. for (const_pred_iterator PredIt(BB), End(BB, true); PredIt != End; ++PredIt) {
  538. const BasicBlock *PBB = *PredIt;
  539. BasicBlockState *PBBS = getBasicBlockState(PBB);
  540. if (PBBS && !CD.isDeadEdge(&CFGDeadness::getEdge(PredIt)))
  541. set_intersect(BBS->AvailableIn, PBBS->AvailableOut);
  542. }
  543. assert(OldInCount >= BBS->AvailableIn.size() && "invariant!");
  544. bool InputsChanged = OldInCount != BBS->AvailableIn.size();
  545. bool ContributionChanged =
  546. removeValidUnrelocatedDefs(BB, BBS, BBS->Contribution);
  547. if (!InputsChanged && !ContributionChanged)
  548. continue;
  549. size_t OldOutCount = BBS->AvailableOut.size();
  550. transferBlock(BB, *BBS, ContributionChanged);
  551. if (OldOutCount != BBS->AvailableOut.size()) {
  552. assert(OldOutCount > BBS->AvailableOut.size() && "invariant!");
  553. Worklist.insert(succ_begin(BB), succ_end(BB));
  554. }
  555. }
  556. }
  557. bool GCPtrTracker::removeValidUnrelocatedDefs(const BasicBlock *BB,
  558. const BasicBlockState *BBS,
  559. AvailableValueSet &Contribution) {
  560. assert(&BBS->Contribution == &Contribution &&
  561. "Passed Contribution should be from the passed BasicBlockState!");
  562. AvailableValueSet AvailableSet = BBS->AvailableIn;
  563. bool ContributionChanged = false;
  564. // For explanation why instructions are processed this way see
  565. // "Rules of deriving" in the comment to this class.
  566. for (const Instruction &I : *BB) {
  567. bool ValidUnrelocatedPointerDef = false;
  568. bool PoisonedPointerDef = false;
  569. // TODO: `select` instructions should be handled here too.
  570. if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
  571. if (containsGCPtrType(PN->getType())) {
  572. // If both is true, output is poisoned.
  573. bool HasRelocatedInputs = false;
  574. bool HasUnrelocatedInputs = false;
  575. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
  576. const BasicBlock *InBB = PN->getIncomingBlock(i);
  577. if (!isMapped(InBB) ||
  578. !CD.hasLiveIncomingEdge(PN, InBB))
  579. continue; // Skip dead block or dead edge.
  580. const Value *InValue = PN->getIncomingValue(i);
  581. if (isNotExclusivelyConstantDerived(InValue)) {
  582. if (isValuePoisoned(InValue)) {
  583. // If any of inputs is poisoned, output is always poisoned too.
  584. HasRelocatedInputs = true;
  585. HasUnrelocatedInputs = true;
  586. break;
  587. }
  588. if (BlockMap[InBB]->AvailableOut.count(InValue))
  589. HasRelocatedInputs = true;
  590. else
  591. HasUnrelocatedInputs = true;
  592. }
  593. }
  594. if (HasUnrelocatedInputs) {
  595. if (HasRelocatedInputs)
  596. PoisonedPointerDef = true;
  597. else
  598. ValidUnrelocatedPointerDef = true;
  599. }
  600. }
  601. } else if ((isa<GetElementPtrInst>(I) || isa<BitCastInst>(I)) &&
  602. containsGCPtrType(I.getType())) {
  603. // GEP/bitcast of unrelocated pointer is legal by itself but this def
  604. // shouldn't appear in any AvailableSet.
  605. for (const Value *V : I.operands())
  606. if (containsGCPtrType(V->getType()) &&
  607. isNotExclusivelyConstantDerived(V) && !AvailableSet.count(V)) {
  608. if (isValuePoisoned(V))
  609. PoisonedPointerDef = true;
  610. else
  611. ValidUnrelocatedPointerDef = true;
  612. break;
  613. }
  614. }
  615. assert(!(ValidUnrelocatedPointerDef && PoisonedPointerDef) &&
  616. "Value cannot be both unrelocated and poisoned!");
  617. if (ValidUnrelocatedPointerDef) {
  618. // Remove def of unrelocated pointer from Contribution of this BB and
  619. // trigger update of all its successors.
  620. Contribution.erase(&I);
  621. PoisonedDefs.erase(&I);
  622. ValidUnrelocatedDefs.insert(&I);
  623. LLVM_DEBUG(dbgs() << "Removing urelocated " << I
  624. << " from Contribution of " << BB->getName() << "\n");
  625. ContributionChanged = true;
  626. } else if (PoisonedPointerDef) {
  627. // Mark pointer as poisoned, remove its def from Contribution and trigger
  628. // update of all successors.
  629. Contribution.erase(&I);
  630. PoisonedDefs.insert(&I);
  631. LLVM_DEBUG(dbgs() << "Removing poisoned " << I << " from Contribution of "
  632. << BB->getName() << "\n");
  633. ContributionChanged = true;
  634. } else {
  635. bool Cleared = false;
  636. transferInstruction(I, Cleared, AvailableSet);
  637. (void)Cleared;
  638. }
  639. }
  640. return ContributionChanged;
  641. }
  642. void GCPtrTracker::gatherDominatingDefs(const BasicBlock *BB,
  643. AvailableValueSet &Result,
  644. const DominatorTree &DT) {
  645. DomTreeNode *DTN = DT[const_cast<BasicBlock *>(BB)];
  646. assert(DTN && "Unreachable blocks are ignored");
  647. while (DTN->getIDom()) {
  648. DTN = DTN->getIDom();
  649. auto BBS = getBasicBlockState(DTN->getBlock());
  650. assert(BBS && "immediate dominator cannot be dead for a live block");
  651. const auto &Defs = BBS->Contribution;
  652. Result.insert(Defs.begin(), Defs.end());
  653. // If this block is 'Cleared', then nothing LiveIn to this block can be
  654. // available after this block completes. Note: This turns out to be
  655. // really important for reducing memory consuption of the initial available
  656. // sets and thus peak memory usage by this verifier.
  657. if (BBS->Cleared)
  658. return;
  659. }
  660. for (const Argument &A : BB->getParent()->args())
  661. if (containsGCPtrType(A.getType()))
  662. Result.insert(&A);
  663. }
  664. void GCPtrTracker::transferBlock(const BasicBlock *BB, BasicBlockState &BBS,
  665. bool ContributionChanged) {
  666. const AvailableValueSet &AvailableIn = BBS.AvailableIn;
  667. AvailableValueSet &AvailableOut = BBS.AvailableOut;
  668. if (BBS.Cleared) {
  669. // AvailableOut will change only when Contribution changed.
  670. if (ContributionChanged)
  671. AvailableOut = BBS.Contribution;
  672. } else {
  673. // Otherwise, we need to reduce the AvailableOut set by things which are no
  674. // longer in our AvailableIn
  675. AvailableValueSet Temp = BBS.Contribution;
  676. set_union(Temp, AvailableIn);
  677. AvailableOut = std::move(Temp);
  678. }
  679. LLVM_DEBUG(dbgs() << "Transfered block " << BB->getName() << " from ";
  680. PrintValueSet(dbgs(), AvailableIn.begin(), AvailableIn.end());
  681. dbgs() << " to ";
  682. PrintValueSet(dbgs(), AvailableOut.begin(), AvailableOut.end());
  683. dbgs() << "\n";);
  684. }
  685. void GCPtrTracker::transferInstruction(const Instruction &I, bool &Cleared,
  686. AvailableValueSet &Available) {
  687. if (isa<GCStatepointInst>(I)) {
  688. Cleared = true;
  689. Available.clear();
  690. } else if (containsGCPtrType(I.getType()))
  691. Available.insert(&I);
  692. }
  693. void InstructionVerifier::verifyInstruction(
  694. const GCPtrTracker *Tracker, const Instruction &I,
  695. const AvailableValueSet &AvailableSet) {
  696. if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
  697. if (containsGCPtrType(PN->getType()))
  698. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
  699. const BasicBlock *InBB = PN->getIncomingBlock(i);
  700. const BasicBlockState *InBBS = Tracker->getBasicBlockState(InBB);
  701. if (!InBBS ||
  702. !Tracker->hasLiveIncomingEdge(PN, InBB))
  703. continue; // Skip dead block or dead edge.
  704. const Value *InValue = PN->getIncomingValue(i);
  705. if (isNotExclusivelyConstantDerived(InValue) &&
  706. !InBBS->AvailableOut.count(InValue))
  707. reportInvalidUse(*InValue, *PN);
  708. }
  709. } else if (isa<CmpInst>(I) &&
  710. containsGCPtrType(I.getOperand(0)->getType())) {
  711. Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
  712. enum BaseType baseTyLHS = getBaseType(LHS),
  713. baseTyRHS = getBaseType(RHS);
  714. // Returns true if LHS and RHS are unrelocated pointers and they are
  715. // valid unrelocated uses.
  716. auto hasValidUnrelocatedUse = [&AvailableSet, Tracker, baseTyLHS, baseTyRHS,
  717. &LHS, &RHS] () {
  718. // A cmp instruction has valid unrelocated pointer operands only if
  719. // both operands are unrelocated pointers.
  720. // In the comparison between two pointers, if one is an unrelocated
  721. // use, the other *should be* an unrelocated use, for this
  722. // instruction to contain valid unrelocated uses. This unrelocated
  723. // use can be a null constant as well, or another unrelocated
  724. // pointer.
  725. if (AvailableSet.count(LHS) || AvailableSet.count(RHS))
  726. return false;
  727. // Constant pointers (that are not exclusively null) may have
  728. // meaning in different VMs, so we cannot reorder the compare
  729. // against constant pointers before the safepoint. In other words,
  730. // comparison of an unrelocated use against a non-null constant
  731. // maybe invalid.
  732. if ((baseTyLHS == BaseType::ExclusivelySomeConstant &&
  733. baseTyRHS == BaseType::NonConstant) ||
  734. (baseTyLHS == BaseType::NonConstant &&
  735. baseTyRHS == BaseType::ExclusivelySomeConstant))
  736. return false;
  737. // If one of pointers is poisoned and other is not exclusively derived
  738. // from null it is an invalid expression: it produces poisoned result
  739. // and unless we want to track all defs (not only gc pointers) the only
  740. // option is to prohibit such instructions.
  741. if ((Tracker->isValuePoisoned(LHS) && baseTyRHS != ExclusivelyNull) ||
  742. (Tracker->isValuePoisoned(RHS) && baseTyLHS != ExclusivelyNull))
  743. return false;
  744. // All other cases are valid cases enumerated below:
  745. // 1. Comparison between an exclusively derived null pointer and a
  746. // constant base pointer.
  747. // 2. Comparison between an exclusively derived null pointer and a
  748. // non-constant unrelocated base pointer.
  749. // 3. Comparison between 2 unrelocated pointers.
  750. // 4. Comparison between a pointer exclusively derived from null and a
  751. // non-constant poisoned pointer.
  752. return true;
  753. };
  754. if (!hasValidUnrelocatedUse()) {
  755. // Print out all non-constant derived pointers that are unrelocated
  756. // uses, which are invalid.
  757. if (baseTyLHS == BaseType::NonConstant && !AvailableSet.count(LHS))
  758. reportInvalidUse(*LHS, I);
  759. if (baseTyRHS == BaseType::NonConstant && !AvailableSet.count(RHS))
  760. reportInvalidUse(*RHS, I);
  761. }
  762. } else {
  763. for (const Value *V : I.operands())
  764. if (containsGCPtrType(V->getType()) &&
  765. isNotExclusivelyConstantDerived(V) && !AvailableSet.count(V))
  766. reportInvalidUse(*V, I);
  767. }
  768. }
  769. void InstructionVerifier::reportInvalidUse(const Value &V,
  770. const Instruction &I) {
  771. errs() << "Illegal use of unrelocated value found!\n";
  772. errs() << "Def: " << V << "\n";
  773. errs() << "Use: " << I << "\n";
  774. if (!PrintOnly)
  775. abort();
  776. AnyInvalidUses = true;
  777. }
  778. static void Verify(const Function &F, const DominatorTree &DT,
  779. const CFGDeadness &CD) {
  780. LLVM_DEBUG(dbgs() << "Verifying gc pointers in function: " << F.getName()
  781. << "\n");
  782. if (PrintOnly)
  783. dbgs() << "Verifying gc pointers in function: " << F.getName() << "\n";
  784. GCPtrTracker Tracker(F, DT, CD);
  785. // We now have all the information we need to decide if the use of a heap
  786. // reference is legal or not, given our safepoint semantics.
  787. InstructionVerifier Verifier;
  788. GCPtrTracker::verifyFunction(std::move(Tracker), Verifier);
  789. if (PrintOnly && !Verifier.hasAnyInvalidUses()) {
  790. dbgs() << "No illegal uses found by SafepointIRVerifier in: " << F.getName()
  791. << "\n";
  792. }
  793. }