https://github.com/Prabhuk updated 
https://github.com/llvm/llvm-project/pull/109320

>From 6fc7aceab6bc193616fe78e21b796efb4dbffb58 Mon Sep 17 00:00:00 2001
From: prabhukr <prabh...@google.com>
Date: Tue, 3 Sep 2024 21:02:15 -0700
Subject: [PATCH 1/2] UEFI backend for x86_64

---
 clang/lib/Basic/Targets/OSTargets.h           | 11 ++++--
 clang/lib/Basic/Targets/X86.h                 |  9 +++--
 .../X86/MCTargetDesc/X86MCTargetDesc.cpp      |  4 +-
 llvm/lib/Target/X86/X86CallingConv.td         | 17 ++++++++-
 llvm/lib/Target/X86/X86FrameLowering.cpp      | 20 ++++++++++
 llvm/lib/Target/X86/X86ISelLowering.cpp       | 37 ++++++++++++-------
 llvm/lib/Target/X86/X86RegisterInfo.cpp       | 11 +++---
 llvm/lib/Target/X86/X86RegisterInfo.h         |  6 ++-
 llvm/lib/Target/X86/X86Subtarget.h            | 15 ++++++--
 llvm/lib/Target/X86/X86TargetMachine.cpp      |  2 +-
 llvm/test/CodeGen/X86/mangle-question-mark.ll |  1 +
 llvm/test/CodeGen/X86/sse-regcall.ll          |  1 +
 llvm/test/CodeGen/X86/win32-preemption.ll     |  2 +
 13 files changed, 101 insertions(+), 35 deletions(-)

diff --git a/clang/lib/Basic/Targets/OSTargets.h 
b/clang/lib/Basic/Targets/OSTargets.h
index a83d6464e789d6..195b4f15136327 100644
--- a/clang/lib/Basic/Targets/OSTargets.h
+++ b/clang/lib/Basic/Targets/OSTargets.h
@@ -778,12 +778,18 @@ class LLVM_LIBRARY_VISIBILITY ZOSTargetInfo : public 
OSTargetInfo<Target> {
   }
 };
 
+void addWindowsDefines(const llvm::Triple &Triple, const LangOptions &Opts,
+                       MacroBuilder &Builder);
+
 // UEFI target
 template <typename Target>
 class LLVM_LIBRARY_VISIBILITY UEFITargetInfo : public OSTargetInfo<Target> {
 protected:
   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
-                    MacroBuilder &Builder) const override {}
+                    MacroBuilder &Builder) const override {
+    Builder.defineMacro("__UEFI__");                      
+    addWindowsDefines(Triple, Opts, Builder);
+  }
 
 public:
   UEFITargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
@@ -793,8 +799,7 @@ class LLVM_LIBRARY_VISIBILITY UEFITargetInfo : public 
OSTargetInfo<Target> {
   }
 };
 
-void addWindowsDefines(const llvm::Triple &Triple, const LangOptions &Opts,
-                       MacroBuilder &Builder);
+
 
 // Windows target
 template <typename Target>
diff --git a/clang/lib/Basic/Targets/X86.h b/clang/lib/Basic/Targets/X86.h
index a99ae62984c7d5..07c98d880ab778 100644
--- a/clang/lib/Basic/Targets/X86.h
+++ b/clang/lib/Basic/Targets/X86.h
@@ -826,10 +826,11 @@ class LLVM_LIBRARY_VISIBILITY UEFIX86_64TargetInfo
                           "i64:64-i128:128-f80:128-n8:16:32:64-S128");
   }
 
