[PATCH] D107262: [CodeGenPrepare] The instruction to be sunk should be inserted before its user in a block

Tiehu Zhang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 2 04:09:16 PDT 2021


TiehuZhang created this revision.
TiehuZhang added reviewers: sdesmalen, paulwalker-arm, mdchen.
Herald added a subscriber: hiraditya.
TiehuZhang requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

In current implementation,  the instruction to be sunk will be inserted into a fixed position without considering the def-use tree, which may case `Instruction does not dominate all uses` error. We need to choose a suitable location to insert, e.g. before the first user of the target block.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D107262

Files:
  llvm/lib/CodeGen/CodeGenPrepare.cpp
  llvm/test/Transforms/CodeGenPrepare/AArch64/sink-free-instructions-1.ll


Index: llvm/test/Transforms/CodeGenPrepare/AArch64/sink-free-instructions-1.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/CodeGenPrepare/AArch64/sink-free-instructions-1.ll
@@ -0,0 +1,34 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -codegenprepare -mtriple=aarch64-linux < %s -o - | FileCheck %s
+
+; ModuleID = '<stdin>'
+source_filename = "<stdin>"
+target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
+target triple = "aarch64-unknown-linux-gnu"
+
+define i64 @c(i16 %e) {
+; CHECK-LABEL: @c(
+; CHECK-NEXT:  for.cond4.preheader.lr.ph:
+; CHECK-NEXT:    [[CONV25:%.*]] = sext i16 [[E:%.*]] to i32
+; CHECK-NEXT:    br i1 undef, label [[FOR_COND4_PREHEADER_US_PREHEADER:%.*]], label [[FOR_COND4_PREHEADER_PREHEADER:%.*]]
+; CHECK:       for.cond4.preheader.us.preheader:
+; CHECK-NEXT:    [[TMP0:%.*]] = insertelement <4 x i32> poison, i32 [[CONV25]], i32 0
+; CHECK-NEXT:    [[BROADCAST_SPLAT144:%.*]] = shufflevector <4 x i32> [[TMP0]], <4 x i32> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT:    [[TMP1:%.*]] = mul <4 x i32> undef, [[BROADCAST_SPLAT144]]
+; CHECK-NEXT:    ret i64 undef
+; CHECK:       for.cond4.preheader.preheader:
+; CHECK-NEXT:    ret i64 undef
+;
+for.cond4.preheader.lr.ph:
+  %conv25 = sext i16 %e to i32
+  %broadcast.splatinsert143 = insertelement <4 x i32> poison, i32 %conv25, i32 0
+  br i1 undef, label %for.cond4.preheader.us.preheader, label %for.cond4.preheader.preheader
+
+for.cond4.preheader.us.preheader:                 ; preds = %for.cond4.preheader.lr.ph
+  %broadcast.splat144 = shufflevector <4 x i32> %broadcast.splatinsert143, <4 x i32> poison, <4 x i32> zeroinitializer
+  %0 = mul <4 x i32> undef, %broadcast.splat144
+  ret i64 undef
+
+for.cond4.preheader.preheader:                    ; preds = %for.cond4.preheader.lr.ph
+  ret i64 undef
+}
Index: llvm/lib/CodeGen/CodeGenPrepare.cpp
===================================================================
--- llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -6959,9 +6959,18 @@
 
   SetVector<Instruction *> MaybeDead;
   DenseMap<Instruction *, Instruction *> NewInstructions;
-  Instruction *InsertPoint = I;
+
+  Instruction *InsertPoint = nullptr;
   for (Use *U : ToReplace) {
     auto *UI = cast<Instruction>(U->get());
+    InsertPoint = I;
+    for (auto *U1 : UI->users()) {
+      auto *UI1 = cast<Instruction>(U1);
+      if (UI1->getParent() == TargetBB) {
+        InsertPoint = UI1;
+        break;
+      }
+    }
     Instruction *NI = UI->clone();
     NewInstructions[UI] = NI;
     MaybeDead.insert(UI);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107262.363430.patch
Type: text/x-patch
Size: 2692 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210802/593c5394/attachment.bin>


More information about the llvm-commits mailing list