Skip to content

Commit 6c8ebc0

Browse files
[NFC][CallPromotionUtils]Extract a helper function versionCallSiteWithCond from versionCallSite (#81181)
* This is to be used by #81378 to implement a variant of versionCallSite that compares vtables. * The parent patch is #81051
1 parent b04c07b commit 6c8ebc0

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

llvm/lib/Transforms/Utils/CallPromotionUtils.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,9 @@ static void createRetBitCast(CallBase &CB, Type *RetTy, CastInst **RetBitCast) {
188188
/// Predicate and clone the given call site.
189189
///
190190
/// This function creates an if-then-else structure at the location of the call
191-
/// site. The "if" condition compares the call site's called value to the given
192-
/// callee. The original call site is moved into the "else" block, and a clone
193-
/// of the call site is placed in the "then" block. The cloned instruction is
194-
/// returned.
191+
/// site. The "if" condition is specified by `Cond`. The original call site is
192+
/// moved into the "else" block, and a clone of the call site is placed in the
193+
/// "then" block. The cloned instruction is returned.
195194
///
196195
/// For example, the call instruction below:
197196
///
@@ -202,7 +201,7 @@ static void createRetBitCast(CallBase &CB, Type *RetTy, CastInst **RetBitCast) {
202201
/// Is replace by the following:
203202
///
204203
/// orig_bb:
205-
/// %cond = icmp eq i32 ()* %ptr, @func
204+
/// %cond = Cond
206205
/// br i1 %cond, %then_bb, %else_bb
207206
///
208207
/// then_bb:
@@ -232,7 +231,7 @@ static void createRetBitCast(CallBase &CB, Type *RetTy, CastInst **RetBitCast) {
232231
/// Is replace by the following:
233232
///
234233
/// orig_bb:
235-
/// %cond = icmp eq i32 ()* %ptr, @func
234+
/// %cond = Cond
236235
/// br i1 %cond, %then_bb, %else_bb
237236
///
238237
/// then_bb:
@@ -267,7 +266,7 @@ static void createRetBitCast(CallBase &CB, Type *RetTy, CastInst **RetBitCast) {
267266
/// Is replaced by the following:
268267
///
269268
/// cond_bb:
270-
/// %cond = icmp eq i32 ()* %ptr, @func
269+
/// %cond = Cond
271270
/// br i1 %cond, %then_bb, %orig_bb
272271
///
273272
/// then_bb:
@@ -280,19 +279,13 @@ static void createRetBitCast(CallBase &CB, Type *RetTy, CastInst **RetBitCast) {
280279
/// ; The original call instruction stays in its original block.
281280
/// %t0 = musttail call i32 %ptr()
282281
/// ret %t0
283-
CallBase &llvm::versionCallSite(CallBase &CB, Value *Callee,
284-
MDNode *BranchWeights) {
282+
static CallBase &versionCallSiteWithCond(CallBase &CB, Value *Cond,
283+
MDNode *BranchWeights) {
285284

286285
IRBuilder<> Builder(&CB);
287286
CallBase *OrigInst = &CB;
288287
BasicBlock *OrigBlock = OrigInst->getParent();
289288

290-
// Create the compare. The called value and callee must have the same type to
291-
// be compared.
292-
if (CB.getCalledOperand()->getType() != Callee->getType())
293-
Callee = Builder.CreateBitCast(Callee, CB.getCalledOperand()->getType());
294-
auto *Cond = Builder.CreateICmpEQ(CB.getCalledOperand(), Callee);
295-
296289
if (OrigInst->isMustTailCall()) {
297290
// Create an if-then structure. The original instruction stays in its block,
298291
// and a clone of the original instruction is placed in the "then" block.
@@ -380,6 +373,22 @@ CallBase &llvm::versionCallSite(CallBase &CB, Value *Callee,
380373
return *NewInst;
381374
}
382375

376+
// Predicate and clone the given call site using condition `CB.callee ==
377+
// Callee`. See the comment `versionCallSiteWithCond` for the transformation.
378+
CallBase &llvm::versionCallSite(CallBase &CB, Value *Callee,
379+
MDNode *BranchWeights) {
380+
381+
IRBuilder<> Builder(&CB);
382+
383+
// Create the compare. The called value and callee must have the same type to
384+
// be compared.
385+
if (CB.getCalledOperand()->getType() != Callee->getType())
386+
Callee = Builder.CreateBitCast(Callee, CB.getCalledOperand()->getType());
387+
auto *Cond = Builder.CreateICmpEQ(CB.getCalledOperand(), Callee);
388+
389+
return versionCallSiteWithCond(CB, Cond, BranchWeights);
390+
}
391+
383392
bool llvm::isLegalToPromote(const CallBase &CB, Function *Callee,
384393
const char **FailureReason) {
385394
assert(!CB.getCalledFunction() && "Only indirect call sites can be promoted");

0 commit comments

Comments
 (0)