|
34 | 34 | #include "llvm/Support/DebugCounter.h"
|
35 | 35 | #include "llvm/Support/FormattedStream.h"
|
36 | 36 | #include "llvm/Transforms/Scalar.h"
|
| 37 | +#include "llvm/Transforms/Utils/OrderedInstructions.h" |
37 | 38 | #include <algorithm>
|
38 | 39 | #define DEBUG_TYPE "predicateinfo"
|
39 | 40 | using namespace llvm;
|
@@ -106,14 +107,27 @@ struct ValueDFS {
|
106 | 107 | bool EdgeOnly = false;
|
107 | 108 | };
|
108 | 109 |
|
| 110 | +// Perform a strict weak ordering on instructions and arguments. |
| 111 | +static bool valueComesBefore(OrderedInstructions &OI, const Value *A, |
| 112 | + const Value *B) { |
| 113 | + auto *ArgA = dyn_cast_or_null<Argument>(A); |
| 114 | + auto *ArgB = dyn_cast_or_null<Argument>(B); |
| 115 | + if (ArgA && !ArgB) |
| 116 | + return true; |
| 117 | + if (ArgB && !ArgA) |
| 118 | + return false; |
| 119 | + if (ArgA && ArgB) |
| 120 | + return ArgA->getArgNo() < ArgB->getArgNo(); |
| 121 | + return OI.dominates(cast<Instruction>(A), cast<Instruction>(B)); |
| 122 | +} |
| 123 | + |
109 | 124 | // This compares ValueDFS structures, creating OrderedBasicBlocks where
|
110 | 125 | // necessary to compare uses/defs in the same block. Doing so allows us to walk
|
111 | 126 | // the minimum number of instructions necessary to compute our def/use ordering.
|
112 | 127 | struct ValueDFS_Compare {
|
113 |
| - DenseMap<const BasicBlock *, std::unique_ptr<OrderedBasicBlock>> &OBBMap; |
114 |
| - ValueDFS_Compare( |
115 |
| - DenseMap<const BasicBlock *, std::unique_ptr<OrderedBasicBlock>> &OBBMap) |
116 |
| - : OBBMap(OBBMap) {} |
| 128 | + OrderedInstructions &OI; |
| 129 | + ValueDFS_Compare(OrderedInstructions &OI) : OI(OI) {} |
| 130 | + |
117 | 131 | bool operator()(const ValueDFS &A, const ValueDFS &B) const {
|
118 | 132 | if (&A == &B)
|
119 | 133 | return false;
|
@@ -196,23 +210,12 @@ struct ValueDFS_Compare {
|
196 | 210 | auto *ArgA = dyn_cast_or_null<Argument>(ADef);
|
197 | 211 | auto *ArgB = dyn_cast_or_null<Argument>(BDef);
|
198 | 212 |
|
199 |
| - if (ArgA && !ArgB) |
200 |
| - return true; |
201 |
| - if (ArgB && !ArgA) |
202 |
| - return false; |
203 |
| - if (ArgA && ArgB) |
204 |
| - return ArgA->getArgNo() < ArgB->getArgNo(); |
| 213 | + if (ArgA || ArgB) |
| 214 | + return valueComesBefore(OI, ArgA, ArgB); |
205 | 215 |
|
206 | 216 | auto *AInst = getDefOrUser(ADef, A.U);
|
207 | 217 | auto *BInst = getDefOrUser(BDef, B.U);
|
208 |
| - |
209 |
| - auto *BB = AInst->getParent(); |
210 |
| - auto LookupResult = OBBMap.find(BB); |
211 |
| - if (LookupResult != OBBMap.end()) |
212 |
| - return LookupResult->second->dominates(AInst, BInst); |
213 |
| - |
214 |
| - auto Result = OBBMap.insert({BB, make_unique<OrderedBasicBlock>(BB)}); |
215 |
| - return Result.first->second->dominates(AInst, BInst); |
| 218 | + return valueComesBefore(OI, AInst, BInst); |
216 | 219 | }
|
217 | 220 | };
|
218 | 221 |
|
@@ -547,38 +550,11 @@ Value *PredicateInfo::materializeStack(unsigned int &Counter,
|
547 | 550 | void PredicateInfo::renameUses(SmallPtrSetImpl<Value *> &OpSet) {
|
548 | 551 | // Sort OpsToRename since we are going to iterate it.
|
549 | 552 | SmallVector<Value *, 8> OpsToRename(OpSet.begin(), OpSet.end());
|
550 |
| - std::sort(OpsToRename.begin(), OpsToRename.end(), [&](const Value *A, |
551 |
| - const Value *B) { |
552 |
| - auto *ArgA = dyn_cast_or_null<Argument>(A); |
553 |
| - auto *ArgB = dyn_cast_or_null<Argument>(B); |
554 |
| - |
555 |
| - // If A and B are args, order them based on their arg no. |
556 |
| - if (ArgA && !ArgB) |
557 |
| - return true; |
558 |
| - if (ArgB && !ArgA) |
559 |
| - return false; |
560 |
| - if (ArgA && ArgB) |
561 |
| - return ArgA->getArgNo() < ArgB->getArgNo(); |
562 |
| - |
563 |
| - // Else, A are B are instructions. |
564 |
| - // If they belong to different BBs, order them by the dominance of BBs. |
565 |
| - auto *AInst = cast<Instruction>(A); |
566 |
| - auto *BInst = cast<Instruction>(B); |
567 |
| - if (AInst->getParent() != BInst->getParent()) |
568 |
| - return DT.dominates(AInst->getParent(), BInst->getParent()); |
569 |
| - |
570 |
| - // Else, A and B belong to the same BB. |
571 |
| - // Order A and B by their dominance. |
572 |
| - auto *BB = AInst->getParent(); |
573 |
| - auto LookupResult = OBBMap.find(BB); |
574 |
| - if (LookupResult != OBBMap.end()) |
575 |
| - return LookupResult->second->dominates(AInst, BInst); |
576 |
| - |
577 |
| - auto Result = OBBMap.insert({BB, make_unique<OrderedBasicBlock>(BB)}); |
578 |
| - return Result.first->second->dominates(AInst, BInst); |
579 |
| - }); |
580 |
| - |
581 |
| - ValueDFS_Compare Compare(OBBMap); |
| 553 | + auto Comparator = [&](const Value *A, const Value *B) { |
| 554 | + return valueComesBefore(OI, A, B); |
| 555 | + }; |
| 556 | + std::sort(OpsToRename.begin(), OpsToRename.end(), Comparator); |
| 557 | + ValueDFS_Compare Compare(OI); |
582 | 558 | // Compute liveness, and rename in O(uses) per Op.
|
583 | 559 | for (auto *Op : OpsToRename) {
|
584 | 560 | unsigned Counter = 0;
|
@@ -715,7 +691,7 @@ PredicateInfo::getValueInfo(Value *Operand) const {
|
715 | 691 |
|
716 | 692 | PredicateInfo::PredicateInfo(Function &F, DominatorTree &DT,
|
717 | 693 | AssumptionCache &AC)
|
718 |
| - : F(F), DT(DT), AC(AC) { |
| 694 | + : F(F), DT(DT), AC(AC), OI(&DT) { |
719 | 695 | // Push an empty operand info so that we can detect 0 as not finding one
|
720 | 696 | ValueInfos.resize(1);
|
721 | 697 | buildPredicateInfo();
|
|
0 commit comments