Skip to content

Commit 46d913c

Browse files
Updated names and created BranchRange.cpp
1 parent d6b3107 commit 46d913c

File tree

5 files changed

+109
-5
lines changed

5 files changed

+109
-5
lines changed

BranchRange.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "llvm/Pass.h"
2+
#include "llvm/IR/Function.h"
3+
#include "llvm/Support/raw_ostream.h"
4+
5+
#include "llvm/IR/LegacyPassManager.h"
6+
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
7+
8+
using namespace llvm;
9+
10+
namespace
11+
{
12+
struct HppsBranchRange : public FunctionPass
13+
{
14+
static char ID;
15+
HppsBranchRange() : FunctionPass(ID) {}
16+
17+
// Run over a single function
18+
bool runOnFunction(Function &Func) override
19+
{
20+
// Run over all basic blocks in the function
21+
for (BasicBlock &BB : Func)
22+
{
23+
// Print out the name of the basic block if it has one, and then the
24+
// number of instructions that it contains
25+
errs() << "Basic block (name=" << BB.getName() << ") has "
26+
<< BB.size() << " instructions.\n";
27+
28+
// Run over all instructions in the basic block
29+
for (Instruction &I : BB)
30+
{
31+
// The next statement works since operator<<(ostream&,...)
32+
// is overloaded for Instruction&
33+
errs() << I << "\n";
34+
}
35+
}
36+
37+
errs().write_escaped(Func.getName()) << '\n';
38+
return false;
39+
}
40+
}; // end of struct HppsBranchRange
41+
} // end of anonymous namespace
42+
43+
char HppsBranchRange::ID = 0;
44+
static RegisterPass<HppsBranchRange> X("branch-range", "Branch Range Pass",
45+
false /* Only looks at CFG */,
46+
false /* Analysis Pass */);
47+
48+
static RegisterStandardPasses Y(
49+
PassManagerBuilder::EP_EarlyAsPossible,
50+
[](const PassManagerBuilder &Builder,
51+
legacy::PassManagerBase &PM) { PM.add(new HppsBranchRange()); });

ConstantRange.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ using namespace llvm;
1616
namespace
1717
{
1818

19-
struct Hpps : public FunctionPass
19+
struct HppsConstantRange : public FunctionPass
2020
{
2121
static char ID;
22-
Hpps() : FunctionPass(ID) {}
22+
HppsConstantRange() : FunctionPass(ID) {}
2323

2424
// Run over a single function (main)
2525
bool runOnFunction(Function &Func) override
@@ -293,13 +293,13 @@ namespace
293293
}; // namespace
294294
} // end of anonymous namespace
295295

296-
char Hpps::ID = 0;
297-
static RegisterPass<Hpps> X("const-range", "Constant Range Pass",
296+
char HppsConstantRange::ID = 0;
297+
static RegisterPass<HppsConstantRange> X("const-range", "Constant Range Pass",
298298
false /* Only looks at CFG */,
299299
false /* Analysis Pass */);
300300

301301
static RegisterStandardPasses Y(PassManagerBuilder::EP_EarlyAsPossible,
302302
[](const PassManagerBuilder &Builder,
303303
legacy::PassManagerBase &PM) {
304-
PM.add(new Hpps());
304+
PM.add(new HppsConstantRange());
305305
});

src/bra/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Generate SSA from using -mem2reg
2+
- Create file `bra.c` in directory **~/Public/project/code**
3+
- Open directory **~/Public/project/llvm-project/build/bin**
4+
- Run command `./clang -c -O0 -emit-llvm ../../../code/bra.c -o bra.bc -Xclang -disable-O0-optnone` to generate `bra.bc` (bitecode)
5+
- Run command `./llvm-dis bra.bc -o=bra.ll` to generate `bra.ll` (IR)
6+
- Run command `./opt -mem2reg -S bra.ll -o bra-opt.ll` to generate SSA form using `-mem2reg` (Remove instructions and add PHI nodes1)

src/bra/bra/bra-opt.ll

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
; ModuleID = 'bra.ll'
2+
source_filename = "../../../code/bra.c"
3+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
4+
target triple = "x86_64-unknown-linux-gnu"
5+
6+
; Function Attrs: noinline nounwind uwtable
7+
define dso_local i32 @main() #0 {
8+
entry:
9+
%cmp = icmp slt i32 0, 100
10+
br i1 %cmp, label %if.then, label %if.else
11+
12+
if.then: ; preds = %entry
13+
%add = add nsw i32 0, 1
14+
br label %if.end
15+
16+
if.else: ; preds = %entry
17+
%sub = sub nsw i32 0, 1
18+
br label %if.end
19+
20+
if.end: ; preds = %if.else, %if.then
21+
%k.0 = phi i32 [ %add, %if.then ], [ %sub, %if.else ]
22+
ret i32 %k.0
23+
}
24+
25+
attributes #0 = { noinline nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
26+
27+
!llvm.module.flags = !{!0}
28+
!llvm.ident = !{!1}
29+
30+
!0 = !{i32 1, !"wchar_size", i32 4}
31+
!1 = !{!"clang version 7.1.0 "}

src/bra/bra/bra.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
int k = 0;
5+
int j = k;
6+
7+
if (k < 100) {
8+
k = k + 1;
9+
j = k;
10+
} else {
11+
j = j - 1;
12+
k = j;
13+
}
14+
15+
return k;
16+
}

0 commit comments

Comments
 (0)