result of multiplication of two bit-fields not truncated
| Bugzilla Link | 9562 |
| Resolution | WONTFIX |
| Resolved on | Mar 25, 2011 18:57 |
| Version | trunk |
| OS | Linux |
| CC | @efriedma-quic |
Extended Description
In the following example, result of a.u33 * a.u33 is not truncated to a 33-bit value (see entry basic block in main):
.c file (based on bitfld-3.c from torture test)
extern void exit (int); extern void abort (void);
struct s { unsigned long long u33: 33; unsigned long long u40: 40; unsigned long long u41: 41; };
struct s a = { 0x100000, 0x100000, 0x100000 };
int main (void) { if (a.u33 * a.u33 != 0 || a.u33 * a.u40 != 0 || a.u40 * a.u33 != 0 || a.u40 * a.u40 != 0) abort (); exit (0); }
.ll file
clang bitfld1.c -S -emit-llvm -O3 -o -
; ModuleID = 'bitfld1.c' target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f3 2:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32" target triple = "i386-pc-linux-gnu"
%0 = type { i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8 }
@a = global %0 { i8 0, i8 0, i8 16, i8 0, i8 0, i8 0, i8 32, i8 0, i8 0, i8 0, i 8 0, i8 32, i8 0, i8 0, i8 0, i8 undef }, align 8
define i32 @main() noreturn nounwind { entry: %0 = load i64* bitcast (%0* @a to i64*), align 8 %bf.clear = and i64 %0, 8589934591 %mul = mul i64 %bf.clear, %bf.clear %cmp = icmp eq i64 %mul, 0 br i1 %cmp, label %lor.lhs.false, label %if.then
lor.lhs.false: ; preds = %entry %1 = lshr i64 %0, 33 %2 = load i64* bitcast (i8* getelementptr inbounds (%0* @a, i32 0, i32 8) to i 64*), align 8
As far as I know, nobody depends on this in practice, so clang doesn't bother to copy gcc's exact behavior.
This is the whole .ll file produced.
clang bitfld1.c -S -emit-llvm -O3 -o -
; ModuleID = 'bitfld1.c' target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f3 2:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32" target triple = "i386-pc-linux-gnu"
%0 = type { i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8 }
@a = global %0 { i8 0, i8 0, i8 16, i8 0, i8 0, i8 0, i8 32, i8 0, i8 0, i8 0, i 8 0, i8 32, i8 0, i8 0, i8 0, i8 undef }, align 8
define i32 @main() noreturn nounwind { entry: %0 = load i64* bitcast (%0* @a to i64*), align 8 %bf.clear = and i64 %0, 8589934591 %mul = mul i64 %bf.clear, %bf.clear %cmp = icmp eq i64 %mul, 0 br i1 %cmp, label %lor.lhs.false, label %if.then
lor.lhs.false: ; preds = %entry %1 = lshr i64 %0, 33 %2 = load i64* bitcast (i8* getelementptr inbounds (%0* @a, i32 0, i32 8) to i 64*), align 8 %bf.clear4 = shl i64 %2, 31 %3 = and i64 %bf.clear4, 1097364144128 %4 = or i64 %3, %1 %mul5 = mul i64 %4, %bf.clear %mul18 = mul i64 %4, %4 %5 = or i64 %mul5, %mul18 %6 = icmp eq i64 %5, 0 br i1 %6, label %if.end, label %if.then
if.then: ; preds = %lor.lhs.false, %entry tail call void @abort() noreturn nounwind unreachable
if.end: ; preds = %lor.lhs.false tail call void @exit(i32 0) noreturn nounwind unreachable }
declare void @abort() noreturn
declare void @exit(i32) noreturn