Index: lib/Target/ARM/ARMConstantIslandPass.cpp =================================================================== --- lib/Target/ARM/ARMConstantIslandPass.cpp +++ lib/Target/ARM/ARMConstantIslandPass.cpp @@ -2130,6 +2130,13 @@ // %idx = tLSLri %idx, 2 // %base = tLEApcrelJT // %t = tLDRr %idx, %base + // + // 1) %base and %idx cannot be the same reg as during expansion, they + // need to be in different registers. + // 2) %base cannot be redefined after %t. Otherwise, it becomes + // %base = + // %base = + // tBB %base, %idx unsigned BaseReg = User.MI->getOperand(0).getReg(); if (User.MI->getIterator() == User.MI->getParent()->begin()) @@ -2141,6 +2148,8 @@ continue; IdxReg = Shift->getOperand(2).getReg(); unsigned ShiftedIdxReg = Shift->getOperand(0).getReg(); + if (BaseReg == IdxReg) + continue; // It's important that IdxReg is live until the actual TBB/TBH. Most of // the range is checked later, but the LEA might still clobber it and not @@ -2158,6 +2167,12 @@ // If we're in PIC mode, there should be another ADD following. auto *TRI = STI->getRegisterInfo(); + + // %base cannot be redefined after the load as it will appear before + // TBB/TBH. + if (registerDefinedBetween(BaseReg, Load->getNextNode(), MBB->end(), TRI)) + continue; + if (isPositionIndependentOrROPI) { MachineInstr *Add = Load->getNextNode(); if (Add->getOpcode() != ARM::tADDrr ||