Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame^] | 1 | //===- FastISel.h - Definition of the FastISel class ------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// This file defines the FastISel class. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_CODEGEN_FASTISEL_H |
| 16 | #define LLVM_CODEGEN_FASTISEL_H |
| 17 | |
| 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "llvm/ADT/SmallVector.h" |
| 20 | #include "llvm/ADT/StringRef.h" |
| 21 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 22 | #include "llvm/CodeGen/TargetLowering.h" |
| 23 | #include "llvm/IR/Attributes.h" |
| 24 | #include "llvm/IR/CallSite.h" |
| 25 | #include "llvm/IR/CallingConv.h" |
| 26 | #include "llvm/IR/DebugLoc.h" |
| 27 | #include "llvm/IR/DerivedTypes.h" |
| 28 | #include "llvm/IR/InstrTypes.h" |
| 29 | #include "llvm/IR/IntrinsicInst.h" |
| 30 | #include "llvm/Support/MachineValueType.h" |
| 31 | #include <algorithm> |
| 32 | #include <cstdint> |
| 33 | #include <utility> |
| 34 | |
| 35 | namespace llvm { |
| 36 | |
| 37 | class AllocaInst; |
| 38 | class BasicBlock; |
| 39 | class CallInst; |
| 40 | class Constant; |
| 41 | class ConstantFP; |
| 42 | class DataLayout; |
| 43 | class FunctionLoweringInfo; |
| 44 | class LoadInst; |
| 45 | class MachineConstantPool; |
| 46 | class MachineFrameInfo; |
| 47 | class MachineFunction; |
| 48 | class MachineInstr; |
| 49 | class MachineMemOperand; |
| 50 | class MachineOperand; |
| 51 | class MachineRegisterInfo; |
| 52 | class MCContext; |
| 53 | class MCInstrDesc; |
| 54 | class MCSymbol; |
| 55 | class TargetInstrInfo; |
| 56 | class TargetLibraryInfo; |
| 57 | class TargetMachine; |
| 58 | class TargetRegisterClass; |
| 59 | class TargetRegisterInfo; |
| 60 | class Type; |
| 61 | class User; |
| 62 | class Value; |
| 63 | |
| 64 | /// \brief This is a fast-path instruction selection class that generates poor |
| 65 | /// code and doesn't support illegal types or non-trivial lowering, but runs |
| 66 | /// quickly. |
| 67 | class FastISel { |
| 68 | public: |
| 69 | using ArgListEntry = TargetLoweringBase::ArgListEntry; |
| 70 | using ArgListTy = TargetLoweringBase::ArgListTy; |
| 71 | struct CallLoweringInfo { |
| 72 | Type *RetTy = nullptr; |
| 73 | bool RetSExt : 1; |
| 74 | bool RetZExt : 1; |
| 75 | bool IsVarArg : 1; |
| 76 | bool IsInReg : 1; |
| 77 | bool DoesNotReturn : 1; |
| 78 | bool IsReturnValueUsed : 1; |
| 79 | bool IsPatchPoint : 1; |
| 80 | |
| 81 | // \brief IsTailCall Should be modified by implementations of FastLowerCall |
| 82 | // that perform tail call conversions. |
| 83 | bool IsTailCall = false; |
| 84 | |
| 85 | unsigned NumFixedArgs = -1; |
| 86 | CallingConv::ID CallConv = CallingConv::C; |
| 87 | const Value *Callee = nullptr; |
| 88 | MCSymbol *Symbol = nullptr; |
| 89 | ArgListTy Args; |
| 90 | ImmutableCallSite *CS = nullptr; |
| 91 | MachineInstr *Call = nullptr; |
| 92 | unsigned ResultReg = 0; |
| 93 | unsigned NumResultRegs = 0; |
| 94 | |
| 95 | SmallVector<Value *, 16> OutVals; |
| 96 | SmallVector<ISD::ArgFlagsTy, 16> OutFlags; |
| 97 | SmallVector<unsigned, 16> OutRegs; |
| 98 | SmallVector<ISD::InputArg, 4> Ins; |
| 99 | SmallVector<unsigned, 4> InRegs; |
| 100 | |
| 101 | CallLoweringInfo() |
| 102 | : RetSExt(false), RetZExt(false), IsVarArg(false), IsInReg(false), |
| 103 | DoesNotReturn(false), IsReturnValueUsed(true), IsPatchPoint(false) {} |
| 104 | |
| 105 | CallLoweringInfo &setCallee(Type *ResultTy, FunctionType *FuncTy, |
| 106 | const Value *Target, ArgListTy &&ArgsList, |
| 107 | ImmutableCallSite &Call) { |
| 108 | RetTy = ResultTy; |
| 109 | Callee = Target; |
| 110 | |
| 111 | IsInReg = Call.hasRetAttr(Attribute::InReg); |
| 112 | DoesNotReturn = Call.doesNotReturn(); |
| 113 | IsVarArg = FuncTy->isVarArg(); |
| 114 | IsReturnValueUsed = !Call.getInstruction()->use_empty(); |
| 115 | RetSExt = Call.hasRetAttr(Attribute::SExt); |
| 116 | RetZExt = Call.hasRetAttr(Attribute::ZExt); |
| 117 | |
| 118 | CallConv = Call.getCallingConv(); |
| 119 | Args = std::move(ArgsList); |
| 120 | NumFixedArgs = FuncTy->getNumParams(); |
| 121 | |
| 122 | CS = &Call; |
| 123 | |
| 124 | return *this; |
| 125 | } |
| 126 | |
| 127 | CallLoweringInfo &setCallee(Type *ResultTy, FunctionType *FuncTy, |
| 128 | MCSymbol *Target, ArgListTy &&ArgsList, |
| 129 | ImmutableCallSite &Call, |
| 130 | unsigned FixedArgs = ~0U) { |
| 131 | RetTy = ResultTy; |
| 132 | Callee = Call.getCalledValue(); |
| 133 | Symbol = Target; |
| 134 | |
| 135 | IsInReg = Call.hasRetAttr(Attribute::InReg); |
| 136 | DoesNotReturn = Call.doesNotReturn(); |
| 137 | IsVarArg = FuncTy->isVarArg(); |
| 138 | IsReturnValueUsed = !Call.getInstruction()->use_empty(); |
| 139 | RetSExt = Call.hasRetAttr(Attribute::SExt); |
| 140 | RetZExt = Call.hasRetAttr(Attribute::ZExt); |
| 141 | |
| 142 | CallConv = Call.getCallingConv(); |
| 143 | Args = std::move(ArgsList); |
| 144 | NumFixedArgs = (FixedArgs == ~0U) ? FuncTy->getNumParams() : FixedArgs; |
| 145 | |
| 146 | CS = &Call; |
| 147 | |
| 148 | return *this; |
| 149 | } |
| 150 | |
| 151 | CallLoweringInfo &setCallee(CallingConv::ID CC, Type *ResultTy, |
| 152 | const Value *Target, ArgListTy &&ArgsList, |
| 153 | unsigned FixedArgs = ~0U) { |
| 154 | RetTy = ResultTy; |
| 155 | Callee = Target; |
| 156 | CallConv = CC; |
| 157 | Args = std::move(ArgsList); |
| 158 | NumFixedArgs = (FixedArgs == ~0U) ? Args.size() : FixedArgs; |
| 159 | return *this; |
| 160 | } |
| 161 | |
| 162 | CallLoweringInfo &setCallee(const DataLayout &DL, MCContext &Ctx, |
| 163 | CallingConv::ID CC, Type *ResultTy, |
| 164 | StringRef Target, ArgListTy &&ArgsList, |
| 165 | unsigned FixedArgs = ~0U); |
| 166 | |
| 167 | CallLoweringInfo &setCallee(CallingConv::ID CC, Type *ResultTy, |
| 168 | MCSymbol *Target, ArgListTy &&ArgsList, |
| 169 | unsigned FixedArgs = ~0U) { |
| 170 | RetTy = ResultTy; |
| 171 | Symbol = Target; |
| 172 | CallConv = CC; |
| 173 | Args = std::move(ArgsList); |
| 174 | NumFixedArgs = (FixedArgs == ~0U) ? Args.size() : FixedArgs; |
| 175 | return *this; |
| 176 | } |
| 177 | |
| 178 | CallLoweringInfo &setTailCall(bool Value = true) { |
| 179 | IsTailCall = Value; |
| 180 | return *this; |
| 181 | } |
| 182 | |
| 183 | CallLoweringInfo &setIsPatchPoint(bool Value = true) { |
| 184 | IsPatchPoint = Value; |
| 185 | return *this; |
| 186 | } |
| 187 | |
| 188 | ArgListTy &getArgs() { return Args; } |
| 189 | |
| 190 | void clearOuts() { |
| 191 | OutVals.clear(); |
| 192 | OutFlags.clear(); |
| 193 | OutRegs.clear(); |
| 194 | } |
| 195 | |
| 196 | void clearIns() { |
| 197 | Ins.clear(); |
| 198 | InRegs.clear(); |
| 199 | } |
| 200 | }; |
| 201 | |
| 202 | protected: |
| 203 | DenseMap<const Value *, unsigned> LocalValueMap; |
| 204 | FunctionLoweringInfo &FuncInfo; |
| 205 | MachineFunction *MF; |
| 206 | MachineRegisterInfo &MRI; |
| 207 | MachineFrameInfo &MFI; |
| 208 | MachineConstantPool &MCP; |
| 209 | DebugLoc DbgLoc; |
| 210 | const TargetMachine &TM; |
| 211 | const DataLayout &DL; |
| 212 | const TargetInstrInfo &TII; |
| 213 | const TargetLowering &TLI; |
| 214 | const TargetRegisterInfo &TRI; |
| 215 | const TargetLibraryInfo *LibInfo; |
| 216 | bool SkipTargetIndependentISel; |
| 217 | |
| 218 | /// \brief The position of the last instruction for materializing constants |
| 219 | /// for use in the current block. It resets to EmitStartPt when it makes sense |
| 220 | /// (for example, it's usually profitable to avoid function calls between the |
| 221 | /// definition and the use) |
| 222 | MachineInstr *LastLocalValue; |
| 223 | |
| 224 | /// \brief The top most instruction in the current block that is allowed for |
| 225 | /// emitting local variables. LastLocalValue resets to EmitStartPt when it |
| 226 | /// makes sense (for example, on function calls) |
| 227 | MachineInstr *EmitStartPt; |
| 228 | |
| 229 | public: |
| 230 | virtual ~FastISel(); |
| 231 | |
| 232 | /// \brief Return the position of the last instruction emitted for |
| 233 | /// materializing constants for use in the current block. |
| 234 | MachineInstr *getLastLocalValue() { return LastLocalValue; } |
| 235 | |
| 236 | /// \brief Update the position of the last instruction emitted for |
| 237 | /// materializing constants for use in the current block. |
| 238 | void setLastLocalValue(MachineInstr *I) { |
| 239 | EmitStartPt = I; |
| 240 | LastLocalValue = I; |
| 241 | } |
| 242 | |
| 243 | /// \brief Set the current block to which generated machine instructions will |
| 244 | /// be appended. |
| 245 | void startNewBlock(); |
| 246 | |
| 247 | /// Flush the local value map and sink local values if possible. |
| 248 | void finishBasicBlock(); |
| 249 | |
| 250 | /// \brief Return current debug location information. |
| 251 | DebugLoc getCurDebugLoc() const { return DbgLoc; } |
| 252 | |
| 253 | /// \brief Do "fast" instruction selection for function arguments and append |
| 254 | /// the machine instructions to the current block. Returns true when |
| 255 | /// successful. |
| 256 | bool lowerArguments(); |
| 257 | |
| 258 | /// \brief Do "fast" instruction selection for the given LLVM IR instruction |
| 259 | /// and append the generated machine instructions to the current block. |
| 260 | /// Returns true if selection was successful. |
| 261 | bool selectInstruction(const Instruction *I); |
| 262 | |
| 263 | /// \brief Do "fast" instruction selection for the given LLVM IR operator |
| 264 | /// (Instruction or ConstantExpr), and append generated machine instructions |
| 265 | /// to the current block. Return true if selection was successful. |
| 266 | bool selectOperator(const User *I, unsigned Opcode); |
| 267 | |
| 268 | /// \brief Create a virtual register and arrange for it to be assigned the |
| 269 | /// value for the given LLVM value. |
| 270 | unsigned getRegForValue(const Value *V); |
| 271 | |
| 272 | /// \brief Look up the value to see if its value is already cached in a |
| 273 | /// register. It may be defined by instructions across blocks or defined |
| 274 | /// locally. |
| 275 | unsigned lookUpRegForValue(const Value *V); |
| 276 | |
| 277 | /// \brief This is a wrapper around getRegForValue that also takes care of |
| 278 | /// truncating or sign-extending the given getelementptr index value. |
| 279 | std::pair<unsigned, bool> getRegForGEPIndex(const Value *V); |
| 280 | |
| 281 | /// \brief We're checking to see if we can fold \p LI into \p FoldInst. Note |
| 282 | /// that we could have a sequence where multiple LLVM IR instructions are |
| 283 | /// folded into the same machineinstr. For example we could have: |
| 284 | /// |
| 285 | /// A: x = load i32 *P |
| 286 | /// B: y = icmp A, 42 |
| 287 | /// C: br y, ... |
| 288 | /// |
| 289 | /// In this scenario, \p LI is "A", and \p FoldInst is "C". We know about "B" |
| 290 | /// (and any other folded instructions) because it is between A and C. |
| 291 | /// |
| 292 | /// If we succeed folding, return true. |
| 293 | bool tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst); |
| 294 | |
| 295 | /// \brief The specified machine instr operand is a vreg, and that vreg is |
| 296 | /// being provided by the specified load instruction. If possible, try to |
| 297 | /// fold the load as an operand to the instruction, returning true if |
| 298 | /// possible. |
| 299 | /// |
| 300 | /// This method should be implemented by targets. |
| 301 | virtual bool tryToFoldLoadIntoMI(MachineInstr * /*MI*/, unsigned /*OpNo*/, |
| 302 | const LoadInst * /*LI*/) { |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | /// \brief Reset InsertPt to prepare for inserting instructions into the |
| 307 | /// current block. |
| 308 | void recomputeInsertPt(); |
| 309 | |
| 310 | /// \brief Remove all dead instructions between the I and E. |
| 311 | void removeDeadCode(MachineBasicBlock::iterator I, |
| 312 | MachineBasicBlock::iterator E); |
| 313 | |
| 314 | struct SavePoint { |
| 315 | MachineBasicBlock::iterator InsertPt; |
| 316 | DebugLoc DL; |
| 317 | }; |
| 318 | |
| 319 | /// \brief Prepare InsertPt to begin inserting instructions into the local |
| 320 | /// value area and return the old insert position. |
| 321 | SavePoint enterLocalValueArea(); |
| 322 | |
| 323 | /// \brief Reset InsertPt to the given old insert position. |
| 324 | void leaveLocalValueArea(SavePoint Old); |
| 325 | |
| 326 | protected: |
| 327 | explicit FastISel(FunctionLoweringInfo &FuncInfo, |
| 328 | const TargetLibraryInfo *LibInfo, |
| 329 | bool SkipTargetIndependentISel = false); |
| 330 | |
| 331 | /// \brief This method is called by target-independent code when the normal |
| 332 | /// FastISel process fails to select an instruction. This gives targets a |
| 333 | /// chance to emit code for anything that doesn't fit into FastISel's |
| 334 | /// framework. It returns true if it was successful. |
| 335 | virtual bool fastSelectInstruction(const Instruction *I) = 0; |
| 336 | |
| 337 | /// \brief This method is called by target-independent code to do target- |
| 338 | /// specific argument lowering. It returns true if it was successful. |
| 339 | virtual bool fastLowerArguments(); |
| 340 | |
| 341 | /// \brief This method is called by target-independent code to do target- |
| 342 | /// specific call lowering. It returns true if it was successful. |
| 343 | virtual bool fastLowerCall(CallLoweringInfo &CLI); |
| 344 | |
| 345 | /// \brief This method is called by target-independent code to do target- |
| 346 | /// specific intrinsic lowering. It returns true if it was successful. |
| 347 | virtual bool fastLowerIntrinsicCall(const IntrinsicInst *II); |
| 348 | |
| 349 | /// \brief This method is called by target-independent code to request that an |
| 350 | /// instruction with the given type and opcode be emitted. |
| 351 | virtual unsigned fastEmit_(MVT VT, MVT RetVT, unsigned Opcode); |
| 352 | |
| 353 | /// \brief This method is called by target-independent code to request that an |
| 354 | /// instruction with the given type, opcode, and register operand be emitted. |
| 355 | virtual unsigned fastEmit_r(MVT VT, MVT RetVT, unsigned Opcode, unsigned Op0, |
| 356 | bool Op0IsKill); |
| 357 | |
| 358 | /// \brief This method is called by target-independent code to request that an |
| 359 | /// instruction with the given type, opcode, and register operands be emitted. |
| 360 | virtual unsigned fastEmit_rr(MVT VT, MVT RetVT, unsigned Opcode, unsigned Op0, |
| 361 | bool Op0IsKill, unsigned Op1, bool Op1IsKill); |
| 362 | |
| 363 | /// \brief This method is called by target-independent code to request that an |
| 364 | /// instruction with the given type, opcode, and register and immediate |
| 365 | /// operands be emitted. |
| 366 | virtual unsigned fastEmit_ri(MVT VT, MVT RetVT, unsigned Opcode, unsigned Op0, |
| 367 | bool Op0IsKill, uint64_t Imm); |
| 368 | |
| 369 | /// \brief This method is a wrapper of fastEmit_ri. |
| 370 | /// |
| 371 | /// It first tries to emit an instruction with an immediate operand using |
| 372 | /// fastEmit_ri. If that fails, it materializes the immediate into a register |
| 373 | /// and try fastEmit_rr instead. |
| 374 | unsigned fastEmit_ri_(MVT VT, unsigned Opcode, unsigned Op0, bool Op0IsKill, |
| 375 | uint64_t Imm, MVT ImmType); |
| 376 | |
| 377 | /// \brief This method is called by target-independent code to request that an |
| 378 | /// instruction with the given type, opcode, and immediate operand be emitted. |
| 379 | virtual unsigned fastEmit_i(MVT VT, MVT RetVT, unsigned Opcode, uint64_t Imm); |
| 380 | |
| 381 | /// \brief This method is called by target-independent code to request that an |
| 382 | /// instruction with the given type, opcode, and floating-point immediate |
| 383 | /// operand be emitted. |
| 384 | virtual unsigned fastEmit_f(MVT VT, MVT RetVT, unsigned Opcode, |
| 385 | const ConstantFP *FPImm); |
| 386 | |
| 387 | /// \brief Emit a MachineInstr with no operands and a result register in the |
| 388 | /// given register class. |
| 389 | unsigned fastEmitInst_(unsigned MachineInstOpcode, |
| 390 | const TargetRegisterClass *RC); |
| 391 | |
| 392 | /// \brief Emit a MachineInstr with one register operand and a result register |
| 393 | /// in the given register class. |
| 394 | unsigned fastEmitInst_r(unsigned MachineInstOpcode, |
| 395 | const TargetRegisterClass *RC, unsigned Op0, |
| 396 | bool Op0IsKill); |
| 397 | |
| 398 | /// \brief Emit a MachineInstr with two register operands and a result |
| 399 | /// register in the given register class. |
| 400 | unsigned fastEmitInst_rr(unsigned MachineInstOpcode, |
| 401 | const TargetRegisterClass *RC, unsigned Op0, |
| 402 | bool Op0IsKill, unsigned Op1, bool Op1IsKill); |
| 403 | |
| 404 | /// \brief Emit a MachineInstr with three register operands and a result |
| 405 | /// register in the given register class. |
| 406 | unsigned fastEmitInst_rrr(unsigned MachineInstOpcode, |
| 407 | const TargetRegisterClass *RC, unsigned Op0, |
| 408 | bool Op0IsKill, unsigned Op1, bool Op1IsKill, |
| 409 | unsigned Op2, bool Op2IsKill); |
| 410 | |
| 411 | /// \brief Emit a MachineInstr with a register operand, an immediate, and a |
| 412 | /// result register in the given register class. |
| 413 | unsigned fastEmitInst_ri(unsigned MachineInstOpcode, |
| 414 | const TargetRegisterClass *RC, unsigned Op0, |
| 415 | bool Op0IsKill, uint64_t Imm); |
| 416 | |
| 417 | /// \brief Emit a MachineInstr with one register operand and two immediate |
| 418 | /// operands. |
| 419 | unsigned fastEmitInst_rii(unsigned MachineInstOpcode, |
| 420 | const TargetRegisterClass *RC, unsigned Op0, |
| 421 | bool Op0IsKill, uint64_t Imm1, uint64_t Imm2); |
| 422 | |
| 423 | /// \brief Emit a MachineInstr with a floating point immediate, and a result |
| 424 | /// register in the given register class. |
| 425 | unsigned fastEmitInst_f(unsigned MachineInstOpcode, |
| 426 | const TargetRegisterClass *RC, |
| 427 | const ConstantFP *FPImm); |
| 428 | |
| 429 | /// \brief Emit a MachineInstr with two register operands, an immediate, and a |
| 430 | /// result register in the given register class. |
| 431 | unsigned fastEmitInst_rri(unsigned MachineInstOpcode, |
| 432 | const TargetRegisterClass *RC, unsigned Op0, |
| 433 | bool Op0IsKill, unsigned Op1, bool Op1IsKill, |
| 434 | uint64_t Imm); |
| 435 | |
| 436 | /// \brief Emit a MachineInstr with a single immediate operand, and a result |
| 437 | /// register in the given register class. |
| 438 | unsigned fastEmitInst_i(unsigned MachineInstrOpcode, |
| 439 | const TargetRegisterClass *RC, uint64_t Imm); |
| 440 | |
| 441 | /// \brief Emit a MachineInstr for an extract_subreg from a specified index of |
| 442 | /// a superregister to a specified type. |
| 443 | unsigned fastEmitInst_extractsubreg(MVT RetVT, unsigned Op0, bool Op0IsKill, |
| 444 | uint32_t Idx); |
| 445 | |
| 446 | /// \brief Emit MachineInstrs to compute the value of Op with all but the |
| 447 | /// least significant bit set to zero. |
| 448 | unsigned fastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill); |
| 449 | |
| 450 | /// \brief Emit an unconditional branch to the given block, unless it is the |
| 451 | /// immediate (fall-through) successor, and update the CFG. |
| 452 | void fastEmitBranch(MachineBasicBlock *MBB, const DebugLoc &DL); |
| 453 | |
| 454 | /// Emit an unconditional branch to \p FalseMBB, obtains the branch weight |
| 455 | /// and adds TrueMBB and FalseMBB to the successor list. |
| 456 | void finishCondBranch(const BasicBlock *BranchBB, MachineBasicBlock *TrueMBB, |
| 457 | MachineBasicBlock *FalseMBB); |
| 458 | |
| 459 | /// \brief Update the value map to include the new mapping for this |
| 460 | /// instruction, or insert an extra copy to get the result in a previous |
| 461 | /// determined register. |
| 462 | /// |
| 463 | /// NOTE: This is only necessary because we might select a block that uses a |
| 464 | /// value before we select the block that defines the value. It might be |
| 465 | /// possible to fix this by selecting blocks in reverse postorder. |
| 466 | void updateValueMap(const Value *I, unsigned Reg, unsigned NumRegs = 1); |
| 467 | |
| 468 | unsigned createResultReg(const TargetRegisterClass *RC); |
| 469 | |
| 470 | /// \brief Try to constrain Op so that it is usable by argument OpNum of the |
| 471 | /// provided MCInstrDesc. If this fails, create a new virtual register in the |
| 472 | /// correct class and COPY the value there. |
| 473 | unsigned constrainOperandRegClass(const MCInstrDesc &II, unsigned Op, |
| 474 | unsigned OpNum); |
| 475 | |
| 476 | /// \brief Emit a constant in a register using target-specific logic, such as |
| 477 | /// constant pool loads. |
| 478 | virtual unsigned fastMaterializeConstant(const Constant *C) { return 0; } |
| 479 | |
| 480 | /// \brief Emit an alloca address in a register using target-specific logic. |
| 481 | virtual unsigned fastMaterializeAlloca(const AllocaInst *C) { return 0; } |
| 482 | |
| 483 | /// \brief Emit the floating-point constant +0.0 in a register using target- |
| 484 | /// specific logic. |
| 485 | virtual unsigned fastMaterializeFloatZero(const ConstantFP *CF) { |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | /// \brief Check if \c Add is an add that can be safely folded into \c GEP. |
| 490 | /// |
| 491 | /// \c Add can be folded into \c GEP if: |
| 492 | /// - \c Add is an add, |
| 493 | /// - \c Add's size matches \c GEP's, |
| 494 | /// - \c Add is in the same basic block as \c GEP, and |
| 495 | /// - \c Add has a constant operand. |
| 496 | bool canFoldAddIntoGEP(const User *GEP, const Value *Add); |
| 497 | |
| 498 | /// \brief Test whether the given value has exactly one use. |
| 499 | bool hasTrivialKill(const Value *V); |
| 500 | |
| 501 | /// \brief Create a machine mem operand from the given instruction. |
| 502 | MachineMemOperand *createMachineMemOperandFor(const Instruction *I) const; |
| 503 | |
| 504 | CmpInst::Predicate optimizeCmpPredicate(const CmpInst *CI) const; |
| 505 | |
| 506 | bool lowerCallTo(const CallInst *CI, MCSymbol *Symbol, unsigned NumArgs); |
| 507 | bool lowerCallTo(const CallInst *CI, const char *SymbolName, |
| 508 | unsigned NumArgs); |
| 509 | bool lowerCallTo(CallLoweringInfo &CLI); |
| 510 | |
| 511 | bool isCommutativeIntrinsic(IntrinsicInst const *II) { |
| 512 | switch (II->getIntrinsicID()) { |
| 513 | case Intrinsic::sadd_with_overflow: |
| 514 | case Intrinsic::uadd_with_overflow: |
| 515 | case Intrinsic::smul_with_overflow: |
| 516 | case Intrinsic::umul_with_overflow: |
| 517 | return true; |
| 518 | default: |
| 519 | return false; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | bool lowerCall(const CallInst *I); |
| 524 | /// \brief Select and emit code for a binary operator instruction, which has |
| 525 | /// an opcode which directly corresponds to the given ISD opcode. |
| 526 | bool selectBinaryOp(const User *I, unsigned ISDOpcode); |
| 527 | bool selectFNeg(const User *I); |
| 528 | bool selectGetElementPtr(const User *I); |
| 529 | bool selectStackmap(const CallInst *I); |
| 530 | bool selectPatchpoint(const CallInst *I); |
| 531 | bool selectCall(const User *Call); |
| 532 | bool selectIntrinsicCall(const IntrinsicInst *II); |
| 533 | bool selectBitCast(const User *I); |
| 534 | bool selectCast(const User *I, unsigned Opcode); |
| 535 | bool selectExtractValue(const User *I); |
| 536 | bool selectInsertValue(const User *I); |
| 537 | bool selectXRayCustomEvent(const CallInst *II); |
| 538 | |
| 539 | private: |
| 540 | /// \brief Handle PHI nodes in successor blocks. |
| 541 | /// |
| 542 | /// Emit code to ensure constants are copied into registers when needed. |
| 543 | /// Remember the virtual registers that need to be added to the Machine PHI |
| 544 | /// nodes as input. We cannot just directly add them, because expansion might |
| 545 | /// result in multiple MBB's for one BB. As such, the start of the BB might |
| 546 | /// correspond to a different MBB than the end. |
| 547 | bool handlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB); |
| 548 | |
| 549 | /// \brief Helper for materializeRegForValue to materialize a constant in a |
| 550 | /// target-independent way. |
| 551 | unsigned materializeConstant(const Value *V, MVT VT); |
| 552 | |
| 553 | /// \brief Helper for getRegForVale. This function is called when the value |
| 554 | /// isn't already available in a register and must be materialized with new |
| 555 | /// instructions. |
| 556 | unsigned materializeRegForValue(const Value *V, MVT VT); |
| 557 | |
| 558 | /// \brief Clears LocalValueMap and moves the area for the new local variables |
| 559 | /// to the beginning of the block. It helps to avoid spilling cached variables |
| 560 | /// across heavy instructions like calls. |
| 561 | void flushLocalValueMap(); |
| 562 | |
| 563 | /// \brief Removes dead local value instructions after SavedLastLocalvalue. |
| 564 | void removeDeadLocalValueCode(MachineInstr *SavedLastLocalValue); |
| 565 | |
| 566 | struct InstOrderMap { |
| 567 | DenseMap<MachineInstr *, unsigned> Orders; |
| 568 | MachineInstr *FirstTerminator = nullptr; |
| 569 | unsigned FirstTerminatorOrder = std::numeric_limits<unsigned>::max(); |
| 570 | |
| 571 | void initialize(MachineBasicBlock *MBB); |
| 572 | }; |
| 573 | |
| 574 | /// Sinks the local value materialization instruction LocalMI to its first use |
| 575 | /// in the basic block, or deletes it if it is not used. |
| 576 | void sinkLocalValueMaterialization(MachineInstr &LocalMI, unsigned DefReg, |
| 577 | InstOrderMap &OrderMap); |
| 578 | |
| 579 | /// \brief Insertion point before trying to select the current instruction. |
| 580 | MachineBasicBlock::iterator SavedInsertPt; |
| 581 | |
| 582 | /// \brief Add a stackmap or patchpoint intrinsic call's live variable |
| 583 | /// operands to a stackmap or patchpoint machine instruction. |
| 584 | bool addStackMapLiveVars(SmallVectorImpl<MachineOperand> &Ops, |
| 585 | const CallInst *CI, unsigned StartIdx); |
| 586 | bool lowerCallOperands(const CallInst *CI, unsigned ArgIdx, unsigned NumArgs, |
| 587 | const Value *Callee, bool ForceRetVoidTy, |
| 588 | CallLoweringInfo &CLI); |
| 589 | }; |
| 590 | |
| 591 | } // end namespace llvm |
| 592 | |
| 593 | #endif // LLVM_CODEGEN_FASTISEL_H |