|
7 | 7 | #include <optional> |
8 | 8 | #include <unordered_map> |
9 | 9 |
|
| 10 | +#include <llvm/ADT/APInt.h> |
10 | 11 | #include <llvm/ADT/SmallPtrSet.h> |
11 | 12 | #include <llvm/Analysis/ValueTracking.h> |
12 | 13 | #include <llvm/BinaryFormat/Dwarf.h> |
@@ -648,6 +649,33 @@ namespace ctrace::stack::analysis |
648 | 649 |
|
649 | 650 | // The `i != N` loop guard is the one branch shape ProgramPointRanges cannot turn |
650 | 651 | // into an interval; with a constant init and step it still bounds the index. |
| 652 | + /// Shifts @p range, the bounds of x, into those of @p op, x + @p offset. Fails when a |
| 653 | + /// value of x may make the sum wrap, which a missing bound on that side allows. |
| 654 | + static bool shiftRangeWithoutWrap(IntRange& range, std::int64_t offset, |
| 655 | + const llvm::BinaryOperator& op, IntReading reading) |
| 656 | + { |
| 657 | + const unsigned width = op.getType()->getIntegerBitWidth(); |
| 658 | + if (width >= 64) |
| 659 | + return false; |
| 660 | + const std::int64_t min = reading == IntReading::Unsigned |
| 661 | + ? 0 |
| 662 | + : llvm::APInt::getSignedMinValue(width).getSExtValue(); |
| 663 | + const std::int64_t max = |
| 664 | + reading == IntReading::Unsigned |
| 665 | + ? static_cast<std::int64_t>(llvm::APInt::getMaxValue(width).getZExtValue()) |
| 666 | + : llvm::APInt::getSignedMaxValue(width).getSExtValue(); |
| 667 | + // Going down, the lowest x must stay above min; going up, the highest below max. |
| 668 | + if (offset < 0 && (!range.hasLower || range.lower + offset < min)) |
| 669 | + return false; |
| 670 | + if (offset > 0 && (!range.hasUpper || range.upper + offset > max)) |
| 671 | + return false; |
| 672 | + if (range.hasLower) |
| 673 | + range.lower += offset; |
| 674 | + if (range.hasUpper) |
| 675 | + range.upper += offset; |
| 676 | + return true; |
| 677 | + } |
| 678 | + |
651 | 679 | static std::optional<IntRange> deriveNeLoopGuardRange(const llvm::BasicBlock& target, |
652 | 680 | const llvm::Value* key) |
653 | 681 | { |
@@ -949,6 +977,29 @@ namespace ctrace::stack::analysis |
949 | 977 | } |
950 | 978 |
|
951 | 979 | // 5) Variable index case: test[i] / ptr[i] |
| 980 | + // An index computed as x + C or x - C has the bounds of x shifted by C: look |
| 981 | + // for those of x, and shift them once found. |
| 982 | + const auto* offsetOp = dyn_cast<BinaryOperator>(baseIdxVal); |
| 983 | + const auto* offsetConst = |
| 984 | + offsetOp ? dyn_cast<ConstantInt>(offsetOp->getOperand(1)) : nullptr; |
| 985 | + std::int64_t offset = 0; |
| 986 | + if (offsetConst && offsetConst->getBitWidth() <= 64 && |
| 987 | + !offsetConst->isMinValue(true) && |
| 988 | + (offsetOp->getOpcode() == Instruction::Add || |
| 989 | + offsetOp->getOpcode() == Instruction::Sub)) |
| 990 | + { |
| 991 | + offset = offsetOp->getOpcode() == Instruction::Add |
| 992 | + ? offsetConst->getSExtValue() |
| 993 | + : -offsetConst->getSExtValue(); |
| 994 | + baseIdxVal = offsetOp->getOperand(0); |
| 995 | + while (auto* cast = dyn_cast<CastInst>(baseIdxVal)) |
| 996 | + { |
| 997 | + indexReading = |
| 998 | + isa<ZExtInst>(cast) ? IntReading::Unsigned : IntReading::Signed; |
| 999 | + baseIdxVal = cast->getOperand(0); |
| 1000 | + } |
| 1001 | + } |
| 1002 | + |
952 | 1003 | // Check whether we have a range for the base value (i, not the cast) |
953 | 1004 | const Value* key = baseIdxVal; |
954 | 1005 |
|
@@ -987,6 +1038,10 @@ namespace ctrace::stack::analysis |
987 | 1038 | hasRange = R.hasLower || R.hasUpper; |
988 | 1039 | } |
989 | 1040 |
|
| 1041 | + if (hasRange && offset != 0 && |
| 1042 | + !shiftRangeWithoutWrap(R, offset, *offsetOp, indexReading)) |
| 1043 | + hasRange = false; |
| 1044 | + |
990 | 1045 | if (!hasRange) |
991 | 1046 | continue; |
992 | 1047 |
|
|
0 commit comments