From: Peizhao Ou Date: Wed, 23 May 2018 07:24:03 +0000 (-0700) Subject: Fixes issue of nondominating load part of RMWs when taiting outside of RMW loop X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=020c3624c18e38cd7a34572dd1d803cdba020ea1;p=oota-llvm.git Fixes issue of nondominating load part of RMWs when taiting outside of RMW loop --- diff --git a/lib/CodeGen/CodeGenPrepare.cpp b/lib/CodeGen/CodeGenPrepare.cpp index f084a894d74..878e2827147 100644 --- a/lib/CodeGen/CodeGenPrepare.cpp +++ b/lib/CodeGen/CodeGenPrepare.cpp @@ -356,7 +356,16 @@ bool CodeGenPrepare::runOnFunction(Function &F) { } } } else if (I->getNeedTainted()) { - TaintRelaxedLoads(&*I, I->getNextNode()); + // Adds a fake conditional branch outside of the RMW loop. + auto* LoopBB = I->getParent(); + auto* LoopTerm = LoopBB->getTerminator(); + auto* BI = dyn_cast(LoopTerm); + assert(BI && BI->isConditional() && "RMW loop ends with unconditional branch?"); + BasicBlock* EndBB = BI->getSuccessor(1); + if (EndBB == LoopBB) { + BI->getSuccessor(0); + } + TaintAtBlockBeginning(&*I, EndBB, DT); } } EverMadeChange |= diff --git a/lib/CodeGen/TaintRelaxedAtomicsUtils.cpp b/lib/CodeGen/TaintRelaxedAtomicsUtils.cpp index 4ce73698be4..3b1f8039384 100644 --- a/lib/CodeGen/TaintRelaxedAtomicsUtils.cpp +++ b/lib/CodeGen/TaintRelaxedAtomicsUtils.cpp @@ -688,6 +688,32 @@ void TaintRelaxedLoads(Instruction* UsageInst, Instruction* InsertPoint) { AddFakeConditionalBranch(FakeCondition->getNextNode(), FakeCondition); } +// Taints the 'Inst' (i.e., adds a fake conditional block that uses the 'Inst') +// at the beginning of basic block 'BB'. Note that if 'Inst' does not dominate +// 'BB', we need to add appropriate an PHI node and taint the PHI node. Returns +// true if the code is changed, and false otherwise. +void TaintAtBlockBeginning(Instruction* Inst, BasicBlock* BB, DominatorTree* DT) { + auto* CurBB = Inst->getParent(); + auto* FirstInst = BB->getFirstNonPHI(); + if (DT->dominates(Inst, FirstInst)) { + return TaintRelaxedLoads(&*Inst, FirstInst); + } + IRBuilder Builder(FirstInst); + auto* Phi = Builder.CreatePHI(Inst->getType(), 0, Inst->getName() + ".phi"); + // Multiple blocks going to BB. We should add a PHI node w.r.t. 'Inst'. + for (auto* Pred : predecessors(BB)) { + Value* Val = nullptr; + if (Pred == CurBB) { + Val = Inst; + } else { + // We don't care what value other paths are. + Val = UndefValue::get(Inst->getType()); + } + Phi->addIncoming(Val, Pred); + } + return TaintRelaxedLoads(Phi, Phi); +} + // XXX-comment: Finds the appropriate Value derived from an atomic load. // 'ChainedBB' contains all the blocks chained together with unconditional // branches from LI's parent BB to the block with the first store/cond branch. diff --git a/lib/CodeGen/TaintRelaxedAtomicsUtils.h b/lib/CodeGen/TaintRelaxedAtomicsUtils.h index 1784e6f5829..1536fd3d880 100644 --- a/lib/CodeGen/TaintRelaxedAtomicsUtils.h +++ b/lib/CodeGen/TaintRelaxedAtomicsUtils.h @@ -186,6 +186,12 @@ void AddFakeConditionalBranch(Instruction* SplitInst, Value* Condition); // Returns true if the code is changed, and false otherwise. void TaintRelaxedLoads(Instruction* UsageInst, Instruction* InsertPoint); +// Taints the 'Inst' (i.e., adds a fake conditional block that uses the 'Inst') +// at the beginning of basic block 'BB'. Note that if 'Inst' does not dominate +// 'BB', we need to add appropriate an PHI node and taint the PHI node. Returns +// true if the code is changed, and false otherwise. +void TaintAtBlockBeginning(Instruction* Inst, BasicBlock* BB, DominatorTree* DT); + // XXX-comment: Finds the appropriate Value derived from an atomic load. // 'ChainedBB' contains all the blocks chained together with unconditional // branches from LI's parent BB to the block with the first store/cond branch.