diff --git a/llvm/lib/Transforms/Scalar/CallSiteSplitting.cpp b/llvm/lib/Transforms/Scalar/CallSiteSplitting.cpp --- a/llvm/lib/Transforms/Scalar/CallSiteSplitting.cpp +++ b/llvm/lib/Transforms/Scalar/CallSiteSplitting.cpp @@ -85,37 +85,36 @@ "their cost is below DuplicationThreshold"), cl::init(5)); -static void addNonNullAttribute(CallSite CS, Value *Op) { +static void addNonNullAttribute(CallBase &CB, Value *Op) { unsigned ArgNo = 0; - for (auto &I : CS.args()) { + for (auto &I : CB.args()) { if (&*I == Op) - CS.addParamAttr(ArgNo, Attribute::NonNull); + CB.addParamAttr(ArgNo, Attribute::NonNull); ++ArgNo; } } -static void setConstantInArgument(CallSite CS, Value *Op, +static void setConstantInArgument(CallBase &CB, Value *Op, Constant *ConstValue) { unsigned ArgNo = 0; - for (auto &I : CS.args()) { + for (auto &I : CB.args()) { if (&*I == Op) { // It is possible we have already added the non-null attribute to the // parameter by using an earlier constraining condition. - CS.removeParamAttr(ArgNo, Attribute::NonNull); - CS.setArgument(ArgNo, ConstValue); + CB.removeParamAttr(ArgNo, Attribute::NonNull); + CB.setArgOperand(ArgNo, ConstValue); } ++ArgNo; } } -static bool isCondRelevantToAnyCallArgument(ICmpInst *Cmp, CallSite CS) { +static bool isCondRelevantToAnyCallArgument(ICmpInst *Cmp, CallBase &CB) { assert(isa(Cmp->getOperand(1)) && "Expected a constant operand."); Value *Op0 = Cmp->getOperand(0); unsigned ArgNo = 0; - for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; - ++I, ++ArgNo) { + for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I, ++ArgNo) { // Don't consider constant or arguments that are already known non-null. - if (isa(*I) || CS.paramHasAttr(ArgNo, Attribute::NonNull)) + if (isa(*I) || CB.paramHasAttr(ArgNo, Attribute::NonNull)) continue; if (*I == Op0) @@ -128,8 +127,8 @@ typedef SmallVector ConditionsTy; /// If From has a conditional jump to To, add the condition to Conditions, -/// if it is relevant to any argument at CS. -static void recordCondition(CallSite CS, BasicBlock *From, BasicBlock *To, +/// if it is relevant to any argument at CB. +static void recordCondition(CallBase &CB, BasicBlock *From, BasicBlock *To, ConditionsTy &Conditions) { auto *BI = dyn_cast(From->getTerminator()); if (!BI || !BI->isConditional()) @@ -142,38 +141,38 @@ ICmpInst *Cmp = cast(Cond); if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) - if (isCondRelevantToAnyCallArgument(Cmp, CS)) + if (isCondRelevantToAnyCallArgument(Cmp, CB)) Conditions.push_back({Cmp, From->getTerminator()->getSuccessor(0) == To ? Pred : Cmp->getInversePredicate()}); } -/// Record ICmp conditions relevant to any argument in CS following Pred's +/// Record ICmp conditions relevant to any argument in CB following Pred's /// single predecessors. If there are conflicting conditions along a path, like /// x == 1 and x == 0, the first condition will be used. We stop once we reach /// an edge to StopAt. -static void recordConditions(CallSite CS, BasicBlock *Pred, +static void recordConditions(CallBase &CB, BasicBlock *Pred, ConditionsTy &Conditions, BasicBlock *StopAt) { BasicBlock *From = Pred; BasicBlock *To = Pred; SmallPtrSet Visited; while (To != StopAt && !Visited.count(From->getSinglePredecessor()) && (From = From->getSinglePredecessor())) { - recordCondition(CS, From, To, Conditions); + recordCondition(CB, From, To, Conditions); Visited.insert(From); To = From; } } -static void addConditions(CallSite CS, const ConditionsTy &Conditions) { +static void addConditions(CallBase &CB, const ConditionsTy &Conditions) { for (auto &Cond : Conditions) { Value *Arg = Cond.first->getOperand(0); Constant *ConstVal = cast(Cond.first->getOperand(1)); if (Cond.second == ICmpInst::ICMP_EQ) - setConstantInArgument(CS, Arg, ConstVal); + setConstantInArgument(CB, Arg, ConstVal); else if (ConstVal->getType()->isPointerTy() && ConstVal->isNullValue()) { assert(Cond.second == ICmpInst::ICMP_NE); - addNonNullAttribute(CS, Arg); + addNonNullAttribute(CB, Arg); } } } @@ -184,17 +183,16 @@ return Preds; } -static bool canSplitCallSite(CallSite CS, TargetTransformInfo &TTI) { - if (CS.isConvergent() || CS.cannotDuplicate()) +static bool canSplitCallSite(CallBase &CB, TargetTransformInfo &TTI) { + if (CB.isConvergent() || CB.cannotDuplicate()) return false; // FIXME: As of now we handle only CallInst. InvokeInst could be handled // without too much effort. - Instruction *Instr = CS.getInstruction(); - if (!isa(Instr)) + if (!isa(CB)) return false; - BasicBlock *CallSiteBB = Instr->getParent(); + BasicBlock *CallSiteBB = CB.getParent(); // Need 2 predecessors and cannot split an edge from an IndirectBrInst. SmallVector Preds(predecessors(CallSiteBB)); if (Preds.size() != 2 || isa(Preds[0]->getTerminator()) || @@ -212,7 +210,7 @@ // corresponding uses will be updated. unsigned Cost = 0; for (auto &InstBeforeCall : - llvm::make_range(CallSiteBB->begin(), Instr->getIterator())) { + llvm::make_range(CallSiteBB->begin(), CB.getIterator())) { Cost += TTI.getInstructionCost(&InstBeforeCall, TargetTransformInfo::TCK_CodeSize); if (Cost >= DuplicationThreshold) @@ -304,24 +302,23 @@ /// predecessors, new call-sites with more constrained arguments will be /// created in createCallSitesOnPredicatedArgument(). static void splitCallSite( - CallSite CS, + CallBase &CB, const SmallVectorImpl> &Preds, DomTreeUpdater &DTU) { - Instruction *Instr = CS.getInstruction(); - BasicBlock *TailBB = Instr->getParent(); - bool IsMustTailCall = CS.isMustTailCall(); + BasicBlock *TailBB = CB.getParent(); + bool IsMustTailCall = CB.isMustTailCall(); PHINode *CallPN = nullptr; // `musttail` calls must be followed by optional `bitcast`, and `ret`. The // split blocks will be terminated right after that so there're no users for // this phi in a `TailBB`. - if (!IsMustTailCall && !Instr->use_empty()) { - CallPN = PHINode::Create(Instr->getType(), Preds.size(), "phi.call"); - CallPN->setDebugLoc(Instr->getDebugLoc()); + if (!IsMustTailCall && !CB.use_empty()) { + CallPN = PHINode::Create(CB.getType(), Preds.size(), "phi.call"); + CallPN->setDebugLoc(CB.getDebugLoc()); } - LLVM_DEBUG(dbgs() << "split call-site : " << *Instr << " into \n"); + LLVM_DEBUG(dbgs() << "split call-site : " << CB << " into \n"); assert(Preds.size() == 2 && "The ValueToValueMaps array has size 2."); // ValueToValueMapTy is neither copy nor moveable, so we use a simple array @@ -330,21 +327,20 @@ for (unsigned i = 0; i < Preds.size(); i++) { BasicBlock *PredBB = Preds[i].first; BasicBlock *SplitBlock = DuplicateInstructionsInSplitBetween( - TailBB, PredBB, &*std::next(Instr->getIterator()), ValueToValueMaps[i], + TailBB, PredBB, &*std::next(CB.getIterator()), ValueToValueMaps[i], DTU); assert(SplitBlock && "Unexpected new basic block split."); - Instruction *NewCI = - &*std::prev(SplitBlock->getTerminator()->getIterator()); - CallSite NewCS(NewCI); - addConditions(NewCS, Preds[i].second); + auto *NewCI = + cast(&*std::prev(SplitBlock->getTerminator()->getIterator())); + addConditions(*NewCI, Preds[i].second); // Handle PHIs used as arguments in the call-site. for (PHINode &PN : TailBB->phis()) { unsigned ArgNo = 0; - for (auto &CI : CS.args()) { + for (auto &CI : CB.args()) { if (&*CI == &PN) { - NewCS.setArgument(ArgNo, PN.getIncomingValueForBlock(SplitBlock)); + NewCI->setArgOperand(ArgNo, PN.getIncomingValueForBlock(SplitBlock)); } ++ArgNo; } @@ -356,7 +352,7 @@ // Clone and place bitcast and return instructions before `TI` if (IsMustTailCall) - copyMustTailReturn(SplitBlock, Instr, NewCI); + copyMustTailReturn(SplitBlock, &CB, NewCI); } NumCallSiteSplit++; @@ -383,7 +379,7 @@ // Replace users of the original call with a PHI mering call-sites split. if (CallPN) { CallPN->insertBefore(OriginalBegin); - Instr->replaceAllUsesWith(CallPN); + CB.replaceAllUsesWith(CallPN); } // Remove instructions moved to split blocks from TailBB, from the duplicated @@ -393,7 +389,7 @@ // instruction, so we do not end up deleting them. By using reverse-order, we // do not introduce unnecessary PHI nodes for def-use chains from the call // instruction to the beginning of the block. - auto I = Instr->getReverseIterator(); + auto I = CB.getReverseIterator(); while (I != TailBB->rend()) { Instruction *CurrentI = &*I++; if (!CurrentI->use_empty()) { @@ -418,14 +414,13 @@ // Return true if the call-site has an argument which is a PHI with only // constant incoming values. -static bool isPredicatedOnPHI(CallSite CS) { - Instruction *Instr = CS.getInstruction(); - BasicBlock *Parent = Instr->getParent(); - if (Instr != Parent->getFirstNonPHIOrDbg()) +static bool isPredicatedOnPHI(CallBase &CB) { + BasicBlock *Parent = CB.getParent(); + if (&CB != Parent->getFirstNonPHIOrDbg()) return false; for (auto &PN : Parent->phis()) { - for (auto &Arg : CS.args()) { + for (auto &Arg : CB.args()) { if (&*Arg != &PN) continue; assert(PN.getNumIncomingValues() == 2 && @@ -446,20 +441,20 @@ // Check if any of the arguments in CS are predicated on a PHI node and return // the set of predecessors we should use for splitting. -static PredsWithCondsTy shouldSplitOnPHIPredicatedArgument(CallSite CS) { - if (!isPredicatedOnPHI(CS)) +static PredsWithCondsTy shouldSplitOnPHIPredicatedArgument(CallBase &CB) { + if (!isPredicatedOnPHI(CB)) return {}; - auto Preds = getTwoPredecessors(CS.getInstruction()->getParent()); + auto Preds = getTwoPredecessors(CB.getParent()); return {{Preds[0], {}}, {Preds[1], {}}}; } // Checks if any of the arguments in CS are predicated in a predecessor and // returns a list of predecessors with the conditions that hold on their edges // to CS. -static PredsWithCondsTy shouldSplitOnPredicatedArgument(CallSite CS, +static PredsWithCondsTy shouldSplitOnPredicatedArgument(CallBase &CB, DomTreeUpdater &DTU) { - auto Preds = getTwoPredecessors(CS.getInstruction()->getParent()); + auto Preds = getTwoPredecessors(CB.getParent()); if (Preds[0] == Preds[1]) return {}; @@ -468,16 +463,16 @@ // that node will be the same for all paths to the call site and splitting // is not beneficial. assert(DTU.hasDomTree() && "We need a DTU with a valid DT!"); - auto *CSDTNode = DTU.getDomTree().getNode(CS.getInstruction()->getParent()); + auto *CSDTNode = DTU.getDomTree().getNode(CB.getParent()); BasicBlock *StopAt = CSDTNode ? CSDTNode->getIDom()->getBlock() : nullptr; SmallVector, 2> PredsCS; for (auto *Pred : make_range(Preds.rbegin(), Preds.rend())) { ConditionsTy Conditions; // Record condition on edge BB(CS) <- Pred - recordCondition(CS, Pred, CS.getInstruction()->getParent(), Conditions); + recordCondition(CB, Pred, CB.getParent(), Conditions); // Record conditions following Pred's single predecessors. - recordConditions(CS, Pred, Conditions, StopAt); + recordConditions(CB, Pred, Conditions, StopAt); PredsCS.push_back({Pred, Conditions}); } @@ -489,19 +484,19 @@ return PredsCS; } -static bool tryToSplitCallSite(CallSite CS, TargetTransformInfo &TTI, +static bool tryToSplitCallSite(CallBase &CB, TargetTransformInfo &TTI, DomTreeUpdater &DTU) { // Check if we can split the call site. - if (!CS.arg_size() || !canSplitCallSite(CS, TTI)) + if (!CB.arg_size() || !canSplitCallSite(CB, TTI)) return false; - auto PredsWithConds = shouldSplitOnPredicatedArgument(CS, DTU); + auto PredsWithConds = shouldSplitOnPredicatedArgument(CB, DTU); if (PredsWithConds.empty()) - PredsWithConds = shouldSplitOnPHIPredicatedArgument(CS); + PredsWithConds = shouldSplitOnPHIPredicatedArgument(CB); if (PredsWithConds.empty()) return false; - splitCallSite(CS, PredsWithConds, DTU); + splitCallSite(CB, PredsWithConds, DTU); return true; } @@ -519,20 +514,19 @@ // case, IE will be invalidated and we also have to check the current // terminator. while (II != IE && &*II != BB.getTerminator()) { - Instruction *I = &*II++; - CallSite CS(cast(I)); - if (!CS || isa(I) || isInstructionTriviallyDead(I, &TLI)) + CallBase *CB = dyn_cast(&*II++); + if (!CB || isa(CB) || isInstructionTriviallyDead(CB, &TLI)) continue; - Function *Callee = CS.getCalledFunction(); + Function *Callee = CB->getCalledFunction(); if (!Callee || Callee->isDeclaration()) continue; // Successful musttail call-site splits result in erased CI and erased BB. // Check if such path is possible before attempting the splitting. - bool IsMustTail = CS.isMustTailCall(); + bool IsMustTail = CB->isMustTailCall(); - Changed |= tryToSplitCallSite(CS, TTI, DTU); + Changed |= tryToSplitCallSite(*CB, TTI, DTU); // There're no interesting instructions after this. The call site // itself might have been erased on splitting.