Skip to content

Commit

Permalink
Phi node fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ruoran committed Dec 10, 2013
1 parent 2c59aa3 commit 8052063
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 140 deletions.
188 changes: 66 additions & 122 deletions example/ppt_example.splited.ll
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ target triple = "x86_64-unknown-linux-gnu"
; Function Attrs: nounwind uwtable
define i32 @main() #0 {
entry:
%retval = alloca i32, align 4
%a = alloca [5 x i32], align 16
%b = alloca [5 x i32], align 16
%c = alloca [5 x i32], align 16
%d = alloca [5 x i32], align 16
%i = alloca i32, align 4
store i32 0, i32* %retval
%0 = bitcast [5 x i32]* %a to i8*
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %0, i8* bitcast ([5 x i32]* @main.a to i8*), i64 20, i32 16, i1 false)
%1 = bitcast [5 x i32]* %b to i8*
Expand All @@ -23,162 +20,109 @@ entry:
call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 20, i32 16, i1 false)
%3 = bitcast [5 x i32]* %d to i8*
call void @llvm.memset.p0i8.i64(i8* %3, i8 0, i64 20, i32 16, i1 false)
store i32 1, i32* %i, align 4
br label %for.cond

for.cond: ; preds = %for.inc, %entry
%4 = load i32* %i, align 4
%cmp = icmp slt i32 %4, 5
br i1 %cmp, label %for.body, label %for.cond.copied
%indvars.iv1 = phi i64 [ %indvars.iv.next2, %for.inc ], [ 1, %entry ]
%lftr.wideiv5 = trunc i64 %indvars.iv1 to i32
%exitcond6 = icmp ne i32 %lftr.wideiv5, 5
br i1 %exitcond6, label %for.body, label %for.cond.copied

for.body: ; preds = %for.cond
%5 = load i32* %i, align 4
%idxprom = sext i32 %5 to i64
%arrayidx = getelementptr inbounds [5 x i32]* %a, i32 0, i64 %idxprom
%6 = load i32* %arrayidx, align 4
%7 = load i32* %i, align 4
%sub = sub nsw i32 %7, 1
%idxprom1 = sext i32 %sub to i64
%arrayidx2 = getelementptr inbounds [5 x i32]* %b, i32 0, i64 %idxprom1
%8 = load i32* %arrayidx2, align 4
%add = add nsw i32 %6, %8
%9 = load i32* %i, align 4
%idxprom3 = sext i32 %9 to i64
%arrayidx4 = getelementptr inbounds [5 x i32]* %a, i32 0, i64 %idxprom3
%arrayidx = getelementptr inbounds [5 x i32]* %a, i64 0, i64 %indvars.iv1
%4 = load i32* %arrayidx, align 4
%5 = add nsw i64 %indvars.iv1, -1
%arrayidx2 = getelementptr inbounds [5 x i32]* %b, i64 0, i64 %5
%6 = load i32* %arrayidx2, align 4
%add = add nsw i32 %4, %6
%arrayidx4 = getelementptr inbounds [5 x i32]* %a, i64 0, i64 %indvars.iv1
store i32 %add, i32* %arrayidx4, align 4
%10 = load i32* %i, align 4
%sub5 = sub nsw i32 %10, 1
%idxprom6 = sext i32 %sub5 to i64
%arrayidx7 = getelementptr inbounds [5 x i32]* %c, i32 0, i64 %idxprom6
%11 = load i32* %arrayidx7, align 4
%add8 = add nsw i32 %11, 5
%12 = load i32* %i, align 4
%idxprom9 = sext i32 %12 to i64
%arrayidx10 = getelementptr inbounds [5 x i32]* %b, i32 0, i64 %idxprom9
%7 = add nsw i64 %indvars.iv1, -1
%arrayidx7 = getelementptr inbounds [5 x i32]* %c, i64 0, i64 %7
%8 = load i32* %arrayidx7, align 4
%add8 = add nsw i32 %8, 5
%arrayidx10 = getelementptr inbounds [5 x i32]* %b, i64 0, i64 %indvars.iv1
store i32 %add8, i32* %arrayidx10, align 4
%13 = load i32* %i, align 4
%idxprom11 = sext i32 %13 to i64
%arrayidx12 = getelementptr inbounds [5 x i32]* %b, i32 0, i64 %idxprom11
%14 = load i32* %arrayidx12, align 4
%mul = mul nsw i32 %14, 2
%15 = load i32* %i, align 4
%idxprom13 = sext i32 %15 to i64
%arrayidx14 = getelementptr inbounds [5 x i32]* %c, i32 0, i64 %idxprom13
%arrayidx12 = getelementptr inbounds [5 x i32]* %b, i64 0, i64 %indvars.iv1
%9 = load i32* %arrayidx12, align 4
%mul = shl nsw i32 %9, 1
%arrayidx14 = getelementptr inbounds [5 x i32]* %c, i64 0, i64 %indvars.iv1
store i32 %mul, i32* %arrayidx14, align 4
%16 = load i32* %i, align 4
%idxprom15 = sext i32 %16 to i64
%arrayidx16 = getelementptr inbounds [5 x i32]* %c, i32 0, i64 %idxprom15
%17 = load i32* %arrayidx16, align 4
%add17 = add nsw i32 %17, 1
%18 = load i32* %i, align 4
%idxprom18 = sext i32 %18 to i64
%arrayidx19 = getelementptr inbounds [5 x i32]* %d, i32 0, i64 %idxprom18
%arrayidx16 = getelementptr inbounds [5 x i32]* %c, i64 0, i64 %indvars.iv1
%10 = load i32* %arrayidx16, align 4
%add17 = add nsw i32 %10, 1
%arrayidx19 = getelementptr inbounds [5 x i32]* %d, i64 0, i64 %indvars.iv1
store i32 %add17, i32* %arrayidx19, align 4
br label %for.inc

