Skip to content

Commit

Permalink
[PowerPC][Future] Initial support for PC Relative addressing for glob…
Browse files Browse the repository at this point in the history
…al values

This patch adds PC Relative support for global values that are known at link
time. If a global value requires access through the global offset table (GOT)
it is not covered in this patch.

Differential Revision: https://reviews.llvm.org/D75280
  • Loading branch information
stefanp-ibm authored and kamaub committed Apr 16, 2020
1 parent d9085f6 commit 18b6050
Show file tree
Hide file tree
Showing 5 changed files with 699 additions and 56 deletions.
26 changes: 17 additions & 9 deletions llvm/lib/Target/PowerPC/PPCISelLowering.cpp
Expand Up @@ -2588,17 +2588,19 @@ bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
}

/// Returns true if this address is a PC Relative address.
/// PC Relative addresses are marked with the flag PPCII::MO_PCREL_FLAG.
/// PC Relative addresses are marked with the flag PPCII::MO_PCREL_FLAG
/// or if the node opcode is PPCISD::MAT_PCREL_ADDR.
bool PPCTargetLowering::SelectAddressPCRel(SDValue N, SDValue &Base) const {
ConstantPoolSDNode *ConstPoolNode =
dyn_cast<ConstantPoolSDNode>(N.getNode());
bool HasFlag = ConstPoolNode &&
ConstPoolNode->getTargetFlags() == PPCII::MO_PCREL_FLAG;
bool HasNode = N.getOpcode() == PPCISD::MAT_PCREL_ADDR;
if (HasFlag || HasNode) {
Base = N;
// This is a materialize PC Relative node. Always select this as PC Relative.
Base = N;
if (N.getOpcode() == PPCISD::MAT_PCREL_ADDR)
return true;
}
if (ConstantPoolSDNode *CPN = dyn_cast<ConstantPoolSDNode>(N))
if (CPN->getTargetFlags() & PPCII::MO_PCREL_FLAG)
return true;
if (GlobalAddressSDNode *GAN = dyn_cast<GlobalAddressSDNode>(N))
if (GAN->getTargetFlags() & PPCII::MO_PCREL_FLAG)
return true;
return false;
}

