Commit 7f2e0e0b authored by Kevin Modzelewski's avatar Kevin Modzelewski

Remap phis when breaking crit edges

parent 91df8ebc
...@@ -76,6 +76,20 @@ void RefcountTracker::refConsumed(llvm::Value* v, llvm::Instruction* inst) { ...@@ -76,6 +76,20 @@ void RefcountTracker::refConsumed(llvm::Value* v, llvm::Instruction* inst) {
//var.ref_consumers.push_back(inst); //var.ref_consumers.push_back(inst);
} }
void remapPhis(llvm::BasicBlock* in_block, llvm::BasicBlock* from_block, llvm::BasicBlock* new_from_block) {
for (llvm::Instruction& i : *in_block) {
llvm::Instruction* I = &i;
llvm::PHINode* phi = llvm::dyn_cast<llvm::PHINode>(I);
if (!phi)
break;
int idx = phi->getBasicBlockIndex(from_block);
if (idx == -1)
continue;
phi->setIncomingBlock(idx, new_from_block);
}
}
llvm::Instruction* findInsertionPoint(llvm::BasicBlock* BB, llvm::BasicBlock* from_bb, llvm::Instruction* findInsertionPoint(llvm::BasicBlock* BB, llvm::BasicBlock* from_bb,
llvm::DenseMap<llvm::BasicBlock*, llvm::Instruction*> cache) { llvm::DenseMap<llvm::BasicBlock*, llvm::Instruction*> cache) {
assert(BB); assert(BB);
...@@ -89,7 +103,7 @@ llvm::Instruction* findInsertionPoint(llvm::BasicBlock* BB, llvm::BasicBlock* fr ...@@ -89,7 +103,7 @@ llvm::Instruction* findInsertionPoint(llvm::BasicBlock* BB, llvm::BasicBlock* fr
if (numPredecessors(BB) > 1) { if (numPredecessors(BB) > 1) {
ASSERT(from_bb, "Don't know how to break the critical edge to(%s)", BB->getName().data()); ASSERT(from_bb, "Don't know how to break the critical edge to(%s)", BB->getName().data());
llvm::BasicBlock* breaker_block = llvm::BasicBlock::Create(g.context, "", from_bb->getParent(), BB); llvm::BasicBlock* breaker_block = llvm::BasicBlock::Create(g.context, "breaker", from_bb->getParent(), BB);
llvm::BranchInst::Create(BB, breaker_block); llvm::BranchInst::Create(BB, breaker_block);
auto terminator = from_bb->getTerminator(); auto terminator = from_bb->getTerminator();
...@@ -108,6 +122,8 @@ llvm::Instruction* findInsertionPoint(llvm::BasicBlock* BB, llvm::BasicBlock* fr ...@@ -108,6 +122,8 @@ llvm::Instruction* findInsertionPoint(llvm::BasicBlock* BB, llvm::BasicBlock* fr
RELEASE_ASSERT(0, "unhandled terminator type"); RELEASE_ASSERT(0, "unhandled terminator type");
} }
remapPhis(BB, from_bb, breaker_block);
cache[BB] = breaker_block->getFirstInsertionPt(); cache[BB] = breaker_block->getFirstInsertionPt();
return cache[BB]; return cache[BB];
} }
......
...@@ -5197,7 +5197,7 @@ Box* nonzeroAndBox(Box* b, bool negate) { ...@@ -5197,7 +5197,7 @@ Box* nonzeroAndBox(Box* b, bool negate) {
if (likely(b->cls == bool_cls)) { if (likely(b->cls == bool_cls)) {
if (negate) if (negate)
return boxBool(b != True); return boxBool(b != True);
return b; return incref(b);
} }
bool t = b->nonzeroIC(); bool t = b->nonzeroIC();
...@@ -5312,8 +5312,8 @@ Box* compareInternal(Box* lhs, Box* rhs, int op_type, CompareRewriteArgs* rewrit ...@@ -5312,8 +5312,8 @@ Box* compareInternal(Box* lhs, Box* rhs, int op_type, CompareRewriteArgs* rewrit
if (rewrite_args) { if (rewrite_args) {
auto r_negate = rewrite_args->rewriter->loadConst((int)(op_type == AST_TYPE::NotIn)); auto r_negate = rewrite_args->rewriter->loadConst((int)(op_type == AST_TYPE::NotIn));
RewriterVar* r_contained_box RewriterVar* r_contained_box = rewrite_args->rewriter->call(true, (void*)nonzeroAndBox, r_contained,
= rewrite_args->rewriter->call(true, (void*)nonzeroAndBox, r_contained, r_negate); r_negate)->setType(RefType::OWNED);
rewrite_args->out_rtn = r_contained_box; rewrite_args->out_rtn = r_contained_box;
rewrite_args->out_success = true; rewrite_args->out_success = true;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment