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