Skip to content

Commit d02978f

Browse files
committed
c: Add attribute to use reverse XOR
1 parent 7118e18 commit d02978f

File tree

8 files changed

+30
-5
lines changed

8 files changed

+30
-5
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4946,3 +4946,10 @@ def MOV32rr_REV: InheritableAttr {
49464946
let Documentation = [Undocumented];
49474947
let SimpleHandler = 1;
49484948
}
4949+
4950+
def XOR32rr_REV: InheritableAttr {
4951+
let Spellings = [GNU<"XOR32rr_REV">];
4952+
let Subjects = SubjectList<[Function]>;
4953+
let Documentation = [Undocumented];
4954+
let SimpleHandler = 1;
4955+
}

clang/lib/CodeGen/CGCall.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,6 +2391,8 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
23912391
FuncAttrs.addAttribute(llvm::Attribute::Convergent);
23922392
if (TargetDecl->hasAttr<MOV32rr_REVAttr>())
23932393
FuncAttrs.addAttribute(llvm::Attribute::MOV32rr_REV);
2394+
if (TargetDecl->hasAttr<XOR32rr_REVAttr>())
2395+
FuncAttrs.addAttribute(llvm::Attribute::XOR32rr_REV);
23942396

23952397
if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
23962398
AddAttributesFromFunctionProtoType(

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7355,6 +7355,10 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
73557355
case clang::ParsedAttr::AT_MOV32rr_REV:
73567356
handleSimpleAttribute<MOV32rr_REVAttr>(S, D, AL);
73577357
break;
7358+
7359+
case clang::ParsedAttr::AT_XOR32rr_REV:
7360+
handleSimpleAttribute<XOR32rr_REVAttr>(S, D, AL);
7361+
break;
73587362
}
73597363
}
73607364

llvm/include/llvm/Bitcode/LLVMBitCodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ enum AttributeKindCodes {
788788
ATTR_KIND_NO_EXT = 99,
789789
ATTR_KIND_NO_DIVERGENCE_SOURCE = 100,
790790
ATTR_MOV32rr_REV = 101,
791+
ATTR_XOR32rr_REV = 102,
791792
};
792793

793794
enum ComdatSelectionKindCodes {

llvm/include/llvm/IR/Attributes.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,5 +467,6 @@ def : MergeRule<"adjustMinLegalVectorWidth">;
467467
def : MergeRule<"adjustNullPointerValidAttr">;
468468
def : MergeRule<"setAND<MustProgressAttr>">;
469469

470-
/// Use alternative MOV instruction opcode.
470+
/// Use alternative MOV and XOR instruction opcode.
471471
def MOV32rr_REV : EnumAttr<"MOV32rr_REV", IntersectPreserve, [FnAttr]>;
472+
def XOR32rr_REV : EnumAttr<"XOR32rr_REV", IntersectPreserve, [FnAttr]>;

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,8 @@ static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
907907
return bitc::ATTR_KIND_NO_EXT;
908908
case Attribute::MOV32rr_REV:
909909
return bitc::ATTR_MOV32rr_REV;
910+
case Attribute::XOR32rr_REV:
911+
return bitc::ATTR_XOR32rr_REV;
910912
case Attribute::EndAttrKinds:
911913
llvm_unreachable("Can not encode end-attribute kinds marker.");
912914
case Attribute::None:

llvm/lib/Target/X86/X86InstrInfo.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5973,9 +5973,11 @@ static bool expandMOV32r1(MachineInstrBuilder &MIB, const TargetInstrInfo &TII,
59735973
MachineBasicBlock &MBB = *MIB->getParent();
59745974
const DebugLoc &DL = MIB->getDebugLoc();
59755975
Register Reg = MIB.getReg(0);
5976+
MachineFunction &MF = *MBB.getParent();
5977+
const Function &F = MF.getFunction();
59765978

59775979
// Insert the XOR.
5978-
BuildMI(MBB, MIB.getInstr(), DL, TII.get(X86::XOR32rr), Reg)
5980+
BuildMI(MBB, MIB.getInstr(), DL, TII.get((F.hasFnAttribute(Attribute::AttrKind::XOR32rr_REV) ? X86::XOR32rr_REV : X86::XOR32rr)), Reg)
59795981
.addReg(Reg, RegState::Undef)
59805982
.addReg(Reg, RegState::Undef);
59815983

@@ -6075,10 +6077,11 @@ static void expandLoadStackGuard(MachineInstrBuilder &MIB,
60756077
static bool expandXorFP(MachineInstrBuilder &MIB, const TargetInstrInfo &TII) {
60766078
MachineBasicBlock &MBB = *MIB->getParent();
60776079
MachineFunction &MF = *MBB.getParent();
6080+
const Function &F = MF.getFunction();
60786081
const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>();
60796082
const X86RegisterInfo *TRI = Subtarget.getRegisterInfo();
60806083
unsigned XorOp =
6081-
MIB->getOpcode() == X86::XOR64_FP ? X86::XOR64rr : X86::XOR32rr;
6084+
MIB->getOpcode() == X86::XOR64_FP ? X86::XOR64rr : (F.hasFnAttribute(Attribute::AttrKind::XOR32rr_REV) ? X86::XOR32rr_REV : X86::XOR32rr);
60826085
MIB->setDesc(TII.get(XorOp));
60836086
MIB.addReg(TRI->getFrameRegister(MF), RegState::Undef);
60846087
return true;
@@ -6144,10 +6147,14 @@ static bool expandSHXDROT(MachineInstrBuilder &MIB, const MCInstrDesc &Desc) {
61446147

61456148
bool X86InstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
61466149
bool HasAVX = Subtarget.hasAVX();
6147-
MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
6150+
6151+
MachineBasicBlock &MBB = *MI.getParent();
6152+
MachineFunction &MF = *MBB.getParent();
6153+
MachineInstrBuilder MIB(MF, MI);
6154+
const Function &F = MF.getFunction();
61486155
switch (MI.getOpcode()) {
61496156
case X86::MOV32r0:
6150-
return Expand2AddrUndef(MIB, get(X86::XOR32rr));
6157+
return Expand2AddrUndef(MIB, get(F.hasFnAttribute(Attribute::AttrKind::XOR32rr_REV) ? X86::XOR32rr_REV : X86::XOR32rr));
61516158
case X86::MOV32r1:
61526159
return expandMOV32r1(MIB, *this, /*MinusOne=*/false);
61536160
case X86::MOV32r_1:

llvm/lib/Transforms/Utils/CodeExtractor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,7 @@ Function *CodeExtractor::constructFunctionDeclaration(
951951
case Attribute::NoProfile:
952952
case Attribute::SkipProfile:
953953
case Attribute::MOV32rr_REV:
954+
case Attribute::XOR32rr_REV:
954955
break;
955956
// These attributes cannot be applied to functions.
956957
case Attribute::Alignment:

0 commit comments

Comments
 (0)