SCCPSolver.cpp 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  1. //===- SCCPSolver.cpp - SCCP Utility --------------------------- *- 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. // \file
  10. // This file implements the Sparse Conditional Constant Propagation (SCCP)
  11. // utility.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Transforms/Utils/SCCPSolver.h"
  15. #include "llvm/Analysis/ConstantFolding.h"
  16. #include "llvm/Analysis/InstructionSimplify.h"
  17. #include "llvm/Analysis/ValueLattice.h"
  18. #include "llvm/Analysis/ValueLatticeUtils.h"
  19. #include "llvm/IR/InstVisitor.h"
  20. #include "llvm/Support/Casting.h"
  21. #include "llvm/Support/Debug.h"
  22. #include "llvm/Support/ErrorHandling.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. #include "llvm/Transforms/Utils/Local.h"
  25. #include <cassert>
  26. #include <utility>
  27. #include <vector>
  28. using namespace llvm;
  29. #define DEBUG_TYPE "sccp"
  30. // The maximum number of range extensions allowed for operations requiring
  31. // widening.
  32. static const unsigned MaxNumRangeExtensions = 10;
  33. /// Returns MergeOptions with MaxWidenSteps set to MaxNumRangeExtensions.
  34. static ValueLatticeElement::MergeOptions getMaxWidenStepsOpts() {
  35. return ValueLatticeElement::MergeOptions().setMaxWidenSteps(
  36. MaxNumRangeExtensions);
  37. }
  38. namespace llvm {
  39. bool SCCPSolver::isConstant(const ValueLatticeElement &LV) {
  40. return LV.isConstant() ||
  41. (LV.isConstantRange() && LV.getConstantRange().isSingleElement());
  42. }
  43. bool SCCPSolver::isOverdefined(const ValueLatticeElement &LV) {
  44. return !LV.isUnknownOrUndef() && !SCCPSolver::isConstant(LV);
  45. }
  46. static bool canRemoveInstruction(Instruction *I) {
  47. if (wouldInstructionBeTriviallyDead(I))
  48. return true;
  49. // Some instructions can be handled but are rejected above. Catch
  50. // those cases by falling through to here.
  51. // TODO: Mark globals as being constant earlier, so
  52. // TODO: wouldInstructionBeTriviallyDead() knows that atomic loads
  53. // TODO: are safe to remove.
  54. return isa<LoadInst>(I);
  55. }
  56. bool SCCPSolver::tryToReplaceWithConstant(Value *V) {
  57. Constant *Const = nullptr;
  58. if (V->getType()->isStructTy()) {
  59. std::vector<ValueLatticeElement> IVs = getStructLatticeValueFor(V);
  60. if (llvm::any_of(IVs, isOverdefined))
  61. return false;
  62. std::vector<Constant *> ConstVals;
  63. auto *ST = cast<StructType>(V->getType());
  64. for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
  65. ValueLatticeElement V = IVs[i];
  66. ConstVals.push_back(SCCPSolver::isConstant(V)
  67. ? getConstant(V)
  68. : UndefValue::get(ST->getElementType(i)));
  69. }
  70. Const = ConstantStruct::get(ST, ConstVals);
  71. } else {
  72. const ValueLatticeElement &IV = getLatticeValueFor(V);
  73. if (isOverdefined(IV))
  74. return false;
  75. Const = SCCPSolver::isConstant(IV) ? getConstant(IV)
  76. : UndefValue::get(V->getType());
  77. }
  78. assert(Const && "Constant is nullptr here!");
  79. // Replacing `musttail` instructions with constant breaks `musttail` invariant
  80. // unless the call itself can be removed.
  81. // Calls with "clang.arc.attachedcall" implicitly use the return value and
  82. // those uses cannot be updated with a constant.
  83. CallBase *CB = dyn_cast<CallBase>(V);
  84. if (CB && ((CB->isMustTailCall() &&
  85. !canRemoveInstruction(CB)) ||
  86. CB->getOperandBundle(LLVMContext::OB_clang_arc_attachedcall))) {
  87. Function *F = CB->getCalledFunction();
  88. // Don't zap returns of the callee
  89. if (F)
  90. addToMustPreserveReturnsInFunctions(F);
  91. LLVM_DEBUG(dbgs() << " Can\'t treat the result of call " << *CB
  92. << " as a constant\n");
  93. return false;
  94. }
  95. LLVM_DEBUG(dbgs() << " Constant: " << *Const << " = " << *V << '\n');
  96. // Replaces all of the uses of a variable with uses of the constant.
  97. V->replaceAllUsesWith(Const);
  98. return true;
  99. }
  100. /// Try to replace signed instructions with their unsigned equivalent.
  101. static bool replaceSignedInst(SCCPSolver &Solver,
  102. SmallPtrSetImpl<Value *> &InsertedValues,
  103. Instruction &Inst) {
  104. // Determine if a signed value is known to be >= 0.
  105. auto isNonNegative = [&Solver](Value *V) {
  106. // If this value was constant-folded, it may not have a solver entry.
  107. // Handle integers. Otherwise, return false.
  108. if (auto *C = dyn_cast<Constant>(V)) {
  109. auto *CInt = dyn_cast<ConstantInt>(C);
  110. return CInt && !CInt->isNegative();
  111. }
  112. const ValueLatticeElement &IV = Solver.getLatticeValueFor(V);
  113. return IV.isConstantRange(/*UndefAllowed=*/false) &&
  114. IV.getConstantRange().isAllNonNegative();
  115. };
  116. Instruction *NewInst = nullptr;
  117. switch (Inst.getOpcode()) {
  118. // Note: We do not fold sitofp -> uitofp here because that could be more
  119. // expensive in codegen and may not be reversible in the backend.
  120. case Instruction::SExt: {
  121. // If the source value is not negative, this is a zext.
  122. Value *Op0 = Inst.getOperand(0);
  123. if (InsertedValues.count(Op0) || !isNonNegative(Op0))
  124. return false;
  125. NewInst = new ZExtInst(Op0, Inst.getType(), "", &Inst);
  126. break;
  127. }
  128. case Instruction::AShr: {
  129. // If the shifted value is not negative, this is a logical shift right.
  130. Value *Op0 = Inst.getOperand(0);
  131. if (InsertedValues.count(Op0) || !isNonNegative(Op0))
  132. return false;
  133. NewInst = BinaryOperator::CreateLShr(Op0, Inst.getOperand(1), "", &Inst);
  134. break;
  135. }
  136. case Instruction::SDiv:
  137. case Instruction::SRem: {
  138. // If both operands are not negative, this is the same as udiv/urem.
  139. Value *Op0 = Inst.getOperand(0), *Op1 = Inst.getOperand(1);
  140. if (InsertedValues.count(Op0) || InsertedValues.count(Op1) ||
  141. !isNonNegative(Op0) || !isNonNegative(Op1))
  142. return false;
  143. auto NewOpcode = Inst.getOpcode() == Instruction::SDiv ? Instruction::UDiv
  144. : Instruction::URem;
  145. NewInst = BinaryOperator::Create(NewOpcode, Op0, Op1, "", &Inst);
  146. break;
  147. }
  148. default:
  149. return false;
  150. }
  151. // Wire up the new instruction and update state.
  152. assert(NewInst && "Expected replacement instruction");
  153. NewInst->takeName(&Inst);
  154. InsertedValues.insert(NewInst);
  155. Inst.replaceAllUsesWith(NewInst);
  156. Solver.removeLatticeValueFor(&Inst);
  157. Inst.eraseFromParent();
  158. return true;
  159. }
  160. bool SCCPSolver::simplifyInstsInBlock(BasicBlock &BB,
  161. SmallPtrSetImpl<Value *> &InsertedValues,
  162. Statistic &InstRemovedStat,
  163. Statistic &InstReplacedStat) {
  164. bool MadeChanges = false;
  165. for (Instruction &Inst : make_early_inc_range(BB)) {
  166. if (Inst.getType()->isVoidTy())
  167. continue;
  168. if (tryToReplaceWithConstant(&Inst)) {
  169. if (canRemoveInstruction(&Inst))
  170. Inst.eraseFromParent();
  171. MadeChanges = true;
  172. ++InstRemovedStat;
  173. } else if (replaceSignedInst(*this, InsertedValues, Inst)) {
  174. MadeChanges = true;
  175. ++InstReplacedStat;
  176. }
  177. }
  178. return MadeChanges;
  179. }
  180. bool SCCPSolver::removeNonFeasibleEdges(BasicBlock *BB, DomTreeUpdater &DTU,
  181. BasicBlock *&NewUnreachableBB) const {
  182. SmallPtrSet<BasicBlock *, 8> FeasibleSuccessors;
  183. bool HasNonFeasibleEdges = false;
  184. for (BasicBlock *Succ : successors(BB)) {
  185. if (isEdgeFeasible(BB, Succ))
  186. FeasibleSuccessors.insert(Succ);
  187. else
  188. HasNonFeasibleEdges = true;
  189. }
  190. // All edges feasible, nothing to do.
  191. if (!HasNonFeasibleEdges)
  192. return false;
  193. // SCCP can only determine non-feasible edges for br, switch and indirectbr.
  194. Instruction *TI = BB->getTerminator();
  195. assert((isa<BranchInst>(TI) || isa<SwitchInst>(TI) ||
  196. isa<IndirectBrInst>(TI)) &&
  197. "Terminator must be a br, switch or indirectbr");
  198. if (FeasibleSuccessors.size() == 0) {
  199. // Branch on undef/poison, replace with unreachable.
  200. SmallPtrSet<BasicBlock *, 8> SeenSuccs;
  201. SmallVector<DominatorTree::UpdateType, 8> Updates;
  202. for (BasicBlock *Succ : successors(BB)) {
  203. Succ->removePredecessor(BB);
  204. if (SeenSuccs.insert(Succ).second)
  205. Updates.push_back({DominatorTree::Delete, BB, Succ});
  206. }
  207. TI->eraseFromParent();
  208. new UnreachableInst(BB->getContext(), BB);
  209. DTU.applyUpdatesPermissive(Updates);
  210. } else if (FeasibleSuccessors.size() == 1) {
  211. // Replace with an unconditional branch to the only feasible successor.
  212. BasicBlock *OnlyFeasibleSuccessor = *FeasibleSuccessors.begin();
  213. SmallVector<DominatorTree::UpdateType, 8> Updates;
  214. bool HaveSeenOnlyFeasibleSuccessor = false;
  215. for (BasicBlock *Succ : successors(BB)) {
  216. if (Succ == OnlyFeasibleSuccessor && !HaveSeenOnlyFeasibleSuccessor) {
  217. // Don't remove the edge to the only feasible successor the first time
  218. // we see it. We still do need to remove any multi-edges to it though.
  219. HaveSeenOnlyFeasibleSuccessor = true;
  220. continue;
  221. }
  222. Succ->removePredecessor(BB);
  223. Updates.push_back({DominatorTree::Delete, BB, Succ});
  224. }
  225. BranchInst::Create(OnlyFeasibleSuccessor, BB);
  226. TI->eraseFromParent();
  227. DTU.applyUpdatesPermissive(Updates);
  228. } else if (FeasibleSuccessors.size() > 1) {
  229. SwitchInstProfUpdateWrapper SI(*cast<SwitchInst>(TI));
  230. SmallVector<DominatorTree::UpdateType, 8> Updates;
  231. // If the default destination is unfeasible it will never be taken. Replace
  232. // it with a new block with a single Unreachable instruction.
  233. BasicBlock *DefaultDest = SI->getDefaultDest();
  234. if (!FeasibleSuccessors.contains(DefaultDest)) {
  235. if (!NewUnreachableBB) {
  236. NewUnreachableBB =
  237. BasicBlock::Create(DefaultDest->getContext(), "default.unreachable",
  238. DefaultDest->getParent(), DefaultDest);
  239. new UnreachableInst(DefaultDest->getContext(), NewUnreachableBB);
  240. }
  241. SI->setDefaultDest(NewUnreachableBB);
  242. Updates.push_back({DominatorTree::Delete, BB, DefaultDest});
  243. Updates.push_back({DominatorTree::Insert, BB, NewUnreachableBB});
  244. }
  245. for (auto CI = SI->case_begin(); CI != SI->case_end();) {
  246. if (FeasibleSuccessors.contains(CI->getCaseSuccessor())) {
  247. ++CI;
  248. continue;
  249. }
  250. BasicBlock *Succ = CI->getCaseSuccessor();
  251. Succ->removePredecessor(BB);
  252. Updates.push_back({DominatorTree::Delete, BB, Succ});
  253. SI.removeCase(CI);
  254. // Don't increment CI, as we removed a case.
  255. }
  256. DTU.applyUpdatesPermissive(Updates);
  257. } else {
  258. llvm_unreachable("Must have at least one feasible successor");
  259. }
  260. return true;
  261. }
  262. /// Helper class for SCCPSolver. This implements the instruction visitor and
  263. /// holds all the state.
  264. class SCCPInstVisitor : public InstVisitor<SCCPInstVisitor> {
  265. const DataLayout &DL;
  266. std::function<const TargetLibraryInfo &(Function &)> GetTLI;
  267. SmallPtrSet<BasicBlock *, 8> BBExecutable; // The BBs that are executable.
  268. DenseMap<Value *, ValueLatticeElement>
  269. ValueState; // The state each value is in.
  270. /// StructValueState - This maintains ValueState for values that have
  271. /// StructType, for example for formal arguments, calls, insertelement, etc.
  272. DenseMap<std::pair<Value *, unsigned>, ValueLatticeElement> StructValueState;
  273. /// GlobalValue - If we are tracking any values for the contents of a global
  274. /// variable, we keep a mapping from the constant accessor to the element of
  275. /// the global, to the currently known value. If the value becomes
  276. /// overdefined, it's entry is simply removed from this map.
  277. DenseMap<GlobalVariable *, ValueLatticeElement> TrackedGlobals;
  278. /// TrackedRetVals - If we are tracking arguments into and the return
  279. /// value out of a function, it will have an entry in this map, indicating
  280. /// what the known return value for the function is.
  281. MapVector<Function *, ValueLatticeElement> TrackedRetVals;
  282. /// TrackedMultipleRetVals - Same as TrackedRetVals, but used for functions
  283. /// that return multiple values.
  284. MapVector<std::pair<Function *, unsigned>, ValueLatticeElement>
  285. TrackedMultipleRetVals;
  286. /// MRVFunctionsTracked - Each function in TrackedMultipleRetVals is
  287. /// represented here for efficient lookup.
  288. SmallPtrSet<Function *, 16> MRVFunctionsTracked;
  289. /// A list of functions whose return cannot be modified.
  290. SmallPtrSet<Function *, 16> MustPreserveReturnsInFunctions;
  291. /// TrackingIncomingArguments - This is the set of functions for whose
  292. /// arguments we make optimistic assumptions about and try to prove as
  293. /// constants.
  294. SmallPtrSet<Function *, 16> TrackingIncomingArguments;
  295. /// The reason for two worklists is that overdefined is the lowest state
  296. /// on the lattice, and moving things to overdefined as fast as possible
  297. /// makes SCCP converge much faster.
  298. ///
  299. /// By having a separate worklist, we accomplish this because everything
  300. /// possibly overdefined will become overdefined at the soonest possible
  301. /// point.
  302. SmallVector<Value *, 64> OverdefinedInstWorkList;
  303. SmallVector<Value *, 64> InstWorkList;
  304. // The BasicBlock work list
  305. SmallVector<BasicBlock *, 64> BBWorkList;
  306. /// KnownFeasibleEdges - Entries in this set are edges which have already had
  307. /// PHI nodes retriggered.
  308. using Edge = std::pair<BasicBlock *, BasicBlock *>;
  309. DenseSet<Edge> KnownFeasibleEdges;
  310. DenseMap<Function *, AnalysisResultsForFn> AnalysisResults;
  311. DenseMap<Value *, SmallPtrSet<User *, 2>> AdditionalUsers;
  312. LLVMContext &Ctx;
  313. private:
  314. ConstantInt *getConstantInt(const ValueLatticeElement &IV) const {
  315. return dyn_cast_or_null<ConstantInt>(getConstant(IV));
  316. }
  317. // pushToWorkList - Helper for markConstant/markOverdefined
  318. void pushToWorkList(ValueLatticeElement &IV, Value *V);
  319. // Helper to push \p V to the worklist, after updating it to \p IV. Also
  320. // prints a debug message with the updated value.
  321. void pushToWorkListMsg(ValueLatticeElement &IV, Value *V);
  322. // markConstant - Make a value be marked as "constant". If the value
  323. // is not already a constant, add it to the instruction work list so that
  324. // the users of the instruction are updated later.
  325. bool markConstant(ValueLatticeElement &IV, Value *V, Constant *C,
  326. bool MayIncludeUndef = false);
  327. bool markConstant(Value *V, Constant *C) {
  328. assert(!V->getType()->isStructTy() && "structs should use mergeInValue");
  329. return markConstant(ValueState[V], V, C);
  330. }
  331. // markOverdefined - Make a value be marked as "overdefined". If the
  332. // value is not already overdefined, add it to the overdefined instruction
  333. // work list so that the users of the instruction are updated later.
  334. bool markOverdefined(ValueLatticeElement &IV, Value *V);
  335. /// Merge \p MergeWithV into \p IV and push \p V to the worklist, if \p IV
  336. /// changes.
  337. bool mergeInValue(ValueLatticeElement &IV, Value *V,
  338. ValueLatticeElement MergeWithV,
  339. ValueLatticeElement::MergeOptions Opts = {
  340. /*MayIncludeUndef=*/false, /*CheckWiden=*/false});
  341. bool mergeInValue(Value *V, ValueLatticeElement MergeWithV,
  342. ValueLatticeElement::MergeOptions Opts = {
  343. /*MayIncludeUndef=*/false, /*CheckWiden=*/false}) {
  344. assert(!V->getType()->isStructTy() &&
  345. "non-structs should use markConstant");
  346. return mergeInValue(ValueState[V], V, MergeWithV, Opts);
  347. }
  348. /// getValueState - Return the ValueLatticeElement object that corresponds to
  349. /// the value. This function handles the case when the value hasn't been seen
  350. /// yet by properly seeding constants etc.
  351. ValueLatticeElement &getValueState(Value *V) {
  352. assert(!V->getType()->isStructTy() && "Should use getStructValueState");
  353. auto I = ValueState.insert(std::make_pair(V, ValueLatticeElement()));
  354. ValueLatticeElement &LV = I.first->second;
  355. if (!I.second)
  356. return LV; // Common case, already in the map.
  357. if (auto *C = dyn_cast<Constant>(V))
  358. LV.markConstant(C); // Constants are constant
  359. // All others are unknown by default.
  360. return LV;
  361. }
  362. /// getStructValueState - Return the ValueLatticeElement object that
  363. /// corresponds to the value/field pair. This function handles the case when
  364. /// the value hasn't been seen yet by properly seeding constants etc.
  365. ValueLatticeElement &getStructValueState(Value *V, unsigned i) {
  366. assert(V->getType()->isStructTy() && "Should use getValueState");
  367. assert(i < cast<StructType>(V->getType())->getNumElements() &&
  368. "Invalid element #");
  369. auto I = StructValueState.insert(
  370. std::make_pair(std::make_pair(V, i), ValueLatticeElement()));
  371. ValueLatticeElement &LV = I.first->second;
  372. if (!I.second)
  373. return LV; // Common case, already in the map.
  374. if (auto *C = dyn_cast<Constant>(V)) {
  375. Constant *Elt = C->getAggregateElement(i);
  376. if (!Elt)
  377. LV.markOverdefined(); // Unknown sort of constant.
  378. else
  379. LV.markConstant(Elt); // Constants are constant.
  380. }
  381. // All others are underdefined by default.
  382. return LV;
  383. }
  384. /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
  385. /// work list if it is not already executable.
  386. bool markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest);
  387. // getFeasibleSuccessors - Return a vector of booleans to indicate which
  388. // successors are reachable from a given terminator instruction.
  389. void getFeasibleSuccessors(Instruction &TI, SmallVectorImpl<bool> &Succs);
  390. // OperandChangedState - This method is invoked on all of the users of an
  391. // instruction that was just changed state somehow. Based on this
  392. // information, we need to update the specified user of this instruction.
  393. void operandChangedState(Instruction *I) {
  394. if (BBExecutable.count(I->getParent())) // Inst is executable?
  395. visit(*I);
  396. }
  397. // Add U as additional user of V.
  398. void addAdditionalUser(Value *V, User *U) {
  399. auto Iter = AdditionalUsers.insert({V, {}});
  400. Iter.first->second.insert(U);
  401. }
  402. // Mark I's users as changed, including AdditionalUsers.
  403. void markUsersAsChanged(Value *I) {
  404. // Functions include their arguments in the use-list. Changed function
  405. // values mean that the result of the function changed. We only need to
  406. // update the call sites with the new function result and do not have to
  407. // propagate the call arguments.
  408. if (isa<Function>(I)) {
  409. for (User *U : I->users()) {
  410. if (auto *CB = dyn_cast<CallBase>(U))
  411. handleCallResult(*CB);
  412. }
  413. } else {
  414. for (User *U : I->users())
  415. if (auto *UI = dyn_cast<Instruction>(U))
  416. operandChangedState(UI);
  417. }
  418. auto Iter = AdditionalUsers.find(I);
  419. if (Iter != AdditionalUsers.end()) {
  420. // Copy additional users before notifying them of changes, because new
  421. // users may be added, potentially invalidating the iterator.
  422. SmallVector<Instruction *, 2> ToNotify;
  423. for (User *U : Iter->second)
  424. if (auto *UI = dyn_cast<Instruction>(U))
  425. ToNotify.push_back(UI);
  426. for (Instruction *UI : ToNotify)
  427. operandChangedState(UI);
  428. }
  429. }
  430. void handleCallOverdefined(CallBase &CB);
  431. void handleCallResult(CallBase &CB);
  432. void handleCallArguments(CallBase &CB);
  433. void handleExtractOfWithOverflow(ExtractValueInst &EVI,
  434. const WithOverflowInst *WO, unsigned Idx);
  435. private:
  436. friend class InstVisitor<SCCPInstVisitor>;
  437. // visit implementations - Something changed in this instruction. Either an
  438. // operand made a transition, or the instruction is newly executable. Change
  439. // the value type of I to reflect these changes if appropriate.
  440. void visitPHINode(PHINode &I);
  441. // Terminators
  442. void visitReturnInst(ReturnInst &I);
  443. void visitTerminator(Instruction &TI);
  444. void visitCastInst(CastInst &I);
  445. void visitSelectInst(SelectInst &I);
  446. void visitUnaryOperator(Instruction &I);
  447. void visitBinaryOperator(Instruction &I);
  448. void visitCmpInst(CmpInst &I);
  449. void visitExtractValueInst(ExtractValueInst &EVI);
  450. void visitInsertValueInst(InsertValueInst &IVI);
  451. void visitCatchSwitchInst(CatchSwitchInst &CPI) {
  452. markOverdefined(&CPI);
  453. visitTerminator(CPI);
  454. }
  455. // Instructions that cannot be folded away.
  456. void visitStoreInst(StoreInst &I);
  457. void visitLoadInst(LoadInst &I);
  458. void visitGetElementPtrInst(GetElementPtrInst &I);
  459. void visitInvokeInst(InvokeInst &II) {
  460. visitCallBase(II);
  461. visitTerminator(II);
  462. }
  463. void visitCallBrInst(CallBrInst &CBI) {
  464. visitCallBase(CBI);
  465. visitTerminator(CBI);
  466. }
  467. void visitCallBase(CallBase &CB);
  468. void visitResumeInst(ResumeInst &I) { /*returns void*/
  469. }
  470. void visitUnreachableInst(UnreachableInst &I) { /*returns void*/
  471. }
  472. void visitFenceInst(FenceInst &I) { /*returns void*/
  473. }
  474. void visitInstruction(Instruction &I);
  475. public:
  476. void addAnalysis(Function &F, AnalysisResultsForFn A) {
  477. AnalysisResults.insert({&F, std::move(A)});
  478. }
  479. void visitCallInst(CallInst &I) { visitCallBase(I); }
  480. bool markBlockExecutable(BasicBlock *BB);
  481. const PredicateBase *getPredicateInfoFor(Instruction *I) {
  482. auto A = AnalysisResults.find(I->getParent()->getParent());
  483. if (A == AnalysisResults.end())
  484. return nullptr;
  485. return A->second.PredInfo->getPredicateInfoFor(I);
  486. }
  487. const LoopInfo &getLoopInfo(Function &F) {
  488. auto A = AnalysisResults.find(&F);
  489. assert(A != AnalysisResults.end() && A->second.LI &&
  490. "Need LoopInfo analysis results for function.");
  491. return *A->second.LI;
  492. }
  493. DomTreeUpdater getDTU(Function &F) {
  494. auto A = AnalysisResults.find(&F);
  495. assert(A != AnalysisResults.end() && "Need analysis results for function.");
  496. return {A->second.DT, A->second.PDT, DomTreeUpdater::UpdateStrategy::Lazy};
  497. }
  498. SCCPInstVisitor(const DataLayout &DL,
  499. std::function<const TargetLibraryInfo &(Function &)> GetTLI,
  500. LLVMContext &Ctx)
  501. : DL(DL), GetTLI(GetTLI), Ctx(Ctx) {}
  502. void trackValueOfGlobalVariable(GlobalVariable *GV) {
  503. // We only track the contents of scalar globals.
  504. if (GV->getValueType()->isSingleValueType()) {
  505. ValueLatticeElement &IV = TrackedGlobals[GV];
  506. IV.markConstant(GV->getInitializer());
  507. }
  508. }
  509. void addTrackedFunction(Function *F) {
  510. // Add an entry, F -> undef.
  511. if (auto *STy = dyn_cast<StructType>(F->getReturnType())) {
  512. MRVFunctionsTracked.insert(F);
  513. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
  514. TrackedMultipleRetVals.insert(
  515. std::make_pair(std::make_pair(F, i), ValueLatticeElement()));
  516. } else if (!F->getReturnType()->isVoidTy())
  517. TrackedRetVals.insert(std::make_pair(F, ValueLatticeElement()));
  518. }
  519. void addToMustPreserveReturnsInFunctions(Function *F) {
  520. MustPreserveReturnsInFunctions.insert(F);
  521. }
  522. bool mustPreserveReturn(Function *F) {
  523. return MustPreserveReturnsInFunctions.count(F);
  524. }
  525. void addArgumentTrackedFunction(Function *F) {
  526. TrackingIncomingArguments.insert(F);
  527. }
  528. bool isArgumentTrackedFunction(Function *F) {
  529. return TrackingIncomingArguments.count(F);
  530. }
  531. void solve();
  532. bool resolvedUndefsIn(Function &F);
  533. bool isBlockExecutable(BasicBlock *BB) const {
  534. return BBExecutable.count(BB);
  535. }
  536. bool isEdgeFeasible(BasicBlock *From, BasicBlock *To) const;
  537. std::vector<ValueLatticeElement> getStructLatticeValueFor(Value *V) const {
  538. std::vector<ValueLatticeElement> StructValues;
  539. auto *STy = dyn_cast<StructType>(V->getType());
  540. assert(STy && "getStructLatticeValueFor() can be called only on structs");
  541. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
  542. auto I = StructValueState.find(std::make_pair(V, i));
  543. assert(I != StructValueState.end() && "Value not in valuemap!");
  544. StructValues.push_back(I->second);
  545. }
  546. return StructValues;
  547. }
  548. void removeLatticeValueFor(Value *V) { ValueState.erase(V); }
  549. const ValueLatticeElement &getLatticeValueFor(Value *V) const {
  550. assert(!V->getType()->isStructTy() &&
  551. "Should use getStructLatticeValueFor");
  552. DenseMap<Value *, ValueLatticeElement>::const_iterator I =
  553. ValueState.find(V);
  554. assert(I != ValueState.end() &&
  555. "V not found in ValueState nor Paramstate map!");
  556. return I->second;
  557. }
  558. const MapVector<Function *, ValueLatticeElement> &getTrackedRetVals() {
  559. return TrackedRetVals;
  560. }
  561. const DenseMap<GlobalVariable *, ValueLatticeElement> &getTrackedGlobals() {
  562. return TrackedGlobals;
  563. }
  564. const SmallPtrSet<Function *, 16> getMRVFunctionsTracked() {
  565. return MRVFunctionsTracked;
  566. }
  567. void markOverdefined(Value *V) {
  568. if (auto *STy = dyn_cast<StructType>(V->getType()))
  569. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
  570. markOverdefined(getStructValueState(V, i), V);
  571. else
  572. markOverdefined(ValueState[V], V);
  573. }
  574. bool isStructLatticeConstant(Function *F, StructType *STy);
  575. Constant *getConstant(const ValueLatticeElement &LV) const;
  576. ConstantRange getConstantRange(const ValueLatticeElement &LV, Type *Ty) const;
  577. SmallPtrSetImpl<Function *> &getArgumentTrackedFunctions() {
  578. return TrackingIncomingArguments;
  579. }
  580. void markArgInFuncSpecialization(Function *F,
  581. const SmallVectorImpl<ArgInfo> &Args);
  582. void markFunctionUnreachable(Function *F) {
  583. for (auto &BB : *F)
  584. BBExecutable.erase(&BB);
  585. }
  586. void solveWhileResolvedUndefsIn(Module &M) {
  587. bool ResolvedUndefs = true;
  588. while (ResolvedUndefs) {
  589. solve();
  590. ResolvedUndefs = false;
  591. for (Function &F : M)
  592. ResolvedUndefs |= resolvedUndefsIn(F);
  593. }
  594. }
  595. void solveWhileResolvedUndefsIn(SmallVectorImpl<Function *> &WorkList) {
  596. bool ResolvedUndefs = true;
  597. while (ResolvedUndefs) {
  598. solve();
  599. ResolvedUndefs = false;
  600. for (Function *F : WorkList)
  601. ResolvedUndefs |= resolvedUndefsIn(*F);
  602. }
  603. }
  604. };
  605. } // namespace llvm
  606. bool SCCPInstVisitor::markBlockExecutable(BasicBlock *BB) {
  607. if (!BBExecutable.insert(BB).second)
  608. return false;
  609. LLVM_DEBUG(dbgs() << "Marking Block Executable: " << BB->getName() << '\n');
  610. BBWorkList.push_back(BB); // Add the block to the work list!
  611. return true;
  612. }
  613. void SCCPInstVisitor::pushToWorkList(ValueLatticeElement &IV, Value *V) {
  614. if (IV.isOverdefined())
  615. return OverdefinedInstWorkList.push_back(V);
  616. InstWorkList.push_back(V);
  617. }
  618. void SCCPInstVisitor::pushToWorkListMsg(ValueLatticeElement &IV, Value *V) {
  619. LLVM_DEBUG(dbgs() << "updated " << IV << ": " << *V << '\n');
  620. pushToWorkList(IV, V);
  621. }
  622. bool SCCPInstVisitor::markConstant(ValueLatticeElement &IV, Value *V,
  623. Constant *C, bool MayIncludeUndef) {
  624. if (!IV.markConstant(C, MayIncludeUndef))
  625. return false;
  626. LLVM_DEBUG(dbgs() << "markConstant: " << *C << ": " << *V << '\n');
  627. pushToWorkList(IV, V);
  628. return true;
  629. }
  630. bool SCCPInstVisitor::markOverdefined(ValueLatticeElement &IV, Value *V) {
  631. if (!IV.markOverdefined())
  632. return false;
  633. LLVM_DEBUG(dbgs() << "markOverdefined: ";
  634. if (auto *F = dyn_cast<Function>(V)) dbgs()
  635. << "Function '" << F->getName() << "'\n";
  636. else dbgs() << *V << '\n');
  637. // Only instructions go on the work list
  638. pushToWorkList(IV, V);
  639. return true;
  640. }
  641. bool SCCPInstVisitor::isStructLatticeConstant(Function *F, StructType *STy) {
  642. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
  643. const auto &It = TrackedMultipleRetVals.find(std::make_pair(F, i));
  644. assert(It != TrackedMultipleRetVals.end());
  645. ValueLatticeElement LV = It->second;
  646. if (!SCCPSolver::isConstant(LV))
  647. return false;
  648. }
  649. return true;
  650. }
  651. Constant *SCCPInstVisitor::getConstant(const ValueLatticeElement &LV) const {
  652. if (LV.isConstant())
  653. return LV.getConstant();
  654. if (LV.isConstantRange()) {
  655. const auto &CR = LV.getConstantRange();
  656. if (CR.getSingleElement())
  657. return ConstantInt::get(Ctx, *CR.getSingleElement());
  658. }
  659. return nullptr;
  660. }
  661. ConstantRange
  662. SCCPInstVisitor::getConstantRange(const ValueLatticeElement &LV,
  663. Type *Ty) const {
  664. assert(Ty->isIntOrIntVectorTy() && "Should be int or int vector");
  665. if (LV.isConstantRange())
  666. return LV.getConstantRange();
  667. return ConstantRange::getFull(Ty->getScalarSizeInBits());
  668. }
  669. void SCCPInstVisitor::markArgInFuncSpecialization(
  670. Function *F, const SmallVectorImpl<ArgInfo> &Args) {
  671. assert(!Args.empty() && "Specialization without arguments");
  672. assert(F->arg_size() == Args[0].Formal->getParent()->arg_size() &&
  673. "Functions should have the same number of arguments");
  674. auto Iter = Args.begin();
  675. Argument *NewArg = F->arg_begin();
  676. Argument *OldArg = Args[0].Formal->getParent()->arg_begin();
  677. for (auto End = F->arg_end(); NewArg != End; ++NewArg, ++OldArg) {
  678. LLVM_DEBUG(dbgs() << "SCCP: Marking argument "
  679. << NewArg->getNameOrAsOperand() << "\n");
  680. if (Iter != Args.end() && OldArg == Iter->Formal) {
  681. // Mark the argument constants in the new function.
  682. markConstant(NewArg, Iter->Actual);
  683. ++Iter;
  684. } else if (ValueState.count(OldArg)) {
  685. // For the remaining arguments in the new function, copy the lattice state
  686. // over from the old function.
  687. //
  688. // Note: This previously looked like this:
  689. // ValueState[NewArg] = ValueState[OldArg];
  690. // This is incorrect because the DenseMap class may resize the underlying
  691. // memory when inserting `NewArg`, which will invalidate the reference to
  692. // `OldArg`. Instead, we make sure `NewArg` exists before setting it.
  693. auto &NewValue = ValueState[NewArg];
  694. NewValue = ValueState[OldArg];
  695. pushToWorkList(NewValue, NewArg);
  696. }
  697. }
  698. }
  699. void SCCPInstVisitor::visitInstruction(Instruction &I) {
  700. // All the instructions we don't do any special handling for just
  701. // go to overdefined.
  702. LLVM_DEBUG(dbgs() << "SCCP: Don't know how to handle: " << I << '\n');
  703. markOverdefined(&I);
  704. }
  705. bool SCCPInstVisitor::mergeInValue(ValueLatticeElement &IV, Value *V,
  706. ValueLatticeElement MergeWithV,
  707. ValueLatticeElement::MergeOptions Opts) {
  708. if (IV.mergeIn(MergeWithV, Opts)) {
  709. pushToWorkList(IV, V);
  710. LLVM_DEBUG(dbgs() << "Merged " << MergeWithV << " into " << *V << " : "
  711. << IV << "\n");
  712. return true;
  713. }
  714. return false;
  715. }
  716. bool SCCPInstVisitor::markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
  717. if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
  718. return false; // This edge is already known to be executable!
  719. if (!markBlockExecutable(Dest)) {
  720. // If the destination is already executable, we just made an *edge*
  721. // feasible that wasn't before. Revisit the PHI nodes in the block
  722. // because they have potentially new operands.
  723. LLVM_DEBUG(dbgs() << "Marking Edge Executable: " << Source->getName()
  724. << " -> " << Dest->getName() << '\n');
  725. for (PHINode &PN : Dest->phis())
  726. visitPHINode(PN);
  727. }
  728. return true;
  729. }
  730. // getFeasibleSuccessors - Return a vector of booleans to indicate which
  731. // successors are reachable from a given terminator instruction.
  732. void SCCPInstVisitor::getFeasibleSuccessors(Instruction &TI,
  733. SmallVectorImpl<bool> &Succs) {
  734. Succs.resize(TI.getNumSuccessors());
  735. if (auto *BI = dyn_cast<BranchInst>(&TI)) {
  736. if (BI->isUnconditional()) {
  737. Succs[0] = true;
  738. return;
  739. }
  740. ValueLatticeElement BCValue = getValueState(BI->getCondition());
  741. ConstantInt *CI = getConstantInt(BCValue);
  742. if (!CI) {
  743. // Overdefined condition variables, and branches on unfoldable constant
  744. // conditions, mean the branch could go either way.
  745. if (!BCValue.isUnknownOrUndef())
  746. Succs[0] = Succs[1] = true;
  747. return;
  748. }
  749. // Constant condition variables mean the branch can only go a single way.
  750. Succs[CI->isZero()] = true;
  751. return;
  752. }
  753. // Unwinding instructions successors are always executable.
  754. if (TI.isExceptionalTerminator()) {
  755. Succs.assign(TI.getNumSuccessors(), true);
  756. return;
  757. }
  758. if (auto *SI = dyn_cast<SwitchInst>(&TI)) {
  759. if (!SI->getNumCases()) {
  760. Succs[0] = true;
  761. return;
  762. }
  763. const ValueLatticeElement &SCValue = getValueState(SI->getCondition());
  764. if (ConstantInt *CI = getConstantInt(SCValue)) {
  765. Succs[SI->findCaseValue(CI)->getSuccessorIndex()] = true;
  766. return;
  767. }
  768. // TODO: Switch on undef is UB. Stop passing false once the rest of LLVM
  769. // is ready.
  770. if (SCValue.isConstantRange(/*UndefAllowed=*/false)) {
  771. const ConstantRange &Range = SCValue.getConstantRange();
  772. for (const auto &Case : SI->cases()) {
  773. const APInt &CaseValue = Case.getCaseValue()->getValue();
  774. if (Range.contains(CaseValue))
  775. Succs[Case.getSuccessorIndex()] = true;
  776. }
  777. // TODO: Determine whether default case is reachable.
  778. Succs[SI->case_default()->getSuccessorIndex()] = true;
  779. return;
  780. }
  781. // Overdefined or unknown condition? All destinations are executable!
  782. if (!SCValue.isUnknownOrUndef())
  783. Succs.assign(TI.getNumSuccessors(), true);
  784. return;
  785. }
  786. // In case of indirect branch and its address is a blockaddress, we mark
  787. // the target as executable.
  788. if (auto *IBR = dyn_cast<IndirectBrInst>(&TI)) {
  789. // Casts are folded by visitCastInst.
  790. ValueLatticeElement IBRValue = getValueState(IBR->getAddress());
  791. BlockAddress *Addr = dyn_cast_or_null<BlockAddress>(getConstant(IBRValue));
  792. if (!Addr) { // Overdefined or unknown condition?
  793. // All destinations are executable!
  794. if (!IBRValue.isUnknownOrUndef())
  795. Succs.assign(TI.getNumSuccessors(), true);
  796. return;
  797. }
  798. BasicBlock *T = Addr->getBasicBlock();
  799. assert(Addr->getFunction() == T->getParent() &&
  800. "Block address of a different function ?");
  801. for (unsigned i = 0; i < IBR->getNumSuccessors(); ++i) {
  802. // This is the target.
  803. if (IBR->getDestination(i) == T) {
  804. Succs[i] = true;
  805. return;
  806. }
  807. }
  808. // If we didn't find our destination in the IBR successor list, then we
  809. // have undefined behavior. Its ok to assume no successor is executable.
  810. return;
  811. }
  812. // In case of callbr, we pessimistically assume that all successors are
  813. // feasible.
  814. if (isa<CallBrInst>(&TI)) {
  815. Succs.assign(TI.getNumSuccessors(), true);
  816. return;
  817. }
  818. LLVM_DEBUG(dbgs() << "Unknown terminator instruction: " << TI << '\n');
  819. llvm_unreachable("SCCP: Don't know how to handle this terminator!");
  820. }
  821. // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
  822. // block to the 'To' basic block is currently feasible.
  823. bool SCCPInstVisitor::isEdgeFeasible(BasicBlock *From, BasicBlock *To) const {
  824. // Check if we've called markEdgeExecutable on the edge yet. (We could
  825. // be more aggressive and try to consider edges which haven't been marked
  826. // yet, but there isn't any need.)
  827. return KnownFeasibleEdges.count(Edge(From, To));
  828. }
  829. // visit Implementations - Something changed in this instruction, either an
  830. // operand made a transition, or the instruction is newly executable. Change
  831. // the value type of I to reflect these changes if appropriate. This method
  832. // makes sure to do the following actions:
  833. //
  834. // 1. If a phi node merges two constants in, and has conflicting value coming
  835. // from different branches, or if the PHI node merges in an overdefined
  836. // value, then the PHI node becomes overdefined.
  837. // 2. If a phi node merges only constants in, and they all agree on value, the
  838. // PHI node becomes a constant value equal to that.
  839. // 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
  840. // 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
  841. // 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
  842. // 6. If a conditional branch has a value that is constant, make the selected
  843. // destination executable
  844. // 7. If a conditional branch has a value that is overdefined, make all
  845. // successors executable.
  846. void SCCPInstVisitor::visitPHINode(PHINode &PN) {
  847. // If this PN returns a struct, just mark the result overdefined.
  848. // TODO: We could do a lot better than this if code actually uses this.
  849. if (PN.getType()->isStructTy())
  850. return (void)markOverdefined(&PN);
  851. if (getValueState(&PN).isOverdefined())
  852. return; // Quick exit
  853. // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant,
  854. // and slow us down a lot. Just mark them overdefined.
  855. if (PN.getNumIncomingValues() > 64)
  856. return (void)markOverdefined(&PN);
  857. unsigned NumActiveIncoming = 0;
  858. // Look at all of the executable operands of the PHI node. If any of them
  859. // are overdefined, the PHI becomes overdefined as well. If they are all
  860. // constant, and they agree with each other, the PHI becomes the identical
  861. // constant. If they are constant and don't agree, the PHI is a constant
  862. // range. If there are no executable operands, the PHI remains unknown.
  863. ValueLatticeElement PhiState = getValueState(&PN);
  864. for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
  865. if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent()))
  866. continue;
  867. ValueLatticeElement IV = getValueState(PN.getIncomingValue(i));
  868. PhiState.mergeIn(IV);
  869. NumActiveIncoming++;
  870. if (PhiState.isOverdefined())
  871. break;
  872. }
  873. // We allow up to 1 range extension per active incoming value and one
  874. // additional extension. Note that we manually adjust the number of range
  875. // extensions to match the number of active incoming values. This helps to
  876. // limit multiple extensions caused by the same incoming value, if other
  877. // incoming values are equal.
  878. mergeInValue(&PN, PhiState,
  879. ValueLatticeElement::MergeOptions().setMaxWidenSteps(
  880. NumActiveIncoming + 1));
  881. ValueLatticeElement &PhiStateRef = getValueState(&PN);
  882. PhiStateRef.setNumRangeExtensions(
  883. std::max(NumActiveIncoming, PhiStateRef.getNumRangeExtensions()));
  884. }
  885. void SCCPInstVisitor::visitReturnInst(ReturnInst &I) {
  886. if (I.getNumOperands() == 0)
  887. return; // ret void
  888. Function *F = I.getParent()->getParent();
  889. Value *ResultOp = I.getOperand(0);
  890. // If we are tracking the return value of this function, merge it in.
  891. if (!TrackedRetVals.empty() && !ResultOp->getType()->isStructTy()) {
  892. auto TFRVI = TrackedRetVals.find(F);
  893. if (TFRVI != TrackedRetVals.end()) {
  894. mergeInValue(TFRVI->second, F, getValueState(ResultOp));
  895. return;
  896. }
  897. }
  898. // Handle functions that return multiple values.
  899. if (!TrackedMultipleRetVals.empty()) {
  900. if (auto *STy = dyn_cast<StructType>(ResultOp->getType()))
  901. if (MRVFunctionsTracked.count(F))
  902. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
  903. mergeInValue(TrackedMultipleRetVals[std::make_pair(F, i)], F,
  904. getStructValueState(ResultOp, i));
  905. }
  906. }
  907. void SCCPInstVisitor::visitTerminator(Instruction &TI) {
  908. SmallVector<bool, 16> SuccFeasible;
  909. getFeasibleSuccessors(TI, SuccFeasible);
  910. BasicBlock *BB = TI.getParent();
  911. // Mark all feasible successors executable.
  912. for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
  913. if (SuccFeasible[i])
  914. markEdgeExecutable(BB, TI.getSuccessor(i));
  915. }
  916. void SCCPInstVisitor::visitCastInst(CastInst &I) {
  917. // ResolvedUndefsIn might mark I as overdefined. Bail out, even if we would
  918. // discover a concrete value later.
  919. if (ValueState[&I].isOverdefined())
  920. return;
  921. ValueLatticeElement OpSt = getValueState(I.getOperand(0));
  922. if (OpSt.isUnknownOrUndef())
  923. return;
  924. if (Constant *OpC = getConstant(OpSt)) {
  925. // Fold the constant as we build.
  926. Constant *C = ConstantFoldCastOperand(I.getOpcode(), OpC, I.getType(), DL);
  927. markConstant(&I, C);
  928. } else if (I.getDestTy()->isIntegerTy() &&
  929. I.getSrcTy()->isIntOrIntVectorTy()) {
  930. auto &LV = getValueState(&I);
  931. ConstantRange OpRange = getConstantRange(OpSt, I.getSrcTy());
  932. Type *DestTy = I.getDestTy();
  933. // Vectors where all elements have the same known constant range are treated
  934. // as a single constant range in the lattice. When bitcasting such vectors,
  935. // there is a mis-match between the width of the lattice value (single
  936. // constant range) and the original operands (vector). Go to overdefined in
  937. // that case.
  938. if (I.getOpcode() == Instruction::BitCast &&
  939. I.getOperand(0)->getType()->isVectorTy() &&
  940. OpRange.getBitWidth() < DL.getTypeSizeInBits(DestTy))
  941. return (void)markOverdefined(&I);
  942. ConstantRange Res =
  943. OpRange.castOp(I.getOpcode(), DL.getTypeSizeInBits(DestTy));
  944. mergeInValue(LV, &I, ValueLatticeElement::getRange(Res));
  945. } else
  946. markOverdefined(&I);
  947. }
  948. void SCCPInstVisitor::handleExtractOfWithOverflow(ExtractValueInst &EVI,
  949. const WithOverflowInst *WO,
  950. unsigned Idx) {
  951. Value *LHS = WO->getLHS(), *RHS = WO->getRHS();
  952. ValueLatticeElement L = getValueState(LHS);
  953. ValueLatticeElement R = getValueState(RHS);
  954. addAdditionalUser(LHS, &EVI);
  955. addAdditionalUser(RHS, &EVI);
  956. if (L.isUnknownOrUndef() || R.isUnknownOrUndef())
  957. return; // Wait to resolve.
  958. Type *Ty = LHS->getType();
  959. ConstantRange LR = getConstantRange(L, Ty);
  960. ConstantRange RR = getConstantRange(R, Ty);
  961. if (Idx == 0) {
  962. ConstantRange Res = LR.binaryOp(WO->getBinaryOp(), RR);
  963. mergeInValue(&EVI, ValueLatticeElement::getRange(Res));
  964. } else {
  965. assert(Idx == 1 && "Index can only be 0 or 1");
  966. ConstantRange NWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
  967. WO->getBinaryOp(), RR, WO->getNoWrapKind());
  968. if (NWRegion.contains(LR))
  969. return (void)markConstant(&EVI, ConstantInt::getFalse(EVI.getType()));
  970. markOverdefined(&EVI);
  971. }
  972. }
  973. void SCCPInstVisitor::visitExtractValueInst(ExtractValueInst &EVI) {
  974. // If this returns a struct, mark all elements over defined, we don't track
  975. // structs in structs.
  976. if (EVI.getType()->isStructTy())
  977. return (void)markOverdefined(&EVI);
  978. // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
  979. // discover a concrete value later.
  980. if (ValueState[&EVI].isOverdefined())
  981. return (void)markOverdefined(&EVI);
  982. // If this is extracting from more than one level of struct, we don't know.
  983. if (EVI.getNumIndices() != 1)
  984. return (void)markOverdefined(&EVI);
  985. Value *AggVal = EVI.getAggregateOperand();
  986. if (AggVal->getType()->isStructTy()) {
  987. unsigned i = *EVI.idx_begin();
  988. if (auto *WO = dyn_cast<WithOverflowInst>(AggVal))
  989. return handleExtractOfWithOverflow(EVI, WO, i);
  990. ValueLatticeElement EltVal = getStructValueState(AggVal, i);
  991. mergeInValue(getValueState(&EVI), &EVI, EltVal);
  992. } else {
  993. // Otherwise, must be extracting from an array.
  994. return (void)markOverdefined(&EVI);
  995. }
  996. }
  997. void SCCPInstVisitor::visitInsertValueInst(InsertValueInst &IVI) {
  998. auto *STy = dyn_cast<StructType>(IVI.getType());
  999. if (!STy)
  1000. return (void)markOverdefined(&IVI);
  1001. // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
  1002. // discover a concrete value later.
  1003. if (SCCPSolver::isOverdefined(ValueState[&IVI]))
  1004. return (void)markOverdefined(&IVI);
  1005. // If this has more than one index, we can't handle it, drive all results to
  1006. // undef.
  1007. if (IVI.getNumIndices() != 1)
  1008. return (void)markOverdefined(&IVI);
  1009. Value *Aggr = IVI.getAggregateOperand();
  1010. unsigned Idx = *IVI.idx_begin();
  1011. // Compute the result based on what we're inserting.
  1012. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
  1013. // This passes through all values that aren't the inserted element.
  1014. if (i != Idx) {
  1015. ValueLatticeElement EltVal = getStructValueState(Aggr, i);
  1016. mergeInValue(getStructValueState(&IVI, i), &IVI, EltVal);
  1017. continue;
  1018. }
  1019. Value *Val = IVI.getInsertedValueOperand();
  1020. if (Val->getType()->isStructTy())
  1021. // We don't track structs in structs.
  1022. markOverdefined(getStructValueState(&IVI, i), &IVI);
  1023. else {
  1024. ValueLatticeElement InVal = getValueState(Val);
  1025. mergeInValue(getStructValueState(&IVI, i), &IVI, InVal);
  1026. }
  1027. }
  1028. }
  1029. void SCCPInstVisitor::visitSelectInst(SelectInst &I) {
  1030. // If this select returns a struct, just mark the result overdefined.
  1031. // TODO: We could do a lot better than this if code actually uses this.
  1032. if (I.getType()->isStructTy())
  1033. return (void)markOverdefined(&I);
  1034. // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
  1035. // discover a concrete value later.
  1036. if (ValueState[&I].isOverdefined())
  1037. return (void)markOverdefined(&I);
  1038. ValueLatticeElement CondValue = getValueState(I.getCondition());
  1039. if (CondValue.isUnknownOrUndef())
  1040. return;
  1041. if (ConstantInt *CondCB = getConstantInt(CondValue)) {
  1042. Value *OpVal = CondCB->isZero() ? I.getFalseValue() : I.getTrueValue();
  1043. mergeInValue(&I, getValueState(OpVal));
  1044. return;
  1045. }
  1046. // Otherwise, the condition is overdefined or a constant we can't evaluate.
  1047. // See if we can produce something better than overdefined based on the T/F
  1048. // value.
  1049. ValueLatticeElement TVal = getValueState(I.getTrueValue());
  1050. ValueLatticeElement FVal = getValueState(I.getFalseValue());
  1051. bool Changed = ValueState[&I].mergeIn(TVal);
  1052. Changed |= ValueState[&I].mergeIn(FVal);
  1053. if (Changed)
  1054. pushToWorkListMsg(ValueState[&I], &I);
  1055. }
  1056. // Handle Unary Operators.
  1057. void SCCPInstVisitor::visitUnaryOperator(Instruction &I) {
  1058. ValueLatticeElement V0State = getValueState(I.getOperand(0));
  1059. ValueLatticeElement &IV = ValueState[&I];
  1060. // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
  1061. // discover a concrete value later.
  1062. if (SCCPSolver::isOverdefined(IV))
  1063. return (void)markOverdefined(&I);
  1064. // If something is unknown/undef, wait for it to resolve.
  1065. if (V0State.isUnknownOrUndef())
  1066. return;
  1067. if (SCCPSolver::isConstant(V0State))
  1068. if (Constant *C = ConstantFoldUnaryOpOperand(I.getOpcode(),
  1069. getConstant(V0State), DL))
  1070. return (void)markConstant(IV, &I, C);
  1071. markOverdefined(&I);
  1072. }
  1073. // Handle Binary Operators.
  1074. void SCCPInstVisitor::visitBinaryOperator(Instruction &I) {
  1075. ValueLatticeElement V1State = getValueState(I.getOperand(0));
  1076. ValueLatticeElement V2State = getValueState(I.getOperand(1));
  1077. ValueLatticeElement &IV = ValueState[&I];
  1078. if (IV.isOverdefined())
  1079. return;
  1080. // If something is undef, wait for it to resolve.
  1081. if (V1State.isUnknownOrUndef() || V2State.isUnknownOrUndef())
  1082. return;
  1083. if (V1State.isOverdefined() && V2State.isOverdefined())
  1084. return (void)markOverdefined(&I);
  1085. // If either of the operands is a constant, try to fold it to a constant.
  1086. // TODO: Use information from notconstant better.
  1087. if ((V1State.isConstant() || V2State.isConstant())) {
  1088. Value *V1 = SCCPSolver::isConstant(V1State) ? getConstant(V1State)
  1089. : I.getOperand(0);
  1090. Value *V2 = SCCPSolver::isConstant(V2State) ? getConstant(V2State)
  1091. : I.getOperand(1);
  1092. Value *R = simplifyBinOp(I.getOpcode(), V1, V2, SimplifyQuery(DL));
  1093. auto *C = dyn_cast_or_null<Constant>(R);
  1094. if (C) {
  1095. // Conservatively assume that the result may be based on operands that may
  1096. // be undef. Note that we use mergeInValue to combine the constant with
  1097. // the existing lattice value for I, as different constants might be found
  1098. // after one of the operands go to overdefined, e.g. due to one operand
  1099. // being a special floating value.
  1100. ValueLatticeElement NewV;
  1101. NewV.markConstant(C, /*MayIncludeUndef=*/true);
  1102. return (void)mergeInValue(&I, NewV);
  1103. }
  1104. }
  1105. // Only use ranges for binary operators on integers.
  1106. if (!I.getType()->isIntegerTy())
  1107. return markOverdefined(&I);
  1108. // Try to simplify to a constant range.
  1109. ConstantRange A = getConstantRange(V1State, I.getType());
  1110. ConstantRange B = getConstantRange(V2State, I.getType());
  1111. ConstantRange R = A.binaryOp(cast<BinaryOperator>(&I)->getOpcode(), B);
  1112. mergeInValue(&I, ValueLatticeElement::getRange(R));
  1113. // TODO: Currently we do not exploit special values that produce something
  1114. // better than overdefined with an overdefined operand for vector or floating
  1115. // point types, like and <4 x i32> overdefined, zeroinitializer.
  1116. }
  1117. // Handle ICmpInst instruction.
  1118. void SCCPInstVisitor::visitCmpInst(CmpInst &I) {
  1119. // Do not cache this lookup, getValueState calls later in the function might
  1120. // invalidate the reference.
  1121. if (SCCPSolver::isOverdefined(ValueState[&I]))
  1122. return (void)markOverdefined(&I);
  1123. Value *Op1 = I.getOperand(0);
  1124. Value *Op2 = I.getOperand(1);
  1125. // For parameters, use ParamState which includes constant range info if
  1126. // available.
  1127. auto V1State = getValueState(Op1);
  1128. auto V2State = getValueState(Op2);
  1129. Constant *C = V1State.getCompare(I.getPredicate(), I.getType(), V2State, DL);
  1130. if (C) {
  1131. ValueLatticeElement CV;
  1132. CV.markConstant(C);
  1133. mergeInValue(&I, CV);
  1134. return;
  1135. }
  1136. // If operands are still unknown, wait for it to resolve.
  1137. if ((V1State.isUnknownOrUndef() || V2State.isUnknownOrUndef()) &&
  1138. !SCCPSolver::isConstant(ValueState[&I]))
  1139. return;
  1140. markOverdefined(&I);
  1141. }
  1142. // Handle getelementptr instructions. If all operands are constants then we
  1143. // can turn this into a getelementptr ConstantExpr.
  1144. void SCCPInstVisitor::visitGetElementPtrInst(GetElementPtrInst &I) {
  1145. if (SCCPSolver::isOverdefined(ValueState[&I]))
  1146. return (void)markOverdefined(&I);
  1147. SmallVector<Constant *, 8> Operands;
  1148. Operands.reserve(I.getNumOperands());
  1149. for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
  1150. ValueLatticeElement State = getValueState(I.getOperand(i));
  1151. if (State.isUnknownOrUndef())
  1152. return; // Operands are not resolved yet.
  1153. if (SCCPSolver::isOverdefined(State))
  1154. return (void)markOverdefined(&I);
  1155. if (Constant *C = getConstant(State)) {
  1156. Operands.push_back(C);
  1157. continue;
  1158. }
  1159. return (void)markOverdefined(&I);
  1160. }
  1161. Constant *Ptr = Operands[0];
  1162. auto Indices = ArrayRef(Operands.begin() + 1, Operands.end());
  1163. Constant *C =
  1164. ConstantExpr::getGetElementPtr(I.getSourceElementType(), Ptr, Indices);
  1165. markConstant(&I, C);
  1166. }
  1167. void SCCPInstVisitor::visitStoreInst(StoreInst &SI) {
  1168. // If this store is of a struct, ignore it.
  1169. if (SI.getOperand(0)->getType()->isStructTy())
  1170. return;
  1171. if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1)))
  1172. return;
  1173. GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1));
  1174. auto I = TrackedGlobals.find(GV);
  1175. if (I == TrackedGlobals.end())
  1176. return;
  1177. // Get the value we are storing into the global, then merge it.
  1178. mergeInValue(I->second, GV, getValueState(SI.getOperand(0)),
  1179. ValueLatticeElement::MergeOptions().setCheckWiden(false));
  1180. if (I->second.isOverdefined())
  1181. TrackedGlobals.erase(I); // No need to keep tracking this!
  1182. }
  1183. static ValueLatticeElement getValueFromMetadata(const Instruction *I) {
  1184. if (MDNode *Ranges = I->getMetadata(LLVMContext::MD_range))
  1185. if (I->getType()->isIntegerTy())
  1186. return ValueLatticeElement::getRange(
  1187. getConstantRangeFromMetadata(*Ranges));
  1188. if (I->hasMetadata(LLVMContext::MD_nonnull))
  1189. return ValueLatticeElement::getNot(
  1190. ConstantPointerNull::get(cast<PointerType>(I->getType())));
  1191. return ValueLatticeElement::getOverdefined();
  1192. }
  1193. // Handle load instructions. If the operand is a constant pointer to a constant
  1194. // global, we can replace the load with the loaded constant value!
  1195. void SCCPInstVisitor::visitLoadInst(LoadInst &I) {
  1196. // If this load is of a struct or the load is volatile, just mark the result
  1197. // as overdefined.
  1198. if (I.getType()->isStructTy() || I.isVolatile())
  1199. return (void)markOverdefined(&I);
  1200. // resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
  1201. // discover a concrete value later.
  1202. if (ValueState[&I].isOverdefined())
  1203. return (void)markOverdefined(&I);
  1204. ValueLatticeElement PtrVal = getValueState(I.getOperand(0));
  1205. if (PtrVal.isUnknownOrUndef())
  1206. return; // The pointer is not resolved yet!
  1207. ValueLatticeElement &IV = ValueState[&I];
  1208. if (SCCPSolver::isConstant(PtrVal)) {
  1209. Constant *Ptr = getConstant(PtrVal);
  1210. // load null is undefined.
  1211. if (isa<ConstantPointerNull>(Ptr)) {
  1212. if (NullPointerIsDefined(I.getFunction(), I.getPointerAddressSpace()))
  1213. return (void)markOverdefined(IV, &I);
  1214. else
  1215. return;
  1216. }
  1217. // Transform load (constant global) into the value loaded.
  1218. if (auto *GV = dyn_cast<GlobalVariable>(Ptr)) {
  1219. if (!TrackedGlobals.empty()) {
  1220. // If we are tracking this global, merge in the known value for it.
  1221. auto It = TrackedGlobals.find(GV);
  1222. if (It != TrackedGlobals.end()) {
  1223. mergeInValue(IV, &I, It->second, getMaxWidenStepsOpts());
  1224. return;
  1225. }
  1226. }
  1227. }
  1228. // Transform load from a constant into a constant if possible.
  1229. if (Constant *C = ConstantFoldLoadFromConstPtr(Ptr, I.getType(), DL))
  1230. return (void)markConstant(IV, &I, C);
  1231. }
  1232. // Fall back to metadata.
  1233. mergeInValue(&I, getValueFromMetadata(&I));
  1234. }
  1235. void SCCPInstVisitor::visitCallBase(CallBase &CB) {
  1236. handleCallResult(CB);
  1237. handleCallArguments(CB);
  1238. }
  1239. void SCCPInstVisitor::handleCallOverdefined(CallBase &CB) {
  1240. Function *F = CB.getCalledFunction();
  1241. // Void return and not tracking callee, just bail.
  1242. if (CB.getType()->isVoidTy())
  1243. return;
  1244. // Always mark struct return as overdefined.
  1245. if (CB.getType()->isStructTy())
  1246. return (void)markOverdefined(&CB);
  1247. // Otherwise, if we have a single return value case, and if the function is
  1248. // a declaration, maybe we can constant fold it.
  1249. if (F && F->isDeclaration() && canConstantFoldCallTo(&CB, F)) {
  1250. SmallVector<Constant *, 8> Operands;
  1251. for (const Use &A : CB.args()) {
  1252. if (A.get()->getType()->isStructTy())
  1253. return markOverdefined(&CB); // Can't handle struct args.
  1254. if (A.get()->getType()->isMetadataTy())
  1255. continue; // Carried in CB, not allowed in Operands.
  1256. ValueLatticeElement State = getValueState(A);
  1257. if (State.isUnknownOrUndef())
  1258. return; // Operands are not resolved yet.
  1259. if (SCCPSolver::isOverdefined(State))
  1260. return (void)markOverdefined(&CB);
  1261. assert(SCCPSolver::isConstant(State) && "Unknown state!");
  1262. Operands.push_back(getConstant(State));
  1263. }
  1264. if (SCCPSolver::isOverdefined(getValueState(&CB)))
  1265. return (void)markOverdefined(&CB);
  1266. // If we can constant fold this, mark the result of the call as a
  1267. // constant.
  1268. if (Constant *C = ConstantFoldCall(&CB, F, Operands, &GetTLI(*F)))
  1269. return (void)markConstant(&CB, C);
  1270. }
  1271. // Fall back to metadata.
  1272. mergeInValue(&CB, getValueFromMetadata(&CB));
  1273. }
  1274. void SCCPInstVisitor::handleCallArguments(CallBase &CB) {
  1275. Function *F = CB.getCalledFunction();
  1276. // If this is a local function that doesn't have its address taken, mark its
  1277. // entry block executable and merge in the actual arguments to the call into
  1278. // the formal arguments of the function.
  1279. if (TrackingIncomingArguments.count(F)) {
  1280. markBlockExecutable(&F->front());
  1281. // Propagate information from this call site into the callee.
  1282. auto CAI = CB.arg_begin();
  1283. for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E;
  1284. ++AI, ++CAI) {
  1285. // If this argument is byval, and if the function is not readonly, there
  1286. // will be an implicit copy formed of the input aggregate.
  1287. if (AI->hasByValAttr() && !F->onlyReadsMemory()) {
  1288. markOverdefined(&*AI);
  1289. continue;
  1290. }
  1291. if (auto *STy = dyn_cast<StructType>(AI->getType())) {
  1292. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
  1293. ValueLatticeElement CallArg = getStructValueState(*CAI, i);
  1294. mergeInValue(getStructValueState(&*AI, i), &*AI, CallArg,
  1295. getMaxWidenStepsOpts());
  1296. }
  1297. } else
  1298. mergeInValue(&*AI, getValueState(*CAI), getMaxWidenStepsOpts());
  1299. }
  1300. }
  1301. }
  1302. void SCCPInstVisitor::handleCallResult(CallBase &CB) {
  1303. Function *F = CB.getCalledFunction();
  1304. if (auto *II = dyn_cast<IntrinsicInst>(&CB)) {
  1305. if (II->getIntrinsicID() == Intrinsic::ssa_copy) {
  1306. if (ValueState[&CB].isOverdefined())
  1307. return;
  1308. Value *CopyOf = CB.getOperand(0);
  1309. ValueLatticeElement CopyOfVal = getValueState(CopyOf);
  1310. const auto *PI = getPredicateInfoFor(&CB);
  1311. assert(PI && "Missing predicate info for ssa.copy");
  1312. const std::optional<PredicateConstraint> &Constraint =
  1313. PI->getConstraint();
  1314. if (!Constraint) {
  1315. mergeInValue(ValueState[&CB], &CB, CopyOfVal);
  1316. return;
  1317. }
  1318. CmpInst::Predicate Pred = Constraint->Predicate;
  1319. Value *OtherOp = Constraint->OtherOp;
  1320. // Wait until OtherOp is resolved.
  1321. if (getValueState(OtherOp).isUnknown()) {
  1322. addAdditionalUser(OtherOp, &CB);
  1323. return;
  1324. }
  1325. ValueLatticeElement CondVal = getValueState(OtherOp);
  1326. ValueLatticeElement &IV = ValueState[&CB];
  1327. if (CondVal.isConstantRange() || CopyOfVal.isConstantRange()) {
  1328. auto ImposedCR =
  1329. ConstantRange::getFull(DL.getTypeSizeInBits(CopyOf->getType()));
  1330. // Get the range imposed by the condition.
  1331. if (CondVal.isConstantRange())
  1332. ImposedCR = ConstantRange::makeAllowedICmpRegion(
  1333. Pred, CondVal.getConstantRange());
  1334. // Combine range info for the original value with the new range from the
  1335. // condition.
  1336. auto CopyOfCR = getConstantRange(CopyOfVal, CopyOf->getType());
  1337. auto NewCR = ImposedCR.intersectWith(CopyOfCR);
  1338. // If the existing information is != x, do not use the information from
  1339. // a chained predicate, as the != x information is more likely to be
  1340. // helpful in practice.
  1341. if (!CopyOfCR.contains(NewCR) && CopyOfCR.getSingleMissingElement())
  1342. NewCR = CopyOfCR;
  1343. // The new range is based on a branch condition. That guarantees that
  1344. // neither of the compare operands can be undef in the branch targets,
  1345. // unless we have conditions that are always true/false (e.g. icmp ule
  1346. // i32, %a, i32_max). For the latter overdefined/empty range will be
  1347. // inferred, but the branch will get folded accordingly anyways.
  1348. addAdditionalUser(OtherOp, &CB);
  1349. mergeInValue(
  1350. IV, &CB,
  1351. ValueLatticeElement::getRange(NewCR, /*MayIncludeUndef*/ false));
  1352. return;
  1353. } else if (Pred == CmpInst::ICMP_EQ &&
  1354. (CondVal.isConstant() || CondVal.isNotConstant())) {
  1355. // For non-integer values or integer constant expressions, only
  1356. // propagate equal constants or not-constants.
  1357. addAdditionalUser(OtherOp, &CB);
  1358. mergeInValue(IV, &CB, CondVal);
  1359. return;
  1360. } else if (Pred == CmpInst::ICMP_NE && CondVal.isConstant()) {
  1361. // Propagate inequalities.
  1362. addAdditionalUser(OtherOp, &CB);
  1363. mergeInValue(IV, &CB,
  1364. ValueLatticeElement::getNot(CondVal.getConstant()));
  1365. return;
  1366. }
  1367. return (void)mergeInValue(IV, &CB, CopyOfVal);
  1368. }
  1369. if (ConstantRange::isIntrinsicSupported(II->getIntrinsicID())) {
  1370. // Compute result range for intrinsics supported by ConstantRange.
  1371. // Do this even if we don't know a range for all operands, as we may
  1372. // still know something about the result range, e.g. of abs(x).
  1373. SmallVector<ConstantRange, 2> OpRanges;
  1374. for (Value *Op : II->args()) {
  1375. const ValueLatticeElement &State = getValueState(Op);
  1376. OpRanges.push_back(getConstantRange(State, Op->getType()));
  1377. }
  1378. ConstantRange Result =
  1379. ConstantRange::intrinsic(II->getIntrinsicID(), OpRanges);
  1380. return (void)mergeInValue(II, ValueLatticeElement::getRange(Result));
  1381. }
  1382. }
  1383. // The common case is that we aren't tracking the callee, either because we
  1384. // are not doing interprocedural analysis or the callee is indirect, or is
  1385. // external. Handle these cases first.
  1386. if (!F || F->isDeclaration())
  1387. return handleCallOverdefined(CB);
  1388. // If this is a single/zero retval case, see if we're tracking the function.
  1389. if (auto *STy = dyn_cast<StructType>(F->getReturnType())) {
  1390. if (!MRVFunctionsTracked.count(F))
  1391. return handleCallOverdefined(CB); // Not tracking this callee.
  1392. // If we are tracking this callee, propagate the result of the function
  1393. // into this call site.
  1394. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
  1395. mergeInValue(getStructValueState(&CB, i), &CB,
  1396. TrackedMultipleRetVals[std::make_pair(F, i)],
  1397. getMaxWidenStepsOpts());
  1398. } else {
  1399. auto TFRVI = TrackedRetVals.find(F);
  1400. if (TFRVI == TrackedRetVals.end())
  1401. return handleCallOverdefined(CB); // Not tracking this callee.
  1402. // If so, propagate the return value of the callee into this call result.
  1403. mergeInValue(&CB, TFRVI->second, getMaxWidenStepsOpts());
  1404. }
  1405. }
  1406. void SCCPInstVisitor::solve() {
  1407. // Process the work lists until they are empty!
  1408. while (!BBWorkList.empty() || !InstWorkList.empty() ||
  1409. !OverdefinedInstWorkList.empty()) {
  1410. // Process the overdefined instruction's work list first, which drives other
  1411. // things to overdefined more quickly.
  1412. while (!OverdefinedInstWorkList.empty()) {
  1413. Value *I = OverdefinedInstWorkList.pop_back_val();
  1414. LLVM_DEBUG(dbgs() << "\nPopped off OI-WL: " << *I << '\n');
  1415. // "I" got into the work list because it either made the transition from
  1416. // bottom to constant, or to overdefined.
  1417. //
  1418. // Anything on this worklist that is overdefined need not be visited
  1419. // since all of its users will have already been marked as overdefined
  1420. // Update all of the users of this instruction's value.
  1421. //
  1422. markUsersAsChanged(I);
  1423. }
  1424. // Process the instruction work list.
  1425. while (!InstWorkList.empty()) {
  1426. Value *I = InstWorkList.pop_back_val();
  1427. LLVM_DEBUG(dbgs() << "\nPopped off I-WL: " << *I << '\n');
  1428. // "I" got into the work list because it made the transition from undef to
  1429. // constant.
  1430. //
  1431. // Anything on this worklist that is overdefined need not be visited
  1432. // since all of its users will have already been marked as overdefined.
  1433. // Update all of the users of this instruction's value.
  1434. //
  1435. if (I->getType()->isStructTy() || !getValueState(I).isOverdefined())
  1436. markUsersAsChanged(I);
  1437. }
  1438. // Process the basic block work list.
  1439. while (!BBWorkList.empty()) {
  1440. BasicBlock *BB = BBWorkList.pop_back_val();
  1441. LLVM_DEBUG(dbgs() << "\nPopped off BBWL: " << *BB << '\n');
  1442. // Notify all instructions in this basic block that they are newly
  1443. // executable.
  1444. visit(BB);
  1445. }
  1446. }
  1447. }
  1448. /// While solving the dataflow for a function, we don't compute a result for
  1449. /// operations with an undef operand, to allow undef to be lowered to a
  1450. /// constant later. For example, constant folding of "zext i8 undef to i16"
  1451. /// would result in "i16 0", and if undef is later lowered to "i8 1", then the
  1452. /// zext result would become "i16 1" and would result into an overdefined
  1453. /// lattice value once merged with the previous result. Not computing the
  1454. /// result of the zext (treating undef the same as unknown) allows us to handle
  1455. /// a later undef->constant lowering more optimally.
  1456. ///
  1457. /// However, if the operand remains undef when the solver returns, we do need
  1458. /// to assign some result to the instruction (otherwise we would treat it as
  1459. /// unreachable). For simplicity, we mark any instructions that are still
  1460. /// unknown as overdefined.
  1461. bool SCCPInstVisitor::resolvedUndefsIn(Function &F) {
  1462. bool MadeChange = false;
  1463. for (BasicBlock &BB : F) {
  1464. if (!BBExecutable.count(&BB))
  1465. continue;
  1466. for (Instruction &I : BB) {
  1467. // Look for instructions which produce undef values.
  1468. if (I.getType()->isVoidTy())
  1469. continue;
  1470. if (auto *STy = dyn_cast<StructType>(I.getType())) {
  1471. // Only a few things that can be structs matter for undef.
  1472. // Tracked calls must never be marked overdefined in resolvedUndefsIn.
  1473. if (auto *CB = dyn_cast<CallBase>(&I))
  1474. if (Function *F = CB->getCalledFunction())
  1475. if (MRVFunctionsTracked.count(F))
  1476. continue;
  1477. // extractvalue and insertvalue don't need to be marked; they are
  1478. // tracked as precisely as their operands.
  1479. if (isa<ExtractValueInst>(I) || isa<InsertValueInst>(I))
  1480. continue;
  1481. // Send the results of everything else to overdefined. We could be
  1482. // more precise than this but it isn't worth bothering.
  1483. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
  1484. ValueLatticeElement &LV = getStructValueState(&I, i);
  1485. if (LV.isUnknown()) {
  1486. markOverdefined(LV, &I);
  1487. MadeChange = true;
  1488. }
  1489. }
  1490. continue;
  1491. }
  1492. ValueLatticeElement &LV = getValueState(&I);
  1493. if (!LV.isUnknown())
  1494. continue;
  1495. // There are two reasons a call can have an undef result
  1496. // 1. It could be tracked.
  1497. // 2. It could be constant-foldable.
  1498. // Because of the way we solve return values, tracked calls must
  1499. // never be marked overdefined in resolvedUndefsIn.
  1500. if (auto *CB = dyn_cast<CallBase>(&I))
  1501. if (Function *F = CB->getCalledFunction())
  1502. if (TrackedRetVals.count(F))
  1503. continue;
  1504. if (isa<LoadInst>(I)) {
  1505. // A load here means one of two things: a load of undef from a global,
  1506. // a load from an unknown pointer. Either way, having it return undef
  1507. // is okay.
  1508. continue;
  1509. }
  1510. markOverdefined(&I);
  1511. MadeChange = true;
  1512. }
  1513. }
  1514. LLVM_DEBUG(if (MadeChange) dbgs()
  1515. << "\nResolved undefs in " << F.getName() << '\n');
  1516. return MadeChange;
  1517. }
  1518. //===----------------------------------------------------------------------===//
  1519. //
  1520. // SCCPSolver implementations
  1521. //
  1522. SCCPSolver::SCCPSolver(
  1523. const DataLayout &DL,
  1524. std::function<const TargetLibraryInfo &(Function &)> GetTLI,
  1525. LLVMContext &Ctx)
  1526. : Visitor(new SCCPInstVisitor(DL, std::move(GetTLI), Ctx)) {}
  1527. SCCPSolver::~SCCPSolver() = default;
  1528. void SCCPSolver::addAnalysis(Function &F, AnalysisResultsForFn A) {
  1529. return Visitor->addAnalysis(F, std::move(A));
  1530. }
  1531. bool SCCPSolver::markBlockExecutable(BasicBlock *BB) {
  1532. return Visitor->markBlockExecutable(BB);
  1533. }
  1534. const PredicateBase *SCCPSolver::getPredicateInfoFor(Instruction *I) {
  1535. return Visitor->getPredicateInfoFor(I);
  1536. }
  1537. const LoopInfo &SCCPSolver::getLoopInfo(Function &F) {
  1538. return Visitor->getLoopInfo(F);
  1539. }
  1540. DomTreeUpdater SCCPSolver::getDTU(Function &F) { return Visitor->getDTU(F); }
  1541. void SCCPSolver::trackValueOfGlobalVariable(GlobalVariable *GV) {
  1542. Visitor->trackValueOfGlobalVariable(GV);
  1543. }
  1544. void SCCPSolver::addTrackedFunction(Function *F) {
  1545. Visitor->addTrackedFunction(F);
  1546. }
  1547. void SCCPSolver::addToMustPreserveReturnsInFunctions(Function *F) {
  1548. Visitor->addToMustPreserveReturnsInFunctions(F);
  1549. }
  1550. bool SCCPSolver::mustPreserveReturn(Function *F) {
  1551. return Visitor->mustPreserveReturn(F);
  1552. }
  1553. void SCCPSolver::addArgumentTrackedFunction(Function *F) {
  1554. Visitor->addArgumentTrackedFunction(F);
  1555. }
  1556. bool SCCPSolver::isArgumentTrackedFunction(Function *F) {
  1557. return Visitor->isArgumentTrackedFunction(F);
  1558. }
  1559. void SCCPSolver::solve() { Visitor->solve(); }
  1560. bool SCCPSolver::resolvedUndefsIn(Function &F) {
  1561. return Visitor->resolvedUndefsIn(F);
  1562. }
  1563. void SCCPSolver::solveWhileResolvedUndefsIn(Module &M) {
  1564. Visitor->solveWhileResolvedUndefsIn(M);
  1565. }
  1566. void
  1567. SCCPSolver::solveWhileResolvedUndefsIn(SmallVectorImpl<Function *> &WorkList) {
  1568. Visitor->solveWhileResolvedUndefsIn(WorkList);
  1569. }
  1570. bool SCCPSolver::isBlockExecutable(BasicBlock *BB) const {
  1571. return Visitor->isBlockExecutable(BB);
  1572. }
  1573. bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) const {
  1574. return Visitor->isEdgeFeasible(From, To);
  1575. }
  1576. std::vector<ValueLatticeElement>
  1577. SCCPSolver::getStructLatticeValueFor(Value *V) const {
  1578. return Visitor->getStructLatticeValueFor(V);
  1579. }
  1580. void SCCPSolver::removeLatticeValueFor(Value *V) {
  1581. return Visitor->removeLatticeValueFor(V);
  1582. }
  1583. const ValueLatticeElement &SCCPSolver::getLatticeValueFor(Value *V) const {
  1584. return Visitor->getLatticeValueFor(V);
  1585. }
  1586. const MapVector<Function *, ValueLatticeElement> &
  1587. SCCPSolver::getTrackedRetVals() {
  1588. return Visitor->getTrackedRetVals();
  1589. }
  1590. const DenseMap<GlobalVariable *, ValueLatticeElement> &
  1591. SCCPSolver::getTrackedGlobals() {
  1592. return Visitor->getTrackedGlobals();
  1593. }
  1594. const SmallPtrSet<Function *, 16> SCCPSolver::getMRVFunctionsTracked() {
  1595. return Visitor->getMRVFunctionsTracked();
  1596. }
  1597. void SCCPSolver::markOverdefined(Value *V) { Visitor->markOverdefined(V); }
  1598. bool SCCPSolver::isStructLatticeConstant(Function *F, StructType *STy) {
  1599. return Visitor->isStructLatticeConstant(F, STy);
  1600. }
  1601. Constant *SCCPSolver::getConstant(const ValueLatticeElement &LV) const {
  1602. return Visitor->getConstant(LV);
  1603. }
  1604. SmallPtrSetImpl<Function *> &SCCPSolver::getArgumentTrackedFunctions() {
  1605. return Visitor->getArgumentTrackedFunctions();
  1606. }
  1607. void SCCPSolver::markArgInFuncSpecialization(
  1608. Function *F, const SmallVectorImpl<ArgInfo> &Args) {
  1609. Visitor->markArgInFuncSpecialization(F, Args);
  1610. }
  1611. void SCCPSolver::markFunctionUnreachable(Function *F) {
  1612. Visitor->markFunctionUnreachable(F);
  1613. }
  1614. void SCCPSolver::visit(Instruction *I) { Visitor->visit(I); }
  1615. void SCCPSolver::visitCall(CallInst &I) { Visitor->visitCall(I); }