diff --git a/bolt/lib/Target/X86/X86MCPlusBuilder.cpp b/bolt/lib/Target/X86/X86MCPlusBuilder.cpp --- a/bolt/lib/Target/X86/X86MCPlusBuilder.cpp +++ b/bolt/lib/Target/X86/X86MCPlusBuilder.cpp @@ -1747,10 +1747,35 @@ } } + Inst.setOpcode(NewOpcode); + + // Replace `movl $0x0, %eax` with `xorl %eax, %eax` + switch (NewOpcode) { + default: + break; + + case X86::MOV64ri: + case X86::MOV64ri32: + case X86::MOV32ri: + auto OpNum = MCPlus::getNumPrimeOperands(Inst) - 1; + if (Inst.getOperand(OpNum).isImm() && !Inst.getOperand(OpNum).getImm()) { + if (NewOpcode == X86::MOV32ri) + NewOpcode = X86::XOR32rr; + else + NewOpcode = X86::XOR64rr; + MCOperand Op = Inst.getOperand(0); + Inst.setOpcode(NewOpcode); + Inst.clear(); + Inst.addOperand(Op); + Inst.addOperand(Op); + Inst.addOperand(Op); + } + break; + } + if (NewOpcode == OldOpcode) return false; - Inst.setOpcode(NewOpcode); return true; }