LazyValueInfo.cpp 76 KB

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