Index: llvm/lib/Analysis/ValueTracking.cpp =================================================================== --- llvm/lib/Analysis/ValueTracking.cpp +++ llvm/lib/Analysis/ValueTracking.cpp @@ -514,8 +514,10 @@ case Intrinsic::lifetime_start: case Intrinsic::lifetime_end: case Intrinsic::objectsize: + case Intrinsic::annotation: case Intrinsic::ptr_annotation: case Intrinsic::var_annotation: + case Intrinsic::experimental_widenable_condition: return true; } @@ -558,12 +560,18 @@ return true; } + // Don't let an assume affect itself - this would cause the problems + // `isEphemeralValueOf` is trying to prevent, and it would also make + // the loop below go out of bounds. + if (Inv == CxtI) + return false; + // The context comes first, but they're both in the same block. Make sure // there is nothing in between that might interrupt the control flow. for (BasicBlock::const_iterator I = std::next(BasicBlock::const_iterator(CxtI)), IE(Inv); I != IE; ++I) - if (!isSafeToSpeculativelyExecute(&*I) && !isAssumeLikeIntrinsic(&*I)) + if (!isGuaranteedToTransferExecutionToSuccessor(&*I)) return false; return !isEphemeralValueOf(Inv, CxtI); @@ -4268,9 +4276,7 @@ // FIXME: This isn't aggressive enough; a call which only writes to a global // is guaranteed to return. return CS.onlyReadsMemory() || CS.onlyAccessesArgMemory() || - match(I, m_Intrinsic()) || - match(I, m_Intrinsic()) || - match(I, m_Intrinsic()); + isAssumeLikeIntrinsic(I); } // Other instructions return normally. Index: llvm/test/Transforms/InstCombine/assume_inevitable.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/InstCombine/assume_inevitable.ll @@ -0,0 +1,59 @@ +; RUN: opt < %s -instcombine -S | FileCheck %s +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +; Check that assume is propagated backwards through all +; operations that are `isGuaranteedToTransferExecutionToSuccessor` +; (it should reach the load and mark it as `align 32`). +define i32 @assume_inevitable(i32* %a, i32* %b, i8* %c) { +; CHECK-LABEL: @assume_inevitable +; CHECK-DAG: load i32, i32* %a, align 32 +; CHECK-DAG: call void @llvm.assume +; CHECK: ret i32 +entry: + %dummy = alloca i8, align 4 + %m = alloca i64 + %0 = load i32, i32* %a, align 4 + + ; START perform a bunch of inevitable operations + %loadres = load i32, i32* %b + %loadres2 = call i32 @llvm.annotation.i32(i32 %loadres, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str1, i32 0, i32 0), i32 2) + store i32 %loadres2, i32* %a + + %dummy_eq = icmp ugt i32 %loadres, 42 + tail call void @llvm.assume(i1 %dummy_eq) + + call void @llvm.lifetime.start.p0i8(i64 1, i8* %dummy) + %i = call {}* @llvm.invariant.start.p0i8(i64 1, i8* %dummy) + call void @llvm.invariant.end.p0i8({}* %i, i64 1, i8* %dummy) + call void @llvm.lifetime.end.p0i8(i64 1, i8* %dummy) + + %m_i8 = bitcast i64* %m to i8* + %m_a = call i8* @llvm.ptr.annotation.p0i8(i8* %m_i8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str1, i32 0, i32 0), i32 2) + %m_x = bitcast i8* %m_a to i64* + %objsz = call i64 @llvm.objectsize.i64.p0i8(i8* %c, i1 false) + store i64 %objsz, i64* %m_x + ; END perform a bunch of inevitable operations + + ; AND here's the assume: + %ptrint = ptrtoint i32* %a to i64 + %maskedptr = and i64 %ptrint, 31 + %maskcond = icmp eq i64 %maskedptr, 0 + tail call void @llvm.assume(i1 %maskcond) + + ret i32 %0 +} + +@.str = private unnamed_addr constant [4 x i8] c"sth\00", section "llvm.metadata" +@.str1 = private unnamed_addr constant [4 x i8] c"t.c\00", section "llvm.metadata" + +declare i64 @llvm.objectsize.i64.p0i8(i8*, i1) +declare i32 @llvm.annotation.i32(i32, i8*, i8*, i32) +declare i8* @llvm.ptr.annotation.p0i8(i8*, i8*, i8*, i32) + +declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture) +declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture) + +declare {}* @llvm.invariant.start.p0i8(i64, i8* nocapture) +declare void @llvm.invariant.end.p0i8({}*, i64, i8* nocapture) +declare void @llvm.assume(i1)