SafepointIRVerifier.cpp 34 KB

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