for.inc: ; preds = %for.body
%19 = load i32* %i, align 4
%inc = add nsw i32 %19, 1
store i32 %inc, i32* %i, align 4
%indvars.iv.next2 = add i64 %indvars.iv1, 1
br label %for.cond

for.end: ; preds = %for.cond.copied
store i32 0, i32* %i, align 4
br label %for.cond20

for.cond20: ; preds = %for.inc31, %for.end
%20 = load i32* %i, align 4
%cmp21 = icmp slt i32 %20, 5
br i1 %cmp21, label %for.body22, label %for.end33
%indvars.iv = phi i64 [ %indvars.iv.next, %for.inc31 ], [ 0, %for.end ]
%lftr.wideiv = trunc i64 %indvars.iv to i32
%exitcond = icmp ne i32 %lftr.wideiv, 5
br i1 %exitcond, label %for.body22, label %for.end33

for.body22: ; preds = %for.cond20
%21 = load i32* %i, align 4
%22 = load i32* %i, align 4
%idxprom23 = sext i32 %22 to i64
%arrayidx24 = getelementptr inbounds [5 x i32]* %a, i32 0, i64 %idxprom23
%23 = load i32* %arrayidx24, align 4
%24 = load i32* %i, align 4
%25 = load i32* %i, align 4
%idxprom25 = sext i32 %25 to i64
%arrayidx26 = getelementptr inbounds [5 x i32]* %b, i32 0, i64 %idxprom25
%26 = load i32* %arrayidx26, align 4
%27 = load i32* %i, align 4
%28 = load i32* %i, align 4
%idxprom27 = sext i32 %28 to i64
%arrayidx28 = getelementptr inbounds [5 x i32]* %c, i32 0, i64 %idxprom27
%29 = load i32* %arrayidx28, align 4
%30 = load i32* %i, align 4
%31 = load i32* %i, align 4
%idxprom29 = sext i32 %31 to i64
%arrayidx30 = getelementptr inbounds [5 x i32]* %d, i32 0, i64 %idxprom29
%32 = load i32* %arrayidx30, align 4
%call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([48 x i8]* @.str, i32 0, i32 0), i32 %21, i32 %23, i32 %24, i32 %26, i32 %27, i32 %29, i32 %30, i32 %32)
%arrayidx24 = getelementptr inbounds [5 x i32]* %a, i64 0, i64 %indvars.iv
%11 = load i32* %arrayidx24, align 4
%arrayidx26 = getelementptr inbounds [5 x i32]* %b, i64 0, i64 %indvars.iv
%12 = load i32* %arrayidx26, align 4
%arrayidx28 = getelementptr inbounds [5 x i32]* %c, i64 0, i64 %indvars.iv
%13 = load i32* %arrayidx28, align 4
%arrayidx30 = getelementptr inbounds [5 x i32]* %d, i64 0, i64 %indvars.iv
%14 = load i32* %arrayidx30, align 4
%15 = trunc i64 %indvars.iv to i32
%call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([48 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %11, i32 %15, i32 %12, i32 %15, i32 %13, i32 %15, i32 %14) #1
br label %for.inc31

for.inc31: ; preds = %for.body22
%33 = load i32* %i, align 4
%inc32 = add nsw i32 %33, 1
store i32 %inc32, i32* %i, align 4
%indvars.iv.next = add i64 %indvars.iv, 1
br label %for.cond20

for.end33: ; preds = %for.cond20
%34 = load i32* %retval
ret i32 %34
ret i32 0

for.cond.copied: ; preds = %for.inc.copied, %for.cond
%35 = load i32* %i, align 4
%cmp.copied = icmp slt i32 %35, 5
br i1 %cmp.copied, label %for.body.copied, label %for.end
%indvars.iv1.copied = phi i64 [ %indvars.iv.next2.copied, %for.inc.copied ], [ 1, %for.cond ]
%lftr.wideiv5.copied = trunc i64 %indvars.iv1.copied to i32
%exitcond6.copied = icmp ne i32 %lftr.wideiv5.copied, 5
br i1 %exitcond6.copied, label %for.body.copied, label %for.end

for.body.copied: ; preds = %for.cond.copied
%36 = load i32* %i, align 4
%idxprom.copied = sext i32 %36 to i64
%arrayidx.copied = getelementptr inbounds [5 x i32]* %a, i32 0, i64 %idxprom.copied
%37 = load i32* %arrayidx.copied, align 4
%38 = load i32* %i, align 4
%sub.copied = sub nsw i32 %38, 1
%idxprom1.copied = sext i32 %sub.copied to i64
%arrayidx2.copied = getelementptr inbounds [5 x i32]* %b, i32 0, i64 %idxprom1.copied
%39 = load i32* %arrayidx2.copied, align 4
%add.copied = add nsw i32 %37, %39
%40 = load i32* %i, align 4
%idxprom3.copied = sext i32 %40 to i64
%arrayidx4.copied = getelementptr inbounds [5 x i32]* %a, i32 0, i64 %idxprom3.copied
%arrayidx.copied = getelementptr inbounds [5 x i32]* %a, i64 0, i64 %indvars.iv1.copied
%16 = load i32* %arrayidx.copied, align 4
%17 = add nsw i64 %indvars.iv1.copied, -1
%arrayidx2.copied = getelementptr inbounds [5 x i32]* %b, i64 0, i64 %17
%18 = load i32* %arrayidx2.copied, align 4
%add.copied = add nsw i32 %16, %18
%arrayidx4.copied = getelementptr inbounds [5 x i32]* %a, i64 0, i64 %indvars.iv1.copied
store i32 %add.copied, i32* %arrayidx4.copied, align 4
%41 = load i32* %i, align 4
%sub5.copied = sub nsw i32 %41, 1
%idxprom6.copied = sext i32 %sub5.copied to i64
%arrayidx7.copied = getelementptr inbounds [5 x i32]* %c, i32 0, i64 %idxprom6.copied
%42 = load i32* %arrayidx7.copied, align 4
%add8.copied = add nsw i32 %42, 5
%43 = load i32* %i, align 4
%idxprom9.copied = sext i32 %43 to i64
%arrayidx10.copied = getelementptr inbounds [5 x i32]* %b, i32 0, i64 %idxprom9.copied
%19 = add nsw i64 %indvars.iv1.copied, -1
%arrayidx7.copied = getelementptr inbounds [5 x i32]* %c, i64 0, i64 %19
%20 = load i32* %arrayidx7.copied, align 4
%add8.copied = add nsw i32 %20, 5
%arrayidx10.copied = getelementptr inbounds [5 x i32]* %b, i64 0, i64 %indvars.iv1.copied
store i32 %add8.copied, i32* %arrayidx10.copied, align 4
%44 = load i32* %i, align 4
%idxprom11.copied = sext i32 %44 to i64
%arrayidx12.copied = getelementptr inbounds [5 x i32]* %b, i32 0, i64 %idxprom11.copied
%45 = load i32* %arrayidx12.copied, align 4
%mul.copied = mul nsw i32 %45, 2
%46 = load i32* %i, align 4
%idxprom13.copied = sext i32 %46 to i64
%arrayidx14.copied = getelementptr inbounds [5 x i32]* %c, i32 0, i64 %idxprom13.copied
%arrayidx12.copied = getelementptr inbounds [5 x i32]* %b, i64 0, i64 %indvars.iv1.copied
%21 = load i32* %arrayidx12.copied, align 4
%mul.copied = shl nsw i32 %21, 1
%arrayidx14.copied = getelementptr inbounds [5 x i32]* %c, i64 0, i64 %indvars.iv1.copied
store i32 %mul.copied, i32* %arrayidx14.copied, align 4
%47 = load i32* %i, align 4
%idxprom15.copied = sext i32 %47 to i64
%arrayidx16.copied = getelementptr inbounds [5 x i32]* %c, i32 0, i64 %idxprom15.copied
%48 = load i32* %arrayidx16.copied, align 4
%add17.copied = add nsw i32 %48, 1
%49 = load i32* %i, align 4
%idxprom18.copied = sext i32 %49 to i64
%arrayidx19.copied = getelementptr inbounds [5 x i32]* %d, i32 0, i64 %idxprom18.copied
%arrayidx16.copied = getelementptr inbounds [5 x i32]* %c, i64 0, i64 %indvars.iv1.copied
%22 = load i32* %arrayidx16.copied, align 4
%add17.copied = add nsw i32 %22, 1
%arrayidx19.copied = getelementptr inbounds [5 x i32]* %d, i64 0, i64 %indvars.iv1.copied
store i32 %add17.copied, i32* %arrayidx19.copied, align 4
br label %for.inc.copied

for.inc.copied: ; preds = %for.body.copied
%50 = load i32* %i, align 4
%inc.copied = add nsw i32 %50, 1
store i32 %inc.copied, i32* %i, align 4
%indvars.iv.next2.copied = add i64 %indvars.iv1.copied, 1
br label %for.cond.copied
}

