I am trying to analyse instruction count of a C++ program using LLVM passes. Issue is the when i use function from standard library like sqrt() from cmath, the LLVM IR does not contain the instructions from sqrt() but instead represents it like
declare double @sqrt(double noundef) #1
This is causing problems from me as I am unable to count the total number of instruction in entire program. Below is my C++ Program
#include <cmath>
#include <iostream>
using namespace std;
int main(){
int num = 10;
float c = (float) std::sqrt((double) num);
return 0;
}
and the LLVM IR
; ModuleID = 'hello.cpp'
source_filename = "hello.cpp"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
module asm ".globl _ZSt21ios_base_library_initv"
; Function Attrs: mustprogress noinline norecurse nounwind uwtable
define dso_local noundef i32 @main() #0 {
%1 = alloca i32, align 4
%2 = alloca i32, align 4
%3 = alloca float, align 4
store i32 0, ptr %1, align 4
store i32 10, ptr %2, align 4
%4 = load i32, ptr %2, align 4
%5 = sitofp i32 %4 to double
%6 = call double @sqrt(double noundef %5) #2
%7 = fptrunc double %6 to float
store float %7, ptr %3, align 4
ret i32 0
}
; Function Attrs: nounwind
declare double @sqrt(double noundef) #1
attributes #0 = { mustprogress noinline norecurse nounwind uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nounwind "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind }
!llvm.module.flags = !{!0, !1, !2, !3, !4}
!llvm.ident = !{!5}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{i32 7, !"frame-pointer", i32 2}
!5 = !{!"Ubuntu clang version 18.1.3 (1)"}
New contributor
tapas1994 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.