Skip to content

Commit

Permalink
[InstCombine] Ensure all undef operands are handled before binary ins…
Browse files Browse the repository at this point in the history
…truction constant folding

As noted in PR18355, this patch makes it clear that all cases with undef operands have been handled before further constant folding is attempted.

Differential Revision: http://reviews.llvm.org/D18305

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@263994 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
RKSimon committed Mar 21, 2016
1 parent 814be6b commit 752e5f9
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/IR/ConstantFold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,9 +916,11 @@ Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg,

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)) {
switch (Opcode) {
switch (static_cast<Instruction::BinaryOps>(Opcode)) {
case Instruction::Xor:
if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
// Handle undef ^ undef -> 0 special case. This is a common
Expand Down Expand Up @@ -998,9 +1000,22 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
return C1;
// undef << X -> 0
return Constant::getNullValue(C1->getType());
case Instruction::FAdd:
case Instruction::FSub:
case Instruction::FMul:
case Instruction::FDiv:
case Instruction::FRem:
// TODO: UNDEF handling for binary float instructions.
return nullptr;
case Instruction::BinaryOpsEnd:
llvm_unreachable("Invalid BinaryOp");
}
}

// At this point neither constant should be an UndefValue.
assert(!isa<UndefValue>(C1) && !isa<UndefValue>(C2) &&
"Unexpected UndefValue");

// Handle simplifications when the RHS is a constant int.
if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
switch (Opcode) {
Expand Down Expand Up @@ -1102,7 +1117,6 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
return ConstantExpr::get(Opcode, C2, C1);
}

// At this point we know neither constant is an UndefValue.
if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
const APInt &C1V = CI1->getValue();
Expand Down

0 comments on commit 752e5f9

Please sign in to comment.