blob: 7383fff4811906c63dd57651254d22bb36235f30 [file] [log] [blame]
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -07001//===- 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
26namespace 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.
34class LoweringContext {
35public:
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 Voungc820ddf2014-07-29 14:38:51 -070044 Inst *getNextInst(InstList::iterator &Iter) const {
Jan Vounge6e497d2014-07-30 10:06:03 -070045 advanceForward(Iter);
Jan Voungc820ddf2014-07-29 14:38:51 -070046 if (Iter == End)
47 return NULL;
48 return *Iter;
49 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070050 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 Vounge6e497d2014-07-30 10:06:03 -070055 Inst *getLastInserted() const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070056 void advanceCur() { Cur = Next; }
Jan Vounge6e497d2014-07-30 10:06:03 -070057 void advanceNext() { advanceForward(Next); }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070058 void setInsertPoint(const InstList::iterator &Position) { Next = Position; }
59
60private:
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 Vounge6e497d2014-07-30 10:06:03 -070075 // Begin is a copy of Insts.begin(), used if iterators are moved backward.
76 InstList::iterator Begin;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070077 // End is a copy of Insts.end(), used if Next needs to be advanced.
78 InstList::iterator End;
79
Jan Voungc820ddf2014-07-29 14:38:51 -070080 void skipDeleted(InstList::iterator &I) const;
Jan Vounge6e497d2014-07-30 10:06:03 -070081 void advanceForward(InstList::iterator &I) const;
82 void advanceBackward(InstList::iterator &I) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070083 LoweringContext(const LoweringContext &) LLVM_DELETED_FUNCTION;
84 LoweringContext &operator=(const LoweringContext &) LLVM_DELETED_FUNCTION;
85};
86
87class TargetLowering {
88public:
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 Stichnothd97c7df2014-06-04 11:57:08 -0700122 // Tries to do address mode optimization on a single instruction.
123 void doAddressOpt();
Matt Walac3302742014-08-15 16:21:56 -0700124 // Randomly insert NOPs.
125 void doNopInsertion();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700126 // Lowers a single instruction.
127 void lower();
Jim Stichnothff9c7062014-09-18 04:50:49 -0700128 // Tries to do branch optimization on a single instruction. Returns
129 // true if some optimization was done.
130 virtual bool doBranchOpt(Inst * /*I*/, const CfgNode * /*NextNode*/) {
131 return false;
132 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700133
134 // Returns a variable pre-colored to the specified physical
135 // register. This is generally used to get very direct access to
136 // the register such as in the prolog or epilog or for marking
137 // scratch registers as killed by a call.
138 virtual Variable *getPhysicalRegister(SizeT RegNum) = 0;
139 // Returns a printable name for the register.
140 virtual IceString getRegName(SizeT RegNum, Type Ty) const = 0;
141
142 virtual bool hasFramePointer() const { return false; }
143 virtual SizeT getFrameOrStackReg() const = 0;
Matt Walad4799f42014-08-14 14:24:12 -0700144 virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0;
Jan Voungb17f61d2014-08-28 16:00:53 -0700145 virtual SizeT getBundleAlignLog2Bytes() const = 0;
146 virtual llvm::ArrayRef<uint8_t> getNonExecBundlePadding() const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700147 bool hasComputedFrame() const { return HasComputedFrame; }
Matt Walac3302742014-08-15 16:21:56 -0700148 bool shouldDoNopInsertion() const;
Jan Voung44d53e12014-09-11 19:18:03 -0700149 // Returns true if this function calls a function that has the
150 // "returns twice" attribute.
151 bool callsReturnsTwice() const { return CallsReturnsTwice; }
152 void setCallsReturnsTwice(bool RetTwice) {
153 CallsReturnsTwice = RetTwice;
154 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700155 int32_t getStackAdjustment() const { return StackAdjustment; }
156 void updateStackAdjustment(int32_t Offset) { StackAdjustment += Offset; }
157 void resetStackAdjustment() { StackAdjustment = 0; }
158 LoweringContext &getContext() { return Context; }
159
160 enum RegSet {
161 RegSet_None = 0,
162 RegSet_CallerSave = 1 << 0,
163 RegSet_CalleeSave = 1 << 1,
164 RegSet_StackPointer = 1 << 2,
165 RegSet_FramePointer = 1 << 3,
166 RegSet_All = ~RegSet_None
167 };
168 typedef uint32_t RegSetMask;
169
170 virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include,
171 RegSetMask Exclude) const = 0;
172 virtual const llvm::SmallBitVector &getRegisterSetForType(Type Ty) const = 0;
173 void regAlloc();
174
175 virtual void emitVariable(const Variable *Var, const Cfg *Func) const = 0;
176
Matt Wala45a06232014-07-09 16:33:22 -0700177 // Performs target-specific argument lowering.
178 virtual void lowerArguments() = 0;
179
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700180 virtual void addProlog(CfgNode *Node) = 0;
181 virtual void addEpilog(CfgNode *Node) = 0;
182
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700183 virtual void emitConstants() const = 0;
184
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700185 virtual ~TargetLowering() {}
186
187protected:
188 TargetLowering(Cfg *Func)
189 : Func(Func), Ctx(Func->getContext()), HasComputedFrame(false),
Jan Voung44d53e12014-09-11 19:18:03 -0700190 CallsReturnsTwice(false), StackAdjustment(0) {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700191 virtual void lowerAlloca(const InstAlloca *Inst) = 0;
192 virtual void lowerArithmetic(const InstArithmetic *Inst) = 0;
193 virtual void lowerAssign(const InstAssign *Inst) = 0;
194 virtual void lowerBr(const InstBr *Inst) = 0;
195 virtual void lowerCall(const InstCall *Inst) = 0;
196 virtual void lowerCast(const InstCast *Inst) = 0;
197 virtual void lowerFcmp(const InstFcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700198 virtual void lowerExtractElement(const InstExtractElement *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700199 virtual void lowerIcmp(const InstIcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700200 virtual void lowerInsertElement(const InstInsertElement *Inst) = 0;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700201 virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700202 virtual void lowerLoad(const InstLoad *Inst) = 0;
203 virtual void lowerPhi(const InstPhi *Inst) = 0;
204 virtual void lowerRet(const InstRet *Inst) = 0;
205 virtual void lowerSelect(const InstSelect *Inst) = 0;
206 virtual void lowerStore(const InstStore *Inst) = 0;
207 virtual void lowerSwitch(const InstSwitch *Inst) = 0;
208 virtual void lowerUnreachable(const InstUnreachable *Inst) = 0;
209
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700210 virtual void doAddressOptLoad() {}
211 virtual void doAddressOptStore() {}
Matt Walac3302742014-08-15 16:21:56 -0700212 virtual void randomlyInsertNop(float Probability) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700213 // This gives the target an opportunity to post-process the lowered
214 // expansion before returning. The primary intention is to do some
215 // Register Manager activity as necessary, specifically to eagerly
216 // allocate registers based on affinity and other factors. The
217 // simplest lowering does nothing here and leaves it all to a
218 // subsequent global register allocation pass.
219 virtual void postLower() {}
220
221 Cfg *Func;
222 GlobalContext *Ctx;
223 bool HasComputedFrame;
Jan Voung44d53e12014-09-11 19:18:03 -0700224 bool CallsReturnsTwice;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700225 // StackAdjustment keeps track of the current stack offset from its
226 // natural location, as arguments are pushed for a function call.
227 int32_t StackAdjustment;
228 LoweringContext Context;
229
230private:
231 TargetLowering(const TargetLowering &) LLVM_DELETED_FUNCTION;
232 TargetLowering &operator=(const TargetLowering &) LLVM_DELETED_FUNCTION;
233};
234
Jim Stichnothde4ca712014-06-29 08:13:48 -0700235// TargetGlobalInitLowering is used for "lowering" global
236// initializers. It is separated out from TargetLowering because it
237// does not require a Cfg.
238class TargetGlobalInitLowering {
239public:
240 static TargetGlobalInitLowering *createLowering(TargetArch Target,
241 GlobalContext *Ctx);
Jan Voung839c4ce2014-07-28 15:19:43 -0700242 virtual ~TargetGlobalInitLowering();
243
Jim Stichnothde4ca712014-06-29 08:13:48 -0700244 // TODO: Allow relocations to be represented as part of the Data.
245 virtual void lower(const IceString &Name, SizeT Align, bool IsInternal,
246 bool IsConst, bool IsZeroInitializer, SizeT Size,
247 const char *Data, bool DisableTranslation) = 0;
248
249protected:
250 TargetGlobalInitLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
251 GlobalContext *Ctx;
252
253private:
254 TargetGlobalInitLowering(const TargetGlobalInitLowering &)
255 LLVM_DELETED_FUNCTION;
256 TargetGlobalInitLowering &
257 operator=(const TargetGlobalInitLowering &) LLVM_DELETED_FUNCTION;
258};
259
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700260} // end of namespace Ice
261
262#endif // SUBZERO_SRC_ICETARGETLOWERING_H