[PATCH] D51215: Fix misfolding of IRBuilder.CreateICmp(int_ty X, bitcast (float_ty Y) to int_ty)

Andrew Scheidecker via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 19 13:29:47 PDT 2018


AndrewScheidecker updated this revision to Diff 166177.
AndrewScheidecker added a comment.

I made the test a little simpler by bitcasting a GlobalVariable pointer to a float to get a FP typed ConstantExpr, instead of using a bitcast vector.


Repository:
  rL LLVM

https://reviews.llvm.org/D51215

Files:
  lib/IR/ConstantFold.cpp
  unittests/IR/IRBuilderTest.cpp


Index: unittests/IR/IRBuilderTest.cpp
===================================================================
--- unittests/IR/IRBuilderTest.cpp
+++ unittests/IR/IRBuilderTest.cpp
@@ -664,4 +664,25 @@
   EXPECT_EQ(MN2, MF2->getRawElements());
   EXPECT_TRUE(verifyModule(*M));
 }
+TEST_F(IRBuilderTest, ICmpFolding) {
+  IRBuilder<> Builder(BB);
+
+  // Create a FP ConstantExpr by bitcasting the address of a global variable.
+  auto I32Type = Type::getInt32Ty(Ctx);
+  auto F32 = ConstantExpr::getBitCast(ConstantExpr::getPtrToInt(GV, I32Type),
+                                      Type::getFloatTy(Ctx));
+  EXPECT_TRUE(isa<ConstantExpr>(F32));
+
+  // Bitcast the FP ConstantExpr back to an I32, but first add it to itself
+  // to ensure that the int->fp bitcast above isn't folded with this
+  // fp->int bitcast.
+  auto F32TimesTwo = Builder.CreateAdd(F32, F32);
+  auto I32 = Builder.CreateBitCast(F32TimesTwo, I32Type);
+
+  // The operands of the folded ICmp should be 32-bit integers.
+  auto ICmp = Builder.CreateICmpEQ(Constant::getNullValue(I32Type), I32);
+  auto ICmpCE = dyn_cast<ConstantExpr>(ICmp);
+  EXPECT_TRUE(ICmpCE->getOperand(0)->getType() == I32Type);
+  EXPECT_TRUE(ICmpCE->getOperand(1)->getType() == I32Type);
+}
 }
Index: lib/IR/ConstantFold.cpp
===================================================================
--- lib/IR/ConstantFold.cpp
+++ lib/IR/ConstantFold.cpp
@@ -1981,11 +1981,13 @@
 
     // If the right hand side is a bitcast, try using its inverse to simplify
     // it by moving it to the left hand side.  We can't do this if it would turn
-    // a vector compare into a scalar compare or visa versa.
+    // a vector compare into a scalar compare or visa versa, or if it would turn
+    // the operands into FP values.
     if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(C2)) {
       Constant *CE2Op0 = CE2->getOperand(0);
       if (CE2->getOpcode() == Instruction::BitCast &&
-          CE2->getType()->isVectorTy() == CE2Op0->getType()->isVectorTy()) {
+          CE2->getType()->isVectorTy() == CE2Op0->getType()->isVectorTy() &&
+          !CE2Op0->getType()->isFPOrFPVectorTy()) {
         Constant *Inverse = ConstantExpr::getBitCast(C1, CE2Op0->getType());
         return ConstantExpr::getICmp(pred, Inverse, CE2Op0);
       }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51215.166177.patch
Type: text/x-patch
Size: 2286 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180919/5108a0bf/attachment.bin>


More information about the llvm-commits mailing list