canonicalize:
authorNuno Lopes <nunoplopes@sapo.pt>
Fri, 8 Jun 2012 22:30:05 +0000 (22:30 +0000)
committerNuno Lopes <nunoplopes@sapo.pt>
Fri, 8 Jun 2012 22:30:05 +0000 (22:30 +0000)
-%a + 42
into
42 - %a

previously we were emitting:
-(%a + 42)

This fixes the infinite loop in PR12338. The generated code is still not perfect, though.
Will work on that next

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@158237 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineAddSub.cpp
test/Transforms/InstCombine/pr12338.ll [new file with mode: 0644]

index c88ceede1f25fc1ba77965c685815c6a3f5f463f..00f99741259d60594cff2e617c902d4528656088 100644 (file)
@@ -170,10 +170,11 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
   // -A + B  -->  B - A
   // -A + -B  -->  -(A + B)
   if (Value *LHSV = dyn_castNegVal(LHS)) {
-    if (Value *RHSV = dyn_castNegVal(RHS)) {
-      Value *NewAdd = Builder->CreateAdd(LHSV, RHSV, "sum");
-      return BinaryOperator::CreateNeg(NewAdd);
-    }
+    if (!isa<Constant>(RHS))
+      if (Value *RHSV = dyn_castNegVal(RHS)) {
+        Value *NewAdd = Builder->CreateAdd(LHSV, RHSV, "sum");
+        return BinaryOperator::CreateNeg(NewAdd);
+      }
     
     return BinaryOperator::CreateSub(RHS, LHSV);
   }
diff --git a/test/Transforms/InstCombine/pr12338.ll b/test/Transforms/InstCombine/pr12338.ll
new file mode 100644 (file)
index 0000000..2b5c8f8
--- /dev/null
@@ -0,0 +1,24 @@
+; RUN: opt < %s -instcombine -S | FileCheck %s
+
+define void @entry() nounwind {\r
+entry:\r
+  br label %for.cond\r
+\r
+for.cond:\r
+  %local = phi <1 x i32> [ <i32 0>, %entry ], [ %phi2, %cond.end47 ]
+; CHECK: sub <1 x i32> <i32 92>, %local\r
+  %phi3 = sub <1 x i32> zeroinitializer, %local\r
+  br label %cond.end\r
+\r
+cond.false:\r
+  br label %cond.end\r
+\r
+cond.end:\r
+  %cond = phi <1 x i32> [ %phi3, %for.cond ], [ undef, %cond.false ]\r
+  br label %cond.end47\r
+\r
+cond.end47:\r
+  %sum = add <1 x i32> %cond, <i32 92>\r
+  %phi2 = sub <1 x i32> zeroinitializer, %sum\r
+  br label %for.cond\r
+}\r