-  void getTargetDefines(const LangOptions &Opts,
-                        MacroBuilder &Builder) const override {
-    getOSDefines(Opts, X86TargetInfo::getTriple(), Builder);
-  }
+  // void getTargetDefines(const LangOptions &Opts,
+  //                       MacroBuilder &Builder) const override {
+  //   getOSDefines(Opts, X86TargetInfo::getTriple(), Builder);
+  //   Builder.defineMacro("__x86_64__");
+  // }
 
   BuiltinVaListKind getBuiltinVaListKind() const override {
     return TargetInfo::CharPtrBuiltinVaList;
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.cpp 
b/llvm/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.cpp
index fe3c42eeb6e8ec..c1baedda69c6b4 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.cpp
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.cpp
@@ -445,7 +445,7 @@ static MCAsmInfo *createX86MCAsmInfo(const MCRegisterInfo 
&MRI,
     // Force the use of an ELF container.
     MAI = new X86ELFMCAsmInfo(TheTriple);
   } else if (TheTriple.isWindowsMSVCEnvironment() ||
-             TheTriple.isWindowsCoreCLREnvironment()) {
+             TheTriple.isWindowsCoreCLREnvironment() || TheTriple.isUEFI()) {
     if (Options.getAssemblyLanguage().equals_insensitive("masm"))
       MAI = new X86MCAsmInfoMicrosoftMASM(TheTriple);
     else
@@ -453,8 +453,6 @@ static MCAsmInfo *createX86MCAsmInfo(const MCRegisterInfo 
&MRI,
   } else if (TheTriple.isOSCygMing() ||
              TheTriple.isWindowsItaniumEnvironment()) {
     MAI = new X86MCAsmInfoGNUCOFF(TheTriple);
-  } else if (TheTriple.isUEFI()) {
-    MAI = new X86MCAsmInfoGNUCOFF(TheTriple);
   } else {
     // The default is ELF.
     MAI = new X86ELFMCAsmInfo(TheTriple);
diff --git a/llvm/lib/Target/X86/X86CallingConv.td 
b/llvm/lib/Target/X86/X86CallingConv.td
index 307aeb2ea4c6fd..93d84577801cd6 100644
--- a/llvm/lib/Target/X86/X86CallingConv.td
+++ b/llvm/lib/Target/X86/X86CallingConv.td
@@ -488,13 +488,24 @@ def RetCC_X86_64 : CallingConv<[
     CCIfSubtarget<"isTargetWin64()", 
CCIfRegCallv4<CCDelegateTo<RetCC_X86_Win64_RegCallv4>>>>,
 
   CCIfCC<"CallingConv::X86_RegCall",
-          CCIfSubtarget<"isTargetWin64()",
+    CCIfSubtarget<"isTargetUEFI64()", 
CCIfRegCallv4<CCDelegateTo<RetCC_X86_Win64_RegCallv4>>>>,
+
+  CCIfCC<"CallingConv::X86_RegCall",
+          CCIfSubtarget<"isTargetWin64()",                        
                         CCDelegateTo<RetCC_X86_Win64_RegCall>>>,
+
+  CCIfCC<"CallingConv::X86_RegCall",
+        CCIfSubtarget<"isTargetUEFI64()",
+                      CCDelegateTo<RetCC_X86_Win64_RegCall>>>,
+
   CCIfCC<"CallingConv::X86_RegCall", CCDelegateTo<RetCC_X86_SysV64_RegCall>>,
           
   // Mingw64 and native Win64 use Win64 CC
   CCIfSubtarget<"isTargetWin64()", CCDelegateTo<RetCC_X86_Win64_C>>,
 
+  // UEFI64 uses Win64 CC
+  CCIfSubtarget<"isTargetUEFI64()", CCDelegateTo<RetCC_X86_Win64_C>>,
+
   // Otherwise, drop to normal X86-64 CC
   CCDelegateTo<RetCC_X86_64_C>
 ]>;
@@ -1079,6 +1090,10 @@ def CC_X86_64 : CallingConv<[
     CCIfSubtarget<"isTargetWin64()", 
CCIfRegCallv4<CCDelegateTo<CC_X86_Win64_RegCallv4>>>>,
   CCIfCC<"CallingConv::X86_RegCall",
     CCIfSubtarget<"isTargetWin64()", CCDelegateTo<CC_X86_Win64_RegCall>>>,
+  CCIfCC<"CallingConv::X86_RegCall",
+    CCIfSubtarget<"isTargetUEFI64()", 
CCIfRegCallv4<CCDelegateTo<CC_X86_Win64_RegCallv4>>>>,
+  CCIfCC<"CallingConv::X86_RegCall",
+    CCIfSubtarget<"isTargetUEFI64()", CCDelegateTo<CC_X86_Win64_RegCall>>>,
   CCIfCC<"CallingConv::X86_RegCall", CCDelegateTo<CC_X86_SysV64_RegCall>>,
   CCIfCC<"CallingConv::PreserveNone", CCDelegateTo<CC_X86_64_Preserve_None>>,
   CCIfCC<"CallingConv::X86_INTR", CCCustom<"CC_X86_Intr">>,
diff --git a/llvm/lib/Target/X86/X86FrameLowering.cpp 
b/llvm/lib/Target/X86/X86FrameLowering.cpp
index 4f83267c999e4a..65714f199be872 100644
--- a/llvm/lib/Target/X86/X86FrameLowering.cpp
+++ b/llvm/lib/Target/X86/X86FrameLowering.cpp
@@ -1534,11 +1534,16 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
                                     MachineBasicBlock &MBB) const {
   assert(&STI == &MF.getSubtarget<X86Subtarget>() &&
          "MF used frame lowering for wrong subtarget");
+
   MachineBasicBlock::iterator MBBI = MBB.begin();
   MachineFrameInfo &MFI = MF.getFrameInfo();
   const Function &Fn = MF.getFunction();
+
   X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
   uint64_t MaxAlign = calculateMaxStackAlign(MF); // Desired stack alignment.
+
+  // errs() << "********** MaxAlign size " << MaxAlign;
+
   uint64_t StackSize = MFI.getStackSize(); // Number of bytes to allocate.
   bool IsFunclet = MBB.isEHFuncletEntry();
   EHPersonality Personality = EHPersonality::Unknown;
@@ -1549,6 +1554,12 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
   bool IsClrFunclet = IsFunclet && FnHasClrFunclet;
   bool HasFP = hasFP(MF);
   bool IsWin64Prologue = isWin64Prologue(MF);
+
+  // if(IsWin64Prologue) {
+  //   errs() << "********** IsWin64Prologue TRUE ";
+  // } else {
+  //   errs() << "********** IsWin64Prologue FALSE FALSE FALSE ";
+  // }
   bool NeedsWin64CFI = IsWin64Prologue && Fn.needsUnwindTableEntry();
   // FIXME: Emit FPO data for EH funclets.
   bool NeedsWinFPO = !IsFunclet && STI.isTargetWin32() &&
@@ -1672,6 +1683,12 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
   // pointer, calls, or dynamic alloca then we do not need to adjust the
   // stack pointer (we fit in the Red Zone). We also check that we don't
   // push and pop from the stack.
+
+  // if (has128ByteRedZone(MF)) {
+  //   errs() << "********** has128ByteRedZone TRUE ";
+  // } else {
+  //   errs() << "********** has128ByteRedZone FALSE FALSE FALSE ";
+  // }
   if (has128ByteRedZone(MF) && !TRI->hasStackRealignment(MF) &&
       !MFI.hasVarSizedObjects() &&             // No dynamic alloca.
       !MFI.adjustsStack() &&                   // No calls.
@@ -1680,6 +1697,7 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
       !MF.shouldSplitStack()) {                // Regular stack
     uint64_t MinSize =
         X86FI->getCalleeSavedFrameSize() - X86FI->getTCReturnAddrDelta();
+
     if (HasFP)
       MinSize += SlotSize;
     X86FI->setUsesRedZone(MinSize > 0 || StackSize > 0);
@@ -1895,7 +1913,9 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
            Opc == X86::PUSH2 || Opc == X86::PUSH2P;
   };
 
+  // uint64_t cont3 = 1;
   while (IsCSPush(MBBI)) {
+    // llvm::outs() << "\n*********** cont3 " << cont3++;
     PushedRegs = true;
     Register Reg = MBBI->getOperand(0).getReg();
     LastCSPush = MBBI;
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp 
b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 9637e96c21cf52..51b245aba1fa3e 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -576,6 +576,7 @@ X86TargetLowering::X86TargetLowering(const X86TargetMachine 
&TM,
   // FIXME - use subtarget debug flags
   if (!Subtarget.isTargetDarwin() && !Subtarget.isTargetELF() &&
       !Subtarget.isTargetCygMing() && !Subtarget.isTargetWin64() &&
+      !Subtarget.isTargetUEFI64() &&
       TM.Options.ExceptionModel != ExceptionHandling::SjLj) {
     setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
   }
@@ -2521,7 +2522,7 @@ X86TargetLowering::X86TargetLowering(const 
X86TargetMachine &TM,
     setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
   }
 
-  if (Subtarget.isTargetWin64()) {
+  if (Subtarget.isTargetWin64() || Subtarget.isTargetUEFI64()) {
     setOperationAction(ISD::SDIV, MVT::i128, Custom);
     setOperationAction(ISD::UDIV, MVT::i128, Custom);
     setOperationAction(ISD::SREM, MVT::i128, Custom);
@@ -19487,7 +19488,8 @@ SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
   else if (isLegalConversion(SrcVT, true, Subtarget))
     return Op;
 
-  if (Subtarget.isTargetWin64() && SrcVT == MVT::i128)
+  if ((Subtarget.isTargetWin64() || Subtarget.isTargetUEFI64()) &&
+      SrcVT == MVT::i128)
     return LowerWin64_INT128_TO_FP(Op, DAG);
 
   if (SDValue Extract = vectorizeExtractedCast(Op, dl, DAG, Subtarget))
@@ -19994,7 +19996,8 @@ SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
   if (DstVT.isVector())
     return lowerUINT_TO_FP_vec(Op, dl, DAG, Subtarget);
 
-  if (Subtarget.isTargetWin64() && SrcVT == MVT::i128)
+  if ((Subtarget.isTargetWin64() || Subtarget.isTargetUEFI64()) &&
+      SrcVT == MVT::i128)
     return LowerWin64_INT128_TO_FP(Op, DAG);
 
   if (SDValue Extract = vectorizeExtractedCast(Op, dl, DAG, Subtarget))
@@ -27626,7 +27629,6 @@ Register X86TargetLowering::getRegisterByName(const 
char* RegName, LLT VT,
                      .Case("r14", X86::R14)
                      .Case("r15", X86::R15)
                      .Default(0);
-
   if (Reg == X86::EBP || Reg == X86::RBP) {
     if (!TFI.hasFP(MF))
       report_fatal_error("register " + StringRef(RegName) +
@@ -27670,7 +27672,7 @@ Register 
X86TargetLowering::getExceptionSelectorRegister(
 }
 
 bool X86TargetLowering::needsFixedCatchObjects() const {
-  return Subtarget.isTargetWin64();
+  return Subtarget.isTargetWin64() || Subtarget.isTargetUEFI64();
 }
 
 SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const 
{
@@ -29224,7 +29226,8 @@ static SDValue LowerMULO(SDValue Op, const X86Subtarget 
&Subtarget,
 }
 
 SDValue X86TargetLowering::LowerWin64_i128OP(SDValue Op, SelectionDAG &DAG) 
const {
-  assert(Subtarget.isTargetWin64() && "Unexpected target");
+  assert((Subtarget.isTargetWin64() || Subtarget.isTargetUEFI64()) &&
+         "Unexpected target");
   EVT VT = Op.getValueType();
   assert(VT.isInteger() && VT.getSizeInBits() == 128 &&
          "Unexpected return type for lowering");
@@ -29291,7 +29294,8 @@ SDValue X86TargetLowering::LowerWin64_i128OP(SDValue 
Op, SelectionDAG &DAG) cons
 SDValue X86TargetLowering::LowerWin64_FP_TO_INT128(SDValue Op,
                                                    SelectionDAG &DAG,
                                                    SDValue &Chain) const {
-  assert(Subtarget.isTargetWin64() && "Unexpected target");
+  assert((Subtarget.isTargetWin64() || Subtarget.isTargetUEFI64()) &&
+         "Unexpected target");
   EVT VT = Op.getValueType();
   bool IsStrict = Op->isStrictFPOpcode();
 
@@ -29324,7 +29328,8 @@ SDValue 
X86TargetLowering::LowerWin64_FP_TO_INT128(SDValue Op,
 
 SDValue X86TargetLowering::LowerWin64_INT128_TO_FP(SDValue Op,
                                                    SelectionDAG &DAG) const {
-  assert(Subtarget.isTargetWin64() && "Unexpected target");
+  assert((Subtarget.isTargetWin64() || Subtarget.isTargetUEFI64()) &&
+         "Unexpected target");
   EVT VT = Op.getValueType();
   bool IsStrict = Op->isStrictFPOpcode();
 
@@ -33564,7 +33569,8 @@ void X86TargetLowering::ReplaceNodeResults(SDNode *N,
       return;
     }
 
-    if (VT == MVT::i128 && Subtarget.isTargetWin64()) {
+    if (VT == MVT::i128 &&
+        (Subtarget.isTargetWin64() || Subtarget.isTargetUEFI64())) {
       SDValue Chain;
       SDValue V = LowerWin64_FP_TO_INT128(SDValue(N, 0), DAG, Chain);
       Results.push_back(V);
@@ -36945,6 +36951,8 @@ X86TargetLowering::EmitSjLjDispatchBlock(MachineInstr 
&MI,
   // N.B. the order the invoke BBs are processed in doesn't matter here.
   SmallVector<MachineBasicBlock *, 64> MBBLPads;
   const MCPhysReg *SavedRegs = MF->getRegInfo().getCalleeSavedRegs();
+  // llvm::outs() << "Callee saved regs from isellowering " <<
+  // SavedRegs->
   for (MachineBasicBlock *MBB : InvokeBBs) {
     // Remove the landing pad successor from the invoke block and replace it
     // with the new dispatch block.
@@ -60153,8 +60161,8 @@ bool X86TargetLowering::hasStackProbeSymbol(const 
MachineFunction &MF) const {
 /// Returns true if stack probing through inline assembly is requested.
 bool X86TargetLowering::hasInlineStackProbe(const MachineFunction &MF) const {
 
-  // No inline stack probe for Windows, they have their own mechanism.
-  if (Subtarget.isOSWindows() ||
+  // No inline stack probe for Windows and UEFI, they have their own mechanism.
+  if (Subtarget.isOSWindows() || Subtarget.isUEFI() ||
       MF.getFunction().hasFnAttribute("no-stack-arg-probe"))
     return false;
 
@@ -60178,9 +60186,10 @@ X86TargetLowering::getStackProbeSymbolName(const 
MachineFunction &MF) const {
   if (MF.getFunction().hasFnAttribute("probe-stack"))
     return MF.getFunction().getFnAttribute("probe-stack").getValueAsString();
 
-  // Generally, if we aren't on Windows, the platform ABI does not include
-  // support for stack probes, so don't emit them.
-  if (!Subtarget.isOSWindows() || Subtarget.isTargetMachO() ||
+  // Generally, if we aren't on Windows or UEFI, the platform ABI does not
+  // include support for stack probes, so don't emit them.
+  if (!(Subtarget.isOSWindows() || Subtarget.isUEFI()) ||
+      Subtarget.isTargetMachO() ||
       MF.getFunction().hasFnAttribute("no-stack-arg-probe"))
     return "";
 
diff --git a/llvm/lib/Target/X86/X86RegisterInfo.cpp 
b/llvm/lib/Target/X86/X86RegisterInfo.cpp
index 1d8808f4e2b7d0..48518df8fb2ea1 100644
--- a/llvm/lib/Target/X86/X86RegisterInfo.cpp
+++ b/llvm/lib/Target/X86/X86RegisterInfo.cpp
@@ -62,6 +62,7 @@ X86RegisterInfo::X86RegisterInfo(const Triple &TT)
   // Cache some information.
   Is64Bit = TT.isArch64Bit();
   IsWin64 = Is64Bit && TT.isOSWindows();
+  IsUEFI64 = Is64Bit && TT.isUEFI();
 
   // Use a callee-saved register as the base pointer.  These registers must
   // not conflict with any ABI requirements.  For example, in 32-bit mode PIC
@@ -238,7 +239,7 @@ bool X86RegisterInfo::shouldRewriteCopySrc(const 
TargetRegisterClass *DefRC,
 const TargetRegisterClass *
 X86RegisterInfo::getGPRsForTailCall(const MachineFunction &MF) const {
   const Function &F = MF.getFunction();
-  if (IsWin64 || (F.getCallingConv() == CallingConv::Win64))
+  if (IsWin64 || IsUEFI64 || (F.getCallingConv() == CallingConv::Win64))
     return &X86::GR64_TCW64RegClass;
   else if (Is64Bit)
     return &X86::GR64_TCRegClass;
@@ -340,7 +341,7 @@ X86RegisterInfo::getCalleeSavedRegs(const MachineFunction 
*MF) const {
   }
   case CallingConv::X86_RegCall:
     if (Is64Bit) {
-      if (IsWin64) {
+      if (IsWin64 || IsUEFI64) {
         return (HasSSE ? CSR_Win64_RegCall_SaveList :
                          CSR_Win64_RegCall_NoSSE_SaveList);
       } else {
@@ -400,7 +401,7 @@ X86RegisterInfo::getCalleeSavedRegs(const MachineFunction 
*MF) const {
       return IsWin64 ? CSR_Win64_SwiftError_SaveList
                      : CSR_64_SwiftError_SaveList;
 
-    if (IsWin64)
+    if (IsWin64 || IsUEFI64)
       return HasSSE ? CSR_Win64_SaveList : CSR_Win64_NoSSE_SaveList;
     if (CallsEHReturn)
       return CSR_64EHRet_SaveList;
@@ -462,7 +463,7 @@ X86RegisterInfo::getCallPreservedMask(const MachineFunction 
&MF,
   }
   case CallingConv::X86_RegCall:
     if (Is64Bit) {
-      if (IsWin64) {
+      if (IsWin64 || IsUEFI64) {
         return (HasSSE ? CSR_Win64_RegCall_RegMask :
                          CSR_Win64_RegCall_NoSSE_RegMask);
       } else {
@@ -520,7 +521,7 @@ X86RegisterInfo::getCallPreservedMask(const MachineFunction 
&MF,
     if (IsSwiftCC)
       return IsWin64 ? CSR_Win64_SwiftError_RegMask : 
CSR_64_SwiftError_RegMask;
 
-    return IsWin64 ? CSR_Win64_RegMask : CSR_64_RegMask;
+    return (IsWin64 || IsUEFI64) ? CSR_Win64_RegMask : CSR_64_RegMask;
   }
 
   return CSR_32_RegMask;
diff --git a/llvm/lib/Target/X86/X86RegisterInfo.h 
b/llvm/lib/Target/X86/X86RegisterInfo.h
index 2f73698a4b94d3..baddc99ad1e0c4 100644
--- a/llvm/lib/Target/X86/X86RegisterInfo.h
+++ b/llvm/lib/Target/X86/X86RegisterInfo.h
@@ -27,10 +27,14 @@ class X86RegisterInfo final : public X86GenRegisterInfo {
   ///
   bool Is64Bit;
 
-  /// IsWin64 - Is the target on of win64 flavours
+  /// IsWin64 - Is the target one of win64 flavours
   ///
   bool IsWin64;
 
+  /// IsUEFI64 - Is this UEFI 64 bit target
+  ///
+  bool IsUEFI64;
+
   /// SlotSize - Stack slot size in bytes.
   ///
   unsigned SlotSize;
diff --git a/llvm/lib/Target/X86/X86Subtarget.h 
b/llvm/lib/Target/X86/X86Subtarget.h
index e3cb9ee8ce1909..2ea6ddcbf5bacd 100644
--- a/llvm/lib/Target/X86/X86Subtarget.h
+++ b/llvm/lib/Target/X86/X86Subtarget.h
@@ -321,7 +321,11 @@ class X86Subtarget final : public X86GenSubtargetInfo {
 
   bool isOSWindows() const { return TargetTriple.isOSWindows(); }
 
-  bool isTargetWin64() const { return Is64Bit && isOSWindows(); }
+  bool isUEFI() const { return TargetTriple.isUEFI(); }
+
+  bool isTargetUEFI64() const { return Is64Bit && isUEFI(); }
+
+  bool isTargetWin64() const { return Is64Bit && (isOSWindows() || isUEFI()); }
 
   bool isTargetWin32() const { return !Is64Bit && isOSWindows(); }
 
@@ -335,8 +339,11 @@ class X86Subtarget final : public X86GenSubtargetInfo {
   bool isPositionIndependent() const;
 
   bool isCallingConvWin64(CallingConv::ID CC) const {
+    // llvm::outs() << "\nIn isCallingConvWin64 check calling
+    // convention******************* ";
     switch (CC) {
-    // On Win64, all these conventions just use the default convention.
+    // On Win64 and UEFI64, all these conventions just use the default
+    // convention.
     case CallingConv::C:
     case CallingConv::Fast:
     case CallingConv::Tail:
@@ -347,9 +354,11 @@ class X86Subtarget final : public X86GenSubtargetInfo {
     case CallingConv::X86_ThisCall:
     case CallingConv::X86_VectorCall:
     case CallingConv::Intel_OCL_BI:
-      return isTargetWin64();
+      return isTargetWin64() || isTargetUEFI64();
     // This convention allows using the Win64 convention on other targets.
     case CallingConv::Win64:
+      // llvm::outs() << "\nReturning true for Win64 calling
+      // convention******************* ";
       return true;
     // This convention allows using the SysV convention on Windows targets.
     case CallingConv::X86_64_SysV:
diff --git a/llvm/lib/Target/X86/X86TargetMachine.cpp 
b/llvm/lib/Target/X86/X86TargetMachine.cpp
index fc2a1e34b711ef..79a5644c095398 100644
--- a/llvm/lib/Target/X86/X86TargetMachine.cpp
+++ b/llvm/lib/Target/X86/X86TargetMachine.cpp
@@ -483,7 +483,7 @@ void X86PassConfig::addIRPasses() {
 
   // Add Control Flow Guard checks.
   const Triple &TT = TM->getTargetTriple();
-  if (TT.isOSWindows()) {
+  if (TT.isOSWindows() || TT.isUEFI()) {
     if (TT.getArch() == Triple::x86_64) {
       addPass(createCFGuardDispatchPass());
     } else {
diff --git a/llvm/test/CodeGen/X86/mangle-question-mark.ll 
b/llvm/test/CodeGen/X86/mangle-question-mark.ll
index fea62ecad97c90..15fdae34bc4c31 100644
--- a/llvm/test/CodeGen/X86/mangle-question-mark.ll
+++ b/llvm/test/CodeGen/X86/mangle-question-mark.ll
@@ -2,6 +2,7 @@
 
 ; RUN: llc -mtriple i686-pc-win32 < %s | FileCheck %s --check-prefix=COFF
 ; RUN: llc -mtriple x86_64-pc-win32 < %s | FileCheck %s --check-prefix=COFF64
+; RUN: llc -mtriple x86_64-unknown-uefi < %s | FileCheck %s 
--check-prefix=COFF64
 ; RUN: llc -mtriple i686-linux-gnu < %s | FileCheck %s --check-prefix=ELF
 ; RUN: llc -mtriple i686-apple-darwin < %s | FileCheck %s --check-prefix=MACHO
 
diff --git a/llvm/test/CodeGen/X86/sse-regcall.ll 
b/llvm/test/CodeGen/X86/sse-regcall.ll
index 6f0293392eef2b..923b31c8820475 100644
--- a/llvm/test/CodeGen/X86/sse-regcall.ll
+++ b/llvm/test/CodeGen/X86/sse-regcall.ll
@@ -1,6 +1,7 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
 ; RUN: llc < %s -mtriple=i386-pc-win32 -mattr=+sse | FileCheck 
--check-prefix=WIN32 %s
 ; RUN: llc < %s -mtriple=x86_64-win32 -mattr=+sse | FileCheck 
--check-prefix=WIN64 %s
+; RUN: llc < %s -mtriple=x86_64-unknown-uefi -mattr=+sse | FileCheck 
--check-prefix=WIN64 %s
 ; RUN: llc < %s -mtriple=x86_64-linux-gnu -mattr=+sse | FileCheck 
--check-prefix=LINUXOSX %s
 
 ; Test regcall when receiving/returning i1
diff --git a/llvm/test/CodeGen/X86/win32-preemption.ll 
b/llvm/test/CodeGen/X86/win32-preemption.ll
index 77dcfa7280daf6..139a1e514556e6 100644
--- a/llvm/test/CodeGen/X86/win32-preemption.ll
+++ b/llvm/test/CodeGen/X86/win32-preemption.ll
@@ -4,6 +4,8 @@
 ; RUN:     -relocation-model=pic     < %s | FileCheck --check-prefix=COFF %s
 ; RUN: llc -mtriple x86_64-pc-win32 \
 ; RUN:  -relocation-model=dynamic-no-pic < %s | FileCheck --check-prefix=COFF 
%s
+; RUN: llc -mtriple x86_64-unknown-uefi \
+; RUN:  -relocation-model=dynamic-no-pic < %s | FileCheck --check-prefix=COFF 
%s
 
 
 ; 32 bits

>From 070b2dca1b57e96de4b4bdb5db1b64de009be176 Mon Sep 17 00:00:00 2001
From: prabhukr <prabh...@google.com>
Date: Wed, 2 Oct 2024 14:56:44 -0700
Subject: [PATCH 2/2] Clean up macro predefines

---
 clang/lib/Basic/Targets/OSTargets.cpp | 1 +
 clang/lib/Basic/Targets/OSTargets.h   | 5 +++--
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Basic/Targets/OSTargets.cpp 
b/clang/lib/Basic/Targets/OSTargets.cpp
index b56e2c7ca9c494..d625967deb2589 100644
--- a/clang/lib/Basic/Targets/OSTargets.cpp
+++ b/clang/lib/Basic/Targets/OSTargets.cpp
@@ -260,6 +260,7 @@ static void addVisualCDefines(const LangOptions &Opts, 
MacroBuilder &Builder) {
 void addWindowsDefines(const llvm::Triple &Triple, const LangOptions &Opts,
                        MacroBuilder &Builder) {
   Builder.defineMacro("_WIN32");
+  Builder.defineMacro("__PECOFF__");
   if (Triple.isArch64Bit())
     Builder.defineMacro("_WIN64");
   if (Triple.isWindowsGNUEnvironment())
diff --git a/clang/lib/Basic/Targets/OSTargets.h 
b/clang/lib/Basic/Targets/OSTargets.h
index 195b4f15136327..547c93146271ff 100644
--- a/clang/lib/Basic/Targets/OSTargets.h
+++ b/clang/lib/Basic/Targets/OSTargets.h
@@ -787,8 +787,9 @@ class LLVM_LIBRARY_VISIBILITY UEFITargetInfo : public 
OSTargetInfo<Target> {
 protected:
   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
                     MacroBuilder &Builder) const override {
-    Builder.defineMacro("__UEFI__");                      
-    addWindowsDefines(Triple, Opts, Builder);
+    Builder.defineMacro("__UEFI__");
+    Builder.defineMacro("__PECOFF__");
+    // addWindowsDefines(Triple, Opts, Builder);
   }
 
 public:

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to