Directly return promoted direct call instead of rely on stripPointerCast.
Summary: stripPointerCast is not reliably returning the value that's being type-casted. Instead it may look further at function attributes to further propagate the value. Instead of relying on stripPOintercast, the more reliable solution is to directly use the pointer to the promoted direct call.
Reviewers: tejohnson, davidxl
Reviewed By: tejohnson
Subscribers: llvm-commits, sanjoy
Differential Revision: https://reviews.llvm.org/D38603
llvm-svn: 315077
diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp
index fb7397c..39e3696 100644
--- a/llvm/lib/Transforms/IPO/SampleProfile.cpp
+++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp
@@ -790,9 +790,8 @@
// as a result, we do not have profile info for the branch
// probability. We set the probability to 80% taken to indicate
// that the static call is likely taken.
- Instruction *DI = dyn_cast<Instruction>(
- promoteIndirectCall(I, R->getValue(), 80, 100, false, ORE)
- ->stripPointerCasts());
+ Instruction *DI = promoteIndirectCall(
+ I, R->getValue(), 80, 100, false, ORE);
PromotedInsns.insert(I);
// If profile mismatches, we should not attempt to inline DI.
if ((isa<CallInst>(DI) || isa<InvokeInst>(DI)) &&
diff --git a/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp b/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp
index f323e08..d8baa4f 100644
--- a/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp
+++ b/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp
@@ -461,11 +461,13 @@
// MergeBB is the bottom BB of the if-then-else-diamond after the
// transformation. For invoke instruction, the edges from DirectCallBB and
// IndirectCallBB to MergeBB are removed before this call (during
-// createIfThenElse).
+// createIfThenElse). Stores the pointer to the Instruction that cast
+// the direct call in \p CastInst.
static Instruction *createDirectCallInst(const Instruction *Inst,
Function *DirectCallee,
BasicBlock *DirectCallBB,
- BasicBlock *MergeBB) {
+ BasicBlock *MergeBB,
+ Instruction *&CastInst) {
Instruction *NewInst = Inst->clone();
if (CallInst *CI = dyn_cast<CallInst>(NewInst)) {
CI->setCalledFunction(DirectCallee);
@@ -499,7 +501,8 @@
}
}
- return insertCallRetCast(Inst, NewInst, DirectCallee);
+ CastInst = insertCallRetCast(Inst, NewInst, DirectCallee);
+ return NewInst;
}
// Create a PHI to unify the return values of calls.
@@ -559,15 +562,17 @@
createIfThenElse(Inst, DirectCallee, Count, TotalCount, &DirectCallBB,
&IndirectCallBB, &MergeBB);
+ // If the return type of the NewInst is not the same as the Inst, a CastInst
+ // is needed for type casting. Otherwise CastInst is the same as NewInst.
+ Instruction *CastInst = nullptr;
Instruction *NewInst =
- createDirectCallInst(Inst, DirectCallee, DirectCallBB, MergeBB);
+ createDirectCallInst(Inst, DirectCallee, DirectCallBB, MergeBB, CastInst);
if (AttachProfToDirectCall) {
SmallVector<uint32_t, 1> Weights;
Weights.push_back(Count);
MDBuilder MDB(NewInst->getContext());
- if (Instruction *DI = dyn_cast<Instruction>(NewInst->stripPointerCasts()))
- DI->setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
+ NewInst->setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
}
// Move Inst from MergeBB to IndirectCallBB.
@@ -589,10 +594,10 @@
// We don't need to update the operand from NormalDest for DirectCallBB.
// Pass nullptr here.
fixupPHINodeForNormalDest(Inst, II->getNormalDest(), MergeBB,
- IndirectCallBB, NewInst);
+ IndirectCallBB, CastInst);
}
- insertCallRetPHI(Inst, NewInst, DirectCallee);
+ insertCallRetPHI(Inst, CastInst, DirectCallee);
DEBUG(dbgs() << "\n== Basic Blocks After ==\n");
DEBUG(dbgs() << *BB << *DirectCallBB << *IndirectCallBB << *MergeBB << "\n");