[PATCH] D122457: [LSR] Canonicalize formula before inserting it.

Serguei Katkov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 24 21:41:12 PDT 2022


skatkov created this revision.
skatkov added reviewers: qcolombet, danilaml, mkazantsev, efriedma, reames.
Herald added a subscriber: hiraditya.
Herald added a project: All.
skatkov requested review of this revision.
Herald added a project: LLVM.

GenerateICmpZeroScales does a modification of a canonical formula
before insertion which may result that adding new formula is not
in canonical form.

In the test attached the base formula looks as
reg((-1 * {0,+,8}<nuw><nsw><%bb2>)<nsw>) + 1*reg((8 * (%arg /u 8))<nuw>)
while formula we want to insert
reg({0,+,8}<nuw><nsw><%bb2>) + 1*reg((-8 * (%arg /u 8)))
which is not canonical.


https://reviews.llvm.org/D122457

Files:
  llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
  llvm/test/Transforms/LoopStrengthReduce/canonical-form.ll


Index: llvm/test/Transforms/LoopStrengthReduce/canonical-form.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/LoopStrengthReduce/canonical-form.ll
@@ -0,0 +1,83 @@
+; RUN: opt -loop-reduce < %s
+
+; Check that no crash here.
+; When GenerateICmpZeroScales transforms the base formula
+; it can get non-canonical form.
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @hoge(i32 %arg) {
+bb:
+  %tmp = and i32 %arg, -8
+  br label %bb2
+
+bb1:                                              ; preds = %bb2
+  ret void
+
+bb2:                                              ; preds = %bb2, %bb
+  %tmp3 = phi i64 [ 0, %bb ], [ %tmp62, %bb2 ]
+  %tmp4 = phi i32 [ 1, %bb ], [ %tmp63, %bb2 ]
+  %tmp5 = phi i32 [ 0, %bb ], [ %tmp64, %bb2 ]
+  %tmp6 = add i64 %tmp3, 1
+  %tmp7 = trunc i64 %tmp6 to i32
+  %tmp8 = sub i32 %tmp4, %tmp7
+  %tmp9 = mul i32 %tmp8, %tmp8
+  %tmp10 = sub i32 %tmp9, %tmp8
+  %tmp11 = sext i32 %tmp10 to i64
+  %tmp12 = sub i64 %tmp6, %tmp11
+  %tmp13 = add nuw nsw i32 %tmp4, 1
+  %tmp14 = add i64 %tmp12, 1
+  %tmp15 = trunc i64 %tmp14 to i32
+  %tmp16 = sub i32 %tmp13, %tmp15
+  %tmp17 = mul i32 %tmp16, %tmp16
+  %tmp18 = sub i32 %tmp17, %tmp16
+  %tmp19 = sext i32 %tmp18 to i64
+  %tmp20 = sub i64 %tmp14, %tmp19
+  %tmp21 = add i64 %tmp20, 1
+  %tmp22 = sub i64 %tmp21, 0
+  %tmp23 = add nuw nsw i32 %tmp4, 3
+  %tmp24 = add i64 %tmp22, 1
+  %tmp25 = trunc i64 %tmp24 to i32
+  %tmp26 = sub i32 %tmp23, %tmp25
+  %tmp27 = mul i32 %tmp26, %tmp26
+  %tmp28 = sub i32 %tmp27, %tmp26
+  %tmp29 = sext i32 %tmp28 to i64
+  %tmp30 = sub i64 %tmp24, %tmp29
+  %tmp31 = add nuw nsw i32 %tmp4, 4
+  %tmp32 = add i64 %tmp30, 1
+  %tmp33 = trunc i64 %tmp32 to i32
+  %tmp34 = sub i32 %tmp31, %tmp33
+  %tmp35 = mul i32 %tmp34, %tmp34
+  %tmp36 = sub i32 %tmp35, %tmp34
+  %tmp37 = sext i32 %tmp36 to i64
+  %tmp38 = sub i64 %tmp32, %tmp37
+  %tmp39 = add nuw nsw i32 %tmp4, 5
+  %tmp40 = add i64 %tmp38, 1
+  %tmp41 = trunc i64 %tmp40 to i32
+  %tmp42 = sub i32 %tmp39, %tmp41
+  %tmp43 = mul i32 %tmp42, %tmp42
+  %tmp44 = sub i32 %tmp43, %tmp42
+  %tmp45 = sext i32 %tmp44 to i64
+  %tmp46 = sub i64 %tmp40, %tmp45
+  %tmp47 = add nuw nsw i32 %tmp4, 6
+  %tmp48 = add i64 %tmp46, 1
+  %tmp49 = trunc i64 %tmp48 to i32
+  %tmp50 = sub i32 %tmp47, %tmp49
+  %tmp51 = mul i32 %tmp50, %tmp50
+  %tmp52 = sub i32 %tmp51, %tmp50
+  %tmp53 = sext i32 %tmp52 to i64
+  %tmp54 = sub i64 %tmp48, %tmp53
+  %tmp55 = add nuw nsw i32 %tmp4, 7
+  %tmp56 = add i64 %tmp54, 1
+  %tmp57 = trunc i64 %tmp56 to i32
+  %tmp58 = sub i32 %tmp55, %tmp57
+  %tmp59 = mul i32 %tmp58, %tmp58
+  %tmp60 = sub i32 %tmp59, %tmp58
+  %tmp61 = sext i32 %tmp60 to i64
+  %tmp62 = sub i64 %tmp56, %tmp61
+  %tmp63 = add nuw nsw i32 %tmp4, 8
+  %tmp64 = add i32 %tmp5, 8
+  %tmp65 = icmp eq i32 %tmp64, %tmp
+  br i1 %tmp65, label %bb1, label %bb2
+}
Index: llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
+++ llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
@@ -4029,6 +4029,7 @@
     }
 
     // If we make it here and it's legal, add it.
+    F.canonicalize(*L);
     (void)InsertFormula(LU, LUIdx, F);
   next:;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122457.418128.patch
Type: text/x-patch
Size: 3396 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220325/488f08cb/attachment.bin>


More information about the llvm-commits mailing list