[PATCH] D105392: [InstCombine] Add optimization to prevent poison from being propagated.

Hyeongyu Kim via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 4 14:28:07 PDT 2021


hyeongyukim updated this revision to Diff 356399.
hyeongyukim marked an inline comment as done.
hyeongyukim added a comment.

Optional was removed and comments were added.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D105392

Files:
  llvm/lib/Transforms/InstCombine/InstructionCombining.cpp


Index: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -3529,30 +3529,39 @@
   //   Op1.fr = Freeze(Op1)
   //   ... = Inst(Op1.fr, NonPoisonOps...)
   auto* OrigOp = OrigFI.getOperand(0);
-  if (auto *OrigOpInst = dyn_cast<Instruction>(OrigOp)) {
-    if (OrigOpInst->hasOneUse() && !canCreateUndefOrPoison(dyn_cast<Operator>(OrigOp))) {
-      Optional<Use *> MaybePoisonOperand;
-      for (Use &U : OrigOpInst->operands()) {
-        if (isGuaranteedNotToBeUndefOrPoison(U.get()))
-          continue;
-        if (!MaybePoisonOperand)
-          MaybePoisonOperand = &U;
-        else if(MaybePoisonOperand)
-          return nullptr;
-      }
+  auto *OrigOpInst = dyn_cast<Instruction>(OrigOp);
 
-      if (!MaybePoisonOperand.hasValue())
-        return nullptr;
+  if (!OrigOpInst)
+    return nullptr;
 
-      auto *MaybePoisonUse = MaybePoisonOperand.getValue();
-      auto *FrozenMaybePoisonOperand = new FreezeInst(MaybePoisonUse->get(), MaybePoisonUse->getUser()->getName() + ".fr");
+  if (!OrigOpInst->hasOneUse() ||
+      canCreateUndefOrPoison(dyn_cast<Operator>(OrigOp)))
+    return nullptr;
 
-      replaceUse(*MaybePoisonUse, FrozenMaybePoisonOperand);
-      FrozenMaybePoisonOperand->insertBefore(OrigOpInst);
-      return OrigOp;
-    }
+  // If operand is guaranteed not to be poison, there is no need to add freeze
+  // to the operand. So we first find the operand that is not guranteed to be
+  // poison.
+  Use *MaybePoisonOperand;
+  for (Use &U : OrigOpInst->operands()) {
+    if (isGuaranteedNotToBeUndefOrPoison(U.get()))
+      continue;
+    if (!MaybePoisonOperand)
+      MaybePoisonOperand = &U;
+    else
+      return nullptr;
   }
-  return nullptr;
+
+  // If all operands are guaranteed to be non-poison, we can drop freeze.
+  if (!MaybePoisonOperand)
+    return OrigOp;
+
+  auto *FrozenMaybePoisonOperand =
+      new FreezeInst(MaybePoisonOperand->get(),
+                     MaybePoisonOperand->getUser()->getName() + ".fr");
+
+  replaceUse(*MaybePoisonOperand, FrozenMaybePoisonOperand);
+  FrozenMaybePoisonOperand->insertBefore(OrigOpInst);
+  return OrigOp;
 }
 
 Instruction *InstCombinerImpl::visitFreeze(FreezeInst &I) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105392.356399.patch
Type: text/x-patch
Size: 2390 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210704/10061687/attachment.bin>


More information about the llvm-commits mailing list