| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#ifndef LLVM_CODEGEN_SELECTIONDAGISEL_H |
| 15 |
#define LLVM_CODEGEN_SELECTIONDAGISEL_H |
| 16 |
|
| 17 |
#include "llvm/CodeGen/MachineFunctionPass.h" |
| 18 |
#include "llvm/CodeGen/SelectionDAG.h" |
| 19 |
#include "llvm/CodeGen/TargetSubtargetInfo.h" |
| 20 |
#include "llvm/IR/BasicBlock.h" |
| 21 |
#include <memory> |
| 22 |
|
| 23 |
namespace llvm { |
| 24 |
class AAResults; |
| 25 |
class SelectionDAGBuilder; |
| 26 |
class SDValue; |
| 27 |
class MachineRegisterInfo; |
| 28 |
class MachineFunction; |
| 29 |
class OptimizationRemarkEmitter; |
| 30 |
class TargetLowering; |
| 31 |
class TargetLibraryInfo; |
| 32 |
class FunctionLoweringInfo; |
| 33 |
class SwiftErrorValueTracking; |
| 34 |
class GCFunctionInfo; |
| 35 |
class ScheduleDAGSDNodes; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class SelectionDAGISel : public MachineFunctionPass { |
| 40 |
public: |
| 41 |
TargetMachine &TM; |
| 42 |
const TargetLibraryInfo *LibInfo; |
| 43 |
std::unique_ptr<FunctionLoweringInfo> FuncInfo; |
| 44 |
SwiftErrorValueTracking *SwiftError; |
| 45 |
MachineFunction *MF; |
| 46 |
MachineRegisterInfo *RegInfo; |
| 47 |
SelectionDAG *CurDAG; |
| 48 |
std::unique_ptr<SelectionDAGBuilder> SDB; |
| 49 |
AAResults *AA = nullptr; |
| 50 |
GCFunctionInfo *GFI = nullptr; |
| 51 |
CodeGenOpt::Level OptLevel; |
| 52 |
const TargetInstrInfo *TII; |
| 53 |
const TargetLowering *TLI; |
| 54 |
bool FastISelFailed; |
| 55 |
SmallPtrSet<const Instruction *, 4> ElidedArgCopyInstrs; |
| 56 |
bool UseInstrRefDebugInfo = false; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
std::unique_ptr<OptimizationRemarkEmitter> ORE; |
| 61 |
|
| 62 |
static char ID; |
| 63 |
|
| 64 |
explicit SelectionDAGISel(TargetMachine &tm, |
| 65 |
CodeGenOpt::Level OL = CodeGenOpt::Default); |
| 66 |
~SelectionDAGISel() override; |
| 67 |
|
| 68 |
const TargetLowering *getTargetLowering() const { return TLI; } |
| 69 |
|
| 70 |
void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 71 |
|
| 72 |
bool runOnMachineFunction(MachineFunction &MF) override; |
| 73 |
|
| 74 |
virtual void emitFunctionEntryCode() {} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
virtual void PreprocessISelDAG() {} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
virtual void PostprocessISelDAG() {} |
| 83 |
|
| 84 |
|
| 85 |
virtual void Select(SDNode *N) = 0; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op, |
| 93 |
unsigned ConstraintID, |
| 94 |
std::vector<SDValue> &OutOps) { |
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
static bool IsLegalToFold(SDValue N, SDNode *U, SDNode *Root, |
| 107 |
CodeGenOpt::Level OptLevel, |
| 108 |
bool IgnoreChains = false); |
| 109 |
|
| 110 |
static void InvalidateNodeId(SDNode *N); |
| 111 |
static int getUninvalidatedNodeId(SDNode *N); |
| 112 |
|
| 113 |
static void EnforceNodeIdInvariant(SDNode *N); |
| 114 |
|
| 115 |
|
| 116 |
enum BuiltinOpcodes { |
| 117 |
OPC_Scope, |
| 118 |
OPC_RecordNode, |
| 119 |
OPC_RecordChild0, OPC_RecordChild1, OPC_RecordChild2, OPC_RecordChild3, |
| 120 |
OPC_RecordChild4, OPC_RecordChild5, OPC_RecordChild6, OPC_RecordChild7, |
| 121 |
OPC_RecordMemRef, |
| 122 |
OPC_CaptureGlueInput, |
| 123 |
OPC_MoveChild, |
| 124 |
OPC_MoveChild0, OPC_MoveChild1, OPC_MoveChild2, OPC_MoveChild3, |
| 125 |
OPC_MoveChild4, OPC_MoveChild5, OPC_MoveChild6, OPC_MoveChild7, |
| 126 |
OPC_MoveParent, |
| 127 |
OPC_CheckSame, |
| 128 |
OPC_CheckChild0Same, OPC_CheckChild1Same, |
| 129 |
OPC_CheckChild2Same, OPC_CheckChild3Same, |
| 130 |
OPC_CheckPatternPredicate, |
| 131 |
OPC_CheckPredicate, |
| 132 |
OPC_CheckPredicateWithOperands, |
| 133 |
OPC_CheckOpcode, |
| 134 |
OPC_SwitchOpcode, |
| 135 |
OPC_CheckType, |
| 136 |
OPC_CheckTypeRes, |
| 137 |
OPC_SwitchType, |
| 138 |
OPC_CheckChild0Type, OPC_CheckChild1Type, OPC_CheckChild2Type, |
| 139 |
OPC_CheckChild3Type, OPC_CheckChild4Type, OPC_CheckChild5Type, |
| 140 |
OPC_CheckChild6Type, OPC_CheckChild7Type, |
| 141 |
OPC_CheckInteger, |
| 142 |
OPC_CheckChild0Integer, OPC_CheckChild1Integer, OPC_CheckChild2Integer, |
| 143 |
OPC_CheckChild3Integer, OPC_CheckChild4Integer, |
| 144 |
OPC_CheckCondCode, OPC_CheckChild2CondCode, |
| 145 |
OPC_CheckValueType, |
| 146 |
OPC_CheckComplexPat, |
| 147 |
OPC_CheckAndImm, OPC_CheckOrImm, |
| 148 |
OPC_CheckImmAllOnesV, |
| 149 |
OPC_CheckImmAllZerosV, |
| 150 |
OPC_CheckFoldableChainNode, |
| 151 |
|
| 152 |
OPC_EmitInteger, |
| 153 |
OPC_EmitStringInteger, |
| 154 |
OPC_EmitRegister, |
| 155 |
OPC_EmitRegister2, |
| 156 |
OPC_EmitConvertToTarget, |
| 157 |
OPC_EmitMergeInputChains, |
| 158 |
OPC_EmitMergeInputChains1_0, |
| 159 |
OPC_EmitMergeInputChains1_1, |
| 160 |
OPC_EmitMergeInputChains1_2, |
| 161 |
OPC_EmitCopyToReg, |
| 162 |
OPC_EmitCopyToReg2, |
| 163 |
OPC_EmitNodeXForm, |
| 164 |
OPC_EmitNode, |
| 165 |
|
| 166 |
OPC_EmitNode0, OPC_EmitNode1, OPC_EmitNode2, |
| 167 |
OPC_MorphNodeTo, |
| 168 |
|
| 169 |
OPC_MorphNodeTo0, OPC_MorphNodeTo1, OPC_MorphNodeTo2, |
| 170 |
OPC_CompleteMatch, |
| 171 |
|
| 172 |
OPC_Coverage |
| 173 |
}; |
| 174 |
|
| 175 |
enum { |
| 176 |
OPFL_None = 0, |
| 177 |
OPFL_Chain = 1, |
| 178 |
OPFL_GlueInput = 2, |
| 179 |
OPFL_GlueOutput = 4, |
| 180 |
OPFL_MemRefs = 8, |
| 181 |
OPFL_Variadic0 = 1<<4, |
| 182 |
OPFL_Variadic1 = 2<<4, |
| 183 |
OPFL_Variadic2 = 3<<4, |
| 184 |
OPFL_Variadic3 = 4<<4, |
| 185 |
OPFL_Variadic4 = 5<<4, |
| 186 |
OPFL_Variadic5 = 6<<4, |
| 187 |
OPFL_Variadic6 = 7<<4, |
| 188 |
|
| 189 |
OPFL_VariadicInfo = OPFL_Variadic6 |
| 190 |
}; |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
static inline int getNumFixedFromVariadicInfo(unsigned Flags) { |
| 196 |
return ((Flags&OPFL_VariadicInfo) >> 4)-1; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
protected: |
| 201 |
|
| 202 |
|
| 203 |
unsigned DAGSize = 0; |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
void ReplaceUses(SDValue F, SDValue T) { |
| 208 |
CurDAG->ReplaceAllUsesOfValueWith(F, T); |
| 209 |
EnforceNodeIdInvariant(T.getNode()); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
void ReplaceUses(const SDValue *F, const SDValue *T, unsigned Num) { |
| 215 |
CurDAG->ReplaceAllUsesOfValuesWith(F, T, Num); |
| 216 |
for (unsigned i = 0; i < Num; ++i) |
| 217 |
EnforceNodeIdInvariant(T[i].getNode()); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
void ReplaceUses(SDNode *F, SDNode *T) { |
| 223 |
CurDAG->ReplaceAllUsesWith(F, T); |
| 224 |
EnforceNodeIdInvariant(T); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
void ReplaceNode(SDNode *F, SDNode *T) { |
| 229 |
CurDAG->ReplaceAllUsesWith(F, T); |
| 230 |
EnforceNodeIdInvariant(T); |
| 231 |
CurDAG->RemoveDeadNode(F); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
void SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops, |
| 237 |
const SDLoc &DL); |
| 238 |
|
| 239 |
|
| 240 |
virtual StringRef getPatternForIndex(unsigned index) { |
| 241 |
llvm_unreachable("Tblgen should generate the implementation of this!"); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
virtual StringRef getIncludePathForIndex(unsigned index) { |
| 246 |
llvm_unreachable("Tblgen should generate the implementation of this!"); |
| 247 |
} |
| 248 |
|
| 249 |
bool shouldOptForSize(const MachineFunction *MF) const { |
| 250 |
return CurDAG->shouldOptForSize(); |
| 251 |
} |
| 252 |
|
| 253 |
public: |
| 254 |
|
| 255 |
bool CheckAndMask(SDValue LHS, ConstantSDNode *RHS, |
| 256 |
int64_t DesiredMaskS) const; |
| 257 |
bool CheckOrMask(SDValue LHS, ConstantSDNode *RHS, |
| 258 |
int64_t DesiredMaskS) const; |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
virtual bool CheckPatternPredicate(unsigned PredNo) const { |
| 266 |
llvm_unreachable("Tblgen should generate the implementation of this!"); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
virtual bool CheckNodePredicate(SDNode *N, unsigned PredNo) const { |
| 274 |
llvm_unreachable("Tblgen should generate the implementation of this!"); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
virtual bool CheckNodePredicateWithOperands( |
| 283 |
SDNode *N, unsigned PredNo, |
| 284 |
const SmallVectorImpl<SDValue> &Operands) const { |
| 285 |
llvm_unreachable("Tblgen should generate the implementation of this!"); |
| 286 |
} |
| 287 |
|
| 288 |
virtual bool CheckComplexPattern(SDNode *Root, SDNode *Parent, SDValue N, |
| 289 |
unsigned PatternNo, |
| 290 |
SmallVectorImpl<std::pair<SDValue, SDNode*> > &Result) { |
| 291 |
llvm_unreachable("Tblgen should generate the implementation of this!"); |
| 292 |
} |
| 293 |
|
| 294 |
virtual SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) { |
| 295 |
llvm_unreachable("Tblgen should generate this!"); |
| 296 |
} |
| 297 |
|
| 298 |
void SelectCodeCommon(SDNode *NodeToMatch, const unsigned char *MatcherTable, |
| 299 |
unsigned TableSize); |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
virtual bool ComplexPatternFuncMutatesDAG() const { |
| 304 |
return false; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
bool mayRaiseFPException(SDNode *Node) const; |
| 309 |
|
| 310 |
bool isOrEquivalentToAdd(const SDNode *N) const; |
| 311 |
|
| 312 |
private: |
| 313 |
|
| 314 |
|
| 315 |
void Select_INLINEASM(SDNode *N); |
| 316 |
void Select_READ_REGISTER(SDNode *Op); |
| 317 |
void Select_WRITE_REGISTER(SDNode *Op); |
| 318 |
void Select_UNDEF(SDNode *N); |
| 319 |
void CannotYetSelect(SDNode *N); |
| 320 |
|
| 321 |
void Select_FREEZE(SDNode *N); |
| 322 |
void Select_ARITH_FENCE(SDNode *N); |
| 323 |
|
| 324 |
private: |
| 325 |
void DoInstructionSelection(); |
| 326 |
SDNode *MorphNode(SDNode *Node, unsigned TargetOpc, SDVTList VTList, |
| 327 |
ArrayRef<SDValue> Ops, unsigned EmitNodeInfo); |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
bool PrepareEHLandingPad(); |
| 333 |
|
| 334 |
|
| 335 |
void SelectAllBasicBlocks(const Function &Fn); |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
void SelectBasicBlock(BasicBlock::const_iterator Begin, |
| 341 |
BasicBlock::const_iterator End, |
| 342 |
bool &HadTailCall); |
| 343 |
void FinishBasicBlock(); |
| 344 |
|
| 345 |
void CodeGenAndEmitDAG(); |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
void LowerArguments(const Function &F); |
| 350 |
|
| 351 |
void ComputeLiveOutVRegInfo(); |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
ScheduleDAGSDNodes *CreateScheduler(); |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
std::vector<unsigned> OpcodeOffset; |
| 362 |
|
| 363 |
void UpdateChains(SDNode *NodeToMatch, SDValue InputChain, |
| 364 |
SmallVectorImpl<SDNode *> &ChainNodesMatched, |
| 365 |
bool isMorphNodeTo); |
| 366 |
}; |
| 367 |
|
| 368 |
} |
| 369 |
|
| 370 |
#endif |