Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1 | //===-- PPC32ISelDAGToDAG.cpp - PPC32 pattern matching inst selector ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a pattern matching instruction selector for 32 bit PowerPC, |
| 11 | // converting from a legalized dag to a PPC dag. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "PowerPC.h" |
| 16 | #include "PPC32TargetMachine.h" |
| 17 | #include "PPC32ISelLowering.h" |
| 18 | #include "llvm/CodeGen/SelectionDAG.h" |
| 19 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 20 | #include "llvm/Target/TargetOptions.h" |
| 21 | #include "llvm/ADT/Statistic.h" |
| 22 | #include "llvm/Support/Debug.h" |
| 23 | #include "llvm/Support/MathExtras.h" |
| 24 | using namespace llvm; |
| 25 | |
| 26 | namespace { |
| 27 | Statistic<> Recorded("ppc-codegen", "Number of recording ops emitted"); |
| 28 | Statistic<> FusedFP ("ppc-codegen", "Number of fused fp operations"); |
| 29 | Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed"); |
| 30 | |
| 31 | //===--------------------------------------------------------------------===// |
| 32 | /// PPC32DAGToDAGISel - PPC32 specific code to select PPC32 machine |
| 33 | /// instructions for SelectionDAG operations. |
| 34 | /// |
| 35 | class PPC32DAGToDAGISel : public SelectionDAGISel { |
| 36 | PPC32TargetLowering PPC32Lowering; |
| 37 | |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 38 | public: |
| 39 | PPC32DAGToDAGISel(TargetMachine &TM) |
| 40 | : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM) {} |
| 41 | |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 42 | /// getI32Imm - Return a target constant with the specified value, of type |
| 43 | /// i32. |
| 44 | inline SDOperand getI32Imm(unsigned Imm) { |
| 45 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
| 46 | } |
| 47 | |
| 48 | // Select - Convert the specified operand from a target-independent to a |
| 49 | // target-specific node if it hasn't already been changed. |
| 50 | SDOperand Select(SDOperand Op); |
| 51 | |
| 52 | SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS, |
| 53 | unsigned OCHi, unsigned OCLo, |
| 54 | bool IsArithmetic = false, |
| 55 | bool Negate = false); |
| 56 | |
| 57 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 58 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 59 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 60 | DEBUG(BB->dump()); |
Chris Lattner | d607c12 | 2005-08-18 18:46:06 +0000 | [diff] [blame^] | 61 | // Select target instructions for the DAG. |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 62 | Select(DAG.getRoot()); |
| 63 | DAG.RemoveDeadNodes(); |
Chris Lattner | d607c12 | 2005-08-18 18:46:06 +0000 | [diff] [blame^] | 64 | |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 65 | DAG.viewGraph(); |
Chris Lattner | d607c12 | 2005-08-18 18:46:06 +0000 | [diff] [blame^] | 66 | |
| 67 | // Emit machine code to BB. |
| 68 | ScheduleAndEmitDAG(DAG); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | virtual const char *getPassName() const { |
| 72 | return "PowerPC DAG->DAG Pattern Instruction Selection"; |
| 73 | } |
| 74 | }; |
| 75 | } |
| 76 | |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 77 | // isIntImmediate - This method tests to see if a constant operand. |
| 78 | // If so Imm will receive the 32 bit value. |
| 79 | static bool isIntImmediate(SDNode *N, unsigned& Imm) { |
| 80 | if (N->getOpcode() == ISD::Constant) { |
| 81 | Imm = cast<ConstantSDNode>(N)->getValue(); |
| 82 | return true; |
| 83 | } |
| 84 | return false; |
| 85 | } |
| 86 | |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 87 | // isOprShiftImm - Returns true if the specified operand is a shift opcode with |
| 88 | // a immediate shift count less than 32. |
| 89 | static bool isOprShiftImm(SDNode *N, unsigned& Opc, unsigned& SH) { |
| 90 | Opc = N->getOpcode(); |
| 91 | return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) && |
| 92 | isIntImmediate(N->getOperand(1).Val, SH) && SH < 32; |
| 93 | } |
| 94 | |
| 95 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 96 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 97 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 98 | // not, since all 1s are not contiguous. |
| 99 | static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) { |
| 100 | if (isShiftedMask_32(Val)) { |
| 101 | // look for the first non-zero bit |
| 102 | MB = CountLeadingZeros_32(Val); |
| 103 | // look for the first zero bit after the run of ones |
| 104 | ME = CountLeadingZeros_32((Val - 1) ^ Val); |
| 105 | return true; |
| 106 | } else if (isShiftedMask_32(Val = ~Val)) { // invert mask |
| 107 | // effectively look for the first zero bit |
| 108 | ME = CountLeadingZeros_32(Val) - 1; |
| 109 | // effectively look for the first one bit after the run of zeros |
| 110 | MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1; |
| 111 | return true; |
| 112 | } |
| 113 | // no run present |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | // isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate |
| 118 | // and mask opcode and mask operation. |
| 119 | static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask, |
| 120 | unsigned &SH, unsigned &MB, unsigned &ME) { |
| 121 | unsigned Shift = 32; |
| 122 | unsigned Indeterminant = ~0; // bit mask marking indeterminant results |
| 123 | unsigned Opcode = N->getOpcode(); |
| 124 | if (!isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31)) |
| 125 | return false; |
| 126 | |
| 127 | if (Opcode == ISD::SHL) { |
| 128 | // apply shift left to mask if it comes first |
| 129 | if (IsShiftMask) Mask = Mask << Shift; |
| 130 | // determine which bits are made indeterminant by shift |
| 131 | Indeterminant = ~(0xFFFFFFFFu << Shift); |
| 132 | } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) { |
| 133 | // apply shift right to mask if it comes first |
| 134 | if (IsShiftMask) Mask = Mask >> Shift; |
| 135 | // determine which bits are made indeterminant by shift |
| 136 | Indeterminant = ~(0xFFFFFFFFu >> Shift); |
| 137 | // adjust for the left rotate |
| 138 | Shift = 32 - Shift; |
| 139 | } else { |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | // if the mask doesn't intersect any Indeterminant bits |
| 144 | if (Mask && !(Mask & Indeterminant)) { |
| 145 | SH = Shift; |
| 146 | // make sure the mask is still a mask (wrap arounds may not be) |
| 147 | return isRunOfOnes(Mask, MB, ME); |
| 148 | } |
| 149 | return false; |
| 150 | } |
| 151 | |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 152 | // isOpcWithIntImmediate - This method tests to see if the node is a specific |
| 153 | // opcode and that it has a immediate integer right operand. |
| 154 | // If so Imm will receive the 32 bit value. |
| 155 | static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) { |
| 156 | return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm); |
| 157 | } |
| 158 | |
| 159 | // isOprNot - Returns true if the specified operand is an xor with immediate -1. |
| 160 | static bool isOprNot(SDNode *N) { |
| 161 | unsigned Imm; |
| 162 | return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1; |
| 163 | } |
| 164 | |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 165 | // Immediate constant composers. |
| 166 | // Lo16 - grabs the lo 16 bits from a 32 bit constant. |
| 167 | // Hi16 - grabs the hi 16 bits from a 32 bit constant. |
| 168 | // HA16 - computes the hi bits required if the lo bits are add/subtracted in |
| 169 | // arithmethically. |
| 170 | static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; } |
| 171 | static unsigned Hi16(unsigned x) { return Lo16(x >> 16); } |
| 172 | static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); } |
| 173 | |
| 174 | // isIntImmediate - This method tests to see if a constant operand. |
| 175 | // If so Imm will receive the 32 bit value. |
| 176 | static bool isIntImmediate(SDOperand N, unsigned& Imm) { |
| 177 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) { |
| 178 | Imm = (unsigned)CN->getSignExtended(); |
| 179 | return true; |
| 180 | } |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | // SelectIntImmediateExpr - Choose code for integer operations with an immediate |
| 185 | // operand. |
| 186 | SDNode *PPC32DAGToDAGISel::SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS, |
| 187 | unsigned OCHi, unsigned OCLo, |
| 188 | bool IsArithmetic, |
| 189 | bool Negate) { |
| 190 | // Check to make sure this is a constant. |
| 191 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS); |
| 192 | // Exit if not a constant. |
| 193 | if (!CN) return 0; |
| 194 | // Extract immediate. |
| 195 | unsigned C = (unsigned)CN->getValue(); |
| 196 | // Negate if required (ISD::SUB). |
| 197 | if (Negate) C = -C; |
| 198 | // Get the hi and lo portions of constant. |
| 199 | unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C); |
| 200 | unsigned Lo = Lo16(C); |
| 201 | |
| 202 | // If two instructions are needed and usage indicates it would be better to |
| 203 | // load immediate into a register, bail out. |
| 204 | if (Hi && Lo && CN->use_size() > 2) return false; |
| 205 | |
| 206 | // Select the first operand. |
| 207 | SDOperand Opr0 = Select(LHS); |
| 208 | |
| 209 | if (Lo) // Add in the lo-part. |
| 210 | Opr0 = CurDAG->getTargetNode(OCLo, MVT::i32, Opr0, getI32Imm(Lo)); |
| 211 | if (Hi) // Add in the hi-part. |
| 212 | Opr0 = CurDAG->getTargetNode(OCHi, MVT::i32, Opr0, getI32Imm(Hi)); |
| 213 | return Opr0.Val; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | // Select - Convert the specified operand from a target-independent to a |
| 218 | // target-specific node if it hasn't already been changed. |
| 219 | SDOperand PPC32DAGToDAGISel::Select(SDOperand Op) { |
| 220 | SDNode *N = Op.Val; |
| 221 | if (N->getOpcode() >= ISD::BUILTIN_OP_END) |
| 222 | return Op; // Already selected. |
| 223 | |
| 224 | switch (N->getOpcode()) { |
| 225 | default: |
| 226 | std::cerr << "Cannot yet select: "; |
| 227 | N->dump(); |
| 228 | std::cerr << "\n"; |
| 229 | abort(); |
| 230 | case ISD::EntryToken: // These leaves remain the same. |
| 231 | case ISD::UNDEF: |
| 232 | return Op; |
| 233 | case ISD::TokenFactor: { |
| 234 | SDOperand New; |
| 235 | if (N->getNumOperands() == 2) { |
| 236 | SDOperand Op0 = Select(N->getOperand(0)); |
| 237 | SDOperand Op1 = Select(N->getOperand(1)); |
| 238 | New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1); |
| 239 | } else { |
| 240 | std::vector<SDOperand> Ops; |
| 241 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 242 | Ops.push_back(Select(N->getOperand(0))); |
| 243 | New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops); |
| 244 | } |
| 245 | |
| 246 | if (New.Val != N) { |
| 247 | CurDAG->ReplaceAllUsesWith(N, New.Val); |
| 248 | N = New.Val; |
| 249 | } |
| 250 | break; |
| 251 | } |
| 252 | case ISD::CopyFromReg: { |
| 253 | SDOperand Chain = Select(N->getOperand(0)); |
| 254 | if (Chain == N->getOperand(0)) return Op; // No change |
| 255 | SDOperand New = CurDAG->getCopyFromReg(Chain, |
| 256 | cast<RegisterSDNode>(N->getOperand(1))->getReg(), N->getValueType(0)); |
| 257 | return New.getValue(Op.ResNo); |
| 258 | } |
| 259 | case ISD::CopyToReg: { |
| 260 | SDOperand Chain = Select(N->getOperand(0)); |
| 261 | SDOperand Reg = N->getOperand(1); |
| 262 | SDOperand Val = Select(N->getOperand(2)); |
| 263 | if (Chain != N->getOperand(0) || Val != N->getOperand(2)) { |
| 264 | SDOperand New = CurDAG->getNode(ISD::CopyToReg, MVT::Other, |
| 265 | Chain, Reg, Val); |
| 266 | CurDAG->ReplaceAllUsesWith(N, New.Val); |
| 267 | N = New.Val; |
| 268 | } |
| 269 | break; |
| 270 | } |
| 271 | case ISD::Constant: { |
| 272 | assert(N->getValueType(0) == MVT::i32); |
| 273 | unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue(); |
Nate Begeman | a694047 | 2005-08-18 18:01:39 +0000 | [diff] [blame] | 274 | unsigned Hi = HA16(v); |
| 275 | unsigned Lo = Lo16(v); |
| 276 | if (Hi && Lo) { |
| 277 | SDOperand Top = CurDAG->getTargetNode(PPC::LIS, MVT::i32, |
| 278 | getI32Imm(v >> 16)); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 279 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORI, Top, getI32Imm(v & 0xFFFF)); |
Nate Begeman | a694047 | 2005-08-18 18:01:39 +0000 | [diff] [blame] | 280 | } else if (Lo) { |
| 281 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::LI, getI32Imm(v)); |
| 282 | } else { |
| 283 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::LIS, getI32Imm(v >> 16)); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 284 | } |
Nate Begeman | a694047 | 2005-08-18 18:01:39 +0000 | [diff] [blame] | 285 | break; |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 286 | } |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 287 | case ISD::SIGN_EXTEND_INREG: |
| 288 | switch(cast<VTSDNode>(N->getOperand(1))->getVT()) { |
| 289 | default: assert(0 && "Illegal type in SIGN_EXTEND_INREG"); break; |
| 290 | case MVT::i16: |
| 291 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::EXTSH, Select(N->getOperand(0))); |
| 292 | break; |
| 293 | case MVT::i8: |
| 294 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::EXTSB, Select(N->getOperand(0))); |
| 295 | break; |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 296 | } |
| 297 | break; |
| 298 | case ISD::CTLZ: |
| 299 | assert(N->getValueType(0) == MVT::i32); |
| 300 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::CNTLZW, Select(N->getOperand(0))); |
| 301 | break; |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 302 | case ISD::ADD: { |
| 303 | MVT::ValueType Ty = N->getValueType(0); |
| 304 | if (Ty == MVT::i32) { |
| 305 | if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1), |
| 306 | PPC::ADDIS, PPC::ADDI, true)) { |
| 307 | CurDAG->ReplaceAllUsesWith(N, I); |
| 308 | N = I; |
| 309 | } else { |
| 310 | CurDAG->SelectNodeTo(N, Ty, PPC::ADD, Select(N->getOperand(0)), |
| 311 | Select(N->getOperand(1))); |
| 312 | } |
| 313 | break; |
| 314 | } |
| 315 | |
| 316 | if (!NoExcessFPPrecision) { // Match FMA ops |
| 317 | if (N->getOperand(0).getOpcode() == ISD::MUL && |
| 318 | N->getOperand(0).Val->hasOneUse()) { |
| 319 | ++FusedFP; // Statistic |
| 320 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, |
| 321 | Select(N->getOperand(0).getOperand(0)), |
| 322 | Select(N->getOperand(0).getOperand(1)), |
| 323 | Select(N->getOperand(1))); |
| 324 | break; |
| 325 | } else if (N->getOperand(1).getOpcode() == ISD::MUL && |
| 326 | N->getOperand(1).hasOneUse()) { |
| 327 | ++FusedFP; // Statistic |
| 328 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, |
| 329 | Select(N->getOperand(1).getOperand(0)), |
| 330 | Select(N->getOperand(1).getOperand(1)), |
| 331 | Select(N->getOperand(0))); |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FADD : PPC::FADDS, |
| 337 | Select(N->getOperand(0)), Select(N->getOperand(1))); |
| 338 | break; |
| 339 | } |
| 340 | case ISD::SUB: { |
| 341 | MVT::ValueType Ty = N->getValueType(0); |
| 342 | if (Ty == MVT::i32) { |
| 343 | unsigned Imm; |
| 344 | if (isIntImmediate(N->getOperand(0), Imm) && isInt16(Imm)) { |
| 345 | CurDAG->SelectNodeTo(N, Ty, PPC::SUBFIC, Select(N->getOperand(1)), |
| 346 | getI32Imm(Lo16(Imm))); |
| 347 | break; |
| 348 | } |
| 349 | if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1), |
| 350 | PPC::ADDIS, PPC::ADDI, true, true)) { |
| 351 | CurDAG->ReplaceAllUsesWith(N, I); |
| 352 | N = I; |
| 353 | } else { |
| 354 | CurDAG->SelectNodeTo(N, Ty, PPC::SUBF, Select(N->getOperand(1)), |
| 355 | Select(N->getOperand(0))); |
| 356 | } |
| 357 | break; |
| 358 | } |
| 359 | |
| 360 | if (!NoExcessFPPrecision) { // Match FMA ops |
| 361 | if (N->getOperand(0).getOpcode() == ISD::MUL && |
| 362 | N->getOperand(0).Val->hasOneUse()) { |
| 363 | ++FusedFP; // Statistic |
| 364 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS, |
| 365 | Select(N->getOperand(0).getOperand(0)), |
| 366 | Select(N->getOperand(0).getOperand(1)), |
| 367 | Select(N->getOperand(1))); |
| 368 | break; |
| 369 | } else if (N->getOperand(1).getOpcode() == ISD::MUL && |
| 370 | N->getOperand(1).Val->hasOneUse()) { |
| 371 | ++FusedFP; // Statistic |
| 372 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS, |
| 373 | Select(N->getOperand(1).getOperand(0)), |
| 374 | Select(N->getOperand(1).getOperand(1)), |
| 375 | Select(N->getOperand(0))); |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSUB : PPC::FSUBS, |
| 380 | Select(N->getOperand(0)), |
| 381 | Select(N->getOperand(1))); |
| 382 | break; |
Nate Begeman | 2665350 | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 383 | } |
Nate Begeman | b5a0668 | 2005-08-18 00:21:41 +0000 | [diff] [blame] | 384 | case ISD::MUL: { |
| 385 | unsigned Imm, Opc; |
| 386 | if (isIntImmediate(N->getOperand(1), Imm) && isInt16(Imm)) { |
| 387 | CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::MULLI, |
| 388 | Select(N->getOperand(0)), getI32Imm(Lo16(Imm))); |
| 389 | break; |
| 390 | } |
| 391 | switch (N->getValueType(0)) { |
| 392 | default: assert(0 && "Unhandled multiply type!"); |
| 393 | case MVT::i32: Opc = PPC::MULLW; break; |
| 394 | case MVT::f32: Opc = PPC::FMULS; break; |
| 395 | case MVT::f64: Opc = PPC::FMUL; break; |
| 396 | } |
| 397 | CurDAG->SelectNodeTo(N, N->getValueType(0), Opc, Select(N->getOperand(0)), |
| 398 | Select(N->getOperand(1))); |
| 399 | break; |
| 400 | } |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 401 | case ISD::MULHS: |
Nate Begeman | b5a0668 | 2005-08-18 00:21:41 +0000 | [diff] [blame] | 402 | assert(N->getValueType(0) == MVT::i32); |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 403 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::MULHW, Select(N->getOperand(0)), |
| 404 | Select(N->getOperand(1))); |
Nate Begeman | b5a0668 | 2005-08-18 00:21:41 +0000 | [diff] [blame] | 405 | break; |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 406 | case ISD::MULHU: |
Nate Begeman | b5a0668 | 2005-08-18 00:21:41 +0000 | [diff] [blame] | 407 | assert(N->getValueType(0) == MVT::i32); |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 408 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::MULHWU, Select(N->getOperand(0)), |
| 409 | Select(N->getOperand(1))); |
Nate Begeman | b5a0668 | 2005-08-18 00:21:41 +0000 | [diff] [blame] | 410 | break; |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 411 | case ISD::AND: { |
Nate Begeman | a694047 | 2005-08-18 18:01:39 +0000 | [diff] [blame] | 412 | unsigned Imm; |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 413 | // If this is an and of a value rotated between 0 and 31 bits and then and'd |
| 414 | // with a mask, emit rlwinm |
| 415 | if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) || |
| 416 | isShiftedMask_32(~Imm))) { |
| 417 | SDOperand Val; |
Nate Begeman | a694047 | 2005-08-18 18:01:39 +0000 | [diff] [blame] | 418 | unsigned SH, MB, ME; |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 419 | if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) { |
| 420 | Val = Select(N->getOperand(0).getOperand(0)); |
| 421 | } else { |
| 422 | Val = Select(N->getOperand(0)); |
| 423 | isRunOfOnes(Imm, MB, ME); |
| 424 | SH = 0; |
| 425 | } |
| 426 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, Val, getI32Imm(SH), |
| 427 | getI32Imm(MB), getI32Imm(ME)); |
| 428 | break; |
| 429 | } |
| 430 | // If this is an and with an immediate that isn't a mask, then codegen it as |
| 431 | // high and low 16 bit immediate ands. |
| 432 | if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), |
| 433 | N->getOperand(1), |
| 434 | PPC::ANDISo, PPC::ANDIo)) { |
| 435 | CurDAG->ReplaceAllUsesWith(N, I); |
| 436 | N = I; |
| 437 | break; |
| 438 | } |
| 439 | // Finally, check for the case where we are being asked to select |
| 440 | // and (not(a), b) or and (a, not(b)) which can be selected as andc. |
| 441 | if (isOprNot(N->getOperand(0).Val)) |
| 442 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::ANDC, Select(N->getOperand(1)), |
| 443 | Select(N->getOperand(0).getOperand(0))); |
| 444 | else if (isOprNot(N->getOperand(1).Val)) |
| 445 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::ANDC, Select(N->getOperand(0)), |
| 446 | Select(N->getOperand(1).getOperand(0))); |
| 447 | else |
| 448 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::AND, Select(N->getOperand(0)), |
| 449 | Select(N->getOperand(1))); |
| 450 | break; |
| 451 | } |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 452 | case ISD::XOR: |
| 453 | // Check whether or not this node is a logical 'not'. This is represented |
| 454 | // by llvm as a xor with the constant value -1 (all bits set). If this is a |
| 455 | // 'not', then fold 'or' into 'nor', and so forth for the supported ops. |
| 456 | if (isOprNot(N)) { |
| 457 | unsigned Opc; |
Nate Begeman | 131a880 | 2005-08-18 05:44:50 +0000 | [diff] [blame] | 458 | SDOperand Val = Select(N->getOperand(0)); |
| 459 | switch (Val.getTargetOpcode()) { |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 460 | default: Opc = 0; break; |
Nate Begeman | 131a880 | 2005-08-18 05:44:50 +0000 | [diff] [blame] | 461 | case PPC::OR: Opc = PPC::NOR; break; |
| 462 | case PPC::AND: Opc = PPC::NAND; break; |
| 463 | case PPC::XOR: Opc = PPC::EQV; break; |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 464 | } |
| 465 | if (Opc) |
Nate Begeman | 131a880 | 2005-08-18 05:44:50 +0000 | [diff] [blame] | 466 | CurDAG->SelectNodeTo(N, MVT::i32, Opc, Val.getOperand(0), |
| 467 | Val.getOperand(1)); |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 468 | else |
Nate Begeman | 131a880 | 2005-08-18 05:44:50 +0000 | [diff] [blame] | 469 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::NOR, Val, Val); |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 470 | break; |
| 471 | } |
| 472 | // If this is a xor with an immediate other than -1, then codegen it as high |
| 473 | // and low 16 bit immediate xors. |
| 474 | if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), |
| 475 | N->getOperand(1), |
| 476 | PPC::XORIS, PPC::XORI)) { |
| 477 | CurDAG->ReplaceAllUsesWith(N, I); |
| 478 | N = I; |
| 479 | break; |
| 480 | } |
| 481 | // Finally, check for the case where we are being asked to select |
| 482 | // xor (not(a), b) which is equivalent to not(xor a, b), which is eqv |
| 483 | if (isOprNot(N->getOperand(0).Val)) |
| 484 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::EQV, |
| 485 | Select(N->getOperand(0).getOperand(0)), |
| 486 | Select(N->getOperand(1))); |
| 487 | else |
| 488 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::XOR, Select(N->getOperand(0)), |
| 489 | Select(N->getOperand(1))); |
| 490 | break; |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 491 | case ISD::FABS: |
Nate Begeman | 6a7d611 | 2005-08-18 00:53:47 +0000 | [diff] [blame] | 492 | CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::FABS, |
| 493 | Select(N->getOperand(0))); |
| 494 | break; |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 495 | case ISD::FP_EXTEND: |
| 496 | assert(MVT::f64 == N->getValueType(0) && |
| 497 | MVT::f32 == N->getOperand(0).getValueType() && "Illegal FP_EXTEND"); |
| 498 | CurDAG->SelectNodeTo(N, MVT::f64, PPC::FMR, Select(N->getOperand(0))); |
| 499 | break; |
| 500 | case ISD::FP_ROUND: |
| 501 | assert(MVT::f32 == N->getValueType(0) && |
| 502 | MVT::f64 == N->getOperand(0).getValueType() && "Illegal FP_ROUND"); |
| 503 | CurDAG->SelectNodeTo(N, MVT::f32, PPC::FRSP, Select(N->getOperand(0))); |
| 504 | break; |
Nate Begeman | 2665350 | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 505 | case ISD::FNEG: { |
| 506 | SDOperand Val = Select(N->getOperand(0)); |
| 507 | MVT::ValueType Ty = N->getValueType(0); |
| 508 | if (Val.Val->hasOneUse()) { |
| 509 | unsigned Opc; |
| 510 | switch (Val.getTargetOpcode()) { |
| 511 | default: Opc = 0; break; |
| 512 | case PPC::FABS: Opc = PPC::FNABS; break; |
| 513 | case PPC::FMADD: Opc = PPC::FNMADD; break; |
| 514 | case PPC::FMADDS: Opc = PPC::FNMADDS; break; |
| 515 | case PPC::FMSUB: Opc = PPC::FNMSUB; break; |
| 516 | case PPC::FMSUBS: Opc = PPC::FNMSUBS; break; |
| 517 | } |
| 518 | // If we inverted the opcode, then emit the new instruction with the |
| 519 | // inverted opcode and the original instruction's operands. Otherwise, |
| 520 | // fall through and generate a fneg instruction. |
| 521 | if (Opc) { |
| 522 | if (PPC::FNABS == Opc) |
| 523 | CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0)); |
| 524 | else |
| 525 | CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0), |
| 526 | Val.getOperand(1), Val.getOperand(2)); |
| 527 | break; |
| 528 | } |
| 529 | } |
| 530 | CurDAG->SelectNodeTo(N, Ty, PPC::FNEG, Val); |
| 531 | break; |
| 532 | } |
Nate Begeman | 6a7d611 | 2005-08-18 00:53:47 +0000 | [diff] [blame] | 533 | case ISD::FSQRT: { |
| 534 | MVT::ValueType Ty = N->getValueType(0); |
| 535 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS, |
| 536 | Select(N->getOperand(0))); |
| 537 | break; |
| 538 | } |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 539 | case ISD::RET: { |
| 540 | SDOperand Chain = Select(N->getOperand(0)); // Token chain. |
| 541 | |
| 542 | if (N->getNumOperands() > 1) { |
| 543 | SDOperand Val = Select(N->getOperand(1)); |
| 544 | switch (N->getOperand(1).getValueType()) { |
| 545 | default: assert(0 && "Unknown return type!"); |
| 546 | case MVT::f64: |
| 547 | case MVT::f32: |
| 548 | Chain = CurDAG->getCopyToReg(Chain, PPC::F1, Val); |
| 549 | break; |
| 550 | case MVT::i32: |
| 551 | Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Val); |
| 552 | break; |
| 553 | } |
| 554 | |
| 555 | if (N->getNumOperands() > 2) { |
| 556 | assert(N->getOperand(1).getValueType() == MVT::i32 && |
| 557 | N->getOperand(2).getValueType() == MVT::i32 && |
| 558 | N->getNumOperands() == 2 && "Unknown two-register ret value!"); |
| 559 | Val = Select(N->getOperand(2)); |
| 560 | Chain = CurDAG->getCopyToReg(Chain, PPC::R4, Val); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | // Finally, select this to a blr (return) instruction. |
| 565 | CurDAG->SelectNodeTo(N, MVT::Other, PPC::BLR, Chain); |
| 566 | break; |
| 567 | } |
| 568 | } |
| 569 | return SDOperand(N, 0); |
| 570 | } |
| 571 | |
| 572 | |
| 573 | /// createPPC32ISelDag - This pass converts a legalized DAG into a |
| 574 | /// PowerPC-specific DAG, ready for instruction scheduling. |
| 575 | /// |
| 576 | FunctionPass *llvm::createPPC32ISelDag(TargetMachine &TM) { |
| 577 | return new PPC32DAGToDAGISel(TM); |
| 578 | } |
| 579 | |