Index: include/llvm/Analysis/ScalarEvolutionExpander.h =================================================================== --- include/llvm/Analysis/ScalarEvolutionExpander.h +++ include/llvm/Analysis/ScalarEvolutionExpander.h @@ -39,6 +39,7 @@ class SCEVExpander : public SCEVVisitor { ScalarEvolution &SE; const DataLayout &DL; + DebugLoc CurDbgLoc; // New instructions receive a name to identifies them with the current pass. const char* IVName; @@ -93,7 +94,6 @@ IRBuilderBase &Builder; AssertingVH Block; BasicBlock::iterator Point; - DebugLoc DbgLoc; SCEVExpander *SE; SCEVInsertPointGuard(const SCEVInsertPointGuard &) = delete; @@ -102,7 +102,7 @@ public: SCEVInsertPointGuard(IRBuilderBase &B, SCEVExpander *SE) : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()), - DbgLoc(B.getCurrentDebugLocation()), SE(SE) { + SE(SE) { SE->InsertPointGuards.push_back(this); } @@ -113,7 +113,6 @@ assert(SE->InsertPointGuards.back() == this); SE->InsertPointGuards.pop_back(); Builder.restoreIP(IRBuilderBase::InsertPoint(Block, Point)); - Builder.SetCurrentDebugLocation(DbgLoc); } BasicBlock::iterator GetInsertPoint() const { return Point; } @@ -177,6 +176,10 @@ /// type for the specified loop (inserting one if there is none). A /// canonical induction variable starts at zero and steps by one on each /// iteration. + PHINode *getOrInsertCanonicalInductionVariable(const Loop *L, + DebugLoc DbgLoc, Type *Ty); + + /// deprecated{Please use the overload which requires a DebugLoc.} PHINode *getOrInsertCanonicalInductionVariable(const Loop *L, Type *Ty); /// Return the induction variable increment's IV operand. @@ -194,38 +197,70 @@ /// Insert code to directly compute the specified SCEV expression into the /// program. The inserted code is inserted into the specified block. + Value *expandCodeFor(const SCEV *SH, DebugLoc DbgLoc, Type *Ty, + Instruction *I); + + /// deprecated{Please use the overload which requires a DebugLoc.} Value *expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I); /// Insert code to directly compute the specified SCEV expression into the /// program. The inserted code is inserted into the SCEVExpander's current /// insertion point. If a type is specified, the result will be expanded to /// have that type, with a cast if necessary. - Value *expandCodeFor(const SCEV *SH, Type *Ty = nullptr); + Value *expandCodeFor(const SCEV *SH, DebugLoc DbgLoc, Type *Ty = nullptr); + /// deprecated{Please use the overload which requires a DebugLoc.} + Value *expandCodeFor(const SCEV *SH, Type *Ty = nullptr); /// Generates a code sequence that evaluates this predicate. The inserted /// instructions will be at position \p Loc. The result will be of type i1 /// and will have a value of 0 when the predicate is false and 1 otherwise. + Value *expandCodeForPredicate(const SCEVPredicate *Pred, DebugLoc DbgLoc, + Instruction *Loc); + + /// deprecated{Please use the overload which requires a DebugLoc.} Value *expandCodeForPredicate(const SCEVPredicate *Pred, Instruction *Loc); /// A specialized variant of expandCodeForPredicate, handling the case when /// we are expanding code for a SCEVEqualPredicate. + Value *expandEqualPredicate(const SCEVEqualPredicate *Pred, DebugLoc DbgLoc, + Instruction *Loc); + + /// deprecated{Please use the overload which requires a DebugLoc.} Value *expandEqualPredicate(const SCEVEqualPredicate *Pred, Instruction *Loc); /// Generates code that evaluates if the \p AR expression will overflow. + Value *generateOverflowCheck(const SCEVAddRecExpr *AR, DebugLoc DbgLoc, + Instruction *Loc, bool Signed); + + /// deprecated{Please use the overload which requires a DebugLoc.} Value *generateOverflowCheck(const SCEVAddRecExpr *AR, Instruction *Loc, bool Signed); /// A specialized variant of expandCodeForPredicate, handling the case when /// we are expanding code for a SCEVWrapPredicate. + Value *expandWrapPredicate(const SCEVWrapPredicate *P, DebugLoc DbgLoc, + Instruction *Loc); + + /// deprecated{Please use the overload which requires a DebugLoc.} Value *expandWrapPredicate(const SCEVWrapPredicate *P, Instruction *Loc); /// A specialized variant of expandCodeForPredicate, handling the case when /// we are expanding code for a SCEVUnionPredicate. + Value *expandUnionPredicate(const SCEVUnionPredicate *Pred, DebugLoc DbgLoc, + Instruction *Loc); + + /// deprecated{Please use the overload which requires a DebugLoc.} Value *expandUnionPredicate(const SCEVUnionPredicate *Pred, Instruction *Loc); + /// Get the debug location which should be used for instructions without + /// a clear correspondence to a source location. + DebugLoc getUnknownDebugLocation(Instruction *Loc = nullptr) { + return IRBuilderBase::getUnknownDebugLocation(Loc); + } + /// Set the current IV increment loop and position. void setIVIncInsertPos(const Loop *L, Instruction *Pos) { assert(!CanonicalMode && @@ -262,9 +297,11 @@ /// expandCodeFor() are going to be made with the same insert point and the /// insert point may be moved during one of the expansions (e.g. if the /// insert point is not a block terminator). + /// + /// Note: This does not change the current debug location. void setInsertPoint(Instruction *IP) { assert(IP); - Builder.SetInsertPoint(IP); + Builder.setInsertPoint(IP, CurDbgLoc); } /// Clear the current insertion point. This is useful if the instruction @@ -384,6 +421,12 @@ Instruction *Pos, PHINode *LoopPhi); void fixupInsertPoints(Instruction *I); + + void setInsertPoint(BasicBlock *BB, BasicBlock::iterator I); + + void setDbgLoc(DebugLoc DbgLoc); + + void setInsertPointAndDbgLoc(Instruction *I, DebugLoc DbgLoc); }; } Index: lib/Analysis/ScalarEvolutionExpander.cpp =================================================================== --- lib/Analysis/ScalarEvolutionExpander.cpp +++ lib/Analysis/ScalarEvolutionExpander.cpp @@ -195,7 +195,6 @@ } // Save the original insertion point so we can restore it when we're done. - DebugLoc Loc = Builder.GetInsertPoint()->getDebugLoc(); SCEVInsertPointGuard Guard(Builder, this); // Move the insertion point out of as many loops as we can. @@ -205,12 +204,11 @@ if (!Preheader) break; // Ok, move up a level. - Builder.SetInsertPoint(Preheader->getTerminator()); + setInsertPoint(Preheader->getTerminator()); } // If we haven't found this binop, insert it. Instruction *BO = cast(Builder.CreateBinOp(Opcode, LHS, RHS)); - BO->setDebugLoc(Loc); rememberInstruction(BO); return BO; @@ -440,7 +438,7 @@ // would obviously be folded away). Value *Scaled = ScaledOps.empty() ? Constant::getNullValue(Ty) : - expandCodeFor(SE.getAddExpr(ScaledOps), Ty); + expandCodeFor(SE.getAddExpr(ScaledOps), CurDbgLoc, Ty); GepIndices.push_back(Scaled); // Collect struct field index operands. @@ -495,7 +493,7 @@ SE.DT.dominates(cast(V), &*Builder.GetInsertPoint())); // Expand the operands for a plain byte offset. - Value *Idx = expandCodeFor(SE.getAddExpr(Ops), Ty); + Value *Idx = expandCodeFor(SE.getAddExpr(Ops), CurDbgLoc, Ty); // Fold a GEP with constant operands. if (Constant *CLHS = dyn_cast(V)) @@ -532,7 +530,7 @@ if (!Preheader) break; // Ok, move up a level. - Builder.SetInsertPoint(Preheader->getTerminator()); + setInsertPoint(Preheader->getTerminator()); } // Emit a GEP. @@ -559,7 +557,7 @@ if (!Preheader) break; // Ok, move up a level. - Builder.SetInsertPoint(Preheader->getTerminator()); + setInsertPoint(Preheader->getTerminator()); } // Insert a pretty getelementptr. Note that this GEP is not marked inbounds, @@ -714,13 +712,13 @@ Sum = expandAddToGEP(NewOps.begin(), NewOps.end(), PTy, Ty, expand(Op)); } else if (Op->isNonConstantNegative()) { // Instead of doing a negate and add, just do a subtract. - Value *W = expandCodeFor(SE.getNegativeSCEV(Op), Ty); + Value *W = expandCodeFor(SE.getNegativeSCEV(Op), CurDbgLoc, Ty); Sum = InsertNoopCastOfTo(Sum, Ty); Sum = InsertBinop(Instruction::Sub, Sum, W); ++I; } else { // A simple add. - Value *W = expandCodeFor(Op, Ty); + Value *W = expandCodeFor(Op, CurDbgLoc, Ty); Sum = InsertNoopCastOfTo(Sum, Ty); // Canonicalize a constant to the RHS. if (isa(Sum)) std::swap(Sum, W); @@ -771,7 +769,7 @@ // Calculate powers with exponents 1, 2, 4, 8 etc. and include those of them // that are needed into the result. - Value *P = expandCodeFor(I->second, Ty); + Value *P = expandCodeFor(I->second, CurDbgLoc, Ty); Value *Result = nullptr; if (Exponent & 1) Result = P; @@ -819,7 +817,7 @@ Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) { Type *Ty = SE.getEffectiveSCEVType(S->getType()); - Value *LHS = expandCodeFor(S->getLHS(), Ty); + Value *LHS = expandCodeFor(S->getLHS(), CurDbgLoc, Ty); if (const SCEVConstant *SC = dyn_cast(S->getRHS())) { const APInt &RHS = SC->getAPInt(); if (RHS.isPowerOf2()) @@ -827,7 +825,7 @@ ConstantInt::get(Ty, RHS.logBase2())); } - Value *RHS = expandCodeFor(S->getRHS(), Ty); + Value *RHS = expandCodeFor(S->getRHS(), CurDbgLoc, Ty); return InsertBinop(Instruction::UDiv, LHS, RHS); } @@ -951,7 +949,7 @@ BasicBlock::iterator It(*I); BasicBlock::iterator NewInsertPt = std::next(It); if (Builder.GetInsertPoint() == It) - Builder.SetInsertPoint(&*NewInsertPt); + setInsertPoint(&*NewInsertPt); for (auto *InsertPtGuard : InsertPointGuards) if (InsertPtGuard->GetInsertPoint() == It) InsertPtGuard->SetInsertPoint(NewInsertPt); @@ -1223,7 +1221,7 @@ // Expand code for the start value into the loop preheader. assert(L->getLoopPreheader() && "Can't expand add recurrences without a loop preheader!"); - Value *StartV = expandCodeFor(Normalized->getStart(), ExpandTy, + Value *StartV = expandCodeFor(Normalized->getStart(), CurDbgLoc, ExpandTy, L->getLoopPreheader()->getTerminator()); // StartV must have been be inserted into L's preheader to dominate the new @@ -1242,7 +1240,8 @@ if (useSubtract) Step = SE.getNegativeSCEV(Step); // Expand the step somewhere that dominates the loop header. - Value *StepV = expandCodeFor(Step, IntTy, &L->getHeader()->front()); + Value *StepV = + expandCodeFor(Step, CurDbgLoc, IntTy, &L->getHeader()->front()); // The no-wrap behavior proved by IsIncrement(NUW|NSW) is only applicable if // we actually do emit an addition. It does not apply if we emit a @@ -1252,7 +1251,7 @@ // Create the PHI. BasicBlock *Header = L->getHeader(); - Builder.SetInsertPoint(Header, Header->begin()); + setInsertPoint(Header, Header->begin()); pred_iterator HPB = pred_begin(Header), HPE = pred_end(Header); PHINode *PN = Builder.CreatePHI(ExpandTy, std::distance(HPB, HPE), Twine(IVName) + ".iv"); @@ -1273,7 +1272,7 @@ // instructions at IVIncInsertPos. Instruction *InsertPos = L == IVIncInsertLoop ? IVIncInsertPos : Pred->getTerminator(); - Builder.SetInsertPoint(InsertPos); + setInsertPoint(InsertPos); Value *IncV = expandIVInc(PN, StepV, L, ExpandTy, IntTy, useSubtract); if (isa(IncV)) { @@ -1388,7 +1387,7 @@ { // Expand the step somewhere that dominates the loop header. SCEVInsertPointGuard Guard(Builder, this); - StepV = expandCodeFor(Step, IntTy, &L->getHeader()->front()); + StepV = expandCodeFor(Step, CurDbgLoc, IntTy, &L->getHeader()->front()); } Result = expandIVInc(PN, StepV, L, ExpandTy, IntTy, useSubtract); } @@ -1408,8 +1407,8 @@ } // Invert the result. if (InvertStep) { - Result = Builder.CreateSub(expandCodeFor(Normalized->getStart(), TruncTy), - Result); + Result = Builder.CreateSub( + expandCodeFor(Normalized->getStart(), CurDbgLoc, TruncTy), Result); rememberInstruction(Result); } } @@ -1419,7 +1418,7 @@ assert(S->isAffine() && "Can't linearly scale non-affine recurrences."); Result = InsertNoopCastOfTo(Result, IntTy); Result = Builder.CreateMul(Result, - expandCodeFor(PostLoopScale, IntTy)); + expandCodeFor(PostLoopScale, CurDbgLoc, IntTy)); rememberInstruction(Result); } @@ -1427,7 +1426,7 @@ if (PostLoopOffset) { if (PointerType *PTy = dyn_cast(ExpandTy)) { if (Result->getType()->isIntegerTy()) { - Value *Base = expandCodeFor(PostLoopOffset, ExpandTy); + Value *Base = expandCodeFor(PostLoopOffset, CurDbgLoc, ExpandTy); const SCEV *const OffsetArray[1] = {SE.getUnknown(Result)}; Result = expandAddToGEP(OffsetArray, OffsetArray + 1, PTy, IntTy, Base); } else { @@ -1437,8 +1436,8 @@ } } else { Result = InsertNoopCastOfTo(Result, IntTy); - Result = Builder.CreateAdd(Result, - expandCodeFor(PostLoopOffset, IntTy)); + Result = Builder.CreateAdd( + Result, expandCodeFor(PostLoopOffset, CurDbgLoc, IntTy)); rememberInstruction(Result); } } @@ -1470,8 +1469,8 @@ S->getNoWrapFlags(SCEV::FlagNW))); BasicBlock::iterator NewInsertPt = findInsertPointAfter(cast(V), Builder.GetInsertBlock()); - V = expandCodeFor(SE.getTruncateExpr(SE.getUnknown(V), Ty), nullptr, - &*NewInsertPt); + V = expandCodeFor(SE.getTruncateExpr(SE.getUnknown(V), Ty), CurDbgLoc, + nullptr, &*NewInsertPt); return V; } @@ -1536,7 +1535,7 @@ Instruction *Add = BinaryOperator::CreateAdd(CanonicalIV, One, "indvar.next", HP->getTerminator()); - Add->setDebugLoc(HP->getTerminator()->getDebugLoc()); + Add->setDebugLoc(CurDbgLoc); rememberInstruction(Add); CanonicalIV->addIncoming(Add, HP); } else { @@ -1586,7 +1585,7 @@ Value *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) { Type *Ty = SE.getEffectiveSCEVType(S->getType()); - Value *V = expandCodeFor(S->getOperand(), + Value *V = expandCodeFor(S->getOperand(), CurDbgLoc, SE.getEffectiveSCEVType(S->getOperand()->getType())); Value *I = Builder.CreateTrunc(V, Ty); rememberInstruction(I); @@ -1595,7 +1594,7 @@ Value *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) { Type *Ty = SE.getEffectiveSCEVType(S->getType()); - Value *V = expandCodeFor(S->getOperand(), + Value *V = expandCodeFor(S->getOperand(), CurDbgLoc, SE.getEffectiveSCEVType(S->getOperand()->getType())); Value *I = Builder.CreateZExt(V, Ty); rememberInstruction(I); @@ -1604,7 +1603,7 @@ Value *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) { Type *Ty = SE.getEffectiveSCEVType(S->getType()); - Value *V = expandCodeFor(S->getOperand(), + Value *V = expandCodeFor(S->getOperand(), CurDbgLoc, SE.getEffectiveSCEVType(S->getOperand()->getType())); Value *I = Builder.CreateSExt(V, Ty); rememberInstruction(I); @@ -1621,7 +1620,7 @@ Ty = SE.getEffectiveSCEVType(Ty); LHS = InsertNoopCastOfTo(LHS, Ty); } - Value *RHS = expandCodeFor(S->getOperand(i), Ty); + Value *RHS = expandCodeFor(S->getOperand(i), CurDbgLoc, Ty); Value *ICmp = Builder.CreateICmpSGT(LHS, RHS); rememberInstruction(ICmp); Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "smax"); @@ -1645,7 +1644,7 @@ Ty = SE.getEffectiveSCEVType(Ty); LHS = InsertNoopCastOfTo(LHS, Ty); } - Value *RHS = expandCodeFor(S->getOperand(i), Ty); + Value *RHS = expandCodeFor(S->getOperand(i), CurDbgLoc, Ty); Value *ICmp = Builder.CreateICmpUGT(LHS, RHS); rememberInstruction(ICmp); Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "umax"); @@ -1659,14 +1658,15 @@ return LHS; } -Value *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty, +Value *SCEVExpander::expandCodeFor(const SCEV *SH, DebugLoc DbgLoc, Type *Ty, Instruction *IP) { - setInsertPoint(IP); - return expandCodeFor(SH, Ty); + setInsertPointAndDbgLoc(IP, DbgLoc); + return expandCodeFor(SH, DbgLoc, Ty); } -Value *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty) { +Value *SCEVExpander::expandCodeFor(const SCEV *SH, DebugLoc DbgLoc, Type *Ty) { // Expand the code for this SCEV. + setDbgLoc(DbgLoc); Value *V = expand(SH); if (Ty) { assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) && @@ -1747,7 +1747,7 @@ return I->second; SCEVInsertPointGuard Guard(Builder, this); - Builder.SetInsertPoint(InsertPt); + setInsertPoint(InsertPt); // Expand the expression into instructions. ScalarEvolution::ValueOffsetPair VO = FindValueInExprValueMap(S, InsertPt); @@ -1798,9 +1798,9 @@ /// canonical induction variable of the specified type for the specified /// loop (inserting one if there is none). A canonical induction variable /// starts at zero and steps by one on each iteration. -PHINode * -SCEVExpander::getOrInsertCanonicalInductionVariable(const Loop *L, - Type *Ty) { +PHINode *SCEVExpander::getOrInsertCanonicalInductionVariable(const Loop *L, + DebugLoc DbgLoc, + Type *Ty) { assert(Ty->isIntegerTy() && "Can only insert integer induction variables!"); // Build a SCEV for {0,+,1}. @@ -1810,8 +1810,8 @@ // Emit code for it. SCEVInsertPointGuard Guard(Builder, this); - PHINode *V = - cast(expandCodeFor(H, nullptr, &L->getHeader()->front())); + PHINode *V = cast( + expandCodeFor(H, CurDbgLoc, nullptr, &L->getHeader()->front())); return V; } @@ -2089,33 +2089,36 @@ } Value *SCEVExpander::expandCodeForPredicate(const SCEVPredicate *Pred, - Instruction *IP) { + DebugLoc DbgLoc, Instruction *IP) { assert(IP); switch (Pred->getKind()) { case SCEVPredicate::P_Union: - return expandUnionPredicate(cast(Pred), IP); + return expandUnionPredicate(cast(Pred), DbgLoc, IP); case SCEVPredicate::P_Equal: - return expandEqualPredicate(cast(Pred), IP); + return expandEqualPredicate(cast(Pred), DbgLoc, IP); case SCEVPredicate::P_Wrap: { auto *AddRecPred = cast(Pred); - return expandWrapPredicate(AddRecPred, IP); + return expandWrapPredicate(AddRecPred, DbgLoc, IP); } } llvm_unreachable("Unknown SCEV predicate type"); } Value *SCEVExpander::expandEqualPredicate(const SCEVEqualPredicate *Pred, - Instruction *IP) { - Value *Expr0 = expandCodeFor(Pred->getLHS(), Pred->getLHS()->getType(), IP); - Value *Expr1 = expandCodeFor(Pred->getRHS(), Pred->getRHS()->getType(), IP); + DebugLoc DbgLoc, Instruction *IP) { + Value *Expr0 = + expandCodeFor(Pred->getLHS(), DbgLoc, Pred->getLHS()->getType(), IP); + Value *Expr1 = + expandCodeFor(Pred->getRHS(), DbgLoc, Pred->getRHS()->getType(), IP); - Builder.SetInsertPoint(IP); + setInsertPointAndDbgLoc(IP, DbgLoc); auto *I = Builder.CreateICmpNE(Expr0, Expr1, "ident.check"); return I; } Value *SCEVExpander::generateOverflowCheck(const SCEVAddRecExpr *AR, - Instruction *Loc, bool Signed) { + DebugLoc DbgLoc, Instruction *Loc, + bool Signed) { assert(AR->isAffine() && "Cannot generate RT check for " "non-affine expression"); @@ -2137,20 +2140,21 @@ // and |Step| * Backedge doesn't unsigned overflow. IntegerType *CountTy = IntegerType::get(Loc->getContext(), SrcBits); - Builder.SetInsertPoint(Loc); - Value *TripCountVal = expandCodeFor(ExitCount, CountTy, Loc); + setInsertPointAndDbgLoc(Loc, DbgLoc); + Value *TripCountVal = expandCodeFor(ExitCount, DbgLoc, CountTy, Loc); IntegerType *Ty = IntegerType::get(Loc->getContext(), SE.getTypeSizeInBits(AR->getType())); - Value *StepValue = expandCodeFor(Step, Ty, Loc); - Value *NegStepValue = expandCodeFor(SE.getNegativeSCEV(Step), Ty, Loc); - Value *StartValue = expandCodeFor(Start, Ty, Loc); + Value *StepValue = expandCodeFor(Step, DbgLoc, Ty, Loc); + Value *NegStepValue = + expandCodeFor(SE.getNegativeSCEV(Step), DbgLoc, Ty, Loc); + Value *StartValue = expandCodeFor(Start, DbgLoc, Ty, Loc); ConstantInt *Zero = ConstantInt::get(Loc->getContext(), APInt::getNullValue(DstBits)); - Builder.SetInsertPoint(Loc); + setInsertPointAndDbgLoc(Loc, DbgLoc); // Compute |Step| Value *StepCompare = Builder.CreateICmp(ICmpInst::ICMP_SLT, StepValue, Zero); Value *AbsStep = Builder.CreateSelect(StepCompare, NegStepValue, StepValue); @@ -2200,17 +2204,17 @@ } Value *SCEVExpander::expandWrapPredicate(const SCEVWrapPredicate *Pred, - Instruction *IP) { + DebugLoc DbgLoc, Instruction *IP) { const auto *A = cast(Pred->getExpr()); Value *NSSWCheck = nullptr, *NUSWCheck = nullptr; // Add a check for NUSW if (Pred->getFlags() & SCEVWrapPredicate::IncrementNUSW) - NUSWCheck = generateOverflowCheck(A, IP, false); + NUSWCheck = generateOverflowCheck(A, DbgLoc, IP, false); // Add a check for NSSW if (Pred->getFlags() & SCEVWrapPredicate::IncrementNSSW) - NSSWCheck = generateOverflowCheck(A, IP, true); + NSSWCheck = generateOverflowCheck(A, DbgLoc, IP, true); if (NUSWCheck && NSSWCheck) return Builder.CreateOr(NUSWCheck, NSSWCheck); @@ -2225,20 +2229,77 @@ } Value *SCEVExpander::expandUnionPredicate(const SCEVUnionPredicate *Union, - Instruction *IP) { + DebugLoc DbgLoc, Instruction *IP) { auto *BoolType = IntegerType::get(IP->getContext(), 1); Value *Check = ConstantInt::getNullValue(BoolType); // Loop over all checks in this set. for (auto Pred : Union->getPredicates()) { - auto *NextCheck = expandCodeForPredicate(Pred, IP); - Builder.SetInsertPoint(IP); + auto *NextCheck = expandCodeForPredicate(Pred, DbgLoc, IP); + setInsertPointAndDbgLoc(IP, DbgLoc); Check = Builder.CreateOr(Check, NextCheck); } return Check; } +/// Deprecated instruction generation methods. + +PHINode *SCEVExpander::getOrInsertCanonicalInductionVariable(const Loop *L, + Type *Ty) { + return getOrInsertCanonicalInductionVariable(L, getUnknownDebugLocation(), + Ty); +} + +Value *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I) { + return expandCodeFor(SH, getUnknownDebugLocation(I), Ty, I); +} + +Value *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty) { + return expandCodeFor(SH, getUnknownDebugLocation(), Ty); +} + +Value *SCEVExpander::expandCodeForPredicate(const SCEVPredicate *Pred, + Instruction *Loc) { + return expandCodeForPredicate(Pred, getUnknownDebugLocation(Loc), Loc); +} + +Value *SCEVExpander::expandEqualPredicate(const SCEVEqualPredicate *Pred, + Instruction *Loc) { + return expandEqualPredicate(Pred, getUnknownDebugLocation(Loc), Loc); +} + +Value *SCEVExpander::generateOverflowCheck(const SCEVAddRecExpr *AR, + Instruction *Loc, bool Signed) { + return generateOverflowCheck(AR, getUnknownDebugLocation(Loc), Loc, Signed); +} + +Value *SCEVExpander::expandWrapPredicate(const SCEVWrapPredicate *P, + Instruction *Loc) { + return expandWrapPredicate(P, getUnknownDebugLocation(Loc), Loc); +} + +Value *SCEVExpander::expandUnionPredicate(const SCEVUnionPredicate *Pred, + Instruction *Loc) { + return expandUnionPredicate(Pred, getUnknownDebugLocation(Loc), Loc); +} + +/// Helpers for insertion point and debug location updates. + +void SCEVExpander::setInsertPoint(BasicBlock *BB, BasicBlock::iterator I) { + Builder.setInsertPoint(BB, I, CurDbgLoc); +} + +void SCEVExpander::setInsertPointAndDbgLoc(Instruction *I, DebugLoc DbgLoc) { + CurDbgLoc = DbgLoc; + Builder.setInsertPoint(I, DbgLoc); +} + +void SCEVExpander::setDbgLoc(DebugLoc DbgLoc) { + CurDbgLoc = DbgLoc; + Builder.SetCurrentDebugLocation(CurDbgLoc); +} + namespace { // Search for a SCEV subexpression that is not safe to expand. Any expression // that may expand to a !isSafeToSpeculativelyExecute value is unsafe, namely Index: unittests/Analysis/ScalarEvolutionTest.cpp =================================================================== --- unittests/Analysis/ScalarEvolutionTest.cpp +++ unittests/Analysis/ScalarEvolutionTest.cpp @@ -15,6 +15,7 @@ #include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/AsmParser/Parser.h" #include "llvm/IR/Constants.h" +#include "llvm/IR/DIBuilder.h" #include "llvm/IR/Dominators.h" #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/IRBuilder.h" @@ -27,6 +28,17 @@ #include "gtest/gtest.h" namespace llvm { + +// Used by gtest to pretty-print EXPECT failures. +void PrintTo(const DebugLoc &L, ::std::ostream *os) { + std::string S; + { + raw_string_ostream SOS(S); + L.print(SOS); + } + *os << S; +} + namespace { // We use this fixture to ensure that we clean up ScalarEvolution before @@ -41,6 +53,7 @@ std::unique_ptr AC; std::unique_ptr DT; std::unique_ptr LI; + std::unique_ptr DIB; ScalarEvolutionsTest() : M("", Context), TLII(), TLI(TLII) {} @@ -51,6 +64,18 @@ return ScalarEvolution(F, TLI, *AC, *DT, *LI); } + DISubprogram *buildDebugInfo(Function &F) { + DIB = llvm::make_unique(M); + auto File = DIB->createFile("tmp.cpp", "/"); + auto CU = DIB->createCompileUnit(dwarf::DW_LANG_C_plus_plus_11, + DIB->createFile("tmp.cpp", "/"), "", true, + "", 0); + auto SPType = DIB->createSubroutineType(DIB->getOrCreateTypeArray(None)); + auto SP = DIB->createFunction(CU, F.getName(), F.getName(), File, 1, SPType, + false, true, 1); + return SP; + } + void runWithSE( Module &M, StringRef FuncName, function_ref Test) { @@ -59,6 +84,9 @@ ScalarEvolution SE = buildSE(*F); Test(*F, *LI, SE); } + + /// Useful for testing SCEVExpander. + std::tuple buildTestCaseForPR30213(); }; TEST_F(ScalarEvolutionsTest, SCEVUnknownRAUW) { @@ -137,7 +165,8 @@ EXPECT_EQ(S1, S2); } -TEST_F(ScalarEvolutionsTest, ExpandPtrTypeSCEV) { +std::tuple +ScalarEvolutionsTest::buildTestCaseForPR30213() { // It is to test the fix for PR30213. It exercises the branch in scev // expansion when the value in ValueOffsetPair is a ptr and the offset // is not divisible by the elem type size of value. @@ -184,7 +213,14 @@ SelectInst *Sel = SelectInst::Create(Cmp, Gep1, Gep2, "select", Br); CastInst *CastB = CastInst::CreateBitOrPointerCast(Sel, I32PtrTy, "bitcast2", Br); + return {F, CastB, Br}; +} +TEST_F(ScalarEvolutionsTest, ExpandPtrTypeSCEV) { + Function *F; + CastInst *CastB; + BranchInst *Br; + std::tie(F, CastB, Br) = buildTestCaseForPR30213(); ScalarEvolution SE = buildSE(*F); auto *S = SE.getSCEV(CastB); SCEVExpander Exp(SE, M.getDataLayout(), "expander"); @@ -201,6 +237,56 @@ EXPECT_TRUE(isa(Gep->getOperand(1))); EXPECT_EQ(cast(Gep->getOperand(1))->getSExtValue(), -1); EXPECT_TRUE(isa(Gep->getPrevNode())); + + // Make sure we get an empty debug loc when the function has no debug info. + EXPECT_EQ(cast(V)->getDebugLoc(), DebugLoc()); +} + +TEST_F(ScalarEvolutionsTest, ExpandWithSpecifiedDebugLoc) { + Function *F; + CastInst *CastB; + BranchInst *Br; + std::tie(F, CastB, Br) = buildTestCaseForPR30213(); + ScalarEvolution SE = buildSE(*F); + auto *S = SE.getSCEV(CastB); + SCEVExpander Exp(SE, M.getDataLayout(), "expander"); + DISubprogram *SP = buildDebugInfo(*F); + + // Attach DL2 to inserted insts, instead of copying the debug loc from the + // insertion point. + DebugLoc DL1 = DebugLoc::get(1, 0, SP); + Br->setDebugLoc(DL1); + DebugLoc DL2 = DebugLoc::get(1, 1, SP); + Value *V = + Exp.expandCodeFor(cast(S)->getOperand(1), DL2, nullptr, Br); + + Instruction *Gep = cast(V)->getPrevNode(); + EXPECT_EQ(Gep->getDebugLoc(), DL2); + EXPECT_EQ(cast(Gep->getPrevNode())->getDebugLoc(), DL2); +} + +TEST_F(ScalarEvolutionsTest, ExpandWithDefaultDebugLoc) { + Function *F; + CastInst *CastB; + BranchInst *Br; + std::tie(F, CastB, Br) = buildTestCaseForPR30213(); + ScalarEvolution SE = buildSE(*F); + auto *S = SE.getSCEV(CastB); + SCEVExpander Exp(SE, M.getDataLayout(), "expander"); + DISubprogram *SP = buildDebugInfo(*F); + + // If the function has debug info, but no debug loc was specified in the call + // to expandCodeFor(), use the default location. + DebugLoc DL1 = DebugLoc::get(1, 0, SP); + Br->setDebugLoc(DL1); + Value *V = + Exp.expandCodeFor(cast(S)->getOperand(1), nullptr, Br); + + DebugLoc DefaultLoc = DebugLoc::get(0, 0, SP); + Instruction *Gep = cast(V)->getPrevNode(); + EXPECT_EQ(Gep->getDebugLoc(), DefaultLoc); + EXPECT_EQ(cast(Gep->getPrevNode())->getDebugLoc(), DefaultLoc); + EXPECT_EQ(DefaultLoc, Exp.getUnknownDebugLocation(Br)); } static Instruction *getInstructionByName(Function &F, StringRef Name) {