diff options
author | Duncan Sands <baldrick@free.fr> | 2012-02-24 15:16:31 +0000 |
---|---|---|
committer | Duncan Sands <baldrick@free.fr> | 2012-02-24 15:16:31 +0000 |
commit | 926d101640f128a0bd32be2039c795d4b3b2fb96 (patch) | |
tree | 457e1315b53624a77e99aedb56971f48cc4f5322 /llvm/lib/Transforms/Scalar/GVN.cpp | |
parent | 6fe3e3d3357acbb7d0343bd1c6fa63c16d4467ea (diff) | |
download | bcm5719-llvm-926d101640f128a0bd32be2039c795d4b3b2fb96.tar.gz bcm5719-llvm-926d101640f128a0bd32be2039c795d4b3b2fb96.zip |
Teach GVN that x+y is the same as y+x and that x<y is the same as y>x.
llvm-svn: 151365
Diffstat (limited to 'llvm/lib/Transforms/Scalar/GVN.cpp')
-rw-r--r-- | llvm/lib/Transforms/Scalar/GVN.cpp | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp index 5b95f5b86fb..162ee2b5cea 100644 --- a/llvm/lib/Transforms/Scalar/GVN.cpp +++ b/llvm/lib/Transforms/Scalar/GVN.cpp @@ -154,9 +154,24 @@ Expression ValueTable::create_expression(Instruction *I) { for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end(); OI != OE; ++OI) e.varargs.push_back(lookup_or_add(*OI)); + if (I->isCommutative()) { + // Ensure that commutative instructions that only differ by a permutation + // of their operands get the same value number by sorting the operand value + // numbers. Since all commutative instructions have two operands it is more + // efficient to sort by hand rather than using, say, std::sort. + assert(I->getNumOperands() == 2 && "Unsupported commutative instruction!"); + if (e.varargs[0] > e.varargs[1]) + std::swap(e.varargs[0], e.varargs[1]); + } if (CmpInst *C = dyn_cast<CmpInst>(I)) { - e.opcode = (C->getOpcode() << 8) | C->getPredicate(); + // Sort the operand value numbers so x<y and y>x get the same value number. + CmpInst::Predicate Predicate = C->getPredicate(); + if (e.varargs[0] > e.varargs[1]) { + std::swap(e.varargs[0], e.varargs[1]); + Predicate = CmpInst::getSwappedPredicate(Predicate); + } + e.opcode = (C->getOpcode() << 8) | Predicate; } else if (InsertValueInst *E = dyn_cast<InsertValueInst>(I)) { for (InsertValueInst::idx_iterator II = E->idx_begin(), IE = E->idx_end(); II != IE; ++II) |