[llvm] 3946854 - [NFC][PartiallyInlineLibCalls] Port to SplitBlockAndInsertIfThen()

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 28 14:12:07 PST 2021


Author: Roman Lebedev
Date: 2021-01-29T01:11:33+03:00
New Revision: 394685481c6ff31ab0721f672ff1d13527e8e42a

URL: https://github.com/llvm/llvm-project/commit/394685481c6ff31ab0721f672ff1d13527e8e42a
DIFF: https://github.com/llvm/llvm-project/commit/394685481c6ff31ab0721f672ff1d13527e8e42a.diff

LOG: [NFC][PartiallyInlineLibCalls] Port to SplitBlockAndInsertIfThen()

This makes follow-up patch for Dominator Tree preservation
somewhat more straight-forward.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp b/llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp
index 58763ec72ece..937a34cc10d0 100644
--- a/llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp
+++ b/llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp
@@ -51,33 +51,44 @@ static bool optimizeSQRT(CallInst *Call, Function *CalledFunc,
   // dst = phi(v0, v1)
   //
 
-  // Move all instructions following Call to newly created block JoinBB.
-  // Create phi and replace all uses.
-  BasicBlock *JoinBB = llvm::SplitBlock(&CurrBB, Call->getNextNode());
-  IRBuilder<> Builder(JoinBB, JoinBB->begin());
   Type *Ty = Call->getType();
+  IRBuilder<> Builder(Call->getNextNode());
+
+  // Split CurrBB right after the call, create a 'then' block (that branches
+  // back to split-off tail of CurrBB) into which we'll insert a libcall.
+  Instruction *LibCallTerm = SplitBlockAndInsertIfThen(
+      Builder.getTrue(), Call->getNextNode(), /*Unreachable=*/false,
+      /*BranchWeights*/ nullptr);
+
+  auto *CurrBBTerm = cast<BranchInst>(CurrBB.getTerminator());
+  // We want an 'else' block though, not a 'then' block.
+  cast<BranchInst>(CurrBBTerm)->swapSuccessors();
+
+  // Create phi that will merge results of either sqrt and replace all uses.
+  BasicBlock *JoinBB = LibCallTerm->getSuccessor(0);
+  JoinBB->setName(CurrBB.getName() + ".split");
+  Builder.SetInsertPoint(JoinBB, JoinBB->begin());
   PHINode *Phi = Builder.CreatePHI(Ty, 2);
   Call->replaceAllUsesWith(Phi);
 
-  // Create basic block LibCallBB and insert a call to library function sqrt.
-  BasicBlock *LibCallBB = BasicBlock::Create(CurrBB.getContext(), "call.sqrt",
-                                             CurrBB.getParent(), JoinBB);
-  Builder.SetInsertPoint(LibCallBB);
+  // Finally, insert the libcall into 'else' block.
+  BasicBlock *LibCallBB = LibCallTerm->getParent();
+  LibCallBB->setName("call.sqrt");
+  Builder.SetInsertPoint(LibCallTerm);
   Instruction *LibCall = Call->clone();
   Builder.Insert(LibCall);
-  Builder.CreateBr(JoinBB);
 
   // Add attribute "readnone" so that backend can use a native sqrt instruction
-  // for this call. Insert a FP compare instruction and a conditional branch
-  // at the end of CurrBB.
+  // for this call.
   Call->addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone);
-  CurrBB.getTerminator()->eraseFromParent();
-  Builder.SetInsertPoint(&CurrBB);
+
+  // Insert a FP compare instruction and use it as the CurrBB branch condition.
+  Builder.SetInsertPoint(CurrBBTerm);
   Value *FCmp = TTI->isFCmpOrdCheaperThanFCmpZero(Ty)
                     ? Builder.CreateFCmpORD(Call, Call)
                     : Builder.CreateFCmpOGE(Call->getOperand(0),
                                             ConstantFP::get(Ty, 0.0));
-  Builder.CreateCondBr(FCmp, JoinBB, LibCallBB);
+  CurrBBTerm->setCondition(FCmp);
 
   // Add phi operands.
   Phi->addIncoming(Call, &CurrBB);


        


More information about the llvm-commits mailing list