blob: 4bfe4b415b46c89124312b56533b00364a4b3561 [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 {
Jim Stichnoth7b451a92014-10-15 14:39:23 -070038 LoweringContext(const LoweringContext &) = delete;
39 LoweringContext &operator=(const LoweringContext &) = delete;
40
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070041public:
Jim Stichnoth98712a32014-10-24 10:59:02 -070042 LoweringContext() : Node(NULL), LastInserted(NULL) {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070043 ~LoweringContext() {}
44 void init(CfgNode *Node);
45 Inst *getNextInst() const {
46 if (Next == End)
47 return NULL;
48 return *Next;
49 }
Jan Voungc820ddf2014-07-29 14:38:51 -070050 Inst *getNextInst(InstList::iterator &Iter) const {
Jan Vounge6e497d2014-07-30 10:06:03 -070051 advanceForward(Iter);
Jan Voungc820ddf2014-07-29 14:38:51 -070052 if (Iter == End)
53 return NULL;
54 return *Iter;
55 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070056 CfgNode *getNode() const { return Node; }
57 bool atEnd() const { return Cur == End; }
58 InstList::iterator getCur() const { return Cur; }
59 InstList::iterator getEnd() const { return End; }
Jim Stichnothf44f3712014-10-01 14:05:51 -070060 // Adaptor to enable range-based for loops.
61 InstList::iterator begin() const { return getCur(); }
62 InstList::iterator end() const { return getEnd(); }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070063 void insert(Inst *Inst);
Jan Vounge6e497d2014-07-30 10:06:03 -070064 Inst *getLastInserted() const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070065 void advanceCur() { Cur = Next; }
Jan Vounge6e497d2014-07-30 10:06:03 -070066 void advanceNext() { advanceForward(Next); }
Jim Stichnoth336f6c42014-10-30 15:01:31 -070067 void rewind();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070068 void setInsertPoint(const InstList::iterator &Position) { Next = Position; }
69
70private:
71 // Node is the argument to Inst::updateVars().
72 CfgNode *Node;
Jim Stichnoth98712a32014-10-24 10:59:02 -070073 Inst *LastInserted;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070074 // Cur points to the current instruction being considered. It is
75 // guaranteed to point to a non-deleted instruction, or to be End.
76 InstList::iterator Cur;
77 // Next doubles as a pointer to the next valid instruction (if any),
78 // and the new-instruction insertion point. It is also updated for
79 // the caller in case the lowering consumes more than one high-level
80 // instruction. It is guaranteed to point to a non-deleted
81 // instruction after Cur, or to be End. TODO: Consider separating
82 // the notion of "next valid instruction" and "new instruction
83 // insertion point", to avoid confusion when previously-deleted
84 // instructions come between the two points.
85 InstList::iterator Next;
Jan Vounge6e497d2014-07-30 10:06:03 -070086 // Begin is a copy of Insts.begin(), used if iterators are moved backward.
87 InstList::iterator Begin;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070088 // End is a copy of Insts.end(), used if Next needs to be advanced.
89 InstList::iterator End;
90
Jan Voungc820ddf2014-07-29 14:38:51 -070091 void skipDeleted(InstList::iterator &I) const;
Jan Vounge6e497d2014-07-30 10:06:03 -070092 void advanceForward(InstList::iterator &I) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070093};
94
95class TargetLowering {
Jim Stichnoth7b451a92014-10-15 14:39:23 -070096 TargetLowering(const TargetLowering &) = delete;
97 TargetLowering &operator=(const TargetLowering &) = delete;
98
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070099public:
100 static TargetLowering *createLowering(TargetArch Target, Cfg *Func);
Jan Voung8acded02014-09-22 18:02:25 -0700101 static Assembler *createAssembler(TargetArch Target, Cfg *Func);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700102 void translate() {
103 switch (Ctx->getOptLevel()) {
104 case Opt_m1:
105 translateOm1();
106 break;
107 case Opt_0:
108 translateO0();
109 break;
110 case Opt_1:
111 translateO1();
112 break;
113 case Opt_2:
114 translateO2();
115 break;
116 default:
117 Func->setError("Target doesn't specify lowering steps.");
118 break;
119 }
120 }
121 virtual void translateOm1() {
122 Func->setError("Target doesn't specify Om1 lowering steps.");
123 }
124 virtual void translateO0() {
125 Func->setError("Target doesn't specify O0 lowering steps.");
126 }
127 virtual void translateO1() {
128 Func->setError("Target doesn't specify O1 lowering steps.");
129 }
130 virtual void translateO2() {
131 Func->setError("Target doesn't specify O2 lowering steps.");
132 }
133
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700134 // Tries to do address mode optimization on a single instruction.
135 void doAddressOpt();
Matt Walac3302742014-08-15 16:21:56 -0700136 // Randomly insert NOPs.
137 void doNopInsertion();
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700138 // Lowers a single non-Phi instruction.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700139 void lower();
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700140 // Does preliminary lowering of the set of Phi instructions in the
141 // current node. The main intention is to do what's needed to keep
142 // the unlowered Phi instructions consistent with the lowered
143 // non-Phi instructions, e.g. to lower 64-bit operands on a 32-bit
144 // target.
145 virtual void prelowerPhis() {}
146 // Lowers a list of "parallel" assignment instructions representing
147 // a topological sort of the Phi instructions.
148 virtual void lowerPhiAssignments(CfgNode *Node,
149 const AssignList &Assignments) = 0;
Jim Stichnothff9c7062014-09-18 04:50:49 -0700150 // Tries to do branch optimization on a single instruction. Returns
151 // true if some optimization was done.
152 virtual bool doBranchOpt(Inst * /*I*/, const CfgNode * /*NextNode*/) {
153 return false;
154 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700155
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700156 virtual SizeT getNumRegisters() const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700157 // Returns a variable pre-colored to the specified physical
158 // register. This is generally used to get very direct access to
159 // the register such as in the prolog or epilog or for marking
Jim Stichnoth98712a32014-10-24 10:59:02 -0700160 // scratch registers as killed by a call. If a Type is not
161 // provided, a target-specific default type is used.
162 virtual Variable *getPhysicalRegister(SizeT RegNum,
163 Type Ty = IceType_void) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700164 // Returns a printable name for the register.
165 virtual IceString getRegName(SizeT RegNum, Type Ty) const = 0;
166
167 virtual bool hasFramePointer() const { return false; }
168 virtual SizeT getFrameOrStackReg() const = 0;
Matt Walad4799f42014-08-14 14:24:12 -0700169 virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0;
Jan Voungb17f61d2014-08-28 16:00:53 -0700170 virtual SizeT getBundleAlignLog2Bytes() const = 0;
Jim Stichnothf44f3712014-10-01 14:05:51 -0700171 virtual llvm::ArrayRef<AsmCodeByte> getNonExecBundlePadding() const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700172 bool hasComputedFrame() const { return HasComputedFrame; }
Matt Walac3302742014-08-15 16:21:56 -0700173 bool shouldDoNopInsertion() const;
Jan Voung44d53e12014-09-11 19:18:03 -0700174 // Returns true if this function calls a function that has the
175 // "returns twice" attribute.
176 bool callsReturnsTwice() const { return CallsReturnsTwice; }
177 void setCallsReturnsTwice(bool RetTwice) {
178 CallsReturnsTwice = RetTwice;
179 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700180 int32_t getStackAdjustment() const { return StackAdjustment; }
181 void updateStackAdjustment(int32_t Offset) { StackAdjustment += Offset; }
182 void resetStackAdjustment() { StackAdjustment = 0; }
183 LoweringContext &getContext() { return Context; }
184
185 enum RegSet {
186 RegSet_None = 0,
187 RegSet_CallerSave = 1 << 0,
188 RegSet_CalleeSave = 1 << 1,
189 RegSet_StackPointer = 1 << 2,
190 RegSet_FramePointer = 1 << 3,
191 RegSet_All = ~RegSet_None
192 };
193 typedef uint32_t RegSetMask;
194
195 virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include,
196 RegSetMask Exclude) const = 0;
197 virtual const llvm::SmallBitVector &getRegisterSetForType(Type Ty) const = 0;
198 void regAlloc();
199
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700200 virtual void emitVariable(const Variable *Var) const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700201
Matt Wala45a06232014-07-09 16:33:22 -0700202 // Performs target-specific argument lowering.
203 virtual void lowerArguments() = 0;
204
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700205 virtual void addProlog(CfgNode *Node) = 0;
206 virtual void addEpilog(CfgNode *Node) = 0;
207
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700208 virtual void emitConstants() const = 0;
209
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700210 virtual ~TargetLowering() {}
211
212protected:
213 TargetLowering(Cfg *Func)
214 : Func(Func), Ctx(Func->getContext()), HasComputedFrame(false),
Jan Voung44d53e12014-09-11 19:18:03 -0700215 CallsReturnsTwice(false), StackAdjustment(0) {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700216 virtual void lowerAlloca(const InstAlloca *Inst) = 0;
217 virtual void lowerArithmetic(const InstArithmetic *Inst) = 0;
218 virtual void lowerAssign(const InstAssign *Inst) = 0;
219 virtual void lowerBr(const InstBr *Inst) = 0;
220 virtual void lowerCall(const InstCall *Inst) = 0;
221 virtual void lowerCast(const InstCast *Inst) = 0;
222 virtual void lowerFcmp(const InstFcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700223 virtual void lowerExtractElement(const InstExtractElement *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700224 virtual void lowerIcmp(const InstIcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700225 virtual void lowerInsertElement(const InstInsertElement *Inst) = 0;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700226 virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700227 virtual void lowerLoad(const InstLoad *Inst) = 0;
228 virtual void lowerPhi(const InstPhi *Inst) = 0;
229 virtual void lowerRet(const InstRet *Inst) = 0;
230 virtual void lowerSelect(const InstSelect *Inst) = 0;
231 virtual void lowerStore(const InstStore *Inst) = 0;
232 virtual void lowerSwitch(const InstSwitch *Inst) = 0;
233 virtual void lowerUnreachable(const InstUnreachable *Inst) = 0;
234
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700235 virtual void doAddressOptLoad() {}
236 virtual void doAddressOptStore() {}
Matt Walac3302742014-08-15 16:21:56 -0700237 virtual void randomlyInsertNop(float Probability) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700238 // This gives the target an opportunity to post-process the lowered
239 // expansion before returning. The primary intention is to do some
240 // Register Manager activity as necessary, specifically to eagerly
241 // allocate registers based on affinity and other factors. The
242 // simplest lowering does nothing here and leaves it all to a
243 // subsequent global register allocation pass.
244 virtual void postLower() {}
245
246 Cfg *Func;
247 GlobalContext *Ctx;
248 bool HasComputedFrame;
Jan Voung44d53e12014-09-11 19:18:03 -0700249 bool CallsReturnsTwice;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700250 // StackAdjustment keeps track of the current stack offset from its
251 // natural location, as arguments are pushed for a function call.
252 int32_t StackAdjustment;
253 LoweringContext Context;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700254};
255
Jim Stichnothde4ca712014-06-29 08:13:48 -0700256// TargetGlobalInitLowering is used for "lowering" global
257// initializers. It is separated out from TargetLowering because it
258// does not require a Cfg.
259class TargetGlobalInitLowering {
Jim Stichnoth7b451a92014-10-15 14:39:23 -0700260 TargetGlobalInitLowering(const TargetGlobalInitLowering &) = delete;
261 TargetGlobalInitLowering &operator=(const TargetGlobalInitLowering &) =
262 delete;
263
Jim Stichnothde4ca712014-06-29 08:13:48 -0700264public:
265 static TargetGlobalInitLowering *createLowering(TargetArch Target,
266 GlobalContext *Ctx);
Jan Voung839c4ce2014-07-28 15:19:43 -0700267 virtual ~TargetGlobalInitLowering();
268
Karl Schimpf9d98d792014-10-13 15:01:08 -0700269 virtual void lower(const VariableDeclaration &Var) = 0;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700270
271protected:
272 TargetGlobalInitLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
273 GlobalContext *Ctx;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700274};
275
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700276} // end of namespace Ice
277
278#endif // SUBZERO_SRC_ICETARGETLOWERING_H