blob: e04d7e1bc359bb09c42b869d68acc4a6b95cc9ba [file] [log] [blame]
Nate Begeman6cca84e2005-10-16 05:39:50 +00001//===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===//
Chris Lattner43ff01e2005-08-17 19:33:03 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner43ff01e2005-08-17 19:33:03 +00007//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman6cca84e2005-10-16 05:39:50 +000010// This file defines a pattern matching instruction selector for PowerPC,
Chris Lattner43ff01e2005-08-17 19:33:03 +000011// converting from a legalized dag to a PPC dag.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000015#include "PPC.h"
Evan Cheng11424442011-07-26 00:24:13 +000016#include "MCTargetDesc/PPCPredicates.h"
Hal Finkel3ee2af72014-07-18 23:29:49 +000017#include "PPCMachineFunctionInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "PPCTargetMachine.h"
Chris Lattner45640392005-08-19 22:38:53 +000019#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000022#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
25#include "llvm/IR/Function.h"
Chandler Carruth1fe21fc2013-01-19 08:03:47 +000026#include "llvm/IR/GlobalAlias.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/GlobalValue.h"
28#include "llvm/IR/GlobalVariable.h"
29#include "llvm/IR/Intrinsics.h"
Justin Hibbitsa88b6052014-11-12 15:16:30 +000030#include "llvm/IR/Module.h"
Hal Finkel940ab932014-02-28 00:27:01 +000031#include "llvm/Support/CommandLine.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000032#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000033#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000035#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Target/TargetOptions.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000037using namespace llvm;
38
Chandler Carruth84e68b22014-04-22 02:41:26 +000039#define DEBUG_TYPE "ppc-codegen"
40
Hal Finkel940ab932014-02-28 00:27:01 +000041// FIXME: Remove this once the bug has been fixed!
42cl::opt<bool> ANDIGlueBug("expose-ppc-andi-glue-bug",
43cl::desc("expose the ANDI glue bug on PPC"), cl::Hidden);
44
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000045namespace llvm {
46 void initializePPCDAGToDAGISelPass(PassRegistry&);
47}
48
Chris Lattner43ff01e2005-08-17 19:33:03 +000049namespace {
Chris Lattner43ff01e2005-08-17 19:33:03 +000050 //===--------------------------------------------------------------------===//
Nate Begeman0b71e002005-10-18 00:28:58 +000051 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattner43ff01e2005-08-17 19:33:03 +000052 /// instructions for SelectionDAG operations.
53 ///
Nick Lewycky02d5f772009-10-25 06:33:48 +000054 class PPCDAGToDAGISel : public SelectionDAGISel {
Dan Gohman21cea8a2010-04-17 15:26:15 +000055 const PPCTargetMachine &TM;
Eric Christopher1b8e7632014-05-22 01:07:24 +000056 const PPCTargetLowering *PPCLowering;
57 const PPCSubtarget *PPCSubTarget;
Chris Lattner45640392005-08-19 22:38:53 +000058 unsigned GlobalBaseReg;
Chris Lattner43ff01e2005-08-17 19:33:03 +000059 public:
Dan Gohman56e3f632008-07-07 18:00:37 +000060 explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
Eric Christopherd9134482014-08-04 21:25:23 +000061 : SelectionDAGISel(tm), TM(tm),
62 PPCLowering(TM.getSubtargetImpl()->getTargetLowering()),
63 PPCSubTarget(TM.getSubtargetImpl()) {
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000064 initializePPCDAGToDAGISelPass(*PassRegistry::getPassRegistry());
65 }
Andrew Trickc416ba62010-12-24 04:28:06 +000066
Craig Topper0d3fa922014-04-29 07:57:37 +000067 bool runOnMachineFunction(MachineFunction &MF) override {
Chris Lattner45640392005-08-19 22:38:53 +000068 // Make sure we re-emit a set of the global base reg if necessary
69 GlobalBaseReg = 0;
Eric Christopherd9134482014-08-04 21:25:23 +000070 PPCLowering = TM.getSubtargetImpl()->getTargetLowering();
Eric Christopher1b8e7632014-05-22 01:07:24 +000071 PPCSubTarget = TM.getSubtargetImpl();
Dan Gohman5ea74d52009-07-31 18:16:33 +000072 SelectionDAGISel::runOnMachineFunction(MF);
Andrew Trickc416ba62010-12-24 04:28:06 +000073
Eric Christopher1b8e7632014-05-22 01:07:24 +000074 if (!PPCSubTarget->isSVR4ABI())
Bill Schmidt38d94582012-10-10 20:54:15 +000075 InsertVRSaveCode(MF);
76
Chris Lattner1678a6c2006-03-16 18:25:23 +000077 return true;
Chris Lattner45640392005-08-19 22:38:53 +000078 }
Andrew Trickc416ba62010-12-24 04:28:06 +000079
Craig Topper0d3fa922014-04-29 07:57:37 +000080 void PostprocessISelDAG() override;
Bill Schmidtf5b474c2013-02-21 00:38:25 +000081
Chris Lattner43ff01e2005-08-17 19:33:03 +000082 /// getI32Imm - Return a target constant with the specified value, of type
83 /// i32.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000084 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000085 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattner43ff01e2005-08-17 19:33:03 +000086 }
Chris Lattner45640392005-08-19 22:38:53 +000087
Chris Lattner97b3da12006-06-27 00:04:13 +000088 /// getI64Imm - Return a target constant with the specified value, of type
89 /// i64.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000090 inline SDValue getI64Imm(uint64_t Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000091 return CurDAG->getTargetConstant(Imm, MVT::i64);
Chris Lattner97b3da12006-06-27 00:04:13 +000092 }
Andrew Trickc416ba62010-12-24 04:28:06 +000093
Chris Lattner97b3da12006-06-27 00:04:13 +000094 /// getSmallIPtrImm - Return a target constant of pointer type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000095 inline SDValue getSmallIPtrImm(unsigned Imm) {
Eric Christopher1b8e7632014-05-22 01:07:24 +000096 return CurDAG->getTargetConstant(Imm, PPCLowering->getPointerTy());
Chris Lattner97b3da12006-06-27 00:04:13 +000097 }
Andrew Trickc416ba62010-12-24 04:28:06 +000098
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +000099 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
Nate Begemand31efd12006-09-22 05:01:56 +0000100 /// with any number of 0s on either side. The 1s are allowed to wrap from
101 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
102 /// 0x0F0F0000 is not, since all 1s are not contiguous.
103 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
104
105
106 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
107 /// rotate and mask opcode and mask operation.
Dale Johannesen86dcae12009-11-24 01:09:07 +0000108 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
Nate Begemand31efd12006-09-22 05:01:56 +0000109 unsigned &SH, unsigned &MB, unsigned &ME);
Andrew Trickc416ba62010-12-24 04:28:06 +0000110
Chris Lattner45640392005-08-19 22:38:53 +0000111 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
112 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000113 SDNode *getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +0000114
Chris Lattner43ff01e2005-08-17 19:33:03 +0000115 // Select - Convert the specified operand from a target-independent to a
116 // target-specific node if it hasn't already been changed.
Craig Topper0d3fa922014-04-29 07:57:37 +0000117 SDNode *Select(SDNode *N) override;
Andrew Trickc416ba62010-12-24 04:28:06 +0000118
Nate Begeman93c4bc62005-08-19 00:38:14 +0000119 SDNode *SelectBitfieldInsert(SDNode *N);
120
Chris Lattner2a1823d2005-08-21 18:50:37 +0000121 /// SelectCC - Select a comparison of the specified values with the
122 /// specified condition code, returning the CR# of the expression.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000123 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDLoc dl);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000124
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000125 /// SelectAddrImm - Returns true if the address N can be represented by
126 /// a base register plus a signed 16-bit displacement [r+imm].
Chris Lattner0e023ea2010-09-21 20:31:19 +0000127 bool SelectAddrImm(SDValue N, SDValue &Disp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000128 SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000129 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, false);
Chris Lattnera801fced2006-11-08 02:15:41 +0000130 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000131
Chris Lattner6f5840c2006-11-16 00:41:37 +0000132 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000133 /// immediate field. Note that the operand at this point is already the
134 /// result of a prior SelectAddressRegImm call.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000135 bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000136 if (N.getOpcode() == ISD::TargetConstant ||
Hal Finkela86b0f22012-06-21 20:10:48 +0000137 N.getOpcode() == ISD::TargetGlobalAddress) {
Hal Finkel1cc27e42012-06-19 02:34:32 +0000138 Out = N;
139 return true;
140 }
141
142 return false;
143 }
144
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000145 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
146 /// represented as an indexed [r+r] operation. Returns false if it can
147 /// be represented by [r+imm], which are preferred.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000148 bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000149 return PPCLowering->SelectAddressRegReg(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000150 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000151
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000152 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
153 /// represented as an indexed [r+r] operation.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000154 bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000155 return PPCLowering->SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000156 }
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000157
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000158 /// SelectAddrImmX4 - Returns true if the address N can be represented by
159 /// a base register plus a signed 16-bit displacement that is a multiple of 4.
160 /// Suitable for use by STD and friends.
161 bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000162 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, true);
Chris Lattnera801fced2006-11-08 02:15:41 +0000163 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000164
Hal Finkel756810f2013-03-21 21:37:52 +0000165 // Select an address into a single register.
166 bool SelectAddr(SDValue N, SDValue &Base) {
167 Base = N;
168 return true;
169 }
170
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000171 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
Dale Johannesen4a50e682009-08-18 00:18:39 +0000172 /// inline asm expressions. It is always correct to compute the value into
173 /// a register. The case of adding a (possibly relocatable) constant to a
174 /// register can be improved, but it is wrong to substitute Reg+Reg for
175 /// Reg in an asm, because the load or store opcode would have to change.
Hal Finkeld4338382014-12-03 23:40:13 +0000176 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Craig Topper0d3fa922014-04-29 07:57:37 +0000177 char ConstraintCode,
178 std::vector<SDValue> &OutOps) override {
Hal Finkeld4338382014-12-03 23:40:13 +0000179 // We need to make sure that this one operand does not end up in r0
180 // (because we might end up lowering this as 0(%op)).
181 const TargetRegisterInfo *TRI = TM.getSubtargetImpl()->getRegisterInfo();
182 const TargetRegisterClass *TRC = TRI->getPointerRegClass(*MF, /*Kind=*/1);
183 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
184 SDValue NewOp =
185 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
186 SDLoc(Op), Op.getValueType(),
187 Op, RC), 0);
188
189 OutOps.push_back(NewOp);
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000190 return false;
191 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000192
Dan Gohman5ea74d52009-07-31 18:16:33 +0000193 void InsertVRSaveCode(MachineFunction &MF);
Chris Lattner1678a6c2006-03-16 18:25:23 +0000194
Craig Topper0d3fa922014-04-29 07:57:37 +0000195 const char *getPassName() const override {
Chris Lattner43ff01e2005-08-17 19:33:03 +0000196 return "PowerPC DAG->DAG Pattern Instruction Selection";
Andrew Trickc416ba62010-12-24 04:28:06 +0000197 }
198
Chris Lattner03e08ee2005-09-13 22:03:06 +0000199// Include the pieces autogenerated from the target description.
Chris Lattner0921e3b2005-10-14 23:37:35 +0000200#include "PPCGenDAGISel.inc"
Andrew Trickc416ba62010-12-24 04:28:06 +0000201
Chris Lattner259e6c72005-10-06 18:45:51 +0000202private:
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000203 SDNode *SelectSETCC(SDNode *N);
Hal Finkel940ab932014-02-28 00:27:01 +0000204
205 void PeepholePPC64();
Eric Christopher02e18042014-05-14 00:31:15 +0000206 void PeepholeCROps();
Hal Finkelb9989152014-02-28 06:11:16 +0000207
208 bool AllUsersSelectZero(SDNode *N);
209 void SwapAllSelectUsers(SDNode *N);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000210 };
211}
212
Chris Lattner1678a6c2006-03-16 18:25:23 +0000213/// InsertVRSaveCode - Once the entire function has been instruction selected,
214/// all virtual registers are created and all machine instructions are built,
215/// check to see if we need to save/restore VRSAVE. If so, do it.
Dan Gohman5ea74d52009-07-31 18:16:33 +0000216void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000217 // Check to see if this function uses vector registers, which means we have to
Andrew Trickc416ba62010-12-24 04:28:06 +0000218 // save and restore the VRSAVE register and update it with the regs we use.
Chris Lattner02e2c182006-03-13 21:52:10 +0000219 //
Dan Gohman4a618822010-02-10 16:03:48 +0000220 // In this case, there will be virtual registers of vector type created
Chris Lattner02e2c182006-03-13 21:52:10 +0000221 // by the scheduler. Detect them now.
Chris Lattner02e2c182006-03-13 21:52:10 +0000222 bool HasVectorVReg = false;
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000223 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
224 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
225 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000226 HasVectorVReg = true;
227 break;
228 }
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000229 }
Chris Lattner1678a6c2006-03-16 18:25:23 +0000230 if (!HasVectorVReg) return; // nothing to do.
Andrew Trickc416ba62010-12-24 04:28:06 +0000231
Chris Lattner02e2c182006-03-13 21:52:10 +0000232 // If we have a vector register, we want to emit code into the entry and exit
233 // blocks to save and restore the VRSAVE register. We do this here (instead
234 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
235 //
236 // 1. This (trivially) reduces the load on the register allocator, by not
237 // having to represent the live range of the VRSAVE register.
238 // 2. This (more significantly) allows us to create a temporary virtual
239 // register to hold the saved VRSAVE value, allowing this temporary to be
240 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000241
242 // Create two vregs - one to hold the VRSAVE register that is live-in to the
243 // function and one for the value after having bits or'd into it.
Chris Lattnera10fff52007-12-31 04:13:23 +0000244 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
245 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Andrew Trickc416ba62010-12-24 04:28:06 +0000246
Eric Christopherd9134482014-08-04 21:25:23 +0000247 const TargetInstrInfo &TII = *TM.getSubtargetImpl()->getInstrInfo();
Chris Lattner1678a6c2006-03-16 18:25:23 +0000248 MachineBasicBlock &EntryBB = *Fn.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000249 DebugLoc dl;
Chris Lattner1678a6c2006-03-16 18:25:23 +0000250 // Emit the following code into the entry block:
251 // InVRSAVE = MFVRSAVE
252 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
253 // MTVRSAVE UpdatedVRSAVE
254 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Dale Johannesene9f623e2009-02-13 02:27:39 +0000255 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
256 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
Chris Lattnera98c6792008-01-07 01:56:04 +0000257 UpdatedVRSAVE).addReg(InVRSAVE);
Dale Johannesene9f623e2009-02-13 02:27:39 +0000258 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000259
Chris Lattner1678a6c2006-03-16 18:25:23 +0000260 // Find all return blocks, outputting a restore in each epilog.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000261 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000262 if (!BB->empty() && BB->back().isReturn()) {
Chris Lattner1678a6c2006-03-16 18:25:23 +0000263 IP = BB->end(); --IP;
Andrew Trickc416ba62010-12-24 04:28:06 +0000264
Chris Lattner1678a6c2006-03-16 18:25:23 +0000265 // Skip over all terminator instructions, which are part of the return
266 // sequence.
267 MachineBasicBlock::iterator I2 = IP;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000268 while (I2 != BB->begin() && (--I2)->isTerminator())
Chris Lattner1678a6c2006-03-16 18:25:23 +0000269 IP = I2;
Andrew Trickc416ba62010-12-24 04:28:06 +0000270
Chris Lattner1678a6c2006-03-16 18:25:23 +0000271 // Emit: MTVRSAVE InVRSave
Dale Johannesene9f623e2009-02-13 02:27:39 +0000272 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000273 }
Chris Lattner02e2c182006-03-13 21:52:10 +0000274 }
Chris Lattner259e6c72005-10-06 18:45:51 +0000275}
Chris Lattner8ae95252005-09-03 01:17:22 +0000276
Chris Lattner1678a6c2006-03-16 18:25:23 +0000277
Chris Lattner45640392005-08-19 22:38:53 +0000278/// getGlobalBaseReg - Output the instructions required to put the
279/// base address to use for accessing globals into a register.
280///
Evan Cheng61413a32006-08-26 05:34:46 +0000281SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner45640392005-08-19 22:38:53 +0000282 if (!GlobalBaseReg) {
Eric Christopherd9134482014-08-04 21:25:23 +0000283 const TargetInstrInfo &TII = *TM.getSubtargetImpl()->getInstrInfo();
Chris Lattner45640392005-08-19 22:38:53 +0000284 // Insert the set of GlobalBaseReg into the first MBB of the function
Dan Gohmanfca89682009-08-15 02:07:36 +0000285 MachineBasicBlock &FirstMBB = MF->front();
Chris Lattner45640392005-08-19 22:38:53 +0000286 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000287 const Module *M = MF->getFunction()->getParent();
Chris Lattner6f306d72010-04-02 20:16:16 +0000288 DebugLoc dl;
Chris Lattner97b3da12006-06-27 00:04:13 +0000289
Eric Christopher1b8e7632014-05-22 01:07:24 +0000290 if (PPCLowering->getPointerTy() == MVT::i32) {
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000291 if (PPCSubTarget->isTargetELF()) {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000292 GlobalBaseReg = PPC::R30;
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000293 if (M->getPICLevel() == PICLevel::Small) {
294 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MoveGOTtoLR));
295 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
296 } else {
297 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
298 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
299 unsigned TempReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
300 BuildMI(FirstMBB, MBBI, dl,
301 TII.get(PPC::UpdateGBR)).addReg(GlobalBaseReg)
302 .addReg(TempReg, RegState::Define).addReg(GlobalBaseReg);
303 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true);
304 }
305 } else {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000306 GlobalBaseReg =
307 RegInfo->createVirtualRegister(&PPC::GPRC_NOR0RegClass);
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000308 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
309 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Hal Finkel3ee2af72014-07-18 23:29:49 +0000310 }
Chris Lattnerb5429252006-11-14 18:43:11 +0000311 } else {
Hal Finkel6daf2aa2014-03-06 01:28:23 +0000312 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_NOX0RegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000313 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000314 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000315 }
Chris Lattner45640392005-08-19 22:38:53 +0000316 }
Gabor Greif81d6a382008-08-31 15:37:04 +0000317 return CurDAG->getRegister(GlobalBaseReg,
Eric Christopher1b8e7632014-05-22 01:07:24 +0000318 PPCLowering->getPointerTy()).getNode();
Chris Lattner97b3da12006-06-27 00:04:13 +0000319}
320
321/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
322/// or 64-bit immediate, and if the value can be accurately represented as a
323/// sign extension from a 16-bit value. If so, this returns true and the
324/// immediate.
325static bool isIntS16Immediate(SDNode *N, short &Imm) {
326 if (N->getOpcode() != ISD::Constant)
327 return false;
328
Dan Gohmaneffb8942008-09-12 16:56:44 +0000329 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +0000330 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +0000331 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000332 else
Dan Gohmaneffb8942008-09-12 16:56:44 +0000333 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000334}
335
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000336static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000337 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattner45640392005-08-19 22:38:53 +0000338}
339
340
Chris Lattner97b3da12006-06-27 00:04:13 +0000341/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
342/// operand. If so Imm will receive the 32-bit value.
343static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000344 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000345 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Nate Begeman72d6f882005-08-18 05:00:13 +0000346 return true;
347 }
348 return false;
349}
350
Chris Lattner97b3da12006-06-27 00:04:13 +0000351/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
352/// operand. If so Imm will receive the 64-bit value.
353static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000354 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000355 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000356 return true;
357 }
358 return false;
359}
360
361// isInt32Immediate - This method tests to see if a constant operand.
362// If so Imm will receive the 32 bit value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000363static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000364 return isInt32Immediate(N.getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000365}
366
367
368// isOpcWithIntImmediate - This method tests to see if the node is a specific
369// opcode and that it has a immediate integer right operand.
370// If so Imm will receive the 32 bit value.
371static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greif81d6a382008-08-31 15:37:04 +0000372 return N->getOpcode() == Opc
373 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000374}
375
Nate Begemand31efd12006-09-22 05:01:56 +0000376bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
Hal Finkelff3ea802013-07-11 16:31:51 +0000377 if (!Val)
378 return false;
379
Nate Begemanb3821a32005-08-18 07:30:46 +0000380 if (isShiftedMask_32(Val)) {
381 // look for the first non-zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000382 MB = countLeadingZeros(Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000383 // look for the first zero bit after the run of ones
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000384 ME = countLeadingZeros((Val - 1) ^ Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000385 return true;
Chris Lattner666512c2005-08-25 04:47:18 +0000386 } else {
387 Val = ~Val; // invert mask
388 if (isShiftedMask_32(Val)) {
389 // effectively look for the first zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000390 ME = countLeadingZeros(Val) - 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000391 // effectively look for the first one bit after the run of zeros
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000392 MB = countLeadingZeros((Val - 1) ^ Val) + 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000393 return true;
394 }
Nate Begemanb3821a32005-08-18 07:30:46 +0000395 }
396 // no run present
397 return false;
398}
399
Andrew Trickc416ba62010-12-24 04:28:06 +0000400bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
401 bool isShiftMask, unsigned &SH,
Nate Begemand31efd12006-09-22 05:01:56 +0000402 unsigned &MB, unsigned &ME) {
Nate Begeman92e77502005-10-19 00:05:37 +0000403 // Don't even go down this path for i64, since different logic will be
404 // necessary for rldicl/rldicr/rldimi.
Owen Anderson9f944592009-08-11 20:47:22 +0000405 if (N->getValueType(0) != MVT::i32)
Nate Begeman92e77502005-10-19 00:05:37 +0000406 return false;
407
Nate Begemanb3821a32005-08-18 07:30:46 +0000408 unsigned Shift = 32;
409 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
410 unsigned Opcode = N->getOpcode();
Chris Lattnere413b602005-08-30 00:59:16 +0000411 if (N->getNumOperands() != 2 ||
Gabor Greiff304a7a2008-08-28 21:40:38 +0000412 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Nate Begemanb3821a32005-08-18 07:30:46 +0000413 return false;
Andrew Trickc416ba62010-12-24 04:28:06 +0000414
Nate Begemanb3821a32005-08-18 07:30:46 +0000415 if (Opcode == ISD::SHL) {
416 // apply shift left to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000417 if (isShiftMask) Mask = Mask << Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000418 // determine which bits are made indeterminant by shift
419 Indeterminant = ~(0xFFFFFFFFu << Shift);
Andrew Trickc416ba62010-12-24 04:28:06 +0000420 } else if (Opcode == ISD::SRL) {
Nate Begemanb3821a32005-08-18 07:30:46 +0000421 // apply shift right to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000422 if (isShiftMask) Mask = Mask >> Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000423 // determine which bits are made indeterminant by shift
424 Indeterminant = ~(0xFFFFFFFFu >> Shift);
425 // adjust for the left rotate
426 Shift = 32 - Shift;
Nate Begemand31efd12006-09-22 05:01:56 +0000427 } else if (Opcode == ISD::ROTL) {
428 Indeterminant = 0;
Nate Begemanb3821a32005-08-18 07:30:46 +0000429 } else {
430 return false;
431 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000432
Nate Begemanb3821a32005-08-18 07:30:46 +0000433 // if the mask doesn't intersect any Indeterminant bits
434 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnera2963392006-05-12 16:29:37 +0000435 SH = Shift & 31;
Nate Begemanb3821a32005-08-18 07:30:46 +0000436 // make sure the mask is still a mask (wrap arounds may not be)
437 return isRunOfOnes(Mask, MB, ME);
438 }
439 return false;
440}
441
Nate Begeman93c4bc62005-08-19 00:38:14 +0000442/// SelectBitfieldInsert - turn an or of two masked values into
443/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman0b71e002005-10-18 00:28:58 +0000444SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000445 SDValue Op0 = N->getOperand(0);
446 SDValue Op1 = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000447 SDLoc dl(N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000448
Dan Gohmanf19609a2008-02-27 01:23:58 +0000449 APInt LKZ, LKO, RKZ, RKO;
Jay Foada0653a32014-05-14 21:14:37 +0000450 CurDAG->computeKnownBits(Op0, LKZ, LKO);
451 CurDAG->computeKnownBits(Op1, RKZ, RKO);
Andrew Trickc416ba62010-12-24 04:28:06 +0000452
Dan Gohmanf19609a2008-02-27 01:23:58 +0000453 unsigned TargetMask = LKZ.getZExtValue();
454 unsigned InsertMask = RKZ.getZExtValue();
Andrew Trickc416ba62010-12-24 04:28:06 +0000455
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000456 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
457 unsigned Op0Opc = Op0.getOpcode();
458 unsigned Op1Opc = Op1.getOpcode();
459 unsigned Value, SH = 0;
460 TargetMask = ~TargetMask;
461 InsertMask = ~InsertMask;
Nate Begeman1333cea2006-05-07 00:23:38 +0000462
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000463 // If the LHS has a foldable shift and the RHS does not, then swap it to the
464 // RHS so that we can fold the shift into the insert.
Nate Begeman1333cea2006-05-07 00:23:38 +0000465 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
466 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
467 Op0.getOperand(0).getOpcode() == ISD::SRL) {
468 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
469 Op1.getOperand(0).getOpcode() != ISD::SRL) {
470 std::swap(Op0, Op1);
471 std::swap(Op0Opc, Op1Opc);
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000472 std::swap(TargetMask, InsertMask);
Nate Begeman1333cea2006-05-07 00:23:38 +0000473 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000474 }
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000475 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
476 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
477 Op1.getOperand(0).getOpcode() != ISD::SRL) {
478 std::swap(Op0, Op1);
479 std::swap(Op0Opc, Op1Opc);
480 std::swap(TargetMask, InsertMask);
481 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000482 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000483
Nate Begeman1333cea2006-05-07 00:23:38 +0000484 unsigned MB, ME;
Hal Finkelff3ea802013-07-11 16:31:51 +0000485 if (isRunOfOnes(InsertMask, MB, ME)) {
Dale Johannesen8495a502009-11-20 22:16:40 +0000486 SDValue Tmp1, Tmp2;
Nate Begeman1333cea2006-05-07 00:23:38 +0000487
488 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000489 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000490 Op1 = Op1.getOperand(0);
491 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
492 }
493 if (Op1Opc == ISD::AND) {
Hal Finkeld9963c72014-04-13 17:10:58 +0000494 // The AND mask might not be a constant, and we need to make sure that
495 // if we're going to fold the masking with the insert, all bits not
496 // know to be zero in the mask are known to be one.
497 APInt MKZ, MKO;
Jay Foada0653a32014-05-14 21:14:37 +0000498 CurDAG->computeKnownBits(Op1.getOperand(1), MKZ, MKO);
Hal Finkeld9963c72014-04-13 17:10:58 +0000499 bool CanFoldMask = InsertMask == MKO.getZExtValue();
500
Nate Begeman1333cea2006-05-07 00:23:38 +0000501 unsigned SHOpc = Op1.getOperand(0).getOpcode();
Hal Finkeld9963c72014-04-13 17:10:58 +0000502 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000503 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Eric Christopher02e18042014-05-14 00:31:15 +0000504 // Note that Value must be in range here (less than 32) because
505 // otherwise there would not be any bits set in InsertMask.
Nate Begeman1333cea2006-05-07 00:23:38 +0000506 Op1 = Op1.getOperand(0).getOperand(0);
507 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
Nate Begeman1333cea2006-05-07 00:23:38 +0000508 }
509 }
Dale Johannesen8495a502009-11-20 22:16:40 +0000510
Chris Lattnera2963392006-05-12 16:29:37 +0000511 SH &= 31;
Dale Johannesen8495a502009-11-20 22:16:40 +0000512 SDValue Ops[] = { Op0, Op1, getI32Imm(SH), getI32Imm(MB),
Evan Chengc3acfc02006-08-27 08:14:06 +0000513 getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +0000514 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000515 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000516 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000517 return nullptr;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000518}
519
Chris Lattner2a1823d2005-08-21 18:50:37 +0000520/// SelectCC - Select a comparison of the specified values with the specified
521/// condition code, returning the CR# of the expression.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000522SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000523 ISD::CondCode CC, SDLoc dl) {
Chris Lattner2a1823d2005-08-21 18:50:37 +0000524 // Always select the LHS.
Chris Lattner97b3da12006-06-27 00:04:13 +0000525 unsigned Opc;
Andrew Trickc416ba62010-12-24 04:28:06 +0000526
Owen Anderson9f944592009-08-11 20:47:22 +0000527 if (LHS.getValueType() == MVT::i32) {
Chris Lattner9a40cca2006-06-27 00:10:13 +0000528 unsigned Imm;
Chris Lattneraa3926b2006-09-20 04:25:47 +0000529 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
530 if (isInt32Immediate(RHS, Imm)) {
531 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000532 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000533 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
534 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +0000535 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000536 if (isInt<16>((int)Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000537 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
538 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000539
Chris Lattneraa3926b2006-09-20 04:25:47 +0000540 // For non-equality comparisons, the default code would materialize the
541 // constant, then compare against it, like this:
542 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +0000543 // ori r2, r2, 22136
Chris Lattneraa3926b2006-09-20 04:25:47 +0000544 // cmpw cr0, r3, r2
545 // Since we are just comparing for equality, we can emit this instead:
546 // xoris r0,r3,0x1234
547 // cmplwi cr0,r0,0x5678
548 // beq cr0,L6
Dan Gohman32f71d72009-09-25 18:54:59 +0000549 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
550 getI32Imm(Imm >> 16)), 0);
551 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
552 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +0000553 }
554 Opc = PPC::CMPLW;
555 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +0000556 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000557 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
558 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +0000559 Opc = PPC::CMPLW;
560 } else {
561 short SImm;
562 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000563 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
564 getI32Imm((int)SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +0000565 0);
566 Opc = PPC::CMPW;
567 }
Owen Anderson9f944592009-08-11 20:47:22 +0000568 } else if (LHS.getValueType() == MVT::i64) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000569 uint64_t Imm;
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000570 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000571 if (isInt64Immediate(RHS.getNode(), Imm)) {
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000572 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000573 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000574 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
575 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000576 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000577 if (isInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000578 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
579 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000580
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000581 // For non-equality comparisons, the default code would materialize the
582 // constant, then compare against it, like this:
583 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +0000584 // ori r2, r2, 22136
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000585 // cmpd cr0, r3, r2
586 // Since we are just comparing for equality, we can emit this instead:
587 // xoris r0,r3,0x1234
588 // cmpldi cr0,r0,0x5678
589 // beq cr0,L6
Benjamin Kramer2788f792010-03-29 21:13:41 +0000590 if (isUInt<32>(Imm)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000591 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
592 getI64Imm(Imm >> 16)), 0);
593 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
594 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000595 }
596 }
597 Opc = PPC::CMPLD;
598 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +0000599 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000600 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
601 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +0000602 Opc = PPC::CMPLD;
603 } else {
604 short SImm;
605 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000606 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
607 getI64Imm(SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +0000608 0);
609 Opc = PPC::CMPD;
610 }
Owen Anderson9f944592009-08-11 20:47:22 +0000611 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000612 Opc = PPC::FCMPUS;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000613 } else {
Owen Anderson9f944592009-08-11 20:47:22 +0000614 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
Eric Christopher1b8e7632014-05-22 01:07:24 +0000615 Opc = PPCSubTarget->hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000616 }
Dan Gohman32f71d72009-09-25 18:54:59 +0000617 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000618}
619
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000620static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
Chris Lattner2a1823d2005-08-21 18:50:37 +0000621 switch (CC) {
Chris Lattner630bbce2006-05-25 16:54:16 +0000622 case ISD::SETUEQ:
Dale Johannesen160be0f2008-11-07 22:54:33 +0000623 case ISD::SETONE:
624 case ISD::SETOLE:
625 case ISD::SETOGE:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000626 llvm_unreachable("Should be lowered by legalize!");
627 default: llvm_unreachable("Unknown condition!");
Dale Johannesen160be0f2008-11-07 22:54:33 +0000628 case ISD::SETOEQ:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000629 case ISD::SETEQ: return PPC::PRED_EQ;
Chris Lattner630bbce2006-05-25 16:54:16 +0000630 case ISD::SETUNE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000631 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000632 case ISD::SETOLT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000633 case ISD::SETLT: return PPC::PRED_LT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000634 case ISD::SETULE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000635 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000636 case ISD::SETOGT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000637 case ISD::SETGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000638 case ISD::SETUGE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000639 case ISD::SETGE: return PPC::PRED_GE;
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000640 case ISD::SETO: return PPC::PRED_NU;
641 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000642 // These two are invalid for floating point. Assume we have int.
643 case ISD::SETULT: return PPC::PRED_LT;
644 case ISD::SETUGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000645 }
Chris Lattner2a1823d2005-08-21 18:50:37 +0000646}
647
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000648/// getCRIdxForSetCC - Return the index of the condition register field
649/// associated with the SetCC condition, and whether or not the field is
650/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Ulrich Weigand47e93282013-07-03 15:13:30 +0000651static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
Chris Lattner89f36e62008-01-08 06:46:30 +0000652 Invert = false;
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000653 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000654 default: llvm_unreachable("Unknown condition!");
Chris Lattner89f36e62008-01-08 06:46:30 +0000655 case ISD::SETOLT:
656 case ISD::SETLT: return 0; // Bit #0 = SETOLT
657 case ISD::SETOGT:
658 case ISD::SETGT: return 1; // Bit #1 = SETOGT
659 case ISD::SETOEQ:
660 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
661 case ISD::SETUO: return 3; // Bit #3 = SETUO
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000662 case ISD::SETUGE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000663 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000664 case ISD::SETULE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000665 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Chris Lattner1fbb0d32006-05-25 18:06:16 +0000666 case ISD::SETUNE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000667 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
668 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Andrew Trickc416ba62010-12-24 04:28:06 +0000669 case ISD::SETUEQ:
670 case ISD::SETOGE:
671 case ISD::SETOLE:
Dale Johannesen160be0f2008-11-07 22:54:33 +0000672 case ISD::SETONE:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000673 llvm_unreachable("Invalid branch code: should be expanded by legalize");
Dale Johannesen160be0f2008-11-07 22:54:33 +0000674 // These are invalid for floating point. Assume integer.
675 case ISD::SETULT: return 0;
676 case ISD::SETUGT: return 1;
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000677 }
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000678}
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000679
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000680// getVCmpInst: return the vector compare instruction for the specified
681// vector type and condition code. Since this is for altivec specific code,
682// only support the altivec types (v16i8, v8i16, v4i32, and v4f32).
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +0000683static unsigned int getVCmpInst(MVT VecVT, ISD::CondCode CC,
684 bool HasVSX, bool &Swap, bool &Negate) {
685 Swap = false;
686 Negate = false;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000687
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +0000688 if (VecVT.isFloatingPoint()) {
689 /* Handle some cases by swapping input operands. */
690 switch (CC) {
691 case ISD::SETLE: CC = ISD::SETGE; Swap = true; break;
692 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
693 case ISD::SETOLE: CC = ISD::SETOGE; Swap = true; break;
694 case ISD::SETOLT: CC = ISD::SETOGT; Swap = true; break;
695 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
696 case ISD::SETUGT: CC = ISD::SETULT; Swap = true; break;
697 default: break;
698 }
699 /* Handle some cases by negating the result. */
700 switch (CC) {
701 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
702 case ISD::SETUNE: CC = ISD::SETOEQ; Negate = true; break;
703 case ISD::SETULE: CC = ISD::SETOGT; Negate = true; break;
704 case ISD::SETULT: CC = ISD::SETOGE; Negate = true; break;
705 default: break;
706 }
707 /* We have instructions implementing the remaining cases. */
708 switch (CC) {
709 case ISD::SETEQ:
710 case ISD::SETOEQ:
711 if (VecVT == MVT::v4f32)
712 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
713 else if (VecVT == MVT::v2f64)
714 return PPC::XVCMPEQDP;
715 break;
716 case ISD::SETGT:
717 case ISD::SETOGT:
718 if (VecVT == MVT::v4f32)
719 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
720 else if (VecVT == MVT::v2f64)
721 return PPC::XVCMPGTDP;
722 break;
723 case ISD::SETGE:
724 case ISD::SETOGE:
725 if (VecVT == MVT::v4f32)
726 return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP;
727 else if (VecVT == MVT::v2f64)
728 return PPC::XVCMPGEDP;
729 break;
730 default:
731 break;
732 }
733 llvm_unreachable("Invalid floating-point vector compare condition");
734 } else {
735 /* Handle some cases by swapping input operands. */
736 switch (CC) {
737 case ISD::SETGE: CC = ISD::SETLE; Swap = true; break;
738 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
739 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
740 case ISD::SETULT: CC = ISD::SETUGT; Swap = true; break;
741 default: break;
742 }
743 /* Handle some cases by negating the result. */
744 switch (CC) {
745 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
746 case ISD::SETUNE: CC = ISD::SETUEQ; Negate = true; break;
747 case ISD::SETLE: CC = ISD::SETGT; Negate = true; break;
748 case ISD::SETULE: CC = ISD::SETUGT; Negate = true; break;
749 default: break;
750 }
751 /* We have instructions implementing the remaining cases. */
752 switch (CC) {
753 case ISD::SETEQ:
754 case ISD::SETUEQ:
755 if (VecVT == MVT::v16i8)
756 return PPC::VCMPEQUB;
757 else if (VecVT == MVT::v8i16)
758 return PPC::VCMPEQUH;
759 else if (VecVT == MVT::v4i32)
760 return PPC::VCMPEQUW;
761 break;
762 case ISD::SETGT:
763 if (VecVT == MVT::v16i8)
764 return PPC::VCMPGTSB;
765 else if (VecVT == MVT::v8i16)
766 return PPC::VCMPGTSH;
767 else if (VecVT == MVT::v4i32)
768 return PPC::VCMPGTSW;
769 break;
770 case ISD::SETUGT:
771 if (VecVT == MVT::v16i8)
772 return PPC::VCMPGTUB;
773 else if (VecVT == MVT::v8i16)
774 return PPC::VCMPGTUH;
775 else if (VecVT == MVT::v4i32)
776 return PPC::VCMPGTUW;
777 break;
778 default:
779 break;
780 }
781 llvm_unreachable("Invalid integer vector compare condition");
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000782 }
783}
784
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000785SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000786 SDLoc dl(N);
Chris Lattner491b8292005-10-06 19:03:35 +0000787 unsigned Imm;
788 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Roman Divacky254f8212011-06-20 15:28:39 +0000789 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
790 bool isPPC64 = (PtrVT == MVT::i64);
791
Eric Christopher1b8e7632014-05-22 01:07:24 +0000792 if (!PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +0000793 isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner491b8292005-10-06 19:03:35 +0000794 // We can codegen setcc op, imm very efficiently compared to a brcond.
795 // Check for those cases here.
796 // setcc op, 0
797 if (Imm == 0) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000798 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +0000799 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +0000800 default: break;
Evan Chengc3acfc02006-08-27 08:14:06 +0000801 case ISD::SETEQ: {
Dan Gohman32f71d72009-09-25 18:54:59 +0000802 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000803 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000804 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +0000805 }
Chris Lattnere2969492005-10-21 21:17:10 +0000806 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +0000807 if (isPPC64) break;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000808 SDValue AD =
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000809 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000810 Op, getI32Imm(~0U)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000811 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000812 AD.getValue(1));
Chris Lattner491b8292005-10-06 19:03:35 +0000813 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000814 case ISD::SETLT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000815 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000816 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +0000817 }
Chris Lattnere2969492005-10-21 21:17:10 +0000818 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000819 SDValue T =
Dan Gohman32f71d72009-09-25 18:54:59 +0000820 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
821 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000822 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000823 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +0000824 }
825 }
Chris Lattner491b8292005-10-06 19:03:35 +0000826 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000827 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +0000828 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +0000829 default: break;
830 case ISD::SETEQ:
Roman Divacky254f8212011-06-20 15:28:39 +0000831 if (isPPC64) break;
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000832 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000833 Op, getI32Imm(1)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000834 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
835 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
Dan Gohman32f71d72009-09-25 18:54:59 +0000836 MVT::i32,
837 getI32Imm(0)), 0),
Dale Johannesenf08a47b2009-02-04 23:02:30 +0000838 Op.getValue(1));
Chris Lattnere2969492005-10-21 21:17:10 +0000839 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +0000840 if (isPPC64) break;
Dan Gohman32f71d72009-09-25 18:54:59 +0000841 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000842 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000843 Op, getI32Imm(~0U));
Owen Anderson9f944592009-08-11 20:47:22 +0000844 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000845 Op, SDValue(AD, 1));
Chris Lattner491b8292005-10-06 19:03:35 +0000846 }
Chris Lattnere2969492005-10-21 21:17:10 +0000847 case ISD::SETLT: {
Dan Gohman32f71d72009-09-25 18:54:59 +0000848 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
849 getI32Imm(1)), 0);
850 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
851 Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000852 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000853 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +0000854 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000855 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000856 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Michael Liaob53d8962013-04-19 22:22:57 +0000857 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
Dale Johannesenf08a47b2009-02-04 23:02:30 +0000858 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000859 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000860 getI32Imm(1));
Chris Lattnere2969492005-10-21 21:17:10 +0000861 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000862 }
Chris Lattner491b8292005-10-06 19:03:35 +0000863 }
864 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000865
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000866 SDValue LHS = N->getOperand(0);
867 SDValue RHS = N->getOperand(1);
868
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000869 // Altivec Vector compare instructions do not set any CR register by default and
870 // vector compare operations return the same type as the operands.
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000871 if (LHS.getValueType().isVector()) {
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000872 EVT VecVT = LHS.getValueType();
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +0000873 bool Swap, Negate;
874 unsigned int VCmpInst = getVCmpInst(VecVT.getSimpleVT(), CC,
875 PPCSubTarget->hasVSX(), Swap, Negate);
876 if (Swap)
877 std::swap(LHS, RHS);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000878
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +0000879 if (Negate) {
880 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
881 return CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLNOR :
882 PPC::VNOR,
883 VecVT, VCmp, VCmp);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000884 }
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +0000885
886 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000887 }
888
Eric Christopher1b8e7632014-05-22 01:07:24 +0000889 if (PPCSubTarget->useCRBits())
Craig Topper062a2ba2014-04-25 05:30:21 +0000890 return nullptr;
Hal Finkel940ab932014-02-28 00:27:01 +0000891
Chris Lattner491b8292005-10-06 19:03:35 +0000892 bool Inv;
Ulrich Weigand47e93282013-07-03 15:13:30 +0000893 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000894 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000895 SDValue IntCR;
Andrew Trickc416ba62010-12-24 04:28:06 +0000896
Chris Lattner491b8292005-10-06 19:03:35 +0000897 // Force the ccreg into CR7.
Owen Anderson9f944592009-08-11 20:47:22 +0000898 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Andrew Trickc416ba62010-12-24 04:28:06 +0000899
Craig Topper062a2ba2014-04-25 05:30:21 +0000900 SDValue InFlag(nullptr, 0); // Null incoming flag value.
Andrew Trickc416ba62010-12-24 04:28:06 +0000901 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Chris Lattnerbd099102005-12-01 03:50:19 +0000902 InFlag).getValue(1);
Andrew Trickc416ba62010-12-24 04:28:06 +0000903
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000904 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
905 CCReg), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000906
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000907 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
Evan Chengc3acfc02006-08-27 08:14:06 +0000908 getI32Imm(31), getI32Imm(31) };
Ulrich Weigand47e93282013-07-03 15:13:30 +0000909 if (!Inv)
Craig Topper481fb282014-04-27 19:21:11 +0000910 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattner89f36e62008-01-08 06:46:30 +0000911
912 // Get the specified bit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000913 SDValue Tmp =
Michael Liaob53d8962013-04-19 22:22:57 +0000914 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Ulrich Weigand47e93282013-07-03 15:13:30 +0000915 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner491b8292005-10-06 19:03:35 +0000916}
Chris Lattner502a3692005-10-06 18:56:10 +0000917
Chris Lattner318622f2005-10-06 19:07:45 +0000918
Chris Lattner43ff01e2005-08-17 19:33:03 +0000919// Select - Convert the specified operand from a target-independent to a
920// target-specific node if it hasn't already been changed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000921SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000922 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +0000923 if (N->isMachineOpcode()) {
924 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +0000925 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +0000926 }
Chris Lattner08c319f2005-09-29 00:59:32 +0000927
Hal Finkel51b3fd12014-09-02 06:23:54 +0000928 // In case any misguided DAG-level optimizations form an ADD with a
929 // TargetConstant operand, crash here instead of miscompiling (by selecting
930 // an r+r add instead of some kind of r+i add).
931 if (N->getOpcode() == ISD::ADD &&
932 N->getOperand(1).getOpcode() == ISD::TargetConstant)
933 llvm_unreachable("Invalid ADD with TargetConstant operand");
934
Chris Lattner43ff01e2005-08-17 19:33:03 +0000935 switch (N->getOpcode()) {
Chris Lattner498915d2005-09-07 23:45:15 +0000936 default: break;
Andrew Trickc416ba62010-12-24 04:28:06 +0000937
Jim Laskey095e6f32006-12-12 13:23:43 +0000938 case ISD::Constant: {
Owen Anderson9f944592009-08-11 20:47:22 +0000939 if (N->getValueType(0) == MVT::i64) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000940 // Get 64 bit value.
Dan Gohmaneffb8942008-09-12 16:56:44 +0000941 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
Jim Laskey095e6f32006-12-12 13:23:43 +0000942 // Assume no remaining bits.
943 unsigned Remainder = 0;
944 // Assume no shift required.
945 unsigned Shift = 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000946
Jim Laskey095e6f32006-12-12 13:23:43 +0000947 // If it can't be represented as a 32 bit value.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000948 if (!isInt<32>(Imm)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000949 Shift = countTrailingZeros<uint64_t>(Imm);
Jim Laskey095e6f32006-12-12 13:23:43 +0000950 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
Andrew Trickc416ba62010-12-24 04:28:06 +0000951
Jim Laskey095e6f32006-12-12 13:23:43 +0000952 // If the shifted value fits 32 bits.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000953 if (isInt<32>(ImmSh)) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000954 // Go with the shifted value.
955 Imm = ImmSh;
956 } else {
957 // Still stuck with a 64 bit value.
958 Remainder = Imm;
959 Shift = 32;
960 Imm >>= 32;
961 }
962 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000963
Jim Laskey095e6f32006-12-12 13:23:43 +0000964 // Intermediate operand.
965 SDNode *Result;
966
967 // Handle first 32 bits.
968 unsigned Lo = Imm & 0xFFFF;
969 unsigned Hi = (Imm >> 16) & 0xFFFF;
Andrew Trickc416ba62010-12-24 04:28:06 +0000970
Jim Laskey095e6f32006-12-12 13:23:43 +0000971 // Simple value.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000972 if (isInt<16>(Imm)) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000973 // Just the Lo bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000974 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000975 } else if (Lo) {
976 // Handle the Hi bits.
977 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
Dan Gohman32f71d72009-09-25 18:54:59 +0000978 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
Jim Laskey095e6f32006-12-12 13:23:43 +0000979 // And Lo bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000980 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
981 SDValue(Result, 0), getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000982 } else {
983 // Just the Hi bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000984 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
Jim Laskey095e6f32006-12-12 13:23:43 +0000985 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000986
Jim Laskey095e6f32006-12-12 13:23:43 +0000987 // If no shift, we're done.
988 if (!Shift) return Result;
989
990 // Shift for next step if the upper 32-bits were not zero.
991 if (Imm) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000992 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
993 SDValue(Result, 0),
994 getI32Imm(Shift),
995 getI32Imm(63 - Shift));
Jim Laskey095e6f32006-12-12 13:23:43 +0000996 }
997
998 // Add in the last bits as required.
999 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
Dan Gohman32f71d72009-09-25 18:54:59 +00001000 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
1001 SDValue(Result, 0), getI32Imm(Hi));
Andrew Trickc416ba62010-12-24 04:28:06 +00001002 }
Jim Laskey095e6f32006-12-12 13:23:43 +00001003 if ((Lo = Remainder & 0xFFFF)) {
Dan Gohman32f71d72009-09-25 18:54:59 +00001004 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
1005 SDValue(Result, 0), getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +00001006 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001007
Jim Laskey095e6f32006-12-12 13:23:43 +00001008 return Result;
1009 }
1010 break;
1011 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001012
Hal Finkel940ab932014-02-28 00:27:01 +00001013 case ISD::SETCC: {
1014 SDNode *SN = SelectSETCC(N);
1015 if (SN)
1016 return SN;
1017 break;
1018 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001019 case PPCISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +00001020 return getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +00001021
Chris Lattnere4c338d2005-08-25 00:45:43 +00001022 case ISD::FrameIndex: {
1023 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001024 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
1025 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001026 if (N->hasOneUse())
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001027 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), TFI,
Evan Cheng34b70ee2006-08-26 08:00:10 +00001028 getSmallIPtrImm(0));
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001029 return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
Dan Gohman32f71d72009-09-25 18:54:59 +00001030 getSmallIPtrImm(0));
Chris Lattnere4c338d2005-08-25 00:45:43 +00001031 }
Chris Lattner6961fc72006-03-26 10:06:40 +00001032
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00001033 case PPCISD::MFOCRF: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001034 SDValue InFlag = N->getOperand(1);
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00001035 return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
1036 N->getOperand(0), InFlag);
Chris Lattner6961fc72006-03-26 10:06:40 +00001037 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001038
Hal Finkelbbdee932014-12-02 22:01:00 +00001039 case PPCISD::READ_TIME_BASE: {
1040 return CurDAG->getMachineNode(PPC::ReadTB, dl, MVT::i32, MVT::i32,
1041 MVT::Other, N->getOperand(0));
1042 }
1043
Chris Lattner57693112005-09-28 22:50:24 +00001044 case ISD::SDIV: {
Nate Begeman4dd38312005-10-21 00:02:42 +00001045 // FIXME: since this depends on the setting of the carry flag from the srawi
1046 // we should really be making notes about that for the scheduler.
Andrew Trickc416ba62010-12-24 04:28:06 +00001047 // FIXME: It sure would be nice if we could cheaply recognize the
Nate Begeman4dd38312005-10-21 00:02:42 +00001048 // srl/add/sra pattern the dag combiner will generate for this as
1049 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattnerdc664572005-08-25 17:50:06 +00001050 unsigned Imm;
Chris Lattner97b3da12006-06-27 00:04:13 +00001051 if (isInt32Immediate(N->getOperand(1), Imm)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001052 SDValue N0 = N->getOperand(0);
Chris Lattnerdc664572005-08-25 17:50:06 +00001053 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Chengd1b82d82006-02-09 07:17:49 +00001054 SDNode *Op =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001055 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00001056 N0, getI32Imm(Log2_32(Imm)));
Andrew Trickc416ba62010-12-24 04:28:06 +00001057 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001058 SDValue(Op, 0), SDValue(Op, 1));
Chris Lattnerdc664572005-08-25 17:50:06 +00001059 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Chengd1b82d82006-02-09 07:17:49 +00001060 SDNode *Op =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001061 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00001062 N0, getI32Imm(Log2_32(-Imm)));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001063 SDValue PT =
Dan Gohman32f71d72009-09-25 18:54:59 +00001064 SDValue(CurDAG->getMachineNode(PPC::ADDZE, dl, MVT::i32,
1065 SDValue(Op, 0), SDValue(Op, 1)),
Evan Chengd1b82d82006-02-09 07:17:49 +00001066 0);
Owen Anderson9f944592009-08-11 20:47:22 +00001067 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattnerdc664572005-08-25 17:50:06 +00001068 }
1069 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001070
Chris Lattner1de57062005-09-29 23:33:31 +00001071 // Other cases are autogenerated.
1072 break;
Chris Lattner6e184f22005-08-25 22:04:30 +00001073 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001074
Chris Lattnerce645542006-11-10 02:08:47 +00001075 case ISD::LOAD: {
1076 // Handle preincrement loads.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001077 LoadSDNode *LD = cast<LoadSDNode>(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001078 EVT LoadedVT = LD->getMemoryVT();
Andrew Trickc416ba62010-12-24 04:28:06 +00001079
Chris Lattnerce645542006-11-10 02:08:47 +00001080 // Normal loads are handled by code generated from the .td file.
1081 if (LD->getAddressingMode() != ISD::PRE_INC)
1082 break;
Andrew Trickc416ba62010-12-24 04:28:06 +00001083
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001084 SDValue Offset = LD->getOffset();
Ulrich Weigandd1b99d32013-03-22 14:58:17 +00001085 if (Offset.getOpcode() == ISD::TargetConstant ||
Chris Lattnerc5102bf2006-11-11 04:53:30 +00001086 Offset.getOpcode() == ISD::TargetGlobalAddress) {
Andrew Trickc416ba62010-12-24 04:28:06 +00001087
Chris Lattner474b5b72006-11-15 19:55:13 +00001088 unsigned Opcode;
1089 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
Owen Anderson9f944592009-08-11 20:47:22 +00001090 if (LD->getValueType(0) != MVT::i64) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001091 // Handle PPC32 integer and normal FP loads.
Owen Anderson9f944592009-08-11 20:47:22 +00001092 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1093 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001094 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00001095 case MVT::f64: Opcode = PPC::LFDU; break;
1096 case MVT::f32: Opcode = PPC::LFSU; break;
1097 case MVT::i32: Opcode = PPC::LWZU; break;
1098 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
1099 case MVT::i1:
1100 case MVT::i8: Opcode = PPC::LBZU; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00001101 }
1102 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00001103 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1104 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1105 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001106 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00001107 case MVT::i64: Opcode = PPC::LDU; break;
1108 case MVT::i32: Opcode = PPC::LWZU8; break;
1109 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
1110 case MVT::i1:
1111 case MVT::i8: Opcode = PPC::LBZU8; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00001112 }
1113 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001114
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001115 SDValue Chain = LD->getChain();
1116 SDValue Base = LD->getBasePtr();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001117 SDValue Ops[] = { Offset, Base, Chain };
Dan Gohman32f71d72009-09-25 18:54:59 +00001118 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
Eric Christopher1b8e7632014-05-22 01:07:24 +00001119 PPCLowering->getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00001120 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00001121 } else {
Hal Finkelca542be2012-06-20 15:43:03 +00001122 unsigned Opcode;
1123 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
1124 if (LD->getValueType(0) != MVT::i64) {
1125 // Handle PPC32 integer and normal FP loads.
1126 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1127 switch (LoadedVT.getSimpleVT().SimpleTy) {
1128 default: llvm_unreachable("Invalid PPC load type!");
1129 case MVT::f64: Opcode = PPC::LFDUX; break;
1130 case MVT::f32: Opcode = PPC::LFSUX; break;
1131 case MVT::i32: Opcode = PPC::LWZUX; break;
1132 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
1133 case MVT::i1:
1134 case MVT::i8: Opcode = PPC::LBZUX; break;
1135 }
1136 } else {
1137 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1138 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
1139 "Invalid sext update load");
1140 switch (LoadedVT.getSimpleVT().SimpleTy) {
1141 default: llvm_unreachable("Invalid PPC load type!");
1142 case MVT::i64: Opcode = PPC::LDUX; break;
1143 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
1144 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
1145 case MVT::i1:
1146 case MVT::i8: Opcode = PPC::LBZUX8; break;
1147 }
1148 }
1149
1150 SDValue Chain = LD->getChain();
1151 SDValue Base = LD->getBasePtr();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001152 SDValue Ops[] = { Base, Offset, Chain };
Hal Finkelca542be2012-06-20 15:43:03 +00001153 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
Eric Christopher1b8e7632014-05-22 01:07:24 +00001154 PPCLowering->getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00001155 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00001156 }
1157 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001158
Nate Begemanb3821a32005-08-18 07:30:46 +00001159 case ISD::AND: {
Nate Begemand31efd12006-09-22 05:01:56 +00001160 unsigned Imm, Imm2, SH, MB, ME;
Hal Finkele39526a2012-08-28 02:10:15 +00001161 uint64_t Imm64;
Nate Begemand31efd12006-09-22 05:01:56 +00001162
Nate Begemanb3821a32005-08-18 07:30:46 +00001163 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1164 // with a mask, emit rlwinm
Chris Lattner97b3da12006-06-27 00:04:13 +00001165 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00001166 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001167 SDValue Val = N->getOperand(0).getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001168 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00001169 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemanb3821a32005-08-18 07:30:46 +00001170 }
Nate Begemand31efd12006-09-22 05:01:56 +00001171 // If this is just a masked value where the input is not handled above, and
1172 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
1173 if (isInt32Immediate(N->getOperand(1), Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00001174 isRunOfOnes(Imm, MB, ME) &&
Nate Begemand31efd12006-09-22 05:01:56 +00001175 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001176 SDValue Val = N->getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001177 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00001178 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemand31efd12006-09-22 05:01:56 +00001179 }
Hal Finkele39526a2012-08-28 02:10:15 +00001180 // If this is a 64-bit zero-extension mask, emit rldicl.
1181 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
1182 isMask_64(Imm64)) {
1183 SDValue Val = N->getOperand(0);
1184 MB = 64 - CountTrailingOnes_64(Imm64);
Hal Finkel22498fa2013-11-20 01:10:15 +00001185 SH = 0;
1186
1187 // If the operand is a logical right shift, we can fold it into this
1188 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb)
1189 // for n <= mb. The right shift is really a left rotate followed by a
1190 // mask, and this mask is a more-restrictive sub-mask of the mask implied
1191 // by the shift.
1192 if (Val.getOpcode() == ISD::SRL &&
1193 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) {
1194 assert(Imm < 64 && "Illegal shift amount");
1195 Val = Val.getOperand(0);
1196 SH = 64 - Imm;
1197 }
1198
1199 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB) };
Craig Topper481fb282014-04-27 19:21:11 +00001200 return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops);
Hal Finkele39526a2012-08-28 02:10:15 +00001201 }
Nate Begemand31efd12006-09-22 05:01:56 +00001202 // AND X, 0 -> 0, not "rlwinm 32".
1203 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001204 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Craig Topper062a2ba2014-04-25 05:30:21 +00001205 return nullptr;
Nate Begemand31efd12006-09-22 05:01:56 +00001206 }
Nate Begeman9aea6e42005-12-24 01:00:15 +00001207 // ISD::OR doesn't get all the bitfield insertion fun.
1208 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
Andrew Trickc416ba62010-12-24 04:28:06 +00001209 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman9aea6e42005-12-24 01:00:15 +00001210 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattner97b3da12006-06-27 00:04:13 +00001211 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattner20c88df2006-01-05 18:32:49 +00001212 unsigned MB, ME;
Nate Begeman9aea6e42005-12-24 01:00:15 +00001213 Imm = ~(Imm^Imm2);
1214 if (isRunOfOnes(Imm, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001215 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001216 N->getOperand(0).getOperand(1),
1217 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +00001218 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman9aea6e42005-12-24 01:00:15 +00001219 }
1220 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001221
Chris Lattner1de57062005-09-29 23:33:31 +00001222 // Other cases are autogenerated.
1223 break;
Nate Begemanb3821a32005-08-18 07:30:46 +00001224 }
Nate Begeman93c4bc62005-08-19 00:38:14 +00001225 case ISD::OR:
Owen Anderson9f944592009-08-11 20:47:22 +00001226 if (N->getValueType(0) == MVT::i32)
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001227 if (SDNode *I = SelectBitfieldInsert(N))
1228 return I;
Andrew Trickc416ba62010-12-24 04:28:06 +00001229
Chris Lattner1de57062005-09-29 23:33:31 +00001230 // Other cases are autogenerated.
1231 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001232 case ISD::SHL: {
1233 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001234 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001235 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001236 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001237 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00001238 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00001239 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001240
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001241 // Other cases are autogenerated.
1242 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001243 }
1244 case ISD::SRL: {
1245 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001246 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00001247 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001248 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001249 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00001250 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00001251 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001252
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001253 // Other cases are autogenerated.
1254 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001255 }
Hal Finkel940ab932014-02-28 00:27:01 +00001256 // FIXME: Remove this once the ANDI glue bug is fixed:
1257 case PPCISD::ANDIo_1_EQ_BIT:
1258 case PPCISD::ANDIo_1_GT_BIT: {
1259 if (!ANDIGlueBug)
1260 break;
1261
1262 EVT InVT = N->getOperand(0).getValueType();
1263 assert((InVT == MVT::i64 || InVT == MVT::i32) &&
1264 "Invalid input type for ANDIo_1_EQ_BIT");
1265
1266 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo;
1267 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue,
1268 N->getOperand(0),
1269 CurDAG->getTargetConstant(1, InVT)), 0);
1270 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
1271 SDValue SRIdxVal =
1272 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ?
1273 PPC::sub_eq : PPC::sub_gt, MVT::i32);
1274
1275 return CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1,
1276 CR0Reg, SRIdxVal,
1277 SDValue(AndI.getNode(), 1) /* glue */);
1278 }
Chris Lattnerbec817c2005-08-26 18:46:49 +00001279 case ISD::SELECT_CC: {
1280 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Roman Divacky254f8212011-06-20 15:28:39 +00001281 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
1282 bool isPPC64 = (PtrVT == MVT::i64);
Andrew Trickc416ba62010-12-24 04:28:06 +00001283
Hal Finkel940ab932014-02-28 00:27:01 +00001284 // If this is a select of i1 operands, we'll pattern match it.
Eric Christopher1b8e7632014-05-22 01:07:24 +00001285 if (PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00001286 N->getOperand(0).getValueType() == MVT::i1)
1287 break;
1288
Chris Lattner97b3da12006-06-27 00:04:13 +00001289 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Roman Divacky254f8212011-06-20 15:28:39 +00001290 if (!isPPC64)
1291 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1292 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1293 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1294 if (N1C->isNullValue() && N3C->isNullValue() &&
1295 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
1296 // FIXME: Implement this optzn for PPC64.
1297 N->getValueType(0) == MVT::i32) {
1298 SDNode *Tmp =
1299 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
1300 N->getOperand(0), getI32Imm(~0U));
1301 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1302 SDValue(Tmp, 0), N->getOperand(0),
1303 SDValue(Tmp, 1));
1304 }
Chris Lattner9b577f12005-08-26 21:23:58 +00001305
Dale Johannesenab8e4422009-02-06 19:16:40 +00001306 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00001307
1308 if (N->getValueType(0) == MVT::i1) {
1309 // An i1 select is: (c & t) | (!c & f).
1310 bool Inv;
1311 unsigned Idx = getCRIdxForSetCC(CC, Inv);
1312
1313 unsigned SRI;
1314 switch (Idx) {
1315 default: llvm_unreachable("Invalid CC index");
1316 case 0: SRI = PPC::sub_lt; break;
1317 case 1: SRI = PPC::sub_gt; break;
1318 case 2: SRI = PPC::sub_eq; break;
1319 case 3: SRI = PPC::sub_un; break;
1320 }
1321
1322 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg);
1323
1324 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1,
1325 CCBit, CCBit), 0);
1326 SDValue C = Inv ? NotCCBit : CCBit,
1327 NotC = Inv ? CCBit : NotCCBit;
1328
1329 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1330 C, N->getOperand(2)), 0);
1331 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1332 NotC, N->getOperand(3)), 0);
1333
1334 return CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF);
1335 }
1336
Chris Lattner8c6a41e2006-11-17 22:10:59 +00001337 unsigned BROpc = getPredicateForSetCC(CC);
Chris Lattner9b577f12005-08-26 21:23:58 +00001338
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001339 unsigned SelectCCOp;
Owen Anderson9f944592009-08-11 20:47:22 +00001340 if (N->getValueType(0) == MVT::i32)
Chris Lattner97b3da12006-06-27 00:04:13 +00001341 SelectCCOp = PPC::SELECT_CC_I4;
Owen Anderson9f944592009-08-11 20:47:22 +00001342 else if (N->getValueType(0) == MVT::i64)
Chris Lattner97b3da12006-06-27 00:04:13 +00001343 SelectCCOp = PPC::SELECT_CC_I8;
Owen Anderson9f944592009-08-11 20:47:22 +00001344 else if (N->getValueType(0) == MVT::f32)
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001345 SelectCCOp = PPC::SELECT_CC_F4;
Owen Anderson9f944592009-08-11 20:47:22 +00001346 else if (N->getValueType(0) == MVT::f64)
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00001347 if (PPCSubTarget->hasVSX())
1348 SelectCCOp = PPC::SELECT_CC_VSFRC;
1349 else
1350 SelectCCOp = PPC::SELECT_CC_F8;
Bill Schmidt61e65232014-10-22 13:13:40 +00001351 else if (N->getValueType(0) == MVT::v2f64 ||
1352 N->getValueType(0) == MVT::v2i64)
1353 SelectCCOp = PPC::SELECT_CC_VSRC;
Chris Lattner0a3d1bb2006-04-08 22:45:08 +00001354 else
1355 SelectCCOp = PPC::SELECT_CC_VRRC;
1356
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001357 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Evan Chengc3acfc02006-08-27 08:14:06 +00001358 getI32Imm(BROpc) };
Craig Topper481fb282014-04-27 19:21:11 +00001359 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops);
Chris Lattnerbec817c2005-08-26 18:46:49 +00001360 }
Hal Finkel732f0f72014-03-26 12:49:28 +00001361 case ISD::VSELECT:
Eric Christopher1b8e7632014-05-22 01:07:24 +00001362 if (PPCSubTarget->hasVSX()) {
Hal Finkel732f0f72014-03-26 12:49:28 +00001363 SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00001364 return CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops);
Hal Finkel732f0f72014-03-26 12:49:28 +00001365 }
1366
1367 break;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001368 case ISD::VECTOR_SHUFFLE:
Eric Christopher1b8e7632014-05-22 01:07:24 +00001369 if (PPCSubTarget->hasVSX() && (N->getValueType(0) == MVT::v2f64 ||
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001370 N->getValueType(0) == MVT::v2i64)) {
1371 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
1372
1373 SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1),
1374 Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1);
1375 unsigned DM[2];
1376
1377 for (int i = 0; i < 2; ++i)
1378 if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2)
1379 DM[i] = 0;
1380 else
1381 DM[i] = 1;
1382
Hal Finkel2583b062014-03-28 20:24:55 +00001383 SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), MVT::i32);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001384
1385 if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 &&
1386 Op1.getOpcode() == ISD::SCALAR_TO_VECTOR &&
1387 isa<LoadSDNode>(Op1.getOperand(0))) {
1388 LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0));
1389 SDValue Base, Offset;
1390
1391 if (LD->isUnindexed() &&
1392 SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) {
1393 SDValue Chain = LD->getChain();
1394 SDValue Ops[] = { Base, Offset, Chain };
1395 return CurDAG->SelectNodeTo(N, PPC::LXVDSX,
Craig Topper481fb282014-04-27 19:21:11 +00001396 N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001397 }
1398 }
1399
1400 SDValue Ops[] = { Op1, Op2, DMV };
Craig Topper481fb282014-04-27 19:21:11 +00001401 return CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001402 }
1403
1404 break;
Hal Finkel25c19922013-05-15 21:37:41 +00001405 case PPCISD::BDNZ:
1406 case PPCISD::BDZ: {
Eric Christopher1b8e7632014-05-22 01:07:24 +00001407 bool IsPPC64 = PPCSubTarget->isPPC64();
Hal Finkel25c19922013-05-15 21:37:41 +00001408 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
1409 return CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ ?
1410 (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1411 (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
Craig Topper481fb282014-04-27 19:21:11 +00001412 MVT::Other, Ops);
Hal Finkel25c19922013-05-15 21:37:41 +00001413 }
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001414 case PPCISD::COND_BRANCH: {
Dan Gohman7a638a82008-11-05 17:16:24 +00001415 // Op #0 is the Chain.
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001416 // Op #1 is the PPC::PRED_* number.
1417 // Op #2 is the CR#
1418 // Op #3 is the Dest MBB
Dan Gohmanf14b77e2008-11-05 04:14:16 +00001419 // Op #4 is the Flag.
Evan Cheng58d1eac2007-06-29 01:25:06 +00001420 // Prevent PPC::PRED_* from being selected into LI.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001421 SDValue Pred =
Dan Gohmaneffb8942008-09-12 16:56:44 +00001422 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001423 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001424 N->getOperand(0), N->getOperand(4) };
Craig Topper481fb282014-04-27 19:21:11 +00001425 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001426 }
Nate Begemanbb01d4f2006-03-17 01:40:33 +00001427 case ISD::BR_CC: {
Chris Lattner2a1823d2005-08-21 18:50:37 +00001428 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Hal Finkel940ab932014-02-28 00:27:01 +00001429 unsigned PCC = getPredicateForSetCC(CC);
1430
1431 if (N->getOperand(2).getValueType() == MVT::i1) {
1432 unsigned Opc;
1433 bool Swap;
1434 switch (PCC) {
1435 default: llvm_unreachable("Unexpected Boolean-operand predicate");
1436 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break;
1437 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break;
1438 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break;
1439 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break;
1440 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break;
1441 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break;
1442 }
1443
1444 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1,
1445 N->getOperand(Swap ? 3 : 2),
1446 N->getOperand(Swap ? 2 : 3)), 0);
1447 return CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other,
1448 BitComp, N->getOperand(4), N->getOperand(0));
1449 }
1450
Dale Johannesenab8e4422009-02-06 19:16:40 +00001451 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00001452 SDValue Ops[] = { getI32Imm(PCC), CondCode,
Evan Chengc3acfc02006-08-27 08:14:06 +00001453 N->getOperand(4), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00001454 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattner2a1823d2005-08-21 18:50:37 +00001455 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001456 case ISD::BRIND: {
Chris Lattnerb055c872006-06-10 01:15:02 +00001457 // FIXME: Should custom lower this.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001458 SDValue Chain = N->getOperand(0);
1459 SDValue Target = N->getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +00001460 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Roman Divackya4a59ae2011-06-03 15:47:49 +00001461 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
Hal Finkel528ff4b2011-12-08 04:36:44 +00001462 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
Dan Gohman32f71d72009-09-25 18:54:59 +00001463 Chain), 0);
Roman Divackya4a59ae2011-06-03 15:47:49 +00001464 return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001465 }
Bill Schmidt34627e32012-11-27 17:35:46 +00001466 case PPCISD::TOC_ENTRY: {
Justin Hibbitsa88b6052014-11-12 15:16:30 +00001467 assert ((PPCSubTarget->isPPC64() || PPCSubTarget->isSVR4ABI()) &&
1468 "Only supported for 64-bit ABI and 32-bit SVR4");
Hal Finkel3ee2af72014-07-18 23:29:49 +00001469 if (PPCSubTarget->isSVR4ABI() && !PPCSubTarget->isPPC64()) {
1470 SDValue GA = N->getOperand(0);
1471 return CurDAG->getMachineNode(PPC::LWZtoc, dl, MVT::i32, GA,
1472 N->getOperand(1));
Justin Hibbits3476db42014-08-28 04:40:55 +00001473 }
Bill Schmidt34627e32012-11-27 17:35:46 +00001474
Bill Schmidt27917782013-02-21 17:12:27 +00001475 // For medium and large code model, we generate two instructions as
1476 // described below. Otherwise we allow SelectCodeCommon to handle this,
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00001477 // selecting one of LDtoc, LDtocJTI, LDtocCPT, and LDtocBA.
Bill Schmidt27917782013-02-21 17:12:27 +00001478 CodeModel::Model CModel = TM.getCodeModel();
1479 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00001480 break;
1481
Bill Schmidt5d82f092014-06-16 21:36:02 +00001482 // The first source operand is a TargetGlobalAddress or a TargetJumpTable.
1483 // If it is an externally defined symbol, a symbol with common linkage,
1484 // a non-local function address, or a jump table address, or if we are
1485 // generating code for large code model, we generate:
Bill Schmidt34627e32012-11-27 17:35:46 +00001486 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
1487 // Otherwise we generate:
1488 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
1489 SDValue GA = N->getOperand(0);
1490 SDValue TOCbase = N->getOperand(1);
1491 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
1492 TOCbase, GA);
1493
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00001494 if (isa<JumpTableSDNode>(GA) || isa<BlockAddressSDNode>(GA) ||
1495 CModel == CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00001496 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1497 SDValue(Tmp, 0));
1498
1499 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
1500 const GlobalValue *GValue = G->getGlobal();
Bill Schmidt5d82f092014-06-16 21:36:02 +00001501 if ((GValue->getType()->getElementType()->isFunctionTy() &&
1502 (GValue->isDeclaration() || GValue->isWeakForLinker())) ||
Rafael Espindola04902862014-05-29 15:41:38 +00001503 GValue->isDeclaration() || GValue->hasCommonLinkage() ||
1504 GValue->hasAvailableExternallyLinkage())
Bill Schmidt34627e32012-11-27 17:35:46 +00001505 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1506 SDValue(Tmp, 0));
1507 }
1508
1509 return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
1510 SDValue(Tmp, 0), GA);
1511 }
Hal Finkel7c8ae532014-07-25 17:47:22 +00001512 case PPCISD::PPC32_PICGOT: {
1513 // Generate a PIC-safe GOT reference.
1514 assert(!PPCSubTarget->isPPC64() && PPCSubTarget->isSVR4ABI() &&
1515 "PPCISD::PPC32_PICGOT is only supported for 32-bit SVR4");
1516 return CurDAG->SelectNodeTo(N, PPC::PPC32PICGOT, PPCLowering->getPointerTy(), MVT::i32);
1517 }
Bill Schmidt51e79512013-02-20 15:50:31 +00001518 case PPCISD::VADD_SPLAT: {
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001519 // This expands into one of three sequences, depending on whether
1520 // the first operand is odd or even, positive or negative.
Bill Schmidt51e79512013-02-20 15:50:31 +00001521 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
1522 isa<ConstantSDNode>(N->getOperand(1)) &&
1523 "Invalid operand on VADD_SPLAT!");
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001524
1525 int Elt = N->getConstantOperandVal(0);
Bill Schmidt51e79512013-02-20 15:50:31 +00001526 int EltSize = N->getConstantOperandVal(1);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001527 unsigned Opc1, Opc2, Opc3;
Bill Schmidt51e79512013-02-20 15:50:31 +00001528 EVT VT;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001529
Bill Schmidt51e79512013-02-20 15:50:31 +00001530 if (EltSize == 1) {
1531 Opc1 = PPC::VSPLTISB;
1532 Opc2 = PPC::VADDUBM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001533 Opc3 = PPC::VSUBUBM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001534 VT = MVT::v16i8;
1535 } else if (EltSize == 2) {
1536 Opc1 = PPC::VSPLTISH;
1537 Opc2 = PPC::VADDUHM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001538 Opc3 = PPC::VSUBUHM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001539 VT = MVT::v8i16;
1540 } else {
1541 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
1542 Opc1 = PPC::VSPLTISW;
1543 Opc2 = PPC::VADDUWM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001544 Opc3 = PPC::VSUBUWM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001545 VT = MVT::v4i32;
1546 }
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001547
1548 if ((Elt & 1) == 0) {
1549 // Elt is even, in the range [-32,-18] + [16,30].
1550 //
1551 // Convert: VADD_SPLAT elt, size
1552 // Into: tmp = VSPLTIS[BHW] elt
1553 // VADDU[BHW]M tmp, tmp
1554 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
1555 SDValue EltVal = getI32Imm(Elt >> 1);
1556 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1557 SDValue TmpVal = SDValue(Tmp, 0);
1558 return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal);
1559
1560 } else if (Elt > 0) {
1561 // Elt is odd and positive, in the range [17,31].
1562 //
1563 // Convert: VADD_SPLAT elt, size
1564 // Into: tmp1 = VSPLTIS[BHW] elt-16
1565 // tmp2 = VSPLTIS[BHW] -16
1566 // VSUBU[BHW]M tmp1, tmp2
1567 SDValue EltVal = getI32Imm(Elt - 16);
1568 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1569 EltVal = getI32Imm(-16);
1570 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1571 return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
1572 SDValue(Tmp2, 0));
1573
1574 } else {
1575 // Elt is odd and negative, in the range [-31,-17].
1576 //
1577 // Convert: VADD_SPLAT elt, size
1578 // Into: tmp1 = VSPLTIS[BHW] elt+16
1579 // tmp2 = VSPLTIS[BHW] -16
1580 // VADDU[BHW]M tmp1, tmp2
1581 SDValue EltVal = getI32Imm(Elt + 16);
1582 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1583 EltVal = getI32Imm(-16);
1584 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1585 return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
1586 SDValue(Tmp2, 0));
1587 }
Bill Schmidt51e79512013-02-20 15:50:31 +00001588 }
Chris Lattner43ff01e2005-08-17 19:33:03 +00001589 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001590
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001591 return SelectCode(N);
Chris Lattner43ff01e2005-08-17 19:33:03 +00001592}
1593
Hal Finkel860fa902014-01-02 22:09:39 +00001594/// PostprocessISelDAG - Perform some late peephole optimizations
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001595/// on the DAG representation.
1596void PPCDAGToDAGISel::PostprocessISelDAG() {
1597
1598 // Skip peepholes at -O0.
1599 if (TM.getOptLevel() == CodeGenOpt::None)
1600 return;
1601
Hal Finkel940ab932014-02-28 00:27:01 +00001602 PeepholePPC64();
Eric Christopher02e18042014-05-14 00:31:15 +00001603 PeepholeCROps();
Hal Finkel940ab932014-02-28 00:27:01 +00001604}
1605
Hal Finkelb9989152014-02-28 06:11:16 +00001606// Check if all users of this node will become isel where the second operand
1607// is the constant zero. If this is so, and if we can negate the condition,
1608// then we can flip the true and false operands. This will allow the zero to
1609// be folded with the isel so that we don't need to materialize a register
1610// containing zero.
1611bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) {
1612 // If we're not using isel, then this does not matter.
Eric Christopher1b8e7632014-05-22 01:07:24 +00001613 if (!PPCSubTarget->hasISEL())
Hal Finkelb9989152014-02-28 06:11:16 +00001614 return false;
1615
1616 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
1617 UI != UE; ++UI) {
1618 SDNode *User = *UI;
1619 if (!User->isMachineOpcode())
1620 return false;
1621 if (User->getMachineOpcode() != PPC::SELECT_I4 &&
1622 User->getMachineOpcode() != PPC::SELECT_I8)
1623 return false;
1624
1625 SDNode *Op2 = User->getOperand(2).getNode();
1626 if (!Op2->isMachineOpcode())
1627 return false;
1628
1629 if (Op2->getMachineOpcode() != PPC::LI &&
1630 Op2->getMachineOpcode() != PPC::LI8)
1631 return false;
1632
1633 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0));
1634 if (!C)
1635 return false;
1636
1637 if (!C->isNullValue())
1638 return false;
1639 }
1640
1641 return true;
1642}
1643
1644void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) {
1645 SmallVector<SDNode *, 4> ToReplace;
1646 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
1647 UI != UE; ++UI) {
1648 SDNode *User = *UI;
1649 assert((User->getMachineOpcode() == PPC::SELECT_I4 ||
1650 User->getMachineOpcode() == PPC::SELECT_I8) &&
1651 "Must have all select users");
1652 ToReplace.push_back(User);
1653 }
1654
1655 for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(),
1656 UE = ToReplace.end(); UI != UE; ++UI) {
1657 SDNode *User = *UI;
1658 SDNode *ResNode =
1659 CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User),
1660 User->getValueType(0), User->getOperand(0),
1661 User->getOperand(2),
1662 User->getOperand(1));
1663
1664 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
1665 DEBUG(User->dump(CurDAG));
1666 DEBUG(dbgs() << "\nNew: ");
1667 DEBUG(ResNode->dump(CurDAG));
1668 DEBUG(dbgs() << "\n");
1669
1670 ReplaceUses(User, ResNode);
1671 }
1672}
1673
Eric Christopher02e18042014-05-14 00:31:15 +00001674void PPCDAGToDAGISel::PeepholeCROps() {
Hal Finkel940ab932014-02-28 00:27:01 +00001675 bool IsModified;
1676 do {
1677 IsModified = false;
1678 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
1679 E = CurDAG->allnodes_end(); I != E; ++I) {
1680 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
1681 if (!MachineNode || MachineNode->use_empty())
1682 continue;
1683 SDNode *ResNode = MachineNode;
1684
1685 bool Op1Set = false, Op1Unset = false,
1686 Op1Not = false,
1687 Op2Set = false, Op2Unset = false,
1688 Op2Not = false;
1689
1690 unsigned Opcode = MachineNode->getMachineOpcode();
1691 switch (Opcode) {
1692 default: break;
1693 case PPC::CRAND:
1694 case PPC::CRNAND:
1695 case PPC::CROR:
1696 case PPC::CRXOR:
1697 case PPC::CRNOR:
1698 case PPC::CREQV:
1699 case PPC::CRANDC:
1700 case PPC::CRORC: {
1701 SDValue Op = MachineNode->getOperand(1);
1702 if (Op.isMachineOpcode()) {
1703 if (Op.getMachineOpcode() == PPC::CRSET)
1704 Op2Set = true;
1705 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1706 Op2Unset = true;
1707 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1708 Op.getOperand(0) == Op.getOperand(1))
1709 Op2Not = true;
1710 }
1711 } // fallthrough
1712 case PPC::BC:
1713 case PPC::BCn:
1714 case PPC::SELECT_I4:
1715 case PPC::SELECT_I8:
1716 case PPC::SELECT_F4:
1717 case PPC::SELECT_F8:
Bill Schmidt61e65232014-10-22 13:13:40 +00001718 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00001719 case PPC::SELECT_VSFRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00001720 case PPC::SELECT_VSRC: {
Hal Finkel940ab932014-02-28 00:27:01 +00001721 SDValue Op = MachineNode->getOperand(0);
1722 if (Op.isMachineOpcode()) {
1723 if (Op.getMachineOpcode() == PPC::CRSET)
1724 Op1Set = true;
1725 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1726 Op1Unset = true;
1727 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1728 Op.getOperand(0) == Op.getOperand(1))
1729 Op1Not = true;
1730 }
1731 }
1732 break;
1733 }
1734
Hal Finkelb9989152014-02-28 06:11:16 +00001735 bool SelectSwap = false;
Hal Finkel940ab932014-02-28 00:27:01 +00001736 switch (Opcode) {
1737 default: break;
1738 case PPC::CRAND:
1739 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1740 // x & x = x
1741 ResNode = MachineNode->getOperand(0).getNode();
1742 else if (Op1Set)
1743 // 1 & y = y
1744 ResNode = MachineNode->getOperand(1).getNode();
1745 else if (Op2Set)
1746 // x & 1 = x
1747 ResNode = MachineNode->getOperand(0).getNode();
1748 else if (Op1Unset || Op2Unset)
1749 // x & 0 = 0 & y = 0
1750 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1751 MVT::i1);
1752 else if (Op1Not)
1753 // ~x & y = andc(y, x)
1754 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1755 MVT::i1, MachineNode->getOperand(1),
1756 MachineNode->getOperand(0).
1757 getOperand(0));
1758 else if (Op2Not)
1759 // x & ~y = andc(x, y)
1760 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1761 MVT::i1, MachineNode->getOperand(0),
1762 MachineNode->getOperand(1).
1763 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001764 else if (AllUsersSelectZero(MachineNode))
1765 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
1766 MVT::i1, MachineNode->getOperand(0),
1767 MachineNode->getOperand(1)),
1768 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001769 break;
1770 case PPC::CRNAND:
1771 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1772 // nand(x, x) -> nor(x, x)
1773 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1774 MVT::i1, MachineNode->getOperand(0),
1775 MachineNode->getOperand(0));
1776 else if (Op1Set)
1777 // nand(1, y) -> nor(y, y)
1778 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1779 MVT::i1, MachineNode->getOperand(1),
1780 MachineNode->getOperand(1));
1781 else if (Op2Set)
1782 // nand(x, 1) -> nor(x, x)
1783 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1784 MVT::i1, MachineNode->getOperand(0),
1785 MachineNode->getOperand(0));
1786 else if (Op1Unset || Op2Unset)
1787 // nand(x, 0) = nand(0, y) = 1
1788 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1789 MVT::i1);
1790 else if (Op1Not)
1791 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y)
1792 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1793 MVT::i1, MachineNode->getOperand(0).
1794 getOperand(0),
1795 MachineNode->getOperand(1));
1796 else if (Op2Not)
1797 // nand(x, ~y) = ~x | y = orc(y, x)
1798 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1799 MVT::i1, MachineNode->getOperand(1).
1800 getOperand(0),
1801 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001802 else if (AllUsersSelectZero(MachineNode))
1803 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
1804 MVT::i1, MachineNode->getOperand(0),
1805 MachineNode->getOperand(1)),
1806 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001807 break;
1808 case PPC::CROR:
1809 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1810 // x | x = x
1811 ResNode = MachineNode->getOperand(0).getNode();
1812 else if (Op1Set || Op2Set)
1813 // x | 1 = 1 | y = 1
1814 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1815 MVT::i1);
1816 else if (Op1Unset)
1817 // 0 | y = y
1818 ResNode = MachineNode->getOperand(1).getNode();
1819 else if (Op2Unset)
1820 // x | 0 = x
1821 ResNode = MachineNode->getOperand(0).getNode();
1822 else if (Op1Not)
1823 // ~x | y = orc(y, x)
1824 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1825 MVT::i1, MachineNode->getOperand(1),
1826 MachineNode->getOperand(0).
1827 getOperand(0));
1828 else if (Op2Not)
1829 // x | ~y = orc(x, y)
1830 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1831 MVT::i1, MachineNode->getOperand(0),
1832 MachineNode->getOperand(1).
1833 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001834 else if (AllUsersSelectZero(MachineNode))
1835 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1836 MVT::i1, MachineNode->getOperand(0),
1837 MachineNode->getOperand(1)),
1838 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001839 break;
1840 case PPC::CRXOR:
1841 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1842 // xor(x, x) = 0
1843 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1844 MVT::i1);
1845 else if (Op1Set)
1846 // xor(1, y) -> nor(y, y)
1847 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1848 MVT::i1, MachineNode->getOperand(1),
1849 MachineNode->getOperand(1));
1850 else if (Op2Set)
1851 // xor(x, 1) -> nor(x, x)
1852 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1853 MVT::i1, MachineNode->getOperand(0),
1854 MachineNode->getOperand(0));
1855 else if (Op1Unset)
1856 // xor(0, y) = y
1857 ResNode = MachineNode->getOperand(1).getNode();
1858 else if (Op2Unset)
1859 // xor(x, 0) = x
1860 ResNode = MachineNode->getOperand(0).getNode();
1861 else if (Op1Not)
1862 // xor(~x, y) = eqv(x, y)
1863 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1864 MVT::i1, MachineNode->getOperand(0).
1865 getOperand(0),
1866 MachineNode->getOperand(1));
1867 else if (Op2Not)
1868 // xor(x, ~y) = eqv(x, y)
1869 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1870 MVT::i1, MachineNode->getOperand(0),
1871 MachineNode->getOperand(1).
1872 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001873 else if (AllUsersSelectZero(MachineNode))
1874 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1875 MVT::i1, MachineNode->getOperand(0),
1876 MachineNode->getOperand(1)),
1877 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001878 break;
1879 case PPC::CRNOR:
1880 if (Op1Set || Op2Set)
1881 // nor(1, y) -> 0
1882 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1883 MVT::i1);
1884 else if (Op1Unset)
1885 // nor(0, y) = ~y -> nor(y, y)
1886 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1887 MVT::i1, MachineNode->getOperand(1),
1888 MachineNode->getOperand(1));
1889 else if (Op2Unset)
1890 // nor(x, 0) = ~x
1891 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1892 MVT::i1, MachineNode->getOperand(0),
1893 MachineNode->getOperand(0));
1894 else if (Op1Not)
1895 // nor(~x, y) = andc(x, y)
1896 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1897 MVT::i1, MachineNode->getOperand(0).
1898 getOperand(0),
1899 MachineNode->getOperand(1));
1900 else if (Op2Not)
1901 // nor(x, ~y) = andc(y, x)
1902 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1903 MVT::i1, MachineNode->getOperand(1).
1904 getOperand(0),
1905 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001906 else if (AllUsersSelectZero(MachineNode))
1907 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
1908 MVT::i1, MachineNode->getOperand(0),
1909 MachineNode->getOperand(1)),
1910 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001911 break;
1912 case PPC::CREQV:
1913 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1914 // eqv(x, x) = 1
1915 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1916 MVT::i1);
1917 else if (Op1Set)
1918 // eqv(1, y) = y
1919 ResNode = MachineNode->getOperand(1).getNode();
1920 else if (Op2Set)
1921 // eqv(x, 1) = x
1922 ResNode = MachineNode->getOperand(0).getNode();
1923 else if (Op1Unset)
1924 // eqv(0, y) = ~y -> nor(y, y)
1925 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1926 MVT::i1, MachineNode->getOperand(1),
1927 MachineNode->getOperand(1));
1928 else if (Op2Unset)
1929 // eqv(x, 0) = ~x
1930 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1931 MVT::i1, MachineNode->getOperand(0),
1932 MachineNode->getOperand(0));
1933 else if (Op1Not)
1934 // eqv(~x, y) = xor(x, y)
1935 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1936 MVT::i1, MachineNode->getOperand(0).
1937 getOperand(0),
1938 MachineNode->getOperand(1));
1939 else if (Op2Not)
1940 // eqv(x, ~y) = xor(x, y)
1941 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1942 MVT::i1, MachineNode->getOperand(0),
1943 MachineNode->getOperand(1).
1944 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001945 else if (AllUsersSelectZero(MachineNode))
1946 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1947 MVT::i1, MachineNode->getOperand(0),
1948 MachineNode->getOperand(1)),
1949 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001950 break;
1951 case PPC::CRANDC:
1952 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1953 // andc(x, x) = 0
1954 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1955 MVT::i1);
1956 else if (Op1Set)
1957 // andc(1, y) = ~y
1958 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1959 MVT::i1, MachineNode->getOperand(1),
1960 MachineNode->getOperand(1));
1961 else if (Op1Unset || Op2Set)
1962 // andc(0, y) = andc(x, 1) = 0
1963 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1964 MVT::i1);
1965 else if (Op2Unset)
1966 // andc(x, 0) = x
1967 ResNode = MachineNode->getOperand(0).getNode();
1968 else if (Op1Not)
1969 // andc(~x, y) = ~(x | y) = nor(x, y)
1970 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1971 MVT::i1, MachineNode->getOperand(0).
1972 getOperand(0),
1973 MachineNode->getOperand(1));
1974 else if (Op2Not)
1975 // andc(x, ~y) = x & y
1976 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
1977 MVT::i1, MachineNode->getOperand(0),
1978 MachineNode->getOperand(1).
1979 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001980 else if (AllUsersSelectZero(MachineNode))
1981 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1982 MVT::i1, MachineNode->getOperand(1),
1983 MachineNode->getOperand(0)),
1984 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001985 break;
1986 case PPC::CRORC:
1987 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1988 // orc(x, x) = 1
1989 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1990 MVT::i1);
1991 else if (Op1Set || Op2Unset)
1992 // orc(1, y) = orc(x, 0) = 1
1993 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1994 MVT::i1);
1995 else if (Op2Set)
1996 // orc(x, 1) = x
1997 ResNode = MachineNode->getOperand(0).getNode();
1998 else if (Op1Unset)
1999 // orc(0, y) = ~y
2000 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
2001 MVT::i1, MachineNode->getOperand(1),
2002 MachineNode->getOperand(1));
2003 else if (Op1Not)
2004 // orc(~x, y) = ~(x & y) = nand(x, y)
2005 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
2006 MVT::i1, MachineNode->getOperand(0).
2007 getOperand(0),
2008 MachineNode->getOperand(1));
2009 else if (Op2Not)
2010 // orc(x, ~y) = x | y
2011 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
2012 MVT::i1, MachineNode->getOperand(0),
2013 MachineNode->getOperand(1).
2014 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00002015 else if (AllUsersSelectZero(MachineNode))
2016 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
2017 MVT::i1, MachineNode->getOperand(1),
2018 MachineNode->getOperand(0)),
2019 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00002020 break;
2021 case PPC::SELECT_I4:
2022 case PPC::SELECT_I8:
2023 case PPC::SELECT_F4:
2024 case PPC::SELECT_F8:
2025 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00002026 case PPC::SELECT_VSFRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00002027 case PPC::SELECT_VSRC:
Hal Finkel940ab932014-02-28 00:27:01 +00002028 if (Op1Set)
2029 ResNode = MachineNode->getOperand(1).getNode();
2030 else if (Op1Unset)
2031 ResNode = MachineNode->getOperand(2).getNode();
2032 else if (Op1Not)
2033 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(),
2034 SDLoc(MachineNode),
2035 MachineNode->getValueType(0),
2036 MachineNode->getOperand(0).
2037 getOperand(0),
2038 MachineNode->getOperand(2),
2039 MachineNode->getOperand(1));
2040 break;
2041 case PPC::BC:
2042 case PPC::BCn:
2043 if (Op1Not)
2044 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn :
2045 PPC::BC,
2046 SDLoc(MachineNode),
2047 MVT::Other,
2048 MachineNode->getOperand(0).
2049 getOperand(0),
2050 MachineNode->getOperand(1),
2051 MachineNode->getOperand(2));
2052 // FIXME: Handle Op1Set, Op1Unset here too.
2053 break;
2054 }
2055
Hal Finkelb9989152014-02-28 06:11:16 +00002056 // If we're inverting this node because it is used only by selects that
2057 // we'd like to swap, then swap the selects before the node replacement.
2058 if (SelectSwap)
2059 SwapAllSelectUsers(MachineNode);
2060
Hal Finkel940ab932014-02-28 00:27:01 +00002061 if (ResNode != MachineNode) {
2062 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
2063 DEBUG(MachineNode->dump(CurDAG));
2064 DEBUG(dbgs() << "\nNew: ");
2065 DEBUG(ResNode->dump(CurDAG));
2066 DEBUG(dbgs() << "\n");
2067
2068 ReplaceUses(MachineNode, ResNode);
2069 IsModified = true;
2070 }
2071 }
2072 if (IsModified)
2073 CurDAG->RemoveDeadNodes();
2074 } while (IsModified);
2075}
2076
2077void PPCDAGToDAGISel::PeepholePPC64() {
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002078 // These optimizations are currently supported only for 64-bit SVR4.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002079 if (PPCSubTarget->isDarwin() || !PPCSubTarget->isPPC64())
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002080 return;
2081
2082 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
2083 ++Position;
2084
2085 while (Position != CurDAG->allnodes_begin()) {
2086 SDNode *N = --Position;
2087 // Skip dead nodes and any non-machine opcodes.
2088 if (N->use_empty() || !N->isMachineOpcode())
2089 continue;
2090
2091 unsigned FirstOp;
2092 unsigned StorageOpcode = N->getMachineOpcode();
2093
2094 switch (StorageOpcode) {
2095 default: continue;
2096
2097 case PPC::LBZ:
2098 case PPC::LBZ8:
2099 case PPC::LD:
2100 case PPC::LFD:
2101 case PPC::LFS:
2102 case PPC::LHA:
2103 case PPC::LHA8:
2104 case PPC::LHZ:
2105 case PPC::LHZ8:
2106 case PPC::LWA:
2107 case PPC::LWZ:
2108 case PPC::LWZ8:
2109 FirstOp = 0;
2110 break;
2111
2112 case PPC::STB:
2113 case PPC::STB8:
2114 case PPC::STD:
2115 case PPC::STFD:
2116 case PPC::STFS:
2117 case PPC::STH:
2118 case PPC::STH8:
2119 case PPC::STW:
2120 case PPC::STW8:
2121 FirstOp = 1;
2122 break;
2123 }
2124
2125 // If this is a load or store with a zero offset, we may be able to
2126 // fold an add-immediate into the memory operation.
2127 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) ||
2128 N->getConstantOperandVal(FirstOp) != 0)
2129 continue;
2130
2131 SDValue Base = N->getOperand(FirstOp + 1);
2132 if (!Base.isMachineOpcode())
2133 continue;
2134
2135 unsigned Flags = 0;
2136 bool ReplaceFlags = true;
2137
2138 // When the feeding operation is an add-immediate of some sort,
2139 // determine whether we need to add relocation information to the
2140 // target flags on the immediate operand when we fold it into the
2141 // load instruction.
2142 //
2143 // For something like ADDItocL, the relocation information is
2144 // inferred from the opcode; when we process it in the AsmPrinter,
2145 // we add the necessary relocation there. A load, though, can receive
2146 // relocation from various flavors of ADDIxxx, so we need to carry
2147 // the relocation information in the target flags.
2148 switch (Base.getMachineOpcode()) {
2149 default: continue;
2150
2151 case PPC::ADDI8:
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +00002152 case PPC::ADDI:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002153 // In some cases (such as TLS) the relocation information
2154 // is already in place on the operand, so copying the operand
2155 // is sufficient.
2156 ReplaceFlags = false;
2157 // For these cases, the immediate may not be divisible by 4, in
2158 // which case the fold is illegal for DS-form instructions. (The
2159 // other cases provide aligned addresses and are always safe.)
2160 if ((StorageOpcode == PPC::LWA ||
2161 StorageOpcode == PPC::LD ||
2162 StorageOpcode == PPC::STD) &&
2163 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
2164 Base.getConstantOperandVal(1) % 4 != 0))
2165 continue;
2166 break;
2167 case PPC::ADDIdtprelL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002168 Flags = PPCII::MO_DTPREL_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002169 break;
2170 case PPC::ADDItlsldL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002171 Flags = PPCII::MO_TLSLD_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002172 break;
2173 case PPC::ADDItocL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002174 Flags = PPCII::MO_TOC_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002175 break;
2176 }
2177
2178 // We found an opportunity. Reverse the operands from the add
2179 // immediate and substitute them into the load or store. If
2180 // needed, update the target flags for the immediate operand to
2181 // reflect the necessary relocation information.
2182 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
2183 DEBUG(Base->dump(CurDAG));
2184 DEBUG(dbgs() << "\nN: ");
2185 DEBUG(N->dump(CurDAG));
2186 DEBUG(dbgs() << "\n");
2187
2188 SDValue ImmOpnd = Base.getOperand(1);
2189
2190 // If the relocation information isn't already present on the
2191 // immediate operand, add it now.
2192 if (ReplaceFlags) {
Bill Schmidt49498da2013-02-21 14:35:42 +00002193 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002194 SDLoc dl(GA);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002195 const GlobalValue *GV = GA->getGlobal();
Bill Schmidt48fc20a2013-07-01 20:52:27 +00002196 // We can't perform this optimization for data whose alignment
2197 // is insufficient for the instruction encoding.
2198 if (GV->getAlignment() < 4 &&
2199 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
2200 StorageOpcode == PPC::LWA)) {
2201 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
2202 continue;
2203 }
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002204 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags);
Bill Schmidt836c45b2013-02-21 17:26:05 +00002205 } else if (ConstantPoolSDNode *CP =
2206 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
Bill Schmidt49498da2013-02-21 14:35:42 +00002207 const Constant *C = CP->getConstVal();
2208 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
2209 CP->getAlignment(),
2210 0, Flags);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002211 }
2212 }
2213
2214 if (FirstOp == 1) // Store
2215 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
2216 Base.getOperand(0), N->getOperand(3));
2217 else // Load
2218 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
2219 N->getOperand(2));
2220
2221 // The add-immediate may now be dead, in which case remove it.
2222 if (Base.getNode()->use_empty())
2223 CurDAG->RemoveDeadNode(Base.getNode());
2224 }
2225}
Chris Lattner43ff01e2005-08-17 19:33:03 +00002226
Chris Lattnerb055c872006-06-10 01:15:02 +00002227
Andrew Trickc416ba62010-12-24 04:28:06 +00002228/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattner43ff01e2005-08-17 19:33:03 +00002229/// PowerPC-specific DAG, ready for instruction scheduling.
2230///
Evan Cheng2dd2c652006-03-13 23:20:37 +00002231FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman0b71e002005-10-18 00:28:58 +00002232 return new PPCDAGToDAGISel(TM);
Chris Lattner43ff01e2005-08-17 19:33:03 +00002233}
2234
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00002235static void initializePassOnce(PassRegistry &Registry) {
2236 const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection";
Craig Topper062a2ba2014-04-25 05:30:21 +00002237 PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID,
2238 nullptr, false, false);
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00002239 Registry.registerPass(*PI, true);
2240}
2241
2242void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) {
2243 CALL_ONCE_INITIALIZATION(initializePassOnce);
2244}
2245