@@ -1409,17 +1409,15 @@ static bool isDispSafeForFrameIndex(int64_t Val) {
14091409
14101410bool X86DAGToDAGISel::foldOffsetIntoAddress (uint64_t Offset,
14111411 X86ISelAddressMode &AM) {
1412- // If the final displacement is 0, we don't need to do any work. We may have
1413- // already matched a displacement and the caller just added the symbolic
1414- // displacement with an offset of 0. So recheck everything if Val is non-zero.
1415- int64_t Val = AM.Disp + Offset;
1416- if (Val == 0 )
1412+ // If there's no offset to fold, we don't need to do any work.
1413+ if (Offset == 0 )
14171414 return false ;
14181415
14191416 // Cannot combine ExternalSymbol displacements with integer offsets.
14201417 if (AM.ES || AM.MCSym )
14211418 return true ;
14221419
1420+ int64_t Val = AM.Disp + Offset;
14231421 CodeModel::Model M = TM.getCodeModel ();
14241422 if (Subtarget->is64Bit ()) {
14251423 if (!X86::isOffsetSuitableForCodeModel (Val, M,
@@ -1583,13 +1581,24 @@ bool X86DAGToDAGISel::matchAdd(SDValue &N, X86ISelAddressMode &AM,
15831581 if (!matchAddressRecursively (N.getOperand (0 ), AM, Depth+1 ) &&
15841582 !matchAddressRecursively (Handle.getValue ().getOperand (1 ), AM, Depth+1 ))
15851583 return false ;
1586- AM = Backup;
15871584
1588- // Try again after commutating the operands.
1589- if (!matchAddressRecursively (Handle.getValue ().getOperand (1 ), AM,
1590- Depth + 1 ) &&
1591- !matchAddressRecursively (Handle.getValue ().getOperand (0 ), AM, Depth + 1 ))
1592- return false ;
1585+ // Don't try commuting operands if the address is in the form of
1586+ // sym+disp(%rip). foldOffsetIntoAddress() currently does not know there is a
1587+ // symbolic displacement and would fold disp. If disp is just a bit smaller
1588+ // than 2**31, it can easily cause a relocation overflow.
1589+ bool NoCommutate = false ;
1590+ if (AM.isRIPRelative () && AM.hasSymbolicDisplacement ())
1591+ if (ConstantSDNode *Cst =
1592+ dyn_cast<ConstantSDNode>(Handle.getValue ().getOperand (1 )))
1593+ NoCommutate = Cst->getSExtValue () != 0 ;
1594+
1595+ AM = Backup;
1596+ if (!NoCommutate) {
1597+ // Try again after commutating the operands.
1598+ if (!matchAddressRecursively (Handle.getValue ().getOperand (1 ), AM, Depth + 1 ) &&
1599+ !matchAddressRecursively (Handle.getValue ().getOperand (0 ), AM, Depth + 1 ))
1600+ return false ;
1601+ }
15931602 AM = Backup;
15941603
15951604 // If we couldn't fold both operands into the address at the same time,
0 commit comments