[llvm-commits] [llvm] r82340 - in /llvm/trunk: lib/VMCore/ConstantFold.cpp test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll

Nick Lewycky nicholas at mxc.ca
Sat Sep 19 17:04:02 PDT 2009


Author: nicholas
Date: Sat Sep 19 19:04:02 2009
New Revision: 82340

URL: http://llvm.org/viewvc/llvm-project?rev=82340&view=rev
Log:
Teach the constant folder how to handle a few simple i1 cases.

Added:
    llvm/trunk/test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll
Modified:
    llvm/trunk/lib/VMCore/ConstantFold.cpp

Modified: llvm/trunk/lib/VMCore/ConstantFold.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/ConstantFold.cpp?rev=82340&r1=82339&r2=82340&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/ConstantFold.cpp (original)
+++ llvm/trunk/lib/VMCore/ConstantFold.cpp Sat Sep 19 19:04:02 2009
@@ -1010,6 +1010,37 @@
     }
   }
 
+  // i1 can be simplified in many cases.
+  if (C1->getType() == Type::getInt1Ty(Context)) {
+    switch (Opcode) {
+    case Instruction::Add:
+    case Instruction::Sub:
+      return ConstantExpr::getXor(const_cast<Constant*>(C1),
+                                  const_cast<Constant*>(C2));
+    case Instruction::Mul:
+      return ConstantExpr::getAnd(const_cast<Constant*>(C1),
+                                  const_cast<Constant*>(C2));
+    case Instruction::Shl:
+    case Instruction::LShr:
+    case Instruction::AShr:
+      // We can assume that C2 == 0.  If it were one the result would be
+      // undefined because the shift value is as large as the bitwidth.
+      return const_cast<Constant*>(C1);
+    case Instruction::SDiv:
+    case Instruction::UDiv:
+      // We can assume that C2 == 1.  If it were zero the result would be
+      // undefined through division by zero.
+      return const_cast<Constant*>(C1);
+    case Instruction::URem:
+    case Instruction::SRem:
+      // We can assume that C2 == 1.  If it were zero the result would be
+      // undefined through division by zero.
+      return ConstantInt::getFalse(Context);
+    default:
+      break;
+    }
+  }
+
   // We don't know how to fold this.
   return 0;
 }

Added: llvm/trunk/test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll?rev=82340&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll (added)
+++ llvm/trunk/test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll Sat Sep 19 19:04:02 2009
@@ -0,0 +1,25 @@
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s
+
+ at X = external global i8
+ at Y = external global i8
+ at Z = external global i8
+
+global i1 add (i1 icmp ult (i8* @X, i8* @Y), i1 icmp ult (i8* @X, i8* @Z))
+; CHECK: xor
+global i1 sub (i1 icmp ult (i8* @X, i8* @Y), i1 icmp ult (i8* @X, i8* @Z))
+; CHECK: xor
+global i1 mul (i1 icmp ult (i8* @X, i8* @Y), i1 icmp ult (i8* @X, i8* @Z))
+; CHECK: and
+
+global i1 sdiv (i1 icmp ult (i8* @X, i8* @Y), i1 icmp ult (i8* @X, i8* @Z))
+; CHECK-NOT: @Z
+; CHECK: i1 icmp ult (i8* @X, i8* @Y)
+global i1 udiv (i1 icmp ult (i8* @X, i8* @Y), i1 icmp ult (i8* @X, i8* @Z))
+; CHECK-NOT: @Z
+; CHECK: i1 icmp ult (i8* @X, i8* @Y)
+global i1 srem (i1 icmp ult (i8* @X, i8* @Y), i1 icmp ult (i8* @X, i8* @Z))
+; CHECK-NOT: icmp
+; CHECK: i1 false
+global i1 urem (i1 icmp ult (i8* @X, i8* @Y), i1 icmp ult (i8* @X, i8* @Z))
+; CHECK-NOT: icmp
+; CHECK: i1 false





More information about the llvm-commits mailing list