@@ -7635,7 +7635,6 @@ void PPCDAGToDAGISel::PeepholePPC64() {
76357635 break ;
76367636 case PPC::ADDItoc:
76377637 case PPC::ADDItoc8:
7638- ReplaceFlags = false ;
76397638 if (RequiresMod4Offset) {
76407639 if (GlobalAddressSDNode *GA =
76417640 dyn_cast<GlobalAddressSDNode>(Base.getOperand (0 ))) {
@@ -7650,58 +7649,42 @@ void PPCDAGToDAGISel::PeepholePPC64() {
76507649 break ;
76517650 }
76527651
7653- const unsigned BaseOpcode = Base.getMachineOpcode ();
7654- // ADDItoc and ADDItoc8 are pseudos used exclusively by AIX small code
7655- // model when a global is defined in the TOC.
7656- const bool OpcodeIsAIXSmallTocData =
7657- BaseOpcode == PPC::ADDItoc || BaseOpcode == PPC::ADDItoc8;
7652+ SDValue ImmOpnd = Base.getOperand (1 );
76587653
7659- SDValue RegOperand;
7660- SDValue ImmOpnd;
7661- // The AIX small code model nodes have the operands reversed.
7662- if (OpcodeIsAIXSmallTocData) {
7663- RegOperand = Base. getOperand ( 1 ) ;
7664- ImmOpnd = Base. getOperand ( 0 );
7665- } else {
7666- RegOperand = Base. getOperand ( 0 );
7667- ImmOpnd = Base. getOperand ( 1 );
7654+ // On PPC64, the TOC base pointer is guaranteed by the ABI only to have
7655+ // 8-byte alignment, and so we can only use offsets less than 8 (otherwise,
7656+ // we might have needed different @ha relocation values for the offset
7657+ // pointers).
7658+ int MaxDisplacement = 7 ;
7659+ if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
7660+ const GlobalValue *GV = GA-> getGlobal ();
7661+ Align Alignment = GV-> getPointerAlignment (CurDAG-> getDataLayout () );
7662+ MaxDisplacement = std::min (( int )Alignment. value () - 1 , MaxDisplacement );
76687663 }
76697664
7670- int Offset = N->getConstantOperandVal (FirstOp);
7671-
7672- SDValue HBase;
76737665 bool UpdateHBase = false ;
7674- if (ReplaceFlags) {
7675- // On PPC64, the TOC base pointer is guaranteed by the ABI only to have
7676- // 8-byte alignment, and so we can only use offsets less than 8
7677- // (otherwise, we might have needed different @ha relocation values for
7678- // the offset pointers).
7679- int MaxDisplacement = 7 ;
7680- if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
7681- const GlobalValue *GV = GA->getGlobal ();
7682- Align Alignment = GV->getPointerAlignment (CurDAG->getDataLayout ());
7683- MaxDisplacement = std::min ((int )Alignment.value () - 1 , MaxDisplacement);
7684- }
7666+ SDValue HBase = Base.getOperand (0 );
76857667
7668+ int Offset = N->getConstantOperandVal (FirstOp);
7669+ if (ReplaceFlags) {
76867670 if (Offset < 0 || Offset > MaxDisplacement) {
76877671 // If we have a addi(toc@l)/addis(toc@ha) pair, and the addis has only
76887672 // one use, then we can do this for any offset, we just need to also
76897673 // update the offset (i.e. the symbol addend) on the addis also.
7690- if (BaseOpcode != PPC::ADDItocL)
7674+ if (Base. getMachineOpcode () != PPC::ADDItocL)
76917675 continue ;
76927676
7693- if (!RegOperand .isMachineOpcode () ||
7694- RegOperand .getMachineOpcode () != PPC::ADDIStocHA8)
7677+ if (!HBase .isMachineOpcode () ||
7678+ HBase .getMachineOpcode () != PPC::ADDIStocHA8)
76957679 continue ;
76967680
7697- if (!Base.hasOneUse () || !RegOperand .hasOneUse ())
7681+ if (!Base.hasOneUse () || !HBase .hasOneUse ())
76987682 continue ;
76997683
7700- SDValue HImmOpnd = RegOperand .getOperand (1 );
7684+ SDValue HImmOpnd = HBase .getOperand (1 );
77017685 if (HImmOpnd != ImmOpnd)
77027686 continue ;
77037687
7704- HBase = RegOperand;
77057688 UpdateHBase = true ;
77067689 }
77077690 } else {
@@ -7726,10 +7709,10 @@ void PPCDAGToDAGISel::PeepholePPC64() {
77267709 }
77277710 }
77287711
7729- // We found an opportunity. Forward the operands from the add
7730- // immediate to the load or store. If needed, update the target
7731- // flags for the immediate operand to reflect the necessary
7732- // relocation information.
7712+ // We found an opportunity. Reverse the operands from the add
7713+ // immediate and substitute them into the load or store. If
7714+ // needed, update the target flags for the immediate operand to
7715+ // reflect the necessary relocation information.
77337716 LLVM_DEBUG (dbgs () << " Folding add-immediate into mem-op:\n Base: " );
77347717 LLVM_DEBUG (Base->dump (CurDAG));
77357718 LLVM_DEBUG (dbgs () << " \n N: " );
@@ -7745,10 +7728,6 @@ void PPCDAGToDAGISel::PeepholePPC64() {
77457728 Align Alignment = GV->getPointerAlignment (CurDAG->getDataLayout ());
77467729 // We can't perform this optimization for data whose alignment
77477730 // is insufficient for the instruction encoding.
7748- // TODO FIXME Verify and document why the offset must be a multiple of
7749- // 4 when the aligment is less than 4. It is not about the encoding of
7750- // the instruction: the value of Offset comes directly from the original
7751- // load/store instruction on the path that reaches this check.
77527731 if (Alignment < 4 && (RequiresMod4Offset || (Offset % 4 ) != 0 )) {
77537732 LLVM_DEBUG (dbgs () << " Rejected this candidate for alignment.\n\n " );
77547733 continue ;
@@ -7762,12 +7741,27 @@ void PPCDAGToDAGISel::PeepholePPC64() {
77627741 }
77637742 }
77647743
7744+ const unsigned BaseOpcode = Base.getMachineOpcode ();
7745+ // ADDItoc and ADDItoc8 are pseudos used exclusively by AIX small code
7746+ // model when a global is defined in the TOC.
7747+ const bool OpcodeIsAIXTocData =
7748+ BaseOpcode == PPC::ADDItoc || BaseOpcode == PPC::ADDItoc8;
7749+
77657750 if (FirstOp == 1 ) // Store
7766- (void )CurDAG->UpdateNodeOperands (N, N->getOperand (0 ), ImmOpnd, RegOperand,
7767- N->getOperand (3 ));
7751+ if (OpcodeIsAIXTocData)
7752+ (void )CurDAG->UpdateNodeOperands (N, N->getOperand (0 ),
7753+ Base.getOperand (0 ), Base.getOperand (1 ),
7754+ N->getOperand (3 ));
7755+ else
7756+ (void )CurDAG->UpdateNodeOperands (N, N->getOperand (0 ), ImmOpnd,
7757+ Base.getOperand (0 ), N->getOperand (3 ));
77687758 else // Load
7769- (void )CurDAG->UpdateNodeOperands (N, ImmOpnd, RegOperand,
7770- N->getOperand (2 ));
7759+ if (OpcodeIsAIXTocData)
7760+ (void )CurDAG->UpdateNodeOperands (N, Base.getOperand (0 ),
7761+ Base.getOperand (1 ), N->getOperand (2 ));
7762+ else
7763+ (void )CurDAG->UpdateNodeOperands (N, ImmOpnd, Base.getOperand (0 ),
7764+ N->getOperand (2 ));
77717765
77727766 if (UpdateHBase)
77737767 (void )CurDAG->UpdateNodeOperands (HBase.getNode (), HBase.getOperand (0 ),
0 commit comments