blob: 6e172a2952b444f0260e4c33b87224e3a9cb0983 [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:
42 LoweringContext() : Node(NULL) {}
43 ~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;
72 // Cur points to the current instruction being considered. It is
73 // guaranteed to point to a non-deleted instruction, or to be End.
74 InstList::iterator Cur;
75 // Next doubles as a pointer to the next valid instruction (if any),
76 // and the new-instruction insertion point. It is also updated for
77 // the caller in case the lowering consumes more than one high-level
78 // instruction. It is guaranteed to point to a non-deleted
79 // instruction after Cur, or to be End. TODO: Consider separating
80 // the notion of "next valid instruction" and "new instruction
81 // insertion point", to avoid confusion when previously-deleted
82 // instructions come between the two points.
83 InstList::iterator Next;
Jan Vounge6e497d2014-07-30 10:06:03 -070084 // Begin is a copy of Insts.begin(), used if iterators are moved backward.
85 InstList::iterator Begin;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070086 // End is a copy of Insts.end(), used if Next needs to be advanced.
87 InstList::iterator End;
88
Jan Voungc820ddf2014-07-29 14:38:51 -070089 void skipDeleted(InstList::iterator &I) const;
Jan Vounge6e497d2014-07-30 10:06:03 -070090 void advanceForward(InstList::iterator &I) const;
91 void advanceBackward(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
148 // scratch registers as killed by a call.
149 virtual Variable *getPhysicalRegister(SizeT RegNum) = 0;
150 // Returns a printable name for the register.
151 virtual IceString getRegName(SizeT RegNum, Type Ty) const = 0;
152
153 virtual bool hasFramePointer() const { return false; }
154 virtual SizeT getFrameOrStackReg() const = 0;
Matt Walad4799f42014-08-14 14:24:12 -0700155 virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0;
Jan Voungb17f61d2014-08-28 16:00:53 -0700156 virtual SizeT getBundleAlignLog2Bytes() const = 0;
Jim Stichnothf44f3712014-10-01 14:05:51 -0700157 virtual llvm::ArrayRef<AsmCodeByte> getNonExecBundlePadding() const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700158 bool hasComputedFrame() const { return HasComputedFrame; }
Matt Walac3302742014-08-15 16:21:56 -0700159 bool shouldDoNopInsertion() const;
Jan Voung44d53e12014-09-11 19:18:03 -0700160 // Returns true if this function calls a function that has the
161 // "returns twice" attribute.
162 bool callsReturnsTwice() const { return CallsReturnsTwice; }
163 void setCallsReturnsTwice(bool RetTwice) {
164 CallsReturnsTwice = RetTwice;
165 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700166 int32_t getStackAdjustment() const { return StackAdjustment; }
167 void updateStackAdjustment(int32_t Offset) { StackAdjustment += Offset; }
168 void resetStackAdjustment() { StackAdjustment = 0; }
169 LoweringContext &getContext() { return Context; }
170
171 enum RegSet {
172 RegSet_None = 0,
173 RegSet_CallerSave = 1 << 0,
174 RegSet_CalleeSave = 1 << 1,
175 RegSet_StackPointer = 1 << 2,
176 RegSet_FramePointer = 1 << 3,
177 RegSet_All = ~RegSet_None
178 };
179 typedef uint32_t RegSetMask;
180
181 virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include,
182 RegSetMask Exclude) const = 0;
183 virtual const llvm::SmallBitVector &getRegisterSetForType(Type Ty) const = 0;
184 void regAlloc();
185
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700186 virtual void emitVariable(const Variable *Var) const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700187
Matt Wala45a06232014-07-09 16:33:22 -0700188 // Performs target-specific argument lowering.
189 virtual void lowerArguments() = 0;
190
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700191 virtual void addProlog(CfgNode *Node) = 0;
192 virtual void addEpilog(CfgNode *Node) = 0;
193
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700194 virtual void emitConstants() const = 0;
195
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700196 virtual ~TargetLowering() {}
197
198protected:
199 TargetLowering(Cfg *Func)
200 : Func(Func), Ctx(Func->getContext()), HasComputedFrame(false),
Jan Voung44d53e12014-09-11 19:18:03 -0700201 CallsReturnsTwice(false), StackAdjustment(0) {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700202 virtual void lowerAlloca(const InstAlloca *Inst) = 0;
203 virtual void lowerArithmetic(const InstArithmetic *Inst) = 0;
204 virtual void lowerAssign(const InstAssign *Inst) = 0;
205 virtual void lowerBr(const InstBr *Inst) = 0;
206 virtual void lowerCall(const InstCall *Inst) = 0;
207 virtual void lowerCast(const InstCast *Inst) = 0;
208 virtual void lowerFcmp(const InstFcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700209 virtual void lowerExtractElement(const InstExtractElement *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700210 virtual void lowerIcmp(const InstIcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700211 virtual void lowerInsertElement(const InstInsertElement *Inst) = 0;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700212 virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700213 virtual void lowerLoad(const InstLoad *Inst) = 0;
214 virtual void lowerPhi(const InstPhi *Inst) = 0;
215 virtual void lowerRet(const InstRet *Inst) = 0;
216 virtual void lowerSelect(const InstSelect *Inst) = 0;
217 virtual void lowerStore(const InstStore *Inst) = 0;
218 virtual void lowerSwitch(const InstSwitch *Inst) = 0;
219 virtual void lowerUnreachable(const InstUnreachable *Inst) = 0;
220
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700221 virtual void doAddressOptLoad() {}
222 virtual void doAddressOptStore() {}
Matt Walac3302742014-08-15 16:21:56 -0700223 virtual void randomlyInsertNop(float Probability) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700224 // This gives the target an opportunity to post-process the lowered
225 // expansion before returning. The primary intention is to do some
226 // Register Manager activity as necessary, specifically to eagerly
227 // allocate registers based on affinity and other factors. The
228 // simplest lowering does nothing here and leaves it all to a
229 // subsequent global register allocation pass.
230 virtual void postLower() {}
231
232 Cfg *Func;
233 GlobalContext *Ctx;
234 bool HasComputedFrame;
Jan Voung44d53e12014-09-11 19:18:03 -0700235 bool CallsReturnsTwice;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700236 // StackAdjustment keeps track of the current stack offset from its
237 // natural location, as arguments are pushed for a function call.
238 int32_t StackAdjustment;
239 LoweringContext Context;
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 {
Jim Stichnoth7b451a92014-10-15 14:39:23 -0700246 TargetGlobalInitLowering(const TargetGlobalInitLowering &) = delete;
247 TargetGlobalInitLowering &operator=(const TargetGlobalInitLowering &) =
248 delete;
249
Jim Stichnothde4ca712014-06-29 08:13:48 -0700250public:
251 static TargetGlobalInitLowering *createLowering(TargetArch Target,
252 GlobalContext *Ctx);
Jan Voung839c4ce2014-07-28 15:19:43 -0700253 virtual ~TargetGlobalInitLowering();
254
Karl Schimpf9d98d792014-10-13 15:01:08 -0700255 virtual void lower(const VariableDeclaration &Var) = 0;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700256
257protected:
258 TargetGlobalInitLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
259 GlobalContext *Ctx;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700260};
261
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700262} // end of namespace Ice
263
264#endif // SUBZERO_SRC_ICETARGETLOWERING_H