llvm.org GIT mirror llvm / 8953163
ConstantFold: div undef, 0 should fold to undef, not zero Dividing by zero yields an undefined value. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223924 91177308-0d34-0410-b5e6-96231b3b80d8 David Majnemer 4 years ago
2 changed file(s) with 26 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
2626 #include "llvm/IR/GlobalVariable.h"
2727 #include "llvm/IR/Instructions.h"
2828 #include "llvm/IR/Operator.h"
29 #include "llvm/IR/PatternMatch.h"
2930 #include "llvm/Support/Compiler.h"
3031 #include "llvm/Support/ErrorHandling.h"
3132 #include "llvm/Support/ManagedStatic.h"
3233 #include "llvm/Support/MathExtras.h"
3334 #include
3435 using namespace llvm;
36 using namespace llvm::PatternMatch;
3537
3638 //===----------------------------------------------------------------------===//
3739 // ConstantFold*Instruction Implementations
922924 // X * undef -> 0 otherwise
923925 return Constant::getNullValue(C1->getType());
924926 }
927 case Instruction::SDiv:
925928 case Instruction::UDiv:
926 case Instruction::SDiv:
929 // X / undef -> undef
930 if (match(C1, m_Zero()))
931 return C2;
932 // undef / 0 -> undef
927933 // undef / 1 -> undef
928 if (Opcode == Instruction::UDiv || Opcode == Instruction::SDiv)
929 if (ConstantInt *CI2 = dyn_cast(C2))
930 if (CI2->isOne())
931 return C1;
932 // FALL THROUGH
934 if (match(C2, m_Zero()) || match(C2, m_One()))
935 return C1;
936 // undef / X -> 0 otherwise
937 return Constant::getNullValue(C1->getType());
933938 case Instruction::URem:
934939 case Instruction::SRem:
935 if (!isa(C2)) // undef / X -> 0
936 return Constant::getNullValue(C1->getType());
937 return C2; // X / undef -> undef
940 // X % undef -> undef
941 if (match(C2, m_Undef()))
942 return C2;
943 // undef % 0 -> undef
944 if (match(C2, m_Zero()))
945 return C1;
946 // undef % X -> 0 otherwise
947 return Constant::getNullValue(C1->getType());
938948 case Instruction::Or: // X | undef -> -1
939949 if (isa(C1) && isa(C2)) // undef | undef -> undef
940950 return C1;
187187 %b = lshr exact i32 undef, %a
188188 ret i32 %b
189189 }
190
191 ; CHECK-LABEL: @test24
192 ; CHECK: ret i32 undef
193 define i32 @test24(i32 %a) {
194 %b = udiv i32 undef, 0
195 ret i32 %b
196 }