LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 1215 - can't constantfold sdiv 0, -1
Summary: can't constantfold sdiv 0, -1
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Core LLVM classes (show other bugs)
Version: trunk
Hardware: All All
: P normal
Assignee: Chris Lattner
URL:
Keywords: compile-fail
Depends on:
Blocks:
 
Reported: 2007-02-21 22:28 PST by Nick Lewycky
Modified: 2010-02-22 12:40 PST (History)
1 user (show)

See Also:
Fixed By Commit(s):


Attachments
llvm-as pr1215.ll | opt -indvars (2.12 KB, application/octet-stream)
2007-02-21 22:29 PST, Nick Lewycky
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Nick Lewycky 2007-02-21 22:28:28 PST
I encountered an assertion failure in scalar evolutions:

opt: ScalarEvolution.cpp:2362: llvm::SCEVHandle
llvm::SCEVAddRecExpr::getNumIterationsInRange(llvm::ConstantRange, bool) const:
Assertion `isa<ConstantInt>(ExitValue) && "Constant folding of integers not
implemented?"' failed.

The assertion has found a real problem. The line in question is
"ConstantExpr::getSDiv(ExitValue, A);" where ExitValue is i32 0 and A is i32 -1.
getSDiv eventually gets into this case in ConstantFoldBinaryInstruction
(ConstantFolding.cpp):

00572       case Instruction::SDiv:
00573         if (CI2->isNullValue()) return 0;        // X / 0 -> can't fold
00574         if (CI2->isAllOnesValue() &&
00575             (((CI1->getType()->getPrimitiveSizeInBits() == 64) && 
00576               (CI1->getSExtValue() == INT64_MIN)) ||
00577              (CI1->getSExtValue() == -CI1->getSExtValue())))
00578           return 0;                              // MIN_INT / -1 -> overflow
00579         return ConstantInt::get(C1->getType(), 
00580                                 CI1->getSExtValue() / CI2->getSExtValue());

Given that CI1 is the left hand side, CI1->getSExtValue() ==
-CI1->getSExtValue() when the left is 0.

This code also fails to properly fold arbitrary precision integers, so it
deserves to be rewritten.
Comment 1 Nick Lewycky 2007-02-21 22:29:19 PST
Created attachment 680 [details]
llvm-as pr1215.ll | opt -indvars
Comment 2 Chris Lattner 2007-02-23 19:17:28 PST
Fixed, testcase here: Transforms/ConstProp/2007-02-23-sdiv.ll

Patch here:
http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20070219/044968.html

-Chris
Comment 3 Chris Lattner 2007-02-23 19:20:47 PST
patch actually here: http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-
Mon-20070219/044969.html