llvm.org GIT mirror llvm / 3a73e34
Teach instruction simplify to use constant ranges to solve problems of the form "icmp pred %X, CI" and a number of examples where "%X = binop %Y, CI2". Some of these cases (div and rem) used to make it through opt -O2, but the others are probably now making code elsewhere redundant (probably instcombine). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@126988 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 8 years ago
2 changed file(s) with 138 addition(s) and 42 deletion(s). Raw diff Collapse all Expand all
2222 #include "llvm/Analysis/ConstantFolding.h"
2323 #include "llvm/Analysis/Dominators.h"
2424 #include "llvm/Analysis/ValueTracking.h"
25 #include "llvm/Support/ConstantRange.h"
2526 #include "llvm/Support/PatternMatch.h"
2627 #include "llvm/Support/ValueHandle.h"
2728 #include "llvm/Target/TargetData.h"
13981399
13991400 // See if we are doing a comparison with a constant integer.
14001401 if (ConstantInt *CI = dyn_cast(RHS)) {
1401 switch (Pred) {
1402 default: break;
1403 case ICmpInst::ICMP_UGT:
1404 if (CI->isMaxValue(false)) // A >u MAX -> FALSE
1405 return ConstantInt::getFalse(CI->getContext());
1406 break;
1407 case ICmpInst::ICMP_UGE:
1408 if (CI->isMinValue(false)) // A >=u MIN -> TRUE
1409 return ConstantInt::getTrue(CI->getContext());
1410 break;
1411 case ICmpInst::ICMP_ULT:
1412 if (CI->isMinValue(false)) // A FALSE
1413 return ConstantInt::getFalse(CI->getContext());
1414 break;
1415 case ICmpInst::ICMP_ULE:
1416 if (CI->isMaxValue(false)) // A <=u MAX -> TRUE
1417 return ConstantInt::getTrue(CI->getContext());
1418 break;
1419 case ICmpInst::ICMP_SGT:
1420 if (CI->isMaxValue(true)) // A >s MAX -> FALSE
1421 return ConstantInt::getFalse(CI->getContext());
1422 break;
1423 case ICmpInst::ICMP_SGE:
1424 if (CI->isMinValue(true)) // A >=s MIN -> TRUE
1425 return ConstantInt::getTrue(CI->getContext());
1426 break;
1427 case ICmpInst::ICMP_SLT:
1428 if (CI->isMinValue(true)) // A FALSE
1429 return ConstantInt::getFalse(CI->getContext());
1430 break;
1431 case ICmpInst::ICMP_SLE:
1432 if (CI->isMaxValue(true)) // A <=s MAX -> TRUE
1433 return ConstantInt::getTrue(CI->getContext());
1434 break;
1435 }
1436
1437 // TODO: integer div and rem with constant RHS all produce values in a
1438 // constant range. We should check whether the comparison is a tautology.
1402 // Rule out tautological comparisons (eg., ult 0 or uge 0).
1403 ConstantRange RHS_CR = ICmpInst::makeConstantRange(Pred, CI->getValue());
1404 if (RHS_CR.isEmptySet())
1405 return ConstantInt::getFalse(CI->getContext());
1406 if (RHS_CR.isFullSet())
1407 return ConstantInt::getTrue(CI->getContext());
1408
1409 // Many binary operators with constant RHS have easy to compute constant
1410 // range. Use them to check whether the comparison is a tautology.
1411 uint32_t Width = CI->getBitWidth();
1412 APInt Lower = APInt(Width, 0);
1413 APInt Upper = APInt(Width, 0);
1414 ConstantInt *CI2;
1415 if (match(LHS, m_URem(m_Value(), m_ConstantInt(CI2)))) {
1416 // 'urem x, CI2' produces [0, CI2).
1417 Upper = CI2->getValue();
1418 } else if (match(LHS, m_SRem(m_Value(), m_ConstantInt(CI2)))) {
1419 // 'srem x, CI2' produces (-|CI2|, |CI2|).
1420 Upper = CI2->getValue().abs();
1421 Lower = (-Upper) + 1;
1422 } else if (match(LHS, m_UDiv(m_Value(), m_ConstantInt(CI2)))) {
1423 // 'udiv x, CI2' produces [0, UINT_MAX / CI2].
1424 APInt NegOne = APInt::getAllOnesValue(Width);
1425 if (!CI2->isZero())
1426 Upper = NegOne.udiv(CI2->getValue()) + 1;
1427 } else if (match(LHS, m_SDiv(m_Value(), m_ConstantInt(CI2)))) {
1428 // 'sdiv x, CI2' produces [INT_MIN / CI2, INT_MAX / CI2].
1429 APInt IntMin = APInt::getSignedMinValue(Width);
1430 APInt IntMax = APInt::getSignedMaxValue(Width);
1431 APInt Val = CI2->getValue().abs();
1432 if (!Val.isMinValue()) {
1433 Lower = IntMin.sdiv(Val);
1434 Upper = IntMax.sdiv(Val) + 1;
1435 }
1436 } else if (match(LHS, m_LShr(m_Value(), m_ConstantInt(CI2)))) {
1437 // 'lshr x, CI2' produces [0, UINT_MAX >> CI2].
1438 APInt NegOne = APInt::getAllOnesValue(Width);
1439 if (CI2->getValue().ult(Width))
1440 Upper = NegOne.lshr(CI2->getValue()) + 1;
1441 } else if (match(LHS, m_AShr(m_Value(), m_ConstantInt(CI2)))) {
1442 // 'ashr x, CI2' produces [INT_MIN >> CI2, INT_MAX >> CI2].
1443 APInt IntMin = APInt::getSignedMinValue(Width);
1444 APInt IntMax = APInt::getSignedMaxValue(Width);
1445 if (CI2->getValue().ult(Width)) {
1446 Lower = IntMin.ashr(CI2->getValue());
1447 Upper = IntMax.ashr(CI2->getValue()) + 1;
1448 }
1449 } else if (match(LHS, m_Or(m_Value(), m_ConstantInt(CI2)))) {
1450 // 'or x, CI2' produces [CI2, UINT_MAX].
1451 Lower = CI2->getValue();
1452 } else if (match(LHS, m_And(m_Value(), m_ConstantInt(CI2)))) {
1453 // 'and x, CI2' produces [0, CI2].
1454 Upper = CI2->getValue() + 1;
1455 }
1456 if (Lower != Upper) {
1457 ConstantRange LHS_CR = ConstantRange(Lower, Upper);
1458 if (RHS_CR.contains(LHS_CR))
1459 return ConstantInt::getTrue(RHS->getContext());
1460 if (RHS_CR.inverse().contains(LHS_CR))
1461 return ConstantInt::getFalse(RHS->getContext());
1462 }
14391463 }
14401464
14411465 // Compare of cast, for example (zext X) != 0 -> X != 0
136136 ; CHECK: ret i1 false
137137 }
138138
139 define i1 @lshr(i32 %x) {
140 ; CHECK: @lshr
139 define i1 @lshr1(i32 %x) {
140 ; CHECK: @lshr1
141141 %s = lshr i32 -1, %x
142142 %c = icmp eq i32 %s, 0
143143 ret i1 %c
144144 ; CHECK: ret i1 false
145145 }
146146
147 define i1 @ashr(i32 %x) {
148 ; CHECK: @ashr
147 define i1 @lshr2(i32 %x) {
148 ; CHECK: @lshr2
149 %s = lshr i32 %x, 30
150 %c = icmp ugt i32 %s, 8
151 ret i1 %c
152 ; CHECK: ret i1 false
153 }
154
155 define i1 @ashr1(i32 %x) {
156 ; CHECK: @ashr1
149157 %s = ashr i32 -1, %x
150158 %c = icmp eq i32 %s, 0
159 ret i1 %c
160 ; CHECK: ret i1 false
161 }
162
163 define i1 @ashr2(i32 %x) {
164 ; CHECK: @ashr2
165 %s = ashr i32 %x, 30
166 %c = icmp slt i32 %s, -5
151167 ret i1 %c
152168 ; CHECK: ret i1 false
153169 }
202218 ret i1 %B
203219 ; CHECK ret i1 false
204220 }
221
222 define i1 @urem3(i32 %X) {
223 ; CHECK: @urem3
224 %A = urem i32 %X, 10
225 %B = icmp ult i32 %A, 15
226 ret i1 %B
227 ; CHECK: ret i1 true
228 }
229
230 define i1 @urem4(i32 %X) {
231 ; CHECK: @urem4
232 %A = urem i32 %X, 15
233 %B = icmp ult i32 %A, 10
234 ret i1 %B
235 ; CHECK: ret i1 %B
236 }
237
238 define i1 @srem1(i32 %X) {
239 ; CHECK: @srem1
240 %A = srem i32 %X, -5
241 %B = icmp sgt i32 %A, 5
242 ret i1 %B
243 ; CHECK: ret i1 false
244 }
245
246 define i1 @udiv1(i32 %X) {
247 ; CHECK: @udiv1
248 %A = udiv i32 %X, 1000000
249 %B = icmp ult i32 %A, 5000
250 ret i1 %B
251 ; CHECK: ret i1 true
252 }
253
254 define i1 @sdiv1(i32 %X) {
255 ; CHECK: @sdiv1
256 %A = sdiv i32 %X, 1000000
257 %B = icmp slt i32 %A, 3000
258 ret i1 %B
259 ; CHECK: ret i1 true
260 }
261
262 define i1 @or1(i32 %X) {
263 ; CHECK: @or1
264 %A = or i32 %X, 62
265 %B = icmp ult i32 %A, 50
266 ret i1 %B
267 ; CHECK: ret i1 false
268 }
269
270 define i1 @and1(i32 %X) {
271 ; CHECK: @and1
272 %A = and i32 %X, 62
273 %B = icmp ugt i32 %A, 70
274 ret i1 %B
275 ; CHECK: ret i1 false
276 }