diff options
| author | Sanjay Patel <spatel@rotateright.com> | 2018-06-04 22:26:45 +0000 |
|---|---|---|
| committer | Sanjay Patel <spatel@rotateright.com> | 2018-06-04 22:26:45 +0000 |
| commit | dcb8d304c319bf8d6c92cde4fcbcff11aa4be238 (patch) | |
| tree | 8106def33450b7a41c25e0950398dec0fa13e746 /llvm/lib | |
| parent | 800ac42573a3fba949c17f7336d3f38ca394acba (diff) | |
| download | bcm5719-llvm-dcb8d304c319bf8d6c92cde4fcbcff11aa4be238.tar.gz bcm5719-llvm-dcb8d304c319bf8d6c92cde4fcbcff11aa4be238.zip | |
[InstCombine] refine UB-handling in shuffle-binop transform
As noted in rL333782, we can be both better for optimization and
safer with this transform:
BinOp (shuffle V1, Mask), C --> shuffle (BinOp V1, NewC), Mask
The only potentially unsafe-to-speculate binops are integer div/rem.
All other binops are always safe (although I don't see a way to
assert that in code here).
For opcodes like shifts that can produce poison, it can't matter
here because we know the lanes with undef are dropped by the
subsequent shuffle.
Differential Revision: https://reviews.llvm.org/D47686
llvm-svn: 333962
Diffstat (limited to 'llvm/lib')
| -rw-r--r-- | llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index f87b94dd355..2b9db4baafa 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -1417,21 +1417,21 @@ Instruction *InstCombiner::foldShuffledBinop(BinaryOperator &Inst) { } } if (MayChange) { - // It's not safe to use a vector with undef elements because the entire - // instruction can be folded to undef (for example, div/rem divisors). - // Replace undef lanes with the first non-undef element. Vector demanded - // elements can change those back to undef values if that is safe. - Constant *SafeDummyConstant = nullptr; - for (unsigned i = 0; i < VWidth; ++i) { - if (!isa<UndefValue>(NewVecC[i])) { - SafeDummyConstant = NewVecC[i]; - break; - } + // With integer div/rem instructions, it is not safe to use a vector with + // undef elements because the entire instruction can be folded to undef. + // So replace undef elements with '1' because that can never induce + // undefined behavior. All other binop opcodes are always safe to + // speculate, and therefore, it is fine to include undef elements for + // unused lanes (and using undefs may help optimization). + BinaryOperator::BinaryOps Opcode = Inst.getOpcode(); + if (Opcode == Instruction::UDiv || Opcode == Instruction::URem || + Opcode == Instruction::SDiv || Opcode == Instruction::SRem) { + assert(C->getType()->getScalarType()->isIntegerTy() && + "Not expecting FP opcodes/operands/constants here"); + for (unsigned i = 0; i < VWidth; ++i) + if (isa<UndefValue>(NewVecC[i])) + NewVecC[i] = ConstantInt::get(NewVecC[i]->getType(), 1); } - assert(SafeDummyConstant && "Undef constant vector was not simplified?"); - for (unsigned i = 0; i < VWidth; ++i) - if (isa<UndefValue>(NewVecC[i])) - NewVecC[i] = SafeDummyConstant; // Op(shuffle(V1, Mask), C) -> shuffle(Op(V1, NewC), Mask) // Op(C, shuffle(V1, Mask)) -> shuffle(Op(NewC, V1), Mask) |

