From: Nick Lewycky Date: Sun, 20 Sep 2009 05:48:50 +0000 (+0000) Subject: Try turning icmp(bitcast(x), bitcast(y)) into icmp(bitcast(bitcast(x)), y) in X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=55a722bf6a08c684e49f5b56301bfc66d6dc9fd4;p=oota-llvm.git Try turning icmp(bitcast(x), bitcast(y)) into icmp(bitcast(bitcast(x)), y) in the hopes that the two bitcasts will merge. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82371 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 8c65b563570..3f5c7efbb9a 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -1627,6 +1627,16 @@ Constant *llvm::ConstantFoldCompareInstruction(LLVMContext &Context, if (Result != -1) return ConstantInt::get(Type::getInt1Ty(Context), Result); + // If the right hand side is a bitcast, try using its inverse to simplify + // it by moving it to the left hand side. + if (ConstantExpr *CE2 = dyn_cast(C2)) { + if (CE2->getOpcode() == Instruction::BitCast) { + Constant *CE2Op0 = CE2->getOperand(0); + Constant *Inverse = ConstantExpr::getBitCast(C1, CE2Op0->getType()); + return ConstantExpr::getICmp(pred, Inverse, CE2Op0); + } + } + if (!isa(C1) && isa(C2)) { // If C2 is a constant expr and C1 isn't, flip them around and fold the // other way if possible. diff --git a/test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll b/test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll index debf9a8e5cd..0a40c0ce2c4 100644 --- a/test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll +++ b/test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll @@ -23,3 +23,8 @@ global i1 srem (i1 icmp ult (i8* @X, i8* @Y), i1 icmp ult (i8* @X, i8* @Z)) global i1 urem (i1 icmp ult (i8* @X, i8* @Y), i1 icmp ult (i8* @X, i8* @Z)) ; CHECK-NOT: icmp ; CHECK: i1 false + +global i1 icmp ule (i32* bitcast (i8* @X to i32*), i32* bitcast (i8* @Y to i32*)) +; CHECK-NOT: bitcast +; CHECK: icmp +