@@ -2515,7 +2515,8 @@ class EarlyOutPatterns : public FunctionPass
25152515 ArrayRef<Instruction*> Values,
25162516 const DenseSet<const Value*>& FoldedVals);
25172517 static unsigned int shortPathToOutput (Value* inst, unsigned int limit);
2518-
2518+ static Instruction* FindLastFoldedValue (const DenseSet<const Value*>& FoldedVals,
2519+ BasicBlock* currentBB);
25192520 CodeGenContext* m_ctx;
25202521 unsigned int m_ShaderLength;
25212522};
@@ -3477,6 +3478,21 @@ DenseSet<const Value*> EarlyOutPatterns::tryAndFoldValues(ArrayRef<Instruction*>
34773478 return FoldedVals;
34783479}
34793480
3481+ Instruction* EarlyOutPatterns::FindLastFoldedValue (const DenseSet<const Value*>& FoldedVals, BasicBlock* currentBB)
3482+ {
3483+ // traverse the block backwards and find the last folded value
3484+ for (BasicBlock::reverse_iterator re = currentBB->rbegin (), ri = currentBB->rend (); re != ri; ++re)
3485+ {
3486+ if (FoldedVals.count (&(*re)))
3487+ {
3488+ re--;
3489+ return &*re;
3490+ }
3491+ }
3492+ // default to return the last instruction in the block
3493+ return &*(currentBB->rbegin ());
3494+ }
3495+
34803496// return the new block where the code after inst was moved
34813497BasicBlock* EarlyOutPatterns::SplitBasicBlock (Instruction* inst, const DenseSet<const Value*>& FoldedVals)
34823498{
@@ -3491,7 +3507,7 @@ BasicBlock* EarlyOutPatterns::SplitBasicBlock(Instruction* inst, const DenseSet<
34913507 elseBlock->getInstList ().splice (elseBlock->begin (),
34923508 currentBB->getInstList (),
34933509 inst->getNextNode ()->getIterator (),
3494- currentBB-> getTerminator ( )->getIterator ());
3510+ FindLastFoldedValue (FoldedVals, currentBB )->getIterator ());
34953511 endifBlock->getInstList ().splice (endifBlock->begin (), currentBB->getInstList (), currentBB->getTerminator ());
34963512 if (isa<ReturnInst>(endifBlock->getTerminator ()))
34973513 {
@@ -3502,7 +3518,6 @@ BasicBlock* EarlyOutPatterns::SplitBasicBlock(Instruction* inst, const DenseSet<
35023518 // split the blocks
35033519 ValueToValueMapTy VMap;
35043520
3505- // TODO: Create if block only for affected instructions
35063521 BasicBlock* ifBlock = CloneBasicBlock (elseBlock, VMap);
35073522 ifBlock->setName (VALUE_NAME (" EO_IF" ));
35083523 currentBB->getParent ()->getBasicBlockList ().insertAfter (currentBB->getIterator (), ifBlock);
@@ -3516,6 +3531,7 @@ BasicBlock* EarlyOutPatterns::SplitBasicBlock(Instruction* inst, const DenseSet<
35163531 II->setOperand (op, It->second );
35173532 }
35183533 }
3534+
35193535 // create phi instruction
35203536 for (auto II = elseBlock->begin (), IE = elseBlock->end (); II != IE; ++II)
35213537 {
@@ -3535,6 +3551,7 @@ BasicBlock* EarlyOutPatterns::SplitBasicBlock(Instruction* inst, const DenseSet<
35353551 }
35363552 }
35373553 }
3554+
35383555 // replace the folded values with 0
35393556 for (auto it : FoldedVals)
35403557 {
0 commit comments