[PATCH] D33839: Prevent Outlining of basic blocks that reference a BasicBlockAddress

serge via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 2 09:38:19 PDT 2017


serge-sans-paille created this revision.

Outlining a basic block that references a BasicBlock address is likely to create cross-function jumps, which is generally not recommended. Better safe than sorry.


Repository:
  rL LLVM

https://reviews.llvm.org/D33839

Files:
  lib/Transforms/Utils/CodeExtractor.cpp


Index: lib/Transforms/Utils/CodeExtractor.cpp
===================================================================
--- lib/Transforms/Utils/CodeExtractor.cpp
+++ lib/Transforms/Utils/CodeExtractor.cpp
@@ -70,6 +70,29 @@
           return false;
   }
 
+  // don't hoist code that uses another basicblock address, as it's likely to
+  // lead to unexpected behavior, like cross-function jumps
+  SmallPtrSet<User const *, 16> Visited;
+  SmallVector<User const *, 16> ToVisit;
+
+  for (Instruction const &Inst : BB)
+    ToVisit.push_back(&Inst);
+
+  // loop
+  while (!ToVisit.empty()) {
+    User const *Curr = ToVisit.pop_back_val();
+    if (Visited.count(Curr))
+      continue;
+    Visited.insert(Curr);
+    if (isa<BlockAddress const>(Curr)) {
+      return true; // even a reference to self is likely to be not compatible
+    }
+    for (auto const &U : Curr->operands()) {
+      if (auto *UU = dyn_cast<User>(U))
+        ToVisit.push_back(UU);
+    }
+  }
+
   return true;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33839.101229.patch
Type: text/x-patch
Size: 993 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170602/e040ddeb/attachment.bin>


More information about the llvm-commits mailing list