Expand Down
62 changes: 44 additions & 18 deletions lib/splitpass/splitpass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,23 @@ Loop * SplitPass::CreateOneLoop(Loop *L, LPPassManager *LPM) {

std::vector<PHINode*> OrigPHINode;

for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
OrigPHINode.push_back(cast<PHINode>(I));
}
// Testing PhiNode Methods
//for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
// PHINode *phi = cast<PHINode>(I);

// phi->dump();
// DEBUG(dbgs() << "\n");

// phi->getIncomingValueForBlock(LatchBlock)->dump();
// DEBUG(dbgs() << "\n");

// phi->getIncomingValueForBlock(PreHeader)->dump();
// DEBUG(dbgs() << "\n");


//}

//body.push_back(Header);
for(std::vector<BasicBlock * >::iterator it = body.begin(); it != body.end(); ++it) {
ValueToValueMapTy VMap;
BasicBlock *New = CloneBasicBlock(*it, VMap, ".copied");
Expand All @@ -91,36 +104,43 @@ Loop * SplitPass::CreateOneLoop(Loop *L, LPPassManager *LPM) {
LastValueMap[VI->first] = VI->second;
}
}
// new_body[0] is Header new_body(new_body.size()-1) is Latch

// change vars
for(unsigned i=0; i<new_body.size(); ++i) {
for (BasicBlock::iterator I = new_body[i]->begin(); I != new_body[i]->end(); ++I) {
remapInstruction(I, LastValueMap);
}
}
DEBUG(dbgs() << "Change Var Done \n");

