LazyValueInfo.cpp 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  1. //===- LazyValueInfo.cpp - Value constraint analysis ------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the interface for lazy computation of value constraint
  10. // information.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Analysis/LazyValueInfo.h"
  14. #include "llvm/ADT/DenseSet.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/Analysis/AssumptionCache.h"
  17. #include "llvm/Analysis/ConstantFolding.h"
  18. #include "llvm/Analysis/InstructionSimplify.h"
  19. #include "llvm/Analysis/TargetLibraryInfo.h"
  20. #include "llvm/Analysis/ValueLattice.h"
  21. #include "llvm/Analysis/ValueTracking.h"
  22. #include "llvm/IR/AssemblyAnnotationWriter.h"
  23. #include "llvm/IR/CFG.h"
  24. #include "llvm/IR/ConstantRange.h"
  25. #include "llvm/IR/Constants.h"
  26. #include "llvm/IR/DataLayout.h"
  27. #include "llvm/IR/Dominators.h"
  28. #include "llvm/IR/Instructions.h"
  29. #include "llvm/IR/IntrinsicInst.h"
  30. #include "llvm/IR/Intrinsics.h"
  31. #include "llvm/IR/LLVMContext.h"
  32. #include "llvm/IR/PatternMatch.h"
  33. #include "llvm/IR/ValueHandle.h"
  34. #include "llvm/InitializePasses.h"
  35. #include "llvm/Support/Debug.h"
  36. #include "llvm/Support/FormattedStream.h"
  37. #include "llvm/Support/KnownBits.h"
  38. #include "llvm/Support/raw_ostream.h"
  39. #include <optional>
  40. using namespace llvm;
  41. using namespace PatternMatch;
  42. #define DEBUG_TYPE "lazy-value-info"
  43. // This is the number of worklist items we will process to try to discover an
  44. // answer for a given value.
  45. static const unsigned MaxProcessedPerValue = 500;
  46. char LazyValueInfoWrapperPass::ID = 0;
  47. LazyValueInfoWrapperPass::LazyValueInfoWrapperPass() : FunctionPass(ID) {
  48. initializeLazyValueInfoWrapperPassPass(*PassRegistry::getPassRegistry());
  49. }
  50. INITIALIZE_PASS_BEGIN(LazyValueInfoWrapperPass, "lazy-value-info",
  51. "Lazy Value Information Analysis", false, true)
  52. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  53. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  54. INITIALIZE_PASS_END(LazyValueInfoWrapperPass, "lazy-value-info",
  55. "Lazy Value Information Analysis", false, true)
  56. namespace llvm {
  57. FunctionPass *createLazyValueInfoPass() { return new LazyValueInfoWrapperPass(); }
  58. }
  59. AnalysisKey LazyValueAnalysis::Key;
  60. /// Returns true if this lattice value represents at most one possible value.
  61. /// This is as precise as any lattice value can get while still representing
  62. /// reachable code.
  63. static bool hasSingleValue(const ValueLatticeElement &Val) {
  64. if (Val.isConstantRange() &&
  65. Val.getConstantRange().isSingleElement())
  66. // Integer constants are single element ranges
  67. return true;
  68. if (Val.isConstant())
  69. // Non integer constants
  70. return true;
  71. return false;
  72. }
  73. /// Combine two sets of facts about the same value into a single set of
  74. /// facts. Note that this method is not suitable for merging facts along
  75. /// different paths in a CFG; that's what the mergeIn function is for. This
  76. /// is for merging facts gathered about the same value at the same location
  77. /// through two independent means.
  78. /// Notes:
  79. /// * This method does not promise to return the most precise possible lattice
  80. /// value implied by A and B. It is allowed to return any lattice element
  81. /// which is at least as strong as *either* A or B (unless our facts
  82. /// conflict, see below).
  83. /// * Due to unreachable code, the intersection of two lattice values could be
  84. /// contradictory. If this happens, we return some valid lattice value so as
  85. /// not confuse the rest of LVI. Ideally, we'd always return Undefined, but
  86. /// we do not make this guarantee. TODO: This would be a useful enhancement.
  87. static ValueLatticeElement intersect(const ValueLatticeElement &A,
  88. const ValueLatticeElement &B) {
  89. // Undefined is the strongest state. It means the value is known to be along
  90. // an unreachable path.
  91. if (A.isUnknown())
  92. return A;
  93. if (B.isUnknown())
  94. return B;
  95. // If we gave up for one, but got a useable fact from the other, use it.
  96. if (A.isOverdefined())
  97. return B;
  98. if (B.isOverdefined())
  99. return A;
  100. // Can't get any more precise than constants.
  101. if (hasSingleValue(A))
  102. return A;
  103. if (hasSingleValue(B))
  104. return B;
  105. // Could be either constant range or not constant here.
  106. if (!A.isConstantRange() || !B.isConstantRange()) {
  107. // TODO: Arbitrary choice, could be improved
  108. return A;
  109. }
  110. // Intersect two constant ranges
  111. ConstantRange Range =
  112. A.getConstantRange().intersectWith(B.getConstantRange());
  113. // Note: An empty range is implicitly converted to unknown or undef depending
  114. // on MayIncludeUndef internally.
  115. return ValueLatticeElement::getRange(
  116. std::move(Range), /*MayIncludeUndef=*/A.isConstantRangeIncludingUndef() ||
  117. B.isConstantRangeIncludingUndef());
  118. }
  119. //===----------------------------------------------------------------------===//
  120. // LazyValueInfoCache Decl
  121. //===----------------------------------------------------------------------===//
  122. namespace {
  123. /// A callback value handle updates the cache when values are erased.
  124. class LazyValueInfoCache;
  125. struct LVIValueHandle final : public CallbackVH {
  126. LazyValueInfoCache *Parent;
  127. LVIValueHandle(Value *V, LazyValueInfoCache *P = nullptr)
  128. : CallbackVH(V), Parent(P) { }
  129. void deleted() override;
  130. void allUsesReplacedWith(Value *V) override {
  131. deleted();
  132. }
  133. };
  134. } // end anonymous namespace
  135. namespace {
  136. using NonNullPointerSet = SmallDenseSet<AssertingVH<Value>, 2>;
  137. /// This is the cache kept by LazyValueInfo which
  138. /// maintains information about queries across the clients' queries.
  139. class LazyValueInfoCache {
  140. /// This is all of the cached information for one basic block. It contains
  141. /// the per-value lattice elements, as well as a separate set for
  142. /// overdefined values to reduce memory usage. Additionally pointers
  143. /// dereferenced in the block are cached for nullability queries.
  144. struct BlockCacheEntry {
  145. SmallDenseMap<AssertingVH<Value>, ValueLatticeElement, 4> LatticeElements;
  146. SmallDenseSet<AssertingVH<Value>, 4> OverDefined;
  147. // None indicates that the nonnull pointers for this basic block
  148. // block have not been computed yet.
  149. std::optional<NonNullPointerSet> NonNullPointers;
  150. };
  151. /// Cached information per basic block.
  152. DenseMap<PoisoningVH<BasicBlock>, std::unique_ptr<BlockCacheEntry>>
  153. BlockCache;
  154. /// Set of value handles used to erase values from the cache on deletion.
  155. DenseSet<LVIValueHandle, DenseMapInfo<Value *>> ValueHandles;
  156. const BlockCacheEntry *getBlockEntry(BasicBlock *BB) const {
  157. auto It = BlockCache.find_as(BB);
  158. if (It == BlockCache.end())
  159. return nullptr;
  160. return It->second.get();
  161. }
  162. BlockCacheEntry *getOrCreateBlockEntry(BasicBlock *BB) {
  163. auto It = BlockCache.find_as(BB);
  164. if (It == BlockCache.end())
  165. It = BlockCache.insert({ BB, std::make_unique<BlockCacheEntry>() })
  166. .first;
  167. return It->second.get();
  168. }
  169. void addValueHandle(Value *Val) {
  170. auto HandleIt = ValueHandles.find_as(Val);
  171. if (HandleIt == ValueHandles.end())
  172. ValueHandles.insert({ Val, this });
  173. }
  174. public:
  175. void insertResult(Value *Val, BasicBlock *BB,
  176. const ValueLatticeElement &Result) {
  177. BlockCacheEntry *Entry = getOrCreateBlockEntry(BB);
  178. // Insert over-defined values into their own cache to reduce memory
  179. // overhead.
  180. if (Result.isOverdefined())
  181. Entry->OverDefined.insert(Val);
  182. else
  183. Entry->LatticeElements.insert({ Val, Result });
  184. addValueHandle(Val);
  185. }
  186. std::optional<ValueLatticeElement>
  187. getCachedValueInfo(Value *V, BasicBlock *BB) const {
  188. const BlockCacheEntry *Entry = getBlockEntry(BB);
  189. if (!Entry)
  190. return std::nullopt;
  191. if (Entry->OverDefined.count(V))
  192. return ValueLatticeElement::getOverdefined();
  193. auto LatticeIt = Entry->LatticeElements.find_as(V);
  194. if (LatticeIt == Entry->LatticeElements.end())
  195. return std::nullopt;
  196. return LatticeIt->second;
  197. }
  198. bool isNonNullAtEndOfBlock(
  199. Value *V, BasicBlock *BB,
  200. function_ref<NonNullPointerSet(BasicBlock *)> InitFn) {
  201. BlockCacheEntry *Entry = getOrCreateBlockEntry(BB);
  202. if (!Entry->NonNullPointers) {
  203. Entry->NonNullPointers = InitFn(BB);
  204. for (Value *V : *Entry->NonNullPointers)
  205. addValueHandle(V);
  206. }
  207. return Entry->NonNullPointers->count(V);
  208. }
  209. /// clear - Empty the cache.
  210. void clear() {
  211. BlockCache.clear();
  212. ValueHandles.clear();
  213. }
  214. /// Inform the cache that a given value has been deleted.
  215. void eraseValue(Value *V);
  216. /// This is part of the update interface to inform the cache
  217. /// that a block has been deleted.
  218. void eraseBlock(BasicBlock *BB);
  219. /// Updates the cache to remove any influence an overdefined value in
  220. /// OldSucc might have (unless also overdefined in NewSucc). This just
  221. /// flushes elements from the cache and does not add any.
  222. void threadEdgeImpl(BasicBlock *OldSucc,BasicBlock *NewSucc);
  223. };
  224. }
  225. void LazyValueInfoCache::eraseValue(Value *V) {
  226. for (auto &Pair : BlockCache) {
  227. Pair.second->LatticeElements.erase(V);
  228. Pair.second->OverDefined.erase(V);
  229. if (Pair.second->NonNullPointers)
  230. Pair.second->NonNullPointers->erase(V);
  231. }
  232. auto HandleIt = ValueHandles.find_as(V);
  233. if (HandleIt != ValueHandles.end())
  234. ValueHandles.erase(HandleIt);
  235. }
  236. void LVIValueHandle::deleted() {
  237. // This erasure deallocates *this, so it MUST happen after we're done
  238. // using any and all members of *this.
  239. Parent->eraseValue(*this);
  240. }
  241. void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
  242. BlockCache.erase(BB);
  243. }
  244. void LazyValueInfoCache::threadEdgeImpl(BasicBlock *OldSucc,
  245. BasicBlock *NewSucc) {
  246. // When an edge in the graph has been threaded, values that we could not
  247. // determine a value for before (i.e. were marked overdefined) may be
  248. // possible to solve now. We do NOT try to proactively update these values.
  249. // Instead, we clear their entries from the cache, and allow lazy updating to
  250. // recompute them when needed.
  251. // The updating process is fairly simple: we need to drop cached info
  252. // for all values that were marked overdefined in OldSucc, and for those same
  253. // values in any successor of OldSucc (except NewSucc) in which they were
  254. // also marked overdefined.
  255. std::vector<BasicBlock*> worklist;
  256. worklist.push_back(OldSucc);
  257. const BlockCacheEntry *Entry = getBlockEntry(OldSucc);
  258. if (!Entry || Entry->OverDefined.empty())
  259. return; // Nothing to process here.
  260. SmallVector<Value *, 4> ValsToClear(Entry->OverDefined.begin(),
  261. Entry->OverDefined.end());
  262. // Use a worklist to perform a depth-first search of OldSucc's successors.
  263. // NOTE: We do not need a visited list since any blocks we have already
  264. // visited will have had their overdefined markers cleared already, and we
  265. // thus won't loop to their successors.
  266. while (!worklist.empty()) {
  267. BasicBlock *ToUpdate = worklist.back();
  268. worklist.pop_back();
  269. // Skip blocks only accessible through NewSucc.
  270. if (ToUpdate == NewSucc) continue;
  271. // If a value was marked overdefined in OldSucc, and is here too...
  272. auto OI = BlockCache.find_as(ToUpdate);
  273. if (OI == BlockCache.end() || OI->second->OverDefined.empty())
  274. continue;
  275. auto &ValueSet = OI->second->OverDefined;
  276. bool changed = false;
  277. for (Value *V : ValsToClear) {
  278. if (!ValueSet.erase(V))
  279. continue;
  280. // If we removed anything, then we potentially need to update
  281. // blocks successors too.
  282. changed = true;
  283. }
  284. if (!changed) continue;
  285. llvm::append_range(worklist, successors(ToUpdate));
  286. }
  287. }
  288. namespace {
  289. /// An assembly annotator class to print LazyValueCache information in
  290. /// comments.
  291. class LazyValueInfoImpl;
  292. class LazyValueInfoAnnotatedWriter : public AssemblyAnnotationWriter {
  293. LazyValueInfoImpl *LVIImpl;
  294. // While analyzing which blocks we can solve values for, we need the dominator
  295. // information.
  296. DominatorTree &DT;
  297. public:
  298. LazyValueInfoAnnotatedWriter(LazyValueInfoImpl *L, DominatorTree &DTree)
  299. : LVIImpl(L), DT(DTree) {}
  300. void emitBasicBlockStartAnnot(const BasicBlock *BB,
  301. formatted_raw_ostream &OS) override;
  302. void emitInstructionAnnot(const Instruction *I,
  303. formatted_raw_ostream &OS) override;
  304. };
  305. }
  306. namespace {
  307. // The actual implementation of the lazy analysis and update. Note that the
  308. // inheritance from LazyValueInfoCache is intended to be temporary while
  309. // splitting the code and then transitioning to a has-a relationship.
  310. class LazyValueInfoImpl {
  311. /// Cached results from previous queries
  312. LazyValueInfoCache TheCache;
  313. /// This stack holds the state of the value solver during a query.
  314. /// It basically emulates the callstack of the naive
  315. /// recursive value lookup process.
  316. SmallVector<std::pair<BasicBlock*, Value*>, 8> BlockValueStack;
  317. /// Keeps track of which block-value pairs are in BlockValueStack.
  318. DenseSet<std::pair<BasicBlock*, Value*> > BlockValueSet;
  319. /// Push BV onto BlockValueStack unless it's already in there.
  320. /// Returns true on success.
  321. bool pushBlockValue(const std::pair<BasicBlock *, Value *> &BV) {
  322. if (!BlockValueSet.insert(BV).second)
  323. return false; // It's already in the stack.
  324. LLVM_DEBUG(dbgs() << "PUSH: " << *BV.second << " in "
  325. << BV.first->getName() << "\n");
  326. BlockValueStack.push_back(BV);
  327. return true;
  328. }
  329. AssumptionCache *AC; ///< A pointer to the cache of @llvm.assume calls.
  330. const DataLayout &DL; ///< A mandatory DataLayout
  331. /// Declaration of the llvm.experimental.guard() intrinsic,
  332. /// if it exists in the module.
  333. Function *GuardDecl;
  334. std::optional<ValueLatticeElement> getBlockValue(Value *Val, BasicBlock *BB,
  335. Instruction *CxtI);
  336. std::optional<ValueLatticeElement> getEdgeValue(Value *V, BasicBlock *F,
  337. BasicBlock *T,
  338. Instruction *CxtI = nullptr);
  339. // These methods process one work item and may add more. A false value
  340. // returned means that the work item was not completely processed and must
  341. // be revisited after going through the new items.
  342. bool solveBlockValue(Value *Val, BasicBlock *BB);
  343. std::optional<ValueLatticeElement> solveBlockValueImpl(Value *Val,
  344. BasicBlock *BB);
  345. std::optional<ValueLatticeElement> solveBlockValueNonLocal(Value *Val,
  346. BasicBlock *BB);
  347. std::optional<ValueLatticeElement> solveBlockValuePHINode(PHINode *PN,
  348. BasicBlock *BB);
  349. std::optional<ValueLatticeElement> solveBlockValueSelect(SelectInst *S,
  350. BasicBlock *BB);
  351. std::optional<ConstantRange> getRangeFor(Value *V, Instruction *CxtI,
  352. BasicBlock *BB);
  353. std::optional<ValueLatticeElement> solveBlockValueBinaryOpImpl(
  354. Instruction *I, BasicBlock *BB,
  355. std::function<ConstantRange(const ConstantRange &, const ConstantRange &)>
  356. OpFn);
  357. std::optional<ValueLatticeElement>
  358. solveBlockValueBinaryOp(BinaryOperator *BBI, BasicBlock *BB);
  359. std::optional<ValueLatticeElement> solveBlockValueCast(CastInst *CI,
  360. BasicBlock *BB);
  361. std::optional<ValueLatticeElement>
  362. solveBlockValueOverflowIntrinsic(WithOverflowInst *WO, BasicBlock *BB);
  363. std::optional<ValueLatticeElement> solveBlockValueIntrinsic(IntrinsicInst *II,
  364. BasicBlock *BB);
  365. std::optional<ValueLatticeElement>
  366. solveBlockValueExtractValue(ExtractValueInst *EVI, BasicBlock *BB);
  367. bool isNonNullAtEndOfBlock(Value *Val, BasicBlock *BB);
  368. void intersectAssumeOrGuardBlockValueConstantRange(Value *Val,
  369. ValueLatticeElement &BBLV,
  370. Instruction *BBI);
  371. void solve();
  372. public:
  373. /// This is the query interface to determine the lattice value for the
  374. /// specified Value* at the context instruction (if specified) or at the
  375. /// start of the block.
  376. ValueLatticeElement getValueInBlock(Value *V, BasicBlock *BB,
  377. Instruction *CxtI = nullptr);
  378. /// This is the query interface to determine the lattice value for the
  379. /// specified Value* at the specified instruction using only information
  380. /// from assumes/guards and range metadata. Unlike getValueInBlock(), no
  381. /// recursive query is performed.
  382. ValueLatticeElement getValueAt(Value *V, Instruction *CxtI);
  383. /// This is the query interface to determine the lattice
  384. /// value for the specified Value* that is true on the specified edge.
  385. ValueLatticeElement getValueOnEdge(Value *V, BasicBlock *FromBB,
  386. BasicBlock *ToBB,
  387. Instruction *CxtI = nullptr);
  388. /// Complete flush all previously computed values
  389. void clear() {
  390. TheCache.clear();
  391. }
  392. /// Printing the LazyValueInfo Analysis.
  393. void printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) {
  394. LazyValueInfoAnnotatedWriter Writer(this, DTree);
  395. F.print(OS, &Writer);
  396. }
  397. /// This is part of the update interface to inform the cache
  398. /// that a block has been deleted.
  399. void eraseBlock(BasicBlock *BB) {
  400. TheCache.eraseBlock(BB);
  401. }
  402. /// This is the update interface to inform the cache that an edge from
  403. /// PredBB to OldSucc has been threaded to be from PredBB to NewSucc.
  404. void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
  405. LazyValueInfoImpl(AssumptionCache *AC, const DataLayout &DL,
  406. Function *GuardDecl)
  407. : AC(AC), DL(DL), GuardDecl(GuardDecl) {}
  408. };
  409. } // end anonymous namespace
  410. void LazyValueInfoImpl::solve() {
  411. SmallVector<std::pair<BasicBlock *, Value *>, 8> StartingStack(
  412. BlockValueStack.begin(), BlockValueStack.end());
  413. unsigned processedCount = 0;
  414. while (!BlockValueStack.empty()) {
  415. processedCount++;
  416. // Abort if we have to process too many values to get a result for this one.
  417. // Because of the design of the overdefined cache currently being per-block
  418. // to avoid naming-related issues (IE it wants to try to give different
  419. // results for the same name in different blocks), overdefined results don't
  420. // get cached globally, which in turn means we will often try to rediscover
  421. // the same overdefined result again and again. Once something like
  422. // PredicateInfo is used in LVI or CVP, we should be able to make the
  423. // overdefined cache global, and remove this throttle.
  424. if (processedCount > MaxProcessedPerValue) {
  425. LLVM_DEBUG(
  426. dbgs() << "Giving up on stack because we are getting too deep\n");
  427. // Fill in the original values
  428. while (!StartingStack.empty()) {
  429. std::pair<BasicBlock *, Value *> &e = StartingStack.back();
  430. TheCache.insertResult(e.second, e.first,
  431. ValueLatticeElement::getOverdefined());
  432. StartingStack.pop_back();
  433. }
  434. BlockValueSet.clear();
  435. BlockValueStack.clear();
  436. return;
  437. }
  438. std::pair<BasicBlock *, Value *> e = BlockValueStack.back();
  439. assert(BlockValueSet.count(e) && "Stack value should be in BlockValueSet!");
  440. if (solveBlockValue(e.second, e.first)) {
  441. // The work item was completely processed.
  442. assert(BlockValueStack.back() == e && "Nothing should have been pushed!");
  443. #ifndef NDEBUG
  444. std::optional<ValueLatticeElement> BBLV =
  445. TheCache.getCachedValueInfo(e.second, e.first);
  446. assert(BBLV && "Result should be in cache!");
  447. LLVM_DEBUG(
  448. dbgs() << "POP " << *e.second << " in " << e.first->getName() << " = "
  449. << *BBLV << "\n");
  450. #endif
  451. BlockValueStack.pop_back();
  452. BlockValueSet.erase(e);
  453. } else {
  454. // More work needs to be done before revisiting.
  455. assert(BlockValueStack.back() != e && "Stack should have been pushed!");
  456. }
  457. }
  458. }
  459. std::optional<ValueLatticeElement>
  460. LazyValueInfoImpl::getBlockValue(Value *Val, BasicBlock *BB,
  461. Instruction *CxtI) {
  462. // If already a constant, there is nothing to compute.
  463. if (Constant *VC = dyn_cast<Constant>(Val))
  464. return ValueLatticeElement::get(VC);
  465. if (std::optional<ValueLatticeElement> OptLatticeVal =
  466. TheCache.getCachedValueInfo(Val, BB)) {
  467. intersectAssumeOrGuardBlockValueConstantRange(Val, *OptLatticeVal, CxtI);
  468. return OptLatticeVal;
  469. }
  470. // We have hit a cycle, assume overdefined.
  471. if (!pushBlockValue({ BB, Val }))
  472. return ValueLatticeElement::getOverdefined();
  473. // Yet to be resolved.
  474. return std::nullopt;
  475. }
  476. static ValueLatticeElement getFromRangeMetadata(Instruction *BBI) {
  477. switch (BBI->getOpcode()) {
  478. default: break;
  479. case Instruction::Load:
  480. case Instruction::Call:
  481. case Instruction::Invoke:
  482. if (MDNode *Ranges = BBI->getMetadata(LLVMContext::MD_range))
  483. if (isa<IntegerType>(BBI->getType())) {
  484. return ValueLatticeElement::getRange(
  485. getConstantRangeFromMetadata(*Ranges));
  486. }
  487. break;
  488. };
  489. // Nothing known - will be intersected with other facts
  490. return ValueLatticeElement::getOverdefined();
  491. }
  492. bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) {
  493. assert(!isa<Constant>(Val) && "Value should not be constant");
  494. assert(!TheCache.getCachedValueInfo(Val, BB) &&
  495. "Value should not be in cache");
  496. // Hold off inserting this value into the Cache in case we have to return
  497. // false and come back later.
  498. std::optional<ValueLatticeElement> Res = solveBlockValueImpl(Val, BB);
  499. if (!Res)
  500. // Work pushed, will revisit
  501. return false;
  502. TheCache.insertResult(Val, BB, *Res);
  503. return true;
  504. }
  505. std::optional<ValueLatticeElement>
  506. LazyValueInfoImpl::solveBlockValueImpl(Value *Val, BasicBlock *BB) {
  507. Instruction *BBI = dyn_cast<Instruction>(Val);
  508. if (!BBI || BBI->getParent() != BB)
  509. return solveBlockValueNonLocal(Val, BB);
  510. if (PHINode *PN = dyn_cast<PHINode>(BBI))
  511. return solveBlockValuePHINode(PN, BB);
  512. if (auto *SI = dyn_cast<SelectInst>(BBI))
  513. return solveBlockValueSelect(SI, BB);
  514. // If this value is a nonnull pointer, record it's range and bailout. Note
  515. // that for all other pointer typed values, we terminate the search at the
  516. // definition. We could easily extend this to look through geps, bitcasts,
  517. // and the like to prove non-nullness, but it's not clear that's worth it
  518. // compile time wise. The context-insensitive value walk done inside
  519. // isKnownNonZero gets most of the profitable cases at much less expense.
  520. // This does mean that we have a sensitivity to where the defining
  521. // instruction is placed, even if it could legally be hoisted much higher.
  522. // That is unfortunate.
  523. PointerType *PT = dyn_cast<PointerType>(BBI->getType());
  524. if (PT && isKnownNonZero(BBI, DL))
  525. return ValueLatticeElement::getNot(ConstantPointerNull::get(PT));
  526. if (BBI->getType()->isIntegerTy()) {
  527. if (auto *CI = dyn_cast<CastInst>(BBI))
  528. return solveBlockValueCast(CI, BB);
  529. if (BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI))
  530. return solveBlockValueBinaryOp(BO, BB);
  531. if (auto *EVI = dyn_cast<ExtractValueInst>(BBI))
  532. return solveBlockValueExtractValue(EVI, BB);
  533. if (auto *II = dyn_cast<IntrinsicInst>(BBI))
  534. return solveBlockValueIntrinsic(II, BB);
  535. }
  536. LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
  537. << "' - unknown inst def found.\n");
  538. return getFromRangeMetadata(BBI);
  539. }
  540. static void AddNonNullPointer(Value *Ptr, NonNullPointerSet &PtrSet) {
  541. // TODO: Use NullPointerIsDefined instead.
  542. if (Ptr->getType()->getPointerAddressSpace() == 0)
  543. PtrSet.insert(getUnderlyingObject(Ptr));
  544. }
  545. static void AddNonNullPointersByInstruction(
  546. Instruction *I, NonNullPointerSet &PtrSet) {
  547. if (LoadInst *L = dyn_cast<LoadInst>(I)) {
  548. AddNonNullPointer(L->getPointerOperand(), PtrSet);
  549. } else if (StoreInst *S = dyn_cast<StoreInst>(I)) {
  550. AddNonNullPointer(S->getPointerOperand(), PtrSet);
  551. } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
  552. if (MI->isVolatile()) return;
  553. // FIXME: check whether it has a valuerange that excludes zero?
  554. ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength());
  555. if (!Len || Len->isZero()) return;
  556. AddNonNullPointer(MI->getRawDest(), PtrSet);
  557. if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
  558. AddNonNullPointer(MTI->getRawSource(), PtrSet);
  559. }
  560. }
  561. bool LazyValueInfoImpl::isNonNullAtEndOfBlock(Value *Val, BasicBlock *BB) {
  562. if (NullPointerIsDefined(BB->getParent(),
  563. Val->getType()->getPointerAddressSpace()))
  564. return false;
  565. Val = Val->stripInBoundsOffsets();
  566. return TheCache.isNonNullAtEndOfBlock(Val, BB, [](BasicBlock *BB) {
  567. NonNullPointerSet NonNullPointers;
  568. for (Instruction &I : *BB)
  569. AddNonNullPointersByInstruction(&I, NonNullPointers);
  570. return NonNullPointers;
  571. });
  572. }
  573. std::optional<ValueLatticeElement>
  574. LazyValueInfoImpl::solveBlockValueNonLocal(Value *Val, BasicBlock *BB) {
  575. ValueLatticeElement Result; // Start Undefined.
  576. // If this is the entry block, we must be asking about an argument. The
  577. // value is overdefined.
  578. if (BB->isEntryBlock()) {
  579. assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
  580. return ValueLatticeElement::getOverdefined();
  581. }
  582. // Loop over all of our predecessors, merging what we know from them into
  583. // result. If we encounter an unexplored predecessor, we eagerly explore it
  584. // in a depth first manner. In practice, this has the effect of discovering
  585. // paths we can't analyze eagerly without spending compile times analyzing
  586. // other paths. This heuristic benefits from the fact that predecessors are
  587. // frequently arranged such that dominating ones come first and we quickly
  588. // find a path to function entry. TODO: We should consider explicitly
  589. // canonicalizing to make this true rather than relying on this happy
  590. // accident.
  591. for (BasicBlock *Pred : predecessors(BB)) {
  592. std::optional<ValueLatticeElement> EdgeResult = getEdgeValue(Val, Pred, BB);
  593. if (!EdgeResult)
  594. // Explore that input, then return here
  595. return std::nullopt;
  596. Result.mergeIn(*EdgeResult);
  597. // If we hit overdefined, exit early. The BlockVals entry is already set
  598. // to overdefined.
  599. if (Result.isOverdefined()) {
  600. LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
  601. << "' - overdefined because of pred '"
  602. << Pred->getName() << "' (non local).\n");
  603. return Result;
  604. }
  605. }
  606. // Return the merged value, which is more precise than 'overdefined'.
  607. assert(!Result.isOverdefined());
  608. return Result;
  609. }
  610. std::optional<ValueLatticeElement>
  611. LazyValueInfoImpl::solveBlockValuePHINode(PHINode *PN, BasicBlock *BB) {
  612. ValueLatticeElement Result; // Start Undefined.
  613. // Loop over all of our predecessors, merging what we know from them into
  614. // result. See the comment about the chosen traversal order in
  615. // solveBlockValueNonLocal; the same reasoning applies here.
  616. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
  617. BasicBlock *PhiBB = PN->getIncomingBlock(i);
  618. Value *PhiVal = PN->getIncomingValue(i);
  619. // Note that we can provide PN as the context value to getEdgeValue, even
  620. // though the results will be cached, because PN is the value being used as
  621. // the cache key in the caller.
  622. std::optional<ValueLatticeElement> EdgeResult =
  623. getEdgeValue(PhiVal, PhiBB, BB, PN);
  624. if (!EdgeResult)
  625. // Explore that input, then return here
  626. return std::nullopt;
  627. Result.mergeIn(*EdgeResult);
  628. // If we hit overdefined, exit early. The BlockVals entry is already set
  629. // to overdefined.
  630. if (Result.isOverdefined()) {
  631. LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
  632. << "' - overdefined because of pred (local).\n");
  633. return Result;
  634. }
  635. }
  636. // Return the merged value, which is more precise than 'overdefined'.
  637. assert(!Result.isOverdefined() && "Possible PHI in entry block?");
  638. return Result;
  639. }
  640. static ValueLatticeElement getValueFromCondition(Value *Val, Value *Cond,
  641. bool isTrueDest = true);
  642. // If we can determine a constraint on the value given conditions assumed by
  643. // the program, intersect those constraints with BBLV
  644. void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
  645. Value *Val, ValueLatticeElement &BBLV, Instruction *BBI) {
  646. BBI = BBI ? BBI : dyn_cast<Instruction>(Val);
  647. if (!BBI)
  648. return;
  649. BasicBlock *BB = BBI->getParent();
  650. for (auto &AssumeVH : AC->assumptionsFor(Val)) {
  651. if (!AssumeVH)
  652. continue;
  653. // Only check assumes in the block of the context instruction. Other
  654. // assumes will have already been taken into account when the value was
  655. // propagated from predecessor blocks.
  656. auto *I = cast<CallInst>(AssumeVH);
  657. if (I->getParent() != BB || !isValidAssumeForContext(I, BBI))
  658. continue;
  659. BBLV = intersect(BBLV, getValueFromCondition(Val, I->getArgOperand(0)));
  660. }
  661. // If guards are not used in the module, don't spend time looking for them
  662. if (GuardDecl && !GuardDecl->use_empty() &&
  663. BBI->getIterator() != BB->begin()) {
  664. for (Instruction &I : make_range(std::next(BBI->getIterator().getReverse()),
  665. BB->rend())) {
  666. Value *Cond = nullptr;
  667. if (match(&I, m_Intrinsic<Intrinsic::experimental_guard>(m_Value(Cond))))
  668. BBLV = intersect(BBLV, getValueFromCondition(Val, Cond));
  669. }
  670. }
  671. if (BBLV.isOverdefined()) {
  672. // Check whether we're checking at the terminator, and the pointer has
  673. // been dereferenced in this block.
  674. PointerType *PTy = dyn_cast<PointerType>(Val->getType());
  675. if (PTy && BB->getTerminator() == BBI &&
  676. isNonNullAtEndOfBlock(Val, BB))
  677. BBLV = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
  678. }
  679. }
  680. static ConstantRange getConstantRangeOrFull(const ValueLatticeElement &Val,
  681. Type *Ty, const DataLayout &DL) {
  682. if (Val.isConstantRange())
  683. return Val.getConstantRange();
  684. return ConstantRange::getFull(DL.getTypeSizeInBits(Ty));
  685. }
  686. std::optional<ValueLatticeElement>
  687. LazyValueInfoImpl::solveBlockValueSelect(SelectInst *SI, BasicBlock *BB) {
  688. // Recurse on our inputs if needed
  689. std::optional<ValueLatticeElement> OptTrueVal =
  690. getBlockValue(SI->getTrueValue(), BB, SI);
  691. if (!OptTrueVal)
  692. return std::nullopt;
  693. ValueLatticeElement &TrueVal = *OptTrueVal;
  694. std::optional<ValueLatticeElement> OptFalseVal =
  695. getBlockValue(SI->getFalseValue(), BB, SI);
  696. if (!OptFalseVal)
  697. return std::nullopt;
  698. ValueLatticeElement &FalseVal = *OptFalseVal;
  699. if (TrueVal.isConstantRange() || FalseVal.isConstantRange()) {
  700. const ConstantRange &TrueCR =
  701. getConstantRangeOrFull(TrueVal, SI->getType(), DL);
  702. const ConstantRange &FalseCR =
  703. getConstantRangeOrFull(FalseVal, SI->getType(), DL);
  704. Value *LHS = nullptr;
  705. Value *RHS = nullptr;
  706. SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS);
  707. // Is this a min specifically of our two inputs? (Avoid the risk of
  708. // ValueTracking getting smarter looking back past our immediate inputs.)
  709. if (SelectPatternResult::isMinOrMax(SPR.Flavor) &&
  710. ((LHS == SI->getTrueValue() && RHS == SI->getFalseValue()) ||
  711. (RHS == SI->getTrueValue() && LHS == SI->getFalseValue()))) {
  712. ConstantRange ResultCR = [&]() {
  713. switch (SPR.Flavor) {
  714. default:
  715. llvm_unreachable("unexpected minmax type!");
  716. case SPF_SMIN: /// Signed minimum
  717. return TrueCR.smin(FalseCR);
  718. case SPF_UMIN: /// Unsigned minimum
  719. return TrueCR.umin(FalseCR);
  720. case SPF_SMAX: /// Signed maximum
  721. return TrueCR.smax(FalseCR);
  722. case SPF_UMAX: /// Unsigned maximum
  723. return TrueCR.umax(FalseCR);
  724. };
  725. }();
  726. return ValueLatticeElement::getRange(
  727. ResultCR, TrueVal.isConstantRangeIncludingUndef() ||
  728. FalseVal.isConstantRangeIncludingUndef());
  729. }
  730. if (SPR.Flavor == SPF_ABS) {
  731. if (LHS == SI->getTrueValue())
  732. return ValueLatticeElement::getRange(
  733. TrueCR.abs(), TrueVal.isConstantRangeIncludingUndef());
  734. if (LHS == SI->getFalseValue())
  735. return ValueLatticeElement::getRange(
  736. FalseCR.abs(), FalseVal.isConstantRangeIncludingUndef());
  737. }
  738. if (SPR.Flavor == SPF_NABS) {
  739. ConstantRange Zero(APInt::getZero(TrueCR.getBitWidth()));
  740. if (LHS == SI->getTrueValue())
  741. return ValueLatticeElement::getRange(
  742. Zero.sub(TrueCR.abs()), FalseVal.isConstantRangeIncludingUndef());
  743. if (LHS == SI->getFalseValue())
  744. return ValueLatticeElement::getRange(
  745. Zero.sub(FalseCR.abs()), FalseVal.isConstantRangeIncludingUndef());
  746. }
  747. }
  748. // Can we constrain the facts about the true and false values by using the
  749. // condition itself? This shows up with idioms like e.g. select(a > 5, a, 5).
  750. // TODO: We could potentially refine an overdefined true value above.
  751. Value *Cond = SI->getCondition();
  752. TrueVal = intersect(TrueVal,
  753. getValueFromCondition(SI->getTrueValue(), Cond, true));
  754. FalseVal = intersect(FalseVal,
  755. getValueFromCondition(SI->getFalseValue(), Cond, false));
  756. ValueLatticeElement Result = TrueVal;
  757. Result.mergeIn(FalseVal);
  758. return Result;
  759. }
  760. std::optional<ConstantRange>
  761. LazyValueInfoImpl::getRangeFor(Value *V, Instruction *CxtI, BasicBlock *BB) {
  762. std::optional<ValueLatticeElement> OptVal = getBlockValue(V, BB, CxtI);
  763. if (!OptVal)
  764. return std::nullopt;
  765. return getConstantRangeOrFull(*OptVal, V->getType(), DL);
  766. }
  767. std::optional<ValueLatticeElement>
  768. LazyValueInfoImpl::solveBlockValueCast(CastInst *CI, BasicBlock *BB) {
  769. // Without knowing how wide the input is, we can't analyze it in any useful
  770. // way.
  771. if (!CI->getOperand(0)->getType()->isSized())
  772. return ValueLatticeElement::getOverdefined();
  773. // Filter out casts we don't know how to reason about before attempting to
  774. // recurse on our operand. This can cut a long search short if we know we're
  775. // not going to be able to get any useful information anways.
  776. switch (CI->getOpcode()) {
  777. case Instruction::Trunc:
  778. case Instruction::SExt:
  779. case Instruction::ZExt:
  780. case Instruction::BitCast:
  781. break;
  782. default:
  783. // Unhandled instructions are overdefined.
  784. LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
  785. << "' - overdefined (unknown cast).\n");
  786. return ValueLatticeElement::getOverdefined();
  787. }
  788. // Figure out the range of the LHS. If that fails, we still apply the
  789. // transfer rule on the full set since we may be able to locally infer
  790. // interesting facts.
  791. std::optional<ConstantRange> LHSRes = getRangeFor(CI->getOperand(0), CI, BB);
  792. if (!LHSRes)
  793. // More work to do before applying this transfer rule.
  794. return std::nullopt;
  795. const ConstantRange &LHSRange = *LHSRes;
  796. const unsigned ResultBitWidth = CI->getType()->getIntegerBitWidth();
  797. // NOTE: We're currently limited by the set of operations that ConstantRange
  798. // can evaluate symbolically. Enhancing that set will allows us to analyze
  799. // more definitions.
  800. return ValueLatticeElement::getRange(LHSRange.castOp(CI->getOpcode(),
  801. ResultBitWidth));
  802. }
  803. std::optional<ValueLatticeElement>
  804. LazyValueInfoImpl::solveBlockValueBinaryOpImpl(
  805. Instruction *I, BasicBlock *BB,
  806. std::function<ConstantRange(const ConstantRange &, const ConstantRange &)>
  807. OpFn) {
  808. // Figure out the ranges of the operands. If that fails, use a
  809. // conservative range, but apply the transfer rule anyways. This
  810. // lets us pick up facts from expressions like "and i32 (call i32
  811. // @foo()), 32"
  812. std::optional<ConstantRange> LHSRes = getRangeFor(I->getOperand(0), I, BB);
  813. std::optional<ConstantRange> RHSRes = getRangeFor(I->getOperand(1), I, BB);
  814. if (!LHSRes || !RHSRes)
  815. // More work to do before applying this transfer rule.
  816. return std::nullopt;
  817. const ConstantRange &LHSRange = *LHSRes;
  818. const ConstantRange &RHSRange = *RHSRes;
  819. return ValueLatticeElement::getRange(OpFn(LHSRange, RHSRange));
  820. }
  821. std::optional<ValueLatticeElement>
  822. LazyValueInfoImpl::solveBlockValueBinaryOp(BinaryOperator *BO, BasicBlock *BB) {
  823. assert(BO->getOperand(0)->getType()->isSized() &&
  824. "all operands to binary operators are sized");
  825. if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(BO)) {
  826. unsigned NoWrapKind = 0;
  827. if (OBO->hasNoUnsignedWrap())
  828. NoWrapKind |= OverflowingBinaryOperator::NoUnsignedWrap;
  829. if (OBO->hasNoSignedWrap())
  830. NoWrapKind |= OverflowingBinaryOperator::NoSignedWrap;
  831. return solveBlockValueBinaryOpImpl(
  832. BO, BB,
  833. [BO, NoWrapKind](const ConstantRange &CR1, const ConstantRange &CR2) {
  834. return CR1.overflowingBinaryOp(BO->getOpcode(), CR2, NoWrapKind);
  835. });
  836. }
  837. return solveBlockValueBinaryOpImpl(
  838. BO, BB, [BO](const ConstantRange &CR1, const ConstantRange &CR2) {
  839. return CR1.binaryOp(BO->getOpcode(), CR2);
  840. });
  841. }
  842. std::optional<ValueLatticeElement>
  843. LazyValueInfoImpl::solveBlockValueOverflowIntrinsic(WithOverflowInst *WO,
  844. BasicBlock *BB) {
  845. return solveBlockValueBinaryOpImpl(
  846. WO, BB, [WO](const ConstantRange &CR1, const ConstantRange &CR2) {
  847. return CR1.binaryOp(WO->getBinaryOp(), CR2);
  848. });
  849. }
  850. std::optional<ValueLatticeElement>
  851. LazyValueInfoImpl::solveBlockValueIntrinsic(IntrinsicInst *II, BasicBlock *BB) {
  852. if (!ConstantRange::isIntrinsicSupported(II->getIntrinsicID())) {
  853. LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
  854. << "' - unknown intrinsic.\n");
  855. return getFromRangeMetadata(II);
  856. }
  857. SmallVector<ConstantRange, 2> OpRanges;
  858. for (Value *Op : II->args()) {
  859. std::optional<ConstantRange> Range = getRangeFor(Op, II, BB);
  860. if (!Range)
  861. return std::nullopt;
  862. OpRanges.push_back(*Range);
  863. }
  864. return ValueLatticeElement::getRange(
  865. ConstantRange::intrinsic(II->getIntrinsicID(), OpRanges));
  866. }
  867. std::optional<ValueLatticeElement>
  868. LazyValueInfoImpl::solveBlockValueExtractValue(ExtractValueInst *EVI,
  869. BasicBlock *BB) {
  870. if (auto *WO = dyn_cast<WithOverflowInst>(EVI->getAggregateOperand()))
  871. if (EVI->getNumIndices() == 1 && *EVI->idx_begin() == 0)
  872. return solveBlockValueOverflowIntrinsic(WO, BB);
  873. // Handle extractvalue of insertvalue to allow further simplification
  874. // based on replaced with.overflow intrinsics.
  875. if (Value *V = simplifyExtractValueInst(
  876. EVI->getAggregateOperand(), EVI->getIndices(),
  877. EVI->getModule()->getDataLayout()))
  878. return getBlockValue(V, BB, EVI);
  879. LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
  880. << "' - overdefined (unknown extractvalue).\n");
  881. return ValueLatticeElement::getOverdefined();
  882. }
  883. static bool matchICmpOperand(APInt &Offset, Value *LHS, Value *Val,
  884. ICmpInst::Predicate Pred) {
  885. if (LHS == Val)
  886. return true;
  887. // Handle range checking idiom produced by InstCombine. We will subtract the
  888. // offset from the allowed range for RHS in this case.
  889. const APInt *C;
  890. if (match(LHS, m_Add(m_Specific(Val), m_APInt(C)))) {
  891. Offset = *C;
  892. return true;
  893. }
  894. // Handle the symmetric case. This appears in saturation patterns like
  895. // (x == 16) ? 16 : (x + 1).
  896. if (match(Val, m_Add(m_Specific(LHS), m_APInt(C)))) {
  897. Offset = -*C;
  898. return true;
  899. }
  900. // If (x | y) < C, then (x < C) && (y < C).
  901. if (match(LHS, m_c_Or(m_Specific(Val), m_Value())) &&
  902. (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE))
  903. return true;
  904. // If (x & y) > C, then (x > C) && (y > C).
  905. if (match(LHS, m_c_And(m_Specific(Val), m_Value())) &&
  906. (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE))
  907. return true;
  908. return false;
  909. }
  910. /// Get value range for a "(Val + Offset) Pred RHS" condition.
  911. static ValueLatticeElement getValueFromSimpleICmpCondition(
  912. CmpInst::Predicate Pred, Value *RHS, const APInt &Offset) {
  913. ConstantRange RHSRange(RHS->getType()->getIntegerBitWidth(),
  914. /*isFullSet=*/true);
  915. if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
  916. RHSRange = ConstantRange(CI->getValue());
  917. else if (Instruction *I = dyn_cast<Instruction>(RHS))
  918. if (auto *Ranges = I->getMetadata(LLVMContext::MD_range))
  919. RHSRange = getConstantRangeFromMetadata(*Ranges);
  920. ConstantRange TrueValues =
  921. ConstantRange::makeAllowedICmpRegion(Pred, RHSRange);
  922. return ValueLatticeElement::getRange(TrueValues.subtract(Offset));
  923. }
  924. static ValueLatticeElement getValueFromICmpCondition(Value *Val, ICmpInst *ICI,
  925. bool isTrueDest) {
  926. Value *LHS = ICI->getOperand(0);
  927. Value *RHS = ICI->getOperand(1);
  928. // Get the predicate that must hold along the considered edge.
  929. CmpInst::Predicate EdgePred =
  930. isTrueDest ? ICI->getPredicate() : ICI->getInversePredicate();
  931. if (isa<Constant>(RHS)) {
  932. if (ICI->isEquality() && LHS == Val) {
  933. if (EdgePred == ICmpInst::ICMP_EQ)
  934. return ValueLatticeElement::get(cast<Constant>(RHS));
  935. else if (!isa<UndefValue>(RHS))
  936. return ValueLatticeElement::getNot(cast<Constant>(RHS));
  937. }
  938. }
  939. Type *Ty = Val->getType();
  940. if (!Ty->isIntegerTy())
  941. return ValueLatticeElement::getOverdefined();
  942. unsigned BitWidth = Ty->getScalarSizeInBits();
  943. APInt Offset(BitWidth, 0);
  944. if (matchICmpOperand(Offset, LHS, Val, EdgePred))
  945. return getValueFromSimpleICmpCondition(EdgePred, RHS, Offset);
  946. CmpInst::Predicate SwappedPred = CmpInst::getSwappedPredicate(EdgePred);
  947. if (matchICmpOperand(Offset, RHS, Val, SwappedPred))
  948. return getValueFromSimpleICmpCondition(SwappedPred, LHS, Offset);
  949. const APInt *Mask, *C;
  950. if (match(LHS, m_And(m_Specific(Val), m_APInt(Mask))) &&
  951. match(RHS, m_APInt(C))) {
  952. // If (Val & Mask) == C then all the masked bits are known and we can
  953. // compute a value range based on that.
  954. if (EdgePred == ICmpInst::ICMP_EQ) {
  955. KnownBits Known;
  956. Known.Zero = ~*C & *Mask;
  957. Known.One = *C & *Mask;
  958. return ValueLatticeElement::getRange(
  959. ConstantRange::fromKnownBits(Known, /*IsSigned*/ false));
  960. }
  961. // If (Val & Mask) != 0 then the value must be larger than the lowest set
  962. // bit of Mask.
  963. if (EdgePred == ICmpInst::ICMP_NE && !Mask->isZero() && C->isZero()) {
  964. return ValueLatticeElement::getRange(ConstantRange::getNonEmpty(
  965. APInt::getOneBitSet(BitWidth, Mask->countTrailingZeros()),
  966. APInt::getZero(BitWidth)));
  967. }
  968. }
  969. // If (X urem Modulus) >= C, then X >= C.
  970. // If trunc X >= C, then X >= C.
  971. // TODO: An upper bound could be computed as well.
  972. if (match(LHS, m_CombineOr(m_URem(m_Specific(Val), m_Value()),
  973. m_Trunc(m_Specific(Val)))) &&
  974. match(RHS, m_APInt(C))) {
  975. // Use the icmp region so we don't have to deal with different predicates.
  976. ConstantRange CR = ConstantRange::makeExactICmpRegion(EdgePred, *C);
  977. if (!CR.isEmptySet())
  978. return ValueLatticeElement::getRange(ConstantRange::getNonEmpty(
  979. CR.getUnsignedMin().zext(BitWidth), APInt(BitWidth, 0)));
  980. }
  981. return ValueLatticeElement::getOverdefined();
  982. }
  983. // Handle conditions of the form
  984. // extractvalue(op.with.overflow(%x, C), 1).
  985. static ValueLatticeElement getValueFromOverflowCondition(
  986. Value *Val, WithOverflowInst *WO, bool IsTrueDest) {
  987. // TODO: This only works with a constant RHS for now. We could also compute
  988. // the range of the RHS, but this doesn't fit into the current structure of
  989. // the edge value calculation.
  990. const APInt *C;
  991. if (WO->getLHS() != Val || !match(WO->getRHS(), m_APInt(C)))
  992. return ValueLatticeElement::getOverdefined();
  993. // Calculate the possible values of %x for which no overflow occurs.
  994. ConstantRange NWR = ConstantRange::makeExactNoWrapRegion(
  995. WO->getBinaryOp(), *C, WO->getNoWrapKind());
  996. // If overflow is false, %x is constrained to NWR. If overflow is true, %x is
  997. // constrained to it's inverse (all values that might cause overflow).
  998. if (IsTrueDest)
  999. NWR = NWR.inverse();
  1000. return ValueLatticeElement::getRange(NWR);
  1001. }
  1002. // Tracks a Value * condition and whether we're interested in it or its inverse
  1003. typedef PointerIntPair<Value *, 1, bool> CondValue;
  1004. static std::optional<ValueLatticeElement> getValueFromConditionImpl(
  1005. Value *Val, CondValue CondVal, bool isRevisit,
  1006. SmallDenseMap<CondValue, ValueLatticeElement> &Visited,
  1007. SmallVectorImpl<CondValue> &Worklist) {
  1008. Value *Cond = CondVal.getPointer();
  1009. bool isTrueDest = CondVal.getInt();
  1010. if (!isRevisit) {
  1011. if (ICmpInst *ICI = dyn_cast<ICmpInst>(Cond))
  1012. return getValueFromICmpCondition(Val, ICI, isTrueDest);
  1013. if (auto *EVI = dyn_cast<ExtractValueInst>(Cond))
  1014. if (auto *WO = dyn_cast<WithOverflowInst>(EVI->getAggregateOperand()))
  1015. if (EVI->getNumIndices() == 1 && *EVI->idx_begin() == 1)
  1016. return getValueFromOverflowCondition(Val, WO, isTrueDest);
  1017. }
  1018. Value *N;
  1019. if (match(Cond, m_Not(m_Value(N)))) {
  1020. CondValue NKey(N, !isTrueDest);
  1021. auto NV = Visited.find(NKey);
  1022. if (NV == Visited.end()) {
  1023. Worklist.push_back(NKey);
  1024. return std::nullopt;
  1025. }
  1026. return NV->second;
  1027. }
  1028. Value *L, *R;
  1029. bool IsAnd;
  1030. if (match(Cond, m_LogicalAnd(m_Value(L), m_Value(R))))
  1031. IsAnd = true;
  1032. else if (match(Cond, m_LogicalOr(m_Value(L), m_Value(R))))
  1033. IsAnd = false;
  1034. else
  1035. return ValueLatticeElement::getOverdefined();
  1036. auto LV = Visited.find(CondValue(L, isTrueDest));
  1037. auto RV = Visited.find(CondValue(R, isTrueDest));
  1038. // if (L && R) -> intersect L and R
  1039. // if (!(L || R)) -> intersect !L and !R
  1040. // if (L || R) -> union L and R
  1041. // if (!(L && R)) -> union !L and !R
  1042. if ((isTrueDest ^ IsAnd) && (LV != Visited.end())) {
  1043. ValueLatticeElement V = LV->second;
  1044. if (V.isOverdefined())
  1045. return V;
  1046. if (RV != Visited.end()) {
  1047. V.mergeIn(RV->second);
  1048. return V;
  1049. }
  1050. }
  1051. if (LV == Visited.end() || RV == Visited.end()) {
  1052. assert(!isRevisit);
  1053. if (LV == Visited.end())
  1054. Worklist.push_back(CondValue(L, isTrueDest));
  1055. if (RV == Visited.end())
  1056. Worklist.push_back(CondValue(R, isTrueDest));
  1057. return std::nullopt;
  1058. }
  1059. return intersect(LV->second, RV->second);
  1060. }
  1061. ValueLatticeElement getValueFromCondition(Value *Val, Value *Cond,
  1062. bool isTrueDest) {
  1063. assert(Cond && "precondition");
  1064. SmallDenseMap<CondValue, ValueLatticeElement> Visited;
  1065. SmallVector<CondValue> Worklist;
  1066. CondValue CondKey(Cond, isTrueDest);
  1067. Worklist.push_back(CondKey);
  1068. do {
  1069. CondValue CurrentCond = Worklist.back();
  1070. // Insert an Overdefined placeholder into the set to prevent
  1071. // infinite recursion if there exists IRs that use not
  1072. // dominated by its def as in this example:
  1073. // "%tmp3 = or i1 undef, %tmp4"
  1074. // "%tmp4 = or i1 undef, %tmp3"
  1075. auto Iter =
  1076. Visited.try_emplace(CurrentCond, ValueLatticeElement::getOverdefined());
  1077. bool isRevisit = !Iter.second;
  1078. std::optional<ValueLatticeElement> Result = getValueFromConditionImpl(
  1079. Val, CurrentCond, isRevisit, Visited, Worklist);
  1080. if (Result) {
  1081. Visited[CurrentCond] = *Result;
  1082. Worklist.pop_back();
  1083. }
  1084. } while (!Worklist.empty());
  1085. auto Result = Visited.find(CondKey);
  1086. assert(Result != Visited.end());
  1087. return Result->second;
  1088. }
  1089. // Return true if Usr has Op as an operand, otherwise false.
  1090. static bool usesOperand(User *Usr, Value *Op) {
  1091. return is_contained(Usr->operands(), Op);
  1092. }
  1093. // Return true if the instruction type of Val is supported by
  1094. // constantFoldUser(). Currently CastInst, BinaryOperator and FreezeInst only.
  1095. // Call this before calling constantFoldUser() to find out if it's even worth
  1096. // attempting to call it.
  1097. static bool isOperationFoldable(User *Usr) {
  1098. return isa<CastInst>(Usr) || isa<BinaryOperator>(Usr) || isa<FreezeInst>(Usr);
  1099. }
  1100. // Check if Usr can be simplified to an integer constant when the value of one
  1101. // of its operands Op is an integer constant OpConstVal. If so, return it as an
  1102. // lattice value range with a single element or otherwise return an overdefined
  1103. // lattice value.
  1104. static ValueLatticeElement constantFoldUser(User *Usr, Value *Op,
  1105. const APInt &OpConstVal,
  1106. const DataLayout &DL) {
  1107. assert(isOperationFoldable(Usr) && "Precondition");
  1108. Constant* OpConst = Constant::getIntegerValue(Op->getType(), OpConstVal);
  1109. // Check if Usr can be simplified to a constant.
  1110. if (auto *CI = dyn_cast<CastInst>(Usr)) {
  1111. assert(CI->getOperand(0) == Op && "Operand 0 isn't Op");
  1112. if (auto *C = dyn_cast_or_null<ConstantInt>(
  1113. simplifyCastInst(CI->getOpcode(), OpConst,
  1114. CI->getDestTy(), DL))) {
  1115. return ValueLatticeElement::getRange(ConstantRange(C->getValue()));
  1116. }
  1117. } else if (auto *BO = dyn_cast<BinaryOperator>(Usr)) {
  1118. bool Op0Match = BO->getOperand(0) == Op;
  1119. bool Op1Match = BO->getOperand(1) == Op;
  1120. assert((Op0Match || Op1Match) &&
  1121. "Operand 0 nor Operand 1 isn't a match");
  1122. Value *LHS = Op0Match ? OpConst : BO->getOperand(0);
  1123. Value *RHS = Op1Match ? OpConst : BO->getOperand(1);
  1124. if (auto *C = dyn_cast_or_null<ConstantInt>(
  1125. simplifyBinOp(BO->getOpcode(), LHS, RHS, DL))) {
  1126. return ValueLatticeElement::getRange(ConstantRange(C->getValue()));
  1127. }
  1128. } else if (isa<FreezeInst>(Usr)) {
  1129. assert(cast<FreezeInst>(Usr)->getOperand(0) == Op && "Operand 0 isn't Op");
  1130. return ValueLatticeElement::getRange(ConstantRange(OpConstVal));
  1131. }
  1132. return ValueLatticeElement::getOverdefined();
  1133. }
  1134. /// Compute the value of Val on the edge BBFrom -> BBTo. Returns false if
  1135. /// Val is not constrained on the edge. Result is unspecified if return value
  1136. /// is false.
  1137. static std::optional<ValueLatticeElement> getEdgeValueLocal(Value *Val,
  1138. BasicBlock *BBFrom,
  1139. BasicBlock *BBTo) {
  1140. // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
  1141. // know that v != 0.
  1142. if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
  1143. // If this is a conditional branch and only one successor goes to BBTo, then
  1144. // we may be able to infer something from the condition.
  1145. if (BI->isConditional() &&
  1146. BI->getSuccessor(0) != BI->getSuccessor(1)) {
  1147. bool isTrueDest = BI->getSuccessor(0) == BBTo;
  1148. assert(BI->getSuccessor(!isTrueDest) == BBTo &&
  1149. "BBTo isn't a successor of BBFrom");
  1150. Value *Condition = BI->getCondition();
  1151. // If V is the condition of the branch itself, then we know exactly what
  1152. // it is.
  1153. if (Condition == Val)
  1154. return ValueLatticeElement::get(ConstantInt::get(
  1155. Type::getInt1Ty(Val->getContext()), isTrueDest));
  1156. // If the condition of the branch is an equality comparison, we may be
  1157. // able to infer the value.
  1158. ValueLatticeElement Result = getValueFromCondition(Val, Condition,
  1159. isTrueDest);
  1160. if (!Result.isOverdefined())
  1161. return Result;
  1162. if (User *Usr = dyn_cast<User>(Val)) {
  1163. assert(Result.isOverdefined() && "Result isn't overdefined");
  1164. // Check with isOperationFoldable() first to avoid linearly iterating
  1165. // over the operands unnecessarily which can be expensive for
  1166. // instructions with many operands.
  1167. if (isa<IntegerType>(Usr->getType()) && isOperationFoldable(Usr)) {
  1168. const DataLayout &DL = BBTo->getModule()->getDataLayout();
  1169. if (usesOperand(Usr, Condition)) {
  1170. // If Val has Condition as an operand and Val can be folded into a
  1171. // constant with either Condition == true or Condition == false,
  1172. // propagate the constant.
  1173. // eg.
  1174. // ; %Val is true on the edge to %then.
  1175. // %Val = and i1 %Condition, true.
  1176. // br %Condition, label %then, label %else
  1177. APInt ConditionVal(1, isTrueDest ? 1 : 0);
  1178. Result = constantFoldUser(Usr, Condition, ConditionVal, DL);
  1179. } else {
  1180. // If one of Val's operand has an inferred value, we may be able to
  1181. // infer the value of Val.
  1182. // eg.
  1183. // ; %Val is 94 on the edge to %then.
  1184. // %Val = add i8 %Op, 1
  1185. // %Condition = icmp eq i8 %Op, 93
  1186. // br i1 %Condition, label %then, label %else
  1187. for (unsigned i = 0; i < Usr->getNumOperands(); ++i) {
  1188. Value *Op = Usr->getOperand(i);
  1189. ValueLatticeElement OpLatticeVal =
  1190. getValueFromCondition(Op, Condition, isTrueDest);
  1191. if (std::optional<APInt> OpConst =
  1192. OpLatticeVal.asConstantInteger()) {
  1193. Result = constantFoldUser(Usr, Op, *OpConst, DL);
  1194. break;
  1195. }
  1196. }
  1197. }
  1198. }
  1199. }
  1200. if (!Result.isOverdefined())
  1201. return Result;
  1202. }
  1203. }
  1204. // If the edge was formed by a switch on the value, then we may know exactly
  1205. // what it is.
  1206. if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
  1207. Value *Condition = SI->getCondition();
  1208. if (!isa<IntegerType>(Val->getType()))
  1209. return std::nullopt;
  1210. bool ValUsesConditionAndMayBeFoldable = false;
  1211. if (Condition != Val) {
  1212. // Check if Val has Condition as an operand.
  1213. if (User *Usr = dyn_cast<User>(Val))
  1214. ValUsesConditionAndMayBeFoldable = isOperationFoldable(Usr) &&
  1215. usesOperand(Usr, Condition);
  1216. if (!ValUsesConditionAndMayBeFoldable)
  1217. return std::nullopt;
  1218. }
  1219. assert((Condition == Val || ValUsesConditionAndMayBeFoldable) &&
  1220. "Condition != Val nor Val doesn't use Condition");
  1221. bool DefaultCase = SI->getDefaultDest() == BBTo;
  1222. unsigned BitWidth = Val->getType()->getIntegerBitWidth();
  1223. ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/);
  1224. for (auto Case : SI->cases()) {
  1225. APInt CaseValue = Case.getCaseValue()->getValue();
  1226. ConstantRange EdgeVal(CaseValue);
  1227. if (ValUsesConditionAndMayBeFoldable) {
  1228. User *Usr = cast<User>(Val);
  1229. const DataLayout &DL = BBTo->getModule()->getDataLayout();
  1230. ValueLatticeElement EdgeLatticeVal =
  1231. constantFoldUser(Usr, Condition, CaseValue, DL);
  1232. if (EdgeLatticeVal.isOverdefined())
  1233. return std::nullopt;
  1234. EdgeVal = EdgeLatticeVal.getConstantRange();
  1235. }
  1236. if (DefaultCase) {
  1237. // It is possible that the default destination is the destination of
  1238. // some cases. We cannot perform difference for those cases.
  1239. // We know Condition != CaseValue in BBTo. In some cases we can use
  1240. // this to infer Val == f(Condition) is != f(CaseValue). For now, we
  1241. // only do this when f is identity (i.e. Val == Condition), but we
  1242. // should be able to do this for any injective f.
  1243. if (Case.getCaseSuccessor() != BBTo && Condition == Val)
  1244. EdgesVals = EdgesVals.difference(EdgeVal);
  1245. } else if (Case.getCaseSuccessor() == BBTo)
  1246. EdgesVals = EdgesVals.unionWith(EdgeVal);
  1247. }
  1248. return ValueLatticeElement::getRange(std::move(EdgesVals));
  1249. }
  1250. return std::nullopt;
  1251. }
  1252. /// Compute the value of Val on the edge BBFrom -> BBTo or the value at
  1253. /// the basic block if the edge does not constrain Val.
  1254. std::optional<ValueLatticeElement>
  1255. LazyValueInfoImpl::getEdgeValue(Value *Val, BasicBlock *BBFrom,
  1256. BasicBlock *BBTo, Instruction *CxtI) {
  1257. // If already a constant, there is nothing to compute.
  1258. if (Constant *VC = dyn_cast<Constant>(Val))
  1259. return ValueLatticeElement::get(VC);
  1260. ValueLatticeElement LocalResult =
  1261. getEdgeValueLocal(Val, BBFrom, BBTo)
  1262. .value_or(ValueLatticeElement::getOverdefined());
  1263. if (hasSingleValue(LocalResult))
  1264. // Can't get any more precise here
  1265. return LocalResult;
  1266. std::optional<ValueLatticeElement> OptInBlock =
  1267. getBlockValue(Val, BBFrom, BBFrom->getTerminator());
  1268. if (!OptInBlock)
  1269. return std::nullopt;
  1270. ValueLatticeElement &InBlock = *OptInBlock;
  1271. // We can use the context instruction (generically the ultimate instruction
  1272. // the calling pass is trying to simplify) here, even though the result of
  1273. // this function is generally cached when called from the solve* functions
  1274. // (and that cached result might be used with queries using a different
  1275. // context instruction), because when this function is called from the solve*
  1276. // functions, the context instruction is not provided. When called from
  1277. // LazyValueInfoImpl::getValueOnEdge, the context instruction is provided,
  1278. // but then the result is not cached.
  1279. intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock, CxtI);
  1280. return intersect(LocalResult, InBlock);
  1281. }
  1282. ValueLatticeElement LazyValueInfoImpl::getValueInBlock(Value *V, BasicBlock *BB,
  1283. Instruction *CxtI) {
  1284. LLVM_DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
  1285. << BB->getName() << "'\n");
  1286. assert(BlockValueStack.empty() && BlockValueSet.empty());
  1287. std::optional<ValueLatticeElement> OptResult = getBlockValue(V, BB, CxtI);
  1288. if (!OptResult) {
  1289. solve();
  1290. OptResult = getBlockValue(V, BB, CxtI);
  1291. assert(OptResult && "Value not available after solving");
  1292. }
  1293. ValueLatticeElement Result = *OptResult;
  1294. LLVM_DEBUG(dbgs() << " Result = " << Result << "\n");
  1295. return Result;
  1296. }
  1297. ValueLatticeElement LazyValueInfoImpl::getValueAt(Value *V, Instruction *CxtI) {
  1298. LLVM_DEBUG(dbgs() << "LVI Getting value " << *V << " at '" << CxtI->getName()
  1299. << "'\n");
  1300. if (auto *C = dyn_cast<Constant>(V))
  1301. return ValueLatticeElement::get(C);
  1302. ValueLatticeElement Result = ValueLatticeElement::getOverdefined();
  1303. if (auto *I = dyn_cast<Instruction>(V))
  1304. Result = getFromRangeMetadata(I);
  1305. intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
  1306. LLVM_DEBUG(dbgs() << " Result = " << Result << "\n");
  1307. return Result;
  1308. }
  1309. ValueLatticeElement LazyValueInfoImpl::
  1310. getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
  1311. Instruction *CxtI) {
  1312. LLVM_DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
  1313. << FromBB->getName() << "' to '" << ToBB->getName()
  1314. << "'\n");
  1315. std::optional<ValueLatticeElement> Result =
  1316. getEdgeValue(V, FromBB, ToBB, CxtI);
  1317. if (!Result) {
  1318. solve();
  1319. Result = getEdgeValue(V, FromBB, ToBB, CxtI);
  1320. assert(Result && "More work to do after problem solved?");
  1321. }
  1322. LLVM_DEBUG(dbgs() << " Result = " << *Result << "\n");
  1323. return *Result;
  1324. }
  1325. void LazyValueInfoImpl::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
  1326. BasicBlock *NewSucc) {
  1327. TheCache.threadEdgeImpl(OldSucc, NewSucc);
  1328. }
  1329. //===----------------------------------------------------------------------===//
  1330. // LazyValueInfo Impl
  1331. //===----------------------------------------------------------------------===//
  1332. /// This lazily constructs the LazyValueInfoImpl.
  1333. static LazyValueInfoImpl &getImpl(void *&PImpl, AssumptionCache *AC,
  1334. const Module *M) {
  1335. if (!PImpl) {
  1336. assert(M && "getCache() called with a null Module");
  1337. const DataLayout &DL = M->getDataLayout();
  1338. Function *GuardDecl = M->getFunction(
  1339. Intrinsic::getName(Intrinsic::experimental_guard));
  1340. PImpl = new LazyValueInfoImpl(AC, DL, GuardDecl);
  1341. }
  1342. return *static_cast<LazyValueInfoImpl*>(PImpl);
  1343. }
  1344. bool LazyValueInfoWrapperPass::runOnFunction(Function &F) {
  1345. Info.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
  1346. Info.TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
  1347. if (Info.PImpl)
  1348. getImpl(Info.PImpl, Info.AC, F.getParent()).clear();
  1349. // Fully lazy.
  1350. return false;
  1351. }
  1352. void LazyValueInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  1353. AU.setPreservesAll();
  1354. AU.addRequired<AssumptionCacheTracker>();
  1355. AU.addRequired<TargetLibraryInfoWrapperPass>();
  1356. }
  1357. LazyValueInfo &LazyValueInfoWrapperPass::getLVI() { return Info; }
  1358. LazyValueInfo::~LazyValueInfo() { releaseMemory(); }
  1359. void LazyValueInfo::releaseMemory() {
  1360. // If the cache was allocated, free it.
  1361. if (PImpl) {
  1362. delete &getImpl(PImpl, AC, nullptr);
  1363. PImpl = nullptr;
  1364. }
  1365. }
  1366. bool LazyValueInfo::invalidate(Function &F, const PreservedAnalyses &PA,
  1367. FunctionAnalysisManager::Invalidator &Inv) {
  1368. // We need to invalidate if we have either failed to preserve this analyses
  1369. // result directly or if any of its dependencies have been invalidated.
  1370. auto PAC = PA.getChecker<LazyValueAnalysis>();
  1371. if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()))
  1372. return true;
  1373. return false;
  1374. }
  1375. void LazyValueInfoWrapperPass::releaseMemory() { Info.releaseMemory(); }
  1376. LazyValueInfo LazyValueAnalysis::run(Function &F,
  1377. FunctionAnalysisManager &FAM) {
  1378. auto &AC = FAM.getResult<AssumptionAnalysis>(F);
  1379. auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
  1380. return LazyValueInfo(&AC, &F.getParent()->getDataLayout(), &TLI);
  1381. }
  1382. /// Returns true if we can statically tell that this value will never be a
  1383. /// "useful" constant. In practice, this means we've got something like an
  1384. /// alloca or a malloc call for which a comparison against a constant can
  1385. /// only be guarding dead code. Note that we are potentially giving up some
  1386. /// precision in dead code (a constant result) in favour of avoiding a
  1387. /// expensive search for a easily answered common query.
  1388. static bool isKnownNonConstant(Value *V) {
  1389. V = V->stripPointerCasts();
  1390. // The return val of alloc cannot be a Constant.
  1391. if (isa<AllocaInst>(V))
  1392. return true;
  1393. return false;
  1394. }
  1395. Constant *LazyValueInfo::getConstant(Value *V, Instruction *CxtI) {
  1396. // Bail out early if V is known not to be a Constant.
  1397. if (isKnownNonConstant(V))
  1398. return nullptr;
  1399. BasicBlock *BB = CxtI->getParent();
  1400. ValueLatticeElement Result =
  1401. getImpl(PImpl, AC, BB->getModule()).getValueInBlock(V, BB, CxtI);
  1402. if (Result.isConstant())
  1403. return Result.getConstant();
  1404. if (Result.isConstantRange()) {
  1405. const ConstantRange &CR = Result.getConstantRange();
  1406. if (const APInt *SingleVal = CR.getSingleElement())
  1407. return ConstantInt::get(V->getContext(), *SingleVal);
  1408. }
  1409. return nullptr;
  1410. }
  1411. ConstantRange LazyValueInfo::getConstantRange(Value *V, Instruction *CxtI,
  1412. bool UndefAllowed) {
  1413. assert(V->getType()->isIntegerTy());
  1414. unsigned Width = V->getType()->getIntegerBitWidth();
  1415. BasicBlock *BB = CxtI->getParent();
  1416. ValueLatticeElement Result =
  1417. getImpl(PImpl, AC, BB->getModule()).getValueInBlock(V, BB, CxtI);
  1418. if (Result.isUnknown())
  1419. return ConstantRange::getEmpty(Width);
  1420. if (Result.isConstantRange(UndefAllowed))
  1421. return Result.getConstantRange(UndefAllowed);
  1422. // We represent ConstantInt constants as constant ranges but other kinds
  1423. // of integer constants, i.e. ConstantExpr will be tagged as constants
  1424. assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
  1425. "ConstantInt value must be represented as constantrange");
  1426. return ConstantRange::getFull(Width);
  1427. }
  1428. ConstantRange LazyValueInfo::getConstantRangeAtUse(const Use &U,
  1429. bool UndefAllowed) {
  1430. Value *V = U.get();
  1431. ConstantRange CR =
  1432. getConstantRange(V, cast<Instruction>(U.getUser()), UndefAllowed);
  1433. // Check whether the only (possibly transitive) use of the value is in a
  1434. // position where V can be constrained by a select or branch condition.
  1435. const Use *CurrU = &U;
  1436. // TODO: Increase limit?
  1437. const unsigned MaxUsesToInspect = 3;
  1438. for (unsigned I = 0; I < MaxUsesToInspect; ++I) {
  1439. std::optional<ValueLatticeElement> CondVal;
  1440. auto *CurrI = cast<Instruction>(CurrU->getUser());
  1441. if (auto *SI = dyn_cast<SelectInst>(CurrI)) {
  1442. if (CurrU->getOperandNo() == 1)
  1443. CondVal = getValueFromCondition(V, SI->getCondition(), true);
  1444. else if (CurrU->getOperandNo() == 2)
  1445. CondVal = getValueFromCondition(V, SI->getCondition(), false);
  1446. } else if (auto *PHI = dyn_cast<PHINode>(CurrI)) {
  1447. // TODO: Use non-local query?
  1448. CondVal =
  1449. getEdgeValueLocal(V, PHI->getIncomingBlock(*CurrU), PHI->getParent());
  1450. }
  1451. if (CondVal && CondVal->isConstantRange())
  1452. CR = CR.intersectWith(CondVal->getConstantRange());
  1453. // Only follow one-use chain, to allow direct intersection of conditions.
  1454. // If there are multiple uses, we would have to intersect with the union of
  1455. // all conditions at different uses.
  1456. // Stop walking if we hit a non-speculatable instruction. Even if the
  1457. // result is only used under a specific condition, executing the
  1458. // instruction itself may cause side effects or UB already.
  1459. // This also disallows looking through phi nodes: If the phi node is part
  1460. // of a cycle, we might end up reasoning about values from different cycle
  1461. // iterations (PR60629).
  1462. if (!CurrI->hasOneUse() || !isSafeToSpeculativelyExecute(CurrI))
  1463. break;
  1464. CurrU = &*CurrI->use_begin();
  1465. }
  1466. return CR;
  1467. }
  1468. /// Determine whether the specified value is known to be a
  1469. /// constant on the specified edge. Return null if not.
  1470. Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
  1471. BasicBlock *ToBB,
  1472. Instruction *CxtI) {
  1473. Module *M = FromBB->getModule();
  1474. ValueLatticeElement Result =
  1475. getImpl(PImpl, AC, M).getValueOnEdge(V, FromBB, ToBB, CxtI);
  1476. if (Result.isConstant())
  1477. return Result.getConstant();
  1478. if (Result.isConstantRange()) {
  1479. const ConstantRange &CR = Result.getConstantRange();
  1480. if (const APInt *SingleVal = CR.getSingleElement())
  1481. return ConstantInt::get(V->getContext(), *SingleVal);
  1482. }
  1483. return nullptr;
  1484. }
  1485. ConstantRange LazyValueInfo::getConstantRangeOnEdge(Value *V,
  1486. BasicBlock *FromBB,
  1487. BasicBlock *ToBB,
  1488. Instruction *CxtI) {
  1489. unsigned Width = V->getType()->getIntegerBitWidth();
  1490. Module *M = FromBB->getModule();
  1491. ValueLatticeElement Result =
  1492. getImpl(PImpl, AC, M).getValueOnEdge(V, FromBB, ToBB, CxtI);
  1493. if (Result.isUnknown())
  1494. return ConstantRange::getEmpty(Width);
  1495. if (Result.isConstantRange())
  1496. return Result.getConstantRange();
  1497. // We represent ConstantInt constants as constant ranges but other kinds
  1498. // of integer constants, i.e. ConstantExpr will be tagged as constants
  1499. assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
  1500. "ConstantInt value must be represented as constantrange");
  1501. return ConstantRange::getFull(Width);
  1502. }
  1503. static LazyValueInfo::Tristate
  1504. getPredicateResult(unsigned Pred, Constant *C, const ValueLatticeElement &Val,
  1505. const DataLayout &DL, TargetLibraryInfo *TLI) {
  1506. // If we know the value is a constant, evaluate the conditional.
  1507. Constant *Res = nullptr;
  1508. if (Val.isConstant()) {
  1509. Res = ConstantFoldCompareInstOperands(Pred, Val.getConstant(), C, DL, TLI);
  1510. if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
  1511. return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True;
  1512. return LazyValueInfo::Unknown;
  1513. }
  1514. if (Val.isConstantRange()) {
  1515. ConstantInt *CI = dyn_cast<ConstantInt>(C);
  1516. if (!CI) return LazyValueInfo::Unknown;
  1517. const ConstantRange &CR = Val.getConstantRange();
  1518. if (Pred == ICmpInst::ICMP_EQ) {
  1519. if (!CR.contains(CI->getValue()))
  1520. return LazyValueInfo::False;
  1521. if (CR.isSingleElement())
  1522. return LazyValueInfo::True;
  1523. } else if (Pred == ICmpInst::ICMP_NE) {
  1524. if (!CR.contains(CI->getValue()))
  1525. return LazyValueInfo::True;
  1526. if (CR.isSingleElement())
  1527. return LazyValueInfo::False;
  1528. } else {
  1529. // Handle more complex predicates.
  1530. ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(
  1531. (ICmpInst::Predicate)Pred, CI->getValue());
  1532. if (TrueValues.contains(CR))
  1533. return LazyValueInfo::True;
  1534. if (TrueValues.inverse().contains(CR))
  1535. return LazyValueInfo::False;
  1536. }
  1537. return LazyValueInfo::Unknown;
  1538. }
  1539. if (Val.isNotConstant()) {
  1540. // If this is an equality comparison, we can try to fold it knowing that
  1541. // "V != C1".
  1542. if (Pred == ICmpInst::ICMP_EQ) {
  1543. // !C1 == C -> false iff C1 == C.
  1544. Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
  1545. Val.getNotConstant(), C, DL,
  1546. TLI);
  1547. if (Res->isNullValue())
  1548. return LazyValueInfo::False;
  1549. } else if (Pred == ICmpInst::ICMP_NE) {
  1550. // !C1 != C -> true iff C1 == C.
  1551. Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
  1552. Val.getNotConstant(), C, DL,
  1553. TLI);
  1554. if (Res->isNullValue())
  1555. return LazyValueInfo::True;
  1556. }
  1557. return LazyValueInfo::Unknown;
  1558. }
  1559. return LazyValueInfo::Unknown;
  1560. }
  1561. /// Determine whether the specified value comparison with a constant is known to
  1562. /// be true or false on the specified CFG edge. Pred is a CmpInst predicate.
  1563. LazyValueInfo::Tristate
  1564. LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
  1565. BasicBlock *FromBB, BasicBlock *ToBB,
  1566. Instruction *CxtI) {
  1567. Module *M = FromBB->getModule();
  1568. ValueLatticeElement Result =
  1569. getImpl(PImpl, AC, M).getValueOnEdge(V, FromBB, ToBB, CxtI);
  1570. return getPredicateResult(Pred, C, Result, M->getDataLayout(), TLI);
  1571. }
  1572. LazyValueInfo::Tristate
  1573. LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C,
  1574. Instruction *CxtI, bool UseBlockValue) {
  1575. // Is or is not NonNull are common predicates being queried. If
  1576. // isKnownNonZero can tell us the result of the predicate, we can
  1577. // return it quickly. But this is only a fastpath, and falling
  1578. // through would still be correct.
  1579. Module *M = CxtI->getModule();
  1580. const DataLayout &DL = M->getDataLayout();
  1581. if (V->getType()->isPointerTy() && C->isNullValue() &&
  1582. isKnownNonZero(V->stripPointerCastsSameRepresentation(), DL)) {
  1583. if (Pred == ICmpInst::ICMP_EQ)
  1584. return LazyValueInfo::False;
  1585. else if (Pred == ICmpInst::ICMP_NE)
  1586. return LazyValueInfo::True;
  1587. }
  1588. ValueLatticeElement Result = UseBlockValue
  1589. ? getImpl(PImpl, AC, M).getValueInBlock(V, CxtI->getParent(), CxtI)
  1590. : getImpl(PImpl, AC, M).getValueAt(V, CxtI);
  1591. Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI);
  1592. if (Ret != Unknown)
  1593. return Ret;
  1594. // Note: The following bit of code is somewhat distinct from the rest of LVI;
  1595. // LVI as a whole tries to compute a lattice value which is conservatively
  1596. // correct at a given location. In this case, we have a predicate which we
  1597. // weren't able to prove about the merged result, and we're pushing that
  1598. // predicate back along each incoming edge to see if we can prove it
  1599. // separately for each input. As a motivating example, consider:
  1600. // bb1:
  1601. // %v1 = ... ; constantrange<1, 5>
  1602. // br label %merge
  1603. // bb2:
  1604. // %v2 = ... ; constantrange<10, 20>
  1605. // br label %merge
  1606. // merge:
  1607. // %phi = phi [%v1, %v2] ; constantrange<1,20>
  1608. // %pred = icmp eq i32 %phi, 8
  1609. // We can't tell from the lattice value for '%phi' that '%pred' is false
  1610. // along each path, but by checking the predicate over each input separately,
  1611. // we can.
  1612. // We limit the search to one step backwards from the current BB and value.
  1613. // We could consider extending this to search further backwards through the
  1614. // CFG and/or value graph, but there are non-obvious compile time vs quality
  1615. // tradeoffs.
  1616. BasicBlock *BB = CxtI->getParent();
  1617. // Function entry or an unreachable block. Bail to avoid confusing
  1618. // analysis below.
  1619. pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
  1620. if (PI == PE)
  1621. return Unknown;
  1622. // If V is a PHI node in the same block as the context, we need to ask
  1623. // questions about the predicate as applied to the incoming value along
  1624. // each edge. This is useful for eliminating cases where the predicate is
  1625. // known along all incoming edges.
  1626. if (auto *PHI = dyn_cast<PHINode>(V))
  1627. if (PHI->getParent() == BB) {
  1628. Tristate Baseline = Unknown;
  1629. for (unsigned i = 0, e = PHI->getNumIncomingValues(); i < e; i++) {
  1630. Value *Incoming = PHI->getIncomingValue(i);
  1631. BasicBlock *PredBB = PHI->getIncomingBlock(i);
  1632. // Note that PredBB may be BB itself.
  1633. Tristate Result =
  1634. getPredicateOnEdge(Pred, Incoming, C, PredBB, BB, CxtI);
  1635. // Keep going as long as we've seen a consistent known result for
  1636. // all inputs.
  1637. Baseline = (i == 0) ? Result /* First iteration */
  1638. : (Baseline == Result ? Baseline
  1639. : Unknown); /* All others */
  1640. if (Baseline == Unknown)
  1641. break;
  1642. }
  1643. if (Baseline != Unknown)
  1644. return Baseline;
  1645. }
  1646. // For a comparison where the V is outside this block, it's possible
  1647. // that we've branched on it before. Look to see if the value is known
  1648. // on all incoming edges.
  1649. if (!isa<Instruction>(V) || cast<Instruction>(V)->getParent() != BB) {
  1650. // For predecessor edge, determine if the comparison is true or false
  1651. // on that edge. If they're all true or all false, we can conclude
  1652. // the value of the comparison in this block.
  1653. Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
  1654. if (Baseline != Unknown) {
  1655. // Check that all remaining incoming values match the first one.
  1656. while (++PI != PE) {
  1657. Tristate Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
  1658. if (Ret != Baseline)
  1659. break;
  1660. }
  1661. // If we terminated early, then one of the values didn't match.
  1662. if (PI == PE) {
  1663. return Baseline;
  1664. }
  1665. }
  1666. }
  1667. return Unknown;
  1668. }
  1669. LazyValueInfo::Tristate LazyValueInfo::getPredicateAt(unsigned P, Value *LHS,
  1670. Value *RHS,
  1671. Instruction *CxtI,
  1672. bool UseBlockValue) {
  1673. CmpInst::Predicate Pred = (CmpInst::Predicate)P;
  1674. if (auto *C = dyn_cast<Constant>(RHS))
  1675. return getPredicateAt(P, LHS, C, CxtI, UseBlockValue);
  1676. if (auto *C = dyn_cast<Constant>(LHS))
  1677. return getPredicateAt(CmpInst::getSwappedPredicate(Pred), RHS, C, CxtI,
  1678. UseBlockValue);
  1679. // Got two non-Constant values. Try to determine the comparison results based
  1680. // on the block values of the two operands, e.g. because they have
  1681. // non-overlapping ranges.
  1682. if (UseBlockValue) {
  1683. Module *M = CxtI->getModule();
  1684. ValueLatticeElement L =
  1685. getImpl(PImpl, AC, M).getValueInBlock(LHS, CxtI->getParent(), CxtI);
  1686. if (L.isOverdefined())
  1687. return LazyValueInfo::Unknown;
  1688. ValueLatticeElement R =
  1689. getImpl(PImpl, AC, M).getValueInBlock(RHS, CxtI->getParent(), CxtI);
  1690. Type *Ty = CmpInst::makeCmpResultType(LHS->getType());
  1691. if (Constant *Res = L.getCompare((CmpInst::Predicate)P, Ty, R,
  1692. M->getDataLayout())) {
  1693. if (Res->isNullValue())
  1694. return LazyValueInfo::False;
  1695. if (Res->isOneValue())
  1696. return LazyValueInfo::True;
  1697. }
  1698. }
  1699. return LazyValueInfo::Unknown;
  1700. }
  1701. void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
  1702. BasicBlock *NewSucc) {
  1703. if (PImpl) {
  1704. getImpl(PImpl, AC, PredBB->getModule())
  1705. .threadEdge(PredBB, OldSucc, NewSucc);
  1706. }
  1707. }
  1708. void LazyValueInfo::eraseBlock(BasicBlock *BB) {
  1709. if (PImpl) {
  1710. getImpl(PImpl, AC, BB->getModule()).eraseBlock(BB);
  1711. }
  1712. }
  1713. void LazyValueInfo::clear(const Module *M) {
  1714. if (PImpl) {
  1715. getImpl(PImpl, AC, M).clear();
  1716. }
  1717. }
  1718. void LazyValueInfo::printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) {
  1719. if (PImpl) {
  1720. getImpl(PImpl, AC, F.getParent()).printLVI(F, DTree, OS);
  1721. }
  1722. }
  1723. // Print the LVI for the function arguments at the start of each basic block.
  1724. void LazyValueInfoAnnotatedWriter::emitBasicBlockStartAnnot(
  1725. const BasicBlock *BB, formatted_raw_ostream &OS) {
  1726. // Find if there are latticevalues defined for arguments of the function.
  1727. auto *F = BB->getParent();
  1728. for (const auto &Arg : F->args()) {
  1729. ValueLatticeElement Result = LVIImpl->getValueInBlock(
  1730. const_cast<Argument *>(&Arg), const_cast<BasicBlock *>(BB));
  1731. if (Result.isUnknown())
  1732. continue;
  1733. OS << "; LatticeVal for: '" << Arg << "' is: " << Result << "\n";
  1734. }
  1735. }
  1736. // This function prints the LVI analysis for the instruction I at the beginning
  1737. // of various basic blocks. It relies on calculated values that are stored in
  1738. // the LazyValueInfoCache, and in the absence of cached values, recalculate the
  1739. // LazyValueInfo for `I`, and print that info.
  1740. void LazyValueInfoAnnotatedWriter::emitInstructionAnnot(
  1741. const Instruction *I, formatted_raw_ostream &OS) {
  1742. auto *ParentBB = I->getParent();
  1743. SmallPtrSet<const BasicBlock*, 16> BlocksContainingLVI;
  1744. // We can generate (solve) LVI values only for blocks that are dominated by
  1745. // the I's parent. However, to avoid generating LVI for all dominating blocks,
  1746. // that contain redundant/uninteresting information, we print LVI for
  1747. // blocks that may use this LVI information (such as immediate successor
  1748. // blocks, and blocks that contain uses of `I`).
  1749. auto printResult = [&](const BasicBlock *BB) {
  1750. if (!BlocksContainingLVI.insert(BB).second)
  1751. return;
  1752. ValueLatticeElement Result = LVIImpl->getValueInBlock(
  1753. const_cast<Instruction *>(I), const_cast<BasicBlock *>(BB));
  1754. OS << "; LatticeVal for: '" << *I << "' in BB: '";
  1755. BB->printAsOperand(OS, false);
  1756. OS << "' is: " << Result << "\n";
  1757. };
  1758. printResult(ParentBB);
  1759. // Print the LVI analysis results for the immediate successor blocks, that
  1760. // are dominated by `ParentBB`.
  1761. for (const auto *BBSucc : successors(ParentBB))
  1762. if (DT.dominates(ParentBB, BBSucc))
  1763. printResult(BBSucc);
  1764. // Print LVI in blocks where `I` is used.
  1765. for (const auto *U : I->users())
  1766. if (auto *UseI = dyn_cast<Instruction>(U))
  1767. if (!isa<PHINode>(UseI) || DT.dominates(ParentBB, UseI->getParent()))
  1768. printResult(UseI->getParent());
  1769. }
  1770. namespace {
  1771. // Printer class for LazyValueInfo results.
  1772. class LazyValueInfoPrinter : public FunctionPass {
  1773. public:
  1774. static char ID; // Pass identification, replacement for typeid
  1775. LazyValueInfoPrinter() : FunctionPass(ID) {
  1776. initializeLazyValueInfoPrinterPass(*PassRegistry::getPassRegistry());
  1777. }
  1778. void getAnalysisUsage(AnalysisUsage &AU) const override {
  1779. AU.setPreservesAll();
  1780. AU.addRequired<LazyValueInfoWrapperPass>();
  1781. AU.addRequired<DominatorTreeWrapperPass>();
  1782. }
  1783. // Get the mandatory dominator tree analysis and pass this in to the
  1784. // LVIPrinter. We cannot rely on the LVI's DT, since it's optional.
  1785. bool runOnFunction(Function &F) override {
  1786. dbgs() << "LVI for function '" << F.getName() << "':\n";
  1787. auto &LVI = getAnalysis<LazyValueInfoWrapperPass>().getLVI();
  1788. auto &DTree = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  1789. LVI.printLVI(F, DTree, dbgs());
  1790. return false;
  1791. }
  1792. };
  1793. }
  1794. char LazyValueInfoPrinter::ID = 0;
  1795. INITIALIZE_PASS_BEGIN(LazyValueInfoPrinter, "print-lazy-value-info",
  1796. "Lazy Value Info Printer Pass", false, false)
  1797. INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
  1798. INITIALIZE_PASS_END(LazyValueInfoPrinter, "print-lazy-value-info",
  1799. "Lazy Value Info Printer Pass", false, false)