DxilValueCache.cpp 17 KB

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