Skip to content

Instantly share code, notes, and snippets.

@amosr
Last active May 3, 2016 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amosr/06938ee5becaf5249de5634499bc1add to your computer and use it in GitHub Desktop.
Save amosr/06938ee5becaf5249de5634499bc1add to your computer and use it in GitHub Desktop.
No global value numbering / duplicate removal
#include <stdio.h>
void b01_sums(int size, int* A)
{
int sum0 = 0;
int sum1 = 0;
for (int i = 0; i != size; ++i)
{
sum0 = sum0 + A[i];
sum1 = sum1 + A[i];
printf("sum0: %d\nsum1: %d\n", sum0, sum1);
}
}
; generated with
; gcc sums.c -c -O3 -save-temps -emit-llvm
; opt sums.bc -gvn -S -O3
; note that there are still two loop accumulators (sum0 and sum1)
define void @b01_sums(i32 %size, i32* nocapture readonly %A) #0 {
%1 = icmp eq i32 %size, 0
br i1 %1, label %._crit_edge, label %.lr.ph.preheader
.lr.ph.preheader: ; preds = %0
%2 = add i32 %size, -1
br label %.lr.ph
.lr.ph: ; preds = %.lr.ph.preheader, %.lr.ph
%indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %.lr.ph.preheader ]
; two sum accumulators, both the same
%sum1.02 = phi i32 [ %6, %.lr.ph ], [ 0, %.lr.ph.preheader ]
%sum0.01 = phi i32 [ %5, %.lr.ph ], [ 0, %.lr.ph.preheader ]
%3 = getelementptr inbounds i32* %A, i64 %indvars.iv
%4 = load i32* %3, align 4, !tbaa !1
%5 = add nsw i32 %4, %sum0.01
%6 = add nsw i32 %4, %sum1.02
%7 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([19 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6) #2
%indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
%lftr.wideiv1 = trunc i64 %indvars.iv to i32
%exitcond2 = icmp eq i32 %lftr.wideiv1, %2
br i1 %exitcond2, label %._crit_edge.loopexit, label %.lr.ph
._crit_edge.loopexit: ; preds = %.lr.ph
br label %._crit_edge
._crit_edge: ; preds = %._crit_edge.loopexit, %0
ret void
}
# generated with:
# gcc sums.c -c -O3 -save-temps
# r15 = A[i]
# r14d = size
testl %r14d, %r14d
je LBB0_3
## BB#1:
# r13d = sum0 = 0
xorl %r13d, %r13d
# literal printf string
leaq L_.str(%rip), %r12
# ebx = sum1 = 0
xorl %ebx, %ebx
# start loop
LBB0_2: ## %.lr.ph
## =>This Inner Loop Header: Depth=1
# A[i] (only once)
movl (%r15), %eax
# add sum0 and sum1
addl %eax, %ebx
addl %eax, %r13d
# printf call
xorl %eax, %eax
movq %r12, %rdi
movl %ebx, %esi
movl %r13d, %edx
callq _printf
# increment etc
addq $4, %r15
decl %r14d
jne LBB0_2
LBB0_3: ## %._crit_edge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment