128#define DEBUG_TYPE "newgvn"
130STATISTIC(NumGVNInstrDeleted,
"Number of instructions deleted");
131STATISTIC(NumGVNBlocksDeleted,
"Number of blocks deleted");
132STATISTIC(NumGVNOpsSimplified,
"Number of Expressions simplified");
133STATISTIC(NumGVNPhisAllSame,
"Number of PHIs whos arguments are all the same");
135 "Maximum Number of iterations it took to converge GVN");
136STATISTIC(NumGVNLeaderChanges,
"Number of leader changes");
137STATISTIC(NumGVNSortedLeaderChanges,
"Number of sorted leader changes");
139 "Number of avoided sorted leader changes");
140STATISTIC(NumGVNDeadStores,
"Number of redundant/dead stores eliminated");
141STATISTIC(NumGVNPHIOfOpsCreated,
"Number of PHI of ops created");
143 "Number of things eliminated using PHI of ops");
145 "Controls which instructions are value numbered");
147 "Controls which instructions we create phi of ops for");
182 TarjanSCC() : Components(1) {}
184 void Start(
const Instruction *Start) {
185 if (Root.lookup(Start) == 0)
189 const SmallPtrSetImpl<const Value *> &getComponentFor(
const Value *V)
const {
190 unsigned ComponentID = ValueToComponent.lookup(V);
193 "Asking for a component for a value we never processed");
194 return Components[ComponentID];
198 void FindSCC(
const Instruction *
I) {
201 unsigned int OurDFS = DFSNum;
202 for (
const auto &
Op :
I->operands()) {
204 if (Root.lookup(
Op) == 0)
206 if (!InComponent.count(
Op))
207 Root[
I] = std::min(Root.lookup(
I), Root.lookup(
Op));
214 if (Root.lookup(
I) == OurDFS) {
215 unsigned ComponentID = Components.size();
216 Components.resize(Components.size() + 1);
220 InComponent.insert(
I);
221 ValueToComponent[
I] = ComponentID;
223 while (!Stack.empty() && Root.lookup(Stack.back()) >= OurDFS) {
224 auto *
Member = Stack.back();
227 InComponent.insert(Member);
228 ValueToComponent[
Member] = ComponentID;
237 unsigned int DFSNum = 1;
238 SmallPtrSet<const Value *, 8> InComponent;
239 DenseMap<const Value *, unsigned int> Root;
240 SmallVector<const Value *, 8> Stack;
246 DenseMap<const Value *, unsigned> ValueToComponent;
287class CongruenceClass {
289 using MemberType =
Value;
290 using MemberSet = SmallPtrSet<MemberType *, 4>;
291 using MemoryMemberType = MemoryPhi;
292 using MemoryMemberSet = SmallPtrSet<const MemoryMemberType *, 2>;
294 explicit CongruenceClass(
unsigned ID) : ID(ID) {}
295 CongruenceClass(
unsigned ID, std::pair<Value *, unsigned int> Leader,
297 : ID(ID), RepLeader(Leader), DefiningExpr(
E) {}
299 unsigned getID()
const {
return ID; }
306 return empty() && memory_empty();
310 Value *getLeader()
const {
return RepLeader.first; }
311 void setLeader(std::pair<Value *, unsigned int> Leader) {
314 const std::pair<Value *, unsigned int> &getNextLeader()
const {
317 void resetNextLeader() { NextLeader = {
nullptr, ~0}; }
318 bool addPossibleLeader(std::pair<Value *, unsigned int> LeaderPair) {
319 if (LeaderPair.second < RepLeader.second) {
320 NextLeader = RepLeader;
321 RepLeader = LeaderPair;
323 }
else if (LeaderPair.second < NextLeader.second) {
324 NextLeader = LeaderPair;
330 void setStoredValue(
Value *Leader) { RepStoredValue = Leader; }
331 const MemoryAccess *getMemoryLeader()
const {
return RepMemoryAccess; }
332 void setMemoryLeader(
const MemoryAccess *Leader) { RepMemoryAccess = Leader; }
335 const Expression *getDefiningExpr()
const {
return DefiningExpr; }
338 bool empty()
const {
return Members.empty(); }
339 unsigned size()
const {
return Members.size(); }
342 void insert(MemberType *M) { Members.insert(M); }
343 void erase(MemberType *M) { Members.erase(M); }
347 bool memory_empty()
const {
return MemoryMembers.empty(); }
348 unsigned memory_size()
const {
return MemoryMembers.size(); }
350 return MemoryMembers.begin();
353 return MemoryMembers.end();
356 return make_range(memory_begin(), memory_end());
359 void memory_insert(
const MemoryMemberType *M) { MemoryMembers.insert(M); }
360 void memory_erase(
const MemoryMemberType *M) { MemoryMembers.erase(M); }
363 unsigned getStoreCount()
const {
return StoreCount; }
364 void incStoreCount() { ++StoreCount; }
365 void decStoreCount() {
366 assert(StoreCount != 0 &&
"Store count went negative");
371 bool definesNoMemory()
const {
return StoreCount == 0 && memory_empty(); }
375 bool isEquivalentTo(
const CongruenceClass *
Other)
const {
381 if (std::tie(StoreCount, RepLeader, RepStoredValue, RepMemoryAccess) !=
383 Other->RepMemoryAccess))
385 if (DefiningExpr !=
Other->DefiningExpr)
386 if (!DefiningExpr || !
Other->DefiningExpr ||
387 *DefiningExpr != *
Other->DefiningExpr)
390 if (Members.size() !=
Other->Members.size())
401 std::pair<Value *, unsigned int> RepLeader = {
nullptr, ~0
U};
406 std::pair<Value *, unsigned int> NextLeader = {
nullptr, ~0
U};
409 Value *RepStoredValue =
nullptr;
413 const MemoryAccess *RepMemoryAccess =
nullptr;
416 const Expression *DefiningExpr =
nullptr;
424 MemoryMemberSet MemoryMembers;
431struct ExactEqualsExpression {
434 explicit ExactEqualsExpression(
const Expression &E) : E(E) {}
436 hash_code getComputedHash()
const {
return E.getComputedHash(); }
439 return E.exactlyEquals(
Other);
446 auto Val =
static_cast<uintptr_t
>(-1);
447 Val <<= PointerLikeTypeTraits<const Expression *>::NumLowBitsAvailable;
448 return reinterpret_cast<const Expression *
>(Val);
452 auto Val =
static_cast<uintptr_t
>(~1U);
453 Val <<= PointerLikeTypeTraits<const Expression *>::NumLowBitsAvailable;
454 return reinterpret_cast<const Expression *
>(Val);
458 return E->getComputedHash();
462 return E.getComputedHash();
481 if (LHS->getComputedHash() != RHS->getComputedHash())
503 mutable TarjanSCC SCCFinder;
505 std::unique_ptr<PredicateInfo> PredInfo;
509 unsigned int NumFuncArgs = 0;
520 CongruenceClass *TOPClass =
nullptr;
521 std::vector<CongruenceClass *> CongruenceClasses;
522 unsigned NextCongruenceNum = 0;
557 ExpressionToPhiOfOps;
606 enum MemoryPhiState { MPS_Invalid, MPS_TOP, MPS_Equivalent, MPS_Unique };
607 DenseMap<const MemoryPhi *, MemoryPhiState> MemoryPhiState;
609 enum InstCycleState { ICS_Unknown, ICS_CycleFree, ICS_Cycle };
610 mutable DenseMap<const Instruction *, InstCycleState> InstCycleState;
613 using ExpressionClassMap = DenseMap<const Expression *, CongruenceClass *>;
614 ExpressionClassMap ExpressionToClass;
621 DeadExpression *SingletonDeadExpression =
nullptr;
624 SmallPtrSet<Value *, 8> LeaderChanges;
627 using BlockEdge = BasicBlockEdge;
628 DenseSet<BlockEdge> ReachableEdges;
629 SmallPtrSet<const BasicBlock *, 8> ReachableBlocks;
640 BitVector TouchedInstructions;
642 DenseMap<const BasicBlock *, std::pair<unsigned, unsigned>> BlockInstRange;
643 mutable DenseMap<const BitCastInst *, const Value *> PredicateSwapChoice;
647 DenseMap<const Value *, unsigned> ProcessedCount;
654 DenseMap<const Value *, unsigned> InstrDFS;
660 SmallPtrSet<Instruction *, 8> InstructionsToErase;
663 NewGVN(Function &
F, DominatorTree *DT, AssumptionCache *AC,
665 const DataLayout &
DL)
666 :
F(
F), DT(DT), TLI(TLI), AA(AA), MSSA(MSSA), AC(AC),
DL(
DL),
669 std::make_unique<PredicateInfo>(
F, *DT, *AC, ExpressionAllocator)),
670 SQ(
DL, TLI, DT, AC, nullptr,
false,
678 const Expression *Expr;
680 const PredicateBase *PredDep;
682 ExprResult(
const Expression *Expr,
Value *ExtraDep =
nullptr,
683 const PredicateBase *PredDep =
nullptr)
684 : Expr(Expr), ExtraDep(ExtraDep), PredDep(PredDep) {}
685 ExprResult(
const ExprResult &) =
delete;
686 ExprResult(ExprResult &&
Other)
688 Other.Expr =
nullptr;
689 Other.ExtraDep =
nullptr;
690 Other.PredDep =
nullptr;
692 ExprResult &operator=(
const ExprResult &
Other) =
delete;
693 ExprResult &operator=(ExprResult &&
Other) =
delete;
695 ~ExprResult() {
assert(!ExtraDep &&
"unhandled ExtraDep"); }
697 operator bool()
const {
return Expr; }
699 static ExprResult
none() {
return {
nullptr,
nullptr,
nullptr}; }
700 static ExprResult some(
const Expression *Expr,
Value *ExtraDep =
nullptr) {
701 return {Expr, ExtraDep,
nullptr};
703 static ExprResult some(
const Expression *Expr,
704 const PredicateBase *PredDep) {
705 return {Expr,
nullptr, PredDep};
707 static ExprResult some(
const Expression *Expr,
Value *ExtraDep,
708 const PredicateBase *PredDep) {
709 return {Expr, ExtraDep, PredDep};
714 ExprResult createExpression(Instruction *)
const;
715 const Expression *createBinaryExpression(
unsigned,
Type *,
Value *,
Value *,
716 Instruction *)
const;
720 using ValPair = std::pair<Value *, BasicBlock *>;
723 BasicBlock *,
bool &HasBackEdge,
724 bool &OriginalOpsConstant)
const;
725 const DeadExpression *createDeadExpression()
const;
726 const VariableExpression *createVariableExpression(
Value *)
const;
727 const ConstantExpression *createConstantExpression(Constant *)
const;
728 const Expression *createVariableOrConstant(
Value *V)
const;
729 const UnknownExpression *createUnknownExpression(Instruction *)
const;
730 const StoreExpression *createStoreExpression(StoreInst *,
731 const MemoryAccess *)
const;
732 LoadExpression *createLoadExpression(
Type *,
Value *, LoadInst *,
733 const MemoryAccess *)
const;
734 const CallExpression *createCallExpression(CallInst *,
735 const MemoryAccess *)
const;
736 const AggregateValueExpression *
737 createAggregateValueExpression(Instruction *)
const;
738 bool setBasicExpressionInfo(Instruction *, BasicExpression *)
const;
741 CongruenceClass *createCongruenceClass(
Value *Leader,
const Expression *
E) {
744 unsigned LeaderDFS = 0;
752 LeaderDFS = InstrToDFSNum(
I);
754 new CongruenceClass(NextCongruenceNum++, {Leader, LeaderDFS},
E);
755 CongruenceClasses.emplace_back(result);
759 CongruenceClass *createMemoryClass(MemoryAccess *MA) {
760 auto *CC = createCongruenceClass(
nullptr,
nullptr);
761 CC->setMemoryLeader(MA);
765 CongruenceClass *ensureLeaderOfMemoryClass(MemoryAccess *MA) {
766 auto *CC = getMemoryClass(MA);
767 if (CC->getMemoryLeader() != MA)
768 CC = createMemoryClass(MA);
772 CongruenceClass *createSingletonCongruenceClass(
Value *Member) {
773 CongruenceClass *CClass = createCongruenceClass(Member,
nullptr);
774 CClass->insert(Member);
775 ValueToClass[
Member] = CClass;
779 void initializeCongruenceClasses(Function &
F);
780 const Expression *makePossiblePHIOfOps(Instruction *,
781 SmallPtrSetImpl<Value *> &);
782 Value *findLeaderForInst(Instruction *ValueOp,
783 SmallPtrSetImpl<Value *> &Visited,
784 MemoryAccess *MemAccess, Instruction *OrigInst,
786 bool OpIsSafeForPHIOfOps(
Value *
Op,
const BasicBlock *PHIBlock,
787 SmallPtrSetImpl<const Value *> &);
788 void addPhiOfOps(PHINode *
Op, BasicBlock *BB, Instruction *ExistingValue);
789 void removePhiOfOps(Instruction *
I, PHINode *PHITemp);
792 void valueNumberMemoryPhi(MemoryPhi *);
793 void valueNumberInstruction(Instruction *);
796 ExprResult checkExprResults(Expression *, Instruction *,
Value *)
const;
797 ExprResult performSymbolicEvaluation(Instruction *,
798 SmallPtrSetImpl<Value *> &)
const;
799 const Expression *performSymbolicLoadCoercion(
Type *,
Value *, LoadInst *,
801 MemoryAccess *)
const;
802 const Expression *performSymbolicLoadEvaluation(Instruction *)
const;
803 const Expression *performSymbolicStoreEvaluation(Instruction *)
const;
804 ExprResult performSymbolicCallEvaluation(Instruction *)
const;
808 BasicBlock *PHIBlock)
const;
809 const Expression *performSymbolicAggrValueEvaluation(Instruction *)
const;
810 ExprResult performSymbolicCmpEvaluation(Instruction *)
const;
811 ExprResult performSymbolicPredicateInfoEvaluation(BitCastInst *)
const;
814 bool someEquivalentDominates(
const Instruction *,
const Instruction *)
const;
816 CongruenceClass *getClassForExpression(
const Expression *
E)
const;
817 void performCongruenceFinding(Instruction *,
const Expression *);
818 void moveValueToNewCongruenceClass(Instruction *,
const Expression *,
819 CongruenceClass *, CongruenceClass *);
820 void moveMemoryToNewCongruenceClass(Instruction *, MemoryAccess *,
821 CongruenceClass *, CongruenceClass *);
822 Value *getNextValueLeader(CongruenceClass *)
const;
823 const MemoryAccess *getNextMemoryLeader(CongruenceClass *)
const;
824 bool setMemoryClass(
const MemoryAccess *From, CongruenceClass *To);
825 CongruenceClass *getMemoryClass(
const MemoryAccess *MA)
const;
826 const MemoryAccess *lookupMemoryLeader(
const MemoryAccess *)
const;
827 bool isMemoryAccessTOP(
const MemoryAccess *)
const;
830 unsigned int getRank(
const Value *)
const;
831 bool shouldSwapOperands(
const Value *,
const Value *)
const;
832 bool shouldSwapOperandsForPredicate(
const Value *,
const Value *,
833 const BitCastInst *
I)
const;
836 void updateReachableEdge(BasicBlock *, BasicBlock *);
837 void processOutgoingEdges(Instruction *, BasicBlock *);
838 Value *findConditionEquivalence(
Value *)
const;
842 void convertClassToDFSOrdered(
const CongruenceClass &,
843 SmallVectorImpl<ValueDFS> &,
844 DenseMap<const Value *, unsigned int> &,
845 SmallPtrSetImpl<Instruction *> &)
const;
846 void convertClassToLoadsAndStores(
const CongruenceClass &,
847 SmallVectorImpl<ValueDFS> &)
const;
849 bool eliminateInstructions(Function &);
850 void replaceInstruction(Instruction *,
Value *);
851 void markInstructionForDeletion(Instruction *);
852 void deleteInstructionsInBlock(BasicBlock *);
853 Value *findPHIOfOpsLeader(
const Expression *,
const Instruction *,
854 const BasicBlock *)
const;
857 template <
typename Map,
typename KeyType>
858 void touchAndErase(Map &,
const KeyType &);
859 void markUsersTouched(
Value *);
860 void markMemoryUsersTouched(
const MemoryAccess *);
861 void markMemoryDefTouched(
const MemoryAccess *);
862 void markPredicateUsersTouched(Instruction *);
863 void markValueLeaderChangeTouched(CongruenceClass *CC);
864 void markMemoryLeaderChangeTouched(CongruenceClass *CC);
865 void markPhiOfOpsChanged(
const Expression *
E);
866 void addMemoryUsers(
const MemoryAccess *To, MemoryAccess *U)
const;
867 void addAdditionalUsers(
Value *To,
Value *User)
const;
868 void addAdditionalUsers(ExprResult &Res, Instruction *User)
const;
871 void iterateTouchedInstructions();
874 void cleanupTables();
875 std::pair<unsigned, unsigned> assignDFSNumbers(BasicBlock *,
unsigned);
876 void updateProcessedCount(
const Value *V);
877 void verifyMemoryCongruency()
const;
878 void verifyIterationSettled(Function &
F);
879 void verifyStoreExpressions()
const;
880 bool singleReachablePHIPath(SmallPtrSet<const MemoryAccess *, 8> &,
881 const MemoryAccess *,
const MemoryAccess *)
const;
883 void deleteExpression(
const Expression *
E)
const;
884 MemoryUseOrDef *getMemoryAccess(
const Instruction *)
const;
885 MemoryPhi *getMemoryAccess(
const BasicBlock *)
const;
886 template <
class T,
class Range>
T *getMinDFSOfRange(
const Range &)
const;
888 unsigned InstrToDFSNum(
const Value *V)
const {
890 return InstrDFS.
lookup(V);
893 unsigned InstrToDFSNum(
const MemoryAccess *MA)
const {
894 return MemoryToDFSNum(MA);
897 Value *InstrFromDFSNum(
unsigned DFSNum) {
return DFSToInstr[DFSNum]; }
902 unsigned MemoryToDFSNum(
const Value *MA)
const {
904 "This should not be used with instructions");
910 bool isCycleFree(
const Instruction *)
const;
911 bool isBackedge(BasicBlock *From, BasicBlock *To)
const;
915 DebugCounter::CounterState StartingVNCounter;
924 return LHS.MemoryExpression::equals(
RHS);
946 return Call->getAttributes()
947 .intersectWith(Call->getContext(), RHS->Call->getAttributes())
967MemoryUseOrDef *NewGVN::getMemoryAccess(
const Instruction *
I)
const {
973MemoryPhi *NewGVN::getMemoryAccess(
const BasicBlock *BB)
const {
980 auto *Parent =
I->getParent();
983 Parent = TempToBlock.
lookup(V);
984 assert(Parent &&
"Every fake instruction should have a block");
989 assert(MP &&
"Should have been an instruction or a MemoryPhi");
990 return MP->getBlock();
996void NewGVN::deleteExpression(
const Expression *
E)
const {
1006 if (BC->getType() == BC->getOperand(0)->getType())
1007 return BC->getOperand(0);
1026 return BlockInstRange.
lookup(
P1.second).first <
1027 BlockInstRange.
lookup(P2.second).first;
1044 const Instruction *
I,
1045 BasicBlock *PHIBlock,
1047 bool &OriginalOpsConstant)
const {
1052 E->setType(PHIOperands.
begin()->first->getType());
1053 E->setOpcode(Instruction::PHI);
1057 auto *BB =
P.second;
1061 if (!ReachableEdges.
count({BB, PHIBlock}))
1064 if (ValueToClass.
lookup(
P.first) == TOPClass)
1066 OriginalOpsConstant = OriginalOpsConstant &&
isa<Constant>(
P.first);
1067 HasBackedge = HasBackedge || isBackedge(BB, PHIBlock);
1068 return lookupOperandLeader(
P.first) !=
I;
1071 return lookupOperandLeader(
P.first);
1079 bool AllConstant =
true;
1081 E->setType(
GEP->getSourceElementType());
1083 E->setType(
I->getType());
1084 E->setOpcode(
I->getOpcode());
1085 E->allocateOperands(ArgRecycler, ExpressionAllocator);
1090 auto Operand = lookupOperandLeader(O);
1098const Expression *NewGVN::createBinaryExpression(
unsigned Opcode,
Type *
T,
1100 Instruction *
I)
const {
1107 E->setOpcode(Opcode);
1108 E->allocateOperands(ArgRecycler, ExpressionAllocator);
1114 if (shouldSwapOperands(Arg1, Arg2))
1117 E->op_push_back(lookupOperandLeader(Arg1));
1118 E->op_push_back(lookupOperandLeader(Arg2));
1121 if (
auto Simplified = checkExprResults(
E,
I, V)) {
1122 addAdditionalUsers(Simplified,
I);
1131NewGVN::ExprResult NewGVN::checkExprResults(
Expression *
E, Instruction *
I,
1134 return ExprResult::none();
1139 <<
" constant " << *
C <<
"\n");
1140 NumGVNOpsSimplified++;
1142 "We should always have had a basic expression here");
1143 deleteExpression(
E);
1144 return ExprResult::some(createConstantExpression(
C));
1148 <<
" variable " << *V <<
"\n");
1149 deleteExpression(
E);
1150 return ExprResult::some(createVariableExpression(V));
1153 CongruenceClass *CC = ValueToClass.
lookup(V);
1155 if (CC->getLeader() && CC->getLeader() !=
I) {
1156 return ExprResult::some(createVariableOrConstant(CC->getLeader()), V);
1158 if (CC->getDefiningExpr()) {
1161 <<
" expression " << *CC->getDefiningExpr() <<
"\n");
1162 NumGVNOpsSimplified++;
1163 deleteExpression(
E);
1164 return ExprResult::some(CC->getDefiningExpr(), V);
1168 return ExprResult::none();
1174NewGVN::ExprResult NewGVN::createExpression(Instruction *
I)
const {
1180 bool AllConstant = setBasicExpressionInfo(
I,
E);
1182 if (
I->isCommutative()) {
1187 assert(
I->getNumOperands() == 2 &&
"Unsupported commutative instruction!");
1188 if (shouldSwapOperands(
E->getOperand(0),
E->getOperand(1)))
1189 E->swapOperands(0, 1);
1196 if (shouldSwapOperands(
E->getOperand(0),
E->getOperand(1))) {
1197 E->swapOperands(0, 1);
1200 E->setOpcode((CI->getOpcode() << 8) | Predicate);
1202 assert(
I->getOperand(0)->getType() ==
I->getOperand(1)->getType() &&
1203 "Wrong types on cmp instruction");
1204 assert((
E->getOperand(0)->getType() ==
I->getOperand(0)->getType() &&
1205 E->getOperand(1)->getType() ==
I->getOperand(1)->getType()));
1208 if (
auto Simplified = checkExprResults(
E,
I, V))
1212 E->getOperand(1) ==
E->getOperand(2)) {
1213 assert(
E->getOperand(1)->getType() ==
I->getOperand(1)->getType() &&
1214 E->getOperand(2)->getType() ==
I->getOperand(2)->getType());
1216 E->getOperand(2), Q);
1217 if (
auto Simplified = checkExprResults(
E,
I, V))
1220 }
else if (
I->isBinaryOp()) {
1223 if (
auto Simplified = checkExprResults(
E,
I, V))
1228 if (
auto Simplified = checkExprResults(
E,
I, V))
1232 ArrayRef(std::next(
E->op_begin()),
E->op_end()),
1233 GEPI->getNoWrapFlags(), Q);
1234 if (
auto Simplified = checkExprResults(
E,
I, V))
1236 }
else if (AllConstant) {
1245 for (
Value *Arg :
E->operands())
1249 if (
auto Simplified = checkExprResults(
E,
I, V))
1252 return ExprResult::some(
E);
1256NewGVN::createAggregateValueExpression(Instruction *
I)
const {
1258 auto *
E =
new (ExpressionAllocator)
1260 setBasicExpressionInfo(
I,
E);
1261 E->allocateIntOperands(ExpressionAllocator);
1265 auto *
E =
new (ExpressionAllocator)
1267 setBasicExpressionInfo(EI,
E);
1268 E->allocateIntOperands(ExpressionAllocator);
1278 return SingletonDeadExpression;
1289 return createConstantExpression(
C);
1290 return createVariableExpression(V);
1306NewGVN::createCallExpression(CallInst *CI,
const MemoryAccess *MA)
const {
1310 setBasicExpressionInfo(CI,
E);
1316 if (shouldSwapOperands(
E->getOperand(0),
E->getOperand(1)))
1317 E->swapOperands(0, 1);
1323bool NewGVN::someEquivalentDominates(
const Instruction *Inst,
1324 const Instruction *U)
const {
1325 auto *CC = ValueToClass.
lookup(Inst);
1348 if (CC->getNextLeader().first &&
1352 return Member != CC->getLeader() &&
1359Value *NewGVN::lookupOperandLeader(
Value *V)
const {
1360 CongruenceClass *CC = ValueToClass.
lookup(V);
1367 return CC->getStoredValue() ? CC->getStoredValue() : CC->getLeader();
1373const MemoryAccess *NewGVN::lookupMemoryLeader(
const MemoryAccess *MA)
const {
1374 auto *CC = getMemoryClass(MA);
1375 assert(CC->getMemoryLeader() &&
1376 "Every MemoryAccess should be mapped to a congruence class with a "
1377 "representative memory access");
1378 return CC->getMemoryLeader();
1384bool NewGVN::isMemoryAccessTOP(
const MemoryAccess *MA)
const {
1385 return getMemoryClass(MA) == TOPClass;
1390 const MemoryAccess *MA)
const {
1392 new (ExpressionAllocator)
LoadExpression(1, LI, lookupMemoryLeader(MA));
1394 E->setType(LoadType);
1398 E->op_push_back(PointerOp);
1407NewGVN::createStoreExpression(StoreInst *SI,
const MemoryAccess *MA)
const {
1408 auto *StoredValueLeader = lookupOperandLeader(
SI->getValueOperand());
1409 auto *
E =
new (ExpressionAllocator)
1412 E->setType(
SI->getValueOperand()->getType());
1416 E->op_push_back(lookupOperandLeader(
SI->getPointerOperand()));
1424const Expression *NewGVN::performSymbolicStoreEvaluation(Instruction *
I)
const {
1428 auto *StoreAccess = getMemoryAccess(SI);
1430 const MemoryAccess *StoreRHS = StoreAccess->getDefiningAccess();
1434 StoreRHS = lookupMemoryLeader(StoreRHS);
1435 if (StoreRHS != StoreAccess->getDefiningAccess())
1436 addMemoryUsers(StoreRHS, StoreAccess);
1438 if (StoreRHS == StoreAccess)
1441 if (
SI->isSimple()) {
1445 const auto *LastStore = createStoreExpression(SI, StoreRHS);
1446 const auto *LastCC = ExpressionToClass.lookup(LastStore);
1452 if (LastCC && LastCC->getStoredValue() == LastStore->getStoredValue())
1460 LastStore->getOperand(0)) &&
1461 (lookupMemoryLeader(getMemoryAccess(LI)->getDefiningAccess()) ==
1464 deleteExpression(LastStore);
1470 return createStoreExpression(SI, StoreAccess);
1476NewGVN::performSymbolicLoadCoercion(
Type *LoadType,
Value *LoadPtr,
1477 LoadInst *LI, Instruction *DepInst,
1478 MemoryAccess *DefiningAccess)
const {
1484 if (LI->
isAtomic() > DepSI->isAtomic() ||
1485 LoadType == DepSI->getValueOperand()->getType())
1490 lookupOperandLeader(DepSI->getValueOperand()))) {
1493 <<
" to constant " << *Res <<
"\n");
1494 return createConstantExpression(Res);
1500 if (LI->
isAtomic() > DepLI->isAtomic())
1506 if (
auto *PossibleConstant =
1509 <<
" to constant " << *PossibleConstant <<
"\n");
1510 return createConstantExpression(PossibleConstant);
1516 if (
auto *PossibleConstant =
1519 <<
" to constant " << *PossibleConstant <<
"\n");
1520 return createConstantExpression(PossibleConstant);
1526 if (
II->getIntrinsicID() == Intrinsic::lifetime_start) {
1527 auto *LifetimePtr =
II->getOperand(0);
1528 if (LoadPtr == lookupOperandLeader(LifetimePtr) ||
1537 (LoadPtr != lookupOperandLeader(DepInst) &&
1546 }
else if (
auto *InitVal =
1548 return createConstantExpression(InitVal);
1553const Expression *NewGVN::performSymbolicLoadEvaluation(Instruction *
I)
const {
1565 MemoryAccess *OriginalAccess = getMemoryAccess(
I);
1566 MemoryAccess *DefiningAccess =
1579 if (
const auto *CoercionResult =
1580 performSymbolicLoadCoercion(LI->
getType(), LoadAddressLeader, LI,
1581 DefiningInst, DefiningAccess))
1582 return CoercionResult;
1586 const auto *
LE = createLoadExpression(LI->
getType(), LoadAddressLeader, LI,
1590 if (
LE->getMemoryLeader() != DefiningAccess)
1591 addMemoryUsers(
LE->getMemoryLeader(), OriginalAccess);
1596NewGVN::performSymbolicPredicateInfoEvaluation(BitCastInst *
I)
const {
1597 auto *PI = PredInfo->getPredicateInfoFor(
I);
1599 return ExprResult::none();
1601 LLVM_DEBUG(
dbgs() <<
"Found predicate info from instruction !\n");
1603 const std::optional<PredicateConstraint> &Constraint = PI->getConstraint();
1605 return ExprResult::none();
1608 Value *CmpOp0 =
I->getOperand(0);
1609 Value *CmpOp1 = Constraint->OtherOp;
1611 Value *FirstOp = lookupOperandLeader(CmpOp0);
1612 Value *SecondOp = lookupOperandLeader(CmpOp1);
1613 Value *AdditionallyUsedValue = CmpOp0;
1616 if (shouldSwapOperandsForPredicate(FirstOp, SecondOp,
I)) {
1619 AdditionallyUsedValue = CmpOp1;
1623 return ExprResult::some(createVariableOrConstant(FirstOp),
1624 AdditionallyUsedValue, PI);
1629 return ExprResult::some(createConstantExpression(
cast<Constant>(FirstOp)),
1630 AdditionallyUsedValue, PI);
1632 return ExprResult::none();
1636NewGVN::ExprResult NewGVN::performSymbolicCallEvaluation(Instruction *
I)
const {
1647 return ExprResult::none();
1653 return ExprResult::none();
1656 return ExprResult::some(
1657 createCallExpression(CI, TOPClass->getMemoryLeader()));
1661 return ExprResult::some(createCallExpression(CI, DefiningAccess));
1663 return ExprResult::some(
1664 createCallExpression(CI, TOPClass->getMemoryLeader()));
1666 return ExprResult::none();
1670CongruenceClass *NewGVN::getMemoryClass(
const MemoryAccess *MA)
const {
1672 assert(Result &&
"Should have found memory class");
1678bool NewGVN::setMemoryClass(
const MemoryAccess *From,
1679 CongruenceClass *NewClass) {
1681 "Every MemoryAccess should be getting mapped to a non-null class");
1685 <<
" with current MemoryAccess leader ");
1691 if (LookupResult != MemoryAccessToClass.
end()) {
1693 if (OldClass != NewClass) {
1696 OldClass->memory_erase(MP);
1697 NewClass->memory_insert(MP);
1699 if (OldClass->getMemoryLeader() == From) {
1700 if (OldClass->definesNoMemory()) {
1701 OldClass->setMemoryLeader(
nullptr);
1703 OldClass->setMemoryLeader(getNextMemoryLeader(OldClass));
1705 << OldClass->getID() <<
" to "
1706 << *OldClass->getMemoryLeader()
1707 <<
" due to removal of a memory member " << *From
1709 markMemoryLeaderChangeTouched(OldClass);
1726bool NewGVN::isCycleFree(
const Instruction *
I)
const {
1732 auto ICS = InstCycleState.
lookup(
I);
1733 if (ICS == ICS_Unknown) {
1735 auto &
SCC = SCCFinder.getComponentFor(
I);
1737 if (
SCC.size() == 1)
1738 InstCycleState.
insert({
I, ICS_CycleFree});
1743 ICS = AllPhis ? ICS_CycleFree : ICS_Cycle;
1744 for (
const auto *Member : SCC)
1746 InstCycleState.
insert({MemberPhi, ICS});
1749 if (ICS == ICS_Cycle)
1758 BasicBlock *PHIBlock)
const {
1760 bool HasBackedge =
false;
1765 bool OriginalOpsConstant =
true;
1767 PHIOps,
I, PHIBlock, HasBackedge, OriginalOpsConstant));
1771 bool HasUndef =
false, HasPoison =
false;
1773 if (isa<PoisonValue>(Arg)) {
1784 if (Filtered.empty()) {
1789 dbgs() <<
"PHI Node " << *
I
1790 <<
" has no non-undef arguments, valuing it as undef\n");
1795 dbgs() <<
"PHI Node " << *
I
1796 <<
" has no non-poison arguments, valuing it as poison\n");
1800 LLVM_DEBUG(
dbgs() <<
"No arguments of PHI node " << *
I <<
" are live\n");
1801 deleteExpression(
E);
1802 return createDeadExpression();
1804 Value *AllSameValue = *(Filtered.begin());
1822 if (HasPoison || HasUndef) {
1828 if (HasBackedge && !OriginalOpsConstant &&
1834 if (!someEquivalentDominates(AllSameInst,
I))
1841 InstrToDFSNum(AllSameValue) > InstrToDFSNum(
I))
1843 NumGVNPhisAllSame++;
1844 LLVM_DEBUG(
dbgs() <<
"Simplified PHI node " << *
I <<
" to " << *AllSameValue
1846 deleteExpression(
E);
1847 return createVariableOrConstant(AllSameValue);
1853NewGVN::performSymbolicAggrValueEvaluation(Instruction *
I)
const {
1856 if (WO && EI->getNumIndices() == 1 && *EI->idx_begin() == 0)
1860 return createBinaryExpression(WO->getBinaryOp(), EI->getType(),
1861 WO->getLHS(), WO->getRHS(),
I);
1864 return createAggregateValueExpression(
I);
1867NewGVN::ExprResult NewGVN::performSymbolicCmpEvaluation(Instruction *
I)
const {
1873 auto Op0 = lookupOperandLeader(CI->
getOperand(0));
1874 auto Op1 = lookupOperandLeader(CI->
getOperand(1));
1875 auto OurPredicate = CI->getPredicate();
1876 if (shouldSwapOperands(Op0, Op1)) {
1878 OurPredicate = CI->getSwappedPredicate();
1882 const PredicateBase *LastPredInfo =
nullptr;
1885 auto *CmpPI = PredInfo->getPredicateInfoFor(
I);
1887 return ExprResult::some(
1892 if (CI->isTrueWhenEqual())
1893 return ExprResult::some(
1895 else if (CI->isFalseWhenEqual())
1896 return ExprResult::some(
1926 auto *PI = PredInfo->getPredicateInfoFor(
Op);
1928 if (PI == LastPredInfo)
1933 if (!DT->
dominates(PBranch->To,
I->getParent()))
1943 auto *BranchOp0 = lookupOperandLeader(BranchCond->getOperand(0));
1944 auto *BranchOp1 = lookupOperandLeader(BranchCond->getOperand(1));
1945 auto BranchPredicate = BranchCond->getPredicate();
1946 if (shouldSwapOperands(BranchOp0, BranchOp1)) {
1948 BranchPredicate = BranchCond->getSwappedPredicate();
1950 if (BranchOp0 == Op0 && BranchOp1 == Op1) {
1951 if (PBranch->TrueEdge) {
1957 return ExprResult::some(createConstantExpression(
C), PI);
1962 if (BranchPredicate == OurPredicate) {
1964 return ExprResult::some(
1967 }
else if (BranchPredicate ==
1970 return ExprResult::some(
1979 return createExpression(
I);
1984NewGVN::performSymbolicEvaluation(Instruction *
I,
1985 SmallPtrSetImpl<Value *> &Visited)
const {
1991 switch (
I->getOpcode()) {
1992 case Instruction::ExtractValue:
1993 case Instruction::InsertValue:
1994 E = performSymbolicAggrValueEvaluation(
I);
1996 case Instruction::PHI: {
1999 for (
unsigned i = 0; i < PN->getNumOperands(); ++i)
2000 Ops.push_back({PN->getIncomingValue(i), PN->getIncomingBlock(i)});
2003 E = performSymbolicPHIEvaluation(
Ops,
I, getBlockForValue(
I));
2005 case Instruction::Call:
2006 return performSymbolicCallEvaluation(
I);
2008 case Instruction::Store:
2009 E = performSymbolicStoreEvaluation(
I);
2011 case Instruction::Load:
2012 E = performSymbolicLoadEvaluation(
I);
2014 case Instruction::BitCast:
2016 if (
I->getType() ==
I->getOperand(0)->getType())
2021 case Instruction::AddrSpaceCast:
2022 case Instruction::Freeze:
2023 return createExpression(
I);
2025 case Instruction::ICmp:
2026 case Instruction::FCmp:
2027 return performSymbolicCmpEvaluation(
I);
2029 case Instruction::FNeg:
2030 case Instruction::Add:
2031 case Instruction::FAdd:
2032 case Instruction::Sub:
2033 case Instruction::FSub:
2034 case Instruction::Mul:
2035 case Instruction::FMul:
2036 case Instruction::UDiv:
2037 case Instruction::SDiv:
2038 case Instruction::FDiv:
2039 case Instruction::URem:
2040 case Instruction::SRem:
2041 case Instruction::FRem:
2042 case Instruction::Shl:
2043 case Instruction::LShr:
2044 case Instruction::AShr:
2045 case Instruction::And:
2046 case Instruction::Or:
2047 case Instruction::Xor:
2048 case Instruction::Trunc:
2049 case Instruction::ZExt:
2050 case Instruction::SExt:
2051 case Instruction::FPToUI:
2052 case Instruction::FPToSI:
2053 case Instruction::UIToFP:
2054 case Instruction::SIToFP:
2055 case Instruction::FPTrunc:
2056 case Instruction::FPExt:
2057 case Instruction::PtrToInt:
2058 case Instruction::PtrToAddr:
2059 case Instruction::IntToPtr:
2060 case Instruction::Select:
2061 case Instruction::ExtractElement:
2062 case Instruction::InsertElement:
2063 case Instruction::GetElementPtr:
2064 return createExpression(
I);
2066 case Instruction::ShuffleVector:
2068 return ExprResult::none();
2070 return ExprResult::none();
2072 return ExprResult::some(
E);
2077template <
typename Map,
typename KeyType>
2078void NewGVN::touchAndErase(Map &M,
const KeyType &
Key) {
2080 if (Result !=
M.end()) {
2081 for (
const typename Map::mapped_type::value_type Mapped :
Result->second)
2082 TouchedInstructions.
set(InstrToDFSNum(Mapped));
2087void NewGVN::addAdditionalUsers(
Value *To,
Value *User)
const {
2088 assert(User && To != User);
2090 AdditionalUsers[To].
insert(User);
2093void NewGVN::addAdditionalUsers(ExprResult &Res, Instruction *User)
const {
2094 if (Res.ExtraDep && Res.ExtraDep != User)
2095 addAdditionalUsers(Res.ExtraDep, User);
2096 Res.ExtraDep =
nullptr;
2100 PredicateToUsers[PBranch->Condition].
insert(User);
2102 PredicateToUsers[PAssume->Condition].
insert(User);
2104 Res.PredDep =
nullptr;
2107void NewGVN::markUsersTouched(
Value *V) {
2109 for (
auto *User :
V->users()) {
2111 TouchedInstructions.
set(InstrToDFSNum(User));
2113 touchAndErase(AdditionalUsers, V);
2116void NewGVN::addMemoryUsers(
const MemoryAccess *To, MemoryAccess *U)
const {
2117 LLVM_DEBUG(
dbgs() <<
"Adding memory user " << *U <<
" to " << *To <<
"\n");
2118 MemoryToUsers[To].
insert(U);
2121void NewGVN::markMemoryDefTouched(
const MemoryAccess *MA) {
2122 TouchedInstructions.
set(MemoryToDFSNum(MA));
2125void NewGVN::markMemoryUsersTouched(
const MemoryAccess *MA) {
2128 for (
const auto *U : MA->
users())
2129 TouchedInstructions.
set(MemoryToDFSNum(U));
2130 touchAndErase(MemoryToUsers, MA);
2134void NewGVN::markPredicateUsersTouched(Instruction *
I) {
2135 touchAndErase(PredicateToUsers,
I);
2139void NewGVN::markMemoryLeaderChangeTouched(CongruenceClass *CC) {
2140 for (
const auto *M : CC->memory())
2141 markMemoryDefTouched(M);
2146void NewGVN::markValueLeaderChangeTouched(CongruenceClass *CC) {
2147 for (
auto *M : *CC) {
2149 TouchedInstructions.
set(InstrToDFSNum(
I));
2156template <
class T,
class Range>
2157T *NewGVN::getMinDFSOfRange(
const Range &R)
const {
2158 std::pair<T *, unsigned> MinDFS = {
nullptr, ~0
U};
2159 for (
const auto X : R) {
2160 auto DFSNum = InstrToDFSNum(
X);
2161 if (DFSNum < MinDFS.second)
2162 MinDFS = {
X, DFSNum};
2164 return MinDFS.first;
2170const MemoryAccess *NewGVN::getNextMemoryLeader(CongruenceClass *CC)
const {
2174 assert(!CC->definesNoMemory() &&
"Can't get next leader if there is none");
2175 if (CC->getStoreCount() > 0) {
2177 return getMemoryAccess(NL);
2183 assert(CC->getStoreCount() == 0);
2187 if (CC->memory_size() == 1)
2188 return *CC->memory_begin();
2189 return getMinDFSOfRange<const MemoryPhi>(CC->memory());
2195Value *NewGVN::getNextValueLeader(CongruenceClass *CC)
const {
2200 if (CC->size() == 1 || CC == TOPClass) {
2201 return *(CC->begin());
2202 }
else if (CC->getNextLeader().first) {
2203 ++NumGVNAvoidedSortedLeaderChanges;
2204 return CC->getNextLeader().first;
2206 ++NumGVNSortedLeaderChanges;
2210 return getMinDFSOfRange<Value>(*CC);
2223void NewGVN::moveMemoryToNewCongruenceClass(Instruction *
I,
2224 MemoryAccess *InstMA,
2225 CongruenceClass *OldClass,
2226 CongruenceClass *NewClass) {
2229 assert((!InstMA || !OldClass->getMemoryLeader() ||
2230 OldClass->getLeader() !=
I ||
2231 MemoryAccessToClass.
lookup(OldClass->getMemoryLeader()) ==
2232 MemoryAccessToClass.
lookup(InstMA)) &&
2233 "Representative MemoryAccess mismatch");
2235 if (!NewClass->getMemoryLeader()) {
2237 assert(NewClass->size() == 1 ||
2239 NewClass->setMemoryLeader(InstMA);
2242 << NewClass->getID()
2243 <<
" due to new memory instruction becoming leader\n");
2244 markMemoryLeaderChangeTouched(NewClass);
2246 setMemoryClass(InstMA, NewClass);
2248 if (OldClass->getMemoryLeader() == InstMA) {
2249 if (!OldClass->definesNoMemory()) {
2250 OldClass->setMemoryLeader(getNextMemoryLeader(OldClass));
2252 << OldClass->getID() <<
" to "
2253 << *OldClass->getMemoryLeader()
2254 <<
" due to removal of old leader " << *InstMA <<
"\n");
2255 markMemoryLeaderChangeTouched(OldClass);
2257 OldClass->setMemoryLeader(
nullptr);
2263void NewGVN::moveValueToNewCongruenceClass(Instruction *
I,
const Expression *
E,
2264 CongruenceClass *OldClass,
2265 CongruenceClass *NewClass) {
2266 if (
I == OldClass->getNextLeader().first)
2267 OldClass->resetNextLeader();
2270 NewClass->insert(
I);
2274 if (NewClass->getLeader() !=
I &&
2275 NewClass->addPossibleLeader({I, InstrToDFSNum(I)})) {
2276 markValueLeaderChangeTouched(NewClass);
2281 OldClass->decStoreCount();
2289 if (NewClass->getStoreCount() == 0 && !NewClass->getStoredValue()) {
2293 NewClass->setStoredValue(SE->getStoredValue());
2294 markValueLeaderChangeTouched(NewClass);
2297 << NewClass->getID() <<
" from "
2298 << *NewClass->getLeader() <<
" to " << *SI
2299 <<
" because store joined class\n");
2302 NewClass->setLeader({
SI, InstrToDFSNum(SI)});
2306 NewClass->incStoreCount();
2314 moveMemoryToNewCongruenceClass(
I, InstMA, OldClass, NewClass);
2315 ValueToClass[
I] = NewClass;
2317 if (OldClass->empty() && OldClass != TOPClass) {
2318 if (OldClass->getDefiningExpr()) {
2319 LLVM_DEBUG(
dbgs() <<
"Erasing expression " << *OldClass->getDefiningExpr()
2320 <<
" from table\n");
2323 auto Iter = ExpressionToClass.find_as(
2324 ExactEqualsExpression(*OldClass->getDefiningExpr()));
2325 if (Iter != ExpressionToClass.end())
2326 ExpressionToClass.erase(Iter);
2327#ifdef EXPENSIVE_CHECKS
2329 (*OldClass->getDefiningExpr() != *
E || ExpressionToClass.lookup(
E)) &&
2330 "We erased the expression we just inserted, which should not happen");
2333 }
else if (OldClass->getLeader() ==
I) {
2338 << OldClass->getID() <<
"\n");
2339 ++NumGVNLeaderChanges;
2344 if (OldClass->getStoreCount() == 0) {
2345 if (OldClass->getStoredValue())
2346 OldClass->setStoredValue(
nullptr);
2348 OldClass->setLeader({getNextValueLeader(OldClass),
2349 InstrToDFSNum(getNextValueLeader(OldClass))});
2350 OldClass->resetNextLeader();
2351 markValueLeaderChangeTouched(OldClass);
2357void NewGVN::markPhiOfOpsChanged(
const Expression *
E) {
2358 touchAndErase(ExpressionToPhiOfOps,
E);
2362void NewGVN::performCongruenceFinding(Instruction *
I,
const Expression *
E) {
2366 CongruenceClass *IClass = ValueToClass.
lookup(
I);
2367 assert(IClass &&
"Should have found a IClass");
2369 assert(!IClass->isDead() &&
"Found a dead class");
2371 CongruenceClass *EClass =
nullptr;
2373 EClass = ValueToClass.
lookup(VE->getVariableValue());
2378 auto lookupResult = ExpressionToClass.try_emplace(
E);
2381 if (lookupResult.second) {
2382 CongruenceClass *NewClass = createCongruenceClass(
nullptr,
E);
2383 auto place = lookupResult.first;
2384 place->second = NewClass;
2388 NewClass->setLeader({
CE->getConstantValue(), 0});
2390 StoreInst *
SI = SE->getStoreInst();
2391 NewClass->setLeader({
SI, InstrToDFSNum(SI)});
2392 NewClass->setStoredValue(SE->getStoredValue());
2396 NewClass->setLeader({
I, InstrToDFSNum(
I)});
2399 "VariableExpression should have been handled already");
2403 <<
" using expression " << *
E <<
" at "
2404 << NewClass->getID() <<
" and leader "
2405 << *(NewClass->getLeader()));
2406 if (NewClass->getStoredValue())
2408 << *(NewClass->getStoredValue()));
2411 EClass = lookupResult.first->second;
2414 (EClass->getStoredValue() &&
2416 "Any class with a constant expression should have a "
2419 assert(EClass &&
"Somehow don't have an eclass");
2421 assert(!EClass->isDead() &&
"We accidentally looked up a dead class");
2424 bool ClassChanged = IClass != EClass;
2425 bool LeaderChanged = LeaderChanges.
erase(
I);
2426 if (ClassChanged || LeaderChanged) {
2427 LLVM_DEBUG(
dbgs() <<
"New class " << EClass->getID() <<
" for expression "
2430 moveValueToNewCongruenceClass(
I,
E, IClass, EClass);
2431 markPhiOfOpsChanged(
E);
2434 markUsersTouched(
I);
2435 if (MemoryAccess *MA = getMemoryAccess(
I))
2436 markMemoryUsersTouched(MA);
2438 markPredicateUsersTouched(CI);
2445 auto *OldE = ValueToExpression.
lookup(
I);
2451 auto Iter = ExpressionToClass.find_as(ExactEqualsExpression(*OldE));
2452 if (Iter != ExpressionToClass.end())
2453 ExpressionToClass.erase(Iter);
2456 ValueToExpression[
I] =
E;
2461void NewGVN::updateReachableEdge(BasicBlock *From, BasicBlock *To) {
2463 if (ReachableEdges.
insert({From, To}).second) {
2465 if (ReachableBlocks.
insert(To).second) {
2467 <<
" marked reachable\n");
2468 const auto &InstRange = BlockInstRange.
lookup(To);
2469 TouchedInstructions.
set(InstRange.first, InstRange.second);
2472 <<
" was reachable, but new edge {"
2474 <<
"} to it found\n");
2480 if (MemoryAccess *MemPhi = getMemoryAccess(To))
2481 TouchedInstructions.
set(InstrToDFSNum(MemPhi));
2486 for (
auto InstNum : RevisitOnReachabilityChange[To])
2487 TouchedInstructions.
set(InstNum);
2500void NewGVN::processOutgoingEdges(Instruction *TI, BasicBlock *
B) {
2505 Value *CondEvaluated = findConditionEquivalence(
Cond);
2506 if (!CondEvaluated) {
2508 SmallPtrSet<Value *, 4> Visited;
2509 auto Res = performSymbolicEvaluation(
I, Visited);
2511 CondEvaluated =
CE->getConstantValue();
2512 addAdditionalUsers(Res,
I);
2516 Res.ExtraDep =
nullptr;
2519 CondEvaluated =
Cond;
2526 <<
" evaluated to true\n");
2527 updateReachableEdge(
B, TrueSucc);
2528 }
else if (CI->
isZero()) {
2530 <<
" evaluated to false\n");
2531 updateReachableEdge(
B, FalseSucc);
2534 updateReachableEdge(
B, TrueSucc);
2535 updateReachableEdge(
B, FalseSucc);
2541 Value *SwitchCond =
SI->getCondition();
2542 Value *CondEvaluated = findConditionEquivalence(SwitchCond);
2547 auto Case = *
SI->findCaseValue(CondVal);
2548 if (Case.getCaseSuccessor() ==
SI->getDefaultDest()) {
2552 updateReachableEdge(
B,
SI->getDefaultDest());
2556 BasicBlock *TargetBlock = Case.getCaseSuccessor();
2557 updateReachableEdge(
B, TargetBlock);
2559 for (BasicBlock *TargetBlock :
successors(
SI->getParent()))
2560 updateReachableEdge(
B, TargetBlock);
2566 updateReachableEdge(
B, TargetBlock);
2571 auto *MA = getMemoryAccess(TI);
2573 auto *CC = ensureLeaderOfMemoryClass(MA);
2574 if (setMemoryClass(MA, CC))
2575 markMemoryUsersTouched(MA);
2581void NewGVN::removePhiOfOps(Instruction *
I, PHINode *PHITemp) {
2582 InstrDFS.
erase(PHITemp);
2585 TempToBlock.
erase(PHITemp);
2594void NewGVN::addPhiOfOps(PHINode *
Op, BasicBlock *BB,
2595 Instruction *ExistingValue) {
2596 InstrDFS[
Op] = InstrToDFSNum(ExistingValue);
2598 TempToBlock[
Op] = BB;
2599 RealToTemp[ExistingValue] =
Op;
2602 for (
auto *U : ExistingValue->
users())
2621bool NewGVN::OpIsSafeForPHIOfOps(
Value *V,
const BasicBlock *PHIBlock,
2622 SmallPtrSetImpl<const Value *> &Visited) {
2625 while (!Worklist.
empty()) {
2630 auto OISIt = OpSafeForPHIOfOps.
find({
I, CacheIdx});
2631 if (OISIt != OpSafeForPHIOfOps.
end())
2632 return OISIt->second;
2637 OpSafeForPHIOfOps.
insert({{
I, CacheIdx},
true});
2642 OpSafeForPHIOfOps.
insert({{
I, CacheIdx},
false});
2653 if (OrigI->mayReadFromMemory())
2657 for (
auto *
Op : OrigI->operand_values()) {
2661 auto OISIt = OpSafeForPHIOfOps.
find({OrigI, CacheIdx});
2662 if (OISIt != OpSafeForPHIOfOps.
end()) {
2663 if (!OISIt->second) {
2664 OpSafeForPHIOfOps.
insert({{
I, CacheIdx},
false});
2674 OpSafeForPHIOfOps.
insert({{
V, CacheIdx},
true});
2683Value *NewGVN::findLeaderForInst(Instruction *TransInst,
2684 SmallPtrSetImpl<Value *> &Visited,
2685 MemoryAccess *MemAccess, Instruction *OrigInst,
2686 BasicBlock *PredBB) {
2687 unsigned IDFSNum = InstrToDFSNum(OrigInst);
2689 AllTempInstructions.
insert(TransInst);
2693 TempToBlock.
insert({TransInst, PredBB});
2694 InstrDFS.
insert({TransInst, IDFSNum});
2696 auto Res = performSymbolicEvaluation(TransInst, Visited);
2698 addAdditionalUsers(Res, OrigInst);
2699 InstrDFS.
erase(TransInst);
2700 AllTempInstructions.
erase(TransInst);
2701 TempToBlock.
erase(TransInst);
2703 TempToMemory.
erase(TransInst);
2706 auto *FoundVal = findPHIOfOpsLeader(
E, OrigInst, PredBB);
2708 ExpressionToPhiOfOps[
E].
insert(OrigInst);
2709 LLVM_DEBUG(
dbgs() <<
"Cannot find phi of ops operand for " << *TransInst
2714 FoundVal =
SI->getValueOperand();
2721NewGVN::makePossiblePHIOfOps(Instruction *
I,
2722 SmallPtrSetImpl<Value *> &Visited) {
2726 if (!Visited.
insert(
I).second)
2732 if (!isCycleFree(
I))
2738 auto *MemAccess = getMemoryAccess(
I);
2742 if (MemAccess && !
isa<MemoryPhi>(MemAccess->getDefiningAccess()) &&
2747 SmallPtrSet<const Value *, 10> VisitedOps;
2750 PHINode *OpPHI =
nullptr;
2753 for (
auto *
Op :
Ops) {
2755 auto *ValuePHI = RealToTemp.
lookup(
Op);
2762 if (!SamePHIBlock) {
2763 SamePHIBlock = getBlockForValue(OpPHI);
2764 }
else if (SamePHIBlock != getBlockForValue(OpPHI)) {
2767 <<
"PHIs for operands are not all in the same block, aborting\n");
2781 SmallPtrSet<Value *, 4> Deps;
2782 auto *PHIBlock = getBlockForValue(OpPHI);
2783 RevisitOnReachabilityChange[PHIBlock].reset(InstrToDFSNum(
I));
2784 for (
unsigned PredNum = 0; PredNum < OpPHI->
getNumOperands(); ++PredNum) {
2786 Value *FoundVal =
nullptr;
2787 SmallPtrSet<Value *, 4> CurrentDeps;
2790 if (ReachableEdges.
count({PredBB, PHIBlock})) {
2798 TempToMemory.
insert({ValueOp, MemAccess});
2799 bool SafeForPHIOfOps =
true;
2802 auto *OrigOp = &*
Op;
2806 Op =
Op->DoPHITranslation(PHIBlock, PredBB);
2807 if (
Op != OrigOp &&
Op !=
I)
2809 }
else if (
auto *ValuePHI = RealToTemp.
lookup(
Op)) {
2810 if (getBlockForValue(ValuePHI) == PHIBlock)
2811 Op = ValuePHI->getIncomingValueForBlock(PredBB);
2816 (
Op != OrigOp || OpIsSafeForPHIOfOps(
Op, PHIBlock, VisitedOps));
2823 FoundVal = !SafeForPHIOfOps ? nullptr
2824 : findLeaderForInst(ValueOp, Visited,
2825 MemAccess,
I, PredBB);
2830 if (SafeForPHIOfOps)
2831 for (
auto *Dep : CurrentDeps)
2832 addAdditionalUsers(Dep,
I);
2838 LLVM_DEBUG(
dbgs() <<
"Skipping phi of ops operand for incoming block "
2840 <<
" because the block is unreachable\n");
2842 RevisitOnReachabilityChange[PHIBlock].set(InstrToDFSNum(
I));
2846 LLVM_DEBUG(
dbgs() <<
"Found phi of ops operand " << *FoundVal <<
" in "
2849 for (
auto *Dep : Deps)
2850 addAdditionalUsers(Dep,
I);
2852 auto *
E = performSymbolicPHIEvaluation(PHIOps,
I, PHIBlock);
2856 <<
"Not creating real PHI of ops because it simplified to existing "
2857 "value or constant\n");
2863 for (
auto &O : PHIOps)
2864 addAdditionalUsers(
O.first,
I);
2868 auto *ValuePHI = RealToTemp.
lookup(
I);
2869 bool NewPHI =
false;
2873 addPhiOfOps(ValuePHI, PHIBlock,
I);
2875 NumGVNPHIOfOpsCreated++;
2878 for (
auto PHIOp : PHIOps)
2879 ValuePHI->addIncoming(PHIOp.first, PHIOp.second);
2881 TempToBlock[ValuePHI] = PHIBlock;
2883 for (
auto PHIOp : PHIOps) {
2884 ValuePHI->setIncomingValue(i, PHIOp.first);
2885 ValuePHI->setIncomingBlock(i, PHIOp.second);
2889 RevisitOnReachabilityChange[PHIBlock].set(InstrToDFSNum(
I));
2890 LLVM_DEBUG(
dbgs() <<
"Created phi of ops " << *ValuePHI <<
" for " << *
I
2899void NewGVN::initializeCongruenceClasses(Function &
F) {
2900 NextCongruenceNum = 0;
2910 TOPClass = createCongruenceClass(
nullptr,
nullptr);
2916 for (
auto *DTN :
nodes(DT)) {
2923 if (MemoryBlockDefs)
2924 for (
const auto &Def : *MemoryBlockDefs) {
2925 MemoryAccessToClass[&
Def] = TOPClass;
2930 TOPClass->memory_insert(MP);
2931 MemoryPhiState.
insert({MP, MPS_TOP});
2935 TOPClass->incStoreCount();
2941 for (
auto &
I : *BB) {
2943 for (
auto *U :
I.users())
2946 PHINodeUses.
insert(UInst);
2949 if (
I.isTerminator() &&
I.getType()->isVoidTy())
2951 TOPClass->insert(&
I);
2952 ValueToClass[&
I] = TOPClass;
2957 for (
auto &FA :
F.args())
2958 createSingletonCongruenceClass(&FA);
2961void NewGVN::cleanupTables() {
2962 for (CongruenceClass *&CC : CongruenceClasses) {
2963 LLVM_DEBUG(
dbgs() <<
"Congruence class " << CC->getID() <<
" has "
2964 << CC->size() <<
" members\n");
2972 SmallVector<Instruction *, 8> TempInst(AllTempInstructions.
begin(),
2973 AllTempInstructions.
end());
2974 AllTempInstructions.
clear();
2978 for (
auto *
I : TempInst) {
2979 I->dropAllReferences();
2982 while (!TempInst.empty()) {
2983 auto *
I = TempInst.pop_back_val();
2987 ValueToClass.
clear();
2988 ArgRecycler.
clear(ExpressionAllocator);
2989 ExpressionAllocator.
Reset();
2990 CongruenceClasses.clear();
2991 ExpressionToClass.clear();
2992 ValueToExpression.
clear();
2994 AdditionalUsers.
clear();
2995 ExpressionToPhiOfOps.
clear();
2996 TempToBlock.
clear();
2997 TempToMemory.
clear();
2998 PHINodeUses.
clear();
2999 OpSafeForPHIOfOps.
clear();
3000 ReachableBlocks.
clear();
3001 ReachableEdges.
clear();
3003 ProcessedCount.
clear();
3006 InstructionsToErase.
clear();
3008 BlockInstRange.
clear();
3009 TouchedInstructions.
clear();
3010 MemoryAccessToClass.
clear();
3011 PredicateToUsers.
clear();
3012 MemoryToUsers.
clear();
3013 RevisitOnReachabilityChange.
clear();
3014 PredicateSwapChoice.
clear();
3019std::pair<unsigned, unsigned> NewGVN::assignDFSNumbers(BasicBlock *
B,
3021 unsigned End =
Start;
3022 if (MemoryAccess *MemPhi = getMemoryAccess(
B)) {
3023 InstrDFS[MemPhi] = End++;
3028 for (
auto &
I : *
B) {
3034 LLVM_DEBUG(
dbgs() <<
"Skipping trivially dead instruction " <<
I <<
"\n");
3036 markInstructionForDeletion(&
I);
3040 RevisitOnReachabilityChange[
B].set(End);
3041 InstrDFS[&
I] = End++;
3048 return std::make_pair(Start, End);
3051void NewGVN::updateProcessedCount(
const Value *V) {
3053 assert(++ProcessedCount[V] < 100 &&
3054 "Seem to have processed the same Value a lot");
3059void NewGVN::valueNumberMemoryPhi(MemoryPhi *MP) {
3066 return cast<MemoryAccess>(U) != MP &&
3067 !isMemoryAccessTOP(cast<MemoryAccess>(U)) &&
3068 ReachableEdges.count({MP->getIncomingBlock(U), PHIBlock});
3073 if (Filtered.begin() == Filtered.end()) {
3074 if (setMemoryClass(MP, TOPClass))
3075 markMemoryUsersTouched(MP);
3081 auto LookupFunc = [&](
const Use &
U) {
3084 auto MappedBegin =
map_iterator(Filtered.begin(), LookupFunc);
3085 auto MappedEnd =
map_iterator(Filtered.end(), LookupFunc);
3089 const auto *AllSameValue = *MappedBegin;
3091 bool AllEqual = std::all_of(
3092 MappedBegin, MappedEnd,
3093 [&AllSameValue](
const MemoryAccess *V) {
return V == AllSameValue; });
3096 LLVM_DEBUG(
dbgs() <<
"Memory Phi value numbered to " << *AllSameValue
3105 CongruenceClass *CC =
3106 AllEqual ? getMemoryClass(AllSameValue) : ensureLeaderOfMemoryClass(MP);
3107 auto OldState = MemoryPhiState.
lookup(MP);
3108 assert(OldState != MPS_Invalid &&
"Invalid memory phi state");
3109 auto NewState = AllEqual ? MPS_Equivalent : MPS_Unique;
3110 MemoryPhiState[MP] = NewState;
3111 if (setMemoryClass(MP, CC) || OldState != NewState)
3112 markMemoryUsersTouched(MP);
3117void NewGVN::valueNumberInstruction(Instruction *
I) {
3119 if (!
I->isTerminator()) {
3121 SmallPtrSet<Value *, 2> Visited;
3123 auto Res = performSymbolicEvaluation(
I, Visited);
3124 Symbolized = Res.Expr;
3125 addAdditionalUsers(Res,
I);
3130 auto *PHIE = makePossiblePHIOfOps(
I, Visited);
3135 }
else if (
auto *
Op = RealToTemp.
lookup(
I)) {
3136 removePhiOfOps(
I,
Op);
3145 if (Symbolized ==
nullptr)
3146 Symbolized = createUnknownExpression(
I);
3147 performCongruenceFinding(
I, Symbolized);
3152 if (!
I->getType()->isVoidTy()) {
3153 auto *Symbolized = createUnknownExpression(
I);
3154 performCongruenceFinding(
I, Symbolized);
3156 processOutgoingEdges(
I,
I->getParent());
3162bool NewGVN::singleReachablePHIPath(
3163 SmallPtrSet<const MemoryAccess *, 8> &Visited,
const MemoryAccess *
First,
3164 const MemoryAccess *Second)
const {
3165 if (
First == Second)
3178 const auto *EndDef =
First;
3180 if (ChainDef == Second)
3187 auto ReachableOperandPred = [&](
const Use &
U) {
3190 auto FilteredPhiArgs =
3204void NewGVN::verifyMemoryCongruency()
const {
3207 for (
const auto *CC : CongruenceClasses) {
3208 if (CC == TOPClass || CC->isDead())
3210 if (CC->getStoreCount() != 0) {
3212 "Any class with a store as a leader should have a "
3213 "representative stored value");
3214 assert(CC->getMemoryLeader() &&
3215 "Any congruence class with a store should have a "
3216 "representative access");
3219 if (CC->getMemoryLeader())
3220 assert(MemoryAccessToClass.
lookup(CC->getMemoryLeader()) == CC &&
3221 "Representative MemoryAccess does not appear to be reverse "
3223 for (
const auto *M : CC->memory())
3225 "Memory member does not appear to be reverse mapped properly");
3233 auto ReachableAccessPred =
3234 [&](
const std::pair<const MemoryAccess *, CongruenceClass *> Pair) {
3235 bool Result = ReachableBlocks.
count(Pair.first->getBlock());
3237 MemoryToDFSNum(Pair.first) == 0)
3245 for (
const auto &U : MemPHI->incoming_values()) {
3258 for (
auto KV : Filtered) {
3261 if (FirstMUD && SecondMUD) {
3262 SmallPtrSet<const MemoryAccess *, 8> VisitedMAS;
3263 assert((singleReachablePHIPath(VisitedMAS, FirstMUD, SecondMUD) ||
3264 ValueToClass.
lookup(FirstMUD->getMemoryInst()) ==
3265 ValueToClass.
lookup(SecondMUD->getMemoryInst())) &&
3266 "The instructions for these memory operations should have "
3267 "been in the same congruence class or reachable through"
3268 "a single argument phi");
3273 auto ReachableOperandPred = [&](
const Use &
U) {
3274 return ReachableEdges.
count(
3275 {FirstMP->getIncomingBlock(U), FirstMP->getBlock()}) &&
3279 auto FilteredPhiArgs =
3282 std::transform(FilteredPhiArgs.begin(), FilteredPhiArgs.end(),
3283 std::back_inserter(PhiOpClasses), [&](
const Use &U) {
3284 const MemoryDef *MD = cast<MemoryDef>(U);
3285 return ValueToClass.lookup(MD->getMemoryInst());
3288 "All MemoryPhi arguments should be in the same class");
3297void NewGVN::verifyIterationSettled(Function &
F) {
3307 std::map<const Value *, CongruenceClass> BeforeIteration;
3309 for (
auto &KV : ValueToClass) {
3312 if (InstrToDFSNum(
I) == 0)
3314 BeforeIteration.insert({KV.first, *KV.second});
3317 TouchedInstructions.
set();
3318 TouchedInstructions.
reset(0);
3319 OpSafeForPHIOfOps.
clear();
3321 iterateTouchedInstructions();
3322 DenseSet<std::pair<const CongruenceClass *, const CongruenceClass *>>
3324 for (
const auto &KV : ValueToClass) {
3327 if (InstrToDFSNum(
I) == 0)
3331 auto *BeforeCC = &BeforeIteration.find(KV.first)->second;
3332 auto *AfterCC = KV.second;
3335 if (!EqualClasses.
count({BeforeCC, AfterCC})) {
3336 assert(BeforeCC->isEquivalentTo(AfterCC) &&
3337 "Value number changed after main loop completed!");
3338 EqualClasses.
insert({BeforeCC, AfterCC});
3349void NewGVN::verifyStoreExpressions()
const {
3354 std::pair<
const Value *,
3355 std::tuple<const Value *, const CongruenceClass *, Value *>>>
3357 for (
const auto &KV : ExpressionToClass) {
3360 auto Res = StoreExpressionSet.insert(
3361 {SE->getOperand(0), std::make_tuple(SE->getMemoryLeader(), KV.second,
3362 SE->getStoredValue())});
3363 bool Okay = Res.second;
3368 Okay = (std::get<1>(Res.first->second) == KV.second) &&
3369 (lookupOperandLeader(std::get<2>(Res.first->second)) ==
3370 lookupOperandLeader(SE->getStoredValue()));
3371 assert(Okay &&
"Stored expression conflict exists in expression table");
3372 auto *ValueExpr = ValueToExpression.
lookup(SE->getStoreInst());
3373 assert(ValueExpr && ValueExpr->equals(*SE) &&
3374 "StoreExpression in ExpressionToClass is not latest "
3375 "StoreExpression for value");
3384void NewGVN::iterateTouchedInstructions() {
3385 uint64_t Iterations = 0;
3387 int FirstInstr = TouchedInstructions.
find_first();
3389 if (FirstInstr == -1)
3391 const BasicBlock *LastBlock = getBlockForValue(InstrFromDFSNum(FirstInstr));
3392 while (TouchedInstructions.
any()) {
3398 for (
unsigned InstrNum : TouchedInstructions.
set_bits()) {
3402 if (InstrNum == 0) {
3403 TouchedInstructions.
reset(InstrNum);
3407 Value *
V = InstrFromDFSNum(InstrNum);
3408 const BasicBlock *CurrBlock = getBlockForValue(V);
3411 if (CurrBlock != LastBlock) {
3412 LastBlock = CurrBlock;
3413 bool BlockReachable = ReachableBlocks.
count(CurrBlock);
3414 const auto &CurrInstRange = BlockInstRange.
lookup(CurrBlock);
3417 if (!BlockReachable) {
3418 TouchedInstructions.
reset(CurrInstRange.first, CurrInstRange.second);
3421 <<
" because it is unreachable\n");
3426 updateProcessedCount(CurrBlock);
3430 TouchedInstructions.
reset(InstrNum);
3434 valueNumberMemoryPhi(MP);
3436 valueNumberInstruction(
I);
3440 updateProcessedCount(V);
3443 NumGVNMaxIterations = std::max(NumGVNMaxIterations.getValue(), Iterations);
3447bool NewGVN::runGVN() {
3451 NumFuncArgs =
F.arg_size();
3453 SingletonDeadExpression =
new (ExpressionAllocator)
DeadExpression();
3457 unsigned ICount = 1;
3468 ReversePostOrderTraversal<Function *> RPOT(&
F);
3469 unsigned Counter = 0;
3470 for (
auto &
B : RPOT) {
3472 assert(Node &&
"RPO and Dominator tree should have same reachability");
3473 RPOOrdering[
Node] = ++Counter;
3476 for (
auto &
B : RPOT) {
3478 if (
Node->getNumChildren() > 1)
3480 return RPOOrdering[
A] < RPOOrdering[
B];
3487 const auto &BlockRange = assignDFSNumbers(
B, ICount);
3488 BlockInstRange.
insert({
B, BlockRange});
3489 ICount += BlockRange.second - BlockRange.first;
3491 initializeCongruenceClasses(
F);
3493 TouchedInstructions.
resize(ICount);
3497 ExpressionToClass.reserve(ICount);
3500 const auto &InstRange = BlockInstRange.
lookup(&
F.getEntryBlock());
3501 TouchedInstructions.
set(InstRange.first, InstRange.second);
3503 <<
" marked reachable\n");
3504 ReachableBlocks.
insert(&
F.getEntryBlock());
3508 iterateTouchedInstructions();
3509 verifyMemoryCongruency();
3510 verifyIterationSettled(
F);
3511 verifyStoreExpressions();
3513 Changed |= eliminateInstructions(
F);
3516 for (Instruction *ToErase : InstructionsToErase) {
3517 if (!ToErase->use_empty())
3520 assert(ToErase->getParent() &&
3521 "BB containing ToErase deleted unexpectedly!");
3522 ToErase->eraseFromParent();
3524 Changed |= !InstructionsToErase.empty();
3527 auto UnreachableBlockPred = [&](
const BasicBlock &BB) {
3528 return !ReachableBlocks.
count(&BB);
3533 <<
" is unreachable\n");
3534 deleteInstructionsInBlock(&BB);
3603void NewGVN::convertClassToDFSOrdered(
3612 assert(BB &&
"Should have figured out a basic block for value");
3621 auto Leader = lookupOperandLeader(
SI->getValueOperand());
3623 VDDef.Def.setPointer(Leader);
3625 VDDef.Def.setPointer(
SI->getValueOperand());
3626 VDDef.Def.setInt(
true);
3629 VDDef.Def.setPointer(
D);
3632 "The dense set member should always be an instruction");
3637 if (
auto *PN = RealToTemp.
lookup(Def)) {
3641 VDDef.Def.setInt(
false);
3642 VDDef.Def.setPointer(PN);
3648 unsigned int UseCount = 0;
3650 for (
auto &U :
Def->uses()) {
3653 if (InstructionsToErase.count(
I))
3659 IBlock =
P->getIncomingBlock(U);
3664 IBlock = getBlockForValue(
I);
3670 if (!ReachableBlocks.
contains(IBlock))
3686 ProbablyDead.
insert(Def);
3688 UseCounts[
Def] = UseCount;
3694void NewGVN::convertClassToLoadsAndStores(
3695 const CongruenceClass &
Dense,
3696 SmallVectorImpl<ValueDFS> &LoadsAndStores)
const {
3706 VD.Def.setPointer(
D);
3720 I->replaceAllUsesWith(Repl);
3723void NewGVN::deleteInstructionsInBlock(BasicBlock *BB) {
3725 ++NumGVNBlocksDeleted;
3729 auto StartPoint = BB->
rbegin();
3742 ++NumGVNInstrDeleted;
3752void NewGVN::markInstructionForDeletion(Instruction *
I) {
3754 InstructionsToErase.insert(
I);
3757void NewGVN::replaceInstruction(Instruction *
I,
Value *V) {
3762 markInstructionForDeletion(
I);
3769class ValueDFSStack {
3771 Value *
back()
const {
return ValueStack.back(); }
3772 std::pair<int, int> dfs_back()
const {
return DFSStack.back(); }
3774 void push_back(
Value *V,
int DFSIn,
int DFSOut) {
3775 ValueStack.emplace_back(V);
3776 DFSStack.emplace_back(DFSIn, DFSOut);
3779 bool empty()
const {
return DFSStack.empty(); }
3781 bool isInScope(
int DFSIn,
int DFSOut)
const {
3784 return DFSIn >= DFSStack.back().first && DFSOut <= DFSStack.back().second;
3787 void popUntilDFSScope(
int DFSIn,
int DFSOut) {
3790 assert(ValueStack.size() == DFSStack.size() &&
3791 "Mismatch between ValueStack and DFSStack");
3793 !DFSStack.empty() &&
3794 !(DFSIn >= DFSStack.back().first && DFSOut <= DFSStack.back().second)) {
3795 DFSStack.pop_back();
3796 ValueStack.pop_back();
3801 SmallVector<Value *, 8> ValueStack;
3808CongruenceClass *NewGVN::getClassForExpression(
const Expression *
E)
const {
3810 return ValueToClass.lookup(VE->getVariableValue());
3813 return ExpressionToClass.lookup(
E);
3819 const Instruction *OrigInst,
3820 const BasicBlock *BB)
const {
3823 return CE->getConstantValue();
3825 auto *
V = VE->getVariableValue();
3827 return VE->getVariableValue();
3830 auto *CC = getClassForExpression(
E);
3834 return CC->getLeader();
3836 for (
auto *Member : *CC) {
3838 if (MemberInst == OrigInst)
3843 if (DT->
dominates(getBlockForValue(MemberInst), BB))
3849bool NewGVN::eliminateInstructions(Function &
F) {
3873 bool AnythingReplaced =
false;
3881 auto ReplaceUnreachablePHIArgs = [&](PHINode *
PHI,
BasicBlock *BB) {
3882 for (
auto &Operand :
PHI->incoming_values())
3883 if (!ReachableEdges.
count({PHI->getIncomingBlock(Operand), BB})) {
3887 <<
" with poison due to it being unreachable\n");
3900 DenseMap<const BasicBlock *, unsigned> ReachablePredCount;
3901 for (
auto &KV : ReachableEdges)
3902 ReachablePredCount[KV.getEnd()]++;
3903 for (
auto &BBPair : RevisitOnReachabilityChange) {
3904 for (
auto InstNum : BBPair.second) {
3905 auto *Inst = InstrFromDFSNum(InstNum);
3910 auto *BB = BBPair.first;
3911 if (ReachablePredCount.
lookup(BB) !=
PHI->getNumIncomingValues())
3912 ReplaceUnreachablePHIArgs(
PHI, BB);
3917 DenseMap<const Value *, unsigned int> UseCounts;
3918 for (
auto *CC :
reverse(CongruenceClasses)) {
3919 LLVM_DEBUG(
dbgs() <<
"Eliminating in congruence class " << CC->getID()
3924 SmallPtrSet<Instruction *, 8> ProbablyDead;
3925 if (CC->isDead() || CC->empty())
3928 if (CC == TOPClass) {
3929 for (
auto *M : *CC) {
3930 auto *VTE = ValueToExpression.
lookup(M);
3935 "Everything in TOP should be unreachable or dead at this "
3941 assert(CC->getLeader() &&
"We should have had a leader");
3947 CC->getStoredValue() ? CC->getStoredValue() : CC->getLeader();
3949 CongruenceClass::MemberSet MembersLeft;
3950 for (
auto *M : *CC) {
3954 Member->getType()->isVoidTy()) {
3955 MembersLeft.
insert(Member);
3959 LLVM_DEBUG(
dbgs() <<
"Found replacement " << *(Leader) <<
" for "
3960 << *Member <<
"\n");
3962 assert(Leader !=
I &&
"About to accidentally remove our leader");
3963 replaceInstruction(
I, Leader);
3964 AnythingReplaced =
true;
3966 CC->swap(MembersLeft);
3969 if (CC->size() != 1 || RealToTemp.
count(Leader)) {
3974 ValueDFSStack EliminationStack;
3978 convertClassToDFSOrdered(*CC, DFSOrderedSet, UseCounts, ProbablyDead);
3982 for (
auto &VD : DFSOrderedSet) {
3983 int MemberDFSIn = VD.
DFSIn;
3984 int MemberDFSOut = VD.
DFSOut;
3986 bool FromStore = VD.Def.getInt();
3989 if (Def &&
Def->getType()->isVoidTy())
3992 if (DefInst && AllTempInstructions.
count(DefInst)) {
3998 AllTempInstructions.
erase(PN);
3999 auto *DefBlock = getBlockForValue(Def);
4003 PN->insertBefore(DefBlock->begin());
4005 NumGVNPHIOfOpsEliminations++;
4008 if (EliminationStack.empty()) {
4012 << EliminationStack.dfs_back().first <<
","
4013 << EliminationStack.dfs_back().second <<
")\n");
4016 LLVM_DEBUG(
dbgs() <<
"Current DFS numbers are (" << MemberDFSIn <<
","
4017 << MemberDFSOut <<
")\n");
4031 bool ShouldPush =
Def && EliminationStack.empty();
4033 !EliminationStack.isInScope(MemberDFSIn, MemberDFSOut);
4035 if (OutOfScope || ShouldPush) {
4037 EliminationStack.popUntilDFSScope(MemberDFSIn, MemberDFSOut);
4038 bool ShouldPush =
Def && EliminationStack.empty();
4040 EliminationStack.push_back(Def, MemberDFSIn, MemberDFSOut);
4060 if (!EliminationStack.empty() && DefI && !FromStore) {
4061 Value *DominatingLeader = EliminationStack.back();
4062 if (DominatingLeader != Def) {
4070 for (
auto *DVR : DVRUsers)
4071 DVR->replaceVariableLocationOp(DefI, DominatingLeader);
4073 markInstructionForDeletion(DefI);
4082 "Current def should have been an instruction");
4084 "Current user should have been an instruction");
4091 if (InstructionsToErase.count(InstUse)) {
4092 auto &UseCount = UseCounts[
U->get()];
4093 if (--UseCount == 0) {
4100 if (EliminationStack.empty())
4103 Value *DominatingLeader = EliminationStack.back();
4107 if (BC->getType() == BC->getOperand(0)->getType() &&
4108 PredInfo->getPredicateInfoFor(DominatingLeader)) {
4110 DominatingLeader = BC->getOperand(0);
4115 if (
U->get() == DominatingLeader)
4122 auto *PI = PredInfo->getPredicateInfoFor(ReplacedInst);
4123 if (!PI || DominatingLeader != PI->OriginalOp)
4127 <<
"Found replacement " << *DominatingLeader <<
" for "
4128 << *
U->get() <<
" in " << *(
U->getUser()) <<
"\n");
4129 U->set(DominatingLeader);
4132 auto &LeaderUseCount = UseCounts[DominatingLeader];
4139 auto It = UseCounts.
find(SSACopy);
4140 if (It != UseCounts.
end()) {
4141 unsigned &IIUseCount = It->second;
4142 if (--IIUseCount == 0)
4143 ProbablyDead.
insert(SSACopy);
4147 AnythingReplaced =
true;
4154 for (
auto *
I : ProbablyDead)
4156 markInstructionForDeletion(
I);
4159 CongruenceClass::MemberSet MembersLeft;
4160 for (
auto *Member : *CC)
4163 MembersLeft.
insert(Member);
4164 CC->swap(MembersLeft);
4167 if (CC->getStoreCount() > 0) {
4168 convertClassToLoadsAndStores(*CC, PossibleDeadStores);
4170 ValueDFSStack EliminationStack;
4171 for (
auto &VD : PossibleDeadStores) {
4172 int MemberDFSIn = VD.
DFSIn;
4173 int MemberDFSOut = VD.
DFSOut;
4175 if (EliminationStack.empty() ||
4176 !EliminationStack.isInScope(MemberDFSIn, MemberDFSOut)) {
4178 EliminationStack.popUntilDFSScope(MemberDFSIn, MemberDFSOut);
4179 if (EliminationStack.empty()) {
4180 EliminationStack.push_back(Member, MemberDFSIn, MemberDFSOut);
4187 assert(!EliminationStack.empty());
4193 <<
" that is dominated by " << *Leader <<
"\n");
4194 markInstructionForDeletion(Member);
4200 return AnythingReplaced;
4208unsigned int NewGVN::getRank(
const Value *V)
const {
4223 return 4 +
A->getArgNo();
4227 unsigned Result = InstrToDFSNum(V);
4229 return 5 + NumFuncArgs +
Result;
4236bool NewGVN::shouldSwapOperands(
const Value *
A,
const Value *
B)
const {
4240 return std::make_pair(getRank(
A),
A) > std::make_pair(getRank(
B),
B);
4243bool NewGVN::shouldSwapOperandsForPredicate(
const Value *
A,
const Value *
B,
4244 const BitCastInst *
I)
const {
4245 if (shouldSwapOperands(
A,
B)) {
4246 PredicateSwapChoice[
I] =
B;
4251 if (LookupResult != PredicateSwapChoice.
end()) {
4253 if (SeenPredicate) {
4255 if (SeenPredicate ==
B)
4274 NewGVN(
F, &DT, &AC, &TLI, &
AA, &MSSA,
F.getDataLayout())
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Unify divergent function exit nodes
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis false
This file defines the BumpPtrAllocator interface.
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file provides an implementation of debug counters.
#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC)
This file defines DenseMapInfo traits for DenseMap.
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
early cse Early CSE w MemorySSA
The header file for the GVN pass that contains expression handling classes.
static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl)
This is the interface for a simple mod/ref and alias analysis over globals.
This file defines the little GraphTraits<X> template class that should be specialized by classes that...
This defines the Use class.
static bool lookup(const GsymReader &GR, DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Branch Probability Basic Block static false std::string getBlockName(const MachineBasicBlock *BB)
Helper to print the name of a MBB.
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
static bool alwaysAvailable(Value *V)
static Value * getCopyOf(const Value *V)
static bool isCopyOfPHI(const Value *V, const PHINode *PN)
static bool isCopyOfAPHI(const Value *V)
static bool okayForPHIOfOps(const Instruction *I)
static cl::opt< bool > EnableStoreRefinement("enable-store-refinement", cl::init(false), cl::Hidden)
static bool equalsLoadStoreHelper(const T &LHS, const Expression &RHS)
static cl::opt< bool > EnablePhiOfOps("enable-phi-of-ops", cl::init(true), cl::Hidden)
Currently, the generation "phi of ops" can result in correctness issues.
This file provides the interface for LLVM's Global Value Numbering pass.
This file defines the PointerIntPair class.
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
This file implements the PredicateInfo analysis, which creates an Extended SSA form for operations us...
const SmallVectorImpl< MachineOperand > & Cond
bool isDead(const MachineInstr &MI, const MachineRegisterInfo &MRI)
This file defines generic set operations that may be used on set's of different types,...
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the SparseBitVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
A manager for alias analyses.
bool isMustAlias(const MemoryLocation &LocA, const MemoryLocation &LocB)
A trivial helper function to check to see if the specified pointers are must-alias.
bool doesNotAccessMemory(const CallBase *Call)
Checks if the specified call is known to never read or write memory.
bool onlyReadsMemory(const CallBase *Call)
Checks if the specified call is known to only read from non-volatile memory (or not access memory at ...
A container for analyses that lazily runs them and caches their results.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Recycle small arrays allocated from a BumpPtrAllocator.
void clear(AllocatorType &Allocator)
Release all the tracked allocations to the allocator.
size_t size() const
size - Get the array size.
A function analysis which provides an AssumptionCache.
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
const Function * getParent() const
Return the enclosing method, or null if none.
reverse_iterator rbegin()
InstListType::reverse_iterator reverse_iterator
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
int find_first() const
find_first - Returns the index of the first set bit, -1 if none of the bits are set.
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
void clear()
clear - Removes all bits from the bitvector.
bool any() const
any - Returns true if any bit is set.
iterator_range< const_set_bits_iterator > set_bits() const
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it,...
void Deallocate(const void *Ptr, size_t Size, size_t)
bool isConvergent() const
Determine if the invoke is convergent.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
static LLVM_ABI ConstantInt * getBool(LLVMContext &Context, bool V)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
static CounterState getCounterState(unsigned ID)
static bool isCounterSet(unsigned ID)
static bool shouldExecute(unsigned CounterName)
static void setCounterState(unsigned ID, CounterState State)
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
iterator find(const_arg_type_t< KeyT > Val)
bool erase(const KeyT &Val)
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Implements a dense probed hash-table based set.
unsigned getDFSNumIn() const
getDFSNumIn/getDFSNumOut - These return the DFS visitation order for nodes in the dominator tree.
unsigned getDFSNumOut() const
Analysis pass which computes a DominatorTree.
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
void updateDFSNumbers() const
updateDFSNumbers - Assign In and Out numbers to the nodes while walking dominator tree in dfs order.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Class representing an expression and its matching format.
bool isPresplitCoroutine() const
Determine if the function is presplit coroutine.
~AggregateValueExpression() override
void allocateOperands(RecyclerType &Recycler, BumpPtrAllocator &Allocator)
~BasicExpression() override
bool equals(const Expression &Other) const override
~CallExpression() override
void setOpcode(unsigned opcode)
bool equals(const Expression &Other) const override
~LoadExpression() override
bool equals(const Expression &Other) const override
~PHIExpression() override
bool equals(const Expression &Other) const override
~StoreExpression() override
Value * getStoredValue() const
static LLVM_ABI std::optional< bool > isImpliedByMatchingCmp(CmpPredicate Pred1, CmpPredicate Pred2)
Determine if Pred1 implies Pred2 is true, false, or if nothing can be inferred about the implication,...
LLVM_ABI bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
LLVM_ABI bool isAtomic() const LLVM_READONLY
Return true if this instruction has an AtomicOrdering of unordered or higher.
LLVM_ABI void insertBefore(InstListType::iterator InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified position.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
Value * getPointerOperand()
BasicBlock * getBlock() const
BasicBlock * getIncomingBlock(unsigned I) const
Return incoming basic block number i.
An analysis that produces MemorySSA for a function.
This is the generic walker interface for walkers of MemorySSA.
MemoryAccess * getClobberingMemoryAccess(const Instruction *I, BatchAAResults &AA)
Given a memory Mod/Ref/ModRef'ing instruction, calling this will give you the nearest dominating Memo...
Encapsulates MemorySSA, including all data associated with memory accesses.
LLVM_ABI MemorySSAWalker * getWalker()
MemoryUseOrDef * getMemoryAccess(const Instruction *I) const
Given a memory Mod/Ref'ing instruction, get the MemorySSA access associated with it.
MemoryAccess * getLiveOnEntryDef() const
const DefsList * getBlockDefs(const BasicBlock *BB) const
Return the list of MemoryDef's and MemoryPhi's for a given basic block.
bool isLiveOnEntryDef(const MemoryAccess *MA) const
Return true if MA represents the live on entry value.
PreservedAnalyses run(Function &F, AnalysisManager< Function > &AM)
Run the pass over the function.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
PointerIntPair - This class implements a pair of a pointer and small integer.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserve()
Mark an analysis as preserved.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
SmallPtrSetIterator< PtrType > const_iterator
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
void insert_range(Range &&R)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
bool isPointerTy() const
True if this is an instance of PointerType.
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
Value * getOperand(unsigned i) const
unsigned getNumOperands() const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
iterator_range< user_iterator > users()
std::pair< iterator, bool > insert(const ValueT &V)
bool erase(const ValueT &V)
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
const ParentTy * getParent() const
self_iterator getIterator()
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract Attribute helper functions.
@ C
The default llvm calling convention, compatible with C.
@ BasicBlock
Various leaf nodes.
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
bool match(Val *V, const Pattern &P)
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
int analyzeLoadFromClobberingStore(Type *LoadTy, Value *LoadPtr, StoreInst *DepSI, const DataLayout &DL)
This function determines whether a value for the pointer LoadPtr can be extracted from the store at D...
Constant * getConstantValueForLoad(Constant *SrcVal, unsigned Offset, Type *LoadTy, const DataLayout &DL)
int analyzeLoadFromClobberingLoad(Type *LoadTy, Value *LoadPtr, LoadInst *DepLI, const DataLayout &DL)
This function determines whether a value for the pointer LoadPtr can be extracted from the load at De...
Constant * getConstantMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset, Type *LoadTy, const DataLayout &DL)
int analyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr, MemIntrinsic *DepMI, const DataLayout &DL)
This function determines whether a value for the pointer LoadPtr can be extracted from the memory int...
@ CE
Windows NT (Windows on ARM)
initializer< Ty > init(const Ty &Val)
std::vector< std::optional< ExecutorSymbolDef > > LookupResult
NodeAddr< DefNode * > Def
NodeAddr< UseNode * > Use
NodeAddr< NodeBase * > Node
friend class Instruction
Iterator for Instructions in a `BasicBlock.
LLVM_ABI Instruction & back() const
LLVM_ABI iterator begin() const
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI Value * simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef< Value * > Indices, GEPNoWrapFlags NW, const SimplifyQuery &Q)
Given operands for a GetElementPtrInst, fold the result or return null.
LLVM_ABI Constant * getInitialValueOfAllocation(const Value *V, const TargetLibraryInfo *TLI, Type *Ty)
If this is a call to an allocation function that initializes memory to a fixed value,...
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI void salvageDebugInfo(const MachineRegisterInfo &MRI, MachineInstr &MI)
Assuming the instruction MI is going to be deleted, attempt to salvage debug users of MI by writing t...
auto successors(const MachineBasicBlock *BB)
SDValue getStoredValue(SDValue Op)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
mapped_iterator< ItTy, FuncTy > map_iterator(ItTy I, FuncTy F)
bool set_is_subset(const S1Ty &S1, const S2Ty &S2)
set_is_subset(A, B) - Return true iff A in B
bool isa_and_nonnull(const Y &Val)
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
LLVM_ABI Value * simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty, const SimplifyQuery &Q)
Given operands for a CastInst, fold the result or return null.
DomTreeNodeBase< BasicBlock > DomTreeNode
auto dyn_cast_or_null(const Y &Val)
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
OutputIt transform(R &&Range, OutputIt d_first, UnaryFunction F)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
auto reverse(ContainerTy &&C)
void sort(IteratorTy Start, IteratorTy End)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI bool wouldInstructionBeTriviallyDead(const Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction would have no side effects if it was not used.
LLVM_ABI void patchReplacementInstruction(Instruction *I, Value *Repl)
Patch the replacement so that it is not more restrictive than the value being replaced.
iterator_range< filter_iterator< detail::IterOfRange< RangeT >, PredicateT > > make_filter_range(RangeT &&Range, PredicateT Pred)
Convenience function that takes a range of elements and a predicate, and return a new filter_iterator...
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
MutableArrayRef(T &OneElt) -> MutableArrayRef< T >
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
LLVM_ABI bool salvageKnowledge(Instruction *I, AssumptionCache *AC=nullptr, DominatorTree *DT=nullptr)
Calls BuildAssumeFromInst and if the resulting llvm.assume is valid insert if before I.
LLVM_ABI Value * simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a BinaryOperator, fold the result or return null.
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt copy(R &&Range, OutputIt Out)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
iterator_range< df_iterator< T > > depth_first(const T &G)
bool all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
LLVM_ABI Value * simplifyCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a CmpInst, fold the result or return null.
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
LLVM_ABI Constant * ConstantFoldInstOperands(const Instruction *I, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands.
AAResults AliasAnalysis
Temporary typedef for legacy code that uses a generic AliasAnalysis pointer or reference.
LLVM_ABI Value * simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q)
Given operands for a SelectInst, fold the result or return null.
LLVM_ABI void findDbgUsers(Value *V, SmallVectorImpl< DbgVariableRecord * > &DbgVariableRecords)
Finds the debug info records describing a value.
iterator_range< def_chain_iterator< T, true > > optimized_def_chain(T MA)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
PointerIntPair< Value *, 1, bool > Def
bool operator<(const ValueDFS &Other) const
DOTGraphTraits - Template class that can be specialized to customize how graphs are converted to 'dot...
static unsigned getHashValue(const ExactEqualsExpression &E)
static unsigned getHashValue(const Expression *E)
static const Expression * getTombstoneKey()
static bool isEqual(const Expression *LHS, const Expression *RHS)
static const Expression * getEmptyKey()
static bool isEqual(const ExactEqualsExpression &LHS, const Expression *RHS)
An information struct used to provide DenseMap with the various necessary components for a given valu...
SimplifyQuery getWithInstruction(const Instruction *I) const