blob: b15c4f1e5f157e3d4045540ce6662a345f47ef44 [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();
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 Walad4799f42014-08-14 14:24:12 -0700139 virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0;
Jan Voungb17f61d2014-08-28 16:00:53 -0700140 virtual SizeT getBundleAlignLog2Bytes() const = 0;
141 virtual llvm::ArrayRef<uint8_t> getNonExecBundlePadding() const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700142 bool hasComputedFrame() const { return HasComputedFrame; }
Matt Walac3302742014-08-15 16:21:56 -0700143 bool shouldDoNopInsertion() const;
Jan Voung44d53e12014-09-11 19:18:03 -0700144 // Returns true if this function calls a function that has the
145 // "returns twice" attribute.
146 bool callsReturnsTwice() const { return CallsReturnsTwice; }
147 void setCallsReturnsTwice(bool RetTwice) {
148 CallsReturnsTwice = RetTwice;
149 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700150 int32_t getStackAdjustment() const { return StackAdjustment; }
151 void updateStackAdjustment(int32_t Offset) { StackAdjustment += Offset; }
152 void resetStackAdjustment() { StackAdjustment = 0; }
153 LoweringContext &getContext() { return Context; }
154
155 enum RegSet {
156 RegSet_None = 0,
157 RegSet_CallerSave = 1 << 0,
158 RegSet_CalleeSave = 1 << 1,
159 RegSet_StackPointer = 1 << 2,
160 RegSet_FramePointer = 1 << 3,
161 RegSet_All = ~RegSet_None
162 };
163 typedef uint32_t RegSetMask;
164
165 virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include,
166 RegSetMask Exclude) const = 0;
167 virtual const llvm::SmallBitVector &getRegisterSetForType(Type Ty) const = 0;
168 void regAlloc();
169
170 virtual void emitVariable(const Variable *Var, const Cfg *Func) const = 0;
171
Matt Wala45a06232014-07-09 16:33:22 -0700172 // Performs target-specific argument lowering.
173 virtual void lowerArguments() = 0;
174
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700175 virtual void addProlog(CfgNode *Node) = 0;
176 virtual void addEpilog(CfgNode *Node) = 0;
177
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700178 virtual void emitConstants() const = 0;
179
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700180 virtual ~TargetLowering() {}
181
182protected:
183 TargetLowering(Cfg *Func)
184 : Func(Func), Ctx(Func->getContext()), HasComputedFrame(false),
Jan Voung44d53e12014-09-11 19:18:03 -0700185 CallsReturnsTwice(false), StackAdjustment(0) {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700186 virtual void lowerAlloca(const InstAlloca *Inst) = 0;
187 virtual void lowerArithmetic(const InstArithmetic *Inst) = 0;
188 virtual void lowerAssign(const InstAssign *Inst) = 0;
189 virtual void lowerBr(const InstBr *Inst) = 0;
190 virtual void lowerCall(const InstCall *Inst) = 0;
191 virtual void lowerCast(const InstCast *Inst) = 0;
192 virtual void lowerFcmp(const InstFcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700193 virtual void lowerExtractElement(const InstExtractElement *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700194 virtual void lowerIcmp(const InstIcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700195 virtual void lowerInsertElement(const InstInsertElement *Inst) = 0;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700196 virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700197 virtual void lowerLoad(const InstLoad *Inst) = 0;
198 virtual void lowerPhi(const InstPhi *Inst) = 0;
199 virtual void lowerRet(const InstRet *Inst) = 0;
200 virtual void lowerSelect(const InstSelect *Inst) = 0;
201 virtual void lowerStore(const InstStore *Inst) = 0;
202 virtual void lowerSwitch(const InstSwitch *Inst) = 0;
203 virtual void lowerUnreachable(const InstUnreachable *Inst) = 0;
204
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700205 virtual void doAddressOptLoad() {}
206 virtual void doAddressOptStore() {}
Matt Walac3302742014-08-15 16:21:56 -0700207 virtual void randomlyInsertNop(float Probability) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700208 // This gives the target an opportunity to post-process the lowered
209 // expansion before returning. The primary intention is to do some
210 // Register Manager activity as necessary, specifically to eagerly
211 // allocate registers based on affinity and other factors. The
212 // simplest lowering does nothing here and leaves it all to a
213 // subsequent global register allocation pass.
214 virtual void postLower() {}
215
216 Cfg *Func;
217 GlobalContext *Ctx;
218 bool HasComputedFrame;
Jan Voung44d53e12014-09-11 19:18:03 -0700219 bool CallsReturnsTwice;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700220 // StackAdjustment keeps track of the current stack offset from its
221 // natural location, as arguments are pushed for a function call.
222 int32_t StackAdjustment;
223 LoweringContext Context;
224
225private:
226 TargetLowering(const TargetLowering &) LLVM_DELETED_FUNCTION;
227 TargetLowering &operator=(const TargetLowering &) LLVM_DELETED_FUNCTION;
228};
229
Jim Stichnothde4ca712014-06-29 08:13:48 -0700230// TargetGlobalInitLowering is used for "lowering" global
231// initializers. It is separated out from TargetLowering because it
232// does not require a Cfg.
233class TargetGlobalInitLowering {
234public:
235 static TargetGlobalInitLowering *createLowering(TargetArch Target,
236 GlobalContext *Ctx);
Jan Voung839c4ce2014-07-28 15:19:43 -0700237 virtual ~TargetGlobalInitLowering();
238
Jim Stichnothde4ca712014-06-29 08:13:48 -0700239 // TODO: Allow relocations to be represented as part of the Data.
240 virtual void lower(const IceString &Name, SizeT Align, bool IsInternal,
241 bool IsConst, bool IsZeroInitializer, SizeT Size,
242 const char *Data, bool DisableTranslation) = 0;
243
244protected:
245 TargetGlobalInitLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
246 GlobalContext *Ctx;
247
248private:
249 TargetGlobalInitLowering(const TargetGlobalInitLowering &)
250 LLVM_DELETED_FUNCTION;
251 TargetGlobalInitLowering &
252 operator=(const TargetGlobalInitLowering &) LLVM_DELETED_FUNCTION;
253};
254
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700255} // end of namespace Ice
256
257#endif // SUBZERO_SRC_ICETARGETLOWERING_H