1 //===- subzero/src/IceTargetLowering.h - Lowering interface -----*- C++ -*-===// 2 // 3 // The Subzero Code Generator 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 /// \brief Declares the TargetLowering, LoweringContext, and TargetDataLowering 12 /// classes. 13 /// 14 /// TargetLowering is an abstract class used to drive the translation/lowering 15 /// process. LoweringContext maintains a context for lowering each instruction, 16 /// offering conveniences such as iterating over non-deleted instructions. 17 /// TargetDataLowering is an abstract class used to drive the lowering/emission 18 /// of global initializers, external global declarations, and internal constant 19 /// pools. 20 /// 21 //===----------------------------------------------------------------------===// 22 23 #ifndef SUBZERO_SRC_ICETARGETLOWERING_H 24 #define SUBZERO_SRC_ICETARGETLOWERING_H 25 26 #include "IceBitVector.h" 27 #include "IceCfgNode.h" 28 #include "IceDefs.h" 29 #include "IceInst.h" // for the names of the Inst subtypes 30 #include "IceOperand.h" 31 #include "IceRegAlloc.h" 32 #include "IceTypes.h" 33 34 #include <utility> 35 36 namespace Ice { 37 38 // UnimplementedError is defined as a macro so that we can get actual line 39 // numbers. 40 #define UnimplementedError(Flags) \ 41 do { \ 42 if (!static_cast<const ClFlags &>(Flags).getSkipUnimplemented()) { \ 43 /* Use llvm_unreachable instead of report_fatal_error, which gives \ 44 better stack traces. */ \ 45 llvm_unreachable("Not yet implemented"); \ 46 abort(); \ 47 } \ 48 } while (0) 49 50 // UnimplementedLoweringError is similar in style to UnimplementedError. Given 51 // a TargetLowering object pointer and an Inst pointer, it adds appropriate 52 // FakeDef and FakeUse instructions to try maintain liveness consistency. 53 #define UnimplementedLoweringError(Target, Instr) \ 54 do { \ 55 if (getFlags().getSkipUnimplemented()) { \ 56 (Target)->addFakeDefUses(Instr); \ 57 } else { \ 58 /* Use llvm_unreachable instead of report_fatal_error, which gives \ 59 better stack traces. */ \ 60 llvm_unreachable( \ 61 (std::string("Not yet implemented: ") + Instr->getInstName()) \ 62 .c_str()); \ 63 abort(); \ 64 } \ 65 } while (0) 66 67 /// LoweringContext makes it easy to iterate through non-deleted instructions in 68 /// a node, and insert new (lowered) instructions at the current point. Along 69 /// with the instruction list container and associated iterators, it holds the 70 /// current node, which is needed when inserting new instructions in order to 71 /// track whether variables are used as single-block or multi-block. 72 class LoweringContext { 73 LoweringContext(const LoweringContext &) = delete; 74 LoweringContext &operator=(const LoweringContext &) = delete; 75 76 public: 77 LoweringContext() = default; 78 ~LoweringContext() = default; 79 void init(CfgNode *Node); getNextInst()80 Inst *getNextInst() const { 81 if (Next == End) 82 return nullptr; 83 return iteratorToInst(Next); 84 } getNextInst(InstList::iterator & Iter)85 Inst *getNextInst(InstList::iterator &Iter) const { 86 advanceForward(Iter); 87 if (Iter == End) 88 return nullptr; 89 return iteratorToInst(Iter); 90 } getNode()91 CfgNode *getNode() const { return Node; } atEnd()92 bool atEnd() const { return Cur == End; } getCur()93 InstList::iterator getCur() const { return Cur; } getNext()94 InstList::iterator getNext() const { return Next; } getEnd()95 InstList::iterator getEnd() const { return End; } 96 void insert(Inst *Instr); insert(Args &&...A)97 template <typename Inst, typename... Args> Inst *insert(Args &&... A) { 98 auto *New = Inst::create(Node->getCfg(), std::forward<Args>(A)...); 99 insert(New); 100 return New; 101 } 102 Inst *getLastInserted() const; advanceCur()103 void advanceCur() { Cur = Next; } advanceNext()104 void advanceNext() { advanceForward(Next); } setCur(InstList::iterator C)105 void setCur(InstList::iterator C) { Cur = C; } setNext(InstList::iterator N)106 void setNext(InstList::iterator N) { Next = N; } 107 void rewind(); setInsertPoint(const InstList::iterator & Position)108 void setInsertPoint(const InstList::iterator &Position) { Next = Position; } 109 void availabilityReset(); 110 void availabilityUpdate(); 111 Variable *availabilityGet(Operand *Src) const; 112 113 private: 114 /// Node is the argument to Inst::updateVars(). 115 CfgNode *Node = nullptr; 116 Inst *LastInserted = nullptr; 117 /// Cur points to the current instruction being considered. It is guaranteed 118 /// to point to a non-deleted instruction, or to be End. 119 InstList::iterator Cur; 120 /// Next doubles as a pointer to the next valid instruction (if any), and the 121 /// new-instruction insertion point. It is also updated for the caller in case 122 /// the lowering consumes more than one high-level instruction. It is 123 /// guaranteed to point to a non-deleted instruction after Cur, or to be End. 124 // TODO: Consider separating the notion of "next valid instruction" and "new 125 // instruction insertion point", to avoid confusion when previously-deleted 126 // instructions come between the two points. 127 InstList::iterator Next; 128 /// Begin is a copy of Insts.begin(), used if iterators are moved backward. 129 InstList::iterator Begin; 130 /// End is a copy of Insts.end(), used if Next needs to be advanced. 131 InstList::iterator End; 132 /// LastDest and LastSrc capture the parameters of the last "Dest=Src" simple 133 /// assignment inserted (provided Src is a variable). This is used for simple 134 /// availability analysis. 135 Variable *LastDest = nullptr; 136 Variable *LastSrc = nullptr; 137 138 void skipDeleted(InstList::iterator &I) const; 139 void advanceForward(InstList::iterator &I) const; 140 }; 141 142 /// A helper class to advance the LoweringContext at each loop iteration. 143 class PostIncrLoweringContext { 144 PostIncrLoweringContext() = delete; 145 PostIncrLoweringContext(const PostIncrLoweringContext &) = delete; 146 PostIncrLoweringContext &operator=(const PostIncrLoweringContext &) = delete; 147 148 public: PostIncrLoweringContext(LoweringContext & Context)149 explicit PostIncrLoweringContext(LoweringContext &Context) 150 : Context(Context) {} ~PostIncrLoweringContext()151 ~PostIncrLoweringContext() { 152 Context.advanceCur(); 153 Context.advanceNext(); 154 } 155 156 private: 157 LoweringContext &Context; 158 }; 159 160 /// TargetLowering is the base class for all backends in Subzero. In addition to 161 /// implementing the abstract methods in this class, each concrete target must 162 /// also implement a named constructor in its own namespace. For instance, for 163 /// X8632 we have: 164 /// 165 /// namespace X8632 { 166 /// void createTargetLowering(Cfg *Func); 167 /// } 168 class TargetLowering { 169 TargetLowering() = delete; 170 TargetLowering(const TargetLowering &) = delete; 171 TargetLowering &operator=(const TargetLowering &) = delete; 172 173 public: 174 static void staticInit(GlobalContext *Ctx); 175 // Each target must define a public static method: 176 // static void staticInit(GlobalContext *Ctx); 177 static bool shouldBePooled(const class Constant *C); 178 static Type getPointerType(); 179 180 static std::unique_ptr<TargetLowering> createLowering(TargetArch Target, 181 Cfg *Func); 182 183 virtual std::unique_ptr<Assembler> createAssembler() const = 0; 184 translate()185 void translate() { 186 switch (Func->getOptLevel()) { 187 case Opt_m1: 188 translateOm1(); 189 break; 190 case Opt_0: 191 translateO0(); 192 break; 193 case Opt_1: 194 translateO1(); 195 break; 196 case Opt_2: 197 translateO2(); 198 break; 199 } 200 } translateOm1()201 virtual void translateOm1() { 202 Func->setError("Target doesn't specify Om1 lowering steps."); 203 } translateO0()204 virtual void translateO0() { 205 Func->setError("Target doesn't specify O0 lowering steps."); 206 } translateO1()207 virtual void translateO1() { 208 Func->setError("Target doesn't specify O1 lowering steps."); 209 } translateO2()210 virtual void translateO2() { 211 Func->setError("Target doesn't specify O2 lowering steps."); 212 } 213 214 /// Generates calls to intrinsics for operations the Target can't handle. 215 void genTargetHelperCalls(); 216 /// Tries to do address mode optimization on a single instruction. 217 void doAddressOpt(); 218 /// Lowers a single non-Phi instruction. 219 void lower(); 220 /// Inserts and lowers a single high-level instruction at a specific insertion 221 /// point. 222 void lowerInst(CfgNode *Node, InstList::iterator Next, InstHighLevel *Instr); 223 /// Does preliminary lowering of the set of Phi instructions in the current 224 /// node. The main intention is to do what's needed to keep the unlowered Phi 225 /// instructions consistent with the lowered non-Phi instructions, e.g. to 226 /// lower 64-bit operands on a 32-bit target. prelowerPhis()227 virtual void prelowerPhis() {} 228 /// Tries to do branch optimization on a single instruction. Returns true if 229 /// some optimization was done. doBranchOpt(Inst *,const CfgNode *)230 virtual bool doBranchOpt(Inst * /*I*/, const CfgNode * /*NextNode*/) { 231 return false; 232 } 233 234 virtual SizeT getNumRegisters() const = 0; 235 /// Returns a variable pre-colored to the specified physical register. This is 236 /// generally used to get very direct access to the register such as in the 237 /// prolog or epilog or for marking scratch registers as killed by a call. If 238 /// a Type is not provided, a target-specific default type is used. 239 virtual Variable *getPhysicalRegister(RegNumT RegNum, 240 Type Ty = IceType_void) = 0; 241 /// Returns a printable name for the register. 242 virtual const char *getRegName(RegNumT RegNum, Type Ty) const = 0; 243 hasFramePointer()244 virtual bool hasFramePointer() const { return false; } 245 virtual void setHasFramePointer() = 0; 246 virtual RegNumT getStackReg() const = 0; 247 virtual RegNumT getFrameReg() const = 0; 248 virtual RegNumT getFrameOrStackReg() const = 0; 249 virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0; 250 virtual uint32_t getStackAlignment() const = 0; needsStackPointerAlignment()251 virtual bool needsStackPointerAlignment() const { return false; } 252 virtual void reserveFixedAllocaArea(size_t Size, size_t Align) = 0; 253 virtual int32_t getFrameFixedAllocaOffset() const = 0; maxOutArgsSizeBytes()254 virtual uint32_t maxOutArgsSizeBytes() const { return 0; } 255 // Addressing relative to frame pointer differs in MIPS compared to X86/ARM 256 // since MIPS decrements its stack pointer prior to saving it in the frame 257 // pointer register. getFramePointerOffset(uint32_t CurrentOffset,uint32_t Size)258 virtual uint32_t getFramePointerOffset(uint32_t CurrentOffset, 259 uint32_t Size) const { 260 return -(CurrentOffset + Size); 261 } 262 /// Return whether a 64-bit Variable should be split into a Variable64On32. 263 virtual bool shouldSplitToVariable64On32(Type Ty) const = 0; 264 265 /// Return whether a Vector Variable should be split into a VariableVecOn32. shouldSplitToVariableVecOn32(Type Ty)266 virtual bool shouldSplitToVariableVecOn32(Type Ty) const { 267 (void)Ty; 268 return false; 269 } 270 hasComputedFrame()271 bool hasComputedFrame() const { return HasComputedFrame; } 272 /// Returns true if this function calls a function that has the "returns 273 /// twice" attribute. callsReturnsTwice()274 bool callsReturnsTwice() const { return CallsReturnsTwice; } setCallsReturnsTwice(bool RetTwice)275 void setCallsReturnsTwice(bool RetTwice) { CallsReturnsTwice = RetTwice; } makeNextLabelNumber()276 SizeT makeNextLabelNumber() { return NextLabelNumber++; } makeNextJumpTableNumber()277 SizeT makeNextJumpTableNumber() { return NextJumpTableNumber++; } getContext()278 LoweringContext &getContext() { return Context; } getFunc()279 Cfg *getFunc() const { return Func; } getGlobalContext()280 GlobalContext *getGlobalContext() const { return Ctx; } 281 282 enum RegSet { 283 RegSet_None = 0, 284 RegSet_CallerSave = 1 << 0, 285 RegSet_CalleeSave = 1 << 1, 286 RegSet_StackPointer = 1 << 2, 287 RegSet_FramePointer = 1 << 3, 288 RegSet_All = ~RegSet_None 289 }; 290 using RegSetMask = uint32_t; 291 292 virtual SmallBitVector getRegisterSet(RegSetMask Include, 293 RegSetMask Exclude) const = 0; 294 /// Get the set of physical registers available for the specified Variable's 295 /// register class, applying register restrictions from the command line. 296 virtual const SmallBitVector & 297 getRegistersForVariable(const Variable *Var) const = 0; 298 /// Get the set of *all* physical registers available for the specified 299 /// Variable's register class, *not* applying register restrictions from the 300 /// command line. 301 virtual const SmallBitVector & 302 getAllRegistersForVariable(const Variable *Var) const = 0; 303 virtual const SmallBitVector &getAliasesForRegister(RegNumT) const = 0; 304 305 void regAlloc(RegAllocKind Kind); 306 void postRegallocSplitting(const SmallBitVector &RegMask); 307 308 /// Get the minimum number of clusters required for a jump table to be 309 /// considered. 310 virtual SizeT getMinJumpTableSize() const = 0; 311 virtual void emitJumpTable(const Cfg *Func, 312 const InstJumpTable *JumpTable) const = 0; 313 314 virtual void emitVariable(const Variable *Var) const = 0; 315 316 void emitWithoutPrefix(const ConstantRelocatable *CR, 317 const char *Suffix = "") const; 318 319 virtual void emit(const ConstantInteger32 *C) const = 0; 320 virtual void emit(const ConstantInteger64 *C) const = 0; 321 virtual void emit(const ConstantFloat *C) const = 0; 322 virtual void emit(const ConstantDouble *C) const = 0; 323 virtual void emit(const ConstantUndef *C) const = 0; 324 virtual void emit(const ConstantRelocatable *CR) const = 0; 325 326 /// Performs target-specific argument lowering. 327 virtual void lowerArguments() = 0; 328 initNodeForLowering(CfgNode *)329 virtual void initNodeForLowering(CfgNode *) {} 330 virtual void addProlog(CfgNode *Node) = 0; 331 virtual void addEpilog(CfgNode *Node) = 0; 332 333 /// Create a properly-typed "mov" instruction. This is primarily for local 334 /// variable splitting. createLoweredMove(Variable * Dest,Variable * SrcVar)335 virtual Inst *createLoweredMove(Variable *Dest, Variable *SrcVar) { 336 // TODO(stichnot): make pure virtual by implementing for all targets 337 (void)Dest; 338 (void)SrcVar; 339 llvm::report_fatal_error("createLoweredMove() unimplemented"); 340 return nullptr; 341 } 342 343 virtual ~TargetLowering() = default; 344 345 private: 346 // This control variable is used by AutoBundle (RAII-style bundle 347 // locking/unlocking) to prevent nested bundles. 348 bool AutoBundling = false; 349 350 /// This indicates whether we are in the genTargetHelperCalls phase, and 351 /// therefore can do things like scalarization. 352 bool GeneratingTargetHelpers = false; 353 354 // _bundle_lock(), and _bundle_unlock(), were made private to force subtargets 355 // to use the AutoBundle helper. 356 void 357 _bundle_lock(InstBundleLock::Option BundleOption = InstBundleLock::Opt_None) { 358 Context.insert<InstBundleLock>(BundleOption); 359 } _bundle_unlock()360 void _bundle_unlock() { Context.insert<InstBundleUnlock>(); } 361 362 protected: 363 /// AutoBundle provides RIAA-style bundling. Sub-targets are expected to use 364 /// it when emitting NaCl Bundles to ensure proper bundle_unlocking, and 365 /// prevent nested bundles. 366 /// 367 /// AutoBundle objects will emit a _bundle_lock during construction (but only 368 /// if sandboxed code generation was requested), and a bundle_unlock() during 369 /// destruction. By carefully scoping objects of this type, Subtargets can 370 /// ensure proper bundle emission. 371 class AutoBundle { 372 AutoBundle() = delete; 373 AutoBundle(const AutoBundle &) = delete; 374 AutoBundle &operator=(const AutoBundle &) = delete; 375 376 public: 377 explicit AutoBundle(TargetLowering *Target, InstBundleLock::Option Option = 378 InstBundleLock::Opt_None); 379 ~AutoBundle(); 380 381 private: 382 TargetLowering *const Target; 383 const bool NeedSandboxing; 384 }; 385 386 explicit TargetLowering(Cfg *Func); 387 // Applies command line filters to TypeToRegisterSet array. 388 static void filterTypeToRegisterSet( 389 GlobalContext *Ctx, int32_t NumRegs, SmallBitVector TypeToRegisterSet[], 390 size_t TypeToRegisterSetSize, 391 std::function<std::string(RegNumT)> getRegName, 392 std::function<const char *(RegClass)> getRegClassName); 393 virtual void lowerAlloca(const InstAlloca *Instr) = 0; 394 virtual void lowerArithmetic(const InstArithmetic *Instr) = 0; 395 virtual void lowerAssign(const InstAssign *Instr) = 0; 396 virtual void lowerBr(const InstBr *Instr) = 0; 397 virtual void lowerBreakpoint(const InstBreakpoint *Instr) = 0; 398 virtual void lowerCall(const InstCall *Instr) = 0; 399 virtual void lowerCast(const InstCast *Instr) = 0; 400 virtual void lowerFcmp(const InstFcmp *Instr) = 0; 401 virtual void lowerExtractElement(const InstExtractElement *Instr) = 0; 402 virtual void lowerIcmp(const InstIcmp *Instr) = 0; 403 virtual void lowerInsertElement(const InstInsertElement *Instr) = 0; 404 virtual void lowerIntrinsic(const InstIntrinsic *Instr) = 0; 405 virtual void lowerLoad(const InstLoad *Instr) = 0; 406 virtual void lowerPhi(const InstPhi *Instr) = 0; 407 virtual void lowerRet(const InstRet *Instr) = 0; 408 virtual void lowerSelect(const InstSelect *Instr) = 0; 409 virtual void lowerShuffleVector(const InstShuffleVector *Instr) = 0; 410 virtual void lowerStore(const InstStore *Instr) = 0; 411 virtual void lowerSwitch(const InstSwitch *Instr) = 0; 412 virtual void lowerUnreachable(const InstUnreachable *Instr) = 0; 413 virtual void lowerOther(const Inst *Instr); 414 415 virtual void genTargetHelperCallFor(Inst *Instr) = 0; 416 virtual uint32_t getCallStackArgumentsSizeBytes(const InstCall *Instr) = 0; 417 418 /// Opportunity to modify other instructions to help Address Optimization doAddressOptOther()419 virtual void doAddressOptOther() {} doAddressOptLoad()420 virtual void doAddressOptLoad() {} doAddressOptStore()421 virtual void doAddressOptStore() {} doAddressOptLoadSubVector()422 virtual void doAddressOptLoadSubVector() {} doAddressOptStoreSubVector()423 virtual void doAddressOptStoreSubVector() {} doMockBoundsCheck(Operand *)424 virtual void doMockBoundsCheck(Operand *) {} 425 /// This gives the target an opportunity to post-process the lowered expansion 426 /// before returning. postLower()427 virtual void postLower() {} 428 429 /// When the SkipUnimplemented flag is set, addFakeDefUses() gets invoked by 430 /// the UnimplementedLoweringError macro to insert fake uses of all the 431 /// instruction variables and a fake def of the instruction dest, in order to 432 /// preserve integrity of liveness analysis. 433 void addFakeDefUses(const Inst *Instr); 434 435 /// Find (non-SSA) instructions where the Dest variable appears in some source 436 /// operand, and set the IsDestRedefined flag. This keeps liveness analysis 437 /// consistent. 438 void markRedefinitions(); 439 440 /// Make a pass over the Cfg to determine which variables need stack slots and 441 /// place them in a sorted list (SortedSpilledVariables). Among those, vars, 442 /// classify the spill variables as local to the basic block vs global 443 /// (multi-block) in order to compute the parameters GlobalsSize and 444 /// SpillAreaSizeBytes (represents locals or general vars if the coalescing of 445 /// locals is disallowed) along with alignments required for variables in each 446 /// area. We rely on accurate VMetadata in order to classify a variable as 447 /// global vs local (otherwise the variable is conservatively global). The 448 /// in-args should be initialized to 0. 449 /// 450 /// This is only a pre-pass and the actual stack slot assignment is handled 451 /// separately. 452 /// 453 /// There may be target-specific Variable types, which will be handled by 454 /// TargetVarHook. If the TargetVarHook returns true, then the variable is 455 /// skipped and not considered with the rest of the spilled variables. 456 void getVarStackSlotParams(VarList &SortedSpilledVariables, 457 SmallBitVector &RegsUsed, size_t *GlobalsSize, 458 size_t *SpillAreaSizeBytes, 459 uint32_t *SpillAreaAlignmentBytes, 460 uint32_t *LocalsSlotsAlignmentBytes, 461 std::function<bool(Variable *)> TargetVarHook); 462 463 /// Calculate the amount of padding needed to align the local and global areas 464 /// to the required alignment. This assumes the globals/locals layout used by 465 /// getVarStackSlotParams and assignVarStackSlots. 466 void alignStackSpillAreas(uint32_t SpillAreaStartOffset, 467 uint32_t SpillAreaAlignmentBytes, 468 size_t GlobalsSize, 469 uint32_t LocalsSlotsAlignmentBytes, 470 uint32_t *SpillAreaPaddingBytes, 471 uint32_t *LocalsSlotsPaddingBytes); 472 473 /// Make a pass through the SortedSpilledVariables and actually assign stack 474 /// slots. SpillAreaPaddingBytes takes into account stack alignment padding. 475 /// The SpillArea starts after that amount of padding. This matches the scheme 476 /// in getVarStackSlotParams, where there may be a separate multi-block global 477 /// var spill area and a local var spill area. 478 void assignVarStackSlots(VarList &SortedSpilledVariables, 479 size_t SpillAreaPaddingBytes, 480 size_t SpillAreaSizeBytes, 481 size_t GlobalsAndSubsequentPaddingSize, 482 bool UsesFramePointer); 483 484 /// Sort the variables in Source based on required alignment. The variables 485 /// with the largest alignment need are placed in the front of the Dest list. 486 void sortVarsByAlignment(VarList &Dest, const VarList &Source) const; 487 488 InstCall *makeHelperCall(RuntimeHelper FuncID, Variable *Dest, SizeT MaxSrcs); 489 _set_dest_redefined()490 void _set_dest_redefined() { Context.getLastInserted()->setDestRedefined(); } 491 492 bool shouldOptimizeMemIntrins(); 493 494 void scalarizeArithmetic(InstArithmetic::OpKind K, Variable *Dest, 495 Operand *Src0, Operand *Src1); 496 497 /// Generalizes scalarizeArithmetic to support other instruction types. 498 /// 499 /// insertScalarInstruction is a function-like object with signature 500 /// (Variable *Dest, Variable *Src0, Variable *Src1) -> Instr *. 501 template <typename... Operands, 502 typename F = std::function<Inst *(Variable *, Operands *...)>> scalarizeInstruction(Variable * Dest,F insertScalarInstruction,Operands * ...Srcs)503 void scalarizeInstruction(Variable *Dest, F insertScalarInstruction, 504 Operands *... Srcs) { 505 assert(GeneratingTargetHelpers && 506 "scalarizeInstruction called during incorrect phase"); 507 const Type DestTy = Dest->getType(); 508 assert(isVectorType(DestTy)); 509 const Type DestElementTy = typeElementType(DestTy); 510 const SizeT NumElements = typeNumElements(DestTy); 511 512 Variable *T = Func->makeVariable(DestTy); 513 if (auto *VarVecOn32 = llvm::dyn_cast<VariableVecOn32>(T)) { 514 VarVecOn32->initVecElement(Func); 515 auto *Undef = ConstantUndef::create(Ctx, DestTy); 516 Context.insert<InstAssign>(T, Undef); 517 } else { 518 Context.insert<InstFakeDef>(T); 519 } 520 521 for (SizeT I = 0; I < NumElements; ++I) { 522 auto *Index = Ctx->getConstantInt32(I); 523 524 auto makeExtractThunk = [this, Index, NumElements](Operand *Src) { 525 return [this, Index, NumElements, Src]() { 526 (void)NumElements; 527 assert(typeNumElements(Src->getType()) == NumElements); 528 529 const auto ElementTy = typeElementType(Src->getType()); 530 auto *Op = Func->makeVariable(ElementTy); 531 Context.insert<InstExtractElement>(Op, Src, Index); 532 return Op; 533 }; 534 }; 535 536 // Perform the operation as a scalar operation. 537 auto *Res = Func->makeVariable(DestElementTy); 538 auto *Arith = applyToThunkedArgs(insertScalarInstruction, Res, 539 makeExtractThunk(Srcs)...); 540 genTargetHelperCallFor(Arith); 541 542 Variable *DestT = Func->makeVariable(DestTy); 543 Context.insert<InstInsertElement>(DestT, T, Res, Index); 544 T = DestT; 545 } 546 Context.insert<InstAssign>(Dest, T); 547 } 548 549 // applyToThunkedArgs is used by scalarizeInstruction. Ideally, we would just 550 // call insertScalarInstruction(Res, Srcs...), but C++ does not specify 551 // evaluation order which means this leads to an unpredictable final 552 // output. Instead, we wrap each of the Srcs in a thunk and these 553 // applyToThunkedArgs functions apply the thunks in a well defined order so we 554 // still get well-defined output. applyToThunkedArgs(std::function<Inst * (Variable *,Variable *)> insertScalarInstruction,Variable * Res,std::function<Variable * ()> thunk0)555 Inst *applyToThunkedArgs( 556 std::function<Inst *(Variable *, Variable *)> insertScalarInstruction, 557 Variable *Res, std::function<Variable *()> thunk0) { 558 auto *Src0 = thunk0(); 559 return insertScalarInstruction(Res, Src0); 560 } 561 562 Inst * applyToThunkedArgs(std::function<Inst * (Variable *,Variable *,Variable *)> insertScalarInstruction,Variable * Res,std::function<Variable * ()> thunk0,std::function<Variable * ()> thunk1)563 applyToThunkedArgs(std::function<Inst *(Variable *, Variable *, Variable *)> 564 insertScalarInstruction, 565 Variable *Res, std::function<Variable *()> thunk0, 566 std::function<Variable *()> thunk1) { 567 auto *Src0 = thunk0(); 568 auto *Src1 = thunk1(); 569 return insertScalarInstruction(Res, Src0, Src1); 570 } 571 applyToThunkedArgs(std::function<Inst * (Variable *,Variable *,Variable *,Variable *)> insertScalarInstruction,Variable * Res,std::function<Variable * ()> thunk0,std::function<Variable * ()> thunk1,std::function<Variable * ()> thunk2)572 Inst *applyToThunkedArgs( 573 std::function<Inst *(Variable *, Variable *, Variable *, Variable *)> 574 insertScalarInstruction, 575 Variable *Res, std::function<Variable *()> thunk0, 576 std::function<Variable *()> thunk1, std::function<Variable *()> thunk2) { 577 auto *Src0 = thunk0(); 578 auto *Src1 = thunk1(); 579 auto *Src2 = thunk2(); 580 return insertScalarInstruction(Res, Src0, Src1, Src2); 581 } 582 583 /// SandboxType enumerates all possible sandboxing strategies that 584 enum SandboxType { 585 ST_None, 586 ST_NaCl, 587 ST_Nonsfi, 588 }; 589 590 static SandboxType determineSandboxTypeFromFlags(const ClFlags &Flags); 591 592 Cfg *Func; 593 GlobalContext *Ctx; 594 bool HasComputedFrame = false; 595 bool CallsReturnsTwice = false; 596 SizeT NextLabelNumber = 0; 597 SizeT NextJumpTableNumber = 0; 598 LoweringContext Context; 599 const SandboxType SandboxingType = ST_None; 600 601 const static constexpr char *H_getIP_prefix = "__Sz_getIP_"; 602 }; 603 604 /// TargetDataLowering is used for "lowering" data including initializers for 605 /// global variables, and the internal constant pools. It is separated out from 606 /// TargetLowering because it does not require a Cfg. 607 class TargetDataLowering { 608 TargetDataLowering() = delete; 609 TargetDataLowering(const TargetDataLowering &) = delete; 610 TargetDataLowering &operator=(const TargetDataLowering &) = delete; 611 612 public: 613 static std::unique_ptr<TargetDataLowering> createLowering(GlobalContext *Ctx); 614 virtual ~TargetDataLowering(); 615 616 virtual void lowerGlobals(const VariableDeclarationList &Vars, 617 const std::string &SectionSuffix) = 0; 618 virtual void lowerConstants() = 0; 619 virtual void lowerJumpTables() = 0; emitTargetRODataSections()620 virtual void emitTargetRODataSections() {} 621 622 protected: 623 void emitGlobal(const VariableDeclaration &Var, 624 const std::string &SectionSuffix); 625 626 /// For now, we assume .long is the right directive for emitting 4 byte emit 627 /// global relocations. However, LLVM MIPS usually uses .4byte instead. 628 /// Perhaps there is some difference when the location is unaligned. getEmit32Directive()629 static const char *getEmit32Directive() { return ".long"; } 630 TargetDataLowering(GlobalContext * Ctx)631 explicit TargetDataLowering(GlobalContext *Ctx) : Ctx(Ctx) {} 632 GlobalContext *Ctx; 633 }; 634 635 /// TargetHeaderLowering is used to "lower" the header of an output file. It 636 /// writes out the target-specific header attributes. E.g., for ARM this writes 637 /// out the build attributes (float ABI, etc.). 638 class TargetHeaderLowering { 639 TargetHeaderLowering() = delete; 640 TargetHeaderLowering(const TargetHeaderLowering &) = delete; 641 TargetHeaderLowering &operator=(const TargetHeaderLowering &) = delete; 642 643 public: 644 static std::unique_ptr<TargetHeaderLowering> 645 createLowering(GlobalContext *Ctx); 646 virtual ~TargetHeaderLowering(); 647 lower()648 virtual void lower() {} 649 650 protected: TargetHeaderLowering(GlobalContext * Ctx)651 explicit TargetHeaderLowering(GlobalContext *Ctx) : Ctx(Ctx) {} 652 GlobalContext *Ctx; 653 }; 654 655 } // end of namespace Ice 656 657 #endif // SUBZERO_SRC_ICETARGETLOWERING_H 658