Expand Down Expand Up @@ -3049,6 +3051,12 @@ SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
// 64-bit SVR4 ABI & AIX ABI code is always position-independent.
// The actual address of the GlobalValue is stored in the TOC.
if (Subtarget.is64BitELFABI() || Subtarget.isAIXABI()) {
if (!isAccessedAsGotIndirect(Op) && Subtarget.isUsingPCRelativeCalls()) {
EVT Ty = getPointerTy(DAG.getDataLayout());
SDValue GA = DAG.getTargetGlobalAddress(GV, DL, Ty, GSDN->getOffset(),
PPCII::MO_PCREL_FLAG);
return DAG.getNode(PPCISD::MAT_PCREL_ADDR, DL, Ty, GA);
}
setUsesTOCBasePtr(DAG);
SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
return getTOCEntry(DAG, DL, GA);
Expand Down
123 changes: 123 additions & 0 deletions llvm/lib/Target/PowerPC/PPCInstrPrefix.td
Expand Up @@ -348,30 +348,153 @@ let Predicates = [PrefixInstrs] in {
// follow-up patches will address this refactoring and the AddedComplexity will
// be removed.
let Predicates = [PCRelativeMemops], AddedComplexity = 500 in {
// Load i32
def : Pat<(i32 (zextloadi8 (PPCmatpcreladdr pcreladdr:$ga))),
(PLBZpc $ga, 0)>;
def : Pat<(i32 (extloadi8 (PPCmatpcreladdr pcreladdr:$ga))),
(PLBZpc $ga, 0)>;
def : Pat<(i32 (sextloadi16 (PPCmatpcreladdr pcreladdr:$ga))),
(PLHApc $ga, 0)>;
def : Pat<(i32 (zextloadi16 (PPCmatpcreladdr pcreladdr:$ga))),
(PLHZpc $ga, 0)>;
def : Pat<(i32 (extloadi16 (PPCmatpcreladdr pcreladdr:$ga))),
(PLHZpc $ga, 0)>;
def : Pat<(i32 (load (PPCmatpcreladdr pcreladdr:$ga))), (PLWZpc $ga, 0)>;

// Store i32
def : Pat<(truncstorei8 i32:$RS, (PPCmatpcreladdr pcreladdr:$ga)),
(PSTBpc $RS, $ga, 0)>;
def : Pat<(truncstorei16 i32:$RS, (PPCmatpcreladdr pcreladdr:$ga)),
(PSTHpc $RS, $ga, 0)>;
def : Pat<(store i32:$RS, (PPCmatpcreladdr pcreladdr:$ga)),
(PSTWpc $RS, $ga, 0)>;

// Load i64
def : Pat<(i64 (zextloadi8 (PPCmatpcreladdr pcreladdr:$ga))),
(PLBZ8pc $ga, 0)>;
def : Pat<(i64 (extloadi8 (PPCmatpcreladdr pcreladdr:$ga))),
(PLBZ8pc $ga, 0)>;
def : Pat<(i64 (sextloadi16 (PPCmatpcreladdr pcreladdr:$ga))),
(PLHA8pc $ga, 0)>;
def : Pat<(i64 (zextloadi16 (PPCmatpcreladdr pcreladdr:$ga))),
(PLHZ8pc $ga, 0)>;
def : Pat<(i64 (extloadi16 (PPCmatpcreladdr pcreladdr:$ga))),
(PLHZ8pc $ga, 0)>;
def : Pat<(i64 (zextloadi32 (PPCmatpcreladdr pcreladdr:$ga))),
(PLWZ8pc $ga, 0)>;
def : Pat<(i64 (sextloadi32 (PPCmatpcreladdr pcreladdr:$ga))),
(PLWA8pc $ga, 0)>;
def : Pat<(i64 (extloadi32 (PPCmatpcreladdr pcreladdr:$ga))),
(PLWZ8pc $ga, 0)>;
def : Pat<(i64 (load (PPCmatpcreladdr pcreladdr:$ga))), (PLDpc $ga, 0)>;

// Store i64
def : Pat<(truncstorei8 i64:$RS, (PPCmatpcreladdr pcreladdr:$ga)),
(PSTB8pc $RS, $ga, 0)>;
def : Pat<(truncstorei16 i64:$RS, (PPCmatpcreladdr pcreladdr:$ga)),
(PSTH8pc $RS, $ga, 0)>;
def : Pat<(truncstorei32 i64:$RS, (PPCmatpcreladdr pcreladdr:$ga)),
(PSTW8pc $RS, $ga, 0)>;
def : Pat<(store i64:$RS, (PPCmatpcreladdr pcreladdr:$ga)),
(PSTDpc $RS, $ga, 0)>;

// Load f32
def : Pat<(f32 (load (PPCmatpcreladdr pcreladdr:$addr))), (PLFSpc $addr, 0)>;

// Store f32
def : Pat<(store f32:$FRS, (PPCmatpcreladdr pcreladdr:$ga)),
(PSTFSpc $FRS, $ga, 0)>;

// Load f64
def : Pat<(f64 (extloadf32 (PPCmatpcreladdr pcreladdr:$addr))),
(COPY_TO_REGCLASS (PLFSpc $addr, 0), VSFRC)>;
def : Pat<(f64 (load (PPCmatpcreladdr pcreladdr:$addr))), (PLFDpc $addr, 0)>;

// Store f64
def : Pat<(store f64:$FRS, (PPCmatpcreladdr pcreladdr:$ga)),
(PSTFDpc $FRS, $ga, 0)>;

// Load f128
def : Pat<(f128 (load (PPCmatpcreladdr pcreladdr:$addr))),
(COPY_TO_REGCLASS (PLXVpc $addr, 0), VRRC)>;

// Store f128
def : Pat<(store f128:$XS, (PPCmatpcreladdr pcreladdr:$ga)),
(PSTXVpc (COPY_TO_REGCLASS $XS, VSRC), $ga, 0)>;

// Load v4i32
def : Pat<(v4i32 (load (PPCmatpcreladdr pcreladdr:$addr))), (PLXVpc $addr, 0)>;

// Store v4i32
def : Pat<(store v4i32:$XS, (PPCmatpcreladdr pcreladdr:$ga)),
(PSTXVpc $XS, $ga, 0)>;

// Load v2i64
def : Pat<(v2i64 (load (PPCmatpcreladdr pcreladdr:$addr))), (PLXVpc $addr, 0)>;

// Store v2i64
def : Pat<(store v2i64:$XS, (PPCmatpcreladdr pcreladdr:$ga)),
(PSTXVpc $XS, $ga, 0)>;

// Load v4f32
def : Pat<(v4f32 (load (PPCmatpcreladdr pcreladdr:$addr))), (PLXVpc $addr, 0)>;

// Store v4f32
def : Pat<(store v4f32:$XS, (PPCmatpcreladdr pcreladdr:$ga)),
(PSTXVpc $XS, $ga, 0)>;

// Load v2f64
def : Pat<(v2f64 (load (PPCmatpcreladdr pcreladdr:$addr))), (PLXVpc $addr, 0)>;

// Store v2f64
def : Pat<(store v2f64:$XS, (PPCmatpcreladdr pcreladdr:$ga)),
(PSTXVpc $XS, $ga, 0)>;

// Atomic Load
def : Pat<(atomic_load_8 (PPCmatpcreladdr pcreladdr:$ga)),
(PLBZpc $ga, 0)>;
def : Pat<(atomic_load_16 (PPCmatpcreladdr pcreladdr:$ga)),
(PLHZpc $ga, 0)>;
def : Pat<(atomic_load_32 (PPCmatpcreladdr pcreladdr:$ga)),
(PLWZpc $ga, 0)>;
def : Pat<(atomic_load_64 (PPCmatpcreladdr pcreladdr:$ga)),
(PLDpc $ga, 0)>;

// Atomic Store
def : Pat<(atomic_store_8 (PPCmatpcreladdr pcreladdr:$ga), i32:$RS),
(PSTBpc $RS, $ga, 0)>;
def : Pat<(atomic_store_16 (PPCmatpcreladdr pcreladdr:$ga), i32:$RS),
(PSTHpc $RS, $ga, 0)>;
def : Pat<(atomic_store_32 (PPCmatpcreladdr pcreladdr:$ga), i32:$RS),
(PSTWpc $RS, $ga, 0)>;
def : Pat<(atomic_store_8 (PPCmatpcreladdr pcreladdr:$ga), i64:$RS),
(PSTB8pc $RS, $ga, 0)>;
def : Pat<(atomic_store_16 (PPCmatpcreladdr pcreladdr:$ga), i64:$RS),
(PSTH8pc $RS, $ga, 0)>;
def : Pat<(atomic_store_32 (PPCmatpcreladdr pcreladdr:$ga), i64:$RS),
(PSTW8pc $RS, $ga, 0)>;
def : Pat<(atomic_store_64 (PPCmatpcreladdr pcreladdr:$ga), i64:$RS),
(PSTDpc $RS, $ga, 0)>;

// Special Cases For PPCstore_scal_int_from_vsr
def : Pat<(PPCstore_scal_int_from_vsr
(f64 (PPCcv_fp_to_sint_in_vsr f64:$src)),
(PPCmatpcreladdr pcreladdr:$dst), 8),
(PSTXSDpc (XSCVDPSXDS f64:$src), $dst, 0)>;
def : Pat<(PPCstore_scal_int_from_vsr
(f64 (PPCcv_fp_to_sint_in_vsr f128:$src)),
(PPCmatpcreladdr pcreladdr:$dst), 8),
(PSTXSDpc (COPY_TO_REGCLASS (XSCVQPSDZ f128:$src), VFRC), $dst, 0)>;

def : Pat<(PPCstore_scal_int_from_vsr
(f64 (PPCcv_fp_to_uint_in_vsr f64:$src)),
(PPCmatpcreladdr pcreladdr:$dst), 8),
(PSTXSDpc (XSCVDPUXDS f64:$src), $dst, 0)>;
def : Pat<(PPCstore_scal_int_from_vsr
(f64 (PPCcv_fp_to_uint_in_vsr f128:$src)),
(PPCmatpcreladdr pcreladdr:$dst), 8),
(PSTXSDpc (COPY_TO_REGCLASS (XSCVQPUDZ f128:$src), VFRC), $dst, 0)>;

// If the PPCmatpcreladdr node is not caught by any other pattern it should be
// caught here and turned into a paddi instruction to materialize the address.
def : Pat<(PPCmatpcreladdr pcreladdr:$addr), (PADDI8pc 0, $addr)>;
Expand Down

0 comments on commit 18b6050

Please sign in to comment.