Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1 | //===-- PPC32ISelDAGToDAG.cpp - PPC32 pattern matching inst selector ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a pattern matching instruction selector for 32 bit PowerPC, |
| 11 | // converting from a legalized dag to a PPC dag. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "PowerPC.h" |
| 16 | #include "PPC32TargetMachine.h" |
| 17 | #include "PPC32ISelLowering.h" |
| 18 | #include "llvm/CodeGen/SelectionDAG.h" |
| 19 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 20 | #include "llvm/Target/TargetOptions.h" |
| 21 | #include "llvm/ADT/Statistic.h" |
| 22 | #include "llvm/Support/Debug.h" |
| 23 | #include "llvm/Support/MathExtras.h" |
| 24 | using namespace llvm; |
| 25 | |
| 26 | namespace { |
| 27 | Statistic<> Recorded("ppc-codegen", "Number of recording ops emitted"); |
| 28 | Statistic<> FusedFP ("ppc-codegen", "Number of fused fp operations"); |
| 29 | Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed"); |
| 30 | |
| 31 | //===--------------------------------------------------------------------===// |
| 32 | /// PPC32DAGToDAGISel - PPC32 specific code to select PPC32 machine |
| 33 | /// instructions for SelectionDAG operations. |
| 34 | /// |
| 35 | class PPC32DAGToDAGISel : public SelectionDAGISel { |
| 36 | PPC32TargetLowering PPC32Lowering; |
| 37 | |
| 38 | unsigned GlobalBaseReg; |
| 39 | bool GlobalBaseInitialized; |
| 40 | public: |
| 41 | PPC32DAGToDAGISel(TargetMachine &TM) |
| 42 | : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM) {} |
| 43 | |
| 44 | /// runOnFunction - Override this function in order to reset our |
| 45 | /// per-function variables. |
| 46 | virtual bool runOnFunction(Function &Fn) { |
| 47 | // Make sure we re-emit a set of the global base reg if necessary |
| 48 | GlobalBaseInitialized = false; |
| 49 | return SelectionDAGISel::runOnFunction(Fn); |
| 50 | } |
| 51 | |
| 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 | } |
| 57 | |
| 58 | // Select - Convert the specified operand from a target-independent to a |
| 59 | // target-specific node if it hasn't already been changed. |
| 60 | SDOperand Select(SDOperand Op); |
| 61 | |
| 62 | SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS, |
| 63 | unsigned OCHi, unsigned OCLo, |
| 64 | bool IsArithmetic = false, |
| 65 | bool Negate = false); |
| 66 | |
| 67 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 68 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 69 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 70 | DEBUG(BB->dump()); |
| 71 | // Codegen the basic block. |
| 72 | Select(DAG.getRoot()); |
| 73 | DAG.RemoveDeadNodes(); |
| 74 | DAG.viewGraph(); |
| 75 | } |
| 76 | |
| 77 | virtual const char *getPassName() const { |
| 78 | return "PowerPC DAG->DAG Pattern Instruction Selection"; |
| 79 | } |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | // Immediate constant composers. |
| 84 | // Lo16 - grabs the lo 16 bits from a 32 bit constant. |
| 85 | // Hi16 - grabs the hi 16 bits from a 32 bit constant. |
| 86 | // HA16 - computes the hi bits required if the lo bits are add/subtracted in |
| 87 | // arithmethically. |
| 88 | static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; } |
| 89 | static unsigned Hi16(unsigned x) { return Lo16(x >> 16); } |
| 90 | static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); } |
| 91 | |
| 92 | // isIntImmediate - This method tests to see if a constant operand. |
| 93 | // If so Imm will receive the 32 bit value. |
| 94 | static bool isIntImmediate(SDOperand N, unsigned& Imm) { |
| 95 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) { |
| 96 | Imm = (unsigned)CN->getSignExtended(); |
| 97 | return true; |
| 98 | } |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | // SelectIntImmediateExpr - Choose code for integer operations with an immediate |
| 103 | // operand. |
| 104 | SDNode *PPC32DAGToDAGISel::SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS, |
| 105 | unsigned OCHi, unsigned OCLo, |
| 106 | bool IsArithmetic, |
| 107 | bool Negate) { |
| 108 | // Check to make sure this is a constant. |
| 109 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS); |
| 110 | // Exit if not a constant. |
| 111 | if (!CN) return 0; |
| 112 | // Extract immediate. |
| 113 | unsigned C = (unsigned)CN->getValue(); |
| 114 | // Negate if required (ISD::SUB). |
| 115 | if (Negate) C = -C; |
| 116 | // Get the hi and lo portions of constant. |
| 117 | unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C); |
| 118 | unsigned Lo = Lo16(C); |
| 119 | |
| 120 | // If two instructions are needed and usage indicates it would be better to |
| 121 | // load immediate into a register, bail out. |
| 122 | if (Hi && Lo && CN->use_size() > 2) return false; |
| 123 | |
| 124 | // Select the first operand. |
| 125 | SDOperand Opr0 = Select(LHS); |
| 126 | |
| 127 | if (Lo) // Add in the lo-part. |
| 128 | Opr0 = CurDAG->getTargetNode(OCLo, MVT::i32, Opr0, getI32Imm(Lo)); |
| 129 | if (Hi) // Add in the hi-part. |
| 130 | Opr0 = CurDAG->getTargetNode(OCHi, MVT::i32, Opr0, getI32Imm(Hi)); |
| 131 | return Opr0.Val; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | // Select - Convert the specified operand from a target-independent to a |
| 136 | // target-specific node if it hasn't already been changed. |
| 137 | SDOperand PPC32DAGToDAGISel::Select(SDOperand Op) { |
| 138 | SDNode *N = Op.Val; |
| 139 | if (N->getOpcode() >= ISD::BUILTIN_OP_END) |
| 140 | return Op; // Already selected. |
| 141 | |
| 142 | switch (N->getOpcode()) { |
| 143 | default: |
| 144 | std::cerr << "Cannot yet select: "; |
| 145 | N->dump(); |
| 146 | std::cerr << "\n"; |
| 147 | abort(); |
| 148 | case ISD::EntryToken: // These leaves remain the same. |
| 149 | case ISD::UNDEF: |
| 150 | return Op; |
| 151 | case ISD::TokenFactor: { |
| 152 | SDOperand New; |
| 153 | if (N->getNumOperands() == 2) { |
| 154 | SDOperand Op0 = Select(N->getOperand(0)); |
| 155 | SDOperand Op1 = Select(N->getOperand(1)); |
| 156 | New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1); |
| 157 | } else { |
| 158 | std::vector<SDOperand> Ops; |
| 159 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 160 | Ops.push_back(Select(N->getOperand(0))); |
| 161 | New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops); |
| 162 | } |
| 163 | |
| 164 | if (New.Val != N) { |
| 165 | CurDAG->ReplaceAllUsesWith(N, New.Val); |
| 166 | N = New.Val; |
| 167 | } |
| 168 | break; |
| 169 | } |
| 170 | case ISD::CopyFromReg: { |
| 171 | SDOperand Chain = Select(N->getOperand(0)); |
| 172 | if (Chain == N->getOperand(0)) return Op; // No change |
| 173 | SDOperand New = CurDAG->getCopyFromReg(Chain, |
| 174 | cast<RegisterSDNode>(N->getOperand(1))->getReg(), N->getValueType(0)); |
| 175 | return New.getValue(Op.ResNo); |
| 176 | } |
| 177 | case ISD::CopyToReg: { |
| 178 | SDOperand Chain = Select(N->getOperand(0)); |
| 179 | SDOperand Reg = N->getOperand(1); |
| 180 | SDOperand Val = Select(N->getOperand(2)); |
| 181 | if (Chain != N->getOperand(0) || Val != N->getOperand(2)) { |
| 182 | SDOperand New = CurDAG->getNode(ISD::CopyToReg, MVT::Other, |
| 183 | Chain, Reg, Val); |
| 184 | CurDAG->ReplaceAllUsesWith(N, New.Val); |
| 185 | N = New.Val; |
| 186 | } |
| 187 | break; |
| 188 | } |
| 189 | case ISD::Constant: { |
| 190 | assert(N->getValueType(0) == MVT::i32); |
| 191 | unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue(); |
| 192 | if ((unsigned)(short)v == v) { |
| 193 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::LI, getI32Imm(v)); |
| 194 | break; |
| 195 | } else { |
| 196 | SDOperand Top = CurDAG->getTargetNode(PPC::LIS, MVT::i32, |
| 197 | getI32Imm(unsigned(v) >> 16)); |
| 198 | CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORI, Top, getI32Imm(v & 0xFFFF)); |
| 199 | break; |
| 200 | } |
| 201 | } |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 202 | case ISD::ADD: { |
| 203 | MVT::ValueType Ty = N->getValueType(0); |
| 204 | if (Ty == MVT::i32) { |
| 205 | if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1), |
| 206 | PPC::ADDIS, PPC::ADDI, true)) { |
| 207 | CurDAG->ReplaceAllUsesWith(N, I); |
| 208 | N = I; |
| 209 | } else { |
| 210 | CurDAG->SelectNodeTo(N, Ty, PPC::ADD, Select(N->getOperand(0)), |
| 211 | Select(N->getOperand(1))); |
| 212 | } |
| 213 | break; |
| 214 | } |
| 215 | |
| 216 | if (!NoExcessFPPrecision) { // Match FMA ops |
| 217 | if (N->getOperand(0).getOpcode() == ISD::MUL && |
| 218 | N->getOperand(0).Val->hasOneUse()) { |
| 219 | ++FusedFP; // Statistic |
| 220 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, |
| 221 | Select(N->getOperand(0).getOperand(0)), |
| 222 | Select(N->getOperand(0).getOperand(1)), |
| 223 | Select(N->getOperand(1))); |
| 224 | break; |
| 225 | } else if (N->getOperand(1).getOpcode() == ISD::MUL && |
| 226 | N->getOperand(1).hasOneUse()) { |
| 227 | ++FusedFP; // Statistic |
| 228 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, |
| 229 | Select(N->getOperand(1).getOperand(0)), |
| 230 | Select(N->getOperand(1).getOperand(1)), |
| 231 | Select(N->getOperand(0))); |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FADD : PPC::FADDS, |
| 237 | Select(N->getOperand(0)), Select(N->getOperand(1))); |
| 238 | break; |
| 239 | } |
| 240 | case ISD::SUB: { |
| 241 | MVT::ValueType Ty = N->getValueType(0); |
| 242 | if (Ty == MVT::i32) { |
| 243 | unsigned Imm; |
| 244 | if (isIntImmediate(N->getOperand(0), Imm) && isInt16(Imm)) { |
| 245 | CurDAG->SelectNodeTo(N, Ty, PPC::SUBFIC, Select(N->getOperand(1)), |
| 246 | getI32Imm(Lo16(Imm))); |
| 247 | break; |
| 248 | } |
| 249 | if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1), |
| 250 | PPC::ADDIS, PPC::ADDI, true, true)) { |
| 251 | CurDAG->ReplaceAllUsesWith(N, I); |
| 252 | N = I; |
| 253 | } else { |
| 254 | CurDAG->SelectNodeTo(N, Ty, PPC::SUBF, Select(N->getOperand(1)), |
| 255 | Select(N->getOperand(0))); |
| 256 | } |
| 257 | break; |
| 258 | } |
| 259 | |
| 260 | if (!NoExcessFPPrecision) { // Match FMA ops |
| 261 | if (N->getOperand(0).getOpcode() == ISD::MUL && |
| 262 | N->getOperand(0).Val->hasOneUse()) { |
| 263 | ++FusedFP; // Statistic |
| 264 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS, |
| 265 | Select(N->getOperand(0).getOperand(0)), |
| 266 | Select(N->getOperand(0).getOperand(1)), |
| 267 | Select(N->getOperand(1))); |
| 268 | break; |
| 269 | } else if (N->getOperand(1).getOpcode() == ISD::MUL && |
| 270 | N->getOperand(1).Val->hasOneUse()) { |
| 271 | ++FusedFP; // Statistic |
| 272 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS, |
| 273 | Select(N->getOperand(1).getOperand(0)), |
| 274 | Select(N->getOperand(1).getOperand(1)), |
| 275 | Select(N->getOperand(0))); |
| 276 | break; |
| 277 | } |
| 278 | } |
| 279 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSUB : PPC::FSUBS, |
| 280 | Select(N->getOperand(0)), |
| 281 | Select(N->getOperand(1))); |
| 282 | break; |
Nate Begeman | 2665350 | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 283 | } |
Nate Begeman | b5a0668 | 2005-08-18 00:21:41 +0000 | [diff] [blame] | 284 | case ISD::MUL: { |
| 285 | unsigned Imm, Opc; |
| 286 | if (isIntImmediate(N->getOperand(1), Imm) && isInt16(Imm)) { |
| 287 | CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::MULLI, |
| 288 | Select(N->getOperand(0)), getI32Imm(Lo16(Imm))); |
| 289 | break; |
| 290 | } |
| 291 | switch (N->getValueType(0)) { |
| 292 | default: assert(0 && "Unhandled multiply type!"); |
| 293 | case MVT::i32: Opc = PPC::MULLW; break; |
| 294 | case MVT::f32: Opc = PPC::FMULS; break; |
| 295 | case MVT::f64: Opc = PPC::FMUL; break; |
| 296 | } |
| 297 | CurDAG->SelectNodeTo(N, N->getValueType(0), Opc, Select(N->getOperand(0)), |
| 298 | Select(N->getOperand(1))); |
| 299 | break; |
| 300 | } |
| 301 | case ISD::MULHS: { |
| 302 | assert(N->getValueType(0) == MVT::i32); |
| 303 | CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::MULHW, |
| 304 | Select(N->getOperand(0)), Select(N->getOperand(1))); |
| 305 | break; |
| 306 | } |
| 307 | case ISD::MULHU: { |
| 308 | assert(N->getValueType(0) == MVT::i32); |
| 309 | CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::MULHWU, |
| 310 | Select(N->getOperand(0)), Select(N->getOperand(1))); |
| 311 | break; |
| 312 | } |
Nate Begeman | 6a7d611 | 2005-08-18 00:53:47 +0000 | [diff] [blame^] | 313 | case ISD::FABS: { |
| 314 | CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::FABS, |
| 315 | Select(N->getOperand(0))); |
| 316 | break; |
| 317 | } |
Nate Begeman | 2665350 | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 318 | case ISD::FNEG: { |
| 319 | SDOperand Val = Select(N->getOperand(0)); |
| 320 | MVT::ValueType Ty = N->getValueType(0); |
| 321 | if (Val.Val->hasOneUse()) { |
| 322 | unsigned Opc; |
| 323 | switch (Val.getTargetOpcode()) { |
| 324 | default: Opc = 0; break; |
| 325 | case PPC::FABS: Opc = PPC::FNABS; break; |
| 326 | case PPC::FMADD: Opc = PPC::FNMADD; break; |
| 327 | case PPC::FMADDS: Opc = PPC::FNMADDS; break; |
| 328 | case PPC::FMSUB: Opc = PPC::FNMSUB; break; |
| 329 | case PPC::FMSUBS: Opc = PPC::FNMSUBS; break; |
| 330 | } |
| 331 | // If we inverted the opcode, then emit the new instruction with the |
| 332 | // inverted opcode and the original instruction's operands. Otherwise, |
| 333 | // fall through and generate a fneg instruction. |
| 334 | if (Opc) { |
| 335 | if (PPC::FNABS == Opc) |
| 336 | CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0)); |
| 337 | else |
| 338 | CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0), |
| 339 | Val.getOperand(1), Val.getOperand(2)); |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | CurDAG->SelectNodeTo(N, Ty, PPC::FNEG, Val); |
| 344 | break; |
| 345 | } |
Nate Begeman | 6a7d611 | 2005-08-18 00:53:47 +0000 | [diff] [blame^] | 346 | case ISD::FSQRT: { |
| 347 | MVT::ValueType Ty = N->getValueType(0); |
| 348 | CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS, |
| 349 | Select(N->getOperand(0))); |
| 350 | break; |
| 351 | } |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 352 | case ISD::RET: { |
| 353 | SDOperand Chain = Select(N->getOperand(0)); // Token chain. |
| 354 | |
| 355 | if (N->getNumOperands() > 1) { |
| 356 | SDOperand Val = Select(N->getOperand(1)); |
| 357 | switch (N->getOperand(1).getValueType()) { |
| 358 | default: assert(0 && "Unknown return type!"); |
| 359 | case MVT::f64: |
| 360 | case MVT::f32: |
| 361 | Chain = CurDAG->getCopyToReg(Chain, PPC::F1, Val); |
| 362 | break; |
| 363 | case MVT::i32: |
| 364 | Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Val); |
| 365 | break; |
| 366 | } |
| 367 | |
| 368 | if (N->getNumOperands() > 2) { |
| 369 | assert(N->getOperand(1).getValueType() == MVT::i32 && |
| 370 | N->getOperand(2).getValueType() == MVT::i32 && |
| 371 | N->getNumOperands() == 2 && "Unknown two-register ret value!"); |
| 372 | Val = Select(N->getOperand(2)); |
| 373 | Chain = CurDAG->getCopyToReg(Chain, PPC::R4, Val); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | // Finally, select this to a blr (return) instruction. |
| 378 | CurDAG->SelectNodeTo(N, MVT::Other, PPC::BLR, Chain); |
| 379 | break; |
| 380 | } |
| 381 | } |
| 382 | return SDOperand(N, 0); |
| 383 | } |
| 384 | |
| 385 | |
| 386 | /// createPPC32ISelDag - This pass converts a legalized DAG into a |
| 387 | /// PowerPC-specific DAG, ready for instruction scheduling. |
| 388 | /// |
| 389 | FunctionPass *llvm::createPPC32ISelDag(TargetMachine &TM) { |
| 390 | return new PPC32DAGToDAGISel(TM); |
| 391 | } |
| 392 | |