Index: lib/MC/MCExpr.cpp =================================================================== --- lib/MC/MCExpr.cpp +++ lib/MC/MCExpr.cpp @@ -659,13 +659,20 @@ return false; Res = MCValue::get(!Value.getConstant()); break; - case MCUnaryExpr::Minus: + case MCUnaryExpr::Minus: { /// -(a - b + const) ==> (b - a - const) if (Value.getSymA() && !Value.getSymB()) return false; - Res = MCValue::get(Value.getSymB(), Value.getSymA(), - -Value.getConstant()); + + // Avoid undefined behavior: the negation of the minimum constant is the + // minimum constant. + int64_t ValConst = Value.getConstant(); + if (ValConst != INT64_MIN) + ValConst = -ValConst; + + Res = MCValue::get(Value.getSymB(), Value.getSymA(), ValConst); break; + } case MCUnaryExpr::Not: if (!Value.isAbsolute()) return false; Index: test/MC/X86/imm-comments.s =================================================================== --- test/MC/X86/imm-comments.s +++ test/MC/X86/imm-comments.s @@ -10,7 +10,9 @@ movl $-2147483648, %eax movabsq $9223372036854775807, %rax -movabsq $-9223372036854775808, %rax + +# This line should not induce undefined behavior via negation of INT64_MIN. +movabsq $-9223372036854775808, %rax # CHECK: movb $127, %al # CHECK: movb $-128, %al