blob: b9eecbe16f3f864029998a5846d7ac425d332f9b [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 Lattner1ef9cd42006-12-19 22:59:26 +000015#define DEBUG_TYPE "ppc-codegen"
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000016#include "PPC.h"
Evan Cheng11424442011-07-26 00:24:13 +000017#include "MCTargetDesc/PPCPredicates.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"
Hal Finkel940ab932014-02-28 00:27:01 +000030#include "llvm/Support/CommandLine.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000031#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000032#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000034#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000035#include "llvm/Target/TargetOptions.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000036using namespace llvm;
37
Hal Finkel940ab932014-02-28 00:27:01 +000038// FIXME: Remove this once the bug has been fixed!
39cl::opt<bool> ANDIGlueBug("expose-ppc-andi-glue-bug",
40cl::desc("expose the ANDI glue bug on PPC"), cl::Hidden);
41
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000042namespace llvm {
43 void initializePPCDAGToDAGISelPass(PassRegistry&);
44}
45
Chris Lattner43ff01e2005-08-17 19:33:03 +000046namespace {
Chris Lattner43ff01e2005-08-17 19:33:03 +000047 //===--------------------------------------------------------------------===//
Nate Begeman0b71e002005-10-18 00:28:58 +000048 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattner43ff01e2005-08-17 19:33:03 +000049 /// instructions for SelectionDAG operations.
50 ///
Nick Lewycky02d5f772009-10-25 06:33:48 +000051 class PPCDAGToDAGISel : public SelectionDAGISel {
Dan Gohman21cea8a2010-04-17 15:26:15 +000052 const PPCTargetMachine &TM;
53 const PPCTargetLowering &PPCLowering;
Evan Chengec271b12007-10-23 06:42:42 +000054 const PPCSubtarget &PPCSubTarget;
Chris Lattner45640392005-08-19 22:38:53 +000055 unsigned GlobalBaseReg;
Chris Lattner43ff01e2005-08-17 19:33:03 +000056 public:
Dan Gohman56e3f632008-07-07 18:00:37 +000057 explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
Dan Gohman619ef482009-01-15 19:20:50 +000058 : SelectionDAGISel(tm), TM(tm),
Evan Chengec271b12007-10-23 06:42:42 +000059 PPCLowering(*TM.getTargetLowering()),
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000060 PPCSubTarget(*TM.getSubtargetImpl()) {
61 initializePPCDAGToDAGISelPass(*PassRegistry::getPassRegistry());
62 }
Andrew Trickc416ba62010-12-24 04:28:06 +000063
Dan Gohman5ea74d52009-07-31 18:16:33 +000064 virtual bool runOnMachineFunction(MachineFunction &MF) {
Chris Lattner45640392005-08-19 22:38:53 +000065 // Make sure we re-emit a set of the global base reg if necessary
66 GlobalBaseReg = 0;
Dan Gohman5ea74d52009-07-31 18:16:33 +000067 SelectionDAGISel::runOnMachineFunction(MF);
Andrew Trickc416ba62010-12-24 04:28:06 +000068
Bill Schmidt38d94582012-10-10 20:54:15 +000069 if (!PPCSubTarget.isSVR4ABI())
70 InsertVRSaveCode(MF);
71
Chris Lattner1678a6c2006-03-16 18:25:23 +000072 return true;
Chris Lattner45640392005-08-19 22:38:53 +000073 }
Andrew Trickc416ba62010-12-24 04:28:06 +000074
Bill Schmidtf5b474c2013-02-21 00:38:25 +000075 virtual void PostprocessISelDAG();
76
Chris Lattner43ff01e2005-08-17 19:33:03 +000077 /// getI32Imm - Return a target constant with the specified value, of type
78 /// i32.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000079 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000080 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattner43ff01e2005-08-17 19:33:03 +000081 }
Chris Lattner45640392005-08-19 22:38:53 +000082
Chris Lattner97b3da12006-06-27 00:04:13 +000083 /// getI64Imm - Return a target constant with the specified value, of type
84 /// i64.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000085 inline SDValue getI64Imm(uint64_t Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000086 return CurDAG->getTargetConstant(Imm, MVT::i64);
Chris Lattner97b3da12006-06-27 00:04:13 +000087 }
Andrew Trickc416ba62010-12-24 04:28:06 +000088
Chris Lattner97b3da12006-06-27 00:04:13 +000089 /// getSmallIPtrImm - Return a target constant of pointer type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000090 inline SDValue getSmallIPtrImm(unsigned Imm) {
Chris Lattner97b3da12006-06-27 00:04:13 +000091 return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy());
92 }
Andrew Trickc416ba62010-12-24 04:28:06 +000093
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +000094 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
Nate Begemand31efd12006-09-22 05:01:56 +000095 /// with any number of 0s on either side. The 1s are allowed to wrap from
96 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
97 /// 0x0F0F0000 is not, since all 1s are not contiguous.
98 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
99
100
101 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
102 /// rotate and mask opcode and mask operation.
Dale Johannesen86dcae12009-11-24 01:09:07 +0000103 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
Nate Begemand31efd12006-09-22 05:01:56 +0000104 unsigned &SH, unsigned &MB, unsigned &ME);
Andrew Trickc416ba62010-12-24 04:28:06 +0000105
Chris Lattner45640392005-08-19 22:38:53 +0000106 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
107 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000108 SDNode *getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +0000109
Chris Lattner43ff01e2005-08-17 19:33:03 +0000110 // Select - Convert the specified operand from a target-independent to a
111 // target-specific node if it hasn't already been changed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000112 SDNode *Select(SDNode *N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000113
Nate Begeman93c4bc62005-08-19 00:38:14 +0000114 SDNode *SelectBitfieldInsert(SDNode *N);
115
Chris Lattner2a1823d2005-08-21 18:50:37 +0000116 /// SelectCC - Select a comparison of the specified values with the
117 /// specified condition code, returning the CR# of the expression.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000118 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDLoc dl);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000119
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000120 /// SelectAddrImm - Returns true if the address N can be represented by
121 /// a base register plus a signed 16-bit displacement [r+imm].
Chris Lattner0e023ea2010-09-21 20:31:19 +0000122 bool SelectAddrImm(SDValue N, SDValue &Disp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000123 SDValue &Base) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000124 return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG, false);
Chris Lattnera801fced2006-11-08 02:15:41 +0000125 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000126
Chris Lattner6f5840c2006-11-16 00:41:37 +0000127 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000128 /// immediate field. Note that the operand at this point is already the
129 /// result of a prior SelectAddressRegImm call.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000130 bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000131 if (N.getOpcode() == ISD::TargetConstant ||
Hal Finkela86b0f22012-06-21 20:10:48 +0000132 N.getOpcode() == ISD::TargetGlobalAddress) {
Hal Finkel1cc27e42012-06-19 02:34:32 +0000133 Out = N;
134 return true;
135 }
136
137 return false;
138 }
139
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000140 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
141 /// represented as an indexed [r+r] operation. Returns false if it can
142 /// be represented by [r+imm], which are preferred.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000143 bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
Chris Lattnera801fced2006-11-08 02:15:41 +0000144 return PPCLowering.SelectAddressRegReg(N, Base, Index, *CurDAG);
145 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000146
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000147 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
148 /// represented as an indexed [r+r] operation.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000149 bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
Chris Lattnera801fced2006-11-08 02:15:41 +0000150 return PPCLowering.SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
151 }
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000152
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000153 /// SelectAddrImmX4 - Returns true if the address N can be represented by
154 /// a base register plus a signed 16-bit displacement that is a multiple of 4.
155 /// Suitable for use by STD and friends.
156 bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) {
157 return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG, true);
Chris Lattnera801fced2006-11-08 02:15:41 +0000158 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000159
Hal Finkel756810f2013-03-21 21:37:52 +0000160 // Select an address into a single register.
161 bool SelectAddr(SDValue N, SDValue &Base) {
162 Base = N;
163 return true;
164 }
165
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000166 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
Dale Johannesen4a50e682009-08-18 00:18:39 +0000167 /// inline asm expressions. It is always correct to compute the value into
168 /// a register. The case of adding a (possibly relocatable) constant to a
169 /// register can be improved, but it is wrong to substitute Reg+Reg for
170 /// Reg in an asm, because the load or store opcode would have to change.
171 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000172 char ConstraintCode,
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000173 std::vector<SDValue> &OutOps) {
Dale Johannesen4a50e682009-08-18 00:18:39 +0000174 OutOps.push_back(Op);
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000175 return false;
176 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000177
Dan Gohman5ea74d52009-07-31 18:16:33 +0000178 void InsertVRSaveCode(MachineFunction &MF);
Chris Lattner1678a6c2006-03-16 18:25:23 +0000179
Chris Lattner43ff01e2005-08-17 19:33:03 +0000180 virtual const char *getPassName() const {
181 return "PowerPC DAG->DAG Pattern Instruction Selection";
Andrew Trickc416ba62010-12-24 04:28:06 +0000182 }
183
Chris Lattner03e08ee2005-09-13 22:03:06 +0000184// Include the pieces autogenerated from the target description.
Chris Lattner0921e3b2005-10-14 23:37:35 +0000185#include "PPCGenDAGISel.inc"
Andrew Trickc416ba62010-12-24 04:28:06 +0000186
Chris Lattner259e6c72005-10-06 18:45:51 +0000187private:
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000188 SDNode *SelectSETCC(SDNode *N);
Hal Finkel940ab932014-02-28 00:27:01 +0000189
190 void PeepholePPC64();
191 void PeepholdCROps();
Chris Lattner43ff01e2005-08-17 19:33:03 +0000192 };
193}
194
Chris Lattner1678a6c2006-03-16 18:25:23 +0000195/// InsertVRSaveCode - Once the entire function has been instruction selected,
196/// all virtual registers are created and all machine instructions are built,
197/// check to see if we need to save/restore VRSAVE. If so, do it.
Dan Gohman5ea74d52009-07-31 18:16:33 +0000198void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000199 // Check to see if this function uses vector registers, which means we have to
Andrew Trickc416ba62010-12-24 04:28:06 +0000200 // save and restore the VRSAVE register and update it with the regs we use.
Chris Lattner02e2c182006-03-13 21:52:10 +0000201 //
Dan Gohman4a618822010-02-10 16:03:48 +0000202 // In this case, there will be virtual registers of vector type created
Chris Lattner02e2c182006-03-13 21:52:10 +0000203 // by the scheduler. Detect them now.
Chris Lattner02e2c182006-03-13 21:52:10 +0000204 bool HasVectorVReg = false;
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000205 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
206 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
207 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000208 HasVectorVReg = true;
209 break;
210 }
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000211 }
Chris Lattner1678a6c2006-03-16 18:25:23 +0000212 if (!HasVectorVReg) return; // nothing to do.
Andrew Trickc416ba62010-12-24 04:28:06 +0000213
Chris Lattner02e2c182006-03-13 21:52:10 +0000214 // If we have a vector register, we want to emit code into the entry and exit
215 // blocks to save and restore the VRSAVE register. We do this here (instead
216 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
217 //
218 // 1. This (trivially) reduces the load on the register allocator, by not
219 // having to represent the live range of the VRSAVE register.
220 // 2. This (more significantly) allows us to create a temporary virtual
221 // register to hold the saved VRSAVE value, allowing this temporary to be
222 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000223
224 // Create two vregs - one to hold the VRSAVE register that is live-in to the
225 // function and one for the value after having bits or'd into it.
Chris Lattnera10fff52007-12-31 04:13:23 +0000226 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
227 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Andrew Trickc416ba62010-12-24 04:28:06 +0000228
Evan Cheng20350c42006-11-27 23:37:22 +0000229 const TargetInstrInfo &TII = *TM.getInstrInfo();
Chris Lattner1678a6c2006-03-16 18:25:23 +0000230 MachineBasicBlock &EntryBB = *Fn.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000231 DebugLoc dl;
Chris Lattner1678a6c2006-03-16 18:25:23 +0000232 // Emit the following code into the entry block:
233 // InVRSAVE = MFVRSAVE
234 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
235 // MTVRSAVE UpdatedVRSAVE
236 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Dale Johannesene9f623e2009-02-13 02:27:39 +0000237 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
238 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
Chris Lattnera98c6792008-01-07 01:56:04 +0000239 UpdatedVRSAVE).addReg(InVRSAVE);
Dale Johannesene9f623e2009-02-13 02:27:39 +0000240 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000241
Chris Lattner1678a6c2006-03-16 18:25:23 +0000242 // Find all return blocks, outputting a restore in each epilog.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000243 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000244 if (!BB->empty() && BB->back().isReturn()) {
Chris Lattner1678a6c2006-03-16 18:25:23 +0000245 IP = BB->end(); --IP;
Andrew Trickc416ba62010-12-24 04:28:06 +0000246
Chris Lattner1678a6c2006-03-16 18:25:23 +0000247 // Skip over all terminator instructions, which are part of the return
248 // sequence.
249 MachineBasicBlock::iterator I2 = IP;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000250 while (I2 != BB->begin() && (--I2)->isTerminator())
Chris Lattner1678a6c2006-03-16 18:25:23 +0000251 IP = I2;
Andrew Trickc416ba62010-12-24 04:28:06 +0000252
Chris Lattner1678a6c2006-03-16 18:25:23 +0000253 // Emit: MTVRSAVE InVRSave
Dale Johannesene9f623e2009-02-13 02:27:39 +0000254 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000255 }
Chris Lattner02e2c182006-03-13 21:52:10 +0000256 }
Chris Lattner259e6c72005-10-06 18:45:51 +0000257}
Chris Lattner8ae95252005-09-03 01:17:22 +0000258
Chris Lattner1678a6c2006-03-16 18:25:23 +0000259
Chris Lattner45640392005-08-19 22:38:53 +0000260/// getGlobalBaseReg - Output the instructions required to put the
261/// base address to use for accessing globals into a register.
262///
Evan Cheng61413a32006-08-26 05:34:46 +0000263SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner45640392005-08-19 22:38:53 +0000264 if (!GlobalBaseReg) {
Evan Cheng20350c42006-11-27 23:37:22 +0000265 const TargetInstrInfo &TII = *TM.getInstrInfo();
Chris Lattner45640392005-08-19 22:38:53 +0000266 // Insert the set of GlobalBaseReg into the first MBB of the function
Dan Gohmanfca89682009-08-15 02:07:36 +0000267 MachineBasicBlock &FirstMBB = MF->front();
Chris Lattner45640392005-08-19 22:38:53 +0000268 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000269 DebugLoc dl;
Chris Lattner97b3da12006-06-27 00:04:13 +0000270
Owen Anderson9f944592009-08-11 20:47:22 +0000271 if (PPCLowering.getPointerTy() == MVT::i32) {
Craig Topperabadc662012-04-20 06:31:50 +0000272 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000273 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000274 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000275 } else {
Craig Topperabadc662012-04-20 06:31:50 +0000276 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RCRegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000277 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000278 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000279 }
Chris Lattner45640392005-08-19 22:38:53 +0000280 }
Gabor Greif81d6a382008-08-31 15:37:04 +0000281 return CurDAG->getRegister(GlobalBaseReg,
282 PPCLowering.getPointerTy()).getNode();
Chris Lattner97b3da12006-06-27 00:04:13 +0000283}
284
285/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
286/// or 64-bit immediate, and if the value can be accurately represented as a
287/// sign extension from a 16-bit value. If so, this returns true and the
288/// immediate.
289static bool isIntS16Immediate(SDNode *N, short &Imm) {
290 if (N->getOpcode() != ISD::Constant)
291 return false;
292
Dan Gohmaneffb8942008-09-12 16:56:44 +0000293 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +0000294 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +0000295 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000296 else
Dan Gohmaneffb8942008-09-12 16:56:44 +0000297 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000298}
299
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000300static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000301 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattner45640392005-08-19 22:38:53 +0000302}
303
304
Chris Lattner97b3da12006-06-27 00:04:13 +0000305/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
306/// operand. If so Imm will receive the 32-bit value.
307static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000308 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000309 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Nate Begeman72d6f882005-08-18 05:00:13 +0000310 return true;
311 }
312 return false;
313}
314
Chris Lattner97b3da12006-06-27 00:04:13 +0000315/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
316/// operand. If so Imm will receive the 64-bit value.
317static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000318 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000319 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000320 return true;
321 }
322 return false;
323}
324
325// isInt32Immediate - This method tests to see if a constant operand.
326// If so Imm will receive the 32 bit value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000327static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000328 return isInt32Immediate(N.getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000329}
330
331
332// isOpcWithIntImmediate - This method tests to see if the node is a specific
333// opcode and that it has a immediate integer right operand.
334// If so Imm will receive the 32 bit value.
335static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greif81d6a382008-08-31 15:37:04 +0000336 return N->getOpcode() == Opc
337 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000338}
339
Nate Begemand31efd12006-09-22 05:01:56 +0000340bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
Hal Finkelff3ea802013-07-11 16:31:51 +0000341 if (!Val)
342 return false;
343
Nate Begemanb3821a32005-08-18 07:30:46 +0000344 if (isShiftedMask_32(Val)) {
345 // look for the first non-zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000346 MB = countLeadingZeros(Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000347 // look for the first zero bit after the run of ones
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000348 ME = countLeadingZeros((Val - 1) ^ Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000349 return true;
Chris Lattner666512c2005-08-25 04:47:18 +0000350 } else {
351 Val = ~Val; // invert mask
352 if (isShiftedMask_32(Val)) {
353 // effectively look for the first zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000354 ME = countLeadingZeros(Val) - 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000355 // effectively look for the first one bit after the run of zeros
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000356 MB = countLeadingZeros((Val - 1) ^ Val) + 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000357 return true;
358 }
Nate Begemanb3821a32005-08-18 07:30:46 +0000359 }
360 // no run present
361 return false;
362}
363
Andrew Trickc416ba62010-12-24 04:28:06 +0000364bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
365 bool isShiftMask, unsigned &SH,
Nate Begemand31efd12006-09-22 05:01:56 +0000366 unsigned &MB, unsigned &ME) {
Nate Begeman92e77502005-10-19 00:05:37 +0000367 // Don't even go down this path for i64, since different logic will be
368 // necessary for rldicl/rldicr/rldimi.
Owen Anderson9f944592009-08-11 20:47:22 +0000369 if (N->getValueType(0) != MVT::i32)
Nate Begeman92e77502005-10-19 00:05:37 +0000370 return false;
371
Nate Begemanb3821a32005-08-18 07:30:46 +0000372 unsigned Shift = 32;
373 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
374 unsigned Opcode = N->getOpcode();
Chris Lattnere413b602005-08-30 00:59:16 +0000375 if (N->getNumOperands() != 2 ||
Gabor Greiff304a7a2008-08-28 21:40:38 +0000376 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Nate Begemanb3821a32005-08-18 07:30:46 +0000377 return false;
Andrew Trickc416ba62010-12-24 04:28:06 +0000378
Nate Begemanb3821a32005-08-18 07:30:46 +0000379 if (Opcode == ISD::SHL) {
380 // apply shift left to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000381 if (isShiftMask) Mask = Mask << Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000382 // determine which bits are made indeterminant by shift
383 Indeterminant = ~(0xFFFFFFFFu << Shift);
Andrew Trickc416ba62010-12-24 04:28:06 +0000384 } else if (Opcode == ISD::SRL) {
Nate Begemanb3821a32005-08-18 07:30:46 +0000385 // apply shift right to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000386 if (isShiftMask) Mask = Mask >> Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000387 // determine which bits are made indeterminant by shift
388 Indeterminant = ~(0xFFFFFFFFu >> Shift);
389 // adjust for the left rotate
390 Shift = 32 - Shift;
Nate Begemand31efd12006-09-22 05:01:56 +0000391 } else if (Opcode == ISD::ROTL) {
392 Indeterminant = 0;
Nate Begemanb3821a32005-08-18 07:30:46 +0000393 } else {
394 return false;
395 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000396
Nate Begemanb3821a32005-08-18 07:30:46 +0000397 // if the mask doesn't intersect any Indeterminant bits
398 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnera2963392006-05-12 16:29:37 +0000399 SH = Shift & 31;
Nate Begemanb3821a32005-08-18 07:30:46 +0000400 // make sure the mask is still a mask (wrap arounds may not be)
401 return isRunOfOnes(Mask, MB, ME);
402 }
403 return false;
404}
405
Nate Begeman93c4bc62005-08-19 00:38:14 +0000406/// SelectBitfieldInsert - turn an or of two masked values into
407/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman0b71e002005-10-18 00:28:58 +0000408SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000409 SDValue Op0 = N->getOperand(0);
410 SDValue Op1 = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000411 SDLoc dl(N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000412
Dan Gohmanf19609a2008-02-27 01:23:58 +0000413 APInt LKZ, LKO, RKZ, RKO;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000414 CurDAG->ComputeMaskedBits(Op0, LKZ, LKO);
415 CurDAG->ComputeMaskedBits(Op1, RKZ, RKO);
Andrew Trickc416ba62010-12-24 04:28:06 +0000416
Dan Gohmanf19609a2008-02-27 01:23:58 +0000417 unsigned TargetMask = LKZ.getZExtValue();
418 unsigned InsertMask = RKZ.getZExtValue();
Andrew Trickc416ba62010-12-24 04:28:06 +0000419
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000420 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
421 unsigned Op0Opc = Op0.getOpcode();
422 unsigned Op1Opc = Op1.getOpcode();
423 unsigned Value, SH = 0;
424 TargetMask = ~TargetMask;
425 InsertMask = ~InsertMask;
Nate Begeman1333cea2006-05-07 00:23:38 +0000426
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000427 // If the LHS has a foldable shift and the RHS does not, then swap it to the
428 // RHS so that we can fold the shift into the insert.
Nate Begeman1333cea2006-05-07 00:23:38 +0000429 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
430 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
431 Op0.getOperand(0).getOpcode() == ISD::SRL) {
432 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
433 Op1.getOperand(0).getOpcode() != ISD::SRL) {
434 std::swap(Op0, Op1);
435 std::swap(Op0Opc, Op1Opc);
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000436 std::swap(TargetMask, InsertMask);
Nate Begeman1333cea2006-05-07 00:23:38 +0000437 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000438 }
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000439 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
440 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
441 Op1.getOperand(0).getOpcode() != ISD::SRL) {
442 std::swap(Op0, Op1);
443 std::swap(Op0Opc, Op1Opc);
444 std::swap(TargetMask, InsertMask);
445 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000446 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000447
Nate Begeman1333cea2006-05-07 00:23:38 +0000448 unsigned MB, ME;
Hal Finkelff3ea802013-07-11 16:31:51 +0000449 if (isRunOfOnes(InsertMask, MB, ME)) {
Dale Johannesen8495a502009-11-20 22:16:40 +0000450 SDValue Tmp1, Tmp2;
Nate Begeman1333cea2006-05-07 00:23:38 +0000451
452 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000453 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000454 Op1 = Op1.getOperand(0);
455 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
456 }
457 if (Op1Opc == ISD::AND) {
458 unsigned SHOpc = Op1.getOperand(0).getOpcode();
459 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000460 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Hal Finkel4ca70102013-06-28 20:00:07 +0000461 // Note that Value must be in range here (less than 32) because
462 // otherwise there would not be any bits set in InsertMask.
Nate Begeman1333cea2006-05-07 00:23:38 +0000463 Op1 = Op1.getOperand(0).getOperand(0);
464 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
Nate Begeman1333cea2006-05-07 00:23:38 +0000465 }
466 }
Dale Johannesen8495a502009-11-20 22:16:40 +0000467
Chris Lattnera2963392006-05-12 16:29:37 +0000468 SH &= 31;
Dale Johannesen8495a502009-11-20 22:16:40 +0000469 SDValue Ops[] = { Op0, Op1, getI32Imm(SH), getI32Imm(MB),
Evan Chengc3acfc02006-08-27 08:14:06 +0000470 getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +0000471 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000472 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000473 }
474 return 0;
475}
476
Chris Lattner2a1823d2005-08-21 18:50:37 +0000477/// SelectCC - Select a comparison of the specified values with the specified
478/// condition code, returning the CR# of the expression.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000479SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000480 ISD::CondCode CC, SDLoc dl) {
Chris Lattner2a1823d2005-08-21 18:50:37 +0000481 // Always select the LHS.
Chris Lattner97b3da12006-06-27 00:04:13 +0000482 unsigned Opc;
Andrew Trickc416ba62010-12-24 04:28:06 +0000483
Owen Anderson9f944592009-08-11 20:47:22 +0000484 if (LHS.getValueType() == MVT::i32) {
Chris Lattner9a40cca2006-06-27 00:10:13 +0000485 unsigned Imm;
Chris Lattneraa3926b2006-09-20 04:25:47 +0000486 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
487 if (isInt32Immediate(RHS, Imm)) {
488 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000489 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000490 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
491 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +0000492 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000493 if (isInt<16>((int)Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000494 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
495 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000496
Chris Lattneraa3926b2006-09-20 04:25:47 +0000497 // For non-equality comparisons, the default code would materialize the
498 // constant, then compare against it, like this:
499 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +0000500 // ori r2, r2, 22136
Chris Lattneraa3926b2006-09-20 04:25:47 +0000501 // cmpw cr0, r3, r2
502 // Since we are just comparing for equality, we can emit this instead:
503 // xoris r0,r3,0x1234
504 // cmplwi cr0,r0,0x5678
505 // beq cr0,L6
Dan Gohman32f71d72009-09-25 18:54:59 +0000506 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
507 getI32Imm(Imm >> 16)), 0);
508 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
509 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +0000510 }
511 Opc = PPC::CMPLW;
512 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +0000513 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000514 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
515 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +0000516 Opc = PPC::CMPLW;
517 } else {
518 short SImm;
519 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000520 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
521 getI32Imm((int)SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +0000522 0);
523 Opc = PPC::CMPW;
524 }
Owen Anderson9f944592009-08-11 20:47:22 +0000525 } else if (LHS.getValueType() == MVT::i64) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000526 uint64_t Imm;
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000527 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000528 if (isInt64Immediate(RHS.getNode(), Imm)) {
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000529 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000530 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000531 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
532 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000533 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000534 if (isInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000535 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
536 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000537
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000538 // For non-equality comparisons, the default code would materialize the
539 // constant, then compare against it, like this:
540 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +0000541 // ori r2, r2, 22136
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000542 // cmpd cr0, r3, r2
543 // Since we are just comparing for equality, we can emit this instead:
544 // xoris r0,r3,0x1234
545 // cmpldi cr0,r0,0x5678
546 // beq cr0,L6
Benjamin Kramer2788f792010-03-29 21:13:41 +0000547 if (isUInt<32>(Imm)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000548 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
549 getI64Imm(Imm >> 16)), 0);
550 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
551 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000552 }
553 }
554 Opc = PPC::CMPLD;
555 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +0000556 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000557 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
558 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +0000559 Opc = PPC::CMPLD;
560 } else {
561 short SImm;
562 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000563 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
564 getI64Imm(SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +0000565 0);
566 Opc = PPC::CMPD;
567 }
Owen Anderson9f944592009-08-11 20:47:22 +0000568 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000569 Opc = PPC::FCMPUS;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000570 } else {
Owen Anderson9f944592009-08-11 20:47:22 +0000571 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
Chris Lattner97b3da12006-06-27 00:04:13 +0000572 Opc = PPC::FCMPUD;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000573 }
Dan Gohman32f71d72009-09-25 18:54:59 +0000574 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000575}
576
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000577static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
Chris Lattner2a1823d2005-08-21 18:50:37 +0000578 switch (CC) {
Chris Lattner630bbce2006-05-25 16:54:16 +0000579 case ISD::SETUEQ:
Dale Johannesen160be0f2008-11-07 22:54:33 +0000580 case ISD::SETONE:
581 case ISD::SETOLE:
582 case ISD::SETOGE:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000583 llvm_unreachable("Should be lowered by legalize!");
584 default: llvm_unreachable("Unknown condition!");
Dale Johannesen160be0f2008-11-07 22:54:33 +0000585 case ISD::SETOEQ:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000586 case ISD::SETEQ: return PPC::PRED_EQ;
Chris Lattner630bbce2006-05-25 16:54:16 +0000587 case ISD::SETUNE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000588 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000589 case ISD::SETOLT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000590 case ISD::SETLT: return PPC::PRED_LT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000591 case ISD::SETULE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000592 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000593 case ISD::SETOGT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000594 case ISD::SETGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000595 case ISD::SETUGE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000596 case ISD::SETGE: return PPC::PRED_GE;
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000597 case ISD::SETO: return PPC::PRED_NU;
598 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000599 // These two are invalid for floating point. Assume we have int.
600 case ISD::SETULT: return PPC::PRED_LT;
601 case ISD::SETUGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000602 }
Chris Lattner2a1823d2005-08-21 18:50:37 +0000603}
604
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000605/// getCRIdxForSetCC - Return the index of the condition register field
606/// associated with the SetCC condition, and whether or not the field is
607/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Ulrich Weigand47e93282013-07-03 15:13:30 +0000608static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
Chris Lattner89f36e62008-01-08 06:46:30 +0000609 Invert = false;
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000610 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000611 default: llvm_unreachable("Unknown condition!");
Chris Lattner89f36e62008-01-08 06:46:30 +0000612 case ISD::SETOLT:
613 case ISD::SETLT: return 0; // Bit #0 = SETOLT
614 case ISD::SETOGT:
615 case ISD::SETGT: return 1; // Bit #1 = SETOGT
616 case ISD::SETOEQ:
617 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
618 case ISD::SETUO: return 3; // Bit #3 = SETUO
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000619 case ISD::SETUGE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000620 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000621 case ISD::SETULE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000622 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Chris Lattner1fbb0d32006-05-25 18:06:16 +0000623 case ISD::SETUNE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000624 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
625 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Andrew Trickc416ba62010-12-24 04:28:06 +0000626 case ISD::SETUEQ:
627 case ISD::SETOGE:
628 case ISD::SETOLE:
Dale Johannesen160be0f2008-11-07 22:54:33 +0000629 case ISD::SETONE:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000630 llvm_unreachable("Invalid branch code: should be expanded by legalize");
Dale Johannesen160be0f2008-11-07 22:54:33 +0000631 // These are invalid for floating point. Assume integer.
632 case ISD::SETULT: return 0;
633 case ISD::SETUGT: return 1;
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000634 }
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000635}
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000636
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000637// getVCmpInst: return the vector compare instruction for the specified
638// vector type and condition code. Since this is for altivec specific code,
639// only support the altivec types (v16i8, v8i16, v4i32, and v4f32).
640static unsigned int getVCmpInst(MVT::SimpleValueType VecVT, ISD::CondCode CC) {
641 switch (CC) {
642 case ISD::SETEQ:
643 case ISD::SETUEQ:
644 case ISD::SETNE:
645 case ISD::SETUNE:
646 if (VecVT == MVT::v16i8)
647 return PPC::VCMPEQUB;
648 else if (VecVT == MVT::v8i16)
649 return PPC::VCMPEQUH;
650 else if (VecVT == MVT::v4i32)
651 return PPC::VCMPEQUW;
652 // v4f32 != v4f32 could be translate to unordered not equal
653 else if (VecVT == MVT::v4f32)
654 return PPC::VCMPEQFP;
655 break;
656 case ISD::SETLT:
657 case ISD::SETGT:
658 case ISD::SETLE:
659 case ISD::SETGE:
660 if (VecVT == MVT::v16i8)
661 return PPC::VCMPGTSB;
662 else if (VecVT == MVT::v8i16)
663 return PPC::VCMPGTSH;
664 else if (VecVT == MVT::v4i32)
665 return PPC::VCMPGTSW;
666 else if (VecVT == MVT::v4f32)
667 return PPC::VCMPGTFP;
668 break;
669 case ISD::SETULT:
670 case ISD::SETUGT:
671 case ISD::SETUGE:
672 case ISD::SETULE:
673 if (VecVT == MVT::v16i8)
674 return PPC::VCMPGTUB;
675 else if (VecVT == MVT::v8i16)
676 return PPC::VCMPGTUH;
677 else if (VecVT == MVT::v4i32)
678 return PPC::VCMPGTUW;
679 break;
680 case ISD::SETOEQ:
681 if (VecVT == MVT::v4f32)
682 return PPC::VCMPEQFP;
683 break;
684 case ISD::SETOLT:
685 case ISD::SETOGT:
686 case ISD::SETOLE:
687 if (VecVT == MVT::v4f32)
688 return PPC::VCMPGTFP;
689 break;
690 case ISD::SETOGE:
691 if (VecVT == MVT::v4f32)
692 return PPC::VCMPGEFP;
693 break;
694 default:
695 break;
696 }
697 llvm_unreachable("Invalid integer vector compare condition");
698}
699
700// getVCmpEQInst: return the equal compare instruction for the specified vector
701// type. Since this is for altivec specific code, only support the altivec
702// types (v16i8, v8i16, v4i32, and v4f32).
703static unsigned int getVCmpEQInst(MVT::SimpleValueType VecVT) {
704 switch (VecVT) {
705 case MVT::v16i8:
706 return PPC::VCMPEQUB;
707 case MVT::v8i16:
708 return PPC::VCMPEQUH;
709 case MVT::v4i32:
710 return PPC::VCMPEQUW;
711 case MVT::v4f32:
712 return PPC::VCMPEQFP;
713 default:
714 llvm_unreachable("Invalid integer vector compare condition");
715 }
716}
717
718
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000719SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000720 SDLoc dl(N);
Chris Lattner491b8292005-10-06 19:03:35 +0000721 unsigned Imm;
722 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Roman Divacky254f8212011-06-20 15:28:39 +0000723 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
724 bool isPPC64 = (PtrVT == MVT::i64);
725
Hal Finkel940ab932014-02-28 00:27:01 +0000726 if (!PPCSubTarget.useCRBits() &&
727 isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner491b8292005-10-06 19:03:35 +0000728 // We can codegen setcc op, imm very efficiently compared to a brcond.
729 // Check for those cases here.
730 // setcc op, 0
731 if (Imm == 0) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000732 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +0000733 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +0000734 default: break;
Evan Chengc3acfc02006-08-27 08:14:06 +0000735 case ISD::SETEQ: {
Dan Gohman32f71d72009-09-25 18:54:59 +0000736 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000737 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
Owen Anderson9f944592009-08-11 20:47:22 +0000738 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Evan Chengc3acfc02006-08-27 08:14:06 +0000739 }
Chris Lattnere2969492005-10-21 21:17:10 +0000740 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +0000741 if (isPPC64) break;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000742 SDValue AD =
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000743 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000744 Op, getI32Imm(~0U)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000745 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000746 AD.getValue(1));
Chris Lattner491b8292005-10-06 19:03:35 +0000747 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000748 case ISD::SETLT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000749 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Owen Anderson9f944592009-08-11 20:47:22 +0000750 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Evan Chengc3acfc02006-08-27 08:14:06 +0000751 }
Chris Lattnere2969492005-10-21 21:17:10 +0000752 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000753 SDValue T =
Dan Gohman32f71d72009-09-25 18:54:59 +0000754 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
755 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000756 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Owen Anderson9f944592009-08-11 20:47:22 +0000757 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattnere2969492005-10-21 21:17:10 +0000758 }
759 }
Chris Lattner491b8292005-10-06 19:03:35 +0000760 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000761 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +0000762 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +0000763 default: break;
764 case ISD::SETEQ:
Roman Divacky254f8212011-06-20 15:28:39 +0000765 if (isPPC64) break;
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000766 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000767 Op, getI32Imm(1)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000768 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
769 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
Dan Gohman32f71d72009-09-25 18:54:59 +0000770 MVT::i32,
771 getI32Imm(0)), 0),
Dale Johannesenf08a47b2009-02-04 23:02:30 +0000772 Op.getValue(1));
Chris Lattnere2969492005-10-21 21:17:10 +0000773 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +0000774 if (isPPC64) break;
Dan Gohman32f71d72009-09-25 18:54:59 +0000775 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000776 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000777 Op, getI32Imm(~0U));
Owen Anderson9f944592009-08-11 20:47:22 +0000778 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000779 Op, SDValue(AD, 1));
Chris Lattner491b8292005-10-06 19:03:35 +0000780 }
Chris Lattnere2969492005-10-21 21:17:10 +0000781 case ISD::SETLT: {
Dan Gohman32f71d72009-09-25 18:54:59 +0000782 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
783 getI32Imm(1)), 0);
784 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
785 Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000786 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Owen Anderson9f944592009-08-11 20:47:22 +0000787 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattnere2969492005-10-21 21:17:10 +0000788 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000789 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000790 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Michael Liaob53d8962013-04-19 22:22:57 +0000791 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
Dale Johannesenf08a47b2009-02-04 23:02:30 +0000792 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000793 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000794 getI32Imm(1));
Chris Lattnere2969492005-10-21 21:17:10 +0000795 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000796 }
Chris Lattner491b8292005-10-06 19:03:35 +0000797 }
798 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000799
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000800 SDValue LHS = N->getOperand(0);
801 SDValue RHS = N->getOperand(1);
802
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000803 // Altivec Vector compare instructions do not set any CR register by default and
804 // vector compare operations return the same type as the operands.
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000805 if (LHS.getValueType().isVector()) {
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000806 EVT VecVT = LHS.getValueType();
807 MVT::SimpleValueType VT = VecVT.getSimpleVT().SimpleTy;
808 unsigned int VCmpInst = getVCmpInst(VT, CC);
809
810 switch (CC) {
811 case ISD::SETEQ:
812 case ISD::SETOEQ:
813 case ISD::SETUEQ:
814 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
815 case ISD::SETNE:
816 case ISD::SETONE:
817 case ISD::SETUNE: {
818 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
819 return CurDAG->SelectNodeTo(N, PPC::VNOR, VecVT, VCmp, VCmp);
820 }
821 case ISD::SETLT:
822 case ISD::SETOLT:
823 case ISD::SETULT:
824 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, RHS, LHS);
825 case ISD::SETGT:
826 case ISD::SETOGT:
827 case ISD::SETUGT:
828 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
829 case ISD::SETGE:
830 case ISD::SETOGE:
831 case ISD::SETUGE: {
832 // Small optimization: Altivec provides a 'Vector Compare Greater Than
833 // or Equal To' instruction (vcmpgefp), so in this case there is no
834 // need for extra logic for the equal compare.
835 if (VecVT.getSimpleVT().isFloatingPoint()) {
836 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
837 } else {
838 SDValue VCmpGT(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
839 unsigned int VCmpEQInst = getVCmpEQInst(VT);
840 SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
841 return CurDAG->SelectNodeTo(N, PPC::VOR, VecVT, VCmpGT, VCmpEQ);
842 }
843 }
844 case ISD::SETLE:
845 case ISD::SETOLE:
846 case ISD::SETULE: {
847 SDValue VCmpLE(CurDAG->getMachineNode(VCmpInst, dl, VecVT, RHS, LHS), 0);
848 unsigned int VCmpEQInst = getVCmpEQInst(VT);
849 SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
850 return CurDAG->SelectNodeTo(N, PPC::VOR, VecVT, VCmpLE, VCmpEQ);
851 }
852 default:
853 llvm_unreachable("Invalid vector compare type: should be expanded by legalize");
854 }
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000855 }
856
Hal Finkel940ab932014-02-28 00:27:01 +0000857 if (PPCSubTarget.useCRBits())
858 return 0;
859
Chris Lattner491b8292005-10-06 19:03:35 +0000860 bool Inv;
Ulrich Weigand47e93282013-07-03 15:13:30 +0000861 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000862 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000863 SDValue IntCR;
Andrew Trickc416ba62010-12-24 04:28:06 +0000864
Chris Lattner491b8292005-10-06 19:03:35 +0000865 // Force the ccreg into CR7.
Owen Anderson9f944592009-08-11 20:47:22 +0000866 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Andrew Trickc416ba62010-12-24 04:28:06 +0000867
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000868 SDValue InFlag(0, 0); // Null incoming flag value.
Andrew Trickc416ba62010-12-24 04:28:06 +0000869 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Chris Lattnerbd099102005-12-01 03:50:19 +0000870 InFlag).getValue(1);
Andrew Trickc416ba62010-12-24 04:28:06 +0000871
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000872 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
873 CCReg), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000874
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000875 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
Evan Chengc3acfc02006-08-27 08:14:06 +0000876 getI32Imm(31), getI32Imm(31) };
Ulrich Weigand47e93282013-07-03 15:13:30 +0000877 if (!Inv)
Owen Anderson9f944592009-08-11 20:47:22 +0000878 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattner89f36e62008-01-08 06:46:30 +0000879
880 // Get the specified bit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000881 SDValue Tmp =
Michael Liaob53d8962013-04-19 22:22:57 +0000882 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Ulrich Weigand47e93282013-07-03 15:13:30 +0000883 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner491b8292005-10-06 19:03:35 +0000884}
Chris Lattner502a3692005-10-06 18:56:10 +0000885
Chris Lattner318622f2005-10-06 19:07:45 +0000886
Chris Lattner43ff01e2005-08-17 19:33:03 +0000887// Select - Convert the specified operand from a target-independent to a
888// target-specific node if it hasn't already been changed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000889SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000890 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +0000891 if (N->isMachineOpcode()) {
892 N->setNodeId(-1);
Evan Chengbd1c5a82006-08-11 09:08:15 +0000893 return NULL; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +0000894 }
Chris Lattner08c319f2005-09-29 00:59:32 +0000895
Chris Lattner43ff01e2005-08-17 19:33:03 +0000896 switch (N->getOpcode()) {
Chris Lattner498915d2005-09-07 23:45:15 +0000897 default: break;
Andrew Trickc416ba62010-12-24 04:28:06 +0000898
Jim Laskey095e6f32006-12-12 13:23:43 +0000899 case ISD::Constant: {
Owen Anderson9f944592009-08-11 20:47:22 +0000900 if (N->getValueType(0) == MVT::i64) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000901 // Get 64 bit value.
Dan Gohmaneffb8942008-09-12 16:56:44 +0000902 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
Jim Laskey095e6f32006-12-12 13:23:43 +0000903 // Assume no remaining bits.
904 unsigned Remainder = 0;
905 // Assume no shift required.
906 unsigned Shift = 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000907
Jim Laskey095e6f32006-12-12 13:23:43 +0000908 // If it can't be represented as a 32 bit value.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000909 if (!isInt<32>(Imm)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000910 Shift = countTrailingZeros<uint64_t>(Imm);
Jim Laskey095e6f32006-12-12 13:23:43 +0000911 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
Andrew Trickc416ba62010-12-24 04:28:06 +0000912
Jim Laskey095e6f32006-12-12 13:23:43 +0000913 // If the shifted value fits 32 bits.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000914 if (isInt<32>(ImmSh)) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000915 // Go with the shifted value.
916 Imm = ImmSh;
917 } else {
918 // Still stuck with a 64 bit value.
919 Remainder = Imm;
920 Shift = 32;
921 Imm >>= 32;
922 }
923 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000924
Jim Laskey095e6f32006-12-12 13:23:43 +0000925 // Intermediate operand.
926 SDNode *Result;
927
928 // Handle first 32 bits.
929 unsigned Lo = Imm & 0xFFFF;
930 unsigned Hi = (Imm >> 16) & 0xFFFF;
Andrew Trickc416ba62010-12-24 04:28:06 +0000931
Jim Laskey095e6f32006-12-12 13:23:43 +0000932 // Simple value.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000933 if (isInt<16>(Imm)) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000934 // Just the Lo bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000935 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000936 } else if (Lo) {
937 // Handle the Hi bits.
938 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
Dan Gohman32f71d72009-09-25 18:54:59 +0000939 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
Jim Laskey095e6f32006-12-12 13:23:43 +0000940 // And Lo bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000941 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
942 SDValue(Result, 0), getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000943 } else {
944 // Just the Hi bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000945 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
Jim Laskey095e6f32006-12-12 13:23:43 +0000946 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000947
Jim Laskey095e6f32006-12-12 13:23:43 +0000948 // If no shift, we're done.
949 if (!Shift) return Result;
950
951 // Shift for next step if the upper 32-bits were not zero.
952 if (Imm) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000953 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
954 SDValue(Result, 0),
955 getI32Imm(Shift),
956 getI32Imm(63 - Shift));
Jim Laskey095e6f32006-12-12 13:23:43 +0000957 }
958
959 // Add in the last bits as required.
960 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000961 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
962 SDValue(Result, 0), getI32Imm(Hi));
Andrew Trickc416ba62010-12-24 04:28:06 +0000963 }
Jim Laskey095e6f32006-12-12 13:23:43 +0000964 if ((Lo = Remainder & 0xFFFF)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000965 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
966 SDValue(Result, 0), getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000967 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000968
Jim Laskey095e6f32006-12-12 13:23:43 +0000969 return Result;
970 }
971 break;
972 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000973
Hal Finkel940ab932014-02-28 00:27:01 +0000974 case ISD::SETCC: {
975 SDNode *SN = SelectSETCC(N);
976 if (SN)
977 return SN;
978 break;
979 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000980 case PPCISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +0000981 return getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +0000982
Chris Lattnere4c338d2005-08-25 00:45:43 +0000983 case ISD::FrameIndex: {
984 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000985 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
986 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
Chris Lattnerbc485fd2006-08-15 23:48:22 +0000987 if (N->hasOneUse())
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000988 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), TFI,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000989 getSmallIPtrImm(0));
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000990 return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
Dan Gohman32f71d72009-09-25 18:54:59 +0000991 getSmallIPtrImm(0));
Chris Lattnere4c338d2005-08-25 00:45:43 +0000992 }
Chris Lattner6961fc72006-03-26 10:06:40 +0000993
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000994 case PPCISD::MFOCRF: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000995 SDValue InFlag = N->getOperand(1);
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000996 return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
997 N->getOperand(0), InFlag);
Chris Lattner6961fc72006-03-26 10:06:40 +0000998 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000999
Chris Lattner57693112005-09-28 22:50:24 +00001000 case ISD::SDIV: {
Nate Begeman4dd38312005-10-21 00:02:42 +00001001 // FIXME: since this depends on the setting of the carry flag from the srawi
1002 // we should really be making notes about that for the scheduler.
Andrew Trickc416ba62010-12-24 04:28:06 +00001003 // FIXME: It sure would be nice if we could cheaply recognize the
Nate Begeman4dd38312005-10-21 00:02:42 +00001004 // srl/add/sra pattern the dag combiner will generate for this as
1005 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattnerdc664572005-08-25 17:50:06 +00001006 unsigned Imm;
Chris Lattner97b3da12006-06-27 00:04:13 +00001007 if (isInt32Immediate(N->getOperand(1), Imm)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001008 SDValue N0 = N->getOperand(0);
Chris Lattnerdc664572005-08-25 17:50:06 +00001009 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Chengd1b82d82006-02-09 07:17:49 +00001010 SDNode *Op =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001011 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00001012 N0, getI32Imm(Log2_32(Imm)));
Andrew Trickc416ba62010-12-24 04:28:06 +00001013 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001014 SDValue(Op, 0), SDValue(Op, 1));
Chris Lattnerdc664572005-08-25 17:50:06 +00001015 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Chengd1b82d82006-02-09 07:17:49 +00001016 SDNode *Op =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001017 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00001018 N0, getI32Imm(Log2_32(-Imm)));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001019 SDValue PT =
Dan Gohman32f71d72009-09-25 18:54:59 +00001020 SDValue(CurDAG->getMachineNode(PPC::ADDZE, dl, MVT::i32,
1021 SDValue(Op, 0), SDValue(Op, 1)),
Evan Chengd1b82d82006-02-09 07:17:49 +00001022 0);
Owen Anderson9f944592009-08-11 20:47:22 +00001023 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattnerdc664572005-08-25 17:50:06 +00001024 }
1025 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001026
Chris Lattner1de57062005-09-29 23:33:31 +00001027 // Other cases are autogenerated.
1028 break;
Chris Lattner6e184f22005-08-25 22:04:30 +00001029 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001030
Chris Lattnerce645542006-11-10 02:08:47 +00001031 case ISD::LOAD: {
1032 // Handle preincrement loads.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001033 LoadSDNode *LD = cast<LoadSDNode>(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001034 EVT LoadedVT = LD->getMemoryVT();
Andrew Trickc416ba62010-12-24 04:28:06 +00001035
Chris Lattnerce645542006-11-10 02:08:47 +00001036 // Normal loads are handled by code generated from the .td file.
1037 if (LD->getAddressingMode() != ISD::PRE_INC)
1038 break;
Andrew Trickc416ba62010-12-24 04:28:06 +00001039
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001040 SDValue Offset = LD->getOffset();
Ulrich Weigandd1b99d32013-03-22 14:58:17 +00001041 if (Offset.getOpcode() == ISD::TargetConstant ||
Chris Lattnerc5102bf2006-11-11 04:53:30 +00001042 Offset.getOpcode() == ISD::TargetGlobalAddress) {
Andrew Trickc416ba62010-12-24 04:28:06 +00001043
Chris Lattner474b5b72006-11-15 19:55:13 +00001044 unsigned Opcode;
1045 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
Owen Anderson9f944592009-08-11 20:47:22 +00001046 if (LD->getValueType(0) != MVT::i64) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001047 // Handle PPC32 integer and normal FP loads.
Owen Anderson9f944592009-08-11 20:47:22 +00001048 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1049 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001050 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00001051 case MVT::f64: Opcode = PPC::LFDU; break;
1052 case MVT::f32: Opcode = PPC::LFSU; break;
1053 case MVT::i32: Opcode = PPC::LWZU; break;
1054 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
1055 case MVT::i1:
1056 case MVT::i8: Opcode = PPC::LBZU; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00001057 }
1058 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00001059 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1060 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1061 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001062 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00001063 case MVT::i64: Opcode = PPC::LDU; break;
1064 case MVT::i32: Opcode = PPC::LWZU8; break;
1065 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
1066 case MVT::i1:
1067 case MVT::i8: Opcode = PPC::LBZU8; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00001068 }
1069 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001070
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001071 SDValue Chain = LD->getChain();
1072 SDValue Base = LD->getBasePtr();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001073 SDValue Ops[] = { Offset, Base, Chain };
Dan Gohman32f71d72009-09-25 18:54:59 +00001074 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
1075 PPCLowering.getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00001076 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00001077 } else {
Hal Finkelca542be2012-06-20 15:43:03 +00001078 unsigned Opcode;
1079 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
1080 if (LD->getValueType(0) != MVT::i64) {
1081 // Handle PPC32 integer and normal FP loads.
1082 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1083 switch (LoadedVT.getSimpleVT().SimpleTy) {
1084 default: llvm_unreachable("Invalid PPC load type!");
1085 case MVT::f64: Opcode = PPC::LFDUX; break;
1086 case MVT::f32: Opcode = PPC::LFSUX; break;
1087 case MVT::i32: Opcode = PPC::LWZUX; break;
1088 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
1089 case MVT::i1:
1090 case MVT::i8: Opcode = PPC::LBZUX; break;
1091 }
1092 } else {
1093 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1094 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
1095 "Invalid sext update load");
1096 switch (LoadedVT.getSimpleVT().SimpleTy) {
1097 default: llvm_unreachable("Invalid PPC load type!");
1098 case MVT::i64: Opcode = PPC::LDUX; break;
1099 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
1100 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
1101 case MVT::i1:
1102 case MVT::i8: Opcode = PPC::LBZUX8; break;
1103 }
1104 }
1105
1106 SDValue Chain = LD->getChain();
1107 SDValue Base = LD->getBasePtr();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001108 SDValue Ops[] = { Base, Offset, Chain };
Hal Finkelca542be2012-06-20 15:43:03 +00001109 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
1110 PPCLowering.getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00001111 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00001112 }
1113 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001114
Nate Begemanb3821a32005-08-18 07:30:46 +00001115 case ISD::AND: {
Nate Begemand31efd12006-09-22 05:01:56 +00001116 unsigned Imm, Imm2, SH, MB, ME;
Hal Finkele39526a2012-08-28 02:10:15 +00001117 uint64_t Imm64;
Nate Begemand31efd12006-09-22 05:01:56 +00001118
Nate Begemanb3821a32005-08-18 07:30:46 +00001119 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1120 // with a mask, emit rlwinm
Chris Lattner97b3da12006-06-27 00:04:13 +00001121 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00001122 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001123 SDValue Val = N->getOperand(0).getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001124 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Owen Anderson9f944592009-08-11 20:47:22 +00001125 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begemanb3821a32005-08-18 07:30:46 +00001126 }
Nate Begemand31efd12006-09-22 05:01:56 +00001127 // If this is just a masked value where the input is not handled above, and
1128 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
1129 if (isInt32Immediate(N->getOperand(1), Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00001130 isRunOfOnes(Imm, MB, ME) &&
Nate Begemand31efd12006-09-22 05:01:56 +00001131 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001132 SDValue Val = N->getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001133 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
Owen Anderson9f944592009-08-11 20:47:22 +00001134 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begemand31efd12006-09-22 05:01:56 +00001135 }
Hal Finkele39526a2012-08-28 02:10:15 +00001136 // If this is a 64-bit zero-extension mask, emit rldicl.
1137 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
1138 isMask_64(Imm64)) {
1139 SDValue Val = N->getOperand(0);
1140 MB = 64 - CountTrailingOnes_64(Imm64);
Hal Finkel22498fa2013-11-20 01:10:15 +00001141 SH = 0;
1142
1143 // If the operand is a logical right shift, we can fold it into this
1144 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb)
1145 // for n <= mb. The right shift is really a left rotate followed by a
1146 // mask, and this mask is a more-restrictive sub-mask of the mask implied
1147 // by the shift.
1148 if (Val.getOpcode() == ISD::SRL &&
1149 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) {
1150 assert(Imm < 64 && "Illegal shift amount");
1151 Val = Val.getOperand(0);
1152 SH = 64 - Imm;
1153 }
1154
1155 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB) };
Hal Finkele39526a2012-08-28 02:10:15 +00001156 return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops, 3);
1157 }
Nate Begemand31efd12006-09-22 05:01:56 +00001158 // AND X, 0 -> 0, not "rlwinm 32".
1159 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001160 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Nate Begemand31efd12006-09-22 05:01:56 +00001161 return NULL;
1162 }
Nate Begeman9aea6e42005-12-24 01:00:15 +00001163 // ISD::OR doesn't get all the bitfield insertion fun.
1164 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
Andrew Trickc416ba62010-12-24 04:28:06 +00001165 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman9aea6e42005-12-24 01:00:15 +00001166 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattner97b3da12006-06-27 00:04:13 +00001167 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattner20c88df2006-01-05 18:32:49 +00001168 unsigned MB, ME;
Nate Begeman9aea6e42005-12-24 01:00:15 +00001169 Imm = ~(Imm^Imm2);
1170 if (isRunOfOnes(Imm, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001171 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001172 N->getOperand(0).getOperand(1),
1173 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +00001174 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman9aea6e42005-12-24 01:00:15 +00001175 }
1176 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001177
Chris Lattner1de57062005-09-29 23:33:31 +00001178 // Other cases are autogenerated.
1179 break;
Nate Begemanb3821a32005-08-18 07:30:46 +00001180 }
Nate Begeman93c4bc62005-08-19 00:38:14 +00001181 case ISD::OR:
Owen Anderson9f944592009-08-11 20:47:22 +00001182 if (N->getValueType(0) == MVT::i32)
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001183 if (SDNode *I = SelectBitfieldInsert(N))
1184 return I;
Andrew Trickc416ba62010-12-24 04:28:06 +00001185
Chris Lattner1de57062005-09-29 23:33:31 +00001186 // Other cases are autogenerated.
1187 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001188 case ISD::SHL: {
1189 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001190 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001191 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001192 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001193 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Owen Anderson9f944592009-08-11 20:47:22 +00001194 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00001195 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001196
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001197 // Other cases are autogenerated.
1198 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001199 }
1200 case ISD::SRL: {
1201 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001202 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00001203 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001204 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001205 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Owen Anderson9f944592009-08-11 20:47:22 +00001206 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00001207 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001208
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001209 // Other cases are autogenerated.
1210 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001211 }
Hal Finkel940ab932014-02-28 00:27:01 +00001212 // FIXME: Remove this once the ANDI glue bug is fixed:
1213 case PPCISD::ANDIo_1_EQ_BIT:
1214 case PPCISD::ANDIo_1_GT_BIT: {
1215 if (!ANDIGlueBug)
1216 break;
1217
1218 EVT InVT = N->getOperand(0).getValueType();
1219 assert((InVT == MVT::i64 || InVT == MVT::i32) &&
1220 "Invalid input type for ANDIo_1_EQ_BIT");
1221
1222 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo;
1223 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue,
1224 N->getOperand(0),
1225 CurDAG->getTargetConstant(1, InVT)), 0);
1226 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
1227 SDValue SRIdxVal =
1228 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ?
1229 PPC::sub_eq : PPC::sub_gt, MVT::i32);
1230
1231 return CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1,
1232 CR0Reg, SRIdxVal,
1233 SDValue(AndI.getNode(), 1) /* glue */);
1234 }
Chris Lattnerbec817c2005-08-26 18:46:49 +00001235 case ISD::SELECT_CC: {
1236 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Roman Divacky254f8212011-06-20 15:28:39 +00001237 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
1238 bool isPPC64 = (PtrVT == MVT::i64);
Andrew Trickc416ba62010-12-24 04:28:06 +00001239
Hal Finkel940ab932014-02-28 00:27:01 +00001240 // If this is a select of i1 operands, we'll pattern match it.
1241 if (PPCSubTarget.useCRBits() &&
1242 N->getOperand(0).getValueType() == MVT::i1)
1243 break;
1244
Chris Lattner97b3da12006-06-27 00:04:13 +00001245 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Roman Divacky254f8212011-06-20 15:28:39 +00001246 if (!isPPC64)
1247 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1248 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1249 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1250 if (N1C->isNullValue() && N3C->isNullValue() &&
1251 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
1252 // FIXME: Implement this optzn for PPC64.
1253 N->getValueType(0) == MVT::i32) {
1254 SDNode *Tmp =
1255 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
1256 N->getOperand(0), getI32Imm(~0U));
1257 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1258 SDValue(Tmp, 0), N->getOperand(0),
1259 SDValue(Tmp, 1));
1260 }
Chris Lattner9b577f12005-08-26 21:23:58 +00001261
Dale Johannesenab8e4422009-02-06 19:16:40 +00001262 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00001263
1264 if (N->getValueType(0) == MVT::i1) {
1265 // An i1 select is: (c & t) | (!c & f).
1266 bool Inv;
1267 unsigned Idx = getCRIdxForSetCC(CC, Inv);
1268
1269 unsigned SRI;
1270 switch (Idx) {
1271 default: llvm_unreachable("Invalid CC index");
1272 case 0: SRI = PPC::sub_lt; break;
1273 case 1: SRI = PPC::sub_gt; break;
1274 case 2: SRI = PPC::sub_eq; break;
1275 case 3: SRI = PPC::sub_un; break;
1276 }
1277
1278 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg);
1279
1280 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1,
1281 CCBit, CCBit), 0);
1282 SDValue C = Inv ? NotCCBit : CCBit,
1283 NotC = Inv ? CCBit : NotCCBit;
1284
1285 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1286 C, N->getOperand(2)), 0);
1287 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1288 NotC, N->getOperand(3)), 0);
1289
1290 return CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF);
1291 }
1292
Chris Lattner8c6a41e2006-11-17 22:10:59 +00001293 unsigned BROpc = getPredicateForSetCC(CC);
Chris Lattner9b577f12005-08-26 21:23:58 +00001294
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001295 unsigned SelectCCOp;
Owen Anderson9f944592009-08-11 20:47:22 +00001296 if (N->getValueType(0) == MVT::i32)
Chris Lattner97b3da12006-06-27 00:04:13 +00001297 SelectCCOp = PPC::SELECT_CC_I4;
Owen Anderson9f944592009-08-11 20:47:22 +00001298 else if (N->getValueType(0) == MVT::i64)
Chris Lattner97b3da12006-06-27 00:04:13 +00001299 SelectCCOp = PPC::SELECT_CC_I8;
Owen Anderson9f944592009-08-11 20:47:22 +00001300 else if (N->getValueType(0) == MVT::f32)
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001301 SelectCCOp = PPC::SELECT_CC_F4;
Owen Anderson9f944592009-08-11 20:47:22 +00001302 else if (N->getValueType(0) == MVT::f64)
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001303 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner0a3d1bb2006-04-08 22:45:08 +00001304 else
1305 SelectCCOp = PPC::SELECT_CC_VRRC;
1306
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001307 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Evan Chengc3acfc02006-08-27 08:14:06 +00001308 getI32Imm(BROpc) };
1309 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops, 4);
Chris Lattnerbec817c2005-08-26 18:46:49 +00001310 }
Hal Finkel25c19922013-05-15 21:37:41 +00001311 case PPCISD::BDNZ:
1312 case PPCISD::BDZ: {
1313 bool IsPPC64 = PPCSubTarget.isPPC64();
1314 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
1315 return CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ ?
1316 (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1317 (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
1318 MVT::Other, Ops, 2);
1319 }
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001320 case PPCISD::COND_BRANCH: {
Dan Gohman7a638a82008-11-05 17:16:24 +00001321 // Op #0 is the Chain.
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001322 // Op #1 is the PPC::PRED_* number.
1323 // Op #2 is the CR#
1324 // Op #3 is the Dest MBB
Dan Gohmanf14b77e2008-11-05 04:14:16 +00001325 // Op #4 is the Flag.
Evan Cheng58d1eac2007-06-29 01:25:06 +00001326 // Prevent PPC::PRED_* from being selected into LI.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001327 SDValue Pred =
Dan Gohmaneffb8942008-09-12 16:56:44 +00001328 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001329 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001330 N->getOperand(0), N->getOperand(4) };
Owen Anderson9f944592009-08-11 20:47:22 +00001331 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 5);
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001332 }
Nate Begemanbb01d4f2006-03-17 01:40:33 +00001333 case ISD::BR_CC: {
Chris Lattner2a1823d2005-08-21 18:50:37 +00001334 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Hal Finkel940ab932014-02-28 00:27:01 +00001335 unsigned PCC = getPredicateForSetCC(CC);
1336
1337 if (N->getOperand(2).getValueType() == MVT::i1) {
1338 unsigned Opc;
1339 bool Swap;
1340 switch (PCC) {
1341 default: llvm_unreachable("Unexpected Boolean-operand predicate");
1342 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break;
1343 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break;
1344 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break;
1345 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break;
1346 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break;
1347 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break;
1348 }
1349
1350 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1,
1351 N->getOperand(Swap ? 3 : 2),
1352 N->getOperand(Swap ? 2 : 3)), 0);
1353 return CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other,
1354 BitComp, N->getOperand(4), N->getOperand(0));
1355 }
1356
Dale Johannesenab8e4422009-02-06 19:16:40 +00001357 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00001358 SDValue Ops[] = { getI32Imm(PCC), CondCode,
Evan Chengc3acfc02006-08-27 08:14:06 +00001359 N->getOperand(4), N->getOperand(0) };
Owen Anderson9f944592009-08-11 20:47:22 +00001360 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 4);
Chris Lattner2a1823d2005-08-21 18:50:37 +00001361 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001362 case ISD::BRIND: {
Chris Lattnerb055c872006-06-10 01:15:02 +00001363 // FIXME: Should custom lower this.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001364 SDValue Chain = N->getOperand(0);
1365 SDValue Target = N->getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +00001366 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Roman Divackya4a59ae2011-06-03 15:47:49 +00001367 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
Hal Finkel528ff4b2011-12-08 04:36:44 +00001368 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
Dan Gohman32f71d72009-09-25 18:54:59 +00001369 Chain), 0);
Roman Divackya4a59ae2011-06-03 15:47:49 +00001370 return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001371 }
Bill Schmidt34627e32012-11-27 17:35:46 +00001372 case PPCISD::TOC_ENTRY: {
1373 assert (PPCSubTarget.isPPC64() && "Only supported for 64-bit ABI");
1374
Bill Schmidt27917782013-02-21 17:12:27 +00001375 // For medium and large code model, we generate two instructions as
1376 // described below. Otherwise we allow SelectCodeCommon to handle this,
1377 // selecting one of LDtoc, LDtocJTI, and LDtocCPT.
1378 CodeModel::Model CModel = TM.getCodeModel();
1379 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00001380 break;
1381
1382 // The first source operand is a TargetGlobalAddress or a
1383 // TargetJumpTable. If it is an externally defined symbol, a symbol
1384 // with common linkage, a function address, or a jump table address,
Bill Schmidt27917782013-02-21 17:12:27 +00001385 // or if we are generating code for large code model, we generate:
Bill Schmidt34627e32012-11-27 17:35:46 +00001386 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
1387 // Otherwise we generate:
1388 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
1389 SDValue GA = N->getOperand(0);
1390 SDValue TOCbase = N->getOperand(1);
1391 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
1392 TOCbase, GA);
1393
Bill Schmidt27917782013-02-21 17:12:27 +00001394 if (isa<JumpTableSDNode>(GA) || CModel == CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00001395 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1396 SDValue(Tmp, 0));
1397
1398 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
1399 const GlobalValue *GValue = G->getGlobal();
Bill Schmidt9b1e3e22013-01-07 19:29:18 +00001400 const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
1401 const GlobalValue *RealGValue = GAlias ?
1402 GAlias->resolveAliasedGlobal(false) : GValue;
1403 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
1404 assert((GVar || isa<Function>(RealGValue)) &&
Bill Schmidt34627e32012-11-27 17:35:46 +00001405 "Unexpected global value subclass!");
1406
1407 // An external variable is one without an initializer. For these,
1408 // for variables with common linkage, and for Functions, generate
1409 // the LDtocL form.
Bill Schmidt9b1e3e22013-01-07 19:29:18 +00001410 if (!GVar || !GVar->hasInitializer() || RealGValue->hasCommonLinkage() ||
1411 RealGValue->hasAvailableExternallyLinkage())
Bill Schmidt34627e32012-11-27 17:35:46 +00001412 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1413 SDValue(Tmp, 0));
1414 }
1415
1416 return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
1417 SDValue(Tmp, 0), GA);
1418 }
Bill Schmidt51e79512013-02-20 15:50:31 +00001419 case PPCISD::VADD_SPLAT: {
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001420 // This expands into one of three sequences, depending on whether
1421 // the first operand is odd or even, positive or negative.
Bill Schmidt51e79512013-02-20 15:50:31 +00001422 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
1423 isa<ConstantSDNode>(N->getOperand(1)) &&
1424 "Invalid operand on VADD_SPLAT!");
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001425
1426 int Elt = N->getConstantOperandVal(0);
Bill Schmidt51e79512013-02-20 15:50:31 +00001427 int EltSize = N->getConstantOperandVal(1);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001428 unsigned Opc1, Opc2, Opc3;
Bill Schmidt51e79512013-02-20 15:50:31 +00001429 EVT VT;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001430
Bill Schmidt51e79512013-02-20 15:50:31 +00001431 if (EltSize == 1) {
1432 Opc1 = PPC::VSPLTISB;
1433 Opc2 = PPC::VADDUBM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001434 Opc3 = PPC::VSUBUBM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001435 VT = MVT::v16i8;
1436 } else if (EltSize == 2) {
1437 Opc1 = PPC::VSPLTISH;
1438 Opc2 = PPC::VADDUHM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001439 Opc3 = PPC::VSUBUHM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001440 VT = MVT::v8i16;
1441 } else {
1442 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
1443 Opc1 = PPC::VSPLTISW;
1444 Opc2 = PPC::VADDUWM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001445 Opc3 = PPC::VSUBUWM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001446 VT = MVT::v4i32;
1447 }
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001448
1449 if ((Elt & 1) == 0) {
1450 // Elt is even, in the range [-32,-18] + [16,30].
1451 //
1452 // Convert: VADD_SPLAT elt, size
1453 // Into: tmp = VSPLTIS[BHW] elt
1454 // VADDU[BHW]M tmp, tmp
1455 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
1456 SDValue EltVal = getI32Imm(Elt >> 1);
1457 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1458 SDValue TmpVal = SDValue(Tmp, 0);
1459 return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal);
1460
1461 } else if (Elt > 0) {
1462 // Elt is odd and positive, in the range [17,31].
1463 //
1464 // Convert: VADD_SPLAT elt, size
1465 // Into: tmp1 = VSPLTIS[BHW] elt-16
1466 // tmp2 = VSPLTIS[BHW] -16
1467 // VSUBU[BHW]M tmp1, tmp2
1468 SDValue EltVal = getI32Imm(Elt - 16);
1469 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1470 EltVal = getI32Imm(-16);
1471 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1472 return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
1473 SDValue(Tmp2, 0));
1474
1475 } else {
1476 // Elt is odd and negative, in the range [-31,-17].
1477 //
1478 // Convert: VADD_SPLAT elt, size
1479 // Into: tmp1 = VSPLTIS[BHW] elt+16
1480 // tmp2 = VSPLTIS[BHW] -16
1481 // VADDU[BHW]M tmp1, tmp2
1482 SDValue EltVal = getI32Imm(Elt + 16);
1483 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1484 EltVal = getI32Imm(-16);
1485 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1486 return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
1487 SDValue(Tmp2, 0));
1488 }
Bill Schmidt51e79512013-02-20 15:50:31 +00001489 }
Chris Lattner43ff01e2005-08-17 19:33:03 +00001490 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001491
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001492 return SelectCode(N);
Chris Lattner43ff01e2005-08-17 19:33:03 +00001493}
1494
Hal Finkel860fa902014-01-02 22:09:39 +00001495/// PostprocessISelDAG - Perform some late peephole optimizations
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001496/// on the DAG representation.
1497void PPCDAGToDAGISel::PostprocessISelDAG() {
1498
1499 // Skip peepholes at -O0.
1500 if (TM.getOptLevel() == CodeGenOpt::None)
1501 return;
1502
Hal Finkel940ab932014-02-28 00:27:01 +00001503 PeepholePPC64();
1504 PeepholdCROps();
1505}
1506
1507void PPCDAGToDAGISel::PeepholdCROps() {
1508 bool IsModified;
1509 do {
1510 IsModified = false;
1511 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
1512 E = CurDAG->allnodes_end(); I != E; ++I) {
1513 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
1514 if (!MachineNode || MachineNode->use_empty())
1515 continue;
1516 SDNode *ResNode = MachineNode;
1517
1518 bool Op1Set = false, Op1Unset = false,
1519 Op1Not = false,
1520 Op2Set = false, Op2Unset = false,
1521 Op2Not = false;
1522
1523 unsigned Opcode = MachineNode->getMachineOpcode();
1524 switch (Opcode) {
1525 default: break;
1526 case PPC::CRAND:
1527 case PPC::CRNAND:
1528 case PPC::CROR:
1529 case PPC::CRXOR:
1530 case PPC::CRNOR:
1531 case PPC::CREQV:
1532 case PPC::CRANDC:
1533 case PPC::CRORC: {
1534 SDValue Op = MachineNode->getOperand(1);
1535 if (Op.isMachineOpcode()) {
1536 if (Op.getMachineOpcode() == PPC::CRSET)
1537 Op2Set = true;
1538 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1539 Op2Unset = true;
1540 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1541 Op.getOperand(0) == Op.getOperand(1))
1542 Op2Not = true;
1543 }
1544 } // fallthrough
1545 case PPC::BC:
1546 case PPC::BCn:
1547 case PPC::SELECT_I4:
1548 case PPC::SELECT_I8:
1549 case PPC::SELECT_F4:
1550 case PPC::SELECT_F8:
1551 case PPC::SELECT_VRRC: {
1552 SDValue Op = MachineNode->getOperand(0);
1553 if (Op.isMachineOpcode()) {
1554 if (Op.getMachineOpcode() == PPC::CRSET)
1555 Op1Set = true;
1556 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1557 Op1Unset = true;
1558 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1559 Op.getOperand(0) == Op.getOperand(1))
1560 Op1Not = true;
1561 }
1562 }
1563 break;
1564 }
1565
1566 switch (Opcode) {
1567 default: break;
1568 case PPC::CRAND:
1569 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1570 // x & x = x
1571 ResNode = MachineNode->getOperand(0).getNode();
1572 else if (Op1Set)
1573 // 1 & y = y
1574 ResNode = MachineNode->getOperand(1).getNode();
1575 else if (Op2Set)
1576 // x & 1 = x
1577 ResNode = MachineNode->getOperand(0).getNode();
1578 else if (Op1Unset || Op2Unset)
1579 // x & 0 = 0 & y = 0
1580 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1581 MVT::i1);
1582 else if (Op1Not)
1583 // ~x & y = andc(y, x)
1584 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1585 MVT::i1, MachineNode->getOperand(1),
1586 MachineNode->getOperand(0).
1587 getOperand(0));
1588 else if (Op2Not)
1589 // x & ~y = andc(x, y)
1590 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1591 MVT::i1, MachineNode->getOperand(0),
1592 MachineNode->getOperand(1).
1593 getOperand(0));
1594 break;
1595 case PPC::CRNAND:
1596 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1597 // nand(x, x) -> nor(x, x)
1598 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1599 MVT::i1, MachineNode->getOperand(0),
1600 MachineNode->getOperand(0));
1601 else if (Op1Set)
1602 // nand(1, y) -> nor(y, y)
1603 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1604 MVT::i1, MachineNode->getOperand(1),
1605 MachineNode->getOperand(1));
1606 else if (Op2Set)
1607 // nand(x, 1) -> nor(x, x)
1608 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1609 MVT::i1, MachineNode->getOperand(0),
1610 MachineNode->getOperand(0));
1611 else if (Op1Unset || Op2Unset)
1612 // nand(x, 0) = nand(0, y) = 1
1613 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1614 MVT::i1);
1615 else if (Op1Not)
1616 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y)
1617 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1618 MVT::i1, MachineNode->getOperand(0).
1619 getOperand(0),
1620 MachineNode->getOperand(1));
1621 else if (Op2Not)
1622 // nand(x, ~y) = ~x | y = orc(y, x)
1623 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1624 MVT::i1, MachineNode->getOperand(1).
1625 getOperand(0),
1626 MachineNode->getOperand(0));
1627 break;
1628 case PPC::CROR:
1629 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1630 // x | x = x
1631 ResNode = MachineNode->getOperand(0).getNode();
1632 else if (Op1Set || Op2Set)
1633 // x | 1 = 1 | y = 1
1634 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1635 MVT::i1);
1636 else if (Op1Unset)
1637 // 0 | y = y
1638 ResNode = MachineNode->getOperand(1).getNode();
1639 else if (Op2Unset)
1640 // x | 0 = x
1641 ResNode = MachineNode->getOperand(0).getNode();
1642 else if (Op1Not)
1643 // ~x | y = orc(y, x)
1644 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1645 MVT::i1, MachineNode->getOperand(1),
1646 MachineNode->getOperand(0).
1647 getOperand(0));
1648 else if (Op2Not)
1649 // x | ~y = orc(x, y)
1650 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1651 MVT::i1, MachineNode->getOperand(0),
1652 MachineNode->getOperand(1).
1653 getOperand(0));
1654 break;
1655 case PPC::CRXOR:
1656 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1657 // xor(x, x) = 0
1658 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1659 MVT::i1);
1660 else if (Op1Set)
1661 // xor(1, y) -> nor(y, y)
1662 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1663 MVT::i1, MachineNode->getOperand(1),
1664 MachineNode->getOperand(1));
1665 else if (Op2Set)
1666 // xor(x, 1) -> nor(x, x)
1667 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1668 MVT::i1, MachineNode->getOperand(0),
1669 MachineNode->getOperand(0));
1670 else if (Op1Unset)
1671 // xor(0, y) = y
1672 ResNode = MachineNode->getOperand(1).getNode();
1673 else if (Op2Unset)
1674 // xor(x, 0) = x
1675 ResNode = MachineNode->getOperand(0).getNode();
1676 else if (Op1Not)
1677 // xor(~x, y) = eqv(x, y)
1678 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1679 MVT::i1, MachineNode->getOperand(0).
1680 getOperand(0),
1681 MachineNode->getOperand(1));
1682 else if (Op2Not)
1683 // xor(x, ~y) = eqv(x, y)
1684 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1685 MVT::i1, MachineNode->getOperand(0),
1686 MachineNode->getOperand(1).
1687 getOperand(0));
1688 break;
1689 case PPC::CRNOR:
1690 if (Op1Set || Op2Set)
1691 // nor(1, y) -> 0
1692 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1693 MVT::i1);
1694 else if (Op1Unset)
1695 // nor(0, y) = ~y -> nor(y, y)
1696 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1697 MVT::i1, MachineNode->getOperand(1),
1698 MachineNode->getOperand(1));
1699 else if (Op2Unset)
1700 // nor(x, 0) = ~x
1701 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1702 MVT::i1, MachineNode->getOperand(0),
1703 MachineNode->getOperand(0));
1704 else if (Op1Not)
1705 // nor(~x, y) = andc(x, y)
1706 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1707 MVT::i1, MachineNode->getOperand(0).
1708 getOperand(0),
1709 MachineNode->getOperand(1));
1710 else if (Op2Not)
1711 // nor(x, ~y) = andc(y, x)
1712 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1713 MVT::i1, MachineNode->getOperand(1).
1714 getOperand(0),
1715 MachineNode->getOperand(0));
1716 break;
1717 case PPC::CREQV:
1718 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1719 // eqv(x, x) = 1
1720 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1721 MVT::i1);
1722 else if (Op1Set)
1723 // eqv(1, y) = y
1724 ResNode = MachineNode->getOperand(1).getNode();
1725 else if (Op2Set)
1726 // eqv(x, 1) = x
1727 ResNode = MachineNode->getOperand(0).getNode();
1728 else if (Op1Unset)
1729 // eqv(0, y) = ~y -> nor(y, y)
1730 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1731 MVT::i1, MachineNode->getOperand(1),
1732 MachineNode->getOperand(1));
1733 else if (Op2Unset)
1734 // eqv(x, 0) = ~x
1735 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1736 MVT::i1, MachineNode->getOperand(0),
1737 MachineNode->getOperand(0));
1738 else if (Op1Not)
1739 // eqv(~x, y) = xor(x, y)
1740 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1741 MVT::i1, MachineNode->getOperand(0).
1742 getOperand(0),
1743 MachineNode->getOperand(1));
1744 else if (Op2Not)
1745 // eqv(x, ~y) = xor(x, y)
1746 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1747 MVT::i1, MachineNode->getOperand(0),
1748 MachineNode->getOperand(1).
1749 getOperand(0));
1750 break;
1751 case PPC::CRANDC:
1752 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1753 // andc(x, x) = 0
1754 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1755 MVT::i1);
1756 else if (Op1Set)
1757 // andc(1, y) = ~y
1758 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1759 MVT::i1, MachineNode->getOperand(1),
1760 MachineNode->getOperand(1));
1761 else if (Op1Unset || Op2Set)
1762 // andc(0, y) = andc(x, 1) = 0
1763 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1764 MVT::i1);
1765 else if (Op2Unset)
1766 // andc(x, 0) = x
1767 ResNode = MachineNode->getOperand(0).getNode();
1768 else if (Op1Not)
1769 // andc(~x, y) = ~(x | y) = nor(x, y)
1770 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1771 MVT::i1, MachineNode->getOperand(0).
1772 getOperand(0),
1773 MachineNode->getOperand(1));
1774 else if (Op2Not)
1775 // andc(x, ~y) = x & y
1776 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
1777 MVT::i1, MachineNode->getOperand(0),
1778 MachineNode->getOperand(1).
1779 getOperand(0));
1780 break;
1781 case PPC::CRORC:
1782 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1783 // orc(x, x) = 1
1784 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1785 MVT::i1);
1786 else if (Op1Set || Op2Unset)
1787 // orc(1, y) = orc(x, 0) = 1
1788 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1789 MVT::i1);
1790 else if (Op2Set)
1791 // orc(x, 1) = x
1792 ResNode = MachineNode->getOperand(0).getNode();
1793 else if (Op1Unset)
1794 // orc(0, y) = ~y
1795 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1796 MVT::i1, MachineNode->getOperand(1),
1797 MachineNode->getOperand(1));
1798 else if (Op1Not)
1799 // orc(~x, y) = ~(x & y) = nand(x, y)
1800 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
1801 MVT::i1, MachineNode->getOperand(0).
1802 getOperand(0),
1803 MachineNode->getOperand(1));
1804 else if (Op2Not)
1805 // orc(x, ~y) = x | y
1806 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
1807 MVT::i1, MachineNode->getOperand(0),
1808 MachineNode->getOperand(1).
1809 getOperand(0));
1810 break;
1811 case PPC::SELECT_I4:
1812 case PPC::SELECT_I8:
1813 case PPC::SELECT_F4:
1814 case PPC::SELECT_F8:
1815 case PPC::SELECT_VRRC:
1816 if (Op1Set)
1817 ResNode = MachineNode->getOperand(1).getNode();
1818 else if (Op1Unset)
1819 ResNode = MachineNode->getOperand(2).getNode();
1820 else if (Op1Not)
1821 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(),
1822 SDLoc(MachineNode),
1823 MachineNode->getValueType(0),
1824 MachineNode->getOperand(0).
1825 getOperand(0),
1826 MachineNode->getOperand(2),
1827 MachineNode->getOperand(1));
1828 break;
1829 case PPC::BC:
1830 case PPC::BCn:
1831 if (Op1Not)
1832 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn :
1833 PPC::BC,
1834 SDLoc(MachineNode),
1835 MVT::Other,
1836 MachineNode->getOperand(0).
1837 getOperand(0),
1838 MachineNode->getOperand(1),
1839 MachineNode->getOperand(2));
1840 // FIXME: Handle Op1Set, Op1Unset here too.
1841 break;
1842 }
1843
1844 if (ResNode != MachineNode) {
1845 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
1846 DEBUG(MachineNode->dump(CurDAG));
1847 DEBUG(dbgs() << "\nNew: ");
1848 DEBUG(ResNode->dump(CurDAG));
1849 DEBUG(dbgs() << "\n");
1850
1851 ReplaceUses(MachineNode, ResNode);
1852 IsModified = true;
1853 }
1854 }
1855 if (IsModified)
1856 CurDAG->RemoveDeadNodes();
1857 } while (IsModified);
1858}
1859
1860void PPCDAGToDAGISel::PeepholePPC64() {
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001861 // These optimizations are currently supported only for 64-bit SVR4.
1862 if (PPCSubTarget.isDarwin() || !PPCSubTarget.isPPC64())
1863 return;
1864
1865 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
1866 ++Position;
1867
1868 while (Position != CurDAG->allnodes_begin()) {
1869 SDNode *N = --Position;
1870 // Skip dead nodes and any non-machine opcodes.
1871 if (N->use_empty() || !N->isMachineOpcode())
1872 continue;
1873
1874 unsigned FirstOp;
1875 unsigned StorageOpcode = N->getMachineOpcode();
1876
1877 switch (StorageOpcode) {
1878 default: continue;
1879
1880 case PPC::LBZ:
1881 case PPC::LBZ8:
1882 case PPC::LD:
1883 case PPC::LFD:
1884 case PPC::LFS:
1885 case PPC::LHA:
1886 case PPC::LHA8:
1887 case PPC::LHZ:
1888 case PPC::LHZ8:
1889 case PPC::LWA:
1890 case PPC::LWZ:
1891 case PPC::LWZ8:
1892 FirstOp = 0;
1893 break;
1894
1895 case PPC::STB:
1896 case PPC::STB8:
1897 case PPC::STD:
1898 case PPC::STFD:
1899 case PPC::STFS:
1900 case PPC::STH:
1901 case PPC::STH8:
1902 case PPC::STW:
1903 case PPC::STW8:
1904 FirstOp = 1;
1905 break;
1906 }
1907
1908 // If this is a load or store with a zero offset, we may be able to
1909 // fold an add-immediate into the memory operation.
1910 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) ||
1911 N->getConstantOperandVal(FirstOp) != 0)
1912 continue;
1913
1914 SDValue Base = N->getOperand(FirstOp + 1);
1915 if (!Base.isMachineOpcode())
1916 continue;
1917
1918 unsigned Flags = 0;
1919 bool ReplaceFlags = true;
1920
1921 // When the feeding operation is an add-immediate of some sort,
1922 // determine whether we need to add relocation information to the
1923 // target flags on the immediate operand when we fold it into the
1924 // load instruction.
1925 //
1926 // For something like ADDItocL, the relocation information is
1927 // inferred from the opcode; when we process it in the AsmPrinter,
1928 // we add the necessary relocation there. A load, though, can receive
1929 // relocation from various flavors of ADDIxxx, so we need to carry
1930 // the relocation information in the target flags.
1931 switch (Base.getMachineOpcode()) {
1932 default: continue;
1933
1934 case PPC::ADDI8:
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +00001935 case PPC::ADDI:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001936 // In some cases (such as TLS) the relocation information
1937 // is already in place on the operand, so copying the operand
1938 // is sufficient.
1939 ReplaceFlags = false;
1940 // For these cases, the immediate may not be divisible by 4, in
1941 // which case the fold is illegal for DS-form instructions. (The
1942 // other cases provide aligned addresses and are always safe.)
1943 if ((StorageOpcode == PPC::LWA ||
1944 StorageOpcode == PPC::LD ||
1945 StorageOpcode == PPC::STD) &&
1946 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
1947 Base.getConstantOperandVal(1) % 4 != 0))
1948 continue;
1949 break;
1950 case PPC::ADDIdtprelL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001951 Flags = PPCII::MO_DTPREL_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001952 break;
1953 case PPC::ADDItlsldL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001954 Flags = PPCII::MO_TLSLD_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001955 break;
1956 case PPC::ADDItocL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001957 Flags = PPCII::MO_TOC_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001958 break;
1959 }
1960
1961 // We found an opportunity. Reverse the operands from the add
1962 // immediate and substitute them into the load or store. If
1963 // needed, update the target flags for the immediate operand to
1964 // reflect the necessary relocation information.
1965 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
1966 DEBUG(Base->dump(CurDAG));
1967 DEBUG(dbgs() << "\nN: ");
1968 DEBUG(N->dump(CurDAG));
1969 DEBUG(dbgs() << "\n");
1970
1971 SDValue ImmOpnd = Base.getOperand(1);
1972
1973 // If the relocation information isn't already present on the
1974 // immediate operand, add it now.
1975 if (ReplaceFlags) {
Bill Schmidt49498da2013-02-21 14:35:42 +00001976 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001977 SDLoc dl(GA);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001978 const GlobalValue *GV = GA->getGlobal();
Bill Schmidt48fc20a2013-07-01 20:52:27 +00001979 // We can't perform this optimization for data whose alignment
1980 // is insufficient for the instruction encoding.
1981 if (GV->getAlignment() < 4 &&
1982 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
1983 StorageOpcode == PPC::LWA)) {
1984 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
1985 continue;
1986 }
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001987 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags);
Bill Schmidt836c45b2013-02-21 17:26:05 +00001988 } else if (ConstantPoolSDNode *CP =
1989 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
Bill Schmidt49498da2013-02-21 14:35:42 +00001990 const Constant *C = CP->getConstVal();
1991 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
1992 CP->getAlignment(),
1993 0, Flags);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001994 }
1995 }
1996
1997 if (FirstOp == 1) // Store
1998 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
1999 Base.getOperand(0), N->getOperand(3));
2000 else // Load
2001 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
2002 N->getOperand(2));
2003
2004 // The add-immediate may now be dead, in which case remove it.
2005 if (Base.getNode()->use_empty())
2006 CurDAG->RemoveDeadNode(Base.getNode());
2007 }
2008}
Chris Lattner43ff01e2005-08-17 19:33:03 +00002009
Chris Lattnerb055c872006-06-10 01:15:02 +00002010
Andrew Trickc416ba62010-12-24 04:28:06 +00002011/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattner43ff01e2005-08-17 19:33:03 +00002012/// PowerPC-specific DAG, ready for instruction scheduling.
2013///
Evan Cheng2dd2c652006-03-13 23:20:37 +00002014FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman0b71e002005-10-18 00:28:58 +00002015 return new PPCDAGToDAGISel(TM);
Chris Lattner43ff01e2005-08-17 19:33:03 +00002016}
2017
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00002018static void initializePassOnce(PassRegistry &Registry) {
2019 const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection";
2020 PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID, 0,
2021 false, false);
2022 Registry.registerPass(*PI, true);
2023}
2024
2025void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) {
2026 CALL_ONCE_INITIALIZATION(initializePassOnce);
2027}
2028