================
@@ -13307,6 +13308,112 @@ void SelectionDAGBuilder::visitVectorSplice(const
CallInst &I) {
setValue(&I, DAG.getVectorShuffle(VT, DL, V1, V2, Mask));
}
+/// Read each physical register that \p V copies from at the top of \p MBB, an
+/// indirect target of a callbr, and make \p V use those reads.
+///
+/// The registers carry the asm's outputs across the edge into \p MBB, so, like
+/// an EH pad's exception registers, they're live-in and have to be read before
+/// anything in the block clobbers them. A CopyFromReg in the DAG doesn't
ensure
+/// that: the scheduler can put an instruction that sets EFLAGS, for example,
+/// ahead of the read of an x86 flag output. A COPY emitted at the top of the
+/// block, before the DAG's instructions, does.
+static void readPhysRegsAtBlockEntry(SDValue &V, MachineBasicBlock &MBB,
+ SelectionDAG &DAG, const SDLoc &DL) {
+ // Find the reads of physical registers that V depends on. These are reads of
+ // the registers an output is in, or of one that the target's lowering picks,
+ // such as EFLAGS for an x86 flag output ("=@ccz").
+ SmallVector<SDNode *, 4> Worklist = {V.getNode()};
+ SmallPtrSet<SDNode *, 4> Visited;
+ SmallVector<SDNode *, 4> PhysRegReads;
+ while (!Worklist.empty()) {
+ SDNode *N = Worklist.pop_back_val();
+ if (!Visited.insert(N).second)
+ continue;
+
+ if (N->getOpcode() == ISD::CopyFromReg) {
+ if (cast<RegisterSDNode>(N->getOperand(1))->getReg().isPhysical())
+ PhysRegReads.push_back(N);
+ continue;
+ }
+
+ // Follow only the operands that the value depends on. The chain leads to
+ // the rest of the block, not to what this output reads.
+ for (SDValue Op : N->op_values())
+ if (Op.getValueType() != MVT::Other)
+ Worklist.push_back(Op.getNode());
+ }
+
+ // V may itself be one of the reads, such as for an output in one register,
+ // so keep track of it while they're replaced.
+ HandleSDNode Handle(V);
+ const TargetLowering &TLI = DAG.getTargetLoweringInfo();
+ const TargetRegisterInfo *TRI = DAG.getSubtarget().getRegisterInfo();
+
+ // The walk found the reads last operand first. Copy them in operand order,
+ // which is the order of an output's registers.
+ for (SDNode *N : reverse(PhysRegReads)) {
+ assert(N->getNumOperands() == 2 && "Unexpected glue on a callbr output");
+ MCRegister PhysReg =
+ cast<RegisterSDNode>(N->getOperand(1))->getReg().asMCReg();
+ MVT VT = N->getSimpleValueType(0);
+
+ // Copy a register that can't be copied directly, such as EFLAGS, through
+ // the class that the target copies it to. The use of the copy then
+ // restores it, which the target lowers.
+ const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(PhysReg);
+ bool CrossCopy = RC->expensiveOrImpossibleToCopy();
+ if (CrossCopy)
+ RC = TRI->getCrossCopyRegClass(RC);
+ else if (const TargetRegisterClass *VTRC = TLI.getRegClassFor(VT);
+ VTRC->contains(PhysReg))
+ RC = VTRC;
+
+ // This marks PhysReg live-in, once, and reuses the copy if another output
+ // reads the same register, such as a second flag output.
+ Register VReg = MBB.addLiveIn(PhysReg, RC);
+
+ // The copy kills PhysReg. When nothing clobbers PhysReg before the
+ // restore, the peephole optimizer removes the restore, so the read after
+ // it then uses PhysReg past that kill.
+ if (CrossCopy)
+ DAG.getMachineFunction()
+ .getRegInfo()
+ .getVRegDef(VReg)
+ ->clearRegisterKills(PhysReg, TRI);
----------------
isanbard wrote:
The `MachineBasicBlock::addLiveIn()` does add a kill flag, because it builds a
`COPY` instruction with one. But there seem to be opportunities for stale kill
flags. For instance, the `PeepholeOptimizer::foldRedundantNAPhysCopy()` erases
the restore when nothing clobbers the register in between:
```llvm
%v:gr32 = COPY killed $eflags ; from addLiveIn()
$eflags = COPY %v ; the restore: erased
SETCCr ..., implicit $eflags ; now reads $eflags past the kill
```
It doesn't clear the kill on the first `COPY`, even though that extends
`$eflags`'s live range. That method could be fixed to clear the flag when it
erases the restore.
https://github.com/llvm/llvm-project/pull/239115
_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits