From: Jessica Paquette Date: Fri, 26 Apr 2019 21:53:13 +0000 (+0000) Subject: [GlobalISel][AArch64] Use getConstantVRegValWithLookThrough for extracts X-Git-Tag: llvmorg-10-init~6879 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=76f64b665bb12b0097a71c10b21a1d302bc84229;p=platform%2Fupstream%2Fllvm.git [GlobalISel][AArch64] Use getConstantVRegValWithLookThrough for extracts getConstantVRegValWithLookThrough does the same thing as the getConstantValueForReg function, and has more visibility across GISel. Plus, it supports looking through G_TRUNC, G_SEXT, and G_ZEXT. So, we get better code reuse and more functionality for free by using it. Add some test cases to select-extract-vector-elt.mir to show that we can now look through those instructions. llvm-svn: 359351 --- diff --git a/llvm/lib/Target/AArch64/AArch64InstructionSelector.cpp b/llvm/lib/Target/AArch64/AArch64InstructionSelector.cpp index 108658d..7f5bc27 100644 --- a/llvm/lib/Target/AArch64/AArch64InstructionSelector.cpp +++ b/llvm/lib/Target/AArch64/AArch64InstructionSelector.cpp @@ -2288,40 +2288,6 @@ static bool getLaneCopyOpcode(unsigned &CopyOpc, unsigned &ExtractSubReg, return true; } -/// Given a register \p Reg, find the value of a constant defining \p Reg. -/// Return true if one could be found, and store it in \p Val. Return false -/// otherwise. -static bool getConstantValueForReg(unsigned Reg, MachineRegisterInfo &MRI, - unsigned &Val) { - // Look at the def of the register. - MachineInstr *Def = MRI.getVRegDef(Reg); - if (!Def) - return false; - - // Find the first definition which isn't a copy. - if (Def->isCopy()) { - Reg = Def->getOperand(1).getReg(); - auto It = find_if_not(MRI.reg_nodbg_instructions(Reg), - [](const MachineInstr &MI) { return MI.isCopy(); }); - if (It == MRI.reg_instr_nodbg_end()) { - LLVM_DEBUG(dbgs() << "Couldn't find non-copy def for register\n"); - return false; - } - Def = &*It; - } - - // TODO: Handle opcodes other than G_CONSTANT. - if (Def->getOpcode() != TargetOpcode::G_CONSTANT) { - LLVM_DEBUG(dbgs() << "VRegs defined by anything other than G_CONSTANT " - "currently unsupported.\n"); - return false; - } - - // Return the constant value associated with the operand. - Val = Def->getOperand(1).getCImm()->getLimitedValue(); - return true; -} - MachineInstr *AArch64InstructionSelector::emitExtractVectorElt( Optional DstReg, const RegisterBank &DstRB, LLT ScalarTy, unsigned VecReg, unsigned LaneIdx, MachineIRBuilder &MIRBuilder) const { @@ -2405,9 +2371,10 @@ bool AArch64InstructionSelector::selectExtractElt( } // Find the index to extract from. - unsigned LaneIdx = 0; - if (!getConstantValueForReg(LaneIdxOp.getReg(), MRI, LaneIdx)) + auto VRegAndVal = getConstantVRegValWithLookThrough(LaneIdxOp.getReg(), MRI); + if (!VRegAndVal) return false; + unsigned LaneIdx = VRegAndVal->Value; MachineIRBuilder MIRBuilder(I); @@ -2983,9 +2950,10 @@ bool AArch64InstructionSelector::selectInsertElt( // Find the definition of the index. Bail out if it's not defined by a // G_CONSTANT. unsigned IdxReg = I.getOperand(3).getReg(); - unsigned LaneIdx = 0; - if (!getConstantValueForReg(IdxReg, MRI, LaneIdx)) + auto VRegAndVal = getConstantVRegValWithLookThrough(IdxReg, MRI); + if (!VRegAndVal) return false; + unsigned LaneIdx = VRegAndVal->Value; // Perform the lane insert. unsigned SrcReg = I.getOperand(1).getReg(); diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/select-extract-vector-elt.mir b/llvm/test/CodeGen/AArch64/GlobalISel/select-extract-vector-elt.mir index f285ca75..41f1479 100644 --- a/llvm/test/CodeGen/AArch64/GlobalISel/select-extract-vector-elt.mir +++ b/llvm/test/CodeGen/AArch64/GlobalISel/select-extract-vector-elt.mir @@ -138,3 +138,74 @@ body: | RET_ReallyLR implicit $h0 ... +--- +name: v8s16_fpr_zext +alignment: 2 +legalized: true +regBankSelected: true +tracksRegLiveness: true +body: | + bb.0: + liveins: $q0 + ; CHECK-LABEL: name: v8s16_fpr_zext + ; CHECK: liveins: $q0 + ; CHECK: [[COPY:%[0-9]+]]:fpr128 = COPY $q0 + ; CHECK: [[CPYi16_:%[0-9]+]]:fpr16 = CPYi16 [[COPY]], 1 + ; CHECK: $h0 = COPY [[CPYi16_]] + ; CHECK: RET_ReallyLR implicit $h0 + %0:fpr(<8 x s16>) = COPY $q0 + %1:gpr(s32) = G_CONSTANT i32 1 + %2:gpr(s64) = G_ZEXT %1 + %3:fpr(s64) = COPY %2(s64) + %4:fpr(s16) = G_EXTRACT_VECTOR_ELT %0(<8 x s16>), %3(s64) + $h0 = COPY %4(s16) + RET_ReallyLR implicit $h0 + +... +--- +name: v8s16_fpr_sext +alignment: 2 +legalized: true +regBankSelected: true +tracksRegLiveness: true +body: | + bb.0: + liveins: $q0 + ; CHECK-LABEL: name: v8s16_fpr_sext + ; CHECK: liveins: $q0 + ; CHECK: [[COPY:%[0-9]+]]:fpr128 = COPY $q0 + ; CHECK: [[CPYi16_:%[0-9]+]]:fpr16 = CPYi16 [[COPY]], 1 + ; CHECK: $h0 = COPY [[CPYi16_]] + ; CHECK: RET_ReallyLR implicit $h0 + %0:fpr(<8 x s16>) = COPY $q0 + %1:gpr(s32) = G_CONSTANT i32 1 + %2:gpr(s64) = G_SEXT %1 + %3:fpr(s64) = COPY %2(s64) + %4:fpr(s16) = G_EXTRACT_VECTOR_ELT %0(<8 x s16>), %3(s64) + $h0 = COPY %4(s16) + RET_ReallyLR implicit $h0 + +... +--- +name: v8s16_fpr_trunc +alignment: 2 +legalized: true +regBankSelected: true +tracksRegLiveness: true +body: | + bb.0: + liveins: $q0 + ; CHECK-LABEL: name: v8s16_fpr_trunc + ; CHECK: liveins: $q0 + ; CHECK: [[COPY:%[0-9]+]]:fpr128 = COPY $q0 + ; CHECK: [[CPYi16_:%[0-9]+]]:fpr16 = CPYi16 [[COPY]], 1 + ; CHECK: $h0 = COPY [[CPYi16_]] + ; CHECK: RET_ReallyLR implicit $h0 + %0:fpr(<8 x s16>) = COPY $q0 + %1:gpr(s64) = G_CONSTANT i64 1 + %2:gpr(s32) = G_TRUNC %1 + %3:gpr(s64) = G_SEXT %2 + %4:fpr(s64) = COPY %3(s64) + %5:fpr(s16) = G_EXTRACT_VECTOR_ELT %0(<8 x s16>), %4(s64) + $h0 = COPY %5(s16) + RET_ReallyLR implicit $h0