Chris Lattner | 43ff01e | 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" |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 19 | #include "llvm/CodeGen/MachineFunction.h" |
| 20 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/SelectionDAG.h" |
| 22 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 23 | #include "llvm/Target/TargetOptions.h" |
| 24 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 25 | #include "llvm/GlobalValue.h" |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/Support/MathExtras.h" |
| 28 | using namespace llvm; |
| 29 | |
| 30 | namespace { |
| 31 | Statistic<> Recorded("ppc-codegen", "Number of recording ops emitted"); |
| 32 | Statistic<> FusedFP ("ppc-codegen", "Number of fused fp operations"); |
| 33 | Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed"); |
| 34 | |
| 35 | //===--------------------------------------------------------------------===// |
| 36 | /// PPC32DAGToDAGISel - PPC32 specific code to select PPC32 machine |
| 37 | /// instructions for SelectionDAG operations. |
| 38 | /// |
| 39 | class PPC32DAGToDAGISel : public SelectionDAGISel { |
| 40 | PPC32TargetLowering PPC32Lowering; |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 41 | unsigned GlobalBaseReg; |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 42 | public: |
| 43 | PPC32DAGToDAGISel(TargetMachine &TM) |
| 44 | : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM) {} |
| 45 | |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 46 | virtual bool runOnFunction(Function &Fn) { |
| 47 | // Make sure we re-emit a set of the global base reg if necessary |
| 48 | GlobalBaseReg = 0; |
| 49 | return SelectionDAGISel::runOnFunction(Fn); |
| 50 | } |
| 51 | |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 52 | /// getI32Imm - Return a target constant with the specified value, of type |
| 53 | /// i32. |
| 54 | inline SDOperand getI32Imm(unsigned Imm) { |
| 55 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
| 56 | } |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 57 | |
| 58 | /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC |
| 59 | /// base register. Return the virtual register that holds this value. |
| 60 | unsigned getGlobalBaseReg(); |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 61 | |
| 62 | // Select - Convert the specified operand from a target-independent to a |
| 63 | // target-specific node if it hasn't already been changed. |
| 64 | SDOperand Select(SDOperand Op); |
| 65 | |
| 66 | SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS, |
| 67 | unsigned OCHi, unsigned OCLo, |
| 68 | bool IsArithmetic = false, |
| 69 | bool Negate = false); |
Nate Begeman | 93c4bc6 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 70 | SDNode *SelectBitfieldInsert(SDNode *N); |
| 71 | |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame^] | 72 | /// SelectCC - Select a comparison of the specified values with the |
| 73 | /// specified condition code, returning the CR# of the expression. |
| 74 | SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC); |
| 75 | |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 76 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 77 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 78 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 79 | DEBUG(BB->dump()); |
Chris Lattner | 015d739 | 2005-08-18 18:46:06 +0000 | [diff] [blame] | 80 | // Select target instructions for the DAG. |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 81 | Select(DAG.getRoot()); |
| 82 | DAG.RemoveDeadNodes(); |
Chris Lattner | 015d739 | 2005-08-18 18:46:06 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 015d739 | 2005-08-18 18:46:06 +0000 | [diff] [blame] | 84 | // Emit machine code to BB. |
| 85 | ScheduleAndEmitDAG(DAG); |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | virtual const char *getPassName() const { |
| 89 | return "PowerPC DAG->DAG Pattern Instruction Selection"; |
| 90 | } |
| 91 | }; |
| 92 | } |
| 93 | |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 94 | /// getGlobalBaseReg - Output the instructions required to put the |
| 95 | /// base address to use for accessing globals into a register. |
| 96 | /// |
| 97 | unsigned PPC32DAGToDAGISel::getGlobalBaseReg() { |
| 98 | if (!GlobalBaseReg) { |
| 99 | // Insert the set of GlobalBaseReg into the first MBB of the function |
| 100 | MachineBasicBlock &FirstMBB = BB->getParent()->front(); |
| 101 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
| 102 | SSARegMap *RegMap = BB->getParent()->getSSARegMap(); |
| 103 | GlobalBaseReg = RegMap->createVirtualRegister(PPC32::GPRCRegisterClass); |
| 104 | BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR); |
| 105 | BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg); |
| 106 | } |
| 107 | return GlobalBaseReg; |
| 108 | } |
| 109 | |
| 110 | |
Nate Begeman | 72d6f88 | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 111 | // isIntImmediate - This method tests to see if a constant operand. |
| 112 | // If so Imm will receive the 32 bit value. |
| 113 | static bool isIntImmediate(SDNode *N, unsigned& Imm) { |
| 114 | if (N->getOpcode() == ISD::Constant) { |
| 115 | Imm = cast<ConstantSDNode>(N)->getValue(); |
| 116 | return true; |
| 117 | } |
| 118 | return false; |
| 119 | } |
| 120 | |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 121 | // isOprShiftImm - Returns true if the specified operand is a shift opcode with |
| 122 | // a immediate shift count less than 32. |
| 123 | static bool isOprShiftImm(SDNode *N, unsigned& Opc, unsigned& SH) { |
| 124 | Opc = N->getOpcode(); |
| 125 | return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) && |
| 126 | isIntImmediate(N->getOperand(1).Val, SH) && SH < 32; |
| 127 | } |
| 128 | |
| 129 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 130 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 131 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 132 | // not, since all 1s are not contiguous. |
| 133 | static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) { |
| 134 | if (isShiftedMask_32(Val)) { |
| 135 | // look for the first non-zero bit |
| 136 | MB = CountLeadingZeros_32(Val); |
| 137 | // look for the first zero bit after the run of ones |
| 138 | ME = CountLeadingZeros_32((Val - 1) ^ Val); |
| 139 | return true; |
| 140 | } else if (isShiftedMask_32(Val = ~Val)) { // invert mask |
| 141 | // effectively look for the first zero bit |
| 142 | ME = CountLeadingZeros_32(Val) - 1; |
| 143 | // effectively look for the first one bit after the run of zeros |
| 144 | MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1; |
| 145 | return true; |
| 146 | } |
| 147 | // no run present |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | // isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate |
| 152 | // and mask opcode and mask operation. |
| 153 | static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask, |
| 154 | unsigned &SH, unsigned &MB, unsigned &ME) { |
| 155 | unsigned Shift = 32; |
| 156 | unsigned Indeterminant = ~0; // bit mask marking indeterminant results |
| 157 | unsigned Opcode = N->getOpcode(); |
| 158 | if (!isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31)) |
| 159 | return false; |
| 160 | |
| 161 | if (Opcode == ISD::SHL) { |
| 162 | // apply shift left to mask if it comes first |
| 163 | if (IsShiftMask) Mask = Mask << Shift; |
| 164 | // determine which bits are made indeterminant by shift |
| 165 | Indeterminant = ~(0xFFFFFFFFu << Shift); |
| 166 | } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) { |
| 167 | // apply shift right to mask if it comes first |
| 168 | if (IsShiftMask) Mask = Mask >> Shift; |
| 169 | // determine which bits are made indeterminant by shift |
| 170 | Indeterminant = ~(0xFFFFFFFFu >> Shift); |
| 171 | // adjust for the left rotate |
| 172 | Shift = 32 - Shift; |
| 173 | } else { |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | // if the mask doesn't intersect any Indeterminant bits |
| 178 | if (Mask && !(Mask & Indeterminant)) { |
| 179 | SH = Shift; |
| 180 | // make sure the mask is still a mask (wrap arounds may not be) |
| 181 | return isRunOfOnes(Mask, MB, ME); |
| 182 | } |
| 183 | return false; |
| 184 | } |
| 185 | |
Nate Begeman | 72d6f88 | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 186 | // isOpcWithIntImmediate - This method tests to see if the node is a specific |
| 187 | // opcode and that it has a immediate integer right operand. |
| 188 | // If so Imm will receive the 32 bit value. |
| 189 | static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) { |
| 190 | return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm); |
| 191 | } |
| 192 | |
| 193 | // isOprNot - Returns true if the specified operand is an xor with immediate -1. |
| 194 | static bool isOprNot(SDNode *N) { |
| 195 | unsigned Imm; |
| 196 | return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1; |
| 197 | } |
| 198 | |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 199 | // Immediate constant composers. |
| 200 | // Lo16 - grabs the lo 16 bits from a 32 bit constant. |
| 201 | // Hi16 - grabs the hi 16 bits from a 32 bit constant. |
| 202 | // HA16 - computes the hi bits required if the lo bits are add/subtracted in |
| 203 | // arithmethically. |
| 204 | static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; } |
| 205 | static unsigned Hi16(unsigned x) { return Lo16(x >> 16); } |
| 206 | static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); } |
| 207 | |
| 208 | // isIntImmediate - This method tests to see if a constant operand. |
| 209 | // If so Imm will receive the 32 bit value. |
| 210 | static bool isIntImmediate(SDOperand N, unsigned& Imm) { |
| 211 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) { |
| 212 | Imm = (unsigned)CN->getSignExtended(); |
| 213 | return true; |
| 214 | } |
| 215 | return false; |
| 216 | } |
| 217 | |
Nate Begeman | 93c4bc6 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 218 | /// SelectBitfieldInsert - turn an or of two masked values into |
| 219 | /// the rotate left word immediate then mask insert (rlwimi) instruction. |
| 220 | /// Returns true on success, false if the caller still needs to select OR. |
| 221 | /// |
| 222 | /// Patterns matched: |
| 223 | /// 1. or shl, and 5. or and, and |
| 224 | /// 2. or and, shl 6. or shl, shr |
| 225 | /// 3. or shr, and 7. or shr, shl |
| 226 | /// 4. or and, shr |
| 227 | SDNode *PPC32DAGToDAGISel::SelectBitfieldInsert(SDNode *N) { |
| 228 | bool IsRotate = false; |
| 229 | unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0; |
| 230 | unsigned Value; |
| 231 | |
| 232 | SDOperand Op0 = N->getOperand(0); |
| 233 | SDOperand Op1 = N->getOperand(1); |
| 234 | |
| 235 | unsigned Op0Opc = Op0.getOpcode(); |
| 236 | unsigned Op1Opc = Op1.getOpcode(); |
| 237 | |
| 238 | // Verify that we have the correct opcodes |
| 239 | if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc) |
| 240 | return false; |
| 241 | if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc) |
| 242 | return false; |
| 243 | |
| 244 | // Generate Mask value for Target |
| 245 | if (isIntImmediate(Op0.getOperand(1), Value)) { |
| 246 | switch(Op0Opc) { |
| 247 | case ISD::SHL: TgtMask <<= Value; break; |
| 248 | case ISD::SRL: TgtMask >>= Value; break; |
| 249 | case ISD::AND: TgtMask &= Value; break; |
| 250 | } |
| 251 | } else { |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | // Generate Mask value for Insert |
| 256 | if (isIntImmediate(Op1.getOperand(1), Value)) { |
| 257 | switch(Op1Opc) { |
| 258 | case ISD::SHL: |
| 259 | SH = Value; |
| 260 | InsMask <<= SH; |
| 261 | if (Op0Opc == ISD::SRL) IsRotate = true; |
| 262 | break; |
| 263 | case ISD::SRL: |
| 264 | SH = Value; |
| 265 | InsMask >>= SH; |
| 266 | SH = 32-SH; |
| 267 | if (Op0Opc == ISD::SHL) IsRotate = true; |
| 268 | break; |
| 269 | case ISD::AND: |
| 270 | InsMask &= Value; |
| 271 | break; |
| 272 | } |
| 273 | } else { |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | // If both of the inputs are ANDs and one of them has a logical shift by |
| 278 | // constant as its input, make that AND the inserted value so that we can |
| 279 | // combine the shift into the rotate part of the rlwimi instruction |
| 280 | bool IsAndWithShiftOp = false; |
| 281 | if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) { |
| 282 | if (Op1.getOperand(0).getOpcode() == ISD::SHL || |
| 283 | Op1.getOperand(0).getOpcode() == ISD::SRL) { |
| 284 | if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) { |
| 285 | SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value; |
| 286 | IsAndWithShiftOp = true; |
| 287 | } |
| 288 | } else if (Op0.getOperand(0).getOpcode() == ISD::SHL || |
| 289 | Op0.getOperand(0).getOpcode() == ISD::SRL) { |
| 290 | if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) { |
| 291 | std::swap(Op0, Op1); |
| 292 | std::swap(TgtMask, InsMask); |
| 293 | SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value; |
| 294 | IsAndWithShiftOp = true; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // Verify that the Target mask and Insert mask together form a full word mask |
| 300 | // and that the Insert mask is a run of set bits (which implies both are runs |
| 301 | // of set bits). Given that, Select the arguments and generate the rlwimi |
| 302 | // instruction. |
| 303 | unsigned MB, ME; |
| 304 | if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) { |
| 305 | bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF; |
| 306 | bool Op0IsAND = Op0Opc == ISD::AND; |
| 307 | // Check for rotlwi / rotrwi here, a special case of bitfield insert |
| 308 | // where both bitfield halves are sourced from the same value. |
| 309 | if (IsRotate && fullMask && |
| 310 | N->getOperand(0).getOperand(0) == N->getOperand(1).getOperand(0)) { |
| 311 | Op0 = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, |
| 312 | Select(N->getOperand(0).getOperand(0)), |
| 313 | getI32Imm(SH), getI32Imm(0), getI32Imm(31)); |
| 314 | return Op0.Val; |
| 315 | } |
| 316 | SDOperand Tmp1 = (Op0IsAND && fullMask) ? Select(Op0.getOperand(0)) |
| 317 | : Select(Op0); |
| 318 | SDOperand Tmp2 = IsAndWithShiftOp ? Select(Op1.getOperand(0).getOperand(0)) |
| 319 | : Select(Op1.getOperand(0)); |
| 320 | Op0 = CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2, |
| 321 | getI32Imm(SH), getI32Imm(MB), getI32Imm(ME)); |
| 322 | return Op0.Val; |
| 323 | } |
| 324 | return 0; |
| 325 | } |
| 326 | |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 327 | // SelectIntImmediateExpr - Choose code for integer operations with an immediate |
| 328 | // operand. |
| 329 | SDNode *PPC32DAGToDAGISel::SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS, |
| 330 | unsigned OCHi, unsigned OCLo, |
| 331 | bool IsArithmetic, |
| 332 | bool Negate) { |
| 333 | // Check to make sure this is a constant. |
| 334 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS); |
| 335 | // Exit if not a constant. |
| 336 | if (!CN) return 0; |
| 337 | // Extract immediate. |
| 338 | unsigned C = (unsigned)CN->getValue(); |
| 339 | // Negate if required (ISD::SUB). |
| 340 | if (Negate) C = -C; |
| 341 | // Get the hi and lo portions of constant. |
| 342 | unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C); |
| 343 | unsigned Lo = Lo16(C); |
| 344 | |
| 345 | // If two instructions are needed and usage indicates it would be better to |
| 346 | // load immediate into a register, bail out. |
| 347 | if (Hi && Lo && CN->use_size() > 2) return false; |
| 348 | |
| 349 | // Select the first operand. |
| 350 | SDOperand Opr0 = Select(LHS); |
| 351 | |
| 352 | if (Lo) // Add in the lo-part. |
| 353 | Opr0 = CurDAG->getTargetNode(OCLo, MVT::i32, Opr0, getI32Imm(Lo)); |
| 354 | if (Hi) // Add in the hi-part. |
| 355 | Opr0 = CurDAG->getTargetNode(OCHi, MVT::i32, Opr0, getI32Imm(Hi)); |
| 356 | return Opr0.Val; |
| 357 | } |
| 358 | |
| 359 | |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame^] | 360 | /// SelectCC - Select a comparison of the specified values with the specified |
| 361 | /// condition code, returning the CR# of the expression. |
| 362 | SDOperand PPC32DAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS, |
| 363 | ISD::CondCode CC) { |
| 364 | // Always select the LHS. |
| 365 | LHS = Select(LHS); |
| 366 | |
| 367 | // Use U to determine whether the SETCC immediate range is signed or not. |
| 368 | if (MVT::isInteger(LHS.getValueType())) { |
| 369 | bool U = ISD::isUnsignedIntSetCC(CC); |
| 370 | unsigned Imm; |
| 371 | if (isIntImmediate(RHS, Imm) && |
| 372 | ((U && isUInt16(Imm)) || (!U && isInt16(Imm)))) |
| 373 | return CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI, MVT::i32, |
| 374 | LHS, getI32Imm(Lo16(Imm))); |
| 375 | return CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32, |
| 376 | LHS, Select(RHS)); |
| 377 | } else { |
| 378 | return CurDAG->getTargetNode(PPC::FCMPU, MVT::i32, LHS, Select(RHS)); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding |
| 383 | /// to Condition. |
| 384 | static unsigned getBCCForSetCC(ISD::CondCode CC) { |
| 385 | switch (CC) { |
| 386 | default: assert(0 && "Unknown condition!"); abort(); |
| 387 | case ISD::SETEQ: return PPC::BEQ; |
| 388 | case ISD::SETNE: return PPC::BNE; |
| 389 | case ISD::SETULT: |
| 390 | case ISD::SETLT: return PPC::BLT; |
| 391 | case ISD::SETULE: |
| 392 | case ISD::SETLE: return PPC::BLE; |
| 393 | case ISD::SETUGT: |
| 394 | case ISD::SETGT: return PPC::BGT; |
| 395 | case ISD::SETUGE: |
| 396 | case ISD::SETGE: return PPC::BGE; |
| 397 | } |
| 398 | return 0; |
| 399 | } |
| 400 | |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 401 | // Select - Convert the specified operand from a target-independent to a |
| 402 | // target-specific node if it hasn't already been changed. |
| 403 | SDOperand PPC32DAGToDAGISel::Select(SDOperand Op) { |
| 404 | SDNode *N = Op.Val; |
| 405 | if (N->getOpcode() >= ISD::BUILTIN_OP_END) |
| 406 | return Op; // Already selected. |
| 407 | |
| 408 | switch (N->getOpcode()) { |
| 409 | default: |
| 410 | std::cerr << "Cannot yet select: "; |
| 411 | N->dump(); |
| 412 | std::cerr << "\n"; |
| 413 | abort(); |
| 414 | case ISD::EntryToken: // These leaves remain the same. |
| 415 | case ISD::UNDEF: |
| 416 | return Op; |
| 417 | case ISD::TokenFactor: { |
| 418 | SDOperand New; |
| 419 | if (N->getNumOperands() == 2) { |
| 420 | SDOperand Op0 = Select(N->getOperand(0)); |
| 421 | SDOperand Op1 = Select(N->getOperand(1)); |
| 422 | New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1); |
| 423 | } else { |
| 424 | std::vector<SDOperand> Ops; |
| 425 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
Chris Lattner | 65d6679 | 2005-08-19 21:33:02 +0000 | [diff] [blame] | 426 | Ops.push_back(Select(N->getOperand(i))); |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 427 | New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops); |
| 428 | } |
| 429 | |
| 430 | if (New.Val != N) { |
| 431 | CurDAG->ReplaceAllUsesWith(N, New.Val); |
| 432 | N = New.Val; |
| 433 | } |
| 434 | break; |
| 435 | } |
| 436 | case ISD::CopyFromReg: { |
| 437 | SDOperand Chain = Select(N->getOperand(0)); |
| 438 | if (Chain == N->getOperand(0)) return Op; // No change |
| 439 | SDOperand New = CurDAG->getCopyFromReg(Chain, |
| 440 | cast<RegisterSDNode>(N->getOperand(1))->getReg(), N->getValueType(0)); |
| 441 | return New.getValue(Op.ResNo); |
| 442 | } |
| 443 | case ISD::CopyToReg: { |
| 444 | SDOperand Chain = Select(N->getOperand(0)); |
| 445 | SDOperand Reg = N->getOperand(1); |
| 446 | SDOperand Val = Select(N->getOperand(2)); |
| 447 | if (Chain != N->getOperand(0) || Val != N->getOperand(2)) { |
| 448 | SDOperand New = CurDAG->getNode(ISD::CopyToReg, MVT::Other, |
| 449 | Chain, Reg, Val); |
| 450 | CurDAG->ReplaceAllUsesWith(N, New.Val); |
| 451 | N = New.Val; |
| 452 | } |
| 453 | break; |
| 454 | } |
| 455 | case ISD::Constant: { |
| 456 | assert(N->getValueType(0) == MVT::i32); |
| 457 | unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue(); |
Nate Begeman | d326387 | 2005-08-18 18:01:39 +0000 | [diff] [blame] | 458 | unsigned Hi = HA16(v); |
| 459 | unsigned Lo = Lo16(v); |
| 460 | if (Hi && Lo) { |
| 461 | SDOperand Top = CurDAG->getTargetNode(PPC::LIS, MVT::i32, |
| 462 | getI32Imm(v >> 16)); |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 463 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORI, Top, getI32Imm(v & 0xFFFF)); |
Nate Begeman | d326387 | 2005-08-18 18:01:39 +0000 | [diff] [blame] | 464 | } else if (Lo) { |
| 465 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::LI, getI32Imm(v)); |
| 466 | } else { |
| 467 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::LIS, getI32Imm(v >> 16)); |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 468 | } |
Nate Begeman | d326387 | 2005-08-18 18:01:39 +0000 | [diff] [blame] | 469 | break; |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 470 | } |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 471 | case ISD::GlobalAddress: { |
| 472 | GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
| 473 | SDOperand Tmp; |
| 474 | SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i32); |
| 475 | if (PICEnabled) { |
| 476 | SDOperand PICBaseReg = CurDAG->getRegister(getGlobalBaseReg(), MVT::i32); |
| 477 | Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, PICBaseReg, GA); |
| 478 | } else { |
| 479 | Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, GA); |
| 480 | } |
| 481 | if (GV->hasWeakLinkage() || GV->isExternal()) |
| 482 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::LWZ, GA, Tmp); |
| 483 | else |
| 484 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::LA, Tmp, GA); |
| 485 | break; |
| 486 | } |
Nate Begeman | 4bfb4a2 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 487 | case ISD::SIGN_EXTEND_INREG: |
| 488 | switch(cast<VTSDNode>(N->getOperand(1))->getVT()) { |
| 489 | default: assert(0 && "Illegal type in SIGN_EXTEND_INREG"); break; |
| 490 | case MVT::i16: |
| 491 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::EXTSH, Select(N->getOperand(0))); |
| 492 | break; |
| 493 | case MVT::i8: |
| 494 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::EXTSB, Select(N->getOperand(0))); |
| 495 | break; |
Nate Begeman | 4bfb4a2 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 496 | } |
| 497 | break; |
| 498 | case ISD::CTLZ: |
| 499 | assert(N->getValueType(0) == MVT::i32); |
| 500 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::CNTLZW, Select(N->getOperand(0))); |
| 501 | break; |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 502 | case ISD::ADD: { |
| 503 | MVT::ValueType Ty = N->getValueType(0); |
| 504 | if (Ty == MVT::i32) { |
| 505 | if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1), |
| 506 | PPC::ADDIS, PPC::ADDI, true)) { |
| 507 | CurDAG->ReplaceAllUsesWith(N, I); |
| 508 | N = I; |
| 509 | } else { |
| 510 | CurDAG->SelectNodeTo(N, Ty, PPC::ADD, Select(N->getOperand(0)), |
| 511 | Select(N->getOperand(1))); |
| 512 | } |
| 513 | break; |
| 514 | } |
| 515 | |
| 516 | if (!NoExcessFPPrecision) { // Match FMA ops |
| 517 | if (N->getOperand(0).getOpcode() == ISD::MUL && |
| 518 | N->getOperand(0).Val->hasOneUse()) { |
| 519 | ++FusedFP; // Statistic |
| 520 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, |
| 521 | Select(N->getOperand(0).getOperand(0)), |
| 522 | Select(N->getOperand(0).getOperand(1)), |
| 523 | Select(N->getOperand(1))); |
| 524 | break; |
| 525 | } else if (N->getOperand(1).getOpcode() == ISD::MUL && |
| 526 | N->getOperand(1).hasOneUse()) { |
| 527 | ++FusedFP; // Statistic |
| 528 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, |
| 529 | Select(N->getOperand(1).getOperand(0)), |
| 530 | Select(N->getOperand(1).getOperand(1)), |
| 531 | Select(N->getOperand(0))); |
| 532 | break; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FADD : PPC::FADDS, |
| 537 | Select(N->getOperand(0)), Select(N->getOperand(1))); |
| 538 | break; |
| 539 | } |
| 540 | case ISD::SUB: { |
| 541 | MVT::ValueType Ty = N->getValueType(0); |
| 542 | if (Ty == MVT::i32) { |
| 543 | unsigned Imm; |
| 544 | if (isIntImmediate(N->getOperand(0), Imm) && isInt16(Imm)) { |
| 545 | CurDAG->SelectNodeTo(N, Ty, PPC::SUBFIC, Select(N->getOperand(1)), |
| 546 | getI32Imm(Lo16(Imm))); |
| 547 | break; |
| 548 | } |
| 549 | if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1), |
| 550 | PPC::ADDIS, PPC::ADDI, true, true)) { |
| 551 | CurDAG->ReplaceAllUsesWith(N, I); |
| 552 | N = I; |
| 553 | } else { |
| 554 | CurDAG->SelectNodeTo(N, Ty, PPC::SUBF, Select(N->getOperand(1)), |
| 555 | Select(N->getOperand(0))); |
| 556 | } |
| 557 | break; |
| 558 | } |
| 559 | |
| 560 | if (!NoExcessFPPrecision) { // Match FMA ops |
| 561 | if (N->getOperand(0).getOpcode() == ISD::MUL && |
| 562 | N->getOperand(0).Val->hasOneUse()) { |
| 563 | ++FusedFP; // Statistic |
| 564 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS, |
| 565 | Select(N->getOperand(0).getOperand(0)), |
| 566 | Select(N->getOperand(0).getOperand(1)), |
| 567 | Select(N->getOperand(1))); |
| 568 | break; |
| 569 | } else if (N->getOperand(1).getOpcode() == ISD::MUL && |
| 570 | N->getOperand(1).Val->hasOneUse()) { |
| 571 | ++FusedFP; // Statistic |
| 572 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS, |
| 573 | Select(N->getOperand(1).getOperand(0)), |
| 574 | Select(N->getOperand(1).getOperand(1)), |
| 575 | Select(N->getOperand(0))); |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSUB : PPC::FSUBS, |
| 580 | Select(N->getOperand(0)), |
| 581 | Select(N->getOperand(1))); |
| 582 | break; |
Nate Begeman | 3fcf47d | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 583 | } |
Nate Begeman | 74d5529 | 2005-08-18 00:21:41 +0000 | [diff] [blame] | 584 | case ISD::MUL: { |
| 585 | unsigned Imm, Opc; |
| 586 | if (isIntImmediate(N->getOperand(1), Imm) && isInt16(Imm)) { |
| 587 | CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::MULLI, |
| 588 | Select(N->getOperand(0)), getI32Imm(Lo16(Imm))); |
| 589 | break; |
| 590 | } |
| 591 | switch (N->getValueType(0)) { |
| 592 | default: assert(0 && "Unhandled multiply type!"); |
| 593 | case MVT::i32: Opc = PPC::MULLW; break; |
| 594 | case MVT::f32: Opc = PPC::FMULS; break; |
| 595 | case MVT::f64: Opc = PPC::FMUL; break; |
| 596 | } |
| 597 | CurDAG->SelectNodeTo(N, N->getValueType(0), Opc, Select(N->getOperand(0)), |
| 598 | Select(N->getOperand(1))); |
| 599 | break; |
| 600 | } |
Nate Begeman | 4bfb4a2 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 601 | case ISD::MULHS: |
Nate Begeman | 74d5529 | 2005-08-18 00:21:41 +0000 | [diff] [blame] | 602 | assert(N->getValueType(0) == MVT::i32); |
Nate Begeman | 4bfb4a2 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 603 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::MULHW, Select(N->getOperand(0)), |
| 604 | Select(N->getOperand(1))); |
Nate Begeman | 74d5529 | 2005-08-18 00:21:41 +0000 | [diff] [blame] | 605 | break; |
Nate Begeman | 4bfb4a2 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 606 | case ISD::MULHU: |
Nate Begeman | 74d5529 | 2005-08-18 00:21:41 +0000 | [diff] [blame] | 607 | assert(N->getValueType(0) == MVT::i32); |
Nate Begeman | 4bfb4a2 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 608 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::MULHWU, Select(N->getOperand(0)), |
| 609 | Select(N->getOperand(1))); |
Nate Begeman | 74d5529 | 2005-08-18 00:21:41 +0000 | [diff] [blame] | 610 | break; |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 611 | case ISD::AND: { |
Nate Begeman | d326387 | 2005-08-18 18:01:39 +0000 | [diff] [blame] | 612 | unsigned Imm; |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 613 | // If this is an and of a value rotated between 0 and 31 bits and then and'd |
| 614 | // with a mask, emit rlwinm |
| 615 | if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) || |
| 616 | isShiftedMask_32(~Imm))) { |
| 617 | SDOperand Val; |
Nate Begeman | d326387 | 2005-08-18 18:01:39 +0000 | [diff] [blame] | 618 | unsigned SH, MB, ME; |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 619 | if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) { |
| 620 | Val = Select(N->getOperand(0).getOperand(0)); |
| 621 | } else { |
| 622 | Val = Select(N->getOperand(0)); |
| 623 | isRunOfOnes(Imm, MB, ME); |
| 624 | SH = 0; |
| 625 | } |
| 626 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, Val, getI32Imm(SH), |
| 627 | getI32Imm(MB), getI32Imm(ME)); |
| 628 | break; |
| 629 | } |
| 630 | // If this is an and with an immediate that isn't a mask, then codegen it as |
| 631 | // high and low 16 bit immediate ands. |
| 632 | if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), |
| 633 | N->getOperand(1), |
| 634 | PPC::ANDISo, PPC::ANDIo)) { |
| 635 | CurDAG->ReplaceAllUsesWith(N, I); |
| 636 | N = I; |
| 637 | break; |
| 638 | } |
| 639 | // Finally, check for the case where we are being asked to select |
| 640 | // and (not(a), b) or and (a, not(b)) which can be selected as andc. |
| 641 | if (isOprNot(N->getOperand(0).Val)) |
| 642 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::ANDC, Select(N->getOperand(1)), |
| 643 | Select(N->getOperand(0).getOperand(0))); |
| 644 | else if (isOprNot(N->getOperand(1).Val)) |
| 645 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::ANDC, Select(N->getOperand(0)), |
| 646 | Select(N->getOperand(1).getOperand(0))); |
| 647 | else |
| 648 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::AND, Select(N->getOperand(0)), |
| 649 | Select(N->getOperand(1))); |
| 650 | break; |
| 651 | } |
Nate Begeman | 93c4bc6 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 652 | case ISD::OR: |
| 653 | if (SDNode *I = SelectBitfieldInsert(N)) { |
| 654 | CurDAG->ReplaceAllUsesWith(N, I); |
| 655 | N = I; |
| 656 | break; |
| 657 | } |
| 658 | if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), |
| 659 | N->getOperand(1), |
| 660 | PPC::ORIS, PPC::ORI)) { |
| 661 | CurDAG->ReplaceAllUsesWith(N, I); |
| 662 | N = I; |
| 663 | break; |
| 664 | } |
| 665 | // Finally, check for the case where we are being asked to select |
| 666 | // 'or (not(a), b)' or 'or (a, not(b))' which can be selected as orc. |
| 667 | if (isOprNot(N->getOperand(0).Val)) |
| 668 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORC, Select(N->getOperand(1)), |
| 669 | Select(N->getOperand(0).getOperand(0))); |
| 670 | else if (isOprNot(N->getOperand(1).Val)) |
| 671 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORC, Select(N->getOperand(0)), |
| 672 | Select(N->getOperand(1).getOperand(0))); |
| 673 | else |
| 674 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::OR, Select(N->getOperand(0)), |
| 675 | Select(N->getOperand(1))); |
| 676 | break; |
Nate Begeman | 72d6f88 | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 677 | case ISD::XOR: |
| 678 | // Check whether or not this node is a logical 'not'. This is represented |
| 679 | // by llvm as a xor with the constant value -1 (all bits set). If this is a |
| 680 | // 'not', then fold 'or' into 'nor', and so forth for the supported ops. |
| 681 | if (isOprNot(N)) { |
| 682 | unsigned Opc; |
Nate Begeman | cfb9a74 | 2005-08-18 05:44:50 +0000 | [diff] [blame] | 683 | SDOperand Val = Select(N->getOperand(0)); |
| 684 | switch (Val.getTargetOpcode()) { |
Nate Begeman | 72d6f88 | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 685 | default: Opc = 0; break; |
Nate Begeman | cfb9a74 | 2005-08-18 05:44:50 +0000 | [diff] [blame] | 686 | case PPC::OR: Opc = PPC::NOR; break; |
| 687 | case PPC::AND: Opc = PPC::NAND; break; |
| 688 | case PPC::XOR: Opc = PPC::EQV; break; |
Nate Begeman | 72d6f88 | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 689 | } |
| 690 | if (Opc) |
Nate Begeman | cfb9a74 | 2005-08-18 05:44:50 +0000 | [diff] [blame] | 691 | CurDAG->SelectNodeTo(N, MVT::i32, Opc, Val.getOperand(0), |
| 692 | Val.getOperand(1)); |
Nate Begeman | 72d6f88 | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 693 | else |
Nate Begeman | cfb9a74 | 2005-08-18 05:44:50 +0000 | [diff] [blame] | 694 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::NOR, Val, Val); |
Nate Begeman | 72d6f88 | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 695 | break; |
| 696 | } |
| 697 | // If this is a xor with an immediate other than -1, then codegen it as high |
| 698 | // and low 16 bit immediate xors. |
| 699 | if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), |
| 700 | N->getOperand(1), |
| 701 | PPC::XORIS, PPC::XORI)) { |
| 702 | CurDAG->ReplaceAllUsesWith(N, I); |
| 703 | N = I; |
| 704 | break; |
| 705 | } |
| 706 | // Finally, check for the case where we are being asked to select |
| 707 | // xor (not(a), b) which is equivalent to not(xor a, b), which is eqv |
| 708 | if (isOprNot(N->getOperand(0).Val)) |
| 709 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::EQV, |
| 710 | Select(N->getOperand(0).getOperand(0)), |
| 711 | Select(N->getOperand(1))); |
| 712 | else |
| 713 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::XOR, Select(N->getOperand(0)), |
| 714 | Select(N->getOperand(1))); |
| 715 | break; |
Nate Begeman | 33acb2c | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 716 | case ISD::SHL: { |
| 717 | unsigned Imm, SH, MB, ME; |
| 718 | if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) && |
| 719 | isRotateAndMask(N, Imm, true, SH, MB, ME)) |
| 720 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, |
| 721 | Select(N->getOperand(0).getOperand(0)), |
| 722 | getI32Imm(SH), getI32Imm(MB), getI32Imm(ME)); |
| 723 | else if (isIntImmediate(N->getOperand(1), Imm)) |
| 724 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, Select(N->getOperand(0)), |
| 725 | getI32Imm(Imm), getI32Imm(0), getI32Imm(31-Imm)); |
| 726 | else |
| 727 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::SLW, Select(N->getOperand(0)), |
| 728 | Select(N->getOperand(1))); |
| 729 | break; |
| 730 | } |
| 731 | case ISD::SRL: { |
| 732 | unsigned Imm, SH, MB, ME; |
| 733 | if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) && |
| 734 | isRotateAndMask(N, Imm, true, SH, MB, ME)) |
| 735 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, |
| 736 | Select(N->getOperand(0).getOperand(0)), |
| 737 | getI32Imm(SH), getI32Imm(MB), getI32Imm(ME)); |
| 738 | else if (isIntImmediate(N->getOperand(1), Imm)) |
| 739 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, Select(N->getOperand(0)), |
| 740 | getI32Imm(32-Imm), getI32Imm(Imm), getI32Imm(31)); |
| 741 | else |
| 742 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::SRW, Select(N->getOperand(0)), |
| 743 | Select(N->getOperand(1))); |
| 744 | break; |
| 745 | } |
| 746 | case ISD::SRA: { |
| 747 | unsigned Imm, SH, MB, ME; |
| 748 | if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) && |
| 749 | isRotateAndMask(N, Imm, true, SH, MB, ME)) |
| 750 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, |
| 751 | Select(N->getOperand(0).getOperand(0)), |
| 752 | getI32Imm(SH), getI32Imm(MB), getI32Imm(ME)); |
| 753 | else if (isIntImmediate(N->getOperand(1), Imm)) |
| 754 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::SRAWI, Select(N->getOperand(0)), |
| 755 | getI32Imm(Imm)); |
| 756 | else |
| 757 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::SRAW, Select(N->getOperand(0)), |
| 758 | Select(N->getOperand(1))); |
| 759 | break; |
| 760 | } |
Nate Begeman | 4bfb4a2 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 761 | case ISD::FABS: |
Nate Begeman | 457367f | 2005-08-18 00:53:47 +0000 | [diff] [blame] | 762 | CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::FABS, |
| 763 | Select(N->getOperand(0))); |
| 764 | break; |
Nate Begeman | 4bfb4a2 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 765 | case ISD::FP_EXTEND: |
| 766 | assert(MVT::f64 == N->getValueType(0) && |
| 767 | MVT::f32 == N->getOperand(0).getValueType() && "Illegal FP_EXTEND"); |
| 768 | CurDAG->SelectNodeTo(N, MVT::f64, PPC::FMR, Select(N->getOperand(0))); |
| 769 | break; |
| 770 | case ISD::FP_ROUND: |
| 771 | assert(MVT::f32 == N->getValueType(0) && |
| 772 | MVT::f64 == N->getOperand(0).getValueType() && "Illegal FP_ROUND"); |
| 773 | CurDAG->SelectNodeTo(N, MVT::f32, PPC::FRSP, Select(N->getOperand(0))); |
| 774 | break; |
Nate Begeman | 3fcf47d | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 775 | case ISD::FNEG: { |
| 776 | SDOperand Val = Select(N->getOperand(0)); |
| 777 | MVT::ValueType Ty = N->getValueType(0); |
| 778 | if (Val.Val->hasOneUse()) { |
| 779 | unsigned Opc; |
| 780 | switch (Val.getTargetOpcode()) { |
| 781 | default: Opc = 0; break; |
| 782 | case PPC::FABS: Opc = PPC::FNABS; break; |
| 783 | case PPC::FMADD: Opc = PPC::FNMADD; break; |
| 784 | case PPC::FMADDS: Opc = PPC::FNMADDS; break; |
| 785 | case PPC::FMSUB: Opc = PPC::FNMSUB; break; |
| 786 | case PPC::FMSUBS: Opc = PPC::FNMSUBS; break; |
| 787 | } |
| 788 | // If we inverted the opcode, then emit the new instruction with the |
| 789 | // inverted opcode and the original instruction's operands. Otherwise, |
| 790 | // fall through and generate a fneg instruction. |
| 791 | if (Opc) { |
| 792 | if (PPC::FNABS == Opc) |
| 793 | CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0)); |
| 794 | else |
| 795 | CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0), |
| 796 | Val.getOperand(1), Val.getOperand(2)); |
| 797 | break; |
| 798 | } |
| 799 | } |
| 800 | CurDAG->SelectNodeTo(N, Ty, PPC::FNEG, Val); |
| 801 | break; |
| 802 | } |
Nate Begeman | 457367f | 2005-08-18 00:53:47 +0000 | [diff] [blame] | 803 | case ISD::FSQRT: { |
| 804 | MVT::ValueType Ty = N->getValueType(0); |
| 805 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS, |
| 806 | Select(N->getOperand(0))); |
| 807 | break; |
| 808 | } |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 809 | case ISD::RET: { |
| 810 | SDOperand Chain = Select(N->getOperand(0)); // Token chain. |
| 811 | |
| 812 | if (N->getNumOperands() > 1) { |
| 813 | SDOperand Val = Select(N->getOperand(1)); |
| 814 | switch (N->getOperand(1).getValueType()) { |
| 815 | default: assert(0 && "Unknown return type!"); |
| 816 | case MVT::f64: |
| 817 | case MVT::f32: |
| 818 | Chain = CurDAG->getCopyToReg(Chain, PPC::F1, Val); |
| 819 | break; |
| 820 | case MVT::i32: |
| 821 | Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Val); |
| 822 | break; |
| 823 | } |
| 824 | |
| 825 | if (N->getNumOperands() > 2) { |
| 826 | assert(N->getOperand(1).getValueType() == MVT::i32 && |
| 827 | N->getOperand(2).getValueType() == MVT::i32 && |
| 828 | N->getNumOperands() == 2 && "Unknown two-register ret value!"); |
| 829 | Val = Select(N->getOperand(2)); |
| 830 | Chain = CurDAG->getCopyToReg(Chain, PPC::R4, Val); |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | // Finally, select this to a blr (return) instruction. |
| 835 | CurDAG->SelectNodeTo(N, MVT::Other, PPC::BLR, Chain); |
| 836 | break; |
| 837 | } |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame^] | 838 | |
| 839 | case ISD::BR_CC: |
| 840 | case ISD::BRTWOWAY_CC: { |
| 841 | SDOperand Chain = Select(N->getOperand(0)); |
| 842 | MachineBasicBlock *Dest = |
| 843 | cast<BasicBlockSDNode>(N->getOperand(4))->getBasicBlock(); |
| 844 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get(); |
| 845 | SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC); |
| 846 | unsigned Opc = getBCCForSetCC(CC); |
| 847 | |
| 848 | // If this is a two way branch, then grab the fallthrough basic block |
| 849 | // argument and build a PowerPC branch pseudo-op, suitable for long branch |
| 850 | // conversion if necessary by the branch selection pass. Otherwise, emit a |
| 851 | // standard conditional branch. |
| 852 | if (N->getOpcode() == ISD::BRTWOWAY_CC) { |
| 853 | MachineBasicBlock *Fallthrough = |
| 854 | cast<BasicBlockSDNode>(N->getOperand(5))->getBasicBlock(); |
| 855 | SDOperand CB = CurDAG->getTargetNode(PPC::COND_BRANCH, MVT::Other, |
| 856 | CondCode, getI32Imm(Opc), |
| 857 | N->getOperand(4), N->getOperand(5), |
| 858 | Chain); |
| 859 | CurDAG->SelectNodeTo(N, MVT::Other, PPC::B, N->getOperand(5), CB); |
| 860 | } else { |
| 861 | // Iterate to the next basic block |
| 862 | ilist<MachineBasicBlock>::iterator It = BB; |
| 863 | ++It; |
| 864 | |
| 865 | // If the fallthrough path is off the end of the function, which would be |
| 866 | // undefined behavior, set it to be the same as the current block because |
| 867 | // we have nothing better to set it to, and leaving it alone will cause |
| 868 | // the PowerPC Branch Selection pass to crash. |
| 869 | if (It == BB->getParent()->end()) It = Dest; |
| 870 | CurDAG->SelectNodeTo(N, MVT::Other, PPC::COND_BRANCH, CondCode, |
| 871 | getI32Imm(Opc), N->getOperand(4), |
| 872 | CurDAG->getBasicBlock(It), Chain); |
| 873 | } |
| 874 | break; |
| 875 | } |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 876 | } |
| 877 | return SDOperand(N, 0); |
| 878 | } |
| 879 | |
| 880 | |
| 881 | /// createPPC32ISelDag - This pass converts a legalized DAG into a |
| 882 | /// PowerPC-specific DAG, ready for instruction scheduling. |
| 883 | /// |
| 884 | FunctionPass *llvm::createPPC32ISelDag(TargetMachine &TM) { |
| 885 | return new PPC32DAGToDAGISel(TM); |
| 886 | } |
| 887 | |