[PATCH] D87428: [RFC PATCH 3] BPF: avoid same condition evaluated in multiple branch instructions

Yonghong Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 23 10:07:29 PDT 2020


yonghong-song updated this revision to Diff 293785.
yonghong-song added a comment.

rebase since https://reviews.llvm.org/D87153 changed


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D87428/new/

https://reviews.llvm.org/D87428

Files:
  llvm/lib/Target/BPF/BPFCheckAndAdjustIR.cpp


Index: llvm/lib/Target/BPF/BPFCheckAndAdjustIR.cpp
===================================================================
--- llvm/lib/Target/BPF/BPFCheckAndAdjustIR.cpp
+++ llvm/lib/Target/BPF/BPFCheckAndAdjustIR.cpp
@@ -46,6 +46,7 @@
   void checkIR(Module &M);
   bool adjustIR(Module &M);
   bool removePassThroughBuiltin(Module &M);
+  bool serializeCond(Module &M);
 };
 } // End anonymous namespace
 
@@ -120,8 +121,67 @@
   return Changed;
 }
 
+bool BPFCheckAndAdjustIR::serializeCond(Module &M) {
+  // Forcing to lower an icmp condition if it is used
+  // in multiple places.
+  // For:
+  //   icmp1 = ...
+  //   ...
+  //   a = select icmp1, val1, val2
+  //   ...
+  //   ... icmp1 ...
+  // Change to:
+  //   icmp1 = ...
+  //   icmp1 = user_asm_barrier(icmp1)
+  //   ...
+  //   a = select icmp1, val1, val2
+  //   ...
+  //   ... icmp1 ...
+  SmallVector<ICmpInst *, 16> Candidates;
+  for (Function &F : M)
+    for (auto &BB : F)
+      for (auto &I : BB) {
+        auto *Cond = dyn_cast<ICmpInst>(&I);
+        if (!Cond || Cond->use_empty() || Cond->hasOneUse())
+          continue;
+        Candidates.push_back(Cond);
+      }
+
+  if (Candidates.empty())
+    return false;
+
+  bool Changed = false;
+  for (auto *Cond : Candidates) {
+    SmallVector<Instruction *, 16> Uses;
+    for (User *U : Cond->users()) {
+      Instruction *Inst = dyn_cast<Instruction>(U);
+      if (!Inst || (!isa<SelectInst>(Inst) && !isa<BranchInst>(Inst)))
+        continue;
+      Uses.push_back(Inst);
+    }
+    if (Uses.size() < 2)
+      continue;
+
+    Changed = true;
+    Instruction *NextI = Cond->getNextNonDebugInstruction();
+    assert(NextI);
+    FunctionType *AsmTy = FunctionType::get(Cond->getType(), {Cond->getType()}, false);
+    InlineAsm *Asm = InlineAsm::get(AsmTy, StringRef(""), StringRef("=r,0"), true);
+    auto *CI = CallInst::Create(Asm, {Cond}, "", NextI);
+    for (Instruction *Inst : Uses) {
+      Inst->setOperand(0, CI);
+    }
+  }
+
+  return Changed;
+}
+
 bool BPFCheckAndAdjustIR::adjustIR(Module &M) {
-  return removePassThroughBuiltin(M);
+  bool Changed = false;
+
+  Changed = removePassThroughBuiltin(M);
+  Changed = serializeCond(M) || Changed;
+  return Changed;
 }
 
 bool BPFCheckAndAdjustIR::runOnModule(Module &M) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87428.293785.patch
Type: text/x-patch
Size: 2286 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200923/76cdae23/attachment.bin>


More information about the llvm-commits mailing list