blob: 37e14dfd9361596d4ea092c56c0110169636d7ce [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
Jan Voung8acded02014-09-22 18:02:25 -070028class Assembler;
29
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070030// LoweringContext makes it easy to iterate through non-deleted
31// instructions in a node, and insert new (lowered) instructions at
32// the current point. Along with the instruction list container and
33// associated iterators, it holds the current node, which is needed
34// when inserting new instructions in order to track whether variables
35// are used as single-block or multi-block.
36class LoweringContext {
37public:
38 LoweringContext() : Node(NULL) {}
39 ~LoweringContext() {}
40 void init(CfgNode *Node);
41 Inst *getNextInst() const {
42 if (Next == End)
43 return NULL;
44 return *Next;
45 }
Jan Voungc820ddf2014-07-29 14:38:51 -070046 Inst *getNextInst(InstList::iterator &Iter) const {
Jan Vounge6e497d2014-07-30 10:06:03 -070047 advanceForward(Iter);
Jan Voungc820ddf2014-07-29 14:38:51 -070048 if (Iter == End)
49 return NULL;
50 return *Iter;
51 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070052 CfgNode *getNode() const { return Node; }
53 bool atEnd() const { return Cur == End; }
54 InstList::iterator getCur() const { return Cur; }
55 InstList::iterator getEnd() const { return End; }
56 void insert(Inst *Inst);
Jan Vounge6e497d2014-07-30 10:06:03 -070057 Inst *getLastInserted() const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070058 void advanceCur() { Cur = Next; }
Jan Vounge6e497d2014-07-30 10:06:03 -070059 void advanceNext() { advanceForward(Next); }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070060 void setInsertPoint(const InstList::iterator &Position) { Next = Position; }
61
62private:
63 // Node is the argument to Inst::updateVars().
64 CfgNode *Node;
65 // Cur points to the current instruction being considered. It is
66 // guaranteed to point to a non-deleted instruction, or to be End.
67 InstList::iterator Cur;
68 // Next doubles as a pointer to the next valid instruction (if any),
69 // and the new-instruction insertion point. It is also updated for
70 // the caller in case the lowering consumes more than one high-level
71 // instruction. It is guaranteed to point to a non-deleted
72 // instruction after Cur, or to be End. TODO: Consider separating
73 // the notion of "next valid instruction" and "new instruction
74 // insertion point", to avoid confusion when previously-deleted
75 // instructions come between the two points.
76 InstList::iterator Next;
Jan Vounge6e497d2014-07-30 10:06:03 -070077 // Begin is a copy of Insts.begin(), used if iterators are moved backward.
78 InstList::iterator Begin;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070079 // End is a copy of Insts.end(), used if Next needs to be advanced.
80 InstList::iterator End;
81
Jan Voungc820ddf2014-07-29 14:38:51 -070082 void skipDeleted(InstList::iterator &I) const;
Jan Vounge6e497d2014-07-30 10:06:03 -070083 void advanceForward(InstList::iterator &I) const;
84 void advanceBackward(InstList::iterator &I) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070085 LoweringContext(const LoweringContext &) LLVM_DELETED_FUNCTION;
86 LoweringContext &operator=(const LoweringContext &) LLVM_DELETED_FUNCTION;
87};
88
89class TargetLowering {
90public:
91 static TargetLowering *createLowering(TargetArch Target, Cfg *Func);
Jan Voung8acded02014-09-22 18:02:25 -070092 static Assembler *createAssembler(TargetArch Target, Cfg *Func);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070093 void translate() {
94 switch (Ctx->getOptLevel()) {
95 case Opt_m1:
96 translateOm1();
97 break;
98 case Opt_0:
99 translateO0();
100 break;
101 case Opt_1:
102 translateO1();
103 break;
104 case Opt_2:
105 translateO2();
106 break;
107 default:
108 Func->setError("Target doesn't specify lowering steps.");
109 break;
110 }
111 }
112 virtual void translateOm1() {
113 Func->setError("Target doesn't specify Om1 lowering steps.");
114 }
115 virtual void translateO0() {
116 Func->setError("Target doesn't specify O0 lowering steps.");
117 }
118 virtual void translateO1() {
119 Func->setError("Target doesn't specify O1 lowering steps.");
120 }
121 virtual void translateO2() {
122 Func->setError("Target doesn't specify O2 lowering steps.");
123 }
124
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700125 // Tries to do address mode optimization on a single instruction.
126 void doAddressOpt();
Matt Walac3302742014-08-15 16:21:56 -0700127 // Randomly insert NOPs.
128 void doNopInsertion();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700129 // Lowers a single instruction.
130 void lower();
Jim Stichnothff9c7062014-09-18 04:50:49 -0700131 // Tries to do branch optimization on a single instruction. Returns
132 // true if some optimization was done.
133 virtual bool doBranchOpt(Inst * /*I*/, const CfgNode * /*NextNode*/) {
134 return false;
135 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700136
137 // Returns a variable pre-colored to the specified physical
138 // register. This is generally used to get very direct access to
139 // the register such as in the prolog or epilog or for marking
140 // scratch registers as killed by a call.
141 virtual Variable *getPhysicalRegister(SizeT RegNum) = 0;
142 // Returns a printable name for the register.
143 virtual IceString getRegName(SizeT RegNum, Type Ty) const = 0;
144
145 virtual bool hasFramePointer() const { return false; }
146 virtual SizeT getFrameOrStackReg() const = 0;
Matt Walad4799f42014-08-14 14:24:12 -0700147 virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0;
Jan Voungb17f61d2014-08-28 16:00:53 -0700148 virtual SizeT getBundleAlignLog2Bytes() const = 0;
149 virtual llvm::ArrayRef<uint8_t> getNonExecBundlePadding() const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700150 bool hasComputedFrame() const { return HasComputedFrame; }
Matt Walac3302742014-08-15 16:21:56 -0700151 bool shouldDoNopInsertion() const;
Jan Voung44d53e12014-09-11 19:18:03 -0700152 // Returns true if this function calls a function that has the
153 // "returns twice" attribute.
154 bool callsReturnsTwice() const { return CallsReturnsTwice; }
155 void setCallsReturnsTwice(bool RetTwice) {
156 CallsReturnsTwice = RetTwice;
157 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700158 int32_t getStackAdjustment() const { return StackAdjustment; }
159 void updateStackAdjustment(int32_t Offset) { StackAdjustment += Offset; }
160 void resetStackAdjustment() { StackAdjustment = 0; }
161 LoweringContext &getContext() { return Context; }
162
163 enum RegSet {
164 RegSet_None = 0,
165 RegSet_CallerSave = 1 << 0,
166 RegSet_CalleeSave = 1 << 1,
167 RegSet_StackPointer = 1 << 2,
168 RegSet_FramePointer = 1 << 3,
169 RegSet_All = ~RegSet_None
170 };
171 typedef uint32_t RegSetMask;
172
173 virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include,
174 RegSetMask Exclude) const = 0;
175 virtual const llvm::SmallBitVector &getRegisterSetForType(Type Ty) const = 0;
176 void regAlloc();
177
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700178 virtual void emitVariable(const Variable *Var) const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700179
Matt Wala45a06232014-07-09 16:33:22 -0700180 // Performs target-specific argument lowering.
181 virtual void lowerArguments() = 0;
182
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700183 virtual void addProlog(CfgNode *Node) = 0;
184 virtual void addEpilog(CfgNode *Node) = 0;
185
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700186 virtual void emitConstants() const = 0;
187
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700188 virtual ~TargetLowering() {}
189
190protected:
191 TargetLowering(Cfg *Func)
192 : Func(Func), Ctx(Func->getContext()), HasComputedFrame(false),
Jan Voung44d53e12014-09-11 19:18:03 -0700193 CallsReturnsTwice(false), StackAdjustment(0) {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700194 virtual void lowerAlloca(const InstAlloca *Inst) = 0;
195 virtual void lowerArithmetic(const InstArithmetic *Inst) = 0;
196 virtual void lowerAssign(const InstAssign *Inst) = 0;
197 virtual void lowerBr(const InstBr *Inst) = 0;
198 virtual void lowerCall(const InstCall *Inst) = 0;
199 virtual void lowerCast(const InstCast *Inst) = 0;
200 virtual void lowerFcmp(const InstFcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700201 virtual void lowerExtractElement(const InstExtractElement *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700202 virtual void lowerIcmp(const InstIcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700203 virtual void lowerInsertElement(const InstInsertElement *Inst) = 0;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700204 virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700205 virtual void lowerLoad(const InstLoad *Inst) = 0;
206 virtual void lowerPhi(const InstPhi *Inst) = 0;
207 virtual void lowerRet(const InstRet *Inst) = 0;
208 virtual void lowerSelect(const InstSelect *Inst) = 0;
209 virtual void lowerStore(const InstStore *Inst) = 0;
210 virtual void lowerSwitch(const InstSwitch *Inst) = 0;
211 virtual void lowerUnreachable(const InstUnreachable *Inst) = 0;
212
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700213 virtual void doAddressOptLoad() {}
214 virtual void doAddressOptStore() {}
Matt Walac3302742014-08-15 16:21:56 -0700215 virtual void randomlyInsertNop(float Probability) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700216 // This gives the target an opportunity to post-process the lowered
217 // expansion before returning. The primary intention is to do some
218 // Register Manager activity as necessary, specifically to eagerly
219 // allocate registers based on affinity and other factors. The
220 // simplest lowering does nothing here and leaves it all to a
221 // subsequent global register allocation pass.
222 virtual void postLower() {}
223
224 Cfg *Func;
225 GlobalContext *Ctx;
226 bool HasComputedFrame;
Jan Voung44d53e12014-09-11 19:18:03 -0700227 bool CallsReturnsTwice;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700228 // StackAdjustment keeps track of the current stack offset from its
229 // natural location, as arguments are pushed for a function call.
230 int32_t StackAdjustment;
231 LoweringContext Context;
232
233private:
234 TargetLowering(const TargetLowering &) LLVM_DELETED_FUNCTION;
235 TargetLowering &operator=(const TargetLowering &) LLVM_DELETED_FUNCTION;
236};
237
Jim Stichnothde4ca712014-06-29 08:13:48 -0700238// TargetGlobalInitLowering is used for "lowering" global
239// initializers. It is separated out from TargetLowering because it
240// does not require a Cfg.
241class TargetGlobalInitLowering {
242public:
243 static TargetGlobalInitLowering *createLowering(TargetArch Target,
244 GlobalContext *Ctx);
Jan Voung839c4ce2014-07-28 15:19:43 -0700245 virtual ~TargetGlobalInitLowering();
246
Jim Stichnothde4ca712014-06-29 08:13:48 -0700247 // TODO: Allow relocations to be represented as part of the Data.
248 virtual void lower(const IceString &Name, SizeT Align, bool IsInternal,
249 bool IsConst, bool IsZeroInitializer, SizeT Size,
250 const char *Data, bool DisableTranslation) = 0;
251
252protected:
253 TargetGlobalInitLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
254 GlobalContext *Ctx;
255
256private:
257 TargetGlobalInitLowering(const TargetGlobalInitLowering &)
258 LLVM_DELETED_FUNCTION;
259 TargetGlobalInitLowering &
260 operator=(const TargetGlobalInitLowering &) LLVM_DELETED_FUNCTION;
261};
262
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700263} // end of namespace Ice
264
265#endif // SUBZERO_SRC_ICETARGETLOWERING_H