/src/llvm-project/llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file defines a pattern matching instruction selector for PowerPC, |
10 | | // converting from a legalized dag to a PPC dag. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "MCTargetDesc/PPCMCTargetDesc.h" |
15 | | #include "MCTargetDesc/PPCPredicates.h" |
16 | | #include "PPC.h" |
17 | | #include "PPCISelLowering.h" |
18 | | #include "PPCMachineFunctionInfo.h" |
19 | | #include "PPCSubtarget.h" |
20 | | #include "PPCTargetMachine.h" |
21 | | #include "llvm/ADT/APInt.h" |
22 | | #include "llvm/ADT/APSInt.h" |
23 | | #include "llvm/ADT/DenseMap.h" |
24 | | #include "llvm/ADT/STLExtras.h" |
25 | | #include "llvm/ADT/SmallPtrSet.h" |
26 | | #include "llvm/ADT/SmallVector.h" |
27 | | #include "llvm/ADT/Statistic.h" |
28 | | #include "llvm/Analysis/BranchProbabilityInfo.h" |
29 | | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
30 | | #include "llvm/CodeGen/ISDOpcodes.h" |
31 | | #include "llvm/CodeGen/MachineBasicBlock.h" |
32 | | #include "llvm/CodeGen/MachineFrameInfo.h" |
33 | | #include "llvm/CodeGen/MachineFunction.h" |
34 | | #include "llvm/CodeGen/MachineInstrBuilder.h" |
35 | | #include "llvm/CodeGen/MachineRegisterInfo.h" |
36 | | #include "llvm/CodeGen/MachineValueType.h" |
37 | | #include "llvm/CodeGen/SelectionDAG.h" |
38 | | #include "llvm/CodeGen/SelectionDAGISel.h" |
39 | | #include "llvm/CodeGen/SelectionDAGNodes.h" |
40 | | #include "llvm/CodeGen/TargetInstrInfo.h" |
41 | | #include "llvm/CodeGen/TargetRegisterInfo.h" |
42 | | #include "llvm/CodeGen/ValueTypes.h" |
43 | | #include "llvm/IR/BasicBlock.h" |
44 | | #include "llvm/IR/DebugLoc.h" |
45 | | #include "llvm/IR/Function.h" |
46 | | #include "llvm/IR/GlobalValue.h" |
47 | | #include "llvm/IR/InlineAsm.h" |
48 | | #include "llvm/IR/InstrTypes.h" |
49 | | #include "llvm/IR/IntrinsicsPowerPC.h" |
50 | | #include "llvm/IR/Module.h" |
51 | | #include "llvm/Support/Casting.h" |
52 | | #include "llvm/Support/CodeGen.h" |
53 | | #include "llvm/Support/CommandLine.h" |
54 | | #include "llvm/Support/Compiler.h" |
55 | | #include "llvm/Support/Debug.h" |
56 | | #include "llvm/Support/ErrorHandling.h" |
57 | | #include "llvm/Support/KnownBits.h" |
58 | | #include "llvm/Support/MathExtras.h" |
59 | | #include "llvm/Support/raw_ostream.h" |
60 | | #include <algorithm> |
61 | | #include <cassert> |
62 | | #include <cstdint> |
63 | | #include <iterator> |
64 | | #include <limits> |
65 | | #include <memory> |
66 | | #include <new> |
67 | | #include <tuple> |
68 | | #include <utility> |
69 | | |
70 | | using namespace llvm; |
71 | | |
72 | | #define DEBUG_TYPE "ppc-isel" |
73 | | #define PASS_NAME "PowerPC DAG->DAG Pattern Instruction Selection" |
74 | | |
75 | | STATISTIC(NumSextSetcc, |
76 | | "Number of (sext(setcc)) nodes expanded into GPR sequence."); |
77 | | STATISTIC(NumZextSetcc, |
78 | | "Number of (zext(setcc)) nodes expanded into GPR sequence."); |
79 | | STATISTIC(SignExtensionsAdded, |
80 | | "Number of sign extensions for compare inputs added."); |
81 | | STATISTIC(ZeroExtensionsAdded, |
82 | | "Number of zero extensions for compare inputs added."); |
83 | | STATISTIC(NumLogicOpsOnComparison, |
84 | | "Number of logical ops on i1 values calculated in GPR."); |
85 | | STATISTIC(OmittedForNonExtendUses, |
86 | | "Number of compares not eliminated as they have non-extending uses."); |
87 | | STATISTIC(NumP9Setb, |
88 | | "Number of compares lowered to setb."); |
89 | | |
90 | | // FIXME: Remove this once the bug has been fixed! |
91 | | cl::opt<bool> ANDIGlueBug("expose-ppc-andi-glue-bug", |
92 | | cl::desc("expose the ANDI glue bug on PPC"), cl::Hidden); |
93 | | |
94 | | static cl::opt<bool> |
95 | | UseBitPermRewriter("ppc-use-bit-perm-rewriter", cl::init(true), |
96 | | cl::desc("use aggressive ppc isel for bit permutations"), |
97 | | cl::Hidden); |
98 | | static cl::opt<bool> BPermRewriterNoMasking( |
99 | | "ppc-bit-perm-rewriter-stress-rotates", |
100 | | cl::desc("stress rotate selection in aggressive ppc isel for " |
101 | | "bit permutations"), |
102 | | cl::Hidden); |
103 | | |
104 | | static cl::opt<bool> EnableBranchHint( |
105 | | "ppc-use-branch-hint", cl::init(true), |
106 | | cl::desc("Enable static hinting of branches on ppc"), |
107 | | cl::Hidden); |
108 | | |
109 | | static cl::opt<bool> EnableTLSOpt( |
110 | | "ppc-tls-opt", cl::init(true), |
111 | | cl::desc("Enable tls optimization peephole"), |
112 | | cl::Hidden); |
113 | | |
114 | | enum ICmpInGPRType { ICGPR_All, ICGPR_None, ICGPR_I32, ICGPR_I64, |
115 | | ICGPR_NonExtIn, ICGPR_Zext, ICGPR_Sext, ICGPR_ZextI32, |
116 | | ICGPR_SextI32, ICGPR_ZextI64, ICGPR_SextI64 }; |
117 | | |
118 | | static cl::opt<ICmpInGPRType> CmpInGPR( |
119 | | "ppc-gpr-icmps", cl::Hidden, cl::init(ICGPR_All), |
120 | | cl::desc("Specify the types of comparisons to emit GPR-only code for."), |
121 | | cl::values(clEnumValN(ICGPR_None, "none", "Do not modify integer comparisons."), |
122 | | clEnumValN(ICGPR_All, "all", "All possible int comparisons in GPRs."), |
123 | | clEnumValN(ICGPR_I32, "i32", "Only i32 comparisons in GPRs."), |
124 | | clEnumValN(ICGPR_I64, "i64", "Only i64 comparisons in GPRs."), |
125 | | clEnumValN(ICGPR_NonExtIn, "nonextin", |
126 | | "Only comparisons where inputs don't need [sz]ext."), |
127 | | clEnumValN(ICGPR_Zext, "zext", "Only comparisons with zext result."), |
128 | | clEnumValN(ICGPR_ZextI32, "zexti32", |
129 | | "Only i32 comparisons with zext result."), |
130 | | clEnumValN(ICGPR_ZextI64, "zexti64", |
131 | | "Only i64 comparisons with zext result."), |
132 | | clEnumValN(ICGPR_Sext, "sext", "Only comparisons with sext result."), |
133 | | clEnumValN(ICGPR_SextI32, "sexti32", |
134 | | "Only i32 comparisons with sext result."), |
135 | | clEnumValN(ICGPR_SextI64, "sexti64", |
136 | | "Only i64 comparisons with sext result."))); |
137 | | namespace { |
138 | | |
139 | | //===--------------------------------------------------------------------===// |
140 | | /// PPCDAGToDAGISel - PPC specific code to select PPC machine |
141 | | /// instructions for SelectionDAG operations. |
142 | | /// |
143 | | class PPCDAGToDAGISel : public SelectionDAGISel { |
144 | | const PPCTargetMachine &TM; |
145 | | const PPCSubtarget *Subtarget = nullptr; |
146 | | const PPCTargetLowering *PPCLowering = nullptr; |
147 | | unsigned GlobalBaseReg = 0; |
148 | | |
149 | | public: |
150 | | static char ID; |
151 | | |
152 | | PPCDAGToDAGISel() = delete; |
153 | | |
154 | | explicit PPCDAGToDAGISel(PPCTargetMachine &tm, CodeGenOptLevel OptLevel) |
155 | 8.28k | : SelectionDAGISel(ID, tm, OptLevel), TM(tm) {} |
156 | | |
157 | 30.4k | bool runOnMachineFunction(MachineFunction &MF) override { |
158 | | // Make sure we re-emit a set of the global base reg if necessary |
159 | 30.4k | GlobalBaseReg = 0; |
160 | 30.4k | Subtarget = &MF.getSubtarget<PPCSubtarget>(); |
161 | 30.4k | PPCLowering = Subtarget->getTargetLowering(); |
162 | 30.4k | if (Subtarget->hasROPProtect()) { |
163 | | // Create a place on the stack for the ROP Protection Hash. |
164 | | // The ROP Protection Hash will always be 8 bytes and aligned to 8 |
165 | | // bytes. |
166 | 0 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
167 | 0 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
168 | 0 | const int Result = MFI.CreateStackObject(8, Align(8), false); |
169 | 0 | FI->setROPProtectionHashSaveIndex(Result); |
170 | 0 | } |
171 | 30.4k | SelectionDAGISel::runOnMachineFunction(MF); |
172 | | |
173 | 30.4k | return true; |
174 | 30.4k | } |
175 | | |
176 | | void PreprocessISelDAG() override; |
177 | | void PostprocessISelDAG() override; |
178 | | |
179 | | /// getI16Imm - Return a target constant with the specified value, of type |
180 | | /// i16. |
181 | 132 | inline SDValue getI16Imm(unsigned Imm, const SDLoc &dl) { |
182 | 132 | return CurDAG->getTargetConstant(Imm, dl, MVT::i16); |
183 | 132 | } |
184 | | |
185 | | /// getI32Imm - Return a target constant with the specified value, of type |
186 | | /// i32. |
187 | 144k | inline SDValue getI32Imm(unsigned Imm, const SDLoc &dl) { |
188 | 144k | return CurDAG->getTargetConstant(Imm, dl, MVT::i32); |
189 | 144k | } |
190 | | |
191 | | /// getI64Imm - Return a target constant with the specified value, of type |
192 | | /// i64. |
193 | 40.2k | inline SDValue getI64Imm(uint64_t Imm, const SDLoc &dl) { |
194 | 40.2k | return CurDAG->getTargetConstant(Imm, dl, MVT::i64); |
195 | 40.2k | } |
196 | | |
197 | | /// getSmallIPtrImm - Return a target constant of pointer type. |
198 | 19.9k | inline SDValue getSmallIPtrImm(uint64_t Imm, const SDLoc &dl) { |
199 | 19.9k | return CurDAG->getTargetConstant( |
200 | 19.9k | Imm, dl, PPCLowering->getPointerTy(CurDAG->getDataLayout())); |
201 | 19.9k | } |
202 | | |
203 | | /// isRotateAndMask - Returns true if Mask and Shift can be folded into a |
204 | | /// rotate and mask opcode and mask operation. |
205 | | static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask, |
206 | | unsigned &SH, unsigned &MB, unsigned &ME); |
207 | | |
208 | | /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC |
209 | | /// base register. Return the virtual register that holds this value. |
210 | | SDNode *getGlobalBaseReg(); |
211 | | |
212 | | void selectFrameIndex(SDNode *SN, SDNode *N, uint64_t Offset = 0); |
213 | | |
214 | | // Select - Convert the specified operand from a target-independent to a |
215 | | // target-specific node if it hasn't already been changed. |
216 | | void Select(SDNode *N) override; |
217 | | |
218 | | bool tryBitfieldInsert(SDNode *N); |
219 | | bool tryBitPermutation(SDNode *N); |
220 | | bool tryIntCompareInGPR(SDNode *N); |
221 | | |
222 | | // tryTLSXFormLoad - Convert an ISD::LOAD fed by a PPCISD::ADD_TLS into |
223 | | // an X-Form load instruction with the offset being a relocation coming from |
224 | | // the PPCISD::ADD_TLS. |
225 | | bool tryTLSXFormLoad(LoadSDNode *N); |
226 | | // tryTLSXFormStore - Convert an ISD::STORE fed by a PPCISD::ADD_TLS into |
227 | | // an X-Form store instruction with the offset being a relocation coming from |
228 | | // the PPCISD::ADD_TLS. |
229 | | bool tryTLSXFormStore(StoreSDNode *N); |
230 | | /// SelectCC - Select a comparison of the specified values with the |
231 | | /// specified condition code, returning the CR# of the expression. |
232 | | SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, |
233 | | const SDLoc &dl, SDValue Chain = SDValue()); |
234 | | |
235 | | /// SelectAddrImmOffs - Return true if the operand is valid for a preinc |
236 | | /// immediate field. Note that the operand at this point is already the |
237 | | /// result of a prior SelectAddressRegImm call. |
238 | 2.35k | bool SelectAddrImmOffs(SDValue N, SDValue &Out) const { |
239 | 2.35k | if (N.getOpcode() == ISD::TargetConstant || |
240 | 2.35k | N.getOpcode() == ISD::TargetGlobalAddress) { |
241 | 410 | Out = N; |
242 | 410 | return true; |
243 | 410 | } |
244 | | |
245 | 1.94k | return false; |
246 | 2.35k | } |
247 | | |
248 | | /// SelectDSForm - Returns true if address N can be represented by the |
249 | | /// addressing mode of DSForm instructions (a base register, plus a signed |
250 | | /// 16-bit displacement that is a multiple of 4. |
251 | 128k | bool SelectDSForm(SDNode *Parent, SDValue N, SDValue &Disp, SDValue &Base) { |
252 | 128k | return PPCLowering->SelectOptimalAddrMode(Parent, N, Disp, Base, *CurDAG, |
253 | 128k | Align(4)) == PPC::AM_DSForm; |
254 | 128k | } |
255 | | |
256 | | /// SelectDQForm - Returns true if address N can be represented by the |
257 | | /// addressing mode of DQForm instructions (a base register, plus a signed |
258 | | /// 16-bit displacement that is a multiple of 16. |
259 | 0 | bool SelectDQForm(SDNode *Parent, SDValue N, SDValue &Disp, SDValue &Base) { |
260 | 0 | return PPCLowering->SelectOptimalAddrMode(Parent, N, Disp, Base, *CurDAG, |
261 | 0 | Align(16)) == PPC::AM_DQForm; |
262 | 0 | } |
263 | | |
264 | | /// SelectDForm - Returns true if address N can be represented by |
265 | | /// the addressing mode of DForm instructions (a base register, plus a |
266 | | /// signed 16-bit immediate. |
267 | 174k | bool SelectDForm(SDNode *Parent, SDValue N, SDValue &Disp, SDValue &Base) { |
268 | 174k | return PPCLowering->SelectOptimalAddrMode(Parent, N, Disp, Base, *CurDAG, |
269 | 174k | std::nullopt) == PPC::AM_DForm; |
270 | 174k | } |
271 | | |
272 | | /// SelectPCRelForm - Returns true if address N can be represented by |
273 | | /// PC-Relative addressing mode. |
274 | | bool SelectPCRelForm(SDNode *Parent, SDValue N, SDValue &Disp, |
275 | 0 | SDValue &Base) { |
276 | 0 | return PPCLowering->SelectOptimalAddrMode(Parent, N, Disp, Base, *CurDAG, |
277 | 0 | std::nullopt) == PPC::AM_PCRel; |
278 | 0 | } |
279 | | |
280 | | /// SelectPDForm - Returns true if address N can be represented by Prefixed |
281 | | /// DForm addressing mode (a base register, plus a signed 34-bit immediate. |
282 | 0 | bool SelectPDForm(SDNode *Parent, SDValue N, SDValue &Disp, SDValue &Base) { |
283 | 0 | return PPCLowering->SelectOptimalAddrMode(Parent, N, Disp, Base, *CurDAG, |
284 | 0 | std::nullopt) == |
285 | 0 | PPC::AM_PrefixDForm; |
286 | 0 | } |
287 | | |
288 | | /// SelectXForm - Returns true if address N can be represented by the |
289 | | /// addressing mode of XForm instructions (an indexed [r+r] operation). |
290 | 48.4k | bool SelectXForm(SDNode *Parent, SDValue N, SDValue &Disp, SDValue &Base) { |
291 | 48.4k | return PPCLowering->SelectOptimalAddrMode(Parent, N, Disp, Base, *CurDAG, |
292 | 48.4k | std::nullopt) == PPC::AM_XForm; |
293 | 48.4k | } |
294 | | |
295 | | /// SelectForceXForm - Given the specified address, force it to be |
296 | | /// represented as an indexed [r+r] operation (an XForm instruction). |
297 | | bool SelectForceXForm(SDNode *Parent, SDValue N, SDValue &Disp, |
298 | 192 | SDValue &Base) { |
299 | 192 | return PPCLowering->SelectForceXFormMode(N, Disp, Base, *CurDAG) == |
300 | 192 | PPC::AM_XForm; |
301 | 192 | } |
302 | | |
303 | | /// SelectAddrIdx - Given the specified address, check to see if it can be |
304 | | /// represented as an indexed [r+r] operation. |
305 | | /// This is for xform instructions whose associated displacement form is D. |
306 | | /// The last parameter \p 0 means associated D form has no requirment for 16 |
307 | | /// bit signed displacement. |
308 | | /// Returns false if it can be represented by [r+imm], which are preferred. |
309 | 0 | bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) { |
310 | 0 | return PPCLowering->SelectAddressRegReg(N, Base, Index, *CurDAG, |
311 | 0 | std::nullopt); |
312 | 0 | } |
313 | | |
314 | | /// SelectAddrIdx4 - Given the specified address, check to see if it can be |
315 | | /// represented as an indexed [r+r] operation. |
316 | | /// This is for xform instructions whose associated displacement form is DS. |
317 | | /// The last parameter \p 4 means associated DS form 16 bit signed |
318 | | /// displacement must be a multiple of 4. |
319 | | /// Returns false if it can be represented by [r+imm], which are preferred. |
320 | 0 | bool SelectAddrIdxX4(SDValue N, SDValue &Base, SDValue &Index) { |
321 | 0 | return PPCLowering->SelectAddressRegReg(N, Base, Index, *CurDAG, |
322 | 0 | Align(4)); |
323 | 0 | } |
324 | | |
325 | | /// SelectAddrIdx16 - Given the specified address, check to see if it can be |
326 | | /// represented as an indexed [r+r] operation. |
327 | | /// This is for xform instructions whose associated displacement form is DQ. |
328 | | /// The last parameter \p 16 means associated DQ form 16 bit signed |
329 | | /// displacement must be a multiple of 16. |
330 | | /// Returns false if it can be represented by [r+imm], which are preferred. |
331 | 0 | bool SelectAddrIdxX16(SDValue N, SDValue &Base, SDValue &Index) { |
332 | 0 | return PPCLowering->SelectAddressRegReg(N, Base, Index, *CurDAG, |
333 | 0 | Align(16)); |
334 | 0 | } |
335 | | |
336 | | /// SelectAddrIdxOnly - Given the specified address, force it to be |
337 | | /// represented as an indexed [r+r] operation. |
338 | 5 | bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) { |
339 | 5 | return PPCLowering->SelectAddressRegRegOnly(N, Base, Index, *CurDAG); |
340 | 5 | } |
341 | | |
342 | | /// SelectAddrImm - Returns true if the address N can be represented by |
343 | | /// a base register plus a signed 16-bit displacement [r+imm]. |
344 | | /// The last parameter \p 0 means D form has no requirment for 16 bit signed |
345 | | /// displacement. |
346 | | bool SelectAddrImm(SDValue N, SDValue &Disp, |
347 | 5.19k | SDValue &Base) { |
348 | 5.19k | return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, |
349 | 5.19k | std::nullopt); |
350 | 5.19k | } |
351 | | |
352 | | /// SelectAddrImmX4 - Returns true if the address N can be represented by |
353 | | /// a base register plus a signed 16-bit displacement that is a multiple of |
354 | | /// 4 (last parameter). Suitable for use by STD and friends. |
355 | 122 | bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) { |
356 | 122 | return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, Align(4)); |
357 | 122 | } |
358 | | |
359 | | /// SelectAddrImmX16 - Returns true if the address N can be represented by |
360 | | /// a base register plus a signed 16-bit displacement that is a multiple of |
361 | | /// 16(last parameter). Suitable for use by STXV and friends. |
362 | 0 | bool SelectAddrImmX16(SDValue N, SDValue &Disp, SDValue &Base) { |
363 | 0 | return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, |
364 | 0 | Align(16)); |
365 | 0 | } |
366 | | |
367 | | /// SelectAddrImmX34 - Returns true if the address N can be represented by |
368 | | /// a base register plus a signed 34-bit displacement. Suitable for use by |
369 | | /// PSTXVP and friends. |
370 | 0 | bool SelectAddrImmX34(SDValue N, SDValue &Disp, SDValue &Base) { |
371 | 0 | return PPCLowering->SelectAddressRegImm34(N, Disp, Base, *CurDAG); |
372 | 0 | } |
373 | | |
374 | | // Select an address into a single register. |
375 | 0 | bool SelectAddr(SDValue N, SDValue &Base) { |
376 | 0 | Base = N; |
377 | 0 | return true; |
378 | 0 | } |
379 | | |
380 | 0 | bool SelectAddrPCRel(SDValue N, SDValue &Base) { |
381 | 0 | return PPCLowering->SelectAddressPCRel(N, Base); |
382 | 0 | } |
383 | | |
384 | | /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for |
385 | | /// inline asm expressions. It is always correct to compute the value into |
386 | | /// a register. The case of adding a (possibly relocatable) constant to a |
387 | | /// register can be improved, but it is wrong to substitute Reg+Reg for |
388 | | /// Reg in an asm, because the load or store opcode would have to change. |
389 | | bool SelectInlineAsmMemoryOperand(const SDValue &Op, |
390 | | InlineAsm::ConstraintCode ConstraintID, |
391 | 0 | std::vector<SDValue> &OutOps) override { |
392 | 0 | switch(ConstraintID) { |
393 | 0 | default: |
394 | 0 | errs() << "ConstraintID: " |
395 | 0 | << InlineAsm::getMemConstraintName(ConstraintID) << "\n"; |
396 | 0 | llvm_unreachable("Unexpected asm memory constraint"); |
397 | 0 | case InlineAsm::ConstraintCode::es: |
398 | 0 | case InlineAsm::ConstraintCode::m: |
399 | 0 | case InlineAsm::ConstraintCode::o: |
400 | 0 | case InlineAsm::ConstraintCode::Q: |
401 | 0 | case InlineAsm::ConstraintCode::Z: |
402 | 0 | case InlineAsm::ConstraintCode::Zy: |
403 | | // We need to make sure that this one operand does not end up in r0 |
404 | | // (because we might end up lowering this as 0(%op)). |
405 | 0 | const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo(); |
406 | 0 | const TargetRegisterClass *TRC = TRI->getPointerRegClass(*MF, /*Kind=*/1); |
407 | 0 | SDLoc dl(Op); |
408 | 0 | SDValue RC = CurDAG->getTargetConstant(TRC->getID(), dl, MVT::i32); |
409 | 0 | SDValue NewOp = |
410 | 0 | SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, |
411 | 0 | dl, Op.getValueType(), |
412 | 0 | Op, RC), 0); |
413 | |
|
414 | 0 | OutOps.push_back(NewOp); |
415 | 0 | return false; |
416 | 0 | } |
417 | 0 | return true; |
418 | 0 | } |
419 | | |
420 | | // Include the pieces autogenerated from the target description. |
421 | | #include "PPCGenDAGISel.inc" |
422 | | |
423 | | private: |
424 | | bool trySETCC(SDNode *N); |
425 | | bool tryFoldSWTestBRCC(SDNode *N); |
426 | | bool trySelectLoopCountIntrinsic(SDNode *N); |
427 | | bool tryAsSingleRLDICL(SDNode *N); |
428 | | bool tryAsSingleRLDCL(SDNode *N); |
429 | | bool tryAsSingleRLDICR(SDNode *N); |
430 | | bool tryAsSingleRLWINM(SDNode *N); |
431 | | bool tryAsSingleRLWINM8(SDNode *N); |
432 | | bool tryAsSingleRLWIMI(SDNode *N); |
433 | | bool tryAsPairOfRLDICL(SDNode *N); |
434 | | bool tryAsSingleRLDIMI(SDNode *N); |
435 | | |
436 | | void PeepholePPC64(); |
437 | | void PeepholePPC64ZExt(); |
438 | | void PeepholeCROps(); |
439 | | |
440 | | SDValue combineToCMPB(SDNode *N); |
441 | | void foldBoolExts(SDValue &Res, SDNode *&N); |
442 | | |
443 | | bool AllUsersSelectZero(SDNode *N); |
444 | | void SwapAllSelectUsers(SDNode *N); |
445 | | |
446 | | bool isOffsetMultipleOf(SDNode *N, unsigned Val) const; |
447 | | void transferMemOperands(SDNode *N, SDNode *Result); |
448 | | }; |
449 | | |
450 | | } // end anonymous namespace |
451 | | |
452 | | char PPCDAGToDAGISel::ID = 0; |
453 | | |
454 | | INITIALIZE_PASS(PPCDAGToDAGISel, DEBUG_TYPE, PASS_NAME, false, false) |
455 | | |
456 | | /// getGlobalBaseReg - Output the instructions required to put the |
457 | | /// base address to use for accessing globals into a register. |
458 | | /// |
459 | 0 | SDNode *PPCDAGToDAGISel::getGlobalBaseReg() { |
460 | 0 | if (!GlobalBaseReg) { |
461 | 0 | const TargetInstrInfo &TII = *Subtarget->getInstrInfo(); |
462 | | // Insert the set of GlobalBaseReg into the first MBB of the function |
463 | 0 | MachineBasicBlock &FirstMBB = MF->front(); |
464 | 0 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
465 | 0 | const Module *M = MF->getFunction().getParent(); |
466 | 0 | DebugLoc dl; |
467 | |
|
468 | 0 | if (PPCLowering->getPointerTy(CurDAG->getDataLayout()) == MVT::i32) { |
469 | 0 | if (Subtarget->isTargetELF()) { |
470 | 0 | GlobalBaseReg = PPC::R30; |
471 | 0 | if (!Subtarget->isSecurePlt() && |
472 | 0 | M->getPICLevel() == PICLevel::SmallPIC) { |
473 | 0 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MoveGOTtoLR)); |
474 | 0 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg); |
475 | 0 | MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true); |
476 | 0 | } else { |
477 | 0 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR)); |
478 | 0 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg); |
479 | 0 | Register TempReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass); |
480 | 0 | BuildMI(FirstMBB, MBBI, dl, |
481 | 0 | TII.get(PPC::UpdateGBR), GlobalBaseReg) |
482 | 0 | .addReg(TempReg, RegState::Define).addReg(GlobalBaseReg); |
483 | 0 | MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true); |
484 | 0 | } |
485 | 0 | } else { |
486 | 0 | GlobalBaseReg = |
487 | 0 | RegInfo->createVirtualRegister(&PPC::GPRC_and_GPRC_NOR0RegClass); |
488 | 0 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR)); |
489 | 0 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg); |
490 | 0 | } |
491 | 0 | } else { |
492 | | // We must ensure that this sequence is dominated by the prologue. |
493 | | // FIXME: This is a bit of a big hammer since we don't get the benefits |
494 | | // of shrink-wrapping whenever we emit this instruction. Considering |
495 | | // this is used in any function where we emit a jump table, this may be |
496 | | // a significant limitation. We should consider inserting this in the |
497 | | // block where it is used and then commoning this sequence up if it |
498 | | // appears in multiple places. |
499 | | // Note: on ISA 3.0 cores, we can use lnia (addpcis) instead of |
500 | | // MovePCtoLR8. |
501 | 0 | MF->getInfo<PPCFunctionInfo>()->setShrinkWrapDisabled(true); |
502 | 0 | GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_and_G8RC_NOX0RegClass); |
503 | 0 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8)); |
504 | 0 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg); |
505 | 0 | } |
506 | 0 | } |
507 | 0 | return CurDAG->getRegister(GlobalBaseReg, |
508 | 0 | PPCLowering->getPointerTy(CurDAG->getDataLayout())) |
509 | 0 | .getNode(); |
510 | 0 | } |
511 | | |
512 | | // Check if a SDValue has the toc-data attribute. |
513 | 0 | static bool hasTocDataAttr(SDValue Val, unsigned PointerSize) { |
514 | 0 | GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Val); |
515 | 0 | if (!GA) |
516 | 0 | return false; |
517 | | |
518 | 0 | const GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(GA->getGlobal()); |
519 | 0 | if (!GV) |
520 | 0 | return false; |
521 | | |
522 | 0 | if (!GV->hasAttribute("toc-data")) |
523 | 0 | return false; |
524 | | |
525 | | // TODO: These asserts should be updated as more support for the toc data |
526 | | // transformation is added (struct support, etc.). |
527 | | |
528 | 0 | assert( |
529 | 0 | PointerSize >= GV->getAlign().valueOrOne().value() && |
530 | 0 | "GlobalVariables with an alignment requirement stricter than TOC entry " |
531 | 0 | "size not supported by the toc data transformation."); |
532 | | |
533 | 0 | Type *GVType = GV->getValueType(); |
534 | |
|
535 | 0 | assert(GVType->isSized() && "A GlobalVariable's size must be known to be " |
536 | 0 | "supported by the toc data transformation."); |
537 | | |
538 | 0 | if (GVType->isVectorTy()) |
539 | 0 | report_fatal_error("A GlobalVariable of Vector type is not currently " |
540 | 0 | "supported by the toc data transformation."); |
541 | |
|
542 | 0 | if (GVType->isArrayTy()) |
543 | 0 | report_fatal_error("A GlobalVariable of Array type is not currently " |
544 | 0 | "supported by the toc data transformation."); |
545 | |
|
546 | 0 | if (GVType->isStructTy()) |
547 | 0 | report_fatal_error("A GlobalVariable of Struct type is not currently " |
548 | 0 | "supported by the toc data transformation."); |
549 | |
|
550 | 0 | assert(GVType->getPrimitiveSizeInBits() <= PointerSize * 8 && |
551 | 0 | "A GlobalVariable with size larger than a TOC entry is not currently " |
552 | 0 | "supported by the toc data transformation."); |
553 | | |
554 | 0 | if (GV->hasLocalLinkage() || GV->hasPrivateLinkage()) |
555 | 0 | report_fatal_error("A GlobalVariable with private or local linkage is not " |
556 | 0 | "currently supported by the toc data transformation."); |
557 | |
|
558 | 0 | assert(!GV->hasCommonLinkage() && |
559 | 0 | "Tentative definitions cannot have the mapping class XMC_TD."); |
560 | | |
561 | 0 | return true; |
562 | 0 | } |
563 | | |
564 | | /// isInt32Immediate - This method tests to see if the node is a 32-bit constant |
565 | | /// operand. If so Imm will receive the 32-bit value. |
566 | 81.2k | static bool isInt32Immediate(SDNode *N, unsigned &Imm) { |
567 | 81.2k | if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) { |
568 | 22.6k | Imm = N->getAsZExtVal(); |
569 | 22.6k | return true; |
570 | 22.6k | } |
571 | 58.6k | return false; |
572 | 81.2k | } |
573 | | |
574 | | /// isInt64Immediate - This method tests to see if the node is a 64-bit constant |
575 | | /// operand. If so Imm will receive the 64-bit value. |
576 | 224k | static bool isInt64Immediate(SDNode *N, uint64_t &Imm) { |
577 | 224k | if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) { |
578 | 79.5k | Imm = N->getAsZExtVal(); |
579 | 79.5k | return true; |
580 | 79.5k | } |
581 | 144k | return false; |
582 | 224k | } |
583 | | |
584 | | // isInt32Immediate - This method tests to see if a constant operand. |
585 | | // If so Imm will receive the 32 bit value. |
586 | 73.3k | static bool isInt32Immediate(SDValue N, unsigned &Imm) { |
587 | 73.3k | return isInt32Immediate(N.getNode(), Imm); |
588 | 73.3k | } |
589 | | |
590 | | /// isInt64Immediate - This method tests to see if the value is a 64-bit |
591 | | /// constant operand. If so Imm will receive the 64-bit value. |
592 | 64.3k | static bool isInt64Immediate(SDValue N, uint64_t &Imm) { |
593 | 64.3k | return isInt64Immediate(N.getNode(), Imm); |
594 | 64.3k | } |
595 | | |
596 | | static unsigned getBranchHint(unsigned PCC, |
597 | | const FunctionLoweringInfo &FuncInfo, |
598 | 9.90k | const SDValue &DestMBB) { |
599 | 9.90k | assert(isa<BasicBlockSDNode>(DestMBB)); |
600 | | |
601 | 9.90k | if (!FuncInfo.BPI) return PPC::BR_NO_HINT; |
602 | | |
603 | 9.90k | const BasicBlock *BB = FuncInfo.MBB->getBasicBlock(); |
604 | 9.90k | const Instruction *BBTerm = BB->getTerminator(); |
605 | | |
606 | 9.90k | if (BBTerm->getNumSuccessors() != 2) return PPC::BR_NO_HINT; |
607 | | |
608 | 9.57k | const BasicBlock *TBB = BBTerm->getSuccessor(0); |
609 | 9.57k | const BasicBlock *FBB = BBTerm->getSuccessor(1); |
610 | | |
611 | 9.57k | auto TProb = FuncInfo.BPI->getEdgeProbability(BB, TBB); |
612 | 9.57k | auto FProb = FuncInfo.BPI->getEdgeProbability(BB, FBB); |
613 | | |
614 | | // We only want to handle cases which are easy to predict at static time, e.g. |
615 | | // C++ throw statement, that is very likely not taken, or calling never |
616 | | // returned function, e.g. stdlib exit(). So we set Threshold to filter |
617 | | // unwanted cases. |
618 | | // |
619 | | // Below is LLVM branch weight table, we only want to handle case 1, 2 |
620 | | // |
621 | | // Case Taken:Nontaken Example |
622 | | // 1. Unreachable 1048575:1 C++ throw, stdlib exit(), |
623 | | // 2. Invoke-terminating 1:1048575 |
624 | | // 3. Coldblock 4:64 __builtin_expect |
625 | | // 4. Loop Branch 124:4 For loop |
626 | | // 5. PH/ZH/FPH 20:12 |
627 | 9.57k | const uint32_t Threshold = 10000; |
628 | | |
629 | 9.57k | if (std::max(TProb, FProb) / Threshold < std::min(TProb, FProb)) |
630 | 9.55k | return PPC::BR_NO_HINT; |
631 | | |
632 | 15 | LLVM_DEBUG(dbgs() << "Use branch hint for '" << FuncInfo.Fn->getName() |
633 | 15 | << "::" << BB->getName() << "'\n" |
634 | 15 | << " -> " << TBB->getName() << ": " << TProb << "\n" |
635 | 15 | << " -> " << FBB->getName() << ": " << FProb << "\n"); |
636 | | |
637 | 15 | const BasicBlockSDNode *BBDN = cast<BasicBlockSDNode>(DestMBB); |
638 | | |
639 | | // If Dest BasicBlock is False-BasicBlock (FBB), swap branch probabilities, |
640 | | // because we want 'TProb' stands for 'branch probability' to Dest BasicBlock |
641 | 15 | if (BBDN->getBasicBlock()->getBasicBlock() != TBB) |
642 | 6 | std::swap(TProb, FProb); |
643 | | |
644 | 15 | return (TProb > FProb) ? PPC::BR_TAKEN_HINT : PPC::BR_NONTAKEN_HINT; |
645 | 9.57k | } |
646 | | |
647 | | // isOpcWithIntImmediate - This method tests to see if the node is a specific |
648 | | // opcode and that it has a immediate integer right operand. |
649 | | // If so Imm will receive the 32 bit value. |
650 | 24.3k | static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) { |
651 | 24.3k | return N->getOpcode() == Opc |
652 | 24.3k | && isInt32Immediate(N->getOperand(1).getNode(), Imm); |
653 | 24.3k | } |
654 | | |
655 | 19.9k | void PPCDAGToDAGISel::selectFrameIndex(SDNode *SN, SDNode *N, uint64_t Offset) { |
656 | 19.9k | SDLoc dl(SN); |
657 | 19.9k | int FI = cast<FrameIndexSDNode>(N)->getIndex(); |
658 | 19.9k | SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0)); |
659 | 19.9k | unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8; |
660 | 19.9k | if (SN->hasOneUse()) |
661 | 13.4k | CurDAG->SelectNodeTo(SN, Opc, N->getValueType(0), TFI, |
662 | 13.4k | getSmallIPtrImm(Offset, dl)); |
663 | 6.44k | else |
664 | 6.44k | ReplaceNode(SN, CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI, |
665 | 6.44k | getSmallIPtrImm(Offset, dl))); |
666 | 19.9k | } |
667 | | |
668 | | bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask, |
669 | | bool isShiftMask, unsigned &SH, |
670 | 17.6k | unsigned &MB, unsigned &ME) { |
671 | | // Don't even go down this path for i64, since different logic will be |
672 | | // necessary for rldicl/rldicr/rldimi. |
673 | 17.6k | if (N->getValueType(0) != MVT::i32) |
674 | 0 | return false; |
675 | | |
676 | 17.6k | unsigned Shift = 32; |
677 | 17.6k | unsigned Indeterminant = ~0; // bit mask marking indeterminant results |
678 | 17.6k | unsigned Opcode = N->getOpcode(); |
679 | 17.6k | if (N->getNumOperands() != 2 || |
680 | 17.6k | !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31)) |
681 | 17.5k | return false; |
682 | | |
683 | 95 | if (Opcode == ISD::SHL) { |
684 | | // apply shift left to mask if it comes first |
685 | 0 | if (isShiftMask) Mask = Mask << Shift; |
686 | | // determine which bits are made indeterminant by shift |
687 | 0 | Indeterminant = ~(0xFFFFFFFFu << Shift); |
688 | 95 | } else if (Opcode == ISD::SRL) { |
689 | | // apply shift right to mask if it comes first |
690 | 0 | if (isShiftMask) Mask = Mask >> Shift; |
691 | | // determine which bits are made indeterminant by shift |
692 | 0 | Indeterminant = ~(0xFFFFFFFFu >> Shift); |
693 | | // adjust for the left rotate |
694 | 0 | Shift = 32 - Shift; |
695 | 95 | } else if (Opcode == ISD::ROTL) { |
696 | 0 | Indeterminant = 0; |
697 | 95 | } else { |
698 | 95 | return false; |
699 | 95 | } |
700 | | |
701 | | // if the mask doesn't intersect any Indeterminant bits |
702 | 0 | if (Mask && !(Mask & Indeterminant)) { |
703 | 0 | SH = Shift & 31; |
704 | | // make sure the mask is still a mask (wrap arounds may not be) |
705 | 0 | return isRunOfOnes(Mask, MB, ME); |
706 | 0 | } |
707 | 0 | return false; |
708 | 0 | } |
709 | | |
710 | | // isThreadPointerAcquisitionNode - Check if the operands of an ADD_TLS |
711 | | // instruction use the thread pointer. |
712 | 0 | static bool isThreadPointerAcquisitionNode(SDValue Base, SelectionDAG *CurDAG) { |
713 | 0 | assert( |
714 | 0 | Base.getOpcode() == PPCISD::ADD_TLS && |
715 | 0 | "Only expecting the ADD_TLS instruction to acquire the thread pointer!"); |
716 | 0 | const PPCSubtarget &Subtarget = |
717 | 0 | CurDAG->getMachineFunction().getSubtarget<PPCSubtarget>(); |
718 | 0 | SDValue ADDTLSOp1 = Base.getOperand(0); |
719 | 0 | unsigned ADDTLSOp1Opcode = ADDTLSOp1.getOpcode(); |
720 | | |
721 | | // Account for when ADD_TLS is used for the initial-exec TLS model on Linux. |
722 | | // |
723 | | // Although ADD_TLS does not explicitly use the thread pointer |
724 | | // register when LD_GOT_TPREL_L is one of it's operands, the LD_GOT_TPREL_L |
725 | | // instruction will have a relocation specifier, @got@tprel, that is used to |
726 | | // generate a GOT entry. The linker replaces this entry with an offset for a |
727 | | // for a thread local variable, which will be relative to the thread pointer. |
728 | 0 | if (ADDTLSOp1Opcode == PPCISD::LD_GOT_TPREL_L) |
729 | 0 | return true; |
730 | | // When using PC-Relative instructions for initial-exec, a MAT_PCREL_ADDR |
731 | | // node is produced instead to represent the aforementioned situation. |
732 | 0 | LoadSDNode *LD = dyn_cast<LoadSDNode>(ADDTLSOp1); |
733 | 0 | if (LD && LD->getBasePtr().getOpcode() == PPCISD::MAT_PCREL_ADDR) |
734 | 0 | return true; |
735 | | |
736 | | // A GET_TPOINTER PPCISD node (only produced on AIX 32-bit mode) as an operand |
737 | | // to ADD_TLS represents a call to .__get_tpointer to get the thread pointer, |
738 | | // later returning it into R3. |
739 | 0 | if (ADDTLSOp1Opcode == PPCISD::GET_TPOINTER) |
740 | 0 | return true; |
741 | | |
742 | | // The ADD_TLS note is explicitly acquiring the thread pointer (X13/R13). |
743 | 0 | RegisterSDNode *AddFirstOpReg = |
744 | 0 | dyn_cast_or_null<RegisterSDNode>(ADDTLSOp1.getNode()); |
745 | 0 | if (AddFirstOpReg && |
746 | 0 | AddFirstOpReg->getReg() == Subtarget.getThreadPointerRegister()) |
747 | 0 | return true; |
748 | | |
749 | 0 | return false; |
750 | 0 | } |
751 | | |
752 | | // canOptimizeTLSDFormToXForm - Optimize TLS accesses when an ADD_TLS |
753 | | // instruction is present. An ADD_TLS instruction, followed by a D-Form memory |
754 | | // operation, can be optimized to use an X-Form load or store, allowing the |
755 | | // ADD_TLS node to be removed completely. |
756 | 0 | static bool canOptimizeTLSDFormToXForm(SelectionDAG *CurDAG, SDValue Base) { |
757 | | |
758 | | // Do not do this transformation at -O0. |
759 | 0 | if (CurDAG->getTarget().getOptLevel() == CodeGenOptLevel::None) |
760 | 0 | return false; |
761 | | |
762 | | // In order to perform this optimization inside tryTLSXForm[Load|Store], |
763 | | // Base is expected to be an ADD_TLS node. |
764 | 0 | if (Base.getOpcode() != PPCISD::ADD_TLS) |
765 | 0 | return false; |
766 | 0 | for (auto *ADDTLSUse : Base.getNode()->uses()) { |
767 | | // The optimization to convert the D-Form load/store into its X-Form |
768 | | // counterpart should only occur if the source value offset of the load/ |
769 | | // store is 0. This also means that The offset should always be undefined. |
770 | 0 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(ADDTLSUse)) { |
771 | 0 | if (LD->getSrcValueOffset() != 0 || !LD->getOffset().isUndef()) |
772 | 0 | return false; |
773 | 0 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(ADDTLSUse)) { |
774 | 0 | if (ST->getSrcValueOffset() != 0 || !ST->getOffset().isUndef()) |
775 | 0 | return false; |
776 | 0 | } else // Don't optimize if there are ADD_TLS users that aren't load/stores. |
777 | 0 | return false; |
778 | 0 | } |
779 | | |
780 | 0 | if (Base.getOperand(1).getOpcode() == PPCISD::TLS_LOCAL_EXEC_MAT_ADDR) |
781 | 0 | return false; |
782 | | |
783 | | // Does the ADD_TLS node of the load/store use the thread pointer? |
784 | | // If the thread pointer is not used as one of the operands of ADD_TLS, |
785 | | // then this optimization is not valid. |
786 | 0 | return isThreadPointerAcquisitionNode(Base, CurDAG); |
787 | 0 | } |
788 | | |
789 | 0 | bool PPCDAGToDAGISel::tryTLSXFormStore(StoreSDNode *ST) { |
790 | 0 | SDValue Base = ST->getBasePtr(); |
791 | 0 | if (!canOptimizeTLSDFormToXForm(CurDAG, Base)) |
792 | 0 | return false; |
793 | | |
794 | 0 | SDLoc dl(ST); |
795 | 0 | EVT MemVT = ST->getMemoryVT(); |
796 | 0 | EVT RegVT = ST->getValue().getValueType(); |
797 | |
|
798 | 0 | unsigned Opcode; |
799 | 0 | switch (MemVT.getSimpleVT().SimpleTy) { |
800 | 0 | default: |
801 | 0 | return false; |
802 | 0 | case MVT::i8: { |
803 | 0 | Opcode = (RegVT == MVT::i32) ? PPC::STBXTLS_32 : PPC::STBXTLS; |
804 | 0 | break; |
805 | 0 | } |
806 | 0 | case MVT::i16: { |
807 | 0 | Opcode = (RegVT == MVT::i32) ? PPC::STHXTLS_32 : PPC::STHXTLS; |
808 | 0 | break; |
809 | 0 | } |
810 | 0 | case MVT::i32: { |
811 | 0 | Opcode = (RegVT == MVT::i32) ? PPC::STWXTLS_32 : PPC::STWXTLS; |
812 | 0 | break; |
813 | 0 | } |
814 | 0 | case MVT::i64: { |
815 | 0 | Opcode = PPC::STDXTLS; |
816 | 0 | break; |
817 | 0 | } |
818 | 0 | case MVT::f32: { |
819 | 0 | Opcode = PPC::STFSXTLS; |
820 | 0 | break; |
821 | 0 | } |
822 | 0 | case MVT::f64: { |
823 | 0 | Opcode = PPC::STFDXTLS; |
824 | 0 | break; |
825 | 0 | } |
826 | 0 | } |
827 | 0 | SDValue Chain = ST->getChain(); |
828 | 0 | SDVTList VTs = ST->getVTList(); |
829 | 0 | SDValue Ops[] = {ST->getValue(), Base.getOperand(0), Base.getOperand(1), |
830 | 0 | Chain}; |
831 | 0 | SDNode *MN = CurDAG->getMachineNode(Opcode, dl, VTs, Ops); |
832 | 0 | transferMemOperands(ST, MN); |
833 | 0 | ReplaceNode(ST, MN); |
834 | 0 | return true; |
835 | 0 | } |
836 | | |
837 | 0 | bool PPCDAGToDAGISel::tryTLSXFormLoad(LoadSDNode *LD) { |
838 | 0 | SDValue Base = LD->getBasePtr(); |
839 | 0 | if (!canOptimizeTLSDFormToXForm(CurDAG, Base)) |
840 | 0 | return false; |
841 | | |
842 | 0 | SDLoc dl(LD); |
843 | 0 | EVT MemVT = LD->getMemoryVT(); |
844 | 0 | EVT RegVT = LD->getValueType(0); |
845 | 0 | bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD; |
846 | 0 | unsigned Opcode; |
847 | 0 | switch (MemVT.getSimpleVT().SimpleTy) { |
848 | 0 | default: |
849 | 0 | return false; |
850 | 0 | case MVT::i8: { |
851 | 0 | Opcode = (RegVT == MVT::i32) ? PPC::LBZXTLS_32 : PPC::LBZXTLS; |
852 | 0 | break; |
853 | 0 | } |
854 | 0 | case MVT::i16: { |
855 | 0 | if (RegVT == MVT::i32) |
856 | 0 | Opcode = isSExt ? PPC::LHAXTLS_32 : PPC::LHZXTLS_32; |
857 | 0 | else |
858 | 0 | Opcode = isSExt ? PPC::LHAXTLS : PPC::LHZXTLS; |
859 | 0 | break; |
860 | 0 | } |
861 | 0 | case MVT::i32: { |
862 | 0 | if (RegVT == MVT::i32) |
863 | 0 | Opcode = isSExt ? PPC::LWAXTLS_32 : PPC::LWZXTLS_32; |
864 | 0 | else |
865 | 0 | Opcode = isSExt ? PPC::LWAXTLS : PPC::LWZXTLS; |
866 | 0 | break; |
867 | 0 | } |
868 | 0 | case MVT::i64: { |
869 | 0 | Opcode = PPC::LDXTLS; |
870 | 0 | break; |
871 | 0 | } |
872 | 0 | case MVT::f32: { |
873 | 0 | Opcode = PPC::LFSXTLS; |
874 | 0 | break; |
875 | 0 | } |
876 | 0 | case MVT::f64: { |
877 | 0 | Opcode = PPC::LFDXTLS; |
878 | 0 | break; |
879 | 0 | } |
880 | 0 | } |
881 | 0 | SDValue Chain = LD->getChain(); |
882 | 0 | SDVTList VTs = LD->getVTList(); |
883 | 0 | SDValue Ops[] = {Base.getOperand(0), Base.getOperand(1), Chain}; |
884 | 0 | SDNode *MN = CurDAG->getMachineNode(Opcode, dl, VTs, Ops); |
885 | 0 | transferMemOperands(LD, MN); |
886 | 0 | ReplaceNode(LD, MN); |
887 | 0 | return true; |
888 | 0 | } |
889 | | |
890 | | /// Turn an or of two masked values into the rotate left word immediate then |
891 | | /// mask insert (rlwimi) instruction. |
892 | 7.23k | bool PPCDAGToDAGISel::tryBitfieldInsert(SDNode *N) { |
893 | 7.23k | SDValue Op0 = N->getOperand(0); |
894 | 7.23k | SDValue Op1 = N->getOperand(1); |
895 | 7.23k | SDLoc dl(N); |
896 | | |
897 | 7.23k | KnownBits LKnown = CurDAG->computeKnownBits(Op0); |
898 | 7.23k | KnownBits RKnown = CurDAG->computeKnownBits(Op1); |
899 | | |
900 | 7.23k | unsigned TargetMask = LKnown.Zero.getZExtValue(); |
901 | 7.23k | unsigned InsertMask = RKnown.Zero.getZExtValue(); |
902 | | |
903 | 7.23k | if ((TargetMask | InsertMask) == 0xFFFFFFFF) { |
904 | 6.18k | unsigned Op0Opc = Op0.getOpcode(); |
905 | 6.18k | unsigned Op1Opc = Op1.getOpcode(); |
906 | 6.18k | unsigned Value, SH = 0; |
907 | 6.18k | TargetMask = ~TargetMask; |
908 | 6.18k | InsertMask = ~InsertMask; |
909 | | |
910 | | // If the LHS has a foldable shift and the RHS does not, then swap it to the |
911 | | // RHS so that we can fold the shift into the insert. |
912 | 6.18k | if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) { |
913 | 0 | if (Op0.getOperand(0).getOpcode() == ISD::SHL || |
914 | 0 | Op0.getOperand(0).getOpcode() == ISD::SRL) { |
915 | 0 | if (Op1.getOperand(0).getOpcode() != ISD::SHL && |
916 | 0 | Op1.getOperand(0).getOpcode() != ISD::SRL) { |
917 | 0 | std::swap(Op0, Op1); |
918 | 0 | std::swap(Op0Opc, Op1Opc); |
919 | 0 | std::swap(TargetMask, InsertMask); |
920 | 0 | } |
921 | 0 | } |
922 | 6.18k | } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) { |
923 | 65 | if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL && |
924 | 65 | Op1.getOperand(0).getOpcode() != ISD::SRL) { |
925 | 0 | std::swap(Op0, Op1); |
926 | 0 | std::swap(Op0Opc, Op1Opc); |
927 | 0 | std::swap(TargetMask, InsertMask); |
928 | 0 | } |
929 | 65 | } |
930 | | |
931 | 6.18k | unsigned MB, ME; |
932 | 6.18k | if (isRunOfOnes(InsertMask, MB, ME)) { |
933 | 6.17k | if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) && |
934 | 6.17k | isInt32Immediate(Op1.getOperand(1), Value)) { |
935 | 45 | Op1 = Op1.getOperand(0); |
936 | 45 | SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value; |
937 | 45 | } |
938 | 6.17k | if (Op1Opc == ISD::AND) { |
939 | | // The AND mask might not be a constant, and we need to make sure that |
940 | | // if we're going to fold the masking with the insert, all bits not |
941 | | // know to be zero in the mask are known to be one. |
942 | 3 | KnownBits MKnown = CurDAG->computeKnownBits(Op1.getOperand(1)); |
943 | 3 | bool CanFoldMask = InsertMask == MKnown.One.getZExtValue(); |
944 | | |
945 | 3 | unsigned SHOpc = Op1.getOperand(0).getOpcode(); |
946 | 3 | if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask && |
947 | 3 | isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) { |
948 | | // Note that Value must be in range here (less than 32) because |
949 | | // otherwise there would not be any bits set in InsertMask. |
950 | 0 | Op1 = Op1.getOperand(0).getOperand(0); |
951 | 0 | SH = (SHOpc == ISD::SHL) ? Value : 32 - Value; |
952 | 0 | } |
953 | 3 | } |
954 | | |
955 | 6.17k | SH &= 31; |
956 | 6.17k | SDValue Ops[] = { Op0, Op1, getI32Imm(SH, dl), getI32Imm(MB, dl), |
957 | 6.17k | getI32Imm(ME, dl) }; |
958 | 6.17k | ReplaceNode(N, CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops)); |
959 | 6.17k | return true; |
960 | 6.17k | } |
961 | 6.18k | } |
962 | 1.05k | return false; |
963 | 7.23k | } |
964 | | |
965 | 79.7k | static unsigned allUsesTruncate(SelectionDAG *CurDAG, SDNode *N) { |
966 | 79.7k | unsigned MaxTruncation = 0; |
967 | | // Cannot use range-based for loop here as we need the actual use (i.e. we |
968 | | // need the operand number corresponding to the use). A range-based for |
969 | | // will unbox the use and provide an SDNode*. |
970 | 79.7k | for (SDNode::use_iterator Use = N->use_begin(), UseEnd = N->use_end(); |
971 | 108k | Use != UseEnd; ++Use) { |
972 | 89.9k | unsigned Opc = |
973 | 89.9k | Use->isMachineOpcode() ? Use->getMachineOpcode() : Use->getOpcode(); |
974 | 89.9k | switch (Opc) { |
975 | 60.1k | default: return 0; |
976 | 0 | case ISD::TRUNCATE: |
977 | 0 | if (Use->isMachineOpcode()) |
978 | 0 | return 0; |
979 | 0 | MaxTruncation = |
980 | 0 | std::max(MaxTruncation, (unsigned)Use->getValueType(0).getSizeInBits()); |
981 | 0 | continue; |
982 | 0 | case ISD::STORE: { |
983 | 0 | if (Use->isMachineOpcode()) |
984 | 0 | return 0; |
985 | 0 | StoreSDNode *STN = cast<StoreSDNode>(*Use); |
986 | 0 | unsigned MemVTSize = STN->getMemoryVT().getSizeInBits(); |
987 | 0 | if (MemVTSize == 64 || Use.getOperandNo() != 0) |
988 | 0 | return 0; |
989 | 0 | MaxTruncation = std::max(MaxTruncation, MemVTSize); |
990 | 0 | continue; |
991 | 0 | } |
992 | 4.83k | case PPC::STW8: |
993 | 5.29k | case PPC::STWX8: |
994 | 5.31k | case PPC::STWU8: |
995 | 5.37k | case PPC::STWUX8: |
996 | 5.37k | if (Use.getOperandNo() != 0) |
997 | 56 | return 0; |
998 | 5.32k | MaxTruncation = std::max(MaxTruncation, 32u); |
999 | 5.32k | continue; |
1000 | 2.63k | case PPC::STH8: |
1001 | 2.86k | case PPC::STHX8: |
1002 | 2.88k | case PPC::STHU8: |
1003 | 2.91k | case PPC::STHUX8: |
1004 | 2.91k | if (Use.getOperandNo() != 0) |
1005 | 42 | return 0; |
1006 | 2.86k | MaxTruncation = std::max(MaxTruncation, 16u); |
1007 | 2.86k | continue; |
1008 | 20.0k | case PPC::STB8: |
1009 | 21.2k | case PPC::STBX8: |
1010 | 21.3k | case PPC::STBU8: |
1011 | 21.5k | case PPC::STBUX8: |
1012 | 21.5k | if (Use.getOperandNo() != 0) |
1013 | 476 | return 0; |
1014 | 21.0k | MaxTruncation = std::max(MaxTruncation, 8u); |
1015 | 21.0k | continue; |
1016 | 89.9k | } |
1017 | 89.9k | } |
1018 | 19.0k | return MaxTruncation; |
1019 | 79.7k | } |
1020 | | |
1021 | | // For any 32 < Num < 64, check if the Imm contains at least Num consecutive |
1022 | | // zeros and return the number of bits by the left of these consecutive zeros. |
1023 | 15.7k | static int findContiguousZerosAtLeast(uint64_t Imm, unsigned Num) { |
1024 | 15.7k | unsigned HiTZ = llvm::countr_zero<uint32_t>(Hi_32(Imm)); |
1025 | 15.7k | unsigned LoLZ = llvm::countl_zero<uint32_t>(Lo_32(Imm)); |
1026 | 15.7k | if ((HiTZ + LoLZ) >= Num) |
1027 | 419 | return (32 + HiTZ); |
1028 | 15.3k | return 0; |
1029 | 15.7k | } |
1030 | | |
1031 | | // Direct materialization of 64-bit constants by enumerated patterns. |
1032 | | static SDNode *selectI64ImmDirect(SelectionDAG *CurDAG, const SDLoc &dl, |
1033 | 160k | uint64_t Imm, unsigned &InstCnt) { |
1034 | 160k | unsigned TZ = llvm::countr_zero<uint64_t>(Imm); |
1035 | 160k | unsigned LZ = llvm::countl_zero<uint64_t>(Imm); |
1036 | 160k | unsigned TO = llvm::countr_one<uint64_t>(Imm); |
1037 | 160k | unsigned LO = llvm::countl_one<uint64_t>(Imm); |
1038 | 160k | unsigned Hi32 = Hi_32(Imm); |
1039 | 160k | unsigned Lo32 = Lo_32(Imm); |
1040 | 160k | SDNode *Result = nullptr; |
1041 | 160k | unsigned Shift = 0; |
1042 | | |
1043 | 160k | auto getI32Imm = [CurDAG, dl](unsigned Imm) { |
1044 | 142k | return CurDAG->getTargetConstant(Imm, dl, MVT::i32); |
1045 | 142k | }; |
1046 | | |
1047 | | // Following patterns use 1 instructions to materialize the Imm. |
1048 | 160k | InstCnt = 1; |
1049 | | // 1-1) Patterns : {zeros}{15-bit valve} |
1050 | | // {ones}{15-bit valve} |
1051 | 160k | if (isInt<16>(Imm)) { |
1052 | 105k | SDValue SDImm = CurDAG->getTargetConstant(Imm, dl, MVT::i64); |
1053 | 105k | return CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, SDImm); |
1054 | 105k | } |
1055 | | // 1-2) Patterns : {zeros}{15-bit valve}{16 zeros} |
1056 | | // {ones}{15-bit valve}{16 zeros} |
1057 | 55.2k | if (TZ > 15 && (LZ > 32 || LO > 32)) |
1058 | 6.37k | return CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, |
1059 | 6.37k | getI32Imm((Imm >> 16) & 0xffff)); |
1060 | | |
1061 | | // Following patterns use 2 instructions to materialize the Imm. |
1062 | 48.9k | InstCnt = 2; |
1063 | 48.9k | assert(LZ < 64 && "Unexpected leading zeros here."); |
1064 | | // Count of ones follwing the leading zeros. |
1065 | 0 | unsigned FO = llvm::countl_one<uint64_t>(Imm << LZ); |
1066 | | // 2-1) Patterns : {zeros}{31-bit value} |
1067 | | // {ones}{31-bit value} |
1068 | 48.9k | if (isInt<32>(Imm)) { |
1069 | 5.58k | uint64_t ImmHi16 = (Imm >> 16) & 0xffff; |
1070 | 5.58k | unsigned Opcode = ImmHi16 ? PPC::LIS8 : PPC::LI8; |
1071 | 5.58k | Result = CurDAG->getMachineNode(Opcode, dl, MVT::i64, getI32Imm(ImmHi16)); |
1072 | 5.58k | return CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64, SDValue(Result, 0), |
1073 | 5.58k | getI32Imm(Imm & 0xffff)); |
1074 | 5.58k | } |
1075 | | // 2-2) Patterns : {zeros}{ones}{15-bit value}{zeros} |
1076 | | // {zeros}{15-bit value}{zeros} |
1077 | | // {zeros}{ones}{15-bit value} |
1078 | | // {ones}{15-bit value}{zeros} |
1079 | | // We can take advantage of LI's sign-extension semantics to generate leading |
1080 | | // ones, and then use RLDIC to mask off the ones in both sides after rotation. |
1081 | 43.3k | if ((LZ + FO + TZ) > 48) { |
1082 | 37.3k | Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, |
1083 | 37.3k | getI32Imm((Imm >> TZ) & 0xffff)); |
1084 | 37.3k | return CurDAG->getMachineNode(PPC::RLDIC, dl, MVT::i64, SDValue(Result, 0), |
1085 | 37.3k | getI32Imm(TZ), getI32Imm(LZ)); |
1086 | 37.3k | } |
1087 | | // 2-3) Pattern : {zeros}{15-bit value}{ones} |
1088 | | // Shift right the Imm by (48 - LZ) bits to construct a negtive 16 bits value, |
1089 | | // therefore we can take advantage of LI's sign-extension semantics, and then |
1090 | | // mask them off after rotation. |
1091 | | // |
1092 | | // +--LZ--||-15-bit-||--TO--+ +-------------|--16-bit--+ |
1093 | | // |00000001bbbbbbbbb1111111| -> |00000000000001bbbbbbbbb1| |
1094 | | // +------------------------+ +------------------------+ |
1095 | | // 63 0 63 0 |
1096 | | // Imm (Imm >> (48 - LZ) & 0xffff) |
1097 | | // +----sext-----|--16-bit--+ +clear-|-----------------+ |
1098 | | // |11111111111111bbbbbbbbb1| -> |00000001bbbbbbbbb1111111| |
1099 | | // +------------------------+ +------------------------+ |
1100 | | // 63 0 63 0 |
1101 | | // LI8: sext many leading zeros RLDICL: rotate left (48 - LZ), clear left LZ |
1102 | 5.96k | if ((LZ + TO) > 48) { |
1103 | | // Since the immediates with (LZ > 32) have been handled by previous |
1104 | | // patterns, here we have (LZ <= 32) to make sure we will not shift right |
1105 | | // the Imm by a negative value. |
1106 | 250 | assert(LZ <= 32 && "Unexpected shift value."); |
1107 | 0 | Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, |
1108 | 250 | getI32Imm((Imm >> (48 - LZ) & 0xffff))); |
1109 | 250 | return CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, SDValue(Result, 0), |
1110 | 250 | getI32Imm(48 - LZ), getI32Imm(LZ)); |
1111 | 250 | } |
1112 | | // 2-4) Patterns : {zeros}{ones}{15-bit value}{ones} |
1113 | | // {ones}{15-bit value}{ones} |
1114 | | // We can take advantage of LI's sign-extension semantics to generate leading |
1115 | | // ones, and then use RLDICL to mask off the ones in left sides (if required) |
1116 | | // after rotation. |
1117 | | // |
1118 | | // +-LZ-FO||-15-bit-||--TO--+ +-------------|--16-bit--+ |
1119 | | // |00011110bbbbbbbbb1111111| -> |000000000011110bbbbbbbbb| |
1120 | | // +------------------------+ +------------------------+ |
1121 | | // 63 0 63 0 |
1122 | | // Imm (Imm >> TO) & 0xffff |
1123 | | // +----sext-----|--16-bit--+ +LZ|---------------------+ |
1124 | | // |111111111111110bbbbbbbbb| -> |00011110bbbbbbbbb1111111| |
1125 | | // +------------------------+ +------------------------+ |
1126 | | // 63 0 63 0 |
1127 | | // LI8: sext many leading zeros RLDICL: rotate left TO, clear left LZ |
1128 | 5.71k | if ((LZ + FO + TO) > 48) { |
1129 | 289 | Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, |
1130 | 289 | getI32Imm((Imm >> TO) & 0xffff)); |
1131 | 289 | return CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, SDValue(Result, 0), |
1132 | 289 | getI32Imm(TO), getI32Imm(LZ)); |
1133 | 289 | } |
1134 | | // 2-5) Pattern : {32 zeros}{****}{0}{15-bit value} |
1135 | | // If Hi32 is zero and the Lo16(in Lo32) can be presented as a positive 16 bit |
1136 | | // value, we can use LI for Lo16 without generating leading ones then add the |
1137 | | // Hi16(in Lo32). |
1138 | 5.42k | if (LZ == 32 && ((Lo32 & 0x8000) == 0)) { |
1139 | 63 | Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, |
1140 | 63 | getI32Imm(Lo32 & 0xffff)); |
1141 | 63 | return CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64, SDValue(Result, 0), |
1142 | 63 | getI32Imm(Lo32 >> 16)); |
1143 | 63 | } |
1144 | | // 2-6) Patterns : {******}{49 zeros}{******} |
1145 | | // {******}{49 ones}{******} |
1146 | | // If the Imm contains 49 consecutive zeros/ones, it means that a total of 15 |
1147 | | // bits remain on both sides. Rotate right the Imm to construct an int<16> |
1148 | | // value, use LI for int<16> value and then use RLDICL without mask to rotate |
1149 | | // it back. |
1150 | | // |
1151 | | // 1) findContiguousZerosAtLeast(Imm, 49) |
1152 | | // +------|--zeros-|------+ +---ones--||---15 bit--+ |
1153 | | // |bbbbbb0000000000aaaaaa| -> |0000000000aaaaaabbbbbb| |
1154 | | // +----------------------+ +----------------------+ |
1155 | | // 63 0 63 0 |
1156 | | // |
1157 | | // 2) findContiguousZerosAtLeast(~Imm, 49) |
1158 | | // +------|--ones--|------+ +---ones--||---15 bit--+ |
1159 | | // |bbbbbb1111111111aaaaaa| -> |1111111111aaaaaabbbbbb| |
1160 | | // +----------------------+ +----------------------+ |
1161 | | // 63 0 63 0 |
1162 | 5.36k | if ((Shift = findContiguousZerosAtLeast(Imm, 49)) || |
1163 | 5.36k | (Shift = findContiguousZerosAtLeast(~Imm, 49))) { |
1164 | 217 | uint64_t RotImm = APInt(64, Imm).rotr(Shift).getZExtValue(); |
1165 | 217 | Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, |
1166 | 217 | getI32Imm(RotImm & 0xffff)); |
1167 | 217 | return CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, SDValue(Result, 0), |
1168 | 217 | getI32Imm(Shift), getI32Imm(0)); |
1169 | 217 | } |
1170 | | // 2-7) Patterns : High word == Low word |
1171 | | // This may require 2 to 3 instructions, depending on whether Lo32 can be |
1172 | | // materialized in 1 instruction. |
1173 | 5.14k | if (Hi32 == Lo32) { |
1174 | | // Handle the first 32 bits. |
1175 | 490 | uint64_t ImmHi16 = (Lo32 >> 16) & 0xffff; |
1176 | 490 | uint64_t ImmLo16 = Lo32 & 0xffff; |
1177 | 490 | if (isInt<16>(Lo32)) |
1178 | 217 | Result = |
1179 | 217 | CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(ImmLo16)); |
1180 | 273 | else if (!ImmLo16) |
1181 | 191 | Result = |
1182 | 191 | CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(ImmHi16)); |
1183 | 82 | else { |
1184 | 82 | InstCnt = 3; |
1185 | 82 | Result = |
1186 | 82 | CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(ImmHi16)); |
1187 | 82 | Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64, |
1188 | 82 | SDValue(Result, 0), getI32Imm(ImmLo16)); |
1189 | 82 | } |
1190 | | // Use rldimi to insert the Low word into High word. |
1191 | 490 | SDValue Ops[] = {SDValue(Result, 0), SDValue(Result, 0), getI32Imm(32), |
1192 | 490 | getI32Imm(0)}; |
1193 | 490 | return CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops); |
1194 | 490 | } |
1195 | | |
1196 | | // Following patterns use 3 instructions to materialize the Imm. |
1197 | 4.65k | InstCnt = 3; |
1198 | | // 3-1) Patterns : {zeros}{ones}{31-bit value}{zeros} |
1199 | | // {zeros}{31-bit value}{zeros} |
1200 | | // {zeros}{ones}{31-bit value} |
1201 | | // {ones}{31-bit value}{zeros} |
1202 | | // We can take advantage of LIS's sign-extension semantics to generate leading |
1203 | | // ones, add the remaining bits with ORI, and then use RLDIC to mask off the |
1204 | | // ones in both sides after rotation. |
1205 | 4.65k | if ((LZ + FO + TZ) > 32) { |
1206 | 1.62k | uint64_t ImmHi16 = (Imm >> (TZ + 16)) & 0xffff; |
1207 | 1.62k | unsigned Opcode = ImmHi16 ? PPC::LIS8 : PPC::LI8; |
1208 | 1.62k | Result = CurDAG->getMachineNode(Opcode, dl, MVT::i64, getI32Imm(ImmHi16)); |
1209 | 1.62k | Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64, SDValue(Result, 0), |
1210 | 1.62k | getI32Imm((Imm >> TZ) & 0xffff)); |
1211 | 1.62k | return CurDAG->getMachineNode(PPC::RLDIC, dl, MVT::i64, SDValue(Result, 0), |
1212 | 1.62k | getI32Imm(TZ), getI32Imm(LZ)); |
1213 | 1.62k | } |
1214 | | // 3-2) Pattern : {zeros}{31-bit value}{ones} |
1215 | | // Shift right the Imm by (32 - LZ) bits to construct a negative 32 bits |
1216 | | // value, therefore we can take advantage of LIS's sign-extension semantics, |
1217 | | // add the remaining bits with ORI, and then mask them off after rotation. |
1218 | | // This is similar to Pattern 2-3, please refer to the diagram there. |
1219 | 3.02k | if ((LZ + TO) > 32) { |
1220 | | // Since the immediates with (LZ > 32) have been handled by previous |
1221 | | // patterns, here we have (LZ <= 32) to make sure we will not shift right |
1222 | | // the Imm by a negative value. |
1223 | 65 | assert(LZ <= 32 && "Unexpected shift value."); |
1224 | 0 | Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, |
1225 | 65 | getI32Imm((Imm >> (48 - LZ)) & 0xffff)); |
1226 | 65 | Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64, SDValue(Result, 0), |
1227 | 65 | getI32Imm((Imm >> (32 - LZ)) & 0xffff)); |
1228 | 65 | return CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, SDValue(Result, 0), |
1229 | 65 | getI32Imm(32 - LZ), getI32Imm(LZ)); |
1230 | 65 | } |
1231 | | // 3-3) Patterns : {zeros}{ones}{31-bit value}{ones} |
1232 | | // {ones}{31-bit value}{ones} |
1233 | | // We can take advantage of LIS's sign-extension semantics to generate leading |
1234 | | // ones, add the remaining bits with ORI, and then use RLDICL to mask off the |
1235 | | // ones in left sides (if required) after rotation. |
1236 | | // This is similar to Pattern 2-4, please refer to the diagram there. |
1237 | 2.96k | if ((LZ + FO + TO) > 32) { |
1238 | 268 | Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, |
1239 | 268 | getI32Imm((Imm >> (TO + 16)) & 0xffff)); |
1240 | 268 | Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64, SDValue(Result, 0), |
1241 | 268 | getI32Imm((Imm >> TO) & 0xffff)); |
1242 | 268 | return CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, SDValue(Result, 0), |
1243 | 268 | getI32Imm(TO), getI32Imm(LZ)); |
1244 | 268 | } |
1245 | | // 3-4) Patterns : {******}{33 zeros}{******} |
1246 | | // {******}{33 ones}{******} |
1247 | | // If the Imm contains 33 consecutive zeros/ones, it means that a total of 31 |
1248 | | // bits remain on both sides. Rotate right the Imm to construct an int<32> |
1249 | | // value, use LIS + ORI for int<32> value and then use RLDICL without mask to |
1250 | | // rotate it back. |
1251 | | // This is similar to Pattern 2-6, please refer to the diagram there. |
1252 | 2.69k | if ((Shift = findContiguousZerosAtLeast(Imm, 33)) || |
1253 | 2.69k | (Shift = findContiguousZerosAtLeast(~Imm, 33))) { |
1254 | 202 | uint64_t RotImm = APInt(64, Imm).rotr(Shift).getZExtValue(); |
1255 | 202 | uint64_t ImmHi16 = (RotImm >> 16) & 0xffff; |
1256 | 202 | unsigned Opcode = ImmHi16 ? PPC::LIS8 : PPC::LI8; |
1257 | 202 | Result = CurDAG->getMachineNode(Opcode, dl, MVT::i64, getI32Imm(ImmHi16)); |
1258 | 202 | Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64, SDValue(Result, 0), |
1259 | 202 | getI32Imm(RotImm & 0xffff)); |
1260 | 202 | return CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, SDValue(Result, 0), |
1261 | 202 | getI32Imm(Shift), getI32Imm(0)); |
1262 | 202 | } |
1263 | | |
1264 | 2.49k | InstCnt = 0; |
1265 | 2.49k | return nullptr; |
1266 | 2.69k | } |
1267 | | |
1268 | | // Try to select instructions to generate a 64 bit immediate using prefix as |
1269 | | // well as non prefix instructions. The function will return the SDNode |
1270 | | // to materialize that constant or it will return nullptr if it does not |
1271 | | // find one. The variable InstCnt is set to the number of instructions that |
1272 | | // were selected. |
1273 | | static SDNode *selectI64ImmDirectPrefix(SelectionDAG *CurDAG, const SDLoc &dl, |
1274 | 0 | uint64_t Imm, unsigned &InstCnt) { |
1275 | 0 | unsigned TZ = llvm::countr_zero<uint64_t>(Imm); |
1276 | 0 | unsigned LZ = llvm::countl_zero<uint64_t>(Imm); |
1277 | 0 | unsigned TO = llvm::countr_one<uint64_t>(Imm); |
1278 | 0 | unsigned FO = llvm::countl_one<uint64_t>(LZ == 64 ? 0 : (Imm << LZ)); |
1279 | 0 | unsigned Hi32 = Hi_32(Imm); |
1280 | 0 | unsigned Lo32 = Lo_32(Imm); |
1281 | |
|
1282 | 0 | auto getI32Imm = [CurDAG, dl](unsigned Imm) { |
1283 | 0 | return CurDAG->getTargetConstant(Imm, dl, MVT::i32); |
1284 | 0 | }; |
1285 | |
|
1286 | 0 | auto getI64Imm = [CurDAG, dl](uint64_t Imm) { |
1287 | 0 | return CurDAG->getTargetConstant(Imm, dl, MVT::i64); |
1288 | 0 | }; |
1289 | | |
1290 | | // Following patterns use 1 instruction to materialize Imm. |
1291 | 0 | InstCnt = 1; |
1292 | | |
1293 | | // The pli instruction can materialize up to 34 bits directly. |
1294 | | // If a constant fits within 34-bits, emit the pli instruction here directly. |
1295 | 0 | if (isInt<34>(Imm)) |
1296 | 0 | return CurDAG->getMachineNode(PPC::PLI8, dl, MVT::i64, |
1297 | 0 | CurDAG->getTargetConstant(Imm, dl, MVT::i64)); |
1298 | | |
1299 | | // Require at least two instructions. |
1300 | 0 | InstCnt = 2; |
1301 | 0 | SDNode *Result = nullptr; |
1302 | | // Patterns : {zeros}{ones}{33-bit value}{zeros} |
1303 | | // {zeros}{33-bit value}{zeros} |
1304 | | // {zeros}{ones}{33-bit value} |
1305 | | // {ones}{33-bit value}{zeros} |
1306 | | // We can take advantage of PLI's sign-extension semantics to generate leading |
1307 | | // ones, and then use RLDIC to mask off the ones on both sides after rotation. |
1308 | 0 | if ((LZ + FO + TZ) > 30) { |
1309 | 0 | APInt SignedInt34 = APInt(34, (Imm >> TZ) & 0x3ffffffff); |
1310 | 0 | APInt Extended = SignedInt34.sext(64); |
1311 | 0 | Result = CurDAG->getMachineNode(PPC::PLI8, dl, MVT::i64, |
1312 | 0 | getI64Imm(*Extended.getRawData())); |
1313 | 0 | return CurDAG->getMachineNode(PPC::RLDIC, dl, MVT::i64, SDValue(Result, 0), |
1314 | 0 | getI32Imm(TZ), getI32Imm(LZ)); |
1315 | 0 | } |
1316 | | // Pattern : {zeros}{33-bit value}{ones} |
1317 | | // Shift right the Imm by (30 - LZ) bits to construct a negative 34 bit value, |
1318 | | // therefore we can take advantage of PLI's sign-extension semantics, and then |
1319 | | // mask them off after rotation. |
1320 | | // |
1321 | | // +--LZ--||-33-bit-||--TO--+ +-------------|--34-bit--+ |
1322 | | // |00000001bbbbbbbbb1111111| -> |00000000000001bbbbbbbbb1| |
1323 | | // +------------------------+ +------------------------+ |
1324 | | // 63 0 63 0 |
1325 | | // |
1326 | | // +----sext-----|--34-bit--+ +clear-|-----------------+ |
1327 | | // |11111111111111bbbbbbbbb1| -> |00000001bbbbbbbbb1111111| |
1328 | | // +------------------------+ +------------------------+ |
1329 | | // 63 0 63 0 |
1330 | 0 | if ((LZ + TO) > 30) { |
1331 | 0 | APInt SignedInt34 = APInt(34, (Imm >> (30 - LZ)) & 0x3ffffffff); |
1332 | 0 | APInt Extended = SignedInt34.sext(64); |
1333 | 0 | Result = CurDAG->getMachineNode(PPC::PLI8, dl, MVT::i64, |
1334 | 0 | getI64Imm(*Extended.getRawData())); |
1335 | 0 | return CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, SDValue(Result, 0), |
1336 | 0 | getI32Imm(30 - LZ), getI32Imm(LZ)); |
1337 | 0 | } |
1338 | | // Patterns : {zeros}{ones}{33-bit value}{ones} |
1339 | | // {ones}{33-bit value}{ones} |
1340 | | // Similar to LI we can take advantage of PLI's sign-extension semantics to |
1341 | | // generate leading ones, and then use RLDICL to mask off the ones in left |
1342 | | // sides (if required) after rotation. |
1343 | 0 | if ((LZ + FO + TO) > 30) { |
1344 | 0 | APInt SignedInt34 = APInt(34, (Imm >> TO) & 0x3ffffffff); |
1345 | 0 | APInt Extended = SignedInt34.sext(64); |
1346 | 0 | Result = CurDAG->getMachineNode(PPC::PLI8, dl, MVT::i64, |
1347 | 0 | getI64Imm(*Extended.getRawData())); |
1348 | 0 | return CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, SDValue(Result, 0), |
1349 | 0 | getI32Imm(TO), getI32Imm(LZ)); |
1350 | 0 | } |
1351 | | // Patterns : {******}{31 zeros}{******} |
1352 | | // : {******}{31 ones}{******} |
1353 | | // If Imm contains 31 consecutive zeros/ones then the remaining bit count |
1354 | | // is 33. Rotate right the Imm to construct a int<33> value, we can use PLI |
1355 | | // for the int<33> value and then use RLDICL without a mask to rotate it back. |
1356 | | // |
1357 | | // +------|--ones--|------+ +---ones--||---33 bit--+ |
1358 | | // |bbbbbb1111111111aaaaaa| -> |1111111111aaaaaabbbbbb| |
1359 | | // +----------------------+ +----------------------+ |
1360 | | // 63 0 63 0 |
1361 | 0 | for (unsigned Shift = 0; Shift < 63; ++Shift) { |
1362 | 0 | uint64_t RotImm = APInt(64, Imm).rotr(Shift).getZExtValue(); |
1363 | 0 | if (isInt<34>(RotImm)) { |
1364 | 0 | Result = |
1365 | 0 | CurDAG->getMachineNode(PPC::PLI8, dl, MVT::i64, getI64Imm(RotImm)); |
1366 | 0 | return CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, |
1367 | 0 | SDValue(Result, 0), getI32Imm(Shift), |
1368 | 0 | getI32Imm(0)); |
1369 | 0 | } |
1370 | 0 | } |
1371 | | |
1372 | | // Patterns : High word == Low word |
1373 | | // This is basically a splat of a 32 bit immediate. |
1374 | 0 | if (Hi32 == Lo32) { |
1375 | 0 | Result = CurDAG->getMachineNode(PPC::PLI8, dl, MVT::i64, getI64Imm(Hi32)); |
1376 | 0 | SDValue Ops[] = {SDValue(Result, 0), SDValue(Result, 0), getI32Imm(32), |
1377 | 0 | getI32Imm(0)}; |
1378 | 0 | return CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops); |
1379 | 0 | } |
1380 | | |
1381 | 0 | InstCnt = 3; |
1382 | | // Catch-all |
1383 | | // This pattern can form any 64 bit immediate in 3 instructions. |
1384 | 0 | SDNode *ResultHi = |
1385 | 0 | CurDAG->getMachineNode(PPC::PLI8, dl, MVT::i64, getI64Imm(Hi32)); |
1386 | 0 | SDNode *ResultLo = |
1387 | 0 | CurDAG->getMachineNode(PPC::PLI8, dl, MVT::i64, getI64Imm(Lo32)); |
1388 | 0 | SDValue Ops[] = {SDValue(ResultLo, 0), SDValue(ResultHi, 0), getI32Imm(32), |
1389 | 0 | getI32Imm(0)}; |
1390 | 0 | return CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops); |
1391 | 0 | } |
1392 | | |
1393 | | static SDNode *selectI64Imm(SelectionDAG *CurDAG, const SDLoc &dl, uint64_t Imm, |
1394 | 158k | unsigned *InstCnt = nullptr) { |
1395 | 158k | unsigned InstCntDirect = 0; |
1396 | | // No more than 3 instructions are used if we can select the i64 immediate |
1397 | | // directly. |
1398 | 158k | SDNode *Result = selectI64ImmDirect(CurDAG, dl, Imm, InstCntDirect); |
1399 | | |
1400 | 158k | const PPCSubtarget &Subtarget = |
1401 | 158k | CurDAG->getMachineFunction().getSubtarget<PPCSubtarget>(); |
1402 | | |
1403 | | // If we have prefixed instructions and there is a chance we can |
1404 | | // materialize the constant with fewer prefixed instructions than |
1405 | | // non-prefixed, try that. |
1406 | 158k | if (Subtarget.hasPrefixInstrs() && InstCntDirect != 1) { |
1407 | 0 | unsigned InstCntDirectP = 0; |
1408 | 0 | SDNode *ResultP = selectI64ImmDirectPrefix(CurDAG, dl, Imm, InstCntDirectP); |
1409 | | // Use the prefix case in either of two cases: |
1410 | | // 1) We have no result from the non-prefix case to use. |
1411 | | // 2) The non-prefix case uses more instructions than the prefix case. |
1412 | | // If the prefix and non-prefix cases use the same number of instructions |
1413 | | // we will prefer the non-prefix case. |
1414 | 0 | if (ResultP && (!Result || InstCntDirectP < InstCntDirect)) { |
1415 | 0 | if (InstCnt) |
1416 | 0 | *InstCnt = InstCntDirectP; |
1417 | 0 | return ResultP; |
1418 | 0 | } |
1419 | 0 | } |
1420 | | |
1421 | 158k | if (Result) { |
1422 | 155k | if (InstCnt) |
1423 | 95.3k | *InstCnt = InstCntDirect; |
1424 | 155k | return Result; |
1425 | 155k | } |
1426 | 4.00k | auto getI32Imm = [CurDAG, dl](unsigned Imm) { |
1427 | 4.00k | return CurDAG->getTargetConstant(Imm, dl, MVT::i32); |
1428 | 4.00k | }; |
1429 | | |
1430 | 2.49k | uint32_t Hi16OfLo32 = (Lo_32(Imm) >> 16) & 0xffff; |
1431 | 2.49k | uint32_t Lo16OfLo32 = Lo_32(Imm) & 0xffff; |
1432 | | |
1433 | | // Try to use 4 instructions to materialize the immediate which is "almost" a |
1434 | | // splat of a 32 bit immediate. |
1435 | 2.49k | if (Hi16OfLo32 && Lo16OfLo32) { |
1436 | 1.46k | uint32_t Hi16OfHi32 = (Hi_32(Imm) >> 16) & 0xffff; |
1437 | 1.46k | uint32_t Lo16OfHi32 = Hi_32(Imm) & 0xffff; |
1438 | 1.46k | bool IsSelected = false; |
1439 | | |
1440 | 1.46k | auto getSplat = [CurDAG, dl, getI32Imm](uint32_t Hi16, uint32_t Lo16) { |
1441 | 9 | SDNode *Result = |
1442 | 9 | CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi16)); |
1443 | 9 | Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64, |
1444 | 9 | SDValue(Result, 0), getI32Imm(Lo16)); |
1445 | 9 | SDValue Ops[] = {SDValue(Result, 0), SDValue(Result, 0), getI32Imm(32), |
1446 | 9 | getI32Imm(0)}; |
1447 | 9 | return CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops); |
1448 | 9 | }; |
1449 | | |
1450 | 1.46k | if (Hi16OfHi32 == Lo16OfHi32 && Lo16OfHi32 == Lo16OfLo32) { |
1451 | 0 | IsSelected = true; |
1452 | 0 | Result = getSplat(Hi16OfLo32, Lo16OfLo32); |
1453 | | // Modify Hi16OfHi32. |
1454 | 0 | SDValue Ops[] = {SDValue(Result, 0), SDValue(Result, 0), getI32Imm(48), |
1455 | 0 | getI32Imm(0)}; |
1456 | 0 | Result = CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops); |
1457 | 1.46k | } else if (Hi16OfHi32 == Hi16OfLo32 && Hi16OfLo32 == Lo16OfLo32) { |
1458 | 0 | IsSelected = true; |
1459 | 0 | Result = getSplat(Hi16OfHi32, Lo16OfHi32); |
1460 | | // Modify Lo16OfLo32. |
1461 | 0 | SDValue Ops[] = {SDValue(Result, 0), SDValue(Result, 0), getI32Imm(16), |
1462 | 0 | getI32Imm(16), getI32Imm(31)}; |
1463 | 0 | Result = CurDAG->getMachineNode(PPC::RLWIMI8, dl, MVT::i64, Ops); |
1464 | 1.46k | } else if (Lo16OfHi32 == Lo16OfLo32 && Hi16OfLo32 == Lo16OfLo32) { |
1465 | 9 | IsSelected = true; |
1466 | 9 | Result = getSplat(Hi16OfHi32, Lo16OfHi32); |
1467 | | // Modify Hi16OfLo32. |
1468 | 9 | SDValue Ops[] = {SDValue(Result, 0), SDValue(Result, 0), getI32Imm(16), |
1469 | 9 | getI32Imm(0), getI32Imm(15)}; |
1470 | 9 | Result = CurDAG->getMachineNode(PPC::RLWIMI8, dl, MVT::i64, Ops); |
1471 | 9 | } |
1472 | 1.46k | if (IsSelected == true) { |
1473 | 9 | if (InstCnt) |
1474 | 0 | *InstCnt = 4; |
1475 | 9 | return Result; |
1476 | 9 | } |
1477 | 1.46k | } |
1478 | | |
1479 | | // Handle the upper 32 bit value. |
1480 | 2.48k | Result = |
1481 | 2.48k | selectI64ImmDirect(CurDAG, dl, Imm & 0xffffffff00000000, InstCntDirect); |
1482 | | // Add in the last bits as required. |
1483 | 2.48k | if (Hi16OfLo32) { |
1484 | 1.55k | Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64, |
1485 | 1.55k | SDValue(Result, 0), getI32Imm(Hi16OfLo32)); |
1486 | 1.55k | ++InstCntDirect; |
1487 | 1.55k | } |
1488 | 2.48k | if (Lo16OfLo32) { |
1489 | 2.38k | Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64, SDValue(Result, 0), |
1490 | 2.38k | getI32Imm(Lo16OfLo32)); |
1491 | 2.38k | ++InstCntDirect; |
1492 | 2.38k | } |
1493 | 2.48k | if (InstCnt) |
1494 | 172 | *InstCnt = InstCntDirect; |
1495 | 2.48k | return Result; |
1496 | 2.49k | } |
1497 | | |
1498 | | // Select a 64-bit constant. |
1499 | 79.7k | static SDNode *selectI64Imm(SelectionDAG *CurDAG, SDNode *N) { |
1500 | 79.7k | SDLoc dl(N); |
1501 | | |
1502 | | // Get 64 bit value. |
1503 | 79.7k | int64_t Imm = N->getAsZExtVal(); |
1504 | 79.7k | if (unsigned MinSize = allUsesTruncate(CurDAG, N)) { |
1505 | 18.9k | uint64_t SextImm = SignExtend64(Imm, MinSize); |
1506 | 18.9k | SDValue SDImm = CurDAG->getTargetConstant(SextImm, dl, MVT::i64); |
1507 | 18.9k | if (isInt<16>(SextImm)) |
1508 | 17.0k | return CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, SDImm); |
1509 | 18.9k | } |
1510 | 62.6k | return selectI64Imm(CurDAG, dl, Imm); |
1511 | 79.7k | } |
1512 | | |
1513 | | namespace { |
1514 | | |
1515 | | class BitPermutationSelector { |
1516 | | struct ValueBit { |
1517 | | SDValue V; |
1518 | | |
1519 | | // The bit number in the value, using a convention where bit 0 is the |
1520 | | // lowest-order bit. |
1521 | | unsigned Idx; |
1522 | | |
1523 | | // ConstZero means a bit we need to mask off. |
1524 | | // Variable is a bit comes from an input variable. |
1525 | | // VariableKnownToBeZero is also a bit comes from an input variable, |
1526 | | // but it is known to be already zero. So we do not need to mask them. |
1527 | | enum Kind { |
1528 | | ConstZero, |
1529 | | Variable, |
1530 | | VariableKnownToBeZero |
1531 | | } K; |
1532 | | |
1533 | | ValueBit(SDValue V, unsigned I, Kind K = Variable) |
1534 | 21.6M | : V(V), Idx(I), K(K) {} |
1535 | 32.3M | ValueBit(Kind K = Variable) : Idx(UINT32_MAX), K(K) {} |
1536 | | |
1537 | 6.34M | bool isZero() const { |
1538 | 6.34M | return K == ConstZero || K == VariableKnownToBeZero; |
1539 | 6.34M | } |
1540 | | |
1541 | 18.7M | bool hasValue() const { |
1542 | 18.7M | return K == Variable || K == VariableKnownToBeZero; |
1543 | 18.7M | } |
1544 | | |
1545 | 4.50M | SDValue getValue() const { |
1546 | 4.50M | assert(hasValue() && "Cannot get the value of a constant bit"); |
1547 | 0 | return V; |
1548 | 4.50M | } |
1549 | | |
1550 | 3.16M | unsigned getValueBitIndex() const { |
1551 | 3.16M | assert(hasValue() && "Cannot get the value bit index of a constant bit"); |
1552 | 0 | return Idx; |
1553 | 3.16M | } |
1554 | | }; |
1555 | | |
1556 | | // A bit group has the same underlying value and the same rotate factor. |
1557 | | struct BitGroup { |
1558 | | SDValue V; |
1559 | | unsigned RLAmt; |
1560 | | unsigned StartIdx, EndIdx; |
1561 | | |
1562 | | // This rotation amount assumes that the lower 32 bits of the quantity are |
1563 | | // replicated in the high 32 bits by the rotation operator (which is done |
1564 | | // by rlwinm and friends in 64-bit mode). |
1565 | | bool Repl32; |
1566 | | // Did converting to Repl32 == true change the rotation factor? If it did, |
1567 | | // it decreased it by 32. |
1568 | | bool Repl32CR; |
1569 | | // Was this group coalesced after setting Repl32 to true? |
1570 | | bool Repl32Coalesced; |
1571 | | |
1572 | | BitGroup(SDValue V, unsigned R, unsigned S, unsigned E) |
1573 | | : V(V), RLAmt(R), StartIdx(S), EndIdx(E), Repl32(false), Repl32CR(false), |
1574 | 91.4k | Repl32Coalesced(false) { |
1575 | 91.4k | LLVM_DEBUG(dbgs() << "\tbit group for " << V.getNode() << " RLAmt = " << R |
1576 | 91.4k | << " [" << S << ", " << E << "]\n"); |
1577 | 91.4k | } |
1578 | | }; |
1579 | | |
1580 | | // Information on each (Value, RLAmt) pair (like the number of groups |
1581 | | // associated with each) used to choose the lowering method. |
1582 | | struct ValueRotInfo { |
1583 | | SDValue V; |
1584 | | unsigned RLAmt = std::numeric_limits<unsigned>::max(); |
1585 | | unsigned NumGroups = 0; |
1586 | | unsigned FirstGroupStartIdx = std::numeric_limits<unsigned>::max(); |
1587 | | bool Repl32 = false; |
1588 | | |
1589 | 91.0k | ValueRotInfo() = default; |
1590 | | |
1591 | | // For sorting (in reverse order) by NumGroups, and then by |
1592 | | // FirstGroupStartIdx. |
1593 | 27.1k | bool operator < (const ValueRotInfo &Other) const { |
1594 | | // We need to sort so that the non-Repl32 come first because, when we're |
1595 | | // doing masking, the Repl32 bit groups might be subsumed into the 64-bit |
1596 | | // masking operation. |
1597 | 27.1k | if (Repl32 < Other.Repl32) |
1598 | 1.44k | return true; |
1599 | 25.7k | else if (Repl32 > Other.Repl32) |
1600 | 797 | return false; |
1601 | 24.9k | else if (NumGroups > Other.NumGroups) |
1602 | 42 | return true; |
1603 | 24.8k | else if (NumGroups < Other.NumGroups) |
1604 | 21 | return false; |
1605 | 24.8k | else if (RLAmt == 0 && Other.RLAmt != 0) |
1606 | 3.76k | return true; |
1607 | 21.0k | else if (RLAmt != 0 && Other.RLAmt == 0) |
1608 | 1.86k | return false; |
1609 | 19.2k | else if (FirstGroupStartIdx < Other.FirstGroupStartIdx) |
1610 | 12.8k | return true; |
1611 | 6.38k | return false; |
1612 | 27.1k | } |
1613 | | }; |
1614 | | |
1615 | | using ValueBitsMemoizedValue = std::pair<bool, SmallVector<ValueBit, 64>>; |
1616 | | using ValueBitsMemoizer = |
1617 | | DenseMap<SDValue, std::unique_ptr<ValueBitsMemoizedValue>>; |
1618 | | ValueBitsMemoizer Memoizer; |
1619 | | |
1620 | | // Return a pair of bool and a SmallVector pointer to a memoization entry. |
1621 | | // The bool is true if something interesting was deduced, otherwise if we're |
1622 | | // providing only a generic representation of V (or something else likewise |
1623 | | // uninteresting for instruction selection) through the SmallVector. |
1624 | | std::pair<bool, SmallVector<ValueBit, 64> *> getValueBits(SDValue V, |
1625 | 525k | unsigned NumBits) { |
1626 | 525k | auto &ValueEntry = Memoizer[V]; |
1627 | 525k | if (ValueEntry) |
1628 | 2.91k | return std::make_pair(ValueEntry->first, &ValueEntry->second); |
1629 | 522k | ValueEntry.reset(new ValueBitsMemoizedValue()); |
1630 | 522k | bool &Interesting = ValueEntry->first; |
1631 | 522k | SmallVector<ValueBit, 64> &Bits = ValueEntry->second; |
1632 | 522k | Bits.resize(NumBits); |
1633 | | |
1634 | 522k | switch (V.getOpcode()) { |
1635 | 123k | default: break; |
1636 | 123k | case ISD::ROTL: |
1637 | 261 | if (isa<ConstantSDNode>(V.getOperand(1))) { |
1638 | 259 | unsigned RotAmt = V.getConstantOperandVal(1); |
1639 | | |
1640 | 259 | const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second; |
1641 | | |
1642 | 16.2k | for (unsigned i = 0; i < NumBits; ++i) |
1643 | 15.9k | Bits[i] = LHSBits[i < RotAmt ? i + (NumBits - RotAmt) : i - RotAmt]; |
1644 | | |
1645 | 259 | return std::make_pair(Interesting = true, &Bits); |
1646 | 259 | } |
1647 | 2 | break; |
1648 | 58.1k | case ISD::SHL: |
1649 | 86.7k | case PPCISD::SHL: |
1650 | 86.7k | if (isa<ConstantSDNode>(V.getOperand(1))) { |
1651 | 34.4k | unsigned ShiftAmt = V.getConstantOperandVal(1); |
1652 | | |
1653 | 34.4k | const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second; |
1654 | | |
1655 | 1.53M | for (unsigned i = ShiftAmt; i < NumBits; ++i) |
1656 | 1.50M | Bits[i] = LHSBits[i - ShiftAmt]; |
1657 | | |
1658 | 575k | for (unsigned i = 0; i < ShiftAmt; ++i) |
1659 | 541k | Bits[i] = ValueBit(ValueBit::ConstZero); |
1660 | | |
1661 | 34.4k | return std::make_pair(Interesting = true, &Bits); |
1662 | 34.4k | } |
1663 | 52.2k | break; |
1664 | 52.2k | case ISD::SRL: |
1665 | 73.7k | case PPCISD::SRL: |
1666 | 73.7k | if (isa<ConstantSDNode>(V.getOperand(1))) { |
1667 | 26.6k | unsigned ShiftAmt = V.getConstantOperandVal(1); |
1668 | | |
1669 | 26.6k | const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second; |
1670 | | |
1671 | 899k | for (unsigned i = 0; i < NumBits - ShiftAmt; ++i) |
1672 | 873k | Bits[i] = LHSBits[i + ShiftAmt]; |
1673 | | |
1674 | 654k | for (unsigned i = NumBits - ShiftAmt; i < NumBits; ++i) |
1675 | 627k | Bits[i] = ValueBit(ValueBit::ConstZero); |
1676 | | |
1677 | 26.6k | return std::make_pair(Interesting = true, &Bits); |
1678 | 26.6k | } |
1679 | 47.0k | break; |
1680 | 63.5k | case ISD::AND: |
1681 | 63.5k | if (isa<ConstantSDNode>(V.getOperand(1))) { |
1682 | 58.3k | uint64_t Mask = V.getConstantOperandVal(1); |
1683 | | |
1684 | 58.3k | const SmallVector<ValueBit, 64> *LHSBits; |
1685 | | // Mark this as interesting, only if the LHS was also interesting. This |
1686 | | // prevents the overall procedure from matching a single immediate 'and' |
1687 | | // (which is non-optimal because such an and might be folded with other |
1688 | | // things if we don't select it here). |
1689 | 58.3k | std::tie(Interesting, LHSBits) = getValueBits(V.getOperand(0), NumBits); |
1690 | | |
1691 | 2.87M | for (unsigned i = 0; i < NumBits; ++i) |
1692 | 2.81M | if (((Mask >> i) & 1) == 1) |
1693 | 865k | Bits[i] = (*LHSBits)[i]; |
1694 | 1.94M | else { |
1695 | | // AND instruction masks this bit. If the input is already zero, |
1696 | | // we have nothing to do here. Otherwise, make the bit ConstZero. |
1697 | 1.94M | if ((*LHSBits)[i].isZero()) |
1698 | 251k | Bits[i] = (*LHSBits)[i]; |
1699 | 1.69M | else |
1700 | 1.69M | Bits[i] = ValueBit(ValueBit::ConstZero); |
1701 | 1.94M | } |
1702 | | |
1703 | 58.3k | return std::make_pair(Interesting, &Bits); |
1704 | 58.3k | } |
1705 | 5.18k | break; |
1706 | 114k | case ISD::OR: { |
1707 | 114k | const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second; |
1708 | 114k | const auto &RHSBits = *getValueBits(V.getOperand(1), NumBits).second; |
1709 | | |
1710 | 114k | bool AllDisjoint = true; |
1711 | 114k | SDValue LastVal = SDValue(); |
1712 | 114k | unsigned LastIdx = 0; |
1713 | 1.03M | for (unsigned i = 0; i < NumBits; ++i) { |
1714 | 1.01M | if (LHSBits[i].isZero() && RHSBits[i].isZero()) { |
1715 | | // If both inputs are known to be zero and one is ConstZero and |
1716 | | // another is VariableKnownToBeZero, we can select whichever |
1717 | | // we like. To minimize the number of bit groups, we select |
1718 | | // VariableKnownToBeZero if this bit is the next bit of the same |
1719 | | // input variable from the previous bit. Otherwise, we select |
1720 | | // ConstZero. |
1721 | 63.8k | if (LHSBits[i].hasValue() && LHSBits[i].getValue() == LastVal && |
1722 | 63.8k | LHSBits[i].getValueBitIndex() == LastIdx + 1) |
1723 | 5.00k | Bits[i] = LHSBits[i]; |
1724 | 58.8k | else if (RHSBits[i].hasValue() && RHSBits[i].getValue() == LastVal && |
1725 | 58.8k | RHSBits[i].getValueBitIndex() == LastIdx + 1) |
1726 | 24.4k | Bits[i] = RHSBits[i]; |
1727 | 34.4k | else |
1728 | 34.4k | Bits[i] = ValueBit(ValueBit::ConstZero); |
1729 | 63.8k | } |
1730 | 955k | else if (LHSBits[i].isZero()) |
1731 | 434k | Bits[i] = RHSBits[i]; |
1732 | 520k | else if (RHSBits[i].isZero()) |
1733 | 422k | Bits[i] = LHSBits[i]; |
1734 | 98.5k | else { |
1735 | 98.5k | AllDisjoint = false; |
1736 | 98.5k | break; |
1737 | 98.5k | } |
1738 | | // We remember the value and bit index of this bit. |
1739 | 920k | if (Bits[i].hasValue()) { |
1740 | 886k | LastVal = Bits[i].getValue(); |
1741 | 886k | LastIdx = Bits[i].getValueBitIndex(); |
1742 | 886k | } |
1743 | 34.4k | else { |
1744 | 34.4k | if (LastVal) LastVal = SDValue(); |
1745 | 34.4k | LastIdx = 0; |
1746 | 34.4k | } |
1747 | 920k | } |
1748 | | |
1749 | 114k | if (!AllDisjoint) |
1750 | 98.5k | break; |
1751 | | |
1752 | 15.7k | return std::make_pair(Interesting = true, &Bits); |
1753 | 114k | } |
1754 | 8.58k | case ISD::ZERO_EXTEND: { |
1755 | | // We support only the case with zero extension from i32 to i64 so far. |
1756 | 8.58k | if (V.getValueType() != MVT::i64 || |
1757 | 8.58k | V.getOperand(0).getValueType() != MVT::i32) |
1758 | 8.12k | break; |
1759 | | |
1760 | 463 | const SmallVector<ValueBit, 64> *LHSBits; |
1761 | 463 | const unsigned NumOperandBits = 32; |
1762 | 463 | std::tie(Interesting, LHSBits) = getValueBits(V.getOperand(0), |
1763 | 463 | NumOperandBits); |
1764 | | |
1765 | 15.2k | for (unsigned i = 0; i < NumOperandBits; ++i) |
1766 | 14.8k | Bits[i] = (*LHSBits)[i]; |
1767 | | |
1768 | 15.2k | for (unsigned i = NumOperandBits; i < NumBits; ++i) |
1769 | 14.8k | Bits[i] = ValueBit(ValueBit::ConstZero); |
1770 | | |
1771 | 463 | return std::make_pair(Interesting, &Bits); |
1772 | 8.58k | } |
1773 | 9.11k | case ISD::TRUNCATE: { |
1774 | 9.11k | EVT FromType = V.getOperand(0).getValueType(); |
1775 | 9.11k | EVT ToType = V.getValueType(); |
1776 | | // We support only the case with truncate from i64 to i32. |
1777 | 9.11k | if (FromType != MVT::i64 || ToType != MVT::i32) |
1778 | 0 | break; |
1779 | 9.11k | const unsigned NumAllBits = FromType.getSizeInBits(); |
1780 | 9.11k | SmallVector<ValueBit, 64> *InBits; |
1781 | 9.11k | std::tie(Interesting, InBits) = getValueBits(V.getOperand(0), |
1782 | 9.11k | NumAllBits); |
1783 | 9.11k | const unsigned NumValidBits = ToType.getSizeInBits(); |
1784 | | |
1785 | | // A 32-bit instruction cannot touch upper 32-bit part of 64-bit value. |
1786 | | // So, we cannot include this truncate. |
1787 | 9.11k | bool UseUpper32bit = false; |
1788 | 297k | for (unsigned i = 0; i < NumValidBits; ++i) |
1789 | 288k | if ((*InBits)[i].hasValue() && (*InBits)[i].getValueBitIndex() >= 32) { |
1790 | 112 | UseUpper32bit = true; |
1791 | 112 | break; |
1792 | 112 | } |
1793 | 9.11k | if (UseUpper32bit) |
1794 | 112 | break; |
1795 | | |
1796 | 296k | for (unsigned i = 0; i < NumValidBits; ++i) |
1797 | 287k | Bits[i] = (*InBits)[i]; |
1798 | | |
1799 | 8.99k | return std::make_pair(Interesting, &Bits); |
1800 | 9.11k | } |
1801 | 5.15k | case ISD::AssertZext: { |
1802 | | // For AssertZext, we look through the operand and |
1803 | | // mark the bits known to be zero. |
1804 | 5.15k | const SmallVector<ValueBit, 64> *LHSBits; |
1805 | 5.15k | std::tie(Interesting, LHSBits) = getValueBits(V.getOperand(0), |
1806 | 5.15k | NumBits); |
1807 | | |
1808 | 5.15k | EVT FromType = cast<VTSDNode>(V.getOperand(1))->getVT(); |
1809 | 5.15k | const unsigned NumValidBits = FromType.getSizeInBits(); |
1810 | 222k | for (unsigned i = 0; i < NumValidBits; ++i) |
1811 | 217k | Bits[i] = (*LHSBits)[i]; |
1812 | | |
1813 | | // These bits are known to be zero but the AssertZext may be from a value |
1814 | | // that already has some constant zero bits (i.e. from a masking and). |
1815 | 97.9k | for (unsigned i = NumValidBits; i < NumBits; ++i) |
1816 | 92.8k | Bits[i] = (*LHSBits)[i].hasValue() |
1817 | 92.8k | ? ValueBit((*LHSBits)[i].getValue(), |
1818 | 92.8k | (*LHSBits)[i].getValueBitIndex(), |
1819 | 92.8k | ValueBit::VariableKnownToBeZero) |
1820 | 92.8k | : ValueBit(ValueBit::ConstZero); |
1821 | | |
1822 | 5.15k | return std::make_pair(Interesting, &Bits); |
1823 | 9.11k | } |
1824 | 38.0k | case ISD::LOAD: |
1825 | 38.0k | LoadSDNode *LD = cast<LoadSDNode>(V); |
1826 | 38.0k | if (ISD::isZEXTLoad(V.getNode()) && V.getResNo() == 0) { |
1827 | 9.61k | EVT VT = LD->getMemoryVT(); |
1828 | 9.61k | const unsigned NumValidBits = VT.getSizeInBits(); |
1829 | | |
1830 | 130k | for (unsigned i = 0; i < NumValidBits; ++i) |
1831 | 120k | Bits[i] = ValueBit(V, i); |
1832 | | |
1833 | | // These bits are known to be zero. |
1834 | 423k | for (unsigned i = NumValidBits; i < NumBits; ++i) |
1835 | 414k | Bits[i] = ValueBit(V, i, ValueBit::VariableKnownToBeZero); |
1836 | | |
1837 | | // Zero-extending load itself cannot be optimized. So, it is not |
1838 | | // interesting by itself though it gives useful information. |
1839 | 9.61k | return std::make_pair(Interesting = false, &Bits); |
1840 | 9.61k | } |
1841 | 28.4k | break; |
1842 | 522k | } |
1843 | | |
1844 | 21.3M | for (unsigned i = 0; i < NumBits; ++i) |
1845 | 21.0M | Bits[i] = ValueBit(V, i); |
1846 | | |
1847 | 363k | return std::make_pair(Interesting = false, &Bits); |
1848 | 522k | } |
1849 | | |
1850 | | // For each value (except the constant ones), compute the left-rotate amount |
1851 | | // to get it from its original to final position. |
1852 | 42.0k | void computeRotationAmounts() { |
1853 | 42.0k | NeedMask = false; |
1854 | 42.0k | RLAmt.resize(Bits.size()); |
1855 | 2.48M | for (unsigned i = 0; i < Bits.size(); ++i) |
1856 | 2.43M | if (Bits[i].hasValue()) { |
1857 | 1.83M | unsigned VBI = Bits[i].getValueBitIndex(); |
1858 | 1.83M | if (i >= VBI) |
1859 | 1.16M | RLAmt[i] = i - VBI; |
1860 | 669k | else |
1861 | 669k | RLAmt[i] = Bits.size() - (VBI - i); |
1862 | 1.83M | } else if (Bits[i].isZero()) { |
1863 | 602k | NeedMask = true; |
1864 | 602k | RLAmt[i] = UINT32_MAX; |
1865 | 602k | } else { |
1866 | 0 | llvm_unreachable("Unknown value bit type"); |
1867 | 0 | } |
1868 | 42.0k | } |
1869 | | |
1870 | | // Collect groups of consecutive bits with the same underlying value and |
1871 | | // rotation factor. If we're doing late masking, we ignore zeros, otherwise |
1872 | | // they break up groups. |
1873 | 77.1k | void collectBitGroups(bool LateMask) { |
1874 | 77.1k | BitGroups.clear(); |
1875 | | |
1876 | 77.1k | unsigned LastRLAmt = RLAmt[0]; |
1877 | 77.1k | SDValue LastValue = Bits[0].hasValue() ? Bits[0].getValue() : SDValue(); |
1878 | 77.1k | unsigned LastGroupStartIdx = 0; |
1879 | 77.1k | bool IsGroupOfZeros = !Bits[LastGroupStartIdx].hasValue(); |
1880 | 4.43M | for (unsigned i = 1; i < Bits.size(); ++i) { |
1881 | 4.36M | unsigned ThisRLAmt = RLAmt[i]; |
1882 | 4.36M | SDValue ThisValue = Bits[i].hasValue() ? Bits[i].getValue() : SDValue(); |
1883 | 4.36M | if (LateMask && !ThisValue) { |
1884 | 584k | ThisValue = LastValue; |
1885 | 584k | ThisRLAmt = LastRLAmt; |
1886 | | // If we're doing late masking, then the first bit group always starts |
1887 | | // at zero (even if the first bits were zero). |
1888 | 584k | if (BitGroups.empty()) |
1889 | 563k | LastGroupStartIdx = 0; |
1890 | 584k | } |
1891 | | |
1892 | | // If this bit is known to be zero and the current group is a bit group |
1893 | | // of zeros, we do not need to terminate the current bit group even the |
1894 | | // Value or RLAmt does not match here. Instead, we terminate this group |
1895 | | // when the first non-zero bit appears later. |
1896 | 4.36M | if (IsGroupOfZeros && Bits[i].isZero()) |
1897 | 761k | continue; |
1898 | | |
1899 | | // If this bit has the same underlying value and the same rotate factor as |
1900 | | // the last one, then they're part of the same group. |
1901 | 3.60M | if (ThisRLAmt == LastRLAmt && ThisValue == LastValue) |
1902 | | // We cannot continue the current group if this bits is not known to |
1903 | | // be zero in a bit group of zeros. |
1904 | 3.53M | if (!(IsGroupOfZeros && ThisValue && !Bits[i].isZero())) |
1905 | 3.53M | continue; |
1906 | | |
1907 | 69.5k | if (LastValue.getNode()) |
1908 | 34.5k | BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx, |
1909 | 34.5k | i-1)); |
1910 | 69.5k | LastRLAmt = ThisRLAmt; |
1911 | 69.5k | LastValue = ThisValue; |
1912 | 69.5k | LastGroupStartIdx = i; |
1913 | 69.5k | IsGroupOfZeros = !Bits[LastGroupStartIdx].hasValue(); |
1914 | 69.5k | } |
1915 | 77.1k | if (LastValue.getNode()) |
1916 | 56.8k | BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx, |
1917 | 56.8k | Bits.size()-1)); |
1918 | | |
1919 | 77.1k | if (BitGroups.empty()) |
1920 | 0 | return; |
1921 | | |
1922 | | // We might be able to combine the first and last groups. |
1923 | 77.1k | if (BitGroups.size() > 1) { |
1924 | | // If the first and last groups are the same, then remove the first group |
1925 | | // in favor of the last group, making the ending index of the last group |
1926 | | // equal to the ending index of the to-be-removed first group. |
1927 | 10.5k | if (BitGroups[0].StartIdx == 0 && |
1928 | 10.5k | BitGroups[BitGroups.size()-1].EndIdx == Bits.size()-1 && |
1929 | 10.5k | BitGroups[0].V == BitGroups[BitGroups.size()-1].V && |
1930 | 10.5k | BitGroups[0].RLAmt == BitGroups[BitGroups.size()-1].RLAmt) { |
1931 | 12 | LLVM_DEBUG(dbgs() << "\tcombining final bit group with initial one\n"); |
1932 | 12 | BitGroups[BitGroups.size()-1].EndIdx = BitGroups[0].EndIdx; |
1933 | 12 | BitGroups.erase(BitGroups.begin()); |
1934 | 12 | } |
1935 | 10.5k | } |
1936 | 77.1k | } |
1937 | | |
1938 | | // Take all (SDValue, RLAmt) pairs and sort them by the number of groups |
1939 | | // associated with each. If the number of groups are same, we prefer a group |
1940 | | // which does not require rotate, i.e. RLAmt is 0, to avoid the first rotate |
1941 | | // instruction. If there is a degeneracy, pick the one that occurs |
1942 | | // first (in the final value). |
1943 | 77.1k | void collectValueRotInfo() { |
1944 | 77.1k | ValueRots.clear(); |
1945 | | |
1946 | 91.4k | for (auto &BG : BitGroups) { |
1947 | 91.4k | unsigned RLAmtKey = BG.RLAmt + (BG.Repl32 ? 64 : 0); |
1948 | 91.4k | ValueRotInfo &VRI = ValueRots[std::make_pair(BG.V, RLAmtKey)]; |
1949 | 91.4k | VRI.V = BG.V; |
1950 | 91.4k | VRI.RLAmt = BG.RLAmt; |
1951 | 91.4k | VRI.Repl32 = BG.Repl32; |
1952 | 91.4k | VRI.NumGroups += 1; |
1953 | 91.4k | VRI.FirstGroupStartIdx = std::min(VRI.FirstGroupStartIdx, BG.StartIdx); |
1954 | 91.4k | } |
1955 | | |
1956 | | // Now that we've collected the various ValueRotInfo instances, we need to |
1957 | | // sort them. |
1958 | 77.1k | ValueRotsVec.clear(); |
1959 | 91.0k | for (auto &I : ValueRots) { |
1960 | 91.0k | ValueRotsVec.push_back(I.second); |
1961 | 91.0k | } |
1962 | 77.1k | llvm::sort(ValueRotsVec); |
1963 | 77.1k | } |
1964 | | |
1965 | | // In 64-bit mode, rlwinm and friends have a rotation operator that |
1966 | | // replicates the low-order 32 bits into the high-order 32-bits. The mask |
1967 | | // indices of these instructions can only be in the lower 32 bits, so they |
1968 | | // can only represent some 64-bit bit groups. However, when they can be used, |
1969 | | // the 32-bit replication can be used to represent, as a single bit group, |
1970 | | // otherwise separate bit groups. We'll convert to replicated-32-bit bit |
1971 | | // groups when possible. Returns true if any of the bit groups were |
1972 | | // converted. |
1973 | 61.6k | void assignRepl32BitGroups() { |
1974 | | // If we have bits like this: |
1975 | | // |
1976 | | // Indices: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
1977 | | // V bits: ... 7 6 5 4 3 2 1 0 31 30 29 28 27 26 25 24 |
1978 | | // Groups: | RLAmt = 8 | RLAmt = 40 | |
1979 | | // |
1980 | | // But, making use of a 32-bit operation that replicates the low-order 32 |
1981 | | // bits into the high-order 32 bits, this can be one bit group with a RLAmt |
1982 | | // of 8. |
1983 | | |
1984 | 61.6k | auto IsAllLow32 = [this](BitGroup & BG) { |
1985 | 10.6k | if (BG.StartIdx <= BG.EndIdx) { |
1986 | 31.2k | for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i) { |
1987 | 27.6k | if (!Bits[i].hasValue()) |
1988 | 48 | continue; |
1989 | 27.6k | if (Bits[i].getValueBitIndex() >= 32) |
1990 | 7.01k | return false; |
1991 | 27.6k | } |
1992 | 10.6k | } else { |
1993 | 164 | for (unsigned i = BG.StartIdx; i < Bits.size(); ++i) { |
1994 | 164 | if (!Bits[i].hasValue()) |
1995 | 0 | continue; |
1996 | 164 | if (Bits[i].getValueBitIndex() >= 32) |
1997 | 5 | return false; |
1998 | 164 | } |
1999 | 0 | for (unsigned i = 0; i <= BG.EndIdx; ++i) { |
2000 | 0 | if (!Bits[i].hasValue()) |
2001 | 0 | continue; |
2002 | 0 | if (Bits[i].getValueBitIndex() >= 32) |
2003 | 0 | return false; |
2004 | 0 | } |
2005 | 0 | } |
2006 | | |
2007 | 3.61k | return true; |
2008 | 10.6k | }; |
2009 | | |
2010 | 72.7k | for (auto &BG : BitGroups) { |
2011 | | // If this bit group has RLAmt of 0 and will not be merged with |
2012 | | // another bit group, we don't benefit from Repl32. We don't mark |
2013 | | // such group to give more freedom for later instruction selection. |
2014 | 72.7k | if (BG.RLAmt == 0) { |
2015 | 2.92k | auto PotentiallyMerged = [this](BitGroup & BG) { |
2016 | 2.92k | for (auto &BG2 : BitGroups) |
2017 | 6.01k | if (&BG != &BG2 && BG.V == BG2.V && |
2018 | 6.01k | (BG2.RLAmt == 0 || BG2.RLAmt == 32)) |
2019 | 41 | return true; |
2020 | 2.88k | return false; |
2021 | 2.92k | }; |
2022 | 2.92k | if (!PotentiallyMerged(BG)) |
2023 | 2.88k | continue; |
2024 | 2.92k | } |
2025 | 69.8k | if (BG.StartIdx < 32 && BG.EndIdx < 32) { |
2026 | 10.6k | if (IsAllLow32(BG)) { |
2027 | 3.60k | if (BG.RLAmt >= 32) { |
2028 | 278 | BG.RLAmt -= 32; |
2029 | 278 | BG.Repl32CR = true; |
2030 | 278 | } |
2031 | | |
2032 | 3.60k | BG.Repl32 = true; |
2033 | | |
2034 | 3.60k | LLVM_DEBUG(dbgs() << "\t32-bit replicated bit group for " |
2035 | 3.60k | << BG.V.getNode() << " RLAmt = " << BG.RLAmt << " [" |
2036 | 3.60k | << BG.StartIdx << ", " << BG.EndIdx << "]\n"); |
2037 | 3.60k | } |
2038 | 10.6k | } |
2039 | 69.8k | } |
2040 | | |
2041 | | // Now walk through the bit groups, consolidating where possible. |
2042 | 134k | for (auto I = BitGroups.begin(); I != BitGroups.end();) { |
2043 | | // We might want to remove this bit group by merging it with the previous |
2044 | | // group (which might be the ending group). |
2045 | 72.7k | auto IP = (I == BitGroups.begin()) ? |
2046 | 61.6k | std::prev(BitGroups.end()) : std::prev(I); |
2047 | 72.7k | if (I->Repl32 && IP->Repl32 && I->V == IP->V && I->RLAmt == IP->RLAmt && |
2048 | 72.7k | I->StartIdx == (IP->EndIdx + 1) % 64 && I != IP) { |
2049 | |
|
2050 | 0 | LLVM_DEBUG(dbgs() << "\tcombining 32-bit replicated bit group for " |
2051 | 0 | << I->V.getNode() << " RLAmt = " << I->RLAmt << " [" |
2052 | 0 | << I->StartIdx << ", " << I->EndIdx |
2053 | 0 | << "] with group with range [" << IP->StartIdx << ", " |
2054 | 0 | << IP->EndIdx << "]\n"); |
2055 | |
|
2056 | 0 | IP->EndIdx = I->EndIdx; |
2057 | 0 | IP->Repl32CR = IP->Repl32CR || I->Repl32CR; |
2058 | 0 | IP->Repl32Coalesced = true; |
2059 | 0 | I = BitGroups.erase(I); |
2060 | 0 | continue; |
2061 | 72.7k | } else { |
2062 | | // There is a special case worth handling: If there is a single group |
2063 | | // covering the entire upper 32 bits, and it can be merged with both |
2064 | | // the next and previous groups (which might be the same group), then |
2065 | | // do so. If it is the same group (so there will be only one group in |
2066 | | // total), then we need to reverse the order of the range so that it |
2067 | | // covers the entire 64 bits. |
2068 | 72.7k | if (I->StartIdx == 32 && I->EndIdx == 63) { |
2069 | 1.25k | assert(std::next(I) == BitGroups.end() && |
2070 | 1.25k | "bit group ends at index 63 but there is another?"); |
2071 | 0 | auto IN = BitGroups.begin(); |
2072 | | |
2073 | 1.25k | if (IP->Repl32 && IN->Repl32 && I->V == IP->V && I->V == IN->V && |
2074 | 1.25k | (I->RLAmt % 32) == IP->RLAmt && (I->RLAmt % 32) == IN->RLAmt && |
2075 | 1.25k | IP->EndIdx == 31 && IN->StartIdx == 0 && I != IP && |
2076 | 1.25k | IsAllLow32(*I)) { |
2077 | | |
2078 | 13 | LLVM_DEBUG(dbgs() << "\tcombining bit group for " << I->V.getNode() |
2079 | 13 | << " RLAmt = " << I->RLAmt << " [" << I->StartIdx |
2080 | 13 | << ", " << I->EndIdx |
2081 | 13 | << "] with 32-bit replicated groups with ranges [" |
2082 | 13 | << IP->StartIdx << ", " << IP->EndIdx << "] and [" |
2083 | 13 | << IN->StartIdx << ", " << IN->EndIdx << "]\n"); |
2084 | | |
2085 | 13 | if (IP == IN) { |
2086 | | // There is only one other group; change it to cover the whole |
2087 | | // range (backward, so that it can still be Repl32 but cover the |
2088 | | // whole 64-bit range). |
2089 | 13 | IP->StartIdx = 31; |
2090 | 13 | IP->EndIdx = 30; |
2091 | 13 | IP->Repl32CR = IP->Repl32CR || I->RLAmt >= 32; |
2092 | 13 | IP->Repl32Coalesced = true; |
2093 | 13 | I = BitGroups.erase(I); |
2094 | 13 | } else { |
2095 | | // There are two separate groups, one before this group and one |
2096 | | // after us (at the beginning). We're going to remove this group, |
2097 | | // but also the group at the very beginning. |
2098 | 0 | IP->EndIdx = IN->EndIdx; |
2099 | 0 | IP->Repl32CR = IP->Repl32CR || IN->Repl32CR || I->RLAmt >= 32; |
2100 | 0 | IP->Repl32Coalesced = true; |
2101 | 0 | I = BitGroups.erase(I); |
2102 | 0 | BitGroups.erase(BitGroups.begin()); |
2103 | 0 | } |
2104 | | |
2105 | | // This must be the last group in the vector (and we might have |
2106 | | // just invalidated the iterator above), so break here. |
2107 | 13 | break; |
2108 | 13 | } |
2109 | 1.25k | } |
2110 | 72.7k | } |
2111 | | |
2112 | 72.7k | ++I; |
2113 | 72.7k | } |
2114 | 61.6k | } |
2115 | | |
2116 | 212k | SDValue getI32Imm(unsigned Imm, const SDLoc &dl) { |
2117 | 212k | return CurDAG->getTargetConstant(Imm, dl, MVT::i32); |
2118 | 212k | } |
2119 | | |
2120 | 35.0k | uint64_t getZerosMask() { |
2121 | 35.0k | uint64_t Mask = 0; |
2122 | 2.03M | for (unsigned i = 0; i < Bits.size(); ++i) { |
2123 | 2.00M | if (Bits[i].hasValue()) |
2124 | 1.39M | continue; |
2125 | 602k | Mask |= (UINT64_C(1) << i); |
2126 | 602k | } |
2127 | | |
2128 | 35.0k | return ~Mask; |
2129 | 35.0k | } |
2130 | | |
2131 | | // This method extends an input value to 64 bit if input is 32-bit integer. |
2132 | | // While selecting instructions in BitPermutationSelector in 64-bit mode, |
2133 | | // an input value can be a 32-bit integer if a ZERO_EXTEND node is included. |
2134 | | // In such case, we extend it to 64 bit to be consistent with other values. |
2135 | 109k | SDValue ExtendToInt64(SDValue V, const SDLoc &dl) { |
2136 | 109k | if (V.getValueSizeInBits() == 64) |
2137 | 108k | return V; |
2138 | | |
2139 | 1.42k | assert(V.getValueSizeInBits() == 32); |
2140 | 0 | SDValue SubRegIdx = CurDAG->getTargetConstant(PPC::sub_32, dl, MVT::i32); |
2141 | 1.42k | SDValue ImDef = SDValue(CurDAG->getMachineNode(PPC::IMPLICIT_DEF, dl, |
2142 | 1.42k | MVT::i64), 0); |
2143 | 1.42k | SDValue ExtVal = SDValue(CurDAG->getMachineNode(PPC::INSERT_SUBREG, dl, |
2144 | 1.42k | MVT::i64, ImDef, V, |
2145 | 1.42k | SubRegIdx), 0); |
2146 | 1.42k | return ExtVal; |
2147 | 109k | } |
2148 | | |
2149 | 18.6k | SDValue TruncateToInt32(SDValue V, const SDLoc &dl) { |
2150 | 18.6k | if (V.getValueSizeInBits() == 32) |
2151 | 10.5k | return V; |
2152 | | |
2153 | 8.05k | assert(V.getValueSizeInBits() == 64); |
2154 | 0 | SDValue SubRegIdx = CurDAG->getTargetConstant(PPC::sub_32, dl, MVT::i32); |
2155 | 8.05k | SDValue SubVal = SDValue(CurDAG->getMachineNode(PPC::EXTRACT_SUBREG, dl, |
2156 | 8.05k | MVT::i32, V, SubRegIdx), 0); |
2157 | 8.05k | return SubVal; |
2158 | 18.6k | } |
2159 | | |
2160 | | // Depending on the number of groups for a particular value, it might be |
2161 | | // better to rotate, mask explicitly (using andi/andis), and then or the |
2162 | | // result. Select this part of the result first. |
2163 | 15.4k | void SelectAndParts32(const SDLoc &dl, SDValue &Res, unsigned *InstCnt) { |
2164 | 15.4k | if (BPermRewriterNoMasking) |
2165 | 0 | return; |
2166 | | |
2167 | 18.5k | for (ValueRotInfo &VRI : ValueRotsVec) { |
2168 | 18.5k | unsigned Mask = 0; |
2169 | 613k | for (unsigned i = 0; i < Bits.size(); ++i) { |
2170 | 595k | if (!Bits[i].hasValue() || Bits[i].getValue() != VRI.V) |
2171 | 406k | continue; |
2172 | 188k | if (RLAmt[i] != VRI.RLAmt) |
2173 | 1.87k | continue; |
2174 | 186k | Mask |= (1u << i); |
2175 | 186k | } |
2176 | | |
2177 | | // Compute the masks for andi/andis that would be necessary. |
2178 | 18.5k | unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16; |
2179 | 18.5k | assert((ANDIMask != 0 || ANDISMask != 0) && |
2180 | 18.5k | "No set bits in mask for value bit groups"); |
2181 | 0 | bool NeedsRotate = VRI.RLAmt != 0; |
2182 | | |
2183 | | // We're trying to minimize the number of instructions. If we have one |
2184 | | // group, using one of andi/andis can break even. If we have three |
2185 | | // groups, we can use both andi and andis and break even (to use both |
2186 | | // andi and andis we also need to or the results together). We need four |
2187 | | // groups if we also need to rotate. To use andi/andis we need to do more |
2188 | | // than break even because rotate-and-mask instructions tend to be easier |
2189 | | // to schedule. |
2190 | | |
2191 | | // FIXME: We've biased here against using andi/andis, which is right for |
2192 | | // POWER cores, but not optimal everywhere. For example, on the A2, |
2193 | | // andi/andis have single-cycle latency whereas the rotate-and-mask |
2194 | | // instructions take two cycles, and it would be better to bias toward |
2195 | | // andi/andis in break-even cases. |
2196 | | |
2197 | 18.5k | unsigned NumAndInsts = (unsigned) NeedsRotate + |
2198 | 18.5k | (unsigned) (ANDIMask != 0) + |
2199 | 18.5k | (unsigned) (ANDISMask != 0) + |
2200 | 18.5k | (unsigned) (ANDIMask != 0 && ANDISMask != 0) + |
2201 | 18.5k | (unsigned) (bool) Res; |
2202 | | |
2203 | 18.5k | LLVM_DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() |
2204 | 18.5k | << " RL: " << VRI.RLAmt << ":" |
2205 | 18.5k | << "\n\t\t\tisel using masking: " << NumAndInsts |
2206 | 18.5k | << " using rotates: " << VRI.NumGroups << "\n"); |
2207 | | |
2208 | 18.5k | if (NumAndInsts >= VRI.NumGroups) |
2209 | 18.5k | continue; |
2210 | | |
2211 | 13 | LLVM_DEBUG(dbgs() << "\t\t\t\tusing masking\n"); |
2212 | | |
2213 | 13 | if (InstCnt) *InstCnt += NumAndInsts; |
2214 | | |
2215 | 13 | SDValue VRot; |
2216 | 13 | if (VRI.RLAmt) { |
2217 | 11 | SDValue Ops[] = |
2218 | 11 | { TruncateToInt32(VRI.V, dl), getI32Imm(VRI.RLAmt, dl), |
2219 | 11 | getI32Imm(0, dl), getI32Imm(31, dl) }; |
2220 | 11 | VRot = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, |
2221 | 11 | Ops), 0); |
2222 | 11 | } else { |
2223 | 2 | VRot = TruncateToInt32(VRI.V, dl); |
2224 | 2 | } |
2225 | | |
2226 | 13 | SDValue ANDIVal, ANDISVal; |
2227 | 13 | if (ANDIMask != 0) |
2228 | 10 | ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDI_rec, dl, MVT::i32, |
2229 | 10 | VRot, getI32Imm(ANDIMask, dl)), |
2230 | 10 | 0); |
2231 | 13 | if (ANDISMask != 0) |
2232 | 8 | ANDISVal = |
2233 | 8 | SDValue(CurDAG->getMachineNode(PPC::ANDIS_rec, dl, MVT::i32, VRot, |
2234 | 8 | getI32Imm(ANDISMask, dl)), |
2235 | 8 | 0); |
2236 | | |
2237 | 13 | SDValue TotalVal; |
2238 | 13 | if (!ANDIVal) |
2239 | 3 | TotalVal = ANDISVal; |
2240 | 10 | else if (!ANDISVal) |
2241 | 5 | TotalVal = ANDIVal; |
2242 | 5 | else |
2243 | 5 | TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32, |
2244 | 5 | ANDIVal, ANDISVal), 0); |
2245 | | |
2246 | 13 | if (!Res) |
2247 | 12 | Res = TotalVal; |
2248 | 1 | else |
2249 | 1 | Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32, |
2250 | 1 | Res, TotalVal), 0); |
2251 | | |
2252 | | // Now, remove all groups with this underlying value and rotation |
2253 | | // factor. |
2254 | 87 | eraseMatchingBitGroups([VRI](const BitGroup &BG) { |
2255 | 87 | return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt; |
2256 | 87 | }); |
2257 | 13 | } |
2258 | 15.4k | } |
2259 | | |
2260 | | // Instruction selection for the 32-bit case. |
2261 | 15.4k | SDNode *Select32(SDNode *N, bool LateMask, unsigned *InstCnt) { |
2262 | 15.4k | SDLoc dl(N); |
2263 | 15.4k | SDValue Res; |
2264 | | |
2265 | 15.4k | if (InstCnt) *InstCnt = 0; |
2266 | | |
2267 | | // Take care of cases that should use andi/andis first. |
2268 | 15.4k | SelectAndParts32(dl, Res, InstCnt); |
2269 | | |
2270 | | // If we've not yet selected a 'starting' instruction, and we have no zeros |
2271 | | // to fill in, select the (Value, RLAmt) with the highest priority (largest |
2272 | | // number of groups), and start with this rotated value. |
2273 | 15.4k | if ((!NeedMask || LateMask) && !Res) { |
2274 | 7.89k | ValueRotInfo &VRI = ValueRotsVec[0]; |
2275 | 7.89k | if (VRI.RLAmt) { |
2276 | 7.29k | if (InstCnt) *InstCnt += 1; |
2277 | 7.29k | SDValue Ops[] = |
2278 | 7.29k | { TruncateToInt32(VRI.V, dl), getI32Imm(VRI.RLAmt, dl), |
2279 | 7.29k | getI32Imm(0, dl), getI32Imm(31, dl) }; |
2280 | 7.29k | Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), |
2281 | 7.29k | 0); |
2282 | 7.29k | } else { |
2283 | 603 | Res = TruncateToInt32(VRI.V, dl); |
2284 | 603 | } |
2285 | | |
2286 | | // Now, remove all groups with this underlying value and rotation factor. |
2287 | 10.0k | eraseMatchingBitGroups([VRI](const BitGroup &BG) { |
2288 | 10.0k | return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt; |
2289 | 10.0k | }); |
2290 | 7.89k | } |
2291 | | |
2292 | 15.4k | if (InstCnt) *InstCnt += BitGroups.size(); |
2293 | | |
2294 | | // Insert the other groups (one at a time). |
2295 | 15.4k | for (auto &BG : BitGroups) { |
2296 | 10.7k | if (!Res) { |
2297 | 7.58k | SDValue Ops[] = |
2298 | 7.58k | { TruncateToInt32(BG.V, dl), getI32Imm(BG.RLAmt, dl), |
2299 | 7.58k | getI32Imm(Bits.size() - BG.EndIdx - 1, dl), |
2300 | 7.58k | getI32Imm(Bits.size() - BG.StartIdx - 1, dl) }; |
2301 | 7.58k | Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0); |
2302 | 7.58k | } else { |
2303 | 3.12k | SDValue Ops[] = |
2304 | 3.12k | { Res, TruncateToInt32(BG.V, dl), getI32Imm(BG.RLAmt, dl), |
2305 | 3.12k | getI32Imm(Bits.size() - BG.EndIdx - 1, dl), |
2306 | 3.12k | getI32Imm(Bits.size() - BG.StartIdx - 1, dl) }; |
2307 | 3.12k | Res = SDValue(CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops), 0); |
2308 | 3.12k | } |
2309 | 10.7k | } |
2310 | | |
2311 | 15.4k | if (LateMask) { |
2312 | 7.59k | unsigned Mask = (unsigned) getZerosMask(); |
2313 | | |
2314 | 7.59k | unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16; |
2315 | 7.59k | assert((ANDIMask != 0 || ANDISMask != 0) && |
2316 | 7.59k | "No set bits in zeros mask?"); |
2317 | | |
2318 | 7.59k | if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) + |
2319 | 7.59k | (unsigned) (ANDISMask != 0) + |
2320 | 7.59k | (unsigned) (ANDIMask != 0 && ANDISMask != 0); |
2321 | | |
2322 | 7.59k | SDValue ANDIVal, ANDISVal; |
2323 | 7.59k | if (ANDIMask != 0) |
2324 | 7.16k | ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDI_rec, dl, MVT::i32, |
2325 | 7.16k | Res, getI32Imm(ANDIMask, dl)), |
2326 | 7.16k | 0); |
2327 | 7.59k | if (ANDISMask != 0) |
2328 | 2.74k | ANDISVal = |
2329 | 2.74k | SDValue(CurDAG->getMachineNode(PPC::ANDIS_rec, dl, MVT::i32, Res, |
2330 | 2.74k | getI32Imm(ANDISMask, dl)), |
2331 | 2.74k | 0); |
2332 | | |
2333 | 7.59k | if (!ANDIVal) |
2334 | 434 | Res = ANDISVal; |
2335 | 7.16k | else if (!ANDISVal) |
2336 | 4.85k | Res = ANDIVal; |
2337 | 2.30k | else |
2338 | 2.30k | Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32, |
2339 | 2.30k | ANDIVal, ANDISVal), 0); |
2340 | 7.59k | } |
2341 | | |
2342 | 0 | return Res.getNode(); |
2343 | 15.4k | } |
2344 | | |
2345 | | unsigned SelectRotMask64Count(unsigned RLAmt, bool Repl32, |
2346 | | unsigned MaskStart, unsigned MaskEnd, |
2347 | 129k | bool IsIns) { |
2348 | | // In the notation used by the instructions, 'start' and 'end' are reversed |
2349 | | // because bits are counted from high to low order. |
2350 | 129k | unsigned InstMaskStart = 64 - MaskEnd - 1, |
2351 | 129k | InstMaskEnd = 64 - MaskStart - 1; |
2352 | | |
2353 | 129k | if (Repl32) |
2354 | 8.24k | return 1; |
2355 | | |
2356 | 120k | if ((!IsIns && (InstMaskEnd == 63 || InstMaskStart == 0)) || |
2357 | 120k | InstMaskEnd == 63 - RLAmt) |
2358 | 108k | return 1; |
2359 | | |
2360 | 12.0k | return 2; |
2361 | 120k | } |
2362 | | |
2363 | | // For 64-bit values, not all combinations of rotates and masks are |
2364 | | // available. Produce one if it is available. |
2365 | | SDValue SelectRotMask64(SDValue V, const SDLoc &dl, unsigned RLAmt, |
2366 | | bool Repl32, unsigned MaskStart, unsigned MaskEnd, |
2367 | 59.3k | unsigned *InstCnt = nullptr) { |
2368 | | // In the notation used by the instructions, 'start' and 'end' are reversed |
2369 | | // because bits are counted from high to low order. |
2370 | 59.3k | unsigned InstMaskStart = 64 - MaskEnd - 1, |
2371 | 59.3k | InstMaskEnd = 64 - MaskStart - 1; |
2372 | | |
2373 | 59.3k | if (InstCnt) *InstCnt += 1; |
2374 | | |
2375 | 59.3k | if (Repl32) { |
2376 | | // This rotation amount assumes that the lower 32 bits of the quantity |
2377 | | // are replicated in the high 32 bits by the rotation operator (which is |
2378 | | // done by rlwinm and friends). |
2379 | 2.30k | assert(InstMaskStart >= 32 && "Mask cannot start out of range"); |
2380 | 0 | assert(InstMaskEnd >= 32 && "Mask cannot end out of range"); |
2381 | 0 | SDValue Ops[] = |
2382 | 2.30k | { ExtendToInt64(V, dl), getI32Imm(RLAmt, dl), |
2383 | 2.30k | getI32Imm(InstMaskStart - 32, dl), getI32Imm(InstMaskEnd - 32, dl) }; |
2384 | 2.30k | return SDValue(CurDAG->getMachineNode(PPC::RLWINM8, dl, MVT::i64, |
2385 | 2.30k | Ops), 0); |
2386 | 2.30k | } |
2387 | | |
2388 | 57.0k | if (InstMaskEnd == 63) { |
2389 | 43.0k | SDValue Ops[] = |
2390 | 43.0k | { ExtendToInt64(V, dl), getI32Imm(RLAmt, dl), |
2391 | 43.0k | getI32Imm(InstMaskStart, dl) }; |
2392 | 43.0k | return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Ops), 0); |
2393 | 43.0k | } |
2394 | | |
2395 | 13.9k | if (InstMaskStart == 0) { |
2396 | 13.1k | SDValue Ops[] = |
2397 | 13.1k | { ExtendToInt64(V, dl), getI32Imm(RLAmt, dl), |
2398 | 13.1k | getI32Imm(InstMaskEnd, dl) }; |
2399 | 13.1k | return SDValue(CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Ops), 0); |
2400 | 13.1k | } |
2401 | | |
2402 | 855 | if (InstMaskEnd == 63 - RLAmt) { |
2403 | 744 | SDValue Ops[] = |
2404 | 744 | { ExtendToInt64(V, dl), getI32Imm(RLAmt, dl), |
2405 | 744 | getI32Imm(InstMaskStart, dl) }; |
2406 | 744 | return SDValue(CurDAG->getMachineNode(PPC::RLDIC, dl, MVT::i64, Ops), 0); |
2407 | 744 | } |
2408 | | |
2409 | | // We cannot do this with a single instruction, so we'll use two. The |
2410 | | // problem is that we're not free to choose both a rotation amount and mask |
2411 | | // start and end independently. We can choose an arbitrary mask start and |
2412 | | // end, but then the rotation amount is fixed. Rotation, however, can be |
2413 | | // inverted, and so by applying an "inverse" rotation first, we can get the |
2414 | | // desired result. |
2415 | 111 | if (InstCnt) *InstCnt += 1; |
2416 | | |
2417 | | // The rotation mask for the second instruction must be MaskStart. |
2418 | 111 | unsigned RLAmt2 = MaskStart; |
2419 | | // The first instruction must rotate V so that the overall rotation amount |
2420 | | // is RLAmt. |
2421 | 111 | unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64; |
2422 | 111 | if (RLAmt1) |
2423 | 111 | V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63); |
2424 | 111 | return SelectRotMask64(V, dl, RLAmt2, false, MaskStart, MaskEnd); |
2425 | 855 | } |
2426 | | |
2427 | | // For 64-bit values, not all combinations of rotates and masks are |
2428 | | // available. Produce a rotate-mask-and-insert if one is available. |
2429 | | SDValue SelectRotMaskIns64(SDValue Base, SDValue V, const SDLoc &dl, |
2430 | | unsigned RLAmt, bool Repl32, unsigned MaskStart, |
2431 | 11.1k | unsigned MaskEnd, unsigned *InstCnt = nullptr) { |
2432 | | // In the notation used by the instructions, 'start' and 'end' are reversed |
2433 | | // because bits are counted from high to low order. |
2434 | 11.1k | unsigned InstMaskStart = 64 - MaskEnd - 1, |
2435 | 11.1k | InstMaskEnd = 64 - MaskStart - 1; |
2436 | | |
2437 | 11.1k | if (InstCnt) *InstCnt += 1; |
2438 | | |
2439 | 11.1k | if (Repl32) { |
2440 | | // This rotation amount assumes that the lower 32 bits of the quantity |
2441 | | // are replicated in the high 32 bits by the rotation operator (which is |
2442 | | // done by rlwinm and friends). |
2443 | 1.21k | assert(InstMaskStart >= 32 && "Mask cannot start out of range"); |
2444 | 0 | assert(InstMaskEnd >= 32 && "Mask cannot end out of range"); |
2445 | 0 | SDValue Ops[] = |
2446 | 1.21k | { ExtendToInt64(Base, dl), ExtendToInt64(V, dl), getI32Imm(RLAmt, dl), |
2447 | 1.21k | getI32Imm(InstMaskStart - 32, dl), getI32Imm(InstMaskEnd - 32, dl) }; |
2448 | 1.21k | return SDValue(CurDAG->getMachineNode(PPC::RLWIMI8, dl, MVT::i64, |
2449 | 1.21k | Ops), 0); |
2450 | 1.21k | } |
2451 | | |
2452 | 9.93k | if (InstMaskEnd == 63 - RLAmt) { |
2453 | 9.67k | SDValue Ops[] = |
2454 | 9.67k | { ExtendToInt64(Base, dl), ExtendToInt64(V, dl), getI32Imm(RLAmt, dl), |
2455 | 9.67k | getI32Imm(InstMaskStart, dl) }; |
2456 | 9.67k | return SDValue(CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops), 0); |
2457 | 9.67k | } |
2458 | | |
2459 | | // We cannot do this with a single instruction, so we'll use two. The |
2460 | | // problem is that we're not free to choose both a rotation amount and mask |
2461 | | // start and end independently. We can choose an arbitrary mask start and |
2462 | | // end, but then the rotation amount is fixed. Rotation, however, can be |
2463 | | // inverted, and so by applying an "inverse" rotation first, we can get the |
2464 | | // desired result. |
2465 | 256 | if (InstCnt) *InstCnt += 1; |
2466 | | |
2467 | | // The rotation mask for the second instruction must be MaskStart. |
2468 | 256 | unsigned RLAmt2 = MaskStart; |
2469 | | // The first instruction must rotate V so that the overall rotation amount |
2470 | | // is RLAmt. |
2471 | 256 | unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64; |
2472 | 256 | if (RLAmt1) |
2473 | 256 | V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63); |
2474 | 256 | return SelectRotMaskIns64(Base, V, dl, RLAmt2, false, MaskStart, MaskEnd); |
2475 | 9.93k | } |
2476 | | |
2477 | 61.6k | void SelectAndParts64(const SDLoc &dl, SDValue &Res, unsigned *InstCnt) { |
2478 | 61.6k | if (BPermRewriterNoMasking) |
2479 | 0 | return; |
2480 | | |
2481 | | // The idea here is the same as in the 32-bit version, but with additional |
2482 | | // complications from the fact that Repl32 might be true. Because we |
2483 | | // aggressively convert bit groups to Repl32 form (which, for small |
2484 | | // rotation factors, involves no other change), and then coalesce, it might |
2485 | | // be the case that a single 64-bit masking operation could handle both |
2486 | | // some Repl32 groups and some non-Repl32 groups. If converting to Repl32 |
2487 | | // form allowed coalescing, then we must use a 32-bit rotaton in order to |
2488 | | // completely capture the new combined bit group. |
2489 | | |
2490 | 72.4k | for (ValueRotInfo &VRI : ValueRotsVec) { |
2491 | 72.4k | uint64_t Mask = 0; |
2492 | | |
2493 | | // We need to add to the mask all bits from the associated bit groups. |
2494 | | // If Repl32 is false, we need to add bits from bit groups that have |
2495 | | // Repl32 true, but are trivially convertable to Repl32 false. Such a |
2496 | | // group is trivially convertable if it overlaps only with the lower 32 |
2497 | | // bits, and the group has not been coalesced. |
2498 | 205k | auto MatchingBG = [VRI](const BitGroup &BG) { |
2499 | 205k | if (VRI.V != BG.V) |
2500 | 57.7k | return false; |
2501 | | |
2502 | 147k | unsigned EffRLAmt = BG.RLAmt; |
2503 | 147k | if (!VRI.Repl32 && BG.Repl32) { |
2504 | 300 | if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx <= BG.EndIdx && |
2505 | 300 | !BG.Repl32Coalesced) { |
2506 | 300 | if (BG.Repl32CR) |
2507 | 242 | EffRLAmt += 32; |
2508 | 300 | } else { |
2509 | 0 | return false; |
2510 | 0 | } |
2511 | 146k | } else if (VRI.Repl32 != BG.Repl32) { |
2512 | 54 | return false; |
2513 | 54 | } |
2514 | | |
2515 | 147k | return VRI.RLAmt == EffRLAmt; |
2516 | 147k | }; |
2517 | | |
2518 | 102k | for (auto &BG : BitGroups) { |
2519 | 102k | if (!MatchingBG(BG)) |
2520 | 29.6k | continue; |
2521 | | |
2522 | 72.7k | if (BG.StartIdx <= BG.EndIdx) { |
2523 | 3.38M | for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i) |
2524 | 3.31M | Mask |= (UINT64_C(1) << i); |
2525 | 72.7k | } else { |
2526 | 806 | for (unsigned i = BG.StartIdx; i < Bits.size(); ++i) |
2527 | 785 | Mask |= (UINT64_C(1) << i); |
2528 | 532 | for (unsigned i = 0; i <= BG.EndIdx; ++i) |
2529 | 511 | Mask |= (UINT64_C(1) << i); |
2530 | 21 | } |
2531 | 72.7k | } |
2532 | | |
2533 | | // We can use the 32-bit andi/andis technique if the mask does not |
2534 | | // require any higher-order bits. This can save an instruction compared |
2535 | | // to always using the general 64-bit technique. |
2536 | 72.4k | bool Use32BitInsts = isUInt<32>(Mask); |
2537 | | // Compute the masks for andi/andis that would be necessary. |
2538 | 72.4k | unsigned ANDIMask = (Mask & UINT16_MAX), |
2539 | 72.4k | ANDISMask = (Mask >> 16) & UINT16_MAX; |
2540 | | |
2541 | 72.4k | bool NeedsRotate = VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask)); |
2542 | | |
2543 | 72.4k | unsigned NumAndInsts = (unsigned) NeedsRotate + |
2544 | 72.4k | (unsigned) (bool) Res; |
2545 | 72.4k | unsigned NumOfSelectInsts = 0; |
2546 | 72.4k | selectI64Imm(CurDAG, dl, Mask, &NumOfSelectInsts); |
2547 | 72.4k | assert(NumOfSelectInsts > 0 && "Failed to select an i64 constant."); |
2548 | 72.4k | if (Use32BitInsts) |
2549 | 12.2k | NumAndInsts += (unsigned) (ANDIMask != 0) + (unsigned) (ANDISMask != 0) + |
2550 | 12.2k | (unsigned) (ANDIMask != 0 && ANDISMask != 0); |
2551 | 60.2k | else |
2552 | 60.2k | NumAndInsts += NumOfSelectInsts + /* and */ 1; |
2553 | | |
2554 | 72.4k | unsigned NumRLInsts = 0; |
2555 | 72.4k | bool FirstBG = true; |
2556 | 72.4k | bool MoreBG = false; |
2557 | 102k | for (auto &BG : BitGroups) { |
2558 | 102k | if (!MatchingBG(BG)) { |
2559 | 29.6k | MoreBG = true; |
2560 | 29.6k | continue; |
2561 | 29.6k | } |
2562 | 72.7k | NumRLInsts += |
2563 | 72.7k | SelectRotMask64Count(BG.RLAmt, BG.Repl32, BG.StartIdx, BG.EndIdx, |
2564 | 72.7k | !FirstBG); |
2565 | 72.7k | FirstBG = false; |
2566 | 72.7k | } |
2567 | | |
2568 | 72.4k | LLVM_DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() |
2569 | 72.4k | << " RL: " << VRI.RLAmt << (VRI.Repl32 ? " (32):" : ":") |
2570 | 72.4k | << "\n\t\t\tisel using masking: " << NumAndInsts |
2571 | 72.4k | << " using rotates: " << NumRLInsts << "\n"); |
2572 | | |
2573 | | // When we'd use andi/andis, we bias toward using the rotates (andi only |
2574 | | // has a record form, and is cracked on POWER cores). However, when using |
2575 | | // general 64-bit constant formation, bias toward the constant form, |
2576 | | // because that exposes more opportunities for CSE. |
2577 | 72.4k | if (NumAndInsts > NumRLInsts) |
2578 | 70.8k | continue; |
2579 | | // When merging multiple bit groups, instruction or is used. |
2580 | | // But when rotate is used, rldimi can inert the rotated value into any |
2581 | | // register, so instruction or can be avoided. |
2582 | 1.64k | if ((Use32BitInsts || MoreBG) && NumAndInsts == NumRLInsts) |
2583 | 1.59k | continue; |
2584 | | |
2585 | 48 | LLVM_DEBUG(dbgs() << "\t\t\t\tusing masking\n"); |
2586 | | |
2587 | 48 | if (InstCnt) *InstCnt += NumAndInsts; |
2588 | | |
2589 | 48 | SDValue VRot; |
2590 | | // We actually need to generate a rotation if we have a non-zero rotation |
2591 | | // factor or, in the Repl32 case, if we care about any of the |
2592 | | // higher-order replicated bits. In the latter case, we generate a mask |
2593 | | // backward so that it actually includes the entire 64 bits. |
2594 | 48 | if (VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask))) |
2595 | 19 | VRot = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32, |
2596 | 19 | VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63); |
2597 | 29 | else |
2598 | 29 | VRot = VRI.V; |
2599 | | |
2600 | 48 | SDValue TotalVal; |
2601 | 48 | if (Use32BitInsts) { |
2602 | 29 | assert((ANDIMask != 0 || ANDISMask != 0) && |
2603 | 29 | "No set bits in mask when using 32-bit ands for 64-bit value"); |
2604 | | |
2605 | 0 | SDValue ANDIVal, ANDISVal; |
2606 | 29 | if (ANDIMask != 0) |
2607 | 21 | ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDI8_rec, dl, MVT::i64, |
2608 | 21 | ExtendToInt64(VRot, dl), |
2609 | 21 | getI32Imm(ANDIMask, dl)), |
2610 | 21 | 0); |
2611 | 29 | if (ANDISMask != 0) |
2612 | 8 | ANDISVal = |
2613 | 8 | SDValue(CurDAG->getMachineNode(PPC::ANDIS8_rec, dl, MVT::i64, |
2614 | 8 | ExtendToInt64(VRot, dl), |
2615 | 8 | getI32Imm(ANDISMask, dl)), |
2616 | 8 | 0); |
2617 | | |
2618 | 29 | if (!ANDIVal) |
2619 | 8 | TotalVal = ANDISVal; |
2620 | 21 | else if (!ANDISVal) |
2621 | 21 | TotalVal = ANDIVal; |
2622 | 0 | else |
2623 | 0 | TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64, |
2624 | 0 | ExtendToInt64(ANDIVal, dl), ANDISVal), 0); |
2625 | 29 | } else { |
2626 | 19 | TotalVal = SDValue(selectI64Imm(CurDAG, dl, Mask), 0); |
2627 | 19 | TotalVal = |
2628 | 19 | SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64, |
2629 | 19 | ExtendToInt64(VRot, dl), TotalVal), |
2630 | 19 | 0); |
2631 | 19 | } |
2632 | | |
2633 | 48 | if (!Res) |
2634 | 42 | Res = TotalVal; |
2635 | 6 | else |
2636 | 6 | Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64, |
2637 | 6 | ExtendToInt64(Res, dl), TotalVal), |
2638 | 6 | 0); |
2639 | | |
2640 | | // Now, remove all groups with this underlying value and rotation |
2641 | | // factor. |
2642 | 48 | eraseMatchingBitGroups(MatchingBG); |
2643 | 48 | } |
2644 | 61.6k | } |
2645 | | |
2646 | | // Instruction selection for the 64-bit case. |
2647 | 61.6k | SDNode *Select64(SDNode *N, bool LateMask, unsigned *InstCnt) { |
2648 | 61.6k | SDLoc dl(N); |
2649 | 61.6k | SDValue Res; |
2650 | | |
2651 | 61.6k | if (InstCnt) *InstCnt = 0; |
2652 | | |
2653 | | // Take care of cases that should use andi/andis first. |
2654 | 61.6k | SelectAndParts64(dl, Res, InstCnt); |
2655 | | |
2656 | | // If we've not yet selected a 'starting' instruction, and we have no zeros |
2657 | | // to fill in, select the (Value, RLAmt) with the highest priority (largest |
2658 | | // number of groups), and start with this rotated value. |
2659 | 61.6k | if ((!NeedMask || LateMask) && !Res) { |
2660 | | // If we have both Repl32 groups and non-Repl32 groups, the non-Repl32 |
2661 | | // groups will come first, and so the VRI representing the largest number |
2662 | | // of groups might not be first (it might be the first Repl32 groups). |
2663 | 34.1k | unsigned MaxGroupsIdx = 0; |
2664 | 34.1k | if (!ValueRotsVec[0].Repl32) { |
2665 | 75.8k | for (unsigned i = 0, ie = ValueRotsVec.size(); i < ie; ++i) |
2666 | 42.2k | if (ValueRotsVec[i].Repl32) { |
2667 | 486 | if (ValueRotsVec[i].NumGroups > ValueRotsVec[0].NumGroups) |
2668 | 4 | MaxGroupsIdx = i; |
2669 | 486 | break; |
2670 | 486 | } |
2671 | 34.1k | } |
2672 | | |
2673 | 34.1k | ValueRotInfo &VRI = ValueRotsVec[MaxGroupsIdx]; |
2674 | 34.1k | bool NeedsRotate = false; |
2675 | 34.1k | if (VRI.RLAmt) { |
2676 | 31.3k | NeedsRotate = true; |
2677 | 31.3k | } else if (VRI.Repl32) { |
2678 | 13 | for (auto &BG : BitGroups) { |
2679 | 13 | if (BG.V != VRI.V || BG.RLAmt != VRI.RLAmt || |
2680 | 13 | BG.Repl32 != VRI.Repl32) |
2681 | 0 | continue; |
2682 | | |
2683 | | // We don't need a rotate if the bit group is confined to the lower |
2684 | | // 32 bits. |
2685 | 13 | if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx < BG.EndIdx) |
2686 | 0 | continue; |
2687 | | |
2688 | 13 | NeedsRotate = true; |
2689 | 13 | break; |
2690 | 13 | } |
2691 | 13 | } |
2692 | | |
2693 | 34.1k | if (NeedsRotate) |
2694 | 31.3k | Res = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32, |
2695 | 31.3k | VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63, |
2696 | 31.3k | InstCnt); |
2697 | 2.74k | else |
2698 | 2.74k | Res = VRI.V; |
2699 | | |
2700 | | // Now, remove all groups with this underlying value and rotation factor. |
2701 | 34.1k | if (Res) |
2702 | 42.9k | eraseMatchingBitGroups([VRI](const BitGroup &BG) { |
2703 | 42.9k | return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt && |
2704 | 42.9k | BG.Repl32 == VRI.Repl32; |
2705 | 42.9k | }); |
2706 | 34.1k | } |
2707 | | |
2708 | | // Because 64-bit rotates are more flexible than inserts, we might have a |
2709 | | // preference regarding which one we do first (to save one instruction). |
2710 | 61.6k | if (!Res) |
2711 | 44.3k | for (auto I = BitGroups.begin(), IE = BitGroups.end(); I != IE; ++I) { |
2712 | 28.2k | if (SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx, |
2713 | 28.2k | false) < |
2714 | 28.2k | SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx, |
2715 | 28.2k | true)) { |
2716 | 11.3k | if (I != BitGroups.begin()) { |
2717 | 21 | BitGroup BG = *I; |
2718 | 21 | BitGroups.erase(I); |
2719 | 21 | BitGroups.insert(BitGroups.begin(), BG); |
2720 | 21 | } |
2721 | | |
2722 | 11.3k | break; |
2723 | 11.3k | } |
2724 | 28.2k | } |
2725 | | |
2726 | | // Insert the other groups (one at a time). |
2727 | 61.6k | for (auto &BG : BitGroups) { |
2728 | 38.3k | if (!Res) |
2729 | 27.4k | Res = SelectRotMask64(BG.V, dl, BG.RLAmt, BG.Repl32, BG.StartIdx, |
2730 | 27.4k | BG.EndIdx, InstCnt); |
2731 | 10.8k | else |
2732 | 10.8k | Res = SelectRotMaskIns64(Res, BG.V, dl, BG.RLAmt, BG.Repl32, |
2733 | 10.8k | BG.StartIdx, BG.EndIdx, InstCnt); |
2734 | 38.3k | } |
2735 | | |
2736 | 61.6k | if (LateMask) { |
2737 | 27.4k | uint64_t Mask = getZerosMask(); |
2738 | | |
2739 | | // We can use the 32-bit andi/andis technique if the mask does not |
2740 | | // require any higher-order bits. This can save an instruction compared |
2741 | | // to always using the general 64-bit technique. |
2742 | 27.4k | bool Use32BitInsts = isUInt<32>(Mask); |
2743 | | // Compute the masks for andi/andis that would be necessary. |
2744 | 27.4k | unsigned ANDIMask = (Mask & UINT16_MAX), |
2745 | 27.4k | ANDISMask = (Mask >> 16) & UINT16_MAX; |
2746 | | |
2747 | 27.4k | if (Use32BitInsts) { |
2748 | 4.40k | assert((ANDIMask != 0 || ANDISMask != 0) && |
2749 | 4.40k | "No set bits in mask when using 32-bit ands for 64-bit value"); |
2750 | | |
2751 | 4.40k | if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) + |
2752 | 4.40k | (unsigned) (ANDISMask != 0) + |
2753 | 4.40k | (unsigned) (ANDIMask != 0 && ANDISMask != 0); |
2754 | | |
2755 | 4.40k | SDValue ANDIVal, ANDISVal; |
2756 | 4.40k | if (ANDIMask != 0) |
2757 | 4.32k | ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDI8_rec, dl, MVT::i64, |
2758 | 4.32k | ExtendToInt64(Res, dl), |
2759 | 4.32k | getI32Imm(ANDIMask, dl)), |
2760 | 4.32k | 0); |
2761 | 4.40k | if (ANDISMask != 0) |
2762 | 773 | ANDISVal = |
2763 | 773 | SDValue(CurDAG->getMachineNode(PPC::ANDIS8_rec, dl, MVT::i64, |
2764 | 773 | ExtendToInt64(Res, dl), |
2765 | 773 | getI32Imm(ANDISMask, dl)), |
2766 | 773 | 0); |
2767 | | |
2768 | 4.40k | if (!ANDIVal) |
2769 | 89 | Res = ANDISVal; |
2770 | 4.32k | else if (!ANDISVal) |
2771 | 3.63k | Res = ANDIVal; |
2772 | 684 | else |
2773 | 684 | Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64, |
2774 | 684 | ExtendToInt64(ANDIVal, dl), ANDISVal), 0); |
2775 | 23.0k | } else { |
2776 | 23.0k | unsigned NumOfSelectInsts = 0; |
2777 | 23.0k | SDValue MaskVal = |
2778 | 23.0k | SDValue(selectI64Imm(CurDAG, dl, Mask, &NumOfSelectInsts), 0); |
2779 | 23.0k | Res = SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64, |
2780 | 23.0k | ExtendToInt64(Res, dl), MaskVal), |
2781 | 23.0k | 0); |
2782 | 23.0k | if (InstCnt) |
2783 | 23.0k | *InstCnt += NumOfSelectInsts + /* and */ 1; |
2784 | 23.0k | } |
2785 | 27.4k | } |
2786 | | |
2787 | 0 | return Res.getNode(); |
2788 | 61.6k | } |
2789 | | |
2790 | 77.1k | SDNode *Select(SDNode *N, bool LateMask, unsigned *InstCnt = nullptr) { |
2791 | | // Fill in BitGroups. |
2792 | 77.1k | collectBitGroups(LateMask); |
2793 | 77.1k | if (BitGroups.empty()) |
2794 | 0 | return nullptr; |
2795 | | |
2796 | | // For 64-bit values, figure out when we can use 32-bit instructions. |
2797 | 77.1k | if (Bits.size() == 64) |
2798 | 61.6k | assignRepl32BitGroups(); |
2799 | | |
2800 | | // Fill in ValueRotsVec. |
2801 | 77.1k | collectValueRotInfo(); |
2802 | | |
2803 | 77.1k | if (Bits.size() == 32) { |
2804 | 15.4k | return Select32(N, LateMask, InstCnt); |
2805 | 61.6k | } else { |
2806 | 61.6k | assert(Bits.size() == 64 && "Not 64 bits here?"); |
2807 | 0 | return Select64(N, LateMask, InstCnt); |
2808 | 61.6k | } |
2809 | | |
2810 | 0 | return nullptr; |
2811 | 77.1k | } |
2812 | | |
2813 | 42.1k | void eraseMatchingBitGroups(function_ref<bool(const BitGroup &)> F) { |
2814 | 42.1k | erase_if(BitGroups, F); |
2815 | 42.1k | } |
2816 | | |
2817 | | SmallVector<ValueBit, 64> Bits; |
2818 | | |
2819 | | bool NeedMask = false; |
2820 | | SmallVector<unsigned, 64> RLAmt; |
2821 | | |
2822 | | SmallVector<BitGroup, 16> BitGroups; |
2823 | | |
2824 | | DenseMap<std::pair<SDValue, unsigned>, ValueRotInfo> ValueRots; |
2825 | | SmallVector<ValueRotInfo, 16> ValueRotsVec; |
2826 | | |
2827 | | SelectionDAG *CurDAG = nullptr; |
2828 | | |
2829 | | public: |
2830 | | BitPermutationSelector(SelectionDAG *DAG) |
2831 | 162k | : CurDAG(DAG) {} |
2832 | | |
2833 | | // Here we try to match complex bit permutations into a set of |
2834 | | // rotate-and-shift/shift/and/or instructions, using a set of heuristics |
2835 | | // known to produce optimal code for common cases (like i32 byte swapping). |
2836 | 162k | SDNode *Select(SDNode *N) { |
2837 | 162k | Memoizer.clear(); |
2838 | 162k | auto Result = |
2839 | 162k | getValueBits(SDValue(N, 0), N->getValueType(0).getSizeInBits()); |
2840 | 162k | if (!Result.first) |
2841 | 120k | return nullptr; |
2842 | 42.0k | Bits = std::move(*Result.second); |
2843 | | |
2844 | 42.0k | LLVM_DEBUG(dbgs() << "Considering bit-permutation-based instruction" |
2845 | 42.0k | " selection for: "); |
2846 | 42.0k | LLVM_DEBUG(N->dump(CurDAG)); |
2847 | | |
2848 | | // Fill it RLAmt and set NeedMask. |
2849 | 42.0k | computeRotationAmounts(); |
2850 | | |
2851 | 42.0k | if (!NeedMask) |
2852 | 6.96k | return Select(N, false); |
2853 | | |
2854 | | // We currently have two techniques for handling results with zeros: early |
2855 | | // masking (the default) and late masking. Late masking is sometimes more |
2856 | | // efficient, but because the structure of the bit groups is different, it |
2857 | | // is hard to tell without generating both and comparing the results. With |
2858 | | // late masking, we ignore zeros in the resulting value when inserting each |
2859 | | // set of bit groups, and then mask in the zeros at the end. With early |
2860 | | // masking, we only insert the non-zero parts of the result at every step. |
2861 | | |
2862 | 35.0k | unsigned InstCnt = 0, InstCntLateMask = 0; |
2863 | 35.0k | LLVM_DEBUG(dbgs() << "\tEarly masking:\n"); |
2864 | 35.0k | SDNode *RN = Select(N, false, &InstCnt); |
2865 | 35.0k | LLVM_DEBUG(dbgs() << "\t\tisel would use " << InstCnt << " instructions\n"); |
2866 | | |
2867 | 35.0k | LLVM_DEBUG(dbgs() << "\tLate masking:\n"); |
2868 | 35.0k | SDNode *RNLM = Select(N, true, &InstCntLateMask); |
2869 | 35.0k | LLVM_DEBUG(dbgs() << "\t\tisel would use " << InstCntLateMask |
2870 | 35.0k | << " instructions\n"); |
2871 | | |
2872 | 35.0k | if (InstCnt <= InstCntLateMask) { |
2873 | 35.0k | LLVM_DEBUG(dbgs() << "\tUsing early-masking for isel\n"); |
2874 | 35.0k | return RN; |
2875 | 35.0k | } |
2876 | | |
2877 | 4 | LLVM_DEBUG(dbgs() << "\tUsing late-masking for isel\n"); |
2878 | 4 | return RNLM; |
2879 | 35.0k | } |
2880 | | }; |
2881 | | |
2882 | | class IntegerCompareEliminator { |
2883 | | SelectionDAG *CurDAG; |
2884 | | PPCDAGToDAGISel *S; |
2885 | | // Conversion type for interpreting results of a 32-bit instruction as |
2886 | | // a 64-bit value or vice versa. |
2887 | | enum ExtOrTruncConversion { Ext, Trunc }; |
2888 | | |
2889 | | // Modifiers to guide how an ISD::SETCC node's result is to be computed |
2890 | | // in a GPR. |
2891 | | // ZExtOrig - use the original condition code, zero-extend value |
2892 | | // ZExtInvert - invert the condition code, zero-extend value |
2893 | | // SExtOrig - use the original condition code, sign-extend value |
2894 | | // SExtInvert - invert the condition code, sign-extend value |
2895 | | enum SetccInGPROpts { ZExtOrig, ZExtInvert, SExtOrig, SExtInvert }; |
2896 | | |
2897 | | // Comparisons against zero to emit GPR code sequences for. Each of these |
2898 | | // sequences may need to be emitted for two or more equivalent patterns. |
2899 | | // For example (a >= 0) == (a > -1). The direction of the comparison (</>) |
2900 | | // matters as well as the extension type: sext (-1/0), zext (1/0). |
2901 | | // GEZExt - (zext (LHS >= 0)) |
2902 | | // GESExt - (sext (LHS >= 0)) |
2903 | | // LEZExt - (zext (LHS <= 0)) |
2904 | | // LESExt - (sext (LHS <= 0)) |
2905 | | enum ZeroCompare { GEZExt, GESExt, LEZExt, LESExt }; |
2906 | | |
2907 | | SDNode *tryEXTEND(SDNode *N); |
2908 | | SDNode *tryLogicOpOfCompares(SDNode *N); |
2909 | | SDValue computeLogicOpInGPR(SDValue LogicOp); |
2910 | | SDValue signExtendInputIfNeeded(SDValue Input); |
2911 | | SDValue zeroExtendInputIfNeeded(SDValue Input); |
2912 | | SDValue addExtOrTrunc(SDValue NatWidthRes, ExtOrTruncConversion Conv); |
2913 | | SDValue getCompoundZeroComparisonInGPR(SDValue LHS, SDLoc dl, |
2914 | | ZeroCompare CmpTy); |
2915 | | SDValue get32BitZExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC, |
2916 | | int64_t RHSValue, SDLoc dl); |
2917 | | SDValue get32BitSExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC, |
2918 | | int64_t RHSValue, SDLoc dl); |
2919 | | SDValue get64BitZExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC, |
2920 | | int64_t RHSValue, SDLoc dl); |
2921 | | SDValue get64BitSExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC, |
2922 | | int64_t RHSValue, SDLoc dl); |
2923 | | SDValue getSETCCInGPR(SDValue Compare, SetccInGPROpts ConvOpts); |
2924 | | |
2925 | | public: |
2926 | | IntegerCompareEliminator(SelectionDAG *DAG, |
2927 | 134k | PPCDAGToDAGISel *Sel) : CurDAG(DAG), S(Sel) { |
2928 | 134k | assert(CurDAG->getTargetLoweringInfo() |
2929 | 134k | .getPointerTy(CurDAG->getDataLayout()).getSizeInBits() == 64 && |
2930 | 134k | "Only expecting to use this on 64 bit targets."); |
2931 | 134k | } |
2932 | 134k | SDNode *Select(SDNode *N) { |
2933 | 134k | if (CmpInGPR == ICGPR_None) |
2934 | 0 | return nullptr; |
2935 | 134k | switch (N->getOpcode()) { |
2936 | 0 | default: break; |
2937 | 21.9k | case ISD::ZERO_EXTEND: |
2938 | 21.9k | if (CmpInGPR == ICGPR_Sext || CmpInGPR == ICGPR_SextI32 || |
2939 | 21.9k | CmpInGPR == ICGPR_SextI64) |
2940 | 0 | return nullptr; |
2941 | 21.9k | [[fallthrough]]; |
2942 | 25.0k | case ISD::SIGN_EXTEND: |
2943 | 25.0k | if (CmpInGPR == ICGPR_Zext || CmpInGPR == ICGPR_ZextI32 || |
2944 | 25.0k | CmpInGPR == ICGPR_ZextI64) |
2945 | 0 | return nullptr; |
2946 | 25.0k | return tryEXTEND(N); |
2947 | 42.8k | case ISD::AND: |
2948 | 96.3k | case ISD::OR: |
2949 | 109k | case ISD::XOR: |
2950 | 109k | return tryLogicOpOfCompares(N); |
2951 | 134k | } |
2952 | 0 | return nullptr; |
2953 | 134k | } |
2954 | | }; |
2955 | | |
2956 | | // The obvious case for wanting to keep the value in a GPR. Namely, the |
2957 | | // result of the comparison is actually needed in a GPR. |
2958 | 25.0k | SDNode *IntegerCompareEliminator::tryEXTEND(SDNode *N) { |
2959 | 25.0k | assert((N->getOpcode() == ISD::ZERO_EXTEND || |
2960 | 25.0k | N->getOpcode() == ISD::SIGN_EXTEND) && |
2961 | 25.0k | "Expecting a zero/sign extend node!"); |
2962 | 0 | SDValue WideRes; |
2963 | | // If we are zero-extending the result of a logical operation on i1 |
2964 | | // values, we can keep the values in GPRs. |
2965 | 25.0k | if (ISD::isBitwiseLogicOp(N->getOperand(0).getOpcode()) && |
2966 | 25.0k | N->getOperand(0).getValueType() == MVT::i1 && |
2967 | 25.0k | N->getOpcode() == ISD::ZERO_EXTEND) |
2968 | 3.65k | WideRes = computeLogicOpInGPR(N->getOperand(0)); |
2969 | 21.3k | else if (N->getOperand(0).getOpcode() != ISD::SETCC) |
2970 | 10.0k | return nullptr; |
2971 | 11.3k | else |
2972 | 11.3k | WideRes = |
2973 | 11.3k | getSETCCInGPR(N->getOperand(0), |
2974 | 11.3k | N->getOpcode() == ISD::SIGN_EXTEND ? |
2975 | 11.2k | SetccInGPROpts::SExtOrig : SetccInGPROpts::ZExtOrig); |
2976 | | |
2977 | 14.9k | if (!WideRes) |
2978 | 6.37k | return nullptr; |
2979 | | |
2980 | 8.58k | SDLoc dl(N); |
2981 | 8.58k | bool Input32Bit = WideRes.getValueType() == MVT::i32; |
2982 | 8.58k | bool Output32Bit = N->getValueType(0) == MVT::i32; |
2983 | | |
2984 | 8.58k | NumSextSetcc += N->getOpcode() == ISD::SIGN_EXTEND ? 1 : 0; |
2985 | 8.58k | NumZextSetcc += N->getOpcode() == ISD::SIGN_EXTEND ? 0 : 1; |
2986 | | |
2987 | 8.58k | SDValue ConvOp = WideRes; |
2988 | 8.58k | if (Input32Bit != Output32Bit) |
2989 | 1.57k | ConvOp = addExtOrTrunc(WideRes, Input32Bit ? ExtOrTruncConversion::Ext : |
2990 | 1.57k | ExtOrTruncConversion::Trunc); |
2991 | 8.58k | return ConvOp.getNode(); |
2992 | 14.9k | } |
2993 | | |
2994 | | // Attempt to perform logical operations on the results of comparisons while |
2995 | | // keeping the values in GPRs. Without doing so, these would end up being |
2996 | | // lowered to CR-logical operations which suffer from significant latency and |
2997 | | // low ILP. |
2998 | 109k | SDNode *IntegerCompareEliminator::tryLogicOpOfCompares(SDNode *N) { |
2999 | 109k | if (N->getValueType(0) != MVT::i1) |
3000 | 109k | return nullptr; |
3001 | 0 | assert(ISD::isBitwiseLogicOp(N->getOpcode()) && |
3002 | 0 | "Expected a logic operation on setcc results."); |
3003 | 0 | SDValue LoweredLogical = computeLogicOpInGPR(SDValue(N, 0)); |
3004 | 0 | if (!LoweredLogical) |
3005 | 0 | return nullptr; |
3006 | | |
3007 | 0 | SDLoc dl(N); |
3008 | 0 | bool IsBitwiseNegate = LoweredLogical.getMachineOpcode() == PPC::XORI8; |
3009 | 0 | unsigned SubRegToExtract = IsBitwiseNegate ? PPC::sub_eq : PPC::sub_gt; |
3010 | 0 | SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32); |
3011 | 0 | SDValue LHS = LoweredLogical.getOperand(0); |
3012 | 0 | SDValue RHS = LoweredLogical.getOperand(1); |
3013 | 0 | SDValue WideOp; |
3014 | 0 | SDValue OpToConvToRecForm; |
3015 | | |
3016 | | // Look through any 32-bit to 64-bit implicit extend nodes to find the |
3017 | | // opcode that is input to the XORI. |
3018 | 0 | if (IsBitwiseNegate && |
3019 | 0 | LoweredLogical.getOperand(0).getMachineOpcode() == PPC::INSERT_SUBREG) |
3020 | 0 | OpToConvToRecForm = LoweredLogical.getOperand(0).getOperand(1); |
3021 | 0 | else if (IsBitwiseNegate) |
3022 | | // If the input to the XORI isn't an extension, that's what we're after. |
3023 | 0 | OpToConvToRecForm = LoweredLogical.getOperand(0); |
3024 | 0 | else |
3025 | | // If this is not an XORI, it is a reg-reg logical op and we can convert |
3026 | | // it to record-form. |
3027 | 0 | OpToConvToRecForm = LoweredLogical; |
3028 | | |
3029 | | // Get the record-form version of the node we're looking to use to get the |
3030 | | // CR result from. |
3031 | 0 | uint16_t NonRecOpc = OpToConvToRecForm.getMachineOpcode(); |
3032 | 0 | int NewOpc = PPCInstrInfo::getRecordFormOpcode(NonRecOpc); |
3033 | | |
3034 | | // Convert the right node to record-form. This is either the logical we're |
3035 | | // looking at or it is the input node to the negation (if we're looking at |
3036 | | // a bitwise negation). |
3037 | 0 | if (NewOpc != -1 && IsBitwiseNegate) { |
3038 | | // The input to the XORI has a record-form. Use it. |
3039 | 0 | assert(LoweredLogical.getConstantOperandVal(1) == 1 && |
3040 | 0 | "Expected a PPC::XORI8 only for bitwise negation."); |
3041 | | // Emit the record-form instruction. |
3042 | 0 | std::vector<SDValue> Ops; |
3043 | 0 | for (int i = 0, e = OpToConvToRecForm.getNumOperands(); i < e; i++) |
3044 | 0 | Ops.push_back(OpToConvToRecForm.getOperand(i)); |
3045 | |
|
3046 | 0 | WideOp = |
3047 | 0 | SDValue(CurDAG->getMachineNode(NewOpc, dl, |
3048 | 0 | OpToConvToRecForm.getValueType(), |
3049 | 0 | MVT::Glue, Ops), 0); |
3050 | 0 | } else { |
3051 | 0 | assert((NewOpc != -1 || !IsBitwiseNegate) && |
3052 | 0 | "No record form available for AND8/OR8/XOR8?"); |
3053 | 0 | WideOp = |
3054 | 0 | SDValue(CurDAG->getMachineNode(NewOpc == -1 ? PPC::ANDI8_rec : NewOpc, |
3055 | 0 | dl, MVT::i64, MVT::Glue, LHS, RHS), |
3056 | 0 | 0); |
3057 | 0 | } |
3058 | | |
3059 | | // Select this node to a single bit from CR0 set by the record-form node |
3060 | | // just created. For bitwise negation, use the EQ bit which is the equivalent |
3061 | | // of negating the result (i.e. it is a bit set when the result of the |
3062 | | // operation is zero). |
3063 | 0 | SDValue SRIdxVal = |
3064 | 0 | CurDAG->getTargetConstant(SubRegToExtract, dl, MVT::i32); |
3065 | 0 | SDValue CRBit = |
3066 | 0 | SDValue(CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, dl, |
3067 | 0 | MVT::i1, CR0Reg, SRIdxVal, |
3068 | 0 | WideOp.getValue(1)), 0); |
3069 | 0 | return CRBit.getNode(); |
3070 | 0 | } |
3071 | | |
3072 | | // Lower a logical operation on i1 values into a GPR sequence if possible. |
3073 | | // The result can be kept in a GPR if requested. |
3074 | | // Three types of inputs can be handled: |
3075 | | // - SETCC |
3076 | | // - TRUNCATE |
3077 | | // - Logical operation (AND/OR/XOR) |
3078 | | // There is also a special case that is handled (namely a complement operation |
3079 | | // achieved with xor %a, -1). |
3080 | 4.94k | SDValue IntegerCompareEliminator::computeLogicOpInGPR(SDValue LogicOp) { |
3081 | 4.94k | assert(ISD::isBitwiseLogicOp(LogicOp.getOpcode()) && |
3082 | 4.94k | "Can only handle logic operations here."); |
3083 | 0 | assert(LogicOp.getValueType() == MVT::i1 && |
3084 | 4.94k | "Can only handle logic operations on i1 values here."); |
3085 | 0 | SDLoc dl(LogicOp); |
3086 | 4.94k | SDValue LHS, RHS; |
3087 | | |
3088 | | // Special case: xor %a, -1 |
3089 | 4.94k | bool IsBitwiseNegation = isBitwiseNot(LogicOp); |
3090 | | |
3091 | | // Produces a GPR sequence for each operand of the binary logic operation. |
3092 | | // For SETCC, it produces the respective comparison, for TRUNCATE it truncates |
3093 | | // the value in a GPR and for logic operations, it will recursively produce |
3094 | | // a GPR sequence for the operation. |
3095 | 9.89k | auto getLogicOperand = [&] (SDValue Operand) -> SDValue { |
3096 | 9.89k | unsigned OperandOpcode = Operand.getOpcode(); |
3097 | 9.89k | if (OperandOpcode == ISD::SETCC) |
3098 | 5.06k | return getSETCCInGPR(Operand, SetccInGPROpts::ZExtOrig); |
3099 | 4.82k | else if (OperandOpcode == ISD::TRUNCATE) { |
3100 | 1.70k | SDValue InputOp = Operand.getOperand(0); |
3101 | 1.70k | EVT InVT = InputOp.getValueType(); |
3102 | 1.70k | return SDValue(CurDAG->getMachineNode(InVT == MVT::i32 ? PPC::RLDICL_32 : |
3103 | 1.70k | PPC::RLDICL, dl, InVT, InputOp, |
3104 | 1.70k | S->getI64Imm(0, dl), |
3105 | 1.70k | S->getI64Imm(63, dl)), 0); |
3106 | 3.12k | } else if (ISD::isBitwiseLogicOp(OperandOpcode)) |
3107 | 1.29k | return computeLogicOpInGPR(Operand); |
3108 | 1.82k | return SDValue(); |
3109 | 9.89k | }; |
3110 | 4.94k | LHS = getLogicOperand(LogicOp.getOperand(0)); |
3111 | 4.94k | RHS = getLogicOperand(LogicOp.getOperand(1)); |
3112 | | |
3113 | | // If a GPR sequence can't be produced for the LHS we can't proceed. |
3114 | | // Not producing a GPR sequence for the RHS is only a problem if this isn't |
3115 | | // a bitwise negation operation. |
3116 | 4.94k | if (!LHS || (!RHS && !IsBitwiseNegation)) |
3117 | 3.16k | return SDValue(); |
3118 | | |
3119 | 1.78k | NumLogicOpsOnComparison++; |
3120 | | |
3121 | | // We will use the inputs as 64-bit values. |
3122 | 1.78k | if (LHS.getValueType() == MVT::i32) |
3123 | 163 | LHS = addExtOrTrunc(LHS, ExtOrTruncConversion::Ext); |
3124 | 1.78k | if (!IsBitwiseNegation && RHS.getValueType() == MVT::i32) |
3125 | 155 | RHS = addExtOrTrunc(RHS, ExtOrTruncConversion::Ext); |
3126 | | |
3127 | 1.78k | unsigned NewOpc; |
3128 | 1.78k | switch (LogicOp.getOpcode()) { |
3129 | 0 | default: llvm_unreachable("Unknown logic operation."); |
3130 | 373 | case ISD::AND: NewOpc = PPC::AND8; break; |
3131 | 438 | case ISD::OR: NewOpc = PPC::OR8; break; |
3132 | 974 | case ISD::XOR: NewOpc = PPC::XOR8; break; |
3133 | 1.78k | } |
3134 | | |
3135 | 1.78k | if (IsBitwiseNegation) { |
3136 | 760 | RHS = S->getI64Imm(1, dl); |
3137 | 760 | NewOpc = PPC::XORI8; |
3138 | 760 | } |
3139 | | |
3140 | 1.78k | return SDValue(CurDAG->getMachineNode(NewOpc, dl, MVT::i64, LHS, RHS), 0); |
3141 | | |
3142 | 1.78k | } |
3143 | | |
3144 | | /// If the value isn't guaranteed to be sign-extended to 64-bits, extend it. |
3145 | | /// Otherwise just reinterpret it as a 64-bit value. |
3146 | | /// Useful when emitting comparison code for 32-bit values without using |
3147 | | /// the compare instruction (which only considers the lower 32-bits). |
3148 | 4.73k | SDValue IntegerCompareEliminator::signExtendInputIfNeeded(SDValue Input) { |
3149 | 4.73k | assert(Input.getValueType() == MVT::i32 && |
3150 | 4.73k | "Can only sign-extend 32-bit values here."); |
3151 | 0 | unsigned Opc = Input.getOpcode(); |
3152 | | |
3153 | | // The value was sign extended and then truncated to 32-bits. No need to |
3154 | | // sign extend it again. |
3155 | 4.73k | if (Opc == ISD::TRUNCATE && |
3156 | 4.73k | (Input.getOperand(0).getOpcode() == ISD::AssertSext || |
3157 | 447 | Input.getOperand(0).getOpcode() == ISD::SIGN_EXTEND)) |
3158 | 3 | return addExtOrTrunc(Input, ExtOrTruncConversion::Ext); |
3159 | | |
3160 | 4.73k | LoadSDNode *InputLoad = dyn_cast<LoadSDNode>(Input); |
3161 | | // The input is a sign-extending load. All ppc sign-extending loads |
3162 | | // sign-extend to the full 64-bits. |
3163 | 4.73k | if (InputLoad && InputLoad->getExtensionType() == ISD::SEXTLOAD) |
3164 | 682 | return addExtOrTrunc(Input, ExtOrTruncConversion::Ext); |
3165 | | |
3166 | 4.05k | ConstantSDNode *InputConst = dyn_cast<ConstantSDNode>(Input); |
3167 | | // We don't sign-extend constants. |
3168 | 4.05k | if (InputConst) |
3169 | 493 | return addExtOrTrunc(Input, ExtOrTruncConversion::Ext); |
3170 | | |
3171 | 3.55k | SDLoc dl(Input); |
3172 | 3.55k | SignExtensionsAdded++; |
3173 | 3.55k | return SDValue(CurDAG->getMachineNode(PPC::EXTSW_32_64, dl, |
3174 | 3.55k | MVT::i64, Input), 0); |
3175 | 4.05k | } |
3176 | | |
3177 | | /// If the value isn't guaranteed to be zero-extended to 64-bits, extend it. |
3178 | | /// Otherwise just reinterpret it as a 64-bit value. |
3179 | | /// Useful when emitting comparison code for 32-bit values without using |
3180 | | /// the compare instruction (which only considers the lower 32-bits). |
3181 | 290 | SDValue IntegerCompareEliminator::zeroExtendInputIfNeeded(SDValue Input) { |
3182 | 290 | assert(Input.getValueType() == MVT::i32 && |
3183 | 290 | "Can only zero-extend 32-bit values here."); |
3184 | 0 | unsigned Opc = Input.getOpcode(); |
3185 | | |
3186 | | // The only condition under which we can omit the actual extend instruction: |
3187 | | // - The value is a positive constant |
3188 | | // - The value comes from a load that isn't a sign-extending load |
3189 | | // An ISD::TRUNCATE needs to be zero-extended unless it is fed by a zext. |
3190 | 290 | bool IsTruncateOfZExt = Opc == ISD::TRUNCATE && |
3191 | 290 | (Input.getOperand(0).getOpcode() == ISD::AssertZext || |
3192 | 31 | Input.getOperand(0).getOpcode() == ISD::ZERO_EXTEND); |
3193 | 290 | if (IsTruncateOfZExt) |
3194 | 0 | return addExtOrTrunc(Input, ExtOrTruncConversion::Ext); |
3195 | | |
3196 | 290 | ConstantSDNode *InputConst = dyn_cast<ConstantSDNode>(Input); |
3197 | 290 | if (InputConst && InputConst->getSExtValue() >= 0) |
3198 | 24 | return addExtOrTrunc(Input, ExtOrTruncConversion::Ext); |
3199 | | |
3200 | 266 | LoadSDNode *InputLoad = dyn_cast<LoadSDNode>(Input); |
3201 | | // The input is a load that doesn't sign-extend (it will be zero-extended). |
3202 | 266 | if (InputLoad && InputLoad->getExtensionType() != ISD::SEXTLOAD) |
3203 | 128 | return addExtOrTrunc(Input, ExtOrTruncConversion::Ext); |
3204 | | |
3205 | | // None of the above, need to zero-extend. |
3206 | 138 | SDLoc dl(Input); |
3207 | 138 | ZeroExtensionsAdded++; |
3208 | 138 | return SDValue(CurDAG->getMachineNode(PPC::RLDICL_32_64, dl, MVT::i64, Input, |
3209 | 138 | S->getI64Imm(0, dl), |
3210 | 138 | S->getI64Imm(32, dl)), 0); |
3211 | 266 | } |
3212 | | |
3213 | | // Handle a 32-bit value in a 64-bit register and vice-versa. These are of |
3214 | | // course not actual zero/sign extensions that will generate machine code, |
3215 | | // they're just a way to reinterpret a 32 bit value in a register as a |
3216 | | // 64 bit value and vice-versa. |
3217 | | SDValue IntegerCompareEliminator::addExtOrTrunc(SDValue NatWidthRes, |
3218 | 3.22k | ExtOrTruncConversion Conv) { |
3219 | 3.22k | SDLoc dl(NatWidthRes); |
3220 | | |
3221 | | // For reinterpreting 32-bit values as 64 bit values, we generate |
3222 | | // INSERT_SUBREG IMPLICIT_DEF:i64, <input>, TargetConstant:i32<1> |
3223 | 3.22k | if (Conv == ExtOrTruncConversion::Ext) { |
3224 | 3.16k | SDValue ImDef(CurDAG->getMachineNode(PPC::IMPLICIT_DEF, dl, MVT::i64), 0); |
3225 | 3.16k | SDValue SubRegIdx = |
3226 | 3.16k | CurDAG->getTargetConstant(PPC::sub_32, dl, MVT::i32); |
3227 | 3.16k | return SDValue(CurDAG->getMachineNode(PPC::INSERT_SUBREG, dl, MVT::i64, |
3228 | 3.16k | ImDef, NatWidthRes, SubRegIdx), 0); |
3229 | 3.16k | } |
3230 | | |
3231 | 53 | assert(Conv == ExtOrTruncConversion::Trunc && |
3232 | 53 | "Unknown convertion between 32 and 64 bit values."); |
3233 | | // For reinterpreting 64-bit values as 32-bit values, we just need to |
3234 | | // EXTRACT_SUBREG (i.e. extract the low word). |
3235 | 0 | SDValue SubRegIdx = |
3236 | 53 | CurDAG->getTargetConstant(PPC::sub_32, dl, MVT::i32); |
3237 | 53 | return SDValue(CurDAG->getMachineNode(PPC::EXTRACT_SUBREG, dl, MVT::i32, |
3238 | 53 | NatWidthRes, SubRegIdx), 0); |
3239 | 3.22k | } |
3240 | | |
3241 | | // Produce a GPR sequence for compound comparisons (<=, >=) against zero. |
3242 | | // Handle both zero-extensions and sign-extensions. |
3243 | | SDValue |
3244 | | IntegerCompareEliminator::getCompoundZeroComparisonInGPR(SDValue LHS, SDLoc dl, |
3245 | 263 | ZeroCompare CmpTy) { |
3246 | 263 | EVT InVT = LHS.getValueType(); |
3247 | 263 | bool Is32Bit = InVT == MVT::i32; |
3248 | 263 | SDValue ToExtend; |
3249 | | |
3250 | | // Produce the value that needs to be either zero or sign extended. |
3251 | 263 | switch (CmpTy) { |
3252 | 78 | case ZeroCompare::GEZExt: |
3253 | 79 | case ZeroCompare::GESExt: |
3254 | 79 | ToExtend = SDValue(CurDAG->getMachineNode(Is32Bit ? PPC::NOR : PPC::NOR8, |
3255 | 79 | dl, InVT, LHS, LHS), 0); |
3256 | 79 | break; |
3257 | 183 | case ZeroCompare::LEZExt: |
3258 | 184 | case ZeroCompare::LESExt: { |
3259 | 184 | if (Is32Bit) { |
3260 | | // Upper 32 bits cannot be undefined for this sequence. |
3261 | 131 | LHS = signExtendInputIfNeeded(LHS); |
3262 | 131 | SDValue Neg = |
3263 | 131 | SDValue(CurDAG->getMachineNode(PPC::NEG8, dl, MVT::i64, LHS), 0); |
3264 | 131 | ToExtend = |
3265 | 131 | SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, |
3266 | 131 | Neg, S->getI64Imm(1, dl), |
3267 | 131 | S->getI64Imm(63, dl)), 0); |
3268 | 131 | } else { |
3269 | 53 | SDValue Addi = |
3270 | 53 | SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, LHS, |
3271 | 53 | S->getI64Imm(~0ULL, dl)), 0); |
3272 | 53 | ToExtend = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64, |
3273 | 53 | Addi, LHS), 0); |
3274 | 53 | } |
3275 | 184 | break; |
3276 | 183 | } |
3277 | 263 | } |
3278 | | |
3279 | | // For 64-bit sequences, the extensions are the same for the GE/LE cases. |
3280 | 263 | if (!Is32Bit && |
3281 | 263 | (CmpTy == ZeroCompare::GEZExt || CmpTy == ZeroCompare::LEZExt)) |
3282 | 66 | return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, |
3283 | 66 | ToExtend, S->getI64Imm(1, dl), |
3284 | 66 | S->getI64Imm(63, dl)), 0); |
3285 | 197 | if (!Is32Bit && |
3286 | 197 | (CmpTy == ZeroCompare::GESExt || CmpTy == ZeroCompare::LESExt)) |
3287 | 1 | return SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, ToExtend, |
3288 | 1 | S->getI64Imm(63, dl)), 0); |
3289 | | |
3290 | 196 | assert(Is32Bit && "Should have handled the 32-bit sequences above."); |
3291 | | // For 32-bit sequences, the extensions differ between GE/LE cases. |
3292 | 0 | switch (CmpTy) { |
3293 | 64 | case ZeroCompare::GEZExt: { |
3294 | 64 | SDValue ShiftOps[] = { ToExtend, S->getI32Imm(1, dl), S->getI32Imm(31, dl), |
3295 | 64 | S->getI32Imm(31, dl) }; |
3296 | 64 | return SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, |
3297 | 64 | ShiftOps), 0); |
3298 | 0 | } |
3299 | 1 | case ZeroCompare::GESExt: |
3300 | 1 | return SDValue(CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, ToExtend, |
3301 | 1 | S->getI32Imm(31, dl)), 0); |
3302 | 131 | case ZeroCompare::LEZExt: |
3303 | 131 | return SDValue(CurDAG->getMachineNode(PPC::XORI8, dl, MVT::i64, ToExtend, |
3304 | 131 | S->getI32Imm(1, dl)), 0); |
3305 | 0 | case ZeroCompare::LESExt: |
3306 | 0 | return SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, ToExtend, |
3307 | 0 | S->getI32Imm(-1, dl)), 0); |
3308 | 196 | } |
3309 | | |
3310 | | // The above case covers all the enumerators so it can't have a default clause |
3311 | | // to avoid compiler warnings. |
3312 | 0 | llvm_unreachable("Unknown zero-comparison type."); |
3313 | 0 | } |
3314 | | |
3315 | | /// Produces a zero-extended result of comparing two 32-bit values according to |
3316 | | /// the passed condition code. |
3317 | | SDValue |
3318 | | IntegerCompareEliminator::get32BitZExtCompare(SDValue LHS, SDValue RHS, |
3319 | | ISD::CondCode CC, |
3320 | 5.55k | int64_t RHSValue, SDLoc dl) { |
3321 | 5.55k | if (CmpInGPR == ICGPR_I64 || CmpInGPR == ICGPR_SextI64 || |
3322 | 5.55k | CmpInGPR == ICGPR_ZextI64 || CmpInGPR == ICGPR_Sext) |
3323 | 0 | return SDValue(); |
3324 | 5.55k | bool IsRHSZero = RHSValue == 0; |
3325 | 5.55k | bool IsRHSOne = RHSValue == 1; |
3326 | 5.55k | bool IsRHSNegOne = RHSValue == -1LL; |
3327 | 5.55k | switch (CC) { |
3328 | 0 | default: return SDValue(); |
3329 | 2.15k | case ISD::SETEQ: { |
3330 | | // (zext (setcc %a, %b, seteq)) -> (lshr (cntlzw (xor %a, %b)), 5) |
3331 | | // (zext (setcc %a, 0, seteq)) -> (lshr (cntlzw %a), 5) |
3332 | 2.15k | SDValue Xor = IsRHSZero ? LHS : |
3333 | 2.15k | SDValue(CurDAG->getMachineNode(PPC::XOR, dl, MVT::i32, LHS, RHS), 0); |
3334 | 2.15k | SDValue Clz = |
3335 | 2.15k | SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Xor), 0); |
3336 | 2.15k | SDValue ShiftOps[] = { Clz, S->getI32Imm(27, dl), S->getI32Imm(5, dl), |
3337 | 2.15k | S->getI32Imm(31, dl) }; |
3338 | 2.15k | return SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, |
3339 | 2.15k | ShiftOps), 0); |
3340 | 0 | } |
3341 | 735 | case ISD::SETNE: { |
3342 | | // (zext (setcc %a, %b, setne)) -> (xor (lshr (cntlzw (xor %a, %b)), 5), 1) |
3343 | | // (zext (setcc %a, 0, setne)) -> (xor (lshr (cntlzw %a), 5), 1) |
3344 | 735 | SDValue Xor = IsRHSZero ? LHS : |
3345 | 735 | SDValue(CurDAG->getMachineNode(PPC::XOR, dl, MVT::i32, LHS, RHS), 0); |
3346 | 735 | SDValue Clz = |
3347 | 735 | SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Xor), 0); |
3348 | 735 | SDValue ShiftOps[] = { Clz, S->getI32Imm(27, dl), S->getI32Imm(5, dl), |
3349 | 735 | S->getI32Imm(31, dl) }; |
3350 | 735 | SDValue Shift = |
3351 | 735 | SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, ShiftOps), 0); |
3352 | 735 | return SDValue(CurDAG->getMachineNode(PPC::XORI, dl, MVT::i32, Shift, |
3353 | 735 | S->getI32Imm(1, dl)), 0); |
3354 | 0 | } |
3355 | 451 | case ISD::SETGE: { |
3356 | | // (zext (setcc %a, %b, setge)) -> (xor (lshr (sub %a, %b), 63), 1) |
3357 | | // (zext (setcc %a, 0, setge)) -> (lshr (~ %a), 31) |
3358 | 451 | if(IsRHSZero) |
3359 | 0 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GEZExt); |
3360 | | |
3361 | | // Not a special case (i.e. RHS == 0). Handle (%a >= %b) as (%b <= %a) |
3362 | | // by swapping inputs and falling through. |
3363 | 451 | std::swap(LHS, RHS); |
3364 | 451 | ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS); |
3365 | 451 | IsRHSZero = RHSConst && RHSConst->isZero(); |
3366 | 451 | [[fallthrough]]; |
3367 | 451 | } |
3368 | 887 | case ISD::SETLE: { |
3369 | 887 | if (CmpInGPR == ICGPR_NonExtIn) |
3370 | 0 | return SDValue(); |
3371 | | // (zext (setcc %a, %b, setle)) -> (xor (lshr (sub %b, %a), 63), 1) |
3372 | | // (zext (setcc %a, 0, setle)) -> (xor (lshr (- %a), 63), 1) |
3373 | 887 | if(IsRHSZero) { |
3374 | 0 | if (CmpInGPR == ICGPR_NonExtIn) |
3375 | 0 | return SDValue(); |
3376 | 0 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LEZExt); |
3377 | 0 | } |
3378 | | |
3379 | | // The upper 32-bits of the register can't be undefined for this sequence. |
3380 | 887 | LHS = signExtendInputIfNeeded(LHS); |
3381 | 887 | RHS = signExtendInputIfNeeded(RHS); |
3382 | 887 | SDValue Sub = |
3383 | 887 | SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, LHS, RHS), 0); |
3384 | 887 | SDValue Shift = |
3385 | 887 | SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Sub, |
3386 | 887 | S->getI64Imm(1, dl), S->getI64Imm(63, dl)), |
3387 | 887 | 0); |
3388 | 887 | return |
3389 | 887 | SDValue(CurDAG->getMachineNode(PPC::XORI8, dl, |
3390 | 887 | MVT::i64, Shift, S->getI32Imm(1, dl)), 0); |
3391 | 887 | } |
3392 | 779 | case ISD::SETGT: { |
3393 | | // (zext (setcc %a, %b, setgt)) -> (lshr (sub %b, %a), 63) |
3394 | | // (zext (setcc %a, -1, setgt)) -> (lshr (~ %a), 31) |
3395 | | // (zext (setcc %a, 0, setgt)) -> (lshr (- %a), 63) |
3396 | | // Handle SETLT -1 (which is equivalent to SETGE 0). |
3397 | 779 | if (IsRHSNegOne) |
3398 | 64 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GEZExt); |
3399 | | |
3400 | 715 | if (IsRHSZero) { |
3401 | 136 | if (CmpInGPR == ICGPR_NonExtIn) |
3402 | 0 | return SDValue(); |
3403 | | // The upper 32-bits of the register can't be undefined for this sequence. |
3404 | 136 | LHS = signExtendInputIfNeeded(LHS); |
3405 | 136 | RHS = signExtendInputIfNeeded(RHS); |
3406 | 136 | SDValue Neg = |
3407 | 136 | SDValue(CurDAG->getMachineNode(PPC::NEG8, dl, MVT::i64, LHS), 0); |
3408 | 136 | return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, |
3409 | 136 | Neg, S->getI32Imm(1, dl), S->getI32Imm(63, dl)), 0); |
3410 | 136 | } |
3411 | | // Not a special case (i.e. RHS == 0 or RHS == -1). Handle (%a > %b) as |
3412 | | // (%b < %a) by swapping inputs and falling through. |
3413 | 579 | std::swap(LHS, RHS); |
3414 | 579 | ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS); |
3415 | 579 | IsRHSZero = RHSConst && RHSConst->isZero(); |
3416 | 579 | IsRHSOne = RHSConst && RHSConst->getSExtValue() == 1; |
3417 | 579 | [[fallthrough]]; |
3418 | 579 | } |
3419 | 1.43k | case ISD::SETLT: { |
3420 | | // (zext (setcc %a, %b, setlt)) -> (lshr (sub %a, %b), 63) |
3421 | | // (zext (setcc %a, 1, setlt)) -> (xor (lshr (- %a), 63), 1) |
3422 | | // (zext (setcc %a, 0, setlt)) -> (lshr %a, 31) |
3423 | | // Handle SETLT 1 (which is equivalent to SETLE 0). |
3424 | 1.43k | if (IsRHSOne) { |
3425 | 131 | if (CmpInGPR == ICGPR_NonExtIn) |
3426 | 0 | return SDValue(); |
3427 | 131 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LEZExt); |
3428 | 131 | } |
3429 | | |
3430 | 1.30k | if (IsRHSZero) { |
3431 | 32 | SDValue ShiftOps[] = { LHS, S->getI32Imm(1, dl), S->getI32Imm(31, dl), |
3432 | 32 | S->getI32Imm(31, dl) }; |
3433 | 32 | return SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, |
3434 | 32 | ShiftOps), 0); |
3435 | 32 | } |
3436 | | |
3437 | 1.27k | if (CmpInGPR == ICGPR_NonExtIn) |
3438 | 0 | return SDValue(); |
3439 | | // The upper 32-bits of the register can't be undefined for this sequence. |
3440 | 1.27k | LHS = signExtendInputIfNeeded(LHS); |
3441 | 1.27k | RHS = signExtendInputIfNeeded(RHS); |
3442 | 1.27k | SDValue SUBFNode = |
3443 | 1.27k | SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, RHS, LHS), 0); |
3444 | 1.27k | return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, |
3445 | 1.27k | SUBFNode, S->getI64Imm(1, dl), |
3446 | 1.27k | S->getI64Imm(63, dl)), 0); |
3447 | 1.27k | } |
3448 | 29 | case ISD::SETUGE: |
3449 | | // (zext (setcc %a, %b, setuge)) -> (xor (lshr (sub %b, %a), 63), 1) |
3450 | | // (zext (setcc %a, %b, setule)) -> (xor (lshr (sub %a, %b), 63), 1) |
3451 | 29 | std::swap(LHS, RHS); |
3452 | 29 | [[fallthrough]]; |
3453 | 62 | case ISD::SETULE: { |
3454 | 62 | if (CmpInGPR == ICGPR_NonExtIn) |
3455 | 0 | return SDValue(); |
3456 | | // The upper 32-bits of the register can't be undefined for this sequence. |
3457 | 62 | LHS = zeroExtendInputIfNeeded(LHS); |
3458 | 62 | RHS = zeroExtendInputIfNeeded(RHS); |
3459 | 62 | SDValue Subtract = |
3460 | 62 | SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, LHS, RHS), 0); |
3461 | 62 | SDValue SrdiNode = |
3462 | 62 | SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, |
3463 | 62 | Subtract, S->getI64Imm(1, dl), |
3464 | 62 | S->getI64Imm(63, dl)), 0); |
3465 | 62 | return SDValue(CurDAG->getMachineNode(PPC::XORI8, dl, MVT::i64, SrdiNode, |
3466 | 62 | S->getI32Imm(1, dl)), 0); |
3467 | 62 | } |
3468 | 32 | case ISD::SETUGT: |
3469 | | // (zext (setcc %a, %b, setugt)) -> (lshr (sub %b, %a), 63) |
3470 | | // (zext (setcc %a, %b, setult)) -> (lshr (sub %a, %b), 63) |
3471 | 32 | std::swap(LHS, RHS); |
3472 | 32 | [[fallthrough]]; |
3473 | 81 | case ISD::SETULT: { |
3474 | 81 | if (CmpInGPR == ICGPR_NonExtIn) |
3475 | 0 | return SDValue(); |
3476 | | // The upper 32-bits of the register can't be undefined for this sequence. |
3477 | 81 | LHS = zeroExtendInputIfNeeded(LHS); |
3478 | 81 | RHS = zeroExtendInputIfNeeded(RHS); |
3479 | 81 | SDValue Subtract = |
3480 | 81 | SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, RHS, LHS), 0); |
3481 | 81 | return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, |
3482 | 81 | Subtract, S->getI64Imm(1, dl), |
3483 | 81 | S->getI64Imm(63, dl)), 0); |
3484 | 81 | } |
3485 | 5.55k | } |
3486 | 5.55k | } |
3487 | | |
3488 | | /// Produces a sign-extended result of comparing two 32-bit values according to |
3489 | | /// the passed condition code. |
3490 | | SDValue |
3491 | | IntegerCompareEliminator::get32BitSExtCompare(SDValue LHS, SDValue RHS, |
3492 | | ISD::CondCode CC, |
3493 | 19 | int64_t RHSValue, SDLoc dl) { |
3494 | 19 | if (CmpInGPR == ICGPR_I64 || CmpInGPR == ICGPR_SextI64 || |
3495 | 19 | CmpInGPR == ICGPR_ZextI64 || CmpInGPR == ICGPR_Zext) |
3496 | 0 | return SDValue(); |
3497 | 19 | bool IsRHSZero = RHSValue == 0; |
3498 | 19 | bool IsRHSOne = RHSValue == 1; |
3499 | 19 | bool IsRHSNegOne = RHSValue == -1LL; |
3500 | | |
3501 | 19 | switch (CC) { |
3502 | 0 | default: return SDValue(); |
3503 | 8 | case ISD::SETEQ: { |
3504 | | // (sext (setcc %a, %b, seteq)) -> |
3505 | | // (ashr (shl (ctlz (xor %a, %b)), 58), 63) |
3506 | | // (sext (setcc %a, 0, seteq)) -> |
3507 | | // (ashr (shl (ctlz %a), 58), 63) |
3508 | 8 | SDValue CountInput = IsRHSZero ? LHS : |
3509 | 8 | SDValue(CurDAG->getMachineNode(PPC::XOR, dl, MVT::i32, LHS, RHS), 0); |
3510 | 8 | SDValue Cntlzw = |
3511 | 8 | SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, CountInput), 0); |
3512 | 8 | SDValue SHLOps[] = { Cntlzw, S->getI32Imm(27, dl), |
3513 | 8 | S->getI32Imm(5, dl), S->getI32Imm(31, dl) }; |
3514 | 8 | SDValue Slwi = |
3515 | 8 | SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, SHLOps), 0); |
3516 | 8 | return SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Slwi), 0); |
3517 | 0 | } |
3518 | 1 | case ISD::SETNE: { |
3519 | | // Bitwise xor the operands, count leading zeros, shift right by 5 bits and |
3520 | | // flip the bit, finally take 2's complement. |
3521 | | // (sext (setcc %a, %b, setne)) -> |
3522 | | // (neg (xor (lshr (ctlz (xor %a, %b)), 5), 1)) |
3523 | | // Same as above, but the first xor is not needed. |
3524 | | // (sext (setcc %a, 0, setne)) -> |
3525 | | // (neg (xor (lshr (ctlz %a), 5), 1)) |
3526 | 1 | SDValue Xor = IsRHSZero ? LHS : |
3527 | 1 | SDValue(CurDAG->getMachineNode(PPC::XOR, dl, MVT::i32, LHS, RHS), 0); |
3528 | 1 | SDValue Clz = |
3529 | 1 | SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Xor), 0); |
3530 | 1 | SDValue ShiftOps[] = |
3531 | 1 | { Clz, S->getI32Imm(27, dl), S->getI32Imm(5, dl), S->getI32Imm(31, dl) }; |
3532 | 1 | SDValue Shift = |
3533 | 1 | SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, ShiftOps), 0); |
3534 | 1 | SDValue Xori = |
3535 | 1 | SDValue(CurDAG->getMachineNode(PPC::XORI, dl, MVT::i32, Shift, |
3536 | 1 | S->getI32Imm(1, dl)), 0); |
3537 | 1 | return SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Xori), 0); |
3538 | 0 | } |
3539 | 0 | case ISD::SETGE: { |
3540 | | // (sext (setcc %a, %b, setge)) -> (add (lshr (sub %a, %b), 63), -1) |
3541 | | // (sext (setcc %a, 0, setge)) -> (ashr (~ %a), 31) |
3542 | 0 | if (IsRHSZero) |
3543 | 0 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GESExt); |
3544 | | |
3545 | | // Not a special case (i.e. RHS == 0). Handle (%a >= %b) as (%b <= %a) |
3546 | | // by swapping inputs and falling through. |
3547 | 0 | std::swap(LHS, RHS); |
3548 | 0 | ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS); |
3549 | 0 | IsRHSZero = RHSConst && RHSConst->isZero(); |
3550 | 0 | [[fallthrough]]; |
3551 | 0 | } |
3552 | 0 | case ISD::SETLE: { |
3553 | 0 | if (CmpInGPR == ICGPR_NonExtIn) |
3554 | 0 | return SDValue(); |
3555 | | // (sext (setcc %a, %b, setge)) -> (add (lshr (sub %b, %a), 63), -1) |
3556 | | // (sext (setcc %a, 0, setle)) -> (add (lshr (- %a), 63), -1) |
3557 | 0 | if (IsRHSZero) |
3558 | 0 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LESExt); |
3559 | | |
3560 | | // The upper 32-bits of the register can't be undefined for this sequence. |
3561 | 0 | LHS = signExtendInputIfNeeded(LHS); |
3562 | 0 | RHS = signExtendInputIfNeeded(RHS); |
3563 | 0 | SDValue SUBFNode = |
3564 | 0 | SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, MVT::Glue, |
3565 | 0 | LHS, RHS), 0); |
3566 | 0 | SDValue Srdi = |
3567 | 0 | SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, |
3568 | 0 | SUBFNode, S->getI64Imm(1, dl), |
3569 | 0 | S->getI64Imm(63, dl)), 0); |
3570 | 0 | return SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, Srdi, |
3571 | 0 | S->getI32Imm(-1, dl)), 0); |
3572 | 0 | } |
3573 | 2 | case ISD::SETGT: { |
3574 | | // (sext (setcc %a, %b, setgt)) -> (ashr (sub %b, %a), 63) |
3575 | | // (sext (setcc %a, -1, setgt)) -> (ashr (~ %a), 31) |
3576 | | // (sext (setcc %a, 0, setgt)) -> (ashr (- %a), 63) |
3577 | 2 | if (IsRHSNegOne) |
3578 | 1 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GESExt); |
3579 | 1 | if (IsRHSZero) { |
3580 | 1 | if (CmpInGPR == ICGPR_NonExtIn) |
3581 | 0 | return SDValue(); |
3582 | | // The upper 32-bits of the register can't be undefined for this sequence. |
3583 | 1 | LHS = signExtendInputIfNeeded(LHS); |
3584 | 1 | RHS = signExtendInputIfNeeded(RHS); |
3585 | 1 | SDValue Neg = |
3586 | 1 | SDValue(CurDAG->getMachineNode(PPC::NEG8, dl, MVT::i64, LHS), 0); |
3587 | 1 | return SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, Neg, |
3588 | 1 | S->getI64Imm(63, dl)), 0); |
3589 | 1 | } |
3590 | | // Not a special case (i.e. RHS == 0 or RHS == -1). Handle (%a > %b) as |
3591 | | // (%b < %a) by swapping inputs and falling through. |
3592 | 0 | std::swap(LHS, RHS); |
3593 | 0 | ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS); |
3594 | 0 | IsRHSZero = RHSConst && RHSConst->isZero(); |
3595 | 0 | IsRHSOne = RHSConst && RHSConst->getSExtValue() == 1; |
3596 | 0 | [[fallthrough]]; |
3597 | 0 | } |
3598 | 6 | case ISD::SETLT: { |
3599 | | // (sext (setcc %a, %b, setgt)) -> (ashr (sub %a, %b), 63) |
3600 | | // (sext (setcc %a, 1, setgt)) -> (add (lshr (- %a), 63), -1) |
3601 | | // (sext (setcc %a, 0, setgt)) -> (ashr %a, 31) |
3602 | 6 | if (IsRHSOne) { |
3603 | 0 | if (CmpInGPR == ICGPR_NonExtIn) |
3604 | 0 | return SDValue(); |
3605 | 0 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LESExt); |
3606 | 0 | } |
3607 | 6 | if (IsRHSZero) |
3608 | 2 | return SDValue(CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, LHS, |
3609 | 2 | S->getI32Imm(31, dl)), 0); |
3610 | | |
3611 | 4 | if (CmpInGPR == ICGPR_NonExtIn) |
3612 | 0 | return SDValue(); |
3613 | | // The upper 32-bits of the register can't be undefined for this sequence. |
3614 | 4 | LHS = signExtendInputIfNeeded(LHS); |
3615 | 4 | RHS = signExtendInputIfNeeded(RHS); |
3616 | 4 | SDValue SUBFNode = |
3617 | 4 | SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, RHS, LHS), 0); |
3618 | 4 | return SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, |
3619 | 4 | SUBFNode, S->getI64Imm(63, dl)), 0); |
3620 | 4 | } |
3621 | 0 | case ISD::SETUGE: |
3622 | | // (sext (setcc %a, %b, setuge)) -> (add (lshr (sub %a, %b), 63), -1) |
3623 | | // (sext (setcc %a, %b, setule)) -> (add (lshr (sub %b, %a), 63), -1) |
3624 | 0 | std::swap(LHS, RHS); |
3625 | 0 | [[fallthrough]]; |
3626 | 0 | case ISD::SETULE: { |
3627 | 0 | if (CmpInGPR == ICGPR_NonExtIn) |
3628 | 0 | return SDValue(); |
3629 | | // The upper 32-bits of the register can't be undefined for this sequence. |
3630 | 0 | LHS = zeroExtendInputIfNeeded(LHS); |
3631 | 0 | RHS = zeroExtendInputIfNeeded(RHS); |
3632 | 0 | SDValue Subtract = |
3633 | 0 | SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, LHS, RHS), 0); |
3634 | 0 | SDValue Shift = |
3635 | 0 | SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Subtract, |
3636 | 0 | S->getI32Imm(1, dl), S->getI32Imm(63,dl)), |
3637 | 0 | 0); |
3638 | 0 | return SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, Shift, |
3639 | 0 | S->getI32Imm(-1, dl)), 0); |
3640 | 0 | } |
3641 | 2 | case ISD::SETUGT: |
3642 | | // (sext (setcc %a, %b, setugt)) -> (ashr (sub %b, %a), 63) |
3643 | | // (sext (setcc %a, %b, setugt)) -> (ashr (sub %a, %b), 63) |
3644 | 2 | std::swap(LHS, RHS); |
3645 | 2 | [[fallthrough]]; |
3646 | 2 | case ISD::SETULT: { |
3647 | 2 | if (CmpInGPR == ICGPR_NonExtIn) |
3648 | 0 | return SDValue(); |
3649 | | // The upper 32-bits of the register can't be undefined for this sequence. |
3650 | 2 | LHS = zeroExtendInputIfNeeded(LHS); |
3651 | 2 | RHS = zeroExtendInputIfNeeded(RHS); |
3652 | 2 | SDValue Subtract = |
3653 | 2 | SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, RHS, LHS), 0); |
3654 | 2 | return SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, |
3655 | 2 | Subtract, S->getI64Imm(63, dl)), 0); |
3656 | 2 | } |
3657 | 19 | } |
3658 | 19 | } |
3659 | | |
3660 | | /// Produces a zero-extended result of comparing two 64-bit values according to |
3661 | | /// the passed condition code. |
3662 | | SDValue |
3663 | | IntegerCompareEliminator::get64BitZExtCompare(SDValue LHS, SDValue RHS, |
3664 | | ISD::CondCode CC, |
3665 | 2.80k | int64_t RHSValue, SDLoc dl) { |
3666 | 2.80k | if (CmpInGPR == ICGPR_I32 || CmpInGPR == ICGPR_SextI32 || |
3667 | 2.80k | CmpInGPR == ICGPR_ZextI32 || CmpInGPR == ICGPR_Sext) |
3668 | 0 | return SDValue(); |
3669 | 2.80k | bool IsRHSZero = RHSValue == 0; |
3670 | 2.80k | bool IsRHSOne = RHSValue == 1; |
3671 | 2.80k | bool IsRHSNegOne = RHSValue == -1LL; |
3672 | 2.80k | switch (CC) { |
3673 | 0 | default: return SDValue(); |
3674 | 738 | case ISD::SETEQ: { |
3675 | | // (zext (setcc %a, %b, seteq)) -> (lshr (ctlz (xor %a, %b)), 6) |
3676 | | // (zext (setcc %a, 0, seteq)) -> (lshr (ctlz %a), 6) |
3677 | 738 | SDValue Xor = IsRHSZero ? LHS : |
3678 | 738 | SDValue(CurDAG->getMachineNode(PPC::XOR8, dl, MVT::i64, LHS, RHS), 0); |
3679 | 738 | SDValue Clz = |
3680 | 738 | SDValue(CurDAG->getMachineNode(PPC::CNTLZD, dl, MVT::i64, Xor), 0); |
3681 | 738 | return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Clz, |
3682 | 738 | S->getI64Imm(58, dl), |
3683 | 738 | S->getI64Imm(63, dl)), 0); |
3684 | 0 | } |
3685 | 533 | case ISD::SETNE: { |
3686 | | // {addc.reg, addc.CA} = (addcarry (xor %a, %b), -1) |
3687 | | // (zext (setcc %a, %b, setne)) -> (sube addc.reg, addc.reg, addc.CA) |
3688 | | // {addcz.reg, addcz.CA} = (addcarry %a, -1) |
3689 | | // (zext (setcc %a, 0, setne)) -> (sube addcz.reg, addcz.reg, addcz.CA) |
3690 | 533 | SDValue Xor = IsRHSZero ? LHS : |
3691 | 533 | SDValue(CurDAG->getMachineNode(PPC::XOR8, dl, MVT::i64, LHS, RHS), 0); |
3692 | 533 | SDValue AC = |
3693 | 533 | SDValue(CurDAG->getMachineNode(PPC::ADDIC8, dl, MVT::i64, MVT::Glue, |
3694 | 533 | Xor, S->getI32Imm(~0U, dl)), 0); |
3695 | 533 | return SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, AC, |
3696 | 533 | Xor, AC.getValue(1)), 0); |
3697 | 0 | } |
3698 | 85 | case ISD::SETGE: { |
3699 | | // {subc.reg, subc.CA} = (subcarry %a, %b) |
3700 | | // (zext (setcc %a, %b, setge)) -> |
3701 | | // (adde (lshr %b, 63), (ashr %a, 63), subc.CA) |
3702 | | // (zext (setcc %a, 0, setge)) -> (lshr (~ %a), 63) |
3703 | 85 | if (IsRHSZero) |
3704 | 0 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GEZExt); |
3705 | 85 | std::swap(LHS, RHS); |
3706 | 85 | ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS); |
3707 | 85 | IsRHSZero = RHSConst && RHSConst->isZero(); |
3708 | 85 | [[fallthrough]]; |
3709 | 85 | } |
3710 | 152 | case ISD::SETLE: { |
3711 | | // {subc.reg, subc.CA} = (subcarry %b, %a) |
3712 | | // (zext (setcc %a, %b, setge)) -> |
3713 | | // (adde (lshr %a, 63), (ashr %b, 63), subc.CA) |
3714 | | // (zext (setcc %a, 0, setge)) -> (lshr (or %a, (add %a, -1)), 63) |
3715 | 152 | if (IsRHSZero) |
3716 | 0 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LEZExt); |
3717 | 152 | SDValue ShiftL = |
3718 | 152 | SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, LHS, |
3719 | 152 | S->getI64Imm(1, dl), |
3720 | 152 | S->getI64Imm(63, dl)), 0); |
3721 | 152 | SDValue ShiftR = |
3722 | 152 | SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, RHS, |
3723 | 152 | S->getI64Imm(63, dl)), 0); |
3724 | 152 | SDValue SubtractCarry = |
3725 | 152 | SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue, |
3726 | 152 | LHS, RHS), 1); |
3727 | 152 | return SDValue(CurDAG->getMachineNode(PPC::ADDE8, dl, MVT::i64, MVT::Glue, |
3728 | 152 | ShiftR, ShiftL, SubtractCarry), 0); |
3729 | 152 | } |
3730 | 287 | case ISD::SETGT: { |
3731 | | // {subc.reg, subc.CA} = (subcarry %b, %a) |
3732 | | // (zext (setcc %a, %b, setgt)) -> |
3733 | | // (xor (adde (lshr %a, 63), (ashr %b, 63), subc.CA), 1) |
3734 | | // (zext (setcc %a, 0, setgt)) -> (lshr (nor (add %a, -1), %a), 63) |
3735 | 287 | if (IsRHSNegOne) |
3736 | 14 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GEZExt); |
3737 | 273 | if (IsRHSZero) { |
3738 | 53 | SDValue Addi = |
3739 | 53 | SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, LHS, |
3740 | 53 | S->getI64Imm(~0ULL, dl)), 0); |
3741 | 53 | SDValue Nor = |
3742 | 53 | SDValue(CurDAG->getMachineNode(PPC::NOR8, dl, MVT::i64, Addi, LHS), 0); |
3743 | 53 | return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Nor, |
3744 | 53 | S->getI64Imm(1, dl), |
3745 | 53 | S->getI64Imm(63, dl)), 0); |
3746 | 53 | } |
3747 | 220 | std::swap(LHS, RHS); |
3748 | 220 | ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS); |
3749 | 220 | IsRHSZero = RHSConst && RHSConst->isZero(); |
3750 | 220 | IsRHSOne = RHSConst && RHSConst->getSExtValue() == 1; |
3751 | 220 | [[fallthrough]]; |
3752 | 220 | } |
3753 | 547 | case ISD::SETLT: { |
3754 | | // {subc.reg, subc.CA} = (subcarry %a, %b) |
3755 | | // (zext (setcc %a, %b, setlt)) -> |
3756 | | // (xor (adde (lshr %b, 63), (ashr %a, 63), subc.CA), 1) |
3757 | | // (zext (setcc %a, 0, setlt)) -> (lshr %a, 63) |
3758 | 547 | if (IsRHSOne) |
3759 | 52 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LEZExt); |
3760 | 495 | if (IsRHSZero) |
3761 | 14 | return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, LHS, |
3762 | 14 | S->getI64Imm(1, dl), |
3763 | 14 | S->getI64Imm(63, dl)), 0); |
3764 | 481 | SDValue SRADINode = |
3765 | 481 | SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, |
3766 | 481 | LHS, S->getI64Imm(63, dl)), 0); |
3767 | 481 | SDValue SRDINode = |
3768 | 481 | SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, |
3769 | 481 | RHS, S->getI64Imm(1, dl), |
3770 | 481 | S->getI64Imm(63, dl)), 0); |
3771 | 481 | SDValue SUBFC8Carry = |
3772 | 481 | SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue, |
3773 | 481 | RHS, LHS), 1); |
3774 | 481 | SDValue ADDE8Node = |
3775 | 481 | SDValue(CurDAG->getMachineNode(PPC::ADDE8, dl, MVT::i64, MVT::Glue, |
3776 | 481 | SRDINode, SRADINode, SUBFC8Carry), 0); |
3777 | 481 | return SDValue(CurDAG->getMachineNode(PPC::XORI8, dl, MVT::i64, |
3778 | 481 | ADDE8Node, S->getI64Imm(1, dl)), 0); |
3779 | 495 | } |
3780 | 56 | case ISD::SETUGE: |
3781 | | // {subc.reg, subc.CA} = (subcarry %a, %b) |
3782 | | // (zext (setcc %a, %b, setuge)) -> (add (sube %b, %b, subc.CA), 1) |
3783 | 56 | std::swap(LHS, RHS); |
3784 | 56 | [[fallthrough]]; |
3785 | 175 | case ISD::SETULE: { |
3786 | | // {subc.reg, subc.CA} = (subcarry %b, %a) |
3787 | | // (zext (setcc %a, %b, setule)) -> (add (sube %a, %a, subc.CA), 1) |
3788 | 175 | SDValue SUBFC8Carry = |
3789 | 175 | SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue, |
3790 | 175 | LHS, RHS), 1); |
3791 | 175 | SDValue SUBFE8Node = |
3792 | 175 | SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, MVT::Glue, |
3793 | 175 | LHS, LHS, SUBFC8Carry), 0); |
3794 | 175 | return SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, |
3795 | 175 | SUBFE8Node, S->getI64Imm(1, dl)), 0); |
3796 | 56 | } |
3797 | 390 | case ISD::SETUGT: |
3798 | | // {subc.reg, subc.CA} = (subcarry %b, %a) |
3799 | | // (zext (setcc %a, %b, setugt)) -> -(sube %b, %b, subc.CA) |
3800 | 390 | std::swap(LHS, RHS); |
3801 | 390 | [[fallthrough]]; |
3802 | 596 | case ISD::SETULT: { |
3803 | | // {subc.reg, subc.CA} = (subcarry %a, %b) |
3804 | | // (zext (setcc %a, %b, setult)) -> -(sube %a, %a, subc.CA) |
3805 | 596 | SDValue SubtractCarry = |
3806 | 596 | SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue, |
3807 | 596 | RHS, LHS), 1); |
3808 | 596 | SDValue ExtSub = |
3809 | 596 | SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, |
3810 | 596 | LHS, LHS, SubtractCarry), 0); |
3811 | 596 | return SDValue(CurDAG->getMachineNode(PPC::NEG8, dl, MVT::i64, |
3812 | 596 | ExtSub), 0); |
3813 | 390 | } |
3814 | 2.80k | } |
3815 | 2.80k | } |
3816 | | |
3817 | | /// Produces a sign-extended result of comparing two 64-bit values according to |
3818 | | /// the passed condition code. |
3819 | | SDValue |
3820 | | IntegerCompareEliminator::get64BitSExtCompare(SDValue LHS, SDValue RHS, |
3821 | | ISD::CondCode CC, |
3822 | 18 | int64_t RHSValue, SDLoc dl) { |
3823 | 18 | if (CmpInGPR == ICGPR_I32 || CmpInGPR == ICGPR_SextI32 || |
3824 | 18 | CmpInGPR == ICGPR_ZextI32 || CmpInGPR == ICGPR_Zext) |
3825 | 0 | return SDValue(); |
3826 | 18 | bool IsRHSZero = RHSValue == 0; |
3827 | 18 | bool IsRHSOne = RHSValue == 1; |
3828 | 18 | bool IsRHSNegOne = RHSValue == -1LL; |
3829 | 18 | switch (CC) { |
3830 | 0 | default: return SDValue(); |
3831 | 2 | case ISD::SETEQ: { |
3832 | | // {addc.reg, addc.CA} = (addcarry (xor %a, %b), -1) |
3833 | | // (sext (setcc %a, %b, seteq)) -> (sube addc.reg, addc.reg, addc.CA) |
3834 | | // {addcz.reg, addcz.CA} = (addcarry %a, -1) |
3835 | | // (sext (setcc %a, 0, seteq)) -> (sube addcz.reg, addcz.reg, addcz.CA) |
3836 | 2 | SDValue AddInput = IsRHSZero ? LHS : |
3837 | 2 | SDValue(CurDAG->getMachineNode(PPC::XOR8, dl, MVT::i64, LHS, RHS), 0); |
3838 | 2 | SDValue Addic = |
3839 | 2 | SDValue(CurDAG->getMachineNode(PPC::ADDIC8, dl, MVT::i64, MVT::Glue, |
3840 | 2 | AddInput, S->getI32Imm(~0U, dl)), 0); |
3841 | 2 | return SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, Addic, |
3842 | 2 | Addic, Addic.getValue(1)), 0); |
3843 | 0 | } |
3844 | 6 | case ISD::SETNE: { |
3845 | | // {subfc.reg, subfc.CA} = (subcarry 0, (xor %a, %b)) |
3846 | | // (sext (setcc %a, %b, setne)) -> (sube subfc.reg, subfc.reg, subfc.CA) |
3847 | | // {subfcz.reg, subfcz.CA} = (subcarry 0, %a) |
3848 | | // (sext (setcc %a, 0, setne)) -> (sube subfcz.reg, subfcz.reg, subfcz.CA) |
3849 | 6 | SDValue Xor = IsRHSZero ? LHS : |
3850 | 6 | SDValue(CurDAG->getMachineNode(PPC::XOR8, dl, MVT::i64, LHS, RHS), 0); |
3851 | 6 | SDValue SC = |
3852 | 6 | SDValue(CurDAG->getMachineNode(PPC::SUBFIC8, dl, MVT::i64, MVT::Glue, |
3853 | 6 | Xor, S->getI32Imm(0, dl)), 0); |
3854 | 6 | return SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, SC, |
3855 | 6 | SC, SC.getValue(1)), 0); |
3856 | 0 | } |
3857 | 1 | case ISD::SETGE: { |
3858 | | // {subc.reg, subc.CA} = (subcarry %a, %b) |
3859 | | // (zext (setcc %a, %b, setge)) -> |
3860 | | // (- (adde (lshr %b, 63), (ashr %a, 63), subc.CA)) |
3861 | | // (zext (setcc %a, 0, setge)) -> (~ (ashr %a, 63)) |
3862 | 1 | if (IsRHSZero) |
3863 | 0 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GESExt); |
3864 | 1 | std::swap(LHS, RHS); |
3865 | 1 | ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS); |
3866 | 1 | IsRHSZero = RHSConst && RHSConst->isZero(); |
3867 | 1 | [[fallthrough]]; |
3868 | 1 | } |
3869 | 1 | case ISD::SETLE: { |
3870 | | // {subc.reg, subc.CA} = (subcarry %b, %a) |
3871 | | // (zext (setcc %a, %b, setge)) -> |
3872 | | // (- (adde (lshr %a, 63), (ashr %b, 63), subc.CA)) |
3873 | | // (zext (setcc %a, 0, setge)) -> (ashr (or %a, (add %a, -1)), 63) |
3874 | 1 | if (IsRHSZero) |
3875 | 0 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LESExt); |
3876 | 1 | SDValue ShiftR = |
3877 | 1 | SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, RHS, |
3878 | 1 | S->getI64Imm(63, dl)), 0); |
3879 | 1 | SDValue ShiftL = |
3880 | 1 | SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, LHS, |
3881 | 1 | S->getI64Imm(1, dl), |
3882 | 1 | S->getI64Imm(63, dl)), 0); |
3883 | 1 | SDValue SubtractCarry = |
3884 | 1 | SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue, |
3885 | 1 | LHS, RHS), 1); |
3886 | 1 | SDValue Adde = |
3887 | 1 | SDValue(CurDAG->getMachineNode(PPC::ADDE8, dl, MVT::i64, MVT::Glue, |
3888 | 1 | ShiftR, ShiftL, SubtractCarry), 0); |
3889 | 1 | return SDValue(CurDAG->getMachineNode(PPC::NEG8, dl, MVT::i64, Adde), 0); |
3890 | 1 | } |
3891 | 0 | case ISD::SETGT: { |
3892 | | // {subc.reg, subc.CA} = (subcarry %b, %a) |
3893 | | // (zext (setcc %a, %b, setgt)) -> |
3894 | | // -(xor (adde (lshr %a, 63), (ashr %b, 63), subc.CA), 1) |
3895 | | // (zext (setcc %a, 0, setgt)) -> (ashr (nor (add %a, -1), %a), 63) |
3896 | 0 | if (IsRHSNegOne) |
3897 | 0 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GESExt); |
3898 | 0 | if (IsRHSZero) { |
3899 | 0 | SDValue Add = |
3900 | 0 | SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, LHS, |
3901 | 0 | S->getI64Imm(-1, dl)), 0); |
3902 | 0 | SDValue Nor = |
3903 | 0 | SDValue(CurDAG->getMachineNode(PPC::NOR8, dl, MVT::i64, Add, LHS), 0); |
3904 | 0 | return SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, Nor, |
3905 | 0 | S->getI64Imm(63, dl)), 0); |
3906 | 0 | } |
3907 | 0 | std::swap(LHS, RHS); |
3908 | 0 | ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS); |
3909 | 0 | IsRHSZero = RHSConst && RHSConst->isZero(); |
3910 | 0 | IsRHSOne = RHSConst && RHSConst->getSExtValue() == 1; |
3911 | 0 | [[fallthrough]]; |
3912 | 0 | } |
3913 | 9 | case ISD::SETLT: { |
3914 | | // {subc.reg, subc.CA} = (subcarry %a, %b) |
3915 | | // (zext (setcc %a, %b, setlt)) -> |
3916 | | // -(xor (adde (lshr %b, 63), (ashr %a, 63), subc.CA), 1) |
3917 | | // (zext (setcc %a, 0, setlt)) -> (ashr %a, 63) |
3918 | 9 | if (IsRHSOne) |
3919 | 1 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LESExt); |
3920 | 8 | if (IsRHSZero) { |
3921 | 0 | return SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, LHS, |
3922 | 0 | S->getI64Imm(63, dl)), 0); |
3923 | 0 | } |
3924 | 8 | SDValue SRADINode = |
3925 | 8 | SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, |
3926 | 8 | LHS, S->getI64Imm(63, dl)), 0); |
3927 | 8 | SDValue SRDINode = |
3928 | 8 | SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, |
3929 | 8 | RHS, S->getI64Imm(1, dl), |
3930 | 8 | S->getI64Imm(63, dl)), 0); |
3931 | 8 | SDValue SUBFC8Carry = |
3932 | 8 | SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue, |
3933 | 8 | RHS, LHS), 1); |
3934 | 8 | SDValue ADDE8Node = |
3935 | 8 | SDValue(CurDAG->getMachineNode(PPC::ADDE8, dl, MVT::i64, |
3936 | 8 | SRDINode, SRADINode, SUBFC8Carry), 0); |
3937 | 8 | SDValue XORI8Node = |
3938 | 8 | SDValue(CurDAG->getMachineNode(PPC::XORI8, dl, MVT::i64, |
3939 | 8 | ADDE8Node, S->getI64Imm(1, dl)), 0); |
3940 | 8 | return SDValue(CurDAG->getMachineNode(PPC::NEG8, dl, MVT::i64, |
3941 | 8 | XORI8Node), 0); |
3942 | 8 | } |
3943 | 0 | case ISD::SETUGE: |
3944 | | // {subc.reg, subc.CA} = (subcarry %a, %b) |
3945 | | // (sext (setcc %a, %b, setuge)) -> ~(sube %b, %b, subc.CA) |
3946 | 0 | std::swap(LHS, RHS); |
3947 | 0 | [[fallthrough]]; |
3948 | 0 | case ISD::SETULE: { |
3949 | | // {subc.reg, subc.CA} = (subcarry %b, %a) |
3950 | | // (sext (setcc %a, %b, setule)) -> ~(sube %a, %a, subc.CA) |
3951 | 0 | SDValue SubtractCarry = |
3952 | 0 | SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue, |
3953 | 0 | LHS, RHS), 1); |
3954 | 0 | SDValue ExtSub = |
3955 | 0 | SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, MVT::Glue, LHS, |
3956 | 0 | LHS, SubtractCarry), 0); |
3957 | 0 | return SDValue(CurDAG->getMachineNode(PPC::NOR8, dl, MVT::i64, |
3958 | 0 | ExtSub, ExtSub), 0); |
3959 | 0 | } |
3960 | 0 | case ISD::SETUGT: |
3961 | | // {subc.reg, subc.CA} = (subcarry %b, %a) |
3962 | | // (sext (setcc %a, %b, setugt)) -> (sube %b, %b, subc.CA) |
3963 | 0 | std::swap(LHS, RHS); |
3964 | 0 | [[fallthrough]]; |
3965 | 0 | case ISD::SETULT: { |
3966 | | // {subc.reg, subc.CA} = (subcarry %a, %b) |
3967 | | // (sext (setcc %a, %b, setult)) -> (sube %a, %a, subc.CA) |
3968 | 0 | SDValue SubCarry = |
3969 | 0 | SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue, |
3970 | 0 | RHS, LHS), 1); |
3971 | 0 | return SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, |
3972 | 0 | LHS, LHS, SubCarry), 0); |
3973 | 0 | } |
3974 | 18 | } |
3975 | 18 | } |
3976 | | |
3977 | | /// Do all uses of this SDValue need the result in a GPR? |
3978 | | /// This is meant to be used on values that have type i1 since |
3979 | | /// it is somewhat meaningless to ask if values of other types |
3980 | | /// should be kept in GPR's. |
3981 | 16.3k | static bool allUsesExtend(SDValue Compare, SelectionDAG *CurDAG) { |
3982 | 16.3k | assert(Compare.getOpcode() == ISD::SETCC && |
3983 | 16.3k | "An ISD::SETCC node required here."); |
3984 | | |
3985 | | // For values that have a single use, the caller should obviously already have |
3986 | | // checked if that use is an extending use. We check the other uses here. |
3987 | 16.3k | if (Compare.hasOneUse()) |
3988 | 14.2k | return true; |
3989 | | // We want the value in a GPR if it is being extended, used for a select, or |
3990 | | // used in logical operations. |
3991 | 2.15k | for (auto *CompareUse : Compare.getNode()->uses()) |
3992 | 3.92k | if (CompareUse->getOpcode() != ISD::SIGN_EXTEND && |
3993 | 3.92k | CompareUse->getOpcode() != ISD::ZERO_EXTEND && |
3994 | 3.92k | CompareUse->getOpcode() != ISD::SELECT && |
3995 | 3.92k | !ISD::isBitwiseLogicOp(CompareUse->getOpcode())) { |
3996 | 1.48k | OmittedForNonExtendUses++; |
3997 | 1.48k | return false; |
3998 | 1.48k | } |
3999 | 671 | return true; |
4000 | 2.15k | } |
4001 | | |
4002 | | /// Returns an equivalent of a SETCC node but with the result the same width as |
4003 | | /// the inputs. This can also be used for SELECT_CC if either the true or false |
4004 | | /// values is a power of two while the other is zero. |
4005 | | SDValue IntegerCompareEliminator::getSETCCInGPR(SDValue Compare, |
4006 | 16.3k | SetccInGPROpts ConvOpts) { |
4007 | 16.3k | assert((Compare.getOpcode() == ISD::SETCC || |
4008 | 16.3k | Compare.getOpcode() == ISD::SELECT_CC) && |
4009 | 16.3k | "An ISD::SETCC node required here."); |
4010 | | |
4011 | | // Don't convert this comparison to a GPR sequence because there are uses |
4012 | | // of the i1 result (i.e. uses that require the result in the CR). |
4013 | 16.3k | if ((Compare.getOpcode() == ISD::SETCC) && !allUsesExtend(Compare, CurDAG)) |
4014 | 1.48k | return SDValue(); |
4015 | | |
4016 | 14.8k | SDValue LHS = Compare.getOperand(0); |
4017 | 14.8k | SDValue RHS = Compare.getOperand(1); |
4018 | | |
4019 | | // The condition code is operand 2 for SETCC and operand 4 for SELECT_CC. |
4020 | 14.8k | int CCOpNum = Compare.getOpcode() == ISD::SELECT_CC ? 4 : 2; |
4021 | 14.8k | ISD::CondCode CC = |
4022 | 14.8k | cast<CondCodeSDNode>(Compare.getOperand(CCOpNum))->get(); |
4023 | 14.8k | EVT InputVT = LHS.getValueType(); |
4024 | 14.8k | if (InputVT != MVT::i32 && InputVT != MVT::i64) |
4025 | 6.49k | return SDValue(); |
4026 | | |
4027 | 8.40k | if (ConvOpts == SetccInGPROpts::ZExtInvert || |
4028 | 8.40k | ConvOpts == SetccInGPROpts::SExtInvert) |
4029 | 0 | CC = ISD::getSetCCInverse(CC, InputVT); |
4030 | | |
4031 | 8.40k | bool Inputs32Bit = InputVT == MVT::i32; |
4032 | | |
4033 | 8.40k | SDLoc dl(Compare); |
4034 | 8.40k | ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS); |
4035 | 8.40k | int64_t RHSValue = RHSConst ? RHSConst->getSExtValue() : INT64_MAX; |
4036 | 8.40k | bool IsSext = ConvOpts == SetccInGPROpts::SExtOrig || |
4037 | 8.40k | ConvOpts == SetccInGPROpts::SExtInvert; |
4038 | | |
4039 | 8.40k | if (IsSext && Inputs32Bit) |
4040 | 19 | return get32BitSExtCompare(LHS, RHS, CC, RHSValue, dl); |
4041 | 8.38k | else if (Inputs32Bit) |
4042 | 5.55k | return get32BitZExtCompare(LHS, RHS, CC, RHSValue, dl); |
4043 | 2.82k | else if (IsSext) |
4044 | 18 | return get64BitSExtCompare(LHS, RHS, CC, RHSValue, dl); |
4045 | 2.80k | return get64BitZExtCompare(LHS, RHS, CC, RHSValue, dl); |
4046 | 8.40k | } |
4047 | | |
4048 | | } // end anonymous namespace |
4049 | | |
4050 | 3.50M | bool PPCDAGToDAGISel::tryIntCompareInGPR(SDNode *N) { |
4051 | 3.50M | if (N->getValueType(0) != MVT::i32 && |
4052 | 3.50M | N->getValueType(0) != MVT::i64) |
4053 | 967k | return false; |
4054 | | |
4055 | | // This optimization will emit code that assumes 64-bit registers |
4056 | | // so we don't want to run it in 32-bit mode. Also don't run it |
4057 | | // on functions that are not to be optimized. |
4058 | 2.54M | if (TM.getOptLevel() == CodeGenOptLevel::None || !TM.isPPC64()) |
4059 | 0 | return false; |
4060 | | |
4061 | | // For POWER10, it is more profitable to use the set boolean extension |
4062 | | // instructions rather than the integer compare elimination codegen. |
4063 | | // Users can override this via the command line option, `--ppc-gpr-icmps`. |
4064 | 2.54M | if (!(CmpInGPR.getNumOccurrences() > 0) && Subtarget->isISA3_1()) |
4065 | 0 | return false; |
4066 | | |
4067 | 2.54M | switch (N->getOpcode()) { |
4068 | 2.40M | default: break; |
4069 | 2.40M | case ISD::ZERO_EXTEND: |
4070 | 25.0k | case ISD::SIGN_EXTEND: |
4071 | 67.9k | case ISD::AND: |
4072 | 121k | case ISD::OR: |
4073 | 134k | case ISD::XOR: { |
4074 | 134k | IntegerCompareEliminator ICmpElim(CurDAG, this); |
4075 | 134k | if (SDNode *New = ICmpElim.Select(N)) { |
4076 | 8.58k | ReplaceNode(N, New); |
4077 | 8.58k | return true; |
4078 | 8.58k | } |
4079 | 134k | } |
4080 | 2.54M | } |
4081 | 2.53M | return false; |
4082 | 2.54M | } |
4083 | | |
4084 | 3.55M | bool PPCDAGToDAGISel::tryBitPermutation(SDNode *N) { |
4085 | 3.55M | if (N->getValueType(0) != MVT::i32 && |
4086 | 3.55M | N->getValueType(0) != MVT::i64) |
4087 | 967k | return false; |
4088 | | |
4089 | 2.58M | if (!UseBitPermRewriter) |
4090 | 0 | return false; |
4091 | | |
4092 | 2.58M | switch (N->getOpcode()) { |
4093 | 2.42M | default: break; |
4094 | 2.42M | case ISD::SRL: |
4095 | | // If we are on P10, we have a pattern for 32-bit (srl (bswap r), 16) that |
4096 | | // uses the BRH instruction. |
4097 | 23.5k | if (Subtarget->isISA3_1() && N->getValueType(0) == MVT::i32 && |
4098 | 23.5k | N->getOperand(0).getOpcode() == ISD::BSWAP) { |
4099 | 0 | auto &OpRight = N->getOperand(1); |
4100 | 0 | ConstantSDNode *SRLConst = dyn_cast<ConstantSDNode>(OpRight); |
4101 | 0 | if (SRLConst && SRLConst->getSExtValue() == 16) |
4102 | 0 | return false; |
4103 | 0 | } |
4104 | 23.5k | [[fallthrough]]; |
4105 | 23.6k | case ISD::ROTL: |
4106 | 54.0k | case ISD::SHL: |
4107 | 102k | case ISD::AND: |
4108 | 162k | case ISD::OR: { |
4109 | 162k | BitPermutationSelector BPS(CurDAG); |
4110 | 162k | if (SDNode *New = BPS.Select(N)) { |
4111 | 42.0k | ReplaceNode(N, New); |
4112 | 42.0k | return true; |
4113 | 42.0k | } |
4114 | 120k | return false; |
4115 | 162k | } |
4116 | 2.58M | } |
4117 | | |
4118 | 2.42M | return false; |
4119 | 2.58M | } |
4120 | | |
4121 | | /// SelectCC - Select a comparison of the specified values with the specified |
4122 | | /// condition code, returning the CR# of the expression. |
4123 | | SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, |
4124 | 18.1k | const SDLoc &dl, SDValue Chain) { |
4125 | | // Always select the LHS. |
4126 | 18.1k | unsigned Opc; |
4127 | | |
4128 | 18.1k | if (LHS.getValueType() == MVT::i32) { |
4129 | 2.40k | unsigned Imm; |
4130 | 2.40k | if (CC == ISD::SETEQ || CC == ISD::SETNE) { |
4131 | 752 | if (isInt32Immediate(RHS, Imm)) { |
4132 | | // SETEQ/SETNE comparison with 16-bit immediate, fold it. |
4133 | 658 | if (isUInt<16>(Imm)) |
4134 | 627 | return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS, |
4135 | 627 | getI32Imm(Imm & 0xFFFF, dl)), |
4136 | 627 | 0); |
4137 | | // If this is a 16-bit signed immediate, fold it. |
4138 | 31 | if (isInt<16>((int)Imm)) |
4139 | 26 | return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS, |
4140 | 26 | getI32Imm(Imm & 0xFFFF, dl)), |
4141 | 26 | 0); |
4142 | | |
4143 | | // For non-equality comparisons, the default code would materialize the |
4144 | | // constant, then compare against it, like this: |
4145 | | // lis r2, 4660 |
4146 | | // ori r2, r2, 22136 |
4147 | | // cmpw cr0, r3, r2 |
4148 | | // Since we are just comparing for equality, we can emit this instead: |
4149 | | // xoris r0,r3,0x1234 |
4150 | | // cmplwi cr0,r0,0x5678 |
4151 | | // beq cr0,L6 |
4152 | 5 | SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS, |
4153 | 5 | getI32Imm(Imm >> 16, dl)), 0); |
4154 | 5 | return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor, |
4155 | 5 | getI32Imm(Imm & 0xFFFF, dl)), 0); |
4156 | 31 | } |
4157 | 94 | Opc = PPC::CMPLW; |
4158 | 1.65k | } else if (ISD::isUnsignedIntSetCC(CC)) { |
4159 | 512 | if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm)) |
4160 | 103 | return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS, |
4161 | 103 | getI32Imm(Imm & 0xFFFF, dl)), 0); |
4162 | 409 | Opc = PPC::CMPLW; |
4163 | 1.14k | } else { |
4164 | 1.14k | int16_t SImm; |
4165 | 1.14k | if (isIntS16Immediate(RHS, SImm)) |
4166 | 1.01k | return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS, |
4167 | 1.01k | getI32Imm((int)SImm & 0xFFFF, |
4168 | 1.01k | dl)), |
4169 | 1.01k | 0); |
4170 | 125 | Opc = PPC::CMPW; |
4171 | 125 | } |
4172 | 15.7k | } else if (LHS.getValueType() == MVT::i64) { |
4173 | 15.5k | uint64_t Imm; |
4174 | 15.5k | if (CC == ISD::SETEQ || CC == ISD::SETNE) { |
4175 | 15.3k | if (isInt64Immediate(RHS.getNode(), Imm)) { |
4176 | | // SETEQ/SETNE comparison with 16-bit immediate, fold it. |
4177 | 14.3k | if (isUInt<16>(Imm)) |
4178 | 14.1k | return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS, |
4179 | 14.1k | getI32Imm(Imm & 0xFFFF, dl)), |
4180 | 14.1k | 0); |
4181 | | // If this is a 16-bit signed immediate, fold it. |
4182 | 133 | if (isInt<16>(Imm)) |
4183 | 76 | return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS, |
4184 | 76 | getI32Imm(Imm & 0xFFFF, dl)), |
4185 | 76 | 0); |
4186 | | |
4187 | | // For non-equality comparisons, the default code would materialize the |
4188 | | // constant, then compare against it, like this: |
4189 | | // lis r2, 4660 |
4190 | | // ori r2, r2, 22136 |
4191 | | // cmpd cr0, r3, r2 |
4192 | | // Since we are just comparing for equality, we can emit this instead: |
4193 | | // xoris r0,r3,0x1234 |
4194 | | // cmpldi cr0,r0,0x5678 |
4195 | | // beq cr0,L6 |
4196 | 57 | if (isUInt<32>(Imm)) { |
4197 | 13 | SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS, |
4198 | 13 | getI64Imm(Imm >> 16, dl)), 0); |
4199 | 13 | return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor, |
4200 | 13 | getI64Imm(Imm & 0xFFFF, dl)), |
4201 | 13 | 0); |
4202 | 13 | } |
4203 | 57 | } |
4204 | 1.03k | Opc = PPC::CMPLD; |
4205 | 1.03k | } else if (ISD::isUnsignedIntSetCC(CC)) { |
4206 | 140 | if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm)) |
4207 | 104 | return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS, |
4208 | 104 | getI64Imm(Imm & 0xFFFF, dl)), 0); |
4209 | 36 | Opc = PPC::CMPLD; |
4210 | 70 | } else { |
4211 | 70 | int16_t SImm; |
4212 | 70 | if (isIntS16Immediate(RHS, SImm)) |
4213 | 56 | return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS, |
4214 | 56 | getI64Imm(SImm & 0xFFFF, dl)), |
4215 | 56 | 0); |
4216 | 14 | Opc = PPC::CMPD; |
4217 | 14 | } |
4218 | 15.5k | } else if (LHS.getValueType() == MVT::f32) { |
4219 | 193 | if (Subtarget->hasSPE()) { |
4220 | 0 | switch (CC) { |
4221 | 0 | default: |
4222 | 0 | case ISD::SETEQ: |
4223 | 0 | case ISD::SETNE: |
4224 | 0 | Opc = PPC::EFSCMPEQ; |
4225 | 0 | break; |
4226 | 0 | case ISD::SETLT: |
4227 | 0 | case ISD::SETGE: |
4228 | 0 | case ISD::SETOLT: |
4229 | 0 | case ISD::SETOGE: |
4230 | 0 | case ISD::SETULT: |
4231 | 0 | case ISD::SETUGE: |
4232 | 0 | Opc = PPC::EFSCMPLT; |
4233 | 0 | break; |
4234 | 0 | case ISD::SETGT: |
4235 | 0 | case ISD::SETLE: |
4236 | 0 | case ISD::SETOGT: |
4237 | 0 | case ISD::SETOLE: |
4238 | 0 | case ISD::SETUGT: |
4239 | 0 | case ISD::SETULE: |
4240 | 0 | Opc = PPC::EFSCMPGT; |
4241 | 0 | break; |
4242 | 0 | } |
4243 | 0 | } else |
4244 | 193 | Opc = PPC::FCMPUS; |
4245 | 193 | } else if (LHS.getValueType() == MVT::f64) { |
4246 | 53 | if (Subtarget->hasSPE()) { |
4247 | 0 | switch (CC) { |
4248 | 0 | default: |
4249 | 0 | case ISD::SETEQ: |
4250 | 0 | case ISD::SETNE: |
4251 | 0 | Opc = PPC::EFDCMPEQ; |
4252 | 0 | break; |
4253 | 0 | case ISD::SETLT: |
4254 | 0 | case ISD::SETGE: |
4255 | 0 | case ISD::SETOLT: |
4256 | 0 | case ISD::SETOGE: |
4257 | 0 | case ISD::SETULT: |
4258 | 0 | case ISD::SETUGE: |
4259 | 0 | Opc = PPC::EFDCMPLT; |
4260 | 0 | break; |
4261 | 0 | case ISD::SETGT: |
4262 | 0 | case ISD::SETLE: |
4263 | 0 | case ISD::SETOGT: |
4264 | 0 | case ISD::SETOLE: |
4265 | 0 | case ISD::SETUGT: |
4266 | 0 | case ISD::SETULE: |
4267 | 0 | Opc = PPC::EFDCMPGT; |
4268 | 0 | break; |
4269 | 0 | } |
4270 | 0 | } else |
4271 | 53 | Opc = Subtarget->hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD; |
4272 | 53 | } else { |
4273 | 0 | assert(LHS.getValueType() == MVT::f128 && "Unknown vt!"); |
4274 | 0 | assert(Subtarget->hasP9Vector() && "XSCMPUQP requires Power9 Vector"); |
4275 | 0 | Opc = PPC::XSCMPUQP; |
4276 | 0 | } |
4277 | 1.96k | if (Chain) |
4278 | 0 | return SDValue( |
4279 | 0 | CurDAG->getMachineNode(Opc, dl, MVT::i32, MVT::Other, LHS, RHS, Chain), |
4280 | 0 | 0); |
4281 | 1.96k | else |
4282 | 1.96k | return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0); |
4283 | 1.96k | } |
4284 | | |
4285 | | static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC, const EVT &VT, |
4286 | 13.7k | const PPCSubtarget *Subtarget) { |
4287 | | // For SPE instructions, the result is in GT bit of the CR |
4288 | 13.7k | bool UseSPE = Subtarget->hasSPE() && VT.isFloatingPoint(); |
4289 | | |
4290 | 13.7k | switch (CC) { |
4291 | 0 | case ISD::SETUEQ: |
4292 | 0 | case ISD::SETONE: |
4293 | 0 | case ISD::SETOLE: |
4294 | 0 | case ISD::SETOGE: |
4295 | 0 | llvm_unreachable("Should be lowered by legalize!"); |
4296 | 0 | default: llvm_unreachable("Unknown condition!"); |
4297 | 2 | case ISD::SETOEQ: |
4298 | 9.03k | case ISD::SETEQ: |
4299 | 9.03k | return UseSPE ? PPC::PRED_GT : PPC::PRED_EQ; |
4300 | 23 | case ISD::SETUNE: |
4301 | 2.62k | case ISD::SETNE: |
4302 | 2.62k | return UseSPE ? PPC::PRED_LE : PPC::PRED_NE; |
4303 | 67 | case ISD::SETOLT: |
4304 | 686 | case ISD::SETLT: |
4305 | 686 | return UseSPE ? PPC::PRED_GT : PPC::PRED_LT; |
4306 | 41 | case ISD::SETULE: |
4307 | 66 | case ISD::SETLE: |
4308 | 66 | return PPC::PRED_LE; |
4309 | 31 | case ISD::SETOGT: |
4310 | 585 | case ISD::SETGT: |
4311 | 585 | return PPC::PRED_GT; |
4312 | 27 | case ISD::SETUGE: |
4313 | 66 | case ISD::SETGE: |
4314 | 66 | return UseSPE ? PPC::PRED_LE : PPC::PRED_GE; |
4315 | 8 | case ISD::SETO: return PPC::PRED_NU; |
4316 | 46 | case ISD::SETUO: return PPC::PRED_UN; |
4317 | | // These two are invalid for floating point. Assume we have int. |
4318 | 127 | case ISD::SETULT: return PPC::PRED_LT; |
4319 | 501 | case ISD::SETUGT: return PPC::PRED_GT; |
4320 | 13.7k | } |
4321 | 13.7k | } |
4322 | | |
4323 | | /// getCRIdxForSetCC - Return the index of the condition register field |
4324 | | /// associated with the SetCC condition, and whether or not the field is |
4325 | | /// treated as inverted. That is, lt = 0; ge = 0 inverted. |
4326 | 4.76k | static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) { |
4327 | 4.76k | Invert = false; |
4328 | 4.76k | switch (CC) { |
4329 | 0 | default: llvm_unreachable("Unknown condition!"); |
4330 | 0 | case ISD::SETOLT: |
4331 | 13 | case ISD::SETLT: return 0; // Bit #0 = SETOLT |
4332 | 0 | case ISD::SETOGT: |
4333 | 0 | case ISD::SETGT: return 1; // Bit #1 = SETOGT |
4334 | 0 | case ISD::SETOEQ: |
4335 | 4.75k | case ISD::SETEQ: return 2; // Bit #2 = SETOEQ |
4336 | 0 | case ISD::SETUO: return 3; // Bit #3 = SETUO |
4337 | 0 | case ISD::SETUGE: |
4338 | 0 | case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE |
4339 | 0 | case ISD::SETULE: |
4340 | 0 | case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE |
4341 | 0 | case ISD::SETUNE: |
4342 | 0 | case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE |
4343 | 0 | case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO |
4344 | 0 | case ISD::SETUEQ: |
4345 | 0 | case ISD::SETOGE: |
4346 | 0 | case ISD::SETOLE: |
4347 | 0 | case ISD::SETONE: |
4348 | 0 | llvm_unreachable("Invalid branch code: should be expanded by legalize"); |
4349 | | // These are invalid for floating point. Assume integer. |
4350 | 0 | case ISD::SETULT: return 0; |
4351 | 0 | case ISD::SETUGT: return 1; |
4352 | 4.76k | } |
4353 | 4.76k | } |
4354 | | |
4355 | | // getVCmpInst: return the vector compare instruction for the specified |
4356 | | // vector type and condition code. Since this is for altivec specific code, |
4357 | | // only support the altivec types (v16i8, v8i16, v4i32, v2i64, v1i128, |
4358 | | // and v4f32). |
4359 | | static unsigned int getVCmpInst(MVT VecVT, ISD::CondCode CC, |
4360 | 0 | bool HasVSX, bool &Swap, bool &Negate) { |
4361 | 0 | Swap = false; |
4362 | 0 | Negate = false; |
4363 | |
|
4364 | 0 | if (VecVT.isFloatingPoint()) { |
4365 | | /* Handle some cases by swapping input operands. */ |
4366 | 0 | switch (CC) { |
4367 | 0 | case ISD::SETLE: CC = ISD::SETGE; Swap = true; break; |
4368 | 0 | case ISD::SETLT: CC = ISD::SETGT; Swap = true; break; |
4369 | 0 | case ISD::SETOLE: CC = ISD::SETOGE; Swap = true; break; |
4370 | 0 | case ISD::SETOLT: CC = ISD::SETOGT; Swap = true; break; |
4371 | 0 | case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break; |
4372 | 0 | case ISD::SETUGT: CC = ISD::SETULT; Swap = true; break; |
4373 | 0 | default: break; |
4374 | 0 | } |
4375 | | /* Handle some cases by negating the result. */ |
4376 | 0 | switch (CC) { |
4377 | 0 | case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break; |
4378 | 0 | case ISD::SETUNE: CC = ISD::SETOEQ; Negate = true; break; |
4379 | 0 | case ISD::SETULE: CC = ISD::SETOGT; Negate = true; break; |
4380 | 0 | case ISD::SETULT: CC = ISD::SETOGE; Negate = true; break; |
4381 | 0 | default: break; |
4382 | 0 | } |
4383 | | /* We have instructions implementing the remaining cases. */ |
4384 | 0 | switch (CC) { |
4385 | 0 | case ISD::SETEQ: |
4386 | 0 | case ISD::SETOEQ: |
4387 | 0 | if (VecVT == MVT::v4f32) |
4388 | 0 | return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP; |
4389 | 0 | else if (VecVT == MVT::v2f64) |
4390 | 0 | return PPC::XVCMPEQDP; |
4391 | 0 | break; |
4392 | 0 | case ISD::SETGT: |
4393 | 0 | case ISD::SETOGT: |
4394 | 0 | if (VecVT == MVT::v4f32) |
4395 | 0 | return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP; |
4396 | 0 | else if (VecVT == MVT::v2f64) |
4397 | 0 | return PPC::XVCMPGTDP; |
4398 | 0 | break; |
4399 | 0 | case ISD::SETGE: |
4400 | 0 | case ISD::SETOGE: |
4401 | 0 | if (VecVT == MVT::v4f32) |
4402 | 0 | return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP; |
4403 | 0 | else if (VecVT == MVT::v2f64) |
4404 | 0 | return PPC::XVCMPGEDP; |
4405 | 0 | break; |
4406 | 0 | default: |
4407 | 0 | break; |
4408 | 0 | } |
4409 | 0 | llvm_unreachable("Invalid floating-point vector compare condition"); |
4410 | 0 | } else { |
4411 | | /* Handle some cases by swapping input operands. */ |
4412 | 0 | switch (CC) { |
4413 | 0 | case ISD::SETGE: CC = ISD::SETLE; Swap = true; break; |
4414 | 0 | case ISD::SETLT: CC = ISD::SETGT; Swap = true; break; |
4415 | 0 | case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break; |
4416 | 0 | case ISD::SETULT: CC = ISD::SETUGT; Swap = true; break; |
4417 | 0 | default: break; |
4418 | 0 | } |
4419 | | /* Handle some cases by negating the result. */ |
4420 | 0 | switch (CC) { |
4421 | 0 | case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break; |
4422 | 0 | case ISD::SETUNE: CC = ISD::SETUEQ; Negate = true; break; |
4423 | 0 | case ISD::SETLE: CC = ISD::SETGT; Negate = true; break; |
4424 | 0 | case ISD::SETULE: CC = ISD::SETUGT; Negate = true; break; |
4425 | 0 | default: break; |
4426 | 0 | } |
4427 | | /* We have instructions implementing the remaining cases. */ |
4428 | 0 | switch (CC) { |
4429 | 0 | case ISD::SETEQ: |
4430 | 0 | case ISD::SETUEQ: |
4431 | 0 | if (VecVT == MVT::v16i8) |
4432 | 0 | return PPC::VCMPEQUB; |
4433 | 0 | else if (VecVT == MVT::v8i16) |
4434 | 0 | return PPC::VCMPEQUH; |
4435 | 0 | else if (VecVT == MVT::v4i32) |
4436 | 0 | return PPC::VCMPEQUW; |
4437 | 0 | else if (VecVT == MVT::v2i64) |
4438 | 0 | return PPC::VCMPEQUD; |
4439 | 0 | else if (VecVT == MVT::v1i128) |
4440 | 0 | return PPC::VCMPEQUQ; |
4441 | 0 | break; |
4442 | 0 | case ISD::SETGT: |
4443 | 0 | if (VecVT == MVT::v16i8) |
4444 | 0 | return PPC::VCMPGTSB; |
4445 | 0 | else if (VecVT == MVT::v8i16) |
4446 | 0 | return PPC::VCMPGTSH; |
4447 | 0 | else if (VecVT == MVT::v4i32) |
4448 | 0 | return PPC::VCMPGTSW; |
4449 | 0 | else if (VecVT == MVT::v2i64) |
4450 | 0 | return PPC::VCMPGTSD; |
4451 | 0 | else if (VecVT == MVT::v1i128) |
4452 | 0 | return PPC::VCMPGTSQ; |
4453 | 0 | break; |
4454 | 0 | case ISD::SETUGT: |
4455 | 0 | if (VecVT == MVT::v16i8) |
4456 | 0 | return PPC::VCMPGTUB; |
4457 | 0 | else if (VecVT == MVT::v8i16) |
4458 | 0 | return PPC::VCMPGTUH; |
4459 | 0 | else if (VecVT == MVT::v4i32) |
4460 | 0 | return PPC::VCMPGTUW; |
4461 | 0 | else if (VecVT == MVT::v2i64) |
4462 | 0 | return PPC::VCMPGTUD; |
4463 | 0 | else if (VecVT == MVT::v1i128) |
4464 | 0 | return PPC::VCMPGTUQ; |
4465 | 0 | break; |
4466 | 0 | default: |
4467 | 0 | break; |
4468 | 0 | } |
4469 | 0 | llvm_unreachable("Invalid integer vector compare condition"); |
4470 | 0 | } |
4471 | 0 | } |
4472 | | |
4473 | 24.3k | bool PPCDAGToDAGISel::trySETCC(SDNode *N) { |
4474 | 24.3k | SDLoc dl(N); |
4475 | 24.3k | unsigned Imm; |
4476 | 24.3k | bool IsStrict = N->isStrictFPOpcode(); |
4477 | 24.3k | ISD::CondCode CC = |
4478 | 24.3k | cast<CondCodeSDNode>(N->getOperand(IsStrict ? 3 : 2))->get(); |
4479 | 24.3k | EVT PtrVT = |
4480 | 24.3k | CurDAG->getTargetLoweringInfo().getPointerTy(CurDAG->getDataLayout()); |
4481 | 24.3k | bool isPPC64 = (PtrVT == MVT::i64); |
4482 | 24.3k | SDValue Chain = IsStrict ? N->getOperand(0) : SDValue(); |
4483 | | |
4484 | 24.3k | SDValue LHS = N->getOperand(IsStrict ? 1 : 0); |
4485 | 24.3k | SDValue RHS = N->getOperand(IsStrict ? 2 : 1); |
4486 | | |
4487 | 24.3k | if (!IsStrict && !Subtarget->useCRBits() && isInt32Immediate(RHS, Imm)) { |
4488 | | // We can codegen setcc op, imm very efficiently compared to a brcond. |
4489 | | // Check for those cases here. |
4490 | | // setcc op, 0 |
4491 | 0 | if (Imm == 0) { |
4492 | 0 | SDValue Op = LHS; |
4493 | 0 | switch (CC) { |
4494 | 0 | default: break; |
4495 | 0 | case ISD::SETEQ: { |
4496 | 0 | Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0); |
4497 | 0 | SDValue Ops[] = { Op, getI32Imm(27, dl), getI32Imm(5, dl), |
4498 | 0 | getI32Imm(31, dl) }; |
4499 | 0 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
4500 | 0 | return true; |
4501 | 0 | } |
4502 | 0 | case ISD::SETNE: { |
4503 | 0 | if (isPPC64) break; |
4504 | 0 | SDValue AD = |
4505 | 0 | SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue, |
4506 | 0 | Op, getI32Imm(~0U, dl)), 0); |
4507 | 0 | CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1)); |
4508 | 0 | return true; |
4509 | 0 | } |
4510 | 0 | case ISD::SETLT: { |
4511 | 0 | SDValue Ops[] = { Op, getI32Imm(1, dl), getI32Imm(31, dl), |
4512 | 0 | getI32Imm(31, dl) }; |
4513 | 0 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
4514 | 0 | return true; |
4515 | 0 | } |
4516 | 0 | case ISD::SETGT: { |
4517 | 0 | SDValue T = |
4518 | 0 | SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0); |
4519 | 0 | T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0); |
4520 | 0 | SDValue Ops[] = { T, getI32Imm(1, dl), getI32Imm(31, dl), |
4521 | 0 | getI32Imm(31, dl) }; |
4522 | 0 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
4523 | 0 | return true; |
4524 | 0 | } |
4525 | 0 | } |
4526 | 0 | } else if (Imm == ~0U) { // setcc op, -1 |
4527 | 0 | SDValue Op = LHS; |
4528 | 0 | switch (CC) { |
4529 | 0 | default: break; |
4530 | 0 | case ISD::SETEQ: |
4531 | 0 | if (isPPC64) break; |
4532 | 0 | Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue, |
4533 | 0 | Op, getI32Imm(1, dl)), 0); |
4534 | 0 | CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, |
4535 | 0 | SDValue(CurDAG->getMachineNode(PPC::LI, dl, |
4536 | 0 | MVT::i32, |
4537 | 0 | getI32Imm(0, dl)), |
4538 | 0 | 0), Op.getValue(1)); |
4539 | 0 | return true; |
4540 | 0 | case ISD::SETNE: { |
4541 | 0 | if (isPPC64) break; |
4542 | 0 | Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0); |
4543 | 0 | SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue, |
4544 | 0 | Op, getI32Imm(~0U, dl)); |
4545 | 0 | CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0), Op, |
4546 | 0 | SDValue(AD, 1)); |
4547 | 0 | return true; |
4548 | 0 | } |
4549 | 0 | case ISD::SETLT: { |
4550 | 0 | SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op, |
4551 | 0 | getI32Imm(1, dl)), 0); |
4552 | 0 | SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD, |
4553 | 0 | Op), 0); |
4554 | 0 | SDValue Ops[] = { AN, getI32Imm(1, dl), getI32Imm(31, dl), |
4555 | 0 | getI32Imm(31, dl) }; |
4556 | 0 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
4557 | 0 | return true; |
4558 | 0 | } |
4559 | 0 | case ISD::SETGT: { |
4560 | 0 | SDValue Ops[] = { Op, getI32Imm(1, dl), getI32Imm(31, dl), |
4561 | 0 | getI32Imm(31, dl) }; |
4562 | 0 | Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0); |
4563 | 0 | CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1, dl)); |
4564 | 0 | return true; |
4565 | 0 | } |
4566 | 0 | } |
4567 | 0 | } |
4568 | 0 | } |
4569 | | |
4570 | | // Altivec Vector compare instructions do not set any CR register by default and |
4571 | | // vector compare operations return the same type as the operands. |
4572 | 24.3k | if (!IsStrict && LHS.getValueType().isVector()) { |
4573 | 0 | if (Subtarget->hasSPE()) |
4574 | 0 | return false; |
4575 | | |
4576 | 0 | EVT VecVT = LHS.getValueType(); |
4577 | 0 | bool Swap, Negate; |
4578 | 0 | unsigned int VCmpInst = |
4579 | 0 | getVCmpInst(VecVT.getSimpleVT(), CC, Subtarget->hasVSX(), Swap, Negate); |
4580 | 0 | if (Swap) |
4581 | 0 | std::swap(LHS, RHS); |
4582 | |
|
4583 | 0 | EVT ResVT = VecVT.changeVectorElementTypeToInteger(); |
4584 | 0 | if (Negate) { |
4585 | 0 | SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, ResVT, LHS, RHS), 0); |
4586 | 0 | CurDAG->SelectNodeTo(N, Subtarget->hasVSX() ? PPC::XXLNOR : PPC::VNOR, |
4587 | 0 | ResVT, VCmp, VCmp); |
4588 | 0 | return true; |
4589 | 0 | } |
4590 | | |
4591 | 0 | CurDAG->SelectNodeTo(N, VCmpInst, ResVT, LHS, RHS); |
4592 | 0 | return true; |
4593 | 0 | } |
4594 | | |
4595 | 24.3k | if (Subtarget->useCRBits()) |
4596 | 24.3k | return false; |
4597 | | |
4598 | 0 | bool Inv; |
4599 | 0 | unsigned Idx = getCRIdxForSetCC(CC, Inv); |
4600 | 0 | SDValue CCReg = SelectCC(LHS, RHS, CC, dl, Chain); |
4601 | 0 | if (IsStrict) |
4602 | 0 | CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 1), CCReg.getValue(1)); |
4603 | 0 | SDValue IntCR; |
4604 | | |
4605 | | // SPE e*cmp* instructions only set the 'gt' bit, so hard-code that |
4606 | | // The correct compare instruction is already set by SelectCC() |
4607 | 0 | if (Subtarget->hasSPE() && LHS.getValueType().isFloatingPoint()) { |
4608 | 0 | Idx = 1; |
4609 | 0 | } |
4610 | | |
4611 | | // Force the ccreg into CR7. |
4612 | 0 | SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32); |
4613 | |
|
4614 | 0 | SDValue InGlue; // Null incoming flag value. |
4615 | 0 | CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg, |
4616 | 0 | InGlue).getValue(1); |
4617 | |
|
4618 | 0 | IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg, |
4619 | 0 | CCReg), 0); |
4620 | |
|
4621 | 0 | SDValue Ops[] = { IntCR, getI32Imm((32 - (3 - Idx)) & 31, dl), |
4622 | 0 | getI32Imm(31, dl), getI32Imm(31, dl) }; |
4623 | 0 | if (!Inv) { |
4624 | 0 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
4625 | 0 | return true; |
4626 | 0 | } |
4627 | | |
4628 | | // Get the specified bit. |
4629 | 0 | SDValue Tmp = |
4630 | 0 | SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0); |
4631 | 0 | CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1, dl)); |
4632 | 0 | return true; |
4633 | 0 | } |
4634 | | |
4635 | | /// Does this node represent a load/store node whose address can be represented |
4636 | | /// with a register plus an immediate that's a multiple of \p Val: |
4637 | 930 | bool PPCDAGToDAGISel::isOffsetMultipleOf(SDNode *N, unsigned Val) const { |
4638 | 930 | LoadSDNode *LDN = dyn_cast<LoadSDNode>(N); |
4639 | 930 | StoreSDNode *STN = dyn_cast<StoreSDNode>(N); |
4640 | 930 | MemIntrinsicSDNode *MIN = dyn_cast<MemIntrinsicSDNode>(N); |
4641 | 930 | SDValue AddrOp; |
4642 | 930 | if (LDN || (MIN && MIN->getOpcode() == PPCISD::LD_SPLAT)) |
4643 | 0 | AddrOp = N->getOperand(1); |
4644 | 930 | else if (STN) |
4645 | 930 | AddrOp = STN->getOperand(2); |
4646 | | |
4647 | | // If the address points a frame object or a frame object with an offset, |
4648 | | // we need to check the object alignment. |
4649 | 930 | short Imm = 0; |
4650 | 930 | if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>( |
4651 | 930 | AddrOp.getOpcode() == ISD::ADD ? AddrOp.getOperand(0) : |
4652 | 930 | AddrOp)) { |
4653 | | // If op0 is a frame index that is under aligned, we can't do it either, |
4654 | | // because it is translated to r31 or r1 + slot + offset. We won't know the |
4655 | | // slot number until the stack frame is finalized. |
4656 | 83 | const MachineFrameInfo &MFI = CurDAG->getMachineFunction().getFrameInfo(); |
4657 | 83 | unsigned SlotAlign = MFI.getObjectAlign(FI->getIndex()).value(); |
4658 | 83 | if ((SlotAlign % Val) != 0) |
4659 | 2 | return false; |
4660 | | |
4661 | | // If we have an offset, we need further check on the offset. |
4662 | 81 | if (AddrOp.getOpcode() != ISD::ADD) |
4663 | 0 | return true; |
4664 | 81 | } |
4665 | | |
4666 | 928 | if (AddrOp.getOpcode() == ISD::ADD) |
4667 | 152 | return isIntS16Immediate(AddrOp.getOperand(1), Imm) && !(Imm % Val); |
4668 | | |
4669 | | // If the address comes from the outside, the offset will be zero. |
4670 | 776 | return AddrOp.getOpcode() == ISD::CopyFromReg; |
4671 | 928 | } |
4672 | | |
4673 | 28.0k | void PPCDAGToDAGISel::transferMemOperands(SDNode *N, SDNode *Result) { |
4674 | | // Transfer memoperands. |
4675 | 28.0k | MachineMemOperand *MemOp = cast<MemSDNode>(N)->getMemOperand(); |
4676 | 28.0k | CurDAG->setNodeMemRefs(cast<MachineSDNode>(Result), {MemOp}); |
4677 | 28.0k | } |
4678 | | |
4679 | | static bool mayUseP9Setb(SDNode *N, const ISD::CondCode &CC, SelectionDAG *DAG, |
4680 | 0 | bool &NeedSwapOps, bool &IsUnCmp) { |
4681 | |
|
4682 | 0 | assert(N->getOpcode() == ISD::SELECT_CC && "Expecting a SELECT_CC here."); |
4683 | | |
4684 | 0 | SDValue LHS = N->getOperand(0); |
4685 | 0 | SDValue RHS = N->getOperand(1); |
4686 | 0 | SDValue TrueRes = N->getOperand(2); |
4687 | 0 | SDValue FalseRes = N->getOperand(3); |
4688 | 0 | ConstantSDNode *TrueConst = dyn_cast<ConstantSDNode>(TrueRes); |
4689 | 0 | if (!TrueConst || (N->getSimpleValueType(0) != MVT::i64 && |
4690 | 0 | N->getSimpleValueType(0) != MVT::i32)) |
4691 | 0 | return false; |
4692 | | |
4693 | | // We are looking for any of: |
4694 | | // (select_cc lhs, rhs, 1, (sext (setcc [lr]hs, [lr]hs, cc2)), cc1) |
4695 | | // (select_cc lhs, rhs, -1, (zext (setcc [lr]hs, [lr]hs, cc2)), cc1) |
4696 | | // (select_cc lhs, rhs, 0, (select_cc [lr]hs, [lr]hs, 1, -1, cc2), seteq) |
4697 | | // (select_cc lhs, rhs, 0, (select_cc [lr]hs, [lr]hs, -1, 1, cc2), seteq) |
4698 | 0 | int64_t TrueResVal = TrueConst->getSExtValue(); |
4699 | 0 | if ((TrueResVal < -1 || TrueResVal > 1) || |
4700 | 0 | (TrueResVal == -1 && FalseRes.getOpcode() != ISD::ZERO_EXTEND) || |
4701 | 0 | (TrueResVal == 1 && FalseRes.getOpcode() != ISD::SIGN_EXTEND) || |
4702 | 0 | (TrueResVal == 0 && |
4703 | 0 | (FalseRes.getOpcode() != ISD::SELECT_CC || CC != ISD::SETEQ))) |
4704 | 0 | return false; |
4705 | | |
4706 | 0 | SDValue SetOrSelCC = FalseRes.getOpcode() == ISD::SELECT_CC |
4707 | 0 | ? FalseRes |
4708 | 0 | : FalseRes.getOperand(0); |
4709 | 0 | bool InnerIsSel = SetOrSelCC.getOpcode() == ISD::SELECT_CC; |
4710 | 0 | if (SetOrSelCC.getOpcode() != ISD::SETCC && |
4711 | 0 | SetOrSelCC.getOpcode() != ISD::SELECT_CC) |
4712 | 0 | return false; |
4713 | | |
4714 | | // Without this setb optimization, the outer SELECT_CC will be manually |
4715 | | // selected to SELECT_CC_I4/SELECT_CC_I8 Pseudo, then expand-isel-pseudos pass |
4716 | | // transforms pseudo instruction to isel instruction. When there are more than |
4717 | | // one use for result like zext/sext, with current optimization we only see |
4718 | | // isel is replaced by setb but can't see any significant gain. Since |
4719 | | // setb has longer latency than original isel, we should avoid this. Another |
4720 | | // point is that setb requires comparison always kept, it can break the |
4721 | | // opportunity to get the comparison away if we have in future. |
4722 | 0 | if (!SetOrSelCC.hasOneUse() || (!InnerIsSel && !FalseRes.hasOneUse())) |
4723 | 0 | return false; |
4724 | | |
4725 | 0 | SDValue InnerLHS = SetOrSelCC.getOperand(0); |
4726 | 0 | SDValue InnerRHS = SetOrSelCC.getOperand(1); |
4727 | 0 | ISD::CondCode InnerCC = |
4728 | 0 | cast<CondCodeSDNode>(SetOrSelCC.getOperand(InnerIsSel ? 4 : 2))->get(); |
4729 | | // If the inner comparison is a select_cc, make sure the true/false values are |
4730 | | // 1/-1 and canonicalize it if needed. |
4731 | 0 | if (InnerIsSel) { |
4732 | 0 | ConstantSDNode *SelCCTrueConst = |
4733 | 0 | dyn_cast<ConstantSDNode>(SetOrSelCC.getOperand(2)); |
4734 | 0 | ConstantSDNode *SelCCFalseConst = |
4735 | 0 | dyn_cast<ConstantSDNode>(SetOrSelCC.getOperand(3)); |
4736 | 0 | if (!SelCCTrueConst || !SelCCFalseConst) |
4737 | 0 | return false; |
4738 | 0 | int64_t SelCCTVal = SelCCTrueConst->getSExtValue(); |
4739 | 0 | int64_t SelCCFVal = SelCCFalseConst->getSExtValue(); |
4740 | | // The values must be -1/1 (requiring a swap) or 1/-1. |
4741 | 0 | if (SelCCTVal == -1 && SelCCFVal == 1) { |
4742 | 0 | std::swap(InnerLHS, InnerRHS); |
4743 | 0 | } else if (SelCCTVal != 1 || SelCCFVal != -1) |
4744 | 0 | return false; |
4745 | 0 | } |
4746 | | |
4747 | | // Canonicalize unsigned case |
4748 | 0 | if (InnerCC == ISD::SETULT || InnerCC == ISD::SETUGT) { |
4749 | 0 | IsUnCmp = true; |
4750 | 0 | InnerCC = (InnerCC == ISD::SETULT) ? ISD::SETLT : ISD::SETGT; |
4751 | 0 | } |
4752 | |
|
4753 | 0 | bool InnerSwapped = false; |
4754 | 0 | if (LHS == InnerRHS && RHS == InnerLHS) |
4755 | 0 | InnerSwapped = true; |
4756 | 0 | else if (LHS != InnerLHS || RHS != InnerRHS) |
4757 | 0 | return false; |
4758 | | |
4759 | 0 | switch (CC) { |
4760 | | // (select_cc lhs, rhs, 0, \ |
4761 | | // (select_cc [lr]hs, [lr]hs, 1, -1, setlt/setgt), seteq) |
4762 | 0 | case ISD::SETEQ: |
4763 | 0 | if (!InnerIsSel) |
4764 | 0 | return false; |
4765 | 0 | if (InnerCC != ISD::SETLT && InnerCC != ISD::SETGT) |
4766 | 0 | return false; |
4767 | 0 | NeedSwapOps = (InnerCC == ISD::SETGT) ? InnerSwapped : !InnerSwapped; |
4768 | 0 | break; |
4769 | | |
4770 | | // (select_cc lhs, rhs, -1, (zext (setcc [lr]hs, [lr]hs, setne)), setu?lt) |
4771 | | // (select_cc lhs, rhs, -1, (zext (setcc lhs, rhs, setgt)), setu?lt) |
4772 | | // (select_cc lhs, rhs, -1, (zext (setcc rhs, lhs, setlt)), setu?lt) |
4773 | | // (select_cc lhs, rhs, 1, (sext (setcc [lr]hs, [lr]hs, setne)), setu?lt) |
4774 | | // (select_cc lhs, rhs, 1, (sext (setcc lhs, rhs, setgt)), setu?lt) |
4775 | | // (select_cc lhs, rhs, 1, (sext (setcc rhs, lhs, setlt)), setu?lt) |
4776 | 0 | case ISD::SETULT: |
4777 | 0 | if (!IsUnCmp && InnerCC != ISD::SETNE) |
4778 | 0 | return false; |
4779 | 0 | IsUnCmp = true; |
4780 | 0 | [[fallthrough]]; |
4781 | 0 | case ISD::SETLT: |
4782 | 0 | if (InnerCC == ISD::SETNE || (InnerCC == ISD::SETGT && !InnerSwapped) || |
4783 | 0 | (InnerCC == ISD::SETLT && InnerSwapped)) |
4784 | 0 | NeedSwapOps = (TrueResVal == 1); |
4785 | 0 | else |
4786 | 0 | return false; |
4787 | 0 | break; |
4788 | | |
4789 | | // (select_cc lhs, rhs, 1, (sext (setcc [lr]hs, [lr]hs, setne)), setu?gt) |
4790 | | // (select_cc lhs, rhs, 1, (sext (setcc lhs, rhs, setlt)), setu?gt) |
4791 | | // (select_cc lhs, rhs, 1, (sext (setcc rhs, lhs, setgt)), setu?gt) |
4792 | | // (select_cc lhs, rhs, -1, (zext (setcc [lr]hs, [lr]hs, setne)), setu?gt) |
4793 | | // (select_cc lhs, rhs, -1, (zext (setcc lhs, rhs, setlt)), setu?gt) |
4794 | | // (select_cc lhs, rhs, -1, (zext (setcc rhs, lhs, setgt)), setu?gt) |
4795 | 0 | case ISD::SETUGT: |
4796 | 0 | if (!IsUnCmp && InnerCC != ISD::SETNE) |
4797 | 0 | return false; |
4798 | 0 | IsUnCmp = true; |
4799 | 0 | [[fallthrough]]; |
4800 | 0 | case ISD::SETGT: |
4801 | 0 | if (InnerCC == ISD::SETNE || (InnerCC == ISD::SETLT && !InnerSwapped) || |
4802 | 0 | (InnerCC == ISD::SETGT && InnerSwapped)) |
4803 | 0 | NeedSwapOps = (TrueResVal == -1); |
4804 | 0 | else |
4805 | 0 | return false; |
4806 | 0 | break; |
4807 | | |
4808 | 0 | default: |
4809 | 0 | return false; |
4810 | 0 | } |
4811 | | |
4812 | 0 | LLVM_DEBUG(dbgs() << "Found a node that can be lowered to a SETB: "); |
4813 | 0 | LLVM_DEBUG(N->dump()); |
4814 | |
|
4815 | 0 | return true; |
4816 | 0 | } |
4817 | | |
4818 | | // Return true if it's a software square-root/divide operand. |
4819 | 8.84k | static bool isSWTestOp(SDValue N) { |
4820 | 8.84k | if (N.getOpcode() == PPCISD::FTSQRT) |
4821 | 0 | return true; |
4822 | 8.84k | if (N.getNumOperands() < 1 || !isa<ConstantSDNode>(N.getOperand(0)) || |
4823 | 8.84k | N.getOpcode() != ISD::INTRINSIC_WO_CHAIN) |
4824 | 8.84k | return false; |
4825 | 0 | switch (N.getConstantOperandVal(0)) { |
4826 | 0 | case Intrinsic::ppc_vsx_xvtdivdp: |
4827 | 0 | case Intrinsic::ppc_vsx_xvtdivsp: |
4828 | 0 | case Intrinsic::ppc_vsx_xvtsqrtdp: |
4829 | 0 | case Intrinsic::ppc_vsx_xvtsqrtsp: |
4830 | 0 | return true; |
4831 | 0 | } |
4832 | 0 | return false; |
4833 | 0 | } |
4834 | | |
4835 | 10.2k | bool PPCDAGToDAGISel::tryFoldSWTestBRCC(SDNode *N) { |
4836 | 10.2k | assert(N->getOpcode() == ISD::BR_CC && "ISD::BR_CC is expected."); |
4837 | | // We are looking for following patterns, where `truncate to i1` actually has |
4838 | | // the same semantic with `and 1`. |
4839 | | // (br_cc seteq, (truncateToi1 SWTestOp), 0) -> (BCC PRED_NU, SWTestOp) |
4840 | | // (br_cc seteq, (and SWTestOp, 2), 0) -> (BCC PRED_NE, SWTestOp) |
4841 | | // (br_cc seteq, (and SWTestOp, 4), 0) -> (BCC PRED_LE, SWTestOp) |
4842 | | // (br_cc seteq, (and SWTestOp, 8), 0) -> (BCC PRED_GE, SWTestOp) |
4843 | | // (br_cc setne, (truncateToi1 SWTestOp), 0) -> (BCC PRED_UN, SWTestOp) |
4844 | | // (br_cc setne, (and SWTestOp, 2), 0) -> (BCC PRED_EQ, SWTestOp) |
4845 | | // (br_cc setne, (and SWTestOp, 4), 0) -> (BCC PRED_GT, SWTestOp) |
4846 | | // (br_cc setne, (and SWTestOp, 8), 0) -> (BCC PRED_LT, SWTestOp) |
4847 | 0 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get(); |
4848 | 10.2k | if (CC != ISD::SETEQ && CC != ISD::SETNE) |
4849 | 769 | return false; |
4850 | | |
4851 | 9.46k | SDValue CmpRHS = N->getOperand(3); |
4852 | 9.46k | if (!isNullConstant(CmpRHS)) |
4853 | 620 | return false; |
4854 | | |
4855 | 8.84k | SDValue CmpLHS = N->getOperand(2); |
4856 | 8.84k | if (CmpLHS.getNumOperands() < 1 || !isSWTestOp(CmpLHS.getOperand(0))) |
4857 | 8.84k | return false; |
4858 | | |
4859 | 0 | unsigned PCC = 0; |
4860 | 0 | bool IsCCNE = CC == ISD::SETNE; |
4861 | 0 | if (CmpLHS.getOpcode() == ISD::AND && |
4862 | 0 | isa<ConstantSDNode>(CmpLHS.getOperand(1))) |
4863 | 0 | switch (CmpLHS.getConstantOperandVal(1)) { |
4864 | 0 | case 1: |
4865 | 0 | PCC = IsCCNE ? PPC::PRED_UN : PPC::PRED_NU; |
4866 | 0 | break; |
4867 | 0 | case 2: |
4868 | 0 | PCC = IsCCNE ? PPC::PRED_EQ : PPC::PRED_NE; |
4869 | 0 | break; |
4870 | 0 | case 4: |
4871 | 0 | PCC = IsCCNE ? PPC::PRED_GT : PPC::PRED_LE; |
4872 | 0 | break; |
4873 | 0 | case 8: |
4874 | 0 | PCC = IsCCNE ? PPC::PRED_LT : PPC::PRED_GE; |
4875 | 0 | break; |
4876 | 0 | default: |
4877 | 0 | return false; |
4878 | 0 | } |
4879 | 0 | else if (CmpLHS.getOpcode() == ISD::TRUNCATE && |
4880 | 0 | CmpLHS.getValueType() == MVT::i1) |
4881 | 0 | PCC = IsCCNE ? PPC::PRED_UN : PPC::PRED_NU; |
4882 | | |
4883 | 0 | if (PCC) { |
4884 | 0 | SDLoc dl(N); |
4885 | 0 | SDValue Ops[] = {getI32Imm(PCC, dl), CmpLHS.getOperand(0), N->getOperand(4), |
4886 | 0 | N->getOperand(0)}; |
4887 | 0 | CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops); |
4888 | 0 | return true; |
4889 | 0 | } |
4890 | 0 | return false; |
4891 | 0 | } |
4892 | | |
4893 | 10.2k | bool PPCDAGToDAGISel::trySelectLoopCountIntrinsic(SDNode *N) { |
4894 | | // Sometimes the promoted value of the intrinsic is ANDed by some non-zero |
4895 | | // value, for example when crbits is disabled. If so, select the |
4896 | | // loop_decrement intrinsics now. |
4897 | 10.2k | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get(); |
4898 | 10.2k | SDValue LHS = N->getOperand(2), RHS = N->getOperand(3); |
4899 | | |
4900 | 10.2k | if (LHS.getOpcode() != ISD::AND || !isa<ConstantSDNode>(LHS.getOperand(1)) || |
4901 | 10.2k | isNullConstant(LHS.getOperand(1))) |
4902 | 3.17k | return false; |
4903 | | |
4904 | 7.05k | if (LHS.getOperand(0).getOpcode() != ISD::INTRINSIC_W_CHAIN || |
4905 | 7.05k | LHS.getOperand(0).getConstantOperandVal(1) != Intrinsic::loop_decrement) |
4906 | 7.05k | return false; |
4907 | | |
4908 | 0 | if (!isa<ConstantSDNode>(RHS)) |
4909 | 0 | return false; |
4910 | | |
4911 | 0 | assert((CC == ISD::SETEQ || CC == ISD::SETNE) && |
4912 | 0 | "Counter decrement comparison is not EQ or NE"); |
4913 | | |
4914 | 0 | SDValue OldDecrement = LHS.getOperand(0); |
4915 | 0 | assert(OldDecrement.hasOneUse() && "loop decrement has more than one use!"); |
4916 | | |
4917 | 0 | SDLoc DecrementLoc(OldDecrement); |
4918 | 0 | SDValue ChainInput = OldDecrement.getOperand(0); |
4919 | 0 | SDValue DecrementOps[] = {Subtarget->isPPC64() ? getI64Imm(1, DecrementLoc) |
4920 | 0 | : getI32Imm(1, DecrementLoc)}; |
4921 | 0 | unsigned DecrementOpcode = |
4922 | 0 | Subtarget->isPPC64() ? PPC::DecreaseCTR8loop : PPC::DecreaseCTRloop; |
4923 | 0 | SDNode *NewDecrement = CurDAG->getMachineNode(DecrementOpcode, DecrementLoc, |
4924 | 0 | MVT::i1, DecrementOps); |
4925 | |
|
4926 | 0 | unsigned Val = RHS->getAsZExtVal(); |
4927 | 0 | bool IsBranchOnTrue = (CC == ISD::SETEQ && Val) || (CC == ISD::SETNE && !Val); |
4928 | 0 | unsigned Opcode = IsBranchOnTrue ? PPC::BC : PPC::BCn; |
4929 | |
|
4930 | 0 | ReplaceUses(LHS.getValue(0), LHS.getOperand(1)); |
4931 | 0 | CurDAG->RemoveDeadNode(LHS.getNode()); |
4932 | | |
4933 | | // Mark the old loop_decrement intrinsic as dead. |
4934 | 0 | ReplaceUses(OldDecrement.getValue(1), ChainInput); |
4935 | 0 | CurDAG->RemoveDeadNode(OldDecrement.getNode()); |
4936 | |
|
4937 | 0 | SDValue Chain = CurDAG->getNode(ISD::TokenFactor, SDLoc(N), MVT::Other, |
4938 | 0 | ChainInput, N->getOperand(0)); |
4939 | |
|
4940 | 0 | CurDAG->SelectNodeTo(N, Opcode, MVT::Other, SDValue(NewDecrement, 0), |
4941 | 0 | N->getOperand(4), Chain); |
4942 | 0 | return true; |
4943 | 0 | } |
4944 | | |
4945 | 44.7k | bool PPCDAGToDAGISel::tryAsSingleRLWINM(SDNode *N) { |
4946 | 44.7k | assert(N->getOpcode() == ISD::AND && "ISD::AND SDNode expected"); |
4947 | 0 | unsigned Imm; |
4948 | 44.7k | if (!isInt32Immediate(N->getOperand(1), Imm)) |
4949 | 27.2k | return false; |
4950 | | |
4951 | 17.5k | SDLoc dl(N); |
4952 | 17.5k | SDValue Val = N->getOperand(0); |
4953 | 17.5k | unsigned SH, MB, ME; |
4954 | | // If this is an and of a value rotated between 0 and 31 bits and then and'd |
4955 | | // with a mask, emit rlwinm |
4956 | 17.5k | if (isRotateAndMask(Val.getNode(), Imm, false, SH, MB, ME)) { |
4957 | 0 | Val = Val.getOperand(0); |
4958 | 0 | SDValue Ops[] = {Val, getI32Imm(SH, dl), getI32Imm(MB, dl), |
4959 | 0 | getI32Imm(ME, dl)}; |
4960 | 0 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
4961 | 0 | return true; |
4962 | 0 | } |
4963 | | |
4964 | | // If this is just a masked value where the input is not handled, and |
4965 | | // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm |
4966 | 17.5k | if (isRunOfOnes(Imm, MB, ME) && Val.getOpcode() != ISD::ROTL) { |
4967 | 17.4k | SDValue Ops[] = {Val, getI32Imm(0, dl), getI32Imm(MB, dl), |
4968 | 17.4k | getI32Imm(ME, dl)}; |
4969 | 17.4k | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
4970 | 17.4k | return true; |
4971 | 17.4k | } |
4972 | | |
4973 | | // AND X, 0 -> 0, not "rlwinm 32". |
4974 | 86 | if (Imm == 0) { |
4975 | 0 | ReplaceUses(SDValue(N, 0), N->getOperand(1)); |
4976 | 0 | return true; |
4977 | 0 | } |
4978 | | |
4979 | 86 | return false; |
4980 | 86 | } |
4981 | | |
4982 | 14.1k | bool PPCDAGToDAGISel::tryAsSingleRLWINM8(SDNode *N) { |
4983 | 14.1k | assert(N->getOpcode() == ISD::AND && "ISD::AND SDNode expected"); |
4984 | 0 | uint64_t Imm64; |
4985 | 14.1k | if (!isInt64Immediate(N->getOperand(1).getNode(), Imm64)) |
4986 | 6.65k | return false; |
4987 | | |
4988 | 7.47k | unsigned MB, ME; |
4989 | 7.47k | if (isRunOfOnes64(Imm64, MB, ME) && MB >= 32 && MB <= ME) { |
4990 | | // MB ME |
4991 | | // +----------------------+ |
4992 | | // |xxxxxxxxxxx00011111000| |
4993 | | // +----------------------+ |
4994 | | // 0 32 64 |
4995 | | // We can only do it if the MB is larger than 32 and MB <= ME |
4996 | | // as RLWINM will replace the contents of [0 - 32) with [32 - 64) even |
4997 | | // we didn't rotate it. |
4998 | 3.50k | SDLoc dl(N); |
4999 | 3.50k | SDValue Ops[] = {N->getOperand(0), getI64Imm(0, dl), getI64Imm(MB - 32, dl), |
5000 | 3.50k | getI64Imm(ME - 32, dl)}; |
5001 | 3.50k | CurDAG->SelectNodeTo(N, PPC::RLWINM8, MVT::i64, Ops); |
5002 | 3.50k | return true; |
5003 | 3.50k | } |
5004 | | |
5005 | 3.96k | return false; |
5006 | 7.47k | } |
5007 | | |
5008 | 10.6k | bool PPCDAGToDAGISel::tryAsPairOfRLDICL(SDNode *N) { |
5009 | 10.6k | assert(N->getOpcode() == ISD::AND && "ISD::AND SDNode expected"); |
5010 | 0 | uint64_t Imm64; |
5011 | 10.6k | if (!isInt64Immediate(N->getOperand(1).getNode(), Imm64)) |
5012 | 6.65k | return false; |
5013 | | |
5014 | | // Do nothing if it is 16-bit imm as the pattern in the .td file handle |
5015 | | // it well with "andi.". |
5016 | 3.96k | if (isUInt<16>(Imm64)) |
5017 | 17 | return false; |
5018 | | |
5019 | 3.95k | SDLoc Loc(N); |
5020 | 3.95k | SDValue Val = N->getOperand(0); |
5021 | | |
5022 | | // Optimized with two rldicl's as follows: |
5023 | | // Add missing bits on left to the mask and check that the mask is a |
5024 | | // wrapped run of ones, i.e. |
5025 | | // Change pattern |0001111100000011111111| |
5026 | | // to |1111111100000011111111|. |
5027 | 3.95k | unsigned NumOfLeadingZeros = llvm::countl_zero(Imm64); |
5028 | 3.95k | if (NumOfLeadingZeros != 0) |
5029 | 3.92k | Imm64 |= maskLeadingOnes<uint64_t>(NumOfLeadingZeros); |
5030 | | |
5031 | 3.95k | unsigned MB, ME; |
5032 | 3.95k | if (!isRunOfOnes64(Imm64, MB, ME)) |
5033 | 31 | return false; |
5034 | | |
5035 | | // ME MB MB-ME+63 |
5036 | | // +----------------------+ +----------------------+ |
5037 | | // |1111111100000011111111| -> |0000001111111111111111| |
5038 | | // +----------------------+ +----------------------+ |
5039 | | // 0 63 0 63 |
5040 | | // There are ME + 1 ones on the left and (MB - ME + 63) & 63 zeros in between. |
5041 | 3.92k | unsigned OnesOnLeft = ME + 1; |
5042 | 3.92k | unsigned ZerosInBetween = (MB - ME + 63) & 63; |
5043 | | // Rotate left by OnesOnLeft (so leading ones are now trailing ones) and clear |
5044 | | // on the left the bits that are already zeros in the mask. |
5045 | 3.92k | Val = SDValue(CurDAG->getMachineNode(PPC::RLDICL, Loc, MVT::i64, Val, |
5046 | 3.92k | getI64Imm(OnesOnLeft, Loc), |
5047 | 3.92k | getI64Imm(ZerosInBetween, Loc)), |
5048 | 3.92k | 0); |
5049 | | // MB-ME+63 ME MB |
5050 | | // +----------------------+ +----------------------+ |
5051 | | // |0000001111111111111111| -> |0001111100000011111111| |
5052 | | // +----------------------+ +----------------------+ |
5053 | | // 0 63 0 63 |
5054 | | // Rotate back by 64 - OnesOnLeft to undo previous rotate. Then clear on the |
5055 | | // left the number of ones we previously added. |
5056 | 3.92k | SDValue Ops[] = {Val, getI64Imm(64 - OnesOnLeft, Loc), |
5057 | 3.92k | getI64Imm(NumOfLeadingZeros, Loc)}; |
5058 | 3.92k | CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops); |
5059 | 3.92k | return true; |
5060 | 3.95k | } |
5061 | | |
5062 | 27.3k | bool PPCDAGToDAGISel::tryAsSingleRLWIMI(SDNode *N) { |
5063 | 27.3k | assert(N->getOpcode() == ISD::AND && "ISD::AND SDNode expected"); |
5064 | 0 | unsigned Imm; |
5065 | 27.3k | if (!isInt32Immediate(N->getOperand(1), Imm)) |
5066 | 27.2k | return false; |
5067 | | |
5068 | 86 | SDValue Val = N->getOperand(0); |
5069 | 86 | unsigned Imm2; |
5070 | | // ISD::OR doesn't get all the bitfield insertion fun. |
5071 | | // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) might be a |
5072 | | // bitfield insert. |
5073 | 86 | if (Val.getOpcode() != ISD::OR || !isInt32Immediate(Val.getOperand(1), Imm2)) |
5074 | 84 | return false; |
5075 | | |
5076 | | // The idea here is to check whether this is equivalent to: |
5077 | | // (c1 & m) | (x & ~m) |
5078 | | // where m is a run-of-ones mask. The logic here is that, for each bit in |
5079 | | // c1 and c2: |
5080 | | // - if both are 1, then the output will be 1. |
5081 | | // - if both are 0, then the output will be 0. |
5082 | | // - if the bit in c1 is 0, and the bit in c2 is 1, then the output will |
5083 | | // come from x. |
5084 | | // - if the bit in c1 is 1, and the bit in c2 is 0, then the output will |
5085 | | // be 0. |
5086 | | // If that last condition is never the case, then we can form m from the |
5087 | | // bits that are the same between c1 and c2. |
5088 | 2 | unsigned MB, ME; |
5089 | 2 | if (isRunOfOnes(~(Imm ^ Imm2), MB, ME) && !(~Imm & Imm2)) { |
5090 | 1 | SDLoc dl(N); |
5091 | 1 | SDValue Ops[] = {Val.getOperand(0), Val.getOperand(1), getI32Imm(0, dl), |
5092 | 1 | getI32Imm(MB, dl), getI32Imm(ME, dl)}; |
5093 | 1 | ReplaceNode(N, CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops)); |
5094 | 1 | return true; |
5095 | 1 | } |
5096 | | |
5097 | 1 | return false; |
5098 | 2 | } |
5099 | | |
5100 | 27.3k | bool PPCDAGToDAGISel::tryAsSingleRLDCL(SDNode *N) { |
5101 | 27.3k | assert(N->getOpcode() == ISD::AND && "ISD::AND SDNode expected"); |
5102 | | |
5103 | 0 | uint64_t Imm64; |
5104 | 27.3k | if (!isInt64Immediate(N->getOperand(1).getNode(), Imm64) || !isMask_64(Imm64)) |
5105 | 14.3k | return false; |
5106 | | |
5107 | 12.9k | SDValue Val = N->getOperand(0); |
5108 | | |
5109 | 12.9k | if (Val.getOpcode() != ISD::ROTL) |
5110 | 12.9k | return false; |
5111 | | |
5112 | | // Looking to try to avoid a situation like this one: |
5113 | | // %2 = tail call i64 @llvm.fshl.i64(i64 %word, i64 %word, i64 23) |
5114 | | // %and1 = and i64 %2, 9223372036854775807 |
5115 | | // In this function we are looking to try to match RLDCL. However, the above |
5116 | | // DAG would better match RLDICL instead which is not what we are looking |
5117 | | // for here. |
5118 | 0 | SDValue RotateAmt = Val.getOperand(1); |
5119 | 0 | if (RotateAmt.getOpcode() == ISD::Constant) |
5120 | 0 | return false; |
5121 | | |
5122 | 0 | unsigned MB = 64 - llvm::countr_one(Imm64); |
5123 | 0 | SDLoc dl(N); |
5124 | 0 | SDValue Ops[] = {Val.getOperand(0), RotateAmt, getI32Imm(MB, dl)}; |
5125 | 0 | CurDAG->SelectNodeTo(N, PPC::RLDCL, MVT::i64, Ops); |
5126 | 0 | return true; |
5127 | 0 | } |
5128 | | |
5129 | 27.3k | bool PPCDAGToDAGISel::tryAsSingleRLDICL(SDNode *N) { |
5130 | 27.3k | assert(N->getOpcode() == ISD::AND && "ISD::AND SDNode expected"); |
5131 | 0 | uint64_t Imm64; |
5132 | 27.3k | if (!isInt64Immediate(N->getOperand(1).getNode(), Imm64) || !isMask_64(Imm64)) |
5133 | 14.3k | return false; |
5134 | | |
5135 | | // If this is a 64-bit zero-extension mask, emit rldicl. |
5136 | 12.9k | unsigned MB = 64 - llvm::countr_one(Imm64); |
5137 | 12.9k | unsigned SH = 0; |
5138 | 12.9k | unsigned Imm; |
5139 | 12.9k | SDValue Val = N->getOperand(0); |
5140 | 12.9k | SDLoc dl(N); |
5141 | | |
5142 | 12.9k | if (Val.getOpcode() == ISD::ANY_EXTEND) { |
5143 | 264 | auto Op0 = Val.getOperand(0); |
5144 | 264 | if (Op0.getOpcode() == ISD::SRL && |
5145 | 264 | isInt32Immediate(Op0.getOperand(1).getNode(), Imm) && Imm <= MB) { |
5146 | |
|
5147 | 0 | auto ResultType = Val.getNode()->getValueType(0); |
5148 | 0 | auto ImDef = CurDAG->getMachineNode(PPC::IMPLICIT_DEF, dl, ResultType); |
5149 | 0 | SDValue IDVal(ImDef, 0); |
5150 | |
|
5151 | 0 | Val = SDValue(CurDAG->getMachineNode(PPC::INSERT_SUBREG, dl, ResultType, |
5152 | 0 | IDVal, Op0.getOperand(0), |
5153 | 0 | getI32Imm(1, dl)), |
5154 | 0 | 0); |
5155 | 0 | SH = 64 - Imm; |
5156 | 0 | } |
5157 | 264 | } |
5158 | | |
5159 | | // If the operand is a logical right shift, we can fold it into this |
5160 | | // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb) |
5161 | | // for n <= mb. The right shift is really a left rotate followed by a |
5162 | | // mask, and this mask is a more-restrictive sub-mask of the mask implied |
5163 | | // by the shift. |
5164 | 12.9k | if (Val.getOpcode() == ISD::SRL && |
5165 | 12.9k | isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) { |
5166 | 0 | assert(Imm < 64 && "Illegal shift amount"); |
5167 | 0 | Val = Val.getOperand(0); |
5168 | 0 | SH = 64 - Imm; |
5169 | 0 | } |
5170 | | |
5171 | 0 | SDValue Ops[] = {Val, getI32Imm(SH, dl), getI32Imm(MB, dl)}; |
5172 | 12.9k | CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops); |
5173 | 12.9k | return true; |
5174 | 27.3k | } |
5175 | | |
5176 | 14.3k | bool PPCDAGToDAGISel::tryAsSingleRLDICR(SDNode *N) { |
5177 | 14.3k | assert(N->getOpcode() == ISD::AND && "ISD::AND SDNode expected"); |
5178 | 0 | uint64_t Imm64; |
5179 | 14.3k | if (!isInt64Immediate(N->getOperand(1).getNode(), Imm64) || |
5180 | 14.3k | !isMask_64(~Imm64)) |
5181 | 14.1k | return false; |
5182 | | |
5183 | | // If this is a negated 64-bit zero-extension mask, |
5184 | | // i.e. the immediate is a sequence of ones from most significant side |
5185 | | // and all zero for reminder, we should use rldicr. |
5186 | 270 | unsigned MB = 63 - llvm::countr_one(~Imm64); |
5187 | 270 | unsigned SH = 0; |
5188 | 270 | SDLoc dl(N); |
5189 | 270 | SDValue Ops[] = {N->getOperand(0), getI32Imm(SH, dl), getI32Imm(MB, dl)}; |
5190 | 270 | CurDAG->SelectNodeTo(N, PPC::RLDICR, MVT::i64, Ops); |
5191 | 270 | return true; |
5192 | 14.3k | } |
5193 | | |
5194 | 50.4k | bool PPCDAGToDAGISel::tryAsSingleRLDIMI(SDNode *N) { |
5195 | 50.4k | assert(N->getOpcode() == ISD::OR && "ISD::OR SDNode expected"); |
5196 | 0 | uint64_t Imm64; |
5197 | 50.4k | unsigned MB, ME; |
5198 | 50.4k | SDValue N0 = N->getOperand(0); |
5199 | | |
5200 | | // We won't get fewer instructions if the imm is 32-bit integer. |
5201 | | // rldimi requires the imm to have consecutive ones with both sides zero. |
5202 | | // Also, make sure the first Op has only one use, otherwise this may increase |
5203 | | // register pressure since rldimi is destructive. |
5204 | 50.4k | if (!isInt64Immediate(N->getOperand(1).getNode(), Imm64) || |
5205 | 50.4k | isUInt<32>(Imm64) || !isRunOfOnes64(Imm64, MB, ME) || !N0.hasOneUse()) |
5206 | 50.3k | return false; |
5207 | | |
5208 | 99 | unsigned SH = 63 - ME; |
5209 | 99 | SDLoc Dl(N); |
5210 | | // Use select64Imm for making LI instr instead of directly putting Imm64 |
5211 | 99 | SDValue Ops[] = { |
5212 | 99 | N->getOperand(0), |
5213 | 99 | SDValue(selectI64Imm(CurDAG, getI64Imm(-1, Dl).getNode()), 0), |
5214 | 99 | getI32Imm(SH, Dl), getI32Imm(MB, Dl)}; |
5215 | 99 | CurDAG->SelectNodeTo(N, PPC::RLDIMI, MVT::i64, Ops); |
5216 | 99 | return true; |
5217 | 50.4k | } |
5218 | | |
5219 | | // Select - Convert the specified operand from a target-independent to a |
5220 | | // target-specific node if it hasn't already been changed. |
5221 | 3.55M | void PPCDAGToDAGISel::Select(SDNode *N) { |
5222 | 3.55M | SDLoc dl(N); |
5223 | 3.55M | if (N->isMachineOpcode()) { |
5224 | 38 | N->setNodeId(-1); |
5225 | 38 | return; // Already selected. |
5226 | 38 | } |
5227 | | |
5228 | | // In case any misguided DAG-level optimizations form an ADD with a |
5229 | | // TargetConstant operand, crash here instead of miscompiling (by selecting |
5230 | | // an r+r add instead of some kind of r+i add). |
5231 | 3.55M | if (N->getOpcode() == ISD::ADD && |
5232 | 3.55M | N->getOperand(1).getOpcode() == ISD::TargetConstant) |
5233 | 0 | llvm_unreachable("Invalid ADD with TargetConstant operand"); |
5234 | | |
5235 | | // Try matching complex bit permutations before doing anything else. |
5236 | 3.55M | if (tryBitPermutation(N)) |
5237 | 42.0k | return; |
5238 | | |
5239 | | // Try to emit integer compares as GPR-only sequences (i.e. no use of CR). |
5240 | 3.50M | if (tryIntCompareInGPR(N)) |
5241 | 8.58k | return; |
5242 | | |
5243 | 3.50M | switch (N->getOpcode()) { |
5244 | 2.82M | default: break; |
5245 | | |
5246 | 2.82M | case ISD::Constant: |
5247 | 87.9k | if (N->getValueType(0) == MVT::i64) { |
5248 | 79.6k | ReplaceNode(N, selectI64Imm(CurDAG, N)); |
5249 | 79.6k | return; |
5250 | 79.6k | } |
5251 | 8.31k | break; |
5252 | | |
5253 | 8.31k | case ISD::INTRINSIC_VOID: { |
5254 | 520 | auto IntrinsicID = N->getConstantOperandVal(1); |
5255 | 520 | if (IntrinsicID != Intrinsic::ppc_tdw && IntrinsicID != Intrinsic::ppc_tw && |
5256 | 520 | IntrinsicID != Intrinsic::ppc_trapd && |
5257 | 520 | IntrinsicID != Intrinsic::ppc_trap) |
5258 | 520 | break; |
5259 | 0 | unsigned Opcode = (IntrinsicID == Intrinsic::ppc_tdw || |
5260 | 0 | IntrinsicID == Intrinsic::ppc_trapd) |
5261 | 0 | ? PPC::TDI |
5262 | 0 | : PPC::TWI; |
5263 | 0 | SmallVector<SDValue, 4> OpsWithMD; |
5264 | 0 | unsigned MDIndex; |
5265 | 0 | if (IntrinsicID == Intrinsic::ppc_tdw || |
5266 | 0 | IntrinsicID == Intrinsic::ppc_tw) { |
5267 | 0 | SDValue Ops[] = {N->getOperand(4), N->getOperand(2), N->getOperand(3)}; |
5268 | 0 | int16_t SImmOperand2; |
5269 | 0 | int16_t SImmOperand3; |
5270 | 0 | int16_t SImmOperand4; |
5271 | 0 | bool isOperand2IntS16Immediate = |
5272 | 0 | isIntS16Immediate(N->getOperand(2), SImmOperand2); |
5273 | 0 | bool isOperand3IntS16Immediate = |
5274 | 0 | isIntS16Immediate(N->getOperand(3), SImmOperand3); |
5275 | | // We will emit PPC::TD or PPC::TW if the 2nd and 3rd operands are reg + |
5276 | | // reg or imm + imm. The imm + imm form will be optimized to either an |
5277 | | // unconditional trap or a nop in a later pass. |
5278 | 0 | if (isOperand2IntS16Immediate == isOperand3IntS16Immediate) |
5279 | 0 | Opcode = IntrinsicID == Intrinsic::ppc_tdw ? PPC::TD : PPC::TW; |
5280 | 0 | else if (isOperand3IntS16Immediate) |
5281 | | // The 2nd and 3rd operands are reg + imm. |
5282 | 0 | Ops[2] = getI32Imm(int(SImmOperand3) & 0xFFFF, dl); |
5283 | 0 | else { |
5284 | | // The 2nd and 3rd operands are imm + reg. |
5285 | 0 | bool isOperand4IntS16Immediate = |
5286 | 0 | isIntS16Immediate(N->getOperand(4), SImmOperand4); |
5287 | 0 | (void)isOperand4IntS16Immediate; |
5288 | 0 | assert(isOperand4IntS16Immediate && |
5289 | 0 | "The 4th operand is not an Immediate"); |
5290 | | // We need to flip the condition immediate TO. |
5291 | 0 | int16_t TO = int(SImmOperand4) & 0x1F; |
5292 | | // We swap the first and second bit of TO if they are not same. |
5293 | 0 | if ((TO & 0x1) != ((TO & 0x2) >> 1)) |
5294 | 0 | TO = (TO & 0x1) ? TO + 1 : TO - 1; |
5295 | | // We swap the fourth and fifth bit of TO if they are not same. |
5296 | 0 | if ((TO & 0x8) != ((TO & 0x10) >> 1)) |
5297 | 0 | TO = (TO & 0x8) ? TO + 8 : TO - 8; |
5298 | 0 | Ops[0] = getI32Imm(TO, dl); |
5299 | 0 | Ops[1] = N->getOperand(3); |
5300 | 0 | Ops[2] = getI32Imm(int(SImmOperand2) & 0xFFFF, dl); |
5301 | 0 | } |
5302 | 0 | OpsWithMD = {Ops[0], Ops[1], Ops[2]}; |
5303 | 0 | MDIndex = 5; |
5304 | 0 | } else { |
5305 | 0 | OpsWithMD = {getI32Imm(24, dl), N->getOperand(2), getI32Imm(0, dl)}; |
5306 | 0 | MDIndex = 3; |
5307 | 0 | } |
5308 | | |
5309 | 0 | if (N->getNumOperands() > MDIndex) { |
5310 | 0 | SDValue MDV = N->getOperand(MDIndex); |
5311 | 0 | const MDNode *MD = cast<MDNodeSDNode>(MDV)->getMD(); |
5312 | 0 | assert(MD->getNumOperands() != 0 && "Empty MDNode in operands!"); |
5313 | 0 | assert((isa<MDString>(MD->getOperand(0)) && cast<MDString>( |
5314 | 0 | MD->getOperand(0))->getString().equals("ppc-trap-reason")) |
5315 | 0 | && "Unsupported annotation data type!"); |
5316 | 0 | for (unsigned i = 1; i < MD->getNumOperands(); i++) { |
5317 | 0 | assert(isa<MDString>(MD->getOperand(i)) && |
5318 | 0 | "Invalid data type for annotation ppc-trap-reason!"); |
5319 | 0 | OpsWithMD.push_back( |
5320 | 0 | getI32Imm(std::stoi(cast<MDString>( |
5321 | 0 | MD->getOperand(i))->getString().str()), dl)); |
5322 | 0 | } |
5323 | 0 | } |
5324 | 0 | OpsWithMD.push_back(N->getOperand(0)); // chain |
5325 | 0 | CurDAG->SelectNodeTo(N, Opcode, MVT::Other, OpsWithMD); |
5326 | 0 | return; |
5327 | 520 | } |
5328 | | |
5329 | 0 | case ISD::INTRINSIC_WO_CHAIN: { |
5330 | | // We emit the PPC::FSELS instruction here because of type conflicts with |
5331 | | // the comparison operand. The FSELS instruction is defined to use an 8-byte |
5332 | | // comparison like the FSELD version. The fsels intrinsic takes a 4-byte |
5333 | | // value for the comparison. When selecting through a .td file, a type |
5334 | | // error is raised. Must check this first so we never break on the |
5335 | | // !Subtarget->isISA3_1() check. |
5336 | 0 | auto IntID = N->getConstantOperandVal(0); |
5337 | 0 | if (IntID == Intrinsic::ppc_fsels) { |
5338 | 0 | SDValue Ops[] = {N->getOperand(1), N->getOperand(2), N->getOperand(3)}; |
5339 | 0 | CurDAG->SelectNodeTo(N, PPC::FSELS, MVT::f32, Ops); |
5340 | 0 | return; |
5341 | 0 | } |
5342 | | |
5343 | 0 | if (IntID == Intrinsic::ppc_bcdadd_p || IntID == Intrinsic::ppc_bcdsub_p) { |
5344 | 0 | auto Pred = N->getConstantOperandVal(1); |
5345 | 0 | unsigned Opcode = |
5346 | 0 | IntID == Intrinsic::ppc_bcdadd_p ? PPC::BCDADD_rec : PPC::BCDSUB_rec; |
5347 | 0 | unsigned SubReg = 0; |
5348 | 0 | unsigned ShiftVal = 0; |
5349 | 0 | bool Reverse = false; |
5350 | 0 | switch (Pred) { |
5351 | 0 | case 0: |
5352 | 0 | SubReg = PPC::sub_eq; |
5353 | 0 | ShiftVal = 1; |
5354 | 0 | break; |
5355 | 0 | case 1: |
5356 | 0 | SubReg = PPC::sub_eq; |
5357 | 0 | ShiftVal = 1; |
5358 | 0 | Reverse = true; |
5359 | 0 | break; |
5360 | 0 | case 2: |
5361 | 0 | SubReg = PPC::sub_lt; |
5362 | 0 | ShiftVal = 3; |
5363 | 0 | break; |
5364 | 0 | case 3: |
5365 | 0 | SubReg = PPC::sub_lt; |
5366 | 0 | ShiftVal = 3; |
5367 | 0 | Reverse = true; |
5368 | 0 | break; |
5369 | 0 | case 4: |
5370 | 0 | SubReg = PPC::sub_gt; |
5371 | 0 | ShiftVal = 2; |
5372 | 0 | break; |
5373 | 0 | case 5: |
5374 | 0 | SubReg = PPC::sub_gt; |
5375 | 0 | ShiftVal = 2; |
5376 | 0 | Reverse = true; |
5377 | 0 | break; |
5378 | 0 | case 6: |
5379 | 0 | SubReg = PPC::sub_un; |
5380 | 0 | break; |
5381 | 0 | case 7: |
5382 | 0 | SubReg = PPC::sub_un; |
5383 | 0 | Reverse = true; |
5384 | 0 | break; |
5385 | 0 | } |
5386 | | |
5387 | 0 | EVT VTs[] = {MVT::v16i8, MVT::Glue}; |
5388 | 0 | SDValue Ops[] = {N->getOperand(2), N->getOperand(3), |
5389 | 0 | CurDAG->getTargetConstant(0, dl, MVT::i32)}; |
5390 | 0 | SDValue BCDOp = SDValue(CurDAG->getMachineNode(Opcode, dl, VTs, Ops), 0); |
5391 | 0 | SDValue CR6Reg = CurDAG->getRegister(PPC::CR6, MVT::i32); |
5392 | | // On Power10, we can use SETBC[R]. On prior architectures, we have to use |
5393 | | // MFOCRF and shift/negate the value. |
5394 | 0 | if (Subtarget->isISA3_1()) { |
5395 | 0 | SDValue SubRegIdx = CurDAG->getTargetConstant(SubReg, dl, MVT::i32); |
5396 | 0 | SDValue CRBit = SDValue( |
5397 | 0 | CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, dl, MVT::i1, |
5398 | 0 | CR6Reg, SubRegIdx, BCDOp.getValue(1)), |
5399 | 0 | 0); |
5400 | 0 | CurDAG->SelectNodeTo(N, Reverse ? PPC::SETBCR : PPC::SETBC, MVT::i32, |
5401 | 0 | CRBit); |
5402 | 0 | } else { |
5403 | 0 | SDValue Move = |
5404 | 0 | SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR6Reg, |
5405 | 0 | BCDOp.getValue(1)), |
5406 | 0 | 0); |
5407 | 0 | SDValue Ops[] = {Move, getI32Imm((32 - (4 + ShiftVal)) & 31, dl), |
5408 | 0 | getI32Imm(31, dl), getI32Imm(31, dl)}; |
5409 | 0 | if (!Reverse) |
5410 | 0 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
5411 | 0 | else { |
5412 | 0 | SDValue Shift = SDValue( |
5413 | 0 | CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0); |
5414 | 0 | CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Shift, getI32Imm(1, dl)); |
5415 | 0 | } |
5416 | 0 | } |
5417 | 0 | return; |
5418 | 0 | } |
5419 | | |
5420 | 0 | if (!Subtarget->isISA3_1()) |
5421 | 0 | break; |
5422 | 0 | unsigned Opcode = 0; |
5423 | 0 | switch (IntID) { |
5424 | 0 | default: |
5425 | 0 | break; |
5426 | 0 | case Intrinsic::ppc_altivec_vstribr_p: |
5427 | 0 | Opcode = PPC::VSTRIBR_rec; |
5428 | 0 | break; |
5429 | 0 | case Intrinsic::ppc_altivec_vstribl_p: |
5430 | 0 | Opcode = PPC::VSTRIBL_rec; |
5431 | 0 | break; |
5432 | 0 | case Intrinsic::ppc_altivec_vstrihr_p: |
5433 | 0 | Opcode = PPC::VSTRIHR_rec; |
5434 | 0 | break; |
5435 | 0 | case Intrinsic::ppc_altivec_vstrihl_p: |
5436 | 0 | Opcode = PPC::VSTRIHL_rec; |
5437 | 0 | break; |
5438 | 0 | } |
5439 | 0 | if (!Opcode) |
5440 | 0 | break; |
5441 | | |
5442 | | // Generate the appropriate vector string isolate intrinsic to match. |
5443 | 0 | EVT VTs[] = {MVT::v16i8, MVT::Glue}; |
5444 | 0 | SDValue VecStrOp = |
5445 | 0 | SDValue(CurDAG->getMachineNode(Opcode, dl, VTs, N->getOperand(2)), 0); |
5446 | | // Vector string isolate instructions update the EQ bit of CR6. |
5447 | | // Generate a SETBC instruction to extract the bit and place it in a GPR. |
5448 | 0 | SDValue SubRegIdx = CurDAG->getTargetConstant(PPC::sub_eq, dl, MVT::i32); |
5449 | 0 | SDValue CR6Reg = CurDAG->getRegister(PPC::CR6, MVT::i32); |
5450 | 0 | SDValue CRBit = SDValue( |
5451 | 0 | CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, dl, MVT::i1, |
5452 | 0 | CR6Reg, SubRegIdx, VecStrOp.getValue(1)), |
5453 | 0 | 0); |
5454 | 0 | CurDAG->SelectNodeTo(N, PPC::SETBC, MVT::i32, CRBit); |
5455 | 0 | return; |
5456 | 0 | } |
5457 | | |
5458 | 24.3k | case ISD::SETCC: |
5459 | 24.3k | case ISD::STRICT_FSETCC: |
5460 | 24.3k | case ISD::STRICT_FSETCCS: |
5461 | 24.3k | if (trySETCC(N)) |
5462 | 0 | return; |
5463 | 24.3k | break; |
5464 | | // These nodes will be transformed into GETtlsADDR32 node, which |
5465 | | // later becomes BL_TLS __tls_get_addr(sym at tlsgd)@PLT |
5466 | 24.3k | case PPCISD::ADDI_TLSLD_L_ADDR: |
5467 | 11 | case PPCISD::ADDI_TLSGD_L_ADDR: { |
5468 | 11 | const Module *Mod = MF->getFunction().getParent(); |
5469 | 11 | if (PPCLowering->getPointerTy(CurDAG->getDataLayout()) != MVT::i32 || |
5470 | 11 | !Subtarget->isSecurePlt() || !Subtarget->isTargetELF() || |
5471 | 11 | Mod->getPICLevel() == PICLevel::SmallPIC) |
5472 | 11 | break; |
5473 | | // Attach global base pointer on GETtlsADDR32 node in order to |
5474 | | // generate secure plt code for TLS symbols. |
5475 | 0 | getGlobalBaseReg(); |
5476 | 0 | } break; |
5477 | 36 | case PPCISD::CALL: { |
5478 | 36 | if (PPCLowering->getPointerTy(CurDAG->getDataLayout()) != MVT::i32 || |
5479 | 36 | !TM.isPositionIndependent() || !Subtarget->isSecurePlt() || |
5480 | 36 | !Subtarget->isTargetELF()) |
5481 | 36 | break; |
5482 | | |
5483 | 0 | SDValue Op = N->getOperand(1); |
5484 | |
|
5485 | 0 | if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op)) { |
5486 | 0 | if (GA->getTargetFlags() == PPCII::MO_PLT) |
5487 | 0 | getGlobalBaseReg(); |
5488 | 0 | } |
5489 | 0 | else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) { |
5490 | 0 | if (ES->getTargetFlags() == PPCII::MO_PLT) |
5491 | 0 | getGlobalBaseReg(); |
5492 | 0 | } |
5493 | 0 | } |
5494 | 0 | break; |
5495 | | |
5496 | 0 | case PPCISD::GlobalBaseReg: |
5497 | 0 | ReplaceNode(N, getGlobalBaseReg()); |
5498 | 0 | return; |
5499 | | |
5500 | 15.6k | case ISD::FrameIndex: |
5501 | 15.6k | selectFrameIndex(N, N); |
5502 | 15.6k | return; |
5503 | | |
5504 | 0 | case PPCISD::MFOCRF: { |
5505 | 0 | SDValue InGlue = N->getOperand(1); |
5506 | 0 | ReplaceNode(N, CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, |
5507 | 0 | N->getOperand(0), InGlue)); |
5508 | 0 | return; |
5509 | 36 | } |
5510 | | |
5511 | 0 | case PPCISD::READ_TIME_BASE: |
5512 | 0 | ReplaceNode(N, CurDAG->getMachineNode(PPC::ReadTB, dl, MVT::i32, MVT::i32, |
5513 | 0 | MVT::Other, N->getOperand(0))); |
5514 | 0 | return; |
5515 | | |
5516 | 250 | case PPCISD::SRA_ADDZE: { |
5517 | 250 | SDValue N0 = N->getOperand(0); |
5518 | 250 | SDValue ShiftAmt = |
5519 | 250 | CurDAG->getTargetConstant(*cast<ConstantSDNode>(N->getOperand(1))-> |
5520 | 250 | getConstantIntValue(), dl, |
5521 | 250 | N->getValueType(0)); |
5522 | 250 | if (N->getValueType(0) == MVT::i64) { |
5523 | 32 | SDNode *Op = |
5524 | 32 | CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, MVT::Glue, |
5525 | 32 | N0, ShiftAmt); |
5526 | 32 | CurDAG->SelectNodeTo(N, PPC::ADDZE8, MVT::i64, SDValue(Op, 0), |
5527 | 32 | SDValue(Op, 1)); |
5528 | 32 | return; |
5529 | 218 | } else { |
5530 | 218 | assert(N->getValueType(0) == MVT::i32 && |
5531 | 218 | "Expecting i64 or i32 in PPCISD::SRA_ADDZE"); |
5532 | 0 | SDNode *Op = |
5533 | 218 | CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue, |
5534 | 218 | N0, ShiftAmt); |
5535 | 218 | CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, SDValue(Op, 0), |
5536 | 218 | SDValue(Op, 1)); |
5537 | 218 | return; |
5538 | 218 | } |
5539 | 250 | } |
5540 | | |
5541 | 202k | case ISD::STORE: { |
5542 | | // Change TLS initial-exec (or TLS local-exec on AIX) D-form stores to |
5543 | | // X-form stores. |
5544 | 202k | StoreSDNode *ST = cast<StoreSDNode>(N); |
5545 | 202k | if (EnableTLSOpt && (Subtarget->isELFv2ABI() || Subtarget->isAIXABI()) && |
5546 | 202k | ST->getAddressingMode() != ISD::PRE_INC) |
5547 | 0 | if (tryTLSXFormStore(ST)) |
5548 | 0 | return; |
5549 | 202k | break; |
5550 | 202k | } |
5551 | 202k | case ISD::LOAD: { |
5552 | | // Handle preincrement loads. |
5553 | 107k | LoadSDNode *LD = cast<LoadSDNode>(N); |
5554 | 107k | EVT LoadedVT = LD->getMemoryVT(); |
5555 | | |
5556 | | // Normal loads are handled by code generated from the .td file. |
5557 | 107k | if (LD->getAddressingMode() != ISD::PRE_INC) { |
5558 | | // Change TLS initial-exec (or TLS local-exec on AIX) D-form loads to |
5559 | | // X-form loads. |
5560 | 103k | if (EnableTLSOpt && (Subtarget->isELFv2ABI() || Subtarget->isAIXABI())) |
5561 | 0 | if (tryTLSXFormLoad(LD)) |
5562 | 0 | return; |
5563 | 103k | break; |
5564 | 103k | } |
5565 | | |
5566 | 4.30k | SDValue Offset = LD->getOffset(); |
5567 | 4.30k | if (Offset.getOpcode() == ISD::TargetConstant || |
5568 | 4.30k | Offset.getOpcode() == ISD::TargetGlobalAddress) { |
5569 | | |
5570 | 562 | unsigned Opcode; |
5571 | 562 | bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD; |
5572 | 562 | if (LD->getValueType(0) != MVT::i64) { |
5573 | | // Handle PPC32 integer and normal FP loads. |
5574 | 277 | assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load"); |
5575 | 0 | switch (LoadedVT.getSimpleVT().SimpleTy) { |
5576 | 0 | default: llvm_unreachable("Invalid PPC load type!"); |
5577 | 43 | case MVT::f64: Opcode = PPC::LFDU; break; |
5578 | 79 | case MVT::f32: Opcode = PPC::LFSU; break; |
5579 | 68 | case MVT::i32: Opcode = PPC::LWZU; break; |
5580 | 15 | case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break; |
5581 | 0 | case MVT::i1: |
5582 | 72 | case MVT::i8: Opcode = PPC::LBZU; break; |
5583 | 277 | } |
5584 | 285 | } else { |
5585 | 285 | assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!"); |
5586 | 0 | assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load"); |
5587 | 0 | switch (LoadedVT.getSimpleVT().SimpleTy) { |
5588 | 0 | default: llvm_unreachable("Invalid PPC load type!"); |
5589 | 133 | case MVT::i64: Opcode = PPC::LDU; break; |
5590 | 27 | case MVT::i32: Opcode = PPC::LWZU8; break; |
5591 | 26 | case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break; |
5592 | 0 | case MVT::i1: |
5593 | 99 | case MVT::i8: Opcode = PPC::LBZU8; break; |
5594 | 285 | } |
5595 | 285 | } |
5596 | | |
5597 | 562 | SDValue Chain = LD->getChain(); |
5598 | 562 | SDValue Base = LD->getBasePtr(); |
5599 | 562 | SDValue Ops[] = { Offset, Base, Chain }; |
5600 | 562 | SDNode *MN = CurDAG->getMachineNode( |
5601 | 562 | Opcode, dl, LD->getValueType(0), |
5602 | 562 | PPCLowering->getPointerTy(CurDAG->getDataLayout()), MVT::Other, Ops); |
5603 | 562 | transferMemOperands(N, MN); |
5604 | 562 | ReplaceNode(N, MN); |
5605 | 562 | return; |
5606 | 3.74k | } else { |
5607 | 3.74k | unsigned Opcode; |
5608 | 3.74k | bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD; |
5609 | 3.74k | if (LD->getValueType(0) != MVT::i64) { |
5610 | | // Handle PPC32 integer and normal FP loads. |
5611 | 766 | assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load"); |
5612 | 0 | switch (LoadedVT.getSimpleVT().SimpleTy) { |
5613 | 0 | default: llvm_unreachable("Invalid PPC load type!"); |
5614 | 167 | case MVT::f64: Opcode = PPC::LFDUX; break; |
5615 | 104 | case MVT::f32: Opcode = PPC::LFSUX; break; |
5616 | 217 | case MVT::i32: Opcode = PPC::LWZUX; break; |
5617 | 131 | case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break; |
5618 | 1 | case MVT::i1: |
5619 | 147 | case MVT::i8: Opcode = PPC::LBZUX; break; |
5620 | 766 | } |
5621 | 2.97k | } else { |
5622 | 2.97k | assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!"); |
5623 | 0 | assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) && |
5624 | 2.97k | "Invalid sext update load"); |
5625 | 0 | switch (LoadedVT.getSimpleVT().SimpleTy) { |
5626 | 0 | default: llvm_unreachable("Invalid PPC load type!"); |
5627 | 2.55k | case MVT::i64: Opcode = PPC::LDUX; break; |
5628 | 129 | case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break; |
5629 | 47 | case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break; |
5630 | 1 | case MVT::i1: |
5631 | 251 | case MVT::i8: Opcode = PPC::LBZUX8; break; |
5632 | 2.97k | } |
5633 | 2.97k | } |
5634 | | |
5635 | 3.74k | SDValue Chain = LD->getChain(); |
5636 | 3.74k | SDValue Base = LD->getBasePtr(); |
5637 | 3.74k | SDValue Ops[] = { Base, Offset, Chain }; |
5638 | 3.74k | SDNode *MN = CurDAG->getMachineNode( |
5639 | 3.74k | Opcode, dl, LD->getValueType(0), |
5640 | 3.74k | PPCLowering->getPointerTy(CurDAG->getDataLayout()), MVT::Other, Ops); |
5641 | 3.74k | transferMemOperands(N, MN); |
5642 | 3.74k | ReplaceNode(N, MN); |
5643 | 3.74k | return; |
5644 | 3.74k | } |
5645 | 4.30k | } |
5646 | | |
5647 | 44.7k | case ISD::AND: |
5648 | | // If this is an 'and' with a mask, try to emit rlwinm/rldicl/rldicr |
5649 | 44.7k | if (tryAsSingleRLWINM(N) || tryAsSingleRLWIMI(N) || tryAsSingleRLDCL(N) || |
5650 | 44.7k | tryAsSingleRLDICL(N) || tryAsSingleRLDICR(N) || tryAsSingleRLWINM8(N) || |
5651 | 44.7k | tryAsPairOfRLDICL(N)) |
5652 | 38.0k | return; |
5653 | | |
5654 | | // Other cases are autogenerated. |
5655 | 6.70k | break; |
5656 | 56.6k | case ISD::OR: { |
5657 | 56.6k | if (N->getValueType(0) == MVT::i32) |
5658 | 7.23k | if (tryBitfieldInsert(N)) |
5659 | 6.17k | return; |
5660 | | |
5661 | 50.4k | int16_t Imm; |
5662 | 50.4k | if (N->getOperand(0)->getOpcode() == ISD::FrameIndex && |
5663 | 50.4k | isIntS16Immediate(N->getOperand(1), Imm)) { |
5664 | 20 | KnownBits LHSKnown = CurDAG->computeKnownBits(N->getOperand(0)); |
5665 | | |
5666 | | // If this is equivalent to an add, then we can fold it with the |
5667 | | // FrameIndex calculation. |
5668 | 20 | if ((LHSKnown.Zero.getZExtValue()|~(uint64_t)Imm) == ~0ULL) { |
5669 | 20 | selectFrameIndex(N, N->getOperand(0).getNode(), (int64_t)Imm); |
5670 | 20 | return; |
5671 | 20 | } |
5672 | 20 | } |
5673 | | |
5674 | | // If this is 'or' against an imm with consecutive ones and both sides zero, |
5675 | | // try to emit rldimi |
5676 | 50.4k | if (tryAsSingleRLDIMI(N)) |
5677 | 99 | return; |
5678 | | |
5679 | | // OR with a 32-bit immediate can be handled by ori + oris |
5680 | | // without creating an immediate in a GPR. |
5681 | 50.3k | uint64_t Imm64 = 0; |
5682 | 50.3k | bool IsPPC64 = Subtarget->isPPC64(); |
5683 | 50.3k | if (IsPPC64 && isInt64Immediate(N->getOperand(1), Imm64) && |
5684 | 50.3k | (Imm64 & ~0xFFFFFFFFuLL) == 0) { |
5685 | | // If ImmHi (ImmHi) is zero, only one ori (oris) is generated later. |
5686 | 746 | uint64_t ImmHi = Imm64 >> 16; |
5687 | 746 | uint64_t ImmLo = Imm64 & 0xFFFF; |
5688 | 746 | if (ImmHi != 0 && ImmLo != 0) { |
5689 | 29 | SDNode *Lo = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64, |
5690 | 29 | N->getOperand(0), |
5691 | 29 | getI16Imm(ImmLo, dl)); |
5692 | 29 | SDValue Ops1[] = { SDValue(Lo, 0), getI16Imm(ImmHi, dl)}; |
5693 | 29 | CurDAG->SelectNodeTo(N, PPC::ORIS8, MVT::i64, Ops1); |
5694 | 29 | return; |
5695 | 29 | } |
5696 | 746 | } |
5697 | | |
5698 | | // Other cases are autogenerated. |
5699 | 50.3k | break; |
5700 | 50.3k | } |
5701 | 50.3k | case ISD::XOR: { |
5702 | | // XOR with a 32-bit immediate can be handled by xori + xoris |
5703 | | // without creating an immediate in a GPR. |
5704 | 14.0k | uint64_t Imm64 = 0; |
5705 | 14.0k | bool IsPPC64 = Subtarget->isPPC64(); |
5706 | 14.0k | if (IsPPC64 && isInt64Immediate(N->getOperand(1), Imm64) && |
5707 | 14.0k | (Imm64 & ~0xFFFFFFFFuLL) == 0) { |
5708 | | // If ImmHi (ImmHi) is zero, only one xori (xoris) is generated later. |
5709 | 715 | uint64_t ImmHi = Imm64 >> 16; |
5710 | 715 | uint64_t ImmLo = Imm64 & 0xFFFF; |
5711 | 715 | if (ImmHi != 0 && ImmLo != 0) { |
5712 | 37 | SDNode *Lo = CurDAG->getMachineNode(PPC::XORI8, dl, MVT::i64, |
5713 | 37 | N->getOperand(0), |
5714 | 37 | getI16Imm(ImmLo, dl)); |
5715 | 37 | SDValue Ops1[] = { SDValue(Lo, 0), getI16Imm(ImmHi, dl)}; |
5716 | 37 | CurDAG->SelectNodeTo(N, PPC::XORIS8, MVT::i64, Ops1); |
5717 | 37 | return; |
5718 | 37 | } |
5719 | 715 | } |
5720 | | |
5721 | 13.9k | break; |
5722 | 14.0k | } |
5723 | 28.5k | case ISD::ADD: { |
5724 | 28.5k | int16_t Imm; |
5725 | 28.5k | if (N->getOperand(0)->getOpcode() == ISD::FrameIndex && |
5726 | 28.5k | isIntS16Immediate(N->getOperand(1), Imm)) { |
5727 | 4.28k | selectFrameIndex(N, N->getOperand(0).getNode(), (int64_t)Imm); |
5728 | 4.28k | return; |
5729 | 4.28k | } |
5730 | | |
5731 | 24.2k | break; |
5732 | 28.5k | } |
5733 | 24.2k | case ISD::SHL: { |
5734 | 13.6k | unsigned Imm, SH, MB, ME; |
5735 | 13.6k | if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) && |
5736 | 13.6k | isRotateAndMask(N, Imm, true, SH, MB, ME)) { |
5737 | 0 | SDValue Ops[] = { N->getOperand(0).getOperand(0), |
5738 | 0 | getI32Imm(SH, dl), getI32Imm(MB, dl), |
5739 | 0 | getI32Imm(ME, dl) }; |
5740 | 0 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
5741 | 0 | return; |
5742 | 0 | } |
5743 | | |
5744 | | // Other cases are autogenerated. |
5745 | 13.6k | break; |
5746 | 13.6k | } |
5747 | 13.6k | case ISD::SRL: { |
5748 | 10.7k | unsigned Imm, SH, MB, ME; |
5749 | 10.7k | if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) && |
5750 | 10.7k | isRotateAndMask(N, Imm, true, SH, MB, ME)) { |
5751 | 0 | SDValue Ops[] = { N->getOperand(0).getOperand(0), |
5752 | 0 | getI32Imm(SH, dl), getI32Imm(MB, dl), |
5753 | 0 | getI32Imm(ME, dl) }; |
5754 | 0 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
5755 | 0 | return; |
5756 | 0 | } |
5757 | | |
5758 | | // Other cases are autogenerated. |
5759 | 10.7k | break; |
5760 | 10.7k | } |
5761 | 14.9k | case ISD::MUL: { |
5762 | 14.9k | SDValue Op1 = N->getOperand(1); |
5763 | 14.9k | if (Op1.getOpcode() != ISD::Constant || |
5764 | 14.9k | (Op1.getValueType() != MVT::i64 && Op1.getValueType() != MVT::i32)) |
5765 | 13.3k | break; |
5766 | | |
5767 | | // If the multiplier fits int16, we can handle it with mulli. |
5768 | 1.61k | int64_t Imm = Op1->getAsZExtVal(); |
5769 | 1.61k | unsigned Shift = llvm::countr_zero<uint64_t>(Imm); |
5770 | 1.61k | if (isInt<16>(Imm) || !Shift) |
5771 | 1.46k | break; |
5772 | | |
5773 | | // If the shifted value fits int16, we can do this transformation: |
5774 | | // (mul X, c1 << c2) -> (rldicr (mulli X, c1) c2). We do this in ISEL due to |
5775 | | // DAGCombiner prefers (shl (mul X, c1), c2) -> (mul X, c1 << c2). |
5776 | 157 | uint64_t ImmSh = Imm >> Shift; |
5777 | 157 | if (!isInt<16>(ImmSh)) |
5778 | 32 | break; |
5779 | | |
5780 | 125 | uint64_t SextImm = SignExtend64(ImmSh & 0xFFFF, 16); |
5781 | 125 | if (Op1.getValueType() == MVT::i64) { |
5782 | 124 | SDValue SDImm = CurDAG->getTargetConstant(SextImm, dl, MVT::i64); |
5783 | 124 | SDNode *MulNode = CurDAG->getMachineNode(PPC::MULLI8, dl, MVT::i64, |
5784 | 124 | N->getOperand(0), SDImm); |
5785 | | |
5786 | 124 | SDValue Ops[] = {SDValue(MulNode, 0), getI32Imm(Shift, dl), |
5787 | 124 | getI32Imm(63 - Shift, dl)}; |
5788 | 124 | CurDAG->SelectNodeTo(N, PPC::RLDICR, MVT::i64, Ops); |
5789 | 124 | return; |
5790 | 124 | } else { |
5791 | 1 | SDValue SDImm = CurDAG->getTargetConstant(SextImm, dl, MVT::i32); |
5792 | 1 | SDNode *MulNode = CurDAG->getMachineNode(PPC::MULLI, dl, MVT::i32, |
5793 | 1 | N->getOperand(0), SDImm); |
5794 | | |
5795 | 1 | SDValue Ops[] = {SDValue(MulNode, 0), getI32Imm(Shift, dl), |
5796 | 1 | getI32Imm(0, dl), getI32Imm(31 - Shift, dl)}; |
5797 | 1 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
5798 | 1 | return; |
5799 | 1 | } |
5800 | 0 | break; |
5801 | 125 | } |
5802 | | // FIXME: Remove this once the ANDI glue bug is fixed: |
5803 | 0 | case PPCISD::ANDI_rec_1_EQ_BIT: |
5804 | 0 | case PPCISD::ANDI_rec_1_GT_BIT: { |
5805 | 0 | if (!ANDIGlueBug) |
5806 | 0 | break; |
5807 | | |
5808 | 0 | EVT InVT = N->getOperand(0).getValueType(); |
5809 | 0 | assert((InVT == MVT::i64 || InVT == MVT::i32) && |
5810 | 0 | "Invalid input type for ANDI_rec_1_EQ_BIT"); |
5811 | | |
5812 | 0 | unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDI8_rec : PPC::ANDI_rec; |
5813 | 0 | SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue, |
5814 | 0 | N->getOperand(0), |
5815 | 0 | CurDAG->getTargetConstant(1, dl, InVT)), |
5816 | 0 | 0); |
5817 | 0 | SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32); |
5818 | 0 | SDValue SRIdxVal = CurDAG->getTargetConstant( |
5819 | 0 | N->getOpcode() == PPCISD::ANDI_rec_1_EQ_BIT ? PPC::sub_eq : PPC::sub_gt, |
5820 | 0 | dl, MVT::i32); |
5821 | |
|
5822 | 0 | CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1, CR0Reg, |
5823 | 0 | SRIdxVal, SDValue(AndI.getNode(), 1) /* glue */); |
5824 | 0 | return; |
5825 | 0 | } |
5826 | 8.42k | case ISD::SELECT_CC: { |
5827 | 8.42k | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get(); |
5828 | 8.42k | EVT PtrVT = |
5829 | 8.42k | CurDAG->getTargetLoweringInfo().getPointerTy(CurDAG->getDataLayout()); |
5830 | 8.42k | bool isPPC64 = (PtrVT == MVT::i64); |
5831 | | |
5832 | | // If this is a select of i1 operands, we'll pattern match it. |
5833 | 8.42k | if (Subtarget->useCRBits() && N->getOperand(0).getValueType() == MVT::i1) |
5834 | 145 | break; |
5835 | | |
5836 | 8.27k | if (Subtarget->isISA3_0() && Subtarget->isPPC64()) { |
5837 | 0 | bool NeedSwapOps = false; |
5838 | 0 | bool IsUnCmp = false; |
5839 | 0 | if (mayUseP9Setb(N, CC, CurDAG, NeedSwapOps, IsUnCmp)) { |
5840 | 0 | SDValue LHS = N->getOperand(0); |
5841 | 0 | SDValue RHS = N->getOperand(1); |
5842 | 0 | if (NeedSwapOps) |
5843 | 0 | std::swap(LHS, RHS); |
5844 | | |
5845 | | // Make use of SelectCC to generate the comparison to set CR bits, for |
5846 | | // equality comparisons having one literal operand, SelectCC probably |
5847 | | // doesn't need to materialize the whole literal and just use xoris to |
5848 | | // check it first, it leads the following comparison result can't |
5849 | | // exactly represent GT/LT relationship. So to avoid this we specify |
5850 | | // SETGT/SETUGT here instead of SETEQ. |
5851 | 0 | SDValue GenCC = |
5852 | 0 | SelectCC(LHS, RHS, IsUnCmp ? ISD::SETUGT : ISD::SETGT, dl); |
5853 | 0 | CurDAG->SelectNodeTo( |
5854 | 0 | N, N->getSimpleValueType(0) == MVT::i64 ? PPC::SETB8 : PPC::SETB, |
5855 | 0 | N->getValueType(0), GenCC); |
5856 | 0 | NumP9Setb++; |
5857 | 0 | return; |
5858 | 0 | } |
5859 | 0 | } |
5860 | | |
5861 | | // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc |
5862 | 8.27k | if (!isPPC64 && isNullConstant(N->getOperand(1)) && |
5863 | 8.27k | isOneConstant(N->getOperand(2)) && isNullConstant(N->getOperand(3)) && |
5864 | 8.27k | CC == ISD::SETNE && |
5865 | | // FIXME: Implement this optzn for PPC64. |
5866 | 8.27k | N->getValueType(0) == MVT::i32) { |
5867 | 0 | SDNode *Tmp = |
5868 | 0 | CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue, |
5869 | 0 | N->getOperand(0), getI32Imm(~0U, dl)); |
5870 | 0 | CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(Tmp, 0), |
5871 | 0 | N->getOperand(0), SDValue(Tmp, 1)); |
5872 | 0 | return; |
5873 | 0 | } |
5874 | | |
5875 | 8.27k | SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl); |
5876 | | |
5877 | 8.27k | if (N->getValueType(0) == MVT::i1) { |
5878 | | // An i1 select is: (c & t) | (!c & f). |
5879 | 4.76k | bool Inv; |
5880 | 4.76k | unsigned Idx = getCRIdxForSetCC(CC, Inv); |
5881 | | |
5882 | 4.76k | unsigned SRI; |
5883 | 4.76k | switch (Idx) { |
5884 | 0 | default: llvm_unreachable("Invalid CC index"); |
5885 | 13 | case 0: SRI = PPC::sub_lt; break; |
5886 | 0 | case 1: SRI = PPC::sub_gt; break; |
5887 | 4.75k | case 2: SRI = PPC::sub_eq; break; |
5888 | 0 | case 3: SRI = PPC::sub_un; break; |
5889 | 4.76k | } |
5890 | | |
5891 | 4.76k | SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg); |
5892 | | |
5893 | 4.76k | SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1, |
5894 | 4.76k | CCBit, CCBit), 0); |
5895 | 4.76k | SDValue C = Inv ? NotCCBit : CCBit, |
5896 | 4.76k | NotC = Inv ? CCBit : NotCCBit; |
5897 | | |
5898 | 4.76k | SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1, |
5899 | 4.76k | C, N->getOperand(2)), 0); |
5900 | 4.76k | SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1, |
5901 | 4.76k | NotC, N->getOperand(3)), 0); |
5902 | | |
5903 | 4.76k | CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF); |
5904 | 4.76k | return; |
5905 | 4.76k | } |
5906 | | |
5907 | 3.51k | unsigned BROpc = |
5908 | 3.51k | getPredicateForSetCC(CC, N->getOperand(0).getValueType(), Subtarget); |
5909 | | |
5910 | 3.51k | unsigned SelectCCOp; |
5911 | 3.51k | if (N->getValueType(0) == MVT::i32) |
5912 | 1.09k | SelectCCOp = PPC::SELECT_CC_I4; |
5913 | 2.41k | else if (N->getValueType(0) == MVT::i64) |
5914 | 2.27k | SelectCCOp = PPC::SELECT_CC_I8; |
5915 | 143 | else if (N->getValueType(0) == MVT::f32) { |
5916 | 108 | if (Subtarget->hasP8Vector()) |
5917 | 0 | SelectCCOp = PPC::SELECT_CC_VSSRC; |
5918 | 108 | else if (Subtarget->hasSPE()) |
5919 | 0 | SelectCCOp = PPC::SELECT_CC_SPE4; |
5920 | 108 | else |
5921 | 108 | SelectCCOp = PPC::SELECT_CC_F4; |
5922 | 108 | } else if (N->getValueType(0) == MVT::f64) { |
5923 | 35 | if (Subtarget->hasVSX()) |
5924 | 0 | SelectCCOp = PPC::SELECT_CC_VSFRC; |
5925 | 35 | else if (Subtarget->hasSPE()) |
5926 | 0 | SelectCCOp = PPC::SELECT_CC_SPE; |
5927 | 35 | else |
5928 | 35 | SelectCCOp = PPC::SELECT_CC_F8; |
5929 | 35 | } else if (N->getValueType(0) == MVT::f128) |
5930 | 0 | SelectCCOp = PPC::SELECT_CC_F16; |
5931 | 0 | else if (Subtarget->hasSPE()) |
5932 | 0 | SelectCCOp = PPC::SELECT_CC_SPE; |
5933 | 0 | else if (N->getValueType(0) == MVT::v2f64 || |
5934 | 0 | N->getValueType(0) == MVT::v2i64) |
5935 | 0 | SelectCCOp = PPC::SELECT_CC_VSRC; |
5936 | 0 | else |
5937 | 0 | SelectCCOp = PPC::SELECT_CC_VRRC; |
5938 | | |
5939 | 3.51k | SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3), |
5940 | 3.51k | getI32Imm(BROpc, dl) }; |
5941 | 3.51k | CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops); |
5942 | 3.51k | return; |
5943 | 8.27k | } |
5944 | 0 | case ISD::VECTOR_SHUFFLE: |
5945 | 0 | if (Subtarget->hasVSX() && (N->getValueType(0) == MVT::v2f64 || |
5946 | 0 | N->getValueType(0) == MVT::v2i64)) { |
5947 | 0 | ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); |
5948 | |
|
5949 | 0 | SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1), |
5950 | 0 | Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1); |
5951 | 0 | unsigned DM[2]; |
5952 | |
|
5953 | 0 | for (int i = 0; i < 2; ++i) |
5954 | 0 | if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2) |
5955 | 0 | DM[i] = 0; |
5956 | 0 | else |
5957 | 0 | DM[i] = 1; |
5958 | |
|
5959 | 0 | if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 && |
5960 | 0 | Op1.getOpcode() == ISD::SCALAR_TO_VECTOR && |
5961 | 0 | isa<LoadSDNode>(Op1.getOperand(0))) { |
5962 | 0 | LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0)); |
5963 | 0 | SDValue Base, Offset; |
5964 | |
|
5965 | 0 | if (LD->isUnindexed() && LD->hasOneUse() && Op1.hasOneUse() && |
5966 | 0 | (LD->getMemoryVT() == MVT::f64 || |
5967 | 0 | LD->getMemoryVT() == MVT::i64) && |
5968 | 0 | SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) { |
5969 | 0 | SDValue Chain = LD->getChain(); |
5970 | 0 | SDValue Ops[] = { Base, Offset, Chain }; |
5971 | 0 | MachineMemOperand *MemOp = LD->getMemOperand(); |
5972 | 0 | SDNode *NewN = CurDAG->SelectNodeTo(N, PPC::LXVDSX, |
5973 | 0 | N->getValueType(0), Ops); |
5974 | 0 | CurDAG->setNodeMemRefs(cast<MachineSDNode>(NewN), {MemOp}); |
5975 | 0 | return; |
5976 | 0 | } |
5977 | 0 | } |
5978 | | |
5979 | | // For little endian, we must swap the input operands and adjust |
5980 | | // the mask elements (reverse and invert them). |
5981 | 0 | if (Subtarget->isLittleEndian()) { |
5982 | 0 | std::swap(Op1, Op2); |
5983 | 0 | unsigned tmp = DM[0]; |
5984 | 0 | DM[0] = 1 - DM[1]; |
5985 | 0 | DM[1] = 1 - tmp; |
5986 | 0 | } |
5987 | |
|
5988 | 0 | SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), dl, |
5989 | 0 | MVT::i32); |
5990 | 0 | SDValue Ops[] = { Op1, Op2, DMV }; |
5991 | 0 | CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops); |
5992 | 0 | return; |
5993 | 0 | } |
5994 | | |
5995 | 0 | break; |
5996 | 0 | case PPCISD::BDNZ: |
5997 | 0 | case PPCISD::BDZ: { |
5998 | 0 | bool IsPPC64 = Subtarget->isPPC64(); |
5999 | 0 | SDValue Ops[] = { N->getOperand(1), N->getOperand(0) }; |
6000 | 0 | CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ |
6001 | 0 | ? (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) |
6002 | 0 | : (IsPPC64 ? PPC::BDZ8 : PPC::BDZ), |
6003 | 0 | MVT::Other, Ops); |
6004 | 0 | return; |
6005 | 0 | } |
6006 | 0 | case PPCISD::COND_BRANCH: { |
6007 | | // Op #0 is the Chain. |
6008 | | // Op #1 is the PPC::PRED_* number. |
6009 | | // Op #2 is the CR# |
6010 | | // Op #3 is the Dest MBB |
6011 | | // Op #4 is the Flag. |
6012 | | // Prevent PPC::PRED_* from being selected into LI. |
6013 | 0 | unsigned PCC = N->getConstantOperandVal(1); |
6014 | 0 | if (EnableBranchHint) |
6015 | 0 | PCC |= getBranchHint(PCC, *FuncInfo, N->getOperand(3)); |
6016 | |
|
6017 | 0 | SDValue Pred = getI32Imm(PCC, dl); |
6018 | 0 | SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3), |
6019 | 0 | N->getOperand(0), N->getOperand(4) }; |
6020 | 0 | CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops); |
6021 | 0 | return; |
6022 | 0 | } |
6023 | 10.2k | case ISD::BR_CC: { |
6024 | 10.2k | if (tryFoldSWTestBRCC(N)) |
6025 | 0 | return; |
6026 | 10.2k | if (trySelectLoopCountIntrinsic(N)) |
6027 | 0 | return; |
6028 | 10.2k | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get(); |
6029 | 10.2k | unsigned PCC = |
6030 | 10.2k | getPredicateForSetCC(CC, N->getOperand(2).getValueType(), Subtarget); |
6031 | | |
6032 | 10.2k | if (N->getOperand(2).getValueType() == MVT::i1) { |
6033 | 329 | unsigned Opc; |
6034 | 329 | bool Swap; |
6035 | 329 | switch (PCC) { |
6036 | 0 | default: llvm_unreachable("Unexpected Boolean-operand predicate"); |
6037 | 4 | case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break; |
6038 | 3 | case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break; |
6039 | 72 | case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break; |
6040 | 3 | case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break; |
6041 | 6 | case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break; |
6042 | 241 | case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break; |
6043 | 329 | } |
6044 | | |
6045 | | // A signed comparison of i1 values produces the opposite result to an |
6046 | | // unsigned one if the condition code includes less-than or greater-than. |
6047 | | // This is because 1 is the most negative signed i1 number and the most |
6048 | | // positive unsigned i1 number. The CR-logical operations used for such |
6049 | | // comparisons are non-commutative so for signed comparisons vs. unsigned |
6050 | | // ones, the input operands just need to be swapped. |
6051 | 329 | if (ISD::isSignedIntSetCC(CC)) |
6052 | 11 | Swap = !Swap; |
6053 | | |
6054 | 329 | SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1, |
6055 | 329 | N->getOperand(Swap ? 3 : 2), |
6056 | 329 | N->getOperand(Swap ? 2 : 3)), 0); |
6057 | 329 | CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other, BitComp, N->getOperand(4), |
6058 | 329 | N->getOperand(0)); |
6059 | 329 | return; |
6060 | 329 | } |
6061 | | |
6062 | 9.90k | if (EnableBranchHint) |
6063 | 9.90k | PCC |= getBranchHint(PCC, *FuncInfo, N->getOperand(4)); |
6064 | | |
6065 | 9.90k | SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl); |
6066 | 9.90k | SDValue Ops[] = { getI32Imm(PCC, dl), CondCode, |
6067 | 9.90k | N->getOperand(4), N->getOperand(0) }; |
6068 | 9.90k | CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops); |
6069 | 9.90k | return; |
6070 | 10.2k | } |
6071 | 69 | case ISD::BRIND: { |
6072 | | // FIXME: Should custom lower this. |
6073 | 69 | SDValue Chain = N->getOperand(0); |
6074 | 69 | SDValue Target = N->getOperand(1); |
6075 | 69 | unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8; |
6076 | 69 | unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8; |
6077 | 69 | Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target, |
6078 | 69 | Chain), 0); |
6079 | 69 | CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain); |
6080 | 69 | return; |
6081 | 10.2k | } |
6082 | 32.0k | case PPCISD::TOC_ENTRY: { |
6083 | 32.0k | const bool isPPC64 = Subtarget->isPPC64(); |
6084 | 32.0k | const bool isELFABI = Subtarget->isSVR4ABI(); |
6085 | 32.0k | const bool isAIXABI = Subtarget->isAIXABI(); |
6086 | | |
6087 | | // PowerPC only support small, medium and large code model. |
6088 | 32.0k | const CodeModel::Model CModel = TM.getCodeModel(); |
6089 | 32.0k | assert(!(CModel == CodeModel::Tiny || CModel == CodeModel::Kernel) && |
6090 | 32.0k | "PowerPC doesn't support tiny or kernel code models."); |
6091 | | |
6092 | 32.0k | if (isAIXABI && CModel == CodeModel::Medium) |
6093 | 0 | report_fatal_error("Medium code model is not supported on AIX."); |
6094 | | |
6095 | | // For 64-bit ELF small code model, we allow SelectCodeCommon to handle |
6096 | | // this, selecting one of LDtoc, LDtocJTI, LDtocCPT, and LDtocBA. For AIX |
6097 | | // small code model, we need to check for a toc-data attribute. |
6098 | 32.0k | if (isPPC64 && !isAIXABI && CModel == CodeModel::Small) |
6099 | 0 | break; |
6100 | | |
6101 | 32.0k | auto replaceWith = [this, &dl](unsigned OpCode, SDNode *TocEntry, |
6102 | 32.0k | EVT OperandTy) { |
6103 | 0 | SDValue GA = TocEntry->getOperand(0); |
6104 | 0 | SDValue TocBase = TocEntry->getOperand(1); |
6105 | 0 | SDNode *MN = CurDAG->getMachineNode(OpCode, dl, OperandTy, GA, TocBase); |
6106 | 0 | transferMemOperands(TocEntry, MN); |
6107 | 0 | ReplaceNode(TocEntry, MN); |
6108 | 0 | }; |
6109 | | |
6110 | | // Handle 32-bit small code model. |
6111 | 32.0k | if (!isPPC64 && CModel == CodeModel::Small) { |
6112 | | // Transforms the ISD::TOC_ENTRY node to passed in Opcode, either |
6113 | | // PPC::ADDItoc, or PPC::LWZtoc |
6114 | 0 | if (isELFABI) { |
6115 | 0 | assert(TM.isPositionIndependent() && |
6116 | 0 | "32-bit ELF can only have TOC entries in position independent" |
6117 | 0 | " code."); |
6118 | | // 32-bit ELF always uses a small code model toc access. |
6119 | 0 | replaceWith(PPC::LWZtoc, N, MVT::i32); |
6120 | 0 | return; |
6121 | 0 | } |
6122 | | |
6123 | 0 | assert(isAIXABI && "ELF ABI already handled"); |
6124 | | |
6125 | 0 | if (hasTocDataAttr(N->getOperand(0), |
6126 | 0 | CurDAG->getDataLayout().getPointerSize())) { |
6127 | 0 | replaceWith(PPC::ADDItoc, N, MVT::i32); |
6128 | 0 | return; |
6129 | 0 | } |
6130 | | |
6131 | 0 | replaceWith(PPC::LWZtoc, N, MVT::i32); |
6132 | 0 | return; |
6133 | 0 | } |
6134 | | |
6135 | 32.0k | if (isPPC64 && CModel == CodeModel::Small) { |
6136 | 0 | assert(isAIXABI && "ELF ABI handled in common SelectCode"); |
6137 | | |
6138 | 0 | if (hasTocDataAttr(N->getOperand(0), |
6139 | 0 | CurDAG->getDataLayout().getPointerSize())) { |
6140 | 0 | replaceWith(PPC::ADDItoc8, N, MVT::i64); |
6141 | 0 | return; |
6142 | 0 | } |
6143 | | // Break if it doesn't have toc data attribute. Proceed with common |
6144 | | // SelectCode. |
6145 | 0 | break; |
6146 | 0 | } |
6147 | | |
6148 | 32.0k | assert(CModel != CodeModel::Small && "All small code models handled."); |
6149 | | |
6150 | 0 | assert((isPPC64 || (isAIXABI && !isPPC64)) && "We are dealing with 64-bit" |
6151 | 32.0k | " ELF/AIX or 32-bit AIX in the following."); |
6152 | | |
6153 | | // Transforms the ISD::TOC_ENTRY node for 32-bit AIX large code model mode |
6154 | | // or 64-bit medium (ELF-only) or large (ELF and AIX) code model code. We |
6155 | | // generate two instructions as described below. The first source operand |
6156 | | // is a symbol reference. If it must be toc-referenced according to |
6157 | | // Subtarget, we generate: |
6158 | | // [32-bit AIX] |
6159 | | // LWZtocL(@sym, ADDIStocHA(%r2, @sym)) |
6160 | | // [64-bit ELF/AIX] |
6161 | | // LDtocL(@sym, ADDIStocHA8(%x2, @sym)) |
6162 | | // Otherwise we generate: |
6163 | | // ADDItocL(ADDIStocHA8(%x2, @sym), @sym) |
6164 | 0 | SDValue GA = N->getOperand(0); |
6165 | 32.0k | SDValue TOCbase = N->getOperand(1); |
6166 | | |
6167 | 32.0k | EVT VT = isPPC64 ? MVT::i64 : MVT::i32; |
6168 | 32.0k | SDNode *Tmp = CurDAG->getMachineNode( |
6169 | 32.0k | isPPC64 ? PPC::ADDIStocHA8 : PPC::ADDIStocHA, dl, VT, TOCbase, GA); |
6170 | | |
6171 | 32.0k | if (PPCLowering->isAccessedAsGotIndirect(GA)) { |
6172 | | // If it is accessed as got-indirect, we need an extra LWZ/LD to load |
6173 | | // the address. |
6174 | 23.7k | SDNode *MN = CurDAG->getMachineNode( |
6175 | 23.7k | isPPC64 ? PPC::LDtocL : PPC::LWZtocL, dl, VT, GA, SDValue(Tmp, 0)); |
6176 | | |
6177 | 23.7k | transferMemOperands(N, MN); |
6178 | 23.7k | ReplaceNode(N, MN); |
6179 | 23.7k | return; |
6180 | 23.7k | } |
6181 | | |
6182 | | // Build the address relative to the TOC-pointer. |
6183 | 8.33k | ReplaceNode(N, CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64, |
6184 | 8.33k | SDValue(Tmp, 0), GA)); |
6185 | 8.33k | return; |
6186 | 32.0k | } |
6187 | 0 | case PPCISD::PPC32_PICGOT: |
6188 | | // Generate a PIC-safe GOT reference. |
6189 | 0 | assert(Subtarget->is32BitELFABI() && |
6190 | 0 | "PPCISD::PPC32_PICGOT is only supported for 32-bit SVR4"); |
6191 | 0 | CurDAG->SelectNodeTo(N, PPC::PPC32PICGOT, |
6192 | 0 | PPCLowering->getPointerTy(CurDAG->getDataLayout()), |
6193 | 0 | MVT::i32); |
6194 | 0 | return; |
6195 | | |
6196 | 0 | case PPCISD::VADD_SPLAT: { |
6197 | | // This expands into one of three sequences, depending on whether |
6198 | | // the first operand is odd or even, positive or negative. |
6199 | 0 | assert(isa<ConstantSDNode>(N->getOperand(0)) && |
6200 | 0 | isa<ConstantSDNode>(N->getOperand(1)) && |
6201 | 0 | "Invalid operand on VADD_SPLAT!"); |
6202 | | |
6203 | 0 | int Elt = N->getConstantOperandVal(0); |
6204 | 0 | int EltSize = N->getConstantOperandVal(1); |
6205 | 0 | unsigned Opc1, Opc2, Opc3; |
6206 | 0 | EVT VT; |
6207 | |
|
6208 | 0 | if (EltSize == 1) { |
6209 | 0 | Opc1 = PPC::VSPLTISB; |
6210 | 0 | Opc2 = PPC::VADDUBM; |
6211 | 0 | Opc3 = PPC::VSUBUBM; |
6212 | 0 | VT = MVT::v16i8; |
6213 | 0 | } else if (EltSize == 2) { |
6214 | 0 | Opc1 = PPC::VSPLTISH; |
6215 | 0 | Opc2 = PPC::VADDUHM; |
6216 | 0 | Opc3 = PPC::VSUBUHM; |
6217 | 0 | VT = MVT::v8i16; |
6218 | 0 | } else { |
6219 | 0 | assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!"); |
6220 | 0 | Opc1 = PPC::VSPLTISW; |
6221 | 0 | Opc2 = PPC::VADDUWM; |
6222 | 0 | Opc3 = PPC::VSUBUWM; |
6223 | 0 | VT = MVT::v4i32; |
6224 | 0 | } |
6225 | | |
6226 | 0 | if ((Elt & 1) == 0) { |
6227 | | // Elt is even, in the range [-32,-18] + [16,30]. |
6228 | | // |
6229 | | // Convert: VADD_SPLAT elt, size |
6230 | | // Into: tmp = VSPLTIS[BHW] elt |
6231 | | // VADDU[BHW]M tmp, tmp |
6232 | | // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4 |
6233 | 0 | SDValue EltVal = getI32Imm(Elt >> 1, dl); |
6234 | 0 | SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
6235 | 0 | SDValue TmpVal = SDValue(Tmp, 0); |
6236 | 0 | ReplaceNode(N, CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal)); |
6237 | 0 | return; |
6238 | 0 | } else if (Elt > 0) { |
6239 | | // Elt is odd and positive, in the range [17,31]. |
6240 | | // |
6241 | | // Convert: VADD_SPLAT elt, size |
6242 | | // Into: tmp1 = VSPLTIS[BHW] elt-16 |
6243 | | // tmp2 = VSPLTIS[BHW] -16 |
6244 | | // VSUBU[BHW]M tmp1, tmp2 |
6245 | 0 | SDValue EltVal = getI32Imm(Elt - 16, dl); |
6246 | 0 | SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
6247 | 0 | EltVal = getI32Imm(-16, dl); |
6248 | 0 | SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
6249 | 0 | ReplaceNode(N, CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0), |
6250 | 0 | SDValue(Tmp2, 0))); |
6251 | 0 | return; |
6252 | 0 | } else { |
6253 | | // Elt is odd and negative, in the range [-31,-17]. |
6254 | | // |
6255 | | // Convert: VADD_SPLAT elt, size |
6256 | | // Into: tmp1 = VSPLTIS[BHW] elt+16 |
6257 | | // tmp2 = VSPLTIS[BHW] -16 |
6258 | | // VADDU[BHW]M tmp1, tmp2 |
6259 | 0 | SDValue EltVal = getI32Imm(Elt + 16, dl); |
6260 | 0 | SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
6261 | 0 | EltVal = getI32Imm(-16, dl); |
6262 | 0 | SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
6263 | 0 | ReplaceNode(N, CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0), |
6264 | 0 | SDValue(Tmp2, 0))); |
6265 | 0 | return; |
6266 | 0 | } |
6267 | 0 | } |
6268 | 0 | case PPCISD::LD_SPLAT: { |
6269 | | // Here we want to handle splat load for type v16i8 and v8i16 when there is |
6270 | | // no direct move, we don't need to use stack for this case. If target has |
6271 | | // direct move, we should be able to get the best selection in the .td file. |
6272 | 0 | if (!Subtarget->hasAltivec() || Subtarget->hasDirectMove()) |
6273 | 0 | break; |
6274 | | |
6275 | 0 | EVT Type = N->getValueType(0); |
6276 | 0 | if (Type != MVT::v16i8 && Type != MVT::v8i16) |
6277 | 0 | break; |
6278 | | |
6279 | | // If the alignment for the load is 16 or bigger, we don't need the |
6280 | | // permutated mask to get the required value. The value must be the 0 |
6281 | | // element in big endian target or 7/15 in little endian target in the |
6282 | | // result vsx register of lvx instruction. |
6283 | | // Select the instruction in the .td file. |
6284 | 0 | if (cast<MemIntrinsicSDNode>(N)->getAlign() >= Align(16) && |
6285 | 0 | isOffsetMultipleOf(N, 16)) |
6286 | 0 | break; |
6287 | | |
6288 | 0 | SDValue ZeroReg = |
6289 | 0 | CurDAG->getRegister(Subtarget->isPPC64() ? PPC::ZERO8 : PPC::ZERO, |
6290 | 0 | Subtarget->isPPC64() ? MVT::i64 : MVT::i32); |
6291 | 0 | unsigned LIOpcode = Subtarget->isPPC64() ? PPC::LI8 : PPC::LI; |
6292 | | // v16i8 LD_SPLAT addr |
6293 | | // ======> |
6294 | | // Mask = LVSR/LVSL 0, addr |
6295 | | // LoadLow = LVX 0, addr |
6296 | | // Perm = VPERM LoadLow, LoadLow, Mask |
6297 | | // Splat = VSPLTB 15/0, Perm |
6298 | | // |
6299 | | // v8i16 LD_SPLAT addr |
6300 | | // ======> |
6301 | | // Mask = LVSR/LVSL 0, addr |
6302 | | // LoadLow = LVX 0, addr |
6303 | | // LoadHigh = LVX (LI, 1), addr |
6304 | | // Perm = VPERM LoadLow, LoadHigh, Mask |
6305 | | // Splat = VSPLTH 7/0, Perm |
6306 | 0 | unsigned SplatOp = (Type == MVT::v16i8) ? PPC::VSPLTB : PPC::VSPLTH; |
6307 | 0 | unsigned SplatElemIndex = |
6308 | 0 | Subtarget->isLittleEndian() ? ((Type == MVT::v16i8) ? 15 : 7) : 0; |
6309 | |
|
6310 | 0 | SDNode *Mask = CurDAG->getMachineNode( |
6311 | 0 | Subtarget->isLittleEndian() ? PPC::LVSR : PPC::LVSL, dl, Type, ZeroReg, |
6312 | 0 | N->getOperand(1)); |
6313 | |
|
6314 | 0 | SDNode *LoadLow = |
6315 | 0 | CurDAG->getMachineNode(PPC::LVX, dl, MVT::v16i8, MVT::Other, |
6316 | 0 | {ZeroReg, N->getOperand(1), N->getOperand(0)}); |
6317 | |
|
6318 | 0 | SDNode *LoadHigh = LoadLow; |
6319 | 0 | if (Type == MVT::v8i16) { |
6320 | 0 | LoadHigh = CurDAG->getMachineNode( |
6321 | 0 | PPC::LVX, dl, MVT::v16i8, MVT::Other, |
6322 | 0 | {SDValue(CurDAG->getMachineNode( |
6323 | 0 | LIOpcode, dl, MVT::i32, |
6324 | 0 | CurDAG->getTargetConstant(1, dl, MVT::i8)), |
6325 | 0 | 0), |
6326 | 0 | N->getOperand(1), SDValue(LoadLow, 1)}); |
6327 | 0 | } |
6328 | |
|
6329 | 0 | CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 1), SDValue(LoadHigh, 1)); |
6330 | 0 | transferMemOperands(N, LoadHigh); |
6331 | |
|
6332 | 0 | SDNode *Perm = |
6333 | 0 | CurDAG->getMachineNode(PPC::VPERM, dl, Type, SDValue(LoadLow, 0), |
6334 | 0 | SDValue(LoadHigh, 0), SDValue(Mask, 0)); |
6335 | 0 | CurDAG->SelectNodeTo(N, SplatOp, Type, |
6336 | 0 | CurDAG->getTargetConstant(SplatElemIndex, dl, MVT::i8), |
6337 | 0 | SDValue(Perm, 0)); |
6338 | 0 | return; |
6339 | 0 | } |
6340 | 3.50M | } |
6341 | | |
6342 | 3.30M | SelectCode(N); |
6343 | 3.30M | } |
6344 | | |
6345 | | // If the target supports the cmpb instruction, do the idiom recognition here. |
6346 | | // We don't do this as a DAG combine because we don't want to do it as nodes |
6347 | | // are being combined (because we might miss part of the eventual idiom). We |
6348 | | // don't want to do it during instruction selection because we want to reuse |
6349 | | // the logic for lowering the masking operations already part of the |
6350 | | // instruction selector. |
6351 | 86.8k | SDValue PPCDAGToDAGISel::combineToCMPB(SDNode *N) { |
6352 | 86.8k | SDLoc dl(N); |
6353 | | |
6354 | 86.8k | assert(N->getOpcode() == ISD::OR && |
6355 | 86.8k | "Only OR nodes are supported for CMPB"); |
6356 | | |
6357 | 0 | SDValue Res; |
6358 | 86.8k | if (!Subtarget->hasCMPB()) |
6359 | 86.8k | return Res; |
6360 | | |
6361 | 0 | if (N->getValueType(0) != MVT::i32 && |
6362 | 0 | N->getValueType(0) != MVT::i64) |
6363 | 0 | return Res; |
6364 | | |
6365 | 0 | EVT VT = N->getValueType(0); |
6366 | |
|
6367 | 0 | SDValue RHS, LHS; |
6368 | 0 | bool BytesFound[8] = {false, false, false, false, false, false, false, false}; |
6369 | 0 | uint64_t Mask = 0, Alt = 0; |
6370 | |
|
6371 | 0 | auto IsByteSelectCC = [this](SDValue O, unsigned &b, |
6372 | 0 | uint64_t &Mask, uint64_t &Alt, |
6373 | 0 | SDValue &LHS, SDValue &RHS) { |
6374 | 0 | if (O.getOpcode() != ISD::SELECT_CC) |
6375 | 0 | return false; |
6376 | 0 | ISD::CondCode CC = cast<CondCodeSDNode>(O.getOperand(4))->get(); |
6377 | |
|
6378 | 0 | if (!isa<ConstantSDNode>(O.getOperand(2)) || |
6379 | 0 | !isa<ConstantSDNode>(O.getOperand(3))) |
6380 | 0 | return false; |
6381 | | |
6382 | 0 | uint64_t PM = O.getConstantOperandVal(2); |
6383 | 0 | uint64_t PAlt = O.getConstantOperandVal(3); |
6384 | 0 | for (b = 0; b < 8; ++b) { |
6385 | 0 | uint64_t Mask = UINT64_C(0xFF) << (8*b); |
6386 | 0 | if (PM && (PM & Mask) == PM && (PAlt & Mask) == PAlt) |
6387 | 0 | break; |
6388 | 0 | } |
6389 | |
|
6390 | 0 | if (b == 8) |
6391 | 0 | return false; |
6392 | 0 | Mask |= PM; |
6393 | 0 | Alt |= PAlt; |
6394 | |
|
6395 | 0 | if (!isa<ConstantSDNode>(O.getOperand(1)) || |
6396 | 0 | O.getConstantOperandVal(1) != 0) { |
6397 | 0 | SDValue Op0 = O.getOperand(0), Op1 = O.getOperand(1); |
6398 | 0 | if (Op0.getOpcode() == ISD::TRUNCATE) |
6399 | 0 | Op0 = Op0.getOperand(0); |
6400 | 0 | if (Op1.getOpcode() == ISD::TRUNCATE) |
6401 | 0 | Op1 = Op1.getOperand(0); |
6402 | |
|
6403 | 0 | if (Op0.getOpcode() == ISD::SRL && Op1.getOpcode() == ISD::SRL && |
6404 | 0 | Op0.getOperand(1) == Op1.getOperand(1) && CC == ISD::SETEQ && |
6405 | 0 | isa<ConstantSDNode>(Op0.getOperand(1))) { |
6406 | |
|
6407 | 0 | unsigned Bits = Op0.getValueSizeInBits(); |
6408 | 0 | if (b != Bits/8-1) |
6409 | 0 | return false; |
6410 | 0 | if (Op0.getConstantOperandVal(1) != Bits-8) |
6411 | 0 | return false; |
6412 | | |
6413 | 0 | LHS = Op0.getOperand(0); |
6414 | 0 | RHS = Op1.getOperand(0); |
6415 | 0 | return true; |
6416 | 0 | } |
6417 | | |
6418 | | // When we have small integers (i16 to be specific), the form present |
6419 | | // post-legalization uses SETULT in the SELECT_CC for the |
6420 | | // higher-order byte, depending on the fact that the |
6421 | | // even-higher-order bytes are known to all be zero, for example: |
6422 | | // select_cc (xor $lhs, $rhs), 256, 65280, 0, setult |
6423 | | // (so when the second byte is the same, because all higher-order |
6424 | | // bits from bytes 3 and 4 are known to be zero, the result of the |
6425 | | // xor can be at most 255) |
6426 | 0 | if (Op0.getOpcode() == ISD::XOR && CC == ISD::SETULT && |
6427 | 0 | isa<ConstantSDNode>(O.getOperand(1))) { |
6428 | |
|
6429 | 0 | uint64_t ULim = O.getConstantOperandVal(1); |
6430 | 0 | if (ULim != (UINT64_C(1) << b*8)) |
6431 | 0 | return false; |
6432 | | |
6433 | | // Now we need to make sure that the upper bytes are known to be |
6434 | | // zero. |
6435 | 0 | unsigned Bits = Op0.getValueSizeInBits(); |
6436 | 0 | if (!CurDAG->MaskedValueIsZero( |
6437 | 0 | Op0, APInt::getHighBitsSet(Bits, Bits - (b + 1) * 8))) |
6438 | 0 | return false; |
6439 | | |
6440 | 0 | LHS = Op0.getOperand(0); |
6441 | 0 | RHS = Op0.getOperand(1); |
6442 | 0 | return true; |
6443 | 0 | } |
6444 | | |
6445 | 0 | return false; |
6446 | 0 | } |
6447 | | |
6448 | 0 | if (CC != ISD::SETEQ) |
6449 | 0 | return false; |
6450 | | |
6451 | 0 | SDValue Op = O.getOperand(0); |
6452 | 0 | if (Op.getOpcode() == ISD::AND) { |
6453 | 0 | if (!isa<ConstantSDNode>(Op.getOperand(1))) |
6454 | 0 | return false; |
6455 | 0 | if (Op.getConstantOperandVal(1) != (UINT64_C(0xFF) << (8*b))) |
6456 | 0 | return false; |
6457 | | |
6458 | 0 | SDValue XOR = Op.getOperand(0); |
6459 | 0 | if (XOR.getOpcode() == ISD::TRUNCATE) |
6460 | 0 | XOR = XOR.getOperand(0); |
6461 | 0 | if (XOR.getOpcode() != ISD::XOR) |
6462 | 0 | return false; |
6463 | | |
6464 | 0 | LHS = XOR.getOperand(0); |
6465 | 0 | RHS = XOR.getOperand(1); |
6466 | 0 | return true; |
6467 | 0 | } else if (Op.getOpcode() == ISD::SRL) { |
6468 | 0 | if (!isa<ConstantSDNode>(Op.getOperand(1))) |
6469 | 0 | return false; |
6470 | 0 | unsigned Bits = Op.getValueSizeInBits(); |
6471 | 0 | if (b != Bits/8-1) |
6472 | 0 | return false; |
6473 | 0 | if (Op.getConstantOperandVal(1) != Bits-8) |
6474 | 0 | return false; |
6475 | | |
6476 | 0 | SDValue XOR = Op.getOperand(0); |
6477 | 0 | if (XOR.getOpcode() == ISD::TRUNCATE) |
6478 | 0 | XOR = XOR.getOperand(0); |
6479 | 0 | if (XOR.getOpcode() != ISD::XOR) |
6480 | 0 | return false; |
6481 | | |
6482 | 0 | LHS = XOR.getOperand(0); |
6483 | 0 | RHS = XOR.getOperand(1); |
6484 | 0 | return true; |
6485 | 0 | } |
6486 | | |
6487 | 0 | return false; |
6488 | 0 | }; |
6489 | |
|
6490 | 0 | SmallVector<SDValue, 8> Queue(1, SDValue(N, 0)); |
6491 | 0 | while (!Queue.empty()) { |
6492 | 0 | SDValue V = Queue.pop_back_val(); |
6493 | |
|
6494 | 0 | for (const SDValue &O : V.getNode()->ops()) { |
6495 | 0 | unsigned b = 0; |
6496 | 0 | uint64_t M = 0, A = 0; |
6497 | 0 | SDValue OLHS, ORHS; |
6498 | 0 | if (O.getOpcode() == ISD::OR) { |
6499 | 0 | Queue.push_back(O); |
6500 | 0 | } else if (IsByteSelectCC(O, b, M, A, OLHS, ORHS)) { |
6501 | 0 | if (!LHS) { |
6502 | 0 | LHS = OLHS; |
6503 | 0 | RHS = ORHS; |
6504 | 0 | BytesFound[b] = true; |
6505 | 0 | Mask |= M; |
6506 | 0 | Alt |= A; |
6507 | 0 | } else if ((LHS == ORHS && RHS == OLHS) || |
6508 | 0 | (RHS == ORHS && LHS == OLHS)) { |
6509 | 0 | BytesFound[b] = true; |
6510 | 0 | Mask |= M; |
6511 | 0 | Alt |= A; |
6512 | 0 | } else { |
6513 | 0 | return Res; |
6514 | 0 | } |
6515 | 0 | } else { |
6516 | 0 | return Res; |
6517 | 0 | } |
6518 | 0 | } |
6519 | 0 | } |
6520 | | |
6521 | 0 | unsigned LastB = 0, BCnt = 0; |
6522 | 0 | for (unsigned i = 0; i < 8; ++i) |
6523 | 0 | if (BytesFound[LastB]) { |
6524 | 0 | ++BCnt; |
6525 | 0 | LastB = i; |
6526 | 0 | } |
6527 | |
|
6528 | 0 | if (!LastB || BCnt < 2) |
6529 | 0 | return Res; |
6530 | | |
6531 | | // Because we'll be zero-extending the output anyway if don't have a specific |
6532 | | // value for each input byte (via the Mask), we can 'anyext' the inputs. |
6533 | 0 | if (LHS.getValueType() != VT) { |
6534 | 0 | LHS = CurDAG->getAnyExtOrTrunc(LHS, dl, VT); |
6535 | 0 | RHS = CurDAG->getAnyExtOrTrunc(RHS, dl, VT); |
6536 | 0 | } |
6537 | |
|
6538 | 0 | Res = CurDAG->getNode(PPCISD::CMPB, dl, VT, LHS, RHS); |
6539 | |
|
6540 | 0 | bool NonTrivialMask = ((int64_t) Mask) != INT64_C(-1); |
6541 | 0 | if (NonTrivialMask && !Alt) { |
6542 | | // Res = Mask & CMPB |
6543 | 0 | Res = CurDAG->getNode(ISD::AND, dl, VT, Res, |
6544 | 0 | CurDAG->getConstant(Mask, dl, VT)); |
6545 | 0 | } else if (Alt) { |
6546 | | // Res = (CMPB & Mask) | (~CMPB & Alt) |
6547 | | // Which, as suggested here: |
6548 | | // https://graphics.stanford.edu/~seander/bithacks.html#MaskedMerge |
6549 | | // can be written as: |
6550 | | // Res = Alt ^ ((Alt ^ Mask) & CMPB) |
6551 | | // useful because the (Alt ^ Mask) can be pre-computed. |
6552 | 0 | Res = CurDAG->getNode(ISD::AND, dl, VT, Res, |
6553 | 0 | CurDAG->getConstant(Mask ^ Alt, dl, VT)); |
6554 | 0 | Res = CurDAG->getNode(ISD::XOR, dl, VT, Res, |
6555 | 0 | CurDAG->getConstant(Alt, dl, VT)); |
6556 | 0 | } |
6557 | |
|
6558 | 0 | return Res; |
6559 | 0 | } |
6560 | | |
6561 | | // When CR bit registers are enabled, an extension of an i1 variable to a i32 |
6562 | | // or i64 value is lowered in terms of a SELECT_I[48] operation, and thus |
6563 | | // involves constant materialization of a 0 or a 1 or both. If the result of |
6564 | | // the extension is then operated upon by some operator that can be constant |
6565 | | // folded with a constant 0 or 1, and that constant can be materialized using |
6566 | | // only one instruction (like a zero or one), then we should fold in those |
6567 | | // operations with the select. |
6568 | 3.91M | void PPCDAGToDAGISel::foldBoolExts(SDValue &Res, SDNode *&N) { |
6569 | 3.91M | if (!Subtarget->useCRBits()) |
6570 | 0 | return; |
6571 | | |
6572 | 3.91M | if (N->getOpcode() != ISD::ZERO_EXTEND && |
6573 | 3.91M | N->getOpcode() != ISD::SIGN_EXTEND && |
6574 | 3.91M | N->getOpcode() != ISD::ANY_EXTEND) |
6575 | 3.87M | return; |
6576 | | |
6577 | 41.6k | if (N->getOperand(0).getValueType() != MVT::i1) |
6578 | 12.3k | return; |
6579 | | |
6580 | 29.3k | if (!N->hasOneUse()) |
6581 | 1.12k | return; |
6582 | | |
6583 | 28.2k | SDLoc dl(N); |
6584 | 28.2k | EVT VT = N->getValueType(0); |
6585 | 28.2k | SDValue Cond = N->getOperand(0); |
6586 | 28.2k | SDValue ConstTrue = |
6587 | 28.2k | CurDAG->getConstant(N->getOpcode() == ISD::SIGN_EXTEND ? -1 : 1, dl, VT); |
6588 | 28.2k | SDValue ConstFalse = CurDAG->getConstant(0, dl, VT); |
6589 | | |
6590 | 35.7k | do { |
6591 | 35.7k | SDNode *User = *N->use_begin(); |
6592 | 35.7k | if (User->getNumOperands() != 2) |
6593 | 18.5k | break; |
6594 | | |
6595 | 24.8k | auto TryFold = [this, N, User, dl](SDValue Val) { |
6596 | 24.8k | SDValue UserO0 = User->getOperand(0), UserO1 = User->getOperand(1); |
6597 | 24.8k | SDValue O0 = UserO0.getNode() == N ? Val : UserO0; |
6598 | 24.8k | SDValue O1 = UserO1.getNode() == N ? Val : UserO1; |
6599 | | |
6600 | 24.8k | return CurDAG->FoldConstantArithmetic(User->getOpcode(), dl, |
6601 | 24.8k | User->getValueType(0), {O0, O1}); |
6602 | 24.8k | }; |
6603 | | |
6604 | | // FIXME: When the semantics of the interaction between select and undef |
6605 | | // are clearly defined, it may turn out to be unnecessary to break here. |
6606 | 17.1k | SDValue TrueRes = TryFold(ConstTrue); |
6607 | 17.1k | if (!TrueRes || TrueRes.isUndef()) |
6608 | 9.47k | break; |
6609 | 7.70k | SDValue FalseRes = TryFold(ConstFalse); |
6610 | 7.70k | if (!FalseRes || FalseRes.isUndef()) |
6611 | 6 | break; |
6612 | | |
6613 | | // For us to materialize these using one instruction, we must be able to |
6614 | | // represent them as signed 16-bit integers. |
6615 | 7.70k | uint64_t True = TrueRes->getAsZExtVal(), False = FalseRes->getAsZExtVal(); |
6616 | 7.70k | if (!isInt<16>(True) || !isInt<16>(False)) |
6617 | 72 | break; |
6618 | | |
6619 | | // We can replace User with a new SELECT node, and try again to see if we |
6620 | | // can fold the select with its user. |
6621 | 7.62k | Res = CurDAG->getSelect(dl, User->getValueType(0), Cond, TrueRes, FalseRes); |
6622 | 7.62k | N = User; |
6623 | 7.62k | ConstTrue = TrueRes; |
6624 | 7.62k | ConstFalse = FalseRes; |
6625 | 7.62k | } while (N->hasOneUse()); |
6626 | 28.2k | } |
6627 | | |
6628 | 56.9k | void PPCDAGToDAGISel::PreprocessISelDAG() { |
6629 | 56.9k | SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_end(); |
6630 | | |
6631 | 56.9k | bool MadeChange = false; |
6632 | 4.03M | while (Position != CurDAG->allnodes_begin()) { |
6633 | 3.97M | SDNode *N = &*--Position; |
6634 | 3.97M | if (N->use_empty()) |
6635 | 56.9k | continue; |
6636 | | |
6637 | 3.91M | SDValue Res; |
6638 | 3.91M | switch (N->getOpcode()) { |
6639 | 3.83M | default: break; |
6640 | 3.83M | case ISD::OR: |
6641 | 86.8k | Res = combineToCMPB(N); |
6642 | 86.8k | break; |
6643 | 3.91M | } |
6644 | | |
6645 | 3.91M | if (!Res) |
6646 | 3.91M | foldBoolExts(Res, N); |
6647 | | |
6648 | 3.91M | if (Res) { |
6649 | 7.61k | LLVM_DEBUG(dbgs() << "PPC DAG preprocessing replacing:\nOld: "); |
6650 | 7.61k | LLVM_DEBUG(N->dump(CurDAG)); |
6651 | 7.61k | LLVM_DEBUG(dbgs() << "\nNew: "); |
6652 | 7.61k | LLVM_DEBUG(Res.getNode()->dump(CurDAG)); |
6653 | 7.61k | LLVM_DEBUG(dbgs() << "\n"); |
6654 | | |
6655 | 7.61k | CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res); |
6656 | 7.61k | MadeChange = true; |
6657 | 7.61k | } |
6658 | 3.91M | } |
6659 | | |
6660 | 56.9k | if (MadeChange) |
6661 | 1.42k | CurDAG->RemoveDeadNodes(); |
6662 | 56.9k | } |
6663 | | |
6664 | | /// PostprocessISelDAG - Perform some late peephole optimizations |
6665 | | /// on the DAG representation. |
6666 | 56.9k | void PPCDAGToDAGISel::PostprocessISelDAG() { |
6667 | | // Skip peepholes at -O0. |
6668 | 56.9k | if (TM.getOptLevel() == CodeGenOptLevel::None) |
6669 | 0 | return; |
6670 | | |
6671 | 56.9k | PeepholePPC64(); |
6672 | 56.9k | PeepholeCROps(); |
6673 | 56.9k | PeepholePPC64ZExt(); |
6674 | 56.9k | } |
6675 | | |
6676 | | // Check if all users of this node will become isel where the second operand |
6677 | | // is the constant zero. If this is so, and if we can negate the condition, |
6678 | | // then we can flip the true and false operands. This will allow the zero to |
6679 | | // be folded with the isel so that we don't need to materialize a register |
6680 | | // containing zero. |
6681 | 49.9k | bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) { |
6682 | 54.4k | for (const SDNode *User : N->uses()) { |
6683 | 54.4k | if (!User->isMachineOpcode()) |
6684 | 1.26k | return false; |
6685 | 53.1k | if (User->getMachineOpcode() != PPC::SELECT_I4 && |
6686 | 53.1k | User->getMachineOpcode() != PPC::SELECT_I8) |
6687 | 35.5k | return false; |
6688 | | |
6689 | 17.5k | SDNode *Op1 = User->getOperand(1).getNode(); |
6690 | 17.5k | SDNode *Op2 = User->getOperand(2).getNode(); |
6691 | | // If we have a degenerate select with two equal operands, swapping will |
6692 | | // not do anything, and we may run into an infinite loop. |
6693 | 17.5k | if (Op1 == Op2) |
6694 | 6 | return false; |
6695 | | |
6696 | 17.5k | if (!Op2->isMachineOpcode()) |
6697 | 225 | return false; |
6698 | | |
6699 | 17.3k | if (Op2->getMachineOpcode() != PPC::LI && |
6700 | 17.3k | Op2->getMachineOpcode() != PPC::LI8) |
6701 | 589 | return false; |
6702 | | |
6703 | 16.7k | if (!isNullConstant(Op2->getOperand(0))) |
6704 | 8.02k | return false; |
6705 | 16.7k | } |
6706 | | |
6707 | 4.22k | return true; |
6708 | 49.9k | } |
6709 | | |
6710 | 4.22k | void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) { |
6711 | 4.22k | SmallVector<SDNode *, 4> ToReplace; |
6712 | 4.35k | for (SDNode *User : N->uses()) { |
6713 | 4.35k | assert((User->getMachineOpcode() == PPC::SELECT_I4 || |
6714 | 4.35k | User->getMachineOpcode() == PPC::SELECT_I8) && |
6715 | 4.35k | "Must have all select users"); |
6716 | 0 | ToReplace.push_back(User); |
6717 | 4.35k | } |
6718 | | |
6719 | 4.35k | for (SDNode *User : ToReplace) { |
6720 | 4.35k | SDNode *ResNode = |
6721 | 4.35k | CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User), |
6722 | 4.35k | User->getValueType(0), User->getOperand(0), |
6723 | 4.35k | User->getOperand(2), |
6724 | 4.35k | User->getOperand(1)); |
6725 | | |
6726 | 4.35k | LLVM_DEBUG(dbgs() << "CR Peephole replacing:\nOld: "); |
6727 | 4.35k | LLVM_DEBUG(User->dump(CurDAG)); |
6728 | 4.35k | LLVM_DEBUG(dbgs() << "\nNew: "); |
6729 | 4.35k | LLVM_DEBUG(ResNode->dump(CurDAG)); |
6730 | 4.35k | LLVM_DEBUG(dbgs() << "\n"); |
6731 | | |
6732 | 4.35k | ReplaceUses(User, ResNode); |
6733 | 4.35k | } |
6734 | 4.22k | } |
6735 | | |
6736 | 56.9k | void PPCDAGToDAGISel::PeepholeCROps() { |
6737 | 56.9k | bool IsModified; |
6738 | 61.4k | do { |
6739 | 61.4k | IsModified = false; |
6740 | 4.82M | for (SDNode &Node : CurDAG->allnodes()) { |
6741 | 4.82M | MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(&Node); |
6742 | 4.82M | if (!MachineNode || MachineNode->use_empty()) |
6743 | 3.26M | continue; |
6744 | 1.55M | SDNode *ResNode = MachineNode; |
6745 | | |
6746 | 1.55M | bool Op1Set = false, Op1Unset = false, |
6747 | 1.55M | Op1Not = false, |
6748 | 1.55M | Op2Set = false, Op2Unset = false, |
6749 | 1.55M | Op2Not = false; |
6750 | | |
6751 | 1.55M | unsigned Opcode = MachineNode->getMachineOpcode(); |
6752 | 1.55M | switch (Opcode) { |
6753 | 1.45M | default: break; |
6754 | 1.45M | case PPC::CRAND: |
6755 | 15.8k | case PPC::CRNAND: |
6756 | 31.0k | case PPC::CROR: |
6757 | 32.7k | case PPC::CRXOR: |
6758 | 47.2k | case PPC::CRNOR: |
6759 | 48.5k | case PPC::CREQV: |
6760 | 61.3k | case PPC::CRANDC: |
6761 | 62.4k | case PPC::CRORC: { |
6762 | 62.4k | SDValue Op = MachineNode->getOperand(1); |
6763 | 62.4k | if (Op.isMachineOpcode()) { |
6764 | 61.0k | if (Op.getMachineOpcode() == PPC::CRSET) |
6765 | 569 | Op2Set = true; |
6766 | 60.4k | else if (Op.getMachineOpcode() == PPC::CRUNSET) |
6767 | 297 | Op2Unset = true; |
6768 | 60.1k | else if ((Op.getMachineOpcode() == PPC::CRNOR && |
6769 | 60.1k | Op.getOperand(0) == Op.getOperand(1)) || |
6770 | 60.1k | Op.getMachineOpcode() == PPC::CRNOT) |
6771 | 5.10k | Op2Not = true; |
6772 | 61.0k | } |
6773 | 62.4k | [[fallthrough]]; |
6774 | 62.4k | } |
6775 | 65.3k | case PPC::BC: |
6776 | 65.8k | case PPC::BCn: |
6777 | 74.3k | case PPC::SELECT_I4: |
6778 | 99.1k | case PPC::SELECT_I8: |
6779 | 99.3k | case PPC::SELECT_F4: |
6780 | 99.4k | case PPC::SELECT_F8: |
6781 | 99.4k | case PPC::SELECT_SPE: |
6782 | 99.4k | case PPC::SELECT_SPE4: |
6783 | 99.4k | case PPC::SELECT_VRRC: |
6784 | 99.4k | case PPC::SELECT_VSFRC: |
6785 | 99.4k | case PPC::SELECT_VSSRC: |
6786 | 99.4k | case PPC::SELECT_VSRC: { |
6787 | 99.4k | SDValue Op = MachineNode->getOperand(0); |
6788 | 99.4k | if (Op.isMachineOpcode()) { |
6789 | 92.2k | if (Op.getMachineOpcode() == PPC::CRSET) |
6790 | 35 | Op1Set = true; |
6791 | 92.1k | else if (Op.getMachineOpcode() == PPC::CRUNSET) |
6792 | 24 | Op1Unset = true; |
6793 | 92.1k | else if ((Op.getMachineOpcode() == PPC::CRNOR && |
6794 | 92.1k | Op.getOperand(0) == Op.getOperand(1)) || |
6795 | 92.1k | Op.getMachineOpcode() == PPC::CRNOT) |
6796 | 10.5k | Op1Not = true; |
6797 | 92.2k | } |
6798 | 99.4k | } |
6799 | 99.4k | break; |
6800 | 1.55M | } |
6801 | | |
6802 | 1.55M | bool SelectSwap = false; |
6803 | 1.55M | switch (Opcode) { |
6804 | 1.45M | default: break; |
6805 | 1.45M | case PPC::CRAND: |
6806 | 15.0k | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
6807 | | // x & x = x |
6808 | 33 | ResNode = MachineNode->getOperand(0).getNode(); |
6809 | 15.0k | else if (Op1Set) |
6810 | | // 1 & y = y |
6811 | 0 | ResNode = MachineNode->getOperand(1).getNode(); |
6812 | 15.0k | else if (Op2Set) |
6813 | | // x & 1 = x |
6814 | 66 | ResNode = MachineNode->getOperand(0).getNode(); |
6815 | 14.9k | else if (Op1Unset || Op2Unset) |
6816 | | // x & 0 = 0 & y = 0 |
6817 | 26 | ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), |
6818 | 26 | MVT::i1); |
6819 | 14.9k | else if (Op1Not) |
6820 | | // ~x & y = andc(y, x) |
6821 | 5.97k | ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), |
6822 | 5.97k | MVT::i1, MachineNode->getOperand(1), |
6823 | 5.97k | MachineNode->getOperand(0). |
6824 | 5.97k | getOperand(0)); |
6825 | 8.93k | else if (Op2Not) |
6826 | | // x & ~y = andc(x, y) |
6827 | 1.66k | ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), |
6828 | 1.66k | MVT::i1, MachineNode->getOperand(0), |
6829 | 1.66k | MachineNode->getOperand(1). |
6830 | 1.66k | getOperand(0)); |
6831 | 7.27k | else if (AllUsersSelectZero(MachineNode)) { |
6832 | 326 | ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode), |
6833 | 326 | MVT::i1, MachineNode->getOperand(0), |
6834 | 326 | MachineNode->getOperand(1)); |
6835 | 326 | SelectSwap = true; |
6836 | 326 | } |
6837 | 15.0k | break; |
6838 | 782 | case PPC::CRNAND: |
6839 | 782 | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
6840 | | // nand(x, x) -> nor(x, x) |
6841 | 0 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
6842 | 0 | MVT::i1, MachineNode->getOperand(0), |
6843 | 0 | MachineNode->getOperand(0)); |
6844 | 782 | else if (Op1Set) |
6845 | | // nand(1, y) -> nor(y, y) |
6846 | 0 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
6847 | 0 | MVT::i1, MachineNode->getOperand(1), |
6848 | 0 | MachineNode->getOperand(1)); |
6849 | 782 | else if (Op2Set) |
6850 | | // nand(x, 1) -> nor(x, x) |
6851 | 0 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
6852 | 0 | MVT::i1, MachineNode->getOperand(0), |
6853 | 0 | MachineNode->getOperand(0)); |
6854 | 782 | else if (Op1Unset || Op2Unset) |
6855 | | // nand(x, 0) = nand(0, y) = 1 |
6856 | 0 | ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), |
6857 | 0 | MVT::i1); |
6858 | 782 | else if (Op1Not) |
6859 | | // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y) |
6860 | 0 | ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), |
6861 | 0 | MVT::i1, MachineNode->getOperand(0). |
6862 | 0 | getOperand(0), |
6863 | 0 | MachineNode->getOperand(1)); |
6864 | 782 | else if (Op2Not) |
6865 | | // nand(x, ~y) = ~x | y = orc(y, x) |
6866 | 0 | ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), |
6867 | 0 | MVT::i1, MachineNode->getOperand(1). |
6868 | 0 | getOperand(0), |
6869 | 0 | MachineNode->getOperand(0)); |
6870 | 782 | else if (AllUsersSelectZero(MachineNode)) { |
6871 | 34 | ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode), |
6872 | 34 | MVT::i1, MachineNode->getOperand(0), |
6873 | 34 | MachineNode->getOperand(1)); |
6874 | 34 | SelectSwap = true; |
6875 | 34 | } |
6876 | 782 | break; |
6877 | 15.2k | case PPC::CROR: |
6878 | 15.2k | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
6879 | | // x | x = x |
6880 | 231 | ResNode = MachineNode->getOperand(0).getNode(); |
6881 | 14.9k | else if (Op1Set || Op2Set) |
6882 | | // x | 1 = 1 | y = 1 |
6883 | 0 | ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), |
6884 | 0 | MVT::i1); |
6885 | 14.9k | else if (Op1Unset) |
6886 | | // 0 | y = y |
6887 | 8 | ResNode = MachineNode->getOperand(1).getNode(); |
6888 | 14.9k | else if (Op2Unset) |
6889 | | // x | 0 = x |
6890 | 5 | ResNode = MachineNode->getOperand(0).getNode(); |
6891 | 14.9k | else if (Op1Not) |
6892 | | // ~x | y = orc(y, x) |
6893 | 123 | ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), |
6894 | 123 | MVT::i1, MachineNode->getOperand(1), |
6895 | 123 | MachineNode->getOperand(0). |
6896 | 123 | getOperand(0)); |
6897 | 14.8k | else if (Op2Not) |
6898 | | // x | ~y = orc(x, y) |
6899 | 86 | ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), |
6900 | 86 | MVT::i1, MachineNode->getOperand(0), |
6901 | 86 | MachineNode->getOperand(1). |
6902 | 86 | getOperand(0)); |
6903 | 14.7k | else if (AllUsersSelectZero(MachineNode)) { |
6904 | 1.80k | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
6905 | 1.80k | MVT::i1, MachineNode->getOperand(0), |
6906 | 1.80k | MachineNode->getOperand(1)); |
6907 | 1.80k | SelectSwap = true; |
6908 | 1.80k | } |
6909 | 15.2k | break; |
6910 | 1.75k | case PPC::CRXOR: |
6911 | 1.75k | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
6912 | | // xor(x, x) = 0 |
6913 | 0 | ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), |
6914 | 0 | MVT::i1); |
6915 | 1.75k | else if (Op1Set) |
6916 | | // xor(1, y) -> nor(y, y) |
6917 | 0 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
6918 | 0 | MVT::i1, MachineNode->getOperand(1), |
6919 | 0 | MachineNode->getOperand(1)); |
6920 | 1.75k | else if (Op2Set) |
6921 | | // xor(x, 1) -> nor(x, x) |
6922 | 503 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
6923 | 503 | MVT::i1, MachineNode->getOperand(0), |
6924 | 503 | MachineNode->getOperand(0)); |
6925 | 1.25k | else if (Op1Unset) |
6926 | | // xor(0, y) = y |
6927 | 0 | ResNode = MachineNode->getOperand(1).getNode(); |
6928 | 1.25k | else if (Op2Unset) |
6929 | | // xor(x, 0) = x |
6930 | 184 | ResNode = MachineNode->getOperand(0).getNode(); |
6931 | 1.07k | else if (Op1Not) |
6932 | | // xor(~x, y) = eqv(x, y) |
6933 | 68 | ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode), |
6934 | 68 | MVT::i1, MachineNode->getOperand(0). |
6935 | 68 | getOperand(0), |
6936 | 68 | MachineNode->getOperand(1)); |
6937 | 1.00k | else if (Op2Not) |
6938 | | // xor(x, ~y) = eqv(x, y) |
6939 | 54 | ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode), |
6940 | 54 | MVT::i1, MachineNode->getOperand(0), |
6941 | 54 | MachineNode->getOperand(1). |
6942 | 54 | getOperand(0)); |
6943 | 949 | else if (AllUsersSelectZero(MachineNode)) { |
6944 | 467 | ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode), |
6945 | 467 | MVT::i1, MachineNode->getOperand(0), |
6946 | 467 | MachineNode->getOperand(1)); |
6947 | 467 | SelectSwap = true; |
6948 | 467 | } |
6949 | 1.75k | break; |
6950 | 14.4k | case PPC::CRNOR: |
6951 | 14.4k | if (Op1Set || Op2Set) |
6952 | | // nor(1, y) -> 0 |
6953 | 0 | ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), |
6954 | 0 | MVT::i1); |
6955 | 14.4k | else if (Op1Unset) |
6956 | | // nor(0, y) = ~y -> nor(y, y) |
6957 | 1 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
6958 | 1 | MVT::i1, MachineNode->getOperand(1), |
6959 | 1 | MachineNode->getOperand(1)); |
6960 | 14.4k | else if (Op2Unset) |
6961 | | // nor(x, 0) = ~x |
6962 | 12 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
6963 | 12 | MVT::i1, MachineNode->getOperand(0), |
6964 | 12 | MachineNode->getOperand(0)); |
6965 | 14.4k | else if (Op1Not) |
6966 | | // nor(~x, y) = andc(x, y) |
6967 | 34 | ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), |
6968 | 34 | MVT::i1, MachineNode->getOperand(0). |
6969 | 34 | getOperand(0), |
6970 | 34 | MachineNode->getOperand(1)); |
6971 | 14.4k | else if (Op2Not) |
6972 | | // nor(x, ~y) = andc(y, x) |
6973 | 19 | ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), |
6974 | 19 | MVT::i1, MachineNode->getOperand(1). |
6975 | 19 | getOperand(0), |
6976 | 19 | MachineNode->getOperand(0)); |
6977 | 14.4k | else if (AllUsersSelectZero(MachineNode)) { |
6978 | 1.08k | ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode), |
6979 | 1.08k | MVT::i1, MachineNode->getOperand(0), |
6980 | 1.08k | MachineNode->getOperand(1)); |
6981 | 1.08k | SelectSwap = true; |
6982 | 1.08k | } |
6983 | 14.4k | break; |
6984 | 1.30k | case PPC::CREQV: |
6985 | 1.30k | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
6986 | | // eqv(x, x) = 1 |
6987 | 5 | ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), |
6988 | 5 | MVT::i1); |
6989 | 1.29k | else if (Op1Set) |
6990 | | // eqv(1, y) = y |
6991 | 0 | ResNode = MachineNode->getOperand(1).getNode(); |
6992 | 1.29k | else if (Op2Set) |
6993 | | // eqv(x, 1) = x |
6994 | 0 | ResNode = MachineNode->getOperand(0).getNode(); |
6995 | 1.29k | else if (Op1Unset) |
6996 | | // eqv(0, y) = ~y -> nor(y, y) |
6997 | 0 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
6998 | 0 | MVT::i1, MachineNode->getOperand(1), |
6999 | 0 | MachineNode->getOperand(1)); |
7000 | 1.29k | else if (Op2Unset) |
7001 | | // eqv(x, 0) = ~x |
7002 | 69 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
7003 | 69 | MVT::i1, MachineNode->getOperand(0), |
7004 | 69 | MachineNode->getOperand(0)); |
7005 | 1.22k | else if (Op1Not) |
7006 | | // eqv(~x, y) = xor(x, y) |
7007 | 9 | ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode), |
7008 | 9 | MVT::i1, MachineNode->getOperand(0). |
7009 | 9 | getOperand(0), |
7010 | 9 | MachineNode->getOperand(1)); |
7011 | 1.21k | else if (Op2Not) |
7012 | | // eqv(x, ~y) = xor(x, y) |
7013 | 15 | ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode), |
7014 | 15 | MVT::i1, MachineNode->getOperand(0), |
7015 | 15 | MachineNode->getOperand(1). |
7016 | 15 | getOperand(0)); |
7017 | 1.20k | else if (AllUsersSelectZero(MachineNode)) { |
7018 | 126 | ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode), |
7019 | 126 | MVT::i1, MachineNode->getOperand(0), |
7020 | 126 | MachineNode->getOperand(1)); |
7021 | 126 | SelectSwap = true; |
7022 | 126 | } |
7023 | 1.30k | break; |
7024 | 12.7k | case PPC::CRANDC: |
7025 | 12.7k | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
7026 | | // andc(x, x) = 0 |
7027 | 0 | ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), |
7028 | 0 | MVT::i1); |
7029 | 12.7k | else if (Op1Set) |
7030 | | // andc(1, y) = ~y |
7031 | 0 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
7032 | 0 | MVT::i1, MachineNode->getOperand(1), |
7033 | 0 | MachineNode->getOperand(1)); |
7034 | 12.7k | else if (Op1Unset || Op2Set) |
7035 | | // andc(0, y) = andc(x, 1) = 0 |
7036 | 1 | ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), |
7037 | 1 | MVT::i1); |
7038 | 12.7k | else if (Op2Unset) |
7039 | | // andc(x, 0) = x |
7040 | 0 | ResNode = MachineNode->getOperand(0).getNode(); |
7041 | 12.7k | else if (Op1Not) |
7042 | | // andc(~x, y) = ~(x | y) = nor(x, y) |
7043 | 3.16k | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
7044 | 3.16k | MVT::i1, MachineNode->getOperand(0). |
7045 | 3.16k | getOperand(0), |
7046 | 3.16k | MachineNode->getOperand(1)); |
7047 | 9.59k | else if (Op2Not) |
7048 | | // andc(x, ~y) = x & y |
7049 | 33 | ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode), |
7050 | 33 | MVT::i1, MachineNode->getOperand(0), |
7051 | 33 | MachineNode->getOperand(1). |
7052 | 33 | getOperand(0)); |
7053 | 9.56k | else if (AllUsersSelectZero(MachineNode)) { |
7054 | 211 | ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), |
7055 | 211 | MVT::i1, MachineNode->getOperand(1), |
7056 | 211 | MachineNode->getOperand(0)); |
7057 | 211 | SelectSwap = true; |
7058 | 211 | } |
7059 | 12.7k | break; |
7060 | 1.06k | case PPC::CRORC: |
7061 | 1.06k | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
7062 | | // orc(x, x) = 1 |
7063 | 0 | ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), |
7064 | 0 | MVT::i1); |
7065 | 1.06k | else if (Op1Set || Op2Unset) |
7066 | | // orc(1, y) = orc(x, 0) = 1 |
7067 | 1 | ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), |
7068 | 1 | MVT::i1); |
7069 | 1.06k | else if (Op2Set) |
7070 | | // orc(x, 1) = x |
7071 | 0 | ResNode = MachineNode->getOperand(0).getNode(); |
7072 | 1.06k | else if (Op1Unset) |
7073 | | // orc(0, y) = ~y |
7074 | 0 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
7075 | 0 | MVT::i1, MachineNode->getOperand(1), |
7076 | 0 | MachineNode->getOperand(1)); |
7077 | 1.06k | else if (Op1Not) |
7078 | | // orc(~x, y) = ~(x & y) = nand(x, y) |
7079 | 78 | ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode), |
7080 | 78 | MVT::i1, MachineNode->getOperand(0). |
7081 | 78 | getOperand(0), |
7082 | 78 | MachineNode->getOperand(1)); |
7083 | 990 | else if (Op2Not) |
7084 | | // orc(x, ~y) = x | y |
7085 | 0 | ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode), |
7086 | 0 | MVT::i1, MachineNode->getOperand(0), |
7087 | 0 | MachineNode->getOperand(1). |
7088 | 0 | getOperand(0)); |
7089 | 990 | else if (AllUsersSelectZero(MachineNode)) { |
7090 | 177 | ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), |
7091 | 177 | MVT::i1, MachineNode->getOperand(1), |
7092 | 177 | MachineNode->getOperand(0)); |
7093 | 177 | SelectSwap = true; |
7094 | 177 | } |
7095 | 1.06k | break; |
7096 | 8.43k | case PPC::SELECT_I4: |
7097 | 33.2k | case PPC::SELECT_I8: |
7098 | 33.4k | case PPC::SELECT_F4: |
7099 | 33.5k | case PPC::SELECT_F8: |
7100 | 33.5k | case PPC::SELECT_SPE: |
7101 | 33.5k | case PPC::SELECT_SPE4: |
7102 | 33.5k | case PPC::SELECT_VRRC: |
7103 | 33.5k | case PPC::SELECT_VSFRC: |
7104 | 33.5k | case PPC::SELECT_VSSRC: |
7105 | 33.5k | case PPC::SELECT_VSRC: |
7106 | 33.5k | if (Op1Set) |
7107 | 2 | ResNode = MachineNode->getOperand(1).getNode(); |
7108 | 33.5k | else if (Op1Unset) |
7109 | 0 | ResNode = MachineNode->getOperand(2).getNode(); |
7110 | 33.5k | else if (Op1Not) |
7111 | 729 | ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(), |
7112 | 729 | SDLoc(MachineNode), |
7113 | 729 | MachineNode->getValueType(0), |
7114 | 729 | MachineNode->getOperand(0). |
7115 | 729 | getOperand(0), |
7116 | 729 | MachineNode->getOperand(2), |
7117 | 729 | MachineNode->getOperand(1)); |
7118 | 33.5k | break; |
7119 | 2.93k | case PPC::BC: |
7120 | 3.47k | case PPC::BCn: |
7121 | 3.47k | if (Op1Not) |
7122 | 271 | ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn : |
7123 | 271 | PPC::BC, |
7124 | 271 | SDLoc(MachineNode), |
7125 | 271 | MVT::Other, |
7126 | 271 | MachineNode->getOperand(0). |
7127 | 271 | getOperand(0), |
7128 | 271 | MachineNode->getOperand(1), |
7129 | 271 | MachineNode->getOperand(2)); |
7130 | | // FIXME: Handle Op1Set, Op1Unset here too. |
7131 | 3.47k | break; |
7132 | 1.55M | } |
7133 | | |
7134 | | // If we're inverting this node because it is used only by selects that |
7135 | | // we'd like to swap, then swap the selects before the node replacement. |
7136 | 1.55M | if (SelectSwap) |
7137 | 4.22k | SwapAllSelectUsers(MachineNode); |
7138 | | |
7139 | 1.55M | if (ResNode != MachineNode) { |
7140 | 17.7k | LLVM_DEBUG(dbgs() << "CR Peephole replacing:\nOld: "); |
7141 | 17.7k | LLVM_DEBUG(MachineNode->dump(CurDAG)); |
7142 | 17.7k | LLVM_DEBUG(dbgs() << "\nNew: "); |
7143 | 17.7k | LLVM_DEBUG(ResNode->dump(CurDAG)); |
7144 | 17.7k | LLVM_DEBUG(dbgs() << "\n"); |
7145 | | |
7146 | 17.7k | ReplaceUses(MachineNode, ResNode); |
7147 | 17.7k | IsModified = true; |
7148 | 17.7k | } |
7149 | 1.55M | } |
7150 | 61.4k | if (IsModified) |
7151 | 4.43k | CurDAG->RemoveDeadNodes(); |
7152 | 61.4k | } while (IsModified); |
7153 | 56.9k | } |
7154 | | |
7155 | | // Gather the set of 32-bit operations that are known to have their |
7156 | | // higher-order 32 bits zero, where ToPromote contains all such operations. |
7157 | | static bool PeepholePPC64ZExtGather(SDValue Op32, |
7158 | 2.63k | SmallPtrSetImpl<SDNode *> &ToPromote) { |
7159 | 2.63k | if (!Op32.isMachineOpcode()) |
7160 | 5 | return false; |
7161 | | |
7162 | | // First, check for the "frontier" instructions (those that will clear the |
7163 | | // higher-order 32 bits. |
7164 | | |
7165 | | // For RLWINM and RLWNM, we need to make sure that the mask does not wrap |
7166 | | // around. If it does not, then these instructions will clear the |
7167 | | // higher-order bits. |
7168 | 2.63k | if ((Op32.getMachineOpcode() == PPC::RLWINM || |
7169 | 2.63k | Op32.getMachineOpcode() == PPC::RLWNM) && |
7170 | 2.63k | Op32.getConstantOperandVal(2) <= Op32.getConstantOperandVal(3)) { |
7171 | 1.35k | ToPromote.insert(Op32.getNode()); |
7172 | 1.35k | return true; |
7173 | 1.35k | } |
7174 | | |
7175 | | // SLW and SRW always clear the higher-order bits. |
7176 | 1.27k | if (Op32.getMachineOpcode() == PPC::SLW || |
7177 | 1.27k | Op32.getMachineOpcode() == PPC::SRW) { |
7178 | 43 | ToPromote.insert(Op32.getNode()); |
7179 | 43 | return true; |
7180 | 43 | } |
7181 | | |
7182 | | // For LI and LIS, we need the immediate to be positive (so that it is not |
7183 | | // sign extended). |
7184 | 1.23k | if (Op32.getMachineOpcode() == PPC::LI || |
7185 | 1.23k | Op32.getMachineOpcode() == PPC::LIS) { |
7186 | 2 | if (!isUInt<15>(Op32.getConstantOperandVal(0))) |
7187 | 0 | return false; |
7188 | | |
7189 | 2 | ToPromote.insert(Op32.getNode()); |
7190 | 2 | return true; |
7191 | 2 | } |
7192 | | |
7193 | | // LHBRX and LWBRX always clear the higher-order bits. |
7194 | 1.23k | if (Op32.getMachineOpcode() == PPC::LHBRX || |
7195 | 1.23k | Op32.getMachineOpcode() == PPC::LWBRX) { |
7196 | 0 | ToPromote.insert(Op32.getNode()); |
7197 | 0 | return true; |
7198 | 0 | } |
7199 | | |
7200 | | // CNT[LT]ZW always produce a 64-bit value in [0,32], and so is zero extended. |
7201 | 1.23k | if (Op32.getMachineOpcode() == PPC::CNTLZW || |
7202 | 1.23k | Op32.getMachineOpcode() == PPC::CNTTZW) { |
7203 | 1 | ToPromote.insert(Op32.getNode()); |
7204 | 1 | return true; |
7205 | 1 | } |
7206 | | |
7207 | | // Next, check for those instructions we can look through. |
7208 | | |
7209 | | // Assuming the mask does not wrap around, then the higher-order bits are |
7210 | | // taken directly from the first operand. |
7211 | 1.23k | if (Op32.getMachineOpcode() == PPC::RLWIMI && |
7212 | 1.23k | Op32.getConstantOperandVal(3) <= Op32.getConstantOperandVal(4)) { |
7213 | 20 | SmallPtrSet<SDNode *, 16> ToPromote1; |
7214 | 20 | if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1)) |
7215 | 5 | return false; |
7216 | | |
7217 | 15 | ToPromote.insert(Op32.getNode()); |
7218 | 15 | ToPromote.insert(ToPromote1.begin(), ToPromote1.end()); |
7219 | 15 | return true; |
7220 | 20 | } |
7221 | | |
7222 | | // For OR, the higher-order bits are zero if that is true for both operands. |
7223 | | // For SELECT_I4, the same is true (but the relevant operand numbers are |
7224 | | // shifted by 1). |
7225 | 1.21k | if (Op32.getMachineOpcode() == PPC::OR || |
7226 | 1.21k | Op32.getMachineOpcode() == PPC::SELECT_I4) { |
7227 | 9 | unsigned B = Op32.getMachineOpcode() == PPC::SELECT_I4 ? 1 : 0; |
7228 | 9 | SmallPtrSet<SDNode *, 16> ToPromote1; |
7229 | 9 | if (!PeepholePPC64ZExtGather(Op32.getOperand(B+0), ToPromote1)) |
7230 | 7 | return false; |
7231 | 2 | if (!PeepholePPC64ZExtGather(Op32.getOperand(B+1), ToPromote1)) |
7232 | 1 | return false; |
7233 | | |
7234 | 1 | ToPromote.insert(Op32.getNode()); |
7235 | 1 | ToPromote.insert(ToPromote1.begin(), ToPromote1.end()); |
7236 | 1 | return true; |
7237 | 2 | } |
7238 | | |
7239 | | // For ORI and ORIS, we need the higher-order bits of the first operand to be |
7240 | | // zero, and also for the constant to be positive (so that it is not sign |
7241 | | // extended). |
7242 | 1.20k | if (Op32.getMachineOpcode() == PPC::ORI || |
7243 | 1.20k | Op32.getMachineOpcode() == PPC::ORIS) { |
7244 | 16 | SmallPtrSet<SDNode *, 16> ToPromote1; |
7245 | 16 | if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1)) |
7246 | 16 | return false; |
7247 | 0 | if (!isUInt<15>(Op32.getConstantOperandVal(1))) |
7248 | 0 | return false; |
7249 | | |
7250 | 0 | ToPromote.insert(Op32.getNode()); |
7251 | 0 | ToPromote.insert(ToPromote1.begin(), ToPromote1.end()); |
7252 | 0 | return true; |
7253 | 0 | } |
7254 | | |
7255 | | // The higher-order bits of AND are zero if that is true for at least one of |
7256 | | // the operands. |
7257 | 1.18k | if (Op32.getMachineOpcode() == PPC::AND) { |
7258 | 11 | SmallPtrSet<SDNode *, 16> ToPromote1, ToPromote2; |
7259 | 11 | bool Op0OK = |
7260 | 11 | PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1); |
7261 | 11 | bool Op1OK = |
7262 | 11 | PeepholePPC64ZExtGather(Op32.getOperand(1), ToPromote2); |
7263 | 11 | if (!Op0OK && !Op1OK) |
7264 | 8 | return false; |
7265 | | |
7266 | 3 | ToPromote.insert(Op32.getNode()); |
7267 | | |
7268 | 3 | if (Op0OK) |
7269 | 3 | ToPromote.insert(ToPromote1.begin(), ToPromote1.end()); |
7270 | | |
7271 | 3 | if (Op1OK) |
7272 | 0 | ToPromote.insert(ToPromote2.begin(), ToPromote2.end()); |
7273 | | |
7274 | 3 | return true; |
7275 | 11 | } |
7276 | | |
7277 | | // For ANDI and ANDIS, the higher-order bits are zero if either that is true |
7278 | | // of the first operand, or if the second operand is positive (so that it is |
7279 | | // not sign extended). |
7280 | 1.17k | if (Op32.getMachineOpcode() == PPC::ANDI_rec || |
7281 | 1.17k | Op32.getMachineOpcode() == PPC::ANDIS_rec) { |
7282 | 5 | SmallPtrSet<SDNode *, 16> ToPromote1; |
7283 | 5 | bool Op0OK = |
7284 | 5 | PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1); |
7285 | 5 | bool Op1OK = isUInt<15>(Op32.getConstantOperandVal(1)); |
7286 | 5 | if (!Op0OK && !Op1OK) |
7287 | 0 | return false; |
7288 | | |
7289 | 5 | ToPromote.insert(Op32.getNode()); |
7290 | | |
7291 | 5 | if (Op0OK) |
7292 | 2 | ToPromote.insert(ToPromote1.begin(), ToPromote1.end()); |
7293 | | |
7294 | 5 | return true; |
7295 | 5 | } |
7296 | | |
7297 | 1.17k | return false; |
7298 | 1.17k | } |
7299 | | |
7300 | 56.9k | void PPCDAGToDAGISel::PeepholePPC64ZExt() { |
7301 | 56.9k | if (!Subtarget->isPPC64()) |
7302 | 0 | return; |
7303 | | |
7304 | | // When we zero-extend from i32 to i64, we use a pattern like this: |
7305 | | // def : Pat<(i64 (zext i32:$in)), |
7306 | | // (RLDICL (INSERT_SUBREG (i64 (IMPLICIT_DEF)), $in, sub_32), |
7307 | | // 0, 32)>; |
7308 | | // There are several 32-bit shift/rotate instructions, however, that will |
7309 | | // clear the higher-order bits of their output, rendering the RLDICL |
7310 | | // unnecessary. When that happens, we remove it here, and redefine the |
7311 | | // relevant 32-bit operation to be a 64-bit operation. |
7312 | | |
7313 | 56.9k | SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_end(); |
7314 | | |
7315 | 56.9k | bool MadeChange = false; |
7316 | 4.22M | while (Position != CurDAG->allnodes_begin()) { |
7317 | 4.16M | SDNode *N = &*--Position; |
7318 | | // Skip dead nodes and any non-machine opcodes. |
7319 | 4.16M | if (N->use_empty() || !N->isMachineOpcode()) |
7320 | 3.01M | continue; |
7321 | | |
7322 | 1.15M | if (N->getMachineOpcode() != PPC::RLDICL) |
7323 | 1.08M | continue; |
7324 | | |
7325 | 64.1k | if (N->getConstantOperandVal(1) != 0 || |
7326 | 64.1k | N->getConstantOperandVal(2) != 32) |
7327 | 60.3k | continue; |
7328 | | |
7329 | 3.75k | SDValue ISR = N->getOperand(0); |
7330 | 3.75k | if (!ISR.isMachineOpcode() || |
7331 | 3.75k | ISR.getMachineOpcode() != TargetOpcode::INSERT_SUBREG) |
7332 | 655 | continue; |
7333 | | |
7334 | 3.10k | if (!ISR.hasOneUse()) |
7335 | 274 | continue; |
7336 | | |
7337 | 2.82k | if (ISR.getConstantOperandVal(2) != PPC::sub_32) |
7338 | 0 | continue; |
7339 | | |
7340 | 2.82k | SDValue IDef = ISR.getOperand(0); |
7341 | 2.82k | if (!IDef.isMachineOpcode() || |
7342 | 2.82k | IDef.getMachineOpcode() != TargetOpcode::IMPLICIT_DEF) |
7343 | 0 | continue; |
7344 | | |
7345 | | // We now know that we're looking at a canonical i32 -> i64 zext. See if we |
7346 | | // can get rid of it. |
7347 | | |
7348 | 2.82k | SDValue Op32 = ISR->getOperand(1); |
7349 | 2.82k | if (!Op32.isMachineOpcode()) |
7350 | 264 | continue; |
7351 | | |
7352 | | // There are some 32-bit instructions that always clear the high-order 32 |
7353 | | // bits, there are also some instructions (like AND) that we can look |
7354 | | // through. |
7355 | 2.56k | SmallPtrSet<SDNode *, 16> ToPromote; |
7356 | 2.56k | if (!PeepholePPC64ZExtGather(Op32, ToPromote)) |
7357 | 1.16k | continue; |
7358 | | |
7359 | | // If the ToPromote set contains nodes that have uses outside of the set |
7360 | | // (except for the original INSERT_SUBREG), then abort the transformation. |
7361 | 1.40k | bool OutsideUse = false; |
7362 | 1.42k | for (SDNode *PN : ToPromote) { |
7363 | 1.51k | for (SDNode *UN : PN->uses()) { |
7364 | 1.51k | if (!ToPromote.count(UN) && UN != ISR.getNode()) { |
7365 | 136 | OutsideUse = true; |
7366 | 136 | break; |
7367 | 136 | } |
7368 | 1.51k | } |
7369 | | |
7370 | 1.42k | if (OutsideUse) |
7371 | 136 | break; |
7372 | 1.42k | } |
7373 | 1.40k | if (OutsideUse) |
7374 | 136 | continue; |
7375 | | |
7376 | 1.26k | MadeChange = true; |
7377 | | |
7378 | | // We now know that this zero extension can be removed by promoting to |
7379 | | // nodes in ToPromote to 64-bit operations, where for operations in the |
7380 | | // frontier of the set, we need to insert INSERT_SUBREGs for their |
7381 | | // operands. |
7382 | 1.28k | for (SDNode *PN : ToPromote) { |
7383 | 1.28k | unsigned NewOpcode; |
7384 | 1.28k | switch (PN->getMachineOpcode()) { |
7385 | 0 | default: |
7386 | 0 | llvm_unreachable("Don't know the 64-bit variant of this instruction"); |
7387 | 1.23k | case PPC::RLWINM: NewOpcode = PPC::RLWINM8; break; |
7388 | 0 | case PPC::RLWNM: NewOpcode = PPC::RLWNM8; break; |
7389 | 13 | case PPC::SLW: NewOpcode = PPC::SLW8; break; |
7390 | 13 | case PPC::SRW: NewOpcode = PPC::SRW8; break; |
7391 | 2 | case PPC::LI: NewOpcode = PPC::LI8; break; |
7392 | 0 | case PPC::LIS: NewOpcode = PPC::LIS8; break; |
7393 | 0 | case PPC::LHBRX: NewOpcode = PPC::LHBRX8; break; |
7394 | 0 | case PPC::LWBRX: NewOpcode = PPC::LWBRX8; break; |
7395 | 1 | case PPC::CNTLZW: NewOpcode = PPC::CNTLZW8; break; |
7396 | 0 | case PPC::CNTTZW: NewOpcode = PPC::CNTTZW8; break; |
7397 | 15 | case PPC::RLWIMI: NewOpcode = PPC::RLWIMI8; break; |
7398 | 0 | case PPC::OR: NewOpcode = PPC::OR8; break; |
7399 | 1 | case PPC::SELECT_I4: NewOpcode = PPC::SELECT_I8; break; |
7400 | 0 | case PPC::ORI: NewOpcode = PPC::ORI8; break; |
7401 | 0 | case PPC::ORIS: NewOpcode = PPC::ORIS8; break; |
7402 | 2 | case PPC::AND: NewOpcode = PPC::AND8; break; |
7403 | 3 | case PPC::ANDI_rec: |
7404 | 3 | NewOpcode = PPC::ANDI8_rec; |
7405 | 3 | break; |
7406 | 0 | case PPC::ANDIS_rec: |
7407 | 0 | NewOpcode = PPC::ANDIS8_rec; |
7408 | 0 | break; |
7409 | 1.28k | } |
7410 | | |
7411 | | // Note: During the replacement process, the nodes will be in an |
7412 | | // inconsistent state (some instructions will have operands with values |
7413 | | // of the wrong type). Once done, however, everything should be right |
7414 | | // again. |
7415 | | |
7416 | 1.28k | SmallVector<SDValue, 4> Ops; |
7417 | 5.09k | for (const SDValue &V : PN->ops()) { |
7418 | 5.09k | if (!ToPromote.count(V.getNode()) && V.getValueType() == MVT::i32 && |
7419 | 5.09k | !isa<ConstantSDNode>(V)) { |
7420 | 1.30k | SDValue ReplOpOps[] = { ISR.getOperand(0), V, ISR.getOperand(2) }; |
7421 | 1.30k | SDNode *ReplOp = |
7422 | 1.30k | CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, SDLoc(V), |
7423 | 1.30k | ISR.getNode()->getVTList(), ReplOpOps); |
7424 | 1.30k | Ops.push_back(SDValue(ReplOp, 0)); |
7425 | 3.78k | } else { |
7426 | 3.78k | Ops.push_back(V); |
7427 | 3.78k | } |
7428 | 5.09k | } |
7429 | | |
7430 | | // Because all to-be-promoted nodes only have users that are other |
7431 | | // promoted nodes (or the original INSERT_SUBREG), we can safely replace |
7432 | | // the i32 result value type with i64. |
7433 | | |
7434 | 1.28k | SmallVector<EVT, 2> NewVTs; |
7435 | 1.28k | SDVTList VTs = PN->getVTList(); |
7436 | 2.57k | for (unsigned i = 0, ie = VTs.NumVTs; i != ie; ++i) |
7437 | 1.28k | if (VTs.VTs[i] == MVT::i32) |
7438 | 1.28k | NewVTs.push_back(MVT::i64); |
7439 | 0 | else |
7440 | 0 | NewVTs.push_back(VTs.VTs[i]); |
7441 | | |
7442 | 1.28k | LLVM_DEBUG(dbgs() << "PPC64 ZExt Peephole morphing:\nOld: "); |
7443 | 1.28k | LLVM_DEBUG(PN->dump(CurDAG)); |
7444 | | |
7445 | 1.28k | CurDAG->SelectNodeTo(PN, NewOpcode, CurDAG->getVTList(NewVTs), Ops); |
7446 | | |
7447 | 1.28k | LLVM_DEBUG(dbgs() << "\nNew: "); |
7448 | 1.28k | LLVM_DEBUG(PN->dump(CurDAG)); |
7449 | 1.28k | LLVM_DEBUG(dbgs() << "\n"); |
7450 | 1.28k | } |
7451 | | |
7452 | | // Now we replace the original zero extend and its associated INSERT_SUBREG |
7453 | | // with the value feeding the INSERT_SUBREG (which has now been promoted to |
7454 | | // return an i64). |
7455 | | |
7456 | 1.26k | LLVM_DEBUG(dbgs() << "PPC64 ZExt Peephole replacing:\nOld: "); |
7457 | 1.26k | LLVM_DEBUG(N->dump(CurDAG)); |
7458 | 1.26k | LLVM_DEBUG(dbgs() << "\nNew: "); |
7459 | 1.26k | LLVM_DEBUG(Op32.getNode()->dump(CurDAG)); |
7460 | 1.26k | LLVM_DEBUG(dbgs() << "\n"); |
7461 | | |
7462 | 1.26k | ReplaceUses(N, Op32.getNode()); |
7463 | 1.26k | } |
7464 | | |
7465 | 56.9k | if (MadeChange) |
7466 | 1.16k | CurDAG->RemoveDeadNodes(); |
7467 | 56.9k | } |
7468 | | |
7469 | 1.18M | static bool isVSXSwap(SDValue N) { |
7470 | 1.18M | if (!N->isMachineOpcode()) |
7471 | 0 | return false; |
7472 | 1.18M | unsigned Opc = N->getMachineOpcode(); |
7473 | | |
7474 | | // Single-operand XXPERMDI or the regular XXPERMDI/XXSLDWI where the immediate |
7475 | | // operand is 2. |
7476 | 1.18M | if (Opc == PPC::XXPERMDIs) { |
7477 | 0 | return isa<ConstantSDNode>(N->getOperand(1)) && |
7478 | 0 | N->getConstantOperandVal(1) == 2; |
7479 | 1.18M | } else if (Opc == PPC::XXPERMDI || Opc == PPC::XXSLDWI) { |
7480 | 0 | return N->getOperand(0) == N->getOperand(1) && |
7481 | 0 | isa<ConstantSDNode>(N->getOperand(2)) && |
7482 | 0 | N->getConstantOperandVal(2) == 2; |
7483 | 0 | } |
7484 | | |
7485 | 1.18M | return false; |
7486 | 1.18M | } |
7487 | | |
7488 | | // TODO: Make this complete and replace with a table-gen bit. |
7489 | 0 | static bool isLaneInsensitive(SDValue N) { |
7490 | 0 | if (!N->isMachineOpcode()) |
7491 | 0 | return false; |
7492 | 0 | unsigned Opc = N->getMachineOpcode(); |
7493 | |
|
7494 | 0 | switch (Opc) { |
7495 | 0 | default: |
7496 | 0 | return false; |
7497 | 0 | case PPC::VAVGSB: |
7498 | 0 | case PPC::VAVGUB: |
7499 | 0 | case PPC::VAVGSH: |
7500 | 0 | case PPC::VAVGUH: |
7501 | 0 | case PPC::VAVGSW: |
7502 | 0 | case PPC::VAVGUW: |
7503 | 0 | case PPC::VMAXFP: |
7504 | 0 | case PPC::VMAXSB: |
7505 | 0 | case PPC::VMAXUB: |
7506 | 0 | case PPC::VMAXSH: |
7507 | 0 | case PPC::VMAXUH: |
7508 | 0 | case PPC::VMAXSW: |
7509 | 0 | case PPC::VMAXUW: |
7510 | 0 | case PPC::VMINFP: |
7511 | 0 | case PPC::VMINSB: |
7512 | 0 | case PPC::VMINUB: |
7513 | 0 | case PPC::VMINSH: |
7514 | 0 | case PPC::VMINUH: |
7515 | 0 | case PPC::VMINSW: |
7516 | 0 | case PPC::VMINUW: |
7517 | 0 | case PPC::VADDFP: |
7518 | 0 | case PPC::VADDUBM: |
7519 | 0 | case PPC::VADDUHM: |
7520 | 0 | case PPC::VADDUWM: |
7521 | 0 | case PPC::VSUBFP: |
7522 | 0 | case PPC::VSUBUBM: |
7523 | 0 | case PPC::VSUBUHM: |
7524 | 0 | case PPC::VSUBUWM: |
7525 | 0 | case PPC::VAND: |
7526 | 0 | case PPC::VANDC: |
7527 | 0 | case PPC::VOR: |
7528 | 0 | case PPC::VORC: |
7529 | 0 | case PPC::VXOR: |
7530 | 0 | case PPC::VNOR: |
7531 | 0 | case PPC::VMULUWM: |
7532 | 0 | return true; |
7533 | 0 | } |
7534 | 0 | } |
7535 | | |
7536 | | // Try to simplify (xxswap (vec-op (xxswap) (xxswap))) where vec-op is |
7537 | | // lane-insensitive. |
7538 | 0 | static void reduceVSXSwap(SDNode *N, SelectionDAG *DAG) { |
7539 | | // Our desired xxswap might be source of COPY_TO_REGCLASS. |
7540 | | // TODO: Can we put this a common method for DAG? |
7541 | 0 | auto SkipRCCopy = [](SDValue V) { |
7542 | 0 | while (V->isMachineOpcode() && |
7543 | 0 | V->getMachineOpcode() == TargetOpcode::COPY_TO_REGCLASS) { |
7544 | | // All values in the chain should have single use. |
7545 | 0 | if (V->use_empty() || !V->use_begin()->isOnlyUserOf(V.getNode())) |
7546 | 0 | return SDValue(); |
7547 | 0 | V = V->getOperand(0); |
7548 | 0 | } |
7549 | 0 | return V.hasOneUse() ? V : SDValue(); |
7550 | 0 | }; |
7551 | |
|
7552 | 0 | SDValue VecOp = SkipRCCopy(N->getOperand(0)); |
7553 | 0 | if (!VecOp || !isLaneInsensitive(VecOp)) |
7554 | 0 | return; |
7555 | | |
7556 | 0 | SDValue LHS = SkipRCCopy(VecOp.getOperand(0)), |
7557 | 0 | RHS = SkipRCCopy(VecOp.getOperand(1)); |
7558 | 0 | if (!LHS || !RHS || !isVSXSwap(LHS) || !isVSXSwap(RHS)) |
7559 | 0 | return; |
7560 | | |
7561 | | // These swaps may still have chain-uses here, count on dead code elimination |
7562 | | // in following passes to remove them. |
7563 | 0 | DAG->ReplaceAllUsesOfValueWith(LHS, LHS.getOperand(0)); |
7564 | 0 | DAG->ReplaceAllUsesOfValueWith(RHS, RHS.getOperand(0)); |
7565 | 0 | DAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), N->getOperand(0)); |
7566 | 0 | } |
7567 | | |
7568 | 56.9k | void PPCDAGToDAGISel::PeepholePPC64() { |
7569 | 56.9k | SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_end(); |
7570 | | |
7571 | 4.28M | while (Position != CurDAG->allnodes_begin()) { |
7572 | 4.22M | SDNode *N = &*--Position; |
7573 | | // Skip dead nodes and any non-machine opcodes. |
7574 | 4.22M | if (N->use_empty() || !N->isMachineOpcode()) |
7575 | 3.03M | continue; |
7576 | | |
7577 | 1.18M | if (isVSXSwap(SDValue(N, 0))) |
7578 | 0 | reduceVSXSwap(N, CurDAG); |
7579 | | |
7580 | 1.18M | unsigned FirstOp; |
7581 | 1.18M | unsigned StorageOpcode = N->getMachineOpcode(); |
7582 | 1.18M | bool RequiresMod4Offset = false; |
7583 | | |
7584 | 1.18M | switch (StorageOpcode) { |
7585 | 940k | default: continue; |
7586 | | |
7587 | 940k | case PPC::LWA: |
7588 | 30.4k | case PPC::LD: |
7589 | 30.4k | case PPC::DFLOADf64: |
7590 | 30.4k | case PPC::DFLOADf32: |
7591 | 30.4k | RequiresMod4Offset = true; |
7592 | 30.4k | [[fallthrough]]; |
7593 | 56.6k | case PPC::LBZ: |
7594 | 63.9k | case PPC::LBZ8: |
7595 | 70.4k | case PPC::LFD: |
7596 | 79.8k | case PPC::LFS: |
7597 | 81.3k | case PPC::LHA: |
7598 | 81.6k | case PPC::LHA8: |
7599 | 85.0k | case PPC::LHZ: |
7600 | 85.6k | case PPC::LHZ8: |
7601 | 96.7k | case PPC::LWZ: |
7602 | 99.4k | case PPC::LWZ8: |
7603 | 99.4k | FirstOp = 0; |
7604 | 99.4k | break; |
7605 | | |
7606 | 56.2k | case PPC::STD: |
7607 | 56.2k | case PPC::DFSTOREf64: |
7608 | 56.2k | case PPC::DFSTOREf32: |
7609 | 56.2k | RequiresMod4Offset = true; |
7610 | 56.2k | [[fallthrough]]; |
7611 | 77.3k | case PPC::STB: |
7612 | 120k | case PPC::STB8: |
7613 | 123k | case PPC::STFD: |
7614 | 125k | case PPC::STFS: |
7615 | 130k | case PPC::STH: |
7616 | 134k | case PPC::STH8: |
7617 | 142k | case PPC::STW: |
7618 | 149k | case PPC::STW8: |
7619 | 149k | FirstOp = 1; |
7620 | 149k | break; |
7621 | 1.18M | } |
7622 | | |
7623 | | // If this is a load or store with a zero offset, or within the alignment, |
7624 | | // we may be able to fold an add-immediate into the memory operation. |
7625 | | // The check against alignment is below, as it can't occur until we check |
7626 | | // the arguments to N |
7627 | 249k | if (!isa<ConstantSDNode>(N->getOperand(FirstOp))) |
7628 | 0 | continue; |
7629 | | |
7630 | 249k | SDValue Base = N->getOperand(FirstOp + 1); |
7631 | 249k | if (!Base.isMachineOpcode()) |
7632 | 125k | continue; |
7633 | | |
7634 | 123k | unsigned Flags = 0; |
7635 | 123k | bool ReplaceFlags = true; |
7636 | | |
7637 | | // When the feeding operation is an add-immediate of some sort, |
7638 | | // determine whether we need to add relocation information to the |
7639 | | // target flags on the immediate operand when we fold it into the |
7640 | | // load instruction. |
7641 | | // |
7642 | | // For something like ADDItocL, the relocation information is |
7643 | | // inferred from the opcode; when we process it in the AsmPrinter, |
7644 | | // we add the necessary relocation there. A load, though, can receive |
7645 | | // relocation from various flavors of ADDIxxx, so we need to carry |
7646 | | // the relocation information in the target flags. |
7647 | 123k | switch (Base.getMachineOpcode()) { |
7648 | 115k | default: continue; |
7649 | | |
7650 | 115k | case PPC::ADDI8: |
7651 | 0 | case PPC::ADDI: |
7652 | | // In some cases (such as TLS) the relocation information |
7653 | | // is already in place on the operand, so copying the operand |
7654 | | // is sufficient. |
7655 | 0 | ReplaceFlags = false; |
7656 | 0 | break; |
7657 | 0 | case PPC::ADDIdtprelL: |
7658 | 0 | Flags = PPCII::MO_DTPREL_LO; |
7659 | 0 | break; |
7660 | 0 | case PPC::ADDItlsldL: |
7661 | 0 | Flags = PPCII::MO_TLSLD_LO; |
7662 | 0 | break; |
7663 | 8.21k | case PPC::ADDItocL: |
7664 | 8.21k | Flags = PPCII::MO_TOC_LO; |
7665 | 8.21k | break; |
7666 | 123k | } |
7667 | | |
7668 | 8.21k | SDValue ImmOpnd = Base.getOperand(1); |
7669 | | |
7670 | | // On PPC64, the TOC base pointer is guaranteed by the ABI only to have |
7671 | | // 8-byte alignment, and so we can only use offsets less than 8 (otherwise, |
7672 | | // we might have needed different @ha relocation values for the offset |
7673 | | // pointers). |
7674 | 8.21k | int MaxDisplacement = 7; |
7675 | 8.21k | if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) { |
7676 | 269 | const GlobalValue *GV = GA->getGlobal(); |
7677 | 269 | Align Alignment = GV->getPointerAlignment(CurDAG->getDataLayout()); |
7678 | 269 | MaxDisplacement = std::min((int)Alignment.value() - 1, MaxDisplacement); |
7679 | 269 | } |
7680 | | |
7681 | 8.21k | bool UpdateHBase = false; |
7682 | 8.21k | SDValue HBase = Base.getOperand(0); |
7683 | | |
7684 | 8.21k | int Offset = N->getConstantOperandVal(FirstOp); |
7685 | 8.21k | if (ReplaceFlags) { |
7686 | 8.21k | if (Offset < 0 || Offset > MaxDisplacement) { |
7687 | | // If we have a addi(toc@l)/addis(toc@ha) pair, and the addis has only |
7688 | | // one use, then we can do this for any offset, we just need to also |
7689 | | // update the offset (i.e. the symbol addend) on the addis also. |
7690 | 81 | if (Base.getMachineOpcode() != PPC::ADDItocL) |
7691 | 0 | continue; |
7692 | | |
7693 | 81 | if (!HBase.isMachineOpcode() || |
7694 | 81 | HBase.getMachineOpcode() != PPC::ADDIStocHA8) |
7695 | 0 | continue; |
7696 | | |
7697 | 81 | if (!Base.hasOneUse() || !HBase.hasOneUse()) |
7698 | 81 | continue; |
7699 | | |
7700 | 0 | SDValue HImmOpnd = HBase.getOperand(1); |
7701 | 0 | if (HImmOpnd != ImmOpnd) |
7702 | 0 | continue; |
7703 | | |
7704 | 0 | UpdateHBase = true; |
7705 | 0 | } |
7706 | 8.21k | } else { |
7707 | | // Global addresses can be folded, but only if they are sufficiently |
7708 | | // aligned. |
7709 | 0 | if (RequiresMod4Offset) { |
7710 | 0 | if (GlobalAddressSDNode *GA = |
7711 | 0 | dyn_cast<GlobalAddressSDNode>(ImmOpnd)) { |
7712 | 0 | const GlobalValue *GV = GA->getGlobal(); |
7713 | 0 | Align Alignment = GV->getPointerAlignment(CurDAG->getDataLayout()); |
7714 | 0 | if (Alignment < 4) |
7715 | 0 | continue; |
7716 | 0 | } |
7717 | 0 | } |
7718 | | |
7719 | | // If we're directly folding the addend from an addi instruction, then: |
7720 | | // 1. In general, the offset on the memory access must be zero. |
7721 | | // 2. If the addend is a constant, then it can be combined with a |
7722 | | // non-zero offset, but only if the result meets the encoding |
7723 | | // requirements. |
7724 | 0 | if (auto *C = dyn_cast<ConstantSDNode>(ImmOpnd)) { |
7725 | 0 | Offset += C->getSExtValue(); |
7726 | |
|
7727 | 0 | if (RequiresMod4Offset && (Offset % 4) != 0) |
7728 | 0 | continue; |
7729 | | |
7730 | 0 | if (!isInt<16>(Offset)) |
7731 | 0 | continue; |
7732 | | |
7733 | 0 | ImmOpnd = CurDAG->getTargetConstant(Offset, SDLoc(ImmOpnd), |
7734 | 0 | ImmOpnd.getValueType()); |
7735 | 0 | } else if (Offset != 0) { |
7736 | 0 | continue; |
7737 | 0 | } |
7738 | 0 | } |
7739 | | |
7740 | | // We found an opportunity. Reverse the operands from the add |
7741 | | // immediate and substitute them into the load or store. If |
7742 | | // needed, update the target flags for the immediate operand to |
7743 | | // reflect the necessary relocation information. |
7744 | 8.13k | LLVM_DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: "); |
7745 | 8.13k | LLVM_DEBUG(Base->dump(CurDAG)); |
7746 | 8.13k | LLVM_DEBUG(dbgs() << "\nN: "); |
7747 | 8.13k | LLVM_DEBUG(N->dump(CurDAG)); |
7748 | 8.13k | LLVM_DEBUG(dbgs() << "\n"); |
7749 | | |
7750 | | // If the relocation information isn't already present on the |
7751 | | // immediate operand, add it now. |
7752 | 8.13k | if (ReplaceFlags) { |
7753 | 8.13k | if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) { |
7754 | 188 | SDLoc dl(GA); |
7755 | 188 | const GlobalValue *GV = GA->getGlobal(); |
7756 | 188 | Align Alignment = GV->getPointerAlignment(CurDAG->getDataLayout()); |
7757 | | // We can't perform this optimization for data whose alignment |
7758 | | // is insufficient for the instruction encoding. |
7759 | 188 | if (Alignment < 4 && (RequiresMod4Offset || (Offset % 4) != 0)) { |
7760 | 1 | LLVM_DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n"); |
7761 | 1 | continue; |
7762 | 1 | } |
7763 | 187 | ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, Offset, Flags); |
7764 | 7.94k | } else if (ConstantPoolSDNode *CP = |
7765 | 7.94k | dyn_cast<ConstantPoolSDNode>(ImmOpnd)) { |
7766 | 7.94k | const Constant *C = CP->getConstVal(); |
7767 | 7.94k | ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64, CP->getAlign(), |
7768 | 7.94k | Offset, Flags); |
7769 | 7.94k | } |
7770 | 8.13k | } |
7771 | | |
7772 | 8.13k | if (FirstOp == 1) // Store |
7773 | 18 | (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd, |
7774 | 18 | Base.getOperand(0), N->getOperand(3)); |
7775 | 8.11k | else // Load |
7776 | 8.11k | (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0), |
7777 | 8.11k | N->getOperand(2)); |
7778 | | |
7779 | 8.13k | if (UpdateHBase) |
7780 | 0 | (void)CurDAG->UpdateNodeOperands(HBase.getNode(), HBase.getOperand(0), |
7781 | 0 | ImmOpnd); |
7782 | | |
7783 | | // The add-immediate may now be dead, in which case remove it. |
7784 | 8.13k | if (Base.getNode()->use_empty()) |
7785 | 8.11k | CurDAG->RemoveDeadNode(Base.getNode()); |
7786 | 8.13k | } |
7787 | 56.9k | } |
7788 | | |
7789 | | /// createPPCISelDag - This pass converts a legalized DAG into a |
7790 | | /// PowerPC-specific DAG, ready for instruction scheduling. |
7791 | | /// |
7792 | | FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM, |
7793 | 8.28k | CodeGenOptLevel OptLevel) { |
7794 | 8.28k | return new PPCDAGToDAGISel(TM, OptLevel); |
7795 | 8.28k | } |