[PATCH] D33839: Prevent outlining of basicblock that uses BlockAddress

serge via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 19 00:17:11 PDT 2017


serge-sans-paille updated this revision to Diff 102992.
serge-sans-paille retitled this revision from "Correctly remap BlockAddress when using CodeExtractor" to "Prevent outlining of basicblock that uses BlockAddress".
serge-sans-paille edited the summary of this revision.

Repository:
  rL LLVM

https://reviews.llvm.org/D33839

Files:
  ../lib/Transforms/Utils/CodeExtractor.cpp
  ../test/Transforms/CodeExtractor/BlockAddressreference.ll


Index: ../test/Transforms/CodeExtractor/BlockAddressreference.ll
===================================================================
--- ../test/Transforms/CodeExtractor/BlockAddressreference.ll
+++ ../test/Transforms/CodeExtractor/BlockAddressreference.ll
@@ -0,0 +1,36 @@
+; RUN: opt < %s -loop-extract -S | FileCheck %s
+
+ at label = common local_unnamed_addr global i8* null
+
+; CHECK: define
+; no outlined function
+; CHECK-NOT: define
+define i32 @sterix(i32 %n) {
+entry:
+  %tobool = icmp ne i32 %n, 0
+  ; this blockaddress references a basic block that goes in the extracted loop
+  %cond = select i1 %tobool, i8* blockaddress(@sterix, %for.cond), i8* blockaddress(@sterix, %exit)
+  store i8* %cond, i8** @label
+  %cmp5 = icmp sgt i32 %n, 0
+  br i1 %cmp5, label %for.body, label %exit
+
+for.cond:
+  %mul = shl nsw i32 %s.06, 1
+  %exitcond = icmp eq i32 %inc, %n
+  br i1 %exitcond, label %exit.loopexit, label %for.body
+
+for.body:
+  %i.07 = phi i32 [ %inc, %for.cond ], [ 0, %entry ]
+  %s.06 = phi i32 [ %mul, %for.cond ], [ 1, %entry ]
+  %inc = add nuw nsw i32 %i.07, 1
+  br label %for.cond
+
+exit.loopexit:
+  %phitmp = icmp ne i32 %s.06, 2
+  %phitmp8 = zext i1 %phitmp to i32
+  br label %exit
+
+exit:
+  %s.1 = phi i32 [ 1, %entry ], [ %phitmp8, %exit.loopexit ]
+  ret i32 %s.1
+}
Index: ../lib/Transforms/Utils/CodeExtractor.cpp
===================================================================
--- ../lib/Transforms/Utils/CodeExtractor.cpp
+++ ../lib/Transforms/Utils/CodeExtractor.cpp
@@ -60,6 +60,33 @@
   if (BB.isEHPad())
     return false;
 
+  // taking the address of a basic block moved to another function is illegal
+  if (BB.hasAddressTaken())
+    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);
+    }
+  }
+
   // Don't hoist code containing allocas, invokes, or vastarts.
   for (BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
     if (isa<AllocaInst>(I) || isa<InvokeInst>(I))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33839.102992.patch
Type: text/x-patch
Size: 2627 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170619/6d3b5633/attachment.bin>


More information about the llvm-commits mailing list