ConstantFold: an undef shift amount results in undef

X shifted by undef results in undef because the undef value can
represent values greater than the width of the operands.

llvm-svn: 223968
This commit is contained in:
David Majnemer 2014-12-10 21:38:05 +00:00
parent db0b13cef0
commit 89cf6d79eb
2 changed files with 35 additions and 13 deletions

View File

@ -951,21 +951,22 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
return C1; return C1;
return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0 return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0
case Instruction::LShr: case Instruction::LShr:
if (isa<UndefValue>(C2) && isa<UndefValue>(C1)) // X >>l undef -> undef
return C1; // undef lshr undef -> undef if (isa<UndefValue>(C2))
return Constant::getNullValue(C1->getType()); // X lshr undef -> 0 return C2;
// undef lshr X -> 0 // undef >>l X -> 0
return Constant::getNullValue(C1->getType());
case Instruction::AShr: case Instruction::AShr:
if (!isa<UndefValue>(C2)) // undef ashr X --> all ones // X >>a undef -> undef
return Constant::getAllOnesValue(C1->getType()); if (isa<UndefValue>(C2))
else if (isa<UndefValue>(C1)) return C2;
return C1; // undef ashr undef -> undef // undef >>a X -> all ones
else return Constant::getAllOnesValue(C1->getType());
return C1; // X ashr undef --> X
case Instruction::Shl: case Instruction::Shl:
if (isa<UndefValue>(C2) && isa<UndefValue>(C1)) // X << undef -> undef
return C1; // undef shl undef -> undef if (isa<UndefValue>(C2))
// undef << X -> 0 or X << undef -> 0 return C2;
// undef << X -> 0
return Constant::getNullValue(C1->getType()); return Constant::getNullValue(C1->getType());
} }
} }

View File

@ -195,3 +195,24 @@ define i32 @test24(i32 %a) {
%b = udiv i32 undef, 0 %b = udiv i32 undef, 0
ret i32 %b ret i32 %b
} }
; CHECK-LABEL: @test25
; CHECK: ret i32 undef
define i32 @test25(i32 %a) {
%b = lshr i32 0, undef
ret i32 %b
}
; CHECK-LABEL: @test26
; CHECK: ret i32 undef
define i32 @test26(i32 %a) {
%b = ashr i32 0, undef
ret i32 %b
}
; CHECK-LABEL: @test27
; CHECK: ret i32 undef
define i32 @test27(i32 %a) {
%b = shl i32 0, undef
ret i32 %b
}