SafepointIRVerifier.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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 (const auto *GCRelocate = dyn_cast<GCRelocateInst>(V)) {
  311. // GCRelocates do not change null-ness or constant-ness of the value.
  312. // So we can continue with derived pointer this instruction relocates.
  313. Worklist.push_back(GCRelocate->getDerivedPtr());
  314. continue;
  315. }
  316. if (const auto *FI = dyn_cast<FreezeInst>(V)) {
  317. // Freeze does not change null-ness or constant-ness of the value.
  318. Worklist.push_back(FI->getOperand(0));
  319. continue;
  320. }
  321. if (isa<Constant>(V)) {
  322. // We found at least one base pointer which is non-null, so this derived
  323. // pointer is not exclusively derived from null.
  324. if (V != Constant::getNullValue(V->getType()))
  325. isExclusivelyDerivedFromNull = false;
  326. // Continue processing the remaining values to make sure it's exclusively
  327. // constant.
  328. continue;
  329. }
  330. // At this point, we know that the base pointer is not exclusively
  331. // constant.
  332. return BaseType::NonConstant;
  333. }
  334. // Now, we know that the base pointer is exclusively constant, but we need to
  335. // differentiate between exclusive null constant and non-null constant.
  336. return isExclusivelyDerivedFromNull ? BaseType::ExclusivelyNull
  337. : BaseType::ExclusivelySomeConstant;
  338. }
  339. static bool isNotExclusivelyConstantDerived(const Value *V) {
  340. return getBaseType(V) == BaseType::NonConstant;
  341. }
  342. namespace {
  343. class InstructionVerifier;
  344. /// Builds BasicBlockState for each BB of the function.
  345. /// It can traverse function for verification and provides all required
  346. /// information.
  347. ///
  348. /// GC pointer may be in one of three states: relocated, unrelocated and
  349. /// poisoned.
  350. /// Relocated pointer may be used without any restrictions.
  351. /// Unrelocated pointer cannot be dereferenced, passed as argument to any call
  352. /// or returned. Unrelocated pointer may be safely compared against another
  353. /// unrelocated pointer or against a pointer exclusively derived from null.
  354. /// Poisoned pointers are produced when we somehow derive pointer from relocated
  355. /// and unrelocated pointers (e.g. phi, select). This pointers may be safely
  356. /// used in a very limited number of situations. Currently the only way to use
  357. /// it is comparison against constant exclusively derived from null. All
  358. /// limitations arise due to their undefined state: this pointers should be
  359. /// treated as relocated and unrelocated simultaneously.
  360. /// Rules of deriving:
  361. /// R + U = P - that's where the poisoned pointers come from
  362. /// P + X = P
  363. /// U + U = U
  364. /// R + R = R
  365. /// X + C = X
  366. /// Where "+" - any operation that somehow derive pointer, U - unrelocated,
  367. /// R - relocated and P - poisoned, C - constant, X - U or R or P or C or
  368. /// nothing (in case when "+" is unary operation).
  369. /// Deriving of pointers by itself is always safe.
  370. /// NOTE: when we are making decision on the status of instruction's result:
  371. /// a) for phi we need to check status of each input *at the end of
  372. /// corresponding predecessor BB*.
  373. /// b) for other instructions we need to check status of each input *at the
  374. /// current point*.
  375. ///
  376. /// FIXME: This works fairly well except one case
  377. /// bb1:
  378. /// p = *some GC-ptr def*
  379. /// p1 = gep p, offset
  380. /// / |
  381. /// / |
  382. /// bb2: |
  383. /// safepoint |
  384. /// \ |
  385. /// \ |
  386. /// bb3:
  387. /// p2 = phi [p, bb2] [p1, bb1]
  388. /// p3 = phi [p, bb2] [p, bb1]
  389. /// here p and p1 is unrelocated
  390. /// p2 and p3 is poisoned (though they shouldn't be)
  391. ///
  392. /// This leads to some weird results:
  393. /// cmp eq p, p2 - illegal instruction (false-positive)
  394. /// cmp eq p1, p2 - illegal instruction (false-positive)
  395. /// cmp eq p, p3 - illegal instruction (false-positive)
  396. /// cmp eq p, p1 - ok
  397. /// To fix this we need to introduce conception of generations and be able to
  398. /// check if two values belong to one generation or not. This way p2 will be
  399. /// considered to be unrelocated and no false alarm will happen.
  400. class GCPtrTracker {
  401. const Function &F;
  402. const CFGDeadness &CD;
  403. SpecificBumpPtrAllocator<BasicBlockState> BSAllocator;
  404. DenseMap<const BasicBlock *, BasicBlockState *> BlockMap;
  405. // This set contains defs of unrelocated pointers that are proved to be legal
  406. // and don't need verification.
  407. DenseSet<const Instruction *> ValidUnrelocatedDefs;
  408. // This set contains poisoned defs. They can be safely ignored during
  409. // verification too.
  410. DenseSet<const Value *> PoisonedDefs;
  411. public:
  412. GCPtrTracker(const Function &F, const DominatorTree &DT,
  413. const CFGDeadness &CD);
  414. bool hasLiveIncomingEdge(const PHINode *PN, const BasicBlock *InBB) const {
  415. return CD.hasLiveIncomingEdge(PN, InBB);
  416. }
  417. BasicBlockState *getBasicBlockState(const BasicBlock *BB);
  418. const BasicBlockState *getBasicBlockState(const BasicBlock *BB) const;
  419. bool isValuePoisoned(const Value *V) const { return PoisonedDefs.count(V); }
  420. /// Traverse each BB of the function and call
  421. /// InstructionVerifier::verifyInstruction for each possibly invalid
  422. /// instruction.
  423. /// It destructively modifies GCPtrTracker so it's passed via rvalue reference
  424. /// in order to prohibit further usages of GCPtrTracker as it'll be in
  425. /// inconsistent state.
  426. static void verifyFunction(GCPtrTracker &&Tracker,
  427. InstructionVerifier &Verifier);
  428. /// Returns true for reachable and live blocks.
  429. bool isMapped(const BasicBlock *BB) const {
  430. return BlockMap.find(BB) != BlockMap.end();
  431. }
  432. private:
  433. /// Returns true if the instruction may be safely skipped during verification.
  434. bool instructionMayBeSkipped(const Instruction *I) const;
  435. /// Iterates over all BBs from BlockMap and recalculates AvailableIn/Out for
  436. /// each of them until it converges.
  437. void recalculateBBsStates();
  438. /// Remove from Contribution all defs that legally produce unrelocated
  439. /// pointers and saves them to ValidUnrelocatedDefs.
  440. /// Though Contribution should belong to BBS it is passed separately with
  441. /// different const-modifier in order to emphasize (and guarantee) that only
  442. /// Contribution will be changed.
  443. /// Returns true if Contribution was changed otherwise false.
  444. bool removeValidUnrelocatedDefs(const BasicBlock *BB,
  445. const BasicBlockState *BBS,
  446. AvailableValueSet &Contribution);
  447. /// Gather all the definitions dominating the start of BB into Result. This is
  448. /// simply the defs introduced by every dominating basic block and the
  449. /// function arguments.
  450. void gatherDominatingDefs(const BasicBlock *BB, AvailableValueSet &Result,
  451. const DominatorTree &DT);
  452. /// Compute the AvailableOut set for BB, based on the BasicBlockState BBS,
  453. /// which is the BasicBlockState for BB.
  454. /// ContributionChanged is set when the verifier runs for the first time
  455. /// (in this case Contribution was changed from 'empty' to its initial state)
  456. /// or when Contribution of this BB was changed since last computation.
  457. static void transferBlock(const BasicBlock *BB, BasicBlockState &BBS,
  458. bool ContributionChanged);
  459. /// Model the effect of an instruction on the set of available values.
  460. static void transferInstruction(const Instruction &I, bool &Cleared,
  461. AvailableValueSet &Available);
  462. };
  463. /// It is a visitor for GCPtrTracker::verifyFunction. It decides if the
  464. /// instruction (which uses heap reference) is legal or not, given our safepoint
  465. /// semantics.
  466. class InstructionVerifier {
  467. bool AnyInvalidUses = false;
  468. public:
  469. void verifyInstruction(const GCPtrTracker *Tracker, const Instruction &I,
  470. const AvailableValueSet &AvailableSet);
  471. bool hasAnyInvalidUses() const { return AnyInvalidUses; }
  472. private:
  473. void reportInvalidUse(const Value &V, const Instruction &I);
  474. };
  475. } // end anonymous namespace
  476. GCPtrTracker::GCPtrTracker(const Function &F, const DominatorTree &DT,
  477. const CFGDeadness &CD) : F(F), CD(CD) {
  478. // Calculate Contribution of each live BB.
  479. // Allocate BB states for live blocks.
  480. for (const BasicBlock &BB : F)
  481. if (!CD.isDeadBlock(&BB)) {
  482. BasicBlockState *BBS = new (BSAllocator.Allocate()) BasicBlockState;
  483. for (const auto &I : BB)
  484. transferInstruction(I, BBS->Cleared, BBS->Contribution);
  485. BlockMap[&BB] = BBS;
  486. }
  487. // Initialize AvailableIn/Out sets of each BB using only information about
  488. // dominating BBs.
  489. for (auto &BBI : BlockMap) {
  490. gatherDominatingDefs(BBI.first, BBI.second->AvailableIn, DT);
  491. transferBlock(BBI.first, *BBI.second, true);
  492. }
  493. // Simulate the flow of defs through the CFG and recalculate AvailableIn/Out
  494. // sets of each BB until it converges. If any def is proved to be an
  495. // unrelocated pointer, it will be removed from all BBSs.
  496. recalculateBBsStates();
  497. }
  498. BasicBlockState *GCPtrTracker::getBasicBlockState(const BasicBlock *BB) {
  499. return BlockMap.lookup(BB);
  500. }
  501. const BasicBlockState *GCPtrTracker::getBasicBlockState(
  502. const BasicBlock *BB) const {
  503. return const_cast<GCPtrTracker *>(this)->getBasicBlockState(BB);
  504. }
  505. bool GCPtrTracker::instructionMayBeSkipped(const Instruction *I) const {
  506. // Poisoned defs are skipped since they are always safe by itself by
  507. // definition (for details see comment to this class).
  508. return ValidUnrelocatedDefs.count(I) || PoisonedDefs.count(I);
  509. }
  510. void GCPtrTracker::verifyFunction(GCPtrTracker &&Tracker,
  511. InstructionVerifier &Verifier) {
  512. // We need RPO here to a) report always the first error b) report errors in
  513. // same order from run to run.
  514. ReversePostOrderTraversal<const Function *> RPOT(&Tracker.F);
  515. for (const BasicBlock *BB : RPOT) {
  516. BasicBlockState *BBS = Tracker.getBasicBlockState(BB);
  517. if (!BBS)
  518. continue;
  519. // We destructively modify AvailableIn as we traverse the block instruction
  520. // by instruction.
  521. AvailableValueSet &AvailableSet = BBS->AvailableIn;
  522. for (const Instruction &I : *BB) {
  523. if (Tracker.instructionMayBeSkipped(&I))
  524. continue; // This instruction shouldn't be added to AvailableSet.
  525. Verifier.verifyInstruction(&Tracker, I, AvailableSet);
  526. // Model the effect of current instruction on AvailableSet to keep the set
  527. // relevant at each point of BB.
  528. bool Cleared = false;
  529. transferInstruction(I, Cleared, AvailableSet);
  530. (void)Cleared;
  531. }
  532. }
  533. }
  534. void GCPtrTracker::recalculateBBsStates() {
  535. SetVector<const BasicBlock *> Worklist;
  536. // TODO: This order is suboptimal, it's better to replace it with priority
  537. // queue where priority is RPO number of BB.
  538. for (auto &BBI : BlockMap)
  539. Worklist.insert(BBI.first);
  540. // This loop iterates the AvailableIn/Out sets until it converges.
  541. // The AvailableIn and AvailableOut sets decrease as we iterate.
  542. while (!Worklist.empty()) {
  543. const BasicBlock *BB = Worklist.pop_back_val();
  544. BasicBlockState *BBS = getBasicBlockState(BB);
  545. if (!BBS)
  546. continue; // Ignore dead successors.
  547. size_t OldInCount = BBS->AvailableIn.size();
  548. for (const_pred_iterator PredIt(BB), End(BB, true); PredIt != End; ++PredIt) {
  549. const BasicBlock *PBB = *PredIt;
  550. BasicBlockState *PBBS = getBasicBlockState(PBB);
  551. if (PBBS && !CD.isDeadEdge(&CFGDeadness::getEdge(PredIt)))
  552. set_intersect(BBS->AvailableIn, PBBS->AvailableOut);
  553. }
  554. assert(OldInCount >= BBS->AvailableIn.size() && "invariant!");
  555. bool InputsChanged = OldInCount != BBS->AvailableIn.size();
  556. bool ContributionChanged =
  557. removeValidUnrelocatedDefs(BB, BBS, BBS->Contribution);
  558. if (!InputsChanged && !ContributionChanged)
  559. continue;
  560. size_t OldOutCount = BBS->AvailableOut.size();
  561. transferBlock(BB, *BBS, ContributionChanged);
  562. if (OldOutCount != BBS->AvailableOut.size()) {
  563. assert(OldOutCount > BBS->AvailableOut.size() && "invariant!");
  564. Worklist.insert(succ_begin(BB), succ_end(BB));
  565. }
  566. }
  567. }
  568. bool GCPtrTracker::removeValidUnrelocatedDefs(const BasicBlock *BB,
  569. const BasicBlockState *BBS,
  570. AvailableValueSet &Contribution) {
  571. assert(&BBS->Contribution == &Contribution &&
  572. "Passed Contribution should be from the passed BasicBlockState!");
  573. AvailableValueSet AvailableSet = BBS->AvailableIn;
  574. bool ContributionChanged = false;
  575. // For explanation why instructions are processed this way see
  576. // "Rules of deriving" in the comment to this class.
  577. for (const Instruction &I : *BB) {
  578. bool ValidUnrelocatedPointerDef = false;
  579. bool PoisonedPointerDef = false;
  580. // TODO: `select` instructions should be handled here too.
  581. if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
  582. if (containsGCPtrType(PN->getType())) {
  583. // If both is true, output is poisoned.
  584. bool HasRelocatedInputs = false;
  585. bool HasUnrelocatedInputs = false;
  586. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
  587. const BasicBlock *InBB = PN->getIncomingBlock(i);
  588. if (!isMapped(InBB) ||
  589. !CD.hasLiveIncomingEdge(PN, InBB))
  590. continue; // Skip dead block or dead edge.
  591. const Value *InValue = PN->getIncomingValue(i);
  592. if (isNotExclusivelyConstantDerived(InValue)) {
  593. if (isValuePoisoned(InValue)) {
  594. // If any of inputs is poisoned, output is always poisoned too.
  595. HasRelocatedInputs = true;
  596. HasUnrelocatedInputs = true;
  597. break;
  598. }
  599. if (BlockMap[InBB]->AvailableOut.count(InValue))
  600. HasRelocatedInputs = true;
  601. else
  602. HasUnrelocatedInputs = true;
  603. }
  604. }
  605. if (HasUnrelocatedInputs) {
  606. if (HasRelocatedInputs)
  607. PoisonedPointerDef = true;
  608. else
  609. ValidUnrelocatedPointerDef = true;
  610. }
  611. }
  612. } else if ((isa<GetElementPtrInst>(I) || isa<BitCastInst>(I)) &&
  613. containsGCPtrType(I.getType())) {
  614. // GEP/bitcast of unrelocated pointer is legal by itself but this def
  615. // shouldn't appear in any AvailableSet.
  616. for (const Value *V : I.operands())
  617. if (containsGCPtrType(V->getType()) &&
  618. isNotExclusivelyConstantDerived(V) && !AvailableSet.count(V)) {
  619. if (isValuePoisoned(V))
  620. PoisonedPointerDef = true;
  621. else
  622. ValidUnrelocatedPointerDef = true;
  623. break;
  624. }
  625. }
  626. assert(!(ValidUnrelocatedPointerDef && PoisonedPointerDef) &&
  627. "Value cannot be both unrelocated and poisoned!");
  628. if (ValidUnrelocatedPointerDef) {
  629. // Remove def of unrelocated pointer from Contribution of this BB and
  630. // trigger update of all its successors.
  631. Contribution.erase(&I);
  632. PoisonedDefs.erase(&I);
  633. ValidUnrelocatedDefs.insert(&I);
  634. LLVM_DEBUG(dbgs() << "Removing urelocated " << I
  635. << " from Contribution of " << BB->getName() << "\n");
  636. ContributionChanged = true;
  637. } else if (PoisonedPointerDef) {
  638. // Mark pointer as poisoned, remove its def from Contribution and trigger
  639. // update of all successors.
  640. Contribution.erase(&I);
  641. PoisonedDefs.insert(&I);
  642. LLVM_DEBUG(dbgs() << "Removing poisoned " << I << " from Contribution of "
  643. << BB->getName() << "\n");
  644. ContributionChanged = true;
  645. } else {
  646. bool Cleared = false;
  647. transferInstruction(I, Cleared, AvailableSet);
  648. (void)Cleared;
  649. }
  650. }
  651. return ContributionChanged;
  652. }
  653. void GCPtrTracker::gatherDominatingDefs(const BasicBlock *BB,
  654. AvailableValueSet &Result,
  655. const DominatorTree &DT) {
  656. DomTreeNode *DTN = DT[const_cast<BasicBlock *>(BB)];
  657. assert(DTN && "Unreachable blocks are ignored");
  658. while (DTN->getIDom()) {
  659. DTN = DTN->getIDom();
  660. auto BBS = getBasicBlockState(DTN->getBlock());
  661. assert(BBS && "immediate dominator cannot be dead for a live block");
  662. const auto &Defs = BBS->Contribution;
  663. Result.insert(Defs.begin(), Defs.end());
  664. // If this block is 'Cleared', then nothing LiveIn to this block can be
  665. // available after this block completes. Note: This turns out to be
  666. // really important for reducing memory consuption of the initial available
  667. // sets and thus peak memory usage by this verifier.
  668. if (BBS->Cleared)
  669. return;
  670. }
  671. for (const Argument &A : BB->getParent()->args())
  672. if (containsGCPtrType(A.getType()))
  673. Result.insert(&A);
  674. }
  675. void GCPtrTracker::transferBlock(const BasicBlock *BB, BasicBlockState &BBS,
  676. bool ContributionChanged) {
  677. const AvailableValueSet &AvailableIn = BBS.AvailableIn;
  678. AvailableValueSet &AvailableOut = BBS.AvailableOut;
  679. if (BBS.Cleared) {
  680. // AvailableOut will change only when Contribution changed.
  681. if (ContributionChanged)
  682. AvailableOut = BBS.Contribution;
  683. } else {
  684. // Otherwise, we need to reduce the AvailableOut set by things which are no
  685. // longer in our AvailableIn
  686. AvailableValueSet Temp = BBS.Contribution;
  687. set_union(Temp, AvailableIn);
  688. AvailableOut = std::move(Temp);
  689. }
  690. LLVM_DEBUG(dbgs() << "Transfered block " << BB->getName() << " from ";
  691. PrintValueSet(dbgs(), AvailableIn.begin(), AvailableIn.end());
  692. dbgs() << " to ";
  693. PrintValueSet(dbgs(), AvailableOut.begin(), AvailableOut.end());
  694. dbgs() << "\n";);
  695. }
  696. void GCPtrTracker::transferInstruction(const Instruction &I, bool &Cleared,
  697. AvailableValueSet &Available) {
  698. if (isa<GCStatepointInst>(I)) {
  699. Cleared = true;
  700. Available.clear();
  701. } else if (containsGCPtrType(I.getType()))
  702. Available.insert(&I);
  703. }
  704. void InstructionVerifier::verifyInstruction(
  705. const GCPtrTracker *Tracker, const Instruction &I,
  706. const AvailableValueSet &AvailableSet) {
  707. if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
  708. if (containsGCPtrType(PN->getType()))
  709. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
  710. const BasicBlock *InBB = PN->getIncomingBlock(i);
  711. const BasicBlockState *InBBS = Tracker->getBasicBlockState(InBB);
  712. if (!InBBS ||
  713. !Tracker->hasLiveIncomingEdge(PN, InBB))
  714. continue; // Skip dead block or dead edge.
  715. const Value *InValue = PN->getIncomingValue(i);
  716. if (isNotExclusivelyConstantDerived(InValue) &&
  717. !InBBS->AvailableOut.count(InValue))
  718. reportInvalidUse(*InValue, *PN);
  719. }
  720. } else if (isa<CmpInst>(I) &&
  721. containsGCPtrType(I.getOperand(0)->getType())) {
  722. Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
  723. enum BaseType baseTyLHS = getBaseType(LHS),
  724. baseTyRHS = getBaseType(RHS);
  725. // Returns true if LHS and RHS are unrelocated pointers and they are
  726. // valid unrelocated uses.
  727. auto hasValidUnrelocatedUse = [&AvailableSet, Tracker, baseTyLHS, baseTyRHS,
  728. &LHS, &RHS] () {
  729. // A cmp instruction has valid unrelocated pointer operands only if
  730. // both operands are unrelocated pointers.
  731. // In the comparison between two pointers, if one is an unrelocated
  732. // use, the other *should be* an unrelocated use, for this
  733. // instruction to contain valid unrelocated uses. This unrelocated
  734. // use can be a null constant as well, or another unrelocated
  735. // pointer.
  736. if (AvailableSet.count(LHS) || AvailableSet.count(RHS))
  737. return false;
  738. // Constant pointers (that are not exclusively null) may have
  739. // meaning in different VMs, so we cannot reorder the compare
  740. // against constant pointers before the safepoint. In other words,
  741. // comparison of an unrelocated use against a non-null constant
  742. // maybe invalid.
  743. if ((baseTyLHS == BaseType::ExclusivelySomeConstant &&
  744. baseTyRHS == BaseType::NonConstant) ||
  745. (baseTyLHS == BaseType::NonConstant &&
  746. baseTyRHS == BaseType::ExclusivelySomeConstant))
  747. return false;
  748. // If one of pointers is poisoned and other is not exclusively derived
  749. // from null it is an invalid expression: it produces poisoned result
  750. // and unless we want to track all defs (not only gc pointers) the only
  751. // option is to prohibit such instructions.
  752. if ((Tracker->isValuePoisoned(LHS) && baseTyRHS != ExclusivelyNull) ||
  753. (Tracker->isValuePoisoned(RHS) && baseTyLHS != ExclusivelyNull))
  754. return false;
  755. // All other cases are valid cases enumerated below:
  756. // 1. Comparison between an exclusively derived null pointer and a
  757. // constant base pointer.
  758. // 2. Comparison between an exclusively derived null pointer and a
  759. // non-constant unrelocated base pointer.
  760. // 3. Comparison between 2 unrelocated pointers.
  761. // 4. Comparison between a pointer exclusively derived from null and a
  762. // non-constant poisoned pointer.
  763. return true;
  764. };
  765. if (!hasValidUnrelocatedUse()) {
  766. // Print out all non-constant derived pointers that are unrelocated
  767. // uses, which are invalid.
  768. if (baseTyLHS == BaseType::NonConstant && !AvailableSet.count(LHS))
  769. reportInvalidUse(*LHS, I);
  770. if (baseTyRHS == BaseType::NonConstant && !AvailableSet.count(RHS))
  771. reportInvalidUse(*RHS, I);
  772. }
  773. } else {
  774. for (const Value *V : I.operands())
  775. if (containsGCPtrType(V->getType()) &&
  776. isNotExclusivelyConstantDerived(V) && !AvailableSet.count(V))
  777. reportInvalidUse(*V, I);
  778. }
  779. }
  780. void InstructionVerifier::reportInvalidUse(const Value &V,
  781. const Instruction &I) {
  782. errs() << "Illegal use of unrelocated value found!\n";
  783. errs() << "Def: " << V << "\n";
  784. errs() << "Use: " << I << "\n";
  785. if (!PrintOnly)
  786. abort();
  787. AnyInvalidUses = true;
  788. }
  789. static void Verify(const Function &F, const DominatorTree &DT,
  790. const CFGDeadness &CD) {
  791. LLVM_DEBUG(dbgs() << "Verifying gc pointers in function: " << F.getName()
  792. << "\n");
  793. if (PrintOnly)
  794. dbgs() << "Verifying gc pointers in function: " << F.getName() << "\n";
  795. GCPtrTracker Tracker(F, DT, CD);
  796. // We now have all the information we need to decide if the use of a heap
  797. // reference is legal or not, given our safepoint semantics.
  798. InstructionVerifier Verifier;
  799. GCPtrTracker::verifyFunction(std::move(Tracker), Verifier);
  800. if (PrintOnly && !Verifier.hasAnyInvalidUses()) {
  801. dbgs() << "No illegal uses found by SafepointIRVerifier in: " << F.getName()
  802. << "\n";
  803. }
  804. }