-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Description
| Bugzilla Link | 9120 |
| Resolution | FIXED |
| Resolved on | Feb 13, 2011 02:28 |
| Version | trunk |
| OS | Linux |
| Reporter | LLVM Bugzilla Contributor |
| CC | @lattner,@nlewycky |
Extended Description
This is gcc pr 47579:
#include
extern void b1(), b2();
void foo(const std::vector& v) { if (v.size() == 0) b1(); else b2(); }
which demonstrates a really neat missed optimization:
define void @_Z3fooRKSt6vectorIiSaIiEE(%"class.std::vector"* nocapture %v) {
entry:
%tmp2.i = getelementptr inbounds %"class.std::vector"* %v, i64 0, i32 0, i64 8
%0 = bitcast i8* %tmp2.i to i32**
%tmp3.i = load i32** %0, align 8, !tbaa !0
%tmp5.i = bitcast %"class.std::vector"* %v to i32**
%tmp6.i = load i32** %tmp5.i, align 8, !tbaa !0
%sub.ptr.lhs.cast.i = ptrtoint i32* %tmp3.i to i64
%sub.ptr.rhs.cast.i = ptrtoint i32* %tmp6.i to i64
%sub.ptr.sub.i = sub i64 %sub.ptr.lhs.cast.i, %sub.ptr.rhs.cast.i
%cmp = icmp ult i64 %sub.ptr.sub.i, 4
br i1 %cmp, label %if.then, label %if.else
[...]
Is the difference between the pointers (%tmp3.i, %tmp6.i) less than 4? Well, the pointers are aligned so that's always true. However, LLVM doesn't know that.
We mark pointer alignment at the use (load/store) instead of the definition. In this case, I think we should add an alignment field to ptrtoint. Clang could set this based on user annotations or the minimum alignment of the type.
If we need a source annotation to make this work, we should make sure libc++ has it too.