BranchInst *new_back_edge = cast<BranchInst>(new_body[new_body.size()-1]->getTerminator()); // Latch's last inst is branch
new_back_edge->setSuccessor(0, new_body[0]); // Set to branch to new Cond (Header) 1st BB

for (BasicBlock::iterator I = new_body[0]->begin(); isa<PHINode>(I); ++I) {
for (unsigned phi_i = 0, e = OrigPHINode.size(); phi_i != e; ++phi_i) {
PHINode *NewPHI = cast<PHINode>(VMap[OrigPHINode[phi_i]]);

Value *InVal;
// 1
InVal = NewPHI->getIncomingValueForBlock(LatchBlock); // original Loop's Latch
if (Instruction *InValI = dyn_cast<Instruction>(InVal)) {
InVal = LastValueMap[InValI];
}
// 2
InVal = NewPHI->getIncomingValueForBlock(PreHeader); // original Loop's Header
if (Instruction *InValI = dyn_cast<Instruction>(InVal)) {
InVal = LastValueMap[InValI];


PHINode *NewPHI = cast<PHINode>(I);
DEBUG(dbgs() << "About to fix phi node: "<< NewPHI << "\n");

for (unsigned i=0; i<NewPHI->getNumIncomingValues(); ++i) {
if (NewPHI->getIncomingBlock(i) == PreHeader) {
NewPHI->setIncomingBlock(i, Header);
}
}
DEBUG(dbgs() << "Done phi node: "<< NewPHI << "\n");

//for (unsigned phi_i = 0, e = OrigPHINode.size(); phi_i != e; ++phi_i) {
// PHINode *NewPHI = cast<PHINode>(VMap[OrigPHINode[phi_i]]);
// DEBUG(dbgs() << "About to fix phi node: "<< NewPHI << "\n");
// NewPHI->setIncomingBlock(0, new_body[new_body.size()-1]);
// NewPHI->setIncomingBlock(1, Header);
//}
}
DEBUG(dbgs() << "Phi Node loop done \n");


BranchInst *new_back_edge = cast<BranchInst>(new_body[new_body.size()-1]->getTerminator()); // Latch's last inst is branch
new_back_edge->setSuccessor(0, new_body[0]); // Set to branch to new Cond (Header) 1st BB

// link new loop body together
for(unsigned i=0; i<new_body.size()-1; i++) {
BranchInst *next = cast<BranchInst>(new_body[i]->getTerminator());
Expand All @@ -132,6 +152,12 @@ Loop * SplitPass::CreateOneLoop(Loop *L, LPPassManager *LPM) {
DEBUG(dbgs() << "branch successors " << first_loop_to_next_loop->getNumSuccessors() << "\n");
first_loop_to_next_loop->setSuccessor(1, new_body[0]);

// first is last, last is first?
Header->dump();

for (unsigned i=0; i<new_body.size();i++) {
new_body[i]->dump();
}

Loop *new_loop = new Loop();

Expand Down

0 comments on commit 8052063

Please sign in to comment.