2016-05-28 00:14:15 +08:00
; RUN: opt < %s -instcombine -sample-profile -sample-profile-file=%S/Inputs/calls.prof | opt -analyze -branch-prob | FileCheck %s
2016-05-28 07:20:16 +08:00
; RUN: opt < %s -passes="function(instcombine),sample-profile" -sample-profile-file=%S/Inputs/calls.prof | opt -analyze -branch-prob | FileCheck %s
2014-01-11 07:23:51 +08:00
; Original C++ test case
;
; #include <stdio.h>
;
; int sum(int x, int y) {
; return x + y;
; }
;
; int main() {
; int s, i = 0;
; while (i++ < 20000 * 20000)
; if (i != 100) s = sum(i, s); else s = 30;
; printf("sum is %d\n", s);
; return 0;
; }
2014-10-22 20:59:00 +08:00
;
2014-01-11 07:23:51 +08:00
@.str = private unnamed_addr constant [ 11 x i8 ] c "sum is %d\0A\00" , align 1
; Function Attrs: nounwind uwtable
2015-11-06 06:03:56 +08:00
define i32 @_Z3sumii ( i32 %x , i32 %y ) !dbg !4 {
2014-01-11 07:23:51 +08:00
entry:
%x.addr = alloca i32 , align 4
%y.addr = alloca i32 , align 4
store i32 %x , i32 * %x.addr , align 4
store i32 %y , i32 * %y.addr , align 4
2015-02-28 05:17:42 +08:00
%0 = load i32 , i32 * %x.addr , align 4 , !dbg !11
%1 = load i32 , i32 * %y.addr , align 4 , !dbg !11
2014-01-11 07:23:51 +08:00
%add = add nsw i32 %0 , %1 , !dbg !11
ret i32 %add , !dbg !11
}
; Function Attrs: uwtable
2015-11-06 06:03:56 +08:00
define i32 @main ( ) !dbg !7 {
2014-01-11 07:23:51 +08:00
entry:
%retval = alloca i32 , align 4
%s = alloca i32 , align 4
%i = alloca i32 , align 4
store i32 0 , i32 * %retval
store i32 0 , i32 * %i , align 4 , !dbg !12
br label %while.cond , !dbg !13
while.cond: ; preds = %if.end, %entry
2015-02-28 05:17:42 +08:00
%0 = load i32 , i32 * %i , align 4 , !dbg !14
2014-03-11 06:41:28 +08:00
%inc = add nsw i32 %0 , 1 , !dbg !14
store i32 %inc , i32 * %i , align 4 , !dbg !14
%cmp = icmp slt i32 %0 , 400000000 , !dbg !14
br i1 %cmp , label %while.body , label %while.end , !dbg !14
2017-03-07 01:49:59 +08:00
; CHECK: edge while.cond -> while.body probability is 0x77f2798d / 0x80000000 = 93.71% [HOT edge]
; CHECK: edge while.cond -> while.end probability is 0x080d8673 / 0x80000000 = 6.29%
2014-01-11 07:23:51 +08:00
while.body: ; preds = %while.cond
2015-02-28 05:17:42 +08:00
%1 = load i32 , i32 * %i , align 4 , !dbg !16
2014-03-11 06:41:28 +08:00
%cmp1 = icmp ne i32 %1 , 100 , !dbg !16
br i1 %cmp1 , label %if.then , label %if.else , !dbg !16
; Without discriminator information, the profiler used to think that
; both branches out of while.body had the same weight. In reality,
; the edge while.body->if.then is taken most of the time.
;
2017-03-07 01:49:59 +08:00
; CHECK: edge while.body -> if.else probability is 0x0005b1e0 / 0x80000000 = 0.02%
; CHECK: edge while.body -> if.then probability is 0x7ffa4e20 / 0x80000000 = 99.98% [HOT edge]
2014-03-11 06:41:28 +08:00
2014-01-11 07:23:51 +08:00
if.then: ; preds = %while.body
2015-02-28 05:17:42 +08:00
%2 = load i32 , i32 * %i , align 4 , !dbg !18
%3 = load i32 , i32 * %s , align 4 , !dbg !18
2014-03-11 06:41:28 +08:00
%call = call i32 @_Z3sumii ( i32 %2 , i32 %3 ) , !dbg !18
store i32 %call , i32 * %s , align 4 , !dbg !18
br label %if.end , !dbg !18
2014-01-11 07:23:51 +08:00
if.else: ; preds = %while.body
2014-03-11 06:41:28 +08:00
store i32 30 , i32 * %s , align 4 , !dbg !20
2014-01-11 07:23:51 +08:00
br label %if.end
if.end: ; preds = %if.else, %if.then
2014-03-11 06:41:28 +08:00
br label %while.cond , !dbg !22
2014-01-11 07:23:51 +08:00
while.end: ; preds = %while.cond
2015-02-28 05:17:42 +08:00
%4 = load i32 , i32 * %s , align 4 , !dbg !24
[opaque pointer type] Add textual IR support for explicit type parameter to the call instruction
See r230786 and r230794 for similar changes to gep and load
respectively.
Call is a bit different because it often doesn't have a single explicit
type - usually the type is deduced from the arguments, and just the
return type is explicit. In those cases there's no need to change the
IR.
When that's not the case, the IR usually contains the pointer type of
the first operand - but since typed pointers are going away, that
representation is insufficient so I'm just stripping the "pointerness"
of the explicit type away.
This does make the IR a bit weird - it /sort of/ reads like the type of
the first operand: "call void () %x(" but %x is actually of type "void
()*" and will eventually be just of type "ptr". But this seems not too
bad and I don't think it would benefit from repeating the type
("void (), void () * %x(" and then eventually "void (), ptr %x(") as has
been done with gep and load.
This also has a side benefit: since the explicit type is no longer a
pointer, there's no ambiguity between an explicit type and a function
that returns a function pointer. Previously this case needed an explicit
type (eg: a function returning a void() function was written as
"call void () () * @x(" rather than "call void () * @x(" because of the
ambiguity between a function returning a pointer to a void() function
and a function returning void).
No ambiguity means even function pointer return types can just be
written alone, without writing the whole function's type.
This leaves /only/ the varargs case where the explicit type is required.
Given the special type syntax in call instructions, the regex-fu used
for migration was a bit more involved in its own unique way (as every
one of these is) so here it is. Use it in conjunction with the apply.sh
script and associated find/xargs commands I've provided in rr230786 to
migrate your out of tree tests. Do let me know if any of this doesn't
cover your cases & we can iterate on a more general script/regexes to
help others with out of tree tests.
About 9 test cases couldn't be automatically migrated - half of those
were functions returning function pointers, where I just had to manually
delete the function argument types now that we didn't need an explicit
function type there. The other half were typedefs of function types used
in calls - just had to manually drop the * from those.
import fileinput
import sys
import re
pat = re.compile(r'((?:=|:|^|\s)call\s(?:[^@]*?))(\s*$|\s*(?:(?:\[\[[a-zA-Z0-9_]+\]\]|[@%](?:(")?[\\\?@a-zA-Z0-9_.]*?(?(3)"|)|{{.*}}))(?:\(|$)|undef|inttoptr|bitcast|null|asm).*$)')
addrspace_end = re.compile(r"addrspace\(\d+\)\s*\*$")
func_end = re.compile("(?:void.*|\)\s*)\*$")
def conv(match, line):
if not match or re.search(addrspace_end, match.group(1)) or not re.search(func_end, match.group(1)):
return line
return line[:match.start()] + match.group(1)[:match.group(1).rfind('*')].rstrip() + match.group(2) + line[match.end():]
for line in sys.stdin:
sys.stdout.write(conv(re.search(pat, line), line))
llvm-svn: 235145
2015-04-17 07:24:18 +08:00
%call2 = call i32 ( i8 * , . . . ) @printf ( i8 * getelementptr inbounds ( [ 11 x i8 ] , [ 11 x i8 ] * @.str , i32 0 , i32 0 ) , i32 %4 ) , !dbg !24
2014-03-11 06:41:28 +08:00
ret i32 0 , !dbg !25
2014-01-11 07:23:51 +08:00
}
2014-03-11 06:41:28 +08:00
declare i32 @printf ( i8 * , . . . ) #2
2014-01-11 07:23:51 +08:00
2016-04-11 23:53:44 +08:00
!llvm.dbg.cu = ! { !0 }
2014-01-11 07:23:51 +08:00
!llvm.module.flags = ! { !8 , !9 }
!llvm.ident = ! { !10 }
2016-04-15 23:57:41 +08:00
!0 = distinct !DICompileUnit ( language: D W _ L A N G _ C _ p l u s _ p l u s , producer: "clang version 3.5 " , isOptimized: false , emissionKind: N o D e b u g , file: !1 , enums: !2 , retainedTypes: !2 , globals: !2 , imports: !2 )
2015-04-30 00:38:44 +08:00
!1 = !DIFile ( filename: "calls.cc" , directory: "." )
IR: Make metadata typeless in assembly
Now that `Metadata` is typeless, reflect that in the assembly. These
are the matching assembly changes for the metadata/value split in
r223802.
- Only use the `metadata` type when referencing metadata from a call
intrinsic -- i.e., only when it's used as a `Value`.
- Stop pretending that `ValueAsMetadata` is wrapped in an `MDNode`
when referencing it from call intrinsics.
So, assembly like this:
define @foo(i32 %v) {
call void @llvm.foo(metadata !{i32 %v}, metadata !0)
call void @llvm.foo(metadata !{i32 7}, metadata !0)
call void @llvm.foo(metadata !1, metadata !0)
call void @llvm.foo(metadata !3, metadata !0)
call void @llvm.foo(metadata !{metadata !3}, metadata !0)
ret void, !bar !2
}
!0 = metadata !{metadata !2}
!1 = metadata !{i32* @global}
!2 = metadata !{metadata !3}
!3 = metadata !{}
turns into this:
define @foo(i32 %v) {
call void @llvm.foo(metadata i32 %v, metadata !0)
call void @llvm.foo(metadata i32 7, metadata !0)
call void @llvm.foo(metadata i32* @global, metadata !0)
call void @llvm.foo(metadata !3, metadata !0)
call void @llvm.foo(metadata !{!3}, metadata !0)
ret void, !bar !2
}
!0 = !{!2}
!1 = !{i32* @global}
!2 = !{!3}
!3 = !{}
I wrote an upgrade script that handled almost all of the tests in llvm
and many of the tests in cfe (even handling many `CHECK` lines). I've
attached it (or will attach it in a moment if you're speedy) to PR21532
to help everyone update their out-of-tree testcases.
This is part of PR21532.
llvm-svn: 224257
2014-12-16 03:07:53 +08:00
!2 = ! { }
2016-04-15 23:57:41 +08:00
!4 = distinct !DISubprogram ( name: "sum" , line: 3 , isLocal: false , isDefinition: true , virtualIndex: 6 , flags: D I F l a g P r o t o t y p e d , isOptimized: false , unit: !0 , scopeLine: 3 , file: !1 , scope: !5 , type: !6 , variables: !2 )
2015-04-30 00:38:44 +08:00
!5 = !DIFile ( filename: "calls.cc" , directory: "." )
!6 = !DISubroutineType ( types: !2 )
2016-04-15 23:57:41 +08:00
!7 = distinct !DISubprogram ( name: "main" , line: 7 , isLocal: false , isDefinition: true , virtualIndex: 6 , flags: D I F l a g P r o t o t y p e d , isOptimized: false , unit: !0 , scopeLine: 7 , file: !1 , scope: !5 , type: !6 , variables: !2 )
IR: Make metadata typeless in assembly
Now that `Metadata` is typeless, reflect that in the assembly. These
are the matching assembly changes for the metadata/value split in
r223802.
- Only use the `metadata` type when referencing metadata from a call
intrinsic -- i.e., only when it's used as a `Value`.
- Stop pretending that `ValueAsMetadata` is wrapped in an `MDNode`
when referencing it from call intrinsics.
So, assembly like this:
define @foo(i32 %v) {
call void @llvm.foo(metadata !{i32 %v}, metadata !0)
call void @llvm.foo(metadata !{i32 7}, metadata !0)
call void @llvm.foo(metadata !1, metadata !0)
call void @llvm.foo(metadata !3, metadata !0)
call void @llvm.foo(metadata !{metadata !3}, metadata !0)
ret void, !bar !2
}
!0 = metadata !{metadata !2}
!1 = metadata !{i32* @global}
!2 = metadata !{metadata !3}
!3 = metadata !{}
turns into this:
define @foo(i32 %v) {
call void @llvm.foo(metadata i32 %v, metadata !0)
call void @llvm.foo(metadata i32 7, metadata !0)
call void @llvm.foo(metadata i32* @global, metadata !0)
call void @llvm.foo(metadata !3, metadata !0)
call void @llvm.foo(metadata !{!3}, metadata !0)
ret void, !bar !2
}
!0 = !{!2}
!1 = !{i32* @global}
!2 = !{!3}
!3 = !{}
I wrote an upgrade script that handled almost all of the tests in llvm
and many of the tests in cfe (even handling many `CHECK` lines). I've
attached it (or will attach it in a moment if you're speedy) to PR21532
to help everyone update their out-of-tree testcases.
This is part of PR21532.
llvm-svn: 224257
2014-12-16 03:07:53 +08:00
!8 = ! { i32 2 , !"Dwarf Version" , i32 4 }
2015-03-04 01:24:31 +08:00
!9 = ! { i32 1 , !"Debug Info Version" , i32 3 }
IR: Make metadata typeless in assembly
Now that `Metadata` is typeless, reflect that in the assembly. These
are the matching assembly changes for the metadata/value split in
r223802.
- Only use the `metadata` type when referencing metadata from a call
intrinsic -- i.e., only when it's used as a `Value`.
- Stop pretending that `ValueAsMetadata` is wrapped in an `MDNode`
when referencing it from call intrinsics.
So, assembly like this:
define @foo(i32 %v) {
call void @llvm.foo(metadata !{i32 %v}, metadata !0)
call void @llvm.foo(metadata !{i32 7}, metadata !0)
call void @llvm.foo(metadata !1, metadata !0)
call void @llvm.foo(metadata !3, metadata !0)
call void @llvm.foo(metadata !{metadata !3}, metadata !0)
ret void, !bar !2
}
!0 = metadata !{metadata !2}
!1 = metadata !{i32* @global}
!2 = metadata !{metadata !3}
!3 = metadata !{}
turns into this:
define @foo(i32 %v) {
call void @llvm.foo(metadata i32 %v, metadata !0)
call void @llvm.foo(metadata i32 7, metadata !0)
call void @llvm.foo(metadata i32* @global, metadata !0)
call void @llvm.foo(metadata !3, metadata !0)
call void @llvm.foo(metadata !{!3}, metadata !0)
ret void, !bar !2
}
!0 = !{!2}
!1 = !{i32* @global}
!2 = !{!3}
!3 = !{}
I wrote an upgrade script that handled almost all of the tests in llvm
and many of the tests in cfe (even handling many `CHECK` lines). I've
attached it (or will attach it in a moment if you're speedy) to PR21532
to help everyone update their out-of-tree testcases.
This is part of PR21532.
llvm-svn: 224257
2014-12-16 03:07:53 +08:00
!10 = ! { !"clang version 3.5 " }
2015-04-30 00:38:44 +08:00
!11 = !DILocation ( line: 4 , scope: !4 )
!12 = !DILocation ( line: 8 , scope: !7 )
!13 = !DILocation ( line: 9 , scope: !7 )
!14 = !DILocation ( line: 9 , scope: !15 )
2017-02-24 02:27:45 +08:00
!15 = !DILexicalBlockFile ( discriminator: 2 , file: !1 , scope: !7 )
2015-04-30 00:38:44 +08:00
!16 = !DILocation ( line: 10 , scope: !17 )
!17 = distinct !DILexicalBlock ( line: 10 , column: 0 , file: !1 , scope: !7 )
!18 = !DILocation ( line: 10 , scope: !19 )
2017-02-24 02:27:45 +08:00
!19 = !DILexicalBlockFile ( discriminator: 2 , file: !1 , scope: !17 )
2015-04-30 00:38:44 +08:00
!20 = !DILocation ( line: 10 , scope: !21 )
2017-02-24 02:27:45 +08:00
!21 = !DILexicalBlockFile ( discriminator: 4 , file: !1 , scope: !17 )
2015-04-30 00:38:44 +08:00
!22 = !DILocation ( line: 10 , scope: !23 )
2017-02-24 02:27:45 +08:00
!23 = !DILexicalBlockFile ( discriminator: 6 , file: !1 , scope: !17 )
2015-04-30 00:38:44 +08:00
!24 = !DILocation ( line: 11 , scope: !7 )
!25 = !DILocation ( line: 12 , scope: !7 )