Skip to content

Commit b7df17e

Browse files
committed
PredicateInfo: Use OrderedInstructions instead of our homemade
version. llvm-svn: 306703
1 parent b779db7 commit b7df17e

File tree

2 files changed

+31
-55
lines changed

2 files changed

+31
-55
lines changed

llvm/include/llvm/Transforms/Utils/PredicateInfo.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#include "llvm/Support/Casting.h"
7575
#include "llvm/Support/Compiler.h"
7676
#include "llvm/Support/ErrorHandling.h"
77+
#include "llvm/Transforms/Utils/OrderedInstructions.h"
7778
#include <algorithm>
7879
#include <cassert>
7980
#include <cstddef>
@@ -89,7 +90,6 @@ class Instruction;
8990
class MemoryAccess;
9091
class LLVMContext;
9192
class raw_ostream;
92-
class OrderedBasicBlock;
9393

9494
enum PredicateType { PT_Branch, PT_Assume, PT_Switch };
9595

@@ -115,7 +115,8 @@ class PredicateWithCondition : public PredicateBase {
115115
public:
116116
Value *Condition;
117117
static inline bool classof(const PredicateBase *PB) {
118-
return PB->Type == PT_Assume || PB->Type == PT_Branch || PB->Type == PT_Switch;
118+
return PB->Type == PT_Assume || PB->Type == PT_Branch ||
119+
PB->Type == PT_Switch;
119120
}
120121

121122
protected:
@@ -244,6 +245,7 @@ class PredicateInfo {
244245
Function &F;
245246
DominatorTree &DT;
246247
AssumptionCache &AC;
248+
OrderedInstructions OI;
247249
// This maps from copy operands to Predicate Info. Note that it does not own
248250
// the Predicate Info, they belong to the ValueInfo structs in the ValueInfos
249251
// vector.
@@ -256,8 +258,6 @@ class PredicateInfo {
256258
// 0 is not a valid Value Info index, you can use DenseMap::lookup and tell
257259
// whether it returned a valid result.
258260
DenseMap<Value *, unsigned int> ValueInfoNums;
259-
// OrderedBasicBlocks used during sorting uses
260-
DenseMap<const BasicBlock *, std::unique_ptr<OrderedBasicBlock>> OBBMap;
261261
// The set of edges along which we can only handle phi uses, due to critical
262262
// edges.
263263
DenseSet<std::pair<BasicBlock *, BasicBlock *>> EdgeUsesOnly;

llvm/lib/Transforms/Utils/PredicateInfo.cpp

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "llvm/Support/DebugCounter.h"
3535
#include "llvm/Support/FormattedStream.h"
3636
#include "llvm/Transforms/Scalar.h"
37+
#include "llvm/Transforms/Utils/OrderedInstructions.h"
3738
#include <algorithm>
3839
#define DEBUG_TYPE "predicateinfo"
3940
using namespace llvm;
@@ -106,14 +107,27 @@ struct ValueDFS {
106107
bool EdgeOnly = false;
107108
};
108109

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+
109124
// This compares ValueDFS structures, creating OrderedBasicBlocks where
110125
// necessary to compare uses/defs in the same block. Doing so allows us to walk
111126
// the minimum number of instructions necessary to compute our def/use ordering.
112127
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+
117131
bool operator()(const ValueDFS &A, const ValueDFS &B) const {
118132
if (&A == &B)
119133
return false;
@@ -196,23 +210,12 @@ struct ValueDFS_Compare {
196210
auto *ArgA = dyn_cast_or_null<Argument>(ADef);
197211
auto *ArgB = dyn_cast_or_null<Argument>(BDef);
198212

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);
205215

206216
auto *AInst = getDefOrUser(ADef, A.U);
207217
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);
216219
}
217220
};
218221

@@ -547,38 +550,11 @@ Value *PredicateInfo::materializeStack(unsigned int &Counter,
547550
void PredicateInfo::renameUses(SmallPtrSetImpl<Value *> &OpSet) {
548551
// Sort OpsToRename since we are going to iterate it.
549552
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);
582558
// Compute liveness, and rename in O(uses) per Op.
583559
for (auto *Op : OpsToRename) {
584560
unsigned Counter = 0;
@@ -715,7 +691,7 @@ PredicateInfo::getValueInfo(Value *Operand) const {
715691

716692
PredicateInfo::PredicateInfo(Function &F, DominatorTree &DT,
717693
AssumptionCache &AC)
718-
: F(F), DT(DT), AC(AC) {
694+
: F(F), DT(DT), AC(AC), OI(&DT) {
719695
// Push an empty operand info so that we can detect 0 as not finding one
720696
ValueInfos.resize(1);
721697
buildPredicateInfo();

0 commit comments

Comments
 (0)