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 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file declares the TargetLowering and LoweringContext |
| 11 | // classes. TargetLowering is an abstract class used to drive the |
| 12 | // translation/lowering process. LoweringContext maintains a |
| 13 | // context for lowering each instruction, offering conveniences such |
| 14 | // as iterating over non-deleted instructions. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #ifndef SUBZERO_SRC_ICETARGETLOWERING_H |
| 19 | #define SUBZERO_SRC_ICETARGETLOWERING_H |
| 20 | |
| 21 | #include "IceDefs.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 22 | #include "IceInst.h" // for the names of the Inst subtypes |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 23 | #include "IceTypes.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 24 | |
| 25 | namespace Ice { |
| 26 | |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 27 | typedef uint8_t AsmCodeByte; |
| 28 | |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 29 | class Assembler; |
| 30 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 31 | // LoweringContext makes it easy to iterate through non-deleted |
| 32 | // instructions in a node, and insert new (lowered) instructions at |
| 33 | // the current point. Along with the instruction list container and |
| 34 | // associated iterators, it holds the current node, which is needed |
| 35 | // when inserting new instructions in order to track whether variables |
| 36 | // are used as single-block or multi-block. |
| 37 | class LoweringContext { |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 38 | LoweringContext(const LoweringContext &) = delete; |
| 39 | LoweringContext &operator=(const LoweringContext &) = delete; |
| 40 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 41 | public: |
Jim Stichnoth | 98712a3 | 2014-10-24 10:59:02 -0700 | [diff] [blame] | 42 | LoweringContext() : Node(NULL), LastInserted(NULL) {} |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 43 | ~LoweringContext() {} |
| 44 | void init(CfgNode *Node); |
| 45 | Inst *getNextInst() const { |
| 46 | if (Next == End) |
| 47 | return NULL; |
| 48 | return *Next; |
| 49 | } |
Jan Voung | c820ddf | 2014-07-29 14:38:51 -0700 | [diff] [blame] | 50 | Inst *getNextInst(InstList::iterator &Iter) const { |
Jan Voung | e6e497d | 2014-07-30 10:06:03 -0700 | [diff] [blame] | 51 | advanceForward(Iter); |
Jan Voung | c820ddf | 2014-07-29 14:38:51 -0700 | [diff] [blame] | 52 | if (Iter == End) |
| 53 | return NULL; |
| 54 | return *Iter; |
| 55 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 56 | CfgNode *getNode() const { return Node; } |
| 57 | bool atEnd() const { return Cur == End; } |
| 58 | InstList::iterator getCur() const { return Cur; } |
| 59 | InstList::iterator getEnd() const { return End; } |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 60 | // Adaptor to enable range-based for loops. |
| 61 | InstList::iterator begin() const { return getCur(); } |
| 62 | InstList::iterator end() const { return getEnd(); } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 63 | void insert(Inst *Inst); |
Jan Voung | e6e497d | 2014-07-30 10:06:03 -0700 | [diff] [blame] | 64 | Inst *getLastInserted() const; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 65 | void advanceCur() { Cur = Next; } |
Jan Voung | e6e497d | 2014-07-30 10:06:03 -0700 | [diff] [blame] | 66 | void advanceNext() { advanceForward(Next); } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 67 | void setInsertPoint(const InstList::iterator &Position) { Next = Position; } |
| 68 | |
| 69 | private: |
| 70 | // Node is the argument to Inst::updateVars(). |
| 71 | CfgNode *Node; |
Jim Stichnoth | 98712a3 | 2014-10-24 10:59:02 -0700 | [diff] [blame] | 72 | Inst *LastInserted; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 73 | // Cur points to the current instruction being considered. It is |
| 74 | // guaranteed to point to a non-deleted instruction, or to be End. |
| 75 | InstList::iterator Cur; |
| 76 | // Next doubles as a pointer to the next valid instruction (if any), |
| 77 | // and the new-instruction insertion point. It is also updated for |
| 78 | // the caller in case the lowering consumes more than one high-level |
| 79 | // instruction. It is guaranteed to point to a non-deleted |
| 80 | // instruction after Cur, or to be End. TODO: Consider separating |
| 81 | // the notion of "next valid instruction" and "new instruction |
| 82 | // insertion point", to avoid confusion when previously-deleted |
| 83 | // instructions come between the two points. |
| 84 | InstList::iterator Next; |
Jan Voung | e6e497d | 2014-07-30 10:06:03 -0700 | [diff] [blame] | 85 | // Begin is a copy of Insts.begin(), used if iterators are moved backward. |
| 86 | InstList::iterator Begin; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 87 | // End is a copy of Insts.end(), used if Next needs to be advanced. |
| 88 | InstList::iterator End; |
| 89 | |
Jan Voung | c820ddf | 2014-07-29 14:38:51 -0700 | [diff] [blame] | 90 | void skipDeleted(InstList::iterator &I) const; |
Jan Voung | e6e497d | 2014-07-30 10:06:03 -0700 | [diff] [blame] | 91 | void advanceForward(InstList::iterator &I) const; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | class TargetLowering { |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 95 | TargetLowering(const TargetLowering &) = delete; |
| 96 | TargetLowering &operator=(const TargetLowering &) = delete; |
| 97 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 98 | public: |
| 99 | static TargetLowering *createLowering(TargetArch Target, Cfg *Func); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 100 | static Assembler *createAssembler(TargetArch Target, Cfg *Func); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 101 | void translate() { |
| 102 | switch (Ctx->getOptLevel()) { |
| 103 | case Opt_m1: |
| 104 | translateOm1(); |
| 105 | break; |
| 106 | case Opt_0: |
| 107 | translateO0(); |
| 108 | break; |
| 109 | case Opt_1: |
| 110 | translateO1(); |
| 111 | break; |
| 112 | case Opt_2: |
| 113 | translateO2(); |
| 114 | break; |
| 115 | default: |
| 116 | Func->setError("Target doesn't specify lowering steps."); |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | virtual void translateOm1() { |
| 121 | Func->setError("Target doesn't specify Om1 lowering steps."); |
| 122 | } |
| 123 | virtual void translateO0() { |
| 124 | Func->setError("Target doesn't specify O0 lowering steps."); |
| 125 | } |
| 126 | virtual void translateO1() { |
| 127 | Func->setError("Target doesn't specify O1 lowering steps."); |
| 128 | } |
| 129 | virtual void translateO2() { |
| 130 | Func->setError("Target doesn't specify O2 lowering steps."); |
| 131 | } |
| 132 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 133 | // Tries to do address mode optimization on a single instruction. |
| 134 | void doAddressOpt(); |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 135 | // Randomly insert NOPs. |
| 136 | void doNopInsertion(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 137 | // Lowers a single instruction. |
| 138 | void lower(); |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 139 | // Tries to do branch optimization on a single instruction. Returns |
| 140 | // true if some optimization was done. |
| 141 | virtual bool doBranchOpt(Inst * /*I*/, const CfgNode * /*NextNode*/) { |
| 142 | return false; |
| 143 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 144 | |
| 145 | // Returns a variable pre-colored to the specified physical |
| 146 | // register. This is generally used to get very direct access to |
| 147 | // the register such as in the prolog or epilog or for marking |
Jim Stichnoth | 98712a3 | 2014-10-24 10:59:02 -0700 | [diff] [blame] | 148 | // scratch registers as killed by a call. If a Type is not |
| 149 | // provided, a target-specific default type is used. |
| 150 | virtual Variable *getPhysicalRegister(SizeT RegNum, |
| 151 | Type Ty = IceType_void) = 0; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 152 | // Returns a printable name for the register. |
| 153 | virtual IceString getRegName(SizeT RegNum, Type Ty) const = 0; |
| 154 | |
| 155 | virtual bool hasFramePointer() const { return false; } |
| 156 | virtual SizeT getFrameOrStackReg() const = 0; |
Matt Wala | d4799f4 | 2014-08-14 14:24:12 -0700 | [diff] [blame] | 157 | virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0; |
Jan Voung | b17f61d | 2014-08-28 16:00:53 -0700 | [diff] [blame] | 158 | virtual SizeT getBundleAlignLog2Bytes() const = 0; |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 159 | virtual llvm::ArrayRef<AsmCodeByte> getNonExecBundlePadding() const = 0; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 160 | bool hasComputedFrame() const { return HasComputedFrame; } |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 161 | bool shouldDoNopInsertion() const; |
Jan Voung | 44d53e1 | 2014-09-11 19:18:03 -0700 | [diff] [blame] | 162 | // Returns true if this function calls a function that has the |
| 163 | // "returns twice" attribute. |
| 164 | bool callsReturnsTwice() const { return CallsReturnsTwice; } |
| 165 | void setCallsReturnsTwice(bool RetTwice) { |
| 166 | CallsReturnsTwice = RetTwice; |
| 167 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 168 | int32_t getStackAdjustment() const { return StackAdjustment; } |
| 169 | void updateStackAdjustment(int32_t Offset) { StackAdjustment += Offset; } |
| 170 | void resetStackAdjustment() { StackAdjustment = 0; } |
| 171 | LoweringContext &getContext() { return Context; } |
| 172 | |
| 173 | enum RegSet { |
| 174 | RegSet_None = 0, |
| 175 | RegSet_CallerSave = 1 << 0, |
| 176 | RegSet_CalleeSave = 1 << 1, |
| 177 | RegSet_StackPointer = 1 << 2, |
| 178 | RegSet_FramePointer = 1 << 3, |
| 179 | RegSet_All = ~RegSet_None |
| 180 | }; |
| 181 | typedef uint32_t RegSetMask; |
| 182 | |
| 183 | virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include, |
| 184 | RegSetMask Exclude) const = 0; |
| 185 | virtual const llvm::SmallBitVector &getRegisterSetForType(Type Ty) const = 0; |
| 186 | void regAlloc(); |
| 187 | |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 188 | virtual void emitVariable(const Variable *Var) const = 0; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 189 | |
Matt Wala | 45a0623 | 2014-07-09 16:33:22 -0700 | [diff] [blame] | 190 | // Performs target-specific argument lowering. |
| 191 | virtual void lowerArguments() = 0; |
| 192 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 193 | virtual void addProlog(CfgNode *Node) = 0; |
| 194 | virtual void addEpilog(CfgNode *Node) = 0; |
| 195 | |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 196 | virtual void emitConstants() const = 0; |
| 197 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 198 | virtual ~TargetLowering() {} |
| 199 | |
| 200 | protected: |
| 201 | TargetLowering(Cfg *Func) |
| 202 | : Func(Func), Ctx(Func->getContext()), HasComputedFrame(false), |
Jan Voung | 44d53e1 | 2014-09-11 19:18:03 -0700 | [diff] [blame] | 203 | CallsReturnsTwice(false), StackAdjustment(0) {} |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 204 | virtual void lowerAlloca(const InstAlloca *Inst) = 0; |
| 205 | virtual void lowerArithmetic(const InstArithmetic *Inst) = 0; |
| 206 | virtual void lowerAssign(const InstAssign *Inst) = 0; |
| 207 | virtual void lowerBr(const InstBr *Inst) = 0; |
| 208 | virtual void lowerCall(const InstCall *Inst) = 0; |
| 209 | virtual void lowerCast(const InstCast *Inst) = 0; |
| 210 | virtual void lowerFcmp(const InstFcmp *Inst) = 0; |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 211 | virtual void lowerExtractElement(const InstExtractElement *Inst) = 0; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 212 | virtual void lowerIcmp(const InstIcmp *Inst) = 0; |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 213 | virtual void lowerInsertElement(const InstInsertElement *Inst) = 0; |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 214 | virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 215 | virtual void lowerLoad(const InstLoad *Inst) = 0; |
| 216 | virtual void lowerPhi(const InstPhi *Inst) = 0; |
| 217 | virtual void lowerRet(const InstRet *Inst) = 0; |
| 218 | virtual void lowerSelect(const InstSelect *Inst) = 0; |
| 219 | virtual void lowerStore(const InstStore *Inst) = 0; |
| 220 | virtual void lowerSwitch(const InstSwitch *Inst) = 0; |
| 221 | virtual void lowerUnreachable(const InstUnreachable *Inst) = 0; |
| 222 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 223 | virtual void doAddressOptLoad() {} |
| 224 | virtual void doAddressOptStore() {} |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 225 | virtual void randomlyInsertNop(float Probability) = 0; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 226 | // This gives the target an opportunity to post-process the lowered |
| 227 | // expansion before returning. The primary intention is to do some |
| 228 | // Register Manager activity as necessary, specifically to eagerly |
| 229 | // allocate registers based on affinity and other factors. The |
| 230 | // simplest lowering does nothing here and leaves it all to a |
| 231 | // subsequent global register allocation pass. |
| 232 | virtual void postLower() {} |
| 233 | |
| 234 | Cfg *Func; |
| 235 | GlobalContext *Ctx; |
| 236 | bool HasComputedFrame; |
Jan Voung | 44d53e1 | 2014-09-11 19:18:03 -0700 | [diff] [blame] | 237 | bool CallsReturnsTwice; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 238 | // StackAdjustment keeps track of the current stack offset from its |
| 239 | // natural location, as arguments are pushed for a function call. |
| 240 | int32_t StackAdjustment; |
| 241 | LoweringContext Context; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 242 | }; |
| 243 | |
Jim Stichnoth | de4ca71 | 2014-06-29 08:13:48 -0700 | [diff] [blame] | 244 | // TargetGlobalInitLowering is used for "lowering" global |
| 245 | // initializers. It is separated out from TargetLowering because it |
| 246 | // does not require a Cfg. |
| 247 | class TargetGlobalInitLowering { |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 248 | TargetGlobalInitLowering(const TargetGlobalInitLowering &) = delete; |
| 249 | TargetGlobalInitLowering &operator=(const TargetGlobalInitLowering &) = |
| 250 | delete; |
| 251 | |
Jim Stichnoth | de4ca71 | 2014-06-29 08:13:48 -0700 | [diff] [blame] | 252 | public: |
| 253 | static TargetGlobalInitLowering *createLowering(TargetArch Target, |
| 254 | GlobalContext *Ctx); |
Jan Voung | 839c4ce | 2014-07-28 15:19:43 -0700 | [diff] [blame] | 255 | virtual ~TargetGlobalInitLowering(); |
| 256 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 257 | virtual void lower(const VariableDeclaration &Var) = 0; |
Jim Stichnoth | de4ca71 | 2014-06-29 08:13:48 -0700 | [diff] [blame] | 258 | |
| 259 | protected: |
| 260 | TargetGlobalInitLowering(GlobalContext *Ctx) : Ctx(Ctx) {} |
| 261 | GlobalContext *Ctx; |
Jim Stichnoth | de4ca71 | 2014-06-29 08:13:48 -0700 | [diff] [blame] | 262 | }; |
| 263 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 264 | } // end of namespace Ice |
| 265 | |
| 266 | #endif // SUBZERO_SRC_ICETARGETLOWERING_H |