Index: llvm/lib/Analysis/ValueTracking.cpp =================================================================== --- llvm/lib/Analysis/ValueTracking.cpp +++ llvm/lib/Analysis/ValueTracking.cpp @@ -131,12 +131,17 @@ // Given the provided Value and, potentially, a context instruction, return // the preferred context instruction (if any). -static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) { +static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI, + bool UseInstrInfo) { // If we've been provided with a context instruction, then use that (provided // it has been inserted). if (CxtI && CxtI->getParent()) return CxtI; + // If we don't use instruction info, we can't return Value itself + if (!UseInstrInfo) + return nullptr; + // If the value is really an already-inserted instruction, then use that. CxtI = dyn_cast(V); if (CxtI && CxtI->getParent()) @@ -145,12 +150,17 @@ return nullptr; } -static const Instruction *safeCxtI(const Value *V1, const Value *V2, const Instruction *CxtI) { +static const Instruction *safeCxtI(const Value *V1, const Value *V2, + const Instruction *CxtI, bool UseInstrInfo) { // If we've been provided with a context instruction, then use that (provided // it has been inserted). if (CxtI && CxtI->getParent()) return CxtI; + // If we don't use instruction info, we can't return Value itself + if (!UseInstrInfo) + return nullptr; + // If the value is really an already-inserted instruction, then use that. CxtI = dyn_cast(V1); if (CxtI && CxtI->getParent()) @@ -225,7 +235,7 @@ const DominatorTree *DT, OptimizationRemarkEmitter *ORE, bool UseInstrInfo) { ::computeKnownBits(V, Known, Depth, - Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE)); + Query(DL, AC, safeCxtI(V, CxtI, UseInstrInfo), DT, UseInstrInfo, ORE)); } void llvm::computeKnownBits(const Value *V, const APInt &DemandedElts, @@ -234,7 +244,7 @@ const Instruction *CxtI, const DominatorTree *DT, OptimizationRemarkEmitter *ORE, bool UseInstrInfo) { ::computeKnownBits(V, DemandedElts, Known, Depth, - Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE)); + Query(DL, AC, safeCxtI(V, CxtI, UseInstrInfo), DT, UseInstrInfo, ORE)); } static KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts, @@ -250,7 +260,7 @@ OptimizationRemarkEmitter *ORE, bool UseInstrInfo) { return ::computeKnownBits( - V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE)); + V, Depth, Query(DL, AC, safeCxtI(V, CxtI, UseInstrInfo), DT, UseInstrInfo, ORE)); } KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts, @@ -261,7 +271,7 @@ bool UseInstrInfo) { return ::computeKnownBits( V, DemandedElts, Depth, - Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE)); + Query(DL, AC, safeCxtI(V, CxtI, UseInstrInfo), DT, UseInstrInfo, ORE)); } bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS, @@ -329,7 +339,7 @@ AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, bool UseInstrInfo) { return ::isKnownToBeAPowerOfTwo( - V, OrZero, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo)); + V, OrZero, Depth, Query(DL, AC, safeCxtI(V, CxtI, UseInstrInfo), DT, UseInstrInfo)); } static bool isKnownNonZero(const Value *V, const APInt &DemandedElts, @@ -341,7 +351,7 @@ AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, bool UseInstrInfo) { return ::isKnownNonZero(V, Depth, - Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo)); + Query(DL, AC, safeCxtI(V, CxtI, UseInstrInfo), DT, UseInstrInfo)); } bool llvm::isKnownNonNegative(const Value *V, const DataLayout &DL, @@ -381,7 +391,7 @@ const Instruction *CxtI, const DominatorTree *DT, bool UseInstrInfo) { return ::isKnownNonEqual(V1, V2, 0, - Query(DL, AC, safeCxtI(V2, V1, CxtI), DT, + Query(DL, AC, safeCxtI(V2, V1, CxtI, UseInstrInfo), DT, UseInstrInfo, /*ORE=*/nullptr)); } @@ -393,7 +403,7 @@ AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, bool UseInstrInfo) { return ::MaskedValueIsZero( - V, Mask, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo)); + V, Mask, Depth, Query(DL, AC, safeCxtI(V, CxtI, UseInstrInfo), DT, UseInstrInfo)); } static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts, @@ -417,7 +427,7 @@ const Instruction *CxtI, const DominatorTree *DT, bool UseInstrInfo) { return ::ComputeNumSignBits( - V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo)); + V, Depth, Query(DL, AC, safeCxtI(V, CxtI, UseInstrInfo), DT, UseInstrInfo)); } unsigned llvm::ComputeMaxSignificantBits(const Value *V, const DataLayout &DL, Index: llvm/lib/Transforms/Scalar/NewGVN.cpp =================================================================== --- llvm/lib/Transforms/Scalar/NewGVN.cpp +++ llvm/lib/Transforms/Scalar/NewGVN.cpp @@ -1074,9 +1074,6 @@ Value *Arg1, Value *Arg2, Instruction *I) const { auto *E = new (ExpressionAllocator) BasicExpression(2); - // TODO: we need to remove context instruction after Value Tracking - // can run without context instruction - const SimplifyQuery Q = SQ.getWithInstruction(I); E->setType(T); E->setOpcode(Opcode); @@ -1092,7 +1089,7 @@ E->op_push_back(lookupOperandLeader(Arg1)); E->op_push_back(lookupOperandLeader(Arg2)); - Value *V = simplifyBinOp(Opcode, E->getOperand(0), E->getOperand(1), Q); + Value *V = simplifyBinOp(Opcode, E->getOperand(0), E->getOperand(1), SQ); if (auto Simplified = checkExprResults(E, I, V)) { addAdditionalUsers(Simplified, I); return Simplified.Expr; @@ -1148,9 +1145,6 @@ NewGVN::ExprResult NewGVN::createExpression(Instruction *I) const { auto *E = new (ExpressionAllocator) BasicExpression(I->getNumOperands()); - // TODO: we need to remove context instruction after Value Tracking - // can run without context instruction - const SimplifyQuery Q = SQ.getWithInstruction(I); bool AllConstant = setBasicExpressionInfo(I, E); @@ -1179,7 +1173,7 @@ assert((E->getOperand(0)->getType() == I->getOperand(0)->getType() && E->getOperand(1)->getType() == I->getOperand(1)->getType())); Value *V = - simplifyCmpInst(Predicate, E->getOperand(0), E->getOperand(1), Q); + simplifyCmpInst(Predicate, E->getOperand(0), E->getOperand(1), SQ); if (auto Simplified = checkExprResults(E, I, V)) return Simplified; } else if (isa(I)) { @@ -1188,25 +1182,25 @@ assert(E->getOperand(1)->getType() == I->getOperand(1)->getType() && E->getOperand(2)->getType() == I->getOperand(2)->getType()); Value *V = simplifySelectInst(E->getOperand(0), E->getOperand(1), - E->getOperand(2), Q); + E->getOperand(2), SQ); if (auto Simplified = checkExprResults(E, I, V)) return Simplified; } } else if (I->isBinaryOp()) { Value *V = - simplifyBinOp(E->getOpcode(), E->getOperand(0), E->getOperand(1), Q); + simplifyBinOp(E->getOpcode(), E->getOperand(0), E->getOperand(1), SQ); if (auto Simplified = checkExprResults(E, I, V)) return Simplified; } else if (auto *CI = dyn_cast(I)) { Value *V = - simplifyCastInst(CI->getOpcode(), E->getOperand(0), CI->getType(), Q); + simplifyCastInst(CI->getOpcode(), E->getOperand(0), CI->getType(), SQ); if (auto Simplified = checkExprResults(E, I, V)) return Simplified; } else if (auto *GEPI = dyn_cast(I)) { Value *V = simplifyGEPInst(GEPI->getSourceElementType(), *E->op_begin(), makeArrayRef(std::next(E->op_begin()), E->op_end()), - GEPI->isInBounds(), Q); + GEPI->isInBounds(), SQ); if (auto Simplified = checkExprResults(E, I, V)) return Simplified; } else if (AllConstant) {