DxilValueCache.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. //===---------- DxilValueCache.cpp - Dxil Constant Value Cache ------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Utility to compute and cache constant values for instructions.
  11. //
  12. #include "llvm/Pass.h"
  13. #include "llvm/IR/Module.h"
  14. #include "llvm/IR/GlobalVariable.h"
  15. #include "llvm/IR/Constants.h"
  16. #include "llvm/IR/Instructions.h"
  17. #include "llvm/IR/Operator.h"
  18. #include "llvm/IR/CFG.h"
  19. #include "llvm/Support/Debug.h"
  20. #include "llvm/IR/Dominators.h"
  21. #include "llvm/Transforms/Scalar.h"
  22. #include "llvm/Analysis/InstructionSimplify.h"
  23. #include "llvm/Analysis/ConstantFolding.h"
  24. #include "llvm/ADT/Statistic.h"
  25. #include "llvm/Analysis/DxilValueCache.h"
  26. #include <unordered_set>
  27. #define DEBUG_TYPE "dxil-value-cache"
  28. using namespace llvm;
  29. static
  30. bool IsConstantTrue(const Value *V) {
  31. if (const ConstantInt *C = dyn_cast<ConstantInt>(V))
  32. return C->getLimitedValue() != 0;
  33. return false;
  34. }
  35. static
  36. bool IsConstantFalse(const Value *V) {
  37. if (const ConstantInt *C = dyn_cast<ConstantInt>(V))
  38. return C->getLimitedValue() == 0;
  39. return false;
  40. }
  41. static
  42. bool IsEntryBlock(const BasicBlock *BB) {
  43. return BB == &BB->getParent()->getEntryBlock();
  44. }
  45. void DxilValueCache::MarkAlwaysReachable(BasicBlock *BB) {
  46. ValueMap.Set(BB, ConstantInt::get(Type::getInt1Ty(BB->getContext()), 1));
  47. }
  48. void DxilValueCache::MarkUnreachable(BasicBlock *BB) {
  49. ValueMap.Set(BB, ConstantInt::get(Type::getInt1Ty(BB->getContext()), 0));
  50. }
  51. bool DxilValueCache::IsAlwaysReachable_(BasicBlock *BB) {
  52. if (Value *V = ValueMap.Get(BB))
  53. if (IsConstantTrue(V))
  54. return true;
  55. return false;
  56. }
  57. bool DxilValueCache::MayBranchTo(BasicBlock *A, BasicBlock *B) {
  58. BranchInst *Br = dyn_cast<BranchInst>(A->getTerminator());
  59. if (!Br) return false;
  60. if (Br->isUnconditional() && Br->getSuccessor(0) == B)
  61. return true;
  62. if (ConstantInt *C = dyn_cast<ConstantInt>(TryGetCachedValue(Br->getCondition()))) {
  63. unsigned SuccIndex = C->getLimitedValue() != 0 ? 0 : 1;
  64. return Br->getSuccessor(SuccIndex) == B;
  65. }
  66. return true;
  67. }
  68. bool DxilValueCache::IsUnreachable_(BasicBlock *BB) {
  69. if (Value *V = ValueMap.Get(BB))
  70. if (IsConstantFalse(V))
  71. return true;
  72. return false;
  73. }
  74. Value *DxilValueCache::ProcessAndSimplify_PHI(Instruction *I, DominatorTree *DT) {
  75. PHINode *PN = cast<PHINode>(I);
  76. BasicBlock *SoleIncoming = nullptr;
  77. bool Unreachable = true;
  78. Value *Simplified = nullptr;
  79. for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) {
  80. BasicBlock *PredBB = PN->getIncomingBlock(i);
  81. if (IsUnreachable_(PredBB))
  82. continue;
  83. Unreachable = false;
  84. if (MayBranchTo(PredBB, PN->getParent())) {
  85. if (SoleIncoming) {
  86. SoleIncoming = nullptr;
  87. break;
  88. }
  89. SoleIncoming = PredBB;
  90. }
  91. }
  92. if (Unreachable) {
  93. return UndefValue::get(I->getType());
  94. }
  95. if (SoleIncoming) {
  96. Value *V = TryGetCachedValue(PN->getIncomingValueForBlock(SoleIncoming));
  97. if (isa<Constant>(V))
  98. Simplified = V;
  99. else if (Instruction *I = dyn_cast<Instruction>(V)) {
  100. // If this is an instruction, we have to make sure it
  101. // dominates this PHI.
  102. // There are several conditions that qualify:
  103. // 1. There's only one predecessor
  104. // 2. If the instruction is in the entry block, then it must dominate
  105. // 3. If we are provided with a Dominator tree, and it decides that
  106. // it dominates.
  107. if (PN->getNumIncomingValues() == 1 ||
  108. IsEntryBlock(I->getParent()) ||
  109. (DT && DT->dominates(I, PN)))
  110. {
  111. Simplified = I;
  112. }
  113. }
  114. }
  115. // If we coulnd't deduce it, run the LLVM stock simplification to see
  116. // if we could do anything.
  117. if (!Simplified)
  118. Simplified = llvm::SimplifyInstruction(I, I->getModule()->getDataLayout());
  119. // One last step, to check if we have anything cached for whatever we
  120. // simplified to.
  121. if (Simplified)
  122. Simplified = TryGetCachedValue(Simplified);
  123. return Simplified;
  124. }
  125. Value *DxilValueCache::ProcessAndSimpilfy_Br(Instruction *I, DominatorTree *DT) {
  126. // The *only* reason we're paying special attention to the
  127. // branch inst, is to mark certain Basic Blocks as always
  128. // reachable or unreachable.
  129. BranchInst *Br = cast<BranchInst>(I);
  130. BasicBlock *BB = Br->getParent();
  131. if (Br->isConditional()) {
  132. BasicBlock *TrueSucc = Br->getSuccessor(0);
  133. BasicBlock *FalseSucc = Br->getSuccessor(1);
  134. Value *Cond = TryGetCachedValue(Br->getCondition());
  135. if (IsUnreachable_(BB)) {
  136. MarkUnreachable(FalseSucc);
  137. MarkUnreachable(TrueSucc);
  138. }
  139. else if (IsConstantTrue(Cond)) {
  140. if (IsAlwaysReachable_(BB)) {
  141. MarkAlwaysReachable(TrueSucc);
  142. }
  143. if (FalseSucc->getSinglePredecessor())
  144. MarkUnreachable(FalseSucc);
  145. }
  146. else if (IsConstantFalse(Cond)) {
  147. if (IsAlwaysReachable_(BB)) {
  148. MarkAlwaysReachable(FalseSucc);
  149. }
  150. if (TrueSucc->getSinglePredecessor())
  151. MarkUnreachable(TrueSucc);
  152. }
  153. }
  154. else {
  155. BasicBlock *Succ = Br->getSuccessor(0);
  156. if (IsAlwaysReachable_(BB))
  157. MarkAlwaysReachable(Succ);
  158. else if (Succ->getSinglePredecessor() && IsUnreachable_(BB))
  159. MarkUnreachable(Succ);
  160. }
  161. return nullptr;
  162. }
  163. Value *DxilValueCache::ProcessAndSimpilfy_Load(Instruction *I, DominatorTree *DT) {
  164. LoadInst *LI = cast<LoadInst>(I);
  165. Value *V = TryGetCachedValue(LI->getPointerOperand());
  166. if (Constant *ConstPtr = dyn_cast<Constant>(V)) {
  167. const DataLayout &DL = I->getModule()->getDataLayout();
  168. return llvm::ConstantFoldLoadFromConstPtr(ConstPtr, DL);
  169. }
  170. return nullptr;
  171. }
  172. Value *DxilValueCache::SimplifyAndCacheResult(Instruction *I, DominatorTree *DT) {
  173. const DataLayout &DL = I->getModule()->getDataLayout();
  174. Value *Simplified = nullptr;
  175. if (Instruction::Br == I->getOpcode()) {
  176. Simplified = ProcessAndSimpilfy_Br(I, DT);
  177. }
  178. else if (Instruction::PHI == I->getOpcode()) {
  179. Simplified = ProcessAndSimplify_PHI(I, DT);
  180. }
  181. else if (Instruction::Load == I->getOpcode()) {
  182. Simplified = ProcessAndSimpilfy_Load(I, DT);
  183. }
  184. // The rest of the checks use LLVM stock simplifications
  185. else if (I->isBinaryOp()) {
  186. Simplified =
  187. llvm::SimplifyBinOp(
  188. I->getOpcode(),
  189. TryGetCachedValue(I->getOperand(0)),
  190. TryGetCachedValue(I->getOperand(1)),
  191. DL);
  192. }
  193. else if (GetElementPtrInst *Gep = dyn_cast<GetElementPtrInst>(I)) {
  194. SmallVector<Value *, 4> Values;
  195. for (Value *V : Gep->operand_values()) {
  196. Values.push_back(TryGetCachedValue(V));
  197. }
  198. Simplified =
  199. llvm::SimplifyGEPInst(Values, DL, nullptr, DT, nullptr, nullptr);
  200. }
  201. else if (CmpInst *Cmp = dyn_cast<CmpInst>(I)) {
  202. Simplified =
  203. llvm::SimplifyCmpInst(Cmp->getPredicate(),
  204. TryGetCachedValue(I->getOperand(0)),
  205. TryGetCachedValue(I->getOperand(1)),
  206. DL);
  207. }
  208. else if (SelectInst *Select = dyn_cast<SelectInst>(I)) {
  209. Simplified =
  210. llvm::SimplifySelectInst(
  211. TryGetCachedValue(Select->getCondition()),
  212. TryGetCachedValue(Select->getTrueValue()),
  213. TryGetCachedValue(Select->getFalseValue()),
  214. DL
  215. );
  216. }
  217. else if (ExtractElementInst *IE = dyn_cast<ExtractElementInst>(I)) {
  218. Simplified =
  219. llvm::SimplifyExtractElementInst(
  220. TryGetCachedValue(IE->getVectorOperand()),
  221. TryGetCachedValue(IE->getIndexOperand()),
  222. DL, nullptr, DT);
  223. }
  224. else if (CastInst *Cast = dyn_cast<CastInst>(I)) {
  225. Simplified =
  226. llvm::SimplifyCastInst(
  227. Cast->getOpcode(),
  228. TryGetCachedValue(Cast->getOperand(0)),
  229. Cast->getType(), DL);
  230. }
  231. if (Simplified && isa<Constant>(Simplified))
  232. ValueMap.Set(I, Simplified);
  233. return Simplified;
  234. }
  235. STATISTIC(StaleValuesEncountered, "Stale Values Encountered");
  236. bool DxilValueCache::WeakValueMap::Seen(Value *V) {
  237. auto FindIt = Map.find(V);
  238. if (FindIt == Map.end())
  239. return false;
  240. auto &Entry = FindIt->second;
  241. if (Entry.IsStale())
  242. return false;
  243. return Entry.Value;
  244. }
  245. Value *DxilValueCache::WeakValueMap::Get(Value *V) {
  246. auto FindIt = Map.find(V);
  247. if (FindIt == Map.end())
  248. return nullptr;
  249. auto &Entry = FindIt->second;
  250. if (Entry.IsStale())
  251. return nullptr;
  252. Value *Result = Entry.Value;
  253. if (Result == GetSentinel(V->getContext()))
  254. return nullptr;
  255. return Result;
  256. }
  257. void DxilValueCache::WeakValueMap::SetSentinel(Value *Key) {
  258. Map[Key].Set(Key, GetSentinel(Key->getContext()));
  259. }
  260. Value *DxilValueCache::WeakValueMap::GetSentinel(LLVMContext &Ctx) {
  261. if (!Sentinel) {
  262. Sentinel.reset( PHINode::Create(Type::getInt1Ty(Ctx), 0) );
  263. }
  264. return Sentinel.get();
  265. }
  266. void DxilValueCache::WeakValueMap::ResetUnknowns() {
  267. if (!Sentinel)
  268. return;
  269. for (auto it = Map.begin(); it != Map.end(); it++) {
  270. if (it->second.Value == Sentinel.get())
  271. it->second.Value = nullptr;
  272. }
  273. }
  274. LLVM_DUMP_METHOD
  275. void DxilValueCache::WeakValueMap::dump() const {
  276. for (auto It = Map.begin(), E = Map.end(); It != E; It++) {
  277. const Value *Key = It->first;
  278. if (It->second.IsStale())
  279. continue;
  280. const Value *V = It->second.Value;
  281. bool IsSentinel = Sentinel && V == Sentinel.get();
  282. if (const BasicBlock *BB = dyn_cast<BasicBlock>(Key)) {
  283. dbgs() << "[BB]" << BB->getName() << " -> ";
  284. if (IsSentinel)
  285. dbgs() << "NO_VALUE";
  286. else {
  287. if (IsConstantTrue(V))
  288. dbgs() << "Always Reachable!";
  289. else if (IsConstantFalse(V))
  290. dbgs() << "Never Reachable!";
  291. }
  292. }
  293. else {
  294. dbgs() << Key->getName() << " -> ";
  295. if (IsSentinel)
  296. dbgs() << "NO_VALUE";
  297. else
  298. dbgs() << *V;
  299. }
  300. dbgs() << "\n";
  301. }
  302. }
  303. void DxilValueCache::WeakValueMap::Set(Value *Key, Value *V) {
  304. Map[Key].Set(Key, V);
  305. }
  306. // If there's a cached value, return it. Otherwise, return
  307. // the value itself.
  308. Value *DxilValueCache::TryGetCachedValue(Value *V) {
  309. if (Value *Simplified = ValueMap.Get(V))
  310. return Simplified;
  311. return V;
  312. }
  313. DxilValueCache::DxilValueCache() : ImmutablePass(ID) {
  314. initializeDxilValueCachePass(*PassRegistry::getPassRegistry());
  315. }
  316. const char *DxilValueCache::getPassName() const {
  317. return "Dxil Value Cache";
  318. }
  319. Value *DxilValueCache::GetValue(Value *V, DominatorTree *DT) {
  320. if (Value *NewV = ValueMap.Get(V))
  321. return NewV;
  322. return ProcessValue(V, DT);
  323. }
  324. Constant *DxilValueCache::GetConstValue(Value *V, DominatorTree *DT) {
  325. if (Value *NewV = GetValue(V))
  326. return dyn_cast<Constant>(NewV);
  327. return nullptr;
  328. }
  329. bool DxilValueCache::IsAlwaysReachable(BasicBlock *BB, DominatorTree *DT) {
  330. ProcessValue(BB, DT);
  331. return IsAlwaysReachable_(BB);
  332. }
  333. bool DxilValueCache::IsUnreachable(BasicBlock *BB, DominatorTree *DT) {
  334. ProcessValue(BB, DT);
  335. return IsUnreachable_(BB);
  336. }
  337. LLVM_DUMP_METHOD
  338. void DxilValueCache::dump() const {
  339. ValueMap.dump();
  340. }
  341. void DxilValueCache::getAnalysisUsage(AnalysisUsage &AU) const {
  342. AU.setPreservesAll();
  343. }
  344. Value *DxilValueCache::ProcessValue(Value *NewV, DominatorTree *DT) {
  345. Value *Result = nullptr;
  346. SmallVector<Value *, 16> WorkList;
  347. // Although we accept all values for convenience, we only process
  348. // Instructions.
  349. if (Instruction *I = dyn_cast<Instruction>(NewV)) {
  350. WorkList.push_back(I);
  351. }
  352. else if (BasicBlock *BB = dyn_cast<BasicBlock>(NewV)) {
  353. WorkList.push_back(BB->getTerminator());
  354. WorkList.push_back(BB);
  355. }
  356. else {
  357. return nullptr;
  358. }
  359. // Unconditionally process this one instruction, whether we've seen
  360. // it or not. The simplification might be able to do something to
  361. // simplify it even when we don't have its value cached.
  362. // This is a basic DFS setup.
  363. while (WorkList.size()) {
  364. Value *V = WorkList.back();
  365. // If we haven't seen this value, go in and push things it depends on
  366. // into the worklist.
  367. if (!ValueMap.Seen(V)) {
  368. ValueMap.SetSentinel(V);
  369. if (Instruction *I = dyn_cast<Instruction>(V)) {
  370. for (Use &U : I->operands()) {
  371. Instruction *UseI = dyn_cast<Instruction>(U.get());
  372. if (!UseI)
  373. continue;
  374. if (!ValueMap.Seen(UseI))
  375. WorkList.push_back(UseI);
  376. }
  377. if (PHINode *PN = dyn_cast<PHINode>(I)) {
  378. for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) {
  379. BasicBlock *BB = PN->getIncomingBlock(i);
  380. TerminatorInst *Term = BB->getTerminator();
  381. if (!ValueMap.Seen(Term))
  382. WorkList.push_back(Term);
  383. if (!ValueMap.Seen(BB))
  384. WorkList.push_back(BB);
  385. }
  386. }
  387. }
  388. else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
  389. if (IsEntryBlock(BB)) {
  390. MarkAlwaysReachable(BB);
  391. }
  392. for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; PI++) {
  393. BasicBlock *PredBB = *PI;
  394. TerminatorInst *Term = PredBB->getTerminator();
  395. if (!ValueMap.Seen(Term))
  396. WorkList.push_back(Term);
  397. if (!ValueMap.Seen(PredBB))
  398. WorkList.push_back(PredBB);
  399. }
  400. }
  401. }
  402. // If we've seen this values, all its dependencies must have been processed
  403. // as well.
  404. else {
  405. WorkList.pop_back();
  406. if (Instruction *I = dyn_cast<Instruction>(V)) {
  407. Value *SimplifiedValue = SimplifyAndCacheResult(I, DT);
  408. // Set the result if this is the input inst.
  409. // SimplifyInst may not have cached the value
  410. // so we return it directly.
  411. if (I == NewV)
  412. Result = SimplifiedValue;
  413. }
  414. else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
  415. // Deduce the basic block's reachability based on
  416. // other analysis.
  417. if (!IsEntryBlock(BB)) {
  418. bool AllNeverReachable = true;
  419. for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; PI++) {
  420. if (!IsUnreachable_(BB)) {
  421. AllNeverReachable = false;
  422. break;
  423. }
  424. }
  425. if (AllNeverReachable)
  426. MarkUnreachable(BB);
  427. }
  428. }
  429. }
  430. }
  431. return Result;
  432. }
  433. char DxilValueCache::ID;
  434. Pass *llvm::createDxilValueCachePass() {
  435. return new DxilValueCache();
  436. }
  437. INITIALIZE_PASS(DxilValueCache, DEBUG_TYPE, "Dxil Value Cache", false, false)