forked from OSchip/llvm-project
				
			Make this transformation slightly less agressive and more correct.
The 'CmpInst::isFalseWhenEqual' function returns 'false' for values other than simply equality. For instance, it returns 'false' for <= or >=. This isn't the correct behavior for this transformation, which is checking for strict equality and non-equality. It was causing the gcc.c-torture/execute/frame-address.c test to fail because it would completely (and incorrectly) optimize a whole function into a 'ret i32 0'. llvm-svn: 152497
This commit is contained in:
		
							parent
							
								
									b52221e941
								
							
						
					
					
						commit
						0624d2a1ec
					
				| 
						 | 
					@ -1609,23 +1609,33 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
 | 
				
			||||||
    if (llvm::isKnownNonNull(LHSPtr) || llvm::isKnownNonNull(RHSPtr)) {
 | 
					    if (llvm::isKnownNonNull(LHSPtr) || llvm::isKnownNonNull(RHSPtr)) {
 | 
				
			||||||
      // If both sides are different identified objects, they aren't equal
 | 
					      // If both sides are different identified objects, they aren't equal
 | 
				
			||||||
      // unless they're null.
 | 
					      // unless they're null.
 | 
				
			||||||
      if (LHSPtr != RHSPtr && llvm::isIdentifiedObject(RHSPtr))
 | 
					      if (LHSPtr != RHSPtr && llvm::isIdentifiedObject(RHSPtr) &&
 | 
				
			||||||
        return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
 | 
					          (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::FCMP_UEQ))
 | 
				
			||||||
 | 
					        return ConstantInt::get(ITy, false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // A local identified object (alloca or noalias call) can't equal any
 | 
					      // A local identified object (alloca or noalias call) can't equal any
 | 
				
			||||||
      // incoming argument, unless they're both null.
 | 
					      // incoming argument, unless they're both null.
 | 
				
			||||||
      if (isa<Instruction>(LHSPtr) && isa<Argument>(RHSPtr))
 | 
					      if (isa<Instruction>(LHSPtr) && isa<Argument>(RHSPtr) &&
 | 
				
			||||||
        return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
 | 
					          (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::FCMP_UEQ))
 | 
				
			||||||
 | 
					        return ConstantInt::get(ITy, false);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Assume that the constant null is on the right.
 | 
					    // Assume that the constant null is on the right.
 | 
				
			||||||
    if (llvm::isKnownNonNull(LHSPtr) && isa<ConstantPointerNull>(RHSPtr))
 | 
					    if (llvm::isKnownNonNull(LHSPtr) && isa<ConstantPointerNull>(RHSPtr)) {
 | 
				
			||||||
      return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
 | 
					      if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::FCMP_UEQ)
 | 
				
			||||||
 | 
					        return ConstantInt::get(ITy, false);
 | 
				
			||||||
 | 
					      else if (Pred == CmpInst::ICMP_NE || Pred == CmpInst::FCMP_ONE)
 | 
				
			||||||
 | 
					        return ConstantInt::get(ITy, true);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  } else if (isa<Argument>(LHSPtr)) {
 | 
					  } else if (isa<Argument>(LHSPtr)) {
 | 
				
			||||||
    RHSPtr = RHSPtr->stripInBoundsOffsets();
 | 
					    RHSPtr = RHSPtr->stripInBoundsOffsets();
 | 
				
			||||||
    // An alloca can't be equal to an argument.
 | 
					    // An alloca can't be equal to an argument.
 | 
				
			||||||
    if (isa<AllocaInst>(RHSPtr))
 | 
					    if (isa<AllocaInst>(RHSPtr)) {
 | 
				
			||||||
      return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
 | 
					      if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::FCMP_UEQ)
 | 
				
			||||||
 | 
					        return ConstantInt::get(ITy, false);
 | 
				
			||||||
 | 
					      else if (Pred == CmpInst::ICMP_NE || Pred == CmpInst::FCMP_ONE)
 | 
				
			||||||
 | 
					        return ConstantInt::get(ITy, true);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // If we are comparing with zero then try hard since this is a common case.
 | 
					  // If we are comparing with zero then try hard since this is a common case.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,35 @@
 | 
				
			||||||
 | 
					; RUN: opt < %s -instcombine | FileCheck %s
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					; Derived from gcc.c-torture/execute/frame-address.c
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					; CHECK:     @func
 | 
				
			||||||
 | 
					; CHECK:     return:
 | 
				
			||||||
 | 
					; CHECK-NOT: ret i32 0
 | 
				
			||||||
 | 
					; CHECK:     ret i32 %retval
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					define i32 @func(i8* %c, i8* %f) nounwind uwtable readnone noinline ssp {
 | 
				
			||||||
 | 
					entry:
 | 
				
			||||||
 | 
					  %d = alloca i8, align 1
 | 
				
			||||||
 | 
					  store i8 0, i8* %d, align 1
 | 
				
			||||||
 | 
					  %cmp = icmp ugt i8* %d, %c
 | 
				
			||||||
 | 
					  br i1 %cmp, label %if.else, label %if.then
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if.then:                                          ; preds = %entry
 | 
				
			||||||
 | 
					  %cmp2 = icmp ule i8* %d, %f
 | 
				
			||||||
 | 
					  %not.cmp1 = icmp uge i8* %c, %f
 | 
				
			||||||
 | 
					  %.cmp2 = and i1 %cmp2, %not.cmp1
 | 
				
			||||||
 | 
					  %land.ext = zext i1 %.cmp2 to i32
 | 
				
			||||||
 | 
					  br label %return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if.else:                                          ; preds = %entry
 | 
				
			||||||
 | 
					  %cmp5 = icmp uge i8* %d, %f
 | 
				
			||||||
 | 
					  %not.cmp3 = icmp ule i8* %c, %f
 | 
				
			||||||
 | 
					  %.cmp5 = and i1 %cmp5, %not.cmp3
 | 
				
			||||||
 | 
					  %land.ext7 = zext i1 %.cmp5 to i32
 | 
				
			||||||
 | 
					  br label %return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					return:                                           ; preds = %if.else, %if.then
 | 
				
			||||||
 | 
					  %retval.0 = phi i32 [ %land.ext, %if.then ], [ %land.ext7, %if.else ]
 | 
				
			||||||
 | 
					  ret i32 %retval.0
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue