blob: 2ad2685f184f21259829a885b8e5ad7ec6106ae0 [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//
Jim Stichnothbbca7542015-02-11 16:08:31 -080010// This file declares the TargetLowering, LoweringContext, and
11// TargetDataLowering classes. TargetLowering is an abstract class
12// used to drive the translation/lowering process. LoweringContext
13// maintains a context for lowering each instruction, offering
14// conveniences such as iterating over non-deleted instructions.
15// TargetDataLowering is an abstract class used to drive the
16// lowering/emission of global initializers, external global
17// declarations, and internal constant pools.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070018//
19//===----------------------------------------------------------------------===//
20
21#ifndef SUBZERO_SRC_ICETARGETLOWERING_H
22#define SUBZERO_SRC_ICETARGETLOWERING_H
23
24#include "IceDefs.h"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070025#include "IceInst.h" // for the names of the Inst subtypes
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070026#include "IceTypes.h"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070027
28namespace Ice {
29
30// 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 {
Jim Stichnoth7b451a92014-10-15 14:39:23 -070037 LoweringContext(const LoweringContext &) = delete;
38 LoweringContext &operator=(const LoweringContext &) = delete;
39
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070040public:
Jim Stichnothae953202014-12-20 06:17:49 -080041 LoweringContext() : Node(nullptr), LastInserted(nullptr) {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070042 ~LoweringContext() {}
43 void init(CfgNode *Node);
44 Inst *getNextInst() const {
45 if (Next == End)
Jim Stichnothae953202014-12-20 06:17:49 -080046 return nullptr;
Jim Stichnoth607e9f02014-11-06 13:32:05 -080047 return Next;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070048 }
Jan Voungc820ddf2014-07-29 14:38:51 -070049 Inst *getNextInst(InstList::iterator &Iter) const {
Jan Vounge6e497d2014-07-30 10:06:03 -070050 advanceForward(Iter);
Jan Voungc820ddf2014-07-29 14:38:51 -070051 if (Iter == End)
Jim Stichnothae953202014-12-20 06:17:49 -080052 return nullptr;
Jim Stichnoth607e9f02014-11-06 13:32:05 -080053 return Iter;
Jan Voungc820ddf2014-07-29 14:38:51 -070054 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070055 CfgNode *getNode() const { return Node; }
56 bool atEnd() const { return Cur == End; }
57 InstList::iterator getCur() const { return Cur; }
Jim Stichnoth5d2fa0c2014-12-01 09:30:55 -080058 InstList::iterator getNext() const { return Next; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070059 InstList::iterator getEnd() const { return End; }
60 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 Stichnoth336f6c42014-10-30 15:01:31 -070064 void rewind();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070065 void setInsertPoint(const InstList::iterator &Position) { Next = Position; }
66
67private:
68 // Node is the argument to Inst::updateVars().
69 CfgNode *Node;
Jim Stichnoth98712a32014-10-24 10:59:02 -070070 Inst *LastInserted;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070071 // Cur points to the current instruction being considered. It is
72 // guaranteed to point to a non-deleted instruction, or to be End.
73 InstList::iterator Cur;
74 // Next doubles as a pointer to the next valid instruction (if any),
75 // and the new-instruction insertion point. It is also updated for
76 // the caller in case the lowering consumes more than one high-level
77 // instruction. It is guaranteed to point to a non-deleted
78 // instruction after Cur, or to be End. TODO: Consider separating
79 // the notion of "next valid instruction" and "new instruction
80 // insertion point", to avoid confusion when previously-deleted
81 // instructions come between the two points.
82 InstList::iterator Next;
Jan Vounge6e497d2014-07-30 10:06:03 -070083 // Begin is a copy of Insts.begin(), used if iterators are moved backward.
84 InstList::iterator Begin;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070085 // End is a copy of Insts.end(), used if Next needs to be advanced.
86 InstList::iterator End;
87
Jan Voungc820ddf2014-07-29 14:38:51 -070088 void skipDeleted(InstList::iterator &I) const;
Jan Vounge6e497d2014-07-30 10:06:03 -070089 void advanceForward(InstList::iterator &I) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070090};
91
92class TargetLowering {
Jim Stichnoth7b451a92014-10-15 14:39:23 -070093 TargetLowering(const TargetLowering &) = delete;
94 TargetLowering &operator=(const TargetLowering &) = delete;
95
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070096public:
97 static TargetLowering *createLowering(TargetArch Target, Cfg *Func);
Jan Voungec270732015-01-12 17:00:22 -080098 static std::unique_ptr<Assembler> createAssembler(TargetArch Target,
99 Cfg *Func);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700100 void translate() {
101 switch (Ctx->getOptLevel()) {
102 case Opt_m1:
103 translateOm1();
104 break;
105 case Opt_0:
106 translateO0();
107 break;
108 case Opt_1:
109 translateO1();
110 break;
111 case Opt_2:
112 translateO2();
113 break;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700114 }
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 Stichnoth336f6c42014-10-30 15:01:31 -0700133 // Lowers a single non-Phi instruction.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700134 void lower();
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700135 // Does preliminary lowering of the set of Phi instructions in the
136 // current node. The main intention is to do what's needed to keep
137 // the unlowered Phi instructions consistent with the lowered
138 // non-Phi instructions, e.g. to lower 64-bit operands on a 32-bit
139 // target.
140 virtual void prelowerPhis() {}
141 // Lowers a list of "parallel" assignment instructions representing
142 // a topological sort of the Phi instructions.
143 virtual void lowerPhiAssignments(CfgNode *Node,
144 const AssignList &Assignments) = 0;
Jim Stichnothff9c7062014-09-18 04:50:49 -0700145 // Tries to do branch optimization on a single instruction. Returns
146 // true if some optimization was done.
147 virtual bool doBranchOpt(Inst * /*I*/, const CfgNode * /*NextNode*/) {
148 return false;
149 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700150
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700151 virtual SizeT getNumRegisters() const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700152 // Returns a variable pre-colored to the specified physical
153 // register. This is generally used to get very direct access to
154 // the register such as in the prolog or epilog or for marking
Jim Stichnoth98712a32014-10-24 10:59:02 -0700155 // scratch registers as killed by a call. If a Type is not
156 // provided, a target-specific default type is used.
157 virtual Variable *getPhysicalRegister(SizeT RegNum,
158 Type Ty = IceType_void) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700159 // Returns a printable name for the register.
160 virtual IceString getRegName(SizeT RegNum, Type Ty) const = 0;
161
162 virtual bool hasFramePointer() const { return false; }
163 virtual SizeT getFrameOrStackReg() const = 0;
Matt Walad4799f42014-08-14 14:24:12 -0700164 virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700165 bool hasComputedFrame() const { return HasComputedFrame; }
Matt Walac3302742014-08-15 16:21:56 -0700166 bool shouldDoNopInsertion() const;
Jan Voung44d53e12014-09-11 19:18:03 -0700167 // Returns true if this function calls a function that has the
168 // "returns twice" attribute.
169 bool callsReturnsTwice() const { return CallsReturnsTwice; }
Jim Stichnothdd842db2015-01-27 12:53:53 -0800170 void setCallsReturnsTwice(bool RetTwice) { CallsReturnsTwice = RetTwice; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700171 int32_t getStackAdjustment() const { return StackAdjustment; }
172 void updateStackAdjustment(int32_t Offset) { StackAdjustment += Offset; }
173 void resetStackAdjustment() { StackAdjustment = 0; }
174 LoweringContext &getContext() { return Context; }
175
176 enum RegSet {
177 RegSet_None = 0,
178 RegSet_CallerSave = 1 << 0,
179 RegSet_CalleeSave = 1 << 1,
180 RegSet_StackPointer = 1 << 2,
181 RegSet_FramePointer = 1 << 3,
182 RegSet_All = ~RegSet_None
183 };
184 typedef uint32_t RegSetMask;
185
186 virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include,
187 RegSetMask Exclude) const = 0;
188 virtual const llvm::SmallBitVector &getRegisterSetForType(Type Ty) const = 0;
Jim Stichnoth70d0a052014-11-14 15:53:46 -0800189 void regAlloc(RegAllocKind Kind);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700190
Jim Stichnothe6d24782014-12-19 05:42:24 -0800191 virtual void makeRandomRegisterPermutation(
192 llvm::SmallVectorImpl<int32_t> &Permutation,
193 const llvm::SmallBitVector &ExcludeRegisters) const = 0;
194
Jim Stichnoth9738a9e2015-02-23 16:39:06 -0800195 // Save/restore any mutable state for the situation where code
196 // emission needs multiple passes, such as sandboxing or relaxation.
197 // Subclasses may provide their own implementation, but should be
198 // sure to also call the parent class's methods.
199 virtual void snapshotEmitState() {
200 SnapshotStackAdjustment = StackAdjustment;
201 }
202 virtual void rollbackEmitState() {
203 StackAdjustment = SnapshotStackAdjustment;
204 }
205
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700206 virtual void emitVariable(const Variable *Var) const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700207
Matt Wala45a06232014-07-09 16:33:22 -0700208 // Performs target-specific argument lowering.
209 virtual void lowerArguments() = 0;
210
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700211 virtual void addProlog(CfgNode *Node) = 0;
212 virtual void addEpilog(CfgNode *Node) = 0;
213
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700214 virtual ~TargetLowering() {}
215
216protected:
Jim Stichnothe6d24782014-12-19 05:42:24 -0800217 TargetLowering(Cfg *Func);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700218 virtual void lowerAlloca(const InstAlloca *Inst) = 0;
219 virtual void lowerArithmetic(const InstArithmetic *Inst) = 0;
220 virtual void lowerAssign(const InstAssign *Inst) = 0;
221 virtual void lowerBr(const InstBr *Inst) = 0;
222 virtual void lowerCall(const InstCall *Inst) = 0;
223 virtual void lowerCast(const InstCast *Inst) = 0;
224 virtual void lowerFcmp(const InstFcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700225 virtual void lowerExtractElement(const InstExtractElement *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700226 virtual void lowerIcmp(const InstIcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700227 virtual void lowerInsertElement(const InstInsertElement *Inst) = 0;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700228 virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700229 virtual void lowerLoad(const InstLoad *Inst) = 0;
230 virtual void lowerPhi(const InstPhi *Inst) = 0;
231 virtual void lowerRet(const InstRet *Inst) = 0;
232 virtual void lowerSelect(const InstSelect *Inst) = 0;
233 virtual void lowerStore(const InstStore *Inst) = 0;
234 virtual void lowerSwitch(const InstSwitch *Inst) = 0;
235 virtual void lowerUnreachable(const InstUnreachable *Inst) = 0;
236
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700237 virtual void doAddressOptLoad() {}
238 virtual void doAddressOptStore() {}
Matt Walac3302742014-08-15 16:21:56 -0700239 virtual void randomlyInsertNop(float Probability) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700240 // This gives the target an opportunity to post-process the lowered
Jim Stichnoth70d0a052014-11-14 15:53:46 -0800241 // expansion before returning.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700242 virtual void postLower() {}
243
244 Cfg *Func;
245 GlobalContext *Ctx;
Jim Stichnothe6d24782014-12-19 05:42:24 -0800246 const bool RandomizeRegisterAllocation;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700247 bool HasComputedFrame;
Jan Voung44d53e12014-09-11 19:18:03 -0700248 bool CallsReturnsTwice;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700249 // StackAdjustment keeps track of the current stack offset from its
250 // natural location, as arguments are pushed for a function call.
251 int32_t StackAdjustment;
252 LoweringContext Context;
Jim Stichnoth9738a9e2015-02-23 16:39:06 -0800253
254private:
255 int32_t SnapshotStackAdjustment;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700256};
257
Jan Voung72984d82015-01-29 14:42:38 -0800258// TargetDataLowering is used for "lowering" data including initializers
259// for global variables, and the internal constant pools. It is separated
260// out from TargetLowering because it does not require a Cfg.
261class TargetDataLowering {
262 TargetDataLowering() = delete;
263 TargetDataLowering(const TargetDataLowering &) = delete;
264 TargetDataLowering &operator=(const TargetDataLowering &) = delete;
Jim Stichnoth7b451a92014-10-15 14:39:23 -0700265
Jim Stichnothde4ca712014-06-29 08:13:48 -0700266public:
Jim Stichnothbbca7542015-02-11 16:08:31 -0800267 static std::unique_ptr<TargetDataLowering> createLowering(GlobalContext *Ctx);
Jan Voung72984d82015-01-29 14:42:38 -0800268 virtual ~TargetDataLowering();
Jan Voung839c4ce2014-07-28 15:19:43 -0700269
Jim Stichnothbbca7542015-02-11 16:08:31 -0800270 virtual void
271 lowerGlobals(std::unique_ptr<VariableDeclarationList> Vars) const = 0;
272 virtual void lowerConstants() const = 0;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700273
274protected:
Jan Voung72984d82015-01-29 14:42:38 -0800275 TargetDataLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
Jim Stichnothde4ca712014-06-29 08:13:48 -0700276 GlobalContext *Ctx;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700277};
278
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700279} // end of namespace Ice
280
281#endif // SUBZERO_SRC_ICETARGETLOWERING_H