Skip to content

Commit 3134703

Browse files
Lab 5 edits.
1 parent a540398 commit 3134703

File tree

11 files changed

+134
-40
lines changed

11 files changed

+134
-40
lines changed

lab5/include/DivZeroAnalysis.h

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ struct DivZeroAnalysis : public DataflowAnalysis {
1313

1414
void doAnalysis(Function &F) override;
1515

16+
void flowIn(Instruction *I, Memory *In);
17+
18+
void flowOut(Instruction *I, Memory *In, Memory *NOut, SetVector <Instruction *> &);
19+
1620
bool check(Instruction *I) override;
1721

1822
std::string getAnalysisName() override { return "DivZero"; }

lab5/include/Domain.h

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#ifndef DOMAIN_H
22
#define DOMAIN_H
33

4+
#include "llvm/Support/raw_ostream.h"
5+
6+
using namespace llvm;
7+
48
namespace dataflow {
59

610
//===----------------------------------------------------------------------===//
@@ -11,8 +15,23 @@ namespace dataflow {
1115
* Implement your abstract domain.
1216
*/
1317
class Domain {
14-
/* Add your code here */
18+
public:
19+
enum Element { Uninit, NonZero, Zero, MaybeZero };
20+
Domain();
21+
Domain(Element V);
22+
Element Value;
23+
24+
static Domain *add(Domain *E1, Domain *E2);
25+
static Domain *sub(Domain *E1, Domain *E2);
26+
static Domain *mul(Domain *E1, Domain *E2);
27+
static Domain *div(Domain *E1, Domain *E2);
28+
static Domain *join(Domain *E1, Domain *E2);
29+
static bool order(Domain E1, Domain E2);
30+
void print(raw_ostream &O);
1531
};
32+
33+
raw_ostream &operator<<(raw_ostream &O, Domain V);
34+
1635
} // namespace dataflow
1736

1837
#endif // DOMAIN_H

lab5/include/RefDomain.h

-30
This file was deleted.

lab5/reference/libRefDomain.so

72 Bytes
Binary file not shown.

lab5/src/DivZeroAnalysis.cpp

+34-4
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,46 @@ namespace dataflow {
88

99
/**
1010
* Implement your data-flow analysis.
11-
* 1. Define "transfer" that computes the semantics of each instruction.
12-
* 2. Define "doAnalysis" that stores your results in "InMap" and "OutMap".
13-
* 3. Define "check" that checks if a given instruction is erroneous or not.
11+
* 1. Define "join" that joins the memory state of two flows.
12+
* 2. Define "order" which states if two memory sets are ordered
13+
* 3. Define "flowIn" that joins the memory set of all incomming flows
14+
* 4. Define "transfer" that computes the semantics of each instruction.
15+
* 5. Define "flowOut" that flows the memory set to all outgoing flows
16+
* 6. Define "doAnalysis" that stores your results in "InMap" and "OutMap".
17+
* 7. Define "check" that checks if a given instruction is erroneous or not.
1418
*/
1519

20+
21+
void join(Memory *Result, Memory *M1, Memory *M2) {
22+
/* Add your code here */
23+
}
24+
25+
bool order(Memory *M1, Memory *M2) {
26+
/* Add your code here */
27+
}
28+
29+
30+
void DivZeroAnalysis::flowIn(Instruction *I, Memory *In) {
31+
/* Add your code here */
32+
}
33+
1634
void DivZeroAnalysis::transfer(Instruction *I, const Memory *In, Memory *NOut) {
1735
/* Add your code here */
1836
}
1937

20-
void DivZeroAnalysis::doAnalysis(Function &F) { /* Add your code here */ }
38+
void DivZeroAnalysis::flowOut(Instruction *I, Memory *Pre, Memory *Post, SetVector <Instruction *> &WorkSet) {
39+
/* Add your code here */
40+
}
41+
42+
void DivZeroAnalysis::doAnalysis(Function &F) {
43+
SetVector<Instruction *> WorkSet;
44+
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
45+
WorkSet.insert(&(*I));
46+
}
47+
48+
/* Add your code here */
49+
50+
}
2151

2252
bool DivZeroAnalysis::check(Instruction *I) {
2353
/* Add your code here */

lab5/test/Makefile

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
.PRECIOUS: %.ll
1+
.PRECIOUS: %.ll %.opt.ll
22

3-
all: simple0.out simple1.out branch0.out branch1.out branch2.out loop0.out input0.out
3+
all: simple0.out simple1.out branch0.out branch1.out branch2.out branch3.out branch4.out branch5.out branch6.out loop0.out loop1.out input0.out
44

5-
%.ll: %.c
6-
clang -emit-llvm -S -fno-discard-value-names -c -o $@ $<
5+
%.opt.ll: %.c
6+
clang -emit-llvm -S -fno-discard-value-names -Xclang -disable-O0-optnone -c -o $@ $<
7+
opt -mem2reg -S $@ -o $*.opt.ll
78

8-
%.out: %.ll
9+
%.out: %.opt.ll
910
opt -load ../build/DataflowPass.so -DivZero $< -disable-output > $@ 2> $*.err
1011

1112
clean:

lab5/test/branch3.c

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdio.h>
2+
3+
void f() {
4+
int x = fgetc(stdin);
5+
int y = 2;
6+
if (x > 10) {
7+
y = 0;
8+
}
9+
int z = x / y; // divide-by-zero after branch
10+
}

lab5/test/branch4.c

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stdio.h>
2+
3+
void f() {
4+
int in = fgetc(stdin);
5+
unsigned int a = 10;
6+
unsigned int b = 2;
7+
8+
if(in > 0){
9+
b = 0;
10+
}else if(in == 0){
11+
b = 2 - b;
12+
}else{
13+
b = -2 + b;
14+
}
15+
16+
int out = a / b;
17+
}

lab5/test/branch5.c

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stdio.h>
2+
3+
void f() {
4+
int in = fgetc(stdin);
5+
unsigned int a = 10;
6+
unsigned int b = 2;
7+
8+
if(in > 0){
9+
b = in + b;
10+
}else if(in == 0){
11+
b = 0;
12+
}else{
13+
b = in - b;
14+
}
15+
16+
int out = a / b;
17+
}

lab5/test/branch6.c

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stdio.h>
2+
3+
void f() {
4+
int in = fgetc(stdin);
5+
int a = 10;
6+
int b = 2;
7+
8+
if(in > 0){
9+
b = 100 + b;
10+
}else if(in == 0){
11+
b = 1;
12+
}else{
13+
b = a + b;
14+
}
15+
16+
int out = a / b;
17+
}

lab5/test/loop1.c

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
void f() {
2+
int i;
3+
int sum = 0;
4+
while(sum < 50){
5+
sum += i;
6+
}
7+
int y = sum - 55;
8+
int z = i / y;
9+
}

0 commit comments

Comments
 (0)