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 | 2fe76e5 | 2005-08-25 04:47:18 +0000 | [diff] [blame] | 25 | #include "llvm/Constants.h" |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 26 | #include "llvm/GlobalValue.h" |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/MathExtras.h" |
| 29 | using namespace llvm; |
| 30 | |
| 31 | namespace { |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 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 | 047b952 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 81 | SDOperand BuildSDIVSequence(SDNode *N); |
| 82 | SDOperand BuildUDIVSequence(SDNode *N); |
| 83 | |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 84 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 85 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 86 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 87 | DEBUG(BB->dump()); |
Chris Lattner | d607c12 | 2005-08-18 18:46:06 +0000 | [diff] [blame] | 88 | // Select target instructions for the DAG. |
Chris Lattner | efa6abc | 2005-08-29 01:07:02 +0000 | [diff] [blame] | 89 | DAG.setRoot(Select(DAG.getRoot())); |
Chris Lattner | 333bd83 | 2005-09-27 17:45:33 +0000 | [diff] [blame] | 90 | CodeGenMap.clear(); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 91 | DAG.RemoveDeadNodes(); |
Chris Lattner | d607c12 | 2005-08-18 18:46:06 +0000 | [diff] [blame] | 92 | |
Chris Lattner | d607c12 | 2005-08-18 18:46:06 +0000 | [diff] [blame] | 93 | // Emit machine code to BB. |
| 94 | ScheduleAndEmitDAG(DAG); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | virtual const char *getPassName() const { |
| 98 | return "PowerPC DAG->DAG Pattern Instruction Selection"; |
| 99 | } |
Chris Lattner | af16538 | 2005-09-13 22:03:06 +0000 | [diff] [blame] | 100 | |
| 101 | // Include the pieces autogenerated from the target description. |
| 102 | #include "PPC32GenDAGISel.inc" |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 103 | }; |
| 104 | } |
| 105 | |
Chris Lattner | 6cd40d5 | 2005-09-03 01:17:22 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 107 | /// getGlobalBaseReg - Output the instructions required to put the |
| 108 | /// base address to use for accessing globals into a register. |
| 109 | /// |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 110 | SDOperand PPC32DAGToDAGISel::getGlobalBaseReg() { |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 111 | if (!GlobalBaseReg) { |
| 112 | // Insert the set of GlobalBaseReg into the first MBB of the function |
| 113 | MachineBasicBlock &FirstMBB = BB->getParent()->front(); |
| 114 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
| 115 | SSARegMap *RegMap = BB->getParent()->getSSARegMap(); |
| 116 | GlobalBaseReg = RegMap->createVirtualRegister(PPC32::GPRCRegisterClass); |
| 117 | BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR); |
| 118 | BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg); |
| 119 | } |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 120 | return CurDAG->getRegister(GlobalBaseReg, MVT::i32); |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 124 | // isIntImmediate - This method tests to see if a constant operand. |
| 125 | // If so Imm will receive the 32 bit value. |
| 126 | static bool isIntImmediate(SDNode *N, unsigned& Imm) { |
| 127 | if (N->getOpcode() == ISD::Constant) { |
| 128 | Imm = cast<ConstantSDNode>(N)->getValue(); |
| 129 | return true; |
| 130 | } |
| 131 | return false; |
| 132 | } |
| 133 | |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 134 | // isOprShiftImm - Returns true if the specified operand is a shift opcode with |
| 135 | // a immediate shift count less than 32. |
| 136 | static bool isOprShiftImm(SDNode *N, unsigned& Opc, unsigned& SH) { |
| 137 | Opc = N->getOpcode(); |
| 138 | return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) && |
| 139 | isIntImmediate(N->getOperand(1).Val, SH) && SH < 32; |
| 140 | } |
| 141 | |
| 142 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 143 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 144 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 145 | // not, since all 1s are not contiguous. |
| 146 | static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) { |
| 147 | if (isShiftedMask_32(Val)) { |
| 148 | // look for the first non-zero bit |
| 149 | MB = CountLeadingZeros_32(Val); |
| 150 | // look for the first zero bit after the run of ones |
| 151 | ME = CountLeadingZeros_32((Val - 1) ^ Val); |
| 152 | return true; |
Chris Lattner | 2fe76e5 | 2005-08-25 04:47:18 +0000 | [diff] [blame] | 153 | } else { |
| 154 | Val = ~Val; // invert mask |
| 155 | if (isShiftedMask_32(Val)) { |
| 156 | // effectively look for the first zero bit |
| 157 | ME = CountLeadingZeros_32(Val) - 1; |
| 158 | // effectively look for the first one bit after the run of zeros |
| 159 | MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1; |
| 160 | return true; |
| 161 | } |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 162 | } |
| 163 | // no run present |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | // isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate |
| 168 | // and mask opcode and mask operation. |
| 169 | static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask, |
| 170 | unsigned &SH, unsigned &MB, unsigned &ME) { |
| 171 | unsigned Shift = 32; |
| 172 | unsigned Indeterminant = ~0; // bit mask marking indeterminant results |
| 173 | unsigned Opcode = N->getOpcode(); |
Chris Lattner | 1505573 | 2005-08-30 00:59:16 +0000 | [diff] [blame] | 174 | if (N->getNumOperands() != 2 || |
| 175 | !isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31)) |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 176 | return false; |
| 177 | |
| 178 | if (Opcode == ISD::SHL) { |
| 179 | // apply shift left to mask if it comes first |
| 180 | if (IsShiftMask) Mask = Mask << Shift; |
| 181 | // determine which bits are made indeterminant by shift |
| 182 | Indeterminant = ~(0xFFFFFFFFu << Shift); |
| 183 | } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) { |
| 184 | // apply shift right to mask if it comes first |
| 185 | if (IsShiftMask) Mask = Mask >> Shift; |
| 186 | // determine which bits are made indeterminant by shift |
| 187 | Indeterminant = ~(0xFFFFFFFFu >> Shift); |
| 188 | // adjust for the left rotate |
| 189 | Shift = 32 - Shift; |
| 190 | } else { |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | // if the mask doesn't intersect any Indeterminant bits |
| 195 | if (Mask && !(Mask & Indeterminant)) { |
| 196 | SH = Shift; |
| 197 | // make sure the mask is still a mask (wrap arounds may not be) |
| 198 | return isRunOfOnes(Mask, MB, ME); |
| 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 203 | // isOpcWithIntImmediate - This method tests to see if the node is a specific |
| 204 | // opcode and that it has a immediate integer right operand. |
| 205 | // If so Imm will receive the 32 bit value. |
| 206 | static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) { |
| 207 | return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm); |
| 208 | } |
| 209 | |
| 210 | // isOprNot - Returns true if the specified operand is an xor with immediate -1. |
| 211 | static bool isOprNot(SDNode *N) { |
| 212 | unsigned Imm; |
| 213 | return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1; |
| 214 | } |
| 215 | |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 216 | // Immediate constant composers. |
| 217 | // Lo16 - grabs the lo 16 bits from a 32 bit constant. |
| 218 | // Hi16 - grabs the hi 16 bits from a 32 bit constant. |
| 219 | // HA16 - computes the hi bits required if the lo bits are add/subtracted in |
| 220 | // arithmethically. |
| 221 | static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; } |
| 222 | static unsigned Hi16(unsigned x) { return Lo16(x >> 16); } |
| 223 | static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); } |
| 224 | |
| 225 | // isIntImmediate - This method tests to see if a constant operand. |
| 226 | // If so Imm will receive the 32 bit value. |
| 227 | static bool isIntImmediate(SDOperand N, unsigned& Imm) { |
| 228 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) { |
| 229 | Imm = (unsigned)CN->getSignExtended(); |
| 230 | return true; |
| 231 | } |
| 232 | return false; |
| 233 | } |
| 234 | |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 235 | /// SelectBitfieldInsert - turn an or of two masked values into |
| 236 | /// the rotate left word immediate then mask insert (rlwimi) instruction. |
| 237 | /// Returns true on success, false if the caller still needs to select OR. |
| 238 | /// |
| 239 | /// Patterns matched: |
| 240 | /// 1. or shl, and 5. or and, and |
| 241 | /// 2. or and, shl 6. or shl, shr |
| 242 | /// 3. or shr, and 7. or shr, shl |
| 243 | /// 4. or and, shr |
| 244 | SDNode *PPC32DAGToDAGISel::SelectBitfieldInsert(SDNode *N) { |
| 245 | bool IsRotate = false; |
| 246 | unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0; |
| 247 | unsigned Value; |
| 248 | |
| 249 | SDOperand Op0 = N->getOperand(0); |
| 250 | SDOperand Op1 = N->getOperand(1); |
| 251 | |
| 252 | unsigned Op0Opc = Op0.getOpcode(); |
| 253 | unsigned Op1Opc = Op1.getOpcode(); |
| 254 | |
| 255 | // Verify that we have the correct opcodes |
| 256 | if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc) |
| 257 | return false; |
| 258 | if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc) |
| 259 | return false; |
| 260 | |
| 261 | // Generate Mask value for Target |
| 262 | if (isIntImmediate(Op0.getOperand(1), Value)) { |
| 263 | switch(Op0Opc) { |
Chris Lattner | 1368721 | 2005-08-30 18:37:48 +0000 | [diff] [blame] | 264 | case ISD::SHL: TgtMask <<= Value; break; |
| 265 | case ISD::SRL: TgtMask >>= Value; break; |
| 266 | case ISD::AND: TgtMask &= Value; break; |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 267 | } |
| 268 | } else { |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | // Generate Mask value for Insert |
Chris Lattner | 1368721 | 2005-08-30 18:37:48 +0000 | [diff] [blame] | 273 | if (!isIntImmediate(Op1.getOperand(1), Value)) |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 274 | return 0; |
Chris Lattner | 1368721 | 2005-08-30 18:37:48 +0000 | [diff] [blame] | 275 | |
| 276 | switch(Op1Opc) { |
| 277 | case ISD::SHL: |
| 278 | SH = Value; |
| 279 | InsMask <<= SH; |
| 280 | if (Op0Opc == ISD::SRL) IsRotate = true; |
| 281 | break; |
| 282 | case ISD::SRL: |
| 283 | SH = Value; |
| 284 | InsMask >>= SH; |
| 285 | SH = 32-SH; |
| 286 | if (Op0Opc == ISD::SHL) IsRotate = true; |
| 287 | break; |
| 288 | case ISD::AND: |
| 289 | InsMask &= Value; |
| 290 | break; |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | // If both of the inputs are ANDs and one of them has a logical shift by |
| 294 | // constant as its input, make that AND the inserted value so that we can |
| 295 | // combine the shift into the rotate part of the rlwimi instruction |
| 296 | bool IsAndWithShiftOp = false; |
| 297 | if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) { |
| 298 | if (Op1.getOperand(0).getOpcode() == ISD::SHL || |
| 299 | Op1.getOperand(0).getOpcode() == ISD::SRL) { |
| 300 | if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) { |
| 301 | SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value; |
| 302 | IsAndWithShiftOp = true; |
| 303 | } |
| 304 | } else if (Op0.getOperand(0).getOpcode() == ISD::SHL || |
| 305 | Op0.getOperand(0).getOpcode() == ISD::SRL) { |
| 306 | if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) { |
| 307 | std::swap(Op0, Op1); |
| 308 | std::swap(TgtMask, InsMask); |
| 309 | SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value; |
| 310 | IsAndWithShiftOp = true; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // Verify that the Target mask and Insert mask together form a full word mask |
| 316 | // and that the Insert mask is a run of set bits (which implies both are runs |
| 317 | // of set bits). Given that, Select the arguments and generate the rlwimi |
| 318 | // instruction. |
| 319 | unsigned MB, ME; |
| 320 | if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) { |
| 321 | bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF; |
| 322 | bool Op0IsAND = Op0Opc == ISD::AND; |
| 323 | // Check for rotlwi / rotrwi here, a special case of bitfield insert |
| 324 | // where both bitfield halves are sourced from the same value. |
| 325 | if (IsRotate && fullMask && |
| 326 | N->getOperand(0).getOperand(0) == N->getOperand(1).getOperand(0)) { |
| 327 | Op0 = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, |
| 328 | Select(N->getOperand(0).getOperand(0)), |
| 329 | getI32Imm(SH), getI32Imm(0), getI32Imm(31)); |
| 330 | return Op0.Val; |
| 331 | } |
| 332 | SDOperand Tmp1 = (Op0IsAND && fullMask) ? Select(Op0.getOperand(0)) |
| 333 | : Select(Op0); |
| 334 | SDOperand Tmp2 = IsAndWithShiftOp ? Select(Op1.getOperand(0).getOperand(0)) |
| 335 | : Select(Op1.getOperand(0)); |
| 336 | Op0 = CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2, |
| 337 | getI32Imm(SH), getI32Imm(MB), getI32Imm(ME)); |
| 338 | return Op0.Val; |
| 339 | } |
| 340 | return 0; |
| 341 | } |
| 342 | |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 343 | // SelectIntImmediateExpr - Choose code for integer operations with an immediate |
| 344 | // operand. |
| 345 | SDNode *PPC32DAGToDAGISel::SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS, |
| 346 | unsigned OCHi, unsigned OCLo, |
| 347 | bool IsArithmetic, |
| 348 | bool Negate) { |
| 349 | // Check to make sure this is a constant. |
| 350 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS); |
| 351 | // Exit if not a constant. |
| 352 | if (!CN) return 0; |
| 353 | // Extract immediate. |
| 354 | unsigned C = (unsigned)CN->getValue(); |
| 355 | // Negate if required (ISD::SUB). |
| 356 | if (Negate) C = -C; |
| 357 | // Get the hi and lo portions of constant. |
| 358 | unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C); |
| 359 | unsigned Lo = Lo16(C); |
| 360 | |
| 361 | // If two instructions are needed and usage indicates it would be better to |
| 362 | // load immediate into a register, bail out. |
| 363 | if (Hi && Lo && CN->use_size() > 2) return false; |
| 364 | |
| 365 | // Select the first operand. |
| 366 | SDOperand Opr0 = Select(LHS); |
| 367 | |
| 368 | if (Lo) // Add in the lo-part. |
| 369 | Opr0 = CurDAG->getTargetNode(OCLo, MVT::i32, Opr0, getI32Imm(Lo)); |
| 370 | if (Hi) // Add in the hi-part. |
| 371 | Opr0 = CurDAG->getTargetNode(OCHi, MVT::i32, Opr0, getI32Imm(Hi)); |
| 372 | return Opr0.Val; |
| 373 | } |
| 374 | |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 375 | /// SelectAddr - Given the specified address, return the two operands for a |
| 376 | /// load/store instruction, and return true if it should be an indexed [r+r] |
| 377 | /// operation. |
| 378 | bool PPC32DAGToDAGISel::SelectAddr(SDOperand Addr, SDOperand &Op1, |
| 379 | SDOperand &Op2) { |
| 380 | unsigned imm = 0; |
| 381 | if (Addr.getOpcode() == ISD::ADD) { |
| 382 | if (isIntImmediate(Addr.getOperand(1), imm) && isInt16(imm)) { |
| 383 | Op1 = getI32Imm(Lo16(imm)); |
Chris Lattner | e28e40a | 2005-08-25 00:45:43 +0000 | [diff] [blame] | 384 | if (FrameIndexSDNode *FI = |
| 385 | dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) { |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 386 | ++FrameOff; |
Chris Lattner | e28e40a | 2005-08-25 00:45:43 +0000 | [diff] [blame] | 387 | Op2 = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32); |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 388 | } else { |
| 389 | Op2 = Select(Addr.getOperand(0)); |
| 390 | } |
| 391 | return false; |
| 392 | } else { |
| 393 | Op1 = Select(Addr.getOperand(0)); |
| 394 | Op2 = Select(Addr.getOperand(1)); |
| 395 | return true; // [r+r] |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // Now check if we're dealing with a global, and whether or not we should emit |
| 400 | // an optimized load or store for statics. |
| 401 | if (GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Addr)) { |
| 402 | GlobalValue *GV = GN->getGlobal(); |
| 403 | if (!GV->hasWeakLinkage() && !GV->isExternal()) { |
| 404 | Op1 = CurDAG->getTargetGlobalAddress(GV, MVT::i32); |
| 405 | if (PICEnabled) |
| 406 | Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(), |
| 407 | Op1); |
| 408 | else |
| 409 | Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1); |
| 410 | return false; |
| 411 | } |
Chris Lattner | e28e40a | 2005-08-25 00:45:43 +0000 | [diff] [blame] | 412 | } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Addr)) { |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 413 | Op1 = getI32Imm(0); |
Chris Lattner | e28e40a | 2005-08-25 00:45:43 +0000 | [diff] [blame] | 414 | Op2 = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32); |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 415 | return false; |
| 416 | } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Addr)) { |
| 417 | Op1 = Addr; |
| 418 | if (PICEnabled) |
| 419 | Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),Op1); |
| 420 | else |
| 421 | Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1); |
| 422 | return false; |
| 423 | } |
| 424 | Op1 = getI32Imm(0); |
| 425 | Op2 = Select(Addr); |
| 426 | return false; |
| 427 | } |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 428 | |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 429 | /// SelectCC - Select a comparison of the specified values with the specified |
| 430 | /// condition code, returning the CR# of the expression. |
| 431 | SDOperand PPC32DAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS, |
| 432 | ISD::CondCode CC) { |
| 433 | // Always select the LHS. |
| 434 | LHS = Select(LHS); |
| 435 | |
| 436 | // Use U to determine whether the SETCC immediate range is signed or not. |
| 437 | if (MVT::isInteger(LHS.getValueType())) { |
| 438 | bool U = ISD::isUnsignedIntSetCC(CC); |
| 439 | unsigned Imm; |
| 440 | if (isIntImmediate(RHS, Imm) && |
| 441 | ((U && isUInt16(Imm)) || (!U && isInt16(Imm)))) |
| 442 | return CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI, MVT::i32, |
| 443 | LHS, getI32Imm(Lo16(Imm))); |
| 444 | return CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32, |
| 445 | LHS, Select(RHS)); |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 446 | } else if (LHS.getValueType() == MVT::f32) { |
| 447 | return CurDAG->getTargetNode(PPC::FCMPUS, MVT::i32, LHS, Select(RHS)); |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 448 | } else { |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 449 | return CurDAG->getTargetNode(PPC::FCMPUD, MVT::i32, LHS, Select(RHS)); |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
| 453 | /// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding |
| 454 | /// to Condition. |
| 455 | static unsigned getBCCForSetCC(ISD::CondCode CC) { |
| 456 | switch (CC) { |
| 457 | default: assert(0 && "Unknown condition!"); abort(); |
| 458 | case ISD::SETEQ: return PPC::BEQ; |
| 459 | case ISD::SETNE: return PPC::BNE; |
| 460 | case ISD::SETULT: |
| 461 | case ISD::SETLT: return PPC::BLT; |
| 462 | case ISD::SETULE: |
| 463 | case ISD::SETLE: return PPC::BLE; |
| 464 | case ISD::SETUGT: |
| 465 | case ISD::SETGT: return PPC::BGT; |
| 466 | case ISD::SETUGE: |
| 467 | case ISD::SETGE: return PPC::BGE; |
| 468 | } |
| 469 | return 0; |
| 470 | } |
| 471 | |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 472 | /// getCRIdxForSetCC - Return the index of the condition register field |
| 473 | /// associated with the SetCC condition, and whether or not the field is |
| 474 | /// treated as inverted. That is, lt = 0; ge = 0 inverted. |
| 475 | static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) { |
| 476 | switch (CC) { |
| 477 | default: assert(0 && "Unknown condition!"); abort(); |
| 478 | case ISD::SETULT: |
| 479 | case ISD::SETLT: Inv = false; return 0; |
| 480 | case ISD::SETUGE: |
| 481 | case ISD::SETGE: Inv = true; return 0; |
| 482 | case ISD::SETUGT: |
| 483 | case ISD::SETGT: Inv = false; return 1; |
| 484 | case ISD::SETULE: |
| 485 | case ISD::SETLE: Inv = true; return 1; |
| 486 | case ISD::SETEQ: Inv = false; return 2; |
| 487 | case ISD::SETNE: Inv = true; return 2; |
| 488 | } |
| 489 | return 0; |
| 490 | } |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 491 | |
Chris Lattner | 047b952 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 492 | // Structure used to return the necessary information to codegen an SDIV as |
| 493 | // a multiply. |
| 494 | struct ms { |
| 495 | int m; // magic number |
| 496 | int s; // shift amount |
| 497 | }; |
| 498 | |
| 499 | struct mu { |
| 500 | unsigned int m; // magic number |
| 501 | int a; // add indicator |
| 502 | int s; // shift amount |
| 503 | }; |
| 504 | |
| 505 | /// magic - calculate the magic numbers required to codegen an integer sdiv as |
| 506 | /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1, |
| 507 | /// or -1. |
| 508 | static struct ms magic(int d) { |
| 509 | int p; |
| 510 | unsigned int ad, anc, delta, q1, r1, q2, r2, t; |
| 511 | const unsigned int two31 = 0x80000000U; |
| 512 | struct ms mag; |
| 513 | |
| 514 | ad = abs(d); |
| 515 | t = two31 + ((unsigned int)d >> 31); |
| 516 | anc = t - 1 - t%ad; // absolute value of nc |
| 517 | p = 31; // initialize p |
| 518 | q1 = two31/anc; // initialize q1 = 2p/abs(nc) |
| 519 | r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc)) |
| 520 | q2 = two31/ad; // initialize q2 = 2p/abs(d) |
| 521 | r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d)) |
| 522 | do { |
| 523 | p = p + 1; |
| 524 | q1 = 2*q1; // update q1 = 2p/abs(nc) |
| 525 | r1 = 2*r1; // update r1 = rem(2p/abs(nc)) |
| 526 | if (r1 >= anc) { // must be unsigned comparison |
| 527 | q1 = q1 + 1; |
| 528 | r1 = r1 - anc; |
| 529 | } |
| 530 | q2 = 2*q2; // update q2 = 2p/abs(d) |
| 531 | r2 = 2*r2; // update r2 = rem(2p/abs(d)) |
| 532 | if (r2 >= ad) { // must be unsigned comparison |
| 533 | q2 = q2 + 1; |
| 534 | r2 = r2 - ad; |
| 535 | } |
| 536 | delta = ad - r2; |
| 537 | } while (q1 < delta || (q1 == delta && r1 == 0)); |
| 538 | |
| 539 | mag.m = q2 + 1; |
| 540 | if (d < 0) mag.m = -mag.m; // resulting magic number |
| 541 | mag.s = p - 32; // resulting shift |
| 542 | return mag; |
| 543 | } |
| 544 | |
| 545 | /// magicu - calculate the magic numbers required to codegen an integer udiv as |
| 546 | /// a sequence of multiply, add and shifts. Requires that the divisor not be 0. |
| 547 | static struct mu magicu(unsigned d) |
| 548 | { |
| 549 | int p; |
| 550 | unsigned int nc, delta, q1, r1, q2, r2; |
| 551 | struct mu magu; |
| 552 | magu.a = 0; // initialize "add" indicator |
| 553 | nc = - 1 - (-d)%d; |
| 554 | p = 31; // initialize p |
| 555 | q1 = 0x80000000/nc; // initialize q1 = 2p/nc |
| 556 | r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc) |
| 557 | q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d |
| 558 | r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d) |
| 559 | do { |
| 560 | p = p + 1; |
| 561 | if (r1 >= nc - r1 ) { |
| 562 | q1 = 2*q1 + 1; // update q1 |
| 563 | r1 = 2*r1 - nc; // update r1 |
| 564 | } |
| 565 | else { |
| 566 | q1 = 2*q1; // update q1 |
| 567 | r1 = 2*r1; // update r1 |
| 568 | } |
| 569 | if (r2 + 1 >= d - r2) { |
| 570 | if (q2 >= 0x7FFFFFFF) magu.a = 1; |
| 571 | q2 = 2*q2 + 1; // update q2 |
| 572 | r2 = 2*r2 + 1 - d; // update r2 |
| 573 | } |
| 574 | else { |
| 575 | if (q2 >= 0x80000000) magu.a = 1; |
| 576 | q2 = 2*q2; // update q2 |
| 577 | r2 = 2*r2 + 1; // update r2 |
| 578 | } |
| 579 | delta = d - 1 - r2; |
| 580 | } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0))); |
| 581 | magu.m = q2 + 1; // resulting magic number |
| 582 | magu.s = p - 32; // resulting shift |
| 583 | return magu; |
| 584 | } |
| 585 | |
| 586 | /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant, |
| 587 | /// return a DAG expression to select that will generate the same value by |
| 588 | /// multiplying by a magic number. See: |
| 589 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 590 | SDOperand PPC32DAGToDAGISel::BuildSDIVSequence(SDNode *N) { |
| 591 | int d = (int)cast<ConstantSDNode>(N->getOperand(1))->getValue(); |
| 592 | ms magics = magic(d); |
| 593 | // Multiply the numerator (operand 0) by the magic value |
| 594 | SDOperand Q = CurDAG->getNode(ISD::MULHS, MVT::i32, N->getOperand(0), |
| 595 | CurDAG->getConstant(magics.m, MVT::i32)); |
| 596 | // If d > 0 and m < 0, add the numerator |
| 597 | if (d > 0 && magics.m < 0) |
| 598 | Q = CurDAG->getNode(ISD::ADD, MVT::i32, Q, N->getOperand(0)); |
| 599 | // If d < 0 and m > 0, subtract the numerator. |
| 600 | if (d < 0 && magics.m > 0) |
| 601 | Q = CurDAG->getNode(ISD::SUB, MVT::i32, Q, N->getOperand(0)); |
| 602 | // Shift right algebraic if shift value is nonzero |
| 603 | if (magics.s > 0) |
| 604 | Q = CurDAG->getNode(ISD::SRA, MVT::i32, Q, |
| 605 | CurDAG->getConstant(magics.s, MVT::i32)); |
| 606 | // Extract the sign bit and add it to the quotient |
| 607 | SDOperand T = |
| 608 | CurDAG->getNode(ISD::SRL, MVT::i32, Q, CurDAG->getConstant(31, MVT::i32)); |
| 609 | return CurDAG->getNode(ISD::ADD, MVT::i32, Q, T); |
| 610 | } |
| 611 | |
| 612 | /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant, |
| 613 | /// return a DAG expression to select that will generate the same value by |
| 614 | /// multiplying by a magic number. See: |
| 615 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 616 | SDOperand PPC32DAGToDAGISel::BuildUDIVSequence(SDNode *N) { |
| 617 | unsigned d = (unsigned)cast<ConstantSDNode>(N->getOperand(1))->getValue(); |
| 618 | mu magics = magicu(d); |
| 619 | // Multiply the numerator (operand 0) by the magic value |
| 620 | SDOperand Q = CurDAG->getNode(ISD::MULHU, MVT::i32, N->getOperand(0), |
| 621 | CurDAG->getConstant(magics.m, MVT::i32)); |
| 622 | if (magics.a == 0) { |
| 623 | return CurDAG->getNode(ISD::SRL, MVT::i32, Q, |
| 624 | CurDAG->getConstant(magics.s, MVT::i32)); |
| 625 | } else { |
| 626 | SDOperand NPQ = CurDAG->getNode(ISD::SUB, MVT::i32, N->getOperand(0), Q); |
| 627 | NPQ = CurDAG->getNode(ISD::SRL, MVT::i32, NPQ, |
| 628 | CurDAG->getConstant(1, MVT::i32)); |
| 629 | NPQ = CurDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q); |
| 630 | return CurDAG->getNode(ISD::SRL, MVT::i32, NPQ, |
| 631 | CurDAG->getConstant(magics.s-1, MVT::i32)); |
| 632 | } |
| 633 | } |
| 634 | |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 635 | // Select - Convert the specified operand from a target-independent to a |
| 636 | // target-specific node if it hasn't already been changed. |
| 637 | SDOperand PPC32DAGToDAGISel::Select(SDOperand Op) { |
| 638 | SDNode *N = Op.Val; |
Chris Lattner | 0bbea95 | 2005-08-26 20:25:03 +0000 | [diff] [blame] | 639 | if (N->getOpcode() >= ISD::BUILTIN_OP_END && |
| 640 | N->getOpcode() < PPCISD::FIRST_NUMBER) |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 641 | return Op; // Already selected. |
Chris Lattner | d3d2cf5 | 2005-09-29 00:59:32 +0000 | [diff] [blame] | 642 | |
| 643 | // If this has already been converted, use it. |
| 644 | std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op); |
| 645 | if (CGMI != CodeGenMap.end()) return CGMI->second; |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 646 | |
| 647 | switch (N->getOpcode()) { |
Chris Lattner | 19c0907 | 2005-09-07 23:45:15 +0000 | [diff] [blame] | 648 | default: break; |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 649 | case ISD::TokenFactor: { |
| 650 | SDOperand New; |
| 651 | if (N->getNumOperands() == 2) { |
| 652 | SDOperand Op0 = Select(N->getOperand(0)); |
| 653 | SDOperand Op1 = Select(N->getOperand(1)); |
| 654 | New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1); |
| 655 | } else { |
| 656 | std::vector<SDOperand> Ops; |
| 657 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
Chris Lattner | 7e65997 | 2005-08-19 21:33:02 +0000 | [diff] [blame] | 658 | Ops.push_back(Select(N->getOperand(i))); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 659 | New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops); |
| 660 | } |
| 661 | |
Chris Lattner | d3d2cf5 | 2005-09-29 00:59:32 +0000 | [diff] [blame] | 662 | if (!N->hasOneUse()) CodeGenMap[Op] = New; |
| 663 | return New; |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 664 | } |
| 665 | case ISD::CopyFromReg: { |
| 666 | SDOperand Chain = Select(N->getOperand(0)); |
| 667 | if (Chain == N->getOperand(0)) return Op; // No change |
| 668 | SDOperand New = CurDAG->getCopyFromReg(Chain, |
| 669 | cast<RegisterSDNode>(N->getOperand(1))->getReg(), N->getValueType(0)); |
| 670 | return New.getValue(Op.ResNo); |
| 671 | } |
| 672 | case ISD::CopyToReg: { |
| 673 | SDOperand Chain = Select(N->getOperand(0)); |
| 674 | SDOperand Reg = N->getOperand(1); |
| 675 | SDOperand Val = Select(N->getOperand(2)); |
Chris Lattner | d3d2cf5 | 2005-09-29 00:59:32 +0000 | [diff] [blame] | 676 | SDOperand New = CurDAG->getNode(ISD::CopyToReg, MVT::Other, |
| 677 | Chain, Reg, Val); |
| 678 | if (!N->hasOneUse()) CodeGenMap[Op] = New; |
| 679 | return New; |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 680 | } |
Chris Lattner | 2b54400 | 2005-08-24 23:08:16 +0000 | [diff] [blame] | 681 | case ISD::UNDEF: |
| 682 | if (N->getValueType(0) == MVT::i32) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 683 | CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_GPR, MVT::i32); |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 684 | else if (N->getValueType(0) == MVT::f32) |
| 685 | CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_F4, MVT::f32); |
| 686 | else |
| 687 | CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_F8, MVT::f64); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 688 | return SDOperand(N, 0); |
Chris Lattner | e28e40a | 2005-08-25 00:45:43 +0000 | [diff] [blame] | 689 | case ISD::FrameIndex: { |
| 690 | int FI = cast<FrameIndexSDNode>(N)->getIndex(); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 691 | CurDAG->SelectNodeTo(N, PPC::ADDI, MVT::i32, |
Chris Lattner | e28e40a | 2005-08-25 00:45:43 +0000 | [diff] [blame] | 692 | CurDAG->getTargetFrameIndex(FI, MVT::i32), |
| 693 | getI32Imm(0)); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 694 | return SDOperand(N, 0); |
Chris Lattner | e28e40a | 2005-08-25 00:45:43 +0000 | [diff] [blame] | 695 | } |
Chris Lattner | 34e1705 | 2005-08-25 05:04:11 +0000 | [diff] [blame] | 696 | case ISD::ConstantPool: { |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 697 | Constant *C = cast<ConstantPoolSDNode>(N)->get(); |
| 698 | SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i32); |
Chris Lattner | 34e1705 | 2005-08-25 05:04:11 +0000 | [diff] [blame] | 699 | if (PICEnabled) |
| 700 | Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),CPI); |
| 701 | else |
| 702 | Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, CPI); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 703 | CurDAG->SelectNodeTo(N, PPC::LA, MVT::i32, Tmp, CPI); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 704 | return SDOperand(N, 0); |
Chris Lattner | 34e1705 | 2005-08-25 05:04:11 +0000 | [diff] [blame] | 705 | } |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 706 | case ISD::GlobalAddress: { |
| 707 | GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
| 708 | SDOperand Tmp; |
| 709 | SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i32); |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 710 | if (PICEnabled) |
| 711 | Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(), GA); |
| 712 | else |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 713 | Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, GA); |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 714 | |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 715 | if (GV->hasWeakLinkage() || GV->isExternal()) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 716 | CurDAG->SelectNodeTo(N, PPC::LWZ, MVT::i32, GA, Tmp); |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 717 | else |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 718 | CurDAG->SelectNodeTo(N, PPC::LA, MVT::i32, Tmp, GA); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 719 | return SDOperand(N, 0); |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 720 | } |
Chris Lattner | 9c2dece | 2005-08-29 23:30:11 +0000 | [diff] [blame] | 721 | case ISD::DYNAMIC_STACKALLOC: { |
| 722 | // FIXME: We are currently ignoring the requested alignment for handling |
| 723 | // greater than the stack alignment. This will need to be revisited at some |
| 724 | // point. Align = N.getOperand(2); |
| 725 | if (!isa<ConstantSDNode>(N->getOperand(2)) || |
| 726 | cast<ConstantSDNode>(N->getOperand(2))->getValue() != 0) { |
| 727 | std::cerr << "Cannot allocate stack object with greater alignment than" |
| 728 | << " the stack alignment yet!"; |
| 729 | abort(); |
| 730 | } |
| 731 | SDOperand Chain = Select(N->getOperand(0)); |
| 732 | SDOperand Amt = Select(N->getOperand(1)); |
| 733 | |
| 734 | SDOperand R1Reg = CurDAG->getRegister(PPC::R1, MVT::i32); |
| 735 | |
Chris Lattner | 75592e4 | 2005-09-01 21:31:30 +0000 | [diff] [blame] | 736 | SDOperand R1Val = CurDAG->getCopyFromReg(Chain, PPC::R1, MVT::i32); |
| 737 | Chain = R1Val.getValue(1); |
| 738 | |
Chris Lattner | 9c2dece | 2005-08-29 23:30:11 +0000 | [diff] [blame] | 739 | // Subtract the amount (guaranteed to be a multiple of the stack alignment) |
| 740 | // from the stack pointer, giving us the result pointer. |
Chris Lattner | 75592e4 | 2005-09-01 21:31:30 +0000 | [diff] [blame] | 741 | SDOperand Result = CurDAG->getTargetNode(PPC::SUBF, MVT::i32, Amt, R1Val); |
Chris Lattner | 9c2dece | 2005-08-29 23:30:11 +0000 | [diff] [blame] | 742 | |
| 743 | // Copy this result back into R1. |
| 744 | Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R1Reg, Result); |
| 745 | |
| 746 | // Copy this result back out of R1 to make sure we're not using the stack |
| 747 | // space without decrementing the stack pointer. |
| 748 | Result = CurDAG->getCopyFromReg(Chain, PPC::R1, MVT::i32); |
| 749 | |
| 750 | // Finally, replace the DYNAMIC_STACKALLOC with the copyfromreg. |
Chris Lattner | d3d2cf5 | 2005-09-29 00:59:32 +0000 | [diff] [blame] | 751 | CodeGenMap[Op.getValue(0)] = Result; |
| 752 | CodeGenMap[Op.getValue(1)] = Result.getValue(1); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 753 | return SDOperand(Result.Val, Op.ResNo); |
Chris Lattner | 9c2dece | 2005-08-29 23:30:11 +0000 | [diff] [blame] | 754 | } |
Chris Lattner | 867940d | 2005-10-02 06:58:23 +0000 | [diff] [blame] | 755 | case PPCISD::FSEL: { |
Chris Lattner | 43f07a4 | 2005-10-02 07:07:49 +0000 | [diff] [blame] | 756 | SDOperand Comparison = Select(N->getOperand(0)); |
| 757 | // Extend the comparison to 64-bits. |
| 758 | if (Comparison.getValueType() == MVT::f32) |
| 759 | Comparison = CurDAG->getTargetNode(PPC::FMRSD, MVT::f64, Comparison); |
| 760 | |
| 761 | unsigned Opc = N->getValueType(0) == MVT::f32 ? PPC::FSELS : PPC::FSELD; |
| 762 | CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Comparison, |
| 763 | Select(N->getOperand(1)), Select(N->getOperand(2))); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 764 | return SDOperand(N, 0); |
Chris Lattner | 867940d | 2005-10-02 06:58:23 +0000 | [diff] [blame] | 765 | } |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 766 | case PPCISD::FCFID: |
| 767 | CurDAG->SelectNodeTo(N, PPC::FCFID, N->getValueType(0), |
| 768 | Select(N->getOperand(0))); |
| 769 | return SDOperand(N, 0); |
| 770 | case PPCISD::FCTIDZ: |
| 771 | CurDAG->SelectNodeTo(N, PPC::FCTIDZ, N->getValueType(0), |
| 772 | Select(N->getOperand(0))); |
| 773 | return SDOperand(N, 0); |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 774 | case PPCISD::FCTIWZ: |
| 775 | CurDAG->SelectNodeTo(N, PPC::FCTIWZ, N->getValueType(0), |
| 776 | Select(N->getOperand(0))); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 777 | return SDOperand(N, 0); |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 778 | case ISD::FADD: { |
| 779 | MVT::ValueType Ty = N->getValueType(0); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 780 | if (!NoExcessFPPrecision) { // Match FMA ops |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 781 | if (N->getOperand(0).getOpcode() == ISD::FMUL && |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 782 | N->getOperand(0).Val->hasOneUse()) { |
| 783 | ++FusedFP; // Statistic |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 784 | CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, Ty, |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 785 | Select(N->getOperand(0).getOperand(0)), |
| 786 | Select(N->getOperand(0).getOperand(1)), |
| 787 | Select(N->getOperand(1))); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 788 | return SDOperand(N, 0); |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 789 | } else if (N->getOperand(1).getOpcode() == ISD::FMUL && |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 790 | N->getOperand(1).hasOneUse()) { |
| 791 | ++FusedFP; // Statistic |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 792 | CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, Ty, |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 793 | Select(N->getOperand(1).getOperand(0)), |
| 794 | Select(N->getOperand(1).getOperand(1)), |
| 795 | Select(N->getOperand(0))); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 796 | return SDOperand(N, 0); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 800 | CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FADD : PPC::FADDS, Ty, |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 801 | Select(N->getOperand(0)), Select(N->getOperand(1))); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 802 | return SDOperand(N, 0); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 803 | } |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 804 | case ISD::FSUB: { |
| 805 | MVT::ValueType Ty = N->getValueType(0); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 806 | |
| 807 | if (!NoExcessFPPrecision) { // Match FMA ops |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 808 | if (N->getOperand(0).getOpcode() == ISD::FMUL && |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 809 | N->getOperand(0).Val->hasOneUse()) { |
| 810 | ++FusedFP; // Statistic |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 811 | CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS, Ty, |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 812 | Select(N->getOperand(0).getOperand(0)), |
| 813 | Select(N->getOperand(0).getOperand(1)), |
| 814 | Select(N->getOperand(1))); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 815 | return SDOperand(N, 0); |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 816 | } else if (N->getOperand(1).getOpcode() == ISD::FMUL && |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 817 | N->getOperand(1).Val->hasOneUse()) { |
| 818 | ++FusedFP; // Statistic |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 819 | CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS, Ty, |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 820 | Select(N->getOperand(1).getOperand(0)), |
| 821 | Select(N->getOperand(1).getOperand(1)), |
| 822 | Select(N->getOperand(0))); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 823 | return SDOperand(N, 0); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 824 | } |
| 825 | } |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 826 | CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FSUB : PPC::FSUBS, Ty, |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 827 | Select(N->getOperand(0)), |
| 828 | Select(N->getOperand(1))); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 829 | return SDOperand(N, 0); |
Nate Begeman | 2665350 | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 830 | } |
Chris Lattner | 88add10 | 2005-09-28 22:50:24 +0000 | [diff] [blame] | 831 | case ISD::SDIV: { |
Chris Lattner | 8784a23 | 2005-08-25 17:50:06 +0000 | [diff] [blame] | 832 | unsigned Imm; |
| 833 | if (isIntImmediate(N->getOperand(1), Imm)) { |
| 834 | if ((signed)Imm > 0 && isPowerOf2_32(Imm)) { |
| 835 | SDOperand Op = |
| 836 | CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag, |
| 837 | Select(N->getOperand(0)), |
| 838 | getI32Imm(Log2_32(Imm))); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 839 | CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, |
Chris Lattner | 8784a23 | 2005-08-25 17:50:06 +0000 | [diff] [blame] | 840 | Op.getValue(0), Op.getValue(1)); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 841 | return SDOperand(N, 0); |
Chris Lattner | 8784a23 | 2005-08-25 17:50:06 +0000 | [diff] [blame] | 842 | } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) { |
| 843 | SDOperand Op = |
Chris Lattner | 2501d5e | 2005-08-30 17:13:58 +0000 | [diff] [blame] | 844 | CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag, |
Chris Lattner | 8784a23 | 2005-08-25 17:50:06 +0000 | [diff] [blame] | 845 | Select(N->getOperand(0)), |
| 846 | getI32Imm(Log2_32(-Imm))); |
| 847 | SDOperand PT = |
Chris Lattner | 2501d5e | 2005-08-30 17:13:58 +0000 | [diff] [blame] | 848 | CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, Op.getValue(0), |
| 849 | Op.getValue(1)); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 850 | CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 851 | return SDOperand(N, 0); |
Chris Lattner | 047b952 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 852 | } else if (Imm) { |
| 853 | SDOperand Result = Select(BuildSDIVSequence(N)); |
Chris Lattner | d3d2cf5 | 2005-09-29 00:59:32 +0000 | [diff] [blame] | 854 | CodeGenMap[Op] = Result; |
| 855 | return Result; |
Chris Lattner | 8784a23 | 2005-08-25 17:50:06 +0000 | [diff] [blame] | 856 | } |
| 857 | } |
Chris Lattner | 047b952 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 858 | |
Chris Lattner | 237733e | 2005-09-29 23:33:31 +0000 | [diff] [blame] | 859 | // Other cases are autogenerated. |
| 860 | break; |
Chris Lattner | 047b952 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 861 | } |
| 862 | case ISD::UDIV: { |
| 863 | // If this is a divide by constant, we can emit code using some magic |
| 864 | // constants to implement it as a multiply instead. |
| 865 | unsigned Imm; |
Chris Lattner | a9317ed | 2005-08-25 23:21:06 +0000 | [diff] [blame] | 866 | if (isIntImmediate(N->getOperand(1), Imm) && Imm) { |
Chris Lattner | 047b952 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 867 | SDOperand Result = Select(BuildUDIVSequence(N)); |
Chris Lattner | d3d2cf5 | 2005-09-29 00:59:32 +0000 | [diff] [blame] | 868 | CodeGenMap[Op] = Result; |
| 869 | return Result; |
Chris Lattner | 047b952 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Chris Lattner | 237733e | 2005-09-29 23:33:31 +0000 | [diff] [blame] | 872 | // Other cases are autogenerated. |
| 873 | break; |
Chris Lattner | 047b952 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 874 | } |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 875 | case ISD::AND: { |
Nate Begeman | a694047 | 2005-08-18 18:01:39 +0000 | [diff] [blame] | 876 | unsigned Imm; |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 877 | // If this is an and of a value rotated between 0 and 31 bits and then and'd |
| 878 | // with a mask, emit rlwinm |
| 879 | if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) || |
| 880 | isShiftedMask_32(~Imm))) { |
| 881 | SDOperand Val; |
Nate Begeman | a694047 | 2005-08-18 18:01:39 +0000 | [diff] [blame] | 882 | unsigned SH, MB, ME; |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 883 | if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) { |
| 884 | Val = Select(N->getOperand(0).getOperand(0)); |
| 885 | } else { |
| 886 | Val = Select(N->getOperand(0)); |
| 887 | isRunOfOnes(Imm, MB, ME); |
| 888 | SH = 0; |
| 889 | } |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 890 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val, getI32Imm(SH), |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 891 | getI32Imm(MB), getI32Imm(ME)); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 892 | return SDOperand(N, 0); |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 893 | } |
Chris Lattner | 237733e | 2005-09-29 23:33:31 +0000 | [diff] [blame] | 894 | |
| 895 | // Other cases are autogenerated. |
| 896 | break; |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 897 | } |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 898 | case ISD::OR: |
Chris Lattner | d3d2cf5 | 2005-09-29 00:59:32 +0000 | [diff] [blame] | 899 | if (SDNode *I = SelectBitfieldInsert(N)) |
| 900 | return CodeGenMap[Op] = SDOperand(I, 0); |
| 901 | |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 902 | if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), |
| 903 | N->getOperand(1), |
Chris Lattner | d3d2cf5 | 2005-09-29 00:59:32 +0000 | [diff] [blame] | 904 | PPC::ORIS, PPC::ORI)) |
| 905 | return CodeGenMap[Op] = SDOperand(I, 0); |
| 906 | |
Chris Lattner | 237733e | 2005-09-29 23:33:31 +0000 | [diff] [blame] | 907 | // Other cases are autogenerated. |
| 908 | break; |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 909 | case ISD::SHL: { |
| 910 | unsigned Imm, SH, MB, ME; |
| 911 | if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) && |
| 912 | isRotateAndMask(N, Imm, true, SH, MB, ME)) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 913 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 914 | Select(N->getOperand(0).getOperand(0)), |
| 915 | getI32Imm(SH), getI32Imm(MB), getI32Imm(ME)); |
| 916 | else if (isIntImmediate(N->getOperand(1), Imm)) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 917 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Select(N->getOperand(0)), |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 918 | getI32Imm(Imm), getI32Imm(0), getI32Imm(31-Imm)); |
| 919 | else |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 920 | CurDAG->SelectNodeTo(N, PPC::SLW, MVT::i32, Select(N->getOperand(0)), |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 921 | Select(N->getOperand(1))); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 922 | return SDOperand(N, 0); |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 923 | } |
| 924 | case ISD::SRL: { |
| 925 | unsigned Imm, SH, MB, ME; |
| 926 | if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) && |
| 927 | isRotateAndMask(N, Imm, true, SH, MB, ME)) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 928 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 929 | Select(N->getOperand(0).getOperand(0)), |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 930 | getI32Imm(SH & 0x1F), getI32Imm(MB), getI32Imm(ME)); |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 931 | else if (isIntImmediate(N->getOperand(1), Imm)) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 932 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Select(N->getOperand(0)), |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 933 | getI32Imm((32-Imm) & 0x1F), getI32Imm(Imm), |
| 934 | getI32Imm(31)); |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 935 | else |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 936 | CurDAG->SelectNodeTo(N, PPC::SRW, MVT::i32, Select(N->getOperand(0)), |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 937 | Select(N->getOperand(1))); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 938 | return SDOperand(N, 0); |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 939 | } |
| 940 | case ISD::SRA: { |
| 941 | unsigned Imm, SH, MB, ME; |
| 942 | if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) && |
| 943 | isRotateAndMask(N, Imm, true, SH, MB, ME)) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 944 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 945 | Select(N->getOperand(0).getOperand(0)), |
| 946 | getI32Imm(SH), getI32Imm(MB), getI32Imm(ME)); |
| 947 | else if (isIntImmediate(N->getOperand(1), Imm)) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 948 | CurDAG->SelectNodeTo(N, PPC::SRAWI, MVT::i32, Select(N->getOperand(0)), |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 949 | getI32Imm(Imm)); |
| 950 | else |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 951 | CurDAG->SelectNodeTo(N, PPC::SRAW, MVT::i32, Select(N->getOperand(0)), |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 952 | Select(N->getOperand(1))); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 953 | return SDOperand(N, 0); |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 954 | } |
Chris Lattner | d8ead9e | 2005-09-28 22:53:16 +0000 | [diff] [blame] | 955 | case ISD::FMUL: { |
| 956 | unsigned Opc = N->getValueType(0) == MVT::f32 ? PPC::FMULS : PPC::FMUL; |
| 957 | CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Select(N->getOperand(0)), |
| 958 | Select(N->getOperand(1))); |
| 959 | return SDOperand(N, 0); |
| 960 | } |
| 961 | case ISD::FDIV: { |
| 962 | unsigned Opc = N->getValueType(0) == MVT::f32 ? PPC::FDIVS : PPC::FDIV; |
| 963 | CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Select(N->getOperand(0)), |
| 964 | Select(N->getOperand(1))); |
| 965 | return SDOperand(N, 0); |
| 966 | } |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 967 | case ISD::FABS: |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 968 | if (N->getValueType(0) == MVT::f32) |
| 969 | CurDAG->SelectNodeTo(N, PPC::FABSS, MVT::f32, Select(N->getOperand(0))); |
| 970 | else |
| 971 | CurDAG->SelectNodeTo(N, PPC::FABSD, MVT::f64, Select(N->getOperand(0))); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 972 | return SDOperand(N, 0); |
Chris Lattner | 8f83872 | 2005-08-30 00:30:43 +0000 | [diff] [blame] | 973 | case ISD::FP_EXTEND: |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 974 | assert(MVT::f64 == N->getValueType(0) && |
| 975 | MVT::f32 == N->getOperand(0).getValueType() && "Illegal FP_EXTEND"); |
Chris Lattner | 8f83872 | 2005-08-30 00:30:43 +0000 | [diff] [blame] | 976 | // We need to emit an FMR to make sure that the result has the right value |
| 977 | // type. |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 978 | CurDAG->SelectNodeTo(N, PPC::FMRSD, MVT::f64, Select(N->getOperand(0))); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 979 | return SDOperand(N, 0); |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 980 | case ISD::FP_ROUND: |
| 981 | assert(MVT::f32 == N->getValueType(0) && |
| 982 | MVT::f64 == N->getOperand(0).getValueType() && "Illegal FP_ROUND"); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 983 | CurDAG->SelectNodeTo(N, PPC::FRSP, MVT::f32, Select(N->getOperand(0))); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 984 | return SDOperand(N, 0); |
Nate Begeman | 2665350 | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 985 | case ISD::FNEG: { |
| 986 | SDOperand Val = Select(N->getOperand(0)); |
| 987 | MVT::ValueType Ty = N->getValueType(0); |
| 988 | if (Val.Val->hasOneUse()) { |
| 989 | unsigned Opc; |
Chris Lattner | 528f58e | 2005-08-28 23:39:22 +0000 | [diff] [blame] | 990 | switch (Val.isTargetOpcode() ? Val.getTargetOpcode() : 0) { |
Nate Begeman | 2665350 | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 991 | default: Opc = 0; break; |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 992 | case PPC::FABSS: Opc = PPC::FNABSS; break; |
| 993 | case PPC::FABSD: Opc = PPC::FNABSD; break; |
Nate Begeman | 2665350 | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 994 | case PPC::FMADD: Opc = PPC::FNMADD; break; |
| 995 | case PPC::FMADDS: Opc = PPC::FNMADDS; break; |
| 996 | case PPC::FMSUB: Opc = PPC::FNMSUB; break; |
| 997 | case PPC::FMSUBS: Opc = PPC::FNMSUBS; break; |
| 998 | } |
| 999 | // If we inverted the opcode, then emit the new instruction with the |
| 1000 | // inverted opcode and the original instruction's operands. Otherwise, |
| 1001 | // fall through and generate a fneg instruction. |
| 1002 | if (Opc) { |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1003 | if (Opc == PPC::FNABSS || Opc == PPC::FNABSD) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1004 | CurDAG->SelectNodeTo(N, Opc, Ty, Val.getOperand(0)); |
Nate Begeman | 2665350 | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 1005 | else |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1006 | CurDAG->SelectNodeTo(N, Opc, Ty, Val.getOperand(0), |
Nate Begeman | 2665350 | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 1007 | Val.getOperand(1), Val.getOperand(2)); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 1008 | return SDOperand(N, 0); |
Nate Begeman | 2665350 | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 1009 | } |
| 1010 | } |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1011 | if (Ty == MVT::f32) |
| 1012 | CurDAG->SelectNodeTo(N, PPC::FNEGS, MVT::f32, Val); |
| 1013 | else |
Chris Lattner | 2c1760f | 2005-10-01 02:51:36 +0000 | [diff] [blame] | 1014 | CurDAG->SelectNodeTo(N, PPC::FNEGD, MVT::f64, Val); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 1015 | return SDOperand(N, 0); |
Nate Begeman | 2665350 | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 1016 | } |
Nate Begeman | 6a7d611 | 2005-08-18 00:53:47 +0000 | [diff] [blame] | 1017 | case ISD::FSQRT: { |
| 1018 | MVT::ValueType Ty = N->getValueType(0); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1019 | CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS, Ty, |
Nate Begeman | 6a7d611 | 2005-08-18 00:53:47 +0000 | [diff] [blame] | 1020 | Select(N->getOperand(0))); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 1021 | return SDOperand(N, 0); |
Nate Begeman | 6a7d611 | 2005-08-18 00:53:47 +0000 | [diff] [blame] | 1022 | } |
Chris Lattner | a9317ed | 2005-08-25 23:21:06 +0000 | [diff] [blame] | 1023 | |
| 1024 | case ISD::ADD_PARTS: { |
| 1025 | SDOperand LHSL = Select(N->getOperand(0)); |
| 1026 | SDOperand LHSH = Select(N->getOperand(1)); |
| 1027 | |
| 1028 | unsigned Imm; |
Chris Lattner | 95e0682 | 2005-08-26 16:38:51 +0000 | [diff] [blame] | 1029 | bool ME = false, ZE = false; |
Chris Lattner | a9317ed | 2005-08-25 23:21:06 +0000 | [diff] [blame] | 1030 | if (isIntImmediate(N->getOperand(3), Imm)) { |
| 1031 | ME = (signed)Imm == -1; |
| 1032 | ZE = Imm == 0; |
| 1033 | } |
| 1034 | |
| 1035 | std::vector<SDOperand> Result; |
| 1036 | SDOperand CarryFromLo; |
| 1037 | if (isIntImmediate(N->getOperand(2), Imm) && |
| 1038 | ((signed)Imm >= -32768 || (signed)Imm < 32768)) { |
| 1039 | // Codegen the low 32 bits of the add. Interestingly, there is no |
| 1040 | // shifted form of add immediate carrying. |
| 1041 | CarryFromLo = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag, |
| 1042 | LHSL, getI32Imm(Imm)); |
| 1043 | } else { |
| 1044 | CarryFromLo = CurDAG->getTargetNode(PPC::ADDC, MVT::i32, MVT::Flag, |
| 1045 | LHSL, Select(N->getOperand(2))); |
| 1046 | } |
Chris Lattner | a9317ed | 2005-08-25 23:21:06 +0000 | [diff] [blame] | 1047 | CarryFromLo = CarryFromLo.getValue(1); |
| 1048 | |
| 1049 | // Codegen the high 32 bits, adding zero, minus one, or the full value |
| 1050 | // along with the carry flag produced by addc/addic. |
| 1051 | SDOperand ResultHi; |
| 1052 | if (ZE) |
| 1053 | ResultHi = CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, LHSH, CarryFromLo); |
| 1054 | else if (ME) |
| 1055 | ResultHi = CurDAG->getTargetNode(PPC::ADDME, MVT::i32, LHSH, CarryFromLo); |
| 1056 | else |
| 1057 | ResultHi = CurDAG->getTargetNode(PPC::ADDE, MVT::i32, LHSH, |
| 1058 | Select(N->getOperand(3)), CarryFromLo); |
Chris Lattner | b20c318 | 2005-08-25 23:36:49 +0000 | [diff] [blame] | 1059 | Result.push_back(CarryFromLo.getValue(0)); |
Chris Lattner | 14b86c7 | 2005-08-30 17:40:13 +0000 | [diff] [blame] | 1060 | Result.push_back(ResultHi); |
Chris Lattner | d3d2cf5 | 2005-09-29 00:59:32 +0000 | [diff] [blame] | 1061 | |
| 1062 | CodeGenMap[Op.getValue(0)] = Result[0]; |
| 1063 | CodeGenMap[Op.getValue(1)] = Result[1]; |
Chris Lattner | a9317ed | 2005-08-25 23:21:06 +0000 | [diff] [blame] | 1064 | return Result[Op.ResNo]; |
| 1065 | } |
| 1066 | case ISD::SUB_PARTS: { |
| 1067 | SDOperand LHSL = Select(N->getOperand(0)); |
| 1068 | SDOperand LHSH = Select(N->getOperand(1)); |
| 1069 | SDOperand RHSL = Select(N->getOperand(2)); |
| 1070 | SDOperand RHSH = Select(N->getOperand(3)); |
| 1071 | |
| 1072 | std::vector<SDOperand> Result; |
| 1073 | Result.push_back(CurDAG->getTargetNode(PPC::SUBFC, MVT::i32, MVT::Flag, |
| 1074 | RHSL, LHSL)); |
| 1075 | Result.push_back(CurDAG->getTargetNode(PPC::SUBFE, MVT::i32, RHSH, LHSH, |
| 1076 | Result[0].getValue(1))); |
Chris Lattner | d3d2cf5 | 2005-09-29 00:59:32 +0000 | [diff] [blame] | 1077 | CodeGenMap[Op.getValue(0)] = Result[0]; |
| 1078 | CodeGenMap[Op.getValue(1)] = Result[1]; |
Chris Lattner | a9317ed | 2005-08-25 23:21:06 +0000 | [diff] [blame] | 1079 | return Result[Op.ResNo]; |
| 1080 | } |
| 1081 | |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 1082 | case ISD::LOAD: |
| 1083 | case ISD::EXTLOAD: |
| 1084 | case ISD::ZEXTLOAD: |
| 1085 | case ISD::SEXTLOAD: { |
| 1086 | SDOperand Op1, Op2; |
| 1087 | bool isIdx = SelectAddr(N->getOperand(1), Op1, Op2); |
| 1088 | |
| 1089 | MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ? |
| 1090 | N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT(); |
| 1091 | unsigned Opc; |
| 1092 | switch (TypeBeingLoaded) { |
| 1093 | default: N->dump(); assert(0 && "Cannot load this type!"); |
| 1094 | case MVT::i1: |
| 1095 | case MVT::i8: Opc = isIdx ? PPC::LBZX : PPC::LBZ; break; |
| 1096 | case MVT::i16: |
| 1097 | if (N->getOpcode() == ISD::SEXTLOAD) { // SEXT load? |
| 1098 | Opc = isIdx ? PPC::LHAX : PPC::LHA; |
| 1099 | } else { |
| 1100 | Opc = isIdx ? PPC::LHZX : PPC::LHZ; |
| 1101 | } |
| 1102 | break; |
| 1103 | case MVT::i32: Opc = isIdx ? PPC::LWZX : PPC::LWZ; break; |
| 1104 | case MVT::f32: Opc = isIdx ? PPC::LFSX : PPC::LFS; break; |
| 1105 | case MVT::f64: Opc = isIdx ? PPC::LFDX : PPC::LFD; break; |
| 1106 | } |
| 1107 | |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1108 | // If this is an f32 -> f64 load, emit the f32 load, then use an 'extending |
| 1109 | // copy'. |
| 1110 | if (TypeBeingLoaded != MVT::f32 || N->getOpcode() == ISD::LOAD) { |
| 1111 | CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other, |
| 1112 | Op1, Op2, Select(N->getOperand(0))); |
| 1113 | return SDOperand(N, Op.ResNo); |
| 1114 | } else { |
| 1115 | std::vector<SDOperand> Ops; |
| 1116 | Ops.push_back(Op1); |
| 1117 | Ops.push_back(Op2); |
| 1118 | Ops.push_back(Select(N->getOperand(0))); |
| 1119 | SDOperand Res = CurDAG->getTargetNode(Opc, MVT::f32, MVT::Other, Ops); |
| 1120 | SDOperand Ext = CurDAG->getTargetNode(PPC::FMRSD, MVT::f64, Res); |
| 1121 | CodeGenMap[Op.getValue(0)] = Ext; |
| 1122 | CodeGenMap[Op.getValue(1)] = Res.getValue(1); |
| 1123 | if (Op.ResNo) |
| 1124 | return Res.getValue(1); |
| 1125 | else |
| 1126 | return Ext; |
| 1127 | } |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Chris Lattner | f7f2255 | 2005-08-22 01:27:59 +0000 | [diff] [blame] | 1130 | case ISD::TRUNCSTORE: |
| 1131 | case ISD::STORE: { |
| 1132 | SDOperand AddrOp1, AddrOp2; |
| 1133 | bool isIdx = SelectAddr(N->getOperand(2), AddrOp1, AddrOp2); |
| 1134 | |
| 1135 | unsigned Opc; |
| 1136 | if (N->getOpcode() == ISD::STORE) { |
| 1137 | switch (N->getOperand(1).getValueType()) { |
| 1138 | default: assert(0 && "unknown Type in store"); |
| 1139 | case MVT::i32: Opc = isIdx ? PPC::STWX : PPC::STW; break; |
| 1140 | case MVT::f64: Opc = isIdx ? PPC::STFDX : PPC::STFD; break; |
| 1141 | case MVT::f32: Opc = isIdx ? PPC::STFSX : PPC::STFS; break; |
| 1142 | } |
| 1143 | } else { //ISD::TRUNCSTORE |
| 1144 | switch(cast<VTSDNode>(N->getOperand(4))->getVT()) { |
| 1145 | default: assert(0 && "unknown Type in store"); |
Chris Lattner | f7f2255 | 2005-08-22 01:27:59 +0000 | [diff] [blame] | 1146 | case MVT::i8: Opc = isIdx ? PPC::STBX : PPC::STB; break; |
| 1147 | case MVT::i16: Opc = isIdx ? PPC::STHX : PPC::STH; break; |
| 1148 | } |
| 1149 | } |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1150 | |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1151 | CurDAG->SelectNodeTo(N, Opc, MVT::Other, Select(N->getOperand(1)), |
Chris Lattner | f7f2255 | 2005-08-22 01:27:59 +0000 | [diff] [blame] | 1152 | AddrOp1, AddrOp2, Select(N->getOperand(0))); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 1153 | return SDOperand(N, 0); |
Chris Lattner | f7f2255 | 2005-08-22 01:27:59 +0000 | [diff] [blame] | 1154 | } |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1155 | |
| 1156 | case ISD::SETCC: { |
| 1157 | unsigned Imm; |
| 1158 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); |
| 1159 | if (isIntImmediate(N->getOperand(1), Imm)) { |
| 1160 | // We can codegen setcc op, imm very efficiently compared to a brcond. |
| 1161 | // Check for those cases here. |
| 1162 | // setcc op, 0 |
| 1163 | if (Imm == 0) { |
| 1164 | SDOperand Op = Select(N->getOperand(0)); |
| 1165 | switch (CC) { |
| 1166 | default: assert(0 && "Unhandled SetCC condition"); abort(); |
| 1167 | case ISD::SETEQ: |
| 1168 | Op = CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1169 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27), |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1170 | getI32Imm(5), getI32Imm(31)); |
| 1171 | break; |
| 1172 | case ISD::SETNE: { |
| 1173 | SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag, |
| 1174 | Op, getI32Imm(~0U)); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1175 | CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1)); |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1176 | break; |
| 1177 | } |
| 1178 | case ISD::SETLT: |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1179 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1), |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1180 | getI32Imm(31), getI32Imm(31)); |
| 1181 | break; |
| 1182 | case ISD::SETGT: { |
| 1183 | SDOperand T = CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op); |
| 1184 | T = CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op);; |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1185 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1), |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1186 | getI32Imm(31), getI32Imm(31)); |
| 1187 | break; |
| 1188 | } |
| 1189 | } |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 1190 | return SDOperand(N, 0); |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1191 | } else if (Imm == ~0U) { // setcc op, -1 |
| 1192 | SDOperand Op = Select(N->getOperand(0)); |
| 1193 | switch (CC) { |
| 1194 | default: assert(0 && "Unhandled SetCC condition"); abort(); |
| 1195 | case ISD::SETEQ: |
| 1196 | Op = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag, |
| 1197 | Op, getI32Imm(1)); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1198 | CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1199 | CurDAG->getTargetNode(PPC::LI, MVT::i32, |
| 1200 | getI32Imm(0)), |
| 1201 | Op.getValue(1)); |
| 1202 | break; |
| 1203 | case ISD::SETNE: { |
| 1204 | Op = CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op); |
Chris Lattner | 8bbcc20 | 2005-08-29 23:49:25 +0000 | [diff] [blame] | 1205 | SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag, |
| 1206 | Op, getI32Imm(~0U)); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1207 | CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1)); |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1208 | break; |
| 1209 | } |
| 1210 | case ISD::SETLT: { |
| 1211 | SDOperand AD = CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op, |
| 1212 | getI32Imm(1)); |
| 1213 | SDOperand AN = CurDAG->getTargetNode(PPC::AND, MVT::i32, AD, Op); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1214 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1), |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1215 | getI32Imm(31), getI32Imm(31)); |
| 1216 | break; |
| 1217 | } |
| 1218 | case ISD::SETGT: |
| 1219 | Op = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op, getI32Imm(1), |
| 1220 | getI32Imm(31), getI32Imm(31)); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1221 | CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1)); |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1222 | break; |
| 1223 | } |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 1224 | return SDOperand(N, 0); |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | bool Inv; |
| 1229 | unsigned Idx = getCRIdxForSetCC(CC, Inv); |
Chris Lattner | 50ff55c | 2005-09-01 19:20:44 +0000 | [diff] [blame] | 1230 | SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC); |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1231 | SDOperand IntCR; |
Chris Lattner | 957fcfb | 2005-08-25 21:39:42 +0000 | [diff] [blame] | 1232 | |
| 1233 | // Force the ccreg into CR7. |
| 1234 | SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32); |
| 1235 | |
| 1236 | std::vector<MVT::ValueType> VTs; |
| 1237 | VTs.push_back(MVT::Other); |
| 1238 | VTs.push_back(MVT::Flag); // NONSTANDARD CopyToReg node: defines a flag |
| 1239 | std::vector<SDOperand> Ops; |
| 1240 | Ops.push_back(CurDAG->getEntryNode()); |
| 1241 | Ops.push_back(CR7Reg); |
| 1242 | Ops.push_back(CCReg); |
| 1243 | CCReg = CurDAG->getNode(ISD::CopyToReg, VTs, Ops).getValue(1); |
| 1244 | |
| 1245 | if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor()) |
| 1246 | IntCR = CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg, CCReg); |
| 1247 | else |
| 1248 | IntCR = CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg); |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1249 | |
| 1250 | if (!Inv) { |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1251 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR, |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1252 | getI32Imm(32-(3-Idx)), getI32Imm(31), getI32Imm(31)); |
| 1253 | } else { |
| 1254 | SDOperand Tmp = |
| 1255 | CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR, |
| 1256 | getI32Imm(32-(3-Idx)), getI32Imm(31),getI32Imm(31)); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1257 | CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1)); |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 1260 | return SDOperand(N, 0); |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1261 | } |
Chris Lattner | a2590c5 | 2005-08-24 00:47:15 +0000 | [diff] [blame] | 1262 | |
Chris Lattner | 13794f5 | 2005-08-26 18:46:49 +0000 | [diff] [blame] | 1263 | case ISD::SELECT_CC: { |
| 1264 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get(); |
| 1265 | |
| 1266 | // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc |
| 1267 | if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1))) |
| 1268 | if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2))) |
| 1269 | if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3))) |
| 1270 | if (N1C->isNullValue() && N3C->isNullValue() && |
| 1271 | N2C->getValue() == 1ULL && CC == ISD::SETNE) { |
| 1272 | SDOperand LHS = Select(N->getOperand(0)); |
| 1273 | SDOperand Tmp = |
| 1274 | CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag, |
| 1275 | LHS, getI32Imm(~0U)); |
| 1276 | CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, Tmp, LHS, |
| 1277 | Tmp.getValue(1)); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 1278 | return SDOperand(N, 0); |
Chris Lattner | 13794f5 | 2005-08-26 18:46:49 +0000 | [diff] [blame] | 1279 | } |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 1280 | |
Chris Lattner | 50ff55c | 2005-09-01 19:20:44 +0000 | [diff] [blame] | 1281 | SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC); |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 1282 | unsigned BROpc = getBCCForSetCC(CC); |
| 1283 | |
| 1284 | bool isFP = MVT::isFloatingPoint(N->getValueType(0)); |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1285 | unsigned SelectCCOp; |
| 1286 | if (MVT::isInteger(N->getValueType(0))) |
| 1287 | SelectCCOp = PPC::SELECT_CC_Int; |
| 1288 | else if (N->getValueType(0) == MVT::f32) |
| 1289 | SelectCCOp = PPC::SELECT_CC_F4; |
| 1290 | else |
| 1291 | SelectCCOp = PPC::SELECT_CC_F8; |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 1292 | CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg, |
| 1293 | Select(N->getOperand(2)), Select(N->getOperand(3)), |
| 1294 | getI32Imm(BROpc)); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 1295 | return SDOperand(N, 0); |
Chris Lattner | 13794f5 | 2005-08-26 18:46:49 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
Chris Lattner | a2590c5 | 2005-08-24 00:47:15 +0000 | [diff] [blame] | 1298 | case ISD::CALLSEQ_START: |
| 1299 | case ISD::CALLSEQ_END: { |
| 1300 | unsigned Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue(); |
| 1301 | unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ? |
| 1302 | PPC::ADJCALLSTACKDOWN : PPC::ADJCALLSTACKUP; |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1303 | CurDAG->SelectNodeTo(N, Opc, MVT::Other, |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1304 | getI32Imm(Amt), Select(N->getOperand(0))); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 1305 | return SDOperand(N, 0); |
Chris Lattner | a2590c5 | 2005-08-24 00:47:15 +0000 | [diff] [blame] | 1306 | } |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1307 | case ISD::CALL: |
| 1308 | case ISD::TAILCALL: { |
| 1309 | SDOperand Chain = Select(N->getOperand(0)); |
| 1310 | |
| 1311 | unsigned CallOpcode; |
| 1312 | std::vector<SDOperand> CallOperands; |
| 1313 | |
| 1314 | if (GlobalAddressSDNode *GASD = |
| 1315 | dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) { |
| 1316 | CallOpcode = PPC::CALLpcrel; |
| 1317 | CallOperands.push_back(CurDAG->getTargetGlobalAddress(GASD->getGlobal(), |
| 1318 | MVT::i32)); |
| 1319 | } else if (ExternalSymbolSDNode *ESSDN = |
| 1320 | dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) { |
| 1321 | CallOpcode = PPC::CALLpcrel; |
| 1322 | CallOperands.push_back(N->getOperand(1)); |
| 1323 | } else { |
| 1324 | // Copy the callee address into the CTR register. |
| 1325 | SDOperand Callee = Select(N->getOperand(1)); |
| 1326 | Chain = CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Callee, Chain); |
| 1327 | |
| 1328 | // Copy the callee address into R12 on darwin. |
| 1329 | SDOperand R12 = CurDAG->getRegister(PPC::R12, MVT::i32); |
Chris Lattner | 2a06a5e | 2005-08-29 00:26:57 +0000 | [diff] [blame] | 1330 | Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R12, Callee); |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1331 | |
| 1332 | CallOperands.push_back(getI32Imm(20)); // Information to encode indcall |
| 1333 | CallOperands.push_back(getI32Imm(0)); // Information to encode indcall |
| 1334 | CallOperands.push_back(R12); |
| 1335 | CallOpcode = PPC::CALLindirect; |
| 1336 | } |
| 1337 | |
| 1338 | unsigned GPR_idx = 0, FPR_idx = 0; |
| 1339 | static const unsigned GPR[] = { |
| 1340 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 1341 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 1342 | }; |
| 1343 | static const unsigned FPR[] = { |
| 1344 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 1345 | PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13 |
| 1346 | }; |
| 1347 | |
Chris Lattner | 31ce12f | 2005-08-30 01:57:02 +0000 | [diff] [blame] | 1348 | SDOperand InFlag; // Null incoming flag value. |
| 1349 | |
Chris Lattner | 7107c10 | 2005-08-29 22:22:57 +0000 | [diff] [blame] | 1350 | for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) { |
| 1351 | unsigned DestReg = 0; |
Chris Lattner | eb80fe8 | 2005-08-30 22:59:48 +0000 | [diff] [blame] | 1352 | MVT::ValueType RegTy = N->getOperand(i).getValueType(); |
| 1353 | if (RegTy == MVT::i32) { |
Chris Lattner | 7107c10 | 2005-08-29 22:22:57 +0000 | [diff] [blame] | 1354 | assert(GPR_idx < 8 && "Too many int args"); |
| 1355 | DestReg = GPR[GPR_idx++]; |
Chris Lattner | 7107c10 | 2005-08-29 22:22:57 +0000 | [diff] [blame] | 1356 | } else { |
| 1357 | assert(MVT::isFloatingPoint(N->getOperand(i).getValueType()) && |
| 1358 | "Unpromoted integer arg?"); |
| 1359 | assert(FPR_idx < 13 && "Too many fp args"); |
| 1360 | DestReg = FPR[FPR_idx++]; |
Chris Lattner | 7107c10 | 2005-08-29 22:22:57 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1363 | if (N->getOperand(i).getOpcode() != ISD::UNDEF) { |
Chris Lattner | 2ea0c66 | 2005-08-30 21:28:19 +0000 | [diff] [blame] | 1364 | SDOperand Val = Select(N->getOperand(i)); |
Chris Lattner | 2ea0c66 | 2005-08-30 21:28:19 +0000 | [diff] [blame] | 1365 | Chain = CurDAG->getCopyToReg(Chain, DestReg, Val, InFlag); |
Chris Lattner | 31ce12f | 2005-08-30 01:57:02 +0000 | [diff] [blame] | 1366 | InFlag = Chain.getValue(1); |
| 1367 | CallOperands.push_back(CurDAG->getRegister(DestReg, RegTy)); |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1368 | } |
Chris Lattner | 7107c10 | 2005-08-29 22:22:57 +0000 | [diff] [blame] | 1369 | } |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1370 | |
| 1371 | // Finally, once everything is in registers to pass to the call, emit the |
| 1372 | // call itself. |
Chris Lattner | 31ce12f | 2005-08-30 01:57:02 +0000 | [diff] [blame] | 1373 | if (InFlag.Val) |
| 1374 | CallOperands.push_back(InFlag); // Strong dep on register copies. |
| 1375 | else |
| 1376 | CallOperands.push_back(Chain); // Weak dep on whatever occurs before |
| 1377 | Chain = CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag, |
| 1378 | CallOperands); |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1379 | |
| 1380 | std::vector<SDOperand> CallResults; |
| 1381 | |
| 1382 | // If the call has results, copy the values out of the ret val registers. |
| 1383 | switch (N->getValueType(0)) { |
| 1384 | default: assert(0 && "Unexpected ret value!"); |
| 1385 | case MVT::Other: break; |
| 1386 | case MVT::i32: |
| 1387 | if (N->getValueType(1) == MVT::i32) { |
Chris Lattner | 31ce12f | 2005-08-30 01:57:02 +0000 | [diff] [blame] | 1388 | Chain = CurDAG->getCopyFromReg(Chain, PPC::R4, MVT::i32, |
| 1389 | Chain.getValue(1)).getValue(1); |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1390 | CallResults.push_back(Chain.getValue(0)); |
Chris Lattner | 31ce12f | 2005-08-30 01:57:02 +0000 | [diff] [blame] | 1391 | Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32, |
Jim Laskey | 242f255 | 2005-09-30 23:43:37 +0000 | [diff] [blame] | 1392 | Chain.getValue(2)).getValue(1); |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1393 | CallResults.push_back(Chain.getValue(0)); |
| 1394 | } else { |
Chris Lattner | 31ce12f | 2005-08-30 01:57:02 +0000 | [diff] [blame] | 1395 | Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32, |
| 1396 | Chain.getValue(1)).getValue(1); |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1397 | CallResults.push_back(Chain.getValue(0)); |
| 1398 | } |
| 1399 | break; |
| 1400 | case MVT::f32: |
| 1401 | case MVT::f64: |
Chris Lattner | eb80fe8 | 2005-08-30 22:59:48 +0000 | [diff] [blame] | 1402 | Chain = CurDAG->getCopyFromReg(Chain, PPC::F1, N->getValueType(0), |
Chris Lattner | 31ce12f | 2005-08-30 01:57:02 +0000 | [diff] [blame] | 1403 | Chain.getValue(1)).getValue(1); |
Chris Lattner | eb80fe8 | 2005-08-30 22:59:48 +0000 | [diff] [blame] | 1404 | CallResults.push_back(Chain.getValue(0)); |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1405 | break; |
| 1406 | } |
| 1407 | |
| 1408 | CallResults.push_back(Chain); |
Chris Lattner | d3d2cf5 | 2005-09-29 00:59:32 +0000 | [diff] [blame] | 1409 | for (unsigned i = 0, e = CallResults.size(); i != e; ++i) |
| 1410 | CodeGenMap[Op.getValue(i)] = CallResults[i]; |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1411 | return CallResults[Op.ResNo]; |
| 1412 | } |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1413 | case ISD::RET: { |
| 1414 | SDOperand Chain = Select(N->getOperand(0)); // Token chain. |
| 1415 | |
Chris Lattner | 7a49fdc | 2005-08-31 01:34:29 +0000 | [diff] [blame] | 1416 | if (N->getNumOperands() == 2) { |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1417 | SDOperand Val = Select(N->getOperand(1)); |
Chris Lattner | eb80fe8 | 2005-08-30 22:59:48 +0000 | [diff] [blame] | 1418 | if (N->getOperand(1).getValueType() == MVT::i32) { |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1419 | Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Val); |
Chris Lattner | eb80fe8 | 2005-08-30 22:59:48 +0000 | [diff] [blame] | 1420 | } else { |
| 1421 | assert(MVT::isFloatingPoint(N->getOperand(1).getValueType())); |
| 1422 | Chain = CurDAG->getCopyToReg(Chain, PPC::F1, Val); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1423 | } |
Chris Lattner | 7a49fdc | 2005-08-31 01:34:29 +0000 | [diff] [blame] | 1424 | } else if (N->getNumOperands() > 1) { |
| 1425 | assert(N->getOperand(1).getValueType() == MVT::i32 && |
| 1426 | N->getOperand(2).getValueType() == MVT::i32 && |
| 1427 | N->getNumOperands() == 3 && "Unknown two-register ret value!"); |
| 1428 | Chain = CurDAG->getCopyToReg(Chain, PPC::R4, Select(N->getOperand(1))); |
| 1429 | Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Select(N->getOperand(2))); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | // Finally, select this to a blr (return) instruction. |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1433 | CurDAG->SelectNodeTo(N, PPC::BLR, MVT::Other, Chain); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 1434 | return SDOperand(N, 0); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1435 | } |
Chris Lattner | 89532c7 | 2005-08-25 00:29:58 +0000 | [diff] [blame] | 1436 | case ISD::BR: |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1437 | CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, N->getOperand(1), |
Chris Lattner | 89532c7 | 2005-08-25 00:29:58 +0000 | [diff] [blame] | 1438 | Select(N->getOperand(0))); |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 1439 | return SDOperand(N, 0); |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 1440 | case ISD::BR_CC: |
| 1441 | case ISD::BRTWOWAY_CC: { |
| 1442 | SDOperand Chain = Select(N->getOperand(0)); |
| 1443 | MachineBasicBlock *Dest = |
| 1444 | cast<BasicBlockSDNode>(N->getOperand(4))->getBasicBlock(); |
| 1445 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get(); |
| 1446 | SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC); |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 1447 | |
| 1448 | // If this is a two way branch, then grab the fallthrough basic block |
| 1449 | // argument and build a PowerPC branch pseudo-op, suitable for long branch |
| 1450 | // conversion if necessary by the branch selection pass. Otherwise, emit a |
| 1451 | // standard conditional branch. |
| 1452 | if (N->getOpcode() == ISD::BRTWOWAY_CC) { |
Chris Lattner | ca0a477 | 2005-10-01 23:06:26 +0000 | [diff] [blame] | 1453 | SDOperand CondTrueBlock = N->getOperand(4); |
| 1454 | SDOperand CondFalseBlock = N->getOperand(5); |
| 1455 | |
| 1456 | // If the false case is the current basic block, then this is a self loop. |
| 1457 | // We do not want to emit "Loop: ... brcond Out; br Loop", as it adds an |
| 1458 | // extra dispatch group to the loop. Instead, invert the condition and |
| 1459 | // emit "Loop: ... br!cond Loop; br Out |
| 1460 | if (cast<BasicBlockSDNode>(CondFalseBlock)->getBasicBlock() == BB) { |
| 1461 | std::swap(CondTrueBlock, CondFalseBlock); |
| 1462 | CC = getSetCCInverse(CC, |
| 1463 | MVT::isInteger(N->getOperand(2).getValueType())); |
| 1464 | } |
| 1465 | |
| 1466 | unsigned Opc = getBCCForSetCC(CC); |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 1467 | SDOperand CB = CurDAG->getTargetNode(PPC::COND_BRANCH, MVT::Other, |
| 1468 | CondCode, getI32Imm(Opc), |
Chris Lattner | ca0a477 | 2005-10-01 23:06:26 +0000 | [diff] [blame] | 1469 | CondTrueBlock, CondFalseBlock, |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 1470 | Chain); |
Chris Lattner | ca0a477 | 2005-10-01 23:06:26 +0000 | [diff] [blame] | 1471 | CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, CondFalseBlock, CB); |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 1472 | } else { |
| 1473 | // Iterate to the next basic block |
| 1474 | ilist<MachineBasicBlock>::iterator It = BB; |
| 1475 | ++It; |
| 1476 | |
| 1477 | // If the fallthrough path is off the end of the function, which would be |
| 1478 | // undefined behavior, set it to be the same as the current block because |
| 1479 | // we have nothing better to set it to, and leaving it alone will cause |
| 1480 | // the PowerPC Branch Selection pass to crash. |
| 1481 | if (It == BB->getParent()->end()) It = Dest; |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1482 | CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other, CondCode, |
Chris Lattner | ca0a477 | 2005-10-01 23:06:26 +0000 | [diff] [blame] | 1483 | getI32Imm(getBCCForSetCC(CC)), N->getOperand(4), |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 1484 | CurDAG->getBasicBlock(It), Chain); |
| 1485 | } |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 1486 | return SDOperand(N, 0); |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 1487 | } |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1488 | } |
Chris Lattner | 25dae72 | 2005-09-03 00:53:47 +0000 | [diff] [blame] | 1489 | |
Chris Lattner | 19c0907 | 2005-09-07 23:45:15 +0000 | [diff] [blame] | 1490 | return SelectCode(Op); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | |
| 1494 | /// createPPC32ISelDag - This pass converts a legalized DAG into a |
| 1495 | /// PowerPC-specific DAG, ready for instruction scheduling. |
| 1496 | /// |
| 1497 | FunctionPass *llvm::createPPC32ISelDag(TargetMachine &TM) { |
| 1498 | return new PPC32DAGToDAGISel(TM); |
| 1499 | } |
| 1500 | |