A problem met during using -ftrapv

Hi There

When I tried to compile a simple program shown as follows using this command: clang -o add add.c -ftrapv

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

void overflow_handler(long long a, long long b, char c, char d) {
   printf("overflow occurs!\n");
}

void (*__overflow_handler)(long long, long long, char, char) = overflow_handler;

int main(void) {
   int64_t test = INT64_MAX + 1;
   printf("test: %lld\n", test);
}

I got the following error message:
clang: X86ISelLowering.cpp:7726: virtual void llvm::X86TargetLowering::ReplaceNodeResults(llvm::SDNode*, llvm::SmallVectorImpl<llvm::SDValue>&, llvm::SelectionDAG&) const: Assertion `false && "Do not know how to custom type legalize this operation!"' failed.
0 clang 0x08fe24c8
Stack dump:
0. Program arguments: /home/peng/LLVM-INSTALL/llvm-install/bin/clang -cc1 -triple i386-pc-linux-gnu -S -disable-free -main-file-name division.c -mrelocation-model static -mdisable-fp-elim -mconstructor-aliases -target-cpu pentium4 -resource-dir /home/peng/LLVM-INSTALL/llvm-install/lib/clang/1.5 -ferror-limit 19 -ftemplate-backtrace-limit 10 -fmessage-length 237 -ftrapv -fgnu-runtime -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/cc-Su3esc.s -x c division.c
1. <eof> parser at end of file
2. Code generation
3. Running pass 'X86 DAG->DAG Instruction Selection' on function '@main'
clang: error: compiler command failed due to signal 6 (use -v to see invocation)

And, it is obvious that this snippet code will lead to integer overflow, when I tested the subtraction operation by replacing
   int64_t test = INT64_MAX + 1;
to
   int64_t test = INT64_MIN - 1;
The same error message still came out.

However, when I lower the 64 bit operation down to 32 bit, i.e., I changed INT64_MAX to INT32_MAX, then the problem disappeared, and the overflow could be caught successfully.Seems the problem occurs only if I used the 64-bit operations, it is wired!

Thanks

Peng

Looks like the x86-32 backend doesn't handle
@llvm.sadd.with.overflow.i64 correctly; please file in Bugzilla.

-Eli

Hi Eli,

I got the following error message:
clang: X86ISelLowering.cpp:7726: virtual void
llvm::X86TargetLowering::ReplaceNodeResults(llvm::SDNode*,
llvm::SmallVectorImpl<llvm::SDValue>&, llvm::SelectionDAG&) const:
Assertion `false&& "Do not know how to custom type legalize this
operation!"' failed.

Looks like the x86-32 backend doesn't handle
@llvm.sadd.with.overflow.i64 correctly; please file in Bugzilla.

I'm a bit surprised, because LegalizeTypes has logic for overflow operations:

   case ISD::SADDO:
   case ISD::SSUBO: Res = PromoteIntRes_SADDSUBO(N, ResNo); break;
   case ISD::UADDO:
   case ISD::USUBO: Res = PromoteIntRes_UADDSUBO(N, ResNo); break;
   case ISD::SMULO:
   case ISD::UMULO: Res = PromoteIntRes_XMULO(N, ResNo); break;

Ciao,

Duncan.

The return type of these two should be long long. Not sure if this is related to your problem. The return value from this function will be truncated and used as the result of the operation.

David

-- Sent from my PDP-11

Hi David

I think the problem is not related to the return type. I have tested it again after I modified the return type to "long long", the same error still came out.

Seems that when those arithmetic operations are considered as the 64-bit operations, this error will occur.

I tested via following simple code:

   int64_t operand = -1;
   int64_t test = operand - 1;
   printf("test: %lld\n", test);

The same compilation error still occurred.

Regards

Peng

David Chisnall wrote: