|
Intel SPMD Program Compiler
1.3.0
|
00001 /* 00002 Copyright (c) 2010-2012, Intel Corporation 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without 00006 modification, are permitted provided that the following conditions are 00007 met: 00008 00009 * Redistributions of source code must retain the above copyright 00010 notice, this list of conditions and the following disclaimer. 00011 00012 * Redistributions in binary form must reproduce the above copyright 00013 notice, this list of conditions and the following disclaimer in the 00014 documentation and/or other materials provided with the distribution. 00015 00016 * Neither the name of Intel Corporation nor the names of its 00017 contributors may be used to endorse or promote products derived from 00018 this software without specific prior written permission. 00019 00020 00021 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00022 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00023 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00024 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 00025 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00026 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00027 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 */ 00033 00034 /** @file ctx.h 00035 @brief Declaration of the FunctionEmitContext class 00036 */ 00037 00038 #ifndef ISPC_CTX_H 00039 #define ISPC_CTX_H 1 00040 00041 #include "ispc.h" 00042 #include <map> 00043 #include <llvm/InstrTypes.h> 00044 #include <llvm/Instructions.h> 00045 #include <llvm/Analysis/DIBuilder.h> 00046 #if defined(LLVM_3_0) || defined(LLVM_3_1) 00047 #include <llvm/Analysis/DebugInfo.h> 00048 #else 00049 #include <llvm/DebugInfo.h> 00050 #endif 00051 00052 struct CFInfo; 00053 00054 /** FunctionEmitContext is one of the key classes in ispc; it is used to 00055 help with emitting the intermediate representation of a function during 00056 compilation. It carries information the current program context during 00057 IR emission (e.g. the basic block into which instructions should be 00058 added; or, the current source file and line number, so debugging 00059 symbols can be correctly generated). This class also provides a number 00060 of helper routines that are useful for code that emits IR. 00061 */ 00062 class FunctionEmitContext { 00063 public: 00064 /** Create a new FunctionEmitContext. 00065 @param function The Function object representing the function 00066 @param funSym Symbol that corresponds to the function 00067 @param llvmFunction LLVM function in the current module that corresponds 00068 to the function 00069 @param firstStmtPos Source file position of the first statement in the 00070 function 00071 */ 00072 FunctionEmitContext(Function *function, Symbol *funSym, 00073 llvm::Function *llvmFunction, 00074 SourcePos firstStmtPos); 00075 ~FunctionEmitContext(); 00076 00077 /** Returns the Function * corresponding to the function that we're 00078 currently generating code for. */ 00079 const Function *GetFunction() const; 00080 00081 /** @name Current basic block management 00082 @{ 00083 */ 00084 /** Returns the current basic block pointer */ 00085 llvm::BasicBlock *GetCurrentBasicBlock(); 00086 00087 /** Set the given llvm::BasicBlock to be the basic block to emit 00088 forthcoming instructions into. */ 00089 void SetCurrentBasicBlock(llvm::BasicBlock *bblock); 00090 00091 /** @name Mask management 00092 @{ 00093 */ 00094 /** Returns the mask value at entry to the current function. */ 00095 llvm::Value *GetFunctionMask(); 00096 00097 /** Returns the mask value corresponding to "varying" control flow 00098 within the current function. (i.e. this doesn't include the effect 00099 of the mask at function entry. */ 00100 llvm::Value *GetInternalMask(); 00101 00102 /** Returns the complete current mask value--i.e. the logical AND of 00103 the function entry mask and the internal mask. */ 00104 llvm::Value *GetFullMask(); 00105 00106 /** Returns a pointer to storage in memory that stores the current full 00107 mask. */ 00108 llvm::Value *GetFullMaskPointer(); 00109 00110 /** Provides the value of the mask at function entry */ 00111 void SetFunctionMask(llvm::Value *val); 00112 00113 /** Sets the internal mask to a new value */ 00114 void SetInternalMask(llvm::Value *val); 00115 00116 /** Sets the internal mask to (oldMask & val) */ 00117 void SetInternalMaskAnd(llvm::Value *oldMask, llvm::Value *val); 00118 00119 /** Sets the internal mask to (oldMask & ~val) */ 00120 void SetInternalMaskAndNot(llvm::Value *oldMask, llvm::Value *test); 00121 00122 /** Emits a branch instruction to the basic block btrue if any of the 00123 lanes of current mask are on and bfalse if none are on. */ 00124 void BranchIfMaskAny(llvm::BasicBlock *btrue, llvm::BasicBlock *bfalse); 00125 00126 /** Emits a branch instruction to the basic block btrue if all of the 00127 lanes of current mask are on and bfalse if none are on. */ 00128 void BranchIfMaskAll(llvm::BasicBlock *btrue, llvm::BasicBlock *bfalse); 00129 00130 /** Emits a branch instruction to the basic block btrue if none of the 00131 lanes of current mask are on and bfalse if none are on. */ 00132 void BranchIfMaskNone(llvm::BasicBlock *btrue, llvm::BasicBlock *bfalse); 00133 /** @} */ 00134 00135 /** @name Control flow management 00136 @{ 00137 */ 00138 /** Notifies the FunctionEmitContext that we're starting emission of an 00139 'if' statement with a uniform test. */ 00140 void StartUniformIf(); 00141 00142 /** Notifies the FunctionEmitContext that we're starting emission of an 00143 'if' statement with a varying test. The value of the mask going 00144 into the 'if' statement is provided in the oldMask parameter. */ 00145 void StartVaryingIf(llvm::Value *oldMask); 00146 00147 /** Notifies the FunctionEmitConitext that we're done emitting the IR 00148 for an 'if' statement. */ 00149 void EndIf(); 00150 00151 /** Notifies the FunctionEmitContext that we're starting to emit IR 00152 for a loop. Basic blocks are provides for where 'break' and 00153 'continue' statements should jump to (if all running lanes want to 00154 break or continue), uniformControlFlow indicates whether the loop 00155 condition is 'uniform'. */ 00156 void StartLoop(llvm::BasicBlock *breakTarget, llvm::BasicBlock *continueTarget, 00157 bool uniformControlFlow); 00158 00159 /** Informs FunctionEmitContext of the value of the mask at the start 00160 of a loop body. */ 00161 void SetLoopMask(llvm::Value *mask); 00162 00163 /** Informs FunctionEmitContext that code generation for a loop is 00164 finished. */ 00165 void EndLoop(); 00166 00167 /** Indicates that code generation for a 'foreach', 'foreach_tiled', 00168 'foreach_active', or 'foreach_unique' loop is about to start. */ 00169 enum ForeachType { FOREACH_REGULAR, FOREACH_ACTIVE, FOREACH_UNIQUE }; 00170 void StartForeach(ForeachType ft); 00171 void EndForeach(); 00172 00173 /** Emit code for a 'break' statement in a loop. If doCoherenceCheck 00174 is true, then if we're in a 'varying' loop, code will be emitted to 00175 see if all of the lanes want to break, in which case a jump to the 00176 break target will be taken. (For 'uniform' loops, the jump is 00177 always done). */ 00178 void Break(bool doCoherenceCheck); 00179 00180 /** Emit code for a 'continue' statement in a loop. If 00181 doCoherenceCheck is true, then if we're in a 'varying' loop, code 00182 will be emitted to see if all of the lanes want to continue, in 00183 which case a jump to the continue target will be taken. (For 00184 'uniform' loops, the jump is always done). */ 00185 void Continue(bool doCoherenceCheck); 00186 00187 /** This method is called by code emitting IR for a loop at the end of 00188 the loop body; it restores the lanes of the mask that executed a 00189 'continue' statement when going through the loop body in the 00190 previous iteration. */ 00191 void RestoreContinuedLanes(); 00192 00193 /** Indicates that code generation for a "switch" statement is about to 00194 start. isUniform indicates whether the "switch" value is uniform, 00195 and bbAfterSwitch gives the basic block immediately following the 00196 "switch" statement. (For example, if the switch condition is 00197 uniform, we jump here upon executing a "break" statement.) */ 00198 void StartSwitch(bool isUniform, llvm::BasicBlock *bbAfterSwitch); 00199 /** Indicates the end of code generation for a "switch" statement. */ 00200 void EndSwitch(); 00201 00202 /** Emits code for a "switch" statement in the program. 00203 @param expr Gives the value of the expression after the "switch" 00204 @param defaultBlock Basic block to execute for the "default" case. This 00205 should be NULL if there is no "default" label inside 00206 the switch. 00207 @param caseBlocks vector that stores the mapping from label values 00208 after "case" statements to basic blocks corresponding 00209 to the "case" labels. 00210 @param nextBlocks For each basic block for a "case" or "default" 00211 label, this gives the basic block for the 00212 immediately-following "case" or "default" label (or 00213 the basic block after the "switch" statement for the 00214 last label.) 00215 */ 00216 void SwitchInst(llvm::Value *expr, llvm::BasicBlock *defaultBlock, 00217 const std::vector<std::pair<int, llvm::BasicBlock *> > &caseBlocks, 00218 const std::map<llvm::BasicBlock *, llvm::BasicBlock *> &nextBlocks); 00219 00220 /** Generates code for a "default" label after a "switch" statement. 00221 The checkMask parameter indicates whether additional code should be 00222 generated to check to see if the execution mask is all off after 00223 the default label (in which case a jump to the following label will 00224 be issued. */ 00225 void EmitDefaultLabel(bool checkMask, SourcePos pos); 00226 00227 /** Generates code for a "case" label after a "switch" statement. See 00228 the documentation for EmitDefaultLabel() for discussion of the 00229 checkMask parameter. */ 00230 void EmitCaseLabel(int value, bool checkMask, SourcePos pos); 00231 00232 /** Returns the current number of nested levels of 'varying' control 00233 flow */ 00234 int VaryingCFDepth() const; 00235 00236 bool InForeachLoop() const; 00237 00238 /** Temporarily disables emission of performance warnings from gathers 00239 and scatters from subsequent code. */ 00240 void DisableGatherScatterWarnings(); 00241 00242 /** Reenables emission of gather/scatter performance warnings. */ 00243 void EnableGatherScatterWarnings(); 00244 00245 void SetContinueTarget(llvm::BasicBlock *bb) { continueTarget = bb; } 00246 00247 /** Step through the code and find label statements; create a basic 00248 block for each one, so that subsequent calls to 00249 GetLabeledBasicBlock() return the corresponding basic block. */ 00250 void InitializeLabelMap(Stmt *code); 00251 00252 /** If there is a label in the function with the given name, return the 00253 new basic block that it starts. */ 00254 llvm::BasicBlock *GetLabeledBasicBlock(const std::string &label); 00255 00256 /** Returns a vector of all labels in the context. This is 00257 simply the key set of the labelMap */ 00258 std::vector<std::string> GetLabels(); 00259 00260 /** Called to generate code for 'return' statement; value is the 00261 expression in the return statement (if non-NULL), and 00262 doCoherenceCheck indicates whether instructions should be generated 00263 to see if all of the currently-running lanes have returned (if 00264 we're under varying control flow). */ 00265 void CurrentLanesReturned(Expr *value, bool doCoherenceCheck); 00266 /** @} */ 00267 00268 /** @name Small helper/utility routines 00269 @{ 00270 */ 00271 /** Given a boolean mask value of type LLVMTypes::MaskType, return an 00272 i1 value that indicates if any of the mask lanes are on. */ 00273 llvm::Value *Any(llvm::Value *mask); 00274 00275 /** Given a boolean mask value of type LLVMTypes::MaskType, return an 00276 i1 value that indicates if all of the mask lanes are on. */ 00277 llvm::Value *All(llvm::Value *mask); 00278 00279 /** Given a boolean mask value of type LLVMTypes::MaskType, return an 00280 i1 value that indicates if all of the mask lanes are off. */ 00281 llvm::Value *None(llvm::Value *mask); 00282 00283 /** Given a boolean mask value of type LLVMTypes::MaskType, return an 00284 i64 value wherein the i'th bit is on if and only if the i'th lane 00285 of the mask is on. */ 00286 llvm::Value *LaneMask(llvm::Value *mask); 00287 00288 /** Given two masks of type LLVMTypes::MaskType, return an i1 value 00289 that indicates whether the two masks are equal. */ 00290 llvm::Value *MasksAllEqual(llvm::Value *mask1, llvm::Value *mask2); 00291 00292 /** Given a string, create an anonymous global variable to hold its 00293 value and return the pointer to the string. */ 00294 llvm::Value *GetStringPtr(const std::string &str); 00295 00296 /** Create a new basic block with given name */ 00297 llvm::BasicBlock *CreateBasicBlock(const char *name); 00298 00299 /** Given a vector with element type i1, return a vector of type 00300 LLVMTypes::BoolVectorType. This method handles the conversion for 00301 the targets where the bool vector element type is, for example, 00302 i32. */ 00303 llvm::Value *I1VecToBoolVec(llvm::Value *b); 00304 00305 /** If the user has asked to compile the program with instrumentation, 00306 this inserts a callback to the user-supplied instrumentation 00307 function at the current point in the code. */ 00308 void AddInstrumentationPoint(const char *note); 00309 /** @} */ 00310 00311 /** @name Debugging support 00312 @{ 00313 */ 00314 /** Set the current source file position; subsequent emitted 00315 instructions will have this position associated with them if 00316 debugging information is being generated. */ 00317 void SetDebugPos(SourcePos pos); 00318 00319 SourcePos GetDebugPos() const; 00320 00321 /** Adds debugging metadata to the given instruction. If pos == NULL, 00322 use FunctionEmitContext::currentPos as the source file position for 00323 the instruction. Similarly, if a DIScope is provided, it's used 00324 and otherwise the scope is found from a GetDIScope() call. This 00325 takes a llvm::Value for the instruction rather than an 00326 llvm::Instruction for convenience; in calling code we often have 00327 Instructions stored using Value pointers; the code here returns 00328 silently if it's not actually given an instruction. */ 00329 void AddDebugPos(llvm::Value *instruction, const SourcePos *pos = NULL, 00330 llvm::DIScope *scope = NULL); 00331 00332 /** Inform the debugging information generation code that a new scope 00333 is starting in the source program. */ 00334 void StartScope(); 00335 00336 /** Inform the debugging information generation code that the current 00337 scope is ending in the source program. */ 00338 void EndScope(); 00339 00340 /** Returns the llvm::DIScope corresponding to the current program 00341 scope. */ 00342 llvm::DIScope GetDIScope() const; 00343 00344 /** Emits debugging information for the variable represented by 00345 sym. */ 00346 void EmitVariableDebugInfo(Symbol *sym); 00347 00348 /** Emits debugging information for the function parameter represented 00349 by sym. */ 00350 void EmitFunctionParameterDebugInfo(Symbol *sym, int parameterNum); 00351 /** @} */ 00352 00353 /** @name IR instruction emission 00354 @brief These methods generally closely correspond to LLVM IR 00355 instructions. See the LLVM assembly language reference manual 00356 (http://llvm.org/docs/LangRef.html) and the LLVM doxygen documentaion 00357 (http://llvm.org/doxygen) for more information. Here we will only 00358 document significant generalizations to the functionality of the 00359 corresponding basic LLVM instructions. 00360 00361 Beyond actually emitting the instruction, the implementations of 00362 these methods in FunctionEmitContext also handle adding debugging 00363 metadata if debugging symbols are enabled, adding the instructions 00364 to the current basic block, and handling generalizations like 00365 'varying' lvalues, arithmetic operations with VectorType operands, 00366 etc. 00367 @{ 00368 */ 00369 /** Emit the binary operator given by the inst parameter. If 00370 llvm::Values corresponding to VectorTypes are given as operands, 00371 this also handles applying the given operation to the vector 00372 elements. */ 00373 llvm::Value *BinaryOperator(llvm::Instruction::BinaryOps inst, 00374 llvm::Value *v0, llvm::Value *v1, 00375 const char *name = NULL); 00376 00377 /** Emit the "not" operator. Like BinaryOperator(), this also handles 00378 a VectorType-based operand. */ 00379 llvm::Value *NotOperator(llvm::Value *v, const char *name = NULL); 00380 00381 /** Emit a comparison instruction. If the operands are VectorTypes, 00382 then a value for the corresponding boolean VectorType is 00383 returned. */ 00384 llvm::Value *CmpInst(llvm::Instruction::OtherOps inst, 00385 llvm::CmpInst::Predicate pred, 00386 llvm::Value *v0, llvm::Value *v1, const char *name = NULL); 00387 00388 /** Given a scalar value, return a vector of the same type (or an 00389 array, for pointer types). */ 00390 llvm::Value *SmearUniform(llvm::Value *value, const char *name = NULL); 00391 00392 llvm::Value *BitCastInst(llvm::Value *value, llvm::Type *type, 00393 const char *name = NULL); 00394 llvm::Value *PtrToIntInst(llvm::Value *value, const char *name = NULL); 00395 llvm::Value *PtrToIntInst(llvm::Value *value, llvm::Type *type, 00396 const char *name = NULL); 00397 llvm::Value *IntToPtrInst(llvm::Value *value, llvm::Type *type, 00398 const char *name = NULL); 00399 00400 llvm::Instruction *TruncInst(llvm::Value *value, llvm::Type *type, 00401 const char *name = NULL); 00402 llvm::Instruction *CastInst(llvm::Instruction::CastOps op, llvm::Value *value, 00403 llvm::Type *type, const char *name = NULL); 00404 llvm::Instruction *FPCastInst(llvm::Value *value, llvm::Type *type, 00405 const char *name = NULL); 00406 llvm::Instruction *SExtInst(llvm::Value *value, llvm::Type *type, 00407 const char *name = NULL); 00408 llvm::Instruction *ZExtInst(llvm::Value *value, llvm::Type *type, 00409 const char *name = NULL); 00410 00411 /** Given two integer-typed values (but possibly one vector and the 00412 other not, and or of possibly-different bit-widths), update their 00413 values as needed so that the two have the same (more general) 00414 type. */ 00415 void MatchIntegerTypes(llvm::Value **v0, llvm::Value **v1); 00416 00417 /** Create a new slice pointer out of the given pointer to an soa type 00418 and an integer offset to a slice within that type. */ 00419 llvm::Value *MakeSlicePointer(llvm::Value *ptr, llvm::Value *offset); 00420 00421 /** These GEP methods are generalizations of the standard ones in LLVM; 00422 they support both uniform and varying basePtr values as well as 00423 uniform and varying index values (arrays of indices). Varying base 00424 pointers are expected to come in as vectors of i32/i64 (depending 00425 on the target), since LLVM doesn't currently support vectors of 00426 pointers. The underlying type of the base pointer must be provided 00427 via the ptrType parameter */ 00428 llvm::Value *GetElementPtrInst(llvm::Value *basePtr, llvm::Value *index, 00429 const Type *ptrType, const char *name = NULL); 00430 llvm::Value *GetElementPtrInst(llvm::Value *basePtr, llvm::Value *index0, 00431 llvm::Value *index1, const Type *ptrType, 00432 const char *name = NULL); 00433 00434 /** This method returns a new pointer that represents offsetting the 00435 given base pointer to point at the given element number of the 00436 structure type that the base pointer points to. (The provided 00437 pointer must be a pointer to a structure type. The ptrType gives 00438 the type of the pointer, though it may be NULL if the base pointer 00439 is uniform. */ 00440 llvm::Value *AddElementOffset(llvm::Value *basePtr, int elementNum, 00441 const Type *ptrType, const char *name = NULL, 00442 const PointerType **resultPtrType = NULL); 00443 00444 /** Load from the memory location(s) given by lvalue, using the given 00445 mask. The lvalue may be varying, in which case this corresponds to 00446 a gather from the multiple memory locations given by the array of 00447 pointer values given by the lvalue. If the lvalue is not varying, 00448 then both the mask pointer and the type pointer may be NULL. */ 00449 llvm::Value *LoadInst(llvm::Value *ptr, llvm::Value *mask, 00450 const Type *ptrType, const char *name = NULL); 00451 00452 llvm::Value *LoadInst(llvm::Value *ptr, const char *name = NULL); 00453 00454 /** Emits an alloca instruction to allocate stack storage for the given 00455 type. If a non-zero alignment is specified, the object is also 00456 allocated at the given alignment. By default, the alloca 00457 instruction is added at the start of the function in the entry 00458 basic block; if it should be added to the current basic block, then 00459 the atEntryBlock parameter should be false. */ 00460 llvm::Value *AllocaInst(llvm::Type *llvmType, 00461 const char *name = NULL, int align = 0, 00462 bool atEntryBlock = true); 00463 00464 /** Standard store instruction; for this variant, the lvalue must be a 00465 single pointer, not a varying lvalue. */ 00466 void StoreInst(llvm::Value *value, llvm::Value *ptr); 00467 00468 /** In this variant of StoreInst(), the lvalue may be varying. If so, 00469 this corresponds to a scatter. Whether the lvalue is uniform of 00470 varying, the given storeMask is used to mask the stores so that 00471 they only execute for the active program instances. */ 00472 void StoreInst(llvm::Value *value, llvm::Value *ptr, 00473 llvm::Value *storeMask, const Type *valueType, 00474 const Type *ptrType); 00475 00476 /** Copy count bytes of memory from the location pointed to by src to 00477 the location pointed to by dest. (src and dest must not be 00478 overlapping.) */ 00479 void MemcpyInst(llvm::Value *dest, llvm::Value *src, llvm::Value *count, 00480 llvm::Value *align = NULL); 00481 00482 void BranchInst(llvm::BasicBlock *block); 00483 void BranchInst(llvm::BasicBlock *trueBlock, llvm::BasicBlock *falseBlock, 00484 llvm::Value *test); 00485 00486 /** This convenience method maps to an llvm::ExtractElementInst if the 00487 given value is a llvm::VectorType, and to an llvm::ExtractValueInst 00488 otherwise. */ 00489 llvm::Value *ExtractInst(llvm::Value *v, int elt, const char *name = NULL); 00490 00491 /** This convenience method maps to an llvm::InsertElementInst if the 00492 given value is a llvm::VectorType, and to an llvm::InsertValueInst 00493 otherwise. */ 00494 llvm::Value *InsertInst(llvm::Value *v, llvm::Value *eltVal, int elt, 00495 const char *name = NULL); 00496 00497 llvm::PHINode *PhiNode(llvm::Type *type, int count, 00498 const char *name = NULL); 00499 llvm::Instruction *SelectInst(llvm::Value *test, llvm::Value *val0, 00500 llvm::Value *val1, const char *name = NULL); 00501 00502 /** Emits IR to do a function call with the given arguments. If the 00503 function type is a varying function pointer type, its full type 00504 must be provided in funcType. funcType can be NULL if func is a 00505 uniform function pointer. */ 00506 llvm::Value *CallInst(llvm::Value *func, const FunctionType *funcType, 00507 const std::vector<llvm::Value *> &args, 00508 const char *name = NULL); 00509 00510 /** This is a convenience method that issues a call instruction to a 00511 function that takes just a single argument. */ 00512 llvm::Value *CallInst(llvm::Value *func, const FunctionType *funcType, 00513 llvm::Value *arg, const char *name = NULL); 00514 00515 /** This is a convenience method that issues a call instruction to a 00516 function that takes two arguments. */ 00517 llvm::Value *CallInst(llvm::Value *func, const FunctionType *funcType, 00518 llvm::Value *arg0, llvm::Value *arg1, 00519 const char *name = NULL); 00520 00521 /** Launch an asynchronous task to run the given function, passing it 00522 he given argument values. */ 00523 llvm::Value *LaunchInst(llvm::Value *callee, 00524 std::vector<llvm::Value *> &argVals, 00525 llvm::Value *launchCount); 00526 00527 void SyncInst(); 00528 00529 llvm::Instruction *ReturnInst(); 00530 /** @} */ 00531 00532 private: 00533 /** Pointer to the Function for which we're currently generating code. */ 00534 Function *function; 00535 00536 /** LLVM function representation for the current function. */ 00537 llvm::Function *llvmFunction; 00538 00539 /** The basic block into which we add any alloca instructions that need 00540 to go at the very start of the function. */ 00541 llvm::BasicBlock *allocaBlock; 00542 00543 /** The current basic block into which we're emitting new 00544 instructions */ 00545 llvm::BasicBlock *bblock; 00546 00547 /** Pointer to stack-allocated memory that stores the current value of 00548 the full program mask. */ 00549 llvm::Value *fullMaskPointer; 00550 00551 /** Pointer to stack-allocated memory that stores the current value of 00552 the program mask representing varying control flow within the 00553 function. */ 00554 llvm::Value *internalMaskPointer; 00555 00556 /** Value of the program mask when the function starts execution. */ 00557 llvm::Value *functionMaskValue; 00558 00559 /** Current source file position; if debugging information is being 00560 generated, this position is used to set file/line information for 00561 instructions. */ 00562 SourcePos currentPos; 00563 00564 /** Source file position where the function definition started. Used 00565 for error messages and debugging symbols. */ 00566 SourcePos funcStartPos; 00567 00568 /** If currently in a loop body, the value of the mask at the start of 00569 the loop. */ 00570 llvm::Value *loopMask; 00571 00572 /** If currently in a loop body or switch statement, this is a pointer 00573 to memory to store a mask value that represents which of the lanes 00574 have executed a 'break' statement. If we're not in a loop body or 00575 switch, this should be NULL. */ 00576 llvm::Value *breakLanesPtr; 00577 00578 /** Similar to breakLanesPtr, if we're inside a loop, this is a pointer 00579 to memory to record which of the program instances have executed a 00580 'continue' statement. */ 00581 llvm::Value *continueLanesPtr; 00582 00583 /** If we're inside a loop or switch statement, this gives the basic 00584 block immediately after the current loop or switch, which we will 00585 jump to if all of the lanes have executed a break statement or are 00586 otherwise done with it. */ 00587 llvm::BasicBlock *breakTarget; 00588 00589 /** If we're inside a loop, this gives the block to jump to if all of 00590 the running lanes have executed a 'continue' statement. */ 00591 llvm::BasicBlock *continueTarget; 00592 00593 /** @name Switch statement state 00594 00595 These variables store various state that's active when we're 00596 generating code for a switch statement. They should all be NULL 00597 outside of a switch. 00598 @{ 00599 */ 00600 00601 /** The value of the expression used to determine which case in the 00602 statements after the switch to execute. */ 00603 llvm::Value *switchExpr; 00604 00605 /** Map from case label numbers to the basic block that will hold code 00606 for that case. */ 00607 const std::vector<std::pair<int, llvm::BasicBlock *> > *caseBlocks; 00608 00609 /** The basic block of code to run for the "default" label in the 00610 switch statement. */ 00611 llvm::BasicBlock *defaultBlock; 00612 00613 /** For each basic block for the code for cases (and the default label, 00614 if present), this map gives the basic block for the immediately 00615 following case/default label. */ 00616 const std::map<llvm::BasicBlock *, llvm::BasicBlock *> *nextBlocks; 00617 00618 /** Records whether the switch condition was uniform; this is a 00619 distinct notion from whether the switch represents uniform or 00620 varying control flow; we may have varying control flow from a 00621 uniform switch condition if there is a 'break' inside the switch 00622 that's under varying control flow. */ 00623 bool switchConditionWasUniform; 00624 /** @} */ 00625 00626 /** A pointer to memory that records which of the program instances 00627 have executed a 'return' statement (and are thus really truly done 00628 running any more instructions in this functions. */ 00629 llvm::Value *returnedLanesPtr; 00630 00631 /** A pointer to memory to store the return value for the function. 00632 Since difference program instances may execute 'return' statements 00633 at different times, we need to accumulate the return values as they 00634 come in until we return for real. */ 00635 llvm::Value *returnValuePtr; 00636 00637 /** The CFInfo structure records information about a nesting level of 00638 control flow. This vector lets us see what control flow is going 00639 around outside the current position in the function being 00640 emitted. */ 00641 std::vector<CFInfo *> controlFlowInfo; 00642 00643 /** DIFile object corresponding to the source file where the current 00644 function was defined (used for debugging info). */ 00645 llvm::DIFile diFile; 00646 00647 /** DISubprogram corresponding to this function (used for debugging 00648 info). */ 00649 llvm::DISubprogram diSubprogram; 00650 00651 /** These correspond to the current set of nested scopes in the 00652 function. */ 00653 std::vector<llvm::DILexicalBlock> debugScopes; 00654 00655 /** True if a 'launch' statement has been encountered in the function. */ 00656 bool launchedTasks; 00657 00658 /** This is a pointer to a void * that is passed to the ISPCLaunch(), 00659 ISPCAlloc(), and ISPCSync() routines as a handle to the group ot 00660 tasks launched from the current function. */ 00661 llvm::Value *launchGroupHandlePtr; 00662 00663 /** Nesting count of the number of times calling code has disabled (and 00664 not yet reenabled) gather/scatter performance warnings. */ 00665 int disableGSWarningCount; 00666 00667 std::map<std::string, llvm::BasicBlock *> labelMap; 00668 00669 static bool initLabelBBlocks(ASTNode *node, void *data); 00670 00671 llvm::Value *pointerVectorToVoidPointers(llvm::Value *value); 00672 static void addGSMetadata(llvm::Value *inst, SourcePos pos); 00673 bool ifsInCFAllUniform(int cfType) const; 00674 void jumpIfAllLoopLanesAreDone(llvm::BasicBlock *target); 00675 llvm::Value *emitGatherCallback(llvm::Value *lvalue, llvm::Value *retPtr); 00676 00677 llvm::Value *applyVaryingGEP(llvm::Value *basePtr, llvm::Value *index, 00678 const Type *ptrType); 00679 00680 void restoreMaskGivenReturns(llvm::Value *oldMask); 00681 void addSwitchMaskCheck(llvm::Value *mask); 00682 bool inSwitchStatement() const; 00683 llvm::Value *getMaskAtSwitchEntry(); 00684 00685 CFInfo *popCFState(); 00686 00687 void scatter(llvm::Value *value, llvm::Value *ptr, const Type *valueType, 00688 const Type *ptrType, llvm::Value *mask); 00689 void maskedStore(llvm::Value *value, llvm::Value *ptr, const Type *ptrType, 00690 llvm::Value *mask); 00691 void storeUniformToSOA(llvm::Value *value, llvm::Value *ptr, 00692 llvm::Value *mask, const Type *valueType, 00693 const PointerType *ptrType); 00694 llvm::Value *loadUniformFromSOA(llvm::Value *ptr, llvm::Value *mask, 00695 const PointerType *ptrType, const char *name); 00696 00697 llvm::Value *gather(llvm::Value *ptr, const PointerType *ptrType, 00698 llvm::Value *mask, const char *name); 00699 00700 llvm::Value *addVaryingOffsetsIfNeeded(llvm::Value *ptr, const Type *ptrType); 00701 }; 00702 00703 #endif // ISPC_CTX_H
1.7.5.1