I'm following the link Parsing and Modifying LLVM IR code to read in IR file and trying to parse it. But I found no matter what input file I wrote in argument(.ll or .bc), it just won't parse file and save it into the variable.
Here's my code,
#include <iostream>
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Function.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/IRReader.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
int main(int argc, char** argv)
{
if (argc < 2) {
errs() << "Expected an argument - IR file name\n";
exit(1);
}
LLVMContext &Context = getGlobalContext();
SMDiagnostic Err;
Module *Mod = ParseIRFile(argv[1], Err, Context);
if (Mod) {
std::cout << "Mod is not null" << std::endl;
}
else {
std::cout << "Mod is null" << std::endl;
}
return 0;
}
By running this code with either ll or bc file it always shows Mod is null.
Can someone give me some hints on how to reslove this issue?
Here is my IR code
; ModuleID = 'bubble.bc'
target datalayout = "e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.9.0"
#Sort = global [8 x i32] [i32 1, i32 4, i32 2, i32 5, i32 7, i32 3, i32 6, i32 3], align 16
#.str = private unnamed_addr constant [4 x i8] c"%d \00", align 1
#.str1 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
; Function Attrs: nounwind ssp uwtable
define void #bubble() #0 {
%tmp = alloca i32, align 4
%i = alloca i32, align 4
%j = alloca i32, align 4
store i32 0, i32* %i, align 4
br label %1
; <label>:1 ; preds = %41, %0
%2 = load i32* %i, align 4
%3 = icmp slt i32 %2, 8
br i1 %3, label %4, label %44
; <label>:4 ; preds = %1
%5 = load i32* %i, align 4
%6 = add nsw i32 %5, 1
store i32 %6, i32* %j, align 4
br label %7
; <label>:7 ; preds = %37, %4
%8 = load i32* %j, align 4
%9 = icmp slt i32 %8, 8
br i1 %9, label %10, label %40
; <label>:10 ; preds = %7
%11 = load i32* %i, align 4
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [8 x i32]* #Sort, i32 0, i64 %12
%14 = load i32* %13, align 4
%15 = load i32* %j, align 4
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [8 x i32]* #Sort, i32 0, i64 %16
%18 = load i32* %17, align 4
%19 = icmp sge i32 %14, %18
br i1 %19, label %20, label %36
//some similar stuff
!0 = metadata !{metadata !"Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)"}
Error meg is ./IRparser: /home/mingaoIrparser/testcase/bubble.ll:10:23: error: expected '{' in function body
define void #bubble() #0 {
^
One more thing is that this IR is compiled from LLVM3.4 the library I'm using is 2.9 for now. Will it matter?
This repository has a bunch of up-to-date samples of using LLVM and Clang as libraries. For example, this sample has what you need (as well as other samples):
int main(int argc, char **argv) {
if (argc < 2) {
errs() << "Usage: " << argv[0] << " <IR file>\n";
return 1;
}
// Parse the input LLVM IR file into a module.
SMDiagnostic Err;
Module *Mod = ParseIRFile(argv[1], Err, getGlobalContext());
if (!Mod) {
Err.print(argv[0], errs());
return 1;
}
// ... use module
}
Related
I am new to llvm framework and was able to run a basic pass to iterate over instructions in a simple IR function with only entry basic block, but to expand upon that I got an .ll file from clang for a simple c function ( don't mind the correctness of the function I don't care about it for the sake of learning llvm at least for now ).
// fact.c
int fact(int n){
int t =1;
for(int i = 2;i<=n;i++){
t = t*i;
}
return t;
}
I was able to get a fact.ll file for this function, given below, and there are 3 functions in the fact.ll which I hand coded into the IR. foo , add and bar. And I attempt to run a simple pass which will iterate over each BasicBlock and gather it's inst opcodes and simply print them at the end, My issue is the opt tool is able to do it for foo, add and bar functions but not for the fact function.
Pass file :
#include "llvm/Transforms/Utils/MyHello.h"
#include <string>
using namespace llvm;
PreservedAnalyses MyHelloPass::run(Function &F,FunctionAnalysisManager &AM) {
std::string output;
errs()<<F.getName()<<"\n";
for(Function::iterator BB = F.begin();BB!=F.end();BB++){
for(BasicBlock::iterator I = BB->begin();I!=BB->end();I++){
output+=(I->getOpcodeName());
output+='\n';
}
}
errs()<<output<<'\n';
return PreservedAnalyses::all();
}
fact.ll
; ModuleID = 'fact.c'
source_filename = "fact.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 #fact(i32 noundef %n) #0 {
entry:
%n.addr = alloca i32, align 4
%t = alloca i32, align 4
%i = alloca i32, align 4
store i32 %n, i32* %n.addr, align 4
store i32 1, i32* %t, align 4
store i32 2, i32* %i, align 4
br label %for.cond
for.cond: ; preds = %for.inc, %entry
%0 = load i32, i32* %i, align 4
%1 = load i32, i32* %n.addr, align 4
%cmp = icmp sle i32 %0, %1
br i1 %cmp, label %for.body, label %for.end
for.body: ; preds = %for.cond
%2 = load i32, i32* %t, align 4
%3 = load i32, i32* %i, align 4
%mul = mul nsw i32 %2, %3
store i32 %mul, i32* %t, align 4
br label %for.inc
for.inc: ; preds = %for.body
%4 = load i32, i32* %i, align 4
%inc = add nsw i32 %4, 1
store i32 %inc, i32* %i, align 4
br label %for.cond, !llvm.loop !6
for.end: ; preds = %for.cond
%5 = load i32, i32* %t, align 4
ret i32 %5
}
define i32 #foo(){
%a = add i32 2,3
ret i32 %a
}
define i32 #add(i32 %a,i32 %b){
%c = add i32 %a,%b
%d = add i32 %c,%c
%e = sub i32 %c, %d
%f = mul i32 %d, %e
ret i32 %f
}
define void #bar(){
ret void
}
attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
!llvm.module.flags = !{!0, !1, !2, !3, !4}
!llvm.ident = !{!5}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"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 = !{!"AMD\C2\A0\C2\A0-DCLANG_REPOSITORY_STRING=CLANG: clang version 15.0.0 (CLANG: Jenkins CPUPC_Mirror_To_Staging_Merge-Build#892) (based on LLVM Mirror.Version.14.0.0)"}
!6 = distinct !{!6, !7}
!7 = !{!"llvm.loop.mustprogress"}
run command : opt -disable-output fact.ll -passes="myhello"
Ouput :
foo
add
ret
add
add
add
sub
mul
ret
bar
ret
See the optnone in:
; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 #fact(i32 noundef %n) #0 {
That means that this function is opting out of optimizations, hence your pass will not be run on that function.
You can manually remove the optnone from the definition of #0 at the bottom (note: the ; Function Attrs: ... line is merely a comment, changing it has no effect) or you can build your LLVM IR with "clang -O2". You may want to also add -mllvm -disable-llvm-optzns if you want clang to produce IR that could be optimized but hasn't been run through LLVM passes.
I am trying to perform -O2 optimisation with LLVM IR obtained by calling CLANG API. Unfortunately, optimisation works only with IR created with manual calls. I have the following function:
int mult_add(int x, int y){
if(x > 2){
return y + 1 + 2;
} else {
return y - 1 + 2;
}
}
And with these calls:
clang -S -emit-llvm main.cpp
opt main.ll -o opt.ll -S -O2
I get the correct result:
define i32 #_Z8mult_addii(i32, i32) local_unnamed_addr #0 {
%3 = icmp sgt i32 %0, 2
%.sink = select i1 %3, i32 3, i32 1
%4 = add nsw i32 %.sink, %1
ret i32 %4
}
Unfortunately, when I do it through LLVM API with legacy::PassManager and legacy::FunctionPassManager optimisation simply does not work and got long ugly code:
define i32 #_Z8mult_addii(i32, i32) #0 {
%3 = alloca i32, align 4
%4 = alloca i32, align 4
%5 = alloca i32, align 4
store i32 %0, i32* %4, align 4
store i32 %1, i32* %5, align 4
%6 = load i32, i32* %4, align 4
%7 = icmp sgt i32 %6, 2
br i1 %7, label %8, label %12
; <label>:8: ; preds = %2
%9 = load i32, i32* %5, align 4
%10 = add nsw i32 %9, 1
%11 = add nsw i32 %10, 2
store i32 %11, i32* %3, align 4
br label %16
; <label>:12: ; preds = %2
%13 = load i32, i32* %5, align 4
%14 = sub nsw i32 %13, 1
%15 = add nsw i32 %14, 2
store i32 %15, i32* %3, align 4
br label %16
; <label>:16: ; preds = %12, %8
%17 = load i32, i32* %3, align 4
ret i32 %17
}
Seems like CLANG creates IR in some unoptimisable state? Because running the passes on a manual created IR works fine.
By the way, PMBuilder.populateModulePassManager is called, here is the code:
legacy::PassManager Passes;
legacy::FunctionPassManager FPasses(M2.get());
AddOptimizationPasses(Passes, FPasses, &(TheJIT->getTargetMachine()), 2, 0);
Passes.add(createPrintModulePass(outs()));
Passes.run(*M2);
And AddOptimizationPasses is stolen and simplified from opt utility:
static void AddOptimizationPasses(legacy::PassManagerBase &MPM,
legacy::FunctionPassManager &FPM,
TargetMachine *TM, unsigned OptLevel,
unsigned SizeLevel) {
FPM.add(createVerifierPass());
PassManagerBuilder Builder;
Builder.OptLevel = OptLevel;
Builder.SizeLevel = SizeLevel;
Builder.Inliner = createFunctionInliningPass(50);
Builder.DisableUnitAtATime = true;//!UnitAtATime;
Builder.DisableUnrollLoops = false;
if (TM)
TM->adjustPassManager(Builder);
//Builder.populateFunctionPassManager(FPM);
Builder.populateModulePassManager(MPM);
}
By the way, initialisation is following:
InitializeAllTargets();
InitializeAllTargetMCs();
InitializeAllAsmPrinters();
Unfortunately, it does not work.
Did you forget to populate the pass manager?
PassManagerBase& PM = ...; // create the pass manager.
PassManagerBuilder PMBuilder;
PMBuilder.OptLevel = 2;
PMBuilder.DisableUnrollLoops = false;
PMBuilder.Inliner = createFunctionInliningPass(50);
PMBuilder.populateModulePassManager(PM);
Module& = ...; // your IR module here
PM.run(M);
Note that a "FunctionPassManager" may not do what you need. You're likely looking for legacy::PassManager instead (which can hold any type of pass).
The DataFlowSanitizer pass on LLVM 3.8.0, 64 bit (Ubuntu 16.04.2) generates the following IR from source:
The source:
test.c
#include <sanitizer/dfsan_interface.h>
int main(void) {
int i = 1;
dfsan_label i_label = dfsan_create_label("i", 0);
dfsan_set_label(i_label, &i, sizeof(i));
return 0;
}
The commands to generate the IR:
clang -c -emit-llvm -fsanitize=dataflow test.c -o test.bc
llvm-dis test.bc
The disassembly:
test.ll
; Function Attrs: nounwind uwtable
define i32 #main() #0 {
entry:
%0 = alloca i16
%retval = alloca i32, align 4
%i = alloca i32, align 4
%1 = alloca i16
%i_label = alloca i16, align 2
store i16 0, i16* %0
store i32 0, i32* %retval, align 4
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%2 = ptrtoint i32* %i to i64
%3 = and i64 %2, -123145302310913
%4 = mul i64 %3, 2
%5 = inttoptr i64 %4 to i16*
%6 = bitcast i16* %5 to i64*
store i64 0, i64* %6, align 2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
store i32 1, i32* %i, align 4
%call = call zeroext i16 #dfsan_create_label(i8* getelementptr inbounds ([2 x i8], [2 x i8]* #.str, i32 0, i32 0), i8* null)
store i16 0, i16* %1
store i16 %call, i16* %i_label, align 2
%7 = load i16, i16* %1
%8 = load i16, i16* %i_label, align 2
%9 = bitcast i32* %i to i8*
call void #dfsan_set_label(i16 zeroext %8, i8* %9, i64 4)
ret i32 0
}
I don't understand why the block of instruction I separated out is being generated. Looking at the Transform/Instrumentation/DataFlowsanitizer.cpp, I can't find the code that inserts the instrumentation above. Can anyone explain this behavior?
When I run a program in C++, it runs slower than the identical program called by MATLAB using Mex functions.
I tried some sample code to test this, which confirmed my suspicion:
Using C++:
#include <stdio.h>
#include <ctime>
void process(int a[10000], int b[10000]) {
const int dim[2] = {1, 10000};
int barData[20000];
clock_t begin = clock();
for (int i = 0; i < dim[1]; i++) {
for (int j = 0; j < i; j++) {
barData[j] = a[i];
barData[j] = b[i];
}
}
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
printf("%f\n", elapsed_secs);
}
int main() {
int a[10000], b[10000];
process(a,b);
return 0;
}
Using Mex functions:
#include <stdio.h>
#include "mex.h"
void process(const mxArray *first, const mxArray *second) {
int* a = (int *)mxGetData(first);
int* b = (int *)mxGetData(second);
const int *dim = mxGetDimensions(first);
const int dims[2] = {1,dim[1]*2};
mxArray* bar = mxCreateNumericArray(2, dims, mxINT64_CLASS, mxREAL);
int* barData = (int*)mxGetData(bar);
clock_t begin = clock();
for (int i = 0; i < dim[1]; i++) {
for (int j = 0; j < i; j++) {
barData[j] = a[i];
barData[j] = b[i];
}
}
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
printf("%f\n", elapsed_secs);
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
process(prhs[0], prhs[1]);
}
And calling it from MATLAB as follows:
mex test.cpp -output foo
foo(rand(1,10000), rand(1,10000))
Mex function gives ~0.012s while C++ code gives 0.108s. The trends scale for larger array sizes too. Why is this, and is there a way to make the C++ code run with the Mex function speed?
As #Praetorian states in a comment above, you are probably not doing optimization on the C++ code.
Here is what the LLVMIR (pseudo-assembly) of your code is without optimization:
; ModuleID = 'test.cpp'
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
#_ZZ7processPiS_E3dim = internal constant [2 x i32] [i32 1, i32 10000], align 4
#.str = private unnamed_addr constant [4 x i8] c"%f\0A\00", align 1
; Function Attrs: uwtable
define void #_Z7processPiS_(i32* %a, i32* %b) #0 {
%1 = alloca i32*, align 8
%2 = alloca i32*, align 8
%barData = alloca [20000 x i32], align 16
%begin = alloca i64, align 8
%i = alloca i32, align 4
%j = alloca i32, align 4
%end = alloca i64, align 8
%elapsed_secs = alloca double, align 8
store i32* %a, i32** %1, align 8
store i32* %b, i32** %2, align 8
%3 = call i64 #clock() #3
store i64 %3, i64* %begin, align 8
store i32 0, i32* %i, align 4
br label %4
; <label>:4 ; preds = %34, %0
%5 = load i32* %i, align 4
%6 = load i32* getelementptr inbounds ([2 x i32]* #_ZZ7processPiS_E3dim, i32 0, i64 1), align 4
%7 = icmp slt i32 %5, %6
br i1 %7, label %8, label %37
; <label>:8 ; preds = %4
store i32 0, i32* %j, align 4
br label %9
; <label>:9 ; preds = %30, %8
%10 = load i32* %j, align 4
%11 = load i32* %i, align 4
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %33
; <label>:13 ; preds = %9
%14 = load i32* %i, align 4
%15 = sext i32 %14 to i64
%16 = load i32** %1, align 8
%17 = getelementptr inbounds i32* %16, i64 %15
%18 = load i32* %17, align 4
%19 = load i32* %j, align 4
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [20000 x i32]* %barData, i32 0, i64 %20
store i32 %18, i32* %21, align 4
%22 = load i32* %i, align 4
%23 = sext i32 %22 to i64
%24 = load i32** %2, align 8
%25 = getelementptr inbounds i32* %24, i64 %23
%26 = load i32* %25, align 4
%27 = load i32* %j, align 4
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [20000 x i32]* %barData, i32 0, i64 %28
store i32 %26, i32* %29, align 4
br label %30
; <label>:30 ; preds = %13
%31 = load i32* %j, align 4
%32 = add nsw i32 %31, 1
store i32 %32, i32* %j, align 4
br label %9
; <label>:33 ; preds = %9
br label %34
; <label>:34 ; preds = %33
%35 = load i32* %i, align 4
%36 = add nsw i32 %35, 1
store i32 %36, i32* %i, align 4
br label %4
; <label>:37 ; preds = %4
%38 = call i64 #clock() #3
store i64 %38, i64* %end, align 8
%39 = load i64* %end, align 8
%40 = load i64* %begin, align 8
%41 = sub nsw i64 %39, %40
%42 = sitofp i64 %41 to double
%43 = fdiv double %42, 1.000000e+06
store double %43, double* %elapsed_secs, align 8
%44 = load double* %elapsed_secs, align 8
%45 = call i32 (i8*, ...)* #printf(i8* getelementptr inbounds ([4 x i8]* #.str, i32 0, i32 0), double %44)
ret void
}
; Function Attrs: nounwind
declare i64 #clock() #1
declare i32 #printf(i8*, ...) #2
; Function Attrs: uwtable
define i32 #main() #0 {
%1 = alloca i32, align 4
%a = alloca [10000 x i32], align 16
%b = alloca [10000 x i32], align 16
store i32 0, i32* %1
%2 = getelementptr inbounds [10000 x i32]* %a, i32 0, i32 0
%3 = getelementptr inbounds [10000 x i32]* %b, i32 0, i32 0
call void #_Z7processPiS_(i32* %2, i32* %3)
ret i32 0
}
attributes #0 = { uwtable "less-precise-fpmad"="false" "no-frame- pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp- math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { nounwind "less-precise-fpmad"="false" "no-frame- pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp- math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #2 = { "less-precise-fpmad"="false" "no-frame-pointer- elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no- nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp- math"="false" "use-soft-float"="false" }
attributes #3 = { nounwind }
!llvm.ident = !{!0}
!0 = !{!"clang version 3.6.2 (tags/RELEASE_362/final)"}
Notice that Z7processPiS is very long.
Here is with optimization -O3 (which is generally safe in C++ nowadays):
; ModuleID = 'test.cpp'
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
#.str = private unnamed_addr constant [4 x i8] c"%f\0A\00", align 1
; Function Attrs: nounwind uwtable
define void #_Z7processPiS_(i32* nocapture readnone %a, i32* nocapture readnone %b) #0 {
%1 = tail call i64 #clock() #2
%2 = tail call i64 #clock() #2
%3 = sub nsw i64 %2, %1
%4 = sitofp i64 %3 to double
%5 = fdiv double %4, 1.000000e+06
%6 = tail call i32 (i8*, ...)* #printf(i8* getelementptr inbounds ([4 x i8]* #.str, i64 0, i64 0), double %5)
ret void
}
; Function Attrs: nounwind
declare i64 #clock() #1
; Function Attrs: nounwind
declare i32 #printf(i8* nocapture readonly, ...) #1
; Function Attrs: nounwind uwtable
define i32 #main() #0 {
%1 = tail call i64 #clock() #2
%2 = tail call i64 #clock() #2
%3 = sub nsw i64 %2, %1
%4 = sitofp i64 %3 to double
%5 = fdiv double %4, 1.000000e+06
%6 = tail call i32 (i8*, ...)* #printf(i8* getelementptr inbounds ([4 x i8]* #.str, i64 0, i64 0), double %5) #2
ret i32 0
}
attributes #0 = { nounwind uwtable "less-precise-fpmad"="false" "no-frame- pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft- float"="false" }
attributes #1 = { nounwind "less-precise-fpmad"="false" "no-frame-pointer- elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack- protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #2 = { nounwind }
!llvm.ident = !{!0}
!0 = !{!"clang version 3.6.2 (tags/RELEASE_362/final)"}
P.S.: It would be more idiomatic to write:
#include <iostream>
#include <vector>
#include <ctime>
using std::vector;
void process(vector<int> a, vector<int> b) {
const pair<int,int> dim = {1, 10000};
vector<int> barData(20000,0);
clock_t begin = clock();
for (int i = 0; i < dim.second; i++) {
for (int j = 0; j < i; j++) {
barData[j] = a[i];
barData[j] = b[i];
}
}
clock_t end = clock();
std::cout << double(end-begin)/CLOCKS_PER_SEC << '\n';
}
int main() {
vector<int> a(10000, 0), b(10000,0);
process(a,b);
return 0;
}
I have a c code that calculates the factorial of an int "factorial.c". I compile it to llvm readable code "factorial.ll" and I modify in the compiled llvm code.
The objective is to execute the modified llvm code and to see its output, How can I do this?
It will depend on how your outputted LLVM is assembled and what libraries it links against, but for example executing the following factorial.ll with the shell command lli
$ lli factorial.ll
Factorial of 10 = 3628800
Will execute the main function with the JIT and use the standard printf to output the result to stdout.
#.str = private unnamed_addr constant [22 x i8] c"Factorial of %d = %d\0A\00", align 1
declare i32 #printf(i8*, ...)
define i32 #factorial(i32 %n) nounwind uwtable {
entry:
%n.addr = alloca i32, align 4
store i32 %n, i32* %n.addr, align 4
%0 = load i32* %n.addr, align 4
%cmp = icmp sle i32 %0, 1
br i1 %cmp, label %cond.true, label %cond.false
cond.true: ; preds = %entry
br label %cond.end
cond.false: ; preds = %entry
%1 = load i32* %n.addr, align 4
%2 = load i32* %n.addr, align 4
%sub = sub nsw i32 %2, 1
%call = call i32 #factorial(i32 %sub)
%mul = mul nsw i32 %1, %call
br label %cond.end
cond.end: ; preds = %cond.false, %cond.true
%cond = phi i32 [ 1, %cond.true ], [ %mul, %cond.false ]
ret i32 %cond
}
define i32 #main(i32 %argc, i8** %argv) nounwind uwtable {
entry:
%retval = alloca i32, align 4
%argc.addr = alloca i32, align 4
%argv.addr = alloca i8**, align 8
store i32 0, i32* %retval
store i32 %argc, i32* %argc.addr, align 4
store i8** %argv, i8*** %argv.addr, align 8
%call = call i32 #factorial(i32 10)
%call1 = call i32 (i8*, ...)* #printf(i8* getelementptr inbounds ([22 x i8]* #.str, i32 0, i32 0), i32 10, i32 %call)
ret i32 0
}