Index: lib/Analysis/ValueTracking.cpp =================================================================== --- lib/Analysis/ValueTracking.cpp +++ lib/Analysis/ValueTracking.cpp @@ -20,6 +20,7 @@ #include "llvm/Analysis/MemoryBuiltins.h" #include "llvm/Analysis/Loads.h" #include "llvm/Analysis/LoopInfo.h" +#include "llvm/Analysis/OptimizationDiagnosticInfo.h" #include "llvm/Analysis/VectorUtils.h" #include "llvm/IR/CallSite.h" #include "llvm/IR/ConstantRange.h" @@ -793,13 +794,15 @@ // have a logical fallacy. This should only happen when a program has // undefined behavior. We can't assert/crash, so clear out the known bits and // hope for the best. - - // FIXME: Publish a warning/remark that we have encountered UB or the compiler - // is broken. - if ((KnownZero & KnownOne) != 0) { KnownZero.clearAllBits(); KnownOne.clearAllBits(); + + Instruction *CxtI = const_cast(Q.CxtI); + OptimizationRemarkEmitter ORE(CxtI->getFunction()); + ORE.emit(OptimizationRemarkMissed("value-tracking", "bad assumption", CxtI) + << "Detected conflicting code assumptions. Program has undefined " + "behavior or internal compiler error."); } } Index: test/Transforms/InstSimplify/assume.ll =================================================================== --- test/Transforms/InstSimplify/assume.ll +++ test/Transforms/InstSimplify/assume.ll @@ -1,5 +1,10 @@ ; NOTE: Assertions have been autogenerated by update_test_checks.py -; RUN: opt -instsimplify -S < %s | FileCheck %s +; RUN: opt -instsimplify -S < %s 2>&1 -pass-remarks-missed=.* | FileCheck %s + +; Verify that warnings are emitted for the 2nd and 3rd tests. + +; CHECK: remark: /tmp/s.c:1:13: Detected conflicting code assumptions. Program has undefined behavior or internal compiler error. +; CHECK: remark: /tmp/s.c:4:10: Detected conflicting code assumptions. Program has undefined behavior or internal compiler error. define void @test1() { ; CHECK-LABEL: @test1( @@ -15,12 +20,12 @@ ; return value. There's no way to win (we can't undo transforms that happened ; based on half-truths), so just don't crash. -define i64 @PR31809() { +define i64 @PR31809() !dbg !7 { ; CHECK-LABEL: @PR31809( ; CHECK-NEXT: ret i64 3 ; %a = alloca i32 - %t1 = ptrtoint i32* %a to i64 + %t1 = ptrtoint i32* %a to i64, !dbg !9 %cond = icmp eq i64 %t1, 3 call void @llvm.assume(i1 %cond) ret i64 %t1 @@ -30,14 +35,14 @@ ; so just don't crash. The second icmp+assume gets processed later, so that ; determines the return value. -define i8 @conflicting_assumptions(i8 %x) { +define i8 @conflicting_assumptions(i8 %x) !dbg !10 { ; CHECK-LABEL: @conflicting_assumptions( ; CHECK-NEXT: call void @llvm.assume(i1 false) ; CHECK-NEXT: [[COND2:%.*]] = icmp eq i8 %x, 4 ; CHECK-NEXT: call void @llvm.assume(i1 [[COND2]]) ; CHECK-NEXT: ret i8 5 ; - %add = add i8 %x, 1 + %add = add i8 %x, 1, !dbg !11 %cond1 = icmp eq i8 %x, 3 call void @llvm.assume(i1 %cond1) %cond2 = icmp eq i8 %x, 4 @@ -47,3 +52,21 @@ declare void @llvm.assume(i1) nounwind +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 4.0.0 (trunk 282540) (llvm/trunk 282542)", isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly, enums: !2) +!1 = !DIFile(filename: "/tmp/s.c", directory: "/tmp") +!2 = !{} +!3 = !{i32 2, !"Dwarf Version", i32 4} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"PIC Level", i32 2} +!6 = !{!"clang version 4.0.0 (trunk 282540) (llvm/trunk 282542)"} +!7 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: !8, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: true, unit: !0, variables: !2) +!8 = !DISubroutineType(types: !2) +!9 = !DILocation(line: 1, column: 13, scope: !7) +!10 = distinct !DISubprogram(name: "bar", scope: !1, file: !1, line: 3, type: !8, isLocal: false, isDefinition: true, scopeLine: 3, isOptimized: true, unit: !0, variables: !2) +!11 = !DILocation(line: 4, column: 10, scope: !10) +!12 = !DILocation(line: 4, column: 3, scope: !10) +