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