Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
| 11 | /// This file declares the TargetLowering, LoweringContext, and |
| 12 | /// TargetDataLowering classes. TargetLowering is an abstract class |
| 13 | /// used to drive the translation/lowering process. LoweringContext |
| 14 | /// maintains a context for lowering each instruction, offering |
| 15 | /// conveniences such as iterating over non-deleted instructions. |
| 16 | /// TargetDataLowering is an abstract class used to drive the |
| 17 | /// lowering/emission of global initializers, external global |
| 18 | /// declarations, and internal constant pools. |
| 19 | /// |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #ifndef SUBZERO_SRC_ICETARGETLOWERING_H |
| 23 | #define SUBZERO_SRC_ICETARGETLOWERING_H |
| 24 | |
| 25 | #include "IceDefs.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 26 | #include "IceInst.h" // for the names of the Inst subtypes |
Jan Voung | 76bb0be | 2015-05-14 09:26:19 -0700 | [diff] [blame] | 27 | #include "IceOperand.h" |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 28 | #include "IceTypes.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 29 | |
| 30 | namespace Ice { |
| 31 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 32 | /// LoweringContext makes it easy to iterate through non-deleted |
| 33 | /// instructions in a node, and insert new (lowered) instructions at |
| 34 | /// the current point. Along with the instruction list container and |
| 35 | /// associated iterators, it holds the current node, which is needed |
| 36 | /// when inserting new instructions in order to track whether variables |
| 37 | /// are used as single-block or multi-block. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 38 | class LoweringContext { |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 39 | LoweringContext(const LoweringContext &) = delete; |
| 40 | LoweringContext &operator=(const LoweringContext &) = delete; |
| 41 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 42 | public: |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 43 | LoweringContext() = default; |
| 44 | ~LoweringContext() = default; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 45 | void init(CfgNode *Node); |
| 46 | Inst *getNextInst() const { |
| 47 | if (Next == End) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 48 | return nullptr; |
Jim Stichnoth | 607e9f0 | 2014-11-06 13:32:05 -0800 | [diff] [blame] | 49 | return Next; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 50 | } |
Jan Voung | c820ddf | 2014-07-29 14:38:51 -0700 | [diff] [blame] | 51 | Inst *getNextInst(InstList::iterator &Iter) const { |
Jan Voung | e6e497d | 2014-07-30 10:06:03 -0700 | [diff] [blame] | 52 | advanceForward(Iter); |
Jan Voung | c820ddf | 2014-07-29 14:38:51 -0700 | [diff] [blame] | 53 | if (Iter == End) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 54 | return nullptr; |
Jim Stichnoth | 607e9f0 | 2014-11-06 13:32:05 -0800 | [diff] [blame] | 55 | return Iter; |
Jan Voung | c820ddf | 2014-07-29 14:38:51 -0700 | [diff] [blame] | 56 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 57 | CfgNode *getNode() const { return Node; } |
| 58 | bool atEnd() const { return Cur == End; } |
| 59 | InstList::iterator getCur() const { return Cur; } |
Jim Stichnoth | 5d2fa0c | 2014-12-01 09:30:55 -0800 | [diff] [blame] | 60 | InstList::iterator getNext() const { return Next; } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 61 | InstList::iterator getEnd() const { return End; } |
| 62 | void insert(Inst *Inst); |
Jan Voung | e6e497d | 2014-07-30 10:06:03 -0700 | [diff] [blame] | 63 | Inst *getLastInserted() const; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 64 | void advanceCur() { Cur = Next; } |
Jan Voung | e6e497d | 2014-07-30 10:06:03 -0700 | [diff] [blame] | 65 | void advanceNext() { advanceForward(Next); } |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 66 | void setCur(InstList::iterator C) { Cur = C; } |
| 67 | void setNext(InstList::iterator N) { Next = N; } |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 68 | void rewind(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 69 | void setInsertPoint(const InstList::iterator &Position) { Next = Position; } |
| 70 | |
| 71 | private: |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 72 | /// Node is the argument to Inst::updateVars(). |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 73 | CfgNode *Node = nullptr; |
| 74 | Inst *LastInserted = nullptr; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 75 | /// Cur points to the current instruction being considered. It is |
| 76 | /// guaranteed to point to a non-deleted instruction, or to be End. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 77 | InstList::iterator Cur; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 78 | /// Next doubles as a pointer to the next valid instruction (if any), |
| 79 | /// and the new-instruction insertion point. It is also updated for |
| 80 | /// the caller in case the lowering consumes more than one high-level |
| 81 | /// instruction. It is guaranteed to point to a non-deleted |
| 82 | /// instruction after Cur, or to be End. TODO: Consider separating |
| 83 | /// the notion of "next valid instruction" and "new instruction |
| 84 | /// insertion point", to avoid confusion when previously-deleted |
| 85 | /// instructions come between the two points. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 86 | InstList::iterator Next; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 87 | /// Begin is a copy of Insts.begin(), used if iterators are moved backward. |
Jan Voung | e6e497d | 2014-07-30 10:06:03 -0700 | [diff] [blame] | 88 | InstList::iterator Begin; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 89 | /// End is a copy of Insts.end(), used if Next needs to be advanced. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 90 | InstList::iterator End; |
| 91 | |
Jan Voung | c820ddf | 2014-07-29 14:38:51 -0700 | [diff] [blame] | 92 | void skipDeleted(InstList::iterator &I) const; |
Jan Voung | e6e497d | 2014-07-30 10:06:03 -0700 | [diff] [blame] | 93 | void advanceForward(InstList::iterator &I) const; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 96 | /// A helper class to advance the LoweringContext at each loop iteration. |
| 97 | class PostIncrLoweringContext { |
| 98 | PostIncrLoweringContext() = delete; |
| 99 | PostIncrLoweringContext(const PostIncrLoweringContext &) = delete; |
| 100 | PostIncrLoweringContext &operator=(const PostIncrLoweringContext &) = delete; |
| 101 | |
| 102 | public: |
| 103 | explicit PostIncrLoweringContext(LoweringContext &Context) |
| 104 | : Context(Context) {} |
| 105 | ~PostIncrLoweringContext() { |
| 106 | Context.advanceCur(); |
| 107 | Context.advanceNext(); |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | LoweringContext &Context; |
| 112 | }; |
| 113 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 114 | class TargetLowering { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 115 | TargetLowering() = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 116 | TargetLowering(const TargetLowering &) = delete; |
| 117 | TargetLowering &operator=(const TargetLowering &) = delete; |
| 118 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 119 | public: |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 120 | // TODO(jvoung): return a unique_ptr like the other factory functions. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 121 | static TargetLowering *createLowering(TargetArch Target, Cfg *Func); |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 122 | static std::unique_ptr<Assembler> createAssembler(TargetArch Target, |
| 123 | Cfg *Func); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 124 | void translate() { |
Jan Voung | 1f47ad0 | 2015-03-20 15:01:26 -0700 | [diff] [blame] | 125 | switch (Ctx->getFlags().getOptLevel()) { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 126 | case Opt_m1: |
| 127 | translateOm1(); |
| 128 | break; |
| 129 | case Opt_0: |
| 130 | translateO0(); |
| 131 | break; |
| 132 | case Opt_1: |
| 133 | translateO1(); |
| 134 | break; |
| 135 | case Opt_2: |
| 136 | translateO2(); |
| 137 | break; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | virtual void translateOm1() { |
| 141 | Func->setError("Target doesn't specify Om1 lowering steps."); |
| 142 | } |
| 143 | virtual void translateO0() { |
| 144 | Func->setError("Target doesn't specify O0 lowering steps."); |
| 145 | } |
| 146 | virtual void translateO1() { |
| 147 | Func->setError("Target doesn't specify O1 lowering steps."); |
| 148 | } |
| 149 | virtual void translateO2() { |
| 150 | Func->setError("Target doesn't specify O2 lowering steps."); |
| 151 | } |
| 152 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 153 | /// Tries to do address mode optimization on a single instruction. |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 154 | void doAddressOpt(); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 155 | /// Randomly insert NOPs. |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 156 | void doNopInsertion(RandomNumberGenerator &RNG); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 157 | /// Lowers a single non-Phi instruction. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 158 | void lower(); |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 159 | /// Inserts and lowers a single high-level instruction at a specific insertion |
| 160 | /// point. |
| 161 | void lowerInst(CfgNode *Node, InstList::iterator Next, InstHighLevel *Instr); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 162 | /// Does preliminary lowering of the set of Phi instructions in the |
| 163 | /// current node. The main intention is to do what's needed to keep |
| 164 | /// the unlowered Phi instructions consistent with the lowered |
| 165 | /// non-Phi instructions, e.g. to lower 64-bit operands on a 32-bit |
| 166 | /// target. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 167 | virtual void prelowerPhis() {} |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 168 | /// Tries to do branch optimization on a single instruction. Returns |
| 169 | /// true if some optimization was done. |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 170 | virtual bool doBranchOpt(Inst * /*I*/, const CfgNode * /*NextNode*/) { |
| 171 | return false; |
| 172 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 173 | |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 174 | virtual SizeT getNumRegisters() const = 0; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 175 | /// Returns a variable pre-colored to the specified physical |
| 176 | /// register. This is generally used to get very direct access to |
| 177 | /// the register such as in the prolog or epilog or for marking |
| 178 | /// scratch registers as killed by a call. If a Type is not |
| 179 | /// provided, a target-specific default type is used. |
Jim Stichnoth | 98712a3 | 2014-10-24 10:59:02 -0700 | [diff] [blame] | 180 | virtual Variable *getPhysicalRegister(SizeT RegNum, |
| 181 | Type Ty = IceType_void) = 0; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 182 | /// Returns a printable name for the register. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 183 | virtual IceString getRegName(SizeT RegNum, Type Ty) const = 0; |
| 184 | |
| 185 | virtual bool hasFramePointer() const { return false; } |
| 186 | virtual SizeT getFrameOrStackReg() const = 0; |
Matt Wala | d4799f4 | 2014-08-14 14:24:12 -0700 | [diff] [blame] | 187 | virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0; |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 188 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 189 | bool hasComputedFrame() const { return HasComputedFrame; } |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 190 | /// Returns true if this function calls a function that has the |
| 191 | /// "returns twice" attribute. |
Jan Voung | 44d53e1 | 2014-09-11 19:18:03 -0700 | [diff] [blame] | 192 | bool callsReturnsTwice() const { return CallsReturnsTwice; } |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 193 | void setCallsReturnsTwice(bool RetTwice) { CallsReturnsTwice = RetTwice; } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 194 | int32_t getStackAdjustment() const { return StackAdjustment; } |
| 195 | void updateStackAdjustment(int32_t Offset) { StackAdjustment += Offset; } |
| 196 | void resetStackAdjustment() { StackAdjustment = 0; } |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 197 | SizeT makeNextLabelNumber() { return NextLabelNumber++; } |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 198 | SizeT makeNextJumpTableNumber() { return NextJumpTableNumber++; } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 199 | LoweringContext &getContext() { return Context; } |
| 200 | |
| 201 | enum RegSet { |
| 202 | RegSet_None = 0, |
| 203 | RegSet_CallerSave = 1 << 0, |
| 204 | RegSet_CalleeSave = 1 << 1, |
| 205 | RegSet_StackPointer = 1 << 2, |
| 206 | RegSet_FramePointer = 1 << 3, |
| 207 | RegSet_All = ~RegSet_None |
| 208 | }; |
| 209 | typedef uint32_t RegSetMask; |
| 210 | |
| 211 | virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include, |
| 212 | RegSetMask Exclude) const = 0; |
| 213 | virtual const llvm::SmallBitVector &getRegisterSetForType(Type Ty) const = 0; |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 214 | void regAlloc(RegAllocKind Kind); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 215 | |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 216 | virtual void |
| 217 | makeRandomRegisterPermutation(llvm::SmallVectorImpl<int32_t> &Permutation, |
| 218 | const llvm::SmallBitVector &ExcludeRegisters, |
| 219 | uint64_t Salt) const = 0; |
Jim Stichnoth | e6d2478 | 2014-12-19 05:42:24 -0800 | [diff] [blame] | 220 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 221 | /// Save/restore any mutable state for the situation where code |
| 222 | /// emission needs multiple passes, such as sandboxing or relaxation. |
| 223 | /// Subclasses may provide their own implementation, but should be |
| 224 | /// sure to also call the parent class's methods. |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 225 | virtual void snapshotEmitState() { |
| 226 | SnapshotStackAdjustment = StackAdjustment; |
| 227 | } |
| 228 | virtual void rollbackEmitState() { |
| 229 | StackAdjustment = SnapshotStackAdjustment; |
| 230 | } |
| 231 | |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 232 | /// Get the minimum number of clusters required for a jump table to be |
| 233 | /// considered. |
| 234 | virtual SizeT getMinJumpTableSize() const = 0; |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 235 | virtual void emitJumpTable(const Cfg *Func, |
| 236 | const InstJumpTable *JumpTable) const = 0; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 237 | |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 238 | virtual void emitVariable(const Variable *Var) const = 0; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 239 | |
Jan Voung | 76bb0be | 2015-05-14 09:26:19 -0700 | [diff] [blame] | 240 | void emitWithoutPrefix(const ConstantRelocatable *CR) const; |
| 241 | void emit(const ConstantRelocatable *CR) const; |
| 242 | virtual const char *getConstantPrefix() const = 0; |
| 243 | |
| 244 | virtual void emit(const ConstantUndef *C) const = 0; |
| 245 | virtual void emit(const ConstantInteger32 *C) const = 0; |
| 246 | virtual void emit(const ConstantInteger64 *C) const = 0; |
| 247 | virtual void emit(const ConstantFloat *C) const = 0; |
| 248 | virtual void emit(const ConstantDouble *C) const = 0; |
| 249 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 250 | /// Performs target-specific argument lowering. |
Matt Wala | 45a0623 | 2014-07-09 16:33:22 -0700 | [diff] [blame] | 251 | virtual void lowerArguments() = 0; |
| 252 | |
Jim Stichnoth | a59ae6f | 2015-05-17 10:11:41 -0700 | [diff] [blame] | 253 | virtual void initNodeForLowering(CfgNode *) {} |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 254 | virtual void addProlog(CfgNode *Node) = 0; |
| 255 | virtual void addEpilog(CfgNode *Node) = 0; |
| 256 | |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 257 | virtual ~TargetLowering() = default; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 258 | |
| 259 | protected: |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 260 | explicit TargetLowering(Cfg *Func); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 261 | virtual void lowerAlloca(const InstAlloca *Inst) = 0; |
| 262 | virtual void lowerArithmetic(const InstArithmetic *Inst) = 0; |
| 263 | virtual void lowerAssign(const InstAssign *Inst) = 0; |
| 264 | virtual void lowerBr(const InstBr *Inst) = 0; |
| 265 | virtual void lowerCall(const InstCall *Inst) = 0; |
| 266 | virtual void lowerCast(const InstCast *Inst) = 0; |
| 267 | virtual void lowerFcmp(const InstFcmp *Inst) = 0; |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 268 | virtual void lowerExtractElement(const InstExtractElement *Inst) = 0; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 269 | virtual void lowerIcmp(const InstIcmp *Inst) = 0; |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 270 | virtual void lowerInsertElement(const InstInsertElement *Inst) = 0; |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 271 | virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 272 | virtual void lowerLoad(const InstLoad *Inst) = 0; |
| 273 | virtual void lowerPhi(const InstPhi *Inst) = 0; |
| 274 | virtual void lowerRet(const InstRet *Inst) = 0; |
| 275 | virtual void lowerSelect(const InstSelect *Inst) = 0; |
| 276 | virtual void lowerStore(const InstStore *Inst) = 0; |
| 277 | virtual void lowerSwitch(const InstSwitch *Inst) = 0; |
| 278 | virtual void lowerUnreachable(const InstUnreachable *Inst) = 0; |
Jim Stichnoth | e4f65d8 | 2015-06-17 22:16:02 -0700 | [diff] [blame] | 279 | virtual void lowerOther(const Inst *Instr); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 280 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 281 | virtual void doAddressOptLoad() {} |
| 282 | virtual void doAddressOptStore() {} |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 283 | virtual void randomlyInsertNop(float Probability, |
| 284 | RandomNumberGenerator &RNG) = 0; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 285 | /// This gives the target an opportunity to post-process the lowered |
| 286 | /// expansion before returning. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 287 | virtual void postLower() {} |
| 288 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 289 | /// Find two-address non-SSA instructions and set the DestNonKillable flag |
| 290 | /// to keep liveness analysis consistent. |
Jan Voung | b3401d2 | 2015-05-18 09:38:21 -0700 | [diff] [blame] | 291 | void inferTwoAddress(); |
| 292 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 293 | /// Make a pass over the Cfg to determine which variables need stack slots |
| 294 | /// and place them in a sorted list (SortedSpilledVariables). Among those, |
| 295 | /// vars, classify the spill variables as local to the basic block vs |
| 296 | /// global (multi-block) in order to compute the parameters GlobalsSize |
| 297 | /// and SpillAreaSizeBytes (represents locals or general vars if the |
| 298 | /// coalescing of locals is disallowed) along with alignments required |
| 299 | /// for variables in each area. We rely on accurate VMetadata in order to |
| 300 | /// classify a variable as global vs local (otherwise the variable is |
| 301 | /// conservatively global). The in-args should be initialized to 0. |
| 302 | /// |
| 303 | /// This is only a pre-pass and the actual stack slot assignment is |
| 304 | /// handled separately. |
| 305 | /// |
| 306 | /// There may be target-specific Variable types, which will be handled |
| 307 | /// by TargetVarHook. If the TargetVarHook returns true, then the variable |
| 308 | /// is skipped and not considered with the rest of the spilled variables. |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 309 | void getVarStackSlotParams(VarList &SortedSpilledVariables, |
| 310 | llvm::SmallBitVector &RegsUsed, |
| 311 | size_t *GlobalsSize, size_t *SpillAreaSizeBytes, |
| 312 | uint32_t *SpillAreaAlignmentBytes, |
| 313 | uint32_t *LocalsSlotsAlignmentBytes, |
| 314 | std::function<bool(Variable *)> TargetVarHook); |
| 315 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 316 | /// Calculate the amount of padding needed to align the local and global |
| 317 | /// areas to the required alignment. This assumes the globals/locals layout |
| 318 | /// used by getVarStackSlotParams and assignVarStackSlots. |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 319 | void alignStackSpillAreas(uint32_t SpillAreaStartOffset, |
| 320 | uint32_t SpillAreaAlignmentBytes, |
| 321 | size_t GlobalsSize, |
| 322 | uint32_t LocalsSlotsAlignmentBytes, |
| 323 | uint32_t *SpillAreaPaddingBytes, |
| 324 | uint32_t *LocalsSlotsPaddingBytes); |
| 325 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 326 | /// Make a pass through the SortedSpilledVariables and actually assign |
| 327 | /// stack slots. SpillAreaPaddingBytes takes into account stack alignment |
| 328 | /// padding. The SpillArea starts after that amount of padding. |
| 329 | /// This matches the scheme in getVarStackSlotParams, where there may |
| 330 | /// be a separate multi-block global var spill area and a local var |
| 331 | /// spill area. |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 332 | void assignVarStackSlots(VarList &SortedSpilledVariables, |
| 333 | size_t SpillAreaPaddingBytes, |
| 334 | size_t SpillAreaSizeBytes, |
| 335 | size_t GlobalsAndSubsequentPaddingSize, |
| 336 | bool UsesFramePointer); |
| 337 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 338 | /// Sort the variables in Source based on required alignment. |
| 339 | /// The variables with the largest alignment need are placed in the front |
| 340 | /// of the Dest list. |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 341 | void sortVarsByAlignment(VarList &Dest, const VarList &Source) const; |
| 342 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 343 | /// Make a call to an external helper function. |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 344 | InstCall *makeHelperCall(const IceString &Name, Variable *Dest, |
| 345 | SizeT MaxSrcs); |
| 346 | |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 347 | void |
| 348 | _bundle_lock(InstBundleLock::Option BundleOption = InstBundleLock::Opt_None) { |
| 349 | Context.insert(InstBundleLock::create(Func, BundleOption)); |
| 350 | } |
| 351 | void _bundle_unlock() { Context.insert(InstBundleUnlock::create(Func)); } |
Jan Voung | f645d85 | 2015-07-09 10:35:09 -0700 | [diff] [blame] | 352 | void _set_dest_nonkillable() { |
| 353 | Context.getLastInserted()->setDestNonKillable(); |
| 354 | } |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 355 | |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 356 | bool shouldOptimizeMemIntrins(); |
| 357 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 358 | Cfg *Func; |
| 359 | GlobalContext *Ctx; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 360 | bool HasComputedFrame = false; |
| 361 | bool CallsReturnsTwice = false; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 362 | /// StackAdjustment keeps track of the current stack offset from its |
| 363 | /// natural location, as arguments are pushed for a function call. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 364 | int32_t StackAdjustment = 0; |
| 365 | SizeT NextLabelNumber = 0; |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 366 | SizeT NextJumpTableNumber = 0; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 367 | LoweringContext Context; |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 368 | |
Jim Stichnoth | c450879 | 2015-03-01 23:12:55 -0800 | [diff] [blame] | 369 | // Runtime helper function names |
| 370 | const static constexpr char *H_bitcast_16xi1_i16 = "__Sz_bitcast_16xi1_i16"; |
| 371 | const static constexpr char *H_bitcast_8xi1_i8 = "__Sz_bitcast_8xi1_i8"; |
| 372 | const static constexpr char *H_bitcast_i16_16xi1 = "__Sz_bitcast_i16_16xi1"; |
| 373 | const static constexpr char *H_bitcast_i8_8xi1 = "__Sz_bitcast_i8_8xi1"; |
| 374 | const static constexpr char *H_call_ctpop_i32 = "__popcountsi2"; |
| 375 | const static constexpr char *H_call_ctpop_i64 = "__popcountdi2"; |
| 376 | const static constexpr char *H_call_longjmp = "longjmp"; |
| 377 | const static constexpr char *H_call_memcpy = "memcpy"; |
| 378 | const static constexpr char *H_call_memmove = "memmove"; |
| 379 | const static constexpr char *H_call_memset = "memset"; |
| 380 | const static constexpr char *H_call_read_tp = "__nacl_read_tp"; |
| 381 | const static constexpr char *H_call_setjmp = "setjmp"; |
| 382 | const static constexpr char *H_fptosi_f32_i64 = "__Sz_fptosi_f32_i64"; |
| 383 | const static constexpr char *H_fptosi_f64_i64 = "__Sz_fptosi_f64_i64"; |
| 384 | const static constexpr char *H_fptoui_4xi32_f32 = "__Sz_fptoui_4xi32_f32"; |
| 385 | const static constexpr char *H_fptoui_f32_i32 = "__Sz_fptoui_f32_i32"; |
| 386 | const static constexpr char *H_fptoui_f32_i64 = "__Sz_fptoui_f32_i64"; |
| 387 | const static constexpr char *H_fptoui_f64_i32 = "__Sz_fptoui_f64_i32"; |
| 388 | const static constexpr char *H_fptoui_f64_i64 = "__Sz_fptoui_f64_i64"; |
| 389 | const static constexpr char *H_frem_f32 = "fmodf"; |
| 390 | const static constexpr char *H_frem_f64 = "fmod"; |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 391 | const static constexpr char *H_sdiv_i32 = "__divsi3"; |
Jim Stichnoth | c450879 | 2015-03-01 23:12:55 -0800 | [diff] [blame] | 392 | const static constexpr char *H_sdiv_i64 = "__divdi3"; |
| 393 | const static constexpr char *H_sitofp_i64_f32 = "__Sz_sitofp_i64_f32"; |
| 394 | const static constexpr char *H_sitofp_i64_f64 = "__Sz_sitofp_i64_f64"; |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 395 | const static constexpr char *H_srem_i32 = "__modsi3"; |
Jim Stichnoth | c450879 | 2015-03-01 23:12:55 -0800 | [diff] [blame] | 396 | const static constexpr char *H_srem_i64 = "__moddi3"; |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 397 | const static constexpr char *H_udiv_i32 = "__udivsi3"; |
Jim Stichnoth | c450879 | 2015-03-01 23:12:55 -0800 | [diff] [blame] | 398 | const static constexpr char *H_udiv_i64 = "__udivdi3"; |
| 399 | const static constexpr char *H_uitofp_4xi32_4xf32 = "__Sz_uitofp_4xi32_4xf32"; |
| 400 | const static constexpr char *H_uitofp_i32_f32 = "__Sz_uitofp_i32_f32"; |
| 401 | const static constexpr char *H_uitofp_i32_f64 = "__Sz_uitofp_i32_f64"; |
| 402 | const static constexpr char *H_uitofp_i64_f32 = "__Sz_uitofp_i64_f32"; |
| 403 | const static constexpr char *H_uitofp_i64_f64 = "__Sz_uitofp_i64_f64"; |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 404 | const static constexpr char *H_urem_i32 = "__umodsi3"; |
Jim Stichnoth | c450879 | 2015-03-01 23:12:55 -0800 | [diff] [blame] | 405 | const static constexpr char *H_urem_i64 = "__umoddi3"; |
| 406 | |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 407 | private: |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 408 | int32_t SnapshotStackAdjustment = 0; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 409 | }; |
| 410 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 411 | /// TargetDataLowering is used for "lowering" data including initializers |
| 412 | /// for global variables, and the internal constant pools. It is separated |
| 413 | /// out from TargetLowering because it does not require a Cfg. |
Jan Voung | 72984d8 | 2015-01-29 14:42:38 -0800 | [diff] [blame] | 414 | class TargetDataLowering { |
| 415 | TargetDataLowering() = delete; |
| 416 | TargetDataLowering(const TargetDataLowering &) = delete; |
| 417 | TargetDataLowering &operator=(const TargetDataLowering &) = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 418 | |
Jim Stichnoth | de4ca71 | 2014-06-29 08:13:48 -0700 | [diff] [blame] | 419 | public: |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 420 | static std::unique_ptr<TargetDataLowering> createLowering(GlobalContext *Ctx); |
Jan Voung | 72984d8 | 2015-01-29 14:42:38 -0800 | [diff] [blame] | 421 | virtual ~TargetDataLowering(); |
Jan Voung | 839c4ce | 2014-07-28 15:19:43 -0700 | [diff] [blame] | 422 | |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 423 | virtual void lowerGlobals(const VariableDeclarationList &Vars, |
| 424 | const IceString &SectionSuffix) = 0; |
John Porto | 0f86d03 | 2015-06-15 07:44:27 -0700 | [diff] [blame] | 425 | virtual void lowerConstants() = 0; |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 426 | virtual void lowerJumpTables() = 0; |
Jim Stichnoth | de4ca71 | 2014-06-29 08:13:48 -0700 | [diff] [blame] | 427 | |
| 428 | protected: |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 429 | void emitGlobal(const VariableDeclaration &Var, |
| 430 | const IceString &SectionSuffix); |
Jan Voung | 58eea4d | 2015-06-15 15:11:56 -0700 | [diff] [blame] | 431 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 432 | /// For now, we assume .long is the right directive for emitting 4 byte |
| 433 | /// emit global relocations. However, LLVM MIPS usually uses .4byte instead. |
| 434 | /// Perhaps there is some difference when the location is unaligned. |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 435 | static const char *getEmit32Directive() { return ".long"; } |
Jan Voung | 58eea4d | 2015-06-15 15:11:56 -0700 | [diff] [blame] | 436 | |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 437 | explicit TargetDataLowering(GlobalContext *Ctx) : Ctx(Ctx) {} |
Jim Stichnoth | de4ca71 | 2014-06-29 08:13:48 -0700 | [diff] [blame] | 438 | GlobalContext *Ctx; |
Jim Stichnoth | de4ca71 | 2014-06-29 08:13:48 -0700 | [diff] [blame] | 439 | }; |
| 440 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 441 | /// TargetHeaderLowering is used to "lower" the header of an output file. |
| 442 | /// It writes out the target-specific header attributes. E.g., for ARM |
| 443 | /// this writes out the build attributes (float ABI, etc.). |
Jan Voung | fb79284 | 2015-06-11 15:27:50 -0700 | [diff] [blame] | 444 | class TargetHeaderLowering { |
| 445 | TargetHeaderLowering() = delete; |
| 446 | TargetHeaderLowering(const TargetHeaderLowering &) = delete; |
| 447 | TargetHeaderLowering &operator=(const TargetHeaderLowering &) = delete; |
| 448 | |
| 449 | public: |
| 450 | static std::unique_ptr<TargetHeaderLowering> |
| 451 | createLowering(GlobalContext *Ctx); |
| 452 | virtual ~TargetHeaderLowering(); |
| 453 | |
| 454 | virtual void lower() {} |
| 455 | |
| 456 | protected: |
| 457 | explicit TargetHeaderLowering(GlobalContext *Ctx) : Ctx(Ctx) {} |
| 458 | GlobalContext *Ctx; |
| 459 | }; |
| 460 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 461 | } // end of namespace Ice |
| 462 | |
| 463 | #endif // SUBZERO_SRC_ICETARGETLOWERING_H |