Index: gcc/llvm-backend.cpp
===================================================================
--- gcc/llvm-backend.cpp	(revision 63481)
+++ gcc/llvm-backend.cpp	(working copy)
@@ -351,10 +351,13 @@
   CodeGenPasses     = 0;
 }
 
+// keep this in sync with TYPE_OVERFLOW_UNDEFINED(char*)
+#define POINTER_OVERFLOW_UNDEFINED (!flag_wrapv && !flag_trapv &&\
+                                    flag_strict_overflow)
+
 static void createPerFunctionOptimizationPasses() {
   if (PerFunctionPasses) 
     return;
-
   // Create and set up the per-function pass manager.
   // FIXME: Move the code generator to be function-at-a-time.
   PerFunctionPasses =
@@ -375,7 +378,7 @@
       PerFunctionPasses->add(createPromoteMemoryToRegisterPass());
     else
       PerFunctionPasses->add(createScalarReplAggregatesPass());
-    PerFunctionPasses->add(createInstructionCombiningPass());
+    PerFunctionPasses->add(createInstructionCombiningPass(POINTER_OVERFLOW_UNDEFINED));
   }
 
   // If there are no module-level passes that have to be run, we codegen as
@@ -455,7 +458,7 @@
         PM->add(createIPConstantPropagationPass()); // IP Constant Propagation
         PM->add(createDeadArgEliminationPass());  // Dead argument elimination
       }
-      PM->add(createInstructionCombiningPass());  // Clean up after IPCP & DAE
+      PM->add(createInstructionCombiningPass(POINTER_OVERFLOW_UNDEFINED));  // Clean up after IPCP & DAE
       PM->add(createCFGSimplificationPass());     // Clean up after IPCP & DAE
       if (flag_unit_at_a_time) {
         if (flag_exceptions)
@@ -470,11 +473,11 @@
         PM->add(createArgumentPromotionPass());   // Scalarize uninlined fn args
       if (!flag_no_simplify_libcalls)
         PM->add(createSimplifyLibCallsPass());    // Library Call Optimizations
-      PM->add(createInstructionCombiningPass());  // Cleanup for scalarrepl.
+      PM->add(createInstructionCombiningPass(POINTER_OVERFLOW_UNDEFINED));  // Cleanup for scalarrepl.
       PM->add(createJumpThreadingPass());         // Thread jumps.
       PM->add(createCFGSimplificationPass());     // Merge & remove BBs
       PM->add(createScalarReplAggregatesPass());  // Break up aggregate allocas
-      PM->add(createInstructionCombiningPass());  // Combine silly seq's
+      PM->add(createInstructionCombiningPass(POINTER_OVERFLOW_UNDEFINED));  // Combine silly seq's
       PM->add(createCondPropagationPass());       // Propagate conditionals
       PM->add(createTailCallEliminationPass());   // Eliminate tail calls
       PM->add(createCFGSimplificationPass());     // Merge & remove BBs
@@ -484,19 +487,19 @@
       // At -O2, loop unswitch should not increase code size.
       PM->add(createLoopUnswitchPass(optimize_size || optimize < 3));
       PM->add(createLoopIndexSplitPass());        // Split loop index
-      PM->add(createInstructionCombiningPass());  
+      PM->add(createInstructionCombiningPass(POINTER_OVERFLOW_UNDEFINED));  
       PM->add(createIndVarSimplifyPass());        // Canonicalize indvars
       PM->add(createLoopDeletionPass());          // Delete dead loops
       if (flag_unroll_loops)
         PM->add(createLoopUnrollPass());          // Unroll small loops
-      PM->add(createInstructionCombiningPass());  // Clean up after the unroller
+      PM->add(createInstructionCombiningPass(POINTER_OVERFLOW_UNDEFINED));  // Clean up after the unroller
       PM->add(createGVNPass());                   // Remove redundancies
       PM->add(createMemCpyOptPass());             // Remove memcpy / form memset
       PM->add(createSCCPPass());                  // Constant prop with SCCP
     
       // Run instcombine after redundancy elimination to exploit opportunities
       // opened up by them.
-      PM->add(createInstructionCombiningPass());
+      PM->add(createInstructionCombiningPass(POINTER_OVERFLOW_UNDEFINED));
       PM->add(createCondPropagationPass());       // Propagate conditionals
       PM->add(createDeadStoreEliminationPass());  // Delete dead stores
       PM->add(createAggressiveDCEPass());         // Delete dead instructions
Index: lib/Transforms/Scalar/InstructionCombining.cpp
===================================================================
--- lib/Transforms/Scalar/InstructionCombining.cpp	(revision 62998)
+++ lib/Transforms/Scalar/InstructionCombining.cpp	(working copy)
@@ -45,6 +45,7 @@
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Support/CallSite.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/ConstantRange.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/GetElementPtrTypeIterator.h"
@@ -63,6 +64,10 @@
 using namespace llvm;
 using namespace llvm::PatternMatch;
 
+static cl::opt<bool>
+StrictPointerOverflowFlag("strict-pointer-overflow", cl::init(true),
+        cl::desc("Assume that pointer arithmetic with positive integers cannot overflow (default)"));
+
 STATISTIC(NumCombined , "Number of insts combined");
 STATISTIC(NumConstProp, "Number of constant folds");
 STATISTIC(NumDeadInst , "Number of dead inst eliminated");
@@ -78,9 +83,13 @@
     DenseMap<Instruction*, unsigned> WorklistMap;
     TargetData *TD;
     bool MustPreserveLCSSA;
+    // strict-overflow: assume that pointer + unsigned value never overflows
+    bool StrictPointerOverflow;
   public:
     static char ID; // Pass identification, replacement for typeid
-    InstCombiner() : FunctionPass(&ID) {}
+    InstCombiner(bool StrictPointerOverflow = true) : FunctionPass(&ID), 
+      StrictPointerOverflow(StrictPointerOverflow && StrictPointerOverflowFlag)
+        {}
 
     /// AddToWorkList - Add the specified instruction to the worklist if it
     /// isn't already in it.
@@ -5319,7 +5328,7 @@
     RHS = BCI->getOperand(0);
 
   Value *PtrBase = GEPLHS->getOperand(0);
-  if (PtrBase == RHS) {
+  if (PtrBase == RHS && StrictPointerOverflow) {
     // ((gep Ptr, OFFSET) cmp Ptr)   ---> (OFFSET cmp 0).
     // This transformation (ignoring the base and scales) is valid because we
     // know pointers can't overflow.  See if we can output an optimized form.
@@ -12513,6 +12522,7 @@
   return EverMadeChange;
 }
 
-FunctionPass *llvm::createInstructionCombiningPass() {
-  return new InstCombiner();
+FunctionPass *llvm::createInstructionCombiningPass(bool
+                                                   StrictPointerOverflow) {
+  return new InstCombiner(StrictPointerOverflow);
 }
