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(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); } 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(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(ICmp); + EXPECT_TRUE(ICmpCE->getOperand(0)->getType() == I32Type); + EXPECT_TRUE(ICmpCE->getOperand(1)->getType() == I32Type); +} }