blob: 41108f8b44e2a52abda1e4a2122981d8b6245a63 [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//===----------------------------------------------------------------------===//
Andrew Scull9612d322015-07-06 14:53:25 -07009///
10/// \file
Jim Stichnoth92a6e5b2015-12-02 16:52:44 -080011/// \brief Declares the TargetLowering, LoweringContext, and TargetDataLowering
12/// classes.
13///
14/// TargetLowering is an abstract class used to drive the translation/lowering
15/// process. LoweringContext maintains a context for lowering each instruction,
16/// offering conveniences such as iterating over non-deleted instructions.
17/// TargetDataLowering is an abstract class used to drive the lowering/emission
18/// of global initializers, external global declarations, and internal constant
19/// pools.
Andrew Scull9612d322015-07-06 14:53:25 -070020///
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070021//===----------------------------------------------------------------------===//
22
23#ifndef SUBZERO_SRC_ICETARGETLOWERING_H
24#define SUBZERO_SRC_ICETARGETLOWERING_H
25
John Portoe82b5602016-02-24 15:58:55 -080026#include "IceBitVector.h"
27#include "IceCfgNode.h"
Manasij Mukherjee7cd926d2016-08-04 12:33:23 -070028#include "IceDefs.h"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070029#include "IceInst.h" // for the names of the Inst subtypes
Jan Voung76bb0be2015-05-14 09:26:19 -070030#include "IceOperand.h"
Manasij Mukherjee7cd926d2016-08-04 12:33:23 -070031#include "IceRegAlloc.h"
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070032#include "IceTypes.h"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070033
John Porto1d937a82015-12-17 06:19:34 -080034#include <utility>
35
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070036namespace Ice {
37
Karl Schimpfc5abdc12015-10-09 13:29:13 -070038// UnimplementedError is defined as a macro so that we can get actual line
39// numbers.
40#define UnimplementedError(Flags) \
41 do { \
42 if (!static_cast<const ClFlags &>(Flags).getSkipUnimplemented()) { \
43 /* Use llvm_unreachable instead of report_fatal_error, which gives \
44 better stack traces. */ \
45 llvm_unreachable("Not yet implemented"); \
46 abort(); \
47 } \
48 } while (0)
49
Jim Stichnoth91c773e2016-01-19 09:52:22 -080050// UnimplementedLoweringError is similar in style to UnimplementedError. Given
51// a TargetLowering object pointer and an Inst pointer, it adds appropriate
52// FakeDef and FakeUse instructions to try maintain liveness consistency.
53#define UnimplementedLoweringError(Target, Instr) \
54 do { \
Karl Schimpfd4699942016-04-02 09:55:31 -070055 if (getFlags().getSkipUnimplemented()) { \
Jim Stichnoth91c773e2016-01-19 09:52:22 -080056 (Target)->addFakeDefUses(Instr); \
57 } else { \
58 /* Use llvm_unreachable instead of report_fatal_error, which gives \
59 better stack traces. */ \
Eric Holke37076a2016-01-27 14:06:35 -080060 llvm_unreachable( \
Jim Stichnoth467ffe52016-03-29 15:01:06 -070061 (std::string("Not yet implemented: ") + Instr->getInstName()) \
62 .c_str()); \
Jim Stichnoth91c773e2016-01-19 09:52:22 -080063 abort(); \
64 } \
65 } while (0)
66
Andrew Scull57e12682015-09-16 11:30:19 -070067/// LoweringContext makes it easy to iterate through non-deleted instructions in
68/// a node, and insert new (lowered) instructions at the current point. Along
69/// with the instruction list container and associated iterators, it holds the
70/// current node, which is needed when inserting new instructions in order to
71/// track whether variables are used as single-block or multi-block.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070072class LoweringContext {
Jim Stichnoth7b451a92014-10-15 14:39:23 -070073 LoweringContext(const LoweringContext &) = delete;
74 LoweringContext &operator=(const LoweringContext &) = delete;
75
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070076public:
Jim Stichnotheafb56c2015-06-22 10:35:22 -070077 LoweringContext() = default;
78 ~LoweringContext() = default;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070079 void init(CfgNode *Node);
80 Inst *getNextInst() const {
81 if (Next == End)
Jim Stichnothae953202014-12-20 06:17:49 -080082 return nullptr;
Jim Stichnothf5fdd232016-05-09 12:24:36 -070083 return iteratorToInst(Next);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070084 }
Jan Voungc820ddf2014-07-29 14:38:51 -070085 Inst *getNextInst(InstList::iterator &Iter) const {
Jan Vounge6e497d2014-07-30 10:06:03 -070086 advanceForward(Iter);
Jan Voungc820ddf2014-07-29 14:38:51 -070087 if (Iter == End)
Jim Stichnothae953202014-12-20 06:17:49 -080088 return nullptr;
Jim Stichnothf5fdd232016-05-09 12:24:36 -070089 return iteratorToInst(Iter);
Jan Voungc820ddf2014-07-29 14:38:51 -070090 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070091 CfgNode *getNode() const { return Node; }
92 bool atEnd() const { return Cur == End; }
93 InstList::iterator getCur() const { return Cur; }
Jim Stichnoth5d2fa0c2014-12-01 09:30:55 -080094 InstList::iterator getNext() const { return Next; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070095 InstList::iterator getEnd() const { return End; }
Jim Stichnoth8cfeb692016-02-05 09:50:02 -080096 void insert(Inst *Instr);
John Porto1d937a82015-12-17 06:19:34 -080097 template <typename Inst, typename... Args> Inst *insert(Args &&... A) {
98 auto *New = Inst::create(Node->getCfg(), std::forward<Args>(A)...);
99 insert(New);
100 return New;
101 }
Jan Vounge6e497d2014-07-30 10:06:03 -0700102 Inst *getLastInserted() const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700103 void advanceCur() { Cur = Next; }
Jan Vounge6e497d2014-07-30 10:06:03 -0700104 void advanceNext() { advanceForward(Next); }
Jim Stichnotha3f57b92015-07-30 12:46:04 -0700105 void setCur(InstList::iterator C) { Cur = C; }
106 void setNext(InstList::iterator N) { Next = N; }
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700107 void rewind();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700108 void setInsertPoint(const InstList::iterator &Position) { Next = Position; }
Jim Stichnoth318f4cd2015-10-01 21:02:37 -0700109 void availabilityReset();
110 void availabilityUpdate();
111 Variable *availabilityGet(Operand *Src) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700112
113private:
Andrew Scull9612d322015-07-06 14:53:25 -0700114 /// Node is the argument to Inst::updateVars().
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700115 CfgNode *Node = nullptr;
116 Inst *LastInserted = nullptr;
Andrew Scull57e12682015-09-16 11:30:19 -0700117 /// Cur points to the current instruction being considered. It is guaranteed
118 /// to point to a non-deleted instruction, or to be End.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700119 InstList::iterator Cur;
Andrew Scull57e12682015-09-16 11:30:19 -0700120 /// Next doubles as a pointer to the next valid instruction (if any), and the
121 /// new-instruction insertion point. It is also updated for the caller in case
122 /// the lowering consumes more than one high-level instruction. It is
123 /// guaranteed to point to a non-deleted instruction after Cur, or to be End.
124 // TODO: Consider separating the notion of "next valid instruction" and "new
125 // instruction insertion point", to avoid confusion when previously-deleted
126 // instructions come between the two points.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700127 InstList::iterator Next;
Andrew Scull9612d322015-07-06 14:53:25 -0700128 /// Begin is a copy of Insts.begin(), used if iterators are moved backward.
Jan Vounge6e497d2014-07-30 10:06:03 -0700129 InstList::iterator Begin;
Andrew Scull9612d322015-07-06 14:53:25 -0700130 /// End is a copy of Insts.end(), used if Next needs to be advanced.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700131 InstList::iterator End;
Jim Stichnoth318f4cd2015-10-01 21:02:37 -0700132 /// LastDest and LastSrc capture the parameters of the last "Dest=Src" simple
133 /// assignment inserted (provided Src is a variable). This is used for simple
134 /// availability analysis.
135 Variable *LastDest = nullptr;
136 Variable *LastSrc = nullptr;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700137
Jan Voungc820ddf2014-07-29 14:38:51 -0700138 void skipDeleted(InstList::iterator &I) const;
Jan Vounge6e497d2014-07-30 10:06:03 -0700139 void advanceForward(InstList::iterator &I) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700140};
141
Jan Voung28068ad2015-07-31 12:58:46 -0700142/// A helper class to advance the LoweringContext at each loop iteration.
143class PostIncrLoweringContext {
144 PostIncrLoweringContext() = delete;
145 PostIncrLoweringContext(const PostIncrLoweringContext &) = delete;
146 PostIncrLoweringContext &operator=(const PostIncrLoweringContext &) = delete;
147
148public:
149 explicit PostIncrLoweringContext(LoweringContext &Context)
150 : Context(Context) {}
151 ~PostIncrLoweringContext() {
152 Context.advanceCur();
153 Context.advanceNext();
154 }
155
156private:
157 LoweringContext &Context;
158};
159
John Porto53611e22015-12-30 07:30:10 -0800160/// TargetLowering is the base class for all backends in Subzero. In addition to
161/// implementing the abstract methods in this class, each concrete target must
162/// also implement a named constructor in its own namespace. For instance, for
163/// X8632 we have:
164///
165/// namespace X8632 {
166/// void createTargetLowering(Cfg *Func);
167/// }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700168class TargetLowering {
Jim Stichnothc6ead202015-02-24 09:30:30 -0800169 TargetLowering() = delete;
Jim Stichnoth7b451a92014-10-15 14:39:23 -0700170 TargetLowering(const TargetLowering &) = delete;
171 TargetLowering &operator=(const TargetLowering &) = delete;
172
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700173public:
Karl Schimpf5403f5d2016-01-15 11:07:46 -0800174 static void staticInit(GlobalContext *Ctx);
Jim Stichnoth8ff4b282016-01-04 15:39:06 -0800175 // Each target must define a public static method:
Karl Schimpf5403f5d2016-01-15 11:07:46 -0800176 // static void staticInit(GlobalContext *Ctx);
Jim Stichnoth467ffe52016-03-29 15:01:06 -0700177 static bool shouldBePooled(const class Constant *C);
Nicolas Capens32f9cce2016-10-19 01:24:27 -0400178 static Type getPointerType();
John Porto53611e22015-12-30 07:30:10 -0800179
180 static std::unique_ptr<TargetLowering> createLowering(TargetArch Target,
181 Cfg *Func);
182
183 virtual std::unique_ptr<Assembler> createAssembler() const = 0;
184
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700185 void translate() {
Jim Stichnothdd6dcfa2016-04-18 12:52:09 -0700186 switch (Func->getOptLevel()) {
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700187 case Opt_m1:
188 translateOm1();
189 break;
190 case Opt_0:
191 translateO0();
192 break;
193 case Opt_1:
194 translateO1();
195 break;
196 case Opt_2:
197 translateO2();
198 break;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700199 }
200 }
201 virtual void translateOm1() {
202 Func->setError("Target doesn't specify Om1 lowering steps.");
203 }
204 virtual void translateO0() {
205 Func->setError("Target doesn't specify O0 lowering steps.");
206 }
207 virtual void translateO1() {
208 Func->setError("Target doesn't specify O1 lowering steps.");
209 }
210 virtual void translateO2() {
211 Func->setError("Target doesn't specify O2 lowering steps.");
212 }
213
John Porto5e0a8a72015-11-20 13:50:36 -0800214 /// Generates calls to intrinsics for operations the Target can't handle.
215 void genTargetHelperCalls();
Andrew Scull9612d322015-07-06 14:53:25 -0700216 /// Tries to do address mode optimization on a single instruction.
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700217 void doAddressOpt();
Andrew Scull9612d322015-07-06 14:53:25 -0700218 /// Randomly insert NOPs.
Qining Luaee5fa82015-08-20 14:59:03 -0700219 void doNopInsertion(RandomNumberGenerator &RNG);
Andrew Scull9612d322015-07-06 14:53:25 -0700220 /// Lowers a single non-Phi instruction.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700221 void lower();
Jim Stichnotha3f57b92015-07-30 12:46:04 -0700222 /// Inserts and lowers a single high-level instruction at a specific insertion
223 /// point.
224 void lowerInst(CfgNode *Node, InstList::iterator Next, InstHighLevel *Instr);
Andrew Scull57e12682015-09-16 11:30:19 -0700225 /// Does preliminary lowering of the set of Phi instructions in the current
226 /// node. The main intention is to do what's needed to keep the unlowered Phi
227 /// instructions consistent with the lowered non-Phi instructions, e.g. to
228 /// lower 64-bit operands on a 32-bit target.
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700229 virtual void prelowerPhis() {}
Andrew Scull57e12682015-09-16 11:30:19 -0700230 /// Tries to do branch optimization on a single instruction. Returns true if
231 /// some optimization was done.
Jim Stichnothff9c7062014-09-18 04:50:49 -0700232 virtual bool doBranchOpt(Inst * /*I*/, const CfgNode * /*NextNode*/) {
233 return false;
234 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700235
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700236 virtual SizeT getNumRegisters() const = 0;
Andrew Scull57e12682015-09-16 11:30:19 -0700237 /// Returns a variable pre-colored to the specified physical register. This is
238 /// generally used to get very direct access to the register such as in the
239 /// prolog or epilog or for marking scratch registers as killed by a call. If
240 /// a Type is not provided, a target-specific default type is used.
Jim Stichnoth8aa39662016-02-10 11:20:30 -0800241 virtual Variable *getPhysicalRegister(RegNumT RegNum,
Jim Stichnoth98712a32014-10-24 10:59:02 -0700242 Type Ty = IceType_void) = 0;
Andrew Scull9612d322015-07-06 14:53:25 -0700243 /// Returns a printable name for the register.
Jim Stichnoth467ffe52016-03-29 15:01:06 -0700244 virtual const char *getRegName(RegNumT RegNum, Type Ty) const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700245
246 virtual bool hasFramePointer() const { return false; }
David Sehre39d0ca2015-11-06 11:25:41 -0800247 virtual void setHasFramePointer() = 0;
Jim Stichnoth8aa39662016-02-10 11:20:30 -0800248 virtual RegNumT getStackReg() const = 0;
249 virtual RegNumT getFrameReg() const = 0;
250 virtual RegNumT getFrameOrStackReg() const = 0;
Matt Walad4799f42014-08-14 14:24:12 -0700251 virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0;
David Sehre39d0ca2015-11-06 11:25:41 -0800252 virtual uint32_t getStackAlignment() const = 0;
David Sehr2f3b8ec2015-11-16 16:51:39 -0800253 virtual void reserveFixedAllocaArea(size_t Size, size_t Align) = 0;
254 virtual int32_t getFrameFixedAllocaOffset() const = 0;
John Porto614140e2015-11-23 11:43:13 -0800255 virtual uint32_t maxOutArgsSizeBytes() const { return 0; }
Stefan Maksimovic298d14e2017-01-11 05:58:27 -0800256 // Addressing relative to frame pointer differs in MIPS compared to X86/ARM
257 // since MIPS decrements its stack pointer prior to saving it in the frame
258 // pointer register.
259 virtual uint32_t getFramePointerOffset(uint32_t CurrentOffset,
260 uint32_t Size) const {
261 return -(CurrentOffset + Size);
262 }
Andrew Scull6d47bcd2015-09-17 17:10:05 -0700263 /// Return whether a 64-bit Variable should be split into a Variable64On32.
264 virtual bool shouldSplitToVariable64On32(Type Ty) const = 0;
265
Jaydeep Patil958ddb72016-10-03 07:52:48 -0700266 /// Return whether a Vector Variable should be split into a VariableVecOn32.
267 virtual bool shouldSplitToVariableVecOn32(Type Ty) const {
268 (void)Ty;
269 return false;
270 }
271
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700272 bool hasComputedFrame() const { return HasComputedFrame; }
Andrew Scull57e12682015-09-16 11:30:19 -0700273 /// Returns true if this function calls a function that has the "returns
274 /// twice" attribute.
Jan Voung44d53e12014-09-11 19:18:03 -0700275 bool callsReturnsTwice() const { return CallsReturnsTwice; }
Jim Stichnothdd842db2015-01-27 12:53:53 -0800276 void setCallsReturnsTwice(bool RetTwice) { CallsReturnsTwice = RetTwice; }
Jan Voungb36ad9b2015-04-21 17:01:49 -0700277 SizeT makeNextLabelNumber() { return NextLabelNumber++; }
Andrew Scull86df4e92015-07-30 13:54:44 -0700278 SizeT makeNextJumpTableNumber() { return NextJumpTableNumber++; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700279 LoweringContext &getContext() { return Context; }
Jim Stichnoth8ff4b282016-01-04 15:39:06 -0800280 Cfg *getFunc() const { return Func; }
281 GlobalContext *getGlobalContext() const { return Ctx; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700282
283 enum RegSet {
284 RegSet_None = 0,
285 RegSet_CallerSave = 1 << 0,
286 RegSet_CalleeSave = 1 << 1,
287 RegSet_StackPointer = 1 << 2,
288 RegSet_FramePointer = 1 << 3,
289 RegSet_All = ~RegSet_None
290 };
Andrew Scull8072bae2015-09-14 16:01:26 -0700291 using RegSetMask = uint32_t;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700292
John Portoe82b5602016-02-24 15:58:55 -0800293 virtual SmallBitVector getRegisterSet(RegSetMask Include,
294 RegSetMask Exclude) const = 0;
Jim Stichnothb40595a2016-01-29 06:14:31 -0800295 /// Get the set of physical registers available for the specified Variable's
296 /// register class, applying register restrictions from the command line.
John Portoe82b5602016-02-24 15:58:55 -0800297 virtual const SmallBitVector &
Jim Stichnothc59288b2015-11-09 11:38:40 -0800298 getRegistersForVariable(const Variable *Var) const = 0;
Jim Stichnothb40595a2016-01-29 06:14:31 -0800299 /// Get the set of *all* physical registers available for the specified
300 /// Variable's register class, *not* applying register restrictions from the
301 /// command line.
John Portoe82b5602016-02-24 15:58:55 -0800302 virtual const SmallBitVector &
Jim Stichnothb40595a2016-01-29 06:14:31 -0800303 getAllRegistersForVariable(const Variable *Var) const = 0;
John Portoe82b5602016-02-24 15:58:55 -0800304 virtual const SmallBitVector &getAliasesForRegister(RegNumT) const = 0;
John Portobb0a5fe2015-09-04 11:23:41 -0700305
Jim Stichnoth70d0a052014-11-14 15:53:46 -0800306 void regAlloc(RegAllocKind Kind);
Manasij Mukherjee7cd926d2016-08-04 12:33:23 -0700307 void postRegallocSplitting(const SmallBitVector &RegMask);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700308
Qining Luaee5fa82015-08-20 14:59:03 -0700309 virtual void
Jim Stichnoth8aa39662016-02-10 11:20:30 -0800310 makeRandomRegisterPermutation(llvm::SmallVectorImpl<RegNumT> &Permutation,
John Portoe82b5602016-02-24 15:58:55 -0800311 const SmallBitVector &ExcludeRegisters,
Qining Luaee5fa82015-08-20 14:59:03 -0700312 uint64_t Salt) const = 0;
Jim Stichnothe6d24782014-12-19 05:42:24 -0800313
Andrew Scull87f80c12015-07-20 10:19:16 -0700314 /// Get the minimum number of clusters required for a jump table to be
315 /// considered.
316 virtual SizeT getMinJumpTableSize() const = 0;
Andrew Scull86df4e92015-07-30 13:54:44 -0700317 virtual void emitJumpTable(const Cfg *Func,
318 const InstJumpTable *JumpTable) const = 0;
Andrew Scull87f80c12015-07-20 10:19:16 -0700319
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700320 virtual void emitVariable(const Variable *Var) const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700321
Jim Stichnoth8ff4b282016-01-04 15:39:06 -0800322 void emitWithoutPrefix(const ConstantRelocatable *CR,
323 const char *Suffix = "") const;
Jan Voung76bb0be2015-05-14 09:26:19 -0700324
Jan Voung76bb0be2015-05-14 09:26:19 -0700325 virtual void emit(const ConstantInteger32 *C) const = 0;
326 virtual void emit(const ConstantInteger64 *C) const = 0;
327 virtual void emit(const ConstantFloat *C) const = 0;
328 virtual void emit(const ConstantDouble *C) const = 0;
Jim Stichnoth8ff4b282016-01-04 15:39:06 -0800329 virtual void emit(const ConstantUndef *C) const = 0;
330 virtual void emit(const ConstantRelocatable *CR) const = 0;
Jan Voung76bb0be2015-05-14 09:26:19 -0700331
Andrew Scull9612d322015-07-06 14:53:25 -0700332 /// Performs target-specific argument lowering.
Matt Wala45a06232014-07-09 16:33:22 -0700333 virtual void lowerArguments() = 0;
334
Jim Stichnotha59ae6f2015-05-17 10:11:41 -0700335 virtual void initNodeForLowering(CfgNode *) {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700336 virtual void addProlog(CfgNode *Node) = 0;
337 virtual void addEpilog(CfgNode *Node) = 0;
338
Jim Stichnothb9a84722016-08-01 13:18:36 -0700339 /// Create a properly-typed "mov" instruction. This is primarily for local
340 /// variable splitting.
341 virtual Inst *createLoweredMove(Variable *Dest, Variable *SrcVar) {
342 // TODO(stichnot): make pure virtual by implementing for all targets
343 (void)Dest;
344 (void)SrcVar;
345 llvm::report_fatal_error("createLoweredMove() unimplemented");
346 return nullptr;
347 }
348
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700349 virtual ~TargetLowering() = default;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700350
John Porto3bf335f2016-01-15 11:17:55 -0800351private:
352 // This control variable is used by AutoBundle (RAII-style bundle
353 // locking/unlocking) to prevent nested bundles.
354 bool AutoBundling = false;
355
Eric Holkd6cf6b32016-02-17 11:09:48 -0800356 /// This indicates whether we are in the genTargetHelperCalls phase, and
357 /// therefore can do things like scalarization.
358 bool GeneratingTargetHelpers = false;
359
John Porto3bf335f2016-01-15 11:17:55 -0800360 // _bundle_lock(), and _bundle_unlock(), were made private to force subtargets
361 // to use the AutoBundle helper.
362 void
363 _bundle_lock(InstBundleLock::Option BundleOption = InstBundleLock::Opt_None) {
364 Context.insert<InstBundleLock>(BundleOption);
365 }
366 void _bundle_unlock() { Context.insert<InstBundleUnlock>(); }
367
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700368protected:
John Porto3bf335f2016-01-15 11:17:55 -0800369 /// AutoBundle provides RIAA-style bundling. Sub-targets are expected to use
370 /// it when emitting NaCl Bundles to ensure proper bundle_unlocking, and
371 /// prevent nested bundles.
372 ///
373 /// AutoBundle objects will emit a _bundle_lock during construction (but only
374 /// if sandboxed code generation was requested), and a bundle_unlock() during
375 /// destruction. By carefully scoping objects of this type, Subtargets can
376 /// ensure proper bundle emission.
377 class AutoBundle {
378 AutoBundle() = delete;
379 AutoBundle(const AutoBundle &) = delete;
380 AutoBundle &operator=(const AutoBundle &) = delete;
381
382 public:
383 explicit AutoBundle(TargetLowering *Target, InstBundleLock::Option Option =
384 InstBundleLock::Opt_None);
385 ~AutoBundle();
386
387 private:
388 TargetLowering *const Target;
389 const bool NeedSandboxing;
390 };
391
Jim Stichnothc6ead202015-02-24 09:30:30 -0800392 explicit TargetLowering(Cfg *Func);
Karl Schimpf5403f5d2016-01-15 11:07:46 -0800393 // Applies command line filters to TypeToRegisterSet array.
Jim Stichnoth467ffe52016-03-29 15:01:06 -0700394 static void filterTypeToRegisterSet(
395 GlobalContext *Ctx, int32_t NumRegs, SmallBitVector TypeToRegisterSet[],
396 size_t TypeToRegisterSetSize,
397 std::function<std::string(RegNumT)> getRegName,
398 std::function<const char *(RegClass)> getRegClassName);
Jim Stichnoth8cfeb692016-02-05 09:50:02 -0800399 virtual void lowerAlloca(const InstAlloca *Instr) = 0;
400 virtual void lowerArithmetic(const InstArithmetic *Instr) = 0;
401 virtual void lowerAssign(const InstAssign *Instr) = 0;
402 virtual void lowerBr(const InstBr *Instr) = 0;
Eric Holk67c7c412016-04-15 13:05:37 -0700403 virtual void lowerBreakpoint(const InstBreakpoint *Instr) = 0;
Jim Stichnoth8cfeb692016-02-05 09:50:02 -0800404 virtual void lowerCall(const InstCall *Instr) = 0;
405 virtual void lowerCast(const InstCast *Instr) = 0;
406 virtual void lowerFcmp(const InstFcmp *Instr) = 0;
407 virtual void lowerExtractElement(const InstExtractElement *Instr) = 0;
408 virtual void lowerIcmp(const InstIcmp *Instr) = 0;
409 virtual void lowerInsertElement(const InstInsertElement *Instr) = 0;
410 virtual void lowerIntrinsicCall(const InstIntrinsicCall *Instr) = 0;
411 virtual void lowerLoad(const InstLoad *Instr) = 0;
412 virtual void lowerPhi(const InstPhi *Instr) = 0;
413 virtual void lowerRet(const InstRet *Instr) = 0;
414 virtual void lowerSelect(const InstSelect *Instr) = 0;
John Portoa47c11c2016-04-21 05:53:42 -0700415 virtual void lowerShuffleVector(const InstShuffleVector *Instr) = 0;
Jim Stichnoth8cfeb692016-02-05 09:50:02 -0800416 virtual void lowerStore(const InstStore *Instr) = 0;
417 virtual void lowerSwitch(const InstSwitch *Instr) = 0;
418 virtual void lowerUnreachable(const InstUnreachable *Instr) = 0;
Jim Stichnothe4f65d82015-06-17 22:16:02 -0700419 virtual void lowerOther(const Inst *Instr);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700420
John Porto5e0a8a72015-11-20 13:50:36 -0800421 virtual void genTargetHelperCallFor(Inst *Instr) = 0;
John Portof4198542015-11-20 14:17:23 -0800422 virtual uint32_t getCallStackArgumentsSizeBytes(const InstCall *Instr) = 0;
John Porto5e0a8a72015-11-20 13:50:36 -0800423
Manasij Mukherjee0c704172016-07-21 12:40:24 -0700424 /// Opportunity to modify other instructions to help Address Optimization
425 virtual void doAddressOptOther() {}
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700426 virtual void doAddressOptLoad() {}
427 virtual void doAddressOptStore() {}
Jim Stichnothad2989b2015-09-15 10:21:42 -0700428 virtual void doMockBoundsCheck(Operand *) {}
Qining Luaee5fa82015-08-20 14:59:03 -0700429 virtual void randomlyInsertNop(float Probability,
430 RandomNumberGenerator &RNG) = 0;
Andrew Scull57e12682015-09-16 11:30:19 -0700431 /// This gives the target an opportunity to post-process the lowered expansion
432 /// before returning.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700433 virtual void postLower() {}
434
Jim Stichnoth91c773e2016-01-19 09:52:22 -0800435 /// When the SkipUnimplemented flag is set, addFakeDefUses() gets invoked by
436 /// the UnimplementedLoweringError macro to insert fake uses of all the
437 /// instruction variables and a fake def of the instruction dest, in order to
438 /// preserve integrity of liveness analysis.
439 void addFakeDefUses(const Inst *Instr);
440
Jim Stichnoth230d4102015-09-25 17:40:32 -0700441 /// Find (non-SSA) instructions where the Dest variable appears in some source
442 /// operand, and set the IsDestRedefined flag. This keeps liveness analysis
443 /// consistent.
444 void markRedefinitions();
Jan Voungb3401d22015-05-18 09:38:21 -0700445
Andrew Scull57e12682015-09-16 11:30:19 -0700446 /// Make a pass over the Cfg to determine which variables need stack slots and
447 /// place them in a sorted list (SortedSpilledVariables). Among those, vars,
448 /// classify the spill variables as local to the basic block vs global
449 /// (multi-block) in order to compute the parameters GlobalsSize and
450 /// SpillAreaSizeBytes (represents locals or general vars if the coalescing of
451 /// locals is disallowed) along with alignments required for variables in each
452 /// area. We rely on accurate VMetadata in order to classify a variable as
453 /// global vs local (otherwise the variable is conservatively global). The
454 /// in-args should be initialized to 0.
Andrew Scull9612d322015-07-06 14:53:25 -0700455 ///
Andrew Scull57e12682015-09-16 11:30:19 -0700456 /// This is only a pre-pass and the actual stack slot assignment is handled
457 /// separately.
Andrew Scull9612d322015-07-06 14:53:25 -0700458 ///
Andrew Scull57e12682015-09-16 11:30:19 -0700459 /// There may be target-specific Variable types, which will be handled by
460 /// TargetVarHook. If the TargetVarHook returns true, then the variable is
461 /// skipped and not considered with the rest of the spilled variables.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700462 void getVarStackSlotParams(VarList &SortedSpilledVariables,
John Portoe82b5602016-02-24 15:58:55 -0800463 SmallBitVector &RegsUsed, size_t *GlobalsSize,
464 size_t *SpillAreaSizeBytes,
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700465 uint32_t *SpillAreaAlignmentBytes,
466 uint32_t *LocalsSlotsAlignmentBytes,
467 std::function<bool(Variable *)> TargetVarHook);
468
Andrew Scull57e12682015-09-16 11:30:19 -0700469 /// Calculate the amount of padding needed to align the local and global areas
470 /// to the required alignment. This assumes the globals/locals layout used by
471 /// getVarStackSlotParams and assignVarStackSlots.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700472 void alignStackSpillAreas(uint32_t SpillAreaStartOffset,
473 uint32_t SpillAreaAlignmentBytes,
474 size_t GlobalsSize,
475 uint32_t LocalsSlotsAlignmentBytes,
476 uint32_t *SpillAreaPaddingBytes,
477 uint32_t *LocalsSlotsPaddingBytes);
478
Andrew Scull57e12682015-09-16 11:30:19 -0700479 /// Make a pass through the SortedSpilledVariables and actually assign stack
480 /// slots. SpillAreaPaddingBytes takes into account stack alignment padding.
481 /// The SpillArea starts after that amount of padding. This matches the scheme
482 /// in getVarStackSlotParams, where there may be a separate multi-block global
483 /// var spill area and a local var spill area.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700484 void assignVarStackSlots(VarList &SortedSpilledVariables,
485 size_t SpillAreaPaddingBytes,
486 size_t SpillAreaSizeBytes,
487 size_t GlobalsAndSubsequentPaddingSize,
488 bool UsesFramePointer);
489
Andrew Scull57e12682015-09-16 11:30:19 -0700490 /// Sort the variables in Source based on required alignment. The variables
491 /// with the largest alignment need are placed in the front of the Dest list.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700492 void sortVarsByAlignment(VarList &Dest, const VarList &Source) const;
493
Karl Schimpf20070e82016-03-17 13:30:13 -0700494 InstCall *makeHelperCall(RuntimeHelper FuncID, Variable *Dest, SizeT MaxSrcs);
Jan Voungb36ad9b2015-04-21 17:01:49 -0700495
Jim Stichnoth230d4102015-09-25 17:40:32 -0700496 void _set_dest_redefined() { Context.getLastInserted()->setDestRedefined(); }
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700497
Andrew Scullcfa628b2015-08-20 14:23:05 -0700498 bool shouldOptimizeMemIntrins();
499
Eric Holkcfc25532016-02-09 17:47:58 -0800500 void scalarizeArithmetic(InstArithmetic::OpKind K, Variable *Dest,
501 Operand *Src0, Operand *Src1);
502
Eric Holkcc69fa22016-02-10 13:07:06 -0800503 /// Generalizes scalarizeArithmetic to support other instruction types.
504 ///
Eric Holkd6cf6b32016-02-17 11:09:48 -0800505 /// insertScalarInstruction is a function-like object with signature
Eric Holkcc69fa22016-02-10 13:07:06 -0800506 /// (Variable *Dest, Variable *Src0, Variable *Src1) -> Instr *.
Eric Holkd6cf6b32016-02-17 11:09:48 -0800507 template <typename... Operands,
508 typename F = std::function<Inst *(Variable *, Operands *...)>>
509 void scalarizeInstruction(Variable *Dest, F insertScalarInstruction,
510 Operands *... Srcs) {
511 assert(GeneratingTargetHelpers &&
512 "scalarizeInstruction called during incorrect phase");
Eric Holkcc69fa22016-02-10 13:07:06 -0800513 const Type DestTy = Dest->getType();
514 assert(isVectorType(DestTy));
515 const Type DestElementTy = typeElementType(DestTy);
516 const SizeT NumElements = typeNumElements(DestTy);
Eric Holkcc69fa22016-02-10 13:07:06 -0800517
518 Variable *T = Func->makeVariable(DestTy);
Jaydeep Patil958ddb72016-10-03 07:52:48 -0700519 if (auto *VarVecOn32 = llvm::dyn_cast<VariableVecOn32>(T)) {
520 VarVecOn32->initVecElement(Func);
Jaydeep Patil3a01f332016-10-17 06:33:50 -0700521 auto *Undef = ConstantUndef::create(Ctx, DestTy);
522 Context.insert<InstAssign>(T, Undef);
523 } else {
524 Context.insert<InstFakeDef>(T);
Jaydeep Patil958ddb72016-10-03 07:52:48 -0700525 }
Eric Holkcc69fa22016-02-10 13:07:06 -0800526
Eric Holkd6cf6b32016-02-17 11:09:48 -0800527 for (SizeT I = 0; I < NumElements; ++I) {
528 auto *Index = Ctx->getConstantInt32(I);
529
530 auto makeExtractThunk = [this, Index, NumElements](Operand *Src) {
531 return [this, Index, NumElements, Src]() {
532 assert(typeNumElements(Src->getType()) == NumElements);
533
534 const auto ElementTy = typeElementType(Src->getType());
535 auto *Op = Func->makeVariable(ElementTy);
536 Context.insert<InstExtractElement>(Op, Src, Index);
537 return Op;
538 };
539 };
Eric Holkcc69fa22016-02-10 13:07:06 -0800540
541 // Perform the operation as a scalar operation.
Eric Holkd6cf6b32016-02-17 11:09:48 -0800542 auto *Res = Func->makeVariable(DestElementTy);
543 auto *Arith = applyToThunkedArgs(insertScalarInstruction, Res,
544 makeExtractThunk(Srcs)...);
Eric Holkcc69fa22016-02-10 13:07:06 -0800545 genTargetHelperCallFor(Arith);
546
Eric Holkcc69fa22016-02-10 13:07:06 -0800547 Variable *DestT = Func->makeVariable(DestTy);
548 Context.insert<InstInsertElement>(DestT, T, Res, Index);
549 T = DestT;
550 }
551 Context.insert<InstAssign>(Dest, T);
552 }
553
Eric Holkd6cf6b32016-02-17 11:09:48 -0800554 // applyToThunkedArgs is used by scalarizeInstruction. Ideally, we would just
555 // call insertScalarInstruction(Res, Srcs...), but C++ does not specify
556 // evaluation order which means this leads to an unpredictable final
557 // output. Instead, we wrap each of the Srcs in a thunk and these
558 // applyToThunkedArgs functions apply the thunks in a well defined order so we
559 // still get well-defined output.
560 Inst *applyToThunkedArgs(
561 std::function<Inst *(Variable *, Variable *)> insertScalarInstruction,
562 Variable *Res, std::function<Variable *()> thunk0) {
563 auto *Src0 = thunk0();
564 return insertScalarInstruction(Res, Src0);
565 }
Eric Holkcc69fa22016-02-10 13:07:06 -0800566
Eric Holkd6cf6b32016-02-17 11:09:48 -0800567 Inst *
568 applyToThunkedArgs(std::function<Inst *(Variable *, Variable *, Variable *)>
569 insertScalarInstruction,
570 Variable *Res, std::function<Variable *()> thunk0,
571 std::function<Variable *()> thunk1) {
572 auto *Src0 = thunk0();
573 auto *Src1 = thunk1();
574 return insertScalarInstruction(Res, Src0, Src1);
575 }
Eric Holkcc69fa22016-02-10 13:07:06 -0800576
Eric Holkd6cf6b32016-02-17 11:09:48 -0800577 Inst *applyToThunkedArgs(
578 std::function<Inst *(Variable *, Variable *, Variable *, Variable *)>
579 insertScalarInstruction,
580 Variable *Res, std::function<Variable *()> thunk0,
581 std::function<Variable *()> thunk1, std::function<Variable *()> thunk2) {
582 auto *Src0 = thunk0();
583 auto *Src1 = thunk1();
584 auto *Src2 = thunk2();
585 return insertScalarInstruction(Res, Src0, Src1, Src2);
Eric Holkcc69fa22016-02-10 13:07:06 -0800586 }
587
John Portoac2388c2016-01-22 07:10:56 -0800588 /// SandboxType enumerates all possible sandboxing strategies that
589 enum SandboxType {
590 ST_None,
591 ST_NaCl,
592 ST_Nonsfi,
593 };
594
595 static SandboxType determineSandboxTypeFromFlags(const ClFlags &Flags);
596
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700597 Cfg *Func;
598 GlobalContext *Ctx;
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700599 bool HasComputedFrame = false;
600 bool CallsReturnsTwice = false;
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700601 SizeT NextLabelNumber = 0;
Andrew Scull86df4e92015-07-30 13:54:44 -0700602 SizeT NextJumpTableNumber = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700603 LoweringContext Context;
John Portoac2388c2016-01-22 07:10:56 -0800604 const SandboxType SandboxingType = ST_None;
Jim Stichnoth9738a9e2015-02-23 16:39:06 -0800605
Jim Stichnoth8ff4b282016-01-04 15:39:06 -0800606 const static constexpr char *H_getIP_prefix = "__Sz_getIP_";
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700607};
608
Andrew Scull57e12682015-09-16 11:30:19 -0700609/// TargetDataLowering is used for "lowering" data including initializers for
610/// global variables, and the internal constant pools. It is separated out from
611/// TargetLowering because it does not require a Cfg.
Jan Voung72984d82015-01-29 14:42:38 -0800612class TargetDataLowering {
613 TargetDataLowering() = delete;
614 TargetDataLowering(const TargetDataLowering &) = delete;
615 TargetDataLowering &operator=(const TargetDataLowering &) = delete;
Jim Stichnoth7b451a92014-10-15 14:39:23 -0700616
Jim Stichnothde4ca712014-06-29 08:13:48 -0700617public:
Jim Stichnothbbca7542015-02-11 16:08:31 -0800618 static std::unique_ptr<TargetDataLowering> createLowering(GlobalContext *Ctx);
Jan Voung72984d82015-01-29 14:42:38 -0800619 virtual ~TargetDataLowering();
Jan Voung839c4ce2014-07-28 15:19:43 -0700620
John Porto8b1a7052015-06-17 13:20:08 -0700621 virtual void lowerGlobals(const VariableDeclarationList &Vars,
Jim Stichnoth467ffe52016-03-29 15:01:06 -0700622 const std::string &SectionSuffix) = 0;
John Porto0f86d032015-06-15 07:44:27 -0700623 virtual void lowerConstants() = 0;
Andrew Scull86df4e92015-07-30 13:54:44 -0700624 virtual void lowerJumpTables() = 0;
Jaydeep Patil3da9f652016-11-03 22:54:06 -0700625 virtual void emitTargetRODataSections() {}
Jim Stichnothde4ca712014-06-29 08:13:48 -0700626
627protected:
John Porto8b1a7052015-06-17 13:20:08 -0700628 void emitGlobal(const VariableDeclaration &Var,
Jim Stichnoth467ffe52016-03-29 15:01:06 -0700629 const std::string &SectionSuffix);
Jan Voung58eea4d2015-06-15 15:11:56 -0700630
Andrew Scull57e12682015-09-16 11:30:19 -0700631 /// For now, we assume .long is the right directive for emitting 4 byte emit
632 /// global relocations. However, LLVM MIPS usually uses .4byte instead.
Andrew Scull9612d322015-07-06 14:53:25 -0700633 /// Perhaps there is some difference when the location is unaligned.
John Porto8b1a7052015-06-17 13:20:08 -0700634 static const char *getEmit32Directive() { return ".long"; }
Jan Voung58eea4d2015-06-15 15:11:56 -0700635
Jim Stichnothc6ead202015-02-24 09:30:30 -0800636 explicit TargetDataLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
Jim Stichnothde4ca712014-06-29 08:13:48 -0700637 GlobalContext *Ctx;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700638};
639
Andrew Scull57e12682015-09-16 11:30:19 -0700640/// TargetHeaderLowering is used to "lower" the header of an output file. It
641/// writes out the target-specific header attributes. E.g., for ARM this writes
642/// out the build attributes (float ABI, etc.).
Jan Voungfb792842015-06-11 15:27:50 -0700643class TargetHeaderLowering {
644 TargetHeaderLowering() = delete;
645 TargetHeaderLowering(const TargetHeaderLowering &) = delete;
646 TargetHeaderLowering &operator=(const TargetHeaderLowering &) = delete;
647
648public:
649 static std::unique_ptr<TargetHeaderLowering>
650 createLowering(GlobalContext *Ctx);
651 virtual ~TargetHeaderLowering();
652
653 virtual void lower() {}
654
655protected:
656 explicit TargetHeaderLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
657 GlobalContext *Ctx;
658};
659
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700660} // end of namespace Ice
661
662#endif // SUBZERO_SRC_ICETARGETLOWERING_H