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 |
Jim Stichnoth | 92a6e5b | 2015-12-02 16:52:44 -0800 | [diff] [blame] | 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. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 20 | /// |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 21 | //===----------------------------------------------------------------------===// |
| 22 | |
| 23 | #ifndef SUBZERO_SRC_ICETARGETLOWERING_H |
| 24 | #define SUBZERO_SRC_ICETARGETLOWERING_H |
| 25 | |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 26 | #include "IceCfgNode.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 27 | #include "IceDefs.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 28 | #include "IceInst.h" // for the names of the Inst subtypes |
Jan Voung | 76bb0be | 2015-05-14 09:26:19 -0700 | [diff] [blame] | 29 | #include "IceOperand.h" |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 30 | #include "IceTypes.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 31 | |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 32 | #include <utility> |
| 33 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 34 | namespace Ice { |
| 35 | |
Karl Schimpf | c5abdc1 | 2015-10-09 13:29:13 -0700 | [diff] [blame] | 36 | // UnimplementedError is defined as a macro so that we can get actual line |
| 37 | // numbers. |
| 38 | #define UnimplementedError(Flags) \ |
| 39 | do { \ |
| 40 | if (!static_cast<const ClFlags &>(Flags).getSkipUnimplemented()) { \ |
| 41 | /* Use llvm_unreachable instead of report_fatal_error, which gives \ |
| 42 | better stack traces. */ \ |
| 43 | llvm_unreachable("Not yet implemented"); \ |
| 44 | abort(); \ |
| 45 | } \ |
| 46 | } while (0) |
| 47 | |
Jim Stichnoth | 91c773e | 2016-01-19 09:52:22 -0800 | [diff] [blame] | 48 | // UnimplementedLoweringError is similar in style to UnimplementedError. Given |
| 49 | // a TargetLowering object pointer and an Inst pointer, it adds appropriate |
| 50 | // FakeDef and FakeUse instructions to try maintain liveness consistency. |
| 51 | #define UnimplementedLoweringError(Target, Instr) \ |
| 52 | do { \ |
| 53 | if ((Target)->Ctx->getFlags().getSkipUnimplemented()) { \ |
| 54 | (Target)->addFakeDefUses(Instr); \ |
| 55 | } else { \ |
| 56 | /* Use llvm_unreachable instead of report_fatal_error, which gives \ |
| 57 | better stack traces. */ \ |
| 58 | llvm_unreachable("Not yet implemented"); \ |
| 59 | abort(); \ |
| 60 | } \ |
| 61 | } while (0) |
| 62 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 63 | /// LoweringContext makes it easy to iterate through non-deleted instructions in |
| 64 | /// a node, and insert new (lowered) instructions at the current point. Along |
| 65 | /// with the instruction list container and associated iterators, it holds the |
| 66 | /// current node, which is needed when inserting new instructions in order to |
| 67 | /// track whether variables are used as single-block or multi-block. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 68 | class LoweringContext { |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 69 | LoweringContext(const LoweringContext &) = delete; |
| 70 | LoweringContext &operator=(const LoweringContext &) = delete; |
| 71 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 72 | public: |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 73 | LoweringContext() = default; |
| 74 | ~LoweringContext() = default; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 75 | void init(CfgNode *Node); |
| 76 | Inst *getNextInst() const { |
| 77 | if (Next == End) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 78 | return nullptr; |
Jim Stichnoth | 607e9f0 | 2014-11-06 13:32:05 -0800 | [diff] [blame] | 79 | return Next; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 80 | } |
Jan Voung | c820ddf | 2014-07-29 14:38:51 -0700 | [diff] [blame] | 81 | Inst *getNextInst(InstList::iterator &Iter) const { |
Jan Voung | e6e497d | 2014-07-30 10:06:03 -0700 | [diff] [blame] | 82 | advanceForward(Iter); |
Jan Voung | c820ddf | 2014-07-29 14:38:51 -0700 | [diff] [blame] | 83 | if (Iter == End) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 84 | return nullptr; |
Jim Stichnoth | 607e9f0 | 2014-11-06 13:32:05 -0800 | [diff] [blame] | 85 | return Iter; |
Jan Voung | c820ddf | 2014-07-29 14:38:51 -0700 | [diff] [blame] | 86 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 87 | CfgNode *getNode() const { return Node; } |
| 88 | bool atEnd() const { return Cur == End; } |
| 89 | InstList::iterator getCur() const { return Cur; } |
Jim Stichnoth | 5d2fa0c | 2014-12-01 09:30:55 -0800 | [diff] [blame] | 90 | InstList::iterator getNext() const { return Next; } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 91 | InstList::iterator getEnd() const { return End; } |
| 92 | void insert(Inst *Inst); |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 93 | template <typename Inst, typename... Args> Inst *insert(Args &&... A) { |
| 94 | auto *New = Inst::create(Node->getCfg(), std::forward<Args>(A)...); |
| 95 | insert(New); |
| 96 | return New; |
| 97 | } |
Jan Voung | e6e497d | 2014-07-30 10:06:03 -0700 | [diff] [blame] | 98 | Inst *getLastInserted() const; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 99 | void advanceCur() { Cur = Next; } |
Jan Voung | e6e497d | 2014-07-30 10:06:03 -0700 | [diff] [blame] | 100 | void advanceNext() { advanceForward(Next); } |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 101 | void setCur(InstList::iterator C) { Cur = C; } |
| 102 | void setNext(InstList::iterator N) { Next = N; } |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 103 | void rewind(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 104 | void setInsertPoint(const InstList::iterator &Position) { Next = Position; } |
Jim Stichnoth | 318f4cd | 2015-10-01 21:02:37 -0700 | [diff] [blame] | 105 | void availabilityReset(); |
| 106 | void availabilityUpdate(); |
| 107 | Variable *availabilityGet(Operand *Src) const; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 108 | |
| 109 | private: |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 110 | /// Node is the argument to Inst::updateVars(). |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 111 | CfgNode *Node = nullptr; |
| 112 | Inst *LastInserted = nullptr; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 113 | /// Cur points to the current instruction being considered. It is guaranteed |
| 114 | /// to point to a non-deleted instruction, or to be End. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 115 | InstList::iterator Cur; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 116 | /// Next doubles as a pointer to the next valid instruction (if any), and the |
| 117 | /// new-instruction insertion point. It is also updated for the caller in case |
| 118 | /// the lowering consumes more than one high-level instruction. It is |
| 119 | /// guaranteed to point to a non-deleted instruction after Cur, or to be End. |
| 120 | // TODO: Consider separating the notion of "next valid instruction" and "new |
| 121 | // instruction insertion point", to avoid confusion when previously-deleted |
| 122 | // instructions come between the two points. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 123 | InstList::iterator Next; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 124 | /// 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] | 125 | InstList::iterator Begin; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 126 | /// 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] | 127 | InstList::iterator End; |
Jim Stichnoth | 318f4cd | 2015-10-01 21:02:37 -0700 | [diff] [blame] | 128 | /// LastDest and LastSrc capture the parameters of the last "Dest=Src" simple |
| 129 | /// assignment inserted (provided Src is a variable). This is used for simple |
| 130 | /// availability analysis. |
| 131 | Variable *LastDest = nullptr; |
| 132 | Variable *LastSrc = nullptr; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 133 | |
Jan Voung | c820ddf | 2014-07-29 14:38:51 -0700 | [diff] [blame] | 134 | void skipDeleted(InstList::iterator &I) const; |
Jan Voung | e6e497d | 2014-07-30 10:06:03 -0700 | [diff] [blame] | 135 | void advanceForward(InstList::iterator &I) const; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 138 | /// A helper class to advance the LoweringContext at each loop iteration. |
| 139 | class PostIncrLoweringContext { |
| 140 | PostIncrLoweringContext() = delete; |
| 141 | PostIncrLoweringContext(const PostIncrLoweringContext &) = delete; |
| 142 | PostIncrLoweringContext &operator=(const PostIncrLoweringContext &) = delete; |
| 143 | |
| 144 | public: |
| 145 | explicit PostIncrLoweringContext(LoweringContext &Context) |
| 146 | : Context(Context) {} |
| 147 | ~PostIncrLoweringContext() { |
| 148 | Context.advanceCur(); |
| 149 | Context.advanceNext(); |
| 150 | } |
| 151 | |
| 152 | private: |
| 153 | LoweringContext &Context; |
| 154 | }; |
| 155 | |
John Porto | 53611e2 | 2015-12-30 07:30:10 -0800 | [diff] [blame] | 156 | /// TargetLowering is the base class for all backends in Subzero. In addition to |
| 157 | /// implementing the abstract methods in this class, each concrete target must |
| 158 | /// also implement a named constructor in its own namespace. For instance, for |
| 159 | /// X8632 we have: |
| 160 | /// |
| 161 | /// namespace X8632 { |
| 162 | /// void createTargetLowering(Cfg *Func); |
| 163 | /// } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 164 | class TargetLowering { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 165 | TargetLowering() = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 166 | TargetLowering(const TargetLowering &) = delete; |
| 167 | TargetLowering &operator=(const TargetLowering &) = delete; |
| 168 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 169 | public: |
Karl Schimpf | 5403f5d | 2016-01-15 11:07:46 -0800 | [diff] [blame] | 170 | static void staticInit(GlobalContext *Ctx); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 171 | // Each target must define a public static method: |
Karl Schimpf | 5403f5d | 2016-01-15 11:07:46 -0800 | [diff] [blame] | 172 | // static void staticInit(GlobalContext *Ctx); |
John Porto | 53611e2 | 2015-12-30 07:30:10 -0800 | [diff] [blame] | 173 | |
| 174 | static std::unique_ptr<TargetLowering> createLowering(TargetArch Target, |
| 175 | Cfg *Func); |
| 176 | |
| 177 | virtual std::unique_ptr<Assembler> createAssembler() const = 0; |
| 178 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 179 | void translate() { |
Jan Voung | 1f47ad0 | 2015-03-20 15:01:26 -0700 | [diff] [blame] | 180 | switch (Ctx->getFlags().getOptLevel()) { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 181 | case Opt_m1: |
| 182 | translateOm1(); |
| 183 | break; |
| 184 | case Opt_0: |
| 185 | translateO0(); |
| 186 | break; |
| 187 | case Opt_1: |
| 188 | translateO1(); |
| 189 | break; |
| 190 | case Opt_2: |
| 191 | translateO2(); |
| 192 | break; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | virtual void translateOm1() { |
| 196 | Func->setError("Target doesn't specify Om1 lowering steps."); |
| 197 | } |
| 198 | virtual void translateO0() { |
| 199 | Func->setError("Target doesn't specify O0 lowering steps."); |
| 200 | } |
| 201 | virtual void translateO1() { |
| 202 | Func->setError("Target doesn't specify O1 lowering steps."); |
| 203 | } |
| 204 | virtual void translateO2() { |
| 205 | Func->setError("Target doesn't specify O2 lowering steps."); |
| 206 | } |
| 207 | |
John Porto | 5e0a8a7 | 2015-11-20 13:50:36 -0800 | [diff] [blame] | 208 | /// Generates calls to intrinsics for operations the Target can't handle. |
| 209 | void genTargetHelperCalls(); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 210 | /// Tries to do address mode optimization on a single instruction. |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 211 | void doAddressOpt(); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 212 | /// Randomly insert NOPs. |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 213 | void doNopInsertion(RandomNumberGenerator &RNG); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 214 | /// Lowers a single non-Phi instruction. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 215 | void lower(); |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 216 | /// Inserts and lowers a single high-level instruction at a specific insertion |
| 217 | /// point. |
| 218 | void lowerInst(CfgNode *Node, InstList::iterator Next, InstHighLevel *Instr); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 219 | /// Does preliminary lowering of the set of Phi instructions in the current |
| 220 | /// node. The main intention is to do what's needed to keep the unlowered Phi |
| 221 | /// instructions consistent with the lowered non-Phi instructions, e.g. to |
| 222 | /// lower 64-bit operands on a 32-bit target. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 223 | virtual void prelowerPhis() {} |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 224 | /// Tries to do branch optimization on a single instruction. Returns true if |
| 225 | /// some optimization was done. |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 226 | virtual bool doBranchOpt(Inst * /*I*/, const CfgNode * /*NextNode*/) { |
| 227 | return false; |
| 228 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 229 | |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 230 | virtual SizeT getNumRegisters() const = 0; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 231 | /// Returns a variable pre-colored to the specified physical register. This is |
| 232 | /// generally used to get very direct access to the register such as in the |
| 233 | /// prolog or epilog or for marking scratch registers as killed by a call. If |
| 234 | /// a Type is not provided, a target-specific default type is used. |
Jim Stichnoth | 98712a3 | 2014-10-24 10:59:02 -0700 | [diff] [blame] | 235 | virtual Variable *getPhysicalRegister(SizeT RegNum, |
| 236 | Type Ty = IceType_void) = 0; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 237 | /// Returns a printable name for the register. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 238 | virtual IceString getRegName(SizeT RegNum, Type Ty) const = 0; |
| 239 | |
| 240 | virtual bool hasFramePointer() const { return false; } |
David Sehr | e39d0ca | 2015-11-06 11:25:41 -0800 | [diff] [blame] | 241 | virtual void setHasFramePointer() = 0; |
Jim Stichnoth | e741871 | 2015-10-09 06:54:02 -0700 | [diff] [blame] | 242 | virtual SizeT getStackReg() const = 0; |
David Sehr | 2f3b8ec | 2015-11-16 16:51:39 -0800 | [diff] [blame] | 243 | virtual SizeT getFrameReg() const = 0; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 244 | virtual SizeT getFrameOrStackReg() const = 0; |
Matt Wala | d4799f4 | 2014-08-14 14:24:12 -0700 | [diff] [blame] | 245 | virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0; |
David Sehr | e39d0ca | 2015-11-06 11:25:41 -0800 | [diff] [blame] | 246 | virtual uint32_t getStackAlignment() const = 0; |
David Sehr | 2f3b8ec | 2015-11-16 16:51:39 -0800 | [diff] [blame] | 247 | virtual void reserveFixedAllocaArea(size_t Size, size_t Align) = 0; |
| 248 | virtual int32_t getFrameFixedAllocaOffset() const = 0; |
John Porto | 614140e | 2015-11-23 11:43:13 -0800 | [diff] [blame] | 249 | virtual uint32_t maxOutArgsSizeBytes() const { return 0; } |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 250 | |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 251 | /// Return whether a 64-bit Variable should be split into a Variable64On32. |
| 252 | virtual bool shouldSplitToVariable64On32(Type Ty) const = 0; |
| 253 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 254 | bool hasComputedFrame() const { return HasComputedFrame; } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 255 | /// Returns true if this function calls a function that has the "returns |
| 256 | /// twice" attribute. |
Jan Voung | 44d53e1 | 2014-09-11 19:18:03 -0700 | [diff] [blame] | 257 | bool callsReturnsTwice() const { return CallsReturnsTwice; } |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 258 | void setCallsReturnsTwice(bool RetTwice) { CallsReturnsTwice = RetTwice; } |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 259 | SizeT makeNextLabelNumber() { return NextLabelNumber++; } |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 260 | SizeT makeNextJumpTableNumber() { return NextJumpTableNumber++; } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 261 | LoweringContext &getContext() { return Context; } |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 262 | Cfg *getFunc() const { return Func; } |
| 263 | GlobalContext *getGlobalContext() const { return Ctx; } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 264 | |
| 265 | enum RegSet { |
| 266 | RegSet_None = 0, |
| 267 | RegSet_CallerSave = 1 << 0, |
| 268 | RegSet_CalleeSave = 1 << 1, |
| 269 | RegSet_StackPointer = 1 << 2, |
| 270 | RegSet_FramePointer = 1 << 3, |
| 271 | RegSet_All = ~RegSet_None |
| 272 | }; |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 273 | using RegSetMask = uint32_t; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 274 | |
| 275 | virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include, |
| 276 | RegSetMask Exclude) const = 0; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 277 | virtual const llvm::SmallBitVector & |
| 278 | getRegistersForVariable(const Variable *Var) const = 0; |
John Porto | bb0a5fe | 2015-09-04 11:23:41 -0700 | [diff] [blame] | 279 | virtual const llvm::SmallBitVector &getAliasesForRegister(SizeT) const = 0; |
| 280 | |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 281 | void regAlloc(RegAllocKind Kind); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 282 | |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 283 | virtual void |
| 284 | makeRandomRegisterPermutation(llvm::SmallVectorImpl<int32_t> &Permutation, |
| 285 | const llvm::SmallBitVector &ExcludeRegisters, |
| 286 | uint64_t Salt) const = 0; |
Jim Stichnoth | e6d2478 | 2014-12-19 05:42:24 -0800 | [diff] [blame] | 287 | |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 288 | /// Get the minimum number of clusters required for a jump table to be |
| 289 | /// considered. |
| 290 | virtual SizeT getMinJumpTableSize() const = 0; |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 291 | virtual void emitJumpTable(const Cfg *Func, |
| 292 | const InstJumpTable *JumpTable) const = 0; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 293 | |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 294 | virtual void emitVariable(const Variable *Var) const = 0; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 295 | |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 296 | void emitWithoutPrefix(const ConstantRelocatable *CR, |
| 297 | const char *Suffix = "") const; |
Jan Voung | 76bb0be | 2015-05-14 09:26:19 -0700 | [diff] [blame] | 298 | |
Jan Voung | 76bb0be | 2015-05-14 09:26:19 -0700 | [diff] [blame] | 299 | virtual void emit(const ConstantInteger32 *C) const = 0; |
| 300 | virtual void emit(const ConstantInteger64 *C) const = 0; |
| 301 | virtual void emit(const ConstantFloat *C) const = 0; |
| 302 | virtual void emit(const ConstantDouble *C) const = 0; |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 303 | virtual void emit(const ConstantUndef *C) const = 0; |
| 304 | virtual void emit(const ConstantRelocatable *CR) const = 0; |
Jan Voung | 76bb0be | 2015-05-14 09:26:19 -0700 | [diff] [blame] | 305 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 306 | /// Performs target-specific argument lowering. |
Matt Wala | 45a0623 | 2014-07-09 16:33:22 -0700 | [diff] [blame] | 307 | virtual void lowerArguments() = 0; |
| 308 | |
Jim Stichnoth | a59ae6f | 2015-05-17 10:11:41 -0700 | [diff] [blame] | 309 | virtual void initNodeForLowering(CfgNode *) {} |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 310 | virtual void addProlog(CfgNode *Node) = 0; |
| 311 | virtual void addEpilog(CfgNode *Node) = 0; |
| 312 | |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 313 | virtual ~TargetLowering() = default; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 314 | |
John Porto | 3bf335f | 2016-01-15 11:17:55 -0800 | [diff] [blame] | 315 | private: |
| 316 | // This control variable is used by AutoBundle (RAII-style bundle |
| 317 | // locking/unlocking) to prevent nested bundles. |
| 318 | bool AutoBundling = false; |
| 319 | |
| 320 | // _bundle_lock(), and _bundle_unlock(), were made private to force subtargets |
| 321 | // to use the AutoBundle helper. |
| 322 | void |
| 323 | _bundle_lock(InstBundleLock::Option BundleOption = InstBundleLock::Opt_None) { |
| 324 | Context.insert<InstBundleLock>(BundleOption); |
| 325 | } |
| 326 | void _bundle_unlock() { Context.insert<InstBundleUnlock>(); } |
| 327 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 328 | protected: |
John Porto | 3bf335f | 2016-01-15 11:17:55 -0800 | [diff] [blame] | 329 | /// AutoBundle provides RIAA-style bundling. Sub-targets are expected to use |
| 330 | /// it when emitting NaCl Bundles to ensure proper bundle_unlocking, and |
| 331 | /// prevent nested bundles. |
| 332 | /// |
| 333 | /// AutoBundle objects will emit a _bundle_lock during construction (but only |
| 334 | /// if sandboxed code generation was requested), and a bundle_unlock() during |
| 335 | /// destruction. By carefully scoping objects of this type, Subtargets can |
| 336 | /// ensure proper bundle emission. |
| 337 | class AutoBundle { |
| 338 | AutoBundle() = delete; |
| 339 | AutoBundle(const AutoBundle &) = delete; |
| 340 | AutoBundle &operator=(const AutoBundle &) = delete; |
| 341 | |
| 342 | public: |
| 343 | explicit AutoBundle(TargetLowering *Target, InstBundleLock::Option Option = |
| 344 | InstBundleLock::Opt_None); |
| 345 | ~AutoBundle(); |
| 346 | |
| 347 | private: |
| 348 | TargetLowering *const Target; |
| 349 | const bool NeedSandboxing; |
| 350 | }; |
| 351 | |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 352 | explicit TargetLowering(Cfg *Func); |
Karl Schimpf | 5403f5d | 2016-01-15 11:07:46 -0800 | [diff] [blame] | 353 | // Applies command line filters to TypeToRegisterSet array. |
| 354 | static void |
| 355 | filterTypeToRegisterSet(GlobalContext *Ctx, int32_t NumRegs, |
| 356 | llvm::SmallBitVector TypeToRegisterSet[], |
| 357 | size_t TypeToRegisterSetSize, |
Jim Stichnoth | 2544d4d | 2016-01-22 13:07:46 -0800 | [diff] [blame] | 358 | std::function<IceString(int32_t)> getRegName, |
| 359 | std::function<IceString(RegClass)> getRegClassName); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 360 | virtual void lowerAlloca(const InstAlloca *Inst) = 0; |
| 361 | virtual void lowerArithmetic(const InstArithmetic *Inst) = 0; |
| 362 | virtual void lowerAssign(const InstAssign *Inst) = 0; |
| 363 | virtual void lowerBr(const InstBr *Inst) = 0; |
| 364 | virtual void lowerCall(const InstCall *Inst) = 0; |
| 365 | virtual void lowerCast(const InstCast *Inst) = 0; |
| 366 | virtual void lowerFcmp(const InstFcmp *Inst) = 0; |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 367 | virtual void lowerExtractElement(const InstExtractElement *Inst) = 0; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 368 | virtual void lowerIcmp(const InstIcmp *Inst) = 0; |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 369 | virtual void lowerInsertElement(const InstInsertElement *Inst) = 0; |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 370 | virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 371 | virtual void lowerLoad(const InstLoad *Inst) = 0; |
| 372 | virtual void lowerPhi(const InstPhi *Inst) = 0; |
| 373 | virtual void lowerRet(const InstRet *Inst) = 0; |
| 374 | virtual void lowerSelect(const InstSelect *Inst) = 0; |
| 375 | virtual void lowerStore(const InstStore *Inst) = 0; |
| 376 | virtual void lowerSwitch(const InstSwitch *Inst) = 0; |
| 377 | virtual void lowerUnreachable(const InstUnreachable *Inst) = 0; |
Jim Stichnoth | e4f65d8 | 2015-06-17 22:16:02 -0700 | [diff] [blame] | 378 | virtual void lowerOther(const Inst *Instr); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 379 | |
John Porto | 5e0a8a7 | 2015-11-20 13:50:36 -0800 | [diff] [blame] | 380 | virtual void genTargetHelperCallFor(Inst *Instr) = 0; |
John Porto | f419854 | 2015-11-20 14:17:23 -0800 | [diff] [blame] | 381 | virtual uint32_t getCallStackArgumentsSizeBytes(const InstCall *Instr) = 0; |
John Porto | 5e0a8a7 | 2015-11-20 13:50:36 -0800 | [diff] [blame] | 382 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 383 | virtual void doAddressOptLoad() {} |
| 384 | virtual void doAddressOptStore() {} |
Jim Stichnoth | ad2989b | 2015-09-15 10:21:42 -0700 | [diff] [blame] | 385 | virtual void doMockBoundsCheck(Operand *) {} |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 386 | virtual void randomlyInsertNop(float Probability, |
| 387 | RandomNumberGenerator &RNG) = 0; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 388 | /// This gives the target an opportunity to post-process the lowered expansion |
| 389 | /// before returning. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 390 | virtual void postLower() {} |
| 391 | |
Jim Stichnoth | 91c773e | 2016-01-19 09:52:22 -0800 | [diff] [blame] | 392 | /// When the SkipUnimplemented flag is set, addFakeDefUses() gets invoked by |
| 393 | /// the UnimplementedLoweringError macro to insert fake uses of all the |
| 394 | /// instruction variables and a fake def of the instruction dest, in order to |
| 395 | /// preserve integrity of liveness analysis. |
| 396 | void addFakeDefUses(const Inst *Instr); |
| 397 | |
Jim Stichnoth | 230d410 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 398 | /// Find (non-SSA) instructions where the Dest variable appears in some source |
| 399 | /// operand, and set the IsDestRedefined flag. This keeps liveness analysis |
| 400 | /// consistent. |
| 401 | void markRedefinitions(); |
Jan Voung | b3401d2 | 2015-05-18 09:38:21 -0700 | [diff] [blame] | 402 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 403 | /// Make a pass over the Cfg to determine which variables need stack slots and |
| 404 | /// place them in a sorted list (SortedSpilledVariables). Among those, vars, |
| 405 | /// classify the spill variables as local to the basic block vs global |
| 406 | /// (multi-block) in order to compute the parameters GlobalsSize and |
| 407 | /// SpillAreaSizeBytes (represents locals or general vars if the coalescing of |
| 408 | /// locals is disallowed) along with alignments required for variables in each |
| 409 | /// area. We rely on accurate VMetadata in order to classify a variable as |
| 410 | /// global vs local (otherwise the variable is conservatively global). The |
| 411 | /// in-args should be initialized to 0. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 412 | /// |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 413 | /// This is only a pre-pass and the actual stack slot assignment is handled |
| 414 | /// separately. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 415 | /// |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 416 | /// There may be target-specific Variable types, which will be handled by |
| 417 | /// TargetVarHook. If the TargetVarHook returns true, then the variable is |
| 418 | /// skipped and not considered with the rest of the spilled variables. |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 419 | void getVarStackSlotParams(VarList &SortedSpilledVariables, |
| 420 | llvm::SmallBitVector &RegsUsed, |
| 421 | size_t *GlobalsSize, size_t *SpillAreaSizeBytes, |
| 422 | uint32_t *SpillAreaAlignmentBytes, |
| 423 | uint32_t *LocalsSlotsAlignmentBytes, |
| 424 | std::function<bool(Variable *)> TargetVarHook); |
| 425 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 426 | /// Calculate the amount of padding needed to align the local and global areas |
| 427 | /// to the required alignment. This assumes the globals/locals layout used by |
| 428 | /// getVarStackSlotParams and assignVarStackSlots. |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 429 | void alignStackSpillAreas(uint32_t SpillAreaStartOffset, |
| 430 | uint32_t SpillAreaAlignmentBytes, |
| 431 | size_t GlobalsSize, |
| 432 | uint32_t LocalsSlotsAlignmentBytes, |
| 433 | uint32_t *SpillAreaPaddingBytes, |
| 434 | uint32_t *LocalsSlotsPaddingBytes); |
| 435 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 436 | /// Make a pass through the SortedSpilledVariables and actually assign stack |
| 437 | /// slots. SpillAreaPaddingBytes takes into account stack alignment padding. |
| 438 | /// The SpillArea starts after that amount of padding. This matches the scheme |
| 439 | /// in getVarStackSlotParams, where there may be a separate multi-block global |
| 440 | /// var spill area and a local var spill area. |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 441 | void assignVarStackSlots(VarList &SortedSpilledVariables, |
| 442 | size_t SpillAreaPaddingBytes, |
| 443 | size_t SpillAreaSizeBytes, |
| 444 | size_t GlobalsAndSubsequentPaddingSize, |
| 445 | bool UsesFramePointer); |
| 446 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 447 | /// Sort the variables in Source based on required alignment. The variables |
| 448 | /// with the largest alignment need are placed in the front of the Dest list. |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 449 | void sortVarsByAlignment(VarList &Dest, const VarList &Source) const; |
| 450 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 451 | /// Make a call to an external helper function. |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 452 | InstCall *makeHelperCall(const IceString &Name, Variable *Dest, |
| 453 | SizeT MaxSrcs); |
| 454 | |
Jim Stichnoth | 230d410 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 455 | void _set_dest_redefined() { Context.getLastInserted()->setDestRedefined(); } |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 456 | |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 457 | bool shouldOptimizeMemIntrins(); |
| 458 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 459 | /// SandboxType enumerates all possible sandboxing strategies that |
| 460 | enum SandboxType { |
| 461 | ST_None, |
| 462 | ST_NaCl, |
| 463 | ST_Nonsfi, |
| 464 | }; |
| 465 | |
| 466 | static SandboxType determineSandboxTypeFromFlags(const ClFlags &Flags); |
| 467 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 468 | Cfg *Func; |
| 469 | GlobalContext *Ctx; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 470 | bool HasComputedFrame = false; |
| 471 | bool CallsReturnsTwice = false; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 472 | SizeT NextLabelNumber = 0; |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 473 | SizeT NextJumpTableNumber = 0; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 474 | LoweringContext Context; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 475 | const SandboxType SandboxingType = ST_None; |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 476 | |
Jim Stichnoth | c450879 | 2015-03-01 23:12:55 -0800 | [diff] [blame] | 477 | // Runtime helper function names |
| 478 | const static constexpr char *H_bitcast_16xi1_i16 = "__Sz_bitcast_16xi1_i16"; |
| 479 | const static constexpr char *H_bitcast_8xi1_i8 = "__Sz_bitcast_8xi1_i8"; |
| 480 | const static constexpr char *H_bitcast_i16_16xi1 = "__Sz_bitcast_i16_16xi1"; |
| 481 | const static constexpr char *H_bitcast_i8_8xi1 = "__Sz_bitcast_i8_8xi1"; |
| 482 | const static constexpr char *H_call_ctpop_i32 = "__popcountsi2"; |
| 483 | const static constexpr char *H_call_ctpop_i64 = "__popcountdi2"; |
| 484 | const static constexpr char *H_call_longjmp = "longjmp"; |
| 485 | const static constexpr char *H_call_memcpy = "memcpy"; |
| 486 | const static constexpr char *H_call_memmove = "memmove"; |
| 487 | const static constexpr char *H_call_memset = "memset"; |
| 488 | const static constexpr char *H_call_read_tp = "__nacl_read_tp"; |
| 489 | const static constexpr char *H_call_setjmp = "setjmp"; |
| 490 | const static constexpr char *H_fptosi_f32_i64 = "__Sz_fptosi_f32_i64"; |
| 491 | const static constexpr char *H_fptosi_f64_i64 = "__Sz_fptosi_f64_i64"; |
| 492 | const static constexpr char *H_fptoui_4xi32_f32 = "__Sz_fptoui_4xi32_f32"; |
| 493 | const static constexpr char *H_fptoui_f32_i32 = "__Sz_fptoui_f32_i32"; |
| 494 | const static constexpr char *H_fptoui_f32_i64 = "__Sz_fptoui_f32_i64"; |
| 495 | const static constexpr char *H_fptoui_f64_i32 = "__Sz_fptoui_f64_i32"; |
| 496 | const static constexpr char *H_fptoui_f64_i64 = "__Sz_fptoui_f64_i64"; |
| 497 | const static constexpr char *H_frem_f32 = "fmodf"; |
| 498 | const static constexpr char *H_frem_f64 = "fmod"; |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 499 | const static constexpr char *H_getIP_prefix = "__Sz_getIP_"; |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 500 | const static constexpr char *H_sdiv_i32 = "__divsi3"; |
Jim Stichnoth | c450879 | 2015-03-01 23:12:55 -0800 | [diff] [blame] | 501 | const static constexpr char *H_sdiv_i64 = "__divdi3"; |
| 502 | const static constexpr char *H_sitofp_i64_f32 = "__Sz_sitofp_i64_f32"; |
| 503 | const static constexpr char *H_sitofp_i64_f64 = "__Sz_sitofp_i64_f64"; |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 504 | const static constexpr char *H_srem_i32 = "__modsi3"; |
Jim Stichnoth | c450879 | 2015-03-01 23:12:55 -0800 | [diff] [blame] | 505 | const static constexpr char *H_srem_i64 = "__moddi3"; |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 506 | const static constexpr char *H_udiv_i32 = "__udivsi3"; |
Jim Stichnoth | c450879 | 2015-03-01 23:12:55 -0800 | [diff] [blame] | 507 | const static constexpr char *H_udiv_i64 = "__udivdi3"; |
| 508 | const static constexpr char *H_uitofp_4xi32_4xf32 = "__Sz_uitofp_4xi32_4xf32"; |
| 509 | const static constexpr char *H_uitofp_i32_f32 = "__Sz_uitofp_i32_f32"; |
| 510 | const static constexpr char *H_uitofp_i32_f64 = "__Sz_uitofp_i32_f64"; |
| 511 | const static constexpr char *H_uitofp_i64_f32 = "__Sz_uitofp_i64_f32"; |
| 512 | const static constexpr char *H_uitofp_i64_f64 = "__Sz_uitofp_i64_f64"; |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 513 | const static constexpr char *H_urem_i32 = "__umodsi3"; |
Jim Stichnoth | c450879 | 2015-03-01 23:12:55 -0800 | [diff] [blame] | 514 | const static constexpr char *H_urem_i64 = "__umoddi3"; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 515 | }; |
| 516 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 517 | /// TargetDataLowering is used for "lowering" data including initializers for |
| 518 | /// global variables, and the internal constant pools. It is separated out from |
| 519 | /// TargetLowering because it does not require a Cfg. |
Jan Voung | 72984d8 | 2015-01-29 14:42:38 -0800 | [diff] [blame] | 520 | class TargetDataLowering { |
| 521 | TargetDataLowering() = delete; |
| 522 | TargetDataLowering(const TargetDataLowering &) = delete; |
| 523 | TargetDataLowering &operator=(const TargetDataLowering &) = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 524 | |
Jim Stichnoth | de4ca71 | 2014-06-29 08:13:48 -0700 | [diff] [blame] | 525 | public: |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 526 | static std::unique_ptr<TargetDataLowering> createLowering(GlobalContext *Ctx); |
Jan Voung | 72984d8 | 2015-01-29 14:42:38 -0800 | [diff] [blame] | 527 | virtual ~TargetDataLowering(); |
Jan Voung | 839c4ce | 2014-07-28 15:19:43 -0700 | [diff] [blame] | 528 | |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 529 | virtual void lowerGlobals(const VariableDeclarationList &Vars, |
| 530 | const IceString &SectionSuffix) = 0; |
John Porto | 0f86d03 | 2015-06-15 07:44:27 -0700 | [diff] [blame] | 531 | virtual void lowerConstants() = 0; |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 532 | virtual void lowerJumpTables() = 0; |
Jim Stichnoth | de4ca71 | 2014-06-29 08:13:48 -0700 | [diff] [blame] | 533 | |
| 534 | protected: |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 535 | void emitGlobal(const VariableDeclaration &Var, |
| 536 | const IceString &SectionSuffix); |
Jan Voung | 58eea4d | 2015-06-15 15:11:56 -0700 | [diff] [blame] | 537 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 538 | /// For now, we assume .long is the right directive for emitting 4 byte emit |
| 539 | /// global relocations. However, LLVM MIPS usually uses .4byte instead. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 540 | /// Perhaps there is some difference when the location is unaligned. |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 541 | static const char *getEmit32Directive() { return ".long"; } |
Jan Voung | 58eea4d | 2015-06-15 15:11:56 -0700 | [diff] [blame] | 542 | |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 543 | explicit TargetDataLowering(GlobalContext *Ctx) : Ctx(Ctx) {} |
Jim Stichnoth | de4ca71 | 2014-06-29 08:13:48 -0700 | [diff] [blame] | 544 | GlobalContext *Ctx; |
Jim Stichnoth | de4ca71 | 2014-06-29 08:13:48 -0700 | [diff] [blame] | 545 | }; |
| 546 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 547 | /// TargetHeaderLowering is used to "lower" the header of an output file. It |
| 548 | /// writes out the target-specific header attributes. E.g., for ARM this writes |
| 549 | /// out the build attributes (float ABI, etc.). |
Jan Voung | fb79284 | 2015-06-11 15:27:50 -0700 | [diff] [blame] | 550 | class TargetHeaderLowering { |
| 551 | TargetHeaderLowering() = delete; |
| 552 | TargetHeaderLowering(const TargetHeaderLowering &) = delete; |
| 553 | TargetHeaderLowering &operator=(const TargetHeaderLowering &) = delete; |
| 554 | |
| 555 | public: |
| 556 | static std::unique_ptr<TargetHeaderLowering> |
| 557 | createLowering(GlobalContext *Ctx); |
| 558 | virtual ~TargetHeaderLowering(); |
| 559 | |
| 560 | virtual void lower() {} |
| 561 | |
| 562 | protected: |
| 563 | explicit TargetHeaderLowering(GlobalContext *Ctx) : Ctx(Ctx) {} |
| 564 | GlobalContext *Ctx; |
| 565 | }; |
| 566 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 567 | } // end of namespace Ice |
| 568 | |
| 569 | #endif // SUBZERO_SRC_ICETARGETLOWERING_H |