diff options
author | Sanjay Patel <spatel@rotateright.com> | 2018-08-20 18:19:02 +0000 |
---|---|---|
committer | Sanjay Patel <spatel@rotateright.com> | 2018-08-20 18:19:02 +0000 |
commit | 3ce999fa417365d4012fe8889965a3ce7a9af60f (patch) | |
tree | 8f6cdc97f44e492015e9722f4ebd16c3e1b80e66 /llvm/lib/IR/ConstantFold.cpp | |
parent | a13746b7eb5306e77c27a4d1246f765850616e48 (diff) | |
download | bcm5719-llvm-3ce999fa417365d4012fe8889965a3ce7a9af60f.tar.gz bcm5719-llvm-3ce999fa417365d4012fe8889965a3ce7a9af60f.zip |
[ConstantFolding] improve folding of binops with vector undef operand
A non-undef operand may still have undef constant elements,
so we should always propagate the vector results per-lane.
llvm-svn: 340194
Diffstat (limited to 'llvm/lib/IR/ConstantFold.cpp')
-rw-r--r-- | llvm/lib/IR/ConstantFold.cpp | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index 90a8366d169..107975df1e7 100644 --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -916,13 +916,14 @@ Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg, return ConstantVector::get(Result); } - -Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, - Constant *C1, Constant *C2) { +Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1, + Constant *C2) { assert(Instruction::isBinaryOp(Opcode) && "Non-binary instruction detected"); - // Handle UndefValue up front. - if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) { + // Handle scalar UndefValue. Vectors are always evaluated per element. + bool HasScalarUndef = !C1->getType()->isVectorTy() && + (isa<UndefValue>(C1) || isa<UndefValue>(C2)); + if (HasScalarUndef) { switch (static_cast<Instruction::BinaryOps>(Opcode)) { case Instruction::Xor: if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) @@ -1024,9 +1025,8 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, } } - // At this point neither constant should be an UndefValue. - assert(!isa<UndefValue>(C1) && !isa<UndefValue>(C2) && - "Unexpected UndefValue"); + // Neither constant should be UndefValue, unless these are vector constants. + assert(!HasScalarUndef && "Unexpected UndefValue"); // Handle simplifications when the RHS is a constant int. if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) { @@ -1218,7 +1218,7 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, } } } else if (VectorType *VTy = dyn_cast<VectorType>(C1->getType())) { - // Perform elementwise folding. + // Fold each element and create a vector constant from those constants. SmallVector<Constant*, 16> Result; Type *Ty = IntegerType::get(VTy->getContext(), 32); for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |