GCRootLowering.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //===-- GCRootLowering.cpp - Garbage collection infrastructure ------------===//
  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. // This file implements the lowering for the gc.root mechanism.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/GCMetadata.h"
  13. #include "llvm/CodeGen/GCStrategy.h"
  14. #include "llvm/CodeGen/MachineFrameInfo.h"
  15. #include "llvm/CodeGen/MachineFunctionPass.h"
  16. #include "llvm/CodeGen/MachineInstrBuilder.h"
  17. #include "llvm/CodeGen/MachineModuleInfo.h"
  18. #include "llvm/CodeGen/Passes.h"
  19. #include "llvm/CodeGen/TargetFrameLowering.h"
  20. #include "llvm/CodeGen/TargetInstrInfo.h"
  21. #include "llvm/CodeGen/TargetRegisterInfo.h"
  22. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  23. #include "llvm/IR/Dominators.h"
  24. #include "llvm/IR/IntrinsicInst.h"
  25. #include "llvm/IR/Module.h"
  26. #include "llvm/Support/Debug.h"
  27. #include "llvm/Support/ErrorHandling.h"
  28. #include "llvm/Support/raw_ostream.h"
  29. using namespace llvm;
  30. namespace {
  31. /// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or
  32. /// llvm.gcwrite intrinsics, replacing them with simple loads and stores as
  33. /// directed by the GCStrategy. It also performs automatic root initialization
  34. /// and custom intrinsic lowering.
  35. class LowerIntrinsics : public FunctionPass {
  36. bool DoLowering(Function &F, GCStrategy &S);
  37. public:
  38. static char ID;
  39. LowerIntrinsics();
  40. StringRef getPassName() const override;
  41. void getAnalysisUsage(AnalysisUsage &AU) const override;
  42. bool doInitialization(Module &M) override;
  43. bool runOnFunction(Function &F) override;
  44. };
  45. /// GCMachineCodeAnalysis - This is a target-independent pass over the machine
  46. /// function representation to identify safe points for the garbage collector
  47. /// in the machine code. It inserts labels at safe points and populates a
  48. /// GCMetadata record for each function.
  49. class GCMachineCodeAnalysis : public MachineFunctionPass {
  50. GCFunctionInfo *FI;
  51. MachineModuleInfo *MMI;
  52. const TargetInstrInfo *TII;
  53. void FindSafePoints(MachineFunction &MF);
  54. void VisitCallPoint(MachineBasicBlock::iterator CI);
  55. MCSymbol *InsertLabel(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
  56. const DebugLoc &DL) const;
  57. void FindStackOffsets(MachineFunction &MF);
  58. public:
  59. static char ID;
  60. GCMachineCodeAnalysis();
  61. void getAnalysisUsage(AnalysisUsage &AU) const override;
  62. bool runOnMachineFunction(MachineFunction &MF) override;
  63. };
  64. }
  65. // -----------------------------------------------------------------------------
  66. INITIALIZE_PASS_BEGIN(LowerIntrinsics, "gc-lowering", "GC Lowering", false,
  67. false)
  68. INITIALIZE_PASS_DEPENDENCY(GCModuleInfo)
  69. INITIALIZE_PASS_END(LowerIntrinsics, "gc-lowering", "GC Lowering", false, false)
  70. FunctionPass *llvm::createGCLoweringPass() { return new LowerIntrinsics(); }
  71. char LowerIntrinsics::ID = 0;
  72. LowerIntrinsics::LowerIntrinsics() : FunctionPass(ID) {
  73. initializeLowerIntrinsicsPass(*PassRegistry::getPassRegistry());
  74. }
  75. StringRef LowerIntrinsics::getPassName() const {
  76. return "Lower Garbage Collection Instructions";
  77. }
  78. void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const {
  79. FunctionPass::getAnalysisUsage(AU);
  80. AU.addRequired<GCModuleInfo>();
  81. AU.addPreserved<DominatorTreeWrapperPass>();
  82. }
  83. /// doInitialization - If this module uses the GC intrinsics, find them now.
  84. bool LowerIntrinsics::doInitialization(Module &M) {
  85. GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
  86. assert(MI && "LowerIntrinsics didn't require GCModuleInfo!?");
  87. for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
  88. if (!I->isDeclaration() && I->hasGC())
  89. MI->getFunctionInfo(*I); // Instantiate the GC strategy.
  90. return false;
  91. }
  92. /// CouldBecomeSafePoint - Predicate to conservatively determine whether the
  93. /// instruction could introduce a safe point.
  94. static bool CouldBecomeSafePoint(Instruction *I) {
  95. // The natural definition of instructions which could introduce safe points
  96. // are:
  97. //
  98. // - call, invoke (AfterCall, BeforeCall)
  99. // - phis (Loops)
  100. // - invoke, ret, unwind (Exit)
  101. //
  102. // However, instructions as seemingly inoccuous as arithmetic can become
  103. // libcalls upon lowering (e.g., div i64 on a 32-bit platform), so instead
  104. // it is necessary to take a conservative approach.
  105. if (isa<AllocaInst>(I) || isa<GetElementPtrInst>(I) || isa<StoreInst>(I) ||
  106. isa<LoadInst>(I))
  107. return false;
  108. // llvm.gcroot is safe because it doesn't do anything at runtime.
  109. if (CallInst *CI = dyn_cast<CallInst>(I))
  110. if (Function *F = CI->getCalledFunction())
  111. if (Intrinsic::ID IID = F->getIntrinsicID())
  112. if (IID == Intrinsic::gcroot)
  113. return false;
  114. return true;
  115. }
  116. static bool InsertRootInitializers(Function &F, ArrayRef<AllocaInst *> Roots) {
  117. // Scroll past alloca instructions.
  118. BasicBlock::iterator IP = F.getEntryBlock().begin();
  119. while (isa<AllocaInst>(IP))
  120. ++IP;
  121. // Search for initializers in the initial BB.
  122. SmallPtrSet<AllocaInst *, 16> InitedRoots;
  123. for (; !CouldBecomeSafePoint(&*IP); ++IP)
  124. if (StoreInst *SI = dyn_cast<StoreInst>(IP))
  125. if (AllocaInst *AI =
  126. dyn_cast<AllocaInst>(SI->getOperand(1)->stripPointerCasts()))
  127. InitedRoots.insert(AI);
  128. // Add root initializers.
  129. bool MadeChange = false;
  130. for (AllocaInst *Root : Roots)
  131. if (!InitedRoots.count(Root)) {
  132. StoreInst *SI = new StoreInst(
  133. ConstantPointerNull::get(cast<PointerType>(Root->getAllocatedType())),
  134. Root);
  135. SI->insertAfter(Root);
  136. MadeChange = true;
  137. }
  138. return MadeChange;
  139. }
  140. /// runOnFunction - Replace gcread/gcwrite intrinsics with loads and stores.
  141. /// Leave gcroot intrinsics; the code generator needs to see those.
  142. bool LowerIntrinsics::runOnFunction(Function &F) {
  143. // Quick exit for functions that do not use GC.
  144. if (!F.hasGC())
  145. return false;
  146. GCFunctionInfo &FI = getAnalysis<GCModuleInfo>().getFunctionInfo(F);
  147. GCStrategy &S = FI.getStrategy();
  148. return DoLowering(F, S);
  149. }
  150. /// Lower barriers out of existance (if the associated GCStrategy hasn't
  151. /// already done so...), and insert initializing stores to roots as a defensive
  152. /// measure. Given we're going to report all roots live at all safepoints, we
  153. /// need to be able to ensure each root has been initialized by the point the
  154. /// first safepoint is reached. This really should have been done by the
  155. /// frontend, but the old API made this non-obvious, so we do a potentially
  156. /// redundant store just in case.
  157. bool LowerIntrinsics::DoLowering(Function &F, GCStrategy &S) {
  158. SmallVector<AllocaInst *, 32> Roots;
  159. bool MadeChange = false;
  160. for (BasicBlock &BB : F)
  161. for (BasicBlock::iterator II = BB.begin(), E = BB.end(); II != E;) {
  162. IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++);
  163. if (!CI)
  164. continue;
  165. Function *F = CI->getCalledFunction();
  166. switch (F->getIntrinsicID()) {
  167. default: break;
  168. case Intrinsic::gcwrite: {
  169. // Replace a write barrier with a simple store.
  170. Value *St = new StoreInst(CI->getArgOperand(0),
  171. CI->getArgOperand(2), CI);
  172. CI->replaceAllUsesWith(St);
  173. CI->eraseFromParent();
  174. MadeChange = true;
  175. break;
  176. }
  177. case Intrinsic::gcread: {
  178. // Replace a read barrier with a simple load.
  179. Value *Ld = new LoadInst(CI->getType(), CI->getArgOperand(1), "", CI);
  180. Ld->takeName(CI);
  181. CI->replaceAllUsesWith(Ld);
  182. CI->eraseFromParent();
  183. MadeChange = true;
  184. break;
  185. }
  186. case Intrinsic::gcroot: {
  187. // Initialize the GC root, but do not delete the intrinsic. The
  188. // backend needs the intrinsic to flag the stack slot.
  189. Roots.push_back(
  190. cast<AllocaInst>(CI->getArgOperand(0)->stripPointerCasts()));
  191. break;
  192. }
  193. }
  194. }
  195. if (Roots.size())
  196. MadeChange |= InsertRootInitializers(F, Roots);
  197. return MadeChange;
  198. }
  199. // -----------------------------------------------------------------------------
  200. char GCMachineCodeAnalysis::ID = 0;
  201. char &llvm::GCMachineCodeAnalysisID = GCMachineCodeAnalysis::ID;
  202. INITIALIZE_PASS(GCMachineCodeAnalysis, "gc-analysis",
  203. "Analyze Machine Code For Garbage Collection", false, false)
  204. GCMachineCodeAnalysis::GCMachineCodeAnalysis() : MachineFunctionPass(ID) {}
  205. void GCMachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
  206. MachineFunctionPass::getAnalysisUsage(AU);
  207. AU.setPreservesAll();
  208. AU.addRequired<MachineModuleInfo>();
  209. AU.addRequired<GCModuleInfo>();
  210. }
  211. MCSymbol *GCMachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB,
  212. MachineBasicBlock::iterator MI,
  213. const DebugLoc &DL) const {
  214. MCSymbol *Label = MBB.getParent()->getContext().createTempSymbol();
  215. BuildMI(MBB, MI, DL, TII->get(TargetOpcode::GC_LABEL)).addSym(Label);
  216. return Label;
  217. }
  218. void GCMachineCodeAnalysis::VisitCallPoint(MachineBasicBlock::iterator CI) {
  219. // Find the return address (next instruction), since that's what will be on
  220. // the stack when the call is suspended and we need to inspect the stack.
  221. MachineBasicBlock::iterator RAI = CI;
  222. ++RAI;
  223. MCSymbol *Label = InsertLabel(*CI->getParent(), RAI, CI->getDebugLoc());
  224. FI->addSafePoint(Label, CI->getDebugLoc());
  225. }
  226. void GCMachineCodeAnalysis::FindSafePoints(MachineFunction &MF) {
  227. for (MachineBasicBlock &MBB : MF)
  228. for (MachineBasicBlock::iterator MI = MBB.begin(), ME = MBB.end();
  229. MI != ME; ++MI)
  230. if (MI->isCall()) {
  231. // Do not treat tail or sibling call sites as safe points. This is
  232. // legal since any arguments passed to the callee which live in the
  233. // remnants of the callers frame will be owned and updated by the
  234. // callee if required.
  235. if (MI->isTerminator())
  236. continue;
  237. VisitCallPoint(MI);
  238. }
  239. }
  240. void GCMachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) {
  241. const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
  242. assert(TFI && "TargetRegisterInfo not available!");
  243. for (GCFunctionInfo::roots_iterator RI = FI->roots_begin();
  244. RI != FI->roots_end();) {
  245. // If the root references a dead object, no need to keep it.
  246. if (MF.getFrameInfo().isDeadObjectIndex(RI->Num)) {
  247. RI = FI->removeStackRoot(RI);
  248. } else {
  249. unsigned FrameReg; // FIXME: surely GCRoot ought to store the
  250. // register that the offset is from?
  251. RI->StackOffset = TFI->getFrameIndexReference(MF, RI->Num, FrameReg);
  252. ++RI;
  253. }
  254. }
  255. }
  256. bool GCMachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) {
  257. // Quick exit for functions that do not use GC.
  258. if (!MF.getFunction().hasGC())
  259. return false;
  260. FI = &getAnalysis<GCModuleInfo>().getFunctionInfo(MF.getFunction());
  261. MMI = &getAnalysis<MachineModuleInfo>();
  262. TII = MF.getSubtarget().getInstrInfo();
  263. // Find the size of the stack frame. There may be no correct static frame
  264. // size, we use UINT64_MAX to represent this.
  265. const MachineFrameInfo &MFI = MF.getFrameInfo();
  266. const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
  267. const bool DynamicFrameSize = MFI.hasVarSizedObjects() ||
  268. RegInfo->needsStackRealignment(MF);
  269. FI->setFrameSize(DynamicFrameSize ? UINT64_MAX : MFI.getStackSize());
  270. // Find all safe points.
  271. if (FI->getStrategy().needsSafePoints())
  272. FindSafePoints(MF);
  273. // Find the concrete stack offsets for all roots (stack slots)
  274. FindStackOffsets(MF);
  275. return false;
  276. }