blob: e46399d311e6aa73ffc7d8e658c772df3b194b4a [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 Stichnoth5bc2b1d2014-05-22 13:38:48 -070067 void setInsertPoint(const InstList::iterator &Position) { Next = Position; }
68
69private:
70 // Node is the argument to Inst::updateVars().
71 CfgNode *Node;
Jim Stichnoth98712a32014-10-24 10:59:02 -070072 Inst *LastInserted;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070073 // Cur points to the current instruction being considered. It is
74 // guaranteed to point to a non-deleted instruction, or to be End.
75 InstList::iterator Cur;
76 // Next doubles as a pointer to the next valid instruction (if any),
77 // and the new-instruction insertion point. It is also updated for
78 // the caller in case the lowering consumes more than one high-level
79 // instruction. It is guaranteed to point to a non-deleted
80 // instruction after Cur, or to be End. TODO: Consider separating
81 // the notion of "next valid instruction" and "new instruction
82 // insertion point", to avoid confusion when previously-deleted
83 // instructions come between the two points.
84 InstList::iterator Next;
Jan Vounge6e497d2014-07-30 10:06:03 -070085 // Begin is a copy of Insts.begin(), used if iterators are moved backward.
86 InstList::iterator Begin;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070087 // End is a copy of Insts.end(), used if Next needs to be advanced.
88 InstList::iterator End;
89
Jan Voungc820ddf2014-07-29 14:38:51 -070090 void skipDeleted(InstList::iterator &I) const;
Jan Vounge6e497d2014-07-30 10:06:03 -070091 void advanceForward(InstList::iterator &I) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070092};
93
94class TargetLowering {
Jim Stichnoth7b451a92014-10-15 14:39:23 -070095 TargetLowering(const TargetLowering &) = delete;
96 TargetLowering &operator=(const TargetLowering &) = delete;
97
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070098public:
99 static TargetLowering *createLowering(TargetArch Target, Cfg *Func);
Jan Voung8acded02014-09-22 18:02:25 -0700100 static Assembler *createAssembler(TargetArch Target, Cfg *Func);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700101 void translate() {
102 switch (Ctx->getOptLevel()) {
103 case Opt_m1:
104 translateOm1();
105 break;
106 case Opt_0:
107 translateO0();
108 break;
109 case Opt_1:
110 translateO1();
111 break;
112 case Opt_2:
113 translateO2();
114 break;
115 default:
116 Func->setError("Target doesn't specify lowering steps.");
117 break;
118 }
119 }
120 virtual void translateOm1() {
121 Func->setError("Target doesn't specify Om1 lowering steps.");
122 }
123 virtual void translateO0() {
124 Func->setError("Target doesn't specify O0 lowering steps.");
125 }
126 virtual void translateO1() {
127 Func->setError("Target doesn't specify O1 lowering steps.");
128 }
129 virtual void translateO2() {
130 Func->setError("Target doesn't specify O2 lowering steps.");
131 }
132
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700133 // Tries to do address mode optimization on a single instruction.
134 void doAddressOpt();
Matt Walac3302742014-08-15 16:21:56 -0700135 // Randomly insert NOPs.
136 void doNopInsertion();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700137 // Lowers a single instruction.
138 void lower();
Jim Stichnothff9c7062014-09-18 04:50:49 -0700139 // Tries to do branch optimization on a single instruction. Returns
140 // true if some optimization was done.
141 virtual bool doBranchOpt(Inst * /*I*/, const CfgNode * /*NextNode*/) {
142 return false;
143 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700144
145 // Returns a variable pre-colored to the specified physical
146 // register. This is generally used to get very direct access to
147 // the register such as in the prolog or epilog or for marking
Jim Stichnoth98712a32014-10-24 10:59:02 -0700148 // scratch registers as killed by a call. If a Type is not
149 // provided, a target-specific default type is used.
150 virtual Variable *getPhysicalRegister(SizeT RegNum,
151 Type Ty = IceType_void) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700152 // Returns a printable name for the register.
153 virtual IceString getRegName(SizeT RegNum, Type Ty) const = 0;
154
155 virtual bool hasFramePointer() const { return false; }
156 virtual SizeT getFrameOrStackReg() const = 0;
Matt Walad4799f42014-08-14 14:24:12 -0700157 virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0;
Jan Voungb17f61d2014-08-28 16:00:53 -0700158 virtual SizeT getBundleAlignLog2Bytes() const = 0;
Jim Stichnothf44f3712014-10-01 14:05:51 -0700159 virtual llvm::ArrayRef<AsmCodeByte> getNonExecBundlePadding() const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700160 bool hasComputedFrame() const { return HasComputedFrame; }
Matt Walac3302742014-08-15 16:21:56 -0700161 bool shouldDoNopInsertion() const;
Jan Voung44d53e12014-09-11 19:18:03 -0700162 // Returns true if this function calls a function that has the
163 // "returns twice" attribute.
164 bool callsReturnsTwice() const { return CallsReturnsTwice; }
165 void setCallsReturnsTwice(bool RetTwice) {
166 CallsReturnsTwice = RetTwice;
167 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700168 int32_t getStackAdjustment() const { return StackAdjustment; }
169 void updateStackAdjustment(int32_t Offset) { StackAdjustment += Offset; }
170 void resetStackAdjustment() { StackAdjustment = 0; }
171 LoweringContext &getContext() { return Context; }
172
173 enum RegSet {
174 RegSet_None = 0,
175 RegSet_CallerSave = 1 << 0,
176 RegSet_CalleeSave = 1 << 1,
177 RegSet_StackPointer = 1 << 2,
178 RegSet_FramePointer = 1 << 3,
179 RegSet_All = ~RegSet_None
180 };
181 typedef uint32_t RegSetMask;
182
183 virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include,
184 RegSetMask Exclude) const = 0;
185 virtual const llvm::SmallBitVector &getRegisterSetForType(Type Ty) const = 0;
186 void regAlloc();
187
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700188 virtual void emitVariable(const Variable *Var) const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700189
Matt Wala45a06232014-07-09 16:33:22 -0700190 // Performs target-specific argument lowering.
191 virtual void lowerArguments() = 0;
192
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700193 virtual void addProlog(CfgNode *Node) = 0;
194 virtual void addEpilog(CfgNode *Node) = 0;
195
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700196 virtual void emitConstants() const = 0;
197
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700198 virtual ~TargetLowering() {}
199
200protected:
201 TargetLowering(Cfg *Func)
202 : Func(Func), Ctx(Func->getContext()), HasComputedFrame(false),
Jan Voung44d53e12014-09-11 19:18:03 -0700203 CallsReturnsTwice(false), StackAdjustment(0) {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700204 virtual void lowerAlloca(const InstAlloca *Inst) = 0;
205 virtual void lowerArithmetic(const InstArithmetic *Inst) = 0;
206 virtual void lowerAssign(const InstAssign *Inst) = 0;
207 virtual void lowerBr(const InstBr *Inst) = 0;
208 virtual void lowerCall(const InstCall *Inst) = 0;
209 virtual void lowerCast(const InstCast *Inst) = 0;
210 virtual void lowerFcmp(const InstFcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700211 virtual void lowerExtractElement(const InstExtractElement *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700212 virtual void lowerIcmp(const InstIcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700213 virtual void lowerInsertElement(const InstInsertElement *Inst) = 0;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700214 virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700215 virtual void lowerLoad(const InstLoad *Inst) = 0;
216 virtual void lowerPhi(const InstPhi *Inst) = 0;
217 virtual void lowerRet(const InstRet *Inst) = 0;
218 virtual void lowerSelect(const InstSelect *Inst) = 0;
219 virtual void lowerStore(const InstStore *Inst) = 0;
220 virtual void lowerSwitch(const InstSwitch *Inst) = 0;
221 virtual void lowerUnreachable(const InstUnreachable *Inst) = 0;
222
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700223 virtual void doAddressOptLoad() {}
224 virtual void doAddressOptStore() {}
Matt Walac3302742014-08-15 16:21:56 -0700225 virtual void randomlyInsertNop(float Probability) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700226 // This gives the target an opportunity to post-process the lowered
227 // expansion before returning. The primary intention is to do some
228 // Register Manager activity as necessary, specifically to eagerly
229 // allocate registers based on affinity and other factors. The
230 // simplest lowering does nothing here and leaves it all to a
231 // subsequent global register allocation pass.
232 virtual void postLower() {}
233
234 Cfg *Func;
235 GlobalContext *Ctx;
236 bool HasComputedFrame;
Jan Voung44d53e12014-09-11 19:18:03 -0700237 bool CallsReturnsTwice;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700238 // StackAdjustment keeps track of the current stack offset from its
239 // natural location, as arguments are pushed for a function call.
240 int32_t StackAdjustment;
241 LoweringContext Context;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700242};
243
Jim Stichnothde4ca712014-06-29 08:13:48 -0700244// TargetGlobalInitLowering is used for "lowering" global
245// initializers. It is separated out from TargetLowering because it
246// does not require a Cfg.
247class TargetGlobalInitLowering {
Jim Stichnoth7b451a92014-10-15 14:39:23 -0700248 TargetGlobalInitLowering(const TargetGlobalInitLowering &) = delete;
249 TargetGlobalInitLowering &operator=(const TargetGlobalInitLowering &) =
250 delete;
251
Jim Stichnothde4ca712014-06-29 08:13:48 -0700252public:
253 static TargetGlobalInitLowering *createLowering(TargetArch Target,
254 GlobalContext *Ctx);
Jan Voung839c4ce2014-07-28 15:19:43 -0700255 virtual ~TargetGlobalInitLowering();
256
Karl Schimpf9d98d792014-10-13 15:01:08 -0700257 virtual void lower(const VariableDeclaration &Var) = 0;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700258
259protected:
260 TargetGlobalInitLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
261 GlobalContext *Ctx;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700262};
263
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700264} // end of namespace Ice
265
266#endif // SUBZERO_SRC_ICETARGETLOWERING_H