[llvm-commits] [llvm] r56686 - /llvm/trunk/lib/Target/X86/X86FastISel.cpp

Dan Gohman gohman at apple.com
Fri Sep 26 13:04:17 PDT 2008


Author: djg
Date: Fri Sep 26 15:04:15 2008
New Revision: 56686

URL: http://llvm.org/viewvc/llvm-project?rev=56686&view=rev
Log:
Fix X86FastISel's address folding to check displacement
values for overflow.

Modified:
    llvm/trunk/lib/Target/X86/X86FastISel.cpp

Modified: llvm/trunk/lib/Target/X86/X86FastISel.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86FastISel.cpp?rev=56686&r1=56685&r2=56686&view=diff

==============================================================================
--- llvm/trunk/lib/Target/X86/X86FastISel.cpp (original)
+++ llvm/trunk/lib/Target/X86/X86FastISel.cpp Fri Sep 26 15:04:15 2008
@@ -345,8 +345,12 @@
     if (isCall) break;
     // Adds of constants are common and easy enough.
     if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
-      AM.Disp += CI->getZExtValue();
-      return X86SelectAddress(U->getOperand(0), AM, isCall);
+      uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
+      // They have to fit in the 32-bit signed displacement field though.
+      if (isInt32(Disp)) {
+        AM.Disp = (uint32_t)Disp;
+        return X86SelectAddress(U->getOperand(0), AM, isCall);
+      }
     }
     break;
   }
@@ -354,7 +358,7 @@
   case Instruction::GetElementPtr: {
     if (isCall) break;
     // Pattern-match simple GEPs.
-    uint64_t Disp = AM.Disp;
+    uint64_t Disp = (int32_t)AM.Disp;
     unsigned IndexReg = AM.IndexReg;
     unsigned Scale = AM.Scale;
     gep_type_iterator GTI = gep_type_begin(U);
@@ -371,7 +375,7 @@
         uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
         if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
           // Constant-offset addressing.
-          Disp += CI->getZExtValue() * S;
+          Disp += CI->getSExtValue() * S;
         } else if (IndexReg == 0 &&
                    (!AM.GV ||
                     !getTargetMachine()->symbolicAddressesAreRIPRel()) &&
@@ -386,11 +390,14 @@
           goto unsupported_gep;
       }
     }
+    // Check for displacement overflow.
+    if (!isInt32(Disp))
+      break;
     // Ok, the GEP indices were covered by constant-offset and scaled-index
     // addressing. Update the address state and move on to examining the base.
     AM.IndexReg = IndexReg;
     AM.Scale = Scale;
-    AM.Disp = Disp;
+    AM.Disp = (uint32_t)Disp;
     return X86SelectAddress(U->getOperand(0), AM, isCall);
   unsupported_gep:
     // Ok, the GEP indices weren't all covered.





More information about the llvm-commits mailing list