blob: bbda1f8b0b36fa8e1a62131282cd0d63877bb942 [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"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070022#include "IceInst.h" // for the names of the Inst subtypes
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070023#include "IceTypes.h"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070024
25namespace Ice {
26
Jim Stichnothf44f3712014-10-01 14:05:51 -070027typedef uint8_t AsmCodeByte;
28
Jan Voung8acded02014-09-22 18:02:25 -070029class Assembler;
30
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070031// 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.
37class LoweringContext {
38public:
39 LoweringContext() : Node(NULL) {}
40 ~LoweringContext() {}
41 void init(CfgNode *Node);
42 Inst *getNextInst() const {
43 if (Next == End)
44 return NULL;
45 return *Next;
46 }
Jan Voungc820ddf2014-07-29 14:38:51 -070047 Inst *getNextInst(InstList::iterator &Iter) const {
Jan Vounge6e497d2014-07-30 10:06:03 -070048 advanceForward(Iter);
Jan Voungc820ddf2014-07-29 14:38:51 -070049 if (Iter == End)
50 return NULL;
51 return *Iter;
52 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070053 CfgNode *getNode() const { return Node; }
54 bool atEnd() const { return Cur == End; }
55 InstList::iterator getCur() const { return Cur; }
56 InstList::iterator getEnd() const { return End; }
Jim Stichnothf44f3712014-10-01 14:05:51 -070057 // Adaptor to enable range-based for loops.
58 InstList::iterator begin() const { return getCur(); }
59 InstList::iterator end() const { return getEnd(); }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070060 void insert(Inst *Inst);
Jan Vounge6e497d2014-07-30 10:06:03 -070061 Inst *getLastInserted() const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070062 void advanceCur() { Cur = Next; }
Jan Vounge6e497d2014-07-30 10:06:03 -070063 void advanceNext() { advanceForward(Next); }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070064 void setInsertPoint(const InstList::iterator &Position) { Next = Position; }
65
66private:
67 // Node is the argument to Inst::updateVars().
68 CfgNode *Node;
69 // Cur points to the current instruction being considered. It is
70 // guaranteed to point to a non-deleted instruction, or to be End.
71 InstList::iterator Cur;
72 // Next doubles as a pointer to the next valid instruction (if any),
73 // and the new-instruction insertion point. It is also updated for
74 // the caller in case the lowering consumes more than one high-level
75 // instruction. It is guaranteed to point to a non-deleted
76 // instruction after Cur, or to be End. TODO: Consider separating
77 // the notion of "next valid instruction" and "new instruction
78 // insertion point", to avoid confusion when previously-deleted
79 // instructions come between the two points.
80 InstList::iterator Next;
Jan Vounge6e497d2014-07-30 10:06:03 -070081 // Begin is a copy of Insts.begin(), used if iterators are moved backward.
82 InstList::iterator Begin;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070083 // End is a copy of Insts.end(), used if Next needs to be advanced.
84 InstList::iterator End;
85
Jan Voungc820ddf2014-07-29 14:38:51 -070086 void skipDeleted(InstList::iterator &I) const;
Jan Vounge6e497d2014-07-30 10:06:03 -070087 void advanceForward(InstList::iterator &I) const;
88 void advanceBackward(InstList::iterator &I) const;
Jim Stichnoth0795ba02014-10-01 14:23:01 -070089 LoweringContext(const LoweringContext &) = delete;
90 LoweringContext &operator=(const LoweringContext &) = delete;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070091};
92
93class TargetLowering {
94public:
95 static TargetLowering *createLowering(TargetArch Target, Cfg *Func);
Jan Voung8acded02014-09-22 18:02:25 -070096 static Assembler *createAssembler(TargetArch Target, Cfg *Func);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070097 void translate() {
98 switch (Ctx->getOptLevel()) {
99 case Opt_m1:
100 translateOm1();
101 break;
102 case Opt_0:
103 translateO0();
104 break;
105 case Opt_1:
106 translateO1();
107 break;
108 case Opt_2:
109 translateO2();
110 break;
111 default:
112 Func->setError("Target doesn't specify lowering steps.");
113 break;
114 }
115 }
116 virtual void translateOm1() {
117 Func->setError("Target doesn't specify Om1 lowering steps.");
118 }
119 virtual void translateO0() {
120 Func->setError("Target doesn't specify O0 lowering steps.");
121 }
122 virtual void translateO1() {
123 Func->setError("Target doesn't specify O1 lowering steps.");
124 }
125 virtual void translateO2() {
126 Func->setError("Target doesn't specify O2 lowering steps.");
127 }
128
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700129 // Tries to do address mode optimization on a single instruction.
130 void doAddressOpt();
Matt Walac3302742014-08-15 16:21:56 -0700131 // Randomly insert NOPs.
132 void doNopInsertion();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700133 // Lowers a single instruction.
134 void lower();
Jim Stichnothff9c7062014-09-18 04:50:49 -0700135 // Tries to do branch optimization on a single instruction. Returns
136 // true if some optimization was done.
137 virtual bool doBranchOpt(Inst * /*I*/, const CfgNode * /*NextNode*/) {
138 return false;
139 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700140
141 // Returns a variable pre-colored to the specified physical
142 // register. This is generally used to get very direct access to
143 // the register such as in the prolog or epilog or for marking
144 // scratch registers as killed by a call.
145 virtual Variable *getPhysicalRegister(SizeT RegNum) = 0;
146 // Returns a printable name for the register.
147 virtual IceString getRegName(SizeT RegNum, Type Ty) const = 0;
148
149 virtual bool hasFramePointer() const { return false; }
150 virtual SizeT getFrameOrStackReg() const = 0;
Matt Walad4799f42014-08-14 14:24:12 -0700151 virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0;
Jan Voungb17f61d2014-08-28 16:00:53 -0700152 virtual SizeT getBundleAlignLog2Bytes() const = 0;
Jim Stichnothf44f3712014-10-01 14:05:51 -0700153 virtual llvm::ArrayRef<AsmCodeByte> getNonExecBundlePadding() const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700154 bool hasComputedFrame() const { return HasComputedFrame; }
Matt Walac3302742014-08-15 16:21:56 -0700155 bool shouldDoNopInsertion() const;
Jan Voung44d53e12014-09-11 19:18:03 -0700156 // Returns true if this function calls a function that has the
157 // "returns twice" attribute.
158 bool callsReturnsTwice() const { return CallsReturnsTwice; }
159 void setCallsReturnsTwice(bool RetTwice) {
160 CallsReturnsTwice = RetTwice;
161 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700162 int32_t getStackAdjustment() const { return StackAdjustment; }
163 void updateStackAdjustment(int32_t Offset) { StackAdjustment += Offset; }
164 void resetStackAdjustment() { StackAdjustment = 0; }
165 LoweringContext &getContext() { return Context; }
166
167 enum RegSet {
168 RegSet_None = 0,
169 RegSet_CallerSave = 1 << 0,
170 RegSet_CalleeSave = 1 << 1,
171 RegSet_StackPointer = 1 << 2,
172 RegSet_FramePointer = 1 << 3,
173 RegSet_All = ~RegSet_None
174 };
175 typedef uint32_t RegSetMask;
176
177 virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include,
178 RegSetMask Exclude) const = 0;
179 virtual const llvm::SmallBitVector &getRegisterSetForType(Type Ty) const = 0;
180 void regAlloc();
181
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700182 virtual void emitVariable(const Variable *Var) const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700183
Matt Wala45a06232014-07-09 16:33:22 -0700184 // Performs target-specific argument lowering.
185 virtual void lowerArguments() = 0;
186
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700187 virtual void addProlog(CfgNode *Node) = 0;
188 virtual void addEpilog(CfgNode *Node) = 0;
189
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700190 virtual void emitConstants() const = 0;
191
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700192 virtual ~TargetLowering() {}
193
194protected:
195 TargetLowering(Cfg *Func)
196 : Func(Func), Ctx(Func->getContext()), HasComputedFrame(false),
Jan Voung44d53e12014-09-11 19:18:03 -0700197 CallsReturnsTwice(false), StackAdjustment(0) {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700198 virtual void lowerAlloca(const InstAlloca *Inst) = 0;
199 virtual void lowerArithmetic(const InstArithmetic *Inst) = 0;
200 virtual void lowerAssign(const InstAssign *Inst) = 0;
201 virtual void lowerBr(const InstBr *Inst) = 0;
202 virtual void lowerCall(const InstCall *Inst) = 0;
203 virtual void lowerCast(const InstCast *Inst) = 0;
204 virtual void lowerFcmp(const InstFcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700205 virtual void lowerExtractElement(const InstExtractElement *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700206 virtual void lowerIcmp(const InstIcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700207 virtual void lowerInsertElement(const InstInsertElement *Inst) = 0;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700208 virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700209 virtual void lowerLoad(const InstLoad *Inst) = 0;
210 virtual void lowerPhi(const InstPhi *Inst) = 0;
211 virtual void lowerRet(const InstRet *Inst) = 0;
212 virtual void lowerSelect(const InstSelect *Inst) = 0;
213 virtual void lowerStore(const InstStore *Inst) = 0;
214 virtual void lowerSwitch(const InstSwitch *Inst) = 0;
215 virtual void lowerUnreachable(const InstUnreachable *Inst) = 0;
216
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700217 virtual void doAddressOptLoad() {}
218 virtual void doAddressOptStore() {}
Matt Walac3302742014-08-15 16:21:56 -0700219 virtual void randomlyInsertNop(float Probability) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700220 // This gives the target an opportunity to post-process the lowered
221 // expansion before returning. The primary intention is to do some
222 // Register Manager activity as necessary, specifically to eagerly
223 // allocate registers based on affinity and other factors. The
224 // simplest lowering does nothing here and leaves it all to a
225 // subsequent global register allocation pass.
226 virtual void postLower() {}
227
228 Cfg *Func;
229 GlobalContext *Ctx;
230 bool HasComputedFrame;
Jan Voung44d53e12014-09-11 19:18:03 -0700231 bool CallsReturnsTwice;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700232 // StackAdjustment keeps track of the current stack offset from its
233 // natural location, as arguments are pushed for a function call.
234 int32_t StackAdjustment;
235 LoweringContext Context;
236
237private:
Jim Stichnoth0795ba02014-10-01 14:23:01 -0700238 TargetLowering(const TargetLowering &) = delete;
239 TargetLowering &operator=(const TargetLowering &) = delete;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700240};
241
Jim Stichnothde4ca712014-06-29 08:13:48 -0700242// TargetGlobalInitLowering is used for "lowering" global
243// initializers. It is separated out from TargetLowering because it
244// does not require a Cfg.
245class TargetGlobalInitLowering {
246public:
247 static TargetGlobalInitLowering *createLowering(TargetArch Target,
248 GlobalContext *Ctx);
Jan Voung839c4ce2014-07-28 15:19:43 -0700249 virtual ~TargetGlobalInitLowering();
250
Karl Schimpf9d98d792014-10-13 15:01:08 -0700251 virtual void lower(const VariableDeclaration &Var) = 0;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700252
253protected:
254 TargetGlobalInitLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
255 GlobalContext *Ctx;
256
257private:
Jim Stichnoth0795ba02014-10-01 14:23:01 -0700258 TargetGlobalInitLowering(const TargetGlobalInitLowering &) = delete;
259 TargetGlobalInitLowering &operator=(const TargetGlobalInitLowering &) =
260 delete;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700261};
262
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700263} // end of namespace Ice
264
265#endif // SUBZERO_SRC_ICETARGETLOWERING_H