Skip to content

Commit 27723bb

Browse files
[CodeGenPrepare] Fix signed overflow
1 parent 1cf5466 commit 27723bb

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5447,11 +5447,17 @@ bool AddressingModeMatcher::matchAddr(Value *Addr, unsigned Depth) {
54475447
TPT.getRestorationPoint();
54485448
if (ConstantInt *CI = dyn_cast<ConstantInt>(Addr)) {
54495449
if (CI->getValue().isSignedIntN(64)) {
5450-
// Fold in immediates if legal for the target.
5451-
AddrMode.BaseOffs += CI->getSExtValue();
5452-
if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace))
5453-
return true;
5454-
AddrMode.BaseOffs -= CI->getSExtValue();
5450+
// Check if the addition would result in a signed overflow.
5451+
int64_t Result;
5452+
bool Overflow =
5453+
AddOverflow(AddrMode.BaseOffs, CI->getSExtValue(), Result);
5454+
if (!Overflow) {
5455+
// Fold in immediates if legal for the target.
5456+
AddrMode.BaseOffs = Result;
5457+
if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace))
5458+
return true;
5459+
AddrMode.BaseOffs -= CI->getSExtValue();
5460+
}
54555461
}
54565462
} else if (GlobalValue *GV = dyn_cast<GlobalValue>(Addr)) {
54575463
// If this is a global variable, try to fold it into the addressing mode.

0 commit comments

Comments
 (0)