4 #//===----------------------------------------------------------------------===//
6 #// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
7 #// See https://llvm.org/LICENSE.txt for license information.
8 #// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10 #//===----------------------------------------------------------------------===//
17 use lib
"$FindBin::Bin/lib";
21 our $VERSION = "0.004";
26 my $hex = qr{[0-9a-f]}i; # hex digit.
28 # mic-specific details.
31 # Before we allowed both elf64-x86-64-freebsd and elf-l1om-freebsd.
32 # Now the first one is obsolete, only elf64-l1om-freebsd is allowed.
35 } elsif ( "$target_mic_arch" eq "knf" ) {
36 return $fmt !~ m{\Aelf64-l1om?\z};
37 } elsif ( "$target_mic_arch" eq "knc" ) {
38 return $fmt !~ m{\Aelf64-k1om?\z};
44 # Undesired instructions for mic: all x87 and some other.
45 # AC: Since compiler 2010-06-30 x87 instructions are supported, removed the check of x87.
47 sub bad_mic_instr
($$) {
48 my ( $instr, $args ) = @_;
49 if ( "$target_mic_arch" eq "knc" ) {
50 # workaround of bad code generation on KNF Linux* OS:
51 return ( defined( $instr ) and $instr =~ $mic_bad_re );
53 return ( defined( $instr ) and $instr =~ $mic_bad_re or defined( $args ) and $args =~ m{xmm}i );
55 }; # sub bad_mic_instr
57 # lin_32-specific details.
61 return $fmt !~ m{\Aelf32-i386\z};
66 movapd movupd movhpd movlpd movmskpd movsd
67 addpd addsd subpd subsd mulpd mulsd divpd divsd sqrtpd sqrtsd maxpd maxsd minpd minsd
68 andpd andnpd orpd xorpd
69 cmppd cmpsd comisd ucomisd
70 shufpd unpckhpd unpcklpd
71 cvtpd2pi cvttpd2pi cvtpi2pd cvtpd2dq cvttpd2dq cvtdq2pd cvtps2pd cvtpd2ps cvtss2sd cvtsd2ss
72 cvtsd2si cvttsd2si cvtsi2sd cvtdq2ps cvtps2dq cvttps2dq movdqa movdqu movq2dq movdq2q
73 pmuludq paddq psubq pshuflw pshufhw pshufd pslldq psrldq punpckhqdq punpcklqdq clflush
74 lfence mfence maskmovdqu movntpd movntdq movnti
78 fisttp lddqu addsubps addsubpd haddps hsubps haddpd hsubpd movshdup movsldup movddup monitor
83 phaddw phaddsw phaddd phsubw phsubsw phsubd pabsb pabsw pabsd pmaddubsw pmulhrsw pshufb
84 psignb psignw psignd palignr
90 pmulld pmuldq dppd dpps movntdqa blendpd blendps blendvpd blendvps pblendvb pblendw pminuw
91 pminud pminsb pminsd pmaxuw pmaxud pmaxsb pmaxsd roundps roundpd roundss roundsd extractps
92 insertps pinsrb pinsrd pinsrq pextrb pextrw pextrd pextrq pmovsxbw pmovzxbw pmovsxbd
93 pmovzxbd pmovsxwd pmovzxwd pmovsxbq pmovzxbq pmovsxwq pmovzxwq pmovsxdq pmovzxdq mpsadbw
94 phminposuw ptest pcmpeqq packusdw
98 pcmpestri pcmpestrm pcmpistri pcmpistrm pcmpgtq crc32 popcnt
102 # Undesired instructions for IA-32 architecture: Pentium 4 (SSE2) and newer.
103 # TODO: It would be much more reliable to list *allowed* instructions rather than list undesired
104 # instructions. In such a case the list will be stable and not require update when SSE5 is released.
105 my @ia32_bad_list = ( @sse2, @sse3, @ssse3, @sse4 );
107 my $ia32_bad_re = qr{@{[ "^(?:" . join( "|", @ia32_bad_list ) . ")" ]}}i
;
109 sub bad_ia32_instr
($$) {
110 my ( $instr, $args ) = @_;
111 return ( defined( $instr ) and $instr =~ $ia32_bad_re );
112 }; # sub bad_ia32_instr
114 sub check_file
($;$$) {
116 my ( $file, $show_instructions, $max_instructions ) = @_;
119 if ( not defined( $max_instructions ) ) {
120 $max_instructions = 100;
123 execute
( [ "x86_64-k1om-linux-objdump", "-d", $file ], -stdout
=> \
@bulk );
127 my $current_func = ""; # Name of current function.
128 my $reported_func = ""; # name of last reported function.
129 foreach my $line ( @bulk ) {
132 } elsif ( $line =~ m{^\s*$} ) {
135 } elsif ( $line =~ m{^In archive (.*?):\s*$} ) {
136 # In archive libomp.a:
137 } elsif ( $line =~ m{^(?:.*?):\s*file format (.*?)\s*$} ) {
138 # libomp.so: file format elf64-x86-64-freebsd
139 # kmp_ftn_cdecl.o: file format elf64-x86-64
141 if ( bad_fmt
( $fmt ) ) {
142 runtime_error
( "Invalid file format: $fmt." );
144 } elsif ( $line =~ m{^Disassembly of section (.*?):\s*$} ) {
145 # Disassembly of section .plt:
146 } elsif ( $line =~ m{^$hex+ <([^>]+)>:\s*$} ) {
147 # 0000000000017e98 <__kmp_str_format@plt-0x10>:
149 } elsif ( $line =~ m{^\s*\.{3}\s
*$} ) {
150 } elsif ( $line =~ m{^\s*($hex+):\s+($hex$hex(?: $hex$hex)*)\s+(?:lock\s+|rex[.a-z]*\s+)?([^ ]+)(?:\s+([^#]+?))?\s*(?:#|$)} ) {
151 # 17e98: ff 35 fa 7d 26 00 pushq 0x267dfa(%rip) # 27fc98 <_GLOBAL_OFFSET_TABLE>
152 my ( $addr, $dump, $instr, $args ) = ( $1, $2, $3, $4 );
153 # Check this is not a bad instruction and xmm registers are not used.
154 if ( bad_instr
( $instr, $args ) ) {
155 if ( $errors == 0 ) {
156 warning
( "Invalid instructions found in `$file':" );
158 if ( $current_func ne $reported_func ) {
159 warning
( " $current_func" );
160 $reported_func = $current_func;
163 if ( $show_instructions ) {
166 if ( $errors >= $max_instructions ) {
167 info
( "$errors invalid instructions found; scanning stopped." );
172 runtime_error
( "Error parsing objdump output line $n:\n>>>> $line\n" );
180 # --------------------------------------------------------------------------------------------------
182 # Parse command line.
183 my $max_instructions;
184 my $show_instructions;
186 "os=s" => \
$target_os,
187 "arch=s" => \
$target_arch,
188 "mic-arch=s" => \
$target_mic_arch,
189 "max-instructions=i" => \
$max_instructions,
190 "show-instructions!" => \
$show_instructions,
192 my $target_platform = $target_os . "_" . $target_arch;
193 if ( "$target_os" eq "lin" and "$target_mic_arch" eq "knf" ) {
194 $mic_bad_re = qr{^(?:pause|[slm]fence|scatter|gather|cmpxchg16b|clevict[12])}i;
196 $mic_bad_re = qr{^(?:pause|[slm]fence|scatter|gather|cmov|cmpxchg16b|clevict[12])}i;
199 } elsif ( $target_platform eq "lin_mic" ) {
200 *bad_instr
= \
*bad_mic_instr
;
201 *bad_fmt
= \
*bad_mic_fmt
;
202 } elsif ( $target_platform eq "lin_32" ) {
203 *bad_instr
= \
*bad_ia32_instr
;
204 *bad_fmt
= \
*bad_ia32_fmt
;
206 runtime_error
( "Only works on lin_32 and lin_mic platforms." );
212 info
( "No arguments specified -- nothing to do." );
214 foreach my $arg ( @ARGV ) {
215 my $errs = check_file
( $arg, $show_instructions, $max_instructions );
230 B<check-instruction-set.pl> -- Make sure binary file does not contain undesired instructions.
234 B<check-instructions.pl> I<option>... I<file>...
240 =item B<--architecture=>I<arch>
242 Specify target architecture.
244 =item B<--max-instructions=>I<number>
246 Stop scanning if I<number> invalid instructions found. 100 by default.
252 =item B<-->[B<no->]B<show-instructions>
254 Show invalid instructions found in the file. Bu default, instructions are not shown.
256 =item Standard Options
264 Print full help message and exit.
268 Print short help message and exit.
272 Print very short usage message and exit.
276 Do print informational messages.
280 Print program version and exit.
284 Work quiet, do not print informational messages.
296 File (object file or library, either static or dynamic) to check.
302 The script runs F<objdump> utility to get disassembler listing and checks the file does not contain
303 unwanted instructions.
305 Currently the script works only for:
311 Intel(R) Many Integrated Core Architecture target OS. Undesired unstructions are: all x87 instructions and some others.
315 Undesired instructions are instructions not valid for Pentium 3 processor (SSE2 and newer).