llvm.org GIT mirror llvm / 25ec483
Move GetStringLength and helper from SimplifyLibCalls to ValueTracking. No functionality change. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@97793 91177308-0d34-0410-b5e6-96231b3b80d8 Eric Christopher 10 years ago
3 changed file(s) with 133 addition(s) and 124 deletion(s). Raw diff Collapse all Expand all
123123 /// character is included in the result string.
124124 bool GetConstantStringInfo(Value *V, std::string &Str, uint64_t Offset = 0,
125125 bool StopAtNul = true);
126
127 /// GetStringLength - If we can compute the length of the string pointed to by
128 /// the specified pointer, return 'len+1'. If we can't, return 0.
129 uint64_t GetStringLength(Value *V);
126130 } // end namespace llvm
127131
128132 #endif
2222 #include "llvm/Target/TargetData.h"
2323 #include "llvm/Support/GetElementPtrTypeIterator.h"
2424 #include "llvm/Support/MathExtras.h"
25 #include "llvm/ADT/SmallPtrSet.h"
2526 #include
2627 using namespace llvm;
2728
14351436 // The array isn't null terminated, but maybe this is a memcpy, not a strcpy.
14361437 return true;
14371438 }
1439
1440 // These next two are very similar to the above, but also look through PHI
1441 // nodes.
1442 // TODO: See if we can integrate these two together.
1443
1444 /// GetStringLengthH - If we can compute the length of the string pointed to by
1445 /// the specified pointer, return 'len+1'. If we can't, return 0.
1446 static uint64_t GetStringLengthH(Value *V, SmallPtrSet &PHIs) {
1447 // Look through noop bitcast instructions.
1448 if (BitCastInst *BCI = dyn_cast(V))
1449 return GetStringLengthH(BCI->getOperand(0), PHIs);
1450
1451 // If this is a PHI node, there are two cases: either we have already seen it
1452 // or we haven't.
1453 if (PHINode *PN = dyn_cast(V)) {
1454 if (!PHIs.insert(PN))
1455 return ~0ULL; // already in the set.
1456
1457 // If it was new, see if all the input strings are the same length.
1458 uint64_t LenSoFar = ~0ULL;
1459 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1460 uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs);
1461 if (Len == 0) return 0; // Unknown length -> unknown.
1462
1463 if (Len == ~0ULL) continue;
1464
1465 if (Len != LenSoFar && LenSoFar != ~0ULL)
1466 return 0; // Disagree -> unknown.
1467 LenSoFar = Len;
1468 }
1469
1470 // Success, all agree.
1471 return LenSoFar;
1472 }
1473
1474 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
1475 if (SelectInst *SI = dyn_cast(V)) {
1476 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
1477 if (Len1 == 0) return 0;
1478 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
1479 if (Len2 == 0) return 0;
1480 if (Len1 == ~0ULL) return Len2;
1481 if (Len2 == ~0ULL) return Len1;
1482 if (Len1 != Len2) return 0;
1483 return Len1;
1484 }
1485
1486 // If the value is not a GEP instruction nor a constant expression with a
1487 // GEP instruction, then return unknown.
1488 User *GEP = 0;
1489 if (GetElementPtrInst *GEPI = dyn_cast(V)) {
1490 GEP = GEPI;
1491 } else if (ConstantExpr *CE = dyn_cast(V)) {
1492 if (CE->getOpcode() != Instruction::GetElementPtr)
1493 return 0;
1494 GEP = CE;
1495 } else {
1496 return 0;
1497 }
1498
1499 // Make sure the GEP has exactly three arguments.
1500 if (GEP->getNumOperands() != 3)
1501 return 0;
1502
1503 // Check to make sure that the first operand of the GEP is an integer and
1504 // has value 0 so that we are sure we're indexing into the initializer.
1505 if (ConstantInt *Idx = dyn_cast(GEP->getOperand(1))) {
1506 if (!Idx->isZero())
1507 return 0;
1508 } else
1509 return 0;
1510
1511 // If the second index isn't a ConstantInt, then this is a variable index
1512 // into the array. If this occurs, we can't say anything meaningful about
1513 // the string.
1514 uint64_t StartIdx = 0;
1515 if (ConstantInt *CI = dyn_cast(GEP->getOperand(2)))
1516 StartIdx = CI->getZExtValue();
1517 else
1518 return 0;
1519
1520 // The GEP instruction, constant or instruction, must reference a global
1521 // variable that is a constant and is initialized. The referenced constant
1522 // initializer is the array that we'll use for optimization.
1523 GlobalVariable* GV = dyn_cast(GEP->getOperand(0));
1524 if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
1525 GV->mayBeOverridden())
1526 return 0;
1527 Constant *GlobalInit = GV->getInitializer();
1528
1529 // Handle the ConstantAggregateZero case, which is a degenerate case. The
1530 // initializer is constant zero so the length of the string must be zero.
1531 if (isa(GlobalInit))
1532 return 1; // Len = 0 offset by 1.
1533
1534 // Must be a Constant Array
1535 ConstantArray *Array = dyn_cast(GlobalInit);
1536 if (!Array || !Array->getType()->getElementType()->isIntegerTy(8))
1537 return false;
1538
1539 // Get the number of elements in the array
1540 uint64_t NumElts = Array->getType()->getNumElements();
1541
1542 // Traverse the constant array from StartIdx (derived above) which is
1543 // the place the GEP refers to in the array.
1544 for (unsigned i = StartIdx; i != NumElts; ++i) {
1545 Constant *Elt = Array->getOperand(i);
1546 ConstantInt *CI = dyn_cast(Elt);
1547 if (!CI) // This array isn't suitable, non-int initializer.
1548 return 0;
1549 if (CI->isZero())
1550 return i-StartIdx+1; // We found end of string, success!
1551 }
1552
1553 return 0; // The array isn't null terminated, conservatively return 'unknown'.
1554 }
1555
1556 /// GetStringLength - If we can compute the length of the string pointed to by
1557 /// the specified pointer, return 'len+1'. If we can't, return 0.
1558 uint64_t llvm::GetStringLength(Value *V) {
1559 if (!V->getType()->isPointerTy()) return 0;
1560
1561 SmallPtrSet PHIs;
1562 uint64_t Len = GetStringLengthH(V, PHIs);
1563 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
1564 // an empty string as a length.
1565 return Len == ~0ULL ? 1 : Len;
1566 }
431431 //===----------------------------------------------------------------------===//
432432 // Helper Functions
433433 //===----------------------------------------------------------------------===//
434
435 /// GetStringLengthH - If we can compute the length of the string pointed to by
436 /// the specified pointer, return 'len+1'. If we can't, return 0.
437 static uint64_t GetStringLengthH(Value *V, SmallPtrSet &PHIs) {
438 // Look through noop bitcast instructions.
439 if (BitCastInst *BCI = dyn_cast(V))
440 return GetStringLengthH(BCI->getOperand(0), PHIs);
441
442 // If this is a PHI node, there are two cases: either we have already seen it
443 // or we haven't.
444 if (PHINode *PN = dyn_cast(V)) {
445 if (!PHIs.insert(PN))
446 return ~0ULL; // already in the set.
447
448 // If it was new, see if all the input strings are the same length.
449 uint64_t LenSoFar = ~0ULL;
450 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
451 uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs);
452 if (Len == 0) return 0; // Unknown length -> unknown.
453
454 if (Len == ~0ULL) continue;
455
456 if (Len != LenSoFar && LenSoFar != ~0ULL)
457 return 0; // Disagree -> unknown.
458 LenSoFar = Len;
459 }
460
461 // Success, all agree.
462 return LenSoFar;
463 }
464
465 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
466 if (SelectInst *SI = dyn_cast(V)) {
467 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
468 if (Len1 == 0) return 0;
469 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
470 if (Len2 == 0) return 0;
471 if (Len1 == ~0ULL) return Len2;
472 if (Len2 == ~0ULL) return Len1;
473 if (Len1 != Len2) return 0;
474 return Len1;
475 }
476
477 // If the value is not a GEP instruction nor a constant expression with a
478 // GEP instruction, then return unknown.
479 User *GEP = 0;
480 if (GetElementPtrInst *GEPI = dyn_cast(V)) {
481 GEP = GEPI;
482 } else if (ConstantExpr *CE = dyn_cast(V)) {
483 if (CE->getOpcode() != Instruction::GetElementPtr)
484 return 0;
485 GEP = CE;
486 } else {
487 return 0;
488 }
489
490 // Make sure the GEP has exactly three arguments.
491 if (GEP->getNumOperands() != 3)
492 return 0;
493
494 // Check to make sure that the first operand of the GEP is an integer and
495 // has value 0 so that we are sure we're indexing into the initializer.
496 if (ConstantInt *Idx = dyn_cast(GEP->getOperand(1))) {
497 if (!Idx->isZero())
498 return 0;
499 } else
500 return 0;
501
502 // If the second index isn't a ConstantInt, then this is a variable index
503 // into the array. If this occurs, we can't say anything meaningful about
504 // the string.
505 uint64_t StartIdx = 0;
506 if (ConstantInt *CI = dyn_cast(GEP->getOperand(2)))
507 StartIdx = CI->getZExtValue();
508 else
509 return 0;
510
511 // The GEP instruction, constant or instruction, must reference a global
512 // variable that is a constant and is initialized. The referenced constant
513 // initializer is the array that we'll use for optimization.
514 GlobalVariable* GV = dyn_cast(GEP->getOperand(0));
515 if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
516 GV->mayBeOverridden())
517 return 0;
518 Constant *GlobalInit = GV->getInitializer();
519
520 // Handle the ConstantAggregateZero case, which is a degenerate case. The
521 // initializer is constant zero so the length of the string must be zero.
522 if (isa(GlobalInit))
523 return 1; // Len = 0 offset by 1.
524
525 // Must be a Constant Array
526 ConstantArray *Array = dyn_cast(GlobalInit);
527 if (!Array || !Array->getType()->getElementType()->isIntegerTy(8))
528 return false;
529
530 // Get the number of elements in the array
531 uint64_t NumElts = Array->getType()->getNumElements();
532
533 // Traverse the constant array from StartIdx (derived above) which is
534 // the place the GEP refers to in the array.
535 for (unsigned i = StartIdx; i != NumElts; ++i) {
536 Constant *Elt = Array->getOperand(i);
537 ConstantInt *CI = dyn_cast(Elt);
538 if (!CI) // This array isn't suitable, non-int initializer.
539 return 0;
540 if (CI->isZero())
541 return i-StartIdx+1; // We found end of string, success!
542 }
543
544 return 0; // The array isn't null terminated, conservatively return 'unknown'.
545 }
546
547 /// GetStringLength - If we can compute the length of the string pointed to by
548 /// the specified pointer, return 'len+1'. If we can't, return 0.
549 static uint64_t GetStringLength(Value *V) {
550 if (!V->getType()->isPointerTy()) return 0;
551
552 SmallPtrSet PHIs;
553 uint64_t Len = GetStringLengthH(V, PHIs);
554 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
555 // an empty string as a length.
556 return Len == ~0ULL ? 1 : Len;
557 }
558434
559435 /// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
560436 /// value is equal or not-equal to zero.