@@ -188,10 +188,9 @@ static void createRetBitCast(CallBase &CB, Type *RetTy, CastInst **RetBitCast) {
188
188
// / Predicate and clone the given call site.
189
189
// /
190
190
// / 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.
195
194
// /
196
195
// / For example, the call instruction below:
197
196
// /
@@ -202,7 +201,7 @@ static void createRetBitCast(CallBase &CB, Type *RetTy, CastInst **RetBitCast) {
202
201
// / Is replace by the following:
203
202
// /
204
203
// / orig_bb:
205
- // / %cond = icmp eq i32 ()* %ptr, @func
204
+ // / %cond = Cond
206
205
// / br i1 %cond, %then_bb, %else_bb
207
206
// /
208
207
// / then_bb:
@@ -232,7 +231,7 @@ static void createRetBitCast(CallBase &CB, Type *RetTy, CastInst **RetBitCast) {
232
231
// / Is replace by the following:
233
232
// /
234
233
// / orig_bb:
235
- // / %cond = icmp eq i32 ()* %ptr, @func
234
+ // / %cond = Cond
236
235
// / br i1 %cond, %then_bb, %else_bb
237
236
// /
238
237
// / then_bb:
@@ -267,7 +266,7 @@ static void createRetBitCast(CallBase &CB, Type *RetTy, CastInst **RetBitCast) {
267
266
// / Is replaced by the following:
268
267
// /
269
268
// / cond_bb:
270
- // / %cond = icmp eq i32 ()* %ptr, @func
269
+ // / %cond = Cond
271
270
// / br i1 %cond, %then_bb, %orig_bb
272
271
// /
273
272
// / then_bb:
@@ -280,19 +279,13 @@ static void createRetBitCast(CallBase &CB, Type *RetTy, CastInst **RetBitCast) {
280
279
// / ; The original call instruction stays in its original block.
281
280
// / %t0 = musttail call i32 %ptr()
282
281
// / ret %t0
283
- CallBase &llvm::versionCallSite (CallBase &CB, Value *Callee ,
284
- MDNode *BranchWeights) {
282
+ static CallBase &versionCallSiteWithCond (CallBase &CB, Value *Cond ,
283
+ MDNode *BranchWeights) {
285
284
286
285
IRBuilder<> Builder (&CB);
287
286
CallBase *OrigInst = &CB;
288
287
BasicBlock *OrigBlock = OrigInst->getParent ();
289
288
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
-
296
289
if (OrigInst->isMustTailCall ()) {
297
290
// Create an if-then structure. The original instruction stays in its block,
298
291
// and a clone of the original instruction is placed in the "then" block.
@@ -380,6 +373,22 @@ CallBase &llvm::versionCallSite(CallBase &CB, Value *Callee,
380
373
return *NewInst;
381
374
}
382
375
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
+
383
392
bool llvm::isLegalToPromote (const CallBase &CB, Function *Callee,
384
393
const char **FailureReason) {
385
394
assert (!CB.getCalledFunction () && " Only indirect call sites can be promoted" );
0